common: Drop linux/delay.h from common header
[oweals/u-boot.git] / drivers / usb / gadget / ci_udc.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2011, Marvell Semiconductor Inc.
4  * Lei Wen <leiwen@marvell.com>
5  *
6  * Back ported to the 8xx platform (from the 8260 platform) by
7  * Murray.Jensen@cmst.csiro.au, 27-Jan-01.
8  */
9
10 #include <common.h>
11 #include <command.h>
12 #include <config.h>
13 #include <cpu_func.h>
14 #include <net.h>
15 #include <malloc.h>
16 #include <asm/byteorder.h>
17 #include <asm/cache.h>
18 #include <linux/delay.h>
19 #include <linux/errno.h>
20 #include <asm/io.h>
21 #include <asm/unaligned.h>
22 #include <linux/types.h>
23 #include <linux/usb/ch9.h>
24 #include <linux/usb/gadget.h>
25 #include <usb/ci_udc.h>
26 #include "../host/ehci.h"
27 #include "ci_udc.h"
28
29 /*
30  * Check if the system has too long cachelines. If the cachelines are
31  * longer then 128b, the driver will not be able flush/invalidate data
32  * cache over separate QH entries. We use 128b because one QH entry is
33  * 64b long and there are always two QH list entries for each endpoint.
34  */
35 #if ARCH_DMA_MINALIGN > 128
36 #error This driver can not work on systems with caches longer than 128b
37 #endif
38
39 /*
40  * Every QTD must be individually aligned, since we can program any
41  * QTD's address into HW. Cache flushing requires ARCH_DMA_MINALIGN,
42  * and the USB HW requires 32-byte alignment. Align to both:
43  */
44 #define ILIST_ALIGN             roundup(ARCH_DMA_MINALIGN, 32)
45 /* Each QTD is this size */
46 #define ILIST_ENT_RAW_SZ        sizeof(struct ept_queue_item)
47 /*
48  * Align the size of the QTD too, so we can add this value to each
49  * QTD's address to get another aligned address.
50  */
51 #define ILIST_ENT_SZ            roundup(ILIST_ENT_RAW_SZ, ILIST_ALIGN)
52 /* For each endpoint, we need 2 QTDs, one for each of IN and OUT */
53 #define ILIST_SZ                (NUM_ENDPOINTS * 2 * ILIST_ENT_SZ)
54
55 #define EP_MAX_LENGTH_TRANSFER  0x4000
56
57 #ifndef DEBUG
58 #define DBG(x...) do {} while (0)
59 #else
60 #define DBG(x...) printf(x)
61 static const char *reqname(unsigned r)
62 {
63         switch (r) {
64         case USB_REQ_GET_STATUS: return "GET_STATUS";
65         case USB_REQ_CLEAR_FEATURE: return "CLEAR_FEATURE";
66         case USB_REQ_SET_FEATURE: return "SET_FEATURE";
67         case USB_REQ_SET_ADDRESS: return "SET_ADDRESS";
68         case USB_REQ_GET_DESCRIPTOR: return "GET_DESCRIPTOR";
69         case USB_REQ_SET_DESCRIPTOR: return "SET_DESCRIPTOR";
70         case USB_REQ_GET_CONFIGURATION: return "GET_CONFIGURATION";
71         case USB_REQ_SET_CONFIGURATION: return "SET_CONFIGURATION";
72         case USB_REQ_GET_INTERFACE: return "GET_INTERFACE";
73         case USB_REQ_SET_INTERFACE: return "SET_INTERFACE";
74         default: return "*UNKNOWN*";
75         }
76 }
77 #endif
78
79 static struct usb_endpoint_descriptor ep0_desc = {
80         .bLength = sizeof(struct usb_endpoint_descriptor),
81         .bDescriptorType = USB_DT_ENDPOINT,
82         .bEndpointAddress = USB_DIR_IN,
83         .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
84 };
85
86 static int ci_pullup(struct usb_gadget *gadget, int is_on);
87 static int ci_ep_enable(struct usb_ep *ep,
88                 const struct usb_endpoint_descriptor *desc);
89 static int ci_ep_disable(struct usb_ep *ep);
90 static int ci_ep_queue(struct usb_ep *ep,
91                 struct usb_request *req, gfp_t gfp_flags);
92 static int ci_ep_dequeue(struct usb_ep *ep, struct usb_request *req);
93 static struct usb_request *
94 ci_ep_alloc_request(struct usb_ep *ep, unsigned int gfp_flags);
95 static void ci_ep_free_request(struct usb_ep *ep, struct usb_request *_req);
96
97 static struct usb_gadget_ops ci_udc_ops = {
98         .pullup = ci_pullup,
99 };
100
101 static struct usb_ep_ops ci_ep_ops = {
102         .enable         = ci_ep_enable,
103         .disable        = ci_ep_disable,
104         .queue          = ci_ep_queue,
105         .dequeue        = ci_ep_dequeue,
106         .alloc_request  = ci_ep_alloc_request,
107         .free_request   = ci_ep_free_request,
108 };
109
110 __weak void ci_init_after_reset(struct ehci_ctrl *ctrl)
111 {
112 }
113
114 /* Init values for USB endpoints. */
115 static const struct usb_ep ci_ep_init[5] = {
116         [0] = { /* EP 0 */
117                 .maxpacket      = 64,
118                 .name           = "ep0",
119                 .ops            = &ci_ep_ops,
120         },
121         [1] = {
122                 .maxpacket      = 512,
123                 .name           = "ep1in-bulk",
124                 .ops            = &ci_ep_ops,
125         },
126         [2] = {
127                 .maxpacket      = 512,
128                 .name           = "ep2out-bulk",
129                 .ops            = &ci_ep_ops,
130         },
131         [3] = {
132                 .maxpacket      = 512,
133                 .name           = "ep3in-int",
134                 .ops            = &ci_ep_ops,
135         },
136         [4] = {
137                 .maxpacket      = 512,
138                 .name           = "ep-",
139                 .ops            = &ci_ep_ops,
140         },
141 };
142
143 static struct ci_drv controller = {
144         .gadget = {
145                 .name   = "ci_udc",
146                 .ops    = &ci_udc_ops,
147                 .is_dualspeed = 1,
148         },
149 };
150
151 /**
152  * ci_get_qh() - return queue head for endpoint
153  * @ep_num:     Endpoint number
154  * @dir_in:     Direction of the endpoint (IN = 1, OUT = 0)
155  *
156  * This function returns the QH associated with particular endpoint
157  * and it's direction.
158  */
159 static struct ept_queue_head *ci_get_qh(int ep_num, int dir_in)
160 {
161         return &controller.epts[(ep_num * 2) + dir_in];
162 }
163
164 /**
165  * ci_get_qtd() - return queue item for endpoint
166  * @ep_num:     Endpoint number
167  * @dir_in:     Direction of the endpoint (IN = 1, OUT = 0)
168  *
169  * This function returns the QH associated with particular endpoint
170  * and it's direction.
171  */
172 static struct ept_queue_item *ci_get_qtd(int ep_num, int dir_in)
173 {
174         int index = (ep_num * 2) + dir_in;
175         uint8_t *imem = controller.items_mem + (index * ILIST_ENT_SZ);
176         return (struct ept_queue_item *)imem;
177 }
178
179 /**
180  * ci_flush_qh - flush cache over queue head
181  * @ep_num:     Endpoint number
182  *
183  * This function flushes cache over QH for particular endpoint.
184  */
185 static void ci_flush_qh(int ep_num)
186 {
187         struct ept_queue_head *head = ci_get_qh(ep_num, 0);
188         const unsigned long start = (unsigned long)head;
189         const unsigned long end = start + 2 * sizeof(*head);
190
191         flush_dcache_range(start, end);
192 }
193
194 /**
195  * ci_invalidate_qh - invalidate cache over queue head
196  * @ep_num:     Endpoint number
197  *
198  * This function invalidates cache over QH for particular endpoint.
199  */
200 static void ci_invalidate_qh(int ep_num)
201 {
202         struct ept_queue_head *head = ci_get_qh(ep_num, 0);
203         unsigned long start = (unsigned long)head;
204         unsigned long end = start + 2 * sizeof(*head);
205
206         invalidate_dcache_range(start, end);
207 }
208
209 /**
210  * ci_flush_qtd - flush cache over queue item
211  * @ep_num:     Endpoint number
212  *
213  * This function flushes cache over qTD pair for particular endpoint.
214  */
215 static void ci_flush_qtd(int ep_num)
216 {
217         struct ept_queue_item *item = ci_get_qtd(ep_num, 0);
218         const unsigned long start = (unsigned long)item;
219         const unsigned long end = start + 2 * ILIST_ENT_SZ;
220
221         flush_dcache_range(start, end);
222 }
223
224 /**
225  * ci_flush_td - flush cache over queue item
226  * @td: td pointer
227  *
228  * This function flushes cache for particular transfer descriptor.
229  */
230 static void ci_flush_td(struct ept_queue_item *td)
231 {
232         const unsigned long start = (unsigned long)td;
233         const unsigned long end = (unsigned long)td + ILIST_ENT_SZ;
234         flush_dcache_range(start, end);
235 }
236
237 /**
238  * ci_invalidate_qtd - invalidate cache over queue item
239  * @ep_num:     Endpoint number
240  *
241  * This function invalidates cache over qTD pair for particular endpoint.
242  */
243 static void ci_invalidate_qtd(int ep_num)
244 {
245         struct ept_queue_item *item = ci_get_qtd(ep_num, 0);
246         const unsigned long start = (unsigned long)item;
247         const unsigned long end = start + 2 * ILIST_ENT_SZ;
248
249         invalidate_dcache_range(start, end);
250 }
251
252 /**
253  * ci_invalidate_td - invalidate cache over queue item
254  * @td: td pointer
255  *
256  * This function invalidates cache for particular transfer descriptor.
257  */
258 static void ci_invalidate_td(struct ept_queue_item *td)
259 {
260         const unsigned long start = (unsigned long)td;
261         const unsigned long end = start + ILIST_ENT_SZ;
262         invalidate_dcache_range(start, end);
263 }
264
265 static struct usb_request *
266 ci_ep_alloc_request(struct usb_ep *ep, unsigned int gfp_flags)
267 {
268         struct ci_ep *ci_ep = container_of(ep, struct ci_ep, ep);
269         int num = -1;
270         struct ci_req *ci_req;
271
272         if (ci_ep->desc)
273                 num = ci_ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
274
275         if (num == 0 && controller.ep0_req)
276                 return &controller.ep0_req->req;
277
278         ci_req = calloc(1, sizeof(*ci_req));
279         if (!ci_req)
280                 return NULL;
281
282         INIT_LIST_HEAD(&ci_req->queue);
283
284         if (num == 0)
285                 controller.ep0_req = ci_req;
286
287         return &ci_req->req;
288 }
289
290 static void ci_ep_free_request(struct usb_ep *ep, struct usb_request *req)
291 {
292         struct ci_ep *ci_ep = container_of(ep, struct ci_ep, ep);
293         struct ci_req *ci_req = container_of(req, struct ci_req, req);
294         int num = -1;
295
296         if (ci_ep->desc)
297                 num = ci_ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
298
299         if (num == 0) {
300                 if (!controller.ep0_req)
301                         return;
302                 controller.ep0_req = 0;
303         }
304
305         if (ci_req->b_buf)
306                 free(ci_req->b_buf);
307         free(ci_req);
308 }
309
310 static void ep_enable(int num, int in, int maxpacket)
311 {
312         struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor;
313         unsigned n;
314
315         n = readl(&udc->epctrl[num]);
316         if (in)
317                 n |= (CTRL_TXE | CTRL_TXR | CTRL_TXT_BULK);
318         else
319                 n |= (CTRL_RXE | CTRL_RXR | CTRL_RXT_BULK);
320
321         if (num != 0) {
322                 struct ept_queue_head *head = ci_get_qh(num, in);
323
324                 head->config = CONFIG_MAX_PKT(maxpacket) | CONFIG_ZLT;
325                 ci_flush_qh(num);
326         }
327         writel(n, &udc->epctrl[num]);
328 }
329
330 static int ci_ep_enable(struct usb_ep *ep,
331                 const struct usb_endpoint_descriptor *desc)
332 {
333         struct ci_ep *ci_ep = container_of(ep, struct ci_ep, ep);
334         int num, in;
335         num = desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
336         in = (desc->bEndpointAddress & USB_DIR_IN) != 0;
337         ci_ep->desc = desc;
338
339         if (num) {
340                 int max = get_unaligned_le16(&desc->wMaxPacketSize);
341
342                 if ((max > 64) && (controller.gadget.speed == USB_SPEED_FULL))
343                         max = 64;
344                 if (ep->maxpacket != max) {
345                         DBG("%s: from %d to %d\n", __func__,
346                             ep->maxpacket, max);
347                         ep->maxpacket = max;
348                 }
349         }
350         ep_enable(num, in, ep->maxpacket);
351         DBG("%s: num=%d maxpacket=%d\n", __func__, num, ep->maxpacket);
352         return 0;
353 }
354
355 static int ci_ep_disable(struct usb_ep *ep)
356 {
357         struct ci_ep *ci_ep = container_of(ep, struct ci_ep, ep);
358
359         ci_ep->desc = NULL;
360         return 0;
361 }
362
363 static int ci_bounce(struct ci_req *ci_req, int in)
364 {
365         struct usb_request *req = &ci_req->req;
366         unsigned long addr = (unsigned long)req->buf;
367         unsigned long hwaddr;
368         uint32_t aligned_used_len;
369
370         /* Input buffer address is not aligned. */
371         if (addr & (ARCH_DMA_MINALIGN - 1))
372                 goto align;
373
374         /* Input buffer length is not aligned. */
375         if (req->length & (ARCH_DMA_MINALIGN - 1))
376                 goto align;
377
378         /* The buffer is well aligned, only flush cache. */
379         ci_req->hw_len = req->length;
380         ci_req->hw_buf = req->buf;
381         goto flush;
382
383 align:
384         if (ci_req->b_buf && req->length > ci_req->b_len) {
385                 free(ci_req->b_buf);
386                 ci_req->b_buf = 0;
387         }
388         if (!ci_req->b_buf) {
389                 ci_req->b_len = roundup(req->length, ARCH_DMA_MINALIGN);
390                 ci_req->b_buf = memalign(ARCH_DMA_MINALIGN, ci_req->b_len);
391                 if (!ci_req->b_buf)
392                         return -ENOMEM;
393         }
394         ci_req->hw_len = ci_req->b_len;
395         ci_req->hw_buf = ci_req->b_buf;
396
397         if (in)
398                 memcpy(ci_req->hw_buf, req->buf, req->length);
399
400 flush:
401         hwaddr = (unsigned long)ci_req->hw_buf;
402         aligned_used_len = roundup(req->length, ARCH_DMA_MINALIGN);
403         flush_dcache_range(hwaddr, hwaddr + aligned_used_len);
404
405         return 0;
406 }
407
408 static void ci_debounce(struct ci_req *ci_req, int in)
409 {
410         struct usb_request *req = &ci_req->req;
411         unsigned long addr = (unsigned long)req->buf;
412         unsigned long hwaddr = (unsigned long)ci_req->hw_buf;
413         uint32_t aligned_used_len;
414
415         if (in)
416                 return;
417
418         aligned_used_len = roundup(req->actual, ARCH_DMA_MINALIGN);
419         invalidate_dcache_range(hwaddr, hwaddr + aligned_used_len);
420
421         if (addr == hwaddr)
422                 return; /* not a bounce */
423
424         memcpy(req->buf, ci_req->hw_buf, req->actual);
425 }
426
427 static void ci_ep_submit_next_request(struct ci_ep *ci_ep)
428 {
429         struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor;
430         struct ept_queue_item *item;
431         struct ept_queue_head *head;
432         int bit, num, len, in;
433         struct ci_req *ci_req;
434         u8 *buf;
435         uint32_t len_left, len_this_dtd;
436         struct ept_queue_item *dtd, *qtd;
437
438         ci_ep->req_primed = true;
439
440         num = ci_ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
441         in = (ci_ep->desc->bEndpointAddress & USB_DIR_IN) != 0;
442         item = ci_get_qtd(num, in);
443         head = ci_get_qh(num, in);
444
445         ci_req = list_first_entry(&ci_ep->queue, struct ci_req, queue);
446         len = ci_req->req.length;
447
448         head->next = (unsigned long)item;
449         head->info = 0;
450
451         ci_req->dtd_count = 0;
452         buf = ci_req->hw_buf;
453         len_left = len;
454         dtd = item;
455
456         do {
457                 len_this_dtd = min(len_left, (unsigned)EP_MAX_LENGTH_TRANSFER);
458
459                 dtd->info = INFO_BYTES(len_this_dtd) | INFO_ACTIVE;
460                 dtd->page0 = (unsigned long)buf;
461                 dtd->page1 = ((unsigned long)buf & 0xfffff000) + 0x1000;
462                 dtd->page2 = ((unsigned long)buf & 0xfffff000) + 0x2000;
463                 dtd->page3 = ((unsigned long)buf & 0xfffff000) + 0x3000;
464                 dtd->page4 = ((unsigned long)buf & 0xfffff000) + 0x4000;
465
466                 len_left -= len_this_dtd;
467                 buf += len_this_dtd;
468
469                 if (len_left) {
470                         qtd = (struct ept_queue_item *)
471                                memalign(ILIST_ALIGN, ILIST_ENT_SZ);
472                         dtd->next = (unsigned long)qtd;
473                         dtd = qtd;
474                         memset(dtd, 0, ILIST_ENT_SZ);
475                 }
476
477                 ci_req->dtd_count++;
478         } while (len_left);
479
480         item = dtd;
481         /*
482          * When sending the data for an IN transaction, the attached host
483          * knows that all data for the IN is sent when one of the following
484          * occurs:
485          * a) A zero-length packet is transmitted.
486          * b) A packet with length that isn't an exact multiple of the ep's
487          *    maxpacket is transmitted.
488          * c) Enough data is sent to exactly fill the host's maximum expected
489          *    IN transaction size.
490          *
491          * One of these conditions MUST apply at the end of an IN transaction,
492          * or the transaction will not be considered complete by the host. If
493          * none of (a)..(c) already applies, then we must force (a) to apply
494          * by explicitly sending an extra zero-length packet.
495          */
496         /*  IN    !a     !b                              !c */
497         if (in && len && !(len % ci_ep->ep.maxpacket) && ci_req->req.zero) {
498                 /*
499                  * Each endpoint has 2 items allocated, even though typically
500                  * only 1 is used at a time since either an IN or an OUT but
501                  * not both is queued. For an IN transaction, item currently
502                  * points at the second of these items, so we know that we
503                  * can use the other to transmit the extra zero-length packet.
504                  */
505                 struct ept_queue_item *other_item = ci_get_qtd(num, 0);
506                 item->next = (unsigned long)other_item;
507                 item = other_item;
508                 item->info = INFO_ACTIVE;
509         }
510
511         item->next = TERMINATE;
512         item->info |= INFO_IOC;
513
514         ci_flush_qtd(num);
515
516         item = (struct ept_queue_item *)(unsigned long)head->next;
517         while (item->next != TERMINATE) {
518                 ci_flush_td((struct ept_queue_item *)(unsigned long)item->next);
519                 item = (struct ept_queue_item *)(unsigned long)item->next;
520         }
521
522         DBG("ept%d %s queue len %x, req %p, buffer %p\n",
523             num, in ? "in" : "out", len, ci_req, ci_req->hw_buf);
524         ci_flush_qh(num);
525
526         if (in)
527                 bit = EPT_TX(num);
528         else
529                 bit = EPT_RX(num);
530
531         writel(bit, &udc->epprime);
532 }
533
534 static int ci_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
535 {
536         struct ci_ep *ci_ep = container_of(_ep, struct ci_ep, ep);
537         struct ci_req *ci_req;
538
539         list_for_each_entry(ci_req, &ci_ep->queue, queue) {
540                 if (&ci_req->req == _req)
541                         break;
542         }
543
544         if (&ci_req->req != _req)
545                 return -EINVAL;
546
547         list_del_init(&ci_req->queue);
548
549         if (ci_req->req.status == -EINPROGRESS) {
550                 ci_req->req.status = -ECONNRESET;
551                 if (ci_req->req.complete)
552                         ci_req->req.complete(_ep, _req);
553         }
554
555         return 0;
556 }
557
558 static int ci_ep_queue(struct usb_ep *ep,
559                 struct usb_request *req, gfp_t gfp_flags)
560 {
561         struct ci_ep *ci_ep = container_of(ep, struct ci_ep, ep);
562         struct ci_req *ci_req = container_of(req, struct ci_req, req);
563         int in, ret;
564         int __maybe_unused num;
565
566         num = ci_ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
567         in = (ci_ep->desc->bEndpointAddress & USB_DIR_IN) != 0;
568
569         if (!num && ci_ep->req_primed) {
570                 /*
571                  * The flipping of ep0 between IN and OUT relies on
572                  * ci_ep_queue consuming the current IN/OUT setting
573                  * immediately. If this is deferred to a later point when the
574                  * req is pulled out of ci_req->queue, then the IN/OUT setting
575                  * may have been changed since the req was queued, and state
576                  * will get out of sync. This condition doesn't occur today,
577                  * but could if bugs were introduced later, and this error
578                  * check will save a lot of debugging time.
579                  */
580                 printf("%s: ep0 transaction already in progress\n", __func__);
581                 return -EPROTO;
582         }
583
584         ret = ci_bounce(ci_req, in);
585         if (ret)
586                 return ret;
587
588         DBG("ept%d %s pre-queue req %p, buffer %p\n",
589             num, in ? "in" : "out", ci_req, ci_req->hw_buf);
590         list_add_tail(&ci_req->queue, &ci_ep->queue);
591
592         if (!ci_ep->req_primed)
593                 ci_ep_submit_next_request(ci_ep);
594
595         return 0;
596 }
597
598 static void flip_ep0_direction(void)
599 {
600         if (ep0_desc.bEndpointAddress == USB_DIR_IN) {
601                 DBG("%s: Flipping ep0 to OUT\n", __func__);
602                 ep0_desc.bEndpointAddress = 0;
603         } else {
604                 DBG("%s: Flipping ep0 to IN\n", __func__);
605                 ep0_desc.bEndpointAddress = USB_DIR_IN;
606         }
607 }
608
609 static void handle_ep_complete(struct ci_ep *ci_ep)
610 {
611         struct ept_queue_item *item, *next_td;
612         int num, in, len, j;
613         struct ci_req *ci_req;
614
615         num = ci_ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
616         in = (ci_ep->desc->bEndpointAddress & USB_DIR_IN) != 0;
617         item = ci_get_qtd(num, in);
618         ci_invalidate_qtd(num);
619         ci_req = list_first_entry(&ci_ep->queue, struct ci_req, queue);
620
621         next_td = item;
622         len = 0;
623         for (j = 0; j < ci_req->dtd_count; j++) {
624                 ci_invalidate_td(next_td);
625                 item = next_td;
626                 len += (item->info >> 16) & 0x7fff;
627                 if (item->info & 0xff)
628                         printf("EP%d/%s FAIL info=%x pg0=%x\n",
629                                num, in ? "in" : "out", item->info, item->page0);
630                 if (j != ci_req->dtd_count - 1)
631                         next_td = (struct ept_queue_item *)(unsigned long)
632                                 item->next;
633                 if (j != 0)
634                         free(item);
635         }
636
637         list_del_init(&ci_req->queue);
638         ci_ep->req_primed = false;
639
640         if (!list_empty(&ci_ep->queue))
641                 ci_ep_submit_next_request(ci_ep);
642
643         ci_req->req.actual = ci_req->req.length - len;
644         ci_debounce(ci_req, in);
645
646         DBG("ept%d %s req %p, complete %x\n",
647             num, in ? "in" : "out", ci_req, len);
648         if (num != 0 || controller.ep0_data_phase)
649                 ci_req->req.complete(&ci_ep->ep, &ci_req->req);
650         if (num == 0 && controller.ep0_data_phase) {
651                 /*
652                  * Data Stage is complete, so flip ep0 dir for Status Stage,
653                  * which always transfers a packet in the opposite direction.
654                  */
655                 DBG("%s: flip ep0 dir for Status Stage\n", __func__);
656                 flip_ep0_direction();
657                 controller.ep0_data_phase = false;
658                 ci_req->req.length = 0;
659                 usb_ep_queue(&ci_ep->ep, &ci_req->req, 0);
660         }
661 }
662
663 #define SETUP(type, request) (((type) << 8) | (request))
664
665 static void handle_setup(void)
666 {
667         struct ci_ep *ci_ep = &controller.ep[0];
668         struct ci_req *ci_req;
669         struct usb_request *req;
670         struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor;
671         struct ept_queue_head *head;
672         struct usb_ctrlrequest r;
673         int status = 0;
674         int num, in, _num, _in, i;
675         char *buf;
676
677         ci_req = controller.ep0_req;
678         req = &ci_req->req;
679         head = ci_get_qh(0, 0); /* EP0 OUT */
680
681         ci_invalidate_qh(0);
682         memcpy(&r, head->setup_data, sizeof(struct usb_ctrlrequest));
683 #ifdef CONFIG_CI_UDC_HAS_HOSTPC
684         writel(EPT_RX(0), &udc->epsetupstat);
685 #else
686         writel(EPT_RX(0), &udc->epstat);
687 #endif
688         DBG("handle setup %s, %x, %x index %x value %x length %x\n",
689             reqname(r.bRequest), r.bRequestType, r.bRequest, r.wIndex,
690             r.wValue, r.wLength);
691
692         /* Set EP0 dir for Data Stage based on Setup Stage data */
693         if (r.bRequestType & USB_DIR_IN) {
694                 DBG("%s: Set ep0 to IN for Data Stage\n", __func__);
695                 ep0_desc.bEndpointAddress = USB_DIR_IN;
696         } else {
697                 DBG("%s: Set ep0 to OUT for Data Stage\n", __func__);
698                 ep0_desc.bEndpointAddress = 0;
699         }
700         if (r.wLength) {
701                 controller.ep0_data_phase = true;
702         } else {
703                 /* 0 length -> no Data Stage. Flip dir for Status Stage */
704                 DBG("%s: 0 length: flip ep0 dir for Status Stage\n", __func__);
705                 flip_ep0_direction();
706                 controller.ep0_data_phase = false;
707         }
708
709         list_del_init(&ci_req->queue);
710         ci_ep->req_primed = false;
711
712         switch (SETUP(r.bRequestType, r.bRequest)) {
713         case SETUP(USB_RECIP_ENDPOINT, USB_REQ_CLEAR_FEATURE):
714                 _num = r.wIndex & 15;
715                 _in = !!(r.wIndex & 0x80);
716
717                 if ((r.wValue == 0) && (r.wLength == 0)) {
718                         req->length = 0;
719                         for (i = 0; i < NUM_ENDPOINTS; i++) {
720                                 struct ci_ep *ep = &controller.ep[i];
721
722                                 if (!ep->desc)
723                                         continue;
724                                 num = ep->desc->bEndpointAddress
725                                                 & USB_ENDPOINT_NUMBER_MASK;
726                                 in = (ep->desc->bEndpointAddress
727                                                 & USB_DIR_IN) != 0;
728                                 if ((num == _num) && (in == _in)) {
729                                         ep_enable(num, in, ep->ep.maxpacket);
730                                         usb_ep_queue(controller.gadget.ep0,
731                                                         req, 0);
732                                         break;
733                                 }
734                         }
735                 }
736                 return;
737
738         case SETUP(USB_RECIP_DEVICE, USB_REQ_SET_ADDRESS):
739                 /*
740                  * write address delayed (will take effect
741                  * after the next IN txn)
742                  */
743                 writel((r.wValue << 25) | (1 << 24), &udc->devaddr);
744                 req->length = 0;
745                 usb_ep_queue(controller.gadget.ep0, req, 0);
746                 return;
747
748         case SETUP(USB_DIR_IN | USB_RECIP_DEVICE, USB_REQ_GET_STATUS):
749                 req->length = 2;
750                 buf = (char *)req->buf;
751                 buf[0] = 1 << USB_DEVICE_SELF_POWERED;
752                 buf[1] = 0;
753                 usb_ep_queue(controller.gadget.ep0, req, 0);
754                 return;
755         }
756         /* pass request up to the gadget driver */
757         if (controller.driver)
758                 status = controller.driver->setup(&controller.gadget, &r);
759         else
760                 status = -ENODEV;
761
762         if (!status)
763                 return;
764         DBG("STALL reqname %s type %x value %x, index %x\n",
765             reqname(r.bRequest), r.bRequestType, r.wValue, r.wIndex);
766         writel((1<<16) | (1 << 0), &udc->epctrl[0]);
767 }
768
769 static void stop_activity(void)
770 {
771         int i, num, in;
772         struct ept_queue_head *head;
773         struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor;
774         writel(readl(&udc->epcomp), &udc->epcomp);
775 #ifdef CONFIG_CI_UDC_HAS_HOSTPC
776         writel(readl(&udc->epsetupstat), &udc->epsetupstat);
777 #endif
778         writel(readl(&udc->epstat), &udc->epstat);
779         writel(0xffffffff, &udc->epflush);
780
781         /* error out any pending reqs */
782         for (i = 0; i < NUM_ENDPOINTS; i++) {
783                 if (i != 0)
784                         writel(0, &udc->epctrl[i]);
785                 if (controller.ep[i].desc) {
786                         num = controller.ep[i].desc->bEndpointAddress
787                                 & USB_ENDPOINT_NUMBER_MASK;
788                         in = (controller.ep[i].desc->bEndpointAddress
789                                 & USB_DIR_IN) != 0;
790                         head = ci_get_qh(num, in);
791                         head->info = INFO_ACTIVE;
792                         ci_flush_qh(num);
793                 }
794         }
795 }
796
797 void udc_irq(void)
798 {
799         struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor;
800         unsigned n = readl(&udc->usbsts);
801         writel(n, &udc->usbsts);
802         int bit, i, num, in;
803
804         n &= (STS_SLI | STS_URI | STS_PCI | STS_UI | STS_UEI);
805         if (n == 0)
806                 return;
807
808         if (n & STS_URI) {
809                 DBG("-- reset --\n");
810                 stop_activity();
811         }
812         if (n & STS_SLI)
813                 DBG("-- suspend --\n");
814
815         if (n & STS_PCI) {
816                 int max = 64;
817                 int speed = USB_SPEED_FULL;
818
819 #ifdef CONFIG_CI_UDC_HAS_HOSTPC
820                 bit = (readl(&udc->hostpc1_devlc) >> 25) & 3;
821 #else
822                 bit = (readl(&udc->portsc) >> 26) & 3;
823 #endif
824                 DBG("-- portchange %x %s\n", bit, (bit == 2) ? "High" : "Full");
825                 if (bit == 2) {
826                         speed = USB_SPEED_HIGH;
827                         max = 512;
828                 }
829                 controller.gadget.speed = speed;
830                 for (i = 1; i < NUM_ENDPOINTS; i++) {
831                         if (controller.ep[i].ep.maxpacket > max)
832                                 controller.ep[i].ep.maxpacket = max;
833                 }
834         }
835
836         if (n & STS_UEI)
837                 printf("<UEI %x>\n", readl(&udc->epcomp));
838
839         if ((n & STS_UI) || (n & STS_UEI)) {
840 #ifdef CONFIG_CI_UDC_HAS_HOSTPC
841                 n = readl(&udc->epsetupstat);
842 #else
843                 n = readl(&udc->epstat);
844 #endif
845                 if (n & EPT_RX(0))
846                         handle_setup();
847
848                 n = readl(&udc->epcomp);
849                 if (n != 0)
850                         writel(n, &udc->epcomp);
851
852                 for (i = 0; i < NUM_ENDPOINTS && n; i++) {
853                         if (controller.ep[i].desc) {
854                                 num = controller.ep[i].desc->bEndpointAddress
855                                         & USB_ENDPOINT_NUMBER_MASK;
856                                 in = (controller.ep[i].desc->bEndpointAddress
857                                                 & USB_DIR_IN) != 0;
858                                 bit = (in) ? EPT_TX(num) : EPT_RX(num);
859                                 if (n & bit)
860                                         handle_ep_complete(&controller.ep[i]);
861                         }
862                 }
863         }
864 }
865
866 int usb_gadget_handle_interrupts(int index)
867 {
868         u32 value;
869         struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor;
870
871         value = readl(&udc->usbsts);
872         if (value)
873                 udc_irq();
874
875         return value;
876 }
877
878 void udc_disconnect(void)
879 {
880         struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor;
881         /* disable pullup */
882         stop_activity();
883         writel(USBCMD_FS2, &udc->usbcmd);
884         udelay(800);
885         if (controller.driver)
886                 controller.driver->disconnect(&controller.gadget);
887 }
888
889 static int ci_pullup(struct usb_gadget *gadget, int is_on)
890 {
891         struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor;
892         if (is_on) {
893                 /* RESET */
894                 writel(USBCMD_ITC(MICRO_8FRAME) | USBCMD_RST, &udc->usbcmd);
895                 udelay(200);
896
897                 ci_init_after_reset(controller.ctrl);
898
899                 writel((unsigned long)controller.epts, &udc->epinitaddr);
900
901                 /* select DEVICE mode */
902                 writel(USBMODE_DEVICE, &udc->usbmode);
903
904 #if !defined(CONFIG_USB_GADGET_DUALSPEED)
905                 /* Port force Full-Speed Connect */
906                 setbits_le32(&udc->portsc, PFSC);
907 #endif
908
909                 writel(0xffffffff, &udc->epflush);
910
911                 /* Turn on the USB connection by enabling the pullup resistor */
912                 setbits_le32(&udc->usbcmd, USBCMD_ITC(MICRO_8FRAME) |
913                              USBCMD_RUN);
914         } else {
915                 udc_disconnect();
916         }
917
918         return 0;
919 }
920
921 static int ci_udc_probe(void)
922 {
923         struct ept_queue_head *head;
924         int i;
925
926         const int num = 2 * NUM_ENDPOINTS;
927
928         const int eplist_min_align = 4096;
929         const int eplist_align = roundup(eplist_min_align, ARCH_DMA_MINALIGN);
930         const int eplist_raw_sz = num * sizeof(struct ept_queue_head);
931         const int eplist_sz = roundup(eplist_raw_sz, ARCH_DMA_MINALIGN);
932
933         /* The QH list must be aligned to 4096 bytes. */
934         controller.epts = memalign(eplist_align, eplist_sz);
935         if (!controller.epts)
936                 return -ENOMEM;
937         memset(controller.epts, 0, eplist_sz);
938
939         controller.items_mem = memalign(ILIST_ALIGN, ILIST_SZ);
940         if (!controller.items_mem) {
941                 free(controller.epts);
942                 return -ENOMEM;
943         }
944         memset(controller.items_mem, 0, ILIST_SZ);
945
946         for (i = 0; i < 2 * NUM_ENDPOINTS; i++) {
947                 /*
948                  * Configure QH for each endpoint. The structure of the QH list
949                  * is such that each two subsequent fields, N and N+1 where N is
950                  * even, in the QH list represent QH for one endpoint. The Nth
951                  * entry represents OUT configuration and the N+1th entry does
952                  * represent IN configuration of the endpoint.
953                  */
954                 head = controller.epts + i;
955                 if (i < 2)
956                         head->config = CONFIG_MAX_PKT(EP0_MAX_PACKET_SIZE)
957                                 | CONFIG_ZLT | CONFIG_IOS;
958                 else
959                         head->config = CONFIG_MAX_PKT(EP_MAX_PACKET_SIZE)
960                                 | CONFIG_ZLT;
961                 head->next = TERMINATE;
962                 head->info = 0;
963
964                 if (i & 1) {
965                         ci_flush_qh(i / 2);
966                         ci_flush_qtd(i / 2);
967                 }
968         }
969
970         INIT_LIST_HEAD(&controller.gadget.ep_list);
971
972         /* Init EP 0 */
973         memcpy(&controller.ep[0].ep, &ci_ep_init[0], sizeof(*ci_ep_init));
974         controller.ep[0].desc = &ep0_desc;
975         INIT_LIST_HEAD(&controller.ep[0].queue);
976         controller.ep[0].req_primed = false;
977         controller.gadget.ep0 = &controller.ep[0].ep;
978         INIT_LIST_HEAD(&controller.gadget.ep0->ep_list);
979
980         /* Init EP 1..3 */
981         for (i = 1; i < 4; i++) {
982                 memcpy(&controller.ep[i].ep, &ci_ep_init[i],
983                        sizeof(*ci_ep_init));
984                 INIT_LIST_HEAD(&controller.ep[i].queue);
985                 controller.ep[i].req_primed = false;
986                 list_add_tail(&controller.ep[i].ep.ep_list,
987                               &controller.gadget.ep_list);
988         }
989
990         /* Init EP 4..n */
991         for (i = 4; i < NUM_ENDPOINTS; i++) {
992                 memcpy(&controller.ep[i].ep, &ci_ep_init[4],
993                        sizeof(*ci_ep_init));
994                 INIT_LIST_HEAD(&controller.ep[i].queue);
995                 controller.ep[i].req_primed = false;
996                 list_add_tail(&controller.ep[i].ep.ep_list,
997                               &controller.gadget.ep_list);
998         }
999
1000         ci_ep_alloc_request(&controller.ep[0].ep, 0);
1001         if (!controller.ep0_req) {
1002                 free(controller.items_mem);
1003                 free(controller.epts);
1004                 return -ENOMEM;
1005         }
1006
1007         return 0;
1008 }
1009
1010 int usb_gadget_register_driver(struct usb_gadget_driver *driver)
1011 {
1012         int ret;
1013
1014         if (!driver)
1015                 return -EINVAL;
1016         if (!driver->bind || !driver->setup || !driver->disconnect)
1017                 return -EINVAL;
1018         if (driver->speed != USB_SPEED_FULL && driver->speed != USB_SPEED_HIGH)
1019                 return -EINVAL;
1020
1021 #if CONFIG_IS_ENABLED(DM_USB)
1022         ret = usb_setup_ehci_gadget(&controller.ctrl);
1023 #else
1024         ret = usb_lowlevel_init(0, USB_INIT_DEVICE, (void **)&controller.ctrl);
1025 #endif
1026         if (ret)
1027                 return ret;
1028
1029         ret = ci_udc_probe();
1030         if (ret) {
1031                 DBG("udc probe failed, returned %d\n", ret);
1032                 return ret;
1033         }
1034
1035         ret = driver->bind(&controller.gadget);
1036         if (ret) {
1037                 DBG("driver->bind() returned %d\n", ret);
1038                 return ret;
1039         }
1040         controller.driver = driver;
1041
1042         return 0;
1043 }
1044
1045 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1046 {
1047         udc_disconnect();
1048
1049         driver->unbind(&controller.gadget);
1050         controller.driver = NULL;
1051
1052         ci_ep_free_request(&controller.ep[0].ep, &controller.ep0_req->req);
1053         free(controller.items_mem);
1054         free(controller.epts);
1055
1056         return 0;
1057 }
1058
1059 bool dfu_usb_get_reset(void)
1060 {
1061         struct ci_udc *udc = (struct ci_udc *)controller.ctrl->hcor;
1062
1063         return !!(readl(&udc->usbsts) & STS_URI);
1064 }