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