2 * drivers/usb/gadget/s3c_udc_otg.c
3 * Samsung S3C on-chip full/high speed USB OTG 2.0 device controllers
5 * Copyright (C) 2008 for Samsung Electronics
7 * BSP Support for Samsung's UDC driver
9 * git://git.kernel.org/pub/scm/linux/kernel/git/kki_ap/linux-2.6-samsung.git
11 * State machine bugfixes:
12 * Marek Szyprowski <m.szyprowski@samsung.com>
15 * Marek Szyprowski <m.szyprowski@samsung.com>
16 * Lukasz Majewski <l.majewski@samsumg.com>
18 * SPDX-License-Identifier: GPL-2.0+
22 #include <asm/errno.h>
23 #include <linux/list.h>
26 #include <linux/usb/ch9.h>
27 #include <linux/usb/gadget.h>
29 #include <asm/byteorder.h>
30 #include <asm/unaligned.h>
33 #include <asm/mach-types.h>
36 #include <usb/lin_gadget_compat.h>
38 /***********************************************************/
40 #define OTG_DMA_MODE 1
45 #define DEBUG_OUT_EP 0
48 #include <usb/s3c_udc.h>
53 static char *state_names[] = {
56 "DATA_STATE_NEED_ZLP",
57 "WAIT_FOR_OUT_STATUS",
60 "WAIT_FOR_OUT_COMPLETE",
61 "WAIT_FOR_IN_COMPLETE",
62 "WAIT_FOR_NULL_COMPLETE",
65 #define DRIVER_DESC "S3C HS USB OTG Device Driver, (c) Samsung Electronics"
66 #define DRIVER_VERSION "15 March 2009"
68 struct s3c_udc *the_controller;
70 static const char driver_name[] = "s3c-udc";
71 static const char driver_desc[] = DRIVER_DESC;
72 static const char ep0name[] = "ep0-control";
75 static unsigned int ep0_fifo_size = 64;
76 static unsigned int ep_fifo_size = 512;
77 static unsigned int ep_fifo_size2 = 1024;
78 static int reset_available = 1;
80 static struct usb_ctrlrequest *usb_ctrl;
81 static dma_addr_t usb_ctrl_dma_addr;
86 static int s3c_ep_enable(struct usb_ep *ep,
87 const struct usb_endpoint_descriptor *);
88 static int s3c_ep_disable(struct usb_ep *ep);
89 static struct usb_request *s3c_alloc_request(struct usb_ep *ep,
91 static void s3c_free_request(struct usb_ep *ep, struct usb_request *);
93 static int s3c_queue(struct usb_ep *ep, struct usb_request *, gfp_t gfp_flags);
94 static int s3c_dequeue(struct usb_ep *ep, struct usb_request *);
95 static int s3c_fifo_status(struct usb_ep *ep);
96 static void s3c_fifo_flush(struct usb_ep *ep);
97 static void s3c_ep0_read(struct s3c_udc *dev);
98 static void s3c_ep0_kick(struct s3c_udc *dev, struct s3c_ep *ep);
99 static void s3c_handle_ep0(struct s3c_udc *dev);
100 static int s3c_ep0_write(struct s3c_udc *dev);
101 static int write_fifo_ep0(struct s3c_ep *ep, struct s3c_request *req);
102 static void done(struct s3c_ep *ep, struct s3c_request *req, int status);
103 static void stop_activity(struct s3c_udc *dev,
104 struct usb_gadget_driver *driver);
105 static int udc_enable(struct s3c_udc *dev);
106 static void udc_set_address(struct s3c_udc *dev, unsigned char address);
107 static void reconfig_usbd(void);
108 static void set_max_pktsize(struct s3c_udc *dev, enum usb_device_speed speed);
109 static void nuke(struct s3c_ep *ep, int status);
110 static int s3c_udc_set_halt(struct usb_ep *_ep, int value);
111 static void s3c_udc_set_nak(struct s3c_ep *ep);
113 void set_udc_gadget_private_data(void *p)
115 debug_cond(DEBUG_SETUP != 0,
116 "%s: the_controller: 0x%p, p: 0x%p\n", __func__,
118 the_controller->gadget.dev.device_data = p;
121 void *get_udc_gadget_private_data(struct usb_gadget *gadget)
123 return gadget->dev.device_data;
126 static struct usb_ep_ops s3c_ep_ops = {
127 .enable = s3c_ep_enable,
128 .disable = s3c_ep_disable,
130 .alloc_request = s3c_alloc_request,
131 .free_request = s3c_free_request,
134 .dequeue = s3c_dequeue,
136 .set_halt = s3c_udc_set_halt,
137 .fifo_status = s3c_fifo_status,
138 .fifo_flush = s3c_fifo_flush,
141 #define create_proc_files() do {} while (0)
142 #define remove_proc_files() do {} while (0)
144 /***********************************************************/
146 void __iomem *regs_otg;
147 struct s3c_usbotg_reg *reg;
149 bool dfu_usb_get_reset(void)
151 return !!(readl(®->gintsts) & INT_RESET);
154 __weak void otg_phy_init(struct s3c_udc *dev) {}
155 __weak void otg_phy_off(struct s3c_udc *dev) {}
157 /***********************************************************/
159 #include "s3c_udc_otg_xfer_dma.c"
162 * udc_disable - disable USB device controller
164 static void udc_disable(struct s3c_udc *dev)
166 debug_cond(DEBUG_SETUP != 0, "%s: %p\n", __func__, dev);
168 udc_set_address(dev, 0);
170 dev->ep0state = WAIT_FOR_SETUP;
171 dev->gadget.speed = USB_SPEED_UNKNOWN;
172 dev->usb_address = 0;
178 * udc_reinit - initialize software state
180 static void udc_reinit(struct s3c_udc *dev)
184 debug_cond(DEBUG_SETUP != 0, "%s: %p\n", __func__, dev);
186 /* device/ep0 records init */
187 INIT_LIST_HEAD(&dev->gadget.ep_list);
188 INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
189 dev->ep0state = WAIT_FOR_SETUP;
191 /* basic endpoint records init */
192 for (i = 0; i < S3C_MAX_ENDPOINTS; i++) {
193 struct s3c_ep *ep = &dev->ep[i];
196 list_add_tail(&ep->ep.ep_list, &dev->gadget.ep_list);
200 INIT_LIST_HEAD(&ep->queue);
204 /* the rest was statically initialized, and is read-only */
207 #define BYTES2MAXP(x) (x / 8)
208 #define MAXP2BYTES(x) (x * 8)
210 /* until it's enabled, this UDC should be completely invisible
213 static int udc_enable(struct s3c_udc *dev)
215 debug_cond(DEBUG_SETUP != 0, "%s: %p\n", __func__, dev);
220 debug_cond(DEBUG_SETUP != 0,
221 "S3C USB 2.0 OTG Controller Core Initialized : 0x%x\n",
222 readl(®->gintmsk));
224 dev->gadget.speed = USB_SPEED_UNKNOWN;
230 Register entry point for the peripheral controller driver.
232 int usb_gadget_register_driver(struct usb_gadget_driver *driver)
234 struct s3c_udc *dev = the_controller;
236 unsigned long flags = 0;
238 debug_cond(DEBUG_SETUP != 0, "%s: %s\n", __func__, "no name");
241 || (driver->speed != USB_SPEED_FULL
242 && driver->speed != USB_SPEED_HIGH)
243 || !driver->bind || !driver->disconnect || !driver->setup)
250 spin_lock_irqsave(&dev->lock, flags);
251 /* first hook up the driver ... */
252 dev->driver = driver;
253 spin_unlock_irqrestore(&dev->lock, flags);
255 if (retval) { /* TODO */
256 printf("target device_add failed, error %d\n", retval);
260 retval = driver->bind(&dev->gadget);
262 debug_cond(DEBUG_SETUP != 0,
263 "%s: bind to driver --> error %d\n",
264 dev->gadget.name, retval);
271 debug_cond(DEBUG_SETUP != 0,
272 "Registered gadget driver %s\n", dev->gadget.name);
279 * Unregister entry point for the peripheral controller driver.
281 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
283 struct s3c_udc *dev = the_controller;
284 unsigned long flags = 0;
288 if (!driver || driver != dev->driver)
291 spin_lock_irqsave(&dev->lock, flags);
293 stop_activity(dev, driver);
294 spin_unlock_irqrestore(&dev->lock, flags);
296 driver->unbind(&dev->gadget);
298 disable_irq(IRQ_OTG);
305 * done - retire a request; caller blocked irqs
307 static void done(struct s3c_ep *ep, struct s3c_request *req, int status)
309 unsigned int stopped = ep->stopped;
311 debug("%s: %s %p, req = %p, stopped = %d\n",
312 __func__, ep->ep.name, ep, &req->req, stopped);
314 list_del_init(&req->queue);
316 if (likely(req->req.status == -EINPROGRESS))
317 req->req.status = status;
319 status = req->req.status;
321 if (status && status != -ESHUTDOWN) {
322 debug("complete %s req %p stat %d len %u/%u\n",
323 ep->ep.name, &req->req, status,
324 req->req.actual, req->req.length);
327 /* don't modify queue heads during completion callback */
331 printf("calling complete callback\n");
333 int i, len = req->req.length;
335 printf("pkt[%d] = ", req->req.length);
338 for (i = 0; i < len; i++) {
339 printf("%02x", ((u8 *)req->req.buf)[i]);
346 spin_unlock(&ep->dev->lock);
347 req->req.complete(&ep->ep, &req->req);
348 spin_lock(&ep->dev->lock);
350 debug("callback completed\n");
352 ep->stopped = stopped;
356 * nuke - dequeue ALL requests
358 static void nuke(struct s3c_ep *ep, int status)
360 struct s3c_request *req;
362 debug("%s: %s %p\n", __func__, ep->ep.name, ep);
364 /* called with irqs blocked */
365 while (!list_empty(&ep->queue)) {
366 req = list_entry(ep->queue.next, struct s3c_request, queue);
367 done(ep, req, status);
371 static void stop_activity(struct s3c_udc *dev,
372 struct usb_gadget_driver *driver)
376 /* don't disconnect drivers more than once */
377 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
379 dev->gadget.speed = USB_SPEED_UNKNOWN;
381 /* prevent new request submissions, kill any outstanding requests */
382 for (i = 0; i < S3C_MAX_ENDPOINTS; i++) {
383 struct s3c_ep *ep = &dev->ep[i];
385 nuke(ep, -ESHUTDOWN);
388 /* report disconnect; the driver is already quiesced */
390 spin_unlock(&dev->lock);
391 driver->disconnect(&dev->gadget);
392 spin_lock(&dev->lock);
395 /* re-init driver-visible data structures */
399 static void reconfig_usbd(void)
401 /* 2. Soft-reset OTG Core and then unreset again. */
403 unsigned int uTemp = writel(CORE_SOFT_RESET, ®->grstctl);
405 debug("Reseting OTG controller\n");
407 writel(0<<15 /* PHY Low Power Clock sel*/
408 |1<<14 /* Non-Periodic TxFIFO Rewind Enable*/
409 |0x5<<10 /* Turnaround time*/
410 |0<<9 | 0<<8 /* [0:HNP disable,1:HNP enable][ 0:SRP disable*/
411 /* 1:SRP enable] H1= 1,1*/
412 |0<<7 /* Ulpi DDR sel*/
413 |0<<6 /* 0: high speed utmi+, 1: full speed serial*/
414 |0<<4 /* 0: utmi+, 1:ulpi*/
415 |1<<3 /* phy i/f 0:8bit, 1:16bit*/
416 |0x7<<0, /* HS/FS Timeout**/
419 /* 3. Put the OTG device core in the disconnected state.*/
420 uTemp = readl(®->dctl);
421 uTemp |= SOFT_DISCONNECT;
422 writel(uTemp, ®->dctl);
426 /* 4. Make the OTG device core exit from the disconnected state.*/
427 uTemp = readl(®->dctl);
428 uTemp = uTemp & ~SOFT_DISCONNECT;
429 writel(uTemp, ®->dctl);
431 /* 5. Configure OTG Core to initial settings of device mode.*/
432 /* [][1: full speed(30Mhz) 0:high speed]*/
433 writel(EP_MISS_CNT(1) | DEV_SPEED_HIGH_SPEED_20, ®->dcfg);
437 /* 6. Unmask the core interrupts*/
438 writel(GINTMSK_INIT, ®->gintmsk);
440 /* 7. Set NAK bit of EP0, EP1, EP2*/
441 writel(DEPCTL_EPDIS|DEPCTL_SNAK, ®->out_endp[EP0_CON].doepctl);
442 writel(DEPCTL_EPDIS|DEPCTL_SNAK, ®->in_endp[EP0_CON].diepctl);
444 for (i = 1; i < S3C_MAX_ENDPOINTS; i++) {
445 writel(DEPCTL_EPDIS|DEPCTL_SNAK, ®->out_endp[i].doepctl);
446 writel(DEPCTL_EPDIS|DEPCTL_SNAK, ®->in_endp[i].diepctl);
449 /* 8. Unmask EPO interrupts*/
450 writel(((1 << EP0_CON) << DAINT_OUT_BIT)
451 | (1 << EP0_CON), ®->daintmsk);
453 /* 9. Unmask device OUT EP common interrupts*/
454 writel(DOEPMSK_INIT, ®->doepmsk);
456 /* 10. Unmask device IN EP common interrupts*/
457 writel(DIEPMSK_INIT, ®->diepmsk);
459 /* 11. Set Rx FIFO Size (in 32-bit words) */
460 writel(RX_FIFO_SIZE >> 2, ®->grxfsiz);
462 /* 12. Set Non Periodic Tx FIFO Size */
463 writel((NPTX_FIFO_SIZE >> 2) << 16 | ((RX_FIFO_SIZE >> 2)) << 0,
466 for (i = 1; i < S3C_MAX_HW_ENDPOINTS; i++)
467 writel((PTX_FIFO_SIZE >> 2) << 16 |
468 ((RX_FIFO_SIZE + NPTX_FIFO_SIZE +
469 PTX_FIFO_SIZE*(i-1)) >> 2) << 0,
472 /* Flush the RX FIFO */
473 writel(RX_FIFO_FLUSH, ®->grstctl);
474 while (readl(®->grstctl) & RX_FIFO_FLUSH)
475 debug("%s: waiting for S3C_UDC_OTG_GRSTCTL\n", __func__);
477 /* Flush all the Tx FIFO's */
478 writel(TX_FIFO_FLUSH_ALL, ®->grstctl);
479 writel(TX_FIFO_FLUSH_ALL | TX_FIFO_FLUSH, ®->grstctl);
480 while (readl(®->grstctl) & TX_FIFO_FLUSH)
481 debug("%s: waiting for S3C_UDC_OTG_GRSTCTL\n", __func__);
483 /* 13. Clear NAK bit of EP0, EP1, EP2*/
485 /* EP0: Control OUT */
486 writel(DEPCTL_EPDIS | DEPCTL_CNAK,
487 ®->out_endp[EP0_CON].doepctl);
489 /* 14. Initialize OTG Link Core.*/
490 writel(GAHBCFG_INIT, ®->gahbcfg);
493 static void set_max_pktsize(struct s3c_udc *dev, enum usb_device_speed speed)
495 unsigned int ep_ctrl;
498 if (speed == USB_SPEED_HIGH) {
501 ep_fifo_size2 = 1024;
502 dev->gadget.speed = USB_SPEED_HIGH;
507 dev->gadget.speed = USB_SPEED_FULL;
510 dev->ep[0].ep.maxpacket = ep0_fifo_size;
511 for (i = 1; i < S3C_MAX_ENDPOINTS; i++)
512 dev->ep[i].ep.maxpacket = ep_fifo_size;
514 /* EP0 - Control IN (64 bytes)*/
515 ep_ctrl = readl(®->in_endp[EP0_CON].diepctl);
516 writel(ep_ctrl|(0<<0), ®->in_endp[EP0_CON].diepctl);
518 /* EP0 - Control OUT (64 bytes)*/
519 ep_ctrl = readl(®->out_endp[EP0_CON].doepctl);
520 writel(ep_ctrl|(0<<0), ®->out_endp[EP0_CON].doepctl);
523 static int s3c_ep_enable(struct usb_ep *_ep,
524 const struct usb_endpoint_descriptor *desc)
528 unsigned long flags = 0;
530 debug("%s: %p\n", __func__, _ep);
532 ep = container_of(_ep, struct s3c_ep, ep);
533 if (!_ep || !desc || ep->desc || _ep->name == ep0name
534 || desc->bDescriptorType != USB_DT_ENDPOINT
535 || ep->bEndpointAddress != desc->bEndpointAddress
536 || ep_maxpacket(ep) <
537 le16_to_cpu(get_unaligned(&desc->wMaxPacketSize))) {
539 debug("%s: bad ep or descriptor\n", __func__);
543 /* xfer types must match, except that interrupt ~= bulk */
544 if (ep->bmAttributes != desc->bmAttributes
545 && ep->bmAttributes != USB_ENDPOINT_XFER_BULK
546 && desc->bmAttributes != USB_ENDPOINT_XFER_INT) {
548 debug("%s: %s type mismatch\n", __func__, _ep->name);
552 /* hardware _could_ do smaller, but driver doesn't */
553 if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
554 && le16_to_cpu(get_unaligned(&desc->wMaxPacketSize)) !=
555 ep_maxpacket(ep)) || !get_unaligned(&desc->wMaxPacketSize)) {
557 debug("%s: bad %s maxpacket\n", __func__, _ep->name);
562 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
564 debug("%s: bogus device state\n", __func__);
571 ep->ep.maxpacket = le16_to_cpu(get_unaligned(&desc->wMaxPacketSize));
573 /* Reset halt state */
575 s3c_udc_set_halt(_ep, 0);
577 spin_lock_irqsave(&ep->dev->lock, flags);
578 s3c_udc_ep_activate(ep);
579 spin_unlock_irqrestore(&ep->dev->lock, flags);
581 debug("%s: enabled %s, stopped = %d, maxpacket = %d\n",
582 __func__, _ep->name, ep->stopped, ep->ep.maxpacket);
589 static int s3c_ep_disable(struct usb_ep *_ep)
592 unsigned long flags = 0;
594 debug("%s: %p\n", __func__, _ep);
596 ep = container_of(_ep, struct s3c_ep, ep);
597 if (!_ep || !ep->desc) {
598 debug("%s: %s not enabled\n", __func__,
599 _ep ? ep->ep.name : NULL);
603 spin_lock_irqsave(&ep->dev->lock, flags);
605 /* Nuke all pending requests */
606 nuke(ep, -ESHUTDOWN);
611 spin_unlock_irqrestore(&ep->dev->lock, flags);
613 debug("%s: disabled %s\n", __func__, _ep->name);
617 static struct usb_request *s3c_alloc_request(struct usb_ep *ep,
620 struct s3c_request *req;
622 debug("%s: %s %p\n", __func__, ep->name, ep);
624 req = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*req));
628 memset(req, 0, sizeof *req);
629 INIT_LIST_HEAD(&req->queue);
634 static void s3c_free_request(struct usb_ep *ep, struct usb_request *_req)
636 struct s3c_request *req;
638 debug("%s: %p\n", __func__, ep);
640 req = container_of(_req, struct s3c_request, req);
641 WARN_ON(!list_empty(&req->queue));
645 /* dequeue JUST ONE request */
646 static int s3c_dequeue(struct usb_ep *_ep, struct usb_request *_req)
649 struct s3c_request *req;
650 unsigned long flags = 0;
652 debug("%s: %p\n", __func__, _ep);
654 ep = container_of(_ep, struct s3c_ep, ep);
655 if (!_ep || ep->ep.name == ep0name)
658 spin_lock_irqsave(&ep->dev->lock, flags);
660 /* make sure it's actually queued on this endpoint */
661 list_for_each_entry(req, &ep->queue, queue) {
662 if (&req->req == _req)
665 if (&req->req != _req) {
666 spin_unlock_irqrestore(&ep->dev->lock, flags);
670 done(ep, req, -ECONNRESET);
672 spin_unlock_irqrestore(&ep->dev->lock, flags);
677 * Return bytes in EP FIFO
679 static int s3c_fifo_status(struct usb_ep *_ep)
684 ep = container_of(_ep, struct s3c_ep, ep);
686 debug("%s: bad ep\n", __func__);
690 debug("%s: %d\n", __func__, ep_index(ep));
692 /* LPD can't report unclaimed bytes from IN fifos */
702 static void s3c_fifo_flush(struct usb_ep *_ep)
706 ep = container_of(_ep, struct s3c_ep, ep);
707 if (unlikely(!_ep || (!ep->desc && ep->ep.name != ep0name))) {
708 debug("%s: bad ep\n", __func__);
712 debug("%s: %d\n", __func__, ep_index(ep));
715 static const struct usb_gadget_ops s3c_udc_ops = {
716 /* current versions must always be self-powered */
719 static struct s3c_udc memory = {
723 .ep0 = &memory.ep[0].ep,
727 /* control endpoint */
732 .maxpacket = EP0_FIFO_SIZE,
736 .bEndpointAddress = 0,
739 .ep_type = ep_control,
742 /* first group of endpoints */
745 .name = "ep1in-bulk",
747 .maxpacket = EP_FIFO_SIZE,
751 .bEndpointAddress = USB_DIR_IN | 1,
752 .bmAttributes = USB_ENDPOINT_XFER_BULK,
754 .ep_type = ep_bulk_out,
760 .name = "ep2out-bulk",
762 .maxpacket = EP_FIFO_SIZE,
766 .bEndpointAddress = USB_DIR_OUT | 2,
767 .bmAttributes = USB_ENDPOINT_XFER_BULK,
769 .ep_type = ep_bulk_in,
777 .maxpacket = EP_FIFO_SIZE,
781 .bEndpointAddress = USB_DIR_IN | 3,
782 .bmAttributes = USB_ENDPOINT_XFER_INT,
784 .ep_type = ep_interrupt,
790 * probe - binds to the platform device
793 int s3c_udc_probe(struct s3c_plat_otg_data *pdata)
795 struct s3c_udc *dev = &memory;
798 debug("%s: %p\n", __func__, pdata);
802 reg = (struct s3c_usbotg_reg *)pdata->regs_otg;
804 /* regs_otg = (void *)pdata->regs_otg; */
806 dev->gadget.is_dualspeed = 1; /* Hack only*/
807 dev->gadget.is_otg = 0;
808 dev->gadget.is_a_peripheral = 0;
809 dev->gadget.b_hnp_enable = 0;
810 dev->gadget.a_hnp_support = 0;
811 dev->gadget.a_alt_hnp_support = 0;
813 the_controller = dev;
815 usb_ctrl = memalign(CONFIG_SYS_CACHELINE_SIZE,
816 ROUND(sizeof(struct usb_ctrlrequest),
817 CONFIG_SYS_CACHELINE_SIZE));
819 error("No memory available for UDC!\n");
823 usb_ctrl_dma_addr = (dma_addr_t) usb_ctrl;
830 int usb_gadget_handle_interrupts()
832 u32 intr_status = readl(®->gintsts);
833 u32 gintmsk = readl(®->gintmsk);
835 if (intr_status & gintmsk)
836 return s3c_udc_irq(1, (void *)the_controller);