ehci-mv.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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,
  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 = pdev->dev.platform_data;
  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. dev_err(&pdev->dev, "cannot allocate ehci_hcd_mv\n");
  127. retval = -ENOMEM;
  128. goto err_put_hcd;
  129. }
  130. platform_set_drvdata(pdev, ehci_mv);
  131. ehci_mv->pdata = pdata;
  132. ehci_mv->hcd = hcd;
  133. ehci_mv->clk = devm_clk_get(&pdev->dev, NULL);
  134. if (IS_ERR(ehci_mv->clk)) {
  135. dev_err(&pdev->dev, "error getting clock\n");
  136. retval = PTR_ERR(ehci_mv->clk);
  137. goto err_put_hcd;
  138. }
  139. r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "phyregs");
  140. if (r == NULL) {
  141. dev_err(&pdev->dev, "no phy I/O memory resource defined\n");
  142. retval = -ENODEV;
  143. goto err_put_hcd;
  144. }
  145. ehci_mv->phy_regs = devm_ioremap(&pdev->dev, r->start,
  146. resource_size(r));
  147. if (ehci_mv->phy_regs == 0) {
  148. dev_err(&pdev->dev, "failed to map phy I/O memory\n");
  149. retval = -EFAULT;
  150. goto err_put_hcd;
  151. }
  152. r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "capregs");
  153. if (!r) {
  154. dev_err(&pdev->dev, "no I/O memory resource defined\n");
  155. retval = -ENODEV;
  156. goto err_put_hcd;
  157. }
  158. ehci_mv->cap_regs = devm_ioremap(&pdev->dev, r->start,
  159. resource_size(r));
  160. if (ehci_mv->cap_regs == NULL) {
  161. dev_err(&pdev->dev, "failed to map I/O memory\n");
  162. retval = -EFAULT;
  163. goto err_put_hcd;
  164. }
  165. retval = mv_ehci_enable(ehci_mv);
  166. if (retval) {
  167. dev_err(&pdev->dev, "init phy error %d\n", retval);
  168. goto err_put_hcd;
  169. }
  170. offset = readl(ehci_mv->cap_regs) & CAPLENGTH_MASK;
  171. ehci_mv->op_regs =
  172. (void __iomem *) ((unsigned long) ehci_mv->cap_regs + offset);
  173. hcd->rsrc_start = r->start;
  174. hcd->rsrc_len = resource_size(r);
  175. hcd->regs = ehci_mv->op_regs;
  176. hcd->irq = platform_get_irq(pdev, 0);
  177. if (!hcd->irq) {
  178. dev_err(&pdev->dev, "Cannot get irq.");
  179. retval = -ENODEV;
  180. goto err_disable_clk;
  181. }
  182. ehci = hcd_to_ehci(hcd);
  183. ehci->caps = (struct ehci_caps *) ehci_mv->cap_regs;
  184. ehci_mv->mode = pdata->mode;
  185. if (ehci_mv->mode == MV_USB_MODE_OTG) {
  186. ehci_mv->otg = devm_usb_get_phy(&pdev->dev, USB_PHY_TYPE_USB2);
  187. if (IS_ERR(ehci_mv->otg)) {
  188. retval = PTR_ERR(ehci_mv->otg);
  189. if (retval == -ENXIO)
  190. dev_info(&pdev->dev, "MV_USB_MODE_OTG "
  191. "must have CONFIG_USB_PHY enabled\n");
  192. else
  193. dev_err(&pdev->dev,
  194. "unable to find transceiver\n");
  195. goto err_disable_clk;
  196. }
  197. retval = otg_set_host(ehci_mv->otg->otg, &hcd->self);
  198. if (retval < 0) {
  199. dev_err(&pdev->dev,
  200. "unable to register with transceiver\n");
  201. retval = -ENODEV;
  202. goto err_disable_clk;
  203. }
  204. /* otg will enable clock before use as host */
  205. mv_ehci_disable(ehci_mv);
  206. } else {
  207. if (pdata->set_vbus)
  208. pdata->set_vbus(1);
  209. retval = usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
  210. if (retval) {
  211. dev_err(&pdev->dev,
  212. "failed to add hcd with err %d\n", retval);
  213. goto err_set_vbus;
  214. }
  215. }
  216. if (pdata->private_init)
  217. pdata->private_init(ehci_mv->op_regs, ehci_mv->phy_regs);
  218. dev_info(&pdev->dev,
  219. "successful find EHCI device with regs 0x%p irq %d"
  220. " working in %s mode\n", hcd->regs, hcd->irq,
  221. ehci_mv->mode == MV_USB_MODE_OTG ? "OTG" : "Host");
  222. return 0;
  223. err_set_vbus:
  224. if (pdata->set_vbus)
  225. pdata->set_vbus(0);
  226. err_disable_clk:
  227. mv_ehci_disable(ehci_mv);
  228. err_put_hcd:
  229. usb_put_hcd(hcd);
  230. return retval;
  231. }
  232. static int mv_ehci_remove(struct platform_device *pdev)
  233. {
  234. struct ehci_hcd_mv *ehci_mv = platform_get_drvdata(pdev);
  235. struct usb_hcd *hcd = ehci_mv->hcd;
  236. if (hcd->rh_registered)
  237. usb_remove_hcd(hcd);
  238. if (!IS_ERR_OR_NULL(ehci_mv->otg))
  239. otg_set_host(ehci_mv->otg->otg, NULL);
  240. if (ehci_mv->mode == MV_USB_MODE_HOST) {
  241. if (ehci_mv->pdata->set_vbus)
  242. ehci_mv->pdata->set_vbus(0);
  243. mv_ehci_disable(ehci_mv);
  244. }
  245. usb_put_hcd(hcd);
  246. return 0;
  247. }
  248. MODULE_ALIAS("mv-ehci");
  249. static const struct platform_device_id ehci_id_table[] = {
  250. {"pxa-u2oehci", PXA_U2OEHCI},
  251. {"pxa-sph", PXA_SPH},
  252. {"mmp3-hsic", MMP3_HSIC},
  253. {"mmp3-fsic", MMP3_FSIC},
  254. {},
  255. };
  256. static void mv_ehci_shutdown(struct platform_device *pdev)
  257. {
  258. struct ehci_hcd_mv *ehci_mv = platform_get_drvdata(pdev);
  259. struct usb_hcd *hcd = ehci_mv->hcd;
  260. if (!hcd->rh_registered)
  261. return;
  262. if (hcd->driver->shutdown)
  263. hcd->driver->shutdown(hcd);
  264. }
  265. static struct platform_driver ehci_mv_driver = {
  266. .probe = mv_ehci_probe,
  267. .remove = mv_ehci_remove,
  268. .shutdown = mv_ehci_shutdown,
  269. .driver = {
  270. .name = "mv-ehci",
  271. .bus = &platform_bus_type,
  272. },
  273. .id_table = ehci_id_table,
  274. };