1 // SPDX-License-Identifier: GPL-2.0+
3 * linux/drivers/usb/gadget/s3c2410_udc.c
5 * Samsung S3C24xx series on-chip full speed USB device controllers
7 * Copyright (C) 2004-2007 Herbert Pötzl - Arnaud Patard
8 * Additional cleanups by Ben Dooks <ben-linux@fluff.org>
11 #define pr_fmt(fmt) "s3c2410_udc: " fmt
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/delay.h>
16 #include <linux/ioport.h>
17 #include <linux/sched.h>
18 #include <linux/slab.h>
19 #include <linux/errno.h>
20 #include <linux/init.h>
21 #include <linux/timer.h>
22 #include <linux/list.h>
23 #include <linux/interrupt.h>
24 #include <linux/platform_device.h>
25 #include <linux/clk.h>
26 #include <linux/gpio.h>
27 #include <linux/prefetch.h>
30 #include <linux/debugfs.h>
31 #include <linux/seq_file.h>
33 #include <linux/usb.h>
34 #include <linux/usb/gadget.h>
36 #include <asm/byteorder.h>
38 #include <asm/unaligned.h>
39 #include <mach/irqs.h>
41 #include <mach/hardware.h>
43 #include <plat/regs-udc.h>
44 #include <linux/platform_data/usb-s3c2410_udc.h>
47 #include "s3c2410_udc.h"
49 #define DRIVER_DESC "S3C2410 USB Device Controller Gadget"
50 #define DRIVER_AUTHOR "Herbert Pötzl <herbert@13thfloor.at>, " \
51 "Arnaud Patard <arnaud.patard@rtp-net.org>"
53 static const char gadget_name[] = "s3c2410_udc";
54 static const char driver_desc[] = DRIVER_DESC;
56 static struct s3c2410_udc *the_controller;
57 static struct clk *udc_clock;
58 static struct clk *usb_bus_clock;
59 static void __iomem *base_addr;
60 static u64 rsrc_start;
62 static struct dentry *s3c2410_udc_debugfs_root;
64 static inline u32 udc_read(u32 reg)
66 return readb(base_addr + reg);
69 static inline void udc_write(u32 value, u32 reg)
71 writeb(value, base_addr + reg);
74 static inline void udc_writeb(void __iomem *base, u32 value, u32 reg)
76 writeb(value, base + reg);
79 static struct s3c2410_udc_mach_info *udc_info;
81 /*************************** DEBUG FUNCTION ***************************/
82 #define DEBUG_NORMAL 1
83 #define DEBUG_VERBOSE 2
85 #ifdef CONFIG_USB_S3C2410_DEBUG
86 #define USB_S3C2410_DEBUG_LEVEL 0
88 static uint32_t s3c2410_ticks = 0;
91 static void dprintk(int level, const char *fmt, ...)
93 static long prevticks;
94 static int invocation;
98 if (level > USB_S3C2410_DEBUG_LEVEL)
106 if (s3c2410_ticks != prevticks) {
107 prevticks = s3c2410_ticks;
111 pr_debug("%1lu.%02d USB: %pV", prevticks, invocation++, &vaf);
117 static void dprintk(int level, const char *fmt, ...)
122 static int s3c2410_udc_debugfs_show(struct seq_file *m, void *p)
124 u32 addr_reg, pwr_reg, ep_int_reg, usb_int_reg;
125 u32 ep_int_en_reg, usb_int_en_reg, ep0_csr;
126 u32 ep1_i_csr1, ep1_i_csr2, ep1_o_csr1, ep1_o_csr2;
127 u32 ep2_i_csr1, ep2_i_csr2, ep2_o_csr1, ep2_o_csr2;
129 addr_reg = udc_read(S3C2410_UDC_FUNC_ADDR_REG);
130 pwr_reg = udc_read(S3C2410_UDC_PWR_REG);
131 ep_int_reg = udc_read(S3C2410_UDC_EP_INT_REG);
132 usb_int_reg = udc_read(S3C2410_UDC_USB_INT_REG);
133 ep_int_en_reg = udc_read(S3C2410_UDC_EP_INT_EN_REG);
134 usb_int_en_reg = udc_read(S3C2410_UDC_USB_INT_EN_REG);
135 udc_write(0, S3C2410_UDC_INDEX_REG);
136 ep0_csr = udc_read(S3C2410_UDC_IN_CSR1_REG);
137 udc_write(1, S3C2410_UDC_INDEX_REG);
138 ep1_i_csr1 = udc_read(S3C2410_UDC_IN_CSR1_REG);
139 ep1_i_csr2 = udc_read(S3C2410_UDC_IN_CSR2_REG);
140 ep1_o_csr1 = udc_read(S3C2410_UDC_IN_CSR1_REG);
141 ep1_o_csr2 = udc_read(S3C2410_UDC_IN_CSR2_REG);
142 udc_write(2, S3C2410_UDC_INDEX_REG);
143 ep2_i_csr1 = udc_read(S3C2410_UDC_IN_CSR1_REG);
144 ep2_i_csr2 = udc_read(S3C2410_UDC_IN_CSR2_REG);
145 ep2_o_csr1 = udc_read(S3C2410_UDC_IN_CSR1_REG);
146 ep2_o_csr2 = udc_read(S3C2410_UDC_IN_CSR2_REG);
148 seq_printf(m, "FUNC_ADDR_REG : 0x%04X\n"
150 "EP_INT_REG : 0x%04X\n"
151 "USB_INT_REG : 0x%04X\n"
152 "EP_INT_EN_REG : 0x%04X\n"
153 "USB_INT_EN_REG : 0x%04X\n"
155 "EP1_I_CSR1 : 0x%04X\n"
156 "EP1_I_CSR2 : 0x%04X\n"
157 "EP1_O_CSR1 : 0x%04X\n"
158 "EP1_O_CSR2 : 0x%04X\n"
159 "EP2_I_CSR1 : 0x%04X\n"
160 "EP2_I_CSR2 : 0x%04X\n"
161 "EP2_O_CSR1 : 0x%04X\n"
162 "EP2_O_CSR2 : 0x%04X\n",
163 addr_reg, pwr_reg, ep_int_reg, usb_int_reg,
164 ep_int_en_reg, usb_int_en_reg, ep0_csr,
165 ep1_i_csr1, ep1_i_csr2, ep1_o_csr1, ep1_o_csr2,
166 ep2_i_csr1, ep2_i_csr2, ep2_o_csr1, ep2_o_csr2
171 DEFINE_SHOW_ATTRIBUTE(s3c2410_udc_debugfs);
175 static inline void s3c2410_udc_clear_ep0_opr(void __iomem *base)
177 udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
178 udc_writeb(base, S3C2410_UDC_EP0_CSR_SOPKTRDY,
179 S3C2410_UDC_EP0_CSR_REG);
182 static inline void s3c2410_udc_clear_ep0_sst(void __iomem *base)
184 udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
185 writeb(0x00, base + S3C2410_UDC_EP0_CSR_REG);
188 static inline void s3c2410_udc_clear_ep0_se(void __iomem *base)
190 udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
191 udc_writeb(base, S3C2410_UDC_EP0_CSR_SSE, S3C2410_UDC_EP0_CSR_REG);
194 static inline void s3c2410_udc_set_ep0_ipr(void __iomem *base)
196 udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
197 udc_writeb(base, S3C2410_UDC_EP0_CSR_IPKRDY, S3C2410_UDC_EP0_CSR_REG);
200 static inline void s3c2410_udc_set_ep0_de(void __iomem *base)
202 udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
203 udc_writeb(base, S3C2410_UDC_EP0_CSR_DE, S3C2410_UDC_EP0_CSR_REG);
206 inline void s3c2410_udc_set_ep0_ss(void __iomem *b)
208 udc_writeb(b, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
209 udc_writeb(b, S3C2410_UDC_EP0_CSR_SENDSTL, S3C2410_UDC_EP0_CSR_REG);
212 static inline void s3c2410_udc_set_ep0_de_out(void __iomem *base)
214 udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
216 udc_writeb(base, (S3C2410_UDC_EP0_CSR_SOPKTRDY
217 | S3C2410_UDC_EP0_CSR_DE),
218 S3C2410_UDC_EP0_CSR_REG);
221 static inline void s3c2410_udc_set_ep0_de_in(void __iomem *base)
223 udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
224 udc_writeb(base, (S3C2410_UDC_EP0_CSR_IPKRDY
225 | S3C2410_UDC_EP0_CSR_DE),
226 S3C2410_UDC_EP0_CSR_REG);
229 /*------------------------- I/O ----------------------------------*/
234 static void s3c2410_udc_done(struct s3c2410_ep *ep,
235 struct s3c2410_request *req, int status)
237 unsigned halted = ep->halted;
239 list_del_init(&req->queue);
241 if (likely(req->req.status == -EINPROGRESS))
242 req->req.status = status;
244 status = req->req.status;
247 usb_gadget_giveback_request(&ep->ep, &req->req);
251 static void s3c2410_udc_nuke(struct s3c2410_udc *udc,
252 struct s3c2410_ep *ep, int status)
254 while (!list_empty(&ep->queue)) {
255 struct s3c2410_request *req;
256 req = list_entry(ep->queue.next, struct s3c2410_request,
258 s3c2410_udc_done(ep, req, status);
262 static inline int s3c2410_udc_fifo_count_out(void)
266 tmp = udc_read(S3C2410_UDC_OUT_FIFO_CNT2_REG) << 8;
267 tmp |= udc_read(S3C2410_UDC_OUT_FIFO_CNT1_REG);
272 * s3c2410_udc_write_packet
274 static inline int s3c2410_udc_write_packet(int fifo,
275 struct s3c2410_request *req,
278 unsigned len = min(req->req.length - req->req.actual, max);
279 u8 *buf = req->req.buf + req->req.actual;
283 dprintk(DEBUG_VERBOSE, "%s %d %d %d %d\n", __func__,
284 req->req.actual, req->req.length, len, req->req.actual + len);
286 req->req.actual += len;
289 writesb(base_addr + fifo, buf, len);
294 * s3c2410_udc_write_fifo
296 * return: 0 = still running, 1 = completed, negative = errno
298 static int s3c2410_udc_write_fifo(struct s3c2410_ep *ep,
299 struct s3c2410_request *req)
307 idx = ep->bEndpointAddress & 0x7F;
313 fifo_reg = S3C2410_UDC_EP0_FIFO_REG;
316 fifo_reg = S3C2410_UDC_EP1_FIFO_REG;
319 fifo_reg = S3C2410_UDC_EP2_FIFO_REG;
322 fifo_reg = S3C2410_UDC_EP3_FIFO_REG;
325 fifo_reg = S3C2410_UDC_EP4_FIFO_REG;
329 count = s3c2410_udc_write_packet(fifo_reg, req, ep->ep.maxpacket);
331 /* last packet is often short (sometimes a zlp) */
332 if (count != ep->ep.maxpacket)
334 else if (req->req.length != req->req.actual || req->req.zero)
339 /* Only ep0 debug messages are interesting */
341 dprintk(DEBUG_NORMAL,
342 "Written ep%d %d.%d of %d b [last %d,z %d]\n",
343 idx, count, req->req.actual, req->req.length,
344 is_last, req->req.zero);
347 /* The order is important. It prevents sending 2 packets
348 * at the same time */
351 /* Reset signal => no need to say 'data sent' */
352 if (!(udc_read(S3C2410_UDC_USB_INT_REG)
353 & S3C2410_UDC_USBINT_RESET))
354 s3c2410_udc_set_ep0_de_in(base_addr);
355 ep->dev->ep0state = EP0_IDLE;
357 udc_write(idx, S3C2410_UDC_INDEX_REG);
358 ep_csr = udc_read(S3C2410_UDC_IN_CSR1_REG);
359 udc_write(idx, S3C2410_UDC_INDEX_REG);
360 udc_write(ep_csr | S3C2410_UDC_ICSR1_PKTRDY,
361 S3C2410_UDC_IN_CSR1_REG);
364 s3c2410_udc_done(ep, req, 0);
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_ipr(base_addr);
373 udc_write(idx, S3C2410_UDC_INDEX_REG);
374 ep_csr = udc_read(S3C2410_UDC_IN_CSR1_REG);
375 udc_write(idx, S3C2410_UDC_INDEX_REG);
376 udc_write(ep_csr | S3C2410_UDC_ICSR1_PKTRDY,
377 S3C2410_UDC_IN_CSR1_REG);
384 static inline int s3c2410_udc_read_packet(int fifo, u8 *buf,
385 struct s3c2410_request *req, unsigned avail)
389 len = min(req->req.length - req->req.actual, avail);
390 req->req.actual += len;
392 readsb(fifo + base_addr, buf, len);
397 * return: 0 = still running, 1 = queue empty, negative = errno
399 static int s3c2410_udc_read_fifo(struct s3c2410_ep *ep,
400 struct s3c2410_request *req)
404 unsigned bufferspace;
411 idx = ep->bEndpointAddress & 0x7F;
418 fifo_reg = S3C2410_UDC_EP0_FIFO_REG;
421 fifo_reg = S3C2410_UDC_EP1_FIFO_REG;
424 fifo_reg = S3C2410_UDC_EP2_FIFO_REG;
427 fifo_reg = S3C2410_UDC_EP3_FIFO_REG;
430 fifo_reg = S3C2410_UDC_EP4_FIFO_REG;
434 if (!req->req.length)
437 buf = req->req.buf + req->req.actual;
438 bufferspace = req->req.length - req->req.actual;
440 dprintk(DEBUG_NORMAL, "%s: buffer full!\n", __func__);
444 udc_write(idx, S3C2410_UDC_INDEX_REG);
446 fifo_count = s3c2410_udc_fifo_count_out();
447 dprintk(DEBUG_NORMAL, "%s fifo count : %d\n", __func__, fifo_count);
449 if (fifo_count > ep->ep.maxpacket)
450 avail = ep->ep.maxpacket;
454 fifo_count = s3c2410_udc_read_packet(fifo_reg, buf, req, avail);
456 /* checking this with ep0 is not accurate as we already
457 * read a control request
459 if (idx != 0 && fifo_count < ep->ep.maxpacket) {
461 /* overflowed this request? flush extra data */
462 if (fifo_count != avail)
463 req->req.status = -EOVERFLOW;
465 is_last = (req->req.length <= req->req.actual) ? 1 : 0;
468 udc_write(idx, S3C2410_UDC_INDEX_REG);
469 fifo_count = s3c2410_udc_fifo_count_out();
471 /* Only ep0 debug messages are interesting */
473 dprintk(DEBUG_VERBOSE, "%s fifo count : %d [last %d]\n",
474 __func__, fifo_count, is_last);
478 s3c2410_udc_set_ep0_de_out(base_addr);
479 ep->dev->ep0state = EP0_IDLE;
481 udc_write(idx, S3C2410_UDC_INDEX_REG);
482 ep_csr = udc_read(S3C2410_UDC_OUT_CSR1_REG);
483 udc_write(idx, S3C2410_UDC_INDEX_REG);
484 udc_write(ep_csr & ~S3C2410_UDC_OCSR1_PKTRDY,
485 S3C2410_UDC_OUT_CSR1_REG);
488 s3c2410_udc_done(ep, req, 0);
491 s3c2410_udc_clear_ep0_opr(base_addr);
493 udc_write(idx, S3C2410_UDC_INDEX_REG);
494 ep_csr = udc_read(S3C2410_UDC_OUT_CSR1_REG);
495 udc_write(idx, S3C2410_UDC_INDEX_REG);
496 udc_write(ep_csr & ~S3C2410_UDC_OCSR1_PKTRDY,
497 S3C2410_UDC_OUT_CSR1_REG);
504 static int s3c2410_udc_read_fifo_crq(struct usb_ctrlrequest *crq)
506 unsigned char *outbuf = (unsigned char *)crq;
509 udc_write(0, S3C2410_UDC_INDEX_REG);
511 bytes_read = s3c2410_udc_fifo_count_out();
513 dprintk(DEBUG_NORMAL, "%s: fifo_count=%d\n", __func__, bytes_read);
515 if (bytes_read > sizeof(struct usb_ctrlrequest))
516 bytes_read = sizeof(struct usb_ctrlrequest);
518 readsb(S3C2410_UDC_EP0_FIFO_REG + base_addr, outbuf, bytes_read);
520 dprintk(DEBUG_VERBOSE, "%s: len=%d %02x:%02x {%x,%x,%x}\n", __func__,
521 bytes_read, crq->bRequest, crq->bRequestType,
522 crq->wValue, crq->wIndex, crq->wLength);
527 static int s3c2410_udc_get_status(struct s3c2410_udc *dev,
528 struct usb_ctrlrequest *crq)
531 u8 ep_num = crq->wIndex & 0x7F;
532 u8 is_in = crq->wIndex & USB_DIR_IN;
534 switch (crq->bRequestType & USB_RECIP_MASK) {
535 case USB_RECIP_INTERFACE:
538 case USB_RECIP_DEVICE:
539 status = dev->devstatus;
542 case USB_RECIP_ENDPOINT:
543 if (ep_num > 4 || crq->wLength > 2)
547 udc_write(0, S3C2410_UDC_INDEX_REG);
548 status = udc_read(S3C2410_UDC_IN_CSR1_REG);
549 status = status & S3C2410_UDC_EP0_CSR_SENDSTL;
551 udc_write(ep_num, S3C2410_UDC_INDEX_REG);
553 status = udc_read(S3C2410_UDC_IN_CSR1_REG);
554 status = status & S3C2410_UDC_ICSR1_SENDSTL;
556 status = udc_read(S3C2410_UDC_OUT_CSR1_REG);
557 status = status & S3C2410_UDC_OCSR1_SENDSTL;
561 status = status ? 1 : 0;
568 /* Seems to be needed to get it working. ouch :( */
570 udc_write(status & 0xFF, S3C2410_UDC_EP0_FIFO_REG);
571 udc_write(status >> 8, S3C2410_UDC_EP0_FIFO_REG);
572 s3c2410_udc_set_ep0_de_in(base_addr);
576 /*------------------------- usb state machine -------------------------------*/
577 static int s3c2410_udc_set_halt(struct usb_ep *_ep, int value);
579 static void s3c2410_udc_handle_ep0_idle(struct s3c2410_udc *dev,
580 struct s3c2410_ep *ep,
581 struct usb_ctrlrequest *crq,
586 /* start control request? */
587 if (!(ep0csr & S3C2410_UDC_EP0_CSR_OPKRDY))
590 s3c2410_udc_nuke(dev, ep, -EPROTO);
592 len = s3c2410_udc_read_fifo_crq(crq);
593 if (len != sizeof(*crq)) {
594 dprintk(DEBUG_NORMAL, "setup begin: fifo READ ERROR"
595 " wanted %d bytes got %d. Stalling out...\n",
597 s3c2410_udc_set_ep0_ss(base_addr);
601 dprintk(DEBUG_NORMAL, "bRequest = %d bRequestType %d wLength = %d\n",
602 crq->bRequest, crq->bRequestType, crq->wLength);
604 /* cope with automagic for some standard requests. */
605 dev->req_std = (crq->bRequestType & USB_TYPE_MASK)
606 == USB_TYPE_STANDARD;
608 dev->req_pending = 1;
610 switch (crq->bRequest) {
611 case USB_REQ_SET_CONFIGURATION:
612 dprintk(DEBUG_NORMAL, "USB_REQ_SET_CONFIGURATION ...\n");
614 if (crq->bRequestType == USB_RECIP_DEVICE) {
616 s3c2410_udc_set_ep0_de_out(base_addr);
620 case USB_REQ_SET_INTERFACE:
621 dprintk(DEBUG_NORMAL, "USB_REQ_SET_INTERFACE ...\n");
623 if (crq->bRequestType == USB_RECIP_INTERFACE) {
625 s3c2410_udc_set_ep0_de_out(base_addr);
629 case USB_REQ_SET_ADDRESS:
630 dprintk(DEBUG_NORMAL, "USB_REQ_SET_ADDRESS ...\n");
632 if (crq->bRequestType == USB_RECIP_DEVICE) {
633 tmp = crq->wValue & 0x7F;
635 udc_write((tmp | S3C2410_UDC_FUNCADDR_UPDATE),
636 S3C2410_UDC_FUNC_ADDR_REG);
637 s3c2410_udc_set_ep0_de_out(base_addr);
642 case USB_REQ_GET_STATUS:
643 dprintk(DEBUG_NORMAL, "USB_REQ_GET_STATUS ...\n");
644 s3c2410_udc_clear_ep0_opr(base_addr);
647 if (!s3c2410_udc_get_status(dev, crq))
652 case USB_REQ_CLEAR_FEATURE:
653 s3c2410_udc_clear_ep0_opr(base_addr);
655 if (crq->bRequestType != USB_RECIP_ENDPOINT)
658 if (crq->wValue != USB_ENDPOINT_HALT || crq->wLength != 0)
661 s3c2410_udc_set_halt(&dev->ep[crq->wIndex & 0x7f].ep, 0);
662 s3c2410_udc_set_ep0_de_out(base_addr);
665 case USB_REQ_SET_FEATURE:
666 s3c2410_udc_clear_ep0_opr(base_addr);
668 if (crq->bRequestType != USB_RECIP_ENDPOINT)
671 if (crq->wValue != USB_ENDPOINT_HALT || crq->wLength != 0)
674 s3c2410_udc_set_halt(&dev->ep[crq->wIndex & 0x7f].ep, 1);
675 s3c2410_udc_set_ep0_de_out(base_addr);
679 s3c2410_udc_clear_ep0_opr(base_addr);
683 if (crq->bRequestType & USB_DIR_IN)
684 dev->ep0state = EP0_IN_DATA_PHASE;
686 dev->ep0state = EP0_OUT_DATA_PHASE;
691 /* deliver the request to the gadget driver */
692 ret = dev->driver->setup(&dev->gadget, crq);
694 if (dev->req_config) {
695 dprintk(DEBUG_NORMAL, "config change %02x fail %d?\n",
700 if (ret == -EOPNOTSUPP)
701 dprintk(DEBUG_NORMAL, "Operation not supported\n");
703 dprintk(DEBUG_NORMAL,
704 "dev->driver->setup failed. (%d)\n", ret);
707 s3c2410_udc_set_ep0_ss(base_addr);
708 s3c2410_udc_set_ep0_de_out(base_addr);
709 dev->ep0state = EP0_IDLE;
710 /* deferred i/o == no response yet */
711 } else if (dev->req_pending) {
712 dprintk(DEBUG_VERBOSE, "dev->req_pending... what now?\n");
713 dev->req_pending = 0;
716 dprintk(DEBUG_VERBOSE, "ep0state %s\n", ep0states[dev->ep0state]);
719 static void s3c2410_udc_handle_ep0(struct s3c2410_udc *dev)
722 struct s3c2410_ep *ep = &dev->ep[0];
723 struct s3c2410_request *req;
724 struct usb_ctrlrequest crq;
726 if (list_empty(&ep->queue))
729 req = list_entry(ep->queue.next, struct s3c2410_request, queue);
731 /* We make the assumption that S3C2410_UDC_IN_CSR1_REG equal to
732 * S3C2410_UDC_EP0_CSR_REG when index is zero */
734 udc_write(0, S3C2410_UDC_INDEX_REG);
735 ep0csr = udc_read(S3C2410_UDC_IN_CSR1_REG);
737 dprintk(DEBUG_NORMAL, "ep0csr %x ep0state %s\n",
738 ep0csr, ep0states[dev->ep0state]);
740 /* clear stall status */
741 if (ep0csr & S3C2410_UDC_EP0_CSR_SENTSTL) {
742 s3c2410_udc_nuke(dev, ep, -EPIPE);
743 dprintk(DEBUG_NORMAL, "... clear SENT_STALL ...\n");
744 s3c2410_udc_clear_ep0_sst(base_addr);
745 dev->ep0state = EP0_IDLE;
749 /* clear setup end */
750 if (ep0csr & S3C2410_UDC_EP0_CSR_SE) {
751 dprintk(DEBUG_NORMAL, "... serviced SETUP_END ...\n");
752 s3c2410_udc_nuke(dev, ep, 0);
753 s3c2410_udc_clear_ep0_se(base_addr);
754 dev->ep0state = EP0_IDLE;
757 switch (dev->ep0state) {
759 s3c2410_udc_handle_ep0_idle(dev, ep, &crq, ep0csr);
762 case EP0_IN_DATA_PHASE: /* GET_DESCRIPTOR etc */
763 dprintk(DEBUG_NORMAL, "EP0_IN_DATA_PHASE ... what now?\n");
764 if (!(ep0csr & S3C2410_UDC_EP0_CSR_IPKRDY) && req)
765 s3c2410_udc_write_fifo(ep, req);
768 case EP0_OUT_DATA_PHASE: /* SET_DESCRIPTOR etc */
769 dprintk(DEBUG_NORMAL, "EP0_OUT_DATA_PHASE ... what now?\n");
770 if ((ep0csr & S3C2410_UDC_EP0_CSR_OPKRDY) && req)
771 s3c2410_udc_read_fifo(ep, req);
775 dprintk(DEBUG_NORMAL, "EP0_END_XFER ... what now?\n");
776 dev->ep0state = EP0_IDLE;
780 dprintk(DEBUG_NORMAL, "EP0_STALL ... what now?\n");
781 dev->ep0state = EP0_IDLE;
787 * handle_ep - Manage I/O endpoints
790 static void s3c2410_udc_handle_ep(struct s3c2410_ep *ep)
792 struct s3c2410_request *req;
793 int is_in = ep->bEndpointAddress & USB_DIR_IN;
797 if (likely(!list_empty(&ep->queue)))
798 req = list_entry(ep->queue.next,
799 struct s3c2410_request, queue);
803 idx = ep->bEndpointAddress & 0x7F;
806 udc_write(idx, S3C2410_UDC_INDEX_REG);
807 ep_csr1 = udc_read(S3C2410_UDC_IN_CSR1_REG);
808 dprintk(DEBUG_VERBOSE, "ep%01d write csr:%02x %d\n",
809 idx, ep_csr1, req ? 1 : 0);
811 if (ep_csr1 & S3C2410_UDC_ICSR1_SENTSTL) {
812 dprintk(DEBUG_VERBOSE, "st\n");
813 udc_write(idx, S3C2410_UDC_INDEX_REG);
814 udc_write(ep_csr1 & ~S3C2410_UDC_ICSR1_SENTSTL,
815 S3C2410_UDC_IN_CSR1_REG);
819 if (!(ep_csr1 & S3C2410_UDC_ICSR1_PKTRDY) && req)
820 s3c2410_udc_write_fifo(ep, req);
822 udc_write(idx, S3C2410_UDC_INDEX_REG);
823 ep_csr1 = udc_read(S3C2410_UDC_OUT_CSR1_REG);
824 dprintk(DEBUG_VERBOSE, "ep%01d rd csr:%02x\n", idx, ep_csr1);
826 if (ep_csr1 & S3C2410_UDC_OCSR1_SENTSTL) {
827 udc_write(idx, S3C2410_UDC_INDEX_REG);
828 udc_write(ep_csr1 & ~S3C2410_UDC_OCSR1_SENTSTL,
829 S3C2410_UDC_OUT_CSR1_REG);
833 if ((ep_csr1 & S3C2410_UDC_OCSR1_PKTRDY) && req)
834 s3c2410_udc_read_fifo(ep, req);
838 #include <mach/regs-irq.h>
841 * s3c2410_udc_irq - interrupt handler
843 static irqreturn_t s3c2410_udc_irq(int dummy, void *_dev)
845 struct s3c2410_udc *dev = _dev;
854 spin_lock_irqsave(&dev->lock, flags);
856 /* Driver connected ? */
858 /* Clear interrupts */
859 udc_write(udc_read(S3C2410_UDC_USB_INT_REG),
860 S3C2410_UDC_USB_INT_REG);
861 udc_write(udc_read(S3C2410_UDC_EP_INT_REG),
862 S3C2410_UDC_EP_INT_REG);
866 idx = udc_read(S3C2410_UDC_INDEX_REG);
868 /* Read status registers */
869 usb_status = udc_read(S3C2410_UDC_USB_INT_REG);
870 usbd_status = udc_read(S3C2410_UDC_EP_INT_REG);
871 pwr_reg = udc_read(S3C2410_UDC_PWR_REG);
873 udc_writeb(base_addr, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
874 ep0csr = udc_read(S3C2410_UDC_IN_CSR1_REG);
876 dprintk(DEBUG_NORMAL, "usbs=%02x, usbds=%02x, pwr=%02x ep0csr=%02x\n",
877 usb_status, usbd_status, pwr_reg, ep0csr);
880 * Now, handle interrupts. There's two types :
881 * - Reset, Resume, Suspend coming -> usb_int_reg
886 if (usb_status & S3C2410_UDC_USBINT_RESET) {
887 /* two kind of reset :
888 * - reset start -> pwr reg = 8
889 * - reset end -> pwr reg = 0
891 dprintk(DEBUG_NORMAL, "USB reset csr %x pwr %x\n",
894 dev->gadget.speed = USB_SPEED_UNKNOWN;
895 udc_write(0x00, S3C2410_UDC_INDEX_REG);
896 udc_write((dev->ep[0].ep.maxpacket & 0x7ff) >> 3,
897 S3C2410_UDC_MAXP_REG);
900 dev->ep0state = EP0_IDLE;
901 dev->gadget.speed = USB_SPEED_FULL;
903 /* clear interrupt */
904 udc_write(S3C2410_UDC_USBINT_RESET,
905 S3C2410_UDC_USB_INT_REG);
907 udc_write(idx, S3C2410_UDC_INDEX_REG);
908 spin_unlock_irqrestore(&dev->lock, flags);
913 if (usb_status & S3C2410_UDC_USBINT_RESUME) {
914 dprintk(DEBUG_NORMAL, "USB resume\n");
916 /* clear interrupt */
917 udc_write(S3C2410_UDC_USBINT_RESUME,
918 S3C2410_UDC_USB_INT_REG);
920 if (dev->gadget.speed != USB_SPEED_UNKNOWN
922 && dev->driver->resume)
923 dev->driver->resume(&dev->gadget);
927 if (usb_status & S3C2410_UDC_USBINT_SUSPEND) {
928 dprintk(DEBUG_NORMAL, "USB suspend\n");
930 /* clear interrupt */
931 udc_write(S3C2410_UDC_USBINT_SUSPEND,
932 S3C2410_UDC_USB_INT_REG);
934 if (dev->gadget.speed != USB_SPEED_UNKNOWN
936 && dev->driver->suspend)
937 dev->driver->suspend(&dev->gadget);
939 dev->ep0state = EP0_IDLE;
943 /* control traffic */
944 /* check on ep0csr != 0 is not a good idea as clearing in_pkt_ready
945 * generate an interrupt
947 if (usbd_status & S3C2410_UDC_INT_EP0) {
948 dprintk(DEBUG_VERBOSE, "USB ep0 irq\n");
949 /* Clear the interrupt bit by setting it to 1 */
950 udc_write(S3C2410_UDC_INT_EP0, S3C2410_UDC_EP_INT_REG);
951 s3c2410_udc_handle_ep0(dev);
954 /* endpoint data transfers */
955 for (i = 1; i < S3C2410_ENDPOINTS; i++) {
957 if (usbd_status & tmp) {
958 dprintk(DEBUG_VERBOSE, "USB ep%d irq\n", i);
960 /* Clear the interrupt bit by setting it to 1 */
961 udc_write(tmp, S3C2410_UDC_EP_INT_REG);
962 s3c2410_udc_handle_ep(&dev->ep[i]);
966 /* what else causes this interrupt? a receive! who is it? */
967 if (!usb_status && !usbd_status && !pwr_reg && !ep0csr) {
968 for (i = 1; i < S3C2410_ENDPOINTS; i++) {
969 idx2 = udc_read(S3C2410_UDC_INDEX_REG);
970 udc_write(i, S3C2410_UDC_INDEX_REG);
972 if (udc_read(S3C2410_UDC_OUT_CSR1_REG) & 0x1)
973 s3c2410_udc_handle_ep(&dev->ep[i]);
976 udc_write(idx2, S3C2410_UDC_INDEX_REG);
980 dprintk(DEBUG_VERBOSE, "irq: %d s3c2410_udc_done.\n", IRQ_USBD);
982 /* Restore old index */
983 udc_write(idx, S3C2410_UDC_INDEX_REG);
985 spin_unlock_irqrestore(&dev->lock, flags);
989 /*------------------------- s3c2410_ep_ops ----------------------------------*/
991 static inline struct s3c2410_ep *to_s3c2410_ep(struct usb_ep *ep)
993 return container_of(ep, struct s3c2410_ep, ep);
996 static inline struct s3c2410_udc *to_s3c2410_udc(struct usb_gadget *gadget)
998 return container_of(gadget, struct s3c2410_udc, gadget);
1001 static inline struct s3c2410_request *to_s3c2410_req(struct usb_request *req)
1003 return container_of(req, struct s3c2410_request, req);
1007 * s3c2410_udc_ep_enable
1009 static int s3c2410_udc_ep_enable(struct usb_ep *_ep,
1010 const struct usb_endpoint_descriptor *desc)
1012 struct s3c2410_udc *dev;
1013 struct s3c2410_ep *ep;
1015 unsigned long flags;
1019 ep = to_s3c2410_ep(_ep);
1022 || _ep->name == ep0name
1023 || desc->bDescriptorType != USB_DT_ENDPOINT)
1027 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
1030 max = usb_endpoint_maxp(desc);
1032 local_irq_save(flags);
1033 _ep->maxpacket = max;
1036 ep->bEndpointAddress = desc->bEndpointAddress;
1038 /* set max packet */
1039 udc_write(ep->num, S3C2410_UDC_INDEX_REG);
1040 udc_write(max >> 3, S3C2410_UDC_MAXP_REG);
1042 /* set type, direction, address; reset fifo counters */
1043 if (desc->bEndpointAddress & USB_DIR_IN) {
1044 csr1 = S3C2410_UDC_ICSR1_FFLUSH|S3C2410_UDC_ICSR1_CLRDT;
1045 csr2 = S3C2410_UDC_ICSR2_MODEIN|S3C2410_UDC_ICSR2_DMAIEN;
1047 udc_write(ep->num, S3C2410_UDC_INDEX_REG);
1048 udc_write(csr1, S3C2410_UDC_IN_CSR1_REG);
1049 udc_write(ep->num, S3C2410_UDC_INDEX_REG);
1050 udc_write(csr2, S3C2410_UDC_IN_CSR2_REG);
1052 /* don't flush in fifo or it will cause endpoint interrupt */
1053 csr1 = S3C2410_UDC_ICSR1_CLRDT;
1054 csr2 = S3C2410_UDC_ICSR2_DMAIEN;
1056 udc_write(ep->num, S3C2410_UDC_INDEX_REG);
1057 udc_write(csr1, S3C2410_UDC_IN_CSR1_REG);
1058 udc_write(ep->num, S3C2410_UDC_INDEX_REG);
1059 udc_write(csr2, S3C2410_UDC_IN_CSR2_REG);
1061 csr1 = S3C2410_UDC_OCSR1_FFLUSH | S3C2410_UDC_OCSR1_CLRDT;
1062 csr2 = S3C2410_UDC_OCSR2_DMAIEN;
1064 udc_write(ep->num, S3C2410_UDC_INDEX_REG);
1065 udc_write(csr1, S3C2410_UDC_OUT_CSR1_REG);
1066 udc_write(ep->num, S3C2410_UDC_INDEX_REG);
1067 udc_write(csr2, S3C2410_UDC_OUT_CSR2_REG);
1071 int_en_reg = udc_read(S3C2410_UDC_EP_INT_EN_REG);
1072 udc_write(int_en_reg | (1 << ep->num), S3C2410_UDC_EP_INT_EN_REG);
1074 /* print some debug message */
1075 tmp = desc->bEndpointAddress;
1076 dprintk(DEBUG_NORMAL, "enable %s(%d) ep%x%s-blk max %02x\n",
1077 _ep->name, ep->num, tmp,
1078 desc->bEndpointAddress & USB_DIR_IN ? "in" : "out", max);
1080 local_irq_restore(flags);
1081 s3c2410_udc_set_halt(_ep, 0);
1087 * s3c2410_udc_ep_disable
1089 static int s3c2410_udc_ep_disable(struct usb_ep *_ep)
1091 struct s3c2410_ep *ep = to_s3c2410_ep(_ep);
1092 unsigned long flags;
1095 if (!_ep || !ep->ep.desc) {
1096 dprintk(DEBUG_NORMAL, "%s not enabled\n",
1097 _ep ? ep->ep.name : NULL);
1101 local_irq_save(flags);
1103 dprintk(DEBUG_NORMAL, "ep_disable: %s\n", _ep->name);
1108 s3c2410_udc_nuke(ep->dev, ep, -ESHUTDOWN);
1111 int_en_reg = udc_read(S3C2410_UDC_EP_INT_EN_REG);
1112 udc_write(int_en_reg & ~(1<<ep->num), S3C2410_UDC_EP_INT_EN_REG);
1114 local_irq_restore(flags);
1116 dprintk(DEBUG_NORMAL, "%s disabled\n", _ep->name);
1122 * s3c2410_udc_alloc_request
1124 static struct usb_request *
1125 s3c2410_udc_alloc_request(struct usb_ep *_ep, gfp_t mem_flags)
1127 struct s3c2410_request *req;
1129 dprintk(DEBUG_VERBOSE, "%s(%p,%d)\n", __func__, _ep, mem_flags);
1134 req = kzalloc(sizeof(struct s3c2410_request), mem_flags);
1138 INIT_LIST_HEAD(&req->queue);
1143 * s3c2410_udc_free_request
1146 s3c2410_udc_free_request(struct usb_ep *_ep, struct usb_request *_req)
1148 struct s3c2410_ep *ep = to_s3c2410_ep(_ep);
1149 struct s3c2410_request *req = to_s3c2410_req(_req);
1151 dprintk(DEBUG_VERBOSE, "%s(%p,%p)\n", __func__, _ep, _req);
1153 if (!ep || !_req || (!ep->ep.desc && _ep->name != ep0name))
1156 WARN_ON(!list_empty(&req->queue));
1163 static int s3c2410_udc_queue(struct usb_ep *_ep, struct usb_request *_req,
1166 struct s3c2410_request *req = to_s3c2410_req(_req);
1167 struct s3c2410_ep *ep = to_s3c2410_ep(_ep);
1168 struct s3c2410_udc *dev;
1171 unsigned long flags;
1173 if (unlikely(!_ep || (!ep->ep.desc && ep->ep.name != ep0name))) {
1174 dprintk(DEBUG_NORMAL, "%s: invalid args\n", __func__);
1179 if (unlikely(!dev->driver
1180 || dev->gadget.speed == USB_SPEED_UNKNOWN)) {
1184 local_irq_save(flags);
1186 if (unlikely(!_req || !_req->complete
1187 || !_req->buf || !list_empty(&req->queue))) {
1189 dprintk(DEBUG_NORMAL, "%s: 1 X X X\n", __func__);
1191 dprintk(DEBUG_NORMAL, "%s: 0 %01d %01d %01d\n",
1192 __func__, !_req->complete, !_req->buf,
1193 !list_empty(&req->queue));
1196 local_irq_restore(flags);
1200 _req->status = -EINPROGRESS;
1203 dprintk(DEBUG_VERBOSE, "%s: ep%x len %d\n",
1204 __func__, ep->bEndpointAddress, _req->length);
1206 if (ep->bEndpointAddress) {
1207 udc_write(ep->bEndpointAddress & 0x7F, S3C2410_UDC_INDEX_REG);
1209 ep_csr = udc_read((ep->bEndpointAddress & USB_DIR_IN)
1210 ? S3C2410_UDC_IN_CSR1_REG
1211 : S3C2410_UDC_OUT_CSR1_REG);
1212 fifo_count = s3c2410_udc_fifo_count_out();
1214 udc_write(0, S3C2410_UDC_INDEX_REG);
1215 ep_csr = udc_read(S3C2410_UDC_IN_CSR1_REG);
1216 fifo_count = s3c2410_udc_fifo_count_out();
1219 /* kickstart this i/o queue? */
1220 if (list_empty(&ep->queue) && !ep->halted) {
1221 if (ep->bEndpointAddress == 0 /* ep0 */) {
1222 switch (dev->ep0state) {
1223 case EP0_IN_DATA_PHASE:
1224 if (!(ep_csr&S3C2410_UDC_EP0_CSR_IPKRDY)
1225 && s3c2410_udc_write_fifo(ep,
1227 dev->ep0state = EP0_IDLE;
1232 case EP0_OUT_DATA_PHASE:
1234 || ((ep_csr & S3C2410_UDC_OCSR1_PKTRDY)
1235 && s3c2410_udc_read_fifo(ep,
1237 dev->ep0state = EP0_IDLE;
1243 local_irq_restore(flags);
1246 } else if ((ep->bEndpointAddress & USB_DIR_IN) != 0
1247 && (!(ep_csr&S3C2410_UDC_OCSR1_PKTRDY))
1248 && s3c2410_udc_write_fifo(ep, req)) {
1250 } else if ((ep_csr & S3C2410_UDC_OCSR1_PKTRDY)
1252 && s3c2410_udc_read_fifo(ep, req)) {
1257 /* pio or dma irq handler advances the queue. */
1259 list_add_tail(&req->queue, &ep->queue);
1261 local_irq_restore(flags);
1263 dprintk(DEBUG_VERBOSE, "%s ok\n", __func__);
1268 * s3c2410_udc_dequeue
1270 static int s3c2410_udc_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1272 struct s3c2410_ep *ep = to_s3c2410_ep(_ep);
1273 struct s3c2410_udc *udc;
1274 int retval = -EINVAL;
1275 unsigned long flags;
1276 struct s3c2410_request *req = NULL;
1278 dprintk(DEBUG_VERBOSE, "%s(%p,%p)\n", __func__, _ep, _req);
1280 if (!the_controller->driver)
1286 udc = to_s3c2410_udc(ep->gadget);
1288 local_irq_save(flags);
1290 list_for_each_entry(req, &ep->queue, queue) {
1291 if (&req->req == _req) {
1292 list_del_init(&req->queue);
1293 _req->status = -ECONNRESET;
1300 dprintk(DEBUG_VERBOSE,
1301 "dequeued req %p from %s, len %d buf %p\n",
1302 req, _ep->name, _req->length, _req->buf);
1304 s3c2410_udc_done(ep, req, -ECONNRESET);
1307 local_irq_restore(flags);
1312 * s3c2410_udc_set_halt
1314 static int s3c2410_udc_set_halt(struct usb_ep *_ep, int value)
1316 struct s3c2410_ep *ep = to_s3c2410_ep(_ep);
1318 unsigned long flags;
1321 if (unlikely(!_ep || (!ep->ep.desc && ep->ep.name != ep0name))) {
1322 dprintk(DEBUG_NORMAL, "%s: inval 2\n", __func__);
1326 local_irq_save(flags);
1328 idx = ep->bEndpointAddress & 0x7F;
1331 s3c2410_udc_set_ep0_ss(base_addr);
1332 s3c2410_udc_set_ep0_de_out(base_addr);
1334 udc_write(idx, S3C2410_UDC_INDEX_REG);
1335 ep_csr = udc_read((ep->bEndpointAddress & USB_DIR_IN)
1336 ? S3C2410_UDC_IN_CSR1_REG
1337 : S3C2410_UDC_OUT_CSR1_REG);
1339 if ((ep->bEndpointAddress & USB_DIR_IN) != 0) {
1341 udc_write(ep_csr | S3C2410_UDC_ICSR1_SENDSTL,
1342 S3C2410_UDC_IN_CSR1_REG);
1344 ep_csr &= ~S3C2410_UDC_ICSR1_SENDSTL;
1345 udc_write(ep_csr, S3C2410_UDC_IN_CSR1_REG);
1346 ep_csr |= S3C2410_UDC_ICSR1_CLRDT;
1347 udc_write(ep_csr, S3C2410_UDC_IN_CSR1_REG);
1351 udc_write(ep_csr | S3C2410_UDC_OCSR1_SENDSTL,
1352 S3C2410_UDC_OUT_CSR1_REG);
1354 ep_csr &= ~S3C2410_UDC_OCSR1_SENDSTL;
1355 udc_write(ep_csr, S3C2410_UDC_OUT_CSR1_REG);
1356 ep_csr |= S3C2410_UDC_OCSR1_CLRDT;
1357 udc_write(ep_csr, S3C2410_UDC_OUT_CSR1_REG);
1362 ep->halted = value ? 1 : 0;
1363 local_irq_restore(flags);
1368 static const struct usb_ep_ops s3c2410_ep_ops = {
1369 .enable = s3c2410_udc_ep_enable,
1370 .disable = s3c2410_udc_ep_disable,
1372 .alloc_request = s3c2410_udc_alloc_request,
1373 .free_request = s3c2410_udc_free_request,
1375 .queue = s3c2410_udc_queue,
1376 .dequeue = s3c2410_udc_dequeue,
1378 .set_halt = s3c2410_udc_set_halt,
1381 /*------------------------- usb_gadget_ops ----------------------------------*/
1384 * s3c2410_udc_get_frame
1386 static int s3c2410_udc_get_frame(struct usb_gadget *_gadget)
1390 dprintk(DEBUG_VERBOSE, "%s()\n", __func__);
1392 tmp = udc_read(S3C2410_UDC_FRAME_NUM2_REG) << 8;
1393 tmp |= udc_read(S3C2410_UDC_FRAME_NUM1_REG);
1398 * s3c2410_udc_wakeup
1400 static int s3c2410_udc_wakeup(struct usb_gadget *_gadget)
1402 dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1407 * s3c2410_udc_set_selfpowered
1409 static int s3c2410_udc_set_selfpowered(struct usb_gadget *gadget, int value)
1411 struct s3c2410_udc *udc = to_s3c2410_udc(gadget);
1413 dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1415 gadget->is_selfpowered = (value != 0);
1417 udc->devstatus |= (1 << USB_DEVICE_SELF_POWERED);
1419 udc->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
1424 static void s3c2410_udc_disable(struct s3c2410_udc *dev);
1425 static void s3c2410_udc_enable(struct s3c2410_udc *dev);
1427 static int s3c2410_udc_set_pullup(struct s3c2410_udc *udc, int is_on)
1429 dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1431 if (udc_info && (udc_info->udc_command ||
1432 gpio_is_valid(udc_info->pullup_pin))) {
1435 s3c2410_udc_enable(udc);
1437 if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
1438 if (udc->driver && udc->driver->disconnect)
1439 udc->driver->disconnect(&udc->gadget);
1442 s3c2410_udc_disable(udc);
1451 static int s3c2410_udc_vbus_session(struct usb_gadget *gadget, int is_active)
1453 struct s3c2410_udc *udc = to_s3c2410_udc(gadget);
1455 dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1457 udc->vbus = (is_active != 0);
1458 s3c2410_udc_set_pullup(udc, is_active);
1462 static int s3c2410_udc_pullup(struct usb_gadget *gadget, int is_on)
1464 struct s3c2410_udc *udc = to_s3c2410_udc(gadget);
1466 dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1468 s3c2410_udc_set_pullup(udc, is_on);
1472 static irqreturn_t s3c2410_udc_vbus_irq(int irq, void *_dev)
1474 struct s3c2410_udc *dev = _dev;
1477 dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1479 value = gpio_get_value(udc_info->vbus_pin) ? 1 : 0;
1480 if (udc_info->vbus_pin_inverted)
1483 if (value != dev->vbus)
1484 s3c2410_udc_vbus_session(&dev->gadget, value);
1489 static int s3c2410_vbus_draw(struct usb_gadget *_gadget, unsigned ma)
1491 dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1493 if (udc_info && udc_info->vbus_draw) {
1494 udc_info->vbus_draw(ma);
1501 static int s3c2410_udc_start(struct usb_gadget *g,
1502 struct usb_gadget_driver *driver);
1503 static int s3c2410_udc_stop(struct usb_gadget *g);
1505 static const struct usb_gadget_ops s3c2410_ops = {
1506 .get_frame = s3c2410_udc_get_frame,
1507 .wakeup = s3c2410_udc_wakeup,
1508 .set_selfpowered = s3c2410_udc_set_selfpowered,
1509 .pullup = s3c2410_udc_pullup,
1510 .vbus_session = s3c2410_udc_vbus_session,
1511 .vbus_draw = s3c2410_vbus_draw,
1512 .udc_start = s3c2410_udc_start,
1513 .udc_stop = s3c2410_udc_stop,
1516 static void s3c2410_udc_command(enum s3c2410_udc_cmd_e cmd)
1521 if (udc_info->udc_command) {
1522 udc_info->udc_command(cmd);
1523 } else if (gpio_is_valid(udc_info->pullup_pin)) {
1527 case S3C2410_UDC_P_ENABLE:
1530 case S3C2410_UDC_P_DISABLE:
1536 value ^= udc_info->pullup_pin_inverted;
1538 gpio_set_value(udc_info->pullup_pin, value);
1542 /*------------------------- gadget driver handling---------------------------*/
1544 * s3c2410_udc_disable
1546 static void s3c2410_udc_disable(struct s3c2410_udc *dev)
1548 dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1550 /* Disable all interrupts */
1551 udc_write(0x00, S3C2410_UDC_USB_INT_EN_REG);
1552 udc_write(0x00, S3C2410_UDC_EP_INT_EN_REG);
1554 /* Clear the interrupt registers */
1555 udc_write(S3C2410_UDC_USBINT_RESET
1556 | S3C2410_UDC_USBINT_RESUME
1557 | S3C2410_UDC_USBINT_SUSPEND,
1558 S3C2410_UDC_USB_INT_REG);
1560 udc_write(0x1F, S3C2410_UDC_EP_INT_REG);
1562 /* Good bye, cruel world */
1563 s3c2410_udc_command(S3C2410_UDC_P_DISABLE);
1565 /* Set speed to unknown */
1566 dev->gadget.speed = USB_SPEED_UNKNOWN;
1570 * s3c2410_udc_reinit
1572 static void s3c2410_udc_reinit(struct s3c2410_udc *dev)
1576 /* device/ep0 records init */
1577 INIT_LIST_HEAD(&dev->gadget.ep_list);
1578 INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
1579 dev->ep0state = EP0_IDLE;
1581 for (i = 0; i < S3C2410_ENDPOINTS; i++) {
1582 struct s3c2410_ep *ep = &dev->ep[i];
1585 list_add_tail(&ep->ep.ep_list, &dev->gadget.ep_list);
1590 INIT_LIST_HEAD(&ep->queue);
1591 usb_ep_set_maxpacket_limit(&ep->ep, ep->ep.maxpacket);
1596 * s3c2410_udc_enable
1598 static void s3c2410_udc_enable(struct s3c2410_udc *dev)
1602 dprintk(DEBUG_NORMAL, "s3c2410_udc_enable called\n");
1604 /* dev->gadget.speed = USB_SPEED_UNKNOWN; */
1605 dev->gadget.speed = USB_SPEED_FULL;
1607 /* Set MAXP for all endpoints */
1608 for (i = 0; i < S3C2410_ENDPOINTS; i++) {
1609 udc_write(i, S3C2410_UDC_INDEX_REG);
1610 udc_write((dev->ep[i].ep.maxpacket & 0x7ff) >> 3,
1611 S3C2410_UDC_MAXP_REG);
1614 /* Set default power state */
1615 udc_write(DEFAULT_POWER_STATE, S3C2410_UDC_PWR_REG);
1617 /* Enable reset and suspend interrupt interrupts */
1618 udc_write(S3C2410_UDC_USBINT_RESET | S3C2410_UDC_USBINT_SUSPEND,
1619 S3C2410_UDC_USB_INT_EN_REG);
1621 /* Enable ep0 interrupt */
1622 udc_write(S3C2410_UDC_INT_EP0, S3C2410_UDC_EP_INT_EN_REG);
1624 /* time to say "hello, world" */
1625 s3c2410_udc_command(S3C2410_UDC_P_ENABLE);
1628 static int s3c2410_udc_start(struct usb_gadget *g,
1629 struct usb_gadget_driver *driver)
1631 struct s3c2410_udc *udc = to_s3c2410(g);
1633 dprintk(DEBUG_NORMAL, "%s() '%s'\n", __func__, driver->driver.name);
1635 /* Hook the driver */
1636 udc->driver = driver;
1639 s3c2410_udc_enable(udc);
1644 static int s3c2410_udc_stop(struct usb_gadget *g)
1646 struct s3c2410_udc *udc = to_s3c2410(g);
1651 s3c2410_udc_disable(udc);
1656 /*---------------------------------------------------------------------------*/
1657 static struct s3c2410_udc memory = {
1659 .ops = &s3c2410_ops,
1660 .ep0 = &memory.ep[0].ep,
1661 .name = gadget_name,
1663 .init_name = "gadget",
1667 /* control endpoint */
1672 .ops = &s3c2410_ep_ops,
1673 .maxpacket = EP0_FIFO_SIZE,
1674 .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_CONTROL,
1675 USB_EP_CAPS_DIR_ALL),
1680 /* first group of endpoints */
1685 .ops = &s3c2410_ep_ops,
1686 .maxpacket = EP_FIFO_SIZE,
1687 .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK,
1688 USB_EP_CAPS_DIR_ALL),
1691 .fifo_size = EP_FIFO_SIZE,
1692 .bEndpointAddress = 1,
1693 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1699 .ops = &s3c2410_ep_ops,
1700 .maxpacket = EP_FIFO_SIZE,
1701 .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK,
1702 USB_EP_CAPS_DIR_ALL),
1705 .fifo_size = EP_FIFO_SIZE,
1706 .bEndpointAddress = 2,
1707 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1713 .ops = &s3c2410_ep_ops,
1714 .maxpacket = EP_FIFO_SIZE,
1715 .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK,
1716 USB_EP_CAPS_DIR_ALL),
1719 .fifo_size = EP_FIFO_SIZE,
1720 .bEndpointAddress = 3,
1721 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1727 .ops = &s3c2410_ep_ops,
1728 .maxpacket = EP_FIFO_SIZE,
1729 .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK,
1730 USB_EP_CAPS_DIR_ALL),
1733 .fifo_size = EP_FIFO_SIZE,
1734 .bEndpointAddress = 4,
1735 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1741 * probe - binds to the platform device
1743 static int s3c2410_udc_probe(struct platform_device *pdev)
1745 struct s3c2410_udc *udc = &memory;
1746 struct device *dev = &pdev->dev;
1750 dev_dbg(dev, "%s()\n", __func__);
1752 usb_bus_clock = clk_get(NULL, "usb-bus-gadget");
1753 if (IS_ERR(usb_bus_clock)) {
1754 dev_err(dev, "failed to get usb bus clock source\n");
1755 return PTR_ERR(usb_bus_clock);
1758 clk_prepare_enable(usb_bus_clock);
1760 udc_clock = clk_get(NULL, "usb-device");
1761 if (IS_ERR(udc_clock)) {
1762 dev_err(dev, "failed to get udc clock source\n");
1763 return PTR_ERR(udc_clock);
1766 clk_prepare_enable(udc_clock);
1770 dev_dbg(dev, "got and enabled clocks\n");
1772 if (strncmp(pdev->name, "s3c2440", 7) == 0) {
1773 dev_info(dev, "S3C2440: increasing FIFO to 128 bytes\n");
1774 memory.ep[1].fifo_size = S3C2440_EP_FIFO_SIZE;
1775 memory.ep[2].fifo_size = S3C2440_EP_FIFO_SIZE;
1776 memory.ep[3].fifo_size = S3C2440_EP_FIFO_SIZE;
1777 memory.ep[4].fifo_size = S3C2440_EP_FIFO_SIZE;
1780 spin_lock_init(&udc->lock);
1781 udc_info = dev_get_platdata(&pdev->dev);
1783 rsrc_start = S3C2410_PA_USBDEV;
1784 rsrc_len = S3C24XX_SZ_USBDEV;
1786 if (!request_mem_region(rsrc_start, rsrc_len, gadget_name))
1789 base_addr = ioremap(rsrc_start, rsrc_len);
1795 the_controller = udc;
1796 platform_set_drvdata(pdev, udc);
1798 s3c2410_udc_disable(udc);
1799 s3c2410_udc_reinit(udc);
1801 /* irq setup after old hardware state is cleaned up */
1802 retval = request_irq(IRQ_USBD, s3c2410_udc_irq,
1803 0, gadget_name, udc);
1806 dev_err(dev, "cannot get irq %i, err %d\n", IRQ_USBD, retval);
1811 dev_dbg(dev, "got irq %i\n", IRQ_USBD);
1813 if (udc_info && udc_info->vbus_pin > 0) {
1814 retval = gpio_request(udc_info->vbus_pin, "udc vbus");
1816 dev_err(dev, "cannot claim vbus pin\n");
1820 irq = gpio_to_irq(udc_info->vbus_pin);
1822 dev_err(dev, "no irq for gpio vbus pin\n");
1824 goto err_gpio_claim;
1827 retval = request_irq(irq, s3c2410_udc_vbus_irq,
1829 | IRQF_TRIGGER_FALLING | IRQF_SHARED,
1833 dev_err(dev, "can't get vbus irq %d, err %d\n",
1836 goto err_gpio_claim;
1839 dev_dbg(dev, "got irq %i\n", irq);
1844 if (udc_info && !udc_info->udc_command &&
1845 gpio_is_valid(udc_info->pullup_pin)) {
1847 retval = gpio_request_one(udc_info->pullup_pin,
1848 udc_info->vbus_pin_inverted ?
1849 GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW,
1855 retval = usb_add_gadget_udc(&pdev->dev, &udc->gadget);
1859 udc->regs_info = debugfs_create_file("registers", S_IRUGO,
1860 s3c2410_udc_debugfs_root, udc,
1861 &s3c2410_udc_debugfs_fops);
1863 dev_dbg(dev, "probe ok\n");
1868 if (udc_info && !udc_info->udc_command &&
1869 gpio_is_valid(udc_info->pullup_pin))
1870 gpio_free(udc_info->pullup_pin);
1872 if (udc_info && udc_info->vbus_pin > 0)
1873 free_irq(gpio_to_irq(udc_info->vbus_pin), udc);
1875 if (udc_info && udc_info->vbus_pin > 0)
1876 gpio_free(udc_info->vbus_pin);
1878 free_irq(IRQ_USBD, udc);
1882 release_mem_region(rsrc_start, rsrc_len);
1888 * s3c2410_udc_remove
1890 static int s3c2410_udc_remove(struct platform_device *pdev)
1892 struct s3c2410_udc *udc = platform_get_drvdata(pdev);
1895 dev_dbg(&pdev->dev, "%s()\n", __func__);
1900 usb_del_gadget_udc(&udc->gadget);
1901 debugfs_remove(udc->regs_info);
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 irq = gpio_to_irq(udc_info->vbus_pin);
1912 free_irq(IRQ_USBD, udc);
1915 release_mem_region(rsrc_start, rsrc_len);
1917 if (!IS_ERR(udc_clock) && udc_clock != NULL) {
1918 clk_disable_unprepare(udc_clock);
1923 if (!IS_ERR(usb_bus_clock) && usb_bus_clock != NULL) {
1924 clk_disable_unprepare(usb_bus_clock);
1925 clk_put(usb_bus_clock);
1926 usb_bus_clock = NULL;
1929 dev_dbg(&pdev->dev, "%s: remove ok\n", __func__);
1935 s3c2410_udc_suspend(struct platform_device *pdev, pm_message_t message)
1937 s3c2410_udc_command(S3C2410_UDC_P_DISABLE);
1942 static int s3c2410_udc_resume(struct platform_device *pdev)
1944 s3c2410_udc_command(S3C2410_UDC_P_ENABLE);
1949 #define s3c2410_udc_suspend NULL
1950 #define s3c2410_udc_resume NULL
1953 static const struct platform_device_id s3c_udc_ids[] = {
1954 { "s3c2410-usbgadget", },
1955 { "s3c2440-usbgadget", },
1958 MODULE_DEVICE_TABLE(platform, s3c_udc_ids);
1960 static struct platform_driver udc_driver_24x0 = {
1962 .name = "s3c24x0-usbgadget",
1964 .probe = s3c2410_udc_probe,
1965 .remove = s3c2410_udc_remove,
1966 .suspend = s3c2410_udc_suspend,
1967 .resume = s3c2410_udc_resume,
1968 .id_table = s3c_udc_ids,
1971 static int __init udc_init(void)
1975 dprintk(DEBUG_NORMAL, "%s\n", gadget_name);
1977 s3c2410_udc_debugfs_root = debugfs_create_dir(gadget_name,
1980 retval = platform_driver_register(&udc_driver_24x0);
1987 debugfs_remove(s3c2410_udc_debugfs_root);
1991 static void __exit udc_exit(void)
1993 platform_driver_unregister(&udc_driver_24x0);
1994 debugfs_remove_recursive(s3c2410_udc_debugfs_root);
1997 module_init(udc_init);
1998 module_exit(udc_exit);
2000 MODULE_AUTHOR(DRIVER_AUTHOR);
2001 MODULE_DESCRIPTION(DRIVER_DESC);
2002 MODULE_LICENSE("GPL");