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.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/delay.h>
28 #include <linux/ioport.h>
29 #include <linux/sched.h>
30 #include <linux/slab.h>
31 #include <linux/smp_lock.h>
32 #include <linux/errno.h>
33 #include <linux/init.h>
34 #include <linux/timer.h>
35 #include <linux/list.h>
36 #include <linux/interrupt.h>
37 #include <linux/platform_device.h>
38 #include <linux/version.h>
39 #include <linux/clk.h>
41 #include <linux/debugfs.h>
42 #include <linux/seq_file.h>
44 #include <linux/usb.h>
45 #include <linux/usb/gadget.h>
47 #include <asm/byteorder.h>
50 #include <asm/system.h>
51 #include <asm/unaligned.h>
52 #include <asm/arch/irqs.h>
54 #include <asm/arch/hardware.h>
55 #include <asm/arch/regs-gpio.h>
57 #include <asm/plat-s3c24xx/regs-udc.h>
58 #include <asm/plat-s3c24xx/udc.h>
60 #include <asm/mach-types.h>
62 #include "s3c2410_udc.h"
64 #define DRIVER_DESC "S3C2410 USB Device Controller Gadget"
65 #define DRIVER_VERSION "29 Apr 2007"
66 #define DRIVER_AUTHOR "Herbert Pötzl <herbert@13thfloor.at>, " \
67 "Arnaud Patard <arnaud.patard@rtp-net.org>"
69 static const char gadget_name
[] = "s3c2410_udc";
70 static const char driver_desc
[] = DRIVER_DESC
;
72 static struct s3c2410_udc
*the_controller
;
73 static struct clk
*udc_clock
;
74 static struct clk
*usb_bus_clock
;
75 static void __iomem
*base_addr
;
76 static u64 rsrc_start
;
78 static struct dentry
*s3c2410_udc_debugfs_root
;
80 static inline u32
udc_read(u32 reg
)
82 return readb(base_addr
+ reg
);
85 static inline void udc_write(u32 value
, u32 reg
)
87 writeb(value
, base_addr
+ reg
);
90 static inline void udc_writeb(void __iomem
*base
, u32 value
, u32 reg
)
92 writeb(value
, base
+ reg
);
95 static struct s3c2410_udc_mach_info
*udc_info
;
97 /*************************** DEBUG FUNCTION ***************************/
98 #define DEBUG_NORMAL 1
99 #define DEBUG_VERBOSE 2
101 #ifdef CONFIG_USB_S3C2410_DEBUG
102 #define USB_S3C2410_DEBUG_LEVEL 0
104 static uint32_t s3c2410_ticks
= 0;
106 static int dprintk(int level
, const char *fmt
, ...)
108 static char printk_buf
[1024];
109 static long prevticks
;
110 static int invocation
;
114 if (level
> USB_S3C2410_DEBUG_LEVEL
)
117 if (s3c2410_ticks
!= prevticks
) {
118 prevticks
= s3c2410_ticks
;
122 len
= scnprintf(printk_buf
,
123 sizeof(printk_buf
), "%1lu.%02d USB: ",
124 prevticks
, invocation
++);
127 len
= vscnprintf(printk_buf
+len
,
128 sizeof(printk_buf
)-len
, fmt
, args
);
131 return printk(KERN_DEBUG
"%s", printk_buf
);
134 static int dprintk(int level
, const char *fmt
, ...)
139 static int s3c2410_udc_debugfs_seq_show(struct seq_file
*m
, void *p
)
141 u32 addr_reg
,pwr_reg
,ep_int_reg
,usb_int_reg
;
142 u32 ep_int_en_reg
, usb_int_en_reg
, ep0_csr
;
143 u32 ep1_i_csr1
,ep1_i_csr2
,ep1_o_csr1
,ep1_o_csr2
;
144 u32 ep2_i_csr1
,ep2_i_csr2
,ep2_o_csr1
,ep2_o_csr2
;
146 addr_reg
= udc_read(S3C2410_UDC_FUNC_ADDR_REG
);
147 pwr_reg
= udc_read(S3C2410_UDC_PWR_REG
);
148 ep_int_reg
= udc_read(S3C2410_UDC_EP_INT_REG
);
149 usb_int_reg
= udc_read(S3C2410_UDC_USB_INT_REG
);
150 ep_int_en_reg
= udc_read(S3C2410_UDC_EP_INT_EN_REG
);
151 usb_int_en_reg
= udc_read(S3C2410_UDC_USB_INT_EN_REG
);
152 udc_write(0, S3C2410_UDC_INDEX_REG
);
153 ep0_csr
= udc_read(S3C2410_UDC_IN_CSR1_REG
);
154 udc_write(1, S3C2410_UDC_INDEX_REG
);
155 ep1_i_csr1
= udc_read(S3C2410_UDC_IN_CSR1_REG
);
156 ep1_i_csr2
= udc_read(S3C2410_UDC_IN_CSR2_REG
);
157 ep1_o_csr1
= udc_read(S3C2410_UDC_IN_CSR1_REG
);
158 ep1_o_csr2
= udc_read(S3C2410_UDC_IN_CSR2_REG
);
159 udc_write(2, S3C2410_UDC_INDEX_REG
);
160 ep2_i_csr1
= udc_read(S3C2410_UDC_IN_CSR1_REG
);
161 ep2_i_csr2
= udc_read(S3C2410_UDC_IN_CSR2_REG
);
162 ep2_o_csr1
= udc_read(S3C2410_UDC_IN_CSR1_REG
);
163 ep2_o_csr2
= udc_read(S3C2410_UDC_IN_CSR2_REG
);
165 seq_printf(m
, "FUNC_ADDR_REG : 0x%04X\n"
167 "EP_INT_REG : 0x%04X\n"
168 "USB_INT_REG : 0x%04X\n"
169 "EP_INT_EN_REG : 0x%04X\n"
170 "USB_INT_EN_REG : 0x%04X\n"
172 "EP1_I_CSR1 : 0x%04X\n"
173 "EP1_I_CSR2 : 0x%04X\n"
174 "EP1_O_CSR1 : 0x%04X\n"
175 "EP1_O_CSR2 : 0x%04X\n"
176 "EP2_I_CSR1 : 0x%04X\n"
177 "EP2_I_CSR2 : 0x%04X\n"
178 "EP2_O_CSR1 : 0x%04X\n"
179 "EP2_O_CSR2 : 0x%04X\n",
180 addr_reg
,pwr_reg
,ep_int_reg
,usb_int_reg
,
181 ep_int_en_reg
, usb_int_en_reg
, ep0_csr
,
182 ep1_i_csr1
,ep1_i_csr2
,ep1_o_csr1
,ep1_o_csr2
,
183 ep2_i_csr1
,ep2_i_csr2
,ep2_o_csr1
,ep2_o_csr2
189 static int s3c2410_udc_debugfs_fops_open(struct inode
*inode
,
192 return single_open(file
, s3c2410_udc_debugfs_seq_show
, NULL
);
195 static const struct file_operations s3c2410_udc_debugfs_fops
= {
196 .open
= s3c2410_udc_debugfs_fops_open
,
199 .release
= single_release
,
200 .owner
= THIS_MODULE
,
205 static inline void s3c2410_udc_clear_ep0_opr(void __iomem
*base
)
207 udc_writeb(base
, S3C2410_UDC_INDEX_EP0
, S3C2410_UDC_INDEX_REG
);
208 udc_writeb(base
, S3C2410_UDC_EP0_CSR_SOPKTRDY
,
209 S3C2410_UDC_EP0_CSR_REG
);
212 static inline void s3c2410_udc_clear_ep0_sst(void __iomem
*base
)
214 udc_writeb(base
, S3C2410_UDC_INDEX_EP0
, S3C2410_UDC_INDEX_REG
);
215 writeb(0x00, base
+ S3C2410_UDC_EP0_CSR_REG
);
218 static inline void s3c2410_udc_clear_ep0_se(void __iomem
*base
)
220 udc_writeb(base
, S3C2410_UDC_INDEX_EP0
, S3C2410_UDC_INDEX_REG
);
221 udc_writeb(base
, S3C2410_UDC_EP0_CSR_SSE
, S3C2410_UDC_EP0_CSR_REG
);
224 static inline void s3c2410_udc_set_ep0_ipr(void __iomem
*base
)
226 udc_writeb(base
, S3C2410_UDC_INDEX_EP0
, S3C2410_UDC_INDEX_REG
);
227 udc_writeb(base
, S3C2410_UDC_EP0_CSR_IPKRDY
, S3C2410_UDC_EP0_CSR_REG
);
230 static inline void s3c2410_udc_set_ep0_de(void __iomem
*base
)
232 udc_writeb(base
, S3C2410_UDC_INDEX_EP0
, S3C2410_UDC_INDEX_REG
);
233 udc_writeb(base
, S3C2410_UDC_EP0_CSR_DE
, S3C2410_UDC_EP0_CSR_REG
);
236 inline void s3c2410_udc_set_ep0_ss(void __iomem
*b
)
238 udc_writeb(b
, S3C2410_UDC_INDEX_EP0
, S3C2410_UDC_INDEX_REG
);
239 udc_writeb(b
, S3C2410_UDC_EP0_CSR_SENDSTL
, S3C2410_UDC_EP0_CSR_REG
);
242 static inline void s3c2410_udc_set_ep0_de_out(void __iomem
*base
)
244 udc_writeb(base
, S3C2410_UDC_INDEX_EP0
, S3C2410_UDC_INDEX_REG
);
246 udc_writeb(base
,(S3C2410_UDC_EP0_CSR_SOPKTRDY
247 | S3C2410_UDC_EP0_CSR_DE
),
248 S3C2410_UDC_EP0_CSR_REG
);
251 static inline void s3c2410_udc_set_ep0_sse_out(void __iomem
*base
)
253 udc_writeb(base
, S3C2410_UDC_INDEX_EP0
, S3C2410_UDC_INDEX_REG
);
254 udc_writeb(base
, (S3C2410_UDC_EP0_CSR_SOPKTRDY
255 | S3C2410_UDC_EP0_CSR_SSE
),
256 S3C2410_UDC_EP0_CSR_REG
);
259 static inline void s3c2410_udc_set_ep0_de_in(void __iomem
*base
)
261 udc_writeb(base
, S3C2410_UDC_INDEX_EP0
, S3C2410_UDC_INDEX_REG
);
262 udc_writeb(base
, (S3C2410_UDC_EP0_CSR_IPKRDY
263 | S3C2410_UDC_EP0_CSR_DE
),
264 S3C2410_UDC_EP0_CSR_REG
);
267 /*------------------------- I/O ----------------------------------*/
272 static void s3c2410_udc_done(struct s3c2410_ep
*ep
,
273 struct s3c2410_request
*req
, int status
)
275 unsigned halted
= ep
->halted
;
277 list_del_init(&req
->queue
);
279 if (likely (req
->req
.status
== -EINPROGRESS
))
280 req
->req
.status
= status
;
282 status
= req
->req
.status
;
285 req
->req
.complete(&ep
->ep
, &req
->req
);
289 static void s3c2410_udc_nuke(struct s3c2410_udc
*udc
,
290 struct s3c2410_ep
*ep
, int status
)
293 if (&ep
->queue
== NULL
)
296 while (!list_empty (&ep
->queue
)) {
297 struct s3c2410_request
*req
;
298 req
= list_entry (ep
->queue
.next
, struct s3c2410_request
,
300 s3c2410_udc_done(ep
, req
, status
);
304 static inline void s3c2410_udc_clear_ep_state(struct s3c2410_udc
*dev
)
308 /* hardware SET_{CONFIGURATION,INTERFACE} automagic resets endpoint
309 * fifos, and pending transactions mustn't be continued in any case.
312 for (i
= 1; i
< S3C2410_ENDPOINTS
; i
++)
313 s3c2410_udc_nuke(dev
, &dev
->ep
[i
], -ECONNABORTED
);
316 static inline int s3c2410_udc_fifo_count_out(void)
320 tmp
= udc_read(S3C2410_UDC_OUT_FIFO_CNT2_REG
) << 8;
321 tmp
|= udc_read(S3C2410_UDC_OUT_FIFO_CNT1_REG
);
326 * s3c2410_udc_write_packet
328 static inline int s3c2410_udc_write_packet(int fifo
,
329 struct s3c2410_request
*req
,
332 unsigned len
= min(req
->req
.length
- req
->req
.actual
, max
);
333 u8
*buf
= req
->req
.buf
+ req
->req
.actual
;
337 dprintk(DEBUG_VERBOSE
, "%s %d %d %d %d\n", __func__
,
338 req
->req
.actual
, req
->req
.length
, len
, req
->req
.actual
+ len
);
340 req
->req
.actual
+= len
;
343 writesb(base_addr
+ fifo
, buf
, len
);
348 * s3c2410_udc_write_fifo
350 * return: 0 = still running, 1 = completed, negative = errno
352 static int s3c2410_udc_write_fifo(struct s3c2410_ep
*ep
,
353 struct s3c2410_request
*req
)
361 idx
= ep
->bEndpointAddress
& 0x7F;
366 fifo_reg
= S3C2410_UDC_EP0_FIFO_REG
;
369 fifo_reg
= S3C2410_UDC_EP1_FIFO_REG
;
372 fifo_reg
= S3C2410_UDC_EP2_FIFO_REG
;
375 fifo_reg
= S3C2410_UDC_EP3_FIFO_REG
;
378 fifo_reg
= S3C2410_UDC_EP4_FIFO_REG
;
382 count
= s3c2410_udc_write_packet(fifo_reg
, req
, ep
->ep
.maxpacket
);
384 /* last packet is often short (sometimes a zlp) */
385 if (count
!= ep
->ep
.maxpacket
)
387 else if (req
->req
.length
!= req
->req
.actual
|| req
->req
.zero
)
392 /* Only ep0 debug messages are interesting */
394 dprintk(DEBUG_NORMAL
,
395 "Written ep%d %d.%d of %d b [last %d,z %d]\n",
396 idx
, count
, req
->req
.actual
, req
->req
.length
,
397 is_last
, req
->req
.zero
);
400 /* The order is important. It prevents sending 2 packets
401 * at the same time */
404 /* Reset signal => no need to say 'data sent' */
405 if (! (udc_read(S3C2410_UDC_USB_INT_REG
)
406 & S3C2410_UDC_USBINT_RESET
))
407 s3c2410_udc_set_ep0_de_in(base_addr
);
408 ep
->dev
->ep0state
=EP0_IDLE
;
410 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
411 ep_csr
= udc_read(S3C2410_UDC_IN_CSR1_REG
);
412 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
413 udc_write(ep_csr
| S3C2410_UDC_ICSR1_PKTRDY
,
414 S3C2410_UDC_IN_CSR1_REG
);
417 s3c2410_udc_done(ep
, req
, 0);
421 /* Reset signal => no need to say 'data sent' */
422 if (! (udc_read(S3C2410_UDC_USB_INT_REG
)
423 & S3C2410_UDC_USBINT_RESET
))
424 s3c2410_udc_set_ep0_ipr(base_addr
);
426 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
427 ep_csr
= udc_read(S3C2410_UDC_IN_CSR1_REG
);
428 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
429 udc_write(ep_csr
| S3C2410_UDC_ICSR1_PKTRDY
,
430 S3C2410_UDC_IN_CSR1_REG
);
437 static inline int s3c2410_udc_read_packet(int fifo
, u8
*buf
,
438 struct s3c2410_request
*req
, unsigned avail
)
442 len
= min(req
->req
.length
- req
->req
.actual
, avail
);
443 req
->req
.actual
+= len
;
445 readsb(fifo
+ base_addr
, buf
, len
);
450 * return: 0 = still running, 1 = queue empty, negative = errno
452 static int s3c2410_udc_read_fifo(struct s3c2410_ep
*ep
,
453 struct s3c2410_request
*req
)
457 unsigned bufferspace
;
464 idx
= ep
->bEndpointAddress
& 0x7F;
470 fifo_reg
= S3C2410_UDC_EP0_FIFO_REG
;
473 fifo_reg
= S3C2410_UDC_EP1_FIFO_REG
;
476 fifo_reg
= S3C2410_UDC_EP2_FIFO_REG
;
479 fifo_reg
= S3C2410_UDC_EP3_FIFO_REG
;
482 fifo_reg
= S3C2410_UDC_EP4_FIFO_REG
;
486 if (!req
->req
.length
)
489 buf
= req
->req
.buf
+ req
->req
.actual
;
490 bufferspace
= req
->req
.length
- req
->req
.actual
;
492 dprintk(DEBUG_NORMAL
, "%s: buffer full!\n", __func__
);
496 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
498 fifo_count
= s3c2410_udc_fifo_count_out();
499 dprintk(DEBUG_NORMAL
, "%s fifo count : %d\n", __func__
, fifo_count
);
501 if (fifo_count
> ep
->ep
.maxpacket
)
502 avail
= ep
->ep
.maxpacket
;
506 fifo_count
= s3c2410_udc_read_packet(fifo_reg
, buf
, req
, avail
);
508 /* checking this with ep0 is not accurate as we already
509 * read a control request
511 if (idx
!= 0 && fifo_count
< ep
->ep
.maxpacket
) {
513 /* overflowed this request? flush extra data */
514 if (fifo_count
!= avail
)
515 req
->req
.status
= -EOVERFLOW
;
517 is_last
= (req
->req
.length
<= req
->req
.actual
) ? 1 : 0;
520 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
521 fifo_count
= s3c2410_udc_fifo_count_out();
523 /* Only ep0 debug messages are interesting */
525 dprintk(DEBUG_VERBOSE
, "%s fifo count : %d [last %d]\n",
526 __func__
, fifo_count
,is_last
);
530 s3c2410_udc_set_ep0_de_out(base_addr
);
531 ep
->dev
->ep0state
= EP0_IDLE
;
533 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
534 ep_csr
= udc_read(S3C2410_UDC_OUT_CSR1_REG
);
535 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
536 udc_write(ep_csr
& ~S3C2410_UDC_OCSR1_PKTRDY
,
537 S3C2410_UDC_OUT_CSR1_REG
);
540 s3c2410_udc_done(ep
, req
, 0);
543 s3c2410_udc_clear_ep0_opr(base_addr
);
545 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
546 ep_csr
= udc_read(S3C2410_UDC_OUT_CSR1_REG
);
547 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
548 udc_write(ep_csr
& ~S3C2410_UDC_OCSR1_PKTRDY
,
549 S3C2410_UDC_OUT_CSR1_REG
);
556 static int s3c2410_udc_read_fifo_crq(struct usb_ctrlrequest
*crq
)
558 unsigned char *outbuf
= (unsigned char*)crq
;
561 udc_write(0, S3C2410_UDC_INDEX_REG
);
563 bytes_read
= s3c2410_udc_fifo_count_out();
565 dprintk(DEBUG_NORMAL
, "%s: fifo_count=%d\n", __func__
, bytes_read
);
567 if (bytes_read
> sizeof(struct usb_ctrlrequest
))
568 bytes_read
= sizeof(struct usb_ctrlrequest
);
570 readsb(S3C2410_UDC_EP0_FIFO_REG
+ base_addr
, outbuf
, bytes_read
);
572 dprintk(DEBUG_VERBOSE
, "%s: len=%d %02x:%02x {%x,%x,%x}\n", __func__
,
573 bytes_read
, crq
->bRequest
, crq
->bRequestType
,
574 crq
->wValue
, crq
->wIndex
, crq
->wLength
);
579 static int s3c2410_udc_get_status(struct s3c2410_udc
*dev
,
580 struct usb_ctrlrequest
*crq
)
583 u8 ep_num
= crq
->wIndex
& 0x7F;
584 u8 is_in
= crq
->wIndex
& USB_DIR_IN
;
586 switch (crq
->bRequestType
& USB_RECIP_MASK
) {
587 case USB_RECIP_INTERFACE
:
590 case USB_RECIP_DEVICE
:
591 status
= dev
->devstatus
;
594 case USB_RECIP_ENDPOINT
:
595 if (ep_num
> 4 || crq
->wLength
> 2)
599 udc_write(0, S3C2410_UDC_INDEX_REG
);
600 status
= udc_read(S3C2410_UDC_IN_CSR1_REG
);
601 status
= status
& S3C2410_UDC_EP0_CSR_SENDSTL
;
603 udc_write(ep_num
, S3C2410_UDC_INDEX_REG
);
605 status
= udc_read(S3C2410_UDC_IN_CSR1_REG
);
606 status
= status
& S3C2410_UDC_ICSR1_SENDSTL
;
608 status
= udc_read(S3C2410_UDC_OUT_CSR1_REG
);
609 status
= status
& S3C2410_UDC_OCSR1_SENDSTL
;
613 status
= status
? 1 : 0;
620 /* Seems to be needed to get it working. ouch :( */
622 udc_write(status
& 0xFF, S3C2410_UDC_EP0_FIFO_REG
);
623 udc_write(status
>> 8, S3C2410_UDC_EP0_FIFO_REG
);
624 s3c2410_udc_set_ep0_de_in(base_addr
);
628 /*------------------------- usb state machine -------------------------------*/
629 static int s3c2410_udc_set_halt(struct usb_ep
*_ep
, int value
);
631 static void s3c2410_udc_handle_ep0_idle(struct s3c2410_udc
*dev
,
632 struct s3c2410_ep
*ep
,
633 struct usb_ctrlrequest
*crq
,
638 /* start control request? */
639 if (!(ep0csr
& S3C2410_UDC_EP0_CSR_OPKRDY
))
642 s3c2410_udc_nuke(dev
, ep
, -EPROTO
);
644 len
= s3c2410_udc_read_fifo_crq(crq
);
645 if (len
!= sizeof(*crq
)) {
646 dprintk(DEBUG_NORMAL
, "setup begin: fifo READ ERROR"
647 " wanted %d bytes got %d. Stalling out...\n",
649 s3c2410_udc_set_ep0_ss(base_addr
);
653 dprintk(DEBUG_NORMAL
, "bRequest = %d bRequestType %d wLength = %d\n",
654 crq
->bRequest
, crq
->bRequestType
, crq
->wLength
);
656 /* cope with automagic for some standard requests. */
657 dev
->req_std
= (crq
->bRequestType
& USB_TYPE_MASK
)
658 == USB_TYPE_STANDARD
;
660 dev
->req_pending
= 1;
662 switch (crq
->bRequest
) {
663 case USB_REQ_SET_CONFIGURATION
:
664 dprintk(DEBUG_NORMAL
, "USB_REQ_SET_CONFIGURATION ... \n");
666 if (crq
->bRequestType
== USB_RECIP_DEVICE
) {
668 s3c2410_udc_set_ep0_de_out(base_addr
);
672 case USB_REQ_SET_INTERFACE
:
673 dprintk(DEBUG_NORMAL
, "USB_REQ_SET_INTERFACE ... \n");
675 if (crq
->bRequestType
== USB_RECIP_INTERFACE
) {
677 s3c2410_udc_set_ep0_de_out(base_addr
);
681 case USB_REQ_SET_ADDRESS
:
682 dprintk(DEBUG_NORMAL
, "USB_REQ_SET_ADDRESS ... \n");
684 if (crq
->bRequestType
== USB_RECIP_DEVICE
) {
685 tmp
= crq
->wValue
& 0x7F;
687 udc_write((tmp
| S3C2410_UDC_FUNCADDR_UPDATE
),
688 S3C2410_UDC_FUNC_ADDR_REG
);
689 s3c2410_udc_set_ep0_de_out(base_addr
);
694 case USB_REQ_GET_STATUS
:
695 dprintk(DEBUG_NORMAL
, "USB_REQ_GET_STATUS ... \n");
696 s3c2410_udc_clear_ep0_opr(base_addr
);
699 if (!s3c2410_udc_get_status(dev
, crq
)) {
705 case USB_REQ_CLEAR_FEATURE
:
706 s3c2410_udc_clear_ep0_opr(base_addr
);
708 if (crq
->bRequestType
!= USB_RECIP_ENDPOINT
)
711 if (crq
->wValue
!= USB_ENDPOINT_HALT
|| crq
->wLength
!= 0)
714 s3c2410_udc_set_halt(&dev
->ep
[crq
->wIndex
& 0x7f].ep
, 0);
715 s3c2410_udc_set_ep0_de_out(base_addr
);
718 case USB_REQ_SET_FEATURE
:
719 s3c2410_udc_clear_ep0_opr(base_addr
);
721 if (crq
->bRequestType
!= USB_RECIP_ENDPOINT
)
724 if (crq
->wValue
!= USB_ENDPOINT_HALT
|| crq
->wLength
!= 0)
727 s3c2410_udc_set_halt(&dev
->ep
[crq
->wIndex
& 0x7f].ep
, 1);
728 s3c2410_udc_set_ep0_de_out(base_addr
);
732 s3c2410_udc_clear_ep0_opr(base_addr
);
736 if (crq
->bRequestType
& USB_DIR_IN
)
737 dev
->ep0state
= EP0_IN_DATA_PHASE
;
739 dev
->ep0state
= EP0_OUT_DATA_PHASE
;
741 ret
= dev
->driver
->setup(&dev
->gadget
, crq
);
743 if (dev
->req_config
) {
744 dprintk(DEBUG_NORMAL
, "config change %02x fail %d?\n",
749 if (ret
== -EOPNOTSUPP
)
750 dprintk(DEBUG_NORMAL
, "Operation not supported\n");
752 dprintk(DEBUG_NORMAL
,
753 "dev->driver->setup failed. (%d)\n", ret
);
756 s3c2410_udc_set_ep0_ss(base_addr
);
757 s3c2410_udc_set_ep0_de_out(base_addr
);
758 dev
->ep0state
= EP0_IDLE
;
759 /* deferred i/o == no response yet */
760 } else if (dev
->req_pending
) {
761 dprintk(DEBUG_VERBOSE
, "dev->req_pending... what now?\n");
765 dprintk(DEBUG_VERBOSE
, "ep0state %s\n", ep0states
[dev
->ep0state
]);
768 static void s3c2410_udc_handle_ep0(struct s3c2410_udc
*dev
)
771 struct s3c2410_ep
*ep
= &dev
->ep
[0];
772 struct s3c2410_request
*req
;
773 struct usb_ctrlrequest crq
;
775 if (list_empty(&ep
->queue
))
778 req
= list_entry(ep
->queue
.next
, struct s3c2410_request
, queue
);
780 /* We make the assumption that S3C2410_UDC_IN_CSR1_REG equal to
781 * S3C2410_UDC_EP0_CSR_REG when index is zero */
783 udc_write(0, S3C2410_UDC_INDEX_REG
);
784 ep0csr
= udc_read(S3C2410_UDC_IN_CSR1_REG
);
786 dprintk(DEBUG_NORMAL
, "ep0csr %x ep0state %s\n",
787 ep0csr
, ep0states
[dev
->ep0state
]);
789 /* clear stall status */
790 if (ep0csr
& S3C2410_UDC_EP0_CSR_SENTSTL
) {
791 s3c2410_udc_nuke(dev
, ep
, -EPIPE
);
792 dprintk(DEBUG_NORMAL
, "... clear SENT_STALL ...\n");
793 s3c2410_udc_clear_ep0_sst(base_addr
);
794 dev
->ep0state
= EP0_IDLE
;
798 /* clear setup end */
799 if (ep0csr
& S3C2410_UDC_EP0_CSR_SE
) {
800 dprintk(DEBUG_NORMAL
, "... serviced SETUP_END ...\n");
801 s3c2410_udc_nuke(dev
, ep
, 0);
802 s3c2410_udc_clear_ep0_se(base_addr
);
803 dev
->ep0state
= EP0_IDLE
;
806 switch (dev
->ep0state
) {
808 s3c2410_udc_handle_ep0_idle(dev
, ep
, &crq
, ep0csr
);
811 case EP0_IN_DATA_PHASE
: /* GET_DESCRIPTOR etc */
812 dprintk(DEBUG_NORMAL
, "EP0_IN_DATA_PHASE ... what now?\n");
813 if (!(ep0csr
& S3C2410_UDC_EP0_CSR_IPKRDY
) && req
) {
814 s3c2410_udc_write_fifo(ep
, req
);
818 case EP0_OUT_DATA_PHASE
: /* SET_DESCRIPTOR etc */
819 dprintk(DEBUG_NORMAL
, "EP0_OUT_DATA_PHASE ... what now?\n");
820 if ((ep0csr
& S3C2410_UDC_EP0_CSR_OPKRDY
) && req
) {
821 s3c2410_udc_read_fifo(ep
,req
);
826 dprintk(DEBUG_NORMAL
, "EP0_END_XFER ... what now?\n");
827 dev
->ep0state
= EP0_IDLE
;
831 dprintk(DEBUG_NORMAL
, "EP0_STALL ... what now?\n");
832 dev
->ep0state
= EP0_IDLE
;
838 * handle_ep - Manage I/O endpoints
841 static void s3c2410_udc_handle_ep(struct s3c2410_ep
*ep
)
843 struct s3c2410_request
*req
;
844 int is_in
= ep
->bEndpointAddress
& USB_DIR_IN
;
848 if (likely (!list_empty(&ep
->queue
)))
849 req
= list_entry(ep
->queue
.next
,
850 struct s3c2410_request
, queue
);
854 idx
= ep
->bEndpointAddress
& 0x7F;
857 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
858 ep_csr1
= udc_read(S3C2410_UDC_IN_CSR1_REG
);
859 dprintk(DEBUG_VERBOSE
, "ep%01d write csr:%02x %d\n",
860 idx
, ep_csr1
, req
? 1 : 0);
862 if (ep_csr1
& S3C2410_UDC_ICSR1_SENTSTL
) {
863 dprintk(DEBUG_VERBOSE
, "st\n");
864 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
865 udc_write(ep_csr1
& ~S3C2410_UDC_ICSR1_SENTSTL
,
866 S3C2410_UDC_IN_CSR1_REG
);
870 if (!(ep_csr1
& S3C2410_UDC_ICSR1_PKTRDY
) && req
) {
871 s3c2410_udc_write_fifo(ep
,req
);
874 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
875 ep_csr1
= udc_read(S3C2410_UDC_OUT_CSR1_REG
);
876 dprintk(DEBUG_VERBOSE
, "ep%01d rd csr:%02x\n", idx
, ep_csr1
);
878 if (ep_csr1
& S3C2410_UDC_OCSR1_SENTSTL
) {
879 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
880 udc_write(ep_csr1
& ~S3C2410_UDC_OCSR1_SENTSTL
,
881 S3C2410_UDC_OUT_CSR1_REG
);
885 if ((ep_csr1
& S3C2410_UDC_OCSR1_PKTRDY
) && req
) {
886 s3c2410_udc_read_fifo(ep
,req
);
891 #include <asm/arch/regs-irq.h>
894 * s3c2410_udc_irq - interrupt handler
896 static irqreturn_t
s3c2410_udc_irq(int dummy
, void *_dev
)
898 struct s3c2410_udc
*dev
= _dev
;
907 spin_lock_irqsave(&dev
->lock
, flags
);
909 /* Driver connected ? */
911 /* Clear interrupts */
912 udc_write(udc_read(S3C2410_UDC_USB_INT_REG
),
913 S3C2410_UDC_USB_INT_REG
);
914 udc_write(udc_read(S3C2410_UDC_EP_INT_REG
),
915 S3C2410_UDC_EP_INT_REG
);
919 idx
= udc_read(S3C2410_UDC_INDEX_REG
);
921 /* Read status registers */
922 usb_status
= udc_read(S3C2410_UDC_USB_INT_REG
);
923 usbd_status
= udc_read(S3C2410_UDC_EP_INT_REG
);
924 pwr_reg
= udc_read(S3C2410_UDC_PWR_REG
);
926 udc_writeb(base_addr
, S3C2410_UDC_INDEX_EP0
, S3C2410_UDC_INDEX_REG
);
927 ep0csr
= udc_read(S3C2410_UDC_IN_CSR1_REG
);
929 dprintk(DEBUG_NORMAL
, "usbs=%02x, usbds=%02x, pwr=%02x ep0csr=%02x\n",
930 usb_status
, usbd_status
, pwr_reg
, ep0csr
);
933 * Now, handle interrupts. There's two types :
934 * - Reset, Resume, Suspend coming -> usb_int_reg
939 if (usb_status
& S3C2410_UDC_USBINT_RESET
) {
940 /* two kind of reset :
941 * - reset start -> pwr reg = 8
942 * - reset end -> pwr reg = 0
944 dprintk(DEBUG_NORMAL
, "USB reset csr %x pwr %x\n",
947 dev
->gadget
.speed
= USB_SPEED_UNKNOWN
;
948 udc_write(0x00, S3C2410_UDC_INDEX_REG
);
949 udc_write((dev
->ep
[0].ep
.maxpacket
& 0x7ff) >> 3,
950 S3C2410_UDC_MAXP_REG
);
953 dev
->ep0state
= EP0_IDLE
;
954 dev
->gadget
.speed
= USB_SPEED_FULL
;
956 /* clear interrupt */
957 udc_write(S3C2410_UDC_USBINT_RESET
,
958 S3C2410_UDC_USB_INT_REG
);
960 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
961 spin_unlock_irqrestore(&dev
->lock
, flags
);
966 if (usb_status
& S3C2410_UDC_USBINT_RESUME
) {
967 dprintk(DEBUG_NORMAL
, "USB resume\n");
969 /* clear interrupt */
970 udc_write(S3C2410_UDC_USBINT_RESUME
,
971 S3C2410_UDC_USB_INT_REG
);
973 if (dev
->gadget
.speed
!= USB_SPEED_UNKNOWN
975 && dev
->driver
->resume
)
976 dev
->driver
->resume(&dev
->gadget
);
980 if (usb_status
& S3C2410_UDC_USBINT_SUSPEND
) {
981 dprintk(DEBUG_NORMAL
, "USB suspend\n");
983 /* clear interrupt */
984 udc_write(S3C2410_UDC_USBINT_SUSPEND
,
985 S3C2410_UDC_USB_INT_REG
);
987 if (dev
->gadget
.speed
!= USB_SPEED_UNKNOWN
989 && dev
->driver
->suspend
)
990 dev
->driver
->suspend(&dev
->gadget
);
992 dev
->ep0state
= EP0_IDLE
;
996 /* control traffic */
997 /* check on ep0csr != 0 is not a good idea as clearing in_pkt_ready
998 * generate an interrupt
1000 if (usbd_status
& S3C2410_UDC_INT_EP0
) {
1001 dprintk(DEBUG_VERBOSE
, "USB ep0 irq\n");
1002 /* Clear the interrupt bit by setting it to 1 */
1003 udc_write(S3C2410_UDC_INT_EP0
, S3C2410_UDC_EP_INT_REG
);
1004 s3c2410_udc_handle_ep0(dev
);
1007 /* endpoint data transfers */
1008 for (i
= 1; i
< S3C2410_ENDPOINTS
; i
++) {
1010 if (usbd_status
& tmp
) {
1011 dprintk(DEBUG_VERBOSE
, "USB ep%d irq\n", i
);
1013 /* Clear the interrupt bit by setting it to 1 */
1014 udc_write(tmp
, S3C2410_UDC_EP_INT_REG
);
1015 s3c2410_udc_handle_ep(&dev
->ep
[i
]);
1019 dprintk(DEBUG_VERBOSE
, "irq: %d s3c2410_udc_done.\n", IRQ_USBD
);
1021 /* Restore old index */
1022 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
1024 spin_unlock_irqrestore(&dev
->lock
, flags
);
1028 /*------------------------- s3c2410_ep_ops ----------------------------------*/
1030 static inline struct s3c2410_ep
*to_s3c2410_ep(struct usb_ep
*ep
)
1032 return container_of(ep
, struct s3c2410_ep
, ep
);
1035 static inline struct s3c2410_udc
*to_s3c2410_udc(struct usb_gadget
*gadget
)
1037 return container_of(gadget
, struct s3c2410_udc
, gadget
);
1040 static inline struct s3c2410_request
*to_s3c2410_req(struct usb_request
*req
)
1042 return container_of(req
, struct s3c2410_request
, req
);
1046 * s3c2410_udc_ep_enable
1048 static int s3c2410_udc_ep_enable(struct usb_ep
*_ep
,
1049 const struct usb_endpoint_descriptor
*desc
)
1051 struct s3c2410_udc
*dev
;
1052 struct s3c2410_ep
*ep
;
1054 unsigned long flags
;
1058 ep
= to_s3c2410_ep(_ep
);
1060 if (!_ep
|| !desc
|| ep
->desc
1061 || _ep
->name
== ep0name
1062 || desc
->bDescriptorType
!= USB_DT_ENDPOINT
)
1066 if (!dev
->driver
|| dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
1069 max
= le16_to_cpu(desc
->wMaxPacketSize
) & 0x1fff;
1071 local_irq_save (flags
);
1072 _ep
->maxpacket
= max
& 0x7ff;
1075 ep
->bEndpointAddress
= desc
->bEndpointAddress
;
1077 /* set max packet */
1078 udc_write(ep
->num
, S3C2410_UDC_INDEX_REG
);
1079 udc_write(max
>> 3, S3C2410_UDC_MAXP_REG
);
1081 /* set type, direction, address; reset fifo counters */
1082 if (desc
->bEndpointAddress
& USB_DIR_IN
) {
1083 csr1
= S3C2410_UDC_ICSR1_FFLUSH
|S3C2410_UDC_ICSR1_CLRDT
;
1084 csr2
= S3C2410_UDC_ICSR2_MODEIN
|S3C2410_UDC_ICSR2_DMAIEN
;
1086 udc_write(ep
->num
, S3C2410_UDC_INDEX_REG
);
1087 udc_write(csr1
, S3C2410_UDC_IN_CSR1_REG
);
1088 udc_write(ep
->num
, S3C2410_UDC_INDEX_REG
);
1089 udc_write(csr2
, S3C2410_UDC_IN_CSR2_REG
);
1091 /* don't flush in fifo or it will cause endpoint interrupt */
1092 csr1
= S3C2410_UDC_ICSR1_CLRDT
;
1093 csr2
= S3C2410_UDC_ICSR2_DMAIEN
;
1095 udc_write(ep
->num
, S3C2410_UDC_INDEX_REG
);
1096 udc_write(csr1
, S3C2410_UDC_IN_CSR1_REG
);
1097 udc_write(ep
->num
, S3C2410_UDC_INDEX_REG
);
1098 udc_write(csr2
, S3C2410_UDC_IN_CSR2_REG
);
1100 csr1
= S3C2410_UDC_OCSR1_FFLUSH
| S3C2410_UDC_OCSR1_CLRDT
;
1101 csr2
= S3C2410_UDC_OCSR2_DMAIEN
;
1103 udc_write(ep
->num
, S3C2410_UDC_INDEX_REG
);
1104 udc_write(csr1
, S3C2410_UDC_OUT_CSR1_REG
);
1105 udc_write(ep
->num
, S3C2410_UDC_INDEX_REG
);
1106 udc_write(csr2
, S3C2410_UDC_OUT_CSR2_REG
);
1110 int_en_reg
= udc_read(S3C2410_UDC_EP_INT_EN_REG
);
1111 udc_write(int_en_reg
| (1 << ep
->num
), S3C2410_UDC_EP_INT_EN_REG
);
1113 /* print some debug message */
1114 tmp
= desc
->bEndpointAddress
;
1115 dprintk (DEBUG_NORMAL
, "enable %s(%d) ep%x%s-blk max %02x\n",
1116 _ep
->name
,ep
->num
, tmp
,
1117 desc
->bEndpointAddress
& USB_DIR_IN
? "in" : "out", max
);
1119 local_irq_restore (flags
);
1120 s3c2410_udc_set_halt(_ep
, 0);
1126 * s3c2410_udc_ep_disable
1128 static int s3c2410_udc_ep_disable(struct usb_ep
*_ep
)
1130 struct s3c2410_ep
*ep
= to_s3c2410_ep(_ep
);
1131 unsigned long flags
;
1134 if (!_ep
|| !ep
->desc
) {
1135 dprintk(DEBUG_NORMAL
, "%s not enabled\n",
1136 _ep
? ep
->ep
.name
: NULL
);
1140 local_irq_save(flags
);
1142 dprintk(DEBUG_NORMAL
, "ep_disable: %s\n", _ep
->name
);
1147 s3c2410_udc_nuke (ep
->dev
, ep
, -ESHUTDOWN
);
1150 int_en_reg
= udc_read(S3C2410_UDC_EP_INT_EN_REG
);
1151 udc_write(int_en_reg
& ~(1<<ep
->num
), S3C2410_UDC_EP_INT_EN_REG
);
1153 local_irq_restore(flags
);
1155 dprintk(DEBUG_NORMAL
, "%s disabled\n", _ep
->name
);
1161 * s3c2410_udc_alloc_request
1163 static struct usb_request
*
1164 s3c2410_udc_alloc_request(struct usb_ep
*_ep
, gfp_t mem_flags
)
1166 struct s3c2410_request
*req
;
1168 dprintk(DEBUG_VERBOSE
,"%s(%p,%d)\n", __func__
, _ep
, mem_flags
);
1173 req
= kzalloc (sizeof(struct s3c2410_request
), mem_flags
);
1177 INIT_LIST_HEAD (&req
->queue
);
1182 * s3c2410_udc_free_request
1185 s3c2410_udc_free_request(struct usb_ep
*_ep
, struct usb_request
*_req
)
1187 struct s3c2410_ep
*ep
= to_s3c2410_ep(_ep
);
1188 struct s3c2410_request
*req
= to_s3c2410_req(_req
);
1190 dprintk(DEBUG_VERBOSE
, "%s(%p,%p)\n", __func__
, _ep
, _req
);
1192 if (!ep
|| !_req
|| (!ep
->desc
&& _ep
->name
!= ep0name
))
1195 WARN_ON (!list_empty (&req
->queue
));
1202 static int s3c2410_udc_queue(struct usb_ep
*_ep
, struct usb_request
*_req
,
1205 struct s3c2410_request
*req
= to_s3c2410_req(_req
);
1206 struct s3c2410_ep
*ep
= to_s3c2410_ep(_ep
);
1207 struct s3c2410_udc
*dev
;
1210 unsigned long flags
;
1212 if (unlikely (!_ep
|| (!ep
->desc
&& ep
->ep
.name
!= ep0name
))) {
1213 dprintk(DEBUG_NORMAL
, "%s: invalid args\n", __func__
);
1218 if (unlikely (!dev
->driver
1219 || dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)) {
1223 local_irq_save (flags
);
1225 if (unlikely(!_req
|| !_req
->complete
1226 || !_req
->buf
|| !list_empty(&req
->queue
))) {
1228 dprintk(DEBUG_NORMAL
, "%s: 1 X X X\n", __func__
);
1230 dprintk(DEBUG_NORMAL
, "%s: 0 %01d %01d %01d\n",
1231 __func__
, !_req
->complete
,!_req
->buf
,
1232 !list_empty(&req
->queue
));
1235 local_irq_restore(flags
);
1239 _req
->status
= -EINPROGRESS
;
1242 dprintk(DEBUG_VERBOSE
, "%s: ep%x len %d\n",
1243 __func__
, ep
->bEndpointAddress
, _req
->length
);
1245 if (ep
->bEndpointAddress
) {
1246 udc_write(ep
->bEndpointAddress
& 0x7F, S3C2410_UDC_INDEX_REG
);
1248 ep_csr
= udc_read((ep
->bEndpointAddress
& USB_DIR_IN
)
1249 ? S3C2410_UDC_IN_CSR1_REG
1250 : S3C2410_UDC_OUT_CSR1_REG
);
1251 fifo_count
= s3c2410_udc_fifo_count_out();
1253 udc_write(0, S3C2410_UDC_INDEX_REG
);
1254 ep_csr
= udc_read(S3C2410_UDC_IN_CSR1_REG
);
1255 fifo_count
= s3c2410_udc_fifo_count_out();
1258 /* kickstart this i/o queue? */
1259 if (list_empty(&ep
->queue
) && !ep
->halted
) {
1260 if (ep
->bEndpointAddress
== 0 /* ep0 */) {
1261 switch (dev
->ep0state
) {
1262 case EP0_IN_DATA_PHASE
:
1263 if (!(ep_csr
&S3C2410_UDC_EP0_CSR_IPKRDY
)
1264 && s3c2410_udc_write_fifo(ep
,
1266 dev
->ep0state
= EP0_IDLE
;
1271 case EP0_OUT_DATA_PHASE
:
1273 || ((ep_csr
& S3C2410_UDC_OCSR1_PKTRDY
)
1274 && s3c2410_udc_read_fifo(ep
,
1276 dev
->ep0state
= EP0_IDLE
;
1282 local_irq_restore(flags
);
1285 } else if ((ep
->bEndpointAddress
& USB_DIR_IN
) != 0
1286 && (!(ep_csr
&S3C2410_UDC_OCSR1_PKTRDY
))
1287 && s3c2410_udc_write_fifo(ep
, req
)) {
1289 } else if ((ep_csr
& S3C2410_UDC_OCSR1_PKTRDY
)
1291 && s3c2410_udc_read_fifo(ep
, req
)) {
1296 /* pio or dma irq handler advances the queue. */
1297 if (likely (req
!= 0))
1298 list_add_tail(&req
->queue
, &ep
->queue
);
1300 local_irq_restore(flags
);
1302 dprintk(DEBUG_VERBOSE
, "%s ok\n", __func__
);
1307 * s3c2410_udc_dequeue
1309 static int s3c2410_udc_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
1311 struct s3c2410_ep
*ep
= to_s3c2410_ep(_ep
);
1312 struct s3c2410_udc
*udc
;
1313 int retval
= -EINVAL
;
1314 unsigned long flags
;
1315 struct s3c2410_request
*req
= NULL
;
1317 dprintk(DEBUG_VERBOSE
, "%s(%p,%p)\n", __func__
, _ep
, _req
);
1319 if (!the_controller
->driver
)
1325 udc
= to_s3c2410_udc(ep
->gadget
);
1327 local_irq_save (flags
);
1329 list_for_each_entry (req
, &ep
->queue
, queue
) {
1330 if (&req
->req
== _req
) {
1331 list_del_init (&req
->queue
);
1332 _req
->status
= -ECONNRESET
;
1339 dprintk(DEBUG_VERBOSE
,
1340 "dequeued req %p from %s, len %d buf %p\n",
1341 req
, _ep
->name
, _req
->length
, _req
->buf
);
1343 s3c2410_udc_done(ep
, req
, -ECONNRESET
);
1346 local_irq_restore (flags
);
1351 * s3c2410_udc_set_halt
1353 static int s3c2410_udc_set_halt(struct usb_ep
*_ep
, int value
)
1355 struct s3c2410_ep
*ep
= to_s3c2410_ep(_ep
);
1357 unsigned long flags
;
1360 if (unlikely (!_ep
|| (!ep
->desc
&& ep
->ep
.name
!= ep0name
))) {
1361 dprintk(DEBUG_NORMAL
, "%s: inval 2\n", __func__
);
1365 local_irq_save (flags
);
1367 idx
= ep
->bEndpointAddress
& 0x7F;
1370 s3c2410_udc_set_ep0_ss(base_addr
);
1371 s3c2410_udc_set_ep0_de_out(base_addr
);
1373 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
1374 ep_csr
= udc_read((ep
->bEndpointAddress
&USB_DIR_IN
)
1375 ? S3C2410_UDC_IN_CSR1_REG
1376 : S3C2410_UDC_OUT_CSR1_REG
);
1378 if ((ep
->bEndpointAddress
& USB_DIR_IN
) != 0) {
1380 udc_write(ep_csr
| S3C2410_UDC_ICSR1_SENDSTL
,
1381 S3C2410_UDC_IN_CSR1_REG
);
1383 ep_csr
&= ~S3C2410_UDC_ICSR1_SENDSTL
;
1384 udc_write(ep_csr
, S3C2410_UDC_IN_CSR1_REG
);
1385 ep_csr
|= S3C2410_UDC_ICSR1_CLRDT
;
1386 udc_write(ep_csr
, S3C2410_UDC_IN_CSR1_REG
);
1390 udc_write(ep_csr
| S3C2410_UDC_OCSR1_SENDSTL
,
1391 S3C2410_UDC_OUT_CSR1_REG
);
1393 ep_csr
&= ~S3C2410_UDC_OCSR1_SENDSTL
;
1394 udc_write(ep_csr
, S3C2410_UDC_OUT_CSR1_REG
);
1395 ep_csr
|= S3C2410_UDC_OCSR1_CLRDT
;
1396 udc_write(ep_csr
, S3C2410_UDC_OUT_CSR1_REG
);
1401 ep
->halted
= value
? 1 : 0;
1402 local_irq_restore (flags
);
1407 static const struct usb_ep_ops s3c2410_ep_ops
= {
1408 .enable
= s3c2410_udc_ep_enable
,
1409 .disable
= s3c2410_udc_ep_disable
,
1411 .alloc_request
= s3c2410_udc_alloc_request
,
1412 .free_request
= s3c2410_udc_free_request
,
1414 .queue
= s3c2410_udc_queue
,
1415 .dequeue
= s3c2410_udc_dequeue
,
1417 .set_halt
= s3c2410_udc_set_halt
,
1420 /*------------------------- usb_gadget_ops ----------------------------------*/
1423 * s3c2410_udc_get_frame
1425 static int s3c2410_udc_get_frame(struct usb_gadget
*_gadget
)
1429 dprintk(DEBUG_VERBOSE
, "%s()\n", __func__
);
1431 tmp
= udc_read(S3C2410_UDC_FRAME_NUM2_REG
) << 8;
1432 tmp
|= udc_read(S3C2410_UDC_FRAME_NUM1_REG
);
1437 * s3c2410_udc_wakeup
1439 static int s3c2410_udc_wakeup(struct usb_gadget
*_gadget
)
1441 dprintk(DEBUG_NORMAL
, "%s()\n", __func__
);
1446 * s3c2410_udc_set_selfpowered
1448 static int s3c2410_udc_set_selfpowered(struct usb_gadget
*gadget
, int value
)
1450 struct s3c2410_udc
*udc
= to_s3c2410_udc(gadget
);
1452 dprintk(DEBUG_NORMAL
, "%s()\n", __func__
);
1455 udc
->devstatus
|= (1 << USB_DEVICE_SELF_POWERED
);
1457 udc
->devstatus
&= ~(1 << USB_DEVICE_SELF_POWERED
);
1462 static void s3c2410_udc_disable(struct s3c2410_udc
*dev
);
1463 static void s3c2410_udc_enable(struct s3c2410_udc
*dev
);
1465 static int s3c2410_udc_set_pullup(struct s3c2410_udc
*udc
, int is_on
)
1467 dprintk(DEBUG_NORMAL
, "%s()\n", __func__
);
1469 if (udc_info
&& udc_info
->udc_command
) {
1471 s3c2410_udc_enable(udc
);
1473 if (udc
->gadget
.speed
!= USB_SPEED_UNKNOWN
) {
1474 if (udc
->driver
&& udc
->driver
->disconnect
)
1475 udc
->driver
->disconnect(&udc
->gadget
);
1478 s3c2410_udc_disable(udc
);
1487 static int s3c2410_udc_vbus_session(struct usb_gadget
*gadget
, int is_active
)
1489 struct s3c2410_udc
*udc
= to_s3c2410_udc(gadget
);
1491 dprintk(DEBUG_NORMAL
, "%s()\n", __func__
);
1493 udc
->vbus
= (is_active
!= 0);
1494 s3c2410_udc_set_pullup(udc
, is_active
);
1498 static int s3c2410_udc_pullup(struct usb_gadget
*gadget
, int is_on
)
1500 struct s3c2410_udc
*udc
= to_s3c2410_udc(gadget
);
1502 dprintk(DEBUG_NORMAL
, "%s()\n", __func__
);
1504 s3c2410_udc_set_pullup(udc
, is_on
? 0 : 1);
1508 static irqreturn_t
s3c2410_udc_vbus_irq(int irq
, void *_dev
)
1510 struct s3c2410_udc
*dev
= _dev
;
1513 dprintk(DEBUG_NORMAL
, "%s()\n", __func__
);
1515 /* some cpus cannot read from an line configured to IRQ! */
1516 s3c2410_gpio_cfgpin(udc_info
->vbus_pin
, S3C2410_GPIO_INPUT
);
1517 value
= s3c2410_gpio_getpin(udc_info
->vbus_pin
);
1518 s3c2410_gpio_cfgpin(udc_info
->vbus_pin
, S3C2410_GPIO_SFN2
);
1520 if (udc_info
->vbus_pin_inverted
)
1523 if (value
!= dev
->vbus
)
1524 s3c2410_udc_vbus_session(&dev
->gadget
, value
);
1529 static int s3c2410_vbus_draw(struct usb_gadget
*_gadget
, unsigned ma
)
1531 dprintk(DEBUG_NORMAL
, "%s()\n", __func__
);
1533 if (udc_info
&& udc_info
->vbus_draw
) {
1534 udc_info
->vbus_draw(ma
);
1541 static const struct usb_gadget_ops s3c2410_ops
= {
1542 .get_frame
= s3c2410_udc_get_frame
,
1543 .wakeup
= s3c2410_udc_wakeup
,
1544 .set_selfpowered
= s3c2410_udc_set_selfpowered
,
1545 .pullup
= s3c2410_udc_pullup
,
1546 .vbus_session
= s3c2410_udc_vbus_session
,
1547 .vbus_draw
= s3c2410_vbus_draw
,
1550 /*------------------------- gadget driver handling---------------------------*/
1552 * s3c2410_udc_disable
1554 static void s3c2410_udc_disable(struct s3c2410_udc
*dev
)
1556 dprintk(DEBUG_NORMAL
, "%s()\n", __func__
);
1558 /* Disable all interrupts */
1559 udc_write(0x00, S3C2410_UDC_USB_INT_EN_REG
);
1560 udc_write(0x00, S3C2410_UDC_EP_INT_EN_REG
);
1562 /* Clear the interrupt registers */
1563 udc_write(S3C2410_UDC_USBINT_RESET
1564 | S3C2410_UDC_USBINT_RESUME
1565 | S3C2410_UDC_USBINT_SUSPEND
,
1566 S3C2410_UDC_USB_INT_REG
);
1568 udc_write(0x1F, S3C2410_UDC_EP_INT_REG
);
1570 /* Good bye, cruel world */
1571 if (udc_info
&& udc_info
->udc_command
)
1572 udc_info
->udc_command(S3C2410_UDC_P_DISABLE
);
1574 /* Set speed to unknown */
1575 dev
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1579 * s3c2410_udc_reinit
1581 static void s3c2410_udc_reinit(struct s3c2410_udc
*dev
)
1585 /* device/ep0 records init */
1586 INIT_LIST_HEAD (&dev
->gadget
.ep_list
);
1587 INIT_LIST_HEAD (&dev
->gadget
.ep0
->ep_list
);
1588 dev
->ep0state
= EP0_IDLE
;
1590 for (i
= 0; i
< S3C2410_ENDPOINTS
; i
++) {
1591 struct s3c2410_ep
*ep
= &dev
->ep
[i
];
1594 list_add_tail (&ep
->ep
.ep_list
, &dev
->gadget
.ep_list
);
1599 INIT_LIST_HEAD (&ep
->queue
);
1604 * s3c2410_udc_enable
1606 static void s3c2410_udc_enable(struct s3c2410_udc
*dev
)
1610 dprintk(DEBUG_NORMAL
, "s3c2410_udc_enable called\n");
1612 /* dev->gadget.speed = USB_SPEED_UNKNOWN; */
1613 dev
->gadget
.speed
= USB_SPEED_FULL
;
1615 /* Set MAXP for all endpoints */
1616 for (i
= 0; i
< S3C2410_ENDPOINTS
; i
++) {
1617 udc_write(i
, S3C2410_UDC_INDEX_REG
);
1618 udc_write((dev
->ep
[i
].ep
.maxpacket
& 0x7ff) >> 3,
1619 S3C2410_UDC_MAXP_REG
);
1622 /* Set default power state */
1623 udc_write(DEFAULT_POWER_STATE
, S3C2410_UDC_PWR_REG
);
1625 /* Enable reset and suspend interrupt interrupts */
1626 udc_write(S3C2410_UDC_USBINT_RESET
| S3C2410_UDC_USBINT_SUSPEND
,
1627 S3C2410_UDC_USB_INT_EN_REG
);
1629 /* Enable ep0 interrupt */
1630 udc_write(S3C2410_UDC_INT_EP0
, S3C2410_UDC_EP_INT_EN_REG
);
1632 /* time to say "hello, world" */
1633 if (udc_info
&& udc_info
->udc_command
)
1634 udc_info
->udc_command(S3C2410_UDC_P_ENABLE
);
1638 * usb_gadget_register_driver
1640 int usb_gadget_register_driver(struct usb_gadget_driver
*driver
)
1642 struct s3c2410_udc
*udc
= the_controller
;
1645 dprintk(DEBUG_NORMAL
, "usb_gadget_register_driver() '%s'\n",
1646 driver
->driver
.name
);
1655 if (!driver
->bind
|| !driver
->setup
1656 || driver
->speed
!= USB_SPEED_FULL
) {
1657 printk(KERN_ERR
"Invalid driver: bind %p setup %p speed %d\n",
1658 driver
->bind
, driver
->setup
, driver
->speed
);
1662 if (!driver
->unbind
) {
1663 printk(KERN_ERR
"Invalid driver: no unbind method\n");
1668 /* Hook the driver */
1669 udc
->driver
= driver
;
1670 udc
->gadget
.dev
.driver
= &driver
->driver
;
1672 /* Bind the driver */
1673 if ((retval
= device_add(&udc
->gadget
.dev
)) != 0) {
1674 printk(KERN_ERR
"Error in device_add() : %d\n",retval
);
1675 goto register_error
;
1678 dprintk(DEBUG_NORMAL
, "binding gadget driver '%s'\n",
1679 driver
->driver
.name
);
1681 if ((retval
= driver
->bind (&udc
->gadget
)) != 0) {
1682 device_del(&udc
->gadget
.dev
);
1683 goto register_error
;
1687 s3c2410_udc_enable(udc
);
1693 udc
->gadget
.dev
.driver
= NULL
;
1698 * usb_gadget_unregister_driver
1700 int usb_gadget_unregister_driver(struct usb_gadget_driver
*driver
)
1702 struct s3c2410_udc
*udc
= the_controller
;
1707 if (!driver
|| driver
!= udc
->driver
|| !driver
->unbind
)
1710 dprintk(DEBUG_NORMAL
,"usb_gadget_register_driver() '%s'\n",
1711 driver
->driver
.name
);
1713 if (driver
->disconnect
)
1714 driver
->disconnect(&udc
->gadget
);
1716 device_del(&udc
->gadget
.dev
);
1720 s3c2410_udc_disable(udc
);
1725 /*---------------------------------------------------------------------------*/
1726 static struct s3c2410_udc memory
= {
1728 .ops
= &s3c2410_ops
,
1729 .ep0
= &memory
.ep
[0].ep
,
1730 .name
= gadget_name
,
1736 /* control endpoint */
1741 .ops
= &s3c2410_ep_ops
,
1742 .maxpacket
= EP0_FIFO_SIZE
,
1747 /* first group of endpoints */
1752 .ops
= &s3c2410_ep_ops
,
1753 .maxpacket
= EP_FIFO_SIZE
,
1756 .fifo_size
= EP_FIFO_SIZE
,
1757 .bEndpointAddress
= 1,
1758 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1764 .ops
= &s3c2410_ep_ops
,
1765 .maxpacket
= EP_FIFO_SIZE
,
1768 .fifo_size
= EP_FIFO_SIZE
,
1769 .bEndpointAddress
= 2,
1770 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1776 .ops
= &s3c2410_ep_ops
,
1777 .maxpacket
= EP_FIFO_SIZE
,
1780 .fifo_size
= EP_FIFO_SIZE
,
1781 .bEndpointAddress
= 3,
1782 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1788 .ops
= &s3c2410_ep_ops
,
1789 .maxpacket
= EP_FIFO_SIZE
,
1792 .fifo_size
= EP_FIFO_SIZE
,
1793 .bEndpointAddress
= 4,
1794 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1800 * probe - binds to the platform device
1802 static int s3c2410_udc_probe(struct platform_device
*pdev
)
1804 struct s3c2410_udc
*udc
= &memory
;
1805 struct device
*dev
= &pdev
->dev
;
1809 dev_dbg(dev
, "%s()\n", __func__
);
1811 usb_bus_clock
= clk_get(NULL
, "usb-bus-gadget");
1812 if (IS_ERR(usb_bus_clock
)) {
1813 dev_err(dev
, "failed to get usb bus clock source\n");
1814 return PTR_ERR(usb_bus_clock
);
1817 clk_enable(usb_bus_clock
);
1819 udc_clock
= clk_get(NULL
, "usb-device");
1820 if (IS_ERR(udc_clock
)) {
1821 dev_err(dev
, "failed to get udc clock source\n");
1822 return PTR_ERR(udc_clock
);
1825 clk_enable(udc_clock
);
1829 dev_dbg(dev
, "got and enabled clocks\n");
1831 if (strncmp(pdev
->name
, "s3c2440", 7) == 0) {
1832 dev_info(dev
, "S3C2440: increasing FIFO to 128 bytes\n");
1833 memory
.ep
[1].fifo_size
= S3C2440_EP_FIFO_SIZE
;
1834 memory
.ep
[2].fifo_size
= S3C2440_EP_FIFO_SIZE
;
1835 memory
.ep
[3].fifo_size
= S3C2440_EP_FIFO_SIZE
;
1836 memory
.ep
[4].fifo_size
= S3C2440_EP_FIFO_SIZE
;
1839 spin_lock_init (&udc
->lock
);
1840 udc_info
= pdev
->dev
.platform_data
;
1842 rsrc_start
= S3C2410_PA_USBDEV
;
1843 rsrc_len
= S3C24XX_SZ_USBDEV
;
1845 if (!request_mem_region(rsrc_start
, rsrc_len
, gadget_name
))
1848 base_addr
= ioremap(rsrc_start
, rsrc_len
);
1854 device_initialize(&udc
->gadget
.dev
);
1855 udc
->gadget
.dev
.parent
= &pdev
->dev
;
1856 udc
->gadget
.dev
.dma_mask
= pdev
->dev
.dma_mask
;
1858 the_controller
= udc
;
1859 platform_set_drvdata(pdev
, udc
);
1861 s3c2410_udc_disable(udc
);
1862 s3c2410_udc_reinit(udc
);
1864 /* irq setup after old hardware state is cleaned up */
1865 retval
= request_irq(IRQ_USBD
, s3c2410_udc_irq
,
1866 IRQF_DISABLED
, gadget_name
, udc
);
1869 dev_err(dev
, "cannot get irq %i, err %d\n", IRQ_USBD
, retval
);
1874 dev_dbg(dev
, "got irq %i\n", IRQ_USBD
);
1876 if (udc_info
&& udc_info
->vbus_pin
> 0) {
1877 irq
= s3c2410_gpio_getirq(udc_info
->vbus_pin
);
1878 retval
= request_irq(irq
, s3c2410_udc_vbus_irq
,
1879 IRQF_DISABLED
| IRQF_TRIGGER_RISING
1880 | IRQF_TRIGGER_FALLING
| IRQF_SHARED
,
1884 dev_err(dev
, "can't get vbus irq %i, err %d\n",
1890 dev_dbg(dev
, "got irq %i\n", irq
);
1895 if (s3c2410_udc_debugfs_root
) {
1896 udc
->regs_info
= debugfs_create_file("registers", S_IRUGO
,
1897 s3c2410_udc_debugfs_root
,
1898 udc
, &s3c2410_udc_debugfs_fops
);
1899 if (IS_ERR(udc
->regs_info
)) {
1900 dev_warn(dev
, "debugfs file creation failed %ld\n",
1901 PTR_ERR(udc
->regs_info
));
1902 udc
->regs_info
= NULL
;
1906 dev_dbg(dev
, "probe ok\n");
1911 free_irq(IRQ_USBD
, udc
);
1915 release_mem_region(rsrc_start
, rsrc_len
);
1921 * s3c2410_udc_remove
1923 static int s3c2410_udc_remove(struct platform_device
*pdev
)
1925 struct s3c2410_udc
*udc
= platform_get_drvdata(pdev
);
1928 dev_dbg(&pdev
->dev
, "%s()\n", __func__
);
1932 debugfs_remove(udc
->regs_info
);
1934 if (udc_info
&& udc_info
->vbus_pin
> 0) {
1935 irq
= s3c2410_gpio_getirq(udc_info
->vbus_pin
);
1939 free_irq(IRQ_USBD
, udc
);
1942 release_mem_region(rsrc_start
, rsrc_len
);
1944 platform_set_drvdata(pdev
, NULL
);
1946 if (!IS_ERR(udc_clock
) && udc_clock
!= NULL
) {
1947 clk_disable(udc_clock
);
1952 if (!IS_ERR(usb_bus_clock
) && usb_bus_clock
!= NULL
) {
1953 clk_disable(usb_bus_clock
);
1954 clk_put(usb_bus_clock
);
1955 usb_bus_clock
= NULL
;
1958 dev_dbg(&pdev
->dev
, "%s: remove ok\n", __func__
);
1963 static int s3c2410_udc_suspend(struct platform_device
*pdev
, pm_message_t message
)
1965 if (udc_info
&& udc_info
->udc_command
)
1966 udc_info
->udc_command(S3C2410_UDC_P_DISABLE
);
1971 static int s3c2410_udc_resume(struct platform_device
*pdev
)
1973 if (udc_info
&& udc_info
->udc_command
)
1974 udc_info
->udc_command(S3C2410_UDC_P_ENABLE
);
1979 #define s3c2410_udc_suspend NULL
1980 #define s3c2410_udc_resume NULL
1983 static struct platform_driver udc_driver_2410
= {
1985 .name
= "s3c2410-usbgadget",
1986 .owner
= THIS_MODULE
,
1988 .probe
= s3c2410_udc_probe
,
1989 .remove
= s3c2410_udc_remove
,
1990 .suspend
= s3c2410_udc_suspend
,
1991 .resume
= s3c2410_udc_resume
,
1994 static struct platform_driver udc_driver_2440
= {
1996 .name
= "s3c2440-usbgadget",
1997 .owner
= THIS_MODULE
,
1999 .probe
= s3c2410_udc_probe
,
2000 .remove
= s3c2410_udc_remove
,
2001 .suspend
= s3c2410_udc_suspend
,
2002 .resume
= s3c2410_udc_resume
,
2005 static int __init
udc_init(void)
2009 dprintk(DEBUG_NORMAL
, "%s: version %s\n", gadget_name
, DRIVER_VERSION
);
2011 s3c2410_udc_debugfs_root
= debugfs_create_dir(gadget_name
, NULL
);
2012 if (IS_ERR(s3c2410_udc_debugfs_root
)) {
2013 printk(KERN_ERR
"%s: debugfs dir creation failed %ld\n",
2014 gadget_name
, PTR_ERR(s3c2410_udc_debugfs_root
));
2015 s3c2410_udc_debugfs_root
= NULL
;
2018 retval
= platform_driver_register(&udc_driver_2410
);
2022 retval
= platform_driver_register(&udc_driver_2440
);
2029 debugfs_remove(s3c2410_udc_debugfs_root
);
2033 static void __exit
udc_exit(void)
2035 platform_driver_unregister(&udc_driver_2410
);
2036 platform_driver_unregister(&udc_driver_2440
);
2037 debugfs_remove(s3c2410_udc_debugfs_root
);
2040 EXPORT_SYMBOL(usb_gadget_unregister_driver
);
2041 EXPORT_SYMBOL(usb_gadget_register_driver
);
2043 module_init(udc_init
);
2044 module_exit(udc_exit
);
2046 MODULE_AUTHOR(DRIVER_AUTHOR
);
2047 MODULE_DESCRIPTION(DRIVER_DESC
);
2048 MODULE_VERSION(DRIVER_VERSION
);
2049 MODULE_LICENSE("GPL");