davinci.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /*
  2. * Copyright (C) 2005-2006 by Texas Instruments
  3. *
  4. * This file is part of the Inventra Controller Driver for Linux.
  5. *
  6. * The Inventra Controller Driver for Linux is free software; you
  7. * can redistribute it and/or modify it under the terms of the GNU
  8. * General Public License version 2 as published by the Free Software
  9. * Foundation.
  10. *
  11. * The Inventra Controller Driver for Linux is distributed in
  12. * the hope that it will be useful, but WITHOUT ANY WARRANTY;
  13. * without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  15. * License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with The Inventra Controller Driver for Linux ; if not,
  19. * write to the Free Software Foundation, Inc., 59 Temple Place,
  20. * Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. */
  23. #include <linux/module.h>
  24. #include <linux/kernel.h>
  25. #include <linux/sched.h>
  26. #include <linux/slab.h>
  27. #include <linux/init.h>
  28. #include <linux/list.h>
  29. #include <linux/delay.h>
  30. #include <linux/clk.h>
  31. #include <linux/io.h>
  32. #include <linux/gpio.h>
  33. #include <mach/hardware.h>
  34. #include <mach/memory.h>
  35. #include <mach/gpio.h>
  36. #include <asm/mach-types.h>
  37. #include "musb_core.h"
  38. #ifdef CONFIG_MACH_DAVINCI_EVM
  39. #define GPIO_nVBUS_DRV 87
  40. #endif
  41. #include "davinci.h"
  42. #include "cppi_dma.h"
  43. /* REVISIT (PM) we should be able to keep the PHY in low power mode most
  44. * of the time (24 MHZ oscillator and PLL off, etc) by setting POWER.D0
  45. * and, when in host mode, autosuspending idle root ports... PHYPLLON
  46. * (overriding SUSPENDM?) then likely needs to stay off.
  47. */
  48. static inline void phy_on(void)
  49. {
  50. /* start the on-chip PHY and its PLL */
  51. __raw_writel(USBPHY_SESNDEN | USBPHY_VBDTCTEN | USBPHY_PHYPLLON,
  52. (void __force __iomem *) IO_ADDRESS(USBPHY_CTL_PADDR));
  53. while ((__raw_readl((void __force __iomem *)
  54. IO_ADDRESS(USBPHY_CTL_PADDR))
  55. & USBPHY_PHYCLKGD) == 0)
  56. cpu_relax();
  57. }
  58. static inline void phy_off(void)
  59. {
  60. /* powerdown the on-chip PHY and its oscillator */
  61. __raw_writel(USBPHY_OSCPDWN | USBPHY_PHYPDWN, (void __force __iomem *)
  62. IO_ADDRESS(USBPHY_CTL_PADDR));
  63. }
  64. static int dma_off = 1;
  65. void musb_platform_enable(struct musb *musb)
  66. {
  67. u32 tmp, old, val;
  68. /* workaround: setup irqs through both register sets */
  69. tmp = (musb->epmask & DAVINCI_USB_TX_ENDPTS_MASK)
  70. << DAVINCI_USB_TXINT_SHIFT;
  71. musb_writel(musb->ctrl_base, DAVINCI_USB_INT_MASK_SET_REG, tmp);
  72. old = tmp;
  73. tmp = (musb->epmask & (0xfffe & DAVINCI_USB_RX_ENDPTS_MASK))
  74. << DAVINCI_USB_RXINT_SHIFT;
  75. musb_writel(musb->ctrl_base, DAVINCI_USB_INT_MASK_SET_REG, tmp);
  76. tmp |= old;
  77. val = ~MUSB_INTR_SOF;
  78. tmp |= ((val & 0x01ff) << DAVINCI_USB_USBINT_SHIFT);
  79. musb_writel(musb->ctrl_base, DAVINCI_USB_INT_MASK_SET_REG, tmp);
  80. if (is_dma_capable() && !dma_off)
  81. printk(KERN_WARNING "%s %s: dma not reactivated\n",
  82. __FILE__, __func__);
  83. else
  84. dma_off = 0;
  85. /* force a DRVVBUS irq so we can start polling for ID change */
  86. if (is_otg_enabled(musb))
  87. musb_writel(musb->ctrl_base, DAVINCI_USB_INT_SET_REG,
  88. DAVINCI_INTR_DRVVBUS << DAVINCI_USB_USBINT_SHIFT);
  89. }
  90. /*
  91. * Disable the HDRC and flush interrupts
  92. */
  93. void musb_platform_disable(struct musb *musb)
  94. {
  95. /* because we don't set CTRLR.UINT, "important" to:
  96. * - not read/write INTRUSB/INTRUSBE
  97. * - (except during initial setup, as workaround)
  98. * - use INTSETR/INTCLRR instead
  99. */
  100. musb_writel(musb->ctrl_base, DAVINCI_USB_INT_MASK_CLR_REG,
  101. DAVINCI_USB_USBINT_MASK
  102. | DAVINCI_USB_TXINT_MASK
  103. | DAVINCI_USB_RXINT_MASK);
  104. musb_writeb(musb->mregs, MUSB_DEVCTL, 0);
  105. musb_writel(musb->ctrl_base, DAVINCI_USB_EOI_REG, 0);
  106. if (is_dma_capable() && !dma_off)
  107. WARNING("dma still active\n");
  108. }
  109. /* REVISIT it's not clear whether DaVinci can support full OTG. */
  110. static int vbus_state = -1;
  111. #ifdef CONFIG_USB_MUSB_HDRC_HCD
  112. #define portstate(stmt) stmt
  113. #else
  114. #define portstate(stmt)
  115. #endif
  116. /* VBUS SWITCHING IS BOARD-SPECIFIC */
  117. #ifdef CONFIG_MACH_DAVINCI_EVM
  118. /* I2C operations are always synchronous, and require a task context.
  119. * With unloaded systems, using the shared workqueue seems to suffice
  120. * to satisfy the 100msec A_WAIT_VRISE timeout...
  121. */
  122. static void evm_deferred_drvvbus(struct work_struct *ignored)
  123. {
  124. gpio_set_value_cansleep(GPIO_nVBUS_DRV, vbus_state);
  125. vbus_state = !vbus_state;
  126. }
  127. static DECLARE_WORK(evm_vbus_work, evm_deferred_drvvbus);
  128. #endif /* EVM */
  129. static void davinci_source_power(struct musb *musb, int is_on, int immediate)
  130. {
  131. if (is_on)
  132. is_on = 1;
  133. if (vbus_state == is_on)
  134. return;
  135. vbus_state = !is_on; /* 0/1 vs "-1 == unknown/init" */
  136. #ifdef CONFIG_MACH_DAVINCI_EVM
  137. if (machine_is_davinci_evm()) {
  138. if (immediate)
  139. gpio_set_value_cansleep(GPIO_nVBUS_DRV, vbus_state);
  140. else
  141. schedule_work(&evm_vbus_work);
  142. }
  143. #endif
  144. if (immediate)
  145. vbus_state = is_on;
  146. }
  147. static void davinci_set_vbus(struct musb *musb, int is_on)
  148. {
  149. WARN_ON(is_on && is_peripheral_active(musb));
  150. davinci_source_power(musb, is_on, 0);
  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. /* We poll because DaVinci's won't expose several OTG-critical
  161. * status change events (from the transceiver) otherwise.
  162. */
  163. devctl = musb_readb(mregs, MUSB_DEVCTL);
  164. DBG(7, "poll devctl %02x (%s)\n", devctl, otg_state_string(musb));
  165. spin_lock_irqsave(&musb->lock, flags);
  166. switch (musb->xceiv.state) {
  167. case OTG_STATE_A_WAIT_VFALL:
  168. /* Wait till VBUS falls below SessionEnd (~0.2V); the 1.3 RTL
  169. * seems to mis-handle session "start" otherwise (or in our
  170. * case "recover"), in routine "VBUS was valid by the time
  171. * VBUSERR got reported during enumeration" cases.
  172. */
  173. if (devctl & MUSB_DEVCTL_VBUS) {
  174. mod_timer(&otg_workaround, jiffies + POLL_SECONDS * HZ);
  175. break;
  176. }
  177. musb->xceiv.state = OTG_STATE_A_WAIT_VRISE;
  178. musb_writel(musb->ctrl_base, DAVINCI_USB_INT_SET_REG,
  179. MUSB_INTR_VBUSERROR << DAVINCI_USB_USBINT_SHIFT);
  180. break;
  181. case OTG_STATE_B_IDLE:
  182. if (!is_peripheral_enabled(musb))
  183. break;
  184. /* There's no ID-changed IRQ, so we have no good way to tell
  185. * when to switch to the A-Default state machine (by setting
  186. * the DEVCTL.SESSION flag).
  187. *
  188. * Workaround: whenever we're in B_IDLE, try setting the
  189. * session flag every few seconds. If it works, ID was
  190. * grounded and we're now in the A-Default state machine.
  191. *
  192. * NOTE setting the session flag is _supposed_ to trigger
  193. * SRP, but clearly it doesn't.
  194. */
  195. musb_writeb(mregs, MUSB_DEVCTL,
  196. devctl | MUSB_DEVCTL_SESSION);
  197. devctl = musb_readb(mregs, MUSB_DEVCTL);
  198. if (devctl & MUSB_DEVCTL_BDEVICE)
  199. mod_timer(&otg_workaround, jiffies + POLL_SECONDS * HZ);
  200. else
  201. musb->xceiv.state = OTG_STATE_A_IDLE;
  202. break;
  203. default:
  204. break;
  205. }
  206. spin_unlock_irqrestore(&musb->lock, flags);
  207. }
  208. static irqreturn_t davinci_interrupt(int irq, void *__hci)
  209. {
  210. unsigned long flags;
  211. irqreturn_t retval = IRQ_NONE;
  212. struct musb *musb = __hci;
  213. void __iomem *tibase = musb->ctrl_base;
  214. u32 tmp;
  215. spin_lock_irqsave(&musb->lock, flags);
  216. /* NOTE: DaVinci shadows the Mentor IRQs. Don't manage them through
  217. * the Mentor registers (except for setup), use the TI ones and EOI.
  218. *
  219. * Docs describe irq "vector" registers asociated with the CPPI and
  220. * USB EOI registers. These hold a bitmask corresponding to the
  221. * current IRQ, not an irq handler address. Would using those bits
  222. * resolve some of the races observed in this dispatch code??
  223. */
  224. /* CPPI interrupts share the same IRQ line, but have their own
  225. * mask, state, "vector", and EOI registers.
  226. */
  227. if (is_cppi_enabled()) {
  228. u32 cppi_tx = musb_readl(tibase, DAVINCI_TXCPPI_MASKED_REG);
  229. u32 cppi_rx = musb_readl(tibase, DAVINCI_RXCPPI_MASKED_REG);
  230. if (cppi_tx || cppi_rx) {
  231. DBG(4, "CPPI IRQ t%x r%x\n", cppi_tx, cppi_rx);
  232. cppi_completion(musb, cppi_rx, cppi_tx);
  233. retval = IRQ_HANDLED;
  234. }
  235. }
  236. /* ack and handle non-CPPI interrupts */
  237. tmp = musb_readl(tibase, DAVINCI_USB_INT_SRC_MASKED_REG);
  238. musb_writel(tibase, DAVINCI_USB_INT_SRC_CLR_REG, tmp);
  239. DBG(4, "IRQ %08x\n", tmp);
  240. musb->int_rx = (tmp & DAVINCI_USB_RXINT_MASK)
  241. >> DAVINCI_USB_RXINT_SHIFT;
  242. musb->int_tx = (tmp & DAVINCI_USB_TXINT_MASK)
  243. >> DAVINCI_USB_TXINT_SHIFT;
  244. musb->int_usb = (tmp & DAVINCI_USB_USBINT_MASK)
  245. >> DAVINCI_USB_USBINT_SHIFT;
  246. /* DRVVBUS irqs are the only proxy we have (a very poor one!) for
  247. * DaVinci's missing ID change IRQ. We need an ID change IRQ to
  248. * switch appropriately between halves of the OTG state machine.
  249. * Managing DEVCTL.SESSION per Mentor docs requires we know its
  250. * value, but DEVCTL.BDEVICE is invalid without DEVCTL.SESSION set.
  251. * Also, DRVVBUS pulses for SRP (but not at 5V) ...
  252. */
  253. if (tmp & (DAVINCI_INTR_DRVVBUS << DAVINCI_USB_USBINT_SHIFT)) {
  254. int drvvbus = musb_readl(tibase, DAVINCI_USB_STAT_REG);
  255. void __iomem *mregs = musb->mregs;
  256. u8 devctl = musb_readb(mregs, MUSB_DEVCTL);
  257. int err = musb->int_usb & MUSB_INTR_VBUSERROR;
  258. err = is_host_enabled(musb)
  259. && (musb->int_usb & MUSB_INTR_VBUSERROR);
  260. if (err) {
  261. /* The Mentor core doesn't debounce VBUS as needed
  262. * to cope with device connect current spikes. This
  263. * means it's not uncommon for bus-powered devices
  264. * to get VBUS errors during enumeration.
  265. *
  266. * This is a workaround, but newer RTL from Mentor
  267. * seems to allow a better one: "re"starting sessions
  268. * without waiting (on EVM, a **long** time) for VBUS
  269. * to stop registering in devctl.
  270. */
  271. musb->int_usb &= ~MUSB_INTR_VBUSERROR;
  272. musb->xceiv.state = OTG_STATE_A_WAIT_VFALL;
  273. mod_timer(&otg_workaround, jiffies + POLL_SECONDS * HZ);
  274. WARNING("VBUS error workaround (delay coming)\n");
  275. } else if (is_host_enabled(musb) && drvvbus) {
  276. musb->is_active = 1;
  277. MUSB_HST_MODE(musb);
  278. musb->xceiv.default_a = 1;
  279. musb->xceiv.state = OTG_STATE_A_WAIT_VRISE;
  280. portstate(musb->port1_status |= USB_PORT_STAT_POWER);
  281. del_timer(&otg_workaround);
  282. } else {
  283. musb->is_active = 0;
  284. MUSB_DEV_MODE(musb);
  285. musb->xceiv.default_a = 0;
  286. musb->xceiv.state = OTG_STATE_B_IDLE;
  287. portstate(musb->port1_status &= ~USB_PORT_STAT_POWER);
  288. }
  289. /* NOTE: this must complete poweron within 100 msec */
  290. davinci_source_power(musb, drvvbus, 0);
  291. DBG(2, "VBUS %s (%s)%s, devctl %02x\n",
  292. drvvbus ? "on" : "off",
  293. otg_state_string(musb),
  294. err ? " ERROR" : "",
  295. devctl);
  296. retval = IRQ_HANDLED;
  297. }
  298. if (musb->int_tx || musb->int_rx || musb->int_usb)
  299. retval |= musb_interrupt(musb);
  300. /* irq stays asserted until EOI is written */
  301. musb_writel(tibase, DAVINCI_USB_EOI_REG, 0);
  302. /* poll for ID change */
  303. if (is_otg_enabled(musb)
  304. && musb->xceiv.state == OTG_STATE_B_IDLE)
  305. mod_timer(&otg_workaround, jiffies + POLL_SECONDS * HZ);
  306. spin_unlock_irqrestore(&musb->lock, flags);
  307. /* REVISIT we sometimes get unhandled IRQs
  308. * (e.g. ep0). not clear why...
  309. */
  310. if (retval != IRQ_HANDLED)
  311. DBG(5, "unhandled? %08x\n", tmp);
  312. return IRQ_HANDLED;
  313. }
  314. int musb_platform_set_mode(struct musb *musb, u8 mode)
  315. {
  316. /* EVM can't do this (right?) */
  317. return -EIO;
  318. }
  319. int __init musb_platform_init(struct musb *musb)
  320. {
  321. void __iomem *tibase = musb->ctrl_base;
  322. u32 revision;
  323. musb->mregs += DAVINCI_BASE_OFFSET;
  324. clk_enable(musb->clock);
  325. /* returns zero if e.g. not clocked */
  326. revision = musb_readl(tibase, DAVINCI_USB_VERSION_REG);
  327. if (revision == 0)
  328. return -ENODEV;
  329. if (is_host_enabled(musb))
  330. setup_timer(&otg_workaround, otg_timer, (unsigned long) musb);
  331. musb->board_set_vbus = davinci_set_vbus;
  332. davinci_source_power(musb, 0, 1);
  333. /* reset the controller */
  334. musb_writel(tibase, DAVINCI_USB_CTRL_REG, 0x1);
  335. /* start the on-chip PHY and its PLL */
  336. phy_on();
  337. msleep(5);
  338. /* NOTE: irqs are in mixed mode, not bypass to pure-musb */
  339. pr_debug("DaVinci OTG revision %08x phy %03x control %02x\n",
  340. revision, __raw_readl((void __force __iomem *)
  341. IO_ADDRESS(USBPHY_CTL_PADDR)),
  342. musb_readb(tibase, DAVINCI_USB_CTRL_REG));
  343. musb->isr = davinci_interrupt;
  344. return 0;
  345. }
  346. int musb_platform_exit(struct musb *musb)
  347. {
  348. if (is_host_enabled(musb))
  349. del_timer_sync(&otg_workaround);
  350. davinci_source_power(musb, 0 /*off*/, 1);
  351. /* delay, to avoid problems with module reload */
  352. if (is_host_enabled(musb) && musb->xceiv.default_a) {
  353. int maxdelay = 30;
  354. u8 devctl, warn = 0;
  355. /* if there's no peripheral connected, this can take a
  356. * long time to fall, especially on EVM with huge C133.
  357. */
  358. do {
  359. devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
  360. if (!(devctl & MUSB_DEVCTL_VBUS))
  361. break;
  362. if ((devctl & MUSB_DEVCTL_VBUS) != warn) {
  363. warn = devctl & MUSB_DEVCTL_VBUS;
  364. DBG(1, "VBUS %d\n",
  365. warn >> MUSB_DEVCTL_VBUS_SHIFT);
  366. }
  367. msleep(1000);
  368. maxdelay--;
  369. } while (maxdelay > 0);
  370. /* in OTG mode, another host might be connected */
  371. if (devctl & MUSB_DEVCTL_VBUS)
  372. DBG(1, "VBUS off timeout (devctl %02x)\n", devctl);
  373. }
  374. phy_off();
  375. clk_disable(musb->clock);
  376. return 0;
  377. }