t7l66xb.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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/err.h>
  27. #include <linux/io.h>
  28. #include <linux/slab.h>
  29. #include <linux/irq.h>
  30. #include <linux/clk.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/mfd/core.h>
  33. #include <linux/mfd/tmio.h>
  34. #include <linux/mfd/t7l66xb.h>
  35. enum {
  36. T7L66XB_CELL_NAND,
  37. T7L66XB_CELL_MMC,
  38. };
  39. static const struct resource t7l66xb_mmc_resources[] = {
  40. {
  41. .start = 0x800,
  42. .end = 0x9ff,
  43. .flags = IORESOURCE_MEM,
  44. },
  45. {
  46. .start = IRQ_T7L66XB_MMC,
  47. .end = IRQ_T7L66XB_MMC,
  48. .flags = IORESOURCE_IRQ,
  49. },
  50. };
  51. #define SCR_REVID 0x08 /* b Revision ID */
  52. #define SCR_IMR 0x42 /* b Interrupt Mask */
  53. #define SCR_DEV_CTL 0xe0 /* b Device control */
  54. #define SCR_ISR 0xe1 /* b Interrupt Status */
  55. #define SCR_GPO_OC 0xf0 /* b GPO output control */
  56. #define SCR_GPO_OS 0xf1 /* b GPO output enable */
  57. #define SCR_GPI_S 0xf2 /* w GPI status */
  58. #define SCR_APDC 0xf8 /* b Active pullup down ctrl */
  59. #define SCR_DEV_CTL_USB BIT(0) /* USB enable */
  60. #define SCR_DEV_CTL_MMC BIT(1) /* MMC enable */
  61. /*--------------------------------------------------------------------------*/
  62. struct t7l66xb {
  63. void __iomem *scr;
  64. /* Lock to protect registers requiring read/modify/write ops. */
  65. spinlock_t lock;
  66. struct resource rscr;
  67. struct clk *clk48m;
  68. struct clk *clk32k;
  69. int irq;
  70. int irq_base;
  71. };
  72. /*--------------------------------------------------------------------------*/
  73. static int t7l66xb_mmc_enable(struct platform_device *mmc)
  74. {
  75. struct platform_device *dev = to_platform_device(mmc->dev.parent);
  76. struct t7l66xb *t7l66xb = platform_get_drvdata(dev);
  77. unsigned long flags;
  78. u8 dev_ctl;
  79. clk_prepare_enable(t7l66xb->clk32k);
  80. spin_lock_irqsave(&t7l66xb->lock, flags);
  81. dev_ctl = tmio_ioread8(t7l66xb->scr + SCR_DEV_CTL);
  82. dev_ctl |= SCR_DEV_CTL_MMC;
  83. tmio_iowrite8(dev_ctl, t7l66xb->scr + SCR_DEV_CTL);
  84. spin_unlock_irqrestore(&t7l66xb->lock, flags);
  85. tmio_core_mmc_enable(t7l66xb->scr + 0x200, 0,
  86. t7l66xb_mmc_resources[0].start & 0xfffe);
  87. return 0;
  88. }
  89. static int t7l66xb_mmc_disable(struct platform_device *mmc)
  90. {
  91. struct platform_device *dev = to_platform_device(mmc->dev.parent);
  92. struct t7l66xb *t7l66xb = platform_get_drvdata(dev);
  93. unsigned long flags;
  94. u8 dev_ctl;
  95. spin_lock_irqsave(&t7l66xb->lock, flags);
  96. dev_ctl = tmio_ioread8(t7l66xb->scr + SCR_DEV_CTL);
  97. dev_ctl &= ~SCR_DEV_CTL_MMC;
  98. tmio_iowrite8(dev_ctl, t7l66xb->scr + SCR_DEV_CTL);
  99. spin_unlock_irqrestore(&t7l66xb->lock, flags);
  100. clk_disable_unprepare(t7l66xb->clk32k);
  101. return 0;
  102. }
  103. static void t7l66xb_mmc_pwr(struct platform_device *mmc, int state)
  104. {
  105. struct platform_device *dev = to_platform_device(mmc->dev.parent);
  106. struct t7l66xb *t7l66xb = platform_get_drvdata(dev);
  107. tmio_core_mmc_pwr(t7l66xb->scr + 0x200, 0, state);
  108. }
  109. static void t7l66xb_mmc_clk_div(struct platform_device *mmc, int state)
  110. {
  111. struct platform_device *dev = to_platform_device(mmc->dev.parent);
  112. struct t7l66xb *t7l66xb = platform_get_drvdata(dev);
  113. tmio_core_mmc_clk_div(t7l66xb->scr + 0x200, 0, state);
  114. }
  115. /*--------------------------------------------------------------------------*/
  116. static struct tmio_mmc_data t7166xb_mmc_data = {
  117. .hclk = 24000000,
  118. .set_pwr = t7l66xb_mmc_pwr,
  119. .set_clk_div = t7l66xb_mmc_clk_div,
  120. };
  121. static const struct resource t7l66xb_nand_resources[] = {
  122. {
  123. .start = 0xc00,
  124. .end = 0xc07,
  125. .flags = IORESOURCE_MEM,
  126. },
  127. {
  128. .start = 0x0100,
  129. .end = 0x01ff,
  130. .flags = IORESOURCE_MEM,
  131. },
  132. {
  133. .start = IRQ_T7L66XB_NAND,
  134. .end = IRQ_T7L66XB_NAND,
  135. .flags = IORESOURCE_IRQ,
  136. },
  137. };
  138. static struct mfd_cell t7l66xb_cells[] = {
  139. [T7L66XB_CELL_MMC] = {
  140. .name = "tmio-mmc",
  141. .enable = t7l66xb_mmc_enable,
  142. .disable = t7l66xb_mmc_disable,
  143. .platform_data = &t7166xb_mmc_data,
  144. .pdata_size = sizeof(t7166xb_mmc_data),
  145. .num_resources = ARRAY_SIZE(t7l66xb_mmc_resources),
  146. .resources = t7l66xb_mmc_resources,
  147. },
  148. [T7L66XB_CELL_NAND] = {
  149. .name = "tmio-nand",
  150. .num_resources = ARRAY_SIZE(t7l66xb_nand_resources),
  151. .resources = t7l66xb_nand_resources,
  152. },
  153. };
  154. /*--------------------------------------------------------------------------*/
  155. /* Handle the T7L66XB interrupt mux */
  156. static void t7l66xb_irq(struct irq_desc *desc)
  157. {
  158. struct t7l66xb *t7l66xb = irq_desc_get_handler_data(desc);
  159. unsigned int isr;
  160. unsigned int i, irq_base;
  161. irq_base = t7l66xb->irq_base;
  162. while ((isr = tmio_ioread8(t7l66xb->scr + SCR_ISR) &
  163. ~tmio_ioread8(t7l66xb->scr + SCR_IMR)))
  164. for (i = 0; i < T7L66XB_NR_IRQS; i++)
  165. if (isr & (1 << i))
  166. generic_handle_irq(irq_base + i);
  167. }
  168. static void t7l66xb_irq_mask(struct irq_data *data)
  169. {
  170. struct t7l66xb *t7l66xb = irq_data_get_irq_chip_data(data);
  171. unsigned long flags;
  172. u8 imr;
  173. spin_lock_irqsave(&t7l66xb->lock, flags);
  174. imr = tmio_ioread8(t7l66xb->scr + SCR_IMR);
  175. imr |= 1 << (data->irq - t7l66xb->irq_base);
  176. tmio_iowrite8(imr, t7l66xb->scr + SCR_IMR);
  177. spin_unlock_irqrestore(&t7l66xb->lock, flags);
  178. }
  179. static void t7l66xb_irq_unmask(struct irq_data *data)
  180. {
  181. struct t7l66xb *t7l66xb = irq_data_get_irq_chip_data(data);
  182. unsigned long flags;
  183. u8 imr;
  184. spin_lock_irqsave(&t7l66xb->lock, flags);
  185. imr = tmio_ioread8(t7l66xb->scr + SCR_IMR);
  186. imr &= ~(1 << (data->irq - t7l66xb->irq_base));
  187. tmio_iowrite8(imr, t7l66xb->scr + SCR_IMR);
  188. spin_unlock_irqrestore(&t7l66xb->lock, flags);
  189. }
  190. static struct irq_chip t7l66xb_chip = {
  191. .name = "t7l66xb",
  192. .irq_ack = t7l66xb_irq_mask,
  193. .irq_mask = t7l66xb_irq_mask,
  194. .irq_unmask = t7l66xb_irq_unmask,
  195. };
  196. /*--------------------------------------------------------------------------*/
  197. /* Install the IRQ handler */
  198. static void t7l66xb_attach_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. for (irq = irq_base; irq < irq_base + T7L66XB_NR_IRQS; irq++) {
  204. irq_set_chip_and_handler(irq, &t7l66xb_chip, handle_level_irq);
  205. irq_set_chip_data(irq, t7l66xb);
  206. }
  207. irq_set_irq_type(t7l66xb->irq, IRQ_TYPE_EDGE_FALLING);
  208. irq_set_chained_handler_and_data(t7l66xb->irq, t7l66xb_irq, t7l66xb);
  209. }
  210. static void t7l66xb_detach_irq(struct platform_device *dev)
  211. {
  212. struct t7l66xb *t7l66xb = platform_get_drvdata(dev);
  213. unsigned int irq, irq_base;
  214. irq_base = t7l66xb->irq_base;
  215. irq_set_chained_handler_and_data(t7l66xb->irq, NULL, NULL);
  216. for (irq = irq_base; irq < irq_base + T7L66XB_NR_IRQS; irq++) {
  217. irq_set_chip(irq, NULL);
  218. irq_set_chip_data(irq, NULL);
  219. }
  220. }
  221. /*--------------------------------------------------------------------------*/
  222. #ifdef CONFIG_PM
  223. static int t7l66xb_suspend(struct platform_device *dev, pm_message_t state)
  224. {
  225. struct t7l66xb *t7l66xb = platform_get_drvdata(dev);
  226. struct t7l66xb_platform_data *pdata = dev_get_platdata(&dev->dev);
  227. if (pdata && pdata->suspend)
  228. pdata->suspend(dev);
  229. clk_disable_unprepare(t7l66xb->clk48m);
  230. return 0;
  231. }
  232. static int t7l66xb_resume(struct platform_device *dev)
  233. {
  234. struct t7l66xb *t7l66xb = platform_get_drvdata(dev);
  235. struct t7l66xb_platform_data *pdata = dev_get_platdata(&dev->dev);
  236. clk_prepare_enable(t7l66xb->clk48m);
  237. if (pdata && pdata->resume)
  238. pdata->resume(dev);
  239. tmio_core_mmc_enable(t7l66xb->scr + 0x200, 0,
  240. t7l66xb_mmc_resources[0].start & 0xfffe);
  241. return 0;
  242. }
  243. #else
  244. #define t7l66xb_suspend NULL
  245. #define t7l66xb_resume NULL
  246. #endif
  247. /*--------------------------------------------------------------------------*/
  248. static int t7l66xb_probe(struct platform_device *dev)
  249. {
  250. struct t7l66xb_platform_data *pdata = dev_get_platdata(&dev->dev);
  251. struct t7l66xb *t7l66xb;
  252. struct resource *iomem, *rscr;
  253. int ret;
  254. if (!pdata)
  255. return -EINVAL;
  256. iomem = platform_get_resource(dev, IORESOURCE_MEM, 0);
  257. if (!iomem)
  258. return -EINVAL;
  259. t7l66xb = kzalloc(sizeof *t7l66xb, GFP_KERNEL);
  260. if (!t7l66xb)
  261. return -ENOMEM;
  262. spin_lock_init(&t7l66xb->lock);
  263. platform_set_drvdata(dev, t7l66xb);
  264. ret = platform_get_irq(dev, 0);
  265. if (ret >= 0)
  266. t7l66xb->irq = ret;
  267. else
  268. goto err_noirq;
  269. t7l66xb->irq_base = pdata->irq_base;
  270. t7l66xb->clk32k = clk_get(&dev->dev, "CLK_CK32K");
  271. if (IS_ERR(t7l66xb->clk32k)) {
  272. ret = PTR_ERR(t7l66xb->clk32k);
  273. goto err_clk32k_get;
  274. }
  275. t7l66xb->clk48m = clk_get(&dev->dev, "CLK_CK48M");
  276. if (IS_ERR(t7l66xb->clk48m)) {
  277. ret = PTR_ERR(t7l66xb->clk48m);
  278. goto err_clk48m_get;
  279. }
  280. rscr = &t7l66xb->rscr;
  281. rscr->name = "t7l66xb-core";
  282. rscr->start = iomem->start;
  283. rscr->end = iomem->start + 0xff;
  284. rscr->flags = IORESOURCE_MEM;
  285. ret = request_resource(iomem, rscr);
  286. if (ret)
  287. goto err_request_scr;
  288. t7l66xb->scr = ioremap(rscr->start, resource_size(rscr));
  289. if (!t7l66xb->scr) {
  290. ret = -ENOMEM;
  291. goto err_ioremap;
  292. }
  293. clk_prepare_enable(t7l66xb->clk48m);
  294. if (pdata->enable)
  295. pdata->enable(dev);
  296. /* Mask all interrupts */
  297. tmio_iowrite8(0xbf, t7l66xb->scr + SCR_IMR);
  298. printk(KERN_INFO "%s rev %d @ 0x%08lx, irq %d\n",
  299. dev->name, tmio_ioread8(t7l66xb->scr + SCR_REVID),
  300. (unsigned long)iomem->start, t7l66xb->irq);
  301. t7l66xb_attach_irq(dev);
  302. t7l66xb_cells[T7L66XB_CELL_NAND].platform_data = pdata->nand_data;
  303. t7l66xb_cells[T7L66XB_CELL_NAND].pdata_size = sizeof(*pdata->nand_data);
  304. ret = mfd_add_devices(&dev->dev, dev->id,
  305. t7l66xb_cells, ARRAY_SIZE(t7l66xb_cells),
  306. iomem, t7l66xb->irq_base, NULL);
  307. if (!ret)
  308. return 0;
  309. t7l66xb_detach_irq(dev);
  310. iounmap(t7l66xb->scr);
  311. err_ioremap:
  312. release_resource(&t7l66xb->rscr);
  313. err_request_scr:
  314. clk_put(t7l66xb->clk48m);
  315. err_clk48m_get:
  316. clk_put(t7l66xb->clk32k);
  317. err_clk32k_get:
  318. err_noirq:
  319. kfree(t7l66xb);
  320. return ret;
  321. }
  322. static int t7l66xb_remove(struct platform_device *dev)
  323. {
  324. struct t7l66xb_platform_data *pdata = dev_get_platdata(&dev->dev);
  325. struct t7l66xb *t7l66xb = platform_get_drvdata(dev);
  326. int ret;
  327. ret = pdata->disable(dev);
  328. clk_disable_unprepare(t7l66xb->clk48m);
  329. clk_put(t7l66xb->clk48m);
  330. clk_disable_unprepare(t7l66xb->clk32k);
  331. clk_put(t7l66xb->clk32k);
  332. t7l66xb_detach_irq(dev);
  333. iounmap(t7l66xb->scr);
  334. release_resource(&t7l66xb->rscr);
  335. mfd_remove_devices(&dev->dev);
  336. kfree(t7l66xb);
  337. return ret;
  338. }
  339. static struct platform_driver t7l66xb_platform_driver = {
  340. .driver = {
  341. .name = "t7l66xb",
  342. },
  343. .suspend = t7l66xb_suspend,
  344. .resume = t7l66xb_resume,
  345. .probe = t7l66xb_probe,
  346. .remove = t7l66xb_remove,
  347. };
  348. /*--------------------------------------------------------------------------*/
  349. module_platform_driver(t7l66xb_platform_driver);
  350. MODULE_DESCRIPTION("Toshiba T7L66XB core driver");
  351. MODULE_LICENSE("GPL v2");
  352. MODULE_AUTHOR("Ian Molton");
  353. MODULE_ALIAS("platform:t7l66xb");