ehci-atmel.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Driver for EHCI UHP on Atmel chips
  4. *
  5. * Copyright (C) 2009 Atmel Corporation,
  6. * Nicolas Ferre <nicolas.ferre@atmel.com>
  7. *
  8. * Based on various ehci-*.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/of_platform.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/usb.h>
  19. #include <linux/usb/hcd.h>
  20. #include "ehci.h"
  21. #define DRIVER_DESC "EHCI Atmel driver"
  22. static const char hcd_name[] = "ehci-atmel";
  23. /* interface and function clocks */
  24. #define hcd_to_atmel_ehci_priv(h) \
  25. ((struct atmel_ehci_priv *)hcd_to_ehci(h)->priv)
  26. struct atmel_ehci_priv {
  27. struct clk *iclk;
  28. struct clk *uclk;
  29. bool clocked;
  30. };
  31. static struct hc_driver __read_mostly ehci_atmel_hc_driver;
  32. static const struct ehci_driver_overrides ehci_atmel_drv_overrides __initconst = {
  33. .extra_priv_size = sizeof(struct atmel_ehci_priv),
  34. };
  35. /*-------------------------------------------------------------------------*/
  36. static void atmel_start_clock(struct atmel_ehci_priv *atmel_ehci)
  37. {
  38. if (atmel_ehci->clocked)
  39. return;
  40. clk_prepare_enable(atmel_ehci->uclk);
  41. clk_prepare_enable(atmel_ehci->iclk);
  42. atmel_ehci->clocked = true;
  43. }
  44. static void atmel_stop_clock(struct atmel_ehci_priv *atmel_ehci)
  45. {
  46. if (!atmel_ehci->clocked)
  47. return;
  48. clk_disable_unprepare(atmel_ehci->iclk);
  49. clk_disable_unprepare(atmel_ehci->uclk);
  50. atmel_ehci->clocked = false;
  51. }
  52. static void atmel_start_ehci(struct platform_device *pdev)
  53. {
  54. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  55. struct atmel_ehci_priv *atmel_ehci = hcd_to_atmel_ehci_priv(hcd);
  56. dev_dbg(&pdev->dev, "start\n");
  57. atmel_start_clock(atmel_ehci);
  58. }
  59. static void atmel_stop_ehci(struct platform_device *pdev)
  60. {
  61. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  62. struct atmel_ehci_priv *atmel_ehci = hcd_to_atmel_ehci_priv(hcd);
  63. dev_dbg(&pdev->dev, "stop\n");
  64. atmel_stop_clock(atmel_ehci);
  65. }
  66. /*-------------------------------------------------------------------------*/
  67. static int ehci_atmel_drv_probe(struct platform_device *pdev)
  68. {
  69. struct usb_hcd *hcd;
  70. const struct hc_driver *driver = &ehci_atmel_hc_driver;
  71. struct resource *res;
  72. struct ehci_hcd *ehci;
  73. struct atmel_ehci_priv *atmel_ehci;
  74. int irq;
  75. int retval;
  76. if (usb_disabled())
  77. return -ENODEV;
  78. pr_debug("Initializing Atmel-SoC USB Host Controller\n");
  79. irq = platform_get_irq(pdev, 0);
  80. if (irq <= 0) {
  81. dev_err(&pdev->dev,
  82. "Found HC with no IRQ. Check %s setup!\n",
  83. dev_name(&pdev->dev));
  84. retval = -ENODEV;
  85. goto fail_create_hcd;
  86. }
  87. /* Right now device-tree probed devices don't get dma_mask set.
  88. * Since shared usb code relies on it, set it here for now.
  89. * Once we have dma capability bindings this can go away.
  90. */
  91. retval = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  92. if (retval)
  93. goto fail_create_hcd;
  94. hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev));
  95. if (!hcd) {
  96. retval = -ENOMEM;
  97. goto fail_create_hcd;
  98. }
  99. atmel_ehci = hcd_to_atmel_ehci_priv(hcd);
  100. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  101. hcd->regs = devm_ioremap_resource(&pdev->dev, res);
  102. if (IS_ERR(hcd->regs)) {
  103. retval = PTR_ERR(hcd->regs);
  104. goto fail_request_resource;
  105. }
  106. hcd->rsrc_start = res->start;
  107. hcd->rsrc_len = resource_size(res);
  108. atmel_ehci->iclk = devm_clk_get(&pdev->dev, "ehci_clk");
  109. if (IS_ERR(atmel_ehci->iclk)) {
  110. dev_err(&pdev->dev, "Error getting interface clock\n");
  111. retval = -ENOENT;
  112. goto fail_request_resource;
  113. }
  114. atmel_ehci->uclk = devm_clk_get(&pdev->dev, "usb_clk");
  115. if (IS_ERR(atmel_ehci->uclk)) {
  116. dev_err(&pdev->dev, "failed to get uclk\n");
  117. retval = PTR_ERR(atmel_ehci->uclk);
  118. goto fail_request_resource;
  119. }
  120. ehci = hcd_to_ehci(hcd);
  121. /* registers start at offset 0x0 */
  122. ehci->caps = hcd->regs;
  123. atmel_start_ehci(pdev);
  124. retval = usb_add_hcd(hcd, irq, IRQF_SHARED);
  125. if (retval)
  126. goto fail_add_hcd;
  127. device_wakeup_enable(hcd->self.controller);
  128. return retval;
  129. fail_add_hcd:
  130. atmel_stop_ehci(pdev);
  131. fail_request_resource:
  132. usb_put_hcd(hcd);
  133. fail_create_hcd:
  134. dev_err(&pdev->dev, "init %s fail, %d\n",
  135. dev_name(&pdev->dev), retval);
  136. return retval;
  137. }
  138. static int ehci_atmel_drv_remove(struct platform_device *pdev)
  139. {
  140. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  141. usb_remove_hcd(hcd);
  142. usb_put_hcd(hcd);
  143. atmel_stop_ehci(pdev);
  144. return 0;
  145. }
  146. static int __maybe_unused ehci_atmel_drv_suspend(struct device *dev)
  147. {
  148. struct usb_hcd *hcd = dev_get_drvdata(dev);
  149. struct atmel_ehci_priv *atmel_ehci = hcd_to_atmel_ehci_priv(hcd);
  150. int ret;
  151. ret = ehci_suspend(hcd, false);
  152. if (ret)
  153. return ret;
  154. atmel_stop_clock(atmel_ehci);
  155. return 0;
  156. }
  157. static int __maybe_unused ehci_atmel_drv_resume(struct device *dev)
  158. {
  159. struct usb_hcd *hcd = dev_get_drvdata(dev);
  160. struct atmel_ehci_priv *atmel_ehci = hcd_to_atmel_ehci_priv(hcd);
  161. atmel_start_clock(atmel_ehci);
  162. ehci_resume(hcd, false);
  163. return 0;
  164. }
  165. #ifdef CONFIG_OF
  166. static const struct of_device_id atmel_ehci_dt_ids[] = {
  167. { .compatible = "atmel,at91sam9g45-ehci" },
  168. { /* sentinel */ }
  169. };
  170. MODULE_DEVICE_TABLE(of, atmel_ehci_dt_ids);
  171. #endif
  172. static SIMPLE_DEV_PM_OPS(ehci_atmel_pm_ops, ehci_atmel_drv_suspend,
  173. ehci_atmel_drv_resume);
  174. static struct platform_driver ehci_atmel_driver = {
  175. .probe = ehci_atmel_drv_probe,
  176. .remove = ehci_atmel_drv_remove,
  177. .shutdown = usb_hcd_platform_shutdown,
  178. .driver = {
  179. .name = "atmel-ehci",
  180. .pm = &ehci_atmel_pm_ops,
  181. .of_match_table = of_match_ptr(atmel_ehci_dt_ids),
  182. },
  183. };
  184. static int __init ehci_atmel_init(void)
  185. {
  186. if (usb_disabled())
  187. return -ENODEV;
  188. pr_info("%s: " DRIVER_DESC "\n", hcd_name);
  189. ehci_init_driver(&ehci_atmel_hc_driver, &ehci_atmel_drv_overrides);
  190. return platform_driver_register(&ehci_atmel_driver);
  191. }
  192. module_init(ehci_atmel_init);
  193. static void __exit ehci_atmel_cleanup(void)
  194. {
  195. platform_driver_unregister(&ehci_atmel_driver);
  196. }
  197. module_exit(ehci_atmel_cleanup);
  198. MODULE_DESCRIPTION(DRIVER_DESC);
  199. MODULE_ALIAS("platform:atmel-ehci");
  200. MODULE_AUTHOR("Nicolas Ferre");
  201. MODULE_LICENSE("GPL");