tizen 2.3.1 release
[platform/kernel/u-boot.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  *
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.
12  *
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.
17  *
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
21  *
22  */
23
24
25 #include <common.h>
26 #include <asm/errno.h>
27 #include <linux/list.h>
28 #include <malloc.h>
29
30 #include <linux/usb/ch9.h>
31 #include <linux/usb/gadget.h>
32
33 /* common */
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(...)
44
45 #define mutex_init(...)
46 #define mutex_lock(...)
47 #define mutex_unlock(...)
48
49 #define WARN_ON(x) if (x) { printf("WARNING in %s line %d\n", __FILE__, __LINE__); }
50
51 #define printk printf
52
53 #define KERN_WARNING
54 #define KERN_ERR
55 #define KERN_NOTICE
56 #define KERN_DEBUG
57
58 #define GFP_KERNEL                      0
59
60 #define IRQ_HANDLED     1
61
62 #define ENOTSUPP        524     /* Operation is not supported */
63
64 #define EXPORT_SYMBOL(x)
65
66 #define dma_cache_maint(addr, size, mode) cache_flush()
67 void cache_flush(void);
68
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); })
72
73 #include <asm/byteorder.h>
74 #include <asm/io.h>
75 //#include <asm-arm/proc-armv/system.h>
76 #include <asm/mach-types.h>
77
78 #include <asm/arch/gpio.h>
79
80 #define __iomem
81
82 #include "s3c_udc.h"
83 #include <asm/arch/regs-otg.h>
84 #include <asm/arch/hs_otg.h>
85 #include <asm/arch/power.h>
86
87
88 /***********************************************************/
89
90 #define OTG_DMA_MODE            1
91
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
97 #undef DEBUG_S3C_UDC
98
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 */
105
106 #define EP0_CON         0
107 #define EP_MASK         0xF
108
109 #if defined(DEBUG_S3C_UDC_SETUP) || defined(DEBUG_S3C_UDC_ISR)\
110         || defined(DEBUG_S3C_UDC_OUT_EP)
111
112 static char *state_names[] = {
113         "WAIT_FOR_SETUP",
114         "DATA_STATE_XMIT",
115         "DATA_STATE_NEED_ZLP",
116         "WAIT_FOR_OUT_STATUS",
117         "DATA_STATE_RECV",
118         "WAIT_FOR_COMPLETE",
119         "WAIT_FOR_OUT_COMPLETE",
120         "WAIT_FOR_IN_COMPLETE",
121         "WAIT_FOR_NULL_COMPLETE",
122         };
123 #endif
124
125 #ifdef DEBUG_S3C_UDC_SETUP
126 #define DEBUG_SETUP(fmt,args...) printk(fmt, ##args)
127 #else
128 #define DEBUG_SETUP(fmt,args...) do {} while(0)
129 #endif
130
131 #ifdef DEBUG_S3C_UDC_EP0
132 #define DEBUG_EP0(fmt,args...) printk(fmt, ##args)
133 #else
134 #define DEBUG_EP0(fmt,args...) do {} while(0)
135 #endif
136
137 #ifdef DEBUG_S3C_UDC
138 #define DEBUG(fmt,args...) printk(fmt, ##args)
139 #else
140 #define DEBUG(fmt,args...) do {} while(0)
141 #endif
142
143 #ifdef DEBUG_S3C_UDC_ISR
144 #define DEBUG_ISR(fmt,args...) printk(fmt, ##args)
145 #else
146 #define DEBUG_ISR(fmt,args...) do {} while(0)
147 #endif
148
149 #ifdef DEBUG_S3C_UDC_OUT_EP
150 #define DEBUG_OUT_EP(fmt,args...) printk(fmt, ##args)
151 #else
152 #define DEBUG_OUT_EP(fmt,args...) do {} while(0)
153 #endif
154
155 #ifdef DEBUG_S3C_UDC_IN_EP
156 #define DEBUG_IN_EP(fmt,args...) printk(fmt, ##args)
157 #else
158 #define DEBUG_IN_EP(fmt,args...) do {} while(0)
159 #endif
160
161 #define DRIVER_DESC             "S3C HS USB OTG Device Driver, (c) 2008-2009 Samsung Electronics"
162 #define DRIVER_VERSION          "15 March 2009"
163
164 struct s3c_udc  *the_controller;
165
166 static const char driver_name[] = "s3c-udc";
167 static const char driver_desc[] = DRIVER_DESC;
168 static const char ep0name[] = "ep0-control";
169
170 /* Max packet size*/
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;
175
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;
180
181 /*
182   Local declarations.
183 */
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 *);
188
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);
207
208 static struct usb_ep_ops s3c_ep_ops = {
209         .enable = s3c_ep_enable,
210         .disable = s3c_ep_disable,
211
212         .alloc_request = s3c_alloc_request,
213         .free_request = s3c_free_request,
214
215         .queue = s3c_queue,
216         .dequeue = s3c_dequeue,
217
218         .set_halt = s3c_udc_set_halt,
219         .fifo_status = s3c_fifo_status,
220         .fifo_flush = s3c_fifo_flush,
221 };
222
223 #define create_proc_files() do {} while (0)
224 #define remove_proc_files() do {} while (0)
225
226 /***********************************************************/
227
228 void __iomem            *regs_otg;
229 void __iomem            *regs_phy;
230
231
232 void otg_phy_init(void)
233 {
234         the_controller->pdata->phy_control(1);
235
236         /*USB PHY0 Enable */
237         printf("USB PHY0 Enable\n");
238
239         /* Enable PHY */
240         writel(readl(S5P_USB_PHY_CONTROL)|(0x1<<0), S5P_USB_PHY_CONTROL);
241
242 #ifdef CONFIG_S5PC110
243         writel((readl(S3C_USBOTG_PHYPWR)
244                     &~(0x3<<3)&~(0x1<<0)), S3C_USBOTG_PHYPWR);
245 #else
246 #ifdef CONFIG_EXYNOS4
247         writel((readl(S3C_USBOTG_PHYPWR)
248                     &~(0x7<<3)&~(0x1<<0)), S3C_USBOTG_PHYPWR);
249 #else
250 #error
251 #endif
252 #endif
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);
257         udelay(10);
258         writel(readl(S3C_USBOTG_RSTCON)
259                    &~(0x7<<0), S3C_USBOTG_RSTCON);
260         udelay(10);
261
262
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); */
269
270         /* writel(0x1, S3C_USBOTG_RSTCON); */
271         /* udelay(20); */
272         /* writel(0x0, S3C_USBOTG_RSTCON); */
273         /* udelay(20); */
274 }
275
276 void otg_phy_off(void)
277 {
278         /* reset controller just in case */
279         writel(0x1, S3C_USBOTG_RSTCON);
280         udelay(20);
281         writel(0x0, S3C_USBOTG_RSTCON);
282         udelay(20);
283
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);
286
287 //      writel((readl(S3C_USBOTG_PHYPWR)&~(0x3<<3)&~(0x1<<0)),S3C_USBOTG_PHYPWR);
288         writel((readl(S3C_USBOTG_PHYCLK)&~(0x5<<2)),S3C_USBOTG_PHYCLK);
289
290         udelay(10000);
291
292         the_controller->pdata->phy_control(0);
293 }
294
295 /***********************************************************/
296
297 #include "s3c_udc_otg_xfer_dma.c"
298
299 /*
300  *      udc_disable - disable USB device controller
301  */
302 static void udc_disable(struct s3c_udc *dev)
303 {
304         DEBUG_SETUP("%s: %p\n", __FUNCTION__, dev);
305
306         udc_set_address(dev, 0);
307
308         dev->ep0state = WAIT_FOR_SETUP;
309         dev->gadget.speed = USB_SPEED_UNKNOWN;
310         dev->usb_address = 0;
311
312         otg_phy_off();
313 }
314
315 /*
316  *      udc_reinit - initialize software state
317  */
318 static void udc_reinit(struct s3c_udc *dev)
319 {
320         unsigned int i;
321
322         DEBUG_SETUP("%s: %p\n", __FUNCTION__, dev);
323
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;
328
329         /* basic endpoint records init */
330         for (i = 0; i < S3C_MAX_ENDPOINTS; i++) {
331                 struct s3c_ep *ep = &dev->ep[i];
332
333                 if (i != 0)
334                         list_add_tail(&ep->ep.ep_list, &dev->gadget.ep_list);
335
336                 ep->desc = 0;
337                 ep->stopped = 0;
338                 INIT_LIST_HEAD(&ep->queue);
339                 ep->pio_irqs = 0;
340         }
341
342         /* the rest was statically initialized, and is read-only */
343 }
344
345 #define BYTES2MAXP(x)   (x / 8)
346 #define MAXP2BYTES(x)   (x * 8)
347
348 /* until it's enabled, this UDC should be completely invisible
349  * to any USB host.
350  */
351 static int udc_enable(struct s3c_udc *dev)
352 {
353         DEBUG_SETUP("%s: %p\n", __FUNCTION__, dev);
354
355         otg_phy_init();
356         reconfig_usbd();
357
358         DEBUG_SETUP("S3C USB 2.0 OTG Controller Core Initialized : 0x%x\n",
359                         readl(S3C_UDC_OTG_GINTMSK));
360
361         dev->gadget.speed = USB_SPEED_UNKNOWN;
362
363         return 0;
364 }
365
366 /*
367   Register entry point for the peripheral controller driver.
368 */
369 int usb_gadget_register_driver(struct usb_gadget_driver *driver)
370 {
371         struct s3c_udc *dev = the_controller;
372         int retval = 0;
373         unsigned long flags;
374
375         DEBUG_SETUP("%s: %s\n", __FUNCTION__, "no name");
376
377         if (!driver
378             || (driver->speed != USB_SPEED_FULL && driver->speed != USB_SPEED_HIGH)
379             || !driver->bind || !driver->disconnect || !driver->setup)
380                 return -EINVAL;
381         if (!dev)
382                 return -ENODEV;
383         if (dev->driver)
384                 return -EBUSY;
385
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;
392                 } else
393                         regulator_enable(dev->regulator_a);
394         }
395
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;
401                 } else
402                         regulator_enable(dev->regulator_d);
403         }
404 */
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);
410
411         if(retval) { /* TODO */
412                 printk("target device_add failed, error %d\n", retval);
413                 return retval;
414         }
415
416         retval = driver->bind(&dev->gadget);
417         if (retval) {
418 //              printk("%s: bind to driver %s --> error %d\n", dev->gadget.name,
419 //                     driver->driver.name, retval);
420 //              device_del(&dev->gadget.dev);
421
422                 dev->driver = 0;
423 //              dev->gadget.dev.driver = 0;
424                 return retval;
425         }
426
427         enable_irq(IRQ_OTG);
428
429 //      printk("Registered gadget driver %s\n", dev->gadget.name);
430         udc_enable(dev);
431
432         return 0;
433 }
434
435 EXPORT_SYMBOL(usb_gadget_register_driver);
436
437 /*
438   Unregister entry point for the peripheral controller driver.
439 */
440 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
441 {
442         struct s3c_udc *dev = the_controller;
443         unsigned long flags;
444
445         if (!dev)
446                 return -ENODEV;
447         if (!driver || driver != dev->driver)
448                 return -EINVAL;
449
450         spin_lock_irqsave(&dev->lock, flags);
451         dev->driver = 0;
452         stop_activity(dev, driver);
453         spin_unlock_irqrestore(&dev->lock, flags);
454
455         driver->unbind(&dev->gadget);
456 //      device_del(&dev->gadget.dev);
457
458         disable_irq(IRQ_OTG);
459
460 //      printk("Unregistered gadget driver '%s'\n", driver->driver.name);
461
462         udc_disable(dev);
463 /*
464         if (!IS_ERR(dev->regulator_a)) {
465                 regulator_disable(dev->regulator_a);
466                 regulator_put(dev->regulator_a);
467                 dev->regulator_a = NULL;
468         }
469         if (!IS_ERR(dev->regulator_d)) {
470                 regulator_disable(dev->regulator_d);
471                 regulator_put(dev->regulator_d);
472                 dev->regulator_d = NULL;
473         }
474 */
475         return 0;
476 }
477
478 EXPORT_SYMBOL(usb_gadget_unregister_driver);
479
480 /*
481  *      done - retire a request; caller blocked irqs
482  */
483 static void done(struct s3c_ep *ep, struct s3c_request *req, int status)
484 {
485         unsigned int stopped = ep->stopped;
486
487         DEBUG("%s: %s %p, req = %p, stopped = %d\n",
488                 __FUNCTION__, ep->ep.name, ep, &req->req, stopped);
489
490         list_del_init(&req->queue);
491
492         if (likely(req->req.status == -EINPROGRESS)) {
493                 req->req.status = status;
494         } else {
495                 status = req->req.status;
496         }
497
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);
502         }
503
504         /* don't modify queue heads during completion callback */
505         ep->stopped = 1;
506
507 #ifdef DEBUG_S3C_UDC
508         printf("calling complete callback\n");
509         {
510                 int i, len = req->req.length;
511
512                 printf("pkt[%d] = ", req->req.length);
513                 if (len > 64)
514                         len = 64;
515                 for (i=0; i<len; i++) {
516                         printf("%02x", ((u8*)req->req.buf)[i]);
517                         if ((i & 7) == 7)
518                                 printf(" ");
519                 }
520                 printf("\n");
521         }
522 #endif
523         spin_unlock(&ep->dev->lock);
524         req->req.complete(&ep->ep, &req->req);
525         spin_lock(&ep->dev->lock);
526
527         DEBUG("callback completed\n");
528
529         ep->stopped = stopped;
530 }
531
532 /*
533  *      nuke - dequeue ALL requests
534  */
535 static void nuke(struct s3c_ep *ep, int status)
536 {
537         struct s3c_request *req;
538
539         DEBUG("%s: %s %p\n", __FUNCTION__, ep->ep.name, ep);
540
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);
545         }
546 }
547
548 static void stop_activity(struct s3c_udc *dev,
549                           struct usb_gadget_driver *driver)
550 {
551         int i;
552
553         /* don't disconnect drivers more than once */
554         if (dev->gadget.speed == USB_SPEED_UNKNOWN)
555                 driver = 0;
556         dev->gadget.speed = USB_SPEED_UNKNOWN;
557
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];
561                 ep->stopped = 1;
562                 nuke(ep, -ESHUTDOWN);
563         }
564
565         /* report disconnect; the driver is already quiesced */
566         if (driver) {
567                 spin_unlock(&dev->lock);
568                 driver->disconnect(&dev->gadget);
569                 spin_lock(&dev->lock);
570         }
571
572         /* re-init driver-visible data structures */
573         udc_reinit(dev);
574 }
575
576 static void reconfig_usbd(void)
577 {
578         /* 2. Soft-reset OTG Core and then unreset again. */
579         int i;
580         unsigned int uTemp = writel(CORE_SOFT_RESET, S3C_UDC_OTG_GRSTCTL);
581
582         DEBUG(2, "Reseting OTG controller\n");
583
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);
594
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);
599
600         udelay(20);
601
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);
606
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]*/
609
610         mdelay(1);
611
612         /* 6. Unmask the core interrupts*/
613         writel(GINTMSK_INIT, S3C_UDC_OTG_GINTMSK);
614
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));
618
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));
622         }
623
624         /* 8. Unmask EPO interrupts*/
625         writel( ((1<<EP0_CON)<<DAINT_OUT_BIT)|(1<<EP0_CON), S3C_UDC_OTG_DAINTMSK);
626
627         /* 9. Unmask device OUT EP common interrupts*/
628         writel(DOEPMSK_INIT, S3C_UDC_OTG_DOEPMSK);
629
630         /* 10. Unmask device IN EP common interrupts*/
631         writel(DIEPMSK_INIT, S3C_UDC_OTG_DIEPMSK);
632
633         /* 11. Set Rx FIFO Size (in 32-bit words) */
634         writel(RX_FIFO_SIZE >> 2, S3C_UDC_OTG_GRXFSIZ);
635
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);
639
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));
644
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!
648 #endif
649
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__);
654
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__);
660
661         /* 13. Clear NAK bit of EP0, EP1, EP2*/
662         /* For Slave mode*/
663         writel(DEPCTL_EPDIS|DEPCTL_CNAK|(0<<0), S3C_UDC_OTG_DOEPCTL(EP0_CON)); /* EP0: Control OUT */
664
665         /* 14. Initialize OTG Link Core.*/
666         writel(GAHBCFG_INIT, S3C_UDC_OTG_GAHBCFG);
667 }
668
669 static void set_max_pktsize(struct s3c_udc *dev, enum usb_device_speed speed)
670 {
671         unsigned int ep_ctrl;
672         int i;
673
674         if (speed == USB_SPEED_HIGH) {
675                 ep0_fifo_size = 64;
676                 ep_fifo_size = 512;
677                 ep_fifo_size2 = 1024;
678                 dev->gadget.speed = USB_SPEED_HIGH;
679         } else {
680                 ep0_fifo_size = 64;
681                 ep_fifo_size = 64;
682                 ep_fifo_size2 = 64;
683                 dev->gadget.speed = USB_SPEED_FULL;
684         }
685
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;
689
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));
693
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));
697 }
698
699 static int s3c_ep_enable(struct usb_ep *_ep,
700                              const struct usb_endpoint_descriptor *desc)
701 {
702         struct s3c_ep *ep;
703         struct s3c_udc *dev;
704         unsigned long flags;
705
706         DEBUG("%s: %p\n", __FUNCTION__, _ep);
707
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)) {
713
714                 DEBUG("%s: bad ep or descriptor\n", __FUNCTION__);
715                 return -EINVAL;
716         }
717
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) {
722
723                 DEBUG("%s: %s type mismatch\n", __FUNCTION__, _ep->name);
724                 return -EINVAL;
725         }
726
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) {
731
732                 DEBUG("%s: bad %s maxpacket\n", __FUNCTION__, _ep->name);
733                 return -ERANGE;
734         }
735
736         dev = ep->dev;
737         if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
738
739                 DEBUG("%s: bogus device state\n", __FUNCTION__);
740                 return -ESHUTDOWN;
741         }
742
743         ep->stopped = 0;
744         ep->desc = desc;
745         ep->pio_irqs = 0;
746         ep->ep.maxpacket = le16_to_cpu(desc->wMaxPacketSize);
747
748         /* Reset halt state */
749         s3c_udc_set_nak(ep);
750         s3c_udc_set_halt(_ep, 0);
751
752         spin_lock_irqsave(&ep->dev->lock, flags);
753         s3c_udc_ep_activate(ep);
754         spin_unlock_irqrestore(&ep->dev->lock, flags);
755
756         DEBUG("%s: enabled %s, stopped = %d, maxpacket = %d\n",
757                 __FUNCTION__, _ep->name, ep->stopped, ep->ep.maxpacket);
758         return 0;
759 }
760
761 /** Disable EP
762  */
763 static int s3c_ep_disable(struct usb_ep *_ep)
764 {
765         struct s3c_ep *ep;
766         unsigned long flags;
767
768         DEBUG("%s: %p\n", __FUNCTION__, _ep);
769
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);
774                 return -EINVAL;
775         }
776
777         spin_lock_irqsave(&ep->dev->lock, flags);
778
779         /* Nuke all pending requests */
780         nuke(ep, -ESHUTDOWN);
781
782         ep->desc = 0;
783         ep->stopped = 1;
784
785         spin_unlock_irqrestore(&ep->dev->lock, flags);
786
787         DEBUG("%s: disabled %s\n", __FUNCTION__, _ep->name);
788         return 0;
789 }
790
791 static struct usb_request *s3c_alloc_request(struct usb_ep *ep,
792                                                  gfp_t gfp_flags)
793 {
794         struct s3c_request *req;
795
796         DEBUG("%s: %s %p\n", __FUNCTION__, ep->name, ep);
797
798         req = kmalloc(sizeof *req, gfp_flags);
799         if (!req)
800                 return 0;
801
802         memset(req, 0, sizeof *req);
803         INIT_LIST_HEAD(&req->queue);
804
805         return &req->req;
806 }
807
808 static void s3c_free_request(struct usb_ep *ep, struct usb_request *_req)
809 {
810         struct s3c_request *req;
811
812         DEBUG("%s: %p\n", __FUNCTION__, ep);
813
814         req = container_of(_req, struct s3c_request, req);
815         WARN_ON(!list_empty(&req->queue));
816         kfree(req);
817 }
818
819 /* dequeue JUST ONE request */
820 static int s3c_dequeue(struct usb_ep *_ep, struct usb_request *_req)
821 {
822         struct s3c_ep *ep;
823         struct s3c_request *req;
824         unsigned long flags;
825
826         DEBUG("%s: %p\n", __FUNCTION__, _ep);
827
828         ep = container_of(_ep, struct s3c_ep, ep);
829         if (!_ep || ep->ep.name == ep0name)
830                 return -EINVAL;
831
832         spin_lock_irqsave(&ep->dev->lock, flags);
833
834         /* make sure it's actually queued on this endpoint */
835         list_for_each_entry(req, &ep->queue, queue) {
836                 if (&req->req == _req)
837                         break;
838         }
839         if (&req->req != _req) {
840                 spin_unlock_irqrestore(&ep->dev->lock, flags);
841                 return -EINVAL;
842         }
843
844         done(ep, req, -ECONNRESET);
845
846         spin_unlock_irqrestore(&ep->dev->lock, flags);
847         return 0;
848 }
849
850 /** Return bytes in EP FIFO
851  */
852 static int s3c_fifo_status(struct usb_ep *_ep)
853 {
854         int count = 0;
855         struct s3c_ep *ep;
856
857         ep = container_of(_ep, struct s3c_ep, ep);
858         if (!_ep) {
859                 DEBUG("%s: bad ep\n", __FUNCTION__);
860                 return -ENODEV;
861         }
862
863         DEBUG("%s: %d\n", __FUNCTION__, ep_index(ep));
864
865         /* LPD can't report unclaimed bytes from IN fifos */
866         if (ep_is_in(ep))
867                 return -EOPNOTSUPP;
868
869         return count;
870 }
871
872 /** Flush EP FIFO
873  */
874 static void s3c_fifo_flush(struct usb_ep *_ep)
875 {
876         struct s3c_ep *ep;
877
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__);
881                 return;
882         }
883
884         DEBUG("%s: %d\n", __FUNCTION__, ep_index(ep));
885 }
886
887 #if 0
888 /* ---------------------------------------------------------------------------
889  *      device-scoped parts of the api to the usb controller hardware
890  * ---------------------------------------------------------------------------
891  */
892
893 static int s3c_udc_get_frame(struct usb_gadget *_gadget)
894 {
895         /*fram count number [21:8]*/
896         unsigned int frame = readl(S3C_UDC_OTG_DSTS);
897
898         DEBUG("%s: %p\n", __FUNCTION__, _gadget);
899         return (frame & 0x3ff00);
900 }
901
902 static int s3c_udc_wakeup(struct usb_gadget *_gadget)
903 {
904         DEBUG("%s: %p\n", __FUNCTION__, _gadget);
905         return -ENOTSUPP;
906 }
907 #endif
908
909 static const struct usb_gadget_ops s3c_udc_ops = {
910         /* current versions must always be self-powered */
911 };
912
913 static struct s3c_udc memory = {
914         .usb_address = 0,
915
916         .gadget = {
917                    .ops = &s3c_udc_ops,
918                    .ep0 = &memory.ep[0].ep,
919                    .name = driver_name,
920 /*                 .dev = {
921                            .init_name = "gadget",
922                            .release = nop_release,
923                            },
924 */
925                    },
926
927         /* control endpoint */
928         .ep[0] = {
929                   .ep = {
930                          .name = ep0name,
931                          .ops = &s3c_ep_ops,
932                          .maxpacket = EP0_FIFO_SIZE,
933                          },
934                   .dev = &memory,
935
936                   .bEndpointAddress = 0,
937                   .bmAttributes = 0,
938
939                   .ep_type = ep_control,
940                   },
941
942         /* first group of endpoints */
943         .ep[1] = {
944                   .ep = {
945                          .name = "ep1in-bulk",
946                          .ops = &s3c_ep_ops,
947                          .maxpacket = EP_FIFO_SIZE,
948                          },
949                   .dev = &memory,
950
951                   .bEndpointAddress = USB_DIR_IN | 1,
952                   .bmAttributes = USB_ENDPOINT_XFER_BULK,
953
954                   .ep_type = ep_bulk_out,
955                   .fifo_num = 1,
956                   },
957
958         .ep[2] = {
959                   .ep = {
960                          .name = "ep2out-bulk",
961                          .ops = &s3c_ep_ops,
962                          .maxpacket = EP_FIFO_SIZE,
963                          },
964                   .dev = &memory,
965
966                   .bEndpointAddress = USB_DIR_OUT | 2,
967                   .bmAttributes = USB_ENDPOINT_XFER_BULK,
968
969                   .ep_type = ep_bulk_in,
970                   .fifo_num = 2,
971                   },
972
973         .ep[3] = {
974                   .ep = {
975                          .name = "ep3in-int",
976                          .ops = &s3c_ep_ops,
977                          .maxpacket = EP_FIFO_SIZE,
978                          },
979                   .dev = &memory,
980
981                   .bEndpointAddress = USB_DIR_IN | 3,
982                   .bmAttributes = USB_ENDPOINT_XFER_INT,
983
984                   .ep_type = ep_interrupt,
985                   .fifo_num = 3,
986                   },
987 #if 0
988         .ep[4] = {
989                   .ep = {
990                          .name = "ep4out-bulk",
991                          .ops = &s3c_ep_ops,
992                          .maxpacket = EP_FIFO_SIZE,
993                          },
994                   .dev = &memory,
995
996                   .bEndpointAddress = USB_DIR_OUT | 4,
997                   .bmAttributes = USB_ENDPOINT_XFER_BULK,
998
999                   .ep_type = ep_bulk_out,
1000                   .fifo_num = 4,
1001                   },
1002         .ep[5] = {
1003                   .ep = {
1004                          .name = "ep5in-bulk",
1005                          .ops = &s3c_ep_ops,
1006                          .maxpacket = EP_FIFO_SIZE,
1007                          },
1008                   .dev = &memory,
1009
1010                   .bEndpointAddress = USB_DIR_IN | 5,
1011                   .bmAttributes = USB_ENDPOINT_XFER_BULK,
1012
1013                   .ep_type = ep_bulk_in,
1014                   .fifo_num = 5,
1015                   },
1016         .ep[6] = {
1017                   .ep = {
1018                          .name = "ep6in-int",
1019                          .ops = &s3c_ep_ops,
1020                          .maxpacket = EP_FIFO_SIZE,
1021                          },
1022                   .dev = &memory,
1023
1024                   .bEndpointAddress = USB_DIR_IN | 6,
1025                   .bmAttributes = USB_ENDPOINT_XFER_INT,
1026
1027                   .ep_type = ep_interrupt,
1028                   .fifo_num = 6,
1029                   },
1030         .ep[7] = {
1031                   .ep = {
1032                          .name = "ep7out-bulk",
1033                          .ops = &s3c_ep_ops,
1034                          .maxpacket = EP_FIFO_SIZE,
1035                          },
1036                   .dev = &memory,
1037
1038                   .bEndpointAddress = USB_DIR_OUT | 7,
1039                   .bmAttributes = USB_ENDPOINT_XFER_BULK,
1040
1041                   .ep_type = ep_bulk_out,
1042                   .fifo_num = 7,
1043                   },
1044         .ep[8] = {
1045                   .ep = {
1046                          .name = "ep8in-bulk",
1047                          .ops = &s3c_ep_ops,
1048                          .maxpacket = EP_FIFO_SIZE,
1049                          },
1050                   .dev = &memory,
1051
1052                   .bEndpointAddress = USB_DIR_IN | 8,
1053                   .bmAttributes = USB_ENDPOINT_XFER_BULK,
1054
1055                   .ep_type = ep_bulk_in,
1056                   .fifo_num = 8,
1057                   },
1058         .ep[9] = {
1059                   .ep = {
1060                          .name = "ep9in-int",
1061                          .ops = &s3c_ep_ops,
1062                          .maxpacket = EP_FIFO_SIZE,
1063                          },
1064                   .dev = &memory,
1065
1066                   .bEndpointAddress = USB_DIR_IN | 9,
1067                   .bmAttributes = USB_ENDPOINT_XFER_INT,
1068
1069                   .ep_type = ep_interrupt,
1070                   .fifo_num = 9,
1071                   },
1072         .ep[10] = {
1073                   .ep = {
1074                          .name = "ep10out-bulk",
1075                          .ops = &s3c_ep_ops,
1076                          .maxpacket = EP_FIFO_SIZE,
1077                          },
1078                   .dev = &memory,
1079
1080                   .bEndpointAddress = USB_DIR_OUT | 10,
1081                   .bmAttributes = USB_ENDPOINT_XFER_BULK,
1082
1083                   .ep_type = ep_bulk_out,
1084                   .fifo_num = 10,
1085                   },
1086         .ep[11] = {
1087                   .ep = {
1088                          .name = "ep11in-bulk",
1089                          .ops = &s3c_ep_ops,
1090                          .maxpacket = EP_FIFO_SIZE,
1091                          },
1092                   .dev = &memory,
1093
1094                   .bEndpointAddress = USB_DIR_IN | 11,
1095                   .bmAttributes = USB_ENDPOINT_XFER_BULK,
1096
1097                   .ep_type = ep_bulk_in,
1098                   .fifo_num = 11,
1099                   },
1100         .ep[12] = {
1101                   .ep = {
1102                          .name = "ep12in-int",
1103                          .ops = &s3c_ep_ops,
1104                          .maxpacket = EP_FIFO_SIZE,
1105                          },
1106                   .dev = &memory,
1107
1108                   .bEndpointAddress = USB_DIR_IN | 12,
1109                   .bmAttributes = USB_ENDPOINT_XFER_INT,
1110
1111                   .ep_type = ep_interrupt,
1112                   .fifo_num = 12,
1113                   },
1114         .ep[13] = {
1115                   .ep = {
1116                          .name = "ep13out-bulk",
1117                          .ops = &s3c_ep_ops,
1118                          .maxpacket = EP_FIFO_SIZE,
1119                          },
1120                   .dev = &memory,
1121
1122                   .bEndpointAddress = USB_DIR_OUT | 13,
1123                   .bmAttributes = USB_ENDPOINT_XFER_BULK,
1124
1125                   .ep_type = ep_bulk_out,
1126                   .fifo_num = 13,
1127                   },
1128         .ep[14] = {
1129                   .ep = {
1130                          .name = "ep14in-bulk",
1131                          .ops = &s3c_ep_ops,
1132                          .maxpacket = EP_FIFO_SIZE,
1133                          },
1134                   .dev = &memory,
1135
1136                   .bEndpointAddress = USB_DIR_IN | 14,
1137                   .bmAttributes = USB_ENDPOINT_XFER_BULK,
1138
1139                   .ep_type = ep_bulk_in,
1140                   .fifo_num = 14,
1141                   },
1142         .ep[15] = {
1143                   .ep = {
1144                          .name = "ep15in-int",
1145                          .ops = &s3c_ep_ops,
1146                          .maxpacket = EP_FIFO_SIZE,
1147                          },
1148                   .dev = &memory,
1149
1150                   .bEndpointAddress = USB_DIR_IN | 15,
1151                   .bmAttributes = USB_ENDPOINT_XFER_INT,
1152
1153                   .ep_type = ep_interrupt,
1154                   .fifo_num = 15,
1155                   },
1156 #endif
1157 };
1158
1159 /*
1160  *      probe - binds to the platform device
1161  */
1162
1163 int s3c_udc_probe(struct s3c_plat_otg_data *pdata)
1164 {
1165         struct s3c_udc *dev = &memory;
1166         int retval=0, i;
1167
1168         DEBUG("%s: %p\n", __FUNCTION__, pdata);
1169
1170         dev->pdata = pdata;
1171
1172         regs_phy = (void *)pdata->regs_phy;
1173         regs_otg = (void*)pdata->regs_otg;
1174
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;
1181
1182         the_controller = dev;
1183
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];
1187         }
1188         usb_ctrl = dev->dma_buf[0];
1189         usb_ctrl_dma_addr = dev->dma_addr[0];
1190
1191         udc_reinit(dev);
1192
1193         return retval;
1194 }
1195 #if 0
1196 static int s3c_udc_remove(struct platform_device *pdev)
1197 {
1198         the_controller = 0;
1199         return 0;
1200 }
1201 #endif
1202 int usb_gadget_handle_interrupts()
1203 {
1204         u32 intr_status = readl(S3C_UDC_OTG_GINTSTS);
1205         u32 gintmsk = readl(S3C_UDC_OTG_GINTMSK);
1206
1207         if (intr_status & gintmsk)
1208                 return s3c_udc_irq(1, (void*)the_controller);
1209         return 0;
1210 }