2 * Driver for the NetChip 2280 USB device controller.
3 * Specs and errata are available from <http://www.netchip.com>.
5 * NetChip Technology Inc. supported the development of this driver.
8 * CODE STATUS HIGHLIGHTS
10 * Used with a gadget driver like "zero.c" this enumerates fine to Windows
11 * or Linux hosts; handles disconnect, reconnect, and reset, for full or
12 * high speed operation; and passes USB-IF "chapter 9" tests.
14 * Handles standard stress loads from the Linux "usbtest" driver, with
15 * either DMA (default) or PIO (use_dma=n) used for ep-{a,b,c,d}. Testing
16 * with "ttcp" (and the "ether.c" driver) behaves nicely too.
18 * DMA is enabled by default. Drivers using transfer queues might use
19 * DMA chaining to remove IRQ latencies between transfers. (Except when
20 * short OUT transfers happen.) Drivers can use the req->no_interrupt
21 * hint to completely eliminate some IRQs, if a later IRQ is guaranteed
22 * and DMA chaining is enabled.
24 * Note that almost all the errata workarounds here are only needed for
25 * rev1 chips. Rev1a silicon (0110) fixes almost all of them.
29 * Copyright (C) 2003 David Brownell
30 * Copyright (C) 2003 NetChip Technologies
32 * This program is free software; you can redistribute it and/or modify
33 * it under the terms of the GNU General Public License as published by
34 * the Free Software Foundation; either version 2 of the License, or
35 * (at your option) any later version.
37 * This program is distributed in the hope that it will be useful,
38 * but WITHOUT ANY WARRANTY; without even the implied warranty of
39 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
40 * GNU General Public License for more details.
42 * You should have received a copy of the GNU General Public License
43 * along with this program; if not, write to the Free Software
44 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
47 #undef DEBUG /* messages on error and most fault paths */
48 #undef VERBOSE /* extra debug messages (success too) */
50 #include <linux/config.h>
51 #include <linux/module.h>
52 #include <linux/pci.h>
53 #include <linux/kernel.h>
54 #include <linux/delay.h>
55 #include <linux/ioport.h>
56 #include <linux/sched.h>
57 #include <linux/slab.h>
58 #include <linux/smp_lock.h>
59 #include <linux/errno.h>
60 #include <linux/init.h>
61 #include <linux/timer.h>
62 #include <linux/list.h>
63 #include <linux/interrupt.h>
65 #include <linux/usb_ch9.h>
66 #include <linux/usb_gadget.h>
68 #include <asm/byteorder.h>
71 #include <asm/system.h>
72 #include <asm/unaligned.h>
75 #define DRIVER_DESC "NetChip 2280 USB Peripheral Controller"
76 #define DRIVER_VERSION "2004 Jan 14"
78 #define DMA_ADDR_INVALID (~(dma_addr_t)0)
79 #define EP_DONTUSE 13 /* nonzero */
81 #define USE_RDK_LEDS /* GPIO pins control three LEDs */
84 static const char driver_name [] = "net2280";
85 static const char driver_desc [] = DRIVER_DESC;
87 static const char ep0name [] = "ep0";
88 static const char *ep_name [] = {
90 "ep-a", "ep-b", "ep-c", "ep-d",
94 /* use_dma -- general goodness, fewer interrupts, less cpu load (vs PIO)
95 * use_dma_chaining -- dma descriptor queueing gives even more irq reduction
97 * The net2280 DMA engines are not tightly integrated with their FIFOs;
98 * not all cases are (yet) handled well in this driver or the silicon.
99 * Some gadget drivers work better with the dma support here than others.
100 * These two parameters let you use PIO or more aggressive DMA.
102 static int use_dma = 1;
103 static int use_dma_chaining = 0;
105 MODULE_PARM (use_dma, "i");
106 MODULE_PARM_DESC (use_dma, "true to use dma controllers");
108 MODULE_PARM (use_dma_chaining, "i");
109 MODULE_PARM_DESC (use_dma_chaining, "true to use dma descriptor queues");
112 /* mode 0 == ep-{a,b,c,d} 1K fifo each
113 * mode 1 == ep-{a,b} 2K fifo each, ep-{c,d} unavailable
114 * mode 2 == ep-a 2K fifo, ep-{b,c} 1K each, ep-d unavailable
116 static ushort fifo_mode = 0;
118 MODULE_PARM (fifo_mode, "h");
119 MODULE_PARM_DESC (fifo_mode, "net2280 fifo mode");
122 #define DIR_STRING(bAddress) (((bAddress) & USB_DIR_IN) ? "in" : "out")
124 #if defined(USE_SYSFS_DEBUG_FILES) || defined (DEBUG)
125 static char *type_string (u8 bmAttributes)
127 switch ((bmAttributes) & USB_ENDPOINT_XFERTYPE_MASK) {
128 case USB_ENDPOINT_XFER_BULK: return "bulk";
129 case USB_ENDPOINT_XFER_ISOC: return "iso";
130 case USB_ENDPOINT_XFER_INT: return "intr";
138 #define valid_bit __constant_cpu_to_le32 (1 << VALID_BIT)
139 #define dma_done_ie __constant_cpu_to_le32 (1 << DMA_DONE_INTERRUPT_ENABLE)
141 /*-------------------------------------------------------------------------*/
144 net2280_enable (struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
147 struct net2280_ep *ep;
151 ep = container_of (_ep, struct net2280_ep, ep);
152 if (!_ep || !desc || ep->desc || _ep->name == ep0name
153 || desc->bDescriptorType != USB_DT_ENDPOINT)
156 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
159 /* erratum 0119 workaround ties up an endpoint number */
160 if ((desc->bEndpointAddress & 0x0f) == EP_DONTUSE)
163 /* sanity check ep-e/ep-f since their fifos are small */
164 max = le16_to_cpu (desc->wMaxPacketSize) & 0x1fff;
165 if (ep->num > 4 && max > 64)
168 spin_lock_irqsave (&dev->lock, flags);
169 _ep->maxpacket = max & 0x7ff;
172 /* ep_reset() has already been called */
174 ep->out_overflow = 0;
176 /* set speed-dependent max packet; may kick in high bandwidth */
177 set_idx_reg (dev->regs, REG_EP_MAXPKT (dev, ep->num), max);
179 /* FIFO lines can't go to different packets. PIO is ok, so
180 * use it instead of troublesome (non-bulk) multi-packet DMA.
182 if (ep->dma && (max % 4) != 0 && use_dma_chaining) {
183 DEBUG (ep->dev, "%s, no dma for maxpacket %d\n",
184 ep->ep.name, ep->ep.maxpacket);
188 /* set type, direction, address; reset fifo counters */
189 writel ((1 << FIFO_FLUSH), &ep->regs->ep_stat);
190 tmp = (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK);
191 if (tmp == USB_ENDPOINT_XFER_INT) {
192 /* erratum 0105 workaround prevents hs NYET */
193 if (dev->chiprev == 0100
194 && dev->gadget.speed == USB_SPEED_HIGH
195 && !(desc->bEndpointAddress & USB_DIR_IN))
196 writel ((1 << CLEAR_NAK_OUT_PACKETS_MODE),
198 } else if (tmp == USB_ENDPOINT_XFER_BULK) {
199 /* catch some particularly blatant driver bugs */
200 if ((dev->gadget.speed == USB_SPEED_HIGH
202 || (dev->gadget.speed == USB_SPEED_FULL
204 spin_unlock_irqrestore (&dev->lock, flags);
208 ep->is_iso = (tmp == USB_ENDPOINT_XFER_ISOC) ? 1 : 0;
209 tmp <<= ENDPOINT_TYPE;
210 tmp |= desc->bEndpointAddress;
211 tmp |= (4 << ENDPOINT_BYTE_COUNT); /* default full fifo lines */
212 tmp |= 1 << ENDPOINT_ENABLE;
215 /* for OUT transfers, block the rx fifo until a read is posted */
216 ep->is_in = (tmp & USB_DIR_IN) != 0;
218 writel ((1 << SET_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
220 writel (tmp, &ep->regs->ep_cfg);
223 if (!ep->dma) { /* pio, per-packet */
224 tmp = (1 << ep->num) | readl (&dev->regs->pciirqenb0);
225 writel (tmp, &dev->regs->pciirqenb0);
227 tmp = (1 << DATA_PACKET_RECEIVED_INTERRUPT_ENABLE)
228 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT_ENABLE)
229 | readl (&ep->regs->ep_irqenb);
230 writel (tmp, &ep->regs->ep_irqenb);
231 } else { /* dma, per-request */
232 tmp = (1 << (8 + ep->num)); /* completion */
233 tmp |= readl (&dev->regs->pciirqenb1);
234 writel (tmp, &dev->regs->pciirqenb1);
236 /* for short OUT transfers, dma completions can't
237 * advance the queue; do it pio-style, by hand.
238 * NOTE erratum 0112 workaround #2
240 if ((desc->bEndpointAddress & USB_DIR_IN) == 0) {
241 tmp = (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT_ENABLE);
242 writel (tmp, &ep->regs->ep_irqenb);
244 tmp = (1 << ep->num) | readl (&dev->regs->pciirqenb0);
245 writel (tmp, &dev->regs->pciirqenb0);
249 tmp = desc->bEndpointAddress;
250 DEBUG (dev, "enabled %s (ep%d%s-%s) %s max %04x\n",
251 _ep->name, tmp & 0x0f, DIR_STRING (tmp),
252 type_string (desc->bmAttributes),
253 ep->dma ? "dma" : "pio", max);
255 /* pci writes may still be posted */
256 spin_unlock_irqrestore (&dev->lock, flags);
260 static int handshake (u32 *ptr, u32 mask, u32 done, int usec)
265 result = readl (ptr);
266 if (result == ~(u32)0) /* "device unplugged" */
277 static struct usb_ep_ops net2280_ep_ops;
279 static void ep_reset (struct net2280_regs *regs, struct net2280_ep *ep)
284 INIT_LIST_HEAD (&ep->queue);
286 ep->ep.maxpacket = ~0;
287 ep->ep.ops = &net2280_ep_ops;
289 /* disable the dma, irqs, endpoint... */
291 writel (0, &ep->dma->dmactl);
292 writel ( (1 << DMA_SCATTER_GATHER_DONE_INTERRUPT)
293 | (1 << DMA_TRANSACTION_DONE_INTERRUPT)
295 , &ep->dma->dmastat);
297 tmp = readl (®s->pciirqenb0);
298 tmp &= ~(1 << ep->num);
299 writel (tmp, ®s->pciirqenb0);
301 tmp = readl (®s->pciirqenb1);
302 tmp &= ~(1 << (8 + ep->num)); /* completion */
303 writel (tmp, ®s->pciirqenb1);
305 writel (0, &ep->regs->ep_irqenb);
307 /* init to our chosen defaults, notably so that we NAK OUT
308 * packets until the driver queues a read (+note erratum 0112)
310 writel ( (1 << SET_NAK_OUT_PACKETS_MODE)
311 | (1 << SET_NAK_OUT_PACKETS)
312 | (1 << CLEAR_EP_HIDE_STATUS_PHASE)
313 | (1 << CLEAR_INTERRUPT_MODE)
314 | (1 << CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE)
315 | (1 << CLEAR_ENDPOINT_TOGGLE)
316 | (1 << CLEAR_ENDPOINT_HALT)
317 , &ep->regs->ep_rsp);
319 /* scrub most status bits, and flush any fifo state */
320 writel ( (1 << TIMEOUT)
321 | (1 << USB_STALL_SENT)
322 | (1 << USB_IN_NAK_SENT)
323 | (1 << USB_IN_ACK_RCVD)
324 | (1 << USB_OUT_PING_NAK_SENT)
325 | (1 << USB_OUT_ACK_SENT)
326 | (1 << FIFO_OVERFLOW)
327 | (1 << FIFO_UNDERFLOW)
329 | (1 << SHORT_PACKET_OUT_DONE_INTERRUPT)
330 | (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)
331 | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
332 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
333 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
334 | (1 << DATA_IN_TOKEN_INTERRUPT)
335 , &ep->regs->ep_stat);
337 /* fifo size is handled separately */
340 static void nuke (struct net2280_ep *);
342 static int net2280_disable (struct usb_ep *_ep)
344 struct net2280_ep *ep;
347 ep = container_of (_ep, struct net2280_ep, ep);
348 if (!_ep || !ep->desc || _ep->name == ep0name)
351 spin_lock_irqsave (&ep->dev->lock, flags);
353 ep_reset (ep->dev->regs, ep);
355 VDEBUG (ep->dev, "disabled %s %s\n",
356 ep->dma ? "dma" : "pio", _ep->name);
358 /* synch memory views with the device */
359 (void) readl (&ep->regs->ep_cfg);
361 if (use_dma && !ep->dma && ep->num >= 1 && ep->num <= 4)
362 ep->dma = &ep->dev->dma [ep->num - 1];
364 spin_unlock_irqrestore (&ep->dev->lock, flags);
368 /*-------------------------------------------------------------------------*/
370 static struct usb_request *
371 net2280_alloc_request (struct usb_ep *_ep, int gfp_flags)
373 struct net2280_ep *ep;
374 struct net2280_request *req;
378 ep = container_of (_ep, struct net2280_ep, ep);
380 req = kmalloc (sizeof *req, gfp_flags);
384 memset (req, 0, sizeof *req);
385 req->req.dma = DMA_ADDR_INVALID;
386 INIT_LIST_HEAD (&req->queue);
388 /* this dma descriptor may be swapped with the previous dummy */
390 struct net2280_dma *td;
392 td = pci_pool_alloc (ep->dev->requests, gfp_flags,
398 td->dmacount = 0; /* not VALID */
399 td->dmaaddr = __constant_cpu_to_le32 (DMA_ADDR_INVALID);
400 td->dmadesc = td->dmaaddr;
407 net2280_free_request (struct usb_ep *_ep, struct usb_request *_req)
409 struct net2280_ep *ep;
410 struct net2280_request *req;
412 ep = container_of (_ep, struct net2280_ep, ep);
416 req = container_of (_req, struct net2280_request, req);
417 WARN_ON (!list_empty (&req->queue));
419 pci_pool_free (ep->dev->requests, req->td, req->td_dma);
423 /*-------------------------------------------------------------------------*/
427 /* many common platforms have dma-coherent caches, which means that it's
428 * safe to use kmalloc() memory for all i/o buffers without using any
429 * cache flushing calls. (unless you're trying to share cache lines
430 * between dma and non-dma activities, which is a slow idea in any case.)
432 * other platforms need more care, with 2.5 having a moderately general
433 * solution (which falls down for allocations smaller than one page)
434 * that improves significantly on the 2.4 PCI allocators by removing
435 * the restriction that memory never be freed in_interrupt().
437 #if defined(CONFIG_X86)
440 #elif defined(CONFIG_PPC) && !defined(CONFIG_NOT_COHERENT_CACHE)
443 #elif defined(CONFIG_MIPS) && !defined(CONFIG_NONCOHERENT_IO)
446 /* FIXME there are other cases, including an x86-64 one ... */
449 /* allocating buffers this way eliminates dma mapping overhead, which
450 * on some platforms will mean eliminating a per-io buffer copy. with
451 * some kinds of system caches, further tweaks may still be needed.
454 net2280_alloc_buffer (
462 struct net2280_ep *ep;
464 ep = container_of (_ep, struct net2280_ep, ep);
467 *dma = DMA_ADDR_INVALID;
469 #if defined(USE_KMALLOC)
470 retval = kmalloc(bytes, gfp_flags);
472 *dma = virt_to_phys(retval);
475 /* one problem with this call is that it wastes memory on
476 * typical 1/N page allocations: it allocates 1..N pages.
477 * another is that it always uses GFP_ATOMIC.
479 #warning Using pci_alloc_consistent even with buffers smaller than a page.
480 retval = pci_alloc_consistent(ep->dev->pdev, bytes, dma);
482 retval = kmalloc(bytes, gfp_flags);
488 net2280_free_buffer (
494 /* free memory into the right allocator */
496 if (dma != DMA_ADDR_INVALID) {
497 struct net2280_ep *ep;
499 ep = container_of(_ep, struct net2280_ep, ep);
502 /* one problem with this call is that some platforms
503 * don't allow it to be used in_irq().
505 pci_free_consistent(ep->dev->pdev, bytes, buf, dma);
511 /*-------------------------------------------------------------------------*/
513 /* load a packet into the fifo we use for usb IN transfers.
514 * works for all endpoints.
516 * NOTE: pio with ep-a..ep-d could stuff multiple packets into the fifo
517 * at a time, but this code is simpler because it knows it only writes
518 * one packet. ep-a..ep-d should use dma instead.
521 write_fifo (struct net2280_ep *ep, struct usb_request *req)
523 struct net2280_ep_regs *regs = ep->regs;
526 unsigned count, total;
528 /* INVARIANT: fifo is currently empty. (testable) */
531 buf = req->buf + req->actual;
533 total = req->length - req->actual;
539 /* write just one packet at a time */
540 count = ep->ep.maxpacket;
541 if (count > total) /* min() cannot be used on a bitfield */
544 VDEBUG (ep->dev, "write %s fifo (IN) %d bytes%s req %p\n",
546 (count != ep->ep.maxpacket) ? " (short)" : "",
549 /* NOTE be careful if you try to align these. fifo lines
550 * should normally be full (4 bytes) and successive partial
551 * lines are ok only in certain cases.
553 tmp = get_unaligned ((u32 *)buf);
555 writel (tmp, ®s->ep_data);
560 /* last fifo entry is "short" unless we wrote a full packet.
561 * also explicitly validate last word in (periodic) transfers
562 * when maxpacket is not a multiple of 4 bytes.
564 if (count || total < ep->ep.maxpacket) {
565 tmp = count ? get_unaligned ((u32 *)buf) : count;
567 set_fifo_bytecount (ep, count & 0x03);
568 writel (tmp, ®s->ep_data);
571 /* pci writes may still be posted */
574 /* work around erratum 0106: PCI and USB race over the OUT fifo.
575 * caller guarantees chiprev 0100, out endpoint is NAKing, and
576 * there's no real data in the fifo.
578 * NOTE: also used in cases where that erratum doesn't apply:
579 * where the host wrote "too much" data to us.
581 static void out_flush (struct net2280_ep *ep)
585 ASSERT_OUT_NAKING (ep);
587 statp = &ep->regs->ep_stat;
588 writel ( (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
589 | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
591 writel ((1 << FIFO_FLUSH), statp);
594 if (tmp & (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
595 /* high speed did bulk NYET; fifo isn't filling */
596 && ep->dev->gadget.speed == USB_SPEED_FULL) {
599 usec = 50; /* 64 byte bulk/interrupt */
600 handshake (statp, (1 << USB_OUT_PING_NAK_SENT),
601 (1 << USB_OUT_PING_NAK_SENT), usec);
602 /* NAK done; now CLEAR_NAK_OUT_PACKETS is safe */
606 /* unload packet(s) from the fifo we use for usb OUT transfers.
607 * returns true iff the request completed, because of short packet
608 * or the request buffer having filled with full packets.
610 * for ep-a..ep-d this will read multiple packets out when they
611 * have been accepted.
614 read_fifo (struct net2280_ep *ep, struct net2280_request *req)
616 struct net2280_ep_regs *regs = ep->regs;
617 u8 *buf = req->req.buf + req->req.actual;
618 unsigned count, tmp, is_short;
619 unsigned cleanup = 0, prevent = 0;
621 /* erratum 0106 ... packets coming in during fifo reads might
622 * be incompletely rejected. not all cases have workarounds.
624 if (ep->dev->chiprev == 0x0100
625 && ep->dev->gadget.speed == USB_SPEED_FULL) {
627 tmp = readl (&ep->regs->ep_stat);
628 if ((tmp & (1 << NAK_OUT_PACKETS)))
630 else if ((tmp & (1 << FIFO_FULL))) {
631 start_out_naking (ep);
634 /* else: hope we don't see the problem */
637 /* never overflow the rx buffer. the fifo reads packets until
638 * it sees a short one; we might not be ready for them all.
641 count = readl (®s->ep_avail);
642 if (unlikely (count == 0)) {
644 tmp = readl (&ep->regs->ep_stat);
645 count = readl (®s->ep_avail);
646 /* handled that data already? */
647 if (count == 0 && (tmp & (1 << NAK_OUT_PACKETS)) == 0)
651 tmp = req->req.length - req->req.actual;
653 /* as with DMA, data overflow gets flushed */
654 if ((tmp % ep->ep.maxpacket) != 0) {
656 "%s out fifo %d bytes, expected %d\n",
657 ep->ep.name, count, tmp);
658 req->req.status = -EOVERFLOW;
660 /* NAK_OUT_PACKETS will be set, so flushing is safe;
661 * the next read will start with the next packet
663 } /* else it's a ZLP, no worries */
666 req->req.actual += count;
668 is_short = (count == 0) || ((count % ep->ep.maxpacket) != 0);
670 VDEBUG (ep->dev, "read %s fifo (OUT) %d bytes%s%s%s req %p %d/%d\n",
671 ep->ep.name, count, is_short ? " (short)" : "",
672 cleanup ? " flush" : "", prevent ? " nak" : "",
673 req, req->req.actual, req->req.length);
676 tmp = readl (®s->ep_data);
678 put_unaligned (tmp, (u32 *)buf);
683 tmp = readl (®s->ep_data);
693 writel ((1 << CLEAR_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
694 (void) readl (&ep->regs->ep_rsp);
697 return is_short || ((req->req.actual == req->req.length)
701 /* fill out dma descriptor to match a given request */
703 fill_dma_desc (struct net2280_ep *ep, struct net2280_request *req, int valid)
705 struct net2280_dma *td = req->td;
706 u32 dmacount = req->req.length;
708 /* don't let DMA continue after a short OUT packet,
709 * so overruns can't affect the next transfer.
710 * in case of overruns on max-size packets, we can't
711 * stop the fifo from filling but we can flush it.
714 dmacount |= (1 << DMA_DIRECTION);
715 else if ((dmacount % ep->ep.maxpacket) != 0)
716 dmacount |= (1 << END_OF_CHAIN);
720 dmacount |= (1 << VALID_BIT);
721 if (likely(!req->req.no_interrupt || !use_dma_chaining))
722 dmacount |= (1 << DMA_DONE_INTERRUPT_ENABLE);
724 /* td->dmadesc = previously set by caller */
725 td->dmaaddr = cpu_to_le32p (&req->req.dma);
727 /* 2280 may be polling VALID_BIT through ep->dma->dmadesc */
729 td->dmacount = cpu_to_le32p (&dmacount);
732 static const u32 dmactl_default =
733 (1 << DMA_SCATTER_GATHER_DONE_INTERRUPT)
734 | (1 << DMA_CLEAR_COUNT_ENABLE)
735 /* erratum 0116 workaround part 1 (use POLLING) */
736 | (POLL_100_USEC << DESCRIPTOR_POLLING_RATE)
737 | (1 << DMA_VALID_BIT_POLLING_ENABLE)
738 | (1 << DMA_VALID_BIT_ENABLE)
739 | (1 << DMA_SCATTER_GATHER_ENABLE)
740 /* erratum 0116 workaround part 2 (no AUTOSTART) */
743 static inline void spin_stop_dma (struct net2280_dma_regs *dma)
745 handshake (&dma->dmactl, (1 << DMA_ENABLE), 0, 50);
748 static inline void stop_dma (struct net2280_dma_regs *dma)
750 writel (readl (&dma->dmactl) & ~(1 << DMA_ENABLE), &dma->dmactl);
754 static void start_queue (struct net2280_ep *ep, u32 dmactl, u32 td_dma)
756 struct net2280_dma_regs *dma = ep->dma;
758 writel ((1 << VALID_BIT) | (ep->is_in << DMA_DIRECTION),
760 writel (readl (&dma->dmastat), &dma->dmastat);
762 writel (td_dma, &dma->dmadesc);
763 writel (dmactl, &dma->dmactl);
765 /* erratum 0116 workaround part 3: pci arbiter away from net2280 */
766 (void) readl (&ep->dev->pci->pcimstctl);
768 writel ((1 << DMA_START), &dma->dmastat);
771 stop_out_naking (ep);
774 static void start_dma (struct net2280_ep *ep, struct net2280_request *req)
777 struct net2280_dma_regs *dma = ep->dma;
779 /* FIXME can't use DMA for ZLPs */
781 /* on this path we "know" there's no dma active (yet) */
782 WARN_ON (readl (&dma->dmactl) & (1 << DMA_ENABLE));
783 writel (0, &ep->dma->dmactl);
785 /* previous OUT packet might have been short */
786 if (!ep->is_in && ((tmp = readl (&ep->regs->ep_stat))
787 & (1 << NAK_OUT_PACKETS)) != 0) {
788 writel ((1 << SHORT_PACKET_TRANSFERRED_INTERRUPT),
791 tmp = readl (&ep->regs->ep_avail);
793 writel (readl (&dma->dmastat), &dma->dmastat);
795 /* transfer all/some fifo data */
796 writel (req->req.dma, &dma->dmaaddr);
797 tmp = min (tmp, req->req.length);
799 /* dma irq, faking scatterlist status */
800 req->td->dmacount = cpu_to_le32 (req->req.length - tmp);
801 writel ((1 << DMA_DONE_INTERRUPT_ENABLE)
802 | tmp, &dma->dmacount);
803 req->td->dmadesc = 0;
806 writel ((1 << DMA_ENABLE), &dma->dmactl);
807 writel ((1 << DMA_START), &dma->dmastat);
812 tmp = dmactl_default;
814 /* force packet boundaries between dma requests, but prevent the
815 * controller from automagically writing a last "short" packet
816 * (zero length) unless the driver explicitly said to do that.
819 if (likely ((req->req.length % ep->ep.maxpacket) != 0
821 tmp |= (1 << DMA_FIFO_VALIDATE);
822 ep->in_fifo_validate = 1;
824 ep->in_fifo_validate = 0;
827 /* init req->td, pointing to the current dummy */
828 req->td->dmadesc = cpu_to_le32 (ep->td_dma);
829 fill_dma_desc (ep, req, 1);
831 if (!use_dma_chaining)
832 req->td->dmacount |= __constant_cpu_to_le32 (1 << END_OF_CHAIN);
834 start_queue (ep, tmp, req->td_dma);
838 queue_dma (struct net2280_ep *ep, struct net2280_request *req, int valid)
840 struct net2280_dma *end;
843 /* swap new dummy for old, link; fill and maybe activate */
849 ep->td_dma = req->td_dma;
852 end->dmadesc = cpu_to_le32 (ep->td_dma);
854 fill_dma_desc (ep, req, valid);
858 done (struct net2280_ep *ep, struct net2280_request *req, int status)
861 unsigned stopped = ep->stopped;
863 list_del_init (&req->queue);
865 if (req->req.status == -EINPROGRESS)
866 req->req.status = status;
868 status = req->req.status;
872 pci_unmap_single (dev->pdev, req->req.dma, req->req.length,
873 ep->is_in ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
874 req->req.dma = DMA_ADDR_INVALID;
878 if (status && status != -ESHUTDOWN)
879 VDEBUG (dev, "complete %s req %p stat %d len %u/%u\n",
880 ep->ep.name, &req->req, status,
881 req->req.actual, req->req.length);
883 /* don't modify queue heads during completion callback */
885 spin_unlock (&dev->lock);
886 req->req.complete (&ep->ep, &req->req);
887 spin_lock (&dev->lock);
888 ep->stopped = stopped;
891 /*-------------------------------------------------------------------------*/
894 net2280_queue (struct usb_ep *_ep, struct usb_request *_req, int gfp_flags)
896 struct net2280_request *req;
897 struct net2280_ep *ep;
901 /* we always require a cpu-view buffer, so that we can
902 * always use pio (as fallback or whatever).
904 req = container_of (_req, struct net2280_request, req);
905 if (!_req || !_req->complete || !_req->buf
906 || !list_empty (&req->queue))
908 if (_req->length > (~0 & DMA_BYTE_COUNT_MASK))
910 ep = container_of (_ep, struct net2280_ep, ep);
911 if (!_ep || (!ep->desc && ep->num != 0))
914 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
917 /* FIXME implement PIO fallback for ZLPs with DMA */
918 if (ep->dma && _req->length == 0)
921 /* set up dma mapping in case the caller didn't */
922 if (ep->dma && _req->dma == DMA_ADDR_INVALID) {
923 _req->dma = pci_map_single (dev->pdev, _req->buf, _req->length,
924 ep->is_in ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
929 VDEBUG (dev, "%s queue req %p, len %d buf %p\n",
930 _ep->name, _req, _req->length, _req->buf);
933 spin_lock_irqsave (&dev->lock, flags);
935 _req->status = -EINPROGRESS;
938 /* kickstart this i/o queue? */
939 if (list_empty (&ep->queue) && !ep->stopped) {
940 /* use DMA if the endpoint supports it, else pio */
944 /* maybe there's no control data, just status ack */
945 if (ep->num == 0 && _req->length == 0) {
948 VDEBUG (dev, "%s status ack\n", ep->ep.name);
952 /* PIO ... stuff the fifo, or unblock it. */
954 write_fifo (ep, _req);
955 else if (list_empty (&ep->queue)) {
958 /* OUT FIFO might have packet(s) buffered */
959 s = readl (&ep->regs->ep_stat);
960 if ((s & (1 << FIFO_EMPTY)) == 0) {
961 /* note: _req->short_not_ok is
962 * ignored here since PIO _always_
963 * stops queue advance here, and
964 * _req->status doesn't change for
965 * short reads (only _req->actual)
967 if (read_fifo (ep, req)) {
974 s = readl (&ep->regs->ep_stat);
977 /* don't NAK, let the fifo fill */
978 if (req && (s & (1 << NAK_OUT_PACKETS)))
979 writel ((1 << CLEAR_NAK_OUT_PACKETS),
984 } else if (ep->dma) {
990 /* preventing magic zlps is per-engine state, not
991 * per-transfer; irq logic must recover hiccups.
993 expect = likely (req->req.zero
994 || (req->req.length % ep->ep.maxpacket) != 0);
995 if (expect != ep->in_fifo_validate)
998 queue_dma (ep, req, valid);
1000 } /* else the irq handler advances the queue. */
1003 list_add_tail (&req->queue, &ep->queue);
1005 spin_unlock_irqrestore (&dev->lock, flags);
1007 /* pci writes may still be posted */
1013 struct net2280_ep *ep,
1014 struct net2280_request *req,
1019 req->req.actual = req->req.length - (DMA_BYTE_COUNT_MASK & dmacount);
1020 done (ep, req, status);
1023 static void restart_dma (struct net2280_ep *ep);
1025 static void scan_dma_completions (struct net2280_ep *ep)
1027 /* only look at descriptors that were "naturally" retired,
1028 * so fifo and list head state won't matter
1030 while (!list_empty (&ep->queue)) {
1031 struct net2280_request *req;
1034 req = list_entry (ep->queue.next,
1035 struct net2280_request, queue);
1039 tmp = le32_to_cpup (&req->td->dmacount);
1040 if ((tmp & (1 << VALID_BIT)) != 0)
1043 /* SHORT_PACKET_TRANSFERRED_INTERRUPT handles "usb-short"
1044 * cases where DMA must be aborted; this code handles
1045 * all non-abort DMA completions.
1047 if (unlikely (req->td->dmadesc == 0)) {
1049 tmp = readl (&ep->dma->dmacount);
1050 if (tmp & DMA_BYTE_COUNT_MASK)
1052 /* single transfer mode */
1053 dma_done (ep, req, tmp, 0);
1055 } else if (!ep->is_in
1056 && (req->req.length % ep->ep.maxpacket) != 0) {
1057 tmp = readl (&ep->regs->ep_stat);
1059 /* AVOID TROUBLE HERE by not issuing short reads from
1060 * your gadget driver. That helps avoids errata 0121,
1061 * 0122, and 0124; not all cases trigger the warning.
1063 if ((tmp & (1 << NAK_OUT_PACKETS)) == 0) {
1064 WARN (ep->dev, "%s lost packet sync!\n",
1066 req->req.status = -EOVERFLOW;
1067 } else if ((tmp = readl (&ep->regs->ep_avail)) != 0) {
1068 /* fifo gets flushed later */
1069 ep->out_overflow = 1;
1070 DEBUG (ep->dev, "%s dma, discard %d len %d\n",
1073 req->req.status = -EOVERFLOW;
1076 dma_done (ep, req, tmp, 0);
1080 static void restart_dma (struct net2280_ep *ep)
1082 struct net2280_request *req;
1083 u32 dmactl = dmactl_default;
1087 req = list_entry (ep->queue.next, struct net2280_request, queue);
1089 if (!use_dma_chaining) {
1090 start_dma (ep, req);
1094 /* the 2280 will be processing the queue unless queue hiccups after
1095 * the previous transfer:
1096 * IN: wanted automagic zlp, head doesn't (or vice versa)
1097 * DMA_FIFO_VALIDATE doesn't init from dma descriptors.
1098 * OUT: was "usb-short", we must restart.
1100 if (ep->is_in && !req->valid) {
1101 struct net2280_request *entry, *prev = 0;
1102 int reqmode, done = 0;
1104 DEBUG (ep->dev, "%s dma hiccup td %p\n", ep->ep.name, req->td);
1105 ep->in_fifo_validate = likely (req->req.zero
1106 || (req->req.length % ep->ep.maxpacket) != 0);
1107 if (ep->in_fifo_validate)
1108 dmactl |= (1 << DMA_FIFO_VALIDATE);
1109 list_for_each_entry (entry, &ep->queue, queue) {
1114 dmacount = entry->td->dmacount;
1116 reqmode = likely (entry->req.zero
1117 || (entry->req.length
1118 % ep->ep.maxpacket) != 0);
1119 if (reqmode == ep->in_fifo_validate) {
1121 dmacount |= valid_bit;
1122 entry->td->dmacount = dmacount;
1126 /* force a hiccup */
1127 prev->td->dmacount |= dma_done_ie;
1132 /* walk the rest of the queue so unlinks behave */
1134 dmacount &= ~valid_bit;
1135 entry->td->dmacount = dmacount;
1140 writel (0, &ep->dma->dmactl);
1141 start_queue (ep, dmactl, req->td_dma);
1144 static void abort_dma (struct net2280_ep *ep)
1146 /* abort the current transfer */
1147 if (likely (!list_empty (&ep->queue))) {
1148 /* FIXME work around errata 0121, 0122, 0124 */
1149 writel ((1 << DMA_ABORT), &ep->dma->dmastat);
1150 spin_stop_dma (ep->dma);
1153 scan_dma_completions (ep);
1156 /* dequeue ALL requests */
1157 static void nuke (struct net2280_ep *ep)
1159 struct net2280_request *req;
1161 /* called with spinlock held */
1165 while (!list_empty (&ep->queue)) {
1166 req = list_entry (ep->queue.next,
1167 struct net2280_request,
1169 done (ep, req, -ESHUTDOWN);
1173 /* dequeue JUST ONE request */
1174 static int net2280_dequeue (struct usb_ep *_ep, struct usb_request *_req)
1176 struct net2280_ep *ep;
1177 struct net2280_request *req;
1178 unsigned long flags;
1182 ep = container_of (_ep, struct net2280_ep, ep);
1183 if (!_ep || (!ep->desc && ep->num != 0) || !_req)
1186 spin_lock_irqsave (&ep->dev->lock, flags);
1187 stopped = ep->stopped;
1189 /* quiesce dma while we patch the queue */
1193 dmactl = readl (&ep->dma->dmactl);
1194 /* WARNING erratum 0127 may kick in ... */
1196 scan_dma_completions (ep);
1199 /* make sure it's still queued on this endpoint */
1200 list_for_each_entry (req, &ep->queue, queue) {
1201 if (&req->req == _req)
1204 if (&req->req != _req) {
1205 spin_unlock_irqrestore (&ep->dev->lock, flags);
1209 /* queue head may be partially complete. */
1210 if (ep->queue.next == &req->queue) {
1212 DEBUG (ep->dev, "unlink (%s) dma\n", _ep->name);
1213 _req->status = -ECONNRESET;
1215 if (likely (ep->queue.next == &req->queue)) {
1216 // NOTE: misreports single-transfer mode
1217 req->td->dmacount = 0; /* invalidate */
1219 readl (&ep->dma->dmacount),
1223 DEBUG (ep->dev, "unlink (%s) pio\n", _ep->name);
1224 done (ep, req, -ECONNRESET);
1228 /* patch up hardware chaining data */
1229 } else if (ep->dma && use_dma_chaining) {
1230 if (req->queue.prev == ep->queue.next) {
1231 writel (le32_to_cpu (req->td->dmadesc),
1233 if (req->td->dmacount & dma_done_ie)
1234 writel (readl (&ep->dma->dmacount)
1236 &ep->dma->dmacount);
1238 struct net2280_request *prev;
1240 prev = list_entry (req->queue.prev,
1241 struct net2280_request, queue);
1242 prev->td->dmadesc = req->td->dmadesc;
1243 if (req->td->dmacount & dma_done_ie)
1244 prev->td->dmacount |= dma_done_ie;
1249 done (ep, req, -ECONNRESET);
1250 ep->stopped = stopped;
1253 /* turn off dma on inactive queues */
1254 if (list_empty (&ep->queue))
1256 else if (!ep->stopped) {
1257 /* resume current request, or start new one */
1259 writel (dmactl, &ep->dma->dmactl);
1261 start_dma (ep, list_entry (ep->queue.next,
1262 struct net2280_request, queue));
1266 spin_unlock_irqrestore (&ep->dev->lock, flags);
1267 return req ? 0 : -EOPNOTSUPP;
1270 /*-------------------------------------------------------------------------*/
1272 static int net2280_fifo_status (struct usb_ep *_ep);
1275 net2280_set_halt (struct usb_ep *_ep, int value)
1277 struct net2280_ep *ep;
1278 unsigned long flags;
1281 ep = container_of (_ep, struct net2280_ep, ep);
1282 if (!_ep || (!ep->desc && ep->num != 0))
1284 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1286 if (ep->desc /* not ep0 */ && (ep->desc->bmAttributes & 0x03)
1287 == USB_ENDPOINT_XFER_ISOC)
1290 spin_lock_irqsave (&ep->dev->lock, flags);
1291 if (!list_empty (&ep->queue))
1293 else if (ep->is_in && value && net2280_fifo_status (_ep) != 0)
1296 VDEBUG (ep->dev, "%s %s halt\n", _ep->name,
1297 value ? "set" : "clear");
1298 /* set/clear, then synch memory views with the device */
1301 ep->dev->protocol_stall = 1;
1306 (void) readl (&ep->regs->ep_rsp);
1308 spin_unlock_irqrestore (&ep->dev->lock, flags);
1314 net2280_fifo_status (struct usb_ep *_ep)
1316 struct net2280_ep *ep;
1319 ep = container_of (_ep, struct net2280_ep, ep);
1320 if (!_ep || (!ep->desc && ep->num != 0))
1322 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1325 avail = readl (&ep->regs->ep_avail) & ((1 << 12) - 1);
1326 if (avail > ep->fifo_size)
1329 avail = ep->fifo_size - avail;
1334 net2280_fifo_flush (struct usb_ep *_ep)
1336 struct net2280_ep *ep;
1338 ep = container_of (_ep, struct net2280_ep, ep);
1339 if (!_ep || (!ep->desc && ep->num != 0))
1341 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1344 writel ((1 << FIFO_FLUSH), &ep->regs->ep_stat);
1345 (void) readl (&ep->regs->ep_rsp);
1348 static struct usb_ep_ops net2280_ep_ops = {
1349 .enable = net2280_enable,
1350 .disable = net2280_disable,
1352 .alloc_request = net2280_alloc_request,
1353 .free_request = net2280_free_request,
1355 .alloc_buffer = net2280_alloc_buffer,
1356 .free_buffer = net2280_free_buffer,
1358 .queue = net2280_queue,
1359 .dequeue = net2280_dequeue,
1361 .set_halt = net2280_set_halt,
1362 .fifo_status = net2280_fifo_status,
1363 .fifo_flush = net2280_fifo_flush,
1366 /*-------------------------------------------------------------------------*/
1368 static int net2280_get_frame (struct usb_gadget *_gadget)
1370 struct net2280 *dev;
1371 unsigned long flags;
1376 dev = container_of (_gadget, struct net2280, gadget);
1377 spin_lock_irqsave (&dev->lock, flags);
1378 retval = get_idx_reg (dev->regs, REG_FRAME) & 0x03ff;
1379 spin_unlock_irqrestore (&dev->lock, flags);
1383 static int net2280_wakeup (struct usb_gadget *_gadget)
1385 struct net2280 *dev;
1387 unsigned long flags;
1391 dev = container_of (_gadget, struct net2280, gadget);
1393 spin_lock_irqsave (&dev->lock, flags);
1394 tmp = readl (&dev->usb->usbctl);
1395 if (tmp & (1 << DEVICE_REMOTE_WAKEUP_ENABLE))
1396 writel (1 << GENERATE_RESUME, &dev->usb->usbstat);
1397 spin_unlock_irqrestore (&dev->lock, flags);
1399 /* pci writes may still be posted */
1403 static int net2280_set_selfpowered (struct usb_gadget *_gadget, int value)
1405 struct net2280 *dev;
1407 unsigned long flags;
1411 dev = container_of (_gadget, struct net2280, gadget);
1413 spin_lock_irqsave (&dev->lock, flags);
1414 tmp = readl (&dev->usb->usbctl);
1416 tmp |= (1 << SELF_POWERED_STATUS);
1418 tmp &= ~(1 << SELF_POWERED_STATUS);
1419 writel (tmp, &dev->usb->usbctl);
1420 spin_unlock_irqrestore (&dev->lock, flags);
1425 static const struct usb_gadget_ops net2280_ops = {
1426 .get_frame = net2280_get_frame,
1427 .wakeup = net2280_wakeup,
1428 .set_selfpowered = net2280_set_selfpowered,
1431 /*-------------------------------------------------------------------------*/
1433 #ifdef USE_SYSFS_DEBUG_FILES
1435 /* "function" sysfs attribute */
1437 show_function (struct device *_dev, char *buf)
1439 struct net2280 *dev = dev_get_drvdata (_dev);
1442 || !dev->driver->function
1443 || strlen (dev->driver->function) > PAGE_SIZE)
1445 return snprintf (buf, PAGE_SIZE, "%s\n", dev->driver->function);
1447 static DEVICE_ATTR (function, S_IRUGO, show_function, NULL);
1450 show_registers (struct device *_dev, char *buf)
1452 struct net2280 *dev;
1455 unsigned long flags;
1460 dev = dev_get_drvdata (_dev);
1463 spin_lock_irqsave (&dev->lock, flags);
1466 s = dev->driver->driver.name;
1470 /* Main Control Registers */
1471 t = snprintf (next, size, "%s version " DRIVER_VERSION
1472 ", chiprev %04x, dma %s\n\n"
1473 "devinit %03x fifoctl %08x gadget '%s'\n"
1474 "pci irqenb0 %02x irqenb1 %08x "
1475 "irqstat0 %04x irqstat1 %08x\n",
1476 driver_name, dev->chiprev,
1478 ? (use_dma_chaining ? "chaining" : "enabled")
1480 readl (&dev->regs->devinit),
1481 readl (&dev->regs->fifoctl),
1483 readl (&dev->regs->pciirqenb0),
1484 readl (&dev->regs->pciirqenb1),
1485 readl (&dev->regs->irqstat0),
1486 readl (&dev->regs->irqstat1));
1490 /* USB Control Registers */
1491 t1 = readl (&dev->usb->usbctl);
1492 t2 = readl (&dev->usb->usbstat);
1493 if (t1 & (1 << VBUS_PIN)) {
1494 if (t2 & (1 << HIGH_SPEED))
1496 else if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1500 /* full speed bit (6) not working?? */
1503 t = snprintf (next, size,
1504 "stdrsp %08x usbctl %08x usbstat %08x "
1505 "addr 0x%02x (%s)\n",
1506 readl (&dev->usb->stdrsp), t1, t2,
1507 readl (&dev->usb->ouraddr), s);
1511 /* PCI Master Control Registers */
1513 /* DMA Control Registers */
1515 /* Configurable EP Control Registers */
1516 for (i = 0; i < 7; i++) {
1517 struct net2280_ep *ep;
1523 t1 = readl (&ep->regs->ep_cfg);
1524 t2 = readl (&ep->regs->ep_rsp) & 0xff;
1525 t = snprintf (next, size,
1526 "\n%s\tcfg %05x rsp (%02x) %s%s%s%s%s%s%s%s"
1528 ep->ep.name, t1, t2,
1529 (t2 & (1 << CLEAR_NAK_OUT_PACKETS))
1531 (t2 & (1 << CLEAR_EP_HIDE_STATUS_PHASE))
1533 (t2 & (1 << CLEAR_EP_FORCE_CRC_ERROR))
1535 (t2 & (1 << CLEAR_INTERRUPT_MODE))
1536 ? "interrupt " : "",
1537 (t2 & (1<<CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE))
1539 (t2 & (1 << CLEAR_NAK_OUT_PACKETS_MODE))
1541 (t2 & (1 << CLEAR_ENDPOINT_TOGGLE))
1542 ? "DATA1 " : "DATA0 ",
1543 (t2 & (1 << CLEAR_ENDPOINT_HALT))
1545 readl (&ep->regs->ep_irqenb));
1549 t = snprintf (next, size,
1550 "\tstat %08x avail %04x "
1552 readl (&ep->regs->ep_stat),
1553 readl (&ep->regs->ep_avail),
1554 t1 & 0x0f, DIR_STRING (t1),
1555 type_string (t1 >> 8),
1556 ep->stopped ? "*" : "");
1563 t = snprintf (next, size,
1564 " dma\tctl %08x stat %08x count %08x\n"
1565 "\taddr %08x desc %08x\n",
1566 readl (&ep->dma->dmactl),
1567 readl (&ep->dma->dmastat),
1568 readl (&ep->dma->dmacount),
1569 readl (&ep->dma->dmaaddr),
1570 readl (&ep->dma->dmadesc));
1576 /* Indexed Registers */
1580 t = snprintf (next, size, "\nirqs: ");
1583 for (i = 0; i < 7; i++) {
1584 struct net2280_ep *ep;
1589 t = snprintf (next, size, " %s/%lu", ep->ep.name, ep->irqs);
1594 t = snprintf (next, size, "\n");
1598 spin_unlock_irqrestore (&dev->lock, flags);
1600 return PAGE_SIZE - size;
1602 static DEVICE_ATTR (registers, S_IRUGO, show_registers, NULL);
1605 show_queues (struct device *_dev, char *buf)
1607 struct net2280 *dev;
1610 unsigned long flags;
1613 dev = dev_get_drvdata (_dev);
1616 spin_lock_irqsave (&dev->lock, flags);
1618 for (i = 0; i < 7; i++) {
1619 struct net2280_ep *ep = &dev->ep [i];
1620 struct net2280_request *req;
1624 const struct usb_endpoint_descriptor *d;
1629 t = d->bEndpointAddress;
1630 t = snprintf (next, size,
1631 "\n%s (ep%d%s-%s) max %04x %s fifo %d\n",
1632 ep->ep.name, t & USB_ENDPOINT_NUMBER_MASK,
1633 (t & USB_DIR_IN) ? "in" : "out",
1635 switch (d->bmAttributes & 0x03) {
1636 case USB_ENDPOINT_XFER_BULK:
1637 val = "bulk"; break;
1638 case USB_ENDPOINT_XFER_INT:
1639 val = "intr"; break;
1643 le16_to_cpu (d->wMaxPacketSize) & 0x1fff,
1644 ep->dma ? "dma" : "pio", ep->fifo_size
1646 } else /* ep0 should only have one transfer queued */
1647 t = snprintf (next, size, "ep0 max 64 pio %s\n",
1648 ep->is_in ? "in" : "out");
1649 if (t <= 0 || t > size)
1654 if (list_empty (&ep->queue)) {
1655 t = snprintf (next, size, "\t(nothing queued)\n");
1656 if (t <= 0 || t > size)
1662 list_for_each_entry (req, &ep->queue, queue) {
1663 if (ep->dma && req->td_dma == readl (&ep->dma->dmadesc))
1664 t = snprintf (next, size,
1665 "\treq %p len %d/%d "
1666 "buf %p (dmacount %08x)\n",
1667 &req->req, req->req.actual,
1668 req->req.length, req->req.buf,
1669 readl (&ep->dma->dmacount));
1671 t = snprintf (next, size,
1672 "\treq %p len %d/%d buf %p\n",
1673 &req->req, req->req.actual,
1674 req->req.length, req->req.buf);
1675 if (t <= 0 || t > size)
1681 struct net2280_dma *td;
1684 t = snprintf (next, size, "\t td %08x "
1685 " count %08x buf %08x desc %08x\n",
1686 req->td_dma, td->dmacount,
1687 td->dmaaddr, td->dmadesc);
1688 if (t <= 0 || t > size)
1697 spin_unlock_irqrestore (&dev->lock, flags);
1698 return PAGE_SIZE - size;
1700 static DEVICE_ATTR (queues, S_IRUGO, show_queues, NULL);
1705 #define device_create_file(a,b) do {} while (0)
1706 #define device_remove_file device_create_file
1710 /*-------------------------------------------------------------------------*/
1712 /* another driver-specific mode might be a request type doing dma
1713 * to/from another device fifo instead of to/from memory.
1716 static void set_fifo_mode (struct net2280 *dev, int mode)
1718 /* keeping high bits preserves BAR2 */
1719 writel ((0xffff << PCI_BASE2_RANGE) | mode, &dev->regs->fifoctl);
1721 /* always ep-{a,b,e,f} ... maybe not ep-c or ep-d */
1722 INIT_LIST_HEAD (&dev->gadget.ep_list);
1723 list_add_tail (&dev->ep [1].ep.ep_list, &dev->gadget.ep_list);
1724 list_add_tail (&dev->ep [2].ep.ep_list, &dev->gadget.ep_list);
1727 list_add_tail (&dev->ep [3].ep.ep_list, &dev->gadget.ep_list);
1728 list_add_tail (&dev->ep [4].ep.ep_list, &dev->gadget.ep_list);
1729 dev->ep [1].fifo_size = dev->ep [2].fifo_size = 1024;
1732 dev->ep [1].fifo_size = dev->ep [2].fifo_size = 2048;
1735 list_add_tail (&dev->ep [3].ep.ep_list, &dev->gadget.ep_list);
1736 dev->ep [1].fifo_size = 2048;
1737 dev->ep [2].fifo_size = 1024;
1740 /* fifo sizes for ep0, ep-c, ep-d, ep-e, and ep-f never change */
1741 list_add_tail (&dev->ep [5].ep.ep_list, &dev->gadget.ep_list);
1742 list_add_tail (&dev->ep [6].ep.ep_list, &dev->gadget.ep_list);
1746 * net2280_set_fifo_mode - change allocation of fifo buffers
1747 * @gadget: access to the net2280 device that will be updated
1748 * @mode: 0 for default, four 1kB buffers (ep-a through ep-d);
1749 * 1 for two 2kB buffers (ep-a and ep-b only);
1750 * 2 for one 2kB buffer (ep-a) and two 1kB ones (ep-b, ep-c).
1752 * returns zero on success, else negative errno. when this succeeds,
1753 * the contents of gadget->ep_list may have changed.
1755 * you may only call this function when endpoints a-d are all disabled.
1756 * use it whenever extra hardware buffering can help performance, such
1757 * as before enabling "high bandwidth" interrupt endpoints that use
1758 * maxpacket bigger than 512 (when double buffering would otherwise
1761 int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode)
1764 struct net2280 *dev;
1766 unsigned long flags;
1770 dev = container_of (gadget, struct net2280, gadget);
1772 spin_lock_irqsave (&dev->lock, flags);
1774 for (i = 1; i <= 4; i++)
1775 if (dev->ep [i].desc) {
1779 if (mode < 0 || mode > 2)
1782 set_fifo_mode (dev, mode);
1783 spin_unlock_irqrestore (&dev->lock, flags);
1787 DEBUG (dev, "fifo: ep-a 2K, ep-b 2K\n");
1789 DEBUG (dev, "fifo: ep-a 2K, ep-b 1K, ep-c 1K\n");
1790 /* else all are 1K */
1794 EXPORT_SYMBOL (net2280_set_fifo_mode);
1796 /*-------------------------------------------------------------------------*/
1798 /* keeping it simple:
1799 * - one bus driver, initted first;
1800 * - one function driver, initted second
1802 * most of the work to support multiple net2280 controllers would
1803 * be to associate this gadget driver (yes?) with all of them, or
1804 * perhaps to bind specific drivers to specific devices.
1807 static struct net2280 *the_controller;
1809 static void usb_reset (struct net2280 *dev)
1813 /* force immediate bus disconnect, and synch through pci */
1814 writel (0, &dev->usb->usbctl);
1815 dev->gadget.speed = USB_SPEED_UNKNOWN;
1816 (void) readl (&dev->usb->usbctl);
1818 net2280_led_init (dev);
1820 /* disable automatic responses, and irqs */
1821 writel (0, &dev->usb->stdrsp);
1822 writel (0, &dev->regs->pciirqenb0);
1823 writel (0, &dev->regs->pciirqenb1);
1825 /* clear old dma and irq state */
1826 for (tmp = 0; tmp < 4; tmp++) {
1827 struct net2280_ep *ep = &dev->ep [tmp + 1];
1832 writel (~0, &dev->regs->irqstat0),
1833 writel (~(1 << SUSPEND_REQUEST_INTERRUPT), &dev->regs->irqstat1),
1835 /* reset, and enable pci */
1836 tmp = readl (&dev->regs->devinit)
1838 | (1 << FIFO_SOFT_RESET)
1839 | (1 << USB_SOFT_RESET)
1840 | (1 << M8051_RESET);
1841 writel (tmp, &dev->regs->devinit);
1843 /* standard fifo and endpoint allocations */
1844 set_fifo_mode (dev, (fifo_mode <= 2) ? fifo_mode : 0);
1847 static void usb_reinit (struct net2280 *dev)
1852 /* use_dma changes are ignored till next device re-init */
1855 /* basic endpoint init */
1856 for (tmp = 0; tmp < 7; tmp++) {
1857 struct net2280_ep *ep = &dev->ep [tmp];
1859 ep->ep.name = ep_name [tmp];
1863 if (tmp > 0 && tmp <= 4) {
1864 ep->fifo_size = 1024;
1866 ep->dma = &dev->dma [tmp - 1];
1869 ep->regs = &dev->epregs [tmp];
1870 ep_reset (dev->regs, ep);
1872 dev->ep [0].ep.maxpacket = 64;
1873 dev->ep [5].ep.maxpacket = 64;
1874 dev->ep [6].ep.maxpacket = 64;
1876 dev->gadget.ep0 = &dev->ep [0].ep;
1877 dev->ep [0].stopped = 0;
1878 INIT_LIST_HEAD (&dev->gadget.ep0->ep_list);
1880 /* we want to prevent lowlevel/insecure access from the USB host,
1881 * but erratum 0119 means this enable bit is ignored
1883 for (tmp = 0; tmp < 5; tmp++)
1884 writel (EP_DONTUSE, &dev->dep [tmp].dep_cfg);
1887 static void ep0_start (struct net2280 *dev)
1889 writel ( (1 << CLEAR_EP_HIDE_STATUS_PHASE)
1890 | (1 << CLEAR_NAK_OUT_PACKETS)
1891 | (1 << CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE)
1892 , &dev->epregs [0].ep_rsp);
1895 * hardware optionally handles a bunch of standard requests
1896 * that the API hides from drivers anyway. have it do so.
1897 * endpoint status/features are handled in software, to
1898 * help pass tests for some dubious behavior.
1900 writel ( (1 << SET_TEST_MODE)
1901 | (1 << SET_ADDRESS)
1902 | (1 << DEVICE_SET_CLEAR_DEVICE_REMOTE_WAKEUP)
1903 | (1 << GET_DEVICE_STATUS)
1904 | (1 << GET_INTERFACE_STATUS)
1905 , &dev->usb->stdrsp);
1906 writel ( (1 << USB_ROOT_PORT_WAKEUP_ENABLE)
1907 | (1 << SELF_POWERED_USB_DEVICE)
1908 | (1 << REMOTE_WAKEUP_SUPPORT)
1909 | (1 << USB_DETECT_ENABLE)
1910 | (1 << SELF_POWERED_STATUS)
1911 , &dev->usb->usbctl);
1913 /* enable irqs so we can see ep0 and general operation */
1914 writel ( (1 << SETUP_PACKET_INTERRUPT_ENABLE)
1915 | (1 << ENDPOINT_0_INTERRUPT_ENABLE)
1916 , &dev->regs->pciirqenb0);
1917 writel ( (1 << PCI_INTERRUPT_ENABLE)
1918 | (1 << PCI_MASTER_ABORT_RECEIVED_INTERRUPT_ENABLE)
1919 | (1 << PCI_TARGET_ABORT_RECEIVED_INTERRUPT_ENABLE)
1920 | (1 << PCI_RETRY_ABORT_INTERRUPT_ENABLE)
1921 | (1 << VBUS_INTERRUPT_ENABLE)
1922 | (1 << ROOT_PORT_RESET_INTERRUPT_ENABLE)
1923 , &dev->regs->pciirqenb1);
1925 /* don't leave any writes posted */
1926 (void) readl (&dev->usb->usbctl);
1929 /* when a driver is successfully registered, it will receive
1930 * control requests including set_configuration(), which enables
1931 * non-control requests. then usb traffic follows until a
1932 * disconnect is reported. then a host may connect again, or
1933 * the driver might get unbound.
1935 int usb_gadget_register_driver (struct usb_gadget_driver *driver)
1937 struct net2280 *dev = the_controller;
1941 /* insist on high speed support from the driver, since
1942 * (dev->usb->xcvrdiag & FORCE_FULL_SPEED_MODE)
1943 * "must not be used in normal operation"
1946 || driver->speed != USB_SPEED_HIGH
1956 for (i = 0; i < 7; i++)
1957 dev->ep [i].irqs = 0;
1959 /* hook up the driver ... */
1960 dev->driver = driver;
1961 retval = driver->bind (&dev->gadget);
1963 DEBUG (dev, "bind to driver %s --> %d\n",
1964 driver->driver.name, retval);
1969 /* ... then enable host detection and ep0; and we're ready
1970 * for set_configuration as well as eventual disconnect.
1972 net2280_led_active (dev, 1);
1975 DEBUG (dev, "%s ready, usbctl %08x stdrsp %08x\n",
1976 driver->driver.name,
1977 readl (&dev->usb->usbctl),
1978 readl (&dev->usb->stdrsp));
1980 /* pci writes may still be posted */
1983 EXPORT_SYMBOL (usb_gadget_register_driver);
1986 stop_activity (struct net2280 *dev, struct usb_gadget_driver *driver)
1990 /* don't disconnect if it's not connected */
1991 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1994 /* stop hardware; prevent new request submissions;
1995 * and kill any outstanding requests.
1998 for (i = 0; i < 7; i++)
1999 nuke (&dev->ep [i]);
2001 /* report disconnect; the driver is already quiesced */
2003 spin_unlock (&dev->lock);
2004 driver->disconnect (&dev->gadget);
2005 spin_lock (&dev->lock);
2011 int usb_gadget_unregister_driver (struct usb_gadget_driver *driver)
2013 struct net2280 *dev = the_controller;
2014 unsigned long flags;
2018 if (!driver || driver != dev->driver)
2021 spin_lock_irqsave (&dev->lock, flags);
2022 stop_activity (dev, driver);
2023 spin_unlock_irqrestore (&dev->lock, flags);
2025 driver->unbind (&dev->gadget);
2028 net2280_led_active (dev, 0);
2030 DEBUG (dev, "unregistered driver '%s'\n", driver->driver.name);
2033 EXPORT_SYMBOL (usb_gadget_unregister_driver);
2036 /*-------------------------------------------------------------------------*/
2038 /* handle ep0, ep-e, ep-f with 64 byte packets: packet per irq.
2039 * also works for dma-capable endpoints, in pio mode or just
2040 * to manually advance the queue after short OUT transfers.
2042 static void handle_ep_small (struct net2280_ep *ep)
2044 struct net2280_request *req;
2046 /* 0 error, 1 mid-data, 2 done */
2049 if (!list_empty (&ep->queue))
2050 req = list_entry (ep->queue.next,
2051 struct net2280_request, queue);
2055 /* ack all, and handle what we care about */
2056 t = readl (&ep->regs->ep_stat);
2059 VDEBUG (ep->dev, "%s ack ep_stat %08x, req %p\n",
2060 ep->ep.name, t, req ? &req->req : 0);
2062 writel (t & ~(1 << NAK_OUT_PACKETS), &ep->regs->ep_stat);
2064 /* for ep0, monitor token irqs to catch data stage length errors
2065 * and to synchronize on status.
2067 * also, to defer reporting of protocol stalls ... here's where
2068 * data or status first appears, handling stalls here should never
2069 * cause trouble on the host side..
2071 * control requests could be slightly faster without token synch for
2072 * status, but status can jam up that way.
2074 if (unlikely (ep->num == 0)) {
2076 /* status; stop NAKing */
2077 if (t & (1 << DATA_OUT_PING_TOKEN_INTERRUPT)) {
2078 if (ep->dev->protocol_stall) {
2085 /* reply to extra IN data tokens with a zlp */
2086 } else if (t & (1 << DATA_IN_TOKEN_INTERRUPT)) {
2087 if (ep->dev->protocol_stall) {
2091 } else if (!req && ep->stopped)
2095 /* status; stop NAKing */
2096 if (t & (1 << DATA_IN_TOKEN_INTERRUPT)) {
2097 if (ep->dev->protocol_stall) {
2102 /* an extra OUT token is an error */
2103 } else if (((t & (1 << DATA_OUT_PING_TOKEN_INTERRUPT))
2105 && req->req.actual == req->req.length)
2107 ep->dev->protocol_stall = 1;
2111 done (ep, req, -EOVERFLOW);
2117 if (unlikely (!req))
2120 /* manual DMA queue advance after short OUT */
2121 if (likely (ep->dma != 0)) {
2122 if (t & (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)) {
2124 int stopped = ep->stopped;
2126 /* TRANSFERRED works around OUT_DONE erratum 0112.
2127 * we expect (N <= maxpacket) bytes; host wrote M.
2128 * iff (M < N) we won't ever see a DMA interrupt.
2131 for (count = 0; ; t = readl (&ep->regs->ep_stat)) {
2133 /* any preceding dma transfers must finish.
2134 * dma handles (M >= N), may empty the queue
2136 scan_dma_completions (ep);
2137 if (unlikely (list_empty (&ep->queue)
2138 || ep->out_overflow)) {
2142 req = list_entry (ep->queue.next,
2143 struct net2280_request, queue);
2145 /* here either (M < N), a "real" short rx;
2146 * or (M == N) and the queue didn't empty
2148 if (likely (t & (1 << FIFO_EMPTY))) {
2149 count = readl (&ep->dma->dmacount);
2150 count &= DMA_BYTE_COUNT_MASK;
2151 if (readl (&ep->dma->dmadesc)
2159 /* stop DMA, leave ep NAKing */
2160 writel ((1 << DMA_ABORT), &ep->dma->dmastat);
2161 spin_stop_dma (ep->dma);
2163 if (likely (req != 0)) {
2164 req->td->dmacount = 0;
2165 t = readl (&ep->regs->ep_avail);
2166 dma_done (ep, req, count, t);
2169 /* also flush to prevent erratum 0106 trouble */
2170 if (unlikely (ep->out_overflow
2171 || (ep->dev->chiprev == 0x0100
2172 && ep->dev->gadget.speed
2173 == USB_SPEED_FULL))) {
2175 ep->out_overflow = 0;
2178 /* (re)start dma if needed, stop NAKing */
2179 ep->stopped = stopped;
2180 if (!list_empty (&ep->queue))
2183 DEBUG (ep->dev, "%s dma ep_stat %08x ??\n",
2187 /* data packet(s) received (in the fifo, OUT) */
2188 } else if (t & (1 << DATA_PACKET_RECEIVED_INTERRUPT)) {
2189 if (read_fifo (ep, req) && ep->num != 0)
2192 /* data packet(s) transmitted (IN) */
2193 } else if (t & (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)) {
2196 len = req->req.length - req->req.actual;
2197 if (len > ep->ep.maxpacket)
2198 len = ep->ep.maxpacket;
2199 req->req.actual += len;
2201 /* if we wrote it all, we're usually done */
2202 if (req->req.actual == req->req.length) {
2204 /* wait for control status */
2207 } else if (!req->req.zero || len != ep->ep.maxpacket)
2211 /* there was nothing to do ... */
2212 } else if (mode == 1)
2217 /* stream endpoints often resubmit/unlink in completion */
2220 /* maybe advance queue to next request */
2222 /* NOTE: net2280 could let gadget driver start the
2223 * status stage later. since not all controllers let
2224 * them control that, the api doesn't (yet) allow it.
2230 if (!list_empty (&ep->queue) && !ep->stopped)
2231 req = list_entry (ep->queue.next,
2232 struct net2280_request, queue);
2235 if (req && !ep->is_in)
2236 stop_out_naking (ep);
2240 /* is there a buffer for the next packet?
2241 * for best streaming performance, make sure there is one.
2243 if (req && !ep->stopped) {
2245 /* load IN fifo with next packet (may be zlp) */
2246 if (t & (1 << DATA_PACKET_TRANSMITTED_INTERRUPT))
2247 write_fifo (ep, &req->req);
2251 static struct net2280_ep *
2252 get_ep_by_addr (struct net2280 *dev, u16 wIndex)
2254 struct net2280_ep *ep;
2256 if ((wIndex & USB_ENDPOINT_NUMBER_MASK) == 0)
2257 return &dev->ep [0];
2258 list_for_each_entry (ep, &dev->gadget.ep_list, ep.ep_list) {
2259 u8 bEndpointAddress;
2263 bEndpointAddress = ep->desc->bEndpointAddress;
2264 if ((wIndex ^ bEndpointAddress) & USB_DIR_IN)
2266 if ((wIndex & 0x0f) == (bEndpointAddress & 0x0f))
2272 static void handle_stat0_irqs (struct net2280 *dev, u32 stat)
2274 struct net2280_ep *ep;
2277 /* most of these don't need individual acks */
2278 stat &= ~(1 << INTA_ASSERTED);
2281 // DEBUG (dev, "irqstat0 %04x\n", stat);
2283 /* starting a control request? */
2284 if (unlikely (stat & (1 << SETUP_PACKET_INTERRUPT))) {
2287 struct usb_ctrlrequest r;
2290 struct net2280_request *req;
2292 if (dev->gadget.speed == USB_SPEED_UNKNOWN) {
2293 if (readl (&dev->usb->usbstat) & (1 << HIGH_SPEED))
2294 dev->gadget.speed = USB_SPEED_HIGH;
2296 dev->gadget.speed = USB_SPEED_FULL;
2297 net2280_led_speed (dev, dev->gadget.speed);
2298 DEBUG (dev, "%s speed\n",
2299 (dev->gadget.speed == USB_SPEED_HIGH)
2306 /* make sure any leftover request state is cleared */
2307 stat &= ~(1 << ENDPOINT_0_INTERRUPT);
2308 while (!list_empty (&ep->queue)) {
2309 req = list_entry (ep->queue.next,
2310 struct net2280_request, queue);
2311 done (ep, req, (req->req.actual == req->req.length)
2315 dev->protocol_stall = 0;
2316 writel ( (1 << TIMEOUT)
2317 | (1 << USB_STALL_SENT)
2318 | (1 << USB_IN_NAK_SENT)
2319 | (1 << USB_IN_ACK_RCVD)
2320 | (1 << USB_OUT_PING_NAK_SENT)
2321 | (1 << USB_OUT_ACK_SENT)
2322 | (1 << FIFO_OVERFLOW)
2323 | (1 << FIFO_UNDERFLOW)
2324 | (1 << SHORT_PACKET_OUT_DONE_INTERRUPT)
2325 | (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)
2326 | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
2327 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
2328 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
2329 | (1 << DATA_IN_TOKEN_INTERRUPT)
2330 , &ep->regs->ep_stat);
2331 u.raw [0] = readl (&dev->usb->setup0123);
2332 u.raw [1] = readl (&dev->usb->setup4567);
2334 cpu_to_le32s (&u.raw [0]);
2335 cpu_to_le32s (&u.raw [1]);
2337 le16_to_cpus (&u.r.wValue);
2338 le16_to_cpus (&u.r.wIndex);
2339 le16_to_cpus (&u.r.wLength);
2342 writel (1 << SETUP_PACKET_INTERRUPT, &dev->regs->irqstat0);
2343 stat ^= (1 << SETUP_PACKET_INTERRUPT);
2345 /* watch control traffic at the token level, and force
2346 * synchronization before letting the status stage happen.
2347 * FIXME ignore tokens we'll NAK, until driver responds.
2348 * that'll mean a lot less irqs for some drivers.
2350 ep->is_in = (u.r.bRequestType & USB_DIR_IN) != 0;
2352 scratch = (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
2353 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
2354 | (1 << DATA_IN_TOKEN_INTERRUPT);
2355 stop_out_naking (ep);
2357 scratch = (1 << DATA_PACKET_RECEIVED_INTERRUPT)
2358 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
2359 | (1 << DATA_IN_TOKEN_INTERRUPT);
2360 writel (scratch, &dev->epregs [0].ep_irqenb);
2362 /* we made the hardware handle most lowlevel requests;
2363 * everything else goes uplevel to the gadget code.
2365 switch (u.r.bRequest) {
2366 case USB_REQ_GET_STATUS: {
2367 struct net2280_ep *e;
2370 /* hw handles device and interface status */
2371 if (u.r.bRequestType != (USB_DIR_IN|USB_RECIP_ENDPOINT))
2373 if ((e = get_ep_by_addr (dev, u.r.wIndex)) == 0
2377 if (readl (&e->regs->ep_rsp)
2378 & (1 << SET_ENDPOINT_HALT))
2379 status = __constant_cpu_to_le16 (1);
2381 status = __constant_cpu_to_le16 (0);
2383 /* don't bother with a request object! */
2384 writel (0, &dev->epregs [0].ep_irqenb);
2385 set_fifo_bytecount (ep, u.r.wLength);
2386 writel (status, &dev->epregs [0].ep_data);
2388 VDEBUG (dev, "%s stat %02x\n", ep->ep.name, status);
2389 goto next_endpoints;
2392 case USB_REQ_CLEAR_FEATURE: {
2393 struct net2280_ep *e;
2395 /* hw handles device features */
2396 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
2398 if (u.r.wValue != 0 /* HALT feature */
2399 || u.r.wLength != 0)
2401 if ((e = get_ep_by_addr (dev, u.r.wIndex)) == 0)
2405 VDEBUG (dev, "%s clear halt\n", ep->ep.name);
2406 goto next_endpoints;
2409 case USB_REQ_SET_FEATURE: {
2410 struct net2280_ep *e;
2412 /* hw handles device features */
2413 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
2415 if (u.r.wValue != 0 /* HALT feature */
2416 || u.r.wLength != 0)
2418 if ((e = get_ep_by_addr (dev, u.r.wIndex)) == 0)
2422 VDEBUG (dev, "%s set halt\n", ep->ep.name);
2423 goto next_endpoints;
2428 VDEBUG (dev, "setup %02x.%02x v%04x i%04x "
2430 u.r.bRequestType, u.r.bRequest,
2431 u.r.wValue, u.r.wIndex,
2432 readl (&ep->regs->ep_cfg));
2433 spin_unlock (&dev->lock);
2434 tmp = dev->driver->setup (&dev->gadget, &u.r);
2435 spin_lock (&dev->lock);
2438 /* stall ep0 on error */
2441 VDEBUG (dev, "req %02x.%02x protocol STALL; stat %d\n",
2442 u.r.bRequestType, u.r.bRequest, tmp);
2443 dev->protocol_stall = 1;
2446 /* some in/out token irq should follow; maybe stall then.
2447 * driver must queue a request (even zlp) or halt ep0
2448 * before the host times out.
2453 /* endpoint data irq ? */
2454 scratch = stat & 0x7f;
2456 for (num = 0; scratch; num++) {
2459 /* do this endpoint's FIFO and queue need tending? */
2461 if ((scratch & t) == 0)
2465 ep = &dev->ep [num];
2466 handle_ep_small (ep);
2470 DEBUG (dev, "unhandled irqstat0 %08x\n", stat);
2473 #define DMA_INTERRUPTS ( \
2474 (1 << DMA_D_INTERRUPT) \
2475 | (1 << DMA_C_INTERRUPT) \
2476 | (1 << DMA_B_INTERRUPT) \
2477 | (1 << DMA_A_INTERRUPT))
2478 #define PCI_ERROR_INTERRUPTS ( \
2479 (1 << PCI_MASTER_ABORT_RECEIVED_INTERRUPT) \
2480 | (1 << PCI_TARGET_ABORT_RECEIVED_INTERRUPT) \
2481 | (1 << PCI_RETRY_ABORT_INTERRUPT))
2483 static void handle_stat1_irqs (struct net2280 *dev, u32 stat)
2485 struct net2280_ep *ep;
2486 u32 tmp, num, scratch;
2488 /* after disconnect there's nothing else to do! */
2489 tmp = (1 << VBUS_INTERRUPT) | (1 << ROOT_PORT_RESET_INTERRUPT);
2491 writel (tmp, &dev->regs->irqstat1);
2492 if (((stat & (1 << ROOT_PORT_RESET_INTERRUPT)) != 0
2493 || (readl (&dev->usb->usbctl) & (1 << VBUS_PIN)) == 0
2494 ) && dev->gadget.speed != USB_SPEED_UNKNOWN) {
2495 DEBUG (dev, "disconnect %s\n",
2496 dev->driver->driver.name);
2497 stop_activity (dev, dev->driver);
2503 /* vBUS can bounce ... one of many reasons to ignore the
2504 * notion of hotplug events on bus connect/disconnect!
2510 /* NOTE: we don't actually suspend the hardware; that starts to
2511 * interact with PCI power management, and needs something like a
2512 * controller->suspend() call to clear SUSPEND_REQUEST_INTERRUPT.
2513 * we shouldn't see resume interrupts.
2514 * for rev 0100, this also avoids erratum 0102.
2516 tmp = (1 << SUSPEND_REQUEST_CHANGE_INTERRUPT);
2518 if (dev->driver->suspend)
2519 dev->driver->suspend (&dev->gadget);
2522 stat &= ~(1 << SUSPEND_REQUEST_INTERRUPT);
2524 /* clear any other status/irqs */
2526 writel (stat, &dev->regs->irqstat1);
2528 /* some status we can just ignore */
2529 stat &= ~((1 << CONTROL_STATUS_INTERRUPT)
2530 | (1 << RESUME_INTERRUPT)
2531 | (1 << SOF_INTERRUPT));
2534 // DEBUG (dev, "irqstat1 %08x\n", stat);
2536 /* DMA status, for ep-{a,b,c,d} */
2537 scratch = stat & DMA_INTERRUPTS;
2538 stat &= ~DMA_INTERRUPTS;
2540 for (num = 0; scratch; num++) {
2541 struct net2280_dma_regs *dma;
2544 if ((tmp & scratch) == 0)
2548 ep = &dev->ep [num + 1];
2554 /* clear ep's dma status */
2555 tmp = readl (&dma->dmastat);
2556 writel (tmp, &dma->dmastat);
2558 /* chaining should stop on abort, short OUT from fifo,
2559 * or (stat0 codepath) short OUT transfer.
2561 if (!use_dma_chaining) {
2562 if ((tmp & (1 << DMA_TRANSACTION_DONE_INTERRUPT))
2564 DEBUG (ep->dev, "%s no xact done? %08x\n",
2571 /* OUT transfers terminate when the data from the
2572 * host is in our memory. Process whatever's done.
2573 * On this path, we know transfer's last packet wasn't
2574 * less than req->length. NAK_OUT_PACKETS may be set,
2575 * or the FIFO may already be holding new packets.
2577 * IN transfers can linger in the FIFO for a very
2578 * long time ... we ignore that for now, accounting
2579 * precisely (like PIO does) needs per-packet irqs
2581 scan_dma_completions (ep);
2583 /* disable dma on inactive queues; else maybe restart */
2584 if (list_empty (&ep->queue)) {
2585 if (use_dma_chaining)
2588 tmp = readl (&dma->dmactl);
2589 if (!use_dma_chaining
2590 || (tmp & (1 << DMA_ENABLE)) == 0)
2592 else if (ep->is_in && use_dma_chaining) {
2593 struct net2280_request *req;
2596 /* the descriptor at the head of the chain
2597 * may still have VALID_BIT clear; that's
2598 * used to trigger changing DMA_FIFO_VALIDATE
2599 * (affects automagic zlp writes).
2601 req = list_entry (ep->queue.next,
2602 struct net2280_request, queue);
2603 dmacount = req->td->dmacount;
2604 dmacount &= __constant_cpu_to_le32 (
2606 | DMA_BYTE_COUNT_MASK);
2607 if (dmacount && (dmacount & valid_bit) == 0)
2614 /* NOTE: there are other PCI errors we might usefully notice.
2615 * if they appear very often, here's where to try recovering.
2617 if (stat & PCI_ERROR_INTERRUPTS) {
2618 ERROR (dev, "pci dma error; stat %08x\n", stat);
2619 stat &= ~PCI_ERROR_INTERRUPTS;
2620 /* these are fatal errors, but "maybe" they won't
2623 stop_activity (dev, dev->driver);
2629 DEBUG (dev, "unhandled irqstat1 %08x\n", stat);
2632 static irqreturn_t net2280_irq (int irq, void *_dev, struct pt_regs * r)
2634 struct net2280 *dev = _dev;
2636 spin_lock (&dev->lock);
2638 /* handle disconnect, dma, and more */
2639 handle_stat1_irqs (dev, readl (&dev->regs->irqstat1));
2641 /* control requests and PIO */
2642 handle_stat0_irqs (dev, readl (&dev->regs->irqstat0));
2644 spin_unlock (&dev->lock);
2649 /*-------------------------------------------------------------------------*/
2651 /* tear down the binding between this driver and the pci device */
2653 static void net2280_remove (struct pci_dev *pdev)
2655 struct net2280 *dev = pci_get_drvdata (pdev);
2657 /* start with the driver above us */
2659 /* should have been done already by driver model core */
2660 WARN (dev, "pci remove, driver '%s' is still registered\n",
2661 dev->driver->driver.name);
2662 usb_gadget_unregister_driver (dev->driver);
2665 /* then clean up the resources we allocated during probe() */
2666 net2280_led_shutdown (dev);
2667 if (dev->requests) {
2669 for (i = 1; i < 5; i++) {
2670 if (!dev->ep [i].dummy)
2672 pci_pool_free (dev->requests, dev->ep [i].dummy,
2673 dev->ep [i].td_dma);
2675 pci_pool_destroy (dev->requests);
2678 free_irq (pdev->irq, dev);
2680 iounmap (dev->regs);
2682 release_mem_region (pci_resource_start (pdev, 0),
2683 pci_resource_len (pdev, 0));
2685 pci_disable_device (pdev);
2686 pci_set_drvdata (pdev, 0);
2688 INFO (dev, "unbind from pci %s\n", pdev->slot_name);
2694 /* wrap this driver around the specified device, but
2695 * don't respond over USB until a gadget driver binds to us.
2698 static int net2280_probe (struct pci_dev *pdev, const struct pci_device_id *id)
2700 struct net2280 *dev;
2701 unsigned long resource, len;
2704 char buf [8], *bufp;
2706 /* if you want to support more than one controller in a system,
2707 * usb_gadget_driver_{register,unregister}() must change.
2709 if (the_controller) {
2710 WARN (the_controller, "ignoring %s\n", pdev->slot_name);
2714 /* alloc, and start init */
2715 dev = kmalloc (sizeof *dev, SLAB_KERNEL);
2721 memset (dev, 0, sizeof *dev);
2722 spin_lock_init (&dev->lock);
2724 dev->gadget.ops = &net2280_ops;
2725 dev->gadget.is_dualspeed = 1;
2727 dev->gadget.dev.bus_id = pdev->slot_name;
2728 dev->gadget.name = driver_name;
2730 /* now all the pci goodies ... */
2731 if (pci_enable_device (pdev) < 0) {
2737 /* BAR 0 holds all the registers
2738 * BAR 1 is 8051 memory; unused here (note erratum 0103)
2739 * BAR 2 is fifo memory; unused here
2741 resource = pci_resource_start (pdev, 0);
2742 len = pci_resource_len (pdev, 0);
2743 if (!request_mem_region (resource, len, driver_name)) {
2744 DEBUG (dev, "controller already in use\n");
2750 base = ioremap_nocache (resource, len);
2752 DEBUG (dev, "can't map memory\n");
2756 dev->regs = (struct net2280_regs *) base;
2757 dev->usb = (struct net2280_usb_regs *) (base + 0x0080);
2758 dev->pci = (struct net2280_pci_regs *) (base + 0x0100);
2759 dev->dma = (struct net2280_dma_regs *) (base + 0x0180);
2760 dev->dep = (struct net2280_dep_regs *) (base + 0x0200);
2761 dev->epregs = (struct net2280_ep_regs *) (base + 0x0300);
2763 /* put into initial config, link up all endpoints */
2767 /* irq setup after old hardware is cleaned up */
2769 ERROR (dev, "No IRQ. Check PCI setup!\n");
2774 snprintf (buf, sizeof buf, "%d", pdev->irq);
2777 bufp = __irq_itoa(pdev->irq);
2779 if (request_irq (pdev->irq, net2280_irq, SA_SHIRQ, driver_name, dev)
2781 ERROR (dev, "request interrupt %s failed\n", bufp);
2788 dev->requests = pci_pool_create ("requests", pdev,
2789 sizeof (struct net2280_dma),
2790 0 /* no alignment requirements */,
2791 0 /* or page-crossing issues */,
2792 SLAB_KERNEL /* 2.4 only */ );
2793 if (!dev->requests) {
2794 DEBUG (dev, "can't get request pool\n");
2798 for (i = 1; i < 5; i++) {
2799 struct net2280_dma *td;
2801 td = pci_pool_alloc (dev->requests, GFP_KERNEL,
2802 &dev->ep [i].td_dma);
2804 DEBUG (dev, "can't get dummy %d\n", i);
2808 td->dmacount = 0; /* not VALID */
2809 td->dmaaddr = __constant_cpu_to_le32 (DMA_ADDR_INVALID);
2810 td->dmadesc = td->dmaaddr;
2811 dev->ep [i].dummy = td;
2814 /* enable lower-overhead pci memory bursts during DMA */
2815 writel ( (1 << DMA_MEMORY_WRITE_AND_INVALIDATE_ENABLE)
2816 // 256 write retries may not be enough...
2817 // | (1 << PCI_RETRY_ABORT_ENABLE)
2818 | (1 << DMA_READ_MULTIPLE_ENABLE)
2819 | (1 << DMA_READ_LINE_ENABLE)
2820 , &dev->pci->pcimstctl);
2821 /* erratum 0115 shouldn't appear: Linux inits PCI_LATENCY_TIMER */
2822 pci_set_master (pdev);
2825 /* ... also flushes any posted pci writes */
2826 dev->chiprev = get_idx_reg (dev->regs, REG_CHIPREV) & 0xffff;
2829 pci_set_drvdata (pdev, dev);
2830 INFO (dev, "%s\n", driver_desc);
2831 INFO (dev, "irq %s, pci mem %p, chip rev %04x\n",
2832 bufp, base, dev->chiprev);
2833 INFO (dev, "version: " DRIVER_VERSION "; dma %s\n",
2835 ? (use_dma_chaining ? "chaining" : "enabled")
2837 the_controller = dev;
2843 net2280_remove (pdev);
2848 /*-------------------------------------------------------------------------*/
2850 static struct pci_device_id pci_ids [] = { {
2851 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
2855 .subvendor = PCI_ANY_ID,
2856 .subdevice = PCI_ANY_ID,
2858 }, { /* end: all zeroes */ }
2860 MODULE_DEVICE_TABLE (pci, pci_ids);
2862 /* pci driver glue; this is a "new style" PCI driver module */
2863 static struct pci_driver net2280_pci_driver = {
2864 .name = (char *) driver_name,
2865 .id_table = pci_ids,
2867 .probe = net2280_probe,
2868 .remove = net2280_remove,
2870 /* FIXME add power management support */
2873 MODULE_DESCRIPTION (DRIVER_DESC);
2874 MODULE_AUTHOR ("David Brownell");
2875 MODULE_LICENSE ("GPL");
2877 static int __init init (void)
2880 use_dma_chaining = 0;
2881 return pci_module_init (&net2280_pci_driver);
2885 static void __exit cleanup (void)
2887 pci_unregister_driver (&net2280_pci_driver);
2889 module_exit (cleanup);