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_seq_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
172 static int s3c2410_udc_debugfs_fops_open(struct inode
*inode
,
175 return single_open(file
, s3c2410_udc_debugfs_seq_show
, NULL
);
178 static const struct file_operations s3c2410_udc_debugfs_fops
= {
179 .open
= s3c2410_udc_debugfs_fops_open
,
182 .release
= single_release
,
183 .owner
= THIS_MODULE
,
188 static inline void s3c2410_udc_clear_ep0_opr(void __iomem
*base
)
190 udc_writeb(base
, S3C2410_UDC_INDEX_EP0
, S3C2410_UDC_INDEX_REG
);
191 udc_writeb(base
, S3C2410_UDC_EP0_CSR_SOPKTRDY
,
192 S3C2410_UDC_EP0_CSR_REG
);
195 static inline void s3c2410_udc_clear_ep0_sst(void __iomem
*base
)
197 udc_writeb(base
, S3C2410_UDC_INDEX_EP0
, S3C2410_UDC_INDEX_REG
);
198 writeb(0x00, base
+ S3C2410_UDC_EP0_CSR_REG
);
201 static inline void s3c2410_udc_clear_ep0_se(void __iomem
*base
)
203 udc_writeb(base
, S3C2410_UDC_INDEX_EP0
, S3C2410_UDC_INDEX_REG
);
204 udc_writeb(base
, S3C2410_UDC_EP0_CSR_SSE
, S3C2410_UDC_EP0_CSR_REG
);
207 static inline void s3c2410_udc_set_ep0_ipr(void __iomem
*base
)
209 udc_writeb(base
, S3C2410_UDC_INDEX_EP0
, S3C2410_UDC_INDEX_REG
);
210 udc_writeb(base
, S3C2410_UDC_EP0_CSR_IPKRDY
, S3C2410_UDC_EP0_CSR_REG
);
213 static inline void s3c2410_udc_set_ep0_de(void __iomem
*base
)
215 udc_writeb(base
, S3C2410_UDC_INDEX_EP0
, S3C2410_UDC_INDEX_REG
);
216 udc_writeb(base
, S3C2410_UDC_EP0_CSR_DE
, S3C2410_UDC_EP0_CSR_REG
);
219 inline void s3c2410_udc_set_ep0_ss(void __iomem
*b
)
221 udc_writeb(b
, S3C2410_UDC_INDEX_EP0
, S3C2410_UDC_INDEX_REG
);
222 udc_writeb(b
, S3C2410_UDC_EP0_CSR_SENDSTL
, S3C2410_UDC_EP0_CSR_REG
);
225 static inline void s3c2410_udc_set_ep0_de_out(void __iomem
*base
)
227 udc_writeb(base
, S3C2410_UDC_INDEX_EP0
, S3C2410_UDC_INDEX_REG
);
229 udc_writeb(base
, (S3C2410_UDC_EP0_CSR_SOPKTRDY
230 | S3C2410_UDC_EP0_CSR_DE
),
231 S3C2410_UDC_EP0_CSR_REG
);
234 static inline void s3c2410_udc_set_ep0_de_in(void __iomem
*base
)
236 udc_writeb(base
, S3C2410_UDC_INDEX_EP0
, S3C2410_UDC_INDEX_REG
);
237 udc_writeb(base
, (S3C2410_UDC_EP0_CSR_IPKRDY
238 | S3C2410_UDC_EP0_CSR_DE
),
239 S3C2410_UDC_EP0_CSR_REG
);
242 /*------------------------- I/O ----------------------------------*/
247 static void s3c2410_udc_done(struct s3c2410_ep
*ep
,
248 struct s3c2410_request
*req
, int status
)
250 unsigned halted
= ep
->halted
;
252 list_del_init(&req
->queue
);
254 if (likely(req
->req
.status
== -EINPROGRESS
))
255 req
->req
.status
= status
;
257 status
= req
->req
.status
;
260 usb_gadget_giveback_request(&ep
->ep
, &req
->req
);
264 static void s3c2410_udc_nuke(struct s3c2410_udc
*udc
,
265 struct s3c2410_ep
*ep
, int status
)
268 if (&ep
->queue
== NULL
)
271 while (!list_empty(&ep
->queue
)) {
272 struct s3c2410_request
*req
;
273 req
= list_entry(ep
->queue
.next
, struct s3c2410_request
,
275 s3c2410_udc_done(ep
, req
, status
);
279 static inline int s3c2410_udc_fifo_count_out(void)
283 tmp
= udc_read(S3C2410_UDC_OUT_FIFO_CNT2_REG
) << 8;
284 tmp
|= udc_read(S3C2410_UDC_OUT_FIFO_CNT1_REG
);
289 * s3c2410_udc_write_packet
291 static inline int s3c2410_udc_write_packet(int fifo
,
292 struct s3c2410_request
*req
,
295 unsigned len
= min(req
->req
.length
- req
->req
.actual
, max
);
296 u8
*buf
= req
->req
.buf
+ req
->req
.actual
;
300 dprintk(DEBUG_VERBOSE
, "%s %d %d %d %d\n", __func__
,
301 req
->req
.actual
, req
->req
.length
, len
, req
->req
.actual
+ len
);
303 req
->req
.actual
+= len
;
306 writesb(base_addr
+ fifo
, buf
, len
);
311 * s3c2410_udc_write_fifo
313 * return: 0 = still running, 1 = completed, negative = errno
315 static int s3c2410_udc_write_fifo(struct s3c2410_ep
*ep
,
316 struct s3c2410_request
*req
)
324 idx
= ep
->bEndpointAddress
& 0x7F;
329 fifo_reg
= S3C2410_UDC_EP0_FIFO_REG
;
332 fifo_reg
= S3C2410_UDC_EP1_FIFO_REG
;
335 fifo_reg
= S3C2410_UDC_EP2_FIFO_REG
;
338 fifo_reg
= S3C2410_UDC_EP3_FIFO_REG
;
341 fifo_reg
= S3C2410_UDC_EP4_FIFO_REG
;
345 count
= s3c2410_udc_write_packet(fifo_reg
, req
, ep
->ep
.maxpacket
);
347 /* last packet is often short (sometimes a zlp) */
348 if (count
!= ep
->ep
.maxpacket
)
350 else if (req
->req
.length
!= req
->req
.actual
|| req
->req
.zero
)
355 /* Only ep0 debug messages are interesting */
357 dprintk(DEBUG_NORMAL
,
358 "Written ep%d %d.%d of %d b [last %d,z %d]\n",
359 idx
, count
, req
->req
.actual
, req
->req
.length
,
360 is_last
, req
->req
.zero
);
363 /* The order is important. It prevents sending 2 packets
364 * at the same time */
367 /* Reset signal => no need to say 'data sent' */
368 if (!(udc_read(S3C2410_UDC_USB_INT_REG
)
369 & S3C2410_UDC_USBINT_RESET
))
370 s3c2410_udc_set_ep0_de_in(base_addr
);
371 ep
->dev
->ep0state
= EP0_IDLE
;
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
);
380 s3c2410_udc_done(ep
, req
, 0);
384 /* Reset signal => no need to say 'data sent' */
385 if (!(udc_read(S3C2410_UDC_USB_INT_REG
)
386 & S3C2410_UDC_USBINT_RESET
))
387 s3c2410_udc_set_ep0_ipr(base_addr
);
389 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
390 ep_csr
= udc_read(S3C2410_UDC_IN_CSR1_REG
);
391 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
392 udc_write(ep_csr
| S3C2410_UDC_ICSR1_PKTRDY
,
393 S3C2410_UDC_IN_CSR1_REG
);
400 static inline int s3c2410_udc_read_packet(int fifo
, u8
*buf
,
401 struct s3c2410_request
*req
, unsigned avail
)
405 len
= min(req
->req
.length
- req
->req
.actual
, avail
);
406 req
->req
.actual
+= len
;
408 readsb(fifo
+ base_addr
, buf
, len
);
413 * return: 0 = still running, 1 = queue empty, negative = errno
415 static int s3c2410_udc_read_fifo(struct s3c2410_ep
*ep
,
416 struct s3c2410_request
*req
)
420 unsigned bufferspace
;
427 idx
= ep
->bEndpointAddress
& 0x7F;
433 fifo_reg
= S3C2410_UDC_EP0_FIFO_REG
;
436 fifo_reg
= S3C2410_UDC_EP1_FIFO_REG
;
439 fifo_reg
= S3C2410_UDC_EP2_FIFO_REG
;
442 fifo_reg
= S3C2410_UDC_EP3_FIFO_REG
;
445 fifo_reg
= S3C2410_UDC_EP4_FIFO_REG
;
449 if (!req
->req
.length
)
452 buf
= req
->req
.buf
+ req
->req
.actual
;
453 bufferspace
= req
->req
.length
- req
->req
.actual
;
455 dprintk(DEBUG_NORMAL
, "%s: buffer full!\n", __func__
);
459 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
461 fifo_count
= s3c2410_udc_fifo_count_out();
462 dprintk(DEBUG_NORMAL
, "%s fifo count : %d\n", __func__
, fifo_count
);
464 if (fifo_count
> ep
->ep
.maxpacket
)
465 avail
= ep
->ep
.maxpacket
;
469 fifo_count
= s3c2410_udc_read_packet(fifo_reg
, buf
, req
, avail
);
471 /* checking this with ep0 is not accurate as we already
472 * read a control request
474 if (idx
!= 0 && fifo_count
< ep
->ep
.maxpacket
) {
476 /* overflowed this request? flush extra data */
477 if (fifo_count
!= avail
)
478 req
->req
.status
= -EOVERFLOW
;
480 is_last
= (req
->req
.length
<= req
->req
.actual
) ? 1 : 0;
483 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
484 fifo_count
= s3c2410_udc_fifo_count_out();
486 /* Only ep0 debug messages are interesting */
488 dprintk(DEBUG_VERBOSE
, "%s fifo count : %d [last %d]\n",
489 __func__
, fifo_count
, is_last
);
493 s3c2410_udc_set_ep0_de_out(base_addr
);
494 ep
->dev
->ep0state
= EP0_IDLE
;
496 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
497 ep_csr
= udc_read(S3C2410_UDC_OUT_CSR1_REG
);
498 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
499 udc_write(ep_csr
& ~S3C2410_UDC_OCSR1_PKTRDY
,
500 S3C2410_UDC_OUT_CSR1_REG
);
503 s3c2410_udc_done(ep
, req
, 0);
506 s3c2410_udc_clear_ep0_opr(base_addr
);
508 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
509 ep_csr
= udc_read(S3C2410_UDC_OUT_CSR1_REG
);
510 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
511 udc_write(ep_csr
& ~S3C2410_UDC_OCSR1_PKTRDY
,
512 S3C2410_UDC_OUT_CSR1_REG
);
519 static int s3c2410_udc_read_fifo_crq(struct usb_ctrlrequest
*crq
)
521 unsigned char *outbuf
= (unsigned char *)crq
;
524 udc_write(0, S3C2410_UDC_INDEX_REG
);
526 bytes_read
= s3c2410_udc_fifo_count_out();
528 dprintk(DEBUG_NORMAL
, "%s: fifo_count=%d\n", __func__
, bytes_read
);
530 if (bytes_read
> sizeof(struct usb_ctrlrequest
))
531 bytes_read
= sizeof(struct usb_ctrlrequest
);
533 readsb(S3C2410_UDC_EP0_FIFO_REG
+ base_addr
, outbuf
, bytes_read
);
535 dprintk(DEBUG_VERBOSE
, "%s: len=%d %02x:%02x {%x,%x,%x}\n", __func__
,
536 bytes_read
, crq
->bRequest
, crq
->bRequestType
,
537 crq
->wValue
, crq
->wIndex
, crq
->wLength
);
542 static int s3c2410_udc_get_status(struct s3c2410_udc
*dev
,
543 struct usb_ctrlrequest
*crq
)
546 u8 ep_num
= crq
->wIndex
& 0x7F;
547 u8 is_in
= crq
->wIndex
& USB_DIR_IN
;
549 switch (crq
->bRequestType
& USB_RECIP_MASK
) {
550 case USB_RECIP_INTERFACE
:
553 case USB_RECIP_DEVICE
:
554 status
= dev
->devstatus
;
557 case USB_RECIP_ENDPOINT
:
558 if (ep_num
> 4 || crq
->wLength
> 2)
562 udc_write(0, S3C2410_UDC_INDEX_REG
);
563 status
= udc_read(S3C2410_UDC_IN_CSR1_REG
);
564 status
= status
& S3C2410_UDC_EP0_CSR_SENDSTL
;
566 udc_write(ep_num
, S3C2410_UDC_INDEX_REG
);
568 status
= udc_read(S3C2410_UDC_IN_CSR1_REG
);
569 status
= status
& S3C2410_UDC_ICSR1_SENDSTL
;
571 status
= udc_read(S3C2410_UDC_OUT_CSR1_REG
);
572 status
= status
& S3C2410_UDC_OCSR1_SENDSTL
;
576 status
= status
? 1 : 0;
583 /* Seems to be needed to get it working. ouch :( */
585 udc_write(status
& 0xFF, S3C2410_UDC_EP0_FIFO_REG
);
586 udc_write(status
>> 8, S3C2410_UDC_EP0_FIFO_REG
);
587 s3c2410_udc_set_ep0_de_in(base_addr
);
591 /*------------------------- usb state machine -------------------------------*/
592 static int s3c2410_udc_set_halt(struct usb_ep
*_ep
, int value
);
594 static void s3c2410_udc_handle_ep0_idle(struct s3c2410_udc
*dev
,
595 struct s3c2410_ep
*ep
,
596 struct usb_ctrlrequest
*crq
,
601 /* start control request? */
602 if (!(ep0csr
& S3C2410_UDC_EP0_CSR_OPKRDY
))
605 s3c2410_udc_nuke(dev
, ep
, -EPROTO
);
607 len
= s3c2410_udc_read_fifo_crq(crq
);
608 if (len
!= sizeof(*crq
)) {
609 dprintk(DEBUG_NORMAL
, "setup begin: fifo READ ERROR"
610 " wanted %d bytes got %d. Stalling out...\n",
612 s3c2410_udc_set_ep0_ss(base_addr
);
616 dprintk(DEBUG_NORMAL
, "bRequest = %d bRequestType %d wLength = %d\n",
617 crq
->bRequest
, crq
->bRequestType
, crq
->wLength
);
619 /* cope with automagic for some standard requests. */
620 dev
->req_std
= (crq
->bRequestType
& USB_TYPE_MASK
)
621 == USB_TYPE_STANDARD
;
623 dev
->req_pending
= 1;
625 switch (crq
->bRequest
) {
626 case USB_REQ_SET_CONFIGURATION
:
627 dprintk(DEBUG_NORMAL
, "USB_REQ_SET_CONFIGURATION ...\n");
629 if (crq
->bRequestType
== USB_RECIP_DEVICE
) {
631 s3c2410_udc_set_ep0_de_out(base_addr
);
635 case USB_REQ_SET_INTERFACE
:
636 dprintk(DEBUG_NORMAL
, "USB_REQ_SET_INTERFACE ...\n");
638 if (crq
->bRequestType
== USB_RECIP_INTERFACE
) {
640 s3c2410_udc_set_ep0_de_out(base_addr
);
644 case USB_REQ_SET_ADDRESS
:
645 dprintk(DEBUG_NORMAL
, "USB_REQ_SET_ADDRESS ...\n");
647 if (crq
->bRequestType
== USB_RECIP_DEVICE
) {
648 tmp
= crq
->wValue
& 0x7F;
650 udc_write((tmp
| S3C2410_UDC_FUNCADDR_UPDATE
),
651 S3C2410_UDC_FUNC_ADDR_REG
);
652 s3c2410_udc_set_ep0_de_out(base_addr
);
657 case USB_REQ_GET_STATUS
:
658 dprintk(DEBUG_NORMAL
, "USB_REQ_GET_STATUS ...\n");
659 s3c2410_udc_clear_ep0_opr(base_addr
);
662 if (!s3c2410_udc_get_status(dev
, crq
))
667 case USB_REQ_CLEAR_FEATURE
:
668 s3c2410_udc_clear_ep0_opr(base_addr
);
670 if (crq
->bRequestType
!= USB_RECIP_ENDPOINT
)
673 if (crq
->wValue
!= USB_ENDPOINT_HALT
|| crq
->wLength
!= 0)
676 s3c2410_udc_set_halt(&dev
->ep
[crq
->wIndex
& 0x7f].ep
, 0);
677 s3c2410_udc_set_ep0_de_out(base_addr
);
680 case USB_REQ_SET_FEATURE
:
681 s3c2410_udc_clear_ep0_opr(base_addr
);
683 if (crq
->bRequestType
!= USB_RECIP_ENDPOINT
)
686 if (crq
->wValue
!= USB_ENDPOINT_HALT
|| crq
->wLength
!= 0)
689 s3c2410_udc_set_halt(&dev
->ep
[crq
->wIndex
& 0x7f].ep
, 1);
690 s3c2410_udc_set_ep0_de_out(base_addr
);
694 s3c2410_udc_clear_ep0_opr(base_addr
);
698 if (crq
->bRequestType
& USB_DIR_IN
)
699 dev
->ep0state
= EP0_IN_DATA_PHASE
;
701 dev
->ep0state
= EP0_OUT_DATA_PHASE
;
706 /* deliver the request to the gadget driver */
707 ret
= dev
->driver
->setup(&dev
->gadget
, crq
);
709 if (dev
->req_config
) {
710 dprintk(DEBUG_NORMAL
, "config change %02x fail %d?\n",
715 if (ret
== -EOPNOTSUPP
)
716 dprintk(DEBUG_NORMAL
, "Operation not supported\n");
718 dprintk(DEBUG_NORMAL
,
719 "dev->driver->setup failed. (%d)\n", ret
);
722 s3c2410_udc_set_ep0_ss(base_addr
);
723 s3c2410_udc_set_ep0_de_out(base_addr
);
724 dev
->ep0state
= EP0_IDLE
;
725 /* deferred i/o == no response yet */
726 } else if (dev
->req_pending
) {
727 dprintk(DEBUG_VERBOSE
, "dev->req_pending... what now?\n");
728 dev
->req_pending
= 0;
731 dprintk(DEBUG_VERBOSE
, "ep0state %s\n", ep0states
[dev
->ep0state
]);
734 static void s3c2410_udc_handle_ep0(struct s3c2410_udc
*dev
)
737 struct s3c2410_ep
*ep
= &dev
->ep
[0];
738 struct s3c2410_request
*req
;
739 struct usb_ctrlrequest crq
;
741 if (list_empty(&ep
->queue
))
744 req
= list_entry(ep
->queue
.next
, struct s3c2410_request
, queue
);
746 /* We make the assumption that S3C2410_UDC_IN_CSR1_REG equal to
747 * S3C2410_UDC_EP0_CSR_REG when index is zero */
749 udc_write(0, S3C2410_UDC_INDEX_REG
);
750 ep0csr
= udc_read(S3C2410_UDC_IN_CSR1_REG
);
752 dprintk(DEBUG_NORMAL
, "ep0csr %x ep0state %s\n",
753 ep0csr
, ep0states
[dev
->ep0state
]);
755 /* clear stall status */
756 if (ep0csr
& S3C2410_UDC_EP0_CSR_SENTSTL
) {
757 s3c2410_udc_nuke(dev
, ep
, -EPIPE
);
758 dprintk(DEBUG_NORMAL
, "... clear SENT_STALL ...\n");
759 s3c2410_udc_clear_ep0_sst(base_addr
);
760 dev
->ep0state
= EP0_IDLE
;
764 /* clear setup end */
765 if (ep0csr
& S3C2410_UDC_EP0_CSR_SE
) {
766 dprintk(DEBUG_NORMAL
, "... serviced SETUP_END ...\n");
767 s3c2410_udc_nuke(dev
, ep
, 0);
768 s3c2410_udc_clear_ep0_se(base_addr
);
769 dev
->ep0state
= EP0_IDLE
;
772 switch (dev
->ep0state
) {
774 s3c2410_udc_handle_ep0_idle(dev
, ep
, &crq
, ep0csr
);
777 case EP0_IN_DATA_PHASE
: /* GET_DESCRIPTOR etc */
778 dprintk(DEBUG_NORMAL
, "EP0_IN_DATA_PHASE ... what now?\n");
779 if (!(ep0csr
& S3C2410_UDC_EP0_CSR_IPKRDY
) && req
)
780 s3c2410_udc_write_fifo(ep
, req
);
783 case EP0_OUT_DATA_PHASE
: /* SET_DESCRIPTOR etc */
784 dprintk(DEBUG_NORMAL
, "EP0_OUT_DATA_PHASE ... what now?\n");
785 if ((ep0csr
& S3C2410_UDC_EP0_CSR_OPKRDY
) && req
)
786 s3c2410_udc_read_fifo(ep
, req
);
790 dprintk(DEBUG_NORMAL
, "EP0_END_XFER ... what now?\n");
791 dev
->ep0state
= EP0_IDLE
;
795 dprintk(DEBUG_NORMAL
, "EP0_STALL ... what now?\n");
796 dev
->ep0state
= EP0_IDLE
;
802 * handle_ep - Manage I/O endpoints
805 static void s3c2410_udc_handle_ep(struct s3c2410_ep
*ep
)
807 struct s3c2410_request
*req
;
808 int is_in
= ep
->bEndpointAddress
& USB_DIR_IN
;
812 if (likely(!list_empty(&ep
->queue
)))
813 req
= list_entry(ep
->queue
.next
,
814 struct s3c2410_request
, queue
);
818 idx
= ep
->bEndpointAddress
& 0x7F;
821 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
822 ep_csr1
= udc_read(S3C2410_UDC_IN_CSR1_REG
);
823 dprintk(DEBUG_VERBOSE
, "ep%01d write csr:%02x %d\n",
824 idx
, ep_csr1
, req
? 1 : 0);
826 if (ep_csr1
& S3C2410_UDC_ICSR1_SENTSTL
) {
827 dprintk(DEBUG_VERBOSE
, "st\n");
828 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
829 udc_write(ep_csr1
& ~S3C2410_UDC_ICSR1_SENTSTL
,
830 S3C2410_UDC_IN_CSR1_REG
);
834 if (!(ep_csr1
& S3C2410_UDC_ICSR1_PKTRDY
) && req
)
835 s3c2410_udc_write_fifo(ep
, req
);
837 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
838 ep_csr1
= udc_read(S3C2410_UDC_OUT_CSR1_REG
);
839 dprintk(DEBUG_VERBOSE
, "ep%01d rd csr:%02x\n", idx
, ep_csr1
);
841 if (ep_csr1
& S3C2410_UDC_OCSR1_SENTSTL
) {
842 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
843 udc_write(ep_csr1
& ~S3C2410_UDC_OCSR1_SENTSTL
,
844 S3C2410_UDC_OUT_CSR1_REG
);
848 if ((ep_csr1
& S3C2410_UDC_OCSR1_PKTRDY
) && req
)
849 s3c2410_udc_read_fifo(ep
, req
);
853 #include <mach/regs-irq.h>
856 * s3c2410_udc_irq - interrupt handler
858 static irqreturn_t
s3c2410_udc_irq(int dummy
, void *_dev
)
860 struct s3c2410_udc
*dev
= _dev
;
869 spin_lock_irqsave(&dev
->lock
, flags
);
871 /* Driver connected ? */
873 /* Clear interrupts */
874 udc_write(udc_read(S3C2410_UDC_USB_INT_REG
),
875 S3C2410_UDC_USB_INT_REG
);
876 udc_write(udc_read(S3C2410_UDC_EP_INT_REG
),
877 S3C2410_UDC_EP_INT_REG
);
881 idx
= udc_read(S3C2410_UDC_INDEX_REG
);
883 /* Read status registers */
884 usb_status
= udc_read(S3C2410_UDC_USB_INT_REG
);
885 usbd_status
= udc_read(S3C2410_UDC_EP_INT_REG
);
886 pwr_reg
= udc_read(S3C2410_UDC_PWR_REG
);
888 udc_writeb(base_addr
, S3C2410_UDC_INDEX_EP0
, S3C2410_UDC_INDEX_REG
);
889 ep0csr
= udc_read(S3C2410_UDC_IN_CSR1_REG
);
891 dprintk(DEBUG_NORMAL
, "usbs=%02x, usbds=%02x, pwr=%02x ep0csr=%02x\n",
892 usb_status
, usbd_status
, pwr_reg
, ep0csr
);
895 * Now, handle interrupts. There's two types :
896 * - Reset, Resume, Suspend coming -> usb_int_reg
901 if (usb_status
& S3C2410_UDC_USBINT_RESET
) {
902 /* two kind of reset :
903 * - reset start -> pwr reg = 8
904 * - reset end -> pwr reg = 0
906 dprintk(DEBUG_NORMAL
, "USB reset csr %x pwr %x\n",
909 dev
->gadget
.speed
= USB_SPEED_UNKNOWN
;
910 udc_write(0x00, S3C2410_UDC_INDEX_REG
);
911 udc_write((dev
->ep
[0].ep
.maxpacket
& 0x7ff) >> 3,
912 S3C2410_UDC_MAXP_REG
);
915 dev
->ep0state
= EP0_IDLE
;
916 dev
->gadget
.speed
= USB_SPEED_FULL
;
918 /* clear interrupt */
919 udc_write(S3C2410_UDC_USBINT_RESET
,
920 S3C2410_UDC_USB_INT_REG
);
922 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
923 spin_unlock_irqrestore(&dev
->lock
, flags
);
928 if (usb_status
& S3C2410_UDC_USBINT_RESUME
) {
929 dprintk(DEBUG_NORMAL
, "USB resume\n");
931 /* clear interrupt */
932 udc_write(S3C2410_UDC_USBINT_RESUME
,
933 S3C2410_UDC_USB_INT_REG
);
935 if (dev
->gadget
.speed
!= USB_SPEED_UNKNOWN
937 && dev
->driver
->resume
)
938 dev
->driver
->resume(&dev
->gadget
);
942 if (usb_status
& S3C2410_UDC_USBINT_SUSPEND
) {
943 dprintk(DEBUG_NORMAL
, "USB suspend\n");
945 /* clear interrupt */
946 udc_write(S3C2410_UDC_USBINT_SUSPEND
,
947 S3C2410_UDC_USB_INT_REG
);
949 if (dev
->gadget
.speed
!= USB_SPEED_UNKNOWN
951 && dev
->driver
->suspend
)
952 dev
->driver
->suspend(&dev
->gadget
);
954 dev
->ep0state
= EP0_IDLE
;
958 /* control traffic */
959 /* check on ep0csr != 0 is not a good idea as clearing in_pkt_ready
960 * generate an interrupt
962 if (usbd_status
& S3C2410_UDC_INT_EP0
) {
963 dprintk(DEBUG_VERBOSE
, "USB ep0 irq\n");
964 /* Clear the interrupt bit by setting it to 1 */
965 udc_write(S3C2410_UDC_INT_EP0
, S3C2410_UDC_EP_INT_REG
);
966 s3c2410_udc_handle_ep0(dev
);
969 /* endpoint data transfers */
970 for (i
= 1; i
< S3C2410_ENDPOINTS
; i
++) {
972 if (usbd_status
& tmp
) {
973 dprintk(DEBUG_VERBOSE
, "USB ep%d irq\n", i
);
975 /* Clear the interrupt bit by setting it to 1 */
976 udc_write(tmp
, S3C2410_UDC_EP_INT_REG
);
977 s3c2410_udc_handle_ep(&dev
->ep
[i
]);
981 /* what else causes this interrupt? a receive! who is it? */
982 if (!usb_status
&& !usbd_status
&& !pwr_reg
&& !ep0csr
) {
983 for (i
= 1; i
< S3C2410_ENDPOINTS
; i
++) {
984 idx2
= udc_read(S3C2410_UDC_INDEX_REG
);
985 udc_write(i
, S3C2410_UDC_INDEX_REG
);
987 if (udc_read(S3C2410_UDC_OUT_CSR1_REG
) & 0x1)
988 s3c2410_udc_handle_ep(&dev
->ep
[i
]);
991 udc_write(idx2
, S3C2410_UDC_INDEX_REG
);
995 dprintk(DEBUG_VERBOSE
, "irq: %d s3c2410_udc_done.\n", IRQ_USBD
);
997 /* Restore old index */
998 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
1000 spin_unlock_irqrestore(&dev
->lock
, flags
);
1004 /*------------------------- s3c2410_ep_ops ----------------------------------*/
1006 static inline struct s3c2410_ep
*to_s3c2410_ep(struct usb_ep
*ep
)
1008 return container_of(ep
, struct s3c2410_ep
, ep
);
1011 static inline struct s3c2410_udc
*to_s3c2410_udc(struct usb_gadget
*gadget
)
1013 return container_of(gadget
, struct s3c2410_udc
, gadget
);
1016 static inline struct s3c2410_request
*to_s3c2410_req(struct usb_request
*req
)
1018 return container_of(req
, struct s3c2410_request
, req
);
1022 * s3c2410_udc_ep_enable
1024 static int s3c2410_udc_ep_enable(struct usb_ep
*_ep
,
1025 const struct usb_endpoint_descriptor
*desc
)
1027 struct s3c2410_udc
*dev
;
1028 struct s3c2410_ep
*ep
;
1030 unsigned long flags
;
1034 ep
= to_s3c2410_ep(_ep
);
1037 || _ep
->name
== ep0name
1038 || desc
->bDescriptorType
!= USB_DT_ENDPOINT
)
1042 if (!dev
->driver
|| dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
1045 max
= usb_endpoint_maxp(desc
);
1047 local_irq_save(flags
);
1048 _ep
->maxpacket
= max
;
1051 ep
->bEndpointAddress
= desc
->bEndpointAddress
;
1053 /* set max packet */
1054 udc_write(ep
->num
, S3C2410_UDC_INDEX_REG
);
1055 udc_write(max
>> 3, S3C2410_UDC_MAXP_REG
);
1057 /* set type, direction, address; reset fifo counters */
1058 if (desc
->bEndpointAddress
& USB_DIR_IN
) {
1059 csr1
= S3C2410_UDC_ICSR1_FFLUSH
|S3C2410_UDC_ICSR1_CLRDT
;
1060 csr2
= S3C2410_UDC_ICSR2_MODEIN
|S3C2410_UDC_ICSR2_DMAIEN
;
1062 udc_write(ep
->num
, S3C2410_UDC_INDEX_REG
);
1063 udc_write(csr1
, S3C2410_UDC_IN_CSR1_REG
);
1064 udc_write(ep
->num
, S3C2410_UDC_INDEX_REG
);
1065 udc_write(csr2
, S3C2410_UDC_IN_CSR2_REG
);
1067 /* don't flush in fifo or it will cause endpoint interrupt */
1068 csr1
= S3C2410_UDC_ICSR1_CLRDT
;
1069 csr2
= S3C2410_UDC_ICSR2_DMAIEN
;
1071 udc_write(ep
->num
, S3C2410_UDC_INDEX_REG
);
1072 udc_write(csr1
, S3C2410_UDC_IN_CSR1_REG
);
1073 udc_write(ep
->num
, S3C2410_UDC_INDEX_REG
);
1074 udc_write(csr2
, S3C2410_UDC_IN_CSR2_REG
);
1076 csr1
= S3C2410_UDC_OCSR1_FFLUSH
| S3C2410_UDC_OCSR1_CLRDT
;
1077 csr2
= S3C2410_UDC_OCSR2_DMAIEN
;
1079 udc_write(ep
->num
, S3C2410_UDC_INDEX_REG
);
1080 udc_write(csr1
, S3C2410_UDC_OUT_CSR1_REG
);
1081 udc_write(ep
->num
, S3C2410_UDC_INDEX_REG
);
1082 udc_write(csr2
, S3C2410_UDC_OUT_CSR2_REG
);
1086 int_en_reg
= udc_read(S3C2410_UDC_EP_INT_EN_REG
);
1087 udc_write(int_en_reg
| (1 << ep
->num
), S3C2410_UDC_EP_INT_EN_REG
);
1089 /* print some debug message */
1090 tmp
= desc
->bEndpointAddress
;
1091 dprintk(DEBUG_NORMAL
, "enable %s(%d) ep%x%s-blk max %02x\n",
1092 _ep
->name
, ep
->num
, tmp
,
1093 desc
->bEndpointAddress
& USB_DIR_IN
? "in" : "out", max
);
1095 local_irq_restore(flags
);
1096 s3c2410_udc_set_halt(_ep
, 0);
1102 * s3c2410_udc_ep_disable
1104 static int s3c2410_udc_ep_disable(struct usb_ep
*_ep
)
1106 struct s3c2410_ep
*ep
= to_s3c2410_ep(_ep
);
1107 unsigned long flags
;
1110 if (!_ep
|| !ep
->ep
.desc
) {
1111 dprintk(DEBUG_NORMAL
, "%s not enabled\n",
1112 _ep
? ep
->ep
.name
: NULL
);
1116 local_irq_save(flags
);
1118 dprintk(DEBUG_NORMAL
, "ep_disable: %s\n", _ep
->name
);
1123 s3c2410_udc_nuke(ep
->dev
, ep
, -ESHUTDOWN
);
1126 int_en_reg
= udc_read(S3C2410_UDC_EP_INT_EN_REG
);
1127 udc_write(int_en_reg
& ~(1<<ep
->num
), S3C2410_UDC_EP_INT_EN_REG
);
1129 local_irq_restore(flags
);
1131 dprintk(DEBUG_NORMAL
, "%s disabled\n", _ep
->name
);
1137 * s3c2410_udc_alloc_request
1139 static struct usb_request
*
1140 s3c2410_udc_alloc_request(struct usb_ep
*_ep
, gfp_t mem_flags
)
1142 struct s3c2410_request
*req
;
1144 dprintk(DEBUG_VERBOSE
, "%s(%p,%d)\n", __func__
, _ep
, mem_flags
);
1149 req
= kzalloc(sizeof(struct s3c2410_request
), mem_flags
);
1153 INIT_LIST_HEAD(&req
->queue
);
1158 * s3c2410_udc_free_request
1161 s3c2410_udc_free_request(struct usb_ep
*_ep
, struct usb_request
*_req
)
1163 struct s3c2410_ep
*ep
= to_s3c2410_ep(_ep
);
1164 struct s3c2410_request
*req
= to_s3c2410_req(_req
);
1166 dprintk(DEBUG_VERBOSE
, "%s(%p,%p)\n", __func__
, _ep
, _req
);
1168 if (!ep
|| !_req
|| (!ep
->ep
.desc
&& _ep
->name
!= ep0name
))
1171 WARN_ON(!list_empty(&req
->queue
));
1178 static int s3c2410_udc_queue(struct usb_ep
*_ep
, struct usb_request
*_req
,
1181 struct s3c2410_request
*req
= to_s3c2410_req(_req
);
1182 struct s3c2410_ep
*ep
= to_s3c2410_ep(_ep
);
1183 struct s3c2410_udc
*dev
;
1186 unsigned long flags
;
1188 if (unlikely(!_ep
|| (!ep
->ep
.desc
&& ep
->ep
.name
!= ep0name
))) {
1189 dprintk(DEBUG_NORMAL
, "%s: invalid args\n", __func__
);
1194 if (unlikely(!dev
->driver
1195 || dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)) {
1199 local_irq_save(flags
);
1201 if (unlikely(!_req
|| !_req
->complete
1202 || !_req
->buf
|| !list_empty(&req
->queue
))) {
1204 dprintk(DEBUG_NORMAL
, "%s: 1 X X X\n", __func__
);
1206 dprintk(DEBUG_NORMAL
, "%s: 0 %01d %01d %01d\n",
1207 __func__
, !_req
->complete
, !_req
->buf
,
1208 !list_empty(&req
->queue
));
1211 local_irq_restore(flags
);
1215 _req
->status
= -EINPROGRESS
;
1218 dprintk(DEBUG_VERBOSE
, "%s: ep%x len %d\n",
1219 __func__
, ep
->bEndpointAddress
, _req
->length
);
1221 if (ep
->bEndpointAddress
) {
1222 udc_write(ep
->bEndpointAddress
& 0x7F, S3C2410_UDC_INDEX_REG
);
1224 ep_csr
= udc_read((ep
->bEndpointAddress
& USB_DIR_IN
)
1225 ? S3C2410_UDC_IN_CSR1_REG
1226 : S3C2410_UDC_OUT_CSR1_REG
);
1227 fifo_count
= s3c2410_udc_fifo_count_out();
1229 udc_write(0, S3C2410_UDC_INDEX_REG
);
1230 ep_csr
= udc_read(S3C2410_UDC_IN_CSR1_REG
);
1231 fifo_count
= s3c2410_udc_fifo_count_out();
1234 /* kickstart this i/o queue? */
1235 if (list_empty(&ep
->queue
) && !ep
->halted
) {
1236 if (ep
->bEndpointAddress
== 0 /* ep0 */) {
1237 switch (dev
->ep0state
) {
1238 case EP0_IN_DATA_PHASE
:
1239 if (!(ep_csr
&S3C2410_UDC_EP0_CSR_IPKRDY
)
1240 && s3c2410_udc_write_fifo(ep
,
1242 dev
->ep0state
= EP0_IDLE
;
1247 case EP0_OUT_DATA_PHASE
:
1249 || ((ep_csr
& S3C2410_UDC_OCSR1_PKTRDY
)
1250 && s3c2410_udc_read_fifo(ep
,
1252 dev
->ep0state
= EP0_IDLE
;
1258 local_irq_restore(flags
);
1261 } else if ((ep
->bEndpointAddress
& USB_DIR_IN
) != 0
1262 && (!(ep_csr
&S3C2410_UDC_OCSR1_PKTRDY
))
1263 && s3c2410_udc_write_fifo(ep
, req
)) {
1265 } else if ((ep_csr
& S3C2410_UDC_OCSR1_PKTRDY
)
1267 && s3c2410_udc_read_fifo(ep
, req
)) {
1272 /* pio or dma irq handler advances the queue. */
1274 list_add_tail(&req
->queue
, &ep
->queue
);
1276 local_irq_restore(flags
);
1278 dprintk(DEBUG_VERBOSE
, "%s ok\n", __func__
);
1283 * s3c2410_udc_dequeue
1285 static int s3c2410_udc_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
1287 struct s3c2410_ep
*ep
= to_s3c2410_ep(_ep
);
1288 struct s3c2410_udc
*udc
;
1289 int retval
= -EINVAL
;
1290 unsigned long flags
;
1291 struct s3c2410_request
*req
= NULL
;
1293 dprintk(DEBUG_VERBOSE
, "%s(%p,%p)\n", __func__
, _ep
, _req
);
1295 if (!the_controller
->driver
)
1301 udc
= to_s3c2410_udc(ep
->gadget
);
1303 local_irq_save(flags
);
1305 list_for_each_entry(req
, &ep
->queue
, queue
) {
1306 if (&req
->req
== _req
) {
1307 list_del_init(&req
->queue
);
1308 _req
->status
= -ECONNRESET
;
1315 dprintk(DEBUG_VERBOSE
,
1316 "dequeued req %p from %s, len %d buf %p\n",
1317 req
, _ep
->name
, _req
->length
, _req
->buf
);
1319 s3c2410_udc_done(ep
, req
, -ECONNRESET
);
1322 local_irq_restore(flags
);
1327 * s3c2410_udc_set_halt
1329 static int s3c2410_udc_set_halt(struct usb_ep
*_ep
, int value
)
1331 struct s3c2410_ep
*ep
= to_s3c2410_ep(_ep
);
1333 unsigned long flags
;
1336 if (unlikely(!_ep
|| (!ep
->ep
.desc
&& ep
->ep
.name
!= ep0name
))) {
1337 dprintk(DEBUG_NORMAL
, "%s: inval 2\n", __func__
);
1341 local_irq_save(flags
);
1343 idx
= ep
->bEndpointAddress
& 0x7F;
1346 s3c2410_udc_set_ep0_ss(base_addr
);
1347 s3c2410_udc_set_ep0_de_out(base_addr
);
1349 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
1350 ep_csr
= udc_read((ep
->bEndpointAddress
& USB_DIR_IN
)
1351 ? S3C2410_UDC_IN_CSR1_REG
1352 : S3C2410_UDC_OUT_CSR1_REG
);
1354 if ((ep
->bEndpointAddress
& USB_DIR_IN
) != 0) {
1356 udc_write(ep_csr
| S3C2410_UDC_ICSR1_SENDSTL
,
1357 S3C2410_UDC_IN_CSR1_REG
);
1359 ep_csr
&= ~S3C2410_UDC_ICSR1_SENDSTL
;
1360 udc_write(ep_csr
, S3C2410_UDC_IN_CSR1_REG
);
1361 ep_csr
|= S3C2410_UDC_ICSR1_CLRDT
;
1362 udc_write(ep_csr
, S3C2410_UDC_IN_CSR1_REG
);
1366 udc_write(ep_csr
| S3C2410_UDC_OCSR1_SENDSTL
,
1367 S3C2410_UDC_OUT_CSR1_REG
);
1369 ep_csr
&= ~S3C2410_UDC_OCSR1_SENDSTL
;
1370 udc_write(ep_csr
, S3C2410_UDC_OUT_CSR1_REG
);
1371 ep_csr
|= S3C2410_UDC_OCSR1_CLRDT
;
1372 udc_write(ep_csr
, S3C2410_UDC_OUT_CSR1_REG
);
1377 ep
->halted
= value
? 1 : 0;
1378 local_irq_restore(flags
);
1383 static const struct usb_ep_ops s3c2410_ep_ops
= {
1384 .enable
= s3c2410_udc_ep_enable
,
1385 .disable
= s3c2410_udc_ep_disable
,
1387 .alloc_request
= s3c2410_udc_alloc_request
,
1388 .free_request
= s3c2410_udc_free_request
,
1390 .queue
= s3c2410_udc_queue
,
1391 .dequeue
= s3c2410_udc_dequeue
,
1393 .set_halt
= s3c2410_udc_set_halt
,
1396 /*------------------------- usb_gadget_ops ----------------------------------*/
1399 * s3c2410_udc_get_frame
1401 static int s3c2410_udc_get_frame(struct usb_gadget
*_gadget
)
1405 dprintk(DEBUG_VERBOSE
, "%s()\n", __func__
);
1407 tmp
= udc_read(S3C2410_UDC_FRAME_NUM2_REG
) << 8;
1408 tmp
|= udc_read(S3C2410_UDC_FRAME_NUM1_REG
);
1413 * s3c2410_udc_wakeup
1415 static int s3c2410_udc_wakeup(struct usb_gadget
*_gadget
)
1417 dprintk(DEBUG_NORMAL
, "%s()\n", __func__
);
1422 * s3c2410_udc_set_selfpowered
1424 static int s3c2410_udc_set_selfpowered(struct usb_gadget
*gadget
, int value
)
1426 struct s3c2410_udc
*udc
= to_s3c2410_udc(gadget
);
1428 dprintk(DEBUG_NORMAL
, "%s()\n", __func__
);
1430 gadget
->is_selfpowered
= (value
!= 0);
1432 udc
->devstatus
|= (1 << USB_DEVICE_SELF_POWERED
);
1434 udc
->devstatus
&= ~(1 << USB_DEVICE_SELF_POWERED
);
1439 static void s3c2410_udc_disable(struct s3c2410_udc
*dev
);
1440 static void s3c2410_udc_enable(struct s3c2410_udc
*dev
);
1442 static int s3c2410_udc_set_pullup(struct s3c2410_udc
*udc
, int is_on
)
1444 dprintk(DEBUG_NORMAL
, "%s()\n", __func__
);
1446 if (udc_info
&& (udc_info
->udc_command
||
1447 gpio_is_valid(udc_info
->pullup_pin
))) {
1450 s3c2410_udc_enable(udc
);
1452 if (udc
->gadget
.speed
!= USB_SPEED_UNKNOWN
) {
1453 if (udc
->driver
&& udc
->driver
->disconnect
)
1454 udc
->driver
->disconnect(&udc
->gadget
);
1457 s3c2410_udc_disable(udc
);
1466 static int s3c2410_udc_vbus_session(struct usb_gadget
*gadget
, int is_active
)
1468 struct s3c2410_udc
*udc
= to_s3c2410_udc(gadget
);
1470 dprintk(DEBUG_NORMAL
, "%s()\n", __func__
);
1472 udc
->vbus
= (is_active
!= 0);
1473 s3c2410_udc_set_pullup(udc
, is_active
);
1477 static int s3c2410_udc_pullup(struct usb_gadget
*gadget
, int is_on
)
1479 struct s3c2410_udc
*udc
= to_s3c2410_udc(gadget
);
1481 dprintk(DEBUG_NORMAL
, "%s()\n", __func__
);
1483 s3c2410_udc_set_pullup(udc
, is_on
);
1487 static irqreturn_t
s3c2410_udc_vbus_irq(int irq
, void *_dev
)
1489 struct s3c2410_udc
*dev
= _dev
;
1492 dprintk(DEBUG_NORMAL
, "%s()\n", __func__
);
1494 value
= gpio_get_value(udc_info
->vbus_pin
) ? 1 : 0;
1495 if (udc_info
->vbus_pin_inverted
)
1498 if (value
!= dev
->vbus
)
1499 s3c2410_udc_vbus_session(&dev
->gadget
, value
);
1504 static int s3c2410_vbus_draw(struct usb_gadget
*_gadget
, unsigned ma
)
1506 dprintk(DEBUG_NORMAL
, "%s()\n", __func__
);
1508 if (udc_info
&& udc_info
->vbus_draw
) {
1509 udc_info
->vbus_draw(ma
);
1516 static int s3c2410_udc_start(struct usb_gadget
*g
,
1517 struct usb_gadget_driver
*driver
);
1518 static int s3c2410_udc_stop(struct usb_gadget
*g
);
1520 static const struct usb_gadget_ops s3c2410_ops
= {
1521 .get_frame
= s3c2410_udc_get_frame
,
1522 .wakeup
= s3c2410_udc_wakeup
,
1523 .set_selfpowered
= s3c2410_udc_set_selfpowered
,
1524 .pullup
= s3c2410_udc_pullup
,
1525 .vbus_session
= s3c2410_udc_vbus_session
,
1526 .vbus_draw
= s3c2410_vbus_draw
,
1527 .udc_start
= s3c2410_udc_start
,
1528 .udc_stop
= s3c2410_udc_stop
,
1531 static void s3c2410_udc_command(enum s3c2410_udc_cmd_e cmd
)
1536 if (udc_info
->udc_command
) {
1537 udc_info
->udc_command(cmd
);
1538 } else if (gpio_is_valid(udc_info
->pullup_pin
)) {
1542 case S3C2410_UDC_P_ENABLE
:
1545 case S3C2410_UDC_P_DISABLE
:
1551 value
^= udc_info
->pullup_pin_inverted
;
1553 gpio_set_value(udc_info
->pullup_pin
, value
);
1557 /*------------------------- gadget driver handling---------------------------*/
1559 * s3c2410_udc_disable
1561 static void s3c2410_udc_disable(struct s3c2410_udc
*dev
)
1563 dprintk(DEBUG_NORMAL
, "%s()\n", __func__
);
1565 /* Disable all interrupts */
1566 udc_write(0x00, S3C2410_UDC_USB_INT_EN_REG
);
1567 udc_write(0x00, S3C2410_UDC_EP_INT_EN_REG
);
1569 /* Clear the interrupt registers */
1570 udc_write(S3C2410_UDC_USBINT_RESET
1571 | S3C2410_UDC_USBINT_RESUME
1572 | S3C2410_UDC_USBINT_SUSPEND
,
1573 S3C2410_UDC_USB_INT_REG
);
1575 udc_write(0x1F, S3C2410_UDC_EP_INT_REG
);
1577 /* Good bye, cruel world */
1578 s3c2410_udc_command(S3C2410_UDC_P_DISABLE
);
1580 /* Set speed to unknown */
1581 dev
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1585 * s3c2410_udc_reinit
1587 static void s3c2410_udc_reinit(struct s3c2410_udc
*dev
)
1591 /* device/ep0 records init */
1592 INIT_LIST_HEAD(&dev
->gadget
.ep_list
);
1593 INIT_LIST_HEAD(&dev
->gadget
.ep0
->ep_list
);
1594 dev
->ep0state
= EP0_IDLE
;
1596 for (i
= 0; i
< S3C2410_ENDPOINTS
; i
++) {
1597 struct s3c2410_ep
*ep
= &dev
->ep
[i
];
1600 list_add_tail(&ep
->ep
.ep_list
, &dev
->gadget
.ep_list
);
1605 INIT_LIST_HEAD(&ep
->queue
);
1606 usb_ep_set_maxpacket_limit(&ep
->ep
, ep
->ep
.maxpacket
);
1611 * s3c2410_udc_enable
1613 static void s3c2410_udc_enable(struct s3c2410_udc
*dev
)
1617 dprintk(DEBUG_NORMAL
, "s3c2410_udc_enable called\n");
1619 /* dev->gadget.speed = USB_SPEED_UNKNOWN; */
1620 dev
->gadget
.speed
= USB_SPEED_FULL
;
1622 /* Set MAXP for all endpoints */
1623 for (i
= 0; i
< S3C2410_ENDPOINTS
; i
++) {
1624 udc_write(i
, S3C2410_UDC_INDEX_REG
);
1625 udc_write((dev
->ep
[i
].ep
.maxpacket
& 0x7ff) >> 3,
1626 S3C2410_UDC_MAXP_REG
);
1629 /* Set default power state */
1630 udc_write(DEFAULT_POWER_STATE
, S3C2410_UDC_PWR_REG
);
1632 /* Enable reset and suspend interrupt interrupts */
1633 udc_write(S3C2410_UDC_USBINT_RESET
| S3C2410_UDC_USBINT_SUSPEND
,
1634 S3C2410_UDC_USB_INT_EN_REG
);
1636 /* Enable ep0 interrupt */
1637 udc_write(S3C2410_UDC_INT_EP0
, S3C2410_UDC_EP_INT_EN_REG
);
1639 /* time to say "hello, world" */
1640 s3c2410_udc_command(S3C2410_UDC_P_ENABLE
);
1643 static int s3c2410_udc_start(struct usb_gadget
*g
,
1644 struct usb_gadget_driver
*driver
)
1646 struct s3c2410_udc
*udc
= to_s3c2410(g
);
1648 dprintk(DEBUG_NORMAL
, "%s() '%s'\n", __func__
, driver
->driver
.name
);
1650 /* Hook the driver */
1651 udc
->driver
= driver
;
1654 s3c2410_udc_enable(udc
);
1659 static int s3c2410_udc_stop(struct usb_gadget
*g
)
1661 struct s3c2410_udc
*udc
= to_s3c2410(g
);
1666 s3c2410_udc_disable(udc
);
1671 /*---------------------------------------------------------------------------*/
1672 static struct s3c2410_udc memory
= {
1674 .ops
= &s3c2410_ops
,
1675 .ep0
= &memory
.ep
[0].ep
,
1676 .name
= gadget_name
,
1678 .init_name
= "gadget",
1682 /* control endpoint */
1687 .ops
= &s3c2410_ep_ops
,
1688 .maxpacket
= EP0_FIFO_SIZE
,
1689 .caps
= USB_EP_CAPS(USB_EP_CAPS_TYPE_CONTROL
,
1690 USB_EP_CAPS_DIR_ALL
),
1695 /* first group of endpoints */
1700 .ops
= &s3c2410_ep_ops
,
1701 .maxpacket
= EP_FIFO_SIZE
,
1702 .caps
= USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK
,
1703 USB_EP_CAPS_DIR_ALL
),
1706 .fifo_size
= EP_FIFO_SIZE
,
1707 .bEndpointAddress
= 1,
1708 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1714 .ops
= &s3c2410_ep_ops
,
1715 .maxpacket
= EP_FIFO_SIZE
,
1716 .caps
= USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK
,
1717 USB_EP_CAPS_DIR_ALL
),
1720 .fifo_size
= EP_FIFO_SIZE
,
1721 .bEndpointAddress
= 2,
1722 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1728 .ops
= &s3c2410_ep_ops
,
1729 .maxpacket
= EP_FIFO_SIZE
,
1730 .caps
= USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK
,
1731 USB_EP_CAPS_DIR_ALL
),
1734 .fifo_size
= EP_FIFO_SIZE
,
1735 .bEndpointAddress
= 3,
1736 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1742 .ops
= &s3c2410_ep_ops
,
1743 .maxpacket
= EP_FIFO_SIZE
,
1744 .caps
= USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK
,
1745 USB_EP_CAPS_DIR_ALL
),
1748 .fifo_size
= EP_FIFO_SIZE
,
1749 .bEndpointAddress
= 4,
1750 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1756 * probe - binds to the platform device
1758 static int s3c2410_udc_probe(struct platform_device
*pdev
)
1760 struct s3c2410_udc
*udc
= &memory
;
1761 struct device
*dev
= &pdev
->dev
;
1765 dev_dbg(dev
, "%s()\n", __func__
);
1767 usb_bus_clock
= clk_get(NULL
, "usb-bus-gadget");
1768 if (IS_ERR(usb_bus_clock
)) {
1769 dev_err(dev
, "failed to get usb bus clock source\n");
1770 return PTR_ERR(usb_bus_clock
);
1773 clk_prepare_enable(usb_bus_clock
);
1775 udc_clock
= clk_get(NULL
, "usb-device");
1776 if (IS_ERR(udc_clock
)) {
1777 dev_err(dev
, "failed to get udc clock source\n");
1778 return PTR_ERR(udc_clock
);
1781 clk_prepare_enable(udc_clock
);
1785 dev_dbg(dev
, "got and enabled clocks\n");
1787 if (strncmp(pdev
->name
, "s3c2440", 7) == 0) {
1788 dev_info(dev
, "S3C2440: increasing FIFO to 128 bytes\n");
1789 memory
.ep
[1].fifo_size
= S3C2440_EP_FIFO_SIZE
;
1790 memory
.ep
[2].fifo_size
= S3C2440_EP_FIFO_SIZE
;
1791 memory
.ep
[3].fifo_size
= S3C2440_EP_FIFO_SIZE
;
1792 memory
.ep
[4].fifo_size
= S3C2440_EP_FIFO_SIZE
;
1795 spin_lock_init(&udc
->lock
);
1796 udc_info
= dev_get_platdata(&pdev
->dev
);
1798 rsrc_start
= S3C2410_PA_USBDEV
;
1799 rsrc_len
= S3C24XX_SZ_USBDEV
;
1801 if (!request_mem_region(rsrc_start
, rsrc_len
, gadget_name
))
1804 base_addr
= ioremap(rsrc_start
, rsrc_len
);
1810 the_controller
= udc
;
1811 platform_set_drvdata(pdev
, udc
);
1813 s3c2410_udc_disable(udc
);
1814 s3c2410_udc_reinit(udc
);
1816 /* irq setup after old hardware state is cleaned up */
1817 retval
= request_irq(IRQ_USBD
, s3c2410_udc_irq
,
1818 0, gadget_name
, udc
);
1821 dev_err(dev
, "cannot get irq %i, err %d\n", IRQ_USBD
, retval
);
1826 dev_dbg(dev
, "got irq %i\n", IRQ_USBD
);
1828 if (udc_info
&& udc_info
->vbus_pin
> 0) {
1829 retval
= gpio_request(udc_info
->vbus_pin
, "udc vbus");
1831 dev_err(dev
, "cannot claim vbus pin\n");
1835 irq
= gpio_to_irq(udc_info
->vbus_pin
);
1837 dev_err(dev
, "no irq for gpio vbus pin\n");
1839 goto err_gpio_claim
;
1842 retval
= request_irq(irq
, s3c2410_udc_vbus_irq
,
1844 | IRQF_TRIGGER_FALLING
| IRQF_SHARED
,
1848 dev_err(dev
, "can't get vbus irq %d, err %d\n",
1851 goto err_gpio_claim
;
1854 dev_dbg(dev
, "got irq %i\n", irq
);
1859 if (udc_info
&& !udc_info
->udc_command
&&
1860 gpio_is_valid(udc_info
->pullup_pin
)) {
1862 retval
= gpio_request_one(udc_info
->pullup_pin
,
1863 udc_info
->vbus_pin_inverted
?
1864 GPIOF_OUT_INIT_HIGH
: GPIOF_OUT_INIT_LOW
,
1870 retval
= usb_add_gadget_udc(&pdev
->dev
, &udc
->gadget
);
1874 udc
->regs_info
= debugfs_create_file("registers", S_IRUGO
,
1875 s3c2410_udc_debugfs_root
, udc
,
1876 &s3c2410_udc_debugfs_fops
);
1878 dev_dbg(dev
, "probe ok\n");
1883 if (udc_info
&& !udc_info
->udc_command
&&
1884 gpio_is_valid(udc_info
->pullup_pin
))
1885 gpio_free(udc_info
->pullup_pin
);
1887 if (udc_info
&& udc_info
->vbus_pin
> 0)
1888 free_irq(gpio_to_irq(udc_info
->vbus_pin
), udc
);
1890 if (udc_info
&& udc_info
->vbus_pin
> 0)
1891 gpio_free(udc_info
->vbus_pin
);
1893 free_irq(IRQ_USBD
, udc
);
1897 release_mem_region(rsrc_start
, rsrc_len
);
1903 * s3c2410_udc_remove
1905 static int s3c2410_udc_remove(struct platform_device
*pdev
)
1907 struct s3c2410_udc
*udc
= platform_get_drvdata(pdev
);
1910 dev_dbg(&pdev
->dev
, "%s()\n", __func__
);
1915 usb_del_gadget_udc(&udc
->gadget
);
1916 debugfs_remove(udc
->regs_info
);
1918 if (udc_info
&& !udc_info
->udc_command
&&
1919 gpio_is_valid(udc_info
->pullup_pin
))
1920 gpio_free(udc_info
->pullup_pin
);
1922 if (udc_info
&& udc_info
->vbus_pin
> 0) {
1923 irq
= gpio_to_irq(udc_info
->vbus_pin
);
1927 free_irq(IRQ_USBD
, udc
);
1930 release_mem_region(rsrc_start
, rsrc_len
);
1932 if (!IS_ERR(udc_clock
) && udc_clock
!= NULL
) {
1933 clk_disable_unprepare(udc_clock
);
1938 if (!IS_ERR(usb_bus_clock
) && usb_bus_clock
!= NULL
) {
1939 clk_disable_unprepare(usb_bus_clock
);
1940 clk_put(usb_bus_clock
);
1941 usb_bus_clock
= NULL
;
1944 dev_dbg(&pdev
->dev
, "%s: remove ok\n", __func__
);
1950 s3c2410_udc_suspend(struct platform_device
*pdev
, pm_message_t message
)
1952 s3c2410_udc_command(S3C2410_UDC_P_DISABLE
);
1957 static int s3c2410_udc_resume(struct platform_device
*pdev
)
1959 s3c2410_udc_command(S3C2410_UDC_P_ENABLE
);
1964 #define s3c2410_udc_suspend NULL
1965 #define s3c2410_udc_resume NULL
1968 static const struct platform_device_id s3c_udc_ids
[] = {
1969 { "s3c2410-usbgadget", },
1970 { "s3c2440-usbgadget", },
1973 MODULE_DEVICE_TABLE(platform
, s3c_udc_ids
);
1975 static struct platform_driver udc_driver_24x0
= {
1977 .name
= "s3c24x0-usbgadget",
1979 .probe
= s3c2410_udc_probe
,
1980 .remove
= s3c2410_udc_remove
,
1981 .suspend
= s3c2410_udc_suspend
,
1982 .resume
= s3c2410_udc_resume
,
1983 .id_table
= s3c_udc_ids
,
1986 static int __init
udc_init(void)
1990 dprintk(DEBUG_NORMAL
, "%s\n", gadget_name
);
1992 s3c2410_udc_debugfs_root
= debugfs_create_dir(gadget_name
, NULL
);
1994 retval
= platform_driver_register(&udc_driver_24x0
);
2001 debugfs_remove(s3c2410_udc_debugfs_root
);
2005 static void __exit
udc_exit(void)
2007 platform_driver_unregister(&udc_driver_24x0
);
2008 debugfs_remove_recursive(s3c2410_udc_debugfs_root
);
2011 module_init(udc_init
);
2012 module_exit(udc_exit
);
2014 MODULE_AUTHOR(DRIVER_AUTHOR
);
2015 MODULE_DESCRIPTION(DRIVER_DESC
);
2016 MODULE_LICENSE("GPL");