spi_flash_w25qxx_mtd.c 9.8 KB

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