txx9ndfmc.c 11 KB

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