ohci-spear.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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/clk.h>
  14. #include <linux/dma-mapping.h>
  15. #include <linux/io.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/of.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/signal.h>
  21. #include <linux/usb.h>
  22. #include <linux/usb/hcd.h>
  23. #include "ohci.h"
  24. #define DRIVER_DESC "OHCI SPEAr driver"
  25. static const char hcd_name[] = "SPEAr-ohci";
  26. struct spear_ohci {
  27. struct clk *clk;
  28. };
  29. #define to_spear_ohci(hcd) (struct spear_ohci *)(hcd_to_ohci(hcd)->priv)
  30. static struct hc_driver __read_mostly ohci_spear_hc_driver;
  31. static int spear_ohci_hcd_drv_probe(struct platform_device *pdev)
  32. {
  33. const struct hc_driver *driver = &ohci_spear_hc_driver;
  34. struct ohci_hcd *ohci;
  35. struct usb_hcd *hcd = NULL;
  36. struct clk *usbh_clk;
  37. struct spear_ohci *sohci_p;
  38. struct resource *res;
  39. int retval, irq;
  40. irq = platform_get_irq(pdev, 0);
  41. if (irq < 0) {
  42. retval = irq;
  43. goto fail;
  44. }
  45. /*
  46. * Right now device-tree probed devices don't get dma_mask set.
  47. * Since shared usb code relies on it, set it here for now.
  48. * Once we have dma capability bindings this can go away.
  49. */
  50. retval = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  51. if (retval)
  52. goto fail;
  53. usbh_clk = devm_clk_get(&pdev->dev, NULL);
  54. if (IS_ERR(usbh_clk)) {
  55. dev_err(&pdev->dev, "Error getting interface clock\n");
  56. retval = PTR_ERR(usbh_clk);
  57. goto fail;
  58. }
  59. hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev));
  60. if (!hcd) {
  61. retval = -ENOMEM;
  62. goto fail;
  63. }
  64. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  65. if (!res) {
  66. retval = -ENODEV;
  67. goto err_put_hcd;
  68. }
  69. hcd->rsrc_start = pdev->resource[0].start;
  70. hcd->rsrc_len = resource_size(res);
  71. hcd->regs = devm_ioremap_resource(&pdev->dev, res);
  72. if (IS_ERR(hcd->regs)) {
  73. retval = PTR_ERR(hcd->regs);
  74. goto err_put_hcd;
  75. }
  76. sohci_p = to_spear_ohci(hcd);
  77. sohci_p->clk = usbh_clk;
  78. clk_prepare_enable(sohci_p->clk);
  79. ohci = hcd_to_ohci(hcd);
  80. retval = usb_add_hcd(hcd, platform_get_irq(pdev, 0), 0);
  81. if (retval == 0) {
  82. device_wakeup_enable(hcd->self.controller);
  83. return retval;
  84. }
  85. clk_disable_unprepare(sohci_p->clk);
  86. err_put_hcd:
  87. usb_put_hcd(hcd);
  88. fail:
  89. dev_err(&pdev->dev, "init fail, %d\n", retval);
  90. return retval;
  91. }
  92. static int spear_ohci_hcd_drv_remove(struct platform_device *pdev)
  93. {
  94. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  95. struct spear_ohci *sohci_p = to_spear_ohci(hcd);
  96. usb_remove_hcd(hcd);
  97. if (sohci_p->clk)
  98. clk_disable_unprepare(sohci_p->clk);
  99. usb_put_hcd(hcd);
  100. return 0;
  101. }
  102. #if defined(CONFIG_PM)
  103. static int spear_ohci_hcd_drv_suspend(struct platform_device *pdev,
  104. pm_message_t message)
  105. {
  106. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  107. struct ohci_hcd *ohci = hcd_to_ohci(hcd);
  108. struct spear_ohci *sohci_p = to_spear_ohci(hcd);
  109. bool do_wakeup = device_may_wakeup(&pdev->dev);
  110. int ret;
  111. if (time_before(jiffies, ohci->next_statechange))
  112. msleep(5);
  113. ohci->next_statechange = jiffies;
  114. ret = ohci_suspend(hcd, do_wakeup);
  115. if (ret)
  116. return ret;
  117. clk_disable_unprepare(sohci_p->clk);
  118. return ret;
  119. }
  120. static int spear_ohci_hcd_drv_resume(struct platform_device *dev)
  121. {
  122. struct usb_hcd *hcd = platform_get_drvdata(dev);
  123. struct ohci_hcd *ohci = hcd_to_ohci(hcd);
  124. struct spear_ohci *sohci_p = to_spear_ohci(hcd);
  125. if (time_before(jiffies, ohci->next_statechange))
  126. msleep(5);
  127. ohci->next_statechange = jiffies;
  128. clk_prepare_enable(sohci_p->clk);
  129. ohci_resume(hcd, false);
  130. return 0;
  131. }
  132. #endif
  133. static struct of_device_id spear_ohci_id_table[] = {
  134. { .compatible = "st,spear600-ohci", },
  135. { },
  136. };
  137. /* Driver definition to register with the platform bus */
  138. static struct platform_driver spear_ohci_hcd_driver = {
  139. .probe = spear_ohci_hcd_drv_probe,
  140. .remove = spear_ohci_hcd_drv_remove,
  141. #ifdef CONFIG_PM
  142. .suspend = spear_ohci_hcd_drv_suspend,
  143. .resume = spear_ohci_hcd_drv_resume,
  144. #endif
  145. .driver = {
  146. .owner = THIS_MODULE,
  147. .name = "spear-ohci",
  148. .of_match_table = spear_ohci_id_table,
  149. },
  150. };
  151. static const struct ohci_driver_overrides spear_overrides __initconst = {
  152. .extra_priv_size = sizeof(struct spear_ohci),
  153. };
  154. static int __init ohci_spear_init(void)
  155. {
  156. if (usb_disabled())
  157. return -ENODEV;
  158. pr_info("%s: " DRIVER_DESC "\n", hcd_name);
  159. ohci_init_driver(&ohci_spear_hc_driver, &spear_overrides);
  160. return platform_driver_register(&spear_ohci_hcd_driver);
  161. }
  162. module_init(ohci_spear_init);
  163. static void __exit ohci_spear_cleanup(void)
  164. {
  165. platform_driver_unregister(&spear_ohci_hcd_driver);
  166. }
  167. module_exit(ohci_spear_cleanup);
  168. MODULE_DESCRIPTION(DRIVER_DESC);
  169. MODULE_AUTHOR("Deepak Sikri");
  170. MODULE_LICENSE("GPL v2");
  171. MODULE_ALIAS("platform:spear-ohci");