ohci-spear.c 4.7 KB

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