xref: /openbmc/linux/drivers/usb/gadget/udc/net2280.c (revision 349d03ff)
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 = NULL;
1244 	struct net2280_request	*iter;
1245 	unsigned long		flags;
1246 	u32			dmactl;
1247 	int			stopped;
1248 
1249 	ep = container_of(_ep, struct net2280_ep, ep);
1250 	if (!_ep || (!ep->desc && ep->num != 0) || !_req) {
1251 		pr_err("%s: Invalid ep=%p or ep->desc or req=%p\n",
1252 						__func__, _ep, _req);
1253 		return -EINVAL;
1254 	}
1255 
1256 	spin_lock_irqsave(&ep->dev->lock, flags);
1257 	stopped = ep->stopped;
1258 
1259 	/* quiesce dma while we patch the queue */
1260 	dmactl = 0;
1261 	ep->stopped = 1;
1262 	if (ep->dma) {
1263 		dmactl = readl(&ep->dma->dmactl);
1264 		/* WARNING erratum 0127 may kick in ... */
1265 		stop_dma(ep->dma);
1266 		scan_dma_completions(ep);
1267 	}
1268 
1269 	/* make sure it's still queued on this endpoint */
1270 	list_for_each_entry(iter, &ep->queue, queue) {
1271 		if (&iter->req != _req)
1272 			continue;
1273 		req = iter;
1274 		break;
1275 	}
1276 	if (!req) {
1277 		ep->stopped = stopped;
1278 		spin_unlock_irqrestore(&ep->dev->lock, flags);
1279 		ep_dbg(ep->dev, "%s: Request mismatch\n", __func__);
1280 		return -EINVAL;
1281 	}
1282 
1283 	/* queue head may be partially complete. */
1284 	if (ep->queue.next == &req->queue) {
1285 		if (ep->dma) {
1286 			ep_dbg(ep->dev, "unlink (%s) dma\n", _ep->name);
1287 			_req->status = -ECONNRESET;
1288 			abort_dma(ep);
1289 			if (likely(ep->queue.next == &req->queue)) {
1290 				/* NOTE: misreports single-transfer mode*/
1291 				req->td->dmacount = 0;	/* invalidate */
1292 				dma_done(ep, req,
1293 					readl(&ep->dma->dmacount),
1294 					-ECONNRESET);
1295 			}
1296 		} else {
1297 			ep_dbg(ep->dev, "unlink (%s) pio\n", _ep->name);
1298 			done(ep, req, -ECONNRESET);
1299 		}
1300 		req = NULL;
1301 	}
1302 
1303 	if (req)
1304 		done(ep, req, -ECONNRESET);
1305 	ep->stopped = stopped;
1306 
1307 	if (ep->dma) {
1308 		/* turn off dma on inactive queues */
1309 		if (list_empty(&ep->queue))
1310 			stop_dma(ep->dma);
1311 		else if (!ep->stopped) {
1312 			/* resume current request, or start new one */
1313 			if (req)
1314 				writel(dmactl, &ep->dma->dmactl);
1315 			else
1316 				start_dma(ep, list_entry(ep->queue.next,
1317 					struct net2280_request, queue));
1318 		}
1319 	}
1320 
1321 	spin_unlock_irqrestore(&ep->dev->lock, flags);
1322 	return 0;
1323 }
1324 
1325 /*-------------------------------------------------------------------------*/
1326 
1327 static int net2280_fifo_status(struct usb_ep *_ep);
1328 
1329 static int
1330 net2280_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged)
1331 {
1332 	struct net2280_ep	*ep;
1333 	unsigned long		flags;
1334 	int			retval = 0;
1335 
1336 	ep = container_of(_ep, struct net2280_ep, ep);
1337 	if (!_ep || (!ep->desc && ep->num != 0)) {
1338 		pr_err("%s: Invalid ep=%p or ep->desc\n", __func__, _ep);
1339 		return -EINVAL;
1340 	}
1341 	if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN) {
1342 		retval = -ESHUTDOWN;
1343 		goto print_err;
1344 	}
1345 	if (ep->desc /* not ep0 */ && (ep->desc->bmAttributes & 0x03)
1346 						== USB_ENDPOINT_XFER_ISOC) {
1347 		retval = -EINVAL;
1348 		goto print_err;
1349 	}
1350 
1351 	spin_lock_irqsave(&ep->dev->lock, flags);
1352 	if (!list_empty(&ep->queue)) {
1353 		retval = -EAGAIN;
1354 		goto print_unlock;
1355 	} else if (ep->is_in && value && net2280_fifo_status(_ep) != 0) {
1356 		retval = -EAGAIN;
1357 		goto print_unlock;
1358 	} else {
1359 		ep_vdbg(ep->dev, "%s %s %s\n", _ep->name,
1360 				value ? "set" : "clear",
1361 				wedged ? "wedge" : "halt");
1362 		/* set/clear, then synch memory views with the device */
1363 		if (value) {
1364 			if (ep->num == 0)
1365 				ep->dev->protocol_stall = 1;
1366 			else
1367 				set_halt(ep);
1368 			if (wedged)
1369 				ep->wedged = 1;
1370 		} else {
1371 			clear_halt(ep);
1372 			if (ep->dev->quirks & PLX_PCIE &&
1373 				!list_empty(&ep->queue) && ep->td_dma)
1374 					restart_dma(ep);
1375 			ep->wedged = 0;
1376 		}
1377 		(void) readl(&ep->regs->ep_rsp);
1378 	}
1379 	spin_unlock_irqrestore(&ep->dev->lock, flags);
1380 
1381 	return retval;
1382 
1383 print_unlock:
1384 	spin_unlock_irqrestore(&ep->dev->lock, flags);
1385 print_err:
1386 	dev_err(&ep->dev->pdev->dev, "%s: error=%d\n", __func__, retval);
1387 	return retval;
1388 }
1389 
1390 static int net2280_set_halt(struct usb_ep *_ep, int value)
1391 {
1392 	return net2280_set_halt_and_wedge(_ep, value, 0);
1393 }
1394 
1395 static int net2280_set_wedge(struct usb_ep *_ep)
1396 {
1397 	if (!_ep || _ep->name == ep0name) {
1398 		pr_err("%s: Invalid ep=%p or ep0\n", __func__, _ep);
1399 		return -EINVAL;
1400 	}
1401 	return net2280_set_halt_and_wedge(_ep, 1, 1);
1402 }
1403 
1404 static int net2280_fifo_status(struct usb_ep *_ep)
1405 {
1406 	struct net2280_ep	*ep;
1407 	u32			avail;
1408 
1409 	ep = container_of(_ep, struct net2280_ep, ep);
1410 	if (!_ep || (!ep->desc && ep->num != 0)) {
1411 		pr_err("%s: Invalid ep=%p or ep->desc\n", __func__, _ep);
1412 		return -ENODEV;
1413 	}
1414 	if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN) {
1415 		dev_err(&ep->dev->pdev->dev,
1416 			"%s: Invalid driver=%p or speed=%d\n",
1417 			__func__, ep->dev->driver, ep->dev->gadget.speed);
1418 		return -ESHUTDOWN;
1419 	}
1420 
1421 	avail = readl(&ep->regs->ep_avail) & (BIT(12) - 1);
1422 	if (avail > ep->fifo_size) {
1423 		dev_err(&ep->dev->pdev->dev, "%s: Fifo overflow\n", __func__);
1424 		return -EOVERFLOW;
1425 	}
1426 	if (ep->is_in)
1427 		avail = ep->fifo_size - avail;
1428 	return avail;
1429 }
1430 
1431 static void net2280_fifo_flush(struct usb_ep *_ep)
1432 {
1433 	struct net2280_ep	*ep;
1434 
1435 	ep = container_of(_ep, struct net2280_ep, ep);
1436 	if (!_ep || (!ep->desc && ep->num != 0)) {
1437 		pr_err("%s: Invalid ep=%p or ep->desc\n", __func__, _ep);
1438 		return;
1439 	}
1440 	if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN) {
1441 		dev_err(&ep->dev->pdev->dev,
1442 			"%s: Invalid driver=%p or speed=%d\n",
1443 			__func__, ep->dev->driver, ep->dev->gadget.speed);
1444 		return;
1445 	}
1446 
1447 	writel(BIT(FIFO_FLUSH), &ep->regs->ep_stat);
1448 	(void) readl(&ep->regs->ep_rsp);
1449 }
1450 
1451 static const struct usb_ep_ops net2280_ep_ops = {
1452 	.enable		= net2280_enable,
1453 	.disable	= net2280_disable,
1454 
1455 	.alloc_request	= net2280_alloc_request,
1456 	.free_request	= net2280_free_request,
1457 
1458 	.queue		= net2280_queue,
1459 	.dequeue	= net2280_dequeue,
1460 
1461 	.set_halt	= net2280_set_halt,
1462 	.set_wedge	= net2280_set_wedge,
1463 	.fifo_status	= net2280_fifo_status,
1464 	.fifo_flush	= net2280_fifo_flush,
1465 };
1466 
1467 /*-------------------------------------------------------------------------*/
1468 
1469 static int net2280_get_frame(struct usb_gadget *_gadget)
1470 {
1471 	struct net2280		*dev;
1472 	unsigned long		flags;
1473 	u16			retval;
1474 
1475 	if (!_gadget)
1476 		return -ENODEV;
1477 	dev = container_of(_gadget, struct net2280, gadget);
1478 	spin_lock_irqsave(&dev->lock, flags);
1479 	retval = get_idx_reg(dev->regs, REG_FRAME) & 0x03ff;
1480 	spin_unlock_irqrestore(&dev->lock, flags);
1481 	return retval;
1482 }
1483 
1484 static int net2280_wakeup(struct usb_gadget *_gadget)
1485 {
1486 	struct net2280		*dev;
1487 	u32			tmp;
1488 	unsigned long		flags;
1489 
1490 	if (!_gadget)
1491 		return 0;
1492 	dev = container_of(_gadget, struct net2280, gadget);
1493 
1494 	spin_lock_irqsave(&dev->lock, flags);
1495 	tmp = readl(&dev->usb->usbctl);
1496 	if (tmp & BIT(DEVICE_REMOTE_WAKEUP_ENABLE))
1497 		writel(BIT(GENERATE_RESUME), &dev->usb->usbstat);
1498 	spin_unlock_irqrestore(&dev->lock, flags);
1499 
1500 	/* pci writes may still be posted */
1501 	return 0;
1502 }
1503 
1504 static int net2280_set_selfpowered(struct usb_gadget *_gadget, int value)
1505 {
1506 	struct net2280		*dev;
1507 	u32			tmp;
1508 	unsigned long		flags;
1509 
1510 	if (!_gadget)
1511 		return 0;
1512 	dev = container_of(_gadget, struct net2280, gadget);
1513 
1514 	spin_lock_irqsave(&dev->lock, flags);
1515 	tmp = readl(&dev->usb->usbctl);
1516 	if (value) {
1517 		tmp |= BIT(SELF_POWERED_STATUS);
1518 		_gadget->is_selfpowered = 1;
1519 	} else {
1520 		tmp &= ~BIT(SELF_POWERED_STATUS);
1521 		_gadget->is_selfpowered = 0;
1522 	}
1523 	writel(tmp, &dev->usb->usbctl);
1524 	spin_unlock_irqrestore(&dev->lock, flags);
1525 
1526 	return 0;
1527 }
1528 
1529 static int net2280_pullup(struct usb_gadget *_gadget, int is_on)
1530 {
1531 	struct net2280  *dev;
1532 	u32             tmp;
1533 	unsigned long   flags;
1534 
1535 	if (!_gadget)
1536 		return -ENODEV;
1537 	dev = container_of(_gadget, struct net2280, gadget);
1538 
1539 	spin_lock_irqsave(&dev->lock, flags);
1540 	tmp = readl(&dev->usb->usbctl);
1541 	dev->softconnect = (is_on != 0);
1542 	if (is_on) {
1543 		ep0_start(dev);
1544 		writel(tmp | BIT(USB_DETECT_ENABLE), &dev->usb->usbctl);
1545 	} else {
1546 		writel(tmp & ~BIT(USB_DETECT_ENABLE), &dev->usb->usbctl);
1547 		stop_activity(dev, NULL);
1548 	}
1549 
1550 	spin_unlock_irqrestore(&dev->lock, flags);
1551 
1552 	return 0;
1553 }
1554 
1555 static struct usb_ep *net2280_match_ep(struct usb_gadget *_gadget,
1556 		struct usb_endpoint_descriptor *desc,
1557 		struct usb_ss_ep_comp_descriptor *ep_comp)
1558 {
1559 	char name[8];
1560 	struct usb_ep *ep;
1561 
1562 	if (usb_endpoint_type(desc) == USB_ENDPOINT_XFER_INT) {
1563 		/* ep-e, ep-f are PIO with only 64 byte fifos */
1564 		ep = gadget_find_ep_by_name(_gadget, "ep-e");
1565 		if (ep && usb_gadget_ep_match_desc(_gadget, ep, desc, ep_comp))
1566 			return ep;
1567 		ep = gadget_find_ep_by_name(_gadget, "ep-f");
1568 		if (ep && usb_gadget_ep_match_desc(_gadget, ep, desc, ep_comp))
1569 			return ep;
1570 	}
1571 
1572 	/* USB3380: Only first four endpoints have DMA channels. Allocate
1573 	 * slower interrupt endpoints from PIO hw endpoints, to allow bulk/isoc
1574 	 * endpoints use DMA hw endpoints.
1575 	 */
1576 	if (usb_endpoint_type(desc) == USB_ENDPOINT_XFER_INT &&
1577 	    usb_endpoint_dir_in(desc)) {
1578 		ep = gadget_find_ep_by_name(_gadget, "ep2in");
1579 		if (ep && usb_gadget_ep_match_desc(_gadget, ep, desc, ep_comp))
1580 			return ep;
1581 		ep = gadget_find_ep_by_name(_gadget, "ep4in");
1582 		if (ep && usb_gadget_ep_match_desc(_gadget, ep, desc, ep_comp))
1583 			return ep;
1584 	} else if (usb_endpoint_type(desc) == USB_ENDPOINT_XFER_INT &&
1585 		   !usb_endpoint_dir_in(desc)) {
1586 		ep = gadget_find_ep_by_name(_gadget, "ep1out");
1587 		if (ep && usb_gadget_ep_match_desc(_gadget, ep, desc, ep_comp))
1588 			return ep;
1589 		ep = gadget_find_ep_by_name(_gadget, "ep3out");
1590 		if (ep && usb_gadget_ep_match_desc(_gadget, ep, desc, ep_comp))
1591 			return ep;
1592 	} else if (usb_endpoint_type(desc) != USB_ENDPOINT_XFER_BULK &&
1593 		   usb_endpoint_dir_in(desc)) {
1594 		ep = gadget_find_ep_by_name(_gadget, "ep1in");
1595 		if (ep && usb_gadget_ep_match_desc(_gadget, ep, desc, ep_comp))
1596 			return ep;
1597 		ep = gadget_find_ep_by_name(_gadget, "ep3in");
1598 		if (ep && usb_gadget_ep_match_desc(_gadget, ep, desc, ep_comp))
1599 			return ep;
1600 	} else if (usb_endpoint_type(desc) != USB_ENDPOINT_XFER_BULK &&
1601 		   !usb_endpoint_dir_in(desc)) {
1602 		ep = gadget_find_ep_by_name(_gadget, "ep2out");
1603 		if (ep && usb_gadget_ep_match_desc(_gadget, ep, desc, ep_comp))
1604 			return ep;
1605 		ep = gadget_find_ep_by_name(_gadget, "ep4out");
1606 		if (ep && usb_gadget_ep_match_desc(_gadget, ep, desc, ep_comp))
1607 			return ep;
1608 	}
1609 
1610 	/* USB3380: use same address for usb and hardware endpoints */
1611 	snprintf(name, sizeof(name), "ep%d%s", usb_endpoint_num(desc),
1612 			usb_endpoint_dir_in(desc) ? "in" : "out");
1613 	ep = gadget_find_ep_by_name(_gadget, name);
1614 	if (ep && usb_gadget_ep_match_desc(_gadget, ep, desc, ep_comp))
1615 		return ep;
1616 
1617 	return NULL;
1618 }
1619 
1620 static int net2280_start(struct usb_gadget *_gadget,
1621 		struct usb_gadget_driver *driver);
1622 static int net2280_stop(struct usb_gadget *_gadget);
1623 static void net2280_async_callbacks(struct usb_gadget *_gadget, bool enable);
1624 
1625 static const struct usb_gadget_ops net2280_ops = {
1626 	.get_frame	= net2280_get_frame,
1627 	.wakeup		= net2280_wakeup,
1628 	.set_selfpowered = net2280_set_selfpowered,
1629 	.pullup		= net2280_pullup,
1630 	.udc_start	= net2280_start,
1631 	.udc_stop	= net2280_stop,
1632 	.udc_async_callbacks = net2280_async_callbacks,
1633 	.match_ep	= net2280_match_ep,
1634 };
1635 
1636 /*-------------------------------------------------------------------------*/
1637 
1638 #ifdef	CONFIG_USB_GADGET_DEBUG_FILES
1639 
1640 /* FIXME move these into procfs, and use seq_file.
1641  * Sysfs _still_ doesn't behave for arbitrarily sized files,
1642  * and also doesn't help products using this with 2.4 kernels.
1643  */
1644 
1645 /* "function" sysfs attribute */
1646 static ssize_t function_show(struct device *_dev, struct device_attribute *attr,
1647 			     char *buf)
1648 {
1649 	struct net2280	*dev = dev_get_drvdata(_dev);
1650 
1651 	if (!dev->driver || !dev->driver->function ||
1652 			strlen(dev->driver->function) > PAGE_SIZE)
1653 		return 0;
1654 	return scnprintf(buf, PAGE_SIZE, "%s\n", dev->driver->function);
1655 }
1656 static DEVICE_ATTR_RO(function);
1657 
1658 static ssize_t registers_show(struct device *_dev,
1659 			      struct device_attribute *attr, char *buf)
1660 {
1661 	struct net2280		*dev;
1662 	char			*next;
1663 	unsigned		size, t;
1664 	unsigned long		flags;
1665 	int			i;
1666 	u32			t1, t2;
1667 	const char		*s;
1668 
1669 	dev = dev_get_drvdata(_dev);
1670 	next = buf;
1671 	size = PAGE_SIZE;
1672 	spin_lock_irqsave(&dev->lock, flags);
1673 
1674 	if (dev->driver)
1675 		s = dev->driver->driver.name;
1676 	else
1677 		s = "(none)";
1678 
1679 	/* Main Control Registers */
1680 	t = scnprintf(next, size, "%s version " DRIVER_VERSION
1681 			", chiprev %04x\n\n"
1682 			"devinit %03x fifoctl %08x gadget '%s'\n"
1683 			"pci irqenb0 %02x irqenb1 %08x "
1684 			"irqstat0 %04x irqstat1 %08x\n",
1685 			driver_name, dev->chiprev,
1686 			readl(&dev->regs->devinit),
1687 			readl(&dev->regs->fifoctl),
1688 			s,
1689 			readl(&dev->regs->pciirqenb0),
1690 			readl(&dev->regs->pciirqenb1),
1691 			readl(&dev->regs->irqstat0),
1692 			readl(&dev->regs->irqstat1));
1693 	size -= t;
1694 	next += t;
1695 
1696 	/* USB Control Registers */
1697 	t1 = readl(&dev->usb->usbctl);
1698 	t2 = readl(&dev->usb->usbstat);
1699 	if (t1 & BIT(VBUS_PIN)) {
1700 		if (t2 & BIT(HIGH_SPEED))
1701 			s = "high speed";
1702 		else if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1703 			s = "powered";
1704 		else
1705 			s = "full speed";
1706 		/* full speed bit (6) not working?? */
1707 	} else
1708 			s = "not attached";
1709 	t = scnprintf(next, size,
1710 			"stdrsp %08x usbctl %08x usbstat %08x "
1711 				"addr 0x%02x (%s)\n",
1712 			readl(&dev->usb->stdrsp), t1, t2,
1713 			readl(&dev->usb->ouraddr), s);
1714 	size -= t;
1715 	next += t;
1716 
1717 	/* PCI Master Control Registers */
1718 
1719 	/* DMA Control Registers */
1720 
1721 	/* Configurable EP Control Registers */
1722 	for (i = 0; i < dev->n_ep; i++) {
1723 		struct net2280_ep	*ep;
1724 
1725 		ep = &dev->ep[i];
1726 		if (i && !ep->desc)
1727 			continue;
1728 
1729 		t1 = readl(&ep->cfg->ep_cfg);
1730 		t2 = readl(&ep->regs->ep_rsp) & 0xff;
1731 		t = scnprintf(next, size,
1732 				"\n%s\tcfg %05x rsp (%02x) %s%s%s%s%s%s%s%s"
1733 					"irqenb %02x\n",
1734 				ep->ep.name, t1, t2,
1735 				(t2 & BIT(CLEAR_NAK_OUT_PACKETS))
1736 					? "NAK " : "",
1737 				(t2 & BIT(CLEAR_EP_HIDE_STATUS_PHASE))
1738 					? "hide " : "",
1739 				(t2 & BIT(CLEAR_EP_FORCE_CRC_ERROR))
1740 					? "CRC " : "",
1741 				(t2 & BIT(CLEAR_INTERRUPT_MODE))
1742 					? "interrupt " : "",
1743 				(t2 & BIT(CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE))
1744 					? "status " : "",
1745 				(t2 & BIT(CLEAR_NAK_OUT_PACKETS_MODE))
1746 					? "NAKmode " : "",
1747 				(t2 & BIT(CLEAR_ENDPOINT_TOGGLE))
1748 					? "DATA1 " : "DATA0 ",
1749 				(t2 & BIT(CLEAR_ENDPOINT_HALT))
1750 					? "HALT " : "",
1751 				readl(&ep->regs->ep_irqenb));
1752 		size -= t;
1753 		next += t;
1754 
1755 		t = scnprintf(next, size,
1756 				"\tstat %08x avail %04x "
1757 				"(ep%d%s-%s)%s\n",
1758 				readl(&ep->regs->ep_stat),
1759 				readl(&ep->regs->ep_avail),
1760 				t1 & 0x0f, DIR_STRING(t1),
1761 				type_string(t1 >> 8),
1762 				ep->stopped ? "*" : "");
1763 		size -= t;
1764 		next += t;
1765 
1766 		if (!ep->dma)
1767 			continue;
1768 
1769 		t = scnprintf(next, size,
1770 				"  dma\tctl %08x stat %08x count %08x\n"
1771 				"\taddr %08x desc %08x\n",
1772 				readl(&ep->dma->dmactl),
1773 				readl(&ep->dma->dmastat),
1774 				readl(&ep->dma->dmacount),
1775 				readl(&ep->dma->dmaaddr),
1776 				readl(&ep->dma->dmadesc));
1777 		size -= t;
1778 		next += t;
1779 
1780 	}
1781 
1782 	/* Indexed Registers (none yet) */
1783 
1784 	/* Statistics */
1785 	t = scnprintf(next, size, "\nirqs:  ");
1786 	size -= t;
1787 	next += t;
1788 	for (i = 0; i < dev->n_ep; i++) {
1789 		struct net2280_ep	*ep;
1790 
1791 		ep = &dev->ep[i];
1792 		if (i && !ep->irqs)
1793 			continue;
1794 		t = scnprintf(next, size, " %s/%lu", ep->ep.name, ep->irqs);
1795 		size -= t;
1796 		next += t;
1797 
1798 	}
1799 	t = scnprintf(next, size, "\n");
1800 	size -= t;
1801 	next += t;
1802 
1803 	spin_unlock_irqrestore(&dev->lock, flags);
1804 
1805 	return PAGE_SIZE - size;
1806 }
1807 static DEVICE_ATTR_RO(registers);
1808 
1809 static ssize_t queues_show(struct device *_dev, struct device_attribute *attr,
1810 			   char *buf)
1811 {
1812 	struct net2280		*dev;
1813 	char			*next;
1814 	unsigned		size;
1815 	unsigned long		flags;
1816 	int			i;
1817 
1818 	dev = dev_get_drvdata(_dev);
1819 	next = buf;
1820 	size = PAGE_SIZE;
1821 	spin_lock_irqsave(&dev->lock, flags);
1822 
1823 	for (i = 0; i < dev->n_ep; i++) {
1824 		struct net2280_ep		*ep = &dev->ep[i];
1825 		struct net2280_request		*req;
1826 		int				t;
1827 
1828 		if (i != 0) {
1829 			const struct usb_endpoint_descriptor	*d;
1830 
1831 			d = ep->desc;
1832 			if (!d)
1833 				continue;
1834 			t = d->bEndpointAddress;
1835 			t = scnprintf(next, size,
1836 				"\n%s (ep%d%s-%s) max %04x %s fifo %d\n",
1837 				ep->ep.name, t & USB_ENDPOINT_NUMBER_MASK,
1838 				(t & USB_DIR_IN) ? "in" : "out",
1839 				type_string(d->bmAttributes),
1840 				usb_endpoint_maxp(d),
1841 				ep->dma ? "dma" : "pio", ep->fifo_size
1842 				);
1843 		} else /* ep0 should only have one transfer queued */
1844 			t = scnprintf(next, size, "ep0 max 64 pio %s\n",
1845 					ep->is_in ? "in" : "out");
1846 		if (t <= 0 || t > size)
1847 			goto done;
1848 		size -= t;
1849 		next += t;
1850 
1851 		if (list_empty(&ep->queue)) {
1852 			t = scnprintf(next, size, "\t(nothing queued)\n");
1853 			if (t <= 0 || t > size)
1854 				goto done;
1855 			size -= t;
1856 			next += t;
1857 			continue;
1858 		}
1859 		list_for_each_entry(req, &ep->queue, queue) {
1860 			if (ep->dma && req->td_dma == readl(&ep->dma->dmadesc))
1861 				t = scnprintf(next, size,
1862 					"\treq %p len %d/%d "
1863 					"buf %p (dmacount %08x)\n",
1864 					&req->req, req->req.actual,
1865 					req->req.length, req->req.buf,
1866 					readl(&ep->dma->dmacount));
1867 			else
1868 				t = scnprintf(next, size,
1869 					"\treq %p len %d/%d buf %p\n",
1870 					&req->req, req->req.actual,
1871 					req->req.length, req->req.buf);
1872 			if (t <= 0 || t > size)
1873 				goto done;
1874 			size -= t;
1875 			next += t;
1876 
1877 			if (ep->dma) {
1878 				struct net2280_dma	*td;
1879 
1880 				td = req->td;
1881 				t = scnprintf(next, size, "\t    td %08x "
1882 					" count %08x buf %08x desc %08x\n",
1883 					(u32) req->td_dma,
1884 					le32_to_cpu(td->dmacount),
1885 					le32_to_cpu(td->dmaaddr),
1886 					le32_to_cpu(td->dmadesc));
1887 				if (t <= 0 || t > size)
1888 					goto done;
1889 				size -= t;
1890 				next += t;
1891 			}
1892 		}
1893 	}
1894 
1895 done:
1896 	spin_unlock_irqrestore(&dev->lock, flags);
1897 	return PAGE_SIZE - size;
1898 }
1899 static DEVICE_ATTR_RO(queues);
1900 
1901 
1902 #else
1903 
1904 #define device_create_file(a, b)	(0)
1905 #define device_remove_file(a, b)	do { } while (0)
1906 
1907 #endif
1908 
1909 /*-------------------------------------------------------------------------*/
1910 
1911 /* another driver-specific mode might be a request type doing dma
1912  * to/from another device fifo instead of to/from memory.
1913  */
1914 
1915 static void set_fifo_mode(struct net2280 *dev, int mode)
1916 {
1917 	/* keeping high bits preserves BAR2 */
1918 	writel((0xffff << PCI_BASE2_RANGE) | mode, &dev->regs->fifoctl);
1919 
1920 	/* always ep-{a,b,e,f} ... maybe not ep-c or ep-d */
1921 	INIT_LIST_HEAD(&dev->gadget.ep_list);
1922 	list_add_tail(&dev->ep[1].ep.ep_list, &dev->gadget.ep_list);
1923 	list_add_tail(&dev->ep[2].ep.ep_list, &dev->gadget.ep_list);
1924 	switch (mode) {
1925 	case 0:
1926 		list_add_tail(&dev->ep[3].ep.ep_list, &dev->gadget.ep_list);
1927 		list_add_tail(&dev->ep[4].ep.ep_list, &dev->gadget.ep_list);
1928 		dev->ep[1].fifo_size = dev->ep[2].fifo_size = 1024;
1929 		break;
1930 	case 1:
1931 		dev->ep[1].fifo_size = dev->ep[2].fifo_size = 2048;
1932 		break;
1933 	case 2:
1934 		list_add_tail(&dev->ep[3].ep.ep_list, &dev->gadget.ep_list);
1935 		dev->ep[1].fifo_size = 2048;
1936 		dev->ep[2].fifo_size = 1024;
1937 		break;
1938 	}
1939 	/* fifo sizes for ep0, ep-c, ep-d, ep-e, and ep-f never change */
1940 	list_add_tail(&dev->ep[5].ep.ep_list, &dev->gadget.ep_list);
1941 	list_add_tail(&dev->ep[6].ep.ep_list, &dev->gadget.ep_list);
1942 }
1943 
1944 static void defect7374_disable_data_eps(struct net2280 *dev)
1945 {
1946 	/*
1947 	 * For Defect 7374, disable data EPs (and more):
1948 	 *  - This phase undoes the earlier phase of the Defect 7374 workaround,
1949 	 *    returing ep regs back to normal.
1950 	 */
1951 	struct net2280_ep *ep;
1952 	int i;
1953 	unsigned char ep_sel;
1954 	u32 tmp_reg;
1955 
1956 	for (i = 1; i < 5; i++) {
1957 		ep = &dev->ep[i];
1958 		writel(i, &ep->cfg->ep_cfg);
1959 	}
1960 
1961 	/* CSROUT, CSRIN, PCIOUT, PCIIN, STATIN, RCIN */
1962 	for (i = 0; i < 6; i++)
1963 		writel(0, &dev->dep[i].dep_cfg);
1964 
1965 	for (ep_sel = 0; ep_sel <= 21; ep_sel++) {
1966 		/* Select an endpoint for subsequent operations: */
1967 		tmp_reg = readl(&dev->plregs->pl_ep_ctrl);
1968 		writel(((tmp_reg & ~0x1f) | ep_sel), &dev->plregs->pl_ep_ctrl);
1969 
1970 		if (ep_sel < 2 || (ep_sel > 9 && ep_sel < 14) ||
1971 					ep_sel == 18 || ep_sel == 20)
1972 			continue;
1973 
1974 		/* Change settings on some selected endpoints */
1975 		tmp_reg = readl(&dev->plregs->pl_ep_cfg_4);
1976 		tmp_reg &= ~BIT(NON_CTRL_IN_TOLERATE_BAD_DIR);
1977 		writel(tmp_reg, &dev->plregs->pl_ep_cfg_4);
1978 		tmp_reg = readl(&dev->plregs->pl_ep_ctrl);
1979 		tmp_reg |= BIT(EP_INITIALIZED);
1980 		writel(tmp_reg, &dev->plregs->pl_ep_ctrl);
1981 	}
1982 }
1983 
1984 static void defect7374_enable_data_eps_zero(struct net2280 *dev)
1985 {
1986 	u32 tmp = 0, tmp_reg;
1987 	u32 scratch;
1988 	int i;
1989 	unsigned char ep_sel;
1990 
1991 	scratch = get_idx_reg(dev->regs, SCRATCH);
1992 
1993 	WARN_ON((scratch & (0xf << DEFECT7374_FSM_FIELD))
1994 		== DEFECT7374_FSM_SS_CONTROL_READ);
1995 
1996 	scratch &= ~(0xf << DEFECT7374_FSM_FIELD);
1997 
1998 	ep_warn(dev, "Operate Defect 7374 workaround soft this time");
1999 	ep_warn(dev, "It will operate on cold-reboot and SS connect");
2000 
2001 	/*GPEPs:*/
2002 	tmp = ((0 << ENDPOINT_NUMBER) | BIT(ENDPOINT_DIRECTION) |
2003 			(2 << OUT_ENDPOINT_TYPE) | (2 << IN_ENDPOINT_TYPE) |
2004 			((dev->enhanced_mode) ?
2005 			 BIT(OUT_ENDPOINT_ENABLE) | BIT(IN_ENDPOINT_ENABLE) :
2006 			 BIT(ENDPOINT_ENABLE)));
2007 
2008 	for (i = 1; i < 5; i++)
2009 		writel(tmp, &dev->ep[i].cfg->ep_cfg);
2010 
2011 	/* CSRIN, PCIIN, STATIN, RCIN*/
2012 	tmp = ((0 << ENDPOINT_NUMBER) | BIT(ENDPOINT_ENABLE));
2013 	writel(tmp, &dev->dep[1].dep_cfg);
2014 	writel(tmp, &dev->dep[3].dep_cfg);
2015 	writel(tmp, &dev->dep[4].dep_cfg);
2016 	writel(tmp, &dev->dep[5].dep_cfg);
2017 
2018 	/*Implemented for development and debug.
2019 	 * Can be refined/tuned later.*/
2020 	for (ep_sel = 0; ep_sel <= 21; ep_sel++) {
2021 		/* Select an endpoint for subsequent operations: */
2022 		tmp_reg = readl(&dev->plregs->pl_ep_ctrl);
2023 		writel(((tmp_reg & ~0x1f) | ep_sel),
2024 				&dev->plregs->pl_ep_ctrl);
2025 
2026 		if (ep_sel == 1) {
2027 			tmp =
2028 				(readl(&dev->plregs->pl_ep_ctrl) |
2029 				 BIT(CLEAR_ACK_ERROR_CODE) | 0);
2030 			writel(tmp, &dev->plregs->pl_ep_ctrl);
2031 			continue;
2032 		}
2033 
2034 		if (ep_sel == 0 || (ep_sel > 9 && ep_sel < 14) ||
2035 				ep_sel == 18  || ep_sel == 20)
2036 			continue;
2037 
2038 		tmp = (readl(&dev->plregs->pl_ep_cfg_4) |
2039 				BIT(NON_CTRL_IN_TOLERATE_BAD_DIR) | 0);
2040 		writel(tmp, &dev->plregs->pl_ep_cfg_4);
2041 
2042 		tmp = readl(&dev->plregs->pl_ep_ctrl) &
2043 			~BIT(EP_INITIALIZED);
2044 		writel(tmp, &dev->plregs->pl_ep_ctrl);
2045 
2046 	}
2047 
2048 	/* Set FSM to focus on the first Control Read:
2049 	 * - Tip: Connection speed is known upon the first
2050 	 * setup request.*/
2051 	scratch |= DEFECT7374_FSM_WAITING_FOR_CONTROL_READ;
2052 	set_idx_reg(dev->regs, SCRATCH, scratch);
2053 
2054 }
2055 
2056 /* keeping it simple:
2057  * - one bus driver, initted first;
2058  * - one function driver, initted second
2059  *
2060  * most of the work to support multiple net2280 controllers would
2061  * be to associate this gadget driver (yes?) with all of them, or
2062  * perhaps to bind specific drivers to specific devices.
2063  */
2064 
2065 static void usb_reset_228x(struct net2280 *dev)
2066 {
2067 	u32	tmp;
2068 
2069 	dev->gadget.speed = USB_SPEED_UNKNOWN;
2070 	(void) readl(&dev->usb->usbctl);
2071 
2072 	net2280_led_init(dev);
2073 
2074 	/* disable automatic responses, and irqs */
2075 	writel(0, &dev->usb->stdrsp);
2076 	writel(0, &dev->regs->pciirqenb0);
2077 	writel(0, &dev->regs->pciirqenb1);
2078 
2079 	/* clear old dma and irq state */
2080 	for (tmp = 0; tmp < 4; tmp++) {
2081 		struct net2280_ep       *ep = &dev->ep[tmp + 1];
2082 		if (ep->dma)
2083 			abort_dma(ep);
2084 	}
2085 
2086 	writel(~0, &dev->regs->irqstat0),
2087 	writel(~(u32)BIT(SUSPEND_REQUEST_INTERRUPT), &dev->regs->irqstat1),
2088 
2089 	/* reset, and enable pci */
2090 	tmp = readl(&dev->regs->devinit) |
2091 		BIT(PCI_ENABLE) |
2092 		BIT(FIFO_SOFT_RESET) |
2093 		BIT(USB_SOFT_RESET) |
2094 		BIT(M8051_RESET);
2095 	writel(tmp, &dev->regs->devinit);
2096 
2097 	/* standard fifo and endpoint allocations */
2098 	set_fifo_mode(dev, (fifo_mode <= 2) ? fifo_mode : 0);
2099 }
2100 
2101 static void usb_reset_338x(struct net2280 *dev)
2102 {
2103 	u32 tmp;
2104 
2105 	dev->gadget.speed = USB_SPEED_UNKNOWN;
2106 	(void)readl(&dev->usb->usbctl);
2107 
2108 	net2280_led_init(dev);
2109 
2110 	if (dev->bug7734_patched) {
2111 		/* disable automatic responses, and irqs */
2112 		writel(0, &dev->usb->stdrsp);
2113 		writel(0, &dev->regs->pciirqenb0);
2114 		writel(0, &dev->regs->pciirqenb1);
2115 	}
2116 
2117 	/* clear old dma and irq state */
2118 	for (tmp = 0; tmp < 4; tmp++) {
2119 		struct net2280_ep *ep = &dev->ep[tmp + 1];
2120 		struct net2280_dma_regs __iomem *dma;
2121 
2122 		if (ep->dma) {
2123 			abort_dma(ep);
2124 		} else {
2125 			dma = &dev->dma[tmp];
2126 			writel(BIT(DMA_ABORT), &dma->dmastat);
2127 			writel(0, &dma->dmactl);
2128 		}
2129 	}
2130 
2131 	writel(~0, &dev->regs->irqstat0), writel(~0, &dev->regs->irqstat1);
2132 
2133 	if (dev->bug7734_patched) {
2134 		/* reset, and enable pci */
2135 		tmp = readl(&dev->regs->devinit) |
2136 		    BIT(PCI_ENABLE) |
2137 		    BIT(FIFO_SOFT_RESET) |
2138 		    BIT(USB_SOFT_RESET) |
2139 		    BIT(M8051_RESET);
2140 
2141 		writel(tmp, &dev->regs->devinit);
2142 	}
2143 
2144 	/* always ep-{1,2,3,4} ... maybe not ep-3 or ep-4 */
2145 	INIT_LIST_HEAD(&dev->gadget.ep_list);
2146 
2147 	for (tmp = 1; tmp < dev->n_ep; tmp++)
2148 		list_add_tail(&dev->ep[tmp].ep.ep_list, &dev->gadget.ep_list);
2149 
2150 }
2151 
2152 static void usb_reset(struct net2280 *dev)
2153 {
2154 	if (dev->quirks & PLX_LEGACY)
2155 		return usb_reset_228x(dev);
2156 	return usb_reset_338x(dev);
2157 }
2158 
2159 static void usb_reinit_228x(struct net2280 *dev)
2160 {
2161 	u32	tmp;
2162 
2163 	/* basic endpoint init */
2164 	for (tmp = 0; tmp < 7; tmp++) {
2165 		struct net2280_ep	*ep = &dev->ep[tmp];
2166 
2167 		ep->ep.name = ep_info_dft[tmp].name;
2168 		ep->ep.caps = ep_info_dft[tmp].caps;
2169 		ep->dev = dev;
2170 		ep->num = tmp;
2171 
2172 		if (tmp > 0 && tmp <= 4) {
2173 			ep->fifo_size = 1024;
2174 			ep->dma = &dev->dma[tmp - 1];
2175 		} else
2176 			ep->fifo_size = 64;
2177 		ep->regs = &dev->epregs[tmp];
2178 		ep->cfg = &dev->epregs[tmp];
2179 		ep_reset_228x(dev->regs, ep);
2180 	}
2181 	usb_ep_set_maxpacket_limit(&dev->ep[0].ep, 64);
2182 	usb_ep_set_maxpacket_limit(&dev->ep[5].ep, 64);
2183 	usb_ep_set_maxpacket_limit(&dev->ep[6].ep, 64);
2184 
2185 	dev->gadget.ep0 = &dev->ep[0].ep;
2186 	dev->ep[0].stopped = 0;
2187 	INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
2188 
2189 	/* we want to prevent lowlevel/insecure access from the USB host,
2190 	 * but erratum 0119 means this enable bit is ignored
2191 	 */
2192 	for (tmp = 0; tmp < 5; tmp++)
2193 		writel(EP_DONTUSE, &dev->dep[tmp].dep_cfg);
2194 }
2195 
2196 static void usb_reinit_338x(struct net2280 *dev)
2197 {
2198 	int i;
2199 	u32 tmp, val;
2200 	static const u32 ne[9] = { 0, 1, 2, 3, 4, 1, 2, 3, 4 };
2201 	static const u32 ep_reg_addr[9] = { 0x00, 0xC0, 0x00, 0xC0, 0x00,
2202 						0x00, 0xC0, 0x00, 0xC0 };
2203 
2204 	/* basic endpoint init */
2205 	for (i = 0; i < dev->n_ep; i++) {
2206 		struct net2280_ep *ep = &dev->ep[i];
2207 
2208 		ep->ep.name = dev->enhanced_mode ? ep_info_adv[i].name :
2209 						   ep_info_dft[i].name;
2210 		ep->ep.caps = dev->enhanced_mode ? ep_info_adv[i].caps :
2211 						   ep_info_dft[i].caps;
2212 		ep->dev = dev;
2213 		ep->num = i;
2214 
2215 		if (i > 0 && i <= 4)
2216 			ep->dma = &dev->dma[i - 1];
2217 
2218 		if (dev->enhanced_mode) {
2219 			ep->cfg = &dev->epregs[ne[i]];
2220 			/*
2221 			 * Set USB endpoint number, hardware allows same number
2222 			 * in both directions.
2223 			 */
2224 			 if (i > 0 && i < 5)
2225 				writel(ne[i], &ep->cfg->ep_cfg);
2226 			ep->regs = (struct net2280_ep_regs __iomem *)
2227 				(((void __iomem *)&dev->epregs[ne[i]]) +
2228 				ep_reg_addr[i]);
2229 		} else {
2230 			ep->cfg = &dev->epregs[i];
2231 			ep->regs = &dev->epregs[i];
2232 		}
2233 
2234 		ep->fifo_size = (i != 0) ? 2048 : 512;
2235 
2236 		ep_reset_338x(dev->regs, ep);
2237 	}
2238 	usb_ep_set_maxpacket_limit(&dev->ep[0].ep, 512);
2239 
2240 	dev->gadget.ep0 = &dev->ep[0].ep;
2241 	dev->ep[0].stopped = 0;
2242 
2243 	/* Link layer set up */
2244 	if (dev->bug7734_patched) {
2245 		tmp = readl(&dev->usb_ext->usbctl2) &
2246 		    ~(BIT(U1_ENABLE) | BIT(U2_ENABLE) | BIT(LTM_ENABLE));
2247 		writel(tmp, &dev->usb_ext->usbctl2);
2248 	}
2249 
2250 	/* Hardware Defect and Workaround */
2251 	val = readl(&dev->llregs->ll_lfps_5);
2252 	val &= ~(0xf << TIMER_LFPS_6US);
2253 	val |= 0x5 << TIMER_LFPS_6US;
2254 	writel(val, &dev->llregs->ll_lfps_5);
2255 
2256 	val = readl(&dev->llregs->ll_lfps_6);
2257 	val &= ~(0xffff << TIMER_LFPS_80US);
2258 	val |= 0x0100 << TIMER_LFPS_80US;
2259 	writel(val, &dev->llregs->ll_lfps_6);
2260 
2261 	/*
2262 	 * AA_AB Errata. Issue 4. Workaround for SuperSpeed USB
2263 	 * Hot Reset Exit Handshake may Fail in Specific Case using
2264 	 * Default Register Settings. Workaround for Enumeration test.
2265 	 */
2266 	val = readl(&dev->llregs->ll_tsn_counters_2);
2267 	val &= ~(0x1f << HOT_TX_NORESET_TS2);
2268 	val |= 0x10 << HOT_TX_NORESET_TS2;
2269 	writel(val, &dev->llregs->ll_tsn_counters_2);
2270 
2271 	val = readl(&dev->llregs->ll_tsn_counters_3);
2272 	val &= ~(0x1f << HOT_RX_RESET_TS2);
2273 	val |= 0x3 << HOT_RX_RESET_TS2;
2274 	writel(val, &dev->llregs->ll_tsn_counters_3);
2275 
2276 	/*
2277 	 * AB errata. Errata 11. Workaround for Default Duration of LFPS
2278 	 * Handshake Signaling for Device-Initiated U1 Exit is too short.
2279 	 * Without this, various enumeration failures observed with
2280 	 * modern superspeed hosts.
2281 	 */
2282 	val = readl(&dev->llregs->ll_lfps_timers_2);
2283 	writel((val & 0xffff0000) | LFPS_TIMERS_2_WORKAROUND_VALUE,
2284 	       &dev->llregs->ll_lfps_timers_2);
2285 
2286 	/*
2287 	 * Set Recovery Idle to Recover bit:
2288 	 * - On SS connections, setting Recovery Idle to Recover Fmw improves
2289 	 *   link robustness with various hosts and hubs.
2290 	 * - It is safe to set for all connection speeds; all chip revisions.
2291 	 * - R-M-W to leave other bits undisturbed.
2292 	 * - Reference PLX TT-7372
2293 	*/
2294 	val = readl(&dev->llregs->ll_tsn_chicken_bit);
2295 	val |= BIT(RECOVERY_IDLE_TO_RECOVER_FMW);
2296 	writel(val, &dev->llregs->ll_tsn_chicken_bit);
2297 
2298 	INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
2299 
2300 	/* disable dedicated endpoints */
2301 	writel(0x0D, &dev->dep[0].dep_cfg);
2302 	writel(0x0D, &dev->dep[1].dep_cfg);
2303 	writel(0x0E, &dev->dep[2].dep_cfg);
2304 	writel(0x0E, &dev->dep[3].dep_cfg);
2305 	writel(0x0F, &dev->dep[4].dep_cfg);
2306 	writel(0x0C, &dev->dep[5].dep_cfg);
2307 }
2308 
2309 static void usb_reinit(struct net2280 *dev)
2310 {
2311 	if (dev->quirks & PLX_LEGACY)
2312 		return usb_reinit_228x(dev);
2313 	return usb_reinit_338x(dev);
2314 }
2315 
2316 static void ep0_start_228x(struct net2280 *dev)
2317 {
2318 	writel(BIT(CLEAR_EP_HIDE_STATUS_PHASE) |
2319 		BIT(CLEAR_NAK_OUT_PACKETS) |
2320 		BIT(CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE),
2321 		&dev->epregs[0].ep_rsp);
2322 
2323 	/*
2324 	 * hardware optionally handles a bunch of standard requests
2325 	 * that the API hides from drivers anyway.  have it do so.
2326 	 * endpoint status/features are handled in software, to
2327 	 * help pass tests for some dubious behavior.
2328 	 */
2329 	writel(BIT(SET_TEST_MODE) |
2330 		BIT(SET_ADDRESS) |
2331 		BIT(DEVICE_SET_CLEAR_DEVICE_REMOTE_WAKEUP) |
2332 		BIT(GET_DEVICE_STATUS) |
2333 		BIT(GET_INTERFACE_STATUS),
2334 		&dev->usb->stdrsp);
2335 	writel(BIT(USB_ROOT_PORT_WAKEUP_ENABLE) |
2336 		BIT(SELF_POWERED_USB_DEVICE) |
2337 		BIT(REMOTE_WAKEUP_SUPPORT) |
2338 		(dev->softconnect << USB_DETECT_ENABLE) |
2339 		BIT(SELF_POWERED_STATUS),
2340 		&dev->usb->usbctl);
2341 
2342 	/* enable irqs so we can see ep0 and general operation  */
2343 	writel(BIT(SETUP_PACKET_INTERRUPT_ENABLE) |
2344 		BIT(ENDPOINT_0_INTERRUPT_ENABLE),
2345 		&dev->regs->pciirqenb0);
2346 	writel(BIT(PCI_INTERRUPT_ENABLE) |
2347 		BIT(PCI_MASTER_ABORT_RECEIVED_INTERRUPT_ENABLE) |
2348 		BIT(PCI_TARGET_ABORT_RECEIVED_INTERRUPT_ENABLE) |
2349 		BIT(PCI_RETRY_ABORT_INTERRUPT_ENABLE) |
2350 		BIT(VBUS_INTERRUPT_ENABLE) |
2351 		BIT(ROOT_PORT_RESET_INTERRUPT_ENABLE) |
2352 		BIT(SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE),
2353 		&dev->regs->pciirqenb1);
2354 
2355 	/* don't leave any writes posted */
2356 	(void) readl(&dev->usb->usbctl);
2357 }
2358 
2359 static void ep0_start_338x(struct net2280 *dev)
2360 {
2361 
2362 	if (dev->bug7734_patched)
2363 		writel(BIT(CLEAR_NAK_OUT_PACKETS_MODE) |
2364 		       BIT(SET_EP_HIDE_STATUS_PHASE),
2365 		       &dev->epregs[0].ep_rsp);
2366 
2367 	/*
2368 	 * hardware optionally handles a bunch of standard requests
2369 	 * that the API hides from drivers anyway.  have it do so.
2370 	 * endpoint status/features are handled in software, to
2371 	 * help pass tests for some dubious behavior.
2372 	 */
2373 	writel(BIT(SET_ISOCHRONOUS_DELAY) |
2374 	       BIT(SET_SEL) |
2375 	       BIT(SET_TEST_MODE) |
2376 	       BIT(SET_ADDRESS) |
2377 	       BIT(GET_INTERFACE_STATUS) |
2378 	       BIT(GET_DEVICE_STATUS),
2379 		&dev->usb->stdrsp);
2380 	dev->wakeup_enable = 1;
2381 	writel(BIT(USB_ROOT_PORT_WAKEUP_ENABLE) |
2382 	       (dev->softconnect << USB_DETECT_ENABLE) |
2383 	       BIT(DEVICE_REMOTE_WAKEUP_ENABLE),
2384 	       &dev->usb->usbctl);
2385 
2386 	/* enable irqs so we can see ep0 and general operation  */
2387 	writel(BIT(SETUP_PACKET_INTERRUPT_ENABLE) |
2388 	       BIT(ENDPOINT_0_INTERRUPT_ENABLE),
2389 	       &dev->regs->pciirqenb0);
2390 	writel(BIT(PCI_INTERRUPT_ENABLE) |
2391 	       BIT(ROOT_PORT_RESET_INTERRUPT_ENABLE) |
2392 	       BIT(SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE) |
2393 	       BIT(VBUS_INTERRUPT_ENABLE),
2394 	       &dev->regs->pciirqenb1);
2395 
2396 	/* don't leave any writes posted */
2397 	(void)readl(&dev->usb->usbctl);
2398 }
2399 
2400 static void ep0_start(struct net2280 *dev)
2401 {
2402 	if (dev->quirks & PLX_LEGACY)
2403 		return ep0_start_228x(dev);
2404 	return ep0_start_338x(dev);
2405 }
2406 
2407 /* when a driver is successfully registered, it will receive
2408  * control requests including set_configuration(), which enables
2409  * non-control requests.  then usb traffic follows until a
2410  * disconnect is reported.  then a host may connect again, or
2411  * the driver might get unbound.
2412  */
2413 static int net2280_start(struct usb_gadget *_gadget,
2414 		struct usb_gadget_driver *driver)
2415 {
2416 	struct net2280		*dev;
2417 	int			retval;
2418 	unsigned		i;
2419 
2420 	/* insist on high speed support from the driver, since
2421 	 * (dev->usb->xcvrdiag & FORCE_FULL_SPEED_MODE)
2422 	 * "must not be used in normal operation"
2423 	 */
2424 	if (!driver || driver->max_speed < USB_SPEED_HIGH ||
2425 			!driver->setup)
2426 		return -EINVAL;
2427 
2428 	dev = container_of(_gadget, struct net2280, gadget);
2429 
2430 	for (i = 0; i < dev->n_ep; i++)
2431 		dev->ep[i].irqs = 0;
2432 
2433 	/* hook up the driver ... */
2434 	driver->driver.bus = NULL;
2435 	dev->driver = driver;
2436 
2437 	retval = device_create_file(&dev->pdev->dev, &dev_attr_function);
2438 	if (retval)
2439 		goto err_unbind;
2440 	retval = device_create_file(&dev->pdev->dev, &dev_attr_queues);
2441 	if (retval)
2442 		goto err_func;
2443 
2444 	/* enable host detection and ep0; and we're ready
2445 	 * for set_configuration as well as eventual disconnect.
2446 	 */
2447 	net2280_led_active(dev, 1);
2448 
2449 	if ((dev->quirks & PLX_PCIE) && !dev->bug7734_patched)
2450 		defect7374_enable_data_eps_zero(dev);
2451 
2452 	ep0_start(dev);
2453 
2454 	/* pci writes may still be posted */
2455 	return 0;
2456 
2457 err_func:
2458 	device_remove_file(&dev->pdev->dev, &dev_attr_function);
2459 err_unbind:
2460 	dev->driver = NULL;
2461 	return retval;
2462 }
2463 
2464 static void stop_activity(struct net2280 *dev, struct usb_gadget_driver *driver)
2465 {
2466 	int			i;
2467 
2468 	/* don't disconnect if it's not connected */
2469 	if (dev->gadget.speed == USB_SPEED_UNKNOWN)
2470 		driver = NULL;
2471 
2472 	/* stop hardware; prevent new request submissions;
2473 	 * and kill any outstanding requests.
2474 	 */
2475 	usb_reset(dev);
2476 	for (i = 0; i < dev->n_ep; i++)
2477 		nuke(&dev->ep[i]);
2478 
2479 	/* report disconnect; the driver is already quiesced */
2480 	if (dev->async_callbacks && driver) {
2481 		spin_unlock(&dev->lock);
2482 		driver->disconnect(&dev->gadget);
2483 		spin_lock(&dev->lock);
2484 	}
2485 
2486 	usb_reinit(dev);
2487 }
2488 
2489 static int net2280_stop(struct usb_gadget *_gadget)
2490 {
2491 	struct net2280	*dev;
2492 	unsigned long	flags;
2493 
2494 	dev = container_of(_gadget, struct net2280, gadget);
2495 
2496 	spin_lock_irqsave(&dev->lock, flags);
2497 	stop_activity(dev, NULL);
2498 	spin_unlock_irqrestore(&dev->lock, flags);
2499 
2500 	net2280_led_active(dev, 0);
2501 
2502 	device_remove_file(&dev->pdev->dev, &dev_attr_function);
2503 	device_remove_file(&dev->pdev->dev, &dev_attr_queues);
2504 
2505 	dev->driver = NULL;
2506 
2507 	return 0;
2508 }
2509 
2510 static void net2280_async_callbacks(struct usb_gadget *_gadget, bool enable)
2511 {
2512 	struct net2280	*dev = container_of(_gadget, struct net2280, gadget);
2513 
2514 	spin_lock_irq(&dev->lock);
2515 	dev->async_callbacks = enable;
2516 	spin_unlock_irq(&dev->lock);
2517 }
2518 
2519 /*-------------------------------------------------------------------------*/
2520 
2521 /* handle ep0, ep-e, ep-f with 64 byte packets: packet per irq.
2522  * also works for dma-capable endpoints, in pio mode or just
2523  * to manually advance the queue after short OUT transfers.
2524  */
2525 static void handle_ep_small(struct net2280_ep *ep)
2526 {
2527 	struct net2280_request	*req;
2528 	u32			t;
2529 	/* 0 error, 1 mid-data, 2 done */
2530 	int			mode = 1;
2531 
2532 	if (!list_empty(&ep->queue))
2533 		req = list_entry(ep->queue.next,
2534 			struct net2280_request, queue);
2535 	else
2536 		req = NULL;
2537 
2538 	/* ack all, and handle what we care about */
2539 	t = readl(&ep->regs->ep_stat);
2540 	ep->irqs++;
2541 
2542 	ep_vdbg(ep->dev, "%s ack ep_stat %08x, req %p\n",
2543 			ep->ep.name, t, req ? &req->req : NULL);
2544 
2545 	if (!ep->is_in || (ep->dev->quirks & PLX_2280))
2546 		writel(t & ~BIT(NAK_OUT_PACKETS), &ep->regs->ep_stat);
2547 	else
2548 		/* Added for 2282 */
2549 		writel(t, &ep->regs->ep_stat);
2550 
2551 	/* for ep0, monitor token irqs to catch data stage length errors
2552 	 * and to synchronize on status.
2553 	 *
2554 	 * also, to defer reporting of protocol stalls ... here's where
2555 	 * data or status first appears, handling stalls here should never
2556 	 * cause trouble on the host side..
2557 	 *
2558 	 * control requests could be slightly faster without token synch for
2559 	 * status, but status can jam up that way.
2560 	 */
2561 	if (unlikely(ep->num == 0)) {
2562 		if (ep->is_in) {
2563 			/* status; stop NAKing */
2564 			if (t & BIT(DATA_OUT_PING_TOKEN_INTERRUPT)) {
2565 				if (ep->dev->protocol_stall) {
2566 					ep->stopped = 1;
2567 					set_halt(ep);
2568 				}
2569 				if (!req)
2570 					allow_status(ep);
2571 				mode = 2;
2572 			/* reply to extra IN data tokens with a zlp */
2573 			} else if (t & BIT(DATA_IN_TOKEN_INTERRUPT)) {
2574 				if (ep->dev->protocol_stall) {
2575 					ep->stopped = 1;
2576 					set_halt(ep);
2577 					mode = 2;
2578 				} else if (ep->responded &&
2579 						!req && !ep->stopped)
2580 					write_fifo(ep, NULL);
2581 			}
2582 		} else {
2583 			/* status; stop NAKing */
2584 			if (t & BIT(DATA_IN_TOKEN_INTERRUPT)) {
2585 				if (ep->dev->protocol_stall) {
2586 					ep->stopped = 1;
2587 					set_halt(ep);
2588 				}
2589 				mode = 2;
2590 			/* an extra OUT token is an error */
2591 			} else if (((t & BIT(DATA_OUT_PING_TOKEN_INTERRUPT)) &&
2592 					req &&
2593 					req->req.actual == req->req.length) ||
2594 					(ep->responded && !req)) {
2595 				ep->dev->protocol_stall = 1;
2596 				set_halt(ep);
2597 				ep->stopped = 1;
2598 				if (req)
2599 					done(ep, req, -EOVERFLOW);
2600 				req = NULL;
2601 			}
2602 		}
2603 	}
2604 
2605 	if (unlikely(!req))
2606 		return;
2607 
2608 	/* manual DMA queue advance after short OUT */
2609 	if (likely(ep->dma)) {
2610 		if (t & BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT)) {
2611 			struct net2280_request *stuck_req = NULL;
2612 			int	stopped = ep->stopped;
2613 			int	num_completed;
2614 			int	stuck = 0;
2615 			u32	count;
2616 
2617 			/* TRANSFERRED works around OUT_DONE erratum 0112.
2618 			 * we expect (N <= maxpacket) bytes; host wrote M.
2619 			 * iff (M < N) we won't ever see a DMA interrupt.
2620 			 */
2621 			ep->stopped = 1;
2622 			for (count = 0; ; t = readl(&ep->regs->ep_stat)) {
2623 
2624 				/* any preceding dma transfers must finish.
2625 				 * dma handles (M >= N), may empty the queue
2626 				 */
2627 				num_completed = scan_dma_completions(ep);
2628 				if (unlikely(list_empty(&ep->queue) ||
2629 						ep->out_overflow)) {
2630 					req = NULL;
2631 					break;
2632 				}
2633 				req = list_entry(ep->queue.next,
2634 					struct net2280_request, queue);
2635 
2636 				/* here either (M < N), a "real" short rx;
2637 				 * or (M == N) and the queue didn't empty
2638 				 */
2639 				if (likely(t & BIT(FIFO_EMPTY))) {
2640 					count = readl(&ep->dma->dmacount);
2641 					count &= DMA_BYTE_COUNT_MASK;
2642 					if (readl(&ep->dma->dmadesc)
2643 							!= req->td_dma)
2644 						req = NULL;
2645 					break;
2646 				}
2647 
2648 				/* Escape loop if no dma transfers completed
2649 				 * after few retries.
2650 				 */
2651 				if (num_completed == 0) {
2652 					if (stuck_req == req &&
2653 					    readl(&ep->dma->dmadesc) !=
2654 						  req->td_dma && stuck++ > 5) {
2655 						count = readl(
2656 							&ep->dma->dmacount);
2657 						count &= DMA_BYTE_COUNT_MASK;
2658 						req = NULL;
2659 						ep_dbg(ep->dev, "%s escape stuck %d, count %u\n",
2660 							ep->ep.name, stuck,
2661 							count);
2662 						break;
2663 					} else if (stuck_req != req) {
2664 						stuck_req = req;
2665 						stuck = 0;
2666 					}
2667 				} else {
2668 					stuck_req = NULL;
2669 					stuck = 0;
2670 				}
2671 
2672 				udelay(1);
2673 			}
2674 
2675 			/* stop DMA, leave ep NAKing */
2676 			writel(BIT(DMA_ABORT), &ep->dma->dmastat);
2677 			spin_stop_dma(ep->dma);
2678 
2679 			if (likely(req)) {
2680 				req->td->dmacount = 0;
2681 				t = readl(&ep->regs->ep_avail);
2682 				dma_done(ep, req, count,
2683 					(ep->out_overflow || t)
2684 						? -EOVERFLOW : 0);
2685 			}
2686 
2687 			/* also flush to prevent erratum 0106 trouble */
2688 			if (unlikely(ep->out_overflow ||
2689 					(ep->dev->chiprev == 0x0100 &&
2690 					ep->dev->gadget.speed
2691 					== USB_SPEED_FULL))) {
2692 				out_flush(ep);
2693 				ep->out_overflow = 0;
2694 			}
2695 
2696 			/* (re)start dma if needed, stop NAKing */
2697 			ep->stopped = stopped;
2698 			if (!list_empty(&ep->queue))
2699 				restart_dma(ep);
2700 		} else
2701 			ep_dbg(ep->dev, "%s dma ep_stat %08x ??\n",
2702 					ep->ep.name, t);
2703 		return;
2704 
2705 	/* data packet(s) received (in the fifo, OUT) */
2706 	} else if (t & BIT(DATA_PACKET_RECEIVED_INTERRUPT)) {
2707 		if (read_fifo(ep, req) && ep->num != 0)
2708 			mode = 2;
2709 
2710 	/* data packet(s) transmitted (IN) */
2711 	} else if (t & BIT(DATA_PACKET_TRANSMITTED_INTERRUPT)) {
2712 		unsigned	len;
2713 
2714 		len = req->req.length - req->req.actual;
2715 		if (len > ep->ep.maxpacket)
2716 			len = ep->ep.maxpacket;
2717 		req->req.actual += len;
2718 
2719 		/* if we wrote it all, we're usually done */
2720 		/* send zlps until the status stage */
2721 		if ((req->req.actual == req->req.length) &&
2722 			(!req->req.zero || len != ep->ep.maxpacket) && ep->num)
2723 				mode = 2;
2724 
2725 	/* there was nothing to do ...  */
2726 	} else if (mode == 1)
2727 		return;
2728 
2729 	/* done */
2730 	if (mode == 2) {
2731 		/* stream endpoints often resubmit/unlink in completion */
2732 		done(ep, req, 0);
2733 
2734 		/* maybe advance queue to next request */
2735 		if (ep->num == 0) {
2736 			/* NOTE:  net2280 could let gadget driver start the
2737 			 * status stage later. since not all controllers let
2738 			 * them control that, the api doesn't (yet) allow it.
2739 			 */
2740 			if (!ep->stopped)
2741 				allow_status(ep);
2742 			req = NULL;
2743 		} else {
2744 			if (!list_empty(&ep->queue) && !ep->stopped)
2745 				req = list_entry(ep->queue.next,
2746 					struct net2280_request, queue);
2747 			else
2748 				req = NULL;
2749 			if (req && !ep->is_in)
2750 				stop_out_naking(ep);
2751 		}
2752 	}
2753 
2754 	/* is there a buffer for the next packet?
2755 	 * for best streaming performance, make sure there is one.
2756 	 */
2757 	if (req && !ep->stopped) {
2758 
2759 		/* load IN fifo with next packet (may be zlp) */
2760 		if (t & BIT(DATA_PACKET_TRANSMITTED_INTERRUPT))
2761 			write_fifo(ep, &req->req);
2762 	}
2763 }
2764 
2765 static struct net2280_ep *get_ep_by_addr(struct net2280 *dev, u16 wIndex)
2766 {
2767 	struct net2280_ep	*ep;
2768 
2769 	if ((wIndex & USB_ENDPOINT_NUMBER_MASK) == 0)
2770 		return &dev->ep[0];
2771 	list_for_each_entry(ep, &dev->gadget.ep_list, ep.ep_list) {
2772 		u8	bEndpointAddress;
2773 
2774 		if (!ep->desc)
2775 			continue;
2776 		bEndpointAddress = ep->desc->bEndpointAddress;
2777 		if ((wIndex ^ bEndpointAddress) & USB_DIR_IN)
2778 			continue;
2779 		if ((wIndex & 0x0f) == (bEndpointAddress & 0x0f))
2780 			return ep;
2781 	}
2782 	return NULL;
2783 }
2784 
2785 static void defect7374_workaround(struct net2280 *dev, struct usb_ctrlrequest r)
2786 {
2787 	u32 scratch, fsmvalue;
2788 	u32 ack_wait_timeout, state;
2789 
2790 	/* Workaround for Defect 7374 (U1/U2 erroneously rejected): */
2791 	scratch = get_idx_reg(dev->regs, SCRATCH);
2792 	fsmvalue = scratch & (0xf << DEFECT7374_FSM_FIELD);
2793 	scratch &= ~(0xf << DEFECT7374_FSM_FIELD);
2794 
2795 	if (!((fsmvalue == DEFECT7374_FSM_WAITING_FOR_CONTROL_READ) &&
2796 				(r.bRequestType & USB_DIR_IN)))
2797 		return;
2798 
2799 	/* This is the first Control Read for this connection: */
2800 	if (!(readl(&dev->usb->usbstat) & BIT(SUPER_SPEED_MODE))) {
2801 		/*
2802 		 * Connection is NOT SS:
2803 		 * - Connection must be FS or HS.
2804 		 * - This FSM state should allow workaround software to
2805 		 * run after the next USB connection.
2806 		 */
2807 		scratch |= DEFECT7374_FSM_NON_SS_CONTROL_READ;
2808 		dev->bug7734_patched = 1;
2809 		goto restore_data_eps;
2810 	}
2811 
2812 	/* Connection is SS: */
2813 	for (ack_wait_timeout = 0;
2814 			ack_wait_timeout < DEFECT_7374_NUMBEROF_MAX_WAIT_LOOPS;
2815 			ack_wait_timeout++) {
2816 
2817 		state =	readl(&dev->plregs->pl_ep_status_1)
2818 			& (0xff << STATE);
2819 		if ((state >= (ACK_GOOD_NORMAL << STATE)) &&
2820 			(state <= (ACK_GOOD_MORE_ACKS_TO_COME << STATE))) {
2821 			scratch |= DEFECT7374_FSM_SS_CONTROL_READ;
2822 			dev->bug7734_patched = 1;
2823 			break;
2824 		}
2825 
2826 		/*
2827 		 * We have not yet received host's Data Phase ACK
2828 		 * - Wait and try again.
2829 		 */
2830 		udelay(DEFECT_7374_PROCESSOR_WAIT_TIME);
2831 	}
2832 
2833 
2834 	if (ack_wait_timeout >= DEFECT_7374_NUMBEROF_MAX_WAIT_LOOPS) {
2835 		ep_err(dev, "FAIL: Defect 7374 workaround waited but failed "
2836 		"to detect SS host's data phase ACK.");
2837 		ep_err(dev, "PL_EP_STATUS_1(23:16):.Expected from 0x11 to 0x16"
2838 		"got 0x%2.2x.\n", state >> STATE);
2839 	} else {
2840 		ep_warn(dev, "INFO: Defect 7374 workaround waited about\n"
2841 		"%duSec for Control Read Data Phase ACK\n",
2842 			DEFECT_7374_PROCESSOR_WAIT_TIME * ack_wait_timeout);
2843 	}
2844 
2845 restore_data_eps:
2846 	/*
2847 	 * Restore data EPs to their pre-workaround settings (disabled,
2848 	 * initialized, and other details).
2849 	 */
2850 	defect7374_disable_data_eps(dev);
2851 
2852 	set_idx_reg(dev->regs, SCRATCH, scratch);
2853 
2854 	return;
2855 }
2856 
2857 static void ep_clear_seqnum(struct net2280_ep *ep)
2858 {
2859 	struct net2280 *dev = ep->dev;
2860 	u32 val;
2861 	static const u32 ep_pl[9] = { 0, 3, 4, 7, 8, 2, 5, 6, 9 };
2862 
2863 	val = readl(&dev->plregs->pl_ep_ctrl) & ~0x1f;
2864 	val |= ep_pl[ep->num];
2865 	writel(val, &dev->plregs->pl_ep_ctrl);
2866 	val |= BIT(SEQUENCE_NUMBER_RESET);
2867 	writel(val, &dev->plregs->pl_ep_ctrl);
2868 
2869 	return;
2870 }
2871 
2872 static void handle_stat0_irqs_superspeed(struct net2280 *dev,
2873 		struct net2280_ep *ep, struct usb_ctrlrequest r)
2874 {
2875 	struct net2280_ep *e;
2876 	u16 status;
2877 	int tmp = 0;
2878 
2879 #define	w_value		le16_to_cpu(r.wValue)
2880 #define	w_index		le16_to_cpu(r.wIndex)
2881 #define	w_length	le16_to_cpu(r.wLength)
2882 
2883 	switch (r.bRequest) {
2884 	case USB_REQ_SET_CONFIGURATION:
2885 		dev->addressed_state = !w_value;
2886 		goto usb3_delegate;
2887 
2888 	case USB_REQ_GET_STATUS:
2889 		switch (r.bRequestType) {
2890 		case (USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE):
2891 			status = dev->wakeup_enable ? 0x02 : 0x00;
2892 			if (dev->gadget.is_selfpowered)
2893 				status |= BIT(0);
2894 			status |= (dev->u1_enable << 2 | dev->u2_enable << 3 |
2895 							dev->ltm_enable << 4);
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 		case (USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_ENDPOINT):
2903 			e = get_ep_by_addr(dev, w_index);
2904 			if (!e)
2905 				goto do_stall3;
2906 			status = readl(&e->regs->ep_rsp) &
2907 						BIT(CLEAR_ENDPOINT_HALT);
2908 			writel(0, &dev->epregs[0].ep_irqenb);
2909 			set_fifo_bytecount(ep, sizeof(status));
2910 			writel((__force u32) status, &dev->epregs[0].ep_data);
2911 			allow_status_338x(ep);
2912 			break;
2913 
2914 		default:
2915 			goto usb3_delegate;
2916 		}
2917 		break;
2918 
2919 	case USB_REQ_CLEAR_FEATURE:
2920 		switch (r.bRequestType) {
2921 		case (USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE):
2922 			if (!dev->addressed_state) {
2923 				switch (w_value) {
2924 				case USB_DEVICE_U1_ENABLE:
2925 					dev->u1_enable = 0;
2926 					writel(readl(&dev->usb_ext->usbctl2) &
2927 						~BIT(U1_ENABLE),
2928 						&dev->usb_ext->usbctl2);
2929 					allow_status_338x(ep);
2930 					goto next_endpoints3;
2931 
2932 				case USB_DEVICE_U2_ENABLE:
2933 					dev->u2_enable = 0;
2934 					writel(readl(&dev->usb_ext->usbctl2) &
2935 						~BIT(U2_ENABLE),
2936 						&dev->usb_ext->usbctl2);
2937 					allow_status_338x(ep);
2938 					goto next_endpoints3;
2939 
2940 				case USB_DEVICE_LTM_ENABLE:
2941 					dev->ltm_enable = 0;
2942 					writel(readl(&dev->usb_ext->usbctl2) &
2943 						~BIT(LTM_ENABLE),
2944 						&dev->usb_ext->usbctl2);
2945 					allow_status_338x(ep);
2946 					goto next_endpoints3;
2947 
2948 				default:
2949 					break;
2950 				}
2951 			}
2952 			if (w_value == USB_DEVICE_REMOTE_WAKEUP) {
2953 				dev->wakeup_enable = 0;
2954 				writel(readl(&dev->usb->usbctl) &
2955 					~BIT(DEVICE_REMOTE_WAKEUP_ENABLE),
2956 					&dev->usb->usbctl);
2957 				allow_status_338x(ep);
2958 				break;
2959 			}
2960 			goto usb3_delegate;
2961 
2962 		case (USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_ENDPOINT):
2963 			e = get_ep_by_addr(dev,	w_index);
2964 			if (!e)
2965 				goto do_stall3;
2966 			if (w_value != USB_ENDPOINT_HALT)
2967 				goto do_stall3;
2968 			ep_vdbg(dev, "%s clear halt\n", e->ep.name);
2969 			/*
2970 			 * Workaround for SS SeqNum not cleared via
2971 			 * Endpoint Halt (Clear) bit. select endpoint
2972 			 */
2973 			ep_clear_seqnum(e);
2974 			clear_halt(e);
2975 			if (!list_empty(&e->queue) && e->td_dma)
2976 				restart_dma(e);
2977 			allow_status(ep);
2978 			ep->stopped = 1;
2979 			break;
2980 
2981 		default:
2982 			goto usb3_delegate;
2983 		}
2984 		break;
2985 	case USB_REQ_SET_FEATURE:
2986 		switch (r.bRequestType) {
2987 		case (USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE):
2988 			if (!dev->addressed_state) {
2989 				switch (w_value) {
2990 				case USB_DEVICE_U1_ENABLE:
2991 					dev->u1_enable = 1;
2992 					writel(readl(&dev->usb_ext->usbctl2) |
2993 						BIT(U1_ENABLE),
2994 						&dev->usb_ext->usbctl2);
2995 					allow_status_338x(ep);
2996 					goto next_endpoints3;
2997 
2998 				case USB_DEVICE_U2_ENABLE:
2999 					dev->u2_enable = 1;
3000 					writel(readl(&dev->usb_ext->usbctl2) |
3001 						BIT(U2_ENABLE),
3002 						&dev->usb_ext->usbctl2);
3003 					allow_status_338x(ep);
3004 					goto next_endpoints3;
3005 
3006 				case USB_DEVICE_LTM_ENABLE:
3007 					dev->ltm_enable = 1;
3008 					writel(readl(&dev->usb_ext->usbctl2) |
3009 						BIT(LTM_ENABLE),
3010 						&dev->usb_ext->usbctl2);
3011 					allow_status_338x(ep);
3012 					goto next_endpoints3;
3013 				default:
3014 					break;
3015 				}
3016 			}
3017 
3018 			if (w_value == USB_DEVICE_REMOTE_WAKEUP) {
3019 				dev->wakeup_enable = 1;
3020 				writel(readl(&dev->usb->usbctl) |
3021 					BIT(DEVICE_REMOTE_WAKEUP_ENABLE),
3022 					&dev->usb->usbctl);
3023 				allow_status_338x(ep);
3024 				break;
3025 			}
3026 			goto usb3_delegate;
3027 
3028 		case (USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_ENDPOINT):
3029 			e = get_ep_by_addr(dev,	w_index);
3030 			if (!e || (w_value != USB_ENDPOINT_HALT))
3031 				goto do_stall3;
3032 			ep->stopped = 1;
3033 			if (ep->num == 0)
3034 				ep->dev->protocol_stall = 1;
3035 			else {
3036 				if (ep->dma)
3037 					abort_dma(ep);
3038 				set_halt(ep);
3039 			}
3040 			allow_status_338x(ep);
3041 			break;
3042 
3043 		default:
3044 			goto usb3_delegate;
3045 		}
3046 
3047 		break;
3048 	default:
3049 
3050 usb3_delegate:
3051 		ep_vdbg(dev, "setup %02x.%02x v%04x i%04x l%04x ep_cfg %08x\n",
3052 				r.bRequestType, r.bRequest,
3053 				w_value, w_index, w_length,
3054 				readl(&ep->cfg->ep_cfg));
3055 
3056 		ep->responded = 0;
3057 		if (dev->async_callbacks) {
3058 			spin_unlock(&dev->lock);
3059 			tmp = dev->driver->setup(&dev->gadget, &r);
3060 			spin_lock(&dev->lock);
3061 		}
3062 	}
3063 do_stall3:
3064 	if (tmp < 0) {
3065 		ep_vdbg(dev, "req %02x.%02x protocol STALL; stat %d\n",
3066 				r.bRequestType, r.bRequest, tmp);
3067 		dev->protocol_stall = 1;
3068 		/* TD 9.9 Halt Endpoint test. TD 9.22 Set feature test */
3069 		set_halt(ep);
3070 	}
3071 
3072 next_endpoints3:
3073 
3074 #undef	w_value
3075 #undef	w_index
3076 #undef	w_length
3077 
3078 	return;
3079 }
3080 
3081 static void usb338x_handle_ep_intr(struct net2280 *dev, u32 stat0)
3082 {
3083 	u32 index;
3084 	u32 bit;
3085 
3086 	for (index = 0; index < ARRAY_SIZE(ep_bit); index++) {
3087 		bit = BIT(ep_bit[index]);
3088 
3089 		if (!stat0)
3090 			break;
3091 
3092 		if (!(stat0 & bit))
3093 			continue;
3094 
3095 		stat0 &= ~bit;
3096 
3097 		handle_ep_small(&dev->ep[index]);
3098 	}
3099 }
3100 
3101 static void handle_stat0_irqs(struct net2280 *dev, u32 stat)
3102 {
3103 	struct net2280_ep	*ep;
3104 	u32			num, scratch;
3105 
3106 	/* most of these don't need individual acks */
3107 	stat &= ~BIT(INTA_ASSERTED);
3108 	if (!stat)
3109 		return;
3110 	/* ep_dbg(dev, "irqstat0 %04x\n", stat); */
3111 
3112 	/* starting a control request? */
3113 	if (unlikely(stat & BIT(SETUP_PACKET_INTERRUPT))) {
3114 		union {
3115 			u32			raw[2];
3116 			struct usb_ctrlrequest	r;
3117 		} u;
3118 		int				tmp;
3119 		struct net2280_request		*req;
3120 
3121 		if (dev->gadget.speed == USB_SPEED_UNKNOWN) {
3122 			u32 val = readl(&dev->usb->usbstat);
3123 			if (val & BIT(SUPER_SPEED)) {
3124 				dev->gadget.speed = USB_SPEED_SUPER;
3125 				usb_ep_set_maxpacket_limit(&dev->ep[0].ep,
3126 						EP0_SS_MAX_PACKET_SIZE);
3127 			} else if (val & BIT(HIGH_SPEED)) {
3128 				dev->gadget.speed = USB_SPEED_HIGH;
3129 				usb_ep_set_maxpacket_limit(&dev->ep[0].ep,
3130 						EP0_HS_MAX_PACKET_SIZE);
3131 			} else {
3132 				dev->gadget.speed = USB_SPEED_FULL;
3133 				usb_ep_set_maxpacket_limit(&dev->ep[0].ep,
3134 						EP0_HS_MAX_PACKET_SIZE);
3135 			}
3136 			net2280_led_speed(dev, dev->gadget.speed);
3137 			ep_dbg(dev, "%s\n",
3138 					usb_speed_string(dev->gadget.speed));
3139 		}
3140 
3141 		ep = &dev->ep[0];
3142 		ep->irqs++;
3143 
3144 		/* make sure any leftover request state is cleared */
3145 		stat &= ~BIT(ENDPOINT_0_INTERRUPT);
3146 		while (!list_empty(&ep->queue)) {
3147 			req = list_entry(ep->queue.next,
3148 					struct net2280_request, queue);
3149 			done(ep, req, (req->req.actual == req->req.length)
3150 						? 0 : -EPROTO);
3151 		}
3152 		ep->stopped = 0;
3153 		dev->protocol_stall = 0;
3154 		if (!(dev->quirks & PLX_PCIE)) {
3155 			if (ep->dev->quirks & PLX_2280)
3156 				tmp = BIT(FIFO_OVERFLOW) |
3157 				    BIT(FIFO_UNDERFLOW);
3158 			else
3159 				tmp = 0;
3160 
3161 			writel(tmp | BIT(TIMEOUT) |
3162 				   BIT(USB_STALL_SENT) |
3163 				   BIT(USB_IN_NAK_SENT) |
3164 				   BIT(USB_IN_ACK_RCVD) |
3165 				   BIT(USB_OUT_PING_NAK_SENT) |
3166 				   BIT(USB_OUT_ACK_SENT) |
3167 				   BIT(SHORT_PACKET_OUT_DONE_INTERRUPT) |
3168 				   BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT) |
3169 				   BIT(DATA_PACKET_RECEIVED_INTERRUPT) |
3170 				   BIT(DATA_PACKET_TRANSMITTED_INTERRUPT) |
3171 				   BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
3172 				   BIT(DATA_IN_TOKEN_INTERRUPT),
3173 				   &ep->regs->ep_stat);
3174 		}
3175 		u.raw[0] = readl(&dev->usb->setup0123);
3176 		u.raw[1] = readl(&dev->usb->setup4567);
3177 
3178 		cpu_to_le32s(&u.raw[0]);
3179 		cpu_to_le32s(&u.raw[1]);
3180 
3181 		if ((dev->quirks & PLX_PCIE) && !dev->bug7734_patched)
3182 			defect7374_workaround(dev, u.r);
3183 
3184 		tmp = 0;
3185 
3186 #define	w_value		le16_to_cpu(u.r.wValue)
3187 #define	w_index		le16_to_cpu(u.r.wIndex)
3188 #define	w_length	le16_to_cpu(u.r.wLength)
3189 
3190 		/* ack the irq */
3191 		writel(BIT(SETUP_PACKET_INTERRUPT), &dev->regs->irqstat0);
3192 		stat ^= BIT(SETUP_PACKET_INTERRUPT);
3193 
3194 		/* watch control traffic at the token level, and force
3195 		 * synchronization before letting the status stage happen.
3196 		 * FIXME ignore tokens we'll NAK, until driver responds.
3197 		 * that'll mean a lot less irqs for some drivers.
3198 		 */
3199 		ep->is_in = (u.r.bRequestType & USB_DIR_IN) != 0;
3200 		if (ep->is_in) {
3201 			scratch = BIT(DATA_PACKET_TRANSMITTED_INTERRUPT) |
3202 				BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
3203 				BIT(DATA_IN_TOKEN_INTERRUPT);
3204 			stop_out_naking(ep);
3205 		} else
3206 			scratch = BIT(DATA_PACKET_RECEIVED_INTERRUPT) |
3207 				BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
3208 				BIT(DATA_IN_TOKEN_INTERRUPT);
3209 		writel(scratch, &dev->epregs[0].ep_irqenb);
3210 
3211 		/* we made the hardware handle most lowlevel requests;
3212 		 * everything else goes uplevel to the gadget code.
3213 		 */
3214 		ep->responded = 1;
3215 
3216 		if (dev->gadget.speed == USB_SPEED_SUPER) {
3217 			handle_stat0_irqs_superspeed(dev, ep, u.r);
3218 			goto next_endpoints;
3219 		}
3220 
3221 		switch (u.r.bRequest) {
3222 		case USB_REQ_GET_STATUS: {
3223 			struct net2280_ep	*e;
3224 			__le32			status;
3225 
3226 			/* hw handles device and interface status */
3227 			if (u.r.bRequestType != (USB_DIR_IN|USB_RECIP_ENDPOINT))
3228 				goto delegate;
3229 			e = get_ep_by_addr(dev, w_index);
3230 			if (!e || w_length > 2)
3231 				goto do_stall;
3232 
3233 			if (readl(&e->regs->ep_rsp) & BIT(SET_ENDPOINT_HALT))
3234 				status = cpu_to_le32(1);
3235 			else
3236 				status = cpu_to_le32(0);
3237 
3238 			/* don't bother with a request object! */
3239 			writel(0, &dev->epregs[0].ep_irqenb);
3240 			set_fifo_bytecount(ep, w_length);
3241 			writel((__force u32)status, &dev->epregs[0].ep_data);
3242 			allow_status(ep);
3243 			ep_vdbg(dev, "%s stat %02x\n", ep->ep.name, status);
3244 			goto next_endpoints;
3245 			}
3246 			break;
3247 		case USB_REQ_CLEAR_FEATURE: {
3248 			struct net2280_ep	*e;
3249 
3250 			/* hw handles device features */
3251 			if (u.r.bRequestType != USB_RECIP_ENDPOINT)
3252 				goto delegate;
3253 			if (w_value != USB_ENDPOINT_HALT || w_length != 0)
3254 				goto do_stall;
3255 			e = get_ep_by_addr(dev, w_index);
3256 			if (!e)
3257 				goto do_stall;
3258 			if (e->wedged) {
3259 				ep_vdbg(dev, "%s wedged, halt not cleared\n",
3260 						ep->ep.name);
3261 			} else {
3262 				ep_vdbg(dev, "%s clear halt\n", e->ep.name);
3263 				clear_halt(e);
3264 				if ((ep->dev->quirks & PLX_PCIE) &&
3265 					!list_empty(&e->queue) && e->td_dma)
3266 						restart_dma(e);
3267 			}
3268 			allow_status(ep);
3269 			goto next_endpoints;
3270 			}
3271 			break;
3272 		case USB_REQ_SET_FEATURE: {
3273 			struct net2280_ep	*e;
3274 
3275 			/* hw handles device features */
3276 			if (u.r.bRequestType != USB_RECIP_ENDPOINT)
3277 				goto delegate;
3278 			if (w_value != USB_ENDPOINT_HALT || w_length != 0)
3279 				goto do_stall;
3280 			e = get_ep_by_addr(dev, w_index);
3281 			if (!e)
3282 				goto do_stall;
3283 			if (e->ep.name == ep0name)
3284 				goto do_stall;
3285 			set_halt(e);
3286 			if ((dev->quirks & PLX_PCIE) && e->dma)
3287 				abort_dma(e);
3288 			allow_status(ep);
3289 			ep_vdbg(dev, "%s set halt\n", ep->ep.name);
3290 			goto next_endpoints;
3291 			}
3292 			break;
3293 		default:
3294 delegate:
3295 			ep_vdbg(dev, "setup %02x.%02x v%04x i%04x l%04x "
3296 				"ep_cfg %08x\n",
3297 				u.r.bRequestType, u.r.bRequest,
3298 				w_value, w_index, w_length,
3299 				readl(&ep->cfg->ep_cfg));
3300 			ep->responded = 0;
3301 			if (dev->async_callbacks) {
3302 				spin_unlock(&dev->lock);
3303 				tmp = dev->driver->setup(&dev->gadget, &u.r);
3304 				spin_lock(&dev->lock);
3305 			}
3306 		}
3307 
3308 		/* stall ep0 on error */
3309 		if (tmp < 0) {
3310 do_stall:
3311 			ep_vdbg(dev, "req %02x.%02x protocol STALL; stat %d\n",
3312 					u.r.bRequestType, u.r.bRequest, tmp);
3313 			dev->protocol_stall = 1;
3314 		}
3315 
3316 		/* some in/out token irq should follow; maybe stall then.
3317 		 * driver must queue a request (even zlp) or halt ep0
3318 		 * before the host times out.
3319 		 */
3320 	}
3321 
3322 #undef	w_value
3323 #undef	w_index
3324 #undef	w_length
3325 
3326 next_endpoints:
3327 	if ((dev->quirks & PLX_PCIE) && dev->enhanced_mode) {
3328 		u32 mask = (BIT(ENDPOINT_0_INTERRUPT) |
3329 			USB3380_IRQSTAT0_EP_INTR_MASK_IN |
3330 			USB3380_IRQSTAT0_EP_INTR_MASK_OUT);
3331 
3332 		if (stat & mask) {
3333 			usb338x_handle_ep_intr(dev, stat & mask);
3334 			stat &= ~mask;
3335 		}
3336 	} else {
3337 		/* endpoint data irq ? */
3338 		scratch = stat & 0x7f;
3339 		stat &= ~0x7f;
3340 		for (num = 0; scratch; num++) {
3341 			u32		t;
3342 
3343 			/* do this endpoint's FIFO and queue need tending? */
3344 			t = BIT(num);
3345 			if ((scratch & t) == 0)
3346 				continue;
3347 			scratch ^= t;
3348 
3349 			ep = &dev->ep[num];
3350 			handle_ep_small(ep);
3351 		}
3352 	}
3353 
3354 	if (stat)
3355 		ep_dbg(dev, "unhandled irqstat0 %08x\n", stat);
3356 }
3357 
3358 #define DMA_INTERRUPTS (BIT(DMA_D_INTERRUPT) | \
3359 		BIT(DMA_C_INTERRUPT) | \
3360 		BIT(DMA_B_INTERRUPT) | \
3361 		BIT(DMA_A_INTERRUPT))
3362 #define	PCI_ERROR_INTERRUPTS ( \
3363 		BIT(PCI_MASTER_ABORT_RECEIVED_INTERRUPT) | \
3364 		BIT(PCI_TARGET_ABORT_RECEIVED_INTERRUPT) | \
3365 		BIT(PCI_RETRY_ABORT_INTERRUPT))
3366 
3367 static void handle_stat1_irqs(struct net2280 *dev, u32 stat)
3368 __releases(dev->lock)
3369 __acquires(dev->lock)
3370 {
3371 	struct net2280_ep	*ep;
3372 	u32			tmp, num, mask, scratch;
3373 
3374 	/* after disconnect there's nothing else to do! */
3375 	tmp = BIT(VBUS_INTERRUPT) | BIT(ROOT_PORT_RESET_INTERRUPT);
3376 	mask = BIT(SUPER_SPEED) | BIT(HIGH_SPEED) | BIT(FULL_SPEED);
3377 
3378 	/* VBUS disconnect is indicated by VBUS_PIN and VBUS_INTERRUPT set.
3379 	 * Root Port Reset is indicated by ROOT_PORT_RESET_INTERRUPT set and
3380 	 * both HIGH_SPEED and FULL_SPEED clear (as ROOT_PORT_RESET_INTERRUPT
3381 	 * only indicates a change in the reset state).
3382 	 */
3383 	if (stat & tmp) {
3384 		bool	reset = false;
3385 		bool	disconnect = false;
3386 
3387 		/*
3388 		 * Ignore disconnects and resets if the speed hasn't been set.
3389 		 * VBUS can bounce and there's always an initial reset.
3390 		 */
3391 		writel(tmp, &dev->regs->irqstat1);
3392 		if (dev->gadget.speed != USB_SPEED_UNKNOWN) {
3393 			if ((stat & BIT(VBUS_INTERRUPT)) &&
3394 					(readl(&dev->usb->usbctl) &
3395 						BIT(VBUS_PIN)) == 0) {
3396 				disconnect = true;
3397 				ep_dbg(dev, "disconnect %s\n",
3398 						dev->driver->driver.name);
3399 			} else if ((stat & BIT(ROOT_PORT_RESET_INTERRUPT)) &&
3400 					(readl(&dev->usb->usbstat) & mask)
3401 						== 0) {
3402 				reset = true;
3403 				ep_dbg(dev, "reset %s\n",
3404 						dev->driver->driver.name);
3405 			}
3406 
3407 			if (disconnect || reset) {
3408 				stop_activity(dev, dev->driver);
3409 				ep0_start(dev);
3410 				if (dev->async_callbacks) {
3411 					spin_unlock(&dev->lock);
3412 					if (reset)
3413 						usb_gadget_udc_reset(&dev->gadget, dev->driver);
3414 					else
3415 						(dev->driver->disconnect)(&dev->gadget);
3416 					spin_lock(&dev->lock);
3417 				}
3418 				return;
3419 			}
3420 		}
3421 		stat &= ~tmp;
3422 
3423 		/* vBUS can bounce ... one of many reasons to ignore the
3424 		 * notion of hotplug events on bus connect/disconnect!
3425 		 */
3426 		if (!stat)
3427 			return;
3428 	}
3429 
3430 	/* NOTE: chip stays in PCI D0 state for now, but it could
3431 	 * enter D1 to save more power
3432 	 */
3433 	tmp = BIT(SUSPEND_REQUEST_CHANGE_INTERRUPT);
3434 	if (stat & tmp) {
3435 		writel(tmp, &dev->regs->irqstat1);
3436 		spin_unlock(&dev->lock);
3437 		if (stat & BIT(SUSPEND_REQUEST_INTERRUPT)) {
3438 			if (dev->async_callbacks && dev->driver->suspend)
3439 				dev->driver->suspend(&dev->gadget);
3440 			if (!enable_suspend)
3441 				stat &= ~BIT(SUSPEND_REQUEST_INTERRUPT);
3442 		} else {
3443 			if (dev->async_callbacks && dev->driver->resume)
3444 				dev->driver->resume(&dev->gadget);
3445 			/* at high speed, note erratum 0133 */
3446 		}
3447 		spin_lock(&dev->lock);
3448 		stat &= ~tmp;
3449 	}
3450 
3451 	/* clear any other status/irqs */
3452 	if (stat)
3453 		writel(stat, &dev->regs->irqstat1);
3454 
3455 	/* some status we can just ignore */
3456 	if (dev->quirks & PLX_2280)
3457 		stat &= ~(BIT(CONTROL_STATUS_INTERRUPT) |
3458 			  BIT(SUSPEND_REQUEST_INTERRUPT) |
3459 			  BIT(RESUME_INTERRUPT) |
3460 			  BIT(SOF_INTERRUPT));
3461 	else
3462 		stat &= ~(BIT(CONTROL_STATUS_INTERRUPT) |
3463 			  BIT(RESUME_INTERRUPT) |
3464 			  BIT(SOF_DOWN_INTERRUPT) |
3465 			  BIT(SOF_INTERRUPT));
3466 
3467 	if (!stat)
3468 		return;
3469 	/* ep_dbg(dev, "irqstat1 %08x\n", stat);*/
3470 
3471 	/* DMA status, for ep-{a,b,c,d} */
3472 	scratch = stat & DMA_INTERRUPTS;
3473 	stat &= ~DMA_INTERRUPTS;
3474 	scratch >>= 9;
3475 	for (num = 0; scratch; num++) {
3476 		struct net2280_dma_regs	__iomem *dma;
3477 
3478 		tmp = BIT(num);
3479 		if ((tmp & scratch) == 0)
3480 			continue;
3481 		scratch ^= tmp;
3482 
3483 		ep = &dev->ep[num + 1];
3484 		dma = ep->dma;
3485 
3486 		if (!dma)
3487 			continue;
3488 
3489 		/* clear ep's dma status */
3490 		tmp = readl(&dma->dmastat);
3491 		writel(tmp, &dma->dmastat);
3492 
3493 		/* dma sync*/
3494 		if (dev->quirks & PLX_PCIE) {
3495 			u32 r_dmacount = readl(&dma->dmacount);
3496 			if (!ep->is_in &&  (r_dmacount & 0x00FFFFFF) &&
3497 			    (tmp & BIT(DMA_TRANSACTION_DONE_INTERRUPT)))
3498 				continue;
3499 		}
3500 
3501 		if (!(tmp & BIT(DMA_TRANSACTION_DONE_INTERRUPT))) {
3502 			ep_dbg(ep->dev, "%s no xact done? %08x\n",
3503 				ep->ep.name, tmp);
3504 			continue;
3505 		}
3506 		stop_dma(ep->dma);
3507 
3508 		/* OUT transfers terminate when the data from the
3509 		 * host is in our memory.  Process whatever's done.
3510 		 * On this path, we know transfer's last packet wasn't
3511 		 * less than req->length. NAK_OUT_PACKETS may be set,
3512 		 * or the FIFO may already be holding new packets.
3513 		 *
3514 		 * IN transfers can linger in the FIFO for a very
3515 		 * long time ... we ignore that for now, accounting
3516 		 * precisely (like PIO does) needs per-packet irqs
3517 		 */
3518 		scan_dma_completions(ep);
3519 
3520 		/* disable dma on inactive queues; else maybe restart */
3521 		if (!list_empty(&ep->queue)) {
3522 			tmp = readl(&dma->dmactl);
3523 			restart_dma(ep);
3524 		}
3525 		ep->irqs++;
3526 	}
3527 
3528 	/* NOTE:  there are other PCI errors we might usefully notice.
3529 	 * if they appear very often, here's where to try recovering.
3530 	 */
3531 	if (stat & PCI_ERROR_INTERRUPTS) {
3532 		ep_err(dev, "pci dma error; stat %08x\n", stat);
3533 		stat &= ~PCI_ERROR_INTERRUPTS;
3534 		/* these are fatal errors, but "maybe" they won't
3535 		 * happen again ...
3536 		 */
3537 		stop_activity(dev, dev->driver);
3538 		ep0_start(dev);
3539 		stat = 0;
3540 	}
3541 
3542 	if (stat)
3543 		ep_dbg(dev, "unhandled irqstat1 %08x\n", stat);
3544 }
3545 
3546 static irqreturn_t net2280_irq(int irq, void *_dev)
3547 {
3548 	struct net2280		*dev = _dev;
3549 
3550 	/* shared interrupt, not ours */
3551 	if ((dev->quirks & PLX_LEGACY) &&
3552 		(!(readl(&dev->regs->irqstat0) & BIT(INTA_ASSERTED))))
3553 		return IRQ_NONE;
3554 
3555 	spin_lock(&dev->lock);
3556 
3557 	/* handle disconnect, dma, and more */
3558 	handle_stat1_irqs(dev, readl(&dev->regs->irqstat1));
3559 
3560 	/* control requests and PIO */
3561 	handle_stat0_irqs(dev, readl(&dev->regs->irqstat0));
3562 
3563 	if (dev->quirks & PLX_PCIE) {
3564 		/* re-enable interrupt to trigger any possible new interrupt */
3565 		u32 pciirqenb1 = readl(&dev->regs->pciirqenb1);
3566 		writel(pciirqenb1 & 0x7FFFFFFF, &dev->regs->pciirqenb1);
3567 		writel(pciirqenb1, &dev->regs->pciirqenb1);
3568 	}
3569 
3570 	spin_unlock(&dev->lock);
3571 
3572 	return IRQ_HANDLED;
3573 }
3574 
3575 /*-------------------------------------------------------------------------*/
3576 
3577 static void gadget_release(struct device *_dev)
3578 {
3579 	struct net2280	*dev = container_of(_dev, struct net2280, gadget.dev);
3580 
3581 	kfree(dev);
3582 }
3583 
3584 /* tear down the binding between this driver and the pci device */
3585 
3586 static void net2280_remove(struct pci_dev *pdev)
3587 {
3588 	struct net2280		*dev = pci_get_drvdata(pdev);
3589 
3590 	if (dev->added)
3591 		usb_del_gadget(&dev->gadget);
3592 
3593 	BUG_ON(dev->driver);
3594 
3595 	/* then clean up the resources we allocated during probe() */
3596 	if (dev->requests) {
3597 		int		i;
3598 		for (i = 1; i < 5; i++) {
3599 			if (!dev->ep[i].dummy)
3600 				continue;
3601 			dma_pool_free(dev->requests, dev->ep[i].dummy,
3602 					dev->ep[i].td_dma);
3603 		}
3604 		dma_pool_destroy(dev->requests);
3605 	}
3606 	if (dev->got_irq)
3607 		free_irq(pdev->irq, dev);
3608 	if (dev->quirks & PLX_PCIE)
3609 		pci_disable_msi(pdev);
3610 	if (dev->regs) {
3611 		net2280_led_shutdown(dev);
3612 		iounmap(dev->regs);
3613 	}
3614 	if (dev->region)
3615 		release_mem_region(pci_resource_start(pdev, 0),
3616 				pci_resource_len(pdev, 0));
3617 	if (dev->enabled)
3618 		pci_disable_device(pdev);
3619 	device_remove_file(&pdev->dev, &dev_attr_registers);
3620 
3621 	ep_info(dev, "unbind\n");
3622 	usb_put_gadget(&dev->gadget);
3623 }
3624 
3625 /* wrap this driver around the specified device, but
3626  * don't respond over USB until a gadget driver binds to us.
3627  */
3628 
3629 static int net2280_probe(struct pci_dev *pdev, const struct pci_device_id *id)
3630 {
3631 	struct net2280		*dev;
3632 	unsigned long		resource, len;
3633 	void			__iomem *base = NULL;
3634 	int			retval, i;
3635 
3636 	/* alloc, and start init */
3637 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
3638 	if (dev == NULL) {
3639 		retval = -ENOMEM;
3640 		goto done;
3641 	}
3642 
3643 	pci_set_drvdata(pdev, dev);
3644 	usb_initialize_gadget(&pdev->dev, &dev->gadget, gadget_release);
3645 	spin_lock_init(&dev->lock);
3646 	dev->quirks = id->driver_data;
3647 	dev->pdev = pdev;
3648 	dev->gadget.ops = &net2280_ops;
3649 	dev->gadget.max_speed = (dev->quirks & PLX_SUPERSPEED) ?
3650 				USB_SPEED_SUPER : USB_SPEED_HIGH;
3651 
3652 	/* the "gadget" abstracts/virtualizes the controller */
3653 	dev->gadget.name = driver_name;
3654 
3655 	/* now all the pci goodies ... */
3656 	if (pci_enable_device(pdev) < 0) {
3657 		retval = -ENODEV;
3658 		goto done;
3659 	}
3660 	dev->enabled = 1;
3661 
3662 	/* BAR 0 holds all the registers
3663 	 * BAR 1 is 8051 memory; unused here (note erratum 0103)
3664 	 * BAR 2 is fifo memory; unused here
3665 	 */
3666 	resource = pci_resource_start(pdev, 0);
3667 	len = pci_resource_len(pdev, 0);
3668 	if (!request_mem_region(resource, len, driver_name)) {
3669 		ep_dbg(dev, "controller already in use\n");
3670 		retval = -EBUSY;
3671 		goto done;
3672 	}
3673 	dev->region = 1;
3674 
3675 	/* FIXME provide firmware download interface to put
3676 	 * 8051 code into the chip, e.g. to turn on PCI PM.
3677 	 */
3678 
3679 	base = ioremap(resource, len);
3680 	if (base == NULL) {
3681 		ep_dbg(dev, "can't map memory\n");
3682 		retval = -EFAULT;
3683 		goto done;
3684 	}
3685 	dev->regs = (struct net2280_regs __iomem *) base;
3686 	dev->usb = (struct net2280_usb_regs __iomem *) (base + 0x0080);
3687 	dev->pci = (struct net2280_pci_regs __iomem *) (base + 0x0100);
3688 	dev->dma = (struct net2280_dma_regs __iomem *) (base + 0x0180);
3689 	dev->dep = (struct net2280_dep_regs __iomem *) (base + 0x0200);
3690 	dev->epregs = (struct net2280_ep_regs __iomem *) (base + 0x0300);
3691 
3692 	if (dev->quirks & PLX_PCIE) {
3693 		u32 fsmvalue;
3694 		u32 usbstat;
3695 		dev->usb_ext = (struct usb338x_usb_ext_regs __iomem *)
3696 							(base + 0x00b4);
3697 		dev->llregs = (struct usb338x_ll_regs __iomem *)
3698 							(base + 0x0700);
3699 		dev->plregs = (struct usb338x_pl_regs __iomem *)
3700 							(base + 0x0800);
3701 		usbstat = readl(&dev->usb->usbstat);
3702 		dev->enhanced_mode = !!(usbstat & BIT(11));
3703 		dev->n_ep = (dev->enhanced_mode) ? 9 : 5;
3704 		/* put into initial config, link up all endpoints */
3705 		fsmvalue = get_idx_reg(dev->regs, SCRATCH) &
3706 					(0xf << DEFECT7374_FSM_FIELD);
3707 		/* See if firmware needs to set up for workaround: */
3708 		if (fsmvalue == DEFECT7374_FSM_SS_CONTROL_READ) {
3709 			dev->bug7734_patched = 1;
3710 			writel(0, &dev->usb->usbctl);
3711 		} else
3712 			dev->bug7734_patched = 0;
3713 	} else {
3714 		dev->enhanced_mode = 0;
3715 		dev->n_ep = 7;
3716 		/* put into initial config, link up all endpoints */
3717 		writel(0, &dev->usb->usbctl);
3718 	}
3719 
3720 	usb_reset(dev);
3721 	usb_reinit(dev);
3722 
3723 	/* irq setup after old hardware is cleaned up */
3724 	if (!pdev->irq) {
3725 		ep_err(dev, "No IRQ.  Check PCI setup!\n");
3726 		retval = -ENODEV;
3727 		goto done;
3728 	}
3729 
3730 	if (dev->quirks & PLX_PCIE)
3731 		if (pci_enable_msi(pdev))
3732 			ep_err(dev, "Failed to enable MSI mode\n");
3733 
3734 	if (request_irq(pdev->irq, net2280_irq, IRQF_SHARED,
3735 							driver_name, dev)) {
3736 		ep_err(dev, "request interrupt %d failed\n", pdev->irq);
3737 		retval = -EBUSY;
3738 		goto done;
3739 	}
3740 	dev->got_irq = 1;
3741 
3742 	/* DMA setup */
3743 	/* NOTE:  we know only the 32 LSBs of dma addresses may be nonzero */
3744 	dev->requests = dma_pool_create("requests", &pdev->dev,
3745 		sizeof(struct net2280_dma),
3746 		0 /* no alignment requirements */,
3747 		0 /* or page-crossing issues */);
3748 	if (!dev->requests) {
3749 		ep_dbg(dev, "can't get request pool\n");
3750 		retval = -ENOMEM;
3751 		goto done;
3752 	}
3753 	for (i = 1; i < 5; i++) {
3754 		struct net2280_dma	*td;
3755 
3756 		td = dma_pool_alloc(dev->requests, GFP_KERNEL,
3757 				&dev->ep[i].td_dma);
3758 		if (!td) {
3759 			ep_dbg(dev, "can't get dummy %d\n", i);
3760 			retval = -ENOMEM;
3761 			goto done;
3762 		}
3763 		td->dmacount = 0;	/* not VALID */
3764 		td->dmadesc = td->dmaaddr;
3765 		dev->ep[i].dummy = td;
3766 	}
3767 
3768 	/* enable lower-overhead pci memory bursts during DMA */
3769 	if (dev->quirks & PLX_LEGACY)
3770 		writel(BIT(DMA_MEMORY_WRITE_AND_INVALIDATE_ENABLE) |
3771 			/*
3772 			 * 256 write retries may not be enough...
3773 			   BIT(PCI_RETRY_ABORT_ENABLE) |
3774 			*/
3775 			BIT(DMA_READ_MULTIPLE_ENABLE) |
3776 			BIT(DMA_READ_LINE_ENABLE),
3777 			&dev->pci->pcimstctl);
3778 	/* erratum 0115 shouldn't appear: Linux inits PCI_LATENCY_TIMER */
3779 	pci_set_master(pdev);
3780 	pci_try_set_mwi(pdev);
3781 
3782 	/* ... also flushes any posted pci writes */
3783 	dev->chiprev = get_idx_reg(dev->regs, REG_CHIPREV) & 0xffff;
3784 
3785 	/* done */
3786 	ep_info(dev, "%s\n", driver_desc);
3787 	ep_info(dev, "irq %d, pci mem %p, chip rev %04x\n",
3788 			pdev->irq, base, dev->chiprev);
3789 	ep_info(dev, "version: " DRIVER_VERSION "; %s\n",
3790 		dev->enhanced_mode ? "enhanced mode" : "legacy mode");
3791 	retval = device_create_file(&pdev->dev, &dev_attr_registers);
3792 	if (retval)
3793 		goto done;
3794 
3795 	retval = usb_add_gadget(&dev->gadget);
3796 	if (retval)
3797 		goto done;
3798 	dev->added = 1;
3799 	return 0;
3800 
3801 done:
3802 	if (dev) {
3803 		net2280_remove(pdev);
3804 		kfree(dev);
3805 	}
3806 	return retval;
3807 }
3808 
3809 /* make sure the board is quiescent; otherwise it will continue
3810  * generating IRQs across the upcoming reboot.
3811  */
3812 
3813 static void net2280_shutdown(struct pci_dev *pdev)
3814 {
3815 	struct net2280		*dev = pci_get_drvdata(pdev);
3816 
3817 	/* disable IRQs */
3818 	writel(0, &dev->regs->pciirqenb0);
3819 	writel(0, &dev->regs->pciirqenb1);
3820 
3821 	/* disable the pullup so the host will think we're gone */
3822 	writel(0, &dev->usb->usbctl);
3823 
3824 }
3825 
3826 
3827 /*-------------------------------------------------------------------------*/
3828 
3829 static const struct pci_device_id pci_ids[] = { {
3830 	.class =	PCI_CLASS_SERIAL_USB_DEVICE,
3831 	.class_mask =	~0,
3832 	.vendor =	PCI_VENDOR_ID_PLX_LEGACY,
3833 	.device =	0x2280,
3834 	.subvendor =	PCI_ANY_ID,
3835 	.subdevice =	PCI_ANY_ID,
3836 	.driver_data =	PLX_LEGACY | PLX_2280,
3837 	}, {
3838 	.class =	PCI_CLASS_SERIAL_USB_DEVICE,
3839 	.class_mask =	~0,
3840 	.vendor =	PCI_VENDOR_ID_PLX_LEGACY,
3841 	.device =	0x2282,
3842 	.subvendor =	PCI_ANY_ID,
3843 	.subdevice =	PCI_ANY_ID,
3844 	.driver_data =	PLX_LEGACY,
3845 	},
3846 	{
3847 	.class =	PCI_CLASS_SERIAL_USB_DEVICE,
3848 	.class_mask =	~0,
3849 	.vendor =	PCI_VENDOR_ID_PLX,
3850 	.device =	0x2380,
3851 	.subvendor =	PCI_ANY_ID,
3852 	.subdevice =	PCI_ANY_ID,
3853 	.driver_data =	PLX_PCIE,
3854 	 },
3855 	{
3856 	.class =	((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
3857 	.class_mask =	~0,
3858 	.vendor =	PCI_VENDOR_ID_PLX,
3859 	.device =	0x3380,
3860 	.subvendor =	PCI_ANY_ID,
3861 	.subdevice =	PCI_ANY_ID,
3862 	.driver_data =	PLX_PCIE | PLX_SUPERSPEED,
3863 	 },
3864 	{
3865 	.class =	PCI_CLASS_SERIAL_USB_DEVICE,
3866 	.class_mask =	~0,
3867 	.vendor =	PCI_VENDOR_ID_PLX,
3868 	.device =	0x3382,
3869 	.subvendor =	PCI_ANY_ID,
3870 	.subdevice =	PCI_ANY_ID,
3871 	.driver_data =	PLX_PCIE | PLX_SUPERSPEED,
3872 	 },
3873 { /* end: all zeroes */ }
3874 };
3875 MODULE_DEVICE_TABLE(pci, pci_ids);
3876 
3877 /* pci driver glue; this is a "new style" PCI driver module */
3878 static struct pci_driver net2280_pci_driver = {
3879 	.name =		driver_name,
3880 	.id_table =	pci_ids,
3881 
3882 	.probe =	net2280_probe,
3883 	.remove =	net2280_remove,
3884 	.shutdown =	net2280_shutdown,
3885 
3886 	/* FIXME add power management support */
3887 };
3888 
3889 module_pci_driver(net2280_pci_driver);
3890 
3891 MODULE_DESCRIPTION(DRIVER_DESC);
3892 MODULE_AUTHOR("David Brownell");
3893 MODULE_LICENSE("GPL");
3894