blob: b9816dfe30a55b495b3d715328eccd6c3f56ccf0 [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
Lukasz Majewskif2ec4e32014-08-25 11:07:29 +0200152bool dfu_usb_get_reset(void)
153{
154 return !!(readl(&reg->gintsts) & INT_RESET);
155}
156
Lukasz Majewski38517a72011-10-27 10:36:46 +0200157void otg_phy_init(struct s3c_udc *dev)
158{
159 dev->pdata->phy_control(1);
160
161 /*USB PHY0 Enable */
162 printf("USB PHY0 Enable\n");
163
164 /* Enable PHY */
165 writel(readl(usb_phy_ctrl) | USB_PHY_CTRL_EN0, usb_phy_ctrl);
166
167 if (dev->pdata->usb_flags == PHY0_SLEEP) /* C210 Universal */
168 writel((readl(&phy->phypwr)
169 &~(PHY_0_SLEEP | OTG_DISABLE_0 | ANALOG_PWRDOWN)
170 &~FORCE_SUSPEND_0), &phy->phypwr);
171 else /* C110 GONI */
172 writel((readl(&phy->phypwr) &~(OTG_DISABLE_0 | ANALOG_PWRDOWN)
173 &~FORCE_SUSPEND_0), &phy->phypwr);
174
Piotr Wilczek4498cf22013-11-21 15:46:44 +0100175 if (s5p_cpu_id == 0x4412)
176 writel((readl(&phy->phyclk) & ~(EXYNOS4X12_ID_PULLUP0 |
177 EXYNOS4X12_COMMON_ON_N0)) | EXYNOS4X12_CLK_SEL_24MHZ,
178 &phy->phyclk); /* PLL 24Mhz */
179 else
180 writel((readl(&phy->phyclk) & ~(ID_PULLUP0 | COMMON_ON_N0)) |
181 CLK_SEL_24MHZ, &phy->phyclk); /* PLL 24Mhz */
Lukasz Majewski38517a72011-10-27 10:36:46 +0200182
183 writel((readl(&phy->rstcon) &~(LINK_SW_RST | PHYLNK_SW_RST))
184 | PHY_SW_RST0, &phy->rstcon);
185 udelay(10);
186 writel(readl(&phy->rstcon)
187 &~(PHY_SW_RST0 | LINK_SW_RST | PHYLNK_SW_RST), &phy->rstcon);
188 udelay(10);
189}
190
191void otg_phy_off(struct s3c_udc *dev)
192{
193 /* reset controller just in case */
194 writel(PHY_SW_RST0, &phy->rstcon);
195 udelay(20);
196 writel(readl(&phy->phypwr) &~PHY_SW_RST0, &phy->rstcon);
197 udelay(20);
198
199 writel(readl(&phy->phypwr) | OTG_DISABLE_0 | ANALOG_PWRDOWN
200 | FORCE_SUSPEND_0, &phy->phypwr);
201
202 writel(readl(usb_phy_ctrl) &~USB_PHY_CTRL_EN0, usb_phy_ctrl);
203
204 writel((readl(&phy->phyclk) & ~(ID_PULLUP0 | COMMON_ON_N0)),
205 &phy->phyclk);
206
207 udelay(10000);
208
209 dev->pdata->phy_control(0);
210}
211
212/***********************************************************/
213
214#include "s3c_udc_otg_xfer_dma.c"
215
216/*
217 * udc_disable - disable USB device controller
218 */
219static void udc_disable(struct s3c_udc *dev)
220{
Lukasz Majewskif3b05ca2012-05-02 13:11:35 +0200221 debug_cond(DEBUG_SETUP != 0, "%s: %p\n", __func__, dev);
Lukasz Majewski38517a72011-10-27 10:36:46 +0200222
223 udc_set_address(dev, 0);
224
225 dev->ep0state = WAIT_FOR_SETUP;
226 dev->gadget.speed = USB_SPEED_UNKNOWN;
227 dev->usb_address = 0;
228
229 otg_phy_off(dev);
230}
231
232/*
233 * udc_reinit - initialize software state
234 */
235static void udc_reinit(struct s3c_udc *dev)
236{
237 unsigned int i;
238
Lukasz Majewskif3b05ca2012-05-02 13:11:35 +0200239 debug_cond(DEBUG_SETUP != 0, "%s: %p\n", __func__, dev);
Lukasz Majewski38517a72011-10-27 10:36:46 +0200240
241 /* device/ep0 records init */
242 INIT_LIST_HEAD(&dev->gadget.ep_list);
243 INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
244 dev->ep0state = WAIT_FOR_SETUP;
245
246 /* basic endpoint records init */
247 for (i = 0; i < S3C_MAX_ENDPOINTS; i++) {
248 struct s3c_ep *ep = &dev->ep[i];
249
250 if (i != 0)
251 list_add_tail(&ep->ep.ep_list, &dev->gadget.ep_list);
252
253 ep->desc = 0;
254 ep->stopped = 0;
255 INIT_LIST_HEAD(&ep->queue);
256 ep->pio_irqs = 0;
257 }
258
259 /* the rest was statically initialized, and is read-only */
260}
261
262#define BYTES2MAXP(x) (x / 8)
263#define MAXP2BYTES(x) (x * 8)
264
265/* until it's enabled, this UDC should be completely invisible
266 * to any USB host.
267 */
268static int udc_enable(struct s3c_udc *dev)
269{
Lukasz Majewskif3b05ca2012-05-02 13:11:35 +0200270 debug_cond(DEBUG_SETUP != 0, "%s: %p\n", __func__, dev);
Lukasz Majewski38517a72011-10-27 10:36:46 +0200271
272 otg_phy_init(dev);
273 reconfig_usbd();
274
Lukasz Majewskif3b05ca2012-05-02 13:11:35 +0200275 debug_cond(DEBUG_SETUP != 0,
276 "S3C USB 2.0 OTG Controller Core Initialized : 0x%x\n",
Lukasz Majewski38517a72011-10-27 10:36:46 +0200277 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;
Bo Shen06fa91c2014-08-27 17:28:18 +0800291 unsigned long flags = 0;
Lukasz Majewski38517a72011-10-27 10:36:46 +0200292
Lukasz Majewskif3b05ca2012-05-02 13:11:35 +0200293 debug_cond(DEBUG_SETUP != 0, "%s: %s\n", __func__, "no name");
Lukasz Majewski38517a72011-10-27 10:36:46 +0200294
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) {
Lukasz Majewskif3b05ca2012-05-02 13:11:35 +0200317 debug_cond(DEBUG_SETUP != 0,
318 "%s: bind to driver --> error %d\n",
Lukasz Majewski38517a72011-10-27 10:36:46 +0200319 dev->gadget.name, retval);
320 dev->driver = 0;
321 return retval;
322 }
323
324 enable_irq(IRQ_OTG);
325
Lukasz Majewskif3b05ca2012-05-02 13:11:35 +0200326 debug_cond(DEBUG_SETUP != 0,
327 "Registered gadget driver %s\n", dev->gadget.name);
Lukasz Majewski38517a72011-10-27 10:36:46 +0200328 udc_enable(dev);
329
330 return 0;
331}
332
333/*
334 * Unregister entry point for the peripheral controller driver.
335 */
336int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
337{
338 struct s3c_udc *dev = the_controller;
Bo Shen06fa91c2014-08-27 17:28:18 +0800339 unsigned long flags = 0;
Lukasz Majewski38517a72011-10-27 10:36:46 +0200340
341 if (!dev)
342 return -ENODEV;
343 if (!driver || driver != dev->driver)
344 return -EINVAL;
345
346 spin_lock_irqsave(&dev->lock, flags);
347 dev->driver = 0;
348 stop_activity(dev, driver);
349 spin_unlock_irqrestore(&dev->lock, flags);
350
351 driver->unbind(&dev->gadget);
352
353 disable_irq(IRQ_OTG);
354
355 udc_disable(dev);
356 return 0;
357}
358
359/*
360 * done - retire a request; caller blocked irqs
361 */
362static void done(struct s3c_ep *ep, struct s3c_request *req, int status)
363{
364 unsigned int stopped = ep->stopped;
365
Anatolij Gustschinea2d9152011-12-19 04:20:35 +0000366 debug("%s: %s %p, req = %p, stopped = %d\n",
Lukasz Majewski38517a72011-10-27 10:36:46 +0200367 __func__, ep->ep.name, ep, &req->req, stopped);
368
369 list_del_init(&req->queue);
370
371 if (likely(req->req.status == -EINPROGRESS))
372 req->req.status = status;
373 else
374 status = req->req.status;
375
376 if (status && status != -ESHUTDOWN) {
Anatolij Gustschinea2d9152011-12-19 04:20:35 +0000377 debug("complete %s req %p stat %d len %u/%u\n",
Lukasz Majewski38517a72011-10-27 10:36:46 +0200378 ep->ep.name, &req->req, status,
379 req->req.actual, req->req.length);
380 }
381
382 /* don't modify queue heads during completion callback */
383 ep->stopped = 1;
384
Lukasz Majewskif3b05ca2012-05-02 13:11:35 +0200385#ifdef DEBUG
Lukasz Majewski38517a72011-10-27 10:36:46 +0200386 printf("calling complete callback\n");
387 {
388 int i, len = req->req.length;
389
390 printf("pkt[%d] = ", req->req.length);
391 if (len > 64)
392 len = 64;
393 for (i = 0; i < len; i++) {
394 printf("%02x", ((u8 *)req->req.buf)[i]);
395 if ((i & 7) == 7)
396 printf(" ");
397 }
398 printf("\n");
399 }
400#endif
401 spin_unlock(&ep->dev->lock);
402 req->req.complete(&ep->ep, &req->req);
403 spin_lock(&ep->dev->lock);
404
Anatolij Gustschinea2d9152011-12-19 04:20:35 +0000405 debug("callback completed\n");
Lukasz Majewski38517a72011-10-27 10:36:46 +0200406
407 ep->stopped = stopped;
408}
409
410/*
411 * nuke - dequeue ALL requests
412 */
413static void nuke(struct s3c_ep *ep, int status)
414{
415 struct s3c_request *req;
416
Anatolij Gustschinea2d9152011-12-19 04:20:35 +0000417 debug("%s: %s %p\n", __func__, ep->ep.name, ep);
Lukasz Majewski38517a72011-10-27 10:36:46 +0200418
419 /* called with irqs blocked */
420 while (!list_empty(&ep->queue)) {
421 req = list_entry(ep->queue.next, struct s3c_request, queue);
422 done(ep, req, status);
423 }
424}
425
426static void stop_activity(struct s3c_udc *dev,
427 struct usb_gadget_driver *driver)
428{
429 int i;
430
431 /* don't disconnect drivers more than once */
432 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
433 driver = 0;
434 dev->gadget.speed = USB_SPEED_UNKNOWN;
435
436 /* prevent new request submissions, kill any outstanding requests */
437 for (i = 0; i < S3C_MAX_ENDPOINTS; i++) {
438 struct s3c_ep *ep = &dev->ep[i];
439 ep->stopped = 1;
440 nuke(ep, -ESHUTDOWN);
441 }
442
443 /* report disconnect; the driver is already quiesced */
444 if (driver) {
445 spin_unlock(&dev->lock);
446 driver->disconnect(&dev->gadget);
447 spin_lock(&dev->lock);
448 }
449
450 /* re-init driver-visible data structures */
451 udc_reinit(dev);
452}
453
454static void reconfig_usbd(void)
455{
456 /* 2. Soft-reset OTG Core and then unreset again. */
457 int i;
458 unsigned int uTemp = writel(CORE_SOFT_RESET, &reg->grstctl);
459
Anatolij Gustschinea2d9152011-12-19 04:20:35 +0000460 debug("Reseting OTG controller\n");
Lukasz Majewski38517a72011-10-27 10:36:46 +0200461
462 writel(0<<15 /* PHY Low Power Clock sel*/
463 |1<<14 /* Non-Periodic TxFIFO Rewind Enable*/
464 |0x5<<10 /* Turnaround time*/
465 |0<<9 | 0<<8 /* [0:HNP disable,1:HNP enable][ 0:SRP disable*/
466 /* 1:SRP enable] H1= 1,1*/
467 |0<<7 /* Ulpi DDR sel*/
468 |0<<6 /* 0: high speed utmi+, 1: full speed serial*/
469 |0<<4 /* 0: utmi+, 1:ulpi*/
470 |1<<3 /* phy i/f 0:8bit, 1:16bit*/
471 |0x7<<0, /* HS/FS Timeout**/
472 &reg->gusbcfg);
473
474 /* 3. Put the OTG device core in the disconnected state.*/
475 uTemp = readl(&reg->dctl);
476 uTemp |= SOFT_DISCONNECT;
477 writel(uTemp, &reg->dctl);
478
479 udelay(20);
480
481 /* 4. Make the OTG device core exit from the disconnected state.*/
482 uTemp = readl(&reg->dctl);
483 uTemp = uTemp & ~SOFT_DISCONNECT;
484 writel(uTemp, &reg->dctl);
485
486 /* 5. Configure OTG Core to initial settings of device mode.*/
487 /* [][1: full speed(30Mhz) 0:high speed]*/
488 writel(EP_MISS_CNT(1) | DEV_SPEED_HIGH_SPEED_20, &reg->dcfg);
489
490 mdelay(1);
491
492 /* 6. Unmask the core interrupts*/
493 writel(GINTMSK_INIT, &reg->gintmsk);
494
495 /* 7. Set NAK bit of EP0, EP1, EP2*/
496 writel(DEPCTL_EPDIS|DEPCTL_SNAK, &reg->out_endp[EP0_CON].doepctl);
497 writel(DEPCTL_EPDIS|DEPCTL_SNAK, &reg->in_endp[EP0_CON].diepctl);
498
499 for (i = 1; i < S3C_MAX_ENDPOINTS; i++) {
500 writel(DEPCTL_EPDIS|DEPCTL_SNAK, &reg->out_endp[i].doepctl);
501 writel(DEPCTL_EPDIS|DEPCTL_SNAK, &reg->in_endp[i].diepctl);
502 }
503
504 /* 8. Unmask EPO interrupts*/
505 writel(((1 << EP0_CON) << DAINT_OUT_BIT)
506 | (1 << EP0_CON), &reg->daintmsk);
507
508 /* 9. Unmask device OUT EP common interrupts*/
509 writel(DOEPMSK_INIT, &reg->doepmsk);
510
511 /* 10. Unmask device IN EP common interrupts*/
512 writel(DIEPMSK_INIT, &reg->diepmsk);
513
514 /* 11. Set Rx FIFO Size (in 32-bit words) */
515 writel(RX_FIFO_SIZE >> 2, &reg->grxfsiz);
516
517 /* 12. Set Non Periodic Tx FIFO Size */
518 writel((NPTX_FIFO_SIZE >> 2) << 16 | ((RX_FIFO_SIZE >> 2)) << 0,
519 &reg->gnptxfsiz);
520
521 for (i = 1; i < S3C_MAX_HW_ENDPOINTS; i++)
522 writel((PTX_FIFO_SIZE >> 2) << 16 |
523 ((RX_FIFO_SIZE + NPTX_FIFO_SIZE +
524 PTX_FIFO_SIZE*(i-1)) >> 2) << 0,
525 &reg->dieptxf[i-1]);
526
527 /* Flush the RX FIFO */
528 writel(RX_FIFO_FLUSH, &reg->grstctl);
529 while (readl(&reg->grstctl) & RX_FIFO_FLUSH)
Anatolij Gustschinea2d9152011-12-19 04:20:35 +0000530 debug("%s: waiting for S3C_UDC_OTG_GRSTCTL\n", __func__);
Lukasz Majewski38517a72011-10-27 10:36:46 +0200531
532 /* Flush all the Tx FIFO's */
533 writel(TX_FIFO_FLUSH_ALL, &reg->grstctl);
534 writel(TX_FIFO_FLUSH_ALL | TX_FIFO_FLUSH, &reg->grstctl);
535 while (readl(&reg->grstctl) & TX_FIFO_FLUSH)
Anatolij Gustschinea2d9152011-12-19 04:20:35 +0000536 debug("%s: waiting for S3C_UDC_OTG_GRSTCTL\n", __func__);
Lukasz Majewski38517a72011-10-27 10:36:46 +0200537
538 /* 13. Clear NAK bit of EP0, EP1, EP2*/
539 /* For Slave mode*/
540 /* EP0: Control OUT */
541 writel(DEPCTL_EPDIS | DEPCTL_CNAK,
542 &reg->out_endp[EP0_CON].doepctl);
543
544 /* 14. Initialize OTG Link Core.*/
545 writel(GAHBCFG_INIT, &reg->gahbcfg);
546}
547
548static void set_max_pktsize(struct s3c_udc *dev, enum usb_device_speed speed)
549{
550 unsigned int ep_ctrl;
551 int i;
552
553 if (speed == USB_SPEED_HIGH) {
554 ep0_fifo_size = 64;
555 ep_fifo_size = 512;
556 ep_fifo_size2 = 1024;
557 dev->gadget.speed = USB_SPEED_HIGH;
558 } else {
559 ep0_fifo_size = 64;
560 ep_fifo_size = 64;
561 ep_fifo_size2 = 64;
562 dev->gadget.speed = USB_SPEED_FULL;
563 }
564
565 dev->ep[0].ep.maxpacket = ep0_fifo_size;
566 for (i = 1; i < S3C_MAX_ENDPOINTS; i++)
567 dev->ep[i].ep.maxpacket = ep_fifo_size;
568
569 /* EP0 - Control IN (64 bytes)*/
570 ep_ctrl = readl(&reg->in_endp[EP0_CON].diepctl);
571 writel(ep_ctrl|(0<<0), &reg->in_endp[EP0_CON].diepctl);
572
573 /* EP0 - Control OUT (64 bytes)*/
574 ep_ctrl = readl(&reg->out_endp[EP0_CON].doepctl);
575 writel(ep_ctrl|(0<<0), &reg->out_endp[EP0_CON].doepctl);
576}
577
578static int s3c_ep_enable(struct usb_ep *_ep,
579 const struct usb_endpoint_descriptor *desc)
580{
581 struct s3c_ep *ep;
582 struct s3c_udc *dev;
Bo Shen06fa91c2014-08-27 17:28:18 +0800583 unsigned long flags = 0;
Lukasz Majewski38517a72011-10-27 10:36:46 +0200584
Anatolij Gustschinea2d9152011-12-19 04:20:35 +0000585 debug("%s: %p\n", __func__, _ep);
Lukasz Majewski38517a72011-10-27 10:36:46 +0200586
587 ep = container_of(_ep, struct s3c_ep, ep);
588 if (!_ep || !desc || ep->desc || _ep->name == ep0name
589 || desc->bDescriptorType != USB_DT_ENDPOINT
590 || ep->bEndpointAddress != desc->bEndpointAddress
Tom Rinib2fb47f2011-12-15 08:40:51 -0700591 || ep_maxpacket(ep) <
592 le16_to_cpu(get_unaligned(&desc->wMaxPacketSize))) {
Lukasz Majewski38517a72011-10-27 10:36:46 +0200593
Anatolij Gustschinea2d9152011-12-19 04:20:35 +0000594 debug("%s: bad ep or descriptor\n", __func__);
Lukasz Majewski38517a72011-10-27 10:36:46 +0200595 return -EINVAL;
596 }
597
598 /* xfer types must match, except that interrupt ~= bulk */
599 if (ep->bmAttributes != desc->bmAttributes
600 && ep->bmAttributes != USB_ENDPOINT_XFER_BULK
601 && desc->bmAttributes != USB_ENDPOINT_XFER_INT) {
602
Anatolij Gustschinea2d9152011-12-19 04:20:35 +0000603 debug("%s: %s type mismatch\n", __func__, _ep->name);
Lukasz Majewski38517a72011-10-27 10:36:46 +0200604 return -EINVAL;
605 }
606
607 /* hardware _could_ do smaller, but driver doesn't */
608 if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
Tom Rinib2fb47f2011-12-15 08:40:51 -0700609 && le16_to_cpu(get_unaligned(&desc->wMaxPacketSize)) !=
610 ep_maxpacket(ep)) || !get_unaligned(&desc->wMaxPacketSize)) {
Lukasz Majewski38517a72011-10-27 10:36:46 +0200611
Anatolij Gustschinea2d9152011-12-19 04:20:35 +0000612 debug("%s: bad %s maxpacket\n", __func__, _ep->name);
Lukasz Majewski38517a72011-10-27 10:36:46 +0200613 return -ERANGE;
614 }
615
616 dev = ep->dev;
617 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
618
Anatolij Gustschinea2d9152011-12-19 04:20:35 +0000619 debug("%s: bogus device state\n", __func__);
Lukasz Majewski38517a72011-10-27 10:36:46 +0200620 return -ESHUTDOWN;
621 }
622
623 ep->stopped = 0;
624 ep->desc = desc;
625 ep->pio_irqs = 0;
Tom Rinib2fb47f2011-12-15 08:40:51 -0700626 ep->ep.maxpacket = le16_to_cpu(get_unaligned(&desc->wMaxPacketSize));
Lukasz Majewski38517a72011-10-27 10:36:46 +0200627
628 /* Reset halt state */
629 s3c_udc_set_nak(ep);
630 s3c_udc_set_halt(_ep, 0);
631
632 spin_lock_irqsave(&ep->dev->lock, flags);
633 s3c_udc_ep_activate(ep);
634 spin_unlock_irqrestore(&ep->dev->lock, flags);
635
Anatolij Gustschinea2d9152011-12-19 04:20:35 +0000636 debug("%s: enabled %s, stopped = %d, maxpacket = %d\n",
Lukasz Majewski38517a72011-10-27 10:36:46 +0200637 __func__, _ep->name, ep->stopped, ep->ep.maxpacket);
638 return 0;
639}
640
641/*
642 * Disable EP
643 */
644static int s3c_ep_disable(struct usb_ep *_ep)
645{
646 struct s3c_ep *ep;
Bo Shen06fa91c2014-08-27 17:28:18 +0800647 unsigned long flags = 0;
Lukasz Majewski38517a72011-10-27 10:36:46 +0200648
Anatolij Gustschinea2d9152011-12-19 04:20:35 +0000649 debug("%s: %p\n", __func__, _ep);
Lukasz Majewski38517a72011-10-27 10:36:46 +0200650
651 ep = container_of(_ep, struct s3c_ep, ep);
652 if (!_ep || !ep->desc) {
Anatolij Gustschinea2d9152011-12-19 04:20:35 +0000653 debug("%s: %s not enabled\n", __func__,
Lukasz Majewski38517a72011-10-27 10:36:46 +0200654 _ep ? ep->ep.name : NULL);
655 return -EINVAL;
656 }
657
658 spin_lock_irqsave(&ep->dev->lock, flags);
659
660 /* Nuke all pending requests */
661 nuke(ep, -ESHUTDOWN);
662
663 ep->desc = 0;
664 ep->stopped = 1;
665
666 spin_unlock_irqrestore(&ep->dev->lock, flags);
667
Anatolij Gustschinea2d9152011-12-19 04:20:35 +0000668 debug("%s: disabled %s\n", __func__, _ep->name);
Lukasz Majewski38517a72011-10-27 10:36:46 +0200669 return 0;
670}
671
672static struct usb_request *s3c_alloc_request(struct usb_ep *ep,
673 gfp_t gfp_flags)
674{
675 struct s3c_request *req;
676
Anatolij Gustschinea2d9152011-12-19 04:20:35 +0000677 debug("%s: %s %p\n", __func__, ep->name, ep);
Lukasz Majewski38517a72011-10-27 10:36:46 +0200678
Mike Frysinger6777a3c2012-04-26 02:34:44 +0000679 req = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*req));
Lukasz Majewski38517a72011-10-27 10:36:46 +0200680 if (!req)
681 return 0;
682
683 memset(req, 0, sizeof *req);
684 INIT_LIST_HEAD(&req->queue);
685
686 return &req->req;
687}
688
689static void s3c_free_request(struct usb_ep *ep, struct usb_request *_req)
690{
691 struct s3c_request *req;
692
Anatolij Gustschinea2d9152011-12-19 04:20:35 +0000693 debug("%s: %p\n", __func__, ep);
Lukasz Majewski38517a72011-10-27 10:36:46 +0200694
695 req = container_of(_req, struct s3c_request, req);
696 WARN_ON(!list_empty(&req->queue));
697 kfree(req);
698}
699
700/* dequeue JUST ONE request */
701static int s3c_dequeue(struct usb_ep *_ep, struct usb_request *_req)
702{
703 struct s3c_ep *ep;
704 struct s3c_request *req;
Bo Shen06fa91c2014-08-27 17:28:18 +0800705 unsigned long flags = 0;
Lukasz Majewski38517a72011-10-27 10:36:46 +0200706
Anatolij Gustschinea2d9152011-12-19 04:20:35 +0000707 debug("%s: %p\n", __func__, _ep);
Lukasz Majewski38517a72011-10-27 10:36:46 +0200708
709 ep = container_of(_ep, struct s3c_ep, ep);
710 if (!_ep || ep->ep.name == ep0name)
711 return -EINVAL;
712
713 spin_lock_irqsave(&ep->dev->lock, flags);
714
715 /* make sure it's actually queued on this endpoint */
716 list_for_each_entry(req, &ep->queue, queue) {
717 if (&req->req == _req)
718 break;
719 }
720 if (&req->req != _req) {
721 spin_unlock_irqrestore(&ep->dev->lock, flags);
722 return -EINVAL;
723 }
724
725 done(ep, req, -ECONNRESET);
726
727 spin_unlock_irqrestore(&ep->dev->lock, flags);
728 return 0;
729}
730
731/*
732 * Return bytes in EP FIFO
733 */
734static int s3c_fifo_status(struct usb_ep *_ep)
735{
736 int count = 0;
737 struct s3c_ep *ep;
738
739 ep = container_of(_ep, struct s3c_ep, ep);
740 if (!_ep) {
Anatolij Gustschinea2d9152011-12-19 04:20:35 +0000741 debug("%s: bad ep\n", __func__);
Lukasz Majewski38517a72011-10-27 10:36:46 +0200742 return -ENODEV;
743 }
744
Anatolij Gustschinea2d9152011-12-19 04:20:35 +0000745 debug("%s: %d\n", __func__, ep_index(ep));
Lukasz Majewski38517a72011-10-27 10:36:46 +0200746
747 /* LPD can't report unclaimed bytes from IN fifos */
748 if (ep_is_in(ep))
749 return -EOPNOTSUPP;
750
751 return count;
752}
753
754/*
755 * Flush EP FIFO
756 */
757static void s3c_fifo_flush(struct usb_ep *_ep)
758{
759 struct s3c_ep *ep;
760
761 ep = container_of(_ep, struct s3c_ep, ep);
762 if (unlikely(!_ep || (!ep->desc && ep->ep.name != ep0name))) {
Anatolij Gustschinea2d9152011-12-19 04:20:35 +0000763 debug("%s: bad ep\n", __func__);
Lukasz Majewski38517a72011-10-27 10:36:46 +0200764 return;
765 }
766
Anatolij Gustschinea2d9152011-12-19 04:20:35 +0000767 debug("%s: %d\n", __func__, ep_index(ep));
Lukasz Majewski38517a72011-10-27 10:36:46 +0200768}
769
770static const struct usb_gadget_ops s3c_udc_ops = {
771 /* current versions must always be self-powered */
772};
773
774static struct s3c_udc memory = {
775 .usb_address = 0,
776 .gadget = {
777 .ops = &s3c_udc_ops,
778 .ep0 = &memory.ep[0].ep,
779 .name = driver_name,
780 },
781
782 /* control endpoint */
783 .ep[0] = {
784 .ep = {
785 .name = ep0name,
786 .ops = &s3c_ep_ops,
787 .maxpacket = EP0_FIFO_SIZE,
788 },
789 .dev = &memory,
790
791 .bEndpointAddress = 0,
792 .bmAttributes = 0,
793
794 .ep_type = ep_control,
795 },
796
797 /* first group of endpoints */
798 .ep[1] = {
799 .ep = {
800 .name = "ep1in-bulk",
801 .ops = &s3c_ep_ops,
802 .maxpacket = EP_FIFO_SIZE,
803 },
804 .dev = &memory,
805
806 .bEndpointAddress = USB_DIR_IN | 1,
807 .bmAttributes = USB_ENDPOINT_XFER_BULK,
808
809 .ep_type = ep_bulk_out,
810 .fifo_num = 1,
811 },
812
813 .ep[2] = {
814 .ep = {
815 .name = "ep2out-bulk",
816 .ops = &s3c_ep_ops,
817 .maxpacket = EP_FIFO_SIZE,
818 },
819 .dev = &memory,
820
821 .bEndpointAddress = USB_DIR_OUT | 2,
822 .bmAttributes = USB_ENDPOINT_XFER_BULK,
823
824 .ep_type = ep_bulk_in,
825 .fifo_num = 2,
826 },
827
828 .ep[3] = {
829 .ep = {
830 .name = "ep3in-int",
831 .ops = &s3c_ep_ops,
832 .maxpacket = EP_FIFO_SIZE,
833 },
834 .dev = &memory,
835
836 .bEndpointAddress = USB_DIR_IN | 3,
837 .bmAttributes = USB_ENDPOINT_XFER_INT,
838
839 .ep_type = ep_interrupt,
840 .fifo_num = 3,
841 },
842};
843
844/*
845 * probe - binds to the platform device
846 */
847
848int s3c_udc_probe(struct s3c_plat_otg_data *pdata)
849{
850 struct s3c_udc *dev = &memory;
Lukasz Majewskie0059ea2014-02-05 10:10:44 +0100851 int retval = 0;
Lukasz Majewski38517a72011-10-27 10:36:46 +0200852
Anatolij Gustschinea2d9152011-12-19 04:20:35 +0000853 debug("%s: %p\n", __func__, pdata);
Lukasz Majewski38517a72011-10-27 10:36:46 +0200854
855 dev->pdata = pdata;
856
857 phy = (struct s3c_usbotg_phy *)pdata->regs_phy;
858 reg = (struct s3c_usbotg_reg *)pdata->regs_otg;
859 usb_phy_ctrl = pdata->usb_phy_ctrl;
860
861 /* regs_otg = (void *)pdata->regs_otg; */
862
863 dev->gadget.is_dualspeed = 1; /* Hack only*/
864 dev->gadget.is_otg = 0;
865 dev->gadget.is_a_peripheral = 0;
866 dev->gadget.b_hnp_enable = 0;
867 dev->gadget.a_hnp_support = 0;
868 dev->gadget.a_alt_hnp_support = 0;
869
870 the_controller = dev;
871
Lukasz Majewskie0059ea2014-02-05 10:10:44 +0100872 usb_ctrl = memalign(CONFIG_SYS_CACHELINE_SIZE,
873 ROUND(sizeof(struct usb_ctrlrequest),
874 CONFIG_SYS_CACHELINE_SIZE));
875 if (!usb_ctrl) {
876 error("No memory available for UDC!\n");
877 return -ENOMEM;
Lukasz Majewski38517a72011-10-27 10:36:46 +0200878 }
Lukasz Majewskie0059ea2014-02-05 10:10:44 +0100879
880 usb_ctrl_dma_addr = (dma_addr_t) usb_ctrl;
Lukasz Majewski38517a72011-10-27 10:36:46 +0200881
882 udc_reinit(dev);
883
884 return retval;
885}
886
887int usb_gadget_handle_interrupts()
888{
889 u32 intr_status = readl(&reg->gintsts);
890 u32 gintmsk = readl(&reg->gintmsk);
891
892 if (intr_status & gintmsk)
893 return s3c_udc_irq(1, (void *)the_controller);
894 return 0;
895}