ehci-atmel.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. if (!pdev->dev.dma_mask)
  84. pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
  85. if (!pdev->dev.coherent_dma_mask)
  86. pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
  87. hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev));
  88. if (!hcd) {
  89. retval = -ENOMEM;
  90. goto fail_create_hcd;
  91. }
  92. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  93. if (!res) {
  94. dev_err(&pdev->dev,
  95. "Found HC with no register addr. Check %s setup!\n",
  96. dev_name(&pdev->dev));
  97. retval = -ENODEV;
  98. goto fail_request_resource;
  99. }
  100. hcd->rsrc_start = res->start;
  101. hcd->rsrc_len = resource_size(res);
  102. hcd->regs = devm_ioremap_resource(&pdev->dev, res);
  103. if (IS_ERR(hcd->regs)) {
  104. retval = PTR_ERR(hcd->regs);
  105. goto fail_request_resource;
  106. }
  107. iclk = devm_clk_get(&pdev->dev, "ehci_clk");
  108. if (IS_ERR(iclk)) {
  109. dev_err(&pdev->dev, "Error getting interface clock\n");
  110. retval = -ENOENT;
  111. goto fail_request_resource;
  112. }
  113. fclk = devm_clk_get(&pdev->dev, "uhpck");
  114. if (IS_ERR(fclk)) {
  115. dev_err(&pdev->dev, "Error getting function clock\n");
  116. retval = -ENOENT;
  117. goto fail_request_resource;
  118. }
  119. if (IS_ENABLED(CONFIG_COMMON_CLK)) {
  120. uclk = devm_clk_get(&pdev->dev, "usb_clk");
  121. if (IS_ERR(uclk)) {
  122. dev_err(&pdev->dev, "failed to get uclk\n");
  123. retval = PTR_ERR(uclk);
  124. goto fail_request_resource;
  125. }
  126. }
  127. ehci = hcd_to_ehci(hcd);
  128. /* registers start at offset 0x0 */
  129. ehci->caps = hcd->regs;
  130. atmel_start_ehci(pdev);
  131. retval = usb_add_hcd(hcd, irq, IRQF_SHARED);
  132. if (retval)
  133. goto fail_add_hcd;
  134. return retval;
  135. fail_add_hcd:
  136. atmel_stop_ehci(pdev);
  137. fail_request_resource:
  138. usb_put_hcd(hcd);
  139. fail_create_hcd:
  140. dev_err(&pdev->dev, "init %s fail, %d\n",
  141. dev_name(&pdev->dev), retval);
  142. return retval;
  143. }
  144. static int ehci_atmel_drv_remove(struct platform_device *pdev)
  145. {
  146. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  147. usb_remove_hcd(hcd);
  148. usb_put_hcd(hcd);
  149. atmel_stop_ehci(pdev);
  150. fclk = iclk = NULL;
  151. return 0;
  152. }
  153. #ifdef CONFIG_OF
  154. static const struct of_device_id atmel_ehci_dt_ids[] = {
  155. { .compatible = "atmel,at91sam9g45-ehci" },
  156. { /* sentinel */ }
  157. };
  158. MODULE_DEVICE_TABLE(of, atmel_ehci_dt_ids);
  159. #endif
  160. static struct platform_driver ehci_atmel_driver = {
  161. .probe = ehci_atmel_drv_probe,
  162. .remove = ehci_atmel_drv_remove,
  163. .shutdown = usb_hcd_platform_shutdown,
  164. .driver = {
  165. .name = "atmel-ehci",
  166. .of_match_table = of_match_ptr(atmel_ehci_dt_ids),
  167. },
  168. };
  169. static int __init ehci_atmel_init(void)
  170. {
  171. if (usb_disabled())
  172. return -ENODEV;
  173. pr_info("%s: " DRIVER_DESC "\n", hcd_name);
  174. ehci_init_driver(&ehci_atmel_hc_driver, NULL);
  175. return platform_driver_register(&ehci_atmel_driver);
  176. }
  177. module_init(ehci_atmel_init);
  178. static void __exit ehci_atmel_cleanup(void)
  179. {
  180. platform_driver_unregister(&ehci_atmel_driver);
  181. }
  182. module_exit(ehci_atmel_cleanup);
  183. MODULE_DESCRIPTION(DRIVER_DESC);
  184. MODULE_ALIAS("platform:atmel-ehci");
  185. MODULE_AUTHOR("Nicolas Ferre");
  186. MODULE_LICENSE("GPL");