ehci-s5p.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. struct device *dev = &pdev->dev;
  80. int err;
  81. int gpio;
  82. if (!dev->of_node)
  83. return;
  84. gpio = of_get_named_gpio(dev->of_node, "samsung,vbus-gpio", 0);
  85. if (!gpio_is_valid(gpio))
  86. return;
  87. err = devm_gpio_request_one(dev, gpio, GPIOF_OUT_INIT_HIGH,
  88. "ehci_vbus_gpio");
  89. if (err)
  90. dev_err(dev, "can't request ehci vbus gpio %d", gpio);
  91. }
  92. static u64 ehci_s5p_dma_mask = DMA_BIT_MASK(32);
  93. static int s5p_ehci_probe(struct platform_device *pdev)
  94. {
  95. struct s5p_ehci_platdata *pdata = pdev->dev.platform_data;
  96. struct s5p_ehci_hcd *s5p_ehci;
  97. struct usb_hcd *hcd;
  98. struct ehci_hcd *ehci;
  99. struct resource *res;
  100. struct usb_phy *phy;
  101. int irq;
  102. int err;
  103. /*
  104. * Right now device-tree probed devices don't get dma_mask set.
  105. * Since shared usb code relies on it, set it here for now.
  106. * Once we move to full device tree support this will vanish off.
  107. */
  108. if (!pdev->dev.dma_mask)
  109. pdev->dev.dma_mask = &ehci_s5p_dma_mask;
  110. if (!pdev->dev.coherent_dma_mask)
  111. pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
  112. s5p_setup_vbus_gpio(pdev);
  113. s5p_ehci = devm_kzalloc(&pdev->dev, sizeof(struct s5p_ehci_hcd),
  114. GFP_KERNEL);
  115. if (!s5p_ehci)
  116. return -ENOMEM;
  117. phy = devm_usb_get_phy(&pdev->dev, USB_PHY_TYPE_USB2);
  118. if (IS_ERR_OR_NULL(phy)) {
  119. /* Fallback to pdata */
  120. if (!pdata) {
  121. dev_warn(&pdev->dev, "no platform data or transceiver defined\n");
  122. return -EPROBE_DEFER;
  123. } else {
  124. s5p_ehci->pdata = pdata;
  125. }
  126. } else {
  127. s5p_ehci->phy = phy;
  128. s5p_ehci->otg = phy->otg;
  129. }
  130. s5p_ehci->dev = &pdev->dev;
  131. hcd = usb_create_hcd(&s5p_ehci_hc_driver, &pdev->dev,
  132. dev_name(&pdev->dev));
  133. if (!hcd) {
  134. dev_err(&pdev->dev, "Unable to create HCD\n");
  135. return -ENOMEM;
  136. }
  137. s5p_ehci->hcd = hcd;
  138. s5p_ehci->clk = devm_clk_get(&pdev->dev, "usbhost");
  139. if (IS_ERR(s5p_ehci->clk)) {
  140. dev_err(&pdev->dev, "Failed to get usbhost clock\n");
  141. err = PTR_ERR(s5p_ehci->clk);
  142. goto fail_clk;
  143. }
  144. err = clk_prepare_enable(s5p_ehci->clk);
  145. if (err)
  146. goto fail_clk;
  147. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  148. if (!res) {
  149. dev_err(&pdev->dev, "Failed to get I/O memory\n");
  150. err = -ENXIO;
  151. goto fail_io;
  152. }
  153. hcd->rsrc_start = res->start;
  154. hcd->rsrc_len = resource_size(res);
  155. hcd->regs = devm_ioremap(&pdev->dev, res->start, hcd->rsrc_len);
  156. if (!hcd->regs) {
  157. dev_err(&pdev->dev, "Failed to remap I/O memory\n");
  158. err = -ENOMEM;
  159. goto fail_io;
  160. }
  161. irq = platform_get_irq(pdev, 0);
  162. if (!irq) {
  163. dev_err(&pdev->dev, "Failed to get IRQ\n");
  164. err = -ENODEV;
  165. goto fail_io;
  166. }
  167. if (s5p_ehci->otg)
  168. s5p_ehci->otg->set_host(s5p_ehci->otg, &s5p_ehci->hcd->self);
  169. s5p_ehci_phy_enable(s5p_ehci);
  170. ehci = hcd_to_ehci(hcd);
  171. ehci->caps = hcd->regs;
  172. /* DMA burst Enable */
  173. writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
  174. err = usb_add_hcd(hcd, irq, IRQF_SHARED);
  175. if (err) {
  176. dev_err(&pdev->dev, "Failed to add USB HCD\n");
  177. goto fail_add_hcd;
  178. }
  179. platform_set_drvdata(pdev, s5p_ehci);
  180. return 0;
  181. fail_add_hcd:
  182. s5p_ehci_phy_disable(s5p_ehci);
  183. fail_io:
  184. clk_disable_unprepare(s5p_ehci->clk);
  185. fail_clk:
  186. usb_put_hcd(hcd);
  187. return err;
  188. }
  189. static int s5p_ehci_remove(struct platform_device *pdev)
  190. {
  191. struct s5p_ehci_hcd *s5p_ehci = platform_get_drvdata(pdev);
  192. struct usb_hcd *hcd = s5p_ehci->hcd;
  193. usb_remove_hcd(hcd);
  194. if (s5p_ehci->otg)
  195. s5p_ehci->otg->set_host(s5p_ehci->otg, &s5p_ehci->hcd->self);
  196. s5p_ehci_phy_disable(s5p_ehci);
  197. clk_disable_unprepare(s5p_ehci->clk);
  198. usb_put_hcd(hcd);
  199. return 0;
  200. }
  201. static void s5p_ehci_shutdown(struct platform_device *pdev)
  202. {
  203. struct s5p_ehci_hcd *s5p_ehci = platform_get_drvdata(pdev);
  204. struct usb_hcd *hcd = s5p_ehci->hcd;
  205. if (hcd->driver->shutdown)
  206. hcd->driver->shutdown(hcd);
  207. }
  208. #ifdef CONFIG_PM
  209. static int s5p_ehci_suspend(struct device *dev)
  210. {
  211. struct s5p_ehci_hcd *s5p_ehci = dev_get_drvdata(dev);
  212. struct usb_hcd *hcd = s5p_ehci->hcd;
  213. bool do_wakeup = device_may_wakeup(dev);
  214. int rc;
  215. rc = ehci_suspend(hcd, do_wakeup);
  216. if (s5p_ehci->otg)
  217. s5p_ehci->otg->set_host(s5p_ehci->otg, &s5p_ehci->hcd->self);
  218. s5p_ehci_phy_disable(s5p_ehci);
  219. clk_disable_unprepare(s5p_ehci->clk);
  220. return rc;
  221. }
  222. static int s5p_ehci_resume(struct device *dev)
  223. {
  224. struct s5p_ehci_hcd *s5p_ehci = dev_get_drvdata(dev);
  225. struct usb_hcd *hcd = s5p_ehci->hcd;
  226. clk_prepare_enable(s5p_ehci->clk);
  227. if (s5p_ehci->otg)
  228. s5p_ehci->otg->set_host(s5p_ehci->otg, &s5p_ehci->hcd->self);
  229. s5p_ehci_phy_enable(s5p_ehci);
  230. /* DMA burst Enable */
  231. writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
  232. ehci_resume(hcd, false);
  233. return 0;
  234. }
  235. #else
  236. #define s5p_ehci_suspend NULL
  237. #define s5p_ehci_resume NULL
  238. #endif
  239. static const struct dev_pm_ops s5p_ehci_pm_ops = {
  240. .suspend = s5p_ehci_suspend,
  241. .resume = s5p_ehci_resume,
  242. };
  243. #ifdef CONFIG_OF
  244. static const struct of_device_id exynos_ehci_match[] = {
  245. { .compatible = "samsung,exynos4210-ehci" },
  246. {},
  247. };
  248. MODULE_DEVICE_TABLE(of, exynos_ehci_match);
  249. #endif
  250. static struct platform_driver s5p_ehci_driver = {
  251. .probe = s5p_ehci_probe,
  252. .remove = s5p_ehci_remove,
  253. .shutdown = s5p_ehci_shutdown,
  254. .driver = {
  255. .name = "s5p-ehci",
  256. .owner = THIS_MODULE,
  257. .pm = &s5p_ehci_pm_ops,
  258. .of_match_table = of_match_ptr(exynos_ehci_match),
  259. }
  260. };
  261. MODULE_ALIAS("platform:s5p-ehci");