blob: b562623a8971f37beb9d47b55a7ab5c20653f996 [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>
Felipe Balbi78c289f2012-07-19 13:32:15 +030022#include <linux/usb/nop-usb-xceiv.h>
Bryan Wu0c6a8812008-12-02 21:33:44 +020023
24#include <asm/cacheflush.h>
25
26#include "musb_core.h"
Mike Frysinger13254302011-03-30 22:48:54 -040027#include "musbhsdma.h"
Bryan Wu0c6a8812008-12-02 21:33:44 +020028#include "blackfin.h"
29
Felipe Balbia023c632010-12-02 09:42:50 +020030struct bfin_glue {
31 struct device *dev;
32 struct platform_device *musb;
33};
Felipe Balbifcd22e32010-12-02 13:13:09 +020034#define glue_to_musb(g) platform_get_drvdata(g->musb)
Felipe Balbia023c632010-12-02 09:42:50 +020035
Bryan Wu0c6a8812008-12-02 21:33:44 +020036/*
37 * Load an endpoint's FIFO
38 */
39void musb_write_fifo(struct musb_hw_ep *hw_ep, u16 len, const u8 *src)
40{
Felipe Balbi28e49702011-05-18 00:25:03 +030041 struct musb *musb = hw_ep->musb;
Bryan Wu0c6a8812008-12-02 21:33:44 +020042 void __iomem *fifo = hw_ep->fifo;
43 void __iomem *epio = hw_ep->regs;
Bryan Wu1c4bdc02009-12-21 09:49:52 -050044 u8 epnum = hw_ep->epnum;
Bryan Wu0c6a8812008-12-02 21:33:44 +020045
46 prefetch((u8 *)src);
47
48 musb_writew(epio, MUSB_TXCOUNT, len);
49
Felipe Balbi5c8a86e2011-05-11 12:44:08 +030050 dev_dbg(musb->controller, "TX ep%d fifo %p count %d buf %p, epio %p\n",
Bryan Wu0c6a8812008-12-02 21:33:44 +020051 hw_ep->epnum, fifo, len, src, epio);
52
53 dump_fifo_data(src, len);
54
Bryan Wu1c4bdc02009-12-21 09:49:52 -050055 if (!ANOMALY_05000380 && epnum != 0) {
Bryan Wu1ca9e9c2009-12-28 13:40:39 +020056 u16 dma_reg;
57
58 flush_dcache_range((unsigned long)src,
59 (unsigned long)(src + len));
Bryan Wu0c6a8812008-12-02 21:33:44 +020060
Bryan Wu1c4bdc02009-12-21 09:49:52 -050061 /* Setup DMA address register */
Bryan Wu1ca9e9c2009-12-28 13:40:39 +020062 dma_reg = (u32)src;
Bryan Wu1c4bdc02009-12-21 09:49:52 -050063 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_ADDR_LOW), dma_reg);
64 SSYNC();
65
Bryan Wu1ca9e9c2009-12-28 13:40:39 +020066 dma_reg = (u32)src >> 16;
Bryan Wu1c4bdc02009-12-21 09:49:52 -050067 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_ADDR_HIGH), dma_reg);
68 SSYNC();
69
70 /* Setup DMA count register */
71 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_COUNT_LOW), len);
72 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_COUNT_HIGH), 0);
73 SSYNC();
74
75 /* Enable the DMA */
76 dma_reg = (epnum << 4) | DMA_ENA | INT_ENA | DIRECTION;
77 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_CTRL), dma_reg);
78 SSYNC();
79
80 /* Wait for compelete */
81 while (!(bfin_read_USB_DMA_INTERRUPT() & (1 << epnum)))
82 cpu_relax();
83
84 /* acknowledge dma interrupt */
85 bfin_write_USB_DMA_INTERRUPT(1 << epnum);
86 SSYNC();
87
88 /* Reset DMA */
89 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_CTRL), 0);
90 SSYNC();
91 } else {
92 SSYNC();
93
94 if (unlikely((unsigned long)src & 0x01))
Bryan Wu1ca9e9c2009-12-28 13:40:39 +020095 outsw_8((unsigned long)fifo, src, (len + 1) >> 1);
Bryan Wu1c4bdc02009-12-21 09:49:52 -050096 else
Bryan Wu1ca9e9c2009-12-28 13:40:39 +020097 outsw((unsigned long)fifo, src, (len + 1) >> 1);
Bryan Wu1c4bdc02009-12-21 09:49:52 -050098 }
99}
Bryan Wu0c6a8812008-12-02 21:33:44 +0200100/*
101 * Unload an endpoint's FIFO
102 */
103void musb_read_fifo(struct musb_hw_ep *hw_ep, u16 len, u8 *dst)
104{
Felipe Balbi28e49702011-05-18 00:25:03 +0300105 struct musb *musb = hw_ep->musb;
Bryan Wu0c6a8812008-12-02 21:33:44 +0200106 void __iomem *fifo = hw_ep->fifo;
107 u8 epnum = hw_ep->epnum;
Bryan Wu0c6a8812008-12-02 21:33:44 +0200108
Bryan Wu1c4bdc02009-12-21 09:49:52 -0500109 if (ANOMALY_05000467 && epnum != 0) {
Bryan Wu1ca9e9c2009-12-28 13:40:39 +0200110 u16 dma_reg;
Bryan Wu0c6a8812008-12-02 21:33:44 +0200111
Bryan Wu1ca9e9c2009-12-28 13:40:39 +0200112 invalidate_dcache_range((unsigned long)dst,
113 (unsigned long)(dst + len));
Bryan Wu0c6a8812008-12-02 21:33:44 +0200114
Bryan Wu1c4bdc02009-12-21 09:49:52 -0500115 /* Setup DMA address register */
Bryan Wu1ca9e9c2009-12-28 13:40:39 +0200116 dma_reg = (u32)dst;
Bryan Wu1c4bdc02009-12-21 09:49:52 -0500117 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_ADDR_LOW), dma_reg);
118 SSYNC();
Bryan Wu0c6a8812008-12-02 21:33:44 +0200119
Bryan Wu1ca9e9c2009-12-28 13:40:39 +0200120 dma_reg = (u32)dst >> 16;
Bryan Wu1c4bdc02009-12-21 09:49:52 -0500121 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_ADDR_HIGH), dma_reg);
122 SSYNC();
Bryan Wu0c6a8812008-12-02 21:33:44 +0200123
Bryan Wu1c4bdc02009-12-21 09:49:52 -0500124 /* Setup DMA count register */
125 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_COUNT_LOW), len);
126 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_COUNT_HIGH), 0);
127 SSYNC();
Bryan Wu0c6a8812008-12-02 21:33:44 +0200128
Bryan Wu1c4bdc02009-12-21 09:49:52 -0500129 /* Enable the DMA */
130 dma_reg = (epnum << 4) | DMA_ENA | INT_ENA;
131 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_CTRL), dma_reg);
132 SSYNC();
Bryan Wu0c6a8812008-12-02 21:33:44 +0200133
Bryan Wu1c4bdc02009-12-21 09:49:52 -0500134 /* Wait for compelete */
135 while (!(bfin_read_USB_DMA_INTERRUPT() & (1 << epnum)))
136 cpu_relax();
Bryan Wu0c6a8812008-12-02 21:33:44 +0200137
Bryan Wu1c4bdc02009-12-21 09:49:52 -0500138 /* acknowledge dma interrupt */
139 bfin_write_USB_DMA_INTERRUPT(1 << epnum);
140 SSYNC();
Bryan Wu0c6a8812008-12-02 21:33:44 +0200141
Bryan Wu1c4bdc02009-12-21 09:49:52 -0500142 /* Reset DMA */
143 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_CTRL), 0);
144 SSYNC();
145 } else {
146 SSYNC();
147 /* Read the last byte of packet with odd size from address fifo + 4
148 * to trigger 1 byte access to EP0 FIFO.
149 */
150 if (len == 1)
151 *dst = (u8)inw((unsigned long)fifo + 4);
152 else {
153 if (unlikely((unsigned long)dst & 0x01))
154 insw_8((unsigned long)fifo, dst, len >> 1);
155 else
156 insw((unsigned long)fifo, dst, len >> 1);
157
158 if (len & 0x01)
159 *(dst + len - 1) = (u8)inw((unsigned long)fifo + 4);
160 }
161 }
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300162 dev_dbg(musb->controller, "%cX ep%d fifo %p count %d buf %p\n",
Mike Frysinger04f40862009-11-16 16:19:19 +0530163 'R', hw_ep->epnum, fifo, len, dst);
164
Bryan Wu0c6a8812008-12-02 21:33:44 +0200165 dump_fifo_data(dst, len);
166}
167
168static irqreturn_t blackfin_interrupt(int irq, void *__hci)
169{
170 unsigned long flags;
171 irqreturn_t retval = IRQ_NONE;
172 struct musb *musb = __hci;
173
174 spin_lock_irqsave(&musb->lock, flags);
175
176 musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB);
177 musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX);
178 musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX);
179
180 if (musb->int_usb || musb->int_tx || musb->int_rx) {
181 musb_writeb(musb->mregs, MUSB_INTRUSB, musb->int_usb);
182 musb_writew(musb->mregs, MUSB_INTRTX, musb->int_tx);
183 musb_writew(musb->mregs, MUSB_INTRRX, musb->int_rx);
184 retval = musb_interrupt(musb);
185 }
186
Cliff Caiff927ad2010-03-25 13:25:19 +0200187 /* Start sampling ID pin, when plug is removed from MUSB */
Bob Liu68f64712010-10-23 05:12:00 -0500188 if ((is_otg_enabled(musb) && (musb->xceiv->state == OTG_STATE_B_IDLE
189 || musb->xceiv->state == OTG_STATE_A_WAIT_BCON)) ||
190 (musb->int_usb & MUSB_INTR_DISCONNECT && is_host_active(musb))) {
Cliff Caiff927ad2010-03-25 13:25:19 +0200191 mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY);
192 musb->a_wait_bcon = TIMER_DELAY;
193 }
194
Bryan Wu0c6a8812008-12-02 21:33:44 +0200195 spin_unlock_irqrestore(&musb->lock, flags);
196
Sergei Shtylyov2f831752010-03-25 13:14:25 +0200197 return retval;
Bryan Wu0c6a8812008-12-02 21:33:44 +0200198}
199
200static void musb_conn_timer_handler(unsigned long _musb)
201{
202 struct musb *musb = (void *)_musb;
203 unsigned long flags;
204 u16 val;
Cliff Caiff927ad2010-03-25 13:25:19 +0200205 static u8 toggle;
Bryan Wu0c6a8812008-12-02 21:33:44 +0200206
207 spin_lock_irqsave(&musb->lock, flags);
David Brownell84e250f2009-03-31 12:30:04 -0700208 switch (musb->xceiv->state) {
Bryan Wu0c6a8812008-12-02 21:33:44 +0200209 case OTG_STATE_A_IDLE:
210 case OTG_STATE_A_WAIT_BCON:
211 /* Start a new session */
212 val = musb_readw(musb->mregs, MUSB_DEVCTL);
Cliff Caiff927ad2010-03-25 13:25:19 +0200213 val &= ~MUSB_DEVCTL_SESSION;
214 musb_writew(musb->mregs, MUSB_DEVCTL, val);
Bryan Wu0c6a8812008-12-02 21:33:44 +0200215 val |= MUSB_DEVCTL_SESSION;
216 musb_writew(musb->mregs, MUSB_DEVCTL, val);
Cliff Caiff927ad2010-03-25 13:25:19 +0200217 /* Check if musb is host or peripheral. */
Bryan Wu0c6a8812008-12-02 21:33:44 +0200218 val = musb_readw(musb->mregs, MUSB_DEVCTL);
Cliff Caiff927ad2010-03-25 13:25:19 +0200219
220 if (!(val & MUSB_DEVCTL_BDEVICE)) {
221 gpio_set_value(musb->config->gpio_vrsel, 1);
222 musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
223 } else {
224 gpio_set_value(musb->config->gpio_vrsel, 0);
225 /* Ignore VBUSERROR and SUSPEND IRQ */
226 val = musb_readb(musb->mregs, MUSB_INTRUSBE);
227 val &= ~MUSB_INTR_VBUSERROR;
228 musb_writeb(musb->mregs, MUSB_INTRUSBE, val);
229
230 val = MUSB_INTR_SUSPEND | MUSB_INTR_VBUSERROR;
231 musb_writeb(musb->mregs, MUSB_INTRUSB, val);
232 if (is_otg_enabled(musb))
233 musb->xceiv->state = OTG_STATE_B_IDLE;
234 else
235 musb_writeb(musb->mregs, MUSB_POWER, MUSB_POWER_HSENAB);
236 }
237 mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY);
238 break;
239 case OTG_STATE_B_IDLE:
240
241 if (!is_peripheral_enabled(musb))
242 break;
243 /* Start a new session. It seems that MUSB needs taking
244 * some time to recognize the type of the plug inserted?
245 */
246 val = musb_readw(musb->mregs, MUSB_DEVCTL);
247 val |= MUSB_DEVCTL_SESSION;
248 musb_writew(musb->mregs, MUSB_DEVCTL, val);
249 val = musb_readw(musb->mregs, MUSB_DEVCTL);
250
Bryan Wu0c6a8812008-12-02 21:33:44 +0200251 if (!(val & MUSB_DEVCTL_BDEVICE)) {
252 gpio_set_value(musb->config->gpio_vrsel, 1);
David Brownell84e250f2009-03-31 12:30:04 -0700253 musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
Bryan Wu0c6a8812008-12-02 21:33:44 +0200254 } else {
255 gpio_set_value(musb->config->gpio_vrsel, 0);
256
257 /* Ignore VBUSERROR and SUSPEND IRQ */
258 val = musb_readb(musb->mregs, MUSB_INTRUSBE);
259 val &= ~MUSB_INTR_VBUSERROR;
260 musb_writeb(musb->mregs, MUSB_INTRUSBE, val);
261
262 val = MUSB_INTR_SUSPEND | MUSB_INTR_VBUSERROR;
263 musb_writeb(musb->mregs, MUSB_INTRUSB, val);
264
Cliff Caiff927ad2010-03-25 13:25:19 +0200265 /* Toggle the Soft Conn bit, so that we can response to
266 * the inserting of either A-plug or B-plug.
267 */
268 if (toggle) {
269 val = musb_readb(musb->mregs, MUSB_POWER);
270 val &= ~MUSB_POWER_SOFTCONN;
271 musb_writeb(musb->mregs, MUSB_POWER, val);
272 toggle = 0;
273 } else {
274 val = musb_readb(musb->mregs, MUSB_POWER);
275 val |= MUSB_POWER_SOFTCONN;
276 musb_writeb(musb->mregs, MUSB_POWER, val);
277 toggle = 1;
278 }
279 /* The delay time is set to 1/4 second by default,
280 * shortening it, if accelerating A-plug detection
281 * is needed in OTG mode.
282 */
283 mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY / 4);
Bryan Wu0c6a8812008-12-02 21:33:44 +0200284 }
Bryan Wu0c6a8812008-12-02 21:33:44 +0200285 break;
Bryan Wu0c6a8812008-12-02 21:33:44 +0200286 default:
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300287 dev_dbg(musb->controller, "%s state not handled\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200288 otg_state_string(musb->xceiv->state));
Bryan Wu0c6a8812008-12-02 21:33:44 +0200289 break;
290 }
291 spin_unlock_irqrestore(&musb->lock, flags);
292
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300293 dev_dbg(musb->controller, "state is %s\n",
294 otg_state_string(musb->xceiv->state));
Bryan Wu0c6a8812008-12-02 21:33:44 +0200295}
296
Felipe Balbi743411b2010-12-01 13:22:05 +0200297static void bfin_musb_enable(struct musb *musb)
Bryan Wu0c6a8812008-12-02 21:33:44 +0200298{
Cliff Caiff927ad2010-03-25 13:25:19 +0200299 if (!is_otg_enabled(musb) && is_host_enabled(musb)) {
Bryan Wu0c6a8812008-12-02 21:33:44 +0200300 mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY);
301 musb->a_wait_bcon = TIMER_DELAY;
302 }
303}
304
Felipe Balbi743411b2010-12-01 13:22:05 +0200305static void bfin_musb_disable(struct musb *musb)
Bryan Wu0c6a8812008-12-02 21:33:44 +0200306{
307}
308
Felipe Balbi743411b2010-12-01 13:22:05 +0200309static void bfin_musb_set_vbus(struct musb *musb, int is_on)
Bryan Wu0c6a8812008-12-02 21:33:44 +0200310{
Cliff Cai6ddc6da2010-03-12 10:29:10 +0200311 int value = musb->config->gpio_vrsel_active;
312 if (!is_on)
313 value = !value;
314 gpio_set_value(musb->config->gpio_vrsel, value);
Bryan Wu0c6a8812008-12-02 21:33:44 +0200315
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300316 dev_dbg(musb->controller, "VBUS %s, devctl %02x "
Bryan Wu0c6a8812008-12-02 21:33:44 +0200317 /* otg %3x conf %08x prcm %08x */ "\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200318 otg_state_string(musb->xceiv->state),
Bryan Wu0c6a8812008-12-02 21:33:44 +0200319 musb_readb(musb->mregs, MUSB_DEVCTL));
320}
321
Heikki Krogerus86753812012-02-13 13:24:02 +0200322static int bfin_musb_set_power(struct usb_phy *x, unsigned mA)
Bryan Wu0c6a8812008-12-02 21:33:44 +0200323{
324 return 0;
325}
326
Felipe Balbi743411b2010-12-01 13:22:05 +0200327static void bfin_musb_try_idle(struct musb *musb, unsigned long timeout)
Bryan Wu0c6a8812008-12-02 21:33:44 +0200328{
Cliff Caiff927ad2010-03-25 13:25:19 +0200329 if (!is_otg_enabled(musb) && is_host_enabled(musb))
Bryan Wu0c6a8812008-12-02 21:33:44 +0200330 mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY);
331}
332
Mike Frysinger45567c22011-03-21 14:06:32 -0400333static int bfin_musb_vbus_status(struct musb *musb)
Bryan Wu0c6a8812008-12-02 21:33:44 +0200334{
335 return 0;
336}
337
Felipe Balbi743411b2010-12-01 13:22:05 +0200338static int bfin_musb_set_mode(struct musb *musb, u8 musb_mode)
Bryan Wu0c6a8812008-12-02 21:33:44 +0200339{
Bryan Wu2002e762009-11-16 16:19:25 +0530340 return -EIO;
Bryan Wu0c6a8812008-12-02 21:33:44 +0200341}
342
Mike Frysinger13254302011-03-30 22:48:54 -0400343static int bfin_musb_adjust_channel_params(struct dma_channel *channel,
344 u16 packet_sz, u8 *mode,
345 dma_addr_t *dma_addr, u32 *len)
346{
347 struct musb_dma_channel *musb_channel = channel->private_data;
348
349 /*
350 * Anomaly 05000450 might cause data corruption when using DMA
351 * MODE 1 transmits with short packet. So to work around this,
352 * we truncate all MODE 1 transfers down to a multiple of the
353 * max packet size, and then do the last short packet transfer
354 * (if there is any) using MODE 0.
355 */
356 if (ANOMALY_05000450) {
357 if (musb_channel->transmit && *mode == 1)
358 *len = *len - (*len % packet_sz);
359 }
360
361 return 0;
362}
363
Felipe Balbi743411b2010-12-01 13:22:05 +0200364static void bfin_musb_reg_init(struct musb *musb)
Bryan Wu0c6a8812008-12-02 21:33:44 +0200365{
Robin Getzd426e602008-12-02 21:33:45 +0200366 if (ANOMALY_05000346) {
367 bfin_write_USB_APHY_CALIB(ANOMALY_05000346_value);
368 SSYNC();
369 }
Bryan Wu0c6a8812008-12-02 21:33:44 +0200370
Robin Getzd426e602008-12-02 21:33:45 +0200371 if (ANOMALY_05000347) {
372 bfin_write_USB_APHY_CNTRL(0x0);
373 SSYNC();
374 }
Bryan Wu0c6a8812008-12-02 21:33:44 +0200375
Bryan Wu0c6a8812008-12-02 21:33:44 +0200376 /* Configure PLL oscillator register */
Bob Liu9c756462010-10-23 05:12:01 -0500377 bfin_write_USB_PLLOSC_CTRL(0x3080 |
378 ((480/musb->config->clkin) << 1));
Bryan Wu0c6a8812008-12-02 21:33:44 +0200379 SSYNC();
380
381 bfin_write_USB_SRP_CLKDIV((get_sclk()/1000) / 32 - 1);
382 SSYNC();
383
384 bfin_write_USB_EP_NI0_RXMAXP(64);
385 SSYNC();
386
387 bfin_write_USB_EP_NI0_TXMAXP(64);
388 SSYNC();
389
390 /* Route INTRUSB/INTR_RX/INTR_TX to USB_INT0*/
391 bfin_write_USB_GLOBINTR(0x7);
392 SSYNC();
393
394 bfin_write_USB_GLOBAL_CTL(GLOBAL_ENA | EP1_TX_ENA | EP2_TX_ENA |
395 EP3_TX_ENA | EP4_TX_ENA | EP5_TX_ENA |
396 EP6_TX_ENA | EP7_TX_ENA | EP1_RX_ENA |
397 EP2_RX_ENA | EP3_RX_ENA | EP4_RX_ENA |
398 EP5_RX_ENA | EP6_RX_ENA | EP7_RX_ENA);
399 SSYNC();
Felipe Balbi743411b2010-12-01 13:22:05 +0200400}
401
402static int bfin_musb_init(struct musb *musb)
403{
404
405 /*
406 * Rev 1.0 BF549 EZ-KITs require PE7 to be high for both DEVICE
407 * and OTG HOST modes, while rev 1.1 and greater require PE7 to
408 * be low for DEVICE mode and high for HOST mode. We set it high
409 * here because we are in host mode
410 */
411
412 if (gpio_request(musb->config->gpio_vrsel, "USB_VRSEL")) {
413 printk(KERN_ERR "Failed ro request USB_VRSEL GPIO_%d\n",
414 musb->config->gpio_vrsel);
415 return -ENODEV;
416 }
417 gpio_direction_output(musb->config->gpio_vrsel, 0);
418
419 usb_nop_xceiv_register();
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +0530420 musb->xceiv = usb_get_phy(USB_PHY_TYPE_USB2);
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +0530421 if (IS_ERR_OR_NULL(musb->xceiv)) {
Felipe Balbi743411b2010-12-01 13:22:05 +0200422 gpio_free(musb->config->gpio_vrsel);
423 return -ENODEV;
424 }
425
426 bfin_musb_reg_init(musb);
Bryan Wu0c6a8812008-12-02 21:33:44 +0200427
428 if (is_host_enabled(musb)) {
Bryan Wu0c6a8812008-12-02 21:33:44 +0200429 setup_timer(&musb_conn_timer,
430 musb_conn_timer_handler, (unsigned long) musb);
431 }
432 if (is_peripheral_enabled(musb))
Felipe Balbi743411b2010-12-01 13:22:05 +0200433 musb->xceiv->set_power = bfin_musb_set_power;
Bryan Wu0c6a8812008-12-02 21:33:44 +0200434
435 musb->isr = blackfin_interrupt;
Felipe Balbi06624812011-01-21 13:39:20 +0800436 musb->double_buffer_not_ok = true;
Bryan Wu0c6a8812008-12-02 21:33:44 +0200437
438 return 0;
439}
440
Felipe Balbi743411b2010-12-01 13:22:05 +0200441static int bfin_musb_exit(struct musb *musb)
Bryan Wu0c6a8812008-12-02 21:33:44 +0200442{
Bryan Wu0c6a8812008-12-02 21:33:44 +0200443 gpio_free(musb->config->gpio_vrsel);
Bryan Wu0c6a8812008-12-02 21:33:44 +0200444
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +0530445 usb_put_phy(musb->xceiv);
Sergei Shtylyov3daad242010-09-29 09:54:30 +0300446 usb_nop_xceiv_unregister();
Bryan Wu0c6a8812008-12-02 21:33:44 +0200447 return 0;
448}
Felipe Balbi743411b2010-12-01 13:22:05 +0200449
Felipe Balbif7ec9432010-12-02 09:48:58 +0200450static const struct musb_platform_ops bfin_ops = {
Felipe Balbi743411b2010-12-01 13:22:05 +0200451 .init = bfin_musb_init,
452 .exit = bfin_musb_exit,
453
454 .enable = bfin_musb_enable,
455 .disable = bfin_musb_disable,
456
457 .set_mode = bfin_musb_set_mode,
458 .try_idle = bfin_musb_try_idle,
459
460 .vbus_status = bfin_musb_vbus_status,
461 .set_vbus = bfin_musb_set_vbus,
Mike Frysinger13254302011-03-30 22:48:54 -0400462
463 .adjust_channel_params = bfin_musb_adjust_channel_params,
Felipe Balbi743411b2010-12-01 13:22:05 +0200464};
Felipe Balbi9cb03082010-12-02 09:21:05 +0200465
466static u64 bfin_dmamask = DMA_BIT_MASK(32);
467
Felipe Balbie9e8c852012-01-26 12:40:23 +0200468static int __devinit bfin_probe(struct platform_device *pdev)
Felipe Balbi9cb03082010-12-02 09:21:05 +0200469{
470 struct musb_hdrc_platform_data *pdata = pdev->dev.platform_data;
471 struct platform_device *musb;
Felipe Balbia023c632010-12-02 09:42:50 +0200472 struct bfin_glue *glue;
Felipe Balbi9cb03082010-12-02 09:21:05 +0200473
474 int ret = -ENOMEM;
475
Felipe Balbia023c632010-12-02 09:42:50 +0200476 glue = kzalloc(sizeof(*glue), GFP_KERNEL);
477 if (!glue) {
478 dev_err(&pdev->dev, "failed to allocate glue context\n");
479 goto err0;
480 }
481
Felipe Balbi9cb03082010-12-02 09:21:05 +0200482 musb = platform_device_alloc("musb-hdrc", -1);
483 if (!musb) {
484 dev_err(&pdev->dev, "failed to allocate musb device\n");
Felipe Balbia023c632010-12-02 09:42:50 +0200485 goto err1;
Felipe Balbi9cb03082010-12-02 09:21:05 +0200486 }
487
488 musb->dev.parent = &pdev->dev;
489 musb->dev.dma_mask = &bfin_dmamask;
490 musb->dev.coherent_dma_mask = bfin_dmamask;
491
Felipe Balbia023c632010-12-02 09:42:50 +0200492 glue->dev = &pdev->dev;
493 glue->musb = musb;
494
Felipe Balbif7ec9432010-12-02 09:48:58 +0200495 pdata->platform_ops = &bfin_ops;
496
Felipe Balbia023c632010-12-02 09:42:50 +0200497 platform_set_drvdata(pdev, glue);
Felipe Balbi9cb03082010-12-02 09:21:05 +0200498
499 ret = platform_device_add_resources(musb, pdev->resource,
500 pdev->num_resources);
501 if (ret) {
502 dev_err(&pdev->dev, "failed to add resources\n");
Felipe Balbia023c632010-12-02 09:42:50 +0200503 goto err2;
Felipe Balbi9cb03082010-12-02 09:21:05 +0200504 }
505
506 ret = platform_device_add_data(musb, pdata, sizeof(*pdata));
507 if (ret) {
508 dev_err(&pdev->dev, "failed to add platform_data\n");
Felipe Balbia023c632010-12-02 09:42:50 +0200509 goto err2;
Felipe Balbi9cb03082010-12-02 09:21:05 +0200510 }
511
512 ret = platform_device_add(musb);
513 if (ret) {
514 dev_err(&pdev->dev, "failed to register musb device\n");
Felipe Balbia023c632010-12-02 09:42:50 +0200515 goto err2;
Felipe Balbi9cb03082010-12-02 09:21:05 +0200516 }
517
518 return 0;
519
Felipe Balbia023c632010-12-02 09:42:50 +0200520err2:
Felipe Balbi9cb03082010-12-02 09:21:05 +0200521 platform_device_put(musb);
522
Felipe Balbia023c632010-12-02 09:42:50 +0200523err1:
524 kfree(glue);
525
Felipe Balbi9cb03082010-12-02 09:21:05 +0200526err0:
527 return ret;
528}
529
Felipe Balbie9e8c852012-01-26 12:40:23 +0200530static int __devexit bfin_remove(struct platform_device *pdev)
Felipe Balbi9cb03082010-12-02 09:21:05 +0200531{
Felipe Balbia023c632010-12-02 09:42:50 +0200532 struct bfin_glue *glue = platform_get_drvdata(pdev);
Felipe Balbi9cb03082010-12-02 09:21:05 +0200533
Felipe Balbia023c632010-12-02 09:42:50 +0200534 platform_device_del(glue->musb);
535 platform_device_put(glue->musb);
536 kfree(glue);
Felipe Balbi9cb03082010-12-02 09:21:05 +0200537
538 return 0;
539}
540
Felipe Balbifcd22e32010-12-02 13:13:09 +0200541#ifdef CONFIG_PM
542static int bfin_suspend(struct device *dev)
543{
544 struct bfin_glue *glue = dev_get_drvdata(dev);
545 struct musb *musb = glue_to_musb(glue);
546
547 if (is_host_active(musb))
548 /*
549 * During hibernate gpio_vrsel will change from high to low
550 * low which will generate wakeup event resume the system
551 * immediately. Set it to 0 before hibernate to avoid this
552 * wakeup event.
553 */
554 gpio_set_value(musb->config->gpio_vrsel, 0);
555
556 return 0;
557}
558
559static int bfin_resume(struct device *dev)
560{
561 struct bfin_glue *glue = dev_get_drvdata(dev);
562 struct musb *musb = glue_to_musb(glue);
563
564 bfin_musb_reg_init(musb);
565
566 return 0;
567}
568
569static struct dev_pm_ops bfin_pm_ops = {
570 .suspend = bfin_suspend,
571 .resume = bfin_resume,
572};
573
Bob Liu8f7e7b82011-03-21 14:06:31 -0400574#define DEV_PM_OPS &bfin_pm_ops
Felipe Balbifcd22e32010-12-02 13:13:09 +0200575#else
576#define DEV_PM_OPS NULL
577#endif
578
Felipe Balbi9cb03082010-12-02 09:21:05 +0200579static struct platform_driver bfin_driver = {
Felipe Balbie9e8c852012-01-26 12:40:23 +0200580 .probe = bfin_probe,
Felipe Balbi9cb03082010-12-02 09:21:05 +0200581 .remove = __exit_p(bfin_remove),
582 .driver = {
Mike Frysinger417ddf82011-03-22 14:43:37 -0400583 .name = "musb-blackfin",
Felipe Balbifcd22e32010-12-02 13:13:09 +0200584 .pm = DEV_PM_OPS,
Felipe Balbi9cb03082010-12-02 09:21:05 +0200585 },
586};
587
588MODULE_DESCRIPTION("Blackfin MUSB Glue Layer");
589MODULE_AUTHOR("Bryan Wy <cooloney@kernel.org>");
590MODULE_LICENSE("GPL v2");
591
592static int __init bfin_init(void)
593{
Felipe Balbie9e8c852012-01-26 12:40:23 +0200594 return platform_driver_register(&bfin_driver);
Felipe Balbi9cb03082010-12-02 09:21:05 +0200595}
Felipe Balbie9e8c852012-01-26 12:40:23 +0200596module_init(bfin_init);
Felipe Balbi9cb03082010-12-02 09:21:05 +0200597
598static void __exit bfin_exit(void)
599{
600 platform_driver_unregister(&bfin_driver);
601}
602module_exit(bfin_exit);