jz4740_nand.c 6.0 KB

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