ohci-spear.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * OHCI HCD (Host Controller Driver) for USB.
  3. *
  4. * Copyright (C) 2010 ST Microelectronics.
  5. * Deepak Sikri<deepak.sikri@st.com>
  6. *
  7. * Based on various ohci-*.c drivers
  8. *
  9. * This file is licensed under the terms of the GNU General Public
  10. * License version 2. This program is licensed "as is" without any
  11. * warranty of any kind, whether express or implied.
  12. */
  13. #include <linux/signal.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/clk.h>
  16. #include <linux/of.h>
  17. struct spear_ohci {
  18. struct ohci_hcd ohci;
  19. struct clk *clk;
  20. };
  21. #define to_spear_ohci(hcd) (struct spear_ohci *)hcd_to_ohci(hcd)
  22. static void spear_start_ohci(struct spear_ohci *ohci)
  23. {
  24. clk_prepare_enable(ohci->clk);
  25. }
  26. static void spear_stop_ohci(struct spear_ohci *ohci)
  27. {
  28. clk_disable_unprepare(ohci->clk);
  29. }
  30. static int ohci_spear_start(struct usb_hcd *hcd)
  31. {
  32. struct ohci_hcd *ohci = hcd_to_ohci(hcd);
  33. int ret;
  34. ret = ohci_init(ohci);
  35. if (ret < 0)
  36. return ret;
  37. ohci->regs = hcd->regs;
  38. ret = ohci_run(ohci);
  39. if (ret < 0) {
  40. dev_err(hcd->self.controller, "can't start\n");
  41. ohci_stop(hcd);
  42. return ret;
  43. }
  44. create_debug_files(ohci);
  45. #ifdef DEBUG
  46. ohci_dump(ohci, 1);
  47. #endif
  48. return 0;
  49. }
  50. static const struct hc_driver ohci_spear_hc_driver = {
  51. .description = hcd_name,
  52. .product_desc = "SPEAr OHCI",
  53. .hcd_priv_size = sizeof(struct spear_ohci),
  54. /* generic hardware linkage */
  55. .irq = ohci_irq,
  56. .flags = HCD_USB11 | HCD_MEMORY,
  57. /* basic lifecycle operations */
  58. .start = ohci_spear_start,
  59. .stop = ohci_stop,
  60. .shutdown = ohci_shutdown,
  61. #ifdef CONFIG_PM
  62. .bus_suspend = ohci_bus_suspend,
  63. .bus_resume = ohci_bus_resume,
  64. #endif
  65. /* managing i/o requests and associated device resources */
  66. .urb_enqueue = ohci_urb_enqueue,
  67. .urb_dequeue = ohci_urb_dequeue,
  68. .endpoint_disable = ohci_endpoint_disable,
  69. /* scheduling support */
  70. .get_frame_number = ohci_get_frame,
  71. /* root hub support */
  72. .hub_status_data = ohci_hub_status_data,
  73. .hub_control = ohci_hub_control,
  74. .start_port_reset = ohci_start_port_reset,
  75. };
  76. static u64 spear_ohci_dma_mask = DMA_BIT_MASK(32);
  77. static int spear_ohci_hcd_drv_probe(struct platform_device *pdev)
  78. {
  79. const struct hc_driver *driver = &ohci_spear_hc_driver;
  80. struct usb_hcd *hcd = NULL;
  81. struct clk *usbh_clk;
  82. struct spear_ohci *ohci_p;
  83. struct resource *res;
  84. int retval, irq;
  85. irq = platform_get_irq(pdev, 0);
  86. if (irq < 0) {
  87. retval = irq;
  88. goto fail;
  89. }
  90. /*
  91. * Right now device-tree probed devices don't get dma_mask set.
  92. * Since shared usb code relies on it, set it here for now.
  93. * Once we have dma capability bindings this can go away.
  94. */
  95. if (!pdev->dev.dma_mask)
  96. pdev->dev.dma_mask = &spear_ohci_dma_mask;
  97. usbh_clk = devm_clk_get(&pdev->dev, NULL);
  98. if (IS_ERR(usbh_clk)) {
  99. dev_err(&pdev->dev, "Error getting interface clock\n");
  100. retval = PTR_ERR(usbh_clk);
  101. goto fail;
  102. }
  103. hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev));
  104. if (!hcd) {
  105. retval = -ENOMEM;
  106. goto fail;
  107. }
  108. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  109. if (!res) {
  110. retval = -ENODEV;
  111. goto err_put_hcd;
  112. }
  113. hcd->rsrc_start = pdev->resource[0].start;
  114. hcd->rsrc_len = resource_size(res);
  115. if (!devm_request_mem_region(&pdev->dev, hcd->rsrc_start, hcd->rsrc_len,
  116. hcd_name)) {
  117. dev_dbg(&pdev->dev, "request_mem_region failed\n");
  118. retval = -EBUSY;
  119. goto err_put_hcd;
  120. }
  121. hcd->regs = devm_ioremap(&pdev->dev, hcd->rsrc_start, hcd->rsrc_len);
  122. if (!hcd->regs) {
  123. dev_dbg(&pdev->dev, "ioremap failed\n");
  124. retval = -ENOMEM;
  125. goto err_put_hcd;
  126. }
  127. ohci_p = (struct spear_ohci *)hcd_to_ohci(hcd);
  128. ohci_p->clk = usbh_clk;
  129. spear_start_ohci(ohci_p);
  130. ohci_hcd_init(hcd_to_ohci(hcd));
  131. retval = usb_add_hcd(hcd, platform_get_irq(pdev, 0), 0);
  132. if (retval == 0)
  133. return retval;
  134. spear_stop_ohci(ohci_p);
  135. err_put_hcd:
  136. usb_put_hcd(hcd);
  137. fail:
  138. dev_err(&pdev->dev, "init fail, %d\n", retval);
  139. return retval;
  140. }
  141. static int spear_ohci_hcd_drv_remove(struct platform_device *pdev)
  142. {
  143. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  144. struct spear_ohci *ohci_p = to_spear_ohci(hcd);
  145. usb_remove_hcd(hcd);
  146. if (ohci_p->clk)
  147. spear_stop_ohci(ohci_p);
  148. usb_put_hcd(hcd);
  149. platform_set_drvdata(pdev, NULL);
  150. return 0;
  151. }
  152. #if defined(CONFIG_PM)
  153. static int spear_ohci_hcd_drv_suspend(struct platform_device *dev,
  154. pm_message_t message)
  155. {
  156. struct usb_hcd *hcd = platform_get_drvdata(dev);
  157. struct ohci_hcd *ohci = hcd_to_ohci(hcd);
  158. struct spear_ohci *ohci_p = to_spear_ohci(hcd);
  159. if (time_before(jiffies, ohci->next_statechange))
  160. msleep(5);
  161. ohci->next_statechange = jiffies;
  162. spear_stop_ohci(ohci_p);
  163. return 0;
  164. }
  165. static int spear_ohci_hcd_drv_resume(struct platform_device *dev)
  166. {
  167. struct usb_hcd *hcd = platform_get_drvdata(dev);
  168. struct ohci_hcd *ohci = hcd_to_ohci(hcd);
  169. struct spear_ohci *ohci_p = to_spear_ohci(hcd);
  170. if (time_before(jiffies, ohci->next_statechange))
  171. msleep(5);
  172. ohci->next_statechange = jiffies;
  173. spear_start_ohci(ohci_p);
  174. ohci_resume(hcd, false);
  175. return 0;
  176. }
  177. #endif
  178. static struct of_device_id spear_ohci_id_table[] = {
  179. { .compatible = "st,spear600-ohci", },
  180. { },
  181. };
  182. /* Driver definition to register with the platform bus */
  183. static struct platform_driver spear_ohci_hcd_driver = {
  184. .probe = spear_ohci_hcd_drv_probe,
  185. .remove = spear_ohci_hcd_drv_remove,
  186. #ifdef CONFIG_PM
  187. .suspend = spear_ohci_hcd_drv_suspend,
  188. .resume = spear_ohci_hcd_drv_resume,
  189. #endif
  190. .driver = {
  191. .owner = THIS_MODULE,
  192. .name = "spear-ohci",
  193. .of_match_table = of_match_ptr(spear_ohci_id_table),
  194. },
  195. };
  196. MODULE_ALIAS("platform:spear-ohci");