ohci-spear.c 4.5 KB

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