ohci-omap.c 12 KB

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