ehci-atmel.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 <[email protected]>
  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 <linux/usb/phy.h>
  21. #include <linux/usb/of.h>
  22. #include "ehci.h"
  23. #define DRIVER_DESC "EHCI Atmel driver"
  24. #define EHCI_INSNREG(index) ((index) * 4 + 0x90)
  25. #define EHCI_INSNREG08_HSIC_EN BIT(2)
  26. /* interface and function clocks */
  27. #define hcd_to_atmel_ehci_priv(h) \
  28. ((struct atmel_ehci_priv *)hcd_to_ehci(h)->priv)
  29. struct atmel_ehci_priv {
  30. struct clk *iclk;
  31. struct clk *uclk;
  32. bool clocked;
  33. };
  34. static struct hc_driver __read_mostly ehci_atmel_hc_driver;
  35. static const struct ehci_driver_overrides ehci_atmel_drv_overrides __initconst = {
  36. .extra_priv_size = sizeof(struct atmel_ehci_priv),
  37. };
  38. /*-------------------------------------------------------------------------*/
  39. static void atmel_start_clock(struct atmel_ehci_priv *atmel_ehci)
  40. {
  41. if (atmel_ehci->clocked)
  42. return;
  43. clk_prepare_enable(atmel_ehci->uclk);
  44. clk_prepare_enable(atmel_ehci->iclk);
  45. atmel_ehci->clocked = true;
  46. }
  47. static void atmel_stop_clock(struct atmel_ehci_priv *atmel_ehci)
  48. {
  49. if (!atmel_ehci->clocked)
  50. return;
  51. clk_disable_unprepare(atmel_ehci->iclk);
  52. clk_disable_unprepare(atmel_ehci->uclk);
  53. atmel_ehci->clocked = false;
  54. }
  55. static void atmel_start_ehci(struct platform_device *pdev)
  56. {
  57. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  58. struct atmel_ehci_priv *atmel_ehci = hcd_to_atmel_ehci_priv(hcd);
  59. dev_dbg(&pdev->dev, "start\n");
  60. atmel_start_clock(atmel_ehci);
  61. }
  62. static void atmel_stop_ehci(struct platform_device *pdev)
  63. {
  64. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  65. struct atmel_ehci_priv *atmel_ehci = hcd_to_atmel_ehci_priv(hcd);
  66. dev_dbg(&pdev->dev, "stop\n");
  67. atmel_stop_clock(atmel_ehci);
  68. }
  69. /*-------------------------------------------------------------------------*/
  70. static int ehci_atmel_drv_probe(struct platform_device *pdev)
  71. {
  72. struct usb_hcd *hcd;
  73. const struct hc_driver *driver = &ehci_atmel_hc_driver;
  74. struct resource *res;
  75. struct ehci_hcd *ehci;
  76. struct atmel_ehci_priv *atmel_ehci;
  77. int irq;
  78. int retval;
  79. if (usb_disabled())
  80. return -ENODEV;
  81. pr_debug("Initializing Atmel-SoC USB Host Controller\n");
  82. irq = platform_get_irq(pdev, 0);
  83. if (irq <= 0) {
  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. if (of_usb_get_phy_mode(pdev->dev.of_node) == USBPHY_INTERFACE_MODE_HSIC)
  129. writel(EHCI_INSNREG08_HSIC_EN, hcd->regs + EHCI_INSNREG(8));
  130. return retval;
  131. fail_add_hcd:
  132. atmel_stop_ehci(pdev);
  133. fail_request_resource:
  134. usb_put_hcd(hcd);
  135. fail_create_hcd:
  136. dev_err(&pdev->dev, "init %s fail, %d\n",
  137. dev_name(&pdev->dev), retval);
  138. return retval;
  139. }
  140. static int ehci_atmel_drv_remove(struct platform_device *pdev)
  141. {
  142. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  143. usb_remove_hcd(hcd);
  144. usb_put_hcd(hcd);
  145. atmel_stop_ehci(pdev);
  146. return 0;
  147. }
  148. static int __maybe_unused ehci_atmel_drv_suspend(struct device *dev)
  149. {
  150. struct usb_hcd *hcd = dev_get_drvdata(dev);
  151. struct atmel_ehci_priv *atmel_ehci = hcd_to_atmel_ehci_priv(hcd);
  152. int ret;
  153. ret = ehci_suspend(hcd, false);
  154. if (ret)
  155. return ret;
  156. atmel_stop_clock(atmel_ehci);
  157. return 0;
  158. }
  159. static int __maybe_unused ehci_atmel_drv_resume(struct device *dev)
  160. {
  161. struct usb_hcd *hcd = dev_get_drvdata(dev);
  162. struct atmel_ehci_priv *atmel_ehci = hcd_to_atmel_ehci_priv(hcd);
  163. atmel_start_clock(atmel_ehci);
  164. ehci_resume(hcd, false);
  165. return 0;
  166. }
  167. #ifdef CONFIG_OF
  168. static const struct of_device_id atmel_ehci_dt_ids[] = {
  169. { .compatible = "atmel,at91sam9g45-ehci" },
  170. { /* sentinel */ }
  171. };
  172. MODULE_DEVICE_TABLE(of, atmel_ehci_dt_ids);
  173. #endif
  174. static SIMPLE_DEV_PM_OPS(ehci_atmel_pm_ops, ehci_atmel_drv_suspend,
  175. ehci_atmel_drv_resume);
  176. static struct platform_driver ehci_atmel_driver = {
  177. .probe = ehci_atmel_drv_probe,
  178. .remove = ehci_atmel_drv_remove,
  179. .shutdown = usb_hcd_platform_shutdown,
  180. .driver = {
  181. .name = "atmel-ehci",
  182. .pm = &ehci_atmel_pm_ops,
  183. .of_match_table = of_match_ptr(atmel_ehci_dt_ids),
  184. },
  185. };
  186. static int __init ehci_atmel_init(void)
  187. {
  188. if (usb_disabled())
  189. return -ENODEV;
  190. ehci_init_driver(&ehci_atmel_hc_driver, &ehci_atmel_drv_overrides);
  191. return platform_driver_register(&ehci_atmel_driver);
  192. }
  193. module_init(ehci_atmel_init);
  194. static void __exit ehci_atmel_cleanup(void)
  195. {
  196. platform_driver_unregister(&ehci_atmel_driver);
  197. }
  198. module_exit(ehci_atmel_cleanup);
  199. MODULE_DESCRIPTION(DRIVER_DESC);
  200. MODULE_ALIAS("platform:atmel-ehci");
  201. MODULE_AUTHOR("Nicolas Ferre");
  202. MODULE_LICENSE("GPL");