omap_gpmc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. * (C) Copyright 2004-2008 Texas Instruments, <www.ti.com>
  3. * Rohit Choraria <rohitkc@ti.com>
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <asm/io.h>
  25. #include <asm/errno.h>
  26. #include <asm/arch/mem.h>
  27. #include <asm/arch/omap_gpmc.h>
  28. #include <linux/mtd/nand_ecc.h>
  29. #include <nand.h>
  30. static uint8_t cs;
  31. static struct nand_ecclayout hw_nand_oob = GPMC_NAND_HW_ECC_LAYOUT;
  32. /*
  33. * omap_nand_hwcontrol - Set the address pointers corretly for the
  34. * following address/data/command operation
  35. */
  36. static void omap_nand_hwcontrol(struct mtd_info *mtd, int32_t cmd,
  37. uint32_t ctrl)
  38. {
  39. register struct nand_chip *this = mtd->priv;
  40. /*
  41. * Point the IO_ADDR to DATA and ADDRESS registers instead
  42. * of chip address
  43. */
  44. switch (ctrl) {
  45. case NAND_CTRL_CHANGE | NAND_CTRL_CLE:
  46. this->IO_ADDR_W = (void __iomem *)&gpmc_cfg->cs[cs].nand_cmd;
  47. break;
  48. case NAND_CTRL_CHANGE | NAND_CTRL_ALE:
  49. this->IO_ADDR_W = (void __iomem *)&gpmc_cfg->cs[cs].nand_adr;
  50. break;
  51. case NAND_CTRL_CHANGE | NAND_NCE:
  52. this->IO_ADDR_W = (void __iomem *)&gpmc_cfg->cs[cs].nand_dat;
  53. break;
  54. }
  55. if (cmd != NAND_CMD_NONE)
  56. writeb(cmd, this->IO_ADDR_W);
  57. }
  58. /*
  59. * omap_hwecc_init - Initialize the Hardware ECC for NAND flash in
  60. * GPMC controller
  61. * @mtd: MTD device structure
  62. *
  63. */
  64. static void omap_hwecc_init(struct nand_chip *chip)
  65. {
  66. /*
  67. * Init ECC Control Register
  68. * Clear all ECC | Enable Reg1
  69. */
  70. writel(ECCCLEAR | ECCRESULTREG1, &gpmc_cfg->ecc_control);
  71. writel(ECCSIZE1 | ECCSIZE0 | ECCSIZE0SEL, &gpmc_cfg->ecc_size_config);
  72. }
  73. /*
  74. * gen_true_ecc - This function will generate true ECC value, which
  75. * can be used when correcting data read from NAND flash memory core
  76. *
  77. * @ecc_buf: buffer to store ecc code
  78. *
  79. * @return: re-formatted ECC value
  80. */
  81. static uint32_t gen_true_ecc(uint8_t *ecc_buf)
  82. {
  83. return ecc_buf[0] | (ecc_buf[1] << 16) | ((ecc_buf[2] & 0xF0) << 20) |
  84. ((ecc_buf[2] & 0x0F) << 8);
  85. }
  86. /*
  87. * omap_correct_data - Compares the ecc read from nand spare area with ECC
  88. * registers values and corrects one bit error if it has occured
  89. * Further details can be had from OMAP TRM and the following selected links:
  90. * http://en.wikipedia.org/wiki/Hamming_code
  91. * http://www.cs.utexas.edu/users/plaxton/c/337/05f/slides/ErrorCorrection-4.pdf
  92. *
  93. * @mtd: MTD device structure
  94. * @dat: page data
  95. * @read_ecc: ecc read from nand flash
  96. * @calc_ecc: ecc read from ECC registers
  97. *
  98. * @return 0 if data is OK or corrected, else returns -1
  99. */
  100. static int omap_correct_data(struct mtd_info *mtd, uint8_t *dat,
  101. uint8_t *read_ecc, uint8_t *calc_ecc)
  102. {
  103. uint32_t orig_ecc, new_ecc, res, hm;
  104. uint16_t parity_bits, byte;
  105. uint8_t bit;
  106. /* Regenerate the orginal ECC */
  107. orig_ecc = gen_true_ecc(read_ecc);
  108. new_ecc = gen_true_ecc(calc_ecc);
  109. /* Get the XOR of real ecc */
  110. res = orig_ecc ^ new_ecc;
  111. if (res) {
  112. /* Get the hamming width */
  113. hm = hweight32(res);
  114. /* Single bit errors can be corrected! */
  115. if (hm == 12) {
  116. /* Correctable data! */
  117. parity_bits = res >> 16;
  118. bit = (parity_bits & 0x7);
  119. byte = (parity_bits >> 3) & 0x1FF;
  120. /* Flip the bit to correct */
  121. dat[byte] ^= (0x1 << bit);
  122. } else if (hm == 1) {
  123. printf("Error: Ecc is wrong\n");
  124. /* ECC itself is corrupted */
  125. return 2;
  126. } else {
  127. /*
  128. * hm distance != parity pairs OR one, could mean 2 bit
  129. * error OR potentially be on a blank page..
  130. * orig_ecc: contains spare area data from nand flash.
  131. * new_ecc: generated ecc while reading data area.
  132. * Note: if the ecc = 0, all data bits from which it was
  133. * generated are 0xFF.
  134. * The 3 byte(24 bits) ecc is generated per 512byte
  135. * chunk of a page. If orig_ecc(from spare area)
  136. * is 0xFF && new_ecc(computed now from data area)=0x0,
  137. * this means that data area is 0xFF and spare area is
  138. * 0xFF. A sure sign of a erased page!
  139. */
  140. if ((orig_ecc == 0x0FFF0FFF) && (new_ecc == 0x00000000))
  141. return 0;
  142. printf("Error: Bad compare! failed\n");
  143. /* detected 2 bit error */
  144. return -1;
  145. }
  146. }
  147. return 0;
  148. }
  149. /*
  150. * omap_calculate_ecc - Generate non-inverted ECC bytes.
  151. *
  152. * Using noninverted ECC can be considered ugly since writing a blank
  153. * page ie. padding will clear the ECC bytes. This is no problem as
  154. * long nobody is trying to write data on the seemingly unused page.
  155. * Reading an erased page will produce an ECC mismatch between
  156. * generated and read ECC bytes that has to be dealt with separately.
  157. * E.g. if page is 0xFF (fresh erased), and if HW ECC engine within GPMC
  158. * is used, the result of read will be 0x0 while the ECC offsets of the
  159. * spare area will be 0xFF which will result in an ECC mismatch.
  160. * @mtd: MTD structure
  161. * @dat: unused
  162. * @ecc_code: ecc_code buffer
  163. */
  164. static int omap_calculate_ecc(struct mtd_info *mtd, const uint8_t *dat,
  165. uint8_t *ecc_code)
  166. {
  167. u_int32_t val;
  168. /* Start Reading from HW ECC1_Result = 0x200 */
  169. val = readl(&gpmc_cfg->ecc1_result);
  170. ecc_code[0] = val & 0xFF;
  171. ecc_code[1] = (val >> 16) & 0xFF;
  172. ecc_code[2] = ((val >> 8) & 0x0F) | ((val >> 20) & 0xF0);
  173. /*
  174. * Stop reading anymore ECC vals and clear old results
  175. * enable will be called if more reads are required
  176. */
  177. writel(0x000, &gpmc_cfg->ecc_config);
  178. return 0;
  179. }
  180. /*
  181. * omap_enable_ecc - This function enables the hardware ecc functionality
  182. * @mtd: MTD device structure
  183. * @mode: Read/Write mode
  184. */
  185. static void omap_enable_hwecc(struct mtd_info *mtd, int32_t mode)
  186. {
  187. struct nand_chip *chip = mtd->priv;
  188. uint32_t val, dev_width = (chip->options & NAND_BUSWIDTH_16) >> 1;
  189. switch (mode) {
  190. case NAND_ECC_READ:
  191. case NAND_ECC_WRITE:
  192. /* Clear the ecc result registers, select ecc reg as 1 */
  193. writel(ECCCLEAR | ECCRESULTREG1, &gpmc_cfg->ecc_control);
  194. /*
  195. * Size 0 = 0xFF, Size1 is 0xFF - both are 512 bytes
  196. * tell all regs to generate size0 sized regs
  197. * we just have a single ECC engine for all CS
  198. */
  199. writel(ECCSIZE1 | ECCSIZE0 | ECCSIZE0SEL,
  200. &gpmc_cfg->ecc_size_config);
  201. val = (dev_width << 7) | (cs << 1) | (0x1);
  202. writel(val, &gpmc_cfg->ecc_config);
  203. break;
  204. default:
  205. printf("Error: Unrecognized Mode[%d]!\n", mode);
  206. break;
  207. }
  208. }
  209. /*
  210. * omap_nand_switch_ecc - switch the ECC operation b/w h/w ecc and s/w ecc.
  211. * The default is to come up on s/w ecc
  212. *
  213. * @hardware - 1 -switch to h/w ecc, 0 - s/w ecc
  214. *
  215. */
  216. void omap_nand_switch_ecc(int32_t hardware)
  217. {
  218. struct nand_chip *nand;
  219. struct mtd_info *mtd;
  220. if (nand_curr_device < 0 ||
  221. nand_curr_device >= CONFIG_SYS_MAX_NAND_DEVICE ||
  222. !nand_info[nand_curr_device].name) {
  223. printf("Error: Can't switch ecc, no devices available\n");
  224. return;
  225. }
  226. mtd = &nand_info[nand_curr_device];
  227. nand = mtd->priv;
  228. nand->options |= NAND_OWN_BUFFERS;
  229. /* Reset ecc interface */
  230. nand->ecc.read_page = NULL;
  231. nand->ecc.write_page = NULL;
  232. nand->ecc.read_oob = NULL;
  233. nand->ecc.write_oob = NULL;
  234. nand->ecc.hwctl = NULL;
  235. nand->ecc.correct = NULL;
  236. nand->ecc.calculate = NULL;
  237. /* Setup the ecc configurations again */
  238. if (hardware) {
  239. nand->ecc.mode = NAND_ECC_HW;
  240. nand->ecc.layout = &hw_nand_oob;
  241. nand->ecc.size = 512;
  242. nand->ecc.bytes = 3;
  243. nand->ecc.hwctl = omap_enable_hwecc;
  244. nand->ecc.correct = omap_correct_data;
  245. nand->ecc.calculate = omap_calculate_ecc;
  246. omap_hwecc_init(nand);
  247. printf("HW ECC selected\n");
  248. } else {
  249. nand->ecc.mode = NAND_ECC_SOFT;
  250. /* Use mtd default settings */
  251. nand->ecc.layout = NULL;
  252. printf("SW ECC selected\n");
  253. }
  254. /* Update NAND handling after ECC mode switch */
  255. nand_scan_tail(mtd);
  256. nand->options &= ~NAND_OWN_BUFFERS;
  257. }
  258. /*
  259. * Board-specific NAND initialization. The following members of the
  260. * argument are board-specific:
  261. * - IO_ADDR_R: address to read the 8 I/O lines of the flash device
  262. * - IO_ADDR_W: address to write the 8 I/O lines of the flash device
  263. * - cmd_ctrl: hardwarespecific function for accesing control-lines
  264. * - waitfunc: hardwarespecific function for accesing device ready/busy line
  265. * - ecc.hwctl: function to enable (reset) hardware ecc generator
  266. * - ecc.mode: mode of ecc, see defines
  267. * - chip_delay: chip dependent delay for transfering data from array to
  268. * read regs (tR)
  269. * - options: various chip options. They can partly be set to inform
  270. * nand_scan about special functionality. See the defines for further
  271. * explanation
  272. */
  273. int board_nand_init(struct nand_chip *nand)
  274. {
  275. int32_t gpmc_config = 0;
  276. cs = 0;
  277. /*
  278. * xloader/Uboot's gpmc configuration would have configured GPMC for
  279. * nand type of memory. The following logic scans and latches on to the
  280. * first CS with NAND type memory.
  281. * TBD: need to make this logic generic to handle multiple CS NAND
  282. * devices.
  283. */
  284. while (cs < GPMC_MAX_CS) {
  285. /* Check if NAND type is set */
  286. if ((readl(&gpmc_cfg->cs[cs].config1) & 0xC00) == 0x800) {
  287. /* Found it!! */
  288. break;
  289. }
  290. cs++;
  291. }
  292. if (cs >= GPMC_MAX_CS) {
  293. printf("NAND: Unable to find NAND settings in "
  294. "GPMC Configuration - quitting\n");
  295. return -ENODEV;
  296. }
  297. gpmc_config = readl(&gpmc_cfg->config);
  298. /* Disable Write protect */
  299. gpmc_config |= 0x10;
  300. writel(gpmc_config, &gpmc_cfg->config);
  301. nand->IO_ADDR_R = (void __iomem *)&gpmc_cfg->cs[cs].nand_dat;
  302. nand->IO_ADDR_W = (void __iomem *)&gpmc_cfg->cs[cs].nand_cmd;
  303. nand->cmd_ctrl = omap_nand_hwcontrol;
  304. nand->options = NAND_NO_PADDING | NAND_CACHEPRG | NAND_NO_AUTOINCR;
  305. /* If we are 16 bit dev, our gpmc config tells us that */
  306. if ((readl(&gpmc_cfg->cs[cs].config1) & 0x3000) == 0x1000)
  307. nand->options |= NAND_BUSWIDTH_16;
  308. nand->chip_delay = 100;
  309. /* Default ECC mode */
  310. nand->ecc.mode = NAND_ECC_SOFT;
  311. return 0;
  312. }