ehci-mv.c 8.7 KB

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