blob: 77c5d0a8a06ebdc42ec09970dc684c3f96ba2737 [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 */
286 if (EP_NO(imx_ep)) {
287 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 }
304 if (i == 50)
305 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
542 if (!list_empty(&imx_ep->queue))
543 req = list_entry(imx_ep->queue.next, struct imx_request, queue);
544
545 if (req) {
546 switch (imx_ep->imx_usb->ep0state) {
547
548 case EP0_IN_DATA_PHASE: /* GET_DESCRIPTOR */
549 write_fifo(imx_ep, req);
550 break;
551 case EP0_OUT_DATA_PHASE: /* SET_DESCRIPTOR */
552 read_fifo(imx_ep, req);
553 break;
554 default:
555 D_EP0(imx_ep->imx_usb->dev,
556 "<%s> ep0 i/o, odd state %d\n",
557 __func__, imx_ep->imx_usb->ep0state);
558 ep_del_request(imx_ep, req);
559 ret = -EL2HLT;
560 break;
561 }
562 }
563
564 return ret;
565}
566
567static void handle_ep0_devreq(struct imx_udc_struct *imx_usb)
568{
569 struct imx_ep_struct *imx_ep = &imx_usb->imx_ep[0];
570 union {
571 struct usb_ctrlrequest r;
572 u8 raw[8];
573 u32 word[2];
574 } u;
575 int temp, i;
576
577 nuke(imx_ep, -EPROTO);
578
579 /* read SETUP packet */
580 for (i = 0; i < 2; i++) {
581 if (imx_ep_empty(imx_ep)) {
582 D_ERR(imx_usb->dev,
583 "<%s> no setup packet received\n", __func__);
584 goto stall;
585 }
586 u.word[i] = __raw_readl(imx_usb->base + USB_EP_FDAT(EP_NO(imx_ep)));
587 }
588
589 temp = imx_ep_empty(imx_ep);
590 while (!imx_ep_empty(imx_ep)) {
591 i = __raw_readl(imx_usb->base + USB_EP_FDAT(EP_NO(imx_ep)));
592 D_ERR(imx_usb->dev,
593 "<%s> wrong to have extra bytes for setup : 0x%08x\n",
594 __func__, i);
595 }
596 if (!temp)
597 goto stall;
598
599 le16_to_cpus(&u.r.wValue);
600 le16_to_cpus(&u.r.wIndex);
601 le16_to_cpus(&u.r.wLength);
602
603 D_REQ(imx_usb->dev, "<%s> SETUP %02x.%02x v%04x i%04x l%04x\n",
604 __func__, u.r.bRequestType, u.r.bRequest,
605 u.r.wValue, u.r.wIndex, u.r.wLength);
606
607 if (imx_usb->set_config) {
608 /* NACK the host by using CMDOVER */
609 temp = __raw_readl(imx_usb->base + USB_CTRL);
610 __raw_writel(temp | CTRL_CMDOVER, imx_usb->base + USB_CTRL);
611
612 D_ERR(imx_usb->dev,
613 "<%s> set config req is pending, NACK the host\n",
614 __func__);
615 return;
616 }
617
618 if (u.r.bRequestType & USB_DIR_IN)
619 ep0_chg_stat(__func__, imx_usb, EP0_IN_DATA_PHASE);
620 else
621 ep0_chg_stat(__func__, imx_usb, EP0_OUT_DATA_PHASE);
622
623 i = imx_usb->driver->setup(&imx_usb->gadget, &u.r);
624 if (i < 0) {
625 D_ERR(imx_usb->dev, "<%s> device setup error %d\n",
626 __func__, i);
627 goto stall;
628 }
629
630 return;
631stall:
632 D_ERR(imx_usb->dev, "<%s> protocol STALL\n", __func__);
633 imx_ep_stall(imx_ep);
634 ep0_chg_stat(__func__, imx_usb, EP0_STALL);
635 return;
636}
637
638/*******************************************************************************
639 * USB gadget callback functions
640 *******************************************************************************
641 */
642
643static int imx_ep_enable(struct usb_ep *usb_ep,
644 const struct usb_endpoint_descriptor *desc)
645{
646 struct imx_ep_struct *imx_ep = container_of(usb_ep,
647 struct imx_ep_struct, ep);
648 struct imx_udc_struct *imx_usb = imx_ep->imx_usb;
649 unsigned long flags;
650
651 if (!usb_ep
652 || !desc
653 || !EP_NO(imx_ep)
654 || desc->bDescriptorType != USB_DT_ENDPOINT
655 || imx_ep->bEndpointAddress != desc->bEndpointAddress) {
656 D_ERR(imx_usb->dev,
657 "<%s> bad ep or descriptor\n", __func__);
658 return -EINVAL;
659 }
660
661 if (imx_ep->bmAttributes != desc->bmAttributes) {
662 D_ERR(imx_usb->dev,
663 "<%s> %s type mismatch\n", __func__, usb_ep->name);
664 return -EINVAL;
665 }
666
667 if (imx_ep->fifosize < le16_to_cpu(desc->wMaxPacketSize)) {
668 D_ERR(imx_usb->dev,
669 "<%s> bad %s maxpacket\n", __func__, usb_ep->name);
670 return -ERANGE;
671 }
672
673 if (!imx_usb->driver || imx_usb->gadget.speed == USB_SPEED_UNKNOWN) {
674 D_ERR(imx_usb->dev, "<%s> bogus device state\n", __func__);
675 return -ESHUTDOWN;
676 }
677
678 local_irq_save(flags);
679
680 imx_ep->stopped = 0;
681 imx_flush(imx_ep);
682 imx_ep_irq_enable(imx_ep);
683
684 local_irq_restore(flags);
685
686 D_EPX(imx_usb->dev, "<%s> ENABLED %s\n", __func__, usb_ep->name);
687 return 0;
688}
689
690static int imx_ep_disable(struct usb_ep *usb_ep)
691{
692 struct imx_ep_struct *imx_ep = container_of(usb_ep,
693 struct imx_ep_struct, ep);
694 unsigned long flags;
695
696 if (!usb_ep || !EP_NO(imx_ep) || !list_empty(&imx_ep->queue)) {
697 D_ERR(imx_ep->imx_usb->dev, "<%s> %s can not be disabled\n",
698 __func__, usb_ep ? imx_ep->ep.name : NULL);
699 return -EINVAL;
700 }
701
702 local_irq_save(flags);
703
704 imx_ep->stopped = 1;
705 nuke(imx_ep, -ESHUTDOWN);
706 imx_flush(imx_ep);
707 imx_ep_irq_disable(imx_ep);
708
709 local_irq_restore(flags);
710
711 D_EPX(imx_ep->imx_usb->dev,
712 "<%s> DISABLED %s\n", __func__, usb_ep->name);
713 return 0;
714}
715
716static struct usb_request *imx_ep_alloc_request
717 (struct usb_ep *usb_ep, gfp_t gfp_flags)
718{
719 struct imx_request *req;
720
721 req = kzalloc(sizeof *req, gfp_flags);
722 if (!req || !usb_ep)
723 return 0;
724
725 INIT_LIST_HEAD(&req->queue);
726 req->in_use = 0;
727
728 return &req->req;
729}
730
731static void imx_ep_free_request
732 (struct usb_ep *usb_ep, struct usb_request *usb_req)
733{
734 struct imx_request *req;
735
736 req = container_of(usb_req, struct imx_request, req);
737 WARN_ON(!list_empty(&req->queue));
738 kfree(req);
739}
740
741static int imx_ep_queue
742 (struct usb_ep *usb_ep, struct usb_request *usb_req, gfp_t gfp_flags)
743{
744 struct imx_ep_struct *imx_ep;
745 struct imx_udc_struct *imx_usb;
746 struct imx_request *req;
747 unsigned long flags;
748 int ret = 0;
749
750 imx_ep = container_of(usb_ep, struct imx_ep_struct, ep);
751 imx_usb = imx_ep->imx_usb;
752 req = container_of(usb_req, struct imx_request, req);
753
754 /*
755 Special care on IMX udc.
756 Ignore enqueue when after set configuration from the
757 host. This assume all gadget drivers reply set
758 configuration with the next ep0 req enqueue.
759 */
760 if (imx_usb->set_config && !EP_NO(imx_ep)) {
761 imx_usb->set_config = 0;
762 D_EPX(imx_usb->dev,
763 "<%s> gadget reply set config\n", __func__);
764 return 0;
765 }
766
767 if (unlikely(!usb_req || !req || !usb_req->complete || !usb_req->buf)) {
768 D_ERR(imx_usb->dev, "<%s> bad params\n", __func__);
769 return -EINVAL;
770 }
771
772 if (unlikely(!usb_ep || !imx_ep)) {
773 D_ERR(imx_usb->dev, "<%s> bad ep\n", __func__);
774 return -EINVAL;
775 }
776
777 if (!imx_usb->driver || imx_usb->gadget.speed == USB_SPEED_UNKNOWN) {
778 D_ERR(imx_usb->dev, "<%s> bogus device state\n", __func__);
779 return -ESHUTDOWN;
780 }
781
782 local_irq_save(flags);
783
784 /* Debug */
785 D_REQ(imx_usb->dev, "<%s> ep%d %s request for [%d] bytes\n",
786 __func__, EP_NO(imx_ep),
787 ((!EP_NO(imx_ep) && imx_ep->imx_usb->ep0state == EP0_IN_DATA_PHASE)
788 || (EP_NO(imx_ep) && EP_DIR(imx_ep))) ? "IN" : "OUT", usb_req->length);
789 dump_req(__func__, imx_ep, usb_req);
790
791 if (imx_ep->stopped) {
792 usb_req->status = -ESHUTDOWN;
793 ret = -ESHUTDOWN;
794 goto out;
795 }
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);
801 goto out;
802 }
803
804 usb_req->status = -EINPROGRESS;
805 usb_req->actual = 0;
806
807 ep_add_request(imx_ep, req);
808
809 if (!EP_NO(imx_ep))
810 ret = handle_ep0(imx_ep);
811 else
812 ret = handle_ep(imx_ep);
813out:
814 local_irq_restore(flags);
815 return ret;
816}
817
818static int imx_ep_dequeue(struct usb_ep *usb_ep, struct usb_request *usb_req)
819{
820
821 struct imx_ep_struct *imx_ep = container_of
822 (usb_ep, struct imx_ep_struct, ep);
823 struct imx_request *req;
824 unsigned long flags;
825
826 if (unlikely(!usb_ep || !EP_NO(imx_ep))) {
827 D_ERR(imx_ep->imx_usb->dev, "<%s> bad ep\n", __func__);
828 return -EINVAL;
829 }
830
831 local_irq_save(flags);
832
833 /* make sure it's actually queued on this endpoint */
834 list_for_each_entry(req, &imx_ep->queue, queue) {
835 if (&req->req == usb_req)
836 break;
837 }
838 if (&req->req != usb_req) {
839 local_irq_restore(flags);
840 return -EINVAL;
841 }
842
843 done(imx_ep, req, -ECONNRESET);
844
845 local_irq_restore(flags);
846 return 0;
847}
848
849static int imx_ep_set_halt(struct usb_ep *usb_ep, int value)
850{
851 struct imx_ep_struct *imx_ep = container_of
852 (usb_ep, struct imx_ep_struct, ep);
853 unsigned long flags;
854
855 if (unlikely(!usb_ep || !EP_NO(imx_ep))) {
856 D_ERR(imx_ep->imx_usb->dev, "<%s> bad ep\n", __func__);
857 return -EINVAL;
858 }
859
860 local_irq_save(flags);
861
862 if ((imx_ep->bEndpointAddress & USB_DIR_IN)
863 && !list_empty(&imx_ep->queue)) {
864 local_irq_restore(flags);
865 return -EAGAIN;
866 }
867
868 imx_ep_stall(imx_ep);
869
870 local_irq_restore(flags);
871
872 D_EPX(imx_ep->imx_usb->dev, "<%s> %s halt\n", __func__, usb_ep->name);
873 return 0;
874}
875
876static int imx_ep_fifo_status(struct usb_ep *usb_ep)
877{
878 struct imx_ep_struct *imx_ep = container_of
879 (usb_ep, struct imx_ep_struct, ep);
880
881 if (!usb_ep) {
882 D_ERR(imx_ep->imx_usb->dev, "<%s> bad ep\n", __func__);
883 return -ENODEV;
884 }
885
886 if (imx_ep->imx_usb->gadget.speed == USB_SPEED_UNKNOWN)
887 return 0;
888 else
889 return imx_fifo_bcount(imx_ep);
890}
891
892static void imx_ep_fifo_flush(struct usb_ep *usb_ep)
893{
894 struct imx_ep_struct *imx_ep = container_of
895 (usb_ep, struct imx_ep_struct, ep);
896 unsigned long flags;
897
898 local_irq_save(flags);
899
900 if (!usb_ep || !EP_NO(imx_ep) || !list_empty(&imx_ep->queue)) {
901 D_ERR(imx_ep->imx_usb->dev, "<%s> bad ep\n", __func__);
902 local_irq_restore(flags);
903 return;
904 }
905
906 /* toggle and halt bits stay unchanged */
907 imx_flush(imx_ep);
908
909 local_irq_restore(flags);
910}
911
912static struct usb_ep_ops imx_ep_ops = {
913 .enable = imx_ep_enable,
914 .disable = imx_ep_disable,
915
916 .alloc_request = imx_ep_alloc_request,
917 .free_request = imx_ep_free_request,
918
919 .queue = imx_ep_queue,
920 .dequeue = imx_ep_dequeue,
921
922 .set_halt = imx_ep_set_halt,
923 .fifo_status = imx_ep_fifo_status,
924 .fifo_flush = imx_ep_fifo_flush,
925};
926
927/*******************************************************************************
928 * USB endpoint control functions
929 *******************************************************************************
930 */
931
932void ep0_chg_stat(const char *label,
933 struct imx_udc_struct *imx_usb, enum ep0_state stat)
934{
935 D_EP0(imx_usb->dev, "<%s> from %15s to %15s\n",
936 label, state_name[imx_usb->ep0state], state_name[stat]);
937
938 if (imx_usb->ep0state == stat)
939 return;
940
941 imx_usb->ep0state = stat;
942}
943
944static void usb_init_data(struct imx_udc_struct *imx_usb)
945{
946 struct imx_ep_struct *imx_ep;
947 u8 i;
948
949 /* device/ep0 records init */
950 INIT_LIST_HEAD(&imx_usb->gadget.ep_list);
951 INIT_LIST_HEAD(&imx_usb->gadget.ep0->ep_list);
952 ep0_chg_stat(__func__, imx_usb, EP0_IDLE);
953
954 /* basic endpoint records init */
955 for (i = 0; i < IMX_USB_NB_EP; i++) {
956 imx_ep = &imx_usb->imx_ep[i];
957
958 if (i) {
959 list_add_tail(&imx_ep->ep.ep_list,
960 &imx_usb->gadget.ep_list);
961 imx_ep->stopped = 1;
962 } else
963 imx_ep->stopped = 0;
964
965 INIT_LIST_HEAD(&imx_ep->queue);
966 }
967}
968
969static void udc_stop_activity(struct imx_udc_struct *imx_usb,
970 struct usb_gadget_driver *driver)
971{
972 struct imx_ep_struct *imx_ep;
973 int i;
974
975 if (imx_usb->gadget.speed == USB_SPEED_UNKNOWN)
976 driver = NULL;
977
978 /* prevent new request submissions, kill any outstanding requests */
979 for (i = 1; i < IMX_USB_NB_EP; i++) {
980 imx_ep = &imx_usb->imx_ep[i];
981 imx_flush(imx_ep);
982 imx_ep->stopped = 1;
983 imx_ep_irq_disable(imx_ep);
984 nuke(imx_ep, -ESHUTDOWN);
985 }
986
987 imx_usb->cfg = 0;
988 imx_usb->intf = 0;
989 imx_usb->alt = 0;
990
991 if (driver)
992 driver->disconnect(&imx_usb->gadget);
993}
994
995/*******************************************************************************
996 * Interrupt handlers
997 *******************************************************************************
998 */
999
1000static irqreturn_t imx_udc_irq(int irq, void *dev)
1001{
1002 struct imx_udc_struct *imx_usb = dev;
1003 struct usb_ctrlrequest u;
1004 int temp, cfg, intf, alt;
1005 int intr = __raw_readl(imx_usb->base + USB_INTR);
1006
1007 if (intr & (INTR_WAKEUP | INTR_SUSPEND | INTR_RESUME | INTR_RESET_START
1008 | INTR_RESET_STOP | INTR_CFG_CHG)) {
1009 dump_intr(__func__, intr, imx_usb->dev);
1010 dump_usb_stat(__func__, imx_usb);
1011 }
1012
1013 if (!imx_usb->driver) {
1014 /*imx_udc_disable(imx_usb);*/
1015 goto end_irq;
1016 }
1017
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) {
1098 if (imx_usb->ep0state == EP0_IDLE) {
1099 temp = __raw_readl(imx_usb->base + USB_CTRL);
1100 __raw_writel(temp | CTRL_CMDOVER, imx_usb->base + USB_CTRL);
1101 }
1102 }
1103
1104end_irq:
1105 __raw_writel(intr, imx_usb->base + USB_INTR);
1106 return IRQ_HANDLED;
1107}
1108
1109static irqreturn_t imx_udc_ctrl_irq(int irq, void *dev)
1110{
1111 struct imx_udc_struct *imx_usb = dev;
1112 int intr = __raw_readl(imx_usb->base + USB_EP_INTR(0));
1113
1114 dump_ep_intr(__func__, 0, intr, imx_usb->dev);
1115
1116 if (!imx_usb->driver) {
1117 __raw_writel(intr, imx_usb->base + USB_EP_INTR(0));
1118 return IRQ_HANDLED;
1119 }
1120
1121 /* DEVREQ IRQ has highest priority */
1122 if (intr & (EPINTR_DEVREQ | EPINTR_MDEVREQ))
1123 handle_ep0_devreq(imx_usb);
1124 /* Seem i.MX is missing EOF interrupt sometimes.
1125 * Therefore we monitor both EOF and FIFO_EMPTY interrups
1126 * when transmiting, and both EOF and FIFO_FULL when
1127 * receiving data.
1128 */
1129 else if (intr & (EPINTR_EOF | EPINTR_FIFO_EMPTY | EPINTR_FIFO_FULL))
1130 handle_ep0(&imx_usb->imx_ep[0]);
1131
1132 __raw_writel(intr, imx_usb->base + USB_EP_INTR(0));
1133
1134 return IRQ_HANDLED;
1135}
1136
1137static irqreturn_t imx_udc_bulk_irq(int irq, void *dev)
1138{
1139 struct imx_udc_struct *imx_usb = dev;
1140 struct imx_ep_struct *imx_ep = &imx_usb->imx_ep[irq - USBD_INT0];
1141 int intr = __raw_readl(imx_usb->base + USB_EP_INTR(EP_NO(imx_ep)));
1142
1143 dump_ep_intr(__func__, irq - USBD_INT0, intr, imx_usb->dev);
1144
1145 if (!imx_usb->driver) {
1146 __raw_writel(intr, imx_usb->base + USB_EP_INTR(EP_NO(imx_ep)));
1147 return IRQ_HANDLED;
1148 }
1149
1150 handle_ep(imx_ep);
1151
1152 __raw_writel(intr, imx_usb->base + USB_EP_INTR(EP_NO(imx_ep)));
1153
1154 return IRQ_HANDLED;
1155}
1156
1157irq_handler_t intr_handler(int i)
1158{
1159 switch (i) {
1160 case 0:
1161 return imx_udc_ctrl_irq;
1162 case 1:
1163 case 2:
1164 case 3:
1165 case 4:
1166 case 5:
1167 return imx_udc_bulk_irq;
1168 default:
1169 return imx_udc_irq;
1170 }
1171}
1172
1173/*******************************************************************************
1174 * Static defined IMX UDC structure
1175 *******************************************************************************
1176 */
1177
1178static const struct usb_gadget_ops imx_udc_ops = {
1179 .get_frame = imx_udc_get_frame,
1180 .wakeup = imx_udc_wakeup,
1181};
1182
1183static struct imx_udc_struct controller = {
1184 .gadget = {
1185 .ops = &imx_udc_ops,
1186 .ep0 = &controller.imx_ep[0].ep,
1187 .name = driver_name,
1188 .dev = {
1189 .bus_id = "gadget",
1190 },
1191 },
1192
1193 .imx_ep[0] = {
1194 .ep = {
1195 .name = ep0name,
1196 .ops = &imx_ep_ops,
1197 .maxpacket = 32,
1198 },
1199 .imx_usb = &controller,
1200 .fifosize = 32,
1201 .bEndpointAddress = 0,
1202 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1203 },
1204 .imx_ep[1] = {
1205 .ep = {
1206 .name = "ep1in-bulk",
1207 .ops = &imx_ep_ops,
1208 .maxpacket = 64,
1209 },
1210 .imx_usb = &controller,
1211 .fifosize = 64,
1212 .bEndpointAddress = USB_DIR_IN | 1,
1213 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1214 },
1215 .imx_ep[2] = {
1216 .ep = {
1217 .name = "ep2out-bulk",
1218 .ops = &imx_ep_ops,
1219 .maxpacket = 64,
1220 },
1221 .imx_usb = &controller,
1222 .fifosize = 64,
1223 .bEndpointAddress = USB_DIR_OUT | 2,
1224 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1225 },
1226 .imx_ep[3] = {
1227 .ep = {
1228 .name = "ep3out-bulk",
1229 .ops = &imx_ep_ops,
1230 .maxpacket = 32,
1231 },
1232 .imx_usb = &controller,
1233 .fifosize = 32,
1234 .bEndpointAddress = USB_DIR_OUT | 3,
1235 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1236 },
1237 .imx_ep[4] = {
1238 .ep = {
1239 .name = "ep4in-int",
1240 .ops = &imx_ep_ops,
1241 .maxpacket = 32,
1242 },
1243 .imx_usb = &controller,
1244 .fifosize = 32,
1245 .bEndpointAddress = USB_DIR_IN | 4,
1246 .bmAttributes = USB_ENDPOINT_XFER_INT,
1247 },
1248 .imx_ep[5] = {
1249 .ep = {
1250 .name = "ep5out-int",
1251 .ops = &imx_ep_ops,
1252 .maxpacket = 32,
1253 },
1254 .imx_usb = &controller,
1255 .fifosize = 32,
1256 .bEndpointAddress = USB_DIR_OUT | 5,
1257 .bmAttributes = USB_ENDPOINT_XFER_INT,
1258 },
1259};
1260
1261/*******************************************************************************
1262 * USB gadged driver functions
1263 *******************************************************************************
1264 */
1265int usb_gadget_register_driver(struct usb_gadget_driver *driver)
1266{
1267 struct imx_udc_struct *imx_usb = &controller;
1268 int retval;
1269
1270 if (!driver
1271 || driver->speed < USB_SPEED_FULL
1272 || !driver->bind
1273 || !driver->disconnect
1274 || !driver->setup)
1275 return -EINVAL;
1276 if (!imx_usb)
1277 return -ENODEV;
1278 if (imx_usb->driver)
1279 return -EBUSY;
1280
1281 /* first hook up the driver ... */
1282 imx_usb->driver = driver;
1283 imx_usb->gadget.dev.driver = &driver->driver;
1284
1285 retval = device_add(&imx_usb->gadget.dev);
1286 if (retval)
1287 goto fail;
1288 retval = driver->bind(&imx_usb->gadget);
1289 if (retval) {
1290 D_ERR(imx_usb->dev, "<%s> bind to driver %s --> error %d\n",
1291 __func__, driver->driver.name, retval);
1292 device_del(&imx_usb->gadget.dev);
1293
1294 goto fail;
1295 }
1296
1297 D_INI(imx_usb->dev, "<%s> registered gadget driver '%s'\n",
1298 __func__, driver->driver.name);
1299
1300 imx_udc_enable(imx_usb);
1301
1302 return 0;
1303fail:
1304 imx_usb->driver = NULL;
1305 imx_usb->gadget.dev.driver = NULL;
1306 return retval;
1307}
1308EXPORT_SYMBOL(usb_gadget_register_driver);
1309
1310int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1311{
1312 struct imx_udc_struct *imx_usb = &controller;
1313
1314 if (!imx_usb)
1315 return -ENODEV;
1316 if (!driver || driver != imx_usb->driver || !driver->unbind)
1317 return -EINVAL;
1318
1319 udc_stop_activity(imx_usb, driver);
1320 imx_udc_disable(imx_usb);
1321
1322 driver->unbind(&imx_usb->gadget);
1323 imx_usb->gadget.dev.driver = NULL;
1324 imx_usb->driver = NULL;
1325
1326 device_del(&imx_usb->gadget.dev);
1327
1328 D_INI(imx_usb->dev, "<%s> unregistered gadget driver '%s'\n",
1329 __func__, driver->driver.name);
1330
1331 return 0;
1332}
1333EXPORT_SYMBOL(usb_gadget_unregister_driver);
1334
1335/*******************************************************************************
1336 * Module functions
1337 *******************************************************************************
1338 */
1339
1340static int __init imx_udc_probe(struct platform_device *pdev)
1341{
1342 struct imx_udc_struct *imx_usb = &controller;
1343 struct resource *res;
1344 struct imxusb_platform_data *pdata;
1345 struct clk *clk;
1346 void __iomem *base;
1347 int ret = 0;
1348 int i, res_size;
1349
1350 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1351 if (!res) {
1352 dev_err(&pdev->dev, "can't get device resources\n");
1353 return -ENODEV;
1354 }
1355
1356 pdata = pdev->dev.platform_data;
1357 if (!pdata) {
1358 dev_err(&pdev->dev, "driver needs platform data\n");
1359 return -ENODEV;
1360 }
1361
1362 res_size = res->end - res->start + 1;
1363 if (!request_mem_region(res->start, res_size, res->name)) {
1364 dev_err(&pdev->dev, "can't allocate %d bytes at %d address\n",
1365 res_size, res->start);
1366 return -ENOMEM;
1367 }
1368
1369 if (pdata->init) {
1370 ret = pdata->init(&pdev->dev);
1371 if (ret)
1372 goto fail0;
1373 }
1374
1375 base = ioremap(res->start, res_size);
1376 if (!base) {
1377 dev_err(&pdev->dev, "ioremap failed\n");
1378 ret = -EIO;
1379 goto fail1;
1380 }
1381
1382 clk = clk_get(NULL, "usbd_clk");
1383 if (IS_ERR(clk)) {
1384 ret = PTR_ERR(clk);
1385 dev_err(&pdev->dev, "can't get USB clock\n");
1386 goto fail2;
1387 }
1388 clk_enable(clk);
1389
1390 if (clk_get_rate(clk) != 48000000) {
1391 D_INI(&pdev->dev,
1392 "Bad USB clock (%d Hz), changing to 48000000 Hz\n",
1393 (int)clk_get_rate(clk));
1394 if (clk_set_rate(clk, 48000000)) {
1395 dev_err(&pdev->dev,
1396 "Unable to set correct USB clock (48MHz)\n");
1397 ret = -EIO;
1398 goto fail3;
1399 }
1400 }
1401
1402 for (i = 0; i < IMX_USB_NB_EP + 1; i++) {
1403 imx_usb->usbd_int[i] = platform_get_irq(pdev, i);
1404 if (imx_usb->usbd_int[i] < 0) {
1405 dev_err(&pdev->dev, "can't get irq number\n");
1406 ret = -ENODEV;
1407 goto fail3;
1408 }
1409 }
1410
1411 for (i = 0; i < IMX_USB_NB_EP + 1; i++) {
1412 ret = request_irq(imx_usb->usbd_int[i], intr_handler(i),
1413 IRQF_DISABLED, driver_name, imx_usb);
1414 if (ret) {
1415 dev_err(&pdev->dev, "can't get irq %i, err %d\n",
1416 imx_usb->usbd_int[i], ret);
1417 for (--i; i >= 0; i--)
1418 free_irq(imx_usb->usbd_int[i], imx_usb);
1419 goto fail3;
1420 }
1421 }
1422
1423 imx_usb->res = res;
1424 imx_usb->base = base;
1425 imx_usb->clk = clk;
1426 imx_usb->dev = &pdev->dev;
1427
1428 device_initialize(&imx_usb->gadget.dev);
1429
1430 imx_usb->gadget.dev.parent = &pdev->dev;
1431 imx_usb->gadget.dev.dma_mask = pdev->dev.dma_mask;
1432
1433 platform_set_drvdata(pdev, imx_usb);
1434
1435 usb_init_data(imx_usb);
1436 imx_udc_init(imx_usb);
1437
1438 return 0;
1439
1440fail3:
1441 clk_put(clk);
1442 clk_disable(clk);
1443fail2:
1444 iounmap(base);
1445fail1:
1446 if (pdata->exit)
1447 pdata->exit(&pdev->dev);
1448fail0:
1449 release_mem_region(res->start, res_size);
1450 return ret;
1451}
1452
1453static int __exit imx_udc_remove(struct platform_device *pdev)
1454{
1455 struct imx_udc_struct *imx_usb = platform_get_drvdata(pdev);
1456 struct imxusb_platform_data *pdata = pdev->dev.platform_data;
1457 int i;
1458
1459 imx_udc_disable(imx_usb);
1460
1461 for (i = 0; i < IMX_USB_NB_EP + 1; i++)
1462 free_irq(imx_usb->usbd_int[i], imx_usb);
1463
1464 clk_put(imx_usb->clk);
1465 clk_disable(imx_usb->clk);
1466 iounmap(imx_usb->base);
1467
1468 release_mem_region(imx_usb->res->start,
1469 imx_usb->res->end - imx_usb->res->start + 1);
1470
1471 if (pdata->exit)
1472 pdata->exit(&pdev->dev);
1473
1474 platform_set_drvdata(pdev, NULL);
1475
1476 return 0;
1477}
1478
1479/*----------------------------------------------------------------------------*/
1480
1481#ifdef CONFIG_PM
1482#define imx_udc_suspend NULL
1483#define imx_udc_resume NULL
1484#else
1485#define imx_udc_suspend NULL
1486#define imx_udc_resume NULL
1487#endif
1488
1489/*----------------------------------------------------------------------------*/
1490
1491static struct platform_driver udc_driver = {
1492 .driver = {
1493 .name = driver_name,
1494 .owner = THIS_MODULE,
1495 },
1496 .remove = __exit_p(imx_udc_remove),
1497 .suspend = imx_udc_suspend,
1498 .resume = imx_udc_resume,
1499};
1500
1501static int __init udc_init(void)
1502{
1503 return platform_driver_probe(&udc_driver, imx_udc_probe);
1504}
1505module_init(udc_init);
1506
1507static void __exit udc_exit(void)
1508{
1509 platform_driver_unregister(&udc_driver);
1510}
1511module_exit(udc_exit);
1512
1513MODULE_DESCRIPTION("IMX USB Device Controller driver");
1514MODULE_AUTHOR("Darius Augulis <augulis.darius@gmail.com>");
1515MODULE_LICENSE("GPL");
1516MODULE_ALIAS("platform:imx_udc");