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 udc_debug
= 0; /* 0: normal mode, 1: test udc cable type mode */
165 module_param(udc_debug
, int, 0);
166 MODULE_PARM_DESC(udc_debug
, "test udc cable or power type");
168 static unsigned int use_dma
= 0; /* 1: use DMA, 0: use PIO */
170 module_param(use_dma
, int, 0);
171 MODULE_PARM_DESC(use_dma
, "DMA mode enable flag");
173 struct jz4740_udc
*the_controller
;
176 * Local declarations.
178 static void jz4740_ep0_kick(struct jz4740_udc
*dev
, struct jz4740_ep
*ep
);
179 static void jz4740_handle_ep0(struct jz4740_udc
*dev
, uint32_t intr
);
181 static void done(struct jz4740_ep
*ep
, struct jz4740_request
*req
,
183 static void pio_irq_enable(struct jz4740_ep
*ep
);
184 static void pio_irq_disable(struct jz4740_ep
*ep
);
185 static void stop_activity(struct jz4740_udc
*dev
,
186 struct usb_gadget_driver
*driver
);
187 static void nuke(struct jz4740_ep
*ep
, int status
);
188 static void flush(struct jz4740_ep
*ep
);
189 static void udc_set_address(struct jz4740_udc
*dev
, unsigned char address
);
191 /*-------------------------------------------------------------------------*/
193 /* inline functions of register read/write/set/clear */
195 static inline uint8_t usb_readb(struct jz4740_udc
*udc
, size_t reg
)
197 return readb(udc
->base
+ reg
);
200 static inline uint16_t usb_readw(struct jz4740_udc
*udc
, size_t reg
)
202 return readw(udc
->base
+ reg
);
205 static inline uint32_t usb_readl(struct jz4740_udc
*udc
, size_t reg
)
207 return readl(udc
->base
+ reg
);
210 static inline void usb_writeb(struct jz4740_udc
*udc
, size_t reg
, uint8_t val
)
212 writeb(val
, udc
->base
+ reg
);
215 static inline void usb_writew(struct jz4740_udc
*udc
, size_t reg
, uint16_t val
)
217 writew(val
, udc
->base
+ reg
);
220 static inline void usb_writel(struct jz4740_udc
*udc
, size_t reg
, uint32_t val
)
222 writel(val
, udc
->base
+ reg
);
225 static inline void usb_setb(struct jz4740_udc
*udc
, size_t reg
, uint8_t mask
)
227 usb_writeb(udc
, reg
, usb_readb(udc
, reg
) | mask
);
230 static inline void usb_setw(struct jz4740_udc
*udc
, size_t reg
, uint8_t mask
)
232 usb_writew(udc
, reg
, usb_readw(udc
, reg
) | mask
);
235 static inline void usb_setl(struct jz4740_udc
*udc
, size_t reg
, uint32_t mask
)
237 usb_writel(udc
, reg
, usb_readl(udc
, reg
) | mask
);
240 static inline void usb_clearb(struct jz4740_udc
*udc
, size_t reg
, uint8_t mask
)
242 usb_writeb(udc
, reg
, usb_readb(udc
, reg
) & ~mask
);
245 static inline void usb_clearw(struct jz4740_udc
*udc
, size_t reg
, uint16_t mask
)
247 usb_writew(udc
, reg
, usb_readw(udc
, reg
) & ~mask
);
250 static inline void usb_clearl(struct jz4740_udc
*udc
, size_t reg
, uint32_t mask
)
252 usb_writel(udc
, reg
, usb_readl(udc
, reg
) & ~mask
);
255 /*-------------------------------------------------------------------------*/
257 static inline void jz_udc_set_index(struct jz4740_udc
*udc
, uint8_t index
)
259 usb_writeb(udc
, JZ_REG_UDC_INDEX
, index
);
262 static inline void jz_udc_select_ep(struct jz4740_ep
*ep
)
264 jz_udc_set_index(ep
->dev
, ep_index(ep
));
267 static inline int write_packet(struct jz4740_ep
*ep
,
268 struct jz4740_request
*req
, int max
)
271 int length
, nlong
, nbyte
;
272 DEBUG("%s:%s[%d]\n", __FILE__
, __func__
, __LINE__
);
274 buf
= req
->req
.buf
+ req
->req
.actual
;
277 length
= req
->req
.length
- req
->req
.actual
;
278 length
= min(length
, max
);
279 req
->req
.actual
+= length
;
281 DEBUG("Write %d (max %d), fifo %x\n", length
, max
, ep
->fifo
);
284 nbyte
= length
& 0x3;
286 usb_writel(ep
->dev
, ep
->fifo
, *((uint32_t *)buf
));
290 usb_writeb(ep
->dev
, ep
->fifo
, *buf
++);
296 static inline int read_packet(struct jz4740_ep
*ep
,
297 struct jz4740_request
*req
, int count
)
300 int length
, nlong
, nbyte
;
301 DEBUG("%s:%s[%d]\n", __FILE__
, __func__
, __LINE__
);
303 buf
= req
->req
.buf
+ req
->req
.actual
;
306 length
= req
->req
.length
- req
->req
.actual
;
307 length
= min(length
, count
);
308 req
->req
.actual
+= length
;
310 DEBUG("Read %d, fifo %x\n", length
, ep
->fifo
);
313 nbyte
= length
& 0x3;
315 *((uint32_t *)buf
) = usb_readl(ep
->dev
, ep
->fifo
);
319 *buf
++ = usb_readb(ep
->dev
, ep
->fifo
);
325 /*-------------------------------------------------------------------------*/
328 * udc_disable - disable USB device controller
330 static void udc_disable(struct jz4740_udc
*dev
)
332 DEBUG("%s:%s[%d]\n", __FILE__
, __func__
, __LINE__
);
334 udc_set_address(dev
, 0);
336 /* Disable interrupts */
337 usb_writew(dev
, JZ_REG_UDC_INTRINE
, 0);
338 usb_writew(dev
, JZ_REG_UDC_INTROUTE
, 0);
339 usb_writeb(dev
, JZ_REG_UDC_INTRUSBE
, 0);
342 usb_writel(dev
, JZ_REG_UDC_CNTL1
, 0);
343 usb_writel(dev
, JZ_REG_UDC_CNTL2
, 0);
345 /* Disconnect from usb */
346 usb_clearb(dev
, JZ_REG_UDC_POWER
, USB_POWER_SOFTCONN
);
348 /* Disable the USB PHY */
349 clk_disable(dev
->clk
);
351 dev
->ep0state
= WAIT_FOR_SETUP
;
352 dev
->gadget
.speed
= USB_SPEED_UNKNOWN
;
358 * udc_reinit - initialize software state
360 static void udc_reinit(struct jz4740_udc
*dev
)
363 DEBUG("%s:%s[%d]\n", __FILE__
, __func__
, __LINE__
);
365 /* device/ep0 records init */
366 INIT_LIST_HEAD(&dev
->gadget
.ep_list
);
367 INIT_LIST_HEAD(&dev
->gadget
.ep0
->ep_list
);
368 dev
->ep0state
= WAIT_FOR_SETUP
;
370 for (i
= 0; i
< UDC_MAX_ENDPOINTS
; i
++) {
371 struct jz4740_ep
*ep
= &dev
->ep
[i
];
374 list_add_tail(&ep
->ep
.ep_list
, &dev
->gadget
.ep_list
);
376 INIT_LIST_HEAD(&ep
->queue
);
383 /* until it's enabled, this UDC should be completely invisible
386 static void udc_enable(struct jz4740_udc
*dev
)
389 DEBUG("%s:%s[%d]\n", __FILE__
, __func__
, __LINE__
);
391 /* UDC state is incorrect - Added by River */
392 if (dev
->state
!= UDC_STATE_ENABLE
) {
396 dev
->gadget
.speed
= USB_SPEED_UNKNOWN
;
398 /* Flush FIFO for each */
399 for (i
= 0; i
< UDC_MAX_ENDPOINTS
; i
++) {
400 struct jz4740_ep
*ep
= &dev
->ep
[i
];
402 jz_udc_set_index(dev
, ep_index(ep
));
406 /* Set this bit to allow the UDC entering low-power mode when
407 * there are no actions on the USB bus.
408 * UDC still works during this bit was set.
410 jz4740_clock_udc_enable_auto_suspend();
412 /* Enable the USB PHY */
413 clk_enable(dev
->clk
);
415 /* Disable interrupts */
416 /* usb_writew(dev, JZ_REG_UDC_INTRINE, 0);
417 usb_writew(dev, JZ_REG_UDC_INTROUTE, 0);
418 usb_writeb(dev, JZ_REG_UDC_INTRUSBE, 0);*/
420 /* Enable interrupts */
421 usb_setw(dev
, JZ_REG_UDC_INTRINE
, USB_INTR_EP0
);
422 usb_setb(dev
, JZ_REG_UDC_INTRUSBE
, USB_INTR_RESET
);
423 /* Don't enable rest of the interrupts */
424 /* usb_setw(dev, JZ_REG_UDC_INTRINE, USB_INTR_INEP1 | USB_INTR_INEP2);
425 usb_setw(dev, JZ_REG_UDC_INTROUTE, USB_INTR_OUTEP1); */
428 /* usb_setb(dev, JZ_REG_UDC_POWER, USB_POWER_SUSPENDM); */
431 usb_setb(dev
, JZ_REG_UDC_POWER
, USB_POWER_HSENAB
);
433 /* Let host detect UDC:
434 * Software must write a 1 to the PMR:USB_POWER_SOFTCONN bit to turn this
435 * transistor on and pull the USBDP pin HIGH.
437 usb_setb(dev
, JZ_REG_UDC_POWER
, USB_POWER_SOFTCONN
);
442 /*-------------------------------------------------------------------------*/
444 /* keeping it simple:
445 * - one bus driver, initted first;
446 * - one function driver, initted second
450 * Register entry point for the peripheral controller driver.
453 int usb_gadget_register_driver(struct usb_gadget_driver
*driver
)
455 struct jz4740_udc
*dev
= the_controller
;
458 if (!driver
|| !driver
->bind
) {
470 /* hook up the driver */
471 dev
->driver
= driver
;
472 dev
->gadget
.dev
.driver
= &driver
->driver
;
474 retval
= driver
->bind(&dev
->gadget
);
476 DEBUG("%s: bind to driver %s --> error %d\n", dev
->gadget
.name
,
477 driver
->driver
.name
, retval
);
482 /* then enable host detection and ep0; and we're ready
483 * for set_configuration as well as eventual disconnect.
487 DEBUG("%s: registered gadget driver '%s'\n", dev
->gadget
.name
,
488 driver
->driver
.name
);
493 EXPORT_SYMBOL(usb_gadget_register_driver
);
495 static void stop_activity(struct jz4740_udc
*dev
,
496 struct usb_gadget_driver
*driver
)
500 DEBUG("%s:%s[%d]\n", __FILE__
, __func__
, __LINE__
);
502 /* don't disconnect drivers more than once */
503 if (dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
505 dev
->gadget
.speed
= USB_SPEED_UNKNOWN
;
507 /* prevent new request submissions, kill any outstanding requests */
508 for (i
= 0; i
< UDC_MAX_ENDPOINTS
; i
++) {
509 struct jz4740_ep
*ep
= &dev
->ep
[i
];
513 jz_udc_set_index(dev
, ep_index(ep
));
514 nuke(ep
, -ESHUTDOWN
);
517 /* report disconnect; the driver is already quiesced */
519 spin_unlock(&dev
->lock
);
520 driver
->disconnect(&dev
->gadget
);
521 spin_lock(&dev
->lock
);
524 /* re-init driver-visible data structures */
530 * Unregister entry point for the peripheral controller driver.
532 int usb_gadget_unregister_driver(struct usb_gadget_driver
*driver
)
534 struct jz4740_udc
*dev
= the_controller
;
536 DEBUG("%s:%s[%d]\n", __FILE__
, __func__
, __LINE__
);
540 if (!driver
|| driver
!= dev
->driver
)
545 spin_lock_irqsave(&dev
->lock
, flags
);
547 stop_activity(dev
, driver
);
548 spin_unlock_irqrestore(&dev
->lock
, flags
);
550 driver
->unbind(&dev
->gadget
);
554 DEBUG("unregistered driver '%s'\n", driver
->driver
.name
);
559 EXPORT_SYMBOL(usb_gadget_unregister_driver
);
561 /*-------------------------------------------------------------------------*/
564 * Starting DMA using mode 1
566 static void kick_dma(struct jz4740_ep
*ep
, struct jz4740_request
*req
)
568 struct jz4740_udc
*dev
= ep
->dev
;
569 uint32_t count
= req
->req
.length
;
570 uint32_t physaddr
= virt_to_phys((void *)req
->req
.buf
);
572 DEBUG("%s:%s[%d]\n", __FILE__
, __func__
, __LINE__
);
574 jz_udc_select_ep(ep
);
576 if (ep_is_in(ep
)) { /* Bulk-IN transfer using DMA channel 1 */
577 ep
->reg_addr
= JZ_REG_UDC_ADDR1
;
579 dma_cache_wback_inv((unsigned long)req
->req
.buf
, count
);
583 usb_writeb(dev
, JZ_REG_UDC_INCSRH
,
584 USB_INCSRH_DMAREQENAB
| USB_INCSRH_AUTOSET
| USB_INCSRH_DMAREQMODE
);
586 usb_writel(dev
, JZ_REG_UDC_ADDR1
, physaddr
);
587 usb_writel(dev
, JZ_REG_UDC_COUNT1
, count
);
588 usb_writel(dev
, JZ_REG_UDC_CNTL1
, USB_CNTL_ENA
| USB_CNTL_DIR_IN
| USB_CNTL_MODE_1
|
589 USB_CNTL_INTR_EN
| USB_CNTL_BURST_16
| USB_CNTL_EP(ep_index(ep
)));
591 else { /* Bulk-OUT transfer using DMA channel 2 */
592 ep
->reg_addr
= JZ_REG_UDC_ADDR2
;
594 dma_cache_wback_inv((unsigned long)req
->req
.buf
, count
);
598 usb_setb(dev
, JZ_REG_UDC_OUTCSRH
,
599 USB_OUTCSRH_DMAREQENAB
| USB_OUTCSRH_AUTOCLR
| USB_OUTCSRH_DMAREQMODE
);
601 usb_writel(dev
, JZ_REG_UDC_ADDR2
, physaddr
);
602 usb_writel(dev
, JZ_REG_UDC_COUNT2
, count
);
603 usb_writel(dev
, JZ_REG_UDC_CNTL2
, USB_CNTL_ENA
| USB_CNTL_MODE_1
|
604 USB_CNTL_INTR_EN
| USB_CNTL_BURST_16
| USB_CNTL_EP(ep_index(ep
)));
608 /*-------------------------------------------------------------------------*/
610 /** Write request to FIFO (max write == maxp size)
611 * Return: 0 = still running, 1 = completed, negative = errno
612 * NOTE: INDEX register must be set for EP
614 static int write_fifo(struct jz4740_ep
*ep
, struct jz4740_request
*req
)
616 struct jz4740_udc
*dev
= ep
->dev
;
618 uint32_t physaddr
= virt_to_phys((void *)req
->req
.buf
);
620 DEBUG("%s:%s[%d]\n", __FILE__
, __func__
, __LINE__
);
621 max
= le16_to_cpu(ep
->desc
->wMaxPacketSize
);
626 /* DMA interrupt generated due to the last packet loaded into the FIFO */
628 dma_count
= usb_readl(dev
, ep
->reg_addr
) - physaddr
;
629 req
->req
.actual
+= dma_count
;
631 if (dma_count
% max
) {
632 /* If the last packet is less than MAXP, set INPKTRDY manually */
633 usb_setb(dev
, ep
->csr
, USB_INCSR_INPKTRDY
);
637 if (list_empty(&ep
->queue
)) {
642 /* advance the request queue */
643 req
= list_entry(ep
->queue
.next
, struct jz4740_request
, queue
);
650 * PIO mode handling starts here ...
653 csr
= usb_readb(dev
, ep
->csr
);
655 if (!(csr
& USB_INCSR_FFNOTEMPT
)) {
657 int is_last
, is_short
;
659 count
= write_packet(ep
, req
, max
);
660 usb_setb(dev
, ep
->csr
, USB_INCSR_INPKTRDY
);
662 /* last packet is usually short (or a zlp) */
663 if (unlikely(count
!= max
))
664 is_last
= is_short
= 1;
666 if (likely(req
->req
.length
!= req
->req
.actual
)
671 /* interrupt/iso maxpacket may not fill the fifo */
672 is_short
= unlikely(max
< ep_maxpacket(ep
));
675 DEBUG("%s: wrote %s %d bytes%s%s %d left %p\n", __FUNCTION__
,
677 is_last
? "/L" : "", is_short
? "/S" : "",
678 req
->req
.length
- req
->req
.actual
, req
);
680 /* requests complete when all IN data is in the FIFO */
683 if (list_empty(&ep
->queue
)) {
689 DEBUG("Hmm.. %d ep FIFO is not empty!\n", ep_index(ep
));
695 /** Read to request from FIFO (max read == bytes in fifo)
696 * Return: 0 = still running, 1 = completed, negative = errno
697 * NOTE: INDEX register must be set for EP
699 static int read_fifo(struct jz4740_ep
*ep
, struct jz4740_request
*req
)
701 struct jz4740_udc
*dev
= ep
->dev
;
703 unsigned count
, is_short
;
704 uint32_t physaddr
= virt_to_phys((void *)req
->req
.buf
);
709 /* DMA interrupt generated due to a packet less than MAXP loaded into the FIFO */
711 dma_count
= usb_readl(dev
, ep
->reg_addr
) - physaddr
;
712 req
->req
.actual
+= dma_count
;
714 /* Disable interrupt and DMA */
716 usb_writel(dev
, JZ_REG_UDC_CNTL2
, 0);
718 /* Read all bytes from this packet */
719 count
= usb_readw(dev
, JZ_REG_UDC_OUTCOUNT
);
720 count
= read_packet(ep
, req
, count
);
723 /* If the last packet is greater than zero, clear OUTPKTRDY manually */
724 usb_clearb(dev
, ep
->csr
, USB_OUTCSR_OUTPKTRDY
);
728 if (!list_empty(&ep
->queue
)) {
729 /* advance the request queue */
730 req
= list_entry(ep
->queue
.next
, struct jz4740_request
, queue
);
738 * PIO mode handling starts here ...
741 /* make sure there's a packet in the FIFO. */
742 csr
= usb_readb(dev
, ep
->csr
);
743 if (!(csr
& USB_OUTCSR_OUTPKTRDY
)) {
744 DEBUG("%s: Packet NOT ready!\n", __FUNCTION__
);
748 /* read all bytes from this packet */
749 count
= usb_readw(dev
, JZ_REG_UDC_OUTCOUNT
);
751 is_short
= (count
< ep
->ep
.maxpacket
);
753 count
= read_packet(ep
, req
, count
);
755 DEBUG("read %s %02x, %d bytes%s req %p %d/%d\n",
756 ep
->ep
.name
, csr
, count
,
757 is_short
? "/S" : "", req
, req
->req
.actual
, req
->req
.length
);
759 /* Clear OutPktRdy */
760 usb_clearb(dev
, ep
->csr
, USB_OUTCSR_OUTPKTRDY
);
763 if (is_short
|| req
->req
.actual
== req
->req
.length
) {
766 if (list_empty(&ep
->queue
))
771 /* finished that packet. the next one may be waiting... */
776 * done - retire a request; caller blocked irqs
777 * INDEX register is preserved to keep same
779 static void done(struct jz4740_ep
*ep
, struct jz4740_request
*req
, int status
)
781 unsigned int stopped
= ep
->stopped
;
785 DEBUG("%s, %p\n", __FUNCTION__
, ep
);
786 list_del_init(&req
->queue
);
788 if (likely(req
->req
.status
== -EINPROGRESS
))
789 req
->req
.status
= status
;
791 status
= req
->req
.status
;
793 if (status
&& status
!= -ESHUTDOWN
)
794 DEBUG("complete %s req %p stat %d len %u/%u\n",
795 ep
->ep
.name
, &req
->req
, status
,
796 req
->req
.actual
, req
->req
.length
);
798 /* don't modify queue heads during completion callback */
800 /* Read current index (completion may modify it) */
801 spin_lock_irqsave(&ep
->dev
->lock
, flags
);
802 index
= usb_readb(ep
->dev
, JZ_REG_UDC_INDEX
);
804 req
->req
.complete(&ep
->ep
, &req
->req
);
807 jz_udc_set_index(ep
->dev
, index
);
808 spin_unlock_irqrestore(&ep
->dev
->lock
, flags
);
809 ep
->stopped
= stopped
;
812 /** Enable EP interrupt */
813 static void pio_irq_enable(struct jz4740_ep
*ep
)
815 uint8_t index
= ep_index(ep
);
816 struct jz4740_udc
*dev
= ep
->dev
;
817 DEBUG("%s: EP%d %s\n", __FUNCTION__
, ep_index(ep
), ep_is_in(ep
) ? "IN": "OUT");
823 usb_setw(dev
, JZ_REG_UDC_INTRINE
, BIT(index
));
824 dev
->in_mask
|= BIT(index
);
827 DEBUG("Unknown endpoint: %d\n", index
);
834 usb_setw(dev
, JZ_REG_UDC_INTROUTE
, BIT(index
));
835 dev
->out_mask
|= BIT(index
);
838 DEBUG("Unknown endpoint: %d\n", index
);
844 /** Disable EP interrupt */
845 static void pio_irq_disable(struct jz4740_ep
*ep
)
847 uint8_t index
= ep_index(ep
);
848 struct jz4740_udc
*dev
= ep
->dev
;
850 DEBUG("%s: EP%d %s\n", __FUNCTION__
, ep_index(ep
), ep_is_in(ep
) ? "IN": "OUT");
853 switch (ep_index(ep
)) {
856 usb_clearw(ep
->dev
, JZ_REG_UDC_INTRINE
, BIT(index
));
857 dev
->in_mask
&= ~BIT(index
);
860 DEBUG("Unknown endpoint: %d\n", index
);
865 switch (ep_index(ep
)) {
867 usb_clearw(ep
->dev
, JZ_REG_UDC_INTROUTE
, BIT(index
));
868 dev
->out_mask
&= ~BIT(index
);
871 DEBUG("Unknown endpoint: %d\n", index
);
878 * nuke - dequeue ALL requests
880 static void nuke(struct jz4740_ep
*ep
, int status
)
882 struct jz4740_request
*req
;
884 DEBUG("%s, %p\n", __FUNCTION__
, ep
);
889 /* called with irqs blocked */
890 while (!list_empty(&ep
->queue
)) {
891 req
= list_entry(ep
->queue
.next
, struct jz4740_request
, queue
);
892 done(ep
, req
, status
);
895 /* Disable IRQ if EP is enabled (has descriptor) */
901 * NOTE: INDEX register must be set before this call
903 static void flush(struct jz4740_ep
*ep
)
905 DEBUG("%s: %s\n", __FUNCTION__
, ep
->ep
.name
);
910 usb_setb(ep
->dev
, ep
->csr
, USB_INCSR_FF
);
913 usb_setb(ep
->dev
, ep
->csr
, USB_OUTCSR_FF
);
921 * jz4740_in_epn - handle IN interrupt
923 static void jz4740_in_epn(struct jz4740_udc
*dev
, uint32_t ep_idx
, uint32_t intr
)
926 struct jz4740_ep
*ep
= &dev
->ep
[ep_idx
+ 1];
927 struct jz4740_request
*req
;
928 DEBUG("%s:%s[%d]\n", __FILE__
, __func__
, __LINE__
);
930 jz_udc_set_index(dev
, ep_index(ep
));
932 csr
= usb_readb(dev
, ep
->csr
);
933 DEBUG("%s: %d, csr %x\n", __FUNCTION__
, ep_idx
, csr
);
935 if (csr
& USB_INCSR_SENTSTALL
) {
936 DEBUG("USB_INCSR_SENTSTALL\n");
937 usb_clearb(dev
, ep
->csr
, USB_INCSR_SENTSTALL
);
942 DEBUG("%s: NO EP DESC\n", __FUNCTION__
);
946 if (list_empty(&ep
->queue
))
949 req
= list_entry(ep
->queue
.next
, struct jz4740_request
, queue
);
951 DEBUG("req: %p\n", req
);
962 static void jz4740_out_epn(struct jz4740_udc
*dev
, uint32_t ep_idx
, uint32_t intr
)
964 struct jz4740_ep
*ep
= &dev
->ep
[ep_idx
];
965 struct jz4740_request
*req
;
967 DEBUG("%s: %d\n", __FUNCTION__
, ep_idx
);
969 jz_udc_set_index(dev
, ep_index(ep
));
974 /* DMA starts here ... */
975 if (list_empty(&ep
->queue
))
978 req
= list_entry(ep
->queue
.next
, struct jz4740_request
, queue
);
986 * PIO mode starts here ...
989 while ((csr
= usb_readb(dev
, ep
->csr
)) &
990 (USB_OUTCSR_OUTPKTRDY
| USB_OUTCSR_SENTSTALL
)) {
991 DEBUG("%s: %x\n", __FUNCTION__
, csr
);
993 if (csr
& USB_OUTCSR_SENTSTALL
) {
994 DEBUG("%s: stall sent, flush fifo\n",
996 /* usb_set(USB_OUT_CSR1_FIFO_FLUSH, ep->csr1); */
998 } else if (csr
& USB_OUTCSR_OUTPKTRDY
) {
999 if (list_empty(&ep
->queue
))
1003 list_entry(ep
->queue
.next
,
1004 struct jz4740_request
,
1008 DEBUG("%s: NULL REQ %d\n",
1009 __FUNCTION__
, ep_idx
);
1017 /* Throw packet away.. */
1018 DEBUG("%s: ep %p ep_indx %d No descriptor?!?\n", __FUNCTION__
, ep
, ep_idx
);
1023 /** Halt specific EP
1024 * Return 0 if success
1025 * NOTE: Sets INDEX register to EP !
1027 static int jz4740_set_halt(struct usb_ep
*_ep
, int value
)
1029 struct jz4740_udc
*dev
;
1030 struct jz4740_ep
*ep
;
1031 unsigned long flags
;
1033 DEBUG("%s:%s[%d]\n", __FILE__
, __func__
, __LINE__
);
1035 ep
= container_of(_ep
, struct jz4740_ep
, ep
);
1036 if (unlikely(!_ep
|| (!ep
->desc
&& ep
->type
!= ep_control
))) {
1037 DEBUG("%s, bad ep\n", __FUNCTION__
);
1043 spin_lock_irqsave(&dev
->lock
, flags
);
1045 jz_udc_select_ep(ep
);
1047 DEBUG("%s, ep %d, val %d\n", __FUNCTION__
, ep_index(ep
), value
);
1049 if (ep_index(ep
) == 0) {
1051 usb_setb(dev
, JZ_REG_UDC_CSR0
, USB_CSR0_SENDSTALL
);
1052 } else if (ep_is_in(ep
)) {
1053 uint32_t csr
= usb_readb(dev
, ep
->csr
);
1054 if (value
&& ((csr
& USB_INCSR_FFNOTEMPT
)
1055 || !list_empty(&ep
->queue
))) {
1057 * Attempts to halt IN endpoints will fail (returning -EAGAIN)
1058 * if any transfer requests are still queued, or if the controller
1059 * FIFO still holds bytes that the host hasn\92t collected.
1061 spin_unlock_irqrestore(&dev
->lock
, flags
);
1063 ("Attempt to halt IN endpoint failed (returning -EAGAIN) %d %d\n",
1064 (csr
& USB_INCSR_FFNOTEMPT
),
1065 !list_empty(&ep
->queue
));
1070 usb_setb(dev
, ep
->csr
, USB_INCSR_SENDSTALL
);
1073 usb_clearb(dev
, ep
->csr
, USB_INCSR_SENDSTALL
);
1074 usb_setb(dev
, ep
->csr
, USB_INCSR_CDT
);
1080 usb_setb(dev
, ep
->csr
, USB_OUTCSR_SENDSTALL
);
1083 usb_clearb(dev
, ep
->csr
, USB_OUTCSR_SENDSTALL
);
1084 usb_setb(dev
, ep
->csr
, USB_OUTCSR_CDT
);
1094 spin_unlock_irqrestore(&dev
->lock
, flags
);
1096 DEBUG("%s %s halted\n", _ep
->name
, value
== 0 ? "NOT" : "IS");
1102 static int jz4740_ep_enable(struct usb_ep
*_ep
,
1103 const struct usb_endpoint_descriptor
*desc
)
1105 struct jz4740_ep
*ep
;
1106 struct jz4740_udc
*dev
;
1107 unsigned long flags
;
1108 uint32_t max
, csrh
= 0;
1110 DEBUG("%s: trying to enable %s\n", __FUNCTION__
, _ep
->name
);
1115 ep
= container_of(_ep
, struct jz4740_ep
, ep
);
1116 if (ep
->desc
|| ep
->type
== ep_control
1117 || desc
->bDescriptorType
!= USB_DT_ENDPOINT
1118 || ep
->bEndpointAddress
!= desc
->bEndpointAddress
) {
1119 DEBUG("%s, bad ep or descriptor\n", __FUNCTION__
);
1123 /* xfer types must match, except that interrupt ~= bulk */
1124 if (ep
->bmAttributes
!= desc
->bmAttributes
1125 && ep
->bmAttributes
!= USB_ENDPOINT_XFER_BULK
1126 && desc
->bmAttributes
!= USB_ENDPOINT_XFER_INT
) {
1127 DEBUG("%s, %s type mismatch\n", __FUNCTION__
, _ep
->name
);
1132 if (!dev
->driver
|| dev
->gadget
.speed
== USB_SPEED_UNKNOWN
) {
1133 DEBUG("%s, bogus device state\n", __FUNCTION__
);
1137 max
= le16_to_cpu(desc
->wMaxPacketSize
);
1139 spin_lock_irqsave(&ep
->dev
->lock
, flags
);
1141 /* Configure the endpoint */
1142 jz_udc_set_index(dev
, desc
->bEndpointAddress
& 0x0F);
1144 usb_writew(dev
, JZ_REG_UDC_INMAXP
, max
);
1145 switch (desc
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) {
1146 case USB_ENDPOINT_XFER_BULK
:
1147 case USB_ENDPOINT_XFER_INT
:
1148 csrh
&= ~USB_INCSRH_ISO
;
1150 case USB_ENDPOINT_XFER_ISOC
:
1151 csrh
|= USB_INCSRH_ISO
;
1154 usb_writeb(dev
, JZ_REG_UDC_INCSRH
, csrh
);
1157 usb_writew(dev
, JZ_REG_UDC_OUTMAXP
, max
);
1158 switch (desc
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) {
1159 case USB_ENDPOINT_XFER_BULK
:
1160 csrh
&= ~USB_OUTCSRH_ISO
;
1162 case USB_ENDPOINT_XFER_INT
:
1163 csrh
&= ~USB_OUTCSRH_ISO
;
1164 csrh
|= USB_OUTCSRH_DNYT
;
1166 case USB_ENDPOINT_XFER_ISOC
:
1167 csrh
|= USB_OUTCSRH_ISO
;
1170 usb_writeb(dev
, JZ_REG_UDC_OUTCSRH
, csrh
);
1177 ep
->ep
.maxpacket
= max
;
1179 spin_unlock_irqrestore(&ep
->dev
->lock
, flags
);
1181 /* Reset halt state (does flush) */
1182 jz4740_set_halt(_ep
, 0);
1184 DEBUG("%s: enabled %s\n", __FUNCTION__
, _ep
->name
);
1190 * NOTE: Sets INDEX register
1192 static int jz4740_ep_disable(struct usb_ep
*_ep
)
1194 struct jz4740_ep
*ep
;
1195 unsigned long flags
;
1197 DEBUG("%s, %p\n", __FUNCTION__
, _ep
);
1199 ep
= container_of(_ep
, struct jz4740_ep
, ep
);
1200 if (!_ep
|| !ep
->desc
) {
1201 DEBUG("%s, %s not enabled\n", __FUNCTION__
,
1202 _ep
? ep
->ep
.name
: NULL
);
1206 spin_lock_irqsave(&ep
->dev
->lock
, flags
);
1208 jz_udc_select_ep(ep
);
1210 /* Nuke all pending requests (does flush) */
1211 nuke(ep
, -ESHUTDOWN
);
1213 /* Disable ep IRQ */
1214 pio_irq_disable(ep
);
1219 spin_unlock_irqrestore(&ep
->dev
->lock
, flags
);
1221 DEBUG("%s: disabled %s\n", __FUNCTION__
, _ep
->name
);
1225 static struct usb_request
*jz4740_alloc_request(struct usb_ep
*ep
, gfp_t gfp_flags
)
1227 struct jz4740_request
*req
;
1229 DEBUG("%s, %p\n", __FUNCTION__
, ep
);
1231 req
= kzalloc(sizeof(*req
), gfp_flags
);
1235 INIT_LIST_HEAD(&req
->queue
);
1240 static void jz4740_free_request(struct usb_ep
*ep
, struct usb_request
*_req
)
1242 struct jz4740_request
*req
;
1244 DEBUG("%s, %p\n", __FUNCTION__
, ep
);
1246 req
= container_of(_req
, struct jz4740_request
, req
);
1247 WARN_ON(!list_empty(&req
->queue
));
1251 /*--------------------------------------------------------------------*/
1253 /** Queue one request
1254 * Kickstart transfer if needed
1255 * NOTE: Sets INDEX register
1257 static int jz4740_queue(struct usb_ep
*_ep
, struct usb_request
*_req
,
1260 struct jz4740_request
*req
;
1261 struct jz4740_ep
*ep
;
1262 struct jz4740_udc
*dev
;
1263 unsigned long flags
;
1265 DEBUG("%s, %p\n", __FUNCTION__
, _ep
);
1267 req
= container_of(_req
, struct jz4740_request
, req
);
1269 (!_req
|| !_req
->complete
|| !_req
->buf
1270 || !list_empty(&req
->queue
))) {
1271 DEBUG("%s, bad params\n", __FUNCTION__
);
1275 ep
= container_of(_ep
, struct jz4740_ep
, ep
);
1276 if (unlikely(!_ep
|| (!ep
->desc
&& ep
->type
!= ep_control
))) {
1277 DEBUG("%s, bad ep\n", __FUNCTION__
);
1282 if (unlikely(!dev
->driver
|| dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)) {
1283 DEBUG("%s, bogus device state %p\n", __FUNCTION__
, dev
->driver
);
1287 DEBUG("%s queue req %p, len %d buf %p\n", _ep
->name
, _req
, _req
->length
,
1290 spin_lock_irqsave(&dev
->lock
, flags
);
1292 _req
->status
= -EINPROGRESS
;
1295 /* kickstart this i/o queue? */
1296 DEBUG("Add to %d Q %d %d\n", ep_index(ep
), list_empty(&ep
->queue
),
1298 if (list_empty(&ep
->queue
) && likely(!ep
->stopped
)) {
1301 if (unlikely(ep_index(ep
) == 0)) {
1303 list_add_tail(&req
->queue
, &ep
->queue
);
1304 jz4740_ep0_kick(dev
, ep
);
1306 } else if (use_dma
) {
1311 else if (ep_is_in(ep
)) {
1313 jz_udc_set_index(dev
, ep_index(ep
));
1314 csr
= usb_readb(dev
, ep
->csr
);
1316 if (!(csr
& USB_INCSR_FFNOTEMPT
)) {
1317 if (write_fifo(ep
, req
) == 1)
1322 jz_udc_set_index(dev
, ep_index(ep
));
1323 csr
= usb_readb(dev
, ep
->csr
);
1325 if (csr
& USB_OUTCSR_OUTPKTRDY
) {
1326 if (read_fifo(ep
, req
) == 1)
1332 /* pio or dma irq handler advances the queue. */
1333 if (likely(req
!= 0))
1334 list_add_tail(&req
->queue
, &ep
->queue
);
1336 spin_unlock_irqrestore(&dev
->lock
, flags
);
1341 /* dequeue JUST ONE request */
1342 static int jz4740_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
1344 struct jz4740_ep
*ep
;
1345 struct jz4740_request
*req
;
1346 unsigned long flags
;
1348 DEBUG("%s, %p\n", __FUNCTION__
, _ep
);
1350 ep
= container_of(_ep
, struct jz4740_ep
, ep
);
1351 if (!_ep
|| ep
->type
== ep_control
)
1354 spin_lock_irqsave(&ep
->dev
->lock
, flags
);
1356 /* make sure it's actually queued on this endpoint */
1357 list_for_each_entry(req
, &ep
->queue
, queue
) {
1358 if (&req
->req
== _req
)
1361 if (&req
->req
!= _req
) {
1362 spin_unlock_irqrestore(&ep
->dev
->lock
, flags
);
1365 done(ep
, req
, -ECONNRESET
);
1367 spin_unlock_irqrestore(&ep
->dev
->lock
, flags
);
1371 /** Return bytes in EP FIFO
1372 * NOTE: Sets INDEX register to EP
1374 static int jz4740_fifo_status(struct usb_ep
*_ep
)
1378 struct jz4740_ep
*ep
;
1379 unsigned long flags
;
1381 ep
= container_of(_ep
, struct jz4740_ep
, ep
);
1383 DEBUG("%s, bad ep\n", __FUNCTION__
);
1387 DEBUG("%s, %d\n", __FUNCTION__
, ep_index(ep
));
1389 /* LPD can't report unclaimed bytes from IN fifos */
1393 spin_lock_irqsave(&ep
->dev
->lock
, flags
);
1394 jz_udc_set_index(ep
->dev
, ep_index(ep
));
1396 csr
= usb_readb(ep
->dev
, ep
->csr
);
1397 if (ep
->dev
->gadget
.speed
!= USB_SPEED_UNKNOWN
||
1399 count
= usb_readw(ep
->dev
, JZ_REG_UDC_OUTCOUNT
);
1402 spin_unlock_irqrestore(&ep
->dev
->lock
, flags
);
1408 * NOTE: Sets INDEX register to EP
1410 static void jz4740_fifo_flush(struct usb_ep
*_ep
)
1412 struct jz4740_ep
*ep
;
1413 unsigned long flags
;
1415 DEBUG("%s:%s[%d]\n", __FILE__
, __func__
, __LINE__
);
1417 ep
= container_of(_ep
, struct jz4740_ep
, ep
);
1418 if (unlikely(!_ep
|| (!ep
->desc
&& ep
->type
== ep_control
))) {
1419 DEBUG("%s, bad ep\n", __FUNCTION__
);
1423 spin_lock_irqsave(&ep
->dev
->lock
, flags
);
1425 jz_udc_set_index(ep
->dev
, ep_index(ep
));
1428 spin_unlock_irqrestore(&ep
->dev
->lock
, flags
);
1431 /****************************************************************/
1432 /* End Point 0 related functions */
1433 /****************************************************************/
1435 /* return: 0 = still running, 1 = completed, negative = errno */
1436 static int write_fifo_ep0(struct jz4740_ep
*ep
, struct jz4740_request
*req
)
1442 DEBUG("%s:%s[%d]\n", __FILE__
, __func__
, __LINE__
);
1443 max
= ep_maxpacket(ep
);
1445 count
= write_packet(ep
, req
, max
);
1447 /* last packet is usually short (or a zlp) */
1448 if (unlikely(count
!= max
))
1451 if (likely(req
->req
.length
!= req
->req
.actual
) || req
->req
.zero
)
1457 DEBUG_EP0("%s: wrote %s %d bytes%s %d left %p\n", __FUNCTION__
,
1459 is_last
? "/L" : "", req
->req
.length
- req
->req
.actual
, req
);
1461 /* requests complete when all IN data is in the FIFO */
1470 static inline int jz4740_fifo_read(struct jz4740_ep
*ep
,
1471 unsigned char *cp
, int max
)
1474 int count
= usb_readw(ep
->dev
, JZ_REG_UDC_OUTCOUNT
);
1480 *cp
++ = usb_readb(ep
->dev
, ep
->fifo
);
1485 static inline void jz4740_fifo_write(struct jz4740_ep
*ep
,
1486 unsigned char *cp
, int count
)
1488 DEBUG("fifo_write: %d %d\n", ep_index(ep
), count
);
1490 usb_writeb(ep
->dev
, ep
->fifo
, *cp
++);
1493 static int read_fifo_ep0(struct jz4740_ep
*ep
, struct jz4740_request
*req
)
1495 struct jz4740_udc
*dev
= ep
->dev
;
1498 unsigned bufferspace
, count
, is_short
;
1500 DEBUG_EP0("%s\n", __FUNCTION__
);
1502 csr
= usb_readb(dev
, JZ_REG_UDC_CSR0
);
1503 if (!(csr
& USB_CSR0_OUTPKTRDY
))
1506 buf
= req
->req
.buf
+ req
->req
.actual
;
1508 bufferspace
= req
->req
.length
- req
->req
.actual
;
1510 /* read all bytes from this packet */
1511 if (likely(csr
& USB_CSR0_OUTPKTRDY
)) {
1512 count
= usb_readw(dev
, JZ_REG_UDC_OUTCOUNT
);
1513 req
->req
.actual
+= min(count
, bufferspace
);
1517 is_short
= (count
< ep
->ep
.maxpacket
);
1518 DEBUG_EP0("read %s %02x, %d bytes%s req %p %d/%d\n",
1519 ep
->ep
.name
, csr
, count
,
1520 is_short
? "/S" : "", req
, req
->req
.actual
, req
->req
.length
);
1522 while (likely(count
-- != 0)) {
1523 uint8_t byte
= (uint8_t)usb_readl(dev
, ep
->fifo
);
1525 if (unlikely(bufferspace
== 0)) {
1526 /* this happens when the driver's buffer
1527 * is smaller than what the host sent.
1528 * discard the extra data.
1530 if (req
->req
.status
!= -EOVERFLOW
)
1531 DEBUG_EP0("%s overflow %d\n", ep
->ep
.name
,
1533 req
->req
.status
= -EOVERFLOW
;
1541 if (is_short
|| req
->req
.actual
== req
->req
.length
) {
1546 /* finished that packet. the next one may be waiting... */
1551 * udc_set_address - set the USB address for this device
1554 * Called from control endpoint function after it decodes a set address setup packet.
1556 static void udc_set_address(struct jz4740_udc
*dev
, unsigned char address
)
1558 DEBUG_EP0("%s: %d\n", __FUNCTION__
, address
);
1560 dev
->usb_address
= address
;
1561 usb_writeb(dev
, JZ_REG_UDC_FADDR
, address
);
1565 * DATA_STATE_RECV (USB_CSR0_OUTPKTRDY)
1567 * set USB_CSR0_SVDOUTPKTRDY | USB_CSR0_DATAEND | USB_CSR0_SENDSTALL bits
1569 * set USB_CSR0_SVDOUTPKTRDY bit
1570 if last set USB_CSR0_DATAEND bit
1572 static void jz4740_ep0_out(struct jz4740_udc
*dev
, uint32_t csr
, int kickstart
)
1574 struct jz4740_request
*req
;
1575 struct jz4740_ep
*ep
= &dev
->ep
[0];
1578 DEBUG_EP0("%s: %x\n", __FUNCTION__
, csr
);
1580 if (list_empty(&ep
->queue
))
1583 req
= list_entry(ep
->queue
.next
, struct jz4740_request
, queue
);
1586 if (req
->req
.length
== 0) {
1587 DEBUG_EP0("ZERO LENGTH OUT!\n");
1588 usb_setb(dev
, JZ_REG_UDC_CSR0
, (USB_CSR0_SVDOUTPKTRDY
| USB_CSR0_DATAEND
));
1589 dev
->ep0state
= WAIT_FOR_SETUP
;
1591 } else if (kickstart
) {
1592 usb_setb(dev
, JZ_REG_UDC_CSR0
, (USB_CSR0_SVDOUTPKTRDY
));
1595 ret
= read_fifo_ep0(ep
, req
);
1598 DEBUG_EP0("%s: finished, waiting for status\n",
1600 usb_setb(dev
, JZ_REG_UDC_CSR0
, (USB_CSR0_SVDOUTPKTRDY
| USB_CSR0_DATAEND
));
1601 dev
->ep0state
= WAIT_FOR_SETUP
;
1603 /* Not done yet.. */
1604 DEBUG_EP0("%s: not finished\n", __FUNCTION__
);
1605 usb_setb(dev
, JZ_REG_UDC_CSR0
, USB_CSR0_SVDOUTPKTRDY
);
1608 DEBUG_EP0("NO REQ??!\n");
1615 static int jz4740_ep0_in(struct jz4740_udc
*dev
, uint32_t csr
)
1617 struct jz4740_request
*req
;
1618 struct jz4740_ep
*ep
= &dev
->ep
[0];
1619 int ret
, need_zlp
= 0;
1621 DEBUG_EP0("%s: %x\n", __FUNCTION__
, csr
);
1623 if (list_empty(&ep
->queue
))
1626 req
= list_entry(ep
->queue
.next
, struct jz4740_request
, queue
);
1629 DEBUG_EP0("%s: NULL REQ\n", __FUNCTION__
);
1633 if (req
->req
.length
== 0) {
1634 usb_setb(dev
, JZ_REG_UDC_CSR0
, (USB_CSR0_INPKTRDY
| USB_CSR0_DATAEND
));
1635 dev
->ep0state
= WAIT_FOR_SETUP
;
1639 if (req
->req
.length
- req
->req
.actual
== EP0_MAXPACKETSIZE
) {
1640 /* Next write will end with the packet size, */
1641 /* so we need zero-length-packet */
1645 ret
= write_fifo_ep0(ep
, req
);
1647 if (ret
== 1 && !need_zlp
) {
1649 DEBUG_EP0("%s: finished, waiting for status\n", __FUNCTION__
);
1651 usb_setb(dev
, JZ_REG_UDC_CSR0
, (USB_CSR0_INPKTRDY
| USB_CSR0_DATAEND
));
1652 dev
->ep0state
= WAIT_FOR_SETUP
;
1654 DEBUG_EP0("%s: not finished\n", __FUNCTION__
);
1655 usb_setb(dev
, JZ_REG_UDC_CSR0
, USB_CSR0_INPKTRDY
);
1659 DEBUG_EP0("%s: Need ZLP!\n", __FUNCTION__
);
1660 usb_setb(dev
, JZ_REG_UDC_CSR0
, USB_CSR0_INPKTRDY
);
1661 dev
->ep0state
= DATA_STATE_NEED_ZLP
;
1667 static int jz4740_handle_get_status(struct jz4740_udc
*dev
,
1668 struct usb_ctrlrequest
*ctrl
)
1670 struct jz4740_ep
*ep0
= &dev
->ep
[0];
1671 struct jz4740_ep
*qep
;
1672 int reqtype
= (ctrl
->bRequestType
& USB_RECIP_MASK
);
1675 DEBUG("%s:%s[%d]\n", __FILE__
, __func__
, __LINE__
);
1677 if (reqtype
== USB_RECIP_INTERFACE
) {
1678 /* This is not supported.
1679 * And according to the USB spec, this one does nothing..
1682 DEBUG_SETUP("GET_STATUS: USB_RECIP_INTERFACE\n");
1683 } else if (reqtype
== USB_RECIP_DEVICE
) {
1684 DEBUG_SETUP("GET_STATUS: USB_RECIP_DEVICE\n");
1685 val
|= (1 << 0); /* Self powered */
1686 /*val |= (1<<1); *//* Remote wakeup */
1687 } else if (reqtype
== USB_RECIP_ENDPOINT
) {
1688 int ep_num
= (ctrl
->wIndex
& ~USB_DIR_IN
);
1691 ("GET_STATUS: USB_RECIP_ENDPOINT (%d), ctrl->wLength = %d\n",
1692 ep_num
, ctrl
->wLength
);
1694 if (ctrl
->wLength
> 2 || ep_num
> 3)
1697 qep
= &dev
->ep
[ep_num
];
1698 if (ep_is_in(qep
) != ((ctrl
->wIndex
& USB_DIR_IN
) ? 1 : 0)
1699 && ep_index(qep
) != 0) {
1703 jz_udc_set_index(dev
, ep_index(qep
));
1705 /* Return status on next IN token */
1706 switch (qep
->type
) {
1709 (usb_readb(dev
, qep
->csr
) & USB_CSR0_SENDSTALL
) ==
1715 (usb_readb(dev
, qep
->csr
) & USB_INCSR_SENDSTALL
) ==
1716 USB_INCSR_SENDSTALL
;
1720 (usb_readb(dev
, qep
->csr
) & USB_OUTCSR_SENDSTALL
) ==
1721 USB_OUTCSR_SENDSTALL
;
1725 /* Back to EP0 index */
1726 jz_udc_set_index(dev
, 0);
1728 DEBUG_SETUP("GET_STATUS, ep: %d (%x), val = %d\n", ep_num
,
1731 DEBUG_SETUP("Unknown REQ TYPE: %d\n", reqtype
);
1735 /* Clear "out packet ready" */
1736 usb_setb(dev
, JZ_REG_UDC_CSR0
, USB_CSR0_SVDOUTPKTRDY
);
1737 /* Put status to FIFO */
1738 jz4740_fifo_write(ep0
, (uint8_t *)&val
, sizeof(val
));
1739 /* Issue "In packet ready" */
1740 usb_setb(dev
, JZ_REG_UDC_CSR0
, (USB_CSR0_INPKTRDY
| USB_CSR0_DATAEND
));
1746 * WAIT_FOR_SETUP (OUTPKTRDY)
1747 * - read data packet from EP0 FIFO
1750 * set USB_CSR0_SVDOUTPKTRDY | USB_CSR0_DATAEND | USB_CSR0_SENDSTALL bits
1752 * set USB_CSR0_SVDOUTPKTRDY | USB_CSR0_DATAEND bits
1754 static void jz4740_ep0_setup(struct jz4740_udc
*dev
, uint32_t csr
)
1756 struct jz4740_ep
*ep
= &dev
->ep
[0];
1757 struct usb_ctrlrequest ctrl
;
1760 DEBUG_SETUP("%s: %x\n", __FUNCTION__
, csr
);
1762 /* Nuke all previous transfers */
1765 /* read control req from fifo (8 bytes) */
1766 jz4740_fifo_read(ep
, (unsigned char *)&ctrl
, 8);
1768 DEBUG_SETUP("SETUP %02x.%02x v%04x i%04x l%04x\n",
1769 ctrl
.bRequestType
, ctrl
.bRequest
,
1770 ctrl
.wValue
, ctrl
.wIndex
, ctrl
.wLength
);
1772 /* Set direction of EP0 */
1773 if (likely(ctrl
.bRequestType
& USB_DIR_IN
)) {
1774 ep
->bEndpointAddress
|= USB_DIR_IN
;
1776 ep
->bEndpointAddress
&= ~USB_DIR_IN
;
1779 /* Handle some SETUP packets ourselves */
1780 switch (ctrl
.bRequest
) {
1781 case USB_REQ_SET_ADDRESS
:
1782 if (ctrl
.bRequestType
!= (USB_TYPE_STANDARD
| USB_RECIP_DEVICE
))
1785 DEBUG_SETUP("USB_REQ_SET_ADDRESS (%d)\n", ctrl
.wValue
);
1786 udc_set_address(dev
, ctrl
.wValue
);
1787 usb_setb(dev
, JZ_REG_UDC_CSR0
, (USB_CSR0_SVDOUTPKTRDY
| USB_CSR0_DATAEND
));
1790 case USB_REQ_SET_CONFIGURATION
:
1791 if (ctrl
.bRequestType
!= (USB_TYPE_STANDARD
| USB_RECIP_DEVICE
))
1794 DEBUG_SETUP("USB_REQ_SET_CONFIGURATION (%d)\n", ctrl
.wValue
);
1795 /* usb_setb(JZ_REG_UDC_CSR0, (USB_CSR0_SVDOUTPKTRDY | USB_CSR0_DATAEND));*/
1797 /* Enable RESUME and SUSPEND interrupts */
1798 usb_setb(dev
, JZ_REG_UDC_INTRUSBE
, (USB_INTR_RESUME
| USB_INTR_SUSPEND
));
1801 case USB_REQ_SET_INTERFACE
:
1802 if (ctrl
.bRequestType
!= (USB_TYPE_STANDARD
| USB_RECIP_DEVICE
))
1805 DEBUG_SETUP("USB_REQ_SET_INTERFACE (%d)\n", ctrl
.wValue
);
1806 /* usb_setb(JZ_REG_UDC_CSR0, (USB_CSR0_SVDOUTPKTRDY | USB_CSR0_DATAEND));*/
1809 case USB_REQ_GET_STATUS
:
1810 if (jz4740_handle_get_status(dev
, &ctrl
) == 0)
1813 case USB_REQ_CLEAR_FEATURE
:
1814 case USB_REQ_SET_FEATURE
:
1815 if (ctrl
.bRequestType
== USB_RECIP_ENDPOINT
) {
1816 struct jz4740_ep
*qep
;
1817 int ep_num
= (ctrl
.wIndex
& 0x0f);
1819 /* Support only HALT feature */
1820 if (ctrl
.wValue
!= 0 || ctrl
.wLength
!= 0
1821 || ep_num
> 3 || ep_num
< 1)
1824 qep
= &dev
->ep
[ep_num
];
1825 spin_unlock(&dev
->lock
);
1826 if (ctrl
.bRequest
== USB_REQ_SET_FEATURE
) {
1827 DEBUG_SETUP("SET_FEATURE (%d)\n",
1829 jz4740_set_halt(&qep
->ep
, 1);
1831 DEBUG_SETUP("CLR_FEATURE (%d)\n",
1833 jz4740_set_halt(&qep
->ep
, 0);
1835 spin_lock(&dev
->lock
);
1837 jz_udc_set_index(dev
, 0);
1839 /* Reply with a ZLP on next IN token */
1840 usb_setb(dev
, JZ_REG_UDC_CSR0
,
1841 (USB_CSR0_SVDOUTPKTRDY
| USB_CSR0_DATAEND
));
1850 /* gadget drivers see class/vendor specific requests,
1851 * {SET,GET}_{INTERFACE,DESCRIPTOR,CONFIGURATION},
1855 /* device-2-host (IN) or no data setup command, process immediately */
1856 spin_unlock(&dev
->lock
);
1858 i
= dev
->driver
->setup(&dev
->gadget
, &ctrl
);
1859 spin_lock(&dev
->lock
);
1861 if (unlikely(i
< 0)) {
1862 /* setup processing failed, force stall */
1864 (" --> ERROR: gadget setup FAILED (stalling), setup returned %d\n",
1866 jz_udc_set_index(dev
, 0);
1867 usb_setb(dev
, JZ_REG_UDC_CSR0
, (USB_CSR0_SVDOUTPKTRDY
| USB_CSR0_DATAEND
| USB_CSR0_SENDSTALL
));
1869 /* ep->stopped = 1; */
1870 dev
->ep0state
= WAIT_FOR_SETUP
;
1873 DEBUG_SETUP("gadget driver setup ok (%d)\n", ctrl
.wLength
);
1874 /* if (!ctrl.wLength) {
1875 usb_setb(JZ_REG_UDC_CSR0, USB_CSR0_SVDOUTPKTRDY);
1882 * DATA_STATE_NEED_ZLP
1884 static void jz4740_ep0_in_zlp(struct jz4740_udc
*dev
, uint32_t csr
)
1886 DEBUG_EP0("%s: %x\n", __FUNCTION__
, csr
);
1888 usb_setb(dev
, JZ_REG_UDC_CSR0
, (USB_CSR0_INPKTRDY
| USB_CSR0_DATAEND
));
1889 dev
->ep0state
= WAIT_FOR_SETUP
;
1893 * handle ep0 interrupt
1895 static void jz4740_handle_ep0(struct jz4740_udc
*dev
, uint32_t intr
)
1897 struct jz4740_ep
*ep
= &dev
->ep
[0];
1900 DEBUG("%s:%s[%d]\n", __FILE__
, __func__
, __LINE__
);
1902 jz_udc_set_index(dev
, 0);
1903 csr
= usb_readb(dev
, JZ_REG_UDC_CSR0
);
1905 DEBUG_EP0("%s: csr = %x state = \n", __FUNCTION__
, csr
);//, state_names[dev->ep0state]);
1908 * if SENT_STALL is set
1909 * - clear the SENT_STALL bit
1911 if (csr
& USB_CSR0_SENTSTALL
) {
1912 DEBUG_EP0("%s: USB_CSR0_SENTSTALL is set: %x\n", __FUNCTION__
, csr
);
1913 usb_clearb(dev
, JZ_REG_UDC_CSR0
, USB_CSR0_SENDSTALL
| USB_CSR0_SENTSTALL
);
1914 nuke(ep
, -ECONNABORTED
);
1915 dev
->ep0state
= WAIT_FOR_SETUP
;
1920 * if a transfer is in progress && INPKTRDY and OUTPKTRDY are clear
1923 * - set IN_PKT_RDY | DATA_END
1927 if (!(csr
& (USB_CSR0_INPKTRDY
| USB_CSR0_OUTPKTRDY
))) {
1928 DEBUG_EP0("%s: INPKTRDY and OUTPKTRDY are clear\n",
1931 switch (dev
->ep0state
) {
1932 case DATA_STATE_XMIT
:
1933 DEBUG_EP0("continue with DATA_STATE_XMIT\n");
1934 jz4740_ep0_in(dev
, csr
);
1936 case DATA_STATE_NEED_ZLP
:
1937 DEBUG_EP0("continue with DATA_STATE_NEED_ZLP\n");
1938 jz4740_ep0_in_zlp(dev
, csr
);
1942 // DEBUG_EP0("Odd state!! state = %s\n",
1943 // state_names[dev->ep0state]);
1944 dev
->ep0state
= WAIT_FOR_SETUP
;
1946 /* usb_setb(ep->csr, USB_CSR0_SENDSTALL); */
1953 * if SETUPEND is set
1954 * - abort the last transfer
1955 * - set SERVICED_SETUP_END_BIT
1957 if (csr
& USB_CSR0_SETUPEND
) {
1958 DEBUG_EP0("%s: USB_CSR0_SETUPEND is set: %x\n", __FUNCTION__
, csr
);
1960 usb_setb(dev
, JZ_REG_UDC_CSR0
, USB_CSR0_SVDSETUPEND
);
1962 dev
->ep0state
= WAIT_FOR_SETUP
;
1966 * if USB_CSR0_OUTPKTRDY is set
1967 * - read data packet from EP0 FIFO
1970 * set SVDOUTPKTRDY | DATAEND | SENDSTALL bits
1972 * set SVDOUTPKTRDY | DATAEND bits
1974 if (csr
& USB_CSR0_OUTPKTRDY
) {
1976 DEBUG_EP0("%s: EP0_OUT_PKT_RDY is set: %x\n", __FUNCTION__
,
1979 switch (dev
->ep0state
) {
1980 case WAIT_FOR_SETUP
:
1981 DEBUG_EP0("WAIT_FOR_SETUP\n");
1982 jz4740_ep0_setup(dev
, csr
);
1985 case DATA_STATE_RECV
:
1986 DEBUG_EP0("DATA_STATE_RECV\n");
1987 jz4740_ep0_out(dev
, csr
, 0);
1992 DEBUG_EP0("strange state!! 2. send stall? state = %d\n",
1999 static void jz4740_ep0_kick(struct jz4740_udc
*dev
, struct jz4740_ep
*ep
)
2003 jz_udc_set_index(dev
, 0);
2005 DEBUG_EP0("%s: %x\n", __FUNCTION__
, csr
);
2007 /* Clear "out packet ready" */
2010 usb_setb(dev
, JZ_REG_UDC_CSR0
, USB_CSR0_SVDOUTPKTRDY
);
2011 csr
= usb_readb(dev
, JZ_REG_UDC_CSR0
);
2012 dev
->ep0state
= DATA_STATE_XMIT
;
2013 jz4740_ep0_in(dev
, csr
);
2015 csr
= usb_readb(dev
, JZ_REG_UDC_CSR0
);
2016 dev
->ep0state
= DATA_STATE_RECV
;
2017 jz4740_ep0_out(dev
, csr
, 1);
2021 /** Handle USB RESET interrupt
2023 static void jz4740_reset_irq(struct jz4740_udc
*dev
)
2025 dev
->gadget
.speed
= (usb_readb(dev
, JZ_REG_UDC_POWER
) & USB_POWER_HSMODE
) ?
2026 USB_SPEED_HIGH
: USB_SPEED_FULL
;
2028 DEBUG_SETUP("%s: address = %d, speed = %s\n", __FUNCTION__
, dev
->usb_address
,
2029 (dev
->gadget
.speed
== USB_SPEED_HIGH
) ? "HIGH":"FULL" );
2033 * jz4740 usb device interrupt handler.
2035 static irqreturn_t
jz4740_udc_irq(int irq
, void *_dev
)
2037 struct jz4740_udc
*dev
= _dev
;
2040 uint32_t intr_usb
= usb_readb(dev
, JZ_REG_UDC_INTRUSB
) & 0x7; /* mask SOF */
2041 uint32_t intr_in
= usb_readw(dev
, JZ_REG_UDC_INTRIN
);
2042 uint32_t intr_out
= usb_readw(dev
, JZ_REG_UDC_INTROUT
);
2043 uint32_t intr_dma
= usb_readb(dev
, JZ_REG_UDC_INTR
);
2045 if (!intr_usb
&& !intr_in
&& !intr_out
&& !intr_dma
)
2049 DEBUG("intr_out=%x intr_in=%x intr_usb=%x\n",
2050 intr_out
, intr_in
, intr_usb
);
2052 spin_lock(&dev
->lock
);
2053 index
= usb_readb(dev
, JZ_REG_UDC_INDEX
);
2055 /* Check for resume from suspend mode */
2056 if ((intr_usb
& USB_INTR_RESUME
) &&
2057 (usb_readb(dev
, JZ_REG_UDC_INTRUSBE
) & USB_INTR_RESUME
)) {
2058 DEBUG("USB resume\n");
2059 dev
->driver
->resume(&dev
->gadget
); /* We have suspend(), so we must have resume() too. */
2062 /* Check for system interrupts */
2063 if (intr_usb
& USB_INTR_RESET
) {
2064 DEBUG("USB reset\n");
2065 jz4740_reset_irq(dev
);
2068 /* Check for endpoint 0 interrupt */
2069 if (intr_in
& USB_INTR_EP0
) {
2070 DEBUG("USB_INTR_EP0 (control)\n");
2071 jz4740_handle_ep0(dev
, intr_in
);
2074 /* Check for Bulk-IN DMA interrupt */
2075 if (intr_dma
& 0x1) {
2077 struct jz4740_ep
*ep
;
2078 ep_num
= (usb_readl(dev
, JZ_REG_UDC_CNTL1
) >> 4) & 0xf;
2079 ep
= &dev
->ep
[ep_num
+ 1];
2080 jz_udc_set_index(dev
, ep_num
);
2081 usb_setb(dev
, ep
->csr
, USB_INCSR_INPKTRDY
);
2082 /* jz4740_in_epn(dev, ep_num, intr_in);*/
2085 /* Check for Bulk-OUT DMA interrupt */
2086 if (intr_dma
& 0x2) {
2088 ep_num
= (usb_readl(dev
, JZ_REG_UDC_CNTL2
) >> 4) & 0xf;
2089 jz4740_out_epn(dev
, ep_num
, intr_out
);
2092 /* Check for each configured endpoint interrupt */
2093 if (intr_in
& USB_INTR_INEP1
) {
2094 DEBUG("USB_INTR_INEP1\n");
2095 jz4740_in_epn(dev
, 1, intr_in
);
2098 if (intr_in
& USB_INTR_INEP2
) {
2099 DEBUG("USB_INTR_INEP2\n");
2100 jz4740_in_epn(dev
, 2, intr_in
);
2103 if (intr_out
& USB_INTR_OUTEP1
) {
2104 DEBUG("USB_INTR_OUTEP1\n");
2105 jz4740_out_epn(dev
, 1, intr_out
);
2108 /* Check for suspend mode */
2109 if ((intr_usb
& USB_INTR_SUSPEND
) &&
2110 (usb_readb(dev
, JZ_REG_UDC_INTRUSBE
) & USB_INTR_SUSPEND
)) {
2111 DEBUG("USB suspend\n");
2112 dev
->driver
->suspend(&dev
->gadget
);
2113 /* Host unloaded from us, can do something, such as flushing
2114 the NAND block cache etc. */
2117 jz_udc_set_index(dev
, index
);
2119 spin_unlock(&dev
->lock
);
2126 /*-------------------------------------------------------------------------*/
2128 /* Common functions - Added by River */
2129 static struct jz4740_udc udc_dev
;
2131 static inline struct jz4740_udc
*gadget_to_udc(struct usb_gadget
*gadget
)
2133 return container_of(gadget
, struct jz4740_udc
, gadget
);
2137 static int jz4740_udc_get_frame(struct usb_gadget
*_gadget
)
2139 DEBUG("%s, %p\n", __FUNCTION__
, _gadget
);
2140 return usb_readw(gadget_to_udc(_gadget
), JZ_REG_UDC_FRAME
);
2143 static int jz4740_udc_wakeup(struct usb_gadget
*_gadget
)
2145 /* host may not have enabled remote wakeup */
2146 /*if ((UDCCS0 & UDCCS0_DRWF) == 0)
2147 return -EHOSTUNREACH;
2148 udc_set_mask_UDCCR(UDCCR_RSM); */
2152 static int jz4740_udc_pullup(struct usb_gadget
*_gadget
, int on
)
2154 struct jz4740_udc
*udc
= gadget_to_udc(_gadget
);
2156 unsigned long flags
;
2158 local_irq_save(flags
);
2161 udc
->state
= UDC_STATE_ENABLE
;
2164 udc
->state
= UDC_STATE_DISABLE
;
2168 local_irq_restore(flags
);
2173 static const struct usb_gadget_ops jz4740_udc_ops
= {
2174 .get_frame
= jz4740_udc_get_frame
,
2175 .wakeup
= jz4740_udc_wakeup
,
2176 .pullup
= jz4740_udc_pullup
,
2177 /* current versions must always be self-powered */
2180 static struct usb_ep_ops jz4740_ep_ops
= {
2181 .enable
= jz4740_ep_enable
,
2182 .disable
= jz4740_ep_disable
,
2184 .alloc_request
= jz4740_alloc_request
,
2185 .free_request
= jz4740_free_request
,
2187 .queue
= jz4740_queue
,
2188 .dequeue
= jz4740_dequeue
,
2190 .set_halt
= jz4740_set_halt
,
2191 .fifo_status
= jz4740_fifo_status
,
2192 .fifo_flush
= jz4740_fifo_flush
,
2196 /*-------------------------------------------------------------------------*/
2198 static struct jz4740_udc udc_dev
= {
2201 .ops
= &jz4740_udc_ops
,
2202 .ep0
= &udc_dev
.ep
[0].ep
,
2205 .init_name
= "gadget",
2209 /* control endpoint */
2213 .ops
= &jz4740_ep_ops
,
2214 .maxpacket
= EP0_MAXPACKETSIZE
,
2218 .bEndpointAddress
= 0,
2222 .fifo
= JZ_REG_UDC_EP_FIFO(0),
2223 .csr
= JZ_REG_UDC_CSR0
,
2226 /* bulk out endpoint */
2229 .name
= "ep1out-bulk",
2230 .ops
= &jz4740_ep_ops
,
2231 .maxpacket
= EPBULK_MAXPACKETSIZE
,
2235 .bEndpointAddress
= 1,
2236 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
2238 .type
= ep_bulk_out
,
2239 .fifo
= JZ_REG_UDC_EP_FIFO(1),
2240 .csr
= JZ_REG_UDC_OUTCSR
,
2243 /* bulk in endpoint */
2246 .name
= "ep1in-bulk",
2247 .ops
= &jz4740_ep_ops
,
2248 .maxpacket
= EPBULK_MAXPACKETSIZE
,
2252 .bEndpointAddress
= 1 | USB_DIR_IN
,
2253 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
2256 .fifo
= JZ_REG_UDC_EP_FIFO(1),
2257 .csr
= JZ_REG_UDC_INCSR
,
2260 /* interrupt in endpoint */
2263 .name
= "ep2in-int",
2264 .ops
= &jz4740_ep_ops
,
2265 .maxpacket
= EPINTR_MAXPACKETSIZE
,
2269 .bEndpointAddress
= 2 | USB_DIR_IN
,
2270 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
2272 .type
= ep_interrupt
,
2273 .fifo
= JZ_REG_UDC_EP_FIFO(2),
2274 .csr
= JZ_REG_UDC_INCSR
,
2278 static void gadget_release(struct device
*_dev
)
2283 static int jz4740_udc_probe(struct platform_device
*pdev
)
2285 struct jz4740_udc
*dev
= &udc_dev
;
2288 spin_lock_init(&dev
->lock
);
2289 the_controller
= dev
;
2291 dev
->dev
= &pdev
->dev
;
2292 dev_set_name(&dev
->gadget
.dev
, "gadget");
2293 dev
->gadget
.dev
.parent
= &pdev
->dev
;
2294 dev
->gadget
.dev
.dma_mask
= pdev
->dev
.dma_mask
;
2295 dev
->gadget
.dev
.release
= gadget_release
;
2297 ret
= device_register(&dev
->gadget
.dev
);
2301 dev
->clk
= clk_get(&pdev
->dev
, "udc");
2302 if (IS_ERR(dev
->clk
)) {
2303 ret
= PTR_ERR(dev
->clk
);
2304 dev_err(&pdev
->dev
, "Failed to get udc clock: %d\n", ret
);
2305 goto err_device_unregister
;
2308 platform_set_drvdata(pdev
, dev
);
2310 dev
->mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2314 dev_err(&pdev
->dev
, "Failed to get mmio memory resource\n");
2318 dev
->mem
= request_mem_region(dev
->mem
->start
, resource_size(dev
->mem
), pdev
->name
);
2322 dev_err(&pdev
->dev
, "Failed to request mmio memory region\n");
2323 goto err_device_unregister
;
2326 dev
->base
= ioremap(dev
->mem
->start
, resource_size(dev
->mem
));
2330 dev_err(&pdev
->dev
, "Failed to ioremap mmio memory\n");
2331 goto err_release_mem_region
;
2334 dev
->irq
= platform_get_irq(pdev
, 0);
2336 ret
= request_irq(dev
->irq
, jz4740_udc_irq
, IRQF_DISABLED
,
2339 dev_err(&pdev
->dev
, "Failed to request irq: %d\n", ret
);
2350 err_release_mem_region
:
2351 release_mem_region(dev
->mem
->start
, resource_size(dev
->mem
));
2354 err_device_unregister
:
2355 device_unregister(&dev
->gadget
.dev
);
2356 platform_set_drvdata(pdev
, NULL
);
2363 static int jz4740_udc_remove(struct platform_device
*pdev
)
2365 struct jz4740_udc
*dev
= platform_get_drvdata(pdev
);
2372 free_irq(dev
->irq
, dev
);
2374 release_mem_region(dev
->mem
->start
, resource_size(dev
->mem
));
2377 platform_set_drvdata(pdev
, NULL
);
2378 device_unregister(&dev
->gadget
.dev
);
2379 the_controller
= NULL
;
2384 static struct platform_driver udc_driver
= {
2385 .probe
= jz4740_udc_probe
,
2386 .remove
= jz4740_udc_remove
,
2389 .owner
= THIS_MODULE
,
2393 /*-------------------------------------------------------------------------*/
2395 static int __init
udc_init (void)
2397 return platform_driver_register(&udc_driver
);
2400 static void __exit
udc_exit (void)
2402 platform_driver_unregister(&udc_driver
);
2405 module_init(udc_init
);
2406 module_exit(udc_exit
);
2408 MODULE_DESCRIPTION("JZ4740 USB Device Controller");
2409 MODULE_AUTHOR("Wei Jianli <jlwei@ingenic.cn>");
2410 MODULE_LICENSE("GPL");