ohci-at91.c 8.2 KB

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