1 // SPDX-License-Identifier: GPL-2.0+
3 * drivers/usb/gadget/dwc2_udc_otg.c
4 * Designware DWC2 on-chip full/high speed USB OTG 2.0 device controllers
6 * Copyright (C) 2008 for Samsung Electronics
8 * BSP Support for Samsung's UDC driver
10 * git://git.kernel.org/pub/scm/linux/kernel/git/kki_ap/linux-2.6-samsung.git
12 * State machine bugfixes:
23 #include <generic-phy.h>
27 #include <dm/device_compat.h>
28 #include <dm/devres.h>
29 #include <linux/bug.h>
30 #include <linux/delay.h>
32 #include <linux/errno.h>
33 #include <linux/list.h>
35 #include <linux/usb/ch9.h>
36 #include <linux/usb/otg.h>
37 #include <linux/usb/gadget.h>
40 #include <asm/byteorder.h>
41 #include <asm/unaligned.h>
44 #include <asm/mach-types.h>
46 #include <power/regulator.h>
48 #include "dwc2_udc_otg_regs.h"
49 #include "dwc2_udc_otg_priv.h"
51 /***********************************************************/
53 #define OTG_DMA_MODE 1
58 #define DEBUG_OUT_EP 0
61 #include <usb/dwc2_udc.h>
66 static char *state_names[] = {
69 "DATA_STATE_NEED_ZLP",
70 "WAIT_FOR_OUT_STATUS",
73 "WAIT_FOR_OUT_COMPLETE",
74 "WAIT_FOR_IN_COMPLETE",
75 "WAIT_FOR_NULL_COMPLETE",
78 #define DRIVER_VERSION "15 March 2009"
80 struct dwc2_udc *the_controller;
82 static const char driver_name[] = "dwc2-udc";
83 static const char ep0name[] = "ep0-control";
86 static unsigned int ep0_fifo_size = 64;
87 static unsigned int ep_fifo_size = 512;
88 static unsigned int ep_fifo_size2 = 1024;
89 static int reset_available = 1;
91 static struct usb_ctrlrequest *usb_ctrl;
92 static dma_addr_t usb_ctrl_dma_addr;
97 static int dwc2_ep_enable(struct usb_ep *ep,
98 const struct usb_endpoint_descriptor *);
99 static int dwc2_ep_disable(struct usb_ep *ep);
100 static struct usb_request *dwc2_alloc_request(struct usb_ep *ep,
102 static void dwc2_free_request(struct usb_ep *ep, struct usb_request *);
104 static int dwc2_queue(struct usb_ep *ep, struct usb_request *, gfp_t gfp_flags);
105 static int dwc2_dequeue(struct usb_ep *ep, struct usb_request *);
106 static int dwc2_fifo_status(struct usb_ep *ep);
107 static void dwc2_fifo_flush(struct usb_ep *ep);
108 static void dwc2_ep0_read(struct dwc2_udc *dev);
109 static void dwc2_ep0_kick(struct dwc2_udc *dev, struct dwc2_ep *ep);
110 static void dwc2_handle_ep0(struct dwc2_udc *dev);
111 static int dwc2_ep0_write(struct dwc2_udc *dev);
112 static int write_fifo_ep0(struct dwc2_ep *ep, struct dwc2_request *req);
113 static void done(struct dwc2_ep *ep, struct dwc2_request *req, int status);
114 static void stop_activity(struct dwc2_udc *dev,
115 struct usb_gadget_driver *driver);
116 static int udc_enable(struct dwc2_udc *dev);
117 static void udc_set_address(struct dwc2_udc *dev, unsigned char address);
118 static void reconfig_usbd(struct dwc2_udc *dev);
119 static void set_max_pktsize(struct dwc2_udc *dev, enum usb_device_speed speed);
120 static void nuke(struct dwc2_ep *ep, int status);
121 static int dwc2_udc_set_halt(struct usb_ep *_ep, int value);
122 static void dwc2_udc_set_nak(struct dwc2_ep *ep);
124 void set_udc_gadget_private_data(void *p)
126 debug_cond(DEBUG_SETUP != 0,
127 "%s: the_controller: 0x%p, p: 0x%p\n", __func__,
129 the_controller->gadget.dev.device_data = p;
132 void *get_udc_gadget_private_data(struct usb_gadget *gadget)
134 return gadget->dev.device_data;
137 static struct usb_ep_ops dwc2_ep_ops = {
138 .enable = dwc2_ep_enable,
139 .disable = dwc2_ep_disable,
141 .alloc_request = dwc2_alloc_request,
142 .free_request = dwc2_free_request,
145 .dequeue = dwc2_dequeue,
147 .set_halt = dwc2_udc_set_halt,
148 .fifo_status = dwc2_fifo_status,
149 .fifo_flush = dwc2_fifo_flush,
152 #define create_proc_files() do {} while (0)
153 #define remove_proc_files() do {} while (0)
155 /***********************************************************/
157 struct dwc2_usbotg_reg *reg;
159 bool dfu_usb_get_reset(void)
161 return !!(readl(®->gintsts) & INT_RESET);
164 __weak void otg_phy_init(struct dwc2_udc *dev) {}
165 __weak void otg_phy_off(struct dwc2_udc *dev) {}
167 /***********************************************************/
169 #include "dwc2_udc_otg_xfer_dma.c"
172 * udc_disable - disable USB device controller
174 static void udc_disable(struct dwc2_udc *dev)
176 debug_cond(DEBUG_SETUP != 0, "%s: %p\n", __func__, dev);
178 udc_set_address(dev, 0);
180 dev->ep0state = WAIT_FOR_SETUP;
181 dev->gadget.speed = USB_SPEED_UNKNOWN;
182 dev->usb_address = 0;
188 * udc_reinit - initialize software state
190 static void udc_reinit(struct dwc2_udc *dev)
194 debug_cond(DEBUG_SETUP != 0, "%s: %p\n", __func__, dev);
196 /* device/ep0 records init */
197 INIT_LIST_HEAD(&dev->gadget.ep_list);
198 INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
199 dev->ep0state = WAIT_FOR_SETUP;
201 /* basic endpoint records init */
202 for (i = 0; i < DWC2_MAX_ENDPOINTS; i++) {
203 struct dwc2_ep *ep = &dev->ep[i];
206 list_add_tail(&ep->ep.ep_list, &dev->gadget.ep_list);
210 INIT_LIST_HEAD(&ep->queue);
214 /* the rest was statically initialized, and is read-only */
217 #define BYTES2MAXP(x) (x / 8)
218 #define MAXP2BYTES(x) (x * 8)
220 /* until it's enabled, this UDC should be completely invisible
223 static int udc_enable(struct dwc2_udc *dev)
225 debug_cond(DEBUG_SETUP != 0, "%s: %p\n", __func__, dev);
230 debug_cond(DEBUG_SETUP != 0,
231 "DWC2 USB 2.0 OTG Controller Core Initialized : 0x%x\n",
232 readl(®->gintmsk));
234 dev->gadget.speed = USB_SPEED_UNKNOWN;
239 #if !CONFIG_IS_ENABLED(DM_USB_GADGET)
241 Register entry point for the peripheral controller driver.
243 int usb_gadget_register_driver(struct usb_gadget_driver *driver)
245 struct dwc2_udc *dev = the_controller;
247 unsigned long flags = 0;
249 debug_cond(DEBUG_SETUP != 0, "%s: %s\n", __func__, "no name");
252 || (driver->speed != USB_SPEED_FULL
253 && driver->speed != USB_SPEED_HIGH)
254 || !driver->bind || !driver->disconnect || !driver->setup)
261 spin_lock_irqsave(&dev->lock, flags);
262 /* first hook up the driver ... */
263 dev->driver = driver;
264 spin_unlock_irqrestore(&dev->lock, flags);
266 if (retval) { /* TODO */
267 printf("target device_add failed, error %d\n", retval);
271 retval = driver->bind(&dev->gadget);
273 debug_cond(DEBUG_SETUP != 0,
274 "%s: bind to driver --> error %d\n",
275 dev->gadget.name, retval);
282 debug_cond(DEBUG_SETUP != 0,
283 "Registered gadget driver %s\n", dev->gadget.name);
290 * Unregister entry point for the peripheral controller driver.
292 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
294 struct dwc2_udc *dev = the_controller;
295 unsigned long flags = 0;
299 if (!driver || driver != dev->driver)
302 spin_lock_irqsave(&dev->lock, flags);
304 stop_activity(dev, driver);
305 spin_unlock_irqrestore(&dev->lock, flags);
307 driver->unbind(&dev->gadget);
309 disable_irq(IRQ_OTG);
314 #else /* !CONFIG_IS_ENABLED(DM_USB_GADGET) */
316 static int dwc2_gadget_start(struct usb_gadget *g,
317 struct usb_gadget_driver *driver)
319 struct dwc2_udc *dev = the_controller;
321 debug_cond(DEBUG_SETUP != 0, "%s: %s\n", __func__, "no name");
324 (driver->speed != USB_SPEED_FULL &&
325 driver->speed != USB_SPEED_HIGH) ||
326 !driver->bind || !driver->disconnect || !driver->setup)
335 /* first hook up the driver ... */
336 dev->driver = driver;
338 debug_cond(DEBUG_SETUP != 0,
339 "Registered gadget driver %s\n", dev->gadget.name);
340 return udc_enable(dev);
343 static int dwc2_gadget_stop(struct usb_gadget *g)
345 struct dwc2_udc *dev = the_controller;
354 stop_activity(dev, dev->driver);
361 #endif /* !CONFIG_IS_ENABLED(DM_USB_GADGET) */
364 * done - retire a request; caller blocked irqs
366 static void done(struct dwc2_ep *ep, struct dwc2_request *req, int status)
368 unsigned int stopped = ep->stopped;
370 debug("%s: %s %p, req = %p, stopped = %d\n",
371 __func__, ep->ep.name, ep, &req->req, stopped);
373 list_del_init(&req->queue);
375 if (likely(req->req.status == -EINPROGRESS))
376 req->req.status = status;
378 status = req->req.status;
380 if (status && status != -ESHUTDOWN) {
381 debug("complete %s req %p stat %d len %u/%u\n",
382 ep->ep.name, &req->req, status,
383 req->req.actual, req->req.length);
386 /* don't modify queue heads during completion callback */
390 printf("calling complete callback\n");
392 int i, len = req->req.length;
394 printf("pkt[%d] = ", req->req.length);
397 for (i = 0; i < len; i++) {
398 printf("%02x", ((u8 *)req->req.buf)[i]);
405 spin_unlock(&ep->dev->lock);
406 req->req.complete(&ep->ep, &req->req);
407 spin_lock(&ep->dev->lock);
409 debug("callback completed\n");
411 ep->stopped = stopped;
415 * nuke - dequeue ALL requests
417 static void nuke(struct dwc2_ep *ep, int status)
419 struct dwc2_request *req;
421 debug("%s: %s %p\n", __func__, ep->ep.name, ep);
423 /* called with irqs blocked */
424 while (!list_empty(&ep->queue)) {
425 req = list_entry(ep->queue.next, struct dwc2_request, queue);
426 done(ep, req, status);
430 static void stop_activity(struct dwc2_udc *dev,
431 struct usb_gadget_driver *driver)
435 /* don't disconnect drivers more than once */
436 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
438 dev->gadget.speed = USB_SPEED_UNKNOWN;
440 /* prevent new request submissions, kill any outstanding requests */
441 for (i = 0; i < DWC2_MAX_ENDPOINTS; i++) {
442 struct dwc2_ep *ep = &dev->ep[i];
444 nuke(ep, -ESHUTDOWN);
447 /* report disconnect; the driver is already quiesced */
449 spin_unlock(&dev->lock);
450 driver->disconnect(&dev->gadget);
451 spin_lock(&dev->lock);
454 /* re-init driver-visible data structures */
458 static void reconfig_usbd(struct dwc2_udc *dev)
460 /* 2. Soft-reset OTG Core and then unreset again. */
462 unsigned int uTemp = writel(CORE_SOFT_RESET, ®->grstctl);
463 uint32_t dflt_gusbcfg;
464 uint32_t rx_fifo_sz, tx_fifo_sz, np_tx_fifo_sz;
468 debug("Reseting OTG controller\n");
471 0<<15 /* PHY Low Power Clock sel*/
472 |1<<14 /* Non-Periodic TxFIFO Rewind Enable*/
473 |0x5<<10 /* Turnaround time*/
474 |0<<9 | 0<<8 /* [0:HNP disable,1:HNP enable][ 0:SRP disable*/
475 /* 1:SRP enable] H1= 1,1*/
476 |0<<7 /* Ulpi DDR sel*/
477 |0<<6 /* 0: high speed utmi+, 1: full speed serial*/
478 |0<<4 /* 0: utmi+, 1:ulpi*/
479 #ifdef CONFIG_USB_GADGET_DWC2_OTG_PHY_BUS_WIDTH_8
480 |0<<3 /* phy i/f 0:8bit, 1:16bit*/
482 |1<<3 /* phy i/f 0:8bit, 1:16bit*/
484 |0x7<<0; /* HS/FS Timeout**/
486 if (dev->pdata->usb_gusbcfg)
487 dflt_gusbcfg = dev->pdata->usb_gusbcfg;
489 writel(dflt_gusbcfg, ®->gusbcfg);
491 /* 3. Put the OTG device core in the disconnected state.*/
492 uTemp = readl(®->dctl);
493 uTemp |= SOFT_DISCONNECT;
494 writel(uTemp, ®->dctl);
498 /* 4. Make the OTG device core exit from the disconnected state.*/
499 uTemp = readl(®->dctl);
500 uTemp = uTemp & ~SOFT_DISCONNECT;
501 writel(uTemp, ®->dctl);
503 /* 5. Configure OTG Core to initial settings of device mode.*/
504 /* [][1: full speed(30Mhz) 0:high speed]*/
505 writel(EP_MISS_CNT(1) | DEV_SPEED_HIGH_SPEED_20, ®->dcfg);
509 /* 6. Unmask the core interrupts*/
510 writel(GINTMSK_INIT, ®->gintmsk);
512 /* 7. Set NAK bit of EP0, EP1, EP2*/
513 writel(DEPCTL_EPDIS|DEPCTL_SNAK, ®->out_endp[EP0_CON].doepctl);
514 writel(DEPCTL_EPDIS|DEPCTL_SNAK, ®->in_endp[EP0_CON].diepctl);
516 for (i = 1; i < DWC2_MAX_ENDPOINTS; i++) {
517 writel(DEPCTL_EPDIS|DEPCTL_SNAK, ®->out_endp[i].doepctl);
518 writel(DEPCTL_EPDIS|DEPCTL_SNAK, ®->in_endp[i].diepctl);
521 /* 8. Unmask EPO interrupts*/
522 writel(((1 << EP0_CON) << DAINT_OUT_BIT)
523 | (1 << EP0_CON), ®->daintmsk);
525 /* 9. Unmask device OUT EP common interrupts*/
526 writel(DOEPMSK_INIT, ®->doepmsk);
528 /* 10. Unmask device IN EP common interrupts*/
529 writel(DIEPMSK_INIT, ®->diepmsk);
531 rx_fifo_sz = RX_FIFO_SIZE;
532 np_tx_fifo_sz = NPTX_FIFO_SIZE;
533 tx_fifo_sz = PTX_FIFO_SIZE;
535 if (dev->pdata->rx_fifo_sz)
536 rx_fifo_sz = dev->pdata->rx_fifo_sz;
537 if (dev->pdata->np_tx_fifo_sz)
538 np_tx_fifo_sz = dev->pdata->np_tx_fifo_sz;
539 if (dev->pdata->tx_fifo_sz)
540 tx_fifo_sz = dev->pdata->tx_fifo_sz;
542 /* 11. Set Rx FIFO Size (in 32-bit words) */
543 writel(rx_fifo_sz, ®->grxfsiz);
545 /* 12. Set Non Periodic Tx FIFO Size */
546 writel((np_tx_fifo_sz << 16) | rx_fifo_sz,
549 /* retrieve the number of IN Endpoints (excluding ep0) */
550 max_hw_ep = (readl(®->ghwcfg4) & GHWCFG4_NUM_IN_EPS_MASK) >>
551 GHWCFG4_NUM_IN_EPS_SHIFT;
552 pdata_hw_ep = dev->pdata->tx_fifo_sz_nb;
554 /* tx_fifo_sz_nb should equal to number of IN Endpoint */
555 if (pdata_hw_ep && max_hw_ep != pdata_hw_ep)
556 pr_warn("Got %d hw endpoint but %d tx-fifo-size in array !!\n",
557 max_hw_ep, pdata_hw_ep);
559 for (i = 0; i < max_hw_ep; i++) {
561 tx_fifo_sz = dev->pdata->tx_fifo_sz_array[i];
563 writel((rx_fifo_sz + np_tx_fifo_sz + (tx_fifo_sz * i)) |
564 tx_fifo_sz << 16, ®->dieptxf[i]);
566 /* Flush the RX FIFO */
567 writel(RX_FIFO_FLUSH, ®->grstctl);
568 while (readl(®->grstctl) & RX_FIFO_FLUSH)
569 debug("%s: waiting for DWC2_UDC_OTG_GRSTCTL\n", __func__);
571 /* Flush all the Tx FIFO's */
572 writel(TX_FIFO_FLUSH_ALL, ®->grstctl);
573 writel(TX_FIFO_FLUSH_ALL | TX_FIFO_FLUSH, ®->grstctl);
574 while (readl(®->grstctl) & TX_FIFO_FLUSH)
575 debug("%s: waiting for DWC2_UDC_OTG_GRSTCTL\n", __func__);
577 /* 13. Clear NAK bit of EP0, EP1, EP2*/
579 /* EP0: Control OUT */
580 writel(DEPCTL_EPDIS | DEPCTL_CNAK,
581 ®->out_endp[EP0_CON].doepctl);
583 /* 14. Initialize OTG Link Core.*/
584 writel(GAHBCFG_INIT, ®->gahbcfg);
587 static void set_max_pktsize(struct dwc2_udc *dev, enum usb_device_speed speed)
589 unsigned int ep_ctrl;
592 if (speed == USB_SPEED_HIGH) {
595 ep_fifo_size2 = 1024;
596 dev->gadget.speed = USB_SPEED_HIGH;
601 dev->gadget.speed = USB_SPEED_FULL;
604 dev->ep[0].ep.maxpacket = ep0_fifo_size;
605 for (i = 1; i < DWC2_MAX_ENDPOINTS; i++)
606 dev->ep[i].ep.maxpacket = ep_fifo_size;
608 /* EP0 - Control IN (64 bytes)*/
609 ep_ctrl = readl(®->in_endp[EP0_CON].diepctl);
610 writel(ep_ctrl|(0<<0), ®->in_endp[EP0_CON].diepctl);
612 /* EP0 - Control OUT (64 bytes)*/
613 ep_ctrl = readl(®->out_endp[EP0_CON].doepctl);
614 writel(ep_ctrl|(0<<0), ®->out_endp[EP0_CON].doepctl);
617 static int dwc2_ep_enable(struct usb_ep *_ep,
618 const struct usb_endpoint_descriptor *desc)
621 struct dwc2_udc *dev;
622 unsigned long flags = 0;
624 debug("%s: %p\n", __func__, _ep);
626 ep = container_of(_ep, struct dwc2_ep, ep);
627 if (!_ep || !desc || ep->desc || _ep->name == ep0name
628 || desc->bDescriptorType != USB_DT_ENDPOINT
629 || ep->bEndpointAddress != desc->bEndpointAddress
630 || ep_maxpacket(ep) <
631 le16_to_cpu(get_unaligned(&desc->wMaxPacketSize))) {
633 debug("%s: bad ep or descriptor\n", __func__);
637 /* xfer types must match, except that interrupt ~= bulk */
638 if (ep->bmAttributes != desc->bmAttributes
639 && ep->bmAttributes != USB_ENDPOINT_XFER_BULK
640 && desc->bmAttributes != USB_ENDPOINT_XFER_INT) {
642 debug("%s: %s type mismatch\n", __func__, _ep->name);
646 /* hardware _could_ do smaller, but driver doesn't */
647 if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK &&
648 le16_to_cpu(get_unaligned(&desc->wMaxPacketSize)) >
649 ep_maxpacket(ep)) || !get_unaligned(&desc->wMaxPacketSize)) {
651 debug("%s: bad %s maxpacket\n", __func__, _ep->name);
656 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
658 debug("%s: bogus device state\n", __func__);
665 ep->ep.maxpacket = le16_to_cpu(get_unaligned(&desc->wMaxPacketSize));
667 /* Reset halt state */
668 dwc2_udc_set_nak(ep);
669 dwc2_udc_set_halt(_ep, 0);
671 spin_lock_irqsave(&ep->dev->lock, flags);
672 dwc2_udc_ep_activate(ep);
673 spin_unlock_irqrestore(&ep->dev->lock, flags);
675 debug("%s: enabled %s, stopped = %d, maxpacket = %d\n",
676 __func__, _ep->name, ep->stopped, ep->ep.maxpacket);
683 static int dwc2_ep_disable(struct usb_ep *_ep)
686 unsigned long flags = 0;
688 debug("%s: %p\n", __func__, _ep);
690 ep = container_of(_ep, struct dwc2_ep, ep);
691 if (!_ep || !ep->desc) {
692 debug("%s: %s not enabled\n", __func__,
693 _ep ? ep->ep.name : NULL);
697 spin_lock_irqsave(&ep->dev->lock, flags);
699 /* Nuke all pending requests */
700 nuke(ep, -ESHUTDOWN);
705 spin_unlock_irqrestore(&ep->dev->lock, flags);
707 debug("%s: disabled %s\n", __func__, _ep->name);
711 static struct usb_request *dwc2_alloc_request(struct usb_ep *ep,
714 struct dwc2_request *req;
716 debug("%s: %s %p\n", __func__, ep->name, ep);
718 req = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*req));
722 memset(req, 0, sizeof *req);
723 INIT_LIST_HEAD(&req->queue);
728 static void dwc2_free_request(struct usb_ep *ep, struct usb_request *_req)
730 struct dwc2_request *req;
732 debug("%s: %p\n", __func__, ep);
734 req = container_of(_req, struct dwc2_request, req);
735 WARN_ON(!list_empty(&req->queue));
739 /* dequeue JUST ONE request */
740 static int dwc2_dequeue(struct usb_ep *_ep, struct usb_request *_req)
743 struct dwc2_request *req;
744 unsigned long flags = 0;
746 debug("%s: %p\n", __func__, _ep);
748 ep = container_of(_ep, struct dwc2_ep, ep);
749 if (!_ep || ep->ep.name == ep0name)
752 spin_lock_irqsave(&ep->dev->lock, flags);
754 /* make sure it's actually queued on this endpoint */
755 list_for_each_entry(req, &ep->queue, queue) {
756 if (&req->req == _req)
759 if (&req->req != _req) {
760 spin_unlock_irqrestore(&ep->dev->lock, flags);
764 done(ep, req, -ECONNRESET);
766 spin_unlock_irqrestore(&ep->dev->lock, flags);
771 * Return bytes in EP FIFO
773 static int dwc2_fifo_status(struct usb_ep *_ep)
778 ep = container_of(_ep, struct dwc2_ep, ep);
780 debug("%s: bad ep\n", __func__);
784 debug("%s: %d\n", __func__, ep_index(ep));
786 /* LPD can't report unclaimed bytes from IN fifos */
796 static void dwc2_fifo_flush(struct usb_ep *_ep)
800 ep = container_of(_ep, struct dwc2_ep, ep);
801 if (unlikely(!_ep || (!ep->desc && ep->ep.name != ep0name))) {
802 debug("%s: bad ep\n", __func__);
806 debug("%s: %d\n", __func__, ep_index(ep));
809 static const struct usb_gadget_ops dwc2_udc_ops = {
810 /* current versions must always be self-powered */
811 #if CONFIG_IS_ENABLED(DM_USB_GADGET)
812 .udc_start = dwc2_gadget_start,
813 .udc_stop = dwc2_gadget_stop,
817 static struct dwc2_udc memory = {
820 .ops = &dwc2_udc_ops,
821 .ep0 = &memory.ep[0].ep,
825 /* control endpoint */
830 .maxpacket = EP0_FIFO_SIZE,
834 .bEndpointAddress = 0,
837 .ep_type = ep_control,
840 /* first group of endpoints */
843 .name = "ep1in-bulk",
845 .maxpacket = EP_FIFO_SIZE,
849 .bEndpointAddress = USB_DIR_IN | 1,
850 .bmAttributes = USB_ENDPOINT_XFER_BULK,
852 .ep_type = ep_bulk_out,
858 .name = "ep2out-bulk",
860 .maxpacket = EP_FIFO_SIZE,
864 .bEndpointAddress = USB_DIR_OUT | 2,
865 .bmAttributes = USB_ENDPOINT_XFER_BULK,
867 .ep_type = ep_bulk_in,
875 .maxpacket = EP_FIFO_SIZE,
879 .bEndpointAddress = USB_DIR_IN | 3,
880 .bmAttributes = USB_ENDPOINT_XFER_INT,
882 .ep_type = ep_interrupt,
888 * probe - binds to the platform device
891 int dwc2_udc_probe(struct dwc2_plat_otg_data *pdata)
893 struct dwc2_udc *dev = &memory;
896 debug("%s: %p\n", __func__, pdata);
900 reg = (struct dwc2_usbotg_reg *)pdata->regs_otg;
902 dev->gadget.is_dualspeed = 1; /* Hack only*/
903 dev->gadget.is_otg = 0;
904 dev->gadget.is_a_peripheral = 0;
905 dev->gadget.b_hnp_enable = 0;
906 dev->gadget.a_hnp_support = 0;
907 dev->gadget.a_alt_hnp_support = 0;
909 the_controller = dev;
911 usb_ctrl = memalign(CONFIG_SYS_CACHELINE_SIZE,
912 ROUND(sizeof(struct usb_ctrlrequest),
913 CONFIG_SYS_CACHELINE_SIZE));
915 pr_err("No memory available for UDC!\n");
919 usb_ctrl_dma_addr = (dma_addr_t) usb_ctrl;
926 int dwc2_udc_handle_interrupt(void)
928 u32 intr_status = readl(®->gintsts);
929 u32 gintmsk = readl(®->gintmsk);
931 if (intr_status & gintmsk)
932 return dwc2_udc_irq(1, (void *)the_controller);
937 #if !CONFIG_IS_ENABLED(DM_USB_GADGET)
939 int usb_gadget_handle_interrupts(int index)
941 return dwc2_udc_handle_interrupt();
944 #else /* CONFIG_IS_ENABLED(DM_USB_GADGET) */
946 struct dwc2_priv_data {
947 struct clk_bulk clks;
948 struct reset_ctl_bulk resets;
949 struct phy_bulk phys;
950 struct udevice *usb33d_supply;
953 int dm_usb_gadget_handle_interrupts(struct udevice *dev)
955 return dwc2_udc_handle_interrupt();
958 static int dwc2_phy_setup(struct udevice *dev, struct phy_bulk *phys)
962 ret = generic_phy_get_bulk(dev, phys);
966 ret = generic_phy_init_bulk(phys);
970 ret = generic_phy_power_on_bulk(phys);
972 generic_phy_exit_bulk(phys);
977 static void dwc2_phy_shutdown(struct udevice *dev, struct phy_bulk *phys)
979 generic_phy_power_off_bulk(phys);
980 generic_phy_exit_bulk(phys);
983 static int dwc2_udc_otg_ofdata_to_platdata(struct udevice *dev)
985 struct dwc2_plat_otg_data *platdata = dev_get_platdata(dev);
987 void (*set_params)(struct dwc2_plat_otg_data *data);
990 if (usb_get_dr_mode(dev->node) != USB_DR_MODE_PERIPHERAL &&
991 usb_get_dr_mode(dev->node) != USB_DR_MODE_OTG) {
992 dev_dbg(dev, "Invalid mode\n");
996 platdata->regs_otg = dev_read_addr(dev);
998 platdata->rx_fifo_sz = dev_read_u32_default(dev, "g-rx-fifo-size", 0);
999 platdata->np_tx_fifo_sz = dev_read_u32_default(dev,
1000 "g-np-tx-fifo-size", 0);
1002 platdata->tx_fifo_sz_nb =
1003 dev_read_size(dev, "g-tx-fifo-size") / sizeof(u32);
1004 if (platdata->tx_fifo_sz_nb > DWC2_MAX_HW_ENDPOINTS)
1005 platdata->tx_fifo_sz_nb = DWC2_MAX_HW_ENDPOINTS;
1006 if (platdata->tx_fifo_sz_nb) {
1007 ret = dev_read_u32_array(dev, "g-tx-fifo-size",
1008 platdata->tx_fifo_sz_array,
1009 platdata->tx_fifo_sz_nb);
1014 platdata->force_b_session_valid =
1015 dev_read_bool(dev, "u-boot,force-b-session-valid");
1017 /* force platdata according compatible */
1018 drvdata = dev_get_driver_data(dev);
1020 set_params = (void *)drvdata;
1021 set_params(platdata);
1027 static void dwc2_set_stm32mp1_hsotg_params(struct dwc2_plat_otg_data *p)
1029 p->activate_stm_id_vb_detection = true;
1031 0 << 15 /* PHY Low Power Clock sel*/
1032 | 0x9 << 10 /* USB Turnaround time (0x9 for HS phy) */
1033 | 0 << 9 /* [0:HNP disable,1:HNP enable]*/
1034 | 0 << 8 /* [0:SRP disable 1:SRP enable]*/
1035 | 0 << 6 /* 0: high speed utmi+, 1: full speed serial*/
1036 | 0x7 << 0; /* FS timeout calibration**/
1038 if (p->force_b_session_valid)
1039 p->usb_gusbcfg |= 1 << 30; /* FDMOD: Force device mode */
1042 static int dwc2_udc_otg_reset_init(struct udevice *dev,
1043 struct reset_ctl_bulk *resets)
1047 ret = reset_get_bulk(dev, resets);
1048 if (ret == -ENOTSUPP)
1054 ret = reset_assert_bulk(resets);
1058 ret = reset_deassert_bulk(resets);
1061 reset_release_bulk(resets);
1068 static int dwc2_udc_otg_clk_init(struct udevice *dev,
1069 struct clk_bulk *clks)
1073 ret = clk_get_bulk(dev, clks);
1080 ret = clk_enable_bulk(clks);
1082 clk_release_bulk(clks);
1089 static int dwc2_udc_otg_probe(struct udevice *dev)
1091 struct dwc2_plat_otg_data *platdata = dev_get_platdata(dev);
1092 struct dwc2_priv_data *priv = dev_get_priv(dev);
1093 struct dwc2_usbotg_reg *usbotg_reg =
1094 (struct dwc2_usbotg_reg *)platdata->regs_otg;
1097 ret = dwc2_udc_otg_clk_init(dev, &priv->clks);
1101 ret = dwc2_udc_otg_reset_init(dev, &priv->resets);
1105 ret = dwc2_phy_setup(dev, &priv->phys);
1109 if (CONFIG_IS_ENABLED(DM_REGULATOR) &&
1110 platdata->activate_stm_id_vb_detection &&
1111 !platdata->force_b_session_valid) {
1112 ret = device_get_supply_regulator(dev, "usb33d-supply",
1113 &priv->usb33d_supply);
1115 dev_err(dev, "can't get voltage level detector supply\n");
1118 ret = regulator_set_enable(priv->usb33d_supply, true);
1120 dev_err(dev, "can't enable voltage level detector supply\n");
1123 /* Enable vbus sensing */
1124 setbits_le32(&usbotg_reg->ggpio,
1125 GGPIO_STM32_OTG_GCCFG_VBDEN |
1126 GGPIO_STM32_OTG_GCCFG_IDEN);
1129 if (platdata->force_b_session_valid)
1130 /* Override B session bits : value and enable */
1131 setbits_le32(&usbotg_reg->gotgctl,
1132 A_VALOEN | A_VALOVAL | B_VALOEN | B_VALOVAL);
1134 ret = dwc2_udc_probe(platdata);
1138 the_controller->driver = 0;
1140 ret = usb_add_gadget_udc((struct device *)dev, &the_controller->gadget);
1145 static int dwc2_udc_otg_remove(struct udevice *dev)
1147 struct dwc2_priv_data *priv = dev_get_priv(dev);
1149 usb_del_gadget_udc(&the_controller->gadget);
1151 reset_release_bulk(&priv->resets);
1153 clk_release_bulk(&priv->clks);
1155 dwc2_phy_shutdown(dev, &priv->phys);
1157 return dm_scan_fdt_dev(dev);
1160 static const struct udevice_id dwc2_udc_otg_ids[] = {
1161 { .compatible = "snps,dwc2" },
1162 { .compatible = "brcm,bcm2835-usb" },
1163 { .compatible = "st,stm32mp1-hsotg",
1164 .data = (ulong)dwc2_set_stm32mp1_hsotg_params },
1168 U_BOOT_DRIVER(dwc2_udc_otg) = {
1169 .name = "dwc2-udc-otg",
1170 .id = UCLASS_USB_GADGET_GENERIC,
1171 .of_match = dwc2_udc_otg_ids,
1172 .ofdata_to_platdata = dwc2_udc_otg_ofdata_to_platdata,
1173 .probe = dwc2_udc_otg_probe,
1174 .remove = dwc2_udc_otg_remove,
1175 .platdata_auto_alloc_size = sizeof(struct dwc2_plat_otg_data),
1176 .priv_auto_alloc_size = sizeof(struct dwc2_priv_data),
1179 int dwc2_udc_B_session_valid(struct udevice *dev)
1181 struct dwc2_plat_otg_data *platdata = dev_get_platdata(dev);
1182 struct dwc2_usbotg_reg *usbotg_reg =
1183 (struct dwc2_usbotg_reg *)platdata->regs_otg;
1185 return readl(&usbotg_reg->gotgctl) & B_SESSION_VALID;
1187 #endif /* CONFIG_IS_ENABLED(DM_USB_GADGET) */