ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / usb / gadget / pxa2xx_udc.h
1 /*
2  * linux/drivers/usb/gadget/pxa2xx_udc.h
3  * Intel PXA2xx on-chip full speed USB device controller
4  *
5  * Copyright (C) 2003 Robert Schwebel <r.schwebel@pengutronix.de>, Pengutronix
6  * Copyright (C) 2003 David Brownell
7  *
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 #ifndef __LINUX_USB_GADGET_PXA2XX_H
25 #define __LINUX_USB_GADGET_PXA2XX_H
26
27 #include <linux/types.h>
28
29 /*-------------------------------------------------------------------------*/
30
31 /* pxa2xx has this (move to include/asm-arm/arch-pxa/pxa-regs.h) */
32 #define UFNRH_SIR       (1 << 7)        /* SOF interrupt request */
33 #define UFNRH_SIM       (1 << 6)        /* SOF interrupt mask */
34 #define UFNRH_IPE14     (1 << 5)        /* ISO packet error, ep14 */
35 #define UFNRH_IPE9      (1 << 4)        /* ISO packet error, ep9 */
36 #define UFNRH_IPE4      (1 << 3)        /* ISO packet error, ep4 */
37
38 /* pxa255 has this (move to include/asm-arm/arch-pxa/pxa-regs.h) */
39 #define UDCCFR          UDC_RES2        /* UDC Control Function Register */
40 #define UDCCFR_AREN     (1 << 7)        /* ACK response enable (now) */
41 #define UDCCFR_ACM      (1 << 2)        /* ACK control mode (wait for AREN) */
42
43 /*-------------------------------------------------------------------------*/
44
45 struct pxa2xx_udc;
46
47 struct pxa2xx_ep {
48         struct usb_ep                           ep;
49         struct pxa2xx_udc                       *dev;
50
51         const struct usb_endpoint_descriptor    *desc;
52         struct list_head                        queue;
53         unsigned long                           pio_irqs;
54         unsigned long                           dma_irqs;
55         short                                   dma; 
56
57         unsigned short                          fifo_size;
58         u8                                      bEndpointAddress;
59         u8                                      bmAttributes;
60
61         unsigned                                stopped : 1;
62         unsigned                                dma_fixup : 1;
63                                                          
64         /* UDCCS = UDC Control/Status for this EP
65          * UBCR = UDC Byte Count Remaining (contents of OUT fifo)
66          * UDDR = UDC Endpoint Data Register (the fifo)
67          * DRCM = DMA Request Channel Map
68          */
69         volatile u32                            *reg_udccs;
70         volatile u32                            *reg_ubcr;
71         volatile u32                            *reg_uddr;
72 #ifdef USE_DMA
73         volatile u32                            *reg_drcmr;
74 #define drcmr(n)  .reg_drcmr = & DRCMR ## n ,
75 #else
76 #define drcmr(n)  
77 #endif
78 };
79
80 struct pxa2xx_request {
81         struct usb_request                      req;
82         struct list_head                        queue;
83 };
84
85 enum ep0_state { 
86         EP0_IDLE,
87         EP0_IN_DATA_PHASE,
88         EP0_OUT_DATA_PHASE,
89         EP0_END_XFER,
90         EP0_STALL,
91 };
92
93 #define EP0_FIFO_SIZE   ((unsigned)16)
94 #define BULK_FIFO_SIZE  ((unsigned)64)
95 #define ISO_FIFO_SIZE   ((unsigned)256)
96 #define INT_FIFO_SIZE   ((unsigned)8)
97
98 struct udc_stats {
99         struct ep0stats {
100                 unsigned long           ops;
101                 unsigned long           bytes;
102         } read, write;
103         unsigned long                   irqs;
104 };
105
106 #ifdef CONFIG_USB_PXA2XX_SMALL
107 /* when memory's tight, SMALL config saves code+data.  */
108 #undef  USE_DMA
109 #define PXA_UDC_NUM_ENDPOINTS   3
110 #endif
111
112 #ifndef PXA_UDC_NUM_ENDPOINTS
113 #define PXA_UDC_NUM_ENDPOINTS   16
114 #endif
115
116 struct pxa2xx_udc {
117         struct usb_gadget                       gadget;
118         struct usb_gadget_driver                *driver;
119
120         enum ep0_state                          ep0state;
121         struct udc_stats                        stats;
122         unsigned                                got_irq : 1,
123                                                 got_disc : 1,
124                                                 has_cfr : 1,
125                                                 req_pending : 1,
126                                                 req_std : 1,
127                                                 req_config : 1;
128
129 #define start_watchdog(dev) mod_timer(&dev->timer, jiffies + (HZ/200))
130         struct timer_list                       timer;
131
132         struct device                           *dev;
133         struct pxa2xx_udc_mach_info             *mach;
134         u64                                     dma_mask;
135         struct pxa2xx_ep                        ep [PXA_UDC_NUM_ENDPOINTS];
136 };
137
138 /*-------------------------------------------------------------------------*/
139
140 #ifdef CONFIG_ARCH_LUBBOCK
141 #include <asm/arch/lubbock.h>
142 /* lubbock can also report usb connect/disconnect irqs */
143
144 #ifdef DEBUG
145 #define HEX_DISPLAY(n)  if (machine_is_lubbock()) { LUB_HEXLED = (n); }
146
147 #define LED_CONNECTED_ON        if (machine_is_lubbock()) { \
148         DISCRETE_LED_ON(D26); }
149 #define LED_CONNECTED_OFF       if(machine_is_lubbock()) { \
150         DISCRETE_LED_OFF(D26); LUB_HEXLED = 0; }
151 #define LED_EP0_ON      if (machine_is_lubbock()) { DISCRETE_LED_ON(D25); }
152 #define LED_EP0_OFF     if (machine_is_lubbock()) { DISCRETE_LED_OFF(D25); }
153 #endif /* DEBUG */
154
155 #endif
156
157 /*-------------------------------------------------------------------------*/
158
159 /* LEDs are only for debug */
160 #ifndef HEX_DISPLAY
161 #define HEX_DISPLAY(n)          do {} while(0)
162 #endif
163
164 #ifndef LED_CONNECTED_ON
165 #define LED_CONNECTED_ON        do {} while(0)
166 #define LED_CONNECTED_OFF       do {} while(0)
167 #endif
168 #ifndef LED_EP0_ON
169 #define LED_EP0_ON              do {} while (0)
170 #define LED_EP0_OFF             do {} while (0)
171 #endif
172
173 /*-------------------------------------------------------------------------*/
174
175 static struct pxa2xx_udc *the_controller;
176
177 /* one GPIO should be used to detect host disconnect */
178 static inline int is_usb_connected(void)
179 {
180         if (!the_controller->mach->udc_is_connected)
181                 return 1;
182         return the_controller->mach->udc_is_connected();
183 }
184
185 /* one GPIO should force the host to see this device (or not) */
186 static inline void make_usb_disappear(void)
187 {
188         if (!the_controller->mach->udc_command)
189                 return;
190         the_controller->mach->udc_command(PXA2XX_UDC_CMD_DISCONNECT);
191 }
192
193 static inline void let_usb_appear(void)
194 {
195         if (!the_controller->mach->udc_command)
196                 return;
197         the_controller->mach->udc_command(PXA2XX_UDC_CMD_CONNECT);
198 }
199
200 /*-------------------------------------------------------------------------*/
201
202 /*
203  * Debugging support vanishes in non-debug builds.  DBG_NORMAL should be
204  * mostly silent during normal use/testing, with no timing side-effects.
205  */
206 #define DBG_NORMAL      1       /* error paths, device state transitions */
207 #define DBG_VERBOSE     2       /* add some success path trace info */
208 #define DBG_NOISY       3       /* ... even more: request level */
209 #define DBG_VERY_NOISY  4       /* ... even more: packet level */
210
211 #ifdef DEBUG
212
213 static const char *state_name[] = {
214         "EP0_IDLE",
215         "EP0_IN_DATA_PHASE", "EP0_OUT_DATA_PHASE",
216         "EP0_END_XFER", "EP0_STALL"
217 };
218
219 #define DMSG(stuff...) printk(KERN_DEBUG "udc: " stuff)
220
221 #ifdef VERBOSE
222 #    define UDC_DEBUG DBG_VERBOSE
223 #else
224 #    define UDC_DEBUG DBG_NORMAL
225 #endif
226
227 static void __attribute__ ((__unused__))
228 dump_udccr(const char *label)
229 {
230         u32     udccr = UDCCR;
231         DMSG("%s %02X =%s%s%s%s%s%s%s%s\n",
232                 label, udccr,
233                 (udccr & UDCCR_REM) ? " rem" : "",
234                 (udccr & UDCCR_RSTIR) ? " rstir" : "",
235                 (udccr & UDCCR_SRM) ? " srm" : "",
236                 (udccr & UDCCR_SUSIR) ? " susir" : "",
237                 (udccr & UDCCR_RESIR) ? " resir" : "",
238                 (udccr & UDCCR_RSM) ? " rsm" : "",
239                 (udccr & UDCCR_UDA) ? " uda" : "",
240                 (udccr & UDCCR_UDE) ? " ude" : "");
241 }
242
243 static void __attribute__ ((__unused__))
244 dump_udccs0(const char *label)
245 {
246         u32             udccs0 = UDCCS0;
247
248         DMSG("%s %s %02X =%s%s%s%s%s%s%s%s\n",
249                 label, state_name[the_controller->ep0state], udccs0,
250                 (udccs0 & UDCCS0_SA) ? " sa" : "",
251                 (udccs0 & UDCCS0_RNE) ? " rne" : "",
252                 (udccs0 & UDCCS0_FST) ? " fst" : "",
253                 (udccs0 & UDCCS0_SST) ? " sst" : "",
254                 (udccs0 & UDCCS0_DRWF) ? " dwrf" : "",
255                 (udccs0 & UDCCS0_FTF) ? " ftf" : "",
256                 (udccs0 & UDCCS0_IPR) ? " ipr" : "",
257                 (udccs0 & UDCCS0_OPR) ? " opr" : "");
258 }
259
260 static void __attribute__ ((__unused__))
261 dump_state(struct pxa2xx_udc *dev)
262 {
263         u32             tmp;
264         unsigned        i;
265
266         DMSG("%s %s, uicr %02X.%02X, usir %02X.%02x, ufnr %02X.%02X\n",
267                 is_usb_connected() ? "host " : "disconnected",
268                 state_name[dev->ep0state],
269                 UICR1, UICR0, USIR1, USIR0, UFNRH, UFNRL);
270         dump_udccr("udccr");
271         if (dev->has_cfr) {
272                 tmp = UDCCFR;
273                 DMSG("udccfr %02X =%s%s\n", tmp,
274                         (tmp & UDCCFR_AREN) ? " aren" : "",
275                         (tmp & UDCCFR_ACM) ? " acm" : "");
276         }
277
278         if (!dev->driver) {
279                 DMSG("no gadget driver bound\n");
280                 return;
281         } else
282                 DMSG("ep0 driver '%s'\n", dev->driver->driver.name);
283
284         if (!is_usb_connected())
285                 return;
286
287         dump_udccs0 ("udccs0");
288         DMSG("ep0 IN %lu/%lu, OUT %lu/%lu\n",
289                 dev->stats.write.bytes, dev->stats.write.ops,
290                 dev->stats.read.bytes, dev->stats.read.ops);
291
292         for (i = 1; i < PXA_UDC_NUM_ENDPOINTS; i++) {
293                 if (dev->ep [i].desc == 0)
294                         continue;
295                 DMSG ("udccs%d = %02x\n", i, *dev->ep->reg_udccs);
296         }
297 }
298
299 #else
300
301 #define DMSG(stuff...)          do{}while(0)
302
303 #define dump_udccr(x)   do{}while(0)
304 #define dump_udccs0(x)  do{}while(0)
305 #define dump_state(x)   do{}while(0)
306
307 #define UDC_DEBUG ((unsigned)0)
308
309 #endif
310
311 #define DBG(lvl, stuff...) do{if ((lvl) <= UDC_DEBUG) DMSG(stuff);}while(0)
312
313 #define WARN(stuff...) printk(KERN_WARNING "udc: " stuff)
314 #define INFO(stuff...) printk(KERN_INFO "udc: " stuff)
315
316
317 #endif /* __LINUX_USB_GADGET_PXA2XX_H */