ehci-atmel.c 5.9 KB

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