blob: d40d5f0b5528b6a117e722e26f51a3a268839487 [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/list.h>
Bryan Wu0c6a8812008-12-02 21:33:44 +020015#include <linux/gpio.h>
16#include <linux/io.h>
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +053017#include <linux/err.h>
Felipe Balbi9cb03082010-12-02 09:21:05 +020018#include <linux/platform_device.h>
19#include <linux/dma-mapping.h>
Bob Liuad50c1b2011-08-05 17:33:05 +080020#include <linux/prefetch.h>
Felipe Balbid7078df2014-04-16 15:28:32 -050021#include <linux/usb/usb_phy_generic.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;
Felipe Balbi2f36ff62014-04-16 16:16:33 -050032 struct platform_device *phy;
Felipe Balbia023c632010-12-02 09:42:50 +020033};
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
Rahul Bedarkar5ae477b2014-01-02 19:27:47 +053080 /* Wait for complete */
Bryan Wu1c4bdc02009-12-21 09:49:52 -050081 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
Rahul Bedarkar5ae477b2014-01-02 19:27:47 +0530134 /* Wait for complete */
Bryan Wu1c4bdc02009-12-21 09:49:52 -0500135 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 */
Felipe Balbi032ec492011-11-24 15:46:26 +0200188 if ((musb->xceiv->state == OTG_STATE_B_IDLE
189 || musb->xceiv->state == OTG_STATE_A_WAIT_BCON) ||
Bob Liu68f64712010-10-23 05:12:00 -0500190 (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);
Felipe Balbi032ec492011-11-24 15:46:26 +0200232 musb->xceiv->state = OTG_STATE_B_IDLE;
Cliff Caiff927ad2010-03-25 13:25:19 +0200233 }
234 mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY);
235 break;
236 case OTG_STATE_B_IDLE:
Felipe Balbi032ec492011-11-24 15:46:26 +0200237 /*
238 * Start a new session. It seems that MUSB needs taking
Cliff Caiff927ad2010-03-25 13:25:19 +0200239 * some time to recognize the type of the plug inserted?
240 */
241 val = musb_readw(musb->mregs, MUSB_DEVCTL);
242 val |= MUSB_DEVCTL_SESSION;
243 musb_writew(musb->mregs, MUSB_DEVCTL, val);
244 val = musb_readw(musb->mregs, MUSB_DEVCTL);
245
Bryan Wu0c6a8812008-12-02 21:33:44 +0200246 if (!(val & MUSB_DEVCTL_BDEVICE)) {
247 gpio_set_value(musb->config->gpio_vrsel, 1);
David Brownell84e250f2009-03-31 12:30:04 -0700248 musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
Bryan Wu0c6a8812008-12-02 21:33:44 +0200249 } else {
250 gpio_set_value(musb->config->gpio_vrsel, 0);
251
252 /* Ignore VBUSERROR and SUSPEND IRQ */
253 val = musb_readb(musb->mregs, MUSB_INTRUSBE);
254 val &= ~MUSB_INTR_VBUSERROR;
255 musb_writeb(musb->mregs, MUSB_INTRUSBE, val);
256
257 val = MUSB_INTR_SUSPEND | MUSB_INTR_VBUSERROR;
258 musb_writeb(musb->mregs, MUSB_INTRUSB, val);
259
Cliff Caiff927ad2010-03-25 13:25:19 +0200260 /* Toggle the Soft Conn bit, so that we can response to
261 * the inserting of either A-plug or B-plug.
262 */
263 if (toggle) {
264 val = musb_readb(musb->mregs, MUSB_POWER);
265 val &= ~MUSB_POWER_SOFTCONN;
266 musb_writeb(musb->mregs, MUSB_POWER, val);
267 toggle = 0;
268 } else {
269 val = musb_readb(musb->mregs, MUSB_POWER);
270 val |= MUSB_POWER_SOFTCONN;
271 musb_writeb(musb->mregs, MUSB_POWER, val);
272 toggle = 1;
273 }
274 /* The delay time is set to 1/4 second by default,
275 * shortening it, if accelerating A-plug detection
276 * is needed in OTG mode.
277 */
278 mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY / 4);
Bryan Wu0c6a8812008-12-02 21:33:44 +0200279 }
Bryan Wu0c6a8812008-12-02 21:33:44 +0200280 break;
Bryan Wu0c6a8812008-12-02 21:33:44 +0200281 default:
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300282 dev_dbg(musb->controller, "%s state not handled\n",
Felipe Balbi42c0bf12013-03-07 10:39:57 +0200283 usb_otg_state_string(musb->xceiv->state));
Bryan Wu0c6a8812008-12-02 21:33:44 +0200284 break;
285 }
286 spin_unlock_irqrestore(&musb->lock, flags);
287
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300288 dev_dbg(musb->controller, "state is %s\n",
Felipe Balbi42c0bf12013-03-07 10:39:57 +0200289 usb_otg_state_string(musb->xceiv->state));
Bryan Wu0c6a8812008-12-02 21:33:44 +0200290}
291
Felipe Balbi743411b2010-12-01 13:22:05 +0200292static void bfin_musb_enable(struct musb *musb)
Bryan Wu0c6a8812008-12-02 21:33:44 +0200293{
Felipe Balbi032ec492011-11-24 15:46:26 +0200294 /* REVISIT is this really correct ? */
Bryan Wu0c6a8812008-12-02 21:33:44 +0200295}
296
Felipe Balbi743411b2010-12-01 13:22:05 +0200297static void bfin_musb_disable(struct musb *musb)
Bryan Wu0c6a8812008-12-02 21:33:44 +0200298{
299}
300
Felipe Balbi743411b2010-12-01 13:22:05 +0200301static void bfin_musb_set_vbus(struct musb *musb, int is_on)
Bryan Wu0c6a8812008-12-02 21:33:44 +0200302{
Cliff Cai6ddc6da2010-03-12 10:29:10 +0200303 int value = musb->config->gpio_vrsel_active;
304 if (!is_on)
305 value = !value;
306 gpio_set_value(musb->config->gpio_vrsel, value);
Bryan Wu0c6a8812008-12-02 21:33:44 +0200307
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300308 dev_dbg(musb->controller, "VBUS %s, devctl %02x "
Bryan Wu0c6a8812008-12-02 21:33:44 +0200309 /* otg %3x conf %08x prcm %08x */ "\n",
Felipe Balbi42c0bf12013-03-07 10:39:57 +0200310 usb_otg_state_string(musb->xceiv->state),
Bryan Wu0c6a8812008-12-02 21:33:44 +0200311 musb_readb(musb->mregs, MUSB_DEVCTL));
312}
313
Heikki Krogerus86753812012-02-13 13:24:02 +0200314static int bfin_musb_set_power(struct usb_phy *x, unsigned mA)
Bryan Wu0c6a8812008-12-02 21:33:44 +0200315{
316 return 0;
317}
318
Mike Frysinger45567c22011-03-21 14:06:32 -0400319static int bfin_musb_vbus_status(struct musb *musb)
Bryan Wu0c6a8812008-12-02 21:33:44 +0200320{
321 return 0;
322}
323
Felipe Balbi743411b2010-12-01 13:22:05 +0200324static int bfin_musb_set_mode(struct musb *musb, u8 musb_mode)
Bryan Wu0c6a8812008-12-02 21:33:44 +0200325{
Bryan Wu2002e762009-11-16 16:19:25 +0530326 return -EIO;
Bryan Wu0c6a8812008-12-02 21:33:44 +0200327}
328
Mike Frysinger13254302011-03-30 22:48:54 -0400329static int bfin_musb_adjust_channel_params(struct dma_channel *channel,
330 u16 packet_sz, u8 *mode,
331 dma_addr_t *dma_addr, u32 *len)
332{
333 struct musb_dma_channel *musb_channel = channel->private_data;
334
335 /*
336 * Anomaly 05000450 might cause data corruption when using DMA
337 * MODE 1 transmits with short packet. So to work around this,
338 * we truncate all MODE 1 transfers down to a multiple of the
339 * max packet size, and then do the last short packet transfer
340 * (if there is any) using MODE 0.
341 */
342 if (ANOMALY_05000450) {
343 if (musb_channel->transmit && *mode == 1)
344 *len = *len - (*len % packet_sz);
345 }
346
347 return 0;
348}
349
Felipe Balbi743411b2010-12-01 13:22:05 +0200350static void bfin_musb_reg_init(struct musb *musb)
Bryan Wu0c6a8812008-12-02 21:33:44 +0200351{
Robin Getzd426e602008-12-02 21:33:45 +0200352 if (ANOMALY_05000346) {
353 bfin_write_USB_APHY_CALIB(ANOMALY_05000346_value);
354 SSYNC();
355 }
Bryan Wu0c6a8812008-12-02 21:33:44 +0200356
Robin Getzd426e602008-12-02 21:33:45 +0200357 if (ANOMALY_05000347) {
358 bfin_write_USB_APHY_CNTRL(0x0);
359 SSYNC();
360 }
Bryan Wu0c6a8812008-12-02 21:33:44 +0200361
Bryan Wu0c6a8812008-12-02 21:33:44 +0200362 /* Configure PLL oscillator register */
Bob Liu9c756462010-10-23 05:12:01 -0500363 bfin_write_USB_PLLOSC_CTRL(0x3080 |
364 ((480/musb->config->clkin) << 1));
Bryan Wu0c6a8812008-12-02 21:33:44 +0200365 SSYNC();
366
367 bfin_write_USB_SRP_CLKDIV((get_sclk()/1000) / 32 - 1);
368 SSYNC();
369
370 bfin_write_USB_EP_NI0_RXMAXP(64);
371 SSYNC();
372
373 bfin_write_USB_EP_NI0_TXMAXP(64);
374 SSYNC();
375
376 /* Route INTRUSB/INTR_RX/INTR_TX to USB_INT0*/
377 bfin_write_USB_GLOBINTR(0x7);
378 SSYNC();
379
380 bfin_write_USB_GLOBAL_CTL(GLOBAL_ENA | EP1_TX_ENA | EP2_TX_ENA |
381 EP3_TX_ENA | EP4_TX_ENA | EP5_TX_ENA |
382 EP6_TX_ENA | EP7_TX_ENA | EP1_RX_ENA |
383 EP2_RX_ENA | EP3_RX_ENA | EP4_RX_ENA |
384 EP5_RX_ENA | EP6_RX_ENA | EP7_RX_ENA);
385 SSYNC();
Felipe Balbi743411b2010-12-01 13:22:05 +0200386}
387
388static int bfin_musb_init(struct musb *musb)
389{
390
391 /*
392 * Rev 1.0 BF549 EZ-KITs require PE7 to be high for both DEVICE
393 * and OTG HOST modes, while rev 1.1 and greater require PE7 to
394 * be low for DEVICE mode and high for HOST mode. We set it high
395 * here because we are in host mode
396 */
397
398 if (gpio_request(musb->config->gpio_vrsel, "USB_VRSEL")) {
399 printk(KERN_ERR "Failed ro request USB_VRSEL GPIO_%d\n",
400 musb->config->gpio_vrsel);
401 return -ENODEV;
402 }
403 gpio_direction_output(musb->config->gpio_vrsel, 0);
404
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);
Ming Lei25736e02013-01-04 23:13:58 +0800408 return -EPROBE_DEFER;
Felipe Balbi743411b2010-12-01 13:22:05 +0200409 }
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);
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +0530427 usb_put_phy(musb->xceiv);
Felipe Balbie741e632014-04-16 16:05:17 -0500428
Bryan Wu0c6a8812008-12-02 21:33:44 +0200429 return 0;
430}
Felipe Balbi743411b2010-12-01 13:22:05 +0200431
Felipe Balbif7ec9432010-12-02 09:48:58 +0200432static const struct musb_platform_ops bfin_ops = {
Felipe Balbi743411b2010-12-01 13:22:05 +0200433 .init = bfin_musb_init,
434 .exit = bfin_musb_exit,
435
436 .enable = bfin_musb_enable,
437 .disable = bfin_musb_disable,
438
439 .set_mode = bfin_musb_set_mode,
Felipe Balbi743411b2010-12-01 13:22:05 +0200440
441 .vbus_status = bfin_musb_vbus_status,
442 .set_vbus = bfin_musb_set_vbus,
Mike Frysinger13254302011-03-30 22:48:54 -0400443
444 .adjust_channel_params = bfin_musb_adjust_channel_params,
Felipe Balbi743411b2010-12-01 13:22:05 +0200445};
Felipe Balbi9cb03082010-12-02 09:21:05 +0200446
447static u64 bfin_dmamask = DMA_BIT_MASK(32);
448
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500449static int bfin_probe(struct platform_device *pdev)
Felipe Balbi9cb03082010-12-02 09:21:05 +0200450{
Felipe Balbi09fc7d22013-04-24 17:21:42 +0300451 struct resource musb_resources[2];
Jingoo Hanc1a7d672013-07-30 17:03:12 +0900452 struct musb_hdrc_platform_data *pdata = dev_get_platdata(&pdev->dev);
Felipe Balbi9cb03082010-12-02 09:21:05 +0200453 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
Sebastian Andrzej Siewior2f771162012-10-31 16:12:43 +0100464 musb = platform_device_alloc("musb-hdrc", PLATFORM_DEVID_AUTO);
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 Balbi2f36ff62014-04-16 16:16:33 -0500479 glue->phy = usb_phy_generic_register();
480 if (IS_ERR(glue->phy))
481 goto err2;
Felipe Balbia023c632010-12-02 09:42:50 +0200482 platform_set_drvdata(pdev, glue);
Felipe Balbi9cb03082010-12-02 09:21:05 +0200483
Felipe Balbi09fc7d22013-04-24 17:21:42 +0300484 memset(musb_resources, 0x00, sizeof(*musb_resources) *
485 ARRAY_SIZE(musb_resources));
486
487 musb_resources[0].name = pdev->resource[0].name;
488 musb_resources[0].start = pdev->resource[0].start;
489 musb_resources[0].end = pdev->resource[0].end;
490 musb_resources[0].flags = pdev->resource[0].flags;
491
492 musb_resources[1].name = pdev->resource[1].name;
493 musb_resources[1].start = pdev->resource[1].start;
494 musb_resources[1].end = pdev->resource[1].end;
495 musb_resources[1].flags = pdev->resource[1].flags;
496
497 ret = platform_device_add_resources(musb, musb_resources,
498 ARRAY_SIZE(musb_resources));
Felipe Balbi9cb03082010-12-02 09:21:05 +0200499 if (ret) {
500 dev_err(&pdev->dev, "failed to add resources\n");
B, Ravi65b3d522012-08-31 11:09:49 +0000501 goto err3;
Felipe Balbi9cb03082010-12-02 09:21:05 +0200502 }
503
504 ret = platform_device_add_data(musb, pdata, sizeof(*pdata));
505 if (ret) {
506 dev_err(&pdev->dev, "failed to add platform_data\n");
B, Ravi65b3d522012-08-31 11:09:49 +0000507 goto err3;
Felipe Balbi9cb03082010-12-02 09:21:05 +0200508 }
509
510 ret = platform_device_add(musb);
511 if (ret) {
512 dev_err(&pdev->dev, "failed to register musb device\n");
B, Ravi65b3d522012-08-31 11:09:49 +0000513 goto err3;
Felipe Balbi9cb03082010-12-02 09:21:05 +0200514 }
515
516 return 0;
517
B, Ravi65b3d522012-08-31 11:09:49 +0000518err3:
Felipe Balbi2f36ff62014-04-16 16:16:33 -0500519 usb_phy_generic_unregister(glue->phy);
520
521err2:
Felipe Balbi9cb03082010-12-02 09:21:05 +0200522 platform_device_put(musb);
523
Felipe Balbia023c632010-12-02 09:42:50 +0200524err1:
525 kfree(glue);
526
Felipe Balbi9cb03082010-12-02 09:21:05 +0200527err0:
528 return ret;
529}
530
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500531static int bfin_remove(struct platform_device *pdev)
Felipe Balbi9cb03082010-12-02 09:21:05 +0200532{
Felipe Balbia023c632010-12-02 09:42:50 +0200533 struct bfin_glue *glue = platform_get_drvdata(pdev);
Felipe Balbi9cb03082010-12-02 09:21:05 +0200534
Wei Yongjun01e40da2012-10-23 13:26:00 +0800535 platform_device_unregister(glue->musb);
Felipe Balbi2f36ff62014-04-16 16:16:33 -0500536 usb_phy_generic_unregister(glue->phy);
Felipe Balbia023c632010-12-02 09:42:50 +0200537 kfree(glue);
Felipe Balbi9cb03082010-12-02 09:21:05 +0200538
539 return 0;
540}
541
Felipe Balbifcd22e32010-12-02 13:13:09 +0200542#ifdef CONFIG_PM
543static int bfin_suspend(struct device *dev)
544{
545 struct bfin_glue *glue = dev_get_drvdata(dev);
546 struct musb *musb = glue_to_musb(glue);
547
548 if (is_host_active(musb))
549 /*
550 * During hibernate gpio_vrsel will change from high to low
551 * low which will generate wakeup event resume the system
552 * immediately. Set it to 0 before hibernate to avoid this
553 * wakeup event.
554 */
555 gpio_set_value(musb->config->gpio_vrsel, 0);
556
557 return 0;
558}
559
560static int bfin_resume(struct device *dev)
561{
562 struct bfin_glue *glue = dev_get_drvdata(dev);
563 struct musb *musb = glue_to_musb(glue);
564
565 bfin_musb_reg_init(musb);
566
567 return 0;
568}
Felipe Balbifcd22e32010-12-02 13:13:09 +0200569#endif
570
Daniel Mack09673132013-09-30 21:02:08 +0200571static SIMPLE_DEV_PM_OPS(bfin_pm_ops, bfin_suspend, bfin_resume);
572
Felipe Balbi9cb03082010-12-02 09:21:05 +0200573static struct platform_driver bfin_driver = {
Felipe Balbie9e8c852012-01-26 12:40:23 +0200574 .probe = bfin_probe,
Felipe Balbi9cb03082010-12-02 09:21:05 +0200575 .remove = __exit_p(bfin_remove),
576 .driver = {
Mike Frysinger417ddf82011-03-22 14:43:37 -0400577 .name = "musb-blackfin",
Daniel Mack09673132013-09-30 21:02:08 +0200578 .pm = &bfin_pm_ops,
Felipe Balbi9cb03082010-12-02 09:21:05 +0200579 },
580};
581
582MODULE_DESCRIPTION("Blackfin MUSB Glue Layer");
583MODULE_AUTHOR("Bryan Wy <cooloney@kernel.org>");
584MODULE_LICENSE("GPL v2");
Srinivas Kandagatla692373e2012-10-10 19:36:52 +0100585module_platform_driver(bfin_driver);