spi_nor.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /*
  2. * Copyright (c) 2019-2022, STMicroelectronics - All Rights Reserved
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <errno.h>
  8. #include <stddef.h>
  9. #include <common/debug.h>
  10. #include <drivers/delay_timer.h>
  11. #include <drivers/spi_nor.h>
  12. #include <lib/utils.h>
  13. #define SR_WIP BIT(0) /* Write in progress */
  14. #define CR_QUAD_EN_SPAN BIT(1) /* Spansion Quad I/O */
  15. #define SR_QUAD_EN_MX BIT(6) /* Macronix Quad I/O */
  16. #define FSR_READY BIT(7) /* Device status, 0 = Busy, 1 = Ready */
  17. /* Defined IDs for supported memories */
  18. #define SPANSION_ID 0x01U
  19. #define MACRONIX_ID 0xC2U
  20. #define MICRON_ID 0x2CU
  21. #define BANK_SIZE 0x1000000U
  22. #define SPI_READY_TIMEOUT_US 40000U
  23. static struct nor_device nor_dev;
  24. #pragma weak plat_get_nor_data
  25. int plat_get_nor_data(struct nor_device *device)
  26. {
  27. return 0;
  28. }
  29. static int spi_nor_reg(uint8_t reg, uint8_t *buf, size_t len,
  30. enum spi_mem_data_dir dir)
  31. {
  32. struct spi_mem_op op;
  33. zeromem(&op, sizeof(struct spi_mem_op));
  34. op.cmd.opcode = reg;
  35. op.cmd.buswidth = SPI_MEM_BUSWIDTH_1_LINE;
  36. op.data.buswidth = SPI_MEM_BUSWIDTH_1_LINE;
  37. op.data.dir = dir;
  38. op.data.nbytes = len;
  39. op.data.buf = buf;
  40. return spi_mem_exec_op(&op);
  41. }
  42. static inline int spi_nor_read_id(uint8_t *id)
  43. {
  44. return spi_nor_reg(SPI_NOR_OP_READ_ID, id, 1U, SPI_MEM_DATA_IN);
  45. }
  46. static inline int spi_nor_read_cr(uint8_t *cr)
  47. {
  48. return spi_nor_reg(SPI_NOR_OP_READ_CR, cr, 1U, SPI_MEM_DATA_IN);
  49. }
  50. static inline int spi_nor_read_sr(uint8_t *sr)
  51. {
  52. return spi_nor_reg(SPI_NOR_OP_READ_SR, sr, 1U, SPI_MEM_DATA_IN);
  53. }
  54. static inline int spi_nor_read_fsr(uint8_t *fsr)
  55. {
  56. return spi_nor_reg(SPI_NOR_OP_READ_FSR, fsr, 1U, SPI_MEM_DATA_IN);
  57. }
  58. static inline int spi_nor_write_en(void)
  59. {
  60. return spi_nor_reg(SPI_NOR_OP_WREN, NULL, 0U, SPI_MEM_DATA_OUT);
  61. }
  62. /*
  63. * Check if device is ready.
  64. *
  65. * Return 0 if ready, 1 if busy or a negative error code otherwise
  66. */
  67. static int spi_nor_ready(void)
  68. {
  69. uint8_t sr;
  70. int ret;
  71. ret = spi_nor_read_sr(&sr);
  72. if (ret != 0) {
  73. return ret;
  74. }
  75. if ((nor_dev.flags & SPI_NOR_USE_FSR) != 0U) {
  76. uint8_t fsr;
  77. ret = spi_nor_read_fsr(&fsr);
  78. if (ret != 0) {
  79. return ret;
  80. }
  81. return (((fsr & FSR_READY) != 0U) && ((sr & SR_WIP) == 0U)) ?
  82. 0 : 1;
  83. }
  84. return (((sr & SR_WIP) == 0U) ? 0 : 1);
  85. }
  86. static int spi_nor_wait_ready(void)
  87. {
  88. int ret;
  89. uint64_t timeout = timeout_init_us(SPI_READY_TIMEOUT_US);
  90. while (!timeout_elapsed(timeout)) {
  91. ret = spi_nor_ready();
  92. if (ret <= 0) {
  93. return ret;
  94. }
  95. }
  96. return -ETIMEDOUT;
  97. }
  98. static int spi_nor_macronix_quad_enable(void)
  99. {
  100. uint8_t sr;
  101. int ret;
  102. ret = spi_nor_read_sr(&sr);
  103. if (ret != 0) {
  104. return ret;
  105. }
  106. if ((sr & SR_QUAD_EN_MX) != 0U) {
  107. return 0;
  108. }
  109. ret = spi_nor_write_en();
  110. if (ret != 0) {
  111. return ret;
  112. }
  113. sr |= SR_QUAD_EN_MX;
  114. ret = spi_nor_reg(SPI_NOR_OP_WRSR, &sr, 1U, SPI_MEM_DATA_OUT);
  115. if (ret != 0) {
  116. return ret;
  117. }
  118. ret = spi_nor_wait_ready();
  119. if (ret != 0) {
  120. return ret;
  121. }
  122. ret = spi_nor_read_sr(&sr);
  123. if ((ret != 0) || ((sr & SR_QUAD_EN_MX) == 0U)) {
  124. return -EINVAL;
  125. }
  126. return 0;
  127. }
  128. static int spi_nor_write_sr_cr(uint8_t *sr_cr)
  129. {
  130. int ret;
  131. ret = spi_nor_write_en();
  132. if (ret != 0) {
  133. return ret;
  134. }
  135. ret = spi_nor_reg(SPI_NOR_OP_WRSR, sr_cr, 2U, SPI_MEM_DATA_OUT);
  136. if (ret != 0) {
  137. return -EINVAL;
  138. }
  139. ret = spi_nor_wait_ready();
  140. if (ret != 0) {
  141. return ret;
  142. }
  143. return 0;
  144. }
  145. static int spi_nor_quad_enable(void)
  146. {
  147. uint8_t sr_cr[2];
  148. int ret;
  149. ret = spi_nor_read_cr(&sr_cr[1]);
  150. if (ret != 0) {
  151. return ret;
  152. }
  153. if ((sr_cr[1] & CR_QUAD_EN_SPAN) != 0U) {
  154. return 0;
  155. }
  156. sr_cr[1] |= CR_QUAD_EN_SPAN;
  157. ret = spi_nor_read_sr(&sr_cr[0]);
  158. if (ret != 0) {
  159. return ret;
  160. }
  161. ret = spi_nor_write_sr_cr(sr_cr);
  162. if (ret != 0) {
  163. return ret;
  164. }
  165. ret = spi_nor_read_cr(&sr_cr[1]);
  166. if ((ret != 0) || ((sr_cr[1] & CR_QUAD_EN_SPAN) == 0U)) {
  167. return -EINVAL;
  168. }
  169. return 0;
  170. }
  171. static int spi_nor_clean_bar(void)
  172. {
  173. int ret;
  174. if (nor_dev.selected_bank == 0U) {
  175. return 0;
  176. }
  177. nor_dev.selected_bank = 0U;
  178. ret = spi_nor_write_en();
  179. if (ret != 0) {
  180. return ret;
  181. }
  182. return spi_nor_reg(nor_dev.bank_write_cmd, &nor_dev.selected_bank,
  183. 1U, SPI_MEM_DATA_OUT);
  184. }
  185. static int spi_nor_write_bar(uint32_t offset)
  186. {
  187. uint8_t selected_bank = offset / BANK_SIZE;
  188. int ret;
  189. if (selected_bank == nor_dev.selected_bank) {
  190. return 0;
  191. }
  192. ret = spi_nor_write_en();
  193. if (ret != 0) {
  194. return ret;
  195. }
  196. ret = spi_nor_reg(nor_dev.bank_write_cmd, &selected_bank,
  197. 1U, SPI_MEM_DATA_OUT);
  198. if (ret != 0) {
  199. return ret;
  200. }
  201. nor_dev.selected_bank = selected_bank;
  202. return 0;
  203. }
  204. static int spi_nor_read_bar(void)
  205. {
  206. uint8_t selected_bank = 0U;
  207. int ret;
  208. ret = spi_nor_reg(nor_dev.bank_read_cmd, &selected_bank,
  209. 1U, SPI_MEM_DATA_IN);
  210. if (ret != 0) {
  211. return ret;
  212. }
  213. nor_dev.selected_bank = selected_bank;
  214. return 0;
  215. }
  216. int spi_nor_read(unsigned int offset, uintptr_t buffer, size_t length,
  217. size_t *length_read)
  218. {
  219. size_t remain_len;
  220. int ret;
  221. *length_read = 0U;
  222. nor_dev.read_op.addr.val = offset;
  223. nor_dev.read_op.data.buf = (void *)buffer;
  224. VERBOSE("%s offset %u length %zu\n", __func__, offset, length);
  225. while (length != 0U) {
  226. if ((nor_dev.flags & SPI_NOR_USE_BANK) != 0U) {
  227. ret = spi_nor_write_bar(nor_dev.read_op.addr.val);
  228. if (ret != 0) {
  229. return ret;
  230. }
  231. remain_len = (BANK_SIZE * (nor_dev.selected_bank + 1)) -
  232. nor_dev.read_op.addr.val;
  233. nor_dev.read_op.data.nbytes = MIN(length, remain_len);
  234. } else {
  235. nor_dev.read_op.data.nbytes = length;
  236. }
  237. ret = spi_mem_exec_op(&nor_dev.read_op);
  238. if (ret != 0) {
  239. spi_nor_clean_bar();
  240. return ret;
  241. }
  242. length -= nor_dev.read_op.data.nbytes;
  243. nor_dev.read_op.addr.val += nor_dev.read_op.data.nbytes;
  244. nor_dev.read_op.data.buf += nor_dev.read_op.data.nbytes;
  245. *length_read += nor_dev.read_op.data.nbytes;
  246. }
  247. if ((nor_dev.flags & SPI_NOR_USE_BANK) != 0U) {
  248. ret = spi_nor_clean_bar();
  249. if (ret != 0) {
  250. return ret;
  251. }
  252. }
  253. return 0;
  254. }
  255. int spi_nor_init(unsigned long long *size, unsigned int *erase_size)
  256. {
  257. int ret;
  258. uint8_t id;
  259. /* Default read command used */
  260. nor_dev.read_op.cmd.opcode = SPI_NOR_OP_READ;
  261. nor_dev.read_op.cmd.buswidth = SPI_MEM_BUSWIDTH_1_LINE;
  262. nor_dev.read_op.addr.nbytes = 3U;
  263. nor_dev.read_op.addr.buswidth = SPI_MEM_BUSWIDTH_1_LINE;
  264. nor_dev.read_op.data.buswidth = SPI_MEM_BUSWIDTH_1_LINE;
  265. nor_dev.read_op.data.dir = SPI_MEM_DATA_IN;
  266. if (plat_get_nor_data(&nor_dev) != 0) {
  267. return -EINVAL;
  268. }
  269. assert(nor_dev.size != 0U);
  270. if (nor_dev.size > BANK_SIZE) {
  271. nor_dev.flags |= SPI_NOR_USE_BANK;
  272. }
  273. *size = nor_dev.size;
  274. ret = spi_nor_read_id(&id);
  275. if (ret != 0) {
  276. return ret;
  277. }
  278. if ((nor_dev.flags & SPI_NOR_USE_BANK) != 0U) {
  279. switch (id) {
  280. case SPANSION_ID:
  281. nor_dev.bank_read_cmd = SPINOR_OP_BRRD;
  282. nor_dev.bank_write_cmd = SPINOR_OP_BRWR;
  283. break;
  284. default:
  285. nor_dev.bank_read_cmd = SPINOR_OP_RDEAR;
  286. nor_dev.bank_write_cmd = SPINOR_OP_WREAR;
  287. break;
  288. }
  289. }
  290. if (nor_dev.read_op.data.buswidth == 4U) {
  291. switch (id) {
  292. case MACRONIX_ID:
  293. INFO("Enable Macronix quad support\n");
  294. ret = spi_nor_macronix_quad_enable();
  295. break;
  296. case MICRON_ID:
  297. break;
  298. default:
  299. ret = spi_nor_quad_enable();
  300. break;
  301. }
  302. }
  303. if ((ret == 0) && ((nor_dev.flags & SPI_NOR_USE_BANK) != 0U)) {
  304. ret = spi_nor_read_bar();
  305. }
  306. return ret;
  307. }