ohci-omap.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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.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/signal.h>
  28. #include <linux/usb.h>
  29. #include <linux/usb/hcd.h>
  30. #include "ohci.h"
  31. #include <asm/io.h>
  32. #include <asm/mach-types.h>
  33. #include <mach/mux.h>
  34. #include <mach/hardware.h>
  35. #include <mach/usb.h>
  36. /* OMAP-1510 OHCI has its own MMU for DMA */
  37. #define OMAP1510_LB_MEMSIZE 32 /* Should be same as SDRAM size */
  38. #define OMAP1510_LB_CLOCK_DIV 0xfffec10c
  39. #define OMAP1510_LB_MMU_CTL 0xfffec208
  40. #define OMAP1510_LB_MMU_LCK 0xfffec224
  41. #define OMAP1510_LB_MMU_LD_TLB 0xfffec228
  42. #define OMAP1510_LB_MMU_CAM_H 0xfffec22c
  43. #define OMAP1510_LB_MMU_CAM_L 0xfffec230
  44. #define OMAP1510_LB_MMU_RAM_H 0xfffec234
  45. #define OMAP1510_LB_MMU_RAM_L 0xfffec238
  46. #define DRIVER_DESC "OHCI OMAP driver"
  47. #ifdef CONFIG_TPS65010
  48. #include <linux/mfd/tps65010.h>
  49. #else
  50. #define LOW 0
  51. #define HIGH 1
  52. #define GPIO1 1
  53. static inline int tps65010_set_gpio_out_value(unsigned gpio, unsigned value)
  54. {
  55. return 0;
  56. }
  57. #endif
  58. static struct clk *usb_host_ck;
  59. static struct clk *usb_dc_ck;
  60. static const char hcd_name[] = "ohci-omap";
  61. static struct hc_driver __read_mostly ohci_omap_hc_driver;
  62. static void omap_ohci_clock_power(int on)
  63. {
  64. if (on) {
  65. clk_enable(usb_dc_ck);
  66. clk_enable(usb_host_ck);
  67. /* guesstimate for T5 == 1x 32K clock + APLL lock time */
  68. udelay(100);
  69. } else {
  70. clk_disable(usb_host_ck);
  71. clk_disable(usb_dc_ck);
  72. }
  73. }
  74. /*
  75. * Board specific gang-switched transceiver power on/off.
  76. * NOTE: OSK supplies power from DC, not battery.
  77. */
  78. static int omap_ohci_transceiver_power(int on)
  79. {
  80. if (on) {
  81. if (machine_is_omap_innovator() && cpu_is_omap1510())
  82. __raw_writeb(__raw_readb(INNOVATOR_FPGA_CAM_USB_CONTROL)
  83. | ((1 << 5/*usb1*/) | (1 << 3/*usb2*/)),
  84. INNOVATOR_FPGA_CAM_USB_CONTROL);
  85. else if (machine_is_omap_osk())
  86. tps65010_set_gpio_out_value(GPIO1, LOW);
  87. } else {
  88. if (machine_is_omap_innovator() && cpu_is_omap1510())
  89. __raw_writeb(__raw_readb(INNOVATOR_FPGA_CAM_USB_CONTROL)
  90. & ~((1 << 5/*usb1*/) | (1 << 3/*usb2*/)),
  91. INNOVATOR_FPGA_CAM_USB_CONTROL);
  92. else if (machine_is_omap_osk())
  93. tps65010_set_gpio_out_value(GPIO1, HIGH);
  94. }
  95. return 0;
  96. }
  97. #ifdef CONFIG_ARCH_OMAP15XX
  98. /*
  99. * OMAP-1510 specific Local Bus clock on/off
  100. */
  101. static int omap_1510_local_bus_power(int on)
  102. {
  103. if (on) {
  104. omap_writel((1 << 1) | (1 << 0), OMAP1510_LB_MMU_CTL);
  105. udelay(200);
  106. } else {
  107. omap_writel(0, OMAP1510_LB_MMU_CTL);
  108. }
  109. return 0;
  110. }
  111. /*
  112. * OMAP-1510 specific Local Bus initialization
  113. * NOTE: This assumes 32MB memory size in OMAP1510LB_MEMSIZE.
  114. * See also arch/mach-omap/memory.h for __virt_to_dma() and
  115. * __dma_to_virt() which need to match with the physical
  116. * Local Bus address below.
  117. */
  118. static int omap_1510_local_bus_init(void)
  119. {
  120. unsigned int tlb;
  121. unsigned long lbaddr, physaddr;
  122. omap_writel((omap_readl(OMAP1510_LB_CLOCK_DIV) & 0xfffffff8) | 0x4,
  123. OMAP1510_LB_CLOCK_DIV);
  124. /* Configure the Local Bus MMU table */
  125. for (tlb = 0; tlb < OMAP1510_LB_MEMSIZE; tlb++) {
  126. lbaddr = tlb * 0x00100000 + OMAP1510_LB_OFFSET;
  127. physaddr = tlb * 0x00100000 + PHYS_OFFSET;
  128. omap_writel((lbaddr & 0x0fffffff) >> 22, OMAP1510_LB_MMU_CAM_H);
  129. omap_writel(((lbaddr & 0x003ffc00) >> 6) | 0xc,
  130. OMAP1510_LB_MMU_CAM_L);
  131. omap_writel(physaddr >> 16, OMAP1510_LB_MMU_RAM_H);
  132. omap_writel((physaddr & 0x0000fc00) | 0x300, OMAP1510_LB_MMU_RAM_L);
  133. omap_writel(tlb << 4, OMAP1510_LB_MMU_LCK);
  134. omap_writel(0x1, OMAP1510_LB_MMU_LD_TLB);
  135. }
  136. /* Enable the walking table */
  137. omap_writel(omap_readl(OMAP1510_LB_MMU_CTL) | (1 << 3), OMAP1510_LB_MMU_CTL);
  138. udelay(200);
  139. return 0;
  140. }
  141. #else
  142. #define omap_1510_local_bus_power(x) {}
  143. #define omap_1510_local_bus_init() {}
  144. #endif
  145. #ifdef CONFIG_USB_OTG
  146. static void start_hnp(struct ohci_hcd *ohci)
  147. {
  148. struct usb_hcd *hcd = ohci_to_hcd(ohci);
  149. const unsigned port = hcd->self.otg_port - 1;
  150. unsigned long flags;
  151. u32 l;
  152. otg_start_hnp(hcd->usb_phy->otg);
  153. local_irq_save(flags);
  154. hcd->usb_phy->otg->state = OTG_STATE_A_SUSPEND;
  155. writel (RH_PS_PSS, &ohci->regs->roothub.portstatus [port]);
  156. l = omap_readl(OTG_CTRL);
  157. l &= ~OTG_A_BUSREQ;
  158. omap_writel(l, OTG_CTRL);
  159. local_irq_restore(flags);
  160. }
  161. #endif
  162. /*-------------------------------------------------------------------------*/
  163. static int ohci_omap_reset(struct usb_hcd *hcd)
  164. {
  165. struct ohci_hcd *ohci = hcd_to_ohci(hcd);
  166. struct omap_usb_config *config = dev_get_platdata(hcd->self.controller);
  167. int need_transceiver = (config->otg != 0);
  168. int ret;
  169. dev_dbg(hcd->self.controller, "starting USB Controller\n");
  170. if (config->otg) {
  171. hcd->self.otg_port = config->otg;
  172. /* default/minimum OTG power budget: 8 mA */
  173. hcd->power_budget = 8;
  174. }
  175. /* boards can use OTG transceivers in non-OTG modes */
  176. need_transceiver = need_transceiver
  177. || machine_is_omap_h2() || machine_is_omap_h3();
  178. /* XXX OMAP16xx only */
  179. if (config->ocpi_enable)
  180. config->ocpi_enable();
  181. #ifdef CONFIG_USB_OTG
  182. if (need_transceiver) {
  183. hcd->usb_phy = usb_get_phy(USB_PHY_TYPE_USB2);
  184. if (!IS_ERR_OR_NULL(hcd->usb_phy)) {
  185. int status = otg_set_host(hcd->usb_phy->otg,
  186. &ohci_to_hcd(ohci)->self);
  187. dev_dbg(hcd->self.controller, "init %s phy, status %d\n",
  188. hcd->usb_phy->label, status);
  189. if (status) {
  190. usb_put_phy(hcd->usb_phy);
  191. return status;
  192. }
  193. } else {
  194. return -EPROBE_DEFER;
  195. }
  196. hcd->skip_phy_initialization = 1;
  197. ohci->start_hnp = start_hnp;
  198. }
  199. #endif
  200. omap_ohci_clock_power(1);
  201. if (cpu_is_omap15xx()) {
  202. omap_1510_local_bus_power(1);
  203. omap_1510_local_bus_init();
  204. }
  205. ret = ohci_setup(hcd);
  206. if (ret < 0)
  207. return ret;
  208. if (config->otg || config->rwc) {
  209. ohci->hc_control = OHCI_CTRL_RWC;
  210. writel(OHCI_CTRL_RWC, &ohci->regs->control);
  211. }
  212. /* board-specific power switching and overcurrent support */
  213. if (machine_is_omap_osk() || machine_is_omap_innovator()) {
  214. u32 rh = roothub_a (ohci);
  215. /* power switching (ganged by default) */
  216. rh &= ~RH_A_NPS;
  217. /* TPS2045 switch for internal transceiver (port 1) */
  218. if (machine_is_omap_osk()) {
  219. ohci_to_hcd(ohci)->power_budget = 250;
  220. rh &= ~RH_A_NOCP;
  221. /* gpio9 for overcurrent detction */
  222. omap_cfg_reg(W8_1610_GPIO9);
  223. gpio_request(9, "OHCI overcurrent");
  224. gpio_direction_input(9);
  225. /* for paranoia's sake: disable USB.PUEN */
  226. omap_cfg_reg(W4_USB_HIGHZ);
  227. }
  228. ohci_writel(ohci, rh, &ohci->regs->roothub.a);
  229. ohci->flags &= ~OHCI_QUIRK_HUB_POWER;
  230. } else if (machine_is_nokia770()) {
  231. /* We require a self-powered hub, which should have
  232. * plenty of power. */
  233. ohci_to_hcd(ohci)->power_budget = 0;
  234. }
  235. /* FIXME hub_wq hub requests should manage power switching */
  236. omap_ohci_transceiver_power(1);
  237. /* board init will have already handled HMC and mux setup.
  238. * any external transceiver should already be initialized
  239. * too, so all configured ports use the right signaling now.
  240. */
  241. return 0;
  242. }
  243. /*-------------------------------------------------------------------------*/
  244. /**
  245. * ohci_hcd_omap_probe - initialize OMAP-based HCDs
  246. * Context: !in_interrupt()
  247. *
  248. * Allocates basic resources for this USB host controller, and
  249. * then invokes the start() method for the HCD associated with it
  250. * through the hotplug entry's driver_data.
  251. */
  252. static int ohci_hcd_omap_probe(struct platform_device *pdev)
  253. {
  254. int retval, irq;
  255. struct usb_hcd *hcd = 0;
  256. if (pdev->num_resources != 2) {
  257. dev_err(&pdev->dev, "invalid num_resources: %i\n",
  258. pdev->num_resources);
  259. return -ENODEV;
  260. }
  261. if (pdev->resource[0].flags != IORESOURCE_MEM
  262. || pdev->resource[1].flags != IORESOURCE_IRQ) {
  263. dev_err(&pdev->dev, "invalid resource type\n");
  264. return -ENODEV;
  265. }
  266. usb_host_ck = clk_get(&pdev->dev, "usb_hhc_ck");
  267. if (IS_ERR(usb_host_ck))
  268. return PTR_ERR(usb_host_ck);
  269. if (!cpu_is_omap15xx())
  270. usb_dc_ck = clk_get(&pdev->dev, "usb_dc_ck");
  271. else
  272. usb_dc_ck = clk_get(&pdev->dev, "lb_ck");
  273. if (IS_ERR(usb_dc_ck)) {
  274. clk_put(usb_host_ck);
  275. return PTR_ERR(usb_dc_ck);
  276. }
  277. hcd = usb_create_hcd(&ohci_omap_hc_driver, &pdev->dev,
  278. dev_name(&pdev->dev));
  279. if (!hcd) {
  280. retval = -ENOMEM;
  281. goto err0;
  282. }
  283. hcd->rsrc_start = pdev->resource[0].start;
  284. hcd->rsrc_len = pdev->resource[0].end - pdev->resource[0].start + 1;
  285. if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, hcd_name)) {
  286. dev_dbg(&pdev->dev, "request_mem_region failed\n");
  287. retval = -EBUSY;
  288. goto err1;
  289. }
  290. hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
  291. if (!hcd->regs) {
  292. dev_err(&pdev->dev, "can't ioremap OHCI HCD\n");
  293. retval = -ENOMEM;
  294. goto err2;
  295. }
  296. irq = platform_get_irq(pdev, 0);
  297. if (irq < 0) {
  298. retval = -ENXIO;
  299. goto err3;
  300. }
  301. retval = usb_add_hcd(hcd, irq, 0);
  302. if (retval)
  303. goto err3;
  304. device_wakeup_enable(hcd->self.controller);
  305. return 0;
  306. err3:
  307. iounmap(hcd->regs);
  308. err2:
  309. release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
  310. err1:
  311. usb_put_hcd(hcd);
  312. err0:
  313. clk_put(usb_dc_ck);
  314. clk_put(usb_host_ck);
  315. return retval;
  316. }
  317. /* may be called with controller, bus, and devices active */
  318. /**
  319. * ohci_hcd_omap_remove - shutdown processing for OMAP-based HCDs
  320. * @dev: USB Host Controller being removed
  321. * Context: !in_interrupt()
  322. *
  323. * Reverses the effect of ohci_hcd_omap_probe(), first invoking
  324. * the HCD's stop() method. It is always called from a thread
  325. * context, normally "rmmod", "apmd", or something similar.
  326. */
  327. static int ohci_hcd_omap_remove(struct platform_device *pdev)
  328. {
  329. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  330. dev_dbg(hcd->self.controller, "stopping USB Controller\n");
  331. usb_remove_hcd(hcd);
  332. omap_ohci_clock_power(0);
  333. if (!IS_ERR_OR_NULL(hcd->usb_phy)) {
  334. (void) otg_set_host(hcd->usb_phy->otg, 0);
  335. usb_put_phy(hcd->usb_phy);
  336. }
  337. if (machine_is_omap_osk())
  338. gpio_free(9);
  339. iounmap(hcd->regs);
  340. release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
  341. usb_put_hcd(hcd);
  342. clk_put(usb_dc_ck);
  343. clk_put(usb_host_ck);
  344. return 0;
  345. }
  346. /*-------------------------------------------------------------------------*/
  347. #ifdef CONFIG_PM
  348. static int ohci_omap_suspend(struct platform_device *pdev, pm_message_t message)
  349. {
  350. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  351. struct ohci_hcd *ohci = hcd_to_ohci(hcd);
  352. bool do_wakeup = device_may_wakeup(&pdev->dev);
  353. int ret;
  354. if (time_before(jiffies, ohci->next_statechange))
  355. msleep(5);
  356. ohci->next_statechange = jiffies;
  357. ret = ohci_suspend(hcd, do_wakeup);
  358. if (ret)
  359. return ret;
  360. omap_ohci_clock_power(0);
  361. return ret;
  362. }
  363. static int ohci_omap_resume(struct platform_device *dev)
  364. {
  365. struct usb_hcd *hcd = platform_get_drvdata(dev);
  366. struct ohci_hcd *ohci = hcd_to_ohci(hcd);
  367. if (time_before(jiffies, ohci->next_statechange))
  368. msleep(5);
  369. ohci->next_statechange = jiffies;
  370. omap_ohci_clock_power(1);
  371. ohci_resume(hcd, false);
  372. return 0;
  373. }
  374. #endif
  375. /*-------------------------------------------------------------------------*/
  376. /*
  377. * Driver definition to register with the OMAP bus
  378. */
  379. static struct platform_driver ohci_hcd_omap_driver = {
  380. .probe = ohci_hcd_omap_probe,
  381. .remove = ohci_hcd_omap_remove,
  382. .shutdown = usb_hcd_platform_shutdown,
  383. #ifdef CONFIG_PM
  384. .suspend = ohci_omap_suspend,
  385. .resume = ohci_omap_resume,
  386. #endif
  387. .driver = {
  388. .name = "ohci",
  389. },
  390. };
  391. static const struct ohci_driver_overrides omap_overrides __initconst = {
  392. .product_desc = "OMAP OHCI",
  393. .reset = ohci_omap_reset
  394. };
  395. static int __init ohci_omap_init(void)
  396. {
  397. if (usb_disabled())
  398. return -ENODEV;
  399. pr_info("%s: " DRIVER_DESC "\n", hcd_name);
  400. ohci_init_driver(&ohci_omap_hc_driver, &omap_overrides);
  401. return platform_driver_register(&ohci_hcd_omap_driver);
  402. }
  403. module_init(ohci_omap_init);
  404. static void __exit ohci_omap_cleanup(void)
  405. {
  406. platform_driver_unregister(&ohci_hcd_omap_driver);
  407. }
  408. module_exit(ohci_omap_cleanup);
  409. MODULE_DESCRIPTION(DRIVER_DESC);
  410. MODULE_ALIAS("platform:ohci");
  411. MODULE_LICENSE("GPL");