t7l66xb.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /*
  2. *
  3. * Toshiba T7L66XB core mfd support
  4. *
  5. * Copyright (c) 2005, 2007, 2008 Ian Molton
  6. * Copyright (c) 2008 Dmitry Baryshkov
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * T7L66 features:
  13. *
  14. * Supported in this driver:
  15. * SD/MMC
  16. * SM/NAND flash controller
  17. *
  18. * As yet not supported
  19. * GPIO interface (on NAND pins)
  20. * Serial interface
  21. * TFT 'interface converter'
  22. * PCMCIA interface logic
  23. */
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/io.h>
  27. #include <linux/irq.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/mfd/core.h>
  30. #include <linux/mfd/tmio.h>
  31. #include <linux/mfd/t7l66xb.h>
  32. enum {
  33. T7L66XB_CELL_NAND,
  34. T7L66XB_CELL_MMC,
  35. };
  36. #define SCR_REVID 0x08 /* b Revision ID */
  37. #define SCR_IMR 0x42 /* b Interrupt Mask */
  38. #define SCR_DEV_CTL 0xe0 /* b Device control */
  39. #define SCR_ISR 0xe1 /* b Interrupt Status */
  40. #define SCR_GPO_OC 0xf0 /* b GPO output control */
  41. #define SCR_GPO_OS 0xf1 /* b GPO output enable */
  42. #define SCR_GPI_S 0xf2 /* w GPI status */
  43. #define SCR_APDC 0xf8 /* b Active pullup down ctrl */
  44. #define SCR_DEV_CTL_USB BIT(0) /* USB enable */
  45. #define SCR_DEV_CTL_MMC BIT(1) /* MMC enable */
  46. /*--------------------------------------------------------------------------*/
  47. struct t7l66xb {
  48. void __iomem *scr;
  49. /* Lock to protect registers requiring read/modify/write ops. */
  50. spinlock_t lock;
  51. struct resource rscr;
  52. int irq;
  53. int irq_base;
  54. };
  55. /*--------------------------------------------------------------------------*/
  56. static int t7l66xb_mmc_enable(struct platform_device *mmc)
  57. {
  58. struct platform_device *dev = to_platform_device(mmc->dev.parent);
  59. struct t7l66xb_platform_data *pdata = dev->dev.platform_data;
  60. struct t7l66xb *t7l66xb = platform_get_drvdata(dev);
  61. unsigned long flags;
  62. u8 dev_ctl;
  63. if (pdata->enable_clk32k)
  64. pdata->enable_clk32k(dev);
  65. spin_lock_irqsave(&t7l66xb->lock, flags);
  66. dev_ctl = tmio_ioread8(t7l66xb->scr + SCR_DEV_CTL);
  67. dev_ctl |= SCR_DEV_CTL_MMC;
  68. tmio_iowrite8(dev_ctl, t7l66xb->scr + SCR_DEV_CTL);
  69. spin_unlock_irqrestore(&t7l66xb->lock, flags);
  70. return 0;
  71. }
  72. static int t7l66xb_mmc_disable(struct platform_device *mmc)
  73. {
  74. struct platform_device *dev = to_platform_device(mmc->dev.parent);
  75. struct t7l66xb_platform_data *pdata = dev->dev.platform_data;
  76. struct t7l66xb *t7l66xb = platform_get_drvdata(dev);
  77. unsigned long flags;
  78. u8 dev_ctl;
  79. spin_lock_irqsave(&t7l66xb->lock, flags);
  80. dev_ctl = tmio_ioread8(t7l66xb->scr + SCR_DEV_CTL);
  81. dev_ctl &= ~SCR_DEV_CTL_MMC;
  82. tmio_iowrite8(dev_ctl, t7l66xb->scr + SCR_DEV_CTL);
  83. spin_unlock_irqrestore(&t7l66xb->lock, flags);
  84. if (pdata->disable_clk32k)
  85. pdata->disable_clk32k(dev);
  86. return 0;
  87. }
  88. /*--------------------------------------------------------------------------*/
  89. const static struct resource t7l66xb_mmc_resources[] = {
  90. {
  91. .start = 0x800,
  92. .end = 0x9ff,
  93. .flags = IORESOURCE_MEM,
  94. },
  95. {
  96. .start = 0x200,
  97. .end = 0x2ff,
  98. .flags = IORESOURCE_MEM,
  99. },
  100. {
  101. .start = IRQ_T7L66XB_MMC,
  102. .end = IRQ_T7L66XB_MMC,
  103. .flags = IORESOURCE_IRQ,
  104. },
  105. };
  106. const static struct resource t7l66xb_nand_resources[] = {
  107. {
  108. .start = 0xc00,
  109. .end = 0xc07,
  110. .flags = IORESOURCE_MEM,
  111. },
  112. {
  113. .start = 0x0100,
  114. .end = 0x01ff,
  115. .flags = IORESOURCE_MEM,
  116. },
  117. {
  118. .start = IRQ_T7L66XB_NAND,
  119. .end = IRQ_T7L66XB_NAND,
  120. .flags = IORESOURCE_IRQ,
  121. },
  122. };
  123. static struct mfd_cell t7l66xb_cells[] = {
  124. [T7L66XB_CELL_MMC] = {
  125. .name = "tmio-mmc",
  126. .enable = t7l66xb_mmc_enable,
  127. .disable = t7l66xb_mmc_disable,
  128. .num_resources = ARRAY_SIZE(t7l66xb_mmc_resources),
  129. .resources = t7l66xb_mmc_resources,
  130. },
  131. [T7L66XB_CELL_NAND] = {
  132. .name = "tmio-nand",
  133. .num_resources = ARRAY_SIZE(t7l66xb_nand_resources),
  134. .resources = t7l66xb_nand_resources,
  135. },
  136. };
  137. /*--------------------------------------------------------------------------*/
  138. /* Handle the T7L66XB interrupt mux */
  139. static void t7l66xb_irq(unsigned int irq, struct irq_desc *desc)
  140. {
  141. struct t7l66xb *t7l66xb = get_irq_data(irq);
  142. unsigned int isr;
  143. unsigned int i, irq_base;
  144. irq_base = t7l66xb->irq_base;
  145. while ((isr = tmio_ioread8(t7l66xb->scr + SCR_ISR) &
  146. ~tmio_ioread8(t7l66xb->scr + SCR_IMR)))
  147. for (i = 0; i < T7L66XB_NR_IRQS; i++)
  148. if (isr & (1 << i))
  149. generic_handle_irq(irq_base + i);
  150. }
  151. static void t7l66xb_irq_mask(unsigned int irq)
  152. {
  153. struct t7l66xb *t7l66xb = get_irq_chip_data(irq);
  154. unsigned long flags;
  155. u8 imr;
  156. spin_lock_irqsave(&t7l66xb->lock, flags);
  157. imr = tmio_ioread8(t7l66xb->scr + SCR_IMR);
  158. imr |= 1 << (irq - t7l66xb->irq_base);
  159. tmio_iowrite8(imr, t7l66xb->scr + SCR_IMR);
  160. spin_unlock_irqrestore(&t7l66xb->lock, flags);
  161. }
  162. static void t7l66xb_irq_unmask(unsigned int irq)
  163. {
  164. struct t7l66xb *t7l66xb = get_irq_chip_data(irq);
  165. unsigned long flags;
  166. u8 imr;
  167. spin_lock_irqsave(&t7l66xb->lock, flags);
  168. imr = tmio_ioread8(t7l66xb->scr + SCR_IMR);
  169. imr &= ~(1 << (irq - t7l66xb->irq_base));
  170. tmio_iowrite8(imr, t7l66xb->scr + SCR_IMR);
  171. spin_unlock_irqrestore(&t7l66xb->lock, flags);
  172. }
  173. static struct irq_chip t7l66xb_chip = {
  174. .name = "t7l66xb",
  175. .ack = t7l66xb_irq_mask,
  176. .mask = t7l66xb_irq_mask,
  177. .unmask = t7l66xb_irq_unmask,
  178. };
  179. /*--------------------------------------------------------------------------*/
  180. /* Install the IRQ handler */
  181. static void t7l66xb_attach_irq(struct platform_device *dev)
  182. {
  183. struct t7l66xb *t7l66xb = platform_get_drvdata(dev);
  184. unsigned int irq, irq_base;
  185. irq_base = t7l66xb->irq_base;
  186. for (irq = irq_base; irq < irq_base + T7L66XB_NR_IRQS; irq++) {
  187. set_irq_chip(irq, &t7l66xb_chip);
  188. set_irq_chip_data(irq, t7l66xb);
  189. set_irq_handler(irq, handle_level_irq);
  190. #ifdef CONFIG_ARM
  191. set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
  192. #endif
  193. }
  194. set_irq_type(t7l66xb->irq, IRQ_TYPE_EDGE_FALLING);
  195. set_irq_data(t7l66xb->irq, t7l66xb);
  196. set_irq_chained_handler(t7l66xb->irq, t7l66xb_irq);
  197. }
  198. static void t7l66xb_detach_irq(struct platform_device *dev)
  199. {
  200. struct t7l66xb *t7l66xb = platform_get_drvdata(dev);
  201. unsigned int irq, irq_base;
  202. irq_base = t7l66xb->irq_base;
  203. set_irq_chained_handler(t7l66xb->irq, NULL);
  204. set_irq_data(t7l66xb->irq, NULL);
  205. for (irq = irq_base; irq < irq_base + T7L66XB_NR_IRQS; irq++) {
  206. #ifdef CONFIG_ARM
  207. set_irq_flags(irq, 0);
  208. #endif
  209. set_irq_chip(irq, NULL);
  210. set_irq_chip_data(irq, NULL);
  211. }
  212. }
  213. /*--------------------------------------------------------------------------*/
  214. #ifdef CONFIG_PM
  215. static int t7l66xb_suspend(struct platform_device *dev, pm_message_t state)
  216. {
  217. struct t7l66xb_platform_data *pdata = dev->dev.platform_data;
  218. if (pdata && pdata->suspend)
  219. pdata->suspend(dev);
  220. return 0;
  221. }
  222. static int t7l66xb_resume(struct platform_device *dev)
  223. {
  224. struct t7l66xb_platform_data *pdata = dev->dev.platform_data;
  225. if (pdata && pdata->resume)
  226. pdata->resume(dev);
  227. return 0;
  228. }
  229. #else
  230. #define t7l66xb_suspend NULL
  231. #define t7l66xb_resume NULL
  232. #endif
  233. /*--------------------------------------------------------------------------*/
  234. static int t7l66xb_probe(struct platform_device *dev)
  235. {
  236. struct t7l66xb_platform_data *pdata = dev->dev.platform_data;
  237. struct t7l66xb *t7l66xb;
  238. struct resource *iomem, *rscr;
  239. int ret;
  240. iomem = platform_get_resource(dev, IORESOURCE_MEM, 0);
  241. if (!iomem)
  242. return -EINVAL;
  243. t7l66xb = kzalloc(sizeof *t7l66xb, GFP_KERNEL);
  244. if (!t7l66xb)
  245. return -ENOMEM;
  246. spin_lock_init(&t7l66xb->lock);
  247. platform_set_drvdata(dev, t7l66xb);
  248. ret = platform_get_irq(dev, 0);
  249. if (ret >= 0)
  250. t7l66xb->irq = ret;
  251. else
  252. goto err_noirq;
  253. t7l66xb->irq_base = pdata->irq_base;
  254. rscr = &t7l66xb->rscr;
  255. rscr->name = "t7l66xb-core";
  256. rscr->start = iomem->start;
  257. rscr->end = iomem->start + 0xff;
  258. rscr->flags = IORESOURCE_MEM;
  259. ret = request_resource(iomem, rscr);
  260. if (ret)
  261. goto err_request_scr;
  262. t7l66xb->scr = ioremap(rscr->start, rscr->end - rscr->start + 1);
  263. if (!t7l66xb->scr) {
  264. ret = -ENOMEM;
  265. goto err_ioremap;
  266. }
  267. if (pdata && pdata->enable)
  268. pdata->enable(dev);
  269. /* Mask all interrupts */
  270. tmio_iowrite8(0xbf, t7l66xb->scr + SCR_IMR);
  271. printk(KERN_INFO "%s rev %d @ 0x%08lx, irq %d\n",
  272. dev->name, tmio_ioread8(t7l66xb->scr + SCR_REVID),
  273. (unsigned long)iomem->start, t7l66xb->irq);
  274. t7l66xb_attach_irq(dev);
  275. t7l66xb_cells[T7L66XB_CELL_NAND].driver_data = pdata->nand_data;
  276. t7l66xb_cells[T7L66XB_CELL_NAND].platform_data =
  277. &t7l66xb_cells[T7L66XB_CELL_NAND];
  278. t7l66xb_cells[T7L66XB_CELL_NAND].data_size =
  279. sizeof(t7l66xb_cells[T7L66XB_CELL_NAND]);
  280. t7l66xb_cells[T7L66XB_CELL_MMC].platform_data =
  281. &t7l66xb_cells[T7L66XB_CELL_MMC];
  282. t7l66xb_cells[T7L66XB_CELL_MMC].data_size =
  283. sizeof(t7l66xb_cells[T7L66XB_CELL_MMC]);
  284. ret = mfd_add_devices(&dev->dev, dev->id,
  285. t7l66xb_cells, ARRAY_SIZE(t7l66xb_cells),
  286. iomem, t7l66xb->irq_base);
  287. if (!ret)
  288. return 0;
  289. t7l66xb_detach_irq(dev);
  290. iounmap(t7l66xb->scr);
  291. err_ioremap:
  292. release_resource(&t7l66xb->rscr);
  293. err_noirq:
  294. err_request_scr:
  295. kfree(t7l66xb);
  296. return ret;
  297. }
  298. static int t7l66xb_remove(struct platform_device *dev)
  299. {
  300. struct t7l66xb_platform_data *pdata = dev->dev.platform_data;
  301. struct t7l66xb *t7l66xb = platform_get_drvdata(dev);
  302. int ret;
  303. ret = pdata->disable(dev);
  304. t7l66xb_detach_irq(dev);
  305. iounmap(t7l66xb->scr);
  306. release_resource(&t7l66xb->rscr);
  307. mfd_remove_devices(&dev->dev);
  308. platform_set_drvdata(dev, NULL);
  309. kfree(t7l66xb);
  310. return ret;
  311. }
  312. static struct platform_driver t7l66xb_platform_driver = {
  313. .driver = {
  314. .name = "t7l66xb",
  315. .owner = THIS_MODULE,
  316. },
  317. .suspend = t7l66xb_suspend,
  318. .resume = t7l66xb_resume,
  319. .probe = t7l66xb_probe,
  320. .remove = t7l66xb_remove,
  321. };
  322. /*--------------------------------------------------------------------------*/
  323. static int __init t7l66xb_init(void)
  324. {
  325. int retval = 0;
  326. retval = platform_driver_register(&t7l66xb_platform_driver);
  327. return retval;
  328. }
  329. static void __exit t7l66xb_exit(void)
  330. {
  331. platform_driver_unregister(&t7l66xb_platform_driver);
  332. }
  333. module_init(t7l66xb_init);
  334. module_exit(t7l66xb_exit);
  335. MODULE_DESCRIPTION("Toshiba T7L66XB core driver");
  336. MODULE_LICENSE("GPL v2");
  337. MODULE_AUTHOR("Ian Molton");
  338. MODULE_ALIAS("platform:t7l66xb");