2 * linux/drivers/usb/gadget/s3c2410_udc.c
4 * Samsung S3C24xx series on-chip full speed USB device controllers
6 * Copyright (C) 2004-2007 Herbert Pötzl - Arnaud Patard
7 * Additional cleanups by Ben Dooks <ben-linux@fluff.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
15 #define pr_fmt(fmt) "s3c2410_udc: " fmt
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/delay.h>
20 #include <linux/ioport.h>
21 #include <linux/sched.h>
22 #include <linux/slab.h>
23 #include <linux/errno.h>
24 #include <linux/init.h>
25 #include <linux/timer.h>
26 #include <linux/list.h>
27 #include <linux/interrupt.h>
28 #include <linux/platform_device.h>
29 #include <linux/clk.h>
30 #include <linux/gpio.h>
31 #include <linux/prefetch.h>
34 #include <linux/debugfs.h>
35 #include <linux/seq_file.h>
37 #include <linux/usb.h>
38 #include <linux/usb/gadget.h>
40 #include <asm/byteorder.h>
42 #include <asm/unaligned.h>
43 #include <mach/irqs.h>
45 #include <mach/hardware.h>
47 #include <plat/regs-udc.h>
48 #include <linux/platform_data/usb-s3c2410_udc.h>
51 #include "s3c2410_udc.h"
53 #define DRIVER_DESC "S3C2410 USB Device Controller Gadget"
54 #define DRIVER_VERSION "29 Apr 2007"
55 #define DRIVER_AUTHOR "Herbert Pötzl <herbert@13thfloor.at>, " \
56 "Arnaud Patard <arnaud.patard@rtp-net.org>"
58 static const char gadget_name[] = "s3c2410_udc";
59 static const char driver_desc[] = DRIVER_DESC;
61 static struct s3c2410_udc *the_controller;
62 static struct clk *udc_clock;
63 static struct clk *usb_bus_clock;
64 static void __iomem *base_addr;
65 static u64 rsrc_start;
67 static struct dentry *s3c2410_udc_debugfs_root;
69 static inline u32 udc_read(u32 reg)
71 return readb(base_addr + reg);
74 static inline void udc_write(u32 value, u32 reg)
76 writeb(value, base_addr + reg);
79 static inline void udc_writeb(void __iomem *base, u32 value, u32 reg)
81 writeb(value, base + reg);
84 static struct s3c2410_udc_mach_info *udc_info;
86 /*************************** DEBUG FUNCTION ***************************/
87 #define DEBUG_NORMAL 1
88 #define DEBUG_VERBOSE 2
90 #ifdef CONFIG_USB_S3C2410_DEBUG
91 #define USB_S3C2410_DEBUG_LEVEL 0
93 static uint32_t s3c2410_ticks = 0;
95 static int dprintk(int level, const char *fmt, ...)
97 static char printk_buf[1024];
98 static long prevticks;
99 static int invocation;
103 if (level > USB_S3C2410_DEBUG_LEVEL)
106 if (s3c2410_ticks != prevticks) {
107 prevticks = s3c2410_ticks;
111 len = scnprintf(printk_buf,
112 sizeof(printk_buf), "%1lu.%02d USB: ",
113 prevticks, invocation++);
116 len = vscnprintf(printk_buf+len,
117 sizeof(printk_buf)-len, fmt, args);
120 pr_debug("%s", printk_buf);
124 static int dprintk(int level, const char *fmt, ...)
129 static int s3c2410_udc_debugfs_seq_show(struct seq_file *m, void *p)
131 u32 addr_reg, pwr_reg, ep_int_reg, usb_int_reg;
132 u32 ep_int_en_reg, usb_int_en_reg, ep0_csr;
133 u32 ep1_i_csr1, ep1_i_csr2, ep1_o_csr1, ep1_o_csr2;
134 u32 ep2_i_csr1, ep2_i_csr2, ep2_o_csr1, ep2_o_csr2;
136 addr_reg = udc_read(S3C2410_UDC_FUNC_ADDR_REG);
137 pwr_reg = udc_read(S3C2410_UDC_PWR_REG);
138 ep_int_reg = udc_read(S3C2410_UDC_EP_INT_REG);
139 usb_int_reg = udc_read(S3C2410_UDC_USB_INT_REG);
140 ep_int_en_reg = udc_read(S3C2410_UDC_EP_INT_EN_REG);
141 usb_int_en_reg = udc_read(S3C2410_UDC_USB_INT_EN_REG);
142 udc_write(0, S3C2410_UDC_INDEX_REG);
143 ep0_csr = udc_read(S3C2410_UDC_IN_CSR1_REG);
144 udc_write(1, S3C2410_UDC_INDEX_REG);
145 ep1_i_csr1 = udc_read(S3C2410_UDC_IN_CSR1_REG);
146 ep1_i_csr2 = udc_read(S3C2410_UDC_IN_CSR2_REG);
147 ep1_o_csr1 = udc_read(S3C2410_UDC_IN_CSR1_REG);
148 ep1_o_csr2 = udc_read(S3C2410_UDC_IN_CSR2_REG);
149 udc_write(2, S3C2410_UDC_INDEX_REG);
150 ep2_i_csr1 = udc_read(S3C2410_UDC_IN_CSR1_REG);
151 ep2_i_csr2 = udc_read(S3C2410_UDC_IN_CSR2_REG);
152 ep2_o_csr1 = udc_read(S3C2410_UDC_IN_CSR1_REG);
153 ep2_o_csr2 = udc_read(S3C2410_UDC_IN_CSR2_REG);
155 seq_printf(m, "FUNC_ADDR_REG : 0x%04X\n"
157 "EP_INT_REG : 0x%04X\n"
158 "USB_INT_REG : 0x%04X\n"
159 "EP_INT_EN_REG : 0x%04X\n"
160 "USB_INT_EN_REG : 0x%04X\n"
162 "EP1_I_CSR1 : 0x%04X\n"
163 "EP1_I_CSR2 : 0x%04X\n"
164 "EP1_O_CSR1 : 0x%04X\n"
165 "EP1_O_CSR2 : 0x%04X\n"
166 "EP2_I_CSR1 : 0x%04X\n"
167 "EP2_I_CSR2 : 0x%04X\n"
168 "EP2_O_CSR1 : 0x%04X\n"
169 "EP2_O_CSR2 : 0x%04X\n",
170 addr_reg, pwr_reg, ep_int_reg, usb_int_reg,
171 ep_int_en_reg, usb_int_en_reg, ep0_csr,
172 ep1_i_csr1, ep1_i_csr2, ep1_o_csr1, ep1_o_csr2,
173 ep2_i_csr1, ep2_i_csr2, ep2_o_csr1, ep2_o_csr2
179 static int s3c2410_udc_debugfs_fops_open(struct inode *inode,
182 return single_open(file, s3c2410_udc_debugfs_seq_show, NULL);
185 static const struct file_operations s3c2410_udc_debugfs_fops = {
186 .open = s3c2410_udc_debugfs_fops_open,
189 .release = single_release,
190 .owner = THIS_MODULE,
195 static inline void s3c2410_udc_clear_ep0_opr(void __iomem *base)
197 udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
198 udc_writeb(base, S3C2410_UDC_EP0_CSR_SOPKTRDY,
199 S3C2410_UDC_EP0_CSR_REG);
202 static inline void s3c2410_udc_clear_ep0_sst(void __iomem *base)
204 udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
205 writeb(0x00, base + S3C2410_UDC_EP0_CSR_REG);
208 static inline void s3c2410_udc_clear_ep0_se(void __iomem *base)
210 udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
211 udc_writeb(base, S3C2410_UDC_EP0_CSR_SSE, S3C2410_UDC_EP0_CSR_REG);
214 static inline void s3c2410_udc_set_ep0_ipr(void __iomem *base)
216 udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
217 udc_writeb(base, S3C2410_UDC_EP0_CSR_IPKRDY, S3C2410_UDC_EP0_CSR_REG);
220 static inline void s3c2410_udc_set_ep0_de(void __iomem *base)
222 udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
223 udc_writeb(base, S3C2410_UDC_EP0_CSR_DE, S3C2410_UDC_EP0_CSR_REG);
226 inline void s3c2410_udc_set_ep0_ss(void __iomem *b)
228 udc_writeb(b, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
229 udc_writeb(b, S3C2410_UDC_EP0_CSR_SENDSTL, S3C2410_UDC_EP0_CSR_REG);
232 static inline void s3c2410_udc_set_ep0_de_out(void __iomem *base)
234 udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
236 udc_writeb(base, (S3C2410_UDC_EP0_CSR_SOPKTRDY
237 | S3C2410_UDC_EP0_CSR_DE),
238 S3C2410_UDC_EP0_CSR_REG);
241 static inline void s3c2410_udc_set_ep0_sse_out(void __iomem *base)
243 udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
244 udc_writeb(base, (S3C2410_UDC_EP0_CSR_SOPKTRDY
245 | S3C2410_UDC_EP0_CSR_SSE),
246 S3C2410_UDC_EP0_CSR_REG);
249 static inline void s3c2410_udc_set_ep0_de_in(void __iomem *base)
251 udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
252 udc_writeb(base, (S3C2410_UDC_EP0_CSR_IPKRDY
253 | S3C2410_UDC_EP0_CSR_DE),
254 S3C2410_UDC_EP0_CSR_REG);
257 /*------------------------- I/O ----------------------------------*/
262 static void s3c2410_udc_done(struct s3c2410_ep *ep,
263 struct s3c2410_request *req, int status)
265 unsigned halted = ep->halted;
267 list_del_init(&req->queue);
269 if (likely(req->req.status == -EINPROGRESS))
270 req->req.status = status;
272 status = req->req.status;
275 usb_gadget_giveback_request(&ep->ep, &req->req);
279 static void s3c2410_udc_nuke(struct s3c2410_udc *udc,
280 struct s3c2410_ep *ep, int status)
283 if (&ep->queue == NULL)
286 while (!list_empty(&ep->queue)) {
287 struct s3c2410_request *req;
288 req = list_entry(ep->queue.next, struct s3c2410_request,
290 s3c2410_udc_done(ep, req, status);
294 static inline void s3c2410_udc_clear_ep_state(struct s3c2410_udc *dev)
298 /* hardware SET_{CONFIGURATION,INTERFACE} automagic resets endpoint
299 * fifos, and pending transactions mustn't be continued in any case.
302 for (i = 1; i < S3C2410_ENDPOINTS; i++)
303 s3c2410_udc_nuke(dev, &dev->ep[i], -ECONNABORTED);
306 static inline int s3c2410_udc_fifo_count_out(void)
310 tmp = udc_read(S3C2410_UDC_OUT_FIFO_CNT2_REG) << 8;
311 tmp |= udc_read(S3C2410_UDC_OUT_FIFO_CNT1_REG);
316 * s3c2410_udc_write_packet
318 static inline int s3c2410_udc_write_packet(int fifo,
319 struct s3c2410_request *req,
322 unsigned len = min(req->req.length - req->req.actual, max);
323 u8 *buf = req->req.buf + req->req.actual;
327 dprintk(DEBUG_VERBOSE, "%s %d %d %d %d\n", __func__,
328 req->req.actual, req->req.length, len, req->req.actual + len);
330 req->req.actual += len;
333 writesb(base_addr + fifo, buf, len);
338 * s3c2410_udc_write_fifo
340 * return: 0 = still running, 1 = completed, negative = errno
342 static int s3c2410_udc_write_fifo(struct s3c2410_ep *ep,
343 struct s3c2410_request *req)
351 idx = ep->bEndpointAddress & 0x7F;
356 fifo_reg = S3C2410_UDC_EP0_FIFO_REG;
359 fifo_reg = S3C2410_UDC_EP1_FIFO_REG;
362 fifo_reg = S3C2410_UDC_EP2_FIFO_REG;
365 fifo_reg = S3C2410_UDC_EP3_FIFO_REG;
368 fifo_reg = S3C2410_UDC_EP4_FIFO_REG;
372 count = s3c2410_udc_write_packet(fifo_reg, req, ep->ep.maxpacket);
374 /* last packet is often short (sometimes a zlp) */
375 if (count != ep->ep.maxpacket)
377 else if (req->req.length != req->req.actual || req->req.zero)
382 /* Only ep0 debug messages are interesting */
384 dprintk(DEBUG_NORMAL,
385 "Written ep%d %d.%d of %d b [last %d,z %d]\n",
386 idx, count, req->req.actual, req->req.length,
387 is_last, req->req.zero);
390 /* The order is important. It prevents sending 2 packets
391 * at the same time */
394 /* Reset signal => no need to say 'data sent' */
395 if (!(udc_read(S3C2410_UDC_USB_INT_REG)
396 & S3C2410_UDC_USBINT_RESET))
397 s3c2410_udc_set_ep0_de_in(base_addr);
398 ep->dev->ep0state = EP0_IDLE;
400 udc_write(idx, S3C2410_UDC_INDEX_REG);
401 ep_csr = udc_read(S3C2410_UDC_IN_CSR1_REG);
402 udc_write(idx, S3C2410_UDC_INDEX_REG);
403 udc_write(ep_csr | S3C2410_UDC_ICSR1_PKTRDY,
404 S3C2410_UDC_IN_CSR1_REG);
407 s3c2410_udc_done(ep, req, 0);
411 /* Reset signal => no need to say 'data sent' */
412 if (!(udc_read(S3C2410_UDC_USB_INT_REG)
413 & S3C2410_UDC_USBINT_RESET))
414 s3c2410_udc_set_ep0_ipr(base_addr);
416 udc_write(idx, S3C2410_UDC_INDEX_REG);
417 ep_csr = udc_read(S3C2410_UDC_IN_CSR1_REG);
418 udc_write(idx, S3C2410_UDC_INDEX_REG);
419 udc_write(ep_csr | S3C2410_UDC_ICSR1_PKTRDY,
420 S3C2410_UDC_IN_CSR1_REG);
427 static inline int s3c2410_udc_read_packet(int fifo, u8 *buf,
428 struct s3c2410_request *req, unsigned avail)
432 len = min(req->req.length - req->req.actual, avail);
433 req->req.actual += len;
435 readsb(fifo + base_addr, buf, len);
440 * return: 0 = still running, 1 = queue empty, negative = errno
442 static int s3c2410_udc_read_fifo(struct s3c2410_ep *ep,
443 struct s3c2410_request *req)
447 unsigned bufferspace;
454 idx = ep->bEndpointAddress & 0x7F;
460 fifo_reg = S3C2410_UDC_EP0_FIFO_REG;
463 fifo_reg = S3C2410_UDC_EP1_FIFO_REG;
466 fifo_reg = S3C2410_UDC_EP2_FIFO_REG;
469 fifo_reg = S3C2410_UDC_EP3_FIFO_REG;
472 fifo_reg = S3C2410_UDC_EP4_FIFO_REG;
476 if (!req->req.length)
479 buf = req->req.buf + req->req.actual;
480 bufferspace = req->req.length - req->req.actual;
482 dprintk(DEBUG_NORMAL, "%s: buffer full!\n", __func__);
486 udc_write(idx, S3C2410_UDC_INDEX_REG);
488 fifo_count = s3c2410_udc_fifo_count_out();
489 dprintk(DEBUG_NORMAL, "%s fifo count : %d\n", __func__, fifo_count);
491 if (fifo_count > ep->ep.maxpacket)
492 avail = ep->ep.maxpacket;
496 fifo_count = s3c2410_udc_read_packet(fifo_reg, buf, req, avail);
498 /* checking this with ep0 is not accurate as we already
499 * read a control request
501 if (idx != 0 && fifo_count < ep->ep.maxpacket) {
503 /* overflowed this request? flush extra data */
504 if (fifo_count != avail)
505 req->req.status = -EOVERFLOW;
507 is_last = (req->req.length <= req->req.actual) ? 1 : 0;
510 udc_write(idx, S3C2410_UDC_INDEX_REG);
511 fifo_count = s3c2410_udc_fifo_count_out();
513 /* Only ep0 debug messages are interesting */
515 dprintk(DEBUG_VERBOSE, "%s fifo count : %d [last %d]\n",
516 __func__, fifo_count, is_last);
520 s3c2410_udc_set_ep0_de_out(base_addr);
521 ep->dev->ep0state = EP0_IDLE;
523 udc_write(idx, S3C2410_UDC_INDEX_REG);
524 ep_csr = udc_read(S3C2410_UDC_OUT_CSR1_REG);
525 udc_write(idx, S3C2410_UDC_INDEX_REG);
526 udc_write(ep_csr & ~S3C2410_UDC_OCSR1_PKTRDY,
527 S3C2410_UDC_OUT_CSR1_REG);
530 s3c2410_udc_done(ep, req, 0);
533 s3c2410_udc_clear_ep0_opr(base_addr);
535 udc_write(idx, S3C2410_UDC_INDEX_REG);
536 ep_csr = udc_read(S3C2410_UDC_OUT_CSR1_REG);
537 udc_write(idx, S3C2410_UDC_INDEX_REG);
538 udc_write(ep_csr & ~S3C2410_UDC_OCSR1_PKTRDY,
539 S3C2410_UDC_OUT_CSR1_REG);
546 static int s3c2410_udc_read_fifo_crq(struct usb_ctrlrequest *crq)
548 unsigned char *outbuf = (unsigned char *)crq;
551 udc_write(0, S3C2410_UDC_INDEX_REG);
553 bytes_read = s3c2410_udc_fifo_count_out();
555 dprintk(DEBUG_NORMAL, "%s: fifo_count=%d\n", __func__, bytes_read);
557 if (bytes_read > sizeof(struct usb_ctrlrequest))
558 bytes_read = sizeof(struct usb_ctrlrequest);
560 readsb(S3C2410_UDC_EP0_FIFO_REG + base_addr, outbuf, bytes_read);
562 dprintk(DEBUG_VERBOSE, "%s: len=%d %02x:%02x {%x,%x,%x}\n", __func__,
563 bytes_read, crq->bRequest, crq->bRequestType,
564 crq->wValue, crq->wIndex, crq->wLength);
569 static int s3c2410_udc_get_status(struct s3c2410_udc *dev,
570 struct usb_ctrlrequest *crq)
573 u8 ep_num = crq->wIndex & 0x7F;
574 u8 is_in = crq->wIndex & USB_DIR_IN;
576 switch (crq->bRequestType & USB_RECIP_MASK) {
577 case USB_RECIP_INTERFACE:
580 case USB_RECIP_DEVICE:
581 status = dev->devstatus;
584 case USB_RECIP_ENDPOINT:
585 if (ep_num > 4 || crq->wLength > 2)
589 udc_write(0, S3C2410_UDC_INDEX_REG);
590 status = udc_read(S3C2410_UDC_IN_CSR1_REG);
591 status = status & S3C2410_UDC_EP0_CSR_SENDSTL;
593 udc_write(ep_num, S3C2410_UDC_INDEX_REG);
595 status = udc_read(S3C2410_UDC_IN_CSR1_REG);
596 status = status & S3C2410_UDC_ICSR1_SENDSTL;
598 status = udc_read(S3C2410_UDC_OUT_CSR1_REG);
599 status = status & S3C2410_UDC_OCSR1_SENDSTL;
603 status = status ? 1 : 0;
610 /* Seems to be needed to get it working. ouch :( */
612 udc_write(status & 0xFF, S3C2410_UDC_EP0_FIFO_REG);
613 udc_write(status >> 8, S3C2410_UDC_EP0_FIFO_REG);
614 s3c2410_udc_set_ep0_de_in(base_addr);
618 /*------------------------- usb state machine -------------------------------*/
619 static int s3c2410_udc_set_halt(struct usb_ep *_ep, int value);
621 static void s3c2410_udc_handle_ep0_idle(struct s3c2410_udc *dev,
622 struct s3c2410_ep *ep,
623 struct usb_ctrlrequest *crq,
628 /* start control request? */
629 if (!(ep0csr & S3C2410_UDC_EP0_CSR_OPKRDY))
632 s3c2410_udc_nuke(dev, ep, -EPROTO);
634 len = s3c2410_udc_read_fifo_crq(crq);
635 if (len != sizeof(*crq)) {
636 dprintk(DEBUG_NORMAL, "setup begin: fifo READ ERROR"
637 " wanted %d bytes got %d. Stalling out...\n",
639 s3c2410_udc_set_ep0_ss(base_addr);
643 dprintk(DEBUG_NORMAL, "bRequest = %d bRequestType %d wLength = %d\n",
644 crq->bRequest, crq->bRequestType, crq->wLength);
646 /* cope with automagic for some standard requests. */
647 dev->req_std = (crq->bRequestType & USB_TYPE_MASK)
648 == USB_TYPE_STANDARD;
650 dev->req_pending = 1;
652 switch (crq->bRequest) {
653 case USB_REQ_SET_CONFIGURATION:
654 dprintk(DEBUG_NORMAL, "USB_REQ_SET_CONFIGURATION ...\n");
656 if (crq->bRequestType == USB_RECIP_DEVICE) {
658 s3c2410_udc_set_ep0_de_out(base_addr);
662 case USB_REQ_SET_INTERFACE:
663 dprintk(DEBUG_NORMAL, "USB_REQ_SET_INTERFACE ...\n");
665 if (crq->bRequestType == USB_RECIP_INTERFACE) {
667 s3c2410_udc_set_ep0_de_out(base_addr);
671 case USB_REQ_SET_ADDRESS:
672 dprintk(DEBUG_NORMAL, "USB_REQ_SET_ADDRESS ...\n");
674 if (crq->bRequestType == USB_RECIP_DEVICE) {
675 tmp = crq->wValue & 0x7F;
677 udc_write((tmp | S3C2410_UDC_FUNCADDR_UPDATE),
678 S3C2410_UDC_FUNC_ADDR_REG);
679 s3c2410_udc_set_ep0_de_out(base_addr);
684 case USB_REQ_GET_STATUS:
685 dprintk(DEBUG_NORMAL, "USB_REQ_GET_STATUS ...\n");
686 s3c2410_udc_clear_ep0_opr(base_addr);
689 if (!s3c2410_udc_get_status(dev, crq))
694 case USB_REQ_CLEAR_FEATURE:
695 s3c2410_udc_clear_ep0_opr(base_addr);
697 if (crq->bRequestType != USB_RECIP_ENDPOINT)
700 if (crq->wValue != USB_ENDPOINT_HALT || crq->wLength != 0)
703 s3c2410_udc_set_halt(&dev->ep[crq->wIndex & 0x7f].ep, 0);
704 s3c2410_udc_set_ep0_de_out(base_addr);
707 case USB_REQ_SET_FEATURE:
708 s3c2410_udc_clear_ep0_opr(base_addr);
710 if (crq->bRequestType != USB_RECIP_ENDPOINT)
713 if (crq->wValue != USB_ENDPOINT_HALT || crq->wLength != 0)
716 s3c2410_udc_set_halt(&dev->ep[crq->wIndex & 0x7f].ep, 1);
717 s3c2410_udc_set_ep0_de_out(base_addr);
721 s3c2410_udc_clear_ep0_opr(base_addr);
725 if (crq->bRequestType & USB_DIR_IN)
726 dev->ep0state = EP0_IN_DATA_PHASE;
728 dev->ep0state = EP0_OUT_DATA_PHASE;
733 /* deliver the request to the gadget driver */
734 ret = dev->driver->setup(&dev->gadget, crq);
736 if (dev->req_config) {
737 dprintk(DEBUG_NORMAL, "config change %02x fail %d?\n",
742 if (ret == -EOPNOTSUPP)
743 dprintk(DEBUG_NORMAL, "Operation not supported\n");
745 dprintk(DEBUG_NORMAL,
746 "dev->driver->setup failed. (%d)\n", ret);
749 s3c2410_udc_set_ep0_ss(base_addr);
750 s3c2410_udc_set_ep0_de_out(base_addr);
751 dev->ep0state = EP0_IDLE;
752 /* deferred i/o == no response yet */
753 } else if (dev->req_pending) {
754 dprintk(DEBUG_VERBOSE, "dev->req_pending... what now?\n");
755 dev->req_pending = 0;
758 dprintk(DEBUG_VERBOSE, "ep0state %s\n", ep0states[dev->ep0state]);
761 static void s3c2410_udc_handle_ep0(struct s3c2410_udc *dev)
764 struct s3c2410_ep *ep = &dev->ep[0];
765 struct s3c2410_request *req;
766 struct usb_ctrlrequest crq;
768 if (list_empty(&ep->queue))
771 req = list_entry(ep->queue.next, struct s3c2410_request, queue);
773 /* We make the assumption that S3C2410_UDC_IN_CSR1_REG equal to
774 * S3C2410_UDC_EP0_CSR_REG when index is zero */
776 udc_write(0, S3C2410_UDC_INDEX_REG);
777 ep0csr = udc_read(S3C2410_UDC_IN_CSR1_REG);
779 dprintk(DEBUG_NORMAL, "ep0csr %x ep0state %s\n",
780 ep0csr, ep0states[dev->ep0state]);
782 /* clear stall status */
783 if (ep0csr & S3C2410_UDC_EP0_CSR_SENTSTL) {
784 s3c2410_udc_nuke(dev, ep, -EPIPE);
785 dprintk(DEBUG_NORMAL, "... clear SENT_STALL ...\n");
786 s3c2410_udc_clear_ep0_sst(base_addr);
787 dev->ep0state = EP0_IDLE;
791 /* clear setup end */
792 if (ep0csr & S3C2410_UDC_EP0_CSR_SE) {
793 dprintk(DEBUG_NORMAL, "... serviced SETUP_END ...\n");
794 s3c2410_udc_nuke(dev, ep, 0);
795 s3c2410_udc_clear_ep0_se(base_addr);
796 dev->ep0state = EP0_IDLE;
799 switch (dev->ep0state) {
801 s3c2410_udc_handle_ep0_idle(dev, ep, &crq, ep0csr);
804 case EP0_IN_DATA_PHASE: /* GET_DESCRIPTOR etc */
805 dprintk(DEBUG_NORMAL, "EP0_IN_DATA_PHASE ... what now?\n");
806 if (!(ep0csr & S3C2410_UDC_EP0_CSR_IPKRDY) && req)
807 s3c2410_udc_write_fifo(ep, req);
810 case EP0_OUT_DATA_PHASE: /* SET_DESCRIPTOR etc */
811 dprintk(DEBUG_NORMAL, "EP0_OUT_DATA_PHASE ... what now?\n");
812 if ((ep0csr & S3C2410_UDC_EP0_CSR_OPKRDY) && req)
813 s3c2410_udc_read_fifo(ep, req);
817 dprintk(DEBUG_NORMAL, "EP0_END_XFER ... what now?\n");
818 dev->ep0state = EP0_IDLE;
822 dprintk(DEBUG_NORMAL, "EP0_STALL ... what now?\n");
823 dev->ep0state = EP0_IDLE;
829 * handle_ep - Manage I/O endpoints
832 static void s3c2410_udc_handle_ep(struct s3c2410_ep *ep)
834 struct s3c2410_request *req;
835 int is_in = ep->bEndpointAddress & USB_DIR_IN;
839 if (likely(!list_empty(&ep->queue)))
840 req = list_entry(ep->queue.next,
841 struct s3c2410_request, queue);
845 idx = ep->bEndpointAddress & 0x7F;
848 udc_write(idx, S3C2410_UDC_INDEX_REG);
849 ep_csr1 = udc_read(S3C2410_UDC_IN_CSR1_REG);
850 dprintk(DEBUG_VERBOSE, "ep%01d write csr:%02x %d\n",
851 idx, ep_csr1, req ? 1 : 0);
853 if (ep_csr1 & S3C2410_UDC_ICSR1_SENTSTL) {
854 dprintk(DEBUG_VERBOSE, "st\n");
855 udc_write(idx, S3C2410_UDC_INDEX_REG);
856 udc_write(ep_csr1 & ~S3C2410_UDC_ICSR1_SENTSTL,
857 S3C2410_UDC_IN_CSR1_REG);
861 if (!(ep_csr1 & S3C2410_UDC_ICSR1_PKTRDY) && req)
862 s3c2410_udc_write_fifo(ep, req);
864 udc_write(idx, S3C2410_UDC_INDEX_REG);
865 ep_csr1 = udc_read(S3C2410_UDC_OUT_CSR1_REG);
866 dprintk(DEBUG_VERBOSE, "ep%01d rd csr:%02x\n", idx, ep_csr1);
868 if (ep_csr1 & S3C2410_UDC_OCSR1_SENTSTL) {
869 udc_write(idx, S3C2410_UDC_INDEX_REG);
870 udc_write(ep_csr1 & ~S3C2410_UDC_OCSR1_SENTSTL,
871 S3C2410_UDC_OUT_CSR1_REG);
875 if ((ep_csr1 & S3C2410_UDC_OCSR1_PKTRDY) && req)
876 s3c2410_udc_read_fifo(ep, req);
880 #include <mach/regs-irq.h>
883 * s3c2410_udc_irq - interrupt handler
885 static irqreturn_t s3c2410_udc_irq(int dummy, void *_dev)
887 struct s3c2410_udc *dev = _dev;
896 spin_lock_irqsave(&dev->lock, flags);
898 /* Driver connected ? */
900 /* Clear interrupts */
901 udc_write(udc_read(S3C2410_UDC_USB_INT_REG),
902 S3C2410_UDC_USB_INT_REG);
903 udc_write(udc_read(S3C2410_UDC_EP_INT_REG),
904 S3C2410_UDC_EP_INT_REG);
908 idx = udc_read(S3C2410_UDC_INDEX_REG);
910 /* Read status registers */
911 usb_status = udc_read(S3C2410_UDC_USB_INT_REG);
912 usbd_status = udc_read(S3C2410_UDC_EP_INT_REG);
913 pwr_reg = udc_read(S3C2410_UDC_PWR_REG);
915 udc_writeb(base_addr, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
916 ep0csr = udc_read(S3C2410_UDC_IN_CSR1_REG);
918 dprintk(DEBUG_NORMAL, "usbs=%02x, usbds=%02x, pwr=%02x ep0csr=%02x\n",
919 usb_status, usbd_status, pwr_reg, ep0csr);
922 * Now, handle interrupts. There's two types :
923 * - Reset, Resume, Suspend coming -> usb_int_reg
928 if (usb_status & S3C2410_UDC_USBINT_RESET) {
929 /* two kind of reset :
930 * - reset start -> pwr reg = 8
931 * - reset end -> pwr reg = 0
933 dprintk(DEBUG_NORMAL, "USB reset csr %x pwr %x\n",
936 dev->gadget.speed = USB_SPEED_UNKNOWN;
937 udc_write(0x00, S3C2410_UDC_INDEX_REG);
938 udc_write((dev->ep[0].ep.maxpacket & 0x7ff) >> 3,
939 S3C2410_UDC_MAXP_REG);
942 dev->ep0state = EP0_IDLE;
943 dev->gadget.speed = USB_SPEED_FULL;
945 /* clear interrupt */
946 udc_write(S3C2410_UDC_USBINT_RESET,
947 S3C2410_UDC_USB_INT_REG);
949 udc_write(idx, S3C2410_UDC_INDEX_REG);
950 spin_unlock_irqrestore(&dev->lock, flags);
955 if (usb_status & S3C2410_UDC_USBINT_RESUME) {
956 dprintk(DEBUG_NORMAL, "USB resume\n");
958 /* clear interrupt */
959 udc_write(S3C2410_UDC_USBINT_RESUME,
960 S3C2410_UDC_USB_INT_REG);
962 if (dev->gadget.speed != USB_SPEED_UNKNOWN
964 && dev->driver->resume)
965 dev->driver->resume(&dev->gadget);
969 if (usb_status & S3C2410_UDC_USBINT_SUSPEND) {
970 dprintk(DEBUG_NORMAL, "USB suspend\n");
972 /* clear interrupt */
973 udc_write(S3C2410_UDC_USBINT_SUSPEND,
974 S3C2410_UDC_USB_INT_REG);
976 if (dev->gadget.speed != USB_SPEED_UNKNOWN
978 && dev->driver->suspend)
979 dev->driver->suspend(&dev->gadget);
981 dev->ep0state = EP0_IDLE;
985 /* control traffic */
986 /* check on ep0csr != 0 is not a good idea as clearing in_pkt_ready
987 * generate an interrupt
989 if (usbd_status & S3C2410_UDC_INT_EP0) {
990 dprintk(DEBUG_VERBOSE, "USB ep0 irq\n");
991 /* Clear the interrupt bit by setting it to 1 */
992 udc_write(S3C2410_UDC_INT_EP0, S3C2410_UDC_EP_INT_REG);
993 s3c2410_udc_handle_ep0(dev);
996 /* endpoint data transfers */
997 for (i = 1; i < S3C2410_ENDPOINTS; i++) {
999 if (usbd_status & tmp) {
1000 dprintk(DEBUG_VERBOSE, "USB ep%d irq\n", i);
1002 /* Clear the interrupt bit by setting it to 1 */
1003 udc_write(tmp, S3C2410_UDC_EP_INT_REG);
1004 s3c2410_udc_handle_ep(&dev->ep[i]);
1008 /* what else causes this interrupt? a receive! who is it? */
1009 if (!usb_status && !usbd_status && !pwr_reg && !ep0csr) {
1010 for (i = 1; i < S3C2410_ENDPOINTS; i++) {
1011 idx2 = udc_read(S3C2410_UDC_INDEX_REG);
1012 udc_write(i, S3C2410_UDC_INDEX_REG);
1014 if (udc_read(S3C2410_UDC_OUT_CSR1_REG) & 0x1)
1015 s3c2410_udc_handle_ep(&dev->ep[i]);
1018 udc_write(idx2, S3C2410_UDC_INDEX_REG);
1022 dprintk(DEBUG_VERBOSE, "irq: %d s3c2410_udc_done.\n", IRQ_USBD);
1024 /* Restore old index */
1025 udc_write(idx, S3C2410_UDC_INDEX_REG);
1027 spin_unlock_irqrestore(&dev->lock, flags);
1031 /*------------------------- s3c2410_ep_ops ----------------------------------*/
1033 static inline struct s3c2410_ep *to_s3c2410_ep(struct usb_ep *ep)
1035 return container_of(ep, struct s3c2410_ep, ep);
1038 static inline struct s3c2410_udc *to_s3c2410_udc(struct usb_gadget *gadget)
1040 return container_of(gadget, struct s3c2410_udc, gadget);
1043 static inline struct s3c2410_request *to_s3c2410_req(struct usb_request *req)
1045 return container_of(req, struct s3c2410_request, req);
1049 * s3c2410_udc_ep_enable
1051 static int s3c2410_udc_ep_enable(struct usb_ep *_ep,
1052 const struct usb_endpoint_descriptor *desc)
1054 struct s3c2410_udc *dev;
1055 struct s3c2410_ep *ep;
1057 unsigned long flags;
1061 ep = to_s3c2410_ep(_ep);
1064 || _ep->name == ep0name
1065 || desc->bDescriptorType != USB_DT_ENDPOINT)
1069 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
1072 max = usb_endpoint_maxp(desc) & 0x1fff;
1074 local_irq_save(flags);
1075 _ep->maxpacket = max & 0x7ff;
1078 ep->bEndpointAddress = desc->bEndpointAddress;
1080 /* set max packet */
1081 udc_write(ep->num, S3C2410_UDC_INDEX_REG);
1082 udc_write(max >> 3, S3C2410_UDC_MAXP_REG);
1084 /* set type, direction, address; reset fifo counters */
1085 if (desc->bEndpointAddress & USB_DIR_IN) {
1086 csr1 = S3C2410_UDC_ICSR1_FFLUSH|S3C2410_UDC_ICSR1_CLRDT;
1087 csr2 = S3C2410_UDC_ICSR2_MODEIN|S3C2410_UDC_ICSR2_DMAIEN;
1089 udc_write(ep->num, S3C2410_UDC_INDEX_REG);
1090 udc_write(csr1, S3C2410_UDC_IN_CSR1_REG);
1091 udc_write(ep->num, S3C2410_UDC_INDEX_REG);
1092 udc_write(csr2, S3C2410_UDC_IN_CSR2_REG);
1094 /* don't flush in fifo or it will cause endpoint interrupt */
1095 csr1 = S3C2410_UDC_ICSR1_CLRDT;
1096 csr2 = S3C2410_UDC_ICSR2_DMAIEN;
1098 udc_write(ep->num, S3C2410_UDC_INDEX_REG);
1099 udc_write(csr1, S3C2410_UDC_IN_CSR1_REG);
1100 udc_write(ep->num, S3C2410_UDC_INDEX_REG);
1101 udc_write(csr2, S3C2410_UDC_IN_CSR2_REG);
1103 csr1 = S3C2410_UDC_OCSR1_FFLUSH | S3C2410_UDC_OCSR1_CLRDT;
1104 csr2 = S3C2410_UDC_OCSR2_DMAIEN;
1106 udc_write(ep->num, S3C2410_UDC_INDEX_REG);
1107 udc_write(csr1, S3C2410_UDC_OUT_CSR1_REG);
1108 udc_write(ep->num, S3C2410_UDC_INDEX_REG);
1109 udc_write(csr2, S3C2410_UDC_OUT_CSR2_REG);
1113 int_en_reg = udc_read(S3C2410_UDC_EP_INT_EN_REG);
1114 udc_write(int_en_reg | (1 << ep->num), S3C2410_UDC_EP_INT_EN_REG);
1116 /* print some debug message */
1117 tmp = desc->bEndpointAddress;
1118 dprintk(DEBUG_NORMAL, "enable %s(%d) ep%x%s-blk max %02x\n",
1119 _ep->name, ep->num, tmp,
1120 desc->bEndpointAddress & USB_DIR_IN ? "in" : "out", max);
1122 local_irq_restore(flags);
1123 s3c2410_udc_set_halt(_ep, 0);
1129 * s3c2410_udc_ep_disable
1131 static int s3c2410_udc_ep_disable(struct usb_ep *_ep)
1133 struct s3c2410_ep *ep = to_s3c2410_ep(_ep);
1134 unsigned long flags;
1137 if (!_ep || !ep->ep.desc) {
1138 dprintk(DEBUG_NORMAL, "%s not enabled\n",
1139 _ep ? ep->ep.name : NULL);
1143 local_irq_save(flags);
1145 dprintk(DEBUG_NORMAL, "ep_disable: %s\n", _ep->name);
1150 s3c2410_udc_nuke(ep->dev, ep, -ESHUTDOWN);
1153 int_en_reg = udc_read(S3C2410_UDC_EP_INT_EN_REG);
1154 udc_write(int_en_reg & ~(1<<ep->num), S3C2410_UDC_EP_INT_EN_REG);
1156 local_irq_restore(flags);
1158 dprintk(DEBUG_NORMAL, "%s disabled\n", _ep->name);
1164 * s3c2410_udc_alloc_request
1166 static struct usb_request *
1167 s3c2410_udc_alloc_request(struct usb_ep *_ep, gfp_t mem_flags)
1169 struct s3c2410_request *req;
1171 dprintk(DEBUG_VERBOSE, "%s(%p,%d)\n", __func__, _ep, mem_flags);
1176 req = kzalloc(sizeof(struct s3c2410_request), mem_flags);
1180 INIT_LIST_HEAD(&req->queue);
1185 * s3c2410_udc_free_request
1188 s3c2410_udc_free_request(struct usb_ep *_ep, struct usb_request *_req)
1190 struct s3c2410_ep *ep = to_s3c2410_ep(_ep);
1191 struct s3c2410_request *req = to_s3c2410_req(_req);
1193 dprintk(DEBUG_VERBOSE, "%s(%p,%p)\n", __func__, _ep, _req);
1195 if (!ep || !_req || (!ep->ep.desc && _ep->name != ep0name))
1198 WARN_ON(!list_empty(&req->queue));
1205 static int s3c2410_udc_queue(struct usb_ep *_ep, struct usb_request *_req,
1208 struct s3c2410_request *req = to_s3c2410_req(_req);
1209 struct s3c2410_ep *ep = to_s3c2410_ep(_ep);
1210 struct s3c2410_udc *dev;
1213 unsigned long flags;
1215 if (unlikely(!_ep || (!ep->ep.desc && ep->ep.name != ep0name))) {
1216 dprintk(DEBUG_NORMAL, "%s: invalid args\n", __func__);
1221 if (unlikely(!dev->driver
1222 || dev->gadget.speed == USB_SPEED_UNKNOWN)) {
1226 local_irq_save(flags);
1228 if (unlikely(!_req || !_req->complete
1229 || !_req->buf || !list_empty(&req->queue))) {
1231 dprintk(DEBUG_NORMAL, "%s: 1 X X X\n", __func__);
1233 dprintk(DEBUG_NORMAL, "%s: 0 %01d %01d %01d\n",
1234 __func__, !_req->complete, !_req->buf,
1235 !list_empty(&req->queue));
1238 local_irq_restore(flags);
1242 _req->status = -EINPROGRESS;
1245 dprintk(DEBUG_VERBOSE, "%s: ep%x len %d\n",
1246 __func__, ep->bEndpointAddress, _req->length);
1248 if (ep->bEndpointAddress) {
1249 udc_write(ep->bEndpointAddress & 0x7F, S3C2410_UDC_INDEX_REG);
1251 ep_csr = udc_read((ep->bEndpointAddress & USB_DIR_IN)
1252 ? S3C2410_UDC_IN_CSR1_REG
1253 : S3C2410_UDC_OUT_CSR1_REG);
1254 fifo_count = s3c2410_udc_fifo_count_out();
1256 udc_write(0, S3C2410_UDC_INDEX_REG);
1257 ep_csr = udc_read(S3C2410_UDC_IN_CSR1_REG);
1258 fifo_count = s3c2410_udc_fifo_count_out();
1261 /* kickstart this i/o queue? */
1262 if (list_empty(&ep->queue) && !ep->halted) {
1263 if (ep->bEndpointAddress == 0 /* ep0 */) {
1264 switch (dev->ep0state) {
1265 case EP0_IN_DATA_PHASE:
1266 if (!(ep_csr&S3C2410_UDC_EP0_CSR_IPKRDY)
1267 && s3c2410_udc_write_fifo(ep,
1269 dev->ep0state = EP0_IDLE;
1274 case EP0_OUT_DATA_PHASE:
1276 || ((ep_csr & S3C2410_UDC_OCSR1_PKTRDY)
1277 && s3c2410_udc_read_fifo(ep,
1279 dev->ep0state = EP0_IDLE;
1285 local_irq_restore(flags);
1288 } else if ((ep->bEndpointAddress & USB_DIR_IN) != 0
1289 && (!(ep_csr&S3C2410_UDC_OCSR1_PKTRDY))
1290 && s3c2410_udc_write_fifo(ep, req)) {
1292 } else if ((ep_csr & S3C2410_UDC_OCSR1_PKTRDY)
1294 && s3c2410_udc_read_fifo(ep, req)) {
1299 /* pio or dma irq handler advances the queue. */
1301 list_add_tail(&req->queue, &ep->queue);
1303 local_irq_restore(flags);
1305 dprintk(DEBUG_VERBOSE, "%s ok\n", __func__);
1310 * s3c2410_udc_dequeue
1312 static int s3c2410_udc_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1314 struct s3c2410_ep *ep = to_s3c2410_ep(_ep);
1315 struct s3c2410_udc *udc;
1316 int retval = -EINVAL;
1317 unsigned long flags;
1318 struct s3c2410_request *req = NULL;
1320 dprintk(DEBUG_VERBOSE, "%s(%p,%p)\n", __func__, _ep, _req);
1322 if (!the_controller->driver)
1328 udc = to_s3c2410_udc(ep->gadget);
1330 local_irq_save(flags);
1332 list_for_each_entry(req, &ep->queue, queue) {
1333 if (&req->req == _req) {
1334 list_del_init(&req->queue);
1335 _req->status = -ECONNRESET;
1342 dprintk(DEBUG_VERBOSE,
1343 "dequeued req %p from %s, len %d buf %p\n",
1344 req, _ep->name, _req->length, _req->buf);
1346 s3c2410_udc_done(ep, req, -ECONNRESET);
1349 local_irq_restore(flags);
1354 * s3c2410_udc_set_halt
1356 static int s3c2410_udc_set_halt(struct usb_ep *_ep, int value)
1358 struct s3c2410_ep *ep = to_s3c2410_ep(_ep);
1360 unsigned long flags;
1363 if (unlikely(!_ep || (!ep->ep.desc && ep->ep.name != ep0name))) {
1364 dprintk(DEBUG_NORMAL, "%s: inval 2\n", __func__);
1368 local_irq_save(flags);
1370 idx = ep->bEndpointAddress & 0x7F;
1373 s3c2410_udc_set_ep0_ss(base_addr);
1374 s3c2410_udc_set_ep0_de_out(base_addr);
1376 udc_write(idx, S3C2410_UDC_INDEX_REG);
1377 ep_csr = udc_read((ep->bEndpointAddress & USB_DIR_IN)
1378 ? S3C2410_UDC_IN_CSR1_REG
1379 : S3C2410_UDC_OUT_CSR1_REG);
1381 if ((ep->bEndpointAddress & USB_DIR_IN) != 0) {
1383 udc_write(ep_csr | S3C2410_UDC_ICSR1_SENDSTL,
1384 S3C2410_UDC_IN_CSR1_REG);
1386 ep_csr &= ~S3C2410_UDC_ICSR1_SENDSTL;
1387 udc_write(ep_csr, S3C2410_UDC_IN_CSR1_REG);
1388 ep_csr |= S3C2410_UDC_ICSR1_CLRDT;
1389 udc_write(ep_csr, S3C2410_UDC_IN_CSR1_REG);
1393 udc_write(ep_csr | S3C2410_UDC_OCSR1_SENDSTL,
1394 S3C2410_UDC_OUT_CSR1_REG);
1396 ep_csr &= ~S3C2410_UDC_OCSR1_SENDSTL;
1397 udc_write(ep_csr, S3C2410_UDC_OUT_CSR1_REG);
1398 ep_csr |= S3C2410_UDC_OCSR1_CLRDT;
1399 udc_write(ep_csr, S3C2410_UDC_OUT_CSR1_REG);
1404 ep->halted = value ? 1 : 0;
1405 local_irq_restore(flags);
1410 static const struct usb_ep_ops s3c2410_ep_ops = {
1411 .enable = s3c2410_udc_ep_enable,
1412 .disable = s3c2410_udc_ep_disable,
1414 .alloc_request = s3c2410_udc_alloc_request,
1415 .free_request = s3c2410_udc_free_request,
1417 .queue = s3c2410_udc_queue,
1418 .dequeue = s3c2410_udc_dequeue,
1420 .set_halt = s3c2410_udc_set_halt,
1423 /*------------------------- usb_gadget_ops ----------------------------------*/
1426 * s3c2410_udc_get_frame
1428 static int s3c2410_udc_get_frame(struct usb_gadget *_gadget)
1432 dprintk(DEBUG_VERBOSE, "%s()\n", __func__);
1434 tmp = udc_read(S3C2410_UDC_FRAME_NUM2_REG) << 8;
1435 tmp |= udc_read(S3C2410_UDC_FRAME_NUM1_REG);
1440 * s3c2410_udc_wakeup
1442 static int s3c2410_udc_wakeup(struct usb_gadget *_gadget)
1444 dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1449 * s3c2410_udc_set_selfpowered
1451 static int s3c2410_udc_set_selfpowered(struct usb_gadget *gadget, int value)
1453 struct s3c2410_udc *udc = to_s3c2410_udc(gadget);
1455 dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1458 udc->devstatus |= (1 << USB_DEVICE_SELF_POWERED);
1460 udc->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
1465 static void s3c2410_udc_disable(struct s3c2410_udc *dev);
1466 static void s3c2410_udc_enable(struct s3c2410_udc *dev);
1468 static int s3c2410_udc_set_pullup(struct s3c2410_udc *udc, int is_on)
1470 dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1472 if (udc_info && (udc_info->udc_command ||
1473 gpio_is_valid(udc_info->pullup_pin))) {
1476 s3c2410_udc_enable(udc);
1478 if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
1479 if (udc->driver && udc->driver->disconnect)
1480 udc->driver->disconnect(&udc->gadget);
1483 s3c2410_udc_disable(udc);
1492 static int s3c2410_udc_vbus_session(struct usb_gadget *gadget, int is_active)
1494 struct s3c2410_udc *udc = to_s3c2410_udc(gadget);
1496 dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1498 udc->vbus = (is_active != 0);
1499 s3c2410_udc_set_pullup(udc, is_active);
1503 static int s3c2410_udc_pullup(struct usb_gadget *gadget, int is_on)
1505 struct s3c2410_udc *udc = to_s3c2410_udc(gadget);
1507 dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1509 s3c2410_udc_set_pullup(udc, is_on ? 0 : 1);
1513 static irqreturn_t s3c2410_udc_vbus_irq(int irq, void *_dev)
1515 struct s3c2410_udc *dev = _dev;
1518 dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1520 value = gpio_get_value(udc_info->vbus_pin) ? 1 : 0;
1521 if (udc_info->vbus_pin_inverted)
1524 if (value != dev->vbus)
1525 s3c2410_udc_vbus_session(&dev->gadget, value);
1530 static int s3c2410_vbus_draw(struct usb_gadget *_gadget, unsigned ma)
1532 dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1534 if (udc_info && udc_info->vbus_draw) {
1535 udc_info->vbus_draw(ma);
1542 static int s3c2410_udc_start(struct usb_gadget *g,
1543 struct usb_gadget_driver *driver);
1544 static int s3c2410_udc_stop(struct usb_gadget *g);
1546 static const struct usb_gadget_ops s3c2410_ops = {
1547 .get_frame = s3c2410_udc_get_frame,
1548 .wakeup = s3c2410_udc_wakeup,
1549 .set_selfpowered = s3c2410_udc_set_selfpowered,
1550 .pullup = s3c2410_udc_pullup,
1551 .vbus_session = s3c2410_udc_vbus_session,
1552 .vbus_draw = s3c2410_vbus_draw,
1553 .udc_start = s3c2410_udc_start,
1554 .udc_stop = s3c2410_udc_stop,
1557 static void s3c2410_udc_command(enum s3c2410_udc_cmd_e cmd)
1562 if (udc_info->udc_command) {
1563 udc_info->udc_command(cmd);
1564 } else if (gpio_is_valid(udc_info->pullup_pin)) {
1568 case S3C2410_UDC_P_ENABLE:
1571 case S3C2410_UDC_P_DISABLE:
1577 value ^= udc_info->pullup_pin_inverted;
1579 gpio_set_value(udc_info->pullup_pin, value);
1583 /*------------------------- gadget driver handling---------------------------*/
1585 * s3c2410_udc_disable
1587 static void s3c2410_udc_disable(struct s3c2410_udc *dev)
1589 dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1591 /* Disable all interrupts */
1592 udc_write(0x00, S3C2410_UDC_USB_INT_EN_REG);
1593 udc_write(0x00, S3C2410_UDC_EP_INT_EN_REG);
1595 /* Clear the interrupt registers */
1596 udc_write(S3C2410_UDC_USBINT_RESET
1597 | S3C2410_UDC_USBINT_RESUME
1598 | S3C2410_UDC_USBINT_SUSPEND,
1599 S3C2410_UDC_USB_INT_REG);
1601 udc_write(0x1F, S3C2410_UDC_EP_INT_REG);
1603 /* Good bye, cruel world */
1604 s3c2410_udc_command(S3C2410_UDC_P_DISABLE);
1606 /* Set speed to unknown */
1607 dev->gadget.speed = USB_SPEED_UNKNOWN;
1611 * s3c2410_udc_reinit
1613 static void s3c2410_udc_reinit(struct s3c2410_udc *dev)
1617 /* device/ep0 records init */
1618 INIT_LIST_HEAD(&dev->gadget.ep_list);
1619 INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
1620 dev->ep0state = EP0_IDLE;
1622 for (i = 0; i < S3C2410_ENDPOINTS; i++) {
1623 struct s3c2410_ep *ep = &dev->ep[i];
1626 list_add_tail(&ep->ep.ep_list, &dev->gadget.ep_list);
1631 INIT_LIST_HEAD(&ep->queue);
1632 usb_ep_set_maxpacket_limit(&ep->ep, ep->ep.maxpacket);
1637 * s3c2410_udc_enable
1639 static void s3c2410_udc_enable(struct s3c2410_udc *dev)
1643 dprintk(DEBUG_NORMAL, "s3c2410_udc_enable called\n");
1645 /* dev->gadget.speed = USB_SPEED_UNKNOWN; */
1646 dev->gadget.speed = USB_SPEED_FULL;
1648 /* Set MAXP for all endpoints */
1649 for (i = 0; i < S3C2410_ENDPOINTS; i++) {
1650 udc_write(i, S3C2410_UDC_INDEX_REG);
1651 udc_write((dev->ep[i].ep.maxpacket & 0x7ff) >> 3,
1652 S3C2410_UDC_MAXP_REG);
1655 /* Set default power state */
1656 udc_write(DEFAULT_POWER_STATE, S3C2410_UDC_PWR_REG);
1658 /* Enable reset and suspend interrupt interrupts */
1659 udc_write(S3C2410_UDC_USBINT_RESET | S3C2410_UDC_USBINT_SUSPEND,
1660 S3C2410_UDC_USB_INT_EN_REG);
1662 /* Enable ep0 interrupt */
1663 udc_write(S3C2410_UDC_INT_EP0, S3C2410_UDC_EP_INT_EN_REG);
1665 /* time to say "hello, world" */
1666 s3c2410_udc_command(S3C2410_UDC_P_ENABLE);
1669 static int s3c2410_udc_start(struct usb_gadget *g,
1670 struct usb_gadget_driver *driver)
1672 struct s3c2410_udc *udc = to_s3c2410(g);
1674 dprintk(DEBUG_NORMAL, "%s() '%s'\n", __func__, driver->driver.name);
1676 /* Hook the driver */
1677 udc->driver = driver;
1680 s3c2410_udc_enable(udc);
1685 static int s3c2410_udc_stop(struct usb_gadget *g)
1687 struct s3c2410_udc *udc = to_s3c2410(g);
1692 s3c2410_udc_disable(udc);
1697 /*---------------------------------------------------------------------------*/
1698 static struct s3c2410_udc memory = {
1700 .ops = &s3c2410_ops,
1701 .ep0 = &memory.ep[0].ep,
1702 .name = gadget_name,
1704 .init_name = "gadget",
1708 /* control endpoint */
1713 .ops = &s3c2410_ep_ops,
1714 .maxpacket = EP0_FIFO_SIZE,
1719 /* first group of endpoints */
1724 .ops = &s3c2410_ep_ops,
1725 .maxpacket = EP_FIFO_SIZE,
1728 .fifo_size = EP_FIFO_SIZE,
1729 .bEndpointAddress = 1,
1730 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1736 .ops = &s3c2410_ep_ops,
1737 .maxpacket = EP_FIFO_SIZE,
1740 .fifo_size = EP_FIFO_SIZE,
1741 .bEndpointAddress = 2,
1742 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1748 .ops = &s3c2410_ep_ops,
1749 .maxpacket = EP_FIFO_SIZE,
1752 .fifo_size = EP_FIFO_SIZE,
1753 .bEndpointAddress = 3,
1754 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1760 .ops = &s3c2410_ep_ops,
1761 .maxpacket = EP_FIFO_SIZE,
1764 .fifo_size = EP_FIFO_SIZE,
1765 .bEndpointAddress = 4,
1766 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1772 * probe - binds to the platform device
1774 static int s3c2410_udc_probe(struct platform_device *pdev)
1776 struct s3c2410_udc *udc = &memory;
1777 struct device *dev = &pdev->dev;
1781 dev_dbg(dev, "%s()\n", __func__);
1783 usb_bus_clock = clk_get(NULL, "usb-bus-gadget");
1784 if (IS_ERR(usb_bus_clock)) {
1785 dev_err(dev, "failed to get usb bus clock source\n");
1786 return PTR_ERR(usb_bus_clock);
1789 clk_prepare_enable(usb_bus_clock);
1791 udc_clock = clk_get(NULL, "usb-device");
1792 if (IS_ERR(udc_clock)) {
1793 dev_err(dev, "failed to get udc clock source\n");
1794 return PTR_ERR(udc_clock);
1797 clk_prepare_enable(udc_clock);
1801 dev_dbg(dev, "got and enabled clocks\n");
1803 if (strncmp(pdev->name, "s3c2440", 7) == 0) {
1804 dev_info(dev, "S3C2440: increasing FIFO to 128 bytes\n");
1805 memory.ep[1].fifo_size = S3C2440_EP_FIFO_SIZE;
1806 memory.ep[2].fifo_size = S3C2440_EP_FIFO_SIZE;
1807 memory.ep[3].fifo_size = S3C2440_EP_FIFO_SIZE;
1808 memory.ep[4].fifo_size = S3C2440_EP_FIFO_SIZE;
1811 spin_lock_init(&udc->lock);
1812 udc_info = dev_get_platdata(&pdev->dev);
1814 rsrc_start = S3C2410_PA_USBDEV;
1815 rsrc_len = S3C24XX_SZ_USBDEV;
1817 if (!request_mem_region(rsrc_start, rsrc_len, gadget_name))
1820 base_addr = ioremap(rsrc_start, rsrc_len);
1826 the_controller = udc;
1827 platform_set_drvdata(pdev, udc);
1829 s3c2410_udc_disable(udc);
1830 s3c2410_udc_reinit(udc);
1832 /* irq setup after old hardware state is cleaned up */
1833 retval = request_irq(IRQ_USBD, s3c2410_udc_irq,
1834 0, gadget_name, udc);
1837 dev_err(dev, "cannot get irq %i, err %d\n", IRQ_USBD, retval);
1842 dev_dbg(dev, "got irq %i\n", IRQ_USBD);
1844 if (udc_info && udc_info->vbus_pin > 0) {
1845 retval = gpio_request(udc_info->vbus_pin, "udc vbus");
1847 dev_err(dev, "cannot claim vbus pin\n");
1851 irq = gpio_to_irq(udc_info->vbus_pin);
1853 dev_err(dev, "no irq for gpio vbus pin\n");
1855 goto err_gpio_claim;
1858 retval = request_irq(irq, s3c2410_udc_vbus_irq,
1860 | IRQF_TRIGGER_FALLING | IRQF_SHARED,
1864 dev_err(dev, "can't get vbus irq %d, err %d\n",
1867 goto err_gpio_claim;
1870 dev_dbg(dev, "got irq %i\n", irq);
1875 if (udc_info && !udc_info->udc_command &&
1876 gpio_is_valid(udc_info->pullup_pin)) {
1878 retval = gpio_request_one(udc_info->pullup_pin,
1879 udc_info->vbus_pin_inverted ?
1880 GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW,
1886 retval = usb_add_gadget_udc(&pdev->dev, &udc->gadget);
1890 if (s3c2410_udc_debugfs_root) {
1891 udc->regs_info = debugfs_create_file("registers", S_IRUGO,
1892 s3c2410_udc_debugfs_root,
1893 udc, &s3c2410_udc_debugfs_fops);
1894 if (!udc->regs_info)
1895 dev_warn(dev, "debugfs file creation failed\n");
1898 dev_dbg(dev, "probe ok\n");
1903 if (udc_info && !udc_info->udc_command &&
1904 gpio_is_valid(udc_info->pullup_pin))
1905 gpio_free(udc_info->pullup_pin);
1907 if (udc_info && udc_info->vbus_pin > 0)
1908 free_irq(gpio_to_irq(udc_info->vbus_pin), udc);
1910 if (udc_info && udc_info->vbus_pin > 0)
1911 gpio_free(udc_info->vbus_pin);
1913 free_irq(IRQ_USBD, udc);
1917 release_mem_region(rsrc_start, rsrc_len);
1923 * s3c2410_udc_remove
1925 static int s3c2410_udc_remove(struct platform_device *pdev)
1927 struct s3c2410_udc *udc = platform_get_drvdata(pdev);
1930 dev_dbg(&pdev->dev, "%s()\n", __func__);
1935 usb_del_gadget_udc(&udc->gadget);
1936 debugfs_remove(udc->regs_info);
1938 if (udc_info && !udc_info->udc_command &&
1939 gpio_is_valid(udc_info->pullup_pin))
1940 gpio_free(udc_info->pullup_pin);
1942 if (udc_info && udc_info->vbus_pin > 0) {
1943 irq = gpio_to_irq(udc_info->vbus_pin);
1947 free_irq(IRQ_USBD, udc);
1950 release_mem_region(rsrc_start, rsrc_len);
1952 if (!IS_ERR(udc_clock) && udc_clock != NULL) {
1953 clk_disable_unprepare(udc_clock);
1958 if (!IS_ERR(usb_bus_clock) && usb_bus_clock != NULL) {
1959 clk_disable_unprepare(usb_bus_clock);
1960 clk_put(usb_bus_clock);
1961 usb_bus_clock = NULL;
1964 dev_dbg(&pdev->dev, "%s: remove ok\n", __func__);
1970 s3c2410_udc_suspend(struct platform_device *pdev, pm_message_t message)
1972 s3c2410_udc_command(S3C2410_UDC_P_DISABLE);
1977 static int s3c2410_udc_resume(struct platform_device *pdev)
1979 s3c2410_udc_command(S3C2410_UDC_P_ENABLE);
1984 #define s3c2410_udc_suspend NULL
1985 #define s3c2410_udc_resume NULL
1988 static const struct platform_device_id s3c_udc_ids[] = {
1989 { "s3c2410-usbgadget", },
1990 { "s3c2440-usbgadget", },
1993 MODULE_DEVICE_TABLE(platform, s3c_udc_ids);
1995 static struct platform_driver udc_driver_24x0 = {
1997 .name = "s3c24x0-usbgadget",
1999 .probe = s3c2410_udc_probe,
2000 .remove = s3c2410_udc_remove,
2001 .suspend = s3c2410_udc_suspend,
2002 .resume = s3c2410_udc_resume,
2003 .id_table = s3c_udc_ids,
2006 static int __init udc_init(void)
2010 dprintk(DEBUG_NORMAL, "%s: version %s\n", gadget_name, DRIVER_VERSION);
2012 s3c2410_udc_debugfs_root = debugfs_create_dir(gadget_name, NULL);
2013 if (IS_ERR(s3c2410_udc_debugfs_root)) {
2014 pr_err("%s: debugfs dir creation failed %ld\n",
2015 gadget_name, PTR_ERR(s3c2410_udc_debugfs_root));
2016 s3c2410_udc_debugfs_root = NULL;
2019 retval = platform_driver_register(&udc_driver_24x0);
2026 debugfs_remove(s3c2410_udc_debugfs_root);
2030 static void __exit udc_exit(void)
2032 platform_driver_unregister(&udc_driver_24x0);
2033 debugfs_remove(s3c2410_udc_debugfs_root);
2036 module_init(udc_init);
2037 module_exit(udc_exit);
2039 MODULE_AUTHOR(DRIVER_AUTHOR);
2040 MODULE_DESCRIPTION(DRIVER_DESC);
2041 MODULE_VERSION(DRIVER_VERSION);
2042 MODULE_LICENSE("GPL");