xref: /openbmc/linux/drivers/usb/gadget/udc/net2280.c (revision cbdf59ad)
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->name == ep0name) {
520 		pr_err("%s: Invalid ep=%p\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 }
794 
795 /* fill out dma descriptor to match a given request */
796 static void fill_dma_desc(struct net2280_ep *ep,
797 					struct net2280_request *req, int valid)
798 {
799 	struct net2280_dma	*td = req->td;
800 	u32			dmacount = req->req.length;
801 
802 	/* don't let DMA continue after a short OUT packet,
803 	 * so overruns can't affect the next transfer.
804 	 * in case of overruns on max-size packets, we can't
805 	 * stop the fifo from filling but we can flush it.
806 	 */
807 	if (ep->is_in)
808 		dmacount |= BIT(DMA_DIRECTION);
809 	if ((!ep->is_in && (dmacount % ep->ep.maxpacket) != 0) ||
810 					!(ep->dev->quirks & PLX_2280))
811 		dmacount |= BIT(END_OF_CHAIN);
812 
813 	req->valid = valid;
814 	if (valid)
815 		dmacount |= BIT(VALID_BIT);
816 	dmacount |= BIT(DMA_DONE_INTERRUPT_ENABLE);
817 
818 	/* td->dmadesc = previously set by caller */
819 	td->dmaaddr = cpu_to_le32 (req->req.dma);
820 
821 	/* 2280 may be polling VALID_BIT through ep->dma->dmadesc */
822 	wmb();
823 	td->dmacount = cpu_to_le32(dmacount);
824 }
825 
826 static const u32 dmactl_default =
827 		BIT(DMA_SCATTER_GATHER_DONE_INTERRUPT) |
828 		BIT(DMA_CLEAR_COUNT_ENABLE) |
829 		/* erratum 0116 workaround part 1 (use POLLING) */
830 		(POLL_100_USEC << DESCRIPTOR_POLLING_RATE) |
831 		BIT(DMA_VALID_BIT_POLLING_ENABLE) |
832 		BIT(DMA_VALID_BIT_ENABLE) |
833 		BIT(DMA_SCATTER_GATHER_ENABLE) |
834 		/* erratum 0116 workaround part 2 (no AUTOSTART) */
835 		BIT(DMA_ENABLE);
836 
837 static inline void spin_stop_dma(struct net2280_dma_regs __iomem *dma)
838 {
839 	handshake(&dma->dmactl, BIT(DMA_ENABLE), 0, 50);
840 }
841 
842 static inline void stop_dma(struct net2280_dma_regs __iomem *dma)
843 {
844 	writel(readl(&dma->dmactl) & ~BIT(DMA_ENABLE), &dma->dmactl);
845 	spin_stop_dma(dma);
846 }
847 
848 static void start_queue(struct net2280_ep *ep, u32 dmactl, u32 td_dma)
849 {
850 	struct net2280_dma_regs	__iomem *dma = ep->dma;
851 	unsigned int tmp = BIT(VALID_BIT) | (ep->is_in << DMA_DIRECTION);
852 
853 	if (!(ep->dev->quirks & PLX_2280))
854 		tmp |= BIT(END_OF_CHAIN);
855 
856 	writel(tmp, &dma->dmacount);
857 	writel(readl(&dma->dmastat), &dma->dmastat);
858 
859 	writel(td_dma, &dma->dmadesc);
860 	if (ep->dev->quirks & PLX_PCIE)
861 		dmactl |= BIT(DMA_REQUEST_OUTSTANDING);
862 	writel(dmactl, &dma->dmactl);
863 
864 	/* erratum 0116 workaround part 3:  pci arbiter away from net2280 */
865 	(void) readl(&ep->dev->pci->pcimstctl);
866 
867 	writel(BIT(DMA_START), &dma->dmastat);
868 }
869 
870 static void start_dma(struct net2280_ep *ep, struct net2280_request *req)
871 {
872 	u32			tmp;
873 	struct net2280_dma_regs	__iomem *dma = ep->dma;
874 
875 	/* FIXME can't use DMA for ZLPs */
876 
877 	/* on this path we "know" there's no dma active (yet) */
878 	WARN_ON(readl(&dma->dmactl) & BIT(DMA_ENABLE));
879 	writel(0, &ep->dma->dmactl);
880 
881 	/* previous OUT packet might have been short */
882 	if (!ep->is_in && (readl(&ep->regs->ep_stat) &
883 				BIT(NAK_OUT_PACKETS))) {
884 		writel(BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT),
885 			&ep->regs->ep_stat);
886 
887 		tmp = readl(&ep->regs->ep_avail);
888 		if (tmp) {
889 			writel(readl(&dma->dmastat), &dma->dmastat);
890 
891 			/* transfer all/some fifo data */
892 			writel(req->req.dma, &dma->dmaaddr);
893 			tmp = min(tmp, req->req.length);
894 
895 			/* dma irq, faking scatterlist status */
896 			req->td->dmacount = cpu_to_le32(req->req.length - tmp);
897 			writel(BIT(DMA_DONE_INTERRUPT_ENABLE) | tmp,
898 					&dma->dmacount);
899 			req->td->dmadesc = 0;
900 			req->valid = 1;
901 
902 			writel(BIT(DMA_ENABLE), &dma->dmactl);
903 			writel(BIT(DMA_START), &dma->dmastat);
904 			return;
905 		}
906 		stop_out_naking(ep);
907 	}
908 
909 	tmp = dmactl_default;
910 
911 	/* force packet boundaries between dma requests, but prevent the
912 	 * controller from automagically writing a last "short" packet
913 	 * (zero length) unless the driver explicitly said to do that.
914 	 */
915 	if (ep->is_in) {
916 		if (likely((req->req.length % ep->ep.maxpacket) ||
917 							req->req.zero)){
918 			tmp |= BIT(DMA_FIFO_VALIDATE);
919 			ep->in_fifo_validate = 1;
920 		} else
921 			ep->in_fifo_validate = 0;
922 	}
923 
924 	/* init req->td, pointing to the current dummy */
925 	req->td->dmadesc = cpu_to_le32 (ep->td_dma);
926 	fill_dma_desc(ep, req, 1);
927 
928 	req->td->dmacount |= cpu_to_le32(BIT(END_OF_CHAIN));
929 
930 	start_queue(ep, tmp, req->td_dma);
931 }
932 
933 static inline void
934 queue_dma(struct net2280_ep *ep, struct net2280_request *req, int valid)
935 {
936 	struct net2280_dma	*end;
937 	dma_addr_t		tmp;
938 
939 	/* swap new dummy for old, link; fill and maybe activate */
940 	end = ep->dummy;
941 	ep->dummy = req->td;
942 	req->td = end;
943 
944 	tmp = ep->td_dma;
945 	ep->td_dma = req->td_dma;
946 	req->td_dma = tmp;
947 
948 	end->dmadesc = cpu_to_le32 (ep->td_dma);
949 
950 	fill_dma_desc(ep, req, valid);
951 }
952 
953 static void
954 done(struct net2280_ep *ep, struct net2280_request *req, int status)
955 {
956 	struct net2280		*dev;
957 	unsigned		stopped = ep->stopped;
958 
959 	list_del_init(&req->queue);
960 
961 	if (req->req.status == -EINPROGRESS)
962 		req->req.status = status;
963 	else
964 		status = req->req.status;
965 
966 	dev = ep->dev;
967 	if (ep->dma)
968 		usb_gadget_unmap_request(&dev->gadget, &req->req, ep->is_in);
969 
970 	if (status && status != -ESHUTDOWN)
971 		ep_vdbg(dev, "complete %s req %p stat %d len %u/%u\n",
972 			ep->ep.name, &req->req, status,
973 			req->req.actual, req->req.length);
974 
975 	/* don't modify queue heads during completion callback */
976 	ep->stopped = 1;
977 	spin_unlock(&dev->lock);
978 	usb_gadget_giveback_request(&ep->ep, &req->req);
979 	spin_lock(&dev->lock);
980 	ep->stopped = stopped;
981 }
982 
983 /*-------------------------------------------------------------------------*/
984 
985 static int
986 net2280_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
987 {
988 	struct net2280_request	*req;
989 	struct net2280_ep	*ep;
990 	struct net2280		*dev;
991 	unsigned long		flags;
992 	int ret = 0;
993 
994 	/* we always require a cpu-view buffer, so that we can
995 	 * always use pio (as fallback or whatever).
996 	 */
997 	ep = container_of(_ep, struct net2280_ep, ep);
998 	if (!_ep || (!ep->desc && ep->num != 0)) {
999 		pr_err("%s: Invalid ep=%p or ep->desc\n", __func__, _ep);
1000 		return -EINVAL;
1001 	}
1002 	req = container_of(_req, struct net2280_request, req);
1003 	if (!_req || !_req->complete || !_req->buf ||
1004 				!list_empty(&req->queue)) {
1005 		ret = -EINVAL;
1006 		goto print_err;
1007 	}
1008 	if (_req->length > (~0 & DMA_BYTE_COUNT_MASK)) {
1009 		ret = -EDOM;
1010 		goto print_err;
1011 	}
1012 	dev = ep->dev;
1013 	if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
1014 		ret = -ESHUTDOWN;
1015 		goto print_err;
1016 	}
1017 
1018 	/* FIXME implement PIO fallback for ZLPs with DMA */
1019 	if (ep->dma && _req->length == 0) {
1020 		ret = -EOPNOTSUPP;
1021 		goto print_err;
1022 	}
1023 
1024 	/* set up dma mapping in case the caller didn't */
1025 	if (ep->dma) {
1026 		ret = usb_gadget_map_request(&dev->gadget, _req,
1027 				ep->is_in);
1028 		if (ret)
1029 			goto print_err;
1030 	}
1031 
1032 	ep_vdbg(dev, "%s queue req %p, len %d buf %p\n",
1033 			_ep->name, _req, _req->length, _req->buf);
1034 
1035 	spin_lock_irqsave(&dev->lock, flags);
1036 
1037 	_req->status = -EINPROGRESS;
1038 	_req->actual = 0;
1039 
1040 	/* kickstart this i/o queue? */
1041 	if  (list_empty(&ep->queue) && !ep->stopped &&
1042 		!((dev->quirks & PLX_PCIE) && ep->dma &&
1043 		  (readl(&ep->regs->ep_rsp) & BIT(CLEAR_ENDPOINT_HALT)))) {
1044 
1045 		/* use DMA if the endpoint supports it, else pio */
1046 		if (ep->dma)
1047 			start_dma(ep, req);
1048 		else {
1049 			/* maybe there's no control data, just status ack */
1050 			if (ep->num == 0 && _req->length == 0) {
1051 				allow_status(ep);
1052 				done(ep, req, 0);
1053 				ep_vdbg(dev, "%s status ack\n", ep->ep.name);
1054 				goto done;
1055 			}
1056 
1057 			/* PIO ... stuff the fifo, or unblock it.  */
1058 			if (ep->is_in)
1059 				write_fifo(ep, _req);
1060 			else {
1061 				u32	s;
1062 
1063 				/* OUT FIFO might have packet(s) buffered */
1064 				s = readl(&ep->regs->ep_stat);
1065 				if ((s & BIT(FIFO_EMPTY)) == 0) {
1066 					/* note:  _req->short_not_ok is
1067 					 * ignored here since PIO _always_
1068 					 * stops queue advance here, and
1069 					 * _req->status doesn't change for
1070 					 * short reads (only _req->actual)
1071 					 */
1072 					if (read_fifo(ep, req) &&
1073 							ep->num == 0) {
1074 						done(ep, req, 0);
1075 						allow_status(ep);
1076 						/* don't queue it */
1077 						req = NULL;
1078 					} else if (read_fifo(ep, req) &&
1079 							ep->num != 0) {
1080 						done(ep, req, 0);
1081 						req = NULL;
1082 					} else
1083 						s = readl(&ep->regs->ep_stat);
1084 				}
1085 
1086 				/* don't NAK, let the fifo fill */
1087 				if (req && (s & BIT(NAK_OUT_PACKETS)))
1088 					writel(BIT(CLEAR_NAK_OUT_PACKETS),
1089 							&ep->regs->ep_rsp);
1090 			}
1091 		}
1092 
1093 	} else if (ep->dma) {
1094 		int	valid = 1;
1095 
1096 		if (ep->is_in) {
1097 			int	expect;
1098 
1099 			/* preventing magic zlps is per-engine state, not
1100 			 * per-transfer; irq logic must recover hiccups.
1101 			 */
1102 			expect = likely(req->req.zero ||
1103 				(req->req.length % ep->ep.maxpacket));
1104 			if (expect != ep->in_fifo_validate)
1105 				valid = 0;
1106 		}
1107 		queue_dma(ep, req, valid);
1108 
1109 	} /* else the irq handler advances the queue. */
1110 
1111 	ep->responded = 1;
1112 	if (req)
1113 		list_add_tail(&req->queue, &ep->queue);
1114 done:
1115 	spin_unlock_irqrestore(&dev->lock, flags);
1116 
1117 	/* pci writes may still be posted */
1118 	return ret;
1119 
1120 print_err:
1121 	dev_err(&ep->dev->pdev->dev, "%s: error=%d\n", __func__, ret);
1122 	return ret;
1123 }
1124 
1125 static inline void
1126 dma_done(struct net2280_ep *ep,	struct net2280_request *req, u32 dmacount,
1127 		int status)
1128 {
1129 	req->req.actual = req->req.length - (DMA_BYTE_COUNT_MASK & dmacount);
1130 	done(ep, req, status);
1131 }
1132 
1133 static int scan_dma_completions(struct net2280_ep *ep)
1134 {
1135 	int num_completed = 0;
1136 
1137 	/* only look at descriptors that were "naturally" retired,
1138 	 * so fifo and list head state won't matter
1139 	 */
1140 	while (!list_empty(&ep->queue)) {
1141 		struct net2280_request	*req;
1142 		u32 req_dma_count;
1143 
1144 		req = list_entry(ep->queue.next,
1145 				struct net2280_request, queue);
1146 		if (!req->valid)
1147 			break;
1148 		rmb();
1149 		req_dma_count = le32_to_cpup(&req->td->dmacount);
1150 		if ((req_dma_count & BIT(VALID_BIT)) != 0)
1151 			break;
1152 
1153 		/* SHORT_PACKET_TRANSFERRED_INTERRUPT handles "usb-short"
1154 		 * cases where DMA must be aborted; this code handles
1155 		 * all non-abort DMA completions.
1156 		 */
1157 		if (unlikely(req->td->dmadesc == 0)) {
1158 			/* paranoia */
1159 			u32 const ep_dmacount = readl(&ep->dma->dmacount);
1160 
1161 			if (ep_dmacount & DMA_BYTE_COUNT_MASK)
1162 				break;
1163 			/* single transfer mode */
1164 			dma_done(ep, req, req_dma_count, 0);
1165 			num_completed++;
1166 			break;
1167 		} else if (!ep->is_in &&
1168 			   (req->req.length % ep->ep.maxpacket) &&
1169 			   !(ep->dev->quirks & PLX_PCIE)) {
1170 
1171 			u32 const ep_stat = readl(&ep->regs->ep_stat);
1172 			/* AVOID TROUBLE HERE by not issuing short reads from
1173 			 * your gadget driver.  That helps avoids errata 0121,
1174 			 * 0122, and 0124; not all cases trigger the warning.
1175 			 */
1176 			if ((ep_stat & BIT(NAK_OUT_PACKETS)) == 0) {
1177 				ep_warn(ep->dev, "%s lost packet sync!\n",
1178 						ep->ep.name);
1179 				req->req.status = -EOVERFLOW;
1180 			} else {
1181 				u32 const ep_avail = readl(&ep->regs->ep_avail);
1182 				if (ep_avail) {
1183 					/* fifo gets flushed later */
1184 					ep->out_overflow = 1;
1185 					ep_dbg(ep->dev,
1186 						"%s dma, discard %d len %d\n",
1187 						ep->ep.name, ep_avail,
1188 						req->req.length);
1189 					req->req.status = -EOVERFLOW;
1190 				}
1191 			}
1192 		}
1193 		dma_done(ep, req, req_dma_count, 0);
1194 		num_completed++;
1195 	}
1196 
1197 	return num_completed;
1198 }
1199 
1200 static void restart_dma(struct net2280_ep *ep)
1201 {
1202 	struct net2280_request	*req;
1203 
1204 	if (ep->stopped)
1205 		return;
1206 	req = list_entry(ep->queue.next, struct net2280_request, queue);
1207 
1208 	start_dma(ep, req);
1209 }
1210 
1211 static void abort_dma(struct net2280_ep *ep)
1212 {
1213 	/* abort the current transfer */
1214 	if (likely(!list_empty(&ep->queue))) {
1215 		/* FIXME work around errata 0121, 0122, 0124 */
1216 		writel(BIT(DMA_ABORT), &ep->dma->dmastat);
1217 		spin_stop_dma(ep->dma);
1218 	} else
1219 		stop_dma(ep->dma);
1220 	scan_dma_completions(ep);
1221 }
1222 
1223 /* dequeue ALL requests */
1224 static void nuke(struct net2280_ep *ep)
1225 {
1226 	struct net2280_request	*req;
1227 
1228 	/* called with spinlock held */
1229 	ep->stopped = 1;
1230 	if (ep->dma)
1231 		abort_dma(ep);
1232 	while (!list_empty(&ep->queue)) {
1233 		req = list_entry(ep->queue.next,
1234 				struct net2280_request,
1235 				queue);
1236 		done(ep, req, -ESHUTDOWN);
1237 	}
1238 }
1239 
1240 /* dequeue JUST ONE request */
1241 static int net2280_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1242 {
1243 	struct net2280_ep	*ep;
1244 	struct net2280_request	*req;
1245 	unsigned long		flags;
1246 	u32			dmactl;
1247 	int			stopped;
1248 
1249 	ep = container_of(_ep, struct net2280_ep, ep);
1250 	if (!_ep || (!ep->desc && ep->num != 0) || !_req) {
1251 		pr_err("%s: Invalid ep=%p or ep->desc or req=%p\n",
1252 						__func__, _ep, _req);
1253 		return -EINVAL;
1254 	}
1255 
1256 	spin_lock_irqsave(&ep->dev->lock, flags);
1257 	stopped = ep->stopped;
1258 
1259 	/* quiesce dma while we patch the queue */
1260 	dmactl = 0;
1261 	ep->stopped = 1;
1262 	if (ep->dma) {
1263 		dmactl = readl(&ep->dma->dmactl);
1264 		/* WARNING erratum 0127 may kick in ... */
1265 		stop_dma(ep->dma);
1266 		scan_dma_completions(ep);
1267 	}
1268 
1269 	/* make sure it's still queued on this endpoint */
1270 	list_for_each_entry(req, &ep->queue, queue) {
1271 		if (&req->req == _req)
1272 			break;
1273 	}
1274 	if (&req->req != _req) {
1275 		ep->stopped = stopped;
1276 		spin_unlock_irqrestore(&ep->dev->lock, flags);
1277 		ep_dbg(ep->dev, "%s: Request mismatch\n", __func__);
1278 		return -EINVAL;
1279 	}
1280 
1281 	/* queue head may be partially complete. */
1282 	if (ep->queue.next == &req->queue) {
1283 		if (ep->dma) {
1284 			ep_dbg(ep->dev, "unlink (%s) dma\n", _ep->name);
1285 			_req->status = -ECONNRESET;
1286 			abort_dma(ep);
1287 			if (likely(ep->queue.next == &req->queue)) {
1288 				/* NOTE: misreports single-transfer mode*/
1289 				req->td->dmacount = 0;	/* invalidate */
1290 				dma_done(ep, req,
1291 					readl(&ep->dma->dmacount),
1292 					-ECONNRESET);
1293 			}
1294 		} else {
1295 			ep_dbg(ep->dev, "unlink (%s) pio\n", _ep->name);
1296 			done(ep, req, -ECONNRESET);
1297 		}
1298 		req = NULL;
1299 	}
1300 
1301 	if (req)
1302 		done(ep, req, -ECONNRESET);
1303 	ep->stopped = stopped;
1304 
1305 	if (ep->dma) {
1306 		/* turn off dma on inactive queues */
1307 		if (list_empty(&ep->queue))
1308 			stop_dma(ep->dma);
1309 		else if (!ep->stopped) {
1310 			/* resume current request, or start new one */
1311 			if (req)
1312 				writel(dmactl, &ep->dma->dmactl);
1313 			else
1314 				start_dma(ep, list_entry(ep->queue.next,
1315 					struct net2280_request, queue));
1316 		}
1317 	}
1318 
1319 	spin_unlock_irqrestore(&ep->dev->lock, flags);
1320 	return 0;
1321 }
1322 
1323 /*-------------------------------------------------------------------------*/
1324 
1325 static int net2280_fifo_status(struct usb_ep *_ep);
1326 
1327 static int
1328 net2280_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged)
1329 {
1330 	struct net2280_ep	*ep;
1331 	unsigned long		flags;
1332 	int			retval = 0;
1333 
1334 	ep = container_of(_ep, struct net2280_ep, ep);
1335 	if (!_ep || (!ep->desc && ep->num != 0)) {
1336 		pr_err("%s: Invalid ep=%p or ep->desc\n", __func__, _ep);
1337 		return -EINVAL;
1338 	}
1339 	if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN) {
1340 		retval = -ESHUTDOWN;
1341 		goto print_err;
1342 	}
1343 	if (ep->desc /* not ep0 */ && (ep->desc->bmAttributes & 0x03)
1344 						== USB_ENDPOINT_XFER_ISOC) {
1345 		retval = -EINVAL;
1346 		goto print_err;
1347 	}
1348 
1349 	spin_lock_irqsave(&ep->dev->lock, flags);
1350 	if (!list_empty(&ep->queue)) {
1351 		retval = -EAGAIN;
1352 		goto print_unlock;
1353 	} else if (ep->is_in && value && net2280_fifo_status(_ep) != 0) {
1354 		retval = -EAGAIN;
1355 		goto print_unlock;
1356 	} else {
1357 		ep_vdbg(ep->dev, "%s %s %s\n", _ep->name,
1358 				value ? "set" : "clear",
1359 				wedged ? "wedge" : "halt");
1360 		/* set/clear, then synch memory views with the device */
1361 		if (value) {
1362 			if (ep->num == 0)
1363 				ep->dev->protocol_stall = 1;
1364 			else
1365 				set_halt(ep);
1366 			if (wedged)
1367 				ep->wedged = 1;
1368 		} else {
1369 			clear_halt(ep);
1370 			if (ep->dev->quirks & PLX_PCIE &&
1371 				!list_empty(&ep->queue) && ep->td_dma)
1372 					restart_dma(ep);
1373 			ep->wedged = 0;
1374 		}
1375 		(void) readl(&ep->regs->ep_rsp);
1376 	}
1377 	spin_unlock_irqrestore(&ep->dev->lock, flags);
1378 
1379 	return retval;
1380 
1381 print_unlock:
1382 	spin_unlock_irqrestore(&ep->dev->lock, flags);
1383 print_err:
1384 	dev_err(&ep->dev->pdev->dev, "%s: error=%d\n", __func__, retval);
1385 	return retval;
1386 }
1387 
1388 static int net2280_set_halt(struct usb_ep *_ep, int value)
1389 {
1390 	return net2280_set_halt_and_wedge(_ep, value, 0);
1391 }
1392 
1393 static int net2280_set_wedge(struct usb_ep *_ep)
1394 {
1395 	if (!_ep || _ep->name == ep0name) {
1396 		pr_err("%s: Invalid ep=%p or ep0\n", __func__, _ep);
1397 		return -EINVAL;
1398 	}
1399 	return net2280_set_halt_and_wedge(_ep, 1, 1);
1400 }
1401 
1402 static int net2280_fifo_status(struct usb_ep *_ep)
1403 {
1404 	struct net2280_ep	*ep;
1405 	u32			avail;
1406 
1407 	ep = container_of(_ep, struct net2280_ep, ep);
1408 	if (!_ep || (!ep->desc && ep->num != 0)) {
1409 		pr_err("%s: Invalid ep=%p or ep->desc\n", __func__, _ep);
1410 		return -ENODEV;
1411 	}
1412 	if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN) {
1413 		dev_err(&ep->dev->pdev->dev,
1414 			"%s: Invalid driver=%p or speed=%d\n",
1415 			__func__, ep->dev->driver, ep->dev->gadget.speed);
1416 		return -ESHUTDOWN;
1417 	}
1418 
1419 	avail = readl(&ep->regs->ep_avail) & (BIT(12) - 1);
1420 	if (avail > ep->fifo_size) {
1421 		dev_err(&ep->dev->pdev->dev, "%s: Fifo overflow\n", __func__);
1422 		return -EOVERFLOW;
1423 	}
1424 	if (ep->is_in)
1425 		avail = ep->fifo_size - avail;
1426 	return avail;
1427 }
1428 
1429 static void net2280_fifo_flush(struct usb_ep *_ep)
1430 {
1431 	struct net2280_ep	*ep;
1432 
1433 	ep = container_of(_ep, struct net2280_ep, ep);
1434 	if (!_ep || (!ep->desc && ep->num != 0)) {
1435 		pr_err("%s: Invalid ep=%p or ep->desc\n", __func__, _ep);
1436 		return;
1437 	}
1438 	if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN) {
1439 		dev_err(&ep->dev->pdev->dev,
1440 			"%s: Invalid driver=%p or speed=%d\n",
1441 			__func__, ep->dev->driver, ep->dev->gadget.speed);
1442 		return;
1443 	}
1444 
1445 	writel(BIT(FIFO_FLUSH), &ep->regs->ep_stat);
1446 	(void) readl(&ep->regs->ep_rsp);
1447 }
1448 
1449 static const struct usb_ep_ops net2280_ep_ops = {
1450 	.enable		= net2280_enable,
1451 	.disable	= net2280_disable,
1452 
1453 	.alloc_request	= net2280_alloc_request,
1454 	.free_request	= net2280_free_request,
1455 
1456 	.queue		= net2280_queue,
1457 	.dequeue	= net2280_dequeue,
1458 
1459 	.set_halt	= net2280_set_halt,
1460 	.set_wedge	= net2280_set_wedge,
1461 	.fifo_status	= net2280_fifo_status,
1462 	.fifo_flush	= net2280_fifo_flush,
1463 };
1464 
1465 /*-------------------------------------------------------------------------*/
1466 
1467 static int net2280_get_frame(struct usb_gadget *_gadget)
1468 {
1469 	struct net2280		*dev;
1470 	unsigned long		flags;
1471 	u16			retval;
1472 
1473 	if (!_gadget)
1474 		return -ENODEV;
1475 	dev = container_of(_gadget, struct net2280, gadget);
1476 	spin_lock_irqsave(&dev->lock, flags);
1477 	retval = get_idx_reg(dev->regs, REG_FRAME) & 0x03ff;
1478 	spin_unlock_irqrestore(&dev->lock, flags);
1479 	return retval;
1480 }
1481 
1482 static int net2280_wakeup(struct usb_gadget *_gadget)
1483 {
1484 	struct net2280		*dev;
1485 	u32			tmp;
1486 	unsigned long		flags;
1487 
1488 	if (!_gadget)
1489 		return 0;
1490 	dev = container_of(_gadget, struct net2280, gadget);
1491 
1492 	spin_lock_irqsave(&dev->lock, flags);
1493 	tmp = readl(&dev->usb->usbctl);
1494 	if (tmp & BIT(DEVICE_REMOTE_WAKEUP_ENABLE))
1495 		writel(BIT(GENERATE_RESUME), &dev->usb->usbstat);
1496 	spin_unlock_irqrestore(&dev->lock, flags);
1497 
1498 	/* pci writes may still be posted */
1499 	return 0;
1500 }
1501 
1502 static int net2280_set_selfpowered(struct usb_gadget *_gadget, int value)
1503 {
1504 	struct net2280		*dev;
1505 	u32			tmp;
1506 	unsigned long		flags;
1507 
1508 	if (!_gadget)
1509 		return 0;
1510 	dev = container_of(_gadget, struct net2280, gadget);
1511 
1512 	spin_lock_irqsave(&dev->lock, flags);
1513 	tmp = readl(&dev->usb->usbctl);
1514 	if (value) {
1515 		tmp |= BIT(SELF_POWERED_STATUS);
1516 		_gadget->is_selfpowered = 1;
1517 	} else {
1518 		tmp &= ~BIT(SELF_POWERED_STATUS);
1519 		_gadget->is_selfpowered = 0;
1520 	}
1521 	writel(tmp, &dev->usb->usbctl);
1522 	spin_unlock_irqrestore(&dev->lock, flags);
1523 
1524 	return 0;
1525 }
1526 
1527 static int net2280_pullup(struct usb_gadget *_gadget, int is_on)
1528 {
1529 	struct net2280  *dev;
1530 	u32             tmp;
1531 	unsigned long   flags;
1532 
1533 	if (!_gadget)
1534 		return -ENODEV;
1535 	dev = container_of(_gadget, struct net2280, gadget);
1536 
1537 	spin_lock_irqsave(&dev->lock, flags);
1538 	tmp = readl(&dev->usb->usbctl);
1539 	dev->softconnect = (is_on != 0);
1540 	if (is_on) {
1541 		ep0_start(dev);
1542 		writel(tmp | BIT(USB_DETECT_ENABLE), &dev->usb->usbctl);
1543 	} else {
1544 		writel(tmp & ~BIT(USB_DETECT_ENABLE), &dev->usb->usbctl);
1545 		stop_activity(dev, NULL);
1546 	}
1547 
1548 	spin_unlock_irqrestore(&dev->lock, flags);
1549 
1550 	return 0;
1551 }
1552 
1553 static struct usb_ep *net2280_match_ep(struct usb_gadget *_gadget,
1554 		struct usb_endpoint_descriptor *desc,
1555 		struct usb_ss_ep_comp_descriptor *ep_comp)
1556 {
1557 	char name[8];
1558 	struct usb_ep *ep;
1559 
1560 	if (usb_endpoint_type(desc) == USB_ENDPOINT_XFER_INT) {
1561 		/* ep-e, ep-f are PIO with only 64 byte fifos */
1562 		ep = gadget_find_ep_by_name(_gadget, "ep-e");
1563 		if (ep && usb_gadget_ep_match_desc(_gadget, ep, desc, ep_comp))
1564 			return ep;
1565 		ep = gadget_find_ep_by_name(_gadget, "ep-f");
1566 		if (ep && usb_gadget_ep_match_desc(_gadget, ep, desc, ep_comp))
1567 			return ep;
1568 	}
1569 
1570 	/* USB3380: Only first four endpoints have DMA channels. Allocate
1571 	 * slower interrupt endpoints from PIO hw endpoints, to allow bulk/isoc
1572 	 * endpoints use DMA hw endpoints.
1573 	 */
1574 	if (usb_endpoint_type(desc) == USB_ENDPOINT_XFER_INT &&
1575 	    usb_endpoint_dir_in(desc)) {
1576 		ep = gadget_find_ep_by_name(_gadget, "ep2in");
1577 		if (ep && usb_gadget_ep_match_desc(_gadget, ep, desc, ep_comp))
1578 			return ep;
1579 		ep = gadget_find_ep_by_name(_gadget, "ep4in");
1580 		if (ep && usb_gadget_ep_match_desc(_gadget, ep, desc, ep_comp))
1581 			return ep;
1582 	} else if (usb_endpoint_type(desc) == USB_ENDPOINT_XFER_INT &&
1583 		   !usb_endpoint_dir_in(desc)) {
1584 		ep = gadget_find_ep_by_name(_gadget, "ep1out");
1585 		if (ep && usb_gadget_ep_match_desc(_gadget, ep, desc, ep_comp))
1586 			return ep;
1587 		ep = gadget_find_ep_by_name(_gadget, "ep3out");
1588 		if (ep && usb_gadget_ep_match_desc(_gadget, ep, desc, ep_comp))
1589 			return ep;
1590 	} else if (usb_endpoint_type(desc) != USB_ENDPOINT_XFER_BULK &&
1591 		   usb_endpoint_dir_in(desc)) {
1592 		ep = gadget_find_ep_by_name(_gadget, "ep1in");
1593 		if (ep && usb_gadget_ep_match_desc(_gadget, ep, desc, ep_comp))
1594 			return ep;
1595 		ep = gadget_find_ep_by_name(_gadget, "ep3in");
1596 		if (ep && usb_gadget_ep_match_desc(_gadget, ep, desc, ep_comp))
1597 			return ep;
1598 	} else if (usb_endpoint_type(desc) != USB_ENDPOINT_XFER_BULK &&
1599 		   !usb_endpoint_dir_in(desc)) {
1600 		ep = gadget_find_ep_by_name(_gadget, "ep2out");
1601 		if (ep && usb_gadget_ep_match_desc(_gadget, ep, desc, ep_comp))
1602 			return ep;
1603 		ep = gadget_find_ep_by_name(_gadget, "ep4out");
1604 		if (ep && usb_gadget_ep_match_desc(_gadget, ep, desc, ep_comp))
1605 			return ep;
1606 	}
1607 
1608 	/* USB3380: use same address for usb and hardware endpoints */
1609 	snprintf(name, sizeof(name), "ep%d%s", usb_endpoint_num(desc),
1610 			usb_endpoint_dir_in(desc) ? "in" : "out");
1611 	ep = gadget_find_ep_by_name(_gadget, name);
1612 	if (ep && usb_gadget_ep_match_desc(_gadget, ep, desc, ep_comp))
1613 		return ep;
1614 
1615 	return NULL;
1616 }
1617 
1618 static int net2280_start(struct usb_gadget *_gadget,
1619 		struct usb_gadget_driver *driver);
1620 static int net2280_stop(struct usb_gadget *_gadget);
1621 
1622 static const struct usb_gadget_ops net2280_ops = {
1623 	.get_frame	= net2280_get_frame,
1624 	.wakeup		= net2280_wakeup,
1625 	.set_selfpowered = net2280_set_selfpowered,
1626 	.pullup		= net2280_pullup,
1627 	.udc_start	= net2280_start,
1628 	.udc_stop	= net2280_stop,
1629 	.match_ep	= net2280_match_ep,
1630 };
1631 
1632 /*-------------------------------------------------------------------------*/
1633 
1634 #ifdef	CONFIG_USB_GADGET_DEBUG_FILES
1635 
1636 /* FIXME move these into procfs, and use seq_file.
1637  * Sysfs _still_ doesn't behave for arbitrarily sized files,
1638  * and also doesn't help products using this with 2.4 kernels.
1639  */
1640 
1641 /* "function" sysfs attribute */
1642 static ssize_t function_show(struct device *_dev, struct device_attribute *attr,
1643 			     char *buf)
1644 {
1645 	struct net2280	*dev = dev_get_drvdata(_dev);
1646 
1647 	if (!dev->driver || !dev->driver->function ||
1648 			strlen(dev->driver->function) > PAGE_SIZE)
1649 		return 0;
1650 	return scnprintf(buf, PAGE_SIZE, "%s\n", dev->driver->function);
1651 }
1652 static DEVICE_ATTR_RO(function);
1653 
1654 static ssize_t registers_show(struct device *_dev,
1655 			      struct device_attribute *attr, char *buf)
1656 {
1657 	struct net2280		*dev;
1658 	char			*next;
1659 	unsigned		size, t;
1660 	unsigned long		flags;
1661 	int			i;
1662 	u32			t1, t2;
1663 	const char		*s;
1664 
1665 	dev = dev_get_drvdata(_dev);
1666 	next = buf;
1667 	size = PAGE_SIZE;
1668 	spin_lock_irqsave(&dev->lock, flags);
1669 
1670 	if (dev->driver)
1671 		s = dev->driver->driver.name;
1672 	else
1673 		s = "(none)";
1674 
1675 	/* Main Control Registers */
1676 	t = scnprintf(next, size, "%s version " DRIVER_VERSION
1677 			", chiprev %04x\n\n"
1678 			"devinit %03x fifoctl %08x gadget '%s'\n"
1679 			"pci irqenb0 %02x irqenb1 %08x "
1680 			"irqstat0 %04x irqstat1 %08x\n",
1681 			driver_name, dev->chiprev,
1682 			readl(&dev->regs->devinit),
1683 			readl(&dev->regs->fifoctl),
1684 			s,
1685 			readl(&dev->regs->pciirqenb0),
1686 			readl(&dev->regs->pciirqenb1),
1687 			readl(&dev->regs->irqstat0),
1688 			readl(&dev->regs->irqstat1));
1689 	size -= t;
1690 	next += t;
1691 
1692 	/* USB Control Registers */
1693 	t1 = readl(&dev->usb->usbctl);
1694 	t2 = readl(&dev->usb->usbstat);
1695 	if (t1 & BIT(VBUS_PIN)) {
1696 		if (t2 & BIT(HIGH_SPEED))
1697 			s = "high speed";
1698 		else if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1699 			s = "powered";
1700 		else
1701 			s = "full speed";
1702 		/* full speed bit (6) not working?? */
1703 	} else
1704 			s = "not attached";
1705 	t = scnprintf(next, size,
1706 			"stdrsp %08x usbctl %08x usbstat %08x "
1707 				"addr 0x%02x (%s)\n",
1708 			readl(&dev->usb->stdrsp), t1, t2,
1709 			readl(&dev->usb->ouraddr), s);
1710 	size -= t;
1711 	next += t;
1712 
1713 	/* PCI Master Control Registers */
1714 
1715 	/* DMA Control Registers */
1716 
1717 	/* Configurable EP Control Registers */
1718 	for (i = 0; i < dev->n_ep; i++) {
1719 		struct net2280_ep	*ep;
1720 
1721 		ep = &dev->ep[i];
1722 		if (i && !ep->desc)
1723 			continue;
1724 
1725 		t1 = readl(&ep->cfg->ep_cfg);
1726 		t2 = readl(&ep->regs->ep_rsp) & 0xff;
1727 		t = scnprintf(next, size,
1728 				"\n%s\tcfg %05x rsp (%02x) %s%s%s%s%s%s%s%s"
1729 					"irqenb %02x\n",
1730 				ep->ep.name, t1, t2,
1731 				(t2 & BIT(CLEAR_NAK_OUT_PACKETS))
1732 					? "NAK " : "",
1733 				(t2 & BIT(CLEAR_EP_HIDE_STATUS_PHASE))
1734 					? "hide " : "",
1735 				(t2 & BIT(CLEAR_EP_FORCE_CRC_ERROR))
1736 					? "CRC " : "",
1737 				(t2 & BIT(CLEAR_INTERRUPT_MODE))
1738 					? "interrupt " : "",
1739 				(t2 & BIT(CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE))
1740 					? "status " : "",
1741 				(t2 & BIT(CLEAR_NAK_OUT_PACKETS_MODE))
1742 					? "NAKmode " : "",
1743 				(t2 & BIT(CLEAR_ENDPOINT_TOGGLE))
1744 					? "DATA1 " : "DATA0 ",
1745 				(t2 & BIT(CLEAR_ENDPOINT_HALT))
1746 					? "HALT " : "",
1747 				readl(&ep->regs->ep_irqenb));
1748 		size -= t;
1749 		next += t;
1750 
1751 		t = scnprintf(next, size,
1752 				"\tstat %08x avail %04x "
1753 				"(ep%d%s-%s)%s\n",
1754 				readl(&ep->regs->ep_stat),
1755 				readl(&ep->regs->ep_avail),
1756 				t1 & 0x0f, DIR_STRING(t1),
1757 				type_string(t1 >> 8),
1758 				ep->stopped ? "*" : "");
1759 		size -= t;
1760 		next += t;
1761 
1762 		if (!ep->dma)
1763 			continue;
1764 
1765 		t = scnprintf(next, size,
1766 				"  dma\tctl %08x stat %08x count %08x\n"
1767 				"\taddr %08x desc %08x\n",
1768 				readl(&ep->dma->dmactl),
1769 				readl(&ep->dma->dmastat),
1770 				readl(&ep->dma->dmacount),
1771 				readl(&ep->dma->dmaaddr),
1772 				readl(&ep->dma->dmadesc));
1773 		size -= t;
1774 		next += t;
1775 
1776 	}
1777 
1778 	/* Indexed Registers (none yet) */
1779 
1780 	/* Statistics */
1781 	t = scnprintf(next, size, "\nirqs:  ");
1782 	size -= t;
1783 	next += t;
1784 	for (i = 0; i < dev->n_ep; i++) {
1785 		struct net2280_ep	*ep;
1786 
1787 		ep = &dev->ep[i];
1788 		if (i && !ep->irqs)
1789 			continue;
1790 		t = scnprintf(next, size, " %s/%lu", ep->ep.name, ep->irqs);
1791 		size -= t;
1792 		next += t;
1793 
1794 	}
1795 	t = scnprintf(next, size, "\n");
1796 	size -= t;
1797 	next += t;
1798 
1799 	spin_unlock_irqrestore(&dev->lock, flags);
1800 
1801 	return PAGE_SIZE - size;
1802 }
1803 static DEVICE_ATTR_RO(registers);
1804 
1805 static ssize_t queues_show(struct device *_dev, struct device_attribute *attr,
1806 			   char *buf)
1807 {
1808 	struct net2280		*dev;
1809 	char			*next;
1810 	unsigned		size;
1811 	unsigned long		flags;
1812 	int			i;
1813 
1814 	dev = dev_get_drvdata(_dev);
1815 	next = buf;
1816 	size = PAGE_SIZE;
1817 	spin_lock_irqsave(&dev->lock, flags);
1818 
1819 	for (i = 0; i < dev->n_ep; i++) {
1820 		struct net2280_ep		*ep = &dev->ep[i];
1821 		struct net2280_request		*req;
1822 		int				t;
1823 
1824 		if (i != 0) {
1825 			const struct usb_endpoint_descriptor	*d;
1826 
1827 			d = ep->desc;
1828 			if (!d)
1829 				continue;
1830 			t = d->bEndpointAddress;
1831 			t = scnprintf(next, size,
1832 				"\n%s (ep%d%s-%s) max %04x %s fifo %d\n",
1833 				ep->ep.name, t & USB_ENDPOINT_NUMBER_MASK,
1834 				(t & USB_DIR_IN) ? "in" : "out",
1835 				type_string(d->bmAttributes),
1836 				usb_endpoint_maxp(d),
1837 				ep->dma ? "dma" : "pio", ep->fifo_size
1838 				);
1839 		} else /* ep0 should only have one transfer queued */
1840 			t = scnprintf(next, size, "ep0 max 64 pio %s\n",
1841 					ep->is_in ? "in" : "out");
1842 		if (t <= 0 || t > size)
1843 			goto done;
1844 		size -= t;
1845 		next += t;
1846 
1847 		if (list_empty(&ep->queue)) {
1848 			t = scnprintf(next, size, "\t(nothing queued)\n");
1849 			if (t <= 0 || t > size)
1850 				goto done;
1851 			size -= t;
1852 			next += t;
1853 			continue;
1854 		}
1855 		list_for_each_entry(req, &ep->queue, queue) {
1856 			if (ep->dma && req->td_dma == readl(&ep->dma->dmadesc))
1857 				t = scnprintf(next, size,
1858 					"\treq %p len %d/%d "
1859 					"buf %p (dmacount %08x)\n",
1860 					&req->req, req->req.actual,
1861 					req->req.length, req->req.buf,
1862 					readl(&ep->dma->dmacount));
1863 			else
1864 				t = scnprintf(next, size,
1865 					"\treq %p len %d/%d buf %p\n",
1866 					&req->req, req->req.actual,
1867 					req->req.length, req->req.buf);
1868 			if (t <= 0 || t > size)
1869 				goto done;
1870 			size -= t;
1871 			next += t;
1872 
1873 			if (ep->dma) {
1874 				struct net2280_dma	*td;
1875 
1876 				td = req->td;
1877 				t = scnprintf(next, size, "\t    td %08x "
1878 					" count %08x buf %08x desc %08x\n",
1879 					(u32) req->td_dma,
1880 					le32_to_cpu(td->dmacount),
1881 					le32_to_cpu(td->dmaaddr),
1882 					le32_to_cpu(td->dmadesc));
1883 				if (t <= 0 || t > size)
1884 					goto done;
1885 				size -= t;
1886 				next += t;
1887 			}
1888 		}
1889 	}
1890 
1891 done:
1892 	spin_unlock_irqrestore(&dev->lock, flags);
1893 	return PAGE_SIZE - size;
1894 }
1895 static DEVICE_ATTR_RO(queues);
1896 
1897 
1898 #else
1899 
1900 #define device_create_file(a, b)	(0)
1901 #define device_remove_file(a, b)	do { } while (0)
1902 
1903 #endif
1904 
1905 /*-------------------------------------------------------------------------*/
1906 
1907 /* another driver-specific mode might be a request type doing dma
1908  * to/from another device fifo instead of to/from memory.
1909  */
1910 
1911 static void set_fifo_mode(struct net2280 *dev, int mode)
1912 {
1913 	/* keeping high bits preserves BAR2 */
1914 	writel((0xffff << PCI_BASE2_RANGE) | mode, &dev->regs->fifoctl);
1915 
1916 	/* always ep-{a,b,e,f} ... maybe not ep-c or ep-d */
1917 	INIT_LIST_HEAD(&dev->gadget.ep_list);
1918 	list_add_tail(&dev->ep[1].ep.ep_list, &dev->gadget.ep_list);
1919 	list_add_tail(&dev->ep[2].ep.ep_list, &dev->gadget.ep_list);
1920 	switch (mode) {
1921 	case 0:
1922 		list_add_tail(&dev->ep[3].ep.ep_list, &dev->gadget.ep_list);
1923 		list_add_tail(&dev->ep[4].ep.ep_list, &dev->gadget.ep_list);
1924 		dev->ep[1].fifo_size = dev->ep[2].fifo_size = 1024;
1925 		break;
1926 	case 1:
1927 		dev->ep[1].fifo_size = dev->ep[2].fifo_size = 2048;
1928 		break;
1929 	case 2:
1930 		list_add_tail(&dev->ep[3].ep.ep_list, &dev->gadget.ep_list);
1931 		dev->ep[1].fifo_size = 2048;
1932 		dev->ep[2].fifo_size = 1024;
1933 		break;
1934 	}
1935 	/* fifo sizes for ep0, ep-c, ep-d, ep-e, and ep-f never change */
1936 	list_add_tail(&dev->ep[5].ep.ep_list, &dev->gadget.ep_list);
1937 	list_add_tail(&dev->ep[6].ep.ep_list, &dev->gadget.ep_list);
1938 }
1939 
1940 static void defect7374_disable_data_eps(struct net2280 *dev)
1941 {
1942 	/*
1943 	 * For Defect 7374, disable data EPs (and more):
1944 	 *  - This phase undoes the earlier phase of the Defect 7374 workaround,
1945 	 *    returing ep regs back to normal.
1946 	 */
1947 	struct net2280_ep *ep;
1948 	int i;
1949 	unsigned char ep_sel;
1950 	u32 tmp_reg;
1951 
1952 	for (i = 1; i < 5; i++) {
1953 		ep = &dev->ep[i];
1954 		writel(i, &ep->cfg->ep_cfg);
1955 	}
1956 
1957 	/* CSROUT, CSRIN, PCIOUT, PCIIN, STATIN, RCIN */
1958 	for (i = 0; i < 6; i++)
1959 		writel(0, &dev->dep[i].dep_cfg);
1960 
1961 	for (ep_sel = 0; ep_sel <= 21; ep_sel++) {
1962 		/* Select an endpoint for subsequent operations: */
1963 		tmp_reg = readl(&dev->plregs->pl_ep_ctrl);
1964 		writel(((tmp_reg & ~0x1f) | ep_sel), &dev->plregs->pl_ep_ctrl);
1965 
1966 		if (ep_sel < 2 || (ep_sel > 9 && ep_sel < 14) ||
1967 					ep_sel == 18 || ep_sel == 20)
1968 			continue;
1969 
1970 		/* Change settings on some selected endpoints */
1971 		tmp_reg = readl(&dev->plregs->pl_ep_cfg_4);
1972 		tmp_reg &= ~BIT(NON_CTRL_IN_TOLERATE_BAD_DIR);
1973 		writel(tmp_reg, &dev->plregs->pl_ep_cfg_4);
1974 		tmp_reg = readl(&dev->plregs->pl_ep_ctrl);
1975 		tmp_reg |= BIT(EP_INITIALIZED);
1976 		writel(tmp_reg, &dev->plregs->pl_ep_ctrl);
1977 	}
1978 }
1979 
1980 static void defect7374_enable_data_eps_zero(struct net2280 *dev)
1981 {
1982 	u32 tmp = 0, tmp_reg;
1983 	u32 scratch;
1984 	int i;
1985 	unsigned char ep_sel;
1986 
1987 	scratch = get_idx_reg(dev->regs, SCRATCH);
1988 
1989 	WARN_ON((scratch & (0xf << DEFECT7374_FSM_FIELD))
1990 		== DEFECT7374_FSM_SS_CONTROL_READ);
1991 
1992 	scratch &= ~(0xf << DEFECT7374_FSM_FIELD);
1993 
1994 	ep_warn(dev, "Operate Defect 7374 workaround soft this time");
1995 	ep_warn(dev, "It will operate on cold-reboot and SS connect");
1996 
1997 	/*GPEPs:*/
1998 	tmp = ((0 << ENDPOINT_NUMBER) | BIT(ENDPOINT_DIRECTION) |
1999 			(2 << OUT_ENDPOINT_TYPE) | (2 << IN_ENDPOINT_TYPE) |
2000 			((dev->enhanced_mode) ?
2001 			 BIT(OUT_ENDPOINT_ENABLE) | BIT(IN_ENDPOINT_ENABLE) :
2002 			 BIT(ENDPOINT_ENABLE)));
2003 
2004 	for (i = 1; i < 5; i++)
2005 		writel(tmp, &dev->ep[i].cfg->ep_cfg);
2006 
2007 	/* CSRIN, PCIIN, STATIN, RCIN*/
2008 	tmp = ((0 << ENDPOINT_NUMBER) | BIT(ENDPOINT_ENABLE));
2009 	writel(tmp, &dev->dep[1].dep_cfg);
2010 	writel(tmp, &dev->dep[3].dep_cfg);
2011 	writel(tmp, &dev->dep[4].dep_cfg);
2012 	writel(tmp, &dev->dep[5].dep_cfg);
2013 
2014 	/*Implemented for development and debug.
2015 	 * Can be refined/tuned later.*/
2016 	for (ep_sel = 0; ep_sel <= 21; ep_sel++) {
2017 		/* Select an endpoint for subsequent operations: */
2018 		tmp_reg = readl(&dev->plregs->pl_ep_ctrl);
2019 		writel(((tmp_reg & ~0x1f) | ep_sel),
2020 				&dev->plregs->pl_ep_ctrl);
2021 
2022 		if (ep_sel == 1) {
2023 			tmp =
2024 				(readl(&dev->plregs->pl_ep_ctrl) |
2025 				 BIT(CLEAR_ACK_ERROR_CODE) | 0);
2026 			writel(tmp, &dev->plregs->pl_ep_ctrl);
2027 			continue;
2028 		}
2029 
2030 		if (ep_sel == 0 || (ep_sel > 9 && ep_sel < 14) ||
2031 				ep_sel == 18  || ep_sel == 20)
2032 			continue;
2033 
2034 		tmp = (readl(&dev->plregs->pl_ep_cfg_4) |
2035 				BIT(NON_CTRL_IN_TOLERATE_BAD_DIR) | 0);
2036 		writel(tmp, &dev->plregs->pl_ep_cfg_4);
2037 
2038 		tmp = readl(&dev->plregs->pl_ep_ctrl) &
2039 			~BIT(EP_INITIALIZED);
2040 		writel(tmp, &dev->plregs->pl_ep_ctrl);
2041 
2042 	}
2043 
2044 	/* Set FSM to focus on the first Control Read:
2045 	 * - Tip: Connection speed is known upon the first
2046 	 * setup request.*/
2047 	scratch |= DEFECT7374_FSM_WAITING_FOR_CONTROL_READ;
2048 	set_idx_reg(dev->regs, SCRATCH, scratch);
2049 
2050 }
2051 
2052 /* keeping it simple:
2053  * - one bus driver, initted first;
2054  * - one function driver, initted second
2055  *
2056  * most of the work to support multiple net2280 controllers would
2057  * be to associate this gadget driver (yes?) with all of them, or
2058  * perhaps to bind specific drivers to specific devices.
2059  */
2060 
2061 static void usb_reset_228x(struct net2280 *dev)
2062 {
2063 	u32	tmp;
2064 
2065 	dev->gadget.speed = USB_SPEED_UNKNOWN;
2066 	(void) readl(&dev->usb->usbctl);
2067 
2068 	net2280_led_init(dev);
2069 
2070 	/* disable automatic responses, and irqs */
2071 	writel(0, &dev->usb->stdrsp);
2072 	writel(0, &dev->regs->pciirqenb0);
2073 	writel(0, &dev->regs->pciirqenb1);
2074 
2075 	/* clear old dma and irq state */
2076 	for (tmp = 0; tmp < 4; tmp++) {
2077 		struct net2280_ep       *ep = &dev->ep[tmp + 1];
2078 		if (ep->dma)
2079 			abort_dma(ep);
2080 	}
2081 
2082 	writel(~0, &dev->regs->irqstat0),
2083 	writel(~(u32)BIT(SUSPEND_REQUEST_INTERRUPT), &dev->regs->irqstat1),
2084 
2085 	/* reset, and enable pci */
2086 	tmp = readl(&dev->regs->devinit) |
2087 		BIT(PCI_ENABLE) |
2088 		BIT(FIFO_SOFT_RESET) |
2089 		BIT(USB_SOFT_RESET) |
2090 		BIT(M8051_RESET);
2091 	writel(tmp, &dev->regs->devinit);
2092 
2093 	/* standard fifo and endpoint allocations */
2094 	set_fifo_mode(dev, (fifo_mode <= 2) ? fifo_mode : 0);
2095 }
2096 
2097 static void usb_reset_338x(struct net2280 *dev)
2098 {
2099 	u32 tmp;
2100 
2101 	dev->gadget.speed = USB_SPEED_UNKNOWN;
2102 	(void)readl(&dev->usb->usbctl);
2103 
2104 	net2280_led_init(dev);
2105 
2106 	if (dev->bug7734_patched) {
2107 		/* disable automatic responses, and irqs */
2108 		writel(0, &dev->usb->stdrsp);
2109 		writel(0, &dev->regs->pciirqenb0);
2110 		writel(0, &dev->regs->pciirqenb1);
2111 	}
2112 
2113 	/* clear old dma and irq state */
2114 	for (tmp = 0; tmp < 4; tmp++) {
2115 		struct net2280_ep *ep = &dev->ep[tmp + 1];
2116 		struct net2280_dma_regs __iomem *dma;
2117 
2118 		if (ep->dma) {
2119 			abort_dma(ep);
2120 		} else {
2121 			dma = &dev->dma[tmp];
2122 			writel(BIT(DMA_ABORT), &dma->dmastat);
2123 			writel(0, &dma->dmactl);
2124 		}
2125 	}
2126 
2127 	writel(~0, &dev->regs->irqstat0), writel(~0, &dev->regs->irqstat1);
2128 
2129 	if (dev->bug7734_patched) {
2130 		/* reset, and enable pci */
2131 		tmp = readl(&dev->regs->devinit) |
2132 		    BIT(PCI_ENABLE) |
2133 		    BIT(FIFO_SOFT_RESET) |
2134 		    BIT(USB_SOFT_RESET) |
2135 		    BIT(M8051_RESET);
2136 
2137 		writel(tmp, &dev->regs->devinit);
2138 	}
2139 
2140 	/* always ep-{1,2,3,4} ... maybe not ep-3 or ep-4 */
2141 	INIT_LIST_HEAD(&dev->gadget.ep_list);
2142 
2143 	for (tmp = 1; tmp < dev->n_ep; tmp++)
2144 		list_add_tail(&dev->ep[tmp].ep.ep_list, &dev->gadget.ep_list);
2145 
2146 }
2147 
2148 static void usb_reset(struct net2280 *dev)
2149 {
2150 	if (dev->quirks & PLX_LEGACY)
2151 		return usb_reset_228x(dev);
2152 	return usb_reset_338x(dev);
2153 }
2154 
2155 static void usb_reinit_228x(struct net2280 *dev)
2156 {
2157 	u32	tmp;
2158 
2159 	/* basic endpoint init */
2160 	for (tmp = 0; tmp < 7; tmp++) {
2161 		struct net2280_ep	*ep = &dev->ep[tmp];
2162 
2163 		ep->ep.name = ep_info_dft[tmp].name;
2164 		ep->ep.caps = ep_info_dft[tmp].caps;
2165 		ep->dev = dev;
2166 		ep->num = tmp;
2167 
2168 		if (tmp > 0 && tmp <= 4) {
2169 			ep->fifo_size = 1024;
2170 			ep->dma = &dev->dma[tmp - 1];
2171 		} else
2172 			ep->fifo_size = 64;
2173 		ep->regs = &dev->epregs[tmp];
2174 		ep->cfg = &dev->epregs[tmp];
2175 		ep_reset_228x(dev->regs, ep);
2176 	}
2177 	usb_ep_set_maxpacket_limit(&dev->ep[0].ep, 64);
2178 	usb_ep_set_maxpacket_limit(&dev->ep[5].ep, 64);
2179 	usb_ep_set_maxpacket_limit(&dev->ep[6].ep, 64);
2180 
2181 	dev->gadget.ep0 = &dev->ep[0].ep;
2182 	dev->ep[0].stopped = 0;
2183 	INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
2184 
2185 	/* we want to prevent lowlevel/insecure access from the USB host,
2186 	 * but erratum 0119 means this enable bit is ignored
2187 	 */
2188 	for (tmp = 0; tmp < 5; tmp++)
2189 		writel(EP_DONTUSE, &dev->dep[tmp].dep_cfg);
2190 }
2191 
2192 static void usb_reinit_338x(struct net2280 *dev)
2193 {
2194 	int i;
2195 	u32 tmp, val;
2196 	static const u32 ne[9] = { 0, 1, 2, 3, 4, 1, 2, 3, 4 };
2197 	static const u32 ep_reg_addr[9] = { 0x00, 0xC0, 0x00, 0xC0, 0x00,
2198 						0x00, 0xC0, 0x00, 0xC0 };
2199 
2200 	/* basic endpoint init */
2201 	for (i = 0; i < dev->n_ep; i++) {
2202 		struct net2280_ep *ep = &dev->ep[i];
2203 
2204 		ep->ep.name = dev->enhanced_mode ? ep_info_adv[i].name :
2205 						   ep_info_dft[i].name;
2206 		ep->ep.caps = dev->enhanced_mode ? ep_info_adv[i].caps :
2207 						   ep_info_dft[i].caps;
2208 		ep->dev = dev;
2209 		ep->num = i;
2210 
2211 		if (i > 0 && i <= 4)
2212 			ep->dma = &dev->dma[i - 1];
2213 
2214 		if (dev->enhanced_mode) {
2215 			ep->cfg = &dev->epregs[ne[i]];
2216 			/*
2217 			 * Set USB endpoint number, hardware allows same number
2218 			 * in both directions.
2219 			 */
2220 			 if (i > 0 && i < 5)
2221 				writel(ne[i], &ep->cfg->ep_cfg);
2222 			ep->regs = (struct net2280_ep_regs __iomem *)
2223 				(((void __iomem *)&dev->epregs[ne[i]]) +
2224 				ep_reg_addr[i]);
2225 		} else {
2226 			ep->cfg = &dev->epregs[i];
2227 			ep->regs = &dev->epregs[i];
2228 		}
2229 
2230 		ep->fifo_size = (i != 0) ? 2048 : 512;
2231 
2232 		ep_reset_338x(dev->regs, ep);
2233 	}
2234 	usb_ep_set_maxpacket_limit(&dev->ep[0].ep, 512);
2235 
2236 	dev->gadget.ep0 = &dev->ep[0].ep;
2237 	dev->ep[0].stopped = 0;
2238 
2239 	/* Link layer set up */
2240 	if (dev->bug7734_patched) {
2241 		tmp = readl(&dev->usb_ext->usbctl2) &
2242 		    ~(BIT(U1_ENABLE) | BIT(U2_ENABLE) | BIT(LTM_ENABLE));
2243 		writel(tmp, &dev->usb_ext->usbctl2);
2244 	}
2245 
2246 	/* Hardware Defect and Workaround */
2247 	val = readl(&dev->ll_lfps_regs->ll_lfps_5);
2248 	val &= ~(0xf << TIMER_LFPS_6US);
2249 	val |= 0x5 << TIMER_LFPS_6US;
2250 	writel(val, &dev->ll_lfps_regs->ll_lfps_5);
2251 
2252 	val = readl(&dev->ll_lfps_regs->ll_lfps_6);
2253 	val &= ~(0xffff << TIMER_LFPS_80US);
2254 	val |= 0x0100 << TIMER_LFPS_80US;
2255 	writel(val, &dev->ll_lfps_regs->ll_lfps_6);
2256 
2257 	/*
2258 	 * AA_AB Errata. Issue 4. Workaround for SuperSpeed USB
2259 	 * Hot Reset Exit Handshake may Fail in Specific Case using
2260 	 * Default Register Settings. Workaround for Enumeration test.
2261 	 */
2262 	val = readl(&dev->ll_tsn_regs->ll_tsn_counters_2);
2263 	val &= ~(0x1f << HOT_TX_NORESET_TS2);
2264 	val |= 0x10 << HOT_TX_NORESET_TS2;
2265 	writel(val, &dev->ll_tsn_regs->ll_tsn_counters_2);
2266 
2267 	val = readl(&dev->ll_tsn_regs->ll_tsn_counters_3);
2268 	val &= ~(0x1f << HOT_RX_RESET_TS2);
2269 	val |= 0x3 << HOT_RX_RESET_TS2;
2270 	writel(val, &dev->ll_tsn_regs->ll_tsn_counters_3);
2271 
2272 	/*
2273 	 * Set Recovery Idle to Recover bit:
2274 	 * - On SS connections, setting Recovery Idle to Recover Fmw improves
2275 	 *   link robustness with various hosts and hubs.
2276 	 * - It is safe to set for all connection speeds; all chip revisions.
2277 	 * - R-M-W to leave other bits undisturbed.
2278 	 * - Reference PLX TT-7372
2279 	 */
2280 	val = readl(&dev->ll_chicken_reg->ll_tsn_chicken_bit);
2281 	val |= BIT(RECOVERY_IDLE_TO_RECOVER_FMW);
2282 	writel(val, &dev->ll_chicken_reg->ll_tsn_chicken_bit);
2283 
2284 	INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
2285 
2286 	/* disable dedicated endpoints */
2287 	writel(0x0D, &dev->dep[0].dep_cfg);
2288 	writel(0x0D, &dev->dep[1].dep_cfg);
2289 	writel(0x0E, &dev->dep[2].dep_cfg);
2290 	writel(0x0E, &dev->dep[3].dep_cfg);
2291 	writel(0x0F, &dev->dep[4].dep_cfg);
2292 	writel(0x0C, &dev->dep[5].dep_cfg);
2293 }
2294 
2295 static void usb_reinit(struct net2280 *dev)
2296 {
2297 	if (dev->quirks & PLX_LEGACY)
2298 		return usb_reinit_228x(dev);
2299 	return usb_reinit_338x(dev);
2300 }
2301 
2302 static void ep0_start_228x(struct net2280 *dev)
2303 {
2304 	writel(BIT(CLEAR_EP_HIDE_STATUS_PHASE) |
2305 		BIT(CLEAR_NAK_OUT_PACKETS) |
2306 		BIT(CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE),
2307 		&dev->epregs[0].ep_rsp);
2308 
2309 	/*
2310 	 * hardware optionally handles a bunch of standard requests
2311 	 * that the API hides from drivers anyway.  have it do so.
2312 	 * endpoint status/features are handled in software, to
2313 	 * help pass tests for some dubious behavior.
2314 	 */
2315 	writel(BIT(SET_TEST_MODE) |
2316 		BIT(SET_ADDRESS) |
2317 		BIT(DEVICE_SET_CLEAR_DEVICE_REMOTE_WAKEUP) |
2318 		BIT(GET_DEVICE_STATUS) |
2319 		BIT(GET_INTERFACE_STATUS),
2320 		&dev->usb->stdrsp);
2321 	writel(BIT(USB_ROOT_PORT_WAKEUP_ENABLE) |
2322 		BIT(SELF_POWERED_USB_DEVICE) |
2323 		BIT(REMOTE_WAKEUP_SUPPORT) |
2324 		(dev->softconnect << USB_DETECT_ENABLE) |
2325 		BIT(SELF_POWERED_STATUS),
2326 		&dev->usb->usbctl);
2327 
2328 	/* enable irqs so we can see ep0 and general operation  */
2329 	writel(BIT(SETUP_PACKET_INTERRUPT_ENABLE) |
2330 		BIT(ENDPOINT_0_INTERRUPT_ENABLE),
2331 		&dev->regs->pciirqenb0);
2332 	writel(BIT(PCI_INTERRUPT_ENABLE) |
2333 		BIT(PCI_MASTER_ABORT_RECEIVED_INTERRUPT_ENABLE) |
2334 		BIT(PCI_TARGET_ABORT_RECEIVED_INTERRUPT_ENABLE) |
2335 		BIT(PCI_RETRY_ABORT_INTERRUPT_ENABLE) |
2336 		BIT(VBUS_INTERRUPT_ENABLE) |
2337 		BIT(ROOT_PORT_RESET_INTERRUPT_ENABLE) |
2338 		BIT(SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE),
2339 		&dev->regs->pciirqenb1);
2340 
2341 	/* don't leave any writes posted */
2342 	(void) readl(&dev->usb->usbctl);
2343 }
2344 
2345 static void ep0_start_338x(struct net2280 *dev)
2346 {
2347 
2348 	if (dev->bug7734_patched)
2349 		writel(BIT(CLEAR_NAK_OUT_PACKETS_MODE) |
2350 		       BIT(SET_EP_HIDE_STATUS_PHASE),
2351 		       &dev->epregs[0].ep_rsp);
2352 
2353 	/*
2354 	 * hardware optionally handles a bunch of standard requests
2355 	 * that the API hides from drivers anyway.  have it do so.
2356 	 * endpoint status/features are handled in software, to
2357 	 * help pass tests for some dubious behavior.
2358 	 */
2359 	writel(BIT(SET_ISOCHRONOUS_DELAY) |
2360 	       BIT(SET_SEL) |
2361 	       BIT(SET_TEST_MODE) |
2362 	       BIT(SET_ADDRESS) |
2363 	       BIT(GET_INTERFACE_STATUS) |
2364 	       BIT(GET_DEVICE_STATUS),
2365 		&dev->usb->stdrsp);
2366 	dev->wakeup_enable = 1;
2367 	writel(BIT(USB_ROOT_PORT_WAKEUP_ENABLE) |
2368 	       (dev->softconnect << USB_DETECT_ENABLE) |
2369 	       BIT(DEVICE_REMOTE_WAKEUP_ENABLE),
2370 	       &dev->usb->usbctl);
2371 
2372 	/* enable irqs so we can see ep0 and general operation  */
2373 	writel(BIT(SETUP_PACKET_INTERRUPT_ENABLE) |
2374 	       BIT(ENDPOINT_0_INTERRUPT_ENABLE),
2375 	       &dev->regs->pciirqenb0);
2376 	writel(BIT(PCI_INTERRUPT_ENABLE) |
2377 	       BIT(ROOT_PORT_RESET_INTERRUPT_ENABLE) |
2378 	       BIT(SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE) |
2379 	       BIT(VBUS_INTERRUPT_ENABLE),
2380 	       &dev->regs->pciirqenb1);
2381 
2382 	/* don't leave any writes posted */
2383 	(void)readl(&dev->usb->usbctl);
2384 }
2385 
2386 static void ep0_start(struct net2280 *dev)
2387 {
2388 	if (dev->quirks & PLX_LEGACY)
2389 		return ep0_start_228x(dev);
2390 	return ep0_start_338x(dev);
2391 }
2392 
2393 /* when a driver is successfully registered, it will receive
2394  * control requests including set_configuration(), which enables
2395  * non-control requests.  then usb traffic follows until a
2396  * disconnect is reported.  then a host may connect again, or
2397  * the driver might get unbound.
2398  */
2399 static int net2280_start(struct usb_gadget *_gadget,
2400 		struct usb_gadget_driver *driver)
2401 {
2402 	struct net2280		*dev;
2403 	int			retval;
2404 	unsigned		i;
2405 
2406 	/* insist on high speed support from the driver, since
2407 	 * (dev->usb->xcvrdiag & FORCE_FULL_SPEED_MODE)
2408 	 * "must not be used in normal operation"
2409 	 */
2410 	if (!driver || driver->max_speed < USB_SPEED_HIGH ||
2411 			!driver->setup)
2412 		return -EINVAL;
2413 
2414 	dev = container_of(_gadget, struct net2280, gadget);
2415 
2416 	for (i = 0; i < dev->n_ep; i++)
2417 		dev->ep[i].irqs = 0;
2418 
2419 	/* hook up the driver ... */
2420 	driver->driver.bus = NULL;
2421 	dev->driver = driver;
2422 
2423 	retval = device_create_file(&dev->pdev->dev, &dev_attr_function);
2424 	if (retval)
2425 		goto err_unbind;
2426 	retval = device_create_file(&dev->pdev->dev, &dev_attr_queues);
2427 	if (retval)
2428 		goto err_func;
2429 
2430 	/* enable host detection and ep0; and we're ready
2431 	 * for set_configuration as well as eventual disconnect.
2432 	 */
2433 	net2280_led_active(dev, 1);
2434 
2435 	if ((dev->quirks & PLX_PCIE) && !dev->bug7734_patched)
2436 		defect7374_enable_data_eps_zero(dev);
2437 
2438 	ep0_start(dev);
2439 
2440 	/* pci writes may still be posted */
2441 	return 0;
2442 
2443 err_func:
2444 	device_remove_file(&dev->pdev->dev, &dev_attr_function);
2445 err_unbind:
2446 	dev->driver = NULL;
2447 	return retval;
2448 }
2449 
2450 static void stop_activity(struct net2280 *dev, struct usb_gadget_driver *driver)
2451 {
2452 	int			i;
2453 
2454 	/* don't disconnect if it's not connected */
2455 	if (dev->gadget.speed == USB_SPEED_UNKNOWN)
2456 		driver = NULL;
2457 
2458 	/* stop hardware; prevent new request submissions;
2459 	 * and kill any outstanding requests.
2460 	 */
2461 	usb_reset(dev);
2462 	for (i = 0; i < dev->n_ep; i++)
2463 		nuke(&dev->ep[i]);
2464 
2465 	/* report disconnect; the driver is already quiesced */
2466 	if (driver) {
2467 		spin_unlock(&dev->lock);
2468 		driver->disconnect(&dev->gadget);
2469 		spin_lock(&dev->lock);
2470 	}
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 __releases(dev->lock)
3345 __acquires(dev->lock)
3346 {
3347 	struct net2280_ep	*ep;
3348 	u32			tmp, num, mask, scratch;
3349 
3350 	/* after disconnect there's nothing else to do! */
3351 	tmp = BIT(VBUS_INTERRUPT) | BIT(ROOT_PORT_RESET_INTERRUPT);
3352 	mask = BIT(SUPER_SPEED) | BIT(HIGH_SPEED) | BIT(FULL_SPEED);
3353 
3354 	/* VBUS disconnect is indicated by VBUS_PIN and VBUS_INTERRUPT set.
3355 	 * Root Port Reset is indicated by ROOT_PORT_RESET_INTERRUPT set and
3356 	 * both HIGH_SPEED and FULL_SPEED clear (as ROOT_PORT_RESET_INTERRUPT
3357 	 * only indicates a change in the reset state).
3358 	 */
3359 	if (stat & tmp) {
3360 		bool	reset = false;
3361 		bool	disconnect = false;
3362 
3363 		/*
3364 		 * Ignore disconnects and resets if the speed hasn't been set.
3365 		 * VBUS can bounce and there's always an initial reset.
3366 		 */
3367 		writel(tmp, &dev->regs->irqstat1);
3368 		if (dev->gadget.speed != USB_SPEED_UNKNOWN) {
3369 			if ((stat & BIT(VBUS_INTERRUPT)) &&
3370 					(readl(&dev->usb->usbctl) &
3371 						BIT(VBUS_PIN)) == 0) {
3372 				disconnect = true;
3373 				ep_dbg(dev, "disconnect %s\n",
3374 						dev->driver->driver.name);
3375 			} else if ((stat & BIT(ROOT_PORT_RESET_INTERRUPT)) &&
3376 					(readl(&dev->usb->usbstat) & mask)
3377 						== 0) {
3378 				reset = true;
3379 				ep_dbg(dev, "reset %s\n",
3380 						dev->driver->driver.name);
3381 			}
3382 
3383 			if (disconnect || reset) {
3384 				stop_activity(dev, dev->driver);
3385 				ep0_start(dev);
3386 				spin_unlock(&dev->lock);
3387 				if (reset)
3388 					usb_gadget_udc_reset
3389 						(&dev->gadget, dev->driver);
3390 				else
3391 					(dev->driver->disconnect)
3392 						(&dev->gadget);
3393 				spin_lock(&dev->lock);
3394 				return;
3395 			}
3396 		}
3397 		stat &= ~tmp;
3398 
3399 		/* vBUS can bounce ... one of many reasons to ignore the
3400 		 * notion of hotplug events on bus connect/disconnect!
3401 		 */
3402 		if (!stat)
3403 			return;
3404 	}
3405 
3406 	/* NOTE: chip stays in PCI D0 state for now, but it could
3407 	 * enter D1 to save more power
3408 	 */
3409 	tmp = BIT(SUSPEND_REQUEST_CHANGE_INTERRUPT);
3410 	if (stat & tmp) {
3411 		writel(tmp, &dev->regs->irqstat1);
3412 		spin_unlock(&dev->lock);
3413 		if (stat & BIT(SUSPEND_REQUEST_INTERRUPT)) {
3414 			if (dev->driver->suspend)
3415 				dev->driver->suspend(&dev->gadget);
3416 			if (!enable_suspend)
3417 				stat &= ~BIT(SUSPEND_REQUEST_INTERRUPT);
3418 		} else {
3419 			if (dev->driver->resume)
3420 				dev->driver->resume(&dev->gadget);
3421 			/* at high speed, note erratum 0133 */
3422 		}
3423 		spin_lock(&dev->lock);
3424 		stat &= ~tmp;
3425 	}
3426 
3427 	/* clear any other status/irqs */
3428 	if (stat)
3429 		writel(stat, &dev->regs->irqstat1);
3430 
3431 	/* some status we can just ignore */
3432 	if (dev->quirks & PLX_2280)
3433 		stat &= ~(BIT(CONTROL_STATUS_INTERRUPT) |
3434 			  BIT(SUSPEND_REQUEST_INTERRUPT) |
3435 			  BIT(RESUME_INTERRUPT) |
3436 			  BIT(SOF_INTERRUPT));
3437 	else
3438 		stat &= ~(BIT(CONTROL_STATUS_INTERRUPT) |
3439 			  BIT(RESUME_INTERRUPT) |
3440 			  BIT(SOF_DOWN_INTERRUPT) |
3441 			  BIT(SOF_INTERRUPT));
3442 
3443 	if (!stat)
3444 		return;
3445 	/* ep_dbg(dev, "irqstat1 %08x\n", stat);*/
3446 
3447 	/* DMA status, for ep-{a,b,c,d} */
3448 	scratch = stat & DMA_INTERRUPTS;
3449 	stat &= ~DMA_INTERRUPTS;
3450 	scratch >>= 9;
3451 	for (num = 0; scratch; num++) {
3452 		struct net2280_dma_regs	__iomem *dma;
3453 
3454 		tmp = BIT(num);
3455 		if ((tmp & scratch) == 0)
3456 			continue;
3457 		scratch ^= tmp;
3458 
3459 		ep = &dev->ep[num + 1];
3460 		dma = ep->dma;
3461 
3462 		if (!dma)
3463 			continue;
3464 
3465 		/* clear ep's dma status */
3466 		tmp = readl(&dma->dmastat);
3467 		writel(tmp, &dma->dmastat);
3468 
3469 		/* dma sync*/
3470 		if (dev->quirks & PLX_PCIE) {
3471 			u32 r_dmacount = readl(&dma->dmacount);
3472 			if (!ep->is_in &&  (r_dmacount & 0x00FFFFFF) &&
3473 			    (tmp & BIT(DMA_TRANSACTION_DONE_INTERRUPT)))
3474 				continue;
3475 		}
3476 
3477 		if (!(tmp & BIT(DMA_TRANSACTION_DONE_INTERRUPT))) {
3478 			ep_dbg(ep->dev, "%s no xact done? %08x\n",
3479 				ep->ep.name, tmp);
3480 			continue;
3481 		}
3482 		stop_dma(ep->dma);
3483 
3484 		/* OUT transfers terminate when the data from the
3485 		 * host is in our memory.  Process whatever's done.
3486 		 * On this path, we know transfer's last packet wasn't
3487 		 * less than req->length. NAK_OUT_PACKETS may be set,
3488 		 * or the FIFO may already be holding new packets.
3489 		 *
3490 		 * IN transfers can linger in the FIFO for a very
3491 		 * long time ... we ignore that for now, accounting
3492 		 * precisely (like PIO does) needs per-packet irqs
3493 		 */
3494 		scan_dma_completions(ep);
3495 
3496 		/* disable dma on inactive queues; else maybe restart */
3497 		if (!list_empty(&ep->queue)) {
3498 			tmp = readl(&dma->dmactl);
3499 			restart_dma(ep);
3500 		}
3501 		ep->irqs++;
3502 	}
3503 
3504 	/* NOTE:  there are other PCI errors we might usefully notice.
3505 	 * if they appear very often, here's where to try recovering.
3506 	 */
3507 	if (stat & PCI_ERROR_INTERRUPTS) {
3508 		ep_err(dev, "pci dma error; stat %08x\n", stat);
3509 		stat &= ~PCI_ERROR_INTERRUPTS;
3510 		/* these are fatal errors, but "maybe" they won't
3511 		 * happen again ...
3512 		 */
3513 		stop_activity(dev, dev->driver);
3514 		ep0_start(dev);
3515 		stat = 0;
3516 	}
3517 
3518 	if (stat)
3519 		ep_dbg(dev, "unhandled irqstat1 %08x\n", stat);
3520 }
3521 
3522 static irqreturn_t net2280_irq(int irq, void *_dev)
3523 {
3524 	struct net2280		*dev = _dev;
3525 
3526 	/* shared interrupt, not ours */
3527 	if ((dev->quirks & PLX_LEGACY) &&
3528 		(!(readl(&dev->regs->irqstat0) & BIT(INTA_ASSERTED))))
3529 		return IRQ_NONE;
3530 
3531 	spin_lock(&dev->lock);
3532 
3533 	/* handle disconnect, dma, and more */
3534 	handle_stat1_irqs(dev, readl(&dev->regs->irqstat1));
3535 
3536 	/* control requests and PIO */
3537 	handle_stat0_irqs(dev, readl(&dev->regs->irqstat0));
3538 
3539 	if (dev->quirks & PLX_PCIE) {
3540 		/* re-enable interrupt to trigger any possible new interrupt */
3541 		u32 pciirqenb1 = readl(&dev->regs->pciirqenb1);
3542 		writel(pciirqenb1 & 0x7FFFFFFF, &dev->regs->pciirqenb1);
3543 		writel(pciirqenb1, &dev->regs->pciirqenb1);
3544 	}
3545 
3546 	spin_unlock(&dev->lock);
3547 
3548 	return IRQ_HANDLED;
3549 }
3550 
3551 /*-------------------------------------------------------------------------*/
3552 
3553 static void gadget_release(struct device *_dev)
3554 {
3555 	struct net2280	*dev = dev_get_drvdata(_dev);
3556 
3557 	kfree(dev);
3558 }
3559 
3560 /* tear down the binding between this driver and the pci device */
3561 
3562 static void net2280_remove(struct pci_dev *pdev)
3563 {
3564 	struct net2280		*dev = pci_get_drvdata(pdev);
3565 
3566 	usb_del_gadget_udc(&dev->gadget);
3567 
3568 	BUG_ON(dev->driver);
3569 
3570 	/* then clean up the resources we allocated during probe() */
3571 	if (dev->requests) {
3572 		int		i;
3573 		for (i = 1; i < 5; i++) {
3574 			if (!dev->ep[i].dummy)
3575 				continue;
3576 			dma_pool_free(dev->requests, dev->ep[i].dummy,
3577 					dev->ep[i].td_dma);
3578 		}
3579 		dma_pool_destroy(dev->requests);
3580 	}
3581 	if (dev->got_irq)
3582 		free_irq(pdev->irq, dev);
3583 	if (dev->quirks & PLX_PCIE)
3584 		pci_disable_msi(pdev);
3585 	if (dev->regs) {
3586 		net2280_led_shutdown(dev);
3587 		iounmap(dev->regs);
3588 	}
3589 	if (dev->region)
3590 		release_mem_region(pci_resource_start(pdev, 0),
3591 				pci_resource_len(pdev, 0));
3592 	if (dev->enabled)
3593 		pci_disable_device(pdev);
3594 	device_remove_file(&pdev->dev, &dev_attr_registers);
3595 
3596 	ep_info(dev, "unbind\n");
3597 }
3598 
3599 /* wrap this driver around the specified device, but
3600  * don't respond over USB until a gadget driver binds to us.
3601  */
3602 
3603 static int net2280_probe(struct pci_dev *pdev, const struct pci_device_id *id)
3604 {
3605 	struct net2280		*dev;
3606 	unsigned long		resource, len;
3607 	void			__iomem *base = NULL;
3608 	int			retval, i;
3609 
3610 	/* alloc, and start init */
3611 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
3612 	if (dev == NULL) {
3613 		retval = -ENOMEM;
3614 		goto done;
3615 	}
3616 
3617 	pci_set_drvdata(pdev, dev);
3618 	spin_lock_init(&dev->lock);
3619 	dev->quirks = id->driver_data;
3620 	dev->pdev = pdev;
3621 	dev->gadget.ops = &net2280_ops;
3622 	dev->gadget.max_speed = (dev->quirks & PLX_SUPERSPEED) ?
3623 				USB_SPEED_SUPER : USB_SPEED_HIGH;
3624 
3625 	/* the "gadget" abstracts/virtualizes the controller */
3626 	dev->gadget.name = driver_name;
3627 
3628 	/* now all the pci goodies ... */
3629 	if (pci_enable_device(pdev) < 0) {
3630 		retval = -ENODEV;
3631 		goto done;
3632 	}
3633 	dev->enabled = 1;
3634 
3635 	/* BAR 0 holds all the registers
3636 	 * BAR 1 is 8051 memory; unused here (note erratum 0103)
3637 	 * BAR 2 is fifo memory; unused here
3638 	 */
3639 	resource = pci_resource_start(pdev, 0);
3640 	len = pci_resource_len(pdev, 0);
3641 	if (!request_mem_region(resource, len, driver_name)) {
3642 		ep_dbg(dev, "controller already in use\n");
3643 		retval = -EBUSY;
3644 		goto done;
3645 	}
3646 	dev->region = 1;
3647 
3648 	/* FIXME provide firmware download interface to put
3649 	 * 8051 code into the chip, e.g. to turn on PCI PM.
3650 	 */
3651 
3652 	base = ioremap_nocache(resource, len);
3653 	if (base == NULL) {
3654 		ep_dbg(dev, "can't map memory\n");
3655 		retval = -EFAULT;
3656 		goto done;
3657 	}
3658 	dev->regs = (struct net2280_regs __iomem *) base;
3659 	dev->usb = (struct net2280_usb_regs __iomem *) (base + 0x0080);
3660 	dev->pci = (struct net2280_pci_regs __iomem *) (base + 0x0100);
3661 	dev->dma = (struct net2280_dma_regs __iomem *) (base + 0x0180);
3662 	dev->dep = (struct net2280_dep_regs __iomem *) (base + 0x0200);
3663 	dev->epregs = (struct net2280_ep_regs __iomem *) (base + 0x0300);
3664 
3665 	if (dev->quirks & PLX_PCIE) {
3666 		u32 fsmvalue;
3667 		u32 usbstat;
3668 		dev->usb_ext = (struct usb338x_usb_ext_regs __iomem *)
3669 							(base + 0x00b4);
3670 		dev->llregs = (struct usb338x_ll_regs __iomem *)
3671 							(base + 0x0700);
3672 		dev->ll_lfps_regs = (struct usb338x_ll_lfps_regs __iomem *)
3673 							(base + 0x0748);
3674 		dev->ll_tsn_regs = (struct usb338x_ll_tsn_regs __iomem *)
3675 							(base + 0x077c);
3676 		dev->ll_chicken_reg = (struct usb338x_ll_chi_regs __iomem *)
3677 							(base + 0x079c);
3678 		dev->plregs = (struct usb338x_pl_regs __iomem *)
3679 							(base + 0x0800);
3680 		usbstat = readl(&dev->usb->usbstat);
3681 		dev->enhanced_mode = !!(usbstat & BIT(11));
3682 		dev->n_ep = (dev->enhanced_mode) ? 9 : 5;
3683 		/* put into initial config, link up all endpoints */
3684 		fsmvalue = get_idx_reg(dev->regs, SCRATCH) &
3685 					(0xf << DEFECT7374_FSM_FIELD);
3686 		/* See if firmware needs to set up for workaround: */
3687 		if (fsmvalue == DEFECT7374_FSM_SS_CONTROL_READ) {
3688 			dev->bug7734_patched = 1;
3689 			writel(0, &dev->usb->usbctl);
3690 		} else
3691 			dev->bug7734_patched = 0;
3692 	} else {
3693 		dev->enhanced_mode = 0;
3694 		dev->n_ep = 7;
3695 		/* put into initial config, link up all endpoints */
3696 		writel(0, &dev->usb->usbctl);
3697 	}
3698 
3699 	usb_reset(dev);
3700 	usb_reinit(dev);
3701 
3702 	/* irq setup after old hardware is cleaned up */
3703 	if (!pdev->irq) {
3704 		ep_err(dev, "No IRQ.  Check PCI setup!\n");
3705 		retval = -ENODEV;
3706 		goto done;
3707 	}
3708 
3709 	if (dev->quirks & PLX_PCIE)
3710 		if (pci_enable_msi(pdev))
3711 			ep_err(dev, "Failed to enable MSI mode\n");
3712 
3713 	if (request_irq(pdev->irq, net2280_irq, IRQF_SHARED,
3714 							driver_name, dev)) {
3715 		ep_err(dev, "request interrupt %d failed\n", pdev->irq);
3716 		retval = -EBUSY;
3717 		goto done;
3718 	}
3719 	dev->got_irq = 1;
3720 
3721 	/* DMA setup */
3722 	/* NOTE:  we know only the 32 LSBs of dma addresses may be nonzero */
3723 	dev->requests = dma_pool_create("requests", &pdev->dev,
3724 		sizeof(struct net2280_dma),
3725 		0 /* no alignment requirements */,
3726 		0 /* or page-crossing issues */);
3727 	if (!dev->requests) {
3728 		ep_dbg(dev, "can't get request pool\n");
3729 		retval = -ENOMEM;
3730 		goto done;
3731 	}
3732 	for (i = 1; i < 5; i++) {
3733 		struct net2280_dma	*td;
3734 
3735 		td = dma_pool_alloc(dev->requests, GFP_KERNEL,
3736 				&dev->ep[i].td_dma);
3737 		if (!td) {
3738 			ep_dbg(dev, "can't get dummy %d\n", i);
3739 			retval = -ENOMEM;
3740 			goto done;
3741 		}
3742 		td->dmacount = 0;	/* not VALID */
3743 		td->dmadesc = td->dmaaddr;
3744 		dev->ep[i].dummy = td;
3745 	}
3746 
3747 	/* enable lower-overhead pci memory bursts during DMA */
3748 	if (dev->quirks & PLX_LEGACY)
3749 		writel(BIT(DMA_MEMORY_WRITE_AND_INVALIDATE_ENABLE) |
3750 			/*
3751 			 * 256 write retries may not be enough...
3752 			   BIT(PCI_RETRY_ABORT_ENABLE) |
3753 			*/
3754 			BIT(DMA_READ_MULTIPLE_ENABLE) |
3755 			BIT(DMA_READ_LINE_ENABLE),
3756 			&dev->pci->pcimstctl);
3757 	/* erratum 0115 shouldn't appear: Linux inits PCI_LATENCY_TIMER */
3758 	pci_set_master(pdev);
3759 	pci_try_set_mwi(pdev);
3760 
3761 	/* ... also flushes any posted pci writes */
3762 	dev->chiprev = get_idx_reg(dev->regs, REG_CHIPREV) & 0xffff;
3763 
3764 	/* done */
3765 	ep_info(dev, "%s\n", driver_desc);
3766 	ep_info(dev, "irq %d, pci mem %p, chip rev %04x\n",
3767 			pdev->irq, base, dev->chiprev);
3768 	ep_info(dev, "version: " DRIVER_VERSION "; %s\n",
3769 		dev->enhanced_mode ? "enhanced mode" : "legacy mode");
3770 	retval = device_create_file(&pdev->dev, &dev_attr_registers);
3771 	if (retval)
3772 		goto done;
3773 
3774 	retval = usb_add_gadget_udc_release(&pdev->dev, &dev->gadget,
3775 			gadget_release);
3776 	if (retval)
3777 		goto done;
3778 	return 0;
3779 
3780 done:
3781 	if (dev)
3782 		net2280_remove(pdev);
3783 	return retval;
3784 }
3785 
3786 /* make sure the board is quiescent; otherwise it will continue
3787  * generating IRQs across the upcoming reboot.
3788  */
3789 
3790 static void net2280_shutdown(struct pci_dev *pdev)
3791 {
3792 	struct net2280		*dev = pci_get_drvdata(pdev);
3793 
3794 	/* disable IRQs */
3795 	writel(0, &dev->regs->pciirqenb0);
3796 	writel(0, &dev->regs->pciirqenb1);
3797 
3798 	/* disable the pullup so the host will think we're gone */
3799 	writel(0, &dev->usb->usbctl);
3800 
3801 }
3802 
3803 
3804 /*-------------------------------------------------------------------------*/
3805 
3806 static const struct pci_device_id pci_ids[] = { {
3807 	.class =	PCI_CLASS_SERIAL_USB_DEVICE,
3808 	.class_mask =	~0,
3809 	.vendor =	PCI_VENDOR_ID_PLX_LEGACY,
3810 	.device =	0x2280,
3811 	.subvendor =	PCI_ANY_ID,
3812 	.subdevice =	PCI_ANY_ID,
3813 	.driver_data =	PLX_LEGACY | PLX_2280,
3814 	}, {
3815 	.class =	PCI_CLASS_SERIAL_USB_DEVICE,
3816 	.class_mask =	~0,
3817 	.vendor =	PCI_VENDOR_ID_PLX_LEGACY,
3818 	.device =	0x2282,
3819 	.subvendor =	PCI_ANY_ID,
3820 	.subdevice =	PCI_ANY_ID,
3821 	.driver_data =	PLX_LEGACY,
3822 	},
3823 	{
3824 	.class =	PCI_CLASS_SERIAL_USB_DEVICE,
3825 	.class_mask =	~0,
3826 	.vendor =	PCI_VENDOR_ID_PLX,
3827 	.device =	0x2380,
3828 	.subvendor =	PCI_ANY_ID,
3829 	.subdevice =	PCI_ANY_ID,
3830 	.driver_data =	PLX_PCIE,
3831 	 },
3832 	{
3833 	.class =	((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
3834 	.class_mask =	~0,
3835 	.vendor =	PCI_VENDOR_ID_PLX,
3836 	.device =	0x3380,
3837 	.subvendor =	PCI_ANY_ID,
3838 	.subdevice =	PCI_ANY_ID,
3839 	.driver_data =	PLX_PCIE | PLX_SUPERSPEED,
3840 	 },
3841 	{
3842 	.class =	PCI_CLASS_SERIAL_USB_DEVICE,
3843 	.class_mask =	~0,
3844 	.vendor =	PCI_VENDOR_ID_PLX,
3845 	.device =	0x3382,
3846 	.subvendor =	PCI_ANY_ID,
3847 	.subdevice =	PCI_ANY_ID,
3848 	.driver_data =	PLX_PCIE | PLX_SUPERSPEED,
3849 	 },
3850 { /* end: all zeroes */ }
3851 };
3852 MODULE_DEVICE_TABLE(pci, pci_ids);
3853 
3854 /* pci driver glue; this is a "new style" PCI driver module */
3855 static struct pci_driver net2280_pci_driver = {
3856 	.name =		(char *) driver_name,
3857 	.id_table =	pci_ids,
3858 
3859 	.probe =	net2280_probe,
3860 	.remove =	net2280_remove,
3861 	.shutdown =	net2280_shutdown,
3862 
3863 	/* FIXME add power management support */
3864 };
3865 
3866 module_pci_driver(net2280_pci_driver);
3867 
3868 MODULE_DESCRIPTION(DRIVER_DESC);
3869 MODULE_AUTHOR("David Brownell");
3870 MODULE_LICENSE("GPL");
3871