txx9ndfmc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /*
  2. * TXx9 NAND flash memory controller driver
  3. * Based on RBTX49xx patch from CELF patch archive.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * (C) Copyright TOSHIBA CORPORATION 2004-2007
  10. * All Rights Reserved.
  11. */
  12. #include <linux/init.h>
  13. #include <linux/slab.h>
  14. #include <linux/module.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/delay.h>
  17. #include <linux/mtd/mtd.h>
  18. #include <linux/mtd/nand.h>
  19. #include <linux/mtd/nand_ecc.h>
  20. #include <linux/mtd/partitions.h>
  21. #include <linux/io.h>
  22. #include <asm/txx9/ndfmc.h>
  23. /* TXX9 NDFMC Registers */
  24. #define TXX9_NDFDTR 0x00
  25. #define TXX9_NDFMCR 0x04
  26. #define TXX9_NDFSR 0x08
  27. #define TXX9_NDFISR 0x0c
  28. #define TXX9_NDFIMR 0x10
  29. #define TXX9_NDFSPR 0x14
  30. #define TXX9_NDFRSTR 0x18 /* not TX4939 */
  31. /* NDFMCR : NDFMC Mode Control */
  32. #define TXX9_NDFMCR_WE 0x80
  33. #define TXX9_NDFMCR_ECC_ALL 0x60
  34. #define TXX9_NDFMCR_ECC_RESET 0x60
  35. #define TXX9_NDFMCR_ECC_READ 0x40
  36. #define TXX9_NDFMCR_ECC_ON 0x20
  37. #define TXX9_NDFMCR_ECC_OFF 0x00
  38. #define TXX9_NDFMCR_CE 0x10
  39. #define TXX9_NDFMCR_BSPRT 0x04 /* TX4925/TX4926 only */
  40. #define TXX9_NDFMCR_ALE 0x02
  41. #define TXX9_NDFMCR_CLE 0x01
  42. /* TX4939 only */
  43. #define TXX9_NDFMCR_X16 0x0400
  44. #define TXX9_NDFMCR_DMAREQ_MASK 0x0300
  45. #define TXX9_NDFMCR_DMAREQ_NODMA 0x0000
  46. #define TXX9_NDFMCR_DMAREQ_128 0x0100
  47. #define TXX9_NDFMCR_DMAREQ_256 0x0200
  48. #define TXX9_NDFMCR_DMAREQ_512 0x0300
  49. #define TXX9_NDFMCR_CS_MASK 0x0c
  50. #define TXX9_NDFMCR_CS(ch) ((ch) << 2)
  51. /* NDFMCR : NDFMC Status */
  52. #define TXX9_NDFSR_BUSY 0x80
  53. /* TX4939 only */
  54. #define TXX9_NDFSR_DMARUN 0x40
  55. /* NDFMCR : NDFMC Reset */
  56. #define TXX9_NDFRSTR_RST 0x01
  57. struct txx9ndfmc_priv {
  58. struct platform_device *dev;
  59. struct nand_chip chip;
  60. struct mtd_info mtd;
  61. int cs;
  62. const char *mtdname;
  63. };
  64. #define MAX_TXX9NDFMC_DEV 4
  65. struct txx9ndfmc_drvdata {
  66. struct mtd_info *mtds[MAX_TXX9NDFMC_DEV];
  67. void __iomem *base;
  68. unsigned char hold; /* in gbusclock */
  69. unsigned char spw; /* in gbusclock */
  70. struct nand_hw_control hw_control;
  71. #ifdef CONFIG_MTD_PARTITIONS
  72. struct mtd_partition *parts[MAX_TXX9NDFMC_DEV];
  73. #endif
  74. };
  75. static struct platform_device *mtd_to_platdev(struct mtd_info *mtd)
  76. {
  77. struct nand_chip *chip = mtd->priv;
  78. struct txx9ndfmc_priv *txx9_priv = chip->priv;
  79. return txx9_priv->dev;
  80. }
  81. static void __iomem *ndregaddr(struct platform_device *dev, unsigned int reg)
  82. {
  83. struct txx9ndfmc_drvdata *drvdata = platform_get_drvdata(dev);
  84. struct txx9ndfmc_platform_data *plat = dev->dev.platform_data;
  85. return drvdata->base + (reg << plat->shift);
  86. }
  87. static u32 txx9ndfmc_read(struct platform_device *dev, unsigned int reg)
  88. {
  89. return __raw_readl(ndregaddr(dev, reg));
  90. }
  91. static void txx9ndfmc_write(struct platform_device *dev,
  92. u32 val, unsigned int reg)
  93. {
  94. __raw_writel(val, ndregaddr(dev, reg));
  95. }
  96. static uint8_t txx9ndfmc_read_byte(struct mtd_info *mtd)
  97. {
  98. struct platform_device *dev = mtd_to_platdev(mtd);
  99. return txx9ndfmc_read(dev, TXX9_NDFDTR);
  100. }
  101. static void txx9ndfmc_write_buf(struct mtd_info *mtd, const uint8_t *buf,
  102. int len)
  103. {
  104. struct platform_device *dev = mtd_to_platdev(mtd);
  105. void __iomem *ndfdtr = ndregaddr(dev, TXX9_NDFDTR);
  106. u32 mcr = txx9ndfmc_read(dev, TXX9_NDFMCR);
  107. txx9ndfmc_write(dev, mcr | TXX9_NDFMCR_WE, TXX9_NDFMCR);
  108. while (len--)
  109. __raw_writel(*buf++, ndfdtr);
  110. txx9ndfmc_write(dev, mcr, TXX9_NDFMCR);
  111. }
  112. static void txx9ndfmc_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  113. {
  114. struct platform_device *dev = mtd_to_platdev(mtd);
  115. void __iomem *ndfdtr = ndregaddr(dev, TXX9_NDFDTR);
  116. while (len--)
  117. *buf++ = __raw_readl(ndfdtr);
  118. }
  119. static int txx9ndfmc_verify_buf(struct mtd_info *mtd, const uint8_t *buf,
  120. int len)
  121. {
  122. struct platform_device *dev = mtd_to_platdev(mtd);
  123. void __iomem *ndfdtr = ndregaddr(dev, TXX9_NDFDTR);
  124. while (len--)
  125. if (*buf++ != (uint8_t)__raw_readl(ndfdtr))
  126. return -EFAULT;
  127. return 0;
  128. }
  129. static void txx9ndfmc_cmd_ctrl(struct mtd_info *mtd, int cmd,
  130. unsigned int ctrl)
  131. {
  132. struct nand_chip *chip = mtd->priv;
  133. struct txx9ndfmc_priv *txx9_priv = chip->priv;
  134. struct platform_device *dev = txx9_priv->dev;
  135. struct txx9ndfmc_platform_data *plat = dev->dev.platform_data;
  136. if (ctrl & NAND_CTRL_CHANGE) {
  137. u32 mcr = txx9ndfmc_read(dev, TXX9_NDFMCR);
  138. mcr &= ~(TXX9_NDFMCR_CLE | TXX9_NDFMCR_ALE | TXX9_NDFMCR_CE);
  139. mcr |= ctrl & NAND_CLE ? TXX9_NDFMCR_CLE : 0;
  140. mcr |= ctrl & NAND_ALE ? TXX9_NDFMCR_ALE : 0;
  141. /* TXX9_NDFMCR_CE bit is 0:high 1:low */
  142. mcr |= ctrl & NAND_NCE ? TXX9_NDFMCR_CE : 0;
  143. if (txx9_priv->cs >= 0 && (ctrl & NAND_NCE)) {
  144. mcr &= ~TXX9_NDFMCR_CS_MASK;
  145. mcr |= TXX9_NDFMCR_CS(txx9_priv->cs);
  146. }
  147. txx9ndfmc_write(dev, mcr, TXX9_NDFMCR);
  148. }
  149. if (cmd != NAND_CMD_NONE)
  150. txx9ndfmc_write(dev, cmd & 0xff, TXX9_NDFDTR);
  151. if (plat->flags & NDFMC_PLAT_FLAG_DUMMYWRITE) {
  152. /* dummy write to update external latch */
  153. if ((ctrl & NAND_CTRL_CHANGE) && cmd == NAND_CMD_NONE)
  154. txx9ndfmc_write(dev, 0, TXX9_NDFDTR);
  155. }
  156. mmiowb();
  157. }
  158. static int txx9ndfmc_dev_ready(struct mtd_info *mtd)
  159. {
  160. struct platform_device *dev = mtd_to_platdev(mtd);
  161. return !(txx9ndfmc_read(dev, TXX9_NDFSR) & TXX9_NDFSR_BUSY);
  162. }
  163. static int txx9ndfmc_calculate_ecc(struct mtd_info *mtd, const uint8_t *dat,
  164. uint8_t *ecc_code)
  165. {
  166. struct platform_device *dev = mtd_to_platdev(mtd);
  167. struct nand_chip *chip = mtd->priv;
  168. int eccbytes;
  169. u32 mcr = txx9ndfmc_read(dev, TXX9_NDFMCR);
  170. mcr &= ~TXX9_NDFMCR_ECC_ALL;
  171. txx9ndfmc_write(dev, mcr | TXX9_NDFMCR_ECC_OFF, TXX9_NDFMCR);
  172. txx9ndfmc_write(dev, mcr | TXX9_NDFMCR_ECC_READ, TXX9_NDFMCR);
  173. for (eccbytes = chip->ecc.bytes; eccbytes > 0; eccbytes -= 3) {
  174. ecc_code[1] = txx9ndfmc_read(dev, TXX9_NDFDTR);
  175. ecc_code[0] = txx9ndfmc_read(dev, TXX9_NDFDTR);
  176. ecc_code[2] = txx9ndfmc_read(dev, TXX9_NDFDTR);
  177. ecc_code += 3;
  178. }
  179. txx9ndfmc_write(dev, mcr | TXX9_NDFMCR_ECC_OFF, TXX9_NDFMCR);
  180. return 0;
  181. }
  182. static int txx9ndfmc_correct_data(struct mtd_info *mtd, unsigned char *buf,
  183. unsigned char *read_ecc, unsigned char *calc_ecc)
  184. {
  185. struct nand_chip *chip = mtd->priv;
  186. int eccsize;
  187. int corrected = 0;
  188. int stat;
  189. for (eccsize = chip->ecc.size; eccsize > 0; eccsize -= 256) {
  190. stat = __nand_correct_data(buf, read_ecc, calc_ecc, 256);
  191. if (stat < 0)
  192. return stat;
  193. corrected += stat;
  194. buf += 256;
  195. read_ecc += 3;
  196. calc_ecc += 3;
  197. }
  198. return corrected;
  199. }
  200. static void txx9ndfmc_enable_hwecc(struct mtd_info *mtd, int mode)
  201. {
  202. struct platform_device *dev = mtd_to_platdev(mtd);
  203. u32 mcr = txx9ndfmc_read(dev, TXX9_NDFMCR);
  204. mcr &= ~TXX9_NDFMCR_ECC_ALL;
  205. txx9ndfmc_write(dev, mcr | TXX9_NDFMCR_ECC_RESET, TXX9_NDFMCR);
  206. txx9ndfmc_write(dev, mcr | TXX9_NDFMCR_ECC_OFF, TXX9_NDFMCR);
  207. txx9ndfmc_write(dev, mcr | TXX9_NDFMCR_ECC_ON, TXX9_NDFMCR);
  208. }
  209. static void txx9ndfmc_initialize(struct platform_device *dev)
  210. {
  211. struct txx9ndfmc_platform_data *plat = dev->dev.platform_data;
  212. struct txx9ndfmc_drvdata *drvdata = platform_get_drvdata(dev);
  213. int tmout = 100;
  214. if (plat->flags & NDFMC_PLAT_FLAG_NO_RSTR)
  215. ; /* no NDFRSTR. Write to NDFSPR resets the NDFMC. */
  216. else {
  217. /* reset NDFMC */
  218. txx9ndfmc_write(dev,
  219. txx9ndfmc_read(dev, TXX9_NDFRSTR) |
  220. TXX9_NDFRSTR_RST,
  221. TXX9_NDFRSTR);
  222. while (txx9ndfmc_read(dev, TXX9_NDFRSTR) & TXX9_NDFRSTR_RST) {
  223. if (--tmout == 0) {
  224. dev_err(&dev->dev, "reset failed.\n");
  225. break;
  226. }
  227. udelay(1);
  228. }
  229. }
  230. /* setup Hold Time, Strobe Pulse Width */
  231. txx9ndfmc_write(dev, (drvdata->hold << 4) | drvdata->spw, TXX9_NDFSPR);
  232. txx9ndfmc_write(dev,
  233. (plat->flags & NDFMC_PLAT_FLAG_USE_BSPRT) ?
  234. TXX9_NDFMCR_BSPRT : 0, TXX9_NDFMCR);
  235. }
  236. #define TXX9NDFMC_NS_TO_CYC(gbusclk, ns) \
  237. DIV_ROUND_UP((ns) * DIV_ROUND_UP(gbusclk, 1000), 1000000)
  238. static int txx9ndfmc_nand_scan(struct mtd_info *mtd)
  239. {
  240. struct nand_chip *chip = mtd->priv;
  241. int ret;
  242. ret = nand_scan_ident(mtd, 1, NULL);
  243. if (!ret) {
  244. if (mtd->writesize >= 512) {
  245. /* Hardware ECC 6 byte ECC per 512 Byte data */
  246. chip->ecc.size = 512;
  247. chip->ecc.bytes = 6;
  248. }
  249. ret = nand_scan_tail(mtd);
  250. }
  251. return ret;
  252. }
  253. static int __init txx9ndfmc_probe(struct platform_device *dev)
  254. {
  255. struct txx9ndfmc_platform_data *plat = dev->dev.platform_data;
  256. #ifdef CONFIG_MTD_PARTITIONS
  257. static const char *probes[] = { "cmdlinepart", NULL };
  258. #endif
  259. int hold, spw;
  260. int i;
  261. struct txx9ndfmc_drvdata *drvdata;
  262. unsigned long gbusclk = plat->gbus_clock;
  263. struct resource *res;
  264. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  265. if (!res)
  266. return -ENODEV;
  267. drvdata = devm_kzalloc(&dev->dev, sizeof(*drvdata), GFP_KERNEL);
  268. if (!drvdata)
  269. return -ENOMEM;
  270. if (!devm_request_mem_region(&dev->dev, res->start,
  271. resource_size(res), dev_name(&dev->dev)))
  272. return -EBUSY;
  273. drvdata->base = devm_ioremap(&dev->dev, res->start,
  274. resource_size(res));
  275. if (!drvdata->base)
  276. return -EBUSY;
  277. hold = plat->hold ?: 20; /* tDH */
  278. spw = plat->spw ?: 90; /* max(tREADID, tWP, tRP) */
  279. hold = TXX9NDFMC_NS_TO_CYC(gbusclk, hold);
  280. spw = TXX9NDFMC_NS_TO_CYC(gbusclk, spw);
  281. if (plat->flags & NDFMC_PLAT_FLAG_HOLDADD)
  282. hold -= 2; /* actual hold time : (HOLD + 2) BUSCLK */
  283. spw -= 1; /* actual wait time : (SPW + 1) BUSCLK */
  284. hold = clamp(hold, 1, 15);
  285. drvdata->hold = hold;
  286. spw = clamp(spw, 1, 15);
  287. drvdata->spw = spw;
  288. dev_info(&dev->dev, "CLK:%ldMHz HOLD:%d SPW:%d\n",
  289. (gbusclk + 500000) / 1000000, hold, spw);
  290. spin_lock_init(&drvdata->hw_control.lock);
  291. init_waitqueue_head(&drvdata->hw_control.wq);
  292. platform_set_drvdata(dev, drvdata);
  293. txx9ndfmc_initialize(dev);
  294. for (i = 0; i < MAX_TXX9NDFMC_DEV; i++) {
  295. struct txx9ndfmc_priv *txx9_priv;
  296. struct nand_chip *chip;
  297. struct mtd_info *mtd;
  298. #ifdef CONFIG_MTD_PARTITIONS
  299. int nr_parts;
  300. #endif
  301. if (!(plat->ch_mask & (1 << i)))
  302. continue;
  303. txx9_priv = kzalloc(sizeof(struct txx9ndfmc_priv),
  304. GFP_KERNEL);
  305. if (!txx9_priv) {
  306. dev_err(&dev->dev, "Unable to allocate "
  307. "TXx9 NDFMC MTD device structure.\n");
  308. continue;
  309. }
  310. chip = &txx9_priv->chip;
  311. mtd = &txx9_priv->mtd;
  312. mtd->owner = THIS_MODULE;
  313. mtd->priv = chip;
  314. chip->read_byte = txx9ndfmc_read_byte;
  315. chip->read_buf = txx9ndfmc_read_buf;
  316. chip->write_buf = txx9ndfmc_write_buf;
  317. chip->verify_buf = txx9ndfmc_verify_buf;
  318. chip->cmd_ctrl = txx9ndfmc_cmd_ctrl;
  319. chip->dev_ready = txx9ndfmc_dev_ready;
  320. chip->ecc.calculate = txx9ndfmc_calculate_ecc;
  321. chip->ecc.correct = txx9ndfmc_correct_data;
  322. chip->ecc.hwctl = txx9ndfmc_enable_hwecc;
  323. chip->ecc.mode = NAND_ECC_HW;
  324. /* txx9ndfmc_nand_scan will overwrite ecc.size and ecc.bytes */
  325. chip->ecc.size = 256;
  326. chip->ecc.bytes = 3;
  327. chip->chip_delay = 100;
  328. chip->controller = &drvdata->hw_control;
  329. chip->priv = txx9_priv;
  330. txx9_priv->dev = dev;
  331. if (plat->ch_mask != 1) {
  332. txx9_priv->cs = i;
  333. txx9_priv->mtdname = kasprintf(GFP_KERNEL, "%s.%u",
  334. dev_name(&dev->dev), i);
  335. } else {
  336. txx9_priv->cs = -1;
  337. txx9_priv->mtdname = kstrdup(dev_name(&dev->dev),
  338. GFP_KERNEL);
  339. }
  340. if (!txx9_priv->mtdname) {
  341. kfree(txx9_priv);
  342. dev_err(&dev->dev, "Unable to allocate MTD name.\n");
  343. continue;
  344. }
  345. if (plat->wide_mask & (1 << i))
  346. chip->options |= NAND_BUSWIDTH_16;
  347. if (txx9ndfmc_nand_scan(mtd)) {
  348. kfree(txx9_priv->mtdname);
  349. kfree(txx9_priv);
  350. continue;
  351. }
  352. mtd->name = txx9_priv->mtdname;
  353. #ifdef CONFIG_MTD_PARTITIONS
  354. nr_parts = parse_mtd_partitions(mtd, probes,
  355. &drvdata->parts[i], 0);
  356. if (nr_parts > 0)
  357. add_mtd_partitions(mtd, drvdata->parts[i], nr_parts);
  358. #endif
  359. add_mtd_device(mtd);
  360. drvdata->mtds[i] = mtd;
  361. }
  362. return 0;
  363. }
  364. static int __exit txx9ndfmc_remove(struct platform_device *dev)
  365. {
  366. struct txx9ndfmc_drvdata *drvdata = platform_get_drvdata(dev);
  367. int i;
  368. platform_set_drvdata(dev, NULL);
  369. if (!drvdata)
  370. return 0;
  371. for (i = 0; i < MAX_TXX9NDFMC_DEV; i++) {
  372. struct mtd_info *mtd = drvdata->mtds[i];
  373. struct nand_chip *chip;
  374. struct txx9ndfmc_priv *txx9_priv;
  375. if (!mtd)
  376. continue;
  377. chip = mtd->priv;
  378. txx9_priv = chip->priv;
  379. nand_release(mtd);
  380. #ifdef CONFIG_MTD_PARTITIONS
  381. kfree(drvdata->parts[i]);
  382. #endif
  383. kfree(txx9_priv->mtdname);
  384. kfree(txx9_priv);
  385. }
  386. return 0;
  387. }
  388. #ifdef CONFIG_PM
  389. static int txx9ndfmc_resume(struct platform_device *dev)
  390. {
  391. if (platform_get_drvdata(dev))
  392. txx9ndfmc_initialize(dev);
  393. return 0;
  394. }
  395. #else
  396. #define txx9ndfmc_resume NULL
  397. #endif
  398. static struct platform_driver txx9ndfmc_driver = {
  399. .remove = __exit_p(txx9ndfmc_remove),
  400. .resume = txx9ndfmc_resume,
  401. .driver = {
  402. .name = "txx9ndfmc",
  403. .owner = THIS_MODULE,
  404. },
  405. };
  406. static int __init txx9ndfmc_init(void)
  407. {
  408. return platform_driver_probe(&txx9ndfmc_driver, txx9ndfmc_probe);
  409. }
  410. static void __exit txx9ndfmc_exit(void)
  411. {
  412. platform_driver_unregister(&txx9ndfmc_driver);
  413. }
  414. module_init(txx9ndfmc_init);
  415. module_exit(txx9ndfmc_exit);
  416. MODULE_LICENSE("GPL");
  417. MODULE_DESCRIPTION("TXx9 SoC NAND flash controller driver");
  418. MODULE_ALIAS("platform:txx9ndfmc");