blob: a257516abbd6f78da1a22b871e0bc48df7e44e59 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +02009 * Copyright (C) 2014 Ricardo Ribalda - Qtechnology/AS
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 */
16
Pete Zaitcev9fc48312006-04-02 10:21:26 -080017#include <linux/usb/net2280.h>
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +020018#include <linux/usb/usb338x.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20/*-------------------------------------------------------------------------*/
21
22#ifdef __KERNEL__
23
24/* indexed registers [11.10] are accessed indirectly
25 * caller must own the device lock.
26 */
27
28static inline u32
29get_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
36static inline void
37set_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
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +020064/* DEFECT 7374 */
65#define DEFECT_7374_NUMBEROF_MAX_WAIT_LOOPS 200
66#define DEFECT_7374_PROCESSOR_WAIT_TIME 10
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +020068/* ep0 max packet size */
69#define EP0_SS_MAX_PACKET_SIZE 0x200
70#define EP0_HS_MAX_PACKET_SIZE 0x40
71#ifdef __KERNEL__
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73/*-------------------------------------------------------------------------*/
74
75/* [8.3] for scatter/gather i/o
76 * use struct net2280_dma_regs bitfields
77 */
78struct net2280_dma {
79 __le32 dmacount;
80 __le32 dmaaddr; /* the buffer */
81 __le32 dmadesc; /* next dma descriptor */
82 __le32 _reserved;
83} __attribute__ ((aligned (16)));
84
85/*-------------------------------------------------------------------------*/
86
87/* DRIVER DATA STRUCTURES and UTILITIES */
88
89struct net2280_ep {
90 struct usb_ep ep;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +020091 struct net2280_ep_regs __iomem *cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 struct net2280_ep_regs __iomem *regs;
93 struct net2280_dma_regs __iomem *dma;
94 struct net2280_dma *dummy;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +020095 struct usb338x_fifo_regs __iomem *fiforegs;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 dma_addr_t td_dma; /* of dummy */
97 struct net2280 *dev;
98 unsigned long irqs;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +020099 unsigned is_halt:1, dma_started:1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101 /* analogous to a host-side qh */
102 struct list_head queue;
103 const struct usb_endpoint_descriptor *desc;
104 unsigned num : 8,
105 fifo_size : 12,
106 in_fifo_validate : 1,
107 out_overflow : 1,
108 stopped : 1,
Alan Stern80661342008-08-14 15:49:11 -0400109 wedged : 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 is_in : 1,
Alan Stern1f26e282006-11-16 10:16:00 -0500111 is_iso : 1,
112 responded : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113};
114
115static inline void allow_status (struct net2280_ep *ep)
116{
117 /* ep0 only */
118 writel ( (1 << CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE)
119 | (1 << CLEAR_NAK_OUT_PACKETS)
120 | (1 << CLEAR_NAK_OUT_PACKETS_MODE)
121 , &ep->regs->ep_rsp);
122 ep->stopped = 1;
123}
124
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200125static void allow_status_338x(struct net2280_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200127 /*
128 * Control Status Phase Handshake was set by the chip when the setup
129 * packet arrived. While set, the chip automatically NAKs the host's
130 * Status Phase tokens.
131 */
132 writel(1 << CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE, &ep->regs->ep_rsp);
133
134 ep->stopped = 1;
135
136 /* TD 9.9 Halt Endpoint test. TD 9.22 set feature test. */
137 ep->responded = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
139
140struct net2280_request {
141 struct usb_request req;
142 struct net2280_dma *td;
143 dma_addr_t td_dma;
144 struct list_head queue;
145 unsigned mapped : 1,
146 valid : 1;
147};
148
149struct net2280 {
150 /* each pci device provides one gadget, several endpoints */
151 struct usb_gadget gadget;
152 spinlock_t lock;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200153 struct net2280_ep ep[9];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 struct usb_gadget_driver *driver;
155 unsigned enabled : 1,
156 protocol_stall : 1,
157 softconnect : 1,
158 got_irq : 1,
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200159 region:1,
160 u1_enable:1,
161 u2_enable:1,
162 ltm_enable:1,
163 wakeup_enable:1,
164 selfpowered:1,
165 addressed_state:1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 u16 chiprev;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200167 int enhanced_mode;
168 int n_ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 /* pci state used to access those endpoints */
171 struct pci_dev *pdev;
172 struct net2280_regs __iomem *regs;
173 struct net2280_usb_regs __iomem *usb;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200174 struct usb338x_usb_ext_regs __iomem *usb_ext;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 struct net2280_pci_regs __iomem *pci;
176 struct net2280_dma_regs __iomem *dma;
177 struct net2280_dep_regs __iomem *dep;
178 struct net2280_ep_regs __iomem *epregs;
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200179 struct usb338x_fifo_regs __iomem *fiforegs;
180 struct usb338x_ll_regs __iomem *llregs;
181 struct usb338x_ll_lfps_regs __iomem *ll_lfps_regs;
182 struct usb338x_ll_tsn_regs __iomem *ll_tsn_regs;
183 struct usb338x_ll_chi_regs __iomem *ll_chicken_reg;
184 struct usb338x_pl_regs __iomem *plregs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186 struct pci_pool *requests;
187 // statistics...
188};
189
190static inline void set_halt (struct net2280_ep *ep)
191{
192 /* ep0 and bulk/intr endpoints */
193 writel ( (1 << CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE)
194 /* set NAK_OUT for erratum 0114 */
195 | ((ep->dev->chiprev == CHIPREV_1) << SET_NAK_OUT_PACKETS)
196 | (1 << SET_ENDPOINT_HALT)
197 , &ep->regs->ep_rsp);
198}
199
200static inline void clear_halt (struct net2280_ep *ep)
201{
202 /* ep0 and bulk/intr endpoints */
203 writel ( (1 << CLEAR_ENDPOINT_HALT)
204 | (1 << CLEAR_ENDPOINT_TOGGLE)
205 /* unless the gadget driver left a short packet in the
206 * fifo, this reverses the erratum 0114 workaround.
207 */
208 | ((ep->dev->chiprev == CHIPREV_1) << CLEAR_NAK_OUT_PACKETS)
209 , &ep->regs->ep_rsp);
210}
211
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200212/*
213 * FSM value for Defect 7374 (U1U2 Test) is managed in
214 * chip's SCRATCH register:
215 */
216#define DEFECT7374_FSM_FIELD 28
217
218/* Waiting for Control Read:
219 * - A transition to this state indicates a fresh USB connection,
220 * before the first Setup Packet. The connection speed is not
221 * known. Firmware is waiting for the first Control Read.
222 * - Starting state: This state can be thought of as the FSM's typical
223 * starting state.
224 * - Tip: Upon the first SS Control Read the FSM never
225 * returns to this state.
226 */
227#define DEFECT7374_FSM_WAITING_FOR_CONTROL_READ (1 << DEFECT7374_FSM_FIELD)
228
229/* Non-SS Control Read:
230 * - A transition to this state indicates detection of the first HS
231 * or FS Control Read.
232 * - Tip: Upon the first SS Control Read the FSM never
233 * returns to this state.
234 */
235#define DEFECT7374_FSM_NON_SS_CONTROL_READ (2 << DEFECT7374_FSM_FIELD)
236
237/* SS Control Read:
238 * - A transition to this state indicates detection of the
239 * first SS Control Read.
240 * - This state indicates workaround completion. Workarounds no longer
241 * need to be applied (as long as the chip remains powered up).
242 * - Tip: Once in this state the FSM state does not change (until
243 * the chip's power is lost and restored).
244 * - This can be thought of as the final state of the FSM;
245 * the FSM 'locks-up' in this state until the chip loses power.
246 */
247#define DEFECT7374_FSM_SS_CONTROL_READ (3 << DEFECT7374_FSM_FIELD)
248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249#ifdef USE_RDK_LEDS
250
251static inline void net2280_led_init (struct net2280 *dev)
252{
253 /* LED3 (green) is on during USB activity. note erratum 0113. */
254 writel ((1 << GPIO3_LED_SELECT)
255 | (1 << GPIO3_OUTPUT_ENABLE)
256 | (1 << GPIO2_OUTPUT_ENABLE)
257 | (1 << GPIO1_OUTPUT_ENABLE)
258 | (1 << GPIO0_OUTPUT_ENABLE)
259 , &dev->regs->gpioctl);
260}
261
262/* indicate speed with bi-color LED 0/1 */
263static inline
264void net2280_led_speed (struct net2280 *dev, enum usb_device_speed speed)
265{
266 u32 val = readl (&dev->regs->gpioctl);
267 switch (speed) {
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200268 case USB_SPEED_SUPER: /* green + red */
269 val |= (1 << GPIO0_DATA) | (1 << GPIO1_DATA);
270 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 case USB_SPEED_HIGH: /* green */
272 val &= ~(1 << GPIO0_DATA);
273 val |= (1 << GPIO1_DATA);
274 break;
275 case USB_SPEED_FULL: /* red */
276 val &= ~(1 << GPIO1_DATA);
277 val |= (1 << GPIO0_DATA);
278 break;
279 default: /* (off/black) */
280 val &= ~((1 << GPIO1_DATA) | (1 << GPIO0_DATA));
281 break;
282 }
283 writel (val, &dev->regs->gpioctl);
284}
285
286/* indicate power with LED 2 */
287static inline void net2280_led_active (struct net2280 *dev, int is_active)
288{
289 u32 val = readl (&dev->regs->gpioctl);
290
291 // FIXME this LED never seems to turn on.
292 if (is_active)
293 val |= GPIO2_DATA;
294 else
295 val &= ~GPIO2_DATA;
296 writel (val, &dev->regs->gpioctl);
297}
298static inline void net2280_led_shutdown (struct net2280 *dev)
299{
300 /* turn off all four GPIO*_DATA bits */
301 writel (readl (&dev->regs->gpioctl) & ~0x0f,
302 &dev->regs->gpioctl);
303}
304
305#else
306
307#define net2280_led_init(dev) do { } while (0)
308#define net2280_led_speed(dev, speed) do { } while (0)
309#define net2280_led_shutdown(dev) do { } while (0)
310
311#endif
312
313/*-------------------------------------------------------------------------*/
314
315#define xprintk(dev,level,fmt,args...) \
316 printk(level "%s %s: " fmt , driver_name , \
317 pci_name(dev->pdev) , ## args)
318
319#ifdef DEBUG
320#undef DEBUG
321#define DEBUG(dev,fmt,args...) \
322 xprintk(dev , KERN_DEBUG , fmt , ## args)
323#else
324#define DEBUG(dev,fmt,args...) \
325 do { } while (0)
326#endif /* DEBUG */
327
328#ifdef VERBOSE
329#define VDEBUG DEBUG
330#else
331#define VDEBUG(dev,fmt,args...) \
332 do { } while (0)
333#endif /* VERBOSE */
334
335#define ERROR(dev,fmt,args...) \
336 xprintk(dev , KERN_ERR , fmt , ## args)
Arjan van de Venb6c63932008-07-25 01:45:52 -0700337#define WARNING(dev,fmt,args...) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 xprintk(dev , KERN_WARNING , fmt , ## args)
339#define INFO(dev,fmt,args...) \
340 xprintk(dev , KERN_INFO , fmt , ## args)
341
342/*-------------------------------------------------------------------------*/
343
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200344static inline void set_fifo_bytecount(struct net2280_ep *ep, unsigned count)
345{
346 if (ep->dev->pdev->vendor == 0x17cc)
347 writeb(count, 2 + (u8 __iomem *) &ep->regs->ep_cfg);
348 else{
349 u32 tmp = readl(&ep->cfg->ep_cfg) &
350 (~(0x07 << EP_FIFO_BYTE_COUNT));
351 writel(tmp | (count << EP_FIFO_BYTE_COUNT), &ep->cfg->ep_cfg);
352 }
353}
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355static inline void start_out_naking (struct net2280_ep *ep)
356{
357 /* NOTE: hardware races lurk here, and PING protocol issues */
358 writel ((1 << SET_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
359 /* synch with device */
360 readl (&ep->regs->ep_rsp);
361}
362
363#ifdef DEBUG
364static inline void assert_out_naking (struct net2280_ep *ep, const char *where)
365{
366 u32 tmp = readl (&ep->regs->ep_stat);
367
368 if ((tmp & (1 << NAK_OUT_PACKETS)) == 0) {
369 DEBUG (ep->dev, "%s %s %08x !NAK\n",
370 ep->ep.name, where, tmp);
371 writel ((1 << SET_NAK_OUT_PACKETS),
372 &ep->regs->ep_rsp);
373 }
374}
Harvey Harrison441b62c2008-03-03 16:08:34 -0800375#define ASSERT_OUT_NAKING(ep) assert_out_naking(ep,__func__)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376#else
377#define ASSERT_OUT_NAKING(ep) do {} while (0)
378#endif
379
380static inline void stop_out_naking (struct net2280_ep *ep)
381{
382 u32 tmp;
383
384 tmp = readl (&ep->regs->ep_stat);
385 if ((tmp & (1 << NAK_OUT_PACKETS)) != 0)
386 writel ((1 << CLEAR_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
387}
388
Ricardo Ribalda Delgadoadc82f72014-05-20 18:30:03 +0200389
390static inline void set_max_speed(struct net2280_ep *ep, u32 max)
391{
392 u32 reg;
393 static const u32 ep_enhanced[9] = { 0x10, 0x60, 0x30, 0x80,
394 0x50, 0x20, 0x70, 0x40, 0x90 };
395
396 if (ep->dev->enhanced_mode)
397 reg = ep_enhanced[ep->num];
398 else{
399 reg = (ep->num + 1) * 0x10;
400 if (ep->dev->gadget.speed != USB_SPEED_HIGH)
401 reg += 1;
402 }
403
404 set_idx_reg(ep->dev->regs, reg, max);
405}
406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407#endif /* __KERNEL__ */