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 = 0;
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 | (1 << REMOTE_WAKEUP_SUPPORT)
1906 | (1 << USB_DETECT_ENABLE)
1907 | (1 << SELF_POWERED_STATUS)
1908 , &dev->usb->usbctl);
1910 /* enable irqs so we can see ep0 and general operation */
1911 writel ( (1 << SETUP_PACKET_INTERRUPT_ENABLE)
1912 | (1 << ENDPOINT_0_INTERRUPT_ENABLE)
1913 , &dev->regs->pciirqenb0);
1914 writel ( (1 << PCI_INTERRUPT_ENABLE)
1915 | (1 << PCI_MASTER_ABORT_RECEIVED_INTERRUPT_ENABLE)
1916 | (1 << PCI_TARGET_ABORT_RECEIVED_INTERRUPT_ENABLE)
1917 | (1 << PCI_RETRY_ABORT_INTERRUPT_ENABLE)
1918 | (1 << VBUS_INTERRUPT_ENABLE)
1919 | (1 << ROOT_PORT_RESET_INTERRUPT_ENABLE)
1920 , &dev->regs->pciirqenb1);
1922 /* don't leave any writes posted */
1923 (void) readl (&dev->usb->usbctl);
1926 /* when a driver is successfully registered, it will receive
1927 * control requests including set_configuration(), which enables
1928 * non-control requests. then usb traffic follows until a
1929 * disconnect is reported. then a host may connect again, or
1930 * the driver might get unbound.
1932 int usb_gadget_register_driver (struct usb_gadget_driver *driver)
1934 struct net2280 *dev = the_controller;
1938 /* insist on high speed support from the driver, since
1939 * (dev->usb->xcvrdiag & FORCE_FULL_SPEED_MODE)
1940 * "must not be used in normal operation"
1943 || driver->speed != USB_SPEED_HIGH
1953 for (i = 0; i < 7; i++)
1954 dev->ep [i].irqs = 0;
1956 /* hook up the driver ... */
1957 driver->driver.bus = 0;
1958 dev->driver = driver;
1959 dev->gadget.dev.driver = &driver->driver;
1960 retval = driver->bind (&dev->gadget);
1962 DEBUG (dev, "bind to driver %s --> %d\n",
1963 driver->driver.name, retval);
1965 dev->gadget.dev.driver = 0;
1969 device_create_file (&dev->pdev->dev, &dev_attr_function);
1970 device_create_file (&dev->pdev->dev, &dev_attr_queues);
1972 /* ... then enable host detection and ep0; and we're ready
1973 * for set_configuration as well as eventual disconnect.
1975 net2280_led_active (dev, 1);
1978 DEBUG (dev, "%s ready, usbctl %08x stdrsp %08x\n",
1979 driver->driver.name,
1980 readl (&dev->usb->usbctl),
1981 readl (&dev->usb->stdrsp));
1983 /* pci writes may still be posted */
1986 EXPORT_SYMBOL (usb_gadget_register_driver);
1989 stop_activity (struct net2280 *dev, struct usb_gadget_driver *driver)
1993 /* don't disconnect if it's not connected */
1994 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1997 /* stop hardware; prevent new request submissions;
1998 * and kill any outstanding requests.
2001 for (i = 0; i < 7; i++)
2002 nuke (&dev->ep [i]);
2004 /* report disconnect; the driver is already quiesced */
2006 spin_unlock (&dev->lock);
2007 driver->disconnect (&dev->gadget);
2008 spin_lock (&dev->lock);
2014 int usb_gadget_unregister_driver (struct usb_gadget_driver *driver)
2016 struct net2280 *dev = the_controller;
2017 unsigned long flags;
2021 if (!driver || driver != dev->driver)
2024 spin_lock_irqsave (&dev->lock, flags);
2025 stop_activity (dev, driver);
2026 spin_unlock_irqrestore (&dev->lock, flags);
2028 driver->unbind (&dev->gadget);
2029 dev->gadget.dev.driver = 0;
2032 net2280_led_active (dev, 0);
2033 device_remove_file (&dev->pdev->dev, &dev_attr_function);
2034 device_remove_file (&dev->pdev->dev, &dev_attr_queues);
2036 DEBUG (dev, "unregistered driver '%s'\n", driver->driver.name);
2039 EXPORT_SYMBOL (usb_gadget_unregister_driver);
2042 /*-------------------------------------------------------------------------*/
2044 /* handle ep0, ep-e, ep-f with 64 byte packets: packet per irq.
2045 * also works for dma-capable endpoints, in pio mode or just
2046 * to manually advance the queue after short OUT transfers.
2048 static void handle_ep_small (struct net2280_ep *ep)
2050 struct net2280_request *req;
2052 /* 0 error, 1 mid-data, 2 done */
2055 if (!list_empty (&ep->queue))
2056 req = list_entry (ep->queue.next,
2057 struct net2280_request, queue);
2061 /* ack all, and handle what we care about */
2062 t = readl (&ep->regs->ep_stat);
2065 VDEBUG (ep->dev, "%s ack ep_stat %08x, req %p\n",
2066 ep->ep.name, t, req ? &req->req : 0);
2068 writel (t & ~(1 << NAK_OUT_PACKETS), &ep->regs->ep_stat);
2070 /* for ep0, monitor token irqs to catch data stage length errors
2071 * and to synchronize on status.
2073 * also, to defer reporting of protocol stalls ... here's where
2074 * data or status first appears, handling stalls here should never
2075 * cause trouble on the host side..
2077 * control requests could be slightly faster without token synch for
2078 * status, but status can jam up that way.
2080 if (unlikely (ep->num == 0)) {
2082 /* status; stop NAKing */
2083 if (t & (1 << DATA_OUT_PING_TOKEN_INTERRUPT)) {
2084 if (ep->dev->protocol_stall) {
2091 /* reply to extra IN data tokens with a zlp */
2092 } else if (t & (1 << DATA_IN_TOKEN_INTERRUPT)) {
2093 if (ep->dev->protocol_stall) {
2097 } else if (!req && ep->stopped)
2101 /* status; stop NAKing */
2102 if (t & (1 << DATA_IN_TOKEN_INTERRUPT)) {
2103 if (ep->dev->protocol_stall) {
2108 /* an extra OUT token is an error */
2109 } else if (((t & (1 << DATA_OUT_PING_TOKEN_INTERRUPT))
2111 && req->req.actual == req->req.length)
2113 ep->dev->protocol_stall = 1;
2117 done (ep, req, -EOVERFLOW);
2123 if (unlikely (!req))
2126 /* manual DMA queue advance after short OUT */
2127 if (likely (ep->dma != 0)) {
2128 if (t & (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)) {
2130 int stopped = ep->stopped;
2132 /* TRANSFERRED works around OUT_DONE erratum 0112.
2133 * we expect (N <= maxpacket) bytes; host wrote M.
2134 * iff (M < N) we won't ever see a DMA interrupt.
2137 for (count = 0; ; t = readl (&ep->regs->ep_stat)) {
2139 /* any preceding dma transfers must finish.
2140 * dma handles (M >= N), may empty the queue
2142 scan_dma_completions (ep);
2143 if (unlikely (list_empty (&ep->queue)
2144 || ep->out_overflow)) {
2148 req = list_entry (ep->queue.next,
2149 struct net2280_request, queue);
2151 /* here either (M < N), a "real" short rx;
2152 * or (M == N) and the queue didn't empty
2154 if (likely (t & (1 << FIFO_EMPTY))) {
2155 count = readl (&ep->dma->dmacount);
2156 count &= DMA_BYTE_COUNT_MASK;
2157 if (readl (&ep->dma->dmadesc)
2165 /* stop DMA, leave ep NAKing */
2166 writel ((1 << DMA_ABORT), &ep->dma->dmastat);
2167 spin_stop_dma (ep->dma);
2170 req->td->dmacount = 0;
2171 t = readl (&ep->regs->ep_avail);
2172 dma_done (ep, req, count, t);
2175 /* also flush to prevent erratum 0106 trouble */
2176 if (unlikely (ep->out_overflow
2177 || (ep->dev->chiprev == 0x0100
2178 && ep->dev->gadget.speed
2179 == USB_SPEED_FULL))) {
2181 ep->out_overflow = 0;
2184 /* (re)start dma if needed, stop NAKing */
2185 ep->stopped = stopped;
2186 if (!list_empty (&ep->queue))
2189 DEBUG (ep->dev, "%s dma ep_stat %08x ??\n",
2193 /* data packet(s) received (in the fifo, OUT) */
2194 } else if (t & (1 << DATA_PACKET_RECEIVED_INTERRUPT)) {
2195 if (read_fifo (ep, req) && ep->num != 0)
2198 /* data packet(s) transmitted (IN) */
2199 } else if (t & (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)) {
2202 len = req->req.length - req->req.actual;
2203 if (len > ep->ep.maxpacket)
2204 len = ep->ep.maxpacket;
2205 req->req.actual += len;
2207 /* if we wrote it all, we're usually done */
2208 if (req->req.actual == req->req.length) {
2210 /* wait for control status */
2213 } else if (!req->req.zero || len != ep->ep.maxpacket)
2217 /* there was nothing to do ... */
2218 } else if (mode == 1)
2223 /* stream endpoints often resubmit/unlink in completion */
2226 /* maybe advance queue to next request */
2228 /* NOTE: net2280 could let gadget driver start the
2229 * status stage later. since not all controllers let
2230 * them control that, the api doesn't (yet) allow it.
2236 if (!list_empty (&ep->queue) && !ep->stopped)
2237 req = list_entry (ep->queue.next,
2238 struct net2280_request, queue);
2241 if (req && !ep->is_in)
2242 stop_out_naking (ep);
2246 /* is there a buffer for the next packet?
2247 * for best streaming performance, make sure there is one.
2249 if (req && !ep->stopped) {
2251 /* load IN fifo with next packet (may be zlp) */
2252 if (t & (1 << DATA_PACKET_TRANSMITTED_INTERRUPT))
2253 write_fifo (ep, &req->req);
2257 static struct net2280_ep *
2258 get_ep_by_addr (struct net2280 *dev, u16 wIndex)
2260 struct net2280_ep *ep;
2262 if ((wIndex & USB_ENDPOINT_NUMBER_MASK) == 0)
2263 return &dev->ep [0];
2264 list_for_each_entry (ep, &dev->gadget.ep_list, ep.ep_list) {
2265 u8 bEndpointAddress;
2269 bEndpointAddress = ep->desc->bEndpointAddress;
2270 if ((wIndex ^ bEndpointAddress) & USB_DIR_IN)
2272 if ((wIndex & 0x0f) == (bEndpointAddress & 0x0f))
2278 static void handle_stat0_irqs (struct net2280 *dev, u32 stat)
2280 struct net2280_ep *ep;
2283 /* most of these don't need individual acks */
2284 stat &= ~(1 << INTA_ASSERTED);
2287 // DEBUG (dev, "irqstat0 %04x\n", stat);
2289 /* starting a control request? */
2290 if (unlikely (stat & (1 << SETUP_PACKET_INTERRUPT))) {
2293 struct usb_ctrlrequest r;
2296 struct net2280_request *req;
2298 if (dev->gadget.speed == USB_SPEED_UNKNOWN) {
2299 if (readl (&dev->usb->usbstat) & (1 << HIGH_SPEED))
2300 dev->gadget.speed = USB_SPEED_HIGH;
2302 dev->gadget.speed = USB_SPEED_FULL;
2303 net2280_led_speed (dev, dev->gadget.speed);
2304 DEBUG (dev, "%s speed\n",
2305 (dev->gadget.speed == USB_SPEED_HIGH)
2312 /* make sure any leftover request state is cleared */
2313 stat &= ~(1 << ENDPOINT_0_INTERRUPT);
2314 while (!list_empty (&ep->queue)) {
2315 req = list_entry (ep->queue.next,
2316 struct net2280_request, queue);
2317 done (ep, req, (req->req.actual == req->req.length)
2321 dev->protocol_stall = 0;
2322 writel ( (1 << TIMEOUT)
2323 | (1 << USB_STALL_SENT)
2324 | (1 << USB_IN_NAK_SENT)
2325 | (1 << USB_IN_ACK_RCVD)
2326 | (1 << USB_OUT_PING_NAK_SENT)
2327 | (1 << USB_OUT_ACK_SENT)
2328 | (1 << FIFO_OVERFLOW)
2329 | (1 << FIFO_UNDERFLOW)
2330 | (1 << SHORT_PACKET_OUT_DONE_INTERRUPT)
2331 | (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)
2332 | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
2333 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
2334 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
2335 | (1 << DATA_IN_TOKEN_INTERRUPT)
2336 , &ep->regs->ep_stat);
2337 u.raw [0] = readl (&dev->usb->setup0123);
2338 u.raw [1] = readl (&dev->usb->setup4567);
2340 cpu_to_le32s (&u.raw [0]);
2341 cpu_to_le32s (&u.raw [1]);
2343 le16_to_cpus (&u.r.wValue);
2344 le16_to_cpus (&u.r.wIndex);
2345 le16_to_cpus (&u.r.wLength);
2348 writel (1 << SETUP_PACKET_INTERRUPT, &dev->regs->irqstat0);
2349 stat ^= (1 << SETUP_PACKET_INTERRUPT);
2351 /* watch control traffic at the token level, and force
2352 * synchronization before letting the status stage happen.
2353 * FIXME ignore tokens we'll NAK, until driver responds.
2354 * that'll mean a lot less irqs for some drivers.
2356 ep->is_in = (u.r.bRequestType & USB_DIR_IN) != 0;
2358 scratch = (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
2359 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
2360 | (1 << DATA_IN_TOKEN_INTERRUPT);
2361 stop_out_naking (ep);
2363 scratch = (1 << DATA_PACKET_RECEIVED_INTERRUPT)
2364 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
2365 | (1 << DATA_IN_TOKEN_INTERRUPT);
2366 writel (scratch, &dev->epregs [0].ep_irqenb);
2368 /* we made the hardware handle most lowlevel requests;
2369 * everything else goes uplevel to the gadget code.
2371 switch (u.r.bRequest) {
2372 case USB_REQ_GET_STATUS: {
2373 struct net2280_ep *e;
2376 /* hw handles device and interface status */
2377 if (u.r.bRequestType != (USB_DIR_IN|USB_RECIP_ENDPOINT))
2379 if ((e = get_ep_by_addr (dev, u.r.wIndex)) == 0
2383 if (readl (&e->regs->ep_rsp)
2384 & (1 << SET_ENDPOINT_HALT))
2385 status = __constant_cpu_to_le16 (1);
2387 status = __constant_cpu_to_le16 (0);
2389 /* don't bother with a request object! */
2390 writel (0, &dev->epregs [0].ep_irqenb);
2391 set_fifo_bytecount (ep, u.r.wLength);
2392 writel (status, &dev->epregs [0].ep_data);
2394 VDEBUG (dev, "%s stat %02x\n", ep->ep.name, status);
2395 goto next_endpoints;
2398 case USB_REQ_CLEAR_FEATURE: {
2399 struct net2280_ep *e;
2401 /* hw handles device features */
2402 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
2404 if (u.r.wValue != 0 /* HALT feature */
2405 || u.r.wLength != 0)
2407 if ((e = get_ep_by_addr (dev, u.r.wIndex)) == 0)
2411 VDEBUG (dev, "%s clear halt\n", ep->ep.name);
2412 goto next_endpoints;
2415 case USB_REQ_SET_FEATURE: {
2416 struct net2280_ep *e;
2418 /* hw handles device features */
2419 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
2421 if (u.r.wValue != 0 /* HALT feature */
2422 || u.r.wLength != 0)
2424 if ((e = get_ep_by_addr (dev, u.r.wIndex)) == 0)
2428 VDEBUG (dev, "%s set halt\n", ep->ep.name);
2429 goto next_endpoints;
2434 VDEBUG (dev, "setup %02x.%02x v%04x i%04x "
2436 u.r.bRequestType, u.r.bRequest,
2437 u.r.wValue, u.r.wIndex,
2438 readl (&ep->regs->ep_cfg));
2439 spin_unlock (&dev->lock);
2440 tmp = dev->driver->setup (&dev->gadget, &u.r);
2441 spin_lock (&dev->lock);
2444 /* stall ep0 on error */
2447 VDEBUG (dev, "req %02x.%02x protocol STALL; stat %d\n",
2448 u.r.bRequestType, u.r.bRequest, tmp);
2449 dev->protocol_stall = 1;
2452 /* some in/out token irq should follow; maybe stall then.
2453 * driver must queue a request (even zlp) or halt ep0
2454 * before the host times out.
2459 /* endpoint data irq ? */
2460 scratch = stat & 0x7f;
2462 for (num = 0; scratch; num++) {
2465 /* do this endpoint's FIFO and queue need tending? */
2467 if ((scratch & t) == 0)
2471 ep = &dev->ep [num];
2472 handle_ep_small (ep);
2476 DEBUG (dev, "unhandled irqstat0 %08x\n", stat);
2479 #define DMA_INTERRUPTS ( \
2480 (1 << DMA_D_INTERRUPT) \
2481 | (1 << DMA_C_INTERRUPT) \
2482 | (1 << DMA_B_INTERRUPT) \
2483 | (1 << DMA_A_INTERRUPT))
2484 #define PCI_ERROR_INTERRUPTS ( \
2485 (1 << PCI_MASTER_ABORT_RECEIVED_INTERRUPT) \
2486 | (1 << PCI_TARGET_ABORT_RECEIVED_INTERRUPT) \
2487 | (1 << PCI_RETRY_ABORT_INTERRUPT))
2489 static void handle_stat1_irqs (struct net2280 *dev, u32 stat)
2491 struct net2280_ep *ep;
2492 u32 tmp, num, scratch;
2494 /* after disconnect there's nothing else to do! */
2495 tmp = (1 << VBUS_INTERRUPT) | (1 << ROOT_PORT_RESET_INTERRUPT);
2497 writel (tmp, &dev->regs->irqstat1);
2498 if (((stat & (1 << ROOT_PORT_RESET_INTERRUPT)) != 0
2499 || (readl (&dev->usb->usbctl) & (1 << VBUS_PIN)) == 0
2500 ) && dev->gadget.speed != USB_SPEED_UNKNOWN) {
2501 DEBUG (dev, "disconnect %s\n",
2502 dev->driver->driver.name);
2503 stop_activity (dev, dev->driver);
2509 /* vBUS can bounce ... one of many reasons to ignore the
2510 * notion of hotplug events on bus connect/disconnect!
2516 /* NOTE: we don't actually suspend the hardware; that starts to
2517 * interact with PCI power management, and needs something like a
2518 * controller->suspend() call to clear SUSPEND_REQUEST_INTERRUPT.
2519 * we shouldn't see resume interrupts.
2520 * for rev 0100, this also avoids erratum 0102.
2522 tmp = (1 << SUSPEND_REQUEST_CHANGE_INTERRUPT);
2524 if (dev->driver->suspend)
2525 dev->driver->suspend (&dev->gadget);
2528 stat &= ~(1 << SUSPEND_REQUEST_INTERRUPT);
2530 /* clear any other status/irqs */
2532 writel (stat, &dev->regs->irqstat1);
2534 /* some status we can just ignore */
2535 stat &= ~((1 << CONTROL_STATUS_INTERRUPT)
2536 | (1 << RESUME_INTERRUPT)
2537 | (1 << SOF_INTERRUPT));
2540 // DEBUG (dev, "irqstat1 %08x\n", stat);
2542 /* DMA status, for ep-{a,b,c,d} */
2543 scratch = stat & DMA_INTERRUPTS;
2544 stat &= ~DMA_INTERRUPTS;
2546 for (num = 0; scratch; num++) {
2547 struct net2280_dma_regs *dma;
2550 if ((tmp & scratch) == 0)
2554 ep = &dev->ep [num + 1];
2560 /* clear ep's dma status */
2561 tmp = readl (&dma->dmastat);
2562 writel (tmp, &dma->dmastat);
2564 /* chaining should stop on abort, short OUT from fifo,
2565 * or (stat0 codepath) short OUT transfer.
2567 if (!use_dma_chaining) {
2568 if ((tmp & (1 << DMA_TRANSACTION_DONE_INTERRUPT))
2570 DEBUG (ep->dev, "%s no xact done? %08x\n",
2577 /* OUT transfers terminate when the data from the
2578 * host is in our memory. Process whatever's done.
2579 * On this path, we know transfer's last packet wasn't
2580 * less than req->length. NAK_OUT_PACKETS may be set,
2581 * or the FIFO may already be holding new packets.
2583 * IN transfers can linger in the FIFO for a very
2584 * long time ... we ignore that for now, accounting
2585 * precisely (like PIO does) needs per-packet irqs
2587 scan_dma_completions (ep);
2589 /* disable dma on inactive queues; else maybe restart */
2590 if (list_empty (&ep->queue)) {
2591 if (use_dma_chaining)
2594 tmp = readl (&dma->dmactl);
2595 if (!use_dma_chaining
2596 || (tmp & (1 << DMA_ENABLE)) == 0)
2598 else if (ep->is_in && use_dma_chaining) {
2599 struct net2280_request *req;
2602 /* the descriptor at the head of the chain
2603 * may still have VALID_BIT clear; that's
2604 * used to trigger changing DMA_FIFO_VALIDATE
2605 * (affects automagic zlp writes).
2607 req = list_entry (ep->queue.next,
2608 struct net2280_request, queue);
2609 dmacount = req->td->dmacount;
2610 dmacount &= __constant_cpu_to_le32 (
2612 | DMA_BYTE_COUNT_MASK);
2613 if (dmacount && (dmacount & valid_bit) == 0)
2620 /* NOTE: there are other PCI errors we might usefully notice.
2621 * if they appear very often, here's where to try recovering.
2623 if (stat & PCI_ERROR_INTERRUPTS) {
2624 ERROR (dev, "pci dma error; stat %08x\n", stat);
2625 stat &= ~PCI_ERROR_INTERRUPTS;
2626 /* these are fatal errors, but "maybe" they won't
2629 stop_activity (dev, dev->driver);
2635 DEBUG (dev, "unhandled irqstat1 %08x\n", stat);
2638 static irqreturn_t net2280_irq (int irq, void *_dev, struct pt_regs * r)
2640 struct net2280 *dev = _dev;
2642 spin_lock (&dev->lock);
2644 /* handle disconnect, dma, and more */
2645 handle_stat1_irqs (dev, readl (&dev->regs->irqstat1));
2647 /* control requests and PIO */
2648 handle_stat0_irqs (dev, readl (&dev->regs->irqstat0));
2650 spin_unlock (&dev->lock);
2655 /*-------------------------------------------------------------------------*/
2657 static void gadget_release (struct device *_dev)
2659 struct net2280 *dev = dev_get_drvdata (_dev);
2664 /* tear down the binding between this driver and the pci device */
2666 static void __exit net2280_remove (struct pci_dev *pdev)
2668 struct net2280 *dev = pci_get_drvdata (pdev);
2670 /* start with the driver above us */
2672 /* should have been done already by driver model core */
2673 WARN (dev, "pci remove, driver '%s' is still registered\n",
2674 dev->driver->driver.name);
2675 usb_gadget_unregister_driver (dev->driver);
2678 /* then clean up the resources we allocated during probe() */
2679 net2280_led_shutdown (dev);
2680 if (dev->requests) {
2682 for (i = 1; i < 5; i++) {
2683 if (!dev->ep [i].dummy)
2685 pci_pool_free (dev->requests, dev->ep [i].dummy,
2686 dev->ep [i].td_dma);
2688 pci_pool_destroy (dev->requests);
2691 free_irq (pdev->irq, dev);
2693 iounmap (dev->regs);
2695 release_mem_region (pci_resource_start (pdev, 0),
2696 pci_resource_len (pdev, 0));
2698 pci_disable_device (pdev);
2699 device_unregister (&dev->gadget.dev);
2700 device_remove_file (&pdev->dev, &dev_attr_registers);
2701 pci_set_drvdata (pdev, 0);
2703 INFO (dev, "unbind\n");
2708 /* wrap this driver around the specified device, but
2709 * don't respond over USB until a gadget driver binds to us.
2712 static int net2280_probe (struct pci_dev *pdev, const struct pci_device_id *id)
2714 struct net2280 *dev;
2715 unsigned long resource, len;
2718 char buf [8], *bufp;
2720 /* if you want to support more than one controller in a system,
2721 * usb_gadget_driver_{register,unregister}() must change.
2723 if (the_controller) {
2724 dev_warn (&pdev->dev, "ignoring\n");
2728 /* alloc, and start init */
2729 dev = kmalloc (sizeof *dev, SLAB_KERNEL);
2735 memset (dev, 0, sizeof *dev);
2736 spin_lock_init (&dev->lock);
2738 dev->gadget.ops = &net2280_ops;
2740 /* the "gadget" abstracts/virtualizes the controller */
2741 strcpy (dev->gadget.dev.bus_id, "gadget");
2742 dev->gadget.dev.parent = &pdev->dev;
2743 dev->gadget.dev.dma_mask = pdev->dev.dma_mask;
2744 dev->gadget.dev.release = gadget_release;
2745 dev->gadget.name = driver_name;
2747 /* now all the pci goodies ... */
2748 if (pci_enable_device (pdev) < 0) {
2754 /* BAR 0 holds all the registers
2755 * BAR 1 is 8051 memory; unused here (note erratum 0103)
2756 * BAR 2 is fifo memory; unused here
2758 resource = pci_resource_start (pdev, 0);
2759 len = pci_resource_len (pdev, 0);
2760 if (!request_mem_region (resource, len, driver_name)) {
2761 DEBUG (dev, "controller already in use\n");
2767 base = ioremap_nocache (resource, len);
2769 DEBUG (dev, "can't map memory\n");
2773 dev->regs = (struct net2280_regs *) base;
2774 dev->usb = (struct net2280_usb_regs *) (base + 0x0080);
2775 dev->pci = (struct net2280_pci_regs *) (base + 0x0100);
2776 dev->dma = (struct net2280_dma_regs *) (base + 0x0180);
2777 dev->dep = (struct net2280_dep_regs *) (base + 0x0200);
2778 dev->epregs = (struct net2280_ep_regs *) (base + 0x0300);
2780 /* put into initial config, link up all endpoints */
2784 /* irq setup after old hardware is cleaned up */
2786 ERROR (dev, "No IRQ. Check PCI setup!\n");
2791 scnprintf (buf, sizeof buf, "%d", pdev->irq);
2794 bufp = __irq_itoa(pdev->irq);
2796 if (request_irq (pdev->irq, net2280_irq, SA_SHIRQ, driver_name, dev)
2798 ERROR (dev, "request interrupt %s failed\n", bufp);
2805 dev->requests = pci_pool_create ("requests", pdev,
2806 sizeof (struct net2280_dma),
2807 0 /* no alignment requirements */,
2808 0 /* or page-crossing issues */);
2809 if (!dev->requests) {
2810 DEBUG (dev, "can't get request pool\n");
2814 for (i = 1; i < 5; i++) {
2815 struct net2280_dma *td;
2817 td = pci_pool_alloc (dev->requests, GFP_KERNEL,
2818 &dev->ep [i].td_dma);
2820 DEBUG (dev, "can't get dummy %d\n", i);
2824 td->dmacount = 0; /* not VALID */
2825 td->dmaaddr = __constant_cpu_to_le32 (DMA_ADDR_INVALID);
2826 td->dmadesc = td->dmaaddr;
2827 dev->ep [i].dummy = td;
2830 /* enable lower-overhead pci memory bursts during DMA */
2831 writel ( (1 << DMA_MEMORY_WRITE_AND_INVALIDATE_ENABLE)
2832 // 256 write retries may not be enough...
2833 // | (1 << PCI_RETRY_ABORT_ENABLE)
2834 | (1 << DMA_READ_MULTIPLE_ENABLE)
2835 | (1 << DMA_READ_LINE_ENABLE)
2836 , &dev->pci->pcimstctl);
2837 /* erratum 0115 shouldn't appear: Linux inits PCI_LATENCY_TIMER */
2838 pci_set_master (pdev);
2841 /* ... also flushes any posted pci writes */
2842 dev->chiprev = get_idx_reg (dev->regs, REG_CHIPREV) & 0xffff;
2845 pci_set_drvdata (pdev, dev);
2846 INFO (dev, "%s\n", driver_desc);
2847 INFO (dev, "irq %s, pci mem %p, chip rev %04x\n",
2848 bufp, base, dev->chiprev);
2849 INFO (dev, "version: " DRIVER_VERSION "; dma %s\n",
2851 ? (use_dma_chaining ? "chaining" : "enabled")
2853 the_controller = dev;
2855 device_register (&dev->gadget.dev);
2856 device_create_file (&pdev->dev, &dev_attr_registers);
2862 net2280_remove (pdev);
2867 /*-------------------------------------------------------------------------*/
2869 static struct pci_device_id pci_ids [] = { {
2870 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
2874 .subvendor = PCI_ANY_ID,
2875 .subdevice = PCI_ANY_ID,
2877 }, { /* end: all zeroes */ }
2879 MODULE_DEVICE_TABLE (pci, pci_ids);
2881 /* pci driver glue; this is a "new style" PCI driver module */
2882 static struct pci_driver net2280_pci_driver = {
2883 .name = (char *) driver_name,
2884 .id_table = pci_ids,
2886 .probe = net2280_probe,
2887 .remove = __exit_p(net2280_remove),
2889 /* FIXME add power management support */
2892 MODULE_DESCRIPTION (DRIVER_DESC);
2893 MODULE_AUTHOR ("David Brownell");
2894 MODULE_LICENSE ("GPL");
2896 static int __init init (void)
2899 use_dma_chaining = 0;
2900 return pci_module_init (&net2280_pci_driver);
2904 static void __exit cleanup (void)
2906 pci_unregister_driver (&net2280_pci_driver);
2908 module_exit (cleanup);