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