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