Merge branch 'staging-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh...
[pandora-kernel.git] / drivers / usb / musb / blackfin.c
1 /*
2  * MUSB OTG controller driver for Blackfin Processors
3  *
4  * Copyright 2006-2008 Analog Devices Inc.
5  *
6  * Enter bugs at http://blackfin.uclinux.org/
7  *
8  * Licensed under the GPL-2 or later.
9  */
10
11 #include <linux/module.h>
12 #include <linux/kernel.h>
13 #include <linux/sched.h>
14 #include <linux/init.h>
15 #include <linux/list.h>
16 #include <linux/gpio.h>
17 #include <linux/io.h>
18
19 #include <asm/cacheflush.h>
20
21 #include "musb_core.h"
22 #include "blackfin.h"
23
24 /*
25  * Load an endpoint's FIFO
26  */
27 void musb_write_fifo(struct musb_hw_ep *hw_ep, u16 len, const u8 *src)
28 {
29         void __iomem *fifo = hw_ep->fifo;
30         void __iomem *epio = hw_ep->regs;
31         u8 epnum = hw_ep->epnum;
32
33         prefetch((u8 *)src);
34
35         musb_writew(epio, MUSB_TXCOUNT, len);
36
37         DBG(4, "TX ep%d fifo %p count %d buf %p, epio %p\n",
38                         hw_ep->epnum, fifo, len, src, epio);
39
40         dump_fifo_data(src, len);
41
42         if (!ANOMALY_05000380 && epnum != 0) {
43                 u16 dma_reg;
44
45                 flush_dcache_range((unsigned long)src,
46                         (unsigned long)(src + len));
47
48                 /* Setup DMA address register */
49                 dma_reg = (u32)src;
50                 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_ADDR_LOW), dma_reg);
51                 SSYNC();
52
53                 dma_reg = (u32)src >> 16;
54                 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_ADDR_HIGH), dma_reg);
55                 SSYNC();
56
57                 /* Setup DMA count register */
58                 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_COUNT_LOW), len);
59                 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_COUNT_HIGH), 0);
60                 SSYNC();
61
62                 /* Enable the DMA */
63                 dma_reg = (epnum << 4) | DMA_ENA | INT_ENA | DIRECTION;
64                 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_CTRL), dma_reg);
65                 SSYNC();
66
67                 /* Wait for compelete */
68                 while (!(bfin_read_USB_DMA_INTERRUPT() & (1 << epnum)))
69                         cpu_relax();
70
71                 /* acknowledge dma interrupt */
72                 bfin_write_USB_DMA_INTERRUPT(1 << epnum);
73                 SSYNC();
74
75                 /* Reset DMA */
76                 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_CTRL), 0);
77                 SSYNC();
78         } else {
79                 SSYNC();
80
81                 if (unlikely((unsigned long)src & 0x01))
82                         outsw_8((unsigned long)fifo, src, (len + 1) >> 1);
83                 else
84                         outsw((unsigned long)fifo, src, (len + 1) >> 1);
85         }
86 }
87 /*
88  * Unload an endpoint's FIFO
89  */
90 void musb_read_fifo(struct musb_hw_ep *hw_ep, u16 len, u8 *dst)
91 {
92         void __iomem *fifo = hw_ep->fifo;
93         u8 epnum = hw_ep->epnum;
94
95         if (ANOMALY_05000467 && epnum != 0) {
96                 u16 dma_reg;
97
98                 invalidate_dcache_range((unsigned long)dst,
99                         (unsigned long)(dst + len));
100
101                 /* Setup DMA address register */
102                 dma_reg = (u32)dst;
103                 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_ADDR_LOW), dma_reg);
104                 SSYNC();
105
106                 dma_reg = (u32)dst >> 16;
107                 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_ADDR_HIGH), dma_reg);
108                 SSYNC();
109
110                 /* Setup DMA count register */
111                 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_COUNT_LOW), len);
112                 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_COUNT_HIGH), 0);
113                 SSYNC();
114
115                 /* Enable the DMA */
116                 dma_reg = (epnum << 4) | DMA_ENA | INT_ENA;
117                 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_CTRL), dma_reg);
118                 SSYNC();
119
120                 /* Wait for compelete */
121                 while (!(bfin_read_USB_DMA_INTERRUPT() & (1 << epnum)))
122                         cpu_relax();
123
124                 /* acknowledge dma interrupt */
125                 bfin_write_USB_DMA_INTERRUPT(1 << epnum);
126                 SSYNC();
127
128                 /* Reset DMA */
129                 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_CTRL), 0);
130                 SSYNC();
131         } else {
132                 SSYNC();
133                 /* Read the last byte of packet with odd size from address fifo + 4
134                  * to trigger 1 byte access to EP0 FIFO.
135                  */
136                 if (len == 1)
137                         *dst = (u8)inw((unsigned long)fifo + 4);
138                 else {
139                         if (unlikely((unsigned long)dst & 0x01))
140                                 insw_8((unsigned long)fifo, dst, len >> 1);
141                         else
142                                 insw((unsigned long)fifo, dst, len >> 1);
143
144                         if (len & 0x01)
145                                 *(dst + len - 1) = (u8)inw((unsigned long)fifo + 4);
146                 }
147         }
148         DBG(4, "%cX ep%d fifo %p count %d buf %p\n",
149                         'R', hw_ep->epnum, fifo, len, dst);
150
151         dump_fifo_data(dst, len);
152 }
153
154 static irqreturn_t blackfin_interrupt(int irq, void *__hci)
155 {
156         unsigned long   flags;
157         irqreturn_t     retval = IRQ_NONE;
158         struct musb     *musb = __hci;
159
160         spin_lock_irqsave(&musb->lock, flags);
161
162         musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB);
163         musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX);
164         musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX);
165
166         if (musb->int_usb || musb->int_tx || musb->int_rx) {
167                 musb_writeb(musb->mregs, MUSB_INTRUSB, musb->int_usb);
168                 musb_writew(musb->mregs, MUSB_INTRTX, musb->int_tx);
169                 musb_writew(musb->mregs, MUSB_INTRRX, musb->int_rx);
170                 retval = musb_interrupt(musb);
171         }
172
173         /* Start sampling ID pin, when plug is removed from MUSB */
174         if ((is_otg_enabled(musb) && (musb->xceiv->state == OTG_STATE_B_IDLE
175                 || musb->xceiv->state == OTG_STATE_A_WAIT_BCON)) ||
176                 (musb->int_usb & MUSB_INTR_DISCONNECT && is_host_active(musb))) {
177                 mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY);
178                 musb->a_wait_bcon = TIMER_DELAY;
179         }
180
181         spin_unlock_irqrestore(&musb->lock, flags);
182
183         return retval;
184 }
185
186 static void musb_conn_timer_handler(unsigned long _musb)
187 {
188         struct musb *musb = (void *)_musb;
189         unsigned long flags;
190         u16 val;
191         static u8 toggle;
192
193         spin_lock_irqsave(&musb->lock, flags);
194         switch (musb->xceiv->state) {
195         case OTG_STATE_A_IDLE:
196         case OTG_STATE_A_WAIT_BCON:
197                 /* Start a new session */
198                 val = musb_readw(musb->mregs, MUSB_DEVCTL);
199                 val &= ~MUSB_DEVCTL_SESSION;
200                 musb_writew(musb->mregs, MUSB_DEVCTL, val);
201                 val |= MUSB_DEVCTL_SESSION;
202                 musb_writew(musb->mregs, MUSB_DEVCTL, val);
203                 /* Check if musb is host or peripheral. */
204                 val = musb_readw(musb->mregs, MUSB_DEVCTL);
205
206                 if (!(val & MUSB_DEVCTL_BDEVICE)) {
207                         gpio_set_value(musb->config->gpio_vrsel, 1);
208                         musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
209                 } else {
210                         gpio_set_value(musb->config->gpio_vrsel, 0);
211                         /* Ignore VBUSERROR and SUSPEND IRQ */
212                         val = musb_readb(musb->mregs, MUSB_INTRUSBE);
213                         val &= ~MUSB_INTR_VBUSERROR;
214                         musb_writeb(musb->mregs, MUSB_INTRUSBE, val);
215
216                         val = MUSB_INTR_SUSPEND | MUSB_INTR_VBUSERROR;
217                         musb_writeb(musb->mregs, MUSB_INTRUSB, val);
218                         if (is_otg_enabled(musb))
219                                 musb->xceiv->state = OTG_STATE_B_IDLE;
220                         else
221                                 musb_writeb(musb->mregs, MUSB_POWER, MUSB_POWER_HSENAB);
222                 }
223                 mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY);
224                 break;
225         case OTG_STATE_B_IDLE:
226
227                 if (!is_peripheral_enabled(musb))
228                         break;
229                 /* Start a new session.  It seems that MUSB needs taking
230                  * some time to recognize the type of the plug inserted?
231                  */
232                 val = musb_readw(musb->mregs, MUSB_DEVCTL);
233                 val |= MUSB_DEVCTL_SESSION;
234                 musb_writew(musb->mregs, MUSB_DEVCTL, val);
235                 val = musb_readw(musb->mregs, MUSB_DEVCTL);
236
237                 if (!(val & MUSB_DEVCTL_BDEVICE)) {
238                         gpio_set_value(musb->config->gpio_vrsel, 1);
239                         musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
240                 } else {
241                         gpio_set_value(musb->config->gpio_vrsel, 0);
242
243                         /* Ignore VBUSERROR and SUSPEND IRQ */
244                         val = musb_readb(musb->mregs, MUSB_INTRUSBE);
245                         val &= ~MUSB_INTR_VBUSERROR;
246                         musb_writeb(musb->mregs, MUSB_INTRUSBE, val);
247
248                         val = MUSB_INTR_SUSPEND | MUSB_INTR_VBUSERROR;
249                         musb_writeb(musb->mregs, MUSB_INTRUSB, val);
250
251                         /* Toggle the Soft Conn bit, so that we can response to
252                          * the inserting of either A-plug or B-plug.
253                          */
254                         if (toggle) {
255                                 val = musb_readb(musb->mregs, MUSB_POWER);
256                                 val &= ~MUSB_POWER_SOFTCONN;
257                                 musb_writeb(musb->mregs, MUSB_POWER, val);
258                                 toggle = 0;
259                         } else {
260                                 val = musb_readb(musb->mregs, MUSB_POWER);
261                                 val |= MUSB_POWER_SOFTCONN;
262                                 musb_writeb(musb->mregs, MUSB_POWER, val);
263                                 toggle = 1;
264                         }
265                         /* The delay time is set to 1/4 second by default,
266                          * shortening it, if accelerating A-plug detection
267                          * is needed in OTG mode.
268                          */
269                         mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY / 4);
270                 }
271                 break;
272         default:
273                 DBG(1, "%s state not handled\n", otg_state_string(musb));
274                 break;
275         }
276         spin_unlock_irqrestore(&musb->lock, flags);
277
278         DBG(4, "state is %s\n", otg_state_string(musb));
279 }
280
281 void musb_platform_enable(struct musb *musb)
282 {
283         if (!is_otg_enabled(musb) && is_host_enabled(musb)) {
284                 mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY);
285                 musb->a_wait_bcon = TIMER_DELAY;
286         }
287 }
288
289 void musb_platform_disable(struct musb *musb)
290 {
291 }
292
293 static void bfin_set_vbus(struct musb *musb, int is_on)
294 {
295         int value = musb->config->gpio_vrsel_active;
296         if (!is_on)
297                 value = !value;
298         gpio_set_value(musb->config->gpio_vrsel, value);
299
300         DBG(1, "VBUS %s, devctl %02x "
301                 /* otg %3x conf %08x prcm %08x */ "\n",
302                 otg_state_string(musb),
303                 musb_readb(musb->mregs, MUSB_DEVCTL));
304 }
305
306 static int bfin_set_power(struct otg_transceiver *x, unsigned mA)
307 {
308         return 0;
309 }
310
311 void musb_platform_try_idle(struct musb *musb, unsigned long timeout)
312 {
313         if (!is_otg_enabled(musb) && is_host_enabled(musb))
314                 mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY);
315 }
316
317 int musb_platform_get_vbus_status(struct musb *musb)
318 {
319         return 0;
320 }
321
322 int musb_platform_set_mode(struct musb *musb, u8 musb_mode)
323 {
324         return -EIO;
325 }
326
327 static void musb_platform_reg_init(struct musb *musb)
328 {
329         if (ANOMALY_05000346) {
330                 bfin_write_USB_APHY_CALIB(ANOMALY_05000346_value);
331                 SSYNC();
332         }
333
334         if (ANOMALY_05000347) {
335                 bfin_write_USB_APHY_CNTRL(0x0);
336                 SSYNC();
337         }
338
339         /* Configure PLL oscillator register */
340         bfin_write_USB_PLLOSC_CTRL(0x3080 |
341                         ((480/musb->config->clkin) << 1));
342         SSYNC();
343
344         bfin_write_USB_SRP_CLKDIV((get_sclk()/1000) / 32 - 1);
345         SSYNC();
346
347         bfin_write_USB_EP_NI0_RXMAXP(64);
348         SSYNC();
349
350         bfin_write_USB_EP_NI0_TXMAXP(64);
351         SSYNC();
352
353         /* Route INTRUSB/INTR_RX/INTR_TX to USB_INT0*/
354         bfin_write_USB_GLOBINTR(0x7);
355         SSYNC();
356
357         bfin_write_USB_GLOBAL_CTL(GLOBAL_ENA | EP1_TX_ENA | EP2_TX_ENA |
358                                 EP3_TX_ENA | EP4_TX_ENA | EP5_TX_ENA |
359                                 EP6_TX_ENA | EP7_TX_ENA | EP1_RX_ENA |
360                                 EP2_RX_ENA | EP3_RX_ENA | EP4_RX_ENA |
361                                 EP5_RX_ENA | EP6_RX_ENA | EP7_RX_ENA);
362         SSYNC();
363 }
364
365 int __init musb_platform_init(struct musb *musb, void *board_data)
366 {
367
368         /*
369          * Rev 1.0 BF549 EZ-KITs require PE7 to be high for both DEVICE
370          * and OTG HOST modes, while rev 1.1 and greater require PE7 to
371          * be low for DEVICE mode and high for HOST mode. We set it high
372          * here because we are in host mode
373          */
374
375         if (gpio_request(musb->config->gpio_vrsel, "USB_VRSEL")) {
376                 printk(KERN_ERR "Failed ro request USB_VRSEL GPIO_%d\n",
377                         musb->config->gpio_vrsel);
378                 return -ENODEV;
379         }
380         gpio_direction_output(musb->config->gpio_vrsel, 0);
381
382         usb_nop_xceiv_register();
383         musb->xceiv = otg_get_transceiver();
384         if (!musb->xceiv) {
385                 gpio_free(musb->config->gpio_vrsel);
386                 return -ENODEV;
387         }
388
389         musb_platform_reg_init(musb);
390
391         if (is_host_enabled(musb)) {
392                 musb->board_set_vbus = bfin_set_vbus;
393                 setup_timer(&musb_conn_timer,
394                         musb_conn_timer_handler, (unsigned long) musb);
395         }
396         if (is_peripheral_enabled(musb))
397                 musb->xceiv->set_power = bfin_set_power;
398
399         musb->isr = blackfin_interrupt;
400
401         return 0;
402 }
403
404 #ifdef CONFIG_PM
405 void musb_platform_save_context(struct musb *musb,
406                         struct musb_context_registers *musb_context)
407 {
408         if (is_host_active(musb))
409                 /*
410                  * During hibernate gpio_vrsel will change from high to low
411                  * low which will generate wakeup event resume the system
412                  * immediately.  Set it to 0 before hibernate to avoid this
413                  * wakeup event.
414                  */
415                 gpio_set_value(musb->config->gpio_vrsel, 0);
416 }
417
418 void musb_platform_restore_context(struct musb *musb,
419                         struct musb_context_registers *musb_context)
420 {
421         musb_platform_reg_init(musb);
422 }
423 #endif
424
425 int musb_platform_exit(struct musb *musb)
426 {
427         gpio_free(musb->config->gpio_vrsel);
428
429         otg_put_transceiver(musb->xceiv);
430         usb_nop_xceiv_unregister();
431         return 0;
432 }