ohci-at91.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. clk_put(fclk);
  138. clk_put(iclk);
  139. fclk = iclk = NULL;
  140. dev_set_drvdata(&pdev->dev, NULL);
  141. return 0;
  142. }
  143. /*-------------------------------------------------------------------------*/
  144. static int __devinit
  145. ohci_at91_start (struct usb_hcd *hcd)
  146. {
  147. struct at91_usbh_data *board = hcd->self.controller->platform_data;
  148. struct ohci_hcd *ohci = hcd_to_ohci (hcd);
  149. int ret;
  150. if ((ret = ohci_init(ohci)) < 0)
  151. return ret;
  152. ohci->num_ports = board->ports;
  153. if ((ret = ohci_run(ohci)) < 0) {
  154. err("can't start %s", hcd->self.bus_name);
  155. ohci_stop(hcd);
  156. return ret;
  157. }
  158. return 0;
  159. }
  160. /*-------------------------------------------------------------------------*/
  161. static const struct hc_driver ohci_at91_hc_driver = {
  162. .description = hcd_name,
  163. .product_desc = "AT91 OHCI",
  164. .hcd_priv_size = sizeof(struct ohci_hcd),
  165. /*
  166. * generic hardware linkage
  167. */
  168. .irq = ohci_irq,
  169. .flags = HCD_USB11 | HCD_MEMORY,
  170. /*
  171. * basic lifecycle operations
  172. */
  173. .start = ohci_at91_start,
  174. .stop = ohci_stop,
  175. .shutdown = ohci_shutdown,
  176. /*
  177. * managing i/o requests and associated device resources
  178. */
  179. .urb_enqueue = ohci_urb_enqueue,
  180. .urb_dequeue = ohci_urb_dequeue,
  181. .endpoint_disable = ohci_endpoint_disable,
  182. /*
  183. * scheduling support
  184. */
  185. .get_frame_number = ohci_get_frame,
  186. /*
  187. * root hub support
  188. */
  189. .hub_status_data = ohci_hub_status_data,
  190. .hub_control = ohci_hub_control,
  191. .hub_irq_enable = ohci_rhsc_enable,
  192. #ifdef CONFIG_PM
  193. .bus_suspend = ohci_bus_suspend,
  194. .bus_resume = ohci_bus_resume,
  195. #endif
  196. .start_port_reset = ohci_start_port_reset,
  197. };
  198. /*-------------------------------------------------------------------------*/
  199. static int ohci_hcd_at91_drv_probe(struct platform_device *pdev)
  200. {
  201. device_init_wakeup(&pdev->dev, 1);
  202. return usb_hcd_at91_probe(&ohci_at91_hc_driver, pdev);
  203. }
  204. static int ohci_hcd_at91_drv_remove(struct platform_device *pdev)
  205. {
  206. device_init_wakeup(&pdev->dev, 0);
  207. return usb_hcd_at91_remove(platform_get_drvdata(pdev), pdev);
  208. }
  209. #ifdef CONFIG_PM
  210. static int
  211. ohci_hcd_at91_drv_suspend(struct platform_device *pdev, pm_message_t mesg)
  212. {
  213. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  214. struct ohci_hcd *ohci = hcd_to_ohci(hcd);
  215. if (device_may_wakeup(&pdev->dev))
  216. enable_irq_wake(hcd->irq);
  217. /*
  218. * The integrated transceivers seem unable to notice disconnect,
  219. * reconnect, or wakeup without the 48 MHz clock active. so for
  220. * correctness, always discard connection state (using reset).
  221. *
  222. * REVISIT: some boards will be able to turn VBUS off...
  223. */
  224. if (at91_suspend_entering_slow_clock()) {
  225. ohci_usb_reset (ohci);
  226. clk_disable(fclk);
  227. clk_disable(iclk);
  228. clocked = 0;
  229. }
  230. return 0;
  231. }
  232. static int ohci_hcd_at91_drv_resume(struct platform_device *pdev)
  233. {
  234. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  235. if (device_may_wakeup(&pdev->dev))
  236. disable_irq_wake(hcd->irq);
  237. if (!clocked) {
  238. clk_enable(iclk);
  239. clk_enable(fclk);
  240. clocked = 1;
  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. };