jz4740_nand.c 6.2 KB

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