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
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <asm/errno.h>
27 #include <linux/list.h>
30 #include <linux/usb/ch9.h>
31 #include <linux/usb/gadget.h>
34 typedef int spinlock_t;
35 typedef int wait_queue_head_t;
36 typedef int irqreturn_t;
37 #define spin_lock_init(...)
38 #define spin_lock(...)
39 #define spin_lock_irqsave(lock, flags) flags=1
40 #define spin_unlock(...)
41 #define spin_unlock_irqrestore(lock, flags) flags=0
42 #define disable_irq(...)
43 #define enable_irq(...)
45 #define mutex_init(...)
46 #define mutex_lock(...)
47 #define mutex_unlock(...)
49 #define WARN_ON(x) if (x) { printf("WARNING in %s line %d\n", __FILE__, __LINE__); }
62 #define ENOTSUPP 524 /* Operation is not supported */
64 #define EXPORT_SYMBOL(x)
66 #define dma_cache_maint(addr, size, mode) cache_flush()
67 void cache_flush(void);
69 #define kmalloc(size, type) malloc(size)
70 #define kfree(addr) free(addr)
71 #define mdelay(n) ({unsigned long msec = (n); while (msec--) udelay(1000); })
73 #include <asm/byteorder.h>
75 //#include <asm-arm/proc-armv/system.h>
76 #include <asm/mach-types.h>
78 #include <asm/arch/gpio.h>
83 #include <asm/arch/regs-otg.h>
84 #include <asm/arch/hs_otg.h>
85 #include <asm/arch/power.h>
88 /***********************************************************/
90 #define OTG_DMA_MODE 1
92 #undef DEBUG_S3C_UDC_SETUP
93 #undef DEBUG_S3C_UDC_EP0
94 #undef DEBUG_S3C_UDC_ISR
95 #undef DEBUG_S3C_UDC_OUT_EP
96 #undef DEBUG_S3C_UDC_IN_EP
99 /* #define DEBUG_S3C_UDC_SETUP */
100 /* #define DEBUG_S3C_UDC_EP0 */
101 /* #define DEBUG_S3C_UDC_ISR */
102 /* #define DEBUG_S3C_UDC_OUT_EP */
103 /* #define DEBUG_S3C_UDC_IN_EP */
104 /* #define DEBUG_S3C_UDC */
109 #if defined(DEBUG_S3C_UDC_SETUP) || defined(DEBUG_S3C_UDC_ISR)\
110 || defined(DEBUG_S3C_UDC_OUT_EP)
112 static char *state_names[] = {
115 "DATA_STATE_NEED_ZLP",
116 "WAIT_FOR_OUT_STATUS",
119 "WAIT_FOR_OUT_COMPLETE",
120 "WAIT_FOR_IN_COMPLETE",
121 "WAIT_FOR_NULL_COMPLETE",
125 #ifdef DEBUG_S3C_UDC_SETUP
126 #define DEBUG_SETUP(fmt,args...) printk(fmt, ##args)
128 #define DEBUG_SETUP(fmt,args...) do {} while(0)
131 #ifdef DEBUG_S3C_UDC_EP0
132 #define DEBUG_EP0(fmt,args...) printk(fmt, ##args)
134 #define DEBUG_EP0(fmt,args...) do {} while(0)
138 #define DEBUG(fmt,args...) printk(fmt, ##args)
140 #define DEBUG(fmt,args...) do {} while(0)
143 #ifdef DEBUG_S3C_UDC_ISR
144 #define DEBUG_ISR(fmt,args...) printk(fmt, ##args)
146 #define DEBUG_ISR(fmt,args...) do {} while(0)
149 #ifdef DEBUG_S3C_UDC_OUT_EP
150 #define DEBUG_OUT_EP(fmt,args...) printk(fmt, ##args)
152 #define DEBUG_OUT_EP(fmt,args...) do {} while(0)
155 #ifdef DEBUG_S3C_UDC_IN_EP
156 #define DEBUG_IN_EP(fmt,args...) printk(fmt, ##args)
158 #define DEBUG_IN_EP(fmt,args...) do {} while(0)
161 #define DRIVER_DESC "S3C HS USB OTG Device Driver, (c) 2008-2009 Samsung Electronics"
162 #define DRIVER_VERSION "15 March 2009"
164 struct s3c_udc *the_controller;
166 static const char driver_name[] = "s3c-udc";
167 static const char driver_desc[] = DRIVER_DESC;
168 static const char ep0name[] = "ep0-control";
171 static unsigned int ep0_fifo_size = 64;
172 static unsigned int ep_fifo_size = 512;
173 static unsigned int ep_fifo_size2 = 1024;
174 static int reset_available = 1;
176 extern void otg_phy_init(void);
177 extern void otg_phy_off(void);
178 static struct usb_ctrlrequest *usb_ctrl;
179 static dma_addr_t usb_ctrl_dma_addr;
184 static int s3c_ep_enable(struct usb_ep *ep, const struct usb_endpoint_descriptor *);
185 static int s3c_ep_disable(struct usb_ep *ep);
186 static struct usb_request *s3c_alloc_request(struct usb_ep *ep, gfp_t gfp_flags);
187 static void s3c_free_request(struct usb_ep *ep, struct usb_request *);
189 static int s3c_queue(struct usb_ep *ep, struct usb_request *, gfp_t gfp_flags);
190 static int s3c_dequeue(struct usb_ep *ep, struct usb_request *);
191 static int s3c_fifo_status(struct usb_ep *ep);
192 static void s3c_fifo_flush(struct usb_ep *ep);
193 static void s3c_ep0_read(struct s3c_udc *dev);
194 static void s3c_ep0_kick(struct s3c_udc *dev, struct s3c_ep *ep);
195 static void s3c_handle_ep0(struct s3c_udc *dev);
196 static int s3c_ep0_write(struct s3c_udc *dev);
197 static int write_fifo_ep0(struct s3c_ep *ep, struct s3c_request *req);
198 static void done(struct s3c_ep *ep, struct s3c_request *req, int status);
199 static void stop_activity(struct s3c_udc *dev, struct usb_gadget_driver *driver);
200 static int udc_enable(struct s3c_udc *dev);
201 static void udc_set_address(struct s3c_udc *dev, unsigned char address);
202 static void reconfig_usbd(void);
203 static void set_max_pktsize(struct s3c_udc *dev, enum usb_device_speed speed);
204 static void nuke(struct s3c_ep *ep, int status);
205 static int s3c_udc_set_halt(struct usb_ep *_ep, int value);
206 static void s3c_udc_set_nak(struct s3c_ep *ep);
208 static struct usb_ep_ops s3c_ep_ops = {
209 .enable = s3c_ep_enable,
210 .disable = s3c_ep_disable,
212 .alloc_request = s3c_alloc_request,
213 .free_request = s3c_free_request,
216 .dequeue = s3c_dequeue,
218 .set_halt = s3c_udc_set_halt,
219 .fifo_status = s3c_fifo_status,
220 .fifo_flush = s3c_fifo_flush,
223 #define create_proc_files() do {} while (0)
224 #define remove_proc_files() do {} while (0)
226 /***********************************************************/
228 void __iomem *regs_otg;
229 void __iomem *regs_phy;
232 void otg_phy_init(void)
234 the_controller->pdata->phy_control(1);
237 printf("USB PHY0 Enable\n");
240 writel(readl(S5P_USB_PHY_CONTROL)|(0x1<<0), S5P_USB_PHY_CONTROL);
242 #ifdef CONFIG_S5PC110
243 writel((readl(S3C_USBOTG_PHYPWR)
244 &~(0x3<<3)&~(0x1<<0)), S3C_USBOTG_PHYPWR);
246 #ifdef CONFIG_EXYNOS4
247 writel((readl(S3C_USBOTG_PHYPWR)
248 &~(0x7<<3)&~(0x1<<0)), S3C_USBOTG_PHYPWR);
253 writel((readl(S3C_USBOTG_PHYCLK)
254 &~(0x5<<2))|(0x3<<0), S3C_USBOTG_PHYCLK); /* PLL 24Mhz */
255 writel((readl(S3C_USBOTG_RSTCON)
256 &~(0x3<<1))|(0x1<<0), S3C_USBOTG_RSTCON);
258 writel(readl(S3C_USBOTG_RSTCON)
259 &~(0x7<<0), S3C_USBOTG_RSTCON);
263 /* writel((readl(S3C_USBOTG_PHYPWR)&~(0x3<<3)&~(0x1<<0))|(0x1<<5), */
264 /* S3C_USBOTG_PHYPWR); */
265 /* writel((readl(S3C_USBOTG_PHYCLK)&~(0x5<<2))|(0x3<<0), */
266 /* S3C_USBOTG_PHYCLK); */
267 /* writel((readl(S3C_USBOTG_RSTCON)&~(0x3<<1))|(0x1<<0), */
268 /* S3C_USBOTG_RSTCON); */
270 /* writel(0x1, S3C_USBOTG_RSTCON); */
272 /* writel(0x0, S3C_USBOTG_RSTCON); */
276 void otg_phy_off(void)
278 /* reset controller just in case */
279 writel(0x1, S3C_USBOTG_RSTCON);
281 writel(0x0, S3C_USBOTG_RSTCON);
284 writel(readl(S3C_USBOTG_PHYPWR)|(0x3<<3)|(0x1), S3C_USBOTG_PHYPWR);
285 writel(readl(S5P_USB_PHY_CONTROL)&~(1<<0), S5P_USB_PHY_CONTROL);
287 // writel((readl(S3C_USBOTG_PHYPWR)&~(0x3<<3)&~(0x1<<0)),S3C_USBOTG_PHYPWR);
288 writel((readl(S3C_USBOTG_PHYCLK)&~(0x5<<2)),S3C_USBOTG_PHYCLK);
292 the_controller->pdata->phy_control(0);
295 /***********************************************************/
297 #include "s3c_udc_otg_xfer_dma.c"
300 * udc_disable - disable USB device controller
302 static void udc_disable(struct s3c_udc *dev)
304 DEBUG_SETUP("%s: %p\n", __FUNCTION__, dev);
306 udc_set_address(dev, 0);
308 dev->ep0state = WAIT_FOR_SETUP;
309 dev->gadget.speed = USB_SPEED_UNKNOWN;
310 dev->usb_address = 0;
316 * udc_reinit - initialize software state
318 static void udc_reinit(struct s3c_udc *dev)
322 DEBUG_SETUP("%s: %p\n", __FUNCTION__, dev);
324 /* device/ep0 records init */
325 INIT_LIST_HEAD(&dev->gadget.ep_list);
326 INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
327 dev->ep0state = WAIT_FOR_SETUP;
329 /* basic endpoint records init */
330 for (i = 0; i < S3C_MAX_ENDPOINTS; i++) {
331 struct s3c_ep *ep = &dev->ep[i];
334 list_add_tail(&ep->ep.ep_list, &dev->gadget.ep_list);
338 INIT_LIST_HEAD(&ep->queue);
342 /* the rest was statically initialized, and is read-only */
345 #define BYTES2MAXP(x) (x / 8)
346 #define MAXP2BYTES(x) (x * 8)
348 /* until it's enabled, this UDC should be completely invisible
351 static int udc_enable(struct s3c_udc *dev)
353 DEBUG_SETUP("%s: %p\n", __FUNCTION__, dev);
358 DEBUG_SETUP("S3C USB 2.0 OTG Controller Core Initialized : 0x%x\n",
359 readl(S3C_UDC_OTG_GINTMSK));
361 dev->gadget.speed = USB_SPEED_UNKNOWN;
367 Register entry point for the peripheral controller driver.
369 int usb_gadget_register_driver(struct usb_gadget_driver *driver)
371 struct s3c_udc *dev = the_controller;
375 DEBUG_SETUP("%s: %s\n", __FUNCTION__, "no name");
378 || (driver->speed != USB_SPEED_FULL && driver->speed != USB_SPEED_HIGH)
379 || !driver->bind || !driver->disconnect || !driver->setup)
386 spin_lock_irqsave(&dev->lock, flags);
387 /* if (!dev->regulator_a) {
388 dev->regulator_a = regulator_get(&dev->dev->dev, "vusb_a");
389 if (IS_ERR(dev->regulator_a)) {
390 dev_info(&dev->dev->dev, "No VDD_USB_A regualtor\n");
391 dev->regulator_a = NULL;
393 regulator_enable(dev->regulator_a);
396 if (!dev->regulator_d) {
397 dev->regulator_d = regulator_get(&dev->dev->dev, "vusb_d");
398 if (IS_ERR(dev->regulator_d)) {
399 dev_info(&dev->dev->dev, "No VDD_USB_D regualtor\n");
400 dev->regulator_d = NULL;
402 regulator_enable(dev->regulator_d);
405 /* first hook up the driver ... */
406 dev->driver = driver;
407 // dev->gadget.dev.driver = &driver->driver;
408 spin_unlock_irqrestore(&dev->lock, flags);
409 // retval = device_add(&dev->gadget.dev);
411 if(retval) { /* TODO */
412 printk("target device_add failed, error %d\n", retval);
416 retval = driver->bind(&dev->gadget);
418 // printk("%s: bind to driver %s --> error %d\n", dev->gadget.name,
419 // driver->driver.name, retval);
420 // device_del(&dev->gadget.dev);
423 // dev->gadget.dev.driver = 0;
429 // printk("Registered gadget driver %s\n", dev->gadget.name);
435 EXPORT_SYMBOL(usb_gadget_register_driver);
438 Unregister entry point for the peripheral controller driver.
440 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
442 struct s3c_udc *dev = the_controller;
447 if (!driver || driver != dev->driver)
450 spin_lock_irqsave(&dev->lock, flags);
452 stop_activity(dev, driver);
453 spin_unlock_irqrestore(&dev->lock, flags);
455 driver->unbind(&dev->gadget);
456 // device_del(&dev->gadget.dev);
458 disable_irq(IRQ_OTG);
460 // printk("Unregistered gadget driver '%s'\n", driver->driver.name);
464 if (!IS_ERR(dev->regulator_a)) {
465 regulator_disable(dev->regulator_a);
466 regulator_put(dev->regulator_a);
467 dev->regulator_a = NULL;
469 if (!IS_ERR(dev->regulator_d)) {
470 regulator_disable(dev->regulator_d);
471 regulator_put(dev->regulator_d);
472 dev->regulator_d = NULL;
478 EXPORT_SYMBOL(usb_gadget_unregister_driver);
481 * done - retire a request; caller blocked irqs
483 static void done(struct s3c_ep *ep, struct s3c_request *req, int status)
485 unsigned int stopped = ep->stopped;
487 DEBUG("%s: %s %p, req = %p, stopped = %d\n",
488 __FUNCTION__, ep->ep.name, ep, &req->req, stopped);
490 list_del_init(&req->queue);
492 if (likely(req->req.status == -EINPROGRESS)) {
493 req->req.status = status;
495 status = req->req.status;
498 if (status && status != -ESHUTDOWN) {
499 DEBUG("complete %s req %p stat %d len %u/%u\n",
500 ep->ep.name, &req->req, status,
501 req->req.actual, req->req.length);
504 /* don't modify queue heads during completion callback */
508 printf("calling complete callback\n");
510 int i, len = req->req.length;
512 printf("pkt[%d] = ", req->req.length);
515 for (i=0; i<len; i++) {
516 printf("%02x", ((u8*)req->req.buf)[i]);
523 spin_unlock(&ep->dev->lock);
524 req->req.complete(&ep->ep, &req->req);
525 spin_lock(&ep->dev->lock);
527 DEBUG("callback completed\n");
529 ep->stopped = stopped;
533 * nuke - dequeue ALL requests
535 static void nuke(struct s3c_ep *ep, int status)
537 struct s3c_request *req;
539 DEBUG("%s: %s %p\n", __FUNCTION__, ep->ep.name, ep);
541 /* called with irqs blocked */
542 while (!list_empty(&ep->queue)) {
543 req = list_entry(ep->queue.next, struct s3c_request, queue);
544 done(ep, req, status);
548 static void stop_activity(struct s3c_udc *dev,
549 struct usb_gadget_driver *driver)
553 /* don't disconnect drivers more than once */
554 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
556 dev->gadget.speed = USB_SPEED_UNKNOWN;
558 /* prevent new request submissions, kill any outstanding requests */
559 for (i = 0; i < S3C_MAX_ENDPOINTS; i++) {
560 struct s3c_ep *ep = &dev->ep[i];
562 nuke(ep, -ESHUTDOWN);
565 /* report disconnect; the driver is already quiesced */
567 spin_unlock(&dev->lock);
568 driver->disconnect(&dev->gadget);
569 spin_lock(&dev->lock);
572 /* re-init driver-visible data structures */
576 static void reconfig_usbd(void)
578 /* 2. Soft-reset OTG Core and then unreset again. */
580 unsigned int uTemp = writel(CORE_SOFT_RESET, S3C_UDC_OTG_GRSTCTL);
582 DEBUG(2, "Reseting OTG controller\n");
584 writel( 0<<15 /* PHY Low Power Clock sel*/
585 |1<<14 /* Non-Periodic TxFIFO Rewind Enable*/
586 |0x5<<10 /* Turnaround time*/
587 |0<<9|0<<8 /* [0:HNP disable, 1:HNP enable][ 0:SRP disable, 1:SRP enable] H1= 1,1*/
588 |0<<7 /* Ulpi DDR sel*/
589 |0<<6 /* 0: high speed utmi+, 1: full speed serial*/
590 |0<<4 /* 0: utmi+, 1:ulpi*/
591 |1<<3 /* phy i/f 0:8bit, 1:16bit*/
592 |0x7<<0, /* HS/FS Timeout**/
593 S3C_UDC_OTG_GUSBCFG);
595 /* 3. Put the OTG device core in the disconnected state.*/
596 uTemp = readl(S3C_UDC_OTG_DCTL);
597 uTemp |= SOFT_DISCONNECT;
598 writel(uTemp, S3C_UDC_OTG_DCTL);
602 /* 4. Make the OTG device core exit from the disconnected state.*/
603 uTemp = readl(S3C_UDC_OTG_DCTL);
604 uTemp = uTemp & ~SOFT_DISCONNECT;
605 writel(uTemp, S3C_UDC_OTG_DCTL);
607 /* 5. Configure OTG Core to initial settings of device mode.*/
608 writel(1<<18|0x0<<0, S3C_UDC_OTG_DCFG); /* [][1: full speed(30Mhz) 0:high speed]*/
612 /* 6. Unmask the core interrupts*/
613 writel(GINTMSK_INIT, S3C_UDC_OTG_GINTMSK);
615 /* 7. Set NAK bit of EP0, EP1, EP2*/
616 writel(DEPCTL_EPDIS|DEPCTL_SNAK|(0<<0), S3C_UDC_OTG_DOEPCTL(EP0_CON));
617 writel(DEPCTL_EPDIS|DEPCTL_SNAK|(0<<0), S3C_UDC_OTG_DIEPCTL(EP0_CON));
619 for (i = 1; i < S3C_MAX_ENDPOINTS; i++) {
620 writel(DEPCTL_EPDIS|DEPCTL_SNAK, S3C_UDC_OTG_DOEPCTL(i));
621 writel(DEPCTL_EPDIS|DEPCTL_SNAK, S3C_UDC_OTG_DIEPCTL(i));
624 /* 8. Unmask EPO interrupts*/
625 writel( ((1<<EP0_CON)<<DAINT_OUT_BIT)|(1<<EP0_CON), S3C_UDC_OTG_DAINTMSK);
627 /* 9. Unmask device OUT EP common interrupts*/
628 writel(DOEPMSK_INIT, S3C_UDC_OTG_DOEPMSK);
630 /* 10. Unmask device IN EP common interrupts*/
631 writel(DIEPMSK_INIT, S3C_UDC_OTG_DIEPMSK);
633 /* 11. Set Rx FIFO Size (in 32-bit words) */
634 writel(RX_FIFO_SIZE >> 2, S3C_UDC_OTG_GRXFSIZ);
636 /* 12. Set Non Periodic Tx FIFO Size*/
637 writel((NPTX_FIFO_SIZE >> 2) << 16 | ((RX_FIFO_SIZE >> 2)) << 0,
638 S3C_UDC_OTG_GNPTXFSIZ);
640 for (i = 1; i < S3C_MAX_HW_ENDPOINTS; i++)
641 writel((PTX_FIFO_SIZE >> 2) << 16 |
642 ((RX_FIFO_SIZE + NPTX_FIFO_SIZE + PTX_FIFO_SIZE*(i-1)) >> 2) << 0,
643 S3C_UDC_OTG_DIEPTXF(i));
645 /* check if defined tx fifo sizes fits in SPRAM (S5PC110 fifo has 7936 entries */
646 #if (((RX_FIFO_SIZE + NPTX_FIFO_SIZE + PTX_FIFO_SIZE*(S3C_MAX_HW_ENDPOINTS-1)) >> 2) >= 7936)
647 #error Too large tx fifo size defined!
650 /* Flush the RX FIFO */
651 writel(0x10, S3C_UDC_OTG_GRSTCTL);
652 while(readl(S3C_UDC_OTG_GRSTCTL) & 0x10)
653 DEBUG("%s: waiting for S3C_UDC_OTG_GRSTCTL\n", __FUNCTION__);
655 /* Flush all the Tx FIFO's */
656 writel(0x10<<6, S3C_UDC_OTG_GRSTCTL);
657 writel((0x10<<6)|0x20, S3C_UDC_OTG_GRSTCTL);
658 while(readl(S3C_UDC_OTG_GRSTCTL) & 0x20)
659 DEBUG("%s: waiting for S3C_UDC_OTG_GRSTCTL\n", __FUNCTION__);
661 /* 13. Clear NAK bit of EP0, EP1, EP2*/
663 writel(DEPCTL_EPDIS|DEPCTL_CNAK|(0<<0), S3C_UDC_OTG_DOEPCTL(EP0_CON)); /* EP0: Control OUT */
665 /* 14. Initialize OTG Link Core.*/
666 writel(GAHBCFG_INIT, S3C_UDC_OTG_GAHBCFG);
669 static void set_max_pktsize(struct s3c_udc *dev, enum usb_device_speed speed)
671 unsigned int ep_ctrl;
674 if (speed == USB_SPEED_HIGH) {
677 ep_fifo_size2 = 1024;
678 dev->gadget.speed = USB_SPEED_HIGH;
683 dev->gadget.speed = USB_SPEED_FULL;
686 dev->ep[0].ep.maxpacket = ep0_fifo_size;
687 for(i = 1; i < S3C_MAX_ENDPOINTS; i++)
688 dev->ep[i].ep.maxpacket = ep_fifo_size;
690 /* EP0 - Control IN (64 bytes)*/
691 ep_ctrl = readl(S3C_UDC_OTG_DIEPCTL(EP0_CON));
692 writel(ep_ctrl|(0<<0), S3C_UDC_OTG_DIEPCTL(EP0_CON));
694 /* EP0 - Control OUT (64 bytes)*/
695 ep_ctrl = readl(S3C_UDC_OTG_DOEPCTL(EP0_CON));
696 writel(ep_ctrl|(0<<0), S3C_UDC_OTG_DOEPCTL(EP0_CON));
699 static int s3c_ep_enable(struct usb_ep *_ep,
700 const struct usb_endpoint_descriptor *desc)
706 DEBUG("%s: %p\n", __FUNCTION__, _ep);
708 ep = container_of(_ep, struct s3c_ep, ep);
709 if (!_ep || !desc || ep->desc || _ep->name == ep0name
710 || desc->bDescriptorType != USB_DT_ENDPOINT
711 || ep->bEndpointAddress != desc->bEndpointAddress
712 || ep_maxpacket(ep) < le16_to_cpu(desc->wMaxPacketSize)) {
714 DEBUG("%s: bad ep or descriptor\n", __FUNCTION__);
718 /* xfer types must match, except that interrupt ~= bulk */
719 if (ep->bmAttributes != desc->bmAttributes
720 && ep->bmAttributes != USB_ENDPOINT_XFER_BULK
721 && desc->bmAttributes != USB_ENDPOINT_XFER_INT) {
723 DEBUG("%s: %s type mismatch\n", __FUNCTION__, _ep->name);
727 /* hardware _could_ do smaller, but driver doesn't */
728 if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
729 && le16_to_cpu(desc->wMaxPacketSize) != ep_maxpacket(ep))
730 || !desc->wMaxPacketSize) {
732 DEBUG("%s: bad %s maxpacket\n", __FUNCTION__, _ep->name);
737 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
739 DEBUG("%s: bogus device state\n", __FUNCTION__);
746 ep->ep.maxpacket = le16_to_cpu(desc->wMaxPacketSize);
748 /* Reset halt state */
750 s3c_udc_set_halt(_ep, 0);
752 spin_lock_irqsave(&ep->dev->lock, flags);
753 s3c_udc_ep_activate(ep);
754 spin_unlock_irqrestore(&ep->dev->lock, flags);
756 DEBUG("%s: enabled %s, stopped = %d, maxpacket = %d\n",
757 __FUNCTION__, _ep->name, ep->stopped, ep->ep.maxpacket);
763 static int s3c_ep_disable(struct usb_ep *_ep)
768 DEBUG("%s: %p\n", __FUNCTION__, _ep);
770 ep = container_of(_ep, struct s3c_ep, ep);
771 if (!_ep || !ep->desc) {
772 DEBUG("%s: %s not enabled\n", __FUNCTION__,
773 _ep ? ep->ep.name : NULL);
777 spin_lock_irqsave(&ep->dev->lock, flags);
779 /* Nuke all pending requests */
780 nuke(ep, -ESHUTDOWN);
785 spin_unlock_irqrestore(&ep->dev->lock, flags);
787 DEBUG("%s: disabled %s\n", __FUNCTION__, _ep->name);
791 static struct usb_request *s3c_alloc_request(struct usb_ep *ep,
794 struct s3c_request *req;
796 DEBUG("%s: %s %p\n", __FUNCTION__, ep->name, ep);
798 req = kmalloc(sizeof *req, gfp_flags);
802 memset(req, 0, sizeof *req);
803 INIT_LIST_HEAD(&req->queue);
808 static void s3c_free_request(struct usb_ep *ep, struct usb_request *_req)
810 struct s3c_request *req;
812 DEBUG("%s: %p\n", __FUNCTION__, ep);
814 req = container_of(_req, struct s3c_request, req);
815 WARN_ON(!list_empty(&req->queue));
819 /* dequeue JUST ONE request */
820 static int s3c_dequeue(struct usb_ep *_ep, struct usb_request *_req)
823 struct s3c_request *req;
826 DEBUG("%s: %p\n", __FUNCTION__, _ep);
828 ep = container_of(_ep, struct s3c_ep, ep);
829 if (!_ep || ep->ep.name == ep0name)
832 spin_lock_irqsave(&ep->dev->lock, flags);
834 /* make sure it's actually queued on this endpoint */
835 list_for_each_entry(req, &ep->queue, queue) {
836 if (&req->req == _req)
839 if (&req->req != _req) {
840 spin_unlock_irqrestore(&ep->dev->lock, flags);
844 done(ep, req, -ECONNRESET);
846 spin_unlock_irqrestore(&ep->dev->lock, flags);
850 /** Return bytes in EP FIFO
852 static int s3c_fifo_status(struct usb_ep *_ep)
857 ep = container_of(_ep, struct s3c_ep, ep);
859 DEBUG("%s: bad ep\n", __FUNCTION__);
863 DEBUG("%s: %d\n", __FUNCTION__, ep_index(ep));
865 /* LPD can't report unclaimed bytes from IN fifos */
874 static void s3c_fifo_flush(struct usb_ep *_ep)
878 ep = container_of(_ep, struct s3c_ep, ep);
879 if (unlikely(!_ep || (!ep->desc && ep->ep.name != ep0name))) {
880 DEBUG("%s: bad ep\n", __FUNCTION__);
884 DEBUG("%s: %d\n", __FUNCTION__, ep_index(ep));
888 /* ---------------------------------------------------------------------------
889 * device-scoped parts of the api to the usb controller hardware
890 * ---------------------------------------------------------------------------
893 static int s3c_udc_get_frame(struct usb_gadget *_gadget)
895 /*fram count number [21:8]*/
896 unsigned int frame = readl(S3C_UDC_OTG_DSTS);
898 DEBUG("%s: %p\n", __FUNCTION__, _gadget);
899 return (frame & 0x3ff00);
902 static int s3c_udc_wakeup(struct usb_gadget *_gadget)
904 DEBUG("%s: %p\n", __FUNCTION__, _gadget);
909 static const struct usb_gadget_ops s3c_udc_ops = {
910 /* current versions must always be self-powered */
913 static struct s3c_udc memory = {
918 .ep0 = &memory.ep[0].ep,
921 .init_name = "gadget",
922 .release = nop_release,
927 /* control endpoint */
932 .maxpacket = EP0_FIFO_SIZE,
936 .bEndpointAddress = 0,
939 .ep_type = ep_control,
942 /* first group of endpoints */
945 .name = "ep1in-bulk",
947 .maxpacket = EP_FIFO_SIZE,
951 .bEndpointAddress = USB_DIR_IN | 1,
952 .bmAttributes = USB_ENDPOINT_XFER_BULK,
954 .ep_type = ep_bulk_out,
960 .name = "ep2out-bulk",
962 .maxpacket = EP_FIFO_SIZE,
966 .bEndpointAddress = USB_DIR_OUT | 2,
967 .bmAttributes = USB_ENDPOINT_XFER_BULK,
969 .ep_type = ep_bulk_in,
977 .maxpacket = EP_FIFO_SIZE,
981 .bEndpointAddress = USB_DIR_IN | 3,
982 .bmAttributes = USB_ENDPOINT_XFER_INT,
984 .ep_type = ep_interrupt,
990 .name = "ep4out-bulk",
992 .maxpacket = EP_FIFO_SIZE,
996 .bEndpointAddress = USB_DIR_OUT | 4,
997 .bmAttributes = USB_ENDPOINT_XFER_BULK,
999 .ep_type = ep_bulk_out,
1004 .name = "ep5in-bulk",
1006 .maxpacket = EP_FIFO_SIZE,
1010 .bEndpointAddress = USB_DIR_IN | 5,
1011 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1013 .ep_type = ep_bulk_in,
1018 .name = "ep6in-int",
1020 .maxpacket = EP_FIFO_SIZE,
1024 .bEndpointAddress = USB_DIR_IN | 6,
1025 .bmAttributes = USB_ENDPOINT_XFER_INT,
1027 .ep_type = ep_interrupt,
1032 .name = "ep7out-bulk",
1034 .maxpacket = EP_FIFO_SIZE,
1038 .bEndpointAddress = USB_DIR_OUT | 7,
1039 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1041 .ep_type = ep_bulk_out,
1046 .name = "ep8in-bulk",
1048 .maxpacket = EP_FIFO_SIZE,
1052 .bEndpointAddress = USB_DIR_IN | 8,
1053 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1055 .ep_type = ep_bulk_in,
1060 .name = "ep9in-int",
1062 .maxpacket = EP_FIFO_SIZE,
1066 .bEndpointAddress = USB_DIR_IN | 9,
1067 .bmAttributes = USB_ENDPOINT_XFER_INT,
1069 .ep_type = ep_interrupt,
1074 .name = "ep10out-bulk",
1076 .maxpacket = EP_FIFO_SIZE,
1080 .bEndpointAddress = USB_DIR_OUT | 10,
1081 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1083 .ep_type = ep_bulk_out,
1088 .name = "ep11in-bulk",
1090 .maxpacket = EP_FIFO_SIZE,
1094 .bEndpointAddress = USB_DIR_IN | 11,
1095 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1097 .ep_type = ep_bulk_in,
1102 .name = "ep12in-int",
1104 .maxpacket = EP_FIFO_SIZE,
1108 .bEndpointAddress = USB_DIR_IN | 12,
1109 .bmAttributes = USB_ENDPOINT_XFER_INT,
1111 .ep_type = ep_interrupt,
1116 .name = "ep13out-bulk",
1118 .maxpacket = EP_FIFO_SIZE,
1122 .bEndpointAddress = USB_DIR_OUT | 13,
1123 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1125 .ep_type = ep_bulk_out,
1130 .name = "ep14in-bulk",
1132 .maxpacket = EP_FIFO_SIZE,
1136 .bEndpointAddress = USB_DIR_IN | 14,
1137 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1139 .ep_type = ep_bulk_in,
1144 .name = "ep15in-int",
1146 .maxpacket = EP_FIFO_SIZE,
1150 .bEndpointAddress = USB_DIR_IN | 15,
1151 .bmAttributes = USB_ENDPOINT_XFER_INT,
1153 .ep_type = ep_interrupt,
1160 * probe - binds to the platform device
1163 int s3c_udc_probe(struct s3c_plat_otg_data *pdata)
1165 struct s3c_udc *dev = &memory;
1168 DEBUG("%s: %p\n", __FUNCTION__, pdata);
1172 regs_phy = (void *)pdata->regs_phy;
1173 regs_otg = (void*)pdata->regs_otg;
1175 dev->gadget.is_dualspeed = 1; /* Hack only*/
1176 dev->gadget.is_otg = 0;
1177 dev->gadget.is_a_peripheral = 0;
1178 dev->gadget.b_hnp_enable = 0;
1179 dev->gadget.a_hnp_support = 0;
1180 dev->gadget.a_alt_hnp_support = 0;
1182 the_controller = dev;
1184 for (i = 0; i < S3C_MAX_ENDPOINTS+1; i++) {
1185 dev->dma_buf[i] = kmalloc(DMA_BUFFER_SIZE, GFP_KERNEL);
1186 dev->dma_addr[i] = dev->dma_buf[i];
1188 usb_ctrl = dev->dma_buf[0];
1189 usb_ctrl_dma_addr = dev->dma_addr[0];
1196 static int s3c_udc_remove(struct platform_device *pdev)
1202 int usb_gadget_handle_interrupts()
1204 u32 intr_status = readl(S3C_UDC_OTG_GINTSTS);
1205 u32 gintmsk = readl(S3C_UDC_OTG_GINTMSK);
1207 if (intr_status & gintmsk)
1208 return s3c_udc_irq(1, (void*)the_controller);