123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546 |
- #include "board.h"
- #include <string.h>
- #include <stdio.h>
- #include "hw_spi_flash.h"
- static spi_flash_chip flash_table[] =
- {
- { "W25Q40BV", SFUD_MF_ID_WINBOND, 0x40, 0x13, 512L * 1024L, SFUD_WM_PAGE_256B, 4096, 0x20 },
- { "W25Q16BV", SFUD_MF_ID_WINBOND, 0x40, 0x15, 2L * 1024L * 1024L, SFUD_WM_PAGE_256B, 4096, 0x20 },
- { "W25Q64DW", SFUD_MF_ID_WINBOND, 0x40, 0x17, 8L * 1024L * 1024L, SFUD_WM_PAGE_256B, 4096, 0x20 },
- { "W25Q128BV", SFUD_MF_ID_WINBOND, 0x40, 0x18, 16L * 1024L * 1024L, SFUD_WM_PAGE_256B, 4096, 0x20 },
- { "W25Q256FV", SFUD_MF_ID_WINBOND, 0x40, 0x19, 32L * 1024L * 1024L, SFUD_WM_PAGE_256B, 4096, 0x20 },
- { "M25P32", SFUD_MF_ID_MICRON, 0x20, 0x16, 4L * 1024L * 1024L, SFUD_WM_PAGE_256B, 64L * 1024L, 0xD8 },
- { "M25P80", SFUD_MF_ID_MICRON, 0x20, 0x14, 1L * 1024L * 1024L, SFUD_WM_PAGE_256B, 64L * 1024L, 0xD8 },
- { "M25P40", SFUD_MF_ID_MICRON, 0x20, 0x13, 512L * 1024L, SFUD_WM_PAGE_256B, 64L * 1024L, 0xD8 },
- { "EN25Q32B", SFUD_MF_ID_EON, 0x30, 0x16, 4L * 1024L * 1024L, SFUD_WM_PAGE_256B, 4096, 0x20 },
- { "GD25Q64B", SFUD_MF_ID_GIGADEVICE, 0x40, 0x17, 8L * 1024L * 1024L, SFUD_WM_PAGE_256B, 4096, 0x20 },
- { "GD25Q16B", SFUD_MF_ID_GIGADEVICE, 0x40, 0x15, 2L * 1024L * 1024L, SFUD_WM_PAGE_256B, 4096, 0x20 },
- { "S25FL216K", SFUD_MF_ID_CYPRESS, 0x40, 0x15, 2L * 1024L * 1024L, SFUD_WM_PAGE_256B, 4096, 0x20 },
- { "S25FL032P", SFUD_MF_ID_CYPRESS, 0x02, 0x15, 4L * 1024L * 1024L, SFUD_WM_PAGE_256B, 4096, 0x20 },
- { "A25L080", SFUD_MF_ID_AMIC, 0x30, 0x14, 1L * 1024L * 1024L, SFUD_WM_PAGE_256B, 4096, 0x20 },
- };
- #define SPI_FLASH_ReadStatusReg1 0x05
- #define SPI_FLASH_ReadStatusReg2 0x35
- #define SPI_FLASH_ReadStatusReg3 0x15
- #define SPI_FLASH_WriteStatusReg1 0x01
- #define SPI_FLASH_WriteStatusReg2 0x31
- #define SPI_FLASH_WriteStatusReg3 0x11
- static hw_spi_flash_device_t device_map[HW_SPI_FLASH_DEVICE_NUMBER] = {
- { 0 }
- };
- /**
- * 将地址填充进数组
- *
- * @author LXZ (011621)
- *
- * @param index
- * @param addr
- * @param array
- */
- static void make_adress_byte_array(int index, uint32_t addr, uint8_t *array) {
- uint8_t i = 0;
- if (device_map[index].addr_in_4_byte) {
- array[i++] = addr >> 24;
- }
- array[i++] = addr >> 16;
- array[i++] = addr >> 8;
- array[i++] = addr >> 0;
- }
- static void set_4_byte_address_mode(int index, uint8_t enabled) {
- uint8_t cmd;
- int device = device_map[index].device;
- spi_flash_set_write_enable(index, 1);
- if (enabled) {
- cmd = 0xB7;
- }
- else {
- cmd = 0xE9;
- }
- hw_spi_device_take(device);
- hw_spi_readwrite(device, &cmd, 0, 1);
- hw_spi_device_release(device);
- device_map[index].addr_in_4_byte = enabled ? 1 : 0;
- }
- /**
- * 按页或者字节写入
- *
- * @author LXZ (011821)
- *
- * @param flash
- * @param addr
- * @param size
- * @param write_gran
- * @param data
- *
- * @return int
- */
- static int page256_or_1_byte_write(int index, uint32_t addr, int size, uint16_t write_gran,
- const uint8_t *data) {
- int device = device_map[index].device;
- static uint8_t cmd_data[5];
- uint8_t cmd_size;
- int data_size;
- if (write_gran != 1 && write_gran != 256) {
- return -1;
- }
- //检测地址范围
- if (addr + size > device_map[index].chip.capacity) {
- return -2;
- }
- /* loop write operate. write unit is write granularity */
- while (size) {
- spi_flash_set_write_enable(index, 1);
- cmd_data[0] = 0x02;
- make_adress_byte_array(index, addr, &cmd_data[1]);
- cmd_size = device_map[index].addr_in_4_byte ? 5 : 4;
- /* make write align and calculate next write address */
- if (addr % write_gran != 0) {
- if (size > write_gran - (addr % write_gran)) {
- data_size = write_gran - (addr % write_gran);
- }
- else {
- data_size = size;
- }
- }
- else {
- if (size > write_gran) {
- data_size = write_gran;
- }
- else {
- data_size = size;
- }
- }
- size -= data_size;
- addr += data_size;
- hw_spi_device_take(device);
- hw_spi_readwrite(device, cmd_data, 0, cmd_size);
- hw_spi_readwrite(device, (uint8_t *)data, 0, data_size);
- hw_spi_device_release(device);
- spi_flash_wait_busy(index);
- data += data_size;
- }
- spi_flash_set_write_enable(index, 0);
- return size;
- }
- //==============================================SPI FLASH芯片驱动======================================//
- /**
- * 芯片写状态
- *
- * @author LXZ (011621)
- *
- * @param index
- * @param is_volatile
- * @param status
- *
- * @return int
- */
- int spi_flash_write_status(int index, uint8_t reg_no, uint8_t status) {
- uint8_t cmd_data[2];
- int device = device_map[index].device;
- switch (reg_no) {
- case 2:
- cmd_data[0] = SPI_FLASH_WriteStatusReg2;
- break;
- case 3:
- cmd_data[0] = SPI_FLASH_WriteStatusReg3;
- break;
- default:
- cmd_data[0] = SPI_FLASH_WriteStatusReg1;
- break;
- }
- cmd_data[1] = status;
- hw_spi_device_take(device);
- hw_spi_readwrite(device, cmd_data, 0, 2);
- hw_spi_device_release(device);
- return 0;
- }
- /**
- * W25Q芯片读取
- *
- * @author LXZ (011621)
- *
- * @param index
- * @param status
- *
- * @return int
- */
- uint8_t spi_flash_read_status(int index, uint8_t reg_no) {
- uint32_t i;
- uint8_t cmd;
- uint8_t status;
- switch (reg_no) {
- case 2:
- cmd = SPI_FLASH_ReadStatusReg2;
- break;
- case 3:
- cmd = SPI_FLASH_ReadStatusReg3;
- break;
- default:
- cmd = SPI_FLASH_ReadStatusReg1;
- }
- int device = device_map[index].device;
- hw_spi_device_take(device);
- hw_spi_readwrite(device, &cmd, 0, 1);
- hw_spi_readwrite(device, 0, &status, 1);
- hw_spi_device_release(device);
- return status;
- }
- /**
- * 芯片等待操作完成
- *
- * @author LXZ (011621)
- *
- * @param index
- *
- * @return int
- */
- int spi_flash_wait_busy(int index) {
- while ((spi_flash_read_status(index, 1) & 0x01) == 0x01);
- return 0;
- }
- /**
- * 设置写保护开关
- *
- * @author LXZ (011621)
- *
- * @param index
- * @param enable
- *
- * @return int
- */
- int spi_flash_set_write_enable(int index, uint8_t enabled) {
- uint8_t cmd;
- int device = device_map[index].device;
- if (enabled) {
- cmd = 0x06;
- }
- else {
- cmd = 0x04;
- }
- hw_spi_device_take(device);
- hw_spi_readwrite(device, &cmd, 0, 1);
- hw_spi_device_release(device);
- return 0;
- }
- /**
- * 芯片复位
- *
- * @author LXZ (011621)
- *
- * @param index
- *
- * @return int
- */
- int spi_flash_reset(int index) {
- uint8_t cmd_data[2];
- int device = device_map[index].device;
- cmd_data[0] = 0x66;
- hw_spi_device_take(device);
- hw_spi_readwrite(device, cmd_data, 0, 1);
- hw_spi_device_release(device);
- spi_flash_wait_busy(index);
- cmd_data[1] = 0x99;
- hw_spi_device_take(device);
- hw_spi_readwrite(device, &cmd_data[1], 0, 1);
- hw_spi_device_release(device);
- spi_flash_wait_busy(index);
- return 0;
- }
- int spi_flash_read_jedec_id(int index) {
- int i;
- int device = device_map[index].device;
- uint8_t cmd_data[2], recv_data[3];
- spi_flash_wait_busy(index);
- cmd_data[0] = 0x9F;
- hw_spi_device_take(device);
- hw_spi_readwrite(device, cmd_data, 0, 1);
- hw_spi_readwrite(device, 0, recv_data, sizeof(recv_data));
- hw_spi_readwrite(device, 0, 0, 2);
- cmd_data[0] = 0x90;
- hw_spi_readwrite(device, cmd_data, 0, 1);
- hw_spi_readwrite(device, 0, 0, 5);
- hw_spi_device_release(device);
- for (i = 0; i < sizeof(flash_table) / sizeof(spi_flash_chip); i++) {
- if (flash_table[i].capacity_id == recv_data[2] &&
- flash_table[i].mf_id == recv_data[0] &&
- flash_table[i].type_id == recv_data[1]) {
- device_map[index].chip = flash_table[i];
- device_map[index].init_ok = 1;
- device_map[index].geometry.sector_count = device_map[index].chip.capacity / device_map[index].chip.erase_gran;
- device_map[index].geometry.bytes_per_sector = device_map[index].chip.erase_gran;
- device_map[index].geometry.block_size = device_map[index].geometry.sector_count;
- break;
- }
- }
- return 0;
- }
- /**
- * 向指定地址写入数据
- *
- * @author LXZ (011621)
- *
- * @param index
- * @param addr
- * @param dat
- * @param size
- *
- * @return int
- */
- int spi_flash_write(int index, uint32_t addr, const uint8_t *data, int size) {
- return page256_or_1_byte_write(index, addr, size, 256, data);
- }
- /**
- * 向指定地址读取数据
- *
- * @author LXZ (011621)
- *
- * @param index
- * @param addr
- * @param dat
- * @param size
- *
- * @return int
- */
- int spi_flash_read(int index, uint32_t addr, uint8_t *data, int size) {
- int device = device_map[index].device;
- uint8_t cmd_data[5], cmd_size;
- //检测是否超过了容量范围
- if (addr + size > device_map[index].chip.capacity) {
- return -1;
- }
- spi_flash_wait_busy(index);
- cmd_data[0] = 0x03;
- make_adress_byte_array(index, addr, &cmd_data[1]);
- cmd_size = device_map[index].addr_in_4_byte ? 5 : 4;
- hw_spi_device_take(device);
- hw_spi_readwrite(device, cmd_data, 0, cmd_size);
- hw_spi_readwrite(device, 0, data, size);
- hw_spi_device_release(device);
- return size;
- }
- /**
- * 擦除指定地址并写入
- *
- * @author LXZ (011821)
- *
- * @param index
- * @param addr
- * @param size
- * @param data
- *
- * @return int
- */
- int spi_flash_erase_write(int index, uint32_t addr, const uint8_t *data, int size) {
- spi_flash_erase(index, addr, size);
- spi_flash_write(index, addr, data, size);
- return 0;
- }
- /**
- * 擦除指定地址
- *
- * @author LXZ (011621)
- *
- * @param index
- * @param addr
- * @param size
- *
- * @return int
- */
- int spi_flash_erase(int index, uint32_t addr, int size) {
- extern int sfud_sfdp_get_suitable_eraser(int index, uint32_t addr, int erase_size);
- int device = device_map[index].device;
- uint8_t cmd_data[5], cmd_size, cur_erase_cmd;
- size_t cur_erase_size;
- /* check the flash address bound */
- if (addr + size > device_map[index].chip.capacity) {
- return -1;
- }
- if (addr == 0 && size == device_map[index].chip.capacity) {
- return spi_flash_erase_chip(index);
- }
- cur_erase_cmd = device_map[index].chip.erase_gran_cmd;
- cur_erase_size = device_map[index].chip.erase_gran;
- /* loop erase operate. erase unit is erase granularity */
- while (size) {
- spi_flash_set_write_enable(index, 1);
- cmd_data[0] = cur_erase_cmd;
- make_adress_byte_array(index, addr, &cmd_data[1]);
- cmd_size = device_map[index].addr_in_4_byte ? 5 : 4;
- hw_spi_device_take(device);
- hw_spi_readwrite(device, cmd_data, 0, cmd_size);
- hw_spi_device_release(device);
- spi_flash_wait_busy(index);
- /* make erase align and calculate next erase address */
- if (addr % cur_erase_size != 0) {
- if (size > cur_erase_size - (addr % cur_erase_size)) {
- size -= cur_erase_size - (addr % cur_erase_size);
- addr += cur_erase_size - (addr % cur_erase_size);
- }
- else {
- goto __exit;
- }
- }
- else {
- if (size > cur_erase_size) {
- size -= cur_erase_size;
- addr += cur_erase_size;
- }
- else {
- goto __exit;
- }
- }
- }
- __exit:
- spi_flash_set_write_enable(index, 0);
- return 0;
- }
- /**
- * 整片擦除
- *
- * @author LXZ (011621)
- *
- * @param index
- * @param addr
- * @param size
- *
- * @return int
- */
- int spi_flash_erase_chip(int index) {
- uint8_t cmd_data[4];
- int device = device_map[index].device;
- spi_flash_set_write_enable(index, 1);
- cmd_data[0] = 0xC7;
- hw_spi_device_take(device);
- hw_spi_readwrite(device, cmd_data, 0, 1);
- hw_spi_device_release(device);
- spi_flash_wait_busy(index);
- spi_flash_set_write_enable(index, 0);
- return 0;
- }
- /**
- * 挂载SPI FLASH
- *
- * @author LXZ (011621)
- *
- * @param port 绑定的设备
- * @param index FLASH编号
- */
- int hw_spi_flash_attach(int index, int device) {
- //读取指定设备上是否有设备
- device_map[index].device = device;
- device_map[index].retry_time = 10;
- printf("scan device int bus %d...\n", device);
- //读取芯片ID
- spi_flash_read_jedec_id(index);
- if (device_map[index].init_ok) {
- printf("Find flash %d:%d: %s. Size is %d bytes.\n", device, index, device_map[index].chip.name,
- device_map[index].chip.capacity);
- spi_flash_reset(index);
- if (device_map[index].chip.capacity > (1L << 24)) {
- set_4_byte_address_mode(index, 1);
- device_map[index].addr_in_4_byte = 1;
- }
- else {
- set_4_byte_address_mode(index, 0);
- }
- }
- else
- {
- printf("Can't Find flash chip at port %d.\n", device);
- }
- return device_map[index].init_ok;
- }
|