mxs_nand_spl.c 6.0 KB

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