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 File Storage, Serial, and Ethernet/RNDIS gadget drivers
13 * as well as Gadget Zero and Gadgetfs.
15 * DMA is enabled by default. Drivers using transfer queues might use
16 * DMA chaining to remove IRQ latencies between transfers. (Except when
17 * short OUT transfers happen.) Drivers can use the req->no_interrupt
18 * hint to completely eliminate some IRQs, if a later IRQ is guaranteed
19 * and DMA chaining is enabled.
21 * Note that almost all the errata workarounds here are only needed for
22 * rev1 chips. Rev1a silicon (0110) fixes almost all of them.
26 * Copyright (C) 2003 David Brownell
27 * Copyright (C) 2003-2005 PLX Technology, Inc.
29 * Modified Seth Levy 2005 PLX Technology, Inc. to provide compatibility
32 * This program is free software; you can redistribute it and/or modify
33 * it under the terms of the GNU General Public License as published by
34 * the Free Software Foundation; either version 2 of the License, or
35 * (at your option) any later version.
38 #undef DEBUG /* messages on error and most fault paths */
39 #undef VERBOSE /* extra debug messages (success too) */
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>
59 #include <asm/byteorder.h>
62 #include <asm/system.h>
63 #include <asm/unaligned.h>
66 #define DRIVER_DESC "PLX NET228x USB Peripheral Controller"
67 #define DRIVER_VERSION "2005 Sept 27"
69 #define DMA_ADDR_INVALID (~(dma_addr_t)0)
70 #define EP_DONTUSE 13 /* nonzero */
72 #define USE_RDK_LEDS /* GPIO pins control three LEDs */
75 static const char driver_name [] = "net2280";
76 static const char driver_desc [] = DRIVER_DESC;
78 static const char ep0name [] = "ep0";
79 static const char *const ep_name [] = {
81 "ep-a", "ep-b", "ep-c", "ep-d",
85 /* use_dma -- general goodness, fewer interrupts, less cpu load (vs PIO)
86 * use_dma_chaining -- dma descriptor queueing gives even more irq reduction
88 * The net2280 DMA engines are not tightly integrated with their FIFOs;
89 * not all cases are (yet) handled well in this driver or the silicon.
90 * Some gadget drivers work better with the dma support here than others.
91 * These two parameters let you use PIO or more aggressive DMA.
93 static bool use_dma = 1;
94 static bool use_dma_chaining = 0;
96 /* "modprobe net2280 use_dma=n" etc */
97 module_param (use_dma, bool, S_IRUGO);
98 module_param (use_dma_chaining, bool, S_IRUGO);
101 /* mode 0 == ep-{a,b,c,d} 1K fifo each
102 * mode 1 == ep-{a,b} 2K fifo each, ep-{c,d} unavailable
103 * mode 2 == ep-a 2K fifo, ep-{b,c} 1K each, ep-d unavailable
105 static ushort fifo_mode = 0;
107 /* "modprobe net2280 fifo_mode=1" etc */
108 module_param (fifo_mode, ushort, 0644);
110 /* enable_suspend -- When enabled, the driver will respond to
111 * USB suspend requests by powering down the NET2280. Otherwise,
112 * USB suspend requests will be ignored. This is acceptable for
113 * self-powered devices
115 static bool enable_suspend = 0;
117 /* "modprobe net2280 enable_suspend=1" etc */
118 module_param (enable_suspend, bool, S_IRUGO);
121 #define DIR_STRING(bAddress) (((bAddress) & USB_DIR_IN) ? "in" : "out")
123 #if defined(CONFIG_USB_GADGET_DEBUG_FILES) || defined (DEBUG)
124 static char *type_string (u8 bmAttributes)
126 switch ((bmAttributes) & USB_ENDPOINT_XFERTYPE_MASK) {
127 case USB_ENDPOINT_XFER_BULK: return "bulk";
128 case USB_ENDPOINT_XFER_ISOC: return "iso";
129 case USB_ENDPOINT_XFER_INT: return "intr";
137 #define valid_bit cpu_to_le32 (1 << VALID_BIT)
138 #define dma_done_ie cpu_to_le32 (1 << DMA_DONE_INTERRUPT_ENABLE)
140 /*-------------------------------------------------------------------------*/
143 net2280_enable (struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
146 struct net2280_ep *ep;
150 ep = container_of (_ep, struct net2280_ep, ep);
151 if (!_ep || !desc || ep->desc || _ep->name == ep0name
152 || desc->bDescriptorType != USB_DT_ENDPOINT)
155 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
158 /* erratum 0119 workaround ties up an endpoint number */
159 if ((desc->bEndpointAddress & 0x0f) == EP_DONTUSE)
162 /* sanity check ep-e/ep-f since their fifos are small */
163 max = usb_endpoint_maxp (desc) & 0x1fff;
164 if (ep->num > 4 && max > 64)
167 spin_lock_irqsave (&dev->lock, flags);
168 _ep->maxpacket = max & 0x7ff;
171 /* ep_reset() has already been called */
174 ep->out_overflow = 0;
176 /* set speed-dependent max packet; may kick in high bandwidth */
177 set_idx_reg (dev->regs, REG_EP_MAXPKT (dev, ep->num), max);
179 /* FIFO lines can't go to different packets. PIO is ok, so
180 * use it instead of troublesome (non-bulk) multi-packet DMA.
182 if (ep->dma && (max % 4) != 0 && use_dma_chaining) {
183 DEBUG (ep->dev, "%s, no dma for maxpacket %d\n",
184 ep->ep.name, ep->ep.maxpacket);
188 /* set type, direction, address; reset fifo counters */
189 writel ((1 << FIFO_FLUSH), &ep->regs->ep_stat);
190 tmp = (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK);
191 if (tmp == USB_ENDPOINT_XFER_INT) {
192 /* erratum 0105 workaround prevents hs NYET */
193 if (dev->chiprev == 0100
194 && dev->gadget.speed == USB_SPEED_HIGH
195 && !(desc->bEndpointAddress & USB_DIR_IN))
196 writel ((1 << CLEAR_NAK_OUT_PACKETS_MODE),
198 } else if (tmp == USB_ENDPOINT_XFER_BULK) {
199 /* catch some particularly blatant driver bugs */
200 if ((dev->gadget.speed == USB_SPEED_HIGH
202 || (dev->gadget.speed == USB_SPEED_FULL
204 spin_unlock_irqrestore (&dev->lock, flags);
208 ep->is_iso = (tmp == USB_ENDPOINT_XFER_ISOC) ? 1 : 0;
209 tmp <<= ENDPOINT_TYPE;
210 tmp |= desc->bEndpointAddress;
211 tmp |= (4 << ENDPOINT_BYTE_COUNT); /* default full fifo lines */
212 tmp |= 1 << ENDPOINT_ENABLE;
215 /* for OUT transfers, block the rx fifo until a read is posted */
216 ep->is_in = (tmp & USB_DIR_IN) != 0;
218 writel ((1 << SET_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
219 else if (dev->pdev->device != 0x2280) {
220 /* Added for 2282, Don't use nak packets on an in endpoint,
221 * this was ignored on 2280
223 writel ((1 << CLEAR_NAK_OUT_PACKETS)
224 | (1 << CLEAR_NAK_OUT_PACKETS_MODE), &ep->regs->ep_rsp);
227 writel (tmp, &ep->regs->ep_cfg);
230 if (!ep->dma) { /* pio, per-packet */
231 tmp = (1 << ep->num) | readl (&dev->regs->pciirqenb0);
232 writel (tmp, &dev->regs->pciirqenb0);
234 tmp = (1 << DATA_PACKET_RECEIVED_INTERRUPT_ENABLE)
235 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT_ENABLE);
236 if (dev->pdev->device == 0x2280)
237 tmp |= readl (&ep->regs->ep_irqenb);
238 writel (tmp, &ep->regs->ep_irqenb);
239 } else { /* dma, per-request */
240 tmp = (1 << (8 + ep->num)); /* completion */
241 tmp |= readl (&dev->regs->pciirqenb1);
242 writel (tmp, &dev->regs->pciirqenb1);
244 /* for short OUT transfers, dma completions can't
245 * advance the queue; do it pio-style, by hand.
246 * NOTE erratum 0112 workaround #2
248 if ((desc->bEndpointAddress & USB_DIR_IN) == 0) {
249 tmp = (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT_ENABLE);
250 writel (tmp, &ep->regs->ep_irqenb);
252 tmp = (1 << ep->num) | readl (&dev->regs->pciirqenb0);
253 writel (tmp, &dev->regs->pciirqenb0);
257 tmp = desc->bEndpointAddress;
258 DEBUG (dev, "enabled %s (ep%d%s-%s) %s max %04x\n",
259 _ep->name, tmp & 0x0f, DIR_STRING (tmp),
260 type_string (desc->bmAttributes),
261 ep->dma ? "dma" : "pio", max);
263 /* pci writes may still be posted */
264 spin_unlock_irqrestore (&dev->lock, flags);
268 static int handshake (u32 __iomem *ptr, u32 mask, u32 done, int usec)
273 result = readl (ptr);
274 if (result == ~(u32)0) /* "device unplugged" */
285 static const struct usb_ep_ops net2280_ep_ops;
287 static void ep_reset (struct net2280_regs __iomem *regs, struct net2280_ep *ep)
292 INIT_LIST_HEAD (&ep->queue);
294 ep->ep.maxpacket = ~0;
295 ep->ep.ops = &net2280_ep_ops;
297 /* disable the dma, irqs, endpoint... */
299 writel (0, &ep->dma->dmactl);
300 writel ( (1 << DMA_SCATTER_GATHER_DONE_INTERRUPT)
301 | (1 << DMA_TRANSACTION_DONE_INTERRUPT)
303 , &ep->dma->dmastat);
305 tmp = readl (®s->pciirqenb0);
306 tmp &= ~(1 << ep->num);
307 writel (tmp, ®s->pciirqenb0);
309 tmp = readl (®s->pciirqenb1);
310 tmp &= ~(1 << (8 + ep->num)); /* completion */
311 writel (tmp, ®s->pciirqenb1);
313 writel (0, &ep->regs->ep_irqenb);
315 /* init to our chosen defaults, notably so that we NAK OUT
316 * packets until the driver queues a read (+note erratum 0112)
318 if (!ep->is_in || ep->dev->pdev->device == 0x2280) {
319 tmp = (1 << SET_NAK_OUT_PACKETS_MODE)
320 | (1 << SET_NAK_OUT_PACKETS)
321 | (1 << CLEAR_EP_HIDE_STATUS_PHASE)
322 | (1 << CLEAR_INTERRUPT_MODE);
325 tmp = (1 << CLEAR_NAK_OUT_PACKETS_MODE)
326 | (1 << CLEAR_NAK_OUT_PACKETS)
327 | (1 << CLEAR_EP_HIDE_STATUS_PHASE)
328 | (1 << CLEAR_INTERRUPT_MODE);
332 tmp |= (1 << CLEAR_ENDPOINT_TOGGLE)
333 | (1 << CLEAR_ENDPOINT_HALT);
335 writel (tmp, &ep->regs->ep_rsp);
337 /* scrub most status bits, and flush any fifo state */
338 if (ep->dev->pdev->device == 0x2280)
339 tmp = (1 << FIFO_OVERFLOW)
340 | (1 << FIFO_UNDERFLOW);
344 writel (tmp | (1 << TIMEOUT)
345 | (1 << USB_STALL_SENT)
346 | (1 << USB_IN_NAK_SENT)
347 | (1 << USB_IN_ACK_RCVD)
348 | (1 << USB_OUT_PING_NAK_SENT)
349 | (1 << USB_OUT_ACK_SENT)
351 | (1 << SHORT_PACKET_OUT_DONE_INTERRUPT)
352 | (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)
353 | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
354 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
355 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
356 | (1 << DATA_IN_TOKEN_INTERRUPT)
357 , &ep->regs->ep_stat);
359 /* fifo size is handled separately */
362 static void nuke (struct net2280_ep *);
364 static int net2280_disable (struct usb_ep *_ep)
366 struct net2280_ep *ep;
369 ep = container_of (_ep, struct net2280_ep, ep);
370 if (!_ep || !ep->desc || _ep->name == ep0name)
373 spin_lock_irqsave (&ep->dev->lock, flags);
375 ep_reset (ep->dev->regs, ep);
377 VDEBUG (ep->dev, "disabled %s %s\n",
378 ep->dma ? "dma" : "pio", _ep->name);
380 /* synch memory views with the device */
381 (void) readl (&ep->regs->ep_cfg);
383 if (use_dma && !ep->dma && ep->num >= 1 && ep->num <= 4)
384 ep->dma = &ep->dev->dma [ep->num - 1];
386 spin_unlock_irqrestore (&ep->dev->lock, flags);
390 /*-------------------------------------------------------------------------*/
392 static struct usb_request *
393 net2280_alloc_request (struct usb_ep *_ep, gfp_t gfp_flags)
395 struct net2280_ep *ep;
396 struct net2280_request *req;
400 ep = container_of (_ep, struct net2280_ep, ep);
402 req = kzalloc(sizeof(*req), gfp_flags);
406 req->req.dma = DMA_ADDR_INVALID;
407 INIT_LIST_HEAD (&req->queue);
409 /* this dma descriptor may be swapped with the previous dummy */
411 struct net2280_dma *td;
413 td = pci_pool_alloc (ep->dev->requests, gfp_flags,
419 td->dmacount = 0; /* not VALID */
420 td->dmaaddr = cpu_to_le32 (DMA_ADDR_INVALID);
421 td->dmadesc = td->dmaaddr;
428 net2280_free_request (struct usb_ep *_ep, struct usb_request *_req)
430 struct net2280_ep *ep;
431 struct net2280_request *req;
433 ep = container_of (_ep, struct net2280_ep, ep);
437 req = container_of (_req, struct net2280_request, req);
438 WARN_ON (!list_empty (&req->queue));
440 pci_pool_free (ep->dev->requests, req->td, req->td_dma);
444 /*-------------------------------------------------------------------------*/
446 /* load a packet into the fifo we use for usb IN transfers.
447 * works for all endpoints.
449 * NOTE: pio with ep-a..ep-d could stuff multiple packets into the fifo
450 * at a time, but this code is simpler because it knows it only writes
451 * one packet. ep-a..ep-d should use dma instead.
454 write_fifo (struct net2280_ep *ep, struct usb_request *req)
456 struct net2280_ep_regs __iomem *regs = ep->regs;
459 unsigned count, total;
461 /* INVARIANT: fifo is currently empty. (testable) */
464 buf = req->buf + req->actual;
466 total = req->length - req->actual;
472 /* write just one packet at a time */
473 count = ep->ep.maxpacket;
474 if (count > total) /* min() cannot be used on a bitfield */
477 VDEBUG (ep->dev, "write %s fifo (IN) %d bytes%s req %p\n",
479 (count != ep->ep.maxpacket) ? " (short)" : "",
482 /* NOTE be careful if you try to align these. fifo lines
483 * should normally be full (4 bytes) and successive partial
484 * lines are ok only in certain cases.
486 tmp = get_unaligned ((u32 *)buf);
488 writel (tmp, ®s->ep_data);
493 /* last fifo entry is "short" unless we wrote a full packet.
494 * also explicitly validate last word in (periodic) transfers
495 * when maxpacket is not a multiple of 4 bytes.
497 if (count || total < ep->ep.maxpacket) {
498 tmp = count ? get_unaligned ((u32 *)buf) : count;
500 set_fifo_bytecount (ep, count & 0x03);
501 writel (tmp, ®s->ep_data);
504 /* pci writes may still be posted */
507 /* work around erratum 0106: PCI and USB race over the OUT fifo.
508 * caller guarantees chiprev 0100, out endpoint is NAKing, and
509 * there's no real data in the fifo.
511 * NOTE: also used in cases where that erratum doesn't apply:
512 * where the host wrote "too much" data to us.
514 static void out_flush (struct net2280_ep *ep)
519 ASSERT_OUT_NAKING (ep);
521 statp = &ep->regs->ep_stat;
522 writel ( (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
523 | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
525 writel ((1 << FIFO_FLUSH), statp);
528 if (tmp & (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
529 /* high speed did bulk NYET; fifo isn't filling */
530 && ep->dev->gadget.speed == USB_SPEED_FULL) {
533 usec = 50; /* 64 byte bulk/interrupt */
534 handshake (statp, (1 << USB_OUT_PING_NAK_SENT),
535 (1 << USB_OUT_PING_NAK_SENT), usec);
536 /* NAK done; now CLEAR_NAK_OUT_PACKETS is safe */
540 /* unload packet(s) from the fifo we use for usb OUT transfers.
541 * returns true iff the request completed, because of short packet
542 * or the request buffer having filled with full packets.
544 * for ep-a..ep-d this will read multiple packets out when they
545 * have been accepted.
548 read_fifo (struct net2280_ep *ep, struct net2280_request *req)
550 struct net2280_ep_regs __iomem *regs = ep->regs;
551 u8 *buf = req->req.buf + req->req.actual;
552 unsigned count, tmp, is_short;
553 unsigned cleanup = 0, prevent = 0;
555 /* erratum 0106 ... packets coming in during fifo reads might
556 * be incompletely rejected. not all cases have workarounds.
558 if (ep->dev->chiprev == 0x0100
559 && ep->dev->gadget.speed == USB_SPEED_FULL) {
561 tmp = readl (&ep->regs->ep_stat);
562 if ((tmp & (1 << NAK_OUT_PACKETS)))
564 else if ((tmp & (1 << FIFO_FULL))) {
565 start_out_naking (ep);
568 /* else: hope we don't see the problem */
571 /* never overflow the rx buffer. the fifo reads packets until
572 * it sees a short one; we might not be ready for them all.
575 count = readl (®s->ep_avail);
576 if (unlikely (count == 0)) {
578 tmp = readl (&ep->regs->ep_stat);
579 count = readl (®s->ep_avail);
580 /* handled that data already? */
581 if (count == 0 && (tmp & (1 << NAK_OUT_PACKETS)) == 0)
585 tmp = req->req.length - req->req.actual;
587 /* as with DMA, data overflow gets flushed */
588 if ((tmp % ep->ep.maxpacket) != 0) {
590 "%s out fifo %d bytes, expected %d\n",
591 ep->ep.name, count, tmp);
592 req->req.status = -EOVERFLOW;
594 /* NAK_OUT_PACKETS will be set, so flushing is safe;
595 * the next read will start with the next packet
597 } /* else it's a ZLP, no worries */
600 req->req.actual += count;
602 is_short = (count == 0) || ((count % ep->ep.maxpacket) != 0);
604 VDEBUG (ep->dev, "read %s fifo (OUT) %d bytes%s%s%s req %p %d/%d\n",
605 ep->ep.name, count, is_short ? " (short)" : "",
606 cleanup ? " flush" : "", prevent ? " nak" : "",
607 req, req->req.actual, req->req.length);
610 tmp = readl (®s->ep_data);
612 put_unaligned (tmp, (u32 *)buf);
617 tmp = readl (®s->ep_data);
618 /* LE conversion is implicit here: */
627 writel ((1 << CLEAR_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
628 (void) readl (&ep->regs->ep_rsp);
631 return is_short || ((req->req.actual == req->req.length)
635 /* fill out dma descriptor to match a given request */
637 fill_dma_desc (struct net2280_ep *ep, struct net2280_request *req, int valid)
639 struct net2280_dma *td = req->td;
640 u32 dmacount = req->req.length;
642 /* don't let DMA continue after a short OUT packet,
643 * so overruns can't affect the next transfer.
644 * in case of overruns on max-size packets, we can't
645 * stop the fifo from filling but we can flush it.
648 dmacount |= (1 << DMA_DIRECTION);
649 if ((!ep->is_in && (dmacount % ep->ep.maxpacket) != 0)
650 || ep->dev->pdev->device != 0x2280)
651 dmacount |= (1 << END_OF_CHAIN);
655 dmacount |= (1 << VALID_BIT);
656 if (likely(!req->req.no_interrupt || !use_dma_chaining))
657 dmacount |= (1 << DMA_DONE_INTERRUPT_ENABLE);
659 /* td->dmadesc = previously set by caller */
660 td->dmaaddr = cpu_to_le32 (req->req.dma);
662 /* 2280 may be polling VALID_BIT through ep->dma->dmadesc */
664 td->dmacount = cpu_to_le32(dmacount);
667 static const u32 dmactl_default =
668 (1 << DMA_SCATTER_GATHER_DONE_INTERRUPT)
669 | (1 << DMA_CLEAR_COUNT_ENABLE)
670 /* erratum 0116 workaround part 1 (use POLLING) */
671 | (POLL_100_USEC << DESCRIPTOR_POLLING_RATE)
672 | (1 << DMA_VALID_BIT_POLLING_ENABLE)
673 | (1 << DMA_VALID_BIT_ENABLE)
674 | (1 << DMA_SCATTER_GATHER_ENABLE)
675 /* erratum 0116 workaround part 2 (no AUTOSTART) */
678 static inline void spin_stop_dma (struct net2280_dma_regs __iomem *dma)
680 handshake (&dma->dmactl, (1 << DMA_ENABLE), 0, 50);
683 static inline void stop_dma (struct net2280_dma_regs __iomem *dma)
685 writel (readl (&dma->dmactl) & ~(1 << DMA_ENABLE), &dma->dmactl);
689 static void start_queue (struct net2280_ep *ep, u32 dmactl, u32 td_dma)
691 struct net2280_dma_regs __iomem *dma = ep->dma;
692 unsigned int tmp = (1 << VALID_BIT) | (ep->is_in << DMA_DIRECTION);
694 if (ep->dev->pdev->device != 0x2280)
695 tmp |= (1 << END_OF_CHAIN);
697 writel (tmp, &dma->dmacount);
698 writel (readl (&dma->dmastat), &dma->dmastat);
700 writel (td_dma, &dma->dmadesc);
701 writel (dmactl, &dma->dmactl);
703 /* erratum 0116 workaround part 3: pci arbiter away from net2280 */
704 (void) readl (&ep->dev->pci->pcimstctl);
706 writel ((1 << DMA_START), &dma->dmastat);
709 stop_out_naking (ep);
712 static void start_dma (struct net2280_ep *ep, struct net2280_request *req)
715 struct net2280_dma_regs __iomem *dma = ep->dma;
717 /* FIXME can't use DMA for ZLPs */
719 /* on this path we "know" there's no dma active (yet) */
720 WARN_ON (readl (&dma->dmactl) & (1 << DMA_ENABLE));
721 writel (0, &ep->dma->dmactl);
723 /* previous OUT packet might have been short */
724 if (!ep->is_in && ((tmp = readl (&ep->regs->ep_stat))
725 & (1 << NAK_OUT_PACKETS)) != 0) {
726 writel ((1 << SHORT_PACKET_TRANSFERRED_INTERRUPT),
729 tmp = readl (&ep->regs->ep_avail);
731 writel (readl (&dma->dmastat), &dma->dmastat);
733 /* transfer all/some fifo data */
734 writel (req->req.dma, &dma->dmaaddr);
735 tmp = min (tmp, req->req.length);
737 /* dma irq, faking scatterlist status */
738 req->td->dmacount = cpu_to_le32 (req->req.length - tmp);
739 writel ((1 << DMA_DONE_INTERRUPT_ENABLE)
740 | tmp, &dma->dmacount);
741 req->td->dmadesc = 0;
744 writel ((1 << DMA_ENABLE), &dma->dmactl);
745 writel ((1 << DMA_START), &dma->dmastat);
750 tmp = dmactl_default;
752 /* force packet boundaries between dma requests, but prevent the
753 * controller from automagically writing a last "short" packet
754 * (zero length) unless the driver explicitly said to do that.
757 if (likely ((req->req.length % ep->ep.maxpacket) != 0
759 tmp |= (1 << DMA_FIFO_VALIDATE);
760 ep->in_fifo_validate = 1;
762 ep->in_fifo_validate = 0;
765 /* init req->td, pointing to the current dummy */
766 req->td->dmadesc = cpu_to_le32 (ep->td_dma);
767 fill_dma_desc (ep, req, 1);
769 if (!use_dma_chaining)
770 req->td->dmacount |= cpu_to_le32 (1 << END_OF_CHAIN);
772 start_queue (ep, tmp, req->td_dma);
776 queue_dma (struct net2280_ep *ep, struct net2280_request *req, int valid)
778 struct net2280_dma *end;
781 /* swap new dummy for old, link; fill and maybe activate */
787 ep->td_dma = req->td_dma;
790 end->dmadesc = cpu_to_le32 (ep->td_dma);
792 fill_dma_desc (ep, req, valid);
796 done (struct net2280_ep *ep, struct net2280_request *req, int status)
799 unsigned stopped = ep->stopped;
801 list_del_init (&req->queue);
803 if (req->req.status == -EINPROGRESS)
804 req->req.status = status;
806 status = req->req.status;
810 usb_gadget_unmap_request(&dev->gadget, &req->req, ep->is_in);
812 if (status && status != -ESHUTDOWN)
813 VDEBUG (dev, "complete %s req %p stat %d len %u/%u\n",
814 ep->ep.name, &req->req, status,
815 req->req.actual, req->req.length);
817 /* don't modify queue heads during completion callback */
819 spin_unlock (&dev->lock);
820 req->req.complete (&ep->ep, &req->req);
821 spin_lock (&dev->lock);
822 ep->stopped = stopped;
825 /*-------------------------------------------------------------------------*/
828 net2280_queue (struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
830 struct net2280_request *req;
831 struct net2280_ep *ep;
835 /* we always require a cpu-view buffer, so that we can
836 * always use pio (as fallback or whatever).
838 req = container_of (_req, struct net2280_request, req);
839 if (!_req || !_req->complete || !_req->buf
840 || !list_empty (&req->queue))
842 if (_req->length > (~0 & DMA_BYTE_COUNT_MASK))
844 ep = container_of (_ep, struct net2280_ep, ep);
845 if (!_ep || (!ep->desc && ep->num != 0))
848 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
851 /* FIXME implement PIO fallback for ZLPs with DMA */
852 if (ep->dma && _req->length == 0)
855 /* set up dma mapping in case the caller didn't */
859 ret = usb_gadget_map_request(&dev->gadget, _req,
866 VDEBUG (dev, "%s queue req %p, len %d buf %p\n",
867 _ep->name, _req, _req->length, _req->buf);
870 spin_lock_irqsave (&dev->lock, flags);
872 _req->status = -EINPROGRESS;
875 /* kickstart this i/o queue? */
876 if (list_empty (&ep->queue) && !ep->stopped) {
877 /* use DMA if the endpoint supports it, else pio */
881 /* maybe there's no control data, just status ack */
882 if (ep->num == 0 && _req->length == 0) {
885 VDEBUG (dev, "%s status ack\n", ep->ep.name);
889 /* PIO ... stuff the fifo, or unblock it. */
891 write_fifo (ep, _req);
892 else if (list_empty (&ep->queue)) {
895 /* OUT FIFO might have packet(s) buffered */
896 s = readl (&ep->regs->ep_stat);
897 if ((s & (1 << FIFO_EMPTY)) == 0) {
898 /* note: _req->short_not_ok is
899 * ignored here since PIO _always_
900 * stops queue advance here, and
901 * _req->status doesn't change for
902 * short reads (only _req->actual)
904 if (read_fifo (ep, req)) {
911 s = readl (&ep->regs->ep_stat);
914 /* don't NAK, let the fifo fill */
915 if (req && (s & (1 << NAK_OUT_PACKETS)))
916 writel ((1 << CLEAR_NAK_OUT_PACKETS),
921 } else if (ep->dma) {
927 /* preventing magic zlps is per-engine state, not
928 * per-transfer; irq logic must recover hiccups.
930 expect = likely (req->req.zero
931 || (req->req.length % ep->ep.maxpacket) != 0);
932 if (expect != ep->in_fifo_validate)
935 queue_dma (ep, req, valid);
937 } /* else the irq handler advances the queue. */
941 list_add_tail (&req->queue, &ep->queue);
943 spin_unlock_irqrestore (&dev->lock, flags);
945 /* pci writes may still be posted */
951 struct net2280_ep *ep,
952 struct net2280_request *req,
957 req->req.actual = req->req.length - (DMA_BYTE_COUNT_MASK & dmacount);
958 done (ep, req, status);
961 static void restart_dma (struct net2280_ep *ep);
963 static void scan_dma_completions (struct net2280_ep *ep)
965 /* only look at descriptors that were "naturally" retired,
966 * so fifo and list head state won't matter
968 while (!list_empty (&ep->queue)) {
969 struct net2280_request *req;
972 req = list_entry (ep->queue.next,
973 struct net2280_request, queue);
977 tmp = le32_to_cpup (&req->td->dmacount);
978 if ((tmp & (1 << VALID_BIT)) != 0)
981 /* SHORT_PACKET_TRANSFERRED_INTERRUPT handles "usb-short"
982 * cases where DMA must be aborted; this code handles
983 * all non-abort DMA completions.
985 if (unlikely (req->td->dmadesc == 0)) {
987 tmp = readl (&ep->dma->dmacount);
988 if (tmp & DMA_BYTE_COUNT_MASK)
990 /* single transfer mode */
991 dma_done (ep, req, tmp, 0);
993 } else if (!ep->is_in
994 && (req->req.length % ep->ep.maxpacket) != 0) {
995 tmp = readl (&ep->regs->ep_stat);
997 /* AVOID TROUBLE HERE by not issuing short reads from
998 * your gadget driver. That helps avoids errata 0121,
999 * 0122, and 0124; not all cases trigger the warning.
1001 if ((tmp & (1 << NAK_OUT_PACKETS)) == 0) {
1002 WARNING (ep->dev, "%s lost packet sync!\n",
1004 req->req.status = -EOVERFLOW;
1005 } else if ((tmp = readl (&ep->regs->ep_avail)) != 0) {
1006 /* fifo gets flushed later */
1007 ep->out_overflow = 1;
1008 DEBUG (ep->dev, "%s dma, discard %d len %d\n",
1011 req->req.status = -EOVERFLOW;
1014 dma_done (ep, req, tmp, 0);
1018 static void restart_dma (struct net2280_ep *ep)
1020 struct net2280_request *req;
1021 u32 dmactl = dmactl_default;
1025 req = list_entry (ep->queue.next, struct net2280_request, queue);
1027 if (!use_dma_chaining) {
1028 start_dma (ep, req);
1032 /* the 2280 will be processing the queue unless queue hiccups after
1033 * the previous transfer:
1034 * IN: wanted automagic zlp, head doesn't (or vice versa)
1035 * DMA_FIFO_VALIDATE doesn't init from dma descriptors.
1036 * OUT: was "usb-short", we must restart.
1038 if (ep->is_in && !req->valid) {
1039 struct net2280_request *entry, *prev = NULL;
1040 int reqmode, done = 0;
1042 DEBUG (ep->dev, "%s dma hiccup td %p\n", ep->ep.name, req->td);
1043 ep->in_fifo_validate = likely (req->req.zero
1044 || (req->req.length % ep->ep.maxpacket) != 0);
1045 if (ep->in_fifo_validate)
1046 dmactl |= (1 << DMA_FIFO_VALIDATE);
1047 list_for_each_entry (entry, &ep->queue, queue) {
1052 dmacount = entry->td->dmacount;
1054 reqmode = likely (entry->req.zero
1055 || (entry->req.length
1056 % ep->ep.maxpacket) != 0);
1057 if (reqmode == ep->in_fifo_validate) {
1059 dmacount |= valid_bit;
1060 entry->td->dmacount = dmacount;
1064 /* force a hiccup */
1065 prev->td->dmacount |= dma_done_ie;
1070 /* walk the rest of the queue so unlinks behave */
1072 dmacount &= ~valid_bit;
1073 entry->td->dmacount = dmacount;
1078 writel (0, &ep->dma->dmactl);
1079 start_queue (ep, dmactl, req->td_dma);
1082 static void abort_dma (struct net2280_ep *ep)
1084 /* abort the current transfer */
1085 if (likely (!list_empty (&ep->queue))) {
1086 /* FIXME work around errata 0121, 0122, 0124 */
1087 writel ((1 << DMA_ABORT), &ep->dma->dmastat);
1088 spin_stop_dma (ep->dma);
1091 scan_dma_completions (ep);
1094 /* dequeue ALL requests */
1095 static void nuke (struct net2280_ep *ep)
1097 struct net2280_request *req;
1099 /* called with spinlock held */
1103 while (!list_empty (&ep->queue)) {
1104 req = list_entry (ep->queue.next,
1105 struct net2280_request,
1107 done (ep, req, -ESHUTDOWN);
1111 /* dequeue JUST ONE request */
1112 static int net2280_dequeue (struct usb_ep *_ep, struct usb_request *_req)
1114 struct net2280_ep *ep;
1115 struct net2280_request *req;
1116 unsigned long flags;
1120 ep = container_of (_ep, struct net2280_ep, ep);
1121 if (!_ep || (!ep->desc && ep->num != 0) || !_req)
1124 spin_lock_irqsave (&ep->dev->lock, flags);
1125 stopped = ep->stopped;
1127 /* quiesce dma while we patch the queue */
1131 dmactl = readl (&ep->dma->dmactl);
1132 /* WARNING erratum 0127 may kick in ... */
1134 scan_dma_completions (ep);
1137 /* make sure it's still queued on this endpoint */
1138 list_for_each_entry (req, &ep->queue, queue) {
1139 if (&req->req == _req)
1142 if (&req->req != _req) {
1143 spin_unlock_irqrestore (&ep->dev->lock, flags);
1147 /* queue head may be partially complete. */
1148 if (ep->queue.next == &req->queue) {
1150 DEBUG (ep->dev, "unlink (%s) dma\n", _ep->name);
1151 _req->status = -ECONNRESET;
1153 if (likely (ep->queue.next == &req->queue)) {
1154 // NOTE: misreports single-transfer mode
1155 req->td->dmacount = 0; /* invalidate */
1157 readl (&ep->dma->dmacount),
1161 DEBUG (ep->dev, "unlink (%s) pio\n", _ep->name);
1162 done (ep, req, -ECONNRESET);
1166 /* patch up hardware chaining data */
1167 } else if (ep->dma && use_dma_chaining) {
1168 if (req->queue.prev == ep->queue.next) {
1169 writel (le32_to_cpu (req->td->dmadesc),
1171 if (req->td->dmacount & dma_done_ie)
1172 writel (readl (&ep->dma->dmacount)
1173 | le32_to_cpu(dma_done_ie),
1174 &ep->dma->dmacount);
1176 struct net2280_request *prev;
1178 prev = list_entry (req->queue.prev,
1179 struct net2280_request, queue);
1180 prev->td->dmadesc = req->td->dmadesc;
1181 if (req->td->dmacount & dma_done_ie)
1182 prev->td->dmacount |= dma_done_ie;
1187 done (ep, req, -ECONNRESET);
1188 ep->stopped = stopped;
1191 /* turn off dma on inactive queues */
1192 if (list_empty (&ep->queue))
1194 else if (!ep->stopped) {
1195 /* resume current request, or start new one */
1197 writel (dmactl, &ep->dma->dmactl);
1199 start_dma (ep, list_entry (ep->queue.next,
1200 struct net2280_request, queue));
1204 spin_unlock_irqrestore (&ep->dev->lock, flags);
1208 /*-------------------------------------------------------------------------*/
1210 static int net2280_fifo_status (struct usb_ep *_ep);
1213 net2280_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged)
1215 struct net2280_ep *ep;
1216 unsigned long flags;
1219 ep = container_of (_ep, struct net2280_ep, ep);
1220 if (!_ep || (!ep->desc && ep->num != 0))
1222 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1224 if (ep->desc /* not ep0 */ && (ep->desc->bmAttributes & 0x03)
1225 == USB_ENDPOINT_XFER_ISOC)
1228 spin_lock_irqsave (&ep->dev->lock, flags);
1229 if (!list_empty (&ep->queue))
1231 else if (ep->is_in && value && net2280_fifo_status (_ep) != 0)
1234 VDEBUG (ep->dev, "%s %s %s\n", _ep->name,
1235 value ? "set" : "clear",
1236 wedged ? "wedge" : "halt");
1237 /* set/clear, then synch memory views with the device */
1240 ep->dev->protocol_stall = 1;
1249 (void) readl (&ep->regs->ep_rsp);
1251 spin_unlock_irqrestore (&ep->dev->lock, flags);
1257 net2280_set_halt(struct usb_ep *_ep, int value)
1259 return net2280_set_halt_and_wedge(_ep, value, 0);
1263 net2280_set_wedge(struct usb_ep *_ep)
1265 if (!_ep || _ep->name == ep0name)
1267 return net2280_set_halt_and_wedge(_ep, 1, 1);
1271 net2280_fifo_status (struct usb_ep *_ep)
1273 struct net2280_ep *ep;
1276 ep = container_of (_ep, struct net2280_ep, ep);
1277 if (!_ep || (!ep->desc && ep->num != 0))
1279 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1282 avail = readl (&ep->regs->ep_avail) & ((1 << 12) - 1);
1283 if (avail > ep->fifo_size)
1286 avail = ep->fifo_size - avail;
1291 net2280_fifo_flush (struct usb_ep *_ep)
1293 struct net2280_ep *ep;
1295 ep = container_of (_ep, struct net2280_ep, ep);
1296 if (!_ep || (!ep->desc && ep->num != 0))
1298 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1301 writel ((1 << FIFO_FLUSH), &ep->regs->ep_stat);
1302 (void) readl (&ep->regs->ep_rsp);
1305 static const struct usb_ep_ops net2280_ep_ops = {
1306 .enable = net2280_enable,
1307 .disable = net2280_disable,
1309 .alloc_request = net2280_alloc_request,
1310 .free_request = net2280_free_request,
1312 .queue = net2280_queue,
1313 .dequeue = net2280_dequeue,
1315 .set_halt = net2280_set_halt,
1316 .set_wedge = net2280_set_wedge,
1317 .fifo_status = net2280_fifo_status,
1318 .fifo_flush = net2280_fifo_flush,
1321 /*-------------------------------------------------------------------------*/
1323 static int net2280_get_frame (struct usb_gadget *_gadget)
1325 struct net2280 *dev;
1326 unsigned long flags;
1331 dev = container_of (_gadget, struct net2280, gadget);
1332 spin_lock_irqsave (&dev->lock, flags);
1333 retval = get_idx_reg (dev->regs, REG_FRAME) & 0x03ff;
1334 spin_unlock_irqrestore (&dev->lock, flags);
1338 static int net2280_wakeup (struct usb_gadget *_gadget)
1340 struct net2280 *dev;
1342 unsigned long flags;
1346 dev = container_of (_gadget, struct net2280, gadget);
1348 spin_lock_irqsave (&dev->lock, flags);
1349 tmp = readl (&dev->usb->usbctl);
1350 if (tmp & (1 << DEVICE_REMOTE_WAKEUP_ENABLE))
1351 writel (1 << GENERATE_RESUME, &dev->usb->usbstat);
1352 spin_unlock_irqrestore (&dev->lock, flags);
1354 /* pci writes may still be posted */
1358 static int net2280_set_selfpowered (struct usb_gadget *_gadget, int value)
1360 struct net2280 *dev;
1362 unsigned long flags;
1366 dev = container_of (_gadget, struct net2280, gadget);
1368 spin_lock_irqsave (&dev->lock, flags);
1369 tmp = readl (&dev->usb->usbctl);
1371 tmp |= (1 << SELF_POWERED_STATUS);
1373 tmp &= ~(1 << SELF_POWERED_STATUS);
1374 writel (tmp, &dev->usb->usbctl);
1375 spin_unlock_irqrestore (&dev->lock, flags);
1380 static int net2280_pullup(struct usb_gadget *_gadget, int is_on)
1382 struct net2280 *dev;
1384 unsigned long flags;
1388 dev = container_of (_gadget, struct net2280, gadget);
1390 spin_lock_irqsave (&dev->lock, flags);
1391 tmp = readl (&dev->usb->usbctl);
1392 dev->softconnect = (is_on != 0);
1394 tmp |= (1 << USB_DETECT_ENABLE);
1396 tmp &= ~(1 << USB_DETECT_ENABLE);
1397 writel (tmp, &dev->usb->usbctl);
1398 spin_unlock_irqrestore (&dev->lock, flags);
1403 static int net2280_start(struct usb_gadget *_gadget,
1404 struct usb_gadget_driver *driver);
1405 static int net2280_stop(struct usb_gadget *_gadget,
1406 struct usb_gadget_driver *driver);
1408 static const struct usb_gadget_ops net2280_ops = {
1409 .get_frame = net2280_get_frame,
1410 .wakeup = net2280_wakeup,
1411 .set_selfpowered = net2280_set_selfpowered,
1412 .pullup = net2280_pullup,
1413 .udc_start = net2280_start,
1414 .udc_stop = net2280_stop,
1417 /*-------------------------------------------------------------------------*/
1419 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1421 /* FIXME move these into procfs, and use seq_file.
1422 * Sysfs _still_ doesn't behave for arbitrarily sized files,
1423 * and also doesn't help products using this with 2.4 kernels.
1426 /* "function" sysfs attribute */
1428 show_function (struct device *_dev, struct device_attribute *attr, char *buf)
1430 struct net2280 *dev = dev_get_drvdata (_dev);
1433 || !dev->driver->function
1434 || strlen (dev->driver->function) > PAGE_SIZE)
1436 return scnprintf (buf, PAGE_SIZE, "%s\n", dev->driver->function);
1438 static DEVICE_ATTR (function, S_IRUGO, show_function, NULL);
1440 static ssize_t net2280_show_registers(struct device *_dev,
1441 struct device_attribute *attr, char *buf)
1443 struct net2280 *dev;
1446 unsigned long flags;
1451 dev = dev_get_drvdata (_dev);
1454 spin_lock_irqsave (&dev->lock, flags);
1457 s = dev->driver->driver.name;
1461 /* Main Control Registers */
1462 t = scnprintf (next, size, "%s version " DRIVER_VERSION
1463 ", chiprev %04x, dma %s\n\n"
1464 "devinit %03x fifoctl %08x gadget '%s'\n"
1465 "pci irqenb0 %02x irqenb1 %08x "
1466 "irqstat0 %04x irqstat1 %08x\n",
1467 driver_name, dev->chiprev,
1469 ? (use_dma_chaining ? "chaining" : "enabled")
1471 readl (&dev->regs->devinit),
1472 readl (&dev->regs->fifoctl),
1474 readl (&dev->regs->pciirqenb0),
1475 readl (&dev->regs->pciirqenb1),
1476 readl (&dev->regs->irqstat0),
1477 readl (&dev->regs->irqstat1));
1481 /* USB Control Registers */
1482 t1 = readl (&dev->usb->usbctl);
1483 t2 = readl (&dev->usb->usbstat);
1484 if (t1 & (1 << VBUS_PIN)) {
1485 if (t2 & (1 << HIGH_SPEED))
1487 else if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1491 /* full speed bit (6) not working?? */
1494 t = scnprintf (next, size,
1495 "stdrsp %08x usbctl %08x usbstat %08x "
1496 "addr 0x%02x (%s)\n",
1497 readl (&dev->usb->stdrsp), t1, t2,
1498 readl (&dev->usb->ouraddr), s);
1502 /* PCI Master Control Registers */
1504 /* DMA Control Registers */
1506 /* Configurable EP Control Registers */
1507 for (i = 0; i < 7; i++) {
1508 struct net2280_ep *ep;
1514 t1 = readl (&ep->regs->ep_cfg);
1515 t2 = readl (&ep->regs->ep_rsp) & 0xff;
1516 t = scnprintf (next, size,
1517 "\n%s\tcfg %05x rsp (%02x) %s%s%s%s%s%s%s%s"
1519 ep->ep.name, t1, t2,
1520 (t2 & (1 << CLEAR_NAK_OUT_PACKETS))
1522 (t2 & (1 << CLEAR_EP_HIDE_STATUS_PHASE))
1524 (t2 & (1 << CLEAR_EP_FORCE_CRC_ERROR))
1526 (t2 & (1 << CLEAR_INTERRUPT_MODE))
1527 ? "interrupt " : "",
1528 (t2 & (1<<CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE))
1530 (t2 & (1 << CLEAR_NAK_OUT_PACKETS_MODE))
1532 (t2 & (1 << CLEAR_ENDPOINT_TOGGLE))
1533 ? "DATA1 " : "DATA0 ",
1534 (t2 & (1 << CLEAR_ENDPOINT_HALT))
1536 readl (&ep->regs->ep_irqenb));
1540 t = scnprintf (next, size,
1541 "\tstat %08x avail %04x "
1543 readl (&ep->regs->ep_stat),
1544 readl (&ep->regs->ep_avail),
1545 t1 & 0x0f, DIR_STRING (t1),
1546 type_string (t1 >> 8),
1547 ep->stopped ? "*" : "");
1554 t = scnprintf (next, size,
1555 " dma\tctl %08x stat %08x count %08x\n"
1556 "\taddr %08x desc %08x\n",
1557 readl (&ep->dma->dmactl),
1558 readl (&ep->dma->dmastat),
1559 readl (&ep->dma->dmacount),
1560 readl (&ep->dma->dmaaddr),
1561 readl (&ep->dma->dmadesc));
1567 /* Indexed Registers */
1571 t = scnprintf (next, size, "\nirqs: ");
1574 for (i = 0; i < 7; i++) {
1575 struct net2280_ep *ep;
1580 t = scnprintf (next, size, " %s/%lu", ep->ep.name, ep->irqs);
1585 t = scnprintf (next, size, "\n");
1589 spin_unlock_irqrestore (&dev->lock, flags);
1591 return PAGE_SIZE - size;
1593 static DEVICE_ATTR(registers, S_IRUGO, net2280_show_registers, NULL);
1596 show_queues (struct device *_dev, struct device_attribute *attr, char *buf)
1598 struct net2280 *dev;
1601 unsigned long flags;
1604 dev = dev_get_drvdata (_dev);
1607 spin_lock_irqsave (&dev->lock, flags);
1609 for (i = 0; i < 7; i++) {
1610 struct net2280_ep *ep = &dev->ep [i];
1611 struct net2280_request *req;
1615 const struct usb_endpoint_descriptor *d;
1620 t = d->bEndpointAddress;
1621 t = scnprintf (next, size,
1622 "\n%s (ep%d%s-%s) max %04x %s fifo %d\n",
1623 ep->ep.name, t & USB_ENDPOINT_NUMBER_MASK,
1624 (t & USB_DIR_IN) ? "in" : "out",
1626 switch (d->bmAttributes & 0x03) {
1627 case USB_ENDPOINT_XFER_BULK:
1628 val = "bulk"; break;
1629 case USB_ENDPOINT_XFER_INT:
1630 val = "intr"; break;
1634 usb_endpoint_maxp (d) & 0x1fff,
1635 ep->dma ? "dma" : "pio", ep->fifo_size
1637 } else /* ep0 should only have one transfer queued */
1638 t = scnprintf (next, size, "ep0 max 64 pio %s\n",
1639 ep->is_in ? "in" : "out");
1640 if (t <= 0 || t > size)
1645 if (list_empty (&ep->queue)) {
1646 t = scnprintf (next, size, "\t(nothing queued)\n");
1647 if (t <= 0 || t > size)
1653 list_for_each_entry (req, &ep->queue, queue) {
1654 if (ep->dma && req->td_dma == readl (&ep->dma->dmadesc))
1655 t = scnprintf (next, size,
1656 "\treq %p len %d/%d "
1657 "buf %p (dmacount %08x)\n",
1658 &req->req, req->req.actual,
1659 req->req.length, req->req.buf,
1660 readl (&ep->dma->dmacount));
1662 t = scnprintf (next, size,
1663 "\treq %p len %d/%d buf %p\n",
1664 &req->req, req->req.actual,
1665 req->req.length, req->req.buf);
1666 if (t <= 0 || t > size)
1672 struct net2280_dma *td;
1675 t = scnprintf (next, size, "\t td %08x "
1676 " count %08x buf %08x desc %08x\n",
1678 le32_to_cpu (td->dmacount),
1679 le32_to_cpu (td->dmaaddr),
1680 le32_to_cpu (td->dmadesc));
1681 if (t <= 0 || t > size)
1690 spin_unlock_irqrestore (&dev->lock, flags);
1691 return PAGE_SIZE - size;
1693 static DEVICE_ATTR (queues, S_IRUGO, show_queues, NULL);
1698 #define device_create_file(a,b) (0)
1699 #define device_remove_file(a,b) do { } while (0)
1703 /*-------------------------------------------------------------------------*/
1705 /* another driver-specific mode might be a request type doing dma
1706 * to/from another device fifo instead of to/from memory.
1709 static void set_fifo_mode (struct net2280 *dev, int mode)
1711 /* keeping high bits preserves BAR2 */
1712 writel ((0xffff << PCI_BASE2_RANGE) | mode, &dev->regs->fifoctl);
1714 /* always ep-{a,b,e,f} ... maybe not ep-c or ep-d */
1715 INIT_LIST_HEAD (&dev->gadget.ep_list);
1716 list_add_tail (&dev->ep [1].ep.ep_list, &dev->gadget.ep_list);
1717 list_add_tail (&dev->ep [2].ep.ep_list, &dev->gadget.ep_list);
1720 list_add_tail (&dev->ep [3].ep.ep_list, &dev->gadget.ep_list);
1721 list_add_tail (&dev->ep [4].ep.ep_list, &dev->gadget.ep_list);
1722 dev->ep [1].fifo_size = dev->ep [2].fifo_size = 1024;
1725 dev->ep [1].fifo_size = dev->ep [2].fifo_size = 2048;
1728 list_add_tail (&dev->ep [3].ep.ep_list, &dev->gadget.ep_list);
1729 dev->ep [1].fifo_size = 2048;
1730 dev->ep [2].fifo_size = 1024;
1733 /* fifo sizes for ep0, ep-c, ep-d, ep-e, and ep-f never change */
1734 list_add_tail (&dev->ep [5].ep.ep_list, &dev->gadget.ep_list);
1735 list_add_tail (&dev->ep [6].ep.ep_list, &dev->gadget.ep_list);
1738 /* keeping it simple:
1739 * - one bus driver, initted first;
1740 * - one function driver, initted second
1742 * most of the work to support multiple net2280 controllers would
1743 * be to associate this gadget driver (yes?) with all of them, or
1744 * perhaps to bind specific drivers to specific devices.
1747 static void usb_reset (struct net2280 *dev)
1751 dev->gadget.speed = USB_SPEED_UNKNOWN;
1752 (void) readl (&dev->usb->usbctl);
1754 net2280_led_init (dev);
1756 /* disable automatic responses, and irqs */
1757 writel (0, &dev->usb->stdrsp);
1758 writel (0, &dev->regs->pciirqenb0);
1759 writel (0, &dev->regs->pciirqenb1);
1761 /* clear old dma and irq state */
1762 for (tmp = 0; tmp < 4; tmp++) {
1763 struct net2280_ep *ep = &dev->ep [tmp + 1];
1768 writel (~0, &dev->regs->irqstat0),
1769 writel (~(1 << SUSPEND_REQUEST_INTERRUPT), &dev->regs->irqstat1),
1771 /* reset, and enable pci */
1772 tmp = readl (&dev->regs->devinit)
1774 | (1 << FIFO_SOFT_RESET)
1775 | (1 << USB_SOFT_RESET)
1776 | (1 << M8051_RESET);
1777 writel (tmp, &dev->regs->devinit);
1779 /* standard fifo and endpoint allocations */
1780 set_fifo_mode (dev, (fifo_mode <= 2) ? fifo_mode : 0);
1783 static void usb_reinit (struct net2280 *dev)
1788 /* use_dma changes are ignored till next device re-init */
1791 /* basic endpoint init */
1792 for (tmp = 0; tmp < 7; tmp++) {
1793 struct net2280_ep *ep = &dev->ep [tmp];
1795 ep->ep.name = ep_name [tmp];
1799 if (tmp > 0 && tmp <= 4) {
1800 ep->fifo_size = 1024;
1802 ep->dma = &dev->dma [tmp - 1];
1805 ep->regs = &dev->epregs [tmp];
1806 ep_reset (dev->regs, ep);
1808 dev->ep [0].ep.maxpacket = 64;
1809 dev->ep [5].ep.maxpacket = 64;
1810 dev->ep [6].ep.maxpacket = 64;
1812 dev->gadget.ep0 = &dev->ep [0].ep;
1813 dev->ep [0].stopped = 0;
1814 INIT_LIST_HEAD (&dev->gadget.ep0->ep_list);
1816 /* we want to prevent lowlevel/insecure access from the USB host,
1817 * but erratum 0119 means this enable bit is ignored
1819 for (tmp = 0; tmp < 5; tmp++)
1820 writel (EP_DONTUSE, &dev->dep [tmp].dep_cfg);
1823 static void ep0_start (struct net2280 *dev)
1825 writel ( (1 << CLEAR_EP_HIDE_STATUS_PHASE)
1826 | (1 << CLEAR_NAK_OUT_PACKETS)
1827 | (1 << CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE)
1828 , &dev->epregs [0].ep_rsp);
1831 * hardware optionally handles a bunch of standard requests
1832 * that the API hides from drivers anyway. have it do so.
1833 * endpoint status/features are handled in software, to
1834 * help pass tests for some dubious behavior.
1836 writel ( (1 << SET_TEST_MODE)
1837 | (1 << SET_ADDRESS)
1838 | (1 << DEVICE_SET_CLEAR_DEVICE_REMOTE_WAKEUP)
1839 | (1 << GET_DEVICE_STATUS)
1840 | (1 << GET_INTERFACE_STATUS)
1841 , &dev->usb->stdrsp);
1842 writel ( (1 << USB_ROOT_PORT_WAKEUP_ENABLE)
1843 | (1 << SELF_POWERED_USB_DEVICE)
1844 | (1 << REMOTE_WAKEUP_SUPPORT)
1845 | (dev->softconnect << USB_DETECT_ENABLE)
1846 | (1 << SELF_POWERED_STATUS)
1847 , &dev->usb->usbctl);
1849 /* enable irqs so we can see ep0 and general operation */
1850 writel ( (1 << SETUP_PACKET_INTERRUPT_ENABLE)
1851 | (1 << ENDPOINT_0_INTERRUPT_ENABLE)
1852 , &dev->regs->pciirqenb0);
1853 writel ( (1 << PCI_INTERRUPT_ENABLE)
1854 | (1 << PCI_MASTER_ABORT_RECEIVED_INTERRUPT_ENABLE)
1855 | (1 << PCI_TARGET_ABORT_RECEIVED_INTERRUPT_ENABLE)
1856 | (1 << PCI_RETRY_ABORT_INTERRUPT_ENABLE)
1857 | (1 << VBUS_INTERRUPT_ENABLE)
1858 | (1 << ROOT_PORT_RESET_INTERRUPT_ENABLE)
1859 | (1 << SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE)
1860 , &dev->regs->pciirqenb1);
1862 /* don't leave any writes posted */
1863 (void) readl (&dev->usb->usbctl);
1866 /* when a driver is successfully registered, it will receive
1867 * control requests including set_configuration(), which enables
1868 * non-control requests. then usb traffic follows until a
1869 * disconnect is reported. then a host may connect again, or
1870 * the driver might get unbound.
1872 static int net2280_start(struct usb_gadget *_gadget,
1873 struct usb_gadget_driver *driver)
1875 struct net2280 *dev;
1879 /* insist on high speed support from the driver, since
1880 * (dev->usb->xcvrdiag & FORCE_FULL_SPEED_MODE)
1881 * "must not be used in normal operation"
1883 if (!driver || driver->max_speed < USB_SPEED_HIGH
1887 dev = container_of (_gadget, struct net2280, gadget);
1889 for (i = 0; i < 7; i++)
1890 dev->ep [i].irqs = 0;
1892 /* hook up the driver ... */
1893 dev->softconnect = 1;
1894 driver->driver.bus = NULL;
1895 dev->driver = driver;
1896 dev->gadget.dev.driver = &driver->driver;
1898 retval = device_create_file (&dev->pdev->dev, &dev_attr_function);
1899 if (retval) goto err_unbind;
1900 retval = device_create_file (&dev->pdev->dev, &dev_attr_queues);
1901 if (retval) goto err_func;
1903 /* ... then enable host detection and ep0; and we're ready
1904 * for set_configuration as well as eventual disconnect.
1906 net2280_led_active (dev, 1);
1909 DEBUG (dev, "%s ready, usbctl %08x stdrsp %08x\n",
1910 driver->driver.name,
1911 readl (&dev->usb->usbctl),
1912 readl (&dev->usb->stdrsp));
1914 /* pci writes may still be posted */
1918 device_remove_file (&dev->pdev->dev, &dev_attr_function);
1920 driver->unbind (&dev->gadget);
1921 dev->gadget.dev.driver = NULL;
1927 stop_activity (struct net2280 *dev, struct usb_gadget_driver *driver)
1931 /* don't disconnect if it's not connected */
1932 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1935 /* stop hardware; prevent new request submissions;
1936 * and kill any outstanding requests.
1939 for (i = 0; i < 7; i++)
1940 nuke (&dev->ep [i]);
1945 static int net2280_stop(struct usb_gadget *_gadget,
1946 struct usb_gadget_driver *driver)
1948 struct net2280 *dev;
1949 unsigned long flags;
1951 dev = container_of (_gadget, struct net2280, gadget);
1953 spin_lock_irqsave (&dev->lock, flags);
1954 stop_activity (dev, driver);
1955 spin_unlock_irqrestore (&dev->lock, flags);
1957 dev->gadget.dev.driver = NULL;
1960 net2280_led_active (dev, 0);
1961 device_remove_file (&dev->pdev->dev, &dev_attr_function);
1962 device_remove_file (&dev->pdev->dev, &dev_attr_queues);
1964 DEBUG (dev, "unregistered driver '%s'\n", driver->driver.name);
1968 /*-------------------------------------------------------------------------*/
1970 /* handle ep0, ep-e, ep-f with 64 byte packets: packet per irq.
1971 * also works for dma-capable endpoints, in pio mode or just
1972 * to manually advance the queue after short OUT transfers.
1974 static void handle_ep_small (struct net2280_ep *ep)
1976 struct net2280_request *req;
1978 /* 0 error, 1 mid-data, 2 done */
1981 if (!list_empty (&ep->queue))
1982 req = list_entry (ep->queue.next,
1983 struct net2280_request, queue);
1987 /* ack all, and handle what we care about */
1988 t = readl (&ep->regs->ep_stat);
1991 VDEBUG (ep->dev, "%s ack ep_stat %08x, req %p\n",
1992 ep->ep.name, t, req ? &req->req : 0);
1994 if (!ep->is_in || ep->dev->pdev->device == 0x2280)
1995 writel (t & ~(1 << NAK_OUT_PACKETS), &ep->regs->ep_stat);
1997 /* Added for 2282 */
1998 writel (t, &ep->regs->ep_stat);
2000 /* for ep0, monitor token irqs to catch data stage length errors
2001 * and to synchronize on status.
2003 * also, to defer reporting of protocol stalls ... here's where
2004 * data or status first appears, handling stalls here should never
2005 * cause trouble on the host side..
2007 * control requests could be slightly faster without token synch for
2008 * status, but status can jam up that way.
2010 if (unlikely (ep->num == 0)) {
2012 /* status; stop NAKing */
2013 if (t & (1 << DATA_OUT_PING_TOKEN_INTERRUPT)) {
2014 if (ep->dev->protocol_stall) {
2021 /* reply to extra IN data tokens with a zlp */
2022 } else if (t & (1 << DATA_IN_TOKEN_INTERRUPT)) {
2023 if (ep->dev->protocol_stall) {
2027 } else if (ep->responded &&
2028 !req && !ep->stopped)
2029 write_fifo (ep, NULL);
2032 /* status; stop NAKing */
2033 if (t & (1 << DATA_IN_TOKEN_INTERRUPT)) {
2034 if (ep->dev->protocol_stall) {
2039 /* an extra OUT token is an error */
2040 } else if (((t & (1 << DATA_OUT_PING_TOKEN_INTERRUPT))
2042 && req->req.actual == req->req.length)
2043 || (ep->responded && !req)) {
2044 ep->dev->protocol_stall = 1;
2048 done (ep, req, -EOVERFLOW);
2054 if (unlikely (!req))
2057 /* manual DMA queue advance after short OUT */
2058 if (likely (ep->dma != 0)) {
2059 if (t & (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)) {
2061 int stopped = ep->stopped;
2063 /* TRANSFERRED works around OUT_DONE erratum 0112.
2064 * we expect (N <= maxpacket) bytes; host wrote M.
2065 * iff (M < N) we won't ever see a DMA interrupt.
2068 for (count = 0; ; t = readl (&ep->regs->ep_stat)) {
2070 /* any preceding dma transfers must finish.
2071 * dma handles (M >= N), may empty the queue
2073 scan_dma_completions (ep);
2074 if (unlikely (list_empty (&ep->queue)
2075 || ep->out_overflow)) {
2079 req = list_entry (ep->queue.next,
2080 struct net2280_request, queue);
2082 /* here either (M < N), a "real" short rx;
2083 * or (M == N) and the queue didn't empty
2085 if (likely (t & (1 << FIFO_EMPTY))) {
2086 count = readl (&ep->dma->dmacount);
2087 count &= DMA_BYTE_COUNT_MASK;
2088 if (readl (&ep->dma->dmadesc)
2096 /* stop DMA, leave ep NAKing */
2097 writel ((1 << DMA_ABORT), &ep->dma->dmastat);
2098 spin_stop_dma (ep->dma);
2101 req->td->dmacount = 0;
2102 t = readl (&ep->regs->ep_avail);
2103 dma_done (ep, req, count,
2104 (ep->out_overflow || t)
2108 /* also flush to prevent erratum 0106 trouble */
2109 if (unlikely (ep->out_overflow
2110 || (ep->dev->chiprev == 0x0100
2111 && ep->dev->gadget.speed
2112 == USB_SPEED_FULL))) {
2114 ep->out_overflow = 0;
2117 /* (re)start dma if needed, stop NAKing */
2118 ep->stopped = stopped;
2119 if (!list_empty (&ep->queue))
2122 DEBUG (ep->dev, "%s dma ep_stat %08x ??\n",
2126 /* data packet(s) received (in the fifo, OUT) */
2127 } else if (t & (1 << DATA_PACKET_RECEIVED_INTERRUPT)) {
2128 if (read_fifo (ep, req) && ep->num != 0)
2131 /* data packet(s) transmitted (IN) */
2132 } else if (t & (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)) {
2135 len = req->req.length - req->req.actual;
2136 if (len > ep->ep.maxpacket)
2137 len = ep->ep.maxpacket;
2138 req->req.actual += len;
2140 /* if we wrote it all, we're usually done */
2141 if (req->req.actual == req->req.length) {
2143 /* send zlps until the status stage */
2144 } else if (!req->req.zero || len != ep->ep.maxpacket)
2148 /* there was nothing to do ... */
2149 } else if (mode == 1)
2154 /* stream endpoints often resubmit/unlink in completion */
2157 /* maybe advance queue to next request */
2159 /* NOTE: net2280 could let gadget driver start the
2160 * status stage later. since not all controllers let
2161 * them control that, the api doesn't (yet) allow it.
2167 if (!list_empty (&ep->queue) && !ep->stopped)
2168 req = list_entry (ep->queue.next,
2169 struct net2280_request, queue);
2172 if (req && !ep->is_in)
2173 stop_out_naking (ep);
2177 /* is there a buffer for the next packet?
2178 * for best streaming performance, make sure there is one.
2180 if (req && !ep->stopped) {
2182 /* load IN fifo with next packet (may be zlp) */
2183 if (t & (1 << DATA_PACKET_TRANSMITTED_INTERRUPT))
2184 write_fifo (ep, &req->req);
2188 static struct net2280_ep *
2189 get_ep_by_addr (struct net2280 *dev, u16 wIndex)
2191 struct net2280_ep *ep;
2193 if ((wIndex & USB_ENDPOINT_NUMBER_MASK) == 0)
2194 return &dev->ep [0];
2195 list_for_each_entry (ep, &dev->gadget.ep_list, ep.ep_list) {
2196 u8 bEndpointAddress;
2200 bEndpointAddress = ep->desc->bEndpointAddress;
2201 if ((wIndex ^ bEndpointAddress) & USB_DIR_IN)
2203 if ((wIndex & 0x0f) == (bEndpointAddress & 0x0f))
2209 static void handle_stat0_irqs (struct net2280 *dev, u32 stat)
2211 struct net2280_ep *ep;
2214 /* most of these don't need individual acks */
2215 stat &= ~(1 << INTA_ASSERTED);
2218 // DEBUG (dev, "irqstat0 %04x\n", stat);
2220 /* starting a control request? */
2221 if (unlikely (stat & (1 << SETUP_PACKET_INTERRUPT))) {
2224 struct usb_ctrlrequest r;
2227 struct net2280_request *req;
2229 if (dev->gadget.speed == USB_SPEED_UNKNOWN) {
2230 if (readl (&dev->usb->usbstat) & (1 << HIGH_SPEED))
2231 dev->gadget.speed = USB_SPEED_HIGH;
2233 dev->gadget.speed = USB_SPEED_FULL;
2234 net2280_led_speed (dev, dev->gadget.speed);
2235 DEBUG(dev, "%s\n", usb_speed_string(dev->gadget.speed));
2241 /* make sure any leftover request state is cleared */
2242 stat &= ~(1 << ENDPOINT_0_INTERRUPT);
2243 while (!list_empty (&ep->queue)) {
2244 req = list_entry (ep->queue.next,
2245 struct net2280_request, queue);
2246 done (ep, req, (req->req.actual == req->req.length)
2250 dev->protocol_stall = 0;
2252 if (ep->dev->pdev->device == 0x2280)
2253 tmp = (1 << FIFO_OVERFLOW)
2254 | (1 << FIFO_UNDERFLOW);
2258 writel (tmp | (1 << TIMEOUT)
2259 | (1 << USB_STALL_SENT)
2260 | (1 << USB_IN_NAK_SENT)
2261 | (1 << USB_IN_ACK_RCVD)
2262 | (1 << USB_OUT_PING_NAK_SENT)
2263 | (1 << USB_OUT_ACK_SENT)
2264 | (1 << SHORT_PACKET_OUT_DONE_INTERRUPT)
2265 | (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)
2266 | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
2267 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
2268 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
2269 | (1 << DATA_IN_TOKEN_INTERRUPT)
2270 , &ep->regs->ep_stat);
2271 u.raw [0] = readl (&dev->usb->setup0123);
2272 u.raw [1] = readl (&dev->usb->setup4567);
2274 cpu_to_le32s (&u.raw [0]);
2275 cpu_to_le32s (&u.raw [1]);
2279 #define w_value le16_to_cpu(u.r.wValue)
2280 #define w_index le16_to_cpu(u.r.wIndex)
2281 #define w_length le16_to_cpu(u.r.wLength)
2284 writel (1 << SETUP_PACKET_INTERRUPT, &dev->regs->irqstat0);
2285 stat ^= (1 << SETUP_PACKET_INTERRUPT);
2287 /* watch control traffic at the token level, and force
2288 * synchronization before letting the status stage happen.
2289 * FIXME ignore tokens we'll NAK, until driver responds.
2290 * that'll mean a lot less irqs for some drivers.
2292 ep->is_in = (u.r.bRequestType & USB_DIR_IN) != 0;
2294 scratch = (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
2295 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
2296 | (1 << DATA_IN_TOKEN_INTERRUPT);
2297 stop_out_naking (ep);
2299 scratch = (1 << DATA_PACKET_RECEIVED_INTERRUPT)
2300 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
2301 | (1 << DATA_IN_TOKEN_INTERRUPT);
2302 writel (scratch, &dev->epregs [0].ep_irqenb);
2304 /* we made the hardware handle most lowlevel requests;
2305 * everything else goes uplevel to the gadget code.
2308 switch (u.r.bRequest) {
2309 case USB_REQ_GET_STATUS: {
2310 struct net2280_ep *e;
2313 /* hw handles device and interface status */
2314 if (u.r.bRequestType != (USB_DIR_IN|USB_RECIP_ENDPOINT))
2316 if ((e = get_ep_by_addr (dev, w_index)) == 0
2320 if (readl (&e->regs->ep_rsp)
2321 & (1 << SET_ENDPOINT_HALT))
2322 status = cpu_to_le32 (1);
2324 status = cpu_to_le32 (0);
2326 /* don't bother with a request object! */
2327 writel (0, &dev->epregs [0].ep_irqenb);
2328 set_fifo_bytecount (ep, w_length);
2329 writel ((__force u32)status, &dev->epregs [0].ep_data);
2331 VDEBUG (dev, "%s stat %02x\n", ep->ep.name, status);
2332 goto next_endpoints;
2335 case USB_REQ_CLEAR_FEATURE: {
2336 struct net2280_ep *e;
2338 /* hw handles device features */
2339 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
2341 if (w_value != USB_ENDPOINT_HALT
2344 if ((e = get_ep_by_addr (dev, w_index)) == 0)
2347 VDEBUG(dev, "%s wedged, halt not cleared\n",
2350 VDEBUG(dev, "%s clear halt\n", ep->ep.name);
2354 goto next_endpoints;
2357 case USB_REQ_SET_FEATURE: {
2358 struct net2280_ep *e;
2360 /* hw handles device features */
2361 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
2363 if (w_value != USB_ENDPOINT_HALT
2366 if ((e = get_ep_by_addr (dev, w_index)) == 0)
2368 if (e->ep.name == ep0name)
2372 VDEBUG (dev, "%s set halt\n", ep->ep.name);
2373 goto next_endpoints;
2378 VDEBUG (dev, "setup %02x.%02x v%04x i%04x l%04x "
2380 u.r.bRequestType, u.r.bRequest,
2381 w_value, w_index, w_length,
2382 readl (&ep->regs->ep_cfg));
2384 spin_unlock (&dev->lock);
2385 tmp = dev->driver->setup (&dev->gadget, &u.r);
2386 spin_lock (&dev->lock);
2389 /* stall ep0 on error */
2392 VDEBUG (dev, "req %02x.%02x protocol STALL; stat %d\n",
2393 u.r.bRequestType, u.r.bRequest, tmp);
2394 dev->protocol_stall = 1;
2397 /* some in/out token irq should follow; maybe stall then.
2398 * driver must queue a request (even zlp) or halt ep0
2399 * before the host times out.
2408 /* endpoint data irq ? */
2409 scratch = stat & 0x7f;
2411 for (num = 0; scratch; num++) {
2414 /* do this endpoint's FIFO and queue need tending? */
2416 if ((scratch & t) == 0)
2420 ep = &dev->ep [num];
2421 handle_ep_small (ep);
2425 DEBUG (dev, "unhandled irqstat0 %08x\n", stat);
2428 #define DMA_INTERRUPTS ( \
2429 (1 << DMA_D_INTERRUPT) \
2430 | (1 << DMA_C_INTERRUPT) \
2431 | (1 << DMA_B_INTERRUPT) \
2432 | (1 << DMA_A_INTERRUPT))
2433 #define PCI_ERROR_INTERRUPTS ( \
2434 (1 << PCI_MASTER_ABORT_RECEIVED_INTERRUPT) \
2435 | (1 << PCI_TARGET_ABORT_RECEIVED_INTERRUPT) \
2436 | (1 << PCI_RETRY_ABORT_INTERRUPT))
2438 static void handle_stat1_irqs (struct net2280 *dev, u32 stat)
2440 struct net2280_ep *ep;
2441 u32 tmp, num, mask, scratch;
2443 /* after disconnect there's nothing else to do! */
2444 tmp = (1 << VBUS_INTERRUPT) | (1 << ROOT_PORT_RESET_INTERRUPT);
2445 mask = (1 << HIGH_SPEED) | (1 << FULL_SPEED);
2447 /* VBUS disconnect is indicated by VBUS_PIN and VBUS_INTERRUPT set.
2448 * Root Port Reset is indicated by ROOT_PORT_RESET_INTERRUPT set and
2449 * both HIGH_SPEED and FULL_SPEED clear (as ROOT_PORT_RESET_INTERRUPT
2450 * only indicates a change in the reset state).
2453 writel (tmp, &dev->regs->irqstat1);
2454 if ((((stat & (1 << ROOT_PORT_RESET_INTERRUPT))
2455 && ((readl (&dev->usb->usbstat) & mask)
2457 || ((readl (&dev->usb->usbctl)
2458 & (1 << VBUS_PIN)) == 0)
2459 ) && ( dev->gadget.speed != USB_SPEED_UNKNOWN)) {
2460 DEBUG (dev, "disconnect %s\n",
2461 dev->driver->driver.name);
2462 stop_activity (dev, dev->driver);
2468 /* vBUS can bounce ... one of many reasons to ignore the
2469 * notion of hotplug events on bus connect/disconnect!
2475 /* NOTE: chip stays in PCI D0 state for now, but it could
2476 * enter D1 to save more power
2478 tmp = (1 << SUSPEND_REQUEST_CHANGE_INTERRUPT);
2480 writel (tmp, &dev->regs->irqstat1);
2481 if (stat & (1 << SUSPEND_REQUEST_INTERRUPT)) {
2482 if (dev->driver->suspend)
2483 dev->driver->suspend (&dev->gadget);
2484 if (!enable_suspend)
2485 stat &= ~(1 << SUSPEND_REQUEST_INTERRUPT);
2487 if (dev->driver->resume)
2488 dev->driver->resume (&dev->gadget);
2489 /* at high speed, note erratum 0133 */
2494 /* clear any other status/irqs */
2496 writel (stat, &dev->regs->irqstat1);
2498 /* some status we can just ignore */
2499 if (dev->pdev->device == 0x2280)
2500 stat &= ~((1 << CONTROL_STATUS_INTERRUPT)
2501 | (1 << SUSPEND_REQUEST_INTERRUPT)
2502 | (1 << RESUME_INTERRUPT)
2503 | (1 << SOF_INTERRUPT));
2505 stat &= ~((1 << CONTROL_STATUS_INTERRUPT)
2506 | (1 << RESUME_INTERRUPT)
2507 | (1 << SOF_DOWN_INTERRUPT)
2508 | (1 << SOF_INTERRUPT));
2512 // DEBUG (dev, "irqstat1 %08x\n", stat);
2514 /* DMA status, for ep-{a,b,c,d} */
2515 scratch = stat & DMA_INTERRUPTS;
2516 stat &= ~DMA_INTERRUPTS;
2518 for (num = 0; scratch; num++) {
2519 struct net2280_dma_regs __iomem *dma;
2522 if ((tmp & scratch) == 0)
2526 ep = &dev->ep [num + 1];
2532 /* clear ep's dma status */
2533 tmp = readl (&dma->dmastat);
2534 writel (tmp, &dma->dmastat);
2536 /* chaining should stop on abort, short OUT from fifo,
2537 * or (stat0 codepath) short OUT transfer.
2539 if (!use_dma_chaining) {
2540 if ((tmp & (1 << DMA_TRANSACTION_DONE_INTERRUPT))
2542 DEBUG (ep->dev, "%s no xact done? %08x\n",
2549 /* OUT transfers terminate when the data from the
2550 * host is in our memory. Process whatever's done.
2551 * On this path, we know transfer's last packet wasn't
2552 * less than req->length. NAK_OUT_PACKETS may be set,
2553 * or the FIFO may already be holding new packets.
2555 * IN transfers can linger in the FIFO for a very
2556 * long time ... we ignore that for now, accounting
2557 * precisely (like PIO does) needs per-packet irqs
2559 scan_dma_completions (ep);
2561 /* disable dma on inactive queues; else maybe restart */
2562 if (list_empty (&ep->queue)) {
2563 if (use_dma_chaining)
2566 tmp = readl (&dma->dmactl);
2567 if (!use_dma_chaining
2568 || (tmp & (1 << DMA_ENABLE)) == 0)
2570 else if (ep->is_in && use_dma_chaining) {
2571 struct net2280_request *req;
2574 /* the descriptor at the head of the chain
2575 * may still have VALID_BIT clear; that's
2576 * used to trigger changing DMA_FIFO_VALIDATE
2577 * (affects automagic zlp writes).
2579 req = list_entry (ep->queue.next,
2580 struct net2280_request, queue);
2581 dmacount = req->td->dmacount;
2582 dmacount &= cpu_to_le32 (
2584 | DMA_BYTE_COUNT_MASK);
2585 if (dmacount && (dmacount & valid_bit) == 0)
2592 /* NOTE: there are other PCI errors we might usefully notice.
2593 * if they appear very often, here's where to try recovering.
2595 if (stat & PCI_ERROR_INTERRUPTS) {
2596 ERROR (dev, "pci dma error; stat %08x\n", stat);
2597 stat &= ~PCI_ERROR_INTERRUPTS;
2598 /* these are fatal errors, but "maybe" they won't
2601 stop_activity (dev, dev->driver);
2607 DEBUG (dev, "unhandled irqstat1 %08x\n", stat);
2610 static irqreturn_t net2280_irq (int irq, void *_dev)
2612 struct net2280 *dev = _dev;
2614 /* shared interrupt, not ours */
2615 if (!(readl(&dev->regs->irqstat0) & (1 << INTA_ASSERTED)))
2618 spin_lock (&dev->lock);
2620 /* handle disconnect, dma, and more */
2621 handle_stat1_irqs (dev, readl (&dev->regs->irqstat1));
2623 /* control requests and PIO */
2624 handle_stat0_irqs (dev, readl (&dev->regs->irqstat0));
2626 spin_unlock (&dev->lock);
2631 /*-------------------------------------------------------------------------*/
2633 static void gadget_release (struct device *_dev)
2635 struct net2280 *dev = dev_get_drvdata (_dev);
2640 /* tear down the binding between this driver and the pci device */
2642 static void net2280_remove (struct pci_dev *pdev)
2644 struct net2280 *dev = pci_get_drvdata (pdev);
2646 usb_del_gadget_udc(&dev->gadget);
2648 BUG_ON(dev->driver);
2650 /* then clean up the resources we allocated during probe() */
2651 net2280_led_shutdown (dev);
2652 if (dev->requests) {
2654 for (i = 1; i < 5; i++) {
2655 if (!dev->ep [i].dummy)
2657 pci_pool_free (dev->requests, dev->ep [i].dummy,
2658 dev->ep [i].td_dma);
2660 pci_pool_destroy (dev->requests);
2663 free_irq (pdev->irq, dev);
2665 iounmap (dev->regs);
2667 release_mem_region (pci_resource_start (pdev, 0),
2668 pci_resource_len (pdev, 0));
2670 pci_disable_device (pdev);
2671 device_unregister (&dev->gadget.dev);
2672 device_remove_file (&pdev->dev, &dev_attr_registers);
2673 pci_set_drvdata (pdev, NULL);
2675 INFO (dev, "unbind\n");
2678 /* wrap this driver around the specified device, but
2679 * don't respond over USB until a gadget driver binds to us.
2682 static int net2280_probe (struct pci_dev *pdev, const struct pci_device_id *id)
2684 struct net2280 *dev;
2685 unsigned long resource, len;
2686 void __iomem *base = NULL;
2689 /* alloc, and start init */
2690 dev = kzalloc (sizeof *dev, GFP_KERNEL);
2696 pci_set_drvdata (pdev, dev);
2697 spin_lock_init (&dev->lock);
2699 dev->gadget.ops = &net2280_ops;
2700 dev->gadget.max_speed = USB_SPEED_HIGH;
2702 /* the "gadget" abstracts/virtualizes the controller */
2703 dev_set_name(&dev->gadget.dev, "gadget");
2704 dev->gadget.dev.parent = &pdev->dev;
2705 dev->gadget.dev.dma_mask = pdev->dev.dma_mask;
2706 dev->gadget.dev.release = gadget_release;
2707 dev->gadget.name = driver_name;
2709 /* now all the pci goodies ... */
2710 if (pci_enable_device (pdev) < 0) {
2716 /* BAR 0 holds all the registers
2717 * BAR 1 is 8051 memory; unused here (note erratum 0103)
2718 * BAR 2 is fifo memory; unused here
2720 resource = pci_resource_start (pdev, 0);
2721 len = pci_resource_len (pdev, 0);
2722 if (!request_mem_region (resource, len, driver_name)) {
2723 DEBUG (dev, "controller already in use\n");
2729 /* FIXME provide firmware download interface to put
2730 * 8051 code into the chip, e.g. to turn on PCI PM.
2733 base = ioremap_nocache (resource, len);
2735 DEBUG (dev, "can't map memory\n");
2739 dev->regs = (struct net2280_regs __iomem *) base;
2740 dev->usb = (struct net2280_usb_regs __iomem *) (base + 0x0080);
2741 dev->pci = (struct net2280_pci_regs __iomem *) (base + 0x0100);
2742 dev->dma = (struct net2280_dma_regs __iomem *) (base + 0x0180);
2743 dev->dep = (struct net2280_dep_regs __iomem *) (base + 0x0200);
2744 dev->epregs = (struct net2280_ep_regs __iomem *) (base + 0x0300);
2746 /* put into initial config, link up all endpoints */
2747 writel (0, &dev->usb->usbctl);
2751 /* irq setup after old hardware is cleaned up */
2753 ERROR (dev, "No IRQ. Check PCI setup!\n");
2758 if (request_irq (pdev->irq, net2280_irq, IRQF_SHARED, driver_name, dev)
2760 ERROR (dev, "request interrupt %d failed\n", pdev->irq);
2767 /* NOTE: we know only the 32 LSBs of dma addresses may be nonzero */
2768 dev->requests = pci_pool_create ("requests", pdev,
2769 sizeof (struct net2280_dma),
2770 0 /* no alignment requirements */,
2771 0 /* or page-crossing issues */);
2772 if (!dev->requests) {
2773 DEBUG (dev, "can't get request pool\n");
2777 for (i = 1; i < 5; i++) {
2778 struct net2280_dma *td;
2780 td = pci_pool_alloc (dev->requests, GFP_KERNEL,
2781 &dev->ep [i].td_dma);
2783 DEBUG (dev, "can't get dummy %d\n", i);
2787 td->dmacount = 0; /* not VALID */
2788 td->dmaaddr = cpu_to_le32 (DMA_ADDR_INVALID);
2789 td->dmadesc = td->dmaaddr;
2790 dev->ep [i].dummy = td;
2793 /* enable lower-overhead pci memory bursts during DMA */
2794 writel ( (1 << DMA_MEMORY_WRITE_AND_INVALIDATE_ENABLE)
2795 // 256 write retries may not be enough...
2796 // | (1 << PCI_RETRY_ABORT_ENABLE)
2797 | (1 << DMA_READ_MULTIPLE_ENABLE)
2798 | (1 << DMA_READ_LINE_ENABLE)
2799 , &dev->pci->pcimstctl);
2800 /* erratum 0115 shouldn't appear: Linux inits PCI_LATENCY_TIMER */
2801 pci_set_master (pdev);
2802 pci_try_set_mwi (pdev);
2804 /* ... also flushes any posted pci writes */
2805 dev->chiprev = get_idx_reg (dev->regs, REG_CHIPREV) & 0xffff;
2808 INFO (dev, "%s\n", driver_desc);
2809 INFO (dev, "irq %d, pci mem %p, chip rev %04x\n",
2810 pdev->irq, base, dev->chiprev);
2811 INFO (dev, "version: " DRIVER_VERSION "; dma %s\n",
2813 ? (use_dma_chaining ? "chaining" : "enabled")
2815 retval = device_register (&dev->gadget.dev);
2816 if (retval) goto done;
2817 retval = device_create_file (&pdev->dev, &dev_attr_registers);
2818 if (retval) goto done;
2820 retval = usb_add_gadget_udc(&pdev->dev, &dev->gadget);
2827 net2280_remove (pdev);
2831 /* make sure the board is quiescent; otherwise it will continue
2832 * generating IRQs across the upcoming reboot.
2835 static void net2280_shutdown (struct pci_dev *pdev)
2837 struct net2280 *dev = pci_get_drvdata (pdev);
2840 writel (0, &dev->regs->pciirqenb0);
2841 writel (0, &dev->regs->pciirqenb1);
2843 /* disable the pullup so the host will think we're gone */
2844 writel (0, &dev->usb->usbctl);
2848 /*-------------------------------------------------------------------------*/
2850 static const struct pci_device_id pci_ids [] = { {
2851 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
2855 .subvendor = PCI_ANY_ID,
2856 .subdevice = PCI_ANY_ID,
2858 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
2862 .subvendor = PCI_ANY_ID,
2863 .subdevice = PCI_ANY_ID,
2865 }, { /* end: all zeroes */ }
2867 MODULE_DEVICE_TABLE (pci, pci_ids);
2869 /* pci driver glue; this is a "new style" PCI driver module */
2870 static struct pci_driver net2280_pci_driver = {
2871 .name = (char *) driver_name,
2872 .id_table = pci_ids,
2874 .probe = net2280_probe,
2875 .remove = net2280_remove,
2876 .shutdown = net2280_shutdown,
2878 /* FIXME add power management support */
2881 MODULE_DESCRIPTION (DRIVER_DESC);
2882 MODULE_AUTHOR ("David Brownell");
2883 MODULE_LICENSE ("GPL");
2885 static int __init init (void)
2888 use_dma_chaining = 0;
2889 return pci_register_driver (&net2280_pci_driver);
2893 static void __exit cleanup (void)
2895 pci_unregister_driver (&net2280_pci_driver);
2897 module_exit (cleanup);