2 * linux/drivers/usb/gadget/jz4740_udc.c
4 * Ingenic JZ4740 on-chip high speed USB device controller
6 * Copyright (C) 2006 - 2008 Ingenic Semiconductor Inc.
7 * Author: <jlwei@ingenic.cn>
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.
16 * This device has ep0, two bulk-in/interrupt-in endpoints, and one bulk-out endpoint.
18 * - Endpoint numbering is fixed: ep0, ep1in-int, ep2in-bulk, ep1out-bulk.
19 * - DMA works with bulk-in (channel 1) and bulk-out (channel 2) endpoints.
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/platform_device.h>
25 #include <linux/delay.h>
26 #include <linux/ioport.h>
27 #include <linux/slab.h>
28 #include <linux/errno.h>
29 #include <linux/init.h>
30 #include <linux/list.h>
31 #include <linux/interrupt.h>
32 #include <linux/proc_fs.h>
33 #include <linux/usb.h>
34 #include <linux/usb/gadget.h>
35 #include <linux/clk.h>
37 #include <asm/byteorder.h>
40 #include <asm/system.h>
41 #include <asm/mach-jz4740/jz4740.h>
42 #include <asm/mach-jz4740/clock.h>
44 #include "jz4740_udc.h"
46 #define JZ_REG_UDC_FADDR 0x00 /* Function Address 8-bit */
47 #define JZ_REG_UDC_POWER 0x01 /* Power Managemetn 8-bit */
48 #define JZ_REG_UDC_INTRIN 0x02 /* Interrupt IN 16-bit */
49 #define JZ_REG_UDC_INTROUT 0x04 /* Interrupt OUT 16-bit */
50 #define JZ_REG_UDC_INTRINE 0x06 /* Intr IN enable 16-bit */
51 #define JZ_REG_UDC_INTROUTE 0x08 /* Intr OUT enable 16-bit */
52 #define JZ_REG_UDC_INTRUSB 0x0a /* Interrupt USB 8-bit */
53 #define JZ_REG_UDC_INTRUSBE 0x0b /* Interrupt USB Enable 8-bit */
54 #define JZ_REG_UDC_FRAME 0x0c /* Frame number 16-bit */
55 #define JZ_REG_UDC_INDEX 0x0e /* Index register 8-bit */
56 #define JZ_REG_UDC_TESTMODE 0x0f /* USB test mode 8-bit */
58 #define JZ_REG_UDC_CSR0 0x12 /* EP0 CSR 8-bit */
59 #define JZ_REG_UDC_INMAXP 0x10 /* EP1-2 IN Max Pkt Size 16-bit */
60 #define JZ_REG_UDC_INCSR 0x12 /* EP1-2 IN CSR LSB 8/16bit */
61 #define JZ_REG_UDC_INCSRH 0x13 /* EP1-2 IN CSR MSB 8-bit */
62 #define JZ_REG_UDC_OUTMAXP 0x14 /* EP1 OUT Max Pkt Size 16-bit */
63 #define JZ_REG_UDC_OUTCSR 0x16 /* EP1 OUT CSR LSB 8/16bit */
64 #define JZ_REG_UDC_OUTCSRH 0x17 /* EP1 OUT CSR MSB 8-bit */
65 #define JZ_REG_UDC_OUTCOUNT 0x18 /* bytes in EP0/1 OUT FIFO 16-bit */
67 #define JZ_REG_UDC_EP_FIFO(x) (4 * (x) + 0x20)
69 #define JZ_REG_UDC_EPINFO 0x78 /* Endpoint information */
70 #define JZ_REG_UDC_RAMINFO 0x79 /* RAM information */
72 #define JZ_REG_UDC_INTR 0x200 /* DMA pending interrupts */
73 #define JZ_REG_UDC_CNTL1 0x204 /* DMA channel 1 control */
74 #define JZ_REG_UDC_ADDR1 0x208 /* DMA channel 1 AHB memory addr */
75 #define JZ_REG_UDC_COUNT1 0x20c /* DMA channel 1 byte count */
76 #define JZ_REG_UDC_CNTL2 0x214 /* DMA channel 2 control */
77 #define JZ_REG_UDC_ADDR2 0x218 /* DMA channel 2 AHB memory addr */
78 #define JZ_REG_UDC_COUNT2 0x21c /* DMA channel 2 byte count */
80 /* Power register bit masks */
81 #define USB_POWER_SUSPENDM 0x01
82 #define USB_POWER_RESUME 0x04
83 #define USB_POWER_HSMODE 0x10
84 #define USB_POWER_HSENAB 0x20
85 #define USB_POWER_SOFTCONN 0x40
87 /* Interrupt register bit masks */
88 #define USB_INTR_SUSPEND 0x01
89 #define USB_INTR_RESUME 0x02
90 #define USB_INTR_RESET 0x04
92 #define USB_INTR_EP0 0x0001
93 #define USB_INTR_INEP1 0x0002
94 #define USB_INTR_INEP2 0x0004
95 #define USB_INTR_OUTEP1 0x0002
98 #define USB_CSR0_OUTPKTRDY 0x01
99 #define USB_CSR0_INPKTRDY 0x02
100 #define USB_CSR0_SENTSTALL 0x04
101 #define USB_CSR0_DATAEND 0x08
102 #define USB_CSR0_SETUPEND 0x10
103 #define USB_CSR0_SENDSTALL 0x20
104 #define USB_CSR0_SVDOUTPKTRDY 0x40
105 #define USB_CSR0_SVDSETUPEND 0x80
107 /* Endpoint CSR register bits */
108 #define USB_INCSRH_AUTOSET 0x80
109 #define USB_INCSRH_ISO 0x40
110 #define USB_INCSRH_MODE 0x20
111 #define USB_INCSRH_DMAREQENAB 0x10
112 #define USB_INCSRH_DMAREQMODE 0x04
113 #define USB_INCSR_CDT 0x40
114 #define USB_INCSR_SENTSTALL 0x20
115 #define USB_INCSR_SENDSTALL 0x10
116 #define USB_INCSR_FF 0x08
117 #define USB_INCSR_UNDERRUN 0x04
118 #define USB_INCSR_FFNOTEMPT 0x02
119 #define USB_INCSR_INPKTRDY 0x01
120 #define USB_OUTCSRH_AUTOCLR 0x80
121 #define USB_OUTCSRH_ISO 0x40
122 #define USB_OUTCSRH_DMAREQENAB 0x20
123 #define USB_OUTCSRH_DNYT 0x10
124 #define USB_OUTCSRH_DMAREQMODE 0x08
125 #define USB_OUTCSR_CDT 0x80
126 #define USB_OUTCSR_SENTSTALL 0x40
127 #define USB_OUTCSR_SENDSTALL 0x20
128 #define USB_OUTCSR_FF 0x10
129 #define USB_OUTCSR_DATAERR 0x08
130 #define USB_OUTCSR_OVERRUN 0x04
131 #define USB_OUTCSR_FFFULL 0x02
132 #define USB_OUTCSR_OUTPKTRDY 0x01
134 /* Testmode register bits */
135 #define USB_TEST_SE0NAK 0x01
136 #define USB_TEST_J 0x02
137 #define USB_TEST_K 0x04
138 #define USB_TEST_PACKET 0x08
140 /* DMA control bits */
141 #define USB_CNTL_ENA 0x01
142 #define USB_CNTL_DIR_IN 0x02
143 #define USB_CNTL_MODE_1 0x04
144 #define USB_CNTL_INTR_EN 0x08
145 #define USB_CNTL_EP(n) ((n) << 4)
146 #define USB_CNTL_BURST_0 (0 << 9)
147 #define USB_CNTL_BURST_4 (1 << 9)
148 #define USB_CNTL_BURST_8 (2 << 9)
149 #define USB_CNTL_BURST_16 (3 << 9)
153 # define DEBUG(fmt,args...) do {} while(0)
157 # define DEBUG_EP0(fmt,args...) do {} while(0)
160 # define DEBUG_SETUP(fmt,args...) do {} while(0)
163 static unsigned int use_dma
= 0; /* 1: use DMA, 0: use PIO */
165 module_param(use_dma
, int, 0);
166 MODULE_PARM_DESC(use_dma
, "DMA mode enable flag");
168 struct jz4740_udc
*the_controller
;
171 * Local declarations.
173 static void jz4740_ep0_kick(struct jz4740_udc
*dev
, struct jz4740_ep
*ep
);
174 static void jz4740_handle_ep0(struct jz4740_udc
*dev
, uint32_t intr
);
176 static void done(struct jz4740_ep
*ep
, struct jz4740_request
*req
,
178 static void pio_irq_enable(struct jz4740_ep
*ep
);
179 static void pio_irq_disable(struct jz4740_ep
*ep
);
180 static void stop_activity(struct jz4740_udc
*dev
,
181 struct usb_gadget_driver
*driver
);
182 static void nuke(struct jz4740_ep
*ep
, int status
);
183 static void flush(struct jz4740_ep
*ep
);
184 static void udc_set_address(struct jz4740_udc
*dev
, unsigned char address
);
186 /*-------------------------------------------------------------------------*/
188 /* inline functions of register read/write/set/clear */
190 static inline uint8_t usb_readb(struct jz4740_udc
*udc
, size_t reg
)
192 return readb(udc
->base
+ reg
);
195 static inline uint16_t usb_readw(struct jz4740_udc
*udc
, size_t reg
)
197 return readw(udc
->base
+ reg
);
200 static inline uint32_t usb_readl(struct jz4740_udc
*udc
, size_t reg
)
202 return readl(udc
->base
+ reg
);
205 static inline void usb_writeb(struct jz4740_udc
*udc
, size_t reg
, uint8_t val
)
207 writeb(val
, udc
->base
+ reg
);
210 static inline void usb_writew(struct jz4740_udc
*udc
, size_t reg
, uint16_t val
)
212 writew(val
, udc
->base
+ reg
);
215 static inline void usb_writel(struct jz4740_udc
*udc
, size_t reg
, uint32_t val
)
217 writel(val
, udc
->base
+ reg
);
220 static inline void usb_setb(struct jz4740_udc
*udc
, size_t reg
, uint8_t mask
)
222 usb_writeb(udc
, reg
, usb_readb(udc
, reg
) | mask
);
225 static inline void usb_setw(struct jz4740_udc
*udc
, size_t reg
, uint8_t mask
)
227 usb_writew(udc
, reg
, usb_readw(udc
, reg
) | mask
);
230 static inline void usb_setl(struct jz4740_udc
*udc
, size_t reg
, uint32_t mask
)
232 usb_writel(udc
, reg
, usb_readl(udc
, reg
) | mask
);
235 static inline void usb_clearb(struct jz4740_udc
*udc
, size_t reg
, uint8_t mask
)
237 usb_writeb(udc
, reg
, usb_readb(udc
, reg
) & ~mask
);
240 static inline void usb_clearw(struct jz4740_udc
*udc
, size_t reg
, uint16_t mask
)
242 usb_writew(udc
, reg
, usb_readw(udc
, reg
) & ~mask
);
245 static inline void usb_clearl(struct jz4740_udc
*udc
, size_t reg
, uint32_t mask
)
247 usb_writel(udc
, reg
, usb_readl(udc
, reg
) & ~mask
);
250 /*-------------------------------------------------------------------------*/
252 static inline void jz_udc_set_index(struct jz4740_udc
*udc
, uint8_t index
)
254 usb_writeb(udc
, JZ_REG_UDC_INDEX
, index
);
257 static inline void jz_udc_select_ep(struct jz4740_ep
*ep
)
259 jz_udc_set_index(ep
->dev
, ep_index(ep
));
262 static inline int write_packet(struct jz4740_ep
*ep
,
263 struct jz4740_request
*req
, int max
)
266 int length
, nlong
, nbyte
;
267 DEBUG("%s:%s[%d]\n", __FILE__
, __func__
, __LINE__
);
269 buf
= req
->req
.buf
+ req
->req
.actual
;
272 length
= req
->req
.length
- req
->req
.actual
;
273 length
= min(length
, max
);
274 req
->req
.actual
+= length
;
276 DEBUG("Write %d (max %d), fifo %x\n", length
, max
, ep
->fifo
);
279 nbyte
= length
& 0x3;
281 usb_writel(ep
->dev
, ep
->fifo
, *((uint32_t *)buf
));
285 usb_writeb(ep
->dev
, ep
->fifo
, *buf
++);
291 static inline int read_packet(struct jz4740_ep
*ep
,
292 struct jz4740_request
*req
, int count
)
295 int length
, nlong
, nbyte
;
296 DEBUG("%s:%s[%d]\n", __FILE__
, __func__
, __LINE__
);
298 buf
= req
->req
.buf
+ req
->req
.actual
;
301 length
= req
->req
.length
- req
->req
.actual
;
302 length
= min(length
, count
);
303 req
->req
.actual
+= length
;
305 DEBUG("Read %d, fifo %x\n", length
, ep
->fifo
);
308 nbyte
= length
& 0x3;
310 *((uint32_t *)buf
) = usb_readl(ep
->dev
, ep
->fifo
);
314 *buf
++ = usb_readb(ep
->dev
, ep
->fifo
);
320 /*-------------------------------------------------------------------------*/
323 * udc_disable - disable USB device controller
325 static void udc_disable(struct jz4740_udc
*dev
)
327 DEBUG("%s:%s[%d]\n", __FILE__
, __func__
, __LINE__
);
329 udc_set_address(dev
, 0);
331 /* Disable interrupts */
332 usb_writew(dev
, JZ_REG_UDC_INTRINE
, 0);
333 usb_writew(dev
, JZ_REG_UDC_INTROUTE
, 0);
334 usb_writeb(dev
, JZ_REG_UDC_INTRUSBE
, 0);
337 usb_writel(dev
, JZ_REG_UDC_CNTL1
, 0);
338 usb_writel(dev
, JZ_REG_UDC_CNTL2
, 0);
340 /* Disconnect from usb */
341 usb_clearb(dev
, JZ_REG_UDC_POWER
, USB_POWER_SOFTCONN
);
343 /* Disable the USB PHY */
344 clk_disable(dev
->clk
);
346 dev
->ep0state
= WAIT_FOR_SETUP
;
347 dev
->gadget
.speed
= USB_SPEED_UNKNOWN
;
353 * udc_reinit - initialize software state
355 static void udc_reinit(struct jz4740_udc
*dev
)
358 DEBUG("%s:%s[%d]\n", __FILE__
, __func__
, __LINE__
);
360 /* device/ep0 records init */
361 INIT_LIST_HEAD(&dev
->gadget
.ep_list
);
362 INIT_LIST_HEAD(&dev
->gadget
.ep0
->ep_list
);
363 dev
->ep0state
= WAIT_FOR_SETUP
;
365 for (i
= 0; i
< UDC_MAX_ENDPOINTS
; i
++) {
366 struct jz4740_ep
*ep
= &dev
->ep
[i
];
369 list_add_tail(&ep
->ep
.ep_list
, &dev
->gadget
.ep_list
);
371 INIT_LIST_HEAD(&ep
->queue
);
378 /* until it's enabled, this UDC should be completely invisible
381 static void udc_enable(struct jz4740_udc
*dev
)
384 DEBUG("%s:%s[%d]\n", __FILE__
, __func__
, __LINE__
);
386 /* UDC state is incorrect - Added by River */
387 if (dev
->state
!= UDC_STATE_ENABLE
) {
391 dev
->gadget
.speed
= USB_SPEED_UNKNOWN
;
393 /* Flush FIFO for each */
394 for (i
= 0; i
< UDC_MAX_ENDPOINTS
; i
++) {
395 struct jz4740_ep
*ep
= &dev
->ep
[i
];
397 jz_udc_set_index(dev
, ep_index(ep
));
401 /* Set this bit to allow the UDC entering low-power mode when
402 * there are no actions on the USB bus.
403 * UDC still works during this bit was set.
405 jz4740_clock_udc_enable_auto_suspend();
407 /* Enable the USB PHY */
408 clk_enable(dev
->clk
);
410 /* Disable interrupts */
411 /* usb_writew(dev, JZ_REG_UDC_INTRINE, 0);
412 usb_writew(dev, JZ_REG_UDC_INTROUTE, 0);
413 usb_writeb(dev, JZ_REG_UDC_INTRUSBE, 0);*/
415 /* Enable interrupts */
416 usb_setw(dev
, JZ_REG_UDC_INTRINE
, USB_INTR_EP0
);
417 usb_setb(dev
, JZ_REG_UDC_INTRUSBE
, USB_INTR_RESET
);
418 /* Don't enable rest of the interrupts */
419 /* usb_setw(dev, JZ_REG_UDC_INTRINE, USB_INTR_INEP1 | USB_INTR_INEP2);
420 usb_setw(dev, JZ_REG_UDC_INTROUTE, USB_INTR_OUTEP1); */
423 /* usb_setb(dev, JZ_REG_UDC_POWER, USB_POWER_SUSPENDM); */
426 usb_setb(dev
, JZ_REG_UDC_POWER
, USB_POWER_HSENAB
);
428 /* Let host detect UDC:
429 * Software must write a 1 to the PMR:USB_POWER_SOFTCONN bit to turn this
430 * transistor on and pull the USBDP pin HIGH.
432 usb_setb(dev
, JZ_REG_UDC_POWER
, USB_POWER_SOFTCONN
);
437 /*-------------------------------------------------------------------------*/
439 /* keeping it simple:
440 * - one bus driver, initted first;
441 * - one function driver, initted second
445 * Register entry point for the peripheral controller driver.
448 int usb_gadget_register_driver(struct usb_gadget_driver
*driver
)
450 struct jz4740_udc
*dev
= the_controller
;
453 if (!driver
|| !driver
->bind
) {
465 /* hook up the driver */
466 dev
->driver
= driver
;
467 dev
->gadget
.dev
.driver
= &driver
->driver
;
469 retval
= driver
->bind(&dev
->gadget
);
471 DEBUG("%s: bind to driver %s --> error %d\n", dev
->gadget
.name
,
472 driver
->driver
.name
, retval
);
477 /* then enable host detection and ep0; and we're ready
478 * for set_configuration as well as eventual disconnect.
482 DEBUG("%s: registered gadget driver '%s'\n", dev
->gadget
.name
,
483 driver
->driver
.name
);
488 EXPORT_SYMBOL(usb_gadget_register_driver
);
490 static void stop_activity(struct jz4740_udc
*dev
,
491 struct usb_gadget_driver
*driver
)
495 DEBUG("%s:%s[%d]\n", __FILE__
, __func__
, __LINE__
);
497 /* don't disconnect drivers more than once */
498 if (dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
500 dev
->gadget
.speed
= USB_SPEED_UNKNOWN
;
502 /* prevent new request submissions, kill any outstanding requests */
503 for (i
= 0; i
< UDC_MAX_ENDPOINTS
; i
++) {
504 struct jz4740_ep
*ep
= &dev
->ep
[i
];
508 jz_udc_set_index(dev
, ep_index(ep
));
509 nuke(ep
, -ESHUTDOWN
);
512 /* report disconnect; the driver is already quiesced */
514 spin_unlock(&dev
->lock
);
515 driver
->disconnect(&dev
->gadget
);
516 spin_lock(&dev
->lock
);
519 /* re-init driver-visible data structures */
525 * Unregister entry point for the peripheral controller driver.
527 int usb_gadget_unregister_driver(struct usb_gadget_driver
*driver
)
529 struct jz4740_udc
*dev
= the_controller
;
531 DEBUG("%s:%s[%d]\n", __FILE__
, __func__
, __LINE__
);
535 if (!driver
|| driver
!= dev
->driver
)
540 spin_lock_irqsave(&dev
->lock
, flags
);
542 stop_activity(dev
, driver
);
543 spin_unlock_irqrestore(&dev
->lock
, flags
);
545 driver
->unbind(&dev
->gadget
);
549 DEBUG("unregistered driver '%s'\n", driver
->driver
.name
);
554 EXPORT_SYMBOL(usb_gadget_unregister_driver
);
556 /*-------------------------------------------------------------------------*/
559 * Starting DMA using mode 1
561 static void kick_dma(struct jz4740_ep
*ep
, struct jz4740_request
*req
)
563 struct jz4740_udc
*dev
= ep
->dev
;
564 uint32_t count
= req
->req
.length
;
565 uint32_t physaddr
= virt_to_phys((void *)req
->req
.buf
);
567 DEBUG("%s:%s[%d]\n", __FILE__
, __func__
, __LINE__
);
569 jz_udc_select_ep(ep
);
571 if (ep_is_in(ep
)) { /* Bulk-IN transfer using DMA channel 1 */
572 ep
->reg_addr
= JZ_REG_UDC_ADDR1
;
574 dma_cache_wback_inv((unsigned long)req
->req
.buf
, count
);
578 usb_writeb(dev
, JZ_REG_UDC_INCSRH
,
579 USB_INCSRH_DMAREQENAB
| USB_INCSRH_AUTOSET
| USB_INCSRH_DMAREQMODE
);
581 usb_writel(dev
, JZ_REG_UDC_ADDR1
, physaddr
);
582 usb_writel(dev
, JZ_REG_UDC_COUNT1
, count
);
583 usb_writel(dev
, JZ_REG_UDC_CNTL1
, USB_CNTL_ENA
| USB_CNTL_DIR_IN
| USB_CNTL_MODE_1
|
584 USB_CNTL_INTR_EN
| USB_CNTL_BURST_16
| USB_CNTL_EP(ep_index(ep
)));
586 else { /* Bulk-OUT transfer using DMA channel 2 */
587 ep
->reg_addr
= JZ_REG_UDC_ADDR2
;
589 dma_cache_wback_inv((unsigned long)req
->req
.buf
, count
);
593 usb_setb(dev
, JZ_REG_UDC_OUTCSRH
,
594 USB_OUTCSRH_DMAREQENAB
| USB_OUTCSRH_AUTOCLR
| USB_OUTCSRH_DMAREQMODE
);
596 usb_writel(dev
, JZ_REG_UDC_ADDR2
, physaddr
);
597 usb_writel(dev
, JZ_REG_UDC_COUNT2
, count
);
598 usb_writel(dev
, JZ_REG_UDC_CNTL2
, USB_CNTL_ENA
| USB_CNTL_MODE_1
|
599 USB_CNTL_INTR_EN
| USB_CNTL_BURST_16
| USB_CNTL_EP(ep_index(ep
)));
603 /*-------------------------------------------------------------------------*/
605 /** Write request to FIFO (max write == maxp size)
606 * Return: 0 = still running, 1 = completed, negative = errno
607 * NOTE: INDEX register must be set for EP
609 static int write_fifo(struct jz4740_ep
*ep
, struct jz4740_request
*req
)
611 struct jz4740_udc
*dev
= ep
->dev
;
613 uint32_t physaddr
= virt_to_phys((void *)req
->req
.buf
);
615 DEBUG("%s:%s[%d]\n", __FILE__
, __func__
, __LINE__
);
616 max
= le16_to_cpu(ep
->desc
->wMaxPacketSize
);
621 /* DMA interrupt generated due to the last packet loaded into the FIFO */
623 dma_count
= usb_readl(dev
, ep
->reg_addr
) - physaddr
;
624 req
->req
.actual
+= dma_count
;
626 if (dma_count
% max
) {
627 /* If the last packet is less than MAXP, set INPKTRDY manually */
628 usb_setb(dev
, ep
->csr
, USB_INCSR_INPKTRDY
);
632 if (list_empty(&ep
->queue
)) {
637 /* advance the request queue */
638 req
= list_entry(ep
->queue
.next
, struct jz4740_request
, queue
);
645 * PIO mode handling starts here ...
648 csr
= usb_readb(dev
, ep
->csr
);
650 if (!(csr
& USB_INCSR_FFNOTEMPT
)) {
652 int is_last
, is_short
;
654 count
= write_packet(ep
, req
, max
);
655 usb_setb(dev
, ep
->csr
, USB_INCSR_INPKTRDY
);
657 /* last packet is usually short (or a zlp) */
658 if (unlikely(count
!= max
))
659 is_last
= is_short
= 1;
661 if (likely(req
->req
.length
!= req
->req
.actual
)
666 /* interrupt/iso maxpacket may not fill the fifo */
667 is_short
= unlikely(max
< ep_maxpacket(ep
));
670 DEBUG("%s: wrote %s %d bytes%s%s %d left %p\n", __FUNCTION__
,
672 is_last
? "/L" : "", is_short
? "/S" : "",
673 req
->req
.length
- req
->req
.actual
, req
);
675 /* requests complete when all IN data is in the FIFO */
678 if (list_empty(&ep
->queue
)) {
684 DEBUG("Hmm.. %d ep FIFO is not empty!\n", ep_index(ep
));
690 /** Read to request from FIFO (max read == bytes in fifo)
691 * Return: 0 = still running, 1 = completed, negative = errno
692 * NOTE: INDEX register must be set for EP
694 static int read_fifo(struct jz4740_ep
*ep
, struct jz4740_request
*req
)
696 struct jz4740_udc
*dev
= ep
->dev
;
698 unsigned count
, is_short
;
699 uint32_t physaddr
= virt_to_phys((void *)req
->req
.buf
);
704 /* DMA interrupt generated due to a packet less than MAXP loaded into the FIFO */
706 dma_count
= usb_readl(dev
, ep
->reg_addr
) - physaddr
;
707 req
->req
.actual
+= dma_count
;
709 /* Disable interrupt and DMA */
711 usb_writel(dev
, JZ_REG_UDC_CNTL2
, 0);
713 /* Read all bytes from this packet */
714 count
= usb_readw(dev
, JZ_REG_UDC_OUTCOUNT
);
715 count
= read_packet(ep
, req
, count
);
718 /* If the last packet is greater than zero, clear OUTPKTRDY manually */
719 usb_clearb(dev
, ep
->csr
, USB_OUTCSR_OUTPKTRDY
);
723 if (!list_empty(&ep
->queue
)) {
724 /* advance the request queue */
725 req
= list_entry(ep
->queue
.next
, struct jz4740_request
, queue
);
733 * PIO mode handling starts here ...
736 /* make sure there's a packet in the FIFO. */
737 csr
= usb_readb(dev
, ep
->csr
);
738 if (!(csr
& USB_OUTCSR_OUTPKTRDY
)) {
739 DEBUG("%s: Packet NOT ready!\n", __FUNCTION__
);
743 /* read all bytes from this packet */
744 count
= usb_readw(dev
, JZ_REG_UDC_OUTCOUNT
);
746 is_short
= (count
< ep
->ep
.maxpacket
);
748 count
= read_packet(ep
, req
, count
);
750 DEBUG("read %s %02x, %d bytes%s req %p %d/%d\n",
751 ep
->ep
.name
, csr
, count
,
752 is_short
? "/S" : "", req
, req
->req
.actual
, req
->req
.length
);
754 /* Clear OutPktRdy */
755 usb_clearb(dev
, ep
->csr
, USB_OUTCSR_OUTPKTRDY
);
758 if (is_short
|| req
->req
.actual
== req
->req
.length
) {
761 if (list_empty(&ep
->queue
))
766 /* finished that packet. the next one may be waiting... */
771 * done - retire a request; caller blocked irqs
772 * INDEX register is preserved to keep same
774 static void done(struct jz4740_ep
*ep
, struct jz4740_request
*req
, int status
)
776 unsigned int stopped
= ep
->stopped
;
780 DEBUG("%s, %p\n", __FUNCTION__
, ep
);
781 list_del_init(&req
->queue
);
783 if (likely(req
->req
.status
== -EINPROGRESS
))
784 req
->req
.status
= status
;
786 status
= req
->req
.status
;
788 if (status
&& status
!= -ESHUTDOWN
)
789 DEBUG("complete %s req %p stat %d len %u/%u\n",
790 ep
->ep
.name
, &req
->req
, status
,
791 req
->req
.actual
, req
->req
.length
);
793 /* don't modify queue heads during completion callback */
795 /* Read current index (completion may modify it) */
796 spin_lock_irqsave(&ep
->dev
->lock
, flags
);
797 index
= usb_readb(ep
->dev
, JZ_REG_UDC_INDEX
);
799 req
->req
.complete(&ep
->ep
, &req
->req
);
802 jz_udc_set_index(ep
->dev
, index
);
803 spin_unlock_irqrestore(&ep
->dev
->lock
, flags
);
804 ep
->stopped
= stopped
;
807 /** Enable EP interrupt */
808 static void pio_irq_enable(struct jz4740_ep
*ep
)
810 uint8_t index
= ep_index(ep
);
811 struct jz4740_udc
*dev
= ep
->dev
;
812 DEBUG("%s: EP%d %s\n", __FUNCTION__
, ep_index(ep
), ep_is_in(ep
) ? "IN": "OUT");
818 usb_setw(dev
, JZ_REG_UDC_INTRINE
, BIT(index
));
819 dev
->in_mask
|= BIT(index
);
822 DEBUG("Unknown endpoint: %d\n", index
);
829 usb_setw(dev
, JZ_REG_UDC_INTROUTE
, BIT(index
));
830 dev
->out_mask
|= BIT(index
);
833 DEBUG("Unknown endpoint: %d\n", index
);
839 /** Disable EP interrupt */
840 static void pio_irq_disable(struct jz4740_ep
*ep
)
842 uint8_t index
= ep_index(ep
);
843 struct jz4740_udc
*dev
= ep
->dev
;
845 DEBUG("%s: EP%d %s\n", __FUNCTION__
, ep_index(ep
), ep_is_in(ep
) ? "IN": "OUT");
848 switch (ep_index(ep
)) {
851 usb_clearw(ep
->dev
, JZ_REG_UDC_INTRINE
, BIT(index
));
852 dev
->in_mask
&= ~BIT(index
);
855 DEBUG("Unknown endpoint: %d\n", index
);
860 switch (ep_index(ep
)) {
862 usb_clearw(ep
->dev
, JZ_REG_UDC_INTROUTE
, BIT(index
));
863 dev
->out_mask
&= ~BIT(index
);
866 DEBUG("Unknown endpoint: %d\n", index
);
873 * nuke - dequeue ALL requests
875 static void nuke(struct jz4740_ep
*ep
, int status
)
877 struct jz4740_request
*req
;
879 DEBUG("%s, %p\n", __FUNCTION__
, ep
);
884 /* called with irqs blocked */
885 while (!list_empty(&ep
->queue
)) {
886 req
= list_entry(ep
->queue
.next
, struct jz4740_request
, queue
);
887 done(ep
, req
, status
);
890 /* Disable IRQ if EP is enabled (has descriptor) */
896 * NOTE: INDEX register must be set before this call
898 static void flush(struct jz4740_ep
*ep
)
900 DEBUG("%s: %s\n", __FUNCTION__
, ep
->ep
.name
);
905 usb_setb(ep
->dev
, ep
->csr
, USB_INCSR_FF
);
908 usb_setb(ep
->dev
, ep
->csr
, USB_OUTCSR_FF
);
916 * jz4740_in_epn - handle IN interrupt
918 static void jz4740_in_epn(struct jz4740_udc
*dev
, uint32_t ep_idx
, uint32_t intr
)
921 struct jz4740_ep
*ep
= &dev
->ep
[ep_idx
+ 1];
922 struct jz4740_request
*req
;
923 DEBUG("%s:%s[%d]\n", __FILE__
, __func__
, __LINE__
);
925 jz_udc_set_index(dev
, ep_index(ep
));
927 csr
= usb_readb(dev
, ep
->csr
);
928 DEBUG("%s: %d, csr %x\n", __FUNCTION__
, ep_idx
, csr
);
930 if (csr
& USB_INCSR_SENTSTALL
) {
931 DEBUG("USB_INCSR_SENTSTALL\n");
932 usb_clearb(dev
, ep
->csr
, USB_INCSR_SENTSTALL
);
937 DEBUG("%s: NO EP DESC\n", __FUNCTION__
);
941 if (list_empty(&ep
->queue
))
944 req
= list_entry(ep
->queue
.next
, struct jz4740_request
, queue
);
946 DEBUG("req: %p\n", req
);
957 static void jz4740_out_epn(struct jz4740_udc
*dev
, uint32_t ep_idx
, uint32_t intr
)
959 struct jz4740_ep
*ep
= &dev
->ep
[ep_idx
];
960 struct jz4740_request
*req
;
962 DEBUG("%s: %d\n", __FUNCTION__
, ep_idx
);
964 jz_udc_set_index(dev
, ep_index(ep
));
969 /* DMA starts here ... */
970 if (list_empty(&ep
->queue
))
973 req
= list_entry(ep
->queue
.next
, struct jz4740_request
, queue
);
981 * PIO mode starts here ...
984 while ((csr
= usb_readb(dev
, ep
->csr
)) &
985 (USB_OUTCSR_OUTPKTRDY
| USB_OUTCSR_SENTSTALL
)) {
986 DEBUG("%s: %x\n", __FUNCTION__
, csr
);
988 if (csr
& USB_OUTCSR_SENTSTALL
) {
989 DEBUG("%s: stall sent, flush fifo\n",
991 /* usb_set(USB_OUT_CSR1_FIFO_FLUSH, ep->csr1); */
993 } else if (csr
& USB_OUTCSR_OUTPKTRDY
) {
994 if (list_empty(&ep
->queue
))
998 list_entry(ep
->queue
.next
,
999 struct jz4740_request
,
1003 DEBUG("%s: NULL REQ %d\n",
1004 __FUNCTION__
, ep_idx
);
1012 /* Throw packet away.. */
1013 DEBUG("%s: ep %p ep_indx %d No descriptor?!?\n", __FUNCTION__
, ep
, ep_idx
);
1018 /** Halt specific EP
1019 * Return 0 if success
1020 * NOTE: Sets INDEX register to EP !
1022 static int jz4740_set_halt(struct usb_ep
*_ep
, int value
)
1024 struct jz4740_udc
*dev
;
1025 struct jz4740_ep
*ep
;
1026 unsigned long flags
;
1028 DEBUG("%s:%s[%d]\n", __FILE__
, __func__
, __LINE__
);
1030 ep
= container_of(_ep
, struct jz4740_ep
, ep
);
1031 if (unlikely(!_ep
|| (!ep
->desc
&& ep
->type
!= ep_control
))) {
1032 DEBUG("%s, bad ep\n", __FUNCTION__
);
1038 spin_lock_irqsave(&dev
->lock
, flags
);
1040 jz_udc_select_ep(ep
);
1042 DEBUG("%s, ep %d, val %d\n", __FUNCTION__
, ep_index(ep
), value
);
1044 if (ep_index(ep
) == 0) {
1046 usb_setb(dev
, JZ_REG_UDC_CSR0
, USB_CSR0_SENDSTALL
);
1047 } else if (ep_is_in(ep
)) {
1048 uint32_t csr
= usb_readb(dev
, ep
->csr
);
1049 if (value
&& ((csr
& USB_INCSR_FFNOTEMPT
)
1050 || !list_empty(&ep
->queue
))) {
1052 * Attempts to halt IN endpoints will fail (returning -EAGAIN)
1053 * if any transfer requests are still queued, or if the controller
1054 * FIFO still holds bytes that the host hasn\92t collected.
1056 spin_unlock_irqrestore(&dev
->lock
, flags
);
1058 ("Attempt to halt IN endpoint failed (returning -EAGAIN) %d %d\n",
1059 (csr
& USB_INCSR_FFNOTEMPT
),
1060 !list_empty(&ep
->queue
));
1065 usb_setb(dev
, ep
->csr
, USB_INCSR_SENDSTALL
);
1068 usb_clearb(dev
, ep
->csr
, USB_INCSR_SENDSTALL
);
1069 usb_setb(dev
, ep
->csr
, USB_INCSR_CDT
);
1075 usb_setb(dev
, ep
->csr
, USB_OUTCSR_SENDSTALL
);
1078 usb_clearb(dev
, ep
->csr
, USB_OUTCSR_SENDSTALL
);
1079 usb_setb(dev
, ep
->csr
, USB_OUTCSR_CDT
);
1089 spin_unlock_irqrestore(&dev
->lock
, flags
);
1091 DEBUG("%s %s halted\n", _ep
->name
, value
== 0 ? "NOT" : "IS");
1097 static int jz4740_ep_enable(struct usb_ep
*_ep
,
1098 const struct usb_endpoint_descriptor
*desc
)
1100 struct jz4740_ep
*ep
;
1101 struct jz4740_udc
*dev
;
1102 unsigned long flags
;
1103 uint32_t max
, csrh
= 0;
1105 DEBUG("%s: trying to enable %s\n", __FUNCTION__
, _ep
->name
);
1110 ep
= container_of(_ep
, struct jz4740_ep
, ep
);
1111 if (ep
->desc
|| ep
->type
== ep_control
1112 || desc
->bDescriptorType
!= USB_DT_ENDPOINT
1113 || ep
->bEndpointAddress
!= desc
->bEndpointAddress
) {
1114 DEBUG("%s, bad ep or descriptor\n", __FUNCTION__
);
1118 /* xfer types must match, except that interrupt ~= bulk */
1119 if (ep
->bmAttributes
!= desc
->bmAttributes
1120 && ep
->bmAttributes
!= USB_ENDPOINT_XFER_BULK
1121 && desc
->bmAttributes
!= USB_ENDPOINT_XFER_INT
) {
1122 DEBUG("%s, %s type mismatch\n", __FUNCTION__
, _ep
->name
);
1127 if (!dev
->driver
|| dev
->gadget
.speed
== USB_SPEED_UNKNOWN
) {
1128 DEBUG("%s, bogus device state\n", __FUNCTION__
);
1132 max
= le16_to_cpu(desc
->wMaxPacketSize
);
1134 spin_lock_irqsave(&ep
->dev
->lock
, flags
);
1136 /* Configure the endpoint */
1137 jz_udc_set_index(dev
, desc
->bEndpointAddress
& 0x0F);
1139 usb_writew(dev
, JZ_REG_UDC_INMAXP
, max
);
1140 switch (desc
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) {
1141 case USB_ENDPOINT_XFER_BULK
:
1142 case USB_ENDPOINT_XFER_INT
:
1143 csrh
&= ~USB_INCSRH_ISO
;
1145 case USB_ENDPOINT_XFER_ISOC
:
1146 csrh
|= USB_INCSRH_ISO
;
1149 usb_writeb(dev
, JZ_REG_UDC_INCSRH
, csrh
);
1152 usb_writew(dev
, JZ_REG_UDC_OUTMAXP
, max
);
1153 switch (desc
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) {
1154 case USB_ENDPOINT_XFER_BULK
:
1155 csrh
&= ~USB_OUTCSRH_ISO
;
1157 case USB_ENDPOINT_XFER_INT
:
1158 csrh
&= ~USB_OUTCSRH_ISO
;
1159 csrh
|= USB_OUTCSRH_DNYT
;
1161 case USB_ENDPOINT_XFER_ISOC
:
1162 csrh
|= USB_OUTCSRH_ISO
;
1165 usb_writeb(dev
, JZ_REG_UDC_OUTCSRH
, csrh
);
1172 ep
->ep
.maxpacket
= max
;
1174 spin_unlock_irqrestore(&ep
->dev
->lock
, flags
);
1176 /* Reset halt state (does flush) */
1177 jz4740_set_halt(_ep
, 0);
1179 DEBUG("%s: enabled %s\n", __FUNCTION__
, _ep
->name
);
1185 * NOTE: Sets INDEX register
1187 static int jz4740_ep_disable(struct usb_ep
*_ep
)
1189 struct jz4740_ep
*ep
;
1190 unsigned long flags
;
1192 DEBUG("%s, %p\n", __FUNCTION__
, _ep
);
1194 ep
= container_of(_ep
, struct jz4740_ep
, ep
);
1195 if (!_ep
|| !ep
->desc
) {
1196 DEBUG("%s, %s not enabled\n", __FUNCTION__
,
1197 _ep
? ep
->ep
.name
: NULL
);
1201 spin_lock_irqsave(&ep
->dev
->lock
, flags
);
1203 jz_udc_select_ep(ep
);
1205 /* Nuke all pending requests (does flush) */
1206 nuke(ep
, -ESHUTDOWN
);
1208 /* Disable ep IRQ */
1209 pio_irq_disable(ep
);
1214 spin_unlock_irqrestore(&ep
->dev
->lock
, flags
);
1216 DEBUG("%s: disabled %s\n", __FUNCTION__
, _ep
->name
);
1220 static struct usb_request
*jz4740_alloc_request(struct usb_ep
*ep
, gfp_t gfp_flags
)
1222 struct jz4740_request
*req
;
1224 DEBUG("%s, %p\n", __FUNCTION__
, ep
);
1226 req
= kzalloc(sizeof(*req
), gfp_flags
);
1230 INIT_LIST_HEAD(&req
->queue
);
1235 static void jz4740_free_request(struct usb_ep
*ep
, struct usb_request
*_req
)
1237 struct jz4740_request
*req
;
1239 DEBUG("%s, %p\n", __FUNCTION__
, ep
);
1241 req
= container_of(_req
, struct jz4740_request
, req
);
1242 WARN_ON(!list_empty(&req
->queue
));
1246 /*--------------------------------------------------------------------*/
1248 /** Queue one request
1249 * Kickstart transfer if needed
1250 * NOTE: Sets INDEX register
1252 static int jz4740_queue(struct usb_ep
*_ep
, struct usb_request
*_req
,
1255 struct jz4740_request
*req
;
1256 struct jz4740_ep
*ep
;
1257 struct jz4740_udc
*dev
;
1258 unsigned long flags
;
1260 DEBUG("%s, %p\n", __FUNCTION__
, _ep
);
1262 req
= container_of(_req
, struct jz4740_request
, req
);
1264 (!_req
|| !_req
->complete
|| !_req
->buf
1265 || !list_empty(&req
->queue
))) {
1266 DEBUG("%s, bad params\n", __FUNCTION__
);
1270 ep
= container_of(_ep
, struct jz4740_ep
, ep
);
1271 if (unlikely(!_ep
|| (!ep
->desc
&& ep
->type
!= ep_control
))) {
1272 DEBUG("%s, bad ep\n", __FUNCTION__
);
1277 if (unlikely(!dev
->driver
|| dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)) {
1278 DEBUG("%s, bogus device state %p\n", __FUNCTION__
, dev
->driver
);
1282 DEBUG("%s queue req %p, len %d buf %p\n", _ep
->name
, _req
, _req
->length
,
1285 spin_lock_irqsave(&dev
->lock
, flags
);
1287 _req
->status
= -EINPROGRESS
;
1290 /* kickstart this i/o queue? */
1291 DEBUG("Add to %d Q %d %d\n", ep_index(ep
), list_empty(&ep
->queue
),
1293 if (list_empty(&ep
->queue
) && likely(!ep
->stopped
)) {
1296 if (unlikely(ep_index(ep
) == 0)) {
1298 list_add_tail(&req
->queue
, &ep
->queue
);
1299 jz4740_ep0_kick(dev
, ep
);
1301 } else if (use_dma
) {
1306 else if (ep_is_in(ep
)) {
1308 jz_udc_set_index(dev
, ep_index(ep
));
1309 csr
= usb_readb(dev
, ep
->csr
);
1311 if (!(csr
& USB_INCSR_FFNOTEMPT
)) {
1312 if (write_fifo(ep
, req
) == 1)
1317 jz_udc_set_index(dev
, ep_index(ep
));
1318 csr
= usb_readb(dev
, ep
->csr
);
1320 if (csr
& USB_OUTCSR_OUTPKTRDY
) {
1321 if (read_fifo(ep
, req
) == 1)
1327 /* pio or dma irq handler advances the queue. */
1328 if (likely(req
!= 0))
1329 list_add_tail(&req
->queue
, &ep
->queue
);
1331 spin_unlock_irqrestore(&dev
->lock
, flags
);
1336 /* dequeue JUST ONE request */
1337 static int jz4740_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
1339 struct jz4740_ep
*ep
;
1340 struct jz4740_request
*req
;
1341 unsigned long flags
;
1343 DEBUG("%s, %p\n", __FUNCTION__
, _ep
);
1345 ep
= container_of(_ep
, struct jz4740_ep
, ep
);
1346 if (!_ep
|| ep
->type
== ep_control
)
1349 spin_lock_irqsave(&ep
->dev
->lock
, flags
);
1351 /* make sure it's actually queued on this endpoint */
1352 list_for_each_entry(req
, &ep
->queue
, queue
) {
1353 if (&req
->req
== _req
)
1356 if (&req
->req
!= _req
) {
1357 spin_unlock_irqrestore(&ep
->dev
->lock
, flags
);
1360 done(ep
, req
, -ECONNRESET
);
1362 spin_unlock_irqrestore(&ep
->dev
->lock
, flags
);
1366 /** Return bytes in EP FIFO
1367 * NOTE: Sets INDEX register to EP
1369 static int jz4740_fifo_status(struct usb_ep
*_ep
)
1373 struct jz4740_ep
*ep
;
1374 unsigned long flags
;
1376 ep
= container_of(_ep
, struct jz4740_ep
, ep
);
1378 DEBUG("%s, bad ep\n", __FUNCTION__
);
1382 DEBUG("%s, %d\n", __FUNCTION__
, ep_index(ep
));
1384 /* LPD can't report unclaimed bytes from IN fifos */
1388 spin_lock_irqsave(&ep
->dev
->lock
, flags
);
1389 jz_udc_set_index(ep
->dev
, ep_index(ep
));
1391 csr
= usb_readb(ep
->dev
, ep
->csr
);
1392 if (ep
->dev
->gadget
.speed
!= USB_SPEED_UNKNOWN
||
1394 count
= usb_readw(ep
->dev
, JZ_REG_UDC_OUTCOUNT
);
1397 spin_unlock_irqrestore(&ep
->dev
->lock
, flags
);
1403 * NOTE: Sets INDEX register to EP
1405 static void jz4740_fifo_flush(struct usb_ep
*_ep
)
1407 struct jz4740_ep
*ep
;
1408 unsigned long flags
;
1410 DEBUG("%s:%s[%d]\n", __FILE__
, __func__
, __LINE__
);
1412 ep
= container_of(_ep
, struct jz4740_ep
, ep
);
1413 if (unlikely(!_ep
|| (!ep
->desc
&& ep
->type
== ep_control
))) {
1414 DEBUG("%s, bad ep\n", __FUNCTION__
);
1418 spin_lock_irqsave(&ep
->dev
->lock
, flags
);
1420 jz_udc_set_index(ep
->dev
, ep_index(ep
));
1423 spin_unlock_irqrestore(&ep
->dev
->lock
, flags
);
1426 /****************************************************************/
1427 /* End Point 0 related functions */
1428 /****************************************************************/
1430 /* return: 0 = still running, 1 = completed, negative = errno */
1431 static int write_fifo_ep0(struct jz4740_ep
*ep
, struct jz4740_request
*req
)
1437 DEBUG("%s:%s[%d]\n", __FILE__
, __func__
, __LINE__
);
1438 max
= ep_maxpacket(ep
);
1440 count
= write_packet(ep
, req
, max
);
1442 /* last packet is usually short (or a zlp) */
1443 if (unlikely(count
!= max
))
1446 if (likely(req
->req
.length
!= req
->req
.actual
) || req
->req
.zero
)
1452 DEBUG_EP0("%s: wrote %s %d bytes%s %d left %p\n", __FUNCTION__
,
1454 is_last
? "/L" : "", req
->req
.length
- req
->req
.actual
, req
);
1456 /* requests complete when all IN data is in the FIFO */
1465 static inline int jz4740_fifo_read(struct jz4740_ep
*ep
,
1466 unsigned char *cp
, int max
)
1469 int count
= usb_readw(ep
->dev
, JZ_REG_UDC_OUTCOUNT
);
1475 *cp
++ = usb_readb(ep
->dev
, ep
->fifo
);
1480 static inline void jz4740_fifo_write(struct jz4740_ep
*ep
,
1481 unsigned char *cp
, int count
)
1483 DEBUG("fifo_write: %d %d\n", ep_index(ep
), count
);
1485 usb_writeb(ep
->dev
, ep
->fifo
, *cp
++);
1488 static int read_fifo_ep0(struct jz4740_ep
*ep
, struct jz4740_request
*req
)
1490 struct jz4740_udc
*dev
= ep
->dev
;
1493 unsigned bufferspace
, count
, is_short
;
1495 DEBUG_EP0("%s\n", __FUNCTION__
);
1497 csr
= usb_readb(dev
, JZ_REG_UDC_CSR0
);
1498 if (!(csr
& USB_CSR0_OUTPKTRDY
))
1501 buf
= req
->req
.buf
+ req
->req
.actual
;
1503 bufferspace
= req
->req
.length
- req
->req
.actual
;
1505 /* read all bytes from this packet */
1506 if (likely(csr
& USB_CSR0_OUTPKTRDY
)) {
1507 count
= usb_readw(dev
, JZ_REG_UDC_OUTCOUNT
);
1508 req
->req
.actual
+= min(count
, bufferspace
);
1512 is_short
= (count
< ep
->ep
.maxpacket
);
1513 DEBUG_EP0("read %s %02x, %d bytes%s req %p %d/%d\n",
1514 ep
->ep
.name
, csr
, count
,
1515 is_short
? "/S" : "", req
, req
->req
.actual
, req
->req
.length
);
1517 while (likely(count
-- != 0)) {
1518 uint8_t byte
= (uint8_t)usb_readl(dev
, ep
->fifo
);
1520 if (unlikely(bufferspace
== 0)) {
1521 /* this happens when the driver's buffer
1522 * is smaller than what the host sent.
1523 * discard the extra data.
1525 if (req
->req
.status
!= -EOVERFLOW
)
1526 DEBUG_EP0("%s overflow %d\n", ep
->ep
.name
,
1528 req
->req
.status
= -EOVERFLOW
;
1536 if (is_short
|| req
->req
.actual
== req
->req
.length
) {
1541 /* finished that packet. the next one may be waiting... */
1546 * udc_set_address - set the USB address for this device
1549 * Called from control endpoint function after it decodes a set address setup packet.
1551 static void udc_set_address(struct jz4740_udc
*dev
, unsigned char address
)
1553 DEBUG_EP0("%s: %d\n", __FUNCTION__
, address
);
1555 dev
->usb_address
= address
;
1556 usb_writeb(dev
, JZ_REG_UDC_FADDR
, address
);
1560 * DATA_STATE_RECV (USB_CSR0_OUTPKTRDY)
1562 * set USB_CSR0_SVDOUTPKTRDY | USB_CSR0_DATAEND | USB_CSR0_SENDSTALL bits
1564 * set USB_CSR0_SVDOUTPKTRDY bit
1565 if last set USB_CSR0_DATAEND bit
1567 static void jz4740_ep0_out(struct jz4740_udc
*dev
, uint32_t csr
, int kickstart
)
1569 struct jz4740_request
*req
;
1570 struct jz4740_ep
*ep
= &dev
->ep
[0];
1573 DEBUG_EP0("%s: %x\n", __FUNCTION__
, csr
);
1575 if (list_empty(&ep
->queue
))
1578 req
= list_entry(ep
->queue
.next
, struct jz4740_request
, queue
);
1581 if (req
->req
.length
== 0) {
1582 DEBUG_EP0("ZERO LENGTH OUT!\n");
1583 usb_setb(dev
, JZ_REG_UDC_CSR0
, (USB_CSR0_SVDOUTPKTRDY
| USB_CSR0_DATAEND
));
1584 dev
->ep0state
= WAIT_FOR_SETUP
;
1586 } else if (kickstart
) {
1587 usb_setb(dev
, JZ_REG_UDC_CSR0
, (USB_CSR0_SVDOUTPKTRDY
));
1590 ret
= read_fifo_ep0(ep
, req
);
1593 DEBUG_EP0("%s: finished, waiting for status\n",
1595 usb_setb(dev
, JZ_REG_UDC_CSR0
, (USB_CSR0_SVDOUTPKTRDY
| USB_CSR0_DATAEND
));
1596 dev
->ep0state
= WAIT_FOR_SETUP
;
1598 /* Not done yet.. */
1599 DEBUG_EP0("%s: not finished\n", __FUNCTION__
);
1600 usb_setb(dev
, JZ_REG_UDC_CSR0
, USB_CSR0_SVDOUTPKTRDY
);
1603 DEBUG_EP0("NO REQ??!\n");
1610 static int jz4740_ep0_in(struct jz4740_udc
*dev
, uint32_t csr
)
1612 struct jz4740_request
*req
;
1613 struct jz4740_ep
*ep
= &dev
->ep
[0];
1614 int ret
, need_zlp
= 0;
1616 DEBUG_EP0("%s: %x\n", __FUNCTION__
, csr
);
1618 if (list_empty(&ep
->queue
))
1621 req
= list_entry(ep
->queue
.next
, struct jz4740_request
, queue
);
1624 DEBUG_EP0("%s: NULL REQ\n", __FUNCTION__
);
1628 if (req
->req
.length
== 0) {
1629 usb_setb(dev
, JZ_REG_UDC_CSR0
, (USB_CSR0_INPKTRDY
| USB_CSR0_DATAEND
));
1630 dev
->ep0state
= WAIT_FOR_SETUP
;
1634 if (req
->req
.length
- req
->req
.actual
== EP0_MAXPACKETSIZE
) {
1635 /* Next write will end with the packet size, */
1636 /* so we need zero-length-packet */
1640 ret
= write_fifo_ep0(ep
, req
);
1642 if (ret
== 1 && !need_zlp
) {
1644 DEBUG_EP0("%s: finished, waiting for status\n", __FUNCTION__
);
1646 usb_setb(dev
, JZ_REG_UDC_CSR0
, (USB_CSR0_INPKTRDY
| USB_CSR0_DATAEND
));
1647 dev
->ep0state
= WAIT_FOR_SETUP
;
1649 DEBUG_EP0("%s: not finished\n", __FUNCTION__
);
1650 usb_setb(dev
, JZ_REG_UDC_CSR0
, USB_CSR0_INPKTRDY
);
1654 DEBUG_EP0("%s: Need ZLP!\n", __FUNCTION__
);
1655 usb_setb(dev
, JZ_REG_UDC_CSR0
, USB_CSR0_INPKTRDY
);
1656 dev
->ep0state
= DATA_STATE_NEED_ZLP
;
1662 static int jz4740_handle_get_status(struct jz4740_udc
*dev
,
1663 struct usb_ctrlrequest
*ctrl
)
1665 struct jz4740_ep
*ep0
= &dev
->ep
[0];
1666 struct jz4740_ep
*qep
;
1667 int reqtype
= (ctrl
->bRequestType
& USB_RECIP_MASK
);
1670 DEBUG("%s:%s[%d]\n", __FILE__
, __func__
, __LINE__
);
1672 if (reqtype
== USB_RECIP_INTERFACE
) {
1673 /* This is not supported.
1674 * And according to the USB spec, this one does nothing..
1677 DEBUG_SETUP("GET_STATUS: USB_RECIP_INTERFACE\n");
1678 } else if (reqtype
== USB_RECIP_DEVICE
) {
1679 DEBUG_SETUP("GET_STATUS: USB_RECIP_DEVICE\n");
1680 val
|= (1 << 0); /* Self powered */
1681 /*val |= (1<<1); *//* Remote wakeup */
1682 } else if (reqtype
== USB_RECIP_ENDPOINT
) {
1683 int ep_num
= (ctrl
->wIndex
& ~USB_DIR_IN
);
1686 ("GET_STATUS: USB_RECIP_ENDPOINT (%d), ctrl->wLength = %d\n",
1687 ep_num
, ctrl
->wLength
);
1689 if (ctrl
->wLength
> 2 || ep_num
> 3)
1692 qep
= &dev
->ep
[ep_num
];
1693 if (ep_is_in(qep
) != ((ctrl
->wIndex
& USB_DIR_IN
) ? 1 : 0)
1694 && ep_index(qep
) != 0) {
1698 jz_udc_set_index(dev
, ep_index(qep
));
1700 /* Return status on next IN token */
1701 switch (qep
->type
) {
1704 (usb_readb(dev
, qep
->csr
) & USB_CSR0_SENDSTALL
) ==
1710 (usb_readb(dev
, qep
->csr
) & USB_INCSR_SENDSTALL
) ==
1711 USB_INCSR_SENDSTALL
;
1715 (usb_readb(dev
, qep
->csr
) & USB_OUTCSR_SENDSTALL
) ==
1716 USB_OUTCSR_SENDSTALL
;
1720 /* Back to EP0 index */
1721 jz_udc_set_index(dev
, 0);
1723 DEBUG_SETUP("GET_STATUS, ep: %d (%x), val = %d\n", ep_num
,
1726 DEBUG_SETUP("Unknown REQ TYPE: %d\n", reqtype
);
1730 /* Clear "out packet ready" */
1731 usb_setb(dev
, JZ_REG_UDC_CSR0
, USB_CSR0_SVDOUTPKTRDY
);
1732 /* Put status to FIFO */
1733 jz4740_fifo_write(ep0
, (uint8_t *)&val
, sizeof(val
));
1734 /* Issue "In packet ready" */
1735 usb_setb(dev
, JZ_REG_UDC_CSR0
, (USB_CSR0_INPKTRDY
| USB_CSR0_DATAEND
));
1741 * WAIT_FOR_SETUP (OUTPKTRDY)
1742 * - read data packet from EP0 FIFO
1745 * set USB_CSR0_SVDOUTPKTRDY | USB_CSR0_DATAEND | USB_CSR0_SENDSTALL bits
1747 * set USB_CSR0_SVDOUTPKTRDY | USB_CSR0_DATAEND bits
1749 static void jz4740_ep0_setup(struct jz4740_udc
*dev
, uint32_t csr
)
1751 struct jz4740_ep
*ep
= &dev
->ep
[0];
1752 struct usb_ctrlrequest ctrl
;
1755 DEBUG_SETUP("%s: %x\n", __FUNCTION__
, csr
);
1757 /* Nuke all previous transfers */
1760 /* read control req from fifo (8 bytes) */
1761 jz4740_fifo_read(ep
, (unsigned char *)&ctrl
, 8);
1763 DEBUG_SETUP("SETUP %02x.%02x v%04x i%04x l%04x\n",
1764 ctrl
.bRequestType
, ctrl
.bRequest
,
1765 ctrl
.wValue
, ctrl
.wIndex
, ctrl
.wLength
);
1767 /* Set direction of EP0 */
1768 if (likely(ctrl
.bRequestType
& USB_DIR_IN
)) {
1769 ep
->bEndpointAddress
|= USB_DIR_IN
;
1771 ep
->bEndpointAddress
&= ~USB_DIR_IN
;
1774 /* Handle some SETUP packets ourselves */
1775 switch (ctrl
.bRequest
) {
1776 case USB_REQ_SET_ADDRESS
:
1777 if (ctrl
.bRequestType
!= (USB_TYPE_STANDARD
| USB_RECIP_DEVICE
))
1780 DEBUG_SETUP("USB_REQ_SET_ADDRESS (%d)\n", ctrl
.wValue
);
1781 udc_set_address(dev
, ctrl
.wValue
);
1782 usb_setb(dev
, JZ_REG_UDC_CSR0
, (USB_CSR0_SVDOUTPKTRDY
| USB_CSR0_DATAEND
));
1785 case USB_REQ_SET_CONFIGURATION
:
1786 if (ctrl
.bRequestType
!= (USB_TYPE_STANDARD
| USB_RECIP_DEVICE
))
1789 DEBUG_SETUP("USB_REQ_SET_CONFIGURATION (%d)\n", ctrl
.wValue
);
1790 /* usb_setb(JZ_REG_UDC_CSR0, (USB_CSR0_SVDOUTPKTRDY | USB_CSR0_DATAEND));*/
1792 /* Enable RESUME and SUSPEND interrupts */
1793 usb_setb(dev
, JZ_REG_UDC_INTRUSBE
, (USB_INTR_RESUME
| USB_INTR_SUSPEND
));
1796 case USB_REQ_SET_INTERFACE
:
1797 if (ctrl
.bRequestType
!= (USB_TYPE_STANDARD
| USB_RECIP_DEVICE
))
1800 DEBUG_SETUP("USB_REQ_SET_INTERFACE (%d)\n", ctrl
.wValue
);
1801 /* usb_setb(JZ_REG_UDC_CSR0, (USB_CSR0_SVDOUTPKTRDY | USB_CSR0_DATAEND));*/
1804 case USB_REQ_GET_STATUS
:
1805 if (jz4740_handle_get_status(dev
, &ctrl
) == 0)
1808 case USB_REQ_CLEAR_FEATURE
:
1809 case USB_REQ_SET_FEATURE
:
1810 if (ctrl
.bRequestType
== USB_RECIP_ENDPOINT
) {
1811 struct jz4740_ep
*qep
;
1812 int ep_num
= (ctrl
.wIndex
& 0x0f);
1814 /* Support only HALT feature */
1815 if (ctrl
.wValue
!= 0 || ctrl
.wLength
!= 0
1816 || ep_num
> 3 || ep_num
< 1)
1819 qep
= &dev
->ep
[ep_num
];
1820 spin_unlock(&dev
->lock
);
1821 if (ctrl
.bRequest
== USB_REQ_SET_FEATURE
) {
1822 DEBUG_SETUP("SET_FEATURE (%d)\n",
1824 jz4740_set_halt(&qep
->ep
, 1);
1826 DEBUG_SETUP("CLR_FEATURE (%d)\n",
1828 jz4740_set_halt(&qep
->ep
, 0);
1830 spin_lock(&dev
->lock
);
1832 jz_udc_set_index(dev
, 0);
1834 /* Reply with a ZLP on next IN token */
1835 usb_setb(dev
, JZ_REG_UDC_CSR0
,
1836 (USB_CSR0_SVDOUTPKTRDY
| USB_CSR0_DATAEND
));
1845 /* gadget drivers see class/vendor specific requests,
1846 * {SET,GET}_{INTERFACE,DESCRIPTOR,CONFIGURATION},
1850 /* device-2-host (IN) or no data setup command, process immediately */
1851 spin_unlock(&dev
->lock
);
1853 i
= dev
->driver
->setup(&dev
->gadget
, &ctrl
);
1854 spin_lock(&dev
->lock
);
1856 if (unlikely(i
< 0)) {
1857 /* setup processing failed, force stall */
1859 (" --> ERROR: gadget setup FAILED (stalling), setup returned %d\n",
1861 jz_udc_set_index(dev
, 0);
1862 usb_setb(dev
, JZ_REG_UDC_CSR0
, (USB_CSR0_SVDOUTPKTRDY
| USB_CSR0_DATAEND
| USB_CSR0_SENDSTALL
));
1864 /* ep->stopped = 1; */
1865 dev
->ep0state
= WAIT_FOR_SETUP
;
1868 DEBUG_SETUP("gadget driver setup ok (%d)\n", ctrl
.wLength
);
1869 /* if (!ctrl.wLength) {
1870 usb_setb(JZ_REG_UDC_CSR0, USB_CSR0_SVDOUTPKTRDY);
1877 * DATA_STATE_NEED_ZLP
1879 static void jz4740_ep0_in_zlp(struct jz4740_udc
*dev
, uint32_t csr
)
1881 DEBUG_EP0("%s: %x\n", __FUNCTION__
, csr
);
1883 usb_setb(dev
, JZ_REG_UDC_CSR0
, (USB_CSR0_INPKTRDY
| USB_CSR0_DATAEND
));
1884 dev
->ep0state
= WAIT_FOR_SETUP
;
1888 * handle ep0 interrupt
1890 static void jz4740_handle_ep0(struct jz4740_udc
*dev
, uint32_t intr
)
1892 struct jz4740_ep
*ep
= &dev
->ep
[0];
1895 DEBUG("%s:%s[%d]\n", __FILE__
, __func__
, __LINE__
);
1897 jz_udc_set_index(dev
, 0);
1898 csr
= usb_readb(dev
, JZ_REG_UDC_CSR0
);
1900 DEBUG_EP0("%s: csr = %x state = \n", __FUNCTION__
, csr
);//, state_names[dev->ep0state]);
1903 * if SENT_STALL is set
1904 * - clear the SENT_STALL bit
1906 if (csr
& USB_CSR0_SENTSTALL
) {
1907 DEBUG_EP0("%s: USB_CSR0_SENTSTALL is set: %x\n", __FUNCTION__
, csr
);
1908 usb_clearb(dev
, JZ_REG_UDC_CSR0
, USB_CSR0_SENDSTALL
| USB_CSR0_SENTSTALL
);
1909 nuke(ep
, -ECONNABORTED
);
1910 dev
->ep0state
= WAIT_FOR_SETUP
;
1915 * if a transfer is in progress && INPKTRDY and OUTPKTRDY are clear
1918 * - set IN_PKT_RDY | DATA_END
1922 if (!(csr
& (USB_CSR0_INPKTRDY
| USB_CSR0_OUTPKTRDY
))) {
1923 DEBUG_EP0("%s: INPKTRDY and OUTPKTRDY are clear\n",
1926 switch (dev
->ep0state
) {
1927 case DATA_STATE_XMIT
:
1928 DEBUG_EP0("continue with DATA_STATE_XMIT\n");
1929 jz4740_ep0_in(dev
, csr
);
1931 case DATA_STATE_NEED_ZLP
:
1932 DEBUG_EP0("continue with DATA_STATE_NEED_ZLP\n");
1933 jz4740_ep0_in_zlp(dev
, csr
);
1937 // DEBUG_EP0("Odd state!! state = %s\n",
1938 // state_names[dev->ep0state]);
1939 dev
->ep0state
= WAIT_FOR_SETUP
;
1941 /* usb_setb(ep->csr, USB_CSR0_SENDSTALL); */
1948 * if SETUPEND is set
1949 * - abort the last transfer
1950 * - set SERVICED_SETUP_END_BIT
1952 if (csr
& USB_CSR0_SETUPEND
) {
1953 DEBUG_EP0("%s: USB_CSR0_SETUPEND is set: %x\n", __FUNCTION__
, csr
);
1955 usb_setb(dev
, JZ_REG_UDC_CSR0
, USB_CSR0_SVDSETUPEND
);
1957 dev
->ep0state
= WAIT_FOR_SETUP
;
1961 * if USB_CSR0_OUTPKTRDY is set
1962 * - read data packet from EP0 FIFO
1965 * set SVDOUTPKTRDY | DATAEND | SENDSTALL bits
1967 * set SVDOUTPKTRDY | DATAEND bits
1969 if (csr
& USB_CSR0_OUTPKTRDY
) {
1971 DEBUG_EP0("%s: EP0_OUT_PKT_RDY is set: %x\n", __FUNCTION__
,
1974 switch (dev
->ep0state
) {
1975 case WAIT_FOR_SETUP
:
1976 DEBUG_EP0("WAIT_FOR_SETUP\n");
1977 jz4740_ep0_setup(dev
, csr
);
1980 case DATA_STATE_RECV
:
1981 DEBUG_EP0("DATA_STATE_RECV\n");
1982 jz4740_ep0_out(dev
, csr
, 0);
1987 DEBUG_EP0("strange state!! 2. send stall? state = %d\n",
1994 static void jz4740_ep0_kick(struct jz4740_udc
*dev
, struct jz4740_ep
*ep
)
1998 jz_udc_set_index(dev
, 0);
2000 DEBUG_EP0("%s: %x\n", __FUNCTION__
, csr
);
2002 /* Clear "out packet ready" */
2005 usb_setb(dev
, JZ_REG_UDC_CSR0
, USB_CSR0_SVDOUTPKTRDY
);
2006 csr
= usb_readb(dev
, JZ_REG_UDC_CSR0
);
2007 dev
->ep0state
= DATA_STATE_XMIT
;
2008 jz4740_ep0_in(dev
, csr
);
2010 csr
= usb_readb(dev
, JZ_REG_UDC_CSR0
);
2011 dev
->ep0state
= DATA_STATE_RECV
;
2012 jz4740_ep0_out(dev
, csr
, 1);
2016 /** Handle USB RESET interrupt
2018 static void jz4740_reset_irq(struct jz4740_udc
*dev
)
2020 dev
->gadget
.speed
= (usb_readb(dev
, JZ_REG_UDC_POWER
) & USB_POWER_HSMODE
) ?
2021 USB_SPEED_HIGH
: USB_SPEED_FULL
;
2023 DEBUG_SETUP("%s: address = %d, speed = %s\n", __FUNCTION__
, dev
->usb_address
,
2024 (dev
->gadget
.speed
== USB_SPEED_HIGH
) ? "HIGH":"FULL" );
2028 * jz4740 usb device interrupt handler.
2030 static irqreturn_t
jz4740_udc_irq(int irq
, void *_dev
)
2032 struct jz4740_udc
*dev
= _dev
;
2035 uint32_t intr_usb
= usb_readb(dev
, JZ_REG_UDC_INTRUSB
) & 0x7; /* mask SOF */
2036 uint32_t intr_in
= usb_readw(dev
, JZ_REG_UDC_INTRIN
);
2037 uint32_t intr_out
= usb_readw(dev
, JZ_REG_UDC_INTROUT
);
2038 uint32_t intr_dma
= usb_readb(dev
, JZ_REG_UDC_INTR
);
2040 if (!intr_usb
&& !intr_in
&& !intr_out
&& !intr_dma
)
2044 DEBUG("intr_out=%x intr_in=%x intr_usb=%x\n",
2045 intr_out
, intr_in
, intr_usb
);
2047 spin_lock(&dev
->lock
);
2048 index
= usb_readb(dev
, JZ_REG_UDC_INDEX
);
2050 /* Check for resume from suspend mode */
2051 if ((intr_usb
& USB_INTR_RESUME
) &&
2052 (usb_readb(dev
, JZ_REG_UDC_INTRUSBE
) & USB_INTR_RESUME
)) {
2053 DEBUG("USB resume\n");
2054 dev
->driver
->resume(&dev
->gadget
); /* We have suspend(), so we must have resume() too. */
2057 /* Check for system interrupts */
2058 if (intr_usb
& USB_INTR_RESET
) {
2059 DEBUG("USB reset\n");
2060 jz4740_reset_irq(dev
);
2063 /* Check for endpoint 0 interrupt */
2064 if (intr_in
& USB_INTR_EP0
) {
2065 DEBUG("USB_INTR_EP0 (control)\n");
2066 jz4740_handle_ep0(dev
, intr_in
);
2069 /* Check for Bulk-IN DMA interrupt */
2070 if (intr_dma
& 0x1) {
2072 struct jz4740_ep
*ep
;
2073 ep_num
= (usb_readl(dev
, JZ_REG_UDC_CNTL1
) >> 4) & 0xf;
2074 ep
= &dev
->ep
[ep_num
+ 1];
2075 jz_udc_set_index(dev
, ep_num
);
2076 usb_setb(dev
, ep
->csr
, USB_INCSR_INPKTRDY
);
2077 /* jz4740_in_epn(dev, ep_num, intr_in);*/
2080 /* Check for Bulk-OUT DMA interrupt */
2081 if (intr_dma
& 0x2) {
2083 ep_num
= (usb_readl(dev
, JZ_REG_UDC_CNTL2
) >> 4) & 0xf;
2084 jz4740_out_epn(dev
, ep_num
, intr_out
);
2087 /* Check for each configured endpoint interrupt */
2088 if (intr_in
& USB_INTR_INEP1
) {
2089 DEBUG("USB_INTR_INEP1\n");
2090 jz4740_in_epn(dev
, 1, intr_in
);
2093 if (intr_in
& USB_INTR_INEP2
) {
2094 DEBUG("USB_INTR_INEP2\n");
2095 jz4740_in_epn(dev
, 2, intr_in
);
2098 if (intr_out
& USB_INTR_OUTEP1
) {
2099 DEBUG("USB_INTR_OUTEP1\n");
2100 jz4740_out_epn(dev
, 1, intr_out
);
2103 /* Check for suspend mode */
2104 if ((intr_usb
& USB_INTR_SUSPEND
) &&
2105 (usb_readb(dev
, JZ_REG_UDC_INTRUSBE
) & USB_INTR_SUSPEND
)) {
2106 DEBUG("USB suspend\n");
2107 dev
->driver
->suspend(&dev
->gadget
);
2108 /* Host unloaded from us, can do something, such as flushing
2109 the NAND block cache etc. */
2112 jz_udc_set_index(dev
, index
);
2114 spin_unlock(&dev
->lock
);
2121 /*-------------------------------------------------------------------------*/
2123 /* Common functions - Added by River */
2124 static struct jz4740_udc udc_dev
;
2126 static inline struct jz4740_udc
*gadget_to_udc(struct usb_gadget
*gadget
)
2128 return container_of(gadget
, struct jz4740_udc
, gadget
);
2132 static int jz4740_udc_get_frame(struct usb_gadget
*_gadget
)
2134 DEBUG("%s, %p\n", __FUNCTION__
, _gadget
);
2135 return usb_readw(gadget_to_udc(_gadget
), JZ_REG_UDC_FRAME
);
2138 static int jz4740_udc_wakeup(struct usb_gadget
*_gadget
)
2140 /* host may not have enabled remote wakeup */
2141 /*if ((UDCCS0 & UDCCS0_DRWF) == 0)
2142 return -EHOSTUNREACH;
2143 udc_set_mask_UDCCR(UDCCR_RSM); */
2147 static int jz4740_udc_pullup(struct usb_gadget
*_gadget
, int on
)
2149 struct jz4740_udc
*udc
= gadget_to_udc(_gadget
);
2150 unsigned long flags
;
2152 local_irq_save(flags
);
2155 udc
->state
= UDC_STATE_ENABLE
;
2158 udc
->state
= UDC_STATE_DISABLE
;
2162 local_irq_restore(flags
);
2168 static const struct usb_gadget_ops jz4740_udc_ops
= {
2169 .get_frame
= jz4740_udc_get_frame
,
2170 .wakeup
= jz4740_udc_wakeup
,
2171 .pullup
= jz4740_udc_pullup
,
2172 /* current versions must always be self-powered */
2175 static struct usb_ep_ops jz4740_ep_ops
= {
2176 .enable
= jz4740_ep_enable
,
2177 .disable
= jz4740_ep_disable
,
2179 .alloc_request
= jz4740_alloc_request
,
2180 .free_request
= jz4740_free_request
,
2182 .queue
= jz4740_queue
,
2183 .dequeue
= jz4740_dequeue
,
2185 .set_halt
= jz4740_set_halt
,
2186 .fifo_status
= jz4740_fifo_status
,
2187 .fifo_flush
= jz4740_fifo_flush
,
2191 /*-------------------------------------------------------------------------*/
2193 static struct jz4740_udc udc_dev
= {
2196 .ops
= &jz4740_udc_ops
,
2197 .ep0
= &udc_dev
.ep
[0].ep
,
2200 .init_name
= "gadget",
2204 /* control endpoint */
2208 .ops
= &jz4740_ep_ops
,
2209 .maxpacket
= EP0_MAXPACKETSIZE
,
2213 .bEndpointAddress
= 0,
2217 .fifo
= JZ_REG_UDC_EP_FIFO(0),
2218 .csr
= JZ_REG_UDC_CSR0
,
2221 /* bulk out endpoint */
2224 .name
= "ep1out-bulk",
2225 .ops
= &jz4740_ep_ops
,
2226 .maxpacket
= EPBULK_MAXPACKETSIZE
,
2230 .bEndpointAddress
= 1,
2231 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
2233 .type
= ep_bulk_out
,
2234 .fifo
= JZ_REG_UDC_EP_FIFO(1),
2235 .csr
= JZ_REG_UDC_OUTCSR
,
2238 /* bulk in endpoint */
2241 .name
= "ep1in-bulk",
2242 .ops
= &jz4740_ep_ops
,
2243 .maxpacket
= EPBULK_MAXPACKETSIZE
,
2247 .bEndpointAddress
= 1 | USB_DIR_IN
,
2248 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
2251 .fifo
= JZ_REG_UDC_EP_FIFO(1),
2252 .csr
= JZ_REG_UDC_INCSR
,
2255 /* interrupt in endpoint */
2258 .name
= "ep2in-int",
2259 .ops
= &jz4740_ep_ops
,
2260 .maxpacket
= EPINTR_MAXPACKETSIZE
,
2264 .bEndpointAddress
= 2 | USB_DIR_IN
,
2265 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
2267 .type
= ep_interrupt
,
2268 .fifo
= JZ_REG_UDC_EP_FIFO(2),
2269 .csr
= JZ_REG_UDC_INCSR
,
2273 static void gadget_release(struct device
*_dev
)
2278 static int jz4740_udc_probe(struct platform_device
*pdev
)
2280 struct jz4740_udc
*dev
= &udc_dev
;
2283 spin_lock_init(&dev
->lock
);
2284 the_controller
= dev
;
2286 dev
->dev
= &pdev
->dev
;
2287 dev_set_name(&dev
->gadget
.dev
, "gadget");
2288 dev
->gadget
.dev
.parent
= &pdev
->dev
;
2289 dev
->gadget
.dev
.dma_mask
= pdev
->dev
.dma_mask
;
2290 dev
->gadget
.dev
.release
= gadget_release
;
2292 ret
= device_register(&dev
->gadget
.dev
);
2296 dev
->clk
= clk_get(&pdev
->dev
, "udc");
2297 if (IS_ERR(dev
->clk
)) {
2298 ret
= PTR_ERR(dev
->clk
);
2299 dev_err(&pdev
->dev
, "Failed to get udc clock: %d\n", ret
);
2300 goto err_device_unregister
;
2303 platform_set_drvdata(pdev
, dev
);
2305 dev
->mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2309 dev_err(&pdev
->dev
, "Failed to get mmio memory resource\n");
2313 dev
->mem
= request_mem_region(dev
->mem
->start
, resource_size(dev
->mem
), pdev
->name
);
2317 dev_err(&pdev
->dev
, "Failed to request mmio memory region\n");
2318 goto err_device_unregister
;
2321 dev
->base
= ioremap(dev
->mem
->start
, resource_size(dev
->mem
));
2325 dev_err(&pdev
->dev
, "Failed to ioremap mmio memory\n");
2326 goto err_release_mem_region
;
2329 dev
->irq
= platform_get_irq(pdev
, 0);
2331 ret
= request_irq(dev
->irq
, jz4740_udc_irq
, IRQF_DISABLED
,
2334 dev_err(&pdev
->dev
, "Failed to request irq: %d\n", ret
);
2345 err_release_mem_region
:
2346 release_mem_region(dev
->mem
->start
, resource_size(dev
->mem
));
2349 err_device_unregister
:
2350 device_unregister(&dev
->gadget
.dev
);
2351 platform_set_drvdata(pdev
, NULL
);
2358 static int jz4740_udc_remove(struct platform_device
*pdev
)
2360 struct jz4740_udc
*dev
= platform_get_drvdata(pdev
);
2367 free_irq(dev
->irq
, dev
);
2369 release_mem_region(dev
->mem
->start
, resource_size(dev
->mem
));
2372 platform_set_drvdata(pdev
, NULL
);
2373 device_unregister(&dev
->gadget
.dev
);
2374 the_controller
= NULL
;
2381 static int jz4740_udc_suspend(struct device
*dev
)
2383 struct jz4740_udc
*udc
= dev_get_drvdata(dev
);
2385 if (udc
->state
== UDC_STATE_ENABLE
)
2391 static int jz4740_udc_resume(struct device
*dev
)
2393 struct jz4740_udc
*udc
= dev_get_drvdata(dev
);
2395 if (udc
->state
== UDC_STATE_ENABLE
)
2401 static struct dev_pm_ops jz4740_udc_pm_ops
= {
2402 .suspend
= jz4740_udc_suspend
,
2403 .resume
= jz4740_udc_resume
,
2406 #define JZ4740_UDC_PM_OPS (&jz4740_udc_pm_ops)
2410 #define JZ4740_UDC_PM_OPS NULL
2414 static struct platform_driver udc_driver
= {
2415 .probe
= jz4740_udc_probe
,
2416 .remove
= jz4740_udc_remove
,
2419 .owner
= THIS_MODULE
,
2420 .pm
= JZ4740_UDC_PM_OPS
,
2424 /*-------------------------------------------------------------------------*/
2426 static int __init
udc_init (void)
2428 return platform_driver_register(&udc_driver
);
2430 module_init(udc_init
);
2432 static void __exit
udc_exit (void)
2434 platform_driver_unregister(&udc_driver
);
2436 module_exit(udc_exit
);
2438 MODULE_DESCRIPTION("JZ4740 USB Device Controller");
2439 MODULE_AUTHOR("Wei Jianli <jlwei@ingenic.cn>");
2440 MODULE_LICENSE("GPL");