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 * This program is free software; you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License as published by
31 * the Free Software Foundation; either version 2 of the License, or
32 * (at your option) any later version.
34 * This program is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 * GNU General Public License for more details.
39 * You should have received a copy of the GNU General Public License
40 * along with this program; if not, write to the Free Software
41 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
44 #undef DEBUG /* messages on error and most fault paths */
45 #undef VERBOSE /* extra debug messages (success too) */
47 #include <linux/config.h>
48 #include <linux/module.h>
49 #include <linux/pci.h>
50 #include <linux/kernel.h>
51 #include <linux/delay.h>
52 #include <linux/ioport.h>
53 #include <linux/sched.h>
54 #include <linux/slab.h>
55 #include <linux/smp_lock.h>
56 #include <linux/errno.h>
57 #include <linux/init.h>
58 #include <linux/timer.h>
59 #include <linux/list.h>
60 #include <linux/interrupt.h>
61 #include <linux/moduleparam.h>
62 #include <linux/device.h>
63 #include <linux/usb_ch9.h>
64 #include <linux/usb_gadget.h>
66 #include <asm/byteorder.h>
69 #include <asm/system.h>
70 #include <asm/unaligned.h>
73 #define DRIVER_DESC "PLX NET2280 USB Peripheral Controller"
74 #define DRIVER_VERSION "2005 Feb 03"
76 #define DMA_ADDR_INVALID (~(dma_addr_t)0)
77 #define EP_DONTUSE 13 /* nonzero */
79 #define USE_RDK_LEDS /* GPIO pins control three LEDs */
82 static const char driver_name [] = "net2280";
83 static const char driver_desc [] = DRIVER_DESC;
85 static const char ep0name [] = "ep0";
86 static const char *ep_name [] = {
88 "ep-a", "ep-b", "ep-c", "ep-d",
92 /* use_dma -- general goodness, fewer interrupts, less cpu load (vs PIO)
93 * use_dma_chaining -- dma descriptor queueing gives even more irq reduction
95 * The net2280 DMA engines are not tightly integrated with their FIFOs;
96 * not all cases are (yet) handled well in this driver or the silicon.
97 * Some gadget drivers work better with the dma support here than others.
98 * These two parameters let you use PIO or more aggressive DMA.
100 static int use_dma = 1;
101 static int use_dma_chaining = 0;
103 /* "modprobe net2280 use_dma=n" etc */
104 module_param (use_dma, bool, S_IRUGO);
105 module_param (use_dma_chaining, bool, S_IRUGO);
108 /* mode 0 == ep-{a,b,c,d} 1K fifo each
109 * mode 1 == ep-{a,b} 2K fifo each, ep-{c,d} unavailable
110 * mode 2 == ep-a 2K fifo, ep-{b,c} 1K each, ep-d unavailable
112 static ushort fifo_mode = 0;
114 /* "modprobe net2280 fifo_mode=1" etc */
115 module_param (fifo_mode, ushort, 0644);
117 /* enable_suspend -- When enabled, the driver will respond to
118 * USB suspend requests by powering down the NET2280. Otherwise,
119 * USB suspend requests will be ignored. This is acceptible for
120 * self-powered devices, and helps avoid some quirks.
122 static int enable_suspend = 0;
124 /* "modprobe net2280 enable_suspend=1" etc */
125 module_param (enable_suspend, bool, S_IRUGO);
128 #define DIR_STRING(bAddress) (((bAddress) & USB_DIR_IN) ? "in" : "out")
130 #if defined(CONFIG_USB_GADGET_DEBUG_FILES) || defined (DEBUG)
131 static char *type_string (u8 bmAttributes)
133 switch ((bmAttributes) & USB_ENDPOINT_XFERTYPE_MASK) {
134 case USB_ENDPOINT_XFER_BULK: return "bulk";
135 case USB_ENDPOINT_XFER_ISOC: return "iso";
136 case USB_ENDPOINT_XFER_INT: return "intr";
144 #define valid_bit __constant_cpu_to_le32 (1 << VALID_BIT)
145 #define dma_done_ie __constant_cpu_to_le32 (1 << DMA_DONE_INTERRUPT_ENABLE)
147 /*-------------------------------------------------------------------------*/
150 net2280_enable (struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
153 struct net2280_ep *ep;
157 ep = container_of (_ep, struct net2280_ep, ep);
158 if (!_ep || !desc || ep->desc || _ep->name == ep0name
159 || desc->bDescriptorType != USB_DT_ENDPOINT)
162 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
165 /* erratum 0119 workaround ties up an endpoint number */
166 if ((desc->bEndpointAddress & 0x0f) == EP_DONTUSE)
169 /* sanity check ep-e/ep-f since their fifos are small */
170 max = le16_to_cpu (desc->wMaxPacketSize) & 0x1fff;
171 if (ep->num > 4 && max > 64)
174 spin_lock_irqsave (&dev->lock, flags);
175 _ep->maxpacket = max & 0x7ff;
178 /* ep_reset() has already been called */
180 ep->out_overflow = 0;
182 /* set speed-dependent max packet; may kick in high bandwidth */
183 set_idx_reg (dev->regs, REG_EP_MAXPKT (dev, ep->num), max);
185 /* FIFO lines can't go to different packets. PIO is ok, so
186 * use it instead of troublesome (non-bulk) multi-packet DMA.
188 if (ep->dma && (max % 4) != 0 && use_dma_chaining) {
189 DEBUG (ep->dev, "%s, no dma for maxpacket %d\n",
190 ep->ep.name, ep->ep.maxpacket);
194 /* set type, direction, address; reset fifo counters */
195 writel ((1 << FIFO_FLUSH), &ep->regs->ep_stat);
196 tmp = (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK);
197 if (tmp == USB_ENDPOINT_XFER_INT) {
198 /* erratum 0105 workaround prevents hs NYET */
199 if (dev->chiprev == 0100
200 && dev->gadget.speed == USB_SPEED_HIGH
201 && !(desc->bEndpointAddress & USB_DIR_IN))
202 writel ((1 << CLEAR_NAK_OUT_PACKETS_MODE),
204 } else if (tmp == USB_ENDPOINT_XFER_BULK) {
205 /* catch some particularly blatant driver bugs */
206 if ((dev->gadget.speed == USB_SPEED_HIGH
208 || (dev->gadget.speed == USB_SPEED_FULL
210 spin_unlock_irqrestore (&dev->lock, flags);
214 ep->is_iso = (tmp == USB_ENDPOINT_XFER_ISOC) ? 1 : 0;
215 tmp <<= ENDPOINT_TYPE;
216 tmp |= desc->bEndpointAddress;
217 tmp |= (4 << ENDPOINT_BYTE_COUNT); /* default full fifo lines */
218 tmp |= 1 << ENDPOINT_ENABLE;
221 /* for OUT transfers, block the rx fifo until a read is posted */
222 ep->is_in = (tmp & USB_DIR_IN) != 0;
224 writel ((1 << SET_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
226 writel (tmp, &ep->regs->ep_cfg);
229 if (!ep->dma) { /* pio, per-packet */
230 tmp = (1 << ep->num) | readl (&dev->regs->pciirqenb0);
231 writel (tmp, &dev->regs->pciirqenb0);
233 tmp = (1 << DATA_PACKET_RECEIVED_INTERRUPT_ENABLE)
234 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT_ENABLE)
235 | readl (&ep->regs->ep_irqenb);
236 writel (tmp, &ep->regs->ep_irqenb);
237 } else { /* dma, per-request */
238 tmp = (1 << (8 + ep->num)); /* completion */
239 tmp |= readl (&dev->regs->pciirqenb1);
240 writel (tmp, &dev->regs->pciirqenb1);
242 /* for short OUT transfers, dma completions can't
243 * advance the queue; do it pio-style, by hand.
244 * NOTE erratum 0112 workaround #2
246 if ((desc->bEndpointAddress & USB_DIR_IN) == 0) {
247 tmp = (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT_ENABLE);
248 writel (tmp, &ep->regs->ep_irqenb);
250 tmp = (1 << ep->num) | readl (&dev->regs->pciirqenb0);
251 writel (tmp, &dev->regs->pciirqenb0);
255 tmp = desc->bEndpointAddress;
256 DEBUG (dev, "enabled %s (ep%d%s-%s) %s max %04x\n",
257 _ep->name, tmp & 0x0f, DIR_STRING (tmp),
258 type_string (desc->bmAttributes),
259 ep->dma ? "dma" : "pio", max);
261 /* pci writes may still be posted */
262 spin_unlock_irqrestore (&dev->lock, flags);
266 static int handshake (u32 __iomem *ptr, u32 mask, u32 done, int usec)
271 result = readl (ptr);
272 if (result == ~(u32)0) /* "device unplugged" */
283 static struct usb_ep_ops net2280_ep_ops;
285 static void ep_reset (struct net2280_regs __iomem *regs, struct net2280_ep *ep)
290 INIT_LIST_HEAD (&ep->queue);
292 ep->ep.maxpacket = ~0;
293 ep->ep.ops = &net2280_ep_ops;
295 /* disable the dma, irqs, endpoint... */
297 writel (0, &ep->dma->dmactl);
298 writel ( (1 << DMA_SCATTER_GATHER_DONE_INTERRUPT)
299 | (1 << DMA_TRANSACTION_DONE_INTERRUPT)
301 , &ep->dma->dmastat);
303 tmp = readl (®s->pciirqenb0);
304 tmp &= ~(1 << ep->num);
305 writel (tmp, ®s->pciirqenb0);
307 tmp = readl (®s->pciirqenb1);
308 tmp &= ~(1 << (8 + ep->num)); /* completion */
309 writel (tmp, ®s->pciirqenb1);
311 writel (0, &ep->regs->ep_irqenb);
313 /* init to our chosen defaults, notably so that we NAK OUT
314 * packets until the driver queues a read (+note erratum 0112)
316 tmp = (1 << SET_NAK_OUT_PACKETS_MODE)
317 | (1 << SET_NAK_OUT_PACKETS)
318 | (1 << CLEAR_EP_HIDE_STATUS_PHASE)
319 | (1 << CLEAR_INTERRUPT_MODE);
322 tmp |= (1 << CLEAR_ENDPOINT_TOGGLE)
323 | (1 << CLEAR_ENDPOINT_HALT);
325 writel (tmp, &ep->regs->ep_rsp);
327 /* scrub most status bits, and flush any fifo state */
328 writel ( (1 << TIMEOUT)
329 | (1 << USB_STALL_SENT)
330 | (1 << USB_IN_NAK_SENT)
331 | (1 << USB_IN_ACK_RCVD)
332 | (1 << USB_OUT_PING_NAK_SENT)
333 | (1 << USB_OUT_ACK_SENT)
334 | (1 << FIFO_OVERFLOW)
335 | (1 << FIFO_UNDERFLOW)
337 | (1 << SHORT_PACKET_OUT_DONE_INTERRUPT)
338 | (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)
339 | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
340 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
341 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
342 | (1 << DATA_IN_TOKEN_INTERRUPT)
343 , &ep->regs->ep_stat);
345 /* fifo size is handled separately */
348 static void nuke (struct net2280_ep *);
350 static int net2280_disable (struct usb_ep *_ep)
352 struct net2280_ep *ep;
355 ep = container_of (_ep, struct net2280_ep, ep);
356 if (!_ep || !ep->desc || _ep->name == ep0name)
359 spin_lock_irqsave (&ep->dev->lock, flags);
361 ep_reset (ep->dev->regs, ep);
363 VDEBUG (ep->dev, "disabled %s %s\n",
364 ep->dma ? "dma" : "pio", _ep->name);
366 /* synch memory views with the device */
367 (void) readl (&ep->regs->ep_cfg);
369 if (use_dma && !ep->dma && ep->num >= 1 && ep->num <= 4)
370 ep->dma = &ep->dev->dma [ep->num - 1];
372 spin_unlock_irqrestore (&ep->dev->lock, flags);
376 /*-------------------------------------------------------------------------*/
378 static struct usb_request *
379 net2280_alloc_request (struct usb_ep *_ep, int gfp_flags)
381 struct net2280_ep *ep;
382 struct net2280_request *req;
386 ep = container_of (_ep, struct net2280_ep, ep);
388 req = kmalloc (sizeof *req, gfp_flags);
392 memset (req, 0, sizeof *req);
393 req->req.dma = DMA_ADDR_INVALID;
394 INIT_LIST_HEAD (&req->queue);
396 /* this dma descriptor may be swapped with the previous dummy */
398 struct net2280_dma *td;
400 td = pci_pool_alloc (ep->dev->requests, gfp_flags,
406 td->dmacount = 0; /* not VALID */
407 td->dmaaddr = __constant_cpu_to_le32 (DMA_ADDR_INVALID);
408 td->dmadesc = td->dmaaddr;
415 net2280_free_request (struct usb_ep *_ep, struct usb_request *_req)
417 struct net2280_ep *ep;
418 struct net2280_request *req;
420 ep = container_of (_ep, struct net2280_ep, ep);
424 req = container_of (_req, struct net2280_request, req);
425 WARN_ON (!list_empty (&req->queue));
427 pci_pool_free (ep->dev->requests, req->td, req->td_dma);
431 /*-------------------------------------------------------------------------*/
435 /* many common platforms have dma-coherent caches, which means that it's
436 * safe to use kmalloc() memory for all i/o buffers without using any
437 * cache flushing calls. (unless you're trying to share cache lines
438 * between dma and non-dma activities, which is a slow idea in any case.)
440 * other platforms need more care, with 2.5 having a moderately general
441 * solution (which falls down for allocations smaller than one page)
442 * that improves significantly on the 2.4 PCI allocators by removing
443 * the restriction that memory never be freed in_interrupt().
445 #if defined(CONFIG_X86)
448 #elif defined(CONFIG_PPC) && !defined(CONFIG_NOT_COHERENT_CACHE)
451 #elif defined(CONFIG_MIPS) && !defined(CONFIG_DMA_NONCOHERENT)
454 /* FIXME there are other cases, including an x86-64 one ... */
457 /* allocating buffers this way eliminates dma mapping overhead, which
458 * on some platforms will mean eliminating a per-io buffer copy. with
459 * some kinds of system caches, further tweaks may still be needed.
462 net2280_alloc_buffer (
470 struct net2280_ep *ep;
472 ep = container_of (_ep, struct net2280_ep, ep);
475 *dma = DMA_ADDR_INVALID;
477 #if defined(USE_KMALLOC)
478 retval = kmalloc(bytes, gfp_flags);
480 *dma = virt_to_phys(retval);
483 /* the main problem with this call is that it wastes memory
484 * on typical 1/N page allocations: it allocates 1-N pages.
486 #warning Using dma_alloc_coherent even with buffers smaller than a page.
487 retval = dma_alloc_coherent(&ep->dev->pdev->dev,
488 bytes, dma, gfp_flags);
490 retval = kmalloc(bytes, gfp_flags);
496 net2280_free_buffer (
502 /* free memory into the right allocator */
504 if (dma != DMA_ADDR_INVALID) {
505 struct net2280_ep *ep;
507 ep = container_of(_ep, struct net2280_ep, ep);
510 dma_free_coherent(&ep->dev->pdev->dev, bytes, buf, dma);
516 /*-------------------------------------------------------------------------*/
518 /* load a packet into the fifo we use for usb IN transfers.
519 * works for all endpoints.
521 * NOTE: pio with ep-a..ep-d could stuff multiple packets into the fifo
522 * at a time, but this code is simpler because it knows it only writes
523 * one packet. ep-a..ep-d should use dma instead.
526 write_fifo (struct net2280_ep *ep, struct usb_request *req)
528 struct net2280_ep_regs __iomem *regs = ep->regs;
531 unsigned count, total;
533 /* INVARIANT: fifo is currently empty. (testable) */
536 buf = req->buf + req->actual;
538 total = req->length - req->actual;
544 /* write just one packet at a time */
545 count = ep->ep.maxpacket;
546 if (count > total) /* min() cannot be used on a bitfield */
549 VDEBUG (ep->dev, "write %s fifo (IN) %d bytes%s req %p\n",
551 (count != ep->ep.maxpacket) ? " (short)" : "",
554 /* NOTE be careful if you try to align these. fifo lines
555 * should normally be full (4 bytes) and successive partial
556 * lines are ok only in certain cases.
558 tmp = get_unaligned ((u32 *)buf);
560 writel (tmp, ®s->ep_data);
565 /* last fifo entry is "short" unless we wrote a full packet.
566 * also explicitly validate last word in (periodic) transfers
567 * when maxpacket is not a multiple of 4 bytes.
569 if (count || total < ep->ep.maxpacket) {
570 tmp = count ? get_unaligned ((u32 *)buf) : count;
572 set_fifo_bytecount (ep, count & 0x03);
573 writel (tmp, ®s->ep_data);
576 /* pci writes may still be posted */
579 /* work around erratum 0106: PCI and USB race over the OUT fifo.
580 * caller guarantees chiprev 0100, out endpoint is NAKing, and
581 * there's no real data in the fifo.
583 * NOTE: also used in cases where that erratum doesn't apply:
584 * where the host wrote "too much" data to us.
586 static void out_flush (struct net2280_ep *ep)
591 ASSERT_OUT_NAKING (ep);
593 statp = &ep->regs->ep_stat;
594 writel ( (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
595 | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
597 writel ((1 << FIFO_FLUSH), statp);
600 if (tmp & (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
601 /* high speed did bulk NYET; fifo isn't filling */
602 && ep->dev->gadget.speed == USB_SPEED_FULL) {
605 usec = 50; /* 64 byte bulk/interrupt */
606 handshake (statp, (1 << USB_OUT_PING_NAK_SENT),
607 (1 << USB_OUT_PING_NAK_SENT), usec);
608 /* NAK done; now CLEAR_NAK_OUT_PACKETS is safe */
612 /* unload packet(s) from the fifo we use for usb OUT transfers.
613 * returns true iff the request completed, because of short packet
614 * or the request buffer having filled with full packets.
616 * for ep-a..ep-d this will read multiple packets out when they
617 * have been accepted.
620 read_fifo (struct net2280_ep *ep, struct net2280_request *req)
622 struct net2280_ep_regs __iomem *regs = ep->regs;
623 u8 *buf = req->req.buf + req->req.actual;
624 unsigned count, tmp, is_short;
625 unsigned cleanup = 0, prevent = 0;
627 /* erratum 0106 ... packets coming in during fifo reads might
628 * be incompletely rejected. not all cases have workarounds.
630 if (ep->dev->chiprev == 0x0100
631 && ep->dev->gadget.speed == USB_SPEED_FULL) {
633 tmp = readl (&ep->regs->ep_stat);
634 if ((tmp & (1 << NAK_OUT_PACKETS)))
636 else if ((tmp & (1 << FIFO_FULL))) {
637 start_out_naking (ep);
640 /* else: hope we don't see the problem */
643 /* never overflow the rx buffer. the fifo reads packets until
644 * it sees a short one; we might not be ready for them all.
647 count = readl (®s->ep_avail);
648 if (unlikely (count == 0)) {
650 tmp = readl (&ep->regs->ep_stat);
651 count = readl (®s->ep_avail);
652 /* handled that data already? */
653 if (count == 0 && (tmp & (1 << NAK_OUT_PACKETS)) == 0)
657 tmp = req->req.length - req->req.actual;
659 /* as with DMA, data overflow gets flushed */
660 if ((tmp % ep->ep.maxpacket) != 0) {
662 "%s out fifo %d bytes, expected %d\n",
663 ep->ep.name, count, tmp);
664 req->req.status = -EOVERFLOW;
666 /* NAK_OUT_PACKETS will be set, so flushing is safe;
667 * the next read will start with the next packet
669 } /* else it's a ZLP, no worries */
672 req->req.actual += count;
674 is_short = (count == 0) || ((count % ep->ep.maxpacket) != 0);
676 VDEBUG (ep->dev, "read %s fifo (OUT) %d bytes%s%s%s req %p %d/%d\n",
677 ep->ep.name, count, is_short ? " (short)" : "",
678 cleanup ? " flush" : "", prevent ? " nak" : "",
679 req, req->req.actual, req->req.length);
682 tmp = readl (®s->ep_data);
684 put_unaligned (tmp, (u32 *)buf);
689 tmp = readl (®s->ep_data);
690 /* LE conversion is implicit here: */
699 writel ((1 << CLEAR_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
700 (void) readl (&ep->regs->ep_rsp);
703 return is_short || ((req->req.actual == req->req.length)
707 /* fill out dma descriptor to match a given request */
709 fill_dma_desc (struct net2280_ep *ep, struct net2280_request *req, int valid)
711 struct net2280_dma *td = req->td;
712 u32 dmacount = req->req.length;
714 /* don't let DMA continue after a short OUT packet,
715 * so overruns can't affect the next transfer.
716 * in case of overruns on max-size packets, we can't
717 * stop the fifo from filling but we can flush it.
720 dmacount |= (1 << DMA_DIRECTION);
721 else if ((dmacount % ep->ep.maxpacket) != 0)
722 dmacount |= (1 << END_OF_CHAIN);
726 dmacount |= (1 << VALID_BIT);
727 if (likely(!req->req.no_interrupt || !use_dma_chaining))
728 dmacount |= (1 << DMA_DONE_INTERRUPT_ENABLE);
730 /* td->dmadesc = previously set by caller */
731 td->dmaaddr = cpu_to_le32 (req->req.dma);
733 /* 2280 may be polling VALID_BIT through ep->dma->dmadesc */
735 td->dmacount = cpu_to_le32p (&dmacount);
738 static const u32 dmactl_default =
739 (1 << DMA_SCATTER_GATHER_DONE_INTERRUPT)
740 | (1 << DMA_CLEAR_COUNT_ENABLE)
741 /* erratum 0116 workaround part 1 (use POLLING) */
742 | (POLL_100_USEC << DESCRIPTOR_POLLING_RATE)
743 | (1 << DMA_VALID_BIT_POLLING_ENABLE)
744 | (1 << DMA_VALID_BIT_ENABLE)
745 | (1 << DMA_SCATTER_GATHER_ENABLE)
746 /* erratum 0116 workaround part 2 (no AUTOSTART) */
749 static inline void spin_stop_dma (struct net2280_dma_regs __iomem *dma)
751 handshake (&dma->dmactl, (1 << DMA_ENABLE), 0, 50);
754 static inline void stop_dma (struct net2280_dma_regs __iomem *dma)
756 writel (readl (&dma->dmactl) & ~(1 << DMA_ENABLE), &dma->dmactl);
760 static void start_queue (struct net2280_ep *ep, u32 dmactl, u32 td_dma)
762 struct net2280_dma_regs __iomem *dma = ep->dma;
764 writel ((1 << VALID_BIT) | (ep->is_in << DMA_DIRECTION),
766 writel (readl (&dma->dmastat), &dma->dmastat);
768 writel (td_dma, &dma->dmadesc);
769 writel (dmactl, &dma->dmactl);
771 /* erratum 0116 workaround part 3: pci arbiter away from net2280 */
772 (void) readl (&ep->dev->pci->pcimstctl);
774 writel ((1 << DMA_START), &dma->dmastat);
777 stop_out_naking (ep);
780 static void start_dma (struct net2280_ep *ep, struct net2280_request *req)
783 struct net2280_dma_regs __iomem *dma = ep->dma;
785 /* FIXME can't use DMA for ZLPs */
787 /* on this path we "know" there's no dma active (yet) */
788 WARN_ON (readl (&dma->dmactl) & (1 << DMA_ENABLE));
789 writel (0, &ep->dma->dmactl);
791 /* previous OUT packet might have been short */
792 if (!ep->is_in && ((tmp = readl (&ep->regs->ep_stat))
793 & (1 << NAK_OUT_PACKETS)) != 0) {
794 writel ((1 << SHORT_PACKET_TRANSFERRED_INTERRUPT),
797 tmp = readl (&ep->regs->ep_avail);
799 writel (readl (&dma->dmastat), &dma->dmastat);
801 /* transfer all/some fifo data */
802 writel (req->req.dma, &dma->dmaaddr);
803 tmp = min (tmp, req->req.length);
805 /* dma irq, faking scatterlist status */
806 req->td->dmacount = cpu_to_le32 (req->req.length - tmp);
807 writel ((1 << DMA_DONE_INTERRUPT_ENABLE)
808 | tmp, &dma->dmacount);
809 req->td->dmadesc = 0;
812 writel ((1 << DMA_ENABLE), &dma->dmactl);
813 writel ((1 << DMA_START), &dma->dmastat);
818 tmp = dmactl_default;
820 /* force packet boundaries between dma requests, but prevent the
821 * controller from automagically writing a last "short" packet
822 * (zero length) unless the driver explicitly said to do that.
825 if (likely ((req->req.length % ep->ep.maxpacket) != 0
827 tmp |= (1 << DMA_FIFO_VALIDATE);
828 ep->in_fifo_validate = 1;
830 ep->in_fifo_validate = 0;
833 /* init req->td, pointing to the current dummy */
834 req->td->dmadesc = cpu_to_le32 (ep->td_dma);
835 fill_dma_desc (ep, req, 1);
837 if (!use_dma_chaining)
838 req->td->dmacount |= __constant_cpu_to_le32 (1 << END_OF_CHAIN);
840 start_queue (ep, tmp, req->td_dma);
844 queue_dma (struct net2280_ep *ep, struct net2280_request *req, int valid)
846 struct net2280_dma *end;
849 /* swap new dummy for old, link; fill and maybe activate */
855 ep->td_dma = req->td_dma;
858 end->dmadesc = cpu_to_le32 (ep->td_dma);
860 fill_dma_desc (ep, req, valid);
864 done (struct net2280_ep *ep, struct net2280_request *req, int status)
867 unsigned stopped = ep->stopped;
869 list_del_init (&req->queue);
871 if (req->req.status == -EINPROGRESS)
872 req->req.status = status;
874 status = req->req.status;
878 pci_unmap_single (dev->pdev, req->req.dma, req->req.length,
879 ep->is_in ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
880 req->req.dma = DMA_ADDR_INVALID;
884 if (status && status != -ESHUTDOWN)
885 VDEBUG (dev, "complete %s req %p stat %d len %u/%u\n",
886 ep->ep.name, &req->req, status,
887 req->req.actual, req->req.length);
889 /* don't modify queue heads during completion callback */
891 spin_unlock (&dev->lock);
892 req->req.complete (&ep->ep, &req->req);
893 spin_lock (&dev->lock);
894 ep->stopped = stopped;
897 /*-------------------------------------------------------------------------*/
900 net2280_queue (struct usb_ep *_ep, struct usb_request *_req, int gfp_flags)
902 struct net2280_request *req;
903 struct net2280_ep *ep;
907 /* we always require a cpu-view buffer, so that we can
908 * always use pio (as fallback or whatever).
910 req = container_of (_req, struct net2280_request, req);
911 if (!_req || !_req->complete || !_req->buf
912 || !list_empty (&req->queue))
914 if (_req->length > (~0 & DMA_BYTE_COUNT_MASK))
916 ep = container_of (_ep, struct net2280_ep, ep);
917 if (!_ep || (!ep->desc && ep->num != 0))
920 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
923 /* FIXME implement PIO fallback for ZLPs with DMA */
924 if (ep->dma && _req->length == 0)
927 /* set up dma mapping in case the caller didn't */
928 if (ep->dma && _req->dma == DMA_ADDR_INVALID) {
929 _req->dma = pci_map_single (dev->pdev, _req->buf, _req->length,
930 ep->is_in ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
935 VDEBUG (dev, "%s queue req %p, len %d buf %p\n",
936 _ep->name, _req, _req->length, _req->buf);
939 spin_lock_irqsave (&dev->lock, flags);
941 _req->status = -EINPROGRESS;
944 /* kickstart this i/o queue? */
945 if (list_empty (&ep->queue) && !ep->stopped) {
946 /* use DMA if the endpoint supports it, else pio */
950 /* maybe there's no control data, just status ack */
951 if (ep->num == 0 && _req->length == 0) {
954 VDEBUG (dev, "%s status ack\n", ep->ep.name);
958 /* PIO ... stuff the fifo, or unblock it. */
960 write_fifo (ep, _req);
961 else if (list_empty (&ep->queue)) {
964 /* OUT FIFO might have packet(s) buffered */
965 s = readl (&ep->regs->ep_stat);
966 if ((s & (1 << FIFO_EMPTY)) == 0) {
967 /* note: _req->short_not_ok is
968 * ignored here since PIO _always_
969 * stops queue advance here, and
970 * _req->status doesn't change for
971 * short reads (only _req->actual)
973 if (read_fifo (ep, req)) {
980 s = readl (&ep->regs->ep_stat);
983 /* don't NAK, let the fifo fill */
984 if (req && (s & (1 << NAK_OUT_PACKETS)))
985 writel ((1 << CLEAR_NAK_OUT_PACKETS),
990 } else if (ep->dma) {
996 /* preventing magic zlps is per-engine state, not
997 * per-transfer; irq logic must recover hiccups.
999 expect = likely (req->req.zero
1000 || (req->req.length % ep->ep.maxpacket) != 0);
1001 if (expect != ep->in_fifo_validate)
1004 queue_dma (ep, req, valid);
1006 } /* else the irq handler advances the queue. */
1009 list_add_tail (&req->queue, &ep->queue);
1011 spin_unlock_irqrestore (&dev->lock, flags);
1013 /* pci writes may still be posted */
1019 struct net2280_ep *ep,
1020 struct net2280_request *req,
1025 req->req.actual = req->req.length - (DMA_BYTE_COUNT_MASK & dmacount);
1026 done (ep, req, status);
1029 static void restart_dma (struct net2280_ep *ep);
1031 static void scan_dma_completions (struct net2280_ep *ep)
1033 /* only look at descriptors that were "naturally" retired,
1034 * so fifo and list head state won't matter
1036 while (!list_empty (&ep->queue)) {
1037 struct net2280_request *req;
1040 req = list_entry (ep->queue.next,
1041 struct net2280_request, queue);
1045 tmp = le32_to_cpup (&req->td->dmacount);
1046 if ((tmp & (1 << VALID_BIT)) != 0)
1049 /* SHORT_PACKET_TRANSFERRED_INTERRUPT handles "usb-short"
1050 * cases where DMA must be aborted; this code handles
1051 * all non-abort DMA completions.
1053 if (unlikely (req->td->dmadesc == 0)) {
1055 tmp = readl (&ep->dma->dmacount);
1056 if (tmp & DMA_BYTE_COUNT_MASK)
1058 /* single transfer mode */
1059 dma_done (ep, req, tmp, 0);
1061 } else if (!ep->is_in
1062 && (req->req.length % ep->ep.maxpacket) != 0) {
1063 tmp = readl (&ep->regs->ep_stat);
1065 /* AVOID TROUBLE HERE by not issuing short reads from
1066 * your gadget driver. That helps avoids errata 0121,
1067 * 0122, and 0124; not all cases trigger the warning.
1069 if ((tmp & (1 << NAK_OUT_PACKETS)) == 0) {
1070 WARN (ep->dev, "%s lost packet sync!\n",
1072 req->req.status = -EOVERFLOW;
1073 } else if ((tmp = readl (&ep->regs->ep_avail)) != 0) {
1074 /* fifo gets flushed later */
1075 ep->out_overflow = 1;
1076 DEBUG (ep->dev, "%s dma, discard %d len %d\n",
1079 req->req.status = -EOVERFLOW;
1082 dma_done (ep, req, tmp, 0);
1086 static void restart_dma (struct net2280_ep *ep)
1088 struct net2280_request *req;
1089 u32 dmactl = dmactl_default;
1093 req = list_entry (ep->queue.next, struct net2280_request, queue);
1095 if (!use_dma_chaining) {
1096 start_dma (ep, req);
1100 /* the 2280 will be processing the queue unless queue hiccups after
1101 * the previous transfer:
1102 * IN: wanted automagic zlp, head doesn't (or vice versa)
1103 * DMA_FIFO_VALIDATE doesn't init from dma descriptors.
1104 * OUT: was "usb-short", we must restart.
1106 if (ep->is_in && !req->valid) {
1107 struct net2280_request *entry, *prev = NULL;
1108 int reqmode, done = 0;
1110 DEBUG (ep->dev, "%s dma hiccup td %p\n", ep->ep.name, req->td);
1111 ep->in_fifo_validate = likely (req->req.zero
1112 || (req->req.length % ep->ep.maxpacket) != 0);
1113 if (ep->in_fifo_validate)
1114 dmactl |= (1 << DMA_FIFO_VALIDATE);
1115 list_for_each_entry (entry, &ep->queue, queue) {
1120 dmacount = entry->td->dmacount;
1122 reqmode = likely (entry->req.zero
1123 || (entry->req.length
1124 % ep->ep.maxpacket) != 0);
1125 if (reqmode == ep->in_fifo_validate) {
1127 dmacount |= valid_bit;
1128 entry->td->dmacount = dmacount;
1132 /* force a hiccup */
1133 prev->td->dmacount |= dma_done_ie;
1138 /* walk the rest of the queue so unlinks behave */
1140 dmacount &= ~valid_bit;
1141 entry->td->dmacount = dmacount;
1146 writel (0, &ep->dma->dmactl);
1147 start_queue (ep, dmactl, req->td_dma);
1150 static void abort_dma (struct net2280_ep *ep)
1152 /* abort the current transfer */
1153 if (likely (!list_empty (&ep->queue))) {
1154 /* FIXME work around errata 0121, 0122, 0124 */
1155 writel ((1 << DMA_ABORT), &ep->dma->dmastat);
1156 spin_stop_dma (ep->dma);
1159 scan_dma_completions (ep);
1162 /* dequeue ALL requests */
1163 static void nuke (struct net2280_ep *ep)
1165 struct net2280_request *req;
1167 /* called with spinlock held */
1171 while (!list_empty (&ep->queue)) {
1172 req = list_entry (ep->queue.next,
1173 struct net2280_request,
1175 done (ep, req, -ESHUTDOWN);
1179 /* dequeue JUST ONE request */
1180 static int net2280_dequeue (struct usb_ep *_ep, struct usb_request *_req)
1182 struct net2280_ep *ep;
1183 struct net2280_request *req;
1184 unsigned long flags;
1188 ep = container_of (_ep, struct net2280_ep, ep);
1189 if (!_ep || (!ep->desc && ep->num != 0) || !_req)
1192 spin_lock_irqsave (&ep->dev->lock, flags);
1193 stopped = ep->stopped;
1195 /* quiesce dma while we patch the queue */
1199 dmactl = readl (&ep->dma->dmactl);
1200 /* WARNING erratum 0127 may kick in ... */
1202 scan_dma_completions (ep);
1205 /* make sure it's still queued on this endpoint */
1206 list_for_each_entry (req, &ep->queue, queue) {
1207 if (&req->req == _req)
1210 if (&req->req != _req) {
1211 spin_unlock_irqrestore (&ep->dev->lock, flags);
1215 /* queue head may be partially complete. */
1216 if (ep->queue.next == &req->queue) {
1218 DEBUG (ep->dev, "unlink (%s) dma\n", _ep->name);
1219 _req->status = -ECONNRESET;
1221 if (likely (ep->queue.next == &req->queue)) {
1222 // NOTE: misreports single-transfer mode
1223 req->td->dmacount = 0; /* invalidate */
1225 readl (&ep->dma->dmacount),
1229 DEBUG (ep->dev, "unlink (%s) pio\n", _ep->name);
1230 done (ep, req, -ECONNRESET);
1234 /* patch up hardware chaining data */
1235 } else if (ep->dma && use_dma_chaining) {
1236 if (req->queue.prev == ep->queue.next) {
1237 writel (le32_to_cpu (req->td->dmadesc),
1239 if (req->td->dmacount & dma_done_ie)
1240 writel (readl (&ep->dma->dmacount)
1241 | le32_to_cpu(dma_done_ie),
1242 &ep->dma->dmacount);
1244 struct net2280_request *prev;
1246 prev = list_entry (req->queue.prev,
1247 struct net2280_request, queue);
1248 prev->td->dmadesc = req->td->dmadesc;
1249 if (req->td->dmacount & dma_done_ie)
1250 prev->td->dmacount |= dma_done_ie;
1255 done (ep, req, -ECONNRESET);
1256 ep->stopped = stopped;
1259 /* turn off dma on inactive queues */
1260 if (list_empty (&ep->queue))
1262 else if (!ep->stopped) {
1263 /* resume current request, or start new one */
1265 writel (dmactl, &ep->dma->dmactl);
1267 start_dma (ep, list_entry (ep->queue.next,
1268 struct net2280_request, queue));
1272 spin_unlock_irqrestore (&ep->dev->lock, flags);
1276 /*-------------------------------------------------------------------------*/
1278 static int net2280_fifo_status (struct usb_ep *_ep);
1281 net2280_set_halt (struct usb_ep *_ep, int value)
1283 struct net2280_ep *ep;
1284 unsigned long flags;
1287 ep = container_of (_ep, struct net2280_ep, ep);
1288 if (!_ep || (!ep->desc && ep->num != 0))
1290 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1292 if (ep->desc /* not ep0 */ && (ep->desc->bmAttributes & 0x03)
1293 == USB_ENDPOINT_XFER_ISOC)
1296 spin_lock_irqsave (&ep->dev->lock, flags);
1297 if (!list_empty (&ep->queue))
1299 else if (ep->is_in && value && net2280_fifo_status (_ep) != 0)
1302 VDEBUG (ep->dev, "%s %s halt\n", _ep->name,
1303 value ? "set" : "clear");
1304 /* set/clear, then synch memory views with the device */
1307 ep->dev->protocol_stall = 1;
1312 (void) readl (&ep->regs->ep_rsp);
1314 spin_unlock_irqrestore (&ep->dev->lock, flags);
1320 net2280_fifo_status (struct usb_ep *_ep)
1322 struct net2280_ep *ep;
1325 ep = container_of (_ep, struct net2280_ep, ep);
1326 if (!_ep || (!ep->desc && ep->num != 0))
1328 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1331 avail = readl (&ep->regs->ep_avail) & ((1 << 12) - 1);
1332 if (avail > ep->fifo_size)
1335 avail = ep->fifo_size - avail;
1340 net2280_fifo_flush (struct usb_ep *_ep)
1342 struct net2280_ep *ep;
1344 ep = container_of (_ep, struct net2280_ep, ep);
1345 if (!_ep || (!ep->desc && ep->num != 0))
1347 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1350 writel ((1 << FIFO_FLUSH), &ep->regs->ep_stat);
1351 (void) readl (&ep->regs->ep_rsp);
1354 static struct usb_ep_ops net2280_ep_ops = {
1355 .enable = net2280_enable,
1356 .disable = net2280_disable,
1358 .alloc_request = net2280_alloc_request,
1359 .free_request = net2280_free_request,
1361 .alloc_buffer = net2280_alloc_buffer,
1362 .free_buffer = net2280_free_buffer,
1364 .queue = net2280_queue,
1365 .dequeue = net2280_dequeue,
1367 .set_halt = net2280_set_halt,
1368 .fifo_status = net2280_fifo_status,
1369 .fifo_flush = net2280_fifo_flush,
1372 /*-------------------------------------------------------------------------*/
1374 static int net2280_get_frame (struct usb_gadget *_gadget)
1376 struct net2280 *dev;
1377 unsigned long flags;
1382 dev = container_of (_gadget, struct net2280, gadget);
1383 spin_lock_irqsave (&dev->lock, flags);
1384 retval = get_idx_reg (dev->regs, REG_FRAME) & 0x03ff;
1385 spin_unlock_irqrestore (&dev->lock, flags);
1389 static int net2280_wakeup (struct usb_gadget *_gadget)
1391 struct net2280 *dev;
1393 unsigned long flags;
1397 dev = container_of (_gadget, struct net2280, gadget);
1399 spin_lock_irqsave (&dev->lock, flags);
1400 tmp = readl (&dev->usb->usbctl);
1401 if (tmp & (1 << DEVICE_REMOTE_WAKEUP_ENABLE))
1402 writel (1 << GENERATE_RESUME, &dev->usb->usbstat);
1403 spin_unlock_irqrestore (&dev->lock, flags);
1405 /* pci writes may still be posted */
1409 static int net2280_set_selfpowered (struct usb_gadget *_gadget, int value)
1411 struct net2280 *dev;
1413 unsigned long flags;
1417 dev = container_of (_gadget, struct net2280, gadget);
1419 spin_lock_irqsave (&dev->lock, flags);
1420 tmp = readl (&dev->usb->usbctl);
1422 tmp |= (1 << SELF_POWERED_STATUS);
1424 tmp &= ~(1 << SELF_POWERED_STATUS);
1425 writel (tmp, &dev->usb->usbctl);
1426 spin_unlock_irqrestore (&dev->lock, flags);
1431 static int net2280_pullup(struct usb_gadget *_gadget, int is_on)
1433 struct net2280 *dev;
1435 unsigned long flags;
1439 dev = container_of (_gadget, struct net2280, gadget);
1441 spin_lock_irqsave (&dev->lock, flags);
1442 tmp = readl (&dev->usb->usbctl);
1443 dev->softconnect = (is_on != 0);
1445 tmp |= (1 << USB_DETECT_ENABLE);
1447 tmp &= ~(1 << USB_DETECT_ENABLE);
1448 writel (tmp, &dev->usb->usbctl);
1449 spin_unlock_irqrestore (&dev->lock, flags);
1454 static const struct usb_gadget_ops net2280_ops = {
1455 .get_frame = net2280_get_frame,
1456 .wakeup = net2280_wakeup,
1457 .set_selfpowered = net2280_set_selfpowered,
1458 .pullup = net2280_pullup,
1461 /*-------------------------------------------------------------------------*/
1463 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1465 /* FIXME move these into procfs, and use seq_file.
1466 * Sysfs _still_ doesn't behave for arbitrarily sized files,
1467 * and also doesn't help products using this with 2.4 kernels.
1470 /* "function" sysfs attribute */
1472 show_function (struct device *_dev, struct device_attribute *attr, char *buf)
1474 struct net2280 *dev = dev_get_drvdata (_dev);
1477 || !dev->driver->function
1478 || strlen (dev->driver->function) > PAGE_SIZE)
1480 return scnprintf (buf, PAGE_SIZE, "%s\n", dev->driver->function);
1482 static DEVICE_ATTR (function, S_IRUGO, show_function, NULL);
1485 show_registers (struct device *_dev, struct device_attribute *attr, char *buf)
1487 struct net2280 *dev;
1490 unsigned long flags;
1495 dev = dev_get_drvdata (_dev);
1498 spin_lock_irqsave (&dev->lock, flags);
1501 s = dev->driver->driver.name;
1505 /* Main Control Registers */
1506 t = scnprintf (next, size, "%s version " DRIVER_VERSION
1507 ", chiprev %04x, dma %s\n\n"
1508 "devinit %03x fifoctl %08x gadget '%s'\n"
1509 "pci irqenb0 %02x irqenb1 %08x "
1510 "irqstat0 %04x irqstat1 %08x\n",
1511 driver_name, dev->chiprev,
1513 ? (use_dma_chaining ? "chaining" : "enabled")
1515 readl (&dev->regs->devinit),
1516 readl (&dev->regs->fifoctl),
1518 readl (&dev->regs->pciirqenb0),
1519 readl (&dev->regs->pciirqenb1),
1520 readl (&dev->regs->irqstat0),
1521 readl (&dev->regs->irqstat1));
1525 /* USB Control Registers */
1526 t1 = readl (&dev->usb->usbctl);
1527 t2 = readl (&dev->usb->usbstat);
1528 if (t1 & (1 << VBUS_PIN)) {
1529 if (t2 & (1 << HIGH_SPEED))
1531 else if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1535 /* full speed bit (6) not working?? */
1538 t = scnprintf (next, size,
1539 "stdrsp %08x usbctl %08x usbstat %08x "
1540 "addr 0x%02x (%s)\n",
1541 readl (&dev->usb->stdrsp), t1, t2,
1542 readl (&dev->usb->ouraddr), s);
1546 /* PCI Master Control Registers */
1548 /* DMA Control Registers */
1550 /* Configurable EP Control Registers */
1551 for (i = 0; i < 7; i++) {
1552 struct net2280_ep *ep;
1558 t1 = readl (&ep->regs->ep_cfg);
1559 t2 = readl (&ep->regs->ep_rsp) & 0xff;
1560 t = scnprintf (next, size,
1561 "\n%s\tcfg %05x rsp (%02x) %s%s%s%s%s%s%s%s"
1563 ep->ep.name, t1, t2,
1564 (t2 & (1 << CLEAR_NAK_OUT_PACKETS))
1566 (t2 & (1 << CLEAR_EP_HIDE_STATUS_PHASE))
1568 (t2 & (1 << CLEAR_EP_FORCE_CRC_ERROR))
1570 (t2 & (1 << CLEAR_INTERRUPT_MODE))
1571 ? "interrupt " : "",
1572 (t2 & (1<<CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE))
1574 (t2 & (1 << CLEAR_NAK_OUT_PACKETS_MODE))
1576 (t2 & (1 << CLEAR_ENDPOINT_TOGGLE))
1577 ? "DATA1 " : "DATA0 ",
1578 (t2 & (1 << CLEAR_ENDPOINT_HALT))
1580 readl (&ep->regs->ep_irqenb));
1584 t = scnprintf (next, size,
1585 "\tstat %08x avail %04x "
1587 readl (&ep->regs->ep_stat),
1588 readl (&ep->regs->ep_avail),
1589 t1 & 0x0f, DIR_STRING (t1),
1590 type_string (t1 >> 8),
1591 ep->stopped ? "*" : "");
1598 t = scnprintf (next, size,
1599 " dma\tctl %08x stat %08x count %08x\n"
1600 "\taddr %08x desc %08x\n",
1601 readl (&ep->dma->dmactl),
1602 readl (&ep->dma->dmastat),
1603 readl (&ep->dma->dmacount),
1604 readl (&ep->dma->dmaaddr),
1605 readl (&ep->dma->dmadesc));
1611 /* Indexed Registers */
1615 t = scnprintf (next, size, "\nirqs: ");
1618 for (i = 0; i < 7; i++) {
1619 struct net2280_ep *ep;
1624 t = scnprintf (next, size, " %s/%lu", ep->ep.name, ep->irqs);
1629 t = scnprintf (next, size, "\n");
1633 spin_unlock_irqrestore (&dev->lock, flags);
1635 return PAGE_SIZE - size;
1637 static DEVICE_ATTR (registers, S_IRUGO, show_registers, NULL);
1640 show_queues (struct device *_dev, struct device_attribute *attr, char *buf)
1642 struct net2280 *dev;
1645 unsigned long flags;
1648 dev = dev_get_drvdata (_dev);
1651 spin_lock_irqsave (&dev->lock, flags);
1653 for (i = 0; i < 7; i++) {
1654 struct net2280_ep *ep = &dev->ep [i];
1655 struct net2280_request *req;
1659 const struct usb_endpoint_descriptor *d;
1664 t = d->bEndpointAddress;
1665 t = scnprintf (next, size,
1666 "\n%s (ep%d%s-%s) max %04x %s fifo %d\n",
1667 ep->ep.name, t & USB_ENDPOINT_NUMBER_MASK,
1668 (t & USB_DIR_IN) ? "in" : "out",
1670 switch (d->bmAttributes & 0x03) {
1671 case USB_ENDPOINT_XFER_BULK:
1672 val = "bulk"; break;
1673 case USB_ENDPOINT_XFER_INT:
1674 val = "intr"; break;
1678 le16_to_cpu (d->wMaxPacketSize) & 0x1fff,
1679 ep->dma ? "dma" : "pio", ep->fifo_size
1681 } else /* ep0 should only have one transfer queued */
1682 t = scnprintf (next, size, "ep0 max 64 pio %s\n",
1683 ep->is_in ? "in" : "out");
1684 if (t <= 0 || t > size)
1689 if (list_empty (&ep->queue)) {
1690 t = scnprintf (next, size, "\t(nothing queued)\n");
1691 if (t <= 0 || t > size)
1697 list_for_each_entry (req, &ep->queue, queue) {
1698 if (ep->dma && req->td_dma == readl (&ep->dma->dmadesc))
1699 t = scnprintf (next, size,
1700 "\treq %p len %d/%d "
1701 "buf %p (dmacount %08x)\n",
1702 &req->req, req->req.actual,
1703 req->req.length, req->req.buf,
1704 readl (&ep->dma->dmacount));
1706 t = scnprintf (next, size,
1707 "\treq %p len %d/%d buf %p\n",
1708 &req->req, req->req.actual,
1709 req->req.length, req->req.buf);
1710 if (t <= 0 || t > size)
1716 struct net2280_dma *td;
1719 t = scnprintf (next, size, "\t td %08x "
1720 " count %08x buf %08x desc %08x\n",
1722 le32_to_cpu (td->dmacount),
1723 le32_to_cpu (td->dmaaddr),
1724 le32_to_cpu (td->dmadesc));
1725 if (t <= 0 || t > size)
1734 spin_unlock_irqrestore (&dev->lock, flags);
1735 return PAGE_SIZE - size;
1737 static DEVICE_ATTR (queues, S_IRUGO, show_queues, NULL);
1742 #define device_create_file(a,b) do {} while (0)
1743 #define device_remove_file device_create_file
1747 /*-------------------------------------------------------------------------*/
1749 /* another driver-specific mode might be a request type doing dma
1750 * to/from another device fifo instead of to/from memory.
1753 static void set_fifo_mode (struct net2280 *dev, int mode)
1755 /* keeping high bits preserves BAR2 */
1756 writel ((0xffff << PCI_BASE2_RANGE) | mode, &dev->regs->fifoctl);
1758 /* always ep-{a,b,e,f} ... maybe not ep-c or ep-d */
1759 INIT_LIST_HEAD (&dev->gadget.ep_list);
1760 list_add_tail (&dev->ep [1].ep.ep_list, &dev->gadget.ep_list);
1761 list_add_tail (&dev->ep [2].ep.ep_list, &dev->gadget.ep_list);
1764 list_add_tail (&dev->ep [3].ep.ep_list, &dev->gadget.ep_list);
1765 list_add_tail (&dev->ep [4].ep.ep_list, &dev->gadget.ep_list);
1766 dev->ep [1].fifo_size = dev->ep [2].fifo_size = 1024;
1769 dev->ep [1].fifo_size = dev->ep [2].fifo_size = 2048;
1772 list_add_tail (&dev->ep [3].ep.ep_list, &dev->gadget.ep_list);
1773 dev->ep [1].fifo_size = 2048;
1774 dev->ep [2].fifo_size = 1024;
1777 /* fifo sizes for ep0, ep-c, ep-d, ep-e, and ep-f never change */
1778 list_add_tail (&dev->ep [5].ep.ep_list, &dev->gadget.ep_list);
1779 list_add_tail (&dev->ep [6].ep.ep_list, &dev->gadget.ep_list);
1782 /* just declare this in any driver that really need it */
1783 extern int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode);
1786 * net2280_set_fifo_mode - change allocation of fifo buffers
1787 * @gadget: access to the net2280 device that will be updated
1788 * @mode: 0 for default, four 1kB buffers (ep-a through ep-d);
1789 * 1 for two 2kB buffers (ep-a and ep-b only);
1790 * 2 for one 2kB buffer (ep-a) and two 1kB ones (ep-b, ep-c).
1792 * returns zero on success, else negative errno. when this succeeds,
1793 * the contents of gadget->ep_list may have changed.
1795 * you may only call this function when endpoints a-d are all disabled.
1796 * use it whenever extra hardware buffering can help performance, such
1797 * as before enabling "high bandwidth" interrupt endpoints that use
1798 * maxpacket bigger than 512 (when double buffering would otherwise
1801 int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode)
1804 struct net2280 *dev;
1806 unsigned long flags;
1810 dev = container_of (gadget, struct net2280, gadget);
1812 spin_lock_irqsave (&dev->lock, flags);
1814 for (i = 1; i <= 4; i++)
1815 if (dev->ep [i].desc) {
1819 if (mode < 0 || mode > 2)
1822 set_fifo_mode (dev, mode);
1823 spin_unlock_irqrestore (&dev->lock, flags);
1827 DEBUG (dev, "fifo: ep-a 2K, ep-b 2K\n");
1829 DEBUG (dev, "fifo: ep-a 2K, ep-b 1K, ep-c 1K\n");
1830 /* else all are 1K */
1834 EXPORT_SYMBOL (net2280_set_fifo_mode);
1836 /*-------------------------------------------------------------------------*/
1838 /* keeping it simple:
1839 * - one bus driver, initted first;
1840 * - one function driver, initted second
1842 * most of the work to support multiple net2280 controllers would
1843 * be to associate this gadget driver (yes?) with all of them, or
1844 * perhaps to bind specific drivers to specific devices.
1847 static struct net2280 *the_controller;
1849 static void usb_reset (struct net2280 *dev)
1853 dev->gadget.speed = USB_SPEED_UNKNOWN;
1854 (void) readl (&dev->usb->usbctl);
1856 net2280_led_init (dev);
1858 /* disable automatic responses, and irqs */
1859 writel (0, &dev->usb->stdrsp);
1860 writel (0, &dev->regs->pciirqenb0);
1861 writel (0, &dev->regs->pciirqenb1);
1863 /* clear old dma and irq state */
1864 for (tmp = 0; tmp < 4; tmp++) {
1865 struct net2280_ep *ep = &dev->ep [tmp + 1];
1870 writel (~0, &dev->regs->irqstat0),
1871 writel (~(1 << SUSPEND_REQUEST_INTERRUPT), &dev->regs->irqstat1),
1873 /* reset, and enable pci */
1874 tmp = readl (&dev->regs->devinit)
1876 | (1 << FIFO_SOFT_RESET)
1877 | (1 << USB_SOFT_RESET)
1878 | (1 << M8051_RESET);
1879 writel (tmp, &dev->regs->devinit);
1881 /* standard fifo and endpoint allocations */
1882 set_fifo_mode (dev, (fifo_mode <= 2) ? fifo_mode : 0);
1885 static void usb_reinit (struct net2280 *dev)
1890 /* use_dma changes are ignored till next device re-init */
1893 /* basic endpoint init */
1894 for (tmp = 0; tmp < 7; tmp++) {
1895 struct net2280_ep *ep = &dev->ep [tmp];
1897 ep->ep.name = ep_name [tmp];
1901 if (tmp > 0 && tmp <= 4) {
1902 ep->fifo_size = 1024;
1904 ep->dma = &dev->dma [tmp - 1];
1907 ep->regs = &dev->epregs [tmp];
1908 ep_reset (dev->regs, ep);
1910 dev->ep [0].ep.maxpacket = 64;
1911 dev->ep [5].ep.maxpacket = 64;
1912 dev->ep [6].ep.maxpacket = 64;
1914 dev->gadget.ep0 = &dev->ep [0].ep;
1915 dev->ep [0].stopped = 0;
1916 INIT_LIST_HEAD (&dev->gadget.ep0->ep_list);
1918 /* we want to prevent lowlevel/insecure access from the USB host,
1919 * but erratum 0119 means this enable bit is ignored
1921 for (tmp = 0; tmp < 5; tmp++)
1922 writel (EP_DONTUSE, &dev->dep [tmp].dep_cfg);
1925 static void ep0_start (struct net2280 *dev)
1927 writel ( (1 << CLEAR_EP_HIDE_STATUS_PHASE)
1928 | (1 << CLEAR_NAK_OUT_PACKETS)
1929 | (1 << CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE)
1930 , &dev->epregs [0].ep_rsp);
1933 * hardware optionally handles a bunch of standard requests
1934 * that the API hides from drivers anyway. have it do so.
1935 * endpoint status/features are handled in software, to
1936 * help pass tests for some dubious behavior.
1938 writel ( (1 << SET_TEST_MODE)
1939 | (1 << SET_ADDRESS)
1940 | (1 << DEVICE_SET_CLEAR_DEVICE_REMOTE_WAKEUP)
1941 | (1 << GET_DEVICE_STATUS)
1942 | (1 << GET_INTERFACE_STATUS)
1943 , &dev->usb->stdrsp);
1944 writel ( (1 << USB_ROOT_PORT_WAKEUP_ENABLE)
1945 | (1 << SELF_POWERED_USB_DEVICE)
1946 | (1 << REMOTE_WAKEUP_SUPPORT)
1947 | (dev->softconnect << USB_DETECT_ENABLE)
1948 | (1 << SELF_POWERED_STATUS)
1949 , &dev->usb->usbctl);
1951 /* enable irqs so we can see ep0 and general operation */
1952 writel ( (1 << SETUP_PACKET_INTERRUPT_ENABLE)
1953 | (1 << ENDPOINT_0_INTERRUPT_ENABLE)
1954 , &dev->regs->pciirqenb0);
1955 writel ( (1 << PCI_INTERRUPT_ENABLE)
1956 | (1 << PCI_MASTER_ABORT_RECEIVED_INTERRUPT_ENABLE)
1957 | (1 << PCI_TARGET_ABORT_RECEIVED_INTERRUPT_ENABLE)
1958 | (1 << PCI_RETRY_ABORT_INTERRUPT_ENABLE)
1959 | (1 << VBUS_INTERRUPT_ENABLE)
1960 | (1 << ROOT_PORT_RESET_INTERRUPT_ENABLE)
1961 | (1 << SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE)
1962 , &dev->regs->pciirqenb1);
1964 /* don't leave any writes posted */
1965 (void) readl (&dev->usb->usbctl);
1968 /* when a driver is successfully registered, it will receive
1969 * control requests including set_configuration(), which enables
1970 * non-control requests. then usb traffic follows until a
1971 * disconnect is reported. then a host may connect again, or
1972 * the driver might get unbound.
1974 int usb_gadget_register_driver (struct usb_gadget_driver *driver)
1976 struct net2280 *dev = the_controller;
1980 /* insist on high speed support from the driver, since
1981 * (dev->usb->xcvrdiag & FORCE_FULL_SPEED_MODE)
1982 * "must not be used in normal operation"
1985 || driver->speed != USB_SPEED_HIGH
1995 for (i = 0; i < 7; i++)
1996 dev->ep [i].irqs = 0;
1998 /* hook up the driver ... */
1999 dev->softconnect = 1;
2000 driver->driver.bus = NULL;
2001 dev->driver = driver;
2002 dev->gadget.dev.driver = &driver->driver;
2003 retval = driver->bind (&dev->gadget);
2005 DEBUG (dev, "bind to driver %s --> %d\n",
2006 driver->driver.name, retval);
2008 dev->gadget.dev.driver = NULL;
2012 device_create_file (&dev->pdev->dev, &dev_attr_function);
2013 device_create_file (&dev->pdev->dev, &dev_attr_queues);
2015 /* ... then enable host detection and ep0; and we're ready
2016 * for set_configuration as well as eventual disconnect.
2018 net2280_led_active (dev, 1);
2021 DEBUG (dev, "%s ready, usbctl %08x stdrsp %08x\n",
2022 driver->driver.name,
2023 readl (&dev->usb->usbctl),
2024 readl (&dev->usb->stdrsp));
2026 /* pci writes may still be posted */
2029 EXPORT_SYMBOL (usb_gadget_register_driver);
2032 stop_activity (struct net2280 *dev, struct usb_gadget_driver *driver)
2036 /* don't disconnect if it's not connected */
2037 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
2040 /* stop hardware; prevent new request submissions;
2041 * and kill any outstanding requests.
2044 for (i = 0; i < 7; i++)
2045 nuke (&dev->ep [i]);
2047 /* report disconnect; the driver is already quiesced */
2049 spin_unlock (&dev->lock);
2050 driver->disconnect (&dev->gadget);
2051 spin_lock (&dev->lock);
2057 int usb_gadget_unregister_driver (struct usb_gadget_driver *driver)
2059 struct net2280 *dev = the_controller;
2060 unsigned long flags;
2064 if (!driver || driver != dev->driver)
2067 spin_lock_irqsave (&dev->lock, flags);
2068 stop_activity (dev, driver);
2069 spin_unlock_irqrestore (&dev->lock, flags);
2071 net2280_pullup (&dev->gadget, 0);
2073 driver->unbind (&dev->gadget);
2074 dev->gadget.dev.driver = NULL;
2077 net2280_led_active (dev, 0);
2078 device_remove_file (&dev->pdev->dev, &dev_attr_function);
2079 device_remove_file (&dev->pdev->dev, &dev_attr_queues);
2081 DEBUG (dev, "unregistered driver '%s'\n", driver->driver.name);
2084 EXPORT_SYMBOL (usb_gadget_unregister_driver);
2087 /*-------------------------------------------------------------------------*/
2089 /* handle ep0, ep-e, ep-f with 64 byte packets: packet per irq.
2090 * also works for dma-capable endpoints, in pio mode or just
2091 * to manually advance the queue after short OUT transfers.
2093 static void handle_ep_small (struct net2280_ep *ep)
2095 struct net2280_request *req;
2097 /* 0 error, 1 mid-data, 2 done */
2100 if (!list_empty (&ep->queue))
2101 req = list_entry (ep->queue.next,
2102 struct net2280_request, queue);
2106 /* ack all, and handle what we care about */
2107 t = readl (&ep->regs->ep_stat);
2110 VDEBUG (ep->dev, "%s ack ep_stat %08x, req %p\n",
2111 ep->ep.name, t, req ? &req->req : 0);
2113 writel (t & ~(1 << NAK_OUT_PACKETS), &ep->regs->ep_stat);
2115 /* for ep0, monitor token irqs to catch data stage length errors
2116 * and to synchronize on status.
2118 * also, to defer reporting of protocol stalls ... here's where
2119 * data or status first appears, handling stalls here should never
2120 * cause trouble on the host side..
2122 * control requests could be slightly faster without token synch for
2123 * status, but status can jam up that way.
2125 if (unlikely (ep->num == 0)) {
2127 /* status; stop NAKing */
2128 if (t & (1 << DATA_OUT_PING_TOKEN_INTERRUPT)) {
2129 if (ep->dev->protocol_stall) {
2136 /* reply to extra IN data tokens with a zlp */
2137 } else if (t & (1 << DATA_IN_TOKEN_INTERRUPT)) {
2138 if (ep->dev->protocol_stall) {
2142 } else if (!req && ep->stopped)
2143 write_fifo (ep, NULL);
2146 /* status; stop NAKing */
2147 if (t & (1 << DATA_IN_TOKEN_INTERRUPT)) {
2148 if (ep->dev->protocol_stall) {
2153 /* an extra OUT token is an error */
2154 } else if (((t & (1 << DATA_OUT_PING_TOKEN_INTERRUPT))
2156 && req->req.actual == req->req.length)
2158 ep->dev->protocol_stall = 1;
2162 done (ep, req, -EOVERFLOW);
2168 if (unlikely (!req))
2171 /* manual DMA queue advance after short OUT */
2172 if (likely (ep->dma != 0)) {
2173 if (t & (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)) {
2175 int stopped = ep->stopped;
2177 /* TRANSFERRED works around OUT_DONE erratum 0112.
2178 * we expect (N <= maxpacket) bytes; host wrote M.
2179 * iff (M < N) we won't ever see a DMA interrupt.
2182 for (count = 0; ; t = readl (&ep->regs->ep_stat)) {
2184 /* any preceding dma transfers must finish.
2185 * dma handles (M >= N), may empty the queue
2187 scan_dma_completions (ep);
2188 if (unlikely (list_empty (&ep->queue)
2189 || ep->out_overflow)) {
2193 req = list_entry (ep->queue.next,
2194 struct net2280_request, queue);
2196 /* here either (M < N), a "real" short rx;
2197 * or (M == N) and the queue didn't empty
2199 if (likely (t & (1 << FIFO_EMPTY))) {
2200 count = readl (&ep->dma->dmacount);
2201 count &= DMA_BYTE_COUNT_MASK;
2202 if (readl (&ep->dma->dmadesc)
2210 /* stop DMA, leave ep NAKing */
2211 writel ((1 << DMA_ABORT), &ep->dma->dmastat);
2212 spin_stop_dma (ep->dma);
2215 req->td->dmacount = 0;
2216 t = readl (&ep->regs->ep_avail);
2217 dma_done (ep, req, count, t);
2220 /* also flush to prevent erratum 0106 trouble */
2221 if (unlikely (ep->out_overflow
2222 || (ep->dev->chiprev == 0x0100
2223 && ep->dev->gadget.speed
2224 == USB_SPEED_FULL))) {
2226 ep->out_overflow = 0;
2229 /* (re)start dma if needed, stop NAKing */
2230 ep->stopped = stopped;
2231 if (!list_empty (&ep->queue))
2234 DEBUG (ep->dev, "%s dma ep_stat %08x ??\n",
2238 /* data packet(s) received (in the fifo, OUT) */
2239 } else if (t & (1 << DATA_PACKET_RECEIVED_INTERRUPT)) {
2240 if (read_fifo (ep, req) && ep->num != 0)
2243 /* data packet(s) transmitted (IN) */
2244 } else if (t & (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)) {
2247 len = req->req.length - req->req.actual;
2248 if (len > ep->ep.maxpacket)
2249 len = ep->ep.maxpacket;
2250 req->req.actual += len;
2252 /* if we wrote it all, we're usually done */
2253 if (req->req.actual == req->req.length) {
2255 /* wait for control status */
2258 } else if (!req->req.zero || len != ep->ep.maxpacket)
2262 /* there was nothing to do ... */
2263 } else if (mode == 1)
2268 /* stream endpoints often resubmit/unlink in completion */
2271 /* maybe advance queue to next request */
2273 /* NOTE: net2280 could let gadget driver start the
2274 * status stage later. since not all controllers let
2275 * them control that, the api doesn't (yet) allow it.
2281 if (!list_empty (&ep->queue) && !ep->stopped)
2282 req = list_entry (ep->queue.next,
2283 struct net2280_request, queue);
2286 if (req && !ep->is_in)
2287 stop_out_naking (ep);
2291 /* is there a buffer for the next packet?
2292 * for best streaming performance, make sure there is one.
2294 if (req && !ep->stopped) {
2296 /* load IN fifo with next packet (may be zlp) */
2297 if (t & (1 << DATA_PACKET_TRANSMITTED_INTERRUPT))
2298 write_fifo (ep, &req->req);
2302 static struct net2280_ep *
2303 get_ep_by_addr (struct net2280 *dev, u16 wIndex)
2305 struct net2280_ep *ep;
2307 if ((wIndex & USB_ENDPOINT_NUMBER_MASK) == 0)
2308 return &dev->ep [0];
2309 list_for_each_entry (ep, &dev->gadget.ep_list, ep.ep_list) {
2310 u8 bEndpointAddress;
2314 bEndpointAddress = ep->desc->bEndpointAddress;
2315 if ((wIndex ^ bEndpointAddress) & USB_DIR_IN)
2317 if ((wIndex & 0x0f) == (bEndpointAddress & 0x0f))
2323 static void handle_stat0_irqs (struct net2280 *dev, u32 stat)
2325 struct net2280_ep *ep;
2328 /* most of these don't need individual acks */
2329 stat &= ~(1 << INTA_ASSERTED);
2332 // DEBUG (dev, "irqstat0 %04x\n", stat);
2334 /* starting a control request? */
2335 if (unlikely (stat & (1 << SETUP_PACKET_INTERRUPT))) {
2338 struct usb_ctrlrequest r;
2341 struct net2280_request *req;
2343 if (dev->gadget.speed == USB_SPEED_UNKNOWN) {
2344 if (readl (&dev->usb->usbstat) & (1 << HIGH_SPEED))
2345 dev->gadget.speed = USB_SPEED_HIGH;
2347 dev->gadget.speed = USB_SPEED_FULL;
2348 net2280_led_speed (dev, dev->gadget.speed);
2349 DEBUG (dev, "%s speed\n",
2350 (dev->gadget.speed == USB_SPEED_HIGH)
2357 /* make sure any leftover request state is cleared */
2358 stat &= ~(1 << ENDPOINT_0_INTERRUPT);
2359 while (!list_empty (&ep->queue)) {
2360 req = list_entry (ep->queue.next,
2361 struct net2280_request, queue);
2362 done (ep, req, (req->req.actual == req->req.length)
2366 dev->protocol_stall = 0;
2367 writel ( (1 << TIMEOUT)
2368 | (1 << USB_STALL_SENT)
2369 | (1 << USB_IN_NAK_SENT)
2370 | (1 << USB_IN_ACK_RCVD)
2371 | (1 << USB_OUT_PING_NAK_SENT)
2372 | (1 << USB_OUT_ACK_SENT)
2373 | (1 << FIFO_OVERFLOW)
2374 | (1 << FIFO_UNDERFLOW)
2375 | (1 << SHORT_PACKET_OUT_DONE_INTERRUPT)
2376 | (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)
2377 | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
2378 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
2379 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
2380 | (1 << DATA_IN_TOKEN_INTERRUPT)
2381 , &ep->regs->ep_stat);
2382 u.raw [0] = readl (&dev->usb->setup0123);
2383 u.raw [1] = readl (&dev->usb->setup4567);
2385 cpu_to_le32s (&u.raw [0]);
2386 cpu_to_le32s (&u.raw [1]);
2388 #define w_value le16_to_cpup (&u.r.wValue)
2389 #define w_index le16_to_cpup (&u.r.wIndex)
2390 #define w_length le16_to_cpup (&u.r.wLength)
2393 writel (1 << SETUP_PACKET_INTERRUPT, &dev->regs->irqstat0);
2394 stat ^= (1 << SETUP_PACKET_INTERRUPT);
2396 /* watch control traffic at the token level, and force
2397 * synchronization before letting the status stage happen.
2398 * FIXME ignore tokens we'll NAK, until driver responds.
2399 * that'll mean a lot less irqs for some drivers.
2401 ep->is_in = (u.r.bRequestType & USB_DIR_IN) != 0;
2403 scratch = (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
2404 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
2405 | (1 << DATA_IN_TOKEN_INTERRUPT);
2406 stop_out_naking (ep);
2408 scratch = (1 << DATA_PACKET_RECEIVED_INTERRUPT)
2409 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
2410 | (1 << DATA_IN_TOKEN_INTERRUPT);
2411 writel (scratch, &dev->epregs [0].ep_irqenb);
2413 /* we made the hardware handle most lowlevel requests;
2414 * everything else goes uplevel to the gadget code.
2416 switch (u.r.bRequest) {
2417 case USB_REQ_GET_STATUS: {
2418 struct net2280_ep *e;
2421 /* hw handles device and interface status */
2422 if (u.r.bRequestType != (USB_DIR_IN|USB_RECIP_ENDPOINT))
2424 if ((e = get_ep_by_addr (dev, w_index)) == 0
2428 if (readl (&e->regs->ep_rsp)
2429 & (1 << SET_ENDPOINT_HALT))
2430 status = __constant_cpu_to_le32 (1);
2432 status = __constant_cpu_to_le32 (0);
2434 /* don't bother with a request object! */
2435 writel (0, &dev->epregs [0].ep_irqenb);
2436 set_fifo_bytecount (ep, w_length);
2437 writel ((__force u32)status, &dev->epregs [0].ep_data);
2439 VDEBUG (dev, "%s stat %02x\n", ep->ep.name, status);
2440 goto next_endpoints;
2443 case USB_REQ_CLEAR_FEATURE: {
2444 struct net2280_ep *e;
2446 /* hw handles device features */
2447 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
2449 if (w_value != USB_ENDPOINT_HALT
2452 if ((e = get_ep_by_addr (dev, w_index)) == 0)
2456 VDEBUG (dev, "%s clear halt\n", ep->ep.name);
2457 goto next_endpoints;
2460 case USB_REQ_SET_FEATURE: {
2461 struct net2280_ep *e;
2463 /* hw handles device features */
2464 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
2466 if (w_value != USB_ENDPOINT_HALT
2469 if ((e = get_ep_by_addr (dev, w_index)) == 0)
2473 VDEBUG (dev, "%s set halt\n", ep->ep.name);
2474 goto next_endpoints;
2479 VDEBUG (dev, "setup %02x.%02x v%04x i%04x l%04x"
2481 u.r.bRequestType, u.r.bRequest,
2482 w_value, w_index, w_length,
2483 readl (&ep->regs->ep_cfg));
2484 spin_unlock (&dev->lock);
2485 tmp = dev->driver->setup (&dev->gadget, &u.r);
2486 spin_lock (&dev->lock);
2489 /* stall ep0 on error */
2492 VDEBUG (dev, "req %02x.%02x protocol STALL; stat %d\n",
2493 u.r.bRequestType, u.r.bRequest, tmp);
2494 dev->protocol_stall = 1;
2497 /* some in/out token irq should follow; maybe stall then.
2498 * driver must queue a request (even zlp) or halt ep0
2499 * before the host times out.
2508 /* endpoint data irq ? */
2509 scratch = stat & 0x7f;
2511 for (num = 0; scratch; num++) {
2514 /* do this endpoint's FIFO and queue need tending? */
2516 if ((scratch & t) == 0)
2520 ep = &dev->ep [num];
2521 handle_ep_small (ep);
2525 DEBUG (dev, "unhandled irqstat0 %08x\n", stat);
2528 #define DMA_INTERRUPTS ( \
2529 (1 << DMA_D_INTERRUPT) \
2530 | (1 << DMA_C_INTERRUPT) \
2531 | (1 << DMA_B_INTERRUPT) \
2532 | (1 << DMA_A_INTERRUPT))
2533 #define PCI_ERROR_INTERRUPTS ( \
2534 (1 << PCI_MASTER_ABORT_RECEIVED_INTERRUPT) \
2535 | (1 << PCI_TARGET_ABORT_RECEIVED_INTERRUPT) \
2536 | (1 << PCI_RETRY_ABORT_INTERRUPT))
2538 static void handle_stat1_irqs (struct net2280 *dev, u32 stat)
2540 struct net2280_ep *ep;
2541 u32 tmp, num, mask, scratch;
2543 /* after disconnect there's nothing else to do! */
2544 tmp = (1 << VBUS_INTERRUPT) | (1 << ROOT_PORT_RESET_INTERRUPT);
2545 mask = (1 << HIGH_SPEED) | (1 << FULL_SPEED);
2547 /* VBUS disconnect is indicated by VBUS_PIN and VBUS_INTERRUPT set.
2548 * Root Port Reset is indicated by ROOT_PORT_RESET_INTERRRUPT set and
2549 * both HIGH_SPEED and FULL_SPEED clear (as ROOT_PORT_RESET_INTERRUPT
2550 * only indicates a change in the reset state).
2553 writel (tmp, &dev->regs->irqstat1);
2554 if ((((stat & (1 << ROOT_PORT_RESET_INTERRUPT)) &&
2555 ((readl (&dev->usb->usbstat) & mask) == 0))
2556 || ((readl (&dev->usb->usbctl) & (1 << VBUS_PIN)) == 0)
2557 ) && ( dev->gadget.speed != USB_SPEED_UNKNOWN)) {
2558 DEBUG (dev, "disconnect %s\n",
2559 dev->driver->driver.name);
2560 stop_activity (dev, dev->driver);
2566 /* vBUS can bounce ... one of many reasons to ignore the
2567 * notion of hotplug events on bus connect/disconnect!
2573 /* NOTE: chip stays in PCI D0 state for now, but it could
2574 * enter D1 to save more power
2576 tmp = (1 << SUSPEND_REQUEST_CHANGE_INTERRUPT);
2578 writel (tmp, &dev->regs->irqstat1);
2579 if (stat & (1 << SUSPEND_REQUEST_INTERRUPT)) {
2580 if (dev->driver->suspend)
2581 dev->driver->suspend (&dev->gadget);
2582 if (!enable_suspend)
2583 stat &= ~(1 << SUSPEND_REQUEST_INTERRUPT);
2585 if (dev->driver->resume)
2586 dev->driver->resume (&dev->gadget);
2587 /* at high speed, note erratum 0133 */
2592 /* clear any other status/irqs */
2594 writel (stat, &dev->regs->irqstat1);
2596 /* some status we can just ignore */
2597 stat &= ~((1 << CONTROL_STATUS_INTERRUPT)
2598 | (1 << SUSPEND_REQUEST_INTERRUPT)
2599 | (1 << RESUME_INTERRUPT)
2600 | (1 << SOF_INTERRUPT));
2603 // DEBUG (dev, "irqstat1 %08x\n", stat);
2605 /* DMA status, for ep-{a,b,c,d} */
2606 scratch = stat & DMA_INTERRUPTS;
2607 stat &= ~DMA_INTERRUPTS;
2609 for (num = 0; scratch; num++) {
2610 struct net2280_dma_regs __iomem *dma;
2613 if ((tmp & scratch) == 0)
2617 ep = &dev->ep [num + 1];
2623 /* clear ep's dma status */
2624 tmp = readl (&dma->dmastat);
2625 writel (tmp, &dma->dmastat);
2627 /* chaining should stop on abort, short OUT from fifo,
2628 * or (stat0 codepath) short OUT transfer.
2630 if (!use_dma_chaining) {
2631 if ((tmp & (1 << DMA_TRANSACTION_DONE_INTERRUPT))
2633 DEBUG (ep->dev, "%s no xact done? %08x\n",
2640 /* OUT transfers terminate when the data from the
2641 * host is in our memory. Process whatever's done.
2642 * On this path, we know transfer's last packet wasn't
2643 * less than req->length. NAK_OUT_PACKETS may be set,
2644 * or the FIFO may already be holding new packets.
2646 * IN transfers can linger in the FIFO for a very
2647 * long time ... we ignore that for now, accounting
2648 * precisely (like PIO does) needs per-packet irqs
2650 scan_dma_completions (ep);
2652 /* disable dma on inactive queues; else maybe restart */
2653 if (list_empty (&ep->queue)) {
2654 if (use_dma_chaining)
2657 tmp = readl (&dma->dmactl);
2658 if (!use_dma_chaining
2659 || (tmp & (1 << DMA_ENABLE)) == 0)
2661 else if (ep->is_in && use_dma_chaining) {
2662 struct net2280_request *req;
2665 /* the descriptor at the head of the chain
2666 * may still have VALID_BIT clear; that's
2667 * used to trigger changing DMA_FIFO_VALIDATE
2668 * (affects automagic zlp writes).
2670 req = list_entry (ep->queue.next,
2671 struct net2280_request, queue);
2672 dmacount = req->td->dmacount;
2673 dmacount &= __constant_cpu_to_le32 (
2675 | DMA_BYTE_COUNT_MASK);
2676 if (dmacount && (dmacount & valid_bit) == 0)
2683 /* NOTE: there are other PCI errors we might usefully notice.
2684 * if they appear very often, here's where to try recovering.
2686 if (stat & PCI_ERROR_INTERRUPTS) {
2687 ERROR (dev, "pci dma error; stat %08x\n", stat);
2688 stat &= ~PCI_ERROR_INTERRUPTS;
2689 /* these are fatal errors, but "maybe" they won't
2692 stop_activity (dev, dev->driver);
2698 DEBUG (dev, "unhandled irqstat1 %08x\n", stat);
2701 static irqreturn_t net2280_irq (int irq, void *_dev, struct pt_regs * r)
2703 struct net2280 *dev = _dev;
2705 spin_lock (&dev->lock);
2707 /* handle disconnect, dma, and more */
2708 handle_stat1_irqs (dev, readl (&dev->regs->irqstat1));
2710 /* control requests and PIO */
2711 handle_stat0_irqs (dev, readl (&dev->regs->irqstat0));
2713 spin_unlock (&dev->lock);
2718 /*-------------------------------------------------------------------------*/
2720 static void gadget_release (struct device *_dev)
2722 struct net2280 *dev = dev_get_drvdata (_dev);
2727 /* tear down the binding between this driver and the pci device */
2729 static void net2280_remove (struct pci_dev *pdev)
2731 struct net2280 *dev = pci_get_drvdata (pdev);
2733 /* start with the driver above us */
2735 /* should have been done already by driver model core */
2736 WARN (dev, "pci remove, driver '%s' is still registered\n",
2737 dev->driver->driver.name);
2738 usb_gadget_unregister_driver (dev->driver);
2741 /* then clean up the resources we allocated during probe() */
2742 net2280_led_shutdown (dev);
2743 if (dev->requests) {
2745 for (i = 1; i < 5; i++) {
2746 if (!dev->ep [i].dummy)
2748 pci_pool_free (dev->requests, dev->ep [i].dummy,
2749 dev->ep [i].td_dma);
2751 pci_pool_destroy (dev->requests);
2754 free_irq (pdev->irq, dev);
2756 iounmap (dev->regs);
2758 release_mem_region (pci_resource_start (pdev, 0),
2759 pci_resource_len (pdev, 0));
2761 pci_disable_device (pdev);
2762 device_unregister (&dev->gadget.dev);
2763 device_remove_file (&pdev->dev, &dev_attr_registers);
2764 pci_set_drvdata (pdev, NULL);
2766 INFO (dev, "unbind\n");
2768 the_controller = NULL;
2771 /* wrap this driver around the specified device, but
2772 * don't respond over USB until a gadget driver binds to us.
2775 static int net2280_probe (struct pci_dev *pdev, const struct pci_device_id *id)
2777 struct net2280 *dev;
2778 unsigned long resource, len;
2779 void __iomem *base = NULL;
2781 char buf [8], *bufp;
2783 /* if you want to support more than one controller in a system,
2784 * usb_gadget_driver_{register,unregister}() must change.
2786 if (the_controller) {
2787 dev_warn (&pdev->dev, "ignoring\n");
2791 /* alloc, and start init */
2792 dev = kmalloc (sizeof *dev, SLAB_KERNEL);
2798 memset (dev, 0, sizeof *dev);
2799 spin_lock_init (&dev->lock);
2801 dev->gadget.ops = &net2280_ops;
2802 dev->gadget.is_dualspeed = 1;
2804 /* the "gadget" abstracts/virtualizes the controller */
2805 strcpy (dev->gadget.dev.bus_id, "gadget");
2806 dev->gadget.dev.parent = &pdev->dev;
2807 dev->gadget.dev.dma_mask = pdev->dev.dma_mask;
2808 dev->gadget.dev.release = gadget_release;
2809 dev->gadget.name = driver_name;
2811 /* now all the pci goodies ... */
2812 if (pci_enable_device (pdev) < 0) {
2818 /* BAR 0 holds all the registers
2819 * BAR 1 is 8051 memory; unused here (note erratum 0103)
2820 * BAR 2 is fifo memory; unused here
2822 resource = pci_resource_start (pdev, 0);
2823 len = pci_resource_len (pdev, 0);
2824 if (!request_mem_region (resource, len, driver_name)) {
2825 DEBUG (dev, "controller already in use\n");
2831 base = ioremap_nocache (resource, len);
2833 DEBUG (dev, "can't map memory\n");
2837 dev->regs = (struct net2280_regs __iomem *) base;
2838 dev->usb = (struct net2280_usb_regs __iomem *) (base + 0x0080);
2839 dev->pci = (struct net2280_pci_regs __iomem *) (base + 0x0100);
2840 dev->dma = (struct net2280_dma_regs __iomem *) (base + 0x0180);
2841 dev->dep = (struct net2280_dep_regs __iomem *) (base + 0x0200);
2842 dev->epregs = (struct net2280_ep_regs __iomem *) (base + 0x0300);
2844 /* put into initial config, link up all endpoints */
2845 writel (0, &dev->usb->usbctl);
2849 /* irq setup after old hardware is cleaned up */
2851 ERROR (dev, "No IRQ. Check PCI setup!\n");
2856 scnprintf (buf, sizeof buf, "%d", pdev->irq);
2859 bufp = __irq_itoa(pdev->irq);
2861 if (request_irq (pdev->irq, net2280_irq, SA_SHIRQ, driver_name, dev)
2863 ERROR (dev, "request interrupt %s failed\n", bufp);
2870 /* NOTE: we know only the 32 LSBs of dma addresses may be nonzero */
2871 dev->requests = pci_pool_create ("requests", pdev,
2872 sizeof (struct net2280_dma),
2873 0 /* no alignment requirements */,
2874 0 /* or page-crossing issues */);
2875 if (!dev->requests) {
2876 DEBUG (dev, "can't get request pool\n");
2880 for (i = 1; i < 5; i++) {
2881 struct net2280_dma *td;
2883 td = pci_pool_alloc (dev->requests, GFP_KERNEL,
2884 &dev->ep [i].td_dma);
2886 DEBUG (dev, "can't get dummy %d\n", i);
2890 td->dmacount = 0; /* not VALID */
2891 td->dmaaddr = __constant_cpu_to_le32 (DMA_ADDR_INVALID);
2892 td->dmadesc = td->dmaaddr;
2893 dev->ep [i].dummy = td;
2896 /* enable lower-overhead pci memory bursts during DMA */
2897 writel ( (1 << DMA_MEMORY_WRITE_AND_INVALIDATE_ENABLE)
2898 // 256 write retries may not be enough...
2899 // | (1 << PCI_RETRY_ABORT_ENABLE)
2900 | (1 << DMA_READ_MULTIPLE_ENABLE)
2901 | (1 << DMA_READ_LINE_ENABLE)
2902 , &dev->pci->pcimstctl);
2903 /* erratum 0115 shouldn't appear: Linux inits PCI_LATENCY_TIMER */
2904 pci_set_master (pdev);
2907 /* ... also flushes any posted pci writes */
2908 dev->chiprev = get_idx_reg (dev->regs, REG_CHIPREV) & 0xffff;
2911 pci_set_drvdata (pdev, dev);
2912 INFO (dev, "%s\n", driver_desc);
2913 INFO (dev, "irq %s, pci mem %p, chip rev %04x\n",
2914 bufp, base, dev->chiprev);
2915 INFO (dev, "version: " DRIVER_VERSION "; dma %s\n",
2917 ? (use_dma_chaining ? "chaining" : "enabled")
2919 the_controller = dev;
2921 device_register (&dev->gadget.dev);
2922 device_create_file (&pdev->dev, &dev_attr_registers);
2928 net2280_remove (pdev);
2933 /*-------------------------------------------------------------------------*/
2935 static struct pci_device_id pci_ids [] = { {
2936 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
2940 .subvendor = PCI_ANY_ID,
2941 .subdevice = PCI_ANY_ID,
2943 }, { /* end: all zeroes */ }
2945 MODULE_DEVICE_TABLE (pci, pci_ids);
2947 /* pci driver glue; this is a "new style" PCI driver module */
2948 static struct pci_driver net2280_pci_driver = {
2949 .name = (char *) driver_name,
2950 .id_table = pci_ids,
2952 .probe = net2280_probe,
2953 .remove = net2280_remove,
2955 /* FIXME add power management support */
2958 MODULE_DESCRIPTION (DRIVER_DESC);
2959 MODULE_AUTHOR ("David Brownell");
2960 MODULE_LICENSE ("GPL");
2962 static int __init init (void)
2965 use_dma_chaining = 0;
2966 return pci_register_driver (&net2280_pci_driver);
2970 static void __exit cleanup (void)
2972 pci_unregister_driver (&net2280_pci_driver);
2974 module_exit (cleanup);