txx9ndfmc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. char mtdname[BUS_ID_SIZE + 2];
  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. 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. ecc_code[1] = txx9ndfmc_read(dev, TXX9_NDFDTR);
  172. ecc_code[0] = txx9ndfmc_read(dev, TXX9_NDFDTR);
  173. ecc_code[2] = txx9ndfmc_read(dev, TXX9_NDFDTR);
  174. txx9ndfmc_write(dev, mcr | TXX9_NDFMCR_ECC_OFF, TXX9_NDFMCR);
  175. return 0;
  176. }
  177. static void txx9ndfmc_enable_hwecc(struct mtd_info *mtd, int mode)
  178. {
  179. struct platform_device *dev = mtd_to_platdev(mtd);
  180. u32 mcr = txx9ndfmc_read(dev, TXX9_NDFMCR);
  181. mcr &= ~TXX9_NDFMCR_ECC_ALL;
  182. txx9ndfmc_write(dev, mcr | TXX9_NDFMCR_ECC_RESET, TXX9_NDFMCR);
  183. txx9ndfmc_write(dev, mcr | TXX9_NDFMCR_ECC_OFF, TXX9_NDFMCR);
  184. txx9ndfmc_write(dev, mcr | TXX9_NDFMCR_ECC_ON, TXX9_NDFMCR);
  185. }
  186. static void txx9ndfmc_initialize(struct platform_device *dev)
  187. {
  188. struct txx9ndfmc_platform_data *plat = dev->dev.platform_data;
  189. struct txx9ndfmc_drvdata *drvdata = platform_get_drvdata(dev);
  190. int tmout = 100;
  191. if (plat->flags & NDFMC_PLAT_FLAG_NO_RSTR)
  192. ; /* no NDFRSTR. Write to NDFSPR resets the NDFMC. */
  193. else {
  194. /* reset NDFMC */
  195. txx9ndfmc_write(dev,
  196. txx9ndfmc_read(dev, TXX9_NDFRSTR) |
  197. TXX9_NDFRSTR_RST,
  198. TXX9_NDFRSTR);
  199. while (txx9ndfmc_read(dev, TXX9_NDFRSTR) & TXX9_NDFRSTR_RST) {
  200. if (--tmout == 0) {
  201. dev_err(&dev->dev, "reset failed.\n");
  202. break;
  203. }
  204. udelay(1);
  205. }
  206. }
  207. /* setup Hold Time, Strobe Pulse Width */
  208. txx9ndfmc_write(dev, (drvdata->hold << 4) | drvdata->spw, TXX9_NDFSPR);
  209. txx9ndfmc_write(dev,
  210. (plat->flags & NDFMC_PLAT_FLAG_USE_BSPRT) ?
  211. TXX9_NDFMCR_BSPRT : 0, TXX9_NDFMCR);
  212. }
  213. #define TXX9NDFMC_NS_TO_CYC(gbusclk, ns) \
  214. DIV_ROUND_UP((ns) * DIV_ROUND_UP(gbusclk, 1000), 1000000)
  215. static int __init txx9ndfmc_probe(struct platform_device *dev)
  216. {
  217. struct txx9ndfmc_platform_data *plat = dev->dev.platform_data;
  218. #ifdef CONFIG_MTD_PARTITIONS
  219. static const char *probes[] = { "cmdlinepart", NULL };
  220. #endif
  221. int hold, spw;
  222. int i;
  223. struct txx9ndfmc_drvdata *drvdata;
  224. unsigned long gbusclk = plat->gbus_clock;
  225. struct resource *res;
  226. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  227. if (!res)
  228. return -ENODEV;
  229. drvdata = devm_kzalloc(&dev->dev, sizeof(*drvdata), GFP_KERNEL);
  230. if (!drvdata)
  231. return -ENOMEM;
  232. if (!devm_request_mem_region(&dev->dev, res->start,
  233. resource_size(res), dev_name(&dev->dev)))
  234. return -EBUSY;
  235. drvdata->base = devm_ioremap(&dev->dev, res->start,
  236. resource_size(res));
  237. if (!drvdata->base)
  238. return -EBUSY;
  239. hold = plat->hold ?: 20; /* tDH */
  240. spw = plat->spw ?: 90; /* max(tREADID, tWP, tRP) */
  241. hold = TXX9NDFMC_NS_TO_CYC(gbusclk, hold);
  242. spw = TXX9NDFMC_NS_TO_CYC(gbusclk, spw);
  243. if (plat->flags & NDFMC_PLAT_FLAG_HOLDADD)
  244. hold -= 2; /* actual hold time : (HOLD + 2) BUSCLK */
  245. spw -= 1; /* actual wait time : (SPW + 1) BUSCLK */
  246. hold = clamp(hold, 1, 15);
  247. drvdata->hold = hold;
  248. spw = clamp(spw, 1, 15);
  249. drvdata->spw = spw;
  250. dev_info(&dev->dev, "CLK:%ldMHz HOLD:%d SPW:%d\n",
  251. (gbusclk + 500000) / 1000000, hold, spw);
  252. spin_lock_init(&drvdata->hw_control.lock);
  253. init_waitqueue_head(&drvdata->hw_control.wq);
  254. platform_set_drvdata(dev, drvdata);
  255. txx9ndfmc_initialize(dev);
  256. for (i = 0; i < MAX_TXX9NDFMC_DEV; i++) {
  257. struct txx9ndfmc_priv *txx9_priv;
  258. struct nand_chip *chip;
  259. struct mtd_info *mtd;
  260. #ifdef CONFIG_MTD_PARTITIONS
  261. int nr_parts;
  262. #endif
  263. if (!(plat->ch_mask & (1 << i)))
  264. continue;
  265. txx9_priv = kzalloc(sizeof(struct txx9ndfmc_priv),
  266. GFP_KERNEL);
  267. if (!txx9_priv) {
  268. dev_err(&dev->dev, "Unable to allocate "
  269. "TXx9 NDFMC MTD device structure.\n");
  270. continue;
  271. }
  272. chip = &txx9_priv->chip;
  273. mtd = &txx9_priv->mtd;
  274. mtd->owner = THIS_MODULE;
  275. mtd->priv = chip;
  276. chip->read_byte = txx9ndfmc_read_byte;
  277. chip->read_buf = txx9ndfmc_read_buf;
  278. chip->write_buf = txx9ndfmc_write_buf;
  279. chip->verify_buf = txx9ndfmc_verify_buf;
  280. chip->cmd_ctrl = txx9ndfmc_cmd_ctrl;
  281. chip->dev_ready = txx9ndfmc_dev_ready;
  282. chip->ecc.calculate = txx9ndfmc_calculate_ecc;
  283. chip->ecc.correct = nand_correct_data;
  284. chip->ecc.hwctl = txx9ndfmc_enable_hwecc;
  285. chip->ecc.mode = NAND_ECC_HW;
  286. chip->ecc.size = 256;
  287. chip->ecc.bytes = 3;
  288. chip->chip_delay = 100;
  289. chip->controller = &drvdata->hw_control;
  290. chip->priv = txx9_priv;
  291. txx9_priv->dev = dev;
  292. if (plat->ch_mask != 1) {
  293. txx9_priv->cs = i;
  294. sprintf(txx9_priv->mtdname, "%s.%u",
  295. dev_name(&dev->dev), i);
  296. } else {
  297. txx9_priv->cs = -1;
  298. strcpy(txx9_priv->mtdname, dev_name(&dev->dev));
  299. }
  300. if (plat->wide_mask & (1 << i))
  301. chip->options |= NAND_BUSWIDTH_16;
  302. if (nand_scan(mtd, 1)) {
  303. kfree(txx9_priv);
  304. continue;
  305. }
  306. mtd->name = txx9_priv->mtdname;
  307. #ifdef CONFIG_MTD_PARTITIONS
  308. nr_parts = parse_mtd_partitions(mtd, probes,
  309. &drvdata->parts[i], 0);
  310. if (nr_parts > 0)
  311. add_mtd_partitions(mtd, drvdata->parts[i], nr_parts);
  312. #endif
  313. add_mtd_device(mtd);
  314. drvdata->mtds[i] = mtd;
  315. }
  316. return 0;
  317. }
  318. static int __exit txx9ndfmc_remove(struct platform_device *dev)
  319. {
  320. struct txx9ndfmc_drvdata *drvdata = platform_get_drvdata(dev);
  321. int i;
  322. platform_set_drvdata(dev, NULL);
  323. if (!drvdata)
  324. return 0;
  325. for (i = 0; i < MAX_TXX9NDFMC_DEV; i++) {
  326. struct mtd_info *mtd = drvdata->mtds[i];
  327. struct nand_chip *chip;
  328. struct txx9ndfmc_priv *txx9_priv;
  329. if (!mtd)
  330. continue;
  331. chip = mtd->priv;
  332. txx9_priv = chip->priv;
  333. #ifdef CONFIG_MTD_PARTITIONS
  334. del_mtd_partitions(mtd);
  335. kfree(drvdata->parts[i]);
  336. #endif
  337. del_mtd_device(mtd);
  338. kfree(txx9_priv);
  339. }
  340. return 0;
  341. }
  342. #ifdef CONFIG_PM
  343. static int txx9ndfmc_resume(struct platform_device *dev)
  344. {
  345. if (platform_get_drvdata(dev))
  346. txx9ndfmc_initialize(dev);
  347. return 0;
  348. }
  349. #else
  350. #define txx9ndfmc_resume NULL
  351. #endif
  352. static struct platform_driver txx9ndfmc_driver = {
  353. .remove = __exit_p(txx9ndfmc_remove),
  354. .resume = txx9ndfmc_resume,
  355. .driver = {
  356. .name = "txx9ndfmc",
  357. .owner = THIS_MODULE,
  358. },
  359. };
  360. static int __init txx9ndfmc_init(void)
  361. {
  362. return platform_driver_probe(&txx9ndfmc_driver, txx9ndfmc_probe);
  363. }
  364. static void __exit txx9ndfmc_exit(void)
  365. {
  366. platform_driver_unregister(&txx9ndfmc_driver);
  367. }
  368. module_init(txx9ndfmc_init);
  369. module_exit(txx9ndfmc_exit);
  370. MODULE_LICENSE("GPL");
  371. MODULE_DESCRIPTION("TXx9 SoC NAND flash controller driver");
  372. MODULE_ALIAS("platform:txx9ndfmc");