ohci-spear.c 4.5 KB

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