sf_ops.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. /*
  2. * SPI flash operations
  3. *
  4. * Copyright (C) 2008 Atmel Corporation
  5. * Copyright (C) 2010 Reinhard Meyer, EMK Elektronik
  6. * Copyright (C) 2013 Jagannadha Sutradharudu Teki, Xilinx Inc.
  7. *
  8. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. #include <common.h>
  11. #include <malloc.h>
  12. #include <spi.h>
  13. #include <spi_flash.h>
  14. #include <watchdog.h>
  15. #include "sf_internal.h"
  16. static void spi_flash_addr(u32 addr, u8 *cmd)
  17. {
  18. /* cmd[0] is actual command */
  19. cmd[1] = addr >> 16;
  20. cmd[2] = addr >> 8;
  21. cmd[3] = addr >> 0;
  22. }
  23. int spi_flash_cmd_write_status(struct spi_flash *flash, u8 sr)
  24. {
  25. u8 cmd;
  26. int ret;
  27. cmd = CMD_WRITE_STATUS;
  28. ret = spi_flash_write_common(flash, &cmd, 1, &sr, 1);
  29. if (ret < 0) {
  30. debug("SF: fail to write status register\n");
  31. return ret;
  32. }
  33. return 0;
  34. }
  35. #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
  36. static int spi_flash_cmd_write_config(struct spi_flash *flash, u8 cr)
  37. {
  38. u8 data[2];
  39. u8 cmd;
  40. int ret;
  41. cmd = CMD_READ_STATUS;
  42. ret = spi_flash_read_common(flash, &cmd, 1, &data[0], 1);
  43. if (ret < 0) {
  44. debug("SF: fail to read status register\n");
  45. return ret;
  46. }
  47. cmd = CMD_WRITE_STATUS;
  48. data[1] = cr;
  49. ret = spi_flash_write_common(flash, &cmd, 1, &data, 2);
  50. if (ret) {
  51. debug("SF: fail to write config register\n");
  52. return ret;
  53. }
  54. return 0;
  55. }
  56. int spi_flash_set_qeb_winspan(struct spi_flash *flash)
  57. {
  58. u8 qeb_status;
  59. u8 cmd;
  60. int ret;
  61. cmd = CMD_READ_CONFIG;
  62. ret = spi_flash_read_common(flash, &cmd, 1, &qeb_status, 1);
  63. if (ret < 0) {
  64. debug("SF: fail to read config register\n");
  65. return ret;
  66. }
  67. if (qeb_status & STATUS_QEB_WINSPAN) {
  68. debug("SF: Quad enable bit is already set\n");
  69. } else {
  70. ret = spi_flash_cmd_write_config(flash, STATUS_QEB_WINSPAN);
  71. if (ret < 0)
  72. return ret;
  73. }
  74. return ret;
  75. }
  76. #endif
  77. #ifdef CONFIG_SPI_FLASH_BAR
  78. static int spi_flash_cmd_bankaddr_write(struct spi_flash *flash, u8 bank_sel)
  79. {
  80. u8 cmd;
  81. int ret;
  82. if (flash->bank_curr == bank_sel) {
  83. debug("SF: not require to enable bank%d\n", bank_sel);
  84. return 0;
  85. }
  86. cmd = flash->bank_write_cmd;
  87. ret = spi_flash_write_common(flash, &cmd, 1, &bank_sel, 1);
  88. if (ret < 0) {
  89. debug("SF: fail to write bank register\n");
  90. return ret;
  91. }
  92. flash->bank_curr = bank_sel;
  93. return 0;
  94. }
  95. static int spi_flash_bank(struct spi_flash *flash, u32 offset)
  96. {
  97. u8 bank_sel;
  98. int ret;
  99. bank_sel = offset / SPI_FLASH_16MB_BOUN;
  100. ret = spi_flash_cmd_bankaddr_write(flash, bank_sel);
  101. if (ret) {
  102. debug("SF: fail to set bank%d\n", bank_sel);
  103. return ret;
  104. }
  105. return 0;
  106. }
  107. #endif
  108. int spi_flash_cmd_wait_ready(struct spi_flash *flash, unsigned long timeout)
  109. {
  110. struct spi_slave *spi = flash->spi;
  111. unsigned long timebase;
  112. int ret;
  113. u8 status;
  114. u8 check_status = 0x0;
  115. u8 poll_bit = STATUS_WIP;
  116. u8 cmd = flash->poll_cmd;
  117. if (cmd == CMD_FLAG_STATUS) {
  118. poll_bit = STATUS_PEC;
  119. check_status = poll_bit;
  120. }
  121. ret = spi_xfer(spi, 8, &cmd, NULL, SPI_XFER_BEGIN);
  122. if (ret) {
  123. debug("SF: fail to read %s status register\n",
  124. cmd == CMD_READ_STATUS ? "read" : "flag");
  125. return ret;
  126. }
  127. timebase = get_timer(0);
  128. do {
  129. WATCHDOG_RESET();
  130. ret = spi_xfer(spi, 8, NULL, &status, 0);
  131. if (ret)
  132. return -1;
  133. if ((status & poll_bit) == check_status)
  134. break;
  135. } while (get_timer(timebase) < timeout);
  136. spi_xfer(spi, 0, NULL, NULL, SPI_XFER_END);
  137. if ((status & poll_bit) == check_status)
  138. return 0;
  139. /* Timed out */
  140. debug("SF: time out!\n");
  141. return -1;
  142. }
  143. int spi_flash_write_common(struct spi_flash *flash, const u8 *cmd,
  144. size_t cmd_len, const void *buf, size_t buf_len)
  145. {
  146. struct spi_slave *spi = flash->spi;
  147. unsigned long timeout = SPI_FLASH_PROG_TIMEOUT;
  148. int ret;
  149. if (buf == NULL)
  150. timeout = SPI_FLASH_PAGE_ERASE_TIMEOUT;
  151. ret = spi_claim_bus(flash->spi);
  152. if (ret) {
  153. debug("SF: unable to claim SPI bus\n");
  154. return ret;
  155. }
  156. ret = spi_flash_cmd_write_enable(flash);
  157. if (ret < 0) {
  158. debug("SF: enabling write failed\n");
  159. return ret;
  160. }
  161. ret = spi_flash_cmd_write(spi, cmd, cmd_len, buf, buf_len);
  162. if (ret < 0) {
  163. debug("SF: write cmd failed\n");
  164. return ret;
  165. }
  166. ret = spi_flash_cmd_wait_ready(flash, timeout);
  167. if (ret < 0) {
  168. debug("SF: write %s timed out\n",
  169. timeout == SPI_FLASH_PROG_TIMEOUT ?
  170. "program" : "page erase");
  171. return ret;
  172. }
  173. spi_release_bus(spi);
  174. return ret;
  175. }
  176. int spi_flash_cmd_erase_ops(struct spi_flash *flash, u32 offset, size_t len)
  177. {
  178. u32 erase_size;
  179. u8 cmd[SPI_FLASH_CMD_LEN];
  180. int ret = -1;
  181. erase_size = flash->erase_size;
  182. if (offset % erase_size || len % erase_size) {
  183. debug("SF: Erase offset/length not multiple of erase size\n");
  184. return -1;
  185. }
  186. cmd[0] = flash->erase_cmd;
  187. while (len) {
  188. #ifdef CONFIG_SPI_FLASH_BAR
  189. ret = spi_flash_bank(flash, offset);
  190. if (ret < 0)
  191. return ret;
  192. #endif
  193. spi_flash_addr(offset, cmd);
  194. debug("SF: erase %2x %2x %2x %2x (%x)\n", cmd[0], cmd[1],
  195. cmd[2], cmd[3], offset);
  196. ret = spi_flash_write_common(flash, cmd, sizeof(cmd), NULL, 0);
  197. if (ret < 0) {
  198. debug("SF: erase failed\n");
  199. break;
  200. }
  201. offset += erase_size;
  202. len -= erase_size;
  203. }
  204. return ret;
  205. }
  206. int spi_flash_cmd_write_ops(struct spi_flash *flash, u32 offset,
  207. size_t len, const void *buf)
  208. {
  209. unsigned long byte_addr, page_size;
  210. size_t chunk_len, actual;
  211. u8 cmd[SPI_FLASH_CMD_LEN];
  212. int ret = -1;
  213. page_size = flash->page_size;
  214. cmd[0] = flash->write_cmd;
  215. for (actual = 0; actual < len; actual += chunk_len) {
  216. #ifdef CONFIG_SPI_FLASH_BAR
  217. ret = spi_flash_bank(flash, offset);
  218. if (ret < 0)
  219. return ret;
  220. #endif
  221. byte_addr = offset % page_size;
  222. chunk_len = min(len - actual, page_size - byte_addr);
  223. if (flash->spi->max_write_size)
  224. chunk_len = min(chunk_len, flash->spi->max_write_size);
  225. spi_flash_addr(offset, cmd);
  226. debug("PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %zu\n",
  227. buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len);
  228. ret = spi_flash_write_common(flash, cmd, sizeof(cmd),
  229. buf + actual, chunk_len);
  230. if (ret < 0) {
  231. debug("SF: write failed\n");
  232. break;
  233. }
  234. offset += chunk_len;
  235. }
  236. return ret;
  237. }
  238. int spi_flash_read_common(struct spi_flash *flash, const u8 *cmd,
  239. size_t cmd_len, void *data, size_t data_len)
  240. {
  241. struct spi_slave *spi = flash->spi;
  242. int ret;
  243. ret = spi_claim_bus(flash->spi);
  244. if (ret) {
  245. debug("SF: unable to claim SPI bus\n");
  246. return ret;
  247. }
  248. ret = spi_flash_cmd_read(spi, cmd, cmd_len, data, data_len);
  249. if (ret < 0) {
  250. debug("SF: read cmd failed\n");
  251. return ret;
  252. }
  253. spi_release_bus(spi);
  254. return ret;
  255. }
  256. int spi_flash_cmd_read_ops(struct spi_flash *flash, u32 offset,
  257. size_t len, void *data)
  258. {
  259. u8 *cmd, cmdsz, bank_sel = 0;
  260. u32 remain_len, read_len;
  261. int ret = -1;
  262. /* Handle memory-mapped SPI */
  263. if (flash->memory_map) {
  264. ret = spi_claim_bus(flash->spi);
  265. if (ret) {
  266. debug("SF: unable to claim SPI bus\n");
  267. return ret;
  268. }
  269. spi_xfer(flash->spi, 0, NULL, NULL, SPI_XFER_MMAP);
  270. memcpy(data, flash->memory_map + offset, len);
  271. spi_xfer(flash->spi, 0, NULL, NULL, SPI_XFER_MMAP_END);
  272. spi_release_bus(flash->spi);
  273. return 0;
  274. }
  275. cmdsz = SPI_FLASH_CMD_LEN + flash->dummy_byte;
  276. cmd = malloc(cmdsz);
  277. memset(cmd, 0, cmdsz);
  278. cmd[0] = flash->read_cmd;
  279. while (len) {
  280. #ifdef CONFIG_SPI_FLASH_BAR
  281. bank_sel = offset / SPI_FLASH_16MB_BOUN;
  282. ret = spi_flash_cmd_bankaddr_write(flash, bank_sel);
  283. if (ret) {
  284. debug("SF: fail to set bank%d\n", bank_sel);
  285. return ret;
  286. }
  287. #endif
  288. remain_len = (SPI_FLASH_16MB_BOUN * (bank_sel + 1)) - offset;
  289. if (len < remain_len)
  290. read_len = len;
  291. else
  292. read_len = remain_len;
  293. spi_flash_addr(offset, cmd);
  294. ret = spi_flash_read_common(flash, cmd, cmdsz, data, read_len);
  295. if (ret < 0) {
  296. debug("SF: read failed\n");
  297. break;
  298. }
  299. offset += read_len;
  300. len -= read_len;
  301. data += read_len;
  302. }
  303. return ret;
  304. }
  305. #ifdef CONFIG_SPI_FLASH_SST
  306. static int sst_byte_write(struct spi_flash *flash, u32 offset, const void *buf)
  307. {
  308. int ret;
  309. u8 cmd[4] = {
  310. CMD_SST_BP,
  311. offset >> 16,
  312. offset >> 8,
  313. offset,
  314. };
  315. debug("BP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
  316. spi_w8r8(flash->spi, CMD_READ_STATUS), buf, cmd[0], offset);
  317. ret = spi_flash_cmd_write_enable(flash);
  318. if (ret)
  319. return ret;
  320. ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd), buf, 1);
  321. if (ret)
  322. return ret;
  323. return spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
  324. }
  325. int sst_write_wp(struct spi_flash *flash, u32 offset, size_t len,
  326. const void *buf)
  327. {
  328. size_t actual, cmd_len;
  329. int ret;
  330. u8 cmd[4];
  331. ret = spi_claim_bus(flash->spi);
  332. if (ret) {
  333. debug("SF: Unable to claim SPI bus\n");
  334. return ret;
  335. }
  336. /* If the data is not word aligned, write out leading single byte */
  337. actual = offset % 2;
  338. if (actual) {
  339. ret = sst_byte_write(flash, offset, buf);
  340. if (ret)
  341. goto done;
  342. }
  343. offset += actual;
  344. ret = spi_flash_cmd_write_enable(flash);
  345. if (ret)
  346. goto done;
  347. cmd_len = 4;
  348. cmd[0] = CMD_SST_AAI_WP;
  349. cmd[1] = offset >> 16;
  350. cmd[2] = offset >> 8;
  351. cmd[3] = offset;
  352. for (; actual < len - 1; actual += 2) {
  353. debug("WP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
  354. spi_w8r8(flash->spi, CMD_READ_STATUS), buf + actual,
  355. cmd[0], offset);
  356. ret = spi_flash_cmd_write(flash->spi, cmd, cmd_len,
  357. buf + actual, 2);
  358. if (ret) {
  359. debug("SF: sst word program failed\n");
  360. break;
  361. }
  362. ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
  363. if (ret)
  364. break;
  365. cmd_len = 1;
  366. offset += 2;
  367. }
  368. if (!ret)
  369. ret = spi_flash_cmd_write_disable(flash);
  370. /* If there is a single trailing byte, write it out */
  371. if (!ret && actual != len)
  372. ret = sst_byte_write(flash, offset, buf + actual);
  373. done:
  374. debug("SF: sst: program %s %zu bytes @ 0x%zx\n",
  375. ret ? "failure" : "success", len, offset - actual);
  376. spi_release_bus(flash->spi);
  377. return ret;
  378. }
  379. #endif