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/list.h>
15 #include <linux/gpio.h>
17 #include <linux/err.h>
18 #include <linux/platform_device.h>
19 #include <linux/dma-mapping.h>
20 #include <linux/prefetch.h>
21 #include <linux/usb/usb_phy_gen_xceiv.h>
23 #include <asm/cacheflush.h>
25 #include "musb_core.h"
26 #include "musbhsdma.h"
31 struct platform_device
*musb
;
33 #define glue_to_musb(g) platform_get_drvdata(g->musb)
36 * Load an endpoint's FIFO
38 void musb_write_fifo(struct musb_hw_ep
*hw_ep
, u16 len
, const u8
*src
)
40 struct musb
*musb
= hw_ep
->musb
;
41 void __iomem
*fifo
= hw_ep
->fifo
;
42 void __iomem
*epio
= hw_ep
->regs
;
43 u8 epnum
= hw_ep
->epnum
;
47 musb_writew(epio
, MUSB_TXCOUNT
, len
);
49 dev_dbg(musb
->controller
, "TX ep%d fifo %p count %d buf %p, epio %p\n",
50 hw_ep
->epnum
, fifo
, len
, src
, epio
);
52 dump_fifo_data(src
, len
);
54 if (!ANOMALY_05000380
&& epnum
!= 0) {
57 flush_dcache_range((unsigned long)src
,
58 (unsigned long)(src
+ len
));
60 /* Setup DMA address register */
62 bfin_write16(USB_DMA_REG(epnum
, USB_DMAx_ADDR_LOW
), dma_reg
);
65 dma_reg
= (u32
)src
>> 16;
66 bfin_write16(USB_DMA_REG(epnum
, USB_DMAx_ADDR_HIGH
), dma_reg
);
69 /* Setup DMA count register */
70 bfin_write16(USB_DMA_REG(epnum
, USB_DMAx_COUNT_LOW
), len
);
71 bfin_write16(USB_DMA_REG(epnum
, USB_DMAx_COUNT_HIGH
), 0);
75 dma_reg
= (epnum
<< 4) | DMA_ENA
| INT_ENA
| DIRECTION
;
76 bfin_write16(USB_DMA_REG(epnum
, USB_DMAx_CTRL
), dma_reg
);
79 /* Wait for complete */
80 while (!(bfin_read_USB_DMA_INTERRUPT() & (1 << epnum
)))
83 /* acknowledge dma interrupt */
84 bfin_write_USB_DMA_INTERRUPT(1 << epnum
);
88 bfin_write16(USB_DMA_REG(epnum
, USB_DMAx_CTRL
), 0);
93 if (unlikely((unsigned long)src
& 0x01))
94 outsw_8((unsigned long)fifo
, src
, (len
+ 1) >> 1);
96 outsw((unsigned long)fifo
, src
, (len
+ 1) >> 1);
100 * Unload an endpoint's FIFO
102 void musb_read_fifo(struct musb_hw_ep
*hw_ep
, u16 len
, u8
*dst
)
104 struct musb
*musb
= hw_ep
->musb
;
105 void __iomem
*fifo
= hw_ep
->fifo
;
106 u8 epnum
= hw_ep
->epnum
;
108 if (ANOMALY_05000467
&& epnum
!= 0) {
111 invalidate_dcache_range((unsigned long)dst
,
112 (unsigned long)(dst
+ len
));
114 /* Setup DMA address register */
116 bfin_write16(USB_DMA_REG(epnum
, USB_DMAx_ADDR_LOW
), dma_reg
);
119 dma_reg
= (u32
)dst
>> 16;
120 bfin_write16(USB_DMA_REG(epnum
, USB_DMAx_ADDR_HIGH
), dma_reg
);
123 /* Setup DMA count register */
124 bfin_write16(USB_DMA_REG(epnum
, USB_DMAx_COUNT_LOW
), len
);
125 bfin_write16(USB_DMA_REG(epnum
, USB_DMAx_COUNT_HIGH
), 0);
129 dma_reg
= (epnum
<< 4) | DMA_ENA
| INT_ENA
;
130 bfin_write16(USB_DMA_REG(epnum
, USB_DMAx_CTRL
), dma_reg
);
133 /* Wait for complete */
134 while (!(bfin_read_USB_DMA_INTERRUPT() & (1 << epnum
)))
137 /* acknowledge dma interrupt */
138 bfin_write_USB_DMA_INTERRUPT(1 << epnum
);
142 bfin_write16(USB_DMA_REG(epnum
, USB_DMAx_CTRL
), 0);
146 /* Read the last byte of packet with odd size from address fifo + 4
147 * to trigger 1 byte access to EP0 FIFO.
150 *dst
= (u8
)inw((unsigned long)fifo
+ 4);
152 if (unlikely((unsigned long)dst
& 0x01))
153 insw_8((unsigned long)fifo
, dst
, len
>> 1);
155 insw((unsigned long)fifo
, dst
, len
>> 1);
158 *(dst
+ len
- 1) = (u8
)inw((unsigned long)fifo
+ 4);
161 dev_dbg(musb
->controller
, "%cX ep%d fifo %p count %d buf %p\n",
162 'R', hw_ep
->epnum
, fifo
, len
, dst
);
164 dump_fifo_data(dst
, len
);
167 static irqreturn_t
blackfin_interrupt(int irq
, void *__hci
)
170 irqreturn_t retval
= IRQ_NONE
;
171 struct musb
*musb
= __hci
;
173 spin_lock_irqsave(&musb
->lock
, flags
);
175 musb
->int_usb
= musb_readb(musb
->mregs
, MUSB_INTRUSB
);
176 musb
->int_tx
= musb_readw(musb
->mregs
, MUSB_INTRTX
);
177 musb
->int_rx
= musb_readw(musb
->mregs
, MUSB_INTRRX
);
179 if (musb
->int_usb
|| musb
->int_tx
|| musb
->int_rx
) {
180 musb_writeb(musb
->mregs
, MUSB_INTRUSB
, musb
->int_usb
);
181 musb_writew(musb
->mregs
, MUSB_INTRTX
, musb
->int_tx
);
182 musb_writew(musb
->mregs
, MUSB_INTRRX
, musb
->int_rx
);
183 retval
= musb_interrupt(musb
);
186 /* Start sampling ID pin, when plug is removed from MUSB */
187 if ((musb
->xceiv
->state
== OTG_STATE_B_IDLE
188 || musb
->xceiv
->state
== OTG_STATE_A_WAIT_BCON
) ||
189 (musb
->int_usb
& MUSB_INTR_DISCONNECT
&& is_host_active(musb
))) {
190 mod_timer(&musb_conn_timer
, jiffies
+ TIMER_DELAY
);
191 musb
->a_wait_bcon
= TIMER_DELAY
;
194 spin_unlock_irqrestore(&musb
->lock
, flags
);
199 static void musb_conn_timer_handler(unsigned long _musb
)
201 struct musb
*musb
= (void *)_musb
;
206 spin_lock_irqsave(&musb
->lock
, flags
);
207 switch (musb
->xceiv
->state
) {
208 case OTG_STATE_A_IDLE
:
209 case OTG_STATE_A_WAIT_BCON
:
210 /* Start a new session */
211 val
= musb_readw(musb
->mregs
, MUSB_DEVCTL
);
212 val
&= ~MUSB_DEVCTL_SESSION
;
213 musb_writew(musb
->mregs
, MUSB_DEVCTL
, val
);
214 val
|= MUSB_DEVCTL_SESSION
;
215 musb_writew(musb
->mregs
, MUSB_DEVCTL
, val
);
216 /* Check if musb is host or peripheral. */
217 val
= musb_readw(musb
->mregs
, MUSB_DEVCTL
);
219 if (!(val
& MUSB_DEVCTL_BDEVICE
)) {
220 gpio_set_value(musb
->config
->gpio_vrsel
, 1);
221 musb
->xceiv
->state
= OTG_STATE_A_WAIT_BCON
;
223 gpio_set_value(musb
->config
->gpio_vrsel
, 0);
224 /* Ignore VBUSERROR and SUSPEND IRQ */
225 val
= musb_readb(musb
->mregs
, MUSB_INTRUSBE
);
226 val
&= ~MUSB_INTR_VBUSERROR
;
227 musb_writeb(musb
->mregs
, MUSB_INTRUSBE
, val
);
229 val
= MUSB_INTR_SUSPEND
| MUSB_INTR_VBUSERROR
;
230 musb_writeb(musb
->mregs
, MUSB_INTRUSB
, val
);
231 musb
->xceiv
->state
= OTG_STATE_B_IDLE
;
233 mod_timer(&musb_conn_timer
, jiffies
+ TIMER_DELAY
);
235 case OTG_STATE_B_IDLE
:
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 dev_dbg(musb
->controller
, "%s state not handled\n",
282 usb_otg_state_string(musb
->xceiv
->state
));
285 spin_unlock_irqrestore(&musb
->lock
, flags
);
287 dev_dbg(musb
->controller
, "state is %s\n",
288 usb_otg_state_string(musb
->xceiv
->state
));
291 static void bfin_musb_enable(struct musb
*musb
)
293 /* REVISIT is this really correct ? */
296 static void bfin_musb_disable(struct musb
*musb
)
300 static void bfin_musb_set_vbus(struct musb
*musb
, int is_on
)
302 int value
= musb
->config
->gpio_vrsel_active
;
305 gpio_set_value(musb
->config
->gpio_vrsel
, value
);
307 dev_dbg(musb
->controller
, "VBUS %s, devctl %02x "
308 /* otg %3x conf %08x prcm %08x */ "\n",
309 usb_otg_state_string(musb
->xceiv
->state
),
310 musb_readb(musb
->mregs
, MUSB_DEVCTL
));
313 static int bfin_musb_set_power(struct usb_phy
*x
, unsigned mA
)
318 static int bfin_musb_vbus_status(struct musb
*musb
)
323 static int bfin_musb_set_mode(struct musb
*musb
, u8 musb_mode
)
328 static int bfin_musb_adjust_channel_params(struct dma_channel
*channel
,
329 u16 packet_sz
, u8
*mode
,
330 dma_addr_t
*dma_addr
, u32
*len
)
332 struct musb_dma_channel
*musb_channel
= channel
->private_data
;
335 * Anomaly 05000450 might cause data corruption when using DMA
336 * MODE 1 transmits with short packet. So to work around this,
337 * we truncate all MODE 1 transfers down to a multiple of the
338 * max packet size, and then do the last short packet transfer
339 * (if there is any) using MODE 0.
341 if (ANOMALY_05000450
) {
342 if (musb_channel
->transmit
&& *mode
== 1)
343 *len
= *len
- (*len
% packet_sz
);
349 static void bfin_musb_reg_init(struct musb
*musb
)
351 if (ANOMALY_05000346
) {
352 bfin_write_USB_APHY_CALIB(ANOMALY_05000346_value
);
356 if (ANOMALY_05000347
) {
357 bfin_write_USB_APHY_CNTRL(0x0);
361 /* Configure PLL oscillator register */
362 bfin_write_USB_PLLOSC_CTRL(0x3080 |
363 ((480/musb
->config
->clkin
) << 1));
366 bfin_write_USB_SRP_CLKDIV((get_sclk()/1000) / 32 - 1);
369 bfin_write_USB_EP_NI0_RXMAXP(64);
372 bfin_write_USB_EP_NI0_TXMAXP(64);
375 /* Route INTRUSB/INTR_RX/INTR_TX to USB_INT0*/
376 bfin_write_USB_GLOBINTR(0x7);
379 bfin_write_USB_GLOBAL_CTL(GLOBAL_ENA
| EP1_TX_ENA
| EP2_TX_ENA
|
380 EP3_TX_ENA
| EP4_TX_ENA
| EP5_TX_ENA
|
381 EP6_TX_ENA
| EP7_TX_ENA
| EP1_RX_ENA
|
382 EP2_RX_ENA
| EP3_RX_ENA
| EP4_RX_ENA
|
383 EP5_RX_ENA
| EP6_RX_ENA
| EP7_RX_ENA
);
387 static int bfin_musb_init(struct musb
*musb
)
391 * Rev 1.0 BF549 EZ-KITs require PE7 to be high for both DEVICE
392 * and OTG HOST modes, while rev 1.1 and greater require PE7 to
393 * be low for DEVICE mode and high for HOST mode. We set it high
394 * here because we are in host mode
397 if (gpio_request(musb
->config
->gpio_vrsel
, "USB_VRSEL")) {
398 printk(KERN_ERR
"Failed ro request USB_VRSEL GPIO_%d\n",
399 musb
->config
->gpio_vrsel
);
402 gpio_direction_output(musb
->config
->gpio_vrsel
, 0);
404 usb_nop_xceiv_register();
405 musb
->xceiv
= usb_get_phy(USB_PHY_TYPE_USB2
);
406 if (IS_ERR_OR_NULL(musb
->xceiv
)) {
407 gpio_free(musb
->config
->gpio_vrsel
);
408 return -EPROBE_DEFER
;
411 bfin_musb_reg_init(musb
);
413 setup_timer(&musb_conn_timer
, musb_conn_timer_handler
,
414 (unsigned long) musb
);
416 musb
->xceiv
->set_power
= bfin_musb_set_power
;
418 musb
->isr
= blackfin_interrupt
;
419 musb
->double_buffer_not_ok
= true;
424 static int bfin_musb_exit(struct musb
*musb
)
426 gpio_free(musb
->config
->gpio_vrsel
);
428 usb_put_phy(musb
->xceiv
);
429 usb_nop_xceiv_unregister();
433 static const struct musb_platform_ops bfin_ops
= {
434 .init
= bfin_musb_init
,
435 .exit
= bfin_musb_exit
,
437 .enable
= bfin_musb_enable
,
438 .disable
= bfin_musb_disable
,
440 .set_mode
= bfin_musb_set_mode
,
442 .vbus_status
= bfin_musb_vbus_status
,
443 .set_vbus
= bfin_musb_set_vbus
,
445 .adjust_channel_params
= bfin_musb_adjust_channel_params
,
448 static u64 bfin_dmamask
= DMA_BIT_MASK(32);
450 static int bfin_probe(struct platform_device
*pdev
)
452 struct resource musb_resources
[2];
453 struct musb_hdrc_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
454 struct platform_device
*musb
;
455 struct bfin_glue
*glue
;
459 glue
= kzalloc(sizeof(*glue
), GFP_KERNEL
);
461 dev_err(&pdev
->dev
, "failed to allocate glue context\n");
465 musb
= platform_device_alloc("musb-hdrc", PLATFORM_DEVID_AUTO
);
467 dev_err(&pdev
->dev
, "failed to allocate musb device\n");
471 musb
->dev
.parent
= &pdev
->dev
;
472 musb
->dev
.dma_mask
= &bfin_dmamask
;
473 musb
->dev
.coherent_dma_mask
= bfin_dmamask
;
475 glue
->dev
= &pdev
->dev
;
478 pdata
->platform_ops
= &bfin_ops
;
480 platform_set_drvdata(pdev
, glue
);
482 memset(musb_resources
, 0x00, sizeof(*musb_resources
) *
483 ARRAY_SIZE(musb_resources
));
485 musb_resources
[0].name
= pdev
->resource
[0].name
;
486 musb_resources
[0].start
= pdev
->resource
[0].start
;
487 musb_resources
[0].end
= pdev
->resource
[0].end
;
488 musb_resources
[0].flags
= pdev
->resource
[0].flags
;
490 musb_resources
[1].name
= pdev
->resource
[1].name
;
491 musb_resources
[1].start
= pdev
->resource
[1].start
;
492 musb_resources
[1].end
= pdev
->resource
[1].end
;
493 musb_resources
[1].flags
= pdev
->resource
[1].flags
;
495 ret
= platform_device_add_resources(musb
, musb_resources
,
496 ARRAY_SIZE(musb_resources
));
498 dev_err(&pdev
->dev
, "failed to add resources\n");
502 ret
= platform_device_add_data(musb
, pdata
, sizeof(*pdata
));
504 dev_err(&pdev
->dev
, "failed to add platform_data\n");
508 ret
= platform_device_add(musb
);
510 dev_err(&pdev
->dev
, "failed to register musb device\n");
517 platform_device_put(musb
);
526 static int bfin_remove(struct platform_device
*pdev
)
528 struct bfin_glue
*glue
= platform_get_drvdata(pdev
);
530 platform_device_unregister(glue
->musb
);
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
549 gpio_set_value(musb
->config
->gpio_vrsel
, 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
);
565 static SIMPLE_DEV_PM_OPS(bfin_pm_ops
, bfin_suspend
, bfin_resume
);
567 static struct platform_driver bfin_driver
= {
569 .remove
= __exit_p(bfin_remove
),
571 .name
= "musb-blackfin",
576 MODULE_DESCRIPTION("Blackfin MUSB Glue Layer");
577 MODULE_AUTHOR("Bryan Wy <cooloney@kernel.org>");
578 MODULE_LICENSE("GPL v2");
579 module_platform_driver(bfin_driver
);