1 From 644d56ba485f220b1b740b320760a12b5e4e0308 Mon Sep 17 00:00:00 2001
2 From: Lars-Peter Clausen <lars@metafoo.de>
3 Date: Sat, 24 Apr 2010 12:18:46 +0200
4 Subject: [PATCH] Add jz4740 udc driver
7 drivers/usb/gadget/Kconfig | 14 +
8 drivers/usb/gadget/Makefile | 1 +
9 drivers/usb/gadget/gadget_chips.h | 9 +
10 drivers/usb/gadget/jz4740_udc.c | 2437 +++++++++++++++++++++++++++++++++++++
11 drivers/usb/gadget/jz4740_udc.h | 100 ++
12 5 files changed, 2561 insertions(+), 0 deletions(-)
13 create mode 100644 drivers/usb/gadget/jz4740_udc.c
14 create mode 100644 drivers/usb/gadget/jz4740_udc.h
16 diff --git a/drivers/usb/gadget/Kconfig b/drivers/usb/gadget/Kconfig
17 index 11a3e0f..a33d0c8 100644
18 --- a/drivers/usb/gadget/Kconfig
19 +++ b/drivers/usb/gadget/Kconfig
20 @@ -121,11 +121,25 @@ choice
22 # Integrated controllers
24 +config USB_GADGET_JZ4740
25 + boolean "JZ4740 UDC"
26 + depends on SOC_JZ4740
27 + select USB_GADGET_SELECTED
28 + select USB_GADGET_DUALSPEED
30 + Select this to support the Ingenic JZ4740 processor
31 + high speed USB device controller.
35 + depends on USB_GADGET_JZ4740
38 config USB_GADGET_AT91
39 boolean "Atmel AT91 USB Device Port"
40 depends on ARCH_AT91 && !ARCH_AT91SAM9RL && !ARCH_AT91CAP9 && !ARCH_AT91SAM9G45
41 select USB_GADGET_SELECTED
44 Many Atmel AT91 processors (such as the AT91RM2000) have a
45 full speed USB Device Port with support for five configurable
46 diff --git a/drivers/usb/gadget/Makefile b/drivers/usb/gadget/Makefile
47 index 43b51da..8c6ee6b 100644
48 --- a/drivers/usb/gadget/Makefile
49 +++ b/drivers/usb/gadget/Makefile
50 @@ -28,6 +28,7 @@ obj-$(CONFIG_USB_FSL_QE) += fsl_qe_udc.o
51 obj-$(CONFIG_USB_CI13XXX) += ci13xxx_udc.o
52 obj-$(CONFIG_USB_S3C_HSOTG) += s3c-hsotg.o
53 obj-$(CONFIG_USB_LANGWELL) += langwell_udc.o
54 +obj-$(CONFIG_USB_JZ4740) += jz4740_udc.o
58 diff --git a/drivers/usb/gadget/gadget_chips.h b/drivers/usb/gadget/gadget_chips.h
59 index e511fec..b2ec5fb 100644
60 --- a/drivers/usb/gadget/gadget_chips.h
61 +++ b/drivers/usb/gadget/gadget_chips.h
63 #ifndef __GADGET_CHIPS_H
64 #define __GADGET_CHIPS_H
66 +#ifdef CONFIG_USB_GADGET_JZ4740
67 +#define gadget_is_jz4740(g) !strcmp("ingenic_hsusb", (g)->name)
69 +#define gadget_is_jz4740(g) 0
72 #ifdef CONFIG_USB_GADGET_NET2280
73 #define gadget_is_net2280(g) !strcmp("net2280", (g)->name)
75 @@ -200,6 +206,9 @@ static inline int usb_gadget_controller_number(struct usb_gadget *gadget)
77 else if (gadget_is_s3c_hsotg(gadget))
79 + else if (gadget_is_jz4740(gadget))
85 diff --git a/drivers/usb/gadget/jz4740_udc.c b/drivers/usb/gadget/jz4740_udc.c
87 index 0000000..e84c817
89 +++ b/drivers/usb/gadget/jz4740_udc.c
92 + * linux/drivers/usb/gadget/jz4740_udc.c
94 + * Ingenic JZ4740 on-chip high speed USB device controller
96 + * Copyright (C) 2006 - 2008 Ingenic Semiconductor Inc.
97 + * Author: <jlwei@ingenic.cn>
99 + * This program is free software; you can redistribute it and/or modify
100 + * it under the terms of the GNU General Public License as published by
101 + * the Free Software Foundation; either version 2 of the License, or
102 + * (at your option) any later version.
106 + * This device has ep0, two bulk-in/interrupt-in endpoints, and one bulk-out endpoint.
108 + * - Endpoint numbering is fixed: ep0, ep1in-int, ep2in-bulk, ep1out-bulk.
109 + * - DMA works with bulk-in (channel 1) and bulk-out (channel 2) endpoints.
112 +#include <linux/kernel.h>
113 +#include <linux/module.h>
114 +#include <linux/platform_device.h>
115 +#include <linux/delay.h>
116 +#include <linux/ioport.h>
117 +#include <linux/slab.h>
118 +#include <linux/errno.h>
119 +#include <linux/init.h>
120 +#include <linux/list.h>
121 +#include <linux/interrupt.h>
122 +#include <linux/proc_fs.h>
123 +#include <linux/usb.h>
124 +#include <linux/usb/gadget.h>
125 +#include <linux/clk.h>
127 +#include <asm/byteorder.h>
129 +#include <asm/irq.h>
130 +#include <asm/system.h>
131 +#include <asm/mach-jz4740/clock.h>
133 +#include "jz4740_udc.h"
135 +#define JZ_REG_UDC_FADDR 0x00 /* Function Address 8-bit */
136 +#define JZ_REG_UDC_POWER 0x01 /* Power Management 8-bit */
137 +#define JZ_REG_UDC_INTRIN 0x02 /* Interrupt IN 16-bit */
138 +#define JZ_REG_UDC_INTROUT 0x04 /* Interrupt OUT 16-bit */
139 +#define JZ_REG_UDC_INTRINE 0x06 /* Intr IN enable 16-bit */
140 +#define JZ_REG_UDC_INTROUTE 0x08 /* Intr OUT enable 16-bit */
141 +#define JZ_REG_UDC_INTRUSB 0x0a /* Interrupt USB 8-bit */
142 +#define JZ_REG_UDC_INTRUSBE 0x0b /* Interrupt USB Enable 8-bit */
143 +#define JZ_REG_UDC_FRAME 0x0c /* Frame number 16-bit */
144 +#define JZ_REG_UDC_INDEX 0x0e /* Index register 8-bit */
145 +#define JZ_REG_UDC_TESTMODE 0x0f /* USB test mode 8-bit */
147 +#define JZ_REG_UDC_CSR0 0x12 /* EP0 CSR 8-bit */
148 +#define JZ_REG_UDC_INMAXP 0x10 /* EP1-2 IN Max Pkt Size 16-bit */
149 +#define JZ_REG_UDC_INCSR 0x12 /* EP1-2 IN CSR LSB 8/16bit */
150 +#define JZ_REG_UDC_INCSRH 0x13 /* EP1-2 IN CSR MSB 8-bit */
151 +#define JZ_REG_UDC_OUTMAXP 0x14 /* EP1 OUT Max Pkt Size 16-bit */
152 +#define JZ_REG_UDC_OUTCSR 0x16 /* EP1 OUT CSR LSB 8/16bit */
153 +#define JZ_REG_UDC_OUTCSRH 0x17 /* EP1 OUT CSR MSB 8-bit */
154 +#define JZ_REG_UDC_OUTCOUNT 0x18 /* bytes in EP0/1 OUT FIFO 16-bit */
156 +#define JZ_REG_UDC_EP_FIFO(x) (4 * (x) + 0x20)
158 +#define JZ_REG_UDC_EPINFO 0x78 /* Endpoint information */
159 +#define JZ_REG_UDC_RAMINFO 0x79 /* RAM information */
161 +#define JZ_REG_UDC_INTR 0x200 /* DMA pending interrupts */
162 +#define JZ_REG_UDC_CNTL1 0x204 /* DMA channel 1 control */
163 +#define JZ_REG_UDC_ADDR1 0x208 /* DMA channel 1 AHB memory addr */
164 +#define JZ_REG_UDC_COUNT1 0x20c /* DMA channel 1 byte count */
165 +#define JZ_REG_UDC_CNTL2 0x214 /* DMA channel 2 control */
166 +#define JZ_REG_UDC_ADDR2 0x218 /* DMA channel 2 AHB memory addr */
167 +#define JZ_REG_UDC_COUNT2 0x21c /* DMA channel 2 byte count */
169 +/* Power register bit masks */
170 +#define USB_POWER_SUSPENDM 0x01
171 +#define USB_POWER_RESUME 0x04
172 +#define USB_POWER_HSMODE 0x10
173 +#define USB_POWER_HSENAB 0x20
174 +#define USB_POWER_SOFTCONN 0x40
176 +/* Interrupt register bit masks */
177 +#define USB_INTR_SUSPEND 0x01
178 +#define USB_INTR_RESUME 0x02
179 +#define USB_INTR_RESET 0x04
181 +#define USB_INTR_EP0 0x0001
182 +#define USB_INTR_INEP1 0x0002
183 +#define USB_INTR_INEP2 0x0004
184 +#define USB_INTR_OUTEP1 0x0002
186 +/* CSR0 bit masks */
187 +#define USB_CSR0_OUTPKTRDY 0x01
188 +#define USB_CSR0_INPKTRDY 0x02
189 +#define USB_CSR0_SENTSTALL 0x04
190 +#define USB_CSR0_DATAEND 0x08
191 +#define USB_CSR0_SETUPEND 0x10
192 +#define USB_CSR0_SENDSTALL 0x20
193 +#define USB_CSR0_SVDOUTPKTRDY 0x40
194 +#define USB_CSR0_SVDSETUPEND 0x80
196 +/* Endpoint CSR register bits */
197 +#define USB_INCSRH_AUTOSET 0x80
198 +#define USB_INCSRH_ISO 0x40
199 +#define USB_INCSRH_MODE 0x20
200 +#define USB_INCSRH_DMAREQENAB 0x10
201 +#define USB_INCSRH_DMAREQMODE 0x04
202 +#define USB_INCSR_CDT 0x40
203 +#define USB_INCSR_SENTSTALL 0x20
204 +#define USB_INCSR_SENDSTALL 0x10
205 +#define USB_INCSR_FF 0x08
206 +#define USB_INCSR_UNDERRUN 0x04
207 +#define USB_INCSR_FFNOTEMPT 0x02
208 +#define USB_INCSR_INPKTRDY 0x01
209 +#define USB_OUTCSRH_AUTOCLR 0x80
210 +#define USB_OUTCSRH_ISO 0x40
211 +#define USB_OUTCSRH_DMAREQENAB 0x20
212 +#define USB_OUTCSRH_DNYT 0x10
213 +#define USB_OUTCSRH_DMAREQMODE 0x08
214 +#define USB_OUTCSR_CDT 0x80
215 +#define USB_OUTCSR_SENTSTALL 0x40
216 +#define USB_OUTCSR_SENDSTALL 0x20
217 +#define USB_OUTCSR_FF 0x10
218 +#define USB_OUTCSR_DATAERR 0x08
219 +#define USB_OUTCSR_OVERRUN 0x04
220 +#define USB_OUTCSR_FFFULL 0x02
221 +#define USB_OUTCSR_OUTPKTRDY 0x01
223 +/* Testmode register bits */
224 +#define USB_TEST_SE0NAK 0x01
225 +#define USB_TEST_J 0x02
226 +#define USB_TEST_K 0x04
227 +#define USB_TEST_PACKET 0x08
229 +/* DMA control bits */
230 +#define USB_CNTL_ENA 0x01
231 +#define USB_CNTL_DIR_IN 0x02
232 +#define USB_CNTL_MODE_1 0x04
233 +#define USB_CNTL_INTR_EN 0x08
234 +#define USB_CNTL_EP(n) ((n) << 4)
235 +#define USB_CNTL_BURST_0 (0 << 9)
236 +#define USB_CNTL_BURST_4 (1 << 9)
237 +#define USB_CNTL_BURST_8 (2 << 9)
238 +#define USB_CNTL_BURST_16 (3 << 9)
242 +# define DEBUG(fmt,args...) do {} while(0)
246 +# define DEBUG_EP0(fmt,args...) do {} while(0)
249 +# define DEBUG_SETUP(fmt,args...) do {} while(0)
252 +static unsigned int use_dma = 0; /* 1: use DMA, 0: use PIO */
254 +module_param(use_dma, int, 0);
255 +MODULE_PARM_DESC(use_dma, "DMA mode enable flag");
257 +struct jz4740_udc *the_controller;
260 + * Local declarations.
262 +static void jz4740_ep0_kick(struct jz4740_udc *dev, struct jz4740_ep *ep);
263 +static void jz4740_handle_ep0(struct jz4740_udc *dev, uint32_t intr);
265 +static void done(struct jz4740_ep *ep, struct jz4740_request *req,
267 +static void pio_irq_enable(struct jz4740_ep *ep);
268 +static void pio_irq_disable(struct jz4740_ep *ep);
269 +static void stop_activity(struct jz4740_udc *dev,
270 + struct usb_gadget_driver *driver);
271 +static void nuke(struct jz4740_ep *ep, int status);
272 +static void flush(struct jz4740_ep *ep);
273 +static void udc_set_address(struct jz4740_udc *dev, unsigned char address);
275 +/*-------------------------------------------------------------------------*/
277 +/* inline functions of register read/write/set/clear */
279 +static inline uint8_t usb_readb(struct jz4740_udc *udc, size_t reg)
281 + return readb(udc->base + reg);
284 +static inline uint16_t usb_readw(struct jz4740_udc *udc, size_t reg)
286 + return readw(udc->base + reg);
289 +static inline uint32_t usb_readl(struct jz4740_udc *udc, size_t reg)
291 + return readl(udc->base + reg);
294 +static inline void usb_writeb(struct jz4740_udc *udc, size_t reg, uint8_t val)
296 + writeb(val, udc->base + reg);
299 +static inline void usb_writew(struct jz4740_udc *udc, size_t reg, uint16_t val)
301 + writew(val, udc->base + reg);
304 +static inline void usb_writel(struct jz4740_udc *udc, size_t reg, uint32_t val)
306 + writel(val, udc->base + reg);
309 +static inline void usb_setb(struct jz4740_udc *udc, size_t reg, uint8_t mask)
311 + usb_writeb(udc, reg, usb_readb(udc, reg) | mask);
314 +static inline void usb_setw(struct jz4740_udc *udc, size_t reg, uint8_t mask)
316 + usb_writew(udc, reg, usb_readw(udc, reg) | mask);
319 +static inline void usb_setl(struct jz4740_udc *udc, size_t reg, uint32_t mask)
321 + usb_writel(udc, reg, usb_readl(udc, reg) | mask);
324 +static inline void usb_clearb(struct jz4740_udc *udc, size_t reg, uint8_t mask)
326 + usb_writeb(udc, reg, usb_readb(udc, reg) & ~mask);
329 +static inline void usb_clearw(struct jz4740_udc *udc, size_t reg, uint16_t mask)
331 + usb_writew(udc, reg, usb_readw(udc, reg) & ~mask);
334 +static inline void usb_clearl(struct jz4740_udc *udc, size_t reg, uint32_t mask)
336 + usb_writel(udc, reg, usb_readl(udc, reg) & ~mask);
339 +/*-------------------------------------------------------------------------*/
341 +static inline void jz_udc_set_index(struct jz4740_udc *udc, uint8_t index)
343 + usb_writeb(udc, JZ_REG_UDC_INDEX, index);
346 +static inline void jz_udc_select_ep(struct jz4740_ep *ep)
348 + jz_udc_set_index(ep->dev, ep_index(ep));
351 +static inline int write_packet(struct jz4740_ep *ep,
352 + struct jz4740_request *req, int max)
355 + int length, nlong, nbyte;
356 + DEBUG("%s:%s[%d]\n", __FILE__, __func__, __LINE__);
358 + buf = req->req.buf + req->req.actual;
361 + length = req->req.length - req->req.actual;
362 + length = min(length, max);
363 + req->req.actual += length;
365 + DEBUG("Write %d (max %d), fifo %x\n", length, max, ep->fifo);
367 + nlong = length >> 2;
368 + nbyte = length & 0x3;
370 + usb_writel(ep->dev, ep->fifo, *((uint32_t *)buf));
374 + usb_writeb(ep->dev, ep->fifo, *buf++);
380 +static inline int read_packet(struct jz4740_ep *ep,
381 + struct jz4740_request *req, int count)
384 + int length, nlong, nbyte;
385 + DEBUG("%s:%s[%d]\n", __FILE__, __func__, __LINE__);
387 + buf = req->req.buf + req->req.actual;
390 + length = req->req.length - req->req.actual;
391 + length = min(length, count);
392 + req->req.actual += length;
394 + DEBUG("Read %d, fifo %x\n", length, ep->fifo);
396 + nlong = length >> 2;
397 + nbyte = length & 0x3;
399 + *((uint32_t *)buf) = usb_readl(ep->dev, ep->fifo);
403 + *buf++ = usb_readb(ep->dev, ep->fifo);
409 +/*-------------------------------------------------------------------------*/
412 + * udc_disable - disable USB device controller
414 +static void udc_disable(struct jz4740_udc *dev)
416 + DEBUG("%s:%s[%d]\n", __FILE__, __func__, __LINE__);
418 + udc_set_address(dev, 0);
420 + /* Disable interrupts */
421 + usb_writew(dev, JZ_REG_UDC_INTRINE, 0);
422 + usb_writew(dev, JZ_REG_UDC_INTROUTE, 0);
423 + usb_writeb(dev, JZ_REG_UDC_INTRUSBE, 0);
426 + usb_writel(dev, JZ_REG_UDC_CNTL1, 0);
427 + usb_writel(dev, JZ_REG_UDC_CNTL2, 0);
429 + /* Disconnect from usb */
430 + usb_clearb(dev, JZ_REG_UDC_POWER, USB_POWER_SOFTCONN);
432 + /* Disable the USB PHY */
433 + clk_disable(dev->clk);
435 + dev->ep0state = WAIT_FOR_SETUP;
436 + dev->gadget.speed = USB_SPEED_UNKNOWN;
442 + * udc_reinit - initialize software state
444 +static void udc_reinit(struct jz4740_udc *dev)
447 + DEBUG("%s:%s[%d]\n", __FILE__, __func__, __LINE__);
449 + /* device/ep0 records init */
450 + INIT_LIST_HEAD(&dev->gadget.ep_list);
451 + INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
452 + dev->ep0state = WAIT_FOR_SETUP;
454 + for (i = 0; i < UDC_MAX_ENDPOINTS; i++) {
455 + struct jz4740_ep *ep = &dev->ep[i];
458 + list_add_tail(&ep->ep.ep_list, &dev->gadget.ep_list);
460 + INIT_LIST_HEAD(&ep->queue);
467 +/* until it's enabled, this UDC should be completely invisible
470 +static void udc_enable(struct jz4740_udc *dev)
473 + DEBUG("%s:%s[%d]\n", __FILE__, __func__, __LINE__);
475 + /* UDC state is incorrect - Added by River */
476 + if (dev->state != UDC_STATE_ENABLE) {
480 + dev->gadget.speed = USB_SPEED_UNKNOWN;
482 + /* Flush FIFO for each */
483 + for (i = 0; i < UDC_MAX_ENDPOINTS; i++) {
484 + struct jz4740_ep *ep = &dev->ep[i];
486 + jz_udc_set_index(dev, ep_index(ep));
490 + /* Set this bit to allow the UDC entering low-power mode when
491 + * there are no actions on the USB bus.
492 + * UDC still works during this bit was set.
494 + jz4740_clock_udc_enable_auto_suspend();
496 + /* Enable the USB PHY */
497 + clk_enable(dev->clk);
499 + /* Disable interrupts */
500 +/* usb_writew(dev, JZ_REG_UDC_INTRINE, 0);
501 + usb_writew(dev, JZ_REG_UDC_INTROUTE, 0);
502 + usb_writeb(dev, JZ_REG_UDC_INTRUSBE, 0);*/
504 + /* Enable interrupts */
505 + usb_setw(dev, JZ_REG_UDC_INTRINE, USB_INTR_EP0);
506 + usb_setb(dev, JZ_REG_UDC_INTRUSBE, USB_INTR_RESET);
507 + /* Don't enable rest of the interrupts */
508 + /* usb_setw(dev, JZ_REG_UDC_INTRINE, USB_INTR_INEP1 | USB_INTR_INEP2);
509 + usb_setw(dev, JZ_REG_UDC_INTROUTE, USB_INTR_OUTEP1); */
511 + /* Enable SUSPEND */
512 + /* usb_setb(dev, JZ_REG_UDC_POWER, USB_POWER_SUSPENDM); */
514 + /* Enable HS Mode */
515 + usb_setb(dev, JZ_REG_UDC_POWER, USB_POWER_HSENAB);
517 + /* Let host detect UDC:
518 + * Software must write a 1 to the PMR:USB_POWER_SOFTCONN bit to turn this
519 + * transistor on and pull the USBDP pin HIGH.
521 + usb_setb(dev, JZ_REG_UDC_POWER, USB_POWER_SOFTCONN);
526 +/*-------------------------------------------------------------------------*/
528 +/* keeping it simple:
529 + * - one bus driver, initted first;
530 + * - one function driver, initted second
534 + * Register entry point for the peripheral controller driver.
537 +int usb_gadget_register_driver(struct usb_gadget_driver *driver)
539 + struct jz4740_udc *dev = the_controller;
542 + if (!driver || !driver->bind) {
554 + /* hook up the driver */
555 + dev->driver = driver;
556 + dev->gadget.dev.driver = &driver->driver;
558 + retval = driver->bind(&dev->gadget);
560 + DEBUG("%s: bind to driver %s --> error %d\n", dev->gadget.name,
561 + driver->driver.name, retval);
566 + /* then enable host detection and ep0; and we're ready
567 + * for set_configuration as well as eventual disconnect.
571 + DEBUG("%s: registered gadget driver '%s'\n", dev->gadget.name,
572 + driver->driver.name);
577 +EXPORT_SYMBOL(usb_gadget_register_driver);
579 +static void stop_activity(struct jz4740_udc *dev,
580 + struct usb_gadget_driver *driver)
584 + DEBUG("%s:%s[%d]\n", __FILE__, __func__, __LINE__);
586 + /* don't disconnect drivers more than once */
587 + if (dev->gadget.speed == USB_SPEED_UNKNOWN)
589 + dev->gadget.speed = USB_SPEED_UNKNOWN;
591 + /* prevent new request submissions, kill any outstanding requests */
592 + for (i = 0; i < UDC_MAX_ENDPOINTS; i++) {
593 + struct jz4740_ep *ep = &dev->ep[i];
597 + jz_udc_set_index(dev, ep_index(ep));
598 + nuke(ep, -ESHUTDOWN);
601 + /* report disconnect; the driver is already quiesced */
603 + spin_unlock(&dev->lock);
604 + driver->disconnect(&dev->gadget);
605 + spin_lock(&dev->lock);
608 + /* re-init driver-visible data structures */
614 + * Unregister entry point for the peripheral controller driver.
616 +int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
618 + struct jz4740_udc *dev = the_controller;
619 + unsigned long flags;
620 + DEBUG("%s:%s[%d]\n", __FILE__, __func__, __LINE__);
624 + if (!driver || driver != dev->driver)
626 + if (!driver->unbind)
629 + spin_lock_irqsave(&dev->lock, flags);
631 + stop_activity(dev, driver);
632 + spin_unlock_irqrestore(&dev->lock, flags);
634 + driver->unbind(&dev->gadget);
638 + DEBUG("unregistered driver '%s'\n", driver->driver.name);
643 +EXPORT_SYMBOL(usb_gadget_unregister_driver);
645 +/*-------------------------------------------------------------------------*/
648 + * Starting DMA using mode 1
650 +static void kick_dma(struct jz4740_ep *ep, struct jz4740_request *req)
652 + struct jz4740_udc *dev = ep->dev;
653 + uint32_t count = req->req.length;
654 + uint32_t physaddr = virt_to_phys((void *)req->req.buf);
656 + DEBUG("%s:%s[%d]\n", __FILE__, __func__, __LINE__);
658 + jz_udc_select_ep(ep);
660 + if (ep_is_in(ep)) { /* Bulk-IN transfer using DMA channel 1 */
661 + ep->reg_addr = JZ_REG_UDC_ADDR1;
663 + dma_cache_wback_inv((unsigned long)req->req.buf, count);
665 + pio_irq_enable(ep);
667 + usb_writeb(dev, JZ_REG_UDC_INCSRH,
668 + USB_INCSRH_DMAREQENAB | USB_INCSRH_AUTOSET | USB_INCSRH_DMAREQMODE);
670 + usb_writel(dev, JZ_REG_UDC_ADDR1, physaddr);
671 + usb_writel(dev, JZ_REG_UDC_COUNT1, count);
672 + usb_writel(dev, JZ_REG_UDC_CNTL1, USB_CNTL_ENA | USB_CNTL_DIR_IN | USB_CNTL_MODE_1 |
673 + USB_CNTL_INTR_EN | USB_CNTL_BURST_16 | USB_CNTL_EP(ep_index(ep)));
675 + else { /* Bulk-OUT transfer using DMA channel 2 */
676 + ep->reg_addr = JZ_REG_UDC_ADDR2;
678 + dma_cache_wback_inv((unsigned long)req->req.buf, count);
680 + pio_irq_enable(ep);
682 + usb_setb(dev, JZ_REG_UDC_OUTCSRH,
683 + USB_OUTCSRH_DMAREQENAB | USB_OUTCSRH_AUTOCLR | USB_OUTCSRH_DMAREQMODE);
685 + usb_writel(dev, JZ_REG_UDC_ADDR2, physaddr);
686 + usb_writel(dev, JZ_REG_UDC_COUNT2, count);
687 + usb_writel(dev, JZ_REG_UDC_CNTL2, USB_CNTL_ENA | USB_CNTL_MODE_1 |
688 + USB_CNTL_INTR_EN | USB_CNTL_BURST_16 | USB_CNTL_EP(ep_index(ep)));
692 +/*-------------------------------------------------------------------------*/
694 +/** Write request to FIFO (max write == maxp size)
695 + * Return: 0 = still running, 1 = completed, negative = errno
696 + * NOTE: INDEX register must be set for EP
698 +static int write_fifo(struct jz4740_ep *ep, struct jz4740_request *req)
700 + struct jz4740_udc *dev = ep->dev;
702 + uint32_t physaddr = virt_to_phys((void *)req->req.buf);
704 + DEBUG("%s:%s[%d]\n", __FILE__, __func__, __LINE__);
705 + max = le16_to_cpu(ep->desc->wMaxPacketSize);
708 + uint32_t dma_count;
710 + /* DMA interrupt generated due to the last packet loaded into the FIFO */
712 + dma_count = usb_readl(dev, ep->reg_addr) - physaddr;
713 + req->req.actual += dma_count;
715 + if (dma_count % max) {
716 + /* If the last packet is less than MAXP, set INPKTRDY manually */
717 + usb_setb(dev, ep->csr, USB_INCSR_INPKTRDY);
721 + if (list_empty(&ep->queue)) {
722 + pio_irq_disable(ep);
726 + /* advance the request queue */
727 + req = list_entry(ep->queue.next, struct jz4740_request, queue);
734 + * PIO mode handling starts here ...
737 + csr = usb_readb(dev, ep->csr);
739 + if (!(csr & USB_INCSR_FFNOTEMPT)) {
741 + int is_last, is_short;
743 + count = write_packet(ep, req, max);
744 + usb_setb(dev, ep->csr, USB_INCSR_INPKTRDY);
746 + /* last packet is usually short (or a zlp) */
747 + if (unlikely(count != max))
748 + is_last = is_short = 1;
750 + if (likely(req->req.length != req->req.actual)
755 + /* interrupt/iso maxpacket may not fill the fifo */
756 + is_short = unlikely(max < ep_maxpacket(ep));
759 + DEBUG("%s: wrote %s %d bytes%s%s %d left %p\n", __FUNCTION__,
760 + ep->ep.name, count,
761 + is_last ? "/L" : "", is_short ? "/S" : "",
762 + req->req.length - req->req.actual, req);
764 + /* requests complete when all IN data is in the FIFO */
767 + if (list_empty(&ep->queue)) {
768 + pio_irq_disable(ep);
773 + DEBUG("Hmm.. %d ep FIFO is not empty!\n", ep_index(ep));
779 +/** Read to request from FIFO (max read == bytes in fifo)
780 + * Return: 0 = still running, 1 = completed, negative = errno
781 + * NOTE: INDEX register must be set for EP
783 +static int read_fifo(struct jz4740_ep *ep, struct jz4740_request *req)
785 + struct jz4740_udc *dev = ep->dev;
787 + unsigned count, is_short;
788 + uint32_t physaddr = virt_to_phys((void *)req->req.buf);
791 + uint32_t dma_count;
793 + /* DMA interrupt generated due to a packet less than MAXP loaded into the FIFO */
795 + dma_count = usb_readl(dev, ep->reg_addr) - physaddr;
796 + req->req.actual += dma_count;
798 + /* Disable interrupt and DMA */
799 + pio_irq_disable(ep);
800 + usb_writel(dev, JZ_REG_UDC_CNTL2, 0);
802 + /* Read all bytes from this packet */
803 + count = usb_readw(dev, JZ_REG_UDC_OUTCOUNT);
804 + count = read_packet(ep, req, count);
807 + /* If the last packet is greater than zero, clear OUTPKTRDY manually */
808 + usb_clearb(dev, ep->csr, USB_OUTCSR_OUTPKTRDY);
812 + if (!list_empty(&ep->queue)) {
813 + /* advance the request queue */
814 + req = list_entry(ep->queue.next, struct jz4740_request, queue);
822 + * PIO mode handling starts here ...
825 + /* make sure there's a packet in the FIFO. */
826 + csr = usb_readb(dev, ep->csr);
827 + if (!(csr & USB_OUTCSR_OUTPKTRDY)) {
828 + DEBUG("%s: Packet NOT ready!\n", __FUNCTION__);
832 + /* read all bytes from this packet */
833 + count = usb_readw(dev, JZ_REG_UDC_OUTCOUNT);
835 + is_short = (count < ep->ep.maxpacket);
837 + count = read_packet(ep, req, count);
839 + DEBUG("read %s %02x, %d bytes%s req %p %d/%d\n",
840 + ep->ep.name, csr, count,
841 + is_short ? "/S" : "", req, req->req.actual, req->req.length);
843 + /* Clear OutPktRdy */
844 + usb_clearb(dev, ep->csr, USB_OUTCSR_OUTPKTRDY);
847 + if (is_short || req->req.actual == req->req.length) {
850 + if (list_empty(&ep->queue))
851 + pio_irq_disable(ep);
855 + /* finished that packet. the next one may be waiting... */
860 + * done - retire a request; caller blocked irqs
861 + * INDEX register is preserved to keep same
863 +static void done(struct jz4740_ep *ep, struct jz4740_request *req, int status)
865 + unsigned int stopped = ep->stopped;
868 + DEBUG("%s, %p\n", __FUNCTION__, ep);
869 + list_del_init(&req->queue);
871 + if (likely(req->req.status == -EINPROGRESS))
872 + req->req.status = status;
874 + status = req->req.status;
876 + if (status && status != -ESHUTDOWN)
877 + DEBUG("complete %s req %p stat %d len %u/%u\n",
878 + ep->ep.name, &req->req, status,
879 + req->req.actual, req->req.length);
881 + /* don't modify queue heads during completion callback */
883 + /* Read current index (completion may modify it) */
884 + index = usb_readb(ep->dev, JZ_REG_UDC_INDEX);
885 + spin_unlock_irqrestore(&ep->dev->lock, ep->dev->lock_flags);
887 + req->req.complete(&ep->ep, &req->req);
889 + spin_lock_irqsave(&ep->dev->lock, ep->dev->lock_flags);
890 + /* Restore index */
891 + jz_udc_set_index(ep->dev, index);
892 + ep->stopped = stopped;
895 +/** Enable EP interrupt */
896 +static void pio_irq_enable(struct jz4740_ep *ep)
898 + uint8_t index = ep_index(ep);
899 + struct jz4740_udc *dev = ep->dev;
900 + DEBUG("%s: EP%d %s\n", __FUNCTION__, ep_index(ep), ep_is_in(ep) ? "IN": "OUT");
902 + if (ep_is_in(ep)) {
906 + usb_setw(dev, JZ_REG_UDC_INTRINE, BIT(index));
907 + dev->in_mask |= BIT(index);
910 + DEBUG("Unknown endpoint: %d\n", index);
917 + usb_setw(dev, JZ_REG_UDC_INTROUTE, BIT(index));
918 + dev->out_mask |= BIT(index);
921 + DEBUG("Unknown endpoint: %d\n", index);
927 +/** Disable EP interrupt */
928 +static void pio_irq_disable(struct jz4740_ep *ep)
930 + uint8_t index = ep_index(ep);
931 + struct jz4740_udc *dev = ep->dev;
933 + DEBUG("%s: EP%d %s\n", __FUNCTION__, ep_index(ep), ep_is_in(ep) ? "IN": "OUT");
935 + if (ep_is_in(ep)) {
936 + switch (ep_index(ep)) {
939 + usb_clearw(ep->dev, JZ_REG_UDC_INTRINE, BIT(index));
940 + dev->in_mask &= ~BIT(index);
943 + DEBUG("Unknown endpoint: %d\n", index);
948 + switch (ep_index(ep)) {
950 + usb_clearw(ep->dev, JZ_REG_UDC_INTROUTE, BIT(index));
951 + dev->out_mask &= ~BIT(index);
954 + DEBUG("Unknown endpoint: %d\n", index);
961 + * nuke - dequeue ALL requests
963 +static void nuke(struct jz4740_ep *ep, int status)
965 + struct jz4740_request *req;
967 + DEBUG("%s, %p\n", __FUNCTION__, ep);
972 + /* called with irqs blocked */
973 + while (!list_empty(&ep->queue)) {
974 + req = list_entry(ep->queue.next, struct jz4740_request, queue);
975 + done(ep, req, status);
978 + /* Disable IRQ if EP is enabled (has descriptor) */
980 + pio_irq_disable(ep);
984 + * NOTE: INDEX register must be set before this call
986 +static void flush(struct jz4740_ep *ep)
988 + DEBUG("%s: %s\n", __FUNCTION__, ep->ep.name);
990 + switch (ep->type) {
993 + usb_setb(ep->dev, ep->csr, USB_INCSR_FF);
996 + usb_setb(ep->dev, ep->csr, USB_OUTCSR_FF);
1004 + * jz4740_in_epn - handle IN interrupt
1006 +static void jz4740_in_epn(struct jz4740_udc *dev, uint32_t ep_idx, uint32_t intr)
1009 + struct jz4740_ep *ep = &dev->ep[ep_idx + 1];
1010 + struct jz4740_request *req;
1011 + DEBUG("%s:%s[%d]\n", __FILE__, __func__, __LINE__);
1013 + jz_udc_set_index(dev, ep_index(ep));
1015 + csr = usb_readb(dev, ep->csr);
1016 + DEBUG("%s: %d, csr %x\n", __FUNCTION__, ep_idx, csr);
1018 + if (csr & USB_INCSR_SENTSTALL) {
1019 + DEBUG("USB_INCSR_SENTSTALL\n");
1020 + usb_clearb(dev, ep->csr, USB_INCSR_SENTSTALL);
1025 + DEBUG("%s: NO EP DESC\n", __FUNCTION__);
1029 + if (list_empty(&ep->queue))
1032 + req = list_entry(ep->queue.next, struct jz4740_request, queue);
1034 + DEBUG("req: %p\n", req);
1039 + write_fifo(ep, req);
1045 +static void jz4740_out_epn(struct jz4740_udc *dev, uint32_t ep_idx, uint32_t intr)
1047 + struct jz4740_ep *ep = &dev->ep[ep_idx];
1048 + struct jz4740_request *req;
1050 + DEBUG("%s: %d\n", __FUNCTION__, ep_idx);
1052 + jz_udc_set_index(dev, ep_index(ep));
1057 + /* DMA starts here ... */
1058 + if (list_empty(&ep->queue))
1061 + req = list_entry(ep->queue.next, struct jz4740_request, queue);
1064 + read_fifo(ep, req);
1069 + * PIO mode starts here ...
1072 + while ((csr = usb_readb(dev, ep->csr)) &
1073 + (USB_OUTCSR_OUTPKTRDY | USB_OUTCSR_SENTSTALL)) {
1074 + DEBUG("%s: %x\n", __FUNCTION__, csr);
1076 + if (csr & USB_OUTCSR_SENTSTALL) {
1077 + DEBUG("%s: stall sent, flush fifo\n",
1079 + /* usb_set(USB_OUT_CSR1_FIFO_FLUSH, ep->csr1); */
1081 + } else if (csr & USB_OUTCSR_OUTPKTRDY) {
1082 + if (list_empty(&ep->queue))
1086 + list_entry(ep->queue.next,
1087 + struct jz4740_request,
1091 + DEBUG("%s: NULL REQ %d\n",
1092 + __FUNCTION__, ep_idx);
1095 + read_fifo(ep, req);
1100 + /* Throw packet away.. */
1101 + DEBUG("%s: ep %p ep_indx %d No descriptor?!?\n", __FUNCTION__, ep, ep_idx);
1106 +/** Halt specific EP
1107 + * Return 0 if success
1108 + * NOTE: Sets INDEX register to EP !
1110 +static int jz4740_set_halt(struct usb_ep *_ep, int value)
1112 + struct jz4740_udc *dev;
1113 + struct jz4740_ep *ep;
1114 + unsigned long flags;
1116 + DEBUG("%s:%s[%d]\n", __FILE__, __func__, __LINE__);
1118 + ep = container_of(_ep, struct jz4740_ep, ep);
1119 + if (unlikely(!_ep || (!ep->desc && ep->type != ep_control))) {
1120 + DEBUG("%s, bad ep\n", __FUNCTION__);
1126 + spin_lock_irqsave(&dev->lock, flags);
1128 + jz_udc_select_ep(ep);
1130 + DEBUG("%s, ep %d, val %d\n", __FUNCTION__, ep_index(ep), value);
1132 + if (ep_index(ep) == 0) {
1134 + usb_setb(dev, JZ_REG_UDC_CSR0, USB_CSR0_SENDSTALL);
1135 + } else if (ep_is_in(ep)) {
1136 + uint32_t csr = usb_readb(dev, ep->csr);
1137 + if (value && ((csr & USB_INCSR_FFNOTEMPT)
1138 + || !list_empty(&ep->queue))) {
1140 + * Attempts to halt IN endpoints will fail (returning -EAGAIN)
1141 + * if any transfer requests are still queued, or if the controller
1142 + * FIFO still holds bytes that the host hasn
\92t collected.
1144 + spin_unlock_irqrestore(&dev->lock, flags);
1146 + ("Attempt to halt IN endpoint failed (returning -EAGAIN) %d %d\n",
1147 + (csr & USB_INCSR_FFNOTEMPT),
1148 + !list_empty(&ep->queue));
1153 + usb_setb(dev, ep->csr, USB_INCSR_SENDSTALL);
1156 + usb_clearb(dev, ep->csr, USB_INCSR_SENDSTALL);
1157 + usb_setb(dev, ep->csr, USB_INCSR_CDT);
1163 + usb_setb(dev, ep->csr, USB_OUTCSR_SENDSTALL);
1166 + usb_clearb(dev, ep->csr, USB_OUTCSR_SENDSTALL);
1167 + usb_setb(dev, ep->csr, USB_OUTCSR_CDT);
1177 + spin_unlock_irqrestore(&dev->lock, flags);
1179 + DEBUG("%s %s halted\n", _ep->name, value == 0 ? "NOT" : "IS");
1185 +static int jz4740_ep_enable(struct usb_ep *_ep,
1186 + const struct usb_endpoint_descriptor *desc)
1188 + struct jz4740_ep *ep;
1189 + struct jz4740_udc *dev;
1190 + unsigned long flags;
1191 + uint32_t max, csrh = 0;
1193 + DEBUG("%s: trying to enable %s\n", __FUNCTION__, _ep->name);
1195 + if (!_ep || !desc)
1198 + ep = container_of(_ep, struct jz4740_ep, ep);
1199 + if (ep->desc || ep->type == ep_control
1200 + || desc->bDescriptorType != USB_DT_ENDPOINT
1201 + || ep->bEndpointAddress != desc->bEndpointAddress) {
1202 + DEBUG("%s, bad ep or descriptor\n", __FUNCTION__);
1206 + /* xfer types must match, except that interrupt ~= bulk */
1207 + if (ep->bmAttributes != desc->bmAttributes
1208 + && ep->bmAttributes != USB_ENDPOINT_XFER_BULK
1209 + && desc->bmAttributes != USB_ENDPOINT_XFER_INT) {
1210 + DEBUG("%s, %s type mismatch\n", __FUNCTION__, _ep->name);
1215 + if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
1216 + DEBUG("%s, bogus device state\n", __FUNCTION__);
1217 + return -ESHUTDOWN;
1220 + max = le16_to_cpu(desc->wMaxPacketSize);
1222 + spin_lock_irqsave(&ep->dev->lock, flags);
1224 + /* Configure the endpoint */
1225 + jz_udc_set_index(dev, desc->bEndpointAddress & 0x0F);
1226 + if (ep_is_in(ep)) {
1227 + usb_writew(dev, JZ_REG_UDC_INMAXP, max);
1228 + switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
1229 + case USB_ENDPOINT_XFER_BULK:
1230 + case USB_ENDPOINT_XFER_INT:
1231 + csrh &= ~USB_INCSRH_ISO;
1233 + case USB_ENDPOINT_XFER_ISOC:
1234 + csrh |= USB_INCSRH_ISO;
1237 + usb_writeb(dev, JZ_REG_UDC_INCSRH, csrh);
1240 + usb_writew(dev, JZ_REG_UDC_OUTMAXP, max);
1241 + switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
1242 + case USB_ENDPOINT_XFER_BULK:
1243 + csrh &= ~USB_OUTCSRH_ISO;
1245 + case USB_ENDPOINT_XFER_INT:
1246 + csrh &= ~USB_OUTCSRH_ISO;
1247 + csrh |= USB_OUTCSRH_DNYT;
1249 + case USB_ENDPOINT_XFER_ISOC:
1250 + csrh |= USB_OUTCSRH_ISO;
1253 + usb_writeb(dev, JZ_REG_UDC_OUTCSRH, csrh);
1260 + ep->ep.maxpacket = max;
1262 + spin_unlock_irqrestore(&ep->dev->lock, flags);
1264 + /* Reset halt state (does flush) */
1265 + jz4740_set_halt(_ep, 0);
1267 + DEBUG("%s: enabled %s\n", __FUNCTION__, _ep->name);
1273 + * NOTE: Sets INDEX register
1275 +static int jz4740_ep_disable(struct usb_ep *_ep)
1277 + struct jz4740_ep *ep;
1278 + unsigned long flags;
1280 + DEBUG("%s, %p\n", __FUNCTION__, _ep);
1282 + ep = container_of(_ep, struct jz4740_ep, ep);
1283 + if (!_ep || !ep->desc) {
1284 + DEBUG("%s, %s not enabled\n", __FUNCTION__,
1285 + _ep ? ep->ep.name : NULL);
1289 + spin_lock_irqsave(&ep->dev->lock, flags);
1291 + jz_udc_select_ep(ep);
1293 + /* Nuke all pending requests (does flush) */
1294 + nuke(ep, -ESHUTDOWN);
1296 + /* Disable ep IRQ */
1297 + pio_irq_disable(ep);
1302 + spin_unlock_irqrestore(&ep->dev->lock, flags);
1304 + DEBUG("%s: disabled %s\n", __FUNCTION__, _ep->name);
1308 +static struct usb_request *jz4740_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
1310 + struct jz4740_request *req;
1312 + DEBUG("%s, %p\n", __FUNCTION__, ep);
1314 + req = kzalloc(sizeof(*req), gfp_flags);
1318 + INIT_LIST_HEAD(&req->queue);
1323 +static void jz4740_free_request(struct usb_ep *ep, struct usb_request *_req)
1325 + struct jz4740_request *req;
1327 + DEBUG("%s, %p\n", __FUNCTION__, ep);
1329 + req = container_of(_req, struct jz4740_request, req);
1330 + WARN_ON(!list_empty(&req->queue));
1334 +/*--------------------------------------------------------------------*/
1336 +/** Queue one request
1337 + * Kickstart transfer if needed
1338 + * NOTE: Sets INDEX register
1340 +static int jz4740_queue(struct usb_ep *_ep, struct usb_request *_req,
1343 + struct jz4740_request *req;
1344 + struct jz4740_ep *ep;
1345 + struct jz4740_udc *dev;
1347 + DEBUG("%s, %p\n", __FUNCTION__, _ep);
1349 + req = container_of(_req, struct jz4740_request, req);
1351 + (!_req || !_req->complete || !_req->buf
1352 + || !list_empty(&req->queue))) {
1353 + DEBUG("%s, bad params\n", __FUNCTION__);
1357 + ep = container_of(_ep, struct jz4740_ep, ep);
1358 + if (unlikely(!_ep || (!ep->desc && ep->type != ep_control))) {
1359 + DEBUG("%s, bad ep\n", __FUNCTION__);
1364 + if (unlikely(!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)) {
1365 + DEBUG("%s, bogus device state %p\n", __FUNCTION__, dev->driver);
1366 + return -ESHUTDOWN;
1369 + DEBUG("%s queue req %p, len %d buf %p\n", _ep->name, _req, _req->length,
1372 + spin_lock_irqsave(&dev->lock, dev->lock_flags);
1374 + _req->status = -EINPROGRESS;
1377 + /* kickstart this i/o queue? */
1378 + DEBUG("Add to %d Q %d %d\n", ep_index(ep), list_empty(&ep->queue),
1380 + if (list_empty(&ep->queue) && likely(!ep->stopped)) {
1383 + if (unlikely(ep_index(ep) == 0)) {
1385 + list_add_tail(&req->queue, &ep->queue);
1386 + jz4740_ep0_kick(dev, ep);
1388 + } else if (use_dma) {
1390 + kick_dma(ep, req);
1393 + else if (ep_is_in(ep)) {
1395 + jz_udc_set_index(dev, ep_index(ep));
1396 + csr = usb_readb(dev, ep->csr);
1397 + pio_irq_enable(ep);
1398 + if (!(csr & USB_INCSR_FFNOTEMPT)) {
1399 + if (write_fifo(ep, req) == 1)
1404 + jz_udc_set_index(dev, ep_index(ep));
1405 + csr = usb_readb(dev, ep->csr);
1406 + pio_irq_enable(ep);
1407 + if (csr & USB_OUTCSR_OUTPKTRDY) {
1408 + if (read_fifo(ep, req) == 1)
1414 + /* pio or dma irq handler advances the queue. */
1415 + if (likely(req != 0))
1416 + list_add_tail(&req->queue, &ep->queue);
1418 + spin_unlock_irqrestore(&dev->lock, dev->lock_flags);
1423 +/* dequeue JUST ONE request */
1424 +static int jz4740_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1426 + struct jz4740_ep *ep;
1427 + struct jz4740_request *req;
1428 + unsigned long flags;
1430 + DEBUG("%s, %p\n", __FUNCTION__, _ep);
1432 + ep = container_of(_ep, struct jz4740_ep, ep);
1433 + if (!_ep || ep->type == ep_control)
1436 + spin_lock_irqsave(&ep->dev->lock, flags);
1438 + /* make sure it's actually queued on this endpoint */
1439 + list_for_each_entry(req, &ep->queue, queue) {
1440 + if (&req->req == _req)
1443 + if (&req->req != _req) {
1444 + spin_unlock_irqrestore(&ep->dev->lock, flags);
1447 + done(ep, req, -ECONNRESET);
1449 + spin_unlock_irqrestore(&ep->dev->lock, flags);
1453 +/** Return bytes in EP FIFO
1454 + * NOTE: Sets INDEX register to EP
1456 +static int jz4740_fifo_status(struct usb_ep *_ep)
1460 + struct jz4740_ep *ep;
1461 + unsigned long flags;
1463 + ep = container_of(_ep, struct jz4740_ep, ep);
1465 + DEBUG("%s, bad ep\n", __FUNCTION__);
1469 + DEBUG("%s, %d\n", __FUNCTION__, ep_index(ep));
1471 + /* LPD can't report unclaimed bytes from IN fifos */
1473 + return -EOPNOTSUPP;
1475 + spin_lock_irqsave(&ep->dev->lock, flags);
1476 + jz_udc_set_index(ep->dev, ep_index(ep));
1478 + csr = usb_readb(ep->dev, ep->csr);
1479 + if (ep->dev->gadget.speed != USB_SPEED_UNKNOWN ||
1481 + count = usb_readw(ep->dev, JZ_REG_UDC_OUTCOUNT);
1484 + spin_unlock_irqrestore(&ep->dev->lock, flags);
1490 + * NOTE: Sets INDEX register to EP
1492 +static void jz4740_fifo_flush(struct usb_ep *_ep)
1494 + struct jz4740_ep *ep;
1495 + unsigned long flags;
1497 + DEBUG("%s:%s[%d]\n", __FILE__, __func__, __LINE__);
1499 + ep = container_of(_ep, struct jz4740_ep, ep);
1500 + if (unlikely(!_ep || (!ep->desc && ep->type == ep_control))) {
1501 + DEBUG("%s, bad ep\n", __FUNCTION__);
1505 + spin_lock_irqsave(&ep->dev->lock, flags);
1507 + jz_udc_set_index(ep->dev, ep_index(ep));
1510 + spin_unlock_irqrestore(&ep->dev->lock, flags);
1513 +/****************************************************************/
1514 +/* End Point 0 related functions */
1515 +/****************************************************************/
1517 +/* return: 0 = still running, 1 = completed, negative = errno */
1518 +static int write_fifo_ep0(struct jz4740_ep *ep, struct jz4740_request *req)
1524 + DEBUG("%s:%s[%d]\n", __FILE__, __func__, __LINE__);
1525 + max = ep_maxpacket(ep);
1527 + count = write_packet(ep, req, max);
1529 + /* last packet is usually short (or a zlp) */
1530 + if (unlikely(count != max))
1533 + if (likely(req->req.length != req->req.actual) || req->req.zero)
1539 + DEBUG_EP0("%s: wrote %s %d bytes%s %d left %p\n", __FUNCTION__,
1540 + ep->ep.name, count,
1541 + is_last ? "/L" : "", req->req.length - req->req.actual, req);
1543 + /* requests complete when all IN data is in the FIFO */
1552 +static inline int jz4740_fifo_read(struct jz4740_ep *ep,
1553 + unsigned char *cp, int max)
1556 + int count = usb_readw(ep->dev, JZ_REG_UDC_OUTCOUNT);
1562 + *cp++ = usb_readb(ep->dev, ep->fifo);
1567 +static inline void jz4740_fifo_write(struct jz4740_ep *ep,
1568 + unsigned char *cp, int count)
1570 + DEBUG("fifo_write: %d %d\n", ep_index(ep), count);
1572 + usb_writeb(ep->dev, ep->fifo, *cp++);
1575 +static int read_fifo_ep0(struct jz4740_ep *ep, struct jz4740_request *req)
1577 + struct jz4740_udc *dev = ep->dev;
1580 + unsigned bufferspace, count, is_short;
1582 + DEBUG_EP0("%s\n", __FUNCTION__);
1584 + csr = usb_readb(dev, JZ_REG_UDC_CSR0);
1585 + if (!(csr & USB_CSR0_OUTPKTRDY))
1588 + buf = req->req.buf + req->req.actual;
1590 + bufferspace = req->req.length - req->req.actual;
1592 + /* read all bytes from this packet */
1593 + if (likely(csr & USB_CSR0_OUTPKTRDY)) {
1594 + count = usb_readw(dev, JZ_REG_UDC_OUTCOUNT);
1595 + req->req.actual += min(count, bufferspace);
1599 + is_short = (count < ep->ep.maxpacket);
1600 + DEBUG_EP0("read %s %02x, %d bytes%s req %p %d/%d\n",
1601 + ep->ep.name, csr, count,
1602 + is_short ? "/S" : "", req, req->req.actual, req->req.length);
1604 + while (likely(count-- != 0)) {
1605 + uint8_t byte = (uint8_t)usb_readl(dev, ep->fifo);
1607 + if (unlikely(bufferspace == 0)) {
1608 + /* this happens when the driver's buffer
1609 + * is smaller than what the host sent.
1610 + * discard the extra data.
1612 + if (req->req.status != -EOVERFLOW)
1613 + DEBUG_EP0("%s overflow %d\n", ep->ep.name,
1615 + req->req.status = -EOVERFLOW;
1623 + if (is_short || req->req.actual == req->req.length) {
1628 + /* finished that packet. the next one may be waiting... */
1633 + * udc_set_address - set the USB address for this device
1636 + * Called from control endpoint function after it decodes a set address setup packet.
1638 +static void udc_set_address(struct jz4740_udc *dev, unsigned char address)
1640 + DEBUG_EP0("%s: %d\n", __FUNCTION__, address);
1642 + dev->usb_address = address;
1643 + usb_writeb(dev, JZ_REG_UDC_FADDR, address);
1647 + * DATA_STATE_RECV (USB_CSR0_OUTPKTRDY)
1649 + * set USB_CSR0_SVDOUTPKTRDY | USB_CSR0_DATAEND | USB_CSR0_SENDSTALL bits
1651 + * set USB_CSR0_SVDOUTPKTRDY bit
1652 + if last set USB_CSR0_DATAEND bit
1654 +static void jz4740_ep0_out(struct jz4740_udc *dev, uint32_t csr, int kickstart)
1656 + struct jz4740_request *req;
1657 + struct jz4740_ep *ep = &dev->ep[0];
1660 + DEBUG_EP0("%s: %x\n", __FUNCTION__, csr);
1662 + if (list_empty(&ep->queue))
1665 + req = list_entry(ep->queue.next, struct jz4740_request, queue);
1668 + if (req->req.length == 0) {
1669 + DEBUG_EP0("ZERO LENGTH OUT!\n");
1670 + usb_setb(dev, JZ_REG_UDC_CSR0, (USB_CSR0_SVDOUTPKTRDY | USB_CSR0_DATAEND));
1671 + dev->ep0state = WAIT_FOR_SETUP;
1673 + } else if (kickstart) {
1674 + usb_setb(dev, JZ_REG_UDC_CSR0, (USB_CSR0_SVDOUTPKTRDY));
1677 + ret = read_fifo_ep0(ep, req);
1680 + DEBUG_EP0("%s: finished, waiting for status\n",
1682 + usb_setb(dev, JZ_REG_UDC_CSR0, (USB_CSR0_SVDOUTPKTRDY | USB_CSR0_DATAEND));
1683 + dev->ep0state = WAIT_FOR_SETUP;
1685 + /* Not done yet.. */
1686 + DEBUG_EP0("%s: not finished\n", __FUNCTION__);
1687 + usb_setb(dev, JZ_REG_UDC_CSR0, USB_CSR0_SVDOUTPKTRDY);
1690 + DEBUG_EP0("NO REQ??!\n");
1697 +static int jz4740_ep0_in(struct jz4740_udc *dev, uint32_t csr)
1699 + struct jz4740_request *req;
1700 + struct jz4740_ep *ep = &dev->ep[0];
1701 + int ret, need_zlp = 0;
1703 + DEBUG_EP0("%s: %x\n", __FUNCTION__, csr);
1705 + if (list_empty(&ep->queue))
1708 + req = list_entry(ep->queue.next, struct jz4740_request, queue);
1711 + DEBUG_EP0("%s: NULL REQ\n", __FUNCTION__);
1715 + if (req->req.length == 0) {
1716 + usb_setb(dev, JZ_REG_UDC_CSR0, (USB_CSR0_INPKTRDY | USB_CSR0_DATAEND));
1717 + dev->ep0state = WAIT_FOR_SETUP;
1721 + if (req->req.length - req->req.actual == EP0_MAXPACKETSIZE) {
1722 + /* Next write will end with the packet size, */
1723 + /* so we need zero-length-packet */
1727 + ret = write_fifo_ep0(ep, req);
1729 + if (ret == 1 && !need_zlp) {
1731 + DEBUG_EP0("%s: finished, waiting for status\n", __FUNCTION__);
1733 + usb_setb(dev, JZ_REG_UDC_CSR0, (USB_CSR0_INPKTRDY | USB_CSR0_DATAEND));
1734 + dev->ep0state = WAIT_FOR_SETUP;
1736 + DEBUG_EP0("%s: not finished\n", __FUNCTION__);
1737 + usb_setb(dev, JZ_REG_UDC_CSR0, USB_CSR0_INPKTRDY);
1741 + DEBUG_EP0("%s: Need ZLP!\n", __FUNCTION__);
1742 + usb_setb(dev, JZ_REG_UDC_CSR0, USB_CSR0_INPKTRDY);
1743 + dev->ep0state = DATA_STATE_NEED_ZLP;
1749 +static int jz4740_handle_get_status(struct jz4740_udc *dev,
1750 + struct usb_ctrlrequest *ctrl)
1752 + struct jz4740_ep *ep0 = &dev->ep[0];
1753 + struct jz4740_ep *qep;
1754 + int reqtype = (ctrl->bRequestType & USB_RECIP_MASK);
1757 + DEBUG("%s:%s[%d]\n", __FILE__, __func__, __LINE__);
1759 + if (reqtype == USB_RECIP_INTERFACE) {
1760 + /* This is not supported.
1761 + * And according to the USB spec, this one does nothing..
1764 + DEBUG_SETUP("GET_STATUS: USB_RECIP_INTERFACE\n");
1765 + } else if (reqtype == USB_RECIP_DEVICE) {
1766 + DEBUG_SETUP("GET_STATUS: USB_RECIP_DEVICE\n");
1767 + val |= (1 << 0); /* Self powered */
1768 + /*val |= (1<<1); *//* Remote wakeup */
1769 + } else if (reqtype == USB_RECIP_ENDPOINT) {
1770 + int ep_num = (ctrl->wIndex & ~USB_DIR_IN);
1773 + ("GET_STATUS: USB_RECIP_ENDPOINT (%d), ctrl->wLength = %d\n",
1774 + ep_num, ctrl->wLength);
1776 + if (ctrl->wLength > 2 || ep_num > 3)
1777 + return -EOPNOTSUPP;
1779 + qep = &dev->ep[ep_num];
1780 + if (ep_is_in(qep) != ((ctrl->wIndex & USB_DIR_IN) ? 1 : 0)
1781 + && ep_index(qep) != 0) {
1782 + return -EOPNOTSUPP;
1785 + jz_udc_set_index(dev, ep_index(qep));
1787 + /* Return status on next IN token */
1788 + switch (qep->type) {
1791 + (usb_readb(dev, qep->csr) & USB_CSR0_SENDSTALL) ==
1792 + USB_CSR0_SENDSTALL;
1795 + case ep_interrupt:
1797 + (usb_readb(dev, qep->csr) & USB_INCSR_SENDSTALL) ==
1798 + USB_INCSR_SENDSTALL;
1802 + (usb_readb(dev, qep->csr) & USB_OUTCSR_SENDSTALL) ==
1803 + USB_OUTCSR_SENDSTALL;
1807 + /* Back to EP0 index */
1808 + jz_udc_set_index(dev, 0);
1810 + DEBUG_SETUP("GET_STATUS, ep: %d (%x), val = %d\n", ep_num,
1811 + ctrl->wIndex, val);
1813 + DEBUG_SETUP("Unknown REQ TYPE: %d\n", reqtype);
1814 + return -EOPNOTSUPP;
1817 + /* Clear "out packet ready" */
1818 + usb_setb(dev, JZ_REG_UDC_CSR0, USB_CSR0_SVDOUTPKTRDY);
1819 + /* Put status to FIFO */
1820 + jz4740_fifo_write(ep0, (uint8_t *)&val, sizeof(val));
1821 + /* Issue "In packet ready" */
1822 + usb_setb(dev, JZ_REG_UDC_CSR0, (USB_CSR0_INPKTRDY | USB_CSR0_DATAEND));
1828 + * WAIT_FOR_SETUP (OUTPKTRDY)
1829 + * - read data packet from EP0 FIFO
1830 + * - decode command
1832 + * set USB_CSR0_SVDOUTPKTRDY | USB_CSR0_DATAEND | USB_CSR0_SENDSTALL bits
1834 + * set USB_CSR0_SVDOUTPKTRDY | USB_CSR0_DATAEND bits
1836 +static void jz4740_ep0_setup(struct jz4740_udc *dev, uint32_t csr)
1838 + struct jz4740_ep *ep = &dev->ep[0];
1839 + struct usb_ctrlrequest ctrl;
1842 + DEBUG_SETUP("%s: %x\n", __FUNCTION__, csr);
1844 + /* Nuke all previous transfers */
1845 + nuke(ep, -EPROTO);
1847 + /* read control req from fifo (8 bytes) */
1848 + jz4740_fifo_read(ep, (unsigned char *)&ctrl, 8);
1850 + DEBUG_SETUP("SETUP %02x.%02x v%04x i%04x l%04x\n",
1851 + ctrl.bRequestType, ctrl.bRequest,
1852 + ctrl.wValue, ctrl.wIndex, ctrl.wLength);
1854 + /* Set direction of EP0 */
1855 + if (likely(ctrl.bRequestType & USB_DIR_IN)) {
1856 + ep->bEndpointAddress |= USB_DIR_IN;
1858 + ep->bEndpointAddress &= ~USB_DIR_IN;
1861 + /* Handle some SETUP packets ourselves */
1862 + switch (ctrl.bRequest) {
1863 + case USB_REQ_SET_ADDRESS:
1864 + if (ctrl.bRequestType != (USB_TYPE_STANDARD | USB_RECIP_DEVICE))
1867 + DEBUG_SETUP("USB_REQ_SET_ADDRESS (%d)\n", ctrl.wValue);
1868 + udc_set_address(dev, ctrl.wValue);
1869 + usb_setb(dev, JZ_REG_UDC_CSR0, (USB_CSR0_SVDOUTPKTRDY | USB_CSR0_DATAEND));
1872 + case USB_REQ_SET_CONFIGURATION:
1873 + if (ctrl.bRequestType != (USB_TYPE_STANDARD | USB_RECIP_DEVICE))
1876 + DEBUG_SETUP("USB_REQ_SET_CONFIGURATION (%d)\n", ctrl.wValue);
1877 +/* usb_setb(JZ_REG_UDC_CSR0, (USB_CSR0_SVDOUTPKTRDY | USB_CSR0_DATAEND));*/
1879 + /* Enable RESUME and SUSPEND interrupts */
1880 + usb_setb(dev, JZ_REG_UDC_INTRUSBE, (USB_INTR_RESUME | USB_INTR_SUSPEND));
1883 + case USB_REQ_SET_INTERFACE:
1884 + if (ctrl.bRequestType != (USB_TYPE_STANDARD | USB_RECIP_DEVICE))
1887 + DEBUG_SETUP("USB_REQ_SET_INTERFACE (%d)\n", ctrl.wValue);
1888 +/* usb_setb(JZ_REG_UDC_CSR0, (USB_CSR0_SVDOUTPKTRDY | USB_CSR0_DATAEND));*/
1891 + case USB_REQ_GET_STATUS:
1892 + if (jz4740_handle_get_status(dev, &ctrl) == 0)
1895 + case USB_REQ_CLEAR_FEATURE:
1896 + case USB_REQ_SET_FEATURE:
1897 + if (ctrl.bRequestType == USB_RECIP_ENDPOINT) {
1898 + struct jz4740_ep *qep;
1899 + int ep_num = (ctrl.wIndex & 0x0f);
1901 + /* Support only HALT feature */
1902 + if (ctrl.wValue != 0 || ctrl.wLength != 0
1903 + || ep_num > 3 || ep_num < 1)
1906 + qep = &dev->ep[ep_num];
1907 + spin_unlock(&dev->lock);
1908 + if (ctrl.bRequest == USB_REQ_SET_FEATURE) {
1909 + DEBUG_SETUP("SET_FEATURE (%d)\n",
1911 + jz4740_set_halt(&qep->ep, 1);
1913 + DEBUG_SETUP("CLR_FEATURE (%d)\n",
1915 + jz4740_set_halt(&qep->ep, 0);
1917 + spin_lock(&dev->lock);
1919 + jz_udc_set_index(dev, 0);
1921 + /* Reply with a ZLP on next IN token */
1922 + usb_setb(dev, JZ_REG_UDC_CSR0,
1923 + (USB_CSR0_SVDOUTPKTRDY | USB_CSR0_DATAEND));
1932 + /* gadget drivers see class/vendor specific requests,
1933 + * {SET,GET}_{INTERFACE,DESCRIPTOR,CONFIGURATION},
1936 + if (dev->driver) {
1937 + /* device-2-host (IN) or no data setup command, process immediately */
1938 + spin_unlock(&dev->lock);
1940 + i = dev->driver->setup(&dev->gadget, &ctrl);
1941 + spin_lock(&dev->lock);
1943 + if (unlikely(i < 0)) {
1944 + /* setup processing failed, force stall */
1946 + (" --> ERROR: gadget setup FAILED (stalling), setup returned %d\n",
1948 + jz_udc_set_index(dev, 0);
1949 + usb_setb(dev, JZ_REG_UDC_CSR0, (USB_CSR0_SVDOUTPKTRDY | USB_CSR0_DATAEND | USB_CSR0_SENDSTALL));
1951 + /* ep->stopped = 1; */
1952 + dev->ep0state = WAIT_FOR_SETUP;
1955 + DEBUG_SETUP("gadget driver setup ok (%d)\n", ctrl.wLength);
1956 +/* if (!ctrl.wLength) {
1957 + usb_setb(JZ_REG_UDC_CSR0, USB_CSR0_SVDOUTPKTRDY);
1964 + * DATA_STATE_NEED_ZLP
1966 +static void jz4740_ep0_in_zlp(struct jz4740_udc *dev, uint32_t csr)
1968 + DEBUG_EP0("%s: %x\n", __FUNCTION__, csr);
1970 + usb_setb(dev, JZ_REG_UDC_CSR0, (USB_CSR0_INPKTRDY | USB_CSR0_DATAEND));
1971 + dev->ep0state = WAIT_FOR_SETUP;
1975 + * handle ep0 interrupt
1977 +static void jz4740_handle_ep0(struct jz4740_udc *dev, uint32_t intr)
1979 + struct jz4740_ep *ep = &dev->ep[0];
1982 + DEBUG("%s:%s[%d]\n", __FILE__, __func__, __LINE__);
1984 + jz_udc_set_index(dev, 0);
1985 + csr = usb_readb(dev, JZ_REG_UDC_CSR0);
1987 + DEBUG_EP0("%s: csr = %x state = \n", __FUNCTION__, csr);//, state_names[dev->ep0state]);
1990 + * if SENT_STALL is set
1991 + * - clear the SENT_STALL bit
1993 + if (csr & USB_CSR0_SENTSTALL) {
1994 + DEBUG_EP0("%s: USB_CSR0_SENTSTALL is set: %x\n", __FUNCTION__, csr);
1995 + usb_clearb(dev, JZ_REG_UDC_CSR0, USB_CSR0_SENDSTALL | USB_CSR0_SENTSTALL);
1996 + nuke(ep, -ECONNABORTED);
1997 + dev->ep0state = WAIT_FOR_SETUP;
2002 + * if a transfer is in progress && INPKTRDY and OUTPKTRDY are clear
2004 + * - if last packet
2005 + * - set IN_PKT_RDY | DATA_END
2009 + if (!(csr & (USB_CSR0_INPKTRDY | USB_CSR0_OUTPKTRDY))) {
2010 + DEBUG_EP0("%s: INPKTRDY and OUTPKTRDY are clear\n",
2013 + switch (dev->ep0state) {
2014 + case DATA_STATE_XMIT:
2015 + DEBUG_EP0("continue with DATA_STATE_XMIT\n");
2016 + jz4740_ep0_in(dev, csr);
2018 + case DATA_STATE_NEED_ZLP:
2019 + DEBUG_EP0("continue with DATA_STATE_NEED_ZLP\n");
2020 + jz4740_ep0_in_zlp(dev, csr);
2024 +// DEBUG_EP0("Odd state!! state = %s\n",
2025 +// state_names[dev->ep0state]);
2026 + dev->ep0state = WAIT_FOR_SETUP;
2027 + /* nuke(ep, 0); */
2028 + /* usb_setb(ep->csr, USB_CSR0_SENDSTALL); */
2035 + * if SETUPEND is set
2036 + * - abort the last transfer
2037 + * - set SERVICED_SETUP_END_BIT
2039 + if (csr & USB_CSR0_SETUPEND) {
2040 + DEBUG_EP0("%s: USB_CSR0_SETUPEND is set: %x\n", __FUNCTION__, csr);
2042 + usb_setb(dev, JZ_REG_UDC_CSR0, USB_CSR0_SVDSETUPEND);
2044 + dev->ep0state = WAIT_FOR_SETUP;
2048 + * if USB_CSR0_OUTPKTRDY is set
2049 + * - read data packet from EP0 FIFO
2050 + * - decode command
2052 + * set SVDOUTPKTRDY | DATAEND | SENDSTALL bits
2054 + * set SVDOUTPKTRDY | DATAEND bits
2056 + if (csr & USB_CSR0_OUTPKTRDY) {
2058 + DEBUG_EP0("%s: EP0_OUT_PKT_RDY is set: %x\n", __FUNCTION__,
2061 + switch (dev->ep0state) {
2062 + case WAIT_FOR_SETUP:
2063 + DEBUG_EP0("WAIT_FOR_SETUP\n");
2064 + jz4740_ep0_setup(dev, csr);
2067 + case DATA_STATE_RECV:
2068 + DEBUG_EP0("DATA_STATE_RECV\n");
2069 + jz4740_ep0_out(dev, csr, 0);
2074 + DEBUG_EP0("strange state!! 2. send stall? state = %d\n",
2081 +static void jz4740_ep0_kick(struct jz4740_udc *dev, struct jz4740_ep *ep)
2085 + jz_udc_set_index(dev, 0);
2087 + DEBUG_EP0("%s: %x\n", __FUNCTION__, csr);
2089 + /* Clear "out packet ready" */
2091 + if (ep_is_in(ep)) {
2092 + usb_setb(dev, JZ_REG_UDC_CSR0, USB_CSR0_SVDOUTPKTRDY);
2093 + csr = usb_readb(dev, JZ_REG_UDC_CSR0);
2094 + dev->ep0state = DATA_STATE_XMIT;
2095 + jz4740_ep0_in(dev, csr);
2097 + csr = usb_readb(dev, JZ_REG_UDC_CSR0);
2098 + dev->ep0state = DATA_STATE_RECV;
2099 + jz4740_ep0_out(dev, csr, 1);
2103 +/** Handle USB RESET interrupt
2105 +static void jz4740_reset_irq(struct jz4740_udc *dev)
2107 + dev->gadget.speed = (usb_readb(dev, JZ_REG_UDC_POWER) & USB_POWER_HSMODE) ?
2108 + USB_SPEED_HIGH : USB_SPEED_FULL;
2110 + DEBUG_SETUP("%s: address = %d, speed = %s\n", __FUNCTION__, dev->usb_address,
2111 + (dev->gadget.speed == USB_SPEED_HIGH) ? "HIGH":"FULL" );
2115 + * jz4740 usb device interrupt handler.
2117 +static irqreturn_t jz4740_udc_irq(int irq, void *_dev)
2119 + struct jz4740_udc *dev = _dev;
2122 + uint32_t intr_usb = usb_readb(dev, JZ_REG_UDC_INTRUSB) & 0x7; /* mask SOF */
2123 + uint32_t intr_in = usb_readw(dev, JZ_REG_UDC_INTRIN);
2124 + uint32_t intr_out = usb_readw(dev, JZ_REG_UDC_INTROUT);
2125 + uint32_t intr_dma = usb_readb(dev, JZ_REG_UDC_INTR);
2127 + if (!intr_usb && !intr_in && !intr_out && !intr_dma)
2128 + return IRQ_HANDLED;
2131 + DEBUG("intr_out=%x intr_in=%x intr_usb=%x\n",
2132 + intr_out, intr_in, intr_usb);
2134 + spin_lock(&dev->lock);
2135 + index = usb_readb(dev, JZ_REG_UDC_INDEX);
2137 + /* Check for resume from suspend mode */
2138 + if ((intr_usb & USB_INTR_RESUME) &&
2139 + (usb_readb(dev, JZ_REG_UDC_INTRUSBE) & USB_INTR_RESUME)) {
2140 + DEBUG("USB resume\n");
2141 + dev->driver->resume(&dev->gadget); /* We have suspend(), so we must have resume() too. */
2144 + /* Check for system interrupts */
2145 + if (intr_usb & USB_INTR_RESET) {
2146 + DEBUG("USB reset\n");
2147 + jz4740_reset_irq(dev);
2150 + /* Check for endpoint 0 interrupt */
2151 + if (intr_in & USB_INTR_EP0) {
2152 + DEBUG("USB_INTR_EP0 (control)\n");
2153 + jz4740_handle_ep0(dev, intr_in);
2156 + /* Check for Bulk-IN DMA interrupt */
2157 + if (intr_dma & 0x1) {
2159 + struct jz4740_ep *ep;
2160 + ep_num = (usb_readl(dev, JZ_REG_UDC_CNTL1) >> 4) & 0xf;
2161 + ep = &dev->ep[ep_num + 1];
2162 + jz_udc_set_index(dev, ep_num);
2163 + usb_setb(dev, ep->csr, USB_INCSR_INPKTRDY);
2164 +/* jz4740_in_epn(dev, ep_num, intr_in);*/
2167 + /* Check for Bulk-OUT DMA interrupt */
2168 + if (intr_dma & 0x2) {
2170 + ep_num = (usb_readl(dev, JZ_REG_UDC_CNTL2) >> 4) & 0xf;
2171 + jz4740_out_epn(dev, ep_num, intr_out);
2174 + /* Check for each configured endpoint interrupt */
2175 + if (intr_in & USB_INTR_INEP1) {
2176 + DEBUG("USB_INTR_INEP1\n");
2177 + jz4740_in_epn(dev, 1, intr_in);
2180 + if (intr_in & USB_INTR_INEP2) {
2181 + DEBUG("USB_INTR_INEP2\n");
2182 + jz4740_in_epn(dev, 2, intr_in);
2185 + if (intr_out & USB_INTR_OUTEP1) {
2186 + DEBUG("USB_INTR_OUTEP1\n");
2187 + jz4740_out_epn(dev, 1, intr_out);
2190 + /* Check for suspend mode */
2191 + if ((intr_usb & USB_INTR_SUSPEND) &&
2192 + (usb_readb(dev, JZ_REG_UDC_INTRUSBE) & USB_INTR_SUSPEND)) {
2193 + DEBUG("USB suspend\n");
2194 + dev->driver->suspend(&dev->gadget);
2195 + /* Host unloaded from us, can do something, such as flushing
2196 + the NAND block cache etc. */
2199 + jz_udc_set_index(dev, index);
2201 + spin_unlock(&dev->lock);
2203 + return IRQ_HANDLED;
2208 +/*-------------------------------------------------------------------------*/
2210 +/* Common functions - Added by River */
2211 +static struct jz4740_udc udc_dev;
2213 +static inline struct jz4740_udc *gadget_to_udc(struct usb_gadget *gadget)
2215 + return container_of(gadget, struct jz4740_udc, gadget);
2219 +static int jz4740_udc_get_frame(struct usb_gadget *_gadget)
2221 + DEBUG("%s, %p\n", __FUNCTION__, _gadget);
2222 + return usb_readw(gadget_to_udc(_gadget), JZ_REG_UDC_FRAME);
2225 +static int jz4740_udc_wakeup(struct usb_gadget *_gadget)
2227 + /* host may not have enabled remote wakeup */
2228 + /*if ((UDCCS0 & UDCCS0_DRWF) == 0)
2229 + return -EHOSTUNREACH;
2230 + udc_set_mask_UDCCR(UDCCR_RSM); */
2234 +static int jz4740_udc_pullup(struct usb_gadget *_gadget, int on)
2236 + struct jz4740_udc *udc = gadget_to_udc(_gadget);
2237 + unsigned long flags;
2239 + local_irq_save(flags);
2242 + udc->state = UDC_STATE_ENABLE;
2245 + udc->state = UDC_STATE_DISABLE;
2249 + local_irq_restore(flags);
2255 +static const struct usb_gadget_ops jz4740_udc_ops = {
2256 + .get_frame = jz4740_udc_get_frame,
2257 + .wakeup = jz4740_udc_wakeup,
2258 + .pullup = jz4740_udc_pullup,
2259 + /* current versions must always be self-powered */
2262 +static struct usb_ep_ops jz4740_ep_ops = {
2263 + .enable = jz4740_ep_enable,
2264 + .disable = jz4740_ep_disable,
2266 + .alloc_request = jz4740_alloc_request,
2267 + .free_request = jz4740_free_request,
2269 + .queue = jz4740_queue,
2270 + .dequeue = jz4740_dequeue,
2272 + .set_halt = jz4740_set_halt,
2273 + .fifo_status = jz4740_fifo_status,
2274 + .fifo_flush = jz4740_fifo_flush,
2278 +/*-------------------------------------------------------------------------*/
2280 +static struct jz4740_udc udc_dev = {
2283 + .ops = &jz4740_udc_ops,
2284 + .ep0 = &udc_dev.ep[0].ep,
2287 + .init_name = "gadget",
2291 + /* control endpoint */
2295 + .ops = &jz4740_ep_ops,
2296 + .maxpacket = EP0_MAXPACKETSIZE,
2300 + .bEndpointAddress = 0,
2301 + .bmAttributes = 0,
2303 + .type = ep_control,
2304 + .fifo = JZ_REG_UDC_EP_FIFO(0),
2305 + .csr = JZ_REG_UDC_CSR0,
2308 + /* bulk out endpoint */
2311 + .name = "ep1out-bulk",
2312 + .ops = &jz4740_ep_ops,
2313 + .maxpacket = EPBULK_MAXPACKETSIZE,
2317 + .bEndpointAddress = 1,
2318 + .bmAttributes = USB_ENDPOINT_XFER_BULK,
2320 + .type = ep_bulk_out,
2321 + .fifo = JZ_REG_UDC_EP_FIFO(1),
2322 + .csr = JZ_REG_UDC_OUTCSR,
2325 + /* bulk in endpoint */
2328 + .name = "ep1in-bulk",
2329 + .ops = &jz4740_ep_ops,
2330 + .maxpacket = EPBULK_MAXPACKETSIZE,
2334 + .bEndpointAddress = 1 | USB_DIR_IN,
2335 + .bmAttributes = USB_ENDPOINT_XFER_BULK,
2337 + .type = ep_bulk_in,
2338 + .fifo = JZ_REG_UDC_EP_FIFO(1),
2339 + .csr = JZ_REG_UDC_INCSR,
2342 + /* interrupt in endpoint */
2345 + .name = "ep2in-int",
2346 + .ops = &jz4740_ep_ops,
2347 + .maxpacket = EPINTR_MAXPACKETSIZE,
2351 + .bEndpointAddress = 2 | USB_DIR_IN,
2352 + .bmAttributes = USB_ENDPOINT_XFER_INT,
2354 + .type = ep_interrupt,
2355 + .fifo = JZ_REG_UDC_EP_FIFO(2),
2356 + .csr = JZ_REG_UDC_INCSR,
2360 +static void gadget_release(struct device *_dev)
2365 +static int __devinit jz4740_udc_probe(struct platform_device *pdev)
2367 + struct jz4740_udc *dev = &udc_dev;
2370 + spin_lock_init(&dev->lock);
2371 + the_controller = dev;
2373 + dev->dev = &pdev->dev;
2374 + dev_set_name(&dev->gadget.dev, "gadget");
2375 + dev->gadget.dev.parent = &pdev->dev;
2376 + dev->gadget.dev.dma_mask = pdev->dev.dma_mask;
2377 + dev->gadget.dev.release = gadget_release;
2379 + ret = device_register(&dev->gadget.dev);
2383 + dev->clk = clk_get(&pdev->dev, "udc");
2384 + if (IS_ERR(dev->clk)) {
2385 + ret = PTR_ERR(dev->clk);
2386 + dev_err(&pdev->dev, "Failed to get udc clock: %d\n", ret);
2387 + goto err_device_unregister;
2390 + platform_set_drvdata(pdev, dev);
2392 + dev->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2396 + dev_err(&pdev->dev, "Failed to get mmio memory resource\n");
2400 + dev->mem = request_mem_region(dev->mem->start, resource_size(dev->mem), pdev->name);
2404 + dev_err(&pdev->dev, "Failed to request mmio memory region\n");
2405 + goto err_device_unregister;
2408 + dev->base = ioremap(dev->mem->start, resource_size(dev->mem));
2412 + dev_err(&pdev->dev, "Failed to ioremap mmio memory\n");
2413 + goto err_release_mem_region;
2416 + dev->irq = platform_get_irq(pdev, 0);
2418 + ret = request_irq(dev->irq, jz4740_udc_irq, IRQF_DISABLED,
2421 + dev_err(&pdev->dev, "Failed to request irq: %d\n", ret);
2431 + iounmap(dev->base);
2432 +err_release_mem_region:
2433 + release_mem_region(dev->mem->start, resource_size(dev->mem));
2435 + clk_put(dev->clk);
2436 +err_device_unregister:
2437 + device_unregister(&dev->gadget.dev);
2438 + platform_set_drvdata(pdev, NULL);
2440 + the_controller = 0;
2445 +static int __devexit jz4740_udc_remove(struct platform_device *pdev)
2447 + struct jz4740_udc *dev = platform_get_drvdata(pdev);
2454 + free_irq(dev->irq, dev);
2455 + iounmap(dev->base);
2456 + release_mem_region(dev->mem->start, resource_size(dev->mem));
2457 + clk_put(dev->clk);
2459 + platform_set_drvdata(pdev, NULL);
2460 + device_unregister(&dev->gadget.dev);
2461 + the_controller = NULL;
2468 +static int jz4740_udc_suspend(struct device *dev)
2470 + struct jz4740_udc *udc = dev_get_drvdata(dev);
2472 + if (udc->state == UDC_STATE_ENABLE)
2478 +static int jz4740_udc_resume(struct device *dev)
2480 + struct jz4740_udc *udc = dev_get_drvdata(dev);
2482 + if (udc->state == UDC_STATE_ENABLE)
2488 +static struct dev_pm_ops jz4740_udc_pm_ops = {
2489 + .suspend = jz4740_udc_suspend,
2490 + .resume = jz4740_udc_resume,
2493 +#define JZ4740_UDC_PM_OPS (&jz4740_udc_pm_ops)
2497 +#define JZ4740_UDC_PM_OPS NULL
2501 +static struct platform_driver udc_driver = {
2502 + .probe = jz4740_udc_probe,
2503 + .remove = __devexit_p(jz4740_udc_remove),
2506 + .owner = THIS_MODULE,
2507 + .pm = JZ4740_UDC_PM_OPS,
2511 +/*-------------------------------------------------------------------------*/
2513 +static int __init udc_init (void)
2515 + return platform_driver_register(&udc_driver);
2517 +module_init(udc_init);
2519 +static void __exit udc_exit (void)
2521 + platform_driver_unregister(&udc_driver);
2523 +module_exit(udc_exit);
2525 +MODULE_DESCRIPTION("JZ4740 USB Device Controller");
2526 +MODULE_AUTHOR("Wei Jianli <jlwei@ingenic.cn>");
2527 +MODULE_LICENSE("GPL");
2528 diff --git a/drivers/usb/gadget/jz4740_udc.h b/drivers/usb/gadget/jz4740_udc.h
2529 new file mode 100644
2530 index 0000000..7156768
2532 +++ b/drivers/usb/gadget/jz4740_udc.h
2535 + * linux/drivers/usb/gadget/jz4740_udc.h
2537 + * Ingenic JZ4740 on-chip high speed USB device controller
2539 + * Copyright (C) 2006 Ingenic Semiconductor Inc.
2540 + * Author: <jlwei@ingenic.cn>
2542 + * This program is free software; you can redistribute it and/or modify
2543 + * it under the terms of the GNU General Public License as published by
2544 + * the Free Software Foundation; either version 2 of the License, or
2545 + * (at your option) any later version.
2548 +#ifndef __USB_GADGET_JZ4740_H__
2549 +#define __USB_GADGET_JZ4740_H__
2551 +/*-------------------------------------------------------------------------*/
2554 +#define EP0_MAXPACKETSIZE 64
2555 +#define EPBULK_MAXPACKETSIZE 512
2556 +#define EPINTR_MAXPACKETSIZE 64
2558 +#define UDC_MAX_ENDPOINTS 4
2560 +/*-------------------------------------------------------------------------*/
2562 +typedef enum ep_type {
2563 + ep_control, ep_bulk_in, ep_bulk_out, ep_interrupt
2568 + struct jz4740_udc *dev;
2570 + const struct usb_endpoint_descriptor *desc;
2571 + unsigned long pio_irqs;
2574 + uint8_t bEndpointAddress;
2575 + uint8_t bmAttributes;
2581 + uint32_t reg_addr;
2582 + struct list_head queue;
2585 +struct jz4740_request {
2586 + struct usb_request req;
2587 + struct list_head queue;
2591 + WAIT_FOR_SETUP, /* between STATUS ack and SETUP report */
2592 + DATA_STATE_XMIT, /* data tx stage */
2593 + DATA_STATE_NEED_ZLP, /* data tx zlp stage */
2594 + WAIT_FOR_OUT_STATUS, /* status stages */
2595 + DATA_STATE_RECV, /* data rx stage */
2598 +/* For function binding with UDC Disable - Added by River */
2600 + UDC_STATE_ENABLE = 0,
2601 + UDC_STATE_DISABLE,
2604 +struct jz4740_udc {
2605 + struct usb_gadget gadget;
2606 + struct usb_gadget_driver *driver;
2607 + struct device *dev;
2609 + unsigned long lock_flags;
2611 + enum ep0state ep0state;
2612 + struct jz4740_ep ep[UDC_MAX_ENDPOINTS];
2614 + unsigned char usb_address;
2616 + udc_state_t state;
2618 + struct resource *mem;
2619 + void __iomem *base;
2622 + uint32_t out_mask;
2627 +extern struct jz4740_udc *the_controller;
2629 +#define ep_is_in(EP) (((EP)->bEndpointAddress&USB_DIR_IN)==USB_DIR_IN)
2630 +#define ep_maxpacket(EP) ((EP)->ep.maxpacket)
2631 +#define ep_index(EP) ((EP)->bEndpointAddress&0xF)
2633 +#endif /* __USB_GADGET_JZ4740_H__ */