proc: use seq_puts()/seq_putc() where possible
[linux-2.6/next.git] / drivers / usb / musb / blackfin.c
blobeeba228eb2af7accb188ad6e3238c146afd0191f
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 "blackfin.h"
26 struct bfin_glue {
27 struct device *dev;
28 struct platform_device *musb;
30 #define glue_to_musb(g) platform_get_drvdata(g->musb)
33 * Load an endpoint's FIFO
35 void musb_write_fifo(struct musb_hw_ep *hw_ep, u16 len, const u8 *src)
37 void __iomem *fifo = hw_ep->fifo;
38 void __iomem *epio = hw_ep->regs;
39 u8 epnum = hw_ep->epnum;
41 prefetch((u8 *)src);
43 musb_writew(epio, MUSB_TXCOUNT, len);
45 DBG(4, "TX ep%d fifo %p count %d buf %p, epio %p\n",
46 hw_ep->epnum, fifo, len, src, epio);
48 dump_fifo_data(src, len);
50 if (!ANOMALY_05000380 && epnum != 0) {
51 u16 dma_reg;
53 flush_dcache_range((unsigned long)src,
54 (unsigned long)(src + len));
56 /* Setup DMA address register */
57 dma_reg = (u32)src;
58 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_ADDR_LOW), dma_reg);
59 SSYNC();
61 dma_reg = (u32)src >> 16;
62 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_ADDR_HIGH), dma_reg);
63 SSYNC();
65 /* Setup DMA count register */
66 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_COUNT_LOW), len);
67 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_COUNT_HIGH), 0);
68 SSYNC();
70 /* Enable the DMA */
71 dma_reg = (epnum << 4) | DMA_ENA | INT_ENA | DIRECTION;
72 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_CTRL), dma_reg);
73 SSYNC();
75 /* Wait for compelete */
76 while (!(bfin_read_USB_DMA_INTERRUPT() & (1 << epnum)))
77 cpu_relax();
79 /* acknowledge dma interrupt */
80 bfin_write_USB_DMA_INTERRUPT(1 << epnum);
81 SSYNC();
83 /* Reset DMA */
84 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_CTRL), 0);
85 SSYNC();
86 } else {
87 SSYNC();
89 if (unlikely((unsigned long)src & 0x01))
90 outsw_8((unsigned long)fifo, src, (len + 1) >> 1);
91 else
92 outsw((unsigned long)fifo, src, (len + 1) >> 1);
96 * Unload an endpoint's FIFO
98 void musb_read_fifo(struct musb_hw_ep *hw_ep, u16 len, u8 *dst)
100 void __iomem *fifo = hw_ep->fifo;
101 u8 epnum = hw_ep->epnum;
103 if (ANOMALY_05000467 && epnum != 0) {
104 u16 dma_reg;
106 invalidate_dcache_range((unsigned long)dst,
107 (unsigned long)(dst + len));
109 /* Setup DMA address register */
110 dma_reg = (u32)dst;
111 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_ADDR_LOW), dma_reg);
112 SSYNC();
114 dma_reg = (u32)dst >> 16;
115 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_ADDR_HIGH), dma_reg);
116 SSYNC();
118 /* Setup DMA count register */
119 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_COUNT_LOW), len);
120 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_COUNT_HIGH), 0);
121 SSYNC();
123 /* Enable the DMA */
124 dma_reg = (epnum << 4) | DMA_ENA | INT_ENA;
125 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_CTRL), dma_reg);
126 SSYNC();
128 /* Wait for compelete */
129 while (!(bfin_read_USB_DMA_INTERRUPT() & (1 << epnum)))
130 cpu_relax();
132 /* acknowledge dma interrupt */
133 bfin_write_USB_DMA_INTERRUPT(1 << epnum);
134 SSYNC();
136 /* Reset DMA */
137 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_CTRL), 0);
138 SSYNC();
139 } else {
140 SSYNC();
141 /* Read the last byte of packet with odd size from address fifo + 4
142 * to trigger 1 byte access to EP0 FIFO.
144 if (len == 1)
145 *dst = (u8)inw((unsigned long)fifo + 4);
146 else {
147 if (unlikely((unsigned long)dst & 0x01))
148 insw_8((unsigned long)fifo, dst, len >> 1);
149 else
150 insw((unsigned long)fifo, dst, len >> 1);
152 if (len & 0x01)
153 *(dst + len - 1) = (u8)inw((unsigned long)fifo + 4);
156 DBG(4, "%cX ep%d fifo %p count %d buf %p\n",
157 'R', hw_ep->epnum, fifo, len, dst);
159 dump_fifo_data(dst, len);
162 static irqreturn_t blackfin_interrupt(int irq, void *__hci)
164 unsigned long flags;
165 irqreturn_t retval = IRQ_NONE;
166 struct musb *musb = __hci;
168 spin_lock_irqsave(&musb->lock, flags);
170 musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB);
171 musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX);
172 musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX);
174 if (musb->int_usb || musb->int_tx || musb->int_rx) {
175 musb_writeb(musb->mregs, MUSB_INTRUSB, musb->int_usb);
176 musb_writew(musb->mregs, MUSB_INTRTX, musb->int_tx);
177 musb_writew(musb->mregs, MUSB_INTRRX, musb->int_rx);
178 retval = musb_interrupt(musb);
181 /* Start sampling ID pin, when plug is removed from MUSB */
182 if ((is_otg_enabled(musb) && (musb->xceiv->state == OTG_STATE_B_IDLE
183 || musb->xceiv->state == OTG_STATE_A_WAIT_BCON)) ||
184 (musb->int_usb & MUSB_INTR_DISCONNECT && is_host_active(musb))) {
185 mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY);
186 musb->a_wait_bcon = TIMER_DELAY;
189 spin_unlock_irqrestore(&musb->lock, flags);
191 return retval;
194 static void musb_conn_timer_handler(unsigned long _musb)
196 struct musb *musb = (void *)_musb;
197 unsigned long flags;
198 u16 val;
199 static u8 toggle;
201 spin_lock_irqsave(&musb->lock, flags);
202 switch (musb->xceiv->state) {
203 case OTG_STATE_A_IDLE:
204 case OTG_STATE_A_WAIT_BCON:
205 /* Start a new session */
206 val = musb_readw(musb->mregs, MUSB_DEVCTL);
207 val &= ~MUSB_DEVCTL_SESSION;
208 musb_writew(musb->mregs, MUSB_DEVCTL, val);
209 val |= MUSB_DEVCTL_SESSION;
210 musb_writew(musb->mregs, MUSB_DEVCTL, val);
211 /* Check if musb is host or peripheral. */
212 val = musb_readw(musb->mregs, MUSB_DEVCTL);
214 if (!(val & MUSB_DEVCTL_BDEVICE)) {
215 gpio_set_value(musb->config->gpio_vrsel, 1);
216 musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
217 } else {
218 gpio_set_value(musb->config->gpio_vrsel, 0);
219 /* Ignore VBUSERROR and SUSPEND IRQ */
220 val = musb_readb(musb->mregs, MUSB_INTRUSBE);
221 val &= ~MUSB_INTR_VBUSERROR;
222 musb_writeb(musb->mregs, MUSB_INTRUSBE, val);
224 val = MUSB_INTR_SUSPEND | MUSB_INTR_VBUSERROR;
225 musb_writeb(musb->mregs, MUSB_INTRUSB, val);
226 if (is_otg_enabled(musb))
227 musb->xceiv->state = OTG_STATE_B_IDLE;
228 else
229 musb_writeb(musb->mregs, MUSB_POWER, MUSB_POWER_HSENAB);
231 mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY);
232 break;
233 case OTG_STATE_B_IDLE:
235 if (!is_peripheral_enabled(musb))
236 break;
237 /* Start a new session. It seems that MUSB needs taking
238 * some time to recognize the type of the plug inserted?
240 val = musb_readw(musb->mregs, MUSB_DEVCTL);
241 val |= MUSB_DEVCTL_SESSION;
242 musb_writew(musb->mregs, MUSB_DEVCTL, val);
243 val = musb_readw(musb->mregs, MUSB_DEVCTL);
245 if (!(val & MUSB_DEVCTL_BDEVICE)) {
246 gpio_set_value(musb->config->gpio_vrsel, 1);
247 musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
248 } else {
249 gpio_set_value(musb->config->gpio_vrsel, 0);
251 /* Ignore VBUSERROR and SUSPEND IRQ */
252 val = musb_readb(musb->mregs, MUSB_INTRUSBE);
253 val &= ~MUSB_INTR_VBUSERROR;
254 musb_writeb(musb->mregs, MUSB_INTRUSBE, val);
256 val = MUSB_INTR_SUSPEND | MUSB_INTR_VBUSERROR;
257 musb_writeb(musb->mregs, MUSB_INTRUSB, val);
259 /* Toggle the Soft Conn bit, so that we can response to
260 * the inserting of either A-plug or B-plug.
262 if (toggle) {
263 val = musb_readb(musb->mregs, MUSB_POWER);
264 val &= ~MUSB_POWER_SOFTCONN;
265 musb_writeb(musb->mregs, MUSB_POWER, val);
266 toggle = 0;
267 } else {
268 val = musb_readb(musb->mregs, MUSB_POWER);
269 val |= MUSB_POWER_SOFTCONN;
270 musb_writeb(musb->mregs, MUSB_POWER, val);
271 toggle = 1;
273 /* The delay time is set to 1/4 second by default,
274 * shortening it, if accelerating A-plug detection
275 * is needed in OTG mode.
277 mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY / 4);
279 break;
280 default:
281 DBG(1, "%s state not handled\n", otg_state_string(musb));
282 break;
284 spin_unlock_irqrestore(&musb->lock, flags);
286 DBG(4, "state is %s\n", otg_state_string(musb));
289 static void bfin_musb_enable(struct musb *musb)
291 if (!is_otg_enabled(musb) && is_host_enabled(musb)) {
292 mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY);
293 musb->a_wait_bcon = TIMER_DELAY;
297 static void bfin_musb_disable(struct musb *musb)
301 static void bfin_musb_set_vbus(struct musb *musb, int is_on)
303 int value = musb->config->gpio_vrsel_active;
304 if (!is_on)
305 value = !value;
306 gpio_set_value(musb->config->gpio_vrsel, value);
308 DBG(1, "VBUS %s, devctl %02x "
309 /* otg %3x conf %08x prcm %08x */ "\n",
310 otg_state_string(musb),
311 musb_readb(musb->mregs, MUSB_DEVCTL));
314 static int bfin_musb_set_power(struct otg_transceiver *x, unsigned mA)
316 return 0;
319 static void bfin_musb_try_idle(struct musb *musb, unsigned long timeout)
321 if (!is_otg_enabled(musb) && is_host_enabled(musb))
322 mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY);
325 static int bfin_musb_get_vbus_status(struct musb *musb)
327 return 0;
330 static int bfin_musb_set_mode(struct musb *musb, u8 musb_mode)
332 return -EIO;
335 static void bfin_musb_reg_init(struct musb *musb)
337 if (ANOMALY_05000346) {
338 bfin_write_USB_APHY_CALIB(ANOMALY_05000346_value);
339 SSYNC();
342 if (ANOMALY_05000347) {
343 bfin_write_USB_APHY_CNTRL(0x0);
344 SSYNC();
347 /* Configure PLL oscillator register */
348 bfin_write_USB_PLLOSC_CTRL(0x3080 |
349 ((480/musb->config->clkin) << 1));
350 SSYNC();
352 bfin_write_USB_SRP_CLKDIV((get_sclk()/1000) / 32 - 1);
353 SSYNC();
355 bfin_write_USB_EP_NI0_RXMAXP(64);
356 SSYNC();
358 bfin_write_USB_EP_NI0_TXMAXP(64);
359 SSYNC();
361 /* Route INTRUSB/INTR_RX/INTR_TX to USB_INT0*/
362 bfin_write_USB_GLOBINTR(0x7);
363 SSYNC();
365 bfin_write_USB_GLOBAL_CTL(GLOBAL_ENA | EP1_TX_ENA | EP2_TX_ENA |
366 EP3_TX_ENA | EP4_TX_ENA | EP5_TX_ENA |
367 EP6_TX_ENA | EP7_TX_ENA | EP1_RX_ENA |
368 EP2_RX_ENA | EP3_RX_ENA | EP4_RX_ENA |
369 EP5_RX_ENA | EP6_RX_ENA | EP7_RX_ENA);
370 SSYNC();
373 static int bfin_musb_init(struct musb *musb)
377 * Rev 1.0 BF549 EZ-KITs require PE7 to be high for both DEVICE
378 * and OTG HOST modes, while rev 1.1 and greater require PE7 to
379 * be low for DEVICE mode and high for HOST mode. We set it high
380 * here because we are in host mode
383 if (gpio_request(musb->config->gpio_vrsel, "USB_VRSEL")) {
384 printk(KERN_ERR "Failed ro request USB_VRSEL GPIO_%d\n",
385 musb->config->gpio_vrsel);
386 return -ENODEV;
388 gpio_direction_output(musb->config->gpio_vrsel, 0);
390 usb_nop_xceiv_register();
391 musb->xceiv = otg_get_transceiver();
392 if (!musb->xceiv) {
393 gpio_free(musb->config->gpio_vrsel);
394 return -ENODEV;
397 bfin_musb_reg_init(musb);
399 if (is_host_enabled(musb)) {
400 setup_timer(&musb_conn_timer,
401 musb_conn_timer_handler, (unsigned long) musb);
403 if (is_peripheral_enabled(musb))
404 musb->xceiv->set_power = bfin_musb_set_power;
406 musb->isr = blackfin_interrupt;
408 return 0;
411 static int bfin_musb_exit(struct musb *musb)
413 gpio_free(musb->config->gpio_vrsel);
415 otg_put_transceiver(musb->xceiv);
416 usb_nop_xceiv_unregister();
417 return 0;
420 static const struct musb_platform_ops bfin_ops = {
421 .init = bfin_musb_init,
422 .exit = bfin_musb_exit,
424 .enable = bfin_musb_enable,
425 .disable = bfin_musb_disable,
427 .set_mode = bfin_musb_set_mode,
428 .try_idle = bfin_musb_try_idle,
430 .vbus_status = bfin_musb_vbus_status,
431 .set_vbus = bfin_musb_set_vbus,
434 static u64 bfin_dmamask = DMA_BIT_MASK(32);
436 static int __init bfin_probe(struct platform_device *pdev)
438 struct musb_hdrc_platform_data *pdata = pdev->dev.platform_data;
439 struct platform_device *musb;
440 struct bfin_glue *glue;
442 int ret = -ENOMEM;
444 glue = kzalloc(sizeof(*glue), GFP_KERNEL);
445 if (!glue) {
446 dev_err(&pdev->dev, "failed to allocate glue context\n");
447 goto err0;
450 musb = platform_device_alloc("musb-hdrc", -1);
451 if (!musb) {
452 dev_err(&pdev->dev, "failed to allocate musb device\n");
453 goto err1;
456 musb->dev.parent = &pdev->dev;
457 musb->dev.dma_mask = &bfin_dmamask;
458 musb->dev.coherent_dma_mask = bfin_dmamask;
460 glue->dev = &pdev->dev;
461 glue->musb = musb;
463 pdata->platform_ops = &bfin_ops;
465 platform_set_drvdata(pdev, glue);
467 ret = platform_device_add_resources(musb, pdev->resource,
468 pdev->num_resources);
469 if (ret) {
470 dev_err(&pdev->dev, "failed to add resources\n");
471 goto err2;
474 ret = platform_device_add_data(musb, pdata, sizeof(*pdata));
475 if (ret) {
476 dev_err(&pdev->dev, "failed to add platform_data\n");
477 goto err2;
480 ret = platform_device_add(musb);
481 if (ret) {
482 dev_err(&pdev->dev, "failed to register musb device\n");
483 goto err2;
486 return 0;
488 err2:
489 platform_device_put(musb);
491 err1:
492 kfree(glue);
494 err0:
495 return ret;
498 static int __exit bfin_remove(struct platform_device *pdev)
500 struct bfin_glue *glue = platform_get_drvdata(pdev);
502 platform_device_del(glue->musb);
503 platform_device_put(glue->musb);
504 kfree(glue);
506 return 0;
509 #ifdef CONFIG_PM
510 static int bfin_suspend(struct device *dev)
512 struct bfin_glue *glue = dev_get_drvdata(dev);
513 struct musb *musb = glue_to_musb(glue);
515 if (is_host_active(musb))
517 * During hibernate gpio_vrsel will change from high to low
518 * low which will generate wakeup event resume the system
519 * immediately. Set it to 0 before hibernate to avoid this
520 * wakeup event.
522 gpio_set_value(musb->config->gpio_vrsel, 0);
524 return 0;
527 static int bfin_resume(struct device *dev)
529 struct bfin_glue *glue = dev_get_drvdata(dev);
530 struct musb *musb = glue_to_musb(glue);
532 bfin_musb_reg_init(musb);
534 return 0;
537 static struct dev_pm_ops bfin_pm_ops = {
538 .suspend = bfin_suspend,
539 .resume = bfin_resume,
542 #define DEV_PM_OPS &bfin_pm_op,
543 #else
544 #define DEV_PM_OPS NULL
545 #endif
547 static struct platform_driver bfin_driver = {
548 .remove = __exit_p(bfin_remove),
549 .driver = {
550 .name = "musb-bfin",
551 .pm = DEV_PM_OPS,
555 MODULE_DESCRIPTION("Blackfin MUSB Glue Layer");
556 MODULE_AUTHOR("Bryan Wy <cooloney@kernel.org>");
557 MODULE_LICENSE("GPL v2");
559 static int __init bfin_init(void)
561 return platform_driver_probe(&bfin_driver, bfin_probe);
563 subsys_initcall(bfin_init);
565 static void __exit bfin_exit(void)
567 platform_driver_unregister(&bfin_driver);
569 module_exit(bfin_exit);