ohci-spear.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. hcd->regs = devm_ioremap_resource(&pdev->dev, res);
  66. if (IS_ERR(hcd->regs)) {
  67. retval = PTR_ERR(hcd->regs);
  68. goto err_put_hcd;
  69. }
  70. hcd->rsrc_start = pdev->resource[0].start;
  71. hcd->rsrc_len = resource_size(res);
  72. sohci_p = to_spear_ohci(hcd);
  73. sohci_p->clk = usbh_clk;
  74. clk_prepare_enable(sohci_p->clk);
  75. ohci = hcd_to_ohci(hcd);
  76. retval = usb_add_hcd(hcd, platform_get_irq(pdev, 0), 0);
  77. if (retval == 0) {
  78. device_wakeup_enable(hcd->self.controller);
  79. return retval;
  80. }
  81. clk_disable_unprepare(sohci_p->clk);
  82. err_put_hcd:
  83. usb_put_hcd(hcd);
  84. fail:
  85. dev_err(&pdev->dev, "init fail, %d\n", retval);
  86. return retval;
  87. }
  88. static int spear_ohci_hcd_drv_remove(struct platform_device *pdev)
  89. {
  90. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  91. struct spear_ohci *sohci_p = to_spear_ohci(hcd);
  92. usb_remove_hcd(hcd);
  93. if (sohci_p->clk)
  94. clk_disable_unprepare(sohci_p->clk);
  95. usb_put_hcd(hcd);
  96. return 0;
  97. }
  98. #if defined(CONFIG_PM)
  99. static int spear_ohci_hcd_drv_suspend(struct platform_device *pdev,
  100. pm_message_t message)
  101. {
  102. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  103. struct ohci_hcd *ohci = hcd_to_ohci(hcd);
  104. struct spear_ohci *sohci_p = to_spear_ohci(hcd);
  105. bool do_wakeup = device_may_wakeup(&pdev->dev);
  106. int ret;
  107. if (time_before(jiffies, ohci->next_statechange))
  108. msleep(5);
  109. ohci->next_statechange = jiffies;
  110. ret = ohci_suspend(hcd, do_wakeup);
  111. if (ret)
  112. return ret;
  113. clk_disable_unprepare(sohci_p->clk);
  114. return ret;
  115. }
  116. static int spear_ohci_hcd_drv_resume(struct platform_device *dev)
  117. {
  118. struct usb_hcd *hcd = platform_get_drvdata(dev);
  119. struct ohci_hcd *ohci = hcd_to_ohci(hcd);
  120. struct spear_ohci *sohci_p = to_spear_ohci(hcd);
  121. if (time_before(jiffies, ohci->next_statechange))
  122. msleep(5);
  123. ohci->next_statechange = jiffies;
  124. clk_prepare_enable(sohci_p->clk);
  125. ohci_resume(hcd, false);
  126. return 0;
  127. }
  128. #endif
  129. static const struct of_device_id spear_ohci_id_table[] = {
  130. { .compatible = "st,spear600-ohci", },
  131. { },
  132. };
  133. MODULE_DEVICE_TABLE(of, spear_ohci_id_table);
  134. /* Driver definition to register with the platform bus */
  135. static struct platform_driver spear_ohci_hcd_driver = {
  136. .probe = spear_ohci_hcd_drv_probe,
  137. .remove = spear_ohci_hcd_drv_remove,
  138. #ifdef CONFIG_PM
  139. .suspend = spear_ohci_hcd_drv_suspend,
  140. .resume = spear_ohci_hcd_drv_resume,
  141. #endif
  142. .driver = {
  143. .name = "spear-ohci",
  144. .of_match_table = spear_ohci_id_table,
  145. },
  146. };
  147. static const struct ohci_driver_overrides spear_overrides __initconst = {
  148. .extra_priv_size = sizeof(struct spear_ohci),
  149. };
  150. static int __init ohci_spear_init(void)
  151. {
  152. if (usb_disabled())
  153. return -ENODEV;
  154. pr_info("%s: " DRIVER_DESC "\n", hcd_name);
  155. ohci_init_driver(&ohci_spear_hc_driver, &spear_overrides);
  156. return platform_driver_register(&spear_ohci_hcd_driver);
  157. }
  158. module_init(ohci_spear_init);
  159. static void __exit ohci_spear_cleanup(void)
  160. {
  161. platform_driver_unregister(&spear_ohci_hcd_driver);
  162. }
  163. module_exit(ohci_spear_cleanup);
  164. MODULE_DESCRIPTION(DRIVER_DESC);
  165. MODULE_AUTHOR("Deepak Sikri");
  166. MODULE_LICENSE("GPL v2");
  167. MODULE_ALIAS("platform:spear-ohci");