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>
64 #include <linux/moduleparam.h>
65 #include <linux/device.h>
66 #include <linux/usb_ch9.h>
67 #include <linux/usb_gadget.h>
69 #include <asm/byteorder.h>
72 #include <asm/system.h>
73 #include <asm/unaligned.h>
76 #define DRIVER_DESC "NetChip 2280 USB Peripheral Controller"
77 #define DRIVER_VERSION "2004 Jan 14"
79 #define DMA_ADDR_INVALID (~(dma_addr_t)0)
80 #define EP_DONTUSE 13 /* nonzero */
82 #define USE_RDK_LEDS /* GPIO pins control three LEDs */
83 #define USE_SYSFS_DEBUG_FILES
86 static const char driver_name [] = "net2280";
87 static const char driver_desc [] = DRIVER_DESC;
89 static const char ep0name [] = "ep0";
90 static const char *ep_name [] = {
92 "ep-a", "ep-b", "ep-c", "ep-d",
96 /* use_dma -- general goodness, fewer interrupts, less cpu load (vs PIO)
97 * use_dma_chaining -- dma descriptor queueing gives even more irq reduction
99 * The net2280 DMA engines are not tightly integrated with their FIFOs;
100 * not all cases are (yet) handled well in this driver or the silicon.
101 * Some gadget drivers work better with the dma support here than others.
102 * These two parameters let you use PIO or more aggressive DMA.
104 static int use_dma = 1;
105 static int use_dma_chaining = 0;
107 /* "modprobe net2280 use_dma=n" etc */
108 module_param (use_dma, bool, S_IRUGO);
109 module_param (use_dma_chaining, bool, S_IRUGO);
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 /* "modprobe net2280 fifo_mode=1" etc */
119 module_param (fifo_mode, ushort, 0644);
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 /* the main problem with this call is that it wastes memory
476 * on typical 1/N page allocations: it allocates 1-N pages.
478 #warning Using dma_alloc_coherent even with buffers smaller than a page.
479 retval = dma_alloc_coherent(&ep->dev->pdev->dev,
480 bytes, dma, gfp_flags);
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 dma_free_coherent(&ep->dev->pdev->dev, bytes, buf, dma);
508 /*-------------------------------------------------------------------------*/
510 /* load a packet into the fifo we use for usb IN transfers.
511 * works for all endpoints.
513 * NOTE: pio with ep-a..ep-d could stuff multiple packets into the fifo
514 * at a time, but this code is simpler because it knows it only writes
515 * one packet. ep-a..ep-d should use dma instead.
518 write_fifo (struct net2280_ep *ep, struct usb_request *req)
520 struct net2280_ep_regs *regs = ep->regs;
523 unsigned count, total;
525 /* INVARIANT: fifo is currently empty. (testable) */
528 buf = req->buf + req->actual;
530 total = req->length - req->actual;
536 /* write just one packet at a time */
537 count = ep->ep.maxpacket;
538 if (count > total) /* min() cannot be used on a bitfield */
541 VDEBUG (ep->dev, "write %s fifo (IN) %d bytes%s req %p\n",
543 (count != ep->ep.maxpacket) ? " (short)" : "",
546 /* NOTE be careful if you try to align these. fifo lines
547 * should normally be full (4 bytes) and successive partial
548 * lines are ok only in certain cases.
550 tmp = get_unaligned ((u32 *)buf);
552 writel (tmp, ®s->ep_data);
557 /* last fifo entry is "short" unless we wrote a full packet.
558 * also explicitly validate last word in (periodic) transfers
559 * when maxpacket is not a multiple of 4 bytes.
561 if (count || total < ep->ep.maxpacket) {
562 tmp = count ? get_unaligned ((u32 *)buf) : count;
564 set_fifo_bytecount (ep, count & 0x03);
565 writel (tmp, ®s->ep_data);
568 /* pci writes may still be posted */
571 /* work around erratum 0106: PCI and USB race over the OUT fifo.
572 * caller guarantees chiprev 0100, out endpoint is NAKing, and
573 * there's no real data in the fifo.
575 * NOTE: also used in cases where that erratum doesn't apply:
576 * where the host wrote "too much" data to us.
578 static void out_flush (struct net2280_ep *ep)
582 ASSERT_OUT_NAKING (ep);
584 statp = &ep->regs->ep_stat;
585 writel ( (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
586 | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
588 writel ((1 << FIFO_FLUSH), statp);
591 if (tmp & (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
592 /* high speed did bulk NYET; fifo isn't filling */
593 && ep->dev->gadget.speed == USB_SPEED_FULL) {
596 usec = 50; /* 64 byte bulk/interrupt */
597 handshake (statp, (1 << USB_OUT_PING_NAK_SENT),
598 (1 << USB_OUT_PING_NAK_SENT), usec);
599 /* NAK done; now CLEAR_NAK_OUT_PACKETS is safe */
603 /* unload packet(s) from the fifo we use for usb OUT transfers.
604 * returns true iff the request completed, because of short packet
605 * or the request buffer having filled with full packets.
607 * for ep-a..ep-d this will read multiple packets out when they
608 * have been accepted.
611 read_fifo (struct net2280_ep *ep, struct net2280_request *req)
613 struct net2280_ep_regs *regs = ep->regs;
614 u8 *buf = req->req.buf + req->req.actual;
615 unsigned count, tmp, is_short;
616 unsigned cleanup = 0, prevent = 0;
618 /* erratum 0106 ... packets coming in during fifo reads might
619 * be incompletely rejected. not all cases have workarounds.
621 if (ep->dev->chiprev == 0x0100
622 && ep->dev->gadget.speed == USB_SPEED_FULL) {
624 tmp = readl (&ep->regs->ep_stat);
625 if ((tmp & (1 << NAK_OUT_PACKETS)))
627 else if ((tmp & (1 << FIFO_FULL))) {
628 start_out_naking (ep);
631 /* else: hope we don't see the problem */
634 /* never overflow the rx buffer. the fifo reads packets until
635 * it sees a short one; we might not be ready for them all.
638 count = readl (®s->ep_avail);
639 if (unlikely (count == 0)) {
641 tmp = readl (&ep->regs->ep_stat);
642 count = readl (®s->ep_avail);
643 /* handled that data already? */
644 if (count == 0 && (tmp & (1 << NAK_OUT_PACKETS)) == 0)
648 tmp = req->req.length - req->req.actual;
650 /* as with DMA, data overflow gets flushed */
651 if ((tmp % ep->ep.maxpacket) != 0) {
653 "%s out fifo %d bytes, expected %d\n",
654 ep->ep.name, count, tmp);
655 req->req.status = -EOVERFLOW;
657 /* NAK_OUT_PACKETS will be set, so flushing is safe;
658 * the next read will start with the next packet
660 } /* else it's a ZLP, no worries */
663 req->req.actual += count;
665 is_short = (count == 0) || ((count % ep->ep.maxpacket) != 0);
667 VDEBUG (ep->dev, "read %s fifo (OUT) %d bytes%s%s%s req %p %d/%d\n",
668 ep->ep.name, count, is_short ? " (short)" : "",
669 cleanup ? " flush" : "", prevent ? " nak" : "",
670 req, req->req.actual, req->req.length);
673 tmp = readl (®s->ep_data);
675 put_unaligned (tmp, (u32 *)buf);
680 tmp = readl (®s->ep_data);
690 writel ((1 << CLEAR_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
691 (void) readl (&ep->regs->ep_rsp);
694 return is_short || ((req->req.actual == req->req.length)
698 /* fill out dma descriptor to match a given request */
700 fill_dma_desc (struct net2280_ep *ep, struct net2280_request *req, int valid)
702 struct net2280_dma *td = req->td;
703 u32 dmacount = req->req.length;
705 /* don't let DMA continue after a short OUT packet,
706 * so overruns can't affect the next transfer.
707 * in case of overruns on max-size packets, we can't
708 * stop the fifo from filling but we can flush it.
711 dmacount |= (1 << DMA_DIRECTION);
712 else if ((dmacount % ep->ep.maxpacket) != 0)
713 dmacount |= (1 << END_OF_CHAIN);
717 dmacount |= (1 << VALID_BIT);
718 if (likely(!req->req.no_interrupt || !use_dma_chaining))
719 dmacount |= (1 << DMA_DONE_INTERRUPT_ENABLE);
721 /* td->dmadesc = previously set by caller */
722 td->dmaaddr = cpu_to_le32p (&req->req.dma);
724 /* 2280 may be polling VALID_BIT through ep->dma->dmadesc */
726 td->dmacount = cpu_to_le32p (&dmacount);
729 static const u32 dmactl_default =
730 (1 << DMA_SCATTER_GATHER_DONE_INTERRUPT)
731 | (1 << DMA_CLEAR_COUNT_ENABLE)
732 /* erratum 0116 workaround part 1 (use POLLING) */
733 | (POLL_100_USEC << DESCRIPTOR_POLLING_RATE)
734 | (1 << DMA_VALID_BIT_POLLING_ENABLE)
735 | (1 << DMA_VALID_BIT_ENABLE)
736 | (1 << DMA_SCATTER_GATHER_ENABLE)
737 /* erratum 0116 workaround part 2 (no AUTOSTART) */
740 static inline void spin_stop_dma (struct net2280_dma_regs *dma)
742 handshake (&dma->dmactl, (1 << DMA_ENABLE), 0, 50);
745 static inline void stop_dma (struct net2280_dma_regs *dma)
747 writel (readl (&dma->dmactl) & ~(1 << DMA_ENABLE), &dma->dmactl);
751 static void start_queue (struct net2280_ep *ep, u32 dmactl, u32 td_dma)
753 struct net2280_dma_regs *dma = ep->dma;
755 writel ((1 << VALID_BIT) | (ep->is_in << DMA_DIRECTION),
757 writel (readl (&dma->dmastat), &dma->dmastat);
759 writel (td_dma, &dma->dmadesc);
760 writel (dmactl, &dma->dmactl);
762 /* erratum 0116 workaround part 3: pci arbiter away from net2280 */
763 (void) readl (&ep->dev->pci->pcimstctl);
765 writel ((1 << DMA_START), &dma->dmastat);
768 stop_out_naking (ep);
771 static void start_dma (struct net2280_ep *ep, struct net2280_request *req)
774 struct net2280_dma_regs *dma = ep->dma;
776 /* FIXME can't use DMA for ZLPs */
778 /* on this path we "know" there's no dma active (yet) */
779 WARN_ON (readl (&dma->dmactl) & (1 << DMA_ENABLE));
780 writel (0, &ep->dma->dmactl);
782 /* previous OUT packet might have been short */
783 if (!ep->is_in && ((tmp = readl (&ep->regs->ep_stat))
784 & (1 << NAK_OUT_PACKETS)) != 0) {
785 writel ((1 << SHORT_PACKET_TRANSFERRED_INTERRUPT),
788 tmp = readl (&ep->regs->ep_avail);
790 writel (readl (&dma->dmastat), &dma->dmastat);
792 /* transfer all/some fifo data */
793 writel (req->req.dma, &dma->dmaaddr);
794 tmp = min (tmp, req->req.length);
796 /* dma irq, faking scatterlist status */
797 req->td->dmacount = cpu_to_le32 (req->req.length - tmp);
798 writel ((1 << DMA_DONE_INTERRUPT_ENABLE)
799 | tmp, &dma->dmacount);
800 req->td->dmadesc = 0;
803 writel ((1 << DMA_ENABLE), &dma->dmactl);
804 writel ((1 << DMA_START), &dma->dmastat);
809 tmp = dmactl_default;
811 /* force packet boundaries between dma requests, but prevent the
812 * controller from automagically writing a last "short" packet
813 * (zero length) unless the driver explicitly said to do that.
816 if (likely ((req->req.length % ep->ep.maxpacket) != 0
818 tmp |= (1 << DMA_FIFO_VALIDATE);
819 ep->in_fifo_validate = 1;
821 ep->in_fifo_validate = 0;
824 /* init req->td, pointing to the current dummy */
825 req->td->dmadesc = cpu_to_le32 (ep->td_dma);
826 fill_dma_desc (ep, req, 1);
828 if (!use_dma_chaining)
829 req->td->dmacount |= __constant_cpu_to_le32 (1 << END_OF_CHAIN);
831 start_queue (ep, tmp, req->td_dma);
835 queue_dma (struct net2280_ep *ep, struct net2280_request *req, int valid)
837 struct net2280_dma *end;
840 /* swap new dummy for old, link; fill and maybe activate */
846 ep->td_dma = req->td_dma;
849 end->dmadesc = cpu_to_le32 (ep->td_dma);
851 fill_dma_desc (ep, req, valid);
855 done (struct net2280_ep *ep, struct net2280_request *req, int status)
858 unsigned stopped = ep->stopped;
860 list_del_init (&req->queue);
862 if (req->req.status == -EINPROGRESS)
863 req->req.status = status;
865 status = req->req.status;
869 pci_unmap_single (dev->pdev, req->req.dma, req->req.length,
870 ep->is_in ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
871 req->req.dma = DMA_ADDR_INVALID;
875 if (status && status != -ESHUTDOWN)
876 VDEBUG (dev, "complete %s req %p stat %d len %u/%u\n",
877 ep->ep.name, &req->req, status,
878 req->req.actual, req->req.length);
880 /* don't modify queue heads during completion callback */
882 spin_unlock (&dev->lock);
883 req->req.complete (&ep->ep, &req->req);
884 spin_lock (&dev->lock);
885 ep->stopped = stopped;
888 /*-------------------------------------------------------------------------*/
891 net2280_queue (struct usb_ep *_ep, struct usb_request *_req, int gfp_flags)
893 struct net2280_request *req;
894 struct net2280_ep *ep;
898 /* we always require a cpu-view buffer, so that we can
899 * always use pio (as fallback or whatever).
901 req = container_of (_req, struct net2280_request, req);
902 if (!_req || !_req->complete || !_req->buf
903 || !list_empty (&req->queue))
905 if (_req->length > (~0 & DMA_BYTE_COUNT_MASK))
907 ep = container_of (_ep, struct net2280_ep, ep);
908 if (!_ep || (!ep->desc && ep->num != 0))
911 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
914 /* FIXME implement PIO fallback for ZLPs with DMA */
915 if (ep->dma && _req->length == 0)
918 /* set up dma mapping in case the caller didn't */
919 if (ep->dma && _req->dma == DMA_ADDR_INVALID) {
920 _req->dma = pci_map_single (dev->pdev, _req->buf, _req->length,
921 ep->is_in ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
926 VDEBUG (dev, "%s queue req %p, len %d buf %p\n",
927 _ep->name, _req, _req->length, _req->buf);
930 spin_lock_irqsave (&dev->lock, flags);
932 _req->status = -EINPROGRESS;
935 /* kickstart this i/o queue? */
936 if (list_empty (&ep->queue) && !ep->stopped) {
937 /* use DMA if the endpoint supports it, else pio */
941 /* maybe there's no control data, just status ack */
942 if (ep->num == 0 && _req->length == 0) {
945 VDEBUG (dev, "%s status ack\n", ep->ep.name);
949 /* PIO ... stuff the fifo, or unblock it. */
951 write_fifo (ep, _req);
952 else if (list_empty (&ep->queue)) {
955 /* OUT FIFO might have packet(s) buffered */
956 s = readl (&ep->regs->ep_stat);
957 if ((s & (1 << FIFO_EMPTY)) == 0) {
958 /* note: _req->short_not_ok is
959 * ignored here since PIO _always_
960 * stops queue advance here, and
961 * _req->status doesn't change for
962 * short reads (only _req->actual)
964 if (read_fifo (ep, req)) {
971 s = readl (&ep->regs->ep_stat);
974 /* don't NAK, let the fifo fill */
975 if (req && (s & (1 << NAK_OUT_PACKETS)))
976 writel ((1 << CLEAR_NAK_OUT_PACKETS),
981 } else if (ep->dma) {
987 /* preventing magic zlps is per-engine state, not
988 * per-transfer; irq logic must recover hiccups.
990 expect = likely (req->req.zero
991 || (req->req.length % ep->ep.maxpacket) != 0);
992 if (expect != ep->in_fifo_validate)
995 queue_dma (ep, req, valid);
997 } /* else the irq handler advances the queue. */
1000 list_add_tail (&req->queue, &ep->queue);
1002 spin_unlock_irqrestore (&dev->lock, flags);
1004 /* pci writes may still be posted */
1010 struct net2280_ep *ep,
1011 struct net2280_request *req,
1016 req->req.actual = req->req.length - (DMA_BYTE_COUNT_MASK & dmacount);
1017 done (ep, req, status);
1020 static void restart_dma (struct net2280_ep *ep);
1022 static void scan_dma_completions (struct net2280_ep *ep)
1024 /* only look at descriptors that were "naturally" retired,
1025 * so fifo and list head state won't matter
1027 while (!list_empty (&ep->queue)) {
1028 struct net2280_request *req;
1031 req = list_entry (ep->queue.next,
1032 struct net2280_request, queue);
1036 tmp = le32_to_cpup (&req->td->dmacount);
1037 if ((tmp & (1 << VALID_BIT)) != 0)
1040 /* SHORT_PACKET_TRANSFERRED_INTERRUPT handles "usb-short"
1041 * cases where DMA must be aborted; this code handles
1042 * all non-abort DMA completions.
1044 if (unlikely (req->td->dmadesc == 0)) {
1046 tmp = readl (&ep->dma->dmacount);
1047 if (tmp & DMA_BYTE_COUNT_MASK)
1049 /* single transfer mode */
1050 dma_done (ep, req, tmp, 0);
1052 } else if (!ep->is_in
1053 && (req->req.length % ep->ep.maxpacket) != 0) {
1054 tmp = readl (&ep->regs->ep_stat);
1056 /* AVOID TROUBLE HERE by not issuing short reads from
1057 * your gadget driver. That helps avoids errata 0121,
1058 * 0122, and 0124; not all cases trigger the warning.
1060 if ((tmp & (1 << NAK_OUT_PACKETS)) == 0) {
1061 WARN (ep->dev, "%s lost packet sync!\n",
1063 req->req.status = -EOVERFLOW;
1064 } else if ((tmp = readl (&ep->regs->ep_avail)) != 0) {
1065 /* fifo gets flushed later */
1066 ep->out_overflow = 1;
1067 DEBUG (ep->dev, "%s dma, discard %d len %d\n",
1070 req->req.status = -EOVERFLOW;
1073 dma_done (ep, req, tmp, 0);
1077 static void restart_dma (struct net2280_ep *ep)
1079 struct net2280_request *req;
1080 u32 dmactl = dmactl_default;
1084 req = list_entry (ep->queue.next, struct net2280_request, queue);
1086 if (!use_dma_chaining) {
1087 start_dma (ep, req);
1091 /* the 2280 will be processing the queue unless queue hiccups after
1092 * the previous transfer:
1093 * IN: wanted automagic zlp, head doesn't (or vice versa)
1094 * DMA_FIFO_VALIDATE doesn't init from dma descriptors.
1095 * OUT: was "usb-short", we must restart.
1097 if (ep->is_in && !req->valid) {
1098 struct net2280_request *entry, *prev = NULL;
1099 int reqmode, done = 0;
1101 DEBUG (ep->dev, "%s dma hiccup td %p\n", ep->ep.name, req->td);
1102 ep->in_fifo_validate = likely (req->req.zero
1103 || (req->req.length % ep->ep.maxpacket) != 0);
1104 if (ep->in_fifo_validate)
1105 dmactl |= (1 << DMA_FIFO_VALIDATE);
1106 list_for_each_entry (entry, &ep->queue, queue) {
1111 dmacount = entry->td->dmacount;
1113 reqmode = likely (entry->req.zero
1114 || (entry->req.length
1115 % ep->ep.maxpacket) != 0);
1116 if (reqmode == ep->in_fifo_validate) {
1118 dmacount |= valid_bit;
1119 entry->td->dmacount = dmacount;
1123 /* force a hiccup */
1124 prev->td->dmacount |= dma_done_ie;
1129 /* walk the rest of the queue so unlinks behave */
1131 dmacount &= ~valid_bit;
1132 entry->td->dmacount = dmacount;
1137 writel (0, &ep->dma->dmactl);
1138 start_queue (ep, dmactl, req->td_dma);
1141 static void abort_dma (struct net2280_ep *ep)
1143 /* abort the current transfer */
1144 if (likely (!list_empty (&ep->queue))) {
1145 /* FIXME work around errata 0121, 0122, 0124 */
1146 writel ((1 << DMA_ABORT), &ep->dma->dmastat);
1147 spin_stop_dma (ep->dma);
1150 scan_dma_completions (ep);
1153 /* dequeue ALL requests */
1154 static void nuke (struct net2280_ep *ep)
1156 struct net2280_request *req;
1158 /* called with spinlock held */
1162 while (!list_empty (&ep->queue)) {
1163 req = list_entry (ep->queue.next,
1164 struct net2280_request,
1166 done (ep, req, -ESHUTDOWN);
1170 /* dequeue JUST ONE request */
1171 static int net2280_dequeue (struct usb_ep *_ep, struct usb_request *_req)
1173 struct net2280_ep *ep;
1174 struct net2280_request *req;
1175 unsigned long flags;
1179 ep = container_of (_ep, struct net2280_ep, ep);
1180 if (!_ep || (!ep->desc && ep->num != 0) || !_req)
1183 spin_lock_irqsave (&ep->dev->lock, flags);
1184 stopped = ep->stopped;
1186 /* quiesce dma while we patch the queue */
1190 dmactl = readl (&ep->dma->dmactl);
1191 /* WARNING erratum 0127 may kick in ... */
1193 scan_dma_completions (ep);
1196 /* make sure it's still queued on this endpoint */
1197 list_for_each_entry (req, &ep->queue, queue) {
1198 if (&req->req == _req)
1201 if (&req->req != _req) {
1202 spin_unlock_irqrestore (&ep->dev->lock, flags);
1206 /* queue head may be partially complete. */
1207 if (ep->queue.next == &req->queue) {
1209 DEBUG (ep->dev, "unlink (%s) dma\n", _ep->name);
1210 _req->status = -ECONNRESET;
1212 if (likely (ep->queue.next == &req->queue)) {
1213 // NOTE: misreports single-transfer mode
1214 req->td->dmacount = 0; /* invalidate */
1216 readl (&ep->dma->dmacount),
1220 DEBUG (ep->dev, "unlink (%s) pio\n", _ep->name);
1221 done (ep, req, -ECONNRESET);
1225 /* patch up hardware chaining data */
1226 } else if (ep->dma && use_dma_chaining) {
1227 if (req->queue.prev == ep->queue.next) {
1228 writel (le32_to_cpu (req->td->dmadesc),
1230 if (req->td->dmacount & dma_done_ie)
1231 writel (readl (&ep->dma->dmacount)
1233 &ep->dma->dmacount);
1235 struct net2280_request *prev;
1237 prev = list_entry (req->queue.prev,
1238 struct net2280_request, queue);
1239 prev->td->dmadesc = req->td->dmadesc;
1240 if (req->td->dmacount & dma_done_ie)
1241 prev->td->dmacount |= dma_done_ie;
1246 done (ep, req, -ECONNRESET);
1247 ep->stopped = stopped;
1250 /* turn off dma on inactive queues */
1251 if (list_empty (&ep->queue))
1253 else if (!ep->stopped) {
1254 /* resume current request, or start new one */
1256 writel (dmactl, &ep->dma->dmactl);
1258 start_dma (ep, list_entry (ep->queue.next,
1259 struct net2280_request, queue));
1263 spin_unlock_irqrestore (&ep->dev->lock, flags);
1264 return req ? 0 : -EOPNOTSUPP;
1267 /*-------------------------------------------------------------------------*/
1269 static int net2280_fifo_status (struct usb_ep *_ep);
1272 net2280_set_halt (struct usb_ep *_ep, int value)
1274 struct net2280_ep *ep;
1275 unsigned long flags;
1278 ep = container_of (_ep, struct net2280_ep, ep);
1279 if (!_ep || (!ep->desc && ep->num != 0))
1281 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1283 if (ep->desc /* not ep0 */ && (ep->desc->bmAttributes & 0x03)
1284 == USB_ENDPOINT_XFER_ISOC)
1287 spin_lock_irqsave (&ep->dev->lock, flags);
1288 if (!list_empty (&ep->queue))
1290 else if (ep->is_in && value && net2280_fifo_status (_ep) != 0)
1293 VDEBUG (ep->dev, "%s %s halt\n", _ep->name,
1294 value ? "set" : "clear");
1295 /* set/clear, then synch memory views with the device */
1298 ep->dev->protocol_stall = 1;
1303 (void) readl (&ep->regs->ep_rsp);
1305 spin_unlock_irqrestore (&ep->dev->lock, flags);
1311 net2280_fifo_status (struct usb_ep *_ep)
1313 struct net2280_ep *ep;
1316 ep = container_of (_ep, struct net2280_ep, ep);
1317 if (!_ep || (!ep->desc && ep->num != 0))
1319 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1322 avail = readl (&ep->regs->ep_avail) & ((1 << 12) - 1);
1323 if (avail > ep->fifo_size)
1326 avail = ep->fifo_size - avail;
1331 net2280_fifo_flush (struct usb_ep *_ep)
1333 struct net2280_ep *ep;
1335 ep = container_of (_ep, struct net2280_ep, ep);
1336 if (!_ep || (!ep->desc && ep->num != 0))
1338 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1341 writel ((1 << FIFO_FLUSH), &ep->regs->ep_stat);
1342 (void) readl (&ep->regs->ep_rsp);
1345 static struct usb_ep_ops net2280_ep_ops = {
1346 .enable = net2280_enable,
1347 .disable = net2280_disable,
1349 .alloc_request = net2280_alloc_request,
1350 .free_request = net2280_free_request,
1352 .alloc_buffer = net2280_alloc_buffer,
1353 .free_buffer = net2280_free_buffer,
1355 .queue = net2280_queue,
1356 .dequeue = net2280_dequeue,
1358 .set_halt = net2280_set_halt,
1359 .fifo_status = net2280_fifo_status,
1360 .fifo_flush = net2280_fifo_flush,
1363 /*-------------------------------------------------------------------------*/
1365 static int net2280_get_frame (struct usb_gadget *_gadget)
1367 struct net2280 *dev;
1368 unsigned long flags;
1373 dev = container_of (_gadget, struct net2280, gadget);
1374 spin_lock_irqsave (&dev->lock, flags);
1375 retval = get_idx_reg (dev->regs, REG_FRAME) & 0x03ff;
1376 spin_unlock_irqrestore (&dev->lock, flags);
1380 static int net2280_wakeup (struct usb_gadget *_gadget)
1382 struct net2280 *dev;
1384 unsigned long flags;
1388 dev = container_of (_gadget, struct net2280, gadget);
1390 spin_lock_irqsave (&dev->lock, flags);
1391 tmp = readl (&dev->usb->usbctl);
1392 if (tmp & (1 << DEVICE_REMOTE_WAKEUP_ENABLE))
1393 writel (1 << GENERATE_RESUME, &dev->usb->usbstat);
1394 spin_unlock_irqrestore (&dev->lock, flags);
1396 /* pci writes may still be posted */
1400 static int net2280_set_selfpowered (struct usb_gadget *_gadget, int value)
1402 struct net2280 *dev;
1404 unsigned long flags;
1408 dev = container_of (_gadget, struct net2280, gadget);
1410 spin_lock_irqsave (&dev->lock, flags);
1411 tmp = readl (&dev->usb->usbctl);
1413 tmp |= (1 << SELF_POWERED_STATUS);
1415 tmp &= ~(1 << SELF_POWERED_STATUS);
1416 writel (tmp, &dev->usb->usbctl);
1417 spin_unlock_irqrestore (&dev->lock, flags);
1422 static const struct usb_gadget_ops net2280_ops = {
1423 .get_frame = net2280_get_frame,
1424 .wakeup = net2280_wakeup,
1425 .set_selfpowered = net2280_set_selfpowered,
1428 /*-------------------------------------------------------------------------*/
1430 #ifdef USE_SYSFS_DEBUG_FILES
1432 /* "function" sysfs attribute */
1434 show_function (struct device *_dev, char *buf)
1436 struct net2280 *dev = dev_get_drvdata (_dev);
1439 || !dev->driver->function
1440 || strlen (dev->driver->function) > PAGE_SIZE)
1442 return scnprintf (buf, PAGE_SIZE, "%s\n", dev->driver->function);
1444 static DEVICE_ATTR (function, S_IRUGO, show_function, NULL);
1447 show_registers (struct device *_dev, char *buf)
1449 struct net2280 *dev;
1452 unsigned long flags;
1457 dev = dev_get_drvdata (_dev);
1460 spin_lock_irqsave (&dev->lock, flags);
1463 s = dev->driver->driver.name;
1467 /* Main Control Registers */
1468 t = scnprintf (next, size, "%s version " DRIVER_VERSION
1469 ", chiprev %04x, dma %s\n\n"
1470 "devinit %03x fifoctl %08x gadget '%s'\n"
1471 "pci irqenb0 %02x irqenb1 %08x "
1472 "irqstat0 %04x irqstat1 %08x\n",
1473 driver_name, dev->chiprev,
1475 ? (use_dma_chaining ? "chaining" : "enabled")
1477 readl (&dev->regs->devinit),
1478 readl (&dev->regs->fifoctl),
1480 readl (&dev->regs->pciirqenb0),
1481 readl (&dev->regs->pciirqenb1),
1482 readl (&dev->regs->irqstat0),
1483 readl (&dev->regs->irqstat1));
1487 /* USB Control Registers */
1488 t1 = readl (&dev->usb->usbctl);
1489 t2 = readl (&dev->usb->usbstat);
1490 if (t1 & (1 << VBUS_PIN)) {
1491 if (t2 & (1 << HIGH_SPEED))
1493 else if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1497 /* full speed bit (6) not working?? */
1500 t = scnprintf (next, size,
1501 "stdrsp %08x usbctl %08x usbstat %08x "
1502 "addr 0x%02x (%s)\n",
1503 readl (&dev->usb->stdrsp), t1, t2,
1504 readl (&dev->usb->ouraddr), s);
1508 /* PCI Master Control Registers */
1510 /* DMA Control Registers */
1512 /* Configurable EP Control Registers */
1513 for (i = 0; i < 7; i++) {
1514 struct net2280_ep *ep;
1520 t1 = readl (&ep->regs->ep_cfg);
1521 t2 = readl (&ep->regs->ep_rsp) & 0xff;
1522 t = scnprintf (next, size,
1523 "\n%s\tcfg %05x rsp (%02x) %s%s%s%s%s%s%s%s"
1525 ep->ep.name, t1, t2,
1526 (t2 & (1 << CLEAR_NAK_OUT_PACKETS))
1528 (t2 & (1 << CLEAR_EP_HIDE_STATUS_PHASE))
1530 (t2 & (1 << CLEAR_EP_FORCE_CRC_ERROR))
1532 (t2 & (1 << CLEAR_INTERRUPT_MODE))
1533 ? "interrupt " : "",
1534 (t2 & (1<<CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE))
1536 (t2 & (1 << CLEAR_NAK_OUT_PACKETS_MODE))
1538 (t2 & (1 << CLEAR_ENDPOINT_TOGGLE))
1539 ? "DATA1 " : "DATA0 ",
1540 (t2 & (1 << CLEAR_ENDPOINT_HALT))
1542 readl (&ep->regs->ep_irqenb));
1546 t = scnprintf (next, size,
1547 "\tstat %08x avail %04x "
1549 readl (&ep->regs->ep_stat),
1550 readl (&ep->regs->ep_avail),
1551 t1 & 0x0f, DIR_STRING (t1),
1552 type_string (t1 >> 8),
1553 ep->stopped ? "*" : "");
1560 t = scnprintf (next, size,
1561 " dma\tctl %08x stat %08x count %08x\n"
1562 "\taddr %08x desc %08x\n",
1563 readl (&ep->dma->dmactl),
1564 readl (&ep->dma->dmastat),
1565 readl (&ep->dma->dmacount),
1566 readl (&ep->dma->dmaaddr),
1567 readl (&ep->dma->dmadesc));
1573 /* Indexed Registers */
1577 t = scnprintf (next, size, "\nirqs: ");
1580 for (i = 0; i < 7; i++) {
1581 struct net2280_ep *ep;
1586 t = scnprintf (next, size, " %s/%lu", ep->ep.name, ep->irqs);
1591 t = scnprintf (next, size, "\n");
1595 spin_unlock_irqrestore (&dev->lock, flags);
1597 return PAGE_SIZE - size;
1599 static DEVICE_ATTR (registers, S_IRUGO, show_registers, NULL);
1602 show_queues (struct device *_dev, char *buf)
1604 struct net2280 *dev;
1607 unsigned long flags;
1610 dev = dev_get_drvdata (_dev);
1613 spin_lock_irqsave (&dev->lock, flags);
1615 for (i = 0; i < 7; i++) {
1616 struct net2280_ep *ep = &dev->ep [i];
1617 struct net2280_request *req;
1621 const struct usb_endpoint_descriptor *d;
1626 t = d->bEndpointAddress;
1627 t = scnprintf (next, size,
1628 "\n%s (ep%d%s-%s) max %04x %s fifo %d\n",
1629 ep->ep.name, t & USB_ENDPOINT_NUMBER_MASK,
1630 (t & USB_DIR_IN) ? "in" : "out",
1632 switch (d->bmAttributes & 0x03) {
1633 case USB_ENDPOINT_XFER_BULK:
1634 val = "bulk"; break;
1635 case USB_ENDPOINT_XFER_INT:
1636 val = "intr"; break;
1640 le16_to_cpu (d->wMaxPacketSize) & 0x1fff,
1641 ep->dma ? "dma" : "pio", ep->fifo_size
1643 } else /* ep0 should only have one transfer queued */
1644 t = scnprintf (next, size, "ep0 max 64 pio %s\n",
1645 ep->is_in ? "in" : "out");
1646 if (t <= 0 || t > size)
1651 if (list_empty (&ep->queue)) {
1652 t = scnprintf (next, size, "\t(nothing queued)\n");
1653 if (t <= 0 || t > size)
1659 list_for_each_entry (req, &ep->queue, queue) {
1660 if (ep->dma && req->td_dma == readl (&ep->dma->dmadesc))
1661 t = scnprintf (next, size,
1662 "\treq %p len %d/%d "
1663 "buf %p (dmacount %08x)\n",
1664 &req->req, req->req.actual,
1665 req->req.length, req->req.buf,
1666 readl (&ep->dma->dmacount));
1668 t = scnprintf (next, size,
1669 "\treq %p len %d/%d buf %p\n",
1670 &req->req, req->req.actual,
1671 req->req.length, req->req.buf);
1672 if (t <= 0 || t > size)
1678 struct net2280_dma *td;
1681 t = scnprintf (next, size, "\t td %08x "
1682 " count %08x buf %08x desc %08x\n",
1683 req->td_dma, td->dmacount,
1684 td->dmaaddr, td->dmadesc);
1685 if (t <= 0 || t > size)
1694 spin_unlock_irqrestore (&dev->lock, flags);
1695 return PAGE_SIZE - size;
1697 static DEVICE_ATTR (queues, S_IRUGO, show_queues, NULL);
1702 #define device_create_file(a,b) do {} while (0)
1703 #define device_remove_file device_create_file
1707 /*-------------------------------------------------------------------------*/
1709 /* another driver-specific mode might be a request type doing dma
1710 * to/from another device fifo instead of to/from memory.
1713 static void set_fifo_mode (struct net2280 *dev, int mode)
1715 /* keeping high bits preserves BAR2 */
1716 writel ((0xffff << PCI_BASE2_RANGE) | mode, &dev->regs->fifoctl);
1718 /* always ep-{a,b,e,f} ... maybe not ep-c or ep-d */
1719 INIT_LIST_HEAD (&dev->gadget.ep_list);
1720 list_add_tail (&dev->ep [1].ep.ep_list, &dev->gadget.ep_list);
1721 list_add_tail (&dev->ep [2].ep.ep_list, &dev->gadget.ep_list);
1724 list_add_tail (&dev->ep [3].ep.ep_list, &dev->gadget.ep_list);
1725 list_add_tail (&dev->ep [4].ep.ep_list, &dev->gadget.ep_list);
1726 dev->ep [1].fifo_size = dev->ep [2].fifo_size = 1024;
1729 dev->ep [1].fifo_size = dev->ep [2].fifo_size = 2048;
1732 list_add_tail (&dev->ep [3].ep.ep_list, &dev->gadget.ep_list);
1733 dev->ep [1].fifo_size = 2048;
1734 dev->ep [2].fifo_size = 1024;
1737 /* fifo sizes for ep0, ep-c, ep-d, ep-e, and ep-f never change */
1738 list_add_tail (&dev->ep [5].ep.ep_list, &dev->gadget.ep_list);
1739 list_add_tail (&dev->ep [6].ep.ep_list, &dev->gadget.ep_list);
1743 * net2280_set_fifo_mode - change allocation of fifo buffers
1744 * @gadget: access to the net2280 device that will be updated
1745 * @mode: 0 for default, four 1kB buffers (ep-a through ep-d);
1746 * 1 for two 2kB buffers (ep-a and ep-b only);
1747 * 2 for one 2kB buffer (ep-a) and two 1kB ones (ep-b, ep-c).
1749 * returns zero on success, else negative errno. when this succeeds,
1750 * the contents of gadget->ep_list may have changed.
1752 * you may only call this function when endpoints a-d are all disabled.
1753 * use it whenever extra hardware buffering can help performance, such
1754 * as before enabling "high bandwidth" interrupt endpoints that use
1755 * maxpacket bigger than 512 (when double buffering would otherwise
1758 int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode)
1761 struct net2280 *dev;
1763 unsigned long flags;
1767 dev = container_of (gadget, struct net2280, gadget);
1769 spin_lock_irqsave (&dev->lock, flags);
1771 for (i = 1; i <= 4; i++)
1772 if (dev->ep [i].desc) {
1776 if (mode < 0 || mode > 2)
1779 set_fifo_mode (dev, mode);
1780 spin_unlock_irqrestore (&dev->lock, flags);
1784 DEBUG (dev, "fifo: ep-a 2K, ep-b 2K\n");
1786 DEBUG (dev, "fifo: ep-a 2K, ep-b 1K, ep-c 1K\n");
1787 /* else all are 1K */
1791 EXPORT_SYMBOL (net2280_set_fifo_mode);
1793 /*-------------------------------------------------------------------------*/
1795 /* keeping it simple:
1796 * - one bus driver, initted first;
1797 * - one function driver, initted second
1799 * most of the work to support multiple net2280 controllers would
1800 * be to associate this gadget driver (yes?) with all of them, or
1801 * perhaps to bind specific drivers to specific devices.
1804 static struct net2280 *the_controller;
1806 static void usb_reset (struct net2280 *dev)
1810 /* force immediate bus disconnect, and synch through pci */
1811 writel (0, &dev->usb->usbctl);
1812 dev->gadget.speed = USB_SPEED_UNKNOWN;
1813 (void) readl (&dev->usb->usbctl);
1815 net2280_led_init (dev);
1817 /* disable automatic responses, and irqs */
1818 writel (0, &dev->usb->stdrsp);
1819 writel (0, &dev->regs->pciirqenb0);
1820 writel (0, &dev->regs->pciirqenb1);
1822 /* clear old dma and irq state */
1823 for (tmp = 0; tmp < 4; tmp++) {
1824 struct net2280_ep *ep = &dev->ep [tmp + 1];
1829 writel (~0, &dev->regs->irqstat0),
1830 writel (~(1 << SUSPEND_REQUEST_INTERRUPT), &dev->regs->irqstat1),
1832 /* reset, and enable pci */
1833 tmp = readl (&dev->regs->devinit)
1835 | (1 << FIFO_SOFT_RESET)
1836 | (1 << USB_SOFT_RESET)
1837 | (1 << M8051_RESET);
1838 writel (tmp, &dev->regs->devinit);
1840 /* standard fifo and endpoint allocations */
1841 set_fifo_mode (dev, (fifo_mode <= 2) ? fifo_mode : 0);
1844 static void usb_reinit (struct net2280 *dev)
1849 /* use_dma changes are ignored till next device re-init */
1852 /* basic endpoint init */
1853 for (tmp = 0; tmp < 7; tmp++) {
1854 struct net2280_ep *ep = &dev->ep [tmp];
1856 ep->ep.name = ep_name [tmp];
1860 if (tmp > 0 && tmp <= 4) {
1861 ep->fifo_size = 1024;
1863 ep->dma = &dev->dma [tmp - 1];
1866 ep->regs = &dev->epregs [tmp];
1867 ep_reset (dev->regs, ep);
1869 dev->ep [0].ep.maxpacket = 64;
1870 dev->ep [5].ep.maxpacket = 64;
1871 dev->ep [6].ep.maxpacket = 64;
1873 dev->gadget.ep0 = &dev->ep [0].ep;
1874 dev->ep [0].stopped = 0;
1875 INIT_LIST_HEAD (&dev->gadget.ep0->ep_list);
1877 /* we want to prevent lowlevel/insecure access from the USB host,
1878 * but erratum 0119 means this enable bit is ignored
1880 for (tmp = 0; tmp < 5; tmp++)
1881 writel (EP_DONTUSE, &dev->dep [tmp].dep_cfg);
1884 static void ep0_start (struct net2280 *dev)
1886 writel ( (1 << CLEAR_EP_HIDE_STATUS_PHASE)
1887 | (1 << CLEAR_NAK_OUT_PACKETS)
1888 | (1 << CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE)
1889 , &dev->epregs [0].ep_rsp);
1892 * hardware optionally handles a bunch of standard requests
1893 * that the API hides from drivers anyway. have it do so.
1894 * endpoint status/features are handled in software, to
1895 * help pass tests for some dubious behavior.
1897 writel ( (1 << SET_TEST_MODE)
1898 | (1 << SET_ADDRESS)
1899 | (1 << DEVICE_SET_CLEAR_DEVICE_REMOTE_WAKEUP)
1900 | (1 << GET_DEVICE_STATUS)
1901 | (1 << GET_INTERFACE_STATUS)
1902 , &dev->usb->stdrsp);
1903 writel ( (1 << USB_ROOT_PORT_WAKEUP_ENABLE)
1904 | (1 << SELF_POWERED_USB_DEVICE)
1905 /* erratum 0102 workaround */
1906 | ((dev->chiprev == 0100) ? 0 : 1) << SUSPEND_IMMEDIATELY
1907 | (1 << REMOTE_WAKEUP_SUPPORT)
1908 | (1 << USB_DETECT_ENABLE)
1909 | (1 << SELF_POWERED_STATUS)
1910 , &dev->usb->usbctl);
1912 /* enable irqs so we can see ep0 and general operation */
1913 writel ( (1 << SETUP_PACKET_INTERRUPT_ENABLE)
1914 | (1 << ENDPOINT_0_INTERRUPT_ENABLE)
1915 , &dev->regs->pciirqenb0);
1916 writel ( (1 << PCI_INTERRUPT_ENABLE)
1917 | (1 << PCI_MASTER_ABORT_RECEIVED_INTERRUPT_ENABLE)
1918 | (1 << PCI_TARGET_ABORT_RECEIVED_INTERRUPT_ENABLE)
1919 | (1 << PCI_RETRY_ABORT_INTERRUPT_ENABLE)
1920 | (1 << VBUS_INTERRUPT_ENABLE)
1921 | (1 << ROOT_PORT_RESET_INTERRUPT_ENABLE)
1922 | (1 << SUSPEND_REQUEST_CHANGE_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 driver->driver.bus = NULL;
1961 dev->driver = driver;
1962 dev->gadget.dev.driver = &driver->driver;
1963 retval = driver->bind (&dev->gadget);
1965 DEBUG (dev, "bind to driver %s --> %d\n",
1966 driver->driver.name, retval);
1968 dev->gadget.dev.driver = NULL;
1972 device_create_file (&dev->pdev->dev, &dev_attr_function);
1973 device_create_file (&dev->pdev->dev, &dev_attr_queues);
1975 /* ... then enable host detection and ep0; and we're ready
1976 * for set_configuration as well as eventual disconnect.
1978 net2280_led_active (dev, 1);
1981 DEBUG (dev, "%s ready, usbctl %08x stdrsp %08x\n",
1982 driver->driver.name,
1983 readl (&dev->usb->usbctl),
1984 readl (&dev->usb->stdrsp));
1986 /* pci writes may still be posted */
1989 EXPORT_SYMBOL (usb_gadget_register_driver);
1992 stop_activity (struct net2280 *dev, struct usb_gadget_driver *driver)
1996 /* don't disconnect if it's not connected */
1997 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
2000 /* stop hardware; prevent new request submissions;
2001 * and kill any outstanding requests.
2004 for (i = 0; i < 7; i++)
2005 nuke (&dev->ep [i]);
2007 /* report disconnect; the driver is already quiesced */
2009 spin_unlock (&dev->lock);
2010 driver->disconnect (&dev->gadget);
2011 spin_lock (&dev->lock);
2017 int usb_gadget_unregister_driver (struct usb_gadget_driver *driver)
2019 struct net2280 *dev = the_controller;
2020 unsigned long flags;
2024 if (!driver || driver != dev->driver)
2027 spin_lock_irqsave (&dev->lock, flags);
2028 stop_activity (dev, driver);
2029 spin_unlock_irqrestore (&dev->lock, flags);
2031 driver->unbind (&dev->gadget);
2032 dev->gadget.dev.driver = NULL;
2035 net2280_led_active (dev, 0);
2036 device_remove_file (&dev->pdev->dev, &dev_attr_function);
2037 device_remove_file (&dev->pdev->dev, &dev_attr_queues);
2039 DEBUG (dev, "unregistered driver '%s'\n", driver->driver.name);
2042 EXPORT_SYMBOL (usb_gadget_unregister_driver);
2045 /*-------------------------------------------------------------------------*/
2047 /* handle ep0, ep-e, ep-f with 64 byte packets: packet per irq.
2048 * also works for dma-capable endpoints, in pio mode or just
2049 * to manually advance the queue after short OUT transfers.
2051 static void handle_ep_small (struct net2280_ep *ep)
2053 struct net2280_request *req;
2055 /* 0 error, 1 mid-data, 2 done */
2058 if (!list_empty (&ep->queue))
2059 req = list_entry (ep->queue.next,
2060 struct net2280_request, queue);
2064 /* ack all, and handle what we care about */
2065 t = readl (&ep->regs->ep_stat);
2068 VDEBUG (ep->dev, "%s ack ep_stat %08x, req %p\n",
2069 ep->ep.name, t, req ? &req->req : 0);
2071 writel (t & ~(1 << NAK_OUT_PACKETS), &ep->regs->ep_stat);
2073 /* for ep0, monitor token irqs to catch data stage length errors
2074 * and to synchronize on status.
2076 * also, to defer reporting of protocol stalls ... here's where
2077 * data or status first appears, handling stalls here should never
2078 * cause trouble on the host side..
2080 * control requests could be slightly faster without token synch for
2081 * status, but status can jam up that way.
2083 if (unlikely (ep->num == 0)) {
2085 /* status; stop NAKing */
2086 if (t & (1 << DATA_OUT_PING_TOKEN_INTERRUPT)) {
2087 if (ep->dev->protocol_stall) {
2094 /* reply to extra IN data tokens with a zlp */
2095 } else if (t & (1 << DATA_IN_TOKEN_INTERRUPT)) {
2096 if (ep->dev->protocol_stall) {
2100 } else if (!req && ep->stopped)
2101 write_fifo (ep, NULL);
2104 /* status; stop NAKing */
2105 if (t & (1 << DATA_IN_TOKEN_INTERRUPT)) {
2106 if (ep->dev->protocol_stall) {
2111 /* an extra OUT token is an error */
2112 } else if (((t & (1 << DATA_OUT_PING_TOKEN_INTERRUPT))
2114 && req->req.actual == req->req.length)
2116 ep->dev->protocol_stall = 1;
2120 done (ep, req, -EOVERFLOW);
2126 if (unlikely (!req))
2129 /* manual DMA queue advance after short OUT */
2130 if (likely (ep->dma != 0)) {
2131 if (t & (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)) {
2133 int stopped = ep->stopped;
2135 /* TRANSFERRED works around OUT_DONE erratum 0112.
2136 * we expect (N <= maxpacket) bytes; host wrote M.
2137 * iff (M < N) we won't ever see a DMA interrupt.
2140 for (count = 0; ; t = readl (&ep->regs->ep_stat)) {
2142 /* any preceding dma transfers must finish.
2143 * dma handles (M >= N), may empty the queue
2145 scan_dma_completions (ep);
2146 if (unlikely (list_empty (&ep->queue)
2147 || ep->out_overflow)) {
2151 req = list_entry (ep->queue.next,
2152 struct net2280_request, queue);
2154 /* here either (M < N), a "real" short rx;
2155 * or (M == N) and the queue didn't empty
2157 if (likely (t & (1 << FIFO_EMPTY))) {
2158 count = readl (&ep->dma->dmacount);
2159 count &= DMA_BYTE_COUNT_MASK;
2160 if (readl (&ep->dma->dmadesc)
2168 /* stop DMA, leave ep NAKing */
2169 writel ((1 << DMA_ABORT), &ep->dma->dmastat);
2170 spin_stop_dma (ep->dma);
2173 req->td->dmacount = 0;
2174 t = readl (&ep->regs->ep_avail);
2175 dma_done (ep, req, count, t);
2178 /* also flush to prevent erratum 0106 trouble */
2179 if (unlikely (ep->out_overflow
2180 || (ep->dev->chiprev == 0x0100
2181 && ep->dev->gadget.speed
2182 == USB_SPEED_FULL))) {
2184 ep->out_overflow = 0;
2187 /* (re)start dma if needed, stop NAKing */
2188 ep->stopped = stopped;
2189 if (!list_empty (&ep->queue))
2192 DEBUG (ep->dev, "%s dma ep_stat %08x ??\n",
2196 /* data packet(s) received (in the fifo, OUT) */
2197 } else if (t & (1 << DATA_PACKET_RECEIVED_INTERRUPT)) {
2198 if (read_fifo (ep, req) && ep->num != 0)
2201 /* data packet(s) transmitted (IN) */
2202 } else if (t & (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)) {
2205 len = req->req.length - req->req.actual;
2206 if (len > ep->ep.maxpacket)
2207 len = ep->ep.maxpacket;
2208 req->req.actual += len;
2210 /* if we wrote it all, we're usually done */
2211 if (req->req.actual == req->req.length) {
2213 /* wait for control status */
2216 } else if (!req->req.zero || len != ep->ep.maxpacket)
2220 /* there was nothing to do ... */
2221 } else if (mode == 1)
2226 /* stream endpoints often resubmit/unlink in completion */
2229 /* maybe advance queue to next request */
2231 /* NOTE: net2280 could let gadget driver start the
2232 * status stage later. since not all controllers let
2233 * them control that, the api doesn't (yet) allow it.
2239 if (!list_empty (&ep->queue) && !ep->stopped)
2240 req = list_entry (ep->queue.next,
2241 struct net2280_request, queue);
2244 if (req && !ep->is_in)
2245 stop_out_naking (ep);
2249 /* is there a buffer for the next packet?
2250 * for best streaming performance, make sure there is one.
2252 if (req && !ep->stopped) {
2254 /* load IN fifo with next packet (may be zlp) */
2255 if (t & (1 << DATA_PACKET_TRANSMITTED_INTERRUPT))
2256 write_fifo (ep, &req->req);
2260 static struct net2280_ep *
2261 get_ep_by_addr (struct net2280 *dev, u16 wIndex)
2263 struct net2280_ep *ep;
2265 if ((wIndex & USB_ENDPOINT_NUMBER_MASK) == 0)
2266 return &dev->ep [0];
2267 list_for_each_entry (ep, &dev->gadget.ep_list, ep.ep_list) {
2268 u8 bEndpointAddress;
2272 bEndpointAddress = ep->desc->bEndpointAddress;
2273 if ((wIndex ^ bEndpointAddress) & USB_DIR_IN)
2275 if ((wIndex & 0x0f) == (bEndpointAddress & 0x0f))
2281 static void handle_stat0_irqs (struct net2280 *dev, u32 stat)
2283 struct net2280_ep *ep;
2286 /* most of these don't need individual acks */
2287 stat &= ~(1 << INTA_ASSERTED);
2290 // DEBUG (dev, "irqstat0 %04x\n", stat);
2292 /* starting a control request? */
2293 if (unlikely (stat & (1 << SETUP_PACKET_INTERRUPT))) {
2296 struct usb_ctrlrequest r;
2299 struct net2280_request *req;
2301 if (dev->gadget.speed == USB_SPEED_UNKNOWN) {
2302 if (readl (&dev->usb->usbstat) & (1 << HIGH_SPEED))
2303 dev->gadget.speed = USB_SPEED_HIGH;
2305 dev->gadget.speed = USB_SPEED_FULL;
2306 net2280_led_speed (dev, dev->gadget.speed);
2307 DEBUG (dev, "%s speed\n",
2308 (dev->gadget.speed == USB_SPEED_HIGH)
2315 /* make sure any leftover request state is cleared */
2316 stat &= ~(1 << ENDPOINT_0_INTERRUPT);
2317 while (!list_empty (&ep->queue)) {
2318 req = list_entry (ep->queue.next,
2319 struct net2280_request, queue);
2320 done (ep, req, (req->req.actual == req->req.length)
2324 dev->protocol_stall = 0;
2325 writel ( (1 << TIMEOUT)
2326 | (1 << USB_STALL_SENT)
2327 | (1 << USB_IN_NAK_SENT)
2328 | (1 << USB_IN_ACK_RCVD)
2329 | (1 << USB_OUT_PING_NAK_SENT)
2330 | (1 << USB_OUT_ACK_SENT)
2331 | (1 << FIFO_OVERFLOW)
2332 | (1 << FIFO_UNDERFLOW)
2333 | (1 << SHORT_PACKET_OUT_DONE_INTERRUPT)
2334 | (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)
2335 | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
2336 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
2337 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
2338 | (1 << DATA_IN_TOKEN_INTERRUPT)
2339 , &ep->regs->ep_stat);
2340 u.raw [0] = readl (&dev->usb->setup0123);
2341 u.raw [1] = readl (&dev->usb->setup4567);
2343 cpu_to_le32s (&u.raw [0]);
2344 cpu_to_le32s (&u.raw [1]);
2346 le16_to_cpus (&u.r.wValue);
2347 le16_to_cpus (&u.r.wIndex);
2348 le16_to_cpus (&u.r.wLength);
2351 writel (1 << SETUP_PACKET_INTERRUPT, &dev->regs->irqstat0);
2352 stat ^= (1 << SETUP_PACKET_INTERRUPT);
2354 /* watch control traffic at the token level, and force
2355 * synchronization before letting the status stage happen.
2356 * FIXME ignore tokens we'll NAK, until driver responds.
2357 * that'll mean a lot less irqs for some drivers.
2359 ep->is_in = (u.r.bRequestType & USB_DIR_IN) != 0;
2361 scratch = (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
2362 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
2363 | (1 << DATA_IN_TOKEN_INTERRUPT);
2364 stop_out_naking (ep);
2366 scratch = (1 << DATA_PACKET_RECEIVED_INTERRUPT)
2367 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
2368 | (1 << DATA_IN_TOKEN_INTERRUPT);
2369 writel (scratch, &dev->epregs [0].ep_irqenb);
2371 /* we made the hardware handle most lowlevel requests;
2372 * everything else goes uplevel to the gadget code.
2374 switch (u.r.bRequest) {
2375 case USB_REQ_GET_STATUS: {
2376 struct net2280_ep *e;
2379 /* hw handles device and interface status */
2380 if (u.r.bRequestType != (USB_DIR_IN|USB_RECIP_ENDPOINT))
2382 if ((e = get_ep_by_addr (dev, u.r.wIndex)) == 0
2386 if (readl (&e->regs->ep_rsp)
2387 & (1 << SET_ENDPOINT_HALT))
2388 status = __constant_cpu_to_le16 (1);
2390 status = __constant_cpu_to_le16 (0);
2392 /* don't bother with a request object! */
2393 writel (0, &dev->epregs [0].ep_irqenb);
2394 set_fifo_bytecount (ep, u.r.wLength);
2395 writel (status, &dev->epregs [0].ep_data);
2397 VDEBUG (dev, "%s stat %02x\n", ep->ep.name, status);
2398 goto next_endpoints;
2401 case USB_REQ_CLEAR_FEATURE: {
2402 struct net2280_ep *e;
2404 /* hw handles device features */
2405 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
2407 if (u.r.wValue != USB_ENDPOINT_HALT
2408 || u.r.wLength != 0)
2410 if ((e = get_ep_by_addr (dev, u.r.wIndex)) == 0)
2414 VDEBUG (dev, "%s clear halt\n", ep->ep.name);
2415 goto next_endpoints;
2418 case USB_REQ_SET_FEATURE: {
2419 struct net2280_ep *e;
2421 /* hw handles device features */
2422 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
2424 if (u.r.wValue != USB_ENDPOINT_HALT
2425 || u.r.wLength != 0)
2427 if ((e = get_ep_by_addr (dev, u.r.wIndex)) == 0)
2431 VDEBUG (dev, "%s set halt\n", ep->ep.name);
2432 goto next_endpoints;
2437 VDEBUG (dev, "setup %02x.%02x v%04x i%04x "
2439 u.r.bRequestType, u.r.bRequest,
2440 u.r.wValue, u.r.wIndex,
2441 readl (&ep->regs->ep_cfg));
2442 spin_unlock (&dev->lock);
2443 tmp = dev->driver->setup (&dev->gadget, &u.r);
2444 spin_lock (&dev->lock);
2447 /* stall ep0 on error */
2450 VDEBUG (dev, "req %02x.%02x protocol STALL; stat %d\n",
2451 u.r.bRequestType, u.r.bRequest, tmp);
2452 dev->protocol_stall = 1;
2455 /* some in/out token irq should follow; maybe stall then.
2456 * driver must queue a request (even zlp) or halt ep0
2457 * before the host times out.
2462 /* endpoint data irq ? */
2463 scratch = stat & 0x7f;
2465 for (num = 0; scratch; num++) {
2468 /* do this endpoint's FIFO and queue need tending? */
2470 if ((scratch & t) == 0)
2474 ep = &dev->ep [num];
2475 handle_ep_small (ep);
2479 DEBUG (dev, "unhandled irqstat0 %08x\n", stat);
2482 #define DMA_INTERRUPTS ( \
2483 (1 << DMA_D_INTERRUPT) \
2484 | (1 << DMA_C_INTERRUPT) \
2485 | (1 << DMA_B_INTERRUPT) \
2486 | (1 << DMA_A_INTERRUPT))
2487 #define PCI_ERROR_INTERRUPTS ( \
2488 (1 << PCI_MASTER_ABORT_RECEIVED_INTERRUPT) \
2489 | (1 << PCI_TARGET_ABORT_RECEIVED_INTERRUPT) \
2490 | (1 << PCI_RETRY_ABORT_INTERRUPT))
2492 static void handle_stat1_irqs (struct net2280 *dev, u32 stat)
2494 struct net2280_ep *ep;
2495 u32 tmp, num, scratch;
2497 /* after disconnect there's nothing else to do! */
2498 tmp = (1 << VBUS_INTERRUPT) | (1 << ROOT_PORT_RESET_INTERRUPT);
2500 writel (tmp, &dev->regs->irqstat1);
2501 if (((stat & (1 << ROOT_PORT_RESET_INTERRUPT)) != 0
2502 || (readl (&dev->usb->usbctl) & (1 << VBUS_PIN)) == 0
2503 ) && dev->gadget.speed != USB_SPEED_UNKNOWN) {
2504 DEBUG (dev, "disconnect %s\n",
2505 dev->driver->driver.name);
2506 stop_activity (dev, dev->driver);
2512 /* vBUS can bounce ... one of many reasons to ignore the
2513 * notion of hotplug events on bus connect/disconnect!
2519 /* NOTE: chip stays in PCI D0 state for now, but it could
2520 * enter D1 to save more power
2522 tmp = (1 << SUSPEND_REQUEST_CHANGE_INTERRUPT);
2524 writel (tmp, &dev->regs->irqstat1);
2525 if (stat & (1 << SUSPEND_REQUEST_INTERRUPT)) {
2526 if (dev->driver->suspend)
2527 dev->driver->suspend (&dev->gadget);
2528 /* we use SUSPEND_IMMEDIATELY */
2529 stat &= ~(1 << SUSPEND_REQUEST_INTERRUPT);
2531 if (dev->driver->resume)
2532 dev->driver->resume (&dev->gadget);
2533 /* at high speed, note erratum 0133 */
2538 /* clear any other status/irqs */
2540 writel (stat, &dev->regs->irqstat1);
2542 /* some status we can just ignore */
2543 stat &= ~((1 << CONTROL_STATUS_INTERRUPT)
2544 | (1 << SUSPEND_REQUEST_INTERRUPT)
2545 | (1 << RESUME_INTERRUPT)
2546 | (1 << SOF_INTERRUPT));
2549 // DEBUG (dev, "irqstat1 %08x\n", stat);
2551 /* DMA status, for ep-{a,b,c,d} */
2552 scratch = stat & DMA_INTERRUPTS;
2553 stat &= ~DMA_INTERRUPTS;
2555 for (num = 0; scratch; num++) {
2556 struct net2280_dma_regs *dma;
2559 if ((tmp & scratch) == 0)
2563 ep = &dev->ep [num + 1];
2569 /* clear ep's dma status */
2570 tmp = readl (&dma->dmastat);
2571 writel (tmp, &dma->dmastat);
2573 /* chaining should stop on abort, short OUT from fifo,
2574 * or (stat0 codepath) short OUT transfer.
2576 if (!use_dma_chaining) {
2577 if ((tmp & (1 << DMA_TRANSACTION_DONE_INTERRUPT))
2579 DEBUG (ep->dev, "%s no xact done? %08x\n",
2586 /* OUT transfers terminate when the data from the
2587 * host is in our memory. Process whatever's done.
2588 * On this path, we know transfer's last packet wasn't
2589 * less than req->length. NAK_OUT_PACKETS may be set,
2590 * or the FIFO may already be holding new packets.
2592 * IN transfers can linger in the FIFO for a very
2593 * long time ... we ignore that for now, accounting
2594 * precisely (like PIO does) needs per-packet irqs
2596 scan_dma_completions (ep);
2598 /* disable dma on inactive queues; else maybe restart */
2599 if (list_empty (&ep->queue)) {
2600 if (use_dma_chaining)
2603 tmp = readl (&dma->dmactl);
2604 if (!use_dma_chaining
2605 || (tmp & (1 << DMA_ENABLE)) == 0)
2607 else if (ep->is_in && use_dma_chaining) {
2608 struct net2280_request *req;
2611 /* the descriptor at the head of the chain
2612 * may still have VALID_BIT clear; that's
2613 * used to trigger changing DMA_FIFO_VALIDATE
2614 * (affects automagic zlp writes).
2616 req = list_entry (ep->queue.next,
2617 struct net2280_request, queue);
2618 dmacount = req->td->dmacount;
2619 dmacount &= __constant_cpu_to_le32 (
2621 | DMA_BYTE_COUNT_MASK);
2622 if (dmacount && (dmacount & valid_bit) == 0)
2629 /* NOTE: there are other PCI errors we might usefully notice.
2630 * if they appear very often, here's where to try recovering.
2632 if (stat & PCI_ERROR_INTERRUPTS) {
2633 ERROR (dev, "pci dma error; stat %08x\n", stat);
2634 stat &= ~PCI_ERROR_INTERRUPTS;
2635 /* these are fatal errors, but "maybe" they won't
2638 stop_activity (dev, dev->driver);
2644 DEBUG (dev, "unhandled irqstat1 %08x\n", stat);
2647 static irqreturn_t net2280_irq (int irq, void *_dev, struct pt_regs * r)
2649 struct net2280 *dev = _dev;
2651 spin_lock (&dev->lock);
2653 /* handle disconnect, dma, and more */
2654 handle_stat1_irqs (dev, readl (&dev->regs->irqstat1));
2656 /* control requests and PIO */
2657 handle_stat0_irqs (dev, readl (&dev->regs->irqstat0));
2659 spin_unlock (&dev->lock);
2664 /*-------------------------------------------------------------------------*/
2666 static void gadget_release (struct device *_dev)
2668 struct net2280 *dev = dev_get_drvdata (_dev);
2673 /* tear down the binding between this driver and the pci device */
2675 static void net2280_remove (struct pci_dev *pdev)
2677 struct net2280 *dev = pci_get_drvdata (pdev);
2679 /* start with the driver above us */
2681 /* should have been done already by driver model core */
2682 WARN (dev, "pci remove, driver '%s' is still registered\n",
2683 dev->driver->driver.name);
2684 usb_gadget_unregister_driver (dev->driver);
2687 /* then clean up the resources we allocated during probe() */
2688 net2280_led_shutdown (dev);
2689 if (dev->requests) {
2691 for (i = 1; i < 5; i++) {
2692 if (!dev->ep [i].dummy)
2694 pci_pool_free (dev->requests, dev->ep [i].dummy,
2695 dev->ep [i].td_dma);
2697 pci_pool_destroy (dev->requests);
2700 free_irq (pdev->irq, dev);
2702 iounmap (dev->regs);
2704 release_mem_region (pci_resource_start (pdev, 0),
2705 pci_resource_len (pdev, 0));
2707 pci_disable_device (pdev);
2708 device_unregister (&dev->gadget.dev);
2709 device_remove_file (&pdev->dev, &dev_attr_registers);
2710 pci_set_drvdata (pdev, NULL);
2712 INFO (dev, "unbind\n");
2714 the_controller = NULL;
2717 /* wrap this driver around the specified device, but
2718 * don't respond over USB until a gadget driver binds to us.
2721 static int net2280_probe (struct pci_dev *pdev, const struct pci_device_id *id)
2723 struct net2280 *dev;
2724 unsigned long resource, len;
2727 char buf [8], *bufp;
2729 /* if you want to support more than one controller in a system,
2730 * usb_gadget_driver_{register,unregister}() must change.
2732 if (the_controller) {
2733 dev_warn (&pdev->dev, "ignoring\n");
2737 /* alloc, and start init */
2738 dev = kmalloc (sizeof *dev, SLAB_KERNEL);
2744 memset (dev, 0, sizeof *dev);
2745 spin_lock_init (&dev->lock);
2747 dev->gadget.ops = &net2280_ops;
2748 dev->gadget.is_dualspeed = 1;
2750 /* the "gadget" abstracts/virtualizes the controller */
2751 strcpy (dev->gadget.dev.bus_id, "gadget");
2752 dev->gadget.dev.parent = &pdev->dev;
2753 dev->gadget.dev.dma_mask = pdev->dev.dma_mask;
2754 dev->gadget.dev.release = gadget_release;
2755 dev->gadget.name = driver_name;
2757 /* now all the pci goodies ... */
2758 if (pci_enable_device (pdev) < 0) {
2764 /* BAR 0 holds all the registers
2765 * BAR 1 is 8051 memory; unused here (note erratum 0103)
2766 * BAR 2 is fifo memory; unused here
2768 resource = pci_resource_start (pdev, 0);
2769 len = pci_resource_len (pdev, 0);
2770 if (!request_mem_region (resource, len, driver_name)) {
2771 DEBUG (dev, "controller already in use\n");
2777 base = ioremap_nocache (resource, len);
2779 DEBUG (dev, "can't map memory\n");
2783 dev->regs = (struct net2280_regs *) base;
2784 dev->usb = (struct net2280_usb_regs *) (base + 0x0080);
2785 dev->pci = (struct net2280_pci_regs *) (base + 0x0100);
2786 dev->dma = (struct net2280_dma_regs *) (base + 0x0180);
2787 dev->dep = (struct net2280_dep_regs *) (base + 0x0200);
2788 dev->epregs = (struct net2280_ep_regs *) (base + 0x0300);
2790 /* put into initial config, link up all endpoints */
2794 /* irq setup after old hardware is cleaned up */
2796 ERROR (dev, "No IRQ. Check PCI setup!\n");
2801 scnprintf (buf, sizeof buf, "%d", pdev->irq);
2804 bufp = __irq_itoa(pdev->irq);
2806 if (request_irq (pdev->irq, net2280_irq, SA_SHIRQ, driver_name, dev)
2808 ERROR (dev, "request interrupt %s failed\n", bufp);
2815 dev->requests = pci_pool_create ("requests", pdev,
2816 sizeof (struct net2280_dma),
2817 0 /* no alignment requirements */,
2818 0 /* or page-crossing issues */);
2819 if (!dev->requests) {
2820 DEBUG (dev, "can't get request pool\n");
2824 for (i = 1; i < 5; i++) {
2825 struct net2280_dma *td;
2827 td = pci_pool_alloc (dev->requests, GFP_KERNEL,
2828 &dev->ep [i].td_dma);
2830 DEBUG (dev, "can't get dummy %d\n", i);
2834 td->dmacount = 0; /* not VALID */
2835 td->dmaaddr = __constant_cpu_to_le32 (DMA_ADDR_INVALID);
2836 td->dmadesc = td->dmaaddr;
2837 dev->ep [i].dummy = td;
2840 /* enable lower-overhead pci memory bursts during DMA */
2841 writel ( (1 << DMA_MEMORY_WRITE_AND_INVALIDATE_ENABLE)
2842 // 256 write retries may not be enough...
2843 // | (1 << PCI_RETRY_ABORT_ENABLE)
2844 | (1 << DMA_READ_MULTIPLE_ENABLE)
2845 | (1 << DMA_READ_LINE_ENABLE)
2846 , &dev->pci->pcimstctl);
2847 /* erratum 0115 shouldn't appear: Linux inits PCI_LATENCY_TIMER */
2848 pci_set_master (pdev);
2851 /* ... also flushes any posted pci writes */
2852 dev->chiprev = get_idx_reg (dev->regs, REG_CHIPREV) & 0xffff;
2855 pci_set_drvdata (pdev, dev);
2856 INFO (dev, "%s\n", driver_desc);
2857 INFO (dev, "irq %s, pci mem %p, chip rev %04x\n",
2858 bufp, base, dev->chiprev);
2859 INFO (dev, "version: " DRIVER_VERSION "; dma %s\n",
2861 ? (use_dma_chaining ? "chaining" : "enabled")
2863 the_controller = dev;
2865 device_register (&dev->gadget.dev);
2866 device_create_file (&pdev->dev, &dev_attr_registers);
2872 net2280_remove (pdev);
2877 /*-------------------------------------------------------------------------*/
2879 static struct pci_device_id pci_ids [] = { {
2880 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
2884 .subvendor = PCI_ANY_ID,
2885 .subdevice = PCI_ANY_ID,
2887 }, { /* end: all zeroes */ }
2889 MODULE_DEVICE_TABLE (pci, pci_ids);
2891 /* pci driver glue; this is a "new style" PCI driver module */
2892 static struct pci_driver net2280_pci_driver = {
2893 .name = (char *) driver_name,
2894 .id_table = pci_ids,
2896 .probe = net2280_probe,
2897 .remove = net2280_remove,
2899 /* FIXME add power management support */
2902 MODULE_DESCRIPTION (DRIVER_DESC);
2903 MODULE_AUTHOR ("David Brownell");
2904 MODULE_LICENSE ("GPL");
2906 static int __init init (void)
2909 use_dma_chaining = 0;
2910 return pci_module_init (&net2280_pci_driver);
2914 static void __exit cleanup (void)
2916 pci_unregister_driver (&net2280_pci_driver);
2918 module_exit (cleanup);