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