da8xx.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /*
  2. * Texas Instruments DA8xx/OMAP-L1x "glue layer"
  3. *
  4. * Copyright (c) 2008-2009 MontaVista Software, Inc. <source@mvista.com>
  5. *
  6. * Based on the DaVinci "glue layer" code.
  7. * Copyright (C) 2005-2006 by Texas Instruments
  8. *
  9. * This file is part of the Inventra Controller Driver for Linux.
  10. *
  11. * The Inventra Controller Driver for Linux is free software; you
  12. * can redistribute it and/or modify it under the terms of the GNU
  13. * General Public License version 2 as published by the Free Software
  14. * Foundation.
  15. *
  16. * The Inventra Controller Driver for Linux is distributed in
  17. * the hope that it will be useful, but WITHOUT ANY WARRANTY;
  18. * without even the implied warranty of MERCHANTABILITY or
  19. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  20. * License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with The Inventra Controller Driver for Linux ; if not,
  24. * write to the Free Software Foundation, Inc., 59 Temple Place,
  25. * Suite 330, Boston, MA 02111-1307 USA
  26. *
  27. */
  28. #include <linux/init.h>
  29. #include <linux/clk.h>
  30. #include <linux/io.h>
  31. #include <mach/da8xx.h>
  32. #include <mach/usb.h>
  33. #include "musb_core.h"
  34. /*
  35. * DA8XX specific definitions
  36. */
  37. /* USB 2.0 OTG module registers */
  38. #define DA8XX_USB_REVISION_REG 0x00
  39. #define DA8XX_USB_CTRL_REG 0x04
  40. #define DA8XX_USB_STAT_REG 0x08
  41. #define DA8XX_USB_EMULATION_REG 0x0c
  42. #define DA8XX_USB_MODE_REG 0x10 /* Transparent, CDC, [Generic] RNDIS */
  43. #define DA8XX_USB_AUTOREQ_REG 0x14
  44. #define DA8XX_USB_SRP_FIX_TIME_REG 0x18
  45. #define DA8XX_USB_TEARDOWN_REG 0x1c
  46. #define DA8XX_USB_INTR_SRC_REG 0x20
  47. #define DA8XX_USB_INTR_SRC_SET_REG 0x24
  48. #define DA8XX_USB_INTR_SRC_CLEAR_REG 0x28
  49. #define DA8XX_USB_INTR_MASK_REG 0x2c
  50. #define DA8XX_USB_INTR_MASK_SET_REG 0x30
  51. #define DA8XX_USB_INTR_MASK_CLEAR_REG 0x34
  52. #define DA8XX_USB_INTR_SRC_MASKED_REG 0x38
  53. #define DA8XX_USB_END_OF_INTR_REG 0x3c
  54. #define DA8XX_USB_GENERIC_RNDIS_EP_SIZE_REG(n) (0x50 + (((n) - 1) << 2))
  55. /* Control register bits */
  56. #define DA8XX_SOFT_RESET_MASK 1
  57. #define DA8XX_USB_TX_EP_MASK 0x1f /* EP0 + 4 Tx EPs */
  58. #define DA8XX_USB_RX_EP_MASK 0x1e /* 4 Rx EPs */
  59. /* USB interrupt register bits */
  60. #define DA8XX_INTR_USB_SHIFT 16
  61. #define DA8XX_INTR_USB_MASK (0x1ff << DA8XX_INTR_USB_SHIFT) /* 8 Mentor */
  62. /* interrupts and DRVVBUS interrupt */
  63. #define DA8XX_INTR_DRVVBUS 0x100
  64. #define DA8XX_INTR_RX_SHIFT 8
  65. #define DA8XX_INTR_RX_MASK (DA8XX_USB_RX_EP_MASK << DA8XX_INTR_RX_SHIFT)
  66. #define DA8XX_INTR_TX_SHIFT 0
  67. #define DA8XX_INTR_TX_MASK (DA8XX_USB_TX_EP_MASK << DA8XX_INTR_TX_SHIFT)
  68. #define DA8XX_MENTOR_CORE_OFFSET 0x400
  69. #define CFGCHIP2 IO_ADDRESS(DA8XX_SYSCFG0_BASE + DA8XX_CFGCHIP2_REG)
  70. /*
  71. * REVISIT (PM): we should be able to keep the PHY in low power mode most
  72. * of the time (24 MHz oscillator and PLL off, etc.) by setting POWER.D0
  73. * and, when in host mode, autosuspending idle root ports... PHY_PLLON
  74. * (overriding SUSPENDM?) then likely needs to stay off.
  75. */
  76. static inline void phy_on(void)
  77. {
  78. u32 cfgchip2 = __raw_readl(CFGCHIP2);
  79. /*
  80. * Start the on-chip PHY and its PLL.
  81. */
  82. cfgchip2 &= ~(CFGCHIP2_RESET | CFGCHIP2_PHYPWRDN | CFGCHIP2_OTGPWRDN);
  83. cfgchip2 |= CFGCHIP2_PHY_PLLON;
  84. __raw_writel(cfgchip2, CFGCHIP2);
  85. pr_info("Waiting for USB PHY clock good...\n");
  86. while (!(__raw_readl(CFGCHIP2) & CFGCHIP2_PHYCLKGD))
  87. cpu_relax();
  88. }
  89. static inline void phy_off(void)
  90. {
  91. u32 cfgchip2 = __raw_readl(CFGCHIP2);
  92. /*
  93. * Ensure that USB 1.1 reference clock is not being sourced from
  94. * USB 2.0 PHY. Otherwise do not power down the PHY.
  95. */
  96. if (!(cfgchip2 & CFGCHIP2_USB1PHYCLKMUX) &&
  97. (cfgchip2 & CFGCHIP2_USB1SUSPENDM)) {
  98. pr_warning("USB 1.1 clocked from USB 2.0 PHY -- "
  99. "can't power it down\n");
  100. return;
  101. }
  102. /*
  103. * Power down the on-chip PHY.
  104. */
  105. cfgchip2 |= CFGCHIP2_PHYPWRDN | CFGCHIP2_OTGPWRDN;
  106. __raw_writel(cfgchip2, CFGCHIP2);
  107. }
  108. /*
  109. * Because we don't set CTRL.UINT, it's "important" to:
  110. * - not read/write INTRUSB/INTRUSBE (except during
  111. * initial setup, as a workaround);
  112. * - use INTSET/INTCLR instead.
  113. */
  114. /**
  115. * musb_platform_enable - enable interrupts
  116. */
  117. void musb_platform_enable(struct musb *musb)
  118. {
  119. void __iomem *reg_base = musb->ctrl_base;
  120. u32 mask;
  121. /* Workaround: setup IRQs through both register sets. */
  122. mask = ((musb->epmask & DA8XX_USB_TX_EP_MASK) << DA8XX_INTR_TX_SHIFT) |
  123. ((musb->epmask & DA8XX_USB_RX_EP_MASK) << DA8XX_INTR_RX_SHIFT) |
  124. DA8XX_INTR_USB_MASK;
  125. musb_writel(reg_base, DA8XX_USB_INTR_MASK_SET_REG, mask);
  126. /* Force the DRVVBUS IRQ so we can start polling for ID change. */
  127. if (is_otg_enabled(musb))
  128. musb_writel(reg_base, DA8XX_USB_INTR_SRC_SET_REG,
  129. DA8XX_INTR_DRVVBUS << DA8XX_INTR_USB_SHIFT);
  130. }
  131. /**
  132. * musb_platform_disable - disable HDRC and flush interrupts
  133. */
  134. void musb_platform_disable(struct musb *musb)
  135. {
  136. void __iomem *reg_base = musb->ctrl_base;
  137. musb_writel(reg_base, DA8XX_USB_INTR_MASK_CLEAR_REG,
  138. DA8XX_INTR_USB_MASK |
  139. DA8XX_INTR_TX_MASK | DA8XX_INTR_RX_MASK);
  140. musb_writeb(musb->mregs, MUSB_DEVCTL, 0);
  141. musb_writel(reg_base, DA8XX_USB_END_OF_INTR_REG, 0);
  142. }
  143. #ifdef CONFIG_USB_MUSB_HDRC_HCD
  144. #define portstate(stmt) stmt
  145. #else
  146. #define portstate(stmt)
  147. #endif
  148. static void da8xx_set_vbus(struct musb *musb, int is_on)
  149. {
  150. WARN_ON(is_on && is_peripheral_active(musb));
  151. }
  152. #define POLL_SECONDS 2
  153. static struct timer_list otg_workaround;
  154. static void otg_timer(unsigned long _musb)
  155. {
  156. struct musb *musb = (void *)_musb;
  157. void __iomem *mregs = musb->mregs;
  158. u8 devctl;
  159. unsigned long flags;
  160. /*
  161. * We poll because DaVinci's won't expose several OTG-critical
  162. * status change events (from the transceiver) otherwise.
  163. */
  164. devctl = musb_readb(mregs, MUSB_DEVCTL);
  165. DBG(7, "Poll devctl %02x (%s)\n", devctl, otg_state_string(musb));
  166. spin_lock_irqsave(&musb->lock, flags);
  167. switch (musb->xceiv->state) {
  168. case OTG_STATE_A_WAIT_BCON:
  169. devctl &= ~MUSB_DEVCTL_SESSION;
  170. musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
  171. devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
  172. if (devctl & MUSB_DEVCTL_BDEVICE) {
  173. musb->xceiv->state = OTG_STATE_B_IDLE;
  174. MUSB_DEV_MODE(musb);
  175. } else {
  176. musb->xceiv->state = OTG_STATE_A_IDLE;
  177. MUSB_HST_MODE(musb);
  178. }
  179. break;
  180. case OTG_STATE_A_WAIT_VFALL:
  181. /*
  182. * Wait till VBUS falls below SessionEnd (~0.2 V); the 1.3
  183. * RTL seems to mis-handle session "start" otherwise (or in
  184. * our case "recover"), in routine "VBUS was valid by the time
  185. * VBUSERR got reported during enumeration" cases.
  186. */
  187. if (devctl & MUSB_DEVCTL_VBUS) {
  188. mod_timer(&otg_workaround, jiffies + POLL_SECONDS * HZ);
  189. break;
  190. }
  191. musb->xceiv->state = OTG_STATE_A_WAIT_VRISE;
  192. musb_writel(musb->ctrl_base, DA8XX_USB_INTR_SRC_SET_REG,
  193. MUSB_INTR_VBUSERROR << DA8XX_INTR_USB_SHIFT);
  194. break;
  195. case OTG_STATE_B_IDLE:
  196. if (!is_peripheral_enabled(musb))
  197. break;
  198. /*
  199. * There's no ID-changed IRQ, so we have no good way to tell
  200. * when to switch to the A-Default state machine (by setting
  201. * the DEVCTL.Session bit).
  202. *
  203. * Workaround: whenever we're in B_IDLE, try setting the
  204. * session flag every few seconds. If it works, ID was
  205. * grounded and we're now in the A-Default state machine.
  206. *
  207. * NOTE: setting the session flag is _supposed_ to trigger
  208. * SRP but clearly it doesn't.
  209. */
  210. musb_writeb(mregs, MUSB_DEVCTL, devctl | MUSB_DEVCTL_SESSION);
  211. devctl = musb_readb(mregs, MUSB_DEVCTL);
  212. if (devctl & MUSB_DEVCTL_BDEVICE)
  213. mod_timer(&otg_workaround, jiffies + POLL_SECONDS * HZ);
  214. else
  215. musb->xceiv->state = OTG_STATE_A_IDLE;
  216. break;
  217. default:
  218. break;
  219. }
  220. spin_unlock_irqrestore(&musb->lock, flags);
  221. }
  222. void musb_platform_try_idle(struct musb *musb, unsigned long timeout)
  223. {
  224. static unsigned long last_timer;
  225. if (!is_otg_enabled(musb))
  226. return;
  227. if (timeout == 0)
  228. timeout = jiffies + msecs_to_jiffies(3);
  229. /* Never idle if active, or when VBUS timeout is not set as host */
  230. if (musb->is_active || (musb->a_wait_bcon == 0 &&
  231. musb->xceiv->state == OTG_STATE_A_WAIT_BCON)) {
  232. DBG(4, "%s active, deleting timer\n", otg_state_string(musb));
  233. del_timer(&otg_workaround);
  234. last_timer = jiffies;
  235. return;
  236. }
  237. if (time_after(last_timer, timeout) && timer_pending(&otg_workaround)) {
  238. DBG(4, "Longer idle timer already pending, ignoring...\n");
  239. return;
  240. }
  241. last_timer = timeout;
  242. DBG(4, "%s inactive, starting idle timer for %u ms\n",
  243. otg_state_string(musb), jiffies_to_msecs(timeout - jiffies));
  244. mod_timer(&otg_workaround, timeout);
  245. }
  246. static irqreturn_t da8xx_interrupt(int irq, void *hci)
  247. {
  248. struct musb *musb = hci;
  249. void __iomem *reg_base = musb->ctrl_base;
  250. unsigned long flags;
  251. irqreturn_t ret = IRQ_NONE;
  252. u32 status;
  253. spin_lock_irqsave(&musb->lock, flags);
  254. /*
  255. * NOTE: DA8XX shadows the Mentor IRQs. Don't manage them through
  256. * the Mentor registers (except for setup), use the TI ones and EOI.
  257. */
  258. /* Acknowledge and handle non-CPPI interrupts */
  259. status = musb_readl(reg_base, DA8XX_USB_INTR_SRC_MASKED_REG);
  260. if (!status)
  261. goto eoi;
  262. musb_writel(reg_base, DA8XX_USB_INTR_SRC_CLEAR_REG, status);
  263. DBG(4, "USB IRQ %08x\n", status);
  264. musb->int_rx = (status & DA8XX_INTR_RX_MASK) >> DA8XX_INTR_RX_SHIFT;
  265. musb->int_tx = (status & DA8XX_INTR_TX_MASK) >> DA8XX_INTR_TX_SHIFT;
  266. musb->int_usb = (status & DA8XX_INTR_USB_MASK) >> DA8XX_INTR_USB_SHIFT;
  267. /*
  268. * DRVVBUS IRQs are the only proxy we have (a very poor one!) for
  269. * DA8xx's missing ID change IRQ. We need an ID change IRQ to
  270. * switch appropriately between halves of the OTG state machine.
  271. * Managing DEVCTL.Session per Mentor docs requires that we know its
  272. * value but DEVCTL.BDevice is invalid without DEVCTL.Session set.
  273. * Also, DRVVBUS pulses for SRP (but not at 5 V)...
  274. */
  275. if (status & (DA8XX_INTR_DRVVBUS << DA8XX_INTR_USB_SHIFT)) {
  276. int drvvbus = musb_readl(reg_base, DA8XX_USB_STAT_REG);
  277. void __iomem *mregs = musb->mregs;
  278. u8 devctl = musb_readb(mregs, MUSB_DEVCTL);
  279. int err;
  280. err = is_host_enabled(musb) && (musb->int_usb &
  281. MUSB_INTR_VBUSERROR);
  282. if (err) {
  283. /*
  284. * The Mentor core doesn't debounce VBUS as needed
  285. * to cope with device connect current spikes. This
  286. * means it's not uncommon for bus-powered devices
  287. * to get VBUS errors during enumeration.
  288. *
  289. * This is a workaround, but newer RTL from Mentor
  290. * seems to allow a better one: "re"-starting sessions
  291. * without waiting for VBUS to stop registering in
  292. * devctl.
  293. */
  294. musb->int_usb &= ~MUSB_INTR_VBUSERROR;
  295. musb->xceiv->state = OTG_STATE_A_WAIT_VFALL;
  296. mod_timer(&otg_workaround, jiffies + POLL_SECONDS * HZ);
  297. WARNING("VBUS error workaround (delay coming)\n");
  298. } else if (is_host_enabled(musb) && drvvbus) {
  299. MUSB_HST_MODE(musb);
  300. musb->xceiv->default_a = 1;
  301. musb->xceiv->state = OTG_STATE_A_WAIT_VRISE;
  302. portstate(musb->port1_status |= USB_PORT_STAT_POWER);
  303. del_timer(&otg_workaround);
  304. } else {
  305. musb->is_active = 0;
  306. MUSB_DEV_MODE(musb);
  307. musb->xceiv->default_a = 0;
  308. musb->xceiv->state = OTG_STATE_B_IDLE;
  309. portstate(musb->port1_status &= ~USB_PORT_STAT_POWER);
  310. }
  311. DBG(2, "VBUS %s (%s)%s, devctl %02x\n",
  312. drvvbus ? "on" : "off",
  313. otg_state_string(musb),
  314. err ? " ERROR" : "",
  315. devctl);
  316. ret = IRQ_HANDLED;
  317. }
  318. if (musb->int_tx || musb->int_rx || musb->int_usb)
  319. ret |= musb_interrupt(musb);
  320. eoi:
  321. /* EOI needs to be written for the IRQ to be re-asserted. */
  322. if (ret == IRQ_HANDLED || status)
  323. musb_writel(reg_base, DA8XX_USB_END_OF_INTR_REG, 0);
  324. /* Poll for ID change */
  325. if (is_otg_enabled(musb) && musb->xceiv->state == OTG_STATE_B_IDLE)
  326. mod_timer(&otg_workaround, jiffies + POLL_SECONDS * HZ);
  327. spin_unlock_irqrestore(&musb->lock, flags);
  328. return ret;
  329. }
  330. int musb_platform_set_mode(struct musb *musb, u8 musb_mode)
  331. {
  332. u32 cfgchip2 = __raw_readl(CFGCHIP2);
  333. cfgchip2 &= ~CFGCHIP2_OTGMODE;
  334. switch (musb_mode) {
  335. #ifdef CONFIG_USB_MUSB_HDRC_HCD
  336. case MUSB_HOST: /* Force VBUS valid, ID = 0 */
  337. cfgchip2 |= CFGCHIP2_FORCE_HOST;
  338. break;
  339. #endif
  340. #ifdef CONFIG_USB_GADGET_MUSB_HDRC
  341. case MUSB_PERIPHERAL: /* Force VBUS valid, ID = 1 */
  342. cfgchip2 |= CFGCHIP2_FORCE_DEVICE;
  343. break;
  344. #endif
  345. #ifdef CONFIG_USB_MUSB_OTG
  346. case MUSB_OTG: /* Don't override the VBUS/ID comparators */
  347. cfgchip2 |= CFGCHIP2_NO_OVERRIDE;
  348. break;
  349. #endif
  350. default:
  351. DBG(2, "Trying to set unsupported mode %u\n", musb_mode);
  352. }
  353. __raw_writel(cfgchip2, CFGCHIP2);
  354. return 0;
  355. }
  356. int __init musb_platform_init(struct musb *musb, void *board_data)
  357. {
  358. void __iomem *reg_base = musb->ctrl_base;
  359. u32 rev;
  360. musb->mregs += DA8XX_MENTOR_CORE_OFFSET;
  361. clk_enable(musb->clock);
  362. /* Returns zero if e.g. not clocked */
  363. rev = musb_readl(reg_base, DA8XX_USB_REVISION_REG);
  364. if (!rev)
  365. goto fail;
  366. usb_nop_xceiv_register();
  367. musb->xceiv = otg_get_transceiver();
  368. if (!musb->xceiv)
  369. goto fail;
  370. if (is_host_enabled(musb))
  371. setup_timer(&otg_workaround, otg_timer, (unsigned long)musb);
  372. musb->board_set_vbus = da8xx_set_vbus;
  373. /* Reset the controller */
  374. musb_writel(reg_base, DA8XX_USB_CTRL_REG, DA8XX_SOFT_RESET_MASK);
  375. /* Start the on-chip PHY and its PLL. */
  376. phy_on();
  377. msleep(5);
  378. /* NOTE: IRQs are in mixed mode, not bypass to pure MUSB */
  379. pr_debug("DA8xx OTG revision %08x, PHY %03x, control %02x\n",
  380. rev, __raw_readl(CFGCHIP2),
  381. musb_readb(reg_base, DA8XX_USB_CTRL_REG));
  382. musb->isr = da8xx_interrupt;
  383. return 0;
  384. fail:
  385. clk_disable(musb->clock);
  386. return -ENODEV;
  387. }
  388. int musb_platform_exit(struct musb *musb)
  389. {
  390. if (is_host_enabled(musb))
  391. del_timer_sync(&otg_workaround);
  392. phy_off();
  393. otg_put_transceiver(musb->xceiv);
  394. usb_nop_xceiv_unregister();
  395. clk_disable(musb->clock);
  396. return 0;
  397. }