davinci_nand.c 15 KB

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