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/dma-mapping.h>
51 #include <linux/kernel.h>
52 #include <linux/delay.h>
53 #include <linux/ioport.h>
54 #include <linux/sched.h>
55 #include <linux/slab.h>
56 #include <linux/smp_lock.h>
57 #include <linux/errno.h>
58 #include <linux/init.h>
59 #include <linux/timer.h>
60 #include <linux/list.h>
61 #include <linux/interrupt.h>
62 #include <linux/moduleparam.h>
63 #include <linux/device.h>
64 #include <linux/usb_ch9.h>
65 #include <linux/usb_gadget.h>
67 #include <asm/byteorder.h>
70 #include <asm/system.h>
71 #include <asm/unaligned.h>
74 #define DRIVER_DESC "PLX NET2280 USB Peripheral Controller"
75 #define DRIVER_VERSION "2005 Feb 03"
77 #define DMA_ADDR_INVALID (~(dma_addr_t)0)
78 #define EP_DONTUSE 13 /* nonzero */
80 #define USE_RDK_LEDS /* GPIO pins control three LEDs */
83 static const char driver_name [] = "net2280";
84 static const char driver_desc [] = DRIVER_DESC;
86 static const char ep0name [] = "ep0";
87 static const char *ep_name [] = {
89 "ep-a", "ep-b", "ep-c", "ep-d",
93 /* use_dma -- general goodness, fewer interrupts, less cpu load (vs PIO)
94 * use_dma_chaining -- dma descriptor queueing gives even more irq reduction
96 * The net2280 DMA engines are not tightly integrated with their FIFOs;
97 * not all cases are (yet) handled well in this driver or the silicon.
98 * Some gadget drivers work better with the dma support here than others.
99 * These two parameters let you use PIO or more aggressive DMA.
101 static int use_dma = 1;
102 static int use_dma_chaining = 0;
104 /* "modprobe net2280 use_dma=n" etc */
105 module_param (use_dma, bool, S_IRUGO);
106 module_param (use_dma_chaining, bool, S_IRUGO);
109 /* mode 0 == ep-{a,b,c,d} 1K fifo each
110 * mode 1 == ep-{a,b} 2K fifo each, ep-{c,d} unavailable
111 * mode 2 == ep-a 2K fifo, ep-{b,c} 1K each, ep-d unavailable
113 static ushort fifo_mode = 0;
115 /* "modprobe net2280 fifo_mode=1" etc */
116 module_param (fifo_mode, ushort, 0644);
118 /* enable_suspend -- When enabled, the driver will respond to
119 * USB suspend requests by powering down the NET2280. Otherwise,
120 * USB suspend requests will be ignored. This is acceptible for
121 * self-powered devices, and helps avoid some quirks.
123 static int enable_suspend = 0;
125 /* "modprobe net2280 enable_suspend=1" etc */
126 module_param (enable_suspend, bool, S_IRUGO);
129 #define DIR_STRING(bAddress) (((bAddress) & USB_DIR_IN) ? "in" : "out")
131 #if defined(CONFIG_USB_GADGET_DEBUG_FILES) || defined (DEBUG)
132 static char *type_string (u8 bmAttributes)
134 switch ((bmAttributes) & USB_ENDPOINT_XFERTYPE_MASK) {
135 case USB_ENDPOINT_XFER_BULK: return "bulk";
136 case USB_ENDPOINT_XFER_ISOC: return "iso";
137 case USB_ENDPOINT_XFER_INT: return "intr";
145 #define valid_bit __constant_cpu_to_le32 (1 << VALID_BIT)
146 #define dma_done_ie __constant_cpu_to_le32 (1 << DMA_DONE_INTERRUPT_ENABLE)
148 /*-------------------------------------------------------------------------*/
151 net2280_enable (struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
154 struct net2280_ep *ep;
158 ep = container_of (_ep, struct net2280_ep, ep);
159 if (!_ep || !desc || ep->desc || _ep->name == ep0name
160 || desc->bDescriptorType != USB_DT_ENDPOINT)
163 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
166 /* erratum 0119 workaround ties up an endpoint number */
167 if ((desc->bEndpointAddress & 0x0f) == EP_DONTUSE)
170 /* sanity check ep-e/ep-f since their fifos are small */
171 max = le16_to_cpu (desc->wMaxPacketSize) & 0x1fff;
172 if (ep->num > 4 && max > 64)
175 spin_lock_irqsave (&dev->lock, flags);
176 _ep->maxpacket = max & 0x7ff;
179 /* ep_reset() has already been called */
181 ep->out_overflow = 0;
183 /* set speed-dependent max packet; may kick in high bandwidth */
184 set_idx_reg (dev->regs, REG_EP_MAXPKT (dev, ep->num), max);
186 /* FIFO lines can't go to different packets. PIO is ok, so
187 * use it instead of troublesome (non-bulk) multi-packet DMA.
189 if (ep->dma && (max % 4) != 0 && use_dma_chaining) {
190 DEBUG (ep->dev, "%s, no dma for maxpacket %d\n",
191 ep->ep.name, ep->ep.maxpacket);
195 /* set type, direction, address; reset fifo counters */
196 writel ((1 << FIFO_FLUSH), &ep->regs->ep_stat);
197 tmp = (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK);
198 if (tmp == USB_ENDPOINT_XFER_INT) {
199 /* erratum 0105 workaround prevents hs NYET */
200 if (dev->chiprev == 0100
201 && dev->gadget.speed == USB_SPEED_HIGH
202 && !(desc->bEndpointAddress & USB_DIR_IN))
203 writel ((1 << CLEAR_NAK_OUT_PACKETS_MODE),
205 } else if (tmp == USB_ENDPOINT_XFER_BULK) {
206 /* catch some particularly blatant driver bugs */
207 if ((dev->gadget.speed == USB_SPEED_HIGH
209 || (dev->gadget.speed == USB_SPEED_FULL
211 spin_unlock_irqrestore (&dev->lock, flags);
215 ep->is_iso = (tmp == USB_ENDPOINT_XFER_ISOC) ? 1 : 0;
216 tmp <<= ENDPOINT_TYPE;
217 tmp |= desc->bEndpointAddress;
218 tmp |= (4 << ENDPOINT_BYTE_COUNT); /* default full fifo lines */
219 tmp |= 1 << ENDPOINT_ENABLE;
222 /* for OUT transfers, block the rx fifo until a read is posted */
223 ep->is_in = (tmp & USB_DIR_IN) != 0;
225 writel ((1 << SET_NAK_OUT_PACKETS), &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 | readl (&ep->regs->ep_irqenb);
237 writel (tmp, &ep->regs->ep_irqenb);
238 } else { /* dma, per-request */
239 tmp = (1 << (8 + ep->num)); /* completion */
240 tmp |= readl (&dev->regs->pciirqenb1);
241 writel (tmp, &dev->regs->pciirqenb1);
243 /* for short OUT transfers, dma completions can't
244 * advance the queue; do it pio-style, by hand.
245 * NOTE erratum 0112 workaround #2
247 if ((desc->bEndpointAddress & USB_DIR_IN) == 0) {
248 tmp = (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT_ENABLE);
249 writel (tmp, &ep->regs->ep_irqenb);
251 tmp = (1 << ep->num) | readl (&dev->regs->pciirqenb0);
252 writel (tmp, &dev->regs->pciirqenb0);
256 tmp = desc->bEndpointAddress;
257 DEBUG (dev, "enabled %s (ep%d%s-%s) %s max %04x\n",
258 _ep->name, tmp & 0x0f, DIR_STRING (tmp),
259 type_string (desc->bmAttributes),
260 ep->dma ? "dma" : "pio", max);
262 /* pci writes may still be posted */
263 spin_unlock_irqrestore (&dev->lock, flags);
267 static int handshake (u32 __iomem *ptr, u32 mask, u32 done, int usec)
272 result = readl (ptr);
273 if (result == ~(u32)0) /* "device unplugged" */
284 static struct usb_ep_ops net2280_ep_ops;
286 static void ep_reset (struct net2280_regs __iomem *regs, struct net2280_ep *ep)
291 INIT_LIST_HEAD (&ep->queue);
293 ep->ep.maxpacket = ~0;
294 ep->ep.ops = &net2280_ep_ops;
296 /* disable the dma, irqs, endpoint... */
298 writel (0, &ep->dma->dmactl);
299 writel ( (1 << DMA_SCATTER_GATHER_DONE_INTERRUPT)
300 | (1 << DMA_TRANSACTION_DONE_INTERRUPT)
302 , &ep->dma->dmastat);
304 tmp = readl (®s->pciirqenb0);
305 tmp &= ~(1 << ep->num);
306 writel (tmp, ®s->pciirqenb0);
308 tmp = readl (®s->pciirqenb1);
309 tmp &= ~(1 << (8 + ep->num)); /* completion */
310 writel (tmp, ®s->pciirqenb1);
312 writel (0, &ep->regs->ep_irqenb);
314 /* init to our chosen defaults, notably so that we NAK OUT
315 * packets until the driver queues a read (+note erratum 0112)
317 tmp = (1 << SET_NAK_OUT_PACKETS_MODE)
318 | (1 << SET_NAK_OUT_PACKETS)
319 | (1 << CLEAR_EP_HIDE_STATUS_PHASE)
320 | (1 << CLEAR_INTERRUPT_MODE);
323 tmp |= (1 << CLEAR_ENDPOINT_TOGGLE)
324 | (1 << CLEAR_ENDPOINT_HALT);
326 writel (tmp, &ep->regs->ep_rsp);
328 /* scrub most status bits, and flush any fifo state */
329 writel ( (1 << TIMEOUT)
330 | (1 << USB_STALL_SENT)
331 | (1 << USB_IN_NAK_SENT)
332 | (1 << USB_IN_ACK_RCVD)
333 | (1 << USB_OUT_PING_NAK_SENT)
334 | (1 << USB_OUT_ACK_SENT)
335 | (1 << FIFO_OVERFLOW)
336 | (1 << FIFO_UNDERFLOW)
338 | (1 << SHORT_PACKET_OUT_DONE_INTERRUPT)
339 | (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)
340 | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
341 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
342 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
343 | (1 << DATA_IN_TOKEN_INTERRUPT)
344 , &ep->regs->ep_stat);
346 /* fifo size is handled separately */
349 static void nuke (struct net2280_ep *);
351 static int net2280_disable (struct usb_ep *_ep)
353 struct net2280_ep *ep;
356 ep = container_of (_ep, struct net2280_ep, ep);
357 if (!_ep || !ep->desc || _ep->name == ep0name)
360 spin_lock_irqsave (&ep->dev->lock, flags);
362 ep_reset (ep->dev->regs, ep);
364 VDEBUG (ep->dev, "disabled %s %s\n",
365 ep->dma ? "dma" : "pio", _ep->name);
367 /* synch memory views with the device */
368 (void) readl (&ep->regs->ep_cfg);
370 if (use_dma && !ep->dma && ep->num >= 1 && ep->num <= 4)
371 ep->dma = &ep->dev->dma [ep->num - 1];
373 spin_unlock_irqrestore (&ep->dev->lock, flags);
377 /*-------------------------------------------------------------------------*/
379 static struct usb_request *
380 net2280_alloc_request (struct usb_ep *_ep, gfp_t gfp_flags)
382 struct net2280_ep *ep;
383 struct net2280_request *req;
387 ep = container_of (_ep, struct net2280_ep, ep);
389 req = kmalloc (sizeof *req, gfp_flags);
393 memset (req, 0, sizeof *req);
394 req->req.dma = DMA_ADDR_INVALID;
395 INIT_LIST_HEAD (&req->queue);
397 /* this dma descriptor may be swapped with the previous dummy */
399 struct net2280_dma *td;
401 td = pci_pool_alloc (ep->dev->requests, gfp_flags,
407 td->dmacount = 0; /* not VALID */
408 td->dmaaddr = __constant_cpu_to_le32 (DMA_ADDR_INVALID);
409 td->dmadesc = td->dmaaddr;
416 net2280_free_request (struct usb_ep *_ep, struct usb_request *_req)
418 struct net2280_ep *ep;
419 struct net2280_request *req;
421 ep = container_of (_ep, struct net2280_ep, ep);
425 req = container_of (_req, struct net2280_request, req);
426 WARN_ON (!list_empty (&req->queue));
428 pci_pool_free (ep->dev->requests, req->td, req->td_dma);
432 /*-------------------------------------------------------------------------*/
436 /* many common platforms have dma-coherent caches, which means that it's
437 * safe to use kmalloc() memory for all i/o buffers without using any
438 * cache flushing calls. (unless you're trying to share cache lines
439 * between dma and non-dma activities, which is a slow idea in any case.)
441 * other platforms need more care, with 2.5 having a moderately general
442 * solution (which falls down for allocations smaller than one page)
443 * that improves significantly on the 2.4 PCI allocators by removing
444 * the restriction that memory never be freed in_interrupt().
446 #if defined(CONFIG_X86)
449 #elif defined(CONFIG_PPC) && !defined(CONFIG_NOT_COHERENT_CACHE)
452 #elif defined(CONFIG_MIPS) && !defined(CONFIG_DMA_NONCOHERENT)
455 /* FIXME there are other cases, including an x86-64 one ... */
458 /* allocating buffers this way eliminates dma mapping overhead, which
459 * on some platforms will mean eliminating a per-io buffer copy. with
460 * some kinds of system caches, further tweaks may still be needed.
463 net2280_alloc_buffer (
471 struct net2280_ep *ep;
473 ep = container_of (_ep, struct net2280_ep, ep);
476 *dma = DMA_ADDR_INVALID;
478 #if defined(USE_KMALLOC)
479 retval = kmalloc(bytes, gfp_flags);
481 *dma = virt_to_phys(retval);
484 /* the main problem with this call is that it wastes memory
485 * on typical 1/N page allocations: it allocates 1-N pages.
487 #warning Using dma_alloc_coherent even with buffers smaller than a page.
488 retval = dma_alloc_coherent(&ep->dev->pdev->dev,
489 bytes, dma, gfp_flags);
491 retval = kmalloc(bytes, gfp_flags);
497 net2280_free_buffer (
503 /* free memory into the right allocator */
505 if (dma != DMA_ADDR_INVALID) {
506 struct net2280_ep *ep;
508 ep = container_of(_ep, struct net2280_ep, ep);
511 dma_free_coherent(&ep->dev->pdev->dev, bytes, buf, dma);
517 /*-------------------------------------------------------------------------*/
519 /* load a packet into the fifo we use for usb IN transfers.
520 * works for all endpoints.
522 * NOTE: pio with ep-a..ep-d could stuff multiple packets into the fifo
523 * at a time, but this code is simpler because it knows it only writes
524 * one packet. ep-a..ep-d should use dma instead.
527 write_fifo (struct net2280_ep *ep, struct usb_request *req)
529 struct net2280_ep_regs __iomem *regs = ep->regs;
532 unsigned count, total;
534 /* INVARIANT: fifo is currently empty. (testable) */
537 buf = req->buf + req->actual;
539 total = req->length - req->actual;
545 /* write just one packet at a time */
546 count = ep->ep.maxpacket;
547 if (count > total) /* min() cannot be used on a bitfield */
550 VDEBUG (ep->dev, "write %s fifo (IN) %d bytes%s req %p\n",
552 (count != ep->ep.maxpacket) ? " (short)" : "",
555 /* NOTE be careful if you try to align these. fifo lines
556 * should normally be full (4 bytes) and successive partial
557 * lines are ok only in certain cases.
559 tmp = get_unaligned ((u32 *)buf);
561 writel (tmp, ®s->ep_data);
566 /* last fifo entry is "short" unless we wrote a full packet.
567 * also explicitly validate last word in (periodic) transfers
568 * when maxpacket is not a multiple of 4 bytes.
570 if (count || total < ep->ep.maxpacket) {
571 tmp = count ? get_unaligned ((u32 *)buf) : count;
573 set_fifo_bytecount (ep, count & 0x03);
574 writel (tmp, ®s->ep_data);
577 /* pci writes may still be posted */
580 /* work around erratum 0106: PCI and USB race over the OUT fifo.
581 * caller guarantees chiprev 0100, out endpoint is NAKing, and
582 * there's no real data in the fifo.
584 * NOTE: also used in cases where that erratum doesn't apply:
585 * where the host wrote "too much" data to us.
587 static void out_flush (struct net2280_ep *ep)
592 ASSERT_OUT_NAKING (ep);
594 statp = &ep->regs->ep_stat;
595 writel ( (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
596 | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
598 writel ((1 << FIFO_FLUSH), statp);
601 if (tmp & (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
602 /* high speed did bulk NYET; fifo isn't filling */
603 && ep->dev->gadget.speed == USB_SPEED_FULL) {
606 usec = 50; /* 64 byte bulk/interrupt */
607 handshake (statp, (1 << USB_OUT_PING_NAK_SENT),
608 (1 << USB_OUT_PING_NAK_SENT), usec);
609 /* NAK done; now CLEAR_NAK_OUT_PACKETS is safe */
613 /* unload packet(s) from the fifo we use for usb OUT transfers.
614 * returns true iff the request completed, because of short packet
615 * or the request buffer having filled with full packets.
617 * for ep-a..ep-d this will read multiple packets out when they
618 * have been accepted.
621 read_fifo (struct net2280_ep *ep, struct net2280_request *req)
623 struct net2280_ep_regs __iomem *regs = ep->regs;
624 u8 *buf = req->req.buf + req->req.actual;
625 unsigned count, tmp, is_short;
626 unsigned cleanup = 0, prevent = 0;
628 /* erratum 0106 ... packets coming in during fifo reads might
629 * be incompletely rejected. not all cases have workarounds.
631 if (ep->dev->chiprev == 0x0100
632 && ep->dev->gadget.speed == USB_SPEED_FULL) {
634 tmp = readl (&ep->regs->ep_stat);
635 if ((tmp & (1 << NAK_OUT_PACKETS)))
637 else if ((tmp & (1 << FIFO_FULL))) {
638 start_out_naking (ep);
641 /* else: hope we don't see the problem */
644 /* never overflow the rx buffer. the fifo reads packets until
645 * it sees a short one; we might not be ready for them all.
648 count = readl (®s->ep_avail);
649 if (unlikely (count == 0)) {
651 tmp = readl (&ep->regs->ep_stat);
652 count = readl (®s->ep_avail);
653 /* handled that data already? */
654 if (count == 0 && (tmp & (1 << NAK_OUT_PACKETS)) == 0)
658 tmp = req->req.length - req->req.actual;
660 /* as with DMA, data overflow gets flushed */
661 if ((tmp % ep->ep.maxpacket) != 0) {
663 "%s out fifo %d bytes, expected %d\n",
664 ep->ep.name, count, tmp);
665 req->req.status = -EOVERFLOW;
667 /* NAK_OUT_PACKETS will be set, so flushing is safe;
668 * the next read will start with the next packet
670 } /* else it's a ZLP, no worries */
673 req->req.actual += count;
675 is_short = (count == 0) || ((count % ep->ep.maxpacket) != 0);
677 VDEBUG (ep->dev, "read %s fifo (OUT) %d bytes%s%s%s req %p %d/%d\n",
678 ep->ep.name, count, is_short ? " (short)" : "",
679 cleanup ? " flush" : "", prevent ? " nak" : "",
680 req, req->req.actual, req->req.length);
683 tmp = readl (®s->ep_data);
685 put_unaligned (tmp, (u32 *)buf);
690 tmp = readl (®s->ep_data);
691 /* LE conversion is implicit here: */
700 writel ((1 << CLEAR_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
701 (void) readl (&ep->regs->ep_rsp);
704 return is_short || ((req->req.actual == req->req.length)
708 /* fill out dma descriptor to match a given request */
710 fill_dma_desc (struct net2280_ep *ep, struct net2280_request *req, int valid)
712 struct net2280_dma *td = req->td;
713 u32 dmacount = req->req.length;
715 /* don't let DMA continue after a short OUT packet,
716 * so overruns can't affect the next transfer.
717 * in case of overruns on max-size packets, we can't
718 * stop the fifo from filling but we can flush it.
721 dmacount |= (1 << DMA_DIRECTION);
722 else if ((dmacount % ep->ep.maxpacket) != 0)
723 dmacount |= (1 << END_OF_CHAIN);
727 dmacount |= (1 << VALID_BIT);
728 if (likely(!req->req.no_interrupt || !use_dma_chaining))
729 dmacount |= (1 << DMA_DONE_INTERRUPT_ENABLE);
731 /* td->dmadesc = previously set by caller */
732 td->dmaaddr = cpu_to_le32 (req->req.dma);
734 /* 2280 may be polling VALID_BIT through ep->dma->dmadesc */
736 td->dmacount = cpu_to_le32p (&dmacount);
739 static const u32 dmactl_default =
740 (1 << DMA_SCATTER_GATHER_DONE_INTERRUPT)
741 | (1 << DMA_CLEAR_COUNT_ENABLE)
742 /* erratum 0116 workaround part 1 (use POLLING) */
743 | (POLL_100_USEC << DESCRIPTOR_POLLING_RATE)
744 | (1 << DMA_VALID_BIT_POLLING_ENABLE)
745 | (1 << DMA_VALID_BIT_ENABLE)
746 | (1 << DMA_SCATTER_GATHER_ENABLE)
747 /* erratum 0116 workaround part 2 (no AUTOSTART) */
750 static inline void spin_stop_dma (struct net2280_dma_regs __iomem *dma)
752 handshake (&dma->dmactl, (1 << DMA_ENABLE), 0, 50);
755 static inline void stop_dma (struct net2280_dma_regs __iomem *dma)
757 writel (readl (&dma->dmactl) & ~(1 << DMA_ENABLE), &dma->dmactl);
761 static void start_queue (struct net2280_ep *ep, u32 dmactl, u32 td_dma)
763 struct net2280_dma_regs __iomem *dma = ep->dma;
765 writel ((1 << VALID_BIT) | (ep->is_in << DMA_DIRECTION),
767 writel (readl (&dma->dmastat), &dma->dmastat);
769 writel (td_dma, &dma->dmadesc);
770 writel (dmactl, &dma->dmactl);
772 /* erratum 0116 workaround part 3: pci arbiter away from net2280 */
773 (void) readl (&ep->dev->pci->pcimstctl);
775 writel ((1 << DMA_START), &dma->dmastat);
778 stop_out_naking (ep);
781 static void start_dma (struct net2280_ep *ep, struct net2280_request *req)
784 struct net2280_dma_regs __iomem *dma = ep->dma;
786 /* FIXME can't use DMA for ZLPs */
788 /* on this path we "know" there's no dma active (yet) */
789 WARN_ON (readl (&dma->dmactl) & (1 << DMA_ENABLE));
790 writel (0, &ep->dma->dmactl);
792 /* previous OUT packet might have been short */
793 if (!ep->is_in && ((tmp = readl (&ep->regs->ep_stat))
794 & (1 << NAK_OUT_PACKETS)) != 0) {
795 writel ((1 << SHORT_PACKET_TRANSFERRED_INTERRUPT),
798 tmp = readl (&ep->regs->ep_avail);
800 writel (readl (&dma->dmastat), &dma->dmastat);
802 /* transfer all/some fifo data */
803 writel (req->req.dma, &dma->dmaaddr);
804 tmp = min (tmp, req->req.length);
806 /* dma irq, faking scatterlist status */
807 req->td->dmacount = cpu_to_le32 (req->req.length - tmp);
808 writel ((1 << DMA_DONE_INTERRUPT_ENABLE)
809 | tmp, &dma->dmacount);
810 req->td->dmadesc = 0;
813 writel ((1 << DMA_ENABLE), &dma->dmactl);
814 writel ((1 << DMA_START), &dma->dmastat);
819 tmp = dmactl_default;
821 /* force packet boundaries between dma requests, but prevent the
822 * controller from automagically writing a last "short" packet
823 * (zero length) unless the driver explicitly said to do that.
826 if (likely ((req->req.length % ep->ep.maxpacket) != 0
828 tmp |= (1 << DMA_FIFO_VALIDATE);
829 ep->in_fifo_validate = 1;
831 ep->in_fifo_validate = 0;
834 /* init req->td, pointing to the current dummy */
835 req->td->dmadesc = cpu_to_le32 (ep->td_dma);
836 fill_dma_desc (ep, req, 1);
838 if (!use_dma_chaining)
839 req->td->dmacount |= __constant_cpu_to_le32 (1 << END_OF_CHAIN);
841 start_queue (ep, tmp, req->td_dma);
845 queue_dma (struct net2280_ep *ep, struct net2280_request *req, int valid)
847 struct net2280_dma *end;
850 /* swap new dummy for old, link; fill and maybe activate */
856 ep->td_dma = req->td_dma;
859 end->dmadesc = cpu_to_le32 (ep->td_dma);
861 fill_dma_desc (ep, req, valid);
865 done (struct net2280_ep *ep, struct net2280_request *req, int status)
868 unsigned stopped = ep->stopped;
870 list_del_init (&req->queue);
872 if (req->req.status == -EINPROGRESS)
873 req->req.status = status;
875 status = req->req.status;
879 pci_unmap_single (dev->pdev, req->req.dma, req->req.length,
880 ep->is_in ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
881 req->req.dma = DMA_ADDR_INVALID;
885 if (status && status != -ESHUTDOWN)
886 VDEBUG (dev, "complete %s req %p stat %d len %u/%u\n",
887 ep->ep.name, &req->req, status,
888 req->req.actual, req->req.length);
890 /* don't modify queue heads during completion callback */
892 spin_unlock (&dev->lock);
893 req->req.complete (&ep->ep, &req->req);
894 spin_lock (&dev->lock);
895 ep->stopped = stopped;
898 /*-------------------------------------------------------------------------*/
901 net2280_queue (struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
903 struct net2280_request *req;
904 struct net2280_ep *ep;
908 /* we always require a cpu-view buffer, so that we can
909 * always use pio (as fallback or whatever).
911 req = container_of (_req, struct net2280_request, req);
912 if (!_req || !_req->complete || !_req->buf
913 || !list_empty (&req->queue))
915 if (_req->length > (~0 & DMA_BYTE_COUNT_MASK))
917 ep = container_of (_ep, struct net2280_ep, ep);
918 if (!_ep || (!ep->desc && ep->num != 0))
921 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
924 /* FIXME implement PIO fallback for ZLPs with DMA */
925 if (ep->dma && _req->length == 0)
928 /* set up dma mapping in case the caller didn't */
929 if (ep->dma && _req->dma == DMA_ADDR_INVALID) {
930 _req->dma = pci_map_single (dev->pdev, _req->buf, _req->length,
931 ep->is_in ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
936 VDEBUG (dev, "%s queue req %p, len %d buf %p\n",
937 _ep->name, _req, _req->length, _req->buf);
940 spin_lock_irqsave (&dev->lock, flags);
942 _req->status = -EINPROGRESS;
945 /* kickstart this i/o queue? */
946 if (list_empty (&ep->queue) && !ep->stopped) {
947 /* use DMA if the endpoint supports it, else pio */
951 /* maybe there's no control data, just status ack */
952 if (ep->num == 0 && _req->length == 0) {
955 VDEBUG (dev, "%s status ack\n", ep->ep.name);
959 /* PIO ... stuff the fifo, or unblock it. */
961 write_fifo (ep, _req);
962 else if (list_empty (&ep->queue)) {
965 /* OUT FIFO might have packet(s) buffered */
966 s = readl (&ep->regs->ep_stat);
967 if ((s & (1 << FIFO_EMPTY)) == 0) {
968 /* note: _req->short_not_ok is
969 * ignored here since PIO _always_
970 * stops queue advance here, and
971 * _req->status doesn't change for
972 * short reads (only _req->actual)
974 if (read_fifo (ep, req)) {
981 s = readl (&ep->regs->ep_stat);
984 /* don't NAK, let the fifo fill */
985 if (req && (s & (1 << NAK_OUT_PACKETS)))
986 writel ((1 << CLEAR_NAK_OUT_PACKETS),
991 } else if (ep->dma) {
997 /* preventing magic zlps is per-engine state, not
998 * per-transfer; irq logic must recover hiccups.
1000 expect = likely (req->req.zero
1001 || (req->req.length % ep->ep.maxpacket) != 0);
1002 if (expect != ep->in_fifo_validate)
1005 queue_dma (ep, req, valid);
1007 } /* else the irq handler advances the queue. */
1010 list_add_tail (&req->queue, &ep->queue);
1012 spin_unlock_irqrestore (&dev->lock, flags);
1014 /* pci writes may still be posted */
1020 struct net2280_ep *ep,
1021 struct net2280_request *req,
1026 req->req.actual = req->req.length - (DMA_BYTE_COUNT_MASK & dmacount);
1027 done (ep, req, status);
1030 static void restart_dma (struct net2280_ep *ep);
1032 static void scan_dma_completions (struct net2280_ep *ep)
1034 /* only look at descriptors that were "naturally" retired,
1035 * so fifo and list head state won't matter
1037 while (!list_empty (&ep->queue)) {
1038 struct net2280_request *req;
1041 req = list_entry (ep->queue.next,
1042 struct net2280_request, queue);
1046 tmp = le32_to_cpup (&req->td->dmacount);
1047 if ((tmp & (1 << VALID_BIT)) != 0)
1050 /* SHORT_PACKET_TRANSFERRED_INTERRUPT handles "usb-short"
1051 * cases where DMA must be aborted; this code handles
1052 * all non-abort DMA completions.
1054 if (unlikely (req->td->dmadesc == 0)) {
1056 tmp = readl (&ep->dma->dmacount);
1057 if (tmp & DMA_BYTE_COUNT_MASK)
1059 /* single transfer mode */
1060 dma_done (ep, req, tmp, 0);
1062 } else if (!ep->is_in
1063 && (req->req.length % ep->ep.maxpacket) != 0) {
1064 tmp = readl (&ep->regs->ep_stat);
1066 /* AVOID TROUBLE HERE by not issuing short reads from
1067 * your gadget driver. That helps avoids errata 0121,
1068 * 0122, and 0124; not all cases trigger the warning.
1070 if ((tmp & (1 << NAK_OUT_PACKETS)) == 0) {
1071 WARN (ep->dev, "%s lost packet sync!\n",
1073 req->req.status = -EOVERFLOW;
1074 } else if ((tmp = readl (&ep->regs->ep_avail)) != 0) {
1075 /* fifo gets flushed later */
1076 ep->out_overflow = 1;
1077 DEBUG (ep->dev, "%s dma, discard %d len %d\n",
1080 req->req.status = -EOVERFLOW;
1083 dma_done (ep, req, tmp, 0);
1087 static void restart_dma (struct net2280_ep *ep)
1089 struct net2280_request *req;
1090 u32 dmactl = dmactl_default;
1094 req = list_entry (ep->queue.next, struct net2280_request, queue);
1096 if (!use_dma_chaining) {
1097 start_dma (ep, req);
1101 /* the 2280 will be processing the queue unless queue hiccups after
1102 * the previous transfer:
1103 * IN: wanted automagic zlp, head doesn't (or vice versa)
1104 * DMA_FIFO_VALIDATE doesn't init from dma descriptors.
1105 * OUT: was "usb-short", we must restart.
1107 if (ep->is_in && !req->valid) {
1108 struct net2280_request *entry, *prev = NULL;
1109 int reqmode, done = 0;
1111 DEBUG (ep->dev, "%s dma hiccup td %p\n", ep->ep.name, req->td);
1112 ep->in_fifo_validate = likely (req->req.zero
1113 || (req->req.length % ep->ep.maxpacket) != 0);
1114 if (ep->in_fifo_validate)
1115 dmactl |= (1 << DMA_FIFO_VALIDATE);
1116 list_for_each_entry (entry, &ep->queue, queue) {
1121 dmacount = entry->td->dmacount;
1123 reqmode = likely (entry->req.zero
1124 || (entry->req.length
1125 % ep->ep.maxpacket) != 0);
1126 if (reqmode == ep->in_fifo_validate) {
1128 dmacount |= valid_bit;
1129 entry->td->dmacount = dmacount;
1133 /* force a hiccup */
1134 prev->td->dmacount |= dma_done_ie;
1139 /* walk the rest of the queue so unlinks behave */
1141 dmacount &= ~valid_bit;
1142 entry->td->dmacount = dmacount;
1147 writel (0, &ep->dma->dmactl);
1148 start_queue (ep, dmactl, req->td_dma);
1151 static void abort_dma (struct net2280_ep *ep)
1153 /* abort the current transfer */
1154 if (likely (!list_empty (&ep->queue))) {
1155 /* FIXME work around errata 0121, 0122, 0124 */
1156 writel ((1 << DMA_ABORT), &ep->dma->dmastat);
1157 spin_stop_dma (ep->dma);
1160 scan_dma_completions (ep);
1163 /* dequeue ALL requests */
1164 static void nuke (struct net2280_ep *ep)
1166 struct net2280_request *req;
1168 /* called with spinlock held */
1172 while (!list_empty (&ep->queue)) {
1173 req = list_entry (ep->queue.next,
1174 struct net2280_request,
1176 done (ep, req, -ESHUTDOWN);
1180 /* dequeue JUST ONE request */
1181 static int net2280_dequeue (struct usb_ep *_ep, struct usb_request *_req)
1183 struct net2280_ep *ep;
1184 struct net2280_request *req;
1185 unsigned long flags;
1189 ep = container_of (_ep, struct net2280_ep, ep);
1190 if (!_ep || (!ep->desc && ep->num != 0) || !_req)
1193 spin_lock_irqsave (&ep->dev->lock, flags);
1194 stopped = ep->stopped;
1196 /* quiesce dma while we patch the queue */
1200 dmactl = readl (&ep->dma->dmactl);
1201 /* WARNING erratum 0127 may kick in ... */
1203 scan_dma_completions (ep);
1206 /* make sure it's still queued on this endpoint */
1207 list_for_each_entry (req, &ep->queue, queue) {
1208 if (&req->req == _req)
1211 if (&req->req != _req) {
1212 spin_unlock_irqrestore (&ep->dev->lock, flags);
1216 /* queue head may be partially complete. */
1217 if (ep->queue.next == &req->queue) {
1219 DEBUG (ep->dev, "unlink (%s) dma\n", _ep->name);
1220 _req->status = -ECONNRESET;
1222 if (likely (ep->queue.next == &req->queue)) {
1223 // NOTE: misreports single-transfer mode
1224 req->td->dmacount = 0; /* invalidate */
1226 readl (&ep->dma->dmacount),
1230 DEBUG (ep->dev, "unlink (%s) pio\n", _ep->name);
1231 done (ep, req, -ECONNRESET);
1235 /* patch up hardware chaining data */
1236 } else if (ep->dma && use_dma_chaining) {
1237 if (req->queue.prev == ep->queue.next) {
1238 writel (le32_to_cpu (req->td->dmadesc),
1240 if (req->td->dmacount & dma_done_ie)
1241 writel (readl (&ep->dma->dmacount)
1242 | le32_to_cpu(dma_done_ie),
1243 &ep->dma->dmacount);
1245 struct net2280_request *prev;
1247 prev = list_entry (req->queue.prev,
1248 struct net2280_request, queue);
1249 prev->td->dmadesc = req->td->dmadesc;
1250 if (req->td->dmacount & dma_done_ie)
1251 prev->td->dmacount |= dma_done_ie;
1256 done (ep, req, -ECONNRESET);
1257 ep->stopped = stopped;
1260 /* turn off dma on inactive queues */
1261 if (list_empty (&ep->queue))
1263 else if (!ep->stopped) {
1264 /* resume current request, or start new one */
1266 writel (dmactl, &ep->dma->dmactl);
1268 start_dma (ep, list_entry (ep->queue.next,
1269 struct net2280_request, queue));
1273 spin_unlock_irqrestore (&ep->dev->lock, flags);
1277 /*-------------------------------------------------------------------------*/
1279 static int net2280_fifo_status (struct usb_ep *_ep);
1282 net2280_set_halt (struct usb_ep *_ep, int value)
1284 struct net2280_ep *ep;
1285 unsigned long flags;
1288 ep = container_of (_ep, struct net2280_ep, ep);
1289 if (!_ep || (!ep->desc && ep->num != 0))
1291 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1293 if (ep->desc /* not ep0 */ && (ep->desc->bmAttributes & 0x03)
1294 == USB_ENDPOINT_XFER_ISOC)
1297 spin_lock_irqsave (&ep->dev->lock, flags);
1298 if (!list_empty (&ep->queue))
1300 else if (ep->is_in && value && net2280_fifo_status (_ep) != 0)
1303 VDEBUG (ep->dev, "%s %s halt\n", _ep->name,
1304 value ? "set" : "clear");
1305 /* set/clear, then synch memory views with the device */
1308 ep->dev->protocol_stall = 1;
1313 (void) readl (&ep->regs->ep_rsp);
1315 spin_unlock_irqrestore (&ep->dev->lock, flags);
1321 net2280_fifo_status (struct usb_ep *_ep)
1323 struct net2280_ep *ep;
1326 ep = container_of (_ep, struct net2280_ep, ep);
1327 if (!_ep || (!ep->desc && ep->num != 0))
1329 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1332 avail = readl (&ep->regs->ep_avail) & ((1 << 12) - 1);
1333 if (avail > ep->fifo_size)
1336 avail = ep->fifo_size - avail;
1341 net2280_fifo_flush (struct usb_ep *_ep)
1343 struct net2280_ep *ep;
1345 ep = container_of (_ep, struct net2280_ep, ep);
1346 if (!_ep || (!ep->desc && ep->num != 0))
1348 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1351 writel ((1 << FIFO_FLUSH), &ep->regs->ep_stat);
1352 (void) readl (&ep->regs->ep_rsp);
1355 static struct usb_ep_ops net2280_ep_ops = {
1356 .enable = net2280_enable,
1357 .disable = net2280_disable,
1359 .alloc_request = net2280_alloc_request,
1360 .free_request = net2280_free_request,
1362 .alloc_buffer = net2280_alloc_buffer,
1363 .free_buffer = net2280_free_buffer,
1365 .queue = net2280_queue,
1366 .dequeue = net2280_dequeue,
1368 .set_halt = net2280_set_halt,
1369 .fifo_status = net2280_fifo_status,
1370 .fifo_flush = net2280_fifo_flush,
1373 /*-------------------------------------------------------------------------*/
1375 static int net2280_get_frame (struct usb_gadget *_gadget)
1377 struct net2280 *dev;
1378 unsigned long flags;
1383 dev = container_of (_gadget, struct net2280, gadget);
1384 spin_lock_irqsave (&dev->lock, flags);
1385 retval = get_idx_reg (dev->regs, REG_FRAME) & 0x03ff;
1386 spin_unlock_irqrestore (&dev->lock, flags);
1390 static int net2280_wakeup (struct usb_gadget *_gadget)
1392 struct net2280 *dev;
1394 unsigned long flags;
1398 dev = container_of (_gadget, struct net2280, gadget);
1400 spin_lock_irqsave (&dev->lock, flags);
1401 tmp = readl (&dev->usb->usbctl);
1402 if (tmp & (1 << DEVICE_REMOTE_WAKEUP_ENABLE))
1403 writel (1 << GENERATE_RESUME, &dev->usb->usbstat);
1404 spin_unlock_irqrestore (&dev->lock, flags);
1406 /* pci writes may still be posted */
1410 static int net2280_set_selfpowered (struct usb_gadget *_gadget, int value)
1412 struct net2280 *dev;
1414 unsigned long flags;
1418 dev = container_of (_gadget, struct net2280, gadget);
1420 spin_lock_irqsave (&dev->lock, flags);
1421 tmp = readl (&dev->usb->usbctl);
1423 tmp |= (1 << SELF_POWERED_STATUS);
1425 tmp &= ~(1 << SELF_POWERED_STATUS);
1426 writel (tmp, &dev->usb->usbctl);
1427 spin_unlock_irqrestore (&dev->lock, flags);
1432 static int net2280_pullup(struct usb_gadget *_gadget, int is_on)
1434 struct net2280 *dev;
1436 unsigned long flags;
1440 dev = container_of (_gadget, struct net2280, gadget);
1442 spin_lock_irqsave (&dev->lock, flags);
1443 tmp = readl (&dev->usb->usbctl);
1444 dev->softconnect = (is_on != 0);
1446 tmp |= (1 << USB_DETECT_ENABLE);
1448 tmp &= ~(1 << USB_DETECT_ENABLE);
1449 writel (tmp, &dev->usb->usbctl);
1450 spin_unlock_irqrestore (&dev->lock, flags);
1455 static const struct usb_gadget_ops net2280_ops = {
1456 .get_frame = net2280_get_frame,
1457 .wakeup = net2280_wakeup,
1458 .set_selfpowered = net2280_set_selfpowered,
1459 .pullup = net2280_pullup,
1462 /*-------------------------------------------------------------------------*/
1464 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1466 /* FIXME move these into procfs, and use seq_file.
1467 * Sysfs _still_ doesn't behave for arbitrarily sized files,
1468 * and also doesn't help products using this with 2.4 kernels.
1471 /* "function" sysfs attribute */
1473 show_function (struct device *_dev, struct device_attribute *attr, char *buf)
1475 struct net2280 *dev = dev_get_drvdata (_dev);
1478 || !dev->driver->function
1479 || strlen (dev->driver->function) > PAGE_SIZE)
1481 return scnprintf (buf, PAGE_SIZE, "%s\n", dev->driver->function);
1483 static DEVICE_ATTR (function, S_IRUGO, show_function, NULL);
1486 show_registers (struct device *_dev, struct device_attribute *attr, char *buf)
1488 struct net2280 *dev;
1491 unsigned long flags;
1496 dev = dev_get_drvdata (_dev);
1499 spin_lock_irqsave (&dev->lock, flags);
1502 s = dev->driver->driver.name;
1506 /* Main Control Registers */
1507 t = scnprintf (next, size, "%s version " DRIVER_VERSION
1508 ", chiprev %04x, dma %s\n\n"
1509 "devinit %03x fifoctl %08x gadget '%s'\n"
1510 "pci irqenb0 %02x irqenb1 %08x "
1511 "irqstat0 %04x irqstat1 %08x\n",
1512 driver_name, dev->chiprev,
1514 ? (use_dma_chaining ? "chaining" : "enabled")
1516 readl (&dev->regs->devinit),
1517 readl (&dev->regs->fifoctl),
1519 readl (&dev->regs->pciirqenb0),
1520 readl (&dev->regs->pciirqenb1),
1521 readl (&dev->regs->irqstat0),
1522 readl (&dev->regs->irqstat1));
1526 /* USB Control Registers */
1527 t1 = readl (&dev->usb->usbctl);
1528 t2 = readl (&dev->usb->usbstat);
1529 if (t1 & (1 << VBUS_PIN)) {
1530 if (t2 & (1 << HIGH_SPEED))
1532 else if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1536 /* full speed bit (6) not working?? */
1539 t = scnprintf (next, size,
1540 "stdrsp %08x usbctl %08x usbstat %08x "
1541 "addr 0x%02x (%s)\n",
1542 readl (&dev->usb->stdrsp), t1, t2,
1543 readl (&dev->usb->ouraddr), s);
1547 /* PCI Master Control Registers */
1549 /* DMA Control Registers */
1551 /* Configurable EP Control Registers */
1552 for (i = 0; i < 7; i++) {
1553 struct net2280_ep *ep;
1559 t1 = readl (&ep->regs->ep_cfg);
1560 t2 = readl (&ep->regs->ep_rsp) & 0xff;
1561 t = scnprintf (next, size,
1562 "\n%s\tcfg %05x rsp (%02x) %s%s%s%s%s%s%s%s"
1564 ep->ep.name, t1, t2,
1565 (t2 & (1 << CLEAR_NAK_OUT_PACKETS))
1567 (t2 & (1 << CLEAR_EP_HIDE_STATUS_PHASE))
1569 (t2 & (1 << CLEAR_EP_FORCE_CRC_ERROR))
1571 (t2 & (1 << CLEAR_INTERRUPT_MODE))
1572 ? "interrupt " : "",
1573 (t2 & (1<<CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE))
1575 (t2 & (1 << CLEAR_NAK_OUT_PACKETS_MODE))
1577 (t2 & (1 << CLEAR_ENDPOINT_TOGGLE))
1578 ? "DATA1 " : "DATA0 ",
1579 (t2 & (1 << CLEAR_ENDPOINT_HALT))
1581 readl (&ep->regs->ep_irqenb));
1585 t = scnprintf (next, size,
1586 "\tstat %08x avail %04x "
1588 readl (&ep->regs->ep_stat),
1589 readl (&ep->regs->ep_avail),
1590 t1 & 0x0f, DIR_STRING (t1),
1591 type_string (t1 >> 8),
1592 ep->stopped ? "*" : "");
1599 t = scnprintf (next, size,
1600 " dma\tctl %08x stat %08x count %08x\n"
1601 "\taddr %08x desc %08x\n",
1602 readl (&ep->dma->dmactl),
1603 readl (&ep->dma->dmastat),
1604 readl (&ep->dma->dmacount),
1605 readl (&ep->dma->dmaaddr),
1606 readl (&ep->dma->dmadesc));
1612 /* Indexed Registers */
1616 t = scnprintf (next, size, "\nirqs: ");
1619 for (i = 0; i < 7; i++) {
1620 struct net2280_ep *ep;
1625 t = scnprintf (next, size, " %s/%lu", ep->ep.name, ep->irqs);
1630 t = scnprintf (next, size, "\n");
1634 spin_unlock_irqrestore (&dev->lock, flags);
1636 return PAGE_SIZE - size;
1638 static DEVICE_ATTR (registers, S_IRUGO, show_registers, NULL);
1641 show_queues (struct device *_dev, struct device_attribute *attr, char *buf)
1643 struct net2280 *dev;
1646 unsigned long flags;
1649 dev = dev_get_drvdata (_dev);
1652 spin_lock_irqsave (&dev->lock, flags);
1654 for (i = 0; i < 7; i++) {
1655 struct net2280_ep *ep = &dev->ep [i];
1656 struct net2280_request *req;
1660 const struct usb_endpoint_descriptor *d;
1665 t = d->bEndpointAddress;
1666 t = scnprintf (next, size,
1667 "\n%s (ep%d%s-%s) max %04x %s fifo %d\n",
1668 ep->ep.name, t & USB_ENDPOINT_NUMBER_MASK,
1669 (t & USB_DIR_IN) ? "in" : "out",
1671 switch (d->bmAttributes & 0x03) {
1672 case USB_ENDPOINT_XFER_BULK:
1673 val = "bulk"; break;
1674 case USB_ENDPOINT_XFER_INT:
1675 val = "intr"; break;
1679 le16_to_cpu (d->wMaxPacketSize) & 0x1fff,
1680 ep->dma ? "dma" : "pio", ep->fifo_size
1682 } else /* ep0 should only have one transfer queued */
1683 t = scnprintf (next, size, "ep0 max 64 pio %s\n",
1684 ep->is_in ? "in" : "out");
1685 if (t <= 0 || t > size)
1690 if (list_empty (&ep->queue)) {
1691 t = scnprintf (next, size, "\t(nothing queued)\n");
1692 if (t <= 0 || t > size)
1698 list_for_each_entry (req, &ep->queue, queue) {
1699 if (ep->dma && req->td_dma == readl (&ep->dma->dmadesc))
1700 t = scnprintf (next, size,
1701 "\treq %p len %d/%d "
1702 "buf %p (dmacount %08x)\n",
1703 &req->req, req->req.actual,
1704 req->req.length, req->req.buf,
1705 readl (&ep->dma->dmacount));
1707 t = scnprintf (next, size,
1708 "\treq %p len %d/%d buf %p\n",
1709 &req->req, req->req.actual,
1710 req->req.length, req->req.buf);
1711 if (t <= 0 || t > size)
1717 struct net2280_dma *td;
1720 t = scnprintf (next, size, "\t td %08x "
1721 " count %08x buf %08x desc %08x\n",
1723 le32_to_cpu (td->dmacount),
1724 le32_to_cpu (td->dmaaddr),
1725 le32_to_cpu (td->dmadesc));
1726 if (t <= 0 || t > size)
1735 spin_unlock_irqrestore (&dev->lock, flags);
1736 return PAGE_SIZE - size;
1738 static DEVICE_ATTR (queues, S_IRUGO, show_queues, NULL);
1743 #define device_create_file(a,b) do {} while (0)
1744 #define device_remove_file device_create_file
1748 /*-------------------------------------------------------------------------*/
1750 /* another driver-specific mode might be a request type doing dma
1751 * to/from another device fifo instead of to/from memory.
1754 static void set_fifo_mode (struct net2280 *dev, int mode)
1756 /* keeping high bits preserves BAR2 */
1757 writel ((0xffff << PCI_BASE2_RANGE) | mode, &dev->regs->fifoctl);
1759 /* always ep-{a,b,e,f} ... maybe not ep-c or ep-d */
1760 INIT_LIST_HEAD (&dev->gadget.ep_list);
1761 list_add_tail (&dev->ep [1].ep.ep_list, &dev->gadget.ep_list);
1762 list_add_tail (&dev->ep [2].ep.ep_list, &dev->gadget.ep_list);
1765 list_add_tail (&dev->ep [3].ep.ep_list, &dev->gadget.ep_list);
1766 list_add_tail (&dev->ep [4].ep.ep_list, &dev->gadget.ep_list);
1767 dev->ep [1].fifo_size = dev->ep [2].fifo_size = 1024;
1770 dev->ep [1].fifo_size = dev->ep [2].fifo_size = 2048;
1773 list_add_tail (&dev->ep [3].ep.ep_list, &dev->gadget.ep_list);
1774 dev->ep [1].fifo_size = 2048;
1775 dev->ep [2].fifo_size = 1024;
1778 /* fifo sizes for ep0, ep-c, ep-d, ep-e, and ep-f never change */
1779 list_add_tail (&dev->ep [5].ep.ep_list, &dev->gadget.ep_list);
1780 list_add_tail (&dev->ep [6].ep.ep_list, &dev->gadget.ep_list);
1783 /* just declare this in any driver that really need it */
1784 extern int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode);
1787 * net2280_set_fifo_mode - change allocation of fifo buffers
1788 * @gadget: access to the net2280 device that will be updated
1789 * @mode: 0 for default, four 1kB buffers (ep-a through ep-d);
1790 * 1 for two 2kB buffers (ep-a and ep-b only);
1791 * 2 for one 2kB buffer (ep-a) and two 1kB ones (ep-b, ep-c).
1793 * returns zero on success, else negative errno. when this succeeds,
1794 * the contents of gadget->ep_list may have changed.
1796 * you may only call this function when endpoints a-d are all disabled.
1797 * use it whenever extra hardware buffering can help performance, such
1798 * as before enabling "high bandwidth" interrupt endpoints that use
1799 * maxpacket bigger than 512 (when double buffering would otherwise
1802 int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode)
1805 struct net2280 *dev;
1807 unsigned long flags;
1811 dev = container_of (gadget, struct net2280, gadget);
1813 spin_lock_irqsave (&dev->lock, flags);
1815 for (i = 1; i <= 4; i++)
1816 if (dev->ep [i].desc) {
1820 if (mode < 0 || mode > 2)
1823 set_fifo_mode (dev, mode);
1824 spin_unlock_irqrestore (&dev->lock, flags);
1828 DEBUG (dev, "fifo: ep-a 2K, ep-b 2K\n");
1830 DEBUG (dev, "fifo: ep-a 2K, ep-b 1K, ep-c 1K\n");
1831 /* else all are 1K */
1835 EXPORT_SYMBOL (net2280_set_fifo_mode);
1837 /*-------------------------------------------------------------------------*/
1839 /* keeping it simple:
1840 * - one bus driver, initted first;
1841 * - one function driver, initted second
1843 * most of the work to support multiple net2280 controllers would
1844 * be to associate this gadget driver (yes?) with all of them, or
1845 * perhaps to bind specific drivers to specific devices.
1848 static struct net2280 *the_controller;
1850 static void usb_reset (struct net2280 *dev)
1854 dev->gadget.speed = USB_SPEED_UNKNOWN;
1855 (void) readl (&dev->usb->usbctl);
1857 net2280_led_init (dev);
1859 /* disable automatic responses, and irqs */
1860 writel (0, &dev->usb->stdrsp);
1861 writel (0, &dev->regs->pciirqenb0);
1862 writel (0, &dev->regs->pciirqenb1);
1864 /* clear old dma and irq state */
1865 for (tmp = 0; tmp < 4; tmp++) {
1866 struct net2280_ep *ep = &dev->ep [tmp + 1];
1871 writel (~0, &dev->regs->irqstat0),
1872 writel (~(1 << SUSPEND_REQUEST_INTERRUPT), &dev->regs->irqstat1),
1874 /* reset, and enable pci */
1875 tmp = readl (&dev->regs->devinit)
1877 | (1 << FIFO_SOFT_RESET)
1878 | (1 << USB_SOFT_RESET)
1879 | (1 << M8051_RESET);
1880 writel (tmp, &dev->regs->devinit);
1882 /* standard fifo and endpoint allocations */
1883 set_fifo_mode (dev, (fifo_mode <= 2) ? fifo_mode : 0);
1886 static void usb_reinit (struct net2280 *dev)
1891 /* use_dma changes are ignored till next device re-init */
1894 /* basic endpoint init */
1895 for (tmp = 0; tmp < 7; tmp++) {
1896 struct net2280_ep *ep = &dev->ep [tmp];
1898 ep->ep.name = ep_name [tmp];
1902 if (tmp > 0 && tmp <= 4) {
1903 ep->fifo_size = 1024;
1905 ep->dma = &dev->dma [tmp - 1];
1908 ep->regs = &dev->epregs [tmp];
1909 ep_reset (dev->regs, ep);
1911 dev->ep [0].ep.maxpacket = 64;
1912 dev->ep [5].ep.maxpacket = 64;
1913 dev->ep [6].ep.maxpacket = 64;
1915 dev->gadget.ep0 = &dev->ep [0].ep;
1916 dev->ep [0].stopped = 0;
1917 INIT_LIST_HEAD (&dev->gadget.ep0->ep_list);
1919 /* we want to prevent lowlevel/insecure access from the USB host,
1920 * but erratum 0119 means this enable bit is ignored
1922 for (tmp = 0; tmp < 5; tmp++)
1923 writel (EP_DONTUSE, &dev->dep [tmp].dep_cfg);
1926 static void ep0_start (struct net2280 *dev)
1928 writel ( (1 << CLEAR_EP_HIDE_STATUS_PHASE)
1929 | (1 << CLEAR_NAK_OUT_PACKETS)
1930 | (1 << CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE)
1931 , &dev->epregs [0].ep_rsp);
1934 * hardware optionally handles a bunch of standard requests
1935 * that the API hides from drivers anyway. have it do so.
1936 * endpoint status/features are handled in software, to
1937 * help pass tests for some dubious behavior.
1939 writel ( (1 << SET_TEST_MODE)
1940 | (1 << SET_ADDRESS)
1941 | (1 << DEVICE_SET_CLEAR_DEVICE_REMOTE_WAKEUP)
1942 | (1 << GET_DEVICE_STATUS)
1943 | (1 << GET_INTERFACE_STATUS)
1944 , &dev->usb->stdrsp);
1945 writel ( (1 << USB_ROOT_PORT_WAKEUP_ENABLE)
1946 | (1 << SELF_POWERED_USB_DEVICE)
1947 | (1 << REMOTE_WAKEUP_SUPPORT)
1948 | (dev->softconnect << USB_DETECT_ENABLE)
1949 | (1 << SELF_POWERED_STATUS)
1950 , &dev->usb->usbctl);
1952 /* enable irqs so we can see ep0 and general operation */
1953 writel ( (1 << SETUP_PACKET_INTERRUPT_ENABLE)
1954 | (1 << ENDPOINT_0_INTERRUPT_ENABLE)
1955 , &dev->regs->pciirqenb0);
1956 writel ( (1 << PCI_INTERRUPT_ENABLE)
1957 | (1 << PCI_MASTER_ABORT_RECEIVED_INTERRUPT_ENABLE)
1958 | (1 << PCI_TARGET_ABORT_RECEIVED_INTERRUPT_ENABLE)
1959 | (1 << PCI_RETRY_ABORT_INTERRUPT_ENABLE)
1960 | (1 << VBUS_INTERRUPT_ENABLE)
1961 | (1 << ROOT_PORT_RESET_INTERRUPT_ENABLE)
1962 | (1 << SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE)
1963 , &dev->regs->pciirqenb1);
1965 /* don't leave any writes posted */
1966 (void) readl (&dev->usb->usbctl);
1969 /* when a driver is successfully registered, it will receive
1970 * control requests including set_configuration(), which enables
1971 * non-control requests. then usb traffic follows until a
1972 * disconnect is reported. then a host may connect again, or
1973 * the driver might get unbound.
1975 int usb_gadget_register_driver (struct usb_gadget_driver *driver)
1977 struct net2280 *dev = the_controller;
1981 /* insist on high speed support from the driver, since
1982 * (dev->usb->xcvrdiag & FORCE_FULL_SPEED_MODE)
1983 * "must not be used in normal operation"
1986 || driver->speed != USB_SPEED_HIGH
1996 for (i = 0; i < 7; i++)
1997 dev->ep [i].irqs = 0;
1999 /* hook up the driver ... */
2000 dev->softconnect = 1;
2001 driver->driver.bus = NULL;
2002 dev->driver = driver;
2003 dev->gadget.dev.driver = &driver->driver;
2004 retval = driver->bind (&dev->gadget);
2006 DEBUG (dev, "bind to driver %s --> %d\n",
2007 driver->driver.name, retval);
2009 dev->gadget.dev.driver = NULL;
2013 device_create_file (&dev->pdev->dev, &dev_attr_function);
2014 device_create_file (&dev->pdev->dev, &dev_attr_queues);
2016 /* ... then enable host detection and ep0; and we're ready
2017 * for set_configuration as well as eventual disconnect.
2019 net2280_led_active (dev, 1);
2022 DEBUG (dev, "%s ready, usbctl %08x stdrsp %08x\n",
2023 driver->driver.name,
2024 readl (&dev->usb->usbctl),
2025 readl (&dev->usb->stdrsp));
2027 /* pci writes may still be posted */
2030 EXPORT_SYMBOL (usb_gadget_register_driver);
2033 stop_activity (struct net2280 *dev, struct usb_gadget_driver *driver)
2037 /* don't disconnect if it's not connected */
2038 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
2041 /* stop hardware; prevent new request submissions;
2042 * and kill any outstanding requests.
2045 for (i = 0; i < 7; i++)
2046 nuke (&dev->ep [i]);
2048 /* report disconnect; the driver is already quiesced */
2050 spin_unlock (&dev->lock);
2051 driver->disconnect (&dev->gadget);
2052 spin_lock (&dev->lock);
2058 int usb_gadget_unregister_driver (struct usb_gadget_driver *driver)
2060 struct net2280 *dev = the_controller;
2061 unsigned long flags;
2065 if (!driver || driver != dev->driver)
2068 spin_lock_irqsave (&dev->lock, flags);
2069 stop_activity (dev, driver);
2070 spin_unlock_irqrestore (&dev->lock, flags);
2072 net2280_pullup (&dev->gadget, 0);
2074 driver->unbind (&dev->gadget);
2075 dev->gadget.dev.driver = NULL;
2078 net2280_led_active (dev, 0);
2079 device_remove_file (&dev->pdev->dev, &dev_attr_function);
2080 device_remove_file (&dev->pdev->dev, &dev_attr_queues);
2082 DEBUG (dev, "unregistered driver '%s'\n", driver->driver.name);
2085 EXPORT_SYMBOL (usb_gadget_unregister_driver);
2088 /*-------------------------------------------------------------------------*/
2090 /* handle ep0, ep-e, ep-f with 64 byte packets: packet per irq.
2091 * also works for dma-capable endpoints, in pio mode or just
2092 * to manually advance the queue after short OUT transfers.
2094 static void handle_ep_small (struct net2280_ep *ep)
2096 struct net2280_request *req;
2098 /* 0 error, 1 mid-data, 2 done */
2101 if (!list_empty (&ep->queue))
2102 req = list_entry (ep->queue.next,
2103 struct net2280_request, queue);
2107 /* ack all, and handle what we care about */
2108 t = readl (&ep->regs->ep_stat);
2111 VDEBUG (ep->dev, "%s ack ep_stat %08x, req %p\n",
2112 ep->ep.name, t, req ? &req->req : 0);
2114 writel (t & ~(1 << NAK_OUT_PACKETS), &ep->regs->ep_stat);
2116 /* for ep0, monitor token irqs to catch data stage length errors
2117 * and to synchronize on status.
2119 * also, to defer reporting of protocol stalls ... here's where
2120 * data or status first appears, handling stalls here should never
2121 * cause trouble on the host side..
2123 * control requests could be slightly faster without token synch for
2124 * status, but status can jam up that way.
2126 if (unlikely (ep->num == 0)) {
2128 /* status; stop NAKing */
2129 if (t & (1 << DATA_OUT_PING_TOKEN_INTERRUPT)) {
2130 if (ep->dev->protocol_stall) {
2137 /* reply to extra IN data tokens with a zlp */
2138 } else if (t & (1 << DATA_IN_TOKEN_INTERRUPT)) {
2139 if (ep->dev->protocol_stall) {
2143 } else if (!req && ep->stopped)
2144 write_fifo (ep, NULL);
2147 /* status; stop NAKing */
2148 if (t & (1 << DATA_IN_TOKEN_INTERRUPT)) {
2149 if (ep->dev->protocol_stall) {
2154 /* an extra OUT token is an error */
2155 } else if (((t & (1 << DATA_OUT_PING_TOKEN_INTERRUPT))
2157 && req->req.actual == req->req.length)
2159 ep->dev->protocol_stall = 1;
2163 done (ep, req, -EOVERFLOW);
2169 if (unlikely (!req))
2172 /* manual DMA queue advance after short OUT */
2173 if (likely (ep->dma != 0)) {
2174 if (t & (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)) {
2176 int stopped = ep->stopped;
2178 /* TRANSFERRED works around OUT_DONE erratum 0112.
2179 * we expect (N <= maxpacket) bytes; host wrote M.
2180 * iff (M < N) we won't ever see a DMA interrupt.
2183 for (count = 0; ; t = readl (&ep->regs->ep_stat)) {
2185 /* any preceding dma transfers must finish.
2186 * dma handles (M >= N), may empty the queue
2188 scan_dma_completions (ep);
2189 if (unlikely (list_empty (&ep->queue)
2190 || ep->out_overflow)) {
2194 req = list_entry (ep->queue.next,
2195 struct net2280_request, queue);
2197 /* here either (M < N), a "real" short rx;
2198 * or (M == N) and the queue didn't empty
2200 if (likely (t & (1 << FIFO_EMPTY))) {
2201 count = readl (&ep->dma->dmacount);
2202 count &= DMA_BYTE_COUNT_MASK;
2203 if (readl (&ep->dma->dmadesc)
2211 /* stop DMA, leave ep NAKing */
2212 writel ((1 << DMA_ABORT), &ep->dma->dmastat);
2213 spin_stop_dma (ep->dma);
2216 req->td->dmacount = 0;
2217 t = readl (&ep->regs->ep_avail);
2218 dma_done (ep, req, count, t);
2221 /* also flush to prevent erratum 0106 trouble */
2222 if (unlikely (ep->out_overflow
2223 || (ep->dev->chiprev == 0x0100
2224 && ep->dev->gadget.speed
2225 == USB_SPEED_FULL))) {
2227 ep->out_overflow = 0;
2230 /* (re)start dma if needed, stop NAKing */
2231 ep->stopped = stopped;
2232 if (!list_empty (&ep->queue))
2235 DEBUG (ep->dev, "%s dma ep_stat %08x ??\n",
2239 /* data packet(s) received (in the fifo, OUT) */
2240 } else if (t & (1 << DATA_PACKET_RECEIVED_INTERRUPT)) {
2241 if (read_fifo (ep, req) && ep->num != 0)
2244 /* data packet(s) transmitted (IN) */
2245 } else if (t & (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)) {
2248 len = req->req.length - req->req.actual;
2249 if (len > ep->ep.maxpacket)
2250 len = ep->ep.maxpacket;
2251 req->req.actual += len;
2253 /* if we wrote it all, we're usually done */
2254 if (req->req.actual == req->req.length) {
2256 /* wait for control status */
2259 } else if (!req->req.zero || len != ep->ep.maxpacket)
2263 /* there was nothing to do ... */
2264 } else if (mode == 1)
2269 /* stream endpoints often resubmit/unlink in completion */
2272 /* maybe advance queue to next request */
2274 /* NOTE: net2280 could let gadget driver start the
2275 * status stage later. since not all controllers let
2276 * them control that, the api doesn't (yet) allow it.
2282 if (!list_empty (&ep->queue) && !ep->stopped)
2283 req = list_entry (ep->queue.next,
2284 struct net2280_request, queue);
2287 if (req && !ep->is_in)
2288 stop_out_naking (ep);
2292 /* is there a buffer for the next packet?
2293 * for best streaming performance, make sure there is one.
2295 if (req && !ep->stopped) {
2297 /* load IN fifo with next packet (may be zlp) */
2298 if (t & (1 << DATA_PACKET_TRANSMITTED_INTERRUPT))
2299 write_fifo (ep, &req->req);
2303 static struct net2280_ep *
2304 get_ep_by_addr (struct net2280 *dev, u16 wIndex)
2306 struct net2280_ep *ep;
2308 if ((wIndex & USB_ENDPOINT_NUMBER_MASK) == 0)
2309 return &dev->ep [0];
2310 list_for_each_entry (ep, &dev->gadget.ep_list, ep.ep_list) {
2311 u8 bEndpointAddress;
2315 bEndpointAddress = ep->desc->bEndpointAddress;
2316 if ((wIndex ^ bEndpointAddress) & USB_DIR_IN)
2318 if ((wIndex & 0x0f) == (bEndpointAddress & 0x0f))
2324 static void handle_stat0_irqs (struct net2280 *dev, u32 stat)
2326 struct net2280_ep *ep;
2329 /* most of these don't need individual acks */
2330 stat &= ~(1 << INTA_ASSERTED);
2333 // DEBUG (dev, "irqstat0 %04x\n", stat);
2335 /* starting a control request? */
2336 if (unlikely (stat & (1 << SETUP_PACKET_INTERRUPT))) {
2339 struct usb_ctrlrequest r;
2342 struct net2280_request *req;
2344 if (dev->gadget.speed == USB_SPEED_UNKNOWN) {
2345 if (readl (&dev->usb->usbstat) & (1 << HIGH_SPEED))
2346 dev->gadget.speed = USB_SPEED_HIGH;
2348 dev->gadget.speed = USB_SPEED_FULL;
2349 net2280_led_speed (dev, dev->gadget.speed);
2350 DEBUG (dev, "%s speed\n",
2351 (dev->gadget.speed == USB_SPEED_HIGH)
2358 /* make sure any leftover request state is cleared */
2359 stat &= ~(1 << ENDPOINT_0_INTERRUPT);
2360 while (!list_empty (&ep->queue)) {
2361 req = list_entry (ep->queue.next,
2362 struct net2280_request, queue);
2363 done (ep, req, (req->req.actual == req->req.length)
2367 dev->protocol_stall = 0;
2368 writel ( (1 << TIMEOUT)
2369 | (1 << USB_STALL_SENT)
2370 | (1 << USB_IN_NAK_SENT)
2371 | (1 << USB_IN_ACK_RCVD)
2372 | (1 << USB_OUT_PING_NAK_SENT)
2373 | (1 << USB_OUT_ACK_SENT)
2374 | (1 << FIFO_OVERFLOW)
2375 | (1 << FIFO_UNDERFLOW)
2376 | (1 << SHORT_PACKET_OUT_DONE_INTERRUPT)
2377 | (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)
2378 | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
2379 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
2380 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
2381 | (1 << DATA_IN_TOKEN_INTERRUPT)
2382 , &ep->regs->ep_stat);
2383 u.raw [0] = readl (&dev->usb->setup0123);
2384 u.raw [1] = readl (&dev->usb->setup4567);
2386 cpu_to_le32s (&u.raw [0]);
2387 cpu_to_le32s (&u.raw [1]);
2389 #define w_value le16_to_cpup (&u.r.wValue)
2390 #define w_index le16_to_cpup (&u.r.wIndex)
2391 #define w_length le16_to_cpup (&u.r.wLength)
2394 writel (1 << SETUP_PACKET_INTERRUPT, &dev->regs->irqstat0);
2395 stat ^= (1 << SETUP_PACKET_INTERRUPT);
2397 /* watch control traffic at the token level, and force
2398 * synchronization before letting the status stage happen.
2399 * FIXME ignore tokens we'll NAK, until driver responds.
2400 * that'll mean a lot less irqs for some drivers.
2402 ep->is_in = (u.r.bRequestType & USB_DIR_IN) != 0;
2404 scratch = (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
2405 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
2406 | (1 << DATA_IN_TOKEN_INTERRUPT);
2407 stop_out_naking (ep);
2409 scratch = (1 << DATA_PACKET_RECEIVED_INTERRUPT)
2410 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
2411 | (1 << DATA_IN_TOKEN_INTERRUPT);
2412 writel (scratch, &dev->epregs [0].ep_irqenb);
2414 /* we made the hardware handle most lowlevel requests;
2415 * everything else goes uplevel to the gadget code.
2417 switch (u.r.bRequest) {
2418 case USB_REQ_GET_STATUS: {
2419 struct net2280_ep *e;
2422 /* hw handles device and interface status */
2423 if (u.r.bRequestType != (USB_DIR_IN|USB_RECIP_ENDPOINT))
2425 if ((e = get_ep_by_addr (dev, w_index)) == 0
2429 if (readl (&e->regs->ep_rsp)
2430 & (1 << SET_ENDPOINT_HALT))
2431 status = __constant_cpu_to_le32 (1);
2433 status = __constant_cpu_to_le32 (0);
2435 /* don't bother with a request object! */
2436 writel (0, &dev->epregs [0].ep_irqenb);
2437 set_fifo_bytecount (ep, w_length);
2438 writel ((__force u32)status, &dev->epregs [0].ep_data);
2440 VDEBUG (dev, "%s stat %02x\n", ep->ep.name, status);
2441 goto next_endpoints;
2444 case USB_REQ_CLEAR_FEATURE: {
2445 struct net2280_ep *e;
2447 /* hw handles device features */
2448 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
2450 if (w_value != USB_ENDPOINT_HALT
2453 if ((e = get_ep_by_addr (dev, w_index)) == 0)
2457 VDEBUG (dev, "%s clear halt\n", ep->ep.name);
2458 goto next_endpoints;
2461 case USB_REQ_SET_FEATURE: {
2462 struct net2280_ep *e;
2464 /* hw handles device features */
2465 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
2467 if (w_value != USB_ENDPOINT_HALT
2470 if ((e = get_ep_by_addr (dev, w_index)) == 0)
2474 VDEBUG (dev, "%s set halt\n", ep->ep.name);
2475 goto next_endpoints;
2480 VDEBUG (dev, "setup %02x.%02x v%04x i%04x l%04x"
2482 u.r.bRequestType, u.r.bRequest,
2483 w_value, w_index, w_length,
2484 readl (&ep->regs->ep_cfg));
2485 spin_unlock (&dev->lock);
2486 tmp = dev->driver->setup (&dev->gadget, &u.r);
2487 spin_lock (&dev->lock);
2490 /* stall ep0 on error */
2493 VDEBUG (dev, "req %02x.%02x protocol STALL; stat %d\n",
2494 u.r.bRequestType, u.r.bRequest, tmp);
2495 dev->protocol_stall = 1;
2498 /* some in/out token irq should follow; maybe stall then.
2499 * driver must queue a request (even zlp) or halt ep0
2500 * before the host times out.
2509 /* endpoint data irq ? */
2510 scratch = stat & 0x7f;
2512 for (num = 0; scratch; num++) {
2515 /* do this endpoint's FIFO and queue need tending? */
2517 if ((scratch & t) == 0)
2521 ep = &dev->ep [num];
2522 handle_ep_small (ep);
2526 DEBUG (dev, "unhandled irqstat0 %08x\n", stat);
2529 #define DMA_INTERRUPTS ( \
2530 (1 << DMA_D_INTERRUPT) \
2531 | (1 << DMA_C_INTERRUPT) \
2532 | (1 << DMA_B_INTERRUPT) \
2533 | (1 << DMA_A_INTERRUPT))
2534 #define PCI_ERROR_INTERRUPTS ( \
2535 (1 << PCI_MASTER_ABORT_RECEIVED_INTERRUPT) \
2536 | (1 << PCI_TARGET_ABORT_RECEIVED_INTERRUPT) \
2537 | (1 << PCI_RETRY_ABORT_INTERRUPT))
2539 static void handle_stat1_irqs (struct net2280 *dev, u32 stat)
2541 struct net2280_ep *ep;
2542 u32 tmp, num, mask, scratch;
2544 /* after disconnect there's nothing else to do! */
2545 tmp = (1 << VBUS_INTERRUPT) | (1 << ROOT_PORT_RESET_INTERRUPT);
2546 mask = (1 << HIGH_SPEED) | (1 << FULL_SPEED);
2548 /* VBUS disconnect is indicated by VBUS_PIN and VBUS_INTERRUPT set.
2549 * Root Port Reset is indicated by ROOT_PORT_RESET_INTERRRUPT set and
2550 * both HIGH_SPEED and FULL_SPEED clear (as ROOT_PORT_RESET_INTERRUPT
2551 * only indicates a change in the reset state).
2554 writel (tmp, &dev->regs->irqstat1);
2555 if ((((stat & (1 << ROOT_PORT_RESET_INTERRUPT)) &&
2556 ((readl (&dev->usb->usbstat) & mask) == 0))
2557 || ((readl (&dev->usb->usbctl) & (1 << VBUS_PIN)) == 0)
2558 ) && ( dev->gadget.speed != USB_SPEED_UNKNOWN)) {
2559 DEBUG (dev, "disconnect %s\n",
2560 dev->driver->driver.name);
2561 stop_activity (dev, dev->driver);
2567 /* vBUS can bounce ... one of many reasons to ignore the
2568 * notion of hotplug events on bus connect/disconnect!
2574 /* NOTE: chip stays in PCI D0 state for now, but it could
2575 * enter D1 to save more power
2577 tmp = (1 << SUSPEND_REQUEST_CHANGE_INTERRUPT);
2579 writel (tmp, &dev->regs->irqstat1);
2580 if (stat & (1 << SUSPEND_REQUEST_INTERRUPT)) {
2581 if (dev->driver->suspend)
2582 dev->driver->suspend (&dev->gadget);
2583 if (!enable_suspend)
2584 stat &= ~(1 << SUSPEND_REQUEST_INTERRUPT);
2586 if (dev->driver->resume)
2587 dev->driver->resume (&dev->gadget);
2588 /* at high speed, note erratum 0133 */
2593 /* clear any other status/irqs */
2595 writel (stat, &dev->regs->irqstat1);
2597 /* some status we can just ignore */
2598 stat &= ~((1 << CONTROL_STATUS_INTERRUPT)
2599 | (1 << SUSPEND_REQUEST_INTERRUPT)
2600 | (1 << RESUME_INTERRUPT)
2601 | (1 << SOF_INTERRUPT));
2604 // DEBUG (dev, "irqstat1 %08x\n", stat);
2606 /* DMA status, for ep-{a,b,c,d} */
2607 scratch = stat & DMA_INTERRUPTS;
2608 stat &= ~DMA_INTERRUPTS;
2610 for (num = 0; scratch; num++) {
2611 struct net2280_dma_regs __iomem *dma;
2614 if ((tmp & scratch) == 0)
2618 ep = &dev->ep [num + 1];
2624 /* clear ep's dma status */
2625 tmp = readl (&dma->dmastat);
2626 writel (tmp, &dma->dmastat);
2628 /* chaining should stop on abort, short OUT from fifo,
2629 * or (stat0 codepath) short OUT transfer.
2631 if (!use_dma_chaining) {
2632 if ((tmp & (1 << DMA_TRANSACTION_DONE_INTERRUPT))
2634 DEBUG (ep->dev, "%s no xact done? %08x\n",
2641 /* OUT transfers terminate when the data from the
2642 * host is in our memory. Process whatever's done.
2643 * On this path, we know transfer's last packet wasn't
2644 * less than req->length. NAK_OUT_PACKETS may be set,
2645 * or the FIFO may already be holding new packets.
2647 * IN transfers can linger in the FIFO for a very
2648 * long time ... we ignore that for now, accounting
2649 * precisely (like PIO does) needs per-packet irqs
2651 scan_dma_completions (ep);
2653 /* disable dma on inactive queues; else maybe restart */
2654 if (list_empty (&ep->queue)) {
2655 if (use_dma_chaining)
2658 tmp = readl (&dma->dmactl);
2659 if (!use_dma_chaining
2660 || (tmp & (1 << DMA_ENABLE)) == 0)
2662 else if (ep->is_in && use_dma_chaining) {
2663 struct net2280_request *req;
2666 /* the descriptor at the head of the chain
2667 * may still have VALID_BIT clear; that's
2668 * used to trigger changing DMA_FIFO_VALIDATE
2669 * (affects automagic zlp writes).
2671 req = list_entry (ep->queue.next,
2672 struct net2280_request, queue);
2673 dmacount = req->td->dmacount;
2674 dmacount &= __constant_cpu_to_le32 (
2676 | DMA_BYTE_COUNT_MASK);
2677 if (dmacount && (dmacount & valid_bit) == 0)
2684 /* NOTE: there are other PCI errors we might usefully notice.
2685 * if they appear very often, here's where to try recovering.
2687 if (stat & PCI_ERROR_INTERRUPTS) {
2688 ERROR (dev, "pci dma error; stat %08x\n", stat);
2689 stat &= ~PCI_ERROR_INTERRUPTS;
2690 /* these are fatal errors, but "maybe" they won't
2693 stop_activity (dev, dev->driver);
2699 DEBUG (dev, "unhandled irqstat1 %08x\n", stat);
2702 static irqreturn_t net2280_irq (int irq, void *_dev, struct pt_regs * r)
2704 struct net2280 *dev = _dev;
2706 spin_lock (&dev->lock);
2708 /* handle disconnect, dma, and more */
2709 handle_stat1_irqs (dev, readl (&dev->regs->irqstat1));
2711 /* control requests and PIO */
2712 handle_stat0_irqs (dev, readl (&dev->regs->irqstat0));
2714 spin_unlock (&dev->lock);
2719 /*-------------------------------------------------------------------------*/
2721 static void gadget_release (struct device *_dev)
2723 struct net2280 *dev = dev_get_drvdata (_dev);
2728 /* tear down the binding between this driver and the pci device */
2730 static void net2280_remove (struct pci_dev *pdev)
2732 struct net2280 *dev = pci_get_drvdata (pdev);
2734 /* start with the driver above us */
2736 /* should have been done already by driver model core */
2737 WARN (dev, "pci remove, driver '%s' is still registered\n",
2738 dev->driver->driver.name);
2739 usb_gadget_unregister_driver (dev->driver);
2742 /* then clean up the resources we allocated during probe() */
2743 net2280_led_shutdown (dev);
2744 if (dev->requests) {
2746 for (i = 1; i < 5; i++) {
2747 if (!dev->ep [i].dummy)
2749 pci_pool_free (dev->requests, dev->ep [i].dummy,
2750 dev->ep [i].td_dma);
2752 pci_pool_destroy (dev->requests);
2755 free_irq (pdev->irq, dev);
2757 iounmap (dev->regs);
2759 release_mem_region (pci_resource_start (pdev, 0),
2760 pci_resource_len (pdev, 0));
2762 pci_disable_device (pdev);
2763 device_unregister (&dev->gadget.dev);
2764 device_remove_file (&pdev->dev, &dev_attr_registers);
2765 pci_set_drvdata (pdev, NULL);
2767 INFO (dev, "unbind\n");
2769 the_controller = NULL;
2772 /* wrap this driver around the specified device, but
2773 * don't respond over USB until a gadget driver binds to us.
2776 static int net2280_probe (struct pci_dev *pdev, const struct pci_device_id *id)
2778 struct net2280 *dev;
2779 unsigned long resource, len;
2780 void __iomem *base = NULL;
2782 char buf [8], *bufp;
2784 /* if you want to support more than one controller in a system,
2785 * usb_gadget_driver_{register,unregister}() must change.
2787 if (the_controller) {
2788 dev_warn (&pdev->dev, "ignoring\n");
2792 /* alloc, and start init */
2793 dev = kmalloc (sizeof *dev, SLAB_KERNEL);
2799 memset (dev, 0, sizeof *dev);
2800 spin_lock_init (&dev->lock);
2802 dev->gadget.ops = &net2280_ops;
2803 dev->gadget.is_dualspeed = 1;
2805 /* the "gadget" abstracts/virtualizes the controller */
2806 strcpy (dev->gadget.dev.bus_id, "gadget");
2807 dev->gadget.dev.parent = &pdev->dev;
2808 dev->gadget.dev.dma_mask = pdev->dev.dma_mask;
2809 dev->gadget.dev.release = gadget_release;
2810 dev->gadget.name = driver_name;
2812 /* now all the pci goodies ... */
2813 if (pci_enable_device (pdev) < 0) {
2819 /* BAR 0 holds all the registers
2820 * BAR 1 is 8051 memory; unused here (note erratum 0103)
2821 * BAR 2 is fifo memory; unused here
2823 resource = pci_resource_start (pdev, 0);
2824 len = pci_resource_len (pdev, 0);
2825 if (!request_mem_region (resource, len, driver_name)) {
2826 DEBUG (dev, "controller already in use\n");
2832 base = ioremap_nocache (resource, len);
2834 DEBUG (dev, "can't map memory\n");
2838 dev->regs = (struct net2280_regs __iomem *) base;
2839 dev->usb = (struct net2280_usb_regs __iomem *) (base + 0x0080);
2840 dev->pci = (struct net2280_pci_regs __iomem *) (base + 0x0100);
2841 dev->dma = (struct net2280_dma_regs __iomem *) (base + 0x0180);
2842 dev->dep = (struct net2280_dep_regs __iomem *) (base + 0x0200);
2843 dev->epregs = (struct net2280_ep_regs __iomem *) (base + 0x0300);
2845 /* put into initial config, link up all endpoints */
2846 writel (0, &dev->usb->usbctl);
2850 /* irq setup after old hardware is cleaned up */
2852 ERROR (dev, "No IRQ. Check PCI setup!\n");
2857 scnprintf (buf, sizeof buf, "%d", pdev->irq);
2860 bufp = __irq_itoa(pdev->irq);
2862 if (request_irq (pdev->irq, net2280_irq, SA_SHIRQ, driver_name, dev)
2864 ERROR (dev, "request interrupt %s failed\n", bufp);
2871 /* NOTE: we know only the 32 LSBs of dma addresses may be nonzero */
2872 dev->requests = pci_pool_create ("requests", pdev,
2873 sizeof (struct net2280_dma),
2874 0 /* no alignment requirements */,
2875 0 /* or page-crossing issues */);
2876 if (!dev->requests) {
2877 DEBUG (dev, "can't get request pool\n");
2881 for (i = 1; i < 5; i++) {
2882 struct net2280_dma *td;
2884 td = pci_pool_alloc (dev->requests, GFP_KERNEL,
2885 &dev->ep [i].td_dma);
2887 DEBUG (dev, "can't get dummy %d\n", i);
2891 td->dmacount = 0; /* not VALID */
2892 td->dmaaddr = __constant_cpu_to_le32 (DMA_ADDR_INVALID);
2893 td->dmadesc = td->dmaaddr;
2894 dev->ep [i].dummy = td;
2897 /* enable lower-overhead pci memory bursts during DMA */
2898 writel ( (1 << DMA_MEMORY_WRITE_AND_INVALIDATE_ENABLE)
2899 // 256 write retries may not be enough...
2900 // | (1 << PCI_RETRY_ABORT_ENABLE)
2901 | (1 << DMA_READ_MULTIPLE_ENABLE)
2902 | (1 << DMA_READ_LINE_ENABLE)
2903 , &dev->pci->pcimstctl);
2904 /* erratum 0115 shouldn't appear: Linux inits PCI_LATENCY_TIMER */
2905 pci_set_master (pdev);
2908 /* ... also flushes any posted pci writes */
2909 dev->chiprev = get_idx_reg (dev->regs, REG_CHIPREV) & 0xffff;
2912 pci_set_drvdata (pdev, dev);
2913 INFO (dev, "%s\n", driver_desc);
2914 INFO (dev, "irq %s, pci mem %p, chip rev %04x\n",
2915 bufp, base, dev->chiprev);
2916 INFO (dev, "version: " DRIVER_VERSION "; dma %s\n",
2918 ? (use_dma_chaining ? "chaining" : "enabled")
2920 the_controller = dev;
2922 device_register (&dev->gadget.dev);
2923 device_create_file (&pdev->dev, &dev_attr_registers);
2929 net2280_remove (pdev);
2934 /*-------------------------------------------------------------------------*/
2936 static struct pci_device_id pci_ids [] = { {
2937 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
2941 .subvendor = PCI_ANY_ID,
2942 .subdevice = PCI_ANY_ID,
2944 }, { /* end: all zeroes */ }
2946 MODULE_DEVICE_TABLE (pci, pci_ids);
2948 /* pci driver glue; this is a "new style" PCI driver module */
2949 static struct pci_driver net2280_pci_driver = {
2950 .name = (char *) driver_name,
2951 .id_table = pci_ids,
2953 .probe = net2280_probe,
2954 .remove = net2280_remove,
2956 /* FIXME add power management support */
2959 MODULE_DESCRIPTION (DRIVER_DESC);
2960 MODULE_AUTHOR ("David Brownell");
2961 MODULE_LICENSE ("GPL");
2963 static int __init init (void)
2966 use_dma_chaining = 0;
2967 return pci_register_driver (&net2280_pci_driver);
2971 static void __exit cleanup (void)
2973 pci_unregister_driver (&net2280_pci_driver);
2975 module_exit (cleanup);