usb: musb: drop unneeded musb_debug trickery
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / usb / musb / blackfin.c
blob95d75c00404b30d917ee469d45251f3881215dbd
1 /*
2 * MUSB OTG controller driver for Blackfin Processors
4 * Copyright 2006-2008 Analog Devices Inc.
6 * Enter bugs at http://blackfin.uclinux.org/
8 * Licensed under the GPL-2 or later.
9 */
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 #include <linux/platform_device.h>
19 #include <linux/dma-mapping.h>
21 #include <asm/cacheflush.h>
23 #include "musb_core.h"
24 #include "musbhsdma.h"
25 #include "blackfin.h"
27 struct bfin_glue {
28 struct device *dev;
29 struct platform_device *musb;
31 #define glue_to_musb(g) platform_get_drvdata(g->musb)
34 * Load an endpoint's FIFO
36 void musb_write_fifo(struct musb_hw_ep *hw_ep, u16 len, const u8 *src)
38 void __iomem *fifo = hw_ep->fifo;
39 void __iomem *epio = hw_ep->regs;
40 u8 epnum = hw_ep->epnum;
42 prefetch((u8 *)src);
44 musb_writew(epio, MUSB_TXCOUNT, len);
46 dev_dbg(musb->controller, "TX ep%d fifo %p count %d buf %p, epio %p\n",
47 hw_ep->epnum, fifo, len, src, epio);
49 dump_fifo_data(src, len);
51 if (!ANOMALY_05000380 && epnum != 0) {
52 u16 dma_reg;
54 flush_dcache_range((unsigned long)src,
55 (unsigned long)(src + len));
57 /* Setup DMA address register */
58 dma_reg = (u32)src;
59 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_ADDR_LOW), dma_reg);
60 SSYNC();
62 dma_reg = (u32)src >> 16;
63 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_ADDR_HIGH), dma_reg);
64 SSYNC();
66 /* Setup DMA count register */
67 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_COUNT_LOW), len);
68 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_COUNT_HIGH), 0);
69 SSYNC();
71 /* Enable the DMA */
72 dma_reg = (epnum << 4) | DMA_ENA | INT_ENA | DIRECTION;
73 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_CTRL), dma_reg);
74 SSYNC();
76 /* Wait for compelete */
77 while (!(bfin_read_USB_DMA_INTERRUPT() & (1 << epnum)))
78 cpu_relax();
80 /* acknowledge dma interrupt */
81 bfin_write_USB_DMA_INTERRUPT(1 << epnum);
82 SSYNC();
84 /* Reset DMA */
85 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_CTRL), 0);
86 SSYNC();
87 } else {
88 SSYNC();
90 if (unlikely((unsigned long)src & 0x01))
91 outsw_8((unsigned long)fifo, src, (len + 1) >> 1);
92 else
93 outsw((unsigned long)fifo, src, (len + 1) >> 1);
97 * Unload an endpoint's FIFO
99 void musb_read_fifo(struct musb_hw_ep *hw_ep, u16 len, u8 *dst)
101 void __iomem *fifo = hw_ep->fifo;
102 u8 epnum = hw_ep->epnum;
104 if (ANOMALY_05000467 && epnum != 0) {
105 u16 dma_reg;
107 invalidate_dcache_range((unsigned long)dst,
108 (unsigned long)(dst + len));
110 /* Setup DMA address register */
111 dma_reg = (u32)dst;
112 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_ADDR_LOW), dma_reg);
113 SSYNC();
115 dma_reg = (u32)dst >> 16;
116 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_ADDR_HIGH), dma_reg);
117 SSYNC();
119 /* Setup DMA count register */
120 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_COUNT_LOW), len);
121 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_COUNT_HIGH), 0);
122 SSYNC();
124 /* Enable the DMA */
125 dma_reg = (epnum << 4) | DMA_ENA | INT_ENA;
126 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_CTRL), dma_reg);
127 SSYNC();
129 /* Wait for compelete */
130 while (!(bfin_read_USB_DMA_INTERRUPT() & (1 << epnum)))
131 cpu_relax();
133 /* acknowledge dma interrupt */
134 bfin_write_USB_DMA_INTERRUPT(1 << epnum);
135 SSYNC();
137 /* Reset DMA */
138 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_CTRL), 0);
139 SSYNC();
140 } else {
141 SSYNC();
142 /* Read the last byte of packet with odd size from address fifo + 4
143 * to trigger 1 byte access to EP0 FIFO.
145 if (len == 1)
146 *dst = (u8)inw((unsigned long)fifo + 4);
147 else {
148 if (unlikely((unsigned long)dst & 0x01))
149 insw_8((unsigned long)fifo, dst, len >> 1);
150 else
151 insw((unsigned long)fifo, dst, len >> 1);
153 if (len & 0x01)
154 *(dst + len - 1) = (u8)inw((unsigned long)fifo + 4);
157 dev_dbg(musb->controller, "%cX ep%d fifo %p count %d buf %p\n",
158 'R', hw_ep->epnum, fifo, len, dst);
160 dump_fifo_data(dst, len);
163 static irqreturn_t blackfin_interrupt(int irq, void *__hci)
165 unsigned long flags;
166 irqreturn_t retval = IRQ_NONE;
167 struct musb *musb = __hci;
169 spin_lock_irqsave(&musb->lock, flags);
171 musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB);
172 musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX);
173 musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX);
175 if (musb->int_usb || musb->int_tx || musb->int_rx) {
176 musb_writeb(musb->mregs, MUSB_INTRUSB, musb->int_usb);
177 musb_writew(musb->mregs, MUSB_INTRTX, musb->int_tx);
178 musb_writew(musb->mregs, MUSB_INTRRX, musb->int_rx);
179 retval = musb_interrupt(musb);
182 /* Start sampling ID pin, when plug is removed from MUSB */
183 if ((is_otg_enabled(musb) && (musb->xceiv->state == OTG_STATE_B_IDLE
184 || musb->xceiv->state == OTG_STATE_A_WAIT_BCON)) ||
185 (musb->int_usb & MUSB_INTR_DISCONNECT && is_host_active(musb))) {
186 mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY);
187 musb->a_wait_bcon = TIMER_DELAY;
190 spin_unlock_irqrestore(&musb->lock, flags);
192 return retval;
195 static void musb_conn_timer_handler(unsigned long _musb)
197 struct musb *musb = (void *)_musb;
198 unsigned long flags;
199 u16 val;
200 static u8 toggle;
202 spin_lock_irqsave(&musb->lock, flags);
203 switch (musb->xceiv->state) {
204 case OTG_STATE_A_IDLE:
205 case OTG_STATE_A_WAIT_BCON:
206 /* Start a new session */
207 val = musb_readw(musb->mregs, MUSB_DEVCTL);
208 val &= ~MUSB_DEVCTL_SESSION;
209 musb_writew(musb->mregs, MUSB_DEVCTL, val);
210 val |= MUSB_DEVCTL_SESSION;
211 musb_writew(musb->mregs, MUSB_DEVCTL, val);
212 /* Check if musb is host or peripheral. */
213 val = musb_readw(musb->mregs, MUSB_DEVCTL);
215 if (!(val & MUSB_DEVCTL_BDEVICE)) {
216 gpio_set_value(musb->config->gpio_vrsel, 1);
217 musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
218 } else {
219 gpio_set_value(musb->config->gpio_vrsel, 0);
220 /* Ignore VBUSERROR and SUSPEND IRQ */
221 val = musb_readb(musb->mregs, MUSB_INTRUSBE);
222 val &= ~MUSB_INTR_VBUSERROR;
223 musb_writeb(musb->mregs, MUSB_INTRUSBE, val);
225 val = MUSB_INTR_SUSPEND | MUSB_INTR_VBUSERROR;
226 musb_writeb(musb->mregs, MUSB_INTRUSB, val);
227 if (is_otg_enabled(musb))
228 musb->xceiv->state = OTG_STATE_B_IDLE;
229 else
230 musb_writeb(musb->mregs, MUSB_POWER, MUSB_POWER_HSENAB);
232 mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY);
233 break;
234 case OTG_STATE_B_IDLE:
236 if (!is_peripheral_enabled(musb))
237 break;
238 /* Start a new session. It seems that MUSB needs taking
239 * some time to recognize the type of the plug inserted?
241 val = musb_readw(musb->mregs, MUSB_DEVCTL);
242 val |= MUSB_DEVCTL_SESSION;
243 musb_writew(musb->mregs, MUSB_DEVCTL, val);
244 val = musb_readw(musb->mregs, MUSB_DEVCTL);
246 if (!(val & MUSB_DEVCTL_BDEVICE)) {
247 gpio_set_value(musb->config->gpio_vrsel, 1);
248 musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
249 } else {
250 gpio_set_value(musb->config->gpio_vrsel, 0);
252 /* Ignore VBUSERROR and SUSPEND IRQ */
253 val = musb_readb(musb->mregs, MUSB_INTRUSBE);
254 val &= ~MUSB_INTR_VBUSERROR;
255 musb_writeb(musb->mregs, MUSB_INTRUSBE, val);
257 val = MUSB_INTR_SUSPEND | MUSB_INTR_VBUSERROR;
258 musb_writeb(musb->mregs, MUSB_INTRUSB, val);
260 /* Toggle the Soft Conn bit, so that we can response to
261 * the inserting of either A-plug or B-plug.
263 if (toggle) {
264 val = musb_readb(musb->mregs, MUSB_POWER);
265 val &= ~MUSB_POWER_SOFTCONN;
266 musb_writeb(musb->mregs, MUSB_POWER, val);
267 toggle = 0;
268 } else {
269 val = musb_readb(musb->mregs, MUSB_POWER);
270 val |= MUSB_POWER_SOFTCONN;
271 musb_writeb(musb->mregs, MUSB_POWER, val);
272 toggle = 1;
274 /* The delay time is set to 1/4 second by default,
275 * shortening it, if accelerating A-plug detection
276 * is needed in OTG mode.
278 mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY / 4);
280 break;
281 default:
282 dev_dbg(musb->controller, "%s state not handled\n",
283 otg_state_string(musb->xceiv->state));
284 break;
286 spin_unlock_irqrestore(&musb->lock, flags);
288 dev_dbg(musb->controller, "state is %s\n",
289 otg_state_string(musb->xceiv->state));
292 static void bfin_musb_enable(struct musb *musb)
294 if (!is_otg_enabled(musb) && is_host_enabled(musb)) {
295 mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY);
296 musb->a_wait_bcon = TIMER_DELAY;
300 static void bfin_musb_disable(struct musb *musb)
304 static void bfin_musb_set_vbus(struct musb *musb, int is_on)
306 int value = musb->config->gpio_vrsel_active;
307 if (!is_on)
308 value = !value;
309 gpio_set_value(musb->config->gpio_vrsel, value);
311 dev_dbg(musb->controller, "VBUS %s, devctl %02x "
312 /* otg %3x conf %08x prcm %08x */ "\n",
313 otg_state_string(musb->xceiv->state),
314 musb_readb(musb->mregs, MUSB_DEVCTL));
317 static int bfin_musb_set_power(struct otg_transceiver *x, unsigned mA)
319 return 0;
322 static void bfin_musb_try_idle(struct musb *musb, unsigned long timeout)
324 if (!is_otg_enabled(musb) && is_host_enabled(musb))
325 mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY);
328 static int bfin_musb_vbus_status(struct musb *musb)
330 return 0;
333 static int bfin_musb_set_mode(struct musb *musb, u8 musb_mode)
335 return -EIO;
338 static int bfin_musb_adjust_channel_params(struct dma_channel *channel,
339 u16 packet_sz, u8 *mode,
340 dma_addr_t *dma_addr, u32 *len)
342 struct musb_dma_channel *musb_channel = channel->private_data;
345 * Anomaly 05000450 might cause data corruption when using DMA
346 * MODE 1 transmits with short packet. So to work around this,
347 * we truncate all MODE 1 transfers down to a multiple of the
348 * max packet size, and then do the last short packet transfer
349 * (if there is any) using MODE 0.
351 if (ANOMALY_05000450) {
352 if (musb_channel->transmit && *mode == 1)
353 *len = *len - (*len % packet_sz);
356 return 0;
359 static void bfin_musb_reg_init(struct musb *musb)
361 if (ANOMALY_05000346) {
362 bfin_write_USB_APHY_CALIB(ANOMALY_05000346_value);
363 SSYNC();
366 if (ANOMALY_05000347) {
367 bfin_write_USB_APHY_CNTRL(0x0);
368 SSYNC();
371 /* Configure PLL oscillator register */
372 bfin_write_USB_PLLOSC_CTRL(0x3080 |
373 ((480/musb->config->clkin) << 1));
374 SSYNC();
376 bfin_write_USB_SRP_CLKDIV((get_sclk()/1000) / 32 - 1);
377 SSYNC();
379 bfin_write_USB_EP_NI0_RXMAXP(64);
380 SSYNC();
382 bfin_write_USB_EP_NI0_TXMAXP(64);
383 SSYNC();
385 /* Route INTRUSB/INTR_RX/INTR_TX to USB_INT0*/
386 bfin_write_USB_GLOBINTR(0x7);
387 SSYNC();
389 bfin_write_USB_GLOBAL_CTL(GLOBAL_ENA | EP1_TX_ENA | EP2_TX_ENA |
390 EP3_TX_ENA | EP4_TX_ENA | EP5_TX_ENA |
391 EP6_TX_ENA | EP7_TX_ENA | EP1_RX_ENA |
392 EP2_RX_ENA | EP3_RX_ENA | EP4_RX_ENA |
393 EP5_RX_ENA | EP6_RX_ENA | EP7_RX_ENA);
394 SSYNC();
397 static int bfin_musb_init(struct musb *musb)
401 * Rev 1.0 BF549 EZ-KITs require PE7 to be high for both DEVICE
402 * and OTG HOST modes, while rev 1.1 and greater require PE7 to
403 * be low for DEVICE mode and high for HOST mode. We set it high
404 * here because we are in host mode
407 if (gpio_request(musb->config->gpio_vrsel, "USB_VRSEL")) {
408 printk(KERN_ERR "Failed ro request USB_VRSEL GPIO_%d\n",
409 musb->config->gpio_vrsel);
410 return -ENODEV;
412 gpio_direction_output(musb->config->gpio_vrsel, 0);
414 usb_nop_xceiv_register();
415 musb->xceiv = otg_get_transceiver();
416 if (!musb->xceiv) {
417 gpio_free(musb->config->gpio_vrsel);
418 return -ENODEV;
421 bfin_musb_reg_init(musb);
423 if (is_host_enabled(musb)) {
424 setup_timer(&musb_conn_timer,
425 musb_conn_timer_handler, (unsigned long) musb);
427 if (is_peripheral_enabled(musb))
428 musb->xceiv->set_power = bfin_musb_set_power;
430 musb->isr = blackfin_interrupt;
431 musb->double_buffer_not_ok = true;
433 return 0;
436 static int bfin_musb_exit(struct musb *musb)
438 gpio_free(musb->config->gpio_vrsel);
440 otg_put_transceiver(musb->xceiv);
441 usb_nop_xceiv_unregister();
442 return 0;
445 static const struct musb_platform_ops bfin_ops = {
446 .init = bfin_musb_init,
447 .exit = bfin_musb_exit,
449 .enable = bfin_musb_enable,
450 .disable = bfin_musb_disable,
452 .set_mode = bfin_musb_set_mode,
453 .try_idle = bfin_musb_try_idle,
455 .vbus_status = bfin_musb_vbus_status,
456 .set_vbus = bfin_musb_set_vbus,
458 .adjust_channel_params = bfin_musb_adjust_channel_params,
461 static u64 bfin_dmamask = DMA_BIT_MASK(32);
463 static int __init bfin_probe(struct platform_device *pdev)
465 struct musb_hdrc_platform_data *pdata = pdev->dev.platform_data;
466 struct platform_device *musb;
467 struct bfin_glue *glue;
469 int ret = -ENOMEM;
471 glue = kzalloc(sizeof(*glue), GFP_KERNEL);
472 if (!glue) {
473 dev_err(&pdev->dev, "failed to allocate glue context\n");
474 goto err0;
477 musb = platform_device_alloc("musb-hdrc", -1);
478 if (!musb) {
479 dev_err(&pdev->dev, "failed to allocate musb device\n");
480 goto err1;
483 musb->dev.parent = &pdev->dev;
484 musb->dev.dma_mask = &bfin_dmamask;
485 musb->dev.coherent_dma_mask = bfin_dmamask;
487 glue->dev = &pdev->dev;
488 glue->musb = musb;
490 pdata->platform_ops = &bfin_ops;
492 platform_set_drvdata(pdev, glue);
494 ret = platform_device_add_resources(musb, pdev->resource,
495 pdev->num_resources);
496 if (ret) {
497 dev_err(&pdev->dev, "failed to add resources\n");
498 goto err2;
501 ret = platform_device_add_data(musb, pdata, sizeof(*pdata));
502 if (ret) {
503 dev_err(&pdev->dev, "failed to add platform_data\n");
504 goto err2;
507 ret = platform_device_add(musb);
508 if (ret) {
509 dev_err(&pdev->dev, "failed to register musb device\n");
510 goto err2;
513 return 0;
515 err2:
516 platform_device_put(musb);
518 err1:
519 kfree(glue);
521 err0:
522 return ret;
525 static int __exit bfin_remove(struct platform_device *pdev)
527 struct bfin_glue *glue = platform_get_drvdata(pdev);
529 platform_device_del(glue->musb);
530 platform_device_put(glue->musb);
531 kfree(glue);
533 return 0;
536 #ifdef CONFIG_PM
537 static int bfin_suspend(struct device *dev)
539 struct bfin_glue *glue = dev_get_drvdata(dev);
540 struct musb *musb = glue_to_musb(glue);
542 if (is_host_active(musb))
544 * During hibernate gpio_vrsel will change from high to low
545 * low which will generate wakeup event resume the system
546 * immediately. Set it to 0 before hibernate to avoid this
547 * wakeup event.
549 gpio_set_value(musb->config->gpio_vrsel, 0);
551 return 0;
554 static int bfin_resume(struct device *dev)
556 struct bfin_glue *glue = dev_get_drvdata(dev);
557 struct musb *musb = glue_to_musb(glue);
559 bfin_musb_reg_init(musb);
561 return 0;
564 static struct dev_pm_ops bfin_pm_ops = {
565 .suspend = bfin_suspend,
566 .resume = bfin_resume,
569 #define DEV_PM_OPS &bfin_pm_ops
570 #else
571 #define DEV_PM_OPS NULL
572 #endif
574 static struct platform_driver bfin_driver = {
575 .remove = __exit_p(bfin_remove),
576 .driver = {
577 .name = "musb-blackfin",
578 .pm = DEV_PM_OPS,
582 MODULE_DESCRIPTION("Blackfin MUSB Glue Layer");
583 MODULE_AUTHOR("Bryan Wy <cooloney@kernel.org>");
584 MODULE_LICENSE("GPL v2");
586 static int __init bfin_init(void)
588 return platform_driver_probe(&bfin_driver, bfin_probe);
590 subsys_initcall(bfin_init);
592 static void __exit bfin_exit(void)
594 platform_driver_unregister(&bfin_driver);
596 module_exit(bfin_exit);