ohci-omap.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. // SPDX-License-Identifier: GPL-1.0+
  2. /*
  3. * OHCI HCD (Host Controller Driver) for USB.
  4. *
  5. * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
  6. * (C) Copyright 2000-2005 David Brownell
  7. * (C) Copyright 2002 Hewlett-Packard Company
  8. *
  9. * OMAP Bus Glue
  10. *
  11. * Modified for OMAP by Tony Lindgren <tony@atomide.com>
  12. * Based on the 2.4 OMAP OHCI driver originally done by MontaVista Software Inc.
  13. * and on ohci-sa1111.c by Christopher Hoover <ch@hpl.hp.com>
  14. *
  15. * This file is licenced under the GPL.
  16. */
  17. #include <linux/clk.h>
  18. #include <linux/dma-mapping.h>
  19. #include <linux/err.h>
  20. #include <linux/gpio/consumer.h>
  21. #include <linux/io.h>
  22. #include <linux/jiffies.h>
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/usb/otg.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/platform_data/usb-omap1.h>
  28. #include <linux/soc/ti/omap1-usb.h>
  29. #include <linux/soc/ti/omap1-mux.h>
  30. #include <linux/soc/ti/omap1-soc.h>
  31. #include <linux/soc/ti/omap1-io.h>
  32. #include <linux/signal.h>
  33. #include <linux/usb.h>
  34. #include <linux/usb/hcd.h>
  35. #include "ohci.h"
  36. #include <asm/io.h>
  37. #include <asm/mach-types.h>
  38. #define DRIVER_DESC "OHCI OMAP driver"
  39. struct ohci_omap_priv {
  40. struct clk *usb_host_ck;
  41. struct clk *usb_dc_ck;
  42. struct gpio_desc *power;
  43. struct gpio_desc *overcurrent;
  44. };
  45. static const char hcd_name[] = "ohci-omap";
  46. static struct hc_driver __read_mostly ohci_omap_hc_driver;
  47. #define hcd_to_ohci_omap_priv(h) \
  48. ((struct ohci_omap_priv *)hcd_to_ohci(h)->priv)
  49. static void omap_ohci_clock_power(struct ohci_omap_priv *priv, int on)
  50. {
  51. if (on) {
  52. clk_enable(priv->usb_dc_ck);
  53. clk_enable(priv->usb_host_ck);
  54. /* guesstimate for T5 == 1x 32K clock + APLL lock time */
  55. udelay(100);
  56. } else {
  57. clk_disable(priv->usb_host_ck);
  58. clk_disable(priv->usb_dc_ck);
  59. }
  60. }
  61. static void start_hnp(struct ohci_hcd *ohci)
  62. {
  63. struct usb_hcd *hcd = ohci_to_hcd(ohci);
  64. const unsigned port = hcd->self.otg_port - 1;
  65. unsigned long flags;
  66. u32 l;
  67. otg_start_hnp(hcd->usb_phy->otg);
  68. local_irq_save(flags);
  69. hcd->usb_phy->otg->state = OTG_STATE_A_SUSPEND;
  70. writel (RH_PS_PSS, &ohci->regs->roothub.portstatus [port]);
  71. l = omap_readl(OTG_CTRL);
  72. l &= ~OTG_A_BUSREQ;
  73. omap_writel(l, OTG_CTRL);
  74. local_irq_restore(flags);
  75. }
  76. /*-------------------------------------------------------------------------*/
  77. static int ohci_omap_reset(struct usb_hcd *hcd)
  78. {
  79. struct ohci_hcd *ohci = hcd_to_ohci(hcd);
  80. struct omap_usb_config *config = dev_get_platdata(hcd->self.controller);
  81. struct ohci_omap_priv *priv = hcd_to_ohci_omap_priv(hcd);
  82. int need_transceiver = (config->otg != 0);
  83. int ret;
  84. dev_dbg(hcd->self.controller, "starting USB Controller\n");
  85. if (config->otg) {
  86. hcd->self.otg_port = config->otg;
  87. /* default/minimum OTG power budget: 8 mA */
  88. hcd->power_budget = 8;
  89. }
  90. /* XXX OMAP16xx only */
  91. if (config->ocpi_enable)
  92. config->ocpi_enable();
  93. if (IS_ENABLED(CONFIG_USB_OTG) && need_transceiver) {
  94. hcd->usb_phy = usb_get_phy(USB_PHY_TYPE_USB2);
  95. if (!IS_ERR_OR_NULL(hcd->usb_phy)) {
  96. int status = otg_set_host(hcd->usb_phy->otg,
  97. &ohci_to_hcd(ohci)->self);
  98. dev_dbg(hcd->self.controller, "init %s phy, status %d\n",
  99. hcd->usb_phy->label, status);
  100. if (status) {
  101. usb_put_phy(hcd->usb_phy);
  102. return status;
  103. }
  104. } else {
  105. return -EPROBE_DEFER;
  106. }
  107. hcd->skip_phy_initialization = 1;
  108. ohci->start_hnp = start_hnp;
  109. }
  110. omap_ohci_clock_power(priv, 1);
  111. if (config->lb_reset)
  112. config->lb_reset();
  113. ret = ohci_setup(hcd);
  114. if (ret < 0)
  115. return ret;
  116. if (config->otg || config->rwc) {
  117. ohci->hc_control = OHCI_CTRL_RWC;
  118. writel(OHCI_CTRL_RWC, &ohci->regs->control);
  119. }
  120. /* board-specific power switching and overcurrent support */
  121. if (machine_is_omap_osk()) {
  122. u32 rh = roothub_a (ohci);
  123. /* power switching (ganged by default) */
  124. rh &= ~RH_A_NPS;
  125. /* TPS2045 switch for internal transceiver (port 1) */
  126. if (machine_is_omap_osk()) {
  127. ohci_to_hcd(ohci)->power_budget = 250;
  128. rh &= ~RH_A_NOCP;
  129. /* gpio9 for overcurrent detction */
  130. omap_cfg_reg(W8_1610_GPIO9);
  131. /* for paranoia's sake: disable USB.PUEN */
  132. omap_cfg_reg(W4_USB_HIGHZ);
  133. }
  134. ohci_writel(ohci, rh, &ohci->regs->roothub.a);
  135. ohci->flags &= ~OHCI_QUIRK_HUB_POWER;
  136. } else if (machine_is_nokia770()) {
  137. /* We require a self-powered hub, which should have
  138. * plenty of power. */
  139. ohci_to_hcd(ohci)->power_budget = 0;
  140. }
  141. /* FIXME hub_wq hub requests should manage power switching */
  142. if (config->transceiver_power)
  143. return config->transceiver_power(1);
  144. if (priv->power)
  145. gpiod_set_value_cansleep(priv->power, 0);
  146. /* board init will have already handled HMC and mux setup.
  147. * any external transceiver should already be initialized
  148. * too, so all configured ports use the right signaling now.
  149. */
  150. return 0;
  151. }
  152. /*-------------------------------------------------------------------------*/
  153. /**
  154. * ohci_hcd_omap_probe - initialize OMAP-based HCDs
  155. * @pdev: USB controller to probe
  156. *
  157. * Context: task context, might sleep
  158. *
  159. * Allocates basic resources for this USB host controller, and
  160. * then invokes the start() method for the HCD associated with it
  161. * through the hotplug entry's driver_data.
  162. */
  163. static int ohci_hcd_omap_probe(struct platform_device *pdev)
  164. {
  165. int retval, irq;
  166. struct usb_hcd *hcd = 0;
  167. struct ohci_omap_priv *priv;
  168. if (pdev->num_resources != 2) {
  169. dev_err(&pdev->dev, "invalid num_resources: %i\n",
  170. pdev->num_resources);
  171. return -ENODEV;
  172. }
  173. if (pdev->resource[0].flags != IORESOURCE_MEM
  174. || pdev->resource[1].flags != IORESOURCE_IRQ) {
  175. dev_err(&pdev->dev, "invalid resource type\n");
  176. return -ENODEV;
  177. }
  178. hcd = usb_create_hcd(&ohci_omap_hc_driver, &pdev->dev,
  179. dev_name(&pdev->dev));
  180. if (!hcd)
  181. return -ENOMEM;
  182. hcd->rsrc_start = pdev->resource[0].start;
  183. hcd->rsrc_len = pdev->resource[0].end - pdev->resource[0].start + 1;
  184. priv = hcd_to_ohci_omap_priv(hcd);
  185. /* Obtain two optional GPIO lines */
  186. priv->power = devm_gpiod_get_optional(&pdev->dev, "power", GPIOD_ASIS);
  187. if (IS_ERR(priv->power)) {
  188. retval = PTR_ERR(priv->power);
  189. goto err_put_hcd;
  190. }
  191. if (priv->power)
  192. gpiod_set_consumer_name(priv->power, "OHCI power");
  193. /*
  194. * This "overcurrent" GPIO line isn't really used in the code,
  195. * but has a designated hardware function.
  196. * TODO: implement proper overcurrent handling.
  197. */
  198. priv->overcurrent = devm_gpiod_get_optional(&pdev->dev, "overcurrent",
  199. GPIOD_IN);
  200. if (IS_ERR(priv->overcurrent)) {
  201. retval = PTR_ERR(priv->overcurrent);
  202. goto err_put_hcd;
  203. }
  204. if (priv->overcurrent)
  205. gpiod_set_consumer_name(priv->overcurrent, "OHCI overcurrent");
  206. priv->usb_host_ck = clk_get(&pdev->dev, "usb_hhc_ck");
  207. if (IS_ERR(priv->usb_host_ck)) {
  208. retval = PTR_ERR(priv->usb_host_ck);
  209. goto err_put_hcd;
  210. }
  211. retval = clk_prepare(priv->usb_host_ck);
  212. if (retval)
  213. goto err_put_host_ck;
  214. if (!cpu_is_omap15xx())
  215. priv->usb_dc_ck = clk_get(&pdev->dev, "usb_dc_ck");
  216. else
  217. priv->usb_dc_ck = clk_get(&pdev->dev, "lb_ck");
  218. if (IS_ERR(priv->usb_dc_ck)) {
  219. retval = PTR_ERR(priv->usb_dc_ck);
  220. goto err_unprepare_host_ck;
  221. }
  222. retval = clk_prepare(priv->usb_dc_ck);
  223. if (retval)
  224. goto err_put_dc_ck;
  225. if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, hcd_name)) {
  226. dev_dbg(&pdev->dev, "request_mem_region failed\n");
  227. retval = -EBUSY;
  228. goto err_unprepare_dc_ck;
  229. }
  230. hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
  231. if (!hcd->regs) {
  232. dev_err(&pdev->dev, "can't ioremap OHCI HCD\n");
  233. retval = -ENOMEM;
  234. goto err2;
  235. }
  236. irq = platform_get_irq(pdev, 0);
  237. if (irq < 0) {
  238. retval = irq;
  239. goto err3;
  240. }
  241. retval = usb_add_hcd(hcd, irq, 0);
  242. if (retval)
  243. goto err3;
  244. device_wakeup_enable(hcd->self.controller);
  245. return 0;
  246. err3:
  247. iounmap(hcd->regs);
  248. err2:
  249. release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
  250. err_unprepare_dc_ck:
  251. clk_unprepare(priv->usb_dc_ck);
  252. err_put_dc_ck:
  253. clk_put(priv->usb_dc_ck);
  254. err_unprepare_host_ck:
  255. clk_unprepare(priv->usb_host_ck);
  256. err_put_host_ck:
  257. clk_put(priv->usb_host_ck);
  258. err_put_hcd:
  259. usb_put_hcd(hcd);
  260. return retval;
  261. }
  262. /* may be called with controller, bus, and devices active */
  263. /**
  264. * ohci_hcd_omap_remove - shutdown processing for OMAP-based HCDs
  265. * @pdev: USB Host Controller being removed
  266. *
  267. * Context: task context, might sleep
  268. *
  269. * Reverses the effect of ohci_hcd_omap_probe(), first invoking
  270. * the HCD's stop() method. It is always called from a thread
  271. * context, normally "rmmod", "apmd", or something similar.
  272. */
  273. static void ohci_hcd_omap_remove(struct platform_device *pdev)
  274. {
  275. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  276. struct ohci_omap_priv *priv = hcd_to_ohci_omap_priv(hcd);
  277. dev_dbg(hcd->self.controller, "stopping USB Controller\n");
  278. usb_remove_hcd(hcd);
  279. omap_ohci_clock_power(priv, 0);
  280. if (!IS_ERR_OR_NULL(hcd->usb_phy)) {
  281. (void) otg_set_host(hcd->usb_phy->otg, 0);
  282. usb_put_phy(hcd->usb_phy);
  283. }
  284. iounmap(hcd->regs);
  285. release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
  286. clk_unprepare(priv->usb_dc_ck);
  287. clk_put(priv->usb_dc_ck);
  288. clk_unprepare(priv->usb_host_ck);
  289. clk_put(priv->usb_host_ck);
  290. usb_put_hcd(hcd);
  291. }
  292. /*-------------------------------------------------------------------------*/
  293. #ifdef CONFIG_PM
  294. static int ohci_omap_suspend(struct platform_device *pdev, pm_message_t message)
  295. {
  296. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  297. struct ohci_hcd *ohci = hcd_to_ohci(hcd);
  298. struct ohci_omap_priv *priv = hcd_to_ohci_omap_priv(hcd);
  299. bool do_wakeup = device_may_wakeup(&pdev->dev);
  300. int ret;
  301. if (time_before(jiffies, ohci->next_statechange))
  302. msleep(5);
  303. ohci->next_statechange = jiffies;
  304. ret = ohci_suspend(hcd, do_wakeup);
  305. if (ret)
  306. return ret;
  307. omap_ohci_clock_power(priv, 0);
  308. return ret;
  309. }
  310. static int ohci_omap_resume(struct platform_device *dev)
  311. {
  312. struct usb_hcd *hcd = platform_get_drvdata(dev);
  313. struct ohci_hcd *ohci = hcd_to_ohci(hcd);
  314. struct ohci_omap_priv *priv = hcd_to_ohci_omap_priv(hcd);
  315. if (time_before(jiffies, ohci->next_statechange))
  316. msleep(5);
  317. ohci->next_statechange = jiffies;
  318. omap_ohci_clock_power(priv, 1);
  319. ohci_resume(hcd, false);
  320. return 0;
  321. }
  322. #endif
  323. /*-------------------------------------------------------------------------*/
  324. /*
  325. * Driver definition to register with the OMAP bus
  326. */
  327. static struct platform_driver ohci_hcd_omap_driver = {
  328. .probe = ohci_hcd_omap_probe,
  329. .remove_new = ohci_hcd_omap_remove,
  330. .shutdown = usb_hcd_platform_shutdown,
  331. #ifdef CONFIG_PM
  332. .suspend = ohci_omap_suspend,
  333. .resume = ohci_omap_resume,
  334. #endif
  335. .driver = {
  336. .name = "ohci",
  337. },
  338. };
  339. static const struct ohci_driver_overrides omap_overrides __initconst = {
  340. .product_desc = "OMAP OHCI",
  341. .reset = ohci_omap_reset,
  342. .extra_priv_size = sizeof(struct ohci_omap_priv),
  343. };
  344. static int __init ohci_omap_init(void)
  345. {
  346. if (usb_disabled())
  347. return -ENODEV;
  348. ohci_init_driver(&ohci_omap_hc_driver, &omap_overrides);
  349. return platform_driver_register(&ohci_hcd_omap_driver);
  350. }
  351. module_init(ohci_omap_init);
  352. static void __exit ohci_omap_cleanup(void)
  353. {
  354. platform_driver_unregister(&ohci_hcd_omap_driver);
  355. }
  356. module_exit(ohci_omap_cleanup);
  357. MODULE_DESCRIPTION(DRIVER_DESC);
  358. MODULE_ALIAS("platform:ohci");
  359. MODULE_LICENSE("GPL");