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 * This driver should work well with most "gadget" drivers, including
11 * the File Storage, Serial, and Ethernet/RNDIS gadget drivers
12 * as well as Gadget Zero and Gadgetfs.
14 * DMA is enabled by default. Drivers using transfer queues might use
15 * DMA chaining to remove IRQ latencies between transfers. (Except when
16 * short OUT transfers happen.) Drivers can use the req->no_interrupt
17 * hint to completely eliminate some IRQs, if a later IRQ is guaranteed
18 * and DMA chaining is enabled.
20 * Note that almost all the errata workarounds here are only needed for
21 * rev1 chips. Rev1a silicon (0110) fixes almost all of them.
25 * Copyright (C) 2003 David Brownell
26 * Copyright (C) 2003 NetChip Technologies
28 * This program is free software; you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License as published by
30 * the Free Software Foundation; either version 2 of the License, or
31 * (at your option) any later version.
33 * This program is distributed in the hope that it will be useful,
34 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36 * GNU General Public License for more details.
38 * You should have received a copy of the GNU General Public License
39 * along with this program; if not, write to the Free Software
40 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
43 #undef DEBUG /* messages on error and most fault paths */
44 #undef VERBOSE /* extra debug messages (success too) */
46 #include <linux/config.h>
47 #include <linux/module.h>
48 #include <linux/pci.h>
49 #include <linux/kernel.h>
50 #include <linux/delay.h>
51 #include <linux/ioport.h>
52 #include <linux/sched.h>
53 #include <linux/slab.h>
54 #include <linux/smp_lock.h>
55 #include <linux/errno.h>
56 #include <linux/init.h>
57 #include <linux/timer.h>
58 #include <linux/list.h>
59 #include <linux/interrupt.h>
60 #include <linux/moduleparam.h>
61 #include <linux/device.h>
62 #include <linux/usb_ch9.h>
63 #include <linux/usb_gadget.h>
65 #include <asm/byteorder.h>
68 #include <asm/system.h>
69 #include <asm/unaligned.h>
72 #define DRIVER_DESC "NetChip 2280 USB Peripheral Controller"
73 #define DRIVER_VERSION "2004 Jan 14"
75 #define DMA_ADDR_INVALID (~(dma_addr_t)0)
76 #define EP_DONTUSE 13 /* nonzero */
78 #define USE_RDK_LEDS /* GPIO pins control three LEDs */
81 static const char driver_name [] = "net2280";
82 static const char driver_desc [] = DRIVER_DESC;
84 static const char ep0name [] = "ep0";
85 static const char *ep_name [] = {
87 "ep-a", "ep-b", "ep-c", "ep-d",
91 /* use_dma -- general goodness, fewer interrupts, less cpu load (vs PIO)
92 * use_dma_chaining -- dma descriptor queueing gives even more irq reduction
94 * The net2280 DMA engines are not tightly integrated with their FIFOs;
95 * not all cases are (yet) handled well in this driver or the silicon.
96 * Some gadget drivers work better with the dma support here than others.
97 * These two parameters let you use PIO or more aggressive DMA.
99 static int use_dma = 1;
100 static int use_dma_chaining = 0;
102 /* "modprobe net2280 use_dma=n" etc */
103 module_param (use_dma, bool, S_IRUGO);
104 module_param (use_dma_chaining, bool, S_IRUGO);
107 /* mode 0 == ep-{a,b,c,d} 1K fifo each
108 * mode 1 == ep-{a,b} 2K fifo each, ep-{c,d} unavailable
109 * mode 2 == ep-a 2K fifo, ep-{b,c} 1K each, ep-d unavailable
111 static ushort fifo_mode = 0;
113 /* "modprobe net2280 fifo_mode=1" etc */
114 module_param (fifo_mode, ushort, 0644);
117 #define DIR_STRING(bAddress) (((bAddress) & USB_DIR_IN) ? "in" : "out")
119 #if defined(CONFIG_USB_GADGET_DEBUG_FILES) || defined (DEBUG)
120 static char *type_string (u8 bmAttributes)
122 switch ((bmAttributes) & USB_ENDPOINT_XFERTYPE_MASK) {
123 case USB_ENDPOINT_XFER_BULK: return "bulk";
124 case USB_ENDPOINT_XFER_ISOC: return "iso";
125 case USB_ENDPOINT_XFER_INT: return "intr";
133 #define valid_bit __constant_cpu_to_le32 (1 << VALID_BIT)
134 #define dma_done_ie __constant_cpu_to_le32 (1 << DMA_DONE_INTERRUPT_ENABLE)
136 /*-------------------------------------------------------------------------*/
139 net2280_enable (struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
142 struct net2280_ep *ep;
146 ep = container_of (_ep, struct net2280_ep, ep);
147 if (!_ep || !desc || ep->desc || _ep->name == ep0name
148 || desc->bDescriptorType != USB_DT_ENDPOINT)
151 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
154 /* erratum 0119 workaround ties up an endpoint number */
155 if ((desc->bEndpointAddress & 0x0f) == EP_DONTUSE)
158 /* sanity check ep-e/ep-f since their fifos are small */
159 max = le16_to_cpu (desc->wMaxPacketSize) & 0x1fff;
160 if (ep->num > 4 && max > 64)
163 spin_lock_irqsave (&dev->lock, flags);
164 _ep->maxpacket = max & 0x7ff;
167 /* ep_reset() has already been called */
169 ep->out_overflow = 0;
171 /* set speed-dependent max packet; may kick in high bandwidth */
172 set_idx_reg (dev->regs, REG_EP_MAXPKT (dev, ep->num), max);
174 /* FIFO lines can't go to different packets. PIO is ok, so
175 * use it instead of troublesome (non-bulk) multi-packet DMA.
177 if (ep->dma && (max % 4) != 0 && use_dma_chaining) {
178 DEBUG (ep->dev, "%s, no dma for maxpacket %d\n",
179 ep->ep.name, ep->ep.maxpacket);
183 /* set type, direction, address; reset fifo counters */
184 writel ((1 << FIFO_FLUSH), &ep->regs->ep_stat);
185 tmp = (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK);
186 if (tmp == USB_ENDPOINT_XFER_INT) {
187 /* erratum 0105 workaround prevents hs NYET */
188 if (dev->chiprev == 0100
189 && dev->gadget.speed == USB_SPEED_HIGH
190 && !(desc->bEndpointAddress & USB_DIR_IN))
191 writel ((1 << CLEAR_NAK_OUT_PACKETS_MODE),
193 } else if (tmp == USB_ENDPOINT_XFER_BULK) {
194 /* catch some particularly blatant driver bugs */
195 if ((dev->gadget.speed == USB_SPEED_HIGH
197 || (dev->gadget.speed == USB_SPEED_FULL
199 spin_unlock_irqrestore (&dev->lock, flags);
203 ep->is_iso = (tmp == USB_ENDPOINT_XFER_ISOC) ? 1 : 0;
204 tmp <<= ENDPOINT_TYPE;
205 tmp |= desc->bEndpointAddress;
206 tmp |= (4 << ENDPOINT_BYTE_COUNT); /* default full fifo lines */
207 tmp |= 1 << ENDPOINT_ENABLE;
210 /* for OUT transfers, block the rx fifo until a read is posted */
211 ep->is_in = (tmp & USB_DIR_IN) != 0;
213 writel ((1 << SET_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
215 writel (tmp, &ep->regs->ep_cfg);
218 if (!ep->dma) { /* pio, per-packet */
219 tmp = (1 << ep->num) | readl (&dev->regs->pciirqenb0);
220 writel (tmp, &dev->regs->pciirqenb0);
222 tmp = (1 << DATA_PACKET_RECEIVED_INTERRUPT_ENABLE)
223 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT_ENABLE)
224 | readl (&ep->regs->ep_irqenb);
225 writel (tmp, &ep->regs->ep_irqenb);
226 } else { /* dma, per-request */
227 tmp = (1 << (8 + ep->num)); /* completion */
228 tmp |= readl (&dev->regs->pciirqenb1);
229 writel (tmp, &dev->regs->pciirqenb1);
231 /* for short OUT transfers, dma completions can't
232 * advance the queue; do it pio-style, by hand.
233 * NOTE erratum 0112 workaround #2
235 if ((desc->bEndpointAddress & USB_DIR_IN) == 0) {
236 tmp = (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT_ENABLE);
237 writel (tmp, &ep->regs->ep_irqenb);
239 tmp = (1 << ep->num) | readl (&dev->regs->pciirqenb0);
240 writel (tmp, &dev->regs->pciirqenb0);
244 tmp = desc->bEndpointAddress;
245 DEBUG (dev, "enabled %s (ep%d%s-%s) %s max %04x\n",
246 _ep->name, tmp & 0x0f, DIR_STRING (tmp),
247 type_string (desc->bmAttributes),
248 ep->dma ? "dma" : "pio", max);
250 /* pci writes may still be posted */
251 spin_unlock_irqrestore (&dev->lock, flags);
255 static int handshake (u32 __iomem *ptr, u32 mask, u32 done, int usec)
260 result = readl (ptr);
261 if (result == ~(u32)0) /* "device unplugged" */
272 static struct usb_ep_ops net2280_ep_ops;
274 static void ep_reset (struct net2280_regs __iomem *regs, struct net2280_ep *ep)
279 INIT_LIST_HEAD (&ep->queue);
281 ep->ep.maxpacket = ~0;
282 ep->ep.ops = &net2280_ep_ops;
284 /* disable the dma, irqs, endpoint... */
286 writel (0, &ep->dma->dmactl);
287 writel ( (1 << DMA_SCATTER_GATHER_DONE_INTERRUPT)
288 | (1 << DMA_TRANSACTION_DONE_INTERRUPT)
290 , &ep->dma->dmastat);
292 tmp = readl (®s->pciirqenb0);
293 tmp &= ~(1 << ep->num);
294 writel (tmp, ®s->pciirqenb0);
296 tmp = readl (®s->pciirqenb1);
297 tmp &= ~(1 << (8 + ep->num)); /* completion */
298 writel (tmp, ®s->pciirqenb1);
300 writel (0, &ep->regs->ep_irqenb);
302 /* init to our chosen defaults, notably so that we NAK OUT
303 * packets until the driver queues a read (+note erratum 0112)
305 tmp = (1 << SET_NAK_OUT_PACKETS_MODE)
306 | (1 << SET_NAK_OUT_PACKETS)
307 | (1 << CLEAR_EP_HIDE_STATUS_PHASE)
308 | (1 << CLEAR_INTERRUPT_MODE);
311 tmp |= (1 << CLEAR_ENDPOINT_TOGGLE)
312 | (1 << CLEAR_ENDPOINT_HALT);
314 writel (tmp, &ep->regs->ep_rsp);
316 /* scrub most status bits, and flush any fifo state */
317 writel ( (1 << TIMEOUT)
318 | (1 << USB_STALL_SENT)
319 | (1 << USB_IN_NAK_SENT)
320 | (1 << USB_IN_ACK_RCVD)
321 | (1 << USB_OUT_PING_NAK_SENT)
322 | (1 << USB_OUT_ACK_SENT)
323 | (1 << FIFO_OVERFLOW)
324 | (1 << FIFO_UNDERFLOW)
326 | (1 << SHORT_PACKET_OUT_DONE_INTERRUPT)
327 | (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)
328 | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
329 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
330 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
331 | (1 << DATA_IN_TOKEN_INTERRUPT)
332 , &ep->regs->ep_stat);
334 /* fifo size is handled separately */
337 static void nuke (struct net2280_ep *);
339 static int net2280_disable (struct usb_ep *_ep)
341 struct net2280_ep *ep;
344 ep = container_of (_ep, struct net2280_ep, ep);
345 if (!_ep || !ep->desc || _ep->name == ep0name)
348 spin_lock_irqsave (&ep->dev->lock, flags);
350 ep_reset (ep->dev->regs, ep);
352 VDEBUG (ep->dev, "disabled %s %s\n",
353 ep->dma ? "dma" : "pio", _ep->name);
355 /* synch memory views with the device */
356 (void) readl (&ep->regs->ep_cfg);
358 if (use_dma && !ep->dma && ep->num >= 1 && ep->num <= 4)
359 ep->dma = &ep->dev->dma [ep->num - 1];
361 spin_unlock_irqrestore (&ep->dev->lock, flags);
365 /*-------------------------------------------------------------------------*/
367 static struct usb_request *
368 net2280_alloc_request (struct usb_ep *_ep, int gfp_flags)
370 struct net2280_ep *ep;
371 struct net2280_request *req;
375 ep = container_of (_ep, struct net2280_ep, ep);
377 req = kmalloc (sizeof *req, gfp_flags);
381 memset (req, 0, sizeof *req);
382 req->req.dma = DMA_ADDR_INVALID;
383 INIT_LIST_HEAD (&req->queue);
385 /* this dma descriptor may be swapped with the previous dummy */
387 struct net2280_dma *td;
389 td = pci_pool_alloc (ep->dev->requests, gfp_flags,
395 td->dmacount = 0; /* not VALID */
396 td->dmaaddr = __constant_cpu_to_le32 (DMA_ADDR_INVALID);
397 td->dmadesc = td->dmaaddr;
404 net2280_free_request (struct usb_ep *_ep, struct usb_request *_req)
406 struct net2280_ep *ep;
407 struct net2280_request *req;
409 ep = container_of (_ep, struct net2280_ep, ep);
413 req = container_of (_req, struct net2280_request, req);
414 WARN_ON (!list_empty (&req->queue));
416 pci_pool_free (ep->dev->requests, req->td, req->td_dma);
420 /*-------------------------------------------------------------------------*/
424 /* many common platforms have dma-coherent caches, which means that it's
425 * safe to use kmalloc() memory for all i/o buffers without using any
426 * cache flushing calls. (unless you're trying to share cache lines
427 * between dma and non-dma activities, which is a slow idea in any case.)
429 * other platforms need more care, with 2.5 having a moderately general
430 * solution (which falls down for allocations smaller than one page)
431 * that improves significantly on the 2.4 PCI allocators by removing
432 * the restriction that memory never be freed in_interrupt().
434 #if defined(CONFIG_X86)
437 #elif defined(CONFIG_PPC) && !defined(CONFIG_NOT_COHERENT_CACHE)
440 #elif defined(CONFIG_MIPS) && !defined(CONFIG_NONCOHERENT_IO)
443 /* FIXME there are other cases, including an x86-64 one ... */
446 /* allocating buffers this way eliminates dma mapping overhead, which
447 * on some platforms will mean eliminating a per-io buffer copy. with
448 * some kinds of system caches, further tweaks may still be needed.
451 net2280_alloc_buffer (
459 struct net2280_ep *ep;
461 ep = container_of (_ep, struct net2280_ep, ep);
464 *dma = DMA_ADDR_INVALID;
466 #if defined(USE_KMALLOC)
467 retval = kmalloc(bytes, gfp_flags);
469 *dma = virt_to_phys(retval);
472 /* the main problem with this call is that it wastes memory
473 * on typical 1/N page allocations: it allocates 1-N pages.
475 #warning Using dma_alloc_coherent even with buffers smaller than a page.
476 retval = dma_alloc_coherent(&ep->dev->pdev->dev,
477 bytes, dma, gfp_flags);
479 retval = kmalloc(bytes, gfp_flags);
485 net2280_free_buffer (
491 /* free memory into the right allocator */
493 if (dma != DMA_ADDR_INVALID) {
494 struct net2280_ep *ep;
496 ep = container_of(_ep, struct net2280_ep, ep);
499 dma_free_coherent(&ep->dev->pdev->dev, bytes, buf, dma);
505 /*-------------------------------------------------------------------------*/
507 /* load a packet into the fifo we use for usb IN transfers.
508 * works for all endpoints.
510 * NOTE: pio with ep-a..ep-d could stuff multiple packets into the fifo
511 * at a time, but this code is simpler because it knows it only writes
512 * one packet. ep-a..ep-d should use dma instead.
515 write_fifo (struct net2280_ep *ep, struct usb_request *req)
517 struct net2280_ep_regs __iomem *regs = ep->regs;
520 unsigned count, total;
522 /* INVARIANT: fifo is currently empty. (testable) */
525 buf = req->buf + req->actual;
527 total = req->length - req->actual;
533 /* write just one packet at a time */
534 count = ep->ep.maxpacket;
535 if (count > total) /* min() cannot be used on a bitfield */
538 VDEBUG (ep->dev, "write %s fifo (IN) %d bytes%s req %p\n",
540 (count != ep->ep.maxpacket) ? " (short)" : "",
543 /* NOTE be careful if you try to align these. fifo lines
544 * should normally be full (4 bytes) and successive partial
545 * lines are ok only in certain cases.
547 tmp = get_unaligned ((u32 *)buf);
549 writel (tmp, ®s->ep_data);
554 /* last fifo entry is "short" unless we wrote a full packet.
555 * also explicitly validate last word in (periodic) transfers
556 * when maxpacket is not a multiple of 4 bytes.
558 if (count || total < ep->ep.maxpacket) {
559 tmp = count ? get_unaligned ((u32 *)buf) : count;
561 set_fifo_bytecount (ep, count & 0x03);
562 writel (tmp, ®s->ep_data);
565 /* pci writes may still be posted */
568 /* work around erratum 0106: PCI and USB race over the OUT fifo.
569 * caller guarantees chiprev 0100, out endpoint is NAKing, and
570 * there's no real data in the fifo.
572 * NOTE: also used in cases where that erratum doesn't apply:
573 * where the host wrote "too much" data to us.
575 static void out_flush (struct net2280_ep *ep)
580 ASSERT_OUT_NAKING (ep);
582 statp = &ep->regs->ep_stat;
583 writel ( (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
584 | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
586 writel ((1 << FIFO_FLUSH), statp);
589 if (tmp & (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
590 /* high speed did bulk NYET; fifo isn't filling */
591 && ep->dev->gadget.speed == USB_SPEED_FULL) {
594 usec = 50; /* 64 byte bulk/interrupt */
595 handshake (statp, (1 << USB_OUT_PING_NAK_SENT),
596 (1 << USB_OUT_PING_NAK_SENT), usec);
597 /* NAK done; now CLEAR_NAK_OUT_PACKETS is safe */
601 /* unload packet(s) from the fifo we use for usb OUT transfers.
602 * returns true iff the request completed, because of short packet
603 * or the request buffer having filled with full packets.
605 * for ep-a..ep-d this will read multiple packets out when they
606 * have been accepted.
609 read_fifo (struct net2280_ep *ep, struct net2280_request *req)
611 struct net2280_ep_regs __iomem *regs = ep->regs;
612 u8 *buf = req->req.buf + req->req.actual;
613 unsigned count, tmp, is_short;
614 unsigned cleanup = 0, prevent = 0;
616 /* erratum 0106 ... packets coming in during fifo reads might
617 * be incompletely rejected. not all cases have workarounds.
619 if (ep->dev->chiprev == 0x0100
620 && ep->dev->gadget.speed == USB_SPEED_FULL) {
622 tmp = readl (&ep->regs->ep_stat);
623 if ((tmp & (1 << NAK_OUT_PACKETS)))
625 else if ((tmp & (1 << FIFO_FULL))) {
626 start_out_naking (ep);
629 /* else: hope we don't see the problem */
632 /* never overflow the rx buffer. the fifo reads packets until
633 * it sees a short one; we might not be ready for them all.
636 count = readl (®s->ep_avail);
637 if (unlikely (count == 0)) {
639 tmp = readl (&ep->regs->ep_stat);
640 count = readl (®s->ep_avail);
641 /* handled that data already? */
642 if (count == 0 && (tmp & (1 << NAK_OUT_PACKETS)) == 0)
646 tmp = req->req.length - req->req.actual;
648 /* as with DMA, data overflow gets flushed */
649 if ((tmp % ep->ep.maxpacket) != 0) {
651 "%s out fifo %d bytes, expected %d\n",
652 ep->ep.name, count, tmp);
653 req->req.status = -EOVERFLOW;
655 /* NAK_OUT_PACKETS will be set, so flushing is safe;
656 * the next read will start with the next packet
658 } /* else it's a ZLP, no worries */
661 req->req.actual += count;
663 is_short = (count == 0) || ((count % ep->ep.maxpacket) != 0);
665 VDEBUG (ep->dev, "read %s fifo (OUT) %d bytes%s%s%s req %p %d/%d\n",
666 ep->ep.name, count, is_short ? " (short)" : "",
667 cleanup ? " flush" : "", prevent ? " nak" : "",
668 req, req->req.actual, req->req.length);
671 tmp = readl (®s->ep_data);
673 put_unaligned (tmp, (u32 *)buf);
678 tmp = readl (®s->ep_data);
679 /* LE conversion is implicit here: */
688 writel ((1 << CLEAR_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
689 (void) readl (&ep->regs->ep_rsp);
692 return is_short || ((req->req.actual == req->req.length)
696 /* fill out dma descriptor to match a given request */
698 fill_dma_desc (struct net2280_ep *ep, struct net2280_request *req, int valid)
700 struct net2280_dma *td = req->td;
701 u32 dmacount = req->req.length;
703 /* don't let DMA continue after a short OUT packet,
704 * so overruns can't affect the next transfer.
705 * in case of overruns on max-size packets, we can't
706 * stop the fifo from filling but we can flush it.
709 dmacount |= (1 << DMA_DIRECTION);
710 else if ((dmacount % ep->ep.maxpacket) != 0)
711 dmacount |= (1 << END_OF_CHAIN);
715 dmacount |= (1 << VALID_BIT);
716 if (likely(!req->req.no_interrupt || !use_dma_chaining))
717 dmacount |= (1 << DMA_DONE_INTERRUPT_ENABLE);
719 /* td->dmadesc = previously set by caller */
720 td->dmaaddr = cpu_to_le32 (req->req.dma);
722 /* 2280 may be polling VALID_BIT through ep->dma->dmadesc */
724 td->dmacount = cpu_to_le32p (&dmacount);
727 static const u32 dmactl_default =
728 (1 << DMA_SCATTER_GATHER_DONE_INTERRUPT)
729 | (1 << DMA_CLEAR_COUNT_ENABLE)
730 /* erratum 0116 workaround part 1 (use POLLING) */
731 | (POLL_100_USEC << DESCRIPTOR_POLLING_RATE)
732 | (1 << DMA_VALID_BIT_POLLING_ENABLE)
733 | (1 << DMA_VALID_BIT_ENABLE)
734 | (1 << DMA_SCATTER_GATHER_ENABLE)
735 /* erratum 0116 workaround part 2 (no AUTOSTART) */
738 static inline void spin_stop_dma (struct net2280_dma_regs __iomem *dma)
740 handshake (&dma->dmactl, (1 << DMA_ENABLE), 0, 50);
743 static inline void stop_dma (struct net2280_dma_regs __iomem *dma)
745 writel (readl (&dma->dmactl) & ~(1 << DMA_ENABLE), &dma->dmactl);
749 static void start_queue (struct net2280_ep *ep, u32 dmactl, u32 td_dma)
751 struct net2280_dma_regs __iomem *dma = ep->dma;
753 writel ((1 << VALID_BIT) | (ep->is_in << DMA_DIRECTION),
755 writel (readl (&dma->dmastat), &dma->dmastat);
757 writel (td_dma, &dma->dmadesc);
758 writel (dmactl, &dma->dmactl);
760 /* erratum 0116 workaround part 3: pci arbiter away from net2280 */
761 (void) readl (&ep->dev->pci->pcimstctl);
763 writel ((1 << DMA_START), &dma->dmastat);
766 stop_out_naking (ep);
769 static void start_dma (struct net2280_ep *ep, struct net2280_request *req)
772 struct net2280_dma_regs __iomem *dma = ep->dma;
774 /* FIXME can't use DMA for ZLPs */
776 /* on this path we "know" there's no dma active (yet) */
777 WARN_ON (readl (&dma->dmactl) & (1 << DMA_ENABLE));
778 writel (0, &ep->dma->dmactl);
780 /* previous OUT packet might have been short */
781 if (!ep->is_in && ((tmp = readl (&ep->regs->ep_stat))
782 & (1 << NAK_OUT_PACKETS)) != 0) {
783 writel ((1 << SHORT_PACKET_TRANSFERRED_INTERRUPT),
786 tmp = readl (&ep->regs->ep_avail);
788 writel (readl (&dma->dmastat), &dma->dmastat);
790 /* transfer all/some fifo data */
791 writel (req->req.dma, &dma->dmaaddr);
792 tmp = min (tmp, req->req.length);
794 /* dma irq, faking scatterlist status */
795 req->td->dmacount = cpu_to_le32 (req->req.length - tmp);
796 writel ((1 << DMA_DONE_INTERRUPT_ENABLE)
797 | tmp, &dma->dmacount);
798 req->td->dmadesc = 0;
801 writel ((1 << DMA_ENABLE), &dma->dmactl);
802 writel ((1 << DMA_START), &dma->dmastat);
807 tmp = dmactl_default;
809 /* force packet boundaries between dma requests, but prevent the
810 * controller from automagically writing a last "short" packet
811 * (zero length) unless the driver explicitly said to do that.
814 if (likely ((req->req.length % ep->ep.maxpacket) != 0
816 tmp |= (1 << DMA_FIFO_VALIDATE);
817 ep->in_fifo_validate = 1;
819 ep->in_fifo_validate = 0;
822 /* init req->td, pointing to the current dummy */
823 req->td->dmadesc = cpu_to_le32 (ep->td_dma);
824 fill_dma_desc (ep, req, 1);
826 if (!use_dma_chaining)
827 req->td->dmacount |= __constant_cpu_to_le32 (1 << END_OF_CHAIN);
829 start_queue (ep, tmp, req->td_dma);
833 queue_dma (struct net2280_ep *ep, struct net2280_request *req, int valid)
835 struct net2280_dma *end;
838 /* swap new dummy for old, link; fill and maybe activate */
844 ep->td_dma = req->td_dma;
847 end->dmadesc = cpu_to_le32 (ep->td_dma);
849 fill_dma_desc (ep, req, valid);
853 done (struct net2280_ep *ep, struct net2280_request *req, int status)
856 unsigned stopped = ep->stopped;
858 list_del_init (&req->queue);
860 if (req->req.status == -EINPROGRESS)
861 req->req.status = status;
863 status = req->req.status;
867 pci_unmap_single (dev->pdev, req->req.dma, req->req.length,
868 ep->is_in ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
869 req->req.dma = DMA_ADDR_INVALID;
873 if (status && status != -ESHUTDOWN)
874 VDEBUG (dev, "complete %s req %p stat %d len %u/%u\n",
875 ep->ep.name, &req->req, status,
876 req->req.actual, req->req.length);
878 /* don't modify queue heads during completion callback */
880 spin_unlock (&dev->lock);
881 req->req.complete (&ep->ep, &req->req);
882 spin_lock (&dev->lock);
883 ep->stopped = stopped;
886 /*-------------------------------------------------------------------------*/
889 net2280_queue (struct usb_ep *_ep, struct usb_request *_req, int gfp_flags)
891 struct net2280_request *req;
892 struct net2280_ep *ep;
896 /* we always require a cpu-view buffer, so that we can
897 * always use pio (as fallback or whatever).
899 req = container_of (_req, struct net2280_request, req);
900 if (!_req || !_req->complete || !_req->buf
901 || !list_empty (&req->queue))
903 if (_req->length > (~0 & DMA_BYTE_COUNT_MASK))
905 ep = container_of (_ep, struct net2280_ep, ep);
906 if (!_ep || (!ep->desc && ep->num != 0))
909 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
912 /* FIXME implement PIO fallback for ZLPs with DMA */
913 if (ep->dma && _req->length == 0)
916 /* set up dma mapping in case the caller didn't */
917 if (ep->dma && _req->dma == DMA_ADDR_INVALID) {
918 _req->dma = pci_map_single (dev->pdev, _req->buf, _req->length,
919 ep->is_in ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
924 VDEBUG (dev, "%s queue req %p, len %d buf %p\n",
925 _ep->name, _req, _req->length, _req->buf);
928 spin_lock_irqsave (&dev->lock, flags);
930 _req->status = -EINPROGRESS;
933 /* kickstart this i/o queue? */
934 if (list_empty (&ep->queue) && !ep->stopped) {
935 /* use DMA if the endpoint supports it, else pio */
939 /* maybe there's no control data, just status ack */
940 if (ep->num == 0 && _req->length == 0) {
943 VDEBUG (dev, "%s status ack\n", ep->ep.name);
947 /* PIO ... stuff the fifo, or unblock it. */
949 write_fifo (ep, _req);
950 else if (list_empty (&ep->queue)) {
953 /* OUT FIFO might have packet(s) buffered */
954 s = readl (&ep->regs->ep_stat);
955 if ((s & (1 << FIFO_EMPTY)) == 0) {
956 /* note: _req->short_not_ok is
957 * ignored here since PIO _always_
958 * stops queue advance here, and
959 * _req->status doesn't change for
960 * short reads (only _req->actual)
962 if (read_fifo (ep, req)) {
969 s = readl (&ep->regs->ep_stat);
972 /* don't NAK, let the fifo fill */
973 if (req && (s & (1 << NAK_OUT_PACKETS)))
974 writel ((1 << CLEAR_NAK_OUT_PACKETS),
979 } else if (ep->dma) {
985 /* preventing magic zlps is per-engine state, not
986 * per-transfer; irq logic must recover hiccups.
988 expect = likely (req->req.zero
989 || (req->req.length % ep->ep.maxpacket) != 0);
990 if (expect != ep->in_fifo_validate)
993 queue_dma (ep, req, valid);
995 } /* else the irq handler advances the queue. */
998 list_add_tail (&req->queue, &ep->queue);
1000 spin_unlock_irqrestore (&dev->lock, flags);
1002 /* pci writes may still be posted */
1008 struct net2280_ep *ep,
1009 struct net2280_request *req,
1014 req->req.actual = req->req.length - (DMA_BYTE_COUNT_MASK & dmacount);
1015 done (ep, req, status);
1018 static void restart_dma (struct net2280_ep *ep);
1020 static void scan_dma_completions (struct net2280_ep *ep)
1022 /* only look at descriptors that were "naturally" retired,
1023 * so fifo and list head state won't matter
1025 while (!list_empty (&ep->queue)) {
1026 struct net2280_request *req;
1029 req = list_entry (ep->queue.next,
1030 struct net2280_request, queue);
1034 tmp = le32_to_cpup (&req->td->dmacount);
1035 if ((tmp & (1 << VALID_BIT)) != 0)
1038 /* SHORT_PACKET_TRANSFERRED_INTERRUPT handles "usb-short"
1039 * cases where DMA must be aborted; this code handles
1040 * all non-abort DMA completions.
1042 if (unlikely (req->td->dmadesc == 0)) {
1044 tmp = readl (&ep->dma->dmacount);
1045 if (tmp & DMA_BYTE_COUNT_MASK)
1047 /* single transfer mode */
1048 dma_done (ep, req, tmp, 0);
1050 } else if (!ep->is_in
1051 && (req->req.length % ep->ep.maxpacket) != 0) {
1052 tmp = readl (&ep->regs->ep_stat);
1054 /* AVOID TROUBLE HERE by not issuing short reads from
1055 * your gadget driver. That helps avoids errata 0121,
1056 * 0122, and 0124; not all cases trigger the warning.
1058 if ((tmp & (1 << NAK_OUT_PACKETS)) == 0) {
1059 WARN (ep->dev, "%s lost packet sync!\n",
1061 req->req.status = -EOVERFLOW;
1062 } else if ((tmp = readl (&ep->regs->ep_avail)) != 0) {
1063 /* fifo gets flushed later */
1064 ep->out_overflow = 1;
1065 DEBUG (ep->dev, "%s dma, discard %d len %d\n",
1068 req->req.status = -EOVERFLOW;
1071 dma_done (ep, req, tmp, 0);
1075 static void restart_dma (struct net2280_ep *ep)
1077 struct net2280_request *req;
1078 u32 dmactl = dmactl_default;
1082 req = list_entry (ep->queue.next, struct net2280_request, queue);
1084 if (!use_dma_chaining) {
1085 start_dma (ep, req);
1089 /* the 2280 will be processing the queue unless queue hiccups after
1090 * the previous transfer:
1091 * IN: wanted automagic zlp, head doesn't (or vice versa)
1092 * DMA_FIFO_VALIDATE doesn't init from dma descriptors.
1093 * OUT: was "usb-short", we must restart.
1095 if (ep->is_in && !req->valid) {
1096 struct net2280_request *entry, *prev = NULL;
1097 int reqmode, done = 0;
1099 DEBUG (ep->dev, "%s dma hiccup td %p\n", ep->ep.name, req->td);
1100 ep->in_fifo_validate = likely (req->req.zero
1101 || (req->req.length % ep->ep.maxpacket) != 0);
1102 if (ep->in_fifo_validate)
1103 dmactl |= (1 << DMA_FIFO_VALIDATE);
1104 list_for_each_entry (entry, &ep->queue, queue) {
1109 dmacount = entry->td->dmacount;
1111 reqmode = likely (entry->req.zero
1112 || (entry->req.length
1113 % ep->ep.maxpacket) != 0);
1114 if (reqmode == ep->in_fifo_validate) {
1116 dmacount |= valid_bit;
1117 entry->td->dmacount = dmacount;
1121 /* force a hiccup */
1122 prev->td->dmacount |= dma_done_ie;
1127 /* walk the rest of the queue so unlinks behave */
1129 dmacount &= ~valid_bit;
1130 entry->td->dmacount = dmacount;
1135 writel (0, &ep->dma->dmactl);
1136 start_queue (ep, dmactl, req->td_dma);
1139 static void abort_dma (struct net2280_ep *ep)
1141 /* abort the current transfer */
1142 if (likely (!list_empty (&ep->queue))) {
1143 /* FIXME work around errata 0121, 0122, 0124 */
1144 writel ((1 << DMA_ABORT), &ep->dma->dmastat);
1145 spin_stop_dma (ep->dma);
1148 scan_dma_completions (ep);
1151 /* dequeue ALL requests */
1152 static void nuke (struct net2280_ep *ep)
1154 struct net2280_request *req;
1156 /* called with spinlock held */
1160 while (!list_empty (&ep->queue)) {
1161 req = list_entry (ep->queue.next,
1162 struct net2280_request,
1164 done (ep, req, -ESHUTDOWN);
1168 /* dequeue JUST ONE request */
1169 static int net2280_dequeue (struct usb_ep *_ep, struct usb_request *_req)
1171 struct net2280_ep *ep;
1172 struct net2280_request *req;
1173 unsigned long flags;
1177 ep = container_of (_ep, struct net2280_ep, ep);
1178 if (!_ep || (!ep->desc && ep->num != 0) || !_req)
1181 spin_lock_irqsave (&ep->dev->lock, flags);
1182 stopped = ep->stopped;
1184 /* quiesce dma while we patch the queue */
1188 dmactl = readl (&ep->dma->dmactl);
1189 /* WARNING erratum 0127 may kick in ... */
1191 scan_dma_completions (ep);
1194 /* make sure it's still queued on this endpoint */
1195 list_for_each_entry (req, &ep->queue, queue) {
1196 if (&req->req == _req)
1199 if (&req->req != _req) {
1200 spin_unlock_irqrestore (&ep->dev->lock, flags);
1204 /* queue head may be partially complete. */
1205 if (ep->queue.next == &req->queue) {
1207 DEBUG (ep->dev, "unlink (%s) dma\n", _ep->name);
1208 _req->status = -ECONNRESET;
1210 if (likely (ep->queue.next == &req->queue)) {
1211 // NOTE: misreports single-transfer mode
1212 req->td->dmacount = 0; /* invalidate */
1214 readl (&ep->dma->dmacount),
1218 DEBUG (ep->dev, "unlink (%s) pio\n", _ep->name);
1219 done (ep, req, -ECONNRESET);
1223 /* patch up hardware chaining data */
1224 } else if (ep->dma && use_dma_chaining) {
1225 if (req->queue.prev == ep->queue.next) {
1226 writel (le32_to_cpu (req->td->dmadesc),
1228 if (req->td->dmacount & dma_done_ie)
1229 writel (readl (&ep->dma->dmacount)
1231 &ep->dma->dmacount);
1233 struct net2280_request *prev;
1235 prev = list_entry (req->queue.prev,
1236 struct net2280_request, queue);
1237 prev->td->dmadesc = req->td->dmadesc;
1238 if (req->td->dmacount & dma_done_ie)
1239 prev->td->dmacount |= dma_done_ie;
1244 done (ep, req, -ECONNRESET);
1245 ep->stopped = stopped;
1248 /* turn off dma on inactive queues */
1249 if (list_empty (&ep->queue))
1251 else if (!ep->stopped) {
1252 /* resume current request, or start new one */
1254 writel (dmactl, &ep->dma->dmactl);
1256 start_dma (ep, list_entry (ep->queue.next,
1257 struct net2280_request, queue));
1261 spin_unlock_irqrestore (&ep->dev->lock, flags);
1262 return req ? 0 : -EOPNOTSUPP;
1265 /*-------------------------------------------------------------------------*/
1267 static int net2280_fifo_status (struct usb_ep *_ep);
1270 net2280_set_halt (struct usb_ep *_ep, int value)
1272 struct net2280_ep *ep;
1273 unsigned long flags;
1276 ep = container_of (_ep, struct net2280_ep, ep);
1277 if (!_ep || (!ep->desc && ep->num != 0))
1279 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1281 if (ep->desc /* not ep0 */ && (ep->desc->bmAttributes & 0x03)
1282 == USB_ENDPOINT_XFER_ISOC)
1285 spin_lock_irqsave (&ep->dev->lock, flags);
1286 if (!list_empty (&ep->queue))
1288 else if (ep->is_in && value && net2280_fifo_status (_ep) != 0)
1291 VDEBUG (ep->dev, "%s %s halt\n", _ep->name,
1292 value ? "set" : "clear");
1293 /* set/clear, then synch memory views with the device */
1296 ep->dev->protocol_stall = 1;
1301 (void) readl (&ep->regs->ep_rsp);
1303 spin_unlock_irqrestore (&ep->dev->lock, flags);
1309 net2280_fifo_status (struct usb_ep *_ep)
1311 struct net2280_ep *ep;
1314 ep = container_of (_ep, struct net2280_ep, ep);
1315 if (!_ep || (!ep->desc && ep->num != 0))
1317 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1320 avail = readl (&ep->regs->ep_avail) & ((1 << 12) - 1);
1321 if (avail > ep->fifo_size)
1324 avail = ep->fifo_size - avail;
1329 net2280_fifo_flush (struct usb_ep *_ep)
1331 struct net2280_ep *ep;
1333 ep = container_of (_ep, struct net2280_ep, ep);
1334 if (!_ep || (!ep->desc && ep->num != 0))
1336 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1339 writel ((1 << FIFO_FLUSH), &ep->regs->ep_stat);
1340 (void) readl (&ep->regs->ep_rsp);
1343 static struct usb_ep_ops net2280_ep_ops = {
1344 .enable = net2280_enable,
1345 .disable = net2280_disable,
1347 .alloc_request = net2280_alloc_request,
1348 .free_request = net2280_free_request,
1350 .alloc_buffer = net2280_alloc_buffer,
1351 .free_buffer = net2280_free_buffer,
1353 .queue = net2280_queue,
1354 .dequeue = net2280_dequeue,
1356 .set_halt = net2280_set_halt,
1357 .fifo_status = net2280_fifo_status,
1358 .fifo_flush = net2280_fifo_flush,
1361 /*-------------------------------------------------------------------------*/
1363 static int net2280_get_frame (struct usb_gadget *_gadget)
1365 struct net2280 *dev;
1366 unsigned long flags;
1371 dev = container_of (_gadget, struct net2280, gadget);
1372 spin_lock_irqsave (&dev->lock, flags);
1373 retval = get_idx_reg (dev->regs, REG_FRAME) & 0x03ff;
1374 spin_unlock_irqrestore (&dev->lock, flags);
1378 static int net2280_wakeup (struct usb_gadget *_gadget)
1380 struct net2280 *dev;
1382 unsigned long flags;
1386 dev = container_of (_gadget, struct net2280, gadget);
1388 spin_lock_irqsave (&dev->lock, flags);
1389 tmp = readl (&dev->usb->usbctl);
1390 if (tmp & (1 << DEVICE_REMOTE_WAKEUP_ENABLE))
1391 writel (1 << GENERATE_RESUME, &dev->usb->usbstat);
1392 spin_unlock_irqrestore (&dev->lock, flags);
1394 /* pci writes may still be posted */
1398 static int net2280_set_selfpowered (struct usb_gadget *_gadget, int value)
1400 struct net2280 *dev;
1402 unsigned long flags;
1406 dev = container_of (_gadget, struct net2280, gadget);
1408 spin_lock_irqsave (&dev->lock, flags);
1409 tmp = readl (&dev->usb->usbctl);
1411 tmp |= (1 << SELF_POWERED_STATUS);
1413 tmp &= ~(1 << SELF_POWERED_STATUS);
1414 writel (tmp, &dev->usb->usbctl);
1415 spin_unlock_irqrestore (&dev->lock, flags);
1420 static int net2280_pullup(struct usb_gadget *_gadget, int is_on)
1422 struct net2280 *dev;
1424 unsigned long flags;
1428 dev = container_of (_gadget, struct net2280, gadget);
1430 spin_lock_irqsave (&dev->lock, flags);
1431 tmp = readl (&dev->usb->usbctl);
1432 dev->softconnect = (is_on != 0);
1434 tmp |= (1 << USB_DETECT_ENABLE);
1436 tmp &= ~(1 << USB_DETECT_ENABLE);
1437 writel (tmp, &dev->usb->usbctl);
1438 spin_unlock_irqrestore (&dev->lock, flags);
1443 static const struct usb_gadget_ops net2280_ops = {
1444 .get_frame = net2280_get_frame,
1445 .wakeup = net2280_wakeup,
1446 .set_selfpowered = net2280_set_selfpowered,
1447 .pullup = net2280_pullup,
1450 /*-------------------------------------------------------------------------*/
1452 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1454 /* FIXME move these into procfs, and use seq_file.
1455 * Sysfs _still_ doesn't behave for arbitrarily sized files,
1456 * and also doesn't help products using this with 2.4 kernels.
1459 /* "function" sysfs attribute */
1461 show_function (struct device *_dev, char *buf)
1463 struct net2280 *dev = dev_get_drvdata (_dev);
1466 || !dev->driver->function
1467 || strlen (dev->driver->function) > PAGE_SIZE)
1469 return scnprintf (buf, PAGE_SIZE, "%s\n", dev->driver->function);
1471 static DEVICE_ATTR (function, S_IRUGO, show_function, NULL);
1474 show_registers (struct device *_dev, char *buf)
1476 struct net2280 *dev;
1479 unsigned long flags;
1484 dev = dev_get_drvdata (_dev);
1487 spin_lock_irqsave (&dev->lock, flags);
1490 s = dev->driver->driver.name;
1494 /* Main Control Registers */
1495 t = scnprintf (next, size, "%s version " DRIVER_VERSION
1496 ", chiprev %04x, dma %s\n\n"
1497 "devinit %03x fifoctl %08x gadget '%s'\n"
1498 "pci irqenb0 %02x irqenb1 %08x "
1499 "irqstat0 %04x irqstat1 %08x\n",
1500 driver_name, dev->chiprev,
1502 ? (use_dma_chaining ? "chaining" : "enabled")
1504 readl (&dev->regs->devinit),
1505 readl (&dev->regs->fifoctl),
1507 readl (&dev->regs->pciirqenb0),
1508 readl (&dev->regs->pciirqenb1),
1509 readl (&dev->regs->irqstat0),
1510 readl (&dev->regs->irqstat1));
1514 /* USB Control Registers */
1515 t1 = readl (&dev->usb->usbctl);
1516 t2 = readl (&dev->usb->usbstat);
1517 if (t1 & (1 << VBUS_PIN)) {
1518 if (t2 & (1 << HIGH_SPEED))
1520 else if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1524 /* full speed bit (6) not working?? */
1527 t = scnprintf (next, size,
1528 "stdrsp %08x usbctl %08x usbstat %08x "
1529 "addr 0x%02x (%s)\n",
1530 readl (&dev->usb->stdrsp), t1, t2,
1531 readl (&dev->usb->ouraddr), s);
1535 /* PCI Master Control Registers */
1537 /* DMA Control Registers */
1539 /* Configurable EP Control Registers */
1540 for (i = 0; i < 7; i++) {
1541 struct net2280_ep *ep;
1547 t1 = readl (&ep->regs->ep_cfg);
1548 t2 = readl (&ep->regs->ep_rsp) & 0xff;
1549 t = scnprintf (next, size,
1550 "\n%s\tcfg %05x rsp (%02x) %s%s%s%s%s%s%s%s"
1552 ep->ep.name, t1, t2,
1553 (t2 & (1 << CLEAR_NAK_OUT_PACKETS))
1555 (t2 & (1 << CLEAR_EP_HIDE_STATUS_PHASE))
1557 (t2 & (1 << CLEAR_EP_FORCE_CRC_ERROR))
1559 (t2 & (1 << CLEAR_INTERRUPT_MODE))
1560 ? "interrupt " : "",
1561 (t2 & (1<<CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE))
1563 (t2 & (1 << CLEAR_NAK_OUT_PACKETS_MODE))
1565 (t2 & (1 << CLEAR_ENDPOINT_TOGGLE))
1566 ? "DATA1 " : "DATA0 ",
1567 (t2 & (1 << CLEAR_ENDPOINT_HALT))
1569 readl (&ep->regs->ep_irqenb));
1573 t = scnprintf (next, size,
1574 "\tstat %08x avail %04x "
1576 readl (&ep->regs->ep_stat),
1577 readl (&ep->regs->ep_avail),
1578 t1 & 0x0f, DIR_STRING (t1),
1579 type_string (t1 >> 8),
1580 ep->stopped ? "*" : "");
1587 t = scnprintf (next, size,
1588 " dma\tctl %08x stat %08x count %08x\n"
1589 "\taddr %08x desc %08x\n",
1590 readl (&ep->dma->dmactl),
1591 readl (&ep->dma->dmastat),
1592 readl (&ep->dma->dmacount),
1593 readl (&ep->dma->dmaaddr),
1594 readl (&ep->dma->dmadesc));
1600 /* Indexed Registers */
1604 t = scnprintf (next, size, "\nirqs: ");
1607 for (i = 0; i < 7; i++) {
1608 struct net2280_ep *ep;
1613 t = scnprintf (next, size, " %s/%lu", ep->ep.name, ep->irqs);
1618 t = scnprintf (next, size, "\n");
1622 spin_unlock_irqrestore (&dev->lock, flags);
1624 return PAGE_SIZE - size;
1626 static DEVICE_ATTR (registers, S_IRUGO, show_registers, NULL);
1629 show_queues (struct device *_dev, char *buf)
1631 struct net2280 *dev;
1634 unsigned long flags;
1637 dev = dev_get_drvdata (_dev);
1640 spin_lock_irqsave (&dev->lock, flags);
1642 for (i = 0; i < 7; i++) {
1643 struct net2280_ep *ep = &dev->ep [i];
1644 struct net2280_request *req;
1648 const struct usb_endpoint_descriptor *d;
1653 t = d->bEndpointAddress;
1654 t = scnprintf (next, size,
1655 "\n%s (ep%d%s-%s) max %04x %s fifo %d\n",
1656 ep->ep.name, t & USB_ENDPOINT_NUMBER_MASK,
1657 (t & USB_DIR_IN) ? "in" : "out",
1659 switch (d->bmAttributes & 0x03) {
1660 case USB_ENDPOINT_XFER_BULK:
1661 val = "bulk"; break;
1662 case USB_ENDPOINT_XFER_INT:
1663 val = "intr"; break;
1667 le16_to_cpu (d->wMaxPacketSize) & 0x1fff,
1668 ep->dma ? "dma" : "pio", ep->fifo_size
1670 } else /* ep0 should only have one transfer queued */
1671 t = scnprintf (next, size, "ep0 max 64 pio %s\n",
1672 ep->is_in ? "in" : "out");
1673 if (t <= 0 || t > size)
1678 if (list_empty (&ep->queue)) {
1679 t = scnprintf (next, size, "\t(nothing queued)\n");
1680 if (t <= 0 || t > size)
1686 list_for_each_entry (req, &ep->queue, queue) {
1687 if (ep->dma && req->td_dma == readl (&ep->dma->dmadesc))
1688 t = scnprintf (next, size,
1689 "\treq %p len %d/%d "
1690 "buf %p (dmacount %08x)\n",
1691 &req->req, req->req.actual,
1692 req->req.length, req->req.buf,
1693 readl (&ep->dma->dmacount));
1695 t = scnprintf (next, size,
1696 "\treq %p len %d/%d buf %p\n",
1697 &req->req, req->req.actual,
1698 req->req.length, req->req.buf);
1699 if (t <= 0 || t > size)
1705 struct net2280_dma *td;
1708 t = scnprintf (next, size, "\t td %08x "
1709 " count %08x buf %08x desc %08x\n",
1711 le32_to_cpu (td->dmacount),
1712 le32_to_cpu (td->dmaaddr),
1713 le32_to_cpu (td->dmadesc));
1714 if (t <= 0 || t > size)
1723 spin_unlock_irqrestore (&dev->lock, flags);
1724 return PAGE_SIZE - size;
1726 static DEVICE_ATTR (queues, S_IRUGO, show_queues, NULL);
1731 #define device_create_file(a,b) do {} while (0)
1732 #define device_remove_file device_create_file
1736 /*-------------------------------------------------------------------------*/
1738 /* another driver-specific mode might be a request type doing dma
1739 * to/from another device fifo instead of to/from memory.
1742 static void set_fifo_mode (struct net2280 *dev, int mode)
1744 /* keeping high bits preserves BAR2 */
1745 writel ((0xffff << PCI_BASE2_RANGE) | mode, &dev->regs->fifoctl);
1747 /* always ep-{a,b,e,f} ... maybe not ep-c or ep-d */
1748 INIT_LIST_HEAD (&dev->gadget.ep_list);
1749 list_add_tail (&dev->ep [1].ep.ep_list, &dev->gadget.ep_list);
1750 list_add_tail (&dev->ep [2].ep.ep_list, &dev->gadget.ep_list);
1753 list_add_tail (&dev->ep [3].ep.ep_list, &dev->gadget.ep_list);
1754 list_add_tail (&dev->ep [4].ep.ep_list, &dev->gadget.ep_list);
1755 dev->ep [1].fifo_size = dev->ep [2].fifo_size = 1024;
1758 dev->ep [1].fifo_size = dev->ep [2].fifo_size = 2048;
1761 list_add_tail (&dev->ep [3].ep.ep_list, &dev->gadget.ep_list);
1762 dev->ep [1].fifo_size = 2048;
1763 dev->ep [2].fifo_size = 1024;
1766 /* fifo sizes for ep0, ep-c, ep-d, ep-e, and ep-f never change */
1767 list_add_tail (&dev->ep [5].ep.ep_list, &dev->gadget.ep_list);
1768 list_add_tail (&dev->ep [6].ep.ep_list, &dev->gadget.ep_list);
1772 * net2280_set_fifo_mode - change allocation of fifo buffers
1773 * @gadget: access to the net2280 device that will be updated
1774 * @mode: 0 for default, four 1kB buffers (ep-a through ep-d);
1775 * 1 for two 2kB buffers (ep-a and ep-b only);
1776 * 2 for one 2kB buffer (ep-a) and two 1kB ones (ep-b, ep-c).
1778 * returns zero on success, else negative errno. when this succeeds,
1779 * the contents of gadget->ep_list may have changed.
1781 * you may only call this function when endpoints a-d are all disabled.
1782 * use it whenever extra hardware buffering can help performance, such
1783 * as before enabling "high bandwidth" interrupt endpoints that use
1784 * maxpacket bigger than 512 (when double buffering would otherwise
1787 int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode)
1790 struct net2280 *dev;
1792 unsigned long flags;
1796 dev = container_of (gadget, struct net2280, gadget);
1798 spin_lock_irqsave (&dev->lock, flags);
1800 for (i = 1; i <= 4; i++)
1801 if (dev->ep [i].desc) {
1805 if (mode < 0 || mode > 2)
1808 set_fifo_mode (dev, mode);
1809 spin_unlock_irqrestore (&dev->lock, flags);
1813 DEBUG (dev, "fifo: ep-a 2K, ep-b 2K\n");
1815 DEBUG (dev, "fifo: ep-a 2K, ep-b 1K, ep-c 1K\n");
1816 /* else all are 1K */
1820 EXPORT_SYMBOL (net2280_set_fifo_mode);
1822 /*-------------------------------------------------------------------------*/
1824 /* keeping it simple:
1825 * - one bus driver, initted first;
1826 * - one function driver, initted second
1828 * most of the work to support multiple net2280 controllers would
1829 * be to associate this gadget driver (yes?) with all of them, or
1830 * perhaps to bind specific drivers to specific devices.
1833 static struct net2280 *the_controller;
1835 static void usb_reset (struct net2280 *dev)
1839 dev->gadget.speed = USB_SPEED_UNKNOWN;
1840 (void) readl (&dev->usb->usbctl);
1842 net2280_led_init (dev);
1844 /* disable automatic responses, and irqs */
1845 writel (0, &dev->usb->stdrsp);
1846 writel (0, &dev->regs->pciirqenb0);
1847 writel (0, &dev->regs->pciirqenb1);
1849 /* clear old dma and irq state */
1850 for (tmp = 0; tmp < 4; tmp++) {
1851 struct net2280_ep *ep = &dev->ep [tmp + 1];
1856 writel (~0, &dev->regs->irqstat0),
1857 writel (~(1 << SUSPEND_REQUEST_INTERRUPT), &dev->regs->irqstat1),
1859 /* reset, and enable pci */
1860 tmp = readl (&dev->regs->devinit)
1862 | (1 << FIFO_SOFT_RESET)
1863 | (1 << USB_SOFT_RESET)
1864 | (1 << M8051_RESET);
1865 writel (tmp, &dev->regs->devinit);
1867 /* standard fifo and endpoint allocations */
1868 set_fifo_mode (dev, (fifo_mode <= 2) ? fifo_mode : 0);
1871 static void usb_reinit (struct net2280 *dev)
1876 /* use_dma changes are ignored till next device re-init */
1879 /* basic endpoint init */
1880 for (tmp = 0; tmp < 7; tmp++) {
1881 struct net2280_ep *ep = &dev->ep [tmp];
1883 ep->ep.name = ep_name [tmp];
1887 if (tmp > 0 && tmp <= 4) {
1888 ep->fifo_size = 1024;
1890 ep->dma = &dev->dma [tmp - 1];
1893 ep->regs = &dev->epregs [tmp];
1894 ep_reset (dev->regs, ep);
1896 dev->ep [0].ep.maxpacket = 64;
1897 dev->ep [5].ep.maxpacket = 64;
1898 dev->ep [6].ep.maxpacket = 64;
1900 dev->gadget.ep0 = &dev->ep [0].ep;
1901 dev->ep [0].stopped = 0;
1902 INIT_LIST_HEAD (&dev->gadget.ep0->ep_list);
1904 /* we want to prevent lowlevel/insecure access from the USB host,
1905 * but erratum 0119 means this enable bit is ignored
1907 for (tmp = 0; tmp < 5; tmp++)
1908 writel (EP_DONTUSE, &dev->dep [tmp].dep_cfg);
1911 static void ep0_start (struct net2280 *dev)
1913 writel ( (1 << CLEAR_EP_HIDE_STATUS_PHASE)
1914 | (1 << CLEAR_NAK_OUT_PACKETS)
1915 | (1 << CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE)
1916 , &dev->epregs [0].ep_rsp);
1919 * hardware optionally handles a bunch of standard requests
1920 * that the API hides from drivers anyway. have it do so.
1921 * endpoint status/features are handled in software, to
1922 * help pass tests for some dubious behavior.
1924 writel ( (1 << SET_TEST_MODE)
1925 | (1 << SET_ADDRESS)
1926 | (1 << DEVICE_SET_CLEAR_DEVICE_REMOTE_WAKEUP)
1927 | (1 << GET_DEVICE_STATUS)
1928 | (1 << GET_INTERFACE_STATUS)
1929 , &dev->usb->stdrsp);
1930 writel ( (1 << USB_ROOT_PORT_WAKEUP_ENABLE)
1931 | (1 << SELF_POWERED_USB_DEVICE)
1932 | (1 << REMOTE_WAKEUP_SUPPORT)
1933 | (dev->softconnect << USB_DETECT_ENABLE)
1934 | (1 << SELF_POWERED_STATUS)
1935 , &dev->usb->usbctl);
1937 /* enable irqs so we can see ep0 and general operation */
1938 writel ( (1 << SETUP_PACKET_INTERRUPT_ENABLE)
1939 | (1 << ENDPOINT_0_INTERRUPT_ENABLE)
1940 , &dev->regs->pciirqenb0);
1941 writel ( (1 << PCI_INTERRUPT_ENABLE)
1942 | (1 << PCI_MASTER_ABORT_RECEIVED_INTERRUPT_ENABLE)
1943 | (1 << PCI_TARGET_ABORT_RECEIVED_INTERRUPT_ENABLE)
1944 | (1 << PCI_RETRY_ABORT_INTERRUPT_ENABLE)
1945 | (1 << VBUS_INTERRUPT_ENABLE)
1946 | (1 << ROOT_PORT_RESET_INTERRUPT_ENABLE)
1947 | (1 << SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE)
1948 , &dev->regs->pciirqenb1);
1950 /* don't leave any writes posted */
1951 (void) readl (&dev->usb->usbctl);
1954 /* when a driver is successfully registered, it will receive
1955 * control requests including set_configuration(), which enables
1956 * non-control requests. then usb traffic follows until a
1957 * disconnect is reported. then a host may connect again, or
1958 * the driver might get unbound.
1960 int usb_gadget_register_driver (struct usb_gadget_driver *driver)
1962 struct net2280 *dev = the_controller;
1966 /* insist on high speed support from the driver, since
1967 * (dev->usb->xcvrdiag & FORCE_FULL_SPEED_MODE)
1968 * "must not be used in normal operation"
1971 || driver->speed != USB_SPEED_HIGH
1981 for (i = 0; i < 7; i++)
1982 dev->ep [i].irqs = 0;
1984 /* hook up the driver ... */
1985 dev->softconnect = 1;
1986 driver->driver.bus = NULL;
1987 dev->driver = driver;
1988 dev->gadget.dev.driver = &driver->driver;
1989 retval = driver->bind (&dev->gadget);
1991 DEBUG (dev, "bind to driver %s --> %d\n",
1992 driver->driver.name, retval);
1994 dev->gadget.dev.driver = NULL;
1998 device_create_file (&dev->pdev->dev, &dev_attr_function);
1999 device_create_file (&dev->pdev->dev, &dev_attr_queues);
2001 /* ... then enable host detection and ep0; and we're ready
2002 * for set_configuration as well as eventual disconnect.
2004 net2280_led_active (dev, 1);
2007 DEBUG (dev, "%s ready, usbctl %08x stdrsp %08x\n",
2008 driver->driver.name,
2009 readl (&dev->usb->usbctl),
2010 readl (&dev->usb->stdrsp));
2012 /* pci writes may still be posted */
2015 EXPORT_SYMBOL (usb_gadget_register_driver);
2018 stop_activity (struct net2280 *dev, struct usb_gadget_driver *driver)
2022 /* don't disconnect if it's not connected */
2023 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
2026 /* stop hardware; prevent new request submissions;
2027 * and kill any outstanding requests.
2030 for (i = 0; i < 7; i++)
2031 nuke (&dev->ep [i]);
2033 /* report disconnect; the driver is already quiesced */
2035 spin_unlock (&dev->lock);
2036 driver->disconnect (&dev->gadget);
2037 spin_lock (&dev->lock);
2043 int usb_gadget_unregister_driver (struct usb_gadget_driver *driver)
2045 struct net2280 *dev = the_controller;
2046 unsigned long flags;
2050 if (!driver || driver != dev->driver)
2053 spin_lock_irqsave (&dev->lock, flags);
2054 stop_activity (dev, driver);
2055 spin_unlock_irqrestore (&dev->lock, flags);
2057 net2280_pullup (&dev->gadget, 0);
2059 driver->unbind (&dev->gadget);
2060 dev->gadget.dev.driver = NULL;
2063 net2280_led_active (dev, 0);
2064 device_remove_file (&dev->pdev->dev, &dev_attr_function);
2065 device_remove_file (&dev->pdev->dev, &dev_attr_queues);
2067 DEBUG (dev, "unregistered driver '%s'\n", driver->driver.name);
2070 EXPORT_SYMBOL (usb_gadget_unregister_driver);
2073 /*-------------------------------------------------------------------------*/
2075 /* handle ep0, ep-e, ep-f with 64 byte packets: packet per irq.
2076 * also works for dma-capable endpoints, in pio mode or just
2077 * to manually advance the queue after short OUT transfers.
2079 static void handle_ep_small (struct net2280_ep *ep)
2081 struct net2280_request *req;
2083 /* 0 error, 1 mid-data, 2 done */
2086 if (!list_empty (&ep->queue))
2087 req = list_entry (ep->queue.next,
2088 struct net2280_request, queue);
2092 /* ack all, and handle what we care about */
2093 t = readl (&ep->regs->ep_stat);
2096 VDEBUG (ep->dev, "%s ack ep_stat %08x, req %p\n",
2097 ep->ep.name, t, req ? &req->req : 0);
2099 writel (t & ~(1 << NAK_OUT_PACKETS), &ep->regs->ep_stat);
2101 /* for ep0, monitor token irqs to catch data stage length errors
2102 * and to synchronize on status.
2104 * also, to defer reporting of protocol stalls ... here's where
2105 * data or status first appears, handling stalls here should never
2106 * cause trouble on the host side..
2108 * control requests could be slightly faster without token synch for
2109 * status, but status can jam up that way.
2111 if (unlikely (ep->num == 0)) {
2113 /* status; stop NAKing */
2114 if (t & (1 << DATA_OUT_PING_TOKEN_INTERRUPT)) {
2115 if (ep->dev->protocol_stall) {
2122 /* reply to extra IN data tokens with a zlp */
2123 } else if (t & (1 << DATA_IN_TOKEN_INTERRUPT)) {
2124 if (ep->dev->protocol_stall) {
2128 } else if (!req && ep->stopped)
2129 write_fifo (ep, NULL);
2132 /* status; stop NAKing */
2133 if (t & (1 << DATA_IN_TOKEN_INTERRUPT)) {
2134 if (ep->dev->protocol_stall) {
2139 /* an extra OUT token is an error */
2140 } else if (((t & (1 << DATA_OUT_PING_TOKEN_INTERRUPT))
2142 && req->req.actual == req->req.length)
2144 ep->dev->protocol_stall = 1;
2148 done (ep, req, -EOVERFLOW);
2154 if (unlikely (!req))
2157 /* manual DMA queue advance after short OUT */
2158 if (likely (ep->dma != 0)) {
2159 if (t & (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)) {
2161 int stopped = ep->stopped;
2163 /* TRANSFERRED works around OUT_DONE erratum 0112.
2164 * we expect (N <= maxpacket) bytes; host wrote M.
2165 * iff (M < N) we won't ever see a DMA interrupt.
2168 for (count = 0; ; t = readl (&ep->regs->ep_stat)) {
2170 /* any preceding dma transfers must finish.
2171 * dma handles (M >= N), may empty the queue
2173 scan_dma_completions (ep);
2174 if (unlikely (list_empty (&ep->queue)
2175 || ep->out_overflow)) {
2179 req = list_entry (ep->queue.next,
2180 struct net2280_request, queue);
2182 /* here either (M < N), a "real" short rx;
2183 * or (M == N) and the queue didn't empty
2185 if (likely (t & (1 << FIFO_EMPTY))) {
2186 count = readl (&ep->dma->dmacount);
2187 count &= DMA_BYTE_COUNT_MASK;
2188 if (readl (&ep->dma->dmadesc)
2196 /* stop DMA, leave ep NAKing */
2197 writel ((1 << DMA_ABORT), &ep->dma->dmastat);
2198 spin_stop_dma (ep->dma);
2201 req->td->dmacount = 0;
2202 t = readl (&ep->regs->ep_avail);
2203 dma_done (ep, req, count, t);
2206 /* also flush to prevent erratum 0106 trouble */
2207 if (unlikely (ep->out_overflow
2208 || (ep->dev->chiprev == 0x0100
2209 && ep->dev->gadget.speed
2210 == USB_SPEED_FULL))) {
2212 ep->out_overflow = 0;
2215 /* (re)start dma if needed, stop NAKing */
2216 ep->stopped = stopped;
2217 if (!list_empty (&ep->queue))
2220 DEBUG (ep->dev, "%s dma ep_stat %08x ??\n",
2224 /* data packet(s) received (in the fifo, OUT) */
2225 } else if (t & (1 << DATA_PACKET_RECEIVED_INTERRUPT)) {
2226 if (read_fifo (ep, req) && ep->num != 0)
2229 /* data packet(s) transmitted (IN) */
2230 } else if (t & (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)) {
2233 len = req->req.length - req->req.actual;
2234 if (len > ep->ep.maxpacket)
2235 len = ep->ep.maxpacket;
2236 req->req.actual += len;
2238 /* if we wrote it all, we're usually done */
2239 if (req->req.actual == req->req.length) {
2241 /* wait for control status */
2244 } else if (!req->req.zero || len != ep->ep.maxpacket)
2248 /* there was nothing to do ... */
2249 } else if (mode == 1)
2254 /* stream endpoints often resubmit/unlink in completion */
2257 /* maybe advance queue to next request */
2259 /* NOTE: net2280 could let gadget driver start the
2260 * status stage later. since not all controllers let
2261 * them control that, the api doesn't (yet) allow it.
2267 if (!list_empty (&ep->queue) && !ep->stopped)
2268 req = list_entry (ep->queue.next,
2269 struct net2280_request, queue);
2272 if (req && !ep->is_in)
2273 stop_out_naking (ep);
2277 /* is there a buffer for the next packet?
2278 * for best streaming performance, make sure there is one.
2280 if (req && !ep->stopped) {
2282 /* load IN fifo with next packet (may be zlp) */
2283 if (t & (1 << DATA_PACKET_TRANSMITTED_INTERRUPT))
2284 write_fifo (ep, &req->req);
2288 static struct net2280_ep *
2289 get_ep_by_addr (struct net2280 *dev, u16 wIndex)
2291 struct net2280_ep *ep;
2293 if ((wIndex & USB_ENDPOINT_NUMBER_MASK) == 0)
2294 return &dev->ep [0];
2295 list_for_each_entry (ep, &dev->gadget.ep_list, ep.ep_list) {
2296 u8 bEndpointAddress;
2300 bEndpointAddress = ep->desc->bEndpointAddress;
2301 if ((wIndex ^ bEndpointAddress) & USB_DIR_IN)
2303 if ((wIndex & 0x0f) == (bEndpointAddress & 0x0f))
2309 static void handle_stat0_irqs (struct net2280 *dev, u32 stat)
2311 struct net2280_ep *ep;
2314 /* most of these don't need individual acks */
2315 stat &= ~(1 << INTA_ASSERTED);
2318 // DEBUG (dev, "irqstat0 %04x\n", stat);
2320 /* starting a control request? */
2321 if (unlikely (stat & (1 << SETUP_PACKET_INTERRUPT))) {
2324 struct usb_ctrlrequest r;
2327 struct net2280_request *req;
2329 if (dev->gadget.speed == USB_SPEED_UNKNOWN) {
2330 if (readl (&dev->usb->usbstat) & (1 << HIGH_SPEED))
2331 dev->gadget.speed = USB_SPEED_HIGH;
2333 dev->gadget.speed = USB_SPEED_FULL;
2334 net2280_led_speed (dev, dev->gadget.speed);
2335 DEBUG (dev, "%s speed\n",
2336 (dev->gadget.speed == USB_SPEED_HIGH)
2343 /* make sure any leftover request state is cleared */
2344 stat &= ~(1 << ENDPOINT_0_INTERRUPT);
2345 while (!list_empty (&ep->queue)) {
2346 req = list_entry (ep->queue.next,
2347 struct net2280_request, queue);
2348 done (ep, req, (req->req.actual == req->req.length)
2352 dev->protocol_stall = 0;
2353 writel ( (1 << TIMEOUT)
2354 | (1 << USB_STALL_SENT)
2355 | (1 << USB_IN_NAK_SENT)
2356 | (1 << USB_IN_ACK_RCVD)
2357 | (1 << USB_OUT_PING_NAK_SENT)
2358 | (1 << USB_OUT_ACK_SENT)
2359 | (1 << FIFO_OVERFLOW)
2360 | (1 << FIFO_UNDERFLOW)
2361 | (1 << SHORT_PACKET_OUT_DONE_INTERRUPT)
2362 | (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)
2363 | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
2364 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
2365 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
2366 | (1 << DATA_IN_TOKEN_INTERRUPT)
2367 , &ep->regs->ep_stat);
2368 u.raw [0] = readl (&dev->usb->setup0123);
2369 u.raw [1] = readl (&dev->usb->setup4567);
2371 cpu_to_le32s (&u.raw [0]);
2372 cpu_to_le32s (&u.raw [1]);
2374 le16_to_cpus (&u.r.wValue);
2375 le16_to_cpus (&u.r.wIndex);
2376 le16_to_cpus (&u.r.wLength);
2379 writel (1 << SETUP_PACKET_INTERRUPT, &dev->regs->irqstat0);
2380 stat ^= (1 << SETUP_PACKET_INTERRUPT);
2382 /* watch control traffic at the token level, and force
2383 * synchronization before letting the status stage happen.
2384 * FIXME ignore tokens we'll NAK, until driver responds.
2385 * that'll mean a lot less irqs for some drivers.
2387 ep->is_in = (u.r.bRequestType & USB_DIR_IN) != 0;
2389 scratch = (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
2390 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
2391 | (1 << DATA_IN_TOKEN_INTERRUPT);
2392 stop_out_naking (ep);
2394 scratch = (1 << DATA_PACKET_RECEIVED_INTERRUPT)
2395 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
2396 | (1 << DATA_IN_TOKEN_INTERRUPT);
2397 writel (scratch, &dev->epregs [0].ep_irqenb);
2399 /* we made the hardware handle most lowlevel requests;
2400 * everything else goes uplevel to the gadget code.
2402 switch (u.r.bRequest) {
2403 case USB_REQ_GET_STATUS: {
2404 struct net2280_ep *e;
2407 /* hw handles device and interface status */
2408 if (u.r.bRequestType != (USB_DIR_IN|USB_RECIP_ENDPOINT))
2410 if ((e = get_ep_by_addr (dev, u.r.wIndex)) == 0
2414 if (readl (&e->regs->ep_rsp)
2415 & (1 << SET_ENDPOINT_HALT))
2416 status = __constant_cpu_to_le16 (1);
2418 status = __constant_cpu_to_le16 (0);
2420 /* don't bother with a request object! */
2421 writel (0, &dev->epregs [0].ep_irqenb);
2422 set_fifo_bytecount (ep, u.r.wLength);
2423 writel (status, &dev->epregs [0].ep_data);
2425 VDEBUG (dev, "%s stat %02x\n", ep->ep.name, status);
2426 goto next_endpoints;
2429 case USB_REQ_CLEAR_FEATURE: {
2430 struct net2280_ep *e;
2432 /* hw handles device features */
2433 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
2435 if (u.r.wValue != USB_ENDPOINT_HALT
2436 || u.r.wLength != 0)
2438 if ((e = get_ep_by_addr (dev, u.r.wIndex)) == 0)
2442 VDEBUG (dev, "%s clear halt\n", ep->ep.name);
2443 goto next_endpoints;
2446 case USB_REQ_SET_FEATURE: {
2447 struct net2280_ep *e;
2449 /* hw handles device features */
2450 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
2452 if (u.r.wValue != USB_ENDPOINT_HALT
2453 || u.r.wLength != 0)
2455 if ((e = get_ep_by_addr (dev, u.r.wIndex)) == 0)
2459 VDEBUG (dev, "%s set halt\n", ep->ep.name);
2460 goto next_endpoints;
2465 VDEBUG (dev, "setup %02x.%02x v%04x i%04x "
2467 u.r.bRequestType, u.r.bRequest,
2468 u.r.wValue, u.r.wIndex,
2469 readl (&ep->regs->ep_cfg));
2470 spin_unlock (&dev->lock);
2471 tmp = dev->driver->setup (&dev->gadget, &u.r);
2472 spin_lock (&dev->lock);
2475 /* stall ep0 on error */
2478 VDEBUG (dev, "req %02x.%02x protocol STALL; stat %d\n",
2479 u.r.bRequestType, u.r.bRequest, tmp);
2480 dev->protocol_stall = 1;
2483 /* some in/out token irq should follow; maybe stall then.
2484 * driver must queue a request (even zlp) or halt ep0
2485 * before the host times out.
2490 /* endpoint data irq ? */
2491 scratch = stat & 0x7f;
2493 for (num = 0; scratch; num++) {
2496 /* do this endpoint's FIFO and queue need tending? */
2498 if ((scratch & t) == 0)
2502 ep = &dev->ep [num];
2503 handle_ep_small (ep);
2507 DEBUG (dev, "unhandled irqstat0 %08x\n", stat);
2510 #define DMA_INTERRUPTS ( \
2511 (1 << DMA_D_INTERRUPT) \
2512 | (1 << DMA_C_INTERRUPT) \
2513 | (1 << DMA_B_INTERRUPT) \
2514 | (1 << DMA_A_INTERRUPT))
2515 #define PCI_ERROR_INTERRUPTS ( \
2516 (1 << PCI_MASTER_ABORT_RECEIVED_INTERRUPT) \
2517 | (1 << PCI_TARGET_ABORT_RECEIVED_INTERRUPT) \
2518 | (1 << PCI_RETRY_ABORT_INTERRUPT))
2520 static void handle_stat1_irqs (struct net2280 *dev, u32 stat)
2522 struct net2280_ep *ep;
2523 u32 tmp, num, mask, scratch;
2525 /* after disconnect there's nothing else to do! */
2526 tmp = (1 << VBUS_INTERRUPT) | (1 << ROOT_PORT_RESET_INTERRUPT);
2527 mask = (1 << HIGH_SPEED) | (1 << FULL_SPEED);
2529 /* VBUS disconnect is indicated by VBUS_PIN and VBUS_INTERRUPT set.
2530 * Root Port Reset is indicated by ROOT_PORT_RESET_INTERRRUPT set and
2531 * both HIGH_SPEED and FULL_SPEED clear (as ROOT_PORT_RESET_INTERRUPT
2532 * only indicates a change in the reset state).
2535 writel (tmp, &dev->regs->irqstat1);
2536 if ((((stat & (1 << ROOT_PORT_RESET_INTERRUPT)) &&
2537 ((readl (&dev->usb->usbstat) & mask) == 0))
2538 || ((readl (&dev->usb->usbctl) & (1 << VBUS_PIN)) == 0)
2539 ) && ( dev->gadget.speed != USB_SPEED_UNKNOWN)) {
2540 DEBUG (dev, "disconnect %s\n",
2541 dev->driver->driver.name);
2542 stop_activity (dev, dev->driver);
2548 /* vBUS can bounce ... one of many reasons to ignore the
2549 * notion of hotplug events on bus connect/disconnect!
2555 /* NOTE: chip stays in PCI D0 state for now, but it could
2556 * enter D1 to save more power
2558 tmp = (1 << SUSPEND_REQUEST_CHANGE_INTERRUPT);
2560 writel (tmp, &dev->regs->irqstat1);
2561 if (stat & (1 << SUSPEND_REQUEST_INTERRUPT)) {
2562 if (dev->driver->suspend)
2563 dev->driver->suspend (&dev->gadget);
2565 if (dev->driver->resume)
2566 dev->driver->resume (&dev->gadget);
2567 /* at high speed, note erratum 0133 */
2572 /* clear any other status/irqs */
2574 writel (stat, &dev->regs->irqstat1);
2576 /* some status we can just ignore */
2577 stat &= ~((1 << CONTROL_STATUS_INTERRUPT)
2578 | (1 << SUSPEND_REQUEST_INTERRUPT)
2579 | (1 << RESUME_INTERRUPT)
2580 | (1 << SOF_INTERRUPT));
2583 // DEBUG (dev, "irqstat1 %08x\n", stat);
2585 /* DMA status, for ep-{a,b,c,d} */
2586 scratch = stat & DMA_INTERRUPTS;
2587 stat &= ~DMA_INTERRUPTS;
2589 for (num = 0; scratch; num++) {
2590 struct net2280_dma_regs __iomem *dma;
2593 if ((tmp & scratch) == 0)
2597 ep = &dev->ep [num + 1];
2603 /* clear ep's dma status */
2604 tmp = readl (&dma->dmastat);
2605 writel (tmp, &dma->dmastat);
2607 /* chaining should stop on abort, short OUT from fifo,
2608 * or (stat0 codepath) short OUT transfer.
2610 if (!use_dma_chaining) {
2611 if ((tmp & (1 << DMA_TRANSACTION_DONE_INTERRUPT))
2613 DEBUG (ep->dev, "%s no xact done? %08x\n",
2620 /* OUT transfers terminate when the data from the
2621 * host is in our memory. Process whatever's done.
2622 * On this path, we know transfer's last packet wasn't
2623 * less than req->length. NAK_OUT_PACKETS may be set,
2624 * or the FIFO may already be holding new packets.
2626 * IN transfers can linger in the FIFO for a very
2627 * long time ... we ignore that for now, accounting
2628 * precisely (like PIO does) needs per-packet irqs
2630 scan_dma_completions (ep);
2632 /* disable dma on inactive queues; else maybe restart */
2633 if (list_empty (&ep->queue)) {
2634 if (use_dma_chaining)
2637 tmp = readl (&dma->dmactl);
2638 if (!use_dma_chaining
2639 || (tmp & (1 << DMA_ENABLE)) == 0)
2641 else if (ep->is_in && use_dma_chaining) {
2642 struct net2280_request *req;
2645 /* the descriptor at the head of the chain
2646 * may still have VALID_BIT clear; that's
2647 * used to trigger changing DMA_FIFO_VALIDATE
2648 * (affects automagic zlp writes).
2650 req = list_entry (ep->queue.next,
2651 struct net2280_request, queue);
2652 dmacount = req->td->dmacount;
2653 dmacount &= __constant_cpu_to_le32 (
2655 | DMA_BYTE_COUNT_MASK);
2656 if (dmacount && (dmacount & valid_bit) == 0)
2663 /* NOTE: there are other PCI errors we might usefully notice.
2664 * if they appear very often, here's where to try recovering.
2666 if (stat & PCI_ERROR_INTERRUPTS) {
2667 ERROR (dev, "pci dma error; stat %08x\n", stat);
2668 stat &= ~PCI_ERROR_INTERRUPTS;
2669 /* these are fatal errors, but "maybe" they won't
2672 stop_activity (dev, dev->driver);
2678 DEBUG (dev, "unhandled irqstat1 %08x\n", stat);
2681 static irqreturn_t net2280_irq (int irq, void *_dev, struct pt_regs * r)
2683 struct net2280 *dev = _dev;
2685 spin_lock (&dev->lock);
2687 /* handle disconnect, dma, and more */
2688 handle_stat1_irqs (dev, readl (&dev->regs->irqstat1));
2690 /* control requests and PIO */
2691 handle_stat0_irqs (dev, readl (&dev->regs->irqstat0));
2693 spin_unlock (&dev->lock);
2698 /*-------------------------------------------------------------------------*/
2700 static void gadget_release (struct device *_dev)
2702 struct net2280 *dev = dev_get_drvdata (_dev);
2707 /* tear down the binding between this driver and the pci device */
2709 static void net2280_remove (struct pci_dev *pdev)
2711 struct net2280 *dev = pci_get_drvdata (pdev);
2713 /* start with the driver above us */
2715 /* should have been done already by driver model core */
2716 WARN (dev, "pci remove, driver '%s' is still registered\n",
2717 dev->driver->driver.name);
2718 usb_gadget_unregister_driver (dev->driver);
2721 /* then clean up the resources we allocated during probe() */
2722 net2280_led_shutdown (dev);
2723 if (dev->requests) {
2725 for (i = 1; i < 5; i++) {
2726 if (!dev->ep [i].dummy)
2728 pci_pool_free (dev->requests, dev->ep [i].dummy,
2729 dev->ep [i].td_dma);
2731 pci_pool_destroy (dev->requests);
2734 free_irq (pdev->irq, dev);
2736 iounmap (dev->regs);
2738 release_mem_region (pci_resource_start (pdev, 0),
2739 pci_resource_len (pdev, 0));
2741 pci_disable_device (pdev);
2742 device_unregister (&dev->gadget.dev);
2743 device_remove_file (&pdev->dev, &dev_attr_registers);
2744 pci_set_drvdata (pdev, NULL);
2746 INFO (dev, "unbind\n");
2748 the_controller = NULL;
2751 /* wrap this driver around the specified device, but
2752 * don't respond over USB until a gadget driver binds to us.
2755 static int net2280_probe (struct pci_dev *pdev, const struct pci_device_id *id)
2757 struct net2280 *dev;
2758 unsigned long resource, len;
2759 void __iomem *base = NULL;
2761 char buf [8], *bufp;
2763 /* if you want to support more than one controller in a system,
2764 * usb_gadget_driver_{register,unregister}() must change.
2766 if (the_controller) {
2767 dev_warn (&pdev->dev, "ignoring\n");
2771 /* alloc, and start init */
2772 dev = kmalloc (sizeof *dev, SLAB_KERNEL);
2778 memset (dev, 0, sizeof *dev);
2779 spin_lock_init (&dev->lock);
2781 dev->gadget.ops = &net2280_ops;
2782 dev->gadget.is_dualspeed = 1;
2784 /* the "gadget" abstracts/virtualizes the controller */
2785 strcpy (dev->gadget.dev.bus_id, "gadget");
2786 dev->gadget.dev.parent = &pdev->dev;
2787 dev->gadget.dev.dma_mask = pdev->dev.dma_mask;
2788 dev->gadget.dev.release = gadget_release;
2789 dev->gadget.name = driver_name;
2791 /* now all the pci goodies ... */
2792 if (pci_enable_device (pdev) < 0) {
2798 /* BAR 0 holds all the registers
2799 * BAR 1 is 8051 memory; unused here (note erratum 0103)
2800 * BAR 2 is fifo memory; unused here
2802 resource = pci_resource_start (pdev, 0);
2803 len = pci_resource_len (pdev, 0);
2804 if (!request_mem_region (resource, len, driver_name)) {
2805 DEBUG (dev, "controller already in use\n");
2811 base = ioremap_nocache (resource, len);
2813 DEBUG (dev, "can't map memory\n");
2817 dev->regs = (struct net2280_regs __iomem *) base;
2818 dev->usb = (struct net2280_usb_regs __iomem *) (base + 0x0080);
2819 dev->pci = (struct net2280_pci_regs __iomem *) (base + 0x0100);
2820 dev->dma = (struct net2280_dma_regs __iomem *) (base + 0x0180);
2821 dev->dep = (struct net2280_dep_regs __iomem *) (base + 0x0200);
2822 dev->epregs = (struct net2280_ep_regs __iomem *) (base + 0x0300);
2824 /* put into initial config, link up all endpoints */
2825 writel (0, &dev->usb->usbctl);
2829 /* irq setup after old hardware is cleaned up */
2831 ERROR (dev, "No IRQ. Check PCI setup!\n");
2836 scnprintf (buf, sizeof buf, "%d", pdev->irq);
2839 bufp = __irq_itoa(pdev->irq);
2841 if (request_irq (pdev->irq, net2280_irq, SA_SHIRQ, driver_name, dev)
2843 ERROR (dev, "request interrupt %s failed\n", bufp);
2850 /* NOTE: we know only the 32 LSBs of dma addresses may be nonzero */
2851 dev->requests = pci_pool_create ("requests", pdev,
2852 sizeof (struct net2280_dma),
2853 0 /* no alignment requirements */,
2854 0 /* or page-crossing issues */);
2855 if (!dev->requests) {
2856 DEBUG (dev, "can't get request pool\n");
2860 for (i = 1; i < 5; i++) {
2861 struct net2280_dma *td;
2863 td = pci_pool_alloc (dev->requests, GFP_KERNEL,
2864 &dev->ep [i].td_dma);
2866 DEBUG (dev, "can't get dummy %d\n", i);
2870 td->dmacount = 0; /* not VALID */
2871 td->dmaaddr = __constant_cpu_to_le32 (DMA_ADDR_INVALID);
2872 td->dmadesc = td->dmaaddr;
2873 dev->ep [i].dummy = td;
2876 /* enable lower-overhead pci memory bursts during DMA */
2877 writel ( (1 << DMA_MEMORY_WRITE_AND_INVALIDATE_ENABLE)
2878 // 256 write retries may not be enough...
2879 // | (1 << PCI_RETRY_ABORT_ENABLE)
2880 | (1 << DMA_READ_MULTIPLE_ENABLE)
2881 | (1 << DMA_READ_LINE_ENABLE)
2882 , &dev->pci->pcimstctl);
2883 /* erratum 0115 shouldn't appear: Linux inits PCI_LATENCY_TIMER */
2884 pci_set_master (pdev);
2887 /* ... also flushes any posted pci writes */
2888 dev->chiprev = get_idx_reg (dev->regs, REG_CHIPREV) & 0xffff;
2891 pci_set_drvdata (pdev, dev);
2892 INFO (dev, "%s\n", driver_desc);
2893 INFO (dev, "irq %s, pci mem %p, chip rev %04x\n",
2894 bufp, base, dev->chiprev);
2895 INFO (dev, "version: " DRIVER_VERSION "; dma %s\n",
2897 ? (use_dma_chaining ? "chaining" : "enabled")
2899 the_controller = dev;
2901 device_register (&dev->gadget.dev);
2902 device_create_file (&pdev->dev, &dev_attr_registers);
2908 net2280_remove (pdev);
2913 /*-------------------------------------------------------------------------*/
2915 static struct pci_device_id pci_ids [] = { {
2916 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
2920 .subvendor = PCI_ANY_ID,
2921 .subdevice = PCI_ANY_ID,
2923 }, { /* end: all zeroes */ }
2925 MODULE_DEVICE_TABLE (pci, pci_ids);
2927 /* pci driver glue; this is a "new style" PCI driver module */
2928 static struct pci_driver net2280_pci_driver = {
2929 .name = (char *) driver_name,
2930 .id_table = pci_ids,
2932 .probe = net2280_probe,
2933 .remove = net2280_remove,
2935 /* FIXME add power management support */
2938 MODULE_DESCRIPTION (DRIVER_DESC);
2939 MODULE_AUTHOR ("David Brownell");
2940 MODULE_LICENSE ("GPL");
2942 static int __init init (void)
2945 use_dma_chaining = 0;
2946 return pci_register_driver (&net2280_pci_driver);
2950 static void __exit cleanup (void)
2952 pci_unregister_driver (&net2280_pci_driver);
2954 module_exit (cleanup);