omap_gpmc.c 10 KB

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