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_de_in(void __iomem *base)
243 udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
244 udc_writeb(base, (S3C2410_UDC_EP0_CSR_IPKRDY
245 | S3C2410_UDC_EP0_CSR_DE),
246 S3C2410_UDC_EP0_CSR_REG);
249 /*------------------------- I/O ----------------------------------*/
254 static void s3c2410_udc_done(struct s3c2410_ep *ep,
255 struct s3c2410_request *req, int status)
257 unsigned halted = ep->halted;
259 list_del_init(&req->queue);
261 if (likely(req->req.status == -EINPROGRESS))
262 req->req.status = status;
264 status = req->req.status;
267 usb_gadget_giveback_request(&ep->ep, &req->req);
271 static void s3c2410_udc_nuke(struct s3c2410_udc *udc,
272 struct s3c2410_ep *ep, int status)
275 if (&ep->queue == NULL)
278 while (!list_empty(&ep->queue)) {
279 struct s3c2410_request *req;
280 req = list_entry(ep->queue.next, struct s3c2410_request,
282 s3c2410_udc_done(ep, req, status);
286 static inline int s3c2410_udc_fifo_count_out(void)
290 tmp = udc_read(S3C2410_UDC_OUT_FIFO_CNT2_REG) << 8;
291 tmp |= udc_read(S3C2410_UDC_OUT_FIFO_CNT1_REG);
296 * s3c2410_udc_write_packet
298 static inline int s3c2410_udc_write_packet(int fifo,
299 struct s3c2410_request *req,
302 unsigned len = min(req->req.length - req->req.actual, max);
303 u8 *buf = req->req.buf + req->req.actual;
307 dprintk(DEBUG_VERBOSE, "%s %d %d %d %d\n", __func__,
308 req->req.actual, req->req.length, len, req->req.actual + len);
310 req->req.actual += len;
313 writesb(base_addr + fifo, buf, len);
318 * s3c2410_udc_write_fifo
320 * return: 0 = still running, 1 = completed, negative = errno
322 static int s3c2410_udc_write_fifo(struct s3c2410_ep *ep,
323 struct s3c2410_request *req)
331 idx = ep->bEndpointAddress & 0x7F;
336 fifo_reg = S3C2410_UDC_EP0_FIFO_REG;
339 fifo_reg = S3C2410_UDC_EP1_FIFO_REG;
342 fifo_reg = S3C2410_UDC_EP2_FIFO_REG;
345 fifo_reg = S3C2410_UDC_EP3_FIFO_REG;
348 fifo_reg = S3C2410_UDC_EP4_FIFO_REG;
352 count = s3c2410_udc_write_packet(fifo_reg, req, ep->ep.maxpacket);
354 /* last packet is often short (sometimes a zlp) */
355 if (count != ep->ep.maxpacket)
357 else if (req->req.length != req->req.actual || req->req.zero)
362 /* Only ep0 debug messages are interesting */
364 dprintk(DEBUG_NORMAL,
365 "Written ep%d %d.%d of %d b [last %d,z %d]\n",
366 idx, count, req->req.actual, req->req.length,
367 is_last, req->req.zero);
370 /* The order is important. It prevents sending 2 packets
371 * at the same time */
374 /* Reset signal => no need to say 'data sent' */
375 if (!(udc_read(S3C2410_UDC_USB_INT_REG)
376 & S3C2410_UDC_USBINT_RESET))
377 s3c2410_udc_set_ep0_de_in(base_addr);
378 ep->dev->ep0state = EP0_IDLE;
380 udc_write(idx, S3C2410_UDC_INDEX_REG);
381 ep_csr = udc_read(S3C2410_UDC_IN_CSR1_REG);
382 udc_write(idx, S3C2410_UDC_INDEX_REG);
383 udc_write(ep_csr | S3C2410_UDC_ICSR1_PKTRDY,
384 S3C2410_UDC_IN_CSR1_REG);
387 s3c2410_udc_done(ep, req, 0);
391 /* Reset signal => no need to say 'data sent' */
392 if (!(udc_read(S3C2410_UDC_USB_INT_REG)
393 & S3C2410_UDC_USBINT_RESET))
394 s3c2410_udc_set_ep0_ipr(base_addr);
396 udc_write(idx, S3C2410_UDC_INDEX_REG);
397 ep_csr = udc_read(S3C2410_UDC_IN_CSR1_REG);
398 udc_write(idx, S3C2410_UDC_INDEX_REG);
399 udc_write(ep_csr | S3C2410_UDC_ICSR1_PKTRDY,
400 S3C2410_UDC_IN_CSR1_REG);
407 static inline int s3c2410_udc_read_packet(int fifo, u8 *buf,
408 struct s3c2410_request *req, unsigned avail)
412 len = min(req->req.length - req->req.actual, avail);
413 req->req.actual += len;
415 readsb(fifo + base_addr, buf, len);
420 * return: 0 = still running, 1 = queue empty, negative = errno
422 static int s3c2410_udc_read_fifo(struct s3c2410_ep *ep,
423 struct s3c2410_request *req)
427 unsigned bufferspace;
434 idx = ep->bEndpointAddress & 0x7F;
440 fifo_reg = S3C2410_UDC_EP0_FIFO_REG;
443 fifo_reg = S3C2410_UDC_EP1_FIFO_REG;
446 fifo_reg = S3C2410_UDC_EP2_FIFO_REG;
449 fifo_reg = S3C2410_UDC_EP3_FIFO_REG;
452 fifo_reg = S3C2410_UDC_EP4_FIFO_REG;
456 if (!req->req.length)
459 buf = req->req.buf + req->req.actual;
460 bufferspace = req->req.length - req->req.actual;
462 dprintk(DEBUG_NORMAL, "%s: buffer full!\n", __func__);
466 udc_write(idx, S3C2410_UDC_INDEX_REG);
468 fifo_count = s3c2410_udc_fifo_count_out();
469 dprintk(DEBUG_NORMAL, "%s fifo count : %d\n", __func__, fifo_count);
471 if (fifo_count > ep->ep.maxpacket)
472 avail = ep->ep.maxpacket;
476 fifo_count = s3c2410_udc_read_packet(fifo_reg, buf, req, avail);
478 /* checking this with ep0 is not accurate as we already
479 * read a control request
481 if (idx != 0 && fifo_count < ep->ep.maxpacket) {
483 /* overflowed this request? flush extra data */
484 if (fifo_count != avail)
485 req->req.status = -EOVERFLOW;
487 is_last = (req->req.length <= req->req.actual) ? 1 : 0;
490 udc_write(idx, S3C2410_UDC_INDEX_REG);
491 fifo_count = s3c2410_udc_fifo_count_out();
493 /* Only ep0 debug messages are interesting */
495 dprintk(DEBUG_VERBOSE, "%s fifo count : %d [last %d]\n",
496 __func__, fifo_count, is_last);
500 s3c2410_udc_set_ep0_de_out(base_addr);
501 ep->dev->ep0state = EP0_IDLE;
503 udc_write(idx, S3C2410_UDC_INDEX_REG);
504 ep_csr = udc_read(S3C2410_UDC_OUT_CSR1_REG);
505 udc_write(idx, S3C2410_UDC_INDEX_REG);
506 udc_write(ep_csr & ~S3C2410_UDC_OCSR1_PKTRDY,
507 S3C2410_UDC_OUT_CSR1_REG);
510 s3c2410_udc_done(ep, req, 0);
513 s3c2410_udc_clear_ep0_opr(base_addr);
515 udc_write(idx, S3C2410_UDC_INDEX_REG);
516 ep_csr = udc_read(S3C2410_UDC_OUT_CSR1_REG);
517 udc_write(idx, S3C2410_UDC_INDEX_REG);
518 udc_write(ep_csr & ~S3C2410_UDC_OCSR1_PKTRDY,
519 S3C2410_UDC_OUT_CSR1_REG);
526 static int s3c2410_udc_read_fifo_crq(struct usb_ctrlrequest *crq)
528 unsigned char *outbuf = (unsigned char *)crq;
531 udc_write(0, S3C2410_UDC_INDEX_REG);
533 bytes_read = s3c2410_udc_fifo_count_out();
535 dprintk(DEBUG_NORMAL, "%s: fifo_count=%d\n", __func__, bytes_read);
537 if (bytes_read > sizeof(struct usb_ctrlrequest))
538 bytes_read = sizeof(struct usb_ctrlrequest);
540 readsb(S3C2410_UDC_EP0_FIFO_REG + base_addr, outbuf, bytes_read);
542 dprintk(DEBUG_VERBOSE, "%s: len=%d %02x:%02x {%x,%x,%x}\n", __func__,
543 bytes_read, crq->bRequest, crq->bRequestType,
544 crq->wValue, crq->wIndex, crq->wLength);
549 static int s3c2410_udc_get_status(struct s3c2410_udc *dev,
550 struct usb_ctrlrequest *crq)
553 u8 ep_num = crq->wIndex & 0x7F;
554 u8 is_in = crq->wIndex & USB_DIR_IN;
556 switch (crq->bRequestType & USB_RECIP_MASK) {
557 case USB_RECIP_INTERFACE:
560 case USB_RECIP_DEVICE:
561 status = dev->devstatus;
564 case USB_RECIP_ENDPOINT:
565 if (ep_num > 4 || crq->wLength > 2)
569 udc_write(0, S3C2410_UDC_INDEX_REG);
570 status = udc_read(S3C2410_UDC_IN_CSR1_REG);
571 status = status & S3C2410_UDC_EP0_CSR_SENDSTL;
573 udc_write(ep_num, S3C2410_UDC_INDEX_REG);
575 status = udc_read(S3C2410_UDC_IN_CSR1_REG);
576 status = status & S3C2410_UDC_ICSR1_SENDSTL;
578 status = udc_read(S3C2410_UDC_OUT_CSR1_REG);
579 status = status & S3C2410_UDC_OCSR1_SENDSTL;
583 status = status ? 1 : 0;
590 /* Seems to be needed to get it working. ouch :( */
592 udc_write(status & 0xFF, S3C2410_UDC_EP0_FIFO_REG);
593 udc_write(status >> 8, S3C2410_UDC_EP0_FIFO_REG);
594 s3c2410_udc_set_ep0_de_in(base_addr);
598 /*------------------------- usb state machine -------------------------------*/
599 static int s3c2410_udc_set_halt(struct usb_ep *_ep, int value);
601 static void s3c2410_udc_handle_ep0_idle(struct s3c2410_udc *dev,
602 struct s3c2410_ep *ep,
603 struct usb_ctrlrequest *crq,
608 /* start control request? */
609 if (!(ep0csr & S3C2410_UDC_EP0_CSR_OPKRDY))
612 s3c2410_udc_nuke(dev, ep, -EPROTO);
614 len = s3c2410_udc_read_fifo_crq(crq);
615 if (len != sizeof(*crq)) {
616 dprintk(DEBUG_NORMAL, "setup begin: fifo READ ERROR"
617 " wanted %d bytes got %d. Stalling out...\n",
619 s3c2410_udc_set_ep0_ss(base_addr);
623 dprintk(DEBUG_NORMAL, "bRequest = %d bRequestType %d wLength = %d\n",
624 crq->bRequest, crq->bRequestType, crq->wLength);
626 /* cope with automagic for some standard requests. */
627 dev->req_std = (crq->bRequestType & USB_TYPE_MASK)
628 == USB_TYPE_STANDARD;
630 dev->req_pending = 1;
632 switch (crq->bRequest) {
633 case USB_REQ_SET_CONFIGURATION:
634 dprintk(DEBUG_NORMAL, "USB_REQ_SET_CONFIGURATION ...\n");
636 if (crq->bRequestType == USB_RECIP_DEVICE) {
638 s3c2410_udc_set_ep0_de_out(base_addr);
642 case USB_REQ_SET_INTERFACE:
643 dprintk(DEBUG_NORMAL, "USB_REQ_SET_INTERFACE ...\n");
645 if (crq->bRequestType == USB_RECIP_INTERFACE) {
647 s3c2410_udc_set_ep0_de_out(base_addr);
651 case USB_REQ_SET_ADDRESS:
652 dprintk(DEBUG_NORMAL, "USB_REQ_SET_ADDRESS ...\n");
654 if (crq->bRequestType == USB_RECIP_DEVICE) {
655 tmp = crq->wValue & 0x7F;
657 udc_write((tmp | S3C2410_UDC_FUNCADDR_UPDATE),
658 S3C2410_UDC_FUNC_ADDR_REG);
659 s3c2410_udc_set_ep0_de_out(base_addr);
664 case USB_REQ_GET_STATUS:
665 dprintk(DEBUG_NORMAL, "USB_REQ_GET_STATUS ...\n");
666 s3c2410_udc_clear_ep0_opr(base_addr);
669 if (!s3c2410_udc_get_status(dev, crq))
674 case USB_REQ_CLEAR_FEATURE:
675 s3c2410_udc_clear_ep0_opr(base_addr);
677 if (crq->bRequestType != USB_RECIP_ENDPOINT)
680 if (crq->wValue != USB_ENDPOINT_HALT || crq->wLength != 0)
683 s3c2410_udc_set_halt(&dev->ep[crq->wIndex & 0x7f].ep, 0);
684 s3c2410_udc_set_ep0_de_out(base_addr);
687 case USB_REQ_SET_FEATURE:
688 s3c2410_udc_clear_ep0_opr(base_addr);
690 if (crq->bRequestType != USB_RECIP_ENDPOINT)
693 if (crq->wValue != USB_ENDPOINT_HALT || crq->wLength != 0)
696 s3c2410_udc_set_halt(&dev->ep[crq->wIndex & 0x7f].ep, 1);
697 s3c2410_udc_set_ep0_de_out(base_addr);
701 s3c2410_udc_clear_ep0_opr(base_addr);
705 if (crq->bRequestType & USB_DIR_IN)
706 dev->ep0state = EP0_IN_DATA_PHASE;
708 dev->ep0state = EP0_OUT_DATA_PHASE;
713 /* deliver the request to the gadget driver */
714 ret = dev->driver->setup(&dev->gadget, crq);
716 if (dev->req_config) {
717 dprintk(DEBUG_NORMAL, "config change %02x fail %d?\n",
722 if (ret == -EOPNOTSUPP)
723 dprintk(DEBUG_NORMAL, "Operation not supported\n");
725 dprintk(DEBUG_NORMAL,
726 "dev->driver->setup failed. (%d)\n", ret);
729 s3c2410_udc_set_ep0_ss(base_addr);
730 s3c2410_udc_set_ep0_de_out(base_addr);
731 dev->ep0state = EP0_IDLE;
732 /* deferred i/o == no response yet */
733 } else if (dev->req_pending) {
734 dprintk(DEBUG_VERBOSE, "dev->req_pending... what now?\n");
735 dev->req_pending = 0;
738 dprintk(DEBUG_VERBOSE, "ep0state %s\n", ep0states[dev->ep0state]);
741 static void s3c2410_udc_handle_ep0(struct s3c2410_udc *dev)
744 struct s3c2410_ep *ep = &dev->ep[0];
745 struct s3c2410_request *req;
746 struct usb_ctrlrequest crq;
748 if (list_empty(&ep->queue))
751 req = list_entry(ep->queue.next, struct s3c2410_request, queue);
753 /* We make the assumption that S3C2410_UDC_IN_CSR1_REG equal to
754 * S3C2410_UDC_EP0_CSR_REG when index is zero */
756 udc_write(0, S3C2410_UDC_INDEX_REG);
757 ep0csr = udc_read(S3C2410_UDC_IN_CSR1_REG);
759 dprintk(DEBUG_NORMAL, "ep0csr %x ep0state %s\n",
760 ep0csr, ep0states[dev->ep0state]);
762 /* clear stall status */
763 if (ep0csr & S3C2410_UDC_EP0_CSR_SENTSTL) {
764 s3c2410_udc_nuke(dev, ep, -EPIPE);
765 dprintk(DEBUG_NORMAL, "... clear SENT_STALL ...\n");
766 s3c2410_udc_clear_ep0_sst(base_addr);
767 dev->ep0state = EP0_IDLE;
771 /* clear setup end */
772 if (ep0csr & S3C2410_UDC_EP0_CSR_SE) {
773 dprintk(DEBUG_NORMAL, "... serviced SETUP_END ...\n");
774 s3c2410_udc_nuke(dev, ep, 0);
775 s3c2410_udc_clear_ep0_se(base_addr);
776 dev->ep0state = EP0_IDLE;
779 switch (dev->ep0state) {
781 s3c2410_udc_handle_ep0_idle(dev, ep, &crq, ep0csr);
784 case EP0_IN_DATA_PHASE: /* GET_DESCRIPTOR etc */
785 dprintk(DEBUG_NORMAL, "EP0_IN_DATA_PHASE ... what now?\n");
786 if (!(ep0csr & S3C2410_UDC_EP0_CSR_IPKRDY) && req)
787 s3c2410_udc_write_fifo(ep, req);
790 case EP0_OUT_DATA_PHASE: /* SET_DESCRIPTOR etc */
791 dprintk(DEBUG_NORMAL, "EP0_OUT_DATA_PHASE ... what now?\n");
792 if ((ep0csr & S3C2410_UDC_EP0_CSR_OPKRDY) && req)
793 s3c2410_udc_read_fifo(ep, req);
797 dprintk(DEBUG_NORMAL, "EP0_END_XFER ... what now?\n");
798 dev->ep0state = EP0_IDLE;
802 dprintk(DEBUG_NORMAL, "EP0_STALL ... what now?\n");
803 dev->ep0state = EP0_IDLE;
809 * handle_ep - Manage I/O endpoints
812 static void s3c2410_udc_handle_ep(struct s3c2410_ep *ep)
814 struct s3c2410_request *req;
815 int is_in = ep->bEndpointAddress & USB_DIR_IN;
819 if (likely(!list_empty(&ep->queue)))
820 req = list_entry(ep->queue.next,
821 struct s3c2410_request, queue);
825 idx = ep->bEndpointAddress & 0x7F;
828 udc_write(idx, S3C2410_UDC_INDEX_REG);
829 ep_csr1 = udc_read(S3C2410_UDC_IN_CSR1_REG);
830 dprintk(DEBUG_VERBOSE, "ep%01d write csr:%02x %d\n",
831 idx, ep_csr1, req ? 1 : 0);
833 if (ep_csr1 & S3C2410_UDC_ICSR1_SENTSTL) {
834 dprintk(DEBUG_VERBOSE, "st\n");
835 udc_write(idx, S3C2410_UDC_INDEX_REG);
836 udc_write(ep_csr1 & ~S3C2410_UDC_ICSR1_SENTSTL,
837 S3C2410_UDC_IN_CSR1_REG);
841 if (!(ep_csr1 & S3C2410_UDC_ICSR1_PKTRDY) && req)
842 s3c2410_udc_write_fifo(ep, req);
844 udc_write(idx, S3C2410_UDC_INDEX_REG);
845 ep_csr1 = udc_read(S3C2410_UDC_OUT_CSR1_REG);
846 dprintk(DEBUG_VERBOSE, "ep%01d rd csr:%02x\n", idx, ep_csr1);
848 if (ep_csr1 & S3C2410_UDC_OCSR1_SENTSTL) {
849 udc_write(idx, S3C2410_UDC_INDEX_REG);
850 udc_write(ep_csr1 & ~S3C2410_UDC_OCSR1_SENTSTL,
851 S3C2410_UDC_OUT_CSR1_REG);
855 if ((ep_csr1 & S3C2410_UDC_OCSR1_PKTRDY) && req)
856 s3c2410_udc_read_fifo(ep, req);
860 #include <mach/regs-irq.h>
863 * s3c2410_udc_irq - interrupt handler
865 static irqreturn_t s3c2410_udc_irq(int dummy, void *_dev)
867 struct s3c2410_udc *dev = _dev;
876 spin_lock_irqsave(&dev->lock, flags);
878 /* Driver connected ? */
880 /* Clear interrupts */
881 udc_write(udc_read(S3C2410_UDC_USB_INT_REG),
882 S3C2410_UDC_USB_INT_REG);
883 udc_write(udc_read(S3C2410_UDC_EP_INT_REG),
884 S3C2410_UDC_EP_INT_REG);
888 idx = udc_read(S3C2410_UDC_INDEX_REG);
890 /* Read status registers */
891 usb_status = udc_read(S3C2410_UDC_USB_INT_REG);
892 usbd_status = udc_read(S3C2410_UDC_EP_INT_REG);
893 pwr_reg = udc_read(S3C2410_UDC_PWR_REG);
895 udc_writeb(base_addr, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
896 ep0csr = udc_read(S3C2410_UDC_IN_CSR1_REG);
898 dprintk(DEBUG_NORMAL, "usbs=%02x, usbds=%02x, pwr=%02x ep0csr=%02x\n",
899 usb_status, usbd_status, pwr_reg, ep0csr);
902 * Now, handle interrupts. There's two types :
903 * - Reset, Resume, Suspend coming -> usb_int_reg
908 if (usb_status & S3C2410_UDC_USBINT_RESET) {
909 /* two kind of reset :
910 * - reset start -> pwr reg = 8
911 * - reset end -> pwr reg = 0
913 dprintk(DEBUG_NORMAL, "USB reset csr %x pwr %x\n",
916 dev->gadget.speed = USB_SPEED_UNKNOWN;
917 udc_write(0x00, S3C2410_UDC_INDEX_REG);
918 udc_write((dev->ep[0].ep.maxpacket & 0x7ff) >> 3,
919 S3C2410_UDC_MAXP_REG);
922 dev->ep0state = EP0_IDLE;
923 dev->gadget.speed = USB_SPEED_FULL;
925 /* clear interrupt */
926 udc_write(S3C2410_UDC_USBINT_RESET,
927 S3C2410_UDC_USB_INT_REG);
929 udc_write(idx, S3C2410_UDC_INDEX_REG);
930 spin_unlock_irqrestore(&dev->lock, flags);
935 if (usb_status & S3C2410_UDC_USBINT_RESUME) {
936 dprintk(DEBUG_NORMAL, "USB resume\n");
938 /* clear interrupt */
939 udc_write(S3C2410_UDC_USBINT_RESUME,
940 S3C2410_UDC_USB_INT_REG);
942 if (dev->gadget.speed != USB_SPEED_UNKNOWN
944 && dev->driver->resume)
945 dev->driver->resume(&dev->gadget);
949 if (usb_status & S3C2410_UDC_USBINT_SUSPEND) {
950 dprintk(DEBUG_NORMAL, "USB suspend\n");
952 /* clear interrupt */
953 udc_write(S3C2410_UDC_USBINT_SUSPEND,
954 S3C2410_UDC_USB_INT_REG);
956 if (dev->gadget.speed != USB_SPEED_UNKNOWN
958 && dev->driver->suspend)
959 dev->driver->suspend(&dev->gadget);
961 dev->ep0state = EP0_IDLE;
965 /* control traffic */
966 /* check on ep0csr != 0 is not a good idea as clearing in_pkt_ready
967 * generate an interrupt
969 if (usbd_status & S3C2410_UDC_INT_EP0) {
970 dprintk(DEBUG_VERBOSE, "USB ep0 irq\n");
971 /* Clear the interrupt bit by setting it to 1 */
972 udc_write(S3C2410_UDC_INT_EP0, S3C2410_UDC_EP_INT_REG);
973 s3c2410_udc_handle_ep0(dev);
976 /* endpoint data transfers */
977 for (i = 1; i < S3C2410_ENDPOINTS; i++) {
979 if (usbd_status & tmp) {
980 dprintk(DEBUG_VERBOSE, "USB ep%d irq\n", i);
982 /* Clear the interrupt bit by setting it to 1 */
983 udc_write(tmp, S3C2410_UDC_EP_INT_REG);
984 s3c2410_udc_handle_ep(&dev->ep[i]);
988 /* what else causes this interrupt? a receive! who is it? */
989 if (!usb_status && !usbd_status && !pwr_reg && !ep0csr) {
990 for (i = 1; i < S3C2410_ENDPOINTS; i++) {
991 idx2 = udc_read(S3C2410_UDC_INDEX_REG);
992 udc_write(i, S3C2410_UDC_INDEX_REG);
994 if (udc_read(S3C2410_UDC_OUT_CSR1_REG) & 0x1)
995 s3c2410_udc_handle_ep(&dev->ep[i]);
998 udc_write(idx2, S3C2410_UDC_INDEX_REG);
1002 dprintk(DEBUG_VERBOSE, "irq: %d s3c2410_udc_done.\n", IRQ_USBD);
1004 /* Restore old index */
1005 udc_write(idx, S3C2410_UDC_INDEX_REG);
1007 spin_unlock_irqrestore(&dev->lock, flags);
1011 /*------------------------- s3c2410_ep_ops ----------------------------------*/
1013 static inline struct s3c2410_ep *to_s3c2410_ep(struct usb_ep *ep)
1015 return container_of(ep, struct s3c2410_ep, ep);
1018 static inline struct s3c2410_udc *to_s3c2410_udc(struct usb_gadget *gadget)
1020 return container_of(gadget, struct s3c2410_udc, gadget);
1023 static inline struct s3c2410_request *to_s3c2410_req(struct usb_request *req)
1025 return container_of(req, struct s3c2410_request, req);
1029 * s3c2410_udc_ep_enable
1031 static int s3c2410_udc_ep_enable(struct usb_ep *_ep,
1032 const struct usb_endpoint_descriptor *desc)
1034 struct s3c2410_udc *dev;
1035 struct s3c2410_ep *ep;
1037 unsigned long flags;
1041 ep = to_s3c2410_ep(_ep);
1044 || _ep->name == ep0name
1045 || desc->bDescriptorType != USB_DT_ENDPOINT)
1049 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
1052 max = usb_endpoint_maxp(desc) & 0x1fff;
1054 local_irq_save(flags);
1055 _ep->maxpacket = max & 0x7ff;
1058 ep->bEndpointAddress = desc->bEndpointAddress;
1060 /* set max packet */
1061 udc_write(ep->num, S3C2410_UDC_INDEX_REG);
1062 udc_write(max >> 3, S3C2410_UDC_MAXP_REG);
1064 /* set type, direction, address; reset fifo counters */
1065 if (desc->bEndpointAddress & USB_DIR_IN) {
1066 csr1 = S3C2410_UDC_ICSR1_FFLUSH|S3C2410_UDC_ICSR1_CLRDT;
1067 csr2 = S3C2410_UDC_ICSR2_MODEIN|S3C2410_UDC_ICSR2_DMAIEN;
1069 udc_write(ep->num, S3C2410_UDC_INDEX_REG);
1070 udc_write(csr1, S3C2410_UDC_IN_CSR1_REG);
1071 udc_write(ep->num, S3C2410_UDC_INDEX_REG);
1072 udc_write(csr2, S3C2410_UDC_IN_CSR2_REG);
1074 /* don't flush in fifo or it will cause endpoint interrupt */
1075 csr1 = S3C2410_UDC_ICSR1_CLRDT;
1076 csr2 = S3C2410_UDC_ICSR2_DMAIEN;
1078 udc_write(ep->num, S3C2410_UDC_INDEX_REG);
1079 udc_write(csr1, S3C2410_UDC_IN_CSR1_REG);
1080 udc_write(ep->num, S3C2410_UDC_INDEX_REG);
1081 udc_write(csr2, S3C2410_UDC_IN_CSR2_REG);
1083 csr1 = S3C2410_UDC_OCSR1_FFLUSH | S3C2410_UDC_OCSR1_CLRDT;
1084 csr2 = S3C2410_UDC_OCSR2_DMAIEN;
1086 udc_write(ep->num, S3C2410_UDC_INDEX_REG);
1087 udc_write(csr1, S3C2410_UDC_OUT_CSR1_REG);
1088 udc_write(ep->num, S3C2410_UDC_INDEX_REG);
1089 udc_write(csr2, S3C2410_UDC_OUT_CSR2_REG);
1093 int_en_reg = udc_read(S3C2410_UDC_EP_INT_EN_REG);
1094 udc_write(int_en_reg | (1 << ep->num), S3C2410_UDC_EP_INT_EN_REG);
1096 /* print some debug message */
1097 tmp = desc->bEndpointAddress;
1098 dprintk(DEBUG_NORMAL, "enable %s(%d) ep%x%s-blk max %02x\n",
1099 _ep->name, ep->num, tmp,
1100 desc->bEndpointAddress & USB_DIR_IN ? "in" : "out", max);
1102 local_irq_restore(flags);
1103 s3c2410_udc_set_halt(_ep, 0);
1109 * s3c2410_udc_ep_disable
1111 static int s3c2410_udc_ep_disable(struct usb_ep *_ep)
1113 struct s3c2410_ep *ep = to_s3c2410_ep(_ep);
1114 unsigned long flags;
1117 if (!_ep || !ep->ep.desc) {
1118 dprintk(DEBUG_NORMAL, "%s not enabled\n",
1119 _ep ? ep->ep.name : NULL);
1123 local_irq_save(flags);
1125 dprintk(DEBUG_NORMAL, "ep_disable: %s\n", _ep->name);
1130 s3c2410_udc_nuke(ep->dev, ep, -ESHUTDOWN);
1133 int_en_reg = udc_read(S3C2410_UDC_EP_INT_EN_REG);
1134 udc_write(int_en_reg & ~(1<<ep->num), S3C2410_UDC_EP_INT_EN_REG);
1136 local_irq_restore(flags);
1138 dprintk(DEBUG_NORMAL, "%s disabled\n", _ep->name);
1144 * s3c2410_udc_alloc_request
1146 static struct usb_request *
1147 s3c2410_udc_alloc_request(struct usb_ep *_ep, gfp_t mem_flags)
1149 struct s3c2410_request *req;
1151 dprintk(DEBUG_VERBOSE, "%s(%p,%d)\n", __func__, _ep, mem_flags);
1156 req = kzalloc(sizeof(struct s3c2410_request), mem_flags);
1160 INIT_LIST_HEAD(&req->queue);
1165 * s3c2410_udc_free_request
1168 s3c2410_udc_free_request(struct usb_ep *_ep, struct usb_request *_req)
1170 struct s3c2410_ep *ep = to_s3c2410_ep(_ep);
1171 struct s3c2410_request *req = to_s3c2410_req(_req);
1173 dprintk(DEBUG_VERBOSE, "%s(%p,%p)\n", __func__, _ep, _req);
1175 if (!ep || !_req || (!ep->ep.desc && _ep->name != ep0name))
1178 WARN_ON(!list_empty(&req->queue));
1185 static int s3c2410_udc_queue(struct usb_ep *_ep, struct usb_request *_req,
1188 struct s3c2410_request *req = to_s3c2410_req(_req);
1189 struct s3c2410_ep *ep = to_s3c2410_ep(_ep);
1190 struct s3c2410_udc *dev;
1193 unsigned long flags;
1195 if (unlikely(!_ep || (!ep->ep.desc && ep->ep.name != ep0name))) {
1196 dprintk(DEBUG_NORMAL, "%s: invalid args\n", __func__);
1201 if (unlikely(!dev->driver
1202 || dev->gadget.speed == USB_SPEED_UNKNOWN)) {
1206 local_irq_save(flags);
1208 if (unlikely(!_req || !_req->complete
1209 || !_req->buf || !list_empty(&req->queue))) {
1211 dprintk(DEBUG_NORMAL, "%s: 1 X X X\n", __func__);
1213 dprintk(DEBUG_NORMAL, "%s: 0 %01d %01d %01d\n",
1214 __func__, !_req->complete, !_req->buf,
1215 !list_empty(&req->queue));
1218 local_irq_restore(flags);
1222 _req->status = -EINPROGRESS;
1225 dprintk(DEBUG_VERBOSE, "%s: ep%x len %d\n",
1226 __func__, ep->bEndpointAddress, _req->length);
1228 if (ep->bEndpointAddress) {
1229 udc_write(ep->bEndpointAddress & 0x7F, S3C2410_UDC_INDEX_REG);
1231 ep_csr = udc_read((ep->bEndpointAddress & USB_DIR_IN)
1232 ? S3C2410_UDC_IN_CSR1_REG
1233 : S3C2410_UDC_OUT_CSR1_REG);
1234 fifo_count = s3c2410_udc_fifo_count_out();
1236 udc_write(0, S3C2410_UDC_INDEX_REG);
1237 ep_csr = udc_read(S3C2410_UDC_IN_CSR1_REG);
1238 fifo_count = s3c2410_udc_fifo_count_out();
1241 /* kickstart this i/o queue? */
1242 if (list_empty(&ep->queue) && !ep->halted) {
1243 if (ep->bEndpointAddress == 0 /* ep0 */) {
1244 switch (dev->ep0state) {
1245 case EP0_IN_DATA_PHASE:
1246 if (!(ep_csr&S3C2410_UDC_EP0_CSR_IPKRDY)
1247 && s3c2410_udc_write_fifo(ep,
1249 dev->ep0state = EP0_IDLE;
1254 case EP0_OUT_DATA_PHASE:
1256 || ((ep_csr & S3C2410_UDC_OCSR1_PKTRDY)
1257 && s3c2410_udc_read_fifo(ep,
1259 dev->ep0state = EP0_IDLE;
1265 local_irq_restore(flags);
1268 } else if ((ep->bEndpointAddress & USB_DIR_IN) != 0
1269 && (!(ep_csr&S3C2410_UDC_OCSR1_PKTRDY))
1270 && s3c2410_udc_write_fifo(ep, req)) {
1272 } else if ((ep_csr & S3C2410_UDC_OCSR1_PKTRDY)
1274 && s3c2410_udc_read_fifo(ep, req)) {
1279 /* pio or dma irq handler advances the queue. */
1281 list_add_tail(&req->queue, &ep->queue);
1283 local_irq_restore(flags);
1285 dprintk(DEBUG_VERBOSE, "%s ok\n", __func__);
1290 * s3c2410_udc_dequeue
1292 static int s3c2410_udc_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1294 struct s3c2410_ep *ep = to_s3c2410_ep(_ep);
1295 struct s3c2410_udc *udc;
1296 int retval = -EINVAL;
1297 unsigned long flags;
1298 struct s3c2410_request *req = NULL;
1300 dprintk(DEBUG_VERBOSE, "%s(%p,%p)\n", __func__, _ep, _req);
1302 if (!the_controller->driver)
1308 udc = to_s3c2410_udc(ep->gadget);
1310 local_irq_save(flags);
1312 list_for_each_entry(req, &ep->queue, queue) {
1313 if (&req->req == _req) {
1314 list_del_init(&req->queue);
1315 _req->status = -ECONNRESET;
1322 dprintk(DEBUG_VERBOSE,
1323 "dequeued req %p from %s, len %d buf %p\n",
1324 req, _ep->name, _req->length, _req->buf);
1326 s3c2410_udc_done(ep, req, -ECONNRESET);
1329 local_irq_restore(flags);
1334 * s3c2410_udc_set_halt
1336 static int s3c2410_udc_set_halt(struct usb_ep *_ep, int value)
1338 struct s3c2410_ep *ep = to_s3c2410_ep(_ep);
1340 unsigned long flags;
1343 if (unlikely(!_ep || (!ep->ep.desc && ep->ep.name != ep0name))) {
1344 dprintk(DEBUG_NORMAL, "%s: inval 2\n", __func__);
1348 local_irq_save(flags);
1350 idx = ep->bEndpointAddress & 0x7F;
1353 s3c2410_udc_set_ep0_ss(base_addr);
1354 s3c2410_udc_set_ep0_de_out(base_addr);
1356 udc_write(idx, S3C2410_UDC_INDEX_REG);
1357 ep_csr = udc_read((ep->bEndpointAddress & USB_DIR_IN)
1358 ? S3C2410_UDC_IN_CSR1_REG
1359 : S3C2410_UDC_OUT_CSR1_REG);
1361 if ((ep->bEndpointAddress & USB_DIR_IN) != 0) {
1363 udc_write(ep_csr | S3C2410_UDC_ICSR1_SENDSTL,
1364 S3C2410_UDC_IN_CSR1_REG);
1366 ep_csr &= ~S3C2410_UDC_ICSR1_SENDSTL;
1367 udc_write(ep_csr, S3C2410_UDC_IN_CSR1_REG);
1368 ep_csr |= S3C2410_UDC_ICSR1_CLRDT;
1369 udc_write(ep_csr, S3C2410_UDC_IN_CSR1_REG);
1373 udc_write(ep_csr | S3C2410_UDC_OCSR1_SENDSTL,
1374 S3C2410_UDC_OUT_CSR1_REG);
1376 ep_csr &= ~S3C2410_UDC_OCSR1_SENDSTL;
1377 udc_write(ep_csr, S3C2410_UDC_OUT_CSR1_REG);
1378 ep_csr |= S3C2410_UDC_OCSR1_CLRDT;
1379 udc_write(ep_csr, S3C2410_UDC_OUT_CSR1_REG);
1384 ep->halted = value ? 1 : 0;
1385 local_irq_restore(flags);
1390 static const struct usb_ep_ops s3c2410_ep_ops = {
1391 .enable = s3c2410_udc_ep_enable,
1392 .disable = s3c2410_udc_ep_disable,
1394 .alloc_request = s3c2410_udc_alloc_request,
1395 .free_request = s3c2410_udc_free_request,
1397 .queue = s3c2410_udc_queue,
1398 .dequeue = s3c2410_udc_dequeue,
1400 .set_halt = s3c2410_udc_set_halt,
1403 /*------------------------- usb_gadget_ops ----------------------------------*/
1406 * s3c2410_udc_get_frame
1408 static int s3c2410_udc_get_frame(struct usb_gadget *_gadget)
1412 dprintk(DEBUG_VERBOSE, "%s()\n", __func__);
1414 tmp = udc_read(S3C2410_UDC_FRAME_NUM2_REG) << 8;
1415 tmp |= udc_read(S3C2410_UDC_FRAME_NUM1_REG);
1420 * s3c2410_udc_wakeup
1422 static int s3c2410_udc_wakeup(struct usb_gadget *_gadget)
1424 dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1429 * s3c2410_udc_set_selfpowered
1431 static int s3c2410_udc_set_selfpowered(struct usb_gadget *gadget, int value)
1433 struct s3c2410_udc *udc = to_s3c2410_udc(gadget);
1435 dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1437 gadget->is_selfpowered = (value != 0);
1439 udc->devstatus |= (1 << USB_DEVICE_SELF_POWERED);
1441 udc->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
1446 static void s3c2410_udc_disable(struct s3c2410_udc *dev);
1447 static void s3c2410_udc_enable(struct s3c2410_udc *dev);
1449 static int s3c2410_udc_set_pullup(struct s3c2410_udc *udc, int is_on)
1451 dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1453 if (udc_info && (udc_info->udc_command ||
1454 gpio_is_valid(udc_info->pullup_pin))) {
1457 s3c2410_udc_enable(udc);
1459 if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
1460 if (udc->driver && udc->driver->disconnect)
1461 udc->driver->disconnect(&udc->gadget);
1464 s3c2410_udc_disable(udc);
1473 static int s3c2410_udc_vbus_session(struct usb_gadget *gadget, int is_active)
1475 struct s3c2410_udc *udc = to_s3c2410_udc(gadget);
1477 dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1479 udc->vbus = (is_active != 0);
1480 s3c2410_udc_set_pullup(udc, is_active);
1484 static int s3c2410_udc_pullup(struct usb_gadget *gadget, int is_on)
1486 struct s3c2410_udc *udc = to_s3c2410_udc(gadget);
1488 dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1490 s3c2410_udc_set_pullup(udc, is_on ? 0 : 1);
1494 static irqreturn_t s3c2410_udc_vbus_irq(int irq, void *_dev)
1496 struct s3c2410_udc *dev = _dev;
1499 dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1501 value = gpio_get_value(udc_info->vbus_pin) ? 1 : 0;
1502 if (udc_info->vbus_pin_inverted)
1505 if (value != dev->vbus)
1506 s3c2410_udc_vbus_session(&dev->gadget, value);
1511 static int s3c2410_vbus_draw(struct usb_gadget *_gadget, unsigned ma)
1513 dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1515 if (udc_info && udc_info->vbus_draw) {
1516 udc_info->vbus_draw(ma);
1523 static int s3c2410_udc_start(struct usb_gadget *g,
1524 struct usb_gadget_driver *driver);
1525 static int s3c2410_udc_stop(struct usb_gadget *g);
1527 static const struct usb_gadget_ops s3c2410_ops = {
1528 .get_frame = s3c2410_udc_get_frame,
1529 .wakeup = s3c2410_udc_wakeup,
1530 .set_selfpowered = s3c2410_udc_set_selfpowered,
1531 .pullup = s3c2410_udc_pullup,
1532 .vbus_session = s3c2410_udc_vbus_session,
1533 .vbus_draw = s3c2410_vbus_draw,
1534 .udc_start = s3c2410_udc_start,
1535 .udc_stop = s3c2410_udc_stop,
1538 static void s3c2410_udc_command(enum s3c2410_udc_cmd_e cmd)
1543 if (udc_info->udc_command) {
1544 udc_info->udc_command(cmd);
1545 } else if (gpio_is_valid(udc_info->pullup_pin)) {
1549 case S3C2410_UDC_P_ENABLE:
1552 case S3C2410_UDC_P_DISABLE:
1558 value ^= udc_info->pullup_pin_inverted;
1560 gpio_set_value(udc_info->pullup_pin, value);
1564 /*------------------------- gadget driver handling---------------------------*/
1566 * s3c2410_udc_disable
1568 static void s3c2410_udc_disable(struct s3c2410_udc *dev)
1570 dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1572 /* Disable all interrupts */
1573 udc_write(0x00, S3C2410_UDC_USB_INT_EN_REG);
1574 udc_write(0x00, S3C2410_UDC_EP_INT_EN_REG);
1576 /* Clear the interrupt registers */
1577 udc_write(S3C2410_UDC_USBINT_RESET
1578 | S3C2410_UDC_USBINT_RESUME
1579 | S3C2410_UDC_USBINT_SUSPEND,
1580 S3C2410_UDC_USB_INT_REG);
1582 udc_write(0x1F, S3C2410_UDC_EP_INT_REG);
1584 /* Good bye, cruel world */
1585 s3c2410_udc_command(S3C2410_UDC_P_DISABLE);
1587 /* Set speed to unknown */
1588 dev->gadget.speed = USB_SPEED_UNKNOWN;
1592 * s3c2410_udc_reinit
1594 static void s3c2410_udc_reinit(struct s3c2410_udc *dev)
1598 /* device/ep0 records init */
1599 INIT_LIST_HEAD(&dev->gadget.ep_list);
1600 INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
1601 dev->ep0state = EP0_IDLE;
1603 for (i = 0; i < S3C2410_ENDPOINTS; i++) {
1604 struct s3c2410_ep *ep = &dev->ep[i];
1607 list_add_tail(&ep->ep.ep_list, &dev->gadget.ep_list);
1612 INIT_LIST_HEAD(&ep->queue);
1613 usb_ep_set_maxpacket_limit(&ep->ep, ep->ep.maxpacket);
1618 * s3c2410_udc_enable
1620 static void s3c2410_udc_enable(struct s3c2410_udc *dev)
1624 dprintk(DEBUG_NORMAL, "s3c2410_udc_enable called\n");
1626 /* dev->gadget.speed = USB_SPEED_UNKNOWN; */
1627 dev->gadget.speed = USB_SPEED_FULL;
1629 /* Set MAXP for all endpoints */
1630 for (i = 0; i < S3C2410_ENDPOINTS; i++) {
1631 udc_write(i, S3C2410_UDC_INDEX_REG);
1632 udc_write((dev->ep[i].ep.maxpacket & 0x7ff) >> 3,
1633 S3C2410_UDC_MAXP_REG);
1636 /* Set default power state */
1637 udc_write(DEFAULT_POWER_STATE, S3C2410_UDC_PWR_REG);
1639 /* Enable reset and suspend interrupt interrupts */
1640 udc_write(S3C2410_UDC_USBINT_RESET | S3C2410_UDC_USBINT_SUSPEND,
1641 S3C2410_UDC_USB_INT_EN_REG);
1643 /* Enable ep0 interrupt */
1644 udc_write(S3C2410_UDC_INT_EP0, S3C2410_UDC_EP_INT_EN_REG);
1646 /* time to say "hello, world" */
1647 s3c2410_udc_command(S3C2410_UDC_P_ENABLE);
1650 static int s3c2410_udc_start(struct usb_gadget *g,
1651 struct usb_gadget_driver *driver)
1653 struct s3c2410_udc *udc = to_s3c2410(g);
1655 dprintk(DEBUG_NORMAL, "%s() '%s'\n", __func__, driver->driver.name);
1657 /* Hook the driver */
1658 udc->driver = driver;
1661 s3c2410_udc_enable(udc);
1666 static int s3c2410_udc_stop(struct usb_gadget *g)
1668 struct s3c2410_udc *udc = to_s3c2410(g);
1673 s3c2410_udc_disable(udc);
1678 /*---------------------------------------------------------------------------*/
1679 static struct s3c2410_udc memory = {
1681 .ops = &s3c2410_ops,
1682 .ep0 = &memory.ep[0].ep,
1683 .name = gadget_name,
1685 .init_name = "gadget",
1689 /* control endpoint */
1694 .ops = &s3c2410_ep_ops,
1695 .maxpacket = EP0_FIFO_SIZE,
1700 /* first group of endpoints */
1705 .ops = &s3c2410_ep_ops,
1706 .maxpacket = EP_FIFO_SIZE,
1709 .fifo_size = EP_FIFO_SIZE,
1710 .bEndpointAddress = 1,
1711 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1717 .ops = &s3c2410_ep_ops,
1718 .maxpacket = EP_FIFO_SIZE,
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,
1733 .fifo_size = EP_FIFO_SIZE,
1734 .bEndpointAddress = 3,
1735 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1741 .ops = &s3c2410_ep_ops,
1742 .maxpacket = EP_FIFO_SIZE,
1745 .fifo_size = EP_FIFO_SIZE,
1746 .bEndpointAddress = 4,
1747 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1753 * probe - binds to the platform device
1755 static int s3c2410_udc_probe(struct platform_device *pdev)
1757 struct s3c2410_udc *udc = &memory;
1758 struct device *dev = &pdev->dev;
1762 dev_dbg(dev, "%s()\n", __func__);
1764 usb_bus_clock = clk_get(NULL, "usb-bus-gadget");
1765 if (IS_ERR(usb_bus_clock)) {
1766 dev_err(dev, "failed to get usb bus clock source\n");
1767 return PTR_ERR(usb_bus_clock);
1770 clk_prepare_enable(usb_bus_clock);
1772 udc_clock = clk_get(NULL, "usb-device");
1773 if (IS_ERR(udc_clock)) {
1774 dev_err(dev, "failed to get udc clock source\n");
1775 return PTR_ERR(udc_clock);
1778 clk_prepare_enable(udc_clock);
1782 dev_dbg(dev, "got and enabled clocks\n");
1784 if (strncmp(pdev->name, "s3c2440", 7) == 0) {
1785 dev_info(dev, "S3C2440: increasing FIFO to 128 bytes\n");
1786 memory.ep[1].fifo_size = S3C2440_EP_FIFO_SIZE;
1787 memory.ep[2].fifo_size = S3C2440_EP_FIFO_SIZE;
1788 memory.ep[3].fifo_size = S3C2440_EP_FIFO_SIZE;
1789 memory.ep[4].fifo_size = S3C2440_EP_FIFO_SIZE;
1792 spin_lock_init(&udc->lock);
1793 udc_info = dev_get_platdata(&pdev->dev);
1795 rsrc_start = S3C2410_PA_USBDEV;
1796 rsrc_len = S3C24XX_SZ_USBDEV;
1798 if (!request_mem_region(rsrc_start, rsrc_len, gadget_name))
1801 base_addr = ioremap(rsrc_start, rsrc_len);
1807 the_controller = udc;
1808 platform_set_drvdata(pdev, udc);
1810 s3c2410_udc_disable(udc);
1811 s3c2410_udc_reinit(udc);
1813 /* irq setup after old hardware state is cleaned up */
1814 retval = request_irq(IRQ_USBD, s3c2410_udc_irq,
1815 0, gadget_name, udc);
1818 dev_err(dev, "cannot get irq %i, err %d\n", IRQ_USBD, retval);
1823 dev_dbg(dev, "got irq %i\n", IRQ_USBD);
1825 if (udc_info && udc_info->vbus_pin > 0) {
1826 retval = gpio_request(udc_info->vbus_pin, "udc vbus");
1828 dev_err(dev, "cannot claim vbus pin\n");
1832 irq = gpio_to_irq(udc_info->vbus_pin);
1834 dev_err(dev, "no irq for gpio vbus pin\n");
1836 goto err_gpio_claim;
1839 retval = request_irq(irq, s3c2410_udc_vbus_irq,
1841 | IRQF_TRIGGER_FALLING | IRQF_SHARED,
1845 dev_err(dev, "can't get vbus irq %d, err %d\n",
1848 goto err_gpio_claim;
1851 dev_dbg(dev, "got irq %i\n", irq);
1856 if (udc_info && !udc_info->udc_command &&
1857 gpio_is_valid(udc_info->pullup_pin)) {
1859 retval = gpio_request_one(udc_info->pullup_pin,
1860 udc_info->vbus_pin_inverted ?
1861 GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW,
1867 retval = usb_add_gadget_udc(&pdev->dev, &udc->gadget);
1871 if (s3c2410_udc_debugfs_root) {
1872 udc->regs_info = debugfs_create_file("registers", S_IRUGO,
1873 s3c2410_udc_debugfs_root,
1874 udc, &s3c2410_udc_debugfs_fops);
1875 if (!udc->regs_info)
1876 dev_warn(dev, "debugfs file creation failed\n");
1879 dev_dbg(dev, "probe ok\n");
1884 if (udc_info && !udc_info->udc_command &&
1885 gpio_is_valid(udc_info->pullup_pin))
1886 gpio_free(udc_info->pullup_pin);
1888 if (udc_info && udc_info->vbus_pin > 0)
1889 free_irq(gpio_to_irq(udc_info->vbus_pin), udc);
1891 if (udc_info && udc_info->vbus_pin > 0)
1892 gpio_free(udc_info->vbus_pin);
1894 free_irq(IRQ_USBD, udc);
1898 release_mem_region(rsrc_start, rsrc_len);
1904 * s3c2410_udc_remove
1906 static int s3c2410_udc_remove(struct platform_device *pdev)
1908 struct s3c2410_udc *udc = platform_get_drvdata(pdev);
1911 dev_dbg(&pdev->dev, "%s()\n", __func__);
1916 usb_del_gadget_udc(&udc->gadget);
1917 debugfs_remove(udc->regs_info);
1919 if (udc_info && !udc_info->udc_command &&
1920 gpio_is_valid(udc_info->pullup_pin))
1921 gpio_free(udc_info->pullup_pin);
1923 if (udc_info && udc_info->vbus_pin > 0) {
1924 irq = gpio_to_irq(udc_info->vbus_pin);
1928 free_irq(IRQ_USBD, udc);
1931 release_mem_region(rsrc_start, rsrc_len);
1933 if (!IS_ERR(udc_clock) && udc_clock != NULL) {
1934 clk_disable_unprepare(udc_clock);
1939 if (!IS_ERR(usb_bus_clock) && usb_bus_clock != NULL) {
1940 clk_disable_unprepare(usb_bus_clock);
1941 clk_put(usb_bus_clock);
1942 usb_bus_clock = NULL;
1945 dev_dbg(&pdev->dev, "%s: remove ok\n", __func__);
1951 s3c2410_udc_suspend(struct platform_device *pdev, pm_message_t message)
1953 s3c2410_udc_command(S3C2410_UDC_P_DISABLE);
1958 static int s3c2410_udc_resume(struct platform_device *pdev)
1960 s3c2410_udc_command(S3C2410_UDC_P_ENABLE);
1965 #define s3c2410_udc_suspend NULL
1966 #define s3c2410_udc_resume NULL
1969 static const struct platform_device_id s3c_udc_ids[] = {
1970 { "s3c2410-usbgadget", },
1971 { "s3c2440-usbgadget", },
1974 MODULE_DEVICE_TABLE(platform, s3c_udc_ids);
1976 static struct platform_driver udc_driver_24x0 = {
1978 .name = "s3c24x0-usbgadget",
1980 .probe = s3c2410_udc_probe,
1981 .remove = s3c2410_udc_remove,
1982 .suspend = s3c2410_udc_suspend,
1983 .resume = s3c2410_udc_resume,
1984 .id_table = s3c_udc_ids,
1987 static int __init udc_init(void)
1991 dprintk(DEBUG_NORMAL, "%s: version %s\n", gadget_name, DRIVER_VERSION);
1993 s3c2410_udc_debugfs_root = debugfs_create_dir(gadget_name, NULL);
1994 if (IS_ERR(s3c2410_udc_debugfs_root)) {
1995 pr_err("%s: debugfs dir creation failed %ld\n",
1996 gadget_name, PTR_ERR(s3c2410_udc_debugfs_root));
1997 s3c2410_udc_debugfs_root = NULL;
2000 retval = platform_driver_register(&udc_driver_24x0);
2007 debugfs_remove(s3c2410_udc_debugfs_root);
2011 static void __exit udc_exit(void)
2013 platform_driver_unregister(&udc_driver_24x0);
2014 debugfs_remove(s3c2410_udc_debugfs_root);
2017 module_init(udc_init);
2018 module_exit(udc_exit);
2020 MODULE_AUTHOR(DRIVER_AUTHOR);
2021 MODULE_DESCRIPTION(DRIVER_DESC);
2022 MODULE_VERSION(DRIVER_VERSION);
2023 MODULE_LICENSE("GPL");