ehci-atmel.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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/platform_device.h>
  15. #include <linux/of.h>
  16. #include <linux/of_platform.h>
  17. /* interface and function clocks */
  18. static struct clk *iclk, *fclk;
  19. static int clocked;
  20. /*-------------------------------------------------------------------------*/
  21. static void atmel_start_clock(void)
  22. {
  23. clk_enable(iclk);
  24. clk_enable(fclk);
  25. clocked = 1;
  26. }
  27. static void atmel_stop_clock(void)
  28. {
  29. clk_disable(fclk);
  30. clk_disable(iclk);
  31. clocked = 0;
  32. }
  33. static void atmel_start_ehci(struct platform_device *pdev)
  34. {
  35. dev_dbg(&pdev->dev, "start\n");
  36. atmel_start_clock();
  37. }
  38. static void atmel_stop_ehci(struct platform_device *pdev)
  39. {
  40. dev_dbg(&pdev->dev, "stop\n");
  41. atmel_stop_clock();
  42. }
  43. /*-------------------------------------------------------------------------*/
  44. static int ehci_atmel_setup(struct usb_hcd *hcd)
  45. {
  46. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  47. int retval;
  48. /* registers start at offset 0x0 */
  49. ehci->caps = hcd->regs;
  50. retval = ehci_setup(hcd);
  51. if (retval)
  52. return retval;
  53. ehci_port_power(ehci, 0);
  54. return retval;
  55. }
  56. static const struct hc_driver ehci_atmel_hc_driver = {
  57. .description = hcd_name,
  58. .product_desc = "Atmel EHCI UHP HS",
  59. .hcd_priv_size = sizeof(struct ehci_hcd),
  60. /* generic hardware linkage */
  61. .irq = ehci_irq,
  62. .flags = HCD_MEMORY | HCD_USB2,
  63. /* basic lifecycle operations */
  64. .reset = ehci_atmel_setup,
  65. .start = ehci_run,
  66. .stop = ehci_stop,
  67. .shutdown = ehci_shutdown,
  68. /* managing i/o requests and associated device resources */
  69. .urb_enqueue = ehci_urb_enqueue,
  70. .urb_dequeue = ehci_urb_dequeue,
  71. .endpoint_disable = ehci_endpoint_disable,
  72. .endpoint_reset = ehci_endpoint_reset,
  73. /* scheduling support */
  74. .get_frame_number = ehci_get_frame,
  75. /* root hub support */
  76. .hub_status_data = ehci_hub_status_data,
  77. .hub_control = ehci_hub_control,
  78. .bus_suspend = ehci_bus_suspend,
  79. .bus_resume = ehci_bus_resume,
  80. .relinquish_port = ehci_relinquish_port,
  81. .port_handed_over = ehci_port_handed_over,
  82. .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
  83. };
  84. static u64 at91_ehci_dma_mask = DMA_BIT_MASK(32);
  85. static int __devinit ehci_atmel_drv_probe(struct platform_device *pdev)
  86. {
  87. struct usb_hcd *hcd;
  88. const struct hc_driver *driver = &ehci_atmel_hc_driver;
  89. struct resource *res;
  90. int irq;
  91. int retval;
  92. if (usb_disabled())
  93. return -ENODEV;
  94. pr_debug("Initializing Atmel-SoC USB Host Controller\n");
  95. irq = platform_get_irq(pdev, 0);
  96. if (irq <= 0) {
  97. dev_err(&pdev->dev,
  98. "Found HC with no IRQ. Check %s setup!\n",
  99. dev_name(&pdev->dev));
  100. retval = -ENODEV;
  101. goto fail_create_hcd;
  102. }
  103. /* Right now device-tree probed devices don't get dma_mask set.
  104. * Since shared usb code relies on it, set it here for now.
  105. * Once we have dma capability bindings this can go away.
  106. */
  107. if (!pdev->dev.dma_mask)
  108. pdev->dev.dma_mask = &at91_ehci_dma_mask;
  109. hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev));
  110. if (!hcd) {
  111. retval = -ENOMEM;
  112. goto fail_create_hcd;
  113. }
  114. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  115. if (!res) {
  116. dev_err(&pdev->dev,
  117. "Found HC with no register addr. Check %s setup!\n",
  118. dev_name(&pdev->dev));
  119. retval = -ENODEV;
  120. goto fail_request_resource;
  121. }
  122. hcd->rsrc_start = res->start;
  123. hcd->rsrc_len = resource_size(res);
  124. if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len,
  125. driver->description)) {
  126. dev_dbg(&pdev->dev, "controller already in use\n");
  127. retval = -EBUSY;
  128. goto fail_request_resource;
  129. }
  130. hcd->regs = ioremap_nocache(hcd->rsrc_start, hcd->rsrc_len);
  131. if (hcd->regs == NULL) {
  132. dev_dbg(&pdev->dev, "error mapping memory\n");
  133. retval = -EFAULT;
  134. goto fail_ioremap;
  135. }
  136. iclk = clk_get(&pdev->dev, "ehci_clk");
  137. if (IS_ERR(iclk)) {
  138. dev_err(&pdev->dev, "Error getting interface clock\n");
  139. retval = -ENOENT;
  140. goto fail_get_iclk;
  141. }
  142. fclk = clk_get(&pdev->dev, "uhpck");
  143. if (IS_ERR(fclk)) {
  144. dev_err(&pdev->dev, "Error getting function clock\n");
  145. retval = -ENOENT;
  146. goto fail_get_fclk;
  147. }
  148. atmel_start_ehci(pdev);
  149. retval = usb_add_hcd(hcd, irq, IRQF_SHARED);
  150. if (retval)
  151. goto fail_add_hcd;
  152. return retval;
  153. fail_add_hcd:
  154. atmel_stop_ehci(pdev);
  155. clk_put(fclk);
  156. fail_get_fclk:
  157. clk_put(iclk);
  158. fail_get_iclk:
  159. iounmap(hcd->regs);
  160. fail_ioremap:
  161. release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
  162. fail_request_resource:
  163. usb_put_hcd(hcd);
  164. fail_create_hcd:
  165. dev_err(&pdev->dev, "init %s fail, %d\n",
  166. dev_name(&pdev->dev), retval);
  167. return retval;
  168. }
  169. static int __devexit ehci_atmel_drv_remove(struct platform_device *pdev)
  170. {
  171. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  172. ehci_shutdown(hcd);
  173. usb_remove_hcd(hcd);
  174. iounmap(hcd->regs);
  175. release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
  176. usb_put_hcd(hcd);
  177. atmel_stop_ehci(pdev);
  178. clk_put(fclk);
  179. clk_put(iclk);
  180. fclk = iclk = NULL;
  181. return 0;
  182. }
  183. #ifdef CONFIG_OF
  184. static const struct of_device_id atmel_ehci_dt_ids[] = {
  185. { .compatible = "atmel,at91sam9g45-ehci" },
  186. { /* sentinel */ }
  187. };
  188. MODULE_DEVICE_TABLE(of, atmel_ehci_dt_ids);
  189. #endif
  190. static struct platform_driver ehci_atmel_driver = {
  191. .probe = ehci_atmel_drv_probe,
  192. .remove = __devexit_p(ehci_atmel_drv_remove),
  193. .shutdown = usb_hcd_platform_shutdown,
  194. .driver = {
  195. .name = "atmel-ehci",
  196. .of_match_table = of_match_ptr(atmel_ehci_dt_ids),
  197. },
  198. };