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