ehci-mv.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2011 Marvell International Ltd. All rights reserved.
  4. * Author: Chao Xie <chao.xie@marvell.com>
  5. * Neil Zhang <zhangwm@marvell.com>
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/clk.h>
  11. #include <linux/err.h>
  12. #include <linux/usb/otg.h>
  13. #include <linux/platform_data/mv_usb.h>
  14. #define CAPLENGTH_MASK (0xff)
  15. struct ehci_hcd_mv {
  16. struct usb_hcd *hcd;
  17. /* Which mode does this ehci running OTG/Host ? */
  18. int mode;
  19. void __iomem *phy_regs;
  20. void __iomem *cap_regs;
  21. void __iomem *op_regs;
  22. struct usb_phy *otg;
  23. struct mv_usb_platform_data *pdata;
  24. struct clk *clk;
  25. };
  26. static void ehci_clock_enable(struct ehci_hcd_mv *ehci_mv)
  27. {
  28. clk_prepare_enable(ehci_mv->clk);
  29. }
  30. static void ehci_clock_disable(struct ehci_hcd_mv *ehci_mv)
  31. {
  32. clk_disable_unprepare(ehci_mv->clk);
  33. }
  34. static int mv_ehci_enable(struct ehci_hcd_mv *ehci_mv)
  35. {
  36. int retval;
  37. ehci_clock_enable(ehci_mv);
  38. if (ehci_mv->pdata->phy_init) {
  39. retval = ehci_mv->pdata->phy_init(ehci_mv->phy_regs);
  40. if (retval)
  41. return retval;
  42. }
  43. return 0;
  44. }
  45. static void mv_ehci_disable(struct ehci_hcd_mv *ehci_mv)
  46. {
  47. if (ehci_mv->pdata->phy_deinit)
  48. ehci_mv->pdata->phy_deinit(ehci_mv->phy_regs);
  49. ehci_clock_disable(ehci_mv);
  50. }
  51. static int mv_ehci_reset(struct usb_hcd *hcd)
  52. {
  53. struct device *dev = hcd->self.controller;
  54. struct ehci_hcd_mv *ehci_mv = dev_get_drvdata(dev);
  55. int retval;
  56. if (ehci_mv == NULL) {
  57. dev_err(dev, "Can not find private ehci data\n");
  58. return -ENODEV;
  59. }
  60. hcd->has_tt = 1;
  61. retval = ehci_setup(hcd);
  62. if (retval)
  63. dev_err(dev, "ehci_setup failed %d\n", retval);
  64. return retval;
  65. }
  66. static const struct hc_driver mv_ehci_hc_driver = {
  67. .description = hcd_name,
  68. .product_desc = "Marvell EHCI",
  69. .hcd_priv_size = sizeof(struct ehci_hcd),
  70. /*
  71. * generic hardware linkage
  72. */
  73. .irq = ehci_irq,
  74. .flags = HCD_MEMORY | HCD_USB2 | HCD_BH,
  75. /*
  76. * basic lifecycle operations
  77. */
  78. .reset = mv_ehci_reset,
  79. .start = ehci_run,
  80. .stop = ehci_stop,
  81. .shutdown = ehci_shutdown,
  82. /*
  83. * managing i/o requests and associated device resources
  84. */
  85. .urb_enqueue = ehci_urb_enqueue,
  86. .urb_dequeue = ehci_urb_dequeue,
  87. .endpoint_disable = ehci_endpoint_disable,
  88. .endpoint_reset = ehci_endpoint_reset,
  89. .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
  90. /*
  91. * scheduling support
  92. */
  93. .get_frame_number = ehci_get_frame,
  94. /*
  95. * root hub support
  96. */
  97. .hub_status_data = ehci_hub_status_data,
  98. .hub_control = ehci_hub_control,
  99. .bus_suspend = ehci_bus_suspend,
  100. .bus_resume = ehci_bus_resume,
  101. };
  102. static int mv_ehci_probe(struct platform_device *pdev)
  103. {
  104. struct mv_usb_platform_data *pdata = dev_get_platdata(&pdev->dev);
  105. struct usb_hcd *hcd;
  106. struct ehci_hcd *ehci;
  107. struct ehci_hcd_mv *ehci_mv;
  108. struct resource *r;
  109. int retval = -ENODEV;
  110. u32 offset;
  111. if (!pdata) {
  112. dev_err(&pdev->dev, "missing platform_data\n");
  113. return -ENODEV;
  114. }
  115. if (usb_disabled())
  116. return -ENODEV;
  117. hcd = usb_create_hcd(&mv_ehci_hc_driver, &pdev->dev, "mv ehci");
  118. if (!hcd)
  119. return -ENOMEM;
  120. ehci_mv = devm_kzalloc(&pdev->dev, sizeof(*ehci_mv), GFP_KERNEL);
  121. if (ehci_mv == NULL) {
  122. retval = -ENOMEM;
  123. goto err_put_hcd;
  124. }
  125. platform_set_drvdata(pdev, ehci_mv);
  126. ehci_mv->pdata = pdata;
  127. ehci_mv->hcd = hcd;
  128. ehci_mv->clk = devm_clk_get(&pdev->dev, NULL);
  129. if (IS_ERR(ehci_mv->clk)) {
  130. dev_err(&pdev->dev, "error getting clock\n");
  131. retval = PTR_ERR(ehci_mv->clk);
  132. goto err_put_hcd;
  133. }
  134. r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "phyregs");
  135. ehci_mv->phy_regs = devm_ioremap_resource(&pdev->dev, r);
  136. if (IS_ERR(ehci_mv->phy_regs)) {
  137. retval = PTR_ERR(ehci_mv->phy_regs);
  138. goto err_put_hcd;
  139. }
  140. r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "capregs");
  141. ehci_mv->cap_regs = devm_ioremap_resource(&pdev->dev, r);
  142. if (IS_ERR(ehci_mv->cap_regs)) {
  143. retval = PTR_ERR(ehci_mv->cap_regs);
  144. goto err_put_hcd;
  145. }
  146. retval = mv_ehci_enable(ehci_mv);
  147. if (retval) {
  148. dev_err(&pdev->dev, "init phy error %d\n", retval);
  149. goto err_put_hcd;
  150. }
  151. offset = readl(ehci_mv->cap_regs) & CAPLENGTH_MASK;
  152. ehci_mv->op_regs =
  153. (void __iomem *) ((unsigned long) ehci_mv->cap_regs + offset);
  154. hcd->rsrc_start = r->start;
  155. hcd->rsrc_len = resource_size(r);
  156. hcd->regs = ehci_mv->op_regs;
  157. retval = platform_get_irq(pdev, 0);
  158. if (retval < 0)
  159. goto err_disable_clk;
  160. hcd->irq = retval;
  161. ehci = hcd_to_ehci(hcd);
  162. ehci->caps = (struct ehci_caps *) ehci_mv->cap_regs;
  163. ehci_mv->mode = pdata->mode;
  164. if (ehci_mv->mode == MV_USB_MODE_OTG) {
  165. ehci_mv->otg = devm_usb_get_phy(&pdev->dev, USB_PHY_TYPE_USB2);
  166. if (IS_ERR(ehci_mv->otg)) {
  167. retval = PTR_ERR(ehci_mv->otg);
  168. if (retval == -ENXIO)
  169. dev_info(&pdev->dev, "MV_USB_MODE_OTG "
  170. "must have CONFIG_USB_PHY enabled\n");
  171. else
  172. dev_err(&pdev->dev,
  173. "unable to find transceiver\n");
  174. goto err_disable_clk;
  175. }
  176. retval = otg_set_host(ehci_mv->otg->otg, &hcd->self);
  177. if (retval < 0) {
  178. dev_err(&pdev->dev,
  179. "unable to register with transceiver\n");
  180. retval = -ENODEV;
  181. goto err_disable_clk;
  182. }
  183. /* otg will enable clock before use as host */
  184. mv_ehci_disable(ehci_mv);
  185. } else {
  186. if (pdata->set_vbus)
  187. pdata->set_vbus(1);
  188. retval = usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
  189. if (retval) {
  190. dev_err(&pdev->dev,
  191. "failed to add hcd with err %d\n", retval);
  192. goto err_set_vbus;
  193. }
  194. device_wakeup_enable(hcd->self.controller);
  195. }
  196. if (pdata->private_init)
  197. pdata->private_init(ehci_mv->op_regs, ehci_mv->phy_regs);
  198. dev_info(&pdev->dev,
  199. "successful find EHCI device with regs 0x%p irq %d"
  200. " working in %s mode\n", hcd->regs, hcd->irq,
  201. ehci_mv->mode == MV_USB_MODE_OTG ? "OTG" : "Host");
  202. return 0;
  203. err_set_vbus:
  204. if (pdata->set_vbus)
  205. pdata->set_vbus(0);
  206. err_disable_clk:
  207. mv_ehci_disable(ehci_mv);
  208. err_put_hcd:
  209. usb_put_hcd(hcd);
  210. return retval;
  211. }
  212. static int mv_ehci_remove(struct platform_device *pdev)
  213. {
  214. struct ehci_hcd_mv *ehci_mv = platform_get_drvdata(pdev);
  215. struct usb_hcd *hcd = ehci_mv->hcd;
  216. if (hcd->rh_registered)
  217. usb_remove_hcd(hcd);
  218. if (!IS_ERR_OR_NULL(ehci_mv->otg))
  219. otg_set_host(ehci_mv->otg->otg, NULL);
  220. if (ehci_mv->mode == MV_USB_MODE_HOST) {
  221. if (ehci_mv->pdata->set_vbus)
  222. ehci_mv->pdata->set_vbus(0);
  223. mv_ehci_disable(ehci_mv);
  224. }
  225. usb_put_hcd(hcd);
  226. return 0;
  227. }
  228. MODULE_ALIAS("mv-ehci");
  229. static const struct platform_device_id ehci_id_table[] = {
  230. {"pxa-u2oehci", PXA_U2OEHCI},
  231. {"pxa-sph", PXA_SPH},
  232. {"mmp3-hsic", MMP3_HSIC},
  233. {"mmp3-fsic", MMP3_FSIC},
  234. {},
  235. };
  236. static void mv_ehci_shutdown(struct platform_device *pdev)
  237. {
  238. struct ehci_hcd_mv *ehci_mv = platform_get_drvdata(pdev);
  239. struct usb_hcd *hcd = ehci_mv->hcd;
  240. if (!hcd->rh_registered)
  241. return;
  242. if (hcd->driver->shutdown)
  243. hcd->driver->shutdown(hcd);
  244. }
  245. static struct platform_driver ehci_mv_driver = {
  246. .probe = mv_ehci_probe,
  247. .remove = mv_ehci_remove,
  248. .shutdown = mv_ehci_shutdown,
  249. .driver = {
  250. .name = "mv-ehci",
  251. .bus = &platform_bus_type,
  252. },
  253. .id_table = ehci_id_table,
  254. };