裝置樹學習之(十)spi flash

2021-07-31 12:36:15 字數 4838 閱讀 4539

開發板:tiny4412sdk + s702 + 4gb flash 

要移植的核心版本:linux-4.4.0 (支援device tree) 

u-boot版本:友善之臂自帶的 u-boot 2010.12 

busybox版本:busybox 1.25

目標: 

驅動外接的8m的 spi flash,註冊為塊裝置。

裝置樹:

&spi_0 ;

};};

這裡指定的 reg = <0> 表示的該 spi 裝置引用第乙個 cs-gpios 

spi-max-frequency = <10000000>;表示最大速率

**:

#include 

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

/* 參考:

* drivers\mtd\devices\mtdram.c

* drivers/mtd/devices/m25p80.c

*/static

struct spi_device *spi_flash;

void spiflashreadid(int *pmid, int *pdid)

static

void spiflashwriteenable(int enable)

static

unsigned

char spiflashreadstatusreg1(void)

static

unsigned

char spiflashreadstatusreg2(void)

static

void spiflashwaitwhenbusy(void)

}static

void spiflashwritestatusreg(unsigned

char reg1, unsigned

char reg2)

static

void spiflashclearprotectforstatusreg(void)

static

void spiflashclearprotectfordata(void)

/* erase 4k */

void spiflasherasesector(unsigned

int addr)

/* program */

void spiflashprogram(unsigned

int addr, unsigned

char *buf, int len)

, ,

};struct spi_message m;

//printk("%s\n",__func__);

tx_buf[0] = 0x02;

tx_buf[1] = addr >> 16;

tx_buf[2] = addr >> 8;

tx_buf[3] = addr & 0xff;

spiflashwriteenable(1);

spi_message_init(&m);

spi_message_add_tail(&t[0], &m);

spi_message_add_tail(&t[1], &m);

spi_sync(spi_flash, &m);

spiflashwaitwhenbusy();

}void spiflashread(unsigned

int addr, unsigned

char *buf, int len)

, ,

};struct spi_message m;

//printk("%s\n",__func__);

tx_buf[0] = 0x03;

tx_buf[1] = addr >> 16;

tx_buf[2] = addr >> 8;

tx_buf[3] = addr & 0xff;

spi_message_init(&m);

spi_message_add_tail(&t[0], &m);

spi_message_add_tail(&t[1], &m);

spi_sync(spi_flash, &m);

}static

void spiflashinit(void)

/* 構造註冊乙個mtd_info

* mtd_device_register(master, parts, nr_parts)

* */

/* 首先: 構造註冊spi_driver

* 然後: 在spi_driver的probe函式裡構造註冊mtd_info

*/static

struct mtd_info spi_flash_dev;

static

int spi_flash_erase(struct mtd_info *mtd, struct erase_info *instr)

for (len = 0; len < instr->len; len += 4096)

instr->state = mtd_erase_done;

mtd_erase_callback(instr);

return0;}

static

int spi_flash_read(struct mtd_info *mtd, loff_t from, size_t len,

size_t *retlen, u_char *buf)

static

int spi_flash_write(struct mtd_info *mtd, loff_t to, size_t len,

size_t *retlen, const u_char *buf)

if (len <= 256)

else

if (len % 256 != 0)

}*retlen = len;

return0;}

static

int spi_flash_probe(struct spi_device *spi)

printk("%s\n",__func__);

//s3c2410_gpio_cfgpin(spi->chip_select, s3c2410_gpio_output);

spiflashinit();

spiflashreadid(&mid, &did);

printk("spi flash id: %02x %02x\n", mid, did);

memset(&spi_flash_dev, 0, sizeof(spi_flash_dev));

//setup the mtd structure

spi_flash_dev.name = "spi_flash";

spi_flash_dev.type = mtd_norflash;

spi_flash_dev.flags = mtd_cap_norflash;

spi_flash_dev.size = 0x800000;

spi_flash_dev.writesize = 1;

spi_flash_dev.writebufsize = 4096;

spi_flash_dev.erasesize = 4096;

spi_flash_dev.owner = this_module;

spi_flash_dev._erase = spi_flash_erase;

spi_flash_dev._read = spi_flash_read;

spi_flash_dev._write = spi_flash_write;

mtd_device_register(&spi_flash_dev, null, 0);

return0;}

static

int spi_flash_remove(struct spi_device *spi)

static

const

struct of_device_id spi_flash_ids = ,

};static

struct spi_driver spi_flash_drv =

, .probe = spi_flash_probe,

.remove = spi_flash_remove,

};static

int spi_flash_init(void)

static

void spi_flash_exit(void)

module_init(spi_flash_init);

module_exit(spi_flash_exit);

module_license("gpl");

歡迎關注並加入物聯網行業聯盟,積累行業人脈和資源。

裝置樹學習之(十)spi flash

開發板 tiny4412sdk s702 4gb flash 要移植的核心版本 linux 4.4.0 支援device tree u boot版本 友善之臂自帶的 u boot 2010.12 busybox版本 busybox 1.25 目標 驅動外接的8m的 spi flash,註冊為塊裝置。...

裝置樹學習(二)(裝置樹的規範)

dts v1 memory reservations 格式為 memreserve 從上面檔案布局我們能看到大概分為以下幾部分,我們依次介紹 1 dts v1 表示dts檔案的版本 2 memory reservations 格式為 memreserve 表示留給自己使用的記憶體。即核心不能使用這部...

裝置樹學習總結

標籤 label 可以為節點建立標籤,別的地方引用時可以用標籤。標籤為可選項。節點 node 每個節點必須有乙個 名稱 裝置位址 形式的名字,為可選項,unit address一般是記憶體位址。名稱 name 乙個位元組大小 就是乙個不超過31位的簡單 ascii 字串,節點的命名應該根據它所體現的...