ohci-at91.c 8.3 KB

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