blob: 8c9c5fc3a6cad509874e51f782010f63c09b031b [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>
Felipe Balbi9cb03082010-12-02 09:21:05 +020018#include <linux/platform_device.h>
19#include <linux/dma-mapping.h>
Bryan Wu0c6a8812008-12-02 21:33:44 +020020
21#include <asm/cacheflush.h>
22
23#include "musb_core.h"
24#include "blackfin.h"
25
Felipe Balbia023c632010-12-02 09:42:50 +020026struct bfin_glue {
27 struct device *dev;
28 struct platform_device *musb;
29};
30
Bryan Wu0c6a8812008-12-02 21:33:44 +020031/*
32 * Load an endpoint's FIFO
33 */
34void musb_write_fifo(struct musb_hw_ep *hw_ep, u16 len, const u8 *src)
35{
36 void __iomem *fifo = hw_ep->fifo;
37 void __iomem *epio = hw_ep->regs;
Bryan Wu1c4bdc02009-12-21 09:49:52 -050038 u8 epnum = hw_ep->epnum;
Bryan Wu0c6a8812008-12-02 21:33:44 +020039
40 prefetch((u8 *)src);
41
42 musb_writew(epio, MUSB_TXCOUNT, len);
43
44 DBG(4, "TX ep%d fifo %p count %d buf %p, epio %p\n",
45 hw_ep->epnum, fifo, len, src, epio);
46
47 dump_fifo_data(src, len);
48
Bryan Wu1c4bdc02009-12-21 09:49:52 -050049 if (!ANOMALY_05000380 && epnum != 0) {
Bryan Wu1ca9e9c2009-12-28 13:40:39 +020050 u16 dma_reg;
51
52 flush_dcache_range((unsigned long)src,
53 (unsigned long)(src + len));
Bryan Wu0c6a8812008-12-02 21:33:44 +020054
Bryan Wu1c4bdc02009-12-21 09:49:52 -050055 /* Setup DMA address register */
Bryan Wu1ca9e9c2009-12-28 13:40:39 +020056 dma_reg = (u32)src;
Bryan Wu1c4bdc02009-12-21 09:49:52 -050057 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_ADDR_LOW), dma_reg);
58 SSYNC();
59
Bryan Wu1ca9e9c2009-12-28 13:40:39 +020060 dma_reg = (u32)src >> 16;
Bryan Wu1c4bdc02009-12-21 09:49:52 -050061 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_ADDR_HIGH), dma_reg);
62 SSYNC();
63
64 /* Setup DMA count register */
65 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_COUNT_LOW), len);
66 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_COUNT_HIGH), 0);
67 SSYNC();
68
69 /* Enable the DMA */
70 dma_reg = (epnum << 4) | DMA_ENA | INT_ENA | DIRECTION;
71 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_CTRL), dma_reg);
72 SSYNC();
73
74 /* Wait for compelete */
75 while (!(bfin_read_USB_DMA_INTERRUPT() & (1 << epnum)))
76 cpu_relax();
77
78 /* acknowledge dma interrupt */
79 bfin_write_USB_DMA_INTERRUPT(1 << epnum);
80 SSYNC();
81
82 /* Reset DMA */
83 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_CTRL), 0);
84 SSYNC();
85 } else {
86 SSYNC();
87
88 if (unlikely((unsigned long)src & 0x01))
Bryan Wu1ca9e9c2009-12-28 13:40:39 +020089 outsw_8((unsigned long)fifo, src, (len + 1) >> 1);
Bryan Wu1c4bdc02009-12-21 09:49:52 -050090 else
Bryan Wu1ca9e9c2009-12-28 13:40:39 +020091 outsw((unsigned long)fifo, src, (len + 1) >> 1);
Bryan Wu1c4bdc02009-12-21 09:49:52 -050092 }
93}
Bryan Wu0c6a8812008-12-02 21:33:44 +020094/*
95 * Unload an endpoint's FIFO
96 */
97void musb_read_fifo(struct musb_hw_ep *hw_ep, u16 len, u8 *dst)
98{
99 void __iomem *fifo = hw_ep->fifo;
100 u8 epnum = hw_ep->epnum;
Bryan Wu0c6a8812008-12-02 21:33:44 +0200101
Bryan Wu1c4bdc02009-12-21 09:49:52 -0500102 if (ANOMALY_05000467 && epnum != 0) {
Bryan Wu1ca9e9c2009-12-28 13:40:39 +0200103 u16 dma_reg;
Bryan Wu0c6a8812008-12-02 21:33:44 +0200104
Bryan Wu1ca9e9c2009-12-28 13:40:39 +0200105 invalidate_dcache_range((unsigned long)dst,
106 (unsigned long)(dst + len));
Bryan Wu0c6a8812008-12-02 21:33:44 +0200107
Bryan Wu1c4bdc02009-12-21 09:49:52 -0500108 /* Setup DMA address register */
Bryan Wu1ca9e9c2009-12-28 13:40:39 +0200109 dma_reg = (u32)dst;
Bryan Wu1c4bdc02009-12-21 09:49:52 -0500110 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_ADDR_LOW), dma_reg);
111 SSYNC();
Bryan Wu0c6a8812008-12-02 21:33:44 +0200112
Bryan Wu1ca9e9c2009-12-28 13:40:39 +0200113 dma_reg = (u32)dst >> 16;
Bryan Wu1c4bdc02009-12-21 09:49:52 -0500114 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_ADDR_HIGH), dma_reg);
115 SSYNC();
Bryan Wu0c6a8812008-12-02 21:33:44 +0200116
Bryan Wu1c4bdc02009-12-21 09:49:52 -0500117 /* Setup DMA count register */
118 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_COUNT_LOW), len);
119 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_COUNT_HIGH), 0);
120 SSYNC();
Bryan Wu0c6a8812008-12-02 21:33:44 +0200121
Bryan Wu1c4bdc02009-12-21 09:49:52 -0500122 /* Enable the DMA */
123 dma_reg = (epnum << 4) | DMA_ENA | INT_ENA;
124 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_CTRL), dma_reg);
125 SSYNC();
Bryan Wu0c6a8812008-12-02 21:33:44 +0200126
Bryan Wu1c4bdc02009-12-21 09:49:52 -0500127 /* Wait for compelete */
128 while (!(bfin_read_USB_DMA_INTERRUPT() & (1 << epnum)))
129 cpu_relax();
Bryan Wu0c6a8812008-12-02 21:33:44 +0200130
Bryan Wu1c4bdc02009-12-21 09:49:52 -0500131 /* acknowledge dma interrupt */
132 bfin_write_USB_DMA_INTERRUPT(1 << epnum);
133 SSYNC();
Bryan Wu0c6a8812008-12-02 21:33:44 +0200134
Bryan Wu1c4bdc02009-12-21 09:49:52 -0500135 /* Reset DMA */
136 bfin_write16(USB_DMA_REG(epnum, USB_DMAx_CTRL), 0);
137 SSYNC();
138 } else {
139 SSYNC();
140 /* Read the last byte of packet with odd size from address fifo + 4
141 * to trigger 1 byte access to EP0 FIFO.
142 */
143 if (len == 1)
144 *dst = (u8)inw((unsigned long)fifo + 4);
145 else {
146 if (unlikely((unsigned long)dst & 0x01))
147 insw_8((unsigned long)fifo, dst, len >> 1);
148 else
149 insw((unsigned long)fifo, dst, len >> 1);
150
151 if (len & 0x01)
152 *(dst + len - 1) = (u8)inw((unsigned long)fifo + 4);
153 }
154 }
Mike Frysinger04f40862009-11-16 16:19:19 +0530155 DBG(4, "%cX ep%d fifo %p count %d buf %p\n",
156 'R', hw_ep->epnum, fifo, len, dst);
157
Bryan Wu0c6a8812008-12-02 21:33:44 +0200158 dump_fifo_data(dst, len);
159}
160
161static irqreturn_t blackfin_interrupt(int irq, void *__hci)
162{
163 unsigned long flags;
164 irqreturn_t retval = IRQ_NONE;
165 struct musb *musb = __hci;
166
167 spin_lock_irqsave(&musb->lock, flags);
168
169 musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB);
170 musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX);
171 musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX);
172
173 if (musb->int_usb || musb->int_tx || musb->int_rx) {
174 musb_writeb(musb->mregs, MUSB_INTRUSB, musb->int_usb);
175 musb_writew(musb->mregs, MUSB_INTRTX, musb->int_tx);
176 musb_writew(musb->mregs, MUSB_INTRRX, musb->int_rx);
177 retval = musb_interrupt(musb);
178 }
179
Cliff Caiff927ad2010-03-25 13:25:19 +0200180 /* Start sampling ID pin, when plug is removed from MUSB */
181 if (is_otg_enabled(musb) && (musb->xceiv->state == OTG_STATE_B_IDLE
182 || musb->xceiv->state == OTG_STATE_A_WAIT_BCON)) {
183 mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY);
184 musb->a_wait_bcon = TIMER_DELAY;
185 }
186
Bryan Wu0c6a8812008-12-02 21:33:44 +0200187 spin_unlock_irqrestore(&musb->lock, flags);
188
Sergei Shtylyov2f831752010-03-25 13:14:25 +0200189 return retval;
Bryan Wu0c6a8812008-12-02 21:33:44 +0200190}
191
192static void musb_conn_timer_handler(unsigned long _musb)
193{
194 struct musb *musb = (void *)_musb;
195 unsigned long flags;
196 u16 val;
Cliff Caiff927ad2010-03-25 13:25:19 +0200197 static u8 toggle;
Bryan Wu0c6a8812008-12-02 21:33:44 +0200198
199 spin_lock_irqsave(&musb->lock, flags);
David Brownell84e250f2009-03-31 12:30:04 -0700200 switch (musb->xceiv->state) {
Bryan Wu0c6a8812008-12-02 21:33:44 +0200201 case OTG_STATE_A_IDLE:
202 case OTG_STATE_A_WAIT_BCON:
203 /* Start a new session */
204 val = musb_readw(musb->mregs, MUSB_DEVCTL);
Cliff Caiff927ad2010-03-25 13:25:19 +0200205 val &= ~MUSB_DEVCTL_SESSION;
206 musb_writew(musb->mregs, MUSB_DEVCTL, val);
Bryan Wu0c6a8812008-12-02 21:33:44 +0200207 val |= MUSB_DEVCTL_SESSION;
208 musb_writew(musb->mregs, MUSB_DEVCTL, val);
Cliff Caiff927ad2010-03-25 13:25:19 +0200209 /* Check if musb is host or peripheral. */
Bryan Wu0c6a8812008-12-02 21:33:44 +0200210 val = musb_readw(musb->mregs, MUSB_DEVCTL);
Cliff Caiff927ad2010-03-25 13:25:19 +0200211
212 if (!(val & MUSB_DEVCTL_BDEVICE)) {
213 gpio_set_value(musb->config->gpio_vrsel, 1);
214 musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
215 } else {
216 gpio_set_value(musb->config->gpio_vrsel, 0);
217 /* Ignore VBUSERROR and SUSPEND IRQ */
218 val = musb_readb(musb->mregs, MUSB_INTRUSBE);
219 val &= ~MUSB_INTR_VBUSERROR;
220 musb_writeb(musb->mregs, MUSB_INTRUSBE, val);
221
222 val = MUSB_INTR_SUSPEND | MUSB_INTR_VBUSERROR;
223 musb_writeb(musb->mregs, MUSB_INTRUSB, val);
224 if (is_otg_enabled(musb))
225 musb->xceiv->state = OTG_STATE_B_IDLE;
226 else
227 musb_writeb(musb->mregs, MUSB_POWER, MUSB_POWER_HSENAB);
228 }
229 mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY);
230 break;
231 case OTG_STATE_B_IDLE:
232
233 if (!is_peripheral_enabled(musb))
234 break;
235 /* Start a new session. It seems that MUSB needs taking
236 * some time to recognize the type of the plug inserted?
237 */
238 val = musb_readw(musb->mregs, MUSB_DEVCTL);
239 val |= MUSB_DEVCTL_SESSION;
240 musb_writew(musb->mregs, MUSB_DEVCTL, val);
241 val = musb_readw(musb->mregs, MUSB_DEVCTL);
242
Bryan Wu0c6a8812008-12-02 21:33:44 +0200243 if (!(val & MUSB_DEVCTL_BDEVICE)) {
244 gpio_set_value(musb->config->gpio_vrsel, 1);
David Brownell84e250f2009-03-31 12:30:04 -0700245 musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
Bryan Wu0c6a8812008-12-02 21:33:44 +0200246 } else {
247 gpio_set_value(musb->config->gpio_vrsel, 0);
248
249 /* Ignore VBUSERROR and SUSPEND IRQ */
250 val = musb_readb(musb->mregs, MUSB_INTRUSBE);
251 val &= ~MUSB_INTR_VBUSERROR;
252 musb_writeb(musb->mregs, MUSB_INTRUSBE, val);
253
254 val = MUSB_INTR_SUSPEND | MUSB_INTR_VBUSERROR;
255 musb_writeb(musb->mregs, MUSB_INTRUSB, val);
256
Cliff Caiff927ad2010-03-25 13:25:19 +0200257 /* Toggle the Soft Conn bit, so that we can response to
258 * the inserting of either A-plug or B-plug.
259 */
260 if (toggle) {
261 val = musb_readb(musb->mregs, MUSB_POWER);
262 val &= ~MUSB_POWER_SOFTCONN;
263 musb_writeb(musb->mregs, MUSB_POWER, val);
264 toggle = 0;
265 } else {
266 val = musb_readb(musb->mregs, MUSB_POWER);
267 val |= MUSB_POWER_SOFTCONN;
268 musb_writeb(musb->mregs, MUSB_POWER, val);
269 toggle = 1;
270 }
271 /* The delay time is set to 1/4 second by default,
272 * shortening it, if accelerating A-plug detection
273 * is needed in OTG mode.
274 */
275 mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY / 4);
Bryan Wu0c6a8812008-12-02 21:33:44 +0200276 }
Bryan Wu0c6a8812008-12-02 21:33:44 +0200277 break;
Bryan Wu0c6a8812008-12-02 21:33:44 +0200278 default:
279 DBG(1, "%s state not handled\n", otg_state_string(musb));
280 break;
281 }
282 spin_unlock_irqrestore(&musb->lock, flags);
283
284 DBG(4, "state is %s\n", otg_state_string(musb));
285}
286
Felipe Balbi743411b2010-12-01 13:22:05 +0200287static void bfin_musb_enable(struct musb *musb)
Bryan Wu0c6a8812008-12-02 21:33:44 +0200288{
Cliff Caiff927ad2010-03-25 13:25:19 +0200289 if (!is_otg_enabled(musb) && is_host_enabled(musb)) {
Bryan Wu0c6a8812008-12-02 21:33:44 +0200290 mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY);
291 musb->a_wait_bcon = TIMER_DELAY;
292 }
293}
294
Felipe Balbi743411b2010-12-01 13:22:05 +0200295static void bfin_musb_disable(struct musb *musb)
Bryan Wu0c6a8812008-12-02 21:33:44 +0200296{
297}
298
Felipe Balbi743411b2010-12-01 13:22:05 +0200299static void bfin_musb_set_vbus(struct musb *musb, int is_on)
Bryan Wu0c6a8812008-12-02 21:33:44 +0200300{
Cliff Cai6ddc6da2010-03-12 10:29:10 +0200301 int value = musb->config->gpio_vrsel_active;
302 if (!is_on)
303 value = !value;
304 gpio_set_value(musb->config->gpio_vrsel, value);
Bryan Wu0c6a8812008-12-02 21:33:44 +0200305
306 DBG(1, "VBUS %s, devctl %02x "
307 /* otg %3x conf %08x prcm %08x */ "\n",
308 otg_state_string(musb),
309 musb_readb(musb->mregs, MUSB_DEVCTL));
310}
311
Felipe Balbi743411b2010-12-01 13:22:05 +0200312static int bfin_musb_set_power(struct otg_transceiver *x, unsigned mA)
Bryan Wu0c6a8812008-12-02 21:33:44 +0200313{
314 return 0;
315}
316
Felipe Balbi743411b2010-12-01 13:22:05 +0200317static void bfin_musb_try_idle(struct musb *musb, unsigned long timeout)
Bryan Wu0c6a8812008-12-02 21:33:44 +0200318{
Cliff Caiff927ad2010-03-25 13:25:19 +0200319 if (!is_otg_enabled(musb) && is_host_enabled(musb))
Bryan Wu0c6a8812008-12-02 21:33:44 +0200320 mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY);
321}
322
Felipe Balbi743411b2010-12-01 13:22:05 +0200323static int bfin_musb_get_vbus_status(struct musb *musb)
Bryan Wu0c6a8812008-12-02 21:33:44 +0200324{
325 return 0;
326}
327
Felipe Balbi743411b2010-12-01 13:22:05 +0200328static int bfin_musb_set_mode(struct musb *musb, u8 musb_mode)
Bryan Wu0c6a8812008-12-02 21:33:44 +0200329{
Bryan Wu2002e762009-11-16 16:19:25 +0530330 return -EIO;
Bryan Wu0c6a8812008-12-02 21:33:44 +0200331}
332
Felipe Balbi743411b2010-12-01 13:22:05 +0200333static void bfin_musb_reg_init(struct musb *musb)
Bryan Wu0c6a8812008-12-02 21:33:44 +0200334{
Robin Getzd426e602008-12-02 21:33:45 +0200335 if (ANOMALY_05000346) {
336 bfin_write_USB_APHY_CALIB(ANOMALY_05000346_value);
337 SSYNC();
338 }
Bryan Wu0c6a8812008-12-02 21:33:44 +0200339
Robin Getzd426e602008-12-02 21:33:45 +0200340 if (ANOMALY_05000347) {
341 bfin_write_USB_APHY_CNTRL(0x0);
342 SSYNC();
343 }
Bryan Wu0c6a8812008-12-02 21:33:44 +0200344
Bryan Wu0c6a8812008-12-02 21:33:44 +0200345 /* Configure PLL oscillator register */
346 bfin_write_USB_PLLOSC_CTRL(0x30a8);
347 SSYNC();
348
349 bfin_write_USB_SRP_CLKDIV((get_sclk()/1000) / 32 - 1);
350 SSYNC();
351
352 bfin_write_USB_EP_NI0_RXMAXP(64);
353 SSYNC();
354
355 bfin_write_USB_EP_NI0_TXMAXP(64);
356 SSYNC();
357
358 /* Route INTRUSB/INTR_RX/INTR_TX to USB_INT0*/
359 bfin_write_USB_GLOBINTR(0x7);
360 SSYNC();
361
362 bfin_write_USB_GLOBAL_CTL(GLOBAL_ENA | EP1_TX_ENA | EP2_TX_ENA |
363 EP3_TX_ENA | EP4_TX_ENA | EP5_TX_ENA |
364 EP6_TX_ENA | EP7_TX_ENA | EP1_RX_ENA |
365 EP2_RX_ENA | EP3_RX_ENA | EP4_RX_ENA |
366 EP5_RX_ENA | EP6_RX_ENA | EP7_RX_ENA);
367 SSYNC();
Felipe Balbi743411b2010-12-01 13:22:05 +0200368}
369
370static int bfin_musb_init(struct musb *musb)
371{
372
373 /*
374 * Rev 1.0 BF549 EZ-KITs require PE7 to be high for both DEVICE
375 * and OTG HOST modes, while rev 1.1 and greater require PE7 to
376 * be low for DEVICE mode and high for HOST mode. We set it high
377 * here because we are in host mode
378 */
379
380 if (gpio_request(musb->config->gpio_vrsel, "USB_VRSEL")) {
381 printk(KERN_ERR "Failed ro request USB_VRSEL GPIO_%d\n",
382 musb->config->gpio_vrsel);
383 return -ENODEV;
384 }
385 gpio_direction_output(musb->config->gpio_vrsel, 0);
386
387 usb_nop_xceiv_register();
388 musb->xceiv = otg_get_transceiver();
389 if (!musb->xceiv) {
390 gpio_free(musb->config->gpio_vrsel);
391 return -ENODEV;
392 }
393
394 bfin_musb_reg_init(musb);
Bryan Wu0c6a8812008-12-02 21:33:44 +0200395
396 if (is_host_enabled(musb)) {
Felipe Balbi743411b2010-12-01 13:22:05 +0200397 musb->board_set_vbus = bfin_musb_set_vbus;
Bryan Wu0c6a8812008-12-02 21:33:44 +0200398 setup_timer(&musb_conn_timer,
399 musb_conn_timer_handler, (unsigned long) musb);
400 }
401 if (is_peripheral_enabled(musb))
Felipe Balbi743411b2010-12-01 13:22:05 +0200402 musb->xceiv->set_power = bfin_musb_set_power;
Bryan Wu0c6a8812008-12-02 21:33:44 +0200403
404 musb->isr = blackfin_interrupt;
405
406 return 0;
407}
408
Felipe Balbi49635142010-12-02 12:27:35 +0200409static int bfin_musb_suspend(struct musb *musb)
Felipe Balbi743411b2010-12-01 13:22:05 +0200410{
411 if (is_host_active(musb))
412 /*
413 * During hibernate gpio_vrsel will change from high to low
414 * low which will generate wakeup event resume the system
415 * immediately. Set it to 0 before hibernate to avoid this
416 * wakeup event.
417 */
418 gpio_set_value(musb->config->gpio_vrsel, 0);
Felipe Balbi49635142010-12-02 12:27:35 +0200419
420 return 0;
Felipe Balbi743411b2010-12-01 13:22:05 +0200421}
422
Felipe Balbi49635142010-12-02 12:27:35 +0200423static int bfin_musb_resume(struct musb *musb)
Felipe Balbi743411b2010-12-01 13:22:05 +0200424{
425 bfin_musb_reg_init(musb);
Felipe Balbi49635142010-12-02 12:27:35 +0200426
427 return 0;
Felipe Balbi743411b2010-12-01 13:22:05 +0200428}
Felipe Balbi743411b2010-12-01 13:22:05 +0200429
430static int bfin_musb_exit(struct musb *musb)
Bryan Wu0c6a8812008-12-02 21:33:44 +0200431{
Bryan Wu0c6a8812008-12-02 21:33:44 +0200432 gpio_free(musb->config->gpio_vrsel);
Bryan Wu0c6a8812008-12-02 21:33:44 +0200433
Sergei Shtylyovf4053872010-09-29 09:54:29 +0300434 otg_put_transceiver(musb->xceiv);
Sergei Shtylyov3daad242010-09-29 09:54:30 +0300435 usb_nop_xceiv_unregister();
Bryan Wu0c6a8812008-12-02 21:33:44 +0200436 return 0;
437}
Felipe Balbi743411b2010-12-01 13:22:05 +0200438
Felipe Balbif7ec9432010-12-02 09:48:58 +0200439static const struct musb_platform_ops bfin_ops = {
Felipe Balbi743411b2010-12-01 13:22:05 +0200440 .init = bfin_musb_init,
441 .exit = bfin_musb_exit,
442
443 .enable = bfin_musb_enable,
444 .disable = bfin_musb_disable,
445
446 .set_mode = bfin_musb_set_mode,
447 .try_idle = bfin_musb_try_idle,
448
Felipe Balbi49635142010-12-02 12:27:35 +0200449 .suspend = bfin_musb_suspend,
450 .resume = bfin_musb_resume,
451
Felipe Balbi743411b2010-12-01 13:22:05 +0200452 .vbus_status = bfin_musb_vbus_status,
453 .set_vbus = bfin_musb_set_vbus,
454};
Felipe Balbi9cb03082010-12-02 09:21:05 +0200455
456static u64 bfin_dmamask = DMA_BIT_MASK(32);
457
458static int __init bfin_probe(struct platform_device *pdev)
459{
460 struct musb_hdrc_platform_data *pdata = pdev->dev.platform_data;
461 struct platform_device *musb;
Felipe Balbia023c632010-12-02 09:42:50 +0200462 struct bfin_glue *glue;
Felipe Balbi9cb03082010-12-02 09:21:05 +0200463
464 int ret = -ENOMEM;
465
Felipe Balbia023c632010-12-02 09:42:50 +0200466 glue = kzalloc(sizeof(*glue), GFP_KERNEL);
467 if (!glue) {
468 dev_err(&pdev->dev, "failed to allocate glue context\n");
469 goto err0;
470 }
471
Felipe Balbi9cb03082010-12-02 09:21:05 +0200472 musb = platform_device_alloc("musb-hdrc", -1);
473 if (!musb) {
474 dev_err(&pdev->dev, "failed to allocate musb device\n");
Felipe Balbia023c632010-12-02 09:42:50 +0200475 goto err1;
Felipe Balbi9cb03082010-12-02 09:21:05 +0200476 }
477
478 musb->dev.parent = &pdev->dev;
479 musb->dev.dma_mask = &bfin_dmamask;
480 musb->dev.coherent_dma_mask = bfin_dmamask;
481
Felipe Balbia023c632010-12-02 09:42:50 +0200482 glue->dev = &pdev->dev;
483 glue->musb = musb;
484
Felipe Balbif7ec9432010-12-02 09:48:58 +0200485 pdata->platform_ops = &bfin_ops;
486
Felipe Balbia023c632010-12-02 09:42:50 +0200487 platform_set_drvdata(pdev, glue);
Felipe Balbi9cb03082010-12-02 09:21:05 +0200488
489 ret = platform_device_add_resources(musb, pdev->resource,
490 pdev->num_resources);
491 if (ret) {
492 dev_err(&pdev->dev, "failed to add resources\n");
Felipe Balbia023c632010-12-02 09:42:50 +0200493 goto err2;
Felipe Balbi9cb03082010-12-02 09:21:05 +0200494 }
495
496 ret = platform_device_add_data(musb, pdata, sizeof(*pdata));
497 if (ret) {
498 dev_err(&pdev->dev, "failed to add platform_data\n");
Felipe Balbia023c632010-12-02 09:42:50 +0200499 goto err2;
Felipe Balbi9cb03082010-12-02 09:21:05 +0200500 }
501
502 ret = platform_device_add(musb);
503 if (ret) {
504 dev_err(&pdev->dev, "failed to register musb device\n");
Felipe Balbia023c632010-12-02 09:42:50 +0200505 goto err2;
Felipe Balbi9cb03082010-12-02 09:21:05 +0200506 }
507
508 return 0;
509
Felipe Balbia023c632010-12-02 09:42:50 +0200510err2:
Felipe Balbi9cb03082010-12-02 09:21:05 +0200511 platform_device_put(musb);
512
Felipe Balbia023c632010-12-02 09:42:50 +0200513err1:
514 kfree(glue);
515
Felipe Balbi9cb03082010-12-02 09:21:05 +0200516err0:
517 return ret;
518}
519
520static int __exit bfin_remove(struct platform_device *pdev)
521{
Felipe Balbia023c632010-12-02 09:42:50 +0200522 struct bfin_glue *glue = platform_get_drvdata(pdev);
Felipe Balbi9cb03082010-12-02 09:21:05 +0200523
Felipe Balbia023c632010-12-02 09:42:50 +0200524 platform_device_del(glue->musb);
525 platform_device_put(glue->musb);
526 kfree(glue);
Felipe Balbi9cb03082010-12-02 09:21:05 +0200527
528 return 0;
529}
530
531static struct platform_driver bfin_driver = {
532 .remove = __exit_p(bfin_remove),
533 .driver = {
534 .name = "musb-bfin",
535 },
536};
537
538MODULE_DESCRIPTION("Blackfin MUSB Glue Layer");
539MODULE_AUTHOR("Bryan Wy <cooloney@kernel.org>");
540MODULE_LICENSE("GPL v2");
541
542static int __init bfin_init(void)
543{
544 return platform_driver_probe(&bfin_driver, bfin_probe);
545}
546subsys_initcall(bfin_init);
547
548static void __exit bfin_exit(void)
549{
550 platform_driver_unregister(&bfin_driver);
551}
552module_exit(bfin_exit);