blob: eed35c933bc2920c3edd30e26af9eb1cc477c212 [file] [log] [blame]
Bryan Wu0c6a8812008-12-02 21:33:44 +02001/*
2 * MUSB OTG controller driver for Blackfin Processors
3 *
4 * Copyright 2006-2008 Analog Devices Inc.
5 *
6 * Enter bugs at http://blackfin.uclinux.org/
7 *
8 * Licensed under the GPL-2 or later.
9 */
10
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>
19#include <linux/io.h>
20
21#include <asm/cacheflush.h>
22
23#include "musb_core.h"
24#include "blackfin.h"
25
26/*
27 * Load an endpoint's FIFO
28 */
29void musb_write_fifo(struct musb_hw_ep *hw_ep, u16 len, const u8 *src)
30{
31 void __iomem *fifo = hw_ep->fifo;
32 void __iomem *epio = hw_ep->regs;
33
34 prefetch((u8 *)src);
35
36 musb_writew(epio, MUSB_TXCOUNT, len);
37
38 DBG(4, "TX ep%d fifo %p count %d buf %p, epio %p\n",
39 hw_ep->epnum, fifo, len, src, epio);
40
41 dump_fifo_data(src, len);
42
43 if (unlikely((unsigned long)src & 0x01))
44 outsw_8(fifo, src, len & 0x01 ? (len >> 1) + 1 : len >> 1);
45 else
46 outsw(fifo, src, len & 0x01 ? (len >> 1) + 1 : len >> 1);
47}
48
49/*
50 * Unload an endpoint's FIFO
51 */
52void musb_read_fifo(struct musb_hw_ep *hw_ep, u16 len, u8 *dst)
53{
54 void __iomem *fifo = hw_ep->fifo;
55 u8 epnum = hw_ep->epnum;
56 u16 dma_reg = 0;
57 int i;
58 u16 *data;
59
60 DBG(4, "%cX ep%d fifo %p count %d buf %p\n",
61 'R', hw_ep->epnum, fifo, len, dst);
62
63#ifdef CONFIG_BF52x
64 invalidate_dcache_range((unsigned int)dst,
65 (unsigned int)(dst + len));
66
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);
70 SSYNC();
71
72 dma_reg = (u16) (((u32) dst >> 16) & 0xFFFF);
73 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_ADDR_HIGH), dma_reg);
74 SSYNC();
75
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);
79 SSYNC();
80
81 /* Enable the DMA */
82 dma_reg = (epnum << 4) | DMA_ENA | INT_ENA;
83 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_CTRL), dma_reg);
84 SSYNC();
85
86 /* Wait for compelete */
87 while (!(bfin_read_USB_DMA_INTERRUPT() & (1 << epnum)))
88 cpu_relax();
89
90 /* acknowledge dma interrupt */
91 bfin_write_USB_DMA_INTERRUPT(1 << epnum);
92 SSYNC();
93
94 /* Reset DMA */
95 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_CTRL), 0);
96 SSYNC();
97#else
98 if (unlikely((unsigned long)dst & 0x01))
99 insw_8(fifo, dst, len & 0x01 ? (len >> 1) + 1 : len >> 1);
100 else
101 insw(fifo, dst, len & 0x01 ? (len >> 1) + 1 : len >> 1);
102#endif
103
104 dump_fifo_data(dst, len);
105}
106
107static irqreturn_t blackfin_interrupt(int irq, void *__hci)
108{
109 unsigned long flags;
110 irqreturn_t retval = IRQ_NONE;
111 struct musb *musb = __hci;
112
113 spin_lock_irqsave(&musb->lock, flags);
114
115 musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB);
116 musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX);
117 musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX);
118
119 if (musb->int_usb || musb->int_tx || musb->int_rx) {
120 musb_writeb(musb->mregs, MUSB_INTRUSB, musb->int_usb);
121 musb_writew(musb->mregs, MUSB_INTRTX, musb->int_tx);
122 musb_writew(musb->mregs, MUSB_INTRRX, musb->int_rx);
123 retval = musb_interrupt(musb);
124 }
125
126 spin_unlock_irqrestore(&musb->lock, flags);
127
128 /* REVISIT we sometimes get spurious IRQs on g_ep0
129 * not clear why... fall in BF54x too.
130 */
131 if (retval != IRQ_HANDLED)
132 DBG(5, "spurious?\n");
133
134 return IRQ_HANDLED;
135}
136
137static void musb_conn_timer_handler(unsigned long _musb)
138{
139 struct musb *musb = (void *)_musb;
140 unsigned long flags;
141 u16 val;
142
143 spin_lock_irqsave(&musb->lock, flags);
144 switch (musb->xceiv.state) {
145 case OTG_STATE_A_IDLE:
146 case OTG_STATE_A_WAIT_BCON:
147 /* Start a new session */
148 val = musb_readw(musb->mregs, MUSB_DEVCTL);
149 val |= MUSB_DEVCTL_SESSION;
150 musb_writew(musb->mregs, MUSB_DEVCTL, val);
151
152 val = musb_readw(musb->mregs, MUSB_DEVCTL);
153 if (!(val & MUSB_DEVCTL_BDEVICE)) {
154 gpio_set_value(musb->config->gpio_vrsel, 1);
155 musb->xceiv.state = OTG_STATE_A_WAIT_BCON;
156 } else {
157 gpio_set_value(musb->config->gpio_vrsel, 0);
158
159 /* Ignore VBUSERROR and SUSPEND IRQ */
160 val = musb_readb(musb->mregs, MUSB_INTRUSBE);
161 val &= ~MUSB_INTR_VBUSERROR;
162 musb_writeb(musb->mregs, MUSB_INTRUSBE, val);
163
164 val = MUSB_INTR_SUSPEND | MUSB_INTR_VBUSERROR;
165 musb_writeb(musb->mregs, MUSB_INTRUSB, val);
166
167 val = MUSB_POWER_HSENAB;
168 musb_writeb(musb->mregs, MUSB_POWER, val);
169 }
170 mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY);
171 break;
172
173 default:
174 DBG(1, "%s state not handled\n", otg_state_string(musb));
175 break;
176 }
177 spin_unlock_irqrestore(&musb->lock, flags);
178
179 DBG(4, "state is %s\n", otg_state_string(musb));
180}
181
182void musb_platform_enable(struct musb *musb)
183{
184 if (is_host_enabled(musb)) {
185 mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY);
186 musb->a_wait_bcon = TIMER_DELAY;
187 }
188}
189
190void musb_platform_disable(struct musb *musb)
191{
192}
193
194static void bfin_vbus_power(struct musb *musb, int is_on, int sleeping)
195{
196}
197
198static void bfin_set_vbus(struct musb *musb, int is_on)
199{
200 if (is_on)
201 gpio_set_value(musb->config->gpio_vrsel, 1);
202 else
203 gpio_set_value(musb->config->gpio_vrsel, 0);
204
205 DBG(1, "VBUS %s, devctl %02x "
206 /* otg %3x conf %08x prcm %08x */ "\n",
207 otg_state_string(musb),
208 musb_readb(musb->mregs, MUSB_DEVCTL));
209}
210
211static int bfin_set_power(struct otg_transceiver *x, unsigned mA)
212{
213 return 0;
214}
215
216void musb_platform_try_idle(struct musb *musb, unsigned long timeout)
217{
218 if (is_host_enabled(musb))
219 mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY);
220}
221
222int musb_platform_get_vbus_status(struct musb *musb)
223{
224 return 0;
225}
226
227void musb_platform_set_mode(struct musb *musb, u8 musb_mode)
228{
229}
230
231int __init musb_platform_init(struct musb *musb)
232{
233
234 /*
235 * Rev 1.0 BF549 EZ-KITs require PE7 to be high for both DEVICE
236 * and OTG HOST modes, while rev 1.1 and greater require PE7 to
237 * be low for DEVICE mode and high for HOST mode. We set it high
238 * here because we are in host mode
239 */
240
241 if (gpio_request(musb->config->gpio_vrsel, "USB_VRSEL")) {
242 printk(KERN_ERR "Failed ro request USB_VRSEL GPIO_%d \n",
243 musb->config->gpio_vrsel);
244 return -ENODEV;
245 }
246 gpio_direction_output(musb->config->gpio_vrsel, 0);
247
Robin Getzd426e602008-12-02 21:33:45 +0200248 if (ANOMALY_05000346) {
249 bfin_write_USB_APHY_CALIB(ANOMALY_05000346_value);
250 SSYNC();
251 }
Bryan Wu0c6a8812008-12-02 21:33:44 +0200252
Robin Getzd426e602008-12-02 21:33:45 +0200253 if (ANOMALY_05000347) {
254 bfin_write_USB_APHY_CNTRL(0x0);
255 SSYNC();
256 }
Bryan Wu0c6a8812008-12-02 21:33:44 +0200257
258 /* TODO
259 * Set SIC-IVG register
260 */
261
262 /* Configure PLL oscillator register */
263 bfin_write_USB_PLLOSC_CTRL(0x30a8);
264 SSYNC();
265
266 bfin_write_USB_SRP_CLKDIV((get_sclk()/1000) / 32 - 1);
267 SSYNC();
268
269 bfin_write_USB_EP_NI0_RXMAXP(64);
270 SSYNC();
271
272 bfin_write_USB_EP_NI0_TXMAXP(64);
273 SSYNC();
274
275 /* Route INTRUSB/INTR_RX/INTR_TX to USB_INT0*/
276 bfin_write_USB_GLOBINTR(0x7);
277 SSYNC();
278
279 bfin_write_USB_GLOBAL_CTL(GLOBAL_ENA | EP1_TX_ENA | EP2_TX_ENA |
280 EP3_TX_ENA | EP4_TX_ENA | EP5_TX_ENA |
281 EP6_TX_ENA | EP7_TX_ENA | EP1_RX_ENA |
282 EP2_RX_ENA | EP3_RX_ENA | EP4_RX_ENA |
283 EP5_RX_ENA | EP6_RX_ENA | EP7_RX_ENA);
284 SSYNC();
285
286 if (is_host_enabled(musb)) {
287 musb->board_set_vbus = bfin_set_vbus;
288 setup_timer(&musb_conn_timer,
289 musb_conn_timer_handler, (unsigned long) musb);
290 }
291 if (is_peripheral_enabled(musb))
292 musb->xceiv.set_power = bfin_set_power;
293
294 musb->isr = blackfin_interrupt;
295
296 return 0;
297}
298
299int musb_platform_suspend(struct musb *musb)
300{
301 return 0;
302}
303
304int musb_platform_resume(struct musb *musb)
305{
306 return 0;
307}
308
309
310int musb_platform_exit(struct musb *musb)
311{
312
313 bfin_vbus_power(musb, 0 /*off*/, 1);
314 gpio_free(musb->config->gpio_vrsel);
315 musb_platform_suspend(musb);
316
317 return 0;
318}