ohci-omap.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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/i2c/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. dev_err(hcd->self.controller, "can't find phy\n");
  194. return -ENODEV;
  195. }
  196. ohci->start_hnp = start_hnp;
  197. }
  198. #endif
  199. omap_ohci_clock_power(1);
  200. if (cpu_is_omap15xx()) {
  201. omap_1510_local_bus_power(1);
  202. omap_1510_local_bus_init();
  203. }
  204. ret = ohci_setup(hcd);
  205. if (ret < 0)
  206. return ret;
  207. if (config->otg || config->rwc) {
  208. ohci->hc_control = OHCI_CTRL_RWC;
  209. writel(OHCI_CTRL_RWC, &ohci->regs->control);
  210. }
  211. /* board-specific power switching and overcurrent support */
  212. if (machine_is_omap_osk() || machine_is_omap_innovator()) {
  213. u32 rh = roothub_a (ohci);
  214. /* power switching (ganged by default) */
  215. rh &= ~RH_A_NPS;
  216. /* TPS2045 switch for internal transceiver (port 1) */
  217. if (machine_is_omap_osk()) {
  218. ohci_to_hcd(ohci)->power_budget = 250;
  219. rh &= ~RH_A_NOCP;
  220. /* gpio9 for overcurrent detction */
  221. omap_cfg_reg(W8_1610_GPIO9);
  222. gpio_request(9, "OHCI overcurrent");
  223. gpio_direction_input(9);
  224. /* for paranoia's sake: disable USB.PUEN */
  225. omap_cfg_reg(W4_USB_HIGHZ);
  226. }
  227. ohci_writel(ohci, rh, &ohci->regs->roothub.a);
  228. ohci->flags &= ~OHCI_QUIRK_HUB_POWER;
  229. } else if (machine_is_nokia770()) {
  230. /* We require a self-powered hub, which should have
  231. * plenty of power. */
  232. ohci_to_hcd(ohci)->power_budget = 0;
  233. }
  234. /* FIXME hub_wq hub requests should manage power switching */
  235. omap_ohci_transceiver_power(1);
  236. /* board init will have already handled HMC and mux setup.
  237. * any external transceiver should already be initialized
  238. * too, so all configured ports use the right signaling now.
  239. */
  240. return 0;
  241. }
  242. /*-------------------------------------------------------------------------*/
  243. /**
  244. * ohci_hcd_omap_probe - initialize OMAP-based HCDs
  245. * Context: !in_interrupt()
  246. *
  247. * Allocates basic resources for this USB host controller, and
  248. * then invokes the start() method for the HCD associated with it
  249. * through the hotplug entry's driver_data.
  250. */
  251. static int ohci_hcd_omap_probe(struct platform_device *pdev)
  252. {
  253. int retval, irq;
  254. struct usb_hcd *hcd = 0;
  255. if (pdev->num_resources != 2) {
  256. dev_err(&pdev->dev, "invalid num_resources: %i\n",
  257. pdev->num_resources);
  258. return -ENODEV;
  259. }
  260. if (pdev->resource[0].flags != IORESOURCE_MEM
  261. || pdev->resource[1].flags != IORESOURCE_IRQ) {
  262. dev_err(&pdev->dev, "invalid resource type\n");
  263. return -ENODEV;
  264. }
  265. usb_host_ck = clk_get(&pdev->dev, "usb_hhc_ck");
  266. if (IS_ERR(usb_host_ck))
  267. return PTR_ERR(usb_host_ck);
  268. if (!cpu_is_omap15xx())
  269. usb_dc_ck = clk_get(&pdev->dev, "usb_dc_ck");
  270. else
  271. usb_dc_ck = clk_get(&pdev->dev, "lb_ck");
  272. if (IS_ERR(usb_dc_ck)) {
  273. clk_put(usb_host_ck);
  274. return PTR_ERR(usb_dc_ck);
  275. }
  276. hcd = usb_create_hcd(&ohci_omap_hc_driver, &pdev->dev,
  277. dev_name(&pdev->dev));
  278. if (!hcd) {
  279. retval = -ENOMEM;
  280. goto err0;
  281. }
  282. hcd->rsrc_start = pdev->resource[0].start;
  283. hcd->rsrc_len = pdev->resource[0].end - pdev->resource[0].start + 1;
  284. if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, hcd_name)) {
  285. dev_dbg(&pdev->dev, "request_mem_region failed\n");
  286. retval = -EBUSY;
  287. goto err1;
  288. }
  289. hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
  290. if (!hcd->regs) {
  291. dev_err(&pdev->dev, "can't ioremap OHCI HCD\n");
  292. retval = -ENOMEM;
  293. goto err2;
  294. }
  295. irq = platform_get_irq(pdev, 0);
  296. if (irq < 0) {
  297. retval = -ENXIO;
  298. goto err3;
  299. }
  300. retval = usb_add_hcd(hcd, irq, 0);
  301. if (retval)
  302. goto err3;
  303. device_wakeup_enable(hcd->self.controller);
  304. return 0;
  305. err3:
  306. iounmap(hcd->regs);
  307. err2:
  308. release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
  309. err1:
  310. usb_put_hcd(hcd);
  311. err0:
  312. clk_put(usb_dc_ck);
  313. clk_put(usb_host_ck);
  314. return retval;
  315. }
  316. /* may be called with controller, bus, and devices active */
  317. /**
  318. * ohci_hcd_omap_remove - shutdown processing for OMAP-based HCDs
  319. * @dev: USB Host Controller being removed
  320. * Context: !in_interrupt()
  321. *
  322. * Reverses the effect of ohci_hcd_omap_probe(), first invoking
  323. * the HCD's stop() method. It is always called from a thread
  324. * context, normally "rmmod", "apmd", or something similar.
  325. */
  326. static int ohci_hcd_omap_remove(struct platform_device *pdev)
  327. {
  328. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  329. dev_dbg(hcd->self.controller, "stopping USB Controller\n");
  330. usb_remove_hcd(hcd);
  331. omap_ohci_clock_power(0);
  332. if (!IS_ERR_OR_NULL(hcd->usb_phy)) {
  333. (void) otg_set_host(hcd->usb_phy->otg, 0);
  334. usb_put_phy(hcd->usb_phy);
  335. }
  336. if (machine_is_omap_osk())
  337. gpio_free(9);
  338. iounmap(hcd->regs);
  339. release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
  340. usb_put_hcd(hcd);
  341. clk_put(usb_dc_ck);
  342. clk_put(usb_host_ck);
  343. return 0;
  344. }
  345. /*-------------------------------------------------------------------------*/
  346. #ifdef CONFIG_PM
  347. static int ohci_omap_suspend(struct platform_device *pdev, pm_message_t message)
  348. {
  349. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  350. struct ohci_hcd *ohci = hcd_to_ohci(hcd);
  351. bool do_wakeup = device_may_wakeup(&pdev->dev);
  352. int ret;
  353. if (time_before(jiffies, ohci->next_statechange))
  354. msleep(5);
  355. ohci->next_statechange = jiffies;
  356. ret = ohci_suspend(hcd, do_wakeup);
  357. if (ret)
  358. return ret;
  359. omap_ohci_clock_power(0);
  360. return ret;
  361. }
  362. static int ohci_omap_resume(struct platform_device *dev)
  363. {
  364. struct usb_hcd *hcd = platform_get_drvdata(dev);
  365. struct ohci_hcd *ohci = hcd_to_ohci(hcd);
  366. if (time_before(jiffies, ohci->next_statechange))
  367. msleep(5);
  368. ohci->next_statechange = jiffies;
  369. omap_ohci_clock_power(1);
  370. ohci_resume(hcd, false);
  371. return 0;
  372. }
  373. #endif
  374. /*-------------------------------------------------------------------------*/
  375. /*
  376. * Driver definition to register with the OMAP bus
  377. */
  378. static struct platform_driver ohci_hcd_omap_driver = {
  379. .probe = ohci_hcd_omap_probe,
  380. .remove = ohci_hcd_omap_remove,
  381. .shutdown = usb_hcd_platform_shutdown,
  382. #ifdef CONFIG_PM
  383. .suspend = ohci_omap_suspend,
  384. .resume = ohci_omap_resume,
  385. #endif
  386. .driver = {
  387. .name = "ohci",
  388. },
  389. };
  390. static const struct ohci_driver_overrides omap_overrides __initconst = {
  391. .product_desc = "OMAP OHCI",
  392. .reset = ohci_omap_reset
  393. };
  394. static int __init ohci_omap_init(void)
  395. {
  396. if (usb_disabled())
  397. return -ENODEV;
  398. pr_info("%s: " DRIVER_DESC "\n", hcd_name);
  399. ohci_init_driver(&ohci_omap_hc_driver, &omap_overrides);
  400. return platform_driver_register(&ohci_hcd_omap_driver);
  401. }
  402. module_init(ohci_omap_init);
  403. static void __exit ohci_omap_cleanup(void)
  404. {
  405. platform_driver_unregister(&ohci_hcd_omap_driver);
  406. }
  407. module_exit(ohci_omap_cleanup);
  408. MODULE_DESCRIPTION(DRIVER_DESC);
  409. MODULE_ALIAS("platform:ohci");
  410. MODULE_LICENSE("GPL");