usb: gadget: mv_udc: don't check CONFIG_USB_MAX_CONTROLLER_COUNT
[platform/kernel/u-boot.git] / drivers / usb / gadget / mv_udc.c
1 /*
2  * Copyright 2011, Marvell Semiconductor Inc.
3  * Lei Wen <leiwen@marvell.com>
4  *
5  * SPDX-License-Identifier:     GPL-2.0+
6  *
7  * Back ported to the 8xx platform (from the 8260 platform) by
8  * Murray.Jensen@cmst.csiro.au, 27-Jan-01.
9  */
10
11 #include <common.h>
12 #include <command.h>
13 #include <config.h>
14 #include <net.h>
15 #include <malloc.h>
16 #include <asm/io.h>
17 #include <linux/types.h>
18 #include <usb/mv_udc.h>
19
20 /*
21  * Check if the system has too long cachelines. If the cachelines are
22  * longer then 128b, the driver will not be able flush/invalidate data
23  * cache over separate QH entries. We use 128b because one QH entry is
24  * 64b long and there are always two QH list entries for each endpoint.
25  */
26 #if ARCH_DMA_MINALIGN > 128
27 #error This driver can not work on systems with caches longer than 128b
28 #endif
29
30 #ifndef DEBUG
31 #define DBG(x...) do {} while (0)
32 #else
33 #define DBG(x...) printf(x)
34 static const char *reqname(unsigned r)
35 {
36         switch (r) {
37         case USB_REQ_GET_STATUS: return "GET_STATUS";
38         case USB_REQ_CLEAR_FEATURE: return "CLEAR_FEATURE";
39         case USB_REQ_SET_FEATURE: return "SET_FEATURE";
40         case USB_REQ_SET_ADDRESS: return "SET_ADDRESS";
41         case USB_REQ_GET_DESCRIPTOR: return "GET_DESCRIPTOR";
42         case USB_REQ_SET_DESCRIPTOR: return "SET_DESCRIPTOR";
43         case USB_REQ_GET_CONFIGURATION: return "GET_CONFIGURATION";
44         case USB_REQ_SET_CONFIGURATION: return "SET_CONFIGURATION";
45         case USB_REQ_GET_INTERFACE: return "GET_INTERFACE";
46         case USB_REQ_SET_INTERFACE: return "SET_INTERFACE";
47         default: return "*UNKNOWN*";
48         }
49 }
50 #endif
51
52 static struct usb_endpoint_descriptor ep0_out_desc = {
53         .bLength = sizeof(struct usb_endpoint_descriptor),
54         .bDescriptorType = USB_DT_ENDPOINT,
55         .bEndpointAddress = 0,
56         .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
57 };
58
59 static struct usb_endpoint_descriptor ep0_in_desc = {
60         .bLength = sizeof(struct usb_endpoint_descriptor),
61         .bDescriptorType = USB_DT_ENDPOINT,
62         .bEndpointAddress = USB_DIR_IN,
63         .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
64 };
65
66 static int mv_pullup(struct usb_gadget *gadget, int is_on);
67 static int mv_ep_enable(struct usb_ep *ep,
68                 const struct usb_endpoint_descriptor *desc);
69 static int mv_ep_disable(struct usb_ep *ep);
70 static int mv_ep_queue(struct usb_ep *ep,
71                 struct usb_request *req, gfp_t gfp_flags);
72 static struct usb_request *
73 mv_ep_alloc_request(struct usb_ep *ep, unsigned int gfp_flags);
74 static void mv_ep_free_request(struct usb_ep *ep, struct usb_request *_req);
75
76 static struct usb_gadget_ops mv_udc_ops = {
77         .pullup = mv_pullup,
78 };
79
80 static struct usb_ep_ops mv_ep_ops = {
81         .enable         = mv_ep_enable,
82         .disable        = mv_ep_disable,
83         .queue          = mv_ep_queue,
84         .alloc_request  = mv_ep_alloc_request,
85         .free_request   = mv_ep_free_request,
86 };
87
88 /* Init values for USB endpoints. */
89 static const struct usb_ep mv_ep_init[2] = {
90         [0] = { /* EP 0 */
91                 .maxpacket      = 64,
92                 .name           = "ep0",
93                 .ops            = &mv_ep_ops,
94         },
95         [1] = { /* EP 1..n */
96                 .maxpacket      = 512,
97                 .name           = "ep-",
98                 .ops            = &mv_ep_ops,
99         },
100 };
101
102 static struct mv_drv controller = {
103         .gadget = {
104                 .name   = "mv_udc",
105                 .ops    = &mv_udc_ops,
106                 .is_dualspeed = 1,
107         },
108 };
109
110 /**
111  * mv_get_qh() - return queue head for endpoint
112  * @ep_num:     Endpoint number
113  * @dir_in:     Direction of the endpoint (IN = 1, OUT = 0)
114  *
115  * This function returns the QH associated with particular endpoint
116  * and it's direction.
117  */
118 static struct ept_queue_head *mv_get_qh(int ep_num, int dir_in)
119 {
120         return &controller.epts[(ep_num * 2) + dir_in];
121 }
122
123 /**
124  * mv_get_qtd() - return queue item for endpoint
125  * @ep_num:     Endpoint number
126  * @dir_in:     Direction of the endpoint (IN = 1, OUT = 0)
127  *
128  * This function returns the QH associated with particular endpoint
129  * and it's direction.
130  */
131 static struct ept_queue_item *mv_get_qtd(int ep_num, int dir_in)
132 {
133         return controller.items[(ep_num * 2) + dir_in];
134 }
135
136 /**
137  * mv_flush_qh - flush cache over queue head
138  * @ep_num:     Endpoint number
139  *
140  * This function flushes cache over QH for particular endpoint.
141  */
142 static void mv_flush_qh(int ep_num)
143 {
144         struct ept_queue_head *head = mv_get_qh(ep_num, 0);
145         const uint32_t start = (uint32_t)head;
146         const uint32_t end = start + 2 * sizeof(*head);
147
148         flush_dcache_range(start, end);
149 }
150
151 /**
152  * mv_invalidate_qh - invalidate cache over queue head
153  * @ep_num:     Endpoint number
154  *
155  * This function invalidates cache over QH for particular endpoint.
156  */
157 static void mv_invalidate_qh(int ep_num)
158 {
159         struct ept_queue_head *head = mv_get_qh(ep_num, 0);
160         uint32_t start = (uint32_t)head;
161         uint32_t end = start + 2 * sizeof(*head);
162
163         invalidate_dcache_range(start, end);
164 }
165
166 /**
167  * mv_flush_qtd - flush cache over queue item
168  * @ep_num:     Endpoint number
169  *
170  * This function flushes cache over qTD pair for particular endpoint.
171  */
172 static void mv_flush_qtd(int ep_num)
173 {
174         struct ept_queue_item *item = mv_get_qtd(ep_num, 0);
175         const uint32_t start = (uint32_t)item;
176         const uint32_t end_raw = start + 2 * sizeof(*item);
177         const uint32_t end = roundup(end_raw, ARCH_DMA_MINALIGN);
178
179         flush_dcache_range(start, end);
180 }
181
182 /**
183  * mv_invalidate_qtd - invalidate cache over queue item
184  * @ep_num:     Endpoint number
185  *
186  * This function invalidates cache over qTD pair for particular endpoint.
187  */
188 static void mv_invalidate_qtd(int ep_num)
189 {
190         struct ept_queue_item *item = mv_get_qtd(ep_num, 0);
191         const uint32_t start = (uint32_t)item;
192         const uint32_t end_raw = start + 2 * sizeof(*item);
193         const uint32_t end = roundup(end_raw, ARCH_DMA_MINALIGN);
194
195         invalidate_dcache_range(start, end);
196 }
197
198 static struct usb_request *
199 mv_ep_alloc_request(struct usb_ep *ep, unsigned int gfp_flags)
200 {
201         struct mv_ep *mv_ep = container_of(ep, struct mv_ep, ep);
202         return &mv_ep->req;
203 }
204
205 static void mv_ep_free_request(struct usb_ep *ep, struct usb_request *_req)
206 {
207         return;
208 }
209
210 static void ep_enable(int num, int in)
211 {
212         struct ept_queue_head *head;
213         struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
214         unsigned n;
215         head = mv_get_qh(num, in);
216
217         n = readl(&udc->epctrl[num]);
218         if (in)
219                 n |= (CTRL_TXE | CTRL_TXR | CTRL_TXT_BULK);
220         else
221                 n |= (CTRL_RXE | CTRL_RXR | CTRL_RXT_BULK);
222
223         if (num != 0) {
224                 head->config = CONFIG_MAX_PKT(EP_MAX_PACKET_SIZE) | CONFIG_ZLT;
225                 mv_flush_qh(num);
226         }
227         writel(n, &udc->epctrl[num]);
228 }
229
230 static int mv_ep_enable(struct usb_ep *ep,
231                 const struct usb_endpoint_descriptor *desc)
232 {
233         struct mv_ep *mv_ep = container_of(ep, struct mv_ep, ep);
234         int num, in;
235         num = desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
236         in = (desc->bEndpointAddress & USB_DIR_IN) != 0;
237         ep_enable(num, in);
238         mv_ep->desc = desc;
239         return 0;
240 }
241
242 static int mv_ep_disable(struct usb_ep *ep)
243 {
244         struct mv_ep *mv_ep = container_of(ep, struct mv_ep, ep);
245
246         mv_ep->desc = NULL;
247         return 0;
248 }
249
250 static int mv_bounce(struct mv_ep *ep)
251 {
252         uint32_t addr = (uint32_t)ep->req.buf;
253         uint32_t ba;
254
255         /* Input buffer address is not aligned. */
256         if (addr & (ARCH_DMA_MINALIGN - 1))
257                 goto align;
258
259         /* Input buffer length is not aligned. */
260         if (ep->req.length & (ARCH_DMA_MINALIGN - 1))
261                 goto align;
262
263         /* The buffer is well aligned, only flush cache. */
264         ep->b_len = ep->req.length;
265         ep->b_buf = ep->req.buf;
266         goto flush;
267
268 align:
269         /* Use internal buffer for small payloads. */
270         if (ep->req.length <= 64) {
271                 ep->b_len = 64;
272                 ep->b_buf = ep->b_fast;
273         } else {
274                 ep->b_len = roundup(ep->req.length, ARCH_DMA_MINALIGN);
275                 ep->b_buf = memalign(ARCH_DMA_MINALIGN, ep->b_len);
276                 if (!ep->b_buf)
277                         return -ENOMEM;
278         }
279
280         memcpy(ep->b_buf, ep->req.buf, ep->req.length);
281
282 flush:
283         ba = (uint32_t)ep->b_buf;
284         flush_dcache_range(ba, ba + ep->b_len);
285
286         return 0;
287 }
288
289 static void mv_debounce(struct mv_ep *ep)
290 {
291         uint32_t addr = (uint32_t)ep->req.buf;
292         uint32_t ba = (uint32_t)ep->b_buf;
293
294         invalidate_dcache_range(ba, ba + ep->b_len);
295
296         /* Input buffer address is not aligned. */
297         if (addr & (ARCH_DMA_MINALIGN - 1))
298                 goto copy;
299
300         /* Input buffer length is not aligned. */
301         if (ep->req.length & (ARCH_DMA_MINALIGN - 1))
302                 goto copy;
303
304         /* The buffer is well aligned, only invalidate cache. */
305         return;
306
307 copy:
308         memcpy(ep->req.buf, ep->b_buf, ep->req.length);
309
310         /* Large payloads use allocated buffer, free it. */
311         if (ep->req.length > 64)
312                 free(ep->b_buf);
313 }
314
315 static int mv_ep_queue(struct usb_ep *ep,
316                 struct usb_request *req, gfp_t gfp_flags)
317 {
318         struct mv_ep *mv_ep = container_of(ep, struct mv_ep, ep);
319         struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
320         struct ept_queue_item *item;
321         struct ept_queue_head *head;
322         int bit, num, len, in, ret;
323         num = mv_ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
324         in = (mv_ep->desc->bEndpointAddress & USB_DIR_IN) != 0;
325         item = mv_get_qtd(num, in);
326         head = mv_get_qh(num, in);
327         len = req->length;
328
329         ret = mv_bounce(mv_ep);
330         if (ret)
331                 return ret;
332
333         item->next = TERMINATE;
334         item->info = INFO_BYTES(len) | INFO_IOC | INFO_ACTIVE;
335         item->page0 = (uint32_t)mv_ep->b_buf;
336         item->page1 = ((uint32_t)mv_ep->b_buf & 0xfffff000) + 0x1000;
337         mv_flush_qtd(num);
338
339         head->next = (unsigned) item;
340         head->info = 0;
341
342         DBG("ept%d %s queue len %x, buffer %p\n",
343             num, in ? "in" : "out", len, mv_ep->b_buf);
344         mv_flush_qh(num);
345
346         if (in)
347                 bit = EPT_TX(num);
348         else
349                 bit = EPT_RX(num);
350
351         writel(bit, &udc->epprime);
352
353         return 0;
354 }
355
356 static void handle_ep_complete(struct mv_ep *ep)
357 {
358         struct ept_queue_item *item;
359         int num, in, len;
360         num = ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
361         in = (ep->desc->bEndpointAddress & USB_DIR_IN) != 0;
362         if (num == 0)
363                 ep->desc = &ep0_out_desc;
364         item = mv_get_qtd(num, in);
365         mv_invalidate_qtd(num);
366
367         if (item->info & 0xff)
368                 printf("EP%d/%s FAIL info=%x pg0=%x\n",
369                        num, in ? "in" : "out", item->info, item->page0);
370
371         len = (item->info >> 16) & 0x7fff;
372
373         mv_debounce(ep);
374
375         ep->req.length -= len;
376         DBG("ept%d %s complete %x\n",
377                         num, in ? "in" : "out", len);
378         ep->req.complete(&ep->ep, &ep->req);
379         if (num == 0) {
380                 ep->req.length = 0;
381                 usb_ep_queue(&ep->ep, &ep->req, 0);
382                 ep->desc = &ep0_in_desc;
383         }
384 }
385
386 #define SETUP(type, request) (((type) << 8) | (request))
387
388 static void handle_setup(void)
389 {
390         struct usb_request *req = &controller.ep[0].req;
391         struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
392         struct ept_queue_head *head;
393         struct usb_ctrlrequest r;
394         int status = 0;
395         int num, in, _num, _in, i;
396         char *buf;
397         head = mv_get_qh(0, 0); /* EP0 OUT */
398
399         mv_invalidate_qh(0);
400         memcpy(&r, head->setup_data, sizeof(struct usb_ctrlrequest));
401         writel(EPT_RX(0), &udc->epstat);
402         DBG("handle setup %s, %x, %x index %x value %x\n", reqname(r.bRequest),
403             r.bRequestType, r.bRequest, r.wIndex, r.wValue);
404
405         switch (SETUP(r.bRequestType, r.bRequest)) {
406         case SETUP(USB_RECIP_ENDPOINT, USB_REQ_CLEAR_FEATURE):
407                 _num = r.wIndex & 15;
408                 _in = !!(r.wIndex & 0x80);
409
410                 if ((r.wValue == 0) && (r.wLength == 0)) {
411                         req->length = 0;
412                         for (i = 0; i < NUM_ENDPOINTS; i++) {
413                                 if (!controller.ep[i].desc)
414                                         continue;
415                                 num = controller.ep[i].desc->bEndpointAddress
416                                                 & USB_ENDPOINT_NUMBER_MASK;
417                                 in = (controller.ep[i].desc->bEndpointAddress
418                                                 & USB_DIR_IN) != 0;
419                                 if ((num == _num) && (in == _in)) {
420                                         ep_enable(num, in);
421                                         usb_ep_queue(controller.gadget.ep0,
422                                                         req, 0);
423                                         break;
424                                 }
425                         }
426                 }
427                 return;
428
429         case SETUP(USB_RECIP_DEVICE, USB_REQ_SET_ADDRESS):
430                 /*
431                  * write address delayed (will take effect
432                  * after the next IN txn)
433                  */
434                 writel((r.wValue << 25) | (1 << 24), &udc->devaddr);
435                 req->length = 0;
436                 usb_ep_queue(controller.gadget.ep0, req, 0);
437                 return;
438
439         case SETUP(USB_DIR_IN | USB_RECIP_DEVICE, USB_REQ_GET_STATUS):
440                 req->length = 2;
441                 buf = (char *)req->buf;
442                 buf[0] = 1 << USB_DEVICE_SELF_POWERED;
443                 buf[1] = 0;
444                 usb_ep_queue(controller.gadget.ep0, req, 0);
445                 return;
446         }
447         /* pass request up to the gadget driver */
448         if (controller.driver)
449                 status = controller.driver->setup(&controller.gadget, &r);
450         else
451                 status = -ENODEV;
452
453         if (!status)
454                 return;
455         DBG("STALL reqname %s type %x value %x, index %x\n",
456             reqname(r.bRequest), r.bRequestType, r.wValue, r.wIndex);
457         writel((1<<16) | (1 << 0), &udc->epctrl[0]);
458 }
459
460 static void stop_activity(void)
461 {
462         int i, num, in;
463         struct ept_queue_head *head;
464         struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
465         writel(readl(&udc->epcomp), &udc->epcomp);
466         writel(readl(&udc->epstat), &udc->epstat);
467         writel(0xffffffff, &udc->epflush);
468
469         /* error out any pending reqs */
470         for (i = 0; i < NUM_ENDPOINTS; i++) {
471                 if (i != 0)
472                         writel(0, &udc->epctrl[i]);
473                 if (controller.ep[i].desc) {
474                         num = controller.ep[i].desc->bEndpointAddress
475                                 & USB_ENDPOINT_NUMBER_MASK;
476                         in = (controller.ep[i].desc->bEndpointAddress
477                                 & USB_DIR_IN) != 0;
478                         head = mv_get_qh(num, in);
479                         head->info = INFO_ACTIVE;
480                         mv_flush_qh(num);
481                 }
482         }
483 }
484
485 void udc_irq(void)
486 {
487         struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
488         unsigned n = readl(&udc->usbsts);
489         writel(n, &udc->usbsts);
490         int bit, i, num, in;
491
492         n &= (STS_SLI | STS_URI | STS_PCI | STS_UI | STS_UEI);
493         if (n == 0)
494                 return;
495
496         if (n & STS_URI) {
497                 DBG("-- reset --\n");
498                 stop_activity();
499         }
500         if (n & STS_SLI)
501                 DBG("-- suspend --\n");
502
503         if (n & STS_PCI) {
504                 DBG("-- portchange --\n");
505                 bit = (readl(&udc->portsc) >> 26) & 3;
506                 if (bit == 2) {
507                         controller.gadget.speed = USB_SPEED_HIGH;
508                         for (i = 1; i < NUM_ENDPOINTS && n; i++)
509                                 if (controller.ep[i].desc)
510                                         controller.ep[i].ep.maxpacket = 512;
511                 } else {
512                         controller.gadget.speed = USB_SPEED_FULL;
513                 }
514         }
515
516         if (n & STS_UEI)
517                 printf("<UEI %x>\n", readl(&udc->epcomp));
518
519         if ((n & STS_UI) || (n & STS_UEI)) {
520                 n = readl(&udc->epstat);
521                 if (n & EPT_RX(0))
522                         handle_setup();
523
524                 n = readl(&udc->epcomp);
525                 if (n != 0)
526                         writel(n, &udc->epcomp);
527
528                 for (i = 0; i < NUM_ENDPOINTS && n; i++) {
529                         if (controller.ep[i].desc) {
530                                 num = controller.ep[i].desc->bEndpointAddress
531                                         & USB_ENDPOINT_NUMBER_MASK;
532                                 in = (controller.ep[i].desc->bEndpointAddress
533                                                 & USB_DIR_IN) != 0;
534                                 bit = (in) ? EPT_TX(num) : EPT_RX(num);
535                                 if (n & bit)
536                                         handle_ep_complete(&controller.ep[i]);
537                         }
538                 }
539         }
540 }
541
542 int usb_gadget_handle_interrupts(void)
543 {
544         u32 value;
545         struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
546
547         value = readl(&udc->usbsts);
548         if (value)
549                 udc_irq();
550
551         return value;
552 }
553
554 static int mv_pullup(struct usb_gadget *gadget, int is_on)
555 {
556         struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
557         if (is_on) {
558                 /* RESET */
559                 writel(USBCMD_ITC(MICRO_8FRAME) | USBCMD_RST, &udc->usbcmd);
560                 udelay(200);
561
562                 writel((unsigned)controller.epts, &udc->epinitaddr);
563
564                 /* select DEVICE mode */
565                 writel(USBMODE_DEVICE, &udc->usbmode);
566
567                 writel(0xffffffff, &udc->epflush);
568
569                 /* Turn on the USB connection by enabling the pullup resistor */
570                 writel(USBCMD_ITC(MICRO_8FRAME) | USBCMD_RUN, &udc->usbcmd);
571         } else {
572                 stop_activity();
573                 writel(USBCMD_FS2, &udc->usbcmd);
574                 udelay(800);
575                 if (controller.driver)
576                         controller.driver->disconnect(gadget);
577         }
578
579         return 0;
580 }
581
582 void udc_disconnect(void)
583 {
584         struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
585         /* disable pullup */
586         stop_activity();
587         writel(USBCMD_FS2, &udc->usbcmd);
588         udelay(800);
589         if (controller.driver)
590                 controller.driver->disconnect(&controller.gadget);
591 }
592
593 static int mvudc_probe(void)
594 {
595         struct ept_queue_head *head;
596         uint8_t *imem;
597         int i;
598
599         const int num = 2 * NUM_ENDPOINTS;
600
601         const int eplist_min_align = 4096;
602         const int eplist_align = roundup(eplist_min_align, ARCH_DMA_MINALIGN);
603         const int eplist_raw_sz = num * sizeof(struct ept_queue_head);
604         const int eplist_sz = roundup(eplist_raw_sz, ARCH_DMA_MINALIGN);
605
606         const int ilist_align = roundup(ARCH_DMA_MINALIGN, 32);
607         const int ilist_ent_raw_sz = 2 * sizeof(struct ept_queue_item);
608         const int ilist_ent_sz = roundup(ilist_ent_raw_sz, ARCH_DMA_MINALIGN);
609         const int ilist_sz = NUM_ENDPOINTS * ilist_ent_sz;
610
611         /* The QH list must be aligned to 4096 bytes. */
612         controller.epts = memalign(eplist_align, eplist_sz);
613         if (!controller.epts)
614                 return -ENOMEM;
615         memset(controller.epts, 0, eplist_sz);
616
617         /*
618          * Each qTD item must be 32-byte aligned, each qTD touple must be
619          * cacheline aligned. There are two qTD items for each endpoint and
620          * only one of them is used for the endpoint at time, so we can group
621          * them together.
622          */
623         controller.items_mem = memalign(ilist_align, ilist_sz);
624         if (!controller.items_mem) {
625                 free(controller.epts);
626                 return -ENOMEM;
627         }
628         memset(controller.items_mem, 0, ilist_sz);
629
630         for (i = 0; i < 2 * NUM_ENDPOINTS; i++) {
631                 /*
632                  * Configure QH for each endpoint. The structure of the QH list
633                  * is such that each two subsequent fields, N and N+1 where N is
634                  * even, in the QH list represent QH for one endpoint. The Nth
635                  * entry represents OUT configuration and the N+1th entry does
636                  * represent IN configuration of the endpoint.
637                  */
638                 head = controller.epts + i;
639                 if (i < 2)
640                         head->config = CONFIG_MAX_PKT(EP0_MAX_PACKET_SIZE)
641                                 | CONFIG_ZLT | CONFIG_IOS;
642                 else
643                         head->config = CONFIG_MAX_PKT(EP_MAX_PACKET_SIZE)
644                                 | CONFIG_ZLT;
645                 head->next = TERMINATE;
646                 head->info = 0;
647
648                 imem = controller.items_mem + ((i >> 1) * ilist_ent_sz);
649                 if (i & 1)
650                         imem += sizeof(struct ept_queue_item);
651
652                 controller.items[i] = (struct ept_queue_item *)imem;
653
654                 if (i & 1) {
655                         mv_flush_qh(i - 1);
656                         mv_flush_qtd(i - 1);
657                 }
658         }
659
660         INIT_LIST_HEAD(&controller.gadget.ep_list);
661
662         /* Init EP 0 */
663         memcpy(&controller.ep[0].ep, &mv_ep_init[0], sizeof(*mv_ep_init));
664         controller.ep[0].desc = &ep0_in_desc;
665         controller.gadget.ep0 = &controller.ep[0].ep;
666         INIT_LIST_HEAD(&controller.gadget.ep0->ep_list);
667
668         /* Init EP 1..n */
669         for (i = 1; i < NUM_ENDPOINTS; i++) {
670                 memcpy(&controller.ep[i].ep, &mv_ep_init[1],
671                        sizeof(*mv_ep_init));
672                 list_add_tail(&controller.ep[i].ep.ep_list,
673                               &controller.gadget.ep_list);
674         }
675
676         return 0;
677 }
678
679 int usb_gadget_register_driver(struct usb_gadget_driver *driver)
680 {
681         struct mv_udc *udc;
682         int ret;
683
684         if (!driver)
685                 return -EINVAL;
686         if (!driver->bind || !driver->setup || !driver->disconnect)
687                 return -EINVAL;
688         if (driver->speed != USB_SPEED_FULL && driver->speed != USB_SPEED_HIGH)
689                 return -EINVAL;
690
691         ret = usb_lowlevel_init(0, USB_INIT_DEVICE, (void **)&controller.ctrl);
692         if (ret)
693                 return ret;
694
695         ret = mvudc_probe();
696         if (!ret) {
697                 udc = (struct mv_udc *)controller.ctrl->hcor;
698
699                 /* select ULPI phy */
700                 writel(PTS(PTS_ENABLE) | PFSC, &udc->portsc);
701         }
702
703         ret = driver->bind(&controller.gadget);
704         if (ret) {
705                 DBG("driver->bind() returned %d\n", ret);
706                 return ret;
707         }
708         controller.driver = driver;
709
710         return 0;
711 }
712
713 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
714 {
715         return 0;
716 }