1 From 0e3b1bffd1974e6852912865a7cea481617b1c39 Mon Sep 17 00:00:00 2001
2 From: John Crispin <blogic@openwrt.org>
3 Date: Thu, 30 May 2013 16:06:35 +0200
4 Subject: [PATCH 162/164] USB: MIPS: ralink: add rt5350/mt7620 UDC
6 Signed-off-by: John Crispin <blogic@openwrt.org>
8 drivers/usb/gadget/Kconfig | 8 +
9 drivers/usb/gadget/Makefile | 1 +
10 drivers/usb/gadget/rt_udc.h | 417 +++++++
11 drivers/usb/gadget/rt_udc_pdma.c | 2547 ++++++++++++++++++++++++++++++++++++++
12 4 files changed, 2973 insertions(+)
13 create mode 100644 drivers/usb/gadget/rt_udc.h
14 create mode 100644 drivers/usb/gadget/rt_udc_pdma.c
16 diff --git a/drivers/usb/gadget/Kconfig b/drivers/usb/gadget/Kconfig
17 index c7525b1..6f0e293 100644
18 --- a/drivers/usb/gadget/Kconfig
19 +++ b/drivers/usb/gadget/Kconfig
20 @@ -336,6 +336,14 @@ config USB_MV_U3D
21 MARVELL PXA2128 Processor series include a super speed USB3.0 device
22 controller, which support super speed USB peripheral.
25 + boolean "Ralink USB Device Port"
26 + depends on SOC_MT7620
28 + Say "y" to link the driver statically, or "m" to build a
29 + dynamically linked module called "rt_udc" and force all
30 + gadget drivers to also be dynamically linked.
33 # Controllers available in both integrated and discrete versions
35 diff --git a/drivers/usb/gadget/Makefile b/drivers/usb/gadget/Makefile
36 index 82fb225..f78a3b2 100644
37 --- a/drivers/usb/gadget/Makefile
38 +++ b/drivers/usb/gadget/Makefile
39 @@ -34,6 +34,7 @@ obj-$(CONFIG_USB_MV_UDC) += mv_udc.o
40 mv_udc-y := mv_udc_core.o
41 obj-$(CONFIG_USB_FUSB300) += fusb300_udc.o
42 obj-$(CONFIG_USB_MV_U3D) += mv_u3d_core.o
43 +obj-$(CONFIG_USB_RT_UDC) += rt_udc_pdma.o
46 obj-$(CONFIG_USB_F_ACM) += f_acm.o
47 diff --git a/drivers/usb/gadget/rt_udc.h b/drivers/usb/gadget/rt_udc.h
49 index 0000000..088e0d9
51 +++ b/drivers/usb/gadget/rt_udc.h
54 + * Copyright (C) 2009 Y.Y. Huang, Ralink Tech.(yy_huang@ralinktech.com)
56 + * This udc driver is now under testing and code is based on pxa2xx_udc.h
57 + * Please use it with your own risk!
59 + * This program is free software; you can redistribute it and/or modify
60 + * it under the terms of the GNU General Public License as published by
61 + * the Free Software Foundation; either version 2 of the License, or
62 + * (at your option) any later version.
64 + * This program is distributed in the hope that it will be useful,
65 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
66 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
67 + * GNU General Public License for more details.
70 +#ifndef __LINUX_USB_GADGET_RT_UDC_H
71 +#define __LINUX_USB_GADGET_RT_UDC_H
73 +#define CONFIG_RALINK_MT7620
75 +#include <linux/types.h>
77 +//#include "../host/ralink_usb.h" /* for port sharing setting and power saving purpose */
79 +#if defined (CONFIG_RALINK_RT3883) || defined (CONFIG_RALINK_RT3352) || defined (CONFIG_RALINK_MT7620)
82 +#elif defined (CONFIG_RALINK_RT5350)
86 +#error "Please define a platform."
90 +#define EP_IDX(ep) ((ep->bEndpointAddress & ~USB_DIR_IN)+(EP_DIR(ep)? 0:IN_EP_NUM)) /* IN:1, OUT:0 */
91 +#define EP_NO(ep) ((ep->bEndpointAddress & ~USB_DIR_IN)) /* IN:1, OUT:0 */
92 +#define EP_DIR(ep) ((ep->bEndpointAddress) & USB_DIR_IN ? 1 : 0)
95 +#define RT_USB_NB_EP (IN_EP_NUM + OUT_EP_NUM + 1)
97 +/* Driver structures */
99 + struct usb_request req;
100 + struct list_head queue;
101 + unsigned int in_use;
102 + struct rt_ep_struct *rt_ep; // test for rx tasklet
103 + int zlp_dma_done; // used for DMA ZLP packet.
110 + EP0_OUT_DATA_PHASE,
115 +struct rt_ep_struct {
117 + struct rt_udc_struct *rt_usb;
118 + struct list_head queue;
119 + unsigned char stopped;
120 + unsigned char bEndpointAddress;
121 + unsigned char bmAttributes;
123 + unsigned char pending;
124 + unsigned int rx_done_count; /* used by OUT EP only */
125 + unsigned int tx_done_count; /* used by OUT EP only */
128 +struct rt_udc_struct {
129 + struct usb_gadget gadget;
130 + struct usb_gadget_driver *driver;
131 + struct device *dev;
132 + struct rt_ep_struct rt_ep[RT_USB_NB_EP];
133 + /* struct clk *clk; */
134 + struct timer_list timer;
135 + enum ep0_state ep0state;
136 + struct resource *res;
137 + void __iomem *base;
138 + unsigned char set_config;
145 +#define USB_BASE (0xB0120000)
147 +#define OUT0BC (0x000)
148 +#define IN0BC (0x001)
149 +#define EP0CS (0x002)
151 +#define OUT1CON (0x00A)
152 +#define IN1CON (0x00E)
153 +#define OUT2CON (0x012)
154 +#define IN2CON (0x016)
155 +#define OUT3CON (0x01A)
156 +#define IN3CON (0x01E)
157 +#define OUT4CON (0x022)
158 +#define IN4CON (0x026)
161 +#define EP0INDAT (0x100)
162 +#define EP0OUTDAT (0x140)
163 +#define SETUPDATA (0x180)
165 +#define IN07IRQ (0x188)
166 +#define IN815IRQ (0x189)
167 +#define OUT07IRQ (0x18A)
168 +#define OUT815IRQ (0x18B)
169 +#define USBIRQ (0x18C)
170 +#define OUT07PNGIRQ (0x18E)
171 +#define OUT815PNGIRQ (0x18F)
173 +#define IN07IEN (0x194)
174 +#define OUT07IEN (0x196)
175 +#define USBIEN (0x198)
177 +#define OUT07PNGIEN (0x19A)
178 +#define OUT815PNGIEN (0x19B)
180 +#define ENDPRST (0x1A2)
181 +#define ENDPRST_IO (0x1 << 4)
182 +#define ENDPRST_TOGRST (0x1 << 5)
183 +#define ENDPRST_FIFORST (0x1 << 6)
185 +#define FIFOCTRL (0x1A8)
187 +#define EP_CS_EP0_STALL (0x1 << 0)
188 +#define EP_CS_EP0_HSNAK (0x1 << 1)
189 +#define EP_CS_EP0_INBSY (0x1 << 2)
190 +#define EP_CS_EP0_OUTBSY (0x1 << 3)
191 +#define EP_CS_AUTO (0x1 << 4)
192 +#define EP_CS_NPAK1 (0x1 << 3)
193 +#define EP_CS_NPAK0 (0x1 << 2)
194 +#define EP_CS_BSY (0x1 << 1)
195 +#define EP_CS_ERR (0x1 << 0)
197 +#define EP0_OUT_BSY (0x1 << 3)
198 +#define EP0_IN_BSY (0x1 << 2)
200 +#define USB_INTR_HSPEED (0x20)
201 +#define USB_INTR_RESET (0x10)
202 +#define USB_INTR_SUSPEND (0x08)
203 +#define USB_INTR_SETUP_TOKEN (0x04)
204 +#define USB_INTR_SOF (0x02)
205 +#define USB_INTR_SETUP_TOKEN_VALID (0x01)
208 +#define RTUSB_UDMA_CTRL (USB_BASE + 0x800)
209 +#define RTUSB_UDMA_WRR (USB_BASE + 0x804)
212 +#define RTUSB_TX_BASE_PTR0 (USB_BASE + 0x1000)
213 +#define RTUSB_TX_MAX_CNT0 (USB_BASE + 0x1004)
214 +#define RTUSB_TX_CTX_IDX0 (USB_BASE + 0x1008)
215 +#define RTUSB_TX_DTX_IDX0 (USB_BASE + 0x100C)
216 +#define RTUSB_TX_BASE_PTR1 (USB_BASE + 0x1010)
217 +#define RTUSB_TX_MAX_CNT1 (USB_BASE + 0x1014)
218 +#define RTUSB_TX_CTX_IDX1 (USB_BASE + 0x1018)
219 +#define RTUSB_TX_DTX_IDX1 (USB_BASE + 0x101C)
220 +#define RTUSB_RX_BASE_PTR0 (USB_BASE + 0x1100)
221 +#define RTUSB_RX_MAX_CNT0 (USB_BASE + 0x1104)
222 +#define RTUSB_RX_CALC_IDX0 (USB_BASE + 0x1108)
223 +#define RTUSB_RX_DRX_IDX0 (USB_BASE + 0x110C)
224 +#define RTUSB_PDMA_GLO_CFG (USB_BASE + 0x1204)
226 +#define RTUSB_TX_WB_DDONE (0x1 << 6)
227 +#define RTUSB_RX_DMA_BUSY (0x1 << 3)
228 +#define RTUSB_RX_DMA_EN (0x1 << 2)
229 +#define RTUSB_TX_DMA_BUSY (0x1 << 1)
230 +#define RTUSB_TX_DMA_EN (0x1 << 0)
232 +#define RTUSB_PDMA_RST_IDX (USB_BASE + 0x1208)
234 +#define RTUSB_RST_DRX_IDX1 (0x1 << 17)
235 +#define RTUSB_RST_DRX_IDX0 (0x1 << 16)
236 +#define RTUSB_RST_DTX_IDX3 (0x1 << 3)
237 +#define RTUSB_RST_DTX_IDX2 (0x1 << 2)
238 +#define RTUSB_RST_DTX_IDX1 (0x1 << 1)
239 +#define RTUSB_RST_DTX_IDX0 (0x1 << 0)
241 +#define RTUSB_DELAY_INT_CFG (USB_BASE + 0x120C)
242 +#define RTUSB_INT_STATUS (USB_BASE + 0x1220)
243 +#define RTUSB_RX_DONE_INT1 (0x1 << 17)
244 +#define RTUSB_RX_DONE_INT0 (0x1 << 16)
245 +#define RTUSB_TX_DONE_INT3 (0x1 << 3)
246 +#define RTUSB_TX_DONE_INT2 (0x1 << 2)
247 +#define RTUSB_TX_DONE_INT1 (0x1 << 1)
248 +#define RTUSB_TX_DONE_INT0 (0x1 << 0)
250 +#define RTUSB_INT_MASK (USB_BASE + 0x1228)
251 +#define RTUSB_RX_DONE_INT_MSK1 (0x1 << 17)
252 +#define RTUSB_RX_DONE_INT_MSK0 (0x1 << 16)
253 +#define RTUSB_TX_DONE_INT_MSK3 (0x1 << 3)
254 +#define RTUSB_TX_DONE_INT_MSK2 (0x1 << 2)
255 +#define RTUSB_TX_DONE_INT_MSK1 (0x1 << 1)
256 +#define RTUSB_TX_DONE_INT_MSK0 (0x1 << 0)
259 +/*=========================================
260 + PDMA RX Descriptor Format define
261 +=========================================*/
262 +//-------------------------------------------------
263 +typedef struct _PDMA_RXD_INFO1_ PDMA_RXD_INFO1_T;
265 +struct _PDMA_RXD_INFO1_ {
268 +//-------------------------------------------------
269 +typedef struct _PDMA_RXD_INFO2_ PDMA_RXD_INFO2_T;
271 +struct _PDMA_RXD_INFO2_ {
272 + unsigned int PLEN1 : 14;
273 + unsigned int LS1 : 1;
274 + unsigned int UN_USED : 1;
275 + unsigned int PLEN0 : 14;
276 + unsigned int LS0 : 1;
277 + unsigned int DDONE_bit : 1;
279 +//-------------------------------------------------
280 +typedef struct _PDMA_RXD_INFO3_ PDMA_RXD_INFO3_T;
282 +struct _PDMA_RXD_INFO3_ {
285 +//-------------------------------------------------
286 +typedef struct _PDMA_RXD_INFO4_ PDMA_RXD_INFO4_T;
288 +struct _PDMA_RXD_INFO4_ {
289 + unsigned int Rx_bcnt:16;
290 + unsigned int Reserved1:8;
291 + unsigned int Out_ep_addr:4;
292 + unsigned int Reserved0:4;
294 +struct PDMA_rxdesc {
295 + PDMA_RXD_INFO1_T rxd_info1;
296 + PDMA_RXD_INFO2_T rxd_info2;
297 + PDMA_RXD_INFO3_T rxd_info3;
298 + PDMA_RXD_INFO4_T rxd_info4;
300 +/*=========================================
301 + PDMA TX Descriptor Format define
302 +=========================================*/
303 +//-------------------------------------------------
304 +typedef struct _PDMA_TXD_INFO1_ PDMA_TXD_INFO1_T;
306 +struct _PDMA_TXD_INFO1_ {
309 +//-------------------------------------------------
310 +typedef struct _PDMA_TXD_INFO2_ PDMA_TXD_INFO2_T;
312 +struct _PDMA_TXD_INFO2_ {
313 + unsigned int SDL1 : 14;
314 + unsigned int LS1_bit : 1;
315 + unsigned int BURST_bit : 1;
316 + unsigned int SDL0 : 14;
317 + unsigned int LS0_bit : 1;
318 + unsigned int DDONE_bit : 1;
320 +//-------------------------------------------------
321 +typedef struct _PDMA_TXD_INFO3_ PDMA_TXD_INFO3_T;
323 +struct _PDMA_TXD_INFO3_ {
326 +//-------------------------------------------------
327 +typedef struct _PDMA_TXD_INFO4_ PDMA_TXD_INFO4_T;
328 +struct _PDMA_TXD_INFO4_ {
329 + unsigned int reserved2:17;
330 + unsigned int zlp_flag:1;
331 + unsigned int reserved1:6;
332 + unsigned int In_ep_addr:4;
333 + unsigned int rsv:4;
336 +struct PDMA_txdesc {
337 + PDMA_TXD_INFO1_T txd_info1;
338 + PDMA_TXD_INFO2_T txd_info2;
339 + PDMA_TXD_INFO3_T txd_info3;
340 + PDMA_TXD_INFO4_T txd_info4;
345 +#define DBG do{ if(debuglevel) printk("%s()\n", __FUNCTION__); }while(0);
346 +#define DD do{ printk("%s: %s %d\n", driver_name, __FUNCTION__, __LINE__); } while(0);
347 +#define xprintk(fmt, args...) do{ if(debuglevel) printk(fmt, ## args); } while(0);
351 +#define xprintk(fmt, args...)
354 +#define FATAL_ERROR(fmt, args...) do{ printk(fmt, ## args); printk("\n############### ERROR #####################\n %s %d\n############### ERROR #####################\n", __FUNCTION__, __LINE__); BUG(); } while(0)
356 +static void inline dump_usbirq(u32 irqreg)
359 + xprintk("U%s%s%s%s%s%s\n",
360 + (irqreg & USB_INTR_SOF) ? "sof" : "",
361 + (irqreg & USB_INTR_RESET) ? " rst" : "",
362 + (irqreg & USB_INTR_SUSPEND) ? " sus" : "",
363 + (irqreg & USB_INTR_SETUP_TOKEN) ? "st" : "",
364 + (irqreg & USB_INTR_SETUP_TOKEN_VALID) ? "sv" : "",
365 + (irqreg & USB_INTR_HSPEED) ? " HS" : "");
367 +// if(irqreg & USB_INTR_SETUP_TOKEN)
369 +// if(irqreg & USB_INTR_SETUP_TOKEN_VALID)
374 +static void inline dump_epirq(u32 irqreg, u32 ienreg, int dir)
377 + xprintk("%s%x\n", dir? "I" : "O", irqreg);
380 +static __inline__ u32 usb_read(u32 addr)
382 + return ioread32( (void __iomem *)(USB_BASE + (addr << 2)) );
385 +static __inline__ void usb_write(u32 addr, u32 value)
387 + iowrite32(value, (void __iomem *)(USB_BASE + (addr << 2)) );
390 +static __inline__ void reg_write(u32 addr, u32 value)
392 + iowrite32(value, (void __iomem *)0x0 + addr);
395 +static __inline__ u32 reg_read(u32 addr)
397 + return ioread32( (void __iomem *)0x0 + addr);
401 +static void handle_pending_epoutirq(struct rt_udc_struct *rt_usb, struct rt_ep_struct *rt_ep, struct rt_request *req);
413 + #define D_REQ(dev, args...) printk(args)
415 + #define D_REQ(dev, args...) do {} while (0)
416 +#endif /* DEBUG_REQ */
419 + #define D_TRX(dev, args...) printk(args)
421 + #define D_TRX(dev, args...) do {} while (0)
422 +#endif /* DEBUG_TRX */
425 + #define D_INI(dev, args...) printk(args)
427 + #define D_INI(dev, args...) do {} while (0)
428 +#endif /* DEBUG_INIT */
431 + static const char *state_name[] = {
438 + #define D_EP0(dev, args...) printk(args)
440 + #define D_EP0(dev, args...) do {} while (0)
441 +#endif /* DEBUG_EP0 */
444 + #define D_EPX(dev, args...) printk(args)
446 + #define D_EPX(dev, args...) do {} while (0)
447 +#endif /* DEBUG_EP0 */
450 + #define D_ERR(dev, args...) printk(args)
452 + #define D_ERR(dev, args...) do {} while (0)
456 + #define D_REQ(dev, args...) do {} while (0)
457 + #define D_TRX(dev, args...) do {} while (0)
458 + #define D_INI(dev, args...) do {} while (0)
459 + #define D_EP0(dev, args...) do {} while (0)
460 + #define D_EPX(dev, args...) do {} while (0)
461 + #define dump_ep_intr(x, y, z, i) do {} while (0)
462 + #define dump_intr(x, y, z) do {} while (0)
463 + #define dump_ep_stat(x, y) do {} while (0)
464 + #define dump_usb_stat(x, y) do {} while (0)
465 + #define dump_req(x, y, z) do {} while (0)
466 + #define D_ERR(dev, args...) do {} while (0)
469 +#endif /* __LINUX_USB_GADGET_RT_UDC_H */
470 diff --git a/drivers/usb/gadget/rt_udc_pdma.c b/drivers/usb/gadget/rt_udc_pdma.c
472 index 0000000..d5b89a2
474 +++ b/drivers/usb/gadget/rt_udc_pdma.c
477 + * driver/usb/gadget/rt_udc.c
479 + * Copyright (C) 2009 Ying Yuan Huang, Ralink Tech. <yyhuang@ralink_tech.com>
481 + * This program is free software; you can redistribute it and/or modify
482 + * it under the terms of the GNU General Public License as published by
483 + * the Free Software Foundation; either version 2 of the License, or
484 + * (at your option) any later version.
486 + * This program is distributed in the hope that it will be useful,
487 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
488 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
489 + * GNU General Public License for more details.
493 + * 1) [ USB composite device ]. The USB PDMA architecture is not suitable for USB composite
494 + * device support. A passive gadget driver(device) may slow down or block other gadget
495 + * (device) because they are in the same ring.
497 +#include <linux/init.h>
498 +#include <linux/kernel.h>
499 +#include <linux/platform_device.h>
500 +#include <linux/module.h>
501 +#include <linux/errno.h>
502 +#include <linux/list.h>
503 +#include <linux/interrupt.h>
504 +#include <linux/io.h>
505 +#include <linux/irq.h>
506 +#include <linux/device.h>
507 +#include <linux/dma-mapping.h>
508 +#include <linux/clk.h>
509 +#include <linux/delay.h>
510 +#include <linux/timer.h>
511 +#include <linux/proc_fs.h>
512 +#include <linux/usb/ch9.h>
513 +#include <linux/version.h>
514 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
515 +#include <linux/usb_gadget.h>
517 +#include <linux/usb/gadget.h>
520 +static const char driver_name[] = "rt_udc";
521 +static const char ep0name[] = "ep0";
522 +static unsigned debuglevel = 0;
523 +module_param (debuglevel, uint, S_IRUGO);
528 +#define PROC_DIR driver_name
529 +#define DEBUGLEVEL_PROCFILE "debuglevel"
530 +static struct proc_dir_entry *pProcDir = NULL;
531 +static struct proc_dir_entry *pProcDebugLevel = NULL;
536 +#define NUM_RX_DESC 256
537 +#define NUM_TX_DESC 256
538 +#define RX_BUFF_SZ 1600 /* 1536 */
539 +#define RING_RESET_TIMEOUT 3000 /* 3 secs */
540 +#define RX_RESCHEDULE 64
541 +#define TX_RESCHEDULE 4
542 +static unsigned dma = 0;
543 +module_param (dma, uint, S_IRUGO);
544 +static unsigned sm = 0;
545 +module_param (sm, uint, S_IRUGO);
546 +static unsigned int TXMAXCAP = 512;
547 +module_param (TXMAXCAP, uint, S_IRUGO);
549 +static struct PDMA_txdesc *tx_ring0_cache = NULL;
550 +static struct PDMA_rxdesc *rx_ring0_cache = NULL;
551 +static volatile struct PDMA_rxdesc *rx_ring0_noncache = NULL;
552 +static volatile struct PDMA_txdesc *tx_ring0_noncache = NULL;
553 +static dma_addr_t tx_ring_bus_addr;
554 +static dma_addr_t rx_ring_bus_addr;
556 +static int rx_dma_owner_idx0; /* Point to the next RXD DMA wants to use in RXD Ring#0. */
557 +static int tx_cpu_owner_idx0;
558 +static int tx_need_free_idx0;
560 +static volatile unsigned char *USBRxPackets[NUM_RX_DESC]; /* Receive packets */
561 +static unsigned char tx_zlp_dummy_buf[8];
562 +struct tasklet_struct rx_dma_tasklet;
563 +struct tasklet_struct tx_dma_tasklet;
565 +static struct rt_udc_struct controller;
566 +static struct rt_request *handle_outep(struct rt_ep_struct *rt_ep);
568 +static int debuglevel_read(char *page, char **start, off_t off,int count, int *eof, void *data)
571 + sprintf(page, "%d\n", debuglevel);
572 + len = strlen(page) + 1;
577 +static int debuglevel_write(struct file *file, const char *buffer, unsigned long count, void *data)
580 + count = (count > 32) ? 32 : count;
581 + memset(tmp, 0, 32);
582 + if (copy_from_user(tmp, buffer, count))
584 + debuglevel = simple_strtol(tmp, 0, 10);
588 +static void ep0_chg_stat(const char *label, struct rt_udc_struct *rt_usb, enum ep0_state stat)
590 + xprintk("<0st>%s->%s\n", state_name[rt_usb->ep0state], state_name[stat]);
592 + if (rt_usb->ep0state == stat)
594 + rt_usb->ep0state = stat;
597 +static u8 read_epcs(struct rt_ep_struct *rt_ep)
599 + int idx = EP_NO(rt_ep);
600 + int dir = EP_DIR(rt_ep);
603 + return usb_read(EP0CS);
605 + return (dir == EP_IN ? usb_read(0x7 + idx*8) : usb_read(0x3 + idx*8) );
608 +static void write_epcs(struct rt_ep_struct *rt_ep, u8 val)
610 + int idx = EP_NO(rt_ep);
611 + int dir = EP_DIR(rt_ep);
614 + usb_write(EP0CS, val);
616 + (dir == EP_IN ? /*IN */ usb_write(0x7 + idx*8, val) : usb_write(0x3 + idx*8, val) );
619 +static u16 read_inbc(int epnum)
622 + if(epnum == 0){ /* EP0 */
623 + low = usb_read(IN0BC);
625 + low = usb_read(epnum * 8 + 4);
626 + high = usb_read((epnum * 8 + 4)+1);
628 + return (low | (high << 8));
631 +static u16 read_outbc(int epnum)
634 + if(epnum == 0){ /* EP0 */
635 + low = usb_read(OUT0BC);
637 + low = usb_read(epnum * 8);
638 + high = usb_read((epnum * 8)+1);
640 + return (low | (high << 8));
644 +static void rt_all_eps_reset(void)
646 + // reset(toggle & fifo) all 16 IN & 16 OUT endpoints
647 + usb_write(ENDPRST, 0x10);
648 + usb_write(ENDPRST, 0x70);
649 + usb_write(ENDPRST, 0x00);
650 + usb_write(ENDPRST, 0x60);
653 +static void rt_ep_rst(struct rt_ep_struct *rt_ep)
656 + u8 idx = EP_NO(rt_ep);
657 + u8 dir = EP_DIR(rt_ep);
659 + reg |= ENDPRST_IO | idx;
660 + usb_write(ENDPRST, reg);
662 + reg |= ENDPRST_TOGRST | ENDPRST_FIFORST;
663 + usb_write(ENDPRST, reg);
666 +static void rt_ep_irq_enable(struct rt_ep_struct *rt_ep)
669 + u8 idx = EP_NO(rt_ep);
670 + u8 dir = EP_DIR(rt_ep);
672 + if(idx == 0 /* ep0 */){
673 + usb_write(IN07IEN, (usb_read(IN07IEN) | 0x1) );
674 + usb_write(OUT07IEN, (usb_read(OUT07IEN) | 0x1) );
676 + reg = usb_read(dir ? IN07IEN : OUT07IEN);
677 + reg = reg | (0x1 << idx);
678 + usb_write(dir == EP_IN ? IN07IEN : OUT07IEN, reg);
679 + reg = usb_read(dir ? IN07IEN : OUT07IEN);
683 +static void rt_udc_init_ep(struct rt_udc_struct *rt_usb)
687 +#if defined (CONFIG_RALINK_RT3883) || defined (CONFIG_RALINK_RT3352) || defined (CONFIG_RALINK_MT7620)
688 + usb_write(IN1CON, 0x8D); // InEP1 : Int, 2 subfifos
689 + usb_write(IN2CON, 0x89); // InEP2 : Bulk, 2 subfifos
690 + usb_write(OUT1CON, 0x8D); // OutEP1 : Int, 2 subfifos
691 + usb_write(OUT2CON, 0x89); // OutEP2 : Bulk, 2 subfifos
692 + //usb_write(OUT3CON, 0x89); // OutEP3 : Bulk, 2 subfifos
693 + //usb_write(OUT4CON, 0x89); // OutEP4 : Bulk, 2 subfifos
694 +#elif defined (CONFIG_RALINK_RT5350)
695 + usb_write(IN1CON, 0x89); // InEP1 : BULK, 2 subfifos
696 + usb_write(OUT1CON, 0x89); // OutEP1 : BULK, 2 subfifos
698 +#error "define a platform"
701 +#if defined (CONFIG_RALINK_RT3883) || defined (CONFIG_RALINK_RT3352) || defined (CONFIG_RALINK_MT7620)
702 + usb_write(IN1CON, 0x8C); // InEP1 : Int , 1 subfifos
703 + usb_write(IN2CON, 0x88); // InEP2 : Bulk, 1 subfifo
704 + usb_write(OUT1CON, 0x8C); // OutEP1 : Int, 1 subfifos
705 + usb_write(OUT2CON, 0x88); // OutEP2 : Bulk, 1 subfifos
706 + //usb_write(OUT3CON, 0x88); // OutEP3 : Bulk, 1 subfifo
707 + //usb_write(OUT4CON, 0x88); // OutEP4 : Bulk. 1 subfifo
709 +#elif defined (CONFIG_RALINK_RT5350)
710 + usb_write(IN1CON, 0x88); // InEP1 : BULK , 1 subfifos
711 + usb_write(OUT1CON, 0x88); // OutEP1 : BULK, 1 subfifos
713 +#error "define a platform"
716 + // clear all pending HW interrupts
717 + usb_write(IN07IRQ, 0xFF);
718 + usb_write(OUT07IRQ, 0xFF);
719 + rt_all_eps_reset();
720 + rt_ep_irq_enable(&rt_usb->rt_ep[0]);
723 +static void rt_udc_init_fifo(struct rt_udc_struct *rt_usb)
727 + usb_write(FIFOCTRL, 0x31); // INEP1, Autoin = 1
728 + usb_write(FIFOCTRL, 0x32); // INEP2, Autoin = 1
729 + usb_write(FIFOCTRL, 0x21); // OUTEP1, Autoin = 1
730 + usb_write(FIFOCTRL, 0x22); // OUTEP2, Autoin = 1
731 + //usb_write(FIFOCTRL, 0x23);// OUTEP3, Autoin = 1
732 + //usb_write(FIFOCTRL, 0x24);// OUTEP4, Autoin = 1
734 + usb_write(FIFOCTRL, 0x00); // Access by DMA
736 + usb_write(FIFOCTRL, 0x11); // INEP1, Autoin = 0
737 + usb_write(FIFOCTRL, 0x12); // INEP2, Autoin = 0
738 + usb_write(FIFOCTRL, 0x01); // OUTEP1, Autoin = 0
739 + usb_write(FIFOCTRL, 0x02); // OUTEP2, Autoin = 0
740 + //usb_write(FIFOCTRL, 0x03);// OUTEP3, Autoin = 0
741 + //usb_write(FIFOCTRL, 0x04);// OUTEP4, Autoin = 0
743 + usb_write(FIFOCTRL, 0x80); // Access by CPU
747 +static void rt_udc_init(struct rt_udc_struct *rt_usb)
749 + /* Setup & Init endpoints */
750 + rt_udc_init_ep(rt_usb);
752 + // Enable HS, reset, suspend, SETUP valid data interrupt
753 + usb_write(USBIRQ, 0xff); // clear first
754 + usb_write(USBIEN, 0x21);
756 + /* Setup ep fifos */
757 + rt_udc_init_fifo(rt_usb);
760 +static void rt_ep_irq_disable(struct rt_ep_struct *rt_ep)
763 + u8 idx = EP_NO(rt_ep);
764 + u8 dir = EP_DIR(rt_ep);
766 + if(idx == 0 /* ep0 */){
767 + usb_write(IN07IEN, (usb_read(IN07IEN) & ~(0x1)) );
768 + usb_write(OUT07IEN, (usb_read(OUT07IEN) & ~(0x1)) );
770 + reg = usb_read(dir ? IN07IEN : OUT07IEN);
771 + reg = reg & ~(0x1 << idx);
772 + usb_write(dir == EP_IN ? IN07IEN : OUT07IEN, reg);
773 + reg = usb_read(dir ? IN07IEN : OUT07IEN);
777 +static u32 rt_fifo_bcount(struct rt_ep_struct *rt_ep)
782 + int idx = EP_NO(rt_ep);
783 + int dir = EP_DIR(rt_ep);
789 + low = usb_read(0x004 + idx*8);
790 + high = usb_read( (0x004 + idx*8)+1 );
792 + low = usb_read(0x000 + idx*8);
793 + high = usb_read( (0x000 + idx*8)+1 );
799 +void rt_flush(struct rt_ep_struct *rt_ep)
804 +void rt_ep_stall(struct rt_ep_struct *rt_ep, int value)
808 + int idx = EP_NO(rt_ep);
809 + int dir = EP_DIR(rt_ep);
812 + tmp = usb_read(EP0CS);
817 + usb_write(EP0CS, tmp);
819 + addr = (dir == EP_IN ? 0x006 : 0x002) + idx * 8;
820 + tmp = usb_read(addr);
825 + usb_write(addr, tmp);
831 +static int rt_udc_get_frame(struct usb_gadget *_gadget)
836 +static int rt_udc_wakeup(struct usb_gadget *_gadget)
843 +/*******************************************************************************
844 + * USB request control functions
845 + *******************************************************************************
847 +static inline void ep_add_request(struct rt_ep_struct *rt_ep, struct rt_request *req)
849 + if (unlikely(!req))
852 + req->zlp_dma_done = 0;
853 + req->rt_ep = rt_ep;
854 + list_add_tail(&req->queue, &rt_ep->queue);
857 +static inline void ep_del_request(struct rt_ep_struct *rt_ep, struct rt_request *req)
859 + if (unlikely(!req))
861 + list_del_init(&req->queue);
862 + req->zlp_dma_done = 0;
866 +static void done(struct rt_ep_struct *rt_ep, struct rt_request *req, int status)
868 + ep_del_request(rt_ep, req);
870 + if (likely(req->req.status == -EINPROGRESS))
871 + req->req.status = status;
873 + status = req->req.status;
875 + if (status && status != -ESHUTDOWN)
876 + D_ERR(rt_ep->rt_usb->dev, "<%s> complete %s req %p stat %d len %u/%u\n", __func__, rt_ep->ep.name, &req->req, status,req->req.actual, req->req.length);
878 + req->req.complete(&rt_ep->ep, &req->req);
883 +struct tasklet_struct rx_tasklet_tmp;
884 +static void rx_done_do_tasklet(unsigned long arg)
886 + struct rt_ep_struct *rt_ep;
887 + struct rt_request *rt_req;
888 + struct usb_request *usb_req;
890 + int i, rx_count, status = 0;
891 + struct rt_udc_struct *rt_usb = &controller;
893 + for (i = (IN_EP_NUM+1); i < RT_USB_NB_EP; i++) {
894 + rt_ep = &rt_usb->rt_ep[i];
897 + // shared by irq handler, protect it
898 + spin_lock_irqsave(&rx_done_lock, rx_done_lock_flags);
899 + rx_count = rt_ep->rx_done_count;
901 + //spin_unlock_irqrestore(&rx_done_lock, rx_done_lock_flags);
903 + for (;rx_count > 0; rx_count--) {
904 + if(unlikely(list_empty(&rt_ep->queue)))
905 + FATAL_ERROR("empty queue");
907 + rt_req = list_entry(rt_ep->queue.next, struct rt_request, queue);
908 + usb_req = &rt_req->req;
910 + ep_del_request(rt_ep, rt_req);
911 + rt_ep->rx_done_count--;
913 + spin_unlock_irqrestore(&rx_done_lock, rx_done_lock_flags);
915 + if (unlikely(rt_req->req.status == -EINPROGRESS))
916 + rt_req->req.status = status;
918 + status = rt_req->req.status;
920 + if (unlikely(status && status != -ESHUTDOWN))
921 + D_ERR(rt_ep->rt_usb->dev, "<%s> complete %s req %p stat %d len %u/%u\n", __func__, rt_ep->ep.name, &rt_req->req, status,rt_req->req.actual, rt_req->req.length);
923 + // indicate gadget driver.
924 + usb_req->complete(ep, usb_req);
926 + spin_lock_irqsave(&rx_done_lock, rx_done_lock_flags);
928 + spin_unlock_irqrestore(&rx_done_lock, rx_done_lock_flags);
933 +struct tasklet_struct tx_tasklet;
934 +static void tx_do_tasklet(unsigned long arg)
939 +struct tasklet_struct rx_tasklet;
940 +static void rx_do_tasklet(unsigned long arg)
942 + struct rt_ep_struct *rt_ep;
943 + struct rt_request *req;
946 + struct rt_udc_struct *rt_usb = &controller;
948 + for (i = (IN_EP_NUM+1/* EP0 */); i < RT_USB_NB_EP; i++){
950 + rt_ep = &rt_usb->rt_ep[i];
953 + epcs = read_epcs(rt_ep);
954 + while(!(epcs & EP_CS_BSY)){
955 + req = handle_outep(rt_ep);
957 + // No usb request found.
958 + // Just set up the flag (pending) and clear int.
959 + rt_ep->pending = 1;
962 + if(req && ( (req->req.actual % rt_ep->ep.maxpacket) || (req->req.actual >= req->req.length))){
963 + xprintk("q.l=%d,q.a=%d\n", req->req.length, req->req.actual);
964 + done(rt_ep, req, 0);
968 + epcs = read_epcs(rt_ep);
969 + write_epcs(rt_ep, 0x0);
970 + epcs = read_epcs(rt_ep);
975 +static void nuke(struct rt_ep_struct *rt_ep, int status)
977 + struct rt_request *req;
980 + while (!list_empty(&rt_ep->queue)) {
981 + req = list_entry(rt_ep->queue.next, struct rt_request, queue);
982 + done(rt_ep, req, status);
987 + *******************************************************************************
988 + * Data tansfer over USB functions
989 + *******************************************************************************
991 +static int read_ep0_fifo(struct rt_ep_struct *rt_ep, struct rt_request *req)
994 + int byte_count, req_bufferspace, count, i;
998 + FATAL_ERROR("not irq context.");
1000 + byte_count = read_outbc(EP_NO(rt_ep));
1001 + req_bufferspace = req->req.length - req->req.actual;
1003 + buf = req->req.buf + req->req.actual;
1005 + if(!req_bufferspace)
1006 + FATAL_ERROR("zlp");
1008 + if(byte_count > req_bufferspace)
1009 + FATAL_ERROR("buffer overflow, byte_count=%d, req->req.length=%d, req->req.actual=%d\n", byte_count, req->req.length ,req->req.actual);
1011 + count = min(byte_count, req_bufferspace);
1013 + //test, Access by CPU
1015 + usb_write(FIFOCTRL, 0x80);
1017 + for (i = 0; i < count; i++){
1018 + *buf = usb_read(EP0OUTDAT+i);
1021 + req->req.actual += count;
1023 + //test, Access by DMA
1025 + usb_write(FIFOCTRL, 0x00);
1031 +static int read_ep_fifo(struct rt_ep_struct *rt_ep, struct rt_request *req)
1033 + u8 *buf, ep_no, ep_no_shift;
1034 + int byte_count, req_bufferspace, count, i;
1037 + ep_no = EP_NO(rt_ep);
1039 + byte_count = read_outbc(ep_no);
1040 + if(unlikely(!byte_count))
1041 + FATAL_ERROR("ep_no:%d bc = 0", ep_no);
1043 + req_bufferspace = req->req.length - req->req.actual;
1045 + buf = req->req.buf + req->req.actual;
1047 + if(unlikely(!req_bufferspace))
1048 + FATAL_ERROR("zlp");
1050 + xprintk("bc=%d,r.l=%d,r.a=%d\n", byte_count, req->req.length ,req->req.actual);
1051 + if(unlikely(byte_count > req_bufferspace))
1052 + FATAL_ERROR("buffer overflow, byte_count=%d, req->req.length=%d, req->req.actual=%d\n", byte_count, req->req.length ,req->req.actual);
1054 + count = min(byte_count, req_bufferspace);
1056 + ep_no_shift = 0x80+ep_no * 4;
1057 + for (i = 0; i < count; i++){
1058 + *buf = usb_read(ep_no_shift);
1062 + req->req.actual += count;
1064 + // EP Out irq handler would arm another transaction.
1068 +static int write_ep_fifo_zlp(struct rt_ep_struct *rt_ep)
1071 + int ep_no = EP_NO(rt_ep);
1074 + xprintk("w%d ZLP\n", EP_NO(rt_ep));
1075 + epcs = read_epcs(rt_ep);
1076 + if(epcs & EP_CS_BSY)
1077 + FATAL_ERROR("EP%d busy. cs=%x\n", ep_no, epcs);
1079 + /* check INEP byte count is zero? */
1080 + if(read_inbc(ep_no))
1081 + FATAL_ERROR("EP%d bc zero. bc=%d\n", ep_no, read_inbc(ep_no));
1083 + epcs = read_epcs(rt_ep);
1084 + write_epcs(rt_ep, epcs);
1090 + * handle_epinirq()
1092 +static int write_ep_fifo(struct rt_ep_struct *rt_ep, struct rt_request *req)
1095 + int length, i, ep_no = EP_NO(rt_ep);
1098 + xprintk("w ep%d req=%p,r.l=%d,r.a=%d\n",EP_NO(rt_ep),&req->req,req->req.length,req->req.actual);
1099 + epcs = read_epcs(rt_ep);
1100 + if(epcs & EP_CS_BSY)
1101 + FATAL_ERROR("EP%d busy. epcs=%x\n", ep_no, epcs);
1103 + /* check INEP byte count is zero? */
1104 + if(read_inbc(ep_no))
1105 + FATAL_ERROR("EP%d bc=%d\n", ep_no, read_inbc(ep_no));
1107 + buf = req->req.buf + req->req.actual;
1108 + length = (req->req.length - req->req.actual) < rt_ep->ep.maxpacket ? (req->req.length - req->req.actual) : rt_ep->ep.maxpacket;
1109 + req->req.actual += length;
1110 + if (!length) { /* zlp */
1112 + xprintk("<%s> zero packet\n", __func__);
1113 + write_ep_fifo_zlp(rt_ep);
1117 + // write to ep in fifo
1118 + for (i=0; i< length; i++)
1119 + usb_write(0x80+ep_no*4, *buf++);
1121 + epcs = read_epcs(rt_ep);
1122 + write_epcs(rt_ep, epcs);
1130 +static int write_ep0_fifo(struct rt_ep_struct *rt_ep, struct rt_request *req)
1137 + xprintk("q.l=%d, q.a=%d, maxp=%d\n", req->req.length, req->req.actual, rt_ep->ep.maxpacket);
1139 + buf = req->req.buf + req->req.actual;
1140 + maxpacket = (u32)(rt_ep->ep.maxpacket);
1141 + length = min(req->req.length - req->req.actual, maxpacket);
1143 + req->req.actual += length;
1145 + if (!length && req->req.zero)
1146 + FATAL_ERROR("zlp");
1149 + FATAL_ERROR("Not in irq context");
1151 + //test, Access by CPU
1153 + usb_write(FIFOCTRL, 0x80);
1155 + //write to ep0in fifo
1156 + for (i=0; i< length; i++)
1157 + usb_write(EP0INDAT+i, *buf++);
1160 + usb_write(IN0BC, length);
1161 + if(length != rt_ep->ep.maxpacket)
1162 + usb_write(EP0CS, 0x2); // clear NAK bit to ACK host.
1164 + //test, Access by CPU
1166 + usb_write(FIFOCTRL, 0x00);
1171 +static struct rt_request *get_unhandled_req(struct rt_ep_struct *rt_ep)
1173 + struct list_head *p;
1174 + struct rt_request *req = NULL;
1176 + if(EP_DIR(rt_ep) == EP_OUT)
1177 + FATAL_ERROR("Out EP");
1180 + list_for_each(p, &rt_ep->queue){
1181 + req = list_entry(p, struct rt_request, queue);
1182 + if(req->req.length > req->req.actual )
1184 + else if(unlikely(req->req.length == 0 && req->zlp_dma_done == 0))
1191 + if (!list_empty(&rt_ep->queue)){
1192 + req = list_entry(rt_ep->queue.next, struct rt_request, queue);
1194 + FATAL_ERROR("%s No request", rt_ep->ep.name);
1200 +#define PADDING_LENGTH 64
1201 +static int write_dma_txring(struct rt_ep_struct *rt_ep,struct rt_request *req)
1205 + int retry_times = 0;
1206 + u32 hw_current_idx;
1208 + xprintk("w%dr=%p,r.l=%d,r.a=%d\n", EP_NO(rt_ep), &req->req,req->req.length,req->req.actual);
1210 + length = req->req.length;
1212 + while(length > 0 || (req->req.length == 0 && req->zlp_dma_done == 0)){
1214 + /* wait for a free TXD */
1215 + hw_current_idx = reg_read(RTUSB_TX_DTX_IDX0);
1216 + if ( tx_ring0_cache[tx_cpu_owner_idx0].txd_info2.DDONE_bit == 0 ||
1217 + ((tx_cpu_owner_idx0+1) % NUM_TX_DESC == hw_current_idx) ) {
1218 + if(retry_times > 1000)
1225 + if(length > TXMAXCAP)
1226 + length = TXMAXCAP;
1228 + buf = req->req.buf + req->req.actual;
1229 + req->req.actual += length;
1231 + /* deal with ZLP.*/
1232 + if(req->req.length == 0 && req->zlp_dma_done == 0)
1233 + req->zlp_dma_done = 1;
1237 +#define phys_to_bus(a) ((u32)a & 0x1FFFFFFF)
1239 + tx_ring0_cache[tx_cpu_owner_idx0].txd_info1.SDP0 = cpu_to_le32(phys_to_bus(buf));
1240 + tx_ring0_cache[tx_cpu_owner_idx0].txd_info2.SDL0 = cpu_to_le32(length);
1241 + tx_ring0_cache[tx_cpu_owner_idx0].txd_info4.zlp_flag = 0;
1242 + dma_cache_sync(NULL, (void *)buf, length, DMA_TO_DEVICE);
1244 + tx_ring0_cache[tx_cpu_owner_idx0].txd_info1.SDP0 = cpu_to_le32(phys_to_bus(tx_zlp_dummy_buf));
1245 + tx_ring0_cache[tx_cpu_owner_idx0].txd_info2.SDL0 = cpu_to_le32(sizeof(tx_zlp_dummy_buf));
1246 + tx_ring0_cache[tx_cpu_owner_idx0].txd_info4.zlp_flag = 1;
1249 + tx_ring0_cache[tx_cpu_owner_idx0].txd_info4.In_ep_addr = cpu_to_le32(EP_NO(rt_ep));
1250 + tx_ring0_cache[tx_cpu_owner_idx0].txd_info2.DDONE_bit = 0;
1251 + tx_cpu_owner_idx0 = (tx_cpu_owner_idx0 + 1) % NUM_TX_DESC;
1253 + length = req->req.length - req->req.actual;
1256 + reg_write(RTUSB_TX_CTX_IDX0, tx_cpu_owner_idx0);
1262 +/*******************************************************************************
1263 + * Endpoint handlers
1264 + *******************************************************************************
1268 + * Handle In Endpoint
1270 + * Enqueue -> Write fifo -> TX_DONE -> Write fifo -> TX_DONE -> ..
1273 + * Enqueue -> Kick off TxD. Enqueue -> Kick off TxD. Enqueue -> Kick off TxD.
1275 +static int handle_inep(struct rt_ep_struct *rt_ep)
1277 + struct rt_request *req;
1280 + if(!(req = get_unhandled_req(rt_ep)))
1284 + write_dma_txring(rt_ep, req);
1286 + write_ep_fifo(rt_ep, req);
1287 + rt_ep->tx_done_count = 1;
1295 +static struct rt_request *handle_outep(struct rt_ep_struct *rt_ep)
1297 + struct rt_request *req;
1298 + struct list_head *p;
1302 + if (list_empty(&rt_ep->queue)){
1306 + list_for_each(p, &rt_ep->queue){
1307 + if(count != rt_ep->rx_done_count){
1311 + req = list_entry(p, struct rt_request, queue);
1312 + read_ep_fifo(rt_ep, req);
1319 +static struct rt_request *handle_inep0(struct rt_ep_struct *rt_ep)
1321 + struct rt_request *req = NULL;
1324 + if (list_empty(&rt_ep->queue)) {
1325 + D_ERR(rt_ep->rt_usb->dev, "<%s> no request on %s\n", __func__, rt_ep->ep.name);
1329 + req = list_entry(rt_ep->queue.next, struct rt_request, queue);
1330 + switch (rt_ep->rt_usb->ep0state) {
1331 + case EP0_IN_DATA_PHASE: /* GET_DESCRIPTOR */
1332 + write_ep0_fifo(rt_ep, req);
1336 + //case EP0_OUT_DATA_PHASE: /* SET_DESCRIPTOR */
1337 + //case EP0_NO_DATA_PHASE: /* for no data stage control transfer */
1340 + D_ERR(rt_ep->rt_usb->dev, "<%s> ep0 i/o, odd state %d\n", __func__, rt_ep->rt_usb->ep0state);
1341 + ep_del_request(rt_ep, req);
1349 +static struct rt_request *handle_outep0(struct rt_ep_struct *rt_ep)
1351 + struct rt_request *req = NULL;
1354 + if (list_empty(&rt_ep->queue)) {
1355 + D_ERR(rt_ep->rt_usb->dev, "<%s> no request on %s\n", __func__, rt_ep->ep.name);
1359 + if(rt_ep->rt_usb->ep0state != EP0_OUT_DATA_PHASE){
1360 + D_EP0(rt_ep->rt_usb->dev, "<%s> ep0 i/o, odd state %d\n", __func__, rt_ep->rt_usb->ep0state);
1361 + ep_del_request(rt_ep, req);
1365 + req = list_entry(rt_ep->queue.next, struct rt_request, queue);
1367 + read_ep0_fifo(rt_ep, req);
1372 +/*******************************************************************************
1373 + * USB gadget callback functions
1374 + *******************************************************************************
1376 +static void handle_dma_rxdone(struct rt_udc_struct *rt_usb)
1379 + tasklet_schedule(&rx_dma_tasklet);
1382 +static void handle_dma_txdone(struct rt_udc_struct *rt_usb)
1385 + tasklet_schedule(&tx_dma_tasklet);
1388 +static void handle_dmairq(struct rt_udc_struct *rt_usb, u32 irq)
1390 + if(irq & RTUSB_RX_DONE_INT0){
1391 + handle_dma_rxdone(rt_usb);
1394 + if(irq & RTUSB_TX_DONE_INT0){
1395 + handle_dma_txdone(rt_usb);
1398 + reg_write(RTUSB_INT_STATUS, irq);
1401 +static inline int udc_dma_reset_txring(void)
1406 + while(count++< RING_RESET_TIMEOUT){
1407 + reg = reg_read(RTUSB_PDMA_GLO_CFG);
1408 + if(reg & RTUSB_TX_DMA_BUSY){
1414 + if(count== RING_RESET_TIMEOUT)
1417 + reg = reg_read(RTUSB_PDMA_RST_IDX);
1419 + reg |= (RTUSB_RST_DTX_IDX1 | RTUSB_RST_DTX_IDX0);
1420 + reg_write(RTUSB_PDMA_RST_IDX, reg);
1425 +static inline int udc_dma_reset_rxring(void)
1430 + while(count++< RING_RESET_TIMEOUT){
1431 + reg = reg_read(RTUSB_PDMA_GLO_CFG);
1432 + if(reg & RTUSB_RX_DMA_BUSY){
1437 + if(count== RING_RESET_TIMEOUT)
1440 + reg = reg_read(RTUSB_PDMA_RST_IDX);
1442 + reg |= (RTUSB_RST_DRX_IDX1 | RTUSB_RST_DRX_IDX0);
1443 + reg_write(RTUSB_PDMA_RST_IDX, reg);
1448 +static int udc_dma_hw_reset(void)
1450 + if(udc_dma_reset_rxring() == -1)
1452 + if(udc_dma_reset_txring() == -1)
1457 +static void udc_dma_enable(int enable)
1460 + reg = reg_read(RTUSB_PDMA_GLO_CFG);
1463 + reg |= RTUSB_TX_WB_DDONE | RTUSB_RX_DMA_EN | RTUSB_TX_DMA_EN ;
1465 + reg &= ~(RTUSB_TX_WB_DDONE | RTUSB_RX_DMA_EN | RTUSB_TX_DMA_EN) ;
1466 + reg_write(RTUSB_PDMA_GLO_CFG, reg);
1470 +static void udc_dma_int_enable(int enable)
1473 + reg = reg_read(RTUSB_INT_MASK);
1476 + reg |= RTUSB_RX_DONE_INT_MSK0 | RTUSB_TX_DONE_INT_MSK0 ;
1478 + reg &= ~(RTUSB_RX_DONE_INT_MSK0 | RTUSB_TX_DONE_INT_MSK0) ;
1479 + reg_write(RTUSB_INT_MASK, reg);
1483 +static inline void udc_dma_tx_int_clear(void)
1485 + reg_write(RTUSB_INT_STATUS, 0x0000000F);
1488 +static inline void udc_dma_rx_int_clear(void)
1490 + reg_write(RTUSB_INT_STATUS, 0x00030000);
1493 +static inline void udc_dma_all_int_clear(void)
1495 + reg_write(RTUSB_INT_STATUS, 0xFFFFFFFF);
1498 +static int copy_data_to_ep(void *src, int length, int ep_num)
1500 + struct rt_ep_struct *rt_ep;
1501 + struct rt_udc_struct *rt_usb = &controller;
1502 + struct rt_request *req;
1503 + int req_bufferspace, count;
1507 + rt_ep = &rt_usb->rt_ep[ep_num+IN_EP_NUM];
1509 + if (list_empty(&rt_ep->queue)){
1510 + /* It is safe to return 0 if no req queued. */
1514 + req = list_entry(rt_ep->queue.next, struct rt_request, queue);
1515 + req_bufferspace = req->req.length - req->req.actual;
1517 + if(unlikely(!req_bufferspace)){
1519 + FATAL_ERROR("zlp");
1523 + if(length > req_bufferspace){
1524 + FATAL_ERROR("buffer overflow");
1528 + // sync with cache.
1529 + if(likely(length))
1530 + dma_cache_sync(NULL, src, length, DMA_FROM_DEVICE);
1532 + buf = req->req.buf + req->req.actual;
1533 + count = min(length, req_bufferspace);
1534 + memcpy(buf, src, count);
1536 + req->req.actual += count;
1538 + if((req->req.actual % rt_ep->ep.maxpacket) || (req->req.actual >= req->req.length)){
1539 + done(rt_ep, req, 0); // short packet indicates transaction is done.
1544 +static void rx_dma_done_do_tasklet(unsigned long arg)
1549 + int processed_count=0;
1553 + if (rx_ring0_cache[rx_dma_owner_idx0].rxd_info2.DDONE_bit == 0)
1556 + if(processed_count++ > RX_RESCHEDULE){
1557 + tasklet_schedule(&rx_dma_tasklet);
1561 + length = rx_ring0_cache[rx_dma_owner_idx0].rxd_info4.Rx_bcnt;
1562 + ep = rx_ring0_cache[rx_dma_owner_idx0].rxd_info4.Out_ep_addr;
1564 + // copy data from RXD->buffer to ep queue.
1565 + rc = copy_data_to_ep((void *)USBRxPackets[rx_dma_owner_idx0], length, ep);
1569 + rxd_info = (u32 *)&rx_ring0_cache[rx_dma_owner_idx0].rxd_info4;
1572 + /* clear DDONE bit*/
1573 + rxd_info = (u32 *)&rx_ring0_cache[rx_dma_owner_idx0].rxd_info2;
1575 + //rx_ring0_cache[rx_dma_owner_idx0].rxd_info2.DDONE_bit = 0;
1576 + //rx_ring0_cache[i].rxd_info2.LS0= 0;
1577 + rx_ring0_cache[rx_dma_owner_idx0].rxd_info2.PLEN0= sizeof(u8) * RX_BUFF_SZ;
1579 + /* Move point to next RXD which wants to alloc */
1580 + //OUTL(cpu_to_le32((u32) rx_dma_owner_idx0), RTUSB_RX_CALC_IDX0);
1581 + reg_write(RTUSB_RX_CALC_IDX0, rx_dma_owner_idx0);
1583 + /* Update to Next packet point that was received.
1585 + rx_dma_owner_idx0 = (rx_dma_owner_idx0 + 1) % NUM_RX_DESC;
1590 + * Recycle reqs and call gadget complete callback function.
1592 +static void tx_dma_done_do_tasklet(unsigned long arg)
1596 + struct rt_ep_struct *rt_ep;
1597 + struct rt_request *rt_req;
1598 + struct rt_udc_struct *rt_usb = &controller;
1601 + while(tx_need_free_idx0 != (hw_current = reg_read(RTUSB_TX_DTX_IDX0))){
1603 + while(tx_ring0_cache[tx_need_free_idx0].txd_info2.DDONE_bit != 1){
1607 + FATAL_ERROR("tx timeout");
1610 + // rt_ep = tx_ring0_req_mapping[tx_need_free_idx0];
1611 + ep_num = tx_ring0_cache[tx_need_free_idx0].txd_info4.In_ep_addr;
1612 + if(!ep_num || ep_num > IN_EP_NUM)
1613 + FATAL_ERROR("Out of range");
1615 + rt_ep = &rt_usb->rt_ep[ep_num];
1616 + if(list_empty(&rt_ep->queue))
1617 + FATAL_ERROR("ep[%d] No request", ep_num);
1619 + rt_req = list_entry(rt_ep->queue.next, struct rt_request, queue);
1620 + rt_req->txd_count--;
1623 + if(rt_req->txd_count == 0)
1624 + done(rt_ep, rt_req, 0);
1626 + tx_need_free_idx0 = (tx_need_free_idx0 + 1) % NUM_TX_DESC;
1631 +static int udc_dma_rst(void)
1633 + if( udc_dma_reset_txring() == -1)
1635 + if( udc_dma_reset_rxring() == -1)
1638 + tx_cpu_owner_idx0 = 0;
1639 + tx_need_free_idx0 = 0;
1640 + rx_dma_owner_idx0 = 0;
1641 + reg_write(RTUSB_RX_CALC_IDX0, cpu_to_le32(NUM_RX_DESC - 1));
1645 +static int rt_udc_dma_init(void)
1649 + if( udc_dma_hw_reset() == -1)
1652 + for(i=0; i<NUM_RX_DESC; i++){
1653 + USBRxPackets[i] = kmalloc(sizeof(u8) * RX_BUFF_SZ, GFP_ATOMIC | GFP_DMA); // todo: use GFP_KERNEL instead.
1654 + if(!USBRxPackets[i]){
1655 + for(i=i-1; i>=0; i--)
1656 + kfree((void *)USBRxPackets[i]);
1657 + printk("No mem.");
1662 + tx_ring0_cache = dma_alloc_coherent(NULL, sizeof(struct PDMA_txdesc) * NUM_TX_DESC, &tx_ring_bus_addr, GFP_KERNEL);
1663 + rx_ring0_cache = dma_alloc_coherent(NULL, sizeof(struct PDMA_rxdesc) * NUM_RX_DESC, &rx_ring_bus_addr, GFP_KERNEL);
1665 + printk("USB PDMA mode enabled.\n");
1666 + printk("tx_ring=%p\n", tx_ring0_cache);
1667 + printk("rx_ring=%p\n", rx_ring0_cache);
1669 + tx_ring0_noncache = tx_ring0_cache;
1670 + rx_ring0_noncache = rx_ring0_cache;
1672 + for(i=0; i < NUM_RX_DESC; i++){
1673 + memset((void *)&rx_ring0_noncache[i], 0, 16 /* sizeof()*/);
1674 + rx_ring0_noncache[i].rxd_info2.DDONE_bit = 0;
1675 + rx_ring0_noncache[i].rxd_info2.LS0= 0;
1676 + rx_ring0_noncache[i].rxd_info2.PLEN0= sizeof(u8) * RX_BUFF_SZ;
1677 + rx_ring0_noncache[i].rxd_info1.PDP0 = dma_map_single(NULL, (void *)USBRxPackets[i], sizeof(u8) * RX_BUFF_SZ, DMA_FROM_DEVICE);
1680 + for (i=0; i < NUM_TX_DESC; i++) {
1681 + memset((void *)&tx_ring0_noncache[i],0, 16 /* sizeof()*/);
1682 + tx_ring0_noncache[i].txd_info2.LS0_bit = 1;
1683 + tx_ring0_noncache[i].txd_info2.DDONE_bit = 1;
1684 + // we would map dma buffer dynamically in IRQ handler & ep_queue();
1687 + rx_dma_owner_idx0 = 0;
1688 + tx_cpu_owner_idx0 = 0;
1689 + tx_need_free_idx0 = 0;
1691 + /* initial UDMA register */
1692 + //OUTL(cpu_to_le32((u32) UDMA_Init_Setting), RTUSB_UDMA_CTRL);
1695 + printk("Storage mode enabled.\n");
1696 + reg_write(RTUSB_UDMA_CTRL, 0x3F000063); /* enable storage mode */
1698 + reg_write(RTUSB_UDMA_CTRL, 0x3F000003);
1701 + /* Tell the adapter where the TX/RX rings are located. */
1702 + //OUTL(phys_to_bus((u32) &rx_ring[0]), RTUSB_RX_BASE_PTR0);
1703 + reg_write(RTUSB_RX_BASE_PTR0, rx_ring_bus_addr);
1705 + //OUTL(phys_to_bus((u32) &tx_ring0[0]), RTUSB_TX_BASE_PTR0);
1706 + reg_write(RTUSB_TX_BASE_PTR0, tx_ring_bus_addr);
1708 + //OUTL(cpu_to_le32((u32) NUM_RX_DESC), RTUSB_RX_MAX_CNT0);
1709 + //OUTL(cpu_to_le32((u32) NUM_TX_DESC), RTUSB_TX_MAX_CNT0);
1710 + reg_write(RTUSB_RX_MAX_CNT0, cpu_to_le32(NUM_RX_DESC));
1711 + reg_write(RTUSB_TX_MAX_CNT0, cpu_to_le32(NUM_TX_DESC));
1713 + //OUTL(cpu_to_le32((u32) tx_cpu_owner_idx0), RTUSB_TX_CTX_IDX0);
1714 + //OUTL(cpu_to_le32((u32) (NUM_RX_DESC - 1)), RTUSB_RX_CALC_IDX0);
1715 + reg_write(RTUSB_TX_CTX_IDX0, cpu_to_le32(tx_cpu_owner_idx0));
1716 + reg_write(RTUSB_RX_CALC_IDX0, cpu_to_le32(NUM_RX_DESC - 1));
1722 +static int udc_dma_fini(void)
1728 + udc_dma_enable(false);
1729 + udc_dma_int_enable(false);
1731 + /* restore UDMA register */
1732 + reg_write(RTUSB_UDMA_CTRL, 0x0);
1734 + // unmap & free RX buffer
1735 + for(i=0; i<NUM_RX_DESC; i++){
1736 + addr = rx_ring0_noncache[i].rxd_info1.PDP0;
1738 + dma_unmap_single(NULL, addr, sizeof(u8) * RX_BUFF_SZ, DMA_FROM_DEVICE);
1739 + kfree((void *)USBRxPackets[i]);
1742 + // unmap Tx buffer only(but not free it)
1743 + for(i=0; i<NUM_TX_DESC; i++){
1744 + addr = tx_ring0_noncache[i].txd_info1.SDP0;
1746 + len = tx_ring0_noncache[i].txd_info2.SDL0;
1747 + dma_unmap_single(NULL, addr, sizeof(u8) * len, DMA_TO_DEVICE);
1751 + dma_free_coherent(NULL, sizeof(struct PDMA_txdesc) * NUM_TX_DESC, tx_ring0_cache, tx_ring_bus_addr);
1752 + dma_free_coherent(NULL, sizeof(struct PDMA_rxdesc) * NUM_RX_DESC, rx_ring0_cache, rx_ring_bus_addr);
1757 +static int rt_ep_enable(struct usb_ep *usb_ep, const struct usb_endpoint_descriptor *desc)
1759 + struct rt_ep_struct *rt_ep = container_of(usb_ep, struct rt_ep_struct, ep);
1760 + struct rt_udc_struct *rt_usb = rt_ep->rt_usb;
1761 + unsigned long flags;
1765 + if (!usb_ep || !desc || !EP_NO(rt_ep) || desc->bDescriptorType != USB_DT_ENDPOINT || rt_ep->bEndpointAddress != desc->bEndpointAddress) {
1766 + D_ERR(rt_usb->dev, "<%s> bad ep or descriptor\n", __func__);
1769 + if (rt_ep->bmAttributes != desc->bmAttributes) {
1770 + D_ERR(rt_usb->dev, "<%s> %s type mismatch, 0x%x, 0x%x\n", __func__, usb_ep->name, rt_ep->bmAttributes, desc->bmAttributes);
1773 + if (!rt_usb->driver || rt_usb->gadget.speed == USB_SPEED_UNKNOWN) {
1774 + D_ERR(rt_usb->dev, "<%s> bogus device state\n", __func__);
1775 + return -ESHUTDOWN;
1777 + local_irq_save(flags);
1778 + rt_ep->stopped = 0;
1780 + //rt_ep_irq_enable(rt_ep);
1782 + rt_ep_irq_enable(rt_ep);
1783 + local_irq_restore(flags);
1785 + xprintk("<%s> ENABLED %s\n", __func__, usb_ep->name);
1789 +static int rt_ep_disable(struct usb_ep *usb_ep)
1791 + struct rt_ep_struct *rt_ep = container_of(usb_ep, struct rt_ep_struct, ep);
1792 + unsigned long flags;
1795 + if (!usb_ep || !EP_NO(rt_ep) /* || !list_empty(&rt_ep->queue) */) {
1796 + D_ERR(rt_ep->rt_usb->dev, "<%s> %s can not be disabled\n", __func__, usb_ep ? rt_ep->ep.name : NULL);
1800 + local_irq_save(flags);
1801 + rt_ep->stopped = 1;
1802 + nuke(rt_ep, -ESHUTDOWN);
1804 + rt_ep_irq_disable(rt_ep);
1806 + local_irq_restore(flags);
1808 + xprintk("<%s> DISABLED %s\n", __func__, usb_ep->name);
1812 +static struct usb_request *rt_ep_alloc_request (struct usb_ep *usb_ep, gfp_t gfp_flags)
1814 + struct rt_request *req;
1817 + req = kzalloc(sizeof *req, gfp_flags);
1818 + if (!req || !usb_ep)
1821 + INIT_LIST_HEAD(&req->queue);
1826 +static void rt_ep_free_request(struct usb_ep *usb_ep, struct usb_request *usb_req)
1828 + struct rt_request *req;
1831 + req = container_of(usb_req, struct rt_request, req);
1832 + WARN_ON(!list_empty(&req->queue));
1838 + * 1) UDC TX (IN EPs)
1839 + * enqueue req -> handle_ep() -> write fifo -> TX_DONE -> handle_ep() -> write next fifo -> TX_DONE...
1841 + * 2) UDC RX (OUT EPs)
1842 + * enqueue req -> RX_DONE -> handle_ep() -> read_fifo -> RX_DONE -> handle_ep() -> read fifo...
1844 +static int rt_ep_queue(struct usb_ep *usb_ep, struct usb_request *req, gfp_t gfp_flags)
1846 + struct rt_ep_struct *rt_ep;
1847 + struct rt_udc_struct *rt_usb;
1848 + struct rt_request *rt_req;
1849 + unsigned long flags;
1851 + int handle_right_now = 0;
1853 + rt_ep = container_of(usb_ep, struct rt_ep_struct, ep);
1854 + rt_usb = rt_ep->rt_usb;
1855 + rt_req = container_of(req, struct rt_request, req);
1856 + rt_req->rt_ep = rt_ep;
1858 + if (rt_usb->set_config && !EP_NO(rt_ep)) {
1859 + rt_usb->set_config = 0;
1860 + D_ERR(rt_usb->dev, "<%s> gadget reply set config\n", __func__);
1864 + if (unlikely(!req || !rt_req || !req->complete || !req->buf)) {
1865 + D_ERR(rt_usb->dev, "<%s> bad params\n", __func__);
1869 + if (unlikely(!usb_ep || !rt_ep)) {
1870 + D_ERR(rt_usb->dev, "<%s> bad ep\n", __func__);
1874 + if (!rt_usb->driver || rt_usb->gadget.speed == USB_SPEED_UNKNOWN) {
1875 + D_ERR(rt_usb->dev, "<%s> bogus device state\n", __func__);
1876 + return -ESHUTDOWN;
1880 + xprintk("<eq> ep%d%s %p %dB\n", EP_NO(rt_ep), ((!EP_NO(rt_ep) && rt_ep->rt_usb->ep0state == EP0_IN_DATA_PHASE) || (EP_NO(rt_ep) && EP_DIR(rt_ep) == EP_IN )) ? "IN" : "OUT", &rt_req->req, req->length);
1882 + if (rt_ep->stopped) {
1883 + printk("EP%d -> stopped.\n", EP_NO(rt_ep));
1884 + req->status = -ESHUTDOWN;
1885 + return -ESHUTDOWN;
1888 + if (rt_req->in_use) {
1889 + D_ERR(rt_usb->dev, "<%s> refusing to queue req %p (already queued)\n", __func__, req);
1893 + local_irq_save(flags);
1895 + * handle No-data Ctrl transfer.
1897 + if(!EP_NO(rt_ep)/* EP0 */ && EP_DIR(rt_ep) == EP_OUT && !req->length){
1898 + done(rt_ep, rt_req, 0);
1899 + local_irq_restore(flags);
1903 + req->status = -EINPROGRESS;
1906 + if(dma || list_empty(&rt_ep->queue))
1907 + handle_right_now = 1;
1909 + ep_add_request(rt_ep, rt_req);
1911 + if(handle_right_now){
1912 + if(!EP_NO(rt_ep) && rt_ep->rt_usb->ep0state != EP0_OUT_DATA_PHASE){ /* ep0 && TX*/
1913 + handle_inep0(rt_ep);
1914 + }else if( EP_DIR(rt_ep) == EP_IN){ /* epin[1-x] */
1915 + handle_inep(rt_ep);
1917 + // other reqs are waiting for TX_DONE int.
1922 + if(EP_NO(rt_ep) && (EP_DIR(rt_ep) == EP_OUT))
1923 + tasklet_schedule(&rx_dma_tasklet);
1925 + if( (EP_DIR(rt_ep) == EP_OUT)/* OUT EP */ && rt_ep->pending){
1926 + rt_ep->pending = 0;
1927 + handle_pending_epoutirq(rt_usb, rt_ep, rt_req);
1931 + local_irq_restore(flags);
1935 +static int rt_ep_dequeue(struct usb_ep *usb_ep, struct usb_request *usb_req)
1937 + struct rt_ep_struct *rt_ep = container_of(usb_ep, struct rt_ep_struct, ep);
1938 + struct rt_request *req;
1939 + unsigned long flags;
1942 + if (unlikely(!usb_ep || !EP_NO(rt_ep))) {
1943 + D_ERR(rt_ep->rt_usb->dev, "<%s> bad ep\n", __func__);
1947 + local_irq_save(flags);
1949 + /* make sure it's actually queued on this endpoint */
1950 + list_for_each_entry(req, &rt_ep->queue, queue) {
1951 + if (&req->req == usb_req)
1954 + if (&req->req != usb_req) {
1955 + local_irq_restore(flags);
1959 + done(rt_ep, req, -ECONNRESET);
1961 + local_irq_restore(flags);
1965 +static int rt_ep_set_halt(struct usb_ep *usb_ep, int value)
1967 + struct rt_ep_struct *rt_ep = container_of(usb_ep, struct rt_ep_struct, ep);
1968 + unsigned long flags;
1971 + if (unlikely(!usb_ep || !EP_NO(rt_ep))) {
1972 + D_ERR(rt_ep->rt_usb->dev, "<%s> bad ep\n", __func__);
1976 + local_irq_save(flags);
1978 + if ((rt_ep->bEndpointAddress & USB_DIR_IN) && !list_empty(&rt_ep->queue)) {
1979 + local_irq_restore(flags);
1983 + rt_ep_stall(rt_ep, 1);
1985 + local_irq_restore(flags);
1987 + D_EPX(rt_ep->rt_usb->dev, "<%s> %s halt\n", __func__, usb_ep->name);
1991 +static int rt_ep_fifo_status(struct usb_ep *usb_ep)
1993 + struct rt_ep_struct *rt_ep = container_of(usb_ep, struct rt_ep_struct, ep);
1997 + D_ERR(rt_ep->rt_usb->dev, "<%s> bad ep\n", __func__);
2001 + if (rt_ep->rt_usb->gadget.speed == USB_SPEED_UNKNOWN)
2004 + return rt_fifo_bcount(rt_ep);
2007 +static void rt_ep_fifo_flush(struct usb_ep *usb_ep)
2009 + struct rt_ep_struct *rt_ep = container_of(usb_ep, struct rt_ep_struct, ep);
2010 + unsigned long flags;
2013 + local_irq_save(flags);
2015 + if (!usb_ep || !EP_NO(rt_ep) || !list_empty(&rt_ep->queue)) {
2016 + D_ERR(rt_ep->rt_usb->dev, "<%s> bad ep\n", __func__);
2017 + local_irq_restore(flags);
2021 + /* toggle and halt bits stay unchanged */
2024 + local_irq_restore(flags);
2027 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
2028 +static void *rt_ep_alloc_buffer(struct usb_ep *_ep, unsigned bytes, dma_addr_t *dma, gfp_t gfp_flags)
2032 + retval = kmalloc (bytes, gfp_flags & ~(__GFP_DMA|__GFP_HIGHMEM));
2034 +// *dma = virt_to_bus (retval);
2035 + *dma = (dma_addr_t)~0;
2039 +static void rt_ep_free_buffer(struct usb_ep *_ep, void *buf, dma_addr_t dma, unsigned bytes)
2045 +static struct usb_ep_ops rt_ep_ops = {
2046 + .enable = rt_ep_enable,
2047 + .disable = rt_ep_disable,
2049 + .alloc_request = rt_ep_alloc_request,
2050 + .free_request = rt_ep_free_request,
2051 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
2052 + .alloc_buffer = rt_ep_alloc_buffer,
2053 + .free_buffer = rt_ep_free_buffer,
2055 + .queue = rt_ep_queue,
2056 + .dequeue = rt_ep_dequeue,
2058 + .set_halt = rt_ep_set_halt,
2059 + .fifo_status= rt_ep_fifo_status,
2060 + .fifo_flush = rt_ep_fifo_flush,
2063 +/*******************************************************************************
2064 + * USB endpoint control functions
2065 + *******************************************************************************
2067 +static void usb_init_data(struct rt_udc_struct *rt_usb)
2069 + struct rt_ep_struct *rt_ep;
2073 + /* device/ep0 records init */
2074 + INIT_LIST_HEAD(&rt_usb->gadget.ep_list);
2075 + INIT_LIST_HEAD(&rt_usb->gadget.ep0->ep_list);
2076 + ep0_chg_stat(__func__, rt_usb, EP0_IDLE);
2078 + /* basic endpoint records init */
2079 + for (i = 0; i < RT_USB_NB_EP; i++) {
2080 + rt_ep = &rt_usb->rt_ep[i];
2083 + list_add_tail(&rt_ep->ep.ep_list, &rt_usb->gadget.ep_list);
2084 + rt_ep->stopped = 1;
2086 + rt_ep->stopped = 0;
2088 + INIT_LIST_HEAD(&rt_ep->queue);
2092 +static void udc_stop_activity(struct rt_udc_struct *rt_usb, struct usb_gadget_driver *driver)
2094 + struct rt_ep_struct *rt_ep;
2097 + if (rt_usb->gadget.speed == USB_SPEED_UNKNOWN)
2100 + /* prevent new request submissions, kill any outstanding requests */
2101 + for (i = 0; i < RT_USB_NB_EP; i++) {
2102 + rt_ep = &rt_usb->rt_ep[i];
2103 + if(i != 0){ /* don't have to flush EP[0]. */
2105 + rt_ep->stopped = 1;
2106 + rt_ep_irq_disable(rt_ep);
2108 + nuke(rt_ep, -ESHUTDOWN);
2116 + driver->disconnect(&rt_usb->gadget);
2120 + * keep for reference.
2122 +static void handle_config(unsigned long data)
2126 + struct imx_udc_struct *imx_usb = (void *)data;
2127 + struct usb_ctrlrequest u;
2128 + int temp, cfg, intf, alt;
2130 + local_irq_disable();
2132 + temp = __raw_readl(imx_usb->base + USB_STAT);
2133 + cfg = (temp & STAT_CFG) >> 5;
2134 + intf = (temp & STAT_INTF) >> 3;
2135 + alt = temp & STAT_ALTSET;
2137 + xprintk("<%s> orig config C=%d, I=%d, A=%d / req config C=%d, I=%d, A=%d\n", __func__, imx_usb->cfg, imx_usb->intf, imx_usb->alt, cfg, intf, alt);
2139 + if (cfg == 1 || cfg == 2) {
2141 + if (imx_usb->cfg != cfg) {
2142 + u.bRequest = USB_REQ_SET_CONFIGURATION;
2143 + u.bRequestType = USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
2147 + imx_usb->cfg = cfg;
2148 + imx_usb->driver->setup(&imx_usb->gadget, &u);
2151 + if (imx_usb->intf != intf || imx_usb->alt != alt) {
2152 + u.bRequest = USB_REQ_SET_INTERFACE;
2153 + u.bRequestType = USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_INTERFACE;
2157 + imx_usb->intf = intf;
2158 + imx_usb->alt = alt;
2159 + imx_usb->driver->setup(&imx_usb->gadget, &u);
2163 + imx_usb->set_config = 0;
2165 + local_irq_enable();
2169 +static void handle_setup(struct rt_udc_struct *rt_usb)
2174 + struct usb_ctrlrequest r;
2178 + struct rt_ep_struct *rt_ep = &rt_usb->rt_ep[0];
2180 + nuke(rt_ep, -EPROTO);
2182 + // read setup packet
2183 + for (i = 0; i < 8; i++)
2184 + u.raw[i] = usb_read(SETUPDATA + i);
2186 + le16_to_cpus(&u.r.wValue);
2187 + le16_to_cpus(&u.r.wIndex);
2188 + le16_to_cpus(&u.r.wLength);
2190 + xprintk("<SETUP> %02x.%02x v%04x\n", u.r.bRequestType, u.r.bRequest, u.r.wValue);
2192 + switch(u.r.bRequest){
2193 + /* HW(CUSB2) has handled it. */
2194 + case USB_REQ_SET_ADDRESS:
2195 + if (u.r.bRequestType != (USB_TYPE_STANDARD | USB_RECIP_DEVICE))
2201 + ep0_chg_stat(__func__, rt_usb, EP0_NO_DATA_PHASE);
2202 + }else if (u.r.bRequestType & USB_DIR_IN){
2203 + ep0_chg_stat(__func__, rt_usb, EP0_IN_DATA_PHASE);
2205 + // reload and clear out0bc
2206 + usb_write(OUT0BC, 0);
2207 + ep0_chg_stat(__func__, rt_usb, EP0_OUT_DATA_PHASE);
2210 + if(!rt_usb->driver){
2211 + printk("<%s> please insert gadget driver/module.\n", __func__);
2215 + if(!rt_usb->driver->setup)
2218 + i = rt_usb->driver->setup(&rt_usb->gadget, &u.r); // gadget would queue more usb req here
2221 + printk("<%s> device setup error %d\n", __func__, i);
2225 + if(rt_usb->ep0state == EP0_NO_DATA_PHASE){
2226 + epcs = read_epcs(rt_ep);
2227 + epcs |= EP_CS_EP0_HSNAK; // clear hsnak to let HW ack the status stage.
2228 + write_epcs(rt_ep, epcs);
2233 + printk("<%s> protocol STALL\n", __func__);
2234 + rt_ep_stall(rt_ep, 1);
2235 + ep0_chg_stat(__func__, rt_usb, EP0_STALL);
2240 + * handle TX done interrupt
2242 +static void handle_epinirq(struct rt_udc_struct *rt_usb, u8 epinirq)
2245 + struct rt_request *req;
2246 + struct rt_ep_struct *rt_ep;
2248 + rt_ep = &rt_usb->rt_ep[epinirq];
2250 + if (list_empty(&rt_ep->queue))
2251 + FATAL_ERROR("empty queue");
2253 + // clear ep interrupt
2255 + irq |= 1 << epinirq;
2256 + usb_write(IN07IRQ, irq);
2258 + irq |= 1 << (epinirq-8);
2259 + usb_write(IN815IRQ, irq);
2262 + req = list_entry(rt_ep->queue.next, struct rt_request, queue);
2263 + xprintk("r.l=%d, r.a=%d\n", req->req.length, req->req.actual);
2264 + if(req->req.actual >= req->req.length ){
2265 + if( req->req.actual && (!(req->req.actual % rt_ep->ep.maxpacket)) && req->req.zero){
2266 + // deal with one more "zlp"
2267 + req->req.zero = 0;
2268 + write_ep_fifo_zlp(rt_ep);
2272 + // the first tx req in ep->queue is done.
2273 + rt_ep->tx_done_count = 0;
2274 + if(!epinirq /* EP0 */)
2275 + ep0_chg_stat(__func__, rt_usb, EP0_IDLE);
2276 + done(rt_ep, req, 0);
2278 + // more reqs there.
2279 + if (!list_empty(&rt_ep->queue) && !rt_ep->tx_done_count){
2280 + if(!epinirq /* EP0 */){
2281 + handle_inep0(rt_ep);
2283 + handle_inep(rt_ep);
2288 + if(!epinirq /* EP0 */){
2289 + handle_inep0(rt_ep);
2291 + handle_inep(rt_ep);
2295 +static void handle_ep0outirq(struct rt_udc_struct *rt_usb, u8 epoutirq)
2297 + u8 epcs, irq = 0x0;
2298 + struct rt_request *req = NULL;
2299 + struct rt_ep_struct *rt_ep = NULL;
2302 + rt_ep = &rt_usb->rt_ep[0];
2304 + if(rt_usb->ep0state == EP0_STALL){
2305 + printk("<%s> protocol STALL\n", __func__);
2306 + rt_ep_stall(rt_ep, 1);
2310 + if(rt_usb->ep0state != EP0_OUT_DATA_PHASE)
2311 + FATAL_ERROR("Odd stage");
2314 + if(unlikely(!read_outbc(0x0)))
2315 + FATAL_ERROR("EP0 BC");
2317 + if (unlikely(list_empty(&rt_ep->queue)))
2318 + FATAL_ERROR("EP0 no req");
2320 + req = handle_outep0(rt_ep);
2322 + //req = list_entry(rt_ep->queue.next, struct rt_request, queue);
2323 + xprintk("q.l=%d,q.a=%d\n", req->req.length, req->req.actual);
2325 + // clear ep interrupt
2327 + usb_write(OUT07IRQ, irq);
2329 + if(req && ((req->req.actual % rt_ep->ep.maxpacket) || (req->req.actual >= req->req.length))){
2330 + ep0_chg_stat(__func__, rt_usb, EP0_IDLE);
2331 + done(rt_ep, req, 0); // short packet indicates transaction is done.
2333 + epcs = read_epcs(rt_ep);
2334 + epcs |= EP_CS_EP0_HSNAK; // clear hsnak bit to let HW ack the status stage.
2335 + write_epcs(rt_ep, epcs);
2340 + usb_write(OUT0BC /*out0bc*/, 0x0);
2341 + epcs = read_epcs(rt_ep);
2342 + }while(!(epcs & EP0_OUT_BSY));
2345 +static void handle_pending_epoutirq(struct rt_udc_struct *rt_usb, struct rt_ep_struct *rt_ep, struct rt_request *req)
2351 + if(unlikely(!read_outbc(EP_NO(rt_ep))))
2352 + FATAL_ERROR("No BC");
2354 + handle_outep(rt_ep);
2355 + if(req && ( (req->req.actual % rt_ep->ep.maxpacket) || (req->req.actual >= req->req.length))){
2356 + xprintk("q.l=%d,q.a=%d\n", req->req.length, req->req.actual);
2358 + //rx_done(rt_ep, req, 0);
2359 + done(rt_ep, req, 0);
2362 + epcs = read_epcs(rt_ep);
2363 + write_epcs(rt_ep, 0x0);
2364 + epcs = read_epcs(rt_ep);
2366 + }while(!(epcs & EP_CS_BSY));
2369 +static void handle_epoutirq(struct rt_udc_struct *rt_usb, u8 epoutirq)
2374 + if(unlikely(epoutirq == 0x0)){
2375 + handle_ep0outirq(rt_usb, 0x0);
2379 + tasklet_schedule(&rx_tasklet);
2381 + // clear ep interrupt
2382 + irq |= 1 << epoutirq;
2383 + usb_write(OUT07IRQ, irq);
2387 +static void eps_change_to_hs(struct rt_udc_struct *rt_usb)
2390 + struct rt_ep_struct *rt_ep;
2391 + for(i = 0; i < RT_USB_NB_EP; i++){
2392 + rt_ep = &rt_usb->rt_ep[i];
2393 + if(rt_ep->bmAttributes == USB_ENDPOINT_XFER_BULK){
2394 + rt_ep->ep.maxpacket = 512;
2399 +static void eps_change_to_fs(struct rt_udc_struct *rt_usb)
2402 + struct rt_ep_struct *rt_ep;
2403 + for(i = 0; i < RT_USB_NB_EP; i++){
2404 + rt_ep = &rt_usb->rt_ep[i];
2405 + if(rt_ep->bmAttributes == USB_ENDPOINT_XFER_BULK){
2406 + rt_ep->ep.maxpacket = 64;
2411 +void handle_highspeed(struct rt_udc_struct *rt_usb)
2415 + eps_change_to_hs(rt_usb);
2418 +#if defined (CONFIG_RALINK_RT3883) || defined (CONFIG_RALINK_RT3352) || defined (CONFIG_RALINK_MT7620)
2419 + usb_write(IN1CON, 0x8D); // InEP1 : Int, 2 subfifos
2420 + usb_write(IN2CON, 0x89); // InEP2 : Bulk, 2 subfifo
2421 + usb_write(OUT1CON, 0x8D); // OutEP1 : Int, 2 subfifos
2422 + usb_write(OUT2CON, 0x89); // OutEP2 : Bulk, 2 subfifos
2423 + //usb_write(OUT3CON, 0x89); // OutEP3 : Bulk, 2 subfifo
2424 + //usb_write(OUT4CON, 0x89); // OutEP4 : Bulk. 2 subfifo
2425 +#elif defined (CONFIG_RALINK_RT5350)
2427 + usb_write(IN1CON, 0x89); // InEP1 : Bulk, 2 subfifos
2428 + usb_write(OUT1CON, 0x89); // OutEP1 : Bulk, 2 subfifos
2430 +#error "define a platform"
2434 +#if defined (CONFIG_RALINK_RT3883) || defined (CONFIG_RALINK_RT3352) || defined (CONFIG_RALINK_MT7620)
2435 + usb_write(IN1CON, 0x8C); // InEP1 : Int , 1 subfifos
2436 + usb_write(IN2CON, 0x88); // InEP2 : Bulk, 1 subfifo
2438 + usb_write(OUT1CON, 0x8C); // OutEP1 : Int, 1 subfifos
2439 + usb_write(OUT2CON, 0x88); // OutEP2 : Bulk, 1 subfifos
2440 + //usb_write(OUT3CON, 0x88); // OutEP3 : Bulk, 1 subfifo
2441 + //usb_write(OUT4CON, 0x88); // OutEP4 : Bulk. 1 subfifo
2442 +#elif defined (CONFIG_RALINK_RT5350)
2444 + usb_write(IN1CON, 0x88); // InEP1 : Bulk , 1 subfifos
2445 + usb_write(OUT1CON, 0x88); // OutEP1 : Bulk, 1 subfifos
2447 +#error "define a platform"
2451 + // clear all pending interrupts
2452 + usb_write(IN07IRQ, 0xFF);
2453 + usb_write(OUT07IRQ, 0xFF);
2455 + rt_usb->gadget.speed = USB_SPEED_HIGH;
2457 + // reset ALL endpoints
2458 + rt_all_eps_reset();
2460 + // Enable ep0 interrupt.
2461 + // (EPx interrupt is enabled in EPx_enable(). )
2462 + rt_ep_irq_enable(&rt_usb->rt_ep[0]);
2465 +static void handle_reset(struct rt_udc_struct *rt_usb)
2467 + struct rt_ep_struct *rt_ep;
2470 + eps_change_to_fs(rt_usb);
2472 + // remove all EPs' usb request
2473 + for (i = 0; i < RT_USB_NB_EP; i++) {
2474 + rt_ep = &rt_usb->rt_ep[i];
2475 + if(i != 0){ /* don't have to flush EP[0]. */
2477 + rt_ep->stopped = 1;
2478 + rt_ep_irq_disable(rt_ep);
2480 + nuke(rt_ep, -ESHUTDOWN);
2488 + // clear all PDMA interrupts
2489 + udc_dma_all_int_clear();
2494 + // clear all pending interrupts
2495 + usb_write(IN07IRQ, 0xFF);
2496 + usb_write(OUT07IRQ, 0xFF);
2498 + // flush all EP's fifo
2499 + rt_all_eps_reset();
2502 +static void handle_usbirq(struct rt_udc_struct *rt_usb, u8 usbirq)
2504 + if(usbirq & USB_INTR_SETUP_TOKEN_VALID){
2505 + // Setup token is arrival.
2506 + // get setup data and pass it to gadget driver.
2507 + handle_setup(rt_usb);
2510 + if(usbirq & USB_INTR_RESET)
2511 + handle_reset(rt_usb);
2513 + if(usbirq & USB_INTR_HSPEED)
2514 + handle_highspeed(rt_usb);
2517 + * DO NOT try to clear SoF and token Interrupt!
2519 + if( (usbirq & USB_INTR_SETUP_TOKEN_VALID) ||
2520 + (usbirq & USB_INTR_HSPEED) ||
2521 + (usbirq & USB_INTR_RESET))
2522 + usb_write(USBIRQ, usbirq);
2525 +static int irq_count = 100; /* for debug */
2527 + * Interrupt handler
2529 +irqreturn_t rt_irq_handler(int irq, void *_dev)
2531 + u32 usbirq,epin07irq,epin07ien,epout07irq,epout07ien;
2532 + struct rt_udc_struct *rt_usb = _dev;
2534 + u32 count_tmp = irq_count;
2541 + usbirq = usb_read(USBIRQ);
2542 + epin07irq = usb_read(IN07IRQ);
2543 + epin07ien = usb_read(IN07IEN);
2544 + epout07irq = usb_read(OUT07IRQ);
2545 + epout07ien = usb_read(OUT07IEN);
2547 + //epin07irq = epin07irq & epin07ien;
2548 + //epout07irq = epout07irq & epout07ien;
2550 + xprintk(">%x\n", count_tmp);
2551 + dump_usbirq(usbirq);
2552 + dump_epirq(epin07irq, epin07ien, 1);
2553 + dump_epirq(epout07irq, epout07ien, 0);
2556 + u32 dma_irq = reg_read(RTUSB_INT_STATUS);
2557 + if(epin07irq & 0x1) // INEP0
2558 + handle_epinirq(rt_usb, 0);
2560 + if(usbirq) // HS, Reset, SetupValid
2561 + handle_usbirq(rt_usb, usbirq);
2563 + if(epout07irq & 0x1) // OUTEP0
2564 + handle_epoutirq(rt_usb, 0);
2567 + handle_dmairq(rt_usb, dma_irq);
2570 + if(epin07irq & 0x1) // INEP0
2571 + handle_epinirq(rt_usb, 0);
2573 + if(usbirq) // HS, Reset, SetupValid
2574 + handle_usbirq(rt_usb, usbirq);
2576 + if(epout07irq & 0x1) // OUTEP0
2577 + handle_epoutirq(rt_usb, 0);
2579 + if(epout07irq & 0x2) // OUTEP1
2580 + handle_epoutirq(rt_usb, 1);
2582 + if(epin07irq & 0x2) // INEP1
2583 + handle_epinirq(rt_usb, 1);
2585 + if(epout07irq & 0x4) // OUTEP2
2586 + handle_epoutirq(rt_usb, 2);
2588 + if(epin07irq & 0x4) // INEP2
2589 + handle_epinirq(rt_usb, 2);
2591 + //if(epout07irq & 0x8) // OUTEP3
2592 + // handle_epoutirq(rt_usb, 3);
2594 + //if(epout07irq & 0x10) // OUTEP4
2595 + // handle_epoutirq(rt_usb, 4);
2597 + xprintk("<%x\n", count_tmp);
2598 + return IRQ_HANDLED;
2602 + ******************************************************************************
2603 + * Static defined Ralink UDC structure
2604 + *******************************************************************************
2606 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36)
2607 +static void nop_release(struct device *dev)
2613 +static const struct usb_gadget_ops rt_udc_ops = {
2614 + .get_frame = rt_udc_get_frame,
2615 + .wakeup = rt_udc_wakeup,
2618 +static struct rt_udc_struct controller = {
2620 + .ops = &rt_udc_ops,
2621 + .ep0 = &controller.rt_ep[0].ep,
2622 + .name = driver_name,
2624 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36)
2625 + .init_name = "gadget",
2626 + .release = nop_release,
2628 + .bus_id = "gadget",
2635 + .ops = &rt_ep_ops,
2638 + .rt_usb = &controller,
2639 + .bEndpointAddress = 0,
2640 + .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
2643 +#if defined (CONFIG_RALINK_RT3883) || defined (CONFIG_RALINK_RT3352) || defined (CONFIG_RALINK_MT7620)
2646 + .name = "ep1in-int",
2647 + .ops = &rt_ep_ops,
2650 + .rt_usb = &controller,
2651 + .bEndpointAddress = USB_DIR_IN | 1,
2652 + .bmAttributes = USB_ENDPOINT_XFER_INT,
2657 + .name = "ep2in-bulk",
2658 + .ops = &rt_ep_ops,
2661 + .rt_usb = &controller,
2662 + .bEndpointAddress = USB_DIR_IN | 2,
2663 + .bmAttributes = USB_ENDPOINT_XFER_BULK,
2668 + .name = "ep1out-int",
2669 + .ops = &rt_ep_ops,
2672 + .rt_usb = &controller,
2673 + .bEndpointAddress = USB_DIR_OUT | 1,
2674 + .bmAttributes = USB_ENDPOINT_XFER_INT,
2679 + .name = "ep2out-bulk",
2680 + .ops = &rt_ep_ops,
2683 + .rt_usb = &controller,
2684 + .bEndpointAddress = USB_DIR_OUT | 2,
2685 + .bmAttributes = USB_ENDPOINT_XFER_BULK,
2691 + .name = "ep3out-bulk",
2692 + .ops = &rt_ep_ops,
2695 + .rt_usb = &controller,
2696 + .bEndpointAddress = USB_DIR_OUT | 3,
2697 + .bmAttributes = USB_ENDPOINT_XFER_BULK,
2702 + .name = "ep4out-bulk",
2703 + .ops = &rt_ep_ops,
2706 + .rt_usb = &controller,
2707 + .bEndpointAddress = USB_DIR_OUT | 4,
2708 + .bmAttributes = USB_ENDPOINT_XFER_BULK,
2713 +#elif defined (CONFIG_RALINK_RT5350)
2716 + .name = "ep1in-bulk",
2717 + .ops = &rt_ep_ops,
2720 + .rt_usb = &controller,
2721 + .bEndpointAddress = USB_DIR_IN | 1,
2722 + .bmAttributes = USB_ENDPOINT_XFER_BULK,
2727 + .name = "ep1out-bulk",
2728 + .ops = &rt_ep_ops,
2731 + .rt_usb = &controller,
2732 + .bEndpointAddress = USB_DIR_OUT | 1,
2733 + .bmAttributes = USB_ENDPOINT_XFER_BULK,
2737 +#error "define a platform"
2742 + *******************************************************************************
2743 + * USB gadged driver functions
2744 + *******************************************************************************
2747 +static void rt_udc_enable(struct rt_udc_struct *rt_usb)
2750 + rt_usb->gadget.speed = USB_SPEED_FULL;
2752 + // enable dma interrupts
2753 + udc_dma_all_int_clear();
2754 + udc_dma_int_enable(true);
2759 + udc_dma_enable(true);
2763 +static void rt_udc_disable(struct rt_udc_struct *rt_usb)
2766 + ep0_chg_stat(__func__, rt_usb, EP0_IDLE);
2767 + rt_usb->gadget.speed = USB_SPEED_UNKNOWN;
2769 + // disable dma interrupts
2770 + udc_dma_all_int_clear();
2771 + udc_dma_int_enable(false);
2776 + udc_dma_enable(false);
2780 +int usb_gadget_register_driver(struct usb_gadget_driver *driver)
2782 + struct rt_udc_struct *rt_usb = &controller;
2786 + if (!driver || driver->speed < USB_SPEED_FULL || !driver->bind || !driver->disconnect || !driver->setup)
2790 + if (rt_usb->driver)
2793 + /* first hook up the driver ... */
2794 + rt_usb->driver = driver;
2795 + rt_usb->gadget.dev.driver = &driver->driver;
2796 + retval = device_add(&rt_usb->gadget.dev);
2800 + retval = driver->bind(&rt_usb->gadget);
2802 + D_ERR(rt_usb->dev, "<%s> bind to driver --> error %d\n", __func__, retval);
2803 + device_del(&rt_usb->gadget.dev);
2807 + D_INI(rt_usb->dev, "<%s> registered gadget driver '%s'\n", __func__, driver->driver.name);
2808 + rt_udc_enable(rt_usb);
2812 + rt_usb->driver = NULL;
2813 + rt_usb->gadget.dev.driver = NULL;
2816 +EXPORT_SYMBOL(usb_gadget_register_driver);
2818 +int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
2820 + struct rt_udc_struct *rt_usb = &controller;
2825 + if (!driver || driver != rt_usb->driver || !driver->unbind)
2828 + udc_stop_activity(rt_usb, driver);
2829 + rt_udc_disable(rt_usb);
2830 + del_timer(&rt_usb->timer);
2832 + driver->unbind(&rt_usb->gadget);
2833 + rt_usb->gadget.dev.driver = NULL;
2834 + rt_usb->driver = NULL;
2836 + device_del(&rt_usb->gadget.dev);
2838 + D_INI(rt_usb->dev, "<%s> unregistered gadget driver '%s'\n", __func__, driver->driver.name);
2842 +EXPORT_SYMBOL(usb_gadget_unregister_driver);
2844 +/*******************************************************************************
2845 + * Module functions
2846 + *******************************************************************************
2848 +static int __init rt_udc_probe(struct platform_device *pdev)
2850 + struct rt_udc_struct *rt_usb = &controller;
2851 + struct resource *res_mem, *res_irq;
2852 + void __iomem *base;
2853 + int ret = 0, res_mem_size;
2856 + res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2858 + dev_err(&pdev->dev, "can't get device resources\n");
2862 + res_mem_size = res_mem->end - res_mem->start + 1;
2863 + if (!request_mem_region(res_mem->start, res_mem_size, res_mem->name)) {
2864 + dev_err(&pdev->dev, "can't allocate %d bytes at %d address\n", res_mem_size, res_mem->start);
2868 + base = ioremap(res_mem->start, res_mem_size);
2870 + dev_err(&pdev->dev, "ioremap failed\n");
2875 + res_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2877 + dev_err(&pdev->dev, "can't get irq number\n");
2881 + rt_usb->interrupt = res_irq->start;
2883 + ret = request_irq(rt_usb->interrupt, rt_irq_handler, IRQF_DISABLED, driver_name, rt_usb);
2885 + dev_err(&pdev->dev, "can't get irq %i, err %d\n", rt_usb->interrupt, ret);
2889 + rt_usb->res = res_mem;
2890 + rt_usb->base = base;
2891 + rt_usb->dev = &pdev->dev;
2893 + device_initialize(&rt_usb->gadget.dev);
2895 + rt_usb->gadget.dev.parent = &pdev->dev;
2896 + rt_usb->gadget.dev.dma_mask = pdev->dev.dma_mask;
2898 + platform_set_drvdata(pdev, rt_usb);
2900 + usb_init_data(rt_usb);
2903 + if(rt_udc_dma_init())
2907 + rt_udc_init(rt_usb);
2909 + init_timer(&rt_usb->timer);
2910 + rt_usb->timer.function = handle_config;
2911 + rt_usb->timer.data = (unsigned long)rt_usb;
2915 + free_irq(rt_usb->interrupt, rt_usb);
2919 + release_mem_region(res_mem->start, res_mem_size);
2923 +static int __exit rt_udc_remove(struct platform_device *pdev)
2925 + struct rt_udc_struct *rt_usb = platform_get_drvdata(pdev);
2928 + rt_udc_disable(rt_usb);
2929 + del_timer(&rt_usb->timer);
2931 + free_irq(rt_usb->interrupt, rt_usb);
2933 + iounmap(rt_usb->base);
2934 + release_mem_region(rt_usb->res->start, rt_usb->res->end - rt_usb->res->start + 1);
2936 + //if (pdata->exit)
2937 + // pdata->exit(&pdev->dev);
2938 + platform_set_drvdata(pdev, NULL);
2943 +static void set_device_mode(void)
2946 + val = le32_to_cpu(*(volatile u_long *)(SYSCFG1));
2947 + val = val & ~(USB0_HOST_MODE);
2948 + *(volatile u_long *)(SYSCFG1) = cpu_to_le32(val);
2952 +/*----------------------------------------------------------------------------*/
2953 +static struct platform_driver udc_driver = {
2956 + .name = driver_name,
2957 + .owner = THIS_MODULE,
2959 + .probe = rt_udc_probe,
2960 + .remove = __exit_p(rt_udc_remove),
2965 +static int udc_create_proc(void)
2967 + pProcDir = proc_mkdir(PROC_DIR, NULL);
2968 + if ((pProcDebugLevel = create_proc_entry(DEBUGLEVEL_PROCFILE, 0, pProcDir))){
2969 + pProcDebugLevel->read_proc = (read_proc_t*)&debuglevel_read;
2970 + pProcDebugLevel->write_proc = (write_proc_t*)&debuglevel_write;
2975 +static int udc_remove_proc(void)
2977 + if (pProcDebugLevel)
2978 + remove_proc_entry(DEBUGLEVEL_PROCFILE, pProcDir);
2980 + remove_proc_entry(PROC_DIR, 0);
2985 +static int __init udc_init(void)
2988 + udc_create_proc();
2991 + set_device_mode();
2993 + ret = platform_driver_register(&udc_driver);
2995 + tasklet_init(&rx_tasklet, rx_do_tasklet, 0);
2996 + tasklet_init(&tx_tasklet, tx_do_tasklet, 0);
2999 + printk("DMA TXMAXCAP=%d\n", TXMAXCAP);
3000 + tasklet_init(&rx_dma_tasklet, rx_dma_done_do_tasklet, 0);
3001 + tasklet_init(&tx_dma_tasklet, tx_dma_done_do_tasklet, 0);
3004 + return ret; //platform_driver_probe(&udc_driver, rt_udc_probe);
3006 +module_init(udc_init);
3008 +static void __exit udc_exit(void)
3011 + udc_remove_proc();
3014 + platform_driver_unregister(&udc_driver);
3016 +module_exit(udc_exit);
3018 +MODULE_DESCRIPTION("Ralink USB Device Controller driver");
3019 +MODULE_AUTHOR("Ying Yuan Huang <yy_huang@ralinktech.com>");
3020 +MODULE_LICENSE("GPL");
3021 +MODULE_ALIAS("platform:rt_udc");