ohci-tmio.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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/init.h>
  30. #include <linux/namei.h>
  31. #include <linux/sched.h>*/
  32. #include <linux/platform_device.h>
  33. #include <linux/mfd/core.h>
  34. #include <linux/mfd/tmio.h>
  35. #include <linux/dma-mapping.h>
  36. /*-------------------------------------------------------------------------*/
  37. /*
  38. * USB Host Controller Configuration Register
  39. */
  40. #define CCR_REVID 0x08 /* b Revision ID */
  41. #define CCR_BASE 0x10 /* l USB Control Register Base Address Low */
  42. #define CCR_ILME 0x40 /* b Internal Local Memory Enable */
  43. #define CCR_PM 0x4c /* w Power Management */
  44. #define CCR_INTC 0x50 /* b INT Control */
  45. #define CCR_LMW1L 0x54 /* w Local Memory Window 1 LMADRS Low */
  46. #define CCR_LMW1H 0x56 /* w Local Memory Window 1 LMADRS High */
  47. #define CCR_LMW1BL 0x58 /* w Local Memory Window 1 Base Address Low */
  48. #define CCR_LMW1BH 0x5A /* w Local Memory Window 1 Base Address High */
  49. #define CCR_LMW2L 0x5C /* w Local Memory Window 2 LMADRS Low */
  50. #define CCR_LMW2H 0x5E /* w Local Memory Window 2 LMADRS High */
  51. #define CCR_LMW2BL 0x60 /* w Local Memory Window 2 Base Address Low */
  52. #define CCR_LMW2BH 0x62 /* w Local Memory Window 2 Base Address High */
  53. #define CCR_MISC 0xFC /* b MISC */
  54. #define CCR_PM_GKEN 0x0001
  55. #define CCR_PM_CKRNEN 0x0002
  56. #define CCR_PM_USBPW1 0x0004
  57. #define CCR_PM_USBPW2 0x0008
  58. #define CCR_PM_USBPW3 0x0008
  59. #define CCR_PM_PMEE 0x0100
  60. #define CCR_PM_PMES 0x8000
  61. /*-------------------------------------------------------------------------*/
  62. struct tmio_hcd {
  63. void __iomem *ccr;
  64. spinlock_t lock; /* protects RMW cycles */
  65. };
  66. #define hcd_to_tmio(hcd) ((struct tmio_hcd *)(hcd_to_ohci(hcd) + 1))
  67. /*-------------------------------------------------------------------------*/
  68. static void tmio_write_pm(struct platform_device *dev)
  69. {
  70. struct usb_hcd *hcd = platform_get_drvdata(dev);
  71. struct tmio_hcd *tmio = hcd_to_tmio(hcd);
  72. u16 pm;
  73. unsigned long flags;
  74. spin_lock_irqsave(&tmio->lock, flags);
  75. pm = CCR_PM_GKEN | CCR_PM_CKRNEN |
  76. CCR_PM_PMEE | CCR_PM_PMES;
  77. tmio_iowrite16(pm, tmio->ccr + CCR_PM);
  78. spin_unlock_irqrestore(&tmio->lock, flags);
  79. }
  80. static void tmio_stop_hc(struct platform_device *dev)
  81. {
  82. struct usb_hcd *hcd = platform_get_drvdata(dev);
  83. struct ohci_hcd *ohci = hcd_to_ohci(hcd);
  84. struct tmio_hcd *tmio = hcd_to_tmio(hcd);
  85. u16 pm;
  86. pm = CCR_PM_GKEN | CCR_PM_CKRNEN;
  87. switch (ohci->num_ports) {
  88. default:
  89. dev_err(&dev->dev, "Unsupported amount of ports: %d\n", ohci->num_ports);
  90. case 3:
  91. pm |= CCR_PM_USBPW3;
  92. case 2:
  93. pm |= CCR_PM_USBPW2;
  94. case 1:
  95. pm |= CCR_PM_USBPW1;
  96. }
  97. tmio_iowrite8(0, tmio->ccr + CCR_INTC);
  98. tmio_iowrite8(0, tmio->ccr + CCR_ILME);
  99. tmio_iowrite16(0, tmio->ccr + CCR_BASE);
  100. tmio_iowrite16(0, tmio->ccr + CCR_BASE + 2);
  101. tmio_iowrite16(pm, tmio->ccr + CCR_PM);
  102. }
  103. static void tmio_start_hc(struct platform_device *dev)
  104. {
  105. struct usb_hcd *hcd = platform_get_drvdata(dev);
  106. struct tmio_hcd *tmio = hcd_to_tmio(hcd);
  107. unsigned long base = hcd->rsrc_start;
  108. tmio_write_pm(dev);
  109. tmio_iowrite16(base, tmio->ccr + CCR_BASE);
  110. tmio_iowrite16(base >> 16, tmio->ccr + CCR_BASE + 2);
  111. tmio_iowrite8(1, tmio->ccr + CCR_ILME);
  112. tmio_iowrite8(2, tmio->ccr + CCR_INTC);
  113. dev_info(&dev->dev, "revision %d @ 0x%08llx, irq %d\n",
  114. tmio_ioread8(tmio->ccr + CCR_REVID), 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. err("can't start %s", hcd->self.bus_name);
  124. ohci_stop(hcd);
  125. return ret;
  126. }
  127. return 0;
  128. }
  129. static const struct hc_driver ohci_tmio_hc_driver = {
  130. .description = hcd_name,
  131. .product_desc = "TMIO OHCI USB Host Controller",
  132. .hcd_priv_size = sizeof(struct ohci_hcd) + sizeof (struct tmio_hcd),
  133. /* generic hardware linkage */
  134. .irq = ohci_irq,
  135. .flags = HCD_USB11 | HCD_MEMORY | HCD_LOCAL_MEM,
  136. /* basic lifecycle operations */
  137. .start = ohci_tmio_start,
  138. .stop = ohci_stop,
  139. .shutdown = ohci_shutdown,
  140. /* managing i/o requests and associated device resources */
  141. .urb_enqueue = ohci_urb_enqueue,
  142. .urb_dequeue = ohci_urb_dequeue,
  143. .endpoint_disable = ohci_endpoint_disable,
  144. /* scheduling support */
  145. .get_frame_number = ohci_get_frame,
  146. /* root hub support */
  147. .hub_status_data = ohci_hub_status_data,
  148. .hub_control = ohci_hub_control,
  149. #ifdef CONFIG_PM
  150. .bus_suspend = ohci_bus_suspend,
  151. .bus_resume = ohci_bus_resume,
  152. #endif
  153. .start_port_reset = ohci_start_port_reset,
  154. };
  155. /*-------------------------------------------------------------------------*/
  156. static struct platform_driver ohci_hcd_tmio_driver;
  157. static int __devinit ohci_hcd_tmio_drv_probe(struct platform_device *dev)
  158. {
  159. const struct mfd_cell *cell = mfd_get_cell(dev);
  160. struct resource *regs = platform_get_resource(dev, IORESOURCE_MEM, 0);
  161. struct resource *config = platform_get_resource(dev, IORESOURCE_MEM, 1);
  162. struct resource *sram = platform_get_resource(dev, IORESOURCE_MEM, 2);
  163. int irq = platform_get_irq(dev, 0);
  164. struct tmio_hcd *tmio;
  165. struct ohci_hcd *ohci;
  166. struct usb_hcd *hcd;
  167. int ret;
  168. if (usb_disabled())
  169. return -ENODEV;
  170. if (!cell)
  171. return -EINVAL;
  172. hcd = usb_create_hcd(&ohci_tmio_hc_driver, &dev->dev, dev_name(&dev->dev));
  173. if (!hcd) {
  174. ret = -ENOMEM;
  175. goto err_usb_create_hcd;
  176. }
  177. hcd->rsrc_start = regs->start;
  178. hcd->rsrc_len = resource_size(regs);
  179. tmio = hcd_to_tmio(hcd);
  180. spin_lock_init(&tmio->lock);
  181. tmio->ccr = ioremap(config->start, resource_size(config));
  182. if (!tmio->ccr) {
  183. ret = -ENOMEM;
  184. goto err_ioremap_ccr;
  185. }
  186. hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
  187. if (!hcd->regs) {
  188. ret = -ENOMEM;
  189. goto err_ioremap_regs;
  190. }
  191. if (!dma_declare_coherent_memory(&dev->dev, sram->start,
  192. sram->start,
  193. resource_size(sram),
  194. DMA_MEMORY_MAP | DMA_MEMORY_EXCLUSIVE)) {
  195. ret = -EBUSY;
  196. goto err_dma_declare;
  197. }
  198. if (cell->enable) {
  199. ret = cell->enable(dev);
  200. if (ret)
  201. goto err_enable;
  202. }
  203. tmio_start_hc(dev);
  204. ohci = hcd_to_ohci(hcd);
  205. ohci_hcd_init(ohci);
  206. ret = usb_add_hcd(hcd, irq, 0);
  207. if (ret)
  208. goto err_add_hcd;
  209. if (ret == 0)
  210. return ret;
  211. usb_remove_hcd(hcd);
  212. err_add_hcd:
  213. tmio_stop_hc(dev);
  214. if (cell->disable)
  215. cell->disable(dev);
  216. err_enable:
  217. dma_release_declared_memory(&dev->dev);
  218. err_dma_declare:
  219. iounmap(hcd->regs);
  220. err_ioremap_regs:
  221. iounmap(tmio->ccr);
  222. err_ioremap_ccr:
  223. usb_put_hcd(hcd);
  224. err_usb_create_hcd:
  225. return ret;
  226. }
  227. static int __devexit ohci_hcd_tmio_drv_remove(struct platform_device *dev)
  228. {
  229. struct usb_hcd *hcd = platform_get_drvdata(dev);
  230. struct tmio_hcd *tmio = hcd_to_tmio(hcd);
  231. const struct mfd_cell *cell = mfd_get_cell(dev);
  232. usb_remove_hcd(hcd);
  233. tmio_stop_hc(dev);
  234. if (cell->disable)
  235. cell->disable(dev);
  236. dma_release_declared_memory(&dev->dev);
  237. iounmap(hcd->regs);
  238. iounmap(tmio->ccr);
  239. usb_put_hcd(hcd);
  240. platform_set_drvdata(dev, NULL);
  241. return 0;
  242. }
  243. #ifdef CONFIG_PM
  244. static int ohci_hcd_tmio_drv_suspend(struct platform_device *dev, pm_message_t state)
  245. {
  246. const struct mfd_cell *cell = mfd_get_cell(dev);
  247. struct usb_hcd *hcd = platform_get_drvdata(dev);
  248. struct ohci_hcd *ohci = hcd_to_ohci(hcd);
  249. struct tmio_hcd *tmio = hcd_to_tmio(hcd);
  250. unsigned long flags;
  251. u8 misc;
  252. int ret;
  253. if (time_before(jiffies, ohci->next_statechange))
  254. msleep(5);
  255. ohci->next_statechange = jiffies;
  256. spin_lock_irqsave(&tmio->lock, flags);
  257. misc = tmio_ioread8(tmio->ccr + CCR_MISC);
  258. misc |= 1 << 3; /* USSUSP */
  259. tmio_iowrite8(misc, tmio->ccr + CCR_MISC);
  260. spin_unlock_irqrestore(&tmio->lock, flags);
  261. if (cell->suspend) {
  262. ret = cell->suspend(dev);
  263. if (ret)
  264. return ret;
  265. }
  266. return 0;
  267. }
  268. static int ohci_hcd_tmio_drv_resume(struct platform_device *dev)
  269. {
  270. const struct mfd_cell *cell = mfd_get_cell(dev);
  271. struct usb_hcd *hcd = platform_get_drvdata(dev);
  272. struct ohci_hcd *ohci = hcd_to_ohci(hcd);
  273. struct tmio_hcd *tmio = hcd_to_tmio(hcd);
  274. unsigned long flags;
  275. u8 misc;
  276. int ret;
  277. if (time_before(jiffies, ohci->next_statechange))
  278. msleep(5);
  279. ohci->next_statechange = jiffies;
  280. if (cell->resume) {
  281. ret = cell->resume(dev);
  282. if (ret)
  283. return ret;
  284. }
  285. tmio_start_hc(dev);
  286. spin_lock_irqsave(&tmio->lock, flags);
  287. misc = tmio_ioread8(tmio->ccr + CCR_MISC);
  288. misc &= ~(1 << 3); /* USSUSP */
  289. tmio_iowrite8(misc, tmio->ccr + CCR_MISC);
  290. spin_unlock_irqrestore(&tmio->lock, flags);
  291. ohci_finish_controller_resume(hcd);
  292. return 0;
  293. }
  294. #else
  295. #define ohci_hcd_tmio_drv_suspend NULL
  296. #define ohci_hcd_tmio_drv_resume NULL
  297. #endif
  298. static struct platform_driver ohci_hcd_tmio_driver = {
  299. .probe = ohci_hcd_tmio_drv_probe,
  300. .remove = __devexit_p(ohci_hcd_tmio_drv_remove),
  301. .shutdown = usb_hcd_platform_shutdown,
  302. .suspend = ohci_hcd_tmio_drv_suspend,
  303. .resume = ohci_hcd_tmio_drv_resume,
  304. .driver = {
  305. .name = "tmio-ohci",
  306. .owner = THIS_MODULE,
  307. },
  308. };