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:
13 * Marek Szyprowski <m.szyprowski@samsung.com>
16 * Marek Szyprowski <m.szyprowski@samsung.com>
17 * Lukasz Majewski <l.majewski@samsumg.com>
21 #include <linux/errno.h>
22 #include <linux/list.h>
25 #include <linux/usb/ch9.h>
26 #include <linux/usb/gadget.h>
28 #include <asm/byteorder.h>
29 #include <asm/unaligned.h>
32 #include <asm/mach-types.h>
34 #include "dwc2_udc_otg_regs.h"
35 #include "dwc2_udc_otg_priv.h"
37 /***********************************************************/
39 #define OTG_DMA_MODE 1
44 #define DEBUG_OUT_EP 0
47 #include <usb/dwc2_udc.h>
52 static char *state_names[] = {
55 "DATA_STATE_NEED_ZLP",
56 "WAIT_FOR_OUT_STATUS",
59 "WAIT_FOR_OUT_COMPLETE",
60 "WAIT_FOR_IN_COMPLETE",
61 "WAIT_FOR_NULL_COMPLETE",
64 #define DRIVER_VERSION "15 March 2009"
66 struct dwc2_udc *the_controller;
68 static const char driver_name[] = "dwc2-udc";
69 static const char ep0name[] = "ep0-control";
72 static unsigned int ep0_fifo_size = 64;
73 static unsigned int ep_fifo_size = 512;
74 static unsigned int ep_fifo_size2 = 1024;
75 static int reset_available = 1;
77 static struct usb_ctrlrequest *usb_ctrl;
78 static dma_addr_t usb_ctrl_dma_addr;
83 static int dwc2_ep_enable(struct usb_ep *ep,
84 const struct usb_endpoint_descriptor *);
85 static int dwc2_ep_disable(struct usb_ep *ep);
86 static struct usb_request *dwc2_alloc_request(struct usb_ep *ep,
88 static void dwc2_free_request(struct usb_ep *ep, struct usb_request *);
90 static int dwc2_queue(struct usb_ep *ep, struct usb_request *, gfp_t gfp_flags);
91 static int dwc2_dequeue(struct usb_ep *ep, struct usb_request *);
92 static int dwc2_fifo_status(struct usb_ep *ep);
93 static void dwc2_fifo_flush(struct usb_ep *ep);
94 static void dwc2_ep0_read(struct dwc2_udc *dev);
95 static void dwc2_ep0_kick(struct dwc2_udc *dev, struct dwc2_ep *ep);
96 static void dwc2_handle_ep0(struct dwc2_udc *dev);
97 static int dwc2_ep0_write(struct dwc2_udc *dev);
98 static int write_fifo_ep0(struct dwc2_ep *ep, struct dwc2_request *req);
99 static void done(struct dwc2_ep *ep, struct dwc2_request *req, int status);
100 static void stop_activity(struct dwc2_udc *dev,
101 struct usb_gadget_driver *driver);
102 static int udc_enable(struct dwc2_udc *dev);
103 static void udc_set_address(struct dwc2_udc *dev, unsigned char address);
104 static void reconfig_usbd(struct dwc2_udc *dev);
105 static void set_max_pktsize(struct dwc2_udc *dev, enum usb_device_speed speed);
106 static void nuke(struct dwc2_ep *ep, int status);
107 static int dwc2_udc_set_halt(struct usb_ep *_ep, int value);
108 static void dwc2_udc_set_nak(struct dwc2_ep *ep);
110 void set_udc_gadget_private_data(void *p)
112 debug_cond(DEBUG_SETUP != 0,
113 "%s: the_controller: 0x%p, p: 0x%p\n", __func__,
115 the_controller->gadget.dev.device_data = p;
118 void *get_udc_gadget_private_data(struct usb_gadget *gadget)
120 return gadget->dev.device_data;
123 static struct usb_ep_ops dwc2_ep_ops = {
124 .enable = dwc2_ep_enable,
125 .disable = dwc2_ep_disable,
127 .alloc_request = dwc2_alloc_request,
128 .free_request = dwc2_free_request,
131 .dequeue = dwc2_dequeue,
133 .set_halt = dwc2_udc_set_halt,
134 .fifo_status = dwc2_fifo_status,
135 .fifo_flush = dwc2_fifo_flush,
138 #define create_proc_files() do {} while (0)
139 #define remove_proc_files() do {} while (0)
141 /***********************************************************/
143 struct dwc2_usbotg_reg *reg;
145 bool dfu_usb_get_reset(void)
147 return !!(readl(®->gintsts) & INT_RESET);
150 __weak void otg_phy_init(struct dwc2_udc *dev) {}
151 __weak void otg_phy_off(struct dwc2_udc *dev) {}
153 /***********************************************************/
155 #include "dwc2_udc_otg_xfer_dma.c"
158 * udc_disable - disable USB device controller
160 static void udc_disable(struct dwc2_udc *dev)
162 debug_cond(DEBUG_SETUP != 0, "%s: %p\n", __func__, dev);
164 udc_set_address(dev, 0);
166 dev->ep0state = WAIT_FOR_SETUP;
167 dev->gadget.speed = USB_SPEED_UNKNOWN;
168 dev->usb_address = 0;
174 * udc_reinit - initialize software state
176 static void udc_reinit(struct dwc2_udc *dev)
180 debug_cond(DEBUG_SETUP != 0, "%s: %p\n", __func__, dev);
182 /* device/ep0 records init */
183 INIT_LIST_HEAD(&dev->gadget.ep_list);
184 INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
185 dev->ep0state = WAIT_FOR_SETUP;
187 /* basic endpoint records init */
188 for (i = 0; i < DWC2_MAX_ENDPOINTS; i++) {
189 struct dwc2_ep *ep = &dev->ep[i];
192 list_add_tail(&ep->ep.ep_list, &dev->gadget.ep_list);
196 INIT_LIST_HEAD(&ep->queue);
200 /* the rest was statically initialized, and is read-only */
203 #define BYTES2MAXP(x) (x / 8)
204 #define MAXP2BYTES(x) (x * 8)
206 /* until it's enabled, this UDC should be completely invisible
209 static int udc_enable(struct dwc2_udc *dev)
211 debug_cond(DEBUG_SETUP != 0, "%s: %p\n", __func__, dev);
216 debug_cond(DEBUG_SETUP != 0,
217 "DWC2 USB 2.0 OTG Controller Core Initialized : 0x%x\n",
218 readl(®->gintmsk));
220 dev->gadget.speed = USB_SPEED_UNKNOWN;
226 Register entry point for the peripheral controller driver.
228 int usb_gadget_register_driver(struct usb_gadget_driver *driver)
230 struct dwc2_udc *dev = the_controller;
232 unsigned long flags = 0;
234 debug_cond(DEBUG_SETUP != 0, "%s: %s\n", __func__, "no name");
237 || (driver->speed != USB_SPEED_FULL
238 && driver->speed != USB_SPEED_HIGH)
239 || !driver->bind || !driver->disconnect || !driver->setup)
246 spin_lock_irqsave(&dev->lock, flags);
247 /* first hook up the driver ... */
248 dev->driver = driver;
249 spin_unlock_irqrestore(&dev->lock, flags);
251 if (retval) { /* TODO */
252 printf("target device_add failed, error %d\n", retval);
256 retval = driver->bind(&dev->gadget);
258 debug_cond(DEBUG_SETUP != 0,
259 "%s: bind to driver --> error %d\n",
260 dev->gadget.name, retval);
267 debug_cond(DEBUG_SETUP != 0,
268 "Registered gadget driver %s\n", dev->gadget.name);
275 * Unregister entry point for the peripheral controller driver.
277 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
279 struct dwc2_udc *dev = the_controller;
280 unsigned long flags = 0;
284 if (!driver || driver != dev->driver)
287 spin_lock_irqsave(&dev->lock, flags);
289 stop_activity(dev, driver);
290 spin_unlock_irqrestore(&dev->lock, flags);
292 driver->unbind(&dev->gadget);
294 disable_irq(IRQ_OTG);
301 * done - retire a request; caller blocked irqs
303 static void done(struct dwc2_ep *ep, struct dwc2_request *req, int status)
305 unsigned int stopped = ep->stopped;
307 debug("%s: %s %p, req = %p, stopped = %d\n",
308 __func__, ep->ep.name, ep, &req->req, stopped);
310 list_del_init(&req->queue);
312 if (likely(req->req.status == -EINPROGRESS))
313 req->req.status = status;
315 status = req->req.status;
317 if (status && status != -ESHUTDOWN) {
318 debug("complete %s req %p stat %d len %u/%u\n",
319 ep->ep.name, &req->req, status,
320 req->req.actual, req->req.length);
323 /* don't modify queue heads during completion callback */
327 printf("calling complete callback\n");
329 int i, len = req->req.length;
331 printf("pkt[%d] = ", req->req.length);
334 for (i = 0; i < len; i++) {
335 printf("%02x", ((u8 *)req->req.buf)[i]);
342 spin_unlock(&ep->dev->lock);
343 req->req.complete(&ep->ep, &req->req);
344 spin_lock(&ep->dev->lock);
346 debug("callback completed\n");
348 ep->stopped = stopped;
352 * nuke - dequeue ALL requests
354 static void nuke(struct dwc2_ep *ep, int status)
356 struct dwc2_request *req;
358 debug("%s: %s %p\n", __func__, ep->ep.name, ep);
360 /* called with irqs blocked */
361 while (!list_empty(&ep->queue)) {
362 req = list_entry(ep->queue.next, struct dwc2_request, queue);
363 done(ep, req, status);
367 static void stop_activity(struct dwc2_udc *dev,
368 struct usb_gadget_driver *driver)
372 /* don't disconnect drivers more than once */
373 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
375 dev->gadget.speed = USB_SPEED_UNKNOWN;
377 /* prevent new request submissions, kill any outstanding requests */
378 for (i = 0; i < DWC2_MAX_ENDPOINTS; i++) {
379 struct dwc2_ep *ep = &dev->ep[i];
381 nuke(ep, -ESHUTDOWN);
384 /* report disconnect; the driver is already quiesced */
386 spin_unlock(&dev->lock);
387 driver->disconnect(&dev->gadget);
388 spin_lock(&dev->lock);
391 /* re-init driver-visible data structures */
395 static void reconfig_usbd(struct dwc2_udc *dev)
397 /* 2. Soft-reset OTG Core and then unreset again. */
399 unsigned int uTemp = writel(CORE_SOFT_RESET, ®->grstctl);
400 uint32_t dflt_gusbcfg;
401 uint32_t rx_fifo_sz, tx_fifo_sz, np_tx_fifo_sz;
403 debug("Reseting OTG controller\n");
406 0<<15 /* PHY Low Power Clock sel*/
407 |1<<14 /* Non-Periodic TxFIFO Rewind Enable*/
408 |0x5<<10 /* Turnaround time*/
409 |0<<9 | 0<<8 /* [0:HNP disable,1:HNP enable][ 0:SRP disable*/
410 /* 1:SRP enable] H1= 1,1*/
411 |0<<7 /* Ulpi DDR sel*/
412 |0<<6 /* 0: high speed utmi+, 1: full speed serial*/
413 |0<<4 /* 0: utmi+, 1:ulpi*/
414 #ifdef CONFIG_USB_GADGET_DWC2_OTG_PHY_BUS_WIDTH_8
415 |0<<3 /* phy i/f 0:8bit, 1:16bit*/
417 |1<<3 /* phy i/f 0:8bit, 1:16bit*/
419 |0x7<<0; /* HS/FS Timeout**/
421 if (dev->pdata->usb_gusbcfg)
422 dflt_gusbcfg = dev->pdata->usb_gusbcfg;
424 writel(dflt_gusbcfg, ®->gusbcfg);
426 /* 3. Put the OTG device core in the disconnected state.*/
427 uTemp = readl(®->dctl);
428 uTemp |= SOFT_DISCONNECT;
429 writel(uTemp, ®->dctl);
433 /* 4. Make the OTG device core exit from the disconnected state.*/
434 uTemp = readl(®->dctl);
435 uTemp = uTemp & ~SOFT_DISCONNECT;
436 writel(uTemp, ®->dctl);
438 /* 5. Configure OTG Core to initial settings of device mode.*/
439 /* [][1: full speed(30Mhz) 0:high speed]*/
440 writel(EP_MISS_CNT(1) | DEV_SPEED_HIGH_SPEED_20, ®->dcfg);
444 /* 6. Unmask the core interrupts*/
445 writel(GINTMSK_INIT, ®->gintmsk);
447 /* 7. Set NAK bit of EP0, EP1, EP2*/
448 writel(DEPCTL_EPDIS|DEPCTL_SNAK, ®->out_endp[EP0_CON].doepctl);
449 writel(DEPCTL_EPDIS|DEPCTL_SNAK, ®->in_endp[EP0_CON].diepctl);
451 for (i = 1; i < DWC2_MAX_ENDPOINTS; i++) {
452 writel(DEPCTL_EPDIS|DEPCTL_SNAK, ®->out_endp[i].doepctl);
453 writel(DEPCTL_EPDIS|DEPCTL_SNAK, ®->in_endp[i].diepctl);
456 /* 8. Unmask EPO interrupts*/
457 writel(((1 << EP0_CON) << DAINT_OUT_BIT)
458 | (1 << EP0_CON), ®->daintmsk);
460 /* 9. Unmask device OUT EP common interrupts*/
461 writel(DOEPMSK_INIT, ®->doepmsk);
463 /* 10. Unmask device IN EP common interrupts*/
464 writel(DIEPMSK_INIT, ®->diepmsk);
466 rx_fifo_sz = RX_FIFO_SIZE;
467 np_tx_fifo_sz = NPTX_FIFO_SIZE;
468 tx_fifo_sz = PTX_FIFO_SIZE;
470 if (dev->pdata->rx_fifo_sz)
471 rx_fifo_sz = dev->pdata->rx_fifo_sz;
472 if (dev->pdata->np_tx_fifo_sz)
473 np_tx_fifo_sz = dev->pdata->np_tx_fifo_sz;
474 if (dev->pdata->tx_fifo_sz)
475 tx_fifo_sz = dev->pdata->tx_fifo_sz;
477 /* 11. Set Rx FIFO Size (in 32-bit words) */
478 writel(rx_fifo_sz, ®->grxfsiz);
480 /* 12. Set Non Periodic Tx FIFO Size */
481 writel((np_tx_fifo_sz << 16) | rx_fifo_sz,
484 for (i = 1; i < DWC2_MAX_HW_ENDPOINTS; i++)
485 writel((rx_fifo_sz + np_tx_fifo_sz + tx_fifo_sz*(i-1)) |
486 tx_fifo_sz << 16, ®->dieptxf[i-1]);
488 /* Flush the RX FIFO */
489 writel(RX_FIFO_FLUSH, ®->grstctl);
490 while (readl(®->grstctl) & RX_FIFO_FLUSH)
491 debug("%s: waiting for DWC2_UDC_OTG_GRSTCTL\n", __func__);
493 /* Flush all the Tx FIFO's */
494 writel(TX_FIFO_FLUSH_ALL, ®->grstctl);
495 writel(TX_FIFO_FLUSH_ALL | TX_FIFO_FLUSH, ®->grstctl);
496 while (readl(®->grstctl) & TX_FIFO_FLUSH)
497 debug("%s: waiting for DWC2_UDC_OTG_GRSTCTL\n", __func__);
499 /* 13. Clear NAK bit of EP0, EP1, EP2*/
501 /* EP0: Control OUT */
502 writel(DEPCTL_EPDIS | DEPCTL_CNAK,
503 ®->out_endp[EP0_CON].doepctl);
505 /* 14. Initialize OTG Link Core.*/
506 writel(GAHBCFG_INIT, ®->gahbcfg);
509 static void set_max_pktsize(struct dwc2_udc *dev, enum usb_device_speed speed)
511 unsigned int ep_ctrl;
514 if (speed == USB_SPEED_HIGH) {
517 ep_fifo_size2 = 1024;
518 dev->gadget.speed = USB_SPEED_HIGH;
523 dev->gadget.speed = USB_SPEED_FULL;
526 dev->ep[0].ep.maxpacket = ep0_fifo_size;
527 for (i = 1; i < DWC2_MAX_ENDPOINTS; i++)
528 dev->ep[i].ep.maxpacket = ep_fifo_size;
530 /* EP0 - Control IN (64 bytes)*/
531 ep_ctrl = readl(®->in_endp[EP0_CON].diepctl);
532 writel(ep_ctrl|(0<<0), ®->in_endp[EP0_CON].diepctl);
534 /* EP0 - Control OUT (64 bytes)*/
535 ep_ctrl = readl(®->out_endp[EP0_CON].doepctl);
536 writel(ep_ctrl|(0<<0), ®->out_endp[EP0_CON].doepctl);
539 static int dwc2_ep_enable(struct usb_ep *_ep,
540 const struct usb_endpoint_descriptor *desc)
543 struct dwc2_udc *dev;
544 unsigned long flags = 0;
546 debug("%s: %p\n", __func__, _ep);
548 ep = container_of(_ep, struct dwc2_ep, ep);
549 if (!_ep || !desc || ep->desc || _ep->name == ep0name
550 || desc->bDescriptorType != USB_DT_ENDPOINT
551 || ep->bEndpointAddress != desc->bEndpointAddress
552 || ep_maxpacket(ep) <
553 le16_to_cpu(get_unaligned(&desc->wMaxPacketSize))) {
555 debug("%s: bad ep or descriptor\n", __func__);
559 /* xfer types must match, except that interrupt ~= bulk */
560 if (ep->bmAttributes != desc->bmAttributes
561 && ep->bmAttributes != USB_ENDPOINT_XFER_BULK
562 && desc->bmAttributes != USB_ENDPOINT_XFER_INT) {
564 debug("%s: %s type mismatch\n", __func__, _ep->name);
568 /* hardware _could_ do smaller, but driver doesn't */
569 if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK &&
570 le16_to_cpu(get_unaligned(&desc->wMaxPacketSize)) >
571 ep_maxpacket(ep)) || !get_unaligned(&desc->wMaxPacketSize)) {
573 debug("%s: bad %s maxpacket\n", __func__, _ep->name);
578 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
580 debug("%s: bogus device state\n", __func__);
587 ep->ep.maxpacket = le16_to_cpu(get_unaligned(&desc->wMaxPacketSize));
589 /* Reset halt state */
590 dwc2_udc_set_nak(ep);
591 dwc2_udc_set_halt(_ep, 0);
593 spin_lock_irqsave(&ep->dev->lock, flags);
594 dwc2_udc_ep_activate(ep);
595 spin_unlock_irqrestore(&ep->dev->lock, flags);
597 debug("%s: enabled %s, stopped = %d, maxpacket = %d\n",
598 __func__, _ep->name, ep->stopped, ep->ep.maxpacket);
605 static int dwc2_ep_disable(struct usb_ep *_ep)
608 unsigned long flags = 0;
610 debug("%s: %p\n", __func__, _ep);
612 ep = container_of(_ep, struct dwc2_ep, ep);
613 if (!_ep || !ep->desc) {
614 debug("%s: %s not enabled\n", __func__,
615 _ep ? ep->ep.name : NULL);
619 spin_lock_irqsave(&ep->dev->lock, flags);
621 /* Nuke all pending requests */
622 nuke(ep, -ESHUTDOWN);
627 spin_unlock_irqrestore(&ep->dev->lock, flags);
629 debug("%s: disabled %s\n", __func__, _ep->name);
633 static struct usb_request *dwc2_alloc_request(struct usb_ep *ep,
636 struct dwc2_request *req;
638 debug("%s: %s %p\n", __func__, ep->name, ep);
640 req = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*req));
644 memset(req, 0, sizeof *req);
645 INIT_LIST_HEAD(&req->queue);
650 static void dwc2_free_request(struct usb_ep *ep, struct usb_request *_req)
652 struct dwc2_request *req;
654 debug("%s: %p\n", __func__, ep);
656 req = container_of(_req, struct dwc2_request, req);
657 WARN_ON(!list_empty(&req->queue));
661 /* dequeue JUST ONE request */
662 static int dwc2_dequeue(struct usb_ep *_ep, struct usb_request *_req)
665 struct dwc2_request *req;
666 unsigned long flags = 0;
668 debug("%s: %p\n", __func__, _ep);
670 ep = container_of(_ep, struct dwc2_ep, ep);
671 if (!_ep || ep->ep.name == ep0name)
674 spin_lock_irqsave(&ep->dev->lock, flags);
676 /* make sure it's actually queued on this endpoint */
677 list_for_each_entry(req, &ep->queue, queue) {
678 if (&req->req == _req)
681 if (&req->req != _req) {
682 spin_unlock_irqrestore(&ep->dev->lock, flags);
686 done(ep, req, -ECONNRESET);
688 spin_unlock_irqrestore(&ep->dev->lock, flags);
693 * Return bytes in EP FIFO
695 static int dwc2_fifo_status(struct usb_ep *_ep)
700 ep = container_of(_ep, struct dwc2_ep, ep);
702 debug("%s: bad ep\n", __func__);
706 debug("%s: %d\n", __func__, ep_index(ep));
708 /* LPD can't report unclaimed bytes from IN fifos */
718 static void dwc2_fifo_flush(struct usb_ep *_ep)
722 ep = container_of(_ep, struct dwc2_ep, ep);
723 if (unlikely(!_ep || (!ep->desc && ep->ep.name != ep0name))) {
724 debug("%s: bad ep\n", __func__);
728 debug("%s: %d\n", __func__, ep_index(ep));
731 static const struct usb_gadget_ops dwc2_udc_ops = {
732 /* current versions must always be self-powered */
735 static struct dwc2_udc memory = {
738 .ops = &dwc2_udc_ops,
739 .ep0 = &memory.ep[0].ep,
743 /* control endpoint */
748 .maxpacket = EP0_FIFO_SIZE,
752 .bEndpointAddress = 0,
755 .ep_type = ep_control,
758 /* first group of endpoints */
761 .name = "ep1in-bulk",
763 .maxpacket = EP_FIFO_SIZE,
767 .bEndpointAddress = USB_DIR_IN | 1,
768 .bmAttributes = USB_ENDPOINT_XFER_BULK,
770 .ep_type = ep_bulk_out,
776 .name = "ep2out-bulk",
778 .maxpacket = EP_FIFO_SIZE,
782 .bEndpointAddress = USB_DIR_OUT | 2,
783 .bmAttributes = USB_ENDPOINT_XFER_BULK,
785 .ep_type = ep_bulk_in,
793 .maxpacket = EP_FIFO_SIZE,
797 .bEndpointAddress = USB_DIR_IN | 3,
798 .bmAttributes = USB_ENDPOINT_XFER_INT,
800 .ep_type = ep_interrupt,
806 * probe - binds to the platform device
809 int dwc2_udc_probe(struct dwc2_plat_otg_data *pdata)
811 struct dwc2_udc *dev = &memory;
814 debug("%s: %p\n", __func__, pdata);
818 reg = (struct dwc2_usbotg_reg *)pdata->regs_otg;
820 dev->gadget.is_dualspeed = 1; /* Hack only*/
821 dev->gadget.is_otg = 0;
822 dev->gadget.is_a_peripheral = 0;
823 dev->gadget.b_hnp_enable = 0;
824 dev->gadget.a_hnp_support = 0;
825 dev->gadget.a_alt_hnp_support = 0;
827 the_controller = dev;
829 usb_ctrl = memalign(CONFIG_SYS_CACHELINE_SIZE,
830 ROUND(sizeof(struct usb_ctrlrequest),
831 CONFIG_SYS_CACHELINE_SIZE));
833 pr_err("No memory available for UDC!\n");
837 usb_ctrl_dma_addr = (dma_addr_t) usb_ctrl;
844 int usb_gadget_handle_interrupts(int index)
846 u32 intr_status = readl(®->gintsts);
847 u32 gintmsk = readl(®->gintmsk);
849 if (intr_status & gintmsk)
850 return dwc2_udc_irq(1, (void *)the_controller);