ehci-s5p.c 7.7 KB

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