Board logo

标题: 读写SPI FLASH--驱动部分(3) [打印本页]

作者: yuyang911220    时间: 2015-2-27 09:06     标题: 读写SPI FLASH--驱动部分(3)

#define BUF_SIZE 512

/******************************************************************************

  Description:
     This structure contains data that is used for most driver operations.

*****************************************************************************/
typedef struct _spi_char
{
    dev_t dev;
    struct cdev cdev;
    uint32_t bios_ctrl_addr;
    void *mem_virt; /* base virtual address  */
    uint32_t mem_base;
    uint32_t mem_size;
    uint32_t mem_ready; /* ioremap successfully? */
    uint8_t iomem_ready;
    uint32_t fp; /* file pointer */
} spi_char_t;

typedef struct _ioctl_arg
{
    uint32_t type;
    union
    {
        struct
        {
            uint32_t start;
            uint32_t end;
        }erase_range;
    }data;   
}ioctl_arg_t;


#ifdef SPI_DEBUG
#define SPI_DBG(args...) printk(KERN_DEBUG "SPI: " args)
#else
#define SPI_DBG(args...)
#endif

#define SPI_ERR(args...) printk(KERN_ERR "SPI: " args)

#endif /* _SPI_H_ */


/*****************************spi.c*************************************/
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/pci.h>
#include <asm/uaccess.h>
#include <linux/ioport.h>
#include <linux/delay.h>
#include <linux/ioctl.h>
#include <linux/cdev.h>
#include "spi.h"

static spi_char_t spi_char;
MODULE_LICENSE("Dual BSD/GPL");

static uint8_t get_flash_status(void);
static uint8_t set_flash_status(uint8_t value);

static int  is_flash_protection(void);
static int disable_flash_protection(void);
static int is_bios_protection(void);
static int disable_bios_protection(void);

static int erase_4K_sector(unsigned int idx);
static int erase_chip(void);
static uint8_t read_byte(uint32_t addr);
static int write_byte(uint32_t addr, uint8_t byte);


/*****************************************************************************
  Description:
    This function is called when the driver is unloaded from memory.   

  Parameters:
    none

  Returns:
      none
******************************************************************************/
void spi_cleanup(void)
{
    //remove cdev struct from system
    cdev_del(&spi_char.cdev);

    //unregister driver module
    unregister_chrdev_region(spi_char.dev, 1);

    //release the reserved IO memory space
    if ( spi_char.mem_ready )
    {
          iounmap(spi_char.mem_virt);
    }
    if(spi_char.iomem_ready)
    {
         release_mem_region(spi_char.mem_base, SPI_MEM_SIZE);
    }

    SPI_DBG("%s:spi_cleanup-Driver unload complete/n", DRIVERNAME);
}


/*****************************************************************************
  Description:
    This function is called when the driver interface is opened     

  Parameters:
    none

  Returns:
      0 => success
    < 0 => error
******************************************************************************/
int spi_open(struct inode *inode, struct file *filp)
{
    SPI_DBG("%s:spi_open-module opened/n", DRIVERNAME);
    spi_char.fp = 0;
    SPI_DBG("spi_char.fp = %08x/n", spi_char.fp);
    return 0;
}




欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) Powered by Discuz! 7.0.0