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/slab.h>
15 #include <linux/init.h>
16 #include <linux/list.h>
17 #include <linux/clk.h>
18 #include <linux/gpio.h>
21 #include <asm/cacheflush.h>
23 #include "musb_core.h"
27 * Load an endpoint's FIFO
29 void musb_write_fifo(struct musb_hw_ep *hw_ep, u16 len, const u8 *src)
31 void __iomem *fifo = hw_ep->fifo;
32 void __iomem *epio = hw_ep->regs;
36 musb_writew(epio, MUSB_TXCOUNT, len);
38 DBG(4, "TX ep%d fifo %p count %d buf %p, epio %p\n",
39 hw_ep->epnum, fifo, len, src, epio);
41 dump_fifo_data(src, len);
43 if (unlikely((unsigned long)src & 0x01))
44 outsw_8((unsigned long)fifo, src,
45 len & 0x01 ? (len >> 1) + 1 : len >> 1);
47 outsw((unsigned long)fifo, src,
48 len & 0x01 ? (len >> 1) + 1 : len >> 1);
52 * Unload an endpoint's FIFO
54 void musb_read_fifo(struct musb_hw_ep *hw_ep, u16 len, u8 *dst)
56 void __iomem *fifo = hw_ep->fifo;
57 u8 epnum = hw_ep->epnum;
60 DBG(4, "%cX ep%d fifo %p count %d buf %p\n",
61 'R', hw_ep->epnum, fifo, len, dst);
64 invalidate_dcache_range((unsigned int)dst,
65 (unsigned int)(dst + len));
67 /* Setup DMA address register */
68 dma_reg = (u16) ((u32) dst & 0xFFFF);
69 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_ADDR_LOW), dma_reg);
72 dma_reg = (u16) (((u32) dst >> 16) & 0xFFFF);
73 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_ADDR_HIGH), dma_reg);
76 /* Setup DMA count register */
77 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_COUNT_LOW), len);
78 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_COUNT_HIGH), 0);
82 dma_reg = (epnum << 4) | DMA_ENA | INT_ENA;
83 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_CTRL), dma_reg);
86 /* Wait for compelete */
87 while (!(bfin_read_USB_DMA_INTERRUPT() & (1 << epnum)))
90 /* acknowledge dma interrupt */
91 bfin_write_USB_DMA_INTERRUPT(1 << epnum);
95 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_CTRL), 0);
98 if (unlikely((unsigned long)dst & 0x01))
99 insw_8((unsigned long)fifo, dst,
100 len & 0x01 ? (len >> 1) + 1 : len >> 1);
102 insw((unsigned long)fifo, dst,
103 len & 0x01 ? (len >> 1) + 1 : len >> 1);
106 dump_fifo_data(dst, len);
109 static irqreturn_t blackfin_interrupt(int irq, void *__hci)
112 irqreturn_t retval = IRQ_NONE;
113 struct musb *musb = __hci;
115 spin_lock_irqsave(&musb->lock, flags);
117 musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB);
118 musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX);
119 musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX);
121 if (musb->int_usb || musb->int_tx || musb->int_rx) {
122 musb_writeb(musb->mregs, MUSB_INTRUSB, musb->int_usb);
123 musb_writew(musb->mregs, MUSB_INTRTX, musb->int_tx);
124 musb_writew(musb->mregs, MUSB_INTRRX, musb->int_rx);
125 retval = musb_interrupt(musb);
128 spin_unlock_irqrestore(&musb->lock, flags);
130 /* REVISIT we sometimes get spurious IRQs on g_ep0
131 * not clear why... fall in BF54x too.
133 if (retval != IRQ_HANDLED)
134 DBG(5, "spurious?\n");
139 static void musb_conn_timer_handler(unsigned long _musb)
141 struct musb *musb = (void *)_musb;
145 spin_lock_irqsave(&musb->lock, flags);
146 switch (musb->xceiv.state) {
147 case OTG_STATE_A_IDLE:
148 case OTG_STATE_A_WAIT_BCON:
149 /* Start a new session */
150 val = musb_readw(musb->mregs, MUSB_DEVCTL);
151 val |= MUSB_DEVCTL_SESSION;
152 musb_writew(musb->mregs, MUSB_DEVCTL, val);
154 val = musb_readw(musb->mregs, MUSB_DEVCTL);
155 if (!(val & MUSB_DEVCTL_BDEVICE)) {
156 gpio_set_value(musb->config->gpio_vrsel, 1);
157 musb->xceiv.state = OTG_STATE_A_WAIT_BCON;
159 gpio_set_value(musb->config->gpio_vrsel, 0);
161 /* Ignore VBUSERROR and SUSPEND IRQ */
162 val = musb_readb(musb->mregs, MUSB_INTRUSBE);
163 val &= ~MUSB_INTR_VBUSERROR;
164 musb_writeb(musb->mregs, MUSB_INTRUSBE, val);
166 val = MUSB_INTR_SUSPEND | MUSB_INTR_VBUSERROR;
167 musb_writeb(musb->mregs, MUSB_INTRUSB, val);
169 val = MUSB_POWER_HSENAB;
170 musb_writeb(musb->mregs, MUSB_POWER, val);
172 mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY);
176 DBG(1, "%s state not handled\n", otg_state_string(musb));
179 spin_unlock_irqrestore(&musb->lock, flags);
181 DBG(4, "state is %s\n", otg_state_string(musb));
184 void musb_platform_enable(struct musb *musb)
186 if (is_host_enabled(musb)) {
187 mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY);
188 musb->a_wait_bcon = TIMER_DELAY;
192 void musb_platform_disable(struct musb *musb)
196 static void bfin_vbus_power(struct musb *musb, int is_on, int sleeping)
200 static void bfin_set_vbus(struct musb *musb, int is_on)
203 gpio_set_value(musb->config->gpio_vrsel, 1);
205 gpio_set_value(musb->config->gpio_vrsel, 0);
207 DBG(1, "VBUS %s, devctl %02x "
208 /* otg %3x conf %08x prcm %08x */ "\n",
209 otg_state_string(musb),
210 musb_readb(musb->mregs, MUSB_DEVCTL));
213 static int bfin_set_power(struct otg_transceiver *x, unsigned mA)
218 void musb_platform_try_idle(struct musb *musb, unsigned long timeout)
220 if (is_host_enabled(musb))
221 mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY);
224 int musb_platform_get_vbus_status(struct musb *musb)
229 void musb_platform_set_mode(struct musb *musb, u8 musb_mode)
233 int __init musb_platform_init(struct musb *musb)
237 * Rev 1.0 BF549 EZ-KITs require PE7 to be high for both DEVICE
238 * and OTG HOST modes, while rev 1.1 and greater require PE7 to
239 * be low for DEVICE mode and high for HOST mode. We set it high
240 * here because we are in host mode
243 if (gpio_request(musb->config->gpio_vrsel, "USB_VRSEL")) {
244 printk(KERN_ERR "Failed ro request USB_VRSEL GPIO_%d \n",
245 musb->config->gpio_vrsel);
248 gpio_direction_output(musb->config->gpio_vrsel, 0);
250 if (ANOMALY_05000346) {
251 bfin_write_USB_APHY_CALIB(ANOMALY_05000346_value);
255 if (ANOMALY_05000347) {
256 bfin_write_USB_APHY_CNTRL(0x0);
261 * Set SIC-IVG register
264 /* Configure PLL oscillator register */
265 bfin_write_USB_PLLOSC_CTRL(0x30a8);
268 bfin_write_USB_SRP_CLKDIV((get_sclk()/1000) / 32 - 1);
271 bfin_write_USB_EP_NI0_RXMAXP(64);
274 bfin_write_USB_EP_NI0_TXMAXP(64);
277 /* Route INTRUSB/INTR_RX/INTR_TX to USB_INT0*/
278 bfin_write_USB_GLOBINTR(0x7);
281 bfin_write_USB_GLOBAL_CTL(GLOBAL_ENA | EP1_TX_ENA | EP2_TX_ENA |
282 EP3_TX_ENA | EP4_TX_ENA | EP5_TX_ENA |
283 EP6_TX_ENA | EP7_TX_ENA | EP1_RX_ENA |
284 EP2_RX_ENA | EP3_RX_ENA | EP4_RX_ENA |
285 EP5_RX_ENA | EP6_RX_ENA | EP7_RX_ENA);
288 if (is_host_enabled(musb)) {
289 musb->board_set_vbus = bfin_set_vbus;
290 setup_timer(&musb_conn_timer,
291 musb_conn_timer_handler, (unsigned long) musb);
293 if (is_peripheral_enabled(musb))
294 musb->xceiv.set_power = bfin_set_power;
296 musb->isr = blackfin_interrupt;
301 int musb_platform_suspend(struct musb *musb)
306 int musb_platform_resume(struct musb *musb)
312 int musb_platform_exit(struct musb *musb)
315 bfin_vbus_power(musb, 0 /*off*/, 1);
316 gpio_free(musb->config->gpio_vrsel);
317 musb_platform_suspend(musb);