tmio_nand.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. /*
  2. * Toshiba TMIO NAND flash controller driver
  3. *
  4. * Slightly murky pre-git history of the driver:
  5. *
  6. * Copyright (c) Ian Molton 2004, 2005, 2008
  7. * Original work, independant of sharps code. Included hardware ECC support.
  8. * Hard ECC did not work for writes in the early revisions.
  9. * Copyright (c) Dirk Opfer 2005.
  10. * Modifications developed from sharps code but
  11. * NOT containing any, ported onto Ians base.
  12. * Copyright (c) Chris Humbert 2005
  13. * Copyright (c) Dmitry Baryshkov 2008
  14. * Minor fixes
  15. *
  16. * Parts copyright Sebastian Carlier
  17. *
  18. * This file is licensed under
  19. * the terms of the GNU General Public License version 2. This program
  20. * is licensed "as is" without any warranty of any kind, whether express
  21. * or implied.
  22. *
  23. */
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/mfd/core.h>
  28. #include <linux/mfd/tmio.h>
  29. #include <linux/delay.h>
  30. #include <linux/io.h>
  31. #include <linux/irq.h>
  32. #include <linux/interrupt.h>
  33. #include <linux/ioport.h>
  34. #include <linux/mtd/mtd.h>
  35. #include <linux/mtd/nand.h>
  36. #include <linux/mtd/nand_ecc.h>
  37. #include <linux/mtd/partitions.h>
  38. #include <linux/slab.h>
  39. /*--------------------------------------------------------------------------*/
  40. /*
  41. * NAND Flash Host Controller Configuration Register
  42. */
  43. #define CCR_COMMAND 0x04 /* w Command */
  44. #define CCR_BASE 0x10 /* l NAND Flash Control Reg Base Addr */
  45. #define CCR_INTP 0x3d /* b Interrupt Pin */
  46. #define CCR_INTE 0x48 /* b Interrupt Enable */
  47. #define CCR_EC 0x4a /* b Event Control */
  48. #define CCR_ICC 0x4c /* b Internal Clock Control */
  49. #define CCR_ECCC 0x5b /* b ECC Control */
  50. #define CCR_NFTC 0x60 /* b NAND Flash Transaction Control */
  51. #define CCR_NFM 0x61 /* b NAND Flash Monitor */
  52. #define CCR_NFPSC 0x62 /* b NAND Flash Power Supply Control */
  53. #define CCR_NFDC 0x63 /* b NAND Flash Detect Control */
  54. /*
  55. * NAND Flash Control Register
  56. */
  57. #define FCR_DATA 0x00 /* bwl Data Register */
  58. #define FCR_MODE 0x04 /* b Mode Register */
  59. #define FCR_STATUS 0x05 /* b Status Register */
  60. #define FCR_ISR 0x06 /* b Interrupt Status Register */
  61. #define FCR_IMR 0x07 /* b Interrupt Mask Register */
  62. /* FCR_MODE Register Command List */
  63. #define FCR_MODE_DATA 0x94 /* Data Data_Mode */
  64. #define FCR_MODE_COMMAND 0x95 /* Data Command_Mode */
  65. #define FCR_MODE_ADDRESS 0x96 /* Data Address_Mode */
  66. #define FCR_MODE_HWECC_CALC 0xB4 /* HW-ECC Data */
  67. #define FCR_MODE_HWECC_RESULT 0xD4 /* HW-ECC Calc result Read_Mode */
  68. #define FCR_MODE_HWECC_RESET 0xF4 /* HW-ECC Reset */
  69. #define FCR_MODE_POWER_ON 0x0C /* Power Supply ON to SSFDC card */
  70. #define FCR_MODE_POWER_OFF 0x08 /* Power Supply OFF to SSFDC card */
  71. #define FCR_MODE_LED_OFF 0x00 /* LED OFF */
  72. #define FCR_MODE_LED_ON 0x04 /* LED ON */
  73. #define FCR_MODE_EJECT_ON 0x68 /* Ejection events active */
  74. #define FCR_MODE_EJECT_OFF 0x08 /* Ejection events ignored */
  75. #define FCR_MODE_LOCK 0x6C /* Lock_Mode. Eject Switch Invalid */
  76. #define FCR_MODE_UNLOCK 0x0C /* UnLock_Mode. Eject Switch is valid */
  77. #define FCR_MODE_CONTROLLER_ID 0x40 /* Controller ID Read */
  78. #define FCR_MODE_STANDBY 0x00 /* SSFDC card Changes Standby State */
  79. #define FCR_MODE_WE 0x80
  80. #define FCR_MODE_ECC1 0x40
  81. #define FCR_MODE_ECC0 0x20
  82. #define FCR_MODE_CE 0x10
  83. #define FCR_MODE_PCNT1 0x08
  84. #define FCR_MODE_PCNT0 0x04
  85. #define FCR_MODE_ALE 0x02
  86. #define FCR_MODE_CLE 0x01
  87. #define FCR_STATUS_BUSY 0x80
  88. /*--------------------------------------------------------------------------*/
  89. struct tmio_nand {
  90. struct mtd_info mtd;
  91. struct nand_chip chip;
  92. struct platform_device *dev;
  93. void __iomem *ccr;
  94. void __iomem *fcr;
  95. unsigned long fcr_base;
  96. unsigned int irq;
  97. /* for tmio_nand_read_byte */
  98. u8 read;
  99. unsigned read_good:1;
  100. };
  101. #define mtd_to_tmio(m) container_of(m, struct tmio_nand, mtd)
  102. #ifdef CONFIG_MTD_CMDLINE_PARTS
  103. static const char *part_probes[] = { "cmdlinepart", NULL };
  104. #endif
  105. /*--------------------------------------------------------------------------*/
  106. static void tmio_nand_hwcontrol(struct mtd_info *mtd, int cmd,
  107. unsigned int ctrl)
  108. {
  109. struct tmio_nand *tmio = mtd_to_tmio(mtd);
  110. struct nand_chip *chip = mtd->priv;
  111. if (ctrl & NAND_CTRL_CHANGE) {
  112. u8 mode;
  113. if (ctrl & NAND_NCE) {
  114. mode = FCR_MODE_DATA;
  115. if (ctrl & NAND_CLE)
  116. mode |= FCR_MODE_CLE;
  117. else
  118. mode &= ~FCR_MODE_CLE;
  119. if (ctrl & NAND_ALE)
  120. mode |= FCR_MODE_ALE;
  121. else
  122. mode &= ~FCR_MODE_ALE;
  123. } else {
  124. mode = FCR_MODE_STANDBY;
  125. }
  126. tmio_iowrite8(mode, tmio->fcr + FCR_MODE);
  127. tmio->read_good = 0;
  128. }
  129. if (cmd != NAND_CMD_NONE)
  130. tmio_iowrite8(cmd, chip->IO_ADDR_W);
  131. }
  132. static int tmio_nand_dev_ready(struct mtd_info *mtd)
  133. {
  134. struct tmio_nand *tmio = mtd_to_tmio(mtd);
  135. return !(tmio_ioread8(tmio->fcr + FCR_STATUS) & FCR_STATUS_BUSY);
  136. }
  137. static irqreturn_t tmio_irq(int irq, void *__tmio)
  138. {
  139. struct tmio_nand *tmio = __tmio;
  140. struct nand_chip *nand_chip = &tmio->chip;
  141. /* disable RDYREQ interrupt */
  142. tmio_iowrite8(0x00, tmio->fcr + FCR_IMR);
  143. if (unlikely(!waitqueue_active(&nand_chip->controller->wq)))
  144. dev_warn(&tmio->dev->dev, "spurious interrupt\n");
  145. wake_up(&nand_chip->controller->wq);
  146. return IRQ_HANDLED;
  147. }
  148. /*
  149. *The TMIO core has a RDYREQ interrupt on the posedge of #SMRB.
  150. *This interrupt is normally disabled, but for long operations like
  151. *erase and write, we enable it to wake us up. The irq handler
  152. *disables the interrupt.
  153. */
  154. static int
  155. tmio_nand_wait(struct mtd_info *mtd, struct nand_chip *nand_chip)
  156. {
  157. struct tmio_nand *tmio = mtd_to_tmio(mtd);
  158. long timeout;
  159. /* enable RDYREQ interrupt */
  160. tmio_iowrite8(0x0f, tmio->fcr + FCR_ISR);
  161. tmio_iowrite8(0x81, tmio->fcr + FCR_IMR);
  162. timeout = wait_event_timeout(nand_chip->controller->wq,
  163. tmio_nand_dev_ready(mtd),
  164. msecs_to_jiffies(nand_chip->state == FL_ERASING ? 400 : 20));
  165. if (unlikely(!tmio_nand_dev_ready(mtd))) {
  166. tmio_iowrite8(0x00, tmio->fcr + FCR_IMR);
  167. dev_warn(&tmio->dev->dev, "still busy with %s after %d ms\n",
  168. nand_chip->state == FL_ERASING ? "erase" : "program",
  169. nand_chip->state == FL_ERASING ? 400 : 20);
  170. } else if (unlikely(!timeout)) {
  171. tmio_iowrite8(0x00, tmio->fcr + FCR_IMR);
  172. dev_warn(&tmio->dev->dev, "timeout waiting for interrupt\n");
  173. }
  174. nand_chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
  175. return nand_chip->read_byte(mtd);
  176. }
  177. /*
  178. *The TMIO controller combines two 8-bit data bytes into one 16-bit
  179. *word. This function separates them so nand_base.c works as expected,
  180. *especially its NAND_CMD_READID routines.
  181. *
  182. *To prevent stale data from being read, tmio_nand_hwcontrol() clears
  183. *tmio->read_good.
  184. */
  185. static u_char tmio_nand_read_byte(struct mtd_info *mtd)
  186. {
  187. struct tmio_nand *tmio = mtd_to_tmio(mtd);
  188. unsigned int data;
  189. if (tmio->read_good--)
  190. return tmio->read;
  191. data = tmio_ioread16(tmio->fcr + FCR_DATA);
  192. tmio->read = data >> 8;
  193. return data;
  194. }
  195. /*
  196. *The TMIO controller converts an 8-bit NAND interface to a 16-bit
  197. *bus interface, so all data reads and writes must be 16-bit wide.
  198. *Thus, we implement 16-bit versions of the read, write, and verify
  199. *buffer functions.
  200. */
  201. static void
  202. tmio_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
  203. {
  204. struct tmio_nand *tmio = mtd_to_tmio(mtd);
  205. tmio_iowrite16_rep(tmio->fcr + FCR_DATA, buf, len >> 1);
  206. }
  207. static void tmio_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
  208. {
  209. struct tmio_nand *tmio = mtd_to_tmio(mtd);
  210. tmio_ioread16_rep(tmio->fcr + FCR_DATA, buf, len >> 1);
  211. }
  212. static int
  213. tmio_nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
  214. {
  215. struct tmio_nand *tmio = mtd_to_tmio(mtd);
  216. u16 *p = (u16 *) buf;
  217. for (len >>= 1; len; len--)
  218. if (*(p++) != tmio_ioread16(tmio->fcr + FCR_DATA))
  219. return -EFAULT;
  220. return 0;
  221. }
  222. static void tmio_nand_enable_hwecc(struct mtd_info *mtd, int mode)
  223. {
  224. struct tmio_nand *tmio = mtd_to_tmio(mtd);
  225. tmio_iowrite8(FCR_MODE_HWECC_RESET, tmio->fcr + FCR_MODE);
  226. tmio_ioread8(tmio->fcr + FCR_DATA); /* dummy read */
  227. tmio_iowrite8(FCR_MODE_HWECC_CALC, tmio->fcr + FCR_MODE);
  228. }
  229. static int tmio_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
  230. u_char *ecc_code)
  231. {
  232. struct tmio_nand *tmio = mtd_to_tmio(mtd);
  233. unsigned int ecc;
  234. tmio_iowrite8(FCR_MODE_HWECC_RESULT, tmio->fcr + FCR_MODE);
  235. ecc = tmio_ioread16(tmio->fcr + FCR_DATA);
  236. ecc_code[1] = ecc; /* 000-255 LP7-0 */
  237. ecc_code[0] = ecc >> 8; /* 000-255 LP15-8 */
  238. ecc = tmio_ioread16(tmio->fcr + FCR_DATA);
  239. ecc_code[2] = ecc; /* 000-255 CP5-0,11b */
  240. ecc_code[4] = ecc >> 8; /* 256-511 LP7-0 */
  241. ecc = tmio_ioread16(tmio->fcr + FCR_DATA);
  242. ecc_code[3] = ecc; /* 256-511 LP15-8 */
  243. ecc_code[5] = ecc >> 8; /* 256-511 CP5-0,11b */
  244. tmio_iowrite8(FCR_MODE_DATA, tmio->fcr + FCR_MODE);
  245. return 0;
  246. }
  247. static int tmio_nand_correct_data(struct mtd_info *mtd, unsigned char *buf,
  248. unsigned char *read_ecc, unsigned char *calc_ecc)
  249. {
  250. int r0, r1;
  251. /* assume ecc.size = 512 and ecc.bytes = 6 */
  252. r0 = __nand_correct_data(buf, read_ecc, calc_ecc, 256);
  253. if (r0 < 0)
  254. return r0;
  255. r1 = __nand_correct_data(buf + 256, read_ecc + 3, calc_ecc + 3, 256);
  256. if (r1 < 0)
  257. return r1;
  258. return r0 + r1;
  259. }
  260. static int tmio_hw_init(struct platform_device *dev, struct tmio_nand *tmio)
  261. {
  262. struct mfd_cell *cell = dev_get_platdata(&dev->dev);
  263. int ret;
  264. if (cell->enable) {
  265. ret = cell->enable(dev);
  266. if (ret)
  267. return ret;
  268. }
  269. /* (4Ch) CLKRUN Enable 1st spcrunc */
  270. tmio_iowrite8(0x81, tmio->ccr + CCR_ICC);
  271. /* (10h)BaseAddress 0x1000 spba.spba2 */
  272. tmio_iowrite16(tmio->fcr_base, tmio->ccr + CCR_BASE);
  273. tmio_iowrite16(tmio->fcr_base >> 16, tmio->ccr + CCR_BASE + 2);
  274. /* (04h)Command Register I/O spcmd */
  275. tmio_iowrite8(0x02, tmio->ccr + CCR_COMMAND);
  276. /* (62h) Power Supply Control ssmpwc */
  277. /* HardPowerOFF - SuspendOFF - PowerSupplyWait_4MS */
  278. tmio_iowrite8(0x02, tmio->ccr + CCR_NFPSC);
  279. /* (63h) Detect Control ssmdtc */
  280. tmio_iowrite8(0x02, tmio->ccr + CCR_NFDC);
  281. /* Interrupt status register clear sintst */
  282. tmio_iowrite8(0x0f, tmio->fcr + FCR_ISR);
  283. /* After power supply, Media are reset smode */
  284. tmio_iowrite8(FCR_MODE_POWER_ON, tmio->fcr + FCR_MODE);
  285. tmio_iowrite8(FCR_MODE_COMMAND, tmio->fcr + FCR_MODE);
  286. tmio_iowrite8(NAND_CMD_RESET, tmio->fcr + FCR_DATA);
  287. /* Standby Mode smode */
  288. tmio_iowrite8(FCR_MODE_STANDBY, tmio->fcr + FCR_MODE);
  289. mdelay(5);
  290. return 0;
  291. }
  292. static void tmio_hw_stop(struct platform_device *dev, struct tmio_nand *tmio)
  293. {
  294. struct mfd_cell *cell = dev_get_platdata(&dev->dev);
  295. tmio_iowrite8(FCR_MODE_POWER_OFF, tmio->fcr + FCR_MODE);
  296. if (cell->disable)
  297. cell->disable(dev);
  298. }
  299. static int tmio_probe(struct platform_device *dev)
  300. {
  301. struct mfd_cell *cell = dev_get_platdata(&dev->dev);
  302. struct tmio_nand_data *data = cell->driver_data;
  303. struct resource *fcr = platform_get_resource(dev,
  304. IORESOURCE_MEM, 0);
  305. struct resource *ccr = platform_get_resource(dev,
  306. IORESOURCE_MEM, 1);
  307. int irq = platform_get_irq(dev, 0);
  308. struct tmio_nand *tmio;
  309. struct mtd_info *mtd;
  310. struct nand_chip *nand_chip;
  311. #ifdef CONFIG_MTD_PARTITIONS
  312. struct mtd_partition *parts;
  313. int nbparts = 0;
  314. #endif
  315. int retval;
  316. if (data == NULL)
  317. dev_warn(&dev->dev, "NULL platform data!\n");
  318. tmio = kzalloc(sizeof *tmio, GFP_KERNEL);
  319. if (!tmio) {
  320. retval = -ENOMEM;
  321. goto err_kzalloc;
  322. }
  323. tmio->dev = dev;
  324. platform_set_drvdata(dev, tmio);
  325. mtd = &tmio->mtd;
  326. nand_chip = &tmio->chip;
  327. mtd->priv = nand_chip;
  328. mtd->name = "tmio-nand";
  329. tmio->ccr = ioremap(ccr->start, resource_size(ccr));
  330. if (!tmio->ccr) {
  331. retval = -EIO;
  332. goto err_iomap_ccr;
  333. }
  334. tmio->fcr_base = fcr->start & 0xfffff;
  335. tmio->fcr = ioremap(fcr->start, resource_size(fcr));
  336. if (!tmio->fcr) {
  337. retval = -EIO;
  338. goto err_iomap_fcr;
  339. }
  340. retval = tmio_hw_init(dev, tmio);
  341. if (retval)
  342. goto err_hwinit;
  343. /* Set address of NAND IO lines */
  344. nand_chip->IO_ADDR_R = tmio->fcr;
  345. nand_chip->IO_ADDR_W = tmio->fcr;
  346. /* Set address of hardware control function */
  347. nand_chip->cmd_ctrl = tmio_nand_hwcontrol;
  348. nand_chip->dev_ready = tmio_nand_dev_ready;
  349. nand_chip->read_byte = tmio_nand_read_byte;
  350. nand_chip->write_buf = tmio_nand_write_buf;
  351. nand_chip->read_buf = tmio_nand_read_buf;
  352. nand_chip->verify_buf = tmio_nand_verify_buf;
  353. /* set eccmode using hardware ECC */
  354. nand_chip->ecc.mode = NAND_ECC_HW;
  355. nand_chip->ecc.size = 512;
  356. nand_chip->ecc.bytes = 6;
  357. nand_chip->ecc.hwctl = tmio_nand_enable_hwecc;
  358. nand_chip->ecc.calculate = tmio_nand_calculate_ecc;
  359. nand_chip->ecc.correct = tmio_nand_correct_data;
  360. if (data)
  361. nand_chip->badblock_pattern = data->badblock_pattern;
  362. /* 15 us command delay time */
  363. nand_chip->chip_delay = 15;
  364. retval = request_irq(irq, &tmio_irq,
  365. IRQF_DISABLED, dev_name(&dev->dev), tmio);
  366. if (retval) {
  367. dev_err(&dev->dev, "request_irq error %d\n", retval);
  368. goto err_irq;
  369. }
  370. tmio->irq = irq;
  371. nand_chip->waitfunc = tmio_nand_wait;
  372. /* Scan to find existence of the device */
  373. if (nand_scan(mtd, 1)) {
  374. retval = -ENODEV;
  375. goto err_scan;
  376. }
  377. /* Register the partitions */
  378. #ifdef CONFIG_MTD_PARTITIONS
  379. #ifdef CONFIG_MTD_CMDLINE_PARTS
  380. nbparts = parse_mtd_partitions(mtd, part_probes, &parts, 0);
  381. #endif
  382. if (nbparts <= 0 && data) {
  383. parts = data->partition;
  384. nbparts = data->num_partitions;
  385. }
  386. if (nbparts)
  387. retval = add_mtd_partitions(mtd, parts, nbparts);
  388. else
  389. #endif
  390. retval = add_mtd_device(mtd);
  391. if (!retval)
  392. return retval;
  393. nand_release(mtd);
  394. err_scan:
  395. if (tmio->irq)
  396. free_irq(tmio->irq, tmio);
  397. err_irq:
  398. tmio_hw_stop(dev, tmio);
  399. err_hwinit:
  400. iounmap(tmio->fcr);
  401. err_iomap_fcr:
  402. iounmap(tmio->ccr);
  403. err_iomap_ccr:
  404. kfree(tmio);
  405. err_kzalloc:
  406. return retval;
  407. }
  408. static int tmio_remove(struct platform_device *dev)
  409. {
  410. struct tmio_nand *tmio = platform_get_drvdata(dev);
  411. nand_release(&tmio->mtd);
  412. if (tmio->irq)
  413. free_irq(tmio->irq, tmio);
  414. tmio_hw_stop(dev, tmio);
  415. iounmap(tmio->fcr);
  416. iounmap(tmio->ccr);
  417. kfree(tmio);
  418. return 0;
  419. }
  420. #ifdef CONFIG_PM
  421. static int tmio_suspend(struct platform_device *dev, pm_message_t state)
  422. {
  423. struct mfd_cell *cell = dev_get_platdata(&dev->dev);
  424. if (cell->suspend)
  425. cell->suspend(dev);
  426. tmio_hw_stop(dev, platform_get_drvdata(dev));
  427. return 0;
  428. }
  429. static int tmio_resume(struct platform_device *dev)
  430. {
  431. struct mfd_cell *cell = dev_get_platdata(&dev->dev);
  432. /* FIXME - is this required or merely another attack of the broken
  433. * SHARP platform? Looks suspicious.
  434. */
  435. tmio_hw_init(dev, platform_get_drvdata(dev));
  436. if (cell->resume)
  437. cell->resume(dev);
  438. return 0;
  439. }
  440. #else
  441. #define tmio_suspend NULL
  442. #define tmio_resume NULL
  443. #endif
  444. static struct platform_driver tmio_driver = {
  445. .driver.name = "tmio-nand",
  446. .driver.owner = THIS_MODULE,
  447. .probe = tmio_probe,
  448. .remove = tmio_remove,
  449. .suspend = tmio_suspend,
  450. .resume = tmio_resume,
  451. };
  452. static int __init tmio_init(void)
  453. {
  454. return platform_driver_register(&tmio_driver);
  455. }
  456. static void __exit tmio_exit(void)
  457. {
  458. platform_driver_unregister(&tmio_driver);
  459. }
  460. module_init(tmio_init);
  461. module_exit(tmio_exit);
  462. MODULE_LICENSE("GPL v2");
  463. MODULE_AUTHOR("Ian Molton, Dirk Opfer, Chris Humbert, Dmitry Baryshkov");
  464. MODULE_DESCRIPTION("NAND flash driver on Toshiba Mobile IO controller");
  465. MODULE_ALIAS("platform:tmio-nand");