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"
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
;
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) {
53 flush_dcache_range((unsigned long)src
,
54 (unsigned long)(src
+ len
));
56 /* Setup DMA address register */
58 bfin_write16(USB_DMA_REG(epnum
, USB_DMAx_ADDR_LOW
), dma_reg
);
61 dma_reg
= (u32
)src
>> 16;
62 bfin_write16(USB_DMA_REG(epnum
, USB_DMAx_ADDR_HIGH
), dma_reg
);
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);
71 dma_reg
= (epnum
<< 4) | DMA_ENA
| INT_ENA
| DIRECTION
;
72 bfin_write16(USB_DMA_REG(epnum
, USB_DMAx_CTRL
), dma_reg
);
75 /* Wait for compelete */
76 while (!(bfin_read_USB_DMA_INTERRUPT() & (1 << epnum
)))
79 /* acknowledge dma interrupt */
80 bfin_write_USB_DMA_INTERRUPT(1 << epnum
);
84 bfin_write16(USB_DMA_REG(epnum
, USB_DMAx_CTRL
), 0);
89 if (unlikely((unsigned long)src
& 0x01))
90 outsw_8((unsigned long)fifo
, src
, (len
+ 1) >> 1);
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) {
106 invalidate_dcache_range((unsigned long)dst
,
107 (unsigned long)(dst
+ len
));
109 /* Setup DMA address register */
111 bfin_write16(USB_DMA_REG(epnum
, USB_DMAx_ADDR_LOW
), dma_reg
);
114 dma_reg
= (u32
)dst
>> 16;
115 bfin_write16(USB_DMA_REG(epnum
, USB_DMAx_ADDR_HIGH
), dma_reg
);
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);
124 dma_reg
= (epnum
<< 4) | DMA_ENA
| INT_ENA
;
125 bfin_write16(USB_DMA_REG(epnum
, USB_DMAx_CTRL
), dma_reg
);
128 /* Wait for compelete */
129 while (!(bfin_read_USB_DMA_INTERRUPT() & (1 << epnum
)))
132 /* acknowledge dma interrupt */
133 bfin_write_USB_DMA_INTERRUPT(1 << epnum
);
137 bfin_write16(USB_DMA_REG(epnum
, USB_DMAx_CTRL
), 0);
141 /* Read the last byte of packet with odd size from address fifo + 4
142 * to trigger 1 byte access to EP0 FIFO.
145 *dst
= (u8
)inw((unsigned long)fifo
+ 4);
147 if (unlikely((unsigned long)dst
& 0x01))
148 insw_8((unsigned long)fifo
, dst
, len
>> 1);
150 insw((unsigned long)fifo
, dst
, len
>> 1);
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
)
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
);
194 static void musb_conn_timer_handler(unsigned long _musb
)
196 struct musb
*musb
= (void *)_musb
;
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
;
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
;
229 musb_writeb(musb
->mregs
, MUSB_POWER
, MUSB_POWER_HSENAB
);
231 mod_timer(&musb_conn_timer
, jiffies
+ TIMER_DELAY
);
233 case OTG_STATE_B_IDLE
:
235 if (!is_peripheral_enabled(musb
))
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
;
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.
263 val
= musb_readb(musb
->mregs
, MUSB_POWER
);
264 val
&= ~MUSB_POWER_SOFTCONN
;
265 musb_writeb(musb
->mregs
, MUSB_POWER
, val
);
268 val
= musb_readb(musb
->mregs
, MUSB_POWER
);
269 val
|= MUSB_POWER_SOFTCONN
;
270 musb_writeb(musb
->mregs
, MUSB_POWER
, val
);
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);
281 DBG(1, "%s state not handled\n", otg_state_string(musb
));
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
;
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
)
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_vbus_status(struct musb
*musb
)
330 static int bfin_musb_set_mode(struct musb
*musb
, u8 musb_mode
)
335 static void bfin_musb_reg_init(struct musb
*musb
)
337 if (ANOMALY_05000346
) {
338 bfin_write_USB_APHY_CALIB(ANOMALY_05000346_value
);
342 if (ANOMALY_05000347
) {
343 bfin_write_USB_APHY_CNTRL(0x0);
347 /* Configure PLL oscillator register */
348 bfin_write_USB_PLLOSC_CTRL(0x3080 |
349 ((480/musb
->config
->clkin
) << 1));
352 bfin_write_USB_SRP_CLKDIV((get_sclk()/1000) / 32 - 1);
355 bfin_write_USB_EP_NI0_RXMAXP(64);
358 bfin_write_USB_EP_NI0_TXMAXP(64);
361 /* Route INTRUSB/INTR_RX/INTR_TX to USB_INT0*/
362 bfin_write_USB_GLOBINTR(0x7);
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
);
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
);
388 gpio_direction_output(musb
->config
->gpio_vrsel
, 0);
390 usb_nop_xceiv_register();
391 musb
->xceiv
= otg_get_transceiver();
393 gpio_free(musb
->config
->gpio_vrsel
);
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
;
407 musb
->double_buffer_not_ok
= true;
412 static int bfin_musb_exit(struct musb
*musb
)
414 gpio_free(musb
->config
->gpio_vrsel
);
416 otg_put_transceiver(musb
->xceiv
);
417 usb_nop_xceiv_unregister();
421 static const struct musb_platform_ops bfin_ops
= {
422 .init
= bfin_musb_init
,
423 .exit
= bfin_musb_exit
,
425 .enable
= bfin_musb_enable
,
426 .disable
= bfin_musb_disable
,
428 .set_mode
= bfin_musb_set_mode
,
429 .try_idle
= bfin_musb_try_idle
,
431 .vbus_status
= bfin_musb_vbus_status
,
432 .set_vbus
= bfin_musb_set_vbus
,
435 static u64 bfin_dmamask
= DMA_BIT_MASK(32);
437 static int __init
bfin_probe(struct platform_device
*pdev
)
439 struct musb_hdrc_platform_data
*pdata
= pdev
->dev
.platform_data
;
440 struct platform_device
*musb
;
441 struct bfin_glue
*glue
;
445 glue
= kzalloc(sizeof(*glue
), GFP_KERNEL
);
447 dev_err(&pdev
->dev
, "failed to allocate glue context\n");
451 musb
= platform_device_alloc("musb-hdrc", -1);
453 dev_err(&pdev
->dev
, "failed to allocate musb device\n");
457 musb
->dev
.parent
= &pdev
->dev
;
458 musb
->dev
.dma_mask
= &bfin_dmamask
;
459 musb
->dev
.coherent_dma_mask
= bfin_dmamask
;
461 glue
->dev
= &pdev
->dev
;
464 pdata
->platform_ops
= &bfin_ops
;
466 platform_set_drvdata(pdev
, glue
);
468 ret
= platform_device_add_resources(musb
, pdev
->resource
,
469 pdev
->num_resources
);
471 dev_err(&pdev
->dev
, "failed to add resources\n");
475 ret
= platform_device_add_data(musb
, pdata
, sizeof(*pdata
));
477 dev_err(&pdev
->dev
, "failed to add platform_data\n");
481 ret
= platform_device_add(musb
);
483 dev_err(&pdev
->dev
, "failed to register musb device\n");
490 platform_device_put(musb
);
499 static int __exit
bfin_remove(struct platform_device
*pdev
)
501 struct bfin_glue
*glue
= platform_get_drvdata(pdev
);
503 platform_device_del(glue
->musb
);
504 platform_device_put(glue
->musb
);
511 static int bfin_suspend(struct device
*dev
)
513 struct bfin_glue
*glue
= dev_get_drvdata(dev
);
514 struct musb
*musb
= glue_to_musb(glue
);
516 if (is_host_active(musb
))
518 * During hibernate gpio_vrsel will change from high to low
519 * low which will generate wakeup event resume the system
520 * immediately. Set it to 0 before hibernate to avoid this
523 gpio_set_value(musb
->config
->gpio_vrsel
, 0);
528 static int bfin_resume(struct device
*dev
)
530 struct bfin_glue
*glue
= dev_get_drvdata(dev
);
531 struct musb
*musb
= glue_to_musb(glue
);
533 bfin_musb_reg_init(musb
);
538 static struct dev_pm_ops bfin_pm_ops
= {
539 .suspend
= bfin_suspend
,
540 .resume
= bfin_resume
,
543 #define DEV_PM_OPS &bfin_pm_ops
545 #define DEV_PM_OPS NULL
548 static struct platform_driver bfin_driver
= {
549 .remove
= __exit_p(bfin_remove
),
551 .name
= "musb-blackfin",
556 MODULE_DESCRIPTION("Blackfin MUSB Glue Layer");
557 MODULE_AUTHOR("Bryan Wy <cooloney@kernel.org>");
558 MODULE_LICENSE("GPL v2");
560 static int __init
bfin_init(void)
562 return platform_driver_probe(&bfin_driver
, bfin_probe
);
564 subsys_initcall(bfin_init
);
566 static void __exit
bfin_exit(void)
568 platform_driver_unregister(&bfin_driver
);
570 module_exit(bfin_exit
);