ehci-atmel.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. static struct hc_driver __read_mostly ehci_atmel_hc_driver;
  27. /* interface and function clocks */
  28. static struct clk *iclk, *fclk, *uclk;
  29. static int clocked;
  30. /*-------------------------------------------------------------------------*/
  31. static void atmel_start_clock(void)
  32. {
  33. if (IS_ENABLED(CONFIG_COMMON_CLK)) {
  34. clk_set_rate(uclk, 48000000);
  35. clk_prepare_enable(uclk);
  36. }
  37. clk_prepare_enable(iclk);
  38. clk_prepare_enable(fclk);
  39. clocked = 1;
  40. }
  41. static void atmel_stop_clock(void)
  42. {
  43. clk_disable_unprepare(fclk);
  44. clk_disable_unprepare(iclk);
  45. if (IS_ENABLED(CONFIG_COMMON_CLK))
  46. clk_disable_unprepare(uclk);
  47. clocked = 0;
  48. }
  49. static void atmel_start_ehci(struct platform_device *pdev)
  50. {
  51. dev_dbg(&pdev->dev, "start\n");
  52. atmel_start_clock();
  53. }
  54. static void atmel_stop_ehci(struct platform_device *pdev)
  55. {
  56. dev_dbg(&pdev->dev, "stop\n");
  57. atmel_stop_clock();
  58. }
  59. /*-------------------------------------------------------------------------*/
  60. static int ehci_atmel_drv_probe(struct platform_device *pdev)
  61. {
  62. struct usb_hcd *hcd;
  63. const struct hc_driver *driver = &ehci_atmel_hc_driver;
  64. struct resource *res;
  65. struct ehci_hcd *ehci;
  66. int irq;
  67. int retval;
  68. if (usb_disabled())
  69. return -ENODEV;
  70. pr_debug("Initializing Atmel-SoC USB Host Controller\n");
  71. irq = platform_get_irq(pdev, 0);
  72. if (irq <= 0) {
  73. dev_err(&pdev->dev,
  74. "Found HC with no IRQ. Check %s setup!\n",
  75. dev_name(&pdev->dev));
  76. retval = -ENODEV;
  77. goto fail_create_hcd;
  78. }
  79. /* Right now device-tree probed devices don't get dma_mask set.
  80. * Since shared usb code relies on it, set it here for now.
  81. * Once we have dma capability bindings this can go away.
  82. */
  83. retval = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  84. if (retval)
  85. goto fail_create_hcd;
  86. hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev));
  87. if (!hcd) {
  88. retval = -ENOMEM;
  89. goto fail_create_hcd;
  90. }
  91. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  92. hcd->regs = devm_ioremap_resource(&pdev->dev, res);
  93. if (IS_ERR(hcd->regs)) {
  94. retval = PTR_ERR(hcd->regs);
  95. goto fail_request_resource;
  96. }
  97. hcd->rsrc_start = res->start;
  98. hcd->rsrc_len = resource_size(res);
  99. iclk = devm_clk_get(&pdev->dev, "ehci_clk");
  100. if (IS_ERR(iclk)) {
  101. dev_err(&pdev->dev, "Error getting interface clock\n");
  102. retval = -ENOENT;
  103. goto fail_request_resource;
  104. }
  105. fclk = devm_clk_get(&pdev->dev, "uhpck");
  106. if (IS_ERR(fclk)) {
  107. dev_err(&pdev->dev, "Error getting function clock\n");
  108. retval = -ENOENT;
  109. goto fail_request_resource;
  110. }
  111. if (IS_ENABLED(CONFIG_COMMON_CLK)) {
  112. uclk = devm_clk_get(&pdev->dev, "usb_clk");
  113. if (IS_ERR(uclk)) {
  114. dev_err(&pdev->dev, "failed to get uclk\n");
  115. retval = PTR_ERR(uclk);
  116. goto fail_request_resource;
  117. }
  118. }
  119. ehci = hcd_to_ehci(hcd);
  120. /* registers start at offset 0x0 */
  121. ehci->caps = hcd->regs;
  122. atmel_start_ehci(pdev);
  123. retval = usb_add_hcd(hcd, irq, IRQF_SHARED);
  124. if (retval)
  125. goto fail_add_hcd;
  126. device_wakeup_enable(hcd->self.controller);
  127. return retval;
  128. fail_add_hcd:
  129. atmel_stop_ehci(pdev);
  130. fail_request_resource:
  131. usb_put_hcd(hcd);
  132. fail_create_hcd:
  133. dev_err(&pdev->dev, "init %s fail, %d\n",
  134. dev_name(&pdev->dev), retval);
  135. return retval;
  136. }
  137. static int ehci_atmel_drv_remove(struct platform_device *pdev)
  138. {
  139. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  140. usb_remove_hcd(hcd);
  141. usb_put_hcd(hcd);
  142. atmel_stop_ehci(pdev);
  143. fclk = iclk = NULL;
  144. return 0;
  145. }
  146. #ifdef CONFIG_OF
  147. static const struct of_device_id atmel_ehci_dt_ids[] = {
  148. { .compatible = "atmel,at91sam9g45-ehci" },
  149. { /* sentinel */ }
  150. };
  151. MODULE_DEVICE_TABLE(of, atmel_ehci_dt_ids);
  152. #endif
  153. static struct platform_driver ehci_atmel_driver = {
  154. .probe = ehci_atmel_drv_probe,
  155. .remove = ehci_atmel_drv_remove,
  156. .shutdown = usb_hcd_platform_shutdown,
  157. .driver = {
  158. .name = "atmel-ehci",
  159. .of_match_table = of_match_ptr(atmel_ehci_dt_ids),
  160. },
  161. };
  162. static int __init ehci_atmel_init(void)
  163. {
  164. if (usb_disabled())
  165. return -ENODEV;
  166. pr_info("%s: " DRIVER_DESC "\n", hcd_name);
  167. ehci_init_driver(&ehci_atmel_hc_driver, NULL);
  168. return platform_driver_register(&ehci_atmel_driver);
  169. }
  170. module_init(ehci_atmel_init);
  171. static void __exit ehci_atmel_cleanup(void)
  172. {
  173. platform_driver_unregister(&ehci_atmel_driver);
  174. }
  175. module_exit(ehci_atmel_cleanup);
  176. MODULE_DESCRIPTION(DRIVER_DESC);
  177. MODULE_ALIAS("platform:atmel-ehci");
  178. MODULE_AUTHOR("Nicolas Ferre");
  179. MODULE_LICENSE("GPL");