txx9ndfmc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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/err.h>
  13. #include <linux/init.h>
  14. #include <linux/slab.h>
  15. #include <linux/module.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/delay.h>
  18. #include <linux/mtd/mtd.h>
  19. #include <linux/mtd/nand.h>
  20. #include <linux/mtd/nand_ecc.h>
  21. #include <linux/mtd/partitions.h>
  22. #include <linux/io.h>
  23. #include <asm/txx9/ndfmc.h>
  24. /* TXX9 NDFMC Registers */
  25. #define TXX9_NDFDTR 0x00
  26. #define TXX9_NDFMCR 0x04
  27. #define TXX9_NDFSR 0x08
  28. #define TXX9_NDFISR 0x0c
  29. #define TXX9_NDFIMR 0x10
  30. #define TXX9_NDFSPR 0x14
  31. #define TXX9_NDFRSTR 0x18 /* not TX4939 */
  32. /* NDFMCR : NDFMC Mode Control */
  33. #define TXX9_NDFMCR_WE 0x80
  34. #define TXX9_NDFMCR_ECC_ALL 0x60
  35. #define TXX9_NDFMCR_ECC_RESET 0x60
  36. #define TXX9_NDFMCR_ECC_READ 0x40
  37. #define TXX9_NDFMCR_ECC_ON 0x20
  38. #define TXX9_NDFMCR_ECC_OFF 0x00
  39. #define TXX9_NDFMCR_CE 0x10
  40. #define TXX9_NDFMCR_BSPRT 0x04 /* TX4925/TX4926 only */
  41. #define TXX9_NDFMCR_ALE 0x02
  42. #define TXX9_NDFMCR_CLE 0x01
  43. /* TX4939 only */
  44. #define TXX9_NDFMCR_X16 0x0400
  45. #define TXX9_NDFMCR_DMAREQ_MASK 0x0300
  46. #define TXX9_NDFMCR_DMAREQ_NODMA 0x0000
  47. #define TXX9_NDFMCR_DMAREQ_128 0x0100
  48. #define TXX9_NDFMCR_DMAREQ_256 0x0200
  49. #define TXX9_NDFMCR_DMAREQ_512 0x0300
  50. #define TXX9_NDFMCR_CS_MASK 0x0c
  51. #define TXX9_NDFMCR_CS(ch) ((ch) << 2)
  52. /* NDFMCR : NDFMC Status */
  53. #define TXX9_NDFSR_BUSY 0x80
  54. /* TX4939 only */
  55. #define TXX9_NDFSR_DMARUN 0x40
  56. /* NDFMCR : NDFMC Reset */
  57. #define TXX9_NDFRSTR_RST 0x01
  58. struct txx9ndfmc_priv {
  59. struct platform_device *dev;
  60. struct nand_chip chip;
  61. struct mtd_info mtd;
  62. int cs;
  63. const char *mtdname;
  64. };
  65. #define MAX_TXX9NDFMC_DEV 4
  66. struct txx9ndfmc_drvdata {
  67. struct mtd_info *mtds[MAX_TXX9NDFMC_DEV];
  68. void __iomem *base;
  69. unsigned char hold; /* in gbusclock */
  70. unsigned char spw; /* in gbusclock */
  71. struct nand_hw_control hw_control;
  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_get_platdata(&dev->dev);
  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 void txx9ndfmc_cmd_ctrl(struct mtd_info *mtd, int cmd,
  118. unsigned int ctrl)
  119. {
  120. struct nand_chip *chip = mtd->priv;
  121. struct txx9ndfmc_priv *txx9_priv = chip->priv;
  122. struct platform_device *dev = txx9_priv->dev;
  123. struct txx9ndfmc_platform_data *plat = dev_get_platdata(&dev->dev);
  124. if (ctrl & NAND_CTRL_CHANGE) {
  125. u32 mcr = txx9ndfmc_read(dev, TXX9_NDFMCR);
  126. mcr &= ~(TXX9_NDFMCR_CLE | TXX9_NDFMCR_ALE | TXX9_NDFMCR_CE);
  127. mcr |= ctrl & NAND_CLE ? TXX9_NDFMCR_CLE : 0;
  128. mcr |= ctrl & NAND_ALE ? TXX9_NDFMCR_ALE : 0;
  129. /* TXX9_NDFMCR_CE bit is 0:high 1:low */
  130. mcr |= ctrl & NAND_NCE ? TXX9_NDFMCR_CE : 0;
  131. if (txx9_priv->cs >= 0 && (ctrl & NAND_NCE)) {
  132. mcr &= ~TXX9_NDFMCR_CS_MASK;
  133. mcr |= TXX9_NDFMCR_CS(txx9_priv->cs);
  134. }
  135. txx9ndfmc_write(dev, mcr, TXX9_NDFMCR);
  136. }
  137. if (cmd != NAND_CMD_NONE)
  138. txx9ndfmc_write(dev, cmd & 0xff, TXX9_NDFDTR);
  139. if (plat->flags & NDFMC_PLAT_FLAG_DUMMYWRITE) {
  140. /* dummy write to update external latch */
  141. if ((ctrl & NAND_CTRL_CHANGE) && cmd == NAND_CMD_NONE)
  142. txx9ndfmc_write(dev, 0, TXX9_NDFDTR);
  143. }
  144. mmiowb();
  145. }
  146. static int txx9ndfmc_dev_ready(struct mtd_info *mtd)
  147. {
  148. struct platform_device *dev = mtd_to_platdev(mtd);
  149. return !(txx9ndfmc_read(dev, TXX9_NDFSR) & TXX9_NDFSR_BUSY);
  150. }
  151. static int txx9ndfmc_calculate_ecc(struct mtd_info *mtd, const uint8_t *dat,
  152. uint8_t *ecc_code)
  153. {
  154. struct platform_device *dev = mtd_to_platdev(mtd);
  155. struct nand_chip *chip = mtd->priv;
  156. int eccbytes;
  157. u32 mcr = txx9ndfmc_read(dev, TXX9_NDFMCR);
  158. mcr &= ~TXX9_NDFMCR_ECC_ALL;
  159. txx9ndfmc_write(dev, mcr | TXX9_NDFMCR_ECC_OFF, TXX9_NDFMCR);
  160. txx9ndfmc_write(dev, mcr | TXX9_NDFMCR_ECC_READ, TXX9_NDFMCR);
  161. for (eccbytes = chip->ecc.bytes; eccbytes > 0; eccbytes -= 3) {
  162. ecc_code[1] = txx9ndfmc_read(dev, TXX9_NDFDTR);
  163. ecc_code[0] = txx9ndfmc_read(dev, TXX9_NDFDTR);
  164. ecc_code[2] = txx9ndfmc_read(dev, TXX9_NDFDTR);
  165. ecc_code += 3;
  166. }
  167. txx9ndfmc_write(dev, mcr | TXX9_NDFMCR_ECC_OFF, TXX9_NDFMCR);
  168. return 0;
  169. }
  170. static int txx9ndfmc_correct_data(struct mtd_info *mtd, unsigned char *buf,
  171. unsigned char *read_ecc, unsigned char *calc_ecc)
  172. {
  173. struct nand_chip *chip = mtd->priv;
  174. int eccsize;
  175. int corrected = 0;
  176. int stat;
  177. for (eccsize = chip->ecc.size; eccsize > 0; eccsize -= 256) {
  178. stat = __nand_correct_data(buf, read_ecc, calc_ecc, 256);
  179. if (stat < 0)
  180. return stat;
  181. corrected += stat;
  182. buf += 256;
  183. read_ecc += 3;
  184. calc_ecc += 3;
  185. }
  186. return corrected;
  187. }
  188. static void txx9ndfmc_enable_hwecc(struct mtd_info *mtd, int mode)
  189. {
  190. struct platform_device *dev = mtd_to_platdev(mtd);
  191. u32 mcr = txx9ndfmc_read(dev, TXX9_NDFMCR);
  192. mcr &= ~TXX9_NDFMCR_ECC_ALL;
  193. txx9ndfmc_write(dev, mcr | TXX9_NDFMCR_ECC_RESET, TXX9_NDFMCR);
  194. txx9ndfmc_write(dev, mcr | TXX9_NDFMCR_ECC_OFF, TXX9_NDFMCR);
  195. txx9ndfmc_write(dev, mcr | TXX9_NDFMCR_ECC_ON, TXX9_NDFMCR);
  196. }
  197. static void txx9ndfmc_initialize(struct platform_device *dev)
  198. {
  199. struct txx9ndfmc_platform_data *plat = dev_get_platdata(&dev->dev);
  200. struct txx9ndfmc_drvdata *drvdata = platform_get_drvdata(dev);
  201. int tmout = 100;
  202. if (plat->flags & NDFMC_PLAT_FLAG_NO_RSTR)
  203. ; /* no NDFRSTR. Write to NDFSPR resets the NDFMC. */
  204. else {
  205. /* reset NDFMC */
  206. txx9ndfmc_write(dev,
  207. txx9ndfmc_read(dev, TXX9_NDFRSTR) |
  208. TXX9_NDFRSTR_RST,
  209. TXX9_NDFRSTR);
  210. while (txx9ndfmc_read(dev, TXX9_NDFRSTR) & TXX9_NDFRSTR_RST) {
  211. if (--tmout == 0) {
  212. dev_err(&dev->dev, "reset failed.\n");
  213. break;
  214. }
  215. udelay(1);
  216. }
  217. }
  218. /* setup Hold Time, Strobe Pulse Width */
  219. txx9ndfmc_write(dev, (drvdata->hold << 4) | drvdata->spw, TXX9_NDFSPR);
  220. txx9ndfmc_write(dev,
  221. (plat->flags & NDFMC_PLAT_FLAG_USE_BSPRT) ?
  222. TXX9_NDFMCR_BSPRT : 0, TXX9_NDFMCR);
  223. }
  224. #define TXX9NDFMC_NS_TO_CYC(gbusclk, ns) \
  225. DIV_ROUND_UP((ns) * DIV_ROUND_UP(gbusclk, 1000), 1000000)
  226. static int txx9ndfmc_nand_scan(struct mtd_info *mtd)
  227. {
  228. struct nand_chip *chip = mtd->priv;
  229. int ret;
  230. ret = nand_scan_ident(mtd, 1, NULL);
  231. if (!ret) {
  232. if (mtd->writesize >= 512) {
  233. /* Hardware ECC 6 byte ECC per 512 Byte data */
  234. chip->ecc.size = 512;
  235. chip->ecc.bytes = 6;
  236. }
  237. ret = nand_scan_tail(mtd);
  238. }
  239. return ret;
  240. }
  241. static int __init txx9ndfmc_probe(struct platform_device *dev)
  242. {
  243. struct txx9ndfmc_platform_data *plat = dev_get_platdata(&dev->dev);
  244. int hold, spw;
  245. int i;
  246. struct txx9ndfmc_drvdata *drvdata;
  247. unsigned long gbusclk = plat->gbus_clock;
  248. struct resource *res;
  249. drvdata = devm_kzalloc(&dev->dev, sizeof(*drvdata), GFP_KERNEL);
  250. if (!drvdata)
  251. return -ENOMEM;
  252. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  253. drvdata->base = devm_ioremap_resource(&dev->dev, res);
  254. if (IS_ERR(drvdata->base))
  255. return PTR_ERR(drvdata->base);
  256. hold = plat->hold ?: 20; /* tDH */
  257. spw = plat->spw ?: 90; /* max(tREADID, tWP, tRP) */
  258. hold = TXX9NDFMC_NS_TO_CYC(gbusclk, hold);
  259. spw = TXX9NDFMC_NS_TO_CYC(gbusclk, spw);
  260. if (plat->flags & NDFMC_PLAT_FLAG_HOLDADD)
  261. hold -= 2; /* actual hold time : (HOLD + 2) BUSCLK */
  262. spw -= 1; /* actual wait time : (SPW + 1) BUSCLK */
  263. hold = clamp(hold, 1, 15);
  264. drvdata->hold = hold;
  265. spw = clamp(spw, 1, 15);
  266. drvdata->spw = spw;
  267. dev_info(&dev->dev, "CLK:%ldMHz HOLD:%d SPW:%d\n",
  268. (gbusclk + 500000) / 1000000, hold, spw);
  269. spin_lock_init(&drvdata->hw_control.lock);
  270. init_waitqueue_head(&drvdata->hw_control.wq);
  271. platform_set_drvdata(dev, drvdata);
  272. txx9ndfmc_initialize(dev);
  273. for (i = 0; i < MAX_TXX9NDFMC_DEV; i++) {
  274. struct txx9ndfmc_priv *txx9_priv;
  275. struct nand_chip *chip;
  276. struct mtd_info *mtd;
  277. if (!(plat->ch_mask & (1 << i)))
  278. continue;
  279. txx9_priv = kzalloc(sizeof(struct txx9ndfmc_priv),
  280. GFP_KERNEL);
  281. if (!txx9_priv)
  282. continue;
  283. chip = &txx9_priv->chip;
  284. mtd = &txx9_priv->mtd;
  285. mtd->dev.parent = &dev->dev;
  286. mtd->priv = chip;
  287. chip->read_byte = txx9ndfmc_read_byte;
  288. chip->read_buf = txx9ndfmc_read_buf;
  289. chip->write_buf = txx9ndfmc_write_buf;
  290. chip->cmd_ctrl = txx9ndfmc_cmd_ctrl;
  291. chip->dev_ready = txx9ndfmc_dev_ready;
  292. chip->ecc.calculate = txx9ndfmc_calculate_ecc;
  293. chip->ecc.correct = txx9ndfmc_correct_data;
  294. chip->ecc.hwctl = txx9ndfmc_enable_hwecc;
  295. chip->ecc.mode = NAND_ECC_HW;
  296. /* txx9ndfmc_nand_scan will overwrite ecc.size and ecc.bytes */
  297. chip->ecc.size = 256;
  298. chip->ecc.bytes = 3;
  299. chip->ecc.strength = 1;
  300. chip->chip_delay = 100;
  301. chip->controller = &drvdata->hw_control;
  302. chip->priv = txx9_priv;
  303. txx9_priv->dev = dev;
  304. if (plat->ch_mask != 1) {
  305. txx9_priv->cs = i;
  306. txx9_priv->mtdname = kasprintf(GFP_KERNEL, "%s.%u",
  307. dev_name(&dev->dev), i);
  308. } else {
  309. txx9_priv->cs = -1;
  310. txx9_priv->mtdname = kstrdup(dev_name(&dev->dev),
  311. GFP_KERNEL);
  312. }
  313. if (!txx9_priv->mtdname) {
  314. kfree(txx9_priv);
  315. dev_err(&dev->dev, "Unable to allocate MTD name.\n");
  316. continue;
  317. }
  318. if (plat->wide_mask & (1 << i))
  319. chip->options |= NAND_BUSWIDTH_16;
  320. if (txx9ndfmc_nand_scan(mtd)) {
  321. kfree(txx9_priv->mtdname);
  322. kfree(txx9_priv);
  323. continue;
  324. }
  325. mtd->name = txx9_priv->mtdname;
  326. mtd_device_parse_register(mtd, NULL, NULL, NULL, 0);
  327. drvdata->mtds[i] = mtd;
  328. }
  329. return 0;
  330. }
  331. static int __exit txx9ndfmc_remove(struct platform_device *dev)
  332. {
  333. struct txx9ndfmc_drvdata *drvdata = platform_get_drvdata(dev);
  334. int i;
  335. if (!drvdata)
  336. return 0;
  337. for (i = 0; i < MAX_TXX9NDFMC_DEV; i++) {
  338. struct mtd_info *mtd = drvdata->mtds[i];
  339. struct nand_chip *chip;
  340. struct txx9ndfmc_priv *txx9_priv;
  341. if (!mtd)
  342. continue;
  343. chip = mtd->priv;
  344. txx9_priv = chip->priv;
  345. nand_release(mtd);
  346. kfree(txx9_priv->mtdname);
  347. kfree(txx9_priv);
  348. }
  349. return 0;
  350. }
  351. #ifdef CONFIG_PM
  352. static int txx9ndfmc_resume(struct platform_device *dev)
  353. {
  354. if (platform_get_drvdata(dev))
  355. txx9ndfmc_initialize(dev);
  356. return 0;
  357. }
  358. #else
  359. #define txx9ndfmc_resume NULL
  360. #endif
  361. static struct platform_driver txx9ndfmc_driver = {
  362. .remove = __exit_p(txx9ndfmc_remove),
  363. .resume = txx9ndfmc_resume,
  364. .driver = {
  365. .name = "txx9ndfmc",
  366. },
  367. };
  368. module_platform_driver_probe(txx9ndfmc_driver, txx9ndfmc_probe);
  369. MODULE_LICENSE("GPL");
  370. MODULE_DESCRIPTION("TXx9 SoC NAND flash controller driver");
  371. MODULE_ALIAS("platform:txx9ndfmc");