flash_spi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. Copyright (C) 2021 Westberry Technology (ChangZhou) Corp., Ltd
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <string.h>
  15. #include "util.h"
  16. #include "wait.h"
  17. #include "debug.h"
  18. #include "timer.h"
  19. #include "flash_spi.h"
  20. #include "spi_master.h"
  21. /*
  22. The time-out time of spi flash transmission.
  23. */
  24. #ifndef EXTERNAL_FLASH_SPI_TIMEOUT
  25. # define EXTERNAL_FLASH_SPI_TIMEOUT 1000
  26. #endif
  27. /* ID comands */
  28. #define FLASH_CMD_RDID 0x9F /* RDID (Read Identification) */
  29. #define FLASH_CMD_RES 0xAB /* RES (Read Electronic ID) */
  30. #define FLASH_CMD_REMS 0x90 /* REMS (Read Electronic & Device ID) */
  31. /* register comands */
  32. #define FLASH_CMD_WRSR 0x01 /* WRSR (Write Status register) */
  33. #define FLASH_CMD_RDSR 0x05 /* RDSR (Read Status register) */
  34. /* READ comands */
  35. #define FLASH_CMD_READ 0x03 /* READ (1 x I/O) */
  36. #define FLASH_CMD_FASTREAD 0x0B /* FAST READ (Fast read data) */
  37. #define FLASH_CMD_DREAD 0x3B /* DREAD (1In/2 Out fast read) */
  38. /* Program comands */
  39. #define FLASH_CMD_WREN 0x06 /* WREN (Write Enable) */
  40. #define FLASH_CMD_WRDI 0x04 /* WRDI (Write Disable) */
  41. #define FLASH_CMD_PP 0x02 /* PP (page program) */
  42. /* Erase comands */
  43. #define FLASH_CMD_SE 0x20 /* SE (Sector Erase) */
  44. #define FLASH_CMD_BE 0xD8 /* BE (Block Erase) */
  45. #define FLASH_CMD_CE 0x60 /* CE (Chip Erase) hex code: 60 or C7 */
  46. /* Mode setting comands */
  47. #define FLASH_CMD_DP 0xB9 /* DP (Deep Power Down) */
  48. #define FLASH_CMD_RDP 0xAB /* RDP (Release form Deep Power Down) */
  49. /* Status register */
  50. #define FLASH_FLAG_WIP 0x01 /* Write in progress bit */
  51. #define FLASH_FLAG_WEL 0x02 /* Write enable latch bit */
  52. // #define DEBUG_FLASH_SPI_OUTPUT
  53. static bool spi_flash_start(void) { return spi_start(EXTERNAL_FLASH_SPI_SLAVE_SELECT_PIN, EXTERNAL_FLASH_SPI_LSBFIRST, EXTERNAL_FLASH_SPI_MODE, EXTERNAL_FLASH_SPI_CLOCK_DIVISOR); }
  54. static flash_status_t spi_flash_wait_while_busy(void) {
  55. uint32_t deadline = timer_read32() + EXTERNAL_FLASH_SPI_TIMEOUT;
  56. flash_status_t response = FLASH_STATUS_SUCCESS;
  57. uint8_t retval;
  58. do {
  59. bool res = spi_flash_start();
  60. if (!res) {
  61. dprint("Failed to start SPI! [spi flash wait while busy]\n");
  62. return FLASH_STATUS_ERROR;
  63. }
  64. spi_write(FLASH_CMD_RDSR);
  65. retval = (uint8_t)spi_read();
  66. spi_stop();
  67. if (timer_read32() >= deadline) {
  68. response = FLASH_STATUS_TIMEOUT;
  69. break;
  70. }
  71. } while (retval & FLASH_FLAG_WIP);
  72. return response;
  73. }
  74. static flash_status_t spi_flash_write_enable(void) {
  75. bool res = spi_flash_start();
  76. if (!res) {
  77. dprint("Failed to start SPI! [spi flash write enable]\n");
  78. return FLASH_STATUS_ERROR;
  79. }
  80. spi_write(FLASH_CMD_WREN);
  81. spi_stop();
  82. return FLASH_STATUS_SUCCESS;
  83. }
  84. static flash_status_t spi_flash_write_disable(void) {
  85. bool res = spi_flash_start();
  86. if (!res) {
  87. dprint("Failed to start SPI! [spi flash write disable]\n");
  88. return FLASH_STATUS_ERROR;
  89. }
  90. spi_write(FLASH_CMD_WRDI);
  91. spi_stop();
  92. return FLASH_STATUS_SUCCESS;
  93. }
  94. /* This function is used for read transfer, write transfer and erase transfer. */
  95. static flash_status_t spi_flash_transaction(uint8_t cmd, uint32_t addr, uint8_t *data, size_t len) {
  96. flash_status_t response = FLASH_STATUS_SUCCESS;
  97. uint8_t buffer[EXTERNAL_FLASH_ADDRESS_SIZE + 1];
  98. buffer[0] = cmd;
  99. for (int i = 0; i < EXTERNAL_FLASH_ADDRESS_SIZE; ++i) {
  100. buffer[EXTERNAL_FLASH_ADDRESS_SIZE - i] = addr & 0xFF;
  101. addr >>= 8;
  102. }
  103. bool res = spi_flash_start();
  104. if (!res) {
  105. dprint("Failed to start SPI! [spi flash transmit]\n");
  106. return FLASH_STATUS_ERROR;
  107. }
  108. response = spi_transmit(buffer, sizeof(buffer));
  109. if ((!response) && (data != NULL)) {
  110. switch (cmd) {
  111. case FLASH_CMD_READ:
  112. response = spi_receive(data, len);
  113. break;
  114. case FLASH_CMD_PP:
  115. response = spi_transmit(data, len);
  116. break;
  117. default:
  118. response = FLASH_STATUS_ERROR;
  119. break;
  120. }
  121. }
  122. spi_stop();
  123. return response;
  124. }
  125. void flash_init(void) { spi_init(); }
  126. flash_status_t flash_erase_chip(void) {
  127. flash_status_t response = FLASH_STATUS_SUCCESS;
  128. /* Wait for the write-in-progress bit to be cleared. */
  129. response = spi_flash_wait_while_busy();
  130. if (response != FLASH_STATUS_SUCCESS) {
  131. dprint("Failed to check WIP flag! [spi flash erase chip]\n");
  132. return response;
  133. }
  134. /* Enable writes. */
  135. response = spi_flash_write_enable();
  136. if (response != FLASH_STATUS_SUCCESS) {
  137. dprint("Failed to write-enable! [spi flash erase chip]\n");
  138. return response;
  139. }
  140. /* Erase Chip. */
  141. bool res = spi_flash_start();
  142. if (!res) {
  143. dprint("Failed to start SPI! [spi flash erase chip]\n");
  144. return FLASH_STATUS_ERROR;
  145. }
  146. spi_write(FLASH_CMD_CE);
  147. spi_stop();
  148. /* Wait for the write-in-progress bit to be cleared.*/
  149. response = spi_flash_wait_while_busy();
  150. if (response != FLASH_STATUS_SUCCESS) {
  151. dprint("Failed to check WIP flag! [spi flash erase chip]\n");
  152. return response;
  153. }
  154. return response;
  155. }
  156. flash_status_t flash_erase_sector(uint32_t addr) {
  157. flash_status_t response = FLASH_STATUS_SUCCESS;
  158. /* Check that the address exceeds the limit. */
  159. if ((addr + (EXTERNAL_FLASH_SECTOR_SIZE)) >= (EXTERNAL_FLASH_SIZE) || ((addr % (EXTERNAL_FLASH_SECTOR_SIZE)) != 0)) {
  160. dprintf("Flash erase sector address over limit! [addr:0x%x]\n", (uint32_t)addr);
  161. return FLASH_STATUS_ERROR;
  162. }
  163. /* Wait for the write-in-progress bit to be cleared. */
  164. response = spi_flash_wait_while_busy();
  165. if (response != FLASH_STATUS_SUCCESS) {
  166. dprint("Failed to check WIP flag! [spi flash erase sector]\n");
  167. return response;
  168. }
  169. /* Enable writes. */
  170. response = spi_flash_write_enable();
  171. if (response != FLASH_STATUS_SUCCESS) {
  172. dprint("Failed to write-enable! [spi flash erase sector]\n");
  173. return response;
  174. }
  175. /* Erase Sector. */
  176. response = spi_flash_transaction(FLASH_CMD_SE, addr, NULL, 0);
  177. if (response != FLASH_STATUS_SUCCESS) {
  178. dprint("Failed to erase sector! [spi flash erase sector]\n");
  179. return response;
  180. }
  181. /* Wait for the write-in-progress bit to be cleared.*/
  182. response = spi_flash_wait_while_busy();
  183. if (response != FLASH_STATUS_SUCCESS) {
  184. dprint("Failed to check WIP flag! [spi flash erase sector]\n");
  185. return response;
  186. }
  187. return response;
  188. }
  189. flash_status_t flash_erase_block(uint32_t addr) {
  190. flash_status_t response = FLASH_STATUS_SUCCESS;
  191. /* Check that the address exceeds the limit. */
  192. if ((addr + (EXTERNAL_FLASH_BLOCK_SIZE)) >= (EXTERNAL_FLASH_SIZE) || ((addr % (EXTERNAL_FLASH_BLOCK_SIZE)) != 0)) {
  193. dprintf("Flash erase block address over limit! [addr:0x%x]\n", (uint32_t)addr);
  194. return FLASH_STATUS_ERROR;
  195. }
  196. /* Wait for the write-in-progress bit to be cleared. */
  197. response = spi_flash_wait_while_busy();
  198. if (response != FLASH_STATUS_SUCCESS) {
  199. dprint("Failed to check WIP flag! [spi flash erase block]\n");
  200. return response;
  201. }
  202. /* Enable writes. */
  203. response = spi_flash_write_enable();
  204. if (response != FLASH_STATUS_SUCCESS) {
  205. dprint("Failed to write-enable! [spi flash erase block]\n");
  206. return response;
  207. }
  208. /* Erase Block. */
  209. response = spi_flash_transaction(FLASH_CMD_BE, addr, NULL, 0);
  210. if (response != FLASH_STATUS_SUCCESS) {
  211. dprint("Failed to erase block! [spi flash erase block]\n");
  212. return response;
  213. }
  214. /* Wait for the write-in-progress bit to be cleared.*/
  215. response = spi_flash_wait_while_busy();
  216. if (response != FLASH_STATUS_SUCCESS) {
  217. dprint("Failed to check WIP flag! [spi flash erase block]\n");
  218. return response;
  219. }
  220. return response;
  221. }
  222. flash_status_t flash_read_block(uint32_t addr, void *buf, size_t len) {
  223. flash_status_t response = FLASH_STATUS_SUCCESS;
  224. uint8_t * read_buf = (uint8_t *)buf;
  225. /* Wait for the write-in-progress bit to be cleared. */
  226. response = spi_flash_wait_while_busy();
  227. if (response != FLASH_STATUS_SUCCESS) {
  228. dprint("Failed to check WIP flag! [spi flash read block]\n");
  229. memset(read_buf, 0, len);
  230. return response;
  231. }
  232. /* Perform read. */
  233. response = spi_flash_transaction(FLASH_CMD_READ, addr, read_buf, len);
  234. if (response != FLASH_STATUS_SUCCESS) {
  235. dprint("Failed to read block! [spi flash read block]\n");
  236. memset(read_buf, 0, len);
  237. return response;
  238. }
  239. #if defined(CONSOLE_ENABLE) && defined(DEBUG_FLASH_SPI_OUTPUT)
  240. dprintf("[SPI FLASH R] 0x%08lX: ", addr);
  241. for (size_t i = 0; i < len; ++i) {
  242. dprintf(" %02X", (int)(((uint8_t *)read_buf)[i]));
  243. }
  244. dprintf("\n");
  245. #endif // DEBUG_FLASH_SPI_OUTPUT
  246. return response;
  247. }
  248. flash_status_t flash_write_block(uint32_t addr, const void *buf, size_t len) {
  249. flash_status_t response = FLASH_STATUS_SUCCESS;
  250. uint8_t * write_buf = (uint8_t *)buf;
  251. while (len > 0) {
  252. uint32_t page_offset = addr % EXTERNAL_FLASH_PAGE_SIZE;
  253. size_t write_length = EXTERNAL_FLASH_PAGE_SIZE - page_offset;
  254. if (write_length > len) {
  255. write_length = len;
  256. }
  257. /* Wait for the write-in-progress bit to be cleared. */
  258. response = spi_flash_wait_while_busy();
  259. if (response != FLASH_STATUS_SUCCESS) {
  260. dprint("Failed to check WIP flag! [spi flash write block]\n");
  261. return response;
  262. }
  263. /* Enable writes. */
  264. response = spi_flash_write_enable();
  265. if (response != FLASH_STATUS_SUCCESS) {
  266. dprint("Failed to write-enable! [spi flash write block]\n");
  267. return response;
  268. }
  269. #if defined(CONSOLE_ENABLE) && defined(DEBUG_FLASH_SPI_OUTPUT)
  270. dprintf("[SPI FLASH W] 0x%08lX: ", addr);
  271. for (size_t i = 0; i < write_length; i++) {
  272. dprintf(" %02X", (int)(uint8_t)(write_buf[i]));
  273. }
  274. dprintf("\n");
  275. #endif // DEBUG_FLASH_SPI_OUTPUT
  276. /* Perform the write. */
  277. response = spi_flash_transaction(FLASH_CMD_PP, addr, write_buf, write_length);
  278. if (response != FLASH_STATUS_SUCCESS) {
  279. dprint("Failed to write block! [spi flash write block]\n");
  280. return response;
  281. }
  282. write_buf += write_length;
  283. addr += write_length;
  284. len -= write_length;
  285. }
  286. /* Wait for the write-in-progress bit to be cleared. */
  287. response = spi_flash_wait_while_busy();
  288. if (response != FLASH_STATUS_SUCCESS) {
  289. dprint("Failed to check WIP flag! [spi flash write block]\n");
  290. return response;
  291. }
  292. /* Disable writes. */
  293. response = spi_flash_write_disable();
  294. if (response != FLASH_STATUS_SUCCESS) {
  295. dprint("Failed to write-disable! [spi flash write block]\n");
  296. return response;
  297. }
  298. return response;
  299. }