ohci-omap.c 11 KB

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