ehci-mv.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. /* clock source and total clock number */
  29. unsigned int clknum;
  30. struct clk *clk[0];
  31. };
  32. static void ehci_clock_enable(struct ehci_hcd_mv *ehci_mv)
  33. {
  34. unsigned int i;
  35. for (i = 0; i < ehci_mv->clknum; i++)
  36. clk_enable(ehci_mv->clk[i]);
  37. }
  38. static void ehci_clock_disable(struct ehci_hcd_mv *ehci_mv)
  39. {
  40. unsigned int i;
  41. for (i = 0; i < ehci_mv->clknum; i++)
  42. clk_disable(ehci_mv->clk[i]);
  43. }
  44. static int mv_ehci_enable(struct ehci_hcd_mv *ehci_mv)
  45. {
  46. int retval;
  47. ehci_clock_enable(ehci_mv);
  48. if (ehci_mv->pdata->phy_init) {
  49. retval = ehci_mv->pdata->phy_init(ehci_mv->phy_regs);
  50. if (retval)
  51. return retval;
  52. }
  53. return 0;
  54. }
  55. static void mv_ehci_disable(struct ehci_hcd_mv *ehci_mv)
  56. {
  57. if (ehci_mv->pdata->phy_deinit)
  58. ehci_mv->pdata->phy_deinit(ehci_mv->phy_regs);
  59. ehci_clock_disable(ehci_mv);
  60. }
  61. static int mv_ehci_reset(struct usb_hcd *hcd)
  62. {
  63. struct device *dev = hcd->self.controller;
  64. struct ehci_hcd_mv *ehci_mv = dev_get_drvdata(dev);
  65. int retval;
  66. if (ehci_mv == NULL) {
  67. dev_err(dev, "Can not find private ehci data\n");
  68. return -ENODEV;
  69. }
  70. hcd->has_tt = 1;
  71. retval = ehci_setup(hcd);
  72. if (retval)
  73. dev_err(dev, "ehci_setup failed %d\n", retval);
  74. return retval;
  75. }
  76. static const struct hc_driver mv_ehci_hc_driver = {
  77. .description = hcd_name,
  78. .product_desc = "Marvell EHCI",
  79. .hcd_priv_size = sizeof(struct ehci_hcd),
  80. /*
  81. * generic hardware linkage
  82. */
  83. .irq = ehci_irq,
  84. .flags = HCD_MEMORY | HCD_USB2,
  85. /*
  86. * basic lifecycle operations
  87. */
  88. .reset = mv_ehci_reset,
  89. .start = ehci_run,
  90. .stop = ehci_stop,
  91. .shutdown = ehci_shutdown,
  92. /*
  93. * managing i/o requests and associated device resources
  94. */
  95. .urb_enqueue = ehci_urb_enqueue,
  96. .urb_dequeue = ehci_urb_dequeue,
  97. .endpoint_disable = ehci_endpoint_disable,
  98. .endpoint_reset = ehci_endpoint_reset,
  99. .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
  100. /*
  101. * scheduling support
  102. */
  103. .get_frame_number = ehci_get_frame,
  104. /*
  105. * root hub support
  106. */
  107. .hub_status_data = ehci_hub_status_data,
  108. .hub_control = ehci_hub_control,
  109. .bus_suspend = ehci_bus_suspend,
  110. .bus_resume = ehci_bus_resume,
  111. };
  112. static int mv_ehci_probe(struct platform_device *pdev)
  113. {
  114. struct mv_usb_platform_data *pdata = pdev->dev.platform_data;
  115. struct usb_hcd *hcd;
  116. struct ehci_hcd *ehci;
  117. struct ehci_hcd_mv *ehci_mv;
  118. struct resource *r;
  119. int clk_i, retval = -ENODEV;
  120. u32 offset;
  121. size_t size;
  122. if (!pdata) {
  123. dev_err(&pdev->dev, "missing platform_data\n");
  124. return -ENODEV;
  125. }
  126. if (usb_disabled())
  127. return -ENODEV;
  128. hcd = usb_create_hcd(&mv_ehci_hc_driver, &pdev->dev, "mv ehci");
  129. if (!hcd)
  130. return -ENOMEM;
  131. size = sizeof(*ehci_mv) + sizeof(struct clk *) * pdata->clknum;
  132. ehci_mv = kzalloc(size, GFP_KERNEL);
  133. if (ehci_mv == NULL) {
  134. dev_err(&pdev->dev, "cannot allocate ehci_hcd_mv\n");
  135. retval = -ENOMEM;
  136. goto err_put_hcd;
  137. }
  138. platform_set_drvdata(pdev, ehci_mv);
  139. ehci_mv->pdata = pdata;
  140. ehci_mv->hcd = hcd;
  141. ehci_mv->clknum = pdata->clknum;
  142. for (clk_i = 0; clk_i < ehci_mv->clknum; clk_i++) {
  143. ehci_mv->clk[clk_i] =
  144. clk_get(&pdev->dev, pdata->clkname[clk_i]);
  145. if (IS_ERR(ehci_mv->clk[clk_i])) {
  146. dev_err(&pdev->dev, "error get clck \"%s\"\n",
  147. pdata->clkname[clk_i]);
  148. retval = PTR_ERR(ehci_mv->clk[clk_i]);
  149. goto err_put_clk;
  150. }
  151. }
  152. r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "phyregs");
  153. if (r == NULL) {
  154. dev_err(&pdev->dev, "no phy I/O memory resource defined\n");
  155. retval = -ENODEV;
  156. goto err_put_clk;
  157. }
  158. ehci_mv->phy_regs = ioremap(r->start, resource_size(r));
  159. if (ehci_mv->phy_regs == 0) {
  160. dev_err(&pdev->dev, "failed to map phy I/O memory\n");
  161. retval = -EFAULT;
  162. goto err_put_clk;
  163. }
  164. r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "capregs");
  165. if (!r) {
  166. dev_err(&pdev->dev, "no I/O memory resource defined\n");
  167. retval = -ENODEV;
  168. goto err_iounmap_phyreg;
  169. }
  170. ehci_mv->cap_regs = ioremap(r->start, resource_size(r));
  171. if (ehci_mv->cap_regs == NULL) {
  172. dev_err(&pdev->dev, "failed to map I/O memory\n");
  173. retval = -EFAULT;
  174. goto err_iounmap_phyreg;
  175. }
  176. retval = mv_ehci_enable(ehci_mv);
  177. if (retval) {
  178. dev_err(&pdev->dev, "init phy error %d\n", retval);
  179. goto err_iounmap_capreg;
  180. }
  181. offset = readl(ehci_mv->cap_regs) & CAPLENGTH_MASK;
  182. ehci_mv->op_regs =
  183. (void __iomem *) ((unsigned long) ehci_mv->cap_regs + offset);
  184. hcd->rsrc_start = r->start;
  185. hcd->rsrc_len = r->end - r->start + 1;
  186. hcd->regs = ehci_mv->op_regs;
  187. hcd->irq = platform_get_irq(pdev, 0);
  188. if (!hcd->irq) {
  189. dev_err(&pdev->dev, "Cannot get irq.");
  190. retval = -ENODEV;
  191. goto err_disable_clk;
  192. }
  193. ehci = hcd_to_ehci(hcd);
  194. ehci->caps = (struct ehci_caps *) ehci_mv->cap_regs;
  195. ehci_mv->mode = pdata->mode;
  196. if (ehci_mv->mode == MV_USB_MODE_OTG) {
  197. #ifdef CONFIG_USB_OTG_UTILS
  198. ehci_mv->otg = usb_get_phy(USB_PHY_TYPE_USB2);
  199. if (IS_ERR_OR_NULL(ehci_mv->otg)) {
  200. dev_err(&pdev->dev,
  201. "unable to find transceiver\n");
  202. retval = -ENODEV;
  203. goto err_disable_clk;
  204. }
  205. retval = otg_set_host(ehci_mv->otg->otg, &hcd->self);
  206. if (retval < 0) {
  207. dev_err(&pdev->dev,
  208. "unable to register with transceiver\n");
  209. retval = -ENODEV;
  210. goto err_put_transceiver;
  211. }
  212. /* otg will enable clock before use as host */
  213. mv_ehci_disable(ehci_mv);
  214. #else
  215. dev_info(&pdev->dev, "MV_USB_MODE_OTG "
  216. "must have CONFIG_USB_OTG_UTILS enabled\n");
  217. goto err_disable_clk;
  218. #endif
  219. } else {
  220. if (pdata->set_vbus)
  221. pdata->set_vbus(1);
  222. retval = usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
  223. if (retval) {
  224. dev_err(&pdev->dev,
  225. "failed to add hcd with err %d\n", retval);
  226. goto err_set_vbus;
  227. }
  228. }
  229. if (pdata->private_init)
  230. pdata->private_init(ehci_mv->op_regs, ehci_mv->phy_regs);
  231. dev_info(&pdev->dev,
  232. "successful find EHCI device with regs 0x%p irq %d"
  233. " working in %s mode\n", hcd->regs, hcd->irq,
  234. ehci_mv->mode == MV_USB_MODE_OTG ? "OTG" : "Host");
  235. return 0;
  236. err_set_vbus:
  237. if (pdata->set_vbus)
  238. pdata->set_vbus(0);
  239. #ifdef CONFIG_USB_OTG_UTILS
  240. err_put_transceiver:
  241. if (!IS_ERR_OR_NULL(ehci_mv->otg))
  242. usb_put_phy(ehci_mv->otg);
  243. #endif
  244. err_disable_clk:
  245. mv_ehci_disable(ehci_mv);
  246. err_iounmap_capreg:
  247. iounmap(ehci_mv->cap_regs);
  248. err_iounmap_phyreg:
  249. iounmap(ehci_mv->phy_regs);
  250. err_put_clk:
  251. for (clk_i--; clk_i >= 0; clk_i--)
  252. clk_put(ehci_mv->clk[clk_i]);
  253. platform_set_drvdata(pdev, NULL);
  254. kfree(ehci_mv);
  255. err_put_hcd:
  256. usb_put_hcd(hcd);
  257. return retval;
  258. }
  259. static int mv_ehci_remove(struct platform_device *pdev)
  260. {
  261. struct ehci_hcd_mv *ehci_mv = platform_get_drvdata(pdev);
  262. struct usb_hcd *hcd = ehci_mv->hcd;
  263. int clk_i;
  264. if (hcd->rh_registered)
  265. usb_remove_hcd(hcd);
  266. if (!IS_ERR_OR_NULL(ehci_mv->otg)) {
  267. otg_set_host(ehci_mv->otg->otg, NULL);
  268. usb_put_phy(ehci_mv->otg);
  269. }
  270. if (ehci_mv->mode == MV_USB_MODE_HOST) {
  271. if (ehci_mv->pdata->set_vbus)
  272. ehci_mv->pdata->set_vbus(0);
  273. mv_ehci_disable(ehci_mv);
  274. }
  275. iounmap(ehci_mv->cap_regs);
  276. iounmap(ehci_mv->phy_regs);
  277. for (clk_i = 0; clk_i < ehci_mv->clknum; clk_i++)
  278. clk_put(ehci_mv->clk[clk_i]);
  279. platform_set_drvdata(pdev, NULL);
  280. kfree(ehci_mv);
  281. usb_put_hcd(hcd);
  282. return 0;
  283. }
  284. MODULE_ALIAS("mv-ehci");
  285. static const struct platform_device_id ehci_id_table[] = {
  286. {"pxa-u2oehci", PXA_U2OEHCI},
  287. {"pxa-sph", PXA_SPH},
  288. {"mmp3-hsic", MMP3_HSIC},
  289. {"mmp3-fsic", MMP3_FSIC},
  290. {},
  291. };
  292. static void mv_ehci_shutdown(struct platform_device *pdev)
  293. {
  294. struct ehci_hcd_mv *ehci_mv = platform_get_drvdata(pdev);
  295. struct usb_hcd *hcd = ehci_mv->hcd;
  296. if (!hcd->rh_registered)
  297. return;
  298. if (hcd->driver->shutdown)
  299. hcd->driver->shutdown(hcd);
  300. }
  301. static struct platform_driver ehci_mv_driver = {
  302. .probe = mv_ehci_probe,
  303. .remove = mv_ehci_remove,
  304. .shutdown = mv_ehci_shutdown,
  305. .driver = {
  306. .name = "mv-ehci",
  307. .bus = &platform_bus_type,
  308. },
  309. .id_table = ehci_id_table,
  310. };