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>
40 #include <linux/platform_data/usb-s3c2410_udc.h>
42 #include "s3c2410_udc.h"
43 #include "s3c2410_udc_regs.h"
45 #define DRIVER_DESC "S3C2410 USB Device Controller Gadget"
46 #define DRIVER_AUTHOR "Herbert Pötzl <herbert@13thfloor.at>, " \
47 "Arnaud Patard <arnaud.patard@rtp-net.org>"
49 static const char gadget_name
[] = "s3c2410_udc";
50 static const char driver_desc
[] = DRIVER_DESC
;
52 static struct s3c2410_udc
*the_controller
;
53 static struct clk
*udc_clock
;
54 static struct clk
*usb_bus_clock
;
55 static void __iomem
*base_addr
;
57 static struct dentry
*s3c2410_udc_debugfs_root
;
59 static inline u32
udc_read(u32 reg
)
61 return readb(base_addr
+ reg
);
64 static inline void udc_write(u32 value
, u32 reg
)
66 writeb(value
, base_addr
+ reg
);
69 static inline void udc_writeb(void __iomem
*base
, u32 value
, u32 reg
)
71 writeb(value
, base
+ reg
);
74 static struct s3c2410_udc_mach_info
*udc_info
;
76 /*************************** DEBUG FUNCTION ***************************/
77 #define DEBUG_NORMAL 1
78 #define DEBUG_VERBOSE 2
80 #ifdef CONFIG_USB_S3C2410_DEBUG
81 #define USB_S3C2410_DEBUG_LEVEL 0
83 static uint32_t s3c2410_ticks
= 0;
86 static void dprintk(int level
, const char *fmt
, ...)
88 static long prevticks
;
89 static int invocation
;
93 if (level
> USB_S3C2410_DEBUG_LEVEL
)
101 if (s3c2410_ticks
!= prevticks
) {
102 prevticks
= s3c2410_ticks
;
106 pr_debug("%1lu.%02d USB: %pV", prevticks
, invocation
++, &vaf
);
112 static void dprintk(int level
, const char *fmt
, ...)
117 static int s3c2410_udc_debugfs_show(struct seq_file
*m
, void *p
)
119 u32 addr_reg
, pwr_reg
, ep_int_reg
, usb_int_reg
;
120 u32 ep_int_en_reg
, usb_int_en_reg
, ep0_csr
;
121 u32 ep1_i_csr1
, ep1_i_csr2
, ep1_o_csr1
, ep1_o_csr2
;
122 u32 ep2_i_csr1
, ep2_i_csr2
, ep2_o_csr1
, ep2_o_csr2
;
124 addr_reg
= udc_read(S3C2410_UDC_FUNC_ADDR_REG
);
125 pwr_reg
= udc_read(S3C2410_UDC_PWR_REG
);
126 ep_int_reg
= udc_read(S3C2410_UDC_EP_INT_REG
);
127 usb_int_reg
= udc_read(S3C2410_UDC_USB_INT_REG
);
128 ep_int_en_reg
= udc_read(S3C2410_UDC_EP_INT_EN_REG
);
129 usb_int_en_reg
= udc_read(S3C2410_UDC_USB_INT_EN_REG
);
130 udc_write(0, S3C2410_UDC_INDEX_REG
);
131 ep0_csr
= udc_read(S3C2410_UDC_IN_CSR1_REG
);
132 udc_write(1, S3C2410_UDC_INDEX_REG
);
133 ep1_i_csr1
= udc_read(S3C2410_UDC_IN_CSR1_REG
);
134 ep1_i_csr2
= udc_read(S3C2410_UDC_IN_CSR2_REG
);
135 ep1_o_csr1
= udc_read(S3C2410_UDC_IN_CSR1_REG
);
136 ep1_o_csr2
= udc_read(S3C2410_UDC_IN_CSR2_REG
);
137 udc_write(2, S3C2410_UDC_INDEX_REG
);
138 ep2_i_csr1
= udc_read(S3C2410_UDC_IN_CSR1_REG
);
139 ep2_i_csr2
= udc_read(S3C2410_UDC_IN_CSR2_REG
);
140 ep2_o_csr1
= udc_read(S3C2410_UDC_IN_CSR1_REG
);
141 ep2_o_csr2
= udc_read(S3C2410_UDC_IN_CSR2_REG
);
143 seq_printf(m
, "FUNC_ADDR_REG : 0x%04X\n"
145 "EP_INT_REG : 0x%04X\n"
146 "USB_INT_REG : 0x%04X\n"
147 "EP_INT_EN_REG : 0x%04X\n"
148 "USB_INT_EN_REG : 0x%04X\n"
150 "EP1_I_CSR1 : 0x%04X\n"
151 "EP1_I_CSR2 : 0x%04X\n"
152 "EP1_O_CSR1 : 0x%04X\n"
153 "EP1_O_CSR2 : 0x%04X\n"
154 "EP2_I_CSR1 : 0x%04X\n"
155 "EP2_I_CSR2 : 0x%04X\n"
156 "EP2_O_CSR1 : 0x%04X\n"
157 "EP2_O_CSR2 : 0x%04X\n",
158 addr_reg
, pwr_reg
, ep_int_reg
, usb_int_reg
,
159 ep_int_en_reg
, usb_int_en_reg
, ep0_csr
,
160 ep1_i_csr1
, ep1_i_csr2
, ep1_o_csr1
, ep1_o_csr2
,
161 ep2_i_csr1
, ep2_i_csr2
, ep2_o_csr1
, ep2_o_csr2
166 DEFINE_SHOW_ATTRIBUTE(s3c2410_udc_debugfs
);
170 static inline void s3c2410_udc_clear_ep0_opr(void __iomem
*base
)
172 udc_writeb(base
, S3C2410_UDC_INDEX_EP0
, S3C2410_UDC_INDEX_REG
);
173 udc_writeb(base
, S3C2410_UDC_EP0_CSR_SOPKTRDY
,
174 S3C2410_UDC_EP0_CSR_REG
);
177 static inline void s3c2410_udc_clear_ep0_sst(void __iomem
*base
)
179 udc_writeb(base
, S3C2410_UDC_INDEX_EP0
, S3C2410_UDC_INDEX_REG
);
180 writeb(0x00, base
+ S3C2410_UDC_EP0_CSR_REG
);
183 static inline void s3c2410_udc_clear_ep0_se(void __iomem
*base
)
185 udc_writeb(base
, S3C2410_UDC_INDEX_EP0
, S3C2410_UDC_INDEX_REG
);
186 udc_writeb(base
, S3C2410_UDC_EP0_CSR_SSE
, S3C2410_UDC_EP0_CSR_REG
);
189 static inline void s3c2410_udc_set_ep0_ipr(void __iomem
*base
)
191 udc_writeb(base
, S3C2410_UDC_INDEX_EP0
, S3C2410_UDC_INDEX_REG
);
192 udc_writeb(base
, S3C2410_UDC_EP0_CSR_IPKRDY
, S3C2410_UDC_EP0_CSR_REG
);
195 static inline void s3c2410_udc_set_ep0_de(void __iomem
*base
)
197 udc_writeb(base
, S3C2410_UDC_INDEX_EP0
, S3C2410_UDC_INDEX_REG
);
198 udc_writeb(base
, S3C2410_UDC_EP0_CSR_DE
, S3C2410_UDC_EP0_CSR_REG
);
201 static inline void s3c2410_udc_set_ep0_ss(void __iomem
*b
)
203 udc_writeb(b
, S3C2410_UDC_INDEX_EP0
, S3C2410_UDC_INDEX_REG
);
204 udc_writeb(b
, S3C2410_UDC_EP0_CSR_SENDSTL
, S3C2410_UDC_EP0_CSR_REG
);
207 static inline void s3c2410_udc_set_ep0_de_out(void __iomem
*base
)
209 udc_writeb(base
, S3C2410_UDC_INDEX_EP0
, S3C2410_UDC_INDEX_REG
);
211 udc_writeb(base
, (S3C2410_UDC_EP0_CSR_SOPKTRDY
212 | S3C2410_UDC_EP0_CSR_DE
),
213 S3C2410_UDC_EP0_CSR_REG
);
216 static inline void s3c2410_udc_set_ep0_de_in(void __iomem
*base
)
218 udc_writeb(base
, S3C2410_UDC_INDEX_EP0
, S3C2410_UDC_INDEX_REG
);
219 udc_writeb(base
, (S3C2410_UDC_EP0_CSR_IPKRDY
220 | S3C2410_UDC_EP0_CSR_DE
),
221 S3C2410_UDC_EP0_CSR_REG
);
224 /*------------------------- I/O ----------------------------------*/
229 static void s3c2410_udc_done(struct s3c2410_ep
*ep
,
230 struct s3c2410_request
*req
, int status
)
232 unsigned halted
= ep
->halted
;
234 list_del_init(&req
->queue
);
236 if (likely(req
->req
.status
== -EINPROGRESS
))
237 req
->req
.status
= status
;
239 status
= req
->req
.status
;
242 usb_gadget_giveback_request(&ep
->ep
, &req
->req
);
246 static void s3c2410_udc_nuke(struct s3c2410_udc
*udc
,
247 struct s3c2410_ep
*ep
, int status
)
249 while (!list_empty(&ep
->queue
)) {
250 struct s3c2410_request
*req
;
251 req
= list_entry(ep
->queue
.next
, struct s3c2410_request
,
253 s3c2410_udc_done(ep
, req
, status
);
257 static inline int s3c2410_udc_fifo_count_out(void)
261 tmp
= udc_read(S3C2410_UDC_OUT_FIFO_CNT2_REG
) << 8;
262 tmp
|= udc_read(S3C2410_UDC_OUT_FIFO_CNT1_REG
);
267 * s3c2410_udc_write_packet
269 static inline int s3c2410_udc_write_packet(int fifo
,
270 struct s3c2410_request
*req
,
273 unsigned len
= min(req
->req
.length
- req
->req
.actual
, max
);
274 u8
*buf
= req
->req
.buf
+ req
->req
.actual
;
278 dprintk(DEBUG_VERBOSE
, "%s %d %d %d %d\n", __func__
,
279 req
->req
.actual
, req
->req
.length
, len
, req
->req
.actual
+ len
);
281 req
->req
.actual
+= len
;
284 writesb(base_addr
+ fifo
, buf
, len
);
289 * s3c2410_udc_write_fifo
291 * return: 0 = still running, 1 = completed, negative = errno
293 static int s3c2410_udc_write_fifo(struct s3c2410_ep
*ep
,
294 struct s3c2410_request
*req
)
302 idx
= ep
->bEndpointAddress
& 0x7F;
308 fifo_reg
= S3C2410_UDC_EP0_FIFO_REG
;
311 fifo_reg
= S3C2410_UDC_EP1_FIFO_REG
;
314 fifo_reg
= S3C2410_UDC_EP2_FIFO_REG
;
317 fifo_reg
= S3C2410_UDC_EP3_FIFO_REG
;
320 fifo_reg
= S3C2410_UDC_EP4_FIFO_REG
;
324 count
= s3c2410_udc_write_packet(fifo_reg
, req
, ep
->ep
.maxpacket
);
326 /* last packet is often short (sometimes a zlp) */
327 if (count
!= ep
->ep
.maxpacket
)
329 else if (req
->req
.length
!= req
->req
.actual
|| req
->req
.zero
)
334 /* Only ep0 debug messages are interesting */
336 dprintk(DEBUG_NORMAL
,
337 "Written ep%d %d.%d of %d b [last %d,z %d]\n",
338 idx
, count
, req
->req
.actual
, req
->req
.length
,
339 is_last
, req
->req
.zero
);
342 /* The order is important. It prevents sending 2 packets
343 * at the same time */
346 /* Reset signal => no need to say 'data sent' */
347 if (!(udc_read(S3C2410_UDC_USB_INT_REG
)
348 & S3C2410_UDC_USBINT_RESET
))
349 s3c2410_udc_set_ep0_de_in(base_addr
);
350 ep
->dev
->ep0state
= EP0_IDLE
;
352 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
353 ep_csr
= udc_read(S3C2410_UDC_IN_CSR1_REG
);
354 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
355 udc_write(ep_csr
| S3C2410_UDC_ICSR1_PKTRDY
,
356 S3C2410_UDC_IN_CSR1_REG
);
359 s3c2410_udc_done(ep
, req
, 0);
363 /* Reset signal => no need to say 'data sent' */
364 if (!(udc_read(S3C2410_UDC_USB_INT_REG
)
365 & S3C2410_UDC_USBINT_RESET
))
366 s3c2410_udc_set_ep0_ipr(base_addr
);
368 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
369 ep_csr
= udc_read(S3C2410_UDC_IN_CSR1_REG
);
370 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
371 udc_write(ep_csr
| S3C2410_UDC_ICSR1_PKTRDY
,
372 S3C2410_UDC_IN_CSR1_REG
);
379 static inline int s3c2410_udc_read_packet(int fifo
, u8
*buf
,
380 struct s3c2410_request
*req
, unsigned avail
)
384 len
= min(req
->req
.length
- req
->req
.actual
, avail
);
385 req
->req
.actual
+= len
;
387 readsb(fifo
+ base_addr
, buf
, len
);
392 * return: 0 = still running, 1 = queue empty, negative = errno
394 static int s3c2410_udc_read_fifo(struct s3c2410_ep
*ep
,
395 struct s3c2410_request
*req
)
399 unsigned bufferspace
;
406 idx
= ep
->bEndpointAddress
& 0x7F;
413 fifo_reg
= S3C2410_UDC_EP0_FIFO_REG
;
416 fifo_reg
= S3C2410_UDC_EP1_FIFO_REG
;
419 fifo_reg
= S3C2410_UDC_EP2_FIFO_REG
;
422 fifo_reg
= S3C2410_UDC_EP3_FIFO_REG
;
425 fifo_reg
= S3C2410_UDC_EP4_FIFO_REG
;
429 if (!req
->req
.length
)
432 buf
= req
->req
.buf
+ req
->req
.actual
;
433 bufferspace
= req
->req
.length
- req
->req
.actual
;
435 dprintk(DEBUG_NORMAL
, "%s: buffer full!\n", __func__
);
439 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
441 fifo_count
= s3c2410_udc_fifo_count_out();
442 dprintk(DEBUG_NORMAL
, "%s fifo count : %d\n", __func__
, fifo_count
);
444 if (fifo_count
> ep
->ep
.maxpacket
)
445 avail
= ep
->ep
.maxpacket
;
449 fifo_count
= s3c2410_udc_read_packet(fifo_reg
, buf
, req
, avail
);
451 /* checking this with ep0 is not accurate as we already
452 * read a control request
454 if (idx
!= 0 && fifo_count
< ep
->ep
.maxpacket
) {
456 /* overflowed this request? flush extra data */
457 if (fifo_count
!= avail
)
458 req
->req
.status
= -EOVERFLOW
;
460 is_last
= (req
->req
.length
<= req
->req
.actual
) ? 1 : 0;
463 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
464 fifo_count
= s3c2410_udc_fifo_count_out();
466 /* Only ep0 debug messages are interesting */
468 dprintk(DEBUG_VERBOSE
, "%s fifo count : %d [last %d]\n",
469 __func__
, fifo_count
, is_last
);
473 s3c2410_udc_set_ep0_de_out(base_addr
);
474 ep
->dev
->ep0state
= EP0_IDLE
;
476 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
477 ep_csr
= udc_read(S3C2410_UDC_OUT_CSR1_REG
);
478 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
479 udc_write(ep_csr
& ~S3C2410_UDC_OCSR1_PKTRDY
,
480 S3C2410_UDC_OUT_CSR1_REG
);
483 s3c2410_udc_done(ep
, req
, 0);
486 s3c2410_udc_clear_ep0_opr(base_addr
);
488 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
489 ep_csr
= udc_read(S3C2410_UDC_OUT_CSR1_REG
);
490 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
491 udc_write(ep_csr
& ~S3C2410_UDC_OCSR1_PKTRDY
,
492 S3C2410_UDC_OUT_CSR1_REG
);
499 static int s3c2410_udc_read_fifo_crq(struct usb_ctrlrequest
*crq
)
501 unsigned char *outbuf
= (unsigned char *)crq
;
504 udc_write(0, S3C2410_UDC_INDEX_REG
);
506 bytes_read
= s3c2410_udc_fifo_count_out();
508 dprintk(DEBUG_NORMAL
, "%s: fifo_count=%d\n", __func__
, bytes_read
);
510 if (bytes_read
> sizeof(struct usb_ctrlrequest
))
511 bytes_read
= sizeof(struct usb_ctrlrequest
);
513 readsb(S3C2410_UDC_EP0_FIFO_REG
+ base_addr
, outbuf
, bytes_read
);
515 dprintk(DEBUG_VERBOSE
, "%s: len=%d %02x:%02x {%x,%x,%x}\n", __func__
,
516 bytes_read
, crq
->bRequest
, crq
->bRequestType
,
517 crq
->wValue
, crq
->wIndex
, crq
->wLength
);
522 static int s3c2410_udc_get_status(struct s3c2410_udc
*dev
,
523 struct usb_ctrlrequest
*crq
)
526 u8 ep_num
= crq
->wIndex
& 0x7F;
527 u8 is_in
= crq
->wIndex
& USB_DIR_IN
;
529 switch (crq
->bRequestType
& USB_RECIP_MASK
) {
530 case USB_RECIP_INTERFACE
:
533 case USB_RECIP_DEVICE
:
534 status
= dev
->devstatus
;
537 case USB_RECIP_ENDPOINT
:
538 if (ep_num
> 4 || crq
->wLength
> 2)
542 udc_write(0, S3C2410_UDC_INDEX_REG
);
543 status
= udc_read(S3C2410_UDC_IN_CSR1_REG
);
544 status
= status
& S3C2410_UDC_EP0_CSR_SENDSTL
;
546 udc_write(ep_num
, S3C2410_UDC_INDEX_REG
);
548 status
= udc_read(S3C2410_UDC_IN_CSR1_REG
);
549 status
= status
& S3C2410_UDC_ICSR1_SENDSTL
;
551 status
= udc_read(S3C2410_UDC_OUT_CSR1_REG
);
552 status
= status
& S3C2410_UDC_OCSR1_SENDSTL
;
556 status
= status
? 1 : 0;
563 /* Seems to be needed to get it working. ouch :( */
565 udc_write(status
& 0xFF, S3C2410_UDC_EP0_FIFO_REG
);
566 udc_write(status
>> 8, S3C2410_UDC_EP0_FIFO_REG
);
567 s3c2410_udc_set_ep0_de_in(base_addr
);
571 /*------------------------- usb state machine -------------------------------*/
572 static int s3c2410_udc_set_halt(struct usb_ep
*_ep
, int value
);
574 static void s3c2410_udc_handle_ep0_idle(struct s3c2410_udc
*dev
,
575 struct s3c2410_ep
*ep
,
576 struct usb_ctrlrequest
*crq
,
581 /* start control request? */
582 if (!(ep0csr
& S3C2410_UDC_EP0_CSR_OPKRDY
))
585 s3c2410_udc_nuke(dev
, ep
, -EPROTO
);
587 len
= s3c2410_udc_read_fifo_crq(crq
);
588 if (len
!= sizeof(*crq
)) {
589 dprintk(DEBUG_NORMAL
, "setup begin: fifo READ ERROR"
590 " wanted %d bytes got %d. Stalling out...\n",
592 s3c2410_udc_set_ep0_ss(base_addr
);
596 dprintk(DEBUG_NORMAL
, "bRequest = %d bRequestType %d wLength = %d\n",
597 crq
->bRequest
, crq
->bRequestType
, crq
->wLength
);
599 /* cope with automagic for some standard requests. */
600 dev
->req_std
= (crq
->bRequestType
& USB_TYPE_MASK
)
601 == USB_TYPE_STANDARD
;
603 dev
->req_pending
= 1;
605 switch (crq
->bRequest
) {
606 case USB_REQ_SET_CONFIGURATION
:
607 dprintk(DEBUG_NORMAL
, "USB_REQ_SET_CONFIGURATION ...\n");
609 if (crq
->bRequestType
== USB_RECIP_DEVICE
) {
611 s3c2410_udc_set_ep0_de_out(base_addr
);
615 case USB_REQ_SET_INTERFACE
:
616 dprintk(DEBUG_NORMAL
, "USB_REQ_SET_INTERFACE ...\n");
618 if (crq
->bRequestType
== USB_RECIP_INTERFACE
) {
620 s3c2410_udc_set_ep0_de_out(base_addr
);
624 case USB_REQ_SET_ADDRESS
:
625 dprintk(DEBUG_NORMAL
, "USB_REQ_SET_ADDRESS ...\n");
627 if (crq
->bRequestType
== USB_RECIP_DEVICE
) {
628 tmp
= crq
->wValue
& 0x7F;
630 udc_write((tmp
| S3C2410_UDC_FUNCADDR_UPDATE
),
631 S3C2410_UDC_FUNC_ADDR_REG
);
632 s3c2410_udc_set_ep0_de_out(base_addr
);
637 case USB_REQ_GET_STATUS
:
638 dprintk(DEBUG_NORMAL
, "USB_REQ_GET_STATUS ...\n");
639 s3c2410_udc_clear_ep0_opr(base_addr
);
642 if (!s3c2410_udc_get_status(dev
, crq
))
647 case USB_REQ_CLEAR_FEATURE
:
648 s3c2410_udc_clear_ep0_opr(base_addr
);
650 if (crq
->bRequestType
!= USB_RECIP_ENDPOINT
)
653 if (crq
->wValue
!= USB_ENDPOINT_HALT
|| crq
->wLength
!= 0)
656 s3c2410_udc_set_halt(&dev
->ep
[crq
->wIndex
& 0x7f].ep
, 0);
657 s3c2410_udc_set_ep0_de_out(base_addr
);
660 case USB_REQ_SET_FEATURE
:
661 s3c2410_udc_clear_ep0_opr(base_addr
);
663 if (crq
->bRequestType
!= USB_RECIP_ENDPOINT
)
666 if (crq
->wValue
!= USB_ENDPOINT_HALT
|| crq
->wLength
!= 0)
669 s3c2410_udc_set_halt(&dev
->ep
[crq
->wIndex
& 0x7f].ep
, 1);
670 s3c2410_udc_set_ep0_de_out(base_addr
);
674 s3c2410_udc_clear_ep0_opr(base_addr
);
678 if (crq
->bRequestType
& USB_DIR_IN
)
679 dev
->ep0state
= EP0_IN_DATA_PHASE
;
681 dev
->ep0state
= EP0_OUT_DATA_PHASE
;
686 /* deliver the request to the gadget driver */
687 ret
= dev
->driver
->setup(&dev
->gadget
, crq
);
689 if (dev
->req_config
) {
690 dprintk(DEBUG_NORMAL
, "config change %02x fail %d?\n",
695 if (ret
== -EOPNOTSUPP
)
696 dprintk(DEBUG_NORMAL
, "Operation not supported\n");
698 dprintk(DEBUG_NORMAL
,
699 "dev->driver->setup failed. (%d)\n", ret
);
702 s3c2410_udc_set_ep0_ss(base_addr
);
703 s3c2410_udc_set_ep0_de_out(base_addr
);
704 dev
->ep0state
= EP0_IDLE
;
705 /* deferred i/o == no response yet */
706 } else if (dev
->req_pending
) {
707 dprintk(DEBUG_VERBOSE
, "dev->req_pending... what now?\n");
708 dev
->req_pending
= 0;
711 dprintk(DEBUG_VERBOSE
, "ep0state %s\n", ep0states
[dev
->ep0state
]);
714 static void s3c2410_udc_handle_ep0(struct s3c2410_udc
*dev
)
717 struct s3c2410_ep
*ep
= &dev
->ep
[0];
718 struct s3c2410_request
*req
;
719 struct usb_ctrlrequest crq
;
721 if (list_empty(&ep
->queue
))
724 req
= list_entry(ep
->queue
.next
, struct s3c2410_request
, queue
);
726 /* We make the assumption that S3C2410_UDC_IN_CSR1_REG equal to
727 * S3C2410_UDC_EP0_CSR_REG when index is zero */
729 udc_write(0, S3C2410_UDC_INDEX_REG
);
730 ep0csr
= udc_read(S3C2410_UDC_IN_CSR1_REG
);
732 dprintk(DEBUG_NORMAL
, "ep0csr %x ep0state %s\n",
733 ep0csr
, ep0states
[dev
->ep0state
]);
735 /* clear stall status */
736 if (ep0csr
& S3C2410_UDC_EP0_CSR_SENTSTL
) {
737 s3c2410_udc_nuke(dev
, ep
, -EPIPE
);
738 dprintk(DEBUG_NORMAL
, "... clear SENT_STALL ...\n");
739 s3c2410_udc_clear_ep0_sst(base_addr
);
740 dev
->ep0state
= EP0_IDLE
;
744 /* clear setup end */
745 if (ep0csr
& S3C2410_UDC_EP0_CSR_SE
) {
746 dprintk(DEBUG_NORMAL
, "... serviced SETUP_END ...\n");
747 s3c2410_udc_nuke(dev
, ep
, 0);
748 s3c2410_udc_clear_ep0_se(base_addr
);
749 dev
->ep0state
= EP0_IDLE
;
752 switch (dev
->ep0state
) {
754 s3c2410_udc_handle_ep0_idle(dev
, ep
, &crq
, ep0csr
);
757 case EP0_IN_DATA_PHASE
: /* GET_DESCRIPTOR etc */
758 dprintk(DEBUG_NORMAL
, "EP0_IN_DATA_PHASE ... what now?\n");
759 if (!(ep0csr
& S3C2410_UDC_EP0_CSR_IPKRDY
) && req
)
760 s3c2410_udc_write_fifo(ep
, req
);
763 case EP0_OUT_DATA_PHASE
: /* SET_DESCRIPTOR etc */
764 dprintk(DEBUG_NORMAL
, "EP0_OUT_DATA_PHASE ... what now?\n");
765 if ((ep0csr
& S3C2410_UDC_EP0_CSR_OPKRDY
) && req
)
766 s3c2410_udc_read_fifo(ep
, req
);
770 dprintk(DEBUG_NORMAL
, "EP0_END_XFER ... what now?\n");
771 dev
->ep0state
= EP0_IDLE
;
775 dprintk(DEBUG_NORMAL
, "EP0_STALL ... what now?\n");
776 dev
->ep0state
= EP0_IDLE
;
782 * handle_ep - Manage I/O endpoints
785 static void s3c2410_udc_handle_ep(struct s3c2410_ep
*ep
)
787 struct s3c2410_request
*req
;
788 int is_in
= ep
->bEndpointAddress
& USB_DIR_IN
;
792 if (likely(!list_empty(&ep
->queue
)))
793 req
= list_entry(ep
->queue
.next
,
794 struct s3c2410_request
, queue
);
798 idx
= ep
->bEndpointAddress
& 0x7F;
801 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
802 ep_csr1
= udc_read(S3C2410_UDC_IN_CSR1_REG
);
803 dprintk(DEBUG_VERBOSE
, "ep%01d write csr:%02x %d\n",
804 idx
, ep_csr1
, req
? 1 : 0);
806 if (ep_csr1
& S3C2410_UDC_ICSR1_SENTSTL
) {
807 dprintk(DEBUG_VERBOSE
, "st\n");
808 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
809 udc_write(ep_csr1
& ~S3C2410_UDC_ICSR1_SENTSTL
,
810 S3C2410_UDC_IN_CSR1_REG
);
814 if (!(ep_csr1
& S3C2410_UDC_ICSR1_PKTRDY
) && req
)
815 s3c2410_udc_write_fifo(ep
, req
);
817 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
818 ep_csr1
= udc_read(S3C2410_UDC_OUT_CSR1_REG
);
819 dprintk(DEBUG_VERBOSE
, "ep%01d rd csr:%02x\n", idx
, ep_csr1
);
821 if (ep_csr1
& S3C2410_UDC_OCSR1_SENTSTL
) {
822 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
823 udc_write(ep_csr1
& ~S3C2410_UDC_OCSR1_SENTSTL
,
824 S3C2410_UDC_OUT_CSR1_REG
);
828 if ((ep_csr1
& S3C2410_UDC_OCSR1_PKTRDY
) && req
)
829 s3c2410_udc_read_fifo(ep
, req
);
834 * s3c2410_udc_irq - interrupt handler
836 static irqreturn_t
s3c2410_udc_irq(int dummy
, void *_dev
)
838 struct s3c2410_udc
*dev
= _dev
;
847 spin_lock_irqsave(&dev
->lock
, flags
);
849 /* Driver connected ? */
851 /* Clear interrupts */
852 udc_write(udc_read(S3C2410_UDC_USB_INT_REG
),
853 S3C2410_UDC_USB_INT_REG
);
854 udc_write(udc_read(S3C2410_UDC_EP_INT_REG
),
855 S3C2410_UDC_EP_INT_REG
);
859 idx
= udc_read(S3C2410_UDC_INDEX_REG
);
861 /* Read status registers */
862 usb_status
= udc_read(S3C2410_UDC_USB_INT_REG
);
863 usbd_status
= udc_read(S3C2410_UDC_EP_INT_REG
);
864 pwr_reg
= udc_read(S3C2410_UDC_PWR_REG
);
866 udc_writeb(base_addr
, S3C2410_UDC_INDEX_EP0
, S3C2410_UDC_INDEX_REG
);
867 ep0csr
= udc_read(S3C2410_UDC_IN_CSR1_REG
);
869 dprintk(DEBUG_NORMAL
, "usbs=%02x, usbds=%02x, pwr=%02x ep0csr=%02x\n",
870 usb_status
, usbd_status
, pwr_reg
, ep0csr
);
873 * Now, handle interrupts. There's two types :
874 * - Reset, Resume, Suspend coming -> usb_int_reg
879 if (usb_status
& S3C2410_UDC_USBINT_RESET
) {
880 /* two kind of reset :
881 * - reset start -> pwr reg = 8
882 * - reset end -> pwr reg = 0
884 dprintk(DEBUG_NORMAL
, "USB reset csr %x pwr %x\n",
887 dev
->gadget
.speed
= USB_SPEED_UNKNOWN
;
888 udc_write(0x00, S3C2410_UDC_INDEX_REG
);
889 udc_write((dev
->ep
[0].ep
.maxpacket
& 0x7ff) >> 3,
890 S3C2410_UDC_MAXP_REG
);
893 dev
->ep0state
= EP0_IDLE
;
894 dev
->gadget
.speed
= USB_SPEED_FULL
;
896 /* clear interrupt */
897 udc_write(S3C2410_UDC_USBINT_RESET
,
898 S3C2410_UDC_USB_INT_REG
);
900 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
901 spin_unlock_irqrestore(&dev
->lock
, flags
);
906 if (usb_status
& S3C2410_UDC_USBINT_RESUME
) {
907 dprintk(DEBUG_NORMAL
, "USB resume\n");
909 /* clear interrupt */
910 udc_write(S3C2410_UDC_USBINT_RESUME
,
911 S3C2410_UDC_USB_INT_REG
);
913 if (dev
->gadget
.speed
!= USB_SPEED_UNKNOWN
915 && dev
->driver
->resume
)
916 dev
->driver
->resume(&dev
->gadget
);
920 if (usb_status
& S3C2410_UDC_USBINT_SUSPEND
) {
921 dprintk(DEBUG_NORMAL
, "USB suspend\n");
923 /* clear interrupt */
924 udc_write(S3C2410_UDC_USBINT_SUSPEND
,
925 S3C2410_UDC_USB_INT_REG
);
927 if (dev
->gadget
.speed
!= USB_SPEED_UNKNOWN
929 && dev
->driver
->suspend
)
930 dev
->driver
->suspend(&dev
->gadget
);
932 dev
->ep0state
= EP0_IDLE
;
936 /* control traffic */
937 /* check on ep0csr != 0 is not a good idea as clearing in_pkt_ready
938 * generate an interrupt
940 if (usbd_status
& S3C2410_UDC_INT_EP0
) {
941 dprintk(DEBUG_VERBOSE
, "USB ep0 irq\n");
942 /* Clear the interrupt bit by setting it to 1 */
943 udc_write(S3C2410_UDC_INT_EP0
, S3C2410_UDC_EP_INT_REG
);
944 s3c2410_udc_handle_ep0(dev
);
947 /* endpoint data transfers */
948 for (i
= 1; i
< S3C2410_ENDPOINTS
; i
++) {
950 if (usbd_status
& tmp
) {
951 dprintk(DEBUG_VERBOSE
, "USB ep%d irq\n", i
);
953 /* Clear the interrupt bit by setting it to 1 */
954 udc_write(tmp
, S3C2410_UDC_EP_INT_REG
);
955 s3c2410_udc_handle_ep(&dev
->ep
[i
]);
959 /* what else causes this interrupt? a receive! who is it? */
960 if (!usb_status
&& !usbd_status
&& !pwr_reg
&& !ep0csr
) {
961 for (i
= 1; i
< S3C2410_ENDPOINTS
; i
++) {
962 idx2
= udc_read(S3C2410_UDC_INDEX_REG
);
963 udc_write(i
, S3C2410_UDC_INDEX_REG
);
965 if (udc_read(S3C2410_UDC_OUT_CSR1_REG
) & 0x1)
966 s3c2410_udc_handle_ep(&dev
->ep
[i
]);
969 udc_write(idx2
, S3C2410_UDC_INDEX_REG
);
973 dprintk(DEBUG_VERBOSE
, "irq: %d s3c2410_udc_done.\n", irq_usbd
);
975 /* Restore old index */
976 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
978 spin_unlock_irqrestore(&dev
->lock
, flags
);
982 /*------------------------- s3c2410_ep_ops ----------------------------------*/
984 static inline struct s3c2410_ep
*to_s3c2410_ep(struct usb_ep
*ep
)
986 return container_of(ep
, struct s3c2410_ep
, ep
);
989 static inline struct s3c2410_udc
*to_s3c2410_udc(struct usb_gadget
*gadget
)
991 return container_of(gadget
, struct s3c2410_udc
, gadget
);
994 static inline struct s3c2410_request
*to_s3c2410_req(struct usb_request
*req
)
996 return container_of(req
, struct s3c2410_request
, req
);
1000 * s3c2410_udc_ep_enable
1002 static int s3c2410_udc_ep_enable(struct usb_ep
*_ep
,
1003 const struct usb_endpoint_descriptor
*desc
)
1005 struct s3c2410_udc
*dev
;
1006 struct s3c2410_ep
*ep
;
1008 unsigned long flags
;
1012 ep
= to_s3c2410_ep(_ep
);
1015 || _ep
->name
== ep0name
1016 || desc
->bDescriptorType
!= USB_DT_ENDPOINT
)
1020 if (!dev
->driver
|| dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
1023 max
= usb_endpoint_maxp(desc
);
1025 local_irq_save(flags
);
1026 _ep
->maxpacket
= max
;
1029 ep
->bEndpointAddress
= desc
->bEndpointAddress
;
1031 /* set max packet */
1032 udc_write(ep
->num
, S3C2410_UDC_INDEX_REG
);
1033 udc_write(max
>> 3, S3C2410_UDC_MAXP_REG
);
1035 /* set type, direction, address; reset fifo counters */
1036 if (desc
->bEndpointAddress
& USB_DIR_IN
) {
1037 csr1
= S3C2410_UDC_ICSR1_FFLUSH
|S3C2410_UDC_ICSR1_CLRDT
;
1038 csr2
= S3C2410_UDC_ICSR2_MODEIN
|S3C2410_UDC_ICSR2_DMAIEN
;
1040 udc_write(ep
->num
, S3C2410_UDC_INDEX_REG
);
1041 udc_write(csr1
, S3C2410_UDC_IN_CSR1_REG
);
1042 udc_write(ep
->num
, S3C2410_UDC_INDEX_REG
);
1043 udc_write(csr2
, S3C2410_UDC_IN_CSR2_REG
);
1045 /* don't flush in fifo or it will cause endpoint interrupt */
1046 csr1
= S3C2410_UDC_ICSR1_CLRDT
;
1047 csr2
= S3C2410_UDC_ICSR2_DMAIEN
;
1049 udc_write(ep
->num
, S3C2410_UDC_INDEX_REG
);
1050 udc_write(csr1
, S3C2410_UDC_IN_CSR1_REG
);
1051 udc_write(ep
->num
, S3C2410_UDC_INDEX_REG
);
1052 udc_write(csr2
, S3C2410_UDC_IN_CSR2_REG
);
1054 csr1
= S3C2410_UDC_OCSR1_FFLUSH
| S3C2410_UDC_OCSR1_CLRDT
;
1055 csr2
= S3C2410_UDC_OCSR2_DMAIEN
;
1057 udc_write(ep
->num
, S3C2410_UDC_INDEX_REG
);
1058 udc_write(csr1
, S3C2410_UDC_OUT_CSR1_REG
);
1059 udc_write(ep
->num
, S3C2410_UDC_INDEX_REG
);
1060 udc_write(csr2
, S3C2410_UDC_OUT_CSR2_REG
);
1064 int_en_reg
= udc_read(S3C2410_UDC_EP_INT_EN_REG
);
1065 udc_write(int_en_reg
| (1 << ep
->num
), S3C2410_UDC_EP_INT_EN_REG
);
1067 /* print some debug message */
1068 tmp
= desc
->bEndpointAddress
;
1069 dprintk(DEBUG_NORMAL
, "enable %s(%d) ep%x%s-blk max %02x\n",
1070 _ep
->name
, ep
->num
, tmp
,
1071 desc
->bEndpointAddress
& USB_DIR_IN
? "in" : "out", max
);
1073 local_irq_restore(flags
);
1074 s3c2410_udc_set_halt(_ep
, 0);
1080 * s3c2410_udc_ep_disable
1082 static int s3c2410_udc_ep_disable(struct usb_ep
*_ep
)
1084 struct s3c2410_ep
*ep
= to_s3c2410_ep(_ep
);
1085 unsigned long flags
;
1088 if (!_ep
|| !ep
->ep
.desc
) {
1089 dprintk(DEBUG_NORMAL
, "%s not enabled\n",
1090 _ep
? ep
->ep
.name
: NULL
);
1094 local_irq_save(flags
);
1096 dprintk(DEBUG_NORMAL
, "ep_disable: %s\n", _ep
->name
);
1101 s3c2410_udc_nuke(ep
->dev
, ep
, -ESHUTDOWN
);
1104 int_en_reg
= udc_read(S3C2410_UDC_EP_INT_EN_REG
);
1105 udc_write(int_en_reg
& ~(1<<ep
->num
), S3C2410_UDC_EP_INT_EN_REG
);
1107 local_irq_restore(flags
);
1109 dprintk(DEBUG_NORMAL
, "%s disabled\n", _ep
->name
);
1115 * s3c2410_udc_alloc_request
1117 static struct usb_request
*
1118 s3c2410_udc_alloc_request(struct usb_ep
*_ep
, gfp_t mem_flags
)
1120 struct s3c2410_request
*req
;
1122 dprintk(DEBUG_VERBOSE
, "%s(%p,%d)\n", __func__
, _ep
, mem_flags
);
1127 req
= kzalloc(sizeof(struct s3c2410_request
), mem_flags
);
1131 INIT_LIST_HEAD(&req
->queue
);
1136 * s3c2410_udc_free_request
1139 s3c2410_udc_free_request(struct usb_ep
*_ep
, struct usb_request
*_req
)
1141 struct s3c2410_ep
*ep
= to_s3c2410_ep(_ep
);
1142 struct s3c2410_request
*req
= to_s3c2410_req(_req
);
1144 dprintk(DEBUG_VERBOSE
, "%s(%p,%p)\n", __func__
, _ep
, _req
);
1146 if (!ep
|| !_req
|| (!ep
->ep
.desc
&& _ep
->name
!= ep0name
))
1149 WARN_ON(!list_empty(&req
->queue
));
1156 static int s3c2410_udc_queue(struct usb_ep
*_ep
, struct usb_request
*_req
,
1159 struct s3c2410_request
*req
= to_s3c2410_req(_req
);
1160 struct s3c2410_ep
*ep
= to_s3c2410_ep(_ep
);
1161 struct s3c2410_udc
*dev
;
1164 unsigned long flags
;
1166 if (unlikely(!_ep
|| (!ep
->ep
.desc
&& ep
->ep
.name
!= ep0name
))) {
1167 dprintk(DEBUG_NORMAL
, "%s: invalid args\n", __func__
);
1172 if (unlikely(!dev
->driver
1173 || dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)) {
1177 local_irq_save(flags
);
1179 if (unlikely(!_req
|| !_req
->complete
1180 || !_req
->buf
|| !list_empty(&req
->queue
))) {
1182 dprintk(DEBUG_NORMAL
, "%s: 1 X X X\n", __func__
);
1184 dprintk(DEBUG_NORMAL
, "%s: 0 %01d %01d %01d\n",
1185 __func__
, !_req
->complete
, !_req
->buf
,
1186 !list_empty(&req
->queue
));
1189 local_irq_restore(flags
);
1193 _req
->status
= -EINPROGRESS
;
1196 dprintk(DEBUG_VERBOSE
, "%s: ep%x len %d\n",
1197 __func__
, ep
->bEndpointAddress
, _req
->length
);
1199 if (ep
->bEndpointAddress
) {
1200 udc_write(ep
->bEndpointAddress
& 0x7F, S3C2410_UDC_INDEX_REG
);
1202 ep_csr
= udc_read((ep
->bEndpointAddress
& USB_DIR_IN
)
1203 ? S3C2410_UDC_IN_CSR1_REG
1204 : S3C2410_UDC_OUT_CSR1_REG
);
1205 fifo_count
= s3c2410_udc_fifo_count_out();
1207 udc_write(0, S3C2410_UDC_INDEX_REG
);
1208 ep_csr
= udc_read(S3C2410_UDC_IN_CSR1_REG
);
1209 fifo_count
= s3c2410_udc_fifo_count_out();
1212 /* kickstart this i/o queue? */
1213 if (list_empty(&ep
->queue
) && !ep
->halted
) {
1214 if (ep
->bEndpointAddress
== 0 /* ep0 */) {
1215 switch (dev
->ep0state
) {
1216 case EP0_IN_DATA_PHASE
:
1217 if (!(ep_csr
&S3C2410_UDC_EP0_CSR_IPKRDY
)
1218 && s3c2410_udc_write_fifo(ep
,
1220 dev
->ep0state
= EP0_IDLE
;
1225 case EP0_OUT_DATA_PHASE
:
1227 || ((ep_csr
& S3C2410_UDC_OCSR1_PKTRDY
)
1228 && s3c2410_udc_read_fifo(ep
,
1230 dev
->ep0state
= EP0_IDLE
;
1236 local_irq_restore(flags
);
1239 } else if ((ep
->bEndpointAddress
& USB_DIR_IN
) != 0
1240 && (!(ep_csr
&S3C2410_UDC_OCSR1_PKTRDY
))
1241 && s3c2410_udc_write_fifo(ep
, req
)) {
1243 } else if ((ep_csr
& S3C2410_UDC_OCSR1_PKTRDY
)
1245 && s3c2410_udc_read_fifo(ep
, req
)) {
1250 /* pio or dma irq handler advances the queue. */
1252 list_add_tail(&req
->queue
, &ep
->queue
);
1254 local_irq_restore(flags
);
1256 dprintk(DEBUG_VERBOSE
, "%s ok\n", __func__
);
1261 * s3c2410_udc_dequeue
1263 static int s3c2410_udc_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
1265 struct s3c2410_ep
*ep
= to_s3c2410_ep(_ep
);
1266 int retval
= -EINVAL
;
1267 unsigned long flags
;
1268 struct s3c2410_request
*req
= NULL
;
1270 dprintk(DEBUG_VERBOSE
, "%s(%p,%p)\n", __func__
, _ep
, _req
);
1272 if (!the_controller
->driver
)
1278 local_irq_save(flags
);
1280 list_for_each_entry(req
, &ep
->queue
, queue
) {
1281 if (&req
->req
== _req
) {
1282 list_del_init(&req
->queue
);
1283 _req
->status
= -ECONNRESET
;
1290 dprintk(DEBUG_VERBOSE
,
1291 "dequeued req %p from %s, len %d buf %p\n",
1292 req
, _ep
->name
, _req
->length
, _req
->buf
);
1294 s3c2410_udc_done(ep
, req
, -ECONNRESET
);
1297 local_irq_restore(flags
);
1302 * s3c2410_udc_set_halt
1304 static int s3c2410_udc_set_halt(struct usb_ep
*_ep
, int value
)
1306 struct s3c2410_ep
*ep
= to_s3c2410_ep(_ep
);
1308 unsigned long flags
;
1311 if (unlikely(!_ep
|| (!ep
->ep
.desc
&& ep
->ep
.name
!= ep0name
))) {
1312 dprintk(DEBUG_NORMAL
, "%s: inval 2\n", __func__
);
1316 local_irq_save(flags
);
1318 idx
= ep
->bEndpointAddress
& 0x7F;
1321 s3c2410_udc_set_ep0_ss(base_addr
);
1322 s3c2410_udc_set_ep0_de_out(base_addr
);
1324 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
1325 ep_csr
= udc_read((ep
->bEndpointAddress
& USB_DIR_IN
)
1326 ? S3C2410_UDC_IN_CSR1_REG
1327 : S3C2410_UDC_OUT_CSR1_REG
);
1329 if ((ep
->bEndpointAddress
& USB_DIR_IN
) != 0) {
1331 udc_write(ep_csr
| S3C2410_UDC_ICSR1_SENDSTL
,
1332 S3C2410_UDC_IN_CSR1_REG
);
1334 ep_csr
&= ~S3C2410_UDC_ICSR1_SENDSTL
;
1335 udc_write(ep_csr
, S3C2410_UDC_IN_CSR1_REG
);
1336 ep_csr
|= S3C2410_UDC_ICSR1_CLRDT
;
1337 udc_write(ep_csr
, S3C2410_UDC_IN_CSR1_REG
);
1341 udc_write(ep_csr
| S3C2410_UDC_OCSR1_SENDSTL
,
1342 S3C2410_UDC_OUT_CSR1_REG
);
1344 ep_csr
&= ~S3C2410_UDC_OCSR1_SENDSTL
;
1345 udc_write(ep_csr
, S3C2410_UDC_OUT_CSR1_REG
);
1346 ep_csr
|= S3C2410_UDC_OCSR1_CLRDT
;
1347 udc_write(ep_csr
, S3C2410_UDC_OUT_CSR1_REG
);
1352 ep
->halted
= value
? 1 : 0;
1353 local_irq_restore(flags
);
1358 static const struct usb_ep_ops s3c2410_ep_ops
= {
1359 .enable
= s3c2410_udc_ep_enable
,
1360 .disable
= s3c2410_udc_ep_disable
,
1362 .alloc_request
= s3c2410_udc_alloc_request
,
1363 .free_request
= s3c2410_udc_free_request
,
1365 .queue
= s3c2410_udc_queue
,
1366 .dequeue
= s3c2410_udc_dequeue
,
1368 .set_halt
= s3c2410_udc_set_halt
,
1371 /*------------------------- usb_gadget_ops ----------------------------------*/
1374 * s3c2410_udc_get_frame
1376 static int s3c2410_udc_get_frame(struct usb_gadget
*_gadget
)
1380 dprintk(DEBUG_VERBOSE
, "%s()\n", __func__
);
1382 tmp
= udc_read(S3C2410_UDC_FRAME_NUM2_REG
) << 8;
1383 tmp
|= udc_read(S3C2410_UDC_FRAME_NUM1_REG
);
1388 * s3c2410_udc_wakeup
1390 static int s3c2410_udc_wakeup(struct usb_gadget
*_gadget
)
1392 dprintk(DEBUG_NORMAL
, "%s()\n", __func__
);
1397 * s3c2410_udc_set_selfpowered
1399 static int s3c2410_udc_set_selfpowered(struct usb_gadget
*gadget
, int value
)
1401 struct s3c2410_udc
*udc
= to_s3c2410_udc(gadget
);
1403 dprintk(DEBUG_NORMAL
, "%s()\n", __func__
);
1405 gadget
->is_selfpowered
= (value
!= 0);
1407 udc
->devstatus
|= (1 << USB_DEVICE_SELF_POWERED
);
1409 udc
->devstatus
&= ~(1 << USB_DEVICE_SELF_POWERED
);
1414 static void s3c2410_udc_disable(struct s3c2410_udc
*dev
);
1415 static void s3c2410_udc_enable(struct s3c2410_udc
*dev
);
1417 static int s3c2410_udc_set_pullup(struct s3c2410_udc
*udc
, int is_on
)
1419 dprintk(DEBUG_NORMAL
, "%s()\n", __func__
);
1421 if (udc_info
&& (udc_info
->udc_command
||
1422 gpio_is_valid(udc_info
->pullup_pin
))) {
1425 s3c2410_udc_enable(udc
);
1427 if (udc
->gadget
.speed
!= USB_SPEED_UNKNOWN
) {
1428 if (udc
->driver
&& udc
->driver
->disconnect
)
1429 udc
->driver
->disconnect(&udc
->gadget
);
1432 s3c2410_udc_disable(udc
);
1441 static int s3c2410_udc_vbus_session(struct usb_gadget
*gadget
, int is_active
)
1443 struct s3c2410_udc
*udc
= to_s3c2410_udc(gadget
);
1445 dprintk(DEBUG_NORMAL
, "%s()\n", __func__
);
1447 udc
->vbus
= (is_active
!= 0);
1448 s3c2410_udc_set_pullup(udc
, is_active
);
1452 static int s3c2410_udc_pullup(struct usb_gadget
*gadget
, int is_on
)
1454 struct s3c2410_udc
*udc
= to_s3c2410_udc(gadget
);
1456 dprintk(DEBUG_NORMAL
, "%s()\n", __func__
);
1458 s3c2410_udc_set_pullup(udc
, is_on
);
1462 static irqreturn_t
s3c2410_udc_vbus_irq(int irq
, void *_dev
)
1464 struct s3c2410_udc
*dev
= _dev
;
1467 dprintk(DEBUG_NORMAL
, "%s()\n", __func__
);
1469 value
= gpio_get_value(udc_info
->vbus_pin
) ? 1 : 0;
1470 if (udc_info
->vbus_pin_inverted
)
1473 if (value
!= dev
->vbus
)
1474 s3c2410_udc_vbus_session(&dev
->gadget
, value
);
1479 static int s3c2410_vbus_draw(struct usb_gadget
*_gadget
, unsigned ma
)
1481 dprintk(DEBUG_NORMAL
, "%s()\n", __func__
);
1483 if (udc_info
&& udc_info
->vbus_draw
) {
1484 udc_info
->vbus_draw(ma
);
1491 static int s3c2410_udc_start(struct usb_gadget
*g
,
1492 struct usb_gadget_driver
*driver
);
1493 static int s3c2410_udc_stop(struct usb_gadget
*g
);
1495 static const struct usb_gadget_ops s3c2410_ops
= {
1496 .get_frame
= s3c2410_udc_get_frame
,
1497 .wakeup
= s3c2410_udc_wakeup
,
1498 .set_selfpowered
= s3c2410_udc_set_selfpowered
,
1499 .pullup
= s3c2410_udc_pullup
,
1500 .vbus_session
= s3c2410_udc_vbus_session
,
1501 .vbus_draw
= s3c2410_vbus_draw
,
1502 .udc_start
= s3c2410_udc_start
,
1503 .udc_stop
= s3c2410_udc_stop
,
1506 static void s3c2410_udc_command(enum s3c2410_udc_cmd_e cmd
)
1511 if (udc_info
->udc_command
) {
1512 udc_info
->udc_command(cmd
);
1513 } else if (gpio_is_valid(udc_info
->pullup_pin
)) {
1517 case S3C2410_UDC_P_ENABLE
:
1520 case S3C2410_UDC_P_DISABLE
:
1526 value
^= udc_info
->pullup_pin_inverted
;
1528 gpio_set_value(udc_info
->pullup_pin
, value
);
1532 /*------------------------- gadget driver handling---------------------------*/
1534 * s3c2410_udc_disable
1536 static void s3c2410_udc_disable(struct s3c2410_udc
*dev
)
1538 dprintk(DEBUG_NORMAL
, "%s()\n", __func__
);
1540 /* Disable all interrupts */
1541 udc_write(0x00, S3C2410_UDC_USB_INT_EN_REG
);
1542 udc_write(0x00, S3C2410_UDC_EP_INT_EN_REG
);
1544 /* Clear the interrupt registers */
1545 udc_write(S3C2410_UDC_USBINT_RESET
1546 | S3C2410_UDC_USBINT_RESUME
1547 | S3C2410_UDC_USBINT_SUSPEND
,
1548 S3C2410_UDC_USB_INT_REG
);
1550 udc_write(0x1F, S3C2410_UDC_EP_INT_REG
);
1552 /* Good bye, cruel world */
1553 s3c2410_udc_command(S3C2410_UDC_P_DISABLE
);
1555 /* Set speed to unknown */
1556 dev
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1560 * s3c2410_udc_reinit
1562 static void s3c2410_udc_reinit(struct s3c2410_udc
*dev
)
1566 /* device/ep0 records init */
1567 INIT_LIST_HEAD(&dev
->gadget
.ep_list
);
1568 INIT_LIST_HEAD(&dev
->gadget
.ep0
->ep_list
);
1569 dev
->ep0state
= EP0_IDLE
;
1571 for (i
= 0; i
< S3C2410_ENDPOINTS
; i
++) {
1572 struct s3c2410_ep
*ep
= &dev
->ep
[i
];
1575 list_add_tail(&ep
->ep
.ep_list
, &dev
->gadget
.ep_list
);
1580 INIT_LIST_HEAD(&ep
->queue
);
1581 usb_ep_set_maxpacket_limit(&ep
->ep
, ep
->ep
.maxpacket
);
1586 * s3c2410_udc_enable
1588 static void s3c2410_udc_enable(struct s3c2410_udc
*dev
)
1592 dprintk(DEBUG_NORMAL
, "s3c2410_udc_enable called\n");
1594 /* dev->gadget.speed = USB_SPEED_UNKNOWN; */
1595 dev
->gadget
.speed
= USB_SPEED_FULL
;
1597 /* Set MAXP for all endpoints */
1598 for (i
= 0; i
< S3C2410_ENDPOINTS
; i
++) {
1599 udc_write(i
, S3C2410_UDC_INDEX_REG
);
1600 udc_write((dev
->ep
[i
].ep
.maxpacket
& 0x7ff) >> 3,
1601 S3C2410_UDC_MAXP_REG
);
1604 /* Set default power state */
1605 udc_write(DEFAULT_POWER_STATE
, S3C2410_UDC_PWR_REG
);
1607 /* Enable reset and suspend interrupt interrupts */
1608 udc_write(S3C2410_UDC_USBINT_RESET
| S3C2410_UDC_USBINT_SUSPEND
,
1609 S3C2410_UDC_USB_INT_EN_REG
);
1611 /* Enable ep0 interrupt */
1612 udc_write(S3C2410_UDC_INT_EP0
, S3C2410_UDC_EP_INT_EN_REG
);
1614 /* time to say "hello, world" */
1615 s3c2410_udc_command(S3C2410_UDC_P_ENABLE
);
1618 static int s3c2410_udc_start(struct usb_gadget
*g
,
1619 struct usb_gadget_driver
*driver
)
1621 struct s3c2410_udc
*udc
= to_s3c2410(g
);
1623 dprintk(DEBUG_NORMAL
, "%s() '%s'\n", __func__
, driver
->driver
.name
);
1625 /* Hook the driver */
1626 udc
->driver
= driver
;
1629 s3c2410_udc_enable(udc
);
1634 static int s3c2410_udc_stop(struct usb_gadget
*g
)
1636 struct s3c2410_udc
*udc
= to_s3c2410(g
);
1641 s3c2410_udc_disable(udc
);
1646 /*---------------------------------------------------------------------------*/
1647 static struct s3c2410_udc memory
= {
1649 .ops
= &s3c2410_ops
,
1650 .ep0
= &memory
.ep
[0].ep
,
1651 .name
= gadget_name
,
1653 .init_name
= "gadget",
1657 /* control endpoint */
1662 .ops
= &s3c2410_ep_ops
,
1663 .maxpacket
= EP0_FIFO_SIZE
,
1664 .caps
= USB_EP_CAPS(USB_EP_CAPS_TYPE_CONTROL
,
1665 USB_EP_CAPS_DIR_ALL
),
1670 /* first group of endpoints */
1675 .ops
= &s3c2410_ep_ops
,
1676 .maxpacket
= EP_FIFO_SIZE
,
1677 .caps
= USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK
,
1678 USB_EP_CAPS_DIR_ALL
),
1681 .fifo_size
= EP_FIFO_SIZE
,
1682 .bEndpointAddress
= 1,
1683 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1689 .ops
= &s3c2410_ep_ops
,
1690 .maxpacket
= EP_FIFO_SIZE
,
1691 .caps
= USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK
,
1692 USB_EP_CAPS_DIR_ALL
),
1695 .fifo_size
= EP_FIFO_SIZE
,
1696 .bEndpointAddress
= 2,
1697 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1703 .ops
= &s3c2410_ep_ops
,
1704 .maxpacket
= EP_FIFO_SIZE
,
1705 .caps
= USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK
,
1706 USB_EP_CAPS_DIR_ALL
),
1709 .fifo_size
= EP_FIFO_SIZE
,
1710 .bEndpointAddress
= 3,
1711 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1717 .ops
= &s3c2410_ep_ops
,
1718 .maxpacket
= EP_FIFO_SIZE
,
1719 .caps
= USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK
,
1720 USB_EP_CAPS_DIR_ALL
),
1723 .fifo_size
= EP_FIFO_SIZE
,
1724 .bEndpointAddress
= 4,
1725 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1731 * probe - binds to the platform device
1733 static int s3c2410_udc_probe(struct platform_device
*pdev
)
1735 struct s3c2410_udc
*udc
= &memory
;
1736 struct device
*dev
= &pdev
->dev
;
1740 dev_dbg(dev
, "%s()\n", __func__
);
1742 usb_bus_clock
= clk_get(NULL
, "usb-bus-gadget");
1743 if (IS_ERR(usb_bus_clock
)) {
1744 dev_err(dev
, "failed to get usb bus clock source\n");
1745 return PTR_ERR(usb_bus_clock
);
1748 clk_prepare_enable(usb_bus_clock
);
1750 udc_clock
= clk_get(NULL
, "usb-device");
1751 if (IS_ERR(udc_clock
)) {
1752 dev_err(dev
, "failed to get udc clock source\n");
1753 retval
= PTR_ERR(udc_clock
);
1754 goto err_usb_bus_clk
;
1757 clk_prepare_enable(udc_clock
);
1761 dev_dbg(dev
, "got and enabled clocks\n");
1763 if (strncmp(pdev
->name
, "s3c2440", 7) == 0) {
1764 dev_info(dev
, "S3C2440: increasing FIFO to 128 bytes\n");
1765 memory
.ep
[1].fifo_size
= S3C2440_EP_FIFO_SIZE
;
1766 memory
.ep
[2].fifo_size
= S3C2440_EP_FIFO_SIZE
;
1767 memory
.ep
[3].fifo_size
= S3C2440_EP_FIFO_SIZE
;
1768 memory
.ep
[4].fifo_size
= S3C2440_EP_FIFO_SIZE
;
1771 spin_lock_init(&udc
->lock
);
1772 udc_info
= dev_get_platdata(&pdev
->dev
);
1774 base_addr
= devm_platform_ioremap_resource(pdev
, 0);
1775 if (IS_ERR(base_addr
)) {
1776 retval
= PTR_ERR(base_addr
);
1780 the_controller
= udc
;
1781 platform_set_drvdata(pdev
, udc
);
1783 s3c2410_udc_disable(udc
);
1784 s3c2410_udc_reinit(udc
);
1786 irq_usbd
= platform_get_irq(pdev
, 0);
1792 /* irq setup after old hardware state is cleaned up */
1793 retval
= request_irq(irq_usbd
, s3c2410_udc_irq
,
1794 0, gadget_name
, udc
);
1797 dev_err(dev
, "cannot get irq %i, err %d\n", irq_usbd
, retval
);
1802 dev_dbg(dev
, "got irq %i\n", irq_usbd
);
1804 if (udc_info
&& udc_info
->vbus_pin
> 0) {
1805 retval
= gpio_request(udc_info
->vbus_pin
, "udc vbus");
1807 dev_err(dev
, "cannot claim vbus pin\n");
1811 irq
= gpio_to_irq(udc_info
->vbus_pin
);
1813 dev_err(dev
, "no irq for gpio vbus pin\n");
1815 goto err_gpio_claim
;
1818 retval
= request_irq(irq
, s3c2410_udc_vbus_irq
,
1820 | IRQF_TRIGGER_FALLING
| IRQF_SHARED
,
1824 dev_err(dev
, "can't get vbus irq %d, err %d\n",
1827 goto err_gpio_claim
;
1830 dev_dbg(dev
, "got irq %i\n", irq
);
1835 if (udc_info
&& !udc_info
->udc_command
&&
1836 gpio_is_valid(udc_info
->pullup_pin
)) {
1838 retval
= gpio_request_one(udc_info
->pullup_pin
,
1839 udc_info
->vbus_pin_inverted
?
1840 GPIOF_OUT_INIT_HIGH
: GPIOF_OUT_INIT_LOW
,
1846 retval
= usb_add_gadget_udc(&pdev
->dev
, &udc
->gadget
);
1850 debugfs_create_file("registers", S_IRUGO
, s3c2410_udc_debugfs_root
, udc
,
1851 &s3c2410_udc_debugfs_fops
);
1853 dev_dbg(dev
, "probe ok\n");
1858 if (udc_info
&& !udc_info
->udc_command
&&
1859 gpio_is_valid(udc_info
->pullup_pin
))
1860 gpio_free(udc_info
->pullup_pin
);
1862 if (udc_info
&& udc_info
->vbus_pin
> 0)
1863 free_irq(gpio_to_irq(udc_info
->vbus_pin
), udc
);
1865 if (udc_info
&& udc_info
->vbus_pin
> 0)
1866 gpio_free(udc_info
->vbus_pin
);
1868 free_irq(irq_usbd
, udc
);
1870 clk_disable_unprepare(udc_clock
);
1874 clk_disable_unprepare(usb_bus_clock
);
1875 clk_put(usb_bus_clock
);
1876 usb_bus_clock
= NULL
;
1882 * s3c2410_udc_remove
1884 static int s3c2410_udc_remove(struct platform_device
*pdev
)
1886 struct s3c2410_udc
*udc
= platform_get_drvdata(pdev
);
1889 dev_dbg(&pdev
->dev
, "%s()\n", __func__
);
1894 usb_del_gadget_udc(&udc
->gadget
);
1895 debugfs_remove(debugfs_lookup("registers", s3c2410_udc_debugfs_root
));
1897 if (udc_info
&& !udc_info
->udc_command
&&
1898 gpio_is_valid(udc_info
->pullup_pin
))
1899 gpio_free(udc_info
->pullup_pin
);
1901 if (udc_info
&& udc_info
->vbus_pin
> 0) {
1902 irq
= gpio_to_irq(udc_info
->vbus_pin
);
1906 free_irq(irq_usbd
, udc
);
1908 if (!IS_ERR(udc_clock
) && udc_clock
!= NULL
) {
1909 clk_disable_unprepare(udc_clock
);
1914 if (!IS_ERR(usb_bus_clock
) && usb_bus_clock
!= NULL
) {
1915 clk_disable_unprepare(usb_bus_clock
);
1916 clk_put(usb_bus_clock
);
1917 usb_bus_clock
= NULL
;
1920 dev_dbg(&pdev
->dev
, "%s: remove ok\n", __func__
);
1926 s3c2410_udc_suspend(struct platform_device
*pdev
, pm_message_t message
)
1928 s3c2410_udc_command(S3C2410_UDC_P_DISABLE
);
1933 static int s3c2410_udc_resume(struct platform_device
*pdev
)
1935 s3c2410_udc_command(S3C2410_UDC_P_ENABLE
);
1940 #define s3c2410_udc_suspend NULL
1941 #define s3c2410_udc_resume NULL
1944 static const struct platform_device_id s3c_udc_ids
[] = {
1945 { "s3c2410-usbgadget", },
1946 { "s3c2440-usbgadget", },
1949 MODULE_DEVICE_TABLE(platform
, s3c_udc_ids
);
1951 static struct platform_driver udc_driver_24x0
= {
1953 .name
= "s3c24x0-usbgadget",
1955 .probe
= s3c2410_udc_probe
,
1956 .remove
= s3c2410_udc_remove
,
1957 .suspend
= s3c2410_udc_suspend
,
1958 .resume
= s3c2410_udc_resume
,
1959 .id_table
= s3c_udc_ids
,
1962 static int __init
udc_init(void)
1966 dprintk(DEBUG_NORMAL
, "%s\n", gadget_name
);
1968 s3c2410_udc_debugfs_root
= debugfs_create_dir(gadget_name
,
1971 retval
= platform_driver_register(&udc_driver_24x0
);
1978 debugfs_remove(s3c2410_udc_debugfs_root
);
1982 static void __exit
udc_exit(void)
1984 platform_driver_unregister(&udc_driver_24x0
);
1985 debugfs_remove_recursive(s3c2410_udc_debugfs_root
);
1988 module_init(udc_init
);
1989 module_exit(udc_exit
);
1991 MODULE_AUTHOR(DRIVER_AUTHOR
);
1992 MODULE_DESCRIPTION(DRIVER_DESC
);
1993 MODULE_LICENSE("GPL");