1 From f39709ba061049463b1a607369e476512da9ec5b 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 02/23] 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 | 2378 +++++++++++++++++++++++++++++++++++++
11 drivers/usb/gadget/jz4740_udc.h | 101 ++
12 5 files changed, 2503 insertions(+), 0 deletions(-)
13 create mode 100644 drivers/usb/gadget/jz4740_udc.c
14 create mode 100644 drivers/usb/gadget/jz4740_udc.h
16 --- a/drivers/usb/gadget/Kconfig
17 +++ b/drivers/usb/gadget/Kconfig
18 @@ -121,11 +121,25 @@ choice
20 # Integrated controllers
22 +config USB_GADGET_JZ4740
23 + boolean "JZ4740 UDC"
24 + depends on MACH_JZ4740
25 + select USB_GADGET_SELECTED
26 + select USB_GADGET_DUALSPEED
28 + Select this to support the Ingenic JZ4740 processor
29 + high speed USB device controller.
33 + depends on USB_GADGET_JZ4740
36 config USB_GADGET_AT91
37 boolean "Atmel AT91 USB Device Port"
38 depends on ARCH_AT91 && !ARCH_AT91SAM9RL && !ARCH_AT91CAP9 && !ARCH_AT91SAM9G45
39 select USB_GADGET_SELECTED
42 Many Atmel AT91 processors (such as the AT91RM2000) have a
43 full speed USB Device Port with support for five configurable
44 --- a/drivers/usb/gadget/Makefile
45 +++ b/drivers/usb/gadget/Makefile
46 @@ -24,6 +24,7 @@ obj-$(CONFIG_USB_FSL_QE) += fsl_qe_udc.o
47 obj-$(CONFIG_USB_CI13XXX) += ci13xxx_udc.o
48 obj-$(CONFIG_USB_S3C_HSOTG) += s3c-hsotg.o
49 obj-$(CONFIG_USB_LANGWELL) += langwell_udc.o
50 +obj-$(CONFIG_USB_JZ4740) += jz4740_udc.o
54 --- a/drivers/usb/gadget/gadget_chips.h
55 +++ b/drivers/usb/gadget/gadget_chips.h
57 #ifndef __GADGET_CHIPS_H
58 #define __GADGET_CHIPS_H
60 +#ifdef CONFIG_USB_GADGET_JZ4740
61 +#define gadget_is_jz4740(g) !strcmp("ingenic_hsusb", (g)->name)
63 +#define gadget_is_jz4740(g) 0
66 #ifdef CONFIG_USB_GADGET_NET2280
67 #define gadget_is_net2280(g) !strcmp("net2280", (g)->name)
69 @@ -200,6 +206,9 @@ static inline int usb_gadget_controller_
71 else if (gadget_is_s3c_hsotg(gadget))
73 + else if (gadget_is_jz4740(gadget))
80 +++ b/drivers/usb/gadget/jz4740_udc.c
83 + * linux/drivers/usb/gadget/jz4740_udc.c
85 + * Ingenic JZ4740 on-chip high speed USB device controller
87 + * Copyright (C) 2006 - 2008 Ingenic Semiconductor Inc.
88 + * Author: <jlwei@ingenic.cn>
90 + * This program is free software; you can redistribute it and/or modify
91 + * it under the terms of the GNU General Public License as published by
92 + * the Free Software Foundation; either version 2 of the License, or
93 + * (at your option) any later version.
97 + * This device has ep0, two bulk-in/interrupt-in endpoints, and one bulk-out endpoint.
99 + * - Endpoint numbering is fixed: ep0, ep1in-int, ep2in-bulk, ep1out-bulk.
100 + * - DMA works with bulk-in (channel 1) and bulk-out (channel 2) endpoints.
103 +#include <linux/kernel.h>
104 +#include <linux/module.h>
105 +#include <linux/platform_device.h>
106 +#include <linux/delay.h>
107 +#include <linux/ioport.h>
108 +#include <linux/slab.h>
109 +#include <linux/errno.h>
110 +#include <linux/init.h>
111 +#include <linux/list.h>
112 +#include <linux/interrupt.h>
113 +#include <linux/proc_fs.h>
114 +#include <linux/usb.h>
115 +#include <linux/usb/gadget.h>
116 +#include <linux/clk.h>
118 +#include <asm/byteorder.h>
120 +#include <asm/irq.h>
121 +#include <asm/system.h>
122 +#include <asm/mach-jz4740/clock.h>
124 +#include "jz4740_udc.h"
126 +#define JZ_REG_UDC_FADDR 0x00 /* Function Address 8-bit */
127 +#define JZ_REG_UDC_POWER 0x01 /* Power Management 8-bit */
128 +#define JZ_REG_UDC_INTRIN 0x02 /* Interrupt IN 16-bit */
129 +#define JZ_REG_UDC_INTROUT 0x04 /* Interrupt OUT 16-bit */
130 +#define JZ_REG_UDC_INTRINE 0x06 /* Intr IN enable 16-bit */
131 +#define JZ_REG_UDC_INTROUTE 0x08 /* Intr OUT enable 16-bit */
132 +#define JZ_REG_UDC_INTRUSB 0x0a /* Interrupt USB 8-bit */
133 +#define JZ_REG_UDC_INTRUSBE 0x0b /* Interrupt USB Enable 8-bit */
134 +#define JZ_REG_UDC_FRAME 0x0c /* Frame number 16-bit */
135 +#define JZ_REG_UDC_INDEX 0x0e /* Index register 8-bit */
136 +#define JZ_REG_UDC_TESTMODE 0x0f /* USB test mode 8-bit */
138 +#define JZ_REG_UDC_CSR0 0x12 /* EP0 CSR 8-bit */
139 +#define JZ_REG_UDC_INMAXP 0x10 /* EP1-2 IN Max Pkt Size 16-bit */
140 +#define JZ_REG_UDC_INCSR 0x12 /* EP1-2 IN CSR LSB 8/16bit */
141 +#define JZ_REG_UDC_INCSRH 0x13 /* EP1-2 IN CSR MSB 8-bit */
142 +#define JZ_REG_UDC_OUTMAXP 0x14 /* EP1 OUT Max Pkt Size 16-bit */
143 +#define JZ_REG_UDC_OUTCSR 0x16 /* EP1 OUT CSR LSB 8/16bit */
144 +#define JZ_REG_UDC_OUTCSRH 0x17 /* EP1 OUT CSR MSB 8-bit */
145 +#define JZ_REG_UDC_OUTCOUNT 0x18 /* bytes in EP0/1 OUT FIFO 16-bit */
147 +#define JZ_REG_UDC_EP_FIFO(x) (4 * (x) + 0x20)
149 +#define JZ_REG_UDC_EPINFO 0x78 /* Endpoint information */
150 +#define JZ_REG_UDC_RAMINFO 0x79 /* RAM information */
152 +#define JZ_REG_UDC_INTR 0x200 /* DMA pending interrupts */
153 +#define JZ_REG_UDC_CNTL1 0x204 /* DMA channel 1 control */
154 +#define JZ_REG_UDC_ADDR1 0x208 /* DMA channel 1 AHB memory addr */
155 +#define JZ_REG_UDC_COUNT1 0x20c /* DMA channel 1 byte count */
156 +#define JZ_REG_UDC_CNTL2 0x214 /* DMA channel 2 control */
157 +#define JZ_REG_UDC_ADDR2 0x218 /* DMA channel 2 AHB memory addr */
158 +#define JZ_REG_UDC_COUNT2 0x21c /* DMA channel 2 byte count */
160 +/* Power register bit masks */
161 +#define USB_POWER_SUSPENDM 0x01
162 +#define USB_POWER_RESUME 0x04
163 +#define USB_POWER_HSMODE 0x10
164 +#define USB_POWER_HSENAB 0x20
165 +#define USB_POWER_SOFTCONN 0x40
167 +/* Interrupt register bit masks */
168 +#define USB_INTR_SUSPEND 0x01
169 +#define USB_INTR_RESUME 0x02
170 +#define USB_INTR_RESET 0x04
172 +#define USB_INTR_EP0 0x0001
173 +#define USB_INTR_INEP1 0x0002
174 +#define USB_INTR_INEP2 0x0004
175 +#define USB_INTR_OUTEP1 0x0002
177 +/* CSR0 bit masks */
178 +#define USB_CSR0_OUTPKTRDY 0x01
179 +#define USB_CSR0_INPKTRDY 0x02
180 +#define USB_CSR0_SENTSTALL 0x04
181 +#define USB_CSR0_DATAEND 0x08
182 +#define USB_CSR0_SETUPEND 0x10
183 +#define USB_CSR0_SENDSTALL 0x20
184 +#define USB_CSR0_SVDOUTPKTRDY 0x40
185 +#define USB_CSR0_SVDSETUPEND 0x80
187 +/* Endpoint CSR register bits */
188 +#define USB_INCSRH_AUTOSET 0x80
189 +#define USB_INCSRH_ISO 0x40
190 +#define USB_INCSRH_MODE 0x20
191 +#define USB_INCSRH_DMAREQENAB 0x10
192 +#define USB_INCSRH_DMAREQMODE 0x04
193 +#define USB_INCSR_CDT 0x40
194 +#define USB_INCSR_SENTSTALL 0x20
195 +#define USB_INCSR_SENDSTALL 0x10
196 +#define USB_INCSR_FF 0x08
197 +#define USB_INCSR_UNDERRUN 0x04
198 +#define USB_INCSR_FFNOTEMPT 0x02
199 +#define USB_INCSR_INPKTRDY 0x01
200 +#define USB_OUTCSRH_AUTOCLR 0x80
201 +#define USB_OUTCSRH_ISO 0x40
202 +#define USB_OUTCSRH_DMAREQENAB 0x20
203 +#define USB_OUTCSRH_DNYT 0x10
204 +#define USB_OUTCSRH_DMAREQMODE 0x08
205 +#define USB_OUTCSR_CDT 0x80
206 +#define USB_OUTCSR_SENTSTALL 0x40
207 +#define USB_OUTCSR_SENDSTALL 0x20
208 +#define USB_OUTCSR_FF 0x10
209 +#define USB_OUTCSR_DATAERR 0x08
210 +#define USB_OUTCSR_OVERRUN 0x04
211 +#define USB_OUTCSR_FFFULL 0x02
212 +#define USB_OUTCSR_OUTPKTRDY 0x01
214 +/* DMA control bits */
215 +#define USB_CNTL_ENA 0x01
216 +#define USB_CNTL_DIR_IN 0x02
217 +#define USB_CNTL_MODE_1 0x04
218 +#define USB_CNTL_INTR_EN 0x08
219 +#define USB_CNTL_EP(n) ((n) << 4)
220 +#define USB_CNTL_BURST_0 (0 << 9)
221 +#define USB_CNTL_BURST_4 (1 << 9)
222 +#define USB_CNTL_BURST_8 (2 << 9)
223 +#define USB_CNTL_BURST_16 (3 << 9)
227 +# define DEBUG(fmt,args...) do {} while(0)
231 +# define DEBUG_EP0(fmt,args...) do {} while(0)
234 +# define DEBUG_SETUP(fmt,args...) do {} while(0)
237 +static unsigned int use_dma = 0; /* 1: use DMA, 0: use PIO */
239 +module_param(use_dma, int, 0);
240 +MODULE_PARM_DESC(use_dma, "DMA mode enable flag");
242 +static struct jz4740_udc jz4740_udc_controller;
245 + * Local declarations.
247 +static void jz4740_ep0_kick(struct jz4740_udc *dev, struct jz4740_ep *ep);
248 +static void jz4740_handle_ep0(struct jz4740_udc *dev, uint32_t intr);
250 +static void done(struct jz4740_ep *ep, struct jz4740_request *req,
252 +static void pio_irq_enable(struct jz4740_ep *ep);
253 +static void pio_irq_disable(struct jz4740_ep *ep);
254 +static void stop_activity(struct jz4740_udc *dev,
255 + struct usb_gadget_driver *driver);
256 +static void nuke(struct jz4740_ep *ep, int status);
257 +static void flush(struct jz4740_ep *ep);
258 +static void udc_set_address(struct jz4740_udc *dev, unsigned char address);
260 +/*-------------------------------------------------------------------------*/
262 +/* inline functions of register read/write/set/clear */
264 +static inline uint8_t usb_readb(struct jz4740_udc *udc, size_t reg)
266 + return readb(udc->base + reg);
269 +static inline uint16_t usb_readw(struct jz4740_udc *udc, size_t reg)
271 + return readw(udc->base + reg);
274 +static inline uint32_t usb_readl(struct jz4740_udc *udc, size_t reg)
276 + return readl(udc->base + reg);
279 +static inline void usb_writeb(struct jz4740_udc *udc, size_t reg, uint8_t val)
281 + writeb(val, udc->base + reg);
284 +static inline void usb_writew(struct jz4740_udc *udc, size_t reg, uint16_t val)
286 + writew(val, udc->base + reg);
289 +static inline void usb_writel(struct jz4740_udc *udc, size_t reg, uint32_t val)
291 + writel(val, udc->base + reg);
294 +static inline void usb_setb(struct jz4740_udc *udc, size_t reg, uint8_t mask)
296 + usb_writeb(udc, reg, usb_readb(udc, reg) | mask);
299 +static inline void usb_setw(struct jz4740_udc *udc, size_t reg, uint8_t mask)
301 + usb_writew(udc, reg, usb_readw(udc, reg) | mask);
304 +static inline void usb_clearb(struct jz4740_udc *udc, size_t reg, uint8_t mask)
306 + usb_writeb(udc, reg, usb_readb(udc, reg) & ~mask);
309 +static inline void usb_clearw(struct jz4740_udc *udc, size_t reg, uint16_t mask)
311 + usb_writew(udc, reg, usb_readw(udc, reg) & ~mask);
314 +/*-------------------------------------------------------------------------*/
316 +static inline void jz_udc_set_index(struct jz4740_udc *udc, uint8_t index)
318 + usb_writeb(udc, JZ_REG_UDC_INDEX, index);
321 +static inline void jz_udc_select_ep(struct jz4740_ep *ep)
323 + jz_udc_set_index(ep->dev, ep_index(ep));
326 +static inline int write_packet(struct jz4740_ep *ep,
327 + struct jz4740_request *req, unsigned int count)
330 + unsigned int length, nlong, nbyte;
331 + void __iomem *fifo = ep->dev->base + ep->fifo;
333 + DEBUG("%s:%s[%d]\n", __FILE__, __func__, __LINE__);
335 + buf = req->req.buf + req->req.actual;
338 + length = req->req.length - req->req.actual;
339 + if (length > count)
341 + req->req.actual += length;
343 + DEBUG("Write %d (count %d), fifo %x\n", length, count, ep->fifo);
345 + nlong = length >> 2;
346 + nbyte = length & 0x3;
348 + writel(*((uint32_t *)buf), fifo);
352 + writeb(*buf++, fifo);
357 +static int read_packet(struct jz4740_ep *ep,
358 + struct jz4740_request *req, unsigned int count)
361 + unsigned int length, nlong, nbyte;
362 + void __iomem *fifo = ep->dev->base + ep->fifo;
363 + DEBUG("%s:%s[%d]\n", __FILE__, __func__, __LINE__);
365 + buf = req->req.buf + req->req.actual;
368 + length = req->req.length - req->req.actual;
369 + if (length > count)
371 + req->req.actual += length;
373 + DEBUG("Read %d, fifo %x\n", length, ep->fifo);
374 + nlong = length >> 2;
375 + nbyte = length & 0x3;
377 + *((uint32_t *)buf) = readl(fifo);
381 + *buf++ = readb(fifo);
386 +/*-------------------------------------------------------------------------*/
389 + * udc_disable - disable USB device controller
391 +static void udc_disable(struct jz4740_udc *dev)
393 + DEBUG("%s:%s[%d]\n", __FILE__, __func__, __LINE__);
395 + udc_set_address(dev, 0);
397 + /* Disable interrupts */
398 + usb_writew(dev, JZ_REG_UDC_INTRINE, 0);
399 + usb_writew(dev, JZ_REG_UDC_INTROUTE, 0);
400 + usb_writeb(dev, JZ_REG_UDC_INTRUSBE, 0);
403 + usb_writel(dev, JZ_REG_UDC_CNTL1, 0);
404 + usb_writel(dev, JZ_REG_UDC_CNTL2, 0);
406 + /* Disconnect from usb */
407 + usb_clearb(dev, JZ_REG_UDC_POWER, USB_POWER_SOFTCONN);
409 + /* Disable the USB PHY */
410 + clk_disable(dev->clk);
412 + dev->ep0state = WAIT_FOR_SETUP;
413 + dev->gadget.speed = USB_SPEED_UNKNOWN;
419 + * udc_reinit - initialize software state
421 +static void udc_reinit(struct jz4740_udc *dev)
424 + DEBUG("%s:%s[%d]\n", __FILE__, __func__, __LINE__);
426 + /* device/ep0 records init */
427 + INIT_LIST_HEAD(&dev->gadget.ep_list);
428 + INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
429 + dev->ep0state = WAIT_FOR_SETUP;
431 + for (i = 0; i < UDC_MAX_ENDPOINTS; i++) {
432 + struct jz4740_ep *ep = &dev->ep[i];
435 + list_add_tail(&ep->ep.ep_list, &dev->gadget.ep_list);
437 + INIT_LIST_HEAD(&ep->queue);
443 +/* until it's enabled, this UDC should be completely invisible
446 +static void udc_enable(struct jz4740_udc *dev)
449 + DEBUG("%s:%s[%d]\n", __FILE__, __func__, __LINE__);
451 + /* UDC state is incorrect - Added by River */
452 + if (dev->state != UDC_STATE_ENABLE) {
456 + dev->gadget.speed = USB_SPEED_UNKNOWN;
458 + /* Flush FIFO for each */
459 + for (i = 0; i < UDC_MAX_ENDPOINTS; i++) {
460 + struct jz4740_ep *ep = &dev->ep[i];
462 + jz_udc_set_index(dev, ep_index(ep));
466 + /* Set this bit to allow the UDC entering low-power mode when
467 + * there are no actions on the USB bus.
468 + * UDC still works during this bit was set.
470 + jz4740_clock_udc_enable_auto_suspend();
472 + /* Enable the USB PHY */
473 + clk_enable(dev->clk);
475 + /* Disable interrupts */
476 +/* usb_writew(dev, JZ_REG_UDC_INTRINE, 0);
477 + usb_writew(dev, JZ_REG_UDC_INTROUTE, 0);
478 + usb_writeb(dev, JZ_REG_UDC_INTRUSBE, 0);*/
480 + /* Enable interrupts */
481 + usb_setw(dev, JZ_REG_UDC_INTRINE, USB_INTR_EP0);
482 + usb_setb(dev, JZ_REG_UDC_INTRUSBE, USB_INTR_RESET);
483 + /* Don't enable rest of the interrupts */
484 + /* usb_setw(dev, JZ_REG_UDC_INTRINE, USB_INTR_INEP1 | USB_INTR_INEP2);
485 + usb_setw(dev, JZ_REG_UDC_INTROUTE, USB_INTR_OUTEP1); */
487 + /* Enable SUSPEND */
488 + /* usb_setb(dev, JZ_REG_UDC_POWER, USB_POWER_SUSPENDM); */
490 + /* Enable HS Mode */
491 + usb_setb(dev, JZ_REG_UDC_POWER, USB_POWER_HSENAB);
493 + /* Let host detect UDC:
494 + * Software must write a 1 to the PMR:USB_POWER_SOFTCONN bit to turn this
495 + * transistor on and pull the USBDP pin HIGH.
497 + usb_setb(dev, JZ_REG_UDC_POWER, USB_POWER_SOFTCONN);
502 +/*-------------------------------------------------------------------------*/
504 +/* keeping it simple:
505 + * - one bus driver, initted first;
506 + * - one function driver, initted second
510 + * Register entry point for the peripheral controller driver.
513 +int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
514 + int (*bind)(struct usb_gadget *))
516 + struct jz4740_udc *dev = &jz4740_udc_controller;
519 + if (!driver || !bind)
528 + /* hook up the driver */
529 + dev->driver = driver;
530 + dev->gadget.dev.driver = &driver->driver;
532 + retval = bind(&dev->gadget);
534 + DEBUG("%s: bind to driver %s --> error %d\n", dev->gadget.name,
535 + driver->driver.name, retval);
540 + /* then enable host detection and ep0; and we're ready
541 + * for set_configuration as well as eventual disconnect.
545 + DEBUG("%s: registered gadget driver '%s'\n", dev->gadget.name,
546 + driver->driver.name);
550 +EXPORT_SYMBOL(usb_gadget_probe_driver);
552 +static void stop_activity(struct jz4740_udc *dev,
553 + struct usb_gadget_driver *driver)
557 + DEBUG("%s:%s[%d]\n", __FILE__, __func__, __LINE__);
559 + /* don't disconnect drivers more than once */
560 + if (dev->gadget.speed == USB_SPEED_UNKNOWN)
562 + dev->gadget.speed = USB_SPEED_UNKNOWN;
564 + /* prevent new request submissions, kill any outstanding requests */
565 + for (i = 0; i < UDC_MAX_ENDPOINTS; i++) {
566 + struct jz4740_ep *ep = &dev->ep[i];
570 + jz_udc_set_index(dev, ep_index(ep));
571 + nuke(ep, -ESHUTDOWN);
574 + /* report disconnect; the driver is already quiesced */
576 + spin_unlock(&dev->lock);
577 + driver->disconnect(&dev->gadget);
578 + spin_lock(&dev->lock);
581 + /* re-init driver-visible data structures */
587 + * Unregister entry point for the peripheral controller driver.
589 +int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
591 + struct jz4740_udc *dev = &jz4740_udc_controller;
592 + unsigned long flags;
593 + DEBUG("%s:%s[%d]\n", __FILE__, __func__, __LINE__);
597 + if (!driver || driver != dev->driver)
599 + if (!driver->unbind)
602 + spin_lock_irqsave(&dev->lock, flags);
604 + stop_activity(dev, driver);
605 + spin_unlock_irqrestore(&dev->lock, flags);
607 + driver->unbind(&dev->gadget);
611 + DEBUG("unregistered driver '%s'\n", driver->driver.name);
616 +EXPORT_SYMBOL(usb_gadget_unregister_driver);
618 +/*-------------------------------------------------------------------------*/
621 + * Starting DMA using mode 1
623 +static void kick_dma(struct jz4740_ep *ep, struct jz4740_request *req)
625 + struct jz4740_udc *dev = ep->dev;
626 + uint32_t count = req->req.length;
627 + uint32_t physaddr = virt_to_phys((void *)req->req.buf);
629 + DEBUG("%s:%s[%d]\n", __FILE__, __func__, __LINE__);
631 + jz_udc_select_ep(ep);
633 + if (ep_is_in(ep)) { /* Bulk-IN transfer using DMA channel 1 */
634 + ep->reg_addr = JZ_REG_UDC_ADDR1;
636 + dma_cache_wback_inv((unsigned long)req->req.buf, count);
638 + pio_irq_enable(ep);
640 + usb_writeb(dev, JZ_REG_UDC_INCSRH,
641 + USB_INCSRH_DMAREQENAB | USB_INCSRH_AUTOSET | USB_INCSRH_DMAREQMODE);
643 + usb_writel(dev, JZ_REG_UDC_ADDR1, physaddr);
644 + usb_writel(dev, JZ_REG_UDC_COUNT1, count);
645 + usb_writel(dev, JZ_REG_UDC_CNTL1, USB_CNTL_ENA | USB_CNTL_DIR_IN | USB_CNTL_MODE_1 |
646 + USB_CNTL_INTR_EN | USB_CNTL_BURST_16 | USB_CNTL_EP(ep_index(ep)));
648 + else { /* Bulk-OUT transfer using DMA channel 2 */
649 + ep->reg_addr = JZ_REG_UDC_ADDR2;
651 + dma_cache_wback_inv((unsigned long)req->req.buf, count);
653 + pio_irq_enable(ep);
655 + usb_setb(dev, JZ_REG_UDC_OUTCSRH,
656 + USB_OUTCSRH_DMAREQENAB | USB_OUTCSRH_AUTOCLR | USB_OUTCSRH_DMAREQMODE);
658 + usb_writel(dev, JZ_REG_UDC_ADDR2, physaddr);
659 + usb_writel(dev, JZ_REG_UDC_COUNT2, count);
660 + usb_writel(dev, JZ_REG_UDC_CNTL2, USB_CNTL_ENA | USB_CNTL_MODE_1 |
661 + USB_CNTL_INTR_EN | USB_CNTL_BURST_16 | USB_CNTL_EP(ep_index(ep)));
665 +/*-------------------------------------------------------------------------*/
667 +/** Write request to FIFO (max write == maxp size)
668 + * Return: 0 = still running, 1 = completed, negative = errno
669 + * NOTE: INDEX register must be set for EP
671 +static int write_fifo(struct jz4740_ep *ep, struct jz4740_request *req)
673 + struct jz4740_udc *dev = ep->dev;
675 + uint32_t physaddr = virt_to_phys((void *)req->req.buf);
677 + DEBUG("%s:%s[%d]\n", __FILE__, __func__, __LINE__);
678 + max = le16_to_cpu(ep->desc->wMaxPacketSize);
681 + uint32_t dma_count;
683 + /* DMA interrupt generated due to the last packet loaded into the FIFO */
685 + dma_count = usb_readl(dev, ep->reg_addr) - physaddr;
686 + req->req.actual += dma_count;
688 + if (dma_count % max) {
689 + /* If the last packet is less than MAXP, set INPKTRDY manually */
690 + usb_setb(dev, ep->csr, USB_INCSR_INPKTRDY);
694 + if (list_empty(&ep->queue)) {
695 + pio_irq_disable(ep);
699 + /* advance the request queue */
700 + req = list_entry(ep->queue.next, struct jz4740_request, queue);
707 + * PIO mode handling starts here ...
710 + csr = usb_readb(dev, ep->csr);
712 + if (!(csr & USB_INCSR_FFNOTEMPT)) {
714 + int is_last, is_short;
716 + count = write_packet(ep, req, max);
717 + usb_setb(dev, ep->csr, USB_INCSR_INPKTRDY);
719 + /* last packet is usually short (or a zlp) */
720 + if (unlikely(count != max))
721 + is_last = is_short = 1;
723 + if (likely(req->req.length != req->req.actual)
728 + /* interrupt/iso maxpacket may not fill the fifo */
729 + is_short = unlikely(max < ep_maxpacket(ep));
732 + DEBUG("%s: wrote %s %d bytes%s%s %d left %p\n", __FUNCTION__,
733 + ep->ep.name, count,
734 + is_last ? "/L" : "", is_short ? "/S" : "",
735 + req->req.length - req->req.actual, req);
737 + /* requests complete when all IN data is in the FIFO */
740 + if (list_empty(&ep->queue)) {
741 + pio_irq_disable(ep);
746 + DEBUG("Hmm.. %d ep FIFO is not empty!\n", ep_index(ep));
752 +/** Read to request from FIFO (max read == bytes in fifo)
753 + * Return: 0 = still running, 1 = completed, negative = errno
754 + * NOTE: INDEX register must be set for EP
756 +static int read_fifo(struct jz4740_ep *ep, struct jz4740_request *req)
758 + struct jz4740_udc *dev = ep->dev;
760 + unsigned count, is_short;
763 + uint32_t physaddr = virt_to_phys((void *)req->req.buf);
766 + uint32_t dma_count;
768 + /* DMA interrupt generated due to a packet less than MAXP loaded into the FIFO */
770 + dma_count = usb_readl(dev, ep->reg_addr) - physaddr;
771 + req->req.actual += dma_count;
773 + /* Disable interrupt and DMA */
774 + pio_irq_disable(ep);
775 + usb_writel(dev, JZ_REG_UDC_CNTL2, 0);
777 + /* Read all bytes from this packet */
778 + count = usb_readw(dev, JZ_REG_UDC_OUTCOUNT);
779 + count = read_packet(ep, req, count);
782 + /* If the last packet is greater than zero, clear OUTPKTRDY manually */
783 + usb_clearb(dev, ep->csr, USB_OUTCSR_OUTPKTRDY);
787 + if (!list_empty(&ep->queue)) {
788 + /* advance the request queue */
789 + req = list_entry(ep->queue.next, struct jz4740_request, queue);
797 + * PIO mode handling starts here ...
800 + /* make sure there's a packet in the FIFO. */
801 + csr = usb_readb(dev, ep->csr);
802 + if (!(csr & USB_OUTCSR_OUTPKTRDY)) {
803 + DEBUG("%s: Packet NOT ready!\n", __FUNCTION__);
807 + /* read all bytes from this packet */
808 + count = usb_readw(dev, JZ_REG_UDC_OUTCOUNT);
810 + is_short = (count < ep->ep.maxpacket);
812 + count = read_packet(ep, req, count);
814 + DEBUG("read %s %02x, %d bytes%s req %p %d/%d\n",
815 + ep->ep.name, csr, count,
816 + is_short ? "/S" : "", req, req->req.actual, req->req.length);
818 + /* Clear OutPktRdy */
819 + usb_clearb(dev, ep->csr, USB_OUTCSR_OUTPKTRDY);
822 + if (is_short || req->req.actual == req->req.length) {
825 + if (list_empty(&ep->queue))
826 + pio_irq_disable(ep);
830 + /* finished that packet. the next one may be waiting... */
835 + * done - retire a request; caller blocked irqs
836 + * INDEX register is preserved to keep same
838 +static void done(struct jz4740_ep *ep, struct jz4740_request *req, int status)
840 + unsigned int stopped = ep->stopped;
843 + DEBUG("%s, %p\n", __FUNCTION__, ep);
844 + list_del_init(&req->queue);
846 + if (likely(req->req.status == -EINPROGRESS))
847 + req->req.status = status;
849 + status = req->req.status;
851 + if (status && status != -ESHUTDOWN)
852 + DEBUG("complete %s req %p stat %d len %u/%u\n",
853 + ep->ep.name, &req->req, status,
854 + req->req.actual, req->req.length);
856 + /* don't modify queue heads during completion callback */
858 + /* Read current index (completion may modify it) */
859 + index = usb_readb(ep->dev, JZ_REG_UDC_INDEX);
860 + spin_unlock_irqrestore(&ep->dev->lock, ep->dev->lock_flags);
862 + req->req.complete(&ep->ep, &req->req);
864 + spin_lock_irqsave(&ep->dev->lock, ep->dev->lock_flags);
865 + /* Restore index */
866 + jz_udc_set_index(ep->dev, index);
867 + ep->stopped = stopped;
870 +/** Enable EP interrupt */
871 +static void pio_irq_enable(struct jz4740_ep *ep)
873 + uint8_t index = ep_index(ep);
874 + struct jz4740_udc *dev = ep->dev;
875 + DEBUG("%s: EP%d %s\n", __FUNCTION__, ep_index(ep), ep_is_in(ep) ? "IN": "OUT");
877 + if (ep_is_in(ep)) {
881 + usb_setw(dev, JZ_REG_UDC_INTRINE, BIT(index));
884 + DEBUG("Unknown endpoint: %d\n", index);
891 + usb_setw(dev, JZ_REG_UDC_INTROUTE, BIT(index));
894 + DEBUG("Unknown endpoint: %d\n", index);
900 +/** Disable EP interrupt */
901 +static void pio_irq_disable(struct jz4740_ep *ep)
903 + uint8_t index = ep_index(ep);
905 + DEBUG("%s: EP%d %s\n", __FUNCTION__, ep_index(ep), ep_is_in(ep) ? "IN": "OUT");
907 + if (ep_is_in(ep)) {
908 + switch (ep_index(ep)) {
911 + usb_clearw(ep->dev, JZ_REG_UDC_INTRINE, BIT(index));
914 + DEBUG("Unknown endpoint: %d\n", index);
919 + switch (ep_index(ep)) {
921 + usb_clearw(ep->dev, JZ_REG_UDC_INTROUTE, BIT(index));
924 + DEBUG("Unknown endpoint: %d\n", index);
931 + * nuke - dequeue ALL requests
933 +static void nuke(struct jz4740_ep *ep, int status)
935 + struct jz4740_request *req;
937 + DEBUG("%s, %p\n", __FUNCTION__, ep);
942 + /* called with irqs blocked */
943 + while (!list_empty(&ep->queue)) {
944 + req = list_entry(ep->queue.next, struct jz4740_request, queue);
945 + done(ep, req, status);
948 + /* Disable IRQ if EP is enabled (has descriptor) */
950 + pio_irq_disable(ep);
954 + * NOTE: INDEX register must be set before this call
956 +static void flush(struct jz4740_ep *ep)
958 + DEBUG("%s: %s\n", __FUNCTION__, ep->ep.name);
960 + switch (ep->type) {
963 + usb_setb(ep->dev, ep->csr, USB_INCSR_FF);
966 + usb_setb(ep->dev, ep->csr, USB_OUTCSR_FF);
974 + * jz4740_in_epn - handle IN interrupt
976 +static void jz4740_in_epn(struct jz4740_udc *dev, uint32_t ep_idx, uint32_t intr)
979 + struct jz4740_ep *ep = &dev->ep[ep_idx + 1];
980 + struct jz4740_request *req;
981 + DEBUG("%s:%s[%d]\n", __FILE__, __func__, __LINE__);
983 + jz_udc_set_index(dev, ep_index(ep));
985 + csr = usb_readb(dev, ep->csr);
986 + DEBUG("%s: %d, csr %x\n", __FUNCTION__, ep_idx, csr);
988 + if (csr & USB_INCSR_SENTSTALL) {
989 + DEBUG("USB_INCSR_SENTSTALL\n");
990 + usb_clearb(dev, ep->csr, USB_INCSR_SENTSTALL);
995 + DEBUG("%s: NO EP DESC\n", __FUNCTION__);
999 + if (!list_empty(&ep->queue)) {
1000 + req = list_first_entry(&ep->queue, struct jz4740_request, queue);
1001 + write_fifo(ep, req);
1008 +static void jz4740_out_epn(struct jz4740_udc *dev, uint32_t ep_idx, uint32_t intr)
1010 + struct jz4740_ep *ep = &dev->ep[ep_idx];
1011 + struct jz4740_request *req;
1013 + DEBUG("%s: %d\n", __FUNCTION__, ep_idx);
1015 + jz_udc_select_ep(ep);
1020 + /* DMA starts here ... */
1021 + if (!list_empty(&ep->queue)) {
1022 + req = list_first_entry(&ep->queue, struct jz4740_request, queue);
1023 + read_fifo(ep, req);
1029 + * PIO mode starts here ...
1032 + while ((csr = usb_readb(dev, ep->csr)) &
1033 + (USB_OUTCSR_OUTPKTRDY | USB_OUTCSR_SENTSTALL)) {
1034 + DEBUG("%s: %x\n", __FUNCTION__, csr);
1036 + if (csr & USB_OUTCSR_SENTSTALL) {
1037 + DEBUG("%s: stall sent, flush fifo\n",
1039 + /* usb_set(USB_OUT_CSR1_FIFO_FLUSH, ep->csr1); */
1041 + } else if (csr & USB_OUTCSR_OUTPKTRDY) {
1042 + if (list_empty(&ep->queue))
1046 + list_entry(ep->queue.next,
1047 + struct jz4740_request,
1051 + DEBUG("%s: NULL REQ %d\n",
1052 + __FUNCTION__, ep_idx);
1055 + read_fifo(ep, req);
1060 + /* Throw packet away.. */
1061 + DEBUG("%s: ep %p ep_indx %d No descriptor?!?\n", __FUNCTION__, ep, ep_idx);
1066 +/** Halt specific EP
1067 + * Return 0 if success
1068 + * NOTE: Sets INDEX register to EP !
1070 +static int jz4740_set_halt(struct usb_ep *_ep, int value)
1072 + struct jz4740_udc *dev;
1073 + struct jz4740_ep *ep;
1074 + unsigned long flags;
1076 + DEBUG("%s:%s[%d]\n", __FILE__, __func__, __LINE__);
1078 + ep = container_of(_ep, struct jz4740_ep, ep);
1079 + if (unlikely(!_ep || (!ep->desc && ep->type != ep_control))) {
1080 + DEBUG("%s, bad ep\n", __FUNCTION__);
1086 + spin_lock_irqsave(&dev->lock, flags);
1088 + jz_udc_select_ep(ep);
1090 + DEBUG("%s, ep %d, val %d\n", __FUNCTION__, ep_index(ep), value);
1092 + if (ep_index(ep) == 0) {
1094 + usb_setb(dev, JZ_REG_UDC_CSR0, USB_CSR0_SENDSTALL);
1095 + } else if (ep_is_in(ep)) {
1096 + uint32_t csr = usb_readb(dev, ep->csr);
1097 + if (value && ((csr & USB_INCSR_FFNOTEMPT)
1098 + || !list_empty(&ep->queue))) {
1100 + * Attempts to halt IN endpoints will fail (returning -EAGAIN)
1101 + * if any transfer requests are still queued, or if the controller
1102 + * FIFO still holds bytes that the host hasn
\92t collected.
1104 + spin_unlock_irqrestore(&dev->lock, flags);
1106 + ("Attempt to halt IN endpoint failed (returning -EAGAIN) %d %d\n",
1107 + (csr & USB_INCSR_FFNOTEMPT),
1108 + !list_empty(&ep->queue));
1113 + usb_setb(dev, ep->csr, USB_INCSR_SENDSTALL);
1115 + usb_clearb(dev, ep->csr, USB_INCSR_SENDSTALL);
1116 + usb_setb(dev, ep->csr, USB_INCSR_CDT);
1122 + usb_setb(dev, ep->csr, USB_OUTCSR_SENDSTALL);
1124 + usb_clearb(dev, ep->csr, USB_OUTCSR_SENDSTALL);
1125 + usb_setb(dev, ep->csr, USB_OUTCSR_CDT);
1129 + ep->stopped = value;
1131 + spin_unlock_irqrestore(&dev->lock, flags);
1133 + DEBUG("%s %s halted\n", _ep->name, value == 0 ? "NOT" : "IS");
1139 +static int jz4740_ep_enable(struct usb_ep *_ep,
1140 + const struct usb_endpoint_descriptor *desc)
1142 + struct jz4740_ep *ep;
1143 + struct jz4740_udc *dev;
1144 + unsigned long flags;
1145 + uint32_t max, csrh = 0;
1147 + DEBUG("%s: trying to enable %s\n", __FUNCTION__, _ep->name);
1149 + if (!_ep || !desc)
1152 + ep = container_of(_ep, struct jz4740_ep, ep);
1153 + if (ep->desc || ep->type == ep_control
1154 + || desc->bDescriptorType != USB_DT_ENDPOINT
1155 + || ep->bEndpointAddress != desc->bEndpointAddress) {
1156 + DEBUG("%s, bad ep or descriptor\n", __FUNCTION__);
1160 + /* xfer types must match, except that interrupt ~= bulk */
1161 + if (ep->bmAttributes != desc->bmAttributes
1162 + && ep->bmAttributes != USB_ENDPOINT_XFER_BULK
1163 + && desc->bmAttributes != USB_ENDPOINT_XFER_INT) {
1164 + DEBUG("%s, %s type mismatch\n", __FUNCTION__, _ep->name);
1169 + if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
1170 + DEBUG("%s, bogus device state\n", __FUNCTION__);
1171 + return -ESHUTDOWN;
1174 + max = le16_to_cpu(desc->wMaxPacketSize);
1176 + spin_lock_irqsave(&ep->dev->lock, flags);
1178 + /* Configure the endpoint */
1179 + jz_udc_select_ep(ep);
1180 + if (ep_is_in(ep)) {
1181 + usb_writew(dev, JZ_REG_UDC_INMAXP, max);
1182 + switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
1183 + case USB_ENDPOINT_XFER_BULK:
1184 + case USB_ENDPOINT_XFER_INT:
1185 + csrh &= ~USB_INCSRH_ISO;
1187 + case USB_ENDPOINT_XFER_ISOC:
1188 + csrh |= USB_INCSRH_ISO;
1191 + usb_writeb(dev, JZ_REG_UDC_INCSRH, csrh);
1194 + usb_writew(dev, JZ_REG_UDC_OUTMAXP, max);
1195 + switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
1196 + case USB_ENDPOINT_XFER_BULK:
1197 + csrh &= ~USB_OUTCSRH_ISO;
1199 + case USB_ENDPOINT_XFER_INT:
1200 + csrh &= ~USB_OUTCSRH_ISO;
1201 + csrh |= USB_OUTCSRH_DNYT;
1203 + case USB_ENDPOINT_XFER_ISOC:
1204 + csrh |= USB_OUTCSRH_ISO;
1207 + usb_writeb(dev, JZ_REG_UDC_OUTCSRH, csrh);
1213 + ep->ep.maxpacket = max;
1215 + spin_unlock_irqrestore(&ep->dev->lock, flags);
1217 + /* Reset halt state (does flush) */
1218 + jz4740_set_halt(_ep, 0);
1220 + DEBUG("%s: enabled %s\n", __FUNCTION__, _ep->name);
1226 + * NOTE: Sets INDEX register
1228 +static int jz4740_ep_disable(struct usb_ep *_ep)
1230 + struct jz4740_ep *ep;
1231 + unsigned long flags;
1233 + DEBUG("%s, %p\n", __FUNCTION__, _ep);
1235 + ep = container_of(_ep, struct jz4740_ep, ep);
1236 + if (!_ep || !ep->desc) {
1237 + DEBUG("%s, %s not enabled\n", __FUNCTION__,
1238 + _ep ? ep->ep.name : NULL);
1242 + spin_lock_irqsave(&ep->dev->lock, flags);
1244 + jz_udc_select_ep(ep);
1246 + /* Nuke all pending requests (does flush) */
1247 + nuke(ep, -ESHUTDOWN);
1249 + /* Disable ep IRQ */
1250 + pio_irq_disable(ep);
1255 + spin_unlock_irqrestore(&ep->dev->lock, flags);
1257 + DEBUG("%s: disabled %s\n", __FUNCTION__, _ep->name);
1261 +static struct usb_request *jz4740_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
1263 + struct jz4740_request *req;
1265 + req = kzalloc(sizeof(*req), gfp_flags);
1269 + INIT_LIST_HEAD(&req->queue);
1274 +static void jz4740_free_request(struct usb_ep *ep, struct usb_request *_req)
1276 + struct jz4740_request *req;
1278 + DEBUG("%s, %p\n", __FUNCTION__, ep);
1280 + req = container_of(_req, struct jz4740_request, req);
1281 + WARN_ON(!list_empty(&req->queue));
1285 +/*--------------------------------------------------------------------*/
1287 +/** Queue one request
1288 + * Kickstart transfer if needed
1289 + * NOTE: Sets INDEX register
1291 +static int jz4740_queue(struct usb_ep *_ep, struct usb_request *_req,
1294 + struct jz4740_request *req;
1295 + struct jz4740_ep *ep;
1296 + struct jz4740_udc *dev;
1298 + DEBUG("%s, %p\n", __FUNCTION__, _ep);
1300 + req = container_of(_req, struct jz4740_request, req);
1302 + (!_req || !_req->complete || !_req->buf
1303 + || !list_empty(&req->queue))) {
1304 + DEBUG("%s, bad params\n", __FUNCTION__);
1308 + ep = container_of(_ep, struct jz4740_ep, ep);
1309 + if (unlikely(!_ep || (!ep->desc && ep->type != ep_control))) {
1310 + DEBUG("%s, bad ep\n", __FUNCTION__);
1315 + if (unlikely(!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)) {
1316 + DEBUG("%s, bogus device state %p\n", __FUNCTION__, dev->driver);
1317 + return -ESHUTDOWN;
1320 + DEBUG("%s queue req %p, len %d buf %p\n", _ep->name, _req, _req->length,
1323 + spin_lock_irqsave(&dev->lock, dev->lock_flags);
1325 + _req->status = -EINPROGRESS;
1328 + /* kickstart this i/o queue? */
1329 + DEBUG("Add to %d Q %d %d\n", ep_index(ep), list_empty(&ep->queue),
1331 + if (list_empty(&ep->queue) && likely(!ep->stopped)) {
1334 + if (unlikely(ep_index(ep) == 0)) {
1336 + list_add_tail(&req->queue, &ep->queue);
1337 + jz4740_ep0_kick(dev, ep);
1339 + } else if (use_dma) {
1341 + kick_dma(ep, req);
1344 + else if (ep_is_in(ep)) {
1346 + jz_udc_select_ep(ep);
1347 + csr = usb_readb(dev, ep->csr);
1348 + pio_irq_enable(ep);
1349 + if (!(csr & USB_INCSR_FFNOTEMPT)) {
1350 + if (write_fifo(ep, req) == 1)
1355 + jz_udc_select_ep(ep);
1356 + csr = usb_readb(dev, ep->csr);
1357 + pio_irq_enable(ep);
1358 + if (csr & USB_OUTCSR_OUTPKTRDY) {
1359 + if (read_fifo(ep, req) == 1)
1365 + /* pio or dma irq handler advances the queue. */
1366 + if (likely(req != 0))
1367 + list_add_tail(&req->queue, &ep->queue);
1369 + spin_unlock_irqrestore(&dev->lock, dev->lock_flags);
1374 +/* dequeue JUST ONE request */
1375 +static int jz4740_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1377 + struct jz4740_ep *ep;
1378 + struct jz4740_request *req;
1379 + unsigned long flags;
1381 + DEBUG("%s, %p\n", __FUNCTION__, _ep);
1383 + ep = container_of(_ep, struct jz4740_ep, ep);
1384 + if (!_ep || ep->type == ep_control)
1387 + spin_lock_irqsave(&ep->dev->lock, flags);
1389 + /* make sure it's actually queued on this endpoint */
1390 + list_for_each_entry(req, &ep->queue, queue) {
1391 + if (&req->req == _req)
1394 + if (&req->req != _req) {
1395 + spin_unlock_irqrestore(&ep->dev->lock, flags);
1398 + done(ep, req, -ECONNRESET);
1400 + spin_unlock_irqrestore(&ep->dev->lock, flags);
1404 +/** Return bytes in EP FIFO
1405 + * NOTE: Sets INDEX register to EP
1407 +static int jz4740_fifo_status(struct usb_ep *_ep)
1411 + struct jz4740_ep *ep;
1412 + unsigned long flags;
1414 + ep = container_of(_ep, struct jz4740_ep, ep);
1416 + DEBUG("%s, bad ep\n", __FUNCTION__);
1420 + DEBUG("%s, %d\n", __FUNCTION__, ep_index(ep));
1422 + /* LPD can't report unclaimed bytes from IN fifos */
1424 + return -EOPNOTSUPP;
1426 + spin_lock_irqsave(&ep->dev->lock, flags);
1427 + jz_udc_select_ep(ep);
1429 + csr = usb_readb(ep->dev, ep->csr);
1430 + if (ep->dev->gadget.speed != USB_SPEED_UNKNOWN ||
1432 + count = usb_readw(ep->dev, JZ_REG_UDC_OUTCOUNT);
1435 + spin_unlock_irqrestore(&ep->dev->lock, flags);
1441 + * NOTE: Sets INDEX register to EP
1443 +static void jz4740_fifo_flush(struct usb_ep *_ep)
1445 + struct jz4740_ep *ep;
1446 + unsigned long flags;
1448 + DEBUG("%s:%s[%d]\n", __FILE__, __func__, __LINE__);
1450 + ep = container_of(_ep, struct jz4740_ep, ep);
1451 + if (unlikely(!_ep || (!ep->desc && ep->type == ep_control))) {
1452 + DEBUG("%s, bad ep\n", __FUNCTION__);
1456 + spin_lock_irqsave(&ep->dev->lock, flags);
1458 + jz_udc_select_ep(ep);
1461 + spin_unlock_irqrestore(&ep->dev->lock, flags);
1464 +/****************************************************************/
1465 +/* End Point 0 related functions */
1466 +/****************************************************************/
1468 +/* return: 0 = still running, 1 = completed, negative = errno */
1469 +static int write_fifo_ep0(struct jz4740_ep *ep, struct jz4740_request *req)
1475 + DEBUG("%s:%s[%d]\n", __FILE__, __func__, __LINE__);
1476 + max = ep_maxpacket(ep);
1478 + count = write_packet(ep, req, max);
1480 + /* last packet is usually short (or a zlp) */
1481 + if (unlikely(count != max))
1484 + if (likely(req->req.length != req->req.actual) || req->req.zero)
1490 + DEBUG_EP0("%s: wrote %s %d bytes%s %d left %p\n", __FUNCTION__,
1491 + ep->ep.name, count,
1492 + is_last ? "/L" : "", req->req.length - req->req.actual, req);
1494 + /* requests complete when all IN data is in the FIFO */
1503 +static inline int jz4740_fifo_read(struct jz4740_ep *ep,
1504 + unsigned char *cp, int max)
1507 + int count = usb_readw(ep->dev, JZ_REG_UDC_OUTCOUNT);
1513 + *cp++ = usb_readb(ep->dev, ep->fifo);
1518 +static inline void jz4740_fifo_write(struct jz4740_ep *ep,
1519 + unsigned char *cp, int count)
1521 + DEBUG("fifo_write: %d %d\n", ep_index(ep), count);
1523 + usb_writeb(ep->dev, ep->fifo, *cp++);
1526 +static int read_fifo_ep0(struct jz4740_ep *ep, struct jz4740_request *req)
1528 + struct jz4740_udc *dev = ep->dev;
1531 + unsigned bufferspace, count, is_short;
1533 + DEBUG_EP0("%s\n", __FUNCTION__);
1535 + csr = usb_readb(dev, JZ_REG_UDC_CSR0);
1536 + if (!(csr & USB_CSR0_OUTPKTRDY))
1539 + buf = req->req.buf + req->req.actual;
1541 + bufferspace = req->req.length - req->req.actual;
1543 + /* read all bytes from this packet */
1544 + if (likely(csr & USB_CSR0_OUTPKTRDY)) {
1545 + count = usb_readw(dev, JZ_REG_UDC_OUTCOUNT);
1546 + req->req.actual += min(count, bufferspace);
1550 + is_short = (count < ep->ep.maxpacket);
1551 + DEBUG_EP0("read %s %02x, %d bytes%s req %p %d/%d\n",
1552 + ep->ep.name, csr, count,
1553 + is_short ? "/S" : "", req, req->req.actual, req->req.length);
1555 + while (likely(count-- != 0)) {
1556 + uint8_t byte = (uint8_t)usb_readl(dev, ep->fifo);
1558 + if (unlikely(bufferspace == 0)) {
1559 + /* this happens when the driver's buffer
1560 + * is smaller than what the host sent.
1561 + * discard the extra data.
1563 + if (req->req.status != -EOVERFLOW)
1564 + DEBUG_EP0("%s overflow %d\n", ep->ep.name,
1566 + req->req.status = -EOVERFLOW;
1574 + if (is_short || req->req.actual == req->req.length) {
1579 + /* finished that packet. the next one may be waiting... */
1584 + * udc_set_address - set the USB address for this device
1587 + * Called from control endpoint function after it decodes a set address setup packet.
1589 +static void udc_set_address(struct jz4740_udc *dev, unsigned char address)
1591 + DEBUG_EP0("%s: %d\n", __FUNCTION__, address);
1593 + usb_writeb(dev, JZ_REG_UDC_FADDR, address);
1597 + * DATA_STATE_RECV (USB_CSR0_OUTPKTRDY)
1599 + * set USB_CSR0_SVDOUTPKTRDY | USB_CSR0_DATAEND | USB_CSR0_SENDSTALL bits
1601 + * set USB_CSR0_SVDOUTPKTRDY bit
1602 + if last set USB_CSR0_DATAEND bit
1604 +static void jz4740_ep0_out(struct jz4740_udc *dev, uint32_t csr, int kickstart)
1606 + struct jz4740_request *req;
1607 + struct jz4740_ep *ep = &dev->ep[0];
1610 + DEBUG_EP0("%s: %x\n", __FUNCTION__, csr);
1612 + if (list_empty(&ep->queue))
1615 + req = list_entry(ep->queue.next, struct jz4740_request, queue);
1618 + if (req->req.length == 0) {
1619 + DEBUG_EP0("ZERO LENGTH OUT!\n");
1620 + usb_setb(dev, JZ_REG_UDC_CSR0, (USB_CSR0_SVDOUTPKTRDY | USB_CSR0_DATAEND));
1621 + dev->ep0state = WAIT_FOR_SETUP;
1623 + } else if (kickstart) {
1624 + usb_setb(dev, JZ_REG_UDC_CSR0, (USB_CSR0_SVDOUTPKTRDY));
1627 + ret = read_fifo_ep0(ep, req);
1630 + DEBUG_EP0("%s: finished, waiting for status\n",
1632 + usb_setb(dev, JZ_REG_UDC_CSR0, (USB_CSR0_SVDOUTPKTRDY | USB_CSR0_DATAEND));
1633 + dev->ep0state = WAIT_FOR_SETUP;
1635 + /* Not done yet.. */
1636 + DEBUG_EP0("%s: not finished\n", __FUNCTION__);
1637 + usb_setb(dev, JZ_REG_UDC_CSR0, USB_CSR0_SVDOUTPKTRDY);
1640 + DEBUG_EP0("NO REQ??!\n");
1647 +static int jz4740_ep0_in(struct jz4740_udc *dev, uint32_t csr)
1649 + struct jz4740_request *req;
1650 + struct jz4740_ep *ep = &dev->ep[0];
1651 + int ret, need_zlp = 0;
1653 + DEBUG_EP0("%s: %x\n", __FUNCTION__, csr);
1655 + if (list_empty(&ep->queue))
1658 + req = list_entry(ep->queue.next, struct jz4740_request, queue);
1661 + DEBUG_EP0("%s: NULL REQ\n", __FUNCTION__);
1665 + if (req->req.length == 0) {
1666 + usb_setb(dev, JZ_REG_UDC_CSR0, (USB_CSR0_INPKTRDY | USB_CSR0_DATAEND));
1667 + dev->ep0state = WAIT_FOR_SETUP;
1671 + if (req->req.length - req->req.actual == EP0_MAXPACKETSIZE) {
1672 + /* Next write will end with the packet size, */
1673 + /* so we need zero-length-packet */
1677 + ret = write_fifo_ep0(ep, req);
1679 + if (ret == 1 && !need_zlp) {
1681 + DEBUG_EP0("%s: finished, waiting for status\n", __FUNCTION__);
1683 + usb_setb(dev, JZ_REG_UDC_CSR0, (USB_CSR0_INPKTRDY | USB_CSR0_DATAEND));
1684 + dev->ep0state = WAIT_FOR_SETUP;
1686 + DEBUG_EP0("%s: not finished\n", __FUNCTION__);
1687 + usb_setb(dev, JZ_REG_UDC_CSR0, USB_CSR0_INPKTRDY);
1691 + DEBUG_EP0("%s: Need ZLP!\n", __FUNCTION__);
1692 + usb_setb(dev, JZ_REG_UDC_CSR0, USB_CSR0_INPKTRDY);
1693 + dev->ep0state = DATA_STATE_NEED_ZLP;
1699 +static int jz4740_handle_get_status(struct jz4740_udc *dev,
1700 + struct usb_ctrlrequest *ctrl)
1702 + struct jz4740_ep *ep0 = &dev->ep[0];
1703 + struct jz4740_ep *qep;
1704 + int reqtype = (ctrl->bRequestType & USB_RECIP_MASK);
1707 + DEBUG("%s:%s[%d]\n", __FILE__, __func__, __LINE__);
1709 + if (reqtype == USB_RECIP_INTERFACE) {
1710 + /* This is not supported.
1711 + * And according to the USB spec, this one does nothing..
1714 + DEBUG_SETUP("GET_STATUS: USB_RECIP_INTERFACE\n");
1715 + } else if (reqtype == USB_RECIP_DEVICE) {
1716 + DEBUG_SETUP("GET_STATUS: USB_RECIP_DEVICE\n");
1717 + val |= (1 << 0); /* Self powered */
1718 + /*val |= (1<<1); *//* Remote wakeup */
1719 + } else if (reqtype == USB_RECIP_ENDPOINT) {
1720 + int ep_num = (ctrl->wIndex & ~USB_DIR_IN);
1723 + ("GET_STATUS: USB_RECIP_ENDPOINT (%d), ctrl->wLength = %d\n",
1724 + ep_num, ctrl->wLength);
1726 + if (ctrl->wLength > 2 || ep_num > 3)
1727 + return -EOPNOTSUPP;
1729 + qep = &dev->ep[ep_num];
1730 + if (ep_is_in(qep) != ((ctrl->wIndex & USB_DIR_IN) ? 1 : 0)
1731 + && ep_index(qep) != 0) {
1732 + return -EOPNOTSUPP;
1735 + jz_udc_set_index(dev, ep_index(qep));
1737 + /* Return status on next IN token */
1738 + switch (qep->type) {
1741 + (usb_readb(dev, qep->csr) & USB_CSR0_SENDSTALL) ==
1742 + USB_CSR0_SENDSTALL;
1745 + case ep_interrupt:
1747 + (usb_readb(dev, qep->csr) & USB_INCSR_SENDSTALL) ==
1748 + USB_INCSR_SENDSTALL;
1752 + (usb_readb(dev, qep->csr) & USB_OUTCSR_SENDSTALL) ==
1753 + USB_OUTCSR_SENDSTALL;
1757 + /* Back to EP0 index */
1758 + jz_udc_set_index(dev, 0);
1760 + DEBUG_SETUP("GET_STATUS, ep: %d (%x), val = %d\n", ep_num,
1761 + ctrl->wIndex, val);
1763 + DEBUG_SETUP("Unknown REQ TYPE: %d\n", reqtype);
1764 + return -EOPNOTSUPP;
1767 + /* Clear "out packet ready" */
1768 + usb_setb(dev, JZ_REG_UDC_CSR0, USB_CSR0_SVDOUTPKTRDY);
1769 + /* Put status to FIFO */
1770 + jz4740_fifo_write(ep0, (uint8_t *)&val, sizeof(val));
1771 + /* Issue "In packet ready" */
1772 + usb_setb(dev, JZ_REG_UDC_CSR0, (USB_CSR0_INPKTRDY | USB_CSR0_DATAEND));
1778 + * WAIT_FOR_SETUP (OUTPKTRDY)
1779 + * - read data packet from EP0 FIFO
1780 + * - decode command
1782 + * set USB_CSR0_SVDOUTPKTRDY | USB_CSR0_DATAEND | USB_CSR0_SENDSTALL bits
1784 + * set USB_CSR0_SVDOUTPKTRDY | USB_CSR0_DATAEND bits
1786 +static void jz4740_ep0_setup(struct jz4740_udc *dev, uint32_t csr)
1788 + struct jz4740_ep *ep = &dev->ep[0];
1789 + struct usb_ctrlrequest ctrl;
1792 + DEBUG_SETUP("%s: %x\n", __FUNCTION__, csr);
1794 + /* Nuke all previous transfers */
1795 + nuke(ep, -EPROTO);
1797 + /* read control req from fifo (8 bytes) */
1798 + jz4740_fifo_read(ep, (unsigned char *)&ctrl, 8);
1800 + DEBUG_SETUP("SETUP %02x.%02x v%04x i%04x l%04x\n",
1801 + ctrl.bRequestType, ctrl.bRequest,
1802 + ctrl.wValue, ctrl.wIndex, ctrl.wLength);
1804 + /* Set direction of EP0 */
1805 + if (likely(ctrl.bRequestType & USB_DIR_IN)) {
1806 + ep->bEndpointAddress |= USB_DIR_IN;
1808 + ep->bEndpointAddress &= ~USB_DIR_IN;
1811 + /* Handle some SETUP packets ourselves */
1812 + switch (ctrl.bRequest) {
1813 + case USB_REQ_SET_ADDRESS:
1814 + if (ctrl.bRequestType != (USB_TYPE_STANDARD | USB_RECIP_DEVICE))
1817 + DEBUG_SETUP("USB_REQ_SET_ADDRESS (%d)\n", ctrl.wValue);
1818 + udc_set_address(dev, ctrl.wValue);
1819 + usb_setb(dev, JZ_REG_UDC_CSR0, (USB_CSR0_SVDOUTPKTRDY | USB_CSR0_DATAEND));
1822 + case USB_REQ_SET_CONFIGURATION:
1823 + if (ctrl.bRequestType != (USB_TYPE_STANDARD | USB_RECIP_DEVICE))
1826 + DEBUG_SETUP("USB_REQ_SET_CONFIGURATION (%d)\n", ctrl.wValue);
1827 +/* usb_setb(JZ_REG_UDC_CSR0, (USB_CSR0_SVDOUTPKTRDY | USB_CSR0_DATAEND));*/
1829 + /* Enable RESUME and SUSPEND interrupts */
1830 + usb_setb(dev, JZ_REG_UDC_INTRUSBE, (USB_INTR_RESUME | USB_INTR_SUSPEND));
1833 + case USB_REQ_SET_INTERFACE:
1834 + if (ctrl.bRequestType != (USB_TYPE_STANDARD | USB_RECIP_DEVICE))
1837 + DEBUG_SETUP("USB_REQ_SET_INTERFACE (%d)\n", ctrl.wValue);
1838 +/* usb_setb(JZ_REG_UDC_CSR0, (USB_CSR0_SVDOUTPKTRDY | USB_CSR0_DATAEND));*/
1841 + case USB_REQ_GET_STATUS:
1842 + if (jz4740_handle_get_status(dev, &ctrl) == 0)
1845 + case USB_REQ_CLEAR_FEATURE:
1846 + case USB_REQ_SET_FEATURE:
1847 + if (ctrl.bRequestType == USB_RECIP_ENDPOINT) {
1848 + struct jz4740_ep *qep;
1849 + int ep_num = (ctrl.wIndex & 0x0f);
1851 + /* Support only HALT feature */
1852 + if (ctrl.wValue != 0 || ctrl.wLength != 0
1853 + || ep_num > 3 || ep_num < 1)
1856 + qep = &dev->ep[ep_num];
1857 + spin_unlock(&dev->lock);
1858 + if (ctrl.bRequest == USB_REQ_SET_FEATURE) {
1859 + DEBUG_SETUP("SET_FEATURE (%d)\n",
1861 + jz4740_set_halt(&qep->ep, 1);
1863 + DEBUG_SETUP("CLR_FEATURE (%d)\n",
1865 + jz4740_set_halt(&qep->ep, 0);
1867 + spin_lock(&dev->lock);
1869 + jz_udc_set_index(dev, 0);
1871 + /* Reply with a ZLP on next IN token */
1872 + usb_setb(dev, JZ_REG_UDC_CSR0,
1873 + (USB_CSR0_SVDOUTPKTRDY | USB_CSR0_DATAEND));
1882 + /* gadget drivers see class/vendor specific requests,
1883 + * {SET,GET}_{INTERFACE,DESCRIPTOR,CONFIGURATION},
1886 + if (dev->driver) {
1887 + /* device-2-host (IN) or no data setup command, process immediately */
1888 + spin_unlock(&dev->lock);
1890 + i = dev->driver->setup(&dev->gadget, &ctrl);
1891 + spin_lock(&dev->lock);
1893 + if (unlikely(i < 0)) {
1894 + /* setup processing failed, force stall */
1896 + (" --> ERROR: gadget setup FAILED (stalling), setup returned %d\n",
1898 + jz_udc_set_index(dev, 0);
1899 + usb_setb(dev, JZ_REG_UDC_CSR0, (USB_CSR0_SVDOUTPKTRDY | USB_CSR0_DATAEND | USB_CSR0_SENDSTALL));
1901 + /* ep->stopped = 1; */
1902 + dev->ep0state = WAIT_FOR_SETUP;
1905 + DEBUG_SETUP("gadget driver setup ok (%d)\n", ctrl.wLength);
1906 +/* if (!ctrl.wLength) {
1907 + usb_setb(JZ_REG_UDC_CSR0, USB_CSR0_SVDOUTPKTRDY);
1914 + * DATA_STATE_NEED_ZLP
1916 +static void jz4740_ep0_in_zlp(struct jz4740_udc *dev, uint32_t csr)
1918 + DEBUG_EP0("%s: %x\n", __FUNCTION__, csr);
1920 + usb_setb(dev, JZ_REG_UDC_CSR0, (USB_CSR0_INPKTRDY | USB_CSR0_DATAEND));
1921 + dev->ep0state = WAIT_FOR_SETUP;
1925 + * handle ep0 interrupt
1927 +static void jz4740_handle_ep0(struct jz4740_udc *dev, uint32_t intr)
1929 + struct jz4740_ep *ep = &dev->ep[0];
1932 + DEBUG("%s:%s[%d]\n", __FILE__, __func__, __LINE__);
1934 + jz_udc_set_index(dev, 0);
1935 + csr = usb_readb(dev, JZ_REG_UDC_CSR0);
1937 + DEBUG_EP0("%s: csr = %x state = \n", __FUNCTION__, csr);//, state_names[dev->ep0state]);
1940 + * if SENT_STALL is set
1941 + * - clear the SENT_STALL bit
1943 + if (csr & USB_CSR0_SENTSTALL) {
1944 + DEBUG_EP0("%s: USB_CSR0_SENTSTALL is set: %x\n", __FUNCTION__, csr);
1945 + usb_clearb(dev, JZ_REG_UDC_CSR0, USB_CSR0_SENDSTALL | USB_CSR0_SENTSTALL);
1946 + nuke(ep, -ECONNABORTED);
1947 + dev->ep0state = WAIT_FOR_SETUP;
1952 + * if a transfer is in progress && INPKTRDY and OUTPKTRDY are clear
1954 + * - if last packet
1955 + * - set IN_PKT_RDY | DATA_END
1959 + if (!(csr & (USB_CSR0_INPKTRDY | USB_CSR0_OUTPKTRDY))) {
1960 + DEBUG_EP0("%s: INPKTRDY and OUTPKTRDY are clear\n",
1963 + switch (dev->ep0state) {
1964 + case DATA_STATE_XMIT:
1965 + DEBUG_EP0("continue with DATA_STATE_XMIT\n");
1966 + jz4740_ep0_in(dev, csr);
1968 + case DATA_STATE_NEED_ZLP:
1969 + DEBUG_EP0("continue with DATA_STATE_NEED_ZLP\n");
1970 + jz4740_ep0_in_zlp(dev, csr);
1974 +// DEBUG_EP0("Odd state!! state = %s\n",
1975 +// state_names[dev->ep0state]);
1976 + dev->ep0state = WAIT_FOR_SETUP;
1977 + /* nuke(ep, 0); */
1978 + /* usb_setb(ep->csr, USB_CSR0_SENDSTALL); */
1985 + * if SETUPEND is set
1986 + * - abort the last transfer
1987 + * - set SERVICED_SETUP_END_BIT
1989 + if (csr & USB_CSR0_SETUPEND) {
1990 + DEBUG_EP0("%s: USB_CSR0_SETUPEND is set: %x\n", __FUNCTION__, csr);
1992 + usb_setb(dev, JZ_REG_UDC_CSR0, USB_CSR0_SVDSETUPEND);
1994 + dev->ep0state = WAIT_FOR_SETUP;
1998 + * if USB_CSR0_OUTPKTRDY is set
1999 + * - read data packet from EP0 FIFO
2000 + * - decode command
2002 + * set SVDOUTPKTRDY | DATAEND | SENDSTALL bits
2004 + * set SVDOUTPKTRDY | DATAEND bits
2006 + if (csr & USB_CSR0_OUTPKTRDY) {
2008 + DEBUG_EP0("%s: EP0_OUT_PKT_RDY is set: %x\n", __FUNCTION__,
2011 + switch (dev->ep0state) {
2012 + case WAIT_FOR_SETUP:
2013 + DEBUG_EP0("WAIT_FOR_SETUP\n");
2014 + jz4740_ep0_setup(dev, csr);
2017 + case DATA_STATE_RECV:
2018 + DEBUG_EP0("DATA_STATE_RECV\n");
2019 + jz4740_ep0_out(dev, csr, 0);
2024 + DEBUG_EP0("strange state!! 2. send stall? state = %d\n",
2031 +static void jz4740_ep0_kick(struct jz4740_udc *dev, struct jz4740_ep *ep)
2035 + jz_udc_set_index(dev, 0);
2037 + DEBUG_EP0("%s: %x\n", __FUNCTION__, csr);
2039 + /* Clear "out packet ready" */
2041 + if (ep_is_in(ep)) {
2042 + usb_setb(dev, JZ_REG_UDC_CSR0, USB_CSR0_SVDOUTPKTRDY);
2043 + csr = usb_readb(dev, JZ_REG_UDC_CSR0);
2044 + dev->ep0state = DATA_STATE_XMIT;
2045 + jz4740_ep0_in(dev, csr);
2047 + csr = usb_readb(dev, JZ_REG_UDC_CSR0);
2048 + dev->ep0state = DATA_STATE_RECV;
2049 + jz4740_ep0_out(dev, csr, 1);
2053 +/** Handle USB RESET interrupt
2055 +static void jz4740_reset_irq(struct jz4740_udc *dev)
2057 + dev->gadget.speed = (usb_readb(dev, JZ_REG_UDC_POWER) & USB_POWER_HSMODE) ?
2058 + USB_SPEED_HIGH : USB_SPEED_FULL;
2060 + DEBUG_SETUP("%s: address = %d, speed = %s\n", __FUNCTION__, 0,
2061 + (dev->gadget.speed == USB_SPEED_HIGH) ? "HIGH":"FULL" );
2065 + * jz4740 usb device interrupt handler.
2067 +static irqreturn_t jz4740_udc_irq(int irq, void *devid)
2069 + struct jz4740_udc *jz4740_udc = devid;
2072 + uint32_t intr_usb = usb_readb(jz4740_udc, JZ_REG_UDC_INTRUSB) & 0x7; /* mask SOF */
2073 + uint32_t intr_in = usb_readw(jz4740_udc, JZ_REG_UDC_INTRIN);
2074 + uint32_t intr_out = usb_readw(jz4740_udc, JZ_REG_UDC_INTROUT);
2075 + uint32_t intr_dma = usb_readb(jz4740_udc, JZ_REG_UDC_INTR);
2077 + if (!intr_usb && !intr_in && !intr_out && !intr_dma)
2078 + return IRQ_HANDLED;
2081 + DEBUG("intr_out=%x intr_in=%x intr_usb=%x\n",
2082 + intr_out, intr_in, intr_usb);
2084 + spin_lock(&jz4740_udc->lock);
2085 + index = usb_readb(jz4740_udc, JZ_REG_UDC_INDEX);
2087 + /* Check for resume from suspend mode */
2088 + if ((intr_usb & USB_INTR_RESUME) &&
2089 + (usb_readb(jz4740_udc, JZ_REG_UDC_INTRUSBE) & USB_INTR_RESUME)) {
2090 + DEBUG("USB resume\n");
2091 + jz4740_udc->driver->resume(&jz4740_udc->gadget); /* We have suspend(), so we must have resume() too. */
2094 + /* Check for system interrupts */
2095 + if (intr_usb & USB_INTR_RESET) {
2096 + DEBUG("USB reset\n");
2097 + jz4740_reset_irq(jz4740_udc);
2100 + /* Check for endpoint 0 interrupt */
2101 + if (intr_in & USB_INTR_EP0) {
2102 + DEBUG("USB_INTR_EP0 (control)\n");
2103 + jz4740_handle_ep0(jz4740_udc, intr_in);
2106 + /* Check for Bulk-IN DMA interrupt */
2107 + if (intr_dma & 0x1) {
2109 + struct jz4740_ep *ep;
2110 + ep_num = (usb_readl(jz4740_udc, JZ_REG_UDC_CNTL1) >> 4) & 0xf;
2111 + ep = &jz4740_udc->ep[ep_num + 1];
2112 + jz_udc_set_index(jz4740_udc, ep_num);
2113 + usb_setb(jz4740_udc, ep->csr, USB_INCSR_INPKTRDY);
2114 +/* jz4740_in_epn(jz4740_udc, ep_num, intr_in);*/
2117 + /* Check for Bulk-OUT DMA interrupt */
2118 + if (intr_dma & 0x2) {
2120 + ep_num = (usb_readl(jz4740_udc, JZ_REG_UDC_CNTL2) >> 4) & 0xf;
2121 + jz4740_out_epn(jz4740_udc, ep_num, intr_out);
2124 + /* Check for each configured endpoint interrupt */
2125 + if (intr_in & USB_INTR_INEP1) {
2126 + DEBUG("USB_INTR_INEP1\n");
2127 + jz4740_in_epn(jz4740_udc, 1, intr_in);
2130 + if (intr_in & USB_INTR_INEP2) {
2131 + DEBUG("USB_INTR_INEP2\n");
2132 + jz4740_in_epn(jz4740_udc, 2, intr_in);
2135 + if (intr_out & USB_INTR_OUTEP1) {
2136 + DEBUG("USB_INTR_OUTEP1\n");
2137 + jz4740_out_epn(jz4740_udc, 1, intr_out);
2140 + /* Check for suspend mode */
2141 + if ((intr_usb & USB_INTR_SUSPEND) &&
2142 + (usb_readb(jz4740_udc, JZ_REG_UDC_INTRUSBE) & USB_INTR_SUSPEND)) {
2143 + DEBUG("USB suspend\n");
2144 + jz4740_udc->driver->suspend(&jz4740_udc->gadget);
2145 + /* Host unloaded from us, can do something, such as flushing
2146 + the NAND block cache etc. */
2149 + jz_udc_set_index(jz4740_udc, index);
2151 + spin_unlock(&jz4740_udc->lock);
2153 + return IRQ_HANDLED;
2158 +/*-------------------------------------------------------------------------*/
2161 +static inline struct jz4740_udc *gadget_to_udc(struct usb_gadget *gadget)
2163 + return container_of(gadget, struct jz4740_udc, gadget);
2166 +static int jz4740_udc_get_frame(struct usb_gadget *_gadget)
2168 + DEBUG("%s, %p\n", __FUNCTION__, _gadget);
2169 + return usb_readw(gadget_to_udc(_gadget), JZ_REG_UDC_FRAME);
2172 +static int jz4740_udc_wakeup(struct usb_gadget *_gadget)
2174 + /* host may not have enabled remote wakeup */
2175 + /*if ((UDCCS0 & UDCCS0_DRWF) == 0)
2176 + return -EHOSTUNREACH;
2177 + udc_set_mask_UDCCR(UDCCR_RSM); */
2181 +static int jz4740_udc_pullup(struct usb_gadget *_gadget, int on)
2183 + struct jz4740_udc *udc = gadget_to_udc(_gadget);
2184 + unsigned long flags;
2186 + local_irq_save(flags);
2189 + udc->state = UDC_STATE_ENABLE;
2192 + udc->state = UDC_STATE_DISABLE;
2196 + local_irq_restore(flags);
2202 +static const struct usb_gadget_ops jz4740_udc_ops = {
2203 + .get_frame = jz4740_udc_get_frame,
2204 + .wakeup = jz4740_udc_wakeup,
2205 + .pullup = jz4740_udc_pullup,
2208 +static struct usb_ep_ops jz4740_ep_ops = {
2209 + .enable = jz4740_ep_enable,
2210 + .disable = jz4740_ep_disable,
2212 + .alloc_request = jz4740_alloc_request,
2213 + .free_request = jz4740_free_request,
2215 + .queue = jz4740_queue,
2216 + .dequeue = jz4740_dequeue,
2218 + .set_halt = jz4740_set_halt,
2219 + .fifo_status = jz4740_fifo_status,
2220 + .fifo_flush = jz4740_fifo_flush,
2224 +/*-------------------------------------------------------------------------*/
2226 +static struct jz4740_udc jz4740_udc_controller = {
2228 + .ops = &jz4740_udc_ops,
2229 + .ep0 = &jz4740_udc_controller.ep[0].ep,
2230 + .name = "jz4740-udc",
2232 + .init_name = "gadget",
2236 + /* control endpoint */
2240 + .ops = &jz4740_ep_ops,
2241 + .maxpacket = EP0_MAXPACKETSIZE,
2243 + .dev = &jz4740_udc_controller,
2245 + .bEndpointAddress = 0,
2246 + .bmAttributes = 0,
2248 + .type = ep_control,
2249 + .fifo = JZ_REG_UDC_EP_FIFO(0),
2250 + .csr = JZ_REG_UDC_CSR0,
2253 + /* bulk out endpoint */
2256 + .name = "ep1out-bulk",
2257 + .ops = &jz4740_ep_ops,
2258 + .maxpacket = EPBULK_MAXPACKETSIZE,
2260 + .dev = &jz4740_udc_controller,
2262 + .bEndpointAddress = 1,
2263 + .bmAttributes = USB_ENDPOINT_XFER_BULK,
2265 + .type = ep_bulk_out,
2266 + .fifo = JZ_REG_UDC_EP_FIFO(1),
2267 + .csr = JZ_REG_UDC_OUTCSR,
2270 + /* bulk in endpoint */
2273 + .name = "ep1in-bulk",
2274 + .ops = &jz4740_ep_ops,
2275 + .maxpacket = EPBULK_MAXPACKETSIZE,
2277 + .dev = &jz4740_udc_controller,
2279 + .bEndpointAddress = 1 | USB_DIR_IN,
2280 + .bmAttributes = USB_ENDPOINT_XFER_BULK,
2282 + .type = ep_bulk_in,
2283 + .fifo = JZ_REG_UDC_EP_FIFO(1),
2284 + .csr = JZ_REG_UDC_INCSR,
2287 + /* interrupt in endpoint */
2290 + .name = "ep2in-int",
2291 + .ops = &jz4740_ep_ops,
2292 + .maxpacket = EPINTR_MAXPACKETSIZE,
2294 + .dev = &jz4740_udc_controller,
2296 + .bEndpointAddress = 2 | USB_DIR_IN,
2297 + .bmAttributes = USB_ENDPOINT_XFER_INT,
2299 + .type = ep_interrupt,
2300 + .fifo = JZ_REG_UDC_EP_FIFO(2),
2301 + .csr = JZ_REG_UDC_INCSR,
2305 +static int __devinit jz4740_udc_probe(struct platform_device *pdev)
2307 + struct jz4740_udc *jz4740_udc = &jz4740_udc_controller;
2310 + spin_lock_init(&jz4740_udc->lock);
2312 + jz4740_udc->dev = &pdev->dev;
2313 + jz4740_udc->gadget.dev.parent = &pdev->dev;
2314 + jz4740_udc->gadget.dev.dma_mask = pdev->dev.dma_mask;
2316 + ret = device_register(&jz4740_udc->gadget.dev);
2320 + jz4740_udc->clk = clk_get(&pdev->dev, "udc");
2321 + if (IS_ERR(jz4740_udc->clk)) {
2322 + ret = PTR_ERR(jz4740_udc->clk);
2323 + dev_err(&pdev->dev, "Failed to get udc clock: %d\n", ret);
2324 + goto err_device_unregister;
2327 + platform_set_drvdata(pdev, jz4740_udc);
2329 + jz4740_udc->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2331 + if (!jz4740_udc->mem) {
2333 + dev_err(&pdev->dev, "Failed to get mmio memory resource\n");
2337 + jz4740_udc->mem = request_mem_region(jz4740_udc->mem->start,
2338 + resource_size(jz4740_udc->mem), pdev->name);
2340 + if (!jz4740_udc->mem) {
2342 + dev_err(&pdev->dev, "Failed to request mmio memory region\n");
2343 + goto err_device_unregister;
2346 + jz4740_udc->base = ioremap(jz4740_udc->mem->start, resource_size(jz4740_udc->mem));
2348 + if (!jz4740_udc->base) {
2350 + dev_err(&pdev->dev, "Failed to ioremap mmio memory\n");
2351 + goto err_release_mem_region;
2354 + jz4740_udc->irq = platform_get_irq(pdev, 0);
2355 + ret = request_irq(jz4740_udc->irq, jz4740_udc_irq, 0, pdev->name,
2358 + dev_err(&pdev->dev, "Failed to request irq: %d\n", ret);
2362 + udc_disable(jz4740_udc);
2363 + udc_reinit(jz4740_udc);
2368 + iounmap(jz4740_udc->base);
2369 +err_release_mem_region:
2370 + release_mem_region(jz4740_udc->mem->start, resource_size(jz4740_udc->mem));
2372 + clk_put(jz4740_udc->clk);
2373 +err_device_unregister:
2374 + device_unregister(&jz4740_udc->gadget.dev);
2375 + platform_set_drvdata(pdev, NULL);
2380 +static int __devexit jz4740_udc_remove(struct platform_device *pdev)
2382 + struct jz4740_udc *dev = platform_get_drvdata(pdev);
2389 + free_irq(dev->irq, dev);
2390 + iounmap(dev->base);
2391 + release_mem_region(dev->mem->start, resource_size(dev->mem));
2392 + clk_put(dev->clk);
2394 + platform_set_drvdata(pdev, NULL);
2395 + device_unregister(&dev->gadget.dev);
2402 +static int jz4740_udc_suspend(struct device *dev)
2404 + struct jz4740_udc *jz4740_udc = dev_get_drvdata(dev);
2406 + if (jz4740_udc->state == UDC_STATE_ENABLE)
2407 + udc_disable(jz4740_udc);
2412 +static int jz4740_udc_resume(struct device *dev)
2414 + struct jz4740_udc *jz4740_udc = dev_get_drvdata(dev);
2416 + if (jz4740_udc->state == UDC_STATE_ENABLE)
2417 + udc_enable(jz4740_udc);
2422 +static const struct dev_pm_ops jz4740_udc_pm_ops = {
2423 + .suspend = jz4740_udc_suspend,
2424 + .resume = jz4740_udc_resume,
2427 +#define JZ4740_UDC_PM_OPS (&jz4740_udc_pm_ops)
2430 +#define JZ4740_UDC_PM_OPS NULL
2433 +static struct platform_driver udc_driver = {
2434 + .probe = jz4740_udc_probe,
2435 + .remove = __devexit_p(jz4740_udc_remove),
2438 + .owner = THIS_MODULE,
2439 + .pm = JZ4740_UDC_PM_OPS,
2443 +/*-------------------------------------------------------------------------*/
2445 +static int __init udc_init (void)
2447 + return platform_driver_register(&udc_driver);
2449 +module_init(udc_init);
2451 +static void __exit udc_exit (void)
2453 + platform_driver_unregister(&udc_driver);
2455 +module_exit(udc_exit);
2457 +MODULE_DESCRIPTION("JZ4740 USB Device Controller");
2458 +MODULE_AUTHOR("Wei Jianli <jlwei@ingenic.cn>");
2459 +MODULE_LICENSE("GPL");
2461 +++ b/drivers/usb/gadget/jz4740_udc.h
2464 + * linux/drivers/usb/gadget/jz4740_udc.h
2466 + * Ingenic JZ4740 on-chip high speed USB device controller
2468 + * Copyright (C) 2006 Ingenic Semiconductor Inc.
2469 + * Author: <jlwei@ingenic.cn>
2471 + * This program is free software; you can redistribute it and/or modify
2472 + * it under the terms of the GNU General Public License as published by
2473 + * the Free Software Foundation; either version 2 of the License, or
2474 + * (at your option) any later version.
2477 +#ifndef __USB_GADGET_JZ4740_H__
2478 +#define __USB_GADGET_JZ4740_H__
2480 +/*-------------------------------------------------------------------------*/
2483 +#define EP0_MAXPACKETSIZE 64
2484 +#define EPBULK_MAXPACKETSIZE 512
2485 +#define EPINTR_MAXPACKETSIZE 64
2487 +#define UDC_MAX_ENDPOINTS 4
2489 +/*-------------------------------------------------------------------------*/
2492 + ep_control, ep_bulk_in, ep_bulk_out, ep_interrupt
2497 + struct jz4740_udc *dev;
2499 + const struct usb_endpoint_descriptor *desc;
2502 + uint8_t bEndpointAddress;
2503 + uint8_t bmAttributes;
2505 + enum ep_type type;
2509 + uint32_t reg_addr;
2510 + struct list_head queue;
2513 +struct jz4740_request {
2514 + struct usb_request req;
2515 + struct list_head queue;
2519 + WAIT_FOR_SETUP, /* between STATUS ack and SETUP report */
2520 + DATA_STATE_XMIT, /* data tx stage */
2521 + DATA_STATE_NEED_ZLP, /* data tx zlp stage */
2522 + WAIT_FOR_OUT_STATUS, /* status stages */
2523 + DATA_STATE_RECV, /* data rx stage */
2526 +/* For function binding with UDC Disable - Added by River */
2528 + UDC_STATE_ENABLE = 0,
2529 + UDC_STATE_DISABLE,
2532 +struct jz4740_udc {
2533 + struct usb_gadget gadget;
2534 + struct usb_gadget_driver *driver;
2535 + struct device *dev;
2537 + unsigned long lock_flags;
2539 + enum ep0state ep0state;
2540 + struct jz4740_ep ep[UDC_MAX_ENDPOINTS];
2542 + udc_state_t state;
2544 + struct resource *mem;
2545 + void __iomem *base;
2551 +#define ep_maxpacket(EP) ((EP)->ep.maxpacket)
2553 +static inline bool ep_is_in(const struct jz4740_ep *ep)
2555 + return (ep->bEndpointAddress & USB_DIR_IN) == USB_DIR_IN;
2558 +static inline uint8_t ep_index(const struct jz4740_ep *ep)
2560 + return ep->bEndpointAddress & 0xf;
2563 +#endif /* __USB_GADGET_JZ4740_H__ */