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