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