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