32bf92752d4208932b8861ecf2b5e068c6a77b14
[oweals/u-boot.git] / drivers / usb / gadget / dwc2_udc_otg.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * drivers/usb/gadget/dwc2_udc_otg.c
4  * Designware DWC2 on-chip full/high speed USB OTG 2.0 device controllers
5  *
6  * Copyright (C) 2008 for Samsung Electronics
7  *
8  * BSP Support for Samsung's UDC driver
9  * available at:
10  * git://git.kernel.org/pub/scm/linux/kernel/git/kki_ap/linux-2.6-samsung.git
11  *
12  * State machine bugfixes:
13  * Marek Szyprowski <m.szyprowski@samsung.com>
14  *
15  * Ported to u-boot:
16  * Marek Szyprowski <m.szyprowski@samsung.com>
17  * Lukasz Majewski <l.majewski@samsumg.com>
18  */
19 #undef DEBUG
20 #include <common.h>
21 #include <clk.h>
22 #include <dm.h>
23 #include <generic-phy.h>
24 #include <log.h>
25 #include <malloc.h>
26 #include <reset.h>
27 #include <dm/device_compat.h>
28 #include <dm/devres.h>
29 #include <linux/bug.h>
30
31 #include <linux/errno.h>
32 #include <linux/list.h>
33
34 #include <linux/usb/ch9.h>
35 #include <linux/usb/otg.h>
36 #include <linux/usb/gadget.h>
37
38 #include <phys2bus.h>
39 #include <asm/byteorder.h>
40 #include <asm/unaligned.h>
41 #include <asm/io.h>
42
43 #include <asm/mach-types.h>
44
45 #include <power/regulator.h>
46
47 #include "dwc2_udc_otg_regs.h"
48 #include "dwc2_udc_otg_priv.h"
49
50 /***********************************************************/
51
52 #define OTG_DMA_MODE            1
53
54 #define DEBUG_SETUP 0
55 #define DEBUG_EP0 0
56 #define DEBUG_ISR 0
57 #define DEBUG_OUT_EP 0
58 #define DEBUG_IN_EP 0
59
60 #include <usb/dwc2_udc.h>
61
62 #define EP0_CON         0
63 #define EP_MASK         0xF
64
65 static char *state_names[] = {
66         "WAIT_FOR_SETUP",
67         "DATA_STATE_XMIT",
68         "DATA_STATE_NEED_ZLP",
69         "WAIT_FOR_OUT_STATUS",
70         "DATA_STATE_RECV",
71         "WAIT_FOR_COMPLETE",
72         "WAIT_FOR_OUT_COMPLETE",
73         "WAIT_FOR_IN_COMPLETE",
74         "WAIT_FOR_NULL_COMPLETE",
75 };
76
77 #define DRIVER_VERSION "15 March 2009"
78
79 struct dwc2_udc *the_controller;
80
81 static const char driver_name[] = "dwc2-udc";
82 static const char ep0name[] = "ep0-control";
83
84 /* Max packet size*/
85 static unsigned int ep0_fifo_size = 64;
86 static unsigned int ep_fifo_size =  512;
87 static unsigned int ep_fifo_size2 = 1024;
88 static int reset_available = 1;
89
90 static struct usb_ctrlrequest *usb_ctrl;
91 static dma_addr_t usb_ctrl_dma_addr;
92
93 /*
94   Local declarations.
95 */
96 static int dwc2_ep_enable(struct usb_ep *ep,
97                          const struct usb_endpoint_descriptor *);
98 static int dwc2_ep_disable(struct usb_ep *ep);
99 static struct usb_request *dwc2_alloc_request(struct usb_ep *ep,
100                                              gfp_t gfp_flags);
101 static void dwc2_free_request(struct usb_ep *ep, struct usb_request *);
102
103 static int dwc2_queue(struct usb_ep *ep, struct usb_request *, gfp_t gfp_flags);
104 static int dwc2_dequeue(struct usb_ep *ep, struct usb_request *);
105 static int dwc2_fifo_status(struct usb_ep *ep);
106 static void dwc2_fifo_flush(struct usb_ep *ep);
107 static void dwc2_ep0_read(struct dwc2_udc *dev);
108 static void dwc2_ep0_kick(struct dwc2_udc *dev, struct dwc2_ep *ep);
109 static void dwc2_handle_ep0(struct dwc2_udc *dev);
110 static int dwc2_ep0_write(struct dwc2_udc *dev);
111 static int write_fifo_ep0(struct dwc2_ep *ep, struct dwc2_request *req);
112 static void done(struct dwc2_ep *ep, struct dwc2_request *req, int status);
113 static void stop_activity(struct dwc2_udc *dev,
114                           struct usb_gadget_driver *driver);
115 static int udc_enable(struct dwc2_udc *dev);
116 static void udc_set_address(struct dwc2_udc *dev, unsigned char address);
117 static void reconfig_usbd(struct dwc2_udc *dev);
118 static void set_max_pktsize(struct dwc2_udc *dev, enum usb_device_speed speed);
119 static void nuke(struct dwc2_ep *ep, int status);
120 static int dwc2_udc_set_halt(struct usb_ep *_ep, int value);
121 static void dwc2_udc_set_nak(struct dwc2_ep *ep);
122
123 void set_udc_gadget_private_data(void *p)
124 {
125         debug_cond(DEBUG_SETUP != 0,
126                    "%s: the_controller: 0x%p, p: 0x%p\n", __func__,
127                    the_controller, p);
128         the_controller->gadget.dev.device_data = p;
129 }
130
131 void *get_udc_gadget_private_data(struct usb_gadget *gadget)
132 {
133         return gadget->dev.device_data;
134 }
135
136 static struct usb_ep_ops dwc2_ep_ops = {
137         .enable = dwc2_ep_enable,
138         .disable = dwc2_ep_disable,
139
140         .alloc_request = dwc2_alloc_request,
141         .free_request = dwc2_free_request,
142
143         .queue = dwc2_queue,
144         .dequeue = dwc2_dequeue,
145
146         .set_halt = dwc2_udc_set_halt,
147         .fifo_status = dwc2_fifo_status,
148         .fifo_flush = dwc2_fifo_flush,
149 };
150
151 #define create_proc_files() do {} while (0)
152 #define remove_proc_files() do {} while (0)
153
154 /***********************************************************/
155
156 struct dwc2_usbotg_reg *reg;
157
158 bool dfu_usb_get_reset(void)
159 {
160         return !!(readl(&reg->gintsts) & INT_RESET);
161 }
162
163 __weak void otg_phy_init(struct dwc2_udc *dev) {}
164 __weak void otg_phy_off(struct dwc2_udc *dev) {}
165
166 /***********************************************************/
167
168 #include "dwc2_udc_otg_xfer_dma.c"
169
170 /*
171  *      udc_disable - disable USB device controller
172  */
173 static void udc_disable(struct dwc2_udc *dev)
174 {
175         debug_cond(DEBUG_SETUP != 0, "%s: %p\n", __func__, dev);
176
177         udc_set_address(dev, 0);
178
179         dev->ep0state = WAIT_FOR_SETUP;
180         dev->gadget.speed = USB_SPEED_UNKNOWN;
181         dev->usb_address = 0;
182
183         otg_phy_off(dev);
184 }
185
186 /*
187  *      udc_reinit - initialize software state
188  */
189 static void udc_reinit(struct dwc2_udc *dev)
190 {
191         unsigned int i;
192
193         debug_cond(DEBUG_SETUP != 0, "%s: %p\n", __func__, dev);
194
195         /* device/ep0 records init */
196         INIT_LIST_HEAD(&dev->gadget.ep_list);
197         INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
198         dev->ep0state = WAIT_FOR_SETUP;
199
200         /* basic endpoint records init */
201         for (i = 0; i < DWC2_MAX_ENDPOINTS; i++) {
202                 struct dwc2_ep *ep = &dev->ep[i];
203
204                 if (i != 0)
205                         list_add_tail(&ep->ep.ep_list, &dev->gadget.ep_list);
206
207                 ep->desc = 0;
208                 ep->stopped = 0;
209                 INIT_LIST_HEAD(&ep->queue);
210                 ep->pio_irqs = 0;
211         }
212
213         /* the rest was statically initialized, and is read-only */
214 }
215
216 #define BYTES2MAXP(x)   (x / 8)
217 #define MAXP2BYTES(x)   (x * 8)
218
219 /* until it's enabled, this UDC should be completely invisible
220  * to any USB host.
221  */
222 static int udc_enable(struct dwc2_udc *dev)
223 {
224         debug_cond(DEBUG_SETUP != 0, "%s: %p\n", __func__, dev);
225
226         otg_phy_init(dev);
227         reconfig_usbd(dev);
228
229         debug_cond(DEBUG_SETUP != 0,
230                    "DWC2 USB 2.0 OTG Controller Core Initialized : 0x%x\n",
231                     readl(&reg->gintmsk));
232
233         dev->gadget.speed = USB_SPEED_UNKNOWN;
234
235         return 0;
236 }
237
238 #if !CONFIG_IS_ENABLED(DM_USB_GADGET)
239 /*
240   Register entry point for the peripheral controller driver.
241 */
242 int usb_gadget_register_driver(struct usb_gadget_driver *driver)
243 {
244         struct dwc2_udc *dev = the_controller;
245         int retval = 0;
246         unsigned long flags = 0;
247
248         debug_cond(DEBUG_SETUP != 0, "%s: %s\n", __func__, "no name");
249
250         if (!driver
251             || (driver->speed != USB_SPEED_FULL
252                 && driver->speed != USB_SPEED_HIGH)
253             || !driver->bind || !driver->disconnect || !driver->setup)
254                 return -EINVAL;
255         if (!dev)
256                 return -ENODEV;
257         if (dev->driver)
258                 return -EBUSY;
259
260         spin_lock_irqsave(&dev->lock, flags);
261         /* first hook up the driver ... */
262         dev->driver = driver;
263         spin_unlock_irqrestore(&dev->lock, flags);
264
265         if (retval) { /* TODO */
266                 printf("target device_add failed, error %d\n", retval);
267                 return retval;
268         }
269
270         retval = driver->bind(&dev->gadget);
271         if (retval) {
272                 debug_cond(DEBUG_SETUP != 0,
273                            "%s: bind to driver --> error %d\n",
274                             dev->gadget.name, retval);
275                 dev->driver = 0;
276                 return retval;
277         }
278
279         enable_irq(IRQ_OTG);
280
281         debug_cond(DEBUG_SETUP != 0,
282                    "Registered gadget driver %s\n", dev->gadget.name);
283         udc_enable(dev);
284
285         return 0;
286 }
287
288 /*
289  * Unregister entry point for the peripheral controller driver.
290  */
291 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
292 {
293         struct dwc2_udc *dev = the_controller;
294         unsigned long flags = 0;
295
296         if (!dev)
297                 return -ENODEV;
298         if (!driver || driver != dev->driver)
299                 return -EINVAL;
300
301         spin_lock_irqsave(&dev->lock, flags);
302         dev->driver = 0;
303         stop_activity(dev, driver);
304         spin_unlock_irqrestore(&dev->lock, flags);
305
306         driver->unbind(&dev->gadget);
307
308         disable_irq(IRQ_OTG);
309
310         udc_disable(dev);
311         return 0;
312 }
313 #else /* !CONFIG_IS_ENABLED(DM_USB_GADGET) */
314
315 static int dwc2_gadget_start(struct usb_gadget *g,
316                              struct usb_gadget_driver *driver)
317 {
318         struct dwc2_udc *dev = the_controller;
319
320         debug_cond(DEBUG_SETUP != 0, "%s: %s\n", __func__, "no name");
321
322         if (!driver ||
323             (driver->speed != USB_SPEED_FULL &&
324              driver->speed != USB_SPEED_HIGH) ||
325             !driver->bind || !driver->disconnect || !driver->setup)
326                 return -EINVAL;
327
328         if (!dev)
329                 return -ENODEV;
330
331         if (dev->driver)
332                 return -EBUSY;
333
334         /* first hook up the driver ... */
335         dev->driver = driver;
336
337         debug_cond(DEBUG_SETUP != 0,
338                    "Registered gadget driver %s\n", dev->gadget.name);
339         return udc_enable(dev);
340 }
341
342 static int dwc2_gadget_stop(struct usb_gadget *g)
343 {
344         struct dwc2_udc *dev = the_controller;
345
346         if (!dev)
347                 return -ENODEV;
348
349         if (!dev->driver)
350                 return -EINVAL;
351
352         dev->driver = 0;
353         stop_activity(dev, dev->driver);
354
355         udc_disable(dev);
356
357         return 0;
358 }
359
360 #endif /* !CONFIG_IS_ENABLED(DM_USB_GADGET) */
361
362 /*
363  *      done - retire a request; caller blocked irqs
364  */
365 static void done(struct dwc2_ep *ep, struct dwc2_request *req, int status)
366 {
367         unsigned int stopped = ep->stopped;
368
369         debug("%s: %s %p, req = %p, stopped = %d\n",
370               __func__, ep->ep.name, ep, &req->req, stopped);
371
372         list_del_init(&req->queue);
373
374         if (likely(req->req.status == -EINPROGRESS))
375                 req->req.status = status;
376         else
377                 status = req->req.status;
378
379         if (status && status != -ESHUTDOWN) {
380                 debug("complete %s req %p stat %d len %u/%u\n",
381                       ep->ep.name, &req->req, status,
382                       req->req.actual, req->req.length);
383         }
384
385         /* don't modify queue heads during completion callback */
386         ep->stopped = 1;
387
388 #ifdef DEBUG
389         printf("calling complete callback\n");
390         {
391                 int i, len = req->req.length;
392
393                 printf("pkt[%d] = ", req->req.length);
394                 if (len > 64)
395                         len = 64;
396                 for (i = 0; i < len; i++) {
397                         printf("%02x", ((u8 *)req->req.buf)[i]);
398                         if ((i & 7) == 7)
399                                 printf(" ");
400                 }
401                 printf("\n");
402         }
403 #endif
404         spin_unlock(&ep->dev->lock);
405         req->req.complete(&ep->ep, &req->req);
406         spin_lock(&ep->dev->lock);
407
408         debug("callback completed\n");
409
410         ep->stopped = stopped;
411 }
412
413 /*
414  *      nuke - dequeue ALL requests
415  */
416 static void nuke(struct dwc2_ep *ep, int status)
417 {
418         struct dwc2_request *req;
419
420         debug("%s: %s %p\n", __func__, ep->ep.name, ep);
421
422         /* called with irqs blocked */
423         while (!list_empty(&ep->queue)) {
424                 req = list_entry(ep->queue.next, struct dwc2_request, queue);
425                 done(ep, req, status);
426         }
427 }
428
429 static void stop_activity(struct dwc2_udc *dev,
430                           struct usb_gadget_driver *driver)
431 {
432         int i;
433
434         /* don't disconnect drivers more than once */
435         if (dev->gadget.speed == USB_SPEED_UNKNOWN)
436                 driver = 0;
437         dev->gadget.speed = USB_SPEED_UNKNOWN;
438
439         /* prevent new request submissions, kill any outstanding requests  */
440         for (i = 0; i < DWC2_MAX_ENDPOINTS; i++) {
441                 struct dwc2_ep *ep = &dev->ep[i];
442                 ep->stopped = 1;
443                 nuke(ep, -ESHUTDOWN);
444         }
445
446         /* report disconnect; the driver is already quiesced */
447         if (driver) {
448                 spin_unlock(&dev->lock);
449                 driver->disconnect(&dev->gadget);
450                 spin_lock(&dev->lock);
451         }
452
453         /* re-init driver-visible data structures */
454         udc_reinit(dev);
455 }
456
457 static void reconfig_usbd(struct dwc2_udc *dev)
458 {
459         /* 2. Soft-reset OTG Core and then unreset again. */
460         int i;
461         unsigned int uTemp = writel(CORE_SOFT_RESET, &reg->grstctl);
462         uint32_t dflt_gusbcfg;
463         uint32_t rx_fifo_sz, tx_fifo_sz, np_tx_fifo_sz;
464         u32 max_hw_ep;
465         int pdata_hw_ep;
466
467         debug("Reseting OTG controller\n");
468
469         dflt_gusbcfg =
470                 0<<15           /* PHY Low Power Clock sel*/
471                 |1<<14          /* Non-Periodic TxFIFO Rewind Enable*/
472                 |0x5<<10        /* Turnaround time*/
473                 |0<<9 | 0<<8    /* [0:HNP disable,1:HNP enable][ 0:SRP disable*/
474                                 /* 1:SRP enable] H1= 1,1*/
475                 |0<<7           /* Ulpi DDR sel*/
476                 |0<<6           /* 0: high speed utmi+, 1: full speed serial*/
477                 |0<<4           /* 0: utmi+, 1:ulpi*/
478 #ifdef CONFIG_USB_GADGET_DWC2_OTG_PHY_BUS_WIDTH_8
479                 |0<<3           /* phy i/f  0:8bit, 1:16bit*/
480 #else
481                 |1<<3           /* phy i/f  0:8bit, 1:16bit*/
482 #endif
483                 |0x7<<0;        /* HS/FS Timeout**/
484
485         if (dev->pdata->usb_gusbcfg)
486                 dflt_gusbcfg = dev->pdata->usb_gusbcfg;
487
488         writel(dflt_gusbcfg, &reg->gusbcfg);
489
490         /* 3. Put the OTG device core in the disconnected state.*/
491         uTemp = readl(&reg->dctl);
492         uTemp |= SOFT_DISCONNECT;
493         writel(uTemp, &reg->dctl);
494
495         udelay(20);
496
497         /* 4. Make the OTG device core exit from the disconnected state.*/
498         uTemp = readl(&reg->dctl);
499         uTemp = uTemp & ~SOFT_DISCONNECT;
500         writel(uTemp, &reg->dctl);
501
502         /* 5. Configure OTG Core to initial settings of device mode.*/
503         /* [][1: full speed(30Mhz) 0:high speed]*/
504         writel(EP_MISS_CNT(1) | DEV_SPEED_HIGH_SPEED_20, &reg->dcfg);
505
506         mdelay(1);
507
508         /* 6. Unmask the core interrupts*/
509         writel(GINTMSK_INIT, &reg->gintmsk);
510
511         /* 7. Set NAK bit of EP0, EP1, EP2*/
512         writel(DEPCTL_EPDIS|DEPCTL_SNAK, &reg->out_endp[EP0_CON].doepctl);
513         writel(DEPCTL_EPDIS|DEPCTL_SNAK, &reg->in_endp[EP0_CON].diepctl);
514
515         for (i = 1; i < DWC2_MAX_ENDPOINTS; i++) {
516                 writel(DEPCTL_EPDIS|DEPCTL_SNAK, &reg->out_endp[i].doepctl);
517                 writel(DEPCTL_EPDIS|DEPCTL_SNAK, &reg->in_endp[i].diepctl);
518         }
519
520         /* 8. Unmask EPO interrupts*/
521         writel(((1 << EP0_CON) << DAINT_OUT_BIT)
522                | (1 << EP0_CON), &reg->daintmsk);
523
524         /* 9. Unmask device OUT EP common interrupts*/
525         writel(DOEPMSK_INIT, &reg->doepmsk);
526
527         /* 10. Unmask device IN EP common interrupts*/
528         writel(DIEPMSK_INIT, &reg->diepmsk);
529
530         rx_fifo_sz = RX_FIFO_SIZE;
531         np_tx_fifo_sz = NPTX_FIFO_SIZE;
532         tx_fifo_sz = PTX_FIFO_SIZE;
533
534         if (dev->pdata->rx_fifo_sz)
535                 rx_fifo_sz = dev->pdata->rx_fifo_sz;
536         if (dev->pdata->np_tx_fifo_sz)
537                 np_tx_fifo_sz = dev->pdata->np_tx_fifo_sz;
538         if (dev->pdata->tx_fifo_sz)
539                 tx_fifo_sz = dev->pdata->tx_fifo_sz;
540
541         /* 11. Set Rx FIFO Size (in 32-bit words) */
542         writel(rx_fifo_sz, &reg->grxfsiz);
543
544         /* 12. Set Non Periodic Tx FIFO Size */
545         writel((np_tx_fifo_sz << 16) | rx_fifo_sz,
546                &reg->gnptxfsiz);
547
548         /* retrieve the number of IN Endpoints (excluding ep0) */
549         max_hw_ep = (readl(&reg->ghwcfg4) & GHWCFG4_NUM_IN_EPS_MASK) >>
550                     GHWCFG4_NUM_IN_EPS_SHIFT;
551         pdata_hw_ep = dev->pdata->tx_fifo_sz_nb;
552
553         /* tx_fifo_sz_nb should equal to number of IN Endpoint */
554         if (pdata_hw_ep && max_hw_ep != pdata_hw_ep)
555                 pr_warn("Got %d hw endpoint but %d tx-fifo-size in array !!\n",
556                         max_hw_ep, pdata_hw_ep);
557
558         for (i = 0; i < max_hw_ep; i++) {
559                 if (pdata_hw_ep)
560                         tx_fifo_sz = dev->pdata->tx_fifo_sz_array[i];
561
562                 writel((rx_fifo_sz + np_tx_fifo_sz + (tx_fifo_sz * i)) |
563                         tx_fifo_sz << 16, &reg->dieptxf[i]);
564         }
565         /* Flush the RX FIFO */
566         writel(RX_FIFO_FLUSH, &reg->grstctl);
567         while (readl(&reg->grstctl) & RX_FIFO_FLUSH)
568                 debug("%s: waiting for DWC2_UDC_OTG_GRSTCTL\n", __func__);
569
570         /* Flush all the Tx FIFO's */
571         writel(TX_FIFO_FLUSH_ALL, &reg->grstctl);
572         writel(TX_FIFO_FLUSH_ALL | TX_FIFO_FLUSH, &reg->grstctl);
573         while (readl(&reg->grstctl) & TX_FIFO_FLUSH)
574                 debug("%s: waiting for DWC2_UDC_OTG_GRSTCTL\n", __func__);
575
576         /* 13. Clear NAK bit of EP0, EP1, EP2*/
577         /* For Slave mode*/
578         /* EP0: Control OUT */
579         writel(DEPCTL_EPDIS | DEPCTL_CNAK,
580                &reg->out_endp[EP0_CON].doepctl);
581
582         /* 14. Initialize OTG Link Core.*/
583         writel(GAHBCFG_INIT, &reg->gahbcfg);
584 }
585
586 static void set_max_pktsize(struct dwc2_udc *dev, enum usb_device_speed speed)
587 {
588         unsigned int ep_ctrl;
589         int i;
590
591         if (speed == USB_SPEED_HIGH) {
592                 ep0_fifo_size = 64;
593                 ep_fifo_size = 512;
594                 ep_fifo_size2 = 1024;
595                 dev->gadget.speed = USB_SPEED_HIGH;
596         } else {
597                 ep0_fifo_size = 64;
598                 ep_fifo_size = 64;
599                 ep_fifo_size2 = 64;
600                 dev->gadget.speed = USB_SPEED_FULL;
601         }
602
603         dev->ep[0].ep.maxpacket = ep0_fifo_size;
604         for (i = 1; i < DWC2_MAX_ENDPOINTS; i++)
605                 dev->ep[i].ep.maxpacket = ep_fifo_size;
606
607         /* EP0 - Control IN (64 bytes)*/
608         ep_ctrl = readl(&reg->in_endp[EP0_CON].diepctl);
609         writel(ep_ctrl|(0<<0), &reg->in_endp[EP0_CON].diepctl);
610
611         /* EP0 - Control OUT (64 bytes)*/
612         ep_ctrl = readl(&reg->out_endp[EP0_CON].doepctl);
613         writel(ep_ctrl|(0<<0), &reg->out_endp[EP0_CON].doepctl);
614 }
615
616 static int dwc2_ep_enable(struct usb_ep *_ep,
617                          const struct usb_endpoint_descriptor *desc)
618 {
619         struct dwc2_ep *ep;
620         struct dwc2_udc *dev;
621         unsigned long flags = 0;
622
623         debug("%s: %p\n", __func__, _ep);
624
625         ep = container_of(_ep, struct dwc2_ep, ep);
626         if (!_ep || !desc || ep->desc || _ep->name == ep0name
627             || desc->bDescriptorType != USB_DT_ENDPOINT
628             || ep->bEndpointAddress != desc->bEndpointAddress
629             || ep_maxpacket(ep) <
630             le16_to_cpu(get_unaligned(&desc->wMaxPacketSize))) {
631
632                 debug("%s: bad ep or descriptor\n", __func__);
633                 return -EINVAL;
634         }
635
636         /* xfer types must match, except that interrupt ~= bulk */
637         if (ep->bmAttributes != desc->bmAttributes
638             && ep->bmAttributes != USB_ENDPOINT_XFER_BULK
639             && desc->bmAttributes != USB_ENDPOINT_XFER_INT) {
640
641                 debug("%s: %s type mismatch\n", __func__, _ep->name);
642                 return -EINVAL;
643         }
644
645         /* hardware _could_ do smaller, but driver doesn't */
646         if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK &&
647              le16_to_cpu(get_unaligned(&desc->wMaxPacketSize)) >
648              ep_maxpacket(ep)) || !get_unaligned(&desc->wMaxPacketSize)) {
649
650                 debug("%s: bad %s maxpacket\n", __func__, _ep->name);
651                 return -ERANGE;
652         }
653
654         dev = ep->dev;
655         if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
656
657                 debug("%s: bogus device state\n", __func__);
658                 return -ESHUTDOWN;
659         }
660
661         ep->stopped = 0;
662         ep->desc = desc;
663         ep->pio_irqs = 0;
664         ep->ep.maxpacket = le16_to_cpu(get_unaligned(&desc->wMaxPacketSize));
665
666         /* Reset halt state */
667         dwc2_udc_set_nak(ep);
668         dwc2_udc_set_halt(_ep, 0);
669
670         spin_lock_irqsave(&ep->dev->lock, flags);
671         dwc2_udc_ep_activate(ep);
672         spin_unlock_irqrestore(&ep->dev->lock, flags);
673
674         debug("%s: enabled %s, stopped = %d, maxpacket = %d\n",
675               __func__, _ep->name, ep->stopped, ep->ep.maxpacket);
676         return 0;
677 }
678
679 /*
680  * Disable EP
681  */
682 static int dwc2_ep_disable(struct usb_ep *_ep)
683 {
684         struct dwc2_ep *ep;
685         unsigned long flags = 0;
686
687         debug("%s: %p\n", __func__, _ep);
688
689         ep = container_of(_ep, struct dwc2_ep, ep);
690         if (!_ep || !ep->desc) {
691                 debug("%s: %s not enabled\n", __func__,
692                       _ep ? ep->ep.name : NULL);
693                 return -EINVAL;
694         }
695
696         spin_lock_irqsave(&ep->dev->lock, flags);
697
698         /* Nuke all pending requests */
699         nuke(ep, -ESHUTDOWN);
700
701         ep->desc = 0;
702         ep->stopped = 1;
703
704         spin_unlock_irqrestore(&ep->dev->lock, flags);
705
706         debug("%s: disabled %s\n", __func__, _ep->name);
707         return 0;
708 }
709
710 static struct usb_request *dwc2_alloc_request(struct usb_ep *ep,
711                                              gfp_t gfp_flags)
712 {
713         struct dwc2_request *req;
714
715         debug("%s: %s %p\n", __func__, ep->name, ep);
716
717         req = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*req));
718         if (!req)
719                 return 0;
720
721         memset(req, 0, sizeof *req);
722         INIT_LIST_HEAD(&req->queue);
723
724         return &req->req;
725 }
726
727 static void dwc2_free_request(struct usb_ep *ep, struct usb_request *_req)
728 {
729         struct dwc2_request *req;
730
731         debug("%s: %p\n", __func__, ep);
732
733         req = container_of(_req, struct dwc2_request, req);
734         WARN_ON(!list_empty(&req->queue));
735         kfree(req);
736 }
737
738 /* dequeue JUST ONE request */
739 static int dwc2_dequeue(struct usb_ep *_ep, struct usb_request *_req)
740 {
741         struct dwc2_ep *ep;
742         struct dwc2_request *req;
743         unsigned long flags = 0;
744
745         debug("%s: %p\n", __func__, _ep);
746
747         ep = container_of(_ep, struct dwc2_ep, ep);
748         if (!_ep || ep->ep.name == ep0name)
749                 return -EINVAL;
750
751         spin_lock_irqsave(&ep->dev->lock, flags);
752
753         /* make sure it's actually queued on this endpoint */
754         list_for_each_entry(req, &ep->queue, queue) {
755                 if (&req->req == _req)
756                         break;
757         }
758         if (&req->req != _req) {
759                 spin_unlock_irqrestore(&ep->dev->lock, flags);
760                 return -EINVAL;
761         }
762
763         done(ep, req, -ECONNRESET);
764
765         spin_unlock_irqrestore(&ep->dev->lock, flags);
766         return 0;
767 }
768
769 /*
770  * Return bytes in EP FIFO
771  */
772 static int dwc2_fifo_status(struct usb_ep *_ep)
773 {
774         int count = 0;
775         struct dwc2_ep *ep;
776
777         ep = container_of(_ep, struct dwc2_ep, ep);
778         if (!_ep) {
779                 debug("%s: bad ep\n", __func__);
780                 return -ENODEV;
781         }
782
783         debug("%s: %d\n", __func__, ep_index(ep));
784
785         /* LPD can't report unclaimed bytes from IN fifos */
786         if (ep_is_in(ep))
787                 return -EOPNOTSUPP;
788
789         return count;
790 }
791
792 /*
793  * Flush EP FIFO
794  */
795 static void dwc2_fifo_flush(struct usb_ep *_ep)
796 {
797         struct dwc2_ep *ep;
798
799         ep = container_of(_ep, struct dwc2_ep, ep);
800         if (unlikely(!_ep || (!ep->desc && ep->ep.name != ep0name))) {
801                 debug("%s: bad ep\n", __func__);
802                 return;
803         }
804
805         debug("%s: %d\n", __func__, ep_index(ep));
806 }
807
808 static const struct usb_gadget_ops dwc2_udc_ops = {
809         /* current versions must always be self-powered */
810 #if CONFIG_IS_ENABLED(DM_USB_GADGET)
811         .udc_start              = dwc2_gadget_start,
812         .udc_stop               = dwc2_gadget_stop,
813 #endif
814 };
815
816 static struct dwc2_udc memory = {
817         .usb_address = 0,
818         .gadget = {
819                 .ops = &dwc2_udc_ops,
820                 .ep0 = &memory.ep[0].ep,
821                 .name = driver_name,
822         },
823
824         /* control endpoint */
825         .ep[0] = {
826                 .ep = {
827                         .name = ep0name,
828                         .ops = &dwc2_ep_ops,
829                         .maxpacket = EP0_FIFO_SIZE,
830                 },
831                 .dev = &memory,
832
833                 .bEndpointAddress = 0,
834                 .bmAttributes = 0,
835
836                 .ep_type = ep_control,
837         },
838
839         /* first group of endpoints */
840         .ep[1] = {
841                 .ep = {
842                         .name = "ep1in-bulk",
843                         .ops = &dwc2_ep_ops,
844                         .maxpacket = EP_FIFO_SIZE,
845                 },
846                 .dev = &memory,
847
848                 .bEndpointAddress = USB_DIR_IN | 1,
849                 .bmAttributes = USB_ENDPOINT_XFER_BULK,
850
851                 .ep_type = ep_bulk_out,
852                 .fifo_num = 1,
853         },
854
855         .ep[2] = {
856                 .ep = {
857                         .name = "ep2out-bulk",
858                         .ops = &dwc2_ep_ops,
859                         .maxpacket = EP_FIFO_SIZE,
860                 },
861                 .dev = &memory,
862
863                 .bEndpointAddress = USB_DIR_OUT | 2,
864                 .bmAttributes = USB_ENDPOINT_XFER_BULK,
865
866                 .ep_type = ep_bulk_in,
867                 .fifo_num = 2,
868         },
869
870         .ep[3] = {
871                 .ep = {
872                         .name = "ep3in-int",
873                         .ops = &dwc2_ep_ops,
874                         .maxpacket = EP_FIFO_SIZE,
875                 },
876                 .dev = &memory,
877
878                 .bEndpointAddress = USB_DIR_IN | 3,
879                 .bmAttributes = USB_ENDPOINT_XFER_INT,
880
881                 .ep_type = ep_interrupt,
882                 .fifo_num = 3,
883         },
884 };
885
886 /*
887  *      probe - binds to the platform device
888  */
889
890 int dwc2_udc_probe(struct dwc2_plat_otg_data *pdata)
891 {
892         struct dwc2_udc *dev = &memory;
893         int retval = 0;
894
895         debug("%s: %p\n", __func__, pdata);
896
897         dev->pdata = pdata;
898
899         reg = (struct dwc2_usbotg_reg *)pdata->regs_otg;
900
901         dev->gadget.is_dualspeed = 1;   /* Hack only*/
902         dev->gadget.is_otg = 0;
903         dev->gadget.is_a_peripheral = 0;
904         dev->gadget.b_hnp_enable = 0;
905         dev->gadget.a_hnp_support = 0;
906         dev->gadget.a_alt_hnp_support = 0;
907
908         the_controller = dev;
909
910         usb_ctrl = memalign(CONFIG_SYS_CACHELINE_SIZE,
911                             ROUND(sizeof(struct usb_ctrlrequest),
912                                   CONFIG_SYS_CACHELINE_SIZE));
913         if (!usb_ctrl) {
914                 pr_err("No memory available for UDC!\n");
915                 return -ENOMEM;
916         }
917
918         usb_ctrl_dma_addr = (dma_addr_t) usb_ctrl;
919
920         udc_reinit(dev);
921
922         return retval;
923 }
924
925 int dwc2_udc_handle_interrupt(void)
926 {
927         u32 intr_status = readl(&reg->gintsts);
928         u32 gintmsk = readl(&reg->gintmsk);
929
930         if (intr_status & gintmsk)
931                 return dwc2_udc_irq(1, (void *)the_controller);
932
933         return 0;
934 }
935
936 #if !CONFIG_IS_ENABLED(DM_USB_GADGET)
937
938 int usb_gadget_handle_interrupts(int index)
939 {
940         return dwc2_udc_handle_interrupt();
941 }
942
943 #else /* CONFIG_IS_ENABLED(DM_USB_GADGET) */
944
945 struct dwc2_priv_data {
946         struct clk_bulk         clks;
947         struct reset_ctl_bulk   resets;
948         struct phy_bulk phys;
949         struct udevice *usb33d_supply;
950 };
951
952 int dm_usb_gadget_handle_interrupts(struct udevice *dev)
953 {
954         return dwc2_udc_handle_interrupt();
955 }
956
957 static int dwc2_phy_setup(struct udevice *dev, struct phy_bulk *phys)
958 {
959         int ret;
960
961         ret = generic_phy_get_bulk(dev, phys);
962         if (ret)
963                 return ret;
964
965         ret = generic_phy_init_bulk(phys);
966         if (ret)
967                 return ret;
968
969         ret = generic_phy_power_on_bulk(phys);
970         if (ret)
971                 generic_phy_exit_bulk(phys);
972
973         return ret;
974 }
975
976 static void dwc2_phy_shutdown(struct udevice *dev, struct phy_bulk *phys)
977 {
978         generic_phy_power_off_bulk(phys);
979         generic_phy_exit_bulk(phys);
980 }
981
982 static int dwc2_udc_otg_ofdata_to_platdata(struct udevice *dev)
983 {
984         struct dwc2_plat_otg_data *platdata = dev_get_platdata(dev);
985         ulong drvdata;
986         void (*set_params)(struct dwc2_plat_otg_data *data);
987         int ret;
988
989         if (usb_get_dr_mode(dev->node) != USB_DR_MODE_PERIPHERAL &&
990             usb_get_dr_mode(dev->node) != USB_DR_MODE_OTG) {
991                 dev_dbg(dev, "Invalid mode\n");
992                 return -ENODEV;
993         }
994
995         platdata->regs_otg = dev_read_addr(dev);
996
997         platdata->rx_fifo_sz = dev_read_u32_default(dev, "g-rx-fifo-size", 0);
998         platdata->np_tx_fifo_sz = dev_read_u32_default(dev,
999                                                        "g-np-tx-fifo-size", 0);
1000
1001         platdata->tx_fifo_sz_nb =
1002                 dev_read_size(dev, "g-tx-fifo-size") / sizeof(u32);
1003         if (platdata->tx_fifo_sz_nb > DWC2_MAX_HW_ENDPOINTS)
1004                 platdata->tx_fifo_sz_nb = DWC2_MAX_HW_ENDPOINTS;
1005         if (platdata->tx_fifo_sz_nb) {
1006                 ret = dev_read_u32_array(dev, "g-tx-fifo-size",
1007                                          platdata->tx_fifo_sz_array,
1008                                          platdata->tx_fifo_sz_nb);
1009                 if (ret)
1010                         return ret;
1011         }
1012
1013         platdata->force_b_session_valid =
1014                 dev_read_bool(dev, "u-boot,force-b-session-valid");
1015
1016         /* force platdata according compatible */
1017         drvdata = dev_get_driver_data(dev);
1018         if (drvdata) {
1019                 set_params = (void *)drvdata;
1020                 set_params(platdata);
1021         }
1022
1023         return 0;
1024 }
1025
1026 static void dwc2_set_stm32mp1_hsotg_params(struct dwc2_plat_otg_data *p)
1027 {
1028         p->activate_stm_id_vb_detection = true;
1029         p->usb_gusbcfg =
1030                 0 << 15         /* PHY Low Power Clock sel*/
1031                 | 0x9 << 10     /* USB Turnaround time (0x9 for HS phy) */
1032                 | 0 << 9        /* [0:HNP disable,1:HNP enable]*/
1033                 | 0 << 8        /* [0:SRP disable 1:SRP enable]*/
1034                 | 0 << 6        /* 0: high speed utmi+, 1: full speed serial*/
1035                 | 0x7 << 0;     /* FS timeout calibration**/
1036
1037         if (p->force_b_session_valid)
1038                 p->usb_gusbcfg |= 1 << 30; /* FDMOD: Force device mode */
1039 }
1040
1041 static int dwc2_udc_otg_reset_init(struct udevice *dev,
1042                                    struct reset_ctl_bulk *resets)
1043 {
1044         int ret;
1045
1046         ret = reset_get_bulk(dev, resets);
1047         if (ret == -ENOTSUPP)
1048                 return 0;
1049
1050         if (ret)
1051                 return ret;
1052
1053         ret = reset_assert_bulk(resets);
1054
1055         if (!ret) {
1056                 udelay(2);
1057                 ret = reset_deassert_bulk(resets);
1058         }
1059         if (ret) {
1060                 reset_release_bulk(resets);
1061                 return ret;
1062         }
1063
1064         return 0;
1065 }
1066
1067 static int dwc2_udc_otg_clk_init(struct udevice *dev,
1068                                  struct clk_bulk *clks)
1069 {
1070         int ret;
1071
1072         ret = clk_get_bulk(dev, clks);
1073         if (ret == -ENOSYS)
1074                 return 0;
1075
1076         if (ret)
1077                 return ret;
1078
1079         ret = clk_enable_bulk(clks);
1080         if (ret) {
1081                 clk_release_bulk(clks);
1082                 return ret;
1083         }
1084
1085         return 0;
1086 }
1087
1088 static int dwc2_udc_otg_probe(struct udevice *dev)
1089 {
1090         struct dwc2_plat_otg_data *platdata = dev_get_platdata(dev);
1091         struct dwc2_priv_data *priv = dev_get_priv(dev);
1092         struct dwc2_usbotg_reg *usbotg_reg =
1093                 (struct dwc2_usbotg_reg *)platdata->regs_otg;
1094         int ret;
1095
1096         ret = dwc2_udc_otg_clk_init(dev, &priv->clks);
1097         if (ret)
1098                 return ret;
1099
1100         ret = dwc2_udc_otg_reset_init(dev, &priv->resets);
1101         if (ret)
1102                 return ret;
1103
1104         ret = dwc2_phy_setup(dev, &priv->phys);
1105         if (ret)
1106                 return ret;
1107
1108         if (CONFIG_IS_ENABLED(DM_REGULATOR) &&
1109             platdata->activate_stm_id_vb_detection &&
1110             !platdata->force_b_session_valid) {
1111                 ret = device_get_supply_regulator(dev, "usb33d-supply",
1112                                                   &priv->usb33d_supply);
1113                 if (ret) {
1114                         dev_err(dev, "can't get voltage level detector supply\n");
1115                         return ret;
1116                 }
1117                 ret = regulator_set_enable(priv->usb33d_supply, true);
1118                 if (ret) {
1119                         dev_err(dev, "can't enable voltage level detector supply\n");
1120                         return ret;
1121                 }
1122                 /* Enable vbus sensing */
1123                 setbits_le32(&usbotg_reg->ggpio,
1124                              GGPIO_STM32_OTG_GCCFG_VBDEN |
1125                              GGPIO_STM32_OTG_GCCFG_IDEN);
1126         }
1127
1128         if (platdata->force_b_session_valid)
1129                 /* Override B session bits : value and enable */
1130                 setbits_le32(&usbotg_reg->gotgctl,
1131                              A_VALOEN | A_VALOVAL | B_VALOEN | B_VALOVAL);
1132
1133         ret = dwc2_udc_probe(platdata);
1134         if (ret)
1135                 return ret;
1136
1137         the_controller->driver = 0;
1138
1139         ret = usb_add_gadget_udc((struct device *)dev, &the_controller->gadget);
1140
1141         return ret;
1142 }
1143
1144 static int dwc2_udc_otg_remove(struct udevice *dev)
1145 {
1146         struct dwc2_priv_data *priv = dev_get_priv(dev);
1147
1148         usb_del_gadget_udc(&the_controller->gadget);
1149
1150         reset_release_bulk(&priv->resets);
1151
1152         clk_release_bulk(&priv->clks);
1153
1154         dwc2_phy_shutdown(dev, &priv->phys);
1155
1156         return dm_scan_fdt_dev(dev);
1157 }
1158
1159 static const struct udevice_id dwc2_udc_otg_ids[] = {
1160         { .compatible = "snps,dwc2" },
1161         { .compatible = "brcm,bcm2835-usb" },
1162         { .compatible = "st,stm32mp1-hsotg",
1163           .data = (ulong)dwc2_set_stm32mp1_hsotg_params },
1164         {},
1165 };
1166
1167 U_BOOT_DRIVER(dwc2_udc_otg) = {
1168         .name   = "dwc2-udc-otg",
1169         .id     = UCLASS_USB_GADGET_GENERIC,
1170         .of_match = dwc2_udc_otg_ids,
1171         .ofdata_to_platdata = dwc2_udc_otg_ofdata_to_platdata,
1172         .probe = dwc2_udc_otg_probe,
1173         .remove = dwc2_udc_otg_remove,
1174         .platdata_auto_alloc_size = sizeof(struct dwc2_plat_otg_data),
1175         .priv_auto_alloc_size = sizeof(struct dwc2_priv_data),
1176 };
1177
1178 int dwc2_udc_B_session_valid(struct udevice *dev)
1179 {
1180         struct dwc2_plat_otg_data *platdata = dev_get_platdata(dev);
1181         struct dwc2_usbotg_reg *usbotg_reg =
1182                 (struct dwc2_usbotg_reg *)platdata->regs_otg;
1183
1184         return readl(&usbotg_reg->gotgctl) & B_SESSION_VALID;
1185 }
1186 #endif /* CONFIG_IS_ENABLED(DM_USB_GADGET) */