txx9ndfmc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. struct mtd_partition *parts[MAX_TXX9NDFMC_DEV];
  72. };
  73. static struct platform_device *mtd_to_platdev(struct mtd_info *mtd)
  74. {
  75. struct nand_chip *chip = mtd->priv;
  76. struct txx9ndfmc_priv *txx9_priv = chip->priv;
  77. return txx9_priv->dev;
  78. }
  79. static void __iomem *ndregaddr(struct platform_device *dev, unsigned int reg)
  80. {
  81. struct txx9ndfmc_drvdata *drvdata = platform_get_drvdata(dev);
  82. struct txx9ndfmc_platform_data *plat = dev->dev.platform_data;
  83. return drvdata->base + (reg << plat->shift);
  84. }
  85. static u32 txx9ndfmc_read(struct platform_device *dev, unsigned int reg)
  86. {
  87. return __raw_readl(ndregaddr(dev, reg));
  88. }
  89. static void txx9ndfmc_write(struct platform_device *dev,
  90. u32 val, unsigned int reg)
  91. {
  92. __raw_writel(val, ndregaddr(dev, reg));
  93. }
  94. static uint8_t txx9ndfmc_read_byte(struct mtd_info *mtd)
  95. {
  96. struct platform_device *dev = mtd_to_platdev(mtd);
  97. return txx9ndfmc_read(dev, TXX9_NDFDTR);
  98. }
  99. static void txx9ndfmc_write_buf(struct mtd_info *mtd, const uint8_t *buf,
  100. int len)
  101. {
  102. struct platform_device *dev = mtd_to_platdev(mtd);
  103. void __iomem *ndfdtr = ndregaddr(dev, TXX9_NDFDTR);
  104. u32 mcr = txx9ndfmc_read(dev, TXX9_NDFMCR);
  105. txx9ndfmc_write(dev, mcr | TXX9_NDFMCR_WE, TXX9_NDFMCR);
  106. while (len--)
  107. __raw_writel(*buf++, ndfdtr);
  108. txx9ndfmc_write(dev, mcr, TXX9_NDFMCR);
  109. }
  110. static void txx9ndfmc_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  111. {
  112. struct platform_device *dev = mtd_to_platdev(mtd);
  113. void __iomem *ndfdtr = ndregaddr(dev, TXX9_NDFDTR);
  114. while (len--)
  115. *buf++ = __raw_readl(ndfdtr);
  116. }
  117. static int txx9ndfmc_verify_buf(struct mtd_info *mtd, const uint8_t *buf,
  118. int len)
  119. {
  120. struct platform_device *dev = mtd_to_platdev(mtd);
  121. void __iomem *ndfdtr = ndregaddr(dev, TXX9_NDFDTR);
  122. while (len--)
  123. if (*buf++ != (uint8_t)__raw_readl(ndfdtr))
  124. return -EFAULT;
  125. return 0;
  126. }
  127. static void txx9ndfmc_cmd_ctrl(struct mtd_info *mtd, int cmd,
  128. unsigned int ctrl)
  129. {
  130. struct nand_chip *chip = mtd->priv;
  131. struct txx9ndfmc_priv *txx9_priv = chip->priv;
  132. struct platform_device *dev = txx9_priv->dev;
  133. struct txx9ndfmc_platform_data *plat = dev->dev.platform_data;
  134. if (ctrl & NAND_CTRL_CHANGE) {
  135. u32 mcr = txx9ndfmc_read(dev, TXX9_NDFMCR);
  136. mcr &= ~(TXX9_NDFMCR_CLE | TXX9_NDFMCR_ALE | TXX9_NDFMCR_CE);
  137. mcr |= ctrl & NAND_CLE ? TXX9_NDFMCR_CLE : 0;
  138. mcr |= ctrl & NAND_ALE ? TXX9_NDFMCR_ALE : 0;
  139. /* TXX9_NDFMCR_CE bit is 0:high 1:low */
  140. mcr |= ctrl & NAND_NCE ? TXX9_NDFMCR_CE : 0;
  141. if (txx9_priv->cs >= 0 && (ctrl & NAND_NCE)) {
  142. mcr &= ~TXX9_NDFMCR_CS_MASK;
  143. mcr |= TXX9_NDFMCR_CS(txx9_priv->cs);
  144. }
  145. txx9ndfmc_write(dev, mcr, TXX9_NDFMCR);
  146. }
  147. if (cmd != NAND_CMD_NONE)
  148. txx9ndfmc_write(dev, cmd & 0xff, TXX9_NDFDTR);
  149. if (plat->flags & NDFMC_PLAT_FLAG_DUMMYWRITE) {
  150. /* dummy write to update external latch */
  151. if ((ctrl & NAND_CTRL_CHANGE) && cmd == NAND_CMD_NONE)
  152. txx9ndfmc_write(dev, 0, TXX9_NDFDTR);
  153. }
  154. mmiowb();
  155. }
  156. static int txx9ndfmc_dev_ready(struct mtd_info *mtd)
  157. {
  158. struct platform_device *dev = mtd_to_platdev(mtd);
  159. return !(txx9ndfmc_read(dev, TXX9_NDFSR) & TXX9_NDFSR_BUSY);
  160. }
  161. static int txx9ndfmc_calculate_ecc(struct mtd_info *mtd, const uint8_t *dat,
  162. uint8_t *ecc_code)
  163. {
  164. struct platform_device *dev = mtd_to_platdev(mtd);
  165. struct nand_chip *chip = mtd->priv;
  166. int eccbytes;
  167. u32 mcr = txx9ndfmc_read(dev, TXX9_NDFMCR);
  168. mcr &= ~TXX9_NDFMCR_ECC_ALL;
  169. txx9ndfmc_write(dev, mcr | TXX9_NDFMCR_ECC_OFF, TXX9_NDFMCR);
  170. txx9ndfmc_write(dev, mcr | TXX9_NDFMCR_ECC_READ, TXX9_NDFMCR);
  171. for (eccbytes = chip->ecc.bytes; eccbytes > 0; eccbytes -= 3) {
  172. ecc_code[1] = txx9ndfmc_read(dev, TXX9_NDFDTR);
  173. ecc_code[0] = txx9ndfmc_read(dev, TXX9_NDFDTR);
  174. ecc_code[2] = txx9ndfmc_read(dev, TXX9_NDFDTR);
  175. ecc_code += 3;
  176. }
  177. txx9ndfmc_write(dev, mcr | TXX9_NDFMCR_ECC_OFF, TXX9_NDFMCR);
  178. return 0;
  179. }
  180. static int txx9ndfmc_correct_data(struct mtd_info *mtd, unsigned char *buf,
  181. unsigned char *read_ecc, unsigned char *calc_ecc)
  182. {
  183. struct nand_chip *chip = mtd->priv;
  184. int eccsize;
  185. int corrected = 0;
  186. int stat;
  187. for (eccsize = chip->ecc.size; eccsize > 0; eccsize -= 256) {
  188. stat = __nand_correct_data(buf, read_ecc, calc_ecc, 256);
  189. if (stat < 0)
  190. return stat;
  191. corrected += stat;
  192. buf += 256;
  193. read_ecc += 3;
  194. calc_ecc += 3;
  195. }
  196. return corrected;
  197. }
  198. static void txx9ndfmc_enable_hwecc(struct mtd_info *mtd, int mode)
  199. {
  200. struct platform_device *dev = mtd_to_platdev(mtd);
  201. u32 mcr = txx9ndfmc_read(dev, TXX9_NDFMCR);
  202. mcr &= ~TXX9_NDFMCR_ECC_ALL;
  203. txx9ndfmc_write(dev, mcr | TXX9_NDFMCR_ECC_RESET, TXX9_NDFMCR);
  204. txx9ndfmc_write(dev, mcr | TXX9_NDFMCR_ECC_OFF, TXX9_NDFMCR);
  205. txx9ndfmc_write(dev, mcr | TXX9_NDFMCR_ECC_ON, TXX9_NDFMCR);
  206. }
  207. static void txx9ndfmc_initialize(struct platform_device *dev)
  208. {
  209. struct txx9ndfmc_platform_data *plat = dev->dev.platform_data;
  210. struct txx9ndfmc_drvdata *drvdata = platform_get_drvdata(dev);
  211. int tmout = 100;
  212. if (plat->flags & NDFMC_PLAT_FLAG_NO_RSTR)
  213. ; /* no NDFRSTR. Write to NDFSPR resets the NDFMC. */
  214. else {
  215. /* reset NDFMC */
  216. txx9ndfmc_write(dev,
  217. txx9ndfmc_read(dev, TXX9_NDFRSTR) |
  218. TXX9_NDFRSTR_RST,
  219. TXX9_NDFRSTR);
  220. while (txx9ndfmc_read(dev, TXX9_NDFRSTR) & TXX9_NDFRSTR_RST) {
  221. if (--tmout == 0) {
  222. dev_err(&dev->dev, "reset failed.\n");
  223. break;
  224. }
  225. udelay(1);
  226. }
  227. }
  228. /* setup Hold Time, Strobe Pulse Width */
  229. txx9ndfmc_write(dev, (drvdata->hold << 4) | drvdata->spw, TXX9_NDFSPR);
  230. txx9ndfmc_write(dev,
  231. (plat->flags & NDFMC_PLAT_FLAG_USE_BSPRT) ?
  232. TXX9_NDFMCR_BSPRT : 0, TXX9_NDFMCR);
  233. }
  234. #define TXX9NDFMC_NS_TO_CYC(gbusclk, ns) \
  235. DIV_ROUND_UP((ns) * DIV_ROUND_UP(gbusclk, 1000), 1000000)
  236. static int txx9ndfmc_nand_scan(struct mtd_info *mtd)
  237. {
  238. struct nand_chip *chip = mtd->priv;
  239. int ret;
  240. ret = nand_scan_ident(mtd, 1, NULL);
  241. if (!ret) {
  242. if (mtd->writesize >= 512) {
  243. /* Hardware ECC 6 byte ECC per 512 Byte data */
  244. chip->ecc.size = 512;
  245. chip->ecc.bytes = 6;
  246. }
  247. ret = nand_scan_tail(mtd);
  248. }
  249. return ret;
  250. }
  251. static int __init txx9ndfmc_probe(struct platform_device *dev)
  252. {
  253. struct txx9ndfmc_platform_data *plat = dev->dev.platform_data;
  254. static const char *probes[] = { "cmdlinepart", NULL };
  255. int hold, spw;
  256. int i;
  257. struct txx9ndfmc_drvdata *drvdata;
  258. unsigned long gbusclk = plat->gbus_clock;
  259. struct resource *res;
  260. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  261. if (!res)
  262. return -ENODEV;
  263. drvdata = devm_kzalloc(&dev->dev, sizeof(*drvdata), GFP_KERNEL);
  264. if (!drvdata)
  265. return -ENOMEM;
  266. if (!devm_request_mem_region(&dev->dev, res->start,
  267. resource_size(res), dev_name(&dev->dev)))
  268. return -EBUSY;
  269. drvdata->base = devm_ioremap(&dev->dev, res->start,
  270. resource_size(res));
  271. if (!drvdata->base)
  272. return -EBUSY;
  273. hold = plat->hold ?: 20; /* tDH */
  274. spw = plat->spw ?: 90; /* max(tREADID, tWP, tRP) */
  275. hold = TXX9NDFMC_NS_TO_CYC(gbusclk, hold);
  276. spw = TXX9NDFMC_NS_TO_CYC(gbusclk, spw);
  277. if (plat->flags & NDFMC_PLAT_FLAG_HOLDADD)
  278. hold -= 2; /* actual hold time : (HOLD + 2) BUSCLK */
  279. spw -= 1; /* actual wait time : (SPW + 1) BUSCLK */
  280. hold = clamp(hold, 1, 15);
  281. drvdata->hold = hold;
  282. spw = clamp(spw, 1, 15);
  283. drvdata->spw = spw;
  284. dev_info(&dev->dev, "CLK:%ldMHz HOLD:%d SPW:%d\n",
  285. (gbusclk + 500000) / 1000000, hold, spw);
  286. spin_lock_init(&drvdata->hw_control.lock);
  287. init_waitqueue_head(&drvdata->hw_control.wq);
  288. platform_set_drvdata(dev, drvdata);
  289. txx9ndfmc_initialize(dev);
  290. for (i = 0; i < MAX_TXX9NDFMC_DEV; i++) {
  291. struct txx9ndfmc_priv *txx9_priv;
  292. struct nand_chip *chip;
  293. struct mtd_info *mtd;
  294. int nr_parts;
  295. if (!(plat->ch_mask & (1 << i)))
  296. continue;
  297. txx9_priv = kzalloc(sizeof(struct txx9ndfmc_priv),
  298. GFP_KERNEL);
  299. if (!txx9_priv) {
  300. dev_err(&dev->dev, "Unable to allocate "
  301. "TXx9 NDFMC MTD device structure.\n");
  302. continue;
  303. }
  304. chip = &txx9_priv->chip;
  305. mtd = &txx9_priv->mtd;
  306. mtd->owner = THIS_MODULE;
  307. mtd->priv = chip;
  308. chip->read_byte = txx9ndfmc_read_byte;
  309. chip->read_buf = txx9ndfmc_read_buf;
  310. chip->write_buf = txx9ndfmc_write_buf;
  311. chip->verify_buf = txx9ndfmc_verify_buf;
  312. chip->cmd_ctrl = txx9ndfmc_cmd_ctrl;
  313. chip->dev_ready = txx9ndfmc_dev_ready;
  314. chip->ecc.calculate = txx9ndfmc_calculate_ecc;
  315. chip->ecc.correct = txx9ndfmc_correct_data;
  316. chip->ecc.hwctl = txx9ndfmc_enable_hwecc;
  317. chip->ecc.mode = NAND_ECC_HW;
  318. /* txx9ndfmc_nand_scan will overwrite ecc.size and ecc.bytes */
  319. chip->ecc.size = 256;
  320. chip->ecc.bytes = 3;
  321. chip->chip_delay = 100;
  322. chip->controller = &drvdata->hw_control;
  323. chip->priv = txx9_priv;
  324. txx9_priv->dev = dev;
  325. if (plat->ch_mask != 1) {
  326. txx9_priv->cs = i;
  327. txx9_priv->mtdname = kasprintf(GFP_KERNEL, "%s.%u",
  328. dev_name(&dev->dev), i);
  329. } else {
  330. txx9_priv->cs = -1;
  331. txx9_priv->mtdname = kstrdup(dev_name(&dev->dev),
  332. GFP_KERNEL);
  333. }
  334. if (!txx9_priv->mtdname) {
  335. kfree(txx9_priv);
  336. dev_err(&dev->dev, "Unable to allocate MTD name.\n");
  337. continue;
  338. }
  339. if (plat->wide_mask & (1 << i))
  340. chip->options |= NAND_BUSWIDTH_16;
  341. if (txx9ndfmc_nand_scan(mtd)) {
  342. kfree(txx9_priv->mtdname);
  343. kfree(txx9_priv);
  344. continue;
  345. }
  346. mtd->name = txx9_priv->mtdname;
  347. nr_parts = parse_mtd_partitions(mtd, probes,
  348. &drvdata->parts[i], 0);
  349. mtd_device_register(mtd, drvdata->parts[i], nr_parts);
  350. drvdata->mtds[i] = mtd;
  351. }
  352. return 0;
  353. }
  354. static int __exit txx9ndfmc_remove(struct platform_device *dev)
  355. {
  356. struct txx9ndfmc_drvdata *drvdata = platform_get_drvdata(dev);
  357. int i;
  358. platform_set_drvdata(dev, NULL);
  359. if (!drvdata)
  360. return 0;
  361. for (i = 0; i < MAX_TXX9NDFMC_DEV; i++) {
  362. struct mtd_info *mtd = drvdata->mtds[i];
  363. struct nand_chip *chip;
  364. struct txx9ndfmc_priv *txx9_priv;
  365. if (!mtd)
  366. continue;
  367. chip = mtd->priv;
  368. txx9_priv = chip->priv;
  369. nand_release(mtd);
  370. kfree(drvdata->parts[i]);
  371. kfree(txx9_priv->mtdname);
  372. kfree(txx9_priv);
  373. }
  374. return 0;
  375. }
  376. #ifdef CONFIG_PM
  377. static int txx9ndfmc_resume(struct platform_device *dev)
  378. {
  379. if (platform_get_drvdata(dev))
  380. txx9ndfmc_initialize(dev);
  381. return 0;
  382. }
  383. #else
  384. #define txx9ndfmc_resume NULL
  385. #endif
  386. static struct platform_driver txx9ndfmc_driver = {
  387. .remove = __exit_p(txx9ndfmc_remove),
  388. .resume = txx9ndfmc_resume,
  389. .driver = {
  390. .name = "txx9ndfmc",
  391. .owner = THIS_MODULE,
  392. },
  393. };
  394. static int __init txx9ndfmc_init(void)
  395. {
  396. return platform_driver_probe(&txx9ndfmc_driver, txx9ndfmc_probe);
  397. }
  398. static void __exit txx9ndfmc_exit(void)
  399. {
  400. platform_driver_unregister(&txx9ndfmc_driver);
  401. }
  402. module_init(txx9ndfmc_init);
  403. module_exit(txx9ndfmc_exit);
  404. MODULE_LICENSE("GPL");
  405. MODULE_DESCRIPTION("TXx9 SoC NAND flash controller driver");
  406. MODULE_ALIAS("platform:txx9ndfmc");