tmio_nand.c 15 KB

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