ohci-at91.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*
  2. * OHCI HCD (Host Controller Driver) for USB.
  3. *
  4. * Copyright (C) 2004 SAN People (Pty) Ltd.
  5. * Copyright (C) 2005 Thibaut VARENE <varenet@parisc-linux.org>
  6. *
  7. * AT91 Bus Glue
  8. *
  9. * Based on fragments of 2.4 driver by Rick Bronson.
  10. * Based on ohci-omap.c
  11. *
  12. * This file is licenced under the GPL.
  13. */
  14. #include <linux/clk.h>
  15. #include <linux/platform_device.h>
  16. #include <asm/mach-types.h>
  17. #include <asm/hardware.h>
  18. #include <asm/arch/board.h>
  19. #ifndef CONFIG_ARCH_AT91
  20. #error "CONFIG_ARCH_AT91 must be defined."
  21. #endif
  22. /* interface and function clocks */
  23. static struct clk *iclk, *fclk;
  24. static int clocked;
  25. extern int usb_disabled(void);
  26. /*-------------------------------------------------------------------------*/
  27. static void at91_start_hc(struct platform_device *pdev)
  28. {
  29. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  30. struct ohci_regs __iomem *regs = hcd->regs;
  31. dev_dbg(&pdev->dev, "start\n");
  32. /*
  33. * Start the USB clocks.
  34. */
  35. clk_enable(iclk);
  36. clk_enable(fclk);
  37. clocked = 1;
  38. /*
  39. * The USB host controller must remain in reset.
  40. */
  41. writel(0, &regs->control);
  42. }
  43. static void at91_stop_hc(struct platform_device *pdev)
  44. {
  45. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  46. struct ohci_regs __iomem *regs = hcd->regs;
  47. dev_dbg(&pdev->dev, "stop\n");
  48. /*
  49. * Put the USB host controller into reset.
  50. */
  51. writel(0, &regs->control);
  52. /*
  53. * Stop the USB clocks.
  54. */
  55. clk_disable(fclk);
  56. clk_disable(iclk);
  57. clocked = 0;
  58. }
  59. /*-------------------------------------------------------------------------*/
  60. static int usb_hcd_at91_remove (struct usb_hcd *, struct platform_device *);
  61. /* configure so an HC device and id are always provided */
  62. /* always called with process context; sleeping is OK */
  63. /**
  64. * usb_hcd_at91_probe - initialize AT91-based HCDs
  65. * Context: !in_interrupt()
  66. *
  67. * Allocates basic resources for this USB host controller, and
  68. * then invokes the start() method for the HCD associated with it
  69. * through the hotplug entry's driver_data.
  70. */
  71. static int usb_hcd_at91_probe(const struct hc_driver *driver,
  72. struct platform_device *pdev)
  73. {
  74. int retval;
  75. struct usb_hcd *hcd = NULL;
  76. if (pdev->num_resources != 2) {
  77. pr_debug("hcd probe: invalid num_resources");
  78. return -ENODEV;
  79. }
  80. if ((pdev->resource[0].flags != IORESOURCE_MEM)
  81. || (pdev->resource[1].flags != IORESOURCE_IRQ)) {
  82. pr_debug("hcd probe: invalid resource type\n");
  83. return -ENODEV;
  84. }
  85. hcd = usb_create_hcd(driver, &pdev->dev, "at91");
  86. if (!hcd)
  87. return -ENOMEM;
  88. hcd->rsrc_start = pdev->resource[0].start;
  89. hcd->rsrc_len = pdev->resource[0].end - pdev->resource[0].start + 1;
  90. if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, hcd_name)) {
  91. pr_debug("request_mem_region failed\n");
  92. retval = -EBUSY;
  93. goto err1;
  94. }
  95. hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
  96. if (!hcd->regs) {
  97. pr_debug("ioremap failed\n");
  98. retval = -EIO;
  99. goto err2;
  100. }
  101. iclk = clk_get(&pdev->dev, "ohci_clk");
  102. fclk = clk_get(&pdev->dev, "uhpck");
  103. at91_start_hc(pdev);
  104. ohci_hcd_init(hcd_to_ohci(hcd));
  105. retval = usb_add_hcd(hcd, pdev->resource[1].start, IRQF_DISABLED);
  106. if (retval == 0)
  107. return retval;
  108. /* Error handling */
  109. at91_stop_hc(pdev);
  110. clk_put(fclk);
  111. clk_put(iclk);
  112. iounmap(hcd->regs);
  113. err2:
  114. release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
  115. err1:
  116. usb_put_hcd(hcd);
  117. return retval;
  118. }
  119. /* may be called with controller, bus, and devices active */
  120. /**
  121. * usb_hcd_at91_remove - shutdown processing for AT91-based HCDs
  122. * @dev: USB Host Controller being removed
  123. * Context: !in_interrupt()
  124. *
  125. * Reverses the effect of usb_hcd_at91_probe(), first invoking
  126. * the HCD's stop() method. It is always called from a thread
  127. * context, "rmmod" or something similar.
  128. *
  129. */
  130. static int usb_hcd_at91_remove(struct usb_hcd *hcd,
  131. struct platform_device *pdev)
  132. {
  133. usb_remove_hcd(hcd);
  134. at91_stop_hc(pdev);
  135. iounmap(hcd->regs);
  136. release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
  137. disable_irq_wake(hcd->irq);
  138. clk_put(fclk);
  139. clk_put(iclk);
  140. fclk = iclk = NULL;
  141. dev_set_drvdata(&pdev->dev, NULL);
  142. return 0;
  143. }
  144. /*-------------------------------------------------------------------------*/
  145. static int __devinit
  146. ohci_at91_start (struct usb_hcd *hcd)
  147. {
  148. struct at91_usbh_data *board = hcd->self.controller->platform_data;
  149. struct ohci_hcd *ohci = hcd_to_ohci (hcd);
  150. struct usb_device *root = hcd->self.root_hub;
  151. int ret;
  152. if ((ret = ohci_init(ohci)) < 0)
  153. return ret;
  154. root->maxchild = board->ports;
  155. if ((ret = ohci_run(ohci)) < 0) {
  156. err("can't start %s", hcd->self.bus_name);
  157. ohci_stop(hcd);
  158. return ret;
  159. }
  160. return 0;
  161. }
  162. /*-------------------------------------------------------------------------*/
  163. static const struct hc_driver ohci_at91_hc_driver = {
  164. .description = hcd_name,
  165. .product_desc = "AT91 OHCI",
  166. .hcd_priv_size = sizeof(struct ohci_hcd),
  167. /*
  168. * generic hardware linkage
  169. */
  170. .irq = ohci_irq,
  171. .flags = HCD_USB11 | HCD_MEMORY,
  172. /*
  173. * basic lifecycle operations
  174. */
  175. .start = ohci_at91_start,
  176. .stop = ohci_stop,
  177. .shutdown = ohci_shutdown,
  178. /*
  179. * managing i/o requests and associated device resources
  180. */
  181. .urb_enqueue = ohci_urb_enqueue,
  182. .urb_dequeue = ohci_urb_dequeue,
  183. .endpoint_disable = ohci_endpoint_disable,
  184. /*
  185. * scheduling support
  186. */
  187. .get_frame_number = ohci_get_frame,
  188. /*
  189. * root hub support
  190. */
  191. .hub_status_data = ohci_hub_status_data,
  192. .hub_control = ohci_hub_control,
  193. .hub_irq_enable = ohci_rhsc_enable,
  194. #ifdef CONFIG_PM
  195. .bus_suspend = ohci_bus_suspend,
  196. .bus_resume = ohci_bus_resume,
  197. #endif
  198. .start_port_reset = ohci_start_port_reset,
  199. };
  200. /*-------------------------------------------------------------------------*/
  201. static int ohci_hcd_at91_drv_probe(struct platform_device *pdev)
  202. {
  203. device_init_wakeup(&pdev->dev, 1);
  204. return usb_hcd_at91_probe(&ohci_at91_hc_driver, pdev);
  205. }
  206. static int ohci_hcd_at91_drv_remove(struct platform_device *pdev)
  207. {
  208. device_init_wakeup(&pdev->dev, 0);
  209. return usb_hcd_at91_remove(platform_get_drvdata(pdev), pdev);
  210. }
  211. #ifdef CONFIG_PM
  212. static int
  213. ohci_hcd_at91_drv_suspend(struct platform_device *pdev, pm_message_t mesg)
  214. {
  215. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  216. struct ohci_hcd *ohci = hcd_to_ohci(hcd);
  217. if (device_may_wakeup(&pdev->dev))
  218. enable_irq_wake(hcd->irq);
  219. else
  220. disable_irq_wake(hcd->irq);
  221. /*
  222. * The integrated transceivers seem unable to notice disconnect,
  223. * reconnect, or wakeup without the 48 MHz clock active. so for
  224. * correctness, always discard connection state (using reset).
  225. *
  226. * REVISIT: some boards will be able to turn VBUS off...
  227. */
  228. if (at91_suspend_entering_slow_clock()) {
  229. ohci_usb_reset (ohci);
  230. clk_disable(fclk);
  231. clk_disable(iclk);
  232. clocked = 0;
  233. }
  234. return 0;
  235. }
  236. static int ohci_hcd_at91_drv_resume(struct platform_device *pdev)
  237. {
  238. if (!clocked) {
  239. clk_enable(iclk);
  240. clk_enable(fclk);
  241. }
  242. return 0;
  243. }
  244. #else
  245. #define ohci_hcd_at91_drv_suspend NULL
  246. #define ohci_hcd_at91_drv_resume NULL
  247. #endif
  248. MODULE_ALIAS("at91_ohci");
  249. static struct platform_driver ohci_hcd_at91_driver = {
  250. .probe = ohci_hcd_at91_drv_probe,
  251. .remove = ohci_hcd_at91_drv_remove,
  252. .shutdown = usb_hcd_platform_shutdown,
  253. .suspend = ohci_hcd_at91_drv_suspend,
  254. .resume = ohci_hcd_at91_drv_resume,
  255. .driver = {
  256. .name = "at91_ohci",
  257. .owner = THIS_MODULE,
  258. },
  259. };
  260. static int __init ohci_hcd_at91_init (void)
  261. {
  262. if (usb_disabled())
  263. return -ENODEV;
  264. return platform_driver_register(&ohci_hcd_at91_driver);
  265. }
  266. static void __exit ohci_hcd_at91_cleanup (void)
  267. {
  268. platform_driver_unregister(&ohci_hcd_at91_driver);
  269. }
  270. module_init (ohci_hcd_at91_init);
  271. module_exit (ohci_hcd_at91_cleanup);