hw_spi_flash.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. #include "board.h"
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include "hw_spi_flash.h"
  5. static spi_flash_chip flash_table[] =
  6. {
  7. { "W25Q40BV", SFUD_MF_ID_WINBOND, 0x40, 0x13, 512L * 1024L, SFUD_WM_PAGE_256B, 4096, 0x20 },
  8. { "W25Q16BV", SFUD_MF_ID_WINBOND, 0x40, 0x15, 2L * 1024L * 1024L, SFUD_WM_PAGE_256B, 4096, 0x20 },
  9. { "W25Q64DW", SFUD_MF_ID_WINBOND, 0x40, 0x17, 8L * 1024L * 1024L, SFUD_WM_PAGE_256B, 4096, 0x20 },
  10. { "W25Q128BV", SFUD_MF_ID_WINBOND, 0x40, 0x18, 16L * 1024L * 1024L, SFUD_WM_PAGE_256B, 4096, 0x20 },
  11. { "W25Q256FV", SFUD_MF_ID_WINBOND, 0x40, 0x19, 32L * 1024L * 1024L, SFUD_WM_PAGE_256B, 4096, 0x20 },
  12. { "M25P32", SFUD_MF_ID_MICRON, 0x20, 0x16, 4L * 1024L * 1024L, SFUD_WM_PAGE_256B, 64L * 1024L, 0xD8 },
  13. { "M25P80", SFUD_MF_ID_MICRON, 0x20, 0x14, 1L * 1024L * 1024L, SFUD_WM_PAGE_256B, 64L * 1024L, 0xD8 },
  14. { "M25P40", SFUD_MF_ID_MICRON, 0x20, 0x13, 512L * 1024L, SFUD_WM_PAGE_256B, 64L * 1024L, 0xD8 },
  15. { "EN25Q32B", SFUD_MF_ID_EON, 0x30, 0x16, 4L * 1024L * 1024L, SFUD_WM_PAGE_256B, 4096, 0x20 },
  16. { "GD25Q64B", SFUD_MF_ID_GIGADEVICE, 0x40, 0x17, 8L * 1024L * 1024L, SFUD_WM_PAGE_256B, 4096, 0x20 },
  17. { "GD25Q16B", SFUD_MF_ID_GIGADEVICE, 0x40, 0x15, 2L * 1024L * 1024L, SFUD_WM_PAGE_256B, 4096, 0x20 },
  18. { "S25FL216K", SFUD_MF_ID_CYPRESS, 0x40, 0x15, 2L * 1024L * 1024L, SFUD_WM_PAGE_256B, 4096, 0x20 },
  19. { "S25FL032P", SFUD_MF_ID_CYPRESS, 0x02, 0x15, 4L * 1024L * 1024L, SFUD_WM_PAGE_256B, 4096, 0x20 },
  20. { "A25L080", SFUD_MF_ID_AMIC, 0x30, 0x14, 1L * 1024L * 1024L, SFUD_WM_PAGE_256B, 4096, 0x20 },
  21. };
  22. #define SPI_FLASH_ReadStatusReg1 0x05
  23. #define SPI_FLASH_ReadStatusReg2 0x35
  24. #define SPI_FLASH_ReadStatusReg3 0x15
  25. #define SPI_FLASH_WriteStatusReg1 0x01
  26. #define SPI_FLASH_WriteStatusReg2 0x31
  27. #define SPI_FLASH_WriteStatusReg3 0x11
  28. static hw_spi_flash_device_t device_map[HW_SPI_FLASH_DEVICE_NUMBER] = {
  29. { 0 }
  30. };
  31. /**
  32. * 将地址填充进数组
  33. *
  34. * @author LXZ (011621)
  35. *
  36. * @param index
  37. * @param addr
  38. * @param array
  39. */
  40. static void make_adress_byte_array(int index, uint32_t addr, uint8_t *array) {
  41. uint8_t i = 0;
  42. if (device_map[index].addr_in_4_byte) {
  43. array[i++] = addr >> 24;
  44. }
  45. array[i++] = addr >> 16;
  46. array[i++] = addr >> 8;
  47. array[i++] = addr >> 0;
  48. }
  49. static void set_4_byte_address_mode(int index, uint8_t enabled) {
  50. uint8_t cmd;
  51. int device = device_map[index].device;
  52. spi_flash_set_write_enable(index, 1);
  53. if (enabled) {
  54. cmd = 0xB7;
  55. }
  56. else {
  57. cmd = 0xE9;
  58. }
  59. hw_spi_device_take(device);
  60. hw_spi_readwrite(device, &cmd, 0, 1);
  61. hw_spi_device_release(device);
  62. device_map[index].addr_in_4_byte = enabled ? 1 : 0;
  63. }
  64. /**
  65. * 按页或者字节写入
  66. *
  67. * @author LXZ (011821)
  68. *
  69. * @param flash
  70. * @param addr
  71. * @param size
  72. * @param write_gran
  73. * @param data
  74. *
  75. * @return int
  76. */
  77. static int page256_or_1_byte_write(int index, uint32_t addr, int size, uint16_t write_gran,
  78. const uint8_t *data) {
  79. int device = device_map[index].device;
  80. static uint8_t cmd_data[5];
  81. uint8_t cmd_size;
  82. int data_size;
  83. if (write_gran != 1 && write_gran != 256) {
  84. return -1;
  85. }
  86. //检测地址范围
  87. if (addr + size > device_map[index].chip.capacity) {
  88. return -2;
  89. }
  90. /* loop write operate. write unit is write granularity */
  91. while (size) {
  92. spi_flash_set_write_enable(index, 1);
  93. cmd_data[0] = 0x02;
  94. make_adress_byte_array(index, addr, &cmd_data[1]);
  95. cmd_size = device_map[index].addr_in_4_byte ? 5 : 4;
  96. /* make write align and calculate next write address */
  97. if (addr % write_gran != 0) {
  98. if (size > write_gran - (addr % write_gran)) {
  99. data_size = write_gran - (addr % write_gran);
  100. }
  101. else {
  102. data_size = size;
  103. }
  104. }
  105. else {
  106. if (size > write_gran) {
  107. data_size = write_gran;
  108. }
  109. else {
  110. data_size = size;
  111. }
  112. }
  113. size -= data_size;
  114. addr += data_size;
  115. hw_spi_device_take(device);
  116. hw_spi_readwrite(device, cmd_data, 0, cmd_size);
  117. hw_spi_readwrite(device, (uint8_t *)data, 0, data_size);
  118. hw_spi_device_release(device);
  119. spi_flash_wait_busy(index);
  120. data += data_size;
  121. }
  122. spi_flash_set_write_enable(index, 0);
  123. return size;
  124. }
  125. //==============================================SPI FLASH芯片驱动======================================//
  126. /**
  127. * 芯片写状态
  128. *
  129. * @author LXZ (011621)
  130. *
  131. * @param index
  132. * @param is_volatile
  133. * @param status
  134. *
  135. * @return int
  136. */
  137. int spi_flash_write_status(int index, uint8_t reg_no, uint8_t status) {
  138. uint8_t cmd_data[2];
  139. int device = device_map[index].device;
  140. switch (reg_no) {
  141. case 2:
  142. cmd_data[0] = SPI_FLASH_WriteStatusReg2;
  143. break;
  144. case 3:
  145. cmd_data[0] = SPI_FLASH_WriteStatusReg3;
  146. break;
  147. default:
  148. cmd_data[0] = SPI_FLASH_WriteStatusReg1;
  149. break;
  150. }
  151. cmd_data[1] = status;
  152. hw_spi_device_take(device);
  153. hw_spi_readwrite(device, cmd_data, 0, 2);
  154. hw_spi_device_release(device);
  155. return 0;
  156. }
  157. /**
  158. * W25Q芯片读取
  159. *
  160. * @author LXZ (011621)
  161. *
  162. * @param index
  163. * @param status
  164. *
  165. * @return int
  166. */
  167. uint8_t spi_flash_read_status(int index, uint8_t reg_no) {
  168. uint32_t i;
  169. uint8_t cmd;
  170. uint8_t status;
  171. switch (reg_no) {
  172. case 2:
  173. cmd = SPI_FLASH_ReadStatusReg2;
  174. break;
  175. case 3:
  176. cmd = SPI_FLASH_ReadStatusReg3;
  177. break;
  178. default:
  179. cmd = SPI_FLASH_ReadStatusReg1;
  180. }
  181. int device = device_map[index].device;
  182. hw_spi_device_take(device);
  183. hw_spi_readwrite(device, &cmd, 0, 1);
  184. hw_spi_readwrite(device, 0, &status, 1);
  185. hw_spi_device_release(device);
  186. return status;
  187. }
  188. /**
  189. * 芯片等待操作完成
  190. *
  191. * @author LXZ (011621)
  192. *
  193. * @param index
  194. *
  195. * @return int
  196. */
  197. int spi_flash_wait_busy(int index) {
  198. while ((spi_flash_read_status(index, 1) & 0x01) == 0x01);
  199. return 0;
  200. }
  201. /**
  202. * 设置写保护开关
  203. *
  204. * @author LXZ (011621)
  205. *
  206. * @param index
  207. * @param enable
  208. *
  209. * @return int
  210. */
  211. int spi_flash_set_write_enable(int index, uint8_t enabled) {
  212. uint8_t cmd;
  213. int device = device_map[index].device;
  214. if (enabled) {
  215. cmd = 0x06;
  216. }
  217. else {
  218. cmd = 0x04;
  219. }
  220. hw_spi_device_take(device);
  221. hw_spi_readwrite(device, &cmd, 0, 1);
  222. hw_spi_device_release(device);
  223. return 0;
  224. }
  225. /**
  226. * 芯片复位
  227. *
  228. * @author LXZ (011621)
  229. *
  230. * @param index
  231. *
  232. * @return int
  233. */
  234. int spi_flash_reset(int index) {
  235. uint8_t cmd_data[2];
  236. int device = device_map[index].device;
  237. cmd_data[0] = 0x66;
  238. hw_spi_device_take(device);
  239. hw_spi_readwrite(device, cmd_data, 0, 1);
  240. hw_spi_device_release(device);
  241. spi_flash_wait_busy(index);
  242. cmd_data[1] = 0x99;
  243. hw_spi_device_take(device);
  244. hw_spi_readwrite(device, &cmd_data[1], 0, 1);
  245. hw_spi_device_release(device);
  246. spi_flash_wait_busy(index);
  247. return 0;
  248. }
  249. int spi_flash_read_jedec_id(int index) {
  250. int i;
  251. int device = device_map[index].device;
  252. uint8_t cmd_data[2], recv_data[3];
  253. spi_flash_wait_busy(index);
  254. cmd_data[0] = 0x9F;
  255. hw_spi_device_take(device);
  256. hw_spi_readwrite(device, cmd_data, 0, 1);
  257. hw_spi_readwrite(device, 0, recv_data, sizeof(recv_data));
  258. hw_spi_readwrite(device, 0, 0, 2);
  259. cmd_data[0] = 0x90;
  260. hw_spi_readwrite(device, cmd_data, 0, 1);
  261. hw_spi_readwrite(device, 0, 0, 5);
  262. hw_spi_device_release(device);
  263. for (i = 0; i < sizeof(flash_table) / sizeof(spi_flash_chip); i++) {
  264. if (flash_table[i].capacity_id == recv_data[2] &&
  265. flash_table[i].mf_id == recv_data[0] &&
  266. flash_table[i].type_id == recv_data[1]) {
  267. device_map[index].chip = flash_table[i];
  268. device_map[index].init_ok = 1;
  269. device_map[index].geometry.sector_count = device_map[index].chip.capacity / device_map[index].chip.erase_gran;
  270. device_map[index].geometry.bytes_per_sector = device_map[index].chip.erase_gran;
  271. device_map[index].geometry.block_size = device_map[index].geometry.sector_count;
  272. break;
  273. }
  274. }
  275. return 0;
  276. }
  277. /**
  278. * 向指定地址写入数据
  279. *
  280. * @author LXZ (011621)
  281. *
  282. * @param index
  283. * @param addr
  284. * @param dat
  285. * @param size
  286. *
  287. * @return int
  288. */
  289. int spi_flash_write(int index, uint32_t addr, const uint8_t *data, int size) {
  290. return page256_or_1_byte_write(index, addr, size, 256, data);
  291. }
  292. /**
  293. * 向指定地址读取数据
  294. *
  295. * @author LXZ (011621)
  296. *
  297. * @param index
  298. * @param addr
  299. * @param dat
  300. * @param size
  301. *
  302. * @return int
  303. */
  304. int spi_flash_read(int index, uint32_t addr, uint8_t *data, int size) {
  305. int device = device_map[index].device;
  306. uint8_t cmd_data[5], cmd_size;
  307. //检测是否超过了容量范围
  308. if (addr + size > device_map[index].chip.capacity) {
  309. return -1;
  310. }
  311. spi_flash_wait_busy(index);
  312. cmd_data[0] = 0x03;
  313. make_adress_byte_array(index, addr, &cmd_data[1]);
  314. cmd_size = device_map[index].addr_in_4_byte ? 5 : 4;
  315. hw_spi_device_take(device);
  316. hw_spi_readwrite(device, cmd_data, 0, cmd_size);
  317. hw_spi_readwrite(device, 0, data, size);
  318. hw_spi_device_release(device);
  319. return size;
  320. }
  321. /**
  322. * 擦除指定地址并写入
  323. *
  324. * @author LXZ (011821)
  325. *
  326. * @param index
  327. * @param addr
  328. * @param size
  329. * @param data
  330. *
  331. * @return int
  332. */
  333. int spi_flash_erase_write(int index, uint32_t addr, const uint8_t *data, int size) {
  334. spi_flash_erase(index, addr, size);
  335. spi_flash_write(index, addr, data, size);
  336. return 0;
  337. }
  338. /**
  339. * 擦除指定地址
  340. *
  341. * @author LXZ (011621)
  342. *
  343. * @param index
  344. * @param addr
  345. * @param size
  346. *
  347. * @return int
  348. */
  349. int spi_flash_erase(int index, uint32_t addr, int size) {
  350. extern int sfud_sfdp_get_suitable_eraser(int index, uint32_t addr, int erase_size);
  351. int device = device_map[index].device;
  352. uint8_t cmd_data[5], cmd_size, cur_erase_cmd;
  353. size_t cur_erase_size;
  354. /* check the flash address bound */
  355. if (addr + size > device_map[index].chip.capacity) {
  356. return -1;
  357. }
  358. if (addr == 0 && size == device_map[index].chip.capacity) {
  359. return spi_flash_erase_chip(index);
  360. }
  361. cur_erase_cmd = device_map[index].chip.erase_gran_cmd;
  362. cur_erase_size = device_map[index].chip.erase_gran;
  363. /* loop erase operate. erase unit is erase granularity */
  364. while (size) {
  365. spi_flash_set_write_enable(index, 1);
  366. cmd_data[0] = cur_erase_cmd;
  367. make_adress_byte_array(index, addr, &cmd_data[1]);
  368. cmd_size = device_map[index].addr_in_4_byte ? 5 : 4;
  369. hw_spi_device_take(device);
  370. hw_spi_readwrite(device, cmd_data, 0, cmd_size);
  371. hw_spi_device_release(device);
  372. spi_flash_wait_busy(index);
  373. /* make erase align and calculate next erase address */
  374. if (addr % cur_erase_size != 0) {
  375. if (size > cur_erase_size - (addr % cur_erase_size)) {
  376. size -= cur_erase_size - (addr % cur_erase_size);
  377. addr += cur_erase_size - (addr % cur_erase_size);
  378. }
  379. else {
  380. goto __exit;
  381. }
  382. }
  383. else {
  384. if (size > cur_erase_size) {
  385. size -= cur_erase_size;
  386. addr += cur_erase_size;
  387. }
  388. else {
  389. goto __exit;
  390. }
  391. }
  392. }
  393. __exit:
  394. spi_flash_set_write_enable(index, 0);
  395. return 0;
  396. }
  397. /**
  398. * 整片擦除
  399. *
  400. * @author LXZ (011621)
  401. *
  402. * @param index
  403. * @param addr
  404. * @param size
  405. *
  406. * @return int
  407. */
  408. int spi_flash_erase_chip(int index) {
  409. uint8_t cmd_data[4];
  410. int device = device_map[index].device;
  411. spi_flash_set_write_enable(index, 1);
  412. cmd_data[0] = 0xC7;
  413. hw_spi_device_take(device);
  414. hw_spi_readwrite(device, cmd_data, 0, 1);
  415. hw_spi_device_release(device);
  416. spi_flash_wait_busy(index);
  417. spi_flash_set_write_enable(index, 0);
  418. return 0;
  419. }
  420. /**
  421. * 挂载SPI FLASH
  422. *
  423. * @author LXZ (011621)
  424. *
  425. * @param port 绑定的设备
  426. * @param index FLASH编号
  427. */
  428. int hw_spi_flash_attach(int index, int device) {
  429. //读取指定设备上是否有设备
  430. device_map[index].device = device;
  431. device_map[index].retry_time = 10;
  432. printf("scan device int bus %d...\n", device);
  433. //读取芯片ID
  434. spi_flash_read_jedec_id(index);
  435. if (device_map[index].init_ok) {
  436. printf("Find flash %d:%d: %s. Size is %d bytes.\n", device, index, device_map[index].chip.name,
  437. device_map[index].chip.capacity);
  438. spi_flash_reset(index);
  439. if (device_map[index].chip.capacity > (1L << 24)) {
  440. set_4_byte_address_mode(index, 1);
  441. device_map[index].addr_in_4_byte = 1;
  442. }
  443. else {
  444. set_4_byte_address_mode(index, 0);
  445. }
  446. }
  447. else
  448. {
  449. printf("Can't Find flash chip at port %d.\n", device);
  450. }
  451. return device_map[index].init_ok;
  452. }