ohci-omap.c 13 KB

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