davinci_nand.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. /*
  2. * davinci_nand.c - NAND Flash Driver for DaVinci family chips
  3. *
  4. * Copyright © 2006 Texas Instruments.
  5. *
  6. * Port to 2.6.23 Copyright © 2008 by:
  7. * Sander Huijsen <Shuijsen@optelecom-nkf.com>
  8. * Troy Kisky <troy.kisky@boundarydevices.com>
  9. * Dirk Behme <Dirk.Behme@gmail.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/init.h>
  27. #include <linux/module.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/err.h>
  30. #include <linux/clk.h>
  31. #include <linux/io.h>
  32. #include <linux/mtd/nand.h>
  33. #include <linux/mtd/partitions.h>
  34. #include <mach/cpu.h>
  35. #include <mach/nand.h>
  36. #include <asm/mach-types.h>
  37. #ifdef CONFIG_MTD_PARTITIONS
  38. static inline int mtd_has_partitions(void) { return 1; }
  39. #else
  40. static inline int mtd_has_partitions(void) { return 0; }
  41. #endif
  42. #ifdef CONFIG_MTD_CMDLINE_PARTS
  43. static inline int mtd_has_cmdlinepart(void) { return 1; }
  44. #else
  45. static inline int mtd_has_cmdlinepart(void) { return 0; }
  46. #endif
  47. /*
  48. * This is a device driver for the NAND flash controller found on the
  49. * various DaVinci family chips. It handles up to four SoC chipselects,
  50. * and some flavors of secondary chipselect (e.g. based on A12) as used
  51. * with multichip packages.
  52. *
  53. * The 1-bit ECC hardware is supported, but not yet the newer 4-bit ECC
  54. * available on chips like the DM355 and OMAP-L137 and needed with the
  55. * more error-prone MLC NAND chips.
  56. *
  57. * This driver assumes EM_WAIT connects all the NAND devices' RDY/nBUSY
  58. * outputs in a "wire-AND" configuration, with no per-chip signals.
  59. */
  60. struct davinci_nand_info {
  61. struct mtd_info mtd;
  62. struct nand_chip chip;
  63. struct device *dev;
  64. struct clk *clk;
  65. bool partitioned;
  66. void __iomem *base;
  67. void __iomem *vaddr;
  68. uint32_t ioaddr;
  69. uint32_t current_cs;
  70. uint32_t mask_chipsel;
  71. uint32_t mask_ale;
  72. uint32_t mask_cle;
  73. uint32_t core_chipsel;
  74. };
  75. static DEFINE_SPINLOCK(davinci_nand_lock);
  76. #define to_davinci_nand(m) container_of(m, struct davinci_nand_info, mtd)
  77. static inline unsigned int davinci_nand_readl(struct davinci_nand_info *info,
  78. int offset)
  79. {
  80. return __raw_readl(info->base + offset);
  81. }
  82. static inline void davinci_nand_writel(struct davinci_nand_info *info,
  83. int offset, unsigned long value)
  84. {
  85. __raw_writel(value, info->base + offset);
  86. }
  87. /*----------------------------------------------------------------------*/
  88. /*
  89. * Access to hardware control lines: ALE, CLE, secondary chipselect.
  90. */
  91. static void nand_davinci_hwcontrol(struct mtd_info *mtd, int cmd,
  92. unsigned int ctrl)
  93. {
  94. struct davinci_nand_info *info = to_davinci_nand(mtd);
  95. uint32_t addr = info->current_cs;
  96. struct nand_chip *nand = mtd->priv;
  97. /* Did the control lines change? */
  98. if (ctrl & NAND_CTRL_CHANGE) {
  99. if ((ctrl & NAND_CTRL_CLE) == NAND_CTRL_CLE)
  100. addr |= info->mask_cle;
  101. else if ((ctrl & NAND_CTRL_ALE) == NAND_CTRL_ALE)
  102. addr |= info->mask_ale;
  103. nand->IO_ADDR_W = (void __iomem __force *)addr;
  104. }
  105. if (cmd != NAND_CMD_NONE)
  106. iowrite8(cmd, nand->IO_ADDR_W);
  107. }
  108. static void nand_davinci_select_chip(struct mtd_info *mtd, int chip)
  109. {
  110. struct davinci_nand_info *info = to_davinci_nand(mtd);
  111. uint32_t addr = info->ioaddr;
  112. /* maybe kick in a second chipselect */
  113. if (chip > 0)
  114. addr |= info->mask_chipsel;
  115. info->current_cs = addr;
  116. info->chip.IO_ADDR_W = (void __iomem __force *)addr;
  117. info->chip.IO_ADDR_R = info->chip.IO_ADDR_W;
  118. }
  119. /*----------------------------------------------------------------------*/
  120. /*
  121. * 1-bit hardware ECC ... context maintained for each core chipselect
  122. */
  123. static inline uint32_t nand_davinci_readecc_1bit(struct mtd_info *mtd)
  124. {
  125. struct davinci_nand_info *info = to_davinci_nand(mtd);
  126. return davinci_nand_readl(info, NANDF1ECC_OFFSET
  127. + 4 * info->core_chipsel);
  128. }
  129. static void nand_davinci_hwctl_1bit(struct mtd_info *mtd, int mode)
  130. {
  131. struct davinci_nand_info *info;
  132. uint32_t nandcfr;
  133. unsigned long flags;
  134. info = to_davinci_nand(mtd);
  135. /* Reset ECC hardware */
  136. nand_davinci_readecc_1bit(mtd);
  137. spin_lock_irqsave(&davinci_nand_lock, flags);
  138. /* Restart ECC hardware */
  139. nandcfr = davinci_nand_readl(info, NANDFCR_OFFSET);
  140. nandcfr |= BIT(8 + info->core_chipsel);
  141. davinci_nand_writel(info, NANDFCR_OFFSET, nandcfr);
  142. spin_unlock_irqrestore(&davinci_nand_lock, flags);
  143. }
  144. /*
  145. * Read hardware ECC value and pack into three bytes
  146. */
  147. static int nand_davinci_calculate_1bit(struct mtd_info *mtd,
  148. const u_char *dat, u_char *ecc_code)
  149. {
  150. unsigned int ecc_val = nand_davinci_readecc_1bit(mtd);
  151. unsigned int ecc24 = (ecc_val & 0x0fff) | ((ecc_val & 0x0fff0000) >> 4);
  152. /* invert so that erased block ecc is correct */
  153. ecc24 = ~ecc24;
  154. ecc_code[0] = (u_char)(ecc24);
  155. ecc_code[1] = (u_char)(ecc24 >> 8);
  156. ecc_code[2] = (u_char)(ecc24 >> 16);
  157. return 0;
  158. }
  159. static int nand_davinci_correct_1bit(struct mtd_info *mtd, u_char *dat,
  160. u_char *read_ecc, u_char *calc_ecc)
  161. {
  162. struct nand_chip *chip = mtd->priv;
  163. uint32_t eccNand = read_ecc[0] | (read_ecc[1] << 8) |
  164. (read_ecc[2] << 16);
  165. uint32_t eccCalc = calc_ecc[0] | (calc_ecc[1] << 8) |
  166. (calc_ecc[2] << 16);
  167. uint32_t diff = eccCalc ^ eccNand;
  168. if (diff) {
  169. if ((((diff >> 12) ^ diff) & 0xfff) == 0xfff) {
  170. /* Correctable error */
  171. if ((diff >> (12 + 3)) < chip->ecc.size) {
  172. dat[diff >> (12 + 3)] ^= BIT((diff >> 12) & 7);
  173. return 1;
  174. } else {
  175. return -1;
  176. }
  177. } else if (!(diff & (diff - 1))) {
  178. /* Single bit ECC error in the ECC itself,
  179. * nothing to fix */
  180. return 1;
  181. } else {
  182. /* Uncorrectable error */
  183. return -1;
  184. }
  185. }
  186. return 0;
  187. }
  188. /*----------------------------------------------------------------------*/
  189. /*
  190. * NOTE: NAND boot requires ALE == EM_A[1], CLE == EM_A[2], so that's
  191. * how these chips are normally wired. This translates to both 8 and 16
  192. * bit busses using ALE == BIT(3) in byte addresses, and CLE == BIT(4).
  193. *
  194. * For now we assume that configuration, or any other one which ignores
  195. * the two LSBs for NAND access ... so we can issue 32-bit reads/writes
  196. * and have that transparently morphed into multiple NAND operations.
  197. */
  198. static void nand_davinci_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  199. {
  200. struct nand_chip *chip = mtd->priv;
  201. if ((0x03 & ((unsigned)buf)) == 0 && (0x03 & len) == 0)
  202. ioread32_rep(chip->IO_ADDR_R, buf, len >> 2);
  203. else if ((0x01 & ((unsigned)buf)) == 0 && (0x01 & len) == 0)
  204. ioread16_rep(chip->IO_ADDR_R, buf, len >> 1);
  205. else
  206. ioread8_rep(chip->IO_ADDR_R, buf, len);
  207. }
  208. static void nand_davinci_write_buf(struct mtd_info *mtd,
  209. const uint8_t *buf, int len)
  210. {
  211. struct nand_chip *chip = mtd->priv;
  212. if ((0x03 & ((unsigned)buf)) == 0 && (0x03 & len) == 0)
  213. iowrite32_rep(chip->IO_ADDR_R, buf, len >> 2);
  214. else if ((0x01 & ((unsigned)buf)) == 0 && (0x01 & len) == 0)
  215. iowrite16_rep(chip->IO_ADDR_R, buf, len >> 1);
  216. else
  217. iowrite8_rep(chip->IO_ADDR_R, buf, len);
  218. }
  219. /*
  220. * Check hardware register for wait status. Returns 1 if device is ready,
  221. * 0 if it is still busy.
  222. */
  223. static int nand_davinci_dev_ready(struct mtd_info *mtd)
  224. {
  225. struct davinci_nand_info *info = to_davinci_nand(mtd);
  226. return davinci_nand_readl(info, NANDFSR_OFFSET) & BIT(0);
  227. }
  228. static void __init nand_dm6446evm_flash_init(struct davinci_nand_info *info)
  229. {
  230. uint32_t regval, a1cr;
  231. /*
  232. * NAND FLASH timings @ PLL1 == 459 MHz
  233. * - AEMIF.CLK freq = PLL1/6 = 459/6 = 76.5 MHz
  234. * - AEMIF.CLK period = 1/76.5 MHz = 13.1 ns
  235. */
  236. regval = 0
  237. | (0 << 31) /* selectStrobe */
  238. | (0 << 30) /* extWait (never with NAND) */
  239. | (1 << 26) /* writeSetup 10 ns */
  240. | (3 << 20) /* writeStrobe 40 ns */
  241. | (1 << 17) /* writeHold 10 ns */
  242. | (0 << 13) /* readSetup 10 ns */
  243. | (3 << 7) /* readStrobe 60 ns */
  244. | (0 << 4) /* readHold 10 ns */
  245. | (3 << 2) /* turnAround ?? ns */
  246. | (0 << 0) /* asyncSize 8-bit bus */
  247. ;
  248. a1cr = davinci_nand_readl(info, A1CR_OFFSET);
  249. if (a1cr != regval) {
  250. dev_dbg(info->dev, "Warning: NAND config: Set A1CR " \
  251. "reg to 0x%08x, was 0x%08x, should be done by " \
  252. "bootloader.\n", regval, a1cr);
  253. davinci_nand_writel(info, A1CR_OFFSET, regval);
  254. }
  255. }
  256. /*----------------------------------------------------------------------*/
  257. static int __init nand_davinci_probe(struct platform_device *pdev)
  258. {
  259. struct davinci_nand_pdata *pdata = pdev->dev.platform_data;
  260. struct davinci_nand_info *info;
  261. struct resource *res1;
  262. struct resource *res2;
  263. void __iomem *vaddr;
  264. void __iomem *base;
  265. int ret;
  266. uint32_t val;
  267. nand_ecc_modes_t ecc_mode;
  268. /* which external chipselect will we be managing? */
  269. if (pdev->id < 0 || pdev->id > 3)
  270. return -ENODEV;
  271. info = kzalloc(sizeof(*info), GFP_KERNEL);
  272. if (!info) {
  273. dev_err(&pdev->dev, "unable to allocate memory\n");
  274. ret = -ENOMEM;
  275. goto err_nomem;
  276. }
  277. platform_set_drvdata(pdev, info);
  278. res1 = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  279. res2 = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  280. if (!res1 || !res2) {
  281. dev_err(&pdev->dev, "resource missing\n");
  282. ret = -EINVAL;
  283. goto err_nomem;
  284. }
  285. vaddr = ioremap(res1->start, res1->end - res1->start);
  286. base = ioremap(res2->start, res2->end - res2->start);
  287. if (!vaddr || !base) {
  288. dev_err(&pdev->dev, "ioremap failed\n");
  289. ret = -EINVAL;
  290. goto err_ioremap;
  291. }
  292. info->dev = &pdev->dev;
  293. info->base = base;
  294. info->vaddr = vaddr;
  295. info->mtd.priv = &info->chip;
  296. info->mtd.name = dev_name(&pdev->dev);
  297. info->mtd.owner = THIS_MODULE;
  298. info->chip.IO_ADDR_R = vaddr;
  299. info->chip.IO_ADDR_W = vaddr;
  300. info->chip.chip_delay = 0;
  301. info->chip.select_chip = nand_davinci_select_chip;
  302. /* options such as NAND_USE_FLASH_BBT or 16-bit widths */
  303. info->chip.options = pdata ? pdata->options : 0;
  304. info->ioaddr = (uint32_t __force) vaddr;
  305. info->current_cs = info->ioaddr;
  306. info->core_chipsel = pdev->id;
  307. info->mask_chipsel = pdata->mask_chipsel;
  308. /* use nandboot-capable ALE/CLE masks by default */
  309. if (pdata && pdata->mask_ale)
  310. info->mask_ale = pdata->mask_cle;
  311. else
  312. info->mask_ale = MASK_ALE;
  313. if (pdata && pdata->mask_cle)
  314. info->mask_cle = pdata->mask_cle;
  315. else
  316. info->mask_cle = MASK_CLE;
  317. /* Set address of hardware control function */
  318. info->chip.cmd_ctrl = nand_davinci_hwcontrol;
  319. info->chip.dev_ready = nand_davinci_dev_ready;
  320. /* Speed up buffer I/O */
  321. info->chip.read_buf = nand_davinci_read_buf;
  322. info->chip.write_buf = nand_davinci_write_buf;
  323. /* use board-specific ECC config; else, the best available */
  324. if (pdata)
  325. ecc_mode = pdata->ecc_mode;
  326. else if (cpu_is_davinci_dm355())
  327. ecc_mode = NAND_ECC_HW_SYNDROME;
  328. else
  329. ecc_mode = NAND_ECC_HW;
  330. switch (ecc_mode) {
  331. case NAND_ECC_NONE:
  332. case NAND_ECC_SOFT:
  333. break;
  334. case NAND_ECC_HW:
  335. info->chip.ecc.calculate = nand_davinci_calculate_1bit;
  336. info->chip.ecc.correct = nand_davinci_correct_1bit;
  337. info->chip.ecc.hwctl = nand_davinci_hwctl_1bit;
  338. info->chip.ecc.size = 512;
  339. info->chip.ecc.bytes = 3;
  340. break;
  341. case NAND_ECC_HW_SYNDROME:
  342. /* FIXME implement */
  343. info->chip.ecc.size = 512;
  344. info->chip.ecc.bytes = 10;
  345. dev_warn(&pdev->dev, "4-bit ECC nyet supported\n");
  346. /* FALL THROUGH */
  347. default:
  348. ret = -EINVAL;
  349. goto err_ecc;
  350. }
  351. info->chip.ecc.mode = ecc_mode;
  352. info->clk = clk_get(&pdev->dev, "AEMIFCLK");
  353. if (IS_ERR(info->clk)) {
  354. ret = PTR_ERR(info->clk);
  355. dev_dbg(&pdev->dev, "unable to get AEMIFCLK, err %d\n", ret);
  356. goto err_clk;
  357. }
  358. ret = clk_enable(info->clk);
  359. if (ret < 0) {
  360. dev_dbg(&pdev->dev, "unable to enable AEMIFCLK, err %d\n", ret);
  361. goto err_clk_enable;
  362. }
  363. /* EMIF timings should normally be set by the boot loader,
  364. * especially after boot-from-NAND. The *only* reason to
  365. * have this special casing for the DM6446 EVM is to work
  366. * with boot-from-NOR ... with CS0 manually re-jumpered
  367. * (after startup) so it addresses the NAND flash, not NOR.
  368. * Even for dev boards, that's unusually rude...
  369. */
  370. if (machine_is_davinci_evm())
  371. nand_dm6446evm_flash_init(info);
  372. spin_lock_irq(&davinci_nand_lock);
  373. /* put CSxNAND into NAND mode */
  374. val = davinci_nand_readl(info, NANDFCR_OFFSET);
  375. val |= BIT(info->core_chipsel);
  376. davinci_nand_writel(info, NANDFCR_OFFSET, val);
  377. spin_unlock_irq(&davinci_nand_lock);
  378. /* Scan to find existence of the device(s) */
  379. ret = nand_scan(&info->mtd, pdata->mask_chipsel ? 2 : 1);
  380. if (ret < 0) {
  381. dev_dbg(&pdev->dev, "no NAND chip(s) found\n");
  382. goto err_scan;
  383. }
  384. if (mtd_has_partitions()) {
  385. struct mtd_partition *mtd_parts = NULL;
  386. int mtd_parts_nb = 0;
  387. if (mtd_has_cmdlinepart()) {
  388. static const char *probes[] __initconst =
  389. { "cmdlinepart", NULL };
  390. const char *master_name;
  391. /* Set info->mtd.name = 0 temporarily */
  392. master_name = info->mtd.name;
  393. info->mtd.name = (char *)0;
  394. /* info->mtd.name == 0, means: don't bother checking
  395. <mtd-id> */
  396. mtd_parts_nb = parse_mtd_partitions(&info->mtd, probes,
  397. &mtd_parts, 0);
  398. /* Restore info->mtd.name */
  399. info->mtd.name = master_name;
  400. }
  401. if (mtd_parts_nb <= 0 && pdata) {
  402. mtd_parts = pdata->parts;
  403. mtd_parts_nb = pdata->nr_parts;
  404. }
  405. /* Register any partitions */
  406. if (mtd_parts_nb > 0) {
  407. ret = add_mtd_partitions(&info->mtd,
  408. mtd_parts, mtd_parts_nb);
  409. if (ret == 0)
  410. info->partitioned = true;
  411. }
  412. } else if (pdata && pdata->nr_parts) {
  413. dev_warn(&pdev->dev, "ignoring %d default partitions on %s\n",
  414. pdata->nr_parts, info->mtd.name);
  415. }
  416. /* If there's no partition info, just package the whole chip
  417. * as a single MTD device.
  418. */
  419. if (!info->partitioned)
  420. ret = add_mtd_device(&info->mtd) ? -ENODEV : 0;
  421. if (ret < 0)
  422. goto err_scan;
  423. val = davinci_nand_readl(info, NRCSR_OFFSET);
  424. dev_info(&pdev->dev, "controller rev. %d.%d\n",
  425. (val >> 8) & 0xff, val & 0xff);
  426. return 0;
  427. err_scan:
  428. clk_disable(info->clk);
  429. err_clk_enable:
  430. clk_put(info->clk);
  431. err_ecc:
  432. err_clk:
  433. err_ioremap:
  434. if (base)
  435. iounmap(base);
  436. if (vaddr)
  437. iounmap(vaddr);
  438. err_nomem:
  439. kfree(info);
  440. return ret;
  441. }
  442. static int __exit nand_davinci_remove(struct platform_device *pdev)
  443. {
  444. struct davinci_nand_info *info = platform_get_drvdata(pdev);
  445. int status;
  446. if (mtd_has_partitions() && info->partitioned)
  447. status = del_mtd_partitions(&info->mtd);
  448. else
  449. status = del_mtd_device(&info->mtd);
  450. iounmap(info->base);
  451. iounmap(info->vaddr);
  452. nand_release(&info->mtd);
  453. clk_disable(info->clk);
  454. clk_put(info->clk);
  455. kfree(info);
  456. return 0;
  457. }
  458. static struct platform_driver nand_davinci_driver = {
  459. .remove = __exit_p(nand_davinci_remove),
  460. .driver = {
  461. .name = "davinci_nand",
  462. },
  463. };
  464. MODULE_ALIAS("platform:davinci_nand");
  465. static int __init nand_davinci_init(void)
  466. {
  467. return platform_driver_probe(&nand_davinci_driver, nand_davinci_probe);
  468. }
  469. module_init(nand_davinci_init);
  470. static void __exit nand_davinci_exit(void)
  471. {
  472. platform_driver_unregister(&nand_davinci_driver);
  473. }
  474. module_exit(nand_davinci_exit);
  475. MODULE_LICENSE("GPL");
  476. MODULE_AUTHOR("Texas Instruments");
  477. MODULE_DESCRIPTION("Davinci NAND flash driver");