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