ehci-s5p.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * SAMSUNG S5P USB HOST EHCI Controller
  3. *
  4. * Copyright (C) 2011 Samsung Electronics Co.Ltd
  5. * Author: Jingoo Han <jg1.han@samsung.com>
  6. * Author: Joonyoung Shim <jy0922.shim@samsung.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. */
  14. #include <linux/clk.h>
  15. #include <linux/of.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/of_gpio.h>
  18. #include <plat/ehci.h>
  19. #include <plat/usb-phy.h>
  20. #define EHCI_INSNREG00(base) (base + 0x90)
  21. #define EHCI_INSNREG00_ENA_INCR16 (0x1 << 25)
  22. #define EHCI_INSNREG00_ENA_INCR8 (0x1 << 24)
  23. #define EHCI_INSNREG00_ENA_INCR4 (0x1 << 23)
  24. #define EHCI_INSNREG00_ENA_INCRX_ALIGN (0x1 << 22)
  25. #define EHCI_INSNREG00_ENABLE_DMA_BURST \
  26. (EHCI_INSNREG00_ENA_INCR16 | EHCI_INSNREG00_ENA_INCR8 | \
  27. EHCI_INSNREG00_ENA_INCR4 | EHCI_INSNREG00_ENA_INCRX_ALIGN)
  28. struct s5p_ehci_hcd {
  29. struct device *dev;
  30. struct usb_hcd *hcd;
  31. struct clk *clk;
  32. };
  33. static const struct hc_driver s5p_ehci_hc_driver = {
  34. .description = hcd_name,
  35. .product_desc = "S5P EHCI Host Controller",
  36. .hcd_priv_size = sizeof(struct ehci_hcd),
  37. .irq = ehci_irq,
  38. .flags = HCD_MEMORY | HCD_USB2,
  39. .reset = ehci_setup,
  40. .start = ehci_run,
  41. .stop = ehci_stop,
  42. .shutdown = ehci_shutdown,
  43. .get_frame_number = ehci_get_frame,
  44. .urb_enqueue = ehci_urb_enqueue,
  45. .urb_dequeue = ehci_urb_dequeue,
  46. .endpoint_disable = ehci_endpoint_disable,
  47. .endpoint_reset = ehci_endpoint_reset,
  48. .hub_status_data = ehci_hub_status_data,
  49. .hub_control = ehci_hub_control,
  50. .bus_suspend = ehci_bus_suspend,
  51. .bus_resume = ehci_bus_resume,
  52. .relinquish_port = ehci_relinquish_port,
  53. .port_handed_over = ehci_port_handed_over,
  54. .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
  55. };
  56. static void s5p_setup_vbus_gpio(struct platform_device *pdev)
  57. {
  58. int err;
  59. int gpio;
  60. if (!pdev->dev.of_node)
  61. return;
  62. gpio = of_get_named_gpio(pdev->dev.of_node,
  63. "samsung,vbus-gpio", 0);
  64. if (!gpio_is_valid(gpio))
  65. return;
  66. err = gpio_request_one(gpio, GPIOF_OUT_INIT_HIGH, "ehci_vbus_gpio");
  67. if (err)
  68. dev_err(&pdev->dev, "can't request ehci vbus gpio %d", gpio);
  69. }
  70. static u64 ehci_s5p_dma_mask = DMA_BIT_MASK(32);
  71. static int __devinit s5p_ehci_probe(struct platform_device *pdev)
  72. {
  73. struct s5p_ehci_platdata *pdata;
  74. struct s5p_ehci_hcd *s5p_ehci;
  75. struct usb_hcd *hcd;
  76. struct ehci_hcd *ehci;
  77. struct resource *res;
  78. int irq;
  79. int err;
  80. pdata = pdev->dev.platform_data;
  81. if (!pdata) {
  82. dev_err(&pdev->dev, "No platform data defined\n");
  83. return -EINVAL;
  84. }
  85. /*
  86. * Right now device-tree probed devices don't get dma_mask set.
  87. * Since shared usb code relies on it, set it here for now.
  88. * Once we move to full device tree support this will vanish off.
  89. */
  90. if (!pdev->dev.dma_mask)
  91. pdev->dev.dma_mask = &ehci_s5p_dma_mask;
  92. if (!pdev->dev.coherent_dma_mask)
  93. pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
  94. s5p_setup_vbus_gpio(pdev);
  95. s5p_ehci = devm_kzalloc(&pdev->dev, sizeof(struct s5p_ehci_hcd),
  96. GFP_KERNEL);
  97. if (!s5p_ehci)
  98. return -ENOMEM;
  99. s5p_ehci->dev = &pdev->dev;
  100. hcd = usb_create_hcd(&s5p_ehci_hc_driver, &pdev->dev,
  101. dev_name(&pdev->dev));
  102. if (!hcd) {
  103. dev_err(&pdev->dev, "Unable to create HCD\n");
  104. return -ENOMEM;
  105. }
  106. s5p_ehci->hcd = hcd;
  107. s5p_ehci->clk = clk_get(&pdev->dev, "usbhost");
  108. if (IS_ERR(s5p_ehci->clk)) {
  109. dev_err(&pdev->dev, "Failed to get usbhost clock\n");
  110. err = PTR_ERR(s5p_ehci->clk);
  111. goto fail_clk;
  112. }
  113. err = clk_enable(s5p_ehci->clk);
  114. if (err)
  115. goto fail_clken;
  116. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  117. if (!res) {
  118. dev_err(&pdev->dev, "Failed to get I/O memory\n");
  119. err = -ENXIO;
  120. goto fail_io;
  121. }
  122. hcd->rsrc_start = res->start;
  123. hcd->rsrc_len = resource_size(res);
  124. hcd->regs = devm_ioremap(&pdev->dev, res->start, hcd->rsrc_len);
  125. if (!hcd->regs) {
  126. dev_err(&pdev->dev, "Failed to remap I/O memory\n");
  127. err = -ENOMEM;
  128. goto fail_io;
  129. }
  130. irq = platform_get_irq(pdev, 0);
  131. if (!irq) {
  132. dev_err(&pdev->dev, "Failed to get IRQ\n");
  133. err = -ENODEV;
  134. goto fail_io;
  135. }
  136. if (pdata->phy_init)
  137. pdata->phy_init(pdev, S5P_USB_PHY_HOST);
  138. ehci = hcd_to_ehci(hcd);
  139. ehci->caps = hcd->regs;
  140. /* DMA burst Enable */
  141. writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
  142. err = usb_add_hcd(hcd, irq, IRQF_SHARED);
  143. if (err) {
  144. dev_err(&pdev->dev, "Failed to add USB HCD\n");
  145. goto fail_io;
  146. }
  147. platform_set_drvdata(pdev, s5p_ehci);
  148. return 0;
  149. fail_io:
  150. clk_disable(s5p_ehci->clk);
  151. fail_clken:
  152. clk_put(s5p_ehci->clk);
  153. fail_clk:
  154. usb_put_hcd(hcd);
  155. return err;
  156. }
  157. static int __devexit s5p_ehci_remove(struct platform_device *pdev)
  158. {
  159. struct s5p_ehci_platdata *pdata = pdev->dev.platform_data;
  160. struct s5p_ehci_hcd *s5p_ehci = platform_get_drvdata(pdev);
  161. struct usb_hcd *hcd = s5p_ehci->hcd;
  162. usb_remove_hcd(hcd);
  163. if (pdata && pdata->phy_exit)
  164. pdata->phy_exit(pdev, S5P_USB_PHY_HOST);
  165. clk_disable(s5p_ehci->clk);
  166. clk_put(s5p_ehci->clk);
  167. usb_put_hcd(hcd);
  168. return 0;
  169. }
  170. static void s5p_ehci_shutdown(struct platform_device *pdev)
  171. {
  172. struct s5p_ehci_hcd *s5p_ehci = platform_get_drvdata(pdev);
  173. struct usb_hcd *hcd = s5p_ehci->hcd;
  174. if (hcd->driver->shutdown)
  175. hcd->driver->shutdown(hcd);
  176. }
  177. #ifdef CONFIG_PM
  178. static int s5p_ehci_suspend(struct device *dev)
  179. {
  180. struct s5p_ehci_hcd *s5p_ehci = dev_get_drvdata(dev);
  181. struct usb_hcd *hcd = s5p_ehci->hcd;
  182. bool do_wakeup = device_may_wakeup(dev);
  183. struct platform_device *pdev = to_platform_device(dev);
  184. struct s5p_ehci_platdata *pdata = pdev->dev.platform_data;
  185. int rc;
  186. rc = ehci_suspend(hcd, do_wakeup);
  187. if (pdata && pdata->phy_exit)
  188. pdata->phy_exit(pdev, S5P_USB_PHY_HOST);
  189. clk_disable(s5p_ehci->clk);
  190. return rc;
  191. }
  192. static int s5p_ehci_resume(struct device *dev)
  193. {
  194. struct s5p_ehci_hcd *s5p_ehci = dev_get_drvdata(dev);
  195. struct usb_hcd *hcd = s5p_ehci->hcd;
  196. struct platform_device *pdev = to_platform_device(dev);
  197. struct s5p_ehci_platdata *pdata = pdev->dev.platform_data;
  198. clk_enable(s5p_ehci->clk);
  199. if (pdata && pdata->phy_init)
  200. pdata->phy_init(pdev, S5P_USB_PHY_HOST);
  201. /* DMA burst Enable */
  202. writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
  203. ehci_resume(hcd, false);
  204. return 0;
  205. }
  206. #else
  207. #define s5p_ehci_suspend NULL
  208. #define s5p_ehci_resume NULL
  209. #endif
  210. static const struct dev_pm_ops s5p_ehci_pm_ops = {
  211. .suspend = s5p_ehci_suspend,
  212. .resume = s5p_ehci_resume,
  213. };
  214. #ifdef CONFIG_OF
  215. static const struct of_device_id exynos_ehci_match[] = {
  216. { .compatible = "samsung,exynos-ehci" },
  217. {},
  218. };
  219. MODULE_DEVICE_TABLE(of, exynos_ehci_match);
  220. #endif
  221. static struct platform_driver s5p_ehci_driver = {
  222. .probe = s5p_ehci_probe,
  223. .remove = __devexit_p(s5p_ehci_remove),
  224. .shutdown = s5p_ehci_shutdown,
  225. .driver = {
  226. .name = "s5p-ehci",
  227. .owner = THIS_MODULE,
  228. .pm = &s5p_ehci_pm_ops,
  229. .of_match_table = of_match_ptr(exynos_ehci_match),
  230. }
  231. };
  232. MODULE_ALIAS("platform:s5p-ehci");