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