jz4740_nand.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * Platform independend driver for JZ4740.
  3. *
  4. * Copyright (c) 2007 Ingenic Semiconductor Inc.
  5. * Author: <jlwei@ingenic.cn>
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #include <common.h>
  10. #include <nand.h>
  11. #include <asm/io.h>
  12. #include <asm/jz4740.h>
  13. #define JZ_NAND_DATA_ADDR ((void __iomem *)0xB8000000)
  14. #define JZ_NAND_CMD_ADDR (JZ_NAND_DATA_ADDR + 0x8000)
  15. #define JZ_NAND_ADDR_ADDR (JZ_NAND_DATA_ADDR + 0x10000)
  16. #define JZ_NAND_ECC_CTRL_ENCODING BIT(3)
  17. #define JZ_NAND_ECC_CTRL_RS BIT(2)
  18. #define JZ_NAND_ECC_CTRL_RESET BIT(1)
  19. #define JZ_NAND_ECC_CTRL_ENABLE BIT(0)
  20. #define EMC_SMCR1_OPT_NAND 0x094c4400
  21. /* Optimize the timing of nand */
  22. static struct jz4740_emc * emc = (struct jz4740_emc *)JZ4740_EMC_BASE;
  23. static struct nand_ecclayout qi_lb60_ecclayout_2gb = {
  24. .eccbytes = 72,
  25. .eccpos = {
  26. 12, 13, 14, 15, 16, 17, 18, 19,
  27. 20, 21, 22, 23, 24, 25, 26, 27,
  28. 28, 29, 30, 31, 32, 33, 34, 35,
  29. 36, 37, 38, 39, 40, 41, 42, 43,
  30. 44, 45, 46, 47, 48, 49, 50, 51,
  31. 52, 53, 54, 55, 56, 57, 58, 59,
  32. 60, 61, 62, 63, 64, 65, 66, 67,
  33. 68, 69, 70, 71, 72, 73, 74, 75,
  34. 76, 77, 78, 79, 80, 81, 82, 83 },
  35. .oobfree = {
  36. {.offset = 2,
  37. .length = 10 },
  38. {.offset = 84,
  39. .length = 44 } }
  40. };
  41. static int is_reading;
  42. static void jz_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
  43. {
  44. struct nand_chip *this = mtd->priv;
  45. uint32_t reg;
  46. if (ctrl & NAND_CTRL_CHANGE) {
  47. if (ctrl & NAND_ALE)
  48. this->IO_ADDR_W = JZ_NAND_ADDR_ADDR;
  49. else if (ctrl & NAND_CLE)
  50. this->IO_ADDR_W = JZ_NAND_CMD_ADDR;
  51. else
  52. this->IO_ADDR_W = JZ_NAND_DATA_ADDR;
  53. reg = readl(&emc->nfcsr);
  54. if (ctrl & NAND_NCE)
  55. reg |= EMC_NFCSR_NFCE1;
  56. else
  57. reg &= ~EMC_NFCSR_NFCE1;
  58. writel(reg, &emc->nfcsr);
  59. }
  60. if (cmd != NAND_CMD_NONE)
  61. writeb(cmd, this->IO_ADDR_W);
  62. }
  63. static int jz_nand_device_ready(struct mtd_info *mtd)
  64. {
  65. return (readl(GPIO_PXPIN(2)) & 0x40000000) ? 1 : 0;
  66. }
  67. void board_nand_select_device(struct nand_chip *nand, int chip)
  68. {
  69. /*
  70. * Don't use "chip" to address the NAND device,
  71. * generate the cs from the address where it is encoded.
  72. */
  73. }
  74. static int jz_nand_rs_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
  75. u_char *ecc_code)
  76. {
  77. uint32_t status;
  78. int i;
  79. if (is_reading)
  80. return 0;
  81. do {
  82. status = readl(&emc->nfints);
  83. } while (!(status & EMC_NFINTS_ENCF));
  84. /* disable ecc */
  85. writel(readl(&emc->nfecr) & ~EMC_NFECR_ECCE, &emc->nfecr);
  86. for (i = 0; i < 9; i++)
  87. ecc_code[i] = readb(&emc->nfpar[i]);
  88. return 0;
  89. }
  90. static void jz_nand_hwctl(struct mtd_info *mtd, int mode)
  91. {
  92. uint32_t reg;
  93. writel(0, &emc->nfints);
  94. reg = readl(&emc->nfecr);
  95. reg |= JZ_NAND_ECC_CTRL_RESET;
  96. reg |= JZ_NAND_ECC_CTRL_ENABLE;
  97. reg |= JZ_NAND_ECC_CTRL_RS;
  98. switch (mode) {
  99. case NAND_ECC_READ:
  100. reg &= ~JZ_NAND_ECC_CTRL_ENCODING;
  101. is_reading = 1;
  102. break;
  103. case NAND_ECC_WRITE:
  104. reg |= JZ_NAND_ECC_CTRL_ENCODING;
  105. is_reading = 0;
  106. break;
  107. default:
  108. break;
  109. }
  110. writel(reg, &emc->nfecr);
  111. }
  112. /* Correct 1~9-bit errors in 512-bytes data */
  113. static void jz_rs_correct(unsigned char *dat, int idx, int mask)
  114. {
  115. int i;
  116. idx--;
  117. i = idx + (idx >> 3);
  118. if (i >= 512)
  119. return;
  120. mask <<= (idx & 0x7);
  121. dat[i] ^= mask & 0xff;
  122. if (i < 511)
  123. dat[i + 1] ^= (mask >> 8) & 0xff;
  124. }
  125. static int jz_nand_rs_correct_data(struct mtd_info *mtd, u_char *dat,
  126. u_char *read_ecc, u_char *calc_ecc)
  127. {
  128. int k;
  129. uint32_t errcnt, index, mask, status;
  130. /* Set PAR values */
  131. const uint8_t all_ff_ecc[] = {
  132. 0xcd, 0x9d, 0x90, 0x58, 0xf4, 0x8b, 0xff, 0xb7, 0x6f };
  133. if (read_ecc[0] == 0xff && read_ecc[1] == 0xff &&
  134. read_ecc[2] == 0xff && read_ecc[3] == 0xff &&
  135. read_ecc[4] == 0xff && read_ecc[5] == 0xff &&
  136. read_ecc[6] == 0xff && read_ecc[7] == 0xff &&
  137. read_ecc[8] == 0xff) {
  138. for (k = 0; k < 9; k++)
  139. writeb(all_ff_ecc[k], &emc->nfpar[k]);
  140. } else {
  141. for (k = 0; k < 9; k++)
  142. writeb(read_ecc[k], &emc->nfpar[k]);
  143. }
  144. /* Set PRDY */
  145. writel(readl(&emc->nfecr) | EMC_NFECR_PRDY, &emc->nfecr);
  146. /* Wait for completion */
  147. do {
  148. status = readl(&emc->nfints);
  149. } while (!(status & EMC_NFINTS_DECF));
  150. /* disable ecc */
  151. writel(readl(&emc->nfecr) & ~EMC_NFECR_ECCE, &emc->nfecr);
  152. /* Check decoding */
  153. if (!(status & EMC_NFINTS_ERR))
  154. return 0;
  155. if (status & EMC_NFINTS_UNCOR) {
  156. printf("uncorrectable ecc\n");
  157. return -1;
  158. }
  159. errcnt = (status & EMC_NFINTS_ERRCNT_MASK) >> EMC_NFINTS_ERRCNT_BIT;
  160. switch (errcnt) {
  161. case 4:
  162. index = (readl(&emc->nferr[3]) & EMC_NFERR_INDEX_MASK) >>
  163. EMC_NFERR_INDEX_BIT;
  164. mask = (readl(&emc->nferr[3]) & EMC_NFERR_MASK_MASK) >>
  165. EMC_NFERR_MASK_BIT;
  166. jz_rs_correct(dat, index, mask);
  167. case 3:
  168. index = (readl(&emc->nferr[2]) & EMC_NFERR_INDEX_MASK) >>
  169. EMC_NFERR_INDEX_BIT;
  170. mask = (readl(&emc->nferr[2]) & EMC_NFERR_MASK_MASK) >>
  171. EMC_NFERR_MASK_BIT;
  172. jz_rs_correct(dat, index, mask);
  173. case 2:
  174. index = (readl(&emc->nferr[1]) & EMC_NFERR_INDEX_MASK) >>
  175. EMC_NFERR_INDEX_BIT;
  176. mask = (readl(&emc->nferr[1]) & EMC_NFERR_MASK_MASK) >>
  177. EMC_NFERR_MASK_BIT;
  178. jz_rs_correct(dat, index, mask);
  179. case 1:
  180. index = (readl(&emc->nferr[0]) & EMC_NFERR_INDEX_MASK) >>
  181. EMC_NFERR_INDEX_BIT;
  182. mask = (readl(&emc->nferr[0]) & EMC_NFERR_MASK_MASK) >>
  183. EMC_NFERR_MASK_BIT;
  184. jz_rs_correct(dat, index, mask);
  185. default:
  186. break;
  187. }
  188. return errcnt;
  189. }
  190. /*
  191. * Main initialization routine
  192. */
  193. int board_nand_init(struct nand_chip *nand)
  194. {
  195. uint32_t reg;
  196. reg = readl(&emc->nfcsr);
  197. reg |= EMC_NFCSR_NFE1; /* EMC setup, Set NFE bit */
  198. writel(reg, &emc->nfcsr);
  199. writel(EMC_SMCR1_OPT_NAND, &emc->smcr[1]);
  200. nand->IO_ADDR_R = JZ_NAND_DATA_ADDR;
  201. nand->IO_ADDR_W = JZ_NAND_DATA_ADDR;
  202. nand->cmd_ctrl = jz_nand_cmd_ctrl;
  203. nand->dev_ready = jz_nand_device_ready;
  204. nand->ecc.hwctl = jz_nand_hwctl;
  205. nand->ecc.correct = jz_nand_rs_correct_data;
  206. nand->ecc.calculate = jz_nand_rs_calculate_ecc;
  207. nand->ecc.mode = NAND_ECC_HW_OOB_FIRST;
  208. nand->ecc.size = CONFIG_SYS_NAND_ECCSIZE;
  209. nand->ecc.bytes = CONFIG_SYS_NAND_ECCBYTES;
  210. nand->ecc.strength = 4;
  211. nand->ecc.layout = &qi_lb60_ecclayout_2gb;
  212. nand->chip_delay = 50;
  213. nand->bbt_options |= NAND_BBT_USE_FLASH;
  214. return 0;
  215. }