ohci-tmio.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. * OHCI HCD(Host Controller Driver) for USB.
  3. *
  4. *(C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
  5. *(C) Copyright 2000-2002 David Brownell <dbrownell@users.sourceforge.net>
  6. *(C) Copyright 2002 Hewlett-Packard Company
  7. *
  8. * Bus glue for Toshiba Mobile IO(TMIO) Controller's OHCI core
  9. * (C) Copyright 2005 Chris Humbert <mahadri-usb@drigon.com>
  10. * (C) Copyright 2007, 2008 Dmitry Baryshkov <dbaryshkov@gmail.com>
  11. *
  12. * This is known to work with the following variants:
  13. * TC6393XB revision 3 (32kB SRAM)
  14. *
  15. * The TMIO's OHCI core DMAs through a small internal buffer that
  16. * is directly addressable by the CPU.
  17. *
  18. * Written from sparse documentation from Toshiba and Sharp's driver
  19. * for the 2.4 kernel,
  20. * usb-ohci-tc6393.c(C) Copyright 2004 Lineo Solutions, Inc.
  21. *
  22. * This program is free software; you can redistribute it and/or modify
  23. * it under the terms of the GNU General Public License version 2 as
  24. * published by the Free Software Foundation.
  25. */
  26. /*#include <linux/fs.h>
  27. #include <linux/mount.h>
  28. #include <linux/pagemap.h>
  29. #include <linux/namei.h>
  30. #include <linux/sched.h>*/
  31. #include <linux/platform_device.h>
  32. #include <linux/mfd/core.h>
  33. #include <linux/mfd/tmio.h>
  34. #include <linux/dma-mapping.h>
  35. /*-------------------------------------------------------------------------*/
  36. /*
  37. * USB Host Controller Configuration Register
  38. */
  39. #define CCR_REVID 0x08 /* b Revision ID */
  40. #define CCR_BASE 0x10 /* l USB Control Register Base Address Low */
  41. #define CCR_ILME 0x40 /* b Internal Local Memory Enable */
  42. #define CCR_PM 0x4c /* w Power Management */
  43. #define CCR_INTC 0x50 /* b INT Control */
  44. #define CCR_LMW1L 0x54 /* w Local Memory Window 1 LMADRS Low */
  45. #define CCR_LMW1H 0x56 /* w Local Memory Window 1 LMADRS High */
  46. #define CCR_LMW1BL 0x58 /* w Local Memory Window 1 Base Address Low */
  47. #define CCR_LMW1BH 0x5A /* w Local Memory Window 1 Base Address High */
  48. #define CCR_LMW2L 0x5C /* w Local Memory Window 2 LMADRS Low */
  49. #define CCR_LMW2H 0x5E /* w Local Memory Window 2 LMADRS High */
  50. #define CCR_LMW2BL 0x60 /* w Local Memory Window 2 Base Address Low */
  51. #define CCR_LMW2BH 0x62 /* w Local Memory Window 2 Base Address High */
  52. #define CCR_MISC 0xFC /* b MISC */
  53. #define CCR_PM_GKEN 0x0001
  54. #define CCR_PM_CKRNEN 0x0002
  55. #define CCR_PM_USBPW1 0x0004
  56. #define CCR_PM_USBPW2 0x0008
  57. #define CCR_PM_USBPW3 0x0010
  58. #define CCR_PM_PMEE 0x0100
  59. #define CCR_PM_PMES 0x8000
  60. /*-------------------------------------------------------------------------*/
  61. struct tmio_hcd {
  62. void __iomem *ccr;
  63. spinlock_t lock; /* protects RMW cycles */
  64. };
  65. #define hcd_to_tmio(hcd) ((struct tmio_hcd *)(hcd_to_ohci(hcd) + 1))
  66. /*-------------------------------------------------------------------------*/
  67. static void tmio_write_pm(struct platform_device *dev)
  68. {
  69. struct usb_hcd *hcd = platform_get_drvdata(dev);
  70. struct tmio_hcd *tmio = hcd_to_tmio(hcd);
  71. u16 pm;
  72. unsigned long flags;
  73. spin_lock_irqsave(&tmio->lock, flags);
  74. pm = CCR_PM_GKEN | CCR_PM_CKRNEN |
  75. CCR_PM_PMEE | CCR_PM_PMES;
  76. tmio_iowrite16(pm, tmio->ccr + CCR_PM);
  77. spin_unlock_irqrestore(&tmio->lock, flags);
  78. }
  79. static void tmio_stop_hc(struct platform_device *dev)
  80. {
  81. struct usb_hcd *hcd = platform_get_drvdata(dev);
  82. struct ohci_hcd *ohci = hcd_to_ohci(hcd);
  83. struct tmio_hcd *tmio = hcd_to_tmio(hcd);
  84. u16 pm;
  85. pm = CCR_PM_GKEN | CCR_PM_CKRNEN;
  86. switch (ohci->num_ports) {
  87. default:
  88. dev_err(&dev->dev, "Unsupported amount of ports: %d\n", ohci->num_ports);
  89. case 3:
  90. pm |= CCR_PM_USBPW3;
  91. case 2:
  92. pm |= CCR_PM_USBPW2;
  93. case 1:
  94. pm |= CCR_PM_USBPW1;
  95. }
  96. tmio_iowrite8(0, tmio->ccr + CCR_INTC);
  97. tmio_iowrite8(0, tmio->ccr + CCR_ILME);
  98. tmio_iowrite16(0, tmio->ccr + CCR_BASE);
  99. tmio_iowrite16(0, tmio->ccr + CCR_BASE + 2);
  100. tmio_iowrite16(pm, tmio->ccr + CCR_PM);
  101. }
  102. static void tmio_start_hc(struct platform_device *dev)
  103. {
  104. struct usb_hcd *hcd = platform_get_drvdata(dev);
  105. struct tmio_hcd *tmio = hcd_to_tmio(hcd);
  106. unsigned long base = hcd->rsrc_start;
  107. tmio_write_pm(dev);
  108. tmio_iowrite16(base, tmio->ccr + CCR_BASE);
  109. tmio_iowrite16(base >> 16, tmio->ccr + CCR_BASE + 2);
  110. tmio_iowrite8(1, tmio->ccr + CCR_ILME);
  111. tmio_iowrite8(2, tmio->ccr + CCR_INTC);
  112. dev_info(&dev->dev, "revision %d @ 0x%08llx, irq %d\n",
  113. tmio_ioread8(tmio->ccr + CCR_REVID),
  114. (u64) hcd->rsrc_start, hcd->irq);
  115. }
  116. static int ohci_tmio_start(struct usb_hcd *hcd)
  117. {
  118. struct ohci_hcd *ohci = hcd_to_ohci(hcd);
  119. int ret;
  120. if ((ret = ohci_init(ohci)) < 0)
  121. return ret;
  122. if ((ret = ohci_run(ohci)) < 0) {
  123. dev_err(hcd->self.controller, "can't start %s\n",
  124. hcd->self.bus_name);
  125. ohci_stop(hcd);
  126. return ret;
  127. }
  128. return 0;
  129. }
  130. static const struct hc_driver ohci_tmio_hc_driver = {
  131. .description = hcd_name,
  132. .product_desc = "TMIO OHCI USB Host Controller",
  133. .hcd_priv_size = sizeof(struct ohci_hcd) + sizeof (struct tmio_hcd),
  134. /* generic hardware linkage */
  135. .irq = ohci_irq,
  136. .flags = HCD_USB11 | HCD_MEMORY | HCD_LOCAL_MEM,
  137. /* basic lifecycle operations */
  138. .start = ohci_tmio_start,
  139. .stop = ohci_stop,
  140. .shutdown = ohci_shutdown,
  141. /* managing i/o requests and associated device resources */
  142. .urb_enqueue = ohci_urb_enqueue,
  143. .urb_dequeue = ohci_urb_dequeue,
  144. .endpoint_disable = ohci_endpoint_disable,
  145. /* scheduling support */
  146. .get_frame_number = ohci_get_frame,
  147. /* root hub support */
  148. .hub_status_data = ohci_hub_status_data,
  149. .hub_control = ohci_hub_control,
  150. #ifdef CONFIG_PM
  151. .bus_suspend = ohci_bus_suspend,
  152. .bus_resume = ohci_bus_resume,
  153. #endif
  154. .start_port_reset = ohci_start_port_reset,
  155. };
  156. /*-------------------------------------------------------------------------*/
  157. static struct platform_driver ohci_hcd_tmio_driver;
  158. static int ohci_hcd_tmio_drv_probe(struct platform_device *dev)
  159. {
  160. const struct mfd_cell *cell = mfd_get_cell(dev);
  161. struct resource *regs = platform_get_resource(dev, IORESOURCE_MEM, 0);
  162. struct resource *config = platform_get_resource(dev, IORESOURCE_MEM, 1);
  163. struct resource *sram = platform_get_resource(dev, IORESOURCE_MEM, 2);
  164. int irq = platform_get_irq(dev, 0);
  165. struct tmio_hcd *tmio;
  166. struct ohci_hcd *ohci;
  167. struct usb_hcd *hcd;
  168. int ret;
  169. if (usb_disabled())
  170. return -ENODEV;
  171. if (!cell)
  172. return -EINVAL;
  173. hcd = usb_create_hcd(&ohci_tmio_hc_driver, &dev->dev, dev_name(&dev->dev));
  174. if (!hcd) {
  175. ret = -ENOMEM;
  176. goto err_usb_create_hcd;
  177. }
  178. hcd->rsrc_start = regs->start;
  179. hcd->rsrc_len = resource_size(regs);
  180. tmio = hcd_to_tmio(hcd);
  181. spin_lock_init(&tmio->lock);
  182. tmio->ccr = ioremap(config->start, resource_size(config));
  183. if (!tmio->ccr) {
  184. ret = -ENOMEM;
  185. goto err_ioremap_ccr;
  186. }
  187. hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
  188. if (!hcd->regs) {
  189. ret = -ENOMEM;
  190. goto err_ioremap_regs;
  191. }
  192. if (!dma_declare_coherent_memory(&dev->dev, sram->start,
  193. sram->start,
  194. resource_size(sram),
  195. DMA_MEMORY_MAP | DMA_MEMORY_EXCLUSIVE)) {
  196. ret = -EBUSY;
  197. goto err_dma_declare;
  198. }
  199. if (cell->enable) {
  200. ret = cell->enable(dev);
  201. if (ret)
  202. goto err_enable;
  203. }
  204. tmio_start_hc(dev);
  205. ohci = hcd_to_ohci(hcd);
  206. ohci_hcd_init(ohci);
  207. ret = usb_add_hcd(hcd, irq, 0);
  208. if (ret)
  209. goto err_add_hcd;
  210. device_wakeup_enable(hcd->self.controller);
  211. if (ret == 0)
  212. return ret;
  213. usb_remove_hcd(hcd);
  214. err_add_hcd:
  215. tmio_stop_hc(dev);
  216. if (cell->disable)
  217. cell->disable(dev);
  218. err_enable:
  219. dma_release_declared_memory(&dev->dev);
  220. err_dma_declare:
  221. iounmap(hcd->regs);
  222. err_ioremap_regs:
  223. iounmap(tmio->ccr);
  224. err_ioremap_ccr:
  225. usb_put_hcd(hcd);
  226. err_usb_create_hcd:
  227. return ret;
  228. }
  229. static int ohci_hcd_tmio_drv_remove(struct platform_device *dev)
  230. {
  231. struct usb_hcd *hcd = platform_get_drvdata(dev);
  232. struct tmio_hcd *tmio = hcd_to_tmio(hcd);
  233. const struct mfd_cell *cell = mfd_get_cell(dev);
  234. usb_remove_hcd(hcd);
  235. tmio_stop_hc(dev);
  236. if (cell->disable)
  237. cell->disable(dev);
  238. dma_release_declared_memory(&dev->dev);
  239. iounmap(hcd->regs);
  240. iounmap(tmio->ccr);
  241. usb_put_hcd(hcd);
  242. return 0;
  243. }
  244. #ifdef CONFIG_PM
  245. static int ohci_hcd_tmio_drv_suspend(struct platform_device *dev, pm_message_t state)
  246. {
  247. const struct mfd_cell *cell = mfd_get_cell(dev);
  248. struct usb_hcd *hcd = platform_get_drvdata(dev);
  249. struct ohci_hcd *ohci = hcd_to_ohci(hcd);
  250. struct tmio_hcd *tmio = hcd_to_tmio(hcd);
  251. unsigned long flags;
  252. u8 misc;
  253. int ret;
  254. if (time_before(jiffies, ohci->next_statechange))
  255. msleep(5);
  256. ohci->next_statechange = jiffies;
  257. spin_lock_irqsave(&tmio->lock, flags);
  258. misc = tmio_ioread8(tmio->ccr + CCR_MISC);
  259. misc |= 1 << 3; /* USSUSP */
  260. tmio_iowrite8(misc, tmio->ccr + CCR_MISC);
  261. spin_unlock_irqrestore(&tmio->lock, flags);
  262. if (cell->suspend) {
  263. ret = cell->suspend(dev);
  264. if (ret)
  265. return ret;
  266. }
  267. return 0;
  268. }
  269. static int ohci_hcd_tmio_drv_resume(struct platform_device *dev)
  270. {
  271. const struct mfd_cell *cell = mfd_get_cell(dev);
  272. struct usb_hcd *hcd = platform_get_drvdata(dev);
  273. struct ohci_hcd *ohci = hcd_to_ohci(hcd);
  274. struct tmio_hcd *tmio = hcd_to_tmio(hcd);
  275. unsigned long flags;
  276. u8 misc;
  277. int ret;
  278. if (time_before(jiffies, ohci->next_statechange))
  279. msleep(5);
  280. ohci->next_statechange = jiffies;
  281. if (cell->resume) {
  282. ret = cell->resume(dev);
  283. if (ret)
  284. return ret;
  285. }
  286. tmio_start_hc(dev);
  287. spin_lock_irqsave(&tmio->lock, flags);
  288. misc = tmio_ioread8(tmio->ccr + CCR_MISC);
  289. misc &= ~(1 << 3); /* USSUSP */
  290. tmio_iowrite8(misc, tmio->ccr + CCR_MISC);
  291. spin_unlock_irqrestore(&tmio->lock, flags);
  292. ohci_resume(hcd, false);
  293. return 0;
  294. }
  295. #else
  296. #define ohci_hcd_tmio_drv_suspend NULL
  297. #define ohci_hcd_tmio_drv_resume NULL
  298. #endif
  299. static struct platform_driver ohci_hcd_tmio_driver = {
  300. .probe = ohci_hcd_tmio_drv_probe,
  301. .remove = ohci_hcd_tmio_drv_remove,
  302. .shutdown = usb_hcd_platform_shutdown,
  303. .suspend = ohci_hcd_tmio_drv_suspend,
  304. .resume = ohci_hcd_tmio_drv_resume,
  305. .driver = {
  306. .name = "tmio-ohci",
  307. },
  308. };