mxs_nand_spl.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2014 Gateworks Corporation
  4. * Author: Tim Harvey <tharvey@gateworks.com>
  5. */
  6. #include <common.h>
  7. #include <nand.h>
  8. #include <malloc.h>
  9. #include <mxs_nand.h>
  10. #include <linux/err.h>
  11. static struct mtd_info *mtd;
  12. static struct nand_chip nand_chip;
  13. static void mxs_nand_command(struct mtd_info *mtd, unsigned int command,
  14. int column, int page_addr)
  15. {
  16. register struct nand_chip *chip = mtd_to_nand(mtd);
  17. u32 timeo, time_start;
  18. /* write out the command to the device */
  19. chip->cmd_ctrl(mtd, command, NAND_CLE);
  20. /* Serially input address */
  21. if (column != -1) {
  22. chip->cmd_ctrl(mtd, column, NAND_ALE);
  23. chip->cmd_ctrl(mtd, column >> 8, NAND_ALE);
  24. }
  25. if (page_addr != -1) {
  26. chip->cmd_ctrl(mtd, page_addr, NAND_ALE);
  27. chip->cmd_ctrl(mtd, page_addr >> 8, NAND_ALE);
  28. /* One more address cycle for devices > 128MiB */
  29. if (chip->chipsize > (128 << 20))
  30. chip->cmd_ctrl(mtd, page_addr >> 16, NAND_ALE);
  31. }
  32. chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0);
  33. if (command == NAND_CMD_READ0) {
  34. chip->cmd_ctrl(mtd, NAND_CMD_READSTART, NAND_CLE);
  35. chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0);
  36. }
  37. /* wait for nand ready */
  38. ndelay(100);
  39. timeo = (CONFIG_SYS_HZ * 20) / 1000;
  40. time_start = get_timer(0);
  41. while (get_timer(time_start) < timeo) {
  42. if (chip->dev_ready(mtd))
  43. break;
  44. }
  45. }
  46. #if defined (CONFIG_SPL_NAND_IDENT)
  47. /* Trying to detect the NAND flash using ONFi, JEDEC, and (extended) IDs */
  48. static int mxs_flash_full_ident(struct mtd_info *mtd)
  49. {
  50. int nand_maf_id, nand_dev_id;
  51. struct nand_chip *chip = mtd_to_nand(mtd);
  52. struct nand_flash_dev *type;
  53. type = nand_get_flash_type(mtd, chip, &nand_maf_id, &nand_dev_id, NULL);
  54. if (IS_ERR(type)) {
  55. chip->select_chip(mtd, -1);
  56. return PTR_ERR(type);
  57. }
  58. return 0;
  59. }
  60. #else
  61. /* Trying to detect the NAND flash using ONFi only */
  62. static int mxs_flash_onfi_ident(struct mtd_info *mtd)
  63. {
  64. register struct nand_chip *chip = mtd_to_nand(mtd);
  65. int i;
  66. u8 mfg_id, dev_id;
  67. u8 id_data[8];
  68. struct nand_onfi_params *p = &chip->onfi_params;
  69. /* Reset the chip */
  70. chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
  71. /* Send the command for reading device ID */
  72. chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
  73. /* Read manufacturer and device IDs */
  74. mfg_id = chip->read_byte(mtd);
  75. dev_id = chip->read_byte(mtd);
  76. /* Try again to make sure */
  77. chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
  78. for (i = 0; i < 8; i++)
  79. id_data[i] = chip->read_byte(mtd);
  80. if (id_data[0] != mfg_id || id_data[1] != dev_id) {
  81. printf("second ID read did not match");
  82. return -1;
  83. }
  84. debug("0x%02x:0x%02x ", mfg_id, dev_id);
  85. /* read ONFI */
  86. chip->onfi_version = 0;
  87. chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
  88. if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
  89. chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I') {
  90. return -2;
  91. }
  92. /* we have ONFI, probe it */
  93. chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
  94. chip->read_buf(mtd, (uint8_t *)p, sizeof(*p));
  95. mtd->name = p->model;
  96. mtd->writesize = le32_to_cpu(p->byte_per_page);
  97. mtd->erasesize = le32_to_cpu(p->pages_per_block) * mtd->writesize;
  98. mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
  99. chip->chipsize = le32_to_cpu(p->blocks_per_lun);
  100. chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
  101. /* Calculate the address shift from the page size */
  102. chip->page_shift = ffs(mtd->writesize) - 1;
  103. chip->phys_erase_shift = ffs(mtd->erasesize) - 1;
  104. /* Convert chipsize to number of pages per chip -1 */
  105. chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
  106. chip->badblockbits = 8;
  107. debug("erasesize=%d (>>%d)\n", mtd->erasesize, chip->phys_erase_shift);
  108. debug("writesize=%d (>>%d)\n", mtd->writesize, chip->page_shift);
  109. debug("oobsize=%d\n", mtd->oobsize);
  110. debug("chipsize=%lld\n", chip->chipsize);
  111. return 0;
  112. }
  113. #endif /* CONFIG_SPL_NAND_IDENT */
  114. static int mxs_flash_ident(struct mtd_info *mtd)
  115. {
  116. int ret;
  117. #if defined (CONFIG_SPL_NAND_IDENT)
  118. ret = mxs_flash_full_ident(mtd);
  119. #else
  120. ret = mxs_flash_onfi_ident(mtd);
  121. #endif
  122. return ret;
  123. }
  124. static int mxs_read_page_ecc(struct mtd_info *mtd, void *buf, unsigned int page)
  125. {
  126. register struct nand_chip *chip = mtd_to_nand(mtd);
  127. int ret;
  128. chip->cmdfunc(mtd, NAND_CMD_READ0, 0x0, page);
  129. ret = nand_chip.ecc.read_page(mtd, chip, buf, 1, page);
  130. if (ret < 0) {
  131. printf("read_page failed %d\n", ret);
  132. return -1;
  133. }
  134. return 0;
  135. }
  136. static int is_badblock(struct mtd_info *mtd, loff_t offs, int allowbbt)
  137. {
  138. register struct nand_chip *chip = mtd_to_nand(mtd);
  139. unsigned int block = offs >> chip->phys_erase_shift;
  140. unsigned int page = offs >> chip->page_shift;
  141. debug("%s offs=0x%08x block:%d page:%d\n", __func__, (int)offs, block,
  142. page);
  143. chip->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
  144. memset(chip->oob_poi, 0, mtd->oobsize);
  145. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  146. return chip->oob_poi[0] != 0xff;
  147. }
  148. /* setup mtd and nand structs and init mxs_nand driver */
  149. void nand_init(void)
  150. {
  151. /* return if already initalized */
  152. if (nand_chip.numchips)
  153. return;
  154. /* init mxs nand driver */
  155. mxs_nand_init_spl(&nand_chip);
  156. mtd = nand_to_mtd(&nand_chip);
  157. /* set mtd functions */
  158. nand_chip.cmdfunc = mxs_nand_command;
  159. nand_chip.scan_bbt = nand_default_bbt;
  160. nand_chip.numchips = 1;
  161. /* identify flash device */
  162. if (mxs_flash_ident(mtd)) {
  163. printf("Failed to identify\n");
  164. nand_chip.numchips = 0; /* If fail, don't use nand */
  165. return;
  166. }
  167. /* allocate and initialize buffers */
  168. nand_chip.buffers = memalign(ARCH_DMA_MINALIGN,
  169. sizeof(*nand_chip.buffers));
  170. nand_chip.oob_poi = nand_chip.buffers->databuf + mtd->writesize;
  171. /* setup flash layout (does not scan as we override that) */
  172. mtd->size = nand_chip.chipsize;
  173. nand_chip.scan_bbt(mtd);
  174. mxs_nand_setup_ecc(mtd);
  175. }
  176. int nand_spl_load_image(uint32_t offs, unsigned int size, void *buf)
  177. {
  178. struct nand_chip *chip;
  179. unsigned int page;
  180. unsigned int nand_page_per_block;
  181. unsigned int sz = 0;
  182. chip = mtd_to_nand(mtd);
  183. if (!chip->numchips)
  184. return -ENODEV;
  185. page = offs >> chip->page_shift;
  186. nand_page_per_block = mtd->erasesize / mtd->writesize;
  187. debug("%s offset:0x%08x len:%d page:%d\n", __func__, offs, size, page);
  188. size = roundup(size, mtd->writesize);
  189. while (sz < size) {
  190. if (mxs_read_page_ecc(mtd, buf, page) < 0)
  191. return -1;
  192. sz += mtd->writesize;
  193. offs += mtd->writesize;
  194. page++;
  195. buf += mtd->writesize;
  196. /*
  197. * Check if we have crossed a block boundary, and if so
  198. * check for bad block.
  199. */
  200. if (!(page % nand_page_per_block)) {
  201. /*
  202. * Yes, new block. See if this block is good. If not,
  203. * loop until we find a good block.
  204. */
  205. while (is_badblock(mtd, offs, 1)) {
  206. page = page + nand_page_per_block;
  207. /* Check i we've reached the end of flash. */
  208. if (page >= mtd->size >> chip->page_shift)
  209. return -ENOMEM;
  210. }
  211. }
  212. }
  213. return 0;
  214. }
  215. int nand_default_bbt(struct mtd_info *mtd)
  216. {
  217. return 0;
  218. }
  219. void nand_deselect(void)
  220. {
  221. }