ehci-s5p.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 <linux/platform_data/usb-ehci-s5p.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 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 = devm_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_prepare_enable(s5p_ehci->clk);
  114. if (err)
  115. goto fail_clk;
  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_unprepare(s5p_ehci->clk);
  151. fail_clk:
  152. usb_put_hcd(hcd);
  153. return err;
  154. }
  155. static int s5p_ehci_remove(struct platform_device *pdev)
  156. {
  157. struct s5p_ehci_platdata *pdata = pdev->dev.platform_data;
  158. struct s5p_ehci_hcd *s5p_ehci = platform_get_drvdata(pdev);
  159. struct usb_hcd *hcd = s5p_ehci->hcd;
  160. usb_remove_hcd(hcd);
  161. if (pdata && pdata->phy_exit)
  162. pdata->phy_exit(pdev, S5P_USB_PHY_HOST);
  163. clk_disable_unprepare(s5p_ehci->clk);
  164. usb_put_hcd(hcd);
  165. return 0;
  166. }
  167. static void s5p_ehci_shutdown(struct platform_device *pdev)
  168. {
  169. struct s5p_ehci_hcd *s5p_ehci = platform_get_drvdata(pdev);
  170. struct usb_hcd *hcd = s5p_ehci->hcd;
  171. if (hcd->driver->shutdown)
  172. hcd->driver->shutdown(hcd);
  173. }
  174. #ifdef CONFIG_PM
  175. static int s5p_ehci_suspend(struct device *dev)
  176. {
  177. struct s5p_ehci_hcd *s5p_ehci = dev_get_drvdata(dev);
  178. struct usb_hcd *hcd = s5p_ehci->hcd;
  179. bool do_wakeup = device_may_wakeup(dev);
  180. struct platform_device *pdev = to_platform_device(dev);
  181. struct s5p_ehci_platdata *pdata = pdev->dev.platform_data;
  182. int rc;
  183. rc = ehci_suspend(hcd, do_wakeup);
  184. if (pdata && pdata->phy_exit)
  185. pdata->phy_exit(pdev, S5P_USB_PHY_HOST);
  186. clk_disable_unprepare(s5p_ehci->clk);
  187. return rc;
  188. }
  189. static int s5p_ehci_resume(struct device *dev)
  190. {
  191. struct s5p_ehci_hcd *s5p_ehci = dev_get_drvdata(dev);
  192. struct usb_hcd *hcd = s5p_ehci->hcd;
  193. struct platform_device *pdev = to_platform_device(dev);
  194. struct s5p_ehci_platdata *pdata = pdev->dev.platform_data;
  195. clk_prepare_enable(s5p_ehci->clk);
  196. if (pdata && pdata->phy_init)
  197. pdata->phy_init(pdev, S5P_USB_PHY_HOST);
  198. /* DMA burst Enable */
  199. writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
  200. ehci_resume(hcd, false);
  201. return 0;
  202. }
  203. #else
  204. #define s5p_ehci_suspend NULL
  205. #define s5p_ehci_resume NULL
  206. #endif
  207. static const struct dev_pm_ops s5p_ehci_pm_ops = {
  208. .suspend = s5p_ehci_suspend,
  209. .resume = s5p_ehci_resume,
  210. };
  211. #ifdef CONFIG_OF
  212. static const struct of_device_id exynos_ehci_match[] = {
  213. { .compatible = "samsung,exynos-ehci" },
  214. {},
  215. };
  216. MODULE_DEVICE_TABLE(of, exynos_ehci_match);
  217. #endif
  218. static struct platform_driver s5p_ehci_driver = {
  219. .probe = s5p_ehci_probe,
  220. .remove = s5p_ehci_remove,
  221. .shutdown = s5p_ehci_shutdown,
  222. .driver = {
  223. .name = "s5p-ehci",
  224. .owner = THIS_MODULE,
  225. .pm = &s5p_ehci_pm_ops,
  226. .of_match_table = of_match_ptr(exynos_ehci_match),
  227. }
  228. };
  229. MODULE_ALIAS("platform:s5p-ehci");