]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/usb/musb/davinci.c
Merge current mainline tree into linux-omap tree
[linux-2.6-omap-h63xx.git] / drivers / usb / musb / davinci.c
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
24 #include <linux/module.h>
25 #include <linux/kernel.h>
26 #include <linux/sched.h>
27 #include <linux/slab.h>
28 #include <linux/init.h>
29 #include <linux/list.h>
30 #include <linux/delay.h>
31 #include <linux/clk.h>
32
33 #include <asm/io.h>
34 #include <asm/arch/hardware.h>
35 #include <asm/arch/memory.h>
36 #include <asm/arch/gpio.h>
37 #include <asm/mach-types.h>
38
39 #include "musb_core.h"
40
41
42 #ifdef CONFIG_MACH_DAVINCI_EVM
43 #include <asm/arch/i2c-client.h>
44 #endif
45
46 #include "davinci.h"
47 #include "cppi_dma.h"
48
49
50 /* REVISIT (PM) we should be able to keep the PHY in low power mode most
51  * of the time (24 MHZ oscillator and PLL off, etc) by setting POWER.D0
52  * and, when in host mode, autosuspending idle root ports... PHYPLLON
53  * (overriding SUSPENDM?) then likely needs to stay off.
54  */
55
56 static inline void phy_on(void)
57 {
58         /* start the on-chip PHY and its PLL */
59         __raw_writel(USBPHY_SESNDEN | USBPHY_VBDTCTEN | USBPHY_PHYPLLON,
60                         (void __force __iomem *) IO_ADDRESS(USBPHY_CTL_PADDR));
61         while ((__raw_readl((void __force __iomem *)
62                                 IO_ADDRESS(USBPHY_CTL_PADDR))
63                         & USBPHY_PHYCLKGD) == 0)
64                 cpu_relax();
65 }
66
67 static inline void phy_off(void)
68 {
69         /* powerdown the on-chip PHY and its oscillator */
70         __raw_writel(USBPHY_OSCPDWN | USBPHY_PHYPDWN, (void __force __iomem *)
71                         IO_ADDRESS(USBPHY_CTL_PADDR));
72 }
73
74 static int dma_off = 1;
75
76 void musb_platform_enable(struct musb *musb)
77 {
78         u32     tmp, old, val;
79
80         /* workaround:  setup irqs through both register sets */
81         tmp = (musb->epmask & DAVINCI_USB_TX_ENDPTS_MASK)
82                         << DAVINCI_USB_TXINT_SHIFT;
83         musb_writel(musb->ctrl_base, DAVINCI_USB_INT_MASK_SET_REG, tmp);
84         old = tmp;
85         tmp = (musb->epmask & (0xfffe & DAVINCI_USB_RX_ENDPTS_MASK))
86                         << DAVINCI_USB_RXINT_SHIFT;
87         musb_writel(musb->ctrl_base, DAVINCI_USB_INT_MASK_SET_REG, tmp);
88         tmp |= old;
89
90         val = ~MUSB_INTR_SOF;
91         tmp |= ((val & 0x01ff) << DAVINCI_USB_USBINT_SHIFT);
92         musb_writel(musb->ctrl_base, DAVINCI_USB_INT_MASK_SET_REG, tmp);
93
94         if (is_dma_capable() && !dma_off)
95                 printk(KERN_WARNING "%s %s: dma not reactivated\n",
96                                 __FILE__, __FUNCTION__);
97         else
98                 dma_off = 0;
99
100         /* force a DRVVBUS irq so we can start polling for ID change */
101         if (is_otg_enabled(musb))
102                 musb_writel(musb->ctrl_base, DAVINCI_USB_INT_SET_REG,
103                         DAVINCI_INTR_DRVVBUS << DAVINCI_USB_USBINT_SHIFT);
104 }
105
106 /*
107  * Disable the HDRC and flush interrupts
108  */
109 void musb_platform_disable(struct musb *musb)
110 {
111         /* because we don't set CTRLR.UINT, "important" to:
112          *  - not read/write INTRUSB/INTRUSBE
113          *  - (except during initial setup, as workaround)
114          *  - use INTSETR/INTCLRR instead
115          */
116         musb_writel(musb->ctrl_base, DAVINCI_USB_INT_MASK_CLR_REG,
117                           DAVINCI_USB_USBINT_MASK
118                         | DAVINCI_USB_TXINT_MASK
119                         | DAVINCI_USB_RXINT_MASK);
120         musb_writeb(musb->mregs, MUSB_DEVCTL, 0);
121         musb_writel(musb->ctrl_base, DAVINCI_USB_EOI_REG, 0);
122
123         if (is_dma_capable() && !dma_off)
124                 WARN("dma still active\n");
125 }
126
127
128 /* REVISIT it's not clear whether DaVinci can support full OTG.  */
129
130 static int vbus_state = -1;
131
132 #ifdef CONFIG_USB_MUSB_HDRC_HCD
133 #define portstate(stmt)         stmt
134 #else
135 #define portstate(stmt)
136 #endif
137
138
139 /* VBUS SWITCHING IS BOARD-SPECIFIC */
140
141 #ifdef CONFIG_MACH_DAVINCI_EVM
142 #ifndef CONFIG_MACH_DAVINCI_EVM_OTG
143
144 /* I2C operations are always synchronous, and require a task context.
145  * With unloaded systems, using the shared workqueue seems to suffice
146  * to satisfy the 100msec A_WAIT_VRISE timeout...
147  */
148 static void evm_deferred_drvvbus(struct work_struct *ignored)
149 {
150         davinci_i2c_expander_op(0x3a, USB_DRVVBUS, vbus_state);
151         vbus_state = !vbus_state;
152 }
153 static DECLARE_WORK(evm_vbus_work, evm_deferred_drvvbus);
154
155 #endif  /* modified board */
156 #endif  /* EVM */
157
158 static void davinci_source_power(struct musb *musb, int is_on, int immediate)
159 {
160         if (is_on)
161                 is_on = 1;
162
163         if (vbus_state == is_on)
164                 return;
165         vbus_state = !is_on;            /* 0/1 vs "-1 == unknown/init" */
166
167 #ifdef CONFIG_MACH_DAVINCI_EVM
168         if (machine_is_davinci_evm()) {
169 #ifdef CONFIG_MACH_DAVINCI_EVM_OTG
170                 /* modified EVM board switching VBUS with GPIO(6) not I2C
171                  * NOTE:  PINMUX0.RGB888 (bit23) must be clear
172                  */
173                 if (is_on)
174                         gpio_set(GPIO(6));
175                 else
176                         gpio_clear(GPIO(6));
177                 immediate = 1;
178 #else
179                 if (immediate)
180                         davinci_i2c_expander_op(0x3a, USB_DRVVBUS, !is_on);
181                 else
182                         schedule_work(&evm_vbus_work);
183 #endif
184         }
185 #endif
186         if (immediate)
187                 vbus_state = is_on;
188 }
189
190 static void davinci_set_vbus(struct musb *musb, int is_on)
191 {
192         WARN_ON(is_on && is_peripheral_active(musb));
193         davinci_source_power(musb, is_on, 0);
194 }
195
196
197 #define POLL_SECONDS    2
198
199 static struct timer_list otg_workaround;
200
201 static void otg_timer(unsigned long _musb)
202 {
203         struct musb             *musb = (void *)_musb;
204         void __iomem            *mregs = musb->mregs;
205         u8                      devctl;
206         unsigned long           flags;
207
208         /* We poll because DaVinci's won't expose several OTG-critical
209         * status change events (from the transceiver) otherwise.
210          */
211         devctl = musb_readb(mregs, MUSB_DEVCTL);
212         DBG(7, "poll devctl %02x (%s)\n", devctl, otg_state_string(musb));
213
214         spin_lock_irqsave(&musb->lock, flags);
215         switch (musb->xceiv.state) {
216         case OTG_STATE_A_WAIT_VFALL:
217                 /* Wait till VBUS falls below SessionEnd (~0.2V); the 1.3 RTL
218                  * seems to mis-handle session "start" otherwise (or in our
219                  * case "recover"), in routine "VBUS was valid by the time
220                  * VBUSERR got reported during enumeration" cases.
221                  */
222                 if (devctl & MUSB_DEVCTL_VBUS) {
223                         mod_timer(&otg_workaround, jiffies + POLL_SECONDS * HZ);
224                         break;
225                 }
226                 musb->xceiv.state = OTG_STATE_A_WAIT_VRISE;
227                 musb_writel(musb->ctrl_base, DAVINCI_USB_INT_SET_REG,
228                         MUSB_INTR_VBUSERROR << DAVINCI_USB_USBINT_SHIFT);
229                 break;
230         case OTG_STATE_B_IDLE:
231                 if (!is_peripheral_enabled(musb))
232                         break;
233
234                 /* There's no ID-changed IRQ, so we have no good way to tell
235                  * when to switch to the A-Default state machine (by setting
236                  * the DEVCTL.SESSION flag).
237                  *
238                  * Workaround:  whenever we're in B_IDLE, try setting the
239                  * session flag every few seconds.  If it works, ID was
240                  * grounded and we're now in the A-Default state machine.
241                  *
242                  * NOTE setting the session flag is _supposed_ to trigger
243                  * SRP, but clearly it doesn't.
244                  */
245                 musb_writeb(mregs, MUSB_DEVCTL,
246                                 devctl | MUSB_DEVCTL_SESSION);
247                 devctl = musb_readb(mregs, MUSB_DEVCTL);
248                 if (devctl & MUSB_DEVCTL_BDEVICE)
249                         mod_timer(&otg_workaround, jiffies + POLL_SECONDS * HZ);
250                 else
251                         musb->xceiv.state = OTG_STATE_A_IDLE;
252                 break;
253         default:
254                 break;
255         }
256         spin_unlock_irqrestore(&musb->lock, flags);
257 }
258
259 static irqreturn_t davinci_interrupt(int irq, void *__hci)
260 {
261         unsigned long   flags;
262         irqreturn_t     retval = IRQ_NONE;
263         struct musb     *musb = __hci;
264         void __iomem    *tibase = musb->ctrl_base;
265         u32             tmp;
266
267         spin_lock_irqsave(&musb->lock, flags);
268
269         /* NOTE: DaVinci shadows the Mentor IRQs.  Don't manage them through
270          * the Mentor registers (except for setup), use the TI ones and EOI.
271          *
272          * Docs describe irq "vector" registers asociated with the CPPI and
273          * USB EOI registers.  These hold a bitmask corresponding to the
274          * current IRQ, not an irq handler address.  Would using those bits
275          * resolve some of the races observed in this dispatch code??
276          */
277
278         /* CPPI interrupts share the same IRQ line, but have their own
279          * mask, state, "vector", and EOI registers.
280          */
281         if (is_cppi_enabled()) {
282                 u32 cppi_tx = musb_readl(tibase, DAVINCI_TXCPPI_MASKED_REG);
283                 u32 cppi_rx = musb_readl(tibase, DAVINCI_RXCPPI_MASKED_REG);
284
285                 if (cppi_tx || cppi_rx) {
286                         DBG(4, "CPPI IRQ t%x r%x\n", cppi_tx, cppi_rx);
287                         cppi_completion(musb, cppi_rx, cppi_tx);
288                         retval = IRQ_HANDLED;
289                 }
290         }
291
292         /* ack and handle non-CPPI interrupts */
293         tmp = musb_readl(tibase, DAVINCI_USB_INT_SRC_MASKED_REG);
294         musb_writel(tibase, DAVINCI_USB_INT_SRC_CLR_REG, tmp);
295         DBG(4, "IRQ %08x\n", tmp);
296
297         musb->int_rx = (tmp & DAVINCI_USB_RXINT_MASK)
298                         >> DAVINCI_USB_RXINT_SHIFT;
299         musb->int_tx = (tmp & DAVINCI_USB_TXINT_MASK)
300                         >> DAVINCI_USB_TXINT_SHIFT;
301         musb->int_usb = (tmp & DAVINCI_USB_USBINT_MASK)
302                         >> DAVINCI_USB_USBINT_SHIFT;
303
304         /* DRVVBUS irqs are the only proxy we have (a very poor one!) for
305          * DaVinci's missing ID change IRQ.  We need an ID change IRQ to
306          * switch appropriately between halves of the OTG state machine.
307          * Managing DEVCTL.SESSION per Mentor docs requires we know its
308          * value, but DEVCTL.BDEVICE is invalid without DEVCTL.SESSION set.
309          * Also, DRVVBUS pulses for SRP (but not at 5V) ...
310          */
311         if (tmp & (DAVINCI_INTR_DRVVBUS << DAVINCI_USB_USBINT_SHIFT)) {
312                 int     drvvbus = musb_readl(tibase, DAVINCI_USB_STAT_REG);
313                 void __iomem *mregs = musb->mregs;
314                 u8      devctl = musb_readb(mregs, MUSB_DEVCTL);
315                 int     err = musb->int_usb & MUSB_INTR_VBUSERROR;
316
317                 err = is_host_enabled(musb)
318                                 && (musb->int_usb & MUSB_INTR_VBUSERROR);
319                 if (err) {
320                         /* The Mentor core doesn't debounce VBUS as needed
321                          * to cope with device connect current spikes. This
322                          * means it's not uncommon for bus-powered devices
323                          * to get VBUS errors during enumeration.
324                          *
325                          * This is a workaround, but newer RTL from Mentor
326                          * seems to allow a better one: "re"starting sessions
327                          * without waiting (on EVM, a **long** time) for VBUS
328                          * to stop registering in devctl.
329                          */
330                         musb->int_usb &= ~MUSB_INTR_VBUSERROR;
331                         musb->xceiv.state = OTG_STATE_A_WAIT_VFALL;
332                         mod_timer(&otg_workaround, jiffies + POLL_SECONDS * HZ);
333                         WARN("VBUS error workaround (delay coming)\n");
334                 } else if (is_host_enabled(musb) && drvvbus) {
335                         musb->is_active = 1;
336                         MUSB_HST_MODE(musb);
337                         musb->xceiv.default_a = 1;
338                         musb->xceiv.state = OTG_STATE_A_WAIT_VRISE;
339                         portstate(musb->port1_status |= USB_PORT_STAT_POWER);
340                         del_timer(&otg_workaround);
341                 } else {
342                         musb->is_active = 0;
343                         MUSB_DEV_MODE(musb);
344                         musb->xceiv.default_a = 0;
345                         musb->xceiv.state = OTG_STATE_B_IDLE;
346                         portstate(musb->port1_status &= ~USB_PORT_STAT_POWER);
347                 }
348
349                 /* NOTE:  this must complete poweron within 100 msec */
350                 davinci_source_power(musb, drvvbus, 0);
351                 DBG(2, "VBUS %s (%s)%s, devctl %02x\n",
352                                 drvvbus ? "on" : "off",
353                                 otg_state_string(musb),
354                                 err ? " ERROR" : "",
355                                 devctl);
356                 retval = IRQ_HANDLED;
357         }
358
359         if (musb->int_tx || musb->int_rx || musb->int_usb)
360                 retval |= musb_interrupt(musb);
361
362         /* irq stays asserted until EOI is written */
363         musb_writel(tibase, DAVINCI_USB_EOI_REG, 0);
364
365         /* poll for ID change */
366         if (is_otg_enabled(musb)
367                         && musb->xceiv.state == OTG_STATE_B_IDLE)
368                 mod_timer(&otg_workaround, jiffies + POLL_SECONDS * HZ);
369
370         spin_unlock_irqrestore(&musb->lock, flags);
371
372         /* REVISIT we sometimes get unhandled IRQs
373          * (e.g. ep0).  not clear why...
374          */
375         if (retval != IRQ_HANDLED)
376                 DBG(5, "unhandled? %08x\n", tmp);
377         return IRQ_HANDLED;
378 }
379
380 int __init musb_platform_init(struct musb *musb)
381 {
382         void __iomem    *tibase = musb->ctrl_base;
383         u32             revision;
384
385         musb->mregs += DAVINCI_BASE_OFFSET;
386 #if 0
387         /* REVISIT there's something odd about clocking, this
388          * didn't appear do the job ...
389          */
390         musb->clock = clk_get(pDevice, "usb");
391         if (IS_ERR(musb->clock))
392                 return PTR_ERR(musb->clock);
393
394         status = clk_enable(musb->clock);
395         if (status < 0)
396                 return -ENODEV;
397 #endif
398
399         /* returns zero if e.g. not clocked */
400         revision = musb_readl(tibase, DAVINCI_USB_VERSION_REG);
401         if (revision == 0)
402                 return -ENODEV;
403
404         if (is_host_enabled(musb))
405                 setup_timer(&otg_workaround, otg_timer, (unsigned long) musb);
406
407         musb->board_set_vbus = davinci_set_vbus;
408         davinci_source_power(musb, 0, 1);
409
410         /* reset the controller */
411         musb_writel(tibase, DAVINCI_USB_CTRL_REG, 0x1);
412
413         /* start the on-chip PHY and its PLL */
414         phy_on();
415
416         msleep(5);
417
418         /* NOTE:  irqs are in mixed mode, not bypass to pure-musb */
419         pr_debug("DaVinci OTG revision %08x phy %03x control %02x\n",
420                 revision, __raw_readl((void __force __iomem *)
421                                 IO_ADDRESS(USBPHY_CTL_PADDR)),
422                 musb_readb(tibase, DAVINCI_USB_CTRL_REG));
423
424         musb->isr = davinci_interrupt;
425         return 0;
426 }
427
428 int musb_platform_exit(struct musb *musb)
429 {
430         if (is_host_enabled(musb))
431                 del_timer_sync(&otg_workaround);
432
433         davinci_source_power(musb, 0 /*off*/, 1);
434
435         /* delay, to avoid problems with module reload */
436         if (is_host_enabled(musb) && musb->xceiv.default_a) {
437                 int     maxdelay = 30;
438                 u8      devctl, warn = 0;
439
440                 /* if there's no peripheral connected, this can take a
441                  * long time to fall, especially on EVM with huge C133.
442                  */
443                 do {
444                         devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
445                         if (!(devctl & MUSB_DEVCTL_VBUS))
446                                 break;
447                         if ((devctl & MUSB_DEVCTL_VBUS) != warn) {
448                                 warn = devctl & MUSB_DEVCTL_VBUS;
449                                 DBG(1, "VBUS %d\n", warn >> MUSB_DEVCTL_VBUS_SHIFT);
450                         }
451                         msleep(1000);
452                         maxdelay--;
453                 } while (maxdelay > 0);
454
455                 /* in OTG mode, another host might be connected */
456                 if (devctl & MUSB_DEVCTL_VBUS)
457                         DBG(1, "VBUS off timeout (devctl %02x)\n", devctl);
458         }
459
460         phy_off();
461         return 0;
462 }