spi_flash_w25qxx_mtd.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. #include <rtthread.h>
  2. #include <rtdevice.h>
  3. #include "spi_flash.h"
  4. #include "spi_flash_w25qxx_mtd.h"
  5. #include <stdint.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <stdlib.h>
  9. #define FLASH_DEBUG
  10. #ifdef FLASH_DEBUG
  11. #define FLASH_TRACE printf
  12. #else
  13. #define FLASH_TRACE(...)
  14. #endif /* #ifdef FLASH_DEBUG */
  15. /* JEDEC Manufacturer¡¯s ID */
  16. #define MF_ID (0xEF)
  17. /* JEDEC Device ID: Memory type and Capacity */
  18. #define MTC_W25Q80_BV (0x4014) /* W25Q80BV */
  19. #define MTC_W25Q16_BV_CL_CV (0x4015) /* W25Q16BV W25Q16CL W25Q16CV */
  20. #define MTC_W25Q16_DW (0x6015) /* W25Q16DW */
  21. #define MTC_W25Q32_BV (0x4016) /* W25Q32BV */
  22. #define MTC_W25Q32_DW (0x6016) /* W25Q32DW */
  23. #define MTC_W25Q64_BV_CV (0x4017) /* W25Q64BV W25Q64CV */
  24. #define MTC_W25Q64_DW (0x4017) /* W25Q64DW */
  25. #define MTC_W25Q128_BV (0x4018) /* W25Q128BV */
  26. #define MTC_W25Q256_FV (TBD) /* W25Q256FV */
  27. #define MTC_W25X80 (0x3014)
  28. /* command list */
  29. #define CMD_WRSR (0x01) /* Write Status Register */
  30. #define CMD_PP (0x02) /* Page Program */
  31. #define CMD_READ (0x03) /* Read Data */
  32. #define CMD_WRDI (0x04) /* Write Disable */
  33. #define CMD_RDSR1 (0x05) /* Read Status Register-1 */
  34. #define CMD_WREN (0x06) /* Write Enable */
  35. #define CMD_FAST_READ (0x0B) /* Fast Read */
  36. #define CMD_ERASE_4K (0x20) /* Sector Erase:4K */
  37. #define CMD_RDSR2 (0x35) /* Read Status Register-2 */
  38. #define CMD_ERASE_32K (0x52) /* 32KB Block Erase */
  39. #define CMD_JEDEC_ID (0x9F) /* Read JEDEC ID */
  40. #define CMD_ERASE_full (0xC7) /* Chip Erase */
  41. #define CMD_ERASE_64K (0xD8) /* 64KB Block Erase */
  42. #define CMD_MANU_ID (0x90)
  43. #define DUMMY (0xFF)
  44. #define FLASH_ERASE_CMD CMD_ERASE_4K
  45. #define FLASH_BLOCK_SIZE 4096
  46. #define FLASH_PAGE_SIZE 256
  47. static void w25qxx_lock(struct rt_mtd_nor_device *device)
  48. {
  49. struct spi_flash_mtd *mtd = (struct spi_flash_mtd *)device;
  50. rt_mutex_take(&mtd->lock, RT_WAITING_FOREVER);
  51. }
  52. static void w25qxx_unlock(struct rt_mtd_nor_device *device)
  53. {
  54. struct spi_flash_mtd *mtd = (struct spi_flash_mtd *)device;
  55. rt_mutex_release(&mtd->lock);
  56. }
  57. static rt_uint8_t w25qxx_read_status(struct rt_mtd_nor_device *device)
  58. {
  59. struct spi_flash_mtd *mtd = (struct spi_flash_mtd *)device;
  60. return rt_spi_sendrecv8(mtd->rt_spi_device, CMD_RDSR1);
  61. }
  62. static void w25qxx_wait_busy(struct rt_mtd_nor_device *device)
  63. {
  64. while( w25qxx_read_status(device) & (0x01));
  65. }
  66. static rt_err_t w25qxx_read_id(struct rt_mtd_nor_device *device)
  67. {
  68. rt_uint8_t cmd;
  69. rt_uint8_t id_recv[3];
  70. struct spi_flash_mtd *mtd = (struct spi_flash_mtd *)device;
  71. w25qxx_lock(device);
  72. cmd = 0xFF; /* reset SPI FLASH, cancel all cmd in processing. */
  73. rt_spi_send(mtd->rt_spi_device, &cmd, 1);
  74. cmd = CMD_WRDI;
  75. rt_spi_send(mtd->rt_spi_device, &cmd, 1);
  76. /* read flash id */
  77. cmd = CMD_JEDEC_ID;
  78. rt_spi_send_then_recv(mtd->rt_spi_device, &cmd, 1, id_recv, 3);
  79. w25qxx_unlock(device);
  80. return (rt_uint32_t)(id_recv[0] << 16) | (id_recv[1] << 8) | id_recv[2];
  81. }
  82. static rt_size_t w25qxx_read(struct rt_mtd_nor_device *device, rt_off_t offset, rt_uint8_t *buffer, rt_size_t length)
  83. {
  84. struct spi_flash_mtd *mtd = (struct spi_flash_mtd *)device;
  85. rt_uint8_t send_buffer[4];
  86. if((offset + length) > device->block_end * FLASH_BLOCK_SIZE)
  87. return 0;
  88. w25qxx_lock(device);
  89. send_buffer[0] = CMD_WRDI;
  90. rt_spi_send(mtd->rt_spi_device, send_buffer, 1);
  91. send_buffer[0] = CMD_READ;
  92. send_buffer[1] = (rt_uint8_t)(offset>>16);
  93. send_buffer[2] = (rt_uint8_t)(offset>>8);
  94. send_buffer[3] = (rt_uint8_t)(offset);
  95. rt_spi_send_then_recv(mtd->rt_spi_device,
  96. send_buffer, 4,
  97. buffer, length);
  98. w25qxx_unlock(device);
  99. return length;
  100. }
  101. static rt_size_t w25qxx_write(struct rt_mtd_nor_device *device, rt_off_t offset, const rt_uint8_t *buffer, rt_size_t length)
  102. {
  103. struct spi_flash_mtd *mtd = (struct spi_flash_mtd *)device;
  104. rt_uint8_t send_buffer[4];
  105. rt_uint8_t *write_ptr ;
  106. rt_size_t write_size,write_total;
  107. if((offset + length) > device->block_end * FLASH_BLOCK_SIZE)
  108. return 0;
  109. w25qxx_lock(device);
  110. send_buffer[0] = CMD_WREN;
  111. rt_spi_send(mtd->rt_spi_device, send_buffer, 1);
  112. w25qxx_wait_busy(device); // wait erase done.
  113. write_size = 0;
  114. write_total = 0;
  115. write_ptr = (rt_uint8_t *)buffer;
  116. while(write_total < length)
  117. {
  118. send_buffer[0] = CMD_WREN;
  119. rt_spi_send(mtd->rt_spi_device, send_buffer, 1);
  120. //write first page...
  121. send_buffer[0] = CMD_PP;
  122. send_buffer[1] = (rt_uint8_t)(offset >> 16);
  123. send_buffer[2] = (rt_uint8_t)(offset >> 8);
  124. send_buffer[3] = (rt_uint8_t)(offset);
  125. //address % FLASH_PAGE_SIZE + length
  126. if(((offset & (FLASH_PAGE_SIZE - 1)) + (length - write_total)) > FLASH_PAGE_SIZE)
  127. {
  128. write_size = FLASH_PAGE_SIZE - (offset & (FLASH_PAGE_SIZE - 1));
  129. }
  130. else
  131. {
  132. write_size = (length - write_total);
  133. }
  134. rt_spi_send_then_send(mtd->rt_spi_device,
  135. send_buffer, 4,
  136. write_ptr + write_total, write_size);
  137. w25qxx_wait_busy(device);
  138. offset += write_size;
  139. write_total += write_size;
  140. }
  141. send_buffer[0] = CMD_WRDI;
  142. rt_spi_send(mtd->rt_spi_device, send_buffer, 1);
  143. w25qxx_unlock(device);
  144. return length;
  145. }
  146. static rt_err_t w25qxx_erase_block(struct rt_mtd_nor_device *device, rt_off_t offset, rt_uint32_t length)
  147. {
  148. struct spi_flash_mtd *mtd = (struct spi_flash_mtd *)device;
  149. rt_uint8_t send_buffer[4];
  150. rt_uint32_t erase_size = 0;
  151. //offset must be ALIGN_DOWN to BLOCKSIZE
  152. if(offset != RT_ALIGN_DOWN(offset,FLASH_BLOCK_SIZE))
  153. return 0;
  154. if((offset + length) > device->block_end * FLASH_BLOCK_SIZE)
  155. return 0;
  156. /* check length must align to block size */
  157. if(length % device->block_size != 0)
  158. {
  159. rt_kprintf("param length = %d ,error\n",length);
  160. return 0;
  161. }
  162. w25qxx_lock(device);
  163. send_buffer[0] = CMD_WREN;
  164. rt_spi_send(mtd->rt_spi_device, send_buffer, 1);
  165. w25qxx_wait_busy(device); // wait erase done.
  166. while (erase_size < length)
  167. {
  168. send_buffer[0] = CMD_ERASE_4K;
  169. send_buffer[1] = (rt_uint8_t) (offset >> 16);
  170. send_buffer[2] = (rt_uint8_t) (offset >> 8);
  171. send_buffer[3] = (rt_uint8_t) (offset);
  172. rt_spi_send(mtd->rt_spi_device, send_buffer, 4);
  173. w25qxx_wait_busy(device); // wait erase done.
  174. erase_size += 4096;
  175. offset += 4096;
  176. }
  177. send_buffer[0] = CMD_WRDI;
  178. rt_spi_send(mtd->rt_spi_device, send_buffer, 1);
  179. w25qxx_unlock(device);
  180. return RT_EOK;
  181. }
  182. const static struct rt_mtd_nor_driver_ops w25qxx_mtd_ops =
  183. {
  184. w25qxx_read_id,
  185. w25qxx_read,
  186. w25qxx_write,
  187. w25qxx_erase_block,
  188. };
  189. rt_err_t w25qxx_mtd_init(const char *mtd_name,const char * spi_device_name)
  190. {
  191. rt_err_t result = RT_EOK;
  192. rt_uint32_t id;
  193. rt_uint8_t send_buffer[3];
  194. struct rt_spi_device* rt_spi_device;
  195. struct spi_flash_mtd* mtd = (struct spi_flash_mtd *)rt_malloc(sizeof(struct spi_flash_mtd));
  196. RT_ASSERT(mtd != RT_NULL);
  197. /* initialize mutex */
  198. if (rt_mutex_init(&mtd->lock, mtd_name, RT_IPC_FLAG_FIFO) != RT_EOK)
  199. {
  200. FLASH_TRACE("init mtd lock mutex failed\n");
  201. result = -RT_ENOSYS;
  202. goto _error_exit;
  203. }
  204. rt_spi_device = (struct rt_spi_device *)rt_device_find(spi_device_name);
  205. if(rt_spi_device == RT_NULL)
  206. {
  207. FLASH_TRACE("spi device %s not found!\r\n", spi_device_name);
  208. result = -RT_ENOSYS;
  209. goto _error_exit;
  210. }
  211. mtd->rt_spi_device = rt_spi_device;
  212. /* config spi */
  213. {
  214. struct rt_spi_configuration cfg;
  215. cfg.data_width = 8;
  216. cfg.mode = RT_SPI_MODE_0 | RT_SPI_MSB; /* SPI Compatible: Mode 0 and Mode 3 */
  217. cfg.max_hz = 20 * 1000 * 1000; /* 20 */
  218. rt_spi_configure(rt_spi_device, &cfg);
  219. }
  220. /* Init Flash device */
  221. {
  222. w25qxx_lock(&mtd->mtd_device);
  223. send_buffer[0] = CMD_WREN;
  224. rt_spi_send(mtd->rt_spi_device, send_buffer, 1);
  225. w25qxx_wait_busy(&mtd->mtd_device);
  226. send_buffer[0] = CMD_WRSR;
  227. send_buffer[1] = 0;
  228. send_buffer[2] = 0;
  229. rt_spi_send(mtd->rt_spi_device, send_buffer, 3);
  230. w25qxx_wait_busy(&mtd->mtd_device);
  231. w25qxx_unlock(&mtd->mtd_device);
  232. }
  233. id = w25qxx_read_id(&mtd->mtd_device);
  234. mtd->mtd_device.block_size = 4096;
  235. mtd->mtd_device.block_start = 0;
  236. switch(id & 0xFFFF)
  237. {
  238. case MTC_W25Q80_BV: /* W25Q80BV */
  239. mtd->mtd_device.block_end = 256;
  240. break;
  241. case MTC_W25Q16_BV_CL_CV: /* W25Q16BV W25Q16CL W25Q16CV */
  242. case MTC_W25Q16_DW: /* W25Q16DW */
  243. mtd->mtd_device.block_end = 512;
  244. break;
  245. case MTC_W25Q32_BV: /* W25Q32BV */
  246. case MTC_W25Q32_DW: /* W25Q32DW */
  247. mtd->mtd_device.block_end = 1024;
  248. break;
  249. case MTC_W25Q64_BV_CV: /* W25Q64BV W25Q64CV */
  250. mtd->mtd_device.block_end = 2048;
  251. break;
  252. case MTC_W25Q128_BV: /* W25Q128BV */
  253. mtd->mtd_device.block_end = 4086;
  254. break;
  255. }
  256. mtd->mtd_device.ops = &w25qxx_mtd_ops;
  257. rt_mtd_nor_register_device(mtd_name,&mtd->mtd_device);
  258. return RT_EOK;
  259. _error_exit:
  260. if(mtd != RT_NULL)
  261. rt_free(mtd);
  262. return result;
  263. }