Merge branch 'next'
[u-boot/qq2440-u-boot.git] / drivers / usb / gadget / s3c_udc_otg.c
blob63d4487a9bb15846d9cb7f8af4f9c0a0947dfa18
1 /*
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
8 * available at:
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>
14 * Ported to u-boot:
15 * Marek Szyprowski <m.szyprowski@samsung.com>
16 * Lukasz Majewski <l.majewski@samsumg.com>
18 * SPDX-License-Identifier: GPL-2.0+
20 #undef DEBUG
21 #include <common.h>
22 #include <asm/errno.h>
23 #include <linux/list.h>
24 #include <malloc.h>
26 #include <linux/usb/ch9.h>
27 #include <linux/usb/gadget.h>
29 #include <asm/byteorder.h>
30 #include <asm/unaligned.h>
31 #include <asm/io.h>
33 #include <asm/mach-types.h>
34 #include <asm/arch/gpio.h>
36 #include "regs-otg.h"
37 #include <usb/lin_gadget_compat.h>
39 /***********************************************************/
41 #define OTG_DMA_MODE 1
43 #define DEBUG_SETUP 0
44 #define DEBUG_EP0 0
45 #define DEBUG_ISR 0
46 #define DEBUG_OUT_EP 0
47 #define DEBUG_IN_EP 0
49 #include <usb/s3c_udc.h>
51 #define EP0_CON 0
52 #define EP_MASK 0xF
54 static char *state_names[] = {
55 "WAIT_FOR_SETUP",
56 "DATA_STATE_XMIT",
57 "DATA_STATE_NEED_ZLP",
58 "WAIT_FOR_OUT_STATUS",
59 "DATA_STATE_RECV",
60 "WAIT_FOR_COMPLETE",
61 "WAIT_FOR_OUT_COMPLETE",
62 "WAIT_FOR_IN_COMPLETE",
63 "WAIT_FOR_NULL_COMPLETE",
66 #define DRIVER_DESC "S3C HS USB OTG Device Driver, (c) Samsung Electronics"
67 #define DRIVER_VERSION "15 March 2009"
69 struct s3c_udc *the_controller;
71 static const char driver_name[] = "s3c-udc";
72 static const char driver_desc[] = DRIVER_DESC;
73 static const char ep0name[] = "ep0-control";
75 /* Max packet size*/
76 static unsigned int ep0_fifo_size = 64;
77 static unsigned int ep_fifo_size = 512;
78 static unsigned int ep_fifo_size2 = 1024;
79 static int reset_available = 1;
81 static struct usb_ctrlrequest *usb_ctrl;
82 static dma_addr_t usb_ctrl_dma_addr;
85 Local declarations.
87 static int s3c_ep_enable(struct usb_ep *ep,
88 const struct usb_endpoint_descriptor *);
89 static int s3c_ep_disable(struct usb_ep *ep);
90 static struct usb_request *s3c_alloc_request(struct usb_ep *ep,
91 gfp_t gfp_flags);
92 static void s3c_free_request(struct usb_ep *ep, struct usb_request *);
94 static int s3c_queue(struct usb_ep *ep, struct usb_request *, gfp_t gfp_flags);
95 static int s3c_dequeue(struct usb_ep *ep, struct usb_request *);
96 static int s3c_fifo_status(struct usb_ep *ep);
97 static void s3c_fifo_flush(struct usb_ep *ep);
98 static void s3c_ep0_read(struct s3c_udc *dev);
99 static void s3c_ep0_kick(struct s3c_udc *dev, struct s3c_ep *ep);
100 static void s3c_handle_ep0(struct s3c_udc *dev);
101 static int s3c_ep0_write(struct s3c_udc *dev);
102 static int write_fifo_ep0(struct s3c_ep *ep, struct s3c_request *req);
103 static void done(struct s3c_ep *ep, struct s3c_request *req, int status);
104 static void stop_activity(struct s3c_udc *dev,
105 struct usb_gadget_driver *driver);
106 static int udc_enable(struct s3c_udc *dev);
107 static void udc_set_address(struct s3c_udc *dev, unsigned char address);
108 static void reconfig_usbd(void);
109 static void set_max_pktsize(struct s3c_udc *dev, enum usb_device_speed speed);
110 static void nuke(struct s3c_ep *ep, int status);
111 static int s3c_udc_set_halt(struct usb_ep *_ep, int value);
112 static void s3c_udc_set_nak(struct s3c_ep *ep);
114 void set_udc_gadget_private_data(void *p)
116 debug_cond(DEBUG_SETUP != 0,
117 "%s: the_controller: 0x%p, p: 0x%p\n", __func__,
118 the_controller, p);
119 the_controller->gadget.dev.device_data = p;
122 void *get_udc_gadget_private_data(struct usb_gadget *gadget)
124 return gadget->dev.device_data;
127 static struct usb_ep_ops s3c_ep_ops = {
128 .enable = s3c_ep_enable,
129 .disable = s3c_ep_disable,
131 .alloc_request = s3c_alloc_request,
132 .free_request = s3c_free_request,
134 .queue = s3c_queue,
135 .dequeue = s3c_dequeue,
137 .set_halt = s3c_udc_set_halt,
138 .fifo_status = s3c_fifo_status,
139 .fifo_flush = s3c_fifo_flush,
142 #define create_proc_files() do {} while (0)
143 #define remove_proc_files() do {} while (0)
145 /***********************************************************/
147 void __iomem *regs_otg;
148 struct s3c_usbotg_reg *reg;
149 struct s3c_usbotg_phy *phy;
150 static unsigned int usb_phy_ctrl;
152 void otg_phy_init(struct s3c_udc *dev)
154 dev->pdata->phy_control(1);
156 /*USB PHY0 Enable */
157 printf("USB PHY0 Enable\n");
159 /* Enable PHY */
160 writel(readl(usb_phy_ctrl) | USB_PHY_CTRL_EN0, usb_phy_ctrl);
162 if (dev->pdata->usb_flags == PHY0_SLEEP) /* C210 Universal */
163 writel((readl(&phy->phypwr)
164 &~(PHY_0_SLEEP | OTG_DISABLE_0 | ANALOG_PWRDOWN)
165 &~FORCE_SUSPEND_0), &phy->phypwr);
166 else /* C110 GONI */
167 writel((readl(&phy->phypwr) &~(OTG_DISABLE_0 | ANALOG_PWRDOWN)
168 &~FORCE_SUSPEND_0), &phy->phypwr);
170 if (s5p_cpu_id == 0x4412)
171 writel((readl(&phy->phyclk) & ~(EXYNOS4X12_ID_PULLUP0 |
172 EXYNOS4X12_COMMON_ON_N0)) | EXYNOS4X12_CLK_SEL_24MHZ,
173 &phy->phyclk); /* PLL 24Mhz */
174 else
175 writel((readl(&phy->phyclk) & ~(ID_PULLUP0 | COMMON_ON_N0)) |
176 CLK_SEL_24MHZ, &phy->phyclk); /* PLL 24Mhz */
178 writel((readl(&phy->rstcon) &~(LINK_SW_RST | PHYLNK_SW_RST))
179 | PHY_SW_RST0, &phy->rstcon);
180 udelay(10);
181 writel(readl(&phy->rstcon)
182 &~(PHY_SW_RST0 | LINK_SW_RST | PHYLNK_SW_RST), &phy->rstcon);
183 udelay(10);
186 void otg_phy_off(struct s3c_udc *dev)
188 /* reset controller just in case */
189 writel(PHY_SW_RST0, &phy->rstcon);
190 udelay(20);
191 writel(readl(&phy->phypwr) &~PHY_SW_RST0, &phy->rstcon);
192 udelay(20);
194 writel(readl(&phy->phypwr) | OTG_DISABLE_0 | ANALOG_PWRDOWN
195 | FORCE_SUSPEND_0, &phy->phypwr);
197 writel(readl(usb_phy_ctrl) &~USB_PHY_CTRL_EN0, usb_phy_ctrl);
199 writel((readl(&phy->phyclk) & ~(ID_PULLUP0 | COMMON_ON_N0)),
200 &phy->phyclk);
202 udelay(10000);
204 dev->pdata->phy_control(0);
207 /***********************************************************/
209 #include "s3c_udc_otg_xfer_dma.c"
212 * udc_disable - disable USB device controller
214 static void udc_disable(struct s3c_udc *dev)
216 debug_cond(DEBUG_SETUP != 0, "%s: %p\n", __func__, dev);
218 udc_set_address(dev, 0);
220 dev->ep0state = WAIT_FOR_SETUP;
221 dev->gadget.speed = USB_SPEED_UNKNOWN;
222 dev->usb_address = 0;
224 otg_phy_off(dev);
228 * udc_reinit - initialize software state
230 static void udc_reinit(struct s3c_udc *dev)
232 unsigned int i;
234 debug_cond(DEBUG_SETUP != 0, "%s: %p\n", __func__, dev);
236 /* device/ep0 records init */
237 INIT_LIST_HEAD(&dev->gadget.ep_list);
238 INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
239 dev->ep0state = WAIT_FOR_SETUP;
241 /* basic endpoint records init */
242 for (i = 0; i < S3C_MAX_ENDPOINTS; i++) {
243 struct s3c_ep *ep = &dev->ep[i];
245 if (i != 0)
246 list_add_tail(&ep->ep.ep_list, &dev->gadget.ep_list);
248 ep->desc = 0;
249 ep->stopped = 0;
250 INIT_LIST_HEAD(&ep->queue);
251 ep->pio_irqs = 0;
254 /* the rest was statically initialized, and is read-only */
257 #define BYTES2MAXP(x) (x / 8)
258 #define MAXP2BYTES(x) (x * 8)
260 /* until it's enabled, this UDC should be completely invisible
261 * to any USB host.
263 static int udc_enable(struct s3c_udc *dev)
265 debug_cond(DEBUG_SETUP != 0, "%s: %p\n", __func__, dev);
267 otg_phy_init(dev);
268 reconfig_usbd();
270 debug_cond(DEBUG_SETUP != 0,
271 "S3C USB 2.0 OTG Controller Core Initialized : 0x%x\n",
272 readl(&reg->gintmsk));
274 dev->gadget.speed = USB_SPEED_UNKNOWN;
276 return 0;
280 Register entry point for the peripheral controller driver.
282 int usb_gadget_register_driver(struct usb_gadget_driver *driver)
284 struct s3c_udc *dev = the_controller;
285 int retval = 0;
286 unsigned long flags;
288 debug_cond(DEBUG_SETUP != 0, "%s: %s\n", __func__, "no name");
290 if (!driver
291 || (driver->speed != USB_SPEED_FULL
292 && driver->speed != USB_SPEED_HIGH)
293 || !driver->bind || !driver->disconnect || !driver->setup)
294 return -EINVAL;
295 if (!dev)
296 return -ENODEV;
297 if (dev->driver)
298 return -EBUSY;
300 spin_lock_irqsave(&dev->lock, flags);
301 /* first hook up the driver ... */
302 dev->driver = driver;
303 spin_unlock_irqrestore(&dev->lock, flags);
305 if (retval) { /* TODO */
306 printf("target device_add failed, error %d\n", retval);
307 return retval;
310 retval = driver->bind(&dev->gadget);
311 if (retval) {
312 debug_cond(DEBUG_SETUP != 0,
313 "%s: bind to driver --> error %d\n",
314 dev->gadget.name, retval);
315 dev->driver = 0;
316 return retval;
319 enable_irq(IRQ_OTG);
321 debug_cond(DEBUG_SETUP != 0,
322 "Registered gadget driver %s\n", dev->gadget.name);
323 udc_enable(dev);
325 return 0;
329 * Unregister entry point for the peripheral controller driver.
331 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
333 struct s3c_udc *dev = the_controller;
334 unsigned long flags;
336 if (!dev)
337 return -ENODEV;
338 if (!driver || driver != dev->driver)
339 return -EINVAL;
341 spin_lock_irqsave(&dev->lock, flags);
342 dev->driver = 0;
343 stop_activity(dev, driver);
344 spin_unlock_irqrestore(&dev->lock, flags);
346 driver->unbind(&dev->gadget);
348 disable_irq(IRQ_OTG);
350 udc_disable(dev);
351 return 0;
355 * done - retire a request; caller blocked irqs
357 static void done(struct s3c_ep *ep, struct s3c_request *req, int status)
359 unsigned int stopped = ep->stopped;
361 debug("%s: %s %p, req = %p, stopped = %d\n",
362 __func__, ep->ep.name, ep, &req->req, stopped);
364 list_del_init(&req->queue);
366 if (likely(req->req.status == -EINPROGRESS))
367 req->req.status = status;
368 else
369 status = req->req.status;
371 if (status && status != -ESHUTDOWN) {
372 debug("complete %s req %p stat %d len %u/%u\n",
373 ep->ep.name, &req->req, status,
374 req->req.actual, req->req.length);
377 /* don't modify queue heads during completion callback */
378 ep->stopped = 1;
380 #ifdef DEBUG
381 printf("calling complete callback\n");
383 int i, len = req->req.length;
385 printf("pkt[%d] = ", req->req.length);
386 if (len > 64)
387 len = 64;
388 for (i = 0; i < len; i++) {
389 printf("%02x", ((u8 *)req->req.buf)[i]);
390 if ((i & 7) == 7)
391 printf(" ");
393 printf("\n");
395 #endif
396 spin_unlock(&ep->dev->lock);
397 req->req.complete(&ep->ep, &req->req);
398 spin_lock(&ep->dev->lock);
400 debug("callback completed\n");
402 ep->stopped = stopped;
406 * nuke - dequeue ALL requests
408 static void nuke(struct s3c_ep *ep, int status)
410 struct s3c_request *req;
412 debug("%s: %s %p\n", __func__, ep->ep.name, ep);
414 /* called with irqs blocked */
415 while (!list_empty(&ep->queue)) {
416 req = list_entry(ep->queue.next, struct s3c_request, queue);
417 done(ep, req, status);
421 static void stop_activity(struct s3c_udc *dev,
422 struct usb_gadget_driver *driver)
424 int i;
426 /* don't disconnect drivers more than once */
427 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
428 driver = 0;
429 dev->gadget.speed = USB_SPEED_UNKNOWN;
431 /* prevent new request submissions, kill any outstanding requests */
432 for (i = 0; i < S3C_MAX_ENDPOINTS; i++) {
433 struct s3c_ep *ep = &dev->ep[i];
434 ep->stopped = 1;
435 nuke(ep, -ESHUTDOWN);
438 /* report disconnect; the driver is already quiesced */
439 if (driver) {
440 spin_unlock(&dev->lock);
441 driver->disconnect(&dev->gadget);
442 spin_lock(&dev->lock);
445 /* re-init driver-visible data structures */
446 udc_reinit(dev);
449 static void reconfig_usbd(void)
451 /* 2. Soft-reset OTG Core and then unreset again. */
452 int i;
453 unsigned int uTemp = writel(CORE_SOFT_RESET, &reg->grstctl);
455 debug("Reseting OTG controller\n");
457 writel(0<<15 /* PHY Low Power Clock sel*/
458 |1<<14 /* Non-Periodic TxFIFO Rewind Enable*/
459 |0x5<<10 /* Turnaround time*/
460 |0<<9 | 0<<8 /* [0:HNP disable,1:HNP enable][ 0:SRP disable*/
461 /* 1:SRP enable] H1= 1,1*/
462 |0<<7 /* Ulpi DDR sel*/
463 |0<<6 /* 0: high speed utmi+, 1: full speed serial*/
464 |0<<4 /* 0: utmi+, 1:ulpi*/
465 |1<<3 /* phy i/f 0:8bit, 1:16bit*/
466 |0x7<<0, /* HS/FS Timeout**/
467 &reg->gusbcfg);
469 /* 3. Put the OTG device core in the disconnected state.*/
470 uTemp = readl(&reg->dctl);
471 uTemp |= SOFT_DISCONNECT;
472 writel(uTemp, &reg->dctl);
474 udelay(20);
476 /* 4. Make the OTG device core exit from the disconnected state.*/
477 uTemp = readl(&reg->dctl);
478 uTemp = uTemp & ~SOFT_DISCONNECT;
479 writel(uTemp, &reg->dctl);
481 /* 5. Configure OTG Core to initial settings of device mode.*/
482 /* [][1: full speed(30Mhz) 0:high speed]*/
483 writel(EP_MISS_CNT(1) | DEV_SPEED_HIGH_SPEED_20, &reg->dcfg);
485 mdelay(1);
487 /* 6. Unmask the core interrupts*/
488 writel(GINTMSK_INIT, &reg->gintmsk);
490 /* 7. Set NAK bit of EP0, EP1, EP2*/
491 writel(DEPCTL_EPDIS|DEPCTL_SNAK, &reg->out_endp[EP0_CON].doepctl);
492 writel(DEPCTL_EPDIS|DEPCTL_SNAK, &reg->in_endp[EP0_CON].diepctl);
494 for (i = 1; i < S3C_MAX_ENDPOINTS; i++) {
495 writel(DEPCTL_EPDIS|DEPCTL_SNAK, &reg->out_endp[i].doepctl);
496 writel(DEPCTL_EPDIS|DEPCTL_SNAK, &reg->in_endp[i].diepctl);
499 /* 8. Unmask EPO interrupts*/
500 writel(((1 << EP0_CON) << DAINT_OUT_BIT)
501 | (1 << EP0_CON), &reg->daintmsk);
503 /* 9. Unmask device OUT EP common interrupts*/
504 writel(DOEPMSK_INIT, &reg->doepmsk);
506 /* 10. Unmask device IN EP common interrupts*/
507 writel(DIEPMSK_INIT, &reg->diepmsk);
509 /* 11. Set Rx FIFO Size (in 32-bit words) */
510 writel(RX_FIFO_SIZE >> 2, &reg->grxfsiz);
512 /* 12. Set Non Periodic Tx FIFO Size */
513 writel((NPTX_FIFO_SIZE >> 2) << 16 | ((RX_FIFO_SIZE >> 2)) << 0,
514 &reg->gnptxfsiz);
516 for (i = 1; i < S3C_MAX_HW_ENDPOINTS; i++)
517 writel((PTX_FIFO_SIZE >> 2) << 16 |
518 ((RX_FIFO_SIZE + NPTX_FIFO_SIZE +
519 PTX_FIFO_SIZE*(i-1)) >> 2) << 0,
520 &reg->dieptxf[i-1]);
522 /* Flush the RX FIFO */
523 writel(RX_FIFO_FLUSH, &reg->grstctl);
524 while (readl(&reg->grstctl) & RX_FIFO_FLUSH)
525 debug("%s: waiting for S3C_UDC_OTG_GRSTCTL\n", __func__);
527 /* Flush all the Tx FIFO's */
528 writel(TX_FIFO_FLUSH_ALL, &reg->grstctl);
529 writel(TX_FIFO_FLUSH_ALL | TX_FIFO_FLUSH, &reg->grstctl);
530 while (readl(&reg->grstctl) & TX_FIFO_FLUSH)
531 debug("%s: waiting for S3C_UDC_OTG_GRSTCTL\n", __func__);
533 /* 13. Clear NAK bit of EP0, EP1, EP2*/
534 /* For Slave mode*/
535 /* EP0: Control OUT */
536 writel(DEPCTL_EPDIS | DEPCTL_CNAK,
537 &reg->out_endp[EP0_CON].doepctl);
539 /* 14. Initialize OTG Link Core.*/
540 writel(GAHBCFG_INIT, &reg->gahbcfg);
543 static void set_max_pktsize(struct s3c_udc *dev, enum usb_device_speed speed)
545 unsigned int ep_ctrl;
546 int i;
548 if (speed == USB_SPEED_HIGH) {
549 ep0_fifo_size = 64;
550 ep_fifo_size = 512;
551 ep_fifo_size2 = 1024;
552 dev->gadget.speed = USB_SPEED_HIGH;
553 } else {
554 ep0_fifo_size = 64;
555 ep_fifo_size = 64;
556 ep_fifo_size2 = 64;
557 dev->gadget.speed = USB_SPEED_FULL;
560 dev->ep[0].ep.maxpacket = ep0_fifo_size;
561 for (i = 1; i < S3C_MAX_ENDPOINTS; i++)
562 dev->ep[i].ep.maxpacket = ep_fifo_size;
564 /* EP0 - Control IN (64 bytes)*/
565 ep_ctrl = readl(&reg->in_endp[EP0_CON].diepctl);
566 writel(ep_ctrl|(0<<0), &reg->in_endp[EP0_CON].diepctl);
568 /* EP0 - Control OUT (64 bytes)*/
569 ep_ctrl = readl(&reg->out_endp[EP0_CON].doepctl);
570 writel(ep_ctrl|(0<<0), &reg->out_endp[EP0_CON].doepctl);
573 static int s3c_ep_enable(struct usb_ep *_ep,
574 const struct usb_endpoint_descriptor *desc)
576 struct s3c_ep *ep;
577 struct s3c_udc *dev;
578 unsigned long flags;
580 debug("%s: %p\n", __func__, _ep);
582 ep = container_of(_ep, struct s3c_ep, ep);
583 if (!_ep || !desc || ep->desc || _ep->name == ep0name
584 || desc->bDescriptorType != USB_DT_ENDPOINT
585 || ep->bEndpointAddress != desc->bEndpointAddress
586 || ep_maxpacket(ep) <
587 le16_to_cpu(get_unaligned(&desc->wMaxPacketSize))) {
589 debug("%s: bad ep or descriptor\n", __func__);
590 return -EINVAL;
593 /* xfer types must match, except that interrupt ~= bulk */
594 if (ep->bmAttributes != desc->bmAttributes
595 && ep->bmAttributes != USB_ENDPOINT_XFER_BULK
596 && desc->bmAttributes != USB_ENDPOINT_XFER_INT) {
598 debug("%s: %s type mismatch\n", __func__, _ep->name);
599 return -EINVAL;
602 /* hardware _could_ do smaller, but driver doesn't */
603 if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
604 && le16_to_cpu(get_unaligned(&desc->wMaxPacketSize)) !=
605 ep_maxpacket(ep)) || !get_unaligned(&desc->wMaxPacketSize)) {
607 debug("%s: bad %s maxpacket\n", __func__, _ep->name);
608 return -ERANGE;
611 dev = ep->dev;
612 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
614 debug("%s: bogus device state\n", __func__);
615 return -ESHUTDOWN;
618 ep->stopped = 0;
619 ep->desc = desc;
620 ep->pio_irqs = 0;
621 ep->ep.maxpacket = le16_to_cpu(get_unaligned(&desc->wMaxPacketSize));
623 /* Reset halt state */
624 s3c_udc_set_nak(ep);
625 s3c_udc_set_halt(_ep, 0);
627 spin_lock_irqsave(&ep->dev->lock, flags);
628 s3c_udc_ep_activate(ep);
629 spin_unlock_irqrestore(&ep->dev->lock, flags);
631 debug("%s: enabled %s, stopped = %d, maxpacket = %d\n",
632 __func__, _ep->name, ep->stopped, ep->ep.maxpacket);
633 return 0;
637 * Disable EP
639 static int s3c_ep_disable(struct usb_ep *_ep)
641 struct s3c_ep *ep;
642 unsigned long flags;
644 debug("%s: %p\n", __func__, _ep);
646 ep = container_of(_ep, struct s3c_ep, ep);
647 if (!_ep || !ep->desc) {
648 debug("%s: %s not enabled\n", __func__,
649 _ep ? ep->ep.name : NULL);
650 return -EINVAL;
653 spin_lock_irqsave(&ep->dev->lock, flags);
655 /* Nuke all pending requests */
656 nuke(ep, -ESHUTDOWN);
658 ep->desc = 0;
659 ep->stopped = 1;
661 spin_unlock_irqrestore(&ep->dev->lock, flags);
663 debug("%s: disabled %s\n", __func__, _ep->name);
664 return 0;
667 static struct usb_request *s3c_alloc_request(struct usb_ep *ep,
668 gfp_t gfp_flags)
670 struct s3c_request *req;
672 debug("%s: %s %p\n", __func__, ep->name, ep);
674 req = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*req));
675 if (!req)
676 return 0;
678 memset(req, 0, sizeof *req);
679 INIT_LIST_HEAD(&req->queue);
681 return &req->req;
684 static void s3c_free_request(struct usb_ep *ep, struct usb_request *_req)
686 struct s3c_request *req;
688 debug("%s: %p\n", __func__, ep);
690 req = container_of(_req, struct s3c_request, req);
691 WARN_ON(!list_empty(&req->queue));
692 kfree(req);
695 /* dequeue JUST ONE request */
696 static int s3c_dequeue(struct usb_ep *_ep, struct usb_request *_req)
698 struct s3c_ep *ep;
699 struct s3c_request *req;
700 unsigned long flags;
702 debug("%s: %p\n", __func__, _ep);
704 ep = container_of(_ep, struct s3c_ep, ep);
705 if (!_ep || ep->ep.name == ep0name)
706 return -EINVAL;
708 spin_lock_irqsave(&ep->dev->lock, flags);
710 /* make sure it's actually queued on this endpoint */
711 list_for_each_entry(req, &ep->queue, queue) {
712 if (&req->req == _req)
713 break;
715 if (&req->req != _req) {
716 spin_unlock_irqrestore(&ep->dev->lock, flags);
717 return -EINVAL;
720 done(ep, req, -ECONNRESET);
722 spin_unlock_irqrestore(&ep->dev->lock, flags);
723 return 0;
727 * Return bytes in EP FIFO
729 static int s3c_fifo_status(struct usb_ep *_ep)
731 int count = 0;
732 struct s3c_ep *ep;
734 ep = container_of(_ep, struct s3c_ep, ep);
735 if (!_ep) {
736 debug("%s: bad ep\n", __func__);
737 return -ENODEV;
740 debug("%s: %d\n", __func__, ep_index(ep));
742 /* LPD can't report unclaimed bytes from IN fifos */
743 if (ep_is_in(ep))
744 return -EOPNOTSUPP;
746 return count;
750 * Flush EP FIFO
752 static void s3c_fifo_flush(struct usb_ep *_ep)
754 struct s3c_ep *ep;
756 ep = container_of(_ep, struct s3c_ep, ep);
757 if (unlikely(!_ep || (!ep->desc && ep->ep.name != ep0name))) {
758 debug("%s: bad ep\n", __func__);
759 return;
762 debug("%s: %d\n", __func__, ep_index(ep));
765 static const struct usb_gadget_ops s3c_udc_ops = {
766 /* current versions must always be self-powered */
769 static struct s3c_udc memory = {
770 .usb_address = 0,
771 .gadget = {
772 .ops = &s3c_udc_ops,
773 .ep0 = &memory.ep[0].ep,
774 .name = driver_name,
777 /* control endpoint */
778 .ep[0] = {
779 .ep = {
780 .name = ep0name,
781 .ops = &s3c_ep_ops,
782 .maxpacket = EP0_FIFO_SIZE,
784 .dev = &memory,
786 .bEndpointAddress = 0,
787 .bmAttributes = 0,
789 .ep_type = ep_control,
792 /* first group of endpoints */
793 .ep[1] = {
794 .ep = {
795 .name = "ep1in-bulk",
796 .ops = &s3c_ep_ops,
797 .maxpacket = EP_FIFO_SIZE,
799 .dev = &memory,
801 .bEndpointAddress = USB_DIR_IN | 1,
802 .bmAttributes = USB_ENDPOINT_XFER_BULK,
804 .ep_type = ep_bulk_out,
805 .fifo_num = 1,
808 .ep[2] = {
809 .ep = {
810 .name = "ep2out-bulk",
811 .ops = &s3c_ep_ops,
812 .maxpacket = EP_FIFO_SIZE,
814 .dev = &memory,
816 .bEndpointAddress = USB_DIR_OUT | 2,
817 .bmAttributes = USB_ENDPOINT_XFER_BULK,
819 .ep_type = ep_bulk_in,
820 .fifo_num = 2,
823 .ep[3] = {
824 .ep = {
825 .name = "ep3in-int",
826 .ops = &s3c_ep_ops,
827 .maxpacket = EP_FIFO_SIZE,
829 .dev = &memory,
831 .bEndpointAddress = USB_DIR_IN | 3,
832 .bmAttributes = USB_ENDPOINT_XFER_INT,
834 .ep_type = ep_interrupt,
835 .fifo_num = 3,
840 * probe - binds to the platform device
843 int s3c_udc_probe(struct s3c_plat_otg_data *pdata)
845 struct s3c_udc *dev = &memory;
846 int retval = 0;
848 debug("%s: %p\n", __func__, pdata);
850 dev->pdata = pdata;
852 phy = (struct s3c_usbotg_phy *)pdata->regs_phy;
853 reg = (struct s3c_usbotg_reg *)pdata->regs_otg;
854 usb_phy_ctrl = pdata->usb_phy_ctrl;
856 /* regs_otg = (void *)pdata->regs_otg; */
858 dev->gadget.is_dualspeed = 1; /* Hack only*/
859 dev->gadget.is_otg = 0;
860 dev->gadget.is_a_peripheral = 0;
861 dev->gadget.b_hnp_enable = 0;
862 dev->gadget.a_hnp_support = 0;
863 dev->gadget.a_alt_hnp_support = 0;
865 the_controller = dev;
867 usb_ctrl = memalign(CONFIG_SYS_CACHELINE_SIZE,
868 ROUND(sizeof(struct usb_ctrlrequest),
869 CONFIG_SYS_CACHELINE_SIZE));
870 if (!usb_ctrl) {
871 error("No memory available for UDC!\n");
872 return -ENOMEM;
875 usb_ctrl_dma_addr = (dma_addr_t) usb_ctrl;
877 udc_reinit(dev);
879 return retval;
882 int usb_gadget_handle_interrupts()
884 u32 intr_status = readl(&reg->gintsts);
885 u32 gintmsk = readl(&reg->gintmsk);
887 if (intr_status & gintmsk)
888 return s3c_udc_irq(1, (void *)the_controller);
889 return 0;