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;
96 static void dprintk(int level, const char *fmt, ...)
98 static long prevticks;
99 static int invocation;
100 struct va_format vaf;
103 if (level > USB_S3C2410_DEBUG_LEVEL)
111 if (s3c2410_ticks != prevticks) {
112 prevticks = s3c2410_ticks;
116 pr_debug("%1lu.%02d USB: %pV", prevticks, invocation++, &vaf);
122 static void dprintk(int level, const char *fmt, ...)
127 static int s3c2410_udc_debugfs_seq_show(struct seq_file *m, void *p)
129 u32 addr_reg, pwr_reg, ep_int_reg, usb_int_reg;
130 u32 ep_int_en_reg, usb_int_en_reg, ep0_csr;
131 u32 ep1_i_csr1, ep1_i_csr2, ep1_o_csr1, ep1_o_csr2;
132 u32 ep2_i_csr1, ep2_i_csr2, ep2_o_csr1, ep2_o_csr2;
134 addr_reg = udc_read(S3C2410_UDC_FUNC_ADDR_REG);
135 pwr_reg = udc_read(S3C2410_UDC_PWR_REG);
136 ep_int_reg = udc_read(S3C2410_UDC_EP_INT_REG);
137 usb_int_reg = udc_read(S3C2410_UDC_USB_INT_REG);
138 ep_int_en_reg = udc_read(S3C2410_UDC_EP_INT_EN_REG);
139 usb_int_en_reg = udc_read(S3C2410_UDC_USB_INT_EN_REG);
140 udc_write(0, S3C2410_UDC_INDEX_REG);
141 ep0_csr = udc_read(S3C2410_UDC_IN_CSR1_REG);
142 udc_write(1, S3C2410_UDC_INDEX_REG);
143 ep1_i_csr1 = udc_read(S3C2410_UDC_IN_CSR1_REG);
144 ep1_i_csr2 = udc_read(S3C2410_UDC_IN_CSR2_REG);
145 ep1_o_csr1 = udc_read(S3C2410_UDC_IN_CSR1_REG);
146 ep1_o_csr2 = udc_read(S3C2410_UDC_IN_CSR2_REG);
147 udc_write(2, S3C2410_UDC_INDEX_REG);
148 ep2_i_csr1 = udc_read(S3C2410_UDC_IN_CSR1_REG);
149 ep2_i_csr2 = udc_read(S3C2410_UDC_IN_CSR2_REG);
150 ep2_o_csr1 = udc_read(S3C2410_UDC_IN_CSR1_REG);
151 ep2_o_csr2 = udc_read(S3C2410_UDC_IN_CSR2_REG);
153 seq_printf(m, "FUNC_ADDR_REG : 0x%04X\n"
155 "EP_INT_REG : 0x%04X\n"
156 "USB_INT_REG : 0x%04X\n"
157 "EP_INT_EN_REG : 0x%04X\n"
158 "USB_INT_EN_REG : 0x%04X\n"
160 "EP1_I_CSR1 : 0x%04X\n"
161 "EP1_I_CSR2 : 0x%04X\n"
162 "EP1_O_CSR1 : 0x%04X\n"
163 "EP1_O_CSR2 : 0x%04X\n"
164 "EP2_I_CSR1 : 0x%04X\n"
165 "EP2_I_CSR2 : 0x%04X\n"
166 "EP2_O_CSR1 : 0x%04X\n"
167 "EP2_O_CSR2 : 0x%04X\n",
168 addr_reg, pwr_reg, ep_int_reg, usb_int_reg,
169 ep_int_en_reg, usb_int_en_reg, ep0_csr,
170 ep1_i_csr1, ep1_i_csr2, ep1_o_csr1, ep1_o_csr2,
171 ep2_i_csr1, ep2_i_csr2, ep2_o_csr1, ep2_o_csr2
177 static int s3c2410_udc_debugfs_fops_open(struct inode *inode,
180 return single_open(file, s3c2410_udc_debugfs_seq_show, NULL);
183 static const struct file_operations s3c2410_udc_debugfs_fops = {
184 .open = s3c2410_udc_debugfs_fops_open,
187 .release = single_release,
188 .owner = THIS_MODULE,
193 static inline void s3c2410_udc_clear_ep0_opr(void __iomem *base)
195 udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
196 udc_writeb(base, S3C2410_UDC_EP0_CSR_SOPKTRDY,
197 S3C2410_UDC_EP0_CSR_REG);
200 static inline void s3c2410_udc_clear_ep0_sst(void __iomem *base)
202 udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
203 writeb(0x00, base + S3C2410_UDC_EP0_CSR_REG);
206 static inline void s3c2410_udc_clear_ep0_se(void __iomem *base)
208 udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
209 udc_writeb(base, S3C2410_UDC_EP0_CSR_SSE, S3C2410_UDC_EP0_CSR_REG);
212 static inline void s3c2410_udc_set_ep0_ipr(void __iomem *base)
214 udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
215 udc_writeb(base, S3C2410_UDC_EP0_CSR_IPKRDY, S3C2410_UDC_EP0_CSR_REG);
218 static inline void s3c2410_udc_set_ep0_de(void __iomem *base)
220 udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
221 udc_writeb(base, S3C2410_UDC_EP0_CSR_DE, S3C2410_UDC_EP0_CSR_REG);
224 inline void s3c2410_udc_set_ep0_ss(void __iomem *b)
226 udc_writeb(b, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
227 udc_writeb(b, S3C2410_UDC_EP0_CSR_SENDSTL, S3C2410_UDC_EP0_CSR_REG);
230 static inline void s3c2410_udc_set_ep0_de_out(void __iomem *base)
232 udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
234 udc_writeb(base, (S3C2410_UDC_EP0_CSR_SOPKTRDY
235 | S3C2410_UDC_EP0_CSR_DE),
236 S3C2410_UDC_EP0_CSR_REG);
239 static inline void s3c2410_udc_set_ep0_de_in(void __iomem *base)
241 udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
242 udc_writeb(base, (S3C2410_UDC_EP0_CSR_IPKRDY
243 | S3C2410_UDC_EP0_CSR_DE),
244 S3C2410_UDC_EP0_CSR_REG);
247 /*------------------------- I/O ----------------------------------*/
252 static void s3c2410_udc_done(struct s3c2410_ep *ep,
253 struct s3c2410_request *req, int status)
255 unsigned halted = ep->halted;
257 list_del_init(&req->queue);
259 if (likely(req->req.status == -EINPROGRESS))
260 req->req.status = status;
262 status = req->req.status;
265 usb_gadget_giveback_request(&ep->ep, &req->req);
269 static void s3c2410_udc_nuke(struct s3c2410_udc *udc,
270 struct s3c2410_ep *ep, int status)
272 while (!list_empty(&ep->queue)) {
273 struct s3c2410_request *req;
274 req = list_entry(ep->queue.next, struct s3c2410_request,
276 s3c2410_udc_done(ep, req, status);
280 static inline int s3c2410_udc_fifo_count_out(void)
284 tmp = udc_read(S3C2410_UDC_OUT_FIFO_CNT2_REG) << 8;
285 tmp |= udc_read(S3C2410_UDC_OUT_FIFO_CNT1_REG);
290 * s3c2410_udc_write_packet
292 static inline int s3c2410_udc_write_packet(int fifo,
293 struct s3c2410_request *req,
296 unsigned len = min(req->req.length - req->req.actual, max);
297 u8 *buf = req->req.buf + req->req.actual;
301 dprintk(DEBUG_VERBOSE, "%s %d %d %d %d\n", __func__,
302 req->req.actual, req->req.length, len, req->req.actual + len);
304 req->req.actual += len;
307 writesb(base_addr + fifo, buf, len);
312 * s3c2410_udc_write_fifo
314 * return: 0 = still running, 1 = completed, negative = errno
316 static int s3c2410_udc_write_fifo(struct s3c2410_ep *ep,
317 struct s3c2410_request *req)
325 idx = ep->bEndpointAddress & 0x7F;
330 fifo_reg = S3C2410_UDC_EP0_FIFO_REG;
333 fifo_reg = S3C2410_UDC_EP1_FIFO_REG;
336 fifo_reg = S3C2410_UDC_EP2_FIFO_REG;
339 fifo_reg = S3C2410_UDC_EP3_FIFO_REG;
342 fifo_reg = S3C2410_UDC_EP4_FIFO_REG;
346 count = s3c2410_udc_write_packet(fifo_reg, req, ep->ep.maxpacket);
348 /* last packet is often short (sometimes a zlp) */
349 if (count != ep->ep.maxpacket)
351 else if (req->req.length != req->req.actual || req->req.zero)
356 /* Only ep0 debug messages are interesting */
358 dprintk(DEBUG_NORMAL,
359 "Written ep%d %d.%d of %d b [last %d,z %d]\n",
360 idx, count, req->req.actual, req->req.length,
361 is_last, req->req.zero);
364 /* The order is important. It prevents sending 2 packets
365 * at the same time */
368 /* Reset signal => no need to say 'data sent' */
369 if (!(udc_read(S3C2410_UDC_USB_INT_REG)
370 & S3C2410_UDC_USBINT_RESET))
371 s3c2410_udc_set_ep0_de_in(base_addr);
372 ep->dev->ep0state = EP0_IDLE;
374 udc_write(idx, S3C2410_UDC_INDEX_REG);
375 ep_csr = udc_read(S3C2410_UDC_IN_CSR1_REG);
376 udc_write(idx, S3C2410_UDC_INDEX_REG);
377 udc_write(ep_csr | S3C2410_UDC_ICSR1_PKTRDY,
378 S3C2410_UDC_IN_CSR1_REG);
381 s3c2410_udc_done(ep, req, 0);
385 /* Reset signal => no need to say 'data sent' */
386 if (!(udc_read(S3C2410_UDC_USB_INT_REG)
387 & S3C2410_UDC_USBINT_RESET))
388 s3c2410_udc_set_ep0_ipr(base_addr);
390 udc_write(idx, S3C2410_UDC_INDEX_REG);
391 ep_csr = udc_read(S3C2410_UDC_IN_CSR1_REG);
392 udc_write(idx, S3C2410_UDC_INDEX_REG);
393 udc_write(ep_csr | S3C2410_UDC_ICSR1_PKTRDY,
394 S3C2410_UDC_IN_CSR1_REG);
401 static inline int s3c2410_udc_read_packet(int fifo, u8 *buf,
402 struct s3c2410_request *req, unsigned avail)
406 len = min(req->req.length - req->req.actual, avail);
407 req->req.actual += len;
409 readsb(fifo + base_addr, buf, len);
414 * return: 0 = still running, 1 = queue empty, negative = errno
416 static int s3c2410_udc_read_fifo(struct s3c2410_ep *ep,
417 struct s3c2410_request *req)
421 unsigned bufferspace;
428 idx = ep->bEndpointAddress & 0x7F;
434 fifo_reg = S3C2410_UDC_EP0_FIFO_REG;
437 fifo_reg = S3C2410_UDC_EP1_FIFO_REG;
440 fifo_reg = S3C2410_UDC_EP2_FIFO_REG;
443 fifo_reg = S3C2410_UDC_EP3_FIFO_REG;
446 fifo_reg = S3C2410_UDC_EP4_FIFO_REG;
450 if (!req->req.length)
453 buf = req->req.buf + req->req.actual;
454 bufferspace = req->req.length - req->req.actual;
456 dprintk(DEBUG_NORMAL, "%s: buffer full!\n", __func__);
460 udc_write(idx, S3C2410_UDC_INDEX_REG);
462 fifo_count = s3c2410_udc_fifo_count_out();
463 dprintk(DEBUG_NORMAL, "%s fifo count : %d\n", __func__, fifo_count);
465 if (fifo_count > ep->ep.maxpacket)
466 avail = ep->ep.maxpacket;
470 fifo_count = s3c2410_udc_read_packet(fifo_reg, buf, req, avail);
472 /* checking this with ep0 is not accurate as we already
473 * read a control request
475 if (idx != 0 && fifo_count < ep->ep.maxpacket) {
477 /* overflowed this request? flush extra data */
478 if (fifo_count != avail)
479 req->req.status = -EOVERFLOW;
481 is_last = (req->req.length <= req->req.actual) ? 1 : 0;
484 udc_write(idx, S3C2410_UDC_INDEX_REG);
485 fifo_count = s3c2410_udc_fifo_count_out();
487 /* Only ep0 debug messages are interesting */
489 dprintk(DEBUG_VERBOSE, "%s fifo count : %d [last %d]\n",
490 __func__, fifo_count, is_last);
494 s3c2410_udc_set_ep0_de_out(base_addr);
495 ep->dev->ep0state = EP0_IDLE;
497 udc_write(idx, S3C2410_UDC_INDEX_REG);
498 ep_csr = udc_read(S3C2410_UDC_OUT_CSR1_REG);
499 udc_write(idx, S3C2410_UDC_INDEX_REG);
500 udc_write(ep_csr & ~S3C2410_UDC_OCSR1_PKTRDY,
501 S3C2410_UDC_OUT_CSR1_REG);
504 s3c2410_udc_done(ep, req, 0);
507 s3c2410_udc_clear_ep0_opr(base_addr);
509 udc_write(idx, S3C2410_UDC_INDEX_REG);
510 ep_csr = udc_read(S3C2410_UDC_OUT_CSR1_REG);
511 udc_write(idx, S3C2410_UDC_INDEX_REG);
512 udc_write(ep_csr & ~S3C2410_UDC_OCSR1_PKTRDY,
513 S3C2410_UDC_OUT_CSR1_REG);
520 static int s3c2410_udc_read_fifo_crq(struct usb_ctrlrequest *crq)
522 unsigned char *outbuf = (unsigned char *)crq;
525 udc_write(0, S3C2410_UDC_INDEX_REG);
527 bytes_read = s3c2410_udc_fifo_count_out();
529 dprintk(DEBUG_NORMAL, "%s: fifo_count=%d\n", __func__, bytes_read);
531 if (bytes_read > sizeof(struct usb_ctrlrequest))
532 bytes_read = sizeof(struct usb_ctrlrequest);
534 readsb(S3C2410_UDC_EP0_FIFO_REG + base_addr, outbuf, bytes_read);
536 dprintk(DEBUG_VERBOSE, "%s: len=%d %02x:%02x {%x,%x,%x}\n", __func__,
537 bytes_read, crq->bRequest, crq->bRequestType,
538 crq->wValue, crq->wIndex, crq->wLength);
543 static int s3c2410_udc_get_status(struct s3c2410_udc *dev,
544 struct usb_ctrlrequest *crq)
547 u8 ep_num = crq->wIndex & 0x7F;
548 u8 is_in = crq->wIndex & USB_DIR_IN;
550 switch (crq->bRequestType & USB_RECIP_MASK) {
551 case USB_RECIP_INTERFACE:
554 case USB_RECIP_DEVICE:
555 status = dev->devstatus;
558 case USB_RECIP_ENDPOINT:
559 if (ep_num > 4 || crq->wLength > 2)
563 udc_write(0, S3C2410_UDC_INDEX_REG);
564 status = udc_read(S3C2410_UDC_IN_CSR1_REG);
565 status = status & S3C2410_UDC_EP0_CSR_SENDSTL;
567 udc_write(ep_num, S3C2410_UDC_INDEX_REG);
569 status = udc_read(S3C2410_UDC_IN_CSR1_REG);
570 status = status & S3C2410_UDC_ICSR1_SENDSTL;
572 status = udc_read(S3C2410_UDC_OUT_CSR1_REG);
573 status = status & S3C2410_UDC_OCSR1_SENDSTL;
577 status = status ? 1 : 0;
584 /* Seems to be needed to get it working. ouch :( */
586 udc_write(status & 0xFF, S3C2410_UDC_EP0_FIFO_REG);
587 udc_write(status >> 8, S3C2410_UDC_EP0_FIFO_REG);
588 s3c2410_udc_set_ep0_de_in(base_addr);
592 /*------------------------- usb state machine -------------------------------*/
593 static int s3c2410_udc_set_halt(struct usb_ep *_ep, int value);
595 static void s3c2410_udc_handle_ep0_idle(struct s3c2410_udc *dev,
596 struct s3c2410_ep *ep,
597 struct usb_ctrlrequest *crq,
602 /* start control request? */
603 if (!(ep0csr & S3C2410_UDC_EP0_CSR_OPKRDY))
606 s3c2410_udc_nuke(dev, ep, -EPROTO);
608 len = s3c2410_udc_read_fifo_crq(crq);
609 if (len != sizeof(*crq)) {
610 dprintk(DEBUG_NORMAL, "setup begin: fifo READ ERROR"
611 " wanted %d bytes got %d. Stalling out...\n",
613 s3c2410_udc_set_ep0_ss(base_addr);
617 dprintk(DEBUG_NORMAL, "bRequest = %d bRequestType %d wLength = %d\n",
618 crq->bRequest, crq->bRequestType, crq->wLength);
620 /* cope with automagic for some standard requests. */
621 dev->req_std = (crq->bRequestType & USB_TYPE_MASK)
622 == USB_TYPE_STANDARD;
624 dev->req_pending = 1;
626 switch (crq->bRequest) {
627 case USB_REQ_SET_CONFIGURATION:
628 dprintk(DEBUG_NORMAL, "USB_REQ_SET_CONFIGURATION ...\n");
630 if (crq->bRequestType == USB_RECIP_DEVICE) {
632 s3c2410_udc_set_ep0_de_out(base_addr);
636 case USB_REQ_SET_INTERFACE:
637 dprintk(DEBUG_NORMAL, "USB_REQ_SET_INTERFACE ...\n");
639 if (crq->bRequestType == USB_RECIP_INTERFACE) {
641 s3c2410_udc_set_ep0_de_out(base_addr);
645 case USB_REQ_SET_ADDRESS:
646 dprintk(DEBUG_NORMAL, "USB_REQ_SET_ADDRESS ...\n");
648 if (crq->bRequestType == USB_RECIP_DEVICE) {
649 tmp = crq->wValue & 0x7F;
651 udc_write((tmp | S3C2410_UDC_FUNCADDR_UPDATE),
652 S3C2410_UDC_FUNC_ADDR_REG);
653 s3c2410_udc_set_ep0_de_out(base_addr);
658 case USB_REQ_GET_STATUS:
659 dprintk(DEBUG_NORMAL, "USB_REQ_GET_STATUS ...\n");
660 s3c2410_udc_clear_ep0_opr(base_addr);
663 if (!s3c2410_udc_get_status(dev, crq))
668 case USB_REQ_CLEAR_FEATURE:
669 s3c2410_udc_clear_ep0_opr(base_addr);
671 if (crq->bRequestType != USB_RECIP_ENDPOINT)
674 if (crq->wValue != USB_ENDPOINT_HALT || crq->wLength != 0)
677 s3c2410_udc_set_halt(&dev->ep[crq->wIndex & 0x7f].ep, 0);
678 s3c2410_udc_set_ep0_de_out(base_addr);
681 case USB_REQ_SET_FEATURE:
682 s3c2410_udc_clear_ep0_opr(base_addr);
684 if (crq->bRequestType != USB_RECIP_ENDPOINT)
687 if (crq->wValue != USB_ENDPOINT_HALT || crq->wLength != 0)
690 s3c2410_udc_set_halt(&dev->ep[crq->wIndex & 0x7f].ep, 1);
691 s3c2410_udc_set_ep0_de_out(base_addr);
695 s3c2410_udc_clear_ep0_opr(base_addr);
699 if (crq->bRequestType & USB_DIR_IN)
700 dev->ep0state = EP0_IN_DATA_PHASE;
702 dev->ep0state = EP0_OUT_DATA_PHASE;
707 /* deliver the request to the gadget driver */
708 ret = dev->driver->setup(&dev->gadget, crq);
710 if (dev->req_config) {
711 dprintk(DEBUG_NORMAL, "config change %02x fail %d?\n",
716 if (ret == -EOPNOTSUPP)
717 dprintk(DEBUG_NORMAL, "Operation not supported\n");
719 dprintk(DEBUG_NORMAL,
720 "dev->driver->setup failed. (%d)\n", ret);
723 s3c2410_udc_set_ep0_ss(base_addr);
724 s3c2410_udc_set_ep0_de_out(base_addr);
725 dev->ep0state = EP0_IDLE;
726 /* deferred i/o == no response yet */
727 } else if (dev->req_pending) {
728 dprintk(DEBUG_VERBOSE, "dev->req_pending... what now?\n");
729 dev->req_pending = 0;
732 dprintk(DEBUG_VERBOSE, "ep0state %s\n", ep0states[dev->ep0state]);
735 static void s3c2410_udc_handle_ep0(struct s3c2410_udc *dev)
738 struct s3c2410_ep *ep = &dev->ep[0];
739 struct s3c2410_request *req;
740 struct usb_ctrlrequest crq;
742 if (list_empty(&ep->queue))
745 req = list_entry(ep->queue.next, struct s3c2410_request, queue);
747 /* We make the assumption that S3C2410_UDC_IN_CSR1_REG equal to
748 * S3C2410_UDC_EP0_CSR_REG when index is zero */
750 udc_write(0, S3C2410_UDC_INDEX_REG);
751 ep0csr = udc_read(S3C2410_UDC_IN_CSR1_REG);
753 dprintk(DEBUG_NORMAL, "ep0csr %x ep0state %s\n",
754 ep0csr, ep0states[dev->ep0state]);
756 /* clear stall status */
757 if (ep0csr & S3C2410_UDC_EP0_CSR_SENTSTL) {
758 s3c2410_udc_nuke(dev, ep, -EPIPE);
759 dprintk(DEBUG_NORMAL, "... clear SENT_STALL ...\n");
760 s3c2410_udc_clear_ep0_sst(base_addr);
761 dev->ep0state = EP0_IDLE;
765 /* clear setup end */
766 if (ep0csr & S3C2410_UDC_EP0_CSR_SE) {
767 dprintk(DEBUG_NORMAL, "... serviced SETUP_END ...\n");
768 s3c2410_udc_nuke(dev, ep, 0);
769 s3c2410_udc_clear_ep0_se(base_addr);
770 dev->ep0state = EP0_IDLE;
773 switch (dev->ep0state) {
775 s3c2410_udc_handle_ep0_idle(dev, ep, &crq, ep0csr);
778 case EP0_IN_DATA_PHASE: /* GET_DESCRIPTOR etc */
779 dprintk(DEBUG_NORMAL, "EP0_IN_DATA_PHASE ... what now?\n");
780 if (!(ep0csr & S3C2410_UDC_EP0_CSR_IPKRDY) && req)
781 s3c2410_udc_write_fifo(ep, req);
784 case EP0_OUT_DATA_PHASE: /* SET_DESCRIPTOR etc */
785 dprintk(DEBUG_NORMAL, "EP0_OUT_DATA_PHASE ... what now?\n");
786 if ((ep0csr & S3C2410_UDC_EP0_CSR_OPKRDY) && req)
787 s3c2410_udc_read_fifo(ep, req);
791 dprintk(DEBUG_NORMAL, "EP0_END_XFER ... what now?\n");
792 dev->ep0state = EP0_IDLE;
796 dprintk(DEBUG_NORMAL, "EP0_STALL ... what now?\n");
797 dev->ep0state = EP0_IDLE;
803 * handle_ep - Manage I/O endpoints
806 static void s3c2410_udc_handle_ep(struct s3c2410_ep *ep)
808 struct s3c2410_request *req;
809 int is_in = ep->bEndpointAddress & USB_DIR_IN;
813 if (likely(!list_empty(&ep->queue)))
814 req = list_entry(ep->queue.next,
815 struct s3c2410_request, queue);
819 idx = ep->bEndpointAddress & 0x7F;
822 udc_write(idx, S3C2410_UDC_INDEX_REG);
823 ep_csr1 = udc_read(S3C2410_UDC_IN_CSR1_REG);
824 dprintk(DEBUG_VERBOSE, "ep%01d write csr:%02x %d\n",
825 idx, ep_csr1, req ? 1 : 0);
827 if (ep_csr1 & S3C2410_UDC_ICSR1_SENTSTL) {
828 dprintk(DEBUG_VERBOSE, "st\n");
829 udc_write(idx, S3C2410_UDC_INDEX_REG);
830 udc_write(ep_csr1 & ~S3C2410_UDC_ICSR1_SENTSTL,
831 S3C2410_UDC_IN_CSR1_REG);
835 if (!(ep_csr1 & S3C2410_UDC_ICSR1_PKTRDY) && req)
836 s3c2410_udc_write_fifo(ep, req);
838 udc_write(idx, S3C2410_UDC_INDEX_REG);
839 ep_csr1 = udc_read(S3C2410_UDC_OUT_CSR1_REG);
840 dprintk(DEBUG_VERBOSE, "ep%01d rd csr:%02x\n", idx, ep_csr1);
842 if (ep_csr1 & S3C2410_UDC_OCSR1_SENTSTL) {
843 udc_write(idx, S3C2410_UDC_INDEX_REG);
844 udc_write(ep_csr1 & ~S3C2410_UDC_OCSR1_SENTSTL,
845 S3C2410_UDC_OUT_CSR1_REG);
849 if ((ep_csr1 & S3C2410_UDC_OCSR1_PKTRDY) && req)
850 s3c2410_udc_read_fifo(ep, req);
854 #include <mach/regs-irq.h>
857 * s3c2410_udc_irq - interrupt handler
859 static irqreturn_t s3c2410_udc_irq(int dummy, void *_dev)
861 struct s3c2410_udc *dev = _dev;
870 spin_lock_irqsave(&dev->lock, flags);
872 /* Driver connected ? */
874 /* Clear interrupts */
875 udc_write(udc_read(S3C2410_UDC_USB_INT_REG),
876 S3C2410_UDC_USB_INT_REG);
877 udc_write(udc_read(S3C2410_UDC_EP_INT_REG),
878 S3C2410_UDC_EP_INT_REG);
882 idx = udc_read(S3C2410_UDC_INDEX_REG);
884 /* Read status registers */
885 usb_status = udc_read(S3C2410_UDC_USB_INT_REG);
886 usbd_status = udc_read(S3C2410_UDC_EP_INT_REG);
887 pwr_reg = udc_read(S3C2410_UDC_PWR_REG);
889 udc_writeb(base_addr, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
890 ep0csr = udc_read(S3C2410_UDC_IN_CSR1_REG);
892 dprintk(DEBUG_NORMAL, "usbs=%02x, usbds=%02x, pwr=%02x ep0csr=%02x\n",
893 usb_status, usbd_status, pwr_reg, ep0csr);
896 * Now, handle interrupts. There's two types :
897 * - Reset, Resume, Suspend coming -> usb_int_reg
902 if (usb_status & S3C2410_UDC_USBINT_RESET) {
903 /* two kind of reset :
904 * - reset start -> pwr reg = 8
905 * - reset end -> pwr reg = 0
907 dprintk(DEBUG_NORMAL, "USB reset csr %x pwr %x\n",
910 dev->gadget.speed = USB_SPEED_UNKNOWN;
911 udc_write(0x00, S3C2410_UDC_INDEX_REG);
912 udc_write((dev->ep[0].ep.maxpacket & 0x7ff) >> 3,
913 S3C2410_UDC_MAXP_REG);
916 dev->ep0state = EP0_IDLE;
917 dev->gadget.speed = USB_SPEED_FULL;
919 /* clear interrupt */
920 udc_write(S3C2410_UDC_USBINT_RESET,
921 S3C2410_UDC_USB_INT_REG);
923 udc_write(idx, S3C2410_UDC_INDEX_REG);
924 spin_unlock_irqrestore(&dev->lock, flags);
929 if (usb_status & S3C2410_UDC_USBINT_RESUME) {
930 dprintk(DEBUG_NORMAL, "USB resume\n");
932 /* clear interrupt */
933 udc_write(S3C2410_UDC_USBINT_RESUME,
934 S3C2410_UDC_USB_INT_REG);
936 if (dev->gadget.speed != USB_SPEED_UNKNOWN
938 && dev->driver->resume)
939 dev->driver->resume(&dev->gadget);
943 if (usb_status & S3C2410_UDC_USBINT_SUSPEND) {
944 dprintk(DEBUG_NORMAL, "USB suspend\n");
946 /* clear interrupt */
947 udc_write(S3C2410_UDC_USBINT_SUSPEND,
948 S3C2410_UDC_USB_INT_REG);
950 if (dev->gadget.speed != USB_SPEED_UNKNOWN
952 && dev->driver->suspend)
953 dev->driver->suspend(&dev->gadget);
955 dev->ep0state = EP0_IDLE;
959 /* control traffic */
960 /* check on ep0csr != 0 is not a good idea as clearing in_pkt_ready
961 * generate an interrupt
963 if (usbd_status & S3C2410_UDC_INT_EP0) {
964 dprintk(DEBUG_VERBOSE, "USB ep0 irq\n");
965 /* Clear the interrupt bit by setting it to 1 */
966 udc_write(S3C2410_UDC_INT_EP0, S3C2410_UDC_EP_INT_REG);
967 s3c2410_udc_handle_ep0(dev);
970 /* endpoint data transfers */
971 for (i = 1; i < S3C2410_ENDPOINTS; i++) {
973 if (usbd_status & tmp) {
974 dprintk(DEBUG_VERBOSE, "USB ep%d irq\n", i);
976 /* Clear the interrupt bit by setting it to 1 */
977 udc_write(tmp, S3C2410_UDC_EP_INT_REG);
978 s3c2410_udc_handle_ep(&dev->ep[i]);
982 /* what else causes this interrupt? a receive! who is it? */
983 if (!usb_status && !usbd_status && !pwr_reg && !ep0csr) {
984 for (i = 1; i < S3C2410_ENDPOINTS; i++) {
985 idx2 = udc_read(S3C2410_UDC_INDEX_REG);
986 udc_write(i, S3C2410_UDC_INDEX_REG);
988 if (udc_read(S3C2410_UDC_OUT_CSR1_REG) & 0x1)
989 s3c2410_udc_handle_ep(&dev->ep[i]);
992 udc_write(idx2, S3C2410_UDC_INDEX_REG);
996 dprintk(DEBUG_VERBOSE, "irq: %d s3c2410_udc_done.\n", IRQ_USBD);
998 /* Restore old index */
999 udc_write(idx, S3C2410_UDC_INDEX_REG);
1001 spin_unlock_irqrestore(&dev->lock, flags);
1005 /*------------------------- s3c2410_ep_ops ----------------------------------*/
1007 static inline struct s3c2410_ep *to_s3c2410_ep(struct usb_ep *ep)
1009 return container_of(ep, struct s3c2410_ep, ep);
1012 static inline struct s3c2410_udc *to_s3c2410_udc(struct usb_gadget *gadget)
1014 return container_of(gadget, struct s3c2410_udc, gadget);
1017 static inline struct s3c2410_request *to_s3c2410_req(struct usb_request *req)
1019 return container_of(req, struct s3c2410_request, req);
1023 * s3c2410_udc_ep_enable
1025 static int s3c2410_udc_ep_enable(struct usb_ep *_ep,
1026 const struct usb_endpoint_descriptor *desc)
1028 struct s3c2410_udc *dev;
1029 struct s3c2410_ep *ep;
1031 unsigned long flags;
1035 ep = to_s3c2410_ep(_ep);
1038 || _ep->name == ep0name
1039 || desc->bDescriptorType != USB_DT_ENDPOINT)
1043 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
1046 max = usb_endpoint_maxp(desc) & 0x1fff;
1048 local_irq_save(flags);
1049 _ep->maxpacket = max & 0x7ff;
1052 ep->bEndpointAddress = desc->bEndpointAddress;
1054 /* set max packet */
1055 udc_write(ep->num, S3C2410_UDC_INDEX_REG);
1056 udc_write(max >> 3, S3C2410_UDC_MAXP_REG);
1058 /* set type, direction, address; reset fifo counters */
1059 if (desc->bEndpointAddress & USB_DIR_IN) {
1060 csr1 = S3C2410_UDC_ICSR1_FFLUSH|S3C2410_UDC_ICSR1_CLRDT;
1061 csr2 = S3C2410_UDC_ICSR2_MODEIN|S3C2410_UDC_ICSR2_DMAIEN;
1063 udc_write(ep->num, S3C2410_UDC_INDEX_REG);
1064 udc_write(csr1, S3C2410_UDC_IN_CSR1_REG);
1065 udc_write(ep->num, S3C2410_UDC_INDEX_REG);
1066 udc_write(csr2, S3C2410_UDC_IN_CSR2_REG);
1068 /* don't flush in fifo or it will cause endpoint interrupt */
1069 csr1 = S3C2410_UDC_ICSR1_CLRDT;
1070 csr2 = S3C2410_UDC_ICSR2_DMAIEN;
1072 udc_write(ep->num, S3C2410_UDC_INDEX_REG);
1073 udc_write(csr1, S3C2410_UDC_IN_CSR1_REG);
1074 udc_write(ep->num, S3C2410_UDC_INDEX_REG);
1075 udc_write(csr2, S3C2410_UDC_IN_CSR2_REG);
1077 csr1 = S3C2410_UDC_OCSR1_FFLUSH | S3C2410_UDC_OCSR1_CLRDT;
1078 csr2 = S3C2410_UDC_OCSR2_DMAIEN;
1080 udc_write(ep->num, S3C2410_UDC_INDEX_REG);
1081 udc_write(csr1, S3C2410_UDC_OUT_CSR1_REG);
1082 udc_write(ep->num, S3C2410_UDC_INDEX_REG);
1083 udc_write(csr2, S3C2410_UDC_OUT_CSR2_REG);
1087 int_en_reg = udc_read(S3C2410_UDC_EP_INT_EN_REG);
1088 udc_write(int_en_reg | (1 << ep->num), S3C2410_UDC_EP_INT_EN_REG);
1090 /* print some debug message */
1091 tmp = desc->bEndpointAddress;
1092 dprintk(DEBUG_NORMAL, "enable %s(%d) ep%x%s-blk max %02x\n",
1093 _ep->name, ep->num, tmp,
1094 desc->bEndpointAddress & USB_DIR_IN ? "in" : "out", max);
1096 local_irq_restore(flags);
1097 s3c2410_udc_set_halt(_ep, 0);
1103 * s3c2410_udc_ep_disable
1105 static int s3c2410_udc_ep_disable(struct usb_ep *_ep)
1107 struct s3c2410_ep *ep = to_s3c2410_ep(_ep);
1108 unsigned long flags;
1111 if (!_ep || !ep->ep.desc) {
1112 dprintk(DEBUG_NORMAL, "%s not enabled\n",
1113 _ep ? ep->ep.name : NULL);
1117 local_irq_save(flags);
1119 dprintk(DEBUG_NORMAL, "ep_disable: %s\n", _ep->name);
1124 s3c2410_udc_nuke(ep->dev, ep, -ESHUTDOWN);
1127 int_en_reg = udc_read(S3C2410_UDC_EP_INT_EN_REG);
1128 udc_write(int_en_reg & ~(1<<ep->num), S3C2410_UDC_EP_INT_EN_REG);
1130 local_irq_restore(flags);
1132 dprintk(DEBUG_NORMAL, "%s disabled\n", _ep->name);
1138 * s3c2410_udc_alloc_request
1140 static struct usb_request *
1141 s3c2410_udc_alloc_request(struct usb_ep *_ep, gfp_t mem_flags)
1143 struct s3c2410_request *req;
1145 dprintk(DEBUG_VERBOSE, "%s(%p,%d)\n", __func__, _ep, mem_flags);
1150 req = kzalloc(sizeof(struct s3c2410_request), mem_flags);
1154 INIT_LIST_HEAD(&req->queue);
1159 * s3c2410_udc_free_request
1162 s3c2410_udc_free_request(struct usb_ep *_ep, struct usb_request *_req)
1164 struct s3c2410_ep *ep = to_s3c2410_ep(_ep);
1165 struct s3c2410_request *req = to_s3c2410_req(_req);
1167 dprintk(DEBUG_VERBOSE, "%s(%p,%p)\n", __func__, _ep, _req);
1169 if (!ep || !_req || (!ep->ep.desc && _ep->name != ep0name))
1172 WARN_ON(!list_empty(&req->queue));
1179 static int s3c2410_udc_queue(struct usb_ep *_ep, struct usb_request *_req,
1182 struct s3c2410_request *req = to_s3c2410_req(_req);
1183 struct s3c2410_ep *ep = to_s3c2410_ep(_ep);
1184 struct s3c2410_udc *dev;
1187 unsigned long flags;
1189 if (unlikely(!_ep || (!ep->ep.desc && ep->ep.name != ep0name))) {
1190 dprintk(DEBUG_NORMAL, "%s: invalid args\n", __func__);
1195 if (unlikely(!dev->driver
1196 || dev->gadget.speed == USB_SPEED_UNKNOWN)) {
1200 local_irq_save(flags);
1202 if (unlikely(!_req || !_req->complete
1203 || !_req->buf || !list_empty(&req->queue))) {
1205 dprintk(DEBUG_NORMAL, "%s: 1 X X X\n", __func__);
1207 dprintk(DEBUG_NORMAL, "%s: 0 %01d %01d %01d\n",
1208 __func__, !_req->complete, !_req->buf,
1209 !list_empty(&req->queue));
1212 local_irq_restore(flags);
1216 _req->status = -EINPROGRESS;
1219 dprintk(DEBUG_VERBOSE, "%s: ep%x len %d\n",
1220 __func__, ep->bEndpointAddress, _req->length);
1222 if (ep->bEndpointAddress) {
1223 udc_write(ep->bEndpointAddress & 0x7F, S3C2410_UDC_INDEX_REG);
1225 ep_csr = udc_read((ep->bEndpointAddress & USB_DIR_IN)
1226 ? S3C2410_UDC_IN_CSR1_REG
1227 : S3C2410_UDC_OUT_CSR1_REG);
1228 fifo_count = s3c2410_udc_fifo_count_out();
1230 udc_write(0, S3C2410_UDC_INDEX_REG);
1231 ep_csr = udc_read(S3C2410_UDC_IN_CSR1_REG);
1232 fifo_count = s3c2410_udc_fifo_count_out();
1235 /* kickstart this i/o queue? */
1236 if (list_empty(&ep->queue) && !ep->halted) {
1237 if (ep->bEndpointAddress == 0 /* ep0 */) {
1238 switch (dev->ep0state) {
1239 case EP0_IN_DATA_PHASE:
1240 if (!(ep_csr&S3C2410_UDC_EP0_CSR_IPKRDY)
1241 && s3c2410_udc_write_fifo(ep,
1243 dev->ep0state = EP0_IDLE;
1248 case EP0_OUT_DATA_PHASE:
1250 || ((ep_csr & S3C2410_UDC_OCSR1_PKTRDY)
1251 && s3c2410_udc_read_fifo(ep,
1253 dev->ep0state = EP0_IDLE;
1259 local_irq_restore(flags);
1262 } else if ((ep->bEndpointAddress & USB_DIR_IN) != 0
1263 && (!(ep_csr&S3C2410_UDC_OCSR1_PKTRDY))
1264 && s3c2410_udc_write_fifo(ep, req)) {
1266 } else if ((ep_csr & S3C2410_UDC_OCSR1_PKTRDY)
1268 && s3c2410_udc_read_fifo(ep, req)) {
1273 /* pio or dma irq handler advances the queue. */
1275 list_add_tail(&req->queue, &ep->queue);
1277 local_irq_restore(flags);
1279 dprintk(DEBUG_VERBOSE, "%s ok\n", __func__);
1284 * s3c2410_udc_dequeue
1286 static int s3c2410_udc_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1288 struct s3c2410_ep *ep = to_s3c2410_ep(_ep);
1289 struct s3c2410_udc *udc;
1290 int retval = -EINVAL;
1291 unsigned long flags;
1292 struct s3c2410_request *req = NULL;
1294 dprintk(DEBUG_VERBOSE, "%s(%p,%p)\n", __func__, _ep, _req);
1296 if (!the_controller->driver)
1302 udc = to_s3c2410_udc(ep->gadget);
1304 local_irq_save(flags);
1306 list_for_each_entry(req, &ep->queue, queue) {
1307 if (&req->req == _req) {
1308 list_del_init(&req->queue);
1309 _req->status = -ECONNRESET;
1316 dprintk(DEBUG_VERBOSE,
1317 "dequeued req %p from %s, len %d buf %p\n",
1318 req, _ep->name, _req->length, _req->buf);
1320 s3c2410_udc_done(ep, req, -ECONNRESET);
1323 local_irq_restore(flags);
1328 * s3c2410_udc_set_halt
1330 static int s3c2410_udc_set_halt(struct usb_ep *_ep, int value)
1332 struct s3c2410_ep *ep = to_s3c2410_ep(_ep);
1334 unsigned long flags;
1337 if (unlikely(!_ep || (!ep->ep.desc && ep->ep.name != ep0name))) {
1338 dprintk(DEBUG_NORMAL, "%s: inval 2\n", __func__);
1342 local_irq_save(flags);
1344 idx = ep->bEndpointAddress & 0x7F;
1347 s3c2410_udc_set_ep0_ss(base_addr);
1348 s3c2410_udc_set_ep0_de_out(base_addr);
1350 udc_write(idx, S3C2410_UDC_INDEX_REG);
1351 ep_csr = udc_read((ep->bEndpointAddress & USB_DIR_IN)
1352 ? S3C2410_UDC_IN_CSR1_REG
1353 : S3C2410_UDC_OUT_CSR1_REG);
1355 if ((ep->bEndpointAddress & USB_DIR_IN) != 0) {
1357 udc_write(ep_csr | S3C2410_UDC_ICSR1_SENDSTL,
1358 S3C2410_UDC_IN_CSR1_REG);
1360 ep_csr &= ~S3C2410_UDC_ICSR1_SENDSTL;
1361 udc_write(ep_csr, S3C2410_UDC_IN_CSR1_REG);
1362 ep_csr |= S3C2410_UDC_ICSR1_CLRDT;
1363 udc_write(ep_csr, S3C2410_UDC_IN_CSR1_REG);
1367 udc_write(ep_csr | S3C2410_UDC_OCSR1_SENDSTL,
1368 S3C2410_UDC_OUT_CSR1_REG);
1370 ep_csr &= ~S3C2410_UDC_OCSR1_SENDSTL;
1371 udc_write(ep_csr, S3C2410_UDC_OUT_CSR1_REG);
1372 ep_csr |= S3C2410_UDC_OCSR1_CLRDT;
1373 udc_write(ep_csr, S3C2410_UDC_OUT_CSR1_REG);
1378 ep->halted = value ? 1 : 0;
1379 local_irq_restore(flags);
1384 static const struct usb_ep_ops s3c2410_ep_ops = {
1385 .enable = s3c2410_udc_ep_enable,
1386 .disable = s3c2410_udc_ep_disable,
1388 .alloc_request = s3c2410_udc_alloc_request,
1389 .free_request = s3c2410_udc_free_request,
1391 .queue = s3c2410_udc_queue,
1392 .dequeue = s3c2410_udc_dequeue,
1394 .set_halt = s3c2410_udc_set_halt,
1397 /*------------------------- usb_gadget_ops ----------------------------------*/
1400 * s3c2410_udc_get_frame
1402 static int s3c2410_udc_get_frame(struct usb_gadget *_gadget)
1406 dprintk(DEBUG_VERBOSE, "%s()\n", __func__);
1408 tmp = udc_read(S3C2410_UDC_FRAME_NUM2_REG) << 8;
1409 tmp |= udc_read(S3C2410_UDC_FRAME_NUM1_REG);
1414 * s3c2410_udc_wakeup
1416 static int s3c2410_udc_wakeup(struct usb_gadget *_gadget)
1418 dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1423 * s3c2410_udc_set_selfpowered
1425 static int s3c2410_udc_set_selfpowered(struct usb_gadget *gadget, int value)
1427 struct s3c2410_udc *udc = to_s3c2410_udc(gadget);
1429 dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1431 gadget->is_selfpowered = (value != 0);
1433 udc->devstatus |= (1 << USB_DEVICE_SELF_POWERED);
1435 udc->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
1440 static void s3c2410_udc_disable(struct s3c2410_udc *dev);
1441 static void s3c2410_udc_enable(struct s3c2410_udc *dev);
1443 static int s3c2410_udc_set_pullup(struct s3c2410_udc *udc, int is_on)
1445 dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1447 if (udc_info && (udc_info->udc_command ||
1448 gpio_is_valid(udc_info->pullup_pin))) {
1451 s3c2410_udc_enable(udc);
1453 if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
1454 if (udc->driver && udc->driver->disconnect)
1455 udc->driver->disconnect(&udc->gadget);
1458 s3c2410_udc_disable(udc);
1467 static int s3c2410_udc_vbus_session(struct usb_gadget *gadget, int is_active)
1469 struct s3c2410_udc *udc = to_s3c2410_udc(gadget);
1471 dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1473 udc->vbus = (is_active != 0);
1474 s3c2410_udc_set_pullup(udc, is_active);
1478 static int s3c2410_udc_pullup(struct usb_gadget *gadget, int is_on)
1480 struct s3c2410_udc *udc = to_s3c2410_udc(gadget);
1482 dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1484 s3c2410_udc_set_pullup(udc, is_on);
1488 static irqreturn_t s3c2410_udc_vbus_irq(int irq, void *_dev)
1490 struct s3c2410_udc *dev = _dev;
1493 dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1495 value = gpio_get_value(udc_info->vbus_pin) ? 1 : 0;
1496 if (udc_info->vbus_pin_inverted)
1499 if (value != dev->vbus)
1500 s3c2410_udc_vbus_session(&dev->gadget, value);
1505 static int s3c2410_vbus_draw(struct usb_gadget *_gadget, unsigned ma)
1507 dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1509 if (udc_info && udc_info->vbus_draw) {
1510 udc_info->vbus_draw(ma);
1517 static int s3c2410_udc_start(struct usb_gadget *g,
1518 struct usb_gadget_driver *driver);
1519 static int s3c2410_udc_stop(struct usb_gadget *g);
1521 static const struct usb_gadget_ops s3c2410_ops = {
1522 .get_frame = s3c2410_udc_get_frame,
1523 .wakeup = s3c2410_udc_wakeup,
1524 .set_selfpowered = s3c2410_udc_set_selfpowered,
1525 .pullup = s3c2410_udc_pullup,
1526 .vbus_session = s3c2410_udc_vbus_session,
1527 .vbus_draw = s3c2410_vbus_draw,
1528 .udc_start = s3c2410_udc_start,
1529 .udc_stop = s3c2410_udc_stop,
1532 static void s3c2410_udc_command(enum s3c2410_udc_cmd_e cmd)
1537 if (udc_info->udc_command) {
1538 udc_info->udc_command(cmd);
1539 } else if (gpio_is_valid(udc_info->pullup_pin)) {
1543 case S3C2410_UDC_P_ENABLE:
1546 case S3C2410_UDC_P_DISABLE:
1552 value ^= udc_info->pullup_pin_inverted;
1554 gpio_set_value(udc_info->pullup_pin, value);
1558 /*------------------------- gadget driver handling---------------------------*/
1560 * s3c2410_udc_disable
1562 static void s3c2410_udc_disable(struct s3c2410_udc *dev)
1564 dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1566 /* Disable all interrupts */
1567 udc_write(0x00, S3C2410_UDC_USB_INT_EN_REG);
1568 udc_write(0x00, S3C2410_UDC_EP_INT_EN_REG);
1570 /* Clear the interrupt registers */
1571 udc_write(S3C2410_UDC_USBINT_RESET
1572 | S3C2410_UDC_USBINT_RESUME
1573 | S3C2410_UDC_USBINT_SUSPEND,
1574 S3C2410_UDC_USB_INT_REG);
1576 udc_write(0x1F, S3C2410_UDC_EP_INT_REG);
1578 /* Good bye, cruel world */
1579 s3c2410_udc_command(S3C2410_UDC_P_DISABLE);
1581 /* Set speed to unknown */
1582 dev->gadget.speed = USB_SPEED_UNKNOWN;
1586 * s3c2410_udc_reinit
1588 static void s3c2410_udc_reinit(struct s3c2410_udc *dev)
1592 /* device/ep0 records init */
1593 INIT_LIST_HEAD(&dev->gadget.ep_list);
1594 INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
1595 dev->ep0state = EP0_IDLE;
1597 for (i = 0; i < S3C2410_ENDPOINTS; i++) {
1598 struct s3c2410_ep *ep = &dev->ep[i];
1601 list_add_tail(&ep->ep.ep_list, &dev->gadget.ep_list);
1606 INIT_LIST_HEAD(&ep->queue);
1607 usb_ep_set_maxpacket_limit(&ep->ep, ep->ep.maxpacket);
1612 * s3c2410_udc_enable
1614 static void s3c2410_udc_enable(struct s3c2410_udc *dev)
1618 dprintk(DEBUG_NORMAL, "s3c2410_udc_enable called\n");
1620 /* dev->gadget.speed = USB_SPEED_UNKNOWN; */
1621 dev->gadget.speed = USB_SPEED_FULL;
1623 /* Set MAXP for all endpoints */
1624 for (i = 0; i < S3C2410_ENDPOINTS; i++) {
1625 udc_write(i, S3C2410_UDC_INDEX_REG);
1626 udc_write((dev->ep[i].ep.maxpacket & 0x7ff) >> 3,
1627 S3C2410_UDC_MAXP_REG);
1630 /* Set default power state */
1631 udc_write(DEFAULT_POWER_STATE, S3C2410_UDC_PWR_REG);
1633 /* Enable reset and suspend interrupt interrupts */
1634 udc_write(S3C2410_UDC_USBINT_RESET | S3C2410_UDC_USBINT_SUSPEND,
1635 S3C2410_UDC_USB_INT_EN_REG);
1637 /* Enable ep0 interrupt */
1638 udc_write(S3C2410_UDC_INT_EP0, S3C2410_UDC_EP_INT_EN_REG);
1640 /* time to say "hello, world" */
1641 s3c2410_udc_command(S3C2410_UDC_P_ENABLE);
1644 static int s3c2410_udc_start(struct usb_gadget *g,
1645 struct usb_gadget_driver *driver)
1647 struct s3c2410_udc *udc = to_s3c2410(g);
1649 dprintk(DEBUG_NORMAL, "%s() '%s'\n", __func__, driver->driver.name);
1651 /* Hook the driver */
1652 udc->driver = driver;
1655 s3c2410_udc_enable(udc);
1660 static int s3c2410_udc_stop(struct usb_gadget *g)
1662 struct s3c2410_udc *udc = to_s3c2410(g);
1667 s3c2410_udc_disable(udc);
1672 /*---------------------------------------------------------------------------*/
1673 static struct s3c2410_udc memory = {
1675 .ops = &s3c2410_ops,
1676 .ep0 = &memory.ep[0].ep,
1677 .name = gadget_name,
1679 .init_name = "gadget",
1683 /* control endpoint */
1688 .ops = &s3c2410_ep_ops,
1689 .maxpacket = EP0_FIFO_SIZE,
1690 .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_CONTROL,
1691 USB_EP_CAPS_DIR_ALL),
1696 /* first group of endpoints */
1701 .ops = &s3c2410_ep_ops,
1702 .maxpacket = EP_FIFO_SIZE,
1703 .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK,
1704 USB_EP_CAPS_DIR_ALL),
1707 .fifo_size = EP_FIFO_SIZE,
1708 .bEndpointAddress = 1,
1709 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1715 .ops = &s3c2410_ep_ops,
1716 .maxpacket = EP_FIFO_SIZE,
1717 .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK,
1718 USB_EP_CAPS_DIR_ALL),
1721 .fifo_size = EP_FIFO_SIZE,
1722 .bEndpointAddress = 2,
1723 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1729 .ops = &s3c2410_ep_ops,
1730 .maxpacket = EP_FIFO_SIZE,
1731 .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK,
1732 USB_EP_CAPS_DIR_ALL),
1735 .fifo_size = EP_FIFO_SIZE,
1736 .bEndpointAddress = 3,
1737 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1743 .ops = &s3c2410_ep_ops,
1744 .maxpacket = EP_FIFO_SIZE,
1745 .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK,
1746 USB_EP_CAPS_DIR_ALL),
1749 .fifo_size = EP_FIFO_SIZE,
1750 .bEndpointAddress = 4,
1751 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1757 * probe - binds to the platform device
1759 static int s3c2410_udc_probe(struct platform_device *pdev)
1761 struct s3c2410_udc *udc = &memory;
1762 struct device *dev = &pdev->dev;
1766 dev_dbg(dev, "%s()\n", __func__);
1768 usb_bus_clock = clk_get(NULL, "usb-bus-gadget");
1769 if (IS_ERR(usb_bus_clock)) {
1770 dev_err(dev, "failed to get usb bus clock source\n");
1771 return PTR_ERR(usb_bus_clock);
1774 clk_prepare_enable(usb_bus_clock);
1776 udc_clock = clk_get(NULL, "usb-device");
1777 if (IS_ERR(udc_clock)) {
1778 dev_err(dev, "failed to get udc clock source\n");
1779 return PTR_ERR(udc_clock);
1782 clk_prepare_enable(udc_clock);
1786 dev_dbg(dev, "got and enabled clocks\n");
1788 if (strncmp(pdev->name, "s3c2440", 7) == 0) {
1789 dev_info(dev, "S3C2440: increasing FIFO to 128 bytes\n");
1790 memory.ep[1].fifo_size = S3C2440_EP_FIFO_SIZE;
1791 memory.ep[2].fifo_size = S3C2440_EP_FIFO_SIZE;
1792 memory.ep[3].fifo_size = S3C2440_EP_FIFO_SIZE;
1793 memory.ep[4].fifo_size = S3C2440_EP_FIFO_SIZE;
1796 spin_lock_init(&udc->lock);
1797 udc_info = dev_get_platdata(&pdev->dev);
1799 rsrc_start = S3C2410_PA_USBDEV;
1800 rsrc_len = S3C24XX_SZ_USBDEV;
1802 if (!request_mem_region(rsrc_start, rsrc_len, gadget_name))
1805 base_addr = ioremap(rsrc_start, rsrc_len);
1811 the_controller = udc;
1812 platform_set_drvdata(pdev, udc);
1814 s3c2410_udc_disable(udc);
1815 s3c2410_udc_reinit(udc);
1817 /* irq setup after old hardware state is cleaned up */
1818 retval = request_irq(IRQ_USBD, s3c2410_udc_irq,
1819 0, gadget_name, udc);
1822 dev_err(dev, "cannot get irq %i, err %d\n", IRQ_USBD, retval);
1827 dev_dbg(dev, "got irq %i\n", IRQ_USBD);
1829 if (udc_info && udc_info->vbus_pin > 0) {
1830 retval = gpio_request(udc_info->vbus_pin, "udc vbus");
1832 dev_err(dev, "cannot claim vbus pin\n");
1836 irq = gpio_to_irq(udc_info->vbus_pin);
1838 dev_err(dev, "no irq for gpio vbus pin\n");
1840 goto err_gpio_claim;
1843 retval = request_irq(irq, s3c2410_udc_vbus_irq,
1845 | IRQF_TRIGGER_FALLING | IRQF_SHARED,
1849 dev_err(dev, "can't get vbus irq %d, err %d\n",
1852 goto err_gpio_claim;
1855 dev_dbg(dev, "got irq %i\n", irq);
1860 if (udc_info && !udc_info->udc_command &&
1861 gpio_is_valid(udc_info->pullup_pin)) {
1863 retval = gpio_request_one(udc_info->pullup_pin,
1864 udc_info->vbus_pin_inverted ?
1865 GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW,
1871 retval = usb_add_gadget_udc(&pdev->dev, &udc->gadget);
1875 if (s3c2410_udc_debugfs_root) {
1876 udc->regs_info = debugfs_create_file("registers", S_IRUGO,
1877 s3c2410_udc_debugfs_root,
1878 udc, &s3c2410_udc_debugfs_fops);
1879 if (!udc->regs_info)
1880 dev_warn(dev, "debugfs file creation failed\n");
1883 dev_dbg(dev, "probe ok\n");
1888 if (udc_info && !udc_info->udc_command &&
1889 gpio_is_valid(udc_info->pullup_pin))
1890 gpio_free(udc_info->pullup_pin);
1892 if (udc_info && udc_info->vbus_pin > 0)
1893 free_irq(gpio_to_irq(udc_info->vbus_pin), udc);
1895 if (udc_info && udc_info->vbus_pin > 0)
1896 gpio_free(udc_info->vbus_pin);
1898 free_irq(IRQ_USBD, udc);
1902 release_mem_region(rsrc_start, rsrc_len);
1908 * s3c2410_udc_remove
1910 static int s3c2410_udc_remove(struct platform_device *pdev)
1912 struct s3c2410_udc *udc = platform_get_drvdata(pdev);
1915 dev_dbg(&pdev->dev, "%s()\n", __func__);
1920 usb_del_gadget_udc(&udc->gadget);
1921 debugfs_remove(udc->regs_info);
1923 if (udc_info && !udc_info->udc_command &&
1924 gpio_is_valid(udc_info->pullup_pin))
1925 gpio_free(udc_info->pullup_pin);
1927 if (udc_info && udc_info->vbus_pin > 0) {
1928 irq = gpio_to_irq(udc_info->vbus_pin);
1932 free_irq(IRQ_USBD, udc);
1935 release_mem_region(rsrc_start, rsrc_len);
1937 if (!IS_ERR(udc_clock) && udc_clock != NULL) {
1938 clk_disable_unprepare(udc_clock);
1943 if (!IS_ERR(usb_bus_clock) && usb_bus_clock != NULL) {
1944 clk_disable_unprepare(usb_bus_clock);
1945 clk_put(usb_bus_clock);
1946 usb_bus_clock = NULL;
1949 dev_dbg(&pdev->dev, "%s: remove ok\n", __func__);
1955 s3c2410_udc_suspend(struct platform_device *pdev, pm_message_t message)
1957 s3c2410_udc_command(S3C2410_UDC_P_DISABLE);
1962 static int s3c2410_udc_resume(struct platform_device *pdev)
1964 s3c2410_udc_command(S3C2410_UDC_P_ENABLE);
1969 #define s3c2410_udc_suspend NULL
1970 #define s3c2410_udc_resume NULL
1973 static const struct platform_device_id s3c_udc_ids[] = {
1974 { "s3c2410-usbgadget", },
1975 { "s3c2440-usbgadget", },
1978 MODULE_DEVICE_TABLE(platform, s3c_udc_ids);
1980 static struct platform_driver udc_driver_24x0 = {
1982 .name = "s3c24x0-usbgadget",
1984 .probe = s3c2410_udc_probe,
1985 .remove = s3c2410_udc_remove,
1986 .suspend = s3c2410_udc_suspend,
1987 .resume = s3c2410_udc_resume,
1988 .id_table = s3c_udc_ids,
1991 static int __init udc_init(void)
1995 dprintk(DEBUG_NORMAL, "%s: version %s\n", gadget_name, DRIVER_VERSION);
1997 s3c2410_udc_debugfs_root = debugfs_create_dir(gadget_name, NULL);
1998 if (IS_ERR(s3c2410_udc_debugfs_root)) {
1999 pr_err("%s: debugfs dir creation failed %ld\n",
2000 gadget_name, PTR_ERR(s3c2410_udc_debugfs_root));
2001 s3c2410_udc_debugfs_root = NULL;
2004 retval = platform_driver_register(&udc_driver_24x0);
2011 debugfs_remove(s3c2410_udc_debugfs_root);
2015 static void __exit udc_exit(void)
2017 platform_driver_unregister(&udc_driver_24x0);
2018 debugfs_remove(s3c2410_udc_debugfs_root);
2021 module_init(udc_init);
2022 module_exit(udc_exit);
2024 MODULE_AUTHOR(DRIVER_AUTHOR);
2025 MODULE_DESCRIPTION(DRIVER_DESC);
2026 MODULE_VERSION(DRIVER_VERSION);
2027 MODULE_LICENSE("GPL");