raw_nand.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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/raw_nand.h>
  12. #include <lib/utils.h>
  13. #include <platform_def.h>
  14. #define ONFI_SIGNATURE_ADDR 0x20U
  15. /* CRC calculation */
  16. #define CRC_POLYNOM 0x8005U
  17. #define CRC_INIT_VALUE 0x4F4EU
  18. /* Status register */
  19. #define NAND_STATUS_READY BIT(6)
  20. static struct rawnand_device rawnand_dev;
  21. #pragma weak plat_get_raw_nand_data
  22. int plat_get_raw_nand_data(struct rawnand_device *device)
  23. {
  24. return 0;
  25. }
  26. static int nand_send_cmd(uint8_t cmd, unsigned int tim)
  27. {
  28. struct nand_req req;
  29. zeromem(&req, sizeof(struct nand_req));
  30. req.nand = rawnand_dev.nand_dev;
  31. req.type = NAND_REQ_CMD | cmd;
  32. req.inst_delay = tim;
  33. return rawnand_dev.ops->exec(&req);
  34. }
  35. static int nand_send_addr(uint8_t addr, unsigned int tim)
  36. {
  37. struct nand_req req;
  38. zeromem(&req, sizeof(struct nand_req));
  39. req.nand = rawnand_dev.nand_dev;
  40. req.type = NAND_REQ_ADDR;
  41. req.addr = &addr;
  42. req.inst_delay = tim;
  43. return rawnand_dev.ops->exec(&req);
  44. }
  45. static int nand_send_wait(unsigned int delay, unsigned int tim)
  46. {
  47. struct nand_req req;
  48. zeromem(&req, sizeof(struct nand_req));
  49. req.nand = rawnand_dev.nand_dev;
  50. req.type = NAND_REQ_WAIT;
  51. req.inst_delay = tim;
  52. req.delay_ms = delay;
  53. return rawnand_dev.ops->exec(&req);
  54. }
  55. static int nand_read_data(uint8_t *data, unsigned int length, bool use_8bit)
  56. {
  57. struct nand_req req;
  58. zeromem(&req, sizeof(struct nand_req));
  59. req.nand = rawnand_dev.nand_dev;
  60. req.type = NAND_REQ_DATAIN | (use_8bit ? NAND_REQ_BUS_WIDTH_8 : 0U);
  61. req.addr = data;
  62. req.length = length;
  63. return rawnand_dev.ops->exec(&req);
  64. }
  65. int nand_change_read_column_cmd(unsigned int offset, uintptr_t buffer,
  66. unsigned int len)
  67. {
  68. int ret;
  69. uint8_t addr[2];
  70. unsigned int i;
  71. ret = nand_send_cmd(NAND_CMD_CHANGE_1ST, 0U);
  72. if (ret != 0) {
  73. return ret;
  74. }
  75. if (rawnand_dev.nand_dev->buswidth == NAND_BUS_WIDTH_16) {
  76. offset /= 2U;
  77. }
  78. addr[0] = offset;
  79. addr[1] = offset >> 8;
  80. for (i = 0; i < 2U; i++) {
  81. ret = nand_send_addr(addr[i], 0U);
  82. if (ret != 0) {
  83. return ret;
  84. }
  85. }
  86. ret = nand_send_cmd(NAND_CMD_CHANGE_2ND, NAND_TCCS_MIN);
  87. if (ret != 0) {
  88. return ret;
  89. }
  90. return nand_read_data((uint8_t *)buffer, len, false);
  91. }
  92. int nand_read_page_cmd(unsigned int page, unsigned int offset,
  93. uintptr_t buffer, unsigned int len)
  94. {
  95. uint8_t addr[5];
  96. uint8_t i = 0U;
  97. uint8_t j;
  98. int ret;
  99. VERBOSE(">%s page %u offset %u buffer 0x%lx\n", __func__, page, offset,
  100. buffer);
  101. if (rawnand_dev.nand_dev->buswidth == NAND_BUS_WIDTH_16) {
  102. offset /= 2U;
  103. }
  104. addr[i++] = offset;
  105. addr[i++] = offset >> 8;
  106. addr[i++] = page;
  107. addr[i++] = page >> 8;
  108. if (rawnand_dev.nand_dev->size > SZ_128M) {
  109. addr[i++] = page >> 16;
  110. }
  111. ret = nand_send_cmd(NAND_CMD_READ_1ST, 0U);
  112. if (ret != 0) {
  113. return ret;
  114. }
  115. for (j = 0U; j < i; j++) {
  116. ret = nand_send_addr(addr[j], 0U);
  117. if (ret != 0) {
  118. return ret;
  119. }
  120. }
  121. ret = nand_send_cmd(NAND_CMD_READ_2ND, NAND_TWB_MAX);
  122. if (ret != 0) {
  123. return ret;
  124. }
  125. ret = nand_send_wait(PSEC_TO_MSEC(NAND_TR_MAX), NAND_TRR_MIN);
  126. if (ret != 0) {
  127. return ret;
  128. }
  129. if (buffer != 0U) {
  130. ret = nand_read_data((uint8_t *)buffer, len, false);
  131. }
  132. return ret;
  133. }
  134. static int nand_status(uint8_t *status)
  135. {
  136. int ret;
  137. ret = nand_send_cmd(NAND_CMD_STATUS, NAND_TWHR_MIN);
  138. if (ret != 0) {
  139. return ret;
  140. }
  141. if (status != NULL) {
  142. ret = nand_read_data(status, 1U, true);
  143. }
  144. return ret;
  145. }
  146. int nand_wait_ready(unsigned int delay_ms)
  147. {
  148. uint8_t status;
  149. int ret;
  150. uint64_t timeout;
  151. /* Wait before reading status */
  152. udelay(1);
  153. ret = nand_status(NULL);
  154. if (ret != 0) {
  155. return ret;
  156. }
  157. timeout = timeout_init_us(delay_ms * 1000U);
  158. while (!timeout_elapsed(timeout)) {
  159. ret = nand_read_data(&status, 1U, true);
  160. if (ret != 0) {
  161. return ret;
  162. }
  163. if ((status & NAND_STATUS_READY) != 0U) {
  164. return nand_send_cmd(NAND_CMD_READ_1ST, 0U);
  165. }
  166. udelay(10);
  167. }
  168. return -ETIMEDOUT;
  169. }
  170. static int nand_reset(void)
  171. {
  172. int ret;
  173. ret = nand_send_cmd(NAND_CMD_RESET, NAND_TWB_MAX);
  174. if (ret != 0) {
  175. return ret;
  176. }
  177. return nand_send_wait(PSEC_TO_MSEC(NAND_TRST_MAX), 0U);
  178. }
  179. #if NAND_ONFI_DETECT
  180. static uint16_t nand_check_crc(uint16_t crc, uint8_t *data_in,
  181. unsigned int data_len)
  182. {
  183. uint32_t i;
  184. uint32_t j;
  185. uint32_t bit;
  186. for (i = 0U; i < data_len; i++) {
  187. uint8_t cur_param = *data_in++;
  188. for (j = BIT(7); j != 0U; j >>= 1) {
  189. bit = crc & BIT(15);
  190. crc <<= 1;
  191. if ((cur_param & j) != 0U) {
  192. bit ^= BIT(15);
  193. }
  194. if (bit != 0U) {
  195. crc ^= CRC_POLYNOM;
  196. }
  197. }
  198. crc &= GENMASK(15, 0);
  199. }
  200. return crc;
  201. }
  202. static int nand_read_id(uint8_t addr, uint8_t *id, unsigned int size)
  203. {
  204. int ret;
  205. ret = nand_send_cmd(NAND_CMD_READID, 0U);
  206. if (ret != 0) {
  207. return ret;
  208. }
  209. ret = nand_send_addr(addr, NAND_TWHR_MIN);
  210. if (ret != 0) {
  211. return ret;
  212. }
  213. return nand_read_data(id, size, true);
  214. }
  215. static int nand_read_param_page(void)
  216. {
  217. struct nand_param_page page;
  218. uint8_t addr = 0U;
  219. int ret;
  220. ret = nand_send_cmd(NAND_CMD_READ_PARAM_PAGE, 0U);
  221. if (ret != 0) {
  222. return ret;
  223. }
  224. ret = nand_send_addr(addr, NAND_TWB_MAX);
  225. if (ret != 0) {
  226. return ret;
  227. }
  228. ret = nand_send_wait(PSEC_TO_MSEC(NAND_TR_MAX), NAND_TRR_MIN);
  229. if (ret != 0) {
  230. return ret;
  231. }
  232. ret = nand_read_data((uint8_t *)&page, sizeof(page), true);
  233. if (ret != 0) {
  234. return ret;
  235. }
  236. if (strncmp((char *)&page.page_sig, "ONFI", 4) != 0) {
  237. WARN("Error ONFI detection\n");
  238. return -EINVAL;
  239. }
  240. if (nand_check_crc(CRC_INIT_VALUE, (uint8_t *)&page, 254U) !=
  241. page.crc16) {
  242. WARN("Error reading param\n");
  243. return -EINVAL;
  244. }
  245. if ((page.features & ONFI_FEAT_BUS_WIDTH_16) != 0U) {
  246. rawnand_dev.nand_dev->buswidth = NAND_BUS_WIDTH_16;
  247. } else {
  248. rawnand_dev.nand_dev->buswidth = NAND_BUS_WIDTH_8;
  249. }
  250. rawnand_dev.nand_dev->block_size = page.num_pages_per_blk *
  251. page.bytes_per_page;
  252. rawnand_dev.nand_dev->page_size = page.bytes_per_page;
  253. rawnand_dev.nand_dev->size = page.num_pages_per_blk *
  254. page.bytes_per_page *
  255. page.num_blk_in_lun * page.num_lun;
  256. if (page.nb_ecc_bits != GENMASK_32(7, 0)) {
  257. rawnand_dev.nand_dev->ecc.max_bit_corr = page.nb_ecc_bits;
  258. rawnand_dev.nand_dev->ecc.size = SZ_512;
  259. }
  260. VERBOSE("Page size %u, block_size %u, Size %llu, ecc %u, buswidth %u\n",
  261. rawnand_dev.nand_dev->page_size,
  262. rawnand_dev.nand_dev->block_size, rawnand_dev.nand_dev->size,
  263. rawnand_dev.nand_dev->ecc.max_bit_corr,
  264. rawnand_dev.nand_dev->buswidth);
  265. return 0;
  266. }
  267. static int detect_onfi(void)
  268. {
  269. int ret;
  270. char id[4];
  271. ret = nand_read_id(ONFI_SIGNATURE_ADDR, (uint8_t *)id, sizeof(id));
  272. if (ret != 0) {
  273. return ret;
  274. }
  275. if (strncmp(id, "ONFI", sizeof(id)) != 0) {
  276. WARN("NAND Non ONFI detected\n");
  277. return -ENODEV;
  278. }
  279. return nand_read_param_page();
  280. }
  281. #endif
  282. static int nand_mtd_block_is_bad(unsigned int block)
  283. {
  284. unsigned int nbpages_per_block = rawnand_dev.nand_dev->block_size /
  285. rawnand_dev.nand_dev->page_size;
  286. uint8_t bbm_marker[2];
  287. uint8_t page;
  288. int ret;
  289. for (page = 0U; page < 2U; page++) {
  290. ret = nand_read_page_cmd(block * nbpages_per_block,
  291. rawnand_dev.nand_dev->page_size,
  292. (uintptr_t)bbm_marker,
  293. sizeof(bbm_marker));
  294. if (ret != 0) {
  295. return ret;
  296. }
  297. if ((bbm_marker[0] != GENMASK_32(7, 0)) ||
  298. (bbm_marker[1] != GENMASK_32(7, 0))) {
  299. WARN("Block %u is bad\n", block);
  300. return 1;
  301. }
  302. }
  303. return 0;
  304. }
  305. static int nand_mtd_read_page_raw(struct nand_device *nand, unsigned int page,
  306. uintptr_t buffer)
  307. {
  308. return nand_read_page_cmd(page, 0U, buffer,
  309. rawnand_dev.nand_dev->page_size);
  310. }
  311. void nand_raw_ctrl_init(const struct nand_ctrl_ops *ops)
  312. {
  313. rawnand_dev.ops = ops;
  314. }
  315. int nand_raw_init(unsigned long long *size, unsigned int *erase_size)
  316. {
  317. int ret;
  318. rawnand_dev.nand_dev = get_nand_device();
  319. if (rawnand_dev.nand_dev == NULL) {
  320. return -EINVAL;
  321. }
  322. rawnand_dev.nand_dev->mtd_block_is_bad = nand_mtd_block_is_bad;
  323. rawnand_dev.nand_dev->mtd_read_page = nand_mtd_read_page_raw;
  324. rawnand_dev.nand_dev->ecc.mode = NAND_ECC_NONE;
  325. if ((rawnand_dev.ops->setup == NULL) ||
  326. (rawnand_dev.ops->exec == NULL)) {
  327. return -ENODEV;
  328. }
  329. ret = nand_reset();
  330. if (ret != 0) {
  331. return ret;
  332. }
  333. #if NAND_ONFI_DETECT
  334. if (detect_onfi() != 0) {
  335. WARN("Detect ONFI failed\n");
  336. }
  337. #endif
  338. if (plat_get_raw_nand_data(&rawnand_dev) != 0) {
  339. return -EINVAL;
  340. }
  341. assert((rawnand_dev.nand_dev->page_size != 0U) &&
  342. (rawnand_dev.nand_dev->block_size != 0U) &&
  343. (rawnand_dev.nand_dev->size != 0U));
  344. *size = rawnand_dev.nand_dev->size;
  345. *erase_size = rawnand_dev.nand_dev->block_size;
  346. rawnand_dev.ops->setup(rawnand_dev.nand_dev);
  347. return 0;
  348. }