]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/usb/gadget/net2280.h
usb: gadget: net2280: Add support for PLX USB338X
[karo-tx-linux.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  * Copyright (C) 2014 Ricardo Ribalda - Qtechnology/AS
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  */
16
17 #include <linux/usb/net2280.h>
18 #include <linux/usb/usb338x.h>
19
20 /*-------------------------------------------------------------------------*/
21
22 #ifdef  __KERNEL__
23
24 /* indexed registers [11.10] are accessed indirectly
25  * caller must own the device lock.
26  */
27
28 static inline u32
29 get_idx_reg (struct net2280_regs __iomem *regs, u32 index)
30 {
31         writel (index, &regs->idxaddr);
32         /* NOTE:  synchs device/cpu memory views */
33         return readl (&regs->idxdata);
34 }
35
36 static inline void
37 set_idx_reg (struct net2280_regs __iomem *regs, u32 index, u32 value)
38 {
39         writel (index, &regs->idxaddr);
40         writel (value, &regs->idxdata);
41         /* posted, may not be visible yet */
42 }
43
44 #endif  /* __KERNEL__ */
45
46
47 #define REG_DIAG                0x0
48 #define     RETRY_COUNTER                                       16
49 #define     FORCE_PCI_SERR                                      11
50 #define     FORCE_PCI_INTERRUPT                                 10
51 #define     FORCE_USB_INTERRUPT                                 9
52 #define     FORCE_CPU_INTERRUPT                                 8
53 #define     ILLEGAL_BYTE_ENABLES                                5
54 #define     FAST_TIMES                                          4
55 #define     FORCE_RECEIVE_ERROR                                 2
56 #define     FORCE_TRANSMIT_CRC_ERROR                            0
57 #define REG_FRAME               0x02    /* from last sof */
58 #define REG_CHIPREV             0x03    /* in bcd */
59 #define REG_HS_NAK_RATE         0x0a    /* NAK per N uframes */
60
61 #define CHIPREV_1       0x0100
62 #define CHIPREV_1A      0x0110
63
64 /* DEFECT 7374 */
65 #define DEFECT_7374_NUMBEROF_MAX_WAIT_LOOPS         200
66 #define DEFECT_7374_PROCESSOR_WAIT_TIME             10
67
68 /* ep0 max packet size */
69 #define EP0_SS_MAX_PACKET_SIZE  0x200
70 #define EP0_HS_MAX_PACKET_SIZE  0x40
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 *cfg;
98         struct net2280_ep_regs                  __iomem *regs;
99         struct net2280_dma_regs                 __iomem *dma;
100         struct net2280_dma                      *dummy;
101         struct usb338x_fifo_regs __iomem *fiforegs;
102         dma_addr_t                              td_dma; /* of dummy */
103         struct net2280                          *dev;
104         unsigned long                           irqs;
105         unsigned is_halt:1, dma_started:1;
106
107         /* analogous to a host-side qh */
108         struct list_head                        queue;
109         const struct usb_endpoint_descriptor    *desc;
110         unsigned                                num : 8,
111                                                 fifo_size : 12,
112                                                 in_fifo_validate : 1,
113                                                 out_overflow : 1,
114                                                 stopped : 1,
115                                                 wedged : 1,
116                                                 is_in : 1,
117                                                 is_iso : 1,
118                                                 responded : 1;
119 };
120
121 static inline void allow_status (struct net2280_ep *ep)
122 {
123         /* ep0 only */
124         writel (  (1 << CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE)
125                 | (1 << CLEAR_NAK_OUT_PACKETS)
126                 | (1 << CLEAR_NAK_OUT_PACKETS_MODE)
127                 , &ep->regs->ep_rsp);
128         ep->stopped = 1;
129 }
130
131 static void allow_status_338x(struct net2280_ep *ep)
132 {
133         /*
134          * Control Status Phase Handshake was set by the chip when the setup
135          * packet arrived. While set, the chip automatically NAKs the host's
136          * Status Phase tokens.
137          */
138         writel(1 << CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE, &ep->regs->ep_rsp);
139
140         ep->stopped = 1;
141
142         /* TD 9.9 Halt Endpoint test.  TD 9.22 set feature test. */
143         ep->responded = 0;
144 }
145
146 struct net2280_request {
147         struct usb_request              req;
148         struct net2280_dma              *td;
149         dma_addr_t                      td_dma;
150         struct list_head                queue;
151         unsigned                        mapped : 1,
152                                         valid : 1;
153 };
154
155 struct net2280 {
156         /* each pci device provides one gadget, several endpoints */
157         struct usb_gadget               gadget;
158         spinlock_t                      lock;
159         struct net2280_ep               ep[9];
160         struct usb_gadget_driver        *driver;
161         unsigned                        enabled : 1,
162                                         protocol_stall : 1,
163                                         softconnect : 1,
164                                         got_irq : 1,
165                                         region:1,
166                                         u1_enable:1,
167                                         u2_enable:1,
168                                         ltm_enable:1,
169                                         wakeup_enable:1,
170                                         selfpowered:1,
171                                         addressed_state:1;
172         u16                             chiprev;
173         int enhanced_mode;
174         int n_ep;
175
176         /* pci state used to access those endpoints */
177         struct pci_dev                  *pdev;
178         struct net2280_regs             __iomem *regs;
179         struct net2280_usb_regs         __iomem *usb;
180         struct usb338x_usb_ext_regs     __iomem *usb_ext;
181         struct net2280_pci_regs         __iomem *pci;
182         struct net2280_dma_regs         __iomem *dma;
183         struct net2280_dep_regs         __iomem *dep;
184         struct net2280_ep_regs          __iomem *epregs;
185         struct usb338x_fifo_regs        __iomem *fiforegs;
186         struct usb338x_ll_regs          __iomem *llregs;
187         struct usb338x_ll_lfps_regs     __iomem *ll_lfps_regs;
188         struct usb338x_ll_tsn_regs      __iomem *ll_tsn_regs;
189         struct usb338x_ll_chi_regs      __iomem *ll_chicken_reg;
190         struct usb338x_pl_regs          __iomem *plregs;
191
192         struct pci_pool                 *requests;
193         // statistics...
194 };
195
196 static inline void set_halt (struct net2280_ep *ep)
197 {
198         /* ep0 and bulk/intr endpoints */
199         writel (  (1 << CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE)
200                     /* set NAK_OUT for erratum 0114 */
201                 | ((ep->dev->chiprev == CHIPREV_1) << SET_NAK_OUT_PACKETS)
202                 | (1 << SET_ENDPOINT_HALT)
203                 , &ep->regs->ep_rsp);
204 }
205
206 static inline void clear_halt (struct net2280_ep *ep)
207 {
208         /* ep0 and bulk/intr endpoints */
209         writel (  (1 << CLEAR_ENDPOINT_HALT)
210                 | (1 << CLEAR_ENDPOINT_TOGGLE)
211                     /* unless the gadget driver left a short packet in the
212                      * fifo, this reverses the erratum 0114 workaround.
213                      */
214                 | ((ep->dev->chiprev == CHIPREV_1) << CLEAR_NAK_OUT_PACKETS)
215                 , &ep->regs->ep_rsp);
216 }
217
218 /*
219  * FSM value for Defect 7374 (U1U2 Test) is managed in
220  * chip's SCRATCH register:
221  */
222 #define DEFECT7374_FSM_FIELD    28
223
224 /* Waiting for Control Read:
225  *  - A transition to this state indicates a fresh USB connection,
226  *    before the first Setup Packet. The connection speed is not
227  *    known. Firmware is waiting for the first Control Read.
228  *  - Starting state: This state can be thought of as the FSM's typical
229  *    starting state.
230  *  - Tip: Upon the first SS Control Read the FSM never
231  *    returns to this state.
232  */
233 #define DEFECT7374_FSM_WAITING_FOR_CONTROL_READ (1 << DEFECT7374_FSM_FIELD)
234
235 /* Non-SS Control Read:
236  *  - A transition to this state indicates detection of the first HS
237  *    or FS Control Read.
238  *  - Tip: Upon the first SS Control Read the FSM never
239  *    returns to this state.
240  */
241 #define DEFECT7374_FSM_NON_SS_CONTROL_READ (2 << DEFECT7374_FSM_FIELD)
242
243 /* SS Control Read:
244  *  - A transition to this state indicates detection of the
245  *    first SS Control Read.
246  *  - This state indicates workaround completion. Workarounds no longer
247  *    need to be applied (as long as the chip remains powered up).
248  *  - Tip: Once in this state the FSM state does not change (until
249  *    the chip's power is lost and restored).
250  *  - This can be thought of as the final state of the FSM;
251  *    the FSM 'locks-up' in this state until the chip loses power.
252  */
253 #define DEFECT7374_FSM_SS_CONTROL_READ (3 << DEFECT7374_FSM_FIELD)
254
255 #ifdef USE_RDK_LEDS
256
257 static inline void net2280_led_init (struct net2280 *dev)
258 {
259         /* LED3 (green) is on during USB activity. note erratum 0113. */
260         writel ((1 << GPIO3_LED_SELECT)
261                 | (1 << GPIO3_OUTPUT_ENABLE)
262                 | (1 << GPIO2_OUTPUT_ENABLE)
263                 | (1 << GPIO1_OUTPUT_ENABLE)
264                 | (1 << GPIO0_OUTPUT_ENABLE)
265                 , &dev->regs->gpioctl);
266 }
267
268 /* indicate speed with bi-color LED 0/1 */
269 static inline
270 void net2280_led_speed (struct net2280 *dev, enum usb_device_speed speed)
271 {
272         u32     val = readl (&dev->regs->gpioctl);
273         switch (speed) {
274         case USB_SPEED_SUPER:           /* green + red */
275                 val |= (1 << GPIO0_DATA) | (1 << GPIO1_DATA);
276                 break;
277         case USB_SPEED_HIGH:            /* green */
278                 val &= ~(1 << GPIO0_DATA);
279                 val |= (1 << GPIO1_DATA);
280                 break;
281         case USB_SPEED_FULL:            /* red */
282                 val &= ~(1 << GPIO1_DATA);
283                 val |= (1 << GPIO0_DATA);
284                 break;
285         default:                        /* (off/black) */
286                 val &= ~((1 << GPIO1_DATA) | (1 << GPIO0_DATA));
287                 break;
288         }
289         writel (val, &dev->regs->gpioctl);
290 }
291
292 /* indicate power with LED 2 */
293 static inline void net2280_led_active (struct net2280 *dev, int is_active)
294 {
295         u32     val = readl (&dev->regs->gpioctl);
296
297         // FIXME this LED never seems to turn on.
298         if (is_active)
299                 val |= GPIO2_DATA;
300         else
301                 val &= ~GPIO2_DATA;
302         writel (val, &dev->regs->gpioctl);
303 }
304 static inline void net2280_led_shutdown (struct net2280 *dev)
305 {
306         /* turn off all four GPIO*_DATA bits */
307         writel (readl (&dev->regs->gpioctl) & ~0x0f,
308                         &dev->regs->gpioctl);
309 }
310
311 #else
312
313 #define net2280_led_init(dev)           do { } while (0)
314 #define net2280_led_speed(dev, speed)   do { } while (0)
315 #define net2280_led_shutdown(dev)       do { } while (0)
316
317 #endif
318
319 /*-------------------------------------------------------------------------*/
320
321 #define xprintk(dev,level,fmt,args...) \
322         printk(level "%s %s: " fmt , driver_name , \
323                         pci_name(dev->pdev) , ## args)
324
325 #ifdef DEBUG
326 #undef DEBUG
327 #define DEBUG(dev,fmt,args...) \
328         xprintk(dev , KERN_DEBUG , fmt , ## args)
329 #else
330 #define DEBUG(dev,fmt,args...) \
331         do { } while (0)
332 #endif /* DEBUG */
333
334 #ifdef VERBOSE
335 #define VDEBUG DEBUG
336 #else
337 #define VDEBUG(dev,fmt,args...) \
338         do { } while (0)
339 #endif  /* VERBOSE */
340
341 #define ERROR(dev,fmt,args...) \
342         xprintk(dev , KERN_ERR , fmt , ## args)
343 #define WARNING(dev,fmt,args...) \
344         xprintk(dev , KERN_WARNING , fmt , ## args)
345 #define INFO(dev,fmt,args...) \
346         xprintk(dev , KERN_INFO , fmt , ## args)
347
348 /*-------------------------------------------------------------------------*/
349
350 static inline void set_fifo_bytecount(struct net2280_ep *ep, unsigned count)
351 {
352         if (ep->dev->pdev->vendor == 0x17cc)
353                 writeb(count, 2 + (u8 __iomem *) &ep->regs->ep_cfg);
354         else{
355                 u32 tmp = readl(&ep->cfg->ep_cfg) &
356                                         (~(0x07 << EP_FIFO_BYTE_COUNT));
357                 writel(tmp | (count << EP_FIFO_BYTE_COUNT), &ep->cfg->ep_cfg);
358         }
359 }
360
361 static inline void start_out_naking (struct net2280_ep *ep)
362 {
363         /* NOTE:  hardware races lurk here, and PING protocol issues */
364         writel ((1 << SET_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
365         /* synch with device */
366         readl (&ep->regs->ep_rsp);
367 }
368
369 #ifdef DEBUG
370 static inline void assert_out_naking (struct net2280_ep *ep, const char *where)
371 {
372         u32     tmp = readl (&ep->regs->ep_stat);
373
374         if ((tmp & (1 << NAK_OUT_PACKETS)) == 0) {
375                 DEBUG (ep->dev, "%s %s %08x !NAK\n",
376                                 ep->ep.name, where, tmp);
377                 writel ((1 << SET_NAK_OUT_PACKETS),
378                         &ep->regs->ep_rsp);
379         }
380 }
381 #define ASSERT_OUT_NAKING(ep) assert_out_naking(ep,__func__)
382 #else
383 #define ASSERT_OUT_NAKING(ep) do {} while (0)
384 #endif
385
386 static inline void stop_out_naking (struct net2280_ep *ep)
387 {
388         u32     tmp;
389
390         tmp = readl (&ep->regs->ep_stat);
391         if ((tmp & (1 << NAK_OUT_PACKETS)) != 0)
392                 writel ((1 << CLEAR_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
393 }
394
395 #endif  /* __KERNEL__ */