ehci-spear.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Driver for EHCI HCD on SPEAr SOC
  3. *
  4. * Copyright (C) 2010 ST Micro Electronics,
  5. * Deepak Sikri <deepak.sikri@st.com>
  6. *
  7. * Based on various ehci-*.c drivers
  8. *
  9. * This file is subject to the terms and conditions of the GNU General Public
  10. * License. See the file COPYING in the main directory of this archive for
  11. * more details.
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/dma-mapping.h>
  15. #include <linux/io.h>
  16. #include <linux/jiffies.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/of.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/pm.h>
  22. #include <linux/usb.h>
  23. #include <linux/usb/hcd.h>
  24. #include "ehci.h"
  25. #define DRIVER_DESC "EHCI SPEAr driver"
  26. static const char hcd_name[] = "SPEAr-ehci";
  27. struct spear_ehci {
  28. struct clk *clk;
  29. };
  30. #define to_spear_ehci(hcd) (struct spear_ehci *)(hcd_to_ehci(hcd)->priv)
  31. static struct hc_driver __read_mostly ehci_spear_hc_driver;
  32. #ifdef CONFIG_PM_SLEEP
  33. static int ehci_spear_drv_suspend(struct device *dev)
  34. {
  35. struct usb_hcd *hcd = dev_get_drvdata(dev);
  36. bool do_wakeup = device_may_wakeup(dev);
  37. return ehci_suspend(hcd, do_wakeup);
  38. }
  39. static int ehci_spear_drv_resume(struct device *dev)
  40. {
  41. struct usb_hcd *hcd = dev_get_drvdata(dev);
  42. ehci_resume(hcd, false);
  43. return 0;
  44. }
  45. #endif /* CONFIG_PM_SLEEP */
  46. static SIMPLE_DEV_PM_OPS(ehci_spear_pm_ops, ehci_spear_drv_suspend,
  47. ehci_spear_drv_resume);
  48. static u64 spear_ehci_dma_mask = DMA_BIT_MASK(32);
  49. static int spear_ehci_hcd_drv_probe(struct platform_device *pdev)
  50. {
  51. struct usb_hcd *hcd ;
  52. struct spear_ehci *sehci;
  53. struct resource *res;
  54. struct clk *usbh_clk;
  55. const struct hc_driver *driver = &ehci_spear_hc_driver;
  56. int irq, retval;
  57. if (usb_disabled())
  58. return -ENODEV;
  59. irq = platform_get_irq(pdev, 0);
  60. if (irq < 0) {
  61. retval = irq;
  62. goto fail;
  63. }
  64. /*
  65. * Right now device-tree probed devices don't get dma_mask set.
  66. * Since shared usb code relies on it, set it here for now.
  67. * Once we have dma capability bindings this can go away.
  68. */
  69. if (!pdev->dev.dma_mask)
  70. pdev->dev.dma_mask = &spear_ehci_dma_mask;
  71. usbh_clk = devm_clk_get(&pdev->dev, NULL);
  72. if (IS_ERR(usbh_clk)) {
  73. dev_err(&pdev->dev, "Error getting interface clock\n");
  74. retval = PTR_ERR(usbh_clk);
  75. goto fail;
  76. }
  77. hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev));
  78. if (!hcd) {
  79. retval = -ENOMEM;
  80. goto fail;
  81. }
  82. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  83. if (!res) {
  84. retval = -ENODEV;
  85. goto err_put_hcd;
  86. }
  87. hcd->rsrc_start = res->start;
  88. hcd->rsrc_len = resource_size(res);
  89. if (!devm_request_mem_region(&pdev->dev, hcd->rsrc_start, hcd->rsrc_len,
  90. driver->description)) {
  91. retval = -EBUSY;
  92. goto err_put_hcd;
  93. }
  94. hcd->regs = devm_ioremap(&pdev->dev, hcd->rsrc_start, hcd->rsrc_len);
  95. if (hcd->regs == NULL) {
  96. dev_dbg(&pdev->dev, "error mapping memory\n");
  97. retval = -ENOMEM;
  98. goto err_put_hcd;
  99. }
  100. sehci = to_spear_ehci(hcd);
  101. sehci->clk = usbh_clk;
  102. /* registers start at offset 0x0 */
  103. hcd_to_ehci(hcd)->caps = hcd->regs;
  104. clk_prepare_enable(sehci->clk);
  105. retval = usb_add_hcd(hcd, irq, IRQF_SHARED);
  106. if (retval)
  107. goto err_stop_ehci;
  108. return retval;
  109. err_stop_ehci:
  110. clk_disable_unprepare(sehci->clk);
  111. err_put_hcd:
  112. usb_put_hcd(hcd);
  113. fail:
  114. dev_err(&pdev->dev, "init fail, %d\n", retval);
  115. return retval ;
  116. }
  117. static int spear_ehci_hcd_drv_remove(struct platform_device *pdev)
  118. {
  119. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  120. struct spear_ehci *sehci = to_spear_ehci(hcd);
  121. if (!hcd)
  122. return 0;
  123. if (in_interrupt())
  124. BUG();
  125. usb_remove_hcd(hcd);
  126. if (sehci->clk)
  127. clk_disable_unprepare(sehci->clk);
  128. usb_put_hcd(hcd);
  129. return 0;
  130. }
  131. static struct of_device_id spear_ehci_id_table[] = {
  132. { .compatible = "st,spear600-ehci", },
  133. { },
  134. };
  135. static struct platform_driver spear_ehci_hcd_driver = {
  136. .probe = spear_ehci_hcd_drv_probe,
  137. .remove = spear_ehci_hcd_drv_remove,
  138. .shutdown = usb_hcd_platform_shutdown,
  139. .driver = {
  140. .name = "spear-ehci",
  141. .bus = &platform_bus_type,
  142. .pm = &ehci_spear_pm_ops,
  143. .of_match_table = of_match_ptr(spear_ehci_id_table),
  144. }
  145. };
  146. static const struct ehci_driver_overrides spear_overrides __initdata = {
  147. .extra_priv_size = sizeof(struct spear_ehci),
  148. };
  149. static int __init ehci_spear_init(void)
  150. {
  151. if (usb_disabled())
  152. return -ENODEV;
  153. pr_info("%s: " DRIVER_DESC "\n", hcd_name);
  154. ehci_init_driver(&ehci_spear_hc_driver, &spear_overrides);
  155. return platform_driver_register(&spear_ehci_hcd_driver);
  156. }
  157. module_init(ehci_spear_init);
  158. static void __exit ehci_spear_cleanup(void)
  159. {
  160. platform_driver_unregister(&spear_ehci_hcd_driver);
  161. }
  162. module_exit(ehci_spear_cleanup);
  163. MODULE_DESCRIPTION(DRIVER_DESC);
  164. MODULE_ALIAS("platform:spear-ehci");
  165. MODULE_AUTHOR("Deepak Sikri");
  166. MODULE_LICENSE("GPL");