blob: 54f1b98b74de7f9b487c597fcefc0e097831b2eb [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>
Bryan Wu0c6a8812008-12-02 21:33:44 +020014#include <linux/init.h>
15#include <linux/list.h>
Bryan Wu0c6a8812008-12-02 21:33:44 +020016#include <linux/gpio.h>
17#include <linux/io.h>
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +053018#include <linux/err.h>
Felipe Balbi9cb03082010-12-02 09:21:05 +020019#include <linux/platform_device.h>
20#include <linux/dma-mapping.h>
Bob Liuad50c1b2011-08-05 17:33:05 +080021#include <linux/prefetch.h>
Bryan Wu0c6a8812008-12-02 21:33:44 +020022
23#include <asm/cacheflush.h>
24
25#include "musb_core.h"
Mike Frysinger13254302011-03-30 22:48:54 -040026#include "musbhsdma.h"
Bryan Wu0c6a8812008-12-02 21:33:44 +020027#include "blackfin.h"
28
Felipe Balbia023c632010-12-02 09:42:50 +020029struct bfin_glue {
30 struct device *dev;
31 struct platform_device *musb;
32};
Felipe Balbifcd22e32010-12-02 13:13:09 +020033#define glue_to_musb(g) platform_get_drvdata(g->musb)
Felipe Balbia023c632010-12-02 09:42:50 +020034
Bryan Wu0c6a8812008-12-02 21:33:44 +020035/*
36 * Load an endpoint's FIFO
37 */
38void musb_write_fifo(struct musb_hw_ep *hw_ep, u16 len, const u8 *src)
39{
Felipe Balbi28e49702011-05-18 00:25:03 +030040 struct musb *musb = hw_ep->musb;
Bryan Wu0c6a8812008-12-02 21:33:44 +020041 void __iomem *fifo = hw_ep->fifo;
42 void __iomem *epio = hw_ep->regs;
Bryan Wu1c4bdc02009-12-21 09:49:52 -050043 u8 epnum = hw_ep->epnum;
Bryan Wu0c6a8812008-12-02 21:33:44 +020044
45 prefetch((u8 *)src);
46
47 musb_writew(epio, MUSB_TXCOUNT, len);
48
Felipe Balbi5c8a86e2011-05-11 12:44:08 +030049 dev_dbg(musb->controller, "TX ep%d fifo %p count %d buf %p, epio %p\n",
Bryan Wu0c6a8812008-12-02 21:33:44 +020050 hw_ep->epnum, fifo, len, src, epio);
51
52 dump_fifo_data(src, len);
53
Bryan Wu1c4bdc02009-12-21 09:49:52 -050054 if (!ANOMALY_05000380 && epnum != 0) {
Bryan Wu1ca9e9c2009-12-28 13:40:39 +020055 u16 dma_reg;
56
57 flush_dcache_range((unsigned long)src,
58 (unsigned long)(src + len));
Bryan Wu0c6a8812008-12-02 21:33:44 +020059
Bryan Wu1c4bdc02009-12-21 09:49:52 -050060 /* Setup DMA address register */
Bryan Wu1ca9e9c2009-12-28 13:40:39 +020061 dma_reg = (u32)src;
Bryan Wu1c4bdc02009-12-21 09:49:52 -050062 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_ADDR_LOW), dma_reg);
63 SSYNC();
64
Bryan Wu1ca9e9c2009-12-28 13:40:39 +020065 dma_reg = (u32)src >> 16;
Bryan Wu1c4bdc02009-12-21 09:49:52 -050066 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_ADDR_HIGH), dma_reg);
67 SSYNC();
68
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);
72 SSYNC();
73
74 /* Enable the DMA */
75 dma_reg = (epnum << 4) | DMA_ENA | INT_ENA | DIRECTION;
76 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_CTRL), dma_reg);
77 SSYNC();
78
79 /* Wait for compelete */
80 while (!(bfin_read_USB_DMA_INTERRUPT() & (1 << epnum)))
81 cpu_relax();
82
83 /* acknowledge dma interrupt */
84 bfin_write_USB_DMA_INTERRUPT(1 << epnum);
85 SSYNC();
86
87 /* Reset DMA */
88 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_CTRL), 0);
89 SSYNC();
90 } else {
91 SSYNC();
92
93 if (unlikely((unsigned long)src & 0x01))
Bryan Wu1ca9e9c2009-12-28 13:40:39 +020094 outsw_8((unsigned long)fifo, src, (len + 1) >> 1);
Bryan Wu1c4bdc02009-12-21 09:49:52 -050095 else
Bryan Wu1ca9e9c2009-12-28 13:40:39 +020096 outsw((unsigned long)fifo, src, (len + 1) >> 1);
Bryan Wu1c4bdc02009-12-21 09:49:52 -050097 }
98}
Bryan Wu0c6a8812008-12-02 21:33:44 +020099/*
100 * Unload an endpoint's FIFO
101 */
102void musb_read_fifo(struct musb_hw_ep *hw_ep, u16 len, u8 *dst)
103{
Felipe Balbi28e49702011-05-18 00:25:03 +0300104 struct musb *musb = hw_ep->musb;
Bryan Wu0c6a8812008-12-02 21:33:44 +0200105 void __iomem *fifo = hw_ep->fifo;
106 u8 epnum = hw_ep->epnum;
Bryan Wu0c6a8812008-12-02 21:33:44 +0200107
Bryan Wu1c4bdc02009-12-21 09:49:52 -0500108 if (ANOMALY_05000467 && epnum != 0) {
Bryan Wu1ca9e9c2009-12-28 13:40:39 +0200109 u16 dma_reg;
Bryan Wu0c6a8812008-12-02 21:33:44 +0200110
Bryan Wu1ca9e9c2009-12-28 13:40:39 +0200111 invalidate_dcache_range((unsigned long)dst,
112 (unsigned long)(dst + len));
Bryan Wu0c6a8812008-12-02 21:33:44 +0200113
Bryan Wu1c4bdc02009-12-21 09:49:52 -0500114 /* Setup DMA address register */
Bryan Wu1ca9e9c2009-12-28 13:40:39 +0200115 dma_reg = (u32)dst;
Bryan Wu1c4bdc02009-12-21 09:49:52 -0500116 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_ADDR_LOW), dma_reg);
117 SSYNC();
Bryan Wu0c6a8812008-12-02 21:33:44 +0200118
Bryan Wu1ca9e9c2009-12-28 13:40:39 +0200119 dma_reg = (u32)dst >> 16;
Bryan Wu1c4bdc02009-12-21 09:49:52 -0500120 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_ADDR_HIGH), dma_reg);
121 SSYNC();
Bryan Wu0c6a8812008-12-02 21:33:44 +0200122
Bryan Wu1c4bdc02009-12-21 09:49:52 -0500123 /* 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);
126 SSYNC();
Bryan Wu0c6a8812008-12-02 21:33:44 +0200127
Bryan Wu1c4bdc02009-12-21 09:49:52 -0500128 /* Enable the DMA */
129 dma_reg = (epnum << 4) | DMA_ENA | INT_ENA;
130 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_CTRL), dma_reg);
131 SSYNC();
Bryan Wu0c6a8812008-12-02 21:33:44 +0200132
Bryan Wu1c4bdc02009-12-21 09:49:52 -0500133 /* Wait for compelete */
134 while (!(bfin_read_USB_DMA_INTERRUPT() & (1 << epnum)))
135 cpu_relax();
Bryan Wu0c6a8812008-12-02 21:33:44 +0200136
Bryan Wu1c4bdc02009-12-21 09:49:52 -0500137 /* acknowledge dma interrupt */
138 bfin_write_USB_DMA_INTERRUPT(1 << epnum);
139 SSYNC();
Bryan Wu0c6a8812008-12-02 21:33:44 +0200140
Bryan Wu1c4bdc02009-12-21 09:49:52 -0500141 /* Reset DMA */
142 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_CTRL), 0);
143 SSYNC();
144 } else {
145 SSYNC();
146 /* Read the last byte of packet with odd size from address fifo + 4
147 * to trigger 1 byte access to EP0 FIFO.
148 */
149 if (len == 1)
150 *dst = (u8)inw((unsigned long)fifo + 4);
151 else {
152 if (unlikely((unsigned long)dst & 0x01))
153 insw_8((unsigned long)fifo, dst, len >> 1);
154 else
155 insw((unsigned long)fifo, dst, len >> 1);
156
157 if (len & 0x01)
158 *(dst + len - 1) = (u8)inw((unsigned long)fifo + 4);
159 }
160 }
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300161 dev_dbg(musb->controller, "%cX ep%d fifo %p count %d buf %p\n",
Mike Frysinger04f40862009-11-16 16:19:19 +0530162 'R', hw_ep->epnum, fifo, len, dst);
163
Bryan Wu0c6a8812008-12-02 21:33:44 +0200164 dump_fifo_data(dst, len);
165}
166
167static irqreturn_t blackfin_interrupt(int irq, void *__hci)
168{
169 unsigned long flags;
170 irqreturn_t retval = IRQ_NONE;
171 struct musb *musb = __hci;
172
173 spin_lock_irqsave(&musb->lock, flags);
174
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);
178
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);
184 }
185
Cliff Caiff927ad2010-03-25 13:25:19 +0200186 /* Start sampling ID pin, when plug is removed from MUSB */
Felipe Balbi032ec492011-11-24 15:46:26 +0200187 if ((musb->xceiv->state == OTG_STATE_B_IDLE
188 || musb->xceiv->state == OTG_STATE_A_WAIT_BCON) ||
Bob Liu68f64712010-10-23 05:12:00 -0500189 (musb->int_usb & MUSB_INTR_DISCONNECT && is_host_active(musb))) {
Cliff Caiff927ad2010-03-25 13:25:19 +0200190 mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY);
191 musb->a_wait_bcon = TIMER_DELAY;
192 }
193
Bryan Wu0c6a8812008-12-02 21:33:44 +0200194 spin_unlock_irqrestore(&musb->lock, flags);
195
Sergei Shtylyov2f831752010-03-25 13:14:25 +0200196 return retval;
Bryan Wu0c6a8812008-12-02 21:33:44 +0200197}
198
199static void musb_conn_timer_handler(unsigned long _musb)
200{
201 struct musb *musb = (void *)_musb;
202 unsigned long flags;
203 u16 val;
Cliff Caiff927ad2010-03-25 13:25:19 +0200204 static u8 toggle;
Bryan Wu0c6a8812008-12-02 21:33:44 +0200205
206 spin_lock_irqsave(&musb->lock, flags);
David Brownell84e250f2009-03-31 12:30:04 -0700207 switch (musb->xceiv->state) {
Bryan Wu0c6a8812008-12-02 21:33:44 +0200208 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);
Cliff Caiff927ad2010-03-25 13:25:19 +0200212 val &= ~MUSB_DEVCTL_SESSION;
213 musb_writew(musb->mregs, MUSB_DEVCTL, val);
Bryan Wu0c6a8812008-12-02 21:33:44 +0200214 val |= MUSB_DEVCTL_SESSION;
215 musb_writew(musb->mregs, MUSB_DEVCTL, val);
Cliff Caiff927ad2010-03-25 13:25:19 +0200216 /* Check if musb is host or peripheral. */
Bryan Wu0c6a8812008-12-02 21:33:44 +0200217 val = musb_readw(musb->mregs, MUSB_DEVCTL);
Cliff Caiff927ad2010-03-25 13:25:19 +0200218
219 if (!(val & MUSB_DEVCTL_BDEVICE)) {
220 gpio_set_value(musb->config->gpio_vrsel, 1);
221 musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
222 } else {
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);
228
229 val = MUSB_INTR_SUSPEND | MUSB_INTR_VBUSERROR;
230 musb_writeb(musb->mregs, MUSB_INTRUSB, val);
Felipe Balbi032ec492011-11-24 15:46:26 +0200231 musb->xceiv->state = OTG_STATE_B_IDLE;
Cliff Caiff927ad2010-03-25 13:25:19 +0200232 }
233 mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY);
234 break;
235 case OTG_STATE_B_IDLE:
Felipe Balbi032ec492011-11-24 15:46:26 +0200236 /*
237 * Start a new session. It seems that MUSB needs taking
Cliff Caiff927ad2010-03-25 13:25:19 +0200238 * some time to recognize the type of the plug inserted?
239 */
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);
244
Bryan Wu0c6a8812008-12-02 21:33:44 +0200245 if (!(val & MUSB_DEVCTL_BDEVICE)) {
246 gpio_set_value(musb->config->gpio_vrsel, 1);
David Brownell84e250f2009-03-31 12:30:04 -0700247 musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
Bryan Wu0c6a8812008-12-02 21:33:44 +0200248 } else {
249 gpio_set_value(musb->config->gpio_vrsel, 0);
250
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);
255
256 val = MUSB_INTR_SUSPEND | MUSB_INTR_VBUSERROR;
257 musb_writeb(musb->mregs, MUSB_INTRUSB, val);
258
Cliff Caiff927ad2010-03-25 13:25:19 +0200259 /* Toggle the Soft Conn bit, so that we can response to
260 * the inserting of either A-plug or B-plug.
261 */
262 if (toggle) {
263 val = musb_readb(musb->mregs, MUSB_POWER);
264 val &= ~MUSB_POWER_SOFTCONN;
265 musb_writeb(musb->mregs, MUSB_POWER, val);
266 toggle = 0;
267 } else {
268 val = musb_readb(musb->mregs, MUSB_POWER);
269 val |= MUSB_POWER_SOFTCONN;
270 musb_writeb(musb->mregs, MUSB_POWER, val);
271 toggle = 1;
272 }
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.
276 */
277 mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY / 4);
Bryan Wu0c6a8812008-12-02 21:33:44 +0200278 }
Bryan Wu0c6a8812008-12-02 21:33:44 +0200279 break;
Bryan Wu0c6a8812008-12-02 21:33:44 +0200280 default:
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300281 dev_dbg(musb->controller, "%s state not handled\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200282 otg_state_string(musb->xceiv->state));
Bryan Wu0c6a8812008-12-02 21:33:44 +0200283 break;
284 }
285 spin_unlock_irqrestore(&musb->lock, flags);
286
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300287 dev_dbg(musb->controller, "state is %s\n",
288 otg_state_string(musb->xceiv->state));
Bryan Wu0c6a8812008-12-02 21:33:44 +0200289}
290
Felipe Balbi743411b2010-12-01 13:22:05 +0200291static void bfin_musb_enable(struct musb *musb)
Bryan Wu0c6a8812008-12-02 21:33:44 +0200292{
Felipe Balbi032ec492011-11-24 15:46:26 +0200293 /* REVISIT is this really correct ? */
Bryan Wu0c6a8812008-12-02 21:33:44 +0200294}
295
Felipe Balbi743411b2010-12-01 13:22:05 +0200296static void bfin_musb_disable(struct musb *musb)
Bryan Wu0c6a8812008-12-02 21:33:44 +0200297{
298}
299
Felipe Balbi743411b2010-12-01 13:22:05 +0200300static void bfin_musb_set_vbus(struct musb *musb, int is_on)
Bryan Wu0c6a8812008-12-02 21:33:44 +0200301{
Cliff Cai6ddc6da2010-03-12 10:29:10 +0200302 int value = musb->config->gpio_vrsel_active;
303 if (!is_on)
304 value = !value;
305 gpio_set_value(musb->config->gpio_vrsel, value);
Bryan Wu0c6a8812008-12-02 21:33:44 +0200306
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300307 dev_dbg(musb->controller, "VBUS %s, devctl %02x "
Bryan Wu0c6a8812008-12-02 21:33:44 +0200308 /* otg %3x conf %08x prcm %08x */ "\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200309 otg_state_string(musb->xceiv->state),
Bryan Wu0c6a8812008-12-02 21:33:44 +0200310 musb_readb(musb->mregs, MUSB_DEVCTL));
311}
312
Heikki Krogerus86753812012-02-13 13:24:02 +0200313static int bfin_musb_set_power(struct usb_phy *x, unsigned mA)
Bryan Wu0c6a8812008-12-02 21:33:44 +0200314{
315 return 0;
316}
317
Mike Frysinger45567c22011-03-21 14:06:32 -0400318static int bfin_musb_vbus_status(struct musb *musb)
Bryan Wu0c6a8812008-12-02 21:33:44 +0200319{
320 return 0;
321}
322
Felipe Balbi743411b2010-12-01 13:22:05 +0200323static int bfin_musb_set_mode(struct musb *musb, u8 musb_mode)
Bryan Wu0c6a8812008-12-02 21:33:44 +0200324{
Bryan Wu2002e762009-11-16 16:19:25 +0530325 return -EIO;
Bryan Wu0c6a8812008-12-02 21:33:44 +0200326}
327
Mike Frysinger13254302011-03-30 22:48:54 -0400328static int bfin_musb_adjust_channel_params(struct dma_channel *channel,
329 u16 packet_sz, u8 *mode,
330 dma_addr_t *dma_addr, u32 *len)
331{
332 struct musb_dma_channel *musb_channel = channel->private_data;
333
334 /*
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.
340 */
341 if (ANOMALY_05000450) {
342 if (musb_channel->transmit && *mode == 1)
343 *len = *len - (*len % packet_sz);
344 }
345
346 return 0;
347}
348
Felipe Balbi743411b2010-12-01 13:22:05 +0200349static void bfin_musb_reg_init(struct musb *musb)
Bryan Wu0c6a8812008-12-02 21:33:44 +0200350{
Robin Getzd426e602008-12-02 21:33:45 +0200351 if (ANOMALY_05000346) {
352 bfin_write_USB_APHY_CALIB(ANOMALY_05000346_value);
353 SSYNC();
354 }
Bryan Wu0c6a8812008-12-02 21:33:44 +0200355
Robin Getzd426e602008-12-02 21:33:45 +0200356 if (ANOMALY_05000347) {
357 bfin_write_USB_APHY_CNTRL(0x0);
358 SSYNC();
359 }
Bryan Wu0c6a8812008-12-02 21:33:44 +0200360
Bryan Wu0c6a8812008-12-02 21:33:44 +0200361 /* Configure PLL oscillator register */
Bob Liu9c756462010-10-23 05:12:01 -0500362 bfin_write_USB_PLLOSC_CTRL(0x3080 |
363 ((480/musb->config->clkin) << 1));
Bryan Wu0c6a8812008-12-02 21:33:44 +0200364 SSYNC();
365
366 bfin_write_USB_SRP_CLKDIV((get_sclk()/1000) / 32 - 1);
367 SSYNC();
368
369 bfin_write_USB_EP_NI0_RXMAXP(64);
370 SSYNC();
371
372 bfin_write_USB_EP_NI0_TXMAXP(64);
373 SSYNC();
374
375 /* Route INTRUSB/INTR_RX/INTR_TX to USB_INT0*/
376 bfin_write_USB_GLOBINTR(0x7);
377 SSYNC();
378
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);
384 SSYNC();
Felipe Balbi743411b2010-12-01 13:22:05 +0200385}
386
387static int bfin_musb_init(struct musb *musb)
388{
389
390 /*
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
395 */
396
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);
400 return -ENODEV;
401 }
402 gpio_direction_output(musb->config->gpio_vrsel, 0);
403
404 usb_nop_xceiv_register();
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +0530405 musb->xceiv = usb_get_phy(USB_PHY_TYPE_USB2);
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +0530406 if (IS_ERR_OR_NULL(musb->xceiv)) {
Felipe Balbi743411b2010-12-01 13:22:05 +0200407 gpio_free(musb->config->gpio_vrsel);
408 return -ENODEV;
409 }
410
411 bfin_musb_reg_init(musb);
Bryan Wu0c6a8812008-12-02 21:33:44 +0200412
Felipe Balbi032ec492011-11-24 15:46:26 +0200413 setup_timer(&musb_conn_timer, musb_conn_timer_handler,
414 (unsigned long) musb);
415
416 musb->xceiv->set_power = bfin_musb_set_power;
Bryan Wu0c6a8812008-12-02 21:33:44 +0200417
418 musb->isr = blackfin_interrupt;
Felipe Balbi06624812011-01-21 13:39:20 +0800419 musb->double_buffer_not_ok = true;
Bryan Wu0c6a8812008-12-02 21:33:44 +0200420
421 return 0;
422}
423
Felipe Balbi743411b2010-12-01 13:22:05 +0200424static int bfin_musb_exit(struct musb *musb)
Bryan Wu0c6a8812008-12-02 21:33:44 +0200425{
Bryan Wu0c6a8812008-12-02 21:33:44 +0200426 gpio_free(musb->config->gpio_vrsel);
Bryan Wu0c6a8812008-12-02 21:33:44 +0200427
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +0530428 usb_put_phy(musb->xceiv);
Sergei Shtylyov3daad242010-09-29 09:54:30 +0300429 usb_nop_xceiv_unregister();
Bryan Wu0c6a8812008-12-02 21:33:44 +0200430 return 0;
431}
Felipe Balbi743411b2010-12-01 13:22:05 +0200432
Felipe Balbif7ec9432010-12-02 09:48:58 +0200433static const struct musb_platform_ops bfin_ops = {
Felipe Balbi743411b2010-12-01 13:22:05 +0200434 .init = bfin_musb_init,
435 .exit = bfin_musb_exit,
436
437 .enable = bfin_musb_enable,
438 .disable = bfin_musb_disable,
439
440 .set_mode = bfin_musb_set_mode,
Felipe Balbi743411b2010-12-01 13:22:05 +0200441
442 .vbus_status = bfin_musb_vbus_status,
443 .set_vbus = bfin_musb_set_vbus,
Mike Frysinger13254302011-03-30 22:48:54 -0400444
445 .adjust_channel_params = bfin_musb_adjust_channel_params,
Felipe Balbi743411b2010-12-01 13:22:05 +0200446};
Felipe Balbi9cb03082010-12-02 09:21:05 +0200447
448static u64 bfin_dmamask = DMA_BIT_MASK(32);
449
Felipe Balbie9e8c852012-01-26 12:40:23 +0200450static int __devinit bfin_probe(struct platform_device *pdev)
Felipe Balbi9cb03082010-12-02 09:21:05 +0200451{
452 struct musb_hdrc_platform_data *pdata = pdev->dev.platform_data;
453 struct platform_device *musb;
Felipe Balbia023c632010-12-02 09:42:50 +0200454 struct bfin_glue *glue;
Felipe Balbi9cb03082010-12-02 09:21:05 +0200455
456 int ret = -ENOMEM;
457
Felipe Balbia023c632010-12-02 09:42:50 +0200458 glue = kzalloc(sizeof(*glue), GFP_KERNEL);
459 if (!glue) {
460 dev_err(&pdev->dev, "failed to allocate glue context\n");
461 goto err0;
462 }
463
Felipe Balbi9cb03082010-12-02 09:21:05 +0200464 musb = platform_device_alloc("musb-hdrc", -1);
465 if (!musb) {
466 dev_err(&pdev->dev, "failed to allocate musb device\n");
Felipe Balbia023c632010-12-02 09:42:50 +0200467 goto err1;
Felipe Balbi9cb03082010-12-02 09:21:05 +0200468 }
469
470 musb->dev.parent = &pdev->dev;
471 musb->dev.dma_mask = &bfin_dmamask;
472 musb->dev.coherent_dma_mask = bfin_dmamask;
473
Felipe Balbia023c632010-12-02 09:42:50 +0200474 glue->dev = &pdev->dev;
475 glue->musb = musb;
476
Felipe Balbif7ec9432010-12-02 09:48:58 +0200477 pdata->platform_ops = &bfin_ops;
478
Felipe Balbia023c632010-12-02 09:42:50 +0200479 platform_set_drvdata(pdev, glue);
Felipe Balbi9cb03082010-12-02 09:21:05 +0200480
481 ret = platform_device_add_resources(musb, pdev->resource,
482 pdev->num_resources);
483 if (ret) {
484 dev_err(&pdev->dev, "failed to add resources\n");
Felipe Balbia023c632010-12-02 09:42:50 +0200485 goto err2;
Felipe Balbi9cb03082010-12-02 09:21:05 +0200486 }
487
488 ret = platform_device_add_data(musb, pdata, sizeof(*pdata));
489 if (ret) {
490 dev_err(&pdev->dev, "failed to add platform_data\n");
Felipe Balbia023c632010-12-02 09:42:50 +0200491 goto err2;
Felipe Balbi9cb03082010-12-02 09:21:05 +0200492 }
493
494 ret = platform_device_add(musb);
495 if (ret) {
496 dev_err(&pdev->dev, "failed to register musb device\n");
Felipe Balbia023c632010-12-02 09:42:50 +0200497 goto err2;
Felipe Balbi9cb03082010-12-02 09:21:05 +0200498 }
499
500 return 0;
501
Felipe Balbia023c632010-12-02 09:42:50 +0200502err2:
Felipe Balbi9cb03082010-12-02 09:21:05 +0200503 platform_device_put(musb);
504
Felipe Balbia023c632010-12-02 09:42:50 +0200505err1:
506 kfree(glue);
507
Felipe Balbi9cb03082010-12-02 09:21:05 +0200508err0:
509 return ret;
510}
511
Felipe Balbie9e8c852012-01-26 12:40:23 +0200512static int __devexit bfin_remove(struct platform_device *pdev)
Felipe Balbi9cb03082010-12-02 09:21:05 +0200513{
Felipe Balbia023c632010-12-02 09:42:50 +0200514 struct bfin_glue *glue = platform_get_drvdata(pdev);
Felipe Balbi9cb03082010-12-02 09:21:05 +0200515
Felipe Balbia023c632010-12-02 09:42:50 +0200516 platform_device_del(glue->musb);
517 platform_device_put(glue->musb);
518 kfree(glue);
Felipe Balbi9cb03082010-12-02 09:21:05 +0200519
520 return 0;
521}
522
Felipe Balbifcd22e32010-12-02 13:13:09 +0200523#ifdef CONFIG_PM
524static int bfin_suspend(struct device *dev)
525{
526 struct bfin_glue *glue = dev_get_drvdata(dev);
527 struct musb *musb = glue_to_musb(glue);
528
529 if (is_host_active(musb))
530 /*
531 * During hibernate gpio_vrsel will change from high to low
532 * low which will generate wakeup event resume the system
533 * immediately. Set it to 0 before hibernate to avoid this
534 * wakeup event.
535 */
536 gpio_set_value(musb->config->gpio_vrsel, 0);
537
538 return 0;
539}
540
541static int bfin_resume(struct device *dev)
542{
543 struct bfin_glue *glue = dev_get_drvdata(dev);
544 struct musb *musb = glue_to_musb(glue);
545
546 bfin_musb_reg_init(musb);
547
548 return 0;
549}
550
551static struct dev_pm_ops bfin_pm_ops = {
552 .suspend = bfin_suspend,
553 .resume = bfin_resume,
554};
555
Bob Liu8f7e7b82011-03-21 14:06:31 -0400556#define DEV_PM_OPS &bfin_pm_ops
Felipe Balbifcd22e32010-12-02 13:13:09 +0200557#else
558#define DEV_PM_OPS NULL
559#endif
560
Felipe Balbi9cb03082010-12-02 09:21:05 +0200561static struct platform_driver bfin_driver = {
Felipe Balbie9e8c852012-01-26 12:40:23 +0200562 .probe = bfin_probe,
Felipe Balbi9cb03082010-12-02 09:21:05 +0200563 .remove = __exit_p(bfin_remove),
564 .driver = {
Mike Frysinger417ddf82011-03-22 14:43:37 -0400565 .name = "musb-blackfin",
Felipe Balbifcd22e32010-12-02 13:13:09 +0200566 .pm = DEV_PM_OPS,
Felipe Balbi9cb03082010-12-02 09:21:05 +0200567 },
568};
569
570MODULE_DESCRIPTION("Blackfin MUSB Glue Layer");
571MODULE_AUTHOR("Bryan Wy <cooloney@kernel.org>");
572MODULE_LICENSE("GPL v2");
573
574static int __init bfin_init(void)
575{
Felipe Balbie9e8c852012-01-26 12:40:23 +0200576 return platform_driver_register(&bfin_driver);
Felipe Balbi9cb03082010-12-02 09:21:05 +0200577}
Felipe Balbie9e8c852012-01-26 12:40:23 +0200578module_init(bfin_init);
Felipe Balbi9cb03082010-12-02 09:21:05 +0200579
580static void __exit bfin_exit(void)
581{
582 platform_driver_unregister(&bfin_driver);
583}
584module_exit(bfin_exit);