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.
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>
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"
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 struct musb
*musb
= hw_ep
->musb
;
39 void __iomem
*fifo
= hw_ep
->fifo
;
40 void __iomem
*epio
= hw_ep
->regs
;
41 u8 epnum
= hw_ep
->epnum
;
45 musb_writew(epio
, MUSB_TXCOUNT
, len
);
47 dev_dbg(musb
->controller
, "TX ep%d fifo %p count %d buf %p, epio %p\n",
48 hw_ep
->epnum
, fifo
, len
, src
, epio
);
50 dump_fifo_data(src
, len
);
52 if (!ANOMALY_05000380
&& epnum
!= 0) {
55 flush_dcache_range((unsigned long)src
,
56 (unsigned long)(src
+ len
));
58 /* Setup DMA address register */
60 bfin_write16(USB_DMA_REG(epnum
, USB_DMAx_ADDR_LOW
), dma_reg
);
63 dma_reg
= (u32
)src
>> 16;
64 bfin_write16(USB_DMA_REG(epnum
, USB_DMAx_ADDR_HIGH
), dma_reg
);
67 /* Setup DMA count register */
68 bfin_write16(USB_DMA_REG(epnum
, USB_DMAx_COUNT_LOW
), len
);
69 bfin_write16(USB_DMA_REG(epnum
, USB_DMAx_COUNT_HIGH
), 0);
73 dma_reg
= (epnum
<< 4) | DMA_ENA
| INT_ENA
| DIRECTION
;
74 bfin_write16(USB_DMA_REG(epnum
, USB_DMAx_CTRL
), dma_reg
);
77 /* Wait for compelete */
78 while (!(bfin_read_USB_DMA_INTERRUPT() & (1 << epnum
)))
81 /* acknowledge dma interrupt */
82 bfin_write_USB_DMA_INTERRUPT(1 << epnum
);
86 bfin_write16(USB_DMA_REG(epnum
, USB_DMAx_CTRL
), 0);
91 if (unlikely((unsigned long)src
& 0x01))
92 outsw_8((unsigned long)fifo
, src
, (len
+ 1) >> 1);
94 outsw((unsigned long)fifo
, src
, (len
+ 1) >> 1);
98 * Unload an endpoint's FIFO
100 void musb_read_fifo(struct musb_hw_ep
*hw_ep
, u16 len
, u8
*dst
)
102 struct musb
*musb
= hw_ep
->musb
;
103 void __iomem
*fifo
= hw_ep
->fifo
;
104 u8 epnum
= hw_ep
->epnum
;
106 if (ANOMALY_05000467
&& epnum
!= 0) {
109 invalidate_dcache_range((unsigned long)dst
,
110 (unsigned long)(dst
+ len
));
112 /* Setup DMA address register */
114 bfin_write16(USB_DMA_REG(epnum
, USB_DMAx_ADDR_LOW
), dma_reg
);
117 dma_reg
= (u32
)dst
>> 16;
118 bfin_write16(USB_DMA_REG(epnum
, USB_DMAx_ADDR_HIGH
), dma_reg
);
121 /* Setup DMA count register */
122 bfin_write16(USB_DMA_REG(epnum
, USB_DMAx_COUNT_LOW
), len
);
123 bfin_write16(USB_DMA_REG(epnum
, USB_DMAx_COUNT_HIGH
), 0);
127 dma_reg
= (epnum
<< 4) | DMA_ENA
| INT_ENA
;
128 bfin_write16(USB_DMA_REG(epnum
, USB_DMAx_CTRL
), dma_reg
);
131 /* Wait for compelete */
132 while (!(bfin_read_USB_DMA_INTERRUPT() & (1 << epnum
)))
135 /* acknowledge dma interrupt */
136 bfin_write_USB_DMA_INTERRUPT(1 << epnum
);
140 bfin_write16(USB_DMA_REG(epnum
, USB_DMAx_CTRL
), 0);
144 /* Read the last byte of packet with odd size from address fifo + 4
145 * to trigger 1 byte access to EP0 FIFO.
148 *dst
= (u8
)inw((unsigned long)fifo
+ 4);
150 if (unlikely((unsigned long)dst
& 0x01))
151 insw_8((unsigned long)fifo
, dst
, len
>> 1);
153 insw((unsigned long)fifo
, dst
, len
>> 1);
156 *(dst
+ len
- 1) = (u8
)inw((unsigned long)fifo
+ 4);
159 dev_dbg(musb
->controller
, "%cX ep%d fifo %p count %d buf %p\n",
160 'R', hw_ep
->epnum
, fifo
, len
, dst
);
162 dump_fifo_data(dst
, len
);
165 static irqreturn_t
blackfin_interrupt(int irq
, void *__hci
)
168 irqreturn_t retval
= IRQ_NONE
;
169 struct musb
*musb
= __hci
;
171 spin_lock_irqsave(&musb
->lock
, flags
);
173 musb
->int_usb
= musb_readb(musb
->mregs
, MUSB_INTRUSB
);
174 musb
->int_tx
= musb_readw(musb
->mregs
, MUSB_INTRTX
);
175 musb
->int_rx
= musb_readw(musb
->mregs
, MUSB_INTRRX
);
177 if (musb
->int_usb
|| musb
->int_tx
|| musb
->int_rx
) {
178 musb_writeb(musb
->mregs
, MUSB_INTRUSB
, musb
->int_usb
);
179 musb_writew(musb
->mregs
, MUSB_INTRTX
, musb
->int_tx
);
180 musb_writew(musb
->mregs
, MUSB_INTRRX
, musb
->int_rx
);
181 retval
= musb_interrupt(musb
);
184 /* Start sampling ID pin, when plug is removed from MUSB */
185 if ((is_otg_enabled(musb
) && (musb
->xceiv
->state
== OTG_STATE_B_IDLE
186 || musb
->xceiv
->state
== OTG_STATE_A_WAIT_BCON
)) ||
187 (musb
->int_usb
& MUSB_INTR_DISCONNECT
&& is_host_active(musb
))) {
188 mod_timer(&musb_conn_timer
, jiffies
+ TIMER_DELAY
);
189 musb
->a_wait_bcon
= TIMER_DELAY
;
192 spin_unlock_irqrestore(&musb
->lock
, flags
);
197 static void musb_conn_timer_handler(unsigned long _musb
)
199 struct musb
*musb
= (void *)_musb
;
204 spin_lock_irqsave(&musb
->lock
, flags
);
205 switch (musb
->xceiv
->state
) {
206 case OTG_STATE_A_IDLE
:
207 case OTG_STATE_A_WAIT_BCON
:
208 /* Start a new session */
209 val
= musb_readw(musb
->mregs
, MUSB_DEVCTL
);
210 val
&= ~MUSB_DEVCTL_SESSION
;
211 musb_writew(musb
->mregs
, MUSB_DEVCTL
, val
);
212 val
|= MUSB_DEVCTL_SESSION
;
213 musb_writew(musb
->mregs
, MUSB_DEVCTL
, val
);
214 /* Check if musb is host or peripheral. */
215 val
= musb_readw(musb
->mregs
, MUSB_DEVCTL
);
217 if (!(val
& MUSB_DEVCTL_BDEVICE
)) {
218 gpio_set_value(musb
->config
->gpio_vrsel
, 1);
219 musb
->xceiv
->state
= OTG_STATE_A_WAIT_BCON
;
221 gpio_set_value(musb
->config
->gpio_vrsel
, 0);
222 /* Ignore VBUSERROR and SUSPEND IRQ */
223 val
= musb_readb(musb
->mregs
, MUSB_INTRUSBE
);
224 val
&= ~MUSB_INTR_VBUSERROR
;
225 musb_writeb(musb
->mregs
, MUSB_INTRUSBE
, val
);
227 val
= MUSB_INTR_SUSPEND
| MUSB_INTR_VBUSERROR
;
228 musb_writeb(musb
->mregs
, MUSB_INTRUSB
, val
);
229 if (is_otg_enabled(musb
))
230 musb
->xceiv
->state
= OTG_STATE_B_IDLE
;
232 musb_writeb(musb
->mregs
, MUSB_POWER
, MUSB_POWER_HSENAB
);
234 mod_timer(&musb_conn_timer
, jiffies
+ TIMER_DELAY
);
236 case OTG_STATE_B_IDLE
:
238 if (!is_peripheral_enabled(musb
))
240 /* Start a new session. It seems that MUSB needs taking
241 * some time to recognize the type of the plug inserted?
243 val
= musb_readw(musb
->mregs
, MUSB_DEVCTL
);
244 val
|= MUSB_DEVCTL_SESSION
;
245 musb_writew(musb
->mregs
, MUSB_DEVCTL
, val
);
246 val
= musb_readw(musb
->mregs
, MUSB_DEVCTL
);
248 if (!(val
& MUSB_DEVCTL_BDEVICE
)) {
249 gpio_set_value(musb
->config
->gpio_vrsel
, 1);
250 musb
->xceiv
->state
= OTG_STATE_A_WAIT_BCON
;
252 gpio_set_value(musb
->config
->gpio_vrsel
, 0);
254 /* Ignore VBUSERROR and SUSPEND IRQ */
255 val
= musb_readb(musb
->mregs
, MUSB_INTRUSBE
);
256 val
&= ~MUSB_INTR_VBUSERROR
;
257 musb_writeb(musb
->mregs
, MUSB_INTRUSBE
, val
);
259 val
= MUSB_INTR_SUSPEND
| MUSB_INTR_VBUSERROR
;
260 musb_writeb(musb
->mregs
, MUSB_INTRUSB
, val
);
262 /* Toggle the Soft Conn bit, so that we can response to
263 * the inserting of either A-plug or B-plug.
266 val
= musb_readb(musb
->mregs
, MUSB_POWER
);
267 val
&= ~MUSB_POWER_SOFTCONN
;
268 musb_writeb(musb
->mregs
, MUSB_POWER
, val
);
271 val
= musb_readb(musb
->mregs
, MUSB_POWER
);
272 val
|= MUSB_POWER_SOFTCONN
;
273 musb_writeb(musb
->mregs
, MUSB_POWER
, val
);
276 /* The delay time is set to 1/4 second by default,
277 * shortening it, if accelerating A-plug detection
278 * is needed in OTG mode.
280 mod_timer(&musb_conn_timer
, jiffies
+ TIMER_DELAY
/ 4);
284 dev_dbg(musb
->controller
, "%s state not handled\n",
285 otg_state_string(musb
->xceiv
->state
));
288 spin_unlock_irqrestore(&musb
->lock
, flags
);
290 dev_dbg(musb
->controller
, "state is %s\n",
291 otg_state_string(musb
->xceiv
->state
));
294 static void bfin_musb_enable(struct musb
*musb
)
296 if (!is_otg_enabled(musb
) && is_host_enabled(musb
)) {
297 mod_timer(&musb_conn_timer
, jiffies
+ TIMER_DELAY
);
298 musb
->a_wait_bcon
= TIMER_DELAY
;
302 static void bfin_musb_disable(struct musb
*musb
)
306 static void bfin_musb_set_vbus(struct musb
*musb
, int is_on
)
308 int value
= musb
->config
->gpio_vrsel_active
;
311 gpio_set_value(musb
->config
->gpio_vrsel
, value
);
313 dev_dbg(musb
->controller
, "VBUS %s, devctl %02x "
314 /* otg %3x conf %08x prcm %08x */ "\n",
315 otg_state_string(musb
->xceiv
->state
),
316 musb_readb(musb
->mregs
, MUSB_DEVCTL
));
319 static int bfin_musb_set_power(struct otg_transceiver
*x
, unsigned mA
)
324 static void bfin_musb_try_idle(struct musb
*musb
, unsigned long timeout
)
326 if (!is_otg_enabled(musb
) && is_host_enabled(musb
))
327 mod_timer(&musb_conn_timer
, jiffies
+ TIMER_DELAY
);
330 static int bfin_musb_vbus_status(struct musb
*musb
)
335 static int bfin_musb_set_mode(struct musb
*musb
, u8 musb_mode
)
340 static int bfin_musb_adjust_channel_params(struct dma_channel
*channel
,
341 u16 packet_sz
, u8
*mode
,
342 dma_addr_t
*dma_addr
, u32
*len
)
344 struct musb_dma_channel
*musb_channel
= channel
->private_data
;
347 * Anomaly 05000450 might cause data corruption when using DMA
348 * MODE 1 transmits with short packet. So to work around this,
349 * we truncate all MODE 1 transfers down to a multiple of the
350 * max packet size, and then do the last short packet transfer
351 * (if there is any) using MODE 0.
353 if (ANOMALY_05000450
) {
354 if (musb_channel
->transmit
&& *mode
== 1)
355 *len
= *len
- (*len
% packet_sz
);
361 static void bfin_musb_reg_init(struct musb
*musb
)
363 if (ANOMALY_05000346
) {
364 bfin_write_USB_APHY_CALIB(ANOMALY_05000346_value
);
368 if (ANOMALY_05000347
) {
369 bfin_write_USB_APHY_CNTRL(0x0);
373 /* Configure PLL oscillator register */
374 bfin_write_USB_PLLOSC_CTRL(0x3080 |
375 ((480/musb
->config
->clkin
) << 1));
378 bfin_write_USB_SRP_CLKDIV((get_sclk()/1000) / 32 - 1);
381 bfin_write_USB_EP_NI0_RXMAXP(64);
384 bfin_write_USB_EP_NI0_TXMAXP(64);
387 /* Route INTRUSB/INTR_RX/INTR_TX to USB_INT0*/
388 bfin_write_USB_GLOBINTR(0x7);
391 bfin_write_USB_GLOBAL_CTL(GLOBAL_ENA
| EP1_TX_ENA
| EP2_TX_ENA
|
392 EP3_TX_ENA
| EP4_TX_ENA
| EP5_TX_ENA
|
393 EP6_TX_ENA
| EP7_TX_ENA
| EP1_RX_ENA
|
394 EP2_RX_ENA
| EP3_RX_ENA
| EP4_RX_ENA
|
395 EP5_RX_ENA
| EP6_RX_ENA
| EP7_RX_ENA
);
399 static int bfin_musb_init(struct musb
*musb
)
403 * Rev 1.0 BF549 EZ-KITs require PE7 to be high for both DEVICE
404 * and OTG HOST modes, while rev 1.1 and greater require PE7 to
405 * be low for DEVICE mode and high for HOST mode. We set it high
406 * here because we are in host mode
409 if (gpio_request(musb
->config
->gpio_vrsel
, "USB_VRSEL")) {
410 printk(KERN_ERR
"Failed ro request USB_VRSEL GPIO_%d\n",
411 musb
->config
->gpio_vrsel
);
414 gpio_direction_output(musb
->config
->gpio_vrsel
, 0);
416 usb_nop_xceiv_register();
417 musb
->xceiv
= otg_get_transceiver();
419 gpio_free(musb
->config
->gpio_vrsel
);
423 bfin_musb_reg_init(musb
);
425 if (is_host_enabled(musb
)) {
426 setup_timer(&musb_conn_timer
,
427 musb_conn_timer_handler
, (unsigned long) musb
);
429 if (is_peripheral_enabled(musb
))
430 musb
->xceiv
->set_power
= bfin_musb_set_power
;
432 musb
->isr
= blackfin_interrupt
;
433 musb
->double_buffer_not_ok
= true;
438 static int bfin_musb_exit(struct musb
*musb
)
440 gpio_free(musb
->config
->gpio_vrsel
);
442 otg_put_transceiver(musb
->xceiv
);
443 usb_nop_xceiv_unregister();
447 static const struct musb_platform_ops bfin_ops
= {
448 .init
= bfin_musb_init
,
449 .exit
= bfin_musb_exit
,
451 .enable
= bfin_musb_enable
,
452 .disable
= bfin_musb_disable
,
454 .set_mode
= bfin_musb_set_mode
,
455 .try_idle
= bfin_musb_try_idle
,
457 .vbus_status
= bfin_musb_vbus_status
,
458 .set_vbus
= bfin_musb_set_vbus
,
460 .adjust_channel_params
= bfin_musb_adjust_channel_params
,
463 static u64 bfin_dmamask
= DMA_BIT_MASK(32);
465 static int __init
bfin_probe(struct platform_device
*pdev
)
467 struct musb_hdrc_platform_data
*pdata
= pdev
->dev
.platform_data
;
468 struct platform_device
*musb
;
469 struct bfin_glue
*glue
;
473 glue
= kzalloc(sizeof(*glue
), GFP_KERNEL
);
475 dev_err(&pdev
->dev
, "failed to allocate glue context\n");
479 musb
= platform_device_alloc("musb-hdrc", -1);
481 dev_err(&pdev
->dev
, "failed to allocate musb device\n");
485 musb
->dev
.parent
= &pdev
->dev
;
486 musb
->dev
.dma_mask
= &bfin_dmamask
;
487 musb
->dev
.coherent_dma_mask
= bfin_dmamask
;
489 glue
->dev
= &pdev
->dev
;
492 pdata
->platform_ops
= &bfin_ops
;
494 platform_set_drvdata(pdev
, glue
);
496 ret
= platform_device_add_resources(musb
, pdev
->resource
,
497 pdev
->num_resources
);
499 dev_err(&pdev
->dev
, "failed to add resources\n");
503 ret
= platform_device_add_data(musb
, pdata
, sizeof(*pdata
));
505 dev_err(&pdev
->dev
, "failed to add platform_data\n");
509 ret
= platform_device_add(musb
);
511 dev_err(&pdev
->dev
, "failed to register musb device\n");
518 platform_device_put(musb
);
527 static int __exit
bfin_remove(struct platform_device
*pdev
)
529 struct bfin_glue
*glue
= platform_get_drvdata(pdev
);
531 platform_device_del(glue
->musb
);
532 platform_device_put(glue
->musb
);
539 static int bfin_suspend(struct device
*dev
)
541 struct bfin_glue
*glue
= dev_get_drvdata(dev
);
542 struct musb
*musb
= glue_to_musb(glue
);
544 if (is_host_active(musb
))
546 * During hibernate gpio_vrsel will change from high to low
547 * low which will generate wakeup event resume the system
548 * immediately. Set it to 0 before hibernate to avoid this
551 gpio_set_value(musb
->config
->gpio_vrsel
, 0);
556 static int bfin_resume(struct device
*dev
)
558 struct bfin_glue
*glue
= dev_get_drvdata(dev
);
559 struct musb
*musb
= glue_to_musb(glue
);
561 bfin_musb_reg_init(musb
);
566 static struct dev_pm_ops bfin_pm_ops
= {
567 .suspend
= bfin_suspend
,
568 .resume
= bfin_resume
,
571 #define DEV_PM_OPS &bfin_pm_ops
573 #define DEV_PM_OPS NULL
576 static struct platform_driver bfin_driver
= {
577 .remove
= __exit_p(bfin_remove
),
579 .name
= "musb-blackfin",
584 MODULE_DESCRIPTION("Blackfin MUSB Glue Layer");
585 MODULE_AUTHOR("Bryan Wy <cooloney@kernel.org>");
586 MODULE_LICENSE("GPL v2");
588 static int __init
bfin_init(void)
590 return platform_driver_probe(&bfin_driver
, bfin_probe
);
592 subsys_initcall(bfin_init
);
594 static void __exit
bfin_exit(void)
596 platform_driver_unregister(&bfin_driver
);
598 module_exit(bfin_exit
);