ehci-atmel.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * Driver for EHCI UHP on Atmel chips
  3. *
  4. * Copyright (C) 2009 Atmel Corporation,
  5. * Nicolas Ferre <nicolas.ferre@atmel.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/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/of.h>
  19. #include <linux/of_platform.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/usb.h>
  22. #include <linux/usb/hcd.h>
  23. #include "ehci.h"
  24. #define DRIVER_DESC "EHCI Atmel driver"
  25. static const char hcd_name[] = "ehci-atmel";
  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. dev_err(&pdev->dev,
  85. "Found HC with no IRQ. Check %s setup!\n",
  86. dev_name(&pdev->dev));
  87. retval = -ENODEV;
  88. goto fail_create_hcd;
  89. }
  90. /* Right now device-tree probed devices don't get dma_mask set.
  91. * Since shared usb code relies on it, set it here for now.
  92. * Once we have dma capability bindings this can go away.
  93. */
  94. retval = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  95. if (retval)
  96. goto fail_create_hcd;
  97. hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev));
  98. if (!hcd) {
  99. retval = -ENOMEM;
  100. goto fail_create_hcd;
  101. }
  102. atmel_ehci = hcd_to_atmel_ehci_priv(hcd);
  103. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  104. hcd->regs = devm_ioremap_resource(&pdev->dev, res);
  105. if (IS_ERR(hcd->regs)) {
  106. retval = PTR_ERR(hcd->regs);
  107. goto fail_request_resource;
  108. }
  109. hcd->rsrc_start = res->start;
  110. hcd->rsrc_len = resource_size(res);
  111. atmel_ehci->iclk = devm_clk_get(&pdev->dev, "ehci_clk");
  112. if (IS_ERR(atmel_ehci->iclk)) {
  113. dev_err(&pdev->dev, "Error getting interface clock\n");
  114. retval = -ENOENT;
  115. goto fail_request_resource;
  116. }
  117. atmel_ehci->uclk = devm_clk_get(&pdev->dev, "usb_clk");
  118. if (IS_ERR(atmel_ehci->uclk)) {
  119. dev_err(&pdev->dev, "failed to get uclk\n");
  120. retval = PTR_ERR(atmel_ehci->uclk);
  121. goto fail_request_resource;
  122. }
  123. ehci = hcd_to_ehci(hcd);
  124. /* registers start at offset 0x0 */
  125. ehci->caps = hcd->regs;
  126. atmel_start_ehci(pdev);
  127. retval = usb_add_hcd(hcd, irq, IRQF_SHARED);
  128. if (retval)
  129. goto fail_add_hcd;
  130. device_wakeup_enable(hcd->self.controller);
  131. return retval;
  132. fail_add_hcd:
  133. atmel_stop_ehci(pdev);
  134. fail_request_resource:
  135. usb_put_hcd(hcd);
  136. fail_create_hcd:
  137. dev_err(&pdev->dev, "init %s fail, %d\n",
  138. dev_name(&pdev->dev), retval);
  139. return retval;
  140. }
  141. static int ehci_atmel_drv_remove(struct platform_device *pdev)
  142. {
  143. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  144. usb_remove_hcd(hcd);
  145. usb_put_hcd(hcd);
  146. atmel_stop_ehci(pdev);
  147. return 0;
  148. }
  149. static int __maybe_unused ehci_atmel_drv_suspend(struct device *dev)
  150. {
  151. struct usb_hcd *hcd = dev_get_drvdata(dev);
  152. struct atmel_ehci_priv *atmel_ehci = hcd_to_atmel_ehci_priv(hcd);
  153. int ret;
  154. ret = ehci_suspend(hcd, false);
  155. if (ret)
  156. return ret;
  157. atmel_stop_clock(atmel_ehci);
  158. return 0;
  159. }
  160. static int __maybe_unused ehci_atmel_drv_resume(struct device *dev)
  161. {
  162. struct usb_hcd *hcd = dev_get_drvdata(dev);
  163. struct atmel_ehci_priv *atmel_ehci = hcd_to_atmel_ehci_priv(hcd);
  164. atmel_start_clock(atmel_ehci);
  165. return ehci_resume(hcd, false);
  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. pr_info("%s: " DRIVER_DESC "\n", hcd_name);
  191. ehci_init_driver(&ehci_atmel_hc_driver, &ehci_atmel_drv_overrides);
  192. return platform_driver_register(&ehci_atmel_driver);
  193. }
  194. module_init(ehci_atmel_init);
  195. static void __exit ehci_atmel_cleanup(void)
  196. {
  197. platform_driver_unregister(&ehci_atmel_driver);
  198. }
  199. module_exit(ehci_atmel_cleanup);
  200. MODULE_DESCRIPTION(DRIVER_DESC);
  201. MODULE_ALIAS("platform:atmel-ehci");
  202. MODULE_AUTHOR("Nicolas Ferre");
  203. MODULE_LICENSE("GPL");