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
)
273 if (&ep
->queue
== NULL
)
276 while (!list_empty(&ep
->queue
)) {
277 struct s3c2410_request
*req
;
278 req
= list_entry(ep
->queue
.next
, struct s3c2410_request
,
280 s3c2410_udc_done(ep
, req
, status
);
284 static inline int s3c2410_udc_fifo_count_out(void)
288 tmp
= udc_read(S3C2410_UDC_OUT_FIFO_CNT2_REG
) << 8;
289 tmp
|= udc_read(S3C2410_UDC_OUT_FIFO_CNT1_REG
);
294 * s3c2410_udc_write_packet
296 static inline int s3c2410_udc_write_packet(int fifo
,
297 struct s3c2410_request
*req
,
300 unsigned len
= min(req
->req
.length
- req
->req
.actual
, max
);
301 u8
*buf
= req
->req
.buf
+ req
->req
.actual
;
305 dprintk(DEBUG_VERBOSE
, "%s %d %d %d %d\n", __func__
,
306 req
->req
.actual
, req
->req
.length
, len
, req
->req
.actual
+ len
);
308 req
->req
.actual
+= len
;
311 writesb(base_addr
+ fifo
, buf
, len
);
316 * s3c2410_udc_write_fifo
318 * return: 0 = still running, 1 = completed, negative = errno
320 static int s3c2410_udc_write_fifo(struct s3c2410_ep
*ep
,
321 struct s3c2410_request
*req
)
329 idx
= ep
->bEndpointAddress
& 0x7F;
334 fifo_reg
= S3C2410_UDC_EP0_FIFO_REG
;
337 fifo_reg
= S3C2410_UDC_EP1_FIFO_REG
;
340 fifo_reg
= S3C2410_UDC_EP2_FIFO_REG
;
343 fifo_reg
= S3C2410_UDC_EP3_FIFO_REG
;
346 fifo_reg
= S3C2410_UDC_EP4_FIFO_REG
;
350 count
= s3c2410_udc_write_packet(fifo_reg
, req
, ep
->ep
.maxpacket
);
352 /* last packet is often short (sometimes a zlp) */
353 if (count
!= ep
->ep
.maxpacket
)
355 else if (req
->req
.length
!= req
->req
.actual
|| req
->req
.zero
)
360 /* Only ep0 debug messages are interesting */
362 dprintk(DEBUG_NORMAL
,
363 "Written ep%d %d.%d of %d b [last %d,z %d]\n",
364 idx
, count
, req
->req
.actual
, req
->req
.length
,
365 is_last
, req
->req
.zero
);
368 /* The order is important. It prevents sending 2 packets
369 * at the same time */
372 /* Reset signal => no need to say 'data sent' */
373 if (!(udc_read(S3C2410_UDC_USB_INT_REG
)
374 & S3C2410_UDC_USBINT_RESET
))
375 s3c2410_udc_set_ep0_de_in(base_addr
);
376 ep
->dev
->ep0state
= EP0_IDLE
;
378 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
379 ep_csr
= udc_read(S3C2410_UDC_IN_CSR1_REG
);
380 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
381 udc_write(ep_csr
| S3C2410_UDC_ICSR1_PKTRDY
,
382 S3C2410_UDC_IN_CSR1_REG
);
385 s3c2410_udc_done(ep
, req
, 0);
389 /* Reset signal => no need to say 'data sent' */
390 if (!(udc_read(S3C2410_UDC_USB_INT_REG
)
391 & S3C2410_UDC_USBINT_RESET
))
392 s3c2410_udc_set_ep0_ipr(base_addr
);
394 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
395 ep_csr
= udc_read(S3C2410_UDC_IN_CSR1_REG
);
396 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
397 udc_write(ep_csr
| S3C2410_UDC_ICSR1_PKTRDY
,
398 S3C2410_UDC_IN_CSR1_REG
);
405 static inline int s3c2410_udc_read_packet(int fifo
, u8
*buf
,
406 struct s3c2410_request
*req
, unsigned avail
)
410 len
= min(req
->req
.length
- req
->req
.actual
, avail
);
411 req
->req
.actual
+= len
;
413 readsb(fifo
+ base_addr
, buf
, len
);
418 * return: 0 = still running, 1 = queue empty, negative = errno
420 static int s3c2410_udc_read_fifo(struct s3c2410_ep
*ep
,
421 struct s3c2410_request
*req
)
425 unsigned bufferspace
;
432 idx
= ep
->bEndpointAddress
& 0x7F;
438 fifo_reg
= S3C2410_UDC_EP0_FIFO_REG
;
441 fifo_reg
= S3C2410_UDC_EP1_FIFO_REG
;
444 fifo_reg
= S3C2410_UDC_EP2_FIFO_REG
;
447 fifo_reg
= S3C2410_UDC_EP3_FIFO_REG
;
450 fifo_reg
= S3C2410_UDC_EP4_FIFO_REG
;
454 if (!req
->req
.length
)
457 buf
= req
->req
.buf
+ req
->req
.actual
;
458 bufferspace
= req
->req
.length
- req
->req
.actual
;
460 dprintk(DEBUG_NORMAL
, "%s: buffer full!\n", __func__
);
464 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
466 fifo_count
= s3c2410_udc_fifo_count_out();
467 dprintk(DEBUG_NORMAL
, "%s fifo count : %d\n", __func__
, fifo_count
);
469 if (fifo_count
> ep
->ep
.maxpacket
)
470 avail
= ep
->ep
.maxpacket
;
474 fifo_count
= s3c2410_udc_read_packet(fifo_reg
, buf
, req
, avail
);
476 /* checking this with ep0 is not accurate as we already
477 * read a control request
479 if (idx
!= 0 && fifo_count
< ep
->ep
.maxpacket
) {
481 /* overflowed this request? flush extra data */
482 if (fifo_count
!= avail
)
483 req
->req
.status
= -EOVERFLOW
;
485 is_last
= (req
->req
.length
<= req
->req
.actual
) ? 1 : 0;
488 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
489 fifo_count
= s3c2410_udc_fifo_count_out();
491 /* Only ep0 debug messages are interesting */
493 dprintk(DEBUG_VERBOSE
, "%s fifo count : %d [last %d]\n",
494 __func__
, fifo_count
, is_last
);
498 s3c2410_udc_set_ep0_de_out(base_addr
);
499 ep
->dev
->ep0state
= EP0_IDLE
;
501 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
502 ep_csr
= udc_read(S3C2410_UDC_OUT_CSR1_REG
);
503 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
504 udc_write(ep_csr
& ~S3C2410_UDC_OCSR1_PKTRDY
,
505 S3C2410_UDC_OUT_CSR1_REG
);
508 s3c2410_udc_done(ep
, req
, 0);
511 s3c2410_udc_clear_ep0_opr(base_addr
);
513 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
514 ep_csr
= udc_read(S3C2410_UDC_OUT_CSR1_REG
);
515 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
516 udc_write(ep_csr
& ~S3C2410_UDC_OCSR1_PKTRDY
,
517 S3C2410_UDC_OUT_CSR1_REG
);
524 static int s3c2410_udc_read_fifo_crq(struct usb_ctrlrequest
*crq
)
526 unsigned char *outbuf
= (unsigned char *)crq
;
529 udc_write(0, S3C2410_UDC_INDEX_REG
);
531 bytes_read
= s3c2410_udc_fifo_count_out();
533 dprintk(DEBUG_NORMAL
, "%s: fifo_count=%d\n", __func__
, bytes_read
);
535 if (bytes_read
> sizeof(struct usb_ctrlrequest
))
536 bytes_read
= sizeof(struct usb_ctrlrequest
);
538 readsb(S3C2410_UDC_EP0_FIFO_REG
+ base_addr
, outbuf
, bytes_read
);
540 dprintk(DEBUG_VERBOSE
, "%s: len=%d %02x:%02x {%x,%x,%x}\n", __func__
,
541 bytes_read
, crq
->bRequest
, crq
->bRequestType
,
542 crq
->wValue
, crq
->wIndex
, crq
->wLength
);
547 static int s3c2410_udc_get_status(struct s3c2410_udc
*dev
,
548 struct usb_ctrlrequest
*crq
)
551 u8 ep_num
= crq
->wIndex
& 0x7F;
552 u8 is_in
= crq
->wIndex
& USB_DIR_IN
;
554 switch (crq
->bRequestType
& USB_RECIP_MASK
) {
555 case USB_RECIP_INTERFACE
:
558 case USB_RECIP_DEVICE
:
559 status
= dev
->devstatus
;
562 case USB_RECIP_ENDPOINT
:
563 if (ep_num
> 4 || crq
->wLength
> 2)
567 udc_write(0, S3C2410_UDC_INDEX_REG
);
568 status
= udc_read(S3C2410_UDC_IN_CSR1_REG
);
569 status
= status
& S3C2410_UDC_EP0_CSR_SENDSTL
;
571 udc_write(ep_num
, S3C2410_UDC_INDEX_REG
);
573 status
= udc_read(S3C2410_UDC_IN_CSR1_REG
);
574 status
= status
& S3C2410_UDC_ICSR1_SENDSTL
;
576 status
= udc_read(S3C2410_UDC_OUT_CSR1_REG
);
577 status
= status
& S3C2410_UDC_OCSR1_SENDSTL
;
581 status
= status
? 1 : 0;
588 /* Seems to be needed to get it working. ouch :( */
590 udc_write(status
& 0xFF, S3C2410_UDC_EP0_FIFO_REG
);
591 udc_write(status
>> 8, S3C2410_UDC_EP0_FIFO_REG
);
592 s3c2410_udc_set_ep0_de_in(base_addr
);
596 /*------------------------- usb state machine -------------------------------*/
597 static int s3c2410_udc_set_halt(struct usb_ep
*_ep
, int value
);
599 static void s3c2410_udc_handle_ep0_idle(struct s3c2410_udc
*dev
,
600 struct s3c2410_ep
*ep
,
601 struct usb_ctrlrequest
*crq
,
606 /* start control request? */
607 if (!(ep0csr
& S3C2410_UDC_EP0_CSR_OPKRDY
))
610 s3c2410_udc_nuke(dev
, ep
, -EPROTO
);
612 len
= s3c2410_udc_read_fifo_crq(crq
);
613 if (len
!= sizeof(*crq
)) {
614 dprintk(DEBUG_NORMAL
, "setup begin: fifo READ ERROR"
615 " wanted %d bytes got %d. Stalling out...\n",
617 s3c2410_udc_set_ep0_ss(base_addr
);
621 dprintk(DEBUG_NORMAL
, "bRequest = %d bRequestType %d wLength = %d\n",
622 crq
->bRequest
, crq
->bRequestType
, crq
->wLength
);
624 /* cope with automagic for some standard requests. */
625 dev
->req_std
= (crq
->bRequestType
& USB_TYPE_MASK
)
626 == USB_TYPE_STANDARD
;
628 dev
->req_pending
= 1;
630 switch (crq
->bRequest
) {
631 case USB_REQ_SET_CONFIGURATION
:
632 dprintk(DEBUG_NORMAL
, "USB_REQ_SET_CONFIGURATION ...\n");
634 if (crq
->bRequestType
== USB_RECIP_DEVICE
) {
636 s3c2410_udc_set_ep0_de_out(base_addr
);
640 case USB_REQ_SET_INTERFACE
:
641 dprintk(DEBUG_NORMAL
, "USB_REQ_SET_INTERFACE ...\n");
643 if (crq
->bRequestType
== USB_RECIP_INTERFACE
) {
645 s3c2410_udc_set_ep0_de_out(base_addr
);
649 case USB_REQ_SET_ADDRESS
:
650 dprintk(DEBUG_NORMAL
, "USB_REQ_SET_ADDRESS ...\n");
652 if (crq
->bRequestType
== USB_RECIP_DEVICE
) {
653 tmp
= crq
->wValue
& 0x7F;
655 udc_write((tmp
| S3C2410_UDC_FUNCADDR_UPDATE
),
656 S3C2410_UDC_FUNC_ADDR_REG
);
657 s3c2410_udc_set_ep0_de_out(base_addr
);
662 case USB_REQ_GET_STATUS
:
663 dprintk(DEBUG_NORMAL
, "USB_REQ_GET_STATUS ...\n");
664 s3c2410_udc_clear_ep0_opr(base_addr
);
667 if (!s3c2410_udc_get_status(dev
, crq
))
672 case USB_REQ_CLEAR_FEATURE
:
673 s3c2410_udc_clear_ep0_opr(base_addr
);
675 if (crq
->bRequestType
!= USB_RECIP_ENDPOINT
)
678 if (crq
->wValue
!= USB_ENDPOINT_HALT
|| crq
->wLength
!= 0)
681 s3c2410_udc_set_halt(&dev
->ep
[crq
->wIndex
& 0x7f].ep
, 0);
682 s3c2410_udc_set_ep0_de_out(base_addr
);
685 case USB_REQ_SET_FEATURE
:
686 s3c2410_udc_clear_ep0_opr(base_addr
);
688 if (crq
->bRequestType
!= USB_RECIP_ENDPOINT
)
691 if (crq
->wValue
!= USB_ENDPOINT_HALT
|| crq
->wLength
!= 0)
694 s3c2410_udc_set_halt(&dev
->ep
[crq
->wIndex
& 0x7f].ep
, 1);
695 s3c2410_udc_set_ep0_de_out(base_addr
);
699 s3c2410_udc_clear_ep0_opr(base_addr
);
703 if (crq
->bRequestType
& USB_DIR_IN
)
704 dev
->ep0state
= EP0_IN_DATA_PHASE
;
706 dev
->ep0state
= EP0_OUT_DATA_PHASE
;
711 /* deliver the request to the gadget driver */
712 ret
= dev
->driver
->setup(&dev
->gadget
, crq
);
714 if (dev
->req_config
) {
715 dprintk(DEBUG_NORMAL
, "config change %02x fail %d?\n",
720 if (ret
== -EOPNOTSUPP
)
721 dprintk(DEBUG_NORMAL
, "Operation not supported\n");
723 dprintk(DEBUG_NORMAL
,
724 "dev->driver->setup failed. (%d)\n", ret
);
727 s3c2410_udc_set_ep0_ss(base_addr
);
728 s3c2410_udc_set_ep0_de_out(base_addr
);
729 dev
->ep0state
= EP0_IDLE
;
730 /* deferred i/o == no response yet */
731 } else if (dev
->req_pending
) {
732 dprintk(DEBUG_VERBOSE
, "dev->req_pending... what now?\n");
733 dev
->req_pending
= 0;
736 dprintk(DEBUG_VERBOSE
, "ep0state %s\n", ep0states
[dev
->ep0state
]);
739 static void s3c2410_udc_handle_ep0(struct s3c2410_udc
*dev
)
742 struct s3c2410_ep
*ep
= &dev
->ep
[0];
743 struct s3c2410_request
*req
;
744 struct usb_ctrlrequest crq
;
746 if (list_empty(&ep
->queue
))
749 req
= list_entry(ep
->queue
.next
, struct s3c2410_request
, queue
);
751 /* We make the assumption that S3C2410_UDC_IN_CSR1_REG equal to
752 * S3C2410_UDC_EP0_CSR_REG when index is zero */
754 udc_write(0, S3C2410_UDC_INDEX_REG
);
755 ep0csr
= udc_read(S3C2410_UDC_IN_CSR1_REG
);
757 dprintk(DEBUG_NORMAL
, "ep0csr %x ep0state %s\n",
758 ep0csr
, ep0states
[dev
->ep0state
]);
760 /* clear stall status */
761 if (ep0csr
& S3C2410_UDC_EP0_CSR_SENTSTL
) {
762 s3c2410_udc_nuke(dev
, ep
, -EPIPE
);
763 dprintk(DEBUG_NORMAL
, "... clear SENT_STALL ...\n");
764 s3c2410_udc_clear_ep0_sst(base_addr
);
765 dev
->ep0state
= EP0_IDLE
;
769 /* clear setup end */
770 if (ep0csr
& S3C2410_UDC_EP0_CSR_SE
) {
771 dprintk(DEBUG_NORMAL
, "... serviced SETUP_END ...\n");
772 s3c2410_udc_nuke(dev
, ep
, 0);
773 s3c2410_udc_clear_ep0_se(base_addr
);
774 dev
->ep0state
= EP0_IDLE
;
777 switch (dev
->ep0state
) {
779 s3c2410_udc_handle_ep0_idle(dev
, ep
, &crq
, ep0csr
);
782 case EP0_IN_DATA_PHASE
: /* GET_DESCRIPTOR etc */
783 dprintk(DEBUG_NORMAL
, "EP0_IN_DATA_PHASE ... what now?\n");
784 if (!(ep0csr
& S3C2410_UDC_EP0_CSR_IPKRDY
) && req
)
785 s3c2410_udc_write_fifo(ep
, req
);
788 case EP0_OUT_DATA_PHASE
: /* SET_DESCRIPTOR etc */
789 dprintk(DEBUG_NORMAL
, "EP0_OUT_DATA_PHASE ... what now?\n");
790 if ((ep0csr
& S3C2410_UDC_EP0_CSR_OPKRDY
) && req
)
791 s3c2410_udc_read_fifo(ep
, req
);
795 dprintk(DEBUG_NORMAL
, "EP0_END_XFER ... what now?\n");
796 dev
->ep0state
= EP0_IDLE
;
800 dprintk(DEBUG_NORMAL
, "EP0_STALL ... what now?\n");
801 dev
->ep0state
= EP0_IDLE
;
807 * handle_ep - Manage I/O endpoints
810 static void s3c2410_udc_handle_ep(struct s3c2410_ep
*ep
)
812 struct s3c2410_request
*req
;
813 int is_in
= ep
->bEndpointAddress
& USB_DIR_IN
;
817 if (likely(!list_empty(&ep
->queue
)))
818 req
= list_entry(ep
->queue
.next
,
819 struct s3c2410_request
, queue
);
823 idx
= ep
->bEndpointAddress
& 0x7F;
826 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
827 ep_csr1
= udc_read(S3C2410_UDC_IN_CSR1_REG
);
828 dprintk(DEBUG_VERBOSE
, "ep%01d write csr:%02x %d\n",
829 idx
, ep_csr1
, req
? 1 : 0);
831 if (ep_csr1
& S3C2410_UDC_ICSR1_SENTSTL
) {
832 dprintk(DEBUG_VERBOSE
, "st\n");
833 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
834 udc_write(ep_csr1
& ~S3C2410_UDC_ICSR1_SENTSTL
,
835 S3C2410_UDC_IN_CSR1_REG
);
839 if (!(ep_csr1
& S3C2410_UDC_ICSR1_PKTRDY
) && req
)
840 s3c2410_udc_write_fifo(ep
, req
);
842 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
843 ep_csr1
= udc_read(S3C2410_UDC_OUT_CSR1_REG
);
844 dprintk(DEBUG_VERBOSE
, "ep%01d rd csr:%02x\n", idx
, ep_csr1
);
846 if (ep_csr1
& S3C2410_UDC_OCSR1_SENTSTL
) {
847 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
848 udc_write(ep_csr1
& ~S3C2410_UDC_OCSR1_SENTSTL
,
849 S3C2410_UDC_OUT_CSR1_REG
);
853 if ((ep_csr1
& S3C2410_UDC_OCSR1_PKTRDY
) && req
)
854 s3c2410_udc_read_fifo(ep
, req
);
858 #include <mach/regs-irq.h>
861 * s3c2410_udc_irq - interrupt handler
863 static irqreturn_t
s3c2410_udc_irq(int dummy
, void *_dev
)
865 struct s3c2410_udc
*dev
= _dev
;
874 spin_lock_irqsave(&dev
->lock
, flags
);
876 /* Driver connected ? */
878 /* Clear interrupts */
879 udc_write(udc_read(S3C2410_UDC_USB_INT_REG
),
880 S3C2410_UDC_USB_INT_REG
);
881 udc_write(udc_read(S3C2410_UDC_EP_INT_REG
),
882 S3C2410_UDC_EP_INT_REG
);
886 idx
= udc_read(S3C2410_UDC_INDEX_REG
);
888 /* Read status registers */
889 usb_status
= udc_read(S3C2410_UDC_USB_INT_REG
);
890 usbd_status
= udc_read(S3C2410_UDC_EP_INT_REG
);
891 pwr_reg
= udc_read(S3C2410_UDC_PWR_REG
);
893 udc_writeb(base_addr
, S3C2410_UDC_INDEX_EP0
, S3C2410_UDC_INDEX_REG
);
894 ep0csr
= udc_read(S3C2410_UDC_IN_CSR1_REG
);
896 dprintk(DEBUG_NORMAL
, "usbs=%02x, usbds=%02x, pwr=%02x ep0csr=%02x\n",
897 usb_status
, usbd_status
, pwr_reg
, ep0csr
);
900 * Now, handle interrupts. There's two types :
901 * - Reset, Resume, Suspend coming -> usb_int_reg
906 if (usb_status
& S3C2410_UDC_USBINT_RESET
) {
907 /* two kind of reset :
908 * - reset start -> pwr reg = 8
909 * - reset end -> pwr reg = 0
911 dprintk(DEBUG_NORMAL
, "USB reset csr %x pwr %x\n",
914 dev
->gadget
.speed
= USB_SPEED_UNKNOWN
;
915 udc_write(0x00, S3C2410_UDC_INDEX_REG
);
916 udc_write((dev
->ep
[0].ep
.maxpacket
& 0x7ff) >> 3,
917 S3C2410_UDC_MAXP_REG
);
920 dev
->ep0state
= EP0_IDLE
;
921 dev
->gadget
.speed
= USB_SPEED_FULL
;
923 /* clear interrupt */
924 udc_write(S3C2410_UDC_USBINT_RESET
,
925 S3C2410_UDC_USB_INT_REG
);
927 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
928 spin_unlock_irqrestore(&dev
->lock
, flags
);
933 if (usb_status
& S3C2410_UDC_USBINT_RESUME
) {
934 dprintk(DEBUG_NORMAL
, "USB resume\n");
936 /* clear interrupt */
937 udc_write(S3C2410_UDC_USBINT_RESUME
,
938 S3C2410_UDC_USB_INT_REG
);
940 if (dev
->gadget
.speed
!= USB_SPEED_UNKNOWN
942 && dev
->driver
->resume
)
943 dev
->driver
->resume(&dev
->gadget
);
947 if (usb_status
& S3C2410_UDC_USBINT_SUSPEND
) {
948 dprintk(DEBUG_NORMAL
, "USB suspend\n");
950 /* clear interrupt */
951 udc_write(S3C2410_UDC_USBINT_SUSPEND
,
952 S3C2410_UDC_USB_INT_REG
);
954 if (dev
->gadget
.speed
!= USB_SPEED_UNKNOWN
956 && dev
->driver
->suspend
)
957 dev
->driver
->suspend(&dev
->gadget
);
959 dev
->ep0state
= EP0_IDLE
;
963 /* control traffic */
964 /* check on ep0csr != 0 is not a good idea as clearing in_pkt_ready
965 * generate an interrupt
967 if (usbd_status
& S3C2410_UDC_INT_EP0
) {
968 dprintk(DEBUG_VERBOSE
, "USB ep0 irq\n");
969 /* Clear the interrupt bit by setting it to 1 */
970 udc_write(S3C2410_UDC_INT_EP0
, S3C2410_UDC_EP_INT_REG
);
971 s3c2410_udc_handle_ep0(dev
);
974 /* endpoint data transfers */
975 for (i
= 1; i
< S3C2410_ENDPOINTS
; i
++) {
977 if (usbd_status
& tmp
) {
978 dprintk(DEBUG_VERBOSE
, "USB ep%d irq\n", i
);
980 /* Clear the interrupt bit by setting it to 1 */
981 udc_write(tmp
, S3C2410_UDC_EP_INT_REG
);
982 s3c2410_udc_handle_ep(&dev
->ep
[i
]);
986 /* what else causes this interrupt? a receive! who is it? */
987 if (!usb_status
&& !usbd_status
&& !pwr_reg
&& !ep0csr
) {
988 for (i
= 1; i
< S3C2410_ENDPOINTS
; i
++) {
989 idx2
= udc_read(S3C2410_UDC_INDEX_REG
);
990 udc_write(i
, S3C2410_UDC_INDEX_REG
);
992 if (udc_read(S3C2410_UDC_OUT_CSR1_REG
) & 0x1)
993 s3c2410_udc_handle_ep(&dev
->ep
[i
]);
996 udc_write(idx2
, S3C2410_UDC_INDEX_REG
);
1000 dprintk(DEBUG_VERBOSE
, "irq: %d s3c2410_udc_done.\n", IRQ_USBD
);
1002 /* Restore old index */
1003 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
1005 spin_unlock_irqrestore(&dev
->lock
, flags
);
1009 /*------------------------- s3c2410_ep_ops ----------------------------------*/
1011 static inline struct s3c2410_ep
*to_s3c2410_ep(struct usb_ep
*ep
)
1013 return container_of(ep
, struct s3c2410_ep
, ep
);
1016 static inline struct s3c2410_udc
*to_s3c2410_udc(struct usb_gadget
*gadget
)
1018 return container_of(gadget
, struct s3c2410_udc
, gadget
);
1021 static inline struct s3c2410_request
*to_s3c2410_req(struct usb_request
*req
)
1023 return container_of(req
, struct s3c2410_request
, req
);
1027 * s3c2410_udc_ep_enable
1029 static int s3c2410_udc_ep_enable(struct usb_ep
*_ep
,
1030 const struct usb_endpoint_descriptor
*desc
)
1032 struct s3c2410_udc
*dev
;
1033 struct s3c2410_ep
*ep
;
1035 unsigned long flags
;
1039 ep
= to_s3c2410_ep(_ep
);
1042 || _ep
->name
== ep0name
1043 || desc
->bDescriptorType
!= USB_DT_ENDPOINT
)
1047 if (!dev
->driver
|| dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
1050 max
= usb_endpoint_maxp(desc
) & 0x1fff;
1052 local_irq_save(flags
);
1053 _ep
->maxpacket
= max
& 0x7ff;
1056 ep
->bEndpointAddress
= desc
->bEndpointAddress
;
1058 /* set max packet */
1059 udc_write(ep
->num
, S3C2410_UDC_INDEX_REG
);
1060 udc_write(max
>> 3, S3C2410_UDC_MAXP_REG
);
1062 /* set type, direction, address; reset fifo counters */
1063 if (desc
->bEndpointAddress
& USB_DIR_IN
) {
1064 csr1
= S3C2410_UDC_ICSR1_FFLUSH
|S3C2410_UDC_ICSR1_CLRDT
;
1065 csr2
= S3C2410_UDC_ICSR2_MODEIN
|S3C2410_UDC_ICSR2_DMAIEN
;
1067 udc_write(ep
->num
, S3C2410_UDC_INDEX_REG
);
1068 udc_write(csr1
, S3C2410_UDC_IN_CSR1_REG
);
1069 udc_write(ep
->num
, S3C2410_UDC_INDEX_REG
);
1070 udc_write(csr2
, S3C2410_UDC_IN_CSR2_REG
);
1072 /* don't flush in fifo or it will cause endpoint interrupt */
1073 csr1
= S3C2410_UDC_ICSR1_CLRDT
;
1074 csr2
= S3C2410_UDC_ICSR2_DMAIEN
;
1076 udc_write(ep
->num
, S3C2410_UDC_INDEX_REG
);
1077 udc_write(csr1
, S3C2410_UDC_IN_CSR1_REG
);
1078 udc_write(ep
->num
, S3C2410_UDC_INDEX_REG
);
1079 udc_write(csr2
, S3C2410_UDC_IN_CSR2_REG
);
1081 csr1
= S3C2410_UDC_OCSR1_FFLUSH
| S3C2410_UDC_OCSR1_CLRDT
;
1082 csr2
= S3C2410_UDC_OCSR2_DMAIEN
;
1084 udc_write(ep
->num
, S3C2410_UDC_INDEX_REG
);
1085 udc_write(csr1
, S3C2410_UDC_OUT_CSR1_REG
);
1086 udc_write(ep
->num
, S3C2410_UDC_INDEX_REG
);
1087 udc_write(csr2
, S3C2410_UDC_OUT_CSR2_REG
);
1091 int_en_reg
= udc_read(S3C2410_UDC_EP_INT_EN_REG
);
1092 udc_write(int_en_reg
| (1 << ep
->num
), S3C2410_UDC_EP_INT_EN_REG
);
1094 /* print some debug message */
1095 tmp
= desc
->bEndpointAddress
;
1096 dprintk(DEBUG_NORMAL
, "enable %s(%d) ep%x%s-blk max %02x\n",
1097 _ep
->name
, ep
->num
, tmp
,
1098 desc
->bEndpointAddress
& USB_DIR_IN
? "in" : "out", max
);
1100 local_irq_restore(flags
);
1101 s3c2410_udc_set_halt(_ep
, 0);
1107 * s3c2410_udc_ep_disable
1109 static int s3c2410_udc_ep_disable(struct usb_ep
*_ep
)
1111 struct s3c2410_ep
*ep
= to_s3c2410_ep(_ep
);
1112 unsigned long flags
;
1115 if (!_ep
|| !ep
->ep
.desc
) {
1116 dprintk(DEBUG_NORMAL
, "%s not enabled\n",
1117 _ep
? ep
->ep
.name
: NULL
);
1121 local_irq_save(flags
);
1123 dprintk(DEBUG_NORMAL
, "ep_disable: %s\n", _ep
->name
);
1128 s3c2410_udc_nuke(ep
->dev
, ep
, -ESHUTDOWN
);
1131 int_en_reg
= udc_read(S3C2410_UDC_EP_INT_EN_REG
);
1132 udc_write(int_en_reg
& ~(1<<ep
->num
), S3C2410_UDC_EP_INT_EN_REG
);
1134 local_irq_restore(flags
);
1136 dprintk(DEBUG_NORMAL
, "%s disabled\n", _ep
->name
);
1142 * s3c2410_udc_alloc_request
1144 static struct usb_request
*
1145 s3c2410_udc_alloc_request(struct usb_ep
*_ep
, gfp_t mem_flags
)
1147 struct s3c2410_request
*req
;
1149 dprintk(DEBUG_VERBOSE
, "%s(%p,%d)\n", __func__
, _ep
, mem_flags
);
1154 req
= kzalloc(sizeof(struct s3c2410_request
), mem_flags
);
1158 INIT_LIST_HEAD(&req
->queue
);
1163 * s3c2410_udc_free_request
1166 s3c2410_udc_free_request(struct usb_ep
*_ep
, struct usb_request
*_req
)
1168 struct s3c2410_ep
*ep
= to_s3c2410_ep(_ep
);
1169 struct s3c2410_request
*req
= to_s3c2410_req(_req
);
1171 dprintk(DEBUG_VERBOSE
, "%s(%p,%p)\n", __func__
, _ep
, _req
);
1173 if (!ep
|| !_req
|| (!ep
->ep
.desc
&& _ep
->name
!= ep0name
))
1176 WARN_ON(!list_empty(&req
->queue
));
1183 static int s3c2410_udc_queue(struct usb_ep
*_ep
, struct usb_request
*_req
,
1186 struct s3c2410_request
*req
= to_s3c2410_req(_req
);
1187 struct s3c2410_ep
*ep
= to_s3c2410_ep(_ep
);
1188 struct s3c2410_udc
*dev
;
1191 unsigned long flags
;
1193 if (unlikely(!_ep
|| (!ep
->ep
.desc
&& ep
->ep
.name
!= ep0name
))) {
1194 dprintk(DEBUG_NORMAL
, "%s: invalid args\n", __func__
);
1199 if (unlikely(!dev
->driver
1200 || dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)) {
1204 local_irq_save(flags
);
1206 if (unlikely(!_req
|| !_req
->complete
1207 || !_req
->buf
|| !list_empty(&req
->queue
))) {
1209 dprintk(DEBUG_NORMAL
, "%s: 1 X X X\n", __func__
);
1211 dprintk(DEBUG_NORMAL
, "%s: 0 %01d %01d %01d\n",
1212 __func__
, !_req
->complete
, !_req
->buf
,
1213 !list_empty(&req
->queue
));
1216 local_irq_restore(flags
);
1220 _req
->status
= -EINPROGRESS
;
1223 dprintk(DEBUG_VERBOSE
, "%s: ep%x len %d\n",
1224 __func__
, ep
->bEndpointAddress
, _req
->length
);
1226 if (ep
->bEndpointAddress
) {
1227 udc_write(ep
->bEndpointAddress
& 0x7F, S3C2410_UDC_INDEX_REG
);
1229 ep_csr
= udc_read((ep
->bEndpointAddress
& USB_DIR_IN
)
1230 ? S3C2410_UDC_IN_CSR1_REG
1231 : S3C2410_UDC_OUT_CSR1_REG
);
1232 fifo_count
= s3c2410_udc_fifo_count_out();
1234 udc_write(0, S3C2410_UDC_INDEX_REG
);
1235 ep_csr
= udc_read(S3C2410_UDC_IN_CSR1_REG
);
1236 fifo_count
= s3c2410_udc_fifo_count_out();
1239 /* kickstart this i/o queue? */
1240 if (list_empty(&ep
->queue
) && !ep
->halted
) {
1241 if (ep
->bEndpointAddress
== 0 /* ep0 */) {
1242 switch (dev
->ep0state
) {
1243 case EP0_IN_DATA_PHASE
:
1244 if (!(ep_csr
&S3C2410_UDC_EP0_CSR_IPKRDY
)
1245 && s3c2410_udc_write_fifo(ep
,
1247 dev
->ep0state
= EP0_IDLE
;
1252 case EP0_OUT_DATA_PHASE
:
1254 || ((ep_csr
& S3C2410_UDC_OCSR1_PKTRDY
)
1255 && s3c2410_udc_read_fifo(ep
,
1257 dev
->ep0state
= EP0_IDLE
;
1263 local_irq_restore(flags
);
1266 } else if ((ep
->bEndpointAddress
& USB_DIR_IN
) != 0
1267 && (!(ep_csr
&S3C2410_UDC_OCSR1_PKTRDY
))
1268 && s3c2410_udc_write_fifo(ep
, req
)) {
1270 } else if ((ep_csr
& S3C2410_UDC_OCSR1_PKTRDY
)
1272 && s3c2410_udc_read_fifo(ep
, req
)) {
1277 /* pio or dma irq handler advances the queue. */
1279 list_add_tail(&req
->queue
, &ep
->queue
);
1281 local_irq_restore(flags
);
1283 dprintk(DEBUG_VERBOSE
, "%s ok\n", __func__
);
1288 * s3c2410_udc_dequeue
1290 static int s3c2410_udc_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
1292 struct s3c2410_ep
*ep
= to_s3c2410_ep(_ep
);
1293 struct s3c2410_udc
*udc
;
1294 int retval
= -EINVAL
;
1295 unsigned long flags
;
1296 struct s3c2410_request
*req
= NULL
;
1298 dprintk(DEBUG_VERBOSE
, "%s(%p,%p)\n", __func__
, _ep
, _req
);
1300 if (!the_controller
->driver
)
1306 udc
= to_s3c2410_udc(ep
->gadget
);
1308 local_irq_save(flags
);
1310 list_for_each_entry(req
, &ep
->queue
, queue
) {
1311 if (&req
->req
== _req
) {
1312 list_del_init(&req
->queue
);
1313 _req
->status
= -ECONNRESET
;
1320 dprintk(DEBUG_VERBOSE
,
1321 "dequeued req %p from %s, len %d buf %p\n",
1322 req
, _ep
->name
, _req
->length
, _req
->buf
);
1324 s3c2410_udc_done(ep
, req
, -ECONNRESET
);
1327 local_irq_restore(flags
);
1332 * s3c2410_udc_set_halt
1334 static int s3c2410_udc_set_halt(struct usb_ep
*_ep
, int value
)
1336 struct s3c2410_ep
*ep
= to_s3c2410_ep(_ep
);
1338 unsigned long flags
;
1341 if (unlikely(!_ep
|| (!ep
->ep
.desc
&& ep
->ep
.name
!= ep0name
))) {
1342 dprintk(DEBUG_NORMAL
, "%s: inval 2\n", __func__
);
1346 local_irq_save(flags
);
1348 idx
= ep
->bEndpointAddress
& 0x7F;
1351 s3c2410_udc_set_ep0_ss(base_addr
);
1352 s3c2410_udc_set_ep0_de_out(base_addr
);
1354 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
1355 ep_csr
= udc_read((ep
->bEndpointAddress
& USB_DIR_IN
)
1356 ? S3C2410_UDC_IN_CSR1_REG
1357 : S3C2410_UDC_OUT_CSR1_REG
);
1359 if ((ep
->bEndpointAddress
& USB_DIR_IN
) != 0) {
1361 udc_write(ep_csr
| S3C2410_UDC_ICSR1_SENDSTL
,
1362 S3C2410_UDC_IN_CSR1_REG
);
1364 ep_csr
&= ~S3C2410_UDC_ICSR1_SENDSTL
;
1365 udc_write(ep_csr
, S3C2410_UDC_IN_CSR1_REG
);
1366 ep_csr
|= S3C2410_UDC_ICSR1_CLRDT
;
1367 udc_write(ep_csr
, S3C2410_UDC_IN_CSR1_REG
);
1371 udc_write(ep_csr
| S3C2410_UDC_OCSR1_SENDSTL
,
1372 S3C2410_UDC_OUT_CSR1_REG
);
1374 ep_csr
&= ~S3C2410_UDC_OCSR1_SENDSTL
;
1375 udc_write(ep_csr
, S3C2410_UDC_OUT_CSR1_REG
);
1376 ep_csr
|= S3C2410_UDC_OCSR1_CLRDT
;
1377 udc_write(ep_csr
, S3C2410_UDC_OUT_CSR1_REG
);
1382 ep
->halted
= value
? 1 : 0;
1383 local_irq_restore(flags
);
1388 static const struct usb_ep_ops s3c2410_ep_ops
= {
1389 .enable
= s3c2410_udc_ep_enable
,
1390 .disable
= s3c2410_udc_ep_disable
,
1392 .alloc_request
= s3c2410_udc_alloc_request
,
1393 .free_request
= s3c2410_udc_free_request
,
1395 .queue
= s3c2410_udc_queue
,
1396 .dequeue
= s3c2410_udc_dequeue
,
1398 .set_halt
= s3c2410_udc_set_halt
,
1401 /*------------------------- usb_gadget_ops ----------------------------------*/
1404 * s3c2410_udc_get_frame
1406 static int s3c2410_udc_get_frame(struct usb_gadget
*_gadget
)
1410 dprintk(DEBUG_VERBOSE
, "%s()\n", __func__
);
1412 tmp
= udc_read(S3C2410_UDC_FRAME_NUM2_REG
) << 8;
1413 tmp
|= udc_read(S3C2410_UDC_FRAME_NUM1_REG
);
1418 * s3c2410_udc_wakeup
1420 static int s3c2410_udc_wakeup(struct usb_gadget
*_gadget
)
1422 dprintk(DEBUG_NORMAL
, "%s()\n", __func__
);
1427 * s3c2410_udc_set_selfpowered
1429 static int s3c2410_udc_set_selfpowered(struct usb_gadget
*gadget
, int value
)
1431 struct s3c2410_udc
*udc
= to_s3c2410_udc(gadget
);
1433 dprintk(DEBUG_NORMAL
, "%s()\n", __func__
);
1435 gadget
->is_selfpowered
= (value
!= 0);
1437 udc
->devstatus
|= (1 << USB_DEVICE_SELF_POWERED
);
1439 udc
->devstatus
&= ~(1 << USB_DEVICE_SELF_POWERED
);
1444 static void s3c2410_udc_disable(struct s3c2410_udc
*dev
);
1445 static void s3c2410_udc_enable(struct s3c2410_udc
*dev
);
1447 static int s3c2410_udc_set_pullup(struct s3c2410_udc
*udc
, int is_on
)
1449 dprintk(DEBUG_NORMAL
, "%s()\n", __func__
);
1451 if (udc_info
&& (udc_info
->udc_command
||
1452 gpio_is_valid(udc_info
->pullup_pin
))) {
1455 s3c2410_udc_enable(udc
);
1457 if (udc
->gadget
.speed
!= USB_SPEED_UNKNOWN
) {
1458 if (udc
->driver
&& udc
->driver
->disconnect
)
1459 udc
->driver
->disconnect(&udc
->gadget
);
1462 s3c2410_udc_disable(udc
);
1471 static int s3c2410_udc_vbus_session(struct usb_gadget
*gadget
, int is_active
)
1473 struct s3c2410_udc
*udc
= to_s3c2410_udc(gadget
);
1475 dprintk(DEBUG_NORMAL
, "%s()\n", __func__
);
1477 udc
->vbus
= (is_active
!= 0);
1478 s3c2410_udc_set_pullup(udc
, is_active
);
1482 static int s3c2410_udc_pullup(struct usb_gadget
*gadget
, int is_on
)
1484 struct s3c2410_udc
*udc
= to_s3c2410_udc(gadget
);
1486 dprintk(DEBUG_NORMAL
, "%s()\n", __func__
);
1488 s3c2410_udc_set_pullup(udc
, is_on
);
1492 static irqreturn_t
s3c2410_udc_vbus_irq(int irq
, void *_dev
)
1494 struct s3c2410_udc
*dev
= _dev
;
1497 dprintk(DEBUG_NORMAL
, "%s()\n", __func__
);
1499 value
= gpio_get_value(udc_info
->vbus_pin
) ? 1 : 0;
1500 if (udc_info
->vbus_pin_inverted
)
1503 if (value
!= dev
->vbus
)
1504 s3c2410_udc_vbus_session(&dev
->gadget
, value
);
1509 static int s3c2410_vbus_draw(struct usb_gadget
*_gadget
, unsigned ma
)
1511 dprintk(DEBUG_NORMAL
, "%s()\n", __func__
);
1513 if (udc_info
&& udc_info
->vbus_draw
) {
1514 udc_info
->vbus_draw(ma
);
1521 static int s3c2410_udc_start(struct usb_gadget
*g
,
1522 struct usb_gadget_driver
*driver
);
1523 static int s3c2410_udc_stop(struct usb_gadget
*g
);
1525 static const struct usb_gadget_ops s3c2410_ops
= {
1526 .get_frame
= s3c2410_udc_get_frame
,
1527 .wakeup
= s3c2410_udc_wakeup
,
1528 .set_selfpowered
= s3c2410_udc_set_selfpowered
,
1529 .pullup
= s3c2410_udc_pullup
,
1530 .vbus_session
= s3c2410_udc_vbus_session
,
1531 .vbus_draw
= s3c2410_vbus_draw
,
1532 .udc_start
= s3c2410_udc_start
,
1533 .udc_stop
= s3c2410_udc_stop
,
1536 static void s3c2410_udc_command(enum s3c2410_udc_cmd_e cmd
)
1541 if (udc_info
->udc_command
) {
1542 udc_info
->udc_command(cmd
);
1543 } else if (gpio_is_valid(udc_info
->pullup_pin
)) {
1547 case S3C2410_UDC_P_ENABLE
:
1550 case S3C2410_UDC_P_DISABLE
:
1556 value
^= udc_info
->pullup_pin_inverted
;
1558 gpio_set_value(udc_info
->pullup_pin
, value
);
1562 /*------------------------- gadget driver handling---------------------------*/
1564 * s3c2410_udc_disable
1566 static void s3c2410_udc_disable(struct s3c2410_udc
*dev
)
1568 dprintk(DEBUG_NORMAL
, "%s()\n", __func__
);
1570 /* Disable all interrupts */
1571 udc_write(0x00, S3C2410_UDC_USB_INT_EN_REG
);
1572 udc_write(0x00, S3C2410_UDC_EP_INT_EN_REG
);
1574 /* Clear the interrupt registers */
1575 udc_write(S3C2410_UDC_USBINT_RESET
1576 | S3C2410_UDC_USBINT_RESUME
1577 | S3C2410_UDC_USBINT_SUSPEND
,
1578 S3C2410_UDC_USB_INT_REG
);
1580 udc_write(0x1F, S3C2410_UDC_EP_INT_REG
);
1582 /* Good bye, cruel world */
1583 s3c2410_udc_command(S3C2410_UDC_P_DISABLE
);
1585 /* Set speed to unknown */
1586 dev
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1590 * s3c2410_udc_reinit
1592 static void s3c2410_udc_reinit(struct s3c2410_udc
*dev
)
1596 /* device/ep0 records init */
1597 INIT_LIST_HEAD(&dev
->gadget
.ep_list
);
1598 INIT_LIST_HEAD(&dev
->gadget
.ep0
->ep_list
);
1599 dev
->ep0state
= EP0_IDLE
;
1601 for (i
= 0; i
< S3C2410_ENDPOINTS
; i
++) {
1602 struct s3c2410_ep
*ep
= &dev
->ep
[i
];
1605 list_add_tail(&ep
->ep
.ep_list
, &dev
->gadget
.ep_list
);
1610 INIT_LIST_HEAD(&ep
->queue
);
1611 usb_ep_set_maxpacket_limit(&ep
->ep
, ep
->ep
.maxpacket
);
1616 * s3c2410_udc_enable
1618 static void s3c2410_udc_enable(struct s3c2410_udc
*dev
)
1622 dprintk(DEBUG_NORMAL
, "s3c2410_udc_enable called\n");
1624 /* dev->gadget.speed = USB_SPEED_UNKNOWN; */
1625 dev
->gadget
.speed
= USB_SPEED_FULL
;
1627 /* Set MAXP for all endpoints */
1628 for (i
= 0; i
< S3C2410_ENDPOINTS
; i
++) {
1629 udc_write(i
, S3C2410_UDC_INDEX_REG
);
1630 udc_write((dev
->ep
[i
].ep
.maxpacket
& 0x7ff) >> 3,
1631 S3C2410_UDC_MAXP_REG
);
1634 /* Set default power state */
1635 udc_write(DEFAULT_POWER_STATE
, S3C2410_UDC_PWR_REG
);
1637 /* Enable reset and suspend interrupt interrupts */
1638 udc_write(S3C2410_UDC_USBINT_RESET
| S3C2410_UDC_USBINT_SUSPEND
,
1639 S3C2410_UDC_USB_INT_EN_REG
);
1641 /* Enable ep0 interrupt */
1642 udc_write(S3C2410_UDC_INT_EP0
, S3C2410_UDC_EP_INT_EN_REG
);
1644 /* time to say "hello, world" */
1645 s3c2410_udc_command(S3C2410_UDC_P_ENABLE
);
1648 static int s3c2410_udc_start(struct usb_gadget
*g
,
1649 struct usb_gadget_driver
*driver
)
1651 struct s3c2410_udc
*udc
= to_s3c2410(g
);
1653 dprintk(DEBUG_NORMAL
, "%s() '%s'\n", __func__
, driver
->driver
.name
);
1655 /* Hook the driver */
1656 udc
->driver
= driver
;
1659 s3c2410_udc_enable(udc
);
1664 static int s3c2410_udc_stop(struct usb_gadget
*g
)
1666 struct s3c2410_udc
*udc
= to_s3c2410(g
);
1671 s3c2410_udc_disable(udc
);
1676 /*---------------------------------------------------------------------------*/
1677 static struct s3c2410_udc memory
= {
1679 .ops
= &s3c2410_ops
,
1680 .ep0
= &memory
.ep
[0].ep
,
1681 .name
= gadget_name
,
1683 .init_name
= "gadget",
1687 /* control endpoint */
1692 .ops
= &s3c2410_ep_ops
,
1693 .maxpacket
= EP0_FIFO_SIZE
,
1694 .caps
= USB_EP_CAPS(USB_EP_CAPS_TYPE_CONTROL
,
1695 USB_EP_CAPS_DIR_ALL
),
1700 /* first group of endpoints */
1705 .ops
= &s3c2410_ep_ops
,
1706 .maxpacket
= EP_FIFO_SIZE
,
1707 .caps
= USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK
,
1708 USB_EP_CAPS_DIR_ALL
),
1711 .fifo_size
= EP_FIFO_SIZE
,
1712 .bEndpointAddress
= 1,
1713 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1719 .ops
= &s3c2410_ep_ops
,
1720 .maxpacket
= EP_FIFO_SIZE
,
1721 .caps
= USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK
,
1722 USB_EP_CAPS_DIR_ALL
),
1725 .fifo_size
= EP_FIFO_SIZE
,
1726 .bEndpointAddress
= 2,
1727 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1733 .ops
= &s3c2410_ep_ops
,
1734 .maxpacket
= EP_FIFO_SIZE
,
1735 .caps
= USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK
,
1736 USB_EP_CAPS_DIR_ALL
),
1739 .fifo_size
= EP_FIFO_SIZE
,
1740 .bEndpointAddress
= 3,
1741 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1747 .ops
= &s3c2410_ep_ops
,
1748 .maxpacket
= EP_FIFO_SIZE
,
1749 .caps
= USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK
,
1750 USB_EP_CAPS_DIR_ALL
),
1753 .fifo_size
= EP_FIFO_SIZE
,
1754 .bEndpointAddress
= 4,
1755 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1761 * probe - binds to the platform device
1763 static int s3c2410_udc_probe(struct platform_device
*pdev
)
1765 struct s3c2410_udc
*udc
= &memory
;
1766 struct device
*dev
= &pdev
->dev
;
1770 dev_dbg(dev
, "%s()\n", __func__
);
1772 usb_bus_clock
= clk_get(NULL
, "usb-bus-gadget");
1773 if (IS_ERR(usb_bus_clock
)) {
1774 dev_err(dev
, "failed to get usb bus clock source\n");
1775 return PTR_ERR(usb_bus_clock
);
1778 clk_prepare_enable(usb_bus_clock
);
1780 udc_clock
= clk_get(NULL
, "usb-device");
1781 if (IS_ERR(udc_clock
)) {
1782 dev_err(dev
, "failed to get udc clock source\n");
1783 return PTR_ERR(udc_clock
);
1786 clk_prepare_enable(udc_clock
);
1790 dev_dbg(dev
, "got and enabled clocks\n");
1792 if (strncmp(pdev
->name
, "s3c2440", 7) == 0) {
1793 dev_info(dev
, "S3C2440: increasing FIFO to 128 bytes\n");
1794 memory
.ep
[1].fifo_size
= S3C2440_EP_FIFO_SIZE
;
1795 memory
.ep
[2].fifo_size
= S3C2440_EP_FIFO_SIZE
;
1796 memory
.ep
[3].fifo_size
= S3C2440_EP_FIFO_SIZE
;
1797 memory
.ep
[4].fifo_size
= S3C2440_EP_FIFO_SIZE
;
1800 spin_lock_init(&udc
->lock
);
1801 udc_info
= dev_get_platdata(&pdev
->dev
);
1803 rsrc_start
= S3C2410_PA_USBDEV
;
1804 rsrc_len
= S3C24XX_SZ_USBDEV
;
1806 if (!request_mem_region(rsrc_start
, rsrc_len
, gadget_name
))
1809 base_addr
= ioremap(rsrc_start
, rsrc_len
);
1815 the_controller
= udc
;
1816 platform_set_drvdata(pdev
, udc
);
1818 s3c2410_udc_disable(udc
);
1819 s3c2410_udc_reinit(udc
);
1821 /* irq setup after old hardware state is cleaned up */
1822 retval
= request_irq(IRQ_USBD
, s3c2410_udc_irq
,
1823 0, gadget_name
, udc
);
1826 dev_err(dev
, "cannot get irq %i, err %d\n", IRQ_USBD
, retval
);
1831 dev_dbg(dev
, "got irq %i\n", IRQ_USBD
);
1833 if (udc_info
&& udc_info
->vbus_pin
> 0) {
1834 retval
= gpio_request(udc_info
->vbus_pin
, "udc vbus");
1836 dev_err(dev
, "cannot claim vbus pin\n");
1840 irq
= gpio_to_irq(udc_info
->vbus_pin
);
1842 dev_err(dev
, "no irq for gpio vbus pin\n");
1844 goto err_gpio_claim
;
1847 retval
= request_irq(irq
, s3c2410_udc_vbus_irq
,
1849 | IRQF_TRIGGER_FALLING
| IRQF_SHARED
,
1853 dev_err(dev
, "can't get vbus irq %d, err %d\n",
1856 goto err_gpio_claim
;
1859 dev_dbg(dev
, "got irq %i\n", irq
);
1864 if (udc_info
&& !udc_info
->udc_command
&&
1865 gpio_is_valid(udc_info
->pullup_pin
)) {
1867 retval
= gpio_request_one(udc_info
->pullup_pin
,
1868 udc_info
->vbus_pin_inverted
?
1869 GPIOF_OUT_INIT_HIGH
: GPIOF_OUT_INIT_LOW
,
1875 retval
= usb_add_gadget_udc(&pdev
->dev
, &udc
->gadget
);
1879 if (s3c2410_udc_debugfs_root
) {
1880 udc
->regs_info
= debugfs_create_file("registers", S_IRUGO
,
1881 s3c2410_udc_debugfs_root
,
1882 udc
, &s3c2410_udc_debugfs_fops
);
1883 if (!udc
->regs_info
)
1884 dev_warn(dev
, "debugfs file creation failed\n");
1887 dev_dbg(dev
, "probe ok\n");
1892 if (udc_info
&& !udc_info
->udc_command
&&
1893 gpio_is_valid(udc_info
->pullup_pin
))
1894 gpio_free(udc_info
->pullup_pin
);
1896 if (udc_info
&& udc_info
->vbus_pin
> 0)
1897 free_irq(gpio_to_irq(udc_info
->vbus_pin
), udc
);
1899 if (udc_info
&& udc_info
->vbus_pin
> 0)
1900 gpio_free(udc_info
->vbus_pin
);
1902 free_irq(IRQ_USBD
, udc
);
1906 release_mem_region(rsrc_start
, rsrc_len
);
1912 * s3c2410_udc_remove
1914 static int s3c2410_udc_remove(struct platform_device
*pdev
)
1916 struct s3c2410_udc
*udc
= platform_get_drvdata(pdev
);
1919 dev_dbg(&pdev
->dev
, "%s()\n", __func__
);
1924 usb_del_gadget_udc(&udc
->gadget
);
1925 debugfs_remove(udc
->regs_info
);
1927 if (udc_info
&& !udc_info
->udc_command
&&
1928 gpio_is_valid(udc_info
->pullup_pin
))
1929 gpio_free(udc_info
->pullup_pin
);
1931 if (udc_info
&& udc_info
->vbus_pin
> 0) {
1932 irq
= gpio_to_irq(udc_info
->vbus_pin
);
1936 free_irq(IRQ_USBD
, udc
);
1939 release_mem_region(rsrc_start
, rsrc_len
);
1941 if (!IS_ERR(udc_clock
) && udc_clock
!= NULL
) {
1942 clk_disable_unprepare(udc_clock
);
1947 if (!IS_ERR(usb_bus_clock
) && usb_bus_clock
!= NULL
) {
1948 clk_disable_unprepare(usb_bus_clock
);
1949 clk_put(usb_bus_clock
);
1950 usb_bus_clock
= NULL
;
1953 dev_dbg(&pdev
->dev
, "%s: remove ok\n", __func__
);
1959 s3c2410_udc_suspend(struct platform_device
*pdev
, pm_message_t message
)
1961 s3c2410_udc_command(S3C2410_UDC_P_DISABLE
);
1966 static int s3c2410_udc_resume(struct platform_device
*pdev
)
1968 s3c2410_udc_command(S3C2410_UDC_P_ENABLE
);
1973 #define s3c2410_udc_suspend NULL
1974 #define s3c2410_udc_resume NULL
1977 static const struct platform_device_id s3c_udc_ids
[] = {
1978 { "s3c2410-usbgadget", },
1979 { "s3c2440-usbgadget", },
1982 MODULE_DEVICE_TABLE(platform
, s3c_udc_ids
);
1984 static struct platform_driver udc_driver_24x0
= {
1986 .name
= "s3c24x0-usbgadget",
1988 .probe
= s3c2410_udc_probe
,
1989 .remove
= s3c2410_udc_remove
,
1990 .suspend
= s3c2410_udc_suspend
,
1991 .resume
= s3c2410_udc_resume
,
1992 .id_table
= s3c_udc_ids
,
1995 static int __init
udc_init(void)
1999 dprintk(DEBUG_NORMAL
, "%s: version %s\n", gadget_name
, DRIVER_VERSION
);
2001 s3c2410_udc_debugfs_root
= debugfs_create_dir(gadget_name
, NULL
);
2002 if (IS_ERR(s3c2410_udc_debugfs_root
)) {
2003 pr_err("%s: debugfs dir creation failed %ld\n",
2004 gadget_name
, PTR_ERR(s3c2410_udc_debugfs_root
));
2005 s3c2410_udc_debugfs_root
= NULL
;
2008 retval
= platform_driver_register(&udc_driver_24x0
);
2015 debugfs_remove(s3c2410_udc_debugfs_root
);
2019 static void __exit
udc_exit(void)
2021 platform_driver_unregister(&udc_driver_24x0
);
2022 debugfs_remove(s3c2410_udc_debugfs_root
);
2025 module_init(udc_init
);
2026 module_exit(udc_exit
);
2028 MODULE_AUTHOR(DRIVER_AUTHOR
);
2029 MODULE_DESCRIPTION(DRIVER_DESC
);
2030 MODULE_VERSION(DRIVER_VERSION
);
2031 MODULE_LICENSE("GPL");