ehci-mv.c 7.1 KB

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