2 * Driver for the PLX NET2280 USB device controller.
3 * Specs and errata are available from <http://www.plxtech.com>.
5 * PLX Technology Inc. (formerly NetChip Technology) supported the
6 * development of this driver.
9 * CODE STATUS HIGHLIGHTS
11 * This driver should work well with most "gadget" drivers, including
12 * the Mass Storage, Serial, and Ethernet/RNDIS gadget drivers
13 * as well as Gadget Zero and Gadgetfs.
15 * DMA is enabled by default.
17 * MSI is enabled by default. The legacy IRQ is used if MSI couldn't
20 * Note that almost all the errata workarounds here are only needed for
21 * rev1 chips. Rev1a silicon (0110) fixes almost all of them.
25 * Copyright (C) 2003 David Brownell
26 * Copyright (C) 2003-2005 PLX Technology, Inc.
27 * Copyright (C) 2014 Ricardo Ribalda - Qtechnology/AS
29 * Modified Seth Levy 2005 PLX Technology, Inc. to provide compatibility
32 * Modified Ricardo Ribalda Qtechnology AS to provide compatibility
33 * with usb 338x chip. Based on PLX driver
35 * This program is free software; you can redistribute it and/or modify
36 * it under the terms of the GNU General Public License as published by
37 * the Free Software Foundation; either version 2 of the License, or
38 * (at your option) any later version.
41 #include <linux/module.h>
42 #include <linux/pci.h>
43 #include <linux/dma-mapping.h>
44 #include <linux/kernel.h>
45 #include <linux/delay.h>
46 #include <linux/ioport.h>
47 #include <linux/slab.h>
48 #include <linux/errno.h>
49 #include <linux/init.h>
50 #include <linux/timer.h>
51 #include <linux/list.h>
52 #include <linux/interrupt.h>
53 #include <linux/moduleparam.h>
54 #include <linux/device.h>
55 #include <linux/usb/ch9.h>
56 #include <linux/usb/gadget.h>
57 #include <linux/prefetch.h>
60 #include <asm/byteorder.h>
62 #include <asm/unaligned.h>
64 #define DRIVER_DESC "PLX NET228x/USB338x USB Peripheral Controller"
65 #define DRIVER_VERSION "2005 Sept 27/v3.0"
67 #define EP_DONTUSE 13 /* nonzero */
69 #define USE_RDK_LEDS /* GPIO pins control three LEDs */
72 static const char driver_name[] = "net2280";
73 static const char driver_desc[] = DRIVER_DESC;
75 static const u32 ep_bit[9] = { 0, 17, 2, 19, 4, 1, 18, 3, 20 };
76 static const char ep0name[] = "ep0";
77 static const char *const ep_name[] = {
79 "ep-a", "ep-b", "ep-c", "ep-d",
80 "ep-e", "ep-f", "ep-g", "ep-h",
83 /* Endpoint names for usb3380 advance mode */
84 static const char *const ep_name_adv[] = {
86 "ep1in", "ep2out", "ep3in", "ep4out",
87 "ep1out", "ep2in", "ep3out", "ep4in",
90 /* mode 0 == ep-{a,b,c,d} 1K fifo each
91 * mode 1 == ep-{a,b} 2K fifo each, ep-{c,d} unavailable
92 * mode 2 == ep-a 2K fifo, ep-{b,c} 1K each, ep-d unavailable
94 static ushort fifo_mode;
96 /* "modprobe net2280 fifo_mode=1" etc */
97 module_param(fifo_mode, ushort, 0644);
99 /* enable_suspend -- When enabled, the driver will respond to
100 * USB suspend requests by powering down the NET2280. Otherwise,
101 * USB suspend requests will be ignored. This is acceptable for
102 * self-powered devices
104 static bool enable_suspend;
106 /* "modprobe net2280 enable_suspend=1" etc */
107 module_param(enable_suspend, bool, 0444);
109 #define DIR_STRING(bAddress) (((bAddress) & USB_DIR_IN) ? "in" : "out")
111 static char *type_string(u8 bmAttributes)
113 switch ((bmAttributes) & USB_ENDPOINT_XFERTYPE_MASK) {
114 case USB_ENDPOINT_XFER_BULK: return "bulk";
115 case USB_ENDPOINT_XFER_ISOC: return "iso";
116 case USB_ENDPOINT_XFER_INT: return "intr";
123 #define valid_bit cpu_to_le32(BIT(VALID_BIT))
124 #define dma_done_ie cpu_to_le32(BIT(DMA_DONE_INTERRUPT_ENABLE))
126 /*-------------------------------------------------------------------------*/
127 static inline void enable_pciirqenb(struct net2280_ep *ep)
129 u32 tmp = readl(&ep->dev->regs->pciirqenb0);
131 if (ep->dev->quirks & PLX_LEGACY)
134 tmp |= BIT(ep_bit[ep->num]);
135 writel(tmp, &ep->dev->regs->pciirqenb0);
141 net2280_enable(struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
144 struct net2280_ep *ep;
147 static const u32 ep_key[9] = { 1, 0, 1, 0, 1, 1, 0, 1, 0 };
150 ep = container_of(_ep, struct net2280_ep, ep);
151 if (!_ep || !desc || ep->desc || _ep->name == ep0name ||
152 desc->bDescriptorType != USB_DT_ENDPOINT) {
153 pr_err("%s: failed at line=%d\n", __func__, __LINE__);
157 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
162 /* erratum 0119 workaround ties up an endpoint number */
163 if ((desc->bEndpointAddress & 0x0f) == EP_DONTUSE) {
168 if (dev->quirks & PLX_SUPERSPEED) {
169 if ((desc->bEndpointAddress & 0x0f) >= 0x0c) {
173 ep->is_in = !!usb_endpoint_dir_in(desc);
174 if (dev->enhanced_mode && ep->is_in && ep_key[ep->num]) {
180 /* sanity check ep-e/ep-f since their fifos are small */
181 max = usb_endpoint_maxp(desc) & 0x1fff;
182 if (ep->num > 4 && max > 64 && (dev->quirks & PLX_LEGACY)) {
187 spin_lock_irqsave(&dev->lock, flags);
188 _ep->maxpacket = max & 0x7ff;
191 /* ep_reset() has already been called */
194 ep->out_overflow = 0;
196 /* set speed-dependent max packet; may kick in high bandwidth */
197 set_max_speed(ep, max);
199 /* set type, direction, address; reset fifo counters */
200 writel(BIT(FIFO_FLUSH), &ep->regs->ep_stat);
201 tmp = (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK);
202 if (tmp == USB_ENDPOINT_XFER_INT) {
203 /* erratum 0105 workaround prevents hs NYET */
204 if (dev->chiprev == 0100 &&
205 dev->gadget.speed == USB_SPEED_HIGH &&
206 !(desc->bEndpointAddress & USB_DIR_IN))
207 writel(BIT(CLEAR_NAK_OUT_PACKETS_MODE),
209 } else if (tmp == USB_ENDPOINT_XFER_BULK) {
210 /* catch some particularly blatant driver bugs */
211 if ((dev->gadget.speed == USB_SPEED_SUPER && max != 1024) ||
212 (dev->gadget.speed == USB_SPEED_HIGH && max != 512) ||
213 (dev->gadget.speed == USB_SPEED_FULL && max > 64)) {
214 spin_unlock_irqrestore(&dev->lock, flags);
219 ep->is_iso = (tmp == USB_ENDPOINT_XFER_ISOC);
220 /* Enable this endpoint */
221 if (dev->quirks & PLX_LEGACY) {
222 tmp <<= ENDPOINT_TYPE;
223 tmp |= desc->bEndpointAddress;
224 /* default full fifo lines */
225 tmp |= (4 << ENDPOINT_BYTE_COUNT);
226 tmp |= BIT(ENDPOINT_ENABLE);
227 ep->is_in = (tmp & USB_DIR_IN) != 0;
229 /* In Legacy mode, only OUT endpoints are used */
230 if (dev->enhanced_mode && ep->is_in) {
231 tmp <<= IN_ENDPOINT_TYPE;
232 tmp |= BIT(IN_ENDPOINT_ENABLE);
233 /* Not applicable to Legacy */
234 tmp |= BIT(ENDPOINT_DIRECTION);
236 tmp <<= OUT_ENDPOINT_TYPE;
237 tmp |= BIT(OUT_ENDPOINT_ENABLE);
238 tmp |= (ep->is_in << ENDPOINT_DIRECTION);
241 tmp |= usb_endpoint_num(desc);
242 tmp |= (ep->ep.maxburst << MAX_BURST_SIZE);
245 /* Make sure all the registers are written before ep_rsp*/
248 /* for OUT transfers, block the rx fifo until a read is posted */
250 writel(BIT(SET_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
251 else if (!(dev->quirks & PLX_2280)) {
252 /* Added for 2282, Don't use nak packets on an in endpoint,
253 * this was ignored on 2280
255 writel(BIT(CLEAR_NAK_OUT_PACKETS) |
256 BIT(CLEAR_NAK_OUT_PACKETS_MODE), &ep->regs->ep_rsp);
259 writel(tmp, &ep->cfg->ep_cfg);
262 if (!ep->dma) { /* pio, per-packet */
263 enable_pciirqenb(ep);
265 tmp = BIT(DATA_PACKET_RECEIVED_INTERRUPT_ENABLE) |
266 BIT(DATA_PACKET_TRANSMITTED_INTERRUPT_ENABLE);
267 if (dev->quirks & PLX_2280)
268 tmp |= readl(&ep->regs->ep_irqenb);
269 writel(tmp, &ep->regs->ep_irqenb);
270 } else { /* dma, per-request */
271 tmp = BIT((8 + ep->num)); /* completion */
272 tmp |= readl(&dev->regs->pciirqenb1);
273 writel(tmp, &dev->regs->pciirqenb1);
275 /* for short OUT transfers, dma completions can't
276 * advance the queue; do it pio-style, by hand.
277 * NOTE erratum 0112 workaround #2
279 if ((desc->bEndpointAddress & USB_DIR_IN) == 0) {
280 tmp = BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT_ENABLE);
281 writel(tmp, &ep->regs->ep_irqenb);
283 enable_pciirqenb(ep);
287 tmp = desc->bEndpointAddress;
288 ep_dbg(dev, "enabled %s (ep%d%s-%s) %s max %04x\n",
289 _ep->name, tmp & 0x0f, DIR_STRING(tmp),
290 type_string(desc->bmAttributes),
291 ep->dma ? "dma" : "pio", max);
293 /* pci writes may still be posted */
294 spin_unlock_irqrestore(&dev->lock, flags);
298 dev_err(&ep->dev->pdev->dev, "%s: error=%d\n", __func__, ret);
302 static int handshake(u32 __iomem *ptr, u32 mask, u32 done, int usec)
308 if (result == ~(u32)0) /* "device unplugged" */
319 static const struct usb_ep_ops net2280_ep_ops;
321 static void ep_reset_228x(struct net2280_regs __iomem *regs,
322 struct net2280_ep *ep)
327 INIT_LIST_HEAD(&ep->queue);
329 usb_ep_set_maxpacket_limit(&ep->ep, ~0);
330 ep->ep.ops = &net2280_ep_ops;
332 /* disable the dma, irqs, endpoint... */
334 writel(0, &ep->dma->dmactl);
335 writel(BIT(DMA_SCATTER_GATHER_DONE_INTERRUPT) |
336 BIT(DMA_TRANSACTION_DONE_INTERRUPT) |
340 tmp = readl(®s->pciirqenb0);
341 tmp &= ~BIT(ep->num);
342 writel(tmp, ®s->pciirqenb0);
344 tmp = readl(®s->pciirqenb1);
345 tmp &= ~BIT((8 + ep->num)); /* completion */
346 writel(tmp, ®s->pciirqenb1);
348 writel(0, &ep->regs->ep_irqenb);
350 /* init to our chosen defaults, notably so that we NAK OUT
351 * packets until the driver queues a read (+note erratum 0112)
353 if (!ep->is_in || (ep->dev->quirks & PLX_2280)) {
354 tmp = BIT(SET_NAK_OUT_PACKETS_MODE) |
355 BIT(SET_NAK_OUT_PACKETS) |
356 BIT(CLEAR_EP_HIDE_STATUS_PHASE) |
357 BIT(CLEAR_INTERRUPT_MODE);
360 tmp = BIT(CLEAR_NAK_OUT_PACKETS_MODE) |
361 BIT(CLEAR_NAK_OUT_PACKETS) |
362 BIT(CLEAR_EP_HIDE_STATUS_PHASE) |
363 BIT(CLEAR_INTERRUPT_MODE);
367 tmp |= BIT(CLEAR_ENDPOINT_TOGGLE) |
368 BIT(CLEAR_ENDPOINT_HALT);
370 writel(tmp, &ep->regs->ep_rsp);
372 /* scrub most status bits, and flush any fifo state */
373 if (ep->dev->quirks & PLX_2280)
374 tmp = BIT(FIFO_OVERFLOW) |
379 writel(tmp | BIT(TIMEOUT) |
380 BIT(USB_STALL_SENT) |
381 BIT(USB_IN_NAK_SENT) |
382 BIT(USB_IN_ACK_RCVD) |
383 BIT(USB_OUT_PING_NAK_SENT) |
384 BIT(USB_OUT_ACK_SENT) |
386 BIT(SHORT_PACKET_OUT_DONE_INTERRUPT) |
387 BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT) |
388 BIT(DATA_PACKET_RECEIVED_INTERRUPT) |
389 BIT(DATA_PACKET_TRANSMITTED_INTERRUPT) |
390 BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
391 BIT(DATA_IN_TOKEN_INTERRUPT),
394 /* fifo size is handled separately */
397 static void ep_reset_338x(struct net2280_regs __iomem *regs,
398 struct net2280_ep *ep)
403 INIT_LIST_HEAD(&ep->queue);
405 usb_ep_set_maxpacket_limit(&ep->ep, ~0);
406 ep->ep.ops = &net2280_ep_ops;
408 /* disable the dma, irqs, endpoint... */
410 writel(0, &ep->dma->dmactl);
411 writel(BIT(DMA_ABORT_DONE_INTERRUPT) |
412 BIT(DMA_PAUSE_DONE_INTERRUPT) |
413 BIT(DMA_SCATTER_GATHER_DONE_INTERRUPT) |
414 BIT(DMA_TRANSACTION_DONE_INTERRUPT),
415 /* | BIT(DMA_ABORT), */
418 dmastat = readl(&ep->dma->dmastat);
419 if (dmastat == 0x5002) {
420 ep_warn(ep->dev, "The dmastat return = %x!!\n",
422 writel(0x5a, &ep->dma->dmastat);
425 tmp = readl(®s->pciirqenb0);
426 tmp &= ~BIT(ep_bit[ep->num]);
427 writel(tmp, ®s->pciirqenb0);
430 tmp = readl(®s->pciirqenb1);
431 tmp &= ~BIT((8 + ep->num)); /* completion */
432 writel(tmp, ®s->pciirqenb1);
435 writel(0, &ep->regs->ep_irqenb);
437 writel(BIT(SHORT_PACKET_OUT_DONE_INTERRUPT) |
438 BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT) |
440 BIT(DATA_PACKET_RECEIVED_INTERRUPT) |
441 BIT(DATA_PACKET_TRANSMITTED_INTERRUPT) |
442 BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
443 BIT(DATA_IN_TOKEN_INTERRUPT), &ep->regs->ep_stat);
446 static void nuke(struct net2280_ep *);
448 static int net2280_disable(struct usb_ep *_ep)
450 struct net2280_ep *ep;
453 ep = container_of(_ep, struct net2280_ep, ep);
454 if (!_ep || !ep->desc || _ep->name == ep0name) {
455 pr_err("%s: Invalid ep=%p or ep->desc\n", __func__, _ep);
458 spin_lock_irqsave(&ep->dev->lock, flags);
461 if (ep->dev->quirks & PLX_SUPERSPEED)
462 ep_reset_338x(ep->dev->regs, ep);
464 ep_reset_228x(ep->dev->regs, ep);
466 ep_vdbg(ep->dev, "disabled %s %s\n",
467 ep->dma ? "dma" : "pio", _ep->name);
469 /* synch memory views with the device */
470 (void)readl(&ep->cfg->ep_cfg);
472 if (!ep->dma && ep->num >= 1 && ep->num <= 4)
473 ep->dma = &ep->dev->dma[ep->num - 1];
475 spin_unlock_irqrestore(&ep->dev->lock, flags);
479 /*-------------------------------------------------------------------------*/
481 static struct usb_request
482 *net2280_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
484 struct net2280_ep *ep;
485 struct net2280_request *req;
488 pr_err("%s: Invalid ep\n", __func__);
491 ep = container_of(_ep, struct net2280_ep, ep);
493 req = kzalloc(sizeof(*req), gfp_flags);
497 INIT_LIST_HEAD(&req->queue);
499 /* this dma descriptor may be swapped with the previous dummy */
501 struct net2280_dma *td;
503 td = pci_pool_alloc(ep->dev->requests, gfp_flags,
509 td->dmacount = 0; /* not VALID */
510 td->dmadesc = td->dmaaddr;
516 static void net2280_free_request(struct usb_ep *_ep, struct usb_request *_req)
518 struct net2280_ep *ep;
519 struct net2280_request *req;
521 ep = container_of(_ep, struct net2280_ep, ep);
523 dev_err(&ep->dev->pdev->dev, "%s: Inavlid ep=%p or req=%p\n",
524 __func__, _ep, _req);
528 req = container_of(_req, struct net2280_request, req);
529 WARN_ON(!list_empty(&req->queue));
531 pci_pool_free(ep->dev->requests, req->td, req->td_dma);
535 /*-------------------------------------------------------------------------*/
537 /* load a packet into the fifo we use for usb IN transfers.
538 * works for all endpoints.
540 * NOTE: pio with ep-a..ep-d could stuff multiple packets into the fifo
541 * at a time, but this code is simpler because it knows it only writes
542 * one packet. ep-a..ep-d should use dma instead.
544 static void write_fifo(struct net2280_ep *ep, struct usb_request *req)
546 struct net2280_ep_regs __iomem *regs = ep->regs;
549 unsigned count, total;
551 /* INVARIANT: fifo is currently empty. (testable) */
554 buf = req->buf + req->actual;
556 total = req->length - req->actual;
562 /* write just one packet at a time */
563 count = ep->ep.maxpacket;
564 if (count > total) /* min() cannot be used on a bitfield */
567 ep_vdbg(ep->dev, "write %s fifo (IN) %d bytes%s req %p\n",
569 (count != ep->ep.maxpacket) ? " (short)" : "",
572 /* NOTE be careful if you try to align these. fifo lines
573 * should normally be full (4 bytes) and successive partial
574 * lines are ok only in certain cases.
576 tmp = get_unaligned((u32 *)buf);
578 writel(tmp, ®s->ep_data);
583 /* last fifo entry is "short" unless we wrote a full packet.
584 * also explicitly validate last word in (periodic) transfers
585 * when maxpacket is not a multiple of 4 bytes.
587 if (count || total < ep->ep.maxpacket) {
588 tmp = count ? get_unaligned((u32 *)buf) : count;
590 set_fifo_bytecount(ep, count & 0x03);
591 writel(tmp, ®s->ep_data);
594 /* pci writes may still be posted */
597 /* work around erratum 0106: PCI and USB race over the OUT fifo.
598 * caller guarantees chiprev 0100, out endpoint is NAKing, and
599 * there's no real data in the fifo.
601 * NOTE: also used in cases where that erratum doesn't apply:
602 * where the host wrote "too much" data to us.
604 static void out_flush(struct net2280_ep *ep)
609 statp = &ep->regs->ep_stat;
612 if (tmp & BIT(NAK_OUT_PACKETS)) {
613 ep_dbg(ep->dev, "%s %s %08x !NAK\n",
614 ep->ep.name, __func__, tmp);
615 writel(BIT(SET_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
618 writel(BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
619 BIT(DATA_PACKET_RECEIVED_INTERRUPT),
621 writel(BIT(FIFO_FLUSH), statp);
622 /* Make sure that stap is written */
625 if (tmp & BIT(DATA_OUT_PING_TOKEN_INTERRUPT) &&
626 /* high speed did bulk NYET; fifo isn't filling */
627 ep->dev->gadget.speed == USB_SPEED_FULL) {
630 usec = 50; /* 64 byte bulk/interrupt */
631 handshake(statp, BIT(USB_OUT_PING_NAK_SENT),
632 BIT(USB_OUT_PING_NAK_SENT), usec);
633 /* NAK done; now CLEAR_NAK_OUT_PACKETS is safe */
637 /* unload packet(s) from the fifo we use for usb OUT transfers.
638 * returns true iff the request completed, because of short packet
639 * or the request buffer having filled with full packets.
641 * for ep-a..ep-d this will read multiple packets out when they
642 * have been accepted.
644 static int read_fifo(struct net2280_ep *ep, struct net2280_request *req)
646 struct net2280_ep_regs __iomem *regs = ep->regs;
647 u8 *buf = req->req.buf + req->req.actual;
648 unsigned count, tmp, is_short;
649 unsigned cleanup = 0, prevent = 0;
651 /* erratum 0106 ... packets coming in during fifo reads might
652 * be incompletely rejected. not all cases have workarounds.
654 if (ep->dev->chiprev == 0x0100 &&
655 ep->dev->gadget.speed == USB_SPEED_FULL) {
657 tmp = readl(&ep->regs->ep_stat);
658 if ((tmp & BIT(NAK_OUT_PACKETS)))
660 else if ((tmp & BIT(FIFO_FULL))) {
661 start_out_naking(ep);
664 /* else: hope we don't see the problem */
667 /* never overflow the rx buffer. the fifo reads packets until
668 * it sees a short one; we might not be ready for them all.
671 count = readl(®s->ep_avail);
672 if (unlikely(count == 0)) {
674 tmp = readl(&ep->regs->ep_stat);
675 count = readl(®s->ep_avail);
676 /* handled that data already? */
677 if (count == 0 && (tmp & BIT(NAK_OUT_PACKETS)) == 0)
681 tmp = req->req.length - req->req.actual;
683 /* as with DMA, data overflow gets flushed */
684 if ((tmp % ep->ep.maxpacket) != 0) {
686 "%s out fifo %d bytes, expected %d\n",
687 ep->ep.name, count, tmp);
688 req->req.status = -EOVERFLOW;
690 /* NAK_OUT_PACKETS will be set, so flushing is safe;
691 * the next read will start with the next packet
693 } /* else it's a ZLP, no worries */
696 req->req.actual += count;
698 is_short = (count == 0) || ((count % ep->ep.maxpacket) != 0);
700 ep_vdbg(ep->dev, "read %s fifo (OUT) %d bytes%s%s%s req %p %d/%d\n",
701 ep->ep.name, count, is_short ? " (short)" : "",
702 cleanup ? " flush" : "", prevent ? " nak" : "",
703 req, req->req.actual, req->req.length);
706 tmp = readl(®s->ep_data);
708 put_unaligned(tmp, (u32 *)buf);
713 tmp = readl(®s->ep_data);
714 /* LE conversion is implicit here: */
723 writel(BIT(CLEAR_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
724 (void) readl(&ep->regs->ep_rsp);
727 return is_short || ((req->req.actual == req->req.length) &&
731 /* fill out dma descriptor to match a given request */
732 static void fill_dma_desc(struct net2280_ep *ep,
733 struct net2280_request *req, int valid)
735 struct net2280_dma *td = req->td;
736 u32 dmacount = req->req.length;
738 /* don't let DMA continue after a short OUT packet,
739 * so overruns can't affect the next transfer.
740 * in case of overruns on max-size packets, we can't
741 * stop the fifo from filling but we can flush it.
744 dmacount |= BIT(DMA_DIRECTION);
745 if ((!ep->is_in && (dmacount % ep->ep.maxpacket) != 0) ||
746 !(ep->dev->quirks & PLX_2280))
747 dmacount |= BIT(END_OF_CHAIN);
751 dmacount |= BIT(VALID_BIT);
752 dmacount |= BIT(DMA_DONE_INTERRUPT_ENABLE);
754 /* td->dmadesc = previously set by caller */
755 td->dmaaddr = cpu_to_le32 (req->req.dma);
757 /* 2280 may be polling VALID_BIT through ep->dma->dmadesc */
759 td->dmacount = cpu_to_le32(dmacount);
762 static const u32 dmactl_default =
763 BIT(DMA_SCATTER_GATHER_DONE_INTERRUPT) |
764 BIT(DMA_CLEAR_COUNT_ENABLE) |
765 /* erratum 0116 workaround part 1 (use POLLING) */
766 (POLL_100_USEC << DESCRIPTOR_POLLING_RATE) |
767 BIT(DMA_VALID_BIT_POLLING_ENABLE) |
768 BIT(DMA_VALID_BIT_ENABLE) |
769 BIT(DMA_SCATTER_GATHER_ENABLE) |
770 /* erratum 0116 workaround part 2 (no AUTOSTART) */
773 static inline void spin_stop_dma(struct net2280_dma_regs __iomem *dma)
775 handshake(&dma->dmactl, BIT(DMA_ENABLE), 0, 50);
778 static inline void stop_dma(struct net2280_dma_regs __iomem *dma)
780 writel(readl(&dma->dmactl) & ~BIT(DMA_ENABLE), &dma->dmactl);
784 static void start_queue(struct net2280_ep *ep, u32 dmactl, u32 td_dma)
786 struct net2280_dma_regs __iomem *dma = ep->dma;
787 unsigned int tmp = BIT(VALID_BIT) | (ep->is_in << DMA_DIRECTION);
789 if (!(ep->dev->quirks & PLX_2280))
790 tmp |= BIT(END_OF_CHAIN);
792 writel(tmp, &dma->dmacount);
793 writel(readl(&dma->dmastat), &dma->dmastat);
795 writel(td_dma, &dma->dmadesc);
796 if (ep->dev->quirks & PLX_SUPERSPEED)
797 dmactl |= BIT(DMA_REQUEST_OUTSTANDING);
798 writel(dmactl, &dma->dmactl);
800 /* erratum 0116 workaround part 3: pci arbiter away from net2280 */
801 (void) readl(&ep->dev->pci->pcimstctl);
803 writel(BIT(DMA_START), &dma->dmastat);
809 static void start_dma(struct net2280_ep *ep, struct net2280_request *req)
812 struct net2280_dma_regs __iomem *dma = ep->dma;
814 /* FIXME can't use DMA for ZLPs */
816 /* on this path we "know" there's no dma active (yet) */
817 WARN_ON(readl(&dma->dmactl) & BIT(DMA_ENABLE));
818 writel(0, &ep->dma->dmactl);
820 /* previous OUT packet might have been short */
821 if (!ep->is_in && (readl(&ep->regs->ep_stat) &
822 BIT(NAK_OUT_PACKETS))) {
823 writel(BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT),
826 tmp = readl(&ep->regs->ep_avail);
828 writel(readl(&dma->dmastat), &dma->dmastat);
830 /* transfer all/some fifo data */
831 writel(req->req.dma, &dma->dmaaddr);
832 tmp = min(tmp, req->req.length);
834 /* dma irq, faking scatterlist status */
835 req->td->dmacount = cpu_to_le32(req->req.length - tmp);
836 writel(BIT(DMA_DONE_INTERRUPT_ENABLE) | tmp,
838 req->td->dmadesc = 0;
841 writel(BIT(DMA_ENABLE), &dma->dmactl);
842 writel(BIT(DMA_START), &dma->dmastat);
847 tmp = dmactl_default;
849 /* force packet boundaries between dma requests, but prevent the
850 * controller from automagically writing a last "short" packet
851 * (zero length) unless the driver explicitly said to do that.
854 if (likely((req->req.length % ep->ep.maxpacket) ||
856 tmp |= BIT(DMA_FIFO_VALIDATE);
857 ep->in_fifo_validate = 1;
859 ep->in_fifo_validate = 0;
862 /* init req->td, pointing to the current dummy */
863 req->td->dmadesc = cpu_to_le32 (ep->td_dma);
864 fill_dma_desc(ep, req, 1);
866 req->td->dmacount |= cpu_to_le32(BIT(END_OF_CHAIN));
868 start_queue(ep, tmp, req->td_dma);
872 queue_dma(struct net2280_ep *ep, struct net2280_request *req, int valid)
874 struct net2280_dma *end;
877 /* swap new dummy for old, link; fill and maybe activate */
883 ep->td_dma = req->td_dma;
886 end->dmadesc = cpu_to_le32 (ep->td_dma);
888 fill_dma_desc(ep, req, valid);
892 done(struct net2280_ep *ep, struct net2280_request *req, int status)
895 unsigned stopped = ep->stopped;
897 list_del_init(&req->queue);
899 if (req->req.status == -EINPROGRESS)
900 req->req.status = status;
902 status = req->req.status;
906 usb_gadget_unmap_request(&dev->gadget, &req->req, ep->is_in);
908 if (status && status != -ESHUTDOWN)
909 ep_vdbg(dev, "complete %s req %p stat %d len %u/%u\n",
910 ep->ep.name, &req->req, status,
911 req->req.actual, req->req.length);
913 /* don't modify queue heads during completion callback */
915 spin_unlock(&dev->lock);
916 usb_gadget_giveback_request(&ep->ep, &req->req);
917 spin_lock(&dev->lock);
918 ep->stopped = stopped;
921 /*-------------------------------------------------------------------------*/
924 net2280_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
926 struct net2280_request *req;
927 struct net2280_ep *ep;
932 /* we always require a cpu-view buffer, so that we can
933 * always use pio (as fallback or whatever).
935 ep = container_of(_ep, struct net2280_ep, ep);
936 if (!_ep || (!ep->desc && ep->num != 0)) {
937 pr_err("%s: Invalid ep=%p or ep->desc\n", __func__, _ep);
940 req = container_of(_req, struct net2280_request, req);
941 if (!_req || !_req->complete || !_req->buf ||
942 !list_empty(&req->queue)) {
946 if (_req->length > (~0 & DMA_BYTE_COUNT_MASK)) {
951 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
956 /* FIXME implement PIO fallback for ZLPs with DMA */
957 if (ep->dma && _req->length == 0) {
962 /* set up dma mapping in case the caller didn't */
964 ret = usb_gadget_map_request(&dev->gadget, _req,
970 ep_vdbg(dev, "%s queue req %p, len %d buf %p\n",
971 _ep->name, _req, _req->length, _req->buf);
973 spin_lock_irqsave(&dev->lock, flags);
975 _req->status = -EINPROGRESS;
978 /* kickstart this i/o queue? */
979 if (list_empty(&ep->queue) && !ep->stopped &&
980 !((dev->quirks & PLX_SUPERSPEED) && ep->dma &&
981 (readl(&ep->regs->ep_rsp) & BIT(CLEAR_ENDPOINT_HALT)))) {
983 /* use DMA if the endpoint supports it, else pio */
987 /* maybe there's no control data, just status ack */
988 if (ep->num == 0 && _req->length == 0) {
991 ep_vdbg(dev, "%s status ack\n", ep->ep.name);
995 /* PIO ... stuff the fifo, or unblock it. */
997 write_fifo(ep, _req);
998 else if (list_empty(&ep->queue)) {
1001 /* OUT FIFO might have packet(s) buffered */
1002 s = readl(&ep->regs->ep_stat);
1003 if ((s & BIT(FIFO_EMPTY)) == 0) {
1004 /* note: _req->short_not_ok is
1005 * ignored here since PIO _always_
1006 * stops queue advance here, and
1007 * _req->status doesn't change for
1008 * short reads (only _req->actual)
1010 if (read_fifo(ep, req) &&
1014 /* don't queue it */
1016 } else if (read_fifo(ep, req) &&
1021 s = readl(&ep->regs->ep_stat);
1024 /* don't NAK, let the fifo fill */
1025 if (req && (s & BIT(NAK_OUT_PACKETS)))
1026 writel(BIT(CLEAR_NAK_OUT_PACKETS),
1031 } else if (ep->dma) {
1037 /* preventing magic zlps is per-engine state, not
1038 * per-transfer; irq logic must recover hiccups.
1040 expect = likely(req->req.zero ||
1041 (req->req.length % ep->ep.maxpacket));
1042 if (expect != ep->in_fifo_validate)
1045 queue_dma(ep, req, valid);
1047 } /* else the irq handler advances the queue. */
1051 list_add_tail(&req->queue, &ep->queue);
1053 spin_unlock_irqrestore(&dev->lock, flags);
1055 /* pci writes may still be posted */
1059 dev_err(&ep->dev->pdev->dev, "%s: error=%d\n", __func__, ret);
1064 dma_done(struct net2280_ep *ep, struct net2280_request *req, u32 dmacount,
1067 req->req.actual = req->req.length - (DMA_BYTE_COUNT_MASK & dmacount);
1068 done(ep, req, status);
1071 static void scan_dma_completions(struct net2280_ep *ep)
1073 /* only look at descriptors that were "naturally" retired,
1074 * so fifo and list head state won't matter
1076 while (!list_empty(&ep->queue)) {
1077 struct net2280_request *req;
1080 req = list_entry(ep->queue.next,
1081 struct net2280_request, queue);
1085 tmp = le32_to_cpup(&req->td->dmacount);
1086 if ((tmp & BIT(VALID_BIT)) != 0)
1089 /* SHORT_PACKET_TRANSFERRED_INTERRUPT handles "usb-short"
1090 * cases where DMA must be aborted; this code handles
1091 * all non-abort DMA completions.
1093 if (unlikely(req->td->dmadesc == 0)) {
1095 tmp = readl(&ep->dma->dmacount);
1096 if (tmp & DMA_BYTE_COUNT_MASK)
1098 /* single transfer mode */
1099 dma_done(ep, req, tmp, 0);
1101 } else if (!ep->is_in &&
1102 (req->req.length % ep->ep.maxpacket) &&
1103 !(ep->dev->quirks & PLX_SUPERSPEED)) {
1105 tmp = readl(&ep->regs->ep_stat);
1106 /* AVOID TROUBLE HERE by not issuing short reads from
1107 * your gadget driver. That helps avoids errata 0121,
1108 * 0122, and 0124; not all cases trigger the warning.
1110 if ((tmp & BIT(NAK_OUT_PACKETS)) == 0) {
1111 ep_warn(ep->dev, "%s lost packet sync!\n",
1113 req->req.status = -EOVERFLOW;
1115 tmp = readl(&ep->regs->ep_avail);
1117 /* fifo gets flushed later */
1118 ep->out_overflow = 1;
1120 "%s dma, discard %d len %d\n",
1123 req->req.status = -EOVERFLOW;
1127 dma_done(ep, req, tmp, 0);
1131 static void restart_dma(struct net2280_ep *ep)
1133 struct net2280_request *req;
1137 req = list_entry(ep->queue.next, struct net2280_request, queue);
1142 static void abort_dma(struct net2280_ep *ep)
1144 /* abort the current transfer */
1145 if (likely(!list_empty(&ep->queue))) {
1146 /* FIXME work around errata 0121, 0122, 0124 */
1147 writel(BIT(DMA_ABORT), &ep->dma->dmastat);
1148 spin_stop_dma(ep->dma);
1151 scan_dma_completions(ep);
1154 /* dequeue ALL requests */
1155 static void nuke(struct net2280_ep *ep)
1157 struct net2280_request *req;
1159 /* called with spinlock held */
1163 while (!list_empty(&ep->queue)) {
1164 req = list_entry(ep->queue.next,
1165 struct net2280_request,
1167 done(ep, req, -ESHUTDOWN);
1171 /* dequeue JUST ONE request */
1172 static int net2280_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1174 struct net2280_ep *ep;
1175 struct net2280_request *req;
1176 unsigned long flags;
1180 ep = container_of(_ep, struct net2280_ep, ep);
1181 if (!_ep || (!ep->desc && ep->num != 0) || !_req) {
1182 pr_err("%s: Invalid ep=%p or ep->desc or req=%p\n",
1183 __func__, _ep, _req);
1187 spin_lock_irqsave(&ep->dev->lock, flags);
1188 stopped = ep->stopped;
1190 /* quiesce dma while we patch the queue */
1194 dmactl = readl(&ep->dma->dmactl);
1195 /* WARNING erratum 0127 may kick in ... */
1197 scan_dma_completions(ep);
1200 /* make sure it's still queued on this endpoint */
1201 list_for_each_entry(req, &ep->queue, queue) {
1202 if (&req->req == _req)
1205 if (&req->req != _req) {
1206 spin_unlock_irqrestore(&ep->dev->lock, flags);
1207 dev_err(&ep->dev->pdev->dev, "%s: Request mismatch\n",
1212 /* queue head may be partially complete. */
1213 if (ep->queue.next == &req->queue) {
1215 ep_dbg(ep->dev, "unlink (%s) dma\n", _ep->name);
1216 _req->status = -ECONNRESET;
1218 if (likely(ep->queue.next == &req->queue)) {
1219 /* NOTE: misreports single-transfer mode*/
1220 req->td->dmacount = 0; /* invalidate */
1222 readl(&ep->dma->dmacount),
1226 ep_dbg(ep->dev, "unlink (%s) pio\n", _ep->name);
1227 done(ep, req, -ECONNRESET);
1233 done(ep, req, -ECONNRESET);
1234 ep->stopped = stopped;
1237 /* turn off dma on inactive queues */
1238 if (list_empty(&ep->queue))
1240 else if (!ep->stopped) {
1241 /* resume current request, or start new one */
1243 writel(dmactl, &ep->dma->dmactl);
1245 start_dma(ep, list_entry(ep->queue.next,
1246 struct net2280_request, queue));
1250 spin_unlock_irqrestore(&ep->dev->lock, flags);
1254 /*-------------------------------------------------------------------------*/
1256 static int net2280_fifo_status(struct usb_ep *_ep);
1259 net2280_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged)
1261 struct net2280_ep *ep;
1262 unsigned long flags;
1265 ep = container_of(_ep, struct net2280_ep, ep);
1266 if (!_ep || (!ep->desc && ep->num != 0)) {
1267 pr_err("%s: Invalid ep=%p or ep->desc\n", __func__, _ep);
1270 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN) {
1271 retval = -ESHUTDOWN;
1274 if (ep->desc /* not ep0 */ && (ep->desc->bmAttributes & 0x03)
1275 == USB_ENDPOINT_XFER_ISOC) {
1280 spin_lock_irqsave(&ep->dev->lock, flags);
1281 if (!list_empty(&ep->queue)) {
1284 } else if (ep->is_in && value && net2280_fifo_status(_ep) != 0) {
1288 ep_vdbg(ep->dev, "%s %s %s\n", _ep->name,
1289 value ? "set" : "clear",
1290 wedged ? "wedge" : "halt");
1291 /* set/clear, then synch memory views with the device */
1294 ep->dev->protocol_stall = 1;
1301 if (ep->dev->quirks & PLX_SUPERSPEED &&
1302 !list_empty(&ep->queue) && ep->td_dma)
1306 (void) readl(&ep->regs->ep_rsp);
1308 spin_unlock_irqrestore(&ep->dev->lock, flags);
1313 spin_unlock_irqrestore(&ep->dev->lock, flags);
1315 dev_err(&ep->dev->pdev->dev, "%s: error=%d\n", __func__, retval);
1319 static int net2280_set_halt(struct usb_ep *_ep, int value)
1321 return net2280_set_halt_and_wedge(_ep, value, 0);
1324 static int net2280_set_wedge(struct usb_ep *_ep)
1326 if (!_ep || _ep->name == ep0name) {
1327 pr_err("%s: Invalid ep=%p or ep0\n", __func__, _ep);
1330 return net2280_set_halt_and_wedge(_ep, 1, 1);
1333 static int net2280_fifo_status(struct usb_ep *_ep)
1335 struct net2280_ep *ep;
1338 ep = container_of(_ep, struct net2280_ep, ep);
1339 if (!_ep || (!ep->desc && ep->num != 0)) {
1340 pr_err("%s: Invalid ep=%p or ep->desc\n", __func__, _ep);
1343 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN) {
1344 dev_err(&ep->dev->pdev->dev,
1345 "%s: Invalid driver=%p or speed=%d\n",
1346 __func__, ep->dev->driver, ep->dev->gadget.speed);
1350 avail = readl(&ep->regs->ep_avail) & (BIT(12) - 1);
1351 if (avail > ep->fifo_size) {
1352 dev_err(&ep->dev->pdev->dev, "%s: Fifo overflow\n", __func__);
1356 avail = ep->fifo_size - avail;
1360 static void net2280_fifo_flush(struct usb_ep *_ep)
1362 struct net2280_ep *ep;
1364 ep = container_of(_ep, struct net2280_ep, ep);
1365 if (!_ep || (!ep->desc && ep->num != 0)) {
1366 pr_err("%s: Invalid ep=%p or ep->desc\n", __func__, _ep);
1369 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN) {
1370 dev_err(&ep->dev->pdev->dev,
1371 "%s: Invalid driver=%p or speed=%d\n",
1372 __func__, ep->dev->driver, ep->dev->gadget.speed);
1376 writel(BIT(FIFO_FLUSH), &ep->regs->ep_stat);
1377 (void) readl(&ep->regs->ep_rsp);
1380 static const struct usb_ep_ops net2280_ep_ops = {
1381 .enable = net2280_enable,
1382 .disable = net2280_disable,
1384 .alloc_request = net2280_alloc_request,
1385 .free_request = net2280_free_request,
1387 .queue = net2280_queue,
1388 .dequeue = net2280_dequeue,
1390 .set_halt = net2280_set_halt,
1391 .set_wedge = net2280_set_wedge,
1392 .fifo_status = net2280_fifo_status,
1393 .fifo_flush = net2280_fifo_flush,
1396 /*-------------------------------------------------------------------------*/
1398 static int net2280_get_frame(struct usb_gadget *_gadget)
1400 struct net2280 *dev;
1401 unsigned long flags;
1406 dev = container_of(_gadget, struct net2280, gadget);
1407 spin_lock_irqsave(&dev->lock, flags);
1408 retval = get_idx_reg(dev->regs, REG_FRAME) & 0x03ff;
1409 spin_unlock_irqrestore(&dev->lock, flags);
1413 static int net2280_wakeup(struct usb_gadget *_gadget)
1415 struct net2280 *dev;
1417 unsigned long flags;
1421 dev = container_of(_gadget, struct net2280, gadget);
1423 spin_lock_irqsave(&dev->lock, flags);
1424 tmp = readl(&dev->usb->usbctl);
1425 if (tmp & BIT(DEVICE_REMOTE_WAKEUP_ENABLE))
1426 writel(BIT(GENERATE_RESUME), &dev->usb->usbstat);
1427 spin_unlock_irqrestore(&dev->lock, flags);
1429 /* pci writes may still be posted */
1433 static int net2280_set_selfpowered(struct usb_gadget *_gadget, int value)
1435 struct net2280 *dev;
1437 unsigned long flags;
1441 dev = container_of(_gadget, struct net2280, gadget);
1443 spin_lock_irqsave(&dev->lock, flags);
1444 tmp = readl(&dev->usb->usbctl);
1446 tmp |= BIT(SELF_POWERED_STATUS);
1447 _gadget->is_selfpowered = 1;
1449 tmp &= ~BIT(SELF_POWERED_STATUS);
1450 _gadget->is_selfpowered = 0;
1452 writel(tmp, &dev->usb->usbctl);
1453 spin_unlock_irqrestore(&dev->lock, flags);
1458 static int net2280_pullup(struct usb_gadget *_gadget, int is_on)
1460 struct net2280 *dev;
1462 unsigned long flags;
1466 dev = container_of(_gadget, struct net2280, gadget);
1468 spin_lock_irqsave(&dev->lock, flags);
1469 tmp = readl(&dev->usb->usbctl);
1470 dev->softconnect = (is_on != 0);
1472 tmp |= BIT(USB_DETECT_ENABLE);
1474 tmp &= ~BIT(USB_DETECT_ENABLE);
1475 writel(tmp, &dev->usb->usbctl);
1476 spin_unlock_irqrestore(&dev->lock, flags);
1481 static int net2280_start(struct usb_gadget *_gadget,
1482 struct usb_gadget_driver *driver);
1483 static int net2280_stop(struct usb_gadget *_gadget);
1485 static const struct usb_gadget_ops net2280_ops = {
1486 .get_frame = net2280_get_frame,
1487 .wakeup = net2280_wakeup,
1488 .set_selfpowered = net2280_set_selfpowered,
1489 .pullup = net2280_pullup,
1490 .udc_start = net2280_start,
1491 .udc_stop = net2280_stop,
1494 /*-------------------------------------------------------------------------*/
1496 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1498 /* FIXME move these into procfs, and use seq_file.
1499 * Sysfs _still_ doesn't behave for arbitrarily sized files,
1500 * and also doesn't help products using this with 2.4 kernels.
1503 /* "function" sysfs attribute */
1504 static ssize_t function_show(struct device *_dev, struct device_attribute *attr,
1507 struct net2280 *dev = dev_get_drvdata(_dev);
1509 if (!dev->driver || !dev->driver->function ||
1510 strlen(dev->driver->function) > PAGE_SIZE)
1512 return scnprintf(buf, PAGE_SIZE, "%s\n", dev->driver->function);
1514 static DEVICE_ATTR_RO(function);
1516 static ssize_t registers_show(struct device *_dev,
1517 struct device_attribute *attr, char *buf)
1519 struct net2280 *dev;
1522 unsigned long flags;
1527 dev = dev_get_drvdata(_dev);
1530 spin_lock_irqsave(&dev->lock, flags);
1533 s = dev->driver->driver.name;
1537 /* Main Control Registers */
1538 t = scnprintf(next, size, "%s version " DRIVER_VERSION
1539 ", chiprev %04x\n\n"
1540 "devinit %03x fifoctl %08x gadget '%s'\n"
1541 "pci irqenb0 %02x irqenb1 %08x "
1542 "irqstat0 %04x irqstat1 %08x\n",
1543 driver_name, dev->chiprev,
1544 readl(&dev->regs->devinit),
1545 readl(&dev->regs->fifoctl),
1547 readl(&dev->regs->pciirqenb0),
1548 readl(&dev->regs->pciirqenb1),
1549 readl(&dev->regs->irqstat0),
1550 readl(&dev->regs->irqstat1));
1554 /* USB Control Registers */
1555 t1 = readl(&dev->usb->usbctl);
1556 t2 = readl(&dev->usb->usbstat);
1557 if (t1 & BIT(VBUS_PIN)) {
1558 if (t2 & BIT(HIGH_SPEED))
1560 else if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1564 /* full speed bit (6) not working?? */
1567 t = scnprintf(next, size,
1568 "stdrsp %08x usbctl %08x usbstat %08x "
1569 "addr 0x%02x (%s)\n",
1570 readl(&dev->usb->stdrsp), t1, t2,
1571 readl(&dev->usb->ouraddr), s);
1575 /* PCI Master Control Registers */
1577 /* DMA Control Registers */
1579 /* Configurable EP Control Registers */
1580 for (i = 0; i < dev->n_ep; i++) {
1581 struct net2280_ep *ep;
1587 t1 = readl(&ep->cfg->ep_cfg);
1588 t2 = readl(&ep->regs->ep_rsp) & 0xff;
1589 t = scnprintf(next, size,
1590 "\n%s\tcfg %05x rsp (%02x) %s%s%s%s%s%s%s%s"
1592 ep->ep.name, t1, t2,
1593 (t2 & BIT(CLEAR_NAK_OUT_PACKETS))
1595 (t2 & BIT(CLEAR_EP_HIDE_STATUS_PHASE))
1597 (t2 & BIT(CLEAR_EP_FORCE_CRC_ERROR))
1599 (t2 & BIT(CLEAR_INTERRUPT_MODE))
1600 ? "interrupt " : "",
1601 (t2 & BIT(CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE))
1603 (t2 & BIT(CLEAR_NAK_OUT_PACKETS_MODE))
1605 (t2 & BIT(CLEAR_ENDPOINT_TOGGLE))
1606 ? "DATA1 " : "DATA0 ",
1607 (t2 & BIT(CLEAR_ENDPOINT_HALT))
1609 readl(&ep->regs->ep_irqenb));
1613 t = scnprintf(next, size,
1614 "\tstat %08x avail %04x "
1616 readl(&ep->regs->ep_stat),
1617 readl(&ep->regs->ep_avail),
1618 t1 & 0x0f, DIR_STRING(t1),
1619 type_string(t1 >> 8),
1620 ep->stopped ? "*" : "");
1627 t = scnprintf(next, size,
1628 " dma\tctl %08x stat %08x count %08x\n"
1629 "\taddr %08x desc %08x\n",
1630 readl(&ep->dma->dmactl),
1631 readl(&ep->dma->dmastat),
1632 readl(&ep->dma->dmacount),
1633 readl(&ep->dma->dmaaddr),
1634 readl(&ep->dma->dmadesc));
1640 /* Indexed Registers (none yet) */
1643 t = scnprintf(next, size, "\nirqs: ");
1646 for (i = 0; i < dev->n_ep; i++) {
1647 struct net2280_ep *ep;
1652 t = scnprintf(next, size, " %s/%lu", ep->ep.name, ep->irqs);
1657 t = scnprintf(next, size, "\n");
1661 spin_unlock_irqrestore(&dev->lock, flags);
1663 return PAGE_SIZE - size;
1665 static DEVICE_ATTR_RO(registers);
1667 static ssize_t queues_show(struct device *_dev, struct device_attribute *attr,
1670 struct net2280 *dev;
1673 unsigned long flags;
1676 dev = dev_get_drvdata(_dev);
1679 spin_lock_irqsave(&dev->lock, flags);
1681 for (i = 0; i < dev->n_ep; i++) {
1682 struct net2280_ep *ep = &dev->ep[i];
1683 struct net2280_request *req;
1687 const struct usb_endpoint_descriptor *d;
1692 t = d->bEndpointAddress;
1693 t = scnprintf(next, size,
1694 "\n%s (ep%d%s-%s) max %04x %s fifo %d\n",
1695 ep->ep.name, t & USB_ENDPOINT_NUMBER_MASK,
1696 (t & USB_DIR_IN) ? "in" : "out",
1697 type_string(d->bmAttributes),
1698 usb_endpoint_maxp(d) & 0x1fff,
1699 ep->dma ? "dma" : "pio", ep->fifo_size
1701 } else /* ep0 should only have one transfer queued */
1702 t = scnprintf(next, size, "ep0 max 64 pio %s\n",
1703 ep->is_in ? "in" : "out");
1704 if (t <= 0 || t > size)
1709 if (list_empty(&ep->queue)) {
1710 t = scnprintf(next, size, "\t(nothing queued)\n");
1711 if (t <= 0 || t > size)
1717 list_for_each_entry(req, &ep->queue, queue) {
1718 if (ep->dma && req->td_dma == readl(&ep->dma->dmadesc))
1719 t = scnprintf(next, size,
1720 "\treq %p len %d/%d "
1721 "buf %p (dmacount %08x)\n",
1722 &req->req, req->req.actual,
1723 req->req.length, req->req.buf,
1724 readl(&ep->dma->dmacount));
1726 t = scnprintf(next, size,
1727 "\treq %p len %d/%d buf %p\n",
1728 &req->req, req->req.actual,
1729 req->req.length, req->req.buf);
1730 if (t <= 0 || t > size)
1736 struct net2280_dma *td;
1739 t = scnprintf(next, size, "\t td %08x "
1740 " count %08x buf %08x desc %08x\n",
1742 le32_to_cpu(td->dmacount),
1743 le32_to_cpu(td->dmaaddr),
1744 le32_to_cpu(td->dmadesc));
1745 if (t <= 0 || t > size)
1754 spin_unlock_irqrestore(&dev->lock, flags);
1755 return PAGE_SIZE - size;
1757 static DEVICE_ATTR_RO(queues);
1762 #define device_create_file(a, b) (0)
1763 #define device_remove_file(a, b) do { } while (0)
1767 /*-------------------------------------------------------------------------*/
1769 /* another driver-specific mode might be a request type doing dma
1770 * to/from another device fifo instead of to/from memory.
1773 static void set_fifo_mode(struct net2280 *dev, int mode)
1775 /* keeping high bits preserves BAR2 */
1776 writel((0xffff << PCI_BASE2_RANGE) | mode, &dev->regs->fifoctl);
1778 /* always ep-{a,b,e,f} ... maybe not ep-c or ep-d */
1779 INIT_LIST_HEAD(&dev->gadget.ep_list);
1780 list_add_tail(&dev->ep[1].ep.ep_list, &dev->gadget.ep_list);
1781 list_add_tail(&dev->ep[2].ep.ep_list, &dev->gadget.ep_list);
1784 list_add_tail(&dev->ep[3].ep.ep_list, &dev->gadget.ep_list);
1785 list_add_tail(&dev->ep[4].ep.ep_list, &dev->gadget.ep_list);
1786 dev->ep[1].fifo_size = dev->ep[2].fifo_size = 1024;
1789 dev->ep[1].fifo_size = dev->ep[2].fifo_size = 2048;
1792 list_add_tail(&dev->ep[3].ep.ep_list, &dev->gadget.ep_list);
1793 dev->ep[1].fifo_size = 2048;
1794 dev->ep[2].fifo_size = 1024;
1797 /* fifo sizes for ep0, ep-c, ep-d, ep-e, and ep-f never change */
1798 list_add_tail(&dev->ep[5].ep.ep_list, &dev->gadget.ep_list);
1799 list_add_tail(&dev->ep[6].ep.ep_list, &dev->gadget.ep_list);
1802 static void defect7374_disable_data_eps(struct net2280 *dev)
1805 * For Defect 7374, disable data EPs (and more):
1806 * - This phase undoes the earlier phase of the Defect 7374 workaround,
1807 * returing ep regs back to normal.
1809 struct net2280_ep *ep;
1811 unsigned char ep_sel;
1814 for (i = 1; i < 5; i++) {
1816 writel(0, &ep->cfg->ep_cfg);
1819 /* CSROUT, CSRIN, PCIOUT, PCIIN, STATIN, RCIN */
1820 for (i = 0; i < 6; i++)
1821 writel(0, &dev->dep[i].dep_cfg);
1823 for (ep_sel = 0; ep_sel <= 21; ep_sel++) {
1824 /* Select an endpoint for subsequent operations: */
1825 tmp_reg = readl(&dev->plregs->pl_ep_ctrl);
1826 writel(((tmp_reg & ~0x1f) | ep_sel), &dev->plregs->pl_ep_ctrl);
1828 if (ep_sel < 2 || (ep_sel > 9 && ep_sel < 14) ||
1829 ep_sel == 18 || ep_sel == 20)
1832 /* Change settings on some selected endpoints */
1833 tmp_reg = readl(&dev->plregs->pl_ep_cfg_4);
1834 tmp_reg &= ~BIT(NON_CTRL_IN_TOLERATE_BAD_DIR);
1835 writel(tmp_reg, &dev->plregs->pl_ep_cfg_4);
1836 tmp_reg = readl(&dev->plregs->pl_ep_ctrl);
1837 tmp_reg |= BIT(EP_INITIALIZED);
1838 writel(tmp_reg, &dev->plregs->pl_ep_ctrl);
1842 static void defect7374_enable_data_eps_zero(struct net2280 *dev)
1844 u32 tmp = 0, tmp_reg;
1847 unsigned char ep_sel;
1849 scratch = get_idx_reg(dev->regs, SCRATCH);
1851 WARN_ON((scratch & (0xf << DEFECT7374_FSM_FIELD))
1852 == DEFECT7374_FSM_SS_CONTROL_READ);
1854 scratch &= ~(0xf << DEFECT7374_FSM_FIELD);
1856 ep_warn(dev, "Operate Defect 7374 workaround soft this time");
1857 ep_warn(dev, "It will operate on cold-reboot and SS connect");
1860 tmp = ((0 << ENDPOINT_NUMBER) | BIT(ENDPOINT_DIRECTION) |
1861 (2 << OUT_ENDPOINT_TYPE) | (2 << IN_ENDPOINT_TYPE) |
1862 ((dev->enhanced_mode) ?
1863 BIT(OUT_ENDPOINT_ENABLE) : BIT(ENDPOINT_ENABLE)) |
1864 BIT(IN_ENDPOINT_ENABLE));
1866 for (i = 1; i < 5; i++)
1867 writel(tmp, &dev->ep[i].cfg->ep_cfg);
1869 /* CSRIN, PCIIN, STATIN, RCIN*/
1870 tmp = ((0 << ENDPOINT_NUMBER) | BIT(ENDPOINT_ENABLE));
1871 writel(tmp, &dev->dep[1].dep_cfg);
1872 writel(tmp, &dev->dep[3].dep_cfg);
1873 writel(tmp, &dev->dep[4].dep_cfg);
1874 writel(tmp, &dev->dep[5].dep_cfg);
1876 /*Implemented for development and debug.
1877 * Can be refined/tuned later.*/
1878 for (ep_sel = 0; ep_sel <= 21; ep_sel++) {
1879 /* Select an endpoint for subsequent operations: */
1880 tmp_reg = readl(&dev->plregs->pl_ep_ctrl);
1881 writel(((tmp_reg & ~0x1f) | ep_sel),
1882 &dev->plregs->pl_ep_ctrl);
1886 (readl(&dev->plregs->pl_ep_ctrl) |
1887 BIT(CLEAR_ACK_ERROR_CODE) | 0);
1888 writel(tmp, &dev->plregs->pl_ep_ctrl);
1892 if (ep_sel == 0 || (ep_sel > 9 && ep_sel < 14) ||
1893 ep_sel == 18 || ep_sel == 20)
1896 tmp = (readl(&dev->plregs->pl_ep_cfg_4) |
1897 BIT(NON_CTRL_IN_TOLERATE_BAD_DIR) | 0);
1898 writel(tmp, &dev->plregs->pl_ep_cfg_4);
1900 tmp = readl(&dev->plregs->pl_ep_ctrl) &
1901 ~BIT(EP_INITIALIZED);
1902 writel(tmp, &dev->plregs->pl_ep_ctrl);
1906 /* Set FSM to focus on the first Control Read:
1907 * - Tip: Connection speed is known upon the first
1909 scratch |= DEFECT7374_FSM_WAITING_FOR_CONTROL_READ;
1910 set_idx_reg(dev->regs, SCRATCH, scratch);
1914 /* keeping it simple:
1915 * - one bus driver, initted first;
1916 * - one function driver, initted second
1918 * most of the work to support multiple net2280 controllers would
1919 * be to associate this gadget driver (yes?) with all of them, or
1920 * perhaps to bind specific drivers to specific devices.
1923 static void usb_reset_228x(struct net2280 *dev)
1927 dev->gadget.speed = USB_SPEED_UNKNOWN;
1928 (void) readl(&dev->usb->usbctl);
1930 net2280_led_init(dev);
1932 /* disable automatic responses, and irqs */
1933 writel(0, &dev->usb->stdrsp);
1934 writel(0, &dev->regs->pciirqenb0);
1935 writel(0, &dev->regs->pciirqenb1);
1937 /* clear old dma and irq state */
1938 for (tmp = 0; tmp < 4; tmp++) {
1939 struct net2280_ep *ep = &dev->ep[tmp + 1];
1944 writel(~0, &dev->regs->irqstat0),
1945 writel(~(u32)BIT(SUSPEND_REQUEST_INTERRUPT), &dev->regs->irqstat1),
1947 /* reset, and enable pci */
1948 tmp = readl(&dev->regs->devinit) |
1950 BIT(FIFO_SOFT_RESET) |
1951 BIT(USB_SOFT_RESET) |
1953 writel(tmp, &dev->regs->devinit);
1955 /* standard fifo and endpoint allocations */
1956 set_fifo_mode(dev, (fifo_mode <= 2) ? fifo_mode : 0);
1959 static void usb_reset_338x(struct net2280 *dev)
1963 dev->gadget.speed = USB_SPEED_UNKNOWN;
1964 (void)readl(&dev->usb->usbctl);
1966 net2280_led_init(dev);
1968 if (dev->bug7734_patched) {
1969 /* disable automatic responses, and irqs */
1970 writel(0, &dev->usb->stdrsp);
1971 writel(0, &dev->regs->pciirqenb0);
1972 writel(0, &dev->regs->pciirqenb1);
1975 /* clear old dma and irq state */
1976 for (tmp = 0; tmp < 4; tmp++) {
1977 struct net2280_ep *ep = &dev->ep[tmp + 1];
1983 writel(~0, &dev->regs->irqstat0), writel(~0, &dev->regs->irqstat1);
1985 if (dev->bug7734_patched) {
1986 /* reset, and enable pci */
1987 tmp = readl(&dev->regs->devinit) |
1989 BIT(FIFO_SOFT_RESET) |
1990 BIT(USB_SOFT_RESET) |
1993 writel(tmp, &dev->regs->devinit);
1996 /* always ep-{1,2,3,4} ... maybe not ep-3 or ep-4 */
1997 INIT_LIST_HEAD(&dev->gadget.ep_list);
1999 for (tmp = 1; tmp < dev->n_ep; tmp++)
2000 list_add_tail(&dev->ep[tmp].ep.ep_list, &dev->gadget.ep_list);
2004 static void usb_reset(struct net2280 *dev)
2006 if (dev->quirks & PLX_LEGACY)
2007 return usb_reset_228x(dev);
2008 return usb_reset_338x(dev);
2011 static void usb_reinit_228x(struct net2280 *dev)
2015 /* basic endpoint init */
2016 for (tmp = 0; tmp < 7; tmp++) {
2017 struct net2280_ep *ep = &dev->ep[tmp];
2019 ep->ep.name = ep_name[tmp];
2023 if (tmp > 0 && tmp <= 4) {
2024 ep->fifo_size = 1024;
2025 ep->dma = &dev->dma[tmp - 1];
2028 ep->regs = &dev->epregs[tmp];
2029 ep->cfg = &dev->epregs[tmp];
2030 ep_reset_228x(dev->regs, ep);
2032 usb_ep_set_maxpacket_limit(&dev->ep[0].ep, 64);
2033 usb_ep_set_maxpacket_limit(&dev->ep[5].ep, 64);
2034 usb_ep_set_maxpacket_limit(&dev->ep[6].ep, 64);
2036 dev->gadget.ep0 = &dev->ep[0].ep;
2037 dev->ep[0].stopped = 0;
2038 INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
2040 /* we want to prevent lowlevel/insecure access from the USB host,
2041 * but erratum 0119 means this enable bit is ignored
2043 for (tmp = 0; tmp < 5; tmp++)
2044 writel(EP_DONTUSE, &dev->dep[tmp].dep_cfg);
2047 static void usb_reinit_338x(struct net2280 *dev)
2051 static const u32 ne[9] = { 0, 1, 2, 3, 4, 1, 2, 3, 4 };
2052 static const u32 ep_reg_addr[9] = { 0x00, 0xC0, 0x00, 0xC0, 0x00,
2053 0x00, 0xC0, 0x00, 0xC0 };
2055 /* basic endpoint init */
2056 for (i = 0; i < dev->n_ep; i++) {
2057 struct net2280_ep *ep = &dev->ep[i];
2059 ep->ep.name = dev->enhanced_mode ? ep_name_adv[i] : ep_name[i];
2063 if (i > 0 && i <= 4)
2064 ep->dma = &dev->dma[i - 1];
2066 if (dev->enhanced_mode) {
2067 ep->cfg = &dev->epregs[ne[i]];
2068 ep->regs = (struct net2280_ep_regs __iomem *)
2069 (((void __iomem *)&dev->epregs[ne[i]]) +
2072 ep->cfg = &dev->epregs[i];
2073 ep->regs = &dev->epregs[i];
2076 ep->fifo_size = (i != 0) ? 2048 : 512;
2078 ep_reset_338x(dev->regs, ep);
2080 usb_ep_set_maxpacket_limit(&dev->ep[0].ep, 512);
2082 dev->gadget.ep0 = &dev->ep[0].ep;
2083 dev->ep[0].stopped = 0;
2085 /* Link layer set up */
2086 if (dev->bug7734_patched) {
2087 tmp = readl(&dev->usb_ext->usbctl2) &
2088 ~(BIT(U1_ENABLE) | BIT(U2_ENABLE) | BIT(LTM_ENABLE));
2089 writel(tmp, &dev->usb_ext->usbctl2);
2092 /* Hardware Defect and Workaround */
2093 val = readl(&dev->ll_lfps_regs->ll_lfps_5);
2094 val &= ~(0xf << TIMER_LFPS_6US);
2095 val |= 0x5 << TIMER_LFPS_6US;
2096 writel(val, &dev->ll_lfps_regs->ll_lfps_5);
2098 val = readl(&dev->ll_lfps_regs->ll_lfps_6);
2099 val &= ~(0xffff << TIMER_LFPS_80US);
2100 val |= 0x0100 << TIMER_LFPS_80US;
2101 writel(val, &dev->ll_lfps_regs->ll_lfps_6);
2104 * AA_AB Errata. Issue 4. Workaround for SuperSpeed USB
2105 * Hot Reset Exit Handshake may Fail in Specific Case using
2106 * Default Register Settings. Workaround for Enumeration test.
2108 val = readl(&dev->ll_tsn_regs->ll_tsn_counters_2);
2109 val &= ~(0x1f << HOT_TX_NORESET_TS2);
2110 val |= 0x10 << HOT_TX_NORESET_TS2;
2111 writel(val, &dev->ll_tsn_regs->ll_tsn_counters_2);
2113 val = readl(&dev->ll_tsn_regs->ll_tsn_counters_3);
2114 val &= ~(0x1f << HOT_RX_RESET_TS2);
2115 val |= 0x3 << HOT_RX_RESET_TS2;
2116 writel(val, &dev->ll_tsn_regs->ll_tsn_counters_3);
2119 * Set Recovery Idle to Recover bit:
2120 * - On SS connections, setting Recovery Idle to Recover Fmw improves
2121 * link robustness with various hosts and hubs.
2122 * - It is safe to set for all connection speeds; all chip revisions.
2123 * - R-M-W to leave other bits undisturbed.
2124 * - Reference PLX TT-7372
2126 val = readl(&dev->ll_chicken_reg->ll_tsn_chicken_bit);
2127 val |= BIT(RECOVERY_IDLE_TO_RECOVER_FMW);
2128 writel(val, &dev->ll_chicken_reg->ll_tsn_chicken_bit);
2130 INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
2132 /* disable dedicated endpoints */
2133 writel(0x0D, &dev->dep[0].dep_cfg);
2134 writel(0x0D, &dev->dep[1].dep_cfg);
2135 writel(0x0E, &dev->dep[2].dep_cfg);
2136 writel(0x0E, &dev->dep[3].dep_cfg);
2137 writel(0x0F, &dev->dep[4].dep_cfg);
2138 writel(0x0C, &dev->dep[5].dep_cfg);
2141 static void usb_reinit(struct net2280 *dev)
2143 if (dev->quirks & PLX_LEGACY)
2144 return usb_reinit_228x(dev);
2145 return usb_reinit_338x(dev);
2148 static void ep0_start_228x(struct net2280 *dev)
2150 writel(BIT(CLEAR_EP_HIDE_STATUS_PHASE) |
2151 BIT(CLEAR_NAK_OUT_PACKETS) |
2152 BIT(CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE),
2153 &dev->epregs[0].ep_rsp);
2156 * hardware optionally handles a bunch of standard requests
2157 * that the API hides from drivers anyway. have it do so.
2158 * endpoint status/features are handled in software, to
2159 * help pass tests for some dubious behavior.
2161 writel(BIT(SET_TEST_MODE) |
2163 BIT(DEVICE_SET_CLEAR_DEVICE_REMOTE_WAKEUP) |
2164 BIT(GET_DEVICE_STATUS) |
2165 BIT(GET_INTERFACE_STATUS),
2167 writel(BIT(USB_ROOT_PORT_WAKEUP_ENABLE) |
2168 BIT(SELF_POWERED_USB_DEVICE) |
2169 BIT(REMOTE_WAKEUP_SUPPORT) |
2170 (dev->softconnect << USB_DETECT_ENABLE) |
2171 BIT(SELF_POWERED_STATUS),
2174 /* enable irqs so we can see ep0 and general operation */
2175 writel(BIT(SETUP_PACKET_INTERRUPT_ENABLE) |
2176 BIT(ENDPOINT_0_INTERRUPT_ENABLE),
2177 &dev->regs->pciirqenb0);
2178 writel(BIT(PCI_INTERRUPT_ENABLE) |
2179 BIT(PCI_MASTER_ABORT_RECEIVED_INTERRUPT_ENABLE) |
2180 BIT(PCI_TARGET_ABORT_RECEIVED_INTERRUPT_ENABLE) |
2181 BIT(PCI_RETRY_ABORT_INTERRUPT_ENABLE) |
2182 BIT(VBUS_INTERRUPT_ENABLE) |
2183 BIT(ROOT_PORT_RESET_INTERRUPT_ENABLE) |
2184 BIT(SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE),
2185 &dev->regs->pciirqenb1);
2187 /* don't leave any writes posted */
2188 (void) readl(&dev->usb->usbctl);
2191 static void ep0_start_338x(struct net2280 *dev)
2194 if (dev->bug7734_patched)
2195 writel(BIT(CLEAR_NAK_OUT_PACKETS_MODE) |
2196 BIT(SET_EP_HIDE_STATUS_PHASE),
2197 &dev->epregs[0].ep_rsp);
2200 * hardware optionally handles a bunch of standard requests
2201 * that the API hides from drivers anyway. have it do so.
2202 * endpoint status/features are handled in software, to
2203 * help pass tests for some dubious behavior.
2205 writel(BIT(SET_ISOCHRONOUS_DELAY) |
2207 BIT(SET_TEST_MODE) |
2209 BIT(GET_INTERFACE_STATUS) |
2210 BIT(GET_DEVICE_STATUS),
2212 dev->wakeup_enable = 1;
2213 writel(BIT(USB_ROOT_PORT_WAKEUP_ENABLE) |
2214 (dev->softconnect << USB_DETECT_ENABLE) |
2215 BIT(DEVICE_REMOTE_WAKEUP_ENABLE),
2218 /* enable irqs so we can see ep0 and general operation */
2219 writel(BIT(SETUP_PACKET_INTERRUPT_ENABLE) |
2220 BIT(ENDPOINT_0_INTERRUPT_ENABLE),
2221 &dev->regs->pciirqenb0);
2222 writel(BIT(PCI_INTERRUPT_ENABLE) |
2223 BIT(ROOT_PORT_RESET_INTERRUPT_ENABLE) |
2224 BIT(SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE) |
2225 BIT(VBUS_INTERRUPT_ENABLE),
2226 &dev->regs->pciirqenb1);
2228 /* don't leave any writes posted */
2229 (void)readl(&dev->usb->usbctl);
2232 static void ep0_start(struct net2280 *dev)
2234 if (dev->quirks & PLX_LEGACY)
2235 return ep0_start_228x(dev);
2236 return ep0_start_338x(dev);
2239 /* when a driver is successfully registered, it will receive
2240 * control requests including set_configuration(), which enables
2241 * non-control requests. then usb traffic follows until a
2242 * disconnect is reported. then a host may connect again, or
2243 * the driver might get unbound.
2245 static int net2280_start(struct usb_gadget *_gadget,
2246 struct usb_gadget_driver *driver)
2248 struct net2280 *dev;
2252 /* insist on high speed support from the driver, since
2253 * (dev->usb->xcvrdiag & FORCE_FULL_SPEED_MODE)
2254 * "must not be used in normal operation"
2256 if (!driver || driver->max_speed < USB_SPEED_HIGH ||
2260 dev = container_of(_gadget, struct net2280, gadget);
2262 for (i = 0; i < dev->n_ep; i++)
2263 dev->ep[i].irqs = 0;
2265 /* hook up the driver ... */
2266 driver->driver.bus = NULL;
2267 dev->driver = driver;
2269 retval = device_create_file(&dev->pdev->dev, &dev_attr_function);
2272 retval = device_create_file(&dev->pdev->dev, &dev_attr_queues);
2276 /* enable host detection and ep0; and we're ready
2277 * for set_configuration as well as eventual disconnect.
2279 net2280_led_active(dev, 1);
2281 if ((dev->quirks & PLX_SUPERSPEED) && !dev->bug7734_patched)
2282 defect7374_enable_data_eps_zero(dev);
2286 /* pci writes may still be posted */
2290 device_remove_file(&dev->pdev->dev, &dev_attr_function);
2296 static void stop_activity(struct net2280 *dev, struct usb_gadget_driver *driver)
2300 /* don't disconnect if it's not connected */
2301 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
2304 /* stop hardware; prevent new request submissions;
2305 * and kill any outstanding requests.
2308 for (i = 0; i < dev->n_ep; i++)
2311 /* report disconnect; the driver is already quiesced */
2313 spin_unlock(&dev->lock);
2314 driver->disconnect(&dev->gadget);
2315 spin_lock(&dev->lock);
2321 static int net2280_stop(struct usb_gadget *_gadget)
2323 struct net2280 *dev;
2324 unsigned long flags;
2326 dev = container_of(_gadget, struct net2280, gadget);
2328 spin_lock_irqsave(&dev->lock, flags);
2329 stop_activity(dev, NULL);
2330 spin_unlock_irqrestore(&dev->lock, flags);
2332 net2280_led_active(dev, 0);
2334 device_remove_file(&dev->pdev->dev, &dev_attr_function);
2335 device_remove_file(&dev->pdev->dev, &dev_attr_queues);
2342 /*-------------------------------------------------------------------------*/
2344 /* handle ep0, ep-e, ep-f with 64 byte packets: packet per irq.
2345 * also works for dma-capable endpoints, in pio mode or just
2346 * to manually advance the queue after short OUT transfers.
2348 static void handle_ep_small(struct net2280_ep *ep)
2350 struct net2280_request *req;
2352 /* 0 error, 1 mid-data, 2 done */
2355 if (!list_empty(&ep->queue))
2356 req = list_entry(ep->queue.next,
2357 struct net2280_request, queue);
2361 /* ack all, and handle what we care about */
2362 t = readl(&ep->regs->ep_stat);
2365 ep_vdbg(ep->dev, "%s ack ep_stat %08x, req %p\n",
2366 ep->ep.name, t, req ? &req->req : NULL);
2368 if (!ep->is_in || (ep->dev->quirks & PLX_2280))
2369 writel(t & ~BIT(NAK_OUT_PACKETS), &ep->regs->ep_stat);
2371 /* Added for 2282 */
2372 writel(t, &ep->regs->ep_stat);
2374 /* for ep0, monitor token irqs to catch data stage length errors
2375 * and to synchronize on status.
2377 * also, to defer reporting of protocol stalls ... here's where
2378 * data or status first appears, handling stalls here should never
2379 * cause trouble on the host side..
2381 * control requests could be slightly faster without token synch for
2382 * status, but status can jam up that way.
2384 if (unlikely(ep->num == 0)) {
2386 /* status; stop NAKing */
2387 if (t & BIT(DATA_OUT_PING_TOKEN_INTERRUPT)) {
2388 if (ep->dev->protocol_stall) {
2395 /* reply to extra IN data tokens with a zlp */
2396 } else if (t & BIT(DATA_IN_TOKEN_INTERRUPT)) {
2397 if (ep->dev->protocol_stall) {
2401 } else if (ep->responded &&
2402 !req && !ep->stopped)
2403 write_fifo(ep, NULL);
2406 /* status; stop NAKing */
2407 if (t & BIT(DATA_IN_TOKEN_INTERRUPT)) {
2408 if (ep->dev->protocol_stall) {
2413 /* an extra OUT token is an error */
2414 } else if (((t & BIT(DATA_OUT_PING_TOKEN_INTERRUPT)) &&
2416 req->req.actual == req->req.length) ||
2417 (ep->responded && !req)) {
2418 ep->dev->protocol_stall = 1;
2422 done(ep, req, -EOVERFLOW);
2431 /* manual DMA queue advance after short OUT */
2432 if (likely(ep->dma)) {
2433 if (t & BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT)) {
2435 int stopped = ep->stopped;
2437 /* TRANSFERRED works around OUT_DONE erratum 0112.
2438 * we expect (N <= maxpacket) bytes; host wrote M.
2439 * iff (M < N) we won't ever see a DMA interrupt.
2442 for (count = 0; ; t = readl(&ep->regs->ep_stat)) {
2444 /* any preceding dma transfers must finish.
2445 * dma handles (M >= N), may empty the queue
2447 scan_dma_completions(ep);
2448 if (unlikely(list_empty(&ep->queue) ||
2449 ep->out_overflow)) {
2453 req = list_entry(ep->queue.next,
2454 struct net2280_request, queue);
2456 /* here either (M < N), a "real" short rx;
2457 * or (M == N) and the queue didn't empty
2459 if (likely(t & BIT(FIFO_EMPTY))) {
2460 count = readl(&ep->dma->dmacount);
2461 count &= DMA_BYTE_COUNT_MASK;
2462 if (readl(&ep->dma->dmadesc)
2470 /* stop DMA, leave ep NAKing */
2471 writel(BIT(DMA_ABORT), &ep->dma->dmastat);
2472 spin_stop_dma(ep->dma);
2475 req->td->dmacount = 0;
2476 t = readl(&ep->regs->ep_avail);
2477 dma_done(ep, req, count,
2478 (ep->out_overflow || t)
2482 /* also flush to prevent erratum 0106 trouble */
2483 if (unlikely(ep->out_overflow ||
2484 (ep->dev->chiprev == 0x0100 &&
2485 ep->dev->gadget.speed
2486 == USB_SPEED_FULL))) {
2488 ep->out_overflow = 0;
2491 /* (re)start dma if needed, stop NAKing */
2492 ep->stopped = stopped;
2493 if (!list_empty(&ep->queue))
2496 ep_dbg(ep->dev, "%s dma ep_stat %08x ??\n",
2500 /* data packet(s) received (in the fifo, OUT) */
2501 } else if (t & BIT(DATA_PACKET_RECEIVED_INTERRUPT)) {
2502 if (read_fifo(ep, req) && ep->num != 0)
2505 /* data packet(s) transmitted (IN) */
2506 } else if (t & BIT(DATA_PACKET_TRANSMITTED_INTERRUPT)) {
2509 len = req->req.length - req->req.actual;
2510 if (len > ep->ep.maxpacket)
2511 len = ep->ep.maxpacket;
2512 req->req.actual += len;
2514 /* if we wrote it all, we're usually done */
2515 /* send zlps until the status stage */
2516 if ((req->req.actual == req->req.length) &&
2517 (!req->req.zero || len != ep->ep.maxpacket) && ep->num)
2520 /* there was nothing to do ... */
2521 } else if (mode == 1)
2526 /* stream endpoints often resubmit/unlink in completion */
2529 /* maybe advance queue to next request */
2531 /* NOTE: net2280 could let gadget driver start the
2532 * status stage later. since not all controllers let
2533 * them control that, the api doesn't (yet) allow it.
2539 if (!list_empty(&ep->queue) && !ep->stopped)
2540 req = list_entry(ep->queue.next,
2541 struct net2280_request, queue);
2544 if (req && !ep->is_in)
2545 stop_out_naking(ep);
2549 /* is there a buffer for the next packet?
2550 * for best streaming performance, make sure there is one.
2552 if (req && !ep->stopped) {
2554 /* load IN fifo with next packet (may be zlp) */
2555 if (t & BIT(DATA_PACKET_TRANSMITTED_INTERRUPT))
2556 write_fifo(ep, &req->req);
2560 static struct net2280_ep *get_ep_by_addr(struct net2280 *dev, u16 wIndex)
2562 struct net2280_ep *ep;
2564 if ((wIndex & USB_ENDPOINT_NUMBER_MASK) == 0)
2566 list_for_each_entry(ep, &dev->gadget.ep_list, ep.ep_list) {
2567 u8 bEndpointAddress;
2571 bEndpointAddress = ep->desc->bEndpointAddress;
2572 if ((wIndex ^ bEndpointAddress) & USB_DIR_IN)
2574 if ((wIndex & 0x0f) == (bEndpointAddress & 0x0f))
2580 static void defect7374_workaround(struct net2280 *dev, struct usb_ctrlrequest r)
2582 u32 scratch, fsmvalue;
2583 u32 ack_wait_timeout, state;
2585 /* Workaround for Defect 7374 (U1/U2 erroneously rejected): */
2586 scratch = get_idx_reg(dev->regs, SCRATCH);
2587 fsmvalue = scratch & (0xf << DEFECT7374_FSM_FIELD);
2588 scratch &= ~(0xf << DEFECT7374_FSM_FIELD);
2590 if (!((fsmvalue == DEFECT7374_FSM_WAITING_FOR_CONTROL_READ) &&
2591 (r.bRequestType & USB_DIR_IN)))
2594 /* This is the first Control Read for this connection: */
2595 if (!(readl(&dev->usb->usbstat) & BIT(SUPER_SPEED_MODE))) {
2597 * Connection is NOT SS:
2598 * - Connection must be FS or HS.
2599 * - This FSM state should allow workaround software to
2600 * run after the next USB connection.
2602 scratch |= DEFECT7374_FSM_NON_SS_CONTROL_READ;
2603 dev->bug7734_patched = 1;
2604 goto restore_data_eps;
2607 /* Connection is SS: */
2608 for (ack_wait_timeout = 0;
2609 ack_wait_timeout < DEFECT_7374_NUMBEROF_MAX_WAIT_LOOPS;
2610 ack_wait_timeout++) {
2612 state = readl(&dev->plregs->pl_ep_status_1)
2614 if ((state >= (ACK_GOOD_NORMAL << STATE)) &&
2615 (state <= (ACK_GOOD_MORE_ACKS_TO_COME << STATE))) {
2616 scratch |= DEFECT7374_FSM_SS_CONTROL_READ;
2617 dev->bug7734_patched = 1;
2622 * We have not yet received host's Data Phase ACK
2623 * - Wait and try again.
2625 udelay(DEFECT_7374_PROCESSOR_WAIT_TIME);
2631 if (ack_wait_timeout >= DEFECT_7374_NUMBEROF_MAX_WAIT_LOOPS) {
2632 ep_err(dev, "FAIL: Defect 7374 workaround waited but failed "
2633 "to detect SS host's data phase ACK.");
2634 ep_err(dev, "PL_EP_STATUS_1(23:16):.Expected from 0x11 to 0x16"
2635 "got 0x%2.2x.\n", state >> STATE);
2637 ep_warn(dev, "INFO: Defect 7374 workaround waited about\n"
2638 "%duSec for Control Read Data Phase ACK\n",
2639 DEFECT_7374_PROCESSOR_WAIT_TIME * ack_wait_timeout);
2644 * Restore data EPs to their pre-workaround settings (disabled,
2645 * initialized, and other details).
2647 defect7374_disable_data_eps(dev);
2649 set_idx_reg(dev->regs, SCRATCH, scratch);
2654 static void ep_clear_seqnum(struct net2280_ep *ep)
2656 struct net2280 *dev = ep->dev;
2658 static const u32 ep_pl[9] = { 0, 3, 4, 7, 8, 2, 5, 6, 9 };
2660 val = readl(&dev->plregs->pl_ep_ctrl) & ~0x1f;
2661 val |= ep_pl[ep->num];
2662 writel(val, &dev->plregs->pl_ep_ctrl);
2663 val |= BIT(SEQUENCE_NUMBER_RESET);
2664 writel(val, &dev->plregs->pl_ep_ctrl);
2669 static void handle_stat0_irqs_superspeed(struct net2280 *dev,
2670 struct net2280_ep *ep, struct usb_ctrlrequest r)
2674 #define w_value le16_to_cpu(r.wValue)
2675 #define w_index le16_to_cpu(r.wIndex)
2676 #define w_length le16_to_cpu(r.wLength)
2678 switch (r.bRequest) {
2679 struct net2280_ep *e;
2682 case USB_REQ_SET_CONFIGURATION:
2683 dev->addressed_state = !w_value;
2686 case USB_REQ_GET_STATUS:
2687 switch (r.bRequestType) {
2688 case (USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE):
2689 status = dev->wakeup_enable ? 0x02 : 0x00;
2690 if (dev->gadget.is_selfpowered)
2692 status |= (dev->u1_enable << 2 | dev->u2_enable << 3 |
2693 dev->ltm_enable << 4);
2694 writel(0, &dev->epregs[0].ep_irqenb);
2695 set_fifo_bytecount(ep, sizeof(status));
2696 writel((__force u32) status, &dev->epregs[0].ep_data);
2697 allow_status_338x(ep);
2700 case (USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_ENDPOINT):
2701 e = get_ep_by_addr(dev, w_index);
2704 status = readl(&e->regs->ep_rsp) &
2705 BIT(CLEAR_ENDPOINT_HALT);
2706 writel(0, &dev->epregs[0].ep_irqenb);
2707 set_fifo_bytecount(ep, sizeof(status));
2708 writel((__force u32) status, &dev->epregs[0].ep_data);
2709 allow_status_338x(ep);
2717 case USB_REQ_CLEAR_FEATURE:
2718 switch (r.bRequestType) {
2719 case (USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE):
2720 if (!dev->addressed_state) {
2722 case USB_DEVICE_U1_ENABLE:
2724 writel(readl(&dev->usb_ext->usbctl2) &
2726 &dev->usb_ext->usbctl2);
2727 allow_status_338x(ep);
2728 goto next_endpoints3;
2730 case USB_DEVICE_U2_ENABLE:
2732 writel(readl(&dev->usb_ext->usbctl2) &
2734 &dev->usb_ext->usbctl2);
2735 allow_status_338x(ep);
2736 goto next_endpoints3;
2738 case USB_DEVICE_LTM_ENABLE:
2739 dev->ltm_enable = 0;
2740 writel(readl(&dev->usb_ext->usbctl2) &
2742 &dev->usb_ext->usbctl2);
2743 allow_status_338x(ep);
2744 goto next_endpoints3;
2750 if (w_value == USB_DEVICE_REMOTE_WAKEUP) {
2751 dev->wakeup_enable = 0;
2752 writel(readl(&dev->usb->usbctl) &
2753 ~BIT(DEVICE_REMOTE_WAKEUP_ENABLE),
2755 allow_status_338x(ep);
2760 case (USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_ENDPOINT):
2761 e = get_ep_by_addr(dev, w_index);
2764 if (w_value != USB_ENDPOINT_HALT)
2766 ep_vdbg(dev, "%s clear halt\n", e->ep.name);
2768 * Workaround for SS SeqNum not cleared via
2769 * Endpoint Halt (Clear) bit. select endpoint
2773 if (!list_empty(&e->queue) && e->td_dma)
2783 case USB_REQ_SET_FEATURE:
2784 switch (r.bRequestType) {
2785 case (USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE):
2786 if (!dev->addressed_state) {
2788 case USB_DEVICE_U1_ENABLE:
2790 writel(readl(&dev->usb_ext->usbctl2) |
2792 &dev->usb_ext->usbctl2);
2793 allow_status_338x(ep);
2794 goto next_endpoints3;
2796 case USB_DEVICE_U2_ENABLE:
2798 writel(readl(&dev->usb_ext->usbctl2) |
2800 &dev->usb_ext->usbctl2);
2801 allow_status_338x(ep);
2802 goto next_endpoints3;
2804 case USB_DEVICE_LTM_ENABLE:
2805 dev->ltm_enable = 1;
2806 writel(readl(&dev->usb_ext->usbctl2) |
2808 &dev->usb_ext->usbctl2);
2809 allow_status_338x(ep);
2810 goto next_endpoints3;
2816 if (w_value == USB_DEVICE_REMOTE_WAKEUP) {
2817 dev->wakeup_enable = 1;
2818 writel(readl(&dev->usb->usbctl) |
2819 BIT(DEVICE_REMOTE_WAKEUP_ENABLE),
2821 allow_status_338x(ep);
2826 case (USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_ENDPOINT):
2827 e = get_ep_by_addr(dev, w_index);
2828 if (!e || (w_value != USB_ENDPOINT_HALT))
2832 ep->dev->protocol_stall = 1;
2838 allow_status_338x(ep);
2849 ep_vdbg(dev, "setup %02x.%02x v%04x i%04x l%04x ep_cfg %08x\n",
2850 r.bRequestType, r.bRequest,
2851 w_value, w_index, w_length,
2852 readl(&ep->cfg->ep_cfg));
2855 spin_unlock(&dev->lock);
2856 tmp = dev->driver->setup(&dev->gadget, &r);
2857 spin_lock(&dev->lock);
2861 ep_vdbg(dev, "req %02x.%02x protocol STALL; stat %d\n",
2862 r.bRequestType, r.bRequest, tmp);
2863 dev->protocol_stall = 1;
2864 /* TD 9.9 Halt Endpoint test. TD 9.22 Set feature test */
2877 static void handle_stat0_irqs(struct net2280 *dev, u32 stat)
2879 struct net2280_ep *ep;
2882 /* most of these don't need individual acks */
2883 stat &= ~BIT(INTA_ASSERTED);
2886 /* ep_dbg(dev, "irqstat0 %04x\n", stat); */
2888 /* starting a control request? */
2889 if (unlikely(stat & BIT(SETUP_PACKET_INTERRUPT))) {
2892 struct usb_ctrlrequest r;
2895 struct net2280_request *req;
2897 if (dev->gadget.speed == USB_SPEED_UNKNOWN) {
2898 u32 val = readl(&dev->usb->usbstat);
2899 if (val & BIT(SUPER_SPEED)) {
2900 dev->gadget.speed = USB_SPEED_SUPER;
2901 usb_ep_set_maxpacket_limit(&dev->ep[0].ep,
2902 EP0_SS_MAX_PACKET_SIZE);
2903 } else if (val & BIT(HIGH_SPEED)) {
2904 dev->gadget.speed = USB_SPEED_HIGH;
2905 usb_ep_set_maxpacket_limit(&dev->ep[0].ep,
2906 EP0_HS_MAX_PACKET_SIZE);
2908 dev->gadget.speed = USB_SPEED_FULL;
2909 usb_ep_set_maxpacket_limit(&dev->ep[0].ep,
2910 EP0_HS_MAX_PACKET_SIZE);
2912 net2280_led_speed(dev, dev->gadget.speed);
2914 usb_speed_string(dev->gadget.speed));
2920 /* make sure any leftover request state is cleared */
2921 stat &= ~BIT(ENDPOINT_0_INTERRUPT);
2922 while (!list_empty(&ep->queue)) {
2923 req = list_entry(ep->queue.next,
2924 struct net2280_request, queue);
2925 done(ep, req, (req->req.actual == req->req.length)
2929 dev->protocol_stall = 0;
2930 if (!(dev->quirks & PLX_SUPERSPEED)) {
2931 if (ep->dev->quirks & PLX_2280)
2932 tmp = BIT(FIFO_OVERFLOW) |
2933 BIT(FIFO_UNDERFLOW);
2937 writel(tmp | BIT(TIMEOUT) |
2938 BIT(USB_STALL_SENT) |
2939 BIT(USB_IN_NAK_SENT) |
2940 BIT(USB_IN_ACK_RCVD) |
2941 BIT(USB_OUT_PING_NAK_SENT) |
2942 BIT(USB_OUT_ACK_SENT) |
2943 BIT(SHORT_PACKET_OUT_DONE_INTERRUPT) |
2944 BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT) |
2945 BIT(DATA_PACKET_RECEIVED_INTERRUPT) |
2946 BIT(DATA_PACKET_TRANSMITTED_INTERRUPT) |
2947 BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
2948 BIT(DATA_IN_TOKEN_INTERRUPT),
2949 &ep->regs->ep_stat);
2951 u.raw[0] = readl(&dev->usb->setup0123);
2952 u.raw[1] = readl(&dev->usb->setup4567);
2954 cpu_to_le32s(&u.raw[0]);
2955 cpu_to_le32s(&u.raw[1]);
2957 if ((dev->quirks & PLX_SUPERSPEED) && !dev->bug7734_patched)
2958 defect7374_workaround(dev, u.r);
2962 #define w_value le16_to_cpu(u.r.wValue)
2963 #define w_index le16_to_cpu(u.r.wIndex)
2964 #define w_length le16_to_cpu(u.r.wLength)
2967 writel(BIT(SETUP_PACKET_INTERRUPT), &dev->regs->irqstat0);
2968 stat ^= BIT(SETUP_PACKET_INTERRUPT);
2970 /* watch control traffic at the token level, and force
2971 * synchronization before letting the status stage happen.
2972 * FIXME ignore tokens we'll NAK, until driver responds.
2973 * that'll mean a lot less irqs for some drivers.
2975 ep->is_in = (u.r.bRequestType & USB_DIR_IN) != 0;
2977 scratch = BIT(DATA_PACKET_TRANSMITTED_INTERRUPT) |
2978 BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
2979 BIT(DATA_IN_TOKEN_INTERRUPT);
2980 stop_out_naking(ep);
2982 scratch = BIT(DATA_PACKET_RECEIVED_INTERRUPT) |
2983 BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
2984 BIT(DATA_IN_TOKEN_INTERRUPT);
2985 writel(scratch, &dev->epregs[0].ep_irqenb);
2987 /* we made the hardware handle most lowlevel requests;
2988 * everything else goes uplevel to the gadget code.
2992 if (dev->gadget.speed == USB_SPEED_SUPER) {
2993 handle_stat0_irqs_superspeed(dev, ep, u.r);
2994 goto next_endpoints;
2997 switch (u.r.bRequest) {
2998 case USB_REQ_GET_STATUS: {
2999 struct net2280_ep *e;
3002 /* hw handles device and interface status */
3003 if (u.r.bRequestType != (USB_DIR_IN|USB_RECIP_ENDPOINT))
3005 e = get_ep_by_addr(dev, w_index);
3006 if (!e || w_length > 2)
3009 if (readl(&e->regs->ep_rsp) & BIT(SET_ENDPOINT_HALT))
3010 status = cpu_to_le32(1);
3012 status = cpu_to_le32(0);
3014 /* don't bother with a request object! */
3015 writel(0, &dev->epregs[0].ep_irqenb);
3016 set_fifo_bytecount(ep, w_length);
3017 writel((__force u32)status, &dev->epregs[0].ep_data);
3019 ep_vdbg(dev, "%s stat %02x\n", ep->ep.name, status);
3020 goto next_endpoints;
3023 case USB_REQ_CLEAR_FEATURE: {
3024 struct net2280_ep *e;
3026 /* hw handles device features */
3027 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
3029 if (w_value != USB_ENDPOINT_HALT || w_length != 0)
3031 e = get_ep_by_addr(dev, w_index);
3035 ep_vdbg(dev, "%s wedged, halt not cleared\n",
3038 ep_vdbg(dev, "%s clear halt\n", e->ep.name);
3040 if ((ep->dev->quirks & PLX_SUPERSPEED) &&
3041 !list_empty(&e->queue) && e->td_dma)
3045 goto next_endpoints;
3048 case USB_REQ_SET_FEATURE: {
3049 struct net2280_ep *e;
3051 /* hw handles device features */
3052 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
3054 if (w_value != USB_ENDPOINT_HALT || w_length != 0)
3056 e = get_ep_by_addr(dev, w_index);
3059 if (e->ep.name == ep0name)
3062 if ((dev->quirks & PLX_SUPERSPEED) && e->dma)
3065 ep_vdbg(dev, "%s set halt\n", ep->ep.name);
3066 goto next_endpoints;
3071 ep_vdbg(dev, "setup %02x.%02x v%04x i%04x l%04x "
3073 u.r.bRequestType, u.r.bRequest,
3074 w_value, w_index, w_length,
3075 readl(&ep->cfg->ep_cfg));
3077 spin_unlock(&dev->lock);
3078 tmp = dev->driver->setup(&dev->gadget, &u.r);
3079 spin_lock(&dev->lock);
3082 /* stall ep0 on error */
3085 ep_vdbg(dev, "req %02x.%02x protocol STALL; stat %d\n",
3086 u.r.bRequestType, u.r.bRequest, tmp);
3087 dev->protocol_stall = 1;
3090 /* some in/out token irq should follow; maybe stall then.
3091 * driver must queue a request (even zlp) or halt ep0
3092 * before the host times out.
3101 /* endpoint data irq ? */
3102 scratch = stat & 0x7f;
3104 for (num = 0; scratch; num++) {
3107 /* do this endpoint's FIFO and queue need tending? */
3109 if ((scratch & t) == 0)
3114 handle_ep_small(ep);
3118 ep_dbg(dev, "unhandled irqstat0 %08x\n", stat);
3121 #define DMA_INTERRUPTS (BIT(DMA_D_INTERRUPT) | \
3122 BIT(DMA_C_INTERRUPT) | \
3123 BIT(DMA_B_INTERRUPT) | \
3124 BIT(DMA_A_INTERRUPT))
3125 #define PCI_ERROR_INTERRUPTS ( \
3126 BIT(PCI_MASTER_ABORT_RECEIVED_INTERRUPT) | \
3127 BIT(PCI_TARGET_ABORT_RECEIVED_INTERRUPT) | \
3128 BIT(PCI_RETRY_ABORT_INTERRUPT))
3130 static void handle_stat1_irqs(struct net2280 *dev, u32 stat)
3131 __releases(dev->lock)
3132 __acquires(dev->lock)
3134 struct net2280_ep *ep;
3135 u32 tmp, num, mask, scratch;
3137 /* after disconnect there's nothing else to do! */
3138 tmp = BIT(VBUS_INTERRUPT) | BIT(ROOT_PORT_RESET_INTERRUPT);
3139 mask = BIT(SUPER_SPEED) | BIT(HIGH_SPEED) | BIT(FULL_SPEED);
3141 /* VBUS disconnect is indicated by VBUS_PIN and VBUS_INTERRUPT set.
3142 * Root Port Reset is indicated by ROOT_PORT_RESET_INTERRUPT set and
3143 * both HIGH_SPEED and FULL_SPEED clear (as ROOT_PORT_RESET_INTERRUPT
3144 * only indicates a change in the reset state).
3148 bool disconnect = false;
3151 * Ignore disconnects and resets if the speed hasn't been set.
3152 * VBUS can bounce and there's always an initial reset.
3154 writel(tmp, &dev->regs->irqstat1);
3155 if (dev->gadget.speed != USB_SPEED_UNKNOWN) {
3156 if ((stat & BIT(VBUS_INTERRUPT)) &&
3157 (readl(&dev->usb->usbctl) &
3158 BIT(VBUS_PIN)) == 0) {
3160 ep_dbg(dev, "disconnect %s\n",
3161 dev->driver->driver.name);
3162 } else if ((stat & BIT(ROOT_PORT_RESET_INTERRUPT)) &&
3163 (readl(&dev->usb->usbstat) & mask)
3166 ep_dbg(dev, "reset %s\n",
3167 dev->driver->driver.name);
3170 if (disconnect || reset) {
3171 stop_activity(dev, dev->driver);
3173 spin_unlock(&dev->lock);
3175 usb_gadget_udc_reset
3176 (&dev->gadget, dev->driver);
3178 (dev->driver->disconnect)
3180 spin_lock(&dev->lock);
3186 /* vBUS can bounce ... one of many reasons to ignore the
3187 * notion of hotplug events on bus connect/disconnect!
3193 /* NOTE: chip stays in PCI D0 state for now, but it could
3194 * enter D1 to save more power
3196 tmp = BIT(SUSPEND_REQUEST_CHANGE_INTERRUPT);
3198 writel(tmp, &dev->regs->irqstat1);
3199 if (stat & BIT(SUSPEND_REQUEST_INTERRUPT)) {
3200 if (dev->driver->suspend)
3201 dev->driver->suspend(&dev->gadget);
3202 if (!enable_suspend)
3203 stat &= ~BIT(SUSPEND_REQUEST_INTERRUPT);
3205 if (dev->driver->resume)
3206 dev->driver->resume(&dev->gadget);
3207 /* at high speed, note erratum 0133 */
3212 /* clear any other status/irqs */
3214 writel(stat, &dev->regs->irqstat1);
3216 /* some status we can just ignore */
3217 if (dev->quirks & PLX_2280)
3218 stat &= ~(BIT(CONTROL_STATUS_INTERRUPT) |
3219 BIT(SUSPEND_REQUEST_INTERRUPT) |
3220 BIT(RESUME_INTERRUPT) |
3221 BIT(SOF_INTERRUPT));
3223 stat &= ~(BIT(CONTROL_STATUS_INTERRUPT) |
3224 BIT(RESUME_INTERRUPT) |
3225 BIT(SOF_DOWN_INTERRUPT) |
3226 BIT(SOF_INTERRUPT));
3230 /* ep_dbg(dev, "irqstat1 %08x\n", stat);*/
3232 /* DMA status, for ep-{a,b,c,d} */
3233 scratch = stat & DMA_INTERRUPTS;
3234 stat &= ~DMA_INTERRUPTS;
3236 for (num = 0; scratch; num++) {
3237 struct net2280_dma_regs __iomem *dma;
3240 if ((tmp & scratch) == 0)
3244 ep = &dev->ep[num + 1];
3250 /* clear ep's dma status */
3251 tmp = readl(&dma->dmastat);
3252 writel(tmp, &dma->dmastat);
3255 if (dev->quirks & PLX_SUPERSPEED) {
3256 u32 r_dmacount = readl(&dma->dmacount);
3257 if (!ep->is_in && (r_dmacount & 0x00FFFFFF) &&
3258 (tmp & BIT(DMA_TRANSACTION_DONE_INTERRUPT)))
3262 if (!(tmp & BIT(DMA_TRANSACTION_DONE_INTERRUPT))) {
3263 ep_dbg(ep->dev, "%s no xact done? %08x\n",
3269 /* OUT transfers terminate when the data from the
3270 * host is in our memory. Process whatever's done.
3271 * On this path, we know transfer's last packet wasn't
3272 * less than req->length. NAK_OUT_PACKETS may be set,
3273 * or the FIFO may already be holding new packets.
3275 * IN transfers can linger in the FIFO for a very
3276 * long time ... we ignore that for now, accounting
3277 * precisely (like PIO does) needs per-packet irqs
3279 scan_dma_completions(ep);
3281 /* disable dma on inactive queues; else maybe restart */
3282 if (!list_empty(&ep->queue)) {
3283 tmp = readl(&dma->dmactl);
3289 /* NOTE: there are other PCI errors we might usefully notice.
3290 * if they appear very often, here's where to try recovering.
3292 if (stat & PCI_ERROR_INTERRUPTS) {
3293 ep_err(dev, "pci dma error; stat %08x\n", stat);
3294 stat &= ~PCI_ERROR_INTERRUPTS;
3295 /* these are fatal errors, but "maybe" they won't
3298 stop_activity(dev, dev->driver);
3304 ep_dbg(dev, "unhandled irqstat1 %08x\n", stat);
3307 static irqreturn_t net2280_irq(int irq, void *_dev)
3309 struct net2280 *dev = _dev;
3311 /* shared interrupt, not ours */
3312 if ((dev->quirks & PLX_LEGACY) &&
3313 (!(readl(&dev->regs->irqstat0) & BIT(INTA_ASSERTED))))
3316 spin_lock(&dev->lock);
3318 /* handle disconnect, dma, and more */
3319 handle_stat1_irqs(dev, readl(&dev->regs->irqstat1));
3321 /* control requests and PIO */
3322 handle_stat0_irqs(dev, readl(&dev->regs->irqstat0));
3324 if (dev->quirks & PLX_SUPERSPEED) {
3325 /* re-enable interrupt to trigger any possible new interrupt */
3326 u32 pciirqenb1 = readl(&dev->regs->pciirqenb1);
3327 writel(pciirqenb1 & 0x7FFFFFFF, &dev->regs->pciirqenb1);
3328 writel(pciirqenb1, &dev->regs->pciirqenb1);
3331 spin_unlock(&dev->lock);
3336 /*-------------------------------------------------------------------------*/
3338 static void gadget_release(struct device *_dev)
3340 struct net2280 *dev = dev_get_drvdata(_dev);
3345 /* tear down the binding between this driver and the pci device */
3347 static void net2280_remove(struct pci_dev *pdev)
3349 struct net2280 *dev = pci_get_drvdata(pdev);
3351 usb_del_gadget_udc(&dev->gadget);
3353 BUG_ON(dev->driver);
3355 /* then clean up the resources we allocated during probe() */
3356 net2280_led_shutdown(dev);
3357 if (dev->requests) {
3359 for (i = 1; i < 5; i++) {
3360 if (!dev->ep[i].dummy)
3362 pci_pool_free(dev->requests, dev->ep[i].dummy,
3365 pci_pool_destroy(dev->requests);
3368 free_irq(pdev->irq, dev);
3369 if (dev->quirks & PLX_SUPERSPEED)
3370 pci_disable_msi(pdev);
3374 release_mem_region(pci_resource_start(pdev, 0),
3375 pci_resource_len(pdev, 0));
3377 pci_disable_device(pdev);
3378 device_remove_file(&pdev->dev, &dev_attr_registers);
3380 ep_info(dev, "unbind\n");
3383 /* wrap this driver around the specified device, but
3384 * don't respond over USB until a gadget driver binds to us.
3387 static int net2280_probe(struct pci_dev *pdev, const struct pci_device_id *id)
3389 struct net2280 *dev;
3390 unsigned long resource, len;
3391 void __iomem *base = NULL;
3394 /* alloc, and start init */
3395 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
3401 pci_set_drvdata(pdev, dev);
3402 spin_lock_init(&dev->lock);
3403 dev->quirks = id->driver_data;
3405 dev->gadget.ops = &net2280_ops;
3406 dev->gadget.max_speed = (dev->quirks & PLX_SUPERSPEED) ?
3407 USB_SPEED_SUPER : USB_SPEED_HIGH;
3409 /* the "gadget" abstracts/virtualizes the controller */
3410 dev->gadget.name = driver_name;
3412 /* now all the pci goodies ... */
3413 if (pci_enable_device(pdev) < 0) {
3419 /* BAR 0 holds all the registers
3420 * BAR 1 is 8051 memory; unused here (note erratum 0103)
3421 * BAR 2 is fifo memory; unused here
3423 resource = pci_resource_start(pdev, 0);
3424 len = pci_resource_len(pdev, 0);
3425 if (!request_mem_region(resource, len, driver_name)) {
3426 ep_dbg(dev, "controller already in use\n");
3432 /* FIXME provide firmware download interface to put
3433 * 8051 code into the chip, e.g. to turn on PCI PM.
3436 base = ioremap_nocache(resource, len);
3438 ep_dbg(dev, "can't map memory\n");
3442 dev->regs = (struct net2280_regs __iomem *) base;
3443 dev->usb = (struct net2280_usb_regs __iomem *) (base + 0x0080);
3444 dev->pci = (struct net2280_pci_regs __iomem *) (base + 0x0100);
3445 dev->dma = (struct net2280_dma_regs __iomem *) (base + 0x0180);
3446 dev->dep = (struct net2280_dep_regs __iomem *) (base + 0x0200);
3447 dev->epregs = (struct net2280_ep_regs __iomem *) (base + 0x0300);
3449 if (dev->quirks & PLX_SUPERSPEED) {
3452 dev->usb_ext = (struct usb338x_usb_ext_regs __iomem *)
3454 dev->llregs = (struct usb338x_ll_regs __iomem *)
3456 dev->ll_lfps_regs = (struct usb338x_ll_lfps_regs __iomem *)
3458 dev->ll_tsn_regs = (struct usb338x_ll_tsn_regs __iomem *)
3460 dev->ll_chicken_reg = (struct usb338x_ll_chi_regs __iomem *)
3462 dev->plregs = (struct usb338x_pl_regs __iomem *)
3464 usbstat = readl(&dev->usb->usbstat);
3465 dev->enhanced_mode = !!(usbstat & BIT(11));
3466 dev->n_ep = (dev->enhanced_mode) ? 9 : 5;
3467 /* put into initial config, link up all endpoints */
3468 fsmvalue = get_idx_reg(dev->regs, SCRATCH) &
3469 (0xf << DEFECT7374_FSM_FIELD);
3470 /* See if firmware needs to set up for workaround: */
3471 if (fsmvalue == DEFECT7374_FSM_SS_CONTROL_READ) {
3472 dev->bug7734_patched = 1;
3473 writel(0, &dev->usb->usbctl);
3475 dev->bug7734_patched = 0;
3477 dev->enhanced_mode = 0;
3479 /* put into initial config, link up all endpoints */
3480 writel(0, &dev->usb->usbctl);
3486 /* irq setup after old hardware is cleaned up */
3488 ep_err(dev, "No IRQ. Check PCI setup!\n");
3493 if (dev->quirks & PLX_SUPERSPEED)
3494 if (pci_enable_msi(pdev))
3495 ep_err(dev, "Failed to enable MSI mode\n");
3497 if (request_irq(pdev->irq, net2280_irq, IRQF_SHARED,
3498 driver_name, dev)) {
3499 ep_err(dev, "request interrupt %d failed\n", pdev->irq);
3506 /* NOTE: we know only the 32 LSBs of dma addresses may be nonzero */
3507 dev->requests = pci_pool_create("requests", pdev,
3508 sizeof(struct net2280_dma),
3509 0 /* no alignment requirements */,
3510 0 /* or page-crossing issues */);
3511 if (!dev->requests) {
3512 ep_dbg(dev, "can't get request pool\n");
3516 for (i = 1; i < 5; i++) {
3517 struct net2280_dma *td;
3519 td = pci_pool_alloc(dev->requests, GFP_KERNEL,
3520 &dev->ep[i].td_dma);
3522 ep_dbg(dev, "can't get dummy %d\n", i);
3526 td->dmacount = 0; /* not VALID */
3527 td->dmadesc = td->dmaaddr;
3528 dev->ep[i].dummy = td;
3531 /* enable lower-overhead pci memory bursts during DMA */
3532 if (dev->quirks & PLX_LEGACY)
3533 writel(BIT(DMA_MEMORY_WRITE_AND_INVALIDATE_ENABLE) |
3535 * 256 write retries may not be enough...
3536 BIT(PCI_RETRY_ABORT_ENABLE) |
3538 BIT(DMA_READ_MULTIPLE_ENABLE) |
3539 BIT(DMA_READ_LINE_ENABLE),
3540 &dev->pci->pcimstctl);
3541 /* erratum 0115 shouldn't appear: Linux inits PCI_LATENCY_TIMER */
3542 pci_set_master(pdev);
3543 pci_try_set_mwi(pdev);
3545 /* ... also flushes any posted pci writes */
3546 dev->chiprev = get_idx_reg(dev->regs, REG_CHIPREV) & 0xffff;
3549 ep_info(dev, "%s\n", driver_desc);
3550 ep_info(dev, "irq %d, pci mem %p, chip rev %04x\n",
3551 pdev->irq, base, dev->chiprev);
3552 ep_info(dev, "version: " DRIVER_VERSION "; %s\n",
3553 dev->enhanced_mode ? "enhanced mode" : "legacy mode");
3554 retval = device_create_file(&pdev->dev, &dev_attr_registers);
3558 retval = usb_add_gadget_udc_release(&pdev->dev, &dev->gadget,
3566 net2280_remove(pdev);
3570 /* make sure the board is quiescent; otherwise it will continue
3571 * generating IRQs across the upcoming reboot.
3574 static void net2280_shutdown(struct pci_dev *pdev)
3576 struct net2280 *dev = pci_get_drvdata(pdev);
3579 writel(0, &dev->regs->pciirqenb0);
3580 writel(0, &dev->regs->pciirqenb1);
3582 /* disable the pullup so the host will think we're gone */
3583 writel(0, &dev->usb->usbctl);
3588 /*-------------------------------------------------------------------------*/
3590 static const struct pci_device_id pci_ids[] = { {
3591 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
3593 .vendor = PCI_VENDOR_ID_PLX_LEGACY,
3595 .subvendor = PCI_ANY_ID,
3596 .subdevice = PCI_ANY_ID,
3597 .driver_data = PLX_LEGACY | PLX_2280,
3599 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
3601 .vendor = PCI_VENDOR_ID_PLX_LEGACY,
3603 .subvendor = PCI_ANY_ID,
3604 .subdevice = PCI_ANY_ID,
3605 .driver_data = PLX_LEGACY,
3608 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
3610 .vendor = PCI_VENDOR_ID_PLX,
3612 .subvendor = PCI_ANY_ID,
3613 .subdevice = PCI_ANY_ID,
3614 .driver_data = PLX_SUPERSPEED,
3617 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
3619 .vendor = PCI_VENDOR_ID_PLX,
3621 .subvendor = PCI_ANY_ID,
3622 .subdevice = PCI_ANY_ID,
3623 .driver_data = PLX_SUPERSPEED,
3625 { /* end: all zeroes */ }
3627 MODULE_DEVICE_TABLE(pci, pci_ids);
3629 /* pci driver glue; this is a "new style" PCI driver module */
3630 static struct pci_driver net2280_pci_driver = {
3631 .name = (char *) driver_name,
3632 .id_table = pci_ids,
3634 .probe = net2280_probe,
3635 .remove = net2280_remove,
3636 .shutdown = net2280_shutdown,
3638 /* FIXME add power management support */
3641 module_pci_driver(net2280_pci_driver);
3643 MODULE_DESCRIPTION(DRIVER_DESC);
3644 MODULE_AUTHOR("David Brownell");
3645 MODULE_LICENSE("GPL");