blob: 9e3fe306301dcac6c6fe020f57bbe19e84a9f699 [file] [log] [blame]
Darius Augulis2a4f1362008-11-12 13:38:31 -08001/*
2 * driver/usb/gadget/imx_udc.c
3 *
4 * Copyright (C) 2005 Mike Lee(eemike@gmail.com)
5 * Copyright (C) 2008 Darius Augulis <augulis.darius@gmail.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 */
17
18#include <linux/init.h>
19#include <linux/kernel.h>
20#include <linux/platform_device.h>
21#include <linux/module.h>
22#include <linux/errno.h>
23#include <linux/list.h>
24#include <linux/interrupt.h>
25#include <linux/io.h>
26#include <linux/irq.h>
27#include <linux/device.h>
28#include <linux/dma-mapping.h>
29#include <linux/clk.h>
30#include <linux/delay.h>
31
32#include <linux/usb/ch9.h>
33#include <linux/usb/gadget.h>
34
35#include <mach/usb.h>
36#include <mach/hardware.h>
37
38#include "imx_udc.h"
39
40static const char driver_name[] = "imx_udc";
41static const char ep0name[] = "ep0";
42
43void ep0_chg_stat(const char *label, struct imx_udc_struct *imx_usb,
44 enum ep0_state stat);
45
46/*******************************************************************************
47 * IMX UDC hardware related functions
48 *******************************************************************************
49 */
50
51void imx_udc_enable(struct imx_udc_struct *imx_usb)
52{
53 int temp = __raw_readl(imx_usb->base + USB_CTRL);
54 __raw_writel(temp | CTRL_FE_ENA | CTRL_AFE_ENA, imx_usb->base + USB_CTRL);
55 imx_usb->gadget.speed = USB_SPEED_FULL;
56}
57
58void imx_udc_disable(struct imx_udc_struct *imx_usb)
59{
60 int temp = __raw_readl(imx_usb->base + USB_CTRL);
61
62 __raw_writel(temp & ~(CTRL_FE_ENA | CTRL_AFE_ENA),
63 imx_usb->base + USB_CTRL);
64
65 ep0_chg_stat(__func__, imx_usb, EP0_IDLE);
66 imx_usb->gadget.speed = USB_SPEED_UNKNOWN;
67}
68
69void imx_udc_reset(struct imx_udc_struct *imx_usb)
70{
71 int temp = __raw_readl(imx_usb->base + USB_ENAB);
72
73 /* set RST bit */
74 __raw_writel(temp | ENAB_RST, imx_usb->base + USB_ENAB);
75
76 /* wait RST bit to clear */
77 do {} while (__raw_readl(imx_usb->base + USB_ENAB) & ENAB_RST);
78
79 /* wait CFG bit to assert */
80 do {} while (!(__raw_readl(imx_usb->base + USB_DADR) & DADR_CFG));
81
82 /* udc module is now ready */
83}
84
85void imx_udc_config(struct imx_udc_struct *imx_usb)
86{
87 u8 ep_conf[5];
88 u8 i, j, cfg;
89 struct imx_ep_struct *imx_ep;
90
91 /* wait CFG bit to assert */
92 do {} while (!(__raw_readl(imx_usb->base + USB_DADR) & DADR_CFG));
93
94 /* Download the endpoint buffer for endpoint 0. */
95 for (j = 0; j < 5; j++) {
96 i = (j == 2 ? imx_usb->imx_ep[0].fifosize : 0x00);
97 __raw_writeb(i, imx_usb->base + USB_DDAT);
98 do {} while (__raw_readl(imx_usb->base + USB_DADR) & DADR_BSY);
99 }
100
101 /* Download the endpoint buffers for endpoints 1-5.
102 * We specify two configurations, one interface
103 */
104 for (cfg = 1; cfg < 3; cfg++) {
105 for (i = 1; i < IMX_USB_NB_EP; i++) {
106 imx_ep = &imx_usb->imx_ep[i];
107 /* EP no | Config no */
108 ep_conf[0] = (i << 4) | (cfg << 2);
109 /* Type | Direction */
110 ep_conf[1] = (imx_ep->bmAttributes << 3) |
111 (EP_DIR(imx_ep) << 2);
112 /* Max packet size */
113 ep_conf[2] = imx_ep->fifosize;
114 /* TRXTYP */
115 ep_conf[3] = 0xC0;
116 /* FIFO no */
117 ep_conf[4] = i;
118
119 D_INI(imx_usb->dev,
120 "<%s> ep%d_conf[%d]:"
121 "[%02x-%02x-%02x-%02x-%02x]\n",
122 __func__, i, cfg,
123 ep_conf[0], ep_conf[1], ep_conf[2],
124 ep_conf[3], ep_conf[4]);
125
126 for (j = 0; j < 5; j++) {
127 __raw_writeb(ep_conf[j],
128 imx_usb->base + USB_DDAT);
129 do {} while (__raw_readl(imx_usb->base + USB_DADR)
130 & DADR_BSY);
131 }
132 }
133 }
134
135 /* wait CFG bit to clear */
136 do {} while (__raw_readl(imx_usb->base + USB_DADR) & DADR_CFG);
137}
138
139void imx_udc_init_irq(struct imx_udc_struct *imx_usb)
140{
141 int i;
142
143 /* Mask and clear all irqs */
144 __raw_writel(0xFFFFFFFF, imx_usb->base + USB_MASK);
145 __raw_writel(0xFFFFFFFF, imx_usb->base + USB_INTR);
146 for (i = 0; i < IMX_USB_NB_EP; i++) {
147 __raw_writel(0x1FF, imx_usb->base + USB_EP_MASK(i));
148 __raw_writel(0x1FF, imx_usb->base + USB_EP_INTR(i));
149 }
150
151 /* Enable USB irqs */
152 __raw_writel(INTR_MSOF | INTR_FRAME_MATCH, imx_usb->base + USB_MASK);
153
154 /* Enable EP0 irqs */
155 __raw_writel(0x1FF & ~(EPINTR_DEVREQ | EPINTR_MDEVREQ | EPINTR_EOT
156 | EPINTR_EOF | EPINTR_FIFO_EMPTY | EPINTR_FIFO_FULL),
157 imx_usb->base + USB_EP_MASK(0));
158}
159
160void imx_udc_init_ep(struct imx_udc_struct *imx_usb)
161{
162 int i, max, temp;
163 struct imx_ep_struct *imx_ep;
164 for (i = 0; i < IMX_USB_NB_EP; i++) {
165 imx_ep = &imx_usb->imx_ep[i];
166 switch (imx_ep->fifosize) {
167 case 8:
168 max = 0;
169 break;
170 case 16:
171 max = 1;
172 break;
173 case 32:
174 max = 2;
175 break;
176 case 64:
177 max = 3;
178 break;
179 default:
180 max = 1;
181 break;
182 }
183 temp = (EP_DIR(imx_ep) << 7) | (max << 5)
184 | (imx_ep->bmAttributes << 3);
185 __raw_writel(temp, imx_usb->base + USB_EP_STAT(i));
186 __raw_writel(temp | EPSTAT_FLUSH, imx_usb->base + USB_EP_STAT(i));
187 D_INI(imx_usb->dev, "<%s> ep%d_stat %08x\n", __func__, i,
188 __raw_readl(imx_usb->base + USB_EP_STAT(i)));
189 }
190}
191
192void imx_udc_init_fifo(struct imx_udc_struct *imx_usb)
193{
194 int i, temp;
195 struct imx_ep_struct *imx_ep;
196 for (i = 0; i < IMX_USB_NB_EP; i++) {
197 imx_ep = &imx_usb->imx_ep[i];
198
199 /* Fifo control */
200 temp = EP_DIR(imx_ep) ? 0x0B000000 : 0x0F000000;
201 __raw_writel(temp, imx_usb->base + USB_EP_FCTRL(i));
202 D_INI(imx_usb->dev, "<%s> ep%d_fctrl %08x\n", __func__, i,
203 __raw_readl(imx_usb->base + USB_EP_FCTRL(i)));
204
205 /* Fifo alarm */
206 temp = (i ? imx_ep->fifosize / 2 : 0);
207 __raw_writel(temp, imx_usb->base + USB_EP_FALRM(i));
208 D_INI(imx_usb->dev, "<%s> ep%d_falrm %08x\n", __func__, i,
209 __raw_readl(imx_usb->base + USB_EP_FALRM(i)));
210 }
211}
212
213static void imx_udc_init(struct imx_udc_struct *imx_usb)
214{
215 /* Reset UDC */
216 imx_udc_reset(imx_usb);
217
218 /* Download config to enpoint buffer */
219 imx_udc_config(imx_usb);
220
221 /* Setup interrups */
222 imx_udc_init_irq(imx_usb);
223
224 /* Setup endpoints */
225 imx_udc_init_ep(imx_usb);
226
227 /* Setup fifos */
228 imx_udc_init_fifo(imx_usb);
229}
230
231void imx_ep_irq_enable(struct imx_ep_struct *imx_ep)
232{
233
234 int i = EP_NO(imx_ep);
235
236 __raw_writel(0x1FF, imx_ep->imx_usb->base + USB_EP_MASK(i));
237 __raw_writel(0x1FF, imx_ep->imx_usb->base + USB_EP_INTR(i));
238 __raw_writel(0x1FF & ~(EPINTR_EOT | EPINTR_EOF),
239 imx_ep->imx_usb->base + USB_EP_MASK(i));
240}
241
242void imx_ep_irq_disable(struct imx_ep_struct *imx_ep)
243{
244
245 int i = EP_NO(imx_ep);
246
247 __raw_writel(0x1FF, imx_ep->imx_usb->base + USB_EP_MASK(i));
248 __raw_writel(0x1FF, imx_ep->imx_usb->base + USB_EP_INTR(i));
249}
250
251int imx_ep_empty(struct imx_ep_struct *imx_ep)
252{
253 struct imx_udc_struct *imx_usb = imx_ep->imx_usb;
254
255 return __raw_readl(imx_usb->base + USB_EP_FSTAT(EP_NO(imx_ep)))
256 & FSTAT_EMPTY;
257}
258
259unsigned imx_fifo_bcount(struct imx_ep_struct *imx_ep)
260{
261 struct imx_udc_struct *imx_usb = imx_ep->imx_usb;
262
263 return (__raw_readl(imx_usb->base + USB_EP_STAT(EP_NO(imx_ep)))
264 & EPSTAT_BCOUNT) >> 16;
265}
266
267void imx_flush(struct imx_ep_struct *imx_ep)
268{
269 struct imx_udc_struct *imx_usb = imx_ep->imx_usb;
270
271 int temp = __raw_readl(imx_usb->base + USB_EP_STAT(EP_NO(imx_ep)));
272 __raw_writel(temp | EPSTAT_FLUSH,
273 imx_usb->base + USB_EP_STAT(EP_NO(imx_ep)));
274}
275
276void imx_ep_stall(struct imx_ep_struct *imx_ep)
277{
278 struct imx_udc_struct *imx_usb = imx_ep->imx_usb;
279 int temp, i;
280
281 D_ERR(imx_usb->dev, "<%s> Forced stall on %s\n", __func__, imx_ep->ep.name);
282
283 imx_flush(imx_ep);
284
285 /* Special care for ep0 */
Darius Augulis8f182e52009-01-21 15:17:25 +0200286 if (!EP_NO(imx_ep)) {
Darius Augulis2a4f1362008-11-12 13:38:31 -0800287 temp = __raw_readl(imx_usb->base + USB_CTRL);
288 __raw_writel(temp | CTRL_CMDOVER | CTRL_CMDERROR, imx_usb->base + USB_CTRL);
289 do { } while (__raw_readl(imx_usb->base + USB_CTRL) & CTRL_CMDOVER);
290 temp = __raw_readl(imx_usb->base + USB_CTRL);
291 __raw_writel(temp & ~CTRL_CMDERROR, imx_usb->base + USB_CTRL);
292 }
293 else {
294 temp = __raw_readl(imx_usb->base + USB_EP_STAT(EP_NO(imx_ep)));
295 __raw_writel(temp | EPSTAT_STALL,
296 imx_usb->base + USB_EP_STAT(EP_NO(imx_ep)));
297
298 for (i = 0; i < 100; i ++) {
299 temp = __raw_readl(imx_usb->base + USB_EP_STAT(EP_NO(imx_ep)));
Roel Kluin0df24792009-01-17 16:52:17 +0100300 if (!(temp & EPSTAT_STALL))
Darius Augulis2a4f1362008-11-12 13:38:31 -0800301 break;
302 udelay(20);
303 }
Darius Augulis8f182e52009-01-21 15:17:25 +0200304 if (i == 100)
Darius Augulis2a4f1362008-11-12 13:38:31 -0800305 D_ERR(imx_usb->dev, "<%s> Non finished stall on %s\n",
306 __func__, imx_ep->ep.name);
307 }
308}
309
310static int imx_udc_get_frame(struct usb_gadget *_gadget)
311{
312 struct imx_udc_struct *imx_usb = container_of(_gadget,
313 struct imx_udc_struct, gadget);
314
315 return __raw_readl(imx_usb->base + USB_FRAME) & 0x7FF;
316}
317
318static int imx_udc_wakeup(struct usb_gadget *_gadget)
319{
320 return 0;
321}
322
323/*******************************************************************************
324 * USB request control functions
325 *******************************************************************************
326 */
327
328static void ep_add_request(struct imx_ep_struct *imx_ep, struct imx_request *req)
329{
330 if (unlikely(!req))
331 return;
332
333 req->in_use = 1;
334 list_add_tail(&req->queue, &imx_ep->queue);
335}
336
337static void ep_del_request(struct imx_ep_struct *imx_ep, struct imx_request *req)
338{
339 if (unlikely(!req))
340 return;
341
342 list_del_init(&req->queue);
343 req->in_use = 0;
344}
345
346static void done(struct imx_ep_struct *imx_ep, struct imx_request *req, int status)
347{
348 ep_del_request(imx_ep, req);
349
350 if (likely(req->req.status == -EINPROGRESS))
351 req->req.status = status;
352 else
353 status = req->req.status;
354
355 if (status && status != -ESHUTDOWN)
356 D_ERR(imx_ep->imx_usb->dev,
357 "<%s> complete %s req %p stat %d len %u/%u\n", __func__,
358 imx_ep->ep.name, &req->req, status,
359 req->req.actual, req->req.length);
360
361 req->req.complete(&imx_ep->ep, &req->req);
362}
363
364static void nuke(struct imx_ep_struct *imx_ep, int status)
365{
366 struct imx_request *req;
367
368 while (!list_empty(&imx_ep->queue)) {
369 req = list_entry(imx_ep->queue.next, struct imx_request, queue);
370 done(imx_ep, req, status);
371 }
372}
373
374/*******************************************************************************
375 * Data tansfer over USB functions
376 *******************************************************************************
377 */
378static int read_packet(struct imx_ep_struct *imx_ep, struct imx_request *req)
379{
380 u8 *buf;
381 int bytes_ep, bufferspace, count, i;
382
383 bytes_ep = imx_fifo_bcount(imx_ep);
384 bufferspace = req->req.length - req->req.actual;
385
386 buf = req->req.buf + req->req.actual;
387 prefetchw(buf);
388
389 if (unlikely(imx_ep_empty(imx_ep)))
390 count = 0; /* zlp */
391 else
392 count = min(bytes_ep, bufferspace);
393
394 for (i = count; i > 0; i--)
395 *buf++ = __raw_readb(imx_ep->imx_usb->base
396 + USB_EP_FDAT0(EP_NO(imx_ep)));
397 req->req.actual += count;
398
399 return count;
400}
401
402static int write_packet(struct imx_ep_struct *imx_ep, struct imx_request *req)
403{
404 u8 *buf;
405 int length, count, temp;
406
407 buf = req->req.buf + req->req.actual;
408 prefetch(buf);
409
410 length = min(req->req.length - req->req.actual, (u32)imx_ep->fifosize);
411
412 if (imx_fifo_bcount(imx_ep) + length > imx_ep->fifosize) {
413 D_TRX(imx_ep->imx_usb->dev, "<%s> packet overfill %s fifo\n",
414 __func__, imx_ep->ep.name);
415 return -1;
416 }
417
418 req->req.actual += length;
419 count = length;
420
421 if (!count && req->req.zero) { /* zlp */
422 temp = __raw_readl(imx_ep->imx_usb->base
423 + USB_EP_STAT(EP_NO(imx_ep)));
424 __raw_writel(temp | EPSTAT_ZLPS, imx_ep->imx_usb->base
425 + USB_EP_STAT(EP_NO(imx_ep)));
426 D_TRX(imx_ep->imx_usb->dev, "<%s> zero packet\n", __func__);
427 return 0;
428 }
429
430 while (count--) {
431 if (count == 0) { /* last byte */
432 temp = __raw_readl(imx_ep->imx_usb->base
433 + USB_EP_FCTRL(EP_NO(imx_ep)));
434 __raw_writel(temp | FCTRL_WFR, imx_ep->imx_usb->base
435 + USB_EP_FCTRL(EP_NO(imx_ep)));
436 }
437 __raw_writeb(*buf++,
438 imx_ep->imx_usb->base + USB_EP_FDAT0(EP_NO(imx_ep)));
439 }
440
441 return length;
442}
443
444static int read_fifo(struct imx_ep_struct *imx_ep, struct imx_request *req)
445{
446 int bytes = 0,
447 count,
448 completed = 0;
449
450 while (__raw_readl(imx_ep->imx_usb->base + USB_EP_FSTAT(EP_NO(imx_ep)))
451 & FSTAT_FR) {
452 count = read_packet(imx_ep, req);
453 bytes += count;
454
455 completed = (count != imx_ep->fifosize);
456 if (completed || req->req.actual == req->req.length) {
457 completed = 1;
458 break;
459 }
460 }
461
462 if (completed || !req->req.length) {
463 done(imx_ep, req, 0);
464 D_REQ(imx_ep->imx_usb->dev, "<%s> %s req<%p> %s\n",
465 __func__, imx_ep->ep.name, req,
466 completed ? "completed" : "not completed");
467 if (!EP_NO(imx_ep))
468 ep0_chg_stat(__func__, imx_ep->imx_usb, EP0_IDLE);
469 }
470
471 D_TRX(imx_ep->imx_usb->dev, "<%s> bytes read: %d\n", __func__, bytes);
472
473 return completed;
474}
475
476static int write_fifo(struct imx_ep_struct *imx_ep, struct imx_request *req)
477{
478 int bytes = 0,
479 count,
480 completed = 0;
481
482 while (!completed) {
483 count = write_packet(imx_ep, req);
484 if (count < 0)
485 break; /* busy */
486 bytes += count;
487
488 /* last packet "must be" short (or a zlp) */
489 completed = (count != imx_ep->fifosize);
490
491 if (unlikely(completed)) {
492 done(imx_ep, req, 0);
493 D_REQ(imx_ep->imx_usb->dev, "<%s> %s req<%p> %s\n",
494 __func__, imx_ep->ep.name, req,
495 completed ? "completed" : "not completed");
496 if (!EP_NO(imx_ep))
497 ep0_chg_stat(__func__, imx_ep->imx_usb, EP0_IDLE);
498 }
499 }
500
501 D_TRX(imx_ep->imx_usb->dev, "<%s> bytes sent: %d\n", __func__, bytes);
502
503 return completed;
504}
505
506/*******************************************************************************
507 * Endpoint handlers
508 *******************************************************************************
509 */
510static int handle_ep(struct imx_ep_struct *imx_ep)
511{
512 struct imx_request *req;
513 int completed = 0;
514
515 do {
516 if (!list_empty(&imx_ep->queue))
517 req = list_entry(imx_ep->queue.next,
518 struct imx_request, queue);
519 else {
520 D_REQ(imx_ep->imx_usb->dev, "<%s> no request on %s\n",
521 __func__, imx_ep->ep.name);
522 return 0;
523 }
524
525 if (EP_DIR(imx_ep)) /* to host */
526 completed = write_fifo(imx_ep, req);
527 else /* to device */
528 completed = read_fifo(imx_ep, req);
529
530 dump_ep_stat(__func__, imx_ep);
531
532 } while (completed);
533
534 return 0;
535}
536
537static int handle_ep0(struct imx_ep_struct *imx_ep)
538{
539 struct imx_request *req = NULL;
540 int ret = 0;
541
Darius Augulis8f182e52009-01-21 15:17:25 +0200542 if (!list_empty(&imx_ep->queue)) {
Darius Augulis2a4f1362008-11-12 13:38:31 -0800543 req = list_entry(imx_ep->queue.next, struct imx_request, queue);
544
Darius Augulis2a4f1362008-11-12 13:38:31 -0800545 switch (imx_ep->imx_usb->ep0state) {
546
547 case EP0_IN_DATA_PHASE: /* GET_DESCRIPTOR */
548 write_fifo(imx_ep, req);
549 break;
550 case EP0_OUT_DATA_PHASE: /* SET_DESCRIPTOR */
551 read_fifo(imx_ep, req);
552 break;
553 default:
554 D_EP0(imx_ep->imx_usb->dev,
555 "<%s> ep0 i/o, odd state %d\n",
556 __func__, imx_ep->imx_usb->ep0state);
557 ep_del_request(imx_ep, req);
558 ret = -EL2HLT;
559 break;
560 }
561 }
562
Darius Augulis8f182e52009-01-21 15:17:25 +0200563 else
564 D_ERR(imx_ep->imx_usb->dev, "<%s> no request on %s\n",
565 __func__, imx_ep->ep.name);
566
Darius Augulis2a4f1362008-11-12 13:38:31 -0800567 return ret;
568}
569
570static void handle_ep0_devreq(struct imx_udc_struct *imx_usb)
571{
572 struct imx_ep_struct *imx_ep = &imx_usb->imx_ep[0];
573 union {
574 struct usb_ctrlrequest r;
575 u8 raw[8];
576 u32 word[2];
577 } u;
578 int temp, i;
579
580 nuke(imx_ep, -EPROTO);
581
582 /* read SETUP packet */
583 for (i = 0; i < 2; i++) {
584 if (imx_ep_empty(imx_ep)) {
585 D_ERR(imx_usb->dev,
586 "<%s> no setup packet received\n", __func__);
587 goto stall;
588 }
589 u.word[i] = __raw_readl(imx_usb->base + USB_EP_FDAT(EP_NO(imx_ep)));
590 }
591
592 temp = imx_ep_empty(imx_ep);
593 while (!imx_ep_empty(imx_ep)) {
594 i = __raw_readl(imx_usb->base + USB_EP_FDAT(EP_NO(imx_ep)));
595 D_ERR(imx_usb->dev,
596 "<%s> wrong to have extra bytes for setup : 0x%08x\n",
597 __func__, i);
598 }
599 if (!temp)
600 goto stall;
601
602 le16_to_cpus(&u.r.wValue);
603 le16_to_cpus(&u.r.wIndex);
604 le16_to_cpus(&u.r.wLength);
605
606 D_REQ(imx_usb->dev, "<%s> SETUP %02x.%02x v%04x i%04x l%04x\n",
607 __func__, u.r.bRequestType, u.r.bRequest,
608 u.r.wValue, u.r.wIndex, u.r.wLength);
609
610 if (imx_usb->set_config) {
611 /* NACK the host by using CMDOVER */
612 temp = __raw_readl(imx_usb->base + USB_CTRL);
613 __raw_writel(temp | CTRL_CMDOVER, imx_usb->base + USB_CTRL);
614
615 D_ERR(imx_usb->dev,
616 "<%s> set config req is pending, NACK the host\n",
617 __func__);
618 return;
619 }
620
621 if (u.r.bRequestType & USB_DIR_IN)
622 ep0_chg_stat(__func__, imx_usb, EP0_IN_DATA_PHASE);
623 else
624 ep0_chg_stat(__func__, imx_usb, EP0_OUT_DATA_PHASE);
625
626 i = imx_usb->driver->setup(&imx_usb->gadget, &u.r);
627 if (i < 0) {
628 D_ERR(imx_usb->dev, "<%s> device setup error %d\n",
629 __func__, i);
630 goto stall;
631 }
632
633 return;
634stall:
635 D_ERR(imx_usb->dev, "<%s> protocol STALL\n", __func__);
636 imx_ep_stall(imx_ep);
637 ep0_chg_stat(__func__, imx_usb, EP0_STALL);
638 return;
639}
640
641/*******************************************************************************
642 * USB gadget callback functions
643 *******************************************************************************
644 */
645
646static int imx_ep_enable(struct usb_ep *usb_ep,
647 const struct usb_endpoint_descriptor *desc)
648{
649 struct imx_ep_struct *imx_ep = container_of(usb_ep,
650 struct imx_ep_struct, ep);
651 struct imx_udc_struct *imx_usb = imx_ep->imx_usb;
652 unsigned long flags;
653
654 if (!usb_ep
655 || !desc
656 || !EP_NO(imx_ep)
657 || desc->bDescriptorType != USB_DT_ENDPOINT
658 || imx_ep->bEndpointAddress != desc->bEndpointAddress) {
659 D_ERR(imx_usb->dev,
660 "<%s> bad ep or descriptor\n", __func__);
661 return -EINVAL;
662 }
663
664 if (imx_ep->bmAttributes != desc->bmAttributes) {
665 D_ERR(imx_usb->dev,
666 "<%s> %s type mismatch\n", __func__, usb_ep->name);
667 return -EINVAL;
668 }
669
670 if (imx_ep->fifosize < le16_to_cpu(desc->wMaxPacketSize)) {
671 D_ERR(imx_usb->dev,
672 "<%s> bad %s maxpacket\n", __func__, usb_ep->name);
673 return -ERANGE;
674 }
675
676 if (!imx_usb->driver || imx_usb->gadget.speed == USB_SPEED_UNKNOWN) {
677 D_ERR(imx_usb->dev, "<%s> bogus device state\n", __func__);
678 return -ESHUTDOWN;
679 }
680
681 local_irq_save(flags);
682
683 imx_ep->stopped = 0;
684 imx_flush(imx_ep);
685 imx_ep_irq_enable(imx_ep);
686
687 local_irq_restore(flags);
688
689 D_EPX(imx_usb->dev, "<%s> ENABLED %s\n", __func__, usb_ep->name);
690 return 0;
691}
692
693static int imx_ep_disable(struct usb_ep *usb_ep)
694{
695 struct imx_ep_struct *imx_ep = container_of(usb_ep,
696 struct imx_ep_struct, ep);
697 unsigned long flags;
698
699 if (!usb_ep || !EP_NO(imx_ep) || !list_empty(&imx_ep->queue)) {
700 D_ERR(imx_ep->imx_usb->dev, "<%s> %s can not be disabled\n",
701 __func__, usb_ep ? imx_ep->ep.name : NULL);
702 return -EINVAL;
703 }
704
705 local_irq_save(flags);
706
707 imx_ep->stopped = 1;
708 nuke(imx_ep, -ESHUTDOWN);
709 imx_flush(imx_ep);
710 imx_ep_irq_disable(imx_ep);
711
712 local_irq_restore(flags);
713
714 D_EPX(imx_ep->imx_usb->dev,
715 "<%s> DISABLED %s\n", __func__, usb_ep->name);
716 return 0;
717}
718
719static struct usb_request *imx_ep_alloc_request
720 (struct usb_ep *usb_ep, gfp_t gfp_flags)
721{
722 struct imx_request *req;
723
724 req = kzalloc(sizeof *req, gfp_flags);
725 if (!req || !usb_ep)
726 return 0;
727
728 INIT_LIST_HEAD(&req->queue);
729 req->in_use = 0;
730
731 return &req->req;
732}
733
734static void imx_ep_free_request
735 (struct usb_ep *usb_ep, struct usb_request *usb_req)
736{
737 struct imx_request *req;
738
739 req = container_of(usb_req, struct imx_request, req);
740 WARN_ON(!list_empty(&req->queue));
741 kfree(req);
742}
743
744static int imx_ep_queue
745 (struct usb_ep *usb_ep, struct usb_request *usb_req, gfp_t gfp_flags)
746{
747 struct imx_ep_struct *imx_ep;
748 struct imx_udc_struct *imx_usb;
749 struct imx_request *req;
750 unsigned long flags;
751 int ret = 0;
752
753 imx_ep = container_of(usb_ep, struct imx_ep_struct, ep);
754 imx_usb = imx_ep->imx_usb;
755 req = container_of(usb_req, struct imx_request, req);
756
757 /*
758 Special care on IMX udc.
759 Ignore enqueue when after set configuration from the
760 host. This assume all gadget drivers reply set
761 configuration with the next ep0 req enqueue.
762 */
763 if (imx_usb->set_config && !EP_NO(imx_ep)) {
764 imx_usb->set_config = 0;
Darius Augulis8f182e52009-01-21 15:17:25 +0200765 D_ERR(imx_usb->dev,
Darius Augulis2a4f1362008-11-12 13:38:31 -0800766 "<%s> gadget reply set config\n", __func__);
767 return 0;
768 }
769
770 if (unlikely(!usb_req || !req || !usb_req->complete || !usb_req->buf)) {
771 D_ERR(imx_usb->dev, "<%s> bad params\n", __func__);
772 return -EINVAL;
773 }
774
775 if (unlikely(!usb_ep || !imx_ep)) {
776 D_ERR(imx_usb->dev, "<%s> bad ep\n", __func__);
777 return -EINVAL;
778 }
779
780 if (!imx_usb->driver || imx_usb->gadget.speed == USB_SPEED_UNKNOWN) {
781 D_ERR(imx_usb->dev, "<%s> bogus device state\n", __func__);
782 return -ESHUTDOWN;
783 }
784
Darius Augulis2a4f1362008-11-12 13:38:31 -0800785 /* Debug */
786 D_REQ(imx_usb->dev, "<%s> ep%d %s request for [%d] bytes\n",
787 __func__, EP_NO(imx_ep),
788 ((!EP_NO(imx_ep) && imx_ep->imx_usb->ep0state == EP0_IN_DATA_PHASE)
789 || (EP_NO(imx_ep) && EP_DIR(imx_ep))) ? "IN" : "OUT", usb_req->length);
790 dump_req(__func__, imx_ep, usb_req);
791
792 if (imx_ep->stopped) {
793 usb_req->status = -ESHUTDOWN;
Darius Augulis8f182e52009-01-21 15:17:25 +0200794 return -ESHUTDOWN;
Darius Augulis2a4f1362008-11-12 13:38:31 -0800795 }
796
797 if (req->in_use) {
798 D_ERR(imx_usb->dev,
799 "<%s> refusing to queue req %p (already queued)\n",
800 __func__, req);
Darius Augulis8f182e52009-01-21 15:17:25 +0200801 return 0;
Darius Augulis2a4f1362008-11-12 13:38:31 -0800802 }
803
Darius Augulis8f182e52009-01-21 15:17:25 +0200804 local_irq_save(flags);
805
Darius Augulis2a4f1362008-11-12 13:38:31 -0800806 usb_req->status = -EINPROGRESS;
807 usb_req->actual = 0;
808
809 ep_add_request(imx_ep, req);
810
811 if (!EP_NO(imx_ep))
812 ret = handle_ep0(imx_ep);
813 else
814 ret = handle_ep(imx_ep);
Darius Augulis8f182e52009-01-21 15:17:25 +0200815
Darius Augulis2a4f1362008-11-12 13:38:31 -0800816 local_irq_restore(flags);
817 return ret;
818}
819
820static int imx_ep_dequeue(struct usb_ep *usb_ep, struct usb_request *usb_req)
821{
822
823 struct imx_ep_struct *imx_ep = container_of
824 (usb_ep, struct imx_ep_struct, ep);
825 struct imx_request *req;
826 unsigned long flags;
827
828 if (unlikely(!usb_ep || !EP_NO(imx_ep))) {
829 D_ERR(imx_ep->imx_usb->dev, "<%s> bad ep\n", __func__);
830 return -EINVAL;
831 }
832
833 local_irq_save(flags);
834
835 /* make sure it's actually queued on this endpoint */
836 list_for_each_entry(req, &imx_ep->queue, queue) {
837 if (&req->req == usb_req)
838 break;
839 }
840 if (&req->req != usb_req) {
841 local_irq_restore(flags);
842 return -EINVAL;
843 }
844
845 done(imx_ep, req, -ECONNRESET);
846
847 local_irq_restore(flags);
848 return 0;
849}
850
851static int imx_ep_set_halt(struct usb_ep *usb_ep, int value)
852{
853 struct imx_ep_struct *imx_ep = container_of
854 (usb_ep, struct imx_ep_struct, ep);
855 unsigned long flags;
856
857 if (unlikely(!usb_ep || !EP_NO(imx_ep))) {
858 D_ERR(imx_ep->imx_usb->dev, "<%s> bad ep\n", __func__);
859 return -EINVAL;
860 }
861
862 local_irq_save(flags);
863
864 if ((imx_ep->bEndpointAddress & USB_DIR_IN)
865 && !list_empty(&imx_ep->queue)) {
866 local_irq_restore(flags);
867 return -EAGAIN;
868 }
869
870 imx_ep_stall(imx_ep);
871
872 local_irq_restore(flags);
873
874 D_EPX(imx_ep->imx_usb->dev, "<%s> %s halt\n", __func__, usb_ep->name);
875 return 0;
876}
877
878static int imx_ep_fifo_status(struct usb_ep *usb_ep)
879{
880 struct imx_ep_struct *imx_ep = container_of
881 (usb_ep, struct imx_ep_struct, ep);
882
883 if (!usb_ep) {
884 D_ERR(imx_ep->imx_usb->dev, "<%s> bad ep\n", __func__);
885 return -ENODEV;
886 }
887
888 if (imx_ep->imx_usb->gadget.speed == USB_SPEED_UNKNOWN)
889 return 0;
890 else
891 return imx_fifo_bcount(imx_ep);
892}
893
894static void imx_ep_fifo_flush(struct usb_ep *usb_ep)
895{
896 struct imx_ep_struct *imx_ep = container_of
897 (usb_ep, struct imx_ep_struct, ep);
898 unsigned long flags;
899
900 local_irq_save(flags);
901
902 if (!usb_ep || !EP_NO(imx_ep) || !list_empty(&imx_ep->queue)) {
903 D_ERR(imx_ep->imx_usb->dev, "<%s> bad ep\n", __func__);
904 local_irq_restore(flags);
905 return;
906 }
907
908 /* toggle and halt bits stay unchanged */
909 imx_flush(imx_ep);
910
911 local_irq_restore(flags);
912}
913
914static struct usb_ep_ops imx_ep_ops = {
915 .enable = imx_ep_enable,
916 .disable = imx_ep_disable,
917
918 .alloc_request = imx_ep_alloc_request,
919 .free_request = imx_ep_free_request,
920
921 .queue = imx_ep_queue,
922 .dequeue = imx_ep_dequeue,
923
924 .set_halt = imx_ep_set_halt,
925 .fifo_status = imx_ep_fifo_status,
926 .fifo_flush = imx_ep_fifo_flush,
927};
928
929/*******************************************************************************
930 * USB endpoint control functions
931 *******************************************************************************
932 */
933
934void ep0_chg_stat(const char *label,
935 struct imx_udc_struct *imx_usb, enum ep0_state stat)
936{
937 D_EP0(imx_usb->dev, "<%s> from %15s to %15s\n",
938 label, state_name[imx_usb->ep0state], state_name[stat]);
939
940 if (imx_usb->ep0state == stat)
941 return;
942
943 imx_usb->ep0state = stat;
944}
945
946static void usb_init_data(struct imx_udc_struct *imx_usb)
947{
948 struct imx_ep_struct *imx_ep;
949 u8 i;
950
951 /* device/ep0 records init */
952 INIT_LIST_HEAD(&imx_usb->gadget.ep_list);
953 INIT_LIST_HEAD(&imx_usb->gadget.ep0->ep_list);
954 ep0_chg_stat(__func__, imx_usb, EP0_IDLE);
955
956 /* basic endpoint records init */
957 for (i = 0; i < IMX_USB_NB_EP; i++) {
958 imx_ep = &imx_usb->imx_ep[i];
959
960 if (i) {
961 list_add_tail(&imx_ep->ep.ep_list,
962 &imx_usb->gadget.ep_list);
963 imx_ep->stopped = 1;
964 } else
965 imx_ep->stopped = 0;
966
967 INIT_LIST_HEAD(&imx_ep->queue);
968 }
969}
970
971static void udc_stop_activity(struct imx_udc_struct *imx_usb,
972 struct usb_gadget_driver *driver)
973{
974 struct imx_ep_struct *imx_ep;
975 int i;
976
977 if (imx_usb->gadget.speed == USB_SPEED_UNKNOWN)
978 driver = NULL;
979
980 /* prevent new request submissions, kill any outstanding requests */
981 for (i = 1; i < IMX_USB_NB_EP; i++) {
982 imx_ep = &imx_usb->imx_ep[i];
983 imx_flush(imx_ep);
984 imx_ep->stopped = 1;
985 imx_ep_irq_disable(imx_ep);
986 nuke(imx_ep, -ESHUTDOWN);
987 }
988
989 imx_usb->cfg = 0;
990 imx_usb->intf = 0;
991 imx_usb->alt = 0;
992
993 if (driver)
994 driver->disconnect(&imx_usb->gadget);
995}
996
997/*******************************************************************************
998 * Interrupt handlers
999 *******************************************************************************
1000 */
1001
1002static irqreturn_t imx_udc_irq(int irq, void *dev)
1003{
1004 struct imx_udc_struct *imx_usb = dev;
1005 struct usb_ctrlrequest u;
1006 int temp, cfg, intf, alt;
1007 int intr = __raw_readl(imx_usb->base + USB_INTR);
1008
1009 if (intr & (INTR_WAKEUP | INTR_SUSPEND | INTR_RESUME | INTR_RESET_START
1010 | INTR_RESET_STOP | INTR_CFG_CHG)) {
1011 dump_intr(__func__, intr, imx_usb->dev);
1012 dump_usb_stat(__func__, imx_usb);
1013 }
1014
Darius Augulis8f182e52009-01-21 15:17:25 +02001015 if (!imx_usb->driver)
Darius Augulis2a4f1362008-11-12 13:38:31 -08001016 goto end_irq;
Darius Augulis2a4f1362008-11-12 13:38:31 -08001017
1018 if (intr & INTR_WAKEUP) {
1019 if (imx_usb->gadget.speed == USB_SPEED_UNKNOWN
1020 && imx_usb->driver && imx_usb->driver->resume)
1021 imx_usb->driver->resume(&imx_usb->gadget);
1022 imx_usb->set_config = 0;
1023 imx_usb->gadget.speed = USB_SPEED_FULL;
1024 }
1025
1026 if (intr & INTR_SUSPEND) {
1027 if (imx_usb->gadget.speed != USB_SPEED_UNKNOWN
1028 && imx_usb->driver && imx_usb->driver->suspend)
1029 imx_usb->driver->suspend(&imx_usb->gadget);
1030 imx_usb->set_config = 0;
1031 imx_usb->gadget.speed = USB_SPEED_UNKNOWN;
1032 }
1033
1034 if (intr & INTR_RESET_START) {
1035 __raw_writel(intr, imx_usb->base + USB_INTR);
1036 udc_stop_activity(imx_usb, imx_usb->driver);
1037 imx_usb->set_config = 0;
1038 imx_usb->gadget.speed = USB_SPEED_UNKNOWN;
1039 }
1040
1041 if (intr & INTR_RESET_STOP)
1042 imx_usb->gadget.speed = USB_SPEED_FULL;
1043
1044 if (intr & INTR_CFG_CHG) {
1045 __raw_writel(INTR_CFG_CHG, imx_usb->base + USB_INTR);
1046 temp = __raw_readl(imx_usb->base + USB_STAT);
1047 cfg = (temp & STAT_CFG) >> 5;
1048 intf = (temp & STAT_INTF) >> 3;
1049 alt = temp & STAT_ALTSET;
1050
1051 D_REQ(imx_usb->dev,
1052 "<%s> orig config C=%d, I=%d, A=%d / "
1053 "req config C=%d, I=%d, A=%d\n",
1054 __func__, imx_usb->cfg, imx_usb->intf, imx_usb->alt,
1055 cfg, intf, alt);
1056
1057 if (cfg != 1 && cfg != 2)
1058 goto end_irq;
1059
1060 imx_usb->set_config = 0;
1061
1062 /* Config setup */
1063 if (imx_usb->cfg != cfg) {
1064 D_REQ(imx_usb->dev, "<%s> Change config start\n",__func__);
1065 u.bRequest = USB_REQ_SET_CONFIGURATION;
1066 u.bRequestType = USB_DIR_OUT |
1067 USB_TYPE_STANDARD |
1068 USB_RECIP_DEVICE;
1069 u.wValue = cfg;
1070 u.wIndex = 0;
1071 u.wLength = 0;
1072 imx_usb->cfg = cfg;
1073 imx_usb->set_config = 1;
1074 imx_usb->driver->setup(&imx_usb->gadget, &u);
1075 imx_usb->set_config = 0;
1076 D_REQ(imx_usb->dev, "<%s> Change config done\n",__func__);
1077
1078 }
1079 if (imx_usb->intf != intf || imx_usb->alt != alt) {
1080 D_REQ(imx_usb->dev, "<%s> Change interface start\n",__func__);
1081 u.bRequest = USB_REQ_SET_INTERFACE;
1082 u.bRequestType = USB_DIR_OUT |
1083 USB_TYPE_STANDARD |
1084 USB_RECIP_INTERFACE;
1085 u.wValue = alt;
1086 u.wIndex = intf;
1087 u.wLength = 0;
1088 imx_usb->intf = intf;
1089 imx_usb->alt = alt;
1090 imx_usb->set_config = 1;
1091 imx_usb->driver->setup(&imx_usb->gadget, &u);
1092 imx_usb->set_config = 0;
1093 D_REQ(imx_usb->dev, "<%s> Change interface done\n",__func__);
1094 }
1095 }
1096
1097 if (intr & INTR_SOF) {
Darius Augulis8f182e52009-01-21 15:17:25 +02001098 /* Copy from Freescale BSP.
1099 We must enable SOF intr and set CMDOVER.
1100 Datasheet don't specifiy this action, but it
1101 is done in Freescale BSP, so just copy it.
1102 */
Darius Augulis2a4f1362008-11-12 13:38:31 -08001103 if (imx_usb->ep0state == EP0_IDLE) {
1104 temp = __raw_readl(imx_usb->base + USB_CTRL);
1105 __raw_writel(temp | CTRL_CMDOVER, imx_usb->base + USB_CTRL);
1106 }
1107 }
1108
1109end_irq:
1110 __raw_writel(intr, imx_usb->base + USB_INTR);
1111 return IRQ_HANDLED;
1112}
1113
1114static irqreturn_t imx_udc_ctrl_irq(int irq, void *dev)
1115{
1116 struct imx_udc_struct *imx_usb = dev;
1117 int intr = __raw_readl(imx_usb->base + USB_EP_INTR(0));
1118
1119 dump_ep_intr(__func__, 0, intr, imx_usb->dev);
1120
1121 if (!imx_usb->driver) {
1122 __raw_writel(intr, imx_usb->base + USB_EP_INTR(0));
1123 return IRQ_HANDLED;
1124 }
1125
1126 /* DEVREQ IRQ has highest priority */
1127 if (intr & (EPINTR_DEVREQ | EPINTR_MDEVREQ))
1128 handle_ep0_devreq(imx_usb);
1129 /* Seem i.MX is missing EOF interrupt sometimes.
1130 * Therefore we monitor both EOF and FIFO_EMPTY interrups
1131 * when transmiting, and both EOF and FIFO_FULL when
1132 * receiving data.
1133 */
1134 else if (intr & (EPINTR_EOF | EPINTR_FIFO_EMPTY | EPINTR_FIFO_FULL))
1135 handle_ep0(&imx_usb->imx_ep[0]);
1136
1137 __raw_writel(intr, imx_usb->base + USB_EP_INTR(0));
1138
1139 return IRQ_HANDLED;
1140}
1141
1142static irqreturn_t imx_udc_bulk_irq(int irq, void *dev)
1143{
1144 struct imx_udc_struct *imx_usb = dev;
1145 struct imx_ep_struct *imx_ep = &imx_usb->imx_ep[irq - USBD_INT0];
1146 int intr = __raw_readl(imx_usb->base + USB_EP_INTR(EP_NO(imx_ep)));
1147
1148 dump_ep_intr(__func__, irq - USBD_INT0, intr, imx_usb->dev);
1149
1150 if (!imx_usb->driver) {
1151 __raw_writel(intr, imx_usb->base + USB_EP_INTR(EP_NO(imx_ep)));
1152 return IRQ_HANDLED;
1153 }
1154
1155 handle_ep(imx_ep);
1156
1157 __raw_writel(intr, imx_usb->base + USB_EP_INTR(EP_NO(imx_ep)));
1158
1159 return IRQ_HANDLED;
1160}
1161
1162irq_handler_t intr_handler(int i)
1163{
1164 switch (i) {
1165 case 0:
1166 return imx_udc_ctrl_irq;
1167 case 1:
1168 case 2:
1169 case 3:
1170 case 4:
1171 case 5:
1172 return imx_udc_bulk_irq;
1173 default:
1174 return imx_udc_irq;
1175 }
1176}
1177
1178/*******************************************************************************
1179 * Static defined IMX UDC structure
1180 *******************************************************************************
1181 */
1182
1183static const struct usb_gadget_ops imx_udc_ops = {
1184 .get_frame = imx_udc_get_frame,
1185 .wakeup = imx_udc_wakeup,
1186};
1187
1188static struct imx_udc_struct controller = {
1189 .gadget = {
1190 .ops = &imx_udc_ops,
1191 .ep0 = &controller.imx_ep[0].ep,
1192 .name = driver_name,
1193 .dev = {
1194 .bus_id = "gadget",
1195 },
1196 },
1197
1198 .imx_ep[0] = {
1199 .ep = {
1200 .name = ep0name,
1201 .ops = &imx_ep_ops,
1202 .maxpacket = 32,
1203 },
1204 .imx_usb = &controller,
1205 .fifosize = 32,
1206 .bEndpointAddress = 0,
1207 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1208 },
1209 .imx_ep[1] = {
1210 .ep = {
1211 .name = "ep1in-bulk",
1212 .ops = &imx_ep_ops,
1213 .maxpacket = 64,
1214 },
1215 .imx_usb = &controller,
1216 .fifosize = 64,
1217 .bEndpointAddress = USB_DIR_IN | 1,
1218 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1219 },
1220 .imx_ep[2] = {
1221 .ep = {
1222 .name = "ep2out-bulk",
1223 .ops = &imx_ep_ops,
1224 .maxpacket = 64,
1225 },
1226 .imx_usb = &controller,
1227 .fifosize = 64,
1228 .bEndpointAddress = USB_DIR_OUT | 2,
1229 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1230 },
1231 .imx_ep[3] = {
1232 .ep = {
1233 .name = "ep3out-bulk",
1234 .ops = &imx_ep_ops,
1235 .maxpacket = 32,
1236 },
1237 .imx_usb = &controller,
1238 .fifosize = 32,
1239 .bEndpointAddress = USB_DIR_OUT | 3,
1240 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1241 },
1242 .imx_ep[4] = {
1243 .ep = {
1244 .name = "ep4in-int",
1245 .ops = &imx_ep_ops,
1246 .maxpacket = 32,
1247 },
1248 .imx_usb = &controller,
1249 .fifosize = 32,
1250 .bEndpointAddress = USB_DIR_IN | 4,
1251 .bmAttributes = USB_ENDPOINT_XFER_INT,
1252 },
1253 .imx_ep[5] = {
1254 .ep = {
1255 .name = "ep5out-int",
1256 .ops = &imx_ep_ops,
1257 .maxpacket = 32,
1258 },
1259 .imx_usb = &controller,
1260 .fifosize = 32,
1261 .bEndpointAddress = USB_DIR_OUT | 5,
1262 .bmAttributes = USB_ENDPOINT_XFER_INT,
1263 },
1264};
1265
1266/*******************************************************************************
1267 * USB gadged driver functions
1268 *******************************************************************************
1269 */
1270int usb_gadget_register_driver(struct usb_gadget_driver *driver)
1271{
1272 struct imx_udc_struct *imx_usb = &controller;
1273 int retval;
1274
1275 if (!driver
1276 || driver->speed < USB_SPEED_FULL
1277 || !driver->bind
1278 || !driver->disconnect
1279 || !driver->setup)
1280 return -EINVAL;
1281 if (!imx_usb)
1282 return -ENODEV;
1283 if (imx_usb->driver)
1284 return -EBUSY;
1285
1286 /* first hook up the driver ... */
1287 imx_usb->driver = driver;
1288 imx_usb->gadget.dev.driver = &driver->driver;
1289
1290 retval = device_add(&imx_usb->gadget.dev);
1291 if (retval)
1292 goto fail;
1293 retval = driver->bind(&imx_usb->gadget);
1294 if (retval) {
1295 D_ERR(imx_usb->dev, "<%s> bind to driver %s --> error %d\n",
1296 __func__, driver->driver.name, retval);
1297 device_del(&imx_usb->gadget.dev);
1298
1299 goto fail;
1300 }
1301
1302 D_INI(imx_usb->dev, "<%s> registered gadget driver '%s'\n",
1303 __func__, driver->driver.name);
1304
1305 imx_udc_enable(imx_usb);
1306
1307 return 0;
1308fail:
1309 imx_usb->driver = NULL;
1310 imx_usb->gadget.dev.driver = NULL;
1311 return retval;
1312}
1313EXPORT_SYMBOL(usb_gadget_register_driver);
1314
1315int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1316{
1317 struct imx_udc_struct *imx_usb = &controller;
1318
1319 if (!imx_usb)
1320 return -ENODEV;
1321 if (!driver || driver != imx_usb->driver || !driver->unbind)
1322 return -EINVAL;
1323
1324 udc_stop_activity(imx_usb, driver);
1325 imx_udc_disable(imx_usb);
1326
1327 driver->unbind(&imx_usb->gadget);
1328 imx_usb->gadget.dev.driver = NULL;
1329 imx_usb->driver = NULL;
1330
1331 device_del(&imx_usb->gadget.dev);
1332
1333 D_INI(imx_usb->dev, "<%s> unregistered gadget driver '%s'\n",
1334 __func__, driver->driver.name);
1335
1336 return 0;
1337}
1338EXPORT_SYMBOL(usb_gadget_unregister_driver);
1339
1340/*******************************************************************************
1341 * Module functions
1342 *******************************************************************************
1343 */
1344
1345static int __init imx_udc_probe(struct platform_device *pdev)
1346{
1347 struct imx_udc_struct *imx_usb = &controller;
1348 struct resource *res;
1349 struct imxusb_platform_data *pdata;
1350 struct clk *clk;
1351 void __iomem *base;
1352 int ret = 0;
1353 int i, res_size;
1354
1355 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1356 if (!res) {
1357 dev_err(&pdev->dev, "can't get device resources\n");
1358 return -ENODEV;
1359 }
1360
1361 pdata = pdev->dev.platform_data;
1362 if (!pdata) {
1363 dev_err(&pdev->dev, "driver needs platform data\n");
1364 return -ENODEV;
1365 }
1366
1367 res_size = res->end - res->start + 1;
1368 if (!request_mem_region(res->start, res_size, res->name)) {
1369 dev_err(&pdev->dev, "can't allocate %d bytes at %d address\n",
1370 res_size, res->start);
1371 return -ENOMEM;
1372 }
1373
1374 if (pdata->init) {
1375 ret = pdata->init(&pdev->dev);
1376 if (ret)
1377 goto fail0;
1378 }
1379
1380 base = ioremap(res->start, res_size);
1381 if (!base) {
1382 dev_err(&pdev->dev, "ioremap failed\n");
1383 ret = -EIO;
1384 goto fail1;
1385 }
1386
1387 clk = clk_get(NULL, "usbd_clk");
1388 if (IS_ERR(clk)) {
1389 ret = PTR_ERR(clk);
1390 dev_err(&pdev->dev, "can't get USB clock\n");
1391 goto fail2;
1392 }
1393 clk_enable(clk);
1394
1395 if (clk_get_rate(clk) != 48000000) {
1396 D_INI(&pdev->dev,
1397 "Bad USB clock (%d Hz), changing to 48000000 Hz\n",
1398 (int)clk_get_rate(clk));
1399 if (clk_set_rate(clk, 48000000)) {
1400 dev_err(&pdev->dev,
1401 "Unable to set correct USB clock (48MHz)\n");
1402 ret = -EIO;
1403 goto fail3;
1404 }
1405 }
1406
1407 for (i = 0; i < IMX_USB_NB_EP + 1; i++) {
1408 imx_usb->usbd_int[i] = platform_get_irq(pdev, i);
1409 if (imx_usb->usbd_int[i] < 0) {
1410 dev_err(&pdev->dev, "can't get irq number\n");
1411 ret = -ENODEV;
1412 goto fail3;
1413 }
1414 }
1415
1416 for (i = 0; i < IMX_USB_NB_EP + 1; i++) {
1417 ret = request_irq(imx_usb->usbd_int[i], intr_handler(i),
1418 IRQF_DISABLED, driver_name, imx_usb);
1419 if (ret) {
1420 dev_err(&pdev->dev, "can't get irq %i, err %d\n",
1421 imx_usb->usbd_int[i], ret);
1422 for (--i; i >= 0; i--)
1423 free_irq(imx_usb->usbd_int[i], imx_usb);
1424 goto fail3;
1425 }
1426 }
1427
1428 imx_usb->res = res;
1429 imx_usb->base = base;
1430 imx_usb->clk = clk;
1431 imx_usb->dev = &pdev->dev;
1432
1433 device_initialize(&imx_usb->gadget.dev);
1434
1435 imx_usb->gadget.dev.parent = &pdev->dev;
1436 imx_usb->gadget.dev.dma_mask = pdev->dev.dma_mask;
1437
1438 platform_set_drvdata(pdev, imx_usb);
1439
1440 usb_init_data(imx_usb);
1441 imx_udc_init(imx_usb);
1442
1443 return 0;
1444
1445fail3:
1446 clk_put(clk);
1447 clk_disable(clk);
1448fail2:
1449 iounmap(base);
1450fail1:
1451 if (pdata->exit)
1452 pdata->exit(&pdev->dev);
1453fail0:
1454 release_mem_region(res->start, res_size);
1455 return ret;
1456}
1457
1458static int __exit imx_udc_remove(struct platform_device *pdev)
1459{
1460 struct imx_udc_struct *imx_usb = platform_get_drvdata(pdev);
1461 struct imxusb_platform_data *pdata = pdev->dev.platform_data;
1462 int i;
1463
1464 imx_udc_disable(imx_usb);
1465
1466 for (i = 0; i < IMX_USB_NB_EP + 1; i++)
1467 free_irq(imx_usb->usbd_int[i], imx_usb);
1468
1469 clk_put(imx_usb->clk);
1470 clk_disable(imx_usb->clk);
1471 iounmap(imx_usb->base);
1472
1473 release_mem_region(imx_usb->res->start,
1474 imx_usb->res->end - imx_usb->res->start + 1);
1475
1476 if (pdata->exit)
1477 pdata->exit(&pdev->dev);
1478
1479 platform_set_drvdata(pdev, NULL);
1480
1481 return 0;
1482}
1483
1484/*----------------------------------------------------------------------------*/
1485
1486#ifdef CONFIG_PM
1487#define imx_udc_suspend NULL
1488#define imx_udc_resume NULL
1489#else
1490#define imx_udc_suspend NULL
1491#define imx_udc_resume NULL
1492#endif
1493
1494/*----------------------------------------------------------------------------*/
1495
1496static struct platform_driver udc_driver = {
1497 .driver = {
1498 .name = driver_name,
1499 .owner = THIS_MODULE,
1500 },
1501 .remove = __exit_p(imx_udc_remove),
1502 .suspend = imx_udc_suspend,
1503 .resume = imx_udc_resume,
1504};
1505
1506static int __init udc_init(void)
1507{
1508 return platform_driver_probe(&udc_driver, imx_udc_probe);
1509}
1510module_init(udc_init);
1511
1512static void __exit udc_exit(void)
1513{
1514 platform_driver_unregister(&udc_driver);
1515}
1516module_exit(udc_exit);
1517
1518MODULE_DESCRIPTION("IMX USB Device Controller driver");
1519MODULE_AUTHOR("Darius Augulis <augulis.darius@gmail.com>");
1520MODULE_LICENSE("GPL");
1521MODULE_ALIAS("platform:imx_udc");