ehci-msm.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /* ehci-msm.c - HSUSB Host Controller Driver Implementation
  2. *
  3. * Copyright (c) 2008-2010, Code Aurora Forum. All rights reserved.
  4. *
  5. * Partly derived from ehci-fsl.c and ehci-hcd.c
  6. * Copyright (c) 2000-2004 by David Brownell
  7. * Copyright (c) 2005 MontaVista Software
  8. *
  9. * All source code in this file is licensed under the following license except
  10. * where indicated.
  11. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms of the GNU General Public License version 2 as published
  14. * by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  19. *
  20. * See the GNU General Public License for more details.
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, you can find it at http://www.fsf.org
  23. */
  24. #include <linux/platform_device.h>
  25. #include <linux/clk.h>
  26. #include <linux/err.h>
  27. #include <linux/pm_runtime.h>
  28. #include <linux/usb/otg.h>
  29. #include <linux/usb/msm_hsusb_hw.h>
  30. #define MSM_USB_BASE (hcd->regs)
  31. static struct otg_transceiver *otg;
  32. /*
  33. * ehci_run defined in drivers/usb/host/ehci-hcd.c reset the controller and
  34. * the configuration settings in ehci_msm_reset vanish after controller is
  35. * reset. Resetting the controler in ehci_run seems to be un-necessary
  36. * provided HCD reset the controller before calling ehci_run. Most of the HCD
  37. * do but some are not. So this function is same as ehci_run but we don't
  38. * reset the controller here.
  39. */
  40. static int ehci_msm_run(struct usb_hcd *hcd)
  41. {
  42. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  43. u32 temp;
  44. u32 hcc_params;
  45. hcd->uses_new_polling = 1;
  46. ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
  47. ehci_writel(ehci, (u32)ehci->async->qh_dma, &ehci->regs->async_next);
  48. /*
  49. * hcc_params controls whether ehci->regs->segment must (!!!)
  50. * be used; it constrains QH/ITD/SITD and QTD locations.
  51. * pci_pool consistent memory always uses segment zero.
  52. * streaming mappings for I/O buffers, like pci_map_single(),
  53. * can return segments above 4GB, if the device allows.
  54. *
  55. * NOTE: the dma mask is visible through dma_supported(), so
  56. * drivers can pass this info along ... like NETIF_F_HIGHDMA,
  57. * Scsi_Host.highmem_io, and so forth. It's readonly to all
  58. * host side drivers though.
  59. */
  60. hcc_params = ehci_readl(ehci, &ehci->caps->hcc_params);
  61. if (HCC_64BIT_ADDR(hcc_params))
  62. ehci_writel(ehci, 0, &ehci->regs->segment);
  63. /*
  64. * Philips, Intel, and maybe others need CMD_RUN before the
  65. * root hub will detect new devices (why?); NEC doesn't
  66. */
  67. ehci->command &= ~(CMD_LRESET|CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
  68. ehci->command |= CMD_RUN;
  69. ehci_writel(ehci, ehci->command, &ehci->regs->command);
  70. dbg_cmd(ehci, "init", ehci->command);
  71. /*
  72. * Start, enabling full USB 2.0 functionality ... usb 1.1 devices
  73. * are explicitly handed to companion controller(s), so no TT is
  74. * involved with the root hub. (Except where one is integrated,
  75. * and there's no companion controller unless maybe for USB OTG.)
  76. *
  77. * Turning on the CF flag will transfer ownership of all ports
  78. * from the companions to the EHCI controller. If any of the
  79. * companions are in the middle of a port reset at the time, it
  80. * could cause trouble. Write-locking ehci_cf_port_reset_rwsem
  81. * guarantees that no resets are in progress. After we set CF,
  82. * a short delay lets the hardware catch up; new resets shouldn't
  83. * be started before the port switching actions could complete.
  84. */
  85. down_write(&ehci_cf_port_reset_rwsem);
  86. hcd->state = HC_STATE_RUNNING;
  87. ehci_writel(ehci, FLAG_CF, &ehci->regs->configured_flag);
  88. ehci_readl(ehci, &ehci->regs->command); /* unblock posted writes */
  89. usleep_range(5000, 5500);
  90. up_write(&ehci_cf_port_reset_rwsem);
  91. ehci->last_periodic_enable = ktime_get_real();
  92. temp = HC_VERSION(ehci_readl(ehci, &ehci->caps->hc_capbase));
  93. ehci_info(ehci,
  94. "USB %x.%x started, EHCI %x.%02x%s\n",
  95. ((ehci->sbrn & 0xf0)>>4), (ehci->sbrn & 0x0f),
  96. temp >> 8, temp & 0xff,
  97. ignore_oc ? ", overcurrent ignored" : "");
  98. ehci_writel(ehci, INTR_MASK,
  99. &ehci->regs->intr_enable); /* Turn On Interrupts */
  100. /* GRR this is run-once init(), being done every time the HC starts.
  101. * So long as they're part of class devices, we can't do it init()
  102. * since the class device isn't created that early.
  103. */
  104. create_debug_files(ehci);
  105. create_companion_file(ehci);
  106. return 0;
  107. }
  108. static int ehci_msm_reset(struct usb_hcd *hcd)
  109. {
  110. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  111. int retval;
  112. ehci->caps = USB_CAPLENGTH;
  113. ehci->regs = USB_CAPLENGTH +
  114. HC_LENGTH(ehci_readl(ehci, &ehci->caps->hc_capbase));
  115. /* cache the data to minimize the chip reads*/
  116. ehci->hcs_params = ehci_readl(ehci, &ehci->caps->hcs_params);
  117. hcd->has_tt = 1;
  118. ehci->sbrn = HCD_USB2;
  119. /* data structure init */
  120. retval = ehci_init(hcd);
  121. if (retval)
  122. return retval;
  123. retval = ehci_reset(ehci);
  124. if (retval)
  125. return retval;
  126. /* bursts of unspecified length. */
  127. writel(0, USB_AHBBURST);
  128. /* Use the AHB transactor */
  129. writel(0, USB_AHBMODE);
  130. /* Disable streaming mode and select host mode */
  131. writel(0x13, USB_USBMODE);
  132. ehci_port_power(ehci, 1);
  133. return 0;
  134. }
  135. static struct hc_driver msm_hc_driver = {
  136. .description = hcd_name,
  137. .product_desc = "Qualcomm On-Chip EHCI Host Controller",
  138. .hcd_priv_size = sizeof(struct ehci_hcd),
  139. /*
  140. * generic hardware linkage
  141. */
  142. .irq = ehci_irq,
  143. .flags = HCD_USB2 | HCD_MEMORY,
  144. .reset = ehci_msm_reset,
  145. .start = ehci_msm_run,
  146. .stop = ehci_stop,
  147. .shutdown = ehci_shutdown,
  148. /*
  149. * managing i/o requests and associated device resources
  150. */
  151. .urb_enqueue = ehci_urb_enqueue,
  152. .urb_dequeue = ehci_urb_dequeue,
  153. .endpoint_disable = ehci_endpoint_disable,
  154. .endpoint_reset = ehci_endpoint_reset,
  155. .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
  156. /*
  157. * scheduling support
  158. */
  159. .get_frame_number = ehci_get_frame,
  160. /*
  161. * root hub support
  162. */
  163. .hub_status_data = ehci_hub_status_data,
  164. .hub_control = ehci_hub_control,
  165. .relinquish_port = ehci_relinquish_port,
  166. .port_handed_over = ehci_port_handed_over,
  167. /*
  168. * PM support
  169. */
  170. .bus_suspend = ehci_bus_suspend,
  171. .bus_resume = ehci_bus_resume,
  172. };
  173. static int ehci_msm_probe(struct platform_device *pdev)
  174. {
  175. struct usb_hcd *hcd;
  176. struct resource *res;
  177. int ret;
  178. dev_dbg(&pdev->dev, "ehci_msm proble\n");
  179. hcd = usb_create_hcd(&msm_hc_driver, &pdev->dev, dev_name(&pdev->dev));
  180. if (!hcd) {
  181. dev_err(&pdev->dev, "Unable to create HCD\n");
  182. return -ENOMEM;
  183. }
  184. hcd->irq = platform_get_irq(pdev, 0);
  185. if (hcd->irq < 0) {
  186. dev_err(&pdev->dev, "Unable to get IRQ resource\n");
  187. ret = hcd->irq;
  188. goto put_hcd;
  189. }
  190. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  191. if (!res) {
  192. dev_err(&pdev->dev, "Unable to get memory resource\n");
  193. ret = -ENODEV;
  194. goto put_hcd;
  195. }
  196. hcd->rsrc_start = res->start;
  197. hcd->rsrc_len = resource_size(res);
  198. hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
  199. if (!hcd->regs) {
  200. dev_err(&pdev->dev, "ioremap failed\n");
  201. ret = -ENOMEM;
  202. goto put_hcd;
  203. }
  204. /*
  205. * OTG driver takes care of PHY initialization, clock management,
  206. * powering up VBUS, mapping of registers address space and power
  207. * management.
  208. */
  209. otg = otg_get_transceiver();
  210. if (!otg) {
  211. dev_err(&pdev->dev, "unable to find transceiver\n");
  212. ret = -ENODEV;
  213. goto unmap;
  214. }
  215. ret = otg_set_host(otg, &hcd->self);
  216. if (ret < 0) {
  217. dev_err(&pdev->dev, "unable to register with transceiver\n");
  218. goto put_transceiver;
  219. }
  220. device_init_wakeup(&pdev->dev, 1);
  221. /*
  222. * OTG device parent of HCD takes care of putting
  223. * hardware into low power mode.
  224. */
  225. pm_runtime_no_callbacks(&pdev->dev);
  226. pm_runtime_enable(&pdev->dev);
  227. return 0;
  228. put_transceiver:
  229. otg_put_transceiver(otg);
  230. unmap:
  231. iounmap(hcd->regs);
  232. put_hcd:
  233. usb_put_hcd(hcd);
  234. return ret;
  235. }
  236. static int __devexit ehci_msm_remove(struct platform_device *pdev)
  237. {
  238. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  239. device_init_wakeup(&pdev->dev, 0);
  240. pm_runtime_disable(&pdev->dev);
  241. pm_runtime_set_suspended(&pdev->dev);
  242. otg_set_host(otg, NULL);
  243. otg_put_transceiver(otg);
  244. usb_put_hcd(hcd);
  245. return 0;
  246. }
  247. #ifdef CONFIG_PM
  248. static int ehci_msm_pm_suspend(struct device *dev)
  249. {
  250. struct usb_hcd *hcd = dev_get_drvdata(dev);
  251. bool wakeup = device_may_wakeup(dev);
  252. dev_dbg(dev, "ehci-msm PM suspend\n");
  253. /*
  254. * EHCI helper function has also the same check before manipulating
  255. * port wakeup flags. We do check here the same condition before
  256. * calling the same helper function to avoid bringing hardware
  257. * from Low power mode when there is no need for adjusting port
  258. * wakeup flags.
  259. */
  260. if (hcd->self.root_hub->do_remote_wakeup && !wakeup) {
  261. pm_runtime_resume(dev);
  262. ehci_prepare_ports_for_controller_suspend(hcd_to_ehci(hcd),
  263. wakeup);
  264. }
  265. return 0;
  266. }
  267. static int ehci_msm_pm_resume(struct device *dev)
  268. {
  269. struct usb_hcd *hcd = dev_get_drvdata(dev);
  270. dev_dbg(dev, "ehci-msm PM resume\n");
  271. ehci_prepare_ports_for_controller_resume(hcd_to_ehci(hcd));
  272. return 0;
  273. }
  274. #else
  275. #define ehci_msm_pm_suspend NULL
  276. #define ehci_msm_pm_resume NULL
  277. #endif
  278. static const struct dev_pm_ops ehci_msm_dev_pm_ops = {
  279. .suspend = ehci_msm_pm_suspend,
  280. .resume = ehci_msm_pm_resume,
  281. };
  282. static struct platform_driver ehci_msm_driver = {
  283. .probe = ehci_msm_probe,
  284. .remove = __devexit_p(ehci_msm_remove),
  285. .driver = {
  286. .name = "msm_hsusb_host",
  287. .pm = &ehci_msm_dev_pm_ops,
  288. },
  289. };