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