]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/usb/gadget/s3c_udc_otg.c
usb:udc:samsung:cleanup Replace DEBUG_* macros with debug_cond() calls
[karo-tx-uboot.git] / drivers / usb / gadget / s3c_udc_otg.c
1 /*
2  * drivers/usb/gadget/s3c_udc_otg.c
3  * Samsung S3C on-chip full/high speed USB OTG 2.0 device controllers
4  *
5  * Copyright (C) 2008 for Samsung Electronics
6  *
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
10  *
11  * State machine bugfixes:
12  * Marek Szyprowski <m.szyprowski@samsung.com>
13  *
14  * Ported to u-boot:
15  * Marek Szyprowski <m.szyprowski@samsung.com>
16  * Lukasz Majewski <l.majewski@samsumg.com>
17  *
18  * This program is free software; you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation; either version 2 of the License, or
21  * (at your option) any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program; if not, write to the Free Software
30  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
31  *
32  */
33
34 #include <common.h>
35 #include <asm/errno.h>
36 #include <linux/list.h>
37 #include <malloc.h>
38
39 #include <linux/usb/ch9.h>
40 #include <usbdescriptors.h>
41 #include <linux/usb/gadget.h>
42
43 #include <asm/byteorder.h>
44 #include <asm/unaligned.h>
45 #include <asm/io.h>
46
47 #include <asm/mach-types.h>
48 #include <asm/arch/gpio.h>
49
50 #include "regs-otg.h"
51 #include <usb/lin_gadget_compat.h>
52
53 /***********************************************************/
54
55 #define OTG_DMA_MODE            1
56
57 #define DEBUG_SETUP 0
58 #define DEBUG_EP0 0
59 #define DEBUG_ISR 0
60 #define DEBUG_OUT_EP 0
61 #define DEBUG_IN_EP 0
62
63 #include <usb/s3c_udc.h>
64
65 #define EP0_CON         0
66 #define EP_MASK         0xF
67
68 static char *state_names[] = {
69         "WAIT_FOR_SETUP",
70         "DATA_STATE_XMIT",
71         "DATA_STATE_NEED_ZLP",
72         "WAIT_FOR_OUT_STATUS",
73         "DATA_STATE_RECV",
74         "WAIT_FOR_COMPLETE",
75         "WAIT_FOR_OUT_COMPLETE",
76         "WAIT_FOR_IN_COMPLETE",
77         "WAIT_FOR_NULL_COMPLETE",
78 };
79
80 #define DRIVER_DESC "S3C HS USB OTG Device Driver, (c) Samsung Electronics"
81 #define DRIVER_VERSION "15 March 2009"
82
83 struct s3c_udc  *the_controller;
84
85 static const char driver_name[] = "s3c-udc";
86 static const char driver_desc[] = DRIVER_DESC;
87 static const char ep0name[] = "ep0-control";
88
89 /* Max packet size*/
90 static unsigned int ep0_fifo_size = 64;
91 static unsigned int ep_fifo_size =  512;
92 static unsigned int ep_fifo_size2 = 1024;
93 static int reset_available = 1;
94
95 static struct usb_ctrlrequest *usb_ctrl;
96 static dma_addr_t usb_ctrl_dma_addr;
97
98 /*
99   Local declarations.
100 */
101 static int s3c_ep_enable(struct usb_ep *ep,
102                          const struct usb_endpoint_descriptor *);
103 static int s3c_ep_disable(struct usb_ep *ep);
104 static struct usb_request *s3c_alloc_request(struct usb_ep *ep,
105                                              gfp_t gfp_flags);
106 static void s3c_free_request(struct usb_ep *ep, struct usb_request *);
107
108 static int s3c_queue(struct usb_ep *ep, struct usb_request *, gfp_t gfp_flags);
109 static int s3c_dequeue(struct usb_ep *ep, struct usb_request *);
110 static int s3c_fifo_status(struct usb_ep *ep);
111 static void s3c_fifo_flush(struct usb_ep *ep);
112 static void s3c_ep0_read(struct s3c_udc *dev);
113 static void s3c_ep0_kick(struct s3c_udc *dev, struct s3c_ep *ep);
114 static void s3c_handle_ep0(struct s3c_udc *dev);
115 static int s3c_ep0_write(struct s3c_udc *dev);
116 static int write_fifo_ep0(struct s3c_ep *ep, struct s3c_request *req);
117 static void done(struct s3c_ep *ep, struct s3c_request *req, int status);
118 static void stop_activity(struct s3c_udc *dev,
119                           struct usb_gadget_driver *driver);
120 static int udc_enable(struct s3c_udc *dev);
121 static void udc_set_address(struct s3c_udc *dev, unsigned char address);
122 static void reconfig_usbd(void);
123 static void set_max_pktsize(struct s3c_udc *dev, enum usb_device_speed speed);
124 static void nuke(struct s3c_ep *ep, int status);
125 static int s3c_udc_set_halt(struct usb_ep *_ep, int value);
126 static void s3c_udc_set_nak(struct s3c_ep *ep);
127
128 static struct usb_ep_ops s3c_ep_ops = {
129         .enable = s3c_ep_enable,
130         .disable = s3c_ep_disable,
131
132         .alloc_request = s3c_alloc_request,
133         .free_request = s3c_free_request,
134
135         .queue = s3c_queue,
136         .dequeue = s3c_dequeue,
137
138         .set_halt = s3c_udc_set_halt,
139         .fifo_status = s3c_fifo_status,
140         .fifo_flush = s3c_fifo_flush,
141 };
142
143 #define create_proc_files() do {} while (0)
144 #define remove_proc_files() do {} while (0)
145
146 /***********************************************************/
147
148 void __iomem            *regs_otg;
149 struct s3c_usbotg_reg *reg;
150 struct s3c_usbotg_phy *phy;
151 static unsigned int usb_phy_ctrl;
152
153 void otg_phy_init(struct s3c_udc *dev)
154 {
155         dev->pdata->phy_control(1);
156
157         /*USB PHY0 Enable */
158         printf("USB PHY0 Enable\n");
159
160         /* Enable PHY */
161         writel(readl(usb_phy_ctrl) | USB_PHY_CTRL_EN0, usb_phy_ctrl);
162
163         if (dev->pdata->usb_flags == PHY0_SLEEP) /* C210 Universal */
164                 writel((readl(&phy->phypwr)
165                         &~(PHY_0_SLEEP | OTG_DISABLE_0 | ANALOG_PWRDOWN)
166                         &~FORCE_SUSPEND_0), &phy->phypwr);
167         else /* C110 GONI */
168                 writel((readl(&phy->phypwr) &~(OTG_DISABLE_0 | ANALOG_PWRDOWN)
169                         &~FORCE_SUSPEND_0), &phy->phypwr);
170
171         writel((readl(&phy->phyclk) &~(ID_PULLUP0 | COMMON_ON_N0)) |
172                CLK_SEL_24MHZ, &phy->phyclk); /* PLL 24Mhz */
173
174         writel((readl(&phy->rstcon) &~(LINK_SW_RST | PHYLNK_SW_RST))
175                | PHY_SW_RST0, &phy->rstcon);
176         udelay(10);
177         writel(readl(&phy->rstcon)
178                &~(PHY_SW_RST0 | LINK_SW_RST | PHYLNK_SW_RST), &phy->rstcon);
179         udelay(10);
180 }
181
182 void otg_phy_off(struct s3c_udc *dev)
183 {
184         /* reset controller just in case */
185         writel(PHY_SW_RST0, &phy->rstcon);
186         udelay(20);
187         writel(readl(&phy->phypwr) &~PHY_SW_RST0, &phy->rstcon);
188         udelay(20);
189
190         writel(readl(&phy->phypwr) | OTG_DISABLE_0 | ANALOG_PWRDOWN
191                | FORCE_SUSPEND_0, &phy->phypwr);
192
193         writel(readl(usb_phy_ctrl) &~USB_PHY_CTRL_EN0, usb_phy_ctrl);
194
195         writel((readl(&phy->phyclk) & ~(ID_PULLUP0 | COMMON_ON_N0)),
196               &phy->phyclk);
197
198         udelay(10000);
199
200         dev->pdata->phy_control(0);
201 }
202
203 /***********************************************************/
204
205 #include "s3c_udc_otg_xfer_dma.c"
206
207 /*
208  *      udc_disable - disable USB device controller
209  */
210 static void udc_disable(struct s3c_udc *dev)
211 {
212         debug_cond(DEBUG_SETUP != 0, "%s: %p\n", __func__, dev);
213
214         udc_set_address(dev, 0);
215
216         dev->ep0state = WAIT_FOR_SETUP;
217         dev->gadget.speed = USB_SPEED_UNKNOWN;
218         dev->usb_address = 0;
219
220         otg_phy_off(dev);
221 }
222
223 /*
224  *      udc_reinit - initialize software state
225  */
226 static void udc_reinit(struct s3c_udc *dev)
227 {
228         unsigned int i;
229
230         debug_cond(DEBUG_SETUP != 0, "%s: %p\n", __func__, dev);
231
232         /* device/ep0 records init */
233         INIT_LIST_HEAD(&dev->gadget.ep_list);
234         INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
235         dev->ep0state = WAIT_FOR_SETUP;
236
237         /* basic endpoint records init */
238         for (i = 0; i < S3C_MAX_ENDPOINTS; i++) {
239                 struct s3c_ep *ep = &dev->ep[i];
240
241                 if (i != 0)
242                         list_add_tail(&ep->ep.ep_list, &dev->gadget.ep_list);
243
244                 ep->desc = 0;
245                 ep->stopped = 0;
246                 INIT_LIST_HEAD(&ep->queue);
247                 ep->pio_irqs = 0;
248         }
249
250         /* the rest was statically initialized, and is read-only */
251 }
252
253 #define BYTES2MAXP(x)   (x / 8)
254 #define MAXP2BYTES(x)   (x * 8)
255
256 /* until it's enabled, this UDC should be completely invisible
257  * to any USB host.
258  */
259 static int udc_enable(struct s3c_udc *dev)
260 {
261         debug_cond(DEBUG_SETUP != 0, "%s: %p\n", __func__, dev);
262
263         otg_phy_init(dev);
264         reconfig_usbd();
265
266         debug_cond(DEBUG_SETUP != 0,
267                    "S3C USB 2.0 OTG Controller Core Initialized : 0x%x\n",
268                     readl(&reg->gintmsk));
269
270         dev->gadget.speed = USB_SPEED_UNKNOWN;
271
272         return 0;
273 }
274
275 /*
276   Register entry point for the peripheral controller driver.
277 */
278 int usb_gadget_register_driver(struct usb_gadget_driver *driver)
279 {
280         struct s3c_udc *dev = the_controller;
281         int retval = 0;
282         unsigned long flags;
283
284         debug_cond(DEBUG_SETUP != 0, "%s: %s\n", __func__, "no name");
285
286         if (!driver
287             || (driver->speed != USB_SPEED_FULL
288                 && driver->speed != USB_SPEED_HIGH)
289             || !driver->bind || !driver->disconnect || !driver->setup)
290                 return -EINVAL;
291         if (!dev)
292                 return -ENODEV;
293         if (dev->driver)
294                 return -EBUSY;
295
296         spin_lock_irqsave(&dev->lock, flags);
297         /* first hook up the driver ... */
298         dev->driver = driver;
299         spin_unlock_irqrestore(&dev->lock, flags);
300
301         if (retval) { /* TODO */
302                 printf("target device_add failed, error %d\n", retval);
303                 return retval;
304         }
305
306         retval = driver->bind(&dev->gadget);
307         if (retval) {
308                 debug_cond(DEBUG_SETUP != 0,
309                            "%s: bind to driver --> error %d\n",
310                             dev->gadget.name, retval);
311                 dev->driver = 0;
312                 return retval;
313         }
314
315         enable_irq(IRQ_OTG);
316
317         debug_cond(DEBUG_SETUP != 0,
318                    "Registered gadget driver %s\n", dev->gadget.name);
319         udc_enable(dev);
320
321         return 0;
322 }
323
324 /*
325  * Unregister entry point for the peripheral controller driver.
326  */
327 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
328 {
329         struct s3c_udc *dev = the_controller;
330         unsigned long flags;
331
332         if (!dev)
333                 return -ENODEV;
334         if (!driver || driver != dev->driver)
335                 return -EINVAL;
336
337         spin_lock_irqsave(&dev->lock, flags);
338         dev->driver = 0;
339         stop_activity(dev, driver);
340         spin_unlock_irqrestore(&dev->lock, flags);
341
342         driver->unbind(&dev->gadget);
343
344         disable_irq(IRQ_OTG);
345
346         udc_disable(dev);
347         return 0;
348 }
349
350 /*
351  *      done - retire a request; caller blocked irqs
352  */
353 static void done(struct s3c_ep *ep, struct s3c_request *req, int status)
354 {
355         unsigned int stopped = ep->stopped;
356
357         debug("%s: %s %p, req = %p, stopped = %d\n",
358               __func__, ep->ep.name, ep, &req->req, stopped);
359
360         list_del_init(&req->queue);
361
362         if (likely(req->req.status == -EINPROGRESS))
363                 req->req.status = status;
364         else
365                 status = req->req.status;
366
367         if (status && status != -ESHUTDOWN) {
368                 debug("complete %s req %p stat %d len %u/%u\n",
369                       ep->ep.name, &req->req, status,
370                       req->req.actual, req->req.length);
371         }
372
373         /* don't modify queue heads during completion callback */
374         ep->stopped = 1;
375
376 #ifdef DEBUG
377         printf("calling complete callback\n");
378         {
379                 int i, len = req->req.length;
380
381                 printf("pkt[%d] = ", req->req.length);
382                 if (len > 64)
383                         len = 64;
384                 for (i = 0; i < len; i++) {
385                         printf("%02x", ((u8 *)req->req.buf)[i]);
386                         if ((i & 7) == 7)
387                                 printf(" ");
388                 }
389                 printf("\n");
390         }
391 #endif
392         spin_unlock(&ep->dev->lock);
393         req->req.complete(&ep->ep, &req->req);
394         spin_lock(&ep->dev->lock);
395
396         debug("callback completed\n");
397
398         ep->stopped = stopped;
399 }
400
401 /*
402  *      nuke - dequeue ALL requests
403  */
404 static void nuke(struct s3c_ep *ep, int status)
405 {
406         struct s3c_request *req;
407
408         debug("%s: %s %p\n", __func__, ep->ep.name, ep);
409
410         /* called with irqs blocked */
411         while (!list_empty(&ep->queue)) {
412                 req = list_entry(ep->queue.next, struct s3c_request, queue);
413                 done(ep, req, status);
414         }
415 }
416
417 static void stop_activity(struct s3c_udc *dev,
418                           struct usb_gadget_driver *driver)
419 {
420         int i;
421
422         /* don't disconnect drivers more than once */
423         if (dev->gadget.speed == USB_SPEED_UNKNOWN)
424                 driver = 0;
425         dev->gadget.speed = USB_SPEED_UNKNOWN;
426
427         /* prevent new request submissions, kill any outstanding requests  */
428         for (i = 0; i < S3C_MAX_ENDPOINTS; i++) {
429                 struct s3c_ep *ep = &dev->ep[i];
430                 ep->stopped = 1;
431                 nuke(ep, -ESHUTDOWN);
432         }
433
434         /* report disconnect; the driver is already quiesced */
435         if (driver) {
436                 spin_unlock(&dev->lock);
437                 driver->disconnect(&dev->gadget);
438                 spin_lock(&dev->lock);
439         }
440
441         /* re-init driver-visible data structures */
442         udc_reinit(dev);
443 }
444
445 static void reconfig_usbd(void)
446 {
447         /* 2. Soft-reset OTG Core and then unreset again. */
448         int i;
449         unsigned int uTemp = writel(CORE_SOFT_RESET, &reg->grstctl);
450
451         debug("Reseting OTG controller\n");
452
453         writel(0<<15            /* PHY Low Power Clock sel*/
454                 |1<<14          /* Non-Periodic TxFIFO Rewind Enable*/
455                 |0x5<<10        /* Turnaround time*/
456                 |0<<9 | 0<<8    /* [0:HNP disable,1:HNP enable][ 0:SRP disable*/
457                                 /* 1:SRP enable] H1= 1,1*/
458                 |0<<7           /* Ulpi DDR sel*/
459                 |0<<6           /* 0: high speed utmi+, 1: full speed serial*/
460                 |0<<4           /* 0: utmi+, 1:ulpi*/
461                 |1<<3           /* phy i/f  0:8bit, 1:16bit*/
462                 |0x7<<0,        /* HS/FS Timeout**/
463                 &reg->gusbcfg);
464
465         /* 3. Put the OTG device core in the disconnected state.*/
466         uTemp = readl(&reg->dctl);
467         uTemp |= SOFT_DISCONNECT;
468         writel(uTemp, &reg->dctl);
469
470         udelay(20);
471
472         /* 4. Make the OTG device core exit from the disconnected state.*/
473         uTemp = readl(&reg->dctl);
474         uTemp = uTemp & ~SOFT_DISCONNECT;
475         writel(uTemp, &reg->dctl);
476
477         /* 5. Configure OTG Core to initial settings of device mode.*/
478         /* [][1: full speed(30Mhz) 0:high speed]*/
479         writel(EP_MISS_CNT(1) | DEV_SPEED_HIGH_SPEED_20, &reg->dcfg);
480
481         mdelay(1);
482
483         /* 6. Unmask the core interrupts*/
484         writel(GINTMSK_INIT, &reg->gintmsk);
485
486         /* 7. Set NAK bit of EP0, EP1, EP2*/
487         writel(DEPCTL_EPDIS|DEPCTL_SNAK, &reg->out_endp[EP0_CON].doepctl);
488         writel(DEPCTL_EPDIS|DEPCTL_SNAK, &reg->in_endp[EP0_CON].diepctl);
489
490         for (i = 1; i < S3C_MAX_ENDPOINTS; i++) {
491                 writel(DEPCTL_EPDIS|DEPCTL_SNAK, &reg->out_endp[i].doepctl);
492                 writel(DEPCTL_EPDIS|DEPCTL_SNAK, &reg->in_endp[i].diepctl);
493         }
494
495         /* 8. Unmask EPO interrupts*/
496         writel(((1 << EP0_CON) << DAINT_OUT_BIT)
497                | (1 << EP0_CON), &reg->daintmsk);
498
499         /* 9. Unmask device OUT EP common interrupts*/
500         writel(DOEPMSK_INIT, &reg->doepmsk);
501
502         /* 10. Unmask device IN EP common interrupts*/
503         writel(DIEPMSK_INIT, &reg->diepmsk);
504
505         /* 11. Set Rx FIFO Size (in 32-bit words) */
506         writel(RX_FIFO_SIZE >> 2, &reg->grxfsiz);
507
508         /* 12. Set Non Periodic Tx FIFO Size */
509         writel((NPTX_FIFO_SIZE >> 2) << 16 | ((RX_FIFO_SIZE >> 2)) << 0,
510                &reg->gnptxfsiz);
511
512         for (i = 1; i < S3C_MAX_HW_ENDPOINTS; i++)
513                 writel((PTX_FIFO_SIZE >> 2) << 16 |
514                        ((RX_FIFO_SIZE + NPTX_FIFO_SIZE +
515                          PTX_FIFO_SIZE*(i-1)) >> 2) << 0,
516                        &reg->dieptxf[i-1]);
517
518         /* Flush the RX FIFO */
519         writel(RX_FIFO_FLUSH, &reg->grstctl);
520         while (readl(&reg->grstctl) & RX_FIFO_FLUSH)
521                 debug("%s: waiting for S3C_UDC_OTG_GRSTCTL\n", __func__);
522
523         /* Flush all the Tx FIFO's */
524         writel(TX_FIFO_FLUSH_ALL, &reg->grstctl);
525         writel(TX_FIFO_FLUSH_ALL | TX_FIFO_FLUSH, &reg->grstctl);
526         while (readl(&reg->grstctl) & TX_FIFO_FLUSH)
527                 debug("%s: waiting for S3C_UDC_OTG_GRSTCTL\n", __func__);
528
529         /* 13. Clear NAK bit of EP0, EP1, EP2*/
530         /* For Slave mode*/
531         /* EP0: Control OUT */
532         writel(DEPCTL_EPDIS | DEPCTL_CNAK,
533                &reg->out_endp[EP0_CON].doepctl);
534
535         /* 14. Initialize OTG Link Core.*/
536         writel(GAHBCFG_INIT, &reg->gahbcfg);
537 }
538
539 static void set_max_pktsize(struct s3c_udc *dev, enum usb_device_speed speed)
540 {
541         unsigned int ep_ctrl;
542         int i;
543
544         if (speed == USB_SPEED_HIGH) {
545                 ep0_fifo_size = 64;
546                 ep_fifo_size = 512;
547                 ep_fifo_size2 = 1024;
548                 dev->gadget.speed = USB_SPEED_HIGH;
549         } else {
550                 ep0_fifo_size = 64;
551                 ep_fifo_size = 64;
552                 ep_fifo_size2 = 64;
553                 dev->gadget.speed = USB_SPEED_FULL;
554         }
555
556         dev->ep[0].ep.maxpacket = ep0_fifo_size;
557         for (i = 1; i < S3C_MAX_ENDPOINTS; i++)
558                 dev->ep[i].ep.maxpacket = ep_fifo_size;
559
560         /* EP0 - Control IN (64 bytes)*/
561         ep_ctrl = readl(&reg->in_endp[EP0_CON].diepctl);
562         writel(ep_ctrl|(0<<0), &reg->in_endp[EP0_CON].diepctl);
563
564         /* EP0 - Control OUT (64 bytes)*/
565         ep_ctrl = readl(&reg->out_endp[EP0_CON].doepctl);
566         writel(ep_ctrl|(0<<0), &reg->out_endp[EP0_CON].doepctl);
567 }
568
569 static int s3c_ep_enable(struct usb_ep *_ep,
570                          const struct usb_endpoint_descriptor *desc)
571 {
572         struct s3c_ep *ep;
573         struct s3c_udc *dev;
574         unsigned long flags;
575
576         debug("%s: %p\n", __func__, _ep);
577
578         ep = container_of(_ep, struct s3c_ep, ep);
579         if (!_ep || !desc || ep->desc || _ep->name == ep0name
580             || desc->bDescriptorType != USB_DT_ENDPOINT
581             || ep->bEndpointAddress != desc->bEndpointAddress
582             || ep_maxpacket(ep) <
583             le16_to_cpu(get_unaligned(&desc->wMaxPacketSize))) {
584
585                 debug("%s: bad ep or descriptor\n", __func__);
586                 return -EINVAL;
587         }
588
589         /* xfer types must match, except that interrupt ~= bulk */
590         if (ep->bmAttributes != desc->bmAttributes
591             && ep->bmAttributes != USB_ENDPOINT_XFER_BULK
592             && desc->bmAttributes != USB_ENDPOINT_XFER_INT) {
593
594                 debug("%s: %s type mismatch\n", __func__, _ep->name);
595                 return -EINVAL;
596         }
597
598         /* hardware _could_ do smaller, but driver doesn't */
599         if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
600              && le16_to_cpu(get_unaligned(&desc->wMaxPacketSize)) !=
601              ep_maxpacket(ep)) || !get_unaligned(&desc->wMaxPacketSize)) {
602
603                 debug("%s: bad %s maxpacket\n", __func__, _ep->name);
604                 return -ERANGE;
605         }
606
607         dev = ep->dev;
608         if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
609
610                 debug("%s: bogus device state\n", __func__);
611                 return -ESHUTDOWN;
612         }
613
614         ep->stopped = 0;
615         ep->desc = desc;
616         ep->pio_irqs = 0;
617         ep->ep.maxpacket = le16_to_cpu(get_unaligned(&desc->wMaxPacketSize));
618
619         /* Reset halt state */
620         s3c_udc_set_nak(ep);
621         s3c_udc_set_halt(_ep, 0);
622
623         spin_lock_irqsave(&ep->dev->lock, flags);
624         s3c_udc_ep_activate(ep);
625         spin_unlock_irqrestore(&ep->dev->lock, flags);
626
627         debug("%s: enabled %s, stopped = %d, maxpacket = %d\n",
628               __func__, _ep->name, ep->stopped, ep->ep.maxpacket);
629         return 0;
630 }
631
632 /*
633  * Disable EP
634  */
635 static int s3c_ep_disable(struct usb_ep *_ep)
636 {
637         struct s3c_ep *ep;
638         unsigned long flags;
639
640         debug("%s: %p\n", __func__, _ep);
641
642         ep = container_of(_ep, struct s3c_ep, ep);
643         if (!_ep || !ep->desc) {
644                 debug("%s: %s not enabled\n", __func__,
645                       _ep ? ep->ep.name : NULL);
646                 return -EINVAL;
647         }
648
649         spin_lock_irqsave(&ep->dev->lock, flags);
650
651         /* Nuke all pending requests */
652         nuke(ep, -ESHUTDOWN);
653
654         ep->desc = 0;
655         ep->stopped = 1;
656
657         spin_unlock_irqrestore(&ep->dev->lock, flags);
658
659         debug("%s: disabled %s\n", __func__, _ep->name);
660         return 0;
661 }
662
663 static struct usb_request *s3c_alloc_request(struct usb_ep *ep,
664                                              gfp_t gfp_flags)
665 {
666         struct s3c_request *req;
667
668         debug("%s: %s %p\n", __func__, ep->name, ep);
669
670         req = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*req));
671         if (!req)
672                 return 0;
673
674         memset(req, 0, sizeof *req);
675         INIT_LIST_HEAD(&req->queue);
676
677         return &req->req;
678 }
679
680 static void s3c_free_request(struct usb_ep *ep, struct usb_request *_req)
681 {
682         struct s3c_request *req;
683
684         debug("%s: %p\n", __func__, ep);
685
686         req = container_of(_req, struct s3c_request, req);
687         WARN_ON(!list_empty(&req->queue));
688         kfree(req);
689 }
690
691 /* dequeue JUST ONE request */
692 static int s3c_dequeue(struct usb_ep *_ep, struct usb_request *_req)
693 {
694         struct s3c_ep *ep;
695         struct s3c_request *req;
696         unsigned long flags;
697
698         debug("%s: %p\n", __func__, _ep);
699
700         ep = container_of(_ep, struct s3c_ep, ep);
701         if (!_ep || ep->ep.name == ep0name)
702                 return -EINVAL;
703
704         spin_lock_irqsave(&ep->dev->lock, flags);
705
706         /* make sure it's actually queued on this endpoint */
707         list_for_each_entry(req, &ep->queue, queue) {
708                 if (&req->req == _req)
709                         break;
710         }
711         if (&req->req != _req) {
712                 spin_unlock_irqrestore(&ep->dev->lock, flags);
713                 return -EINVAL;
714         }
715
716         done(ep, req, -ECONNRESET);
717
718         spin_unlock_irqrestore(&ep->dev->lock, flags);
719         return 0;
720 }
721
722 /*
723  * Return bytes in EP FIFO
724  */
725 static int s3c_fifo_status(struct usb_ep *_ep)
726 {
727         int count = 0;
728         struct s3c_ep *ep;
729
730         ep = container_of(_ep, struct s3c_ep, ep);
731         if (!_ep) {
732                 debug("%s: bad ep\n", __func__);
733                 return -ENODEV;
734         }
735
736         debug("%s: %d\n", __func__, ep_index(ep));
737
738         /* LPD can't report unclaimed bytes from IN fifos */
739         if (ep_is_in(ep))
740                 return -EOPNOTSUPP;
741
742         return count;
743 }
744
745 /*
746  * Flush EP FIFO
747  */
748 static void s3c_fifo_flush(struct usb_ep *_ep)
749 {
750         struct s3c_ep *ep;
751
752         ep = container_of(_ep, struct s3c_ep, ep);
753         if (unlikely(!_ep || (!ep->desc && ep->ep.name != ep0name))) {
754                 debug("%s: bad ep\n", __func__);
755                 return;
756         }
757
758         debug("%s: %d\n", __func__, ep_index(ep));
759 }
760
761 static const struct usb_gadget_ops s3c_udc_ops = {
762         /* current versions must always be self-powered */
763 };
764
765 static struct s3c_udc memory = {
766         .usb_address = 0,
767         .gadget = {
768                 .ops = &s3c_udc_ops,
769                 .ep0 = &memory.ep[0].ep,
770                 .name = driver_name,
771         },
772
773         /* control endpoint */
774         .ep[0] = {
775                 .ep = {
776                         .name = ep0name,
777                         .ops = &s3c_ep_ops,
778                         .maxpacket = EP0_FIFO_SIZE,
779                 },
780                 .dev = &memory,
781
782                 .bEndpointAddress = 0,
783                 .bmAttributes = 0,
784
785                 .ep_type = ep_control,
786         },
787
788         /* first group of endpoints */
789         .ep[1] = {
790                 .ep = {
791                         .name = "ep1in-bulk",
792                         .ops = &s3c_ep_ops,
793                         .maxpacket = EP_FIFO_SIZE,
794                 },
795                 .dev = &memory,
796
797                 .bEndpointAddress = USB_DIR_IN | 1,
798                 .bmAttributes = USB_ENDPOINT_XFER_BULK,
799
800                 .ep_type = ep_bulk_out,
801                 .fifo_num = 1,
802         },
803
804         .ep[2] = {
805                 .ep = {
806                         .name = "ep2out-bulk",
807                         .ops = &s3c_ep_ops,
808                         .maxpacket = EP_FIFO_SIZE,
809                 },
810                 .dev = &memory,
811
812                 .bEndpointAddress = USB_DIR_OUT | 2,
813                 .bmAttributes = USB_ENDPOINT_XFER_BULK,
814
815                 .ep_type = ep_bulk_in,
816                 .fifo_num = 2,
817         },
818
819         .ep[3] = {
820                 .ep = {
821                         .name = "ep3in-int",
822                         .ops = &s3c_ep_ops,
823                         .maxpacket = EP_FIFO_SIZE,
824                 },
825                 .dev = &memory,
826
827                 .bEndpointAddress = USB_DIR_IN | 3,
828                 .bmAttributes = USB_ENDPOINT_XFER_INT,
829
830                 .ep_type = ep_interrupt,
831                 .fifo_num = 3,
832         },
833 };
834
835 /*
836  *      probe - binds to the platform device
837  */
838
839 int s3c_udc_probe(struct s3c_plat_otg_data *pdata)
840 {
841         struct s3c_udc *dev = &memory;
842         int retval = 0, i;
843
844         debug("%s: %p\n", __func__, pdata);
845
846         dev->pdata = pdata;
847
848         phy = (struct s3c_usbotg_phy *)pdata->regs_phy;
849         reg = (struct s3c_usbotg_reg *)pdata->regs_otg;
850         usb_phy_ctrl = pdata->usb_phy_ctrl;
851
852         /* regs_otg = (void *)pdata->regs_otg; */
853
854         dev->gadget.is_dualspeed = 1;   /* Hack only*/
855         dev->gadget.is_otg = 0;
856         dev->gadget.is_a_peripheral = 0;
857         dev->gadget.b_hnp_enable = 0;
858         dev->gadget.a_hnp_support = 0;
859         dev->gadget.a_alt_hnp_support = 0;
860
861         the_controller = dev;
862
863         for (i = 0; i < S3C_MAX_ENDPOINTS+1; i++) {
864                 dev->dma_buf[i] = memalign(CONFIG_SYS_CACHELINE_SIZE,
865                                            DMA_BUFFER_SIZE);
866                 dev->dma_addr[i] = (dma_addr_t) dev->dma_buf[i];
867                 invalidate_dcache_range((unsigned long) dev->dma_buf[i],
868                                         (unsigned long) (dev->dma_buf[i]
869                                                          + DMA_BUFFER_SIZE));
870         }
871         usb_ctrl = dev->dma_buf[0];
872         usb_ctrl_dma_addr = dev->dma_addr[0];
873
874         udc_reinit(dev);
875
876         return retval;
877 }
878
879 int usb_gadget_handle_interrupts()
880 {
881         u32 intr_status = readl(&reg->gintsts);
882         u32 gintmsk = readl(&reg->gintmsk);
883
884         if (intr_status & gintmsk)
885                 return s3c_udc_irq(1, (void *)the_controller);
886         return 0;
887 }