omap_gpmc.c 11 KB

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