xref: /openbmc/linux/drivers/usb/gadget/udc/net2280.c (revision 68198dca)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Driver for the PLX NET2280 USB device controller.
4  * Specs and errata are available from <http://www.plxtech.com>.
5  *
6  * PLX Technology Inc. (formerly NetChip Technology) supported the
7  * development of this driver.
8  *
9  *
10  * CODE STATUS HIGHLIGHTS
11  *
12  * This driver should work well with most "gadget" drivers, including
13  * the Mass Storage, Serial, and Ethernet/RNDIS gadget drivers
14  * as well as Gadget Zero and Gadgetfs.
15  *
16  * DMA is enabled by default.
17  *
18  * MSI is enabled by default.  The legacy IRQ is used if MSI couldn't
19  * be enabled.
20  *
21  * Note that almost all the errata workarounds here are only needed for
22  * rev1 chips.  Rev1a silicon (0110) fixes almost all of them.
23  */
24 
25 /*
26  * Copyright (C) 2003 David Brownell
27  * Copyright (C) 2003-2005 PLX Technology, Inc.
28  * Copyright (C) 2014 Ricardo Ribalda - Qtechnology/AS
29  *
30  * Modified Seth Levy 2005 PLX Technology, Inc. to provide compatibility
31  *	with 2282 chip
32  *
33  * Modified Ricardo Ribalda Qtechnology AS  to provide compatibility
34  *	with usb 338x chip. Based on PLX driver
35  */
36 
37 #include <linux/module.h>
38 #include <linux/pci.h>
39 #include <linux/dma-mapping.h>
40 #include <linux/kernel.h>
41 #include <linux/delay.h>
42 #include <linux/ioport.h>
43 #include <linux/slab.h>
44 #include <linux/errno.h>
45 #include <linux/init.h>
46 #include <linux/timer.h>
47 #include <linux/list.h>
48 #include <linux/interrupt.h>
49 #include <linux/moduleparam.h>
50 #include <linux/device.h>
51 #include <linux/usb/ch9.h>
52 #include <linux/usb/gadget.h>
53 #include <linux/prefetch.h>
54 #include <linux/io.h>
55 
56 #include <asm/byteorder.h>
57 #include <asm/irq.h>
58 #include <asm/unaligned.h>
59 
60 #define	DRIVER_DESC		"PLX NET228x/USB338x USB Peripheral Controller"
61 #define	DRIVER_VERSION		"2005 Sept 27/v3.0"
62 
63 #define	EP_DONTUSE		13	/* nonzero */
64 
65 #define USE_RDK_LEDS		/* GPIO pins control three LEDs */
66 
67 
68 static const char driver_name[] = "net2280";
69 static const char driver_desc[] = DRIVER_DESC;
70 
71 static const u32 ep_bit[9] = { 0, 17, 2, 19, 4, 1, 18, 3, 20 };
72 static const char ep0name[] = "ep0";
73 
74 #define EP_INFO(_name, _caps) \
75 	{ \
76 		.name = _name, \
77 		.caps = _caps, \
78 	}
79 
80 static const struct {
81 	const char *name;
82 	const struct usb_ep_caps caps;
83 } ep_info_dft[] = { /* Default endpoint configuration */
84 	EP_INFO(ep0name,
85 		USB_EP_CAPS(USB_EP_CAPS_TYPE_CONTROL, USB_EP_CAPS_DIR_ALL)),
86 	EP_INFO("ep-a",
87 		USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL, USB_EP_CAPS_DIR_ALL)),
88 	EP_INFO("ep-b",
89 		USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL, USB_EP_CAPS_DIR_ALL)),
90 	EP_INFO("ep-c",
91 		USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL, USB_EP_CAPS_DIR_ALL)),
92 	EP_INFO("ep-d",
93 		USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL, USB_EP_CAPS_DIR_ALL)),
94 	EP_INFO("ep-e",
95 		USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL, USB_EP_CAPS_DIR_ALL)),
96 	EP_INFO("ep-f",
97 		USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL, USB_EP_CAPS_DIR_ALL)),
98 	EP_INFO("ep-g",
99 		USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL, USB_EP_CAPS_DIR_ALL)),
100 	EP_INFO("ep-h",
101 		USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL, USB_EP_CAPS_DIR_ALL)),
102 }, ep_info_adv[] = { /* Endpoints for usb3380 advance mode */
103 	EP_INFO(ep0name,
104 		USB_EP_CAPS(USB_EP_CAPS_TYPE_CONTROL, USB_EP_CAPS_DIR_ALL)),
105 	EP_INFO("ep1in",
106 		USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL, USB_EP_CAPS_DIR_IN)),
107 	EP_INFO("ep2out",
108 		USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL, USB_EP_CAPS_DIR_OUT)),
109 	EP_INFO("ep3in",
110 		USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL, USB_EP_CAPS_DIR_IN)),
111 	EP_INFO("ep4out",
112 		USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL, USB_EP_CAPS_DIR_OUT)),
113 	EP_INFO("ep1out",
114 		USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL, USB_EP_CAPS_DIR_OUT)),
115 	EP_INFO("ep2in",
116 		USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL, USB_EP_CAPS_DIR_IN)),
117 	EP_INFO("ep3out",
118 		USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL, USB_EP_CAPS_DIR_OUT)),
119 	EP_INFO("ep4in",
120 		USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL, USB_EP_CAPS_DIR_IN)),
121 };
122 
123 #undef EP_INFO
124 
125 /* mode 0 == ep-{a,b,c,d} 1K fifo each
126  * mode 1 == ep-{a,b} 2K fifo each, ep-{c,d} unavailable
127  * mode 2 == ep-a 2K fifo, ep-{b,c} 1K each, ep-d unavailable
128  */
129 static ushort fifo_mode;
130 
131 /* "modprobe net2280 fifo_mode=1" etc */
132 module_param(fifo_mode, ushort, 0644);
133 
134 /* enable_suspend -- When enabled, the driver will respond to
135  * USB suspend requests by powering down the NET2280.  Otherwise,
136  * USB suspend requests will be ignored.  This is acceptable for
137  * self-powered devices
138  */
139 static bool enable_suspend;
140 
141 /* "modprobe net2280 enable_suspend=1" etc */
142 module_param(enable_suspend, bool, 0444);
143 
144 #define	DIR_STRING(bAddress) (((bAddress) & USB_DIR_IN) ? "in" : "out")
145 
146 static char *type_string(u8 bmAttributes)
147 {
148 	switch ((bmAttributes) & USB_ENDPOINT_XFERTYPE_MASK) {
149 	case USB_ENDPOINT_XFER_BULK:	return "bulk";
150 	case USB_ENDPOINT_XFER_ISOC:	return "iso";
151 	case USB_ENDPOINT_XFER_INT:	return "intr";
152 	}
153 	return "control";
154 }
155 
156 #include "net2280.h"
157 
158 #define valid_bit	cpu_to_le32(BIT(VALID_BIT))
159 #define dma_done_ie	cpu_to_le32(BIT(DMA_DONE_INTERRUPT_ENABLE))
160 
161 static void ep_clear_seqnum(struct net2280_ep *ep);
162 static void stop_activity(struct net2280 *dev,
163 					struct usb_gadget_driver *driver);
164 static void ep0_start(struct net2280 *dev);
165 
166 /*-------------------------------------------------------------------------*/
167 static inline void enable_pciirqenb(struct net2280_ep *ep)
168 {
169 	u32 tmp = readl(&ep->dev->regs->pciirqenb0);
170 
171 	if (ep->dev->quirks & PLX_LEGACY)
172 		tmp |= BIT(ep->num);
173 	else
174 		tmp |= BIT(ep_bit[ep->num]);
175 	writel(tmp, &ep->dev->regs->pciirqenb0);
176 
177 	return;
178 }
179 
180 static int
181 net2280_enable(struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
182 {
183 	struct net2280		*dev;
184 	struct net2280_ep	*ep;
185 	u32			max;
186 	u32 tmp = 0;
187 	u32 type;
188 	unsigned long		flags;
189 	static const u32 ep_key[9] = { 1, 0, 1, 0, 1, 1, 0, 1, 0 };
190 	int ret = 0;
191 
192 	ep = container_of(_ep, struct net2280_ep, ep);
193 	if (!_ep || !desc || ep->desc || _ep->name == ep0name ||
194 			desc->bDescriptorType != USB_DT_ENDPOINT) {
195 		pr_err("%s: failed at line=%d\n", __func__, __LINE__);
196 		return -EINVAL;
197 	}
198 	dev = ep->dev;
199 	if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
200 		ret = -ESHUTDOWN;
201 		goto print_err;
202 	}
203 
204 	/* erratum 0119 workaround ties up an endpoint number */
205 	if ((desc->bEndpointAddress & 0x0f) == EP_DONTUSE) {
206 		ret = -EDOM;
207 		goto print_err;
208 	}
209 
210 	if (dev->quirks & PLX_PCIE) {
211 		if ((desc->bEndpointAddress & 0x0f) >= 0x0c) {
212 			ret = -EDOM;
213 			goto print_err;
214 		}
215 		ep->is_in = !!usb_endpoint_dir_in(desc);
216 		if (dev->enhanced_mode && ep->is_in && ep_key[ep->num]) {
217 			ret = -EINVAL;
218 			goto print_err;
219 		}
220 	}
221 
222 	/* sanity check ep-e/ep-f since their fifos are small */
223 	max = usb_endpoint_maxp(desc);
224 	if (ep->num > 4 && max > 64 && (dev->quirks & PLX_LEGACY)) {
225 		ret = -ERANGE;
226 		goto print_err;
227 	}
228 
229 	spin_lock_irqsave(&dev->lock, flags);
230 	_ep->maxpacket = max;
231 	ep->desc = desc;
232 
233 	/* ep_reset() has already been called */
234 	ep->stopped = 0;
235 	ep->wedged = 0;
236 	ep->out_overflow = 0;
237 
238 	/* set speed-dependent max packet; may kick in high bandwidth */
239 	set_max_speed(ep, max);
240 
241 	/* set type, direction, address; reset fifo counters */
242 	writel(BIT(FIFO_FLUSH), &ep->regs->ep_stat);
243 
244 	if ((dev->quirks & PLX_PCIE) && dev->enhanced_mode) {
245 		tmp = readl(&ep->cfg->ep_cfg);
246 		/* If USB ep number doesn't match hardware ep number */
247 		if ((tmp & 0xf) != usb_endpoint_num(desc)) {
248 			ret = -EINVAL;
249 			spin_unlock_irqrestore(&dev->lock, flags);
250 			goto print_err;
251 		}
252 		if (ep->is_in)
253 			tmp &= ~USB3380_EP_CFG_MASK_IN;
254 		else
255 			tmp &= ~USB3380_EP_CFG_MASK_OUT;
256 	}
257 	type = (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK);
258 	if (type == USB_ENDPOINT_XFER_INT) {
259 		/* erratum 0105 workaround prevents hs NYET */
260 		if (dev->chiprev == 0100 &&
261 				dev->gadget.speed == USB_SPEED_HIGH &&
262 				!(desc->bEndpointAddress & USB_DIR_IN))
263 			writel(BIT(CLEAR_NAK_OUT_PACKETS_MODE),
264 				&ep->regs->ep_rsp);
265 	} else if (type == USB_ENDPOINT_XFER_BULK) {
266 		/* catch some particularly blatant driver bugs */
267 		if ((dev->gadget.speed == USB_SPEED_SUPER && max != 1024) ||
268 		    (dev->gadget.speed == USB_SPEED_HIGH && max != 512) ||
269 		    (dev->gadget.speed == USB_SPEED_FULL && max > 64)) {
270 			spin_unlock_irqrestore(&dev->lock, flags);
271 			ret = -ERANGE;
272 			goto print_err;
273 		}
274 	}
275 	ep->is_iso = (type == USB_ENDPOINT_XFER_ISOC);
276 	/* Enable this endpoint */
277 	if (dev->quirks & PLX_LEGACY) {
278 		tmp |= type << ENDPOINT_TYPE;
279 		tmp |= desc->bEndpointAddress;
280 		/* default full fifo lines */
281 		tmp |= (4 << ENDPOINT_BYTE_COUNT);
282 		tmp |= BIT(ENDPOINT_ENABLE);
283 		ep->is_in = (tmp & USB_DIR_IN) != 0;
284 	} else {
285 		/* In Legacy mode, only OUT endpoints are used */
286 		if (dev->enhanced_mode && ep->is_in) {
287 			tmp |= type << IN_ENDPOINT_TYPE;
288 			tmp |= BIT(IN_ENDPOINT_ENABLE);
289 		} else {
290 			tmp |= type << OUT_ENDPOINT_TYPE;
291 			tmp |= BIT(OUT_ENDPOINT_ENABLE);
292 			tmp |= (ep->is_in << ENDPOINT_DIRECTION);
293 		}
294 
295 		tmp |= (4 << ENDPOINT_BYTE_COUNT);
296 		if (!dev->enhanced_mode)
297 			tmp |= usb_endpoint_num(desc);
298 		tmp |= (ep->ep.maxburst << MAX_BURST_SIZE);
299 	}
300 
301 	/* Make sure all the registers are written before ep_rsp*/
302 	wmb();
303 
304 	/* for OUT transfers, block the rx fifo until a read is posted */
305 	if (!ep->is_in)
306 		writel(BIT(SET_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
307 	else if (!(dev->quirks & PLX_2280)) {
308 		/* Added for 2282, Don't use nak packets on an in endpoint,
309 		 * this was ignored on 2280
310 		 */
311 		writel(BIT(CLEAR_NAK_OUT_PACKETS) |
312 			BIT(CLEAR_NAK_OUT_PACKETS_MODE), &ep->regs->ep_rsp);
313 	}
314 
315 	if (dev->quirks & PLX_PCIE)
316 		ep_clear_seqnum(ep);
317 	writel(tmp, &ep->cfg->ep_cfg);
318 
319 	/* enable irqs */
320 	if (!ep->dma) {				/* pio, per-packet */
321 		enable_pciirqenb(ep);
322 
323 		tmp = BIT(DATA_PACKET_RECEIVED_INTERRUPT_ENABLE) |
324 			BIT(DATA_PACKET_TRANSMITTED_INTERRUPT_ENABLE);
325 		if (dev->quirks & PLX_2280)
326 			tmp |= readl(&ep->regs->ep_irqenb);
327 		writel(tmp, &ep->regs->ep_irqenb);
328 	} else {				/* dma, per-request */
329 		tmp = BIT((8 + ep->num));	/* completion */
330 		tmp |= readl(&dev->regs->pciirqenb1);
331 		writel(tmp, &dev->regs->pciirqenb1);
332 
333 		/* for short OUT transfers, dma completions can't
334 		 * advance the queue; do it pio-style, by hand.
335 		 * NOTE erratum 0112 workaround #2
336 		 */
337 		if ((desc->bEndpointAddress & USB_DIR_IN) == 0) {
338 			tmp = BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT_ENABLE);
339 			writel(tmp, &ep->regs->ep_irqenb);
340 
341 			enable_pciirqenb(ep);
342 		}
343 	}
344 
345 	tmp = desc->bEndpointAddress;
346 	ep_dbg(dev, "enabled %s (ep%d%s-%s) %s max %04x\n",
347 		_ep->name, tmp & 0x0f, DIR_STRING(tmp),
348 		type_string(desc->bmAttributes),
349 		ep->dma ? "dma" : "pio", max);
350 
351 	/* pci writes may still be posted */
352 	spin_unlock_irqrestore(&dev->lock, flags);
353 	return ret;
354 
355 print_err:
356 	dev_err(&ep->dev->pdev->dev, "%s: error=%d\n", __func__, ret);
357 	return ret;
358 }
359 
360 static int handshake(u32 __iomem *ptr, u32 mask, u32 done, int usec)
361 {
362 	u32	result;
363 
364 	do {
365 		result = readl(ptr);
366 		if (result == ~(u32)0)		/* "device unplugged" */
367 			return -ENODEV;
368 		result &= mask;
369 		if (result == done)
370 			return 0;
371 		udelay(1);
372 		usec--;
373 	} while (usec > 0);
374 	return -ETIMEDOUT;
375 }
376 
377 static const struct usb_ep_ops net2280_ep_ops;
378 
379 static void ep_reset_228x(struct net2280_regs __iomem *regs,
380 			  struct net2280_ep *ep)
381 {
382 	u32		tmp;
383 
384 	ep->desc = NULL;
385 	INIT_LIST_HEAD(&ep->queue);
386 
387 	usb_ep_set_maxpacket_limit(&ep->ep, ~0);
388 	ep->ep.ops = &net2280_ep_ops;
389 
390 	/* disable the dma, irqs, endpoint... */
391 	if (ep->dma) {
392 		writel(0, &ep->dma->dmactl);
393 		writel(BIT(DMA_SCATTER_GATHER_DONE_INTERRUPT) |
394 			BIT(DMA_TRANSACTION_DONE_INTERRUPT) |
395 			BIT(DMA_ABORT),
396 			&ep->dma->dmastat);
397 
398 		tmp = readl(&regs->pciirqenb0);
399 		tmp &= ~BIT(ep->num);
400 		writel(tmp, &regs->pciirqenb0);
401 	} else {
402 		tmp = readl(&regs->pciirqenb1);
403 		tmp &= ~BIT((8 + ep->num));	/* completion */
404 		writel(tmp, &regs->pciirqenb1);
405 	}
406 	writel(0, &ep->regs->ep_irqenb);
407 
408 	/* init to our chosen defaults, notably so that we NAK OUT
409 	 * packets until the driver queues a read (+note erratum 0112)
410 	 */
411 	if (!ep->is_in || (ep->dev->quirks & PLX_2280)) {
412 		tmp = BIT(SET_NAK_OUT_PACKETS_MODE) |
413 		BIT(SET_NAK_OUT_PACKETS) |
414 		BIT(CLEAR_EP_HIDE_STATUS_PHASE) |
415 		BIT(CLEAR_INTERRUPT_MODE);
416 	} else {
417 		/* added for 2282 */
418 		tmp = BIT(CLEAR_NAK_OUT_PACKETS_MODE) |
419 		BIT(CLEAR_NAK_OUT_PACKETS) |
420 		BIT(CLEAR_EP_HIDE_STATUS_PHASE) |
421 		BIT(CLEAR_INTERRUPT_MODE);
422 	}
423 
424 	if (ep->num != 0) {
425 		tmp |= BIT(CLEAR_ENDPOINT_TOGGLE) |
426 			BIT(CLEAR_ENDPOINT_HALT);
427 	}
428 	writel(tmp, &ep->regs->ep_rsp);
429 
430 	/* scrub most status bits, and flush any fifo state */
431 	if (ep->dev->quirks & PLX_2280)
432 		tmp = BIT(FIFO_OVERFLOW) |
433 			BIT(FIFO_UNDERFLOW);
434 	else
435 		tmp = 0;
436 
437 	writel(tmp | BIT(TIMEOUT) |
438 		BIT(USB_STALL_SENT) |
439 		BIT(USB_IN_NAK_SENT) |
440 		BIT(USB_IN_ACK_RCVD) |
441 		BIT(USB_OUT_PING_NAK_SENT) |
442 		BIT(USB_OUT_ACK_SENT) |
443 		BIT(FIFO_FLUSH) |
444 		BIT(SHORT_PACKET_OUT_DONE_INTERRUPT) |
445 		BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT) |
446 		BIT(DATA_PACKET_RECEIVED_INTERRUPT) |
447 		BIT(DATA_PACKET_TRANSMITTED_INTERRUPT) |
448 		BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
449 		BIT(DATA_IN_TOKEN_INTERRUPT),
450 		&ep->regs->ep_stat);
451 
452 	/* fifo size is handled separately */
453 }
454 
455 static void ep_reset_338x(struct net2280_regs __iomem *regs,
456 					struct net2280_ep *ep)
457 {
458 	u32 tmp, dmastat;
459 
460 	ep->desc = NULL;
461 	INIT_LIST_HEAD(&ep->queue);
462 
463 	usb_ep_set_maxpacket_limit(&ep->ep, ~0);
464 	ep->ep.ops = &net2280_ep_ops;
465 
466 	/* disable the dma, irqs, endpoint... */
467 	if (ep->dma) {
468 		writel(0, &ep->dma->dmactl);
469 		writel(BIT(DMA_ABORT_DONE_INTERRUPT) |
470 		       BIT(DMA_PAUSE_DONE_INTERRUPT) |
471 		       BIT(DMA_SCATTER_GATHER_DONE_INTERRUPT) |
472 		       BIT(DMA_TRANSACTION_DONE_INTERRUPT),
473 		       /* | BIT(DMA_ABORT), */
474 		       &ep->dma->dmastat);
475 
476 		dmastat = readl(&ep->dma->dmastat);
477 		if (dmastat == 0x5002) {
478 			ep_warn(ep->dev, "The dmastat return = %x!!\n",
479 			       dmastat);
480 			writel(0x5a, &ep->dma->dmastat);
481 		}
482 
483 		tmp = readl(&regs->pciirqenb0);
484 		tmp &= ~BIT(ep_bit[ep->num]);
485 		writel(tmp, &regs->pciirqenb0);
486 	} else {
487 		if (ep->num < 5) {
488 			tmp = readl(&regs->pciirqenb1);
489 			tmp &= ~BIT((8 + ep->num));	/* completion */
490 			writel(tmp, &regs->pciirqenb1);
491 		}
492 	}
493 	writel(0, &ep->regs->ep_irqenb);
494 
495 	writel(BIT(SHORT_PACKET_OUT_DONE_INTERRUPT) |
496 	       BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT) |
497 	       BIT(FIFO_OVERFLOW) |
498 	       BIT(DATA_PACKET_RECEIVED_INTERRUPT) |
499 	       BIT(DATA_PACKET_TRANSMITTED_INTERRUPT) |
500 	       BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
501 	       BIT(DATA_IN_TOKEN_INTERRUPT), &ep->regs->ep_stat);
502 
503 	tmp = readl(&ep->cfg->ep_cfg);
504 	if (ep->is_in)
505 		tmp &= ~USB3380_EP_CFG_MASK_IN;
506 	else
507 		tmp &= ~USB3380_EP_CFG_MASK_OUT;
508 	writel(tmp, &ep->cfg->ep_cfg);
509 }
510 
511 static void nuke(struct net2280_ep *);
512 
513 static int net2280_disable(struct usb_ep *_ep)
514 {
515 	struct net2280_ep	*ep;
516 	unsigned long		flags;
517 
518 	ep = container_of(_ep, struct net2280_ep, ep);
519 	if (!_ep || !ep->desc || _ep->name == ep0name) {
520 		pr_err("%s: Invalid ep=%p or ep->desc\n", __func__, _ep);
521 		return -EINVAL;
522 	}
523 	spin_lock_irqsave(&ep->dev->lock, flags);
524 	nuke(ep);
525 
526 	if (ep->dev->quirks & PLX_PCIE)
527 		ep_reset_338x(ep->dev->regs, ep);
528 	else
529 		ep_reset_228x(ep->dev->regs, ep);
530 
531 	ep_vdbg(ep->dev, "disabled %s %s\n",
532 			ep->dma ? "dma" : "pio", _ep->name);
533 
534 	/* synch memory views with the device */
535 	(void)readl(&ep->cfg->ep_cfg);
536 
537 	if (!ep->dma && ep->num >= 1 && ep->num <= 4)
538 		ep->dma = &ep->dev->dma[ep->num - 1];
539 
540 	spin_unlock_irqrestore(&ep->dev->lock, flags);
541 	return 0;
542 }
543 
544 /*-------------------------------------------------------------------------*/
545 
546 static struct usb_request
547 *net2280_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
548 {
549 	struct net2280_ep	*ep;
550 	struct net2280_request	*req;
551 
552 	if (!_ep) {
553 		pr_err("%s: Invalid ep\n", __func__);
554 		return NULL;
555 	}
556 	ep = container_of(_ep, struct net2280_ep, ep);
557 
558 	req = kzalloc(sizeof(*req), gfp_flags);
559 	if (!req)
560 		return NULL;
561 
562 	INIT_LIST_HEAD(&req->queue);
563 
564 	/* this dma descriptor may be swapped with the previous dummy */
565 	if (ep->dma) {
566 		struct net2280_dma	*td;
567 
568 		td = dma_pool_alloc(ep->dev->requests, gfp_flags,
569 				&req->td_dma);
570 		if (!td) {
571 			kfree(req);
572 			return NULL;
573 		}
574 		td->dmacount = 0;	/* not VALID */
575 		td->dmadesc = td->dmaaddr;
576 		req->td = td;
577 	}
578 	return &req->req;
579 }
580 
581 static void net2280_free_request(struct usb_ep *_ep, struct usb_request *_req)
582 {
583 	struct net2280_ep	*ep;
584 	struct net2280_request	*req;
585 
586 	ep = container_of(_ep, struct net2280_ep, ep);
587 	if (!_ep || !_req) {
588 		dev_err(&ep->dev->pdev->dev, "%s: Invalid ep=%p or req=%p\n",
589 							__func__, _ep, _req);
590 		return;
591 	}
592 
593 	req = container_of(_req, struct net2280_request, req);
594 	WARN_ON(!list_empty(&req->queue));
595 	if (req->td)
596 		dma_pool_free(ep->dev->requests, req->td, req->td_dma);
597 	kfree(req);
598 }
599 
600 /*-------------------------------------------------------------------------*/
601 
602 /* load a packet into the fifo we use for usb IN transfers.
603  * works for all endpoints.
604  *
605  * NOTE: pio with ep-a..ep-d could stuff multiple packets into the fifo
606  * at a time, but this code is simpler because it knows it only writes
607  * one packet.  ep-a..ep-d should use dma instead.
608  */
609 static void write_fifo(struct net2280_ep *ep, struct usb_request *req)
610 {
611 	struct net2280_ep_regs	__iomem *regs = ep->regs;
612 	u8			*buf;
613 	u32			tmp;
614 	unsigned		count, total;
615 
616 	/* INVARIANT:  fifo is currently empty. (testable) */
617 
618 	if (req) {
619 		buf = req->buf + req->actual;
620 		prefetch(buf);
621 		total = req->length - req->actual;
622 	} else {
623 		total = 0;
624 		buf = NULL;
625 	}
626 
627 	/* write just one packet at a time */
628 	count = ep->ep.maxpacket;
629 	if (count > total)	/* min() cannot be used on a bitfield */
630 		count = total;
631 
632 	ep_vdbg(ep->dev, "write %s fifo (IN) %d bytes%s req %p\n",
633 			ep->ep.name, count,
634 			(count != ep->ep.maxpacket) ? " (short)" : "",
635 			req);
636 	while (count >= 4) {
637 		/* NOTE be careful if you try to align these. fifo lines
638 		 * should normally be full (4 bytes) and successive partial
639 		 * lines are ok only in certain cases.
640 		 */
641 		tmp = get_unaligned((u32 *)buf);
642 		cpu_to_le32s(&tmp);
643 		writel(tmp, &regs->ep_data);
644 		buf += 4;
645 		count -= 4;
646 	}
647 
648 	/* last fifo entry is "short" unless we wrote a full packet.
649 	 * also explicitly validate last word in (periodic) transfers
650 	 * when maxpacket is not a multiple of 4 bytes.
651 	 */
652 	if (count || total < ep->ep.maxpacket) {
653 		tmp = count ? get_unaligned((u32 *)buf) : count;
654 		cpu_to_le32s(&tmp);
655 		set_fifo_bytecount(ep, count & 0x03);
656 		writel(tmp, &regs->ep_data);
657 	}
658 
659 	/* pci writes may still be posted */
660 }
661 
662 /* work around erratum 0106: PCI and USB race over the OUT fifo.
663  * caller guarantees chiprev 0100, out endpoint is NAKing, and
664  * there's no real data in the fifo.
665  *
666  * NOTE:  also used in cases where that erratum doesn't apply:
667  * where the host wrote "too much" data to us.
668  */
669 static void out_flush(struct net2280_ep *ep)
670 {
671 	u32	__iomem *statp;
672 	u32	tmp;
673 
674 	statp = &ep->regs->ep_stat;
675 
676 	tmp = readl(statp);
677 	if (tmp & BIT(NAK_OUT_PACKETS)) {
678 		ep_dbg(ep->dev, "%s %s %08x !NAK\n",
679 			ep->ep.name, __func__, tmp);
680 		writel(BIT(SET_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
681 	}
682 
683 	writel(BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
684 		BIT(DATA_PACKET_RECEIVED_INTERRUPT),
685 		statp);
686 	writel(BIT(FIFO_FLUSH), statp);
687 	/* Make sure that stap is written */
688 	mb();
689 	tmp = readl(statp);
690 	if (tmp & BIT(DATA_OUT_PING_TOKEN_INTERRUPT) &&
691 			/* high speed did bulk NYET; fifo isn't filling */
692 			ep->dev->gadget.speed == USB_SPEED_FULL) {
693 		unsigned	usec;
694 
695 		usec = 50;		/* 64 byte bulk/interrupt */
696 		handshake(statp, BIT(USB_OUT_PING_NAK_SENT),
697 				BIT(USB_OUT_PING_NAK_SENT), usec);
698 		/* NAK done; now CLEAR_NAK_OUT_PACKETS is safe */
699 	}
700 }
701 
702 /* unload packet(s) from the fifo we use for usb OUT transfers.
703  * returns true iff the request completed, because of short packet
704  * or the request buffer having filled with full packets.
705  *
706  * for ep-a..ep-d this will read multiple packets out when they
707  * have been accepted.
708  */
709 static int read_fifo(struct net2280_ep *ep, struct net2280_request *req)
710 {
711 	struct net2280_ep_regs	__iomem *regs = ep->regs;
712 	u8			*buf = req->req.buf + req->req.actual;
713 	unsigned		count, tmp, is_short;
714 	unsigned		cleanup = 0, prevent = 0;
715 
716 	/* erratum 0106 ... packets coming in during fifo reads might
717 	 * be incompletely rejected.  not all cases have workarounds.
718 	 */
719 	if (ep->dev->chiprev == 0x0100 &&
720 			ep->dev->gadget.speed == USB_SPEED_FULL) {
721 		udelay(1);
722 		tmp = readl(&ep->regs->ep_stat);
723 		if ((tmp & BIT(NAK_OUT_PACKETS)))
724 			cleanup = 1;
725 		else if ((tmp & BIT(FIFO_FULL))) {
726 			start_out_naking(ep);
727 			prevent = 1;
728 		}
729 		/* else: hope we don't see the problem */
730 	}
731 
732 	/* never overflow the rx buffer. the fifo reads packets until
733 	 * it sees a short one; we might not be ready for them all.
734 	 */
735 	prefetchw(buf);
736 	count = readl(&regs->ep_avail);
737 	if (unlikely(count == 0)) {
738 		udelay(1);
739 		tmp = readl(&ep->regs->ep_stat);
740 		count = readl(&regs->ep_avail);
741 		/* handled that data already? */
742 		if (count == 0 && (tmp & BIT(NAK_OUT_PACKETS)) == 0)
743 			return 0;
744 	}
745 
746 	tmp = req->req.length - req->req.actual;
747 	if (count > tmp) {
748 		/* as with DMA, data overflow gets flushed */
749 		if ((tmp % ep->ep.maxpacket) != 0) {
750 			ep_err(ep->dev,
751 				"%s out fifo %d bytes, expected %d\n",
752 				ep->ep.name, count, tmp);
753 			req->req.status = -EOVERFLOW;
754 			cleanup = 1;
755 			/* NAK_OUT_PACKETS will be set, so flushing is safe;
756 			 * the next read will start with the next packet
757 			 */
758 		} /* else it's a ZLP, no worries */
759 		count = tmp;
760 	}
761 	req->req.actual += count;
762 
763 	is_short = (count == 0) || ((count % ep->ep.maxpacket) != 0);
764 
765 	ep_vdbg(ep->dev, "read %s fifo (OUT) %d bytes%s%s%s req %p %d/%d\n",
766 			ep->ep.name, count, is_short ? " (short)" : "",
767 			cleanup ? " flush" : "", prevent ? " nak" : "",
768 			req, req->req.actual, req->req.length);
769 
770 	while (count >= 4) {
771 		tmp = readl(&regs->ep_data);
772 		cpu_to_le32s(&tmp);
773 		put_unaligned(tmp, (u32 *)buf);
774 		buf += 4;
775 		count -= 4;
776 	}
777 	if (count) {
778 		tmp = readl(&regs->ep_data);
779 		/* LE conversion is implicit here: */
780 		do {
781 			*buf++ = (u8) tmp;
782 			tmp >>= 8;
783 		} while (--count);
784 	}
785 	if (cleanup)
786 		out_flush(ep);
787 	if (prevent) {
788 		writel(BIT(CLEAR_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
789 		(void) readl(&ep->regs->ep_rsp);
790 	}
791 
792 	return is_short || ((req->req.actual == req->req.length) &&
793 			!req->req.zero);
794 }
795 
796 /* fill out dma descriptor to match a given request */
797 static void fill_dma_desc(struct net2280_ep *ep,
798 					struct net2280_request *req, int valid)
799 {
800 	struct net2280_dma	*td = req->td;
801 	u32			dmacount = req->req.length;
802 
803 	/* don't let DMA continue after a short OUT packet,
804 	 * so overruns can't affect the next transfer.
805 	 * in case of overruns on max-size packets, we can't
806 	 * stop the fifo from filling but we can flush it.
807 	 */
808 	if (ep->is_in)
809 		dmacount |= BIT(DMA_DIRECTION);
810 	if ((!ep->is_in && (dmacount % ep->ep.maxpacket) != 0) ||
811 					!(ep->dev->quirks & PLX_2280))
812 		dmacount |= BIT(END_OF_CHAIN);
813 
814 	req->valid = valid;
815 	if (valid)
816 		dmacount |= BIT(VALID_BIT);
817 	dmacount |= BIT(DMA_DONE_INTERRUPT_ENABLE);
818 
819 	/* td->dmadesc = previously set by caller */
820 	td->dmaaddr = cpu_to_le32 (req->req.dma);
821 
822 	/* 2280 may be polling VALID_BIT through ep->dma->dmadesc */
823 	wmb();
824 	td->dmacount = cpu_to_le32(dmacount);
825 }
826 
827 static const u32 dmactl_default =
828 		BIT(DMA_SCATTER_GATHER_DONE_INTERRUPT) |
829 		BIT(DMA_CLEAR_COUNT_ENABLE) |
830 		/* erratum 0116 workaround part 1 (use POLLING) */
831 		(POLL_100_USEC << DESCRIPTOR_POLLING_RATE) |
832 		BIT(DMA_VALID_BIT_POLLING_ENABLE) |
833 		BIT(DMA_VALID_BIT_ENABLE) |
834 		BIT(DMA_SCATTER_GATHER_ENABLE) |
835 		/* erratum 0116 workaround part 2 (no AUTOSTART) */
836 		BIT(DMA_ENABLE);
837 
838 static inline void spin_stop_dma(struct net2280_dma_regs __iomem *dma)
839 {
840 	handshake(&dma->dmactl, BIT(DMA_ENABLE), 0, 50);
841 }
842 
843 static inline void stop_dma(struct net2280_dma_regs __iomem *dma)
844 {
845 	writel(readl(&dma->dmactl) & ~BIT(DMA_ENABLE), &dma->dmactl);
846 	spin_stop_dma(dma);
847 }
848 
849 static void start_queue(struct net2280_ep *ep, u32 dmactl, u32 td_dma)
850 {
851 	struct net2280_dma_regs	__iomem *dma = ep->dma;
852 	unsigned int tmp = BIT(VALID_BIT) | (ep->is_in << DMA_DIRECTION);
853 
854 	if (!(ep->dev->quirks & PLX_2280))
855 		tmp |= BIT(END_OF_CHAIN);
856 
857 	writel(tmp, &dma->dmacount);
858 	writel(readl(&dma->dmastat), &dma->dmastat);
859 
860 	writel(td_dma, &dma->dmadesc);
861 	if (ep->dev->quirks & PLX_PCIE)
862 		dmactl |= BIT(DMA_REQUEST_OUTSTANDING);
863 	writel(dmactl, &dma->dmactl);
864 
865 	/* erratum 0116 workaround part 3:  pci arbiter away from net2280 */
866 	(void) readl(&ep->dev->pci->pcimstctl);
867 
868 	writel(BIT(DMA_START), &dma->dmastat);
869 
870 	if (!ep->is_in)
871 		stop_out_naking(ep);
872 }
873 
874 static void start_dma(struct net2280_ep *ep, struct net2280_request *req)
875 {
876 	u32			tmp;
877 	struct net2280_dma_regs	__iomem *dma = ep->dma;
878 
879 	/* FIXME can't use DMA for ZLPs */
880 
881 	/* on this path we "know" there's no dma active (yet) */
882 	WARN_ON(readl(&dma->dmactl) & BIT(DMA_ENABLE));
883 	writel(0, &ep->dma->dmactl);
884 
885 	/* previous OUT packet might have been short */
886 	if (!ep->is_in && (readl(&ep->regs->ep_stat) &
887 				BIT(NAK_OUT_PACKETS))) {
888 		writel(BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT),
889 			&ep->regs->ep_stat);
890 
891 		tmp = readl(&ep->regs->ep_avail);
892 		if (tmp) {
893 			writel(readl(&dma->dmastat), &dma->dmastat);
894 
895 			/* transfer all/some fifo data */
896 			writel(req->req.dma, &dma->dmaaddr);
897 			tmp = min(tmp, req->req.length);
898 
899 			/* dma irq, faking scatterlist status */
900 			req->td->dmacount = cpu_to_le32(req->req.length - tmp);
901 			writel(BIT(DMA_DONE_INTERRUPT_ENABLE) | tmp,
902 					&dma->dmacount);
903 			req->td->dmadesc = 0;
904 			req->valid = 1;
905 
906 			writel(BIT(DMA_ENABLE), &dma->dmactl);
907 			writel(BIT(DMA_START), &dma->dmastat);
908 			return;
909 		}
910 	}
911 
912 	tmp = dmactl_default;
913 
914 	/* force packet boundaries between dma requests, but prevent the
915 	 * controller from automagically writing a last "short" packet
916 	 * (zero length) unless the driver explicitly said to do that.
917 	 */
918 	if (ep->is_in) {
919 		if (likely((req->req.length % ep->ep.maxpacket) ||
920 							req->req.zero)){
921 			tmp |= BIT(DMA_FIFO_VALIDATE);
922 			ep->in_fifo_validate = 1;
923 		} else
924 			ep->in_fifo_validate = 0;
925 	}
926 
927 	/* init req->td, pointing to the current dummy */
928 	req->td->dmadesc = cpu_to_le32 (ep->td_dma);
929 	fill_dma_desc(ep, req, 1);
930 
931 	req->td->dmacount |= cpu_to_le32(BIT(END_OF_CHAIN));
932 
933 	start_queue(ep, tmp, req->td_dma);
934 }
935 
936 static inline void
937 queue_dma(struct net2280_ep *ep, struct net2280_request *req, int valid)
938 {
939 	struct net2280_dma	*end;
940 	dma_addr_t		tmp;
941 
942 	/* swap new dummy for old, link; fill and maybe activate */
943 	end = ep->dummy;
944 	ep->dummy = req->td;
945 	req->td = end;
946 
947 	tmp = ep->td_dma;
948 	ep->td_dma = req->td_dma;
949 	req->td_dma = tmp;
950 
951 	end->dmadesc = cpu_to_le32 (ep->td_dma);
952 
953 	fill_dma_desc(ep, req, valid);
954 }
955 
956 static void
957 done(struct net2280_ep *ep, struct net2280_request *req, int status)
958 {
959 	struct net2280		*dev;
960 	unsigned		stopped = ep->stopped;
961 
962 	list_del_init(&req->queue);
963 
964 	if (req->req.status == -EINPROGRESS)
965 		req->req.status = status;
966 	else
967 		status = req->req.status;
968 
969 	dev = ep->dev;
970 	if (ep->dma)
971 		usb_gadget_unmap_request(&dev->gadget, &req->req, ep->is_in);
972 
973 	if (status && status != -ESHUTDOWN)
974 		ep_vdbg(dev, "complete %s req %p stat %d len %u/%u\n",
975 			ep->ep.name, &req->req, status,
976 			req->req.actual, req->req.length);
977 
978 	/* don't modify queue heads during completion callback */
979 	ep->stopped = 1;
980 	spin_unlock(&dev->lock);
981 	usb_gadget_giveback_request(&ep->ep, &req->req);
982 	spin_lock(&dev->lock);
983 	ep->stopped = stopped;
984 }
985 
986 /*-------------------------------------------------------------------------*/
987 
988 static int
989 net2280_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
990 {
991 	struct net2280_request	*req;
992 	struct net2280_ep	*ep;
993 	struct net2280		*dev;
994 	unsigned long		flags;
995 	int ret = 0;
996 
997 	/* we always require a cpu-view buffer, so that we can
998 	 * always use pio (as fallback or whatever).
999 	 */
1000 	ep = container_of(_ep, struct net2280_ep, ep);
1001 	if (!_ep || (!ep->desc && ep->num != 0)) {
1002 		pr_err("%s: Invalid ep=%p or ep->desc\n", __func__, _ep);
1003 		return -EINVAL;
1004 	}
1005 	req = container_of(_req, struct net2280_request, req);
1006 	if (!_req || !_req->complete || !_req->buf ||
1007 				!list_empty(&req->queue)) {
1008 		ret = -EINVAL;
1009 		goto print_err;
1010 	}
1011 	if (_req->length > (~0 & DMA_BYTE_COUNT_MASK)) {
1012 		ret = -EDOM;
1013 		goto print_err;
1014 	}
1015 	dev = ep->dev;
1016 	if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
1017 		ret = -ESHUTDOWN;
1018 		goto print_err;
1019 	}
1020 
1021 	/* FIXME implement PIO fallback for ZLPs with DMA */
1022 	if (ep->dma && _req->length == 0) {
1023 		ret = -EOPNOTSUPP;
1024 		goto print_err;
1025 	}
1026 
1027 	/* set up dma mapping in case the caller didn't */
1028 	if (ep->dma) {
1029 		ret = usb_gadget_map_request(&dev->gadget, _req,
1030 				ep->is_in);
1031 		if (ret)
1032 			goto print_err;
1033 	}
1034 
1035 	ep_vdbg(dev, "%s queue req %p, len %d buf %p\n",
1036 			_ep->name, _req, _req->length, _req->buf);
1037 
1038 	spin_lock_irqsave(&dev->lock, flags);
1039 
1040 	_req->status = -EINPROGRESS;
1041 	_req->actual = 0;
1042 
1043 	/* kickstart this i/o queue? */
1044 	if  (list_empty(&ep->queue) && !ep->stopped &&
1045 		!((dev->quirks & PLX_PCIE) && ep->dma &&
1046 		  (readl(&ep->regs->ep_rsp) & BIT(CLEAR_ENDPOINT_HALT)))) {
1047 
1048 		/* use DMA if the endpoint supports it, else pio */
1049 		if (ep->dma)
1050 			start_dma(ep, req);
1051 		else {
1052 			/* maybe there's no control data, just status ack */
1053 			if (ep->num == 0 && _req->length == 0) {
1054 				allow_status(ep);
1055 				done(ep, req, 0);
1056 				ep_vdbg(dev, "%s status ack\n", ep->ep.name);
1057 				goto done;
1058 			}
1059 
1060 			/* PIO ... stuff the fifo, or unblock it.  */
1061 			if (ep->is_in)
1062 				write_fifo(ep, _req);
1063 			else if (list_empty(&ep->queue)) {
1064 				u32	s;
1065 
1066 				/* OUT FIFO might have packet(s) buffered */
1067 				s = readl(&ep->regs->ep_stat);
1068 				if ((s & BIT(FIFO_EMPTY)) == 0) {
1069 					/* note:  _req->short_not_ok is
1070 					 * ignored here since PIO _always_
1071 					 * stops queue advance here, and
1072 					 * _req->status doesn't change for
1073 					 * short reads (only _req->actual)
1074 					 */
1075 					if (read_fifo(ep, req) &&
1076 							ep->num == 0) {
1077 						done(ep, req, 0);
1078 						allow_status(ep);
1079 						/* don't queue it */
1080 						req = NULL;
1081 					} else if (read_fifo(ep, req) &&
1082 							ep->num != 0) {
1083 						done(ep, req, 0);
1084 						req = NULL;
1085 					} else
1086 						s = readl(&ep->regs->ep_stat);
1087 				}
1088 
1089 				/* don't NAK, let the fifo fill */
1090 				if (req && (s & BIT(NAK_OUT_PACKETS)))
1091 					writel(BIT(CLEAR_NAK_OUT_PACKETS),
1092 							&ep->regs->ep_rsp);
1093 			}
1094 		}
1095 
1096 	} else if (ep->dma) {
1097 		int	valid = 1;
1098 
1099 		if (ep->is_in) {
1100 			int	expect;
1101 
1102 			/* preventing magic zlps is per-engine state, not
1103 			 * per-transfer; irq logic must recover hiccups.
1104 			 */
1105 			expect = likely(req->req.zero ||
1106 				(req->req.length % ep->ep.maxpacket));
1107 			if (expect != ep->in_fifo_validate)
1108 				valid = 0;
1109 		}
1110 		queue_dma(ep, req, valid);
1111 
1112 	} /* else the irq handler advances the queue. */
1113 
1114 	ep->responded = 1;
1115 	if (req)
1116 		list_add_tail(&req->queue, &ep->queue);
1117 done:
1118 	spin_unlock_irqrestore(&dev->lock, flags);
1119 
1120 	/* pci writes may still be posted */
1121 	return ret;
1122 
1123 print_err:
1124 	dev_err(&ep->dev->pdev->dev, "%s: error=%d\n", __func__, ret);
1125 	return ret;
1126 }
1127 
1128 static inline void
1129 dma_done(struct net2280_ep *ep,	struct net2280_request *req, u32 dmacount,
1130 		int status)
1131 {
1132 	req->req.actual = req->req.length - (DMA_BYTE_COUNT_MASK & dmacount);
1133 	done(ep, req, status);
1134 }
1135 
1136 static int scan_dma_completions(struct net2280_ep *ep)
1137 {
1138 	int num_completed = 0;
1139 
1140 	/* only look at descriptors that were "naturally" retired,
1141 	 * so fifo and list head state won't matter
1142 	 */
1143 	while (!list_empty(&ep->queue)) {
1144 		struct net2280_request	*req;
1145 		u32 req_dma_count;
1146 
1147 		req = list_entry(ep->queue.next,
1148 				struct net2280_request, queue);
1149 		if (!req->valid)
1150 			break;
1151 		rmb();
1152 		req_dma_count = le32_to_cpup(&req->td->dmacount);
1153 		if ((req_dma_count & BIT(VALID_BIT)) != 0)
1154 			break;
1155 
1156 		/* SHORT_PACKET_TRANSFERRED_INTERRUPT handles "usb-short"
1157 		 * cases where DMA must be aborted; this code handles
1158 		 * all non-abort DMA completions.
1159 		 */
1160 		if (unlikely(req->td->dmadesc == 0)) {
1161 			/* paranoia */
1162 			u32 const ep_dmacount = readl(&ep->dma->dmacount);
1163 
1164 			if (ep_dmacount & DMA_BYTE_COUNT_MASK)
1165 				break;
1166 			/* single transfer mode */
1167 			dma_done(ep, req, req_dma_count, 0);
1168 			num_completed++;
1169 			break;
1170 		} else if (!ep->is_in &&
1171 			   (req->req.length % ep->ep.maxpacket) &&
1172 			   !(ep->dev->quirks & PLX_PCIE)) {
1173 
1174 			u32 const ep_stat = readl(&ep->regs->ep_stat);
1175 			/* AVOID TROUBLE HERE by not issuing short reads from
1176 			 * your gadget driver.  That helps avoids errata 0121,
1177 			 * 0122, and 0124; not all cases trigger the warning.
1178 			 */
1179 			if ((ep_stat & BIT(NAK_OUT_PACKETS)) == 0) {
1180 				ep_warn(ep->dev, "%s lost packet sync!\n",
1181 						ep->ep.name);
1182 				req->req.status = -EOVERFLOW;
1183 			} else {
1184 				u32 const ep_avail = readl(&ep->regs->ep_avail);
1185 				if (ep_avail) {
1186 					/* fifo gets flushed later */
1187 					ep->out_overflow = 1;
1188 					ep_dbg(ep->dev,
1189 						"%s dma, discard %d len %d\n",
1190 						ep->ep.name, ep_avail,
1191 						req->req.length);
1192 					req->req.status = -EOVERFLOW;
1193 				}
1194 			}
1195 		}
1196 		dma_done(ep, req, req_dma_count, 0);
1197 		num_completed++;
1198 	}
1199 
1200 	return num_completed;
1201 }
1202 
1203 static void restart_dma(struct net2280_ep *ep)
1204 {
1205 	struct net2280_request	*req;
1206 
1207 	if (ep->stopped)
1208 		return;
1209 	req = list_entry(ep->queue.next, struct net2280_request, queue);
1210 
1211 	start_dma(ep, req);
1212 }
1213 
1214 static void abort_dma(struct net2280_ep *ep)
1215 {
1216 	/* abort the current transfer */
1217 	if (likely(!list_empty(&ep->queue))) {
1218 		/* FIXME work around errata 0121, 0122, 0124 */
1219 		writel(BIT(DMA_ABORT), &ep->dma->dmastat);
1220 		spin_stop_dma(ep->dma);
1221 	} else
1222 		stop_dma(ep->dma);
1223 	scan_dma_completions(ep);
1224 }
1225 
1226 /* dequeue ALL requests */
1227 static void nuke(struct net2280_ep *ep)
1228 {
1229 	struct net2280_request	*req;
1230 
1231 	/* called with spinlock held */
1232 	ep->stopped = 1;
1233 	if (ep->dma)
1234 		abort_dma(ep);
1235 	while (!list_empty(&ep->queue)) {
1236 		req = list_entry(ep->queue.next,
1237 				struct net2280_request,
1238 				queue);
1239 		done(ep, req, -ESHUTDOWN);
1240 	}
1241 }
1242 
1243 /* dequeue JUST ONE request */
1244 static int net2280_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1245 {
1246 	struct net2280_ep	*ep;
1247 	struct net2280_request	*req;
1248 	unsigned long		flags;
1249 	u32			dmactl;
1250 	int			stopped;
1251 
1252 	ep = container_of(_ep, struct net2280_ep, ep);
1253 	if (!_ep || (!ep->desc && ep->num != 0) || !_req) {
1254 		pr_err("%s: Invalid ep=%p or ep->desc or req=%p\n",
1255 						__func__, _ep, _req);
1256 		return -EINVAL;
1257 	}
1258 
1259 	spin_lock_irqsave(&ep->dev->lock, flags);
1260 	stopped = ep->stopped;
1261 
1262 	/* quiesce dma while we patch the queue */
1263 	dmactl = 0;
1264 	ep->stopped = 1;
1265 	if (ep->dma) {
1266 		dmactl = readl(&ep->dma->dmactl);
1267 		/* WARNING erratum 0127 may kick in ... */
1268 		stop_dma(ep->dma);
1269 		scan_dma_completions(ep);
1270 	}
1271 
1272 	/* make sure it's still queued on this endpoint */
1273 	list_for_each_entry(req, &ep->queue, queue) {
1274 		if (&req->req == _req)
1275 			break;
1276 	}
1277 	if (&req->req != _req) {
1278 		spin_unlock_irqrestore(&ep->dev->lock, flags);
1279 		dev_err(&ep->dev->pdev->dev, "%s: Request mismatch\n",
1280 								__func__);
1281 		return -EINVAL;
1282 	}
1283 
1284 	/* queue head may be partially complete. */
1285 	if (ep->queue.next == &req->queue) {
1286 		if (ep->dma) {
1287 			ep_dbg(ep->dev, "unlink (%s) dma\n", _ep->name);
1288 			_req->status = -ECONNRESET;
1289 			abort_dma(ep);
1290 			if (likely(ep->queue.next == &req->queue)) {
1291 				/* NOTE: misreports single-transfer mode*/
1292 				req->td->dmacount = 0;	/* invalidate */
1293 				dma_done(ep, req,
1294 					readl(&ep->dma->dmacount),
1295 					-ECONNRESET);
1296 			}
1297 		} else {
1298 			ep_dbg(ep->dev, "unlink (%s) pio\n", _ep->name);
1299 			done(ep, req, -ECONNRESET);
1300 		}
1301 		req = NULL;
1302 	}
1303 
1304 	if (req)
1305 		done(ep, req, -ECONNRESET);
1306 	ep->stopped = stopped;
1307 
1308 	if (ep->dma) {
1309 		/* turn off dma on inactive queues */
1310 		if (list_empty(&ep->queue))
1311 			stop_dma(ep->dma);
1312 		else if (!ep->stopped) {
1313 			/* resume current request, or start new one */
1314 			if (req)
1315 				writel(dmactl, &ep->dma->dmactl);
1316 			else
1317 				start_dma(ep, list_entry(ep->queue.next,
1318 					struct net2280_request, queue));
1319 		}
1320 	}
1321 
1322 	spin_unlock_irqrestore(&ep->dev->lock, flags);
1323 	return 0;
1324 }
1325 
1326 /*-------------------------------------------------------------------------*/
1327 
1328 static int net2280_fifo_status(struct usb_ep *_ep);
1329 
1330 static int
1331 net2280_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged)
1332 {
1333 	struct net2280_ep	*ep;
1334 	unsigned long		flags;
1335 	int			retval = 0;
1336 
1337 	ep = container_of(_ep, struct net2280_ep, ep);
1338 	if (!_ep || (!ep->desc && ep->num != 0)) {
1339 		pr_err("%s: Invalid ep=%p or ep->desc\n", __func__, _ep);
1340 		return -EINVAL;
1341 	}
1342 	if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN) {
1343 		retval = -ESHUTDOWN;
1344 		goto print_err;
1345 	}
1346 	if (ep->desc /* not ep0 */ && (ep->desc->bmAttributes & 0x03)
1347 						== USB_ENDPOINT_XFER_ISOC) {
1348 		retval = -EINVAL;
1349 		goto print_err;
1350 	}
1351 
1352 	spin_lock_irqsave(&ep->dev->lock, flags);
1353 	if (!list_empty(&ep->queue)) {
1354 		retval = -EAGAIN;
1355 		goto print_unlock;
1356 	} else if (ep->is_in && value && net2280_fifo_status(_ep) != 0) {
1357 		retval = -EAGAIN;
1358 		goto print_unlock;
1359 	} else {
1360 		ep_vdbg(ep->dev, "%s %s %s\n", _ep->name,
1361 				value ? "set" : "clear",
1362 				wedged ? "wedge" : "halt");
1363 		/* set/clear, then synch memory views with the device */
1364 		if (value) {
1365 			if (ep->num == 0)
1366 				ep->dev->protocol_stall = 1;
1367 			else
1368 				set_halt(ep);
1369 			if (wedged)
1370 				ep->wedged = 1;
1371 		} else {
1372 			clear_halt(ep);
1373 			if (ep->dev->quirks & PLX_PCIE &&
1374 				!list_empty(&ep->queue) && ep->td_dma)
1375 					restart_dma(ep);
1376 			ep->wedged = 0;
1377 		}
1378 		(void) readl(&ep->regs->ep_rsp);
1379 	}
1380 	spin_unlock_irqrestore(&ep->dev->lock, flags);
1381 
1382 	return retval;
1383 
1384 print_unlock:
1385 	spin_unlock_irqrestore(&ep->dev->lock, flags);
1386 print_err:
1387 	dev_err(&ep->dev->pdev->dev, "%s: error=%d\n", __func__, retval);
1388 	return retval;
1389 }
1390 
1391 static int net2280_set_halt(struct usb_ep *_ep, int value)
1392 {
1393 	return net2280_set_halt_and_wedge(_ep, value, 0);
1394 }
1395 
1396 static int net2280_set_wedge(struct usb_ep *_ep)
1397 {
1398 	if (!_ep || _ep->name == ep0name) {
1399 		pr_err("%s: Invalid ep=%p or ep0\n", __func__, _ep);
1400 		return -EINVAL;
1401 	}
1402 	return net2280_set_halt_and_wedge(_ep, 1, 1);
1403 }
1404 
1405 static int net2280_fifo_status(struct usb_ep *_ep)
1406 {
1407 	struct net2280_ep	*ep;
1408 	u32			avail;
1409 
1410 	ep = container_of(_ep, struct net2280_ep, ep);
1411 	if (!_ep || (!ep->desc && ep->num != 0)) {
1412 		pr_err("%s: Invalid ep=%p or ep->desc\n", __func__, _ep);
1413 		return -ENODEV;
1414 	}
1415 	if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN) {
1416 		dev_err(&ep->dev->pdev->dev,
1417 			"%s: Invalid driver=%p or speed=%d\n",
1418 			__func__, ep->dev->driver, ep->dev->gadget.speed);
1419 		return -ESHUTDOWN;
1420 	}
1421 
1422 	avail = readl(&ep->regs->ep_avail) & (BIT(12) - 1);
1423 	if (avail > ep->fifo_size) {
1424 		dev_err(&ep->dev->pdev->dev, "%s: Fifo overflow\n", __func__);
1425 		return -EOVERFLOW;
1426 	}
1427 	if (ep->is_in)
1428 		avail = ep->fifo_size - avail;
1429 	return avail;
1430 }
1431 
1432 static void net2280_fifo_flush(struct usb_ep *_ep)
1433 {
1434 	struct net2280_ep	*ep;
1435 
1436 	ep = container_of(_ep, struct net2280_ep, ep);
1437 	if (!_ep || (!ep->desc && ep->num != 0)) {
1438 		pr_err("%s: Invalid ep=%p or ep->desc\n", __func__, _ep);
1439 		return;
1440 	}
1441 	if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN) {
1442 		dev_err(&ep->dev->pdev->dev,
1443 			"%s: Invalid driver=%p or speed=%d\n",
1444 			__func__, ep->dev->driver, ep->dev->gadget.speed);
1445 		return;
1446 	}
1447 
1448 	writel(BIT(FIFO_FLUSH), &ep->regs->ep_stat);
1449 	(void) readl(&ep->regs->ep_rsp);
1450 }
1451 
1452 static const struct usb_ep_ops net2280_ep_ops = {
1453 	.enable		= net2280_enable,
1454 	.disable	= net2280_disable,
1455 
1456 	.alloc_request	= net2280_alloc_request,
1457 	.free_request	= net2280_free_request,
1458 
1459 	.queue		= net2280_queue,
1460 	.dequeue	= net2280_dequeue,
1461 
1462 	.set_halt	= net2280_set_halt,
1463 	.set_wedge	= net2280_set_wedge,
1464 	.fifo_status	= net2280_fifo_status,
1465 	.fifo_flush	= net2280_fifo_flush,
1466 };
1467 
1468 /*-------------------------------------------------------------------------*/
1469 
1470 static int net2280_get_frame(struct usb_gadget *_gadget)
1471 {
1472 	struct net2280		*dev;
1473 	unsigned long		flags;
1474 	u16			retval;
1475 
1476 	if (!_gadget)
1477 		return -ENODEV;
1478 	dev = container_of(_gadget, struct net2280, gadget);
1479 	spin_lock_irqsave(&dev->lock, flags);
1480 	retval = get_idx_reg(dev->regs, REG_FRAME) & 0x03ff;
1481 	spin_unlock_irqrestore(&dev->lock, flags);
1482 	return retval;
1483 }
1484 
1485 static int net2280_wakeup(struct usb_gadget *_gadget)
1486 {
1487 	struct net2280		*dev;
1488 	u32			tmp;
1489 	unsigned long		flags;
1490 
1491 	if (!_gadget)
1492 		return 0;
1493 	dev = container_of(_gadget, struct net2280, gadget);
1494 
1495 	spin_lock_irqsave(&dev->lock, flags);
1496 	tmp = readl(&dev->usb->usbctl);
1497 	if (tmp & BIT(DEVICE_REMOTE_WAKEUP_ENABLE))
1498 		writel(BIT(GENERATE_RESUME), &dev->usb->usbstat);
1499 	spin_unlock_irqrestore(&dev->lock, flags);
1500 
1501 	/* pci writes may still be posted */
1502 	return 0;
1503 }
1504 
1505 static int net2280_set_selfpowered(struct usb_gadget *_gadget, int value)
1506 {
1507 	struct net2280		*dev;
1508 	u32			tmp;
1509 	unsigned long		flags;
1510 
1511 	if (!_gadget)
1512 		return 0;
1513 	dev = container_of(_gadget, struct net2280, gadget);
1514 
1515 	spin_lock_irqsave(&dev->lock, flags);
1516 	tmp = readl(&dev->usb->usbctl);
1517 	if (value) {
1518 		tmp |= BIT(SELF_POWERED_STATUS);
1519 		_gadget->is_selfpowered = 1;
1520 	} else {
1521 		tmp &= ~BIT(SELF_POWERED_STATUS);
1522 		_gadget->is_selfpowered = 0;
1523 	}
1524 	writel(tmp, &dev->usb->usbctl);
1525 	spin_unlock_irqrestore(&dev->lock, flags);
1526 
1527 	return 0;
1528 }
1529 
1530 static int net2280_pullup(struct usb_gadget *_gadget, int is_on)
1531 {
1532 	struct net2280  *dev;
1533 	u32             tmp;
1534 	unsigned long   flags;
1535 
1536 	if (!_gadget)
1537 		return -ENODEV;
1538 	dev = container_of(_gadget, struct net2280, gadget);
1539 
1540 	spin_lock_irqsave(&dev->lock, flags);
1541 	tmp = readl(&dev->usb->usbctl);
1542 	dev->softconnect = (is_on != 0);
1543 	if (is_on) {
1544 		ep0_start(dev);
1545 		writel(tmp | BIT(USB_DETECT_ENABLE), &dev->usb->usbctl);
1546 	} else {
1547 		writel(tmp & ~BIT(USB_DETECT_ENABLE), &dev->usb->usbctl);
1548 		stop_activity(dev, dev->driver);
1549 	}
1550 
1551 	spin_unlock_irqrestore(&dev->lock, flags);
1552 
1553 	return 0;
1554 }
1555 
1556 static struct usb_ep *net2280_match_ep(struct usb_gadget *_gadget,
1557 		struct usb_endpoint_descriptor *desc,
1558 		struct usb_ss_ep_comp_descriptor *ep_comp)
1559 {
1560 	char name[8];
1561 	struct usb_ep *ep;
1562 
1563 	if (usb_endpoint_type(desc) == USB_ENDPOINT_XFER_INT) {
1564 		/* ep-e, ep-f are PIO with only 64 byte fifos */
1565 		ep = gadget_find_ep_by_name(_gadget, "ep-e");
1566 		if (ep && usb_gadget_ep_match_desc(_gadget, ep, desc, ep_comp))
1567 			return ep;
1568 		ep = gadget_find_ep_by_name(_gadget, "ep-f");
1569 		if (ep && usb_gadget_ep_match_desc(_gadget, ep, desc, ep_comp))
1570 			return ep;
1571 	}
1572 
1573 	/* USB3380: Only first four endpoints have DMA channels. Allocate
1574 	 * slower interrupt endpoints from PIO hw endpoints, to allow bulk/isoc
1575 	 * endpoints use DMA hw endpoints.
1576 	 */
1577 	if (usb_endpoint_type(desc) == USB_ENDPOINT_XFER_INT &&
1578 	    usb_endpoint_dir_in(desc)) {
1579 		ep = gadget_find_ep_by_name(_gadget, "ep2in");
1580 		if (ep && usb_gadget_ep_match_desc(_gadget, ep, desc, ep_comp))
1581 			return ep;
1582 		ep = gadget_find_ep_by_name(_gadget, "ep4in");
1583 		if (ep && usb_gadget_ep_match_desc(_gadget, ep, desc, ep_comp))
1584 			return ep;
1585 	} else if (usb_endpoint_type(desc) == USB_ENDPOINT_XFER_INT &&
1586 		   !usb_endpoint_dir_in(desc)) {
1587 		ep = gadget_find_ep_by_name(_gadget, "ep1out");
1588 		if (ep && usb_gadget_ep_match_desc(_gadget, ep, desc, ep_comp))
1589 			return ep;
1590 		ep = gadget_find_ep_by_name(_gadget, "ep3out");
1591 		if (ep && usb_gadget_ep_match_desc(_gadget, ep, desc, ep_comp))
1592 			return ep;
1593 	} else if (usb_endpoint_type(desc) != USB_ENDPOINT_XFER_BULK &&
1594 		   usb_endpoint_dir_in(desc)) {
1595 		ep = gadget_find_ep_by_name(_gadget, "ep1in");
1596 		if (ep && usb_gadget_ep_match_desc(_gadget, ep, desc, ep_comp))
1597 			return ep;
1598 		ep = gadget_find_ep_by_name(_gadget, "ep3in");
1599 		if (ep && usb_gadget_ep_match_desc(_gadget, ep, desc, ep_comp))
1600 			return ep;
1601 	} else if (usb_endpoint_type(desc) != USB_ENDPOINT_XFER_BULK &&
1602 		   !usb_endpoint_dir_in(desc)) {
1603 		ep = gadget_find_ep_by_name(_gadget, "ep2out");
1604 		if (ep && usb_gadget_ep_match_desc(_gadget, ep, desc, ep_comp))
1605 			return ep;
1606 		ep = gadget_find_ep_by_name(_gadget, "ep4out");
1607 		if (ep && usb_gadget_ep_match_desc(_gadget, ep, desc, ep_comp))
1608 			return ep;
1609 	}
1610 
1611 	/* USB3380: use same address for usb and hardware endpoints */
1612 	snprintf(name, sizeof(name), "ep%d%s", usb_endpoint_num(desc),
1613 			usb_endpoint_dir_in(desc) ? "in" : "out");
1614 	ep = gadget_find_ep_by_name(_gadget, name);
1615 	if (ep && usb_gadget_ep_match_desc(_gadget, ep, desc, ep_comp))
1616 		return ep;
1617 
1618 	return NULL;
1619 }
1620 
1621 static int net2280_start(struct usb_gadget *_gadget,
1622 		struct usb_gadget_driver *driver);
1623 static int net2280_stop(struct usb_gadget *_gadget);
1624 
1625 static const struct usb_gadget_ops net2280_ops = {
1626 	.get_frame	= net2280_get_frame,
1627 	.wakeup		= net2280_wakeup,
1628 	.set_selfpowered = net2280_set_selfpowered,
1629 	.pullup		= net2280_pullup,
1630 	.udc_start	= net2280_start,
1631 	.udc_stop	= net2280_stop,
1632 	.match_ep	= net2280_match_ep,
1633 };
1634 
1635 /*-------------------------------------------------------------------------*/
1636 
1637 #ifdef	CONFIG_USB_GADGET_DEBUG_FILES
1638 
1639 /* FIXME move these into procfs, and use seq_file.
1640  * Sysfs _still_ doesn't behave for arbitrarily sized files,
1641  * and also doesn't help products using this with 2.4 kernels.
1642  */
1643 
1644 /* "function" sysfs attribute */
1645 static ssize_t function_show(struct device *_dev, struct device_attribute *attr,
1646 			     char *buf)
1647 {
1648 	struct net2280	*dev = dev_get_drvdata(_dev);
1649 
1650 	if (!dev->driver || !dev->driver->function ||
1651 			strlen(dev->driver->function) > PAGE_SIZE)
1652 		return 0;
1653 	return scnprintf(buf, PAGE_SIZE, "%s\n", dev->driver->function);
1654 }
1655 static DEVICE_ATTR_RO(function);
1656 
1657 static ssize_t registers_show(struct device *_dev,
1658 			      struct device_attribute *attr, char *buf)
1659 {
1660 	struct net2280		*dev;
1661 	char			*next;
1662 	unsigned		size, t;
1663 	unsigned long		flags;
1664 	int			i;
1665 	u32			t1, t2;
1666 	const char		*s;
1667 
1668 	dev = dev_get_drvdata(_dev);
1669 	next = buf;
1670 	size = PAGE_SIZE;
1671 	spin_lock_irqsave(&dev->lock, flags);
1672 
1673 	if (dev->driver)
1674 		s = dev->driver->driver.name;
1675 	else
1676 		s = "(none)";
1677 
1678 	/* Main Control Registers */
1679 	t = scnprintf(next, size, "%s version " DRIVER_VERSION
1680 			", chiprev %04x\n\n"
1681 			"devinit %03x fifoctl %08x gadget '%s'\n"
1682 			"pci irqenb0 %02x irqenb1 %08x "
1683 			"irqstat0 %04x irqstat1 %08x\n",
1684 			driver_name, dev->chiprev,
1685 			readl(&dev->regs->devinit),
1686 			readl(&dev->regs->fifoctl),
1687 			s,
1688 			readl(&dev->regs->pciirqenb0),
1689 			readl(&dev->regs->pciirqenb1),
1690 			readl(&dev->regs->irqstat0),
1691 			readl(&dev->regs->irqstat1));
1692 	size -= t;
1693 	next += t;
1694 
1695 	/* USB Control Registers */
1696 	t1 = readl(&dev->usb->usbctl);
1697 	t2 = readl(&dev->usb->usbstat);
1698 	if (t1 & BIT(VBUS_PIN)) {
1699 		if (t2 & BIT(HIGH_SPEED))
1700 			s = "high speed";
1701 		else if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1702 			s = "powered";
1703 		else
1704 			s = "full speed";
1705 		/* full speed bit (6) not working?? */
1706 	} else
1707 			s = "not attached";
1708 	t = scnprintf(next, size,
1709 			"stdrsp %08x usbctl %08x usbstat %08x "
1710 				"addr 0x%02x (%s)\n",
1711 			readl(&dev->usb->stdrsp), t1, t2,
1712 			readl(&dev->usb->ouraddr), s);
1713 	size -= t;
1714 	next += t;
1715 
1716 	/* PCI Master Control Registers */
1717 
1718 	/* DMA Control Registers */
1719 
1720 	/* Configurable EP Control Registers */
1721 	for (i = 0; i < dev->n_ep; i++) {
1722 		struct net2280_ep	*ep;
1723 
1724 		ep = &dev->ep[i];
1725 		if (i && !ep->desc)
1726 			continue;
1727 
1728 		t1 = readl(&ep->cfg->ep_cfg);
1729 		t2 = readl(&ep->regs->ep_rsp) & 0xff;
1730 		t = scnprintf(next, size,
1731 				"\n%s\tcfg %05x rsp (%02x) %s%s%s%s%s%s%s%s"
1732 					"irqenb %02x\n",
1733 				ep->ep.name, t1, t2,
1734 				(t2 & BIT(CLEAR_NAK_OUT_PACKETS))
1735 					? "NAK " : "",
1736 				(t2 & BIT(CLEAR_EP_HIDE_STATUS_PHASE))
1737 					? "hide " : "",
1738 				(t2 & BIT(CLEAR_EP_FORCE_CRC_ERROR))
1739 					? "CRC " : "",
1740 				(t2 & BIT(CLEAR_INTERRUPT_MODE))
1741 					? "interrupt " : "",
1742 				(t2 & BIT(CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE))
1743 					? "status " : "",
1744 				(t2 & BIT(CLEAR_NAK_OUT_PACKETS_MODE))
1745 					? "NAKmode " : "",
1746 				(t2 & BIT(CLEAR_ENDPOINT_TOGGLE))
1747 					? "DATA1 " : "DATA0 ",
1748 				(t2 & BIT(CLEAR_ENDPOINT_HALT))
1749 					? "HALT " : "",
1750 				readl(&ep->regs->ep_irqenb));
1751 		size -= t;
1752 		next += t;
1753 
1754 		t = scnprintf(next, size,
1755 				"\tstat %08x avail %04x "
1756 				"(ep%d%s-%s)%s\n",
1757 				readl(&ep->regs->ep_stat),
1758 				readl(&ep->regs->ep_avail),
1759 				t1 & 0x0f, DIR_STRING(t1),
1760 				type_string(t1 >> 8),
1761 				ep->stopped ? "*" : "");
1762 		size -= t;
1763 		next += t;
1764 
1765 		if (!ep->dma)
1766 			continue;
1767 
1768 		t = scnprintf(next, size,
1769 				"  dma\tctl %08x stat %08x count %08x\n"
1770 				"\taddr %08x desc %08x\n",
1771 				readl(&ep->dma->dmactl),
1772 				readl(&ep->dma->dmastat),
1773 				readl(&ep->dma->dmacount),
1774 				readl(&ep->dma->dmaaddr),
1775 				readl(&ep->dma->dmadesc));
1776 		size -= t;
1777 		next += t;
1778 
1779 	}
1780 
1781 	/* Indexed Registers (none yet) */
1782 
1783 	/* Statistics */
1784 	t = scnprintf(next, size, "\nirqs:  ");
1785 	size -= t;
1786 	next += t;
1787 	for (i = 0; i < dev->n_ep; i++) {
1788 		struct net2280_ep	*ep;
1789 
1790 		ep = &dev->ep[i];
1791 		if (i && !ep->irqs)
1792 			continue;
1793 		t = scnprintf(next, size, " %s/%lu", ep->ep.name, ep->irqs);
1794 		size -= t;
1795 		next += t;
1796 
1797 	}
1798 	t = scnprintf(next, size, "\n");
1799 	size -= t;
1800 	next += t;
1801 
1802 	spin_unlock_irqrestore(&dev->lock, flags);
1803 
1804 	return PAGE_SIZE - size;
1805 }
1806 static DEVICE_ATTR_RO(registers);
1807 
1808 static ssize_t queues_show(struct device *_dev, struct device_attribute *attr,
1809 			   char *buf)
1810 {
1811 	struct net2280		*dev;
1812 	char			*next;
1813 	unsigned		size;
1814 	unsigned long		flags;
1815 	int			i;
1816 
1817 	dev = dev_get_drvdata(_dev);
1818 	next = buf;
1819 	size = PAGE_SIZE;
1820 	spin_lock_irqsave(&dev->lock, flags);
1821 
1822 	for (i = 0; i < dev->n_ep; i++) {
1823 		struct net2280_ep		*ep = &dev->ep[i];
1824 		struct net2280_request		*req;
1825 		int				t;
1826 
1827 		if (i != 0) {
1828 			const struct usb_endpoint_descriptor	*d;
1829 
1830 			d = ep->desc;
1831 			if (!d)
1832 				continue;
1833 			t = d->bEndpointAddress;
1834 			t = scnprintf(next, size,
1835 				"\n%s (ep%d%s-%s) max %04x %s fifo %d\n",
1836 				ep->ep.name, t & USB_ENDPOINT_NUMBER_MASK,
1837 				(t & USB_DIR_IN) ? "in" : "out",
1838 				type_string(d->bmAttributes),
1839 				usb_endpoint_maxp(d),
1840 				ep->dma ? "dma" : "pio", ep->fifo_size
1841 				);
1842 		} else /* ep0 should only have one transfer queued */
1843 			t = scnprintf(next, size, "ep0 max 64 pio %s\n",
1844 					ep->is_in ? "in" : "out");
1845 		if (t <= 0 || t > size)
1846 			goto done;
1847 		size -= t;
1848 		next += t;
1849 
1850 		if (list_empty(&ep->queue)) {
1851 			t = scnprintf(next, size, "\t(nothing queued)\n");
1852 			if (t <= 0 || t > size)
1853 				goto done;
1854 			size -= t;
1855 			next += t;
1856 			continue;
1857 		}
1858 		list_for_each_entry(req, &ep->queue, queue) {
1859 			if (ep->dma && req->td_dma == readl(&ep->dma->dmadesc))
1860 				t = scnprintf(next, size,
1861 					"\treq %p len %d/%d "
1862 					"buf %p (dmacount %08x)\n",
1863 					&req->req, req->req.actual,
1864 					req->req.length, req->req.buf,
1865 					readl(&ep->dma->dmacount));
1866 			else
1867 				t = scnprintf(next, size,
1868 					"\treq %p len %d/%d buf %p\n",
1869 					&req->req, req->req.actual,
1870 					req->req.length, req->req.buf);
1871 			if (t <= 0 || t > size)
1872 				goto done;
1873 			size -= t;
1874 			next += t;
1875 
1876 			if (ep->dma) {
1877 				struct net2280_dma	*td;
1878 
1879 				td = req->td;
1880 				t = scnprintf(next, size, "\t    td %08x "
1881 					" count %08x buf %08x desc %08x\n",
1882 					(u32) req->td_dma,
1883 					le32_to_cpu(td->dmacount),
1884 					le32_to_cpu(td->dmaaddr),
1885 					le32_to_cpu(td->dmadesc));
1886 				if (t <= 0 || t > size)
1887 					goto done;
1888 				size -= t;
1889 				next += t;
1890 			}
1891 		}
1892 	}
1893 
1894 done:
1895 	spin_unlock_irqrestore(&dev->lock, flags);
1896 	return PAGE_SIZE - size;
1897 }
1898 static DEVICE_ATTR_RO(queues);
1899 
1900 
1901 #else
1902 
1903 #define device_create_file(a, b)	(0)
1904 #define device_remove_file(a, b)	do { } while (0)
1905 
1906 #endif
1907 
1908 /*-------------------------------------------------------------------------*/
1909 
1910 /* another driver-specific mode might be a request type doing dma
1911  * to/from another device fifo instead of to/from memory.
1912  */
1913 
1914 static void set_fifo_mode(struct net2280 *dev, int mode)
1915 {
1916 	/* keeping high bits preserves BAR2 */
1917 	writel((0xffff << PCI_BASE2_RANGE) | mode, &dev->regs->fifoctl);
1918 
1919 	/* always ep-{a,b,e,f} ... maybe not ep-c or ep-d */
1920 	INIT_LIST_HEAD(&dev->gadget.ep_list);
1921 	list_add_tail(&dev->ep[1].ep.ep_list, &dev->gadget.ep_list);
1922 	list_add_tail(&dev->ep[2].ep.ep_list, &dev->gadget.ep_list);
1923 	switch (mode) {
1924 	case 0:
1925 		list_add_tail(&dev->ep[3].ep.ep_list, &dev->gadget.ep_list);
1926 		list_add_tail(&dev->ep[4].ep.ep_list, &dev->gadget.ep_list);
1927 		dev->ep[1].fifo_size = dev->ep[2].fifo_size = 1024;
1928 		break;
1929 	case 1:
1930 		dev->ep[1].fifo_size = dev->ep[2].fifo_size = 2048;
1931 		break;
1932 	case 2:
1933 		list_add_tail(&dev->ep[3].ep.ep_list, &dev->gadget.ep_list);
1934 		dev->ep[1].fifo_size = 2048;
1935 		dev->ep[2].fifo_size = 1024;
1936 		break;
1937 	}
1938 	/* fifo sizes for ep0, ep-c, ep-d, ep-e, and ep-f never change */
1939 	list_add_tail(&dev->ep[5].ep.ep_list, &dev->gadget.ep_list);
1940 	list_add_tail(&dev->ep[6].ep.ep_list, &dev->gadget.ep_list);
1941 }
1942 
1943 static void defect7374_disable_data_eps(struct net2280 *dev)
1944 {
1945 	/*
1946 	 * For Defect 7374, disable data EPs (and more):
1947 	 *  - This phase undoes the earlier phase of the Defect 7374 workaround,
1948 	 *    returing ep regs back to normal.
1949 	 */
1950 	struct net2280_ep *ep;
1951 	int i;
1952 	unsigned char ep_sel;
1953 	u32 tmp_reg;
1954 
1955 	for (i = 1; i < 5; i++) {
1956 		ep = &dev->ep[i];
1957 		writel(i, &ep->cfg->ep_cfg);
1958 	}
1959 
1960 	/* CSROUT, CSRIN, PCIOUT, PCIIN, STATIN, RCIN */
1961 	for (i = 0; i < 6; i++)
1962 		writel(0, &dev->dep[i].dep_cfg);
1963 
1964 	for (ep_sel = 0; ep_sel <= 21; ep_sel++) {
1965 		/* Select an endpoint for subsequent operations: */
1966 		tmp_reg = readl(&dev->plregs->pl_ep_ctrl);
1967 		writel(((tmp_reg & ~0x1f) | ep_sel), &dev->plregs->pl_ep_ctrl);
1968 
1969 		if (ep_sel < 2 || (ep_sel > 9 && ep_sel < 14) ||
1970 					ep_sel == 18 || ep_sel == 20)
1971 			continue;
1972 
1973 		/* Change settings on some selected endpoints */
1974 		tmp_reg = readl(&dev->plregs->pl_ep_cfg_4);
1975 		tmp_reg &= ~BIT(NON_CTRL_IN_TOLERATE_BAD_DIR);
1976 		writel(tmp_reg, &dev->plregs->pl_ep_cfg_4);
1977 		tmp_reg = readl(&dev->plregs->pl_ep_ctrl);
1978 		tmp_reg |= BIT(EP_INITIALIZED);
1979 		writel(tmp_reg, &dev->plregs->pl_ep_ctrl);
1980 	}
1981 }
1982 
1983 static void defect7374_enable_data_eps_zero(struct net2280 *dev)
1984 {
1985 	u32 tmp = 0, tmp_reg;
1986 	u32 scratch;
1987 	int i;
1988 	unsigned char ep_sel;
1989 
1990 	scratch = get_idx_reg(dev->regs, SCRATCH);
1991 
1992 	WARN_ON((scratch & (0xf << DEFECT7374_FSM_FIELD))
1993 		== DEFECT7374_FSM_SS_CONTROL_READ);
1994 
1995 	scratch &= ~(0xf << DEFECT7374_FSM_FIELD);
1996 
1997 	ep_warn(dev, "Operate Defect 7374 workaround soft this time");
1998 	ep_warn(dev, "It will operate on cold-reboot and SS connect");
1999 
2000 	/*GPEPs:*/
2001 	tmp = ((0 << ENDPOINT_NUMBER) | BIT(ENDPOINT_DIRECTION) |
2002 			(2 << OUT_ENDPOINT_TYPE) | (2 << IN_ENDPOINT_TYPE) |
2003 			((dev->enhanced_mode) ?
2004 			 BIT(OUT_ENDPOINT_ENABLE) | BIT(IN_ENDPOINT_ENABLE) :
2005 			 BIT(ENDPOINT_ENABLE)));
2006 
2007 	for (i = 1; i < 5; i++)
2008 		writel(tmp, &dev->ep[i].cfg->ep_cfg);
2009 
2010 	/* CSRIN, PCIIN, STATIN, RCIN*/
2011 	tmp = ((0 << ENDPOINT_NUMBER) | BIT(ENDPOINT_ENABLE));
2012 	writel(tmp, &dev->dep[1].dep_cfg);
2013 	writel(tmp, &dev->dep[3].dep_cfg);
2014 	writel(tmp, &dev->dep[4].dep_cfg);
2015 	writel(tmp, &dev->dep[5].dep_cfg);
2016 
2017 	/*Implemented for development and debug.
2018 	 * Can be refined/tuned later.*/
2019 	for (ep_sel = 0; ep_sel <= 21; ep_sel++) {
2020 		/* Select an endpoint for subsequent operations: */
2021 		tmp_reg = readl(&dev->plregs->pl_ep_ctrl);
2022 		writel(((tmp_reg & ~0x1f) | ep_sel),
2023 				&dev->plregs->pl_ep_ctrl);
2024 
2025 		if (ep_sel == 1) {
2026 			tmp =
2027 				(readl(&dev->plregs->pl_ep_ctrl) |
2028 				 BIT(CLEAR_ACK_ERROR_CODE) | 0);
2029 			writel(tmp, &dev->plregs->pl_ep_ctrl);
2030 			continue;
2031 		}
2032 
2033 		if (ep_sel == 0 || (ep_sel > 9 && ep_sel < 14) ||
2034 				ep_sel == 18  || ep_sel == 20)
2035 			continue;
2036 
2037 		tmp = (readl(&dev->plregs->pl_ep_cfg_4) |
2038 				BIT(NON_CTRL_IN_TOLERATE_BAD_DIR) | 0);
2039 		writel(tmp, &dev->plregs->pl_ep_cfg_4);
2040 
2041 		tmp = readl(&dev->plregs->pl_ep_ctrl) &
2042 			~BIT(EP_INITIALIZED);
2043 		writel(tmp, &dev->plregs->pl_ep_ctrl);
2044 
2045 	}
2046 
2047 	/* Set FSM to focus on the first Control Read:
2048 	 * - Tip: Connection speed is known upon the first
2049 	 * setup request.*/
2050 	scratch |= DEFECT7374_FSM_WAITING_FOR_CONTROL_READ;
2051 	set_idx_reg(dev->regs, SCRATCH, scratch);
2052 
2053 }
2054 
2055 /* keeping it simple:
2056  * - one bus driver, initted first;
2057  * - one function driver, initted second
2058  *
2059  * most of the work to support multiple net2280 controllers would
2060  * be to associate this gadget driver (yes?) with all of them, or
2061  * perhaps to bind specific drivers to specific devices.
2062  */
2063 
2064 static void usb_reset_228x(struct net2280 *dev)
2065 {
2066 	u32	tmp;
2067 
2068 	dev->gadget.speed = USB_SPEED_UNKNOWN;
2069 	(void) readl(&dev->usb->usbctl);
2070 
2071 	net2280_led_init(dev);
2072 
2073 	/* disable automatic responses, and irqs */
2074 	writel(0, &dev->usb->stdrsp);
2075 	writel(0, &dev->regs->pciirqenb0);
2076 	writel(0, &dev->regs->pciirqenb1);
2077 
2078 	/* clear old dma and irq state */
2079 	for (tmp = 0; tmp < 4; tmp++) {
2080 		struct net2280_ep       *ep = &dev->ep[tmp + 1];
2081 		if (ep->dma)
2082 			abort_dma(ep);
2083 	}
2084 
2085 	writel(~0, &dev->regs->irqstat0),
2086 	writel(~(u32)BIT(SUSPEND_REQUEST_INTERRUPT), &dev->regs->irqstat1),
2087 
2088 	/* reset, and enable pci */
2089 	tmp = readl(&dev->regs->devinit) |
2090 		BIT(PCI_ENABLE) |
2091 		BIT(FIFO_SOFT_RESET) |
2092 		BIT(USB_SOFT_RESET) |
2093 		BIT(M8051_RESET);
2094 	writel(tmp, &dev->regs->devinit);
2095 
2096 	/* standard fifo and endpoint allocations */
2097 	set_fifo_mode(dev, (fifo_mode <= 2) ? fifo_mode : 0);
2098 }
2099 
2100 static void usb_reset_338x(struct net2280 *dev)
2101 {
2102 	u32 tmp;
2103 
2104 	dev->gadget.speed = USB_SPEED_UNKNOWN;
2105 	(void)readl(&dev->usb->usbctl);
2106 
2107 	net2280_led_init(dev);
2108 
2109 	if (dev->bug7734_patched) {
2110 		/* disable automatic responses, and irqs */
2111 		writel(0, &dev->usb->stdrsp);
2112 		writel(0, &dev->regs->pciirqenb0);
2113 		writel(0, &dev->regs->pciirqenb1);
2114 	}
2115 
2116 	/* clear old dma and irq state */
2117 	for (tmp = 0; tmp < 4; tmp++) {
2118 		struct net2280_ep *ep = &dev->ep[tmp + 1];
2119 		struct net2280_dma_regs __iomem *dma;
2120 
2121 		if (ep->dma) {
2122 			abort_dma(ep);
2123 		} else {
2124 			dma = &dev->dma[tmp];
2125 			writel(BIT(DMA_ABORT), &dma->dmastat);
2126 			writel(0, &dma->dmactl);
2127 		}
2128 	}
2129 
2130 	writel(~0, &dev->regs->irqstat0), writel(~0, &dev->regs->irqstat1);
2131 
2132 	if (dev->bug7734_patched) {
2133 		/* reset, and enable pci */
2134 		tmp = readl(&dev->regs->devinit) |
2135 		    BIT(PCI_ENABLE) |
2136 		    BIT(FIFO_SOFT_RESET) |
2137 		    BIT(USB_SOFT_RESET) |
2138 		    BIT(M8051_RESET);
2139 
2140 		writel(tmp, &dev->regs->devinit);
2141 	}
2142 
2143 	/* always ep-{1,2,3,4} ... maybe not ep-3 or ep-4 */
2144 	INIT_LIST_HEAD(&dev->gadget.ep_list);
2145 
2146 	for (tmp = 1; tmp < dev->n_ep; tmp++)
2147 		list_add_tail(&dev->ep[tmp].ep.ep_list, &dev->gadget.ep_list);
2148 
2149 }
2150 
2151 static void usb_reset(struct net2280 *dev)
2152 {
2153 	if (dev->quirks & PLX_LEGACY)
2154 		return usb_reset_228x(dev);
2155 	return usb_reset_338x(dev);
2156 }
2157 
2158 static void usb_reinit_228x(struct net2280 *dev)
2159 {
2160 	u32	tmp;
2161 
2162 	/* basic endpoint init */
2163 	for (tmp = 0; tmp < 7; tmp++) {
2164 		struct net2280_ep	*ep = &dev->ep[tmp];
2165 
2166 		ep->ep.name = ep_info_dft[tmp].name;
2167 		ep->ep.caps = ep_info_dft[tmp].caps;
2168 		ep->dev = dev;
2169 		ep->num = tmp;
2170 
2171 		if (tmp > 0 && tmp <= 4) {
2172 			ep->fifo_size = 1024;
2173 			ep->dma = &dev->dma[tmp - 1];
2174 		} else
2175 			ep->fifo_size = 64;
2176 		ep->regs = &dev->epregs[tmp];
2177 		ep->cfg = &dev->epregs[tmp];
2178 		ep_reset_228x(dev->regs, ep);
2179 	}
2180 	usb_ep_set_maxpacket_limit(&dev->ep[0].ep, 64);
2181 	usb_ep_set_maxpacket_limit(&dev->ep[5].ep, 64);
2182 	usb_ep_set_maxpacket_limit(&dev->ep[6].ep, 64);
2183 
2184 	dev->gadget.ep0 = &dev->ep[0].ep;
2185 	dev->ep[0].stopped = 0;
2186 	INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
2187 
2188 	/* we want to prevent lowlevel/insecure access from the USB host,
2189 	 * but erratum 0119 means this enable bit is ignored
2190 	 */
2191 	for (tmp = 0; tmp < 5; tmp++)
2192 		writel(EP_DONTUSE, &dev->dep[tmp].dep_cfg);
2193 }
2194 
2195 static void usb_reinit_338x(struct net2280 *dev)
2196 {
2197 	int i;
2198 	u32 tmp, val;
2199 	static const u32 ne[9] = { 0, 1, 2, 3, 4, 1, 2, 3, 4 };
2200 	static const u32 ep_reg_addr[9] = { 0x00, 0xC0, 0x00, 0xC0, 0x00,
2201 						0x00, 0xC0, 0x00, 0xC0 };
2202 
2203 	/* basic endpoint init */
2204 	for (i = 0; i < dev->n_ep; i++) {
2205 		struct net2280_ep *ep = &dev->ep[i];
2206 
2207 		ep->ep.name = dev->enhanced_mode ? ep_info_adv[i].name :
2208 						   ep_info_dft[i].name;
2209 		ep->ep.caps = dev->enhanced_mode ? ep_info_adv[i].caps :
2210 						   ep_info_dft[i].caps;
2211 		ep->dev = dev;
2212 		ep->num = i;
2213 
2214 		if (i > 0 && i <= 4)
2215 			ep->dma = &dev->dma[i - 1];
2216 
2217 		if (dev->enhanced_mode) {
2218 			ep->cfg = &dev->epregs[ne[i]];
2219 			/*
2220 			 * Set USB endpoint number, hardware allows same number
2221 			 * in both directions.
2222 			 */
2223 			 if (i > 0 && i < 5)
2224 				writel(ne[i], &ep->cfg->ep_cfg);
2225 			ep->regs = (struct net2280_ep_regs __iomem *)
2226 				(((void __iomem *)&dev->epregs[ne[i]]) +
2227 				ep_reg_addr[i]);
2228 		} else {
2229 			ep->cfg = &dev->epregs[i];
2230 			ep->regs = &dev->epregs[i];
2231 		}
2232 
2233 		ep->fifo_size = (i != 0) ? 2048 : 512;
2234 
2235 		ep_reset_338x(dev->regs, ep);
2236 	}
2237 	usb_ep_set_maxpacket_limit(&dev->ep[0].ep, 512);
2238 
2239 	dev->gadget.ep0 = &dev->ep[0].ep;
2240 	dev->ep[0].stopped = 0;
2241 
2242 	/* Link layer set up */
2243 	if (dev->bug7734_patched) {
2244 		tmp = readl(&dev->usb_ext->usbctl2) &
2245 		    ~(BIT(U1_ENABLE) | BIT(U2_ENABLE) | BIT(LTM_ENABLE));
2246 		writel(tmp, &dev->usb_ext->usbctl2);
2247 	}
2248 
2249 	/* Hardware Defect and Workaround */
2250 	val = readl(&dev->ll_lfps_regs->ll_lfps_5);
2251 	val &= ~(0xf << TIMER_LFPS_6US);
2252 	val |= 0x5 << TIMER_LFPS_6US;
2253 	writel(val, &dev->ll_lfps_regs->ll_lfps_5);
2254 
2255 	val = readl(&dev->ll_lfps_regs->ll_lfps_6);
2256 	val &= ~(0xffff << TIMER_LFPS_80US);
2257 	val |= 0x0100 << TIMER_LFPS_80US;
2258 	writel(val, &dev->ll_lfps_regs->ll_lfps_6);
2259 
2260 	/*
2261 	 * AA_AB Errata. Issue 4. Workaround for SuperSpeed USB
2262 	 * Hot Reset Exit Handshake may Fail in Specific Case using
2263 	 * Default Register Settings. Workaround for Enumeration test.
2264 	 */
2265 	val = readl(&dev->ll_tsn_regs->ll_tsn_counters_2);
2266 	val &= ~(0x1f << HOT_TX_NORESET_TS2);
2267 	val |= 0x10 << HOT_TX_NORESET_TS2;
2268 	writel(val, &dev->ll_tsn_regs->ll_tsn_counters_2);
2269 
2270 	val = readl(&dev->ll_tsn_regs->ll_tsn_counters_3);
2271 	val &= ~(0x1f << HOT_RX_RESET_TS2);
2272 	val |= 0x3 << HOT_RX_RESET_TS2;
2273 	writel(val, &dev->ll_tsn_regs->ll_tsn_counters_3);
2274 
2275 	/*
2276 	 * Set Recovery Idle to Recover bit:
2277 	 * - On SS connections, setting Recovery Idle to Recover Fmw improves
2278 	 *   link robustness with various hosts and hubs.
2279 	 * - It is safe to set for all connection speeds; all chip revisions.
2280 	 * - R-M-W to leave other bits undisturbed.
2281 	 * - Reference PLX TT-7372
2282 	*/
2283 	val = readl(&dev->ll_chicken_reg->ll_tsn_chicken_bit);
2284 	val |= BIT(RECOVERY_IDLE_TO_RECOVER_FMW);
2285 	writel(val, &dev->ll_chicken_reg->ll_tsn_chicken_bit);
2286 
2287 	INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
2288 
2289 	/* disable dedicated endpoints */
2290 	writel(0x0D, &dev->dep[0].dep_cfg);
2291 	writel(0x0D, &dev->dep[1].dep_cfg);
2292 	writel(0x0E, &dev->dep[2].dep_cfg);
2293 	writel(0x0E, &dev->dep[3].dep_cfg);
2294 	writel(0x0F, &dev->dep[4].dep_cfg);
2295 	writel(0x0C, &dev->dep[5].dep_cfg);
2296 }
2297 
2298 static void usb_reinit(struct net2280 *dev)
2299 {
2300 	if (dev->quirks & PLX_LEGACY)
2301 		return usb_reinit_228x(dev);
2302 	return usb_reinit_338x(dev);
2303 }
2304 
2305 static void ep0_start_228x(struct net2280 *dev)
2306 {
2307 	writel(BIT(CLEAR_EP_HIDE_STATUS_PHASE) |
2308 		BIT(CLEAR_NAK_OUT_PACKETS) |
2309 		BIT(CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE),
2310 		&dev->epregs[0].ep_rsp);
2311 
2312 	/*
2313 	 * hardware optionally handles a bunch of standard requests
2314 	 * that the API hides from drivers anyway.  have it do so.
2315 	 * endpoint status/features are handled in software, to
2316 	 * help pass tests for some dubious behavior.
2317 	 */
2318 	writel(BIT(SET_TEST_MODE) |
2319 		BIT(SET_ADDRESS) |
2320 		BIT(DEVICE_SET_CLEAR_DEVICE_REMOTE_WAKEUP) |
2321 		BIT(GET_DEVICE_STATUS) |
2322 		BIT(GET_INTERFACE_STATUS),
2323 		&dev->usb->stdrsp);
2324 	writel(BIT(USB_ROOT_PORT_WAKEUP_ENABLE) |
2325 		BIT(SELF_POWERED_USB_DEVICE) |
2326 		BIT(REMOTE_WAKEUP_SUPPORT) |
2327 		(dev->softconnect << USB_DETECT_ENABLE) |
2328 		BIT(SELF_POWERED_STATUS),
2329 		&dev->usb->usbctl);
2330 
2331 	/* enable irqs so we can see ep0 and general operation  */
2332 	writel(BIT(SETUP_PACKET_INTERRUPT_ENABLE) |
2333 		BIT(ENDPOINT_0_INTERRUPT_ENABLE),
2334 		&dev->regs->pciirqenb0);
2335 	writel(BIT(PCI_INTERRUPT_ENABLE) |
2336 		BIT(PCI_MASTER_ABORT_RECEIVED_INTERRUPT_ENABLE) |
2337 		BIT(PCI_TARGET_ABORT_RECEIVED_INTERRUPT_ENABLE) |
2338 		BIT(PCI_RETRY_ABORT_INTERRUPT_ENABLE) |
2339 		BIT(VBUS_INTERRUPT_ENABLE) |
2340 		BIT(ROOT_PORT_RESET_INTERRUPT_ENABLE) |
2341 		BIT(SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE),
2342 		&dev->regs->pciirqenb1);
2343 
2344 	/* don't leave any writes posted */
2345 	(void) readl(&dev->usb->usbctl);
2346 }
2347 
2348 static void ep0_start_338x(struct net2280 *dev)
2349 {
2350 
2351 	if (dev->bug7734_patched)
2352 		writel(BIT(CLEAR_NAK_OUT_PACKETS_MODE) |
2353 		       BIT(SET_EP_HIDE_STATUS_PHASE),
2354 		       &dev->epregs[0].ep_rsp);
2355 
2356 	/*
2357 	 * hardware optionally handles a bunch of standard requests
2358 	 * that the API hides from drivers anyway.  have it do so.
2359 	 * endpoint status/features are handled in software, to
2360 	 * help pass tests for some dubious behavior.
2361 	 */
2362 	writel(BIT(SET_ISOCHRONOUS_DELAY) |
2363 	       BIT(SET_SEL) |
2364 	       BIT(SET_TEST_MODE) |
2365 	       BIT(SET_ADDRESS) |
2366 	       BIT(GET_INTERFACE_STATUS) |
2367 	       BIT(GET_DEVICE_STATUS),
2368 		&dev->usb->stdrsp);
2369 	dev->wakeup_enable = 1;
2370 	writel(BIT(USB_ROOT_PORT_WAKEUP_ENABLE) |
2371 	       (dev->softconnect << USB_DETECT_ENABLE) |
2372 	       BIT(DEVICE_REMOTE_WAKEUP_ENABLE),
2373 	       &dev->usb->usbctl);
2374 
2375 	/* enable irqs so we can see ep0 and general operation  */
2376 	writel(BIT(SETUP_PACKET_INTERRUPT_ENABLE) |
2377 	       BIT(ENDPOINT_0_INTERRUPT_ENABLE),
2378 	       &dev->regs->pciirqenb0);
2379 	writel(BIT(PCI_INTERRUPT_ENABLE) |
2380 	       BIT(ROOT_PORT_RESET_INTERRUPT_ENABLE) |
2381 	       BIT(SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE) |
2382 	       BIT(VBUS_INTERRUPT_ENABLE),
2383 	       &dev->regs->pciirqenb1);
2384 
2385 	/* don't leave any writes posted */
2386 	(void)readl(&dev->usb->usbctl);
2387 }
2388 
2389 static void ep0_start(struct net2280 *dev)
2390 {
2391 	if (dev->quirks & PLX_LEGACY)
2392 		return ep0_start_228x(dev);
2393 	return ep0_start_338x(dev);
2394 }
2395 
2396 /* when a driver is successfully registered, it will receive
2397  * control requests including set_configuration(), which enables
2398  * non-control requests.  then usb traffic follows until a
2399  * disconnect is reported.  then a host may connect again, or
2400  * the driver might get unbound.
2401  */
2402 static int net2280_start(struct usb_gadget *_gadget,
2403 		struct usb_gadget_driver *driver)
2404 {
2405 	struct net2280		*dev;
2406 	int			retval;
2407 	unsigned		i;
2408 
2409 	/* insist on high speed support from the driver, since
2410 	 * (dev->usb->xcvrdiag & FORCE_FULL_SPEED_MODE)
2411 	 * "must not be used in normal operation"
2412 	 */
2413 	if (!driver || driver->max_speed < USB_SPEED_HIGH ||
2414 			!driver->setup)
2415 		return -EINVAL;
2416 
2417 	dev = container_of(_gadget, struct net2280, gadget);
2418 
2419 	for (i = 0; i < dev->n_ep; i++)
2420 		dev->ep[i].irqs = 0;
2421 
2422 	/* hook up the driver ... */
2423 	driver->driver.bus = NULL;
2424 	dev->driver = driver;
2425 
2426 	retval = device_create_file(&dev->pdev->dev, &dev_attr_function);
2427 	if (retval)
2428 		goto err_unbind;
2429 	retval = device_create_file(&dev->pdev->dev, &dev_attr_queues);
2430 	if (retval)
2431 		goto err_func;
2432 
2433 	/* enable host detection and ep0; and we're ready
2434 	 * for set_configuration as well as eventual disconnect.
2435 	 */
2436 	net2280_led_active(dev, 1);
2437 
2438 	if ((dev->quirks & PLX_PCIE) && !dev->bug7734_patched)
2439 		defect7374_enable_data_eps_zero(dev);
2440 
2441 	ep0_start(dev);
2442 
2443 	/* pci writes may still be posted */
2444 	return 0;
2445 
2446 err_func:
2447 	device_remove_file(&dev->pdev->dev, &dev_attr_function);
2448 err_unbind:
2449 	dev->driver = NULL;
2450 	return retval;
2451 }
2452 
2453 static void stop_activity(struct net2280 *dev, struct usb_gadget_driver *driver)
2454 {
2455 	int			i;
2456 
2457 	/* don't disconnect if it's not connected */
2458 	if (dev->gadget.speed == USB_SPEED_UNKNOWN)
2459 		driver = NULL;
2460 
2461 	/* stop hardware; prevent new request submissions;
2462 	 * and kill any outstanding requests.
2463 	 */
2464 	usb_reset(dev);
2465 	for (i = 0; i < dev->n_ep; i++)
2466 		nuke(&dev->ep[i]);
2467 
2468 	/* report disconnect; the driver is already quiesced */
2469 	if (driver)
2470 		driver->disconnect(&dev->gadget);
2471 
2472 	usb_reinit(dev);
2473 }
2474 
2475 static int net2280_stop(struct usb_gadget *_gadget)
2476 {
2477 	struct net2280	*dev;
2478 	unsigned long	flags;
2479 
2480 	dev = container_of(_gadget, struct net2280, gadget);
2481 
2482 	spin_lock_irqsave(&dev->lock, flags);
2483 	stop_activity(dev, NULL);
2484 	spin_unlock_irqrestore(&dev->lock, flags);
2485 
2486 	net2280_led_active(dev, 0);
2487 
2488 	device_remove_file(&dev->pdev->dev, &dev_attr_function);
2489 	device_remove_file(&dev->pdev->dev, &dev_attr_queues);
2490 
2491 	dev->driver = NULL;
2492 
2493 	return 0;
2494 }
2495 
2496 /*-------------------------------------------------------------------------*/
2497 
2498 /* handle ep0, ep-e, ep-f with 64 byte packets: packet per irq.
2499  * also works for dma-capable endpoints, in pio mode or just
2500  * to manually advance the queue after short OUT transfers.
2501  */
2502 static void handle_ep_small(struct net2280_ep *ep)
2503 {
2504 	struct net2280_request	*req;
2505 	u32			t;
2506 	/* 0 error, 1 mid-data, 2 done */
2507 	int			mode = 1;
2508 
2509 	if (!list_empty(&ep->queue))
2510 		req = list_entry(ep->queue.next,
2511 			struct net2280_request, queue);
2512 	else
2513 		req = NULL;
2514 
2515 	/* ack all, and handle what we care about */
2516 	t = readl(&ep->regs->ep_stat);
2517 	ep->irqs++;
2518 
2519 	ep_vdbg(ep->dev, "%s ack ep_stat %08x, req %p\n",
2520 			ep->ep.name, t, req ? &req->req : NULL);
2521 
2522 	if (!ep->is_in || (ep->dev->quirks & PLX_2280))
2523 		writel(t & ~BIT(NAK_OUT_PACKETS), &ep->regs->ep_stat);
2524 	else
2525 		/* Added for 2282 */
2526 		writel(t, &ep->regs->ep_stat);
2527 
2528 	/* for ep0, monitor token irqs to catch data stage length errors
2529 	 * and to synchronize on status.
2530 	 *
2531 	 * also, to defer reporting of protocol stalls ... here's where
2532 	 * data or status first appears, handling stalls here should never
2533 	 * cause trouble on the host side..
2534 	 *
2535 	 * control requests could be slightly faster without token synch for
2536 	 * status, but status can jam up that way.
2537 	 */
2538 	if (unlikely(ep->num == 0)) {
2539 		if (ep->is_in) {
2540 			/* status; stop NAKing */
2541 			if (t & BIT(DATA_OUT_PING_TOKEN_INTERRUPT)) {
2542 				if (ep->dev->protocol_stall) {
2543 					ep->stopped = 1;
2544 					set_halt(ep);
2545 				}
2546 				if (!req)
2547 					allow_status(ep);
2548 				mode = 2;
2549 			/* reply to extra IN data tokens with a zlp */
2550 			} else if (t & BIT(DATA_IN_TOKEN_INTERRUPT)) {
2551 				if (ep->dev->protocol_stall) {
2552 					ep->stopped = 1;
2553 					set_halt(ep);
2554 					mode = 2;
2555 				} else if (ep->responded &&
2556 						!req && !ep->stopped)
2557 					write_fifo(ep, NULL);
2558 			}
2559 		} else {
2560 			/* status; stop NAKing */
2561 			if (t & BIT(DATA_IN_TOKEN_INTERRUPT)) {
2562 				if (ep->dev->protocol_stall) {
2563 					ep->stopped = 1;
2564 					set_halt(ep);
2565 				}
2566 				mode = 2;
2567 			/* an extra OUT token is an error */
2568 			} else if (((t & BIT(DATA_OUT_PING_TOKEN_INTERRUPT)) &&
2569 					req &&
2570 					req->req.actual == req->req.length) ||
2571 					(ep->responded && !req)) {
2572 				ep->dev->protocol_stall = 1;
2573 				set_halt(ep);
2574 				ep->stopped = 1;
2575 				if (req)
2576 					done(ep, req, -EOVERFLOW);
2577 				req = NULL;
2578 			}
2579 		}
2580 	}
2581 
2582 	if (unlikely(!req))
2583 		return;
2584 
2585 	/* manual DMA queue advance after short OUT */
2586 	if (likely(ep->dma)) {
2587 		if (t & BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT)) {
2588 			struct net2280_request *stuck_req = NULL;
2589 			int	stopped = ep->stopped;
2590 			int	num_completed;
2591 			int	stuck = 0;
2592 			u32	count;
2593 
2594 			/* TRANSFERRED works around OUT_DONE erratum 0112.
2595 			 * we expect (N <= maxpacket) bytes; host wrote M.
2596 			 * iff (M < N) we won't ever see a DMA interrupt.
2597 			 */
2598 			ep->stopped = 1;
2599 			for (count = 0; ; t = readl(&ep->regs->ep_stat)) {
2600 
2601 				/* any preceding dma transfers must finish.
2602 				 * dma handles (M >= N), may empty the queue
2603 				 */
2604 				num_completed = scan_dma_completions(ep);
2605 				if (unlikely(list_empty(&ep->queue) ||
2606 						ep->out_overflow)) {
2607 					req = NULL;
2608 					break;
2609 				}
2610 				req = list_entry(ep->queue.next,
2611 					struct net2280_request, queue);
2612 
2613 				/* here either (M < N), a "real" short rx;
2614 				 * or (M == N) and the queue didn't empty
2615 				 */
2616 				if (likely(t & BIT(FIFO_EMPTY))) {
2617 					count = readl(&ep->dma->dmacount);
2618 					count &= DMA_BYTE_COUNT_MASK;
2619 					if (readl(&ep->dma->dmadesc)
2620 							!= req->td_dma)
2621 						req = NULL;
2622 					break;
2623 				}
2624 
2625 				/* Escape loop if no dma transfers completed
2626 				 * after few retries.
2627 				 */
2628 				if (num_completed == 0) {
2629 					if (stuck_req == req &&
2630 					    readl(&ep->dma->dmadesc) !=
2631 						  req->td_dma && stuck++ > 5) {
2632 						count = readl(
2633 							&ep->dma->dmacount);
2634 						count &= DMA_BYTE_COUNT_MASK;
2635 						req = NULL;
2636 						ep_dbg(ep->dev, "%s escape stuck %d, count %u\n",
2637 							ep->ep.name, stuck,
2638 							count);
2639 						break;
2640 					} else if (stuck_req != req) {
2641 						stuck_req = req;
2642 						stuck = 0;
2643 					}
2644 				} else {
2645 					stuck_req = NULL;
2646 					stuck = 0;
2647 				}
2648 
2649 				udelay(1);
2650 			}
2651 
2652 			/* stop DMA, leave ep NAKing */
2653 			writel(BIT(DMA_ABORT), &ep->dma->dmastat);
2654 			spin_stop_dma(ep->dma);
2655 
2656 			if (likely(req)) {
2657 				req->td->dmacount = 0;
2658 				t = readl(&ep->regs->ep_avail);
2659 				dma_done(ep, req, count,
2660 					(ep->out_overflow || t)
2661 						? -EOVERFLOW : 0);
2662 			}
2663 
2664 			/* also flush to prevent erratum 0106 trouble */
2665 			if (unlikely(ep->out_overflow ||
2666 					(ep->dev->chiprev == 0x0100 &&
2667 					ep->dev->gadget.speed
2668 					== USB_SPEED_FULL))) {
2669 				out_flush(ep);
2670 				ep->out_overflow = 0;
2671 			}
2672 
2673 			/* (re)start dma if needed, stop NAKing */
2674 			ep->stopped = stopped;
2675 			if (!list_empty(&ep->queue))
2676 				restart_dma(ep);
2677 		} else
2678 			ep_dbg(ep->dev, "%s dma ep_stat %08x ??\n",
2679 					ep->ep.name, t);
2680 		return;
2681 
2682 	/* data packet(s) received (in the fifo, OUT) */
2683 	} else if (t & BIT(DATA_PACKET_RECEIVED_INTERRUPT)) {
2684 		if (read_fifo(ep, req) && ep->num != 0)
2685 			mode = 2;
2686 
2687 	/* data packet(s) transmitted (IN) */
2688 	} else if (t & BIT(DATA_PACKET_TRANSMITTED_INTERRUPT)) {
2689 		unsigned	len;
2690 
2691 		len = req->req.length - req->req.actual;
2692 		if (len > ep->ep.maxpacket)
2693 			len = ep->ep.maxpacket;
2694 		req->req.actual += len;
2695 
2696 		/* if we wrote it all, we're usually done */
2697 		/* send zlps until the status stage */
2698 		if ((req->req.actual == req->req.length) &&
2699 			(!req->req.zero || len != ep->ep.maxpacket) && ep->num)
2700 				mode = 2;
2701 
2702 	/* there was nothing to do ...  */
2703 	} else if (mode == 1)
2704 		return;
2705 
2706 	/* done */
2707 	if (mode == 2) {
2708 		/* stream endpoints often resubmit/unlink in completion */
2709 		done(ep, req, 0);
2710 
2711 		/* maybe advance queue to next request */
2712 		if (ep->num == 0) {
2713 			/* NOTE:  net2280 could let gadget driver start the
2714 			 * status stage later. since not all controllers let
2715 			 * them control that, the api doesn't (yet) allow it.
2716 			 */
2717 			if (!ep->stopped)
2718 				allow_status(ep);
2719 			req = NULL;
2720 		} else {
2721 			if (!list_empty(&ep->queue) && !ep->stopped)
2722 				req = list_entry(ep->queue.next,
2723 					struct net2280_request, queue);
2724 			else
2725 				req = NULL;
2726 			if (req && !ep->is_in)
2727 				stop_out_naking(ep);
2728 		}
2729 	}
2730 
2731 	/* is there a buffer for the next packet?
2732 	 * for best streaming performance, make sure there is one.
2733 	 */
2734 	if (req && !ep->stopped) {
2735 
2736 		/* load IN fifo with next packet (may be zlp) */
2737 		if (t & BIT(DATA_PACKET_TRANSMITTED_INTERRUPT))
2738 			write_fifo(ep, &req->req);
2739 	}
2740 }
2741 
2742 static struct net2280_ep *get_ep_by_addr(struct net2280 *dev, u16 wIndex)
2743 {
2744 	struct net2280_ep	*ep;
2745 
2746 	if ((wIndex & USB_ENDPOINT_NUMBER_MASK) == 0)
2747 		return &dev->ep[0];
2748 	list_for_each_entry(ep, &dev->gadget.ep_list, ep.ep_list) {
2749 		u8	bEndpointAddress;
2750 
2751 		if (!ep->desc)
2752 			continue;
2753 		bEndpointAddress = ep->desc->bEndpointAddress;
2754 		if ((wIndex ^ bEndpointAddress) & USB_DIR_IN)
2755 			continue;
2756 		if ((wIndex & 0x0f) == (bEndpointAddress & 0x0f))
2757 			return ep;
2758 	}
2759 	return NULL;
2760 }
2761 
2762 static void defect7374_workaround(struct net2280 *dev, struct usb_ctrlrequest r)
2763 {
2764 	u32 scratch, fsmvalue;
2765 	u32 ack_wait_timeout, state;
2766 
2767 	/* Workaround for Defect 7374 (U1/U2 erroneously rejected): */
2768 	scratch = get_idx_reg(dev->regs, SCRATCH);
2769 	fsmvalue = scratch & (0xf << DEFECT7374_FSM_FIELD);
2770 	scratch &= ~(0xf << DEFECT7374_FSM_FIELD);
2771 
2772 	if (!((fsmvalue == DEFECT7374_FSM_WAITING_FOR_CONTROL_READ) &&
2773 				(r.bRequestType & USB_DIR_IN)))
2774 		return;
2775 
2776 	/* This is the first Control Read for this connection: */
2777 	if (!(readl(&dev->usb->usbstat) & BIT(SUPER_SPEED_MODE))) {
2778 		/*
2779 		 * Connection is NOT SS:
2780 		 * - Connection must be FS or HS.
2781 		 * - This FSM state should allow workaround software to
2782 		 * run after the next USB connection.
2783 		 */
2784 		scratch |= DEFECT7374_FSM_NON_SS_CONTROL_READ;
2785 		dev->bug7734_patched = 1;
2786 		goto restore_data_eps;
2787 	}
2788 
2789 	/* Connection is SS: */
2790 	for (ack_wait_timeout = 0;
2791 			ack_wait_timeout < DEFECT_7374_NUMBEROF_MAX_WAIT_LOOPS;
2792 			ack_wait_timeout++) {
2793 
2794 		state =	readl(&dev->plregs->pl_ep_status_1)
2795 			& (0xff << STATE);
2796 		if ((state >= (ACK_GOOD_NORMAL << STATE)) &&
2797 			(state <= (ACK_GOOD_MORE_ACKS_TO_COME << STATE))) {
2798 			scratch |= DEFECT7374_FSM_SS_CONTROL_READ;
2799 			dev->bug7734_patched = 1;
2800 			break;
2801 		}
2802 
2803 		/*
2804 		 * We have not yet received host's Data Phase ACK
2805 		 * - Wait and try again.
2806 		 */
2807 		udelay(DEFECT_7374_PROCESSOR_WAIT_TIME);
2808 
2809 		continue;
2810 	}
2811 
2812 
2813 	if (ack_wait_timeout >= DEFECT_7374_NUMBEROF_MAX_WAIT_LOOPS) {
2814 		ep_err(dev, "FAIL: Defect 7374 workaround waited but failed "
2815 		"to detect SS host's data phase ACK.");
2816 		ep_err(dev, "PL_EP_STATUS_1(23:16):.Expected from 0x11 to 0x16"
2817 		"got 0x%2.2x.\n", state >> STATE);
2818 	} else {
2819 		ep_warn(dev, "INFO: Defect 7374 workaround waited about\n"
2820 		"%duSec for Control Read Data Phase ACK\n",
2821 			DEFECT_7374_PROCESSOR_WAIT_TIME * ack_wait_timeout);
2822 	}
2823 
2824 restore_data_eps:
2825 	/*
2826 	 * Restore data EPs to their pre-workaround settings (disabled,
2827 	 * initialized, and other details).
2828 	 */
2829 	defect7374_disable_data_eps(dev);
2830 
2831 	set_idx_reg(dev->regs, SCRATCH, scratch);
2832 
2833 	return;
2834 }
2835 
2836 static void ep_clear_seqnum(struct net2280_ep *ep)
2837 {
2838 	struct net2280 *dev = ep->dev;
2839 	u32 val;
2840 	static const u32 ep_pl[9] = { 0, 3, 4, 7, 8, 2, 5, 6, 9 };
2841 
2842 	val = readl(&dev->plregs->pl_ep_ctrl) & ~0x1f;
2843 	val |= ep_pl[ep->num];
2844 	writel(val, &dev->plregs->pl_ep_ctrl);
2845 	val |= BIT(SEQUENCE_NUMBER_RESET);
2846 	writel(val, &dev->plregs->pl_ep_ctrl);
2847 
2848 	return;
2849 }
2850 
2851 static void handle_stat0_irqs_superspeed(struct net2280 *dev,
2852 		struct net2280_ep *ep, struct usb_ctrlrequest r)
2853 {
2854 	int tmp = 0;
2855 
2856 #define	w_value		le16_to_cpu(r.wValue)
2857 #define	w_index		le16_to_cpu(r.wIndex)
2858 #define	w_length	le16_to_cpu(r.wLength)
2859 
2860 	switch (r.bRequest) {
2861 		struct net2280_ep *e;
2862 		u16 status;
2863 
2864 	case USB_REQ_SET_CONFIGURATION:
2865 		dev->addressed_state = !w_value;
2866 		goto usb3_delegate;
2867 
2868 	case USB_REQ_GET_STATUS:
2869 		switch (r.bRequestType) {
2870 		case (USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE):
2871 			status = dev->wakeup_enable ? 0x02 : 0x00;
2872 			if (dev->gadget.is_selfpowered)
2873 				status |= BIT(0);
2874 			status |= (dev->u1_enable << 2 | dev->u2_enable << 3 |
2875 							dev->ltm_enable << 4);
2876 			writel(0, &dev->epregs[0].ep_irqenb);
2877 			set_fifo_bytecount(ep, sizeof(status));
2878 			writel((__force u32) status, &dev->epregs[0].ep_data);
2879 			allow_status_338x(ep);
2880 			break;
2881 
2882 		case (USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_ENDPOINT):
2883 			e = get_ep_by_addr(dev, w_index);
2884 			if (!e)
2885 				goto do_stall3;
2886 			status = readl(&e->regs->ep_rsp) &
2887 						BIT(CLEAR_ENDPOINT_HALT);
2888 			writel(0, &dev->epregs[0].ep_irqenb);
2889 			set_fifo_bytecount(ep, sizeof(status));
2890 			writel((__force u32) status, &dev->epregs[0].ep_data);
2891 			allow_status_338x(ep);
2892 			break;
2893 
2894 		default:
2895 			goto usb3_delegate;
2896 		}
2897 		break;
2898 
2899 	case USB_REQ_CLEAR_FEATURE:
2900 		switch (r.bRequestType) {
2901 		case (USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE):
2902 			if (!dev->addressed_state) {
2903 				switch (w_value) {
2904 				case USB_DEVICE_U1_ENABLE:
2905 					dev->u1_enable = 0;
2906 					writel(readl(&dev->usb_ext->usbctl2) &
2907 						~BIT(U1_ENABLE),
2908 						&dev->usb_ext->usbctl2);
2909 					allow_status_338x(ep);
2910 					goto next_endpoints3;
2911 
2912 				case USB_DEVICE_U2_ENABLE:
2913 					dev->u2_enable = 0;
2914 					writel(readl(&dev->usb_ext->usbctl2) &
2915 						~BIT(U2_ENABLE),
2916 						&dev->usb_ext->usbctl2);
2917 					allow_status_338x(ep);
2918 					goto next_endpoints3;
2919 
2920 				case USB_DEVICE_LTM_ENABLE:
2921 					dev->ltm_enable = 0;
2922 					writel(readl(&dev->usb_ext->usbctl2) &
2923 						~BIT(LTM_ENABLE),
2924 						&dev->usb_ext->usbctl2);
2925 					allow_status_338x(ep);
2926 					goto next_endpoints3;
2927 
2928 				default:
2929 					break;
2930 				}
2931 			}
2932 			if (w_value == USB_DEVICE_REMOTE_WAKEUP) {
2933 				dev->wakeup_enable = 0;
2934 				writel(readl(&dev->usb->usbctl) &
2935 					~BIT(DEVICE_REMOTE_WAKEUP_ENABLE),
2936 					&dev->usb->usbctl);
2937 				allow_status_338x(ep);
2938 				break;
2939 			}
2940 			goto usb3_delegate;
2941 
2942 		case (USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_ENDPOINT):
2943 			e = get_ep_by_addr(dev,	w_index);
2944 			if (!e)
2945 				goto do_stall3;
2946 			if (w_value != USB_ENDPOINT_HALT)
2947 				goto do_stall3;
2948 			ep_vdbg(dev, "%s clear halt\n", e->ep.name);
2949 			/*
2950 			 * Workaround for SS SeqNum not cleared via
2951 			 * Endpoint Halt (Clear) bit. select endpoint
2952 			 */
2953 			ep_clear_seqnum(e);
2954 			clear_halt(e);
2955 			if (!list_empty(&e->queue) && e->td_dma)
2956 				restart_dma(e);
2957 			allow_status(ep);
2958 			ep->stopped = 1;
2959 			break;
2960 
2961 		default:
2962 			goto usb3_delegate;
2963 		}
2964 		break;
2965 	case USB_REQ_SET_FEATURE:
2966 		switch (r.bRequestType) {
2967 		case (USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE):
2968 			if (!dev->addressed_state) {
2969 				switch (w_value) {
2970 				case USB_DEVICE_U1_ENABLE:
2971 					dev->u1_enable = 1;
2972 					writel(readl(&dev->usb_ext->usbctl2) |
2973 						BIT(U1_ENABLE),
2974 						&dev->usb_ext->usbctl2);
2975 					allow_status_338x(ep);
2976 					goto next_endpoints3;
2977 
2978 				case USB_DEVICE_U2_ENABLE:
2979 					dev->u2_enable = 1;
2980 					writel(readl(&dev->usb_ext->usbctl2) |
2981 						BIT(U2_ENABLE),
2982 						&dev->usb_ext->usbctl2);
2983 					allow_status_338x(ep);
2984 					goto next_endpoints3;
2985 
2986 				case USB_DEVICE_LTM_ENABLE:
2987 					dev->ltm_enable = 1;
2988 					writel(readl(&dev->usb_ext->usbctl2) |
2989 						BIT(LTM_ENABLE),
2990 						&dev->usb_ext->usbctl2);
2991 					allow_status_338x(ep);
2992 					goto next_endpoints3;
2993 				default:
2994 					break;
2995 				}
2996 			}
2997 
2998 			if (w_value == USB_DEVICE_REMOTE_WAKEUP) {
2999 				dev->wakeup_enable = 1;
3000 				writel(readl(&dev->usb->usbctl) |
3001 					BIT(DEVICE_REMOTE_WAKEUP_ENABLE),
3002 					&dev->usb->usbctl);
3003 				allow_status_338x(ep);
3004 				break;
3005 			}
3006 			goto usb3_delegate;
3007 
3008 		case (USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_ENDPOINT):
3009 			e = get_ep_by_addr(dev,	w_index);
3010 			if (!e || (w_value != USB_ENDPOINT_HALT))
3011 				goto do_stall3;
3012 			ep->stopped = 1;
3013 			if (ep->num == 0)
3014 				ep->dev->protocol_stall = 1;
3015 			else {
3016 				if (ep->dma)
3017 					abort_dma(ep);
3018 				set_halt(ep);
3019 			}
3020 			allow_status_338x(ep);
3021 			break;
3022 
3023 		default:
3024 			goto usb3_delegate;
3025 		}
3026 
3027 		break;
3028 	default:
3029 
3030 usb3_delegate:
3031 		ep_vdbg(dev, "setup %02x.%02x v%04x i%04x l%04x ep_cfg %08x\n",
3032 				r.bRequestType, r.bRequest,
3033 				w_value, w_index, w_length,
3034 				readl(&ep->cfg->ep_cfg));
3035 
3036 		ep->responded = 0;
3037 		spin_unlock(&dev->lock);
3038 		tmp = dev->driver->setup(&dev->gadget, &r);
3039 		spin_lock(&dev->lock);
3040 	}
3041 do_stall3:
3042 	if (tmp < 0) {
3043 		ep_vdbg(dev, "req %02x.%02x protocol STALL; stat %d\n",
3044 				r.bRequestType, r.bRequest, tmp);
3045 		dev->protocol_stall = 1;
3046 		/* TD 9.9 Halt Endpoint test. TD 9.22 Set feature test */
3047 		set_halt(ep);
3048 	}
3049 
3050 next_endpoints3:
3051 
3052 #undef	w_value
3053 #undef	w_index
3054 #undef	w_length
3055 
3056 	return;
3057 }
3058 
3059 static void usb338x_handle_ep_intr(struct net2280 *dev, u32 stat0)
3060 {
3061 	u32 index;
3062 	u32 bit;
3063 
3064 	for (index = 0; index < ARRAY_SIZE(ep_bit); index++) {
3065 		bit = BIT(ep_bit[index]);
3066 
3067 		if (!stat0)
3068 			break;
3069 
3070 		if (!(stat0 & bit))
3071 			continue;
3072 
3073 		stat0 &= ~bit;
3074 
3075 		handle_ep_small(&dev->ep[index]);
3076 	}
3077 }
3078 
3079 static void handle_stat0_irqs(struct net2280 *dev, u32 stat)
3080 {
3081 	struct net2280_ep	*ep;
3082 	u32			num, scratch;
3083 
3084 	/* most of these don't need individual acks */
3085 	stat &= ~BIT(INTA_ASSERTED);
3086 	if (!stat)
3087 		return;
3088 	/* ep_dbg(dev, "irqstat0 %04x\n", stat); */
3089 
3090 	/* starting a control request? */
3091 	if (unlikely(stat & BIT(SETUP_PACKET_INTERRUPT))) {
3092 		union {
3093 			u32			raw[2];
3094 			struct usb_ctrlrequest	r;
3095 		} u;
3096 		int				tmp;
3097 		struct net2280_request		*req;
3098 
3099 		if (dev->gadget.speed == USB_SPEED_UNKNOWN) {
3100 			u32 val = readl(&dev->usb->usbstat);
3101 			if (val & BIT(SUPER_SPEED)) {
3102 				dev->gadget.speed = USB_SPEED_SUPER;
3103 				usb_ep_set_maxpacket_limit(&dev->ep[0].ep,
3104 						EP0_SS_MAX_PACKET_SIZE);
3105 			} else if (val & BIT(HIGH_SPEED)) {
3106 				dev->gadget.speed = USB_SPEED_HIGH;
3107 				usb_ep_set_maxpacket_limit(&dev->ep[0].ep,
3108 						EP0_HS_MAX_PACKET_SIZE);
3109 			} else {
3110 				dev->gadget.speed = USB_SPEED_FULL;
3111 				usb_ep_set_maxpacket_limit(&dev->ep[0].ep,
3112 						EP0_HS_MAX_PACKET_SIZE);
3113 			}
3114 			net2280_led_speed(dev, dev->gadget.speed);
3115 			ep_dbg(dev, "%s\n",
3116 					usb_speed_string(dev->gadget.speed));
3117 		}
3118 
3119 		ep = &dev->ep[0];
3120 		ep->irqs++;
3121 
3122 		/* make sure any leftover request state is cleared */
3123 		stat &= ~BIT(ENDPOINT_0_INTERRUPT);
3124 		while (!list_empty(&ep->queue)) {
3125 			req = list_entry(ep->queue.next,
3126 					struct net2280_request, queue);
3127 			done(ep, req, (req->req.actual == req->req.length)
3128 						? 0 : -EPROTO);
3129 		}
3130 		ep->stopped = 0;
3131 		dev->protocol_stall = 0;
3132 		if (!(dev->quirks & PLX_PCIE)) {
3133 			if (ep->dev->quirks & PLX_2280)
3134 				tmp = BIT(FIFO_OVERFLOW) |
3135 				    BIT(FIFO_UNDERFLOW);
3136 			else
3137 				tmp = 0;
3138 
3139 			writel(tmp | BIT(TIMEOUT) |
3140 				   BIT(USB_STALL_SENT) |
3141 				   BIT(USB_IN_NAK_SENT) |
3142 				   BIT(USB_IN_ACK_RCVD) |
3143 				   BIT(USB_OUT_PING_NAK_SENT) |
3144 				   BIT(USB_OUT_ACK_SENT) |
3145 				   BIT(SHORT_PACKET_OUT_DONE_INTERRUPT) |
3146 				   BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT) |
3147 				   BIT(DATA_PACKET_RECEIVED_INTERRUPT) |
3148 				   BIT(DATA_PACKET_TRANSMITTED_INTERRUPT) |
3149 				   BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
3150 				   BIT(DATA_IN_TOKEN_INTERRUPT),
3151 				   &ep->regs->ep_stat);
3152 		}
3153 		u.raw[0] = readl(&dev->usb->setup0123);
3154 		u.raw[1] = readl(&dev->usb->setup4567);
3155 
3156 		cpu_to_le32s(&u.raw[0]);
3157 		cpu_to_le32s(&u.raw[1]);
3158 
3159 		if ((dev->quirks & PLX_PCIE) && !dev->bug7734_patched)
3160 			defect7374_workaround(dev, u.r);
3161 
3162 		tmp = 0;
3163 
3164 #define	w_value		le16_to_cpu(u.r.wValue)
3165 #define	w_index		le16_to_cpu(u.r.wIndex)
3166 #define	w_length	le16_to_cpu(u.r.wLength)
3167 
3168 		/* ack the irq */
3169 		writel(BIT(SETUP_PACKET_INTERRUPT), &dev->regs->irqstat0);
3170 		stat ^= BIT(SETUP_PACKET_INTERRUPT);
3171 
3172 		/* watch control traffic at the token level, and force
3173 		 * synchronization before letting the status stage happen.
3174 		 * FIXME ignore tokens we'll NAK, until driver responds.
3175 		 * that'll mean a lot less irqs for some drivers.
3176 		 */
3177 		ep->is_in = (u.r.bRequestType & USB_DIR_IN) != 0;
3178 		if (ep->is_in) {
3179 			scratch = BIT(DATA_PACKET_TRANSMITTED_INTERRUPT) |
3180 				BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
3181 				BIT(DATA_IN_TOKEN_INTERRUPT);
3182 			stop_out_naking(ep);
3183 		} else
3184 			scratch = BIT(DATA_PACKET_RECEIVED_INTERRUPT) |
3185 				BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
3186 				BIT(DATA_IN_TOKEN_INTERRUPT);
3187 		writel(scratch, &dev->epregs[0].ep_irqenb);
3188 
3189 		/* we made the hardware handle most lowlevel requests;
3190 		 * everything else goes uplevel to the gadget code.
3191 		 */
3192 		ep->responded = 1;
3193 
3194 		if (dev->gadget.speed == USB_SPEED_SUPER) {
3195 			handle_stat0_irqs_superspeed(dev, ep, u.r);
3196 			goto next_endpoints;
3197 		}
3198 
3199 		switch (u.r.bRequest) {
3200 		case USB_REQ_GET_STATUS: {
3201 			struct net2280_ep	*e;
3202 			__le32			status;
3203 
3204 			/* hw handles device and interface status */
3205 			if (u.r.bRequestType != (USB_DIR_IN|USB_RECIP_ENDPOINT))
3206 				goto delegate;
3207 			e = get_ep_by_addr(dev, w_index);
3208 			if (!e || w_length > 2)
3209 				goto do_stall;
3210 
3211 			if (readl(&e->regs->ep_rsp) & BIT(SET_ENDPOINT_HALT))
3212 				status = cpu_to_le32(1);
3213 			else
3214 				status = cpu_to_le32(0);
3215 
3216 			/* don't bother with a request object! */
3217 			writel(0, &dev->epregs[0].ep_irqenb);
3218 			set_fifo_bytecount(ep, w_length);
3219 			writel((__force u32)status, &dev->epregs[0].ep_data);
3220 			allow_status(ep);
3221 			ep_vdbg(dev, "%s stat %02x\n", ep->ep.name, status);
3222 			goto next_endpoints;
3223 			}
3224 			break;
3225 		case USB_REQ_CLEAR_FEATURE: {
3226 			struct net2280_ep	*e;
3227 
3228 			/* hw handles device features */
3229 			if (u.r.bRequestType != USB_RECIP_ENDPOINT)
3230 				goto delegate;
3231 			if (w_value != USB_ENDPOINT_HALT || w_length != 0)
3232 				goto do_stall;
3233 			e = get_ep_by_addr(dev, w_index);
3234 			if (!e)
3235 				goto do_stall;
3236 			if (e->wedged) {
3237 				ep_vdbg(dev, "%s wedged, halt not cleared\n",
3238 						ep->ep.name);
3239 			} else {
3240 				ep_vdbg(dev, "%s clear halt\n", e->ep.name);
3241 				clear_halt(e);
3242 				if ((ep->dev->quirks & PLX_PCIE) &&
3243 					!list_empty(&e->queue) && e->td_dma)
3244 						restart_dma(e);
3245 			}
3246 			allow_status(ep);
3247 			goto next_endpoints;
3248 			}
3249 			break;
3250 		case USB_REQ_SET_FEATURE: {
3251 			struct net2280_ep	*e;
3252 
3253 			/* hw handles device features */
3254 			if (u.r.bRequestType != USB_RECIP_ENDPOINT)
3255 				goto delegate;
3256 			if (w_value != USB_ENDPOINT_HALT || w_length != 0)
3257 				goto do_stall;
3258 			e = get_ep_by_addr(dev, w_index);
3259 			if (!e)
3260 				goto do_stall;
3261 			if (e->ep.name == ep0name)
3262 				goto do_stall;
3263 			set_halt(e);
3264 			if ((dev->quirks & PLX_PCIE) && e->dma)
3265 				abort_dma(e);
3266 			allow_status(ep);
3267 			ep_vdbg(dev, "%s set halt\n", ep->ep.name);
3268 			goto next_endpoints;
3269 			}
3270 			break;
3271 		default:
3272 delegate:
3273 			ep_vdbg(dev, "setup %02x.%02x v%04x i%04x l%04x "
3274 				"ep_cfg %08x\n",
3275 				u.r.bRequestType, u.r.bRequest,
3276 				w_value, w_index, w_length,
3277 				readl(&ep->cfg->ep_cfg));
3278 			ep->responded = 0;
3279 			spin_unlock(&dev->lock);
3280 			tmp = dev->driver->setup(&dev->gadget, &u.r);
3281 			spin_lock(&dev->lock);
3282 		}
3283 
3284 		/* stall ep0 on error */
3285 		if (tmp < 0) {
3286 do_stall:
3287 			ep_vdbg(dev, "req %02x.%02x protocol STALL; stat %d\n",
3288 					u.r.bRequestType, u.r.bRequest, tmp);
3289 			dev->protocol_stall = 1;
3290 		}
3291 
3292 		/* some in/out token irq should follow; maybe stall then.
3293 		 * driver must queue a request (even zlp) or halt ep0
3294 		 * before the host times out.
3295 		 */
3296 	}
3297 
3298 #undef	w_value
3299 #undef	w_index
3300 #undef	w_length
3301 
3302 next_endpoints:
3303 	if ((dev->quirks & PLX_PCIE) && dev->enhanced_mode) {
3304 		u32 mask = (BIT(ENDPOINT_0_INTERRUPT) |
3305 			USB3380_IRQSTAT0_EP_INTR_MASK_IN |
3306 			USB3380_IRQSTAT0_EP_INTR_MASK_OUT);
3307 
3308 		if (stat & mask) {
3309 			usb338x_handle_ep_intr(dev, stat & mask);
3310 			stat &= ~mask;
3311 		}
3312 	} else {
3313 		/* endpoint data irq ? */
3314 		scratch = stat & 0x7f;
3315 		stat &= ~0x7f;
3316 		for (num = 0; scratch; num++) {
3317 			u32		t;
3318 
3319 			/* do this endpoint's FIFO and queue need tending? */
3320 			t = BIT(num);
3321 			if ((scratch & t) == 0)
3322 				continue;
3323 			scratch ^= t;
3324 
3325 			ep = &dev->ep[num];
3326 			handle_ep_small(ep);
3327 		}
3328 	}
3329 
3330 	if (stat)
3331 		ep_dbg(dev, "unhandled irqstat0 %08x\n", stat);
3332 }
3333 
3334 #define DMA_INTERRUPTS (BIT(DMA_D_INTERRUPT) | \
3335 		BIT(DMA_C_INTERRUPT) | \
3336 		BIT(DMA_B_INTERRUPT) | \
3337 		BIT(DMA_A_INTERRUPT))
3338 #define	PCI_ERROR_INTERRUPTS ( \
3339 		BIT(PCI_MASTER_ABORT_RECEIVED_INTERRUPT) | \
3340 		BIT(PCI_TARGET_ABORT_RECEIVED_INTERRUPT) | \
3341 		BIT(PCI_RETRY_ABORT_INTERRUPT))
3342 
3343 static void handle_stat1_irqs(struct net2280 *dev, u32 stat)
3344 {
3345 	struct net2280_ep	*ep;
3346 	u32			tmp, num, mask, scratch;
3347 
3348 	/* after disconnect there's nothing else to do! */
3349 	tmp = BIT(VBUS_INTERRUPT) | BIT(ROOT_PORT_RESET_INTERRUPT);
3350 	mask = BIT(SUPER_SPEED) | BIT(HIGH_SPEED) | BIT(FULL_SPEED);
3351 
3352 	/* VBUS disconnect is indicated by VBUS_PIN and VBUS_INTERRUPT set.
3353 	 * Root Port Reset is indicated by ROOT_PORT_RESET_INTERRUPT set and
3354 	 * both HIGH_SPEED and FULL_SPEED clear (as ROOT_PORT_RESET_INTERRUPT
3355 	 * only indicates a change in the reset state).
3356 	 */
3357 	if (stat & tmp) {
3358 		bool	reset = false;
3359 		bool	disconnect = false;
3360 
3361 		/*
3362 		 * Ignore disconnects and resets if the speed hasn't been set.
3363 		 * VBUS can bounce and there's always an initial reset.
3364 		 */
3365 		writel(tmp, &dev->regs->irqstat1);
3366 		if (dev->gadget.speed != USB_SPEED_UNKNOWN) {
3367 			if ((stat & BIT(VBUS_INTERRUPT)) &&
3368 					(readl(&dev->usb->usbctl) &
3369 						BIT(VBUS_PIN)) == 0) {
3370 				disconnect = true;
3371 				ep_dbg(dev, "disconnect %s\n",
3372 						dev->driver->driver.name);
3373 			} else if ((stat & BIT(ROOT_PORT_RESET_INTERRUPT)) &&
3374 					(readl(&dev->usb->usbstat) & mask)
3375 						== 0) {
3376 				reset = true;
3377 				ep_dbg(dev, "reset %s\n",
3378 						dev->driver->driver.name);
3379 			}
3380 
3381 			if (disconnect || reset) {
3382 				stop_activity(dev, dev->driver);
3383 				ep0_start(dev);
3384 				if (reset)
3385 					usb_gadget_udc_reset
3386 						(&dev->gadget, dev->driver);
3387 				else
3388 					(dev->driver->disconnect)
3389 						(&dev->gadget);
3390 				return;
3391 			}
3392 		}
3393 		stat &= ~tmp;
3394 
3395 		/* vBUS can bounce ... one of many reasons to ignore the
3396 		 * notion of hotplug events on bus connect/disconnect!
3397 		 */
3398 		if (!stat)
3399 			return;
3400 	}
3401 
3402 	/* NOTE: chip stays in PCI D0 state for now, but it could
3403 	 * enter D1 to save more power
3404 	 */
3405 	tmp = BIT(SUSPEND_REQUEST_CHANGE_INTERRUPT);
3406 	if (stat & tmp) {
3407 		writel(tmp, &dev->regs->irqstat1);
3408 		if (stat & BIT(SUSPEND_REQUEST_INTERRUPT)) {
3409 			if (dev->driver->suspend)
3410 				dev->driver->suspend(&dev->gadget);
3411 			if (!enable_suspend)
3412 				stat &= ~BIT(SUSPEND_REQUEST_INTERRUPT);
3413 		} else {
3414 			if (dev->driver->resume)
3415 				dev->driver->resume(&dev->gadget);
3416 			/* at high speed, note erratum 0133 */
3417 		}
3418 		stat &= ~tmp;
3419 	}
3420 
3421 	/* clear any other status/irqs */
3422 	if (stat)
3423 		writel(stat, &dev->regs->irqstat1);
3424 
3425 	/* some status we can just ignore */
3426 	if (dev->quirks & PLX_2280)
3427 		stat &= ~(BIT(CONTROL_STATUS_INTERRUPT) |
3428 			  BIT(SUSPEND_REQUEST_INTERRUPT) |
3429 			  BIT(RESUME_INTERRUPT) |
3430 			  BIT(SOF_INTERRUPT));
3431 	else
3432 		stat &= ~(BIT(CONTROL_STATUS_INTERRUPT) |
3433 			  BIT(RESUME_INTERRUPT) |
3434 			  BIT(SOF_DOWN_INTERRUPT) |
3435 			  BIT(SOF_INTERRUPT));
3436 
3437 	if (!stat)
3438 		return;
3439 	/* ep_dbg(dev, "irqstat1 %08x\n", stat);*/
3440 
3441 	/* DMA status, for ep-{a,b,c,d} */
3442 	scratch = stat & DMA_INTERRUPTS;
3443 	stat &= ~DMA_INTERRUPTS;
3444 	scratch >>= 9;
3445 	for (num = 0; scratch; num++) {
3446 		struct net2280_dma_regs	__iomem *dma;
3447 
3448 		tmp = BIT(num);
3449 		if ((tmp & scratch) == 0)
3450 			continue;
3451 		scratch ^= tmp;
3452 
3453 		ep = &dev->ep[num + 1];
3454 		dma = ep->dma;
3455 
3456 		if (!dma)
3457 			continue;
3458 
3459 		/* clear ep's dma status */
3460 		tmp = readl(&dma->dmastat);
3461 		writel(tmp, &dma->dmastat);
3462 
3463 		/* dma sync*/
3464 		if (dev->quirks & PLX_PCIE) {
3465 			u32 r_dmacount = readl(&dma->dmacount);
3466 			if (!ep->is_in &&  (r_dmacount & 0x00FFFFFF) &&
3467 			    (tmp & BIT(DMA_TRANSACTION_DONE_INTERRUPT)))
3468 				continue;
3469 		}
3470 
3471 		if (!(tmp & BIT(DMA_TRANSACTION_DONE_INTERRUPT))) {
3472 			ep_dbg(ep->dev, "%s no xact done? %08x\n",
3473 				ep->ep.name, tmp);
3474 			continue;
3475 		}
3476 		stop_dma(ep->dma);
3477 
3478 		/* OUT transfers terminate when the data from the
3479 		 * host is in our memory.  Process whatever's done.
3480 		 * On this path, we know transfer's last packet wasn't
3481 		 * less than req->length. NAK_OUT_PACKETS may be set,
3482 		 * or the FIFO may already be holding new packets.
3483 		 *
3484 		 * IN transfers can linger in the FIFO for a very
3485 		 * long time ... we ignore that for now, accounting
3486 		 * precisely (like PIO does) needs per-packet irqs
3487 		 */
3488 		scan_dma_completions(ep);
3489 
3490 		/* disable dma on inactive queues; else maybe restart */
3491 		if (!list_empty(&ep->queue)) {
3492 			tmp = readl(&dma->dmactl);
3493 			restart_dma(ep);
3494 		}
3495 		ep->irqs++;
3496 	}
3497 
3498 	/* NOTE:  there are other PCI errors we might usefully notice.
3499 	 * if they appear very often, here's where to try recovering.
3500 	 */
3501 	if (stat & PCI_ERROR_INTERRUPTS) {
3502 		ep_err(dev, "pci dma error; stat %08x\n", stat);
3503 		stat &= ~PCI_ERROR_INTERRUPTS;
3504 		/* these are fatal errors, but "maybe" they won't
3505 		 * happen again ...
3506 		 */
3507 		stop_activity(dev, dev->driver);
3508 		ep0_start(dev);
3509 		stat = 0;
3510 	}
3511 
3512 	if (stat)
3513 		ep_dbg(dev, "unhandled irqstat1 %08x\n", stat);
3514 }
3515 
3516 static irqreturn_t net2280_irq(int irq, void *_dev)
3517 {
3518 	struct net2280		*dev = _dev;
3519 
3520 	/* shared interrupt, not ours */
3521 	if ((dev->quirks & PLX_LEGACY) &&
3522 		(!(readl(&dev->regs->irqstat0) & BIT(INTA_ASSERTED))))
3523 		return IRQ_NONE;
3524 
3525 	spin_lock(&dev->lock);
3526 
3527 	/* handle disconnect, dma, and more */
3528 	handle_stat1_irqs(dev, readl(&dev->regs->irqstat1));
3529 
3530 	/* control requests and PIO */
3531 	handle_stat0_irqs(dev, readl(&dev->regs->irqstat0));
3532 
3533 	if (dev->quirks & PLX_PCIE) {
3534 		/* re-enable interrupt to trigger any possible new interrupt */
3535 		u32 pciirqenb1 = readl(&dev->regs->pciirqenb1);
3536 		writel(pciirqenb1 & 0x7FFFFFFF, &dev->regs->pciirqenb1);
3537 		writel(pciirqenb1, &dev->regs->pciirqenb1);
3538 	}
3539 
3540 	spin_unlock(&dev->lock);
3541 
3542 	return IRQ_HANDLED;
3543 }
3544 
3545 /*-------------------------------------------------------------------------*/
3546 
3547 static void gadget_release(struct device *_dev)
3548 {
3549 	struct net2280	*dev = dev_get_drvdata(_dev);
3550 
3551 	kfree(dev);
3552 }
3553 
3554 /* tear down the binding between this driver and the pci device */
3555 
3556 static void net2280_remove(struct pci_dev *pdev)
3557 {
3558 	struct net2280		*dev = pci_get_drvdata(pdev);
3559 
3560 	usb_del_gadget_udc(&dev->gadget);
3561 
3562 	BUG_ON(dev->driver);
3563 
3564 	/* then clean up the resources we allocated during probe() */
3565 	if (dev->requests) {
3566 		int		i;
3567 		for (i = 1; i < 5; i++) {
3568 			if (!dev->ep[i].dummy)
3569 				continue;
3570 			dma_pool_free(dev->requests, dev->ep[i].dummy,
3571 					dev->ep[i].td_dma);
3572 		}
3573 		dma_pool_destroy(dev->requests);
3574 	}
3575 	if (dev->got_irq)
3576 		free_irq(pdev->irq, dev);
3577 	if (dev->quirks & PLX_PCIE)
3578 		pci_disable_msi(pdev);
3579 	if (dev->regs) {
3580 		net2280_led_shutdown(dev);
3581 		iounmap(dev->regs);
3582 	}
3583 	if (dev->region)
3584 		release_mem_region(pci_resource_start(pdev, 0),
3585 				pci_resource_len(pdev, 0));
3586 	if (dev->enabled)
3587 		pci_disable_device(pdev);
3588 	device_remove_file(&pdev->dev, &dev_attr_registers);
3589 
3590 	ep_info(dev, "unbind\n");
3591 }
3592 
3593 /* wrap this driver around the specified device, but
3594  * don't respond over USB until a gadget driver binds to us.
3595  */
3596 
3597 static int net2280_probe(struct pci_dev *pdev, const struct pci_device_id *id)
3598 {
3599 	struct net2280		*dev;
3600 	unsigned long		resource, len;
3601 	void			__iomem *base = NULL;
3602 	int			retval, i;
3603 
3604 	/* alloc, and start init */
3605 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
3606 	if (dev == NULL) {
3607 		retval = -ENOMEM;
3608 		goto done;
3609 	}
3610 
3611 	pci_set_drvdata(pdev, dev);
3612 	spin_lock_init(&dev->lock);
3613 	dev->quirks = id->driver_data;
3614 	dev->pdev = pdev;
3615 	dev->gadget.ops = &net2280_ops;
3616 	dev->gadget.max_speed = (dev->quirks & PLX_SUPERSPEED) ?
3617 				USB_SPEED_SUPER : USB_SPEED_HIGH;
3618 
3619 	/* the "gadget" abstracts/virtualizes the controller */
3620 	dev->gadget.name = driver_name;
3621 
3622 	/* now all the pci goodies ... */
3623 	if (pci_enable_device(pdev) < 0) {
3624 		retval = -ENODEV;
3625 		goto done;
3626 	}
3627 	dev->enabled = 1;
3628 
3629 	/* BAR 0 holds all the registers
3630 	 * BAR 1 is 8051 memory; unused here (note erratum 0103)
3631 	 * BAR 2 is fifo memory; unused here
3632 	 */
3633 	resource = pci_resource_start(pdev, 0);
3634 	len = pci_resource_len(pdev, 0);
3635 	if (!request_mem_region(resource, len, driver_name)) {
3636 		ep_dbg(dev, "controller already in use\n");
3637 		retval = -EBUSY;
3638 		goto done;
3639 	}
3640 	dev->region = 1;
3641 
3642 	/* FIXME provide firmware download interface to put
3643 	 * 8051 code into the chip, e.g. to turn on PCI PM.
3644 	 */
3645 
3646 	base = ioremap_nocache(resource, len);
3647 	if (base == NULL) {
3648 		ep_dbg(dev, "can't map memory\n");
3649 		retval = -EFAULT;
3650 		goto done;
3651 	}
3652 	dev->regs = (struct net2280_regs __iomem *) base;
3653 	dev->usb = (struct net2280_usb_regs __iomem *) (base + 0x0080);
3654 	dev->pci = (struct net2280_pci_regs __iomem *) (base + 0x0100);
3655 	dev->dma = (struct net2280_dma_regs __iomem *) (base + 0x0180);
3656 	dev->dep = (struct net2280_dep_regs __iomem *) (base + 0x0200);
3657 	dev->epregs = (struct net2280_ep_regs __iomem *) (base + 0x0300);
3658 
3659 	if (dev->quirks & PLX_PCIE) {
3660 		u32 fsmvalue;
3661 		u32 usbstat;
3662 		dev->usb_ext = (struct usb338x_usb_ext_regs __iomem *)
3663 							(base + 0x00b4);
3664 		dev->llregs = (struct usb338x_ll_regs __iomem *)
3665 							(base + 0x0700);
3666 		dev->ll_lfps_regs = (struct usb338x_ll_lfps_regs __iomem *)
3667 							(base + 0x0748);
3668 		dev->ll_tsn_regs = (struct usb338x_ll_tsn_regs __iomem *)
3669 							(base + 0x077c);
3670 		dev->ll_chicken_reg = (struct usb338x_ll_chi_regs __iomem *)
3671 							(base + 0x079c);
3672 		dev->plregs = (struct usb338x_pl_regs __iomem *)
3673 							(base + 0x0800);
3674 		usbstat = readl(&dev->usb->usbstat);
3675 		dev->enhanced_mode = !!(usbstat & BIT(11));
3676 		dev->n_ep = (dev->enhanced_mode) ? 9 : 5;
3677 		/* put into initial config, link up all endpoints */
3678 		fsmvalue = get_idx_reg(dev->regs, SCRATCH) &
3679 					(0xf << DEFECT7374_FSM_FIELD);
3680 		/* See if firmware needs to set up for workaround: */
3681 		if (fsmvalue == DEFECT7374_FSM_SS_CONTROL_READ) {
3682 			dev->bug7734_patched = 1;
3683 			writel(0, &dev->usb->usbctl);
3684 		} else
3685 			dev->bug7734_patched = 0;
3686 	} else {
3687 		dev->enhanced_mode = 0;
3688 		dev->n_ep = 7;
3689 		/* put into initial config, link up all endpoints */
3690 		writel(0, &dev->usb->usbctl);
3691 	}
3692 
3693 	usb_reset(dev);
3694 	usb_reinit(dev);
3695 
3696 	/* irq setup after old hardware is cleaned up */
3697 	if (!pdev->irq) {
3698 		ep_err(dev, "No IRQ.  Check PCI setup!\n");
3699 		retval = -ENODEV;
3700 		goto done;
3701 	}
3702 
3703 	if (dev->quirks & PLX_PCIE)
3704 		if (pci_enable_msi(pdev))
3705 			ep_err(dev, "Failed to enable MSI mode\n");
3706 
3707 	if (request_irq(pdev->irq, net2280_irq, IRQF_SHARED,
3708 							driver_name, dev)) {
3709 		ep_err(dev, "request interrupt %d failed\n", pdev->irq);
3710 		retval = -EBUSY;
3711 		goto done;
3712 	}
3713 	dev->got_irq = 1;
3714 
3715 	/* DMA setup */
3716 	/* NOTE:  we know only the 32 LSBs of dma addresses may be nonzero */
3717 	dev->requests = dma_pool_create("requests", &pdev->dev,
3718 		sizeof(struct net2280_dma),
3719 		0 /* no alignment requirements */,
3720 		0 /* or page-crossing issues */);
3721 	if (!dev->requests) {
3722 		ep_dbg(dev, "can't get request pool\n");
3723 		retval = -ENOMEM;
3724 		goto done;
3725 	}
3726 	for (i = 1; i < 5; i++) {
3727 		struct net2280_dma	*td;
3728 
3729 		td = dma_pool_alloc(dev->requests, GFP_KERNEL,
3730 				&dev->ep[i].td_dma);
3731 		if (!td) {
3732 			ep_dbg(dev, "can't get dummy %d\n", i);
3733 			retval = -ENOMEM;
3734 			goto done;
3735 		}
3736 		td->dmacount = 0;	/* not VALID */
3737 		td->dmadesc = td->dmaaddr;
3738 		dev->ep[i].dummy = td;
3739 	}
3740 
3741 	/* enable lower-overhead pci memory bursts during DMA */
3742 	if (dev->quirks & PLX_LEGACY)
3743 		writel(BIT(DMA_MEMORY_WRITE_AND_INVALIDATE_ENABLE) |
3744 			/*
3745 			 * 256 write retries may not be enough...
3746 			   BIT(PCI_RETRY_ABORT_ENABLE) |
3747 			*/
3748 			BIT(DMA_READ_MULTIPLE_ENABLE) |
3749 			BIT(DMA_READ_LINE_ENABLE),
3750 			&dev->pci->pcimstctl);
3751 	/* erratum 0115 shouldn't appear: Linux inits PCI_LATENCY_TIMER */
3752 	pci_set_master(pdev);
3753 	pci_try_set_mwi(pdev);
3754 
3755 	/* ... also flushes any posted pci writes */
3756 	dev->chiprev = get_idx_reg(dev->regs, REG_CHIPREV) & 0xffff;
3757 
3758 	/* done */
3759 	ep_info(dev, "%s\n", driver_desc);
3760 	ep_info(dev, "irq %d, pci mem %p, chip rev %04x\n",
3761 			pdev->irq, base, dev->chiprev);
3762 	ep_info(dev, "version: " DRIVER_VERSION "; %s\n",
3763 		dev->enhanced_mode ? "enhanced mode" : "legacy mode");
3764 	retval = device_create_file(&pdev->dev, &dev_attr_registers);
3765 	if (retval)
3766 		goto done;
3767 
3768 	retval = usb_add_gadget_udc_release(&pdev->dev, &dev->gadget,
3769 			gadget_release);
3770 	if (retval)
3771 		goto done;
3772 	return 0;
3773 
3774 done:
3775 	if (dev)
3776 		net2280_remove(pdev);
3777 	return retval;
3778 }
3779 
3780 /* make sure the board is quiescent; otherwise it will continue
3781  * generating IRQs across the upcoming reboot.
3782  */
3783 
3784 static void net2280_shutdown(struct pci_dev *pdev)
3785 {
3786 	struct net2280		*dev = pci_get_drvdata(pdev);
3787 
3788 	/* disable IRQs */
3789 	writel(0, &dev->regs->pciirqenb0);
3790 	writel(0, &dev->regs->pciirqenb1);
3791 
3792 	/* disable the pullup so the host will think we're gone */
3793 	writel(0, &dev->usb->usbctl);
3794 
3795 }
3796 
3797 
3798 /*-------------------------------------------------------------------------*/
3799 
3800 static const struct pci_device_id pci_ids[] = { {
3801 	.class =	PCI_CLASS_SERIAL_USB_DEVICE,
3802 	.class_mask =	~0,
3803 	.vendor =	PCI_VENDOR_ID_PLX_LEGACY,
3804 	.device =	0x2280,
3805 	.subvendor =	PCI_ANY_ID,
3806 	.subdevice =	PCI_ANY_ID,
3807 	.driver_data =	PLX_LEGACY | PLX_2280,
3808 	}, {
3809 	.class =	PCI_CLASS_SERIAL_USB_DEVICE,
3810 	.class_mask =	~0,
3811 	.vendor =	PCI_VENDOR_ID_PLX_LEGACY,
3812 	.device =	0x2282,
3813 	.subvendor =	PCI_ANY_ID,
3814 	.subdevice =	PCI_ANY_ID,
3815 	.driver_data =	PLX_LEGACY,
3816 	},
3817 	{
3818 	.class =	PCI_CLASS_SERIAL_USB_DEVICE,
3819 	.class_mask =	~0,
3820 	.vendor =	PCI_VENDOR_ID_PLX,
3821 	.device =	0x2380,
3822 	.subvendor =	PCI_ANY_ID,
3823 	.subdevice =	PCI_ANY_ID,
3824 	.driver_data =	PLX_PCIE,
3825 	 },
3826 	{
3827 	.class =	((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
3828 	.class_mask =	~0,
3829 	.vendor =	PCI_VENDOR_ID_PLX,
3830 	.device =	0x3380,
3831 	.subvendor =	PCI_ANY_ID,
3832 	.subdevice =	PCI_ANY_ID,
3833 	.driver_data =	PLX_PCIE | PLX_SUPERSPEED,
3834 	 },
3835 	{
3836 	.class =	PCI_CLASS_SERIAL_USB_DEVICE,
3837 	.class_mask =	~0,
3838 	.vendor =	PCI_VENDOR_ID_PLX,
3839 	.device =	0x3382,
3840 	.subvendor =	PCI_ANY_ID,
3841 	.subdevice =	PCI_ANY_ID,
3842 	.driver_data =	PLX_PCIE | PLX_SUPERSPEED,
3843 	 },
3844 { /* end: all zeroes */ }
3845 };
3846 MODULE_DEVICE_TABLE(pci, pci_ids);
3847 
3848 /* pci driver glue; this is a "new style" PCI driver module */
3849 static struct pci_driver net2280_pci_driver = {
3850 	.name =		(char *) driver_name,
3851 	.id_table =	pci_ids,
3852 
3853 	.probe =	net2280_probe,
3854 	.remove =	net2280_remove,
3855 	.shutdown =	net2280_shutdown,
3856 
3857 	/* FIXME add power management support */
3858 };
3859 
3860 module_pci_driver(net2280_pci_driver);
3861 
3862 MODULE_DESCRIPTION(DRIVER_DESC);
3863 MODULE_AUTHOR("David Brownell");
3864 MODULE_LICENSE("GPL");
3865