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