Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[linux-2.6.git] / drivers / usb / gadget / net2280.h
1 /*
2  * NetChip 2280 high/full speed USB device controller.
3  * Unlike many such controllers, this one talks PCI.
4  */
5
6 /*
7  * Copyright (C) 2002 NetChip Technology, Inc. (http://www.netchip.com)
8  * Copyright (C) 2003 David Brownell
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23  */
24
25 #include <linux/usb/net2280.h>
26
27 /*-------------------------------------------------------------------------*/
28
29 #ifdef  __KERNEL__
30
31 /* indexed registers [11.10] are accessed indirectly
32  * caller must own the device lock.
33  */
34
35 static inline u32
36 get_idx_reg (struct net2280_regs __iomem *regs, u32 index)
37 {
38         writel (index, &regs->idxaddr);
39         /* NOTE:  synchs device/cpu memory views */
40         return readl (&regs->idxdata);
41 }
42
43 static inline void
44 set_idx_reg (struct net2280_regs __iomem *regs, u32 index, u32 value)
45 {
46         writel (index, &regs->idxaddr);
47         writel (value, &regs->idxdata);
48         /* posted, may not be visible yet */
49 }
50
51 #endif  /* __KERNEL__ */
52
53
54 #define REG_DIAG                0x0
55 #define     RETRY_COUNTER                                       16
56 #define     FORCE_PCI_SERR                                      11
57 #define     FORCE_PCI_INTERRUPT                                 10
58 #define     FORCE_USB_INTERRUPT                                 9
59 #define     FORCE_CPU_INTERRUPT                                 8
60 #define     ILLEGAL_BYTE_ENABLES                                5
61 #define     FAST_TIMES                                          4
62 #define     FORCE_RECEIVE_ERROR                                 2
63 #define     FORCE_TRANSMIT_CRC_ERROR                            0
64 #define REG_FRAME               0x02    /* from last sof */
65 #define REG_CHIPREV             0x03    /* in bcd */
66 #define REG_HS_NAK_RATE         0x0a    /* NAK per N uframes */
67
68 #define CHIPREV_1       0x0100
69 #define CHIPREV_1A      0x0110
70
71 #ifdef  __KERNEL__
72
73 /* ep a-f highspeed and fullspeed maxpacket, addresses
74  * computed from ep->num
75  */
76 #define REG_EP_MAXPKT(dev,num) (((num) + 1) * 0x10 + \
77                 (((dev)->gadget.speed == USB_SPEED_HIGH) ? 0 : 1))
78
79 /*-------------------------------------------------------------------------*/
80
81 /* [8.3] for scatter/gather i/o
82  * use struct net2280_dma_regs bitfields
83  */
84 struct net2280_dma {
85         __le32          dmacount;
86         __le32          dmaaddr;                /* the buffer */
87         __le32          dmadesc;                /* next dma descriptor */
88         __le32          _reserved;
89 } __attribute__ ((aligned (16)));
90
91 /*-------------------------------------------------------------------------*/
92
93 /* DRIVER DATA STRUCTURES and UTILITIES */
94
95 struct net2280_ep {
96         struct usb_ep                           ep;
97         struct net2280_ep_regs                  __iomem *regs;
98         struct net2280_dma_regs                 __iomem *dma;
99         struct net2280_dma                      *dummy;
100         dma_addr_t                              td_dma; /* of dummy */
101         struct net2280                          *dev;
102         unsigned long                           irqs;
103
104         /* analogous to a host-side qh */
105         struct list_head                        queue;
106         const struct usb_endpoint_descriptor    *desc;
107         unsigned                                num : 8,
108                                                 fifo_size : 12,
109                                                 in_fifo_validate : 1,
110                                                 out_overflow : 1,
111                                                 stopped : 1,
112                                                 is_in : 1,
113                                                 is_iso : 1;
114 };
115
116 static inline void allow_status (struct net2280_ep *ep)
117 {
118         /* ep0 only */
119         writel (  (1 << CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE)
120                 | (1 << CLEAR_NAK_OUT_PACKETS)
121                 | (1 << CLEAR_NAK_OUT_PACKETS_MODE)
122                 , &ep->regs->ep_rsp);
123         ep->stopped = 1;
124 }
125
126 /* count (<= 4) bytes in the next fifo write will be valid */
127 static inline void set_fifo_bytecount (struct net2280_ep *ep, unsigned count)
128 {
129         writeb (count, 2 + (u8 __iomem *) &ep->regs->ep_cfg);
130 }
131
132 struct net2280_request {
133         struct usb_request              req;
134         struct net2280_dma              *td;
135         dma_addr_t                      td_dma;
136         struct list_head                queue;
137         unsigned                        mapped : 1,
138                                         valid : 1;
139 };
140
141 struct net2280 {
142         /* each pci device provides one gadget, several endpoints */
143         struct usb_gadget               gadget;
144         spinlock_t                      lock;
145         struct net2280_ep               ep [7];
146         struct usb_gadget_driver        *driver;
147         unsigned                        enabled : 1,
148                                         protocol_stall : 1,
149                                         softconnect : 1,
150                                         got_irq : 1,
151                                         region : 1;
152         u16                             chiprev;
153
154         /* pci state used to access those endpoints */
155         struct pci_dev                  *pdev;
156         struct net2280_regs             __iomem *regs;
157         struct net2280_usb_regs         __iomem *usb;
158         struct net2280_pci_regs         __iomem *pci;
159         struct net2280_dma_regs         __iomem *dma;
160         struct net2280_dep_regs         __iomem *dep;
161         struct net2280_ep_regs          __iomem *epregs;
162
163         struct pci_pool                 *requests;
164         // statistics...
165 };
166
167 static inline void set_halt (struct net2280_ep *ep)
168 {
169         /* ep0 and bulk/intr endpoints */
170         writel (  (1 << CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE)
171                     /* set NAK_OUT for erratum 0114 */
172                 | ((ep->dev->chiprev == CHIPREV_1) << SET_NAK_OUT_PACKETS)
173                 | (1 << SET_ENDPOINT_HALT)
174                 , &ep->regs->ep_rsp);
175 }
176
177 static inline void clear_halt (struct net2280_ep *ep)
178 {
179         /* ep0 and bulk/intr endpoints */
180         writel (  (1 << CLEAR_ENDPOINT_HALT)
181                 | (1 << CLEAR_ENDPOINT_TOGGLE)
182                     /* unless the gadget driver left a short packet in the
183                      * fifo, this reverses the erratum 0114 workaround.
184                      */
185                 | ((ep->dev->chiprev == CHIPREV_1) << CLEAR_NAK_OUT_PACKETS)
186                 , &ep->regs->ep_rsp);
187 }
188
189 #ifdef USE_RDK_LEDS
190
191 static inline void net2280_led_init (struct net2280 *dev)
192 {
193         /* LED3 (green) is on during USB activity. note erratum 0113. */
194         writel ((1 << GPIO3_LED_SELECT)
195                 | (1 << GPIO3_OUTPUT_ENABLE)
196                 | (1 << GPIO2_OUTPUT_ENABLE)
197                 | (1 << GPIO1_OUTPUT_ENABLE)
198                 | (1 << GPIO0_OUTPUT_ENABLE)
199                 , &dev->regs->gpioctl);
200 }
201
202 /* indicate speed with bi-color LED 0/1 */
203 static inline
204 void net2280_led_speed (struct net2280 *dev, enum usb_device_speed speed)
205 {
206         u32     val = readl (&dev->regs->gpioctl);
207         switch (speed) {
208         case USB_SPEED_HIGH:            /* green */
209                 val &= ~(1 << GPIO0_DATA);
210                 val |= (1 << GPIO1_DATA);
211                 break;
212         case USB_SPEED_FULL:            /* red */
213                 val &= ~(1 << GPIO1_DATA);
214                 val |= (1 << GPIO0_DATA);
215                 break;
216         default:                        /* (off/black) */
217                 val &= ~((1 << GPIO1_DATA) | (1 << GPIO0_DATA));
218                 break;
219         }
220         writel (val, &dev->regs->gpioctl);
221 }
222
223 /* indicate power with LED 2 */
224 static inline void net2280_led_active (struct net2280 *dev, int is_active)
225 {
226         u32     val = readl (&dev->regs->gpioctl);
227
228         // FIXME this LED never seems to turn on.
229         if (is_active)
230                 val |= GPIO2_DATA;
231         else
232                 val &= ~GPIO2_DATA;
233         writel (val, &dev->regs->gpioctl);
234 }
235 static inline void net2280_led_shutdown (struct net2280 *dev)
236 {
237         /* turn off all four GPIO*_DATA bits */
238         writel (readl (&dev->regs->gpioctl) & ~0x0f,
239                         &dev->regs->gpioctl);
240 }
241
242 #else
243
244 #define net2280_led_init(dev)           do { } while (0)
245 #define net2280_led_speed(dev, speed)   do { } while (0)
246 #define net2280_led_shutdown(dev)       do { } while (0)
247
248 #endif
249
250 /*-------------------------------------------------------------------------*/
251
252 #define xprintk(dev,level,fmt,args...) \
253         printk(level "%s %s: " fmt , driver_name , \
254                         pci_name(dev->pdev) , ## args)
255
256 #ifdef DEBUG
257 #undef DEBUG
258 #define DEBUG(dev,fmt,args...) \
259         xprintk(dev , KERN_DEBUG , fmt , ## args)
260 #else
261 #define DEBUG(dev,fmt,args...) \
262         do { } while (0)
263 #endif /* DEBUG */
264
265 #ifdef VERBOSE
266 #define VDEBUG DEBUG
267 #else
268 #define VDEBUG(dev,fmt,args...) \
269         do { } while (0)
270 #endif  /* VERBOSE */
271
272 #define ERROR(dev,fmt,args...) \
273         xprintk(dev , KERN_ERR , fmt , ## args)
274 #define WARN(dev,fmt,args...) \
275         xprintk(dev , KERN_WARNING , fmt , ## args)
276 #define INFO(dev,fmt,args...) \
277         xprintk(dev , KERN_INFO , fmt , ## args)
278
279 /*-------------------------------------------------------------------------*/
280
281 static inline void start_out_naking (struct net2280_ep *ep)
282 {
283         /* NOTE:  hardware races lurk here, and PING protocol issues */
284         writel ((1 << SET_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
285         /* synch with device */
286         readl (&ep->regs->ep_rsp);
287 }
288
289 #ifdef DEBUG
290 static inline void assert_out_naking (struct net2280_ep *ep, const char *where)
291 {
292         u32     tmp = readl (&ep->regs->ep_stat);
293
294         if ((tmp & (1 << NAK_OUT_PACKETS)) == 0) {
295                 DEBUG (ep->dev, "%s %s %08x !NAK\n",
296                                 ep->ep.name, where, tmp);
297                 writel ((1 << SET_NAK_OUT_PACKETS),
298                         &ep->regs->ep_rsp);
299         }
300 }
301 #define ASSERT_OUT_NAKING(ep) assert_out_naking(ep,__FUNCTION__)
302 #else
303 #define ASSERT_OUT_NAKING(ep) do {} while (0)
304 #endif
305
306 static inline void stop_out_naking (struct net2280_ep *ep)
307 {
308         u32     tmp;
309
310         tmp = readl (&ep->regs->ep_stat);
311         if ((tmp & (1 << NAK_OUT_PACKETS)) != 0)
312                 writel ((1 << CLEAR_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
313 }
314
315 #endif  /* __KERNEL__ */