ehci-s5p.c 6.9 KB

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