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