2 * Driver for the PLX NET2280 USB device controller.
3 * Specs and errata are available from <http://www.plxtech.com>.
5 * PLX Technology Inc. (formerly NetChip Technology) supported the
6 * development of this driver.
9 * CODE STATUS HIGHLIGHTS
11 * This driver should work well with most "gadget" drivers, including
12 * the File Storage, Serial, and Ethernet/RNDIS gadget drivers
13 * as well as Gadget Zero and Gadgetfs.
15 * DMA is enabled by default. Drivers using transfer queues might use
16 * DMA chaining to remove IRQ latencies between transfers. (Except when
17 * short OUT transfers happen.) Drivers can use the req->no_interrupt
18 * hint to completely eliminate some IRQs, if a later IRQ is guaranteed
19 * and DMA chaining is enabled.
21 * Note that almost all the errata workarounds here are only needed for
22 * rev1 chips. Rev1a silicon (0110) fixes almost all of them.
26 * Copyright (C) 2003 David Brownell
27 * Copyright (C) 2003-2005 PLX Technology, Inc.
29 * Modified Seth Levy 2005 PLX Technology, Inc. to provide compatibility with 2282 chip
31 * This program is free software; you can redistribute it and/or modify
32 * it under the terms of the GNU General Public License as published by
33 * the Free Software Foundation; either version 2 of the License, or
34 * (at your option) any later version.
36 * This program is distributed in the hope that it will be useful,
37 * but WITHOUT ANY WARRANTY; without even the implied warranty of
38 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
39 * GNU General Public License for more details.
41 * You should have received a copy of the GNU General Public License
42 * along with this program; if not, write to the Free Software
43 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
46 #undef DEBUG /* messages on error and most fault paths */
47 #undef VERBOSE /* extra debug messages (success too) */
49 #include <linux/config.h>
50 #include <linux/module.h>
51 #include <linux/pci.h>
52 #include <linux/dma-mapping.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 "PLX NET228x USB Peripheral Controller"
77 #define DRIVER_VERSION "2005 Sept 27"
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 */
85 static const char driver_name [] = "net2280";
86 static const char driver_desc [] = DRIVER_DESC;
88 static const char ep0name [] = "ep0";
89 static const char *ep_name [] = {
91 "ep-a", "ep-b", "ep-c", "ep-d",
95 /* use_dma -- general goodness, fewer interrupts, less cpu load (vs PIO)
96 * use_dma_chaining -- dma descriptor queueing gives even more irq reduction
98 * The net2280 DMA engines are not tightly integrated with their FIFOs;
99 * not all cases are (yet) handled well in this driver or the silicon.
100 * Some gadget drivers work better with the dma support here than others.
101 * These two parameters let you use PIO or more aggressive DMA.
103 static int use_dma = 1;
104 static int use_dma_chaining = 0;
106 /* "modprobe net2280 use_dma=n" etc */
107 module_param (use_dma, bool, S_IRUGO);
108 module_param (use_dma_chaining, bool, S_IRUGO);
111 /* mode 0 == ep-{a,b,c,d} 1K fifo each
112 * mode 1 == ep-{a,b} 2K fifo each, ep-{c,d} unavailable
113 * mode 2 == ep-a 2K fifo, ep-{b,c} 1K each, ep-d unavailable
115 static ushort fifo_mode = 0;
117 /* "modprobe net2280 fifo_mode=1" etc */
118 module_param (fifo_mode, ushort, 0644);
120 /* enable_suspend -- When enabled, the driver will respond to
121 * USB suspend requests by powering down the NET2280. Otherwise,
122 * USB suspend requests will be ignored. This is acceptible for
123 * self-powered devices
125 static int enable_suspend = 0;
127 /* "modprobe net2280 enable_suspend=1" etc */
128 module_param (enable_suspend, bool, S_IRUGO);
131 #define DIR_STRING(bAddress) (((bAddress) & USB_DIR_IN) ? "in" : "out")
133 #if defined(CONFIG_USB_GADGET_DEBUG_FILES) || defined (DEBUG)
134 static char *type_string (u8 bmAttributes)
136 switch ((bmAttributes) & USB_ENDPOINT_XFERTYPE_MASK) {
137 case USB_ENDPOINT_XFER_BULK: return "bulk";
138 case USB_ENDPOINT_XFER_ISOC: return "iso";
139 case USB_ENDPOINT_XFER_INT: return "intr";
147 #define valid_bit __constant_cpu_to_le32 (1 << VALID_BIT)
148 #define dma_done_ie __constant_cpu_to_le32 (1 << DMA_DONE_INTERRUPT_ENABLE)
150 /*-------------------------------------------------------------------------*/
153 net2280_enable (struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
156 struct net2280_ep *ep;
160 ep = container_of (_ep, struct net2280_ep, ep);
161 if (!_ep || !desc || ep->desc || _ep->name == ep0name
162 || desc->bDescriptorType != USB_DT_ENDPOINT)
165 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
168 /* erratum 0119 workaround ties up an endpoint number */
169 if ((desc->bEndpointAddress & 0x0f) == EP_DONTUSE)
172 /* sanity check ep-e/ep-f since their fifos are small */
173 max = le16_to_cpu (desc->wMaxPacketSize) & 0x1fff;
174 if (ep->num > 4 && max > 64)
177 spin_lock_irqsave (&dev->lock, flags);
178 _ep->maxpacket = max & 0x7ff;
181 /* ep_reset() has already been called */
183 ep->out_overflow = 0;
185 /* set speed-dependent max packet; may kick in high bandwidth */
186 set_idx_reg (dev->regs, REG_EP_MAXPKT (dev, ep->num), max);
188 /* FIFO lines can't go to different packets. PIO is ok, so
189 * use it instead of troublesome (non-bulk) multi-packet DMA.
191 if (ep->dma && (max % 4) != 0 && use_dma_chaining) {
192 DEBUG (ep->dev, "%s, no dma for maxpacket %d\n",
193 ep->ep.name, ep->ep.maxpacket);
197 /* set type, direction, address; reset fifo counters */
198 writel ((1 << FIFO_FLUSH), &ep->regs->ep_stat);
199 tmp = (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK);
200 if (tmp == USB_ENDPOINT_XFER_INT) {
201 /* erratum 0105 workaround prevents hs NYET */
202 if (dev->chiprev == 0100
203 && dev->gadget.speed == USB_SPEED_HIGH
204 && !(desc->bEndpointAddress & USB_DIR_IN))
205 writel ((1 << CLEAR_NAK_OUT_PACKETS_MODE),
207 } else if (tmp == USB_ENDPOINT_XFER_BULK) {
208 /* catch some particularly blatant driver bugs */
209 if ((dev->gadget.speed == USB_SPEED_HIGH
211 || (dev->gadget.speed == USB_SPEED_FULL
213 spin_unlock_irqrestore (&dev->lock, flags);
217 ep->is_iso = (tmp == USB_ENDPOINT_XFER_ISOC) ? 1 : 0;
218 tmp <<= ENDPOINT_TYPE;
219 tmp |= desc->bEndpointAddress;
220 tmp |= (4 << ENDPOINT_BYTE_COUNT); /* default full fifo lines */
221 tmp |= 1 << ENDPOINT_ENABLE;
224 /* for OUT transfers, block the rx fifo until a read is posted */
225 ep->is_in = (tmp & USB_DIR_IN) != 0;
227 writel ((1 << SET_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
228 else if (dev->pdev->device != 0x2280) {
229 /* Added for 2282, Don't use nak packets on an in endpoint, this was ignored on 2280 */
230 writel ((1 << CLEAR_NAK_OUT_PACKETS)
231 | (1 << CLEAR_NAK_OUT_PACKETS_MODE), &ep->regs->ep_rsp);
234 writel (tmp, &ep->regs->ep_cfg);
237 if (!ep->dma) { /* pio, per-packet */
238 tmp = (1 << ep->num) | readl (&dev->regs->pciirqenb0);
239 writel (tmp, &dev->regs->pciirqenb0);
241 tmp = (1 << DATA_PACKET_RECEIVED_INTERRUPT_ENABLE)
242 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT_ENABLE);
243 if (dev->pdev->device == 0x2280)
244 tmp |= readl (&ep->regs->ep_irqenb);
245 writel (tmp, &ep->regs->ep_irqenb);
246 } else { /* dma, per-request */
247 tmp = (1 << (8 + ep->num)); /* completion */
248 tmp |= readl (&dev->regs->pciirqenb1);
249 writel (tmp, &dev->regs->pciirqenb1);
251 /* for short OUT transfers, dma completions can't
252 * advance the queue; do it pio-style, by hand.
253 * NOTE erratum 0112 workaround #2
255 if ((desc->bEndpointAddress & USB_DIR_IN) == 0) {
256 tmp = (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT_ENABLE);
257 writel (tmp, &ep->regs->ep_irqenb);
259 tmp = (1 << ep->num) | readl (&dev->regs->pciirqenb0);
260 writel (tmp, &dev->regs->pciirqenb0);
264 tmp = desc->bEndpointAddress;
265 DEBUG (dev, "enabled %s (ep%d%s-%s) %s max %04x\n",
266 _ep->name, tmp & 0x0f, DIR_STRING (tmp),
267 type_string (desc->bmAttributes),
268 ep->dma ? "dma" : "pio", max);
270 /* pci writes may still be posted */
271 spin_unlock_irqrestore (&dev->lock, flags);
275 static int handshake (u32 __iomem *ptr, u32 mask, u32 done, int usec)
280 result = readl (ptr);
281 if (result == ~(u32)0) /* "device unplugged" */
292 static struct usb_ep_ops net2280_ep_ops;
294 static void ep_reset (struct net2280_regs __iomem *regs, struct net2280_ep *ep)
299 INIT_LIST_HEAD (&ep->queue);
301 ep->ep.maxpacket = ~0;
302 ep->ep.ops = &net2280_ep_ops;
304 /* disable the dma, irqs, endpoint... */
306 writel (0, &ep->dma->dmactl);
307 writel ( (1 << DMA_SCATTER_GATHER_DONE_INTERRUPT)
308 | (1 << DMA_TRANSACTION_DONE_INTERRUPT)
310 , &ep->dma->dmastat);
312 tmp = readl (®s->pciirqenb0);
313 tmp &= ~(1 << ep->num);
314 writel (tmp, ®s->pciirqenb0);
316 tmp = readl (®s->pciirqenb1);
317 tmp &= ~(1 << (8 + ep->num)); /* completion */
318 writel (tmp, ®s->pciirqenb1);
320 writel (0, &ep->regs->ep_irqenb);
322 /* init to our chosen defaults, notably so that we NAK OUT
323 * packets until the driver queues a read (+note erratum 0112)
325 if (!ep->is_in || ep->dev->pdev->device == 0x2280) {
326 tmp = (1 << SET_NAK_OUT_PACKETS_MODE)
327 | (1 << SET_NAK_OUT_PACKETS)
328 | (1 << CLEAR_EP_HIDE_STATUS_PHASE)
329 | (1 << CLEAR_INTERRUPT_MODE);
332 tmp = (1 << CLEAR_NAK_OUT_PACKETS_MODE)
333 | (1 << CLEAR_NAK_OUT_PACKETS)
334 | (1 << CLEAR_EP_HIDE_STATUS_PHASE)
335 | (1 << CLEAR_INTERRUPT_MODE);
339 tmp |= (1 << CLEAR_ENDPOINT_TOGGLE)
340 | (1 << CLEAR_ENDPOINT_HALT);
342 writel (tmp, &ep->regs->ep_rsp);
344 /* scrub most status bits, and flush any fifo state */
345 if (ep->dev->pdev->device == 0x2280)
346 tmp = (1 << FIFO_OVERFLOW)
347 | (1 << FIFO_UNDERFLOW);
351 writel (tmp | (1 << TIMEOUT)
352 | (1 << USB_STALL_SENT)
353 | (1 << USB_IN_NAK_SENT)
354 | (1 << USB_IN_ACK_RCVD)
355 | (1 << USB_OUT_PING_NAK_SENT)
356 | (1 << USB_OUT_ACK_SENT)
358 | (1 << SHORT_PACKET_OUT_DONE_INTERRUPT)
359 | (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)
360 | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
361 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
362 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
363 | (1 << DATA_IN_TOKEN_INTERRUPT)
364 , &ep->regs->ep_stat);
366 /* fifo size is handled separately */
369 static void nuke (struct net2280_ep *);
371 static int net2280_disable (struct usb_ep *_ep)
373 struct net2280_ep *ep;
376 ep = container_of (_ep, struct net2280_ep, ep);
377 if (!_ep || !ep->desc || _ep->name == ep0name)
380 spin_lock_irqsave (&ep->dev->lock, flags);
382 ep_reset (ep->dev->regs, ep);
384 VDEBUG (ep->dev, "disabled %s %s\n",
385 ep->dma ? "dma" : "pio", _ep->name);
387 /* synch memory views with the device */
388 (void) readl (&ep->regs->ep_cfg);
390 if (use_dma && !ep->dma && ep->num >= 1 && ep->num <= 4)
391 ep->dma = &ep->dev->dma [ep->num - 1];
393 spin_unlock_irqrestore (&ep->dev->lock, flags);
397 /*-------------------------------------------------------------------------*/
399 static struct usb_request *
400 net2280_alloc_request (struct usb_ep *_ep, gfp_t gfp_flags)
402 struct net2280_ep *ep;
403 struct net2280_request *req;
407 ep = container_of (_ep, struct net2280_ep, ep);
409 req = kzalloc(sizeof(*req), gfp_flags);
413 req->req.dma = DMA_ADDR_INVALID;
414 INIT_LIST_HEAD (&req->queue);
416 /* this dma descriptor may be swapped with the previous dummy */
418 struct net2280_dma *td;
420 td = pci_pool_alloc (ep->dev->requests, gfp_flags,
426 td->dmacount = 0; /* not VALID */
427 td->dmaaddr = __constant_cpu_to_le32 (DMA_ADDR_INVALID);
428 td->dmadesc = td->dmaaddr;
435 net2280_free_request (struct usb_ep *_ep, struct usb_request *_req)
437 struct net2280_ep *ep;
438 struct net2280_request *req;
440 ep = container_of (_ep, struct net2280_ep, ep);
444 req = container_of (_req, struct net2280_request, req);
445 WARN_ON (!list_empty (&req->queue));
447 pci_pool_free (ep->dev->requests, req->td, req->td_dma);
451 /*-------------------------------------------------------------------------*/
455 /* many common platforms have dma-coherent caches, which means that it's
456 * safe to use kmalloc() memory for all i/o buffers without using any
457 * cache flushing calls. (unless you're trying to share cache lines
458 * between dma and non-dma activities, which is a slow idea in any case.)
460 * other platforms need more care, with 2.5 having a moderately general
461 * solution (which falls down for allocations smaller than one page)
462 * that improves significantly on the 2.4 PCI allocators by removing
463 * the restriction that memory never be freed in_interrupt().
465 #if defined(CONFIG_X86)
468 #elif defined(CONFIG_PPC) && !defined(CONFIG_NOT_COHERENT_CACHE)
471 #elif defined(CONFIG_MIPS) && !defined(CONFIG_DMA_NONCOHERENT)
474 /* FIXME there are other cases, including an x86-64 one ... */
477 /* allocating buffers this way eliminates dma mapping overhead, which
478 * on some platforms will mean eliminating a per-io buffer copy. with
479 * some kinds of system caches, further tweaks may still be needed.
482 net2280_alloc_buffer (
490 struct net2280_ep *ep;
492 ep = container_of (_ep, struct net2280_ep, ep);
495 *dma = DMA_ADDR_INVALID;
497 #if defined(USE_KMALLOC)
498 retval = kmalloc(bytes, gfp_flags);
500 *dma = virt_to_phys(retval);
503 /* the main problem with this call is that it wastes memory
504 * on typical 1/N page allocations: it allocates 1-N pages.
506 #warning Using dma_alloc_coherent even with buffers smaller than a page.
507 retval = dma_alloc_coherent(&ep->dev->pdev->dev,
508 bytes, dma, gfp_flags);
510 retval = kmalloc(bytes, gfp_flags);
516 net2280_free_buffer (
522 /* free memory into the right allocator */
524 if (dma != DMA_ADDR_INVALID) {
525 struct net2280_ep *ep;
527 ep = container_of(_ep, struct net2280_ep, ep);
530 dma_free_coherent(&ep->dev->pdev->dev, bytes, buf, dma);
536 /*-------------------------------------------------------------------------*/
538 /* load a packet into the fifo we use for usb IN transfers.
539 * works for all endpoints.
541 * NOTE: pio with ep-a..ep-d could stuff multiple packets into the fifo
542 * at a time, but this code is simpler because it knows it only writes
543 * one packet. ep-a..ep-d should use dma instead.
546 write_fifo (struct net2280_ep *ep, struct usb_request *req)
548 struct net2280_ep_regs __iomem *regs = ep->regs;
551 unsigned count, total;
553 /* INVARIANT: fifo is currently empty. (testable) */
556 buf = req->buf + req->actual;
558 total = req->length - req->actual;
564 /* write just one packet at a time */
565 count = ep->ep.maxpacket;
566 if (count > total) /* min() cannot be used on a bitfield */
569 VDEBUG (ep->dev, "write %s fifo (IN) %d bytes%s req %p\n",
571 (count != ep->ep.maxpacket) ? " (short)" : "",
574 /* NOTE be careful if you try to align these. fifo lines
575 * should normally be full (4 bytes) and successive partial
576 * lines are ok only in certain cases.
578 tmp = get_unaligned ((u32 *)buf);
580 writel (tmp, ®s->ep_data);
585 /* last fifo entry is "short" unless we wrote a full packet.
586 * also explicitly validate last word in (periodic) transfers
587 * when maxpacket is not a multiple of 4 bytes.
589 if (count || total < ep->ep.maxpacket) {
590 tmp = count ? get_unaligned ((u32 *)buf) : count;
592 set_fifo_bytecount (ep, count & 0x03);
593 writel (tmp, ®s->ep_data);
596 /* pci writes may still be posted */
599 /* work around erratum 0106: PCI and USB race over the OUT fifo.
600 * caller guarantees chiprev 0100, out endpoint is NAKing, and
601 * there's no real data in the fifo.
603 * NOTE: also used in cases where that erratum doesn't apply:
604 * where the host wrote "too much" data to us.
606 static void out_flush (struct net2280_ep *ep)
611 ASSERT_OUT_NAKING (ep);
613 statp = &ep->regs->ep_stat;
614 writel ( (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
615 | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
617 writel ((1 << FIFO_FLUSH), statp);
620 if (tmp & (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
621 /* high speed did bulk NYET; fifo isn't filling */
622 && ep->dev->gadget.speed == USB_SPEED_FULL) {
625 usec = 50; /* 64 byte bulk/interrupt */
626 handshake (statp, (1 << USB_OUT_PING_NAK_SENT),
627 (1 << USB_OUT_PING_NAK_SENT), usec);
628 /* NAK done; now CLEAR_NAK_OUT_PACKETS is safe */
632 /* unload packet(s) from the fifo we use for usb OUT transfers.
633 * returns true iff the request completed, because of short packet
634 * or the request buffer having filled with full packets.
636 * for ep-a..ep-d this will read multiple packets out when they
637 * have been accepted.
640 read_fifo (struct net2280_ep *ep, struct net2280_request *req)
642 struct net2280_ep_regs __iomem *regs = ep->regs;
643 u8 *buf = req->req.buf + req->req.actual;
644 unsigned count, tmp, is_short;
645 unsigned cleanup = 0, prevent = 0;
647 /* erratum 0106 ... packets coming in during fifo reads might
648 * be incompletely rejected. not all cases have workarounds.
650 if (ep->dev->chiprev == 0x0100
651 && ep->dev->gadget.speed == USB_SPEED_FULL) {
653 tmp = readl (&ep->regs->ep_stat);
654 if ((tmp & (1 << NAK_OUT_PACKETS)))
656 else if ((tmp & (1 << FIFO_FULL))) {
657 start_out_naking (ep);
660 /* else: hope we don't see the problem */
663 /* never overflow the rx buffer. the fifo reads packets until
664 * it sees a short one; we might not be ready for them all.
667 count = readl (®s->ep_avail);
668 if (unlikely (count == 0)) {
670 tmp = readl (&ep->regs->ep_stat);
671 count = readl (®s->ep_avail);
672 /* handled that data already? */
673 if (count == 0 && (tmp & (1 << NAK_OUT_PACKETS)) == 0)
677 tmp = req->req.length - req->req.actual;
679 /* as with DMA, data overflow gets flushed */
680 if ((tmp % ep->ep.maxpacket) != 0) {
682 "%s out fifo %d bytes, expected %d\n",
683 ep->ep.name, count, tmp);
684 req->req.status = -EOVERFLOW;
686 /* NAK_OUT_PACKETS will be set, so flushing is safe;
687 * the next read will start with the next packet
689 } /* else it's a ZLP, no worries */
692 req->req.actual += count;
694 is_short = (count == 0) || ((count % ep->ep.maxpacket) != 0);
696 VDEBUG (ep->dev, "read %s fifo (OUT) %d bytes%s%s%s req %p %d/%d\n",
697 ep->ep.name, count, is_short ? " (short)" : "",
698 cleanup ? " flush" : "", prevent ? " nak" : "",
699 req, req->req.actual, req->req.length);
702 tmp = readl (®s->ep_data);
704 put_unaligned (tmp, (u32 *)buf);
709 tmp = readl (®s->ep_data);
710 /* LE conversion is implicit here: */
719 writel ((1 << CLEAR_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
720 (void) readl (&ep->regs->ep_rsp);
723 return is_short || ((req->req.actual == req->req.length)
727 /* fill out dma descriptor to match a given request */
729 fill_dma_desc (struct net2280_ep *ep, struct net2280_request *req, int valid)
731 struct net2280_dma *td = req->td;
732 u32 dmacount = req->req.length;
734 /* don't let DMA continue after a short OUT packet,
735 * so overruns can't affect the next transfer.
736 * in case of overruns on max-size packets, we can't
737 * stop the fifo from filling but we can flush it.
740 dmacount |= (1 << DMA_DIRECTION);
741 if ((!ep->is_in && (dmacount % ep->ep.maxpacket) != 0) || ep->dev->pdev->device != 0x2280)
742 dmacount |= (1 << END_OF_CHAIN);
746 dmacount |= (1 << VALID_BIT);
747 if (likely(!req->req.no_interrupt || !use_dma_chaining))
748 dmacount |= (1 << DMA_DONE_INTERRUPT_ENABLE);
750 /* td->dmadesc = previously set by caller */
751 td->dmaaddr = cpu_to_le32 (req->req.dma);
753 /* 2280 may be polling VALID_BIT through ep->dma->dmadesc */
755 td->dmacount = cpu_to_le32p (&dmacount);
758 static const u32 dmactl_default =
759 (1 << DMA_SCATTER_GATHER_DONE_INTERRUPT)
760 | (1 << DMA_CLEAR_COUNT_ENABLE)
761 /* erratum 0116 workaround part 1 (use POLLING) */
762 | (POLL_100_USEC << DESCRIPTOR_POLLING_RATE)
763 | (1 << DMA_VALID_BIT_POLLING_ENABLE)
764 | (1 << DMA_VALID_BIT_ENABLE)
765 | (1 << DMA_SCATTER_GATHER_ENABLE)
766 /* erratum 0116 workaround part 2 (no AUTOSTART) */
769 static inline void spin_stop_dma (struct net2280_dma_regs __iomem *dma)
771 handshake (&dma->dmactl, (1 << DMA_ENABLE), 0, 50);
774 static inline void stop_dma (struct net2280_dma_regs __iomem *dma)
776 writel (readl (&dma->dmactl) & ~(1 << DMA_ENABLE), &dma->dmactl);
780 static void start_queue (struct net2280_ep *ep, u32 dmactl, u32 td_dma)
782 struct net2280_dma_regs __iomem *dma = ep->dma;
783 unsigned int tmp = (1 << VALID_BIT) | (ep->is_in << DMA_DIRECTION);
785 if (ep->dev->pdev->device != 0x2280)
786 tmp |= (1 << END_OF_CHAIN);
788 writel (tmp, &dma->dmacount);
789 writel (readl (&dma->dmastat), &dma->dmastat);
791 writel (td_dma, &dma->dmadesc);
792 writel (dmactl, &dma->dmactl);
794 /* erratum 0116 workaround part 3: pci arbiter away from net2280 */
795 (void) readl (&ep->dev->pci->pcimstctl);
797 writel ((1 << DMA_START), &dma->dmastat);
800 stop_out_naking (ep);
803 static void start_dma (struct net2280_ep *ep, struct net2280_request *req)
806 struct net2280_dma_regs __iomem *dma = ep->dma;
808 /* FIXME can't use DMA for ZLPs */
810 /* on this path we "know" there's no dma active (yet) */
811 WARN_ON (readl (&dma->dmactl) & (1 << DMA_ENABLE));
812 writel (0, &ep->dma->dmactl);
814 /* previous OUT packet might have been short */
815 if (!ep->is_in && ((tmp = readl (&ep->regs->ep_stat))
816 & (1 << NAK_OUT_PACKETS)) != 0) {
817 writel ((1 << SHORT_PACKET_TRANSFERRED_INTERRUPT),
820 tmp = readl (&ep->regs->ep_avail);
822 writel (readl (&dma->dmastat), &dma->dmastat);
824 /* transfer all/some fifo data */
825 writel (req->req.dma, &dma->dmaaddr);
826 tmp = min (tmp, req->req.length);
828 /* dma irq, faking scatterlist status */
829 req->td->dmacount = cpu_to_le32 (req->req.length - tmp);
830 writel ((1 << DMA_DONE_INTERRUPT_ENABLE)
831 | tmp, &dma->dmacount);
832 req->td->dmadesc = 0;
835 writel ((1 << DMA_ENABLE), &dma->dmactl);
836 writel ((1 << DMA_START), &dma->dmastat);
841 tmp = dmactl_default;
843 /* force packet boundaries between dma requests, but prevent the
844 * controller from automagically writing a last "short" packet
845 * (zero length) unless the driver explicitly said to do that.
848 if (likely ((req->req.length % ep->ep.maxpacket) != 0
850 tmp |= (1 << DMA_FIFO_VALIDATE);
851 ep->in_fifo_validate = 1;
853 ep->in_fifo_validate = 0;
856 /* init req->td, pointing to the current dummy */
857 req->td->dmadesc = cpu_to_le32 (ep->td_dma);
858 fill_dma_desc (ep, req, 1);
860 if (!use_dma_chaining)
861 req->td->dmacount |= __constant_cpu_to_le32 (1 << END_OF_CHAIN);
863 start_queue (ep, tmp, req->td_dma);
867 queue_dma (struct net2280_ep *ep, struct net2280_request *req, int valid)
869 struct net2280_dma *end;
872 /* swap new dummy for old, link; fill and maybe activate */
878 ep->td_dma = req->td_dma;
881 end->dmadesc = cpu_to_le32 (ep->td_dma);
883 fill_dma_desc (ep, req, valid);
887 done (struct net2280_ep *ep, struct net2280_request *req, int status)
890 unsigned stopped = ep->stopped;
892 list_del_init (&req->queue);
894 if (req->req.status == -EINPROGRESS)
895 req->req.status = status;
897 status = req->req.status;
901 pci_unmap_single (dev->pdev, req->req.dma, req->req.length,
902 ep->is_in ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
903 req->req.dma = DMA_ADDR_INVALID;
907 if (status && status != -ESHUTDOWN)
908 VDEBUG (dev, "complete %s req %p stat %d len %u/%u\n",
909 ep->ep.name, &req->req, status,
910 req->req.actual, req->req.length);
912 /* don't modify queue heads during completion callback */
914 spin_unlock (&dev->lock);
915 req->req.complete (&ep->ep, &req->req);
916 spin_lock (&dev->lock);
917 ep->stopped = stopped;
920 /*-------------------------------------------------------------------------*/
923 net2280_queue (struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
925 struct net2280_request *req;
926 struct net2280_ep *ep;
930 /* we always require a cpu-view buffer, so that we can
931 * always use pio (as fallback or whatever).
933 req = container_of (_req, struct net2280_request, req);
934 if (!_req || !_req->complete || !_req->buf
935 || !list_empty (&req->queue))
937 if (_req->length > (~0 & DMA_BYTE_COUNT_MASK))
939 ep = container_of (_ep, struct net2280_ep, ep);
940 if (!_ep || (!ep->desc && ep->num != 0))
943 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
946 /* FIXME implement PIO fallback for ZLPs with DMA */
947 if (ep->dma && _req->length == 0)
950 /* set up dma mapping in case the caller didn't */
951 if (ep->dma && _req->dma == DMA_ADDR_INVALID) {
952 _req->dma = pci_map_single (dev->pdev, _req->buf, _req->length,
953 ep->is_in ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
958 VDEBUG (dev, "%s queue req %p, len %d buf %p\n",
959 _ep->name, _req, _req->length, _req->buf);
962 spin_lock_irqsave (&dev->lock, flags);
964 _req->status = -EINPROGRESS;
967 /* kickstart this i/o queue? */
968 if (list_empty (&ep->queue) && !ep->stopped) {
969 /* use DMA if the endpoint supports it, else pio */
973 /* maybe there's no control data, just status ack */
974 if (ep->num == 0 && _req->length == 0) {
977 VDEBUG (dev, "%s status ack\n", ep->ep.name);
981 /* PIO ... stuff the fifo, or unblock it. */
983 write_fifo (ep, _req);
984 else if (list_empty (&ep->queue)) {
987 /* OUT FIFO might have packet(s) buffered */
988 s = readl (&ep->regs->ep_stat);
989 if ((s & (1 << FIFO_EMPTY)) == 0) {
990 /* note: _req->short_not_ok is
991 * ignored here since PIO _always_
992 * stops queue advance here, and
993 * _req->status doesn't change for
994 * short reads (only _req->actual)
996 if (read_fifo (ep, req)) {
1000 /* don't queue it */
1003 s = readl (&ep->regs->ep_stat);
1006 /* don't NAK, let the fifo fill */
1007 if (req && (s & (1 << NAK_OUT_PACKETS)))
1008 writel ((1 << CLEAR_NAK_OUT_PACKETS),
1013 } else if (ep->dma) {
1019 /* preventing magic zlps is per-engine state, not
1020 * per-transfer; irq logic must recover hiccups.
1022 expect = likely (req->req.zero
1023 || (req->req.length % ep->ep.maxpacket) != 0);
1024 if (expect != ep->in_fifo_validate)
1027 queue_dma (ep, req, valid);
1029 } /* else the irq handler advances the queue. */
1032 list_add_tail (&req->queue, &ep->queue);
1034 spin_unlock_irqrestore (&dev->lock, flags);
1036 /* pci writes may still be posted */
1042 struct net2280_ep *ep,
1043 struct net2280_request *req,
1048 req->req.actual = req->req.length - (DMA_BYTE_COUNT_MASK & dmacount);
1049 done (ep, req, status);
1052 static void restart_dma (struct net2280_ep *ep);
1054 static void scan_dma_completions (struct net2280_ep *ep)
1056 /* only look at descriptors that were "naturally" retired,
1057 * so fifo and list head state won't matter
1059 while (!list_empty (&ep->queue)) {
1060 struct net2280_request *req;
1063 req = list_entry (ep->queue.next,
1064 struct net2280_request, queue);
1068 tmp = le32_to_cpup (&req->td->dmacount);
1069 if ((tmp & (1 << VALID_BIT)) != 0)
1072 /* SHORT_PACKET_TRANSFERRED_INTERRUPT handles "usb-short"
1073 * cases where DMA must be aborted; this code handles
1074 * all non-abort DMA completions.
1076 if (unlikely (req->td->dmadesc == 0)) {
1078 tmp = readl (&ep->dma->dmacount);
1079 if (tmp & DMA_BYTE_COUNT_MASK)
1081 /* single transfer mode */
1082 dma_done (ep, req, tmp, 0);
1084 } else if (!ep->is_in
1085 && (req->req.length % ep->ep.maxpacket) != 0) {
1086 tmp = readl (&ep->regs->ep_stat);
1088 /* AVOID TROUBLE HERE by not issuing short reads from
1089 * your gadget driver. That helps avoids errata 0121,
1090 * 0122, and 0124; not all cases trigger the warning.
1092 if ((tmp & (1 << NAK_OUT_PACKETS)) == 0) {
1093 WARN (ep->dev, "%s lost packet sync!\n",
1095 req->req.status = -EOVERFLOW;
1096 } else if ((tmp = readl (&ep->regs->ep_avail)) != 0) {
1097 /* fifo gets flushed later */
1098 ep->out_overflow = 1;
1099 DEBUG (ep->dev, "%s dma, discard %d len %d\n",
1102 req->req.status = -EOVERFLOW;
1105 dma_done (ep, req, tmp, 0);
1109 static void restart_dma (struct net2280_ep *ep)
1111 struct net2280_request *req;
1112 u32 dmactl = dmactl_default;
1116 req = list_entry (ep->queue.next, struct net2280_request, queue);
1118 if (!use_dma_chaining) {
1119 start_dma (ep, req);
1123 /* the 2280 will be processing the queue unless queue hiccups after
1124 * the previous transfer:
1125 * IN: wanted automagic zlp, head doesn't (or vice versa)
1126 * DMA_FIFO_VALIDATE doesn't init from dma descriptors.
1127 * OUT: was "usb-short", we must restart.
1129 if (ep->is_in && !req->valid) {
1130 struct net2280_request *entry, *prev = NULL;
1131 int reqmode, done = 0;
1133 DEBUG (ep->dev, "%s dma hiccup td %p\n", ep->ep.name, req->td);
1134 ep->in_fifo_validate = likely (req->req.zero
1135 || (req->req.length % ep->ep.maxpacket) != 0);
1136 if (ep->in_fifo_validate)
1137 dmactl |= (1 << DMA_FIFO_VALIDATE);
1138 list_for_each_entry (entry, &ep->queue, queue) {
1143 dmacount = entry->td->dmacount;
1145 reqmode = likely (entry->req.zero
1146 || (entry->req.length
1147 % ep->ep.maxpacket) != 0);
1148 if (reqmode == ep->in_fifo_validate) {
1150 dmacount |= valid_bit;
1151 entry->td->dmacount = dmacount;
1155 /* force a hiccup */
1156 prev->td->dmacount |= dma_done_ie;
1161 /* walk the rest of the queue so unlinks behave */
1163 dmacount &= ~valid_bit;
1164 entry->td->dmacount = dmacount;
1169 writel (0, &ep->dma->dmactl);
1170 start_queue (ep, dmactl, req->td_dma);
1173 static void abort_dma (struct net2280_ep *ep)
1175 /* abort the current transfer */
1176 if (likely (!list_empty (&ep->queue))) {
1177 /* FIXME work around errata 0121, 0122, 0124 */
1178 writel ((1 << DMA_ABORT), &ep->dma->dmastat);
1179 spin_stop_dma (ep->dma);
1182 scan_dma_completions (ep);
1185 /* dequeue ALL requests */
1186 static void nuke (struct net2280_ep *ep)
1188 struct net2280_request *req;
1190 /* called with spinlock held */
1194 while (!list_empty (&ep->queue)) {
1195 req = list_entry (ep->queue.next,
1196 struct net2280_request,
1198 done (ep, req, -ESHUTDOWN);
1202 /* dequeue JUST ONE request */
1203 static int net2280_dequeue (struct usb_ep *_ep, struct usb_request *_req)
1205 struct net2280_ep *ep;
1206 struct net2280_request *req;
1207 unsigned long flags;
1211 ep = container_of (_ep, struct net2280_ep, ep);
1212 if (!_ep || (!ep->desc && ep->num != 0) || !_req)
1215 spin_lock_irqsave (&ep->dev->lock, flags);
1216 stopped = ep->stopped;
1218 /* quiesce dma while we patch the queue */
1222 dmactl = readl (&ep->dma->dmactl);
1223 /* WARNING erratum 0127 may kick in ... */
1225 scan_dma_completions (ep);
1228 /* make sure it's still queued on this endpoint */
1229 list_for_each_entry (req, &ep->queue, queue) {
1230 if (&req->req == _req)
1233 if (&req->req != _req) {
1234 spin_unlock_irqrestore (&ep->dev->lock, flags);
1238 /* queue head may be partially complete. */
1239 if (ep->queue.next == &req->queue) {
1241 DEBUG (ep->dev, "unlink (%s) dma\n", _ep->name);
1242 _req->status = -ECONNRESET;
1244 if (likely (ep->queue.next == &req->queue)) {
1245 // NOTE: misreports single-transfer mode
1246 req->td->dmacount = 0; /* invalidate */
1248 readl (&ep->dma->dmacount),
1252 DEBUG (ep->dev, "unlink (%s) pio\n", _ep->name);
1253 done (ep, req, -ECONNRESET);
1257 /* patch up hardware chaining data */
1258 } else if (ep->dma && use_dma_chaining) {
1259 if (req->queue.prev == ep->queue.next) {
1260 writel (le32_to_cpu (req->td->dmadesc),
1262 if (req->td->dmacount & dma_done_ie)
1263 writel (readl (&ep->dma->dmacount)
1264 | le32_to_cpu(dma_done_ie),
1265 &ep->dma->dmacount);
1267 struct net2280_request *prev;
1269 prev = list_entry (req->queue.prev,
1270 struct net2280_request, queue);
1271 prev->td->dmadesc = req->td->dmadesc;
1272 if (req->td->dmacount & dma_done_ie)
1273 prev->td->dmacount |= dma_done_ie;
1278 done (ep, req, -ECONNRESET);
1279 ep->stopped = stopped;
1282 /* turn off dma on inactive queues */
1283 if (list_empty (&ep->queue))
1285 else if (!ep->stopped) {
1286 /* resume current request, or start new one */
1288 writel (dmactl, &ep->dma->dmactl);
1290 start_dma (ep, list_entry (ep->queue.next,
1291 struct net2280_request, queue));
1295 spin_unlock_irqrestore (&ep->dev->lock, flags);
1299 /*-------------------------------------------------------------------------*/
1301 static int net2280_fifo_status (struct usb_ep *_ep);
1304 net2280_set_halt (struct usb_ep *_ep, int value)
1306 struct net2280_ep *ep;
1307 unsigned long flags;
1310 ep = container_of (_ep, struct net2280_ep, ep);
1311 if (!_ep || (!ep->desc && ep->num != 0))
1313 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1315 if (ep->desc /* not ep0 */ && (ep->desc->bmAttributes & 0x03)
1316 == USB_ENDPOINT_XFER_ISOC)
1319 spin_lock_irqsave (&ep->dev->lock, flags);
1320 if (!list_empty (&ep->queue))
1322 else if (ep->is_in && value && net2280_fifo_status (_ep) != 0)
1325 VDEBUG (ep->dev, "%s %s halt\n", _ep->name,
1326 value ? "set" : "clear");
1327 /* set/clear, then synch memory views with the device */
1330 ep->dev->protocol_stall = 1;
1335 (void) readl (&ep->regs->ep_rsp);
1337 spin_unlock_irqrestore (&ep->dev->lock, flags);
1343 net2280_fifo_status (struct usb_ep *_ep)
1345 struct net2280_ep *ep;
1348 ep = container_of (_ep, struct net2280_ep, ep);
1349 if (!_ep || (!ep->desc && ep->num != 0))
1351 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1354 avail = readl (&ep->regs->ep_avail) & ((1 << 12) - 1);
1355 if (avail > ep->fifo_size)
1358 avail = ep->fifo_size - avail;
1363 net2280_fifo_flush (struct usb_ep *_ep)
1365 struct net2280_ep *ep;
1367 ep = container_of (_ep, struct net2280_ep, ep);
1368 if (!_ep || (!ep->desc && ep->num != 0))
1370 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1373 writel ((1 << FIFO_FLUSH), &ep->regs->ep_stat);
1374 (void) readl (&ep->regs->ep_rsp);
1377 static struct usb_ep_ops net2280_ep_ops = {
1378 .enable = net2280_enable,
1379 .disable = net2280_disable,
1381 .alloc_request = net2280_alloc_request,
1382 .free_request = net2280_free_request,
1384 .alloc_buffer = net2280_alloc_buffer,
1385 .free_buffer = net2280_free_buffer,
1387 .queue = net2280_queue,
1388 .dequeue = net2280_dequeue,
1390 .set_halt = net2280_set_halt,
1391 .fifo_status = net2280_fifo_status,
1392 .fifo_flush = net2280_fifo_flush,
1395 /*-------------------------------------------------------------------------*/
1397 static int net2280_get_frame (struct usb_gadget *_gadget)
1399 struct net2280 *dev;
1400 unsigned long flags;
1405 dev = container_of (_gadget, struct net2280, gadget);
1406 spin_lock_irqsave (&dev->lock, flags);
1407 retval = get_idx_reg (dev->regs, REG_FRAME) & 0x03ff;
1408 spin_unlock_irqrestore (&dev->lock, flags);
1412 static int net2280_wakeup (struct usb_gadget *_gadget)
1414 struct net2280 *dev;
1416 unsigned long flags;
1420 dev = container_of (_gadget, struct net2280, gadget);
1422 spin_lock_irqsave (&dev->lock, flags);
1423 tmp = readl (&dev->usb->usbctl);
1424 if (tmp & (1 << DEVICE_REMOTE_WAKEUP_ENABLE))
1425 writel (1 << GENERATE_RESUME, &dev->usb->usbstat);
1426 spin_unlock_irqrestore (&dev->lock, flags);
1428 /* pci writes may still be posted */
1432 static int net2280_set_selfpowered (struct usb_gadget *_gadget, int value)
1434 struct net2280 *dev;
1436 unsigned long flags;
1440 dev = container_of (_gadget, struct net2280, gadget);
1442 spin_lock_irqsave (&dev->lock, flags);
1443 tmp = readl (&dev->usb->usbctl);
1445 tmp |= (1 << SELF_POWERED_STATUS);
1447 tmp &= ~(1 << SELF_POWERED_STATUS);
1448 writel (tmp, &dev->usb->usbctl);
1449 spin_unlock_irqrestore (&dev->lock, flags);
1454 static int net2280_pullup(struct usb_gadget *_gadget, int is_on)
1456 struct net2280 *dev;
1458 unsigned long flags;
1462 dev = container_of (_gadget, struct net2280, gadget);
1464 spin_lock_irqsave (&dev->lock, flags);
1465 tmp = readl (&dev->usb->usbctl);
1466 dev->softconnect = (is_on != 0);
1468 tmp |= (1 << USB_DETECT_ENABLE);
1470 tmp &= ~(1 << USB_DETECT_ENABLE);
1471 writel (tmp, &dev->usb->usbctl);
1472 spin_unlock_irqrestore (&dev->lock, flags);
1477 static const struct usb_gadget_ops net2280_ops = {
1478 .get_frame = net2280_get_frame,
1479 .wakeup = net2280_wakeup,
1480 .set_selfpowered = net2280_set_selfpowered,
1481 .pullup = net2280_pullup,
1484 /*-------------------------------------------------------------------------*/
1486 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1488 /* FIXME move these into procfs, and use seq_file.
1489 * Sysfs _still_ doesn't behave for arbitrarily sized files,
1490 * and also doesn't help products using this with 2.4 kernels.
1493 /* "function" sysfs attribute */
1495 show_function (struct device *_dev, struct device_attribute *attr, char *buf)
1497 struct net2280 *dev = dev_get_drvdata (_dev);
1500 || !dev->driver->function
1501 || strlen (dev->driver->function) > PAGE_SIZE)
1503 return scnprintf (buf, PAGE_SIZE, "%s\n", dev->driver->function);
1505 static DEVICE_ATTR (function, S_IRUGO, show_function, NULL);
1508 show_registers (struct device *_dev, struct device_attribute *attr, char *buf)
1510 struct net2280 *dev;
1513 unsigned long flags;
1518 dev = dev_get_drvdata (_dev);
1521 spin_lock_irqsave (&dev->lock, flags);
1524 s = dev->driver->driver.name;
1528 /* Main Control Registers */
1529 t = scnprintf (next, size, "%s version " DRIVER_VERSION
1530 ", chiprev %04x, dma %s\n\n"
1531 "devinit %03x fifoctl %08x gadget '%s'\n"
1532 "pci irqenb0 %02x irqenb1 %08x "
1533 "irqstat0 %04x irqstat1 %08x\n",
1534 driver_name, dev->chiprev,
1536 ? (use_dma_chaining ? "chaining" : "enabled")
1538 readl (&dev->regs->devinit),
1539 readl (&dev->regs->fifoctl),
1541 readl (&dev->regs->pciirqenb0),
1542 readl (&dev->regs->pciirqenb1),
1543 readl (&dev->regs->irqstat0),
1544 readl (&dev->regs->irqstat1));
1548 /* USB Control Registers */
1549 t1 = readl (&dev->usb->usbctl);
1550 t2 = readl (&dev->usb->usbstat);
1551 if (t1 & (1 << VBUS_PIN)) {
1552 if (t2 & (1 << HIGH_SPEED))
1554 else if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1558 /* full speed bit (6) not working?? */
1561 t = scnprintf (next, size,
1562 "stdrsp %08x usbctl %08x usbstat %08x "
1563 "addr 0x%02x (%s)\n",
1564 readl (&dev->usb->stdrsp), t1, t2,
1565 readl (&dev->usb->ouraddr), s);
1569 /* PCI Master Control Registers */
1571 /* DMA Control Registers */
1573 /* Configurable EP Control Registers */
1574 for (i = 0; i < 7; i++) {
1575 struct net2280_ep *ep;
1581 t1 = readl (&ep->regs->ep_cfg);
1582 t2 = readl (&ep->regs->ep_rsp) & 0xff;
1583 t = scnprintf (next, size,
1584 "\n%s\tcfg %05x rsp (%02x) %s%s%s%s%s%s%s%s"
1586 ep->ep.name, t1, t2,
1587 (t2 & (1 << CLEAR_NAK_OUT_PACKETS))
1589 (t2 & (1 << CLEAR_EP_HIDE_STATUS_PHASE))
1591 (t2 & (1 << CLEAR_EP_FORCE_CRC_ERROR))
1593 (t2 & (1 << CLEAR_INTERRUPT_MODE))
1594 ? "interrupt " : "",
1595 (t2 & (1<<CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE))
1597 (t2 & (1 << CLEAR_NAK_OUT_PACKETS_MODE))
1599 (t2 & (1 << CLEAR_ENDPOINT_TOGGLE))
1600 ? "DATA1 " : "DATA0 ",
1601 (t2 & (1 << CLEAR_ENDPOINT_HALT))
1603 readl (&ep->regs->ep_irqenb));
1607 t = scnprintf (next, size,
1608 "\tstat %08x avail %04x "
1610 readl (&ep->regs->ep_stat),
1611 readl (&ep->regs->ep_avail),
1612 t1 & 0x0f, DIR_STRING (t1),
1613 type_string (t1 >> 8),
1614 ep->stopped ? "*" : "");
1621 t = scnprintf (next, size,
1622 " dma\tctl %08x stat %08x count %08x\n"
1623 "\taddr %08x desc %08x\n",
1624 readl (&ep->dma->dmactl),
1625 readl (&ep->dma->dmastat),
1626 readl (&ep->dma->dmacount),
1627 readl (&ep->dma->dmaaddr),
1628 readl (&ep->dma->dmadesc));
1634 /* Indexed Registers */
1638 t = scnprintf (next, size, "\nirqs: ");
1641 for (i = 0; i < 7; i++) {
1642 struct net2280_ep *ep;
1647 t = scnprintf (next, size, " %s/%lu", ep->ep.name, ep->irqs);
1652 t = scnprintf (next, size, "\n");
1656 spin_unlock_irqrestore (&dev->lock, flags);
1658 return PAGE_SIZE - size;
1660 static DEVICE_ATTR (registers, S_IRUGO, show_registers, NULL);
1663 show_queues (struct device *_dev, struct device_attribute *attr, char *buf)
1665 struct net2280 *dev;
1668 unsigned long flags;
1671 dev = dev_get_drvdata (_dev);
1674 spin_lock_irqsave (&dev->lock, flags);
1676 for (i = 0; i < 7; i++) {
1677 struct net2280_ep *ep = &dev->ep [i];
1678 struct net2280_request *req;
1682 const struct usb_endpoint_descriptor *d;
1687 t = d->bEndpointAddress;
1688 t = scnprintf (next, size,
1689 "\n%s (ep%d%s-%s) max %04x %s fifo %d\n",
1690 ep->ep.name, t & USB_ENDPOINT_NUMBER_MASK,
1691 (t & USB_DIR_IN) ? "in" : "out",
1693 switch (d->bmAttributes & 0x03) {
1694 case USB_ENDPOINT_XFER_BULK:
1695 val = "bulk"; break;
1696 case USB_ENDPOINT_XFER_INT:
1697 val = "intr"; break;
1701 le16_to_cpu (d->wMaxPacketSize) & 0x1fff,
1702 ep->dma ? "dma" : "pio", ep->fifo_size
1704 } else /* ep0 should only have one transfer queued */
1705 t = scnprintf (next, size, "ep0 max 64 pio %s\n",
1706 ep->is_in ? "in" : "out");
1707 if (t <= 0 || t > size)
1712 if (list_empty (&ep->queue)) {
1713 t = scnprintf (next, size, "\t(nothing queued)\n");
1714 if (t <= 0 || t > size)
1720 list_for_each_entry (req, &ep->queue, queue) {
1721 if (ep->dma && req->td_dma == readl (&ep->dma->dmadesc))
1722 t = scnprintf (next, size,
1723 "\treq %p len %d/%d "
1724 "buf %p (dmacount %08x)\n",
1725 &req->req, req->req.actual,
1726 req->req.length, req->req.buf,
1727 readl (&ep->dma->dmacount));
1729 t = scnprintf (next, size,
1730 "\treq %p len %d/%d buf %p\n",
1731 &req->req, req->req.actual,
1732 req->req.length, req->req.buf);
1733 if (t <= 0 || t > size)
1739 struct net2280_dma *td;
1742 t = scnprintf (next, size, "\t td %08x "
1743 " count %08x buf %08x desc %08x\n",
1745 le32_to_cpu (td->dmacount),
1746 le32_to_cpu (td->dmaaddr),
1747 le32_to_cpu (td->dmadesc));
1748 if (t <= 0 || t > size)
1757 spin_unlock_irqrestore (&dev->lock, flags);
1758 return PAGE_SIZE - size;
1760 static DEVICE_ATTR (queues, S_IRUGO, show_queues, NULL);
1765 #define device_create_file(a,b) do {} while (0)
1766 #define device_remove_file device_create_file
1770 /*-------------------------------------------------------------------------*/
1772 /* another driver-specific mode might be a request type doing dma
1773 * to/from another device fifo instead of to/from memory.
1776 static void set_fifo_mode (struct net2280 *dev, int mode)
1778 /* keeping high bits preserves BAR2 */
1779 writel ((0xffff << PCI_BASE2_RANGE) | mode, &dev->regs->fifoctl);
1781 /* always ep-{a,b,e,f} ... maybe not ep-c or ep-d */
1782 INIT_LIST_HEAD (&dev->gadget.ep_list);
1783 list_add_tail (&dev->ep [1].ep.ep_list, &dev->gadget.ep_list);
1784 list_add_tail (&dev->ep [2].ep.ep_list, &dev->gadget.ep_list);
1787 list_add_tail (&dev->ep [3].ep.ep_list, &dev->gadget.ep_list);
1788 list_add_tail (&dev->ep [4].ep.ep_list, &dev->gadget.ep_list);
1789 dev->ep [1].fifo_size = dev->ep [2].fifo_size = 1024;
1792 dev->ep [1].fifo_size = dev->ep [2].fifo_size = 2048;
1795 list_add_tail (&dev->ep [3].ep.ep_list, &dev->gadget.ep_list);
1796 dev->ep [1].fifo_size = 2048;
1797 dev->ep [2].fifo_size = 1024;
1800 /* fifo sizes for ep0, ep-c, ep-d, ep-e, and ep-f never change */
1801 list_add_tail (&dev->ep [5].ep.ep_list, &dev->gadget.ep_list);
1802 list_add_tail (&dev->ep [6].ep.ep_list, &dev->gadget.ep_list);
1805 /* just declare this in any driver that really need it */
1806 extern int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode);
1809 * net2280_set_fifo_mode - change allocation of fifo buffers
1810 * @gadget: access to the net2280 device that will be updated
1811 * @mode: 0 for default, four 1kB buffers (ep-a through ep-d);
1812 * 1 for two 2kB buffers (ep-a and ep-b only);
1813 * 2 for one 2kB buffer (ep-a) and two 1kB ones (ep-b, ep-c).
1815 * returns zero on success, else negative errno. when this succeeds,
1816 * the contents of gadget->ep_list may have changed.
1818 * you may only call this function when endpoints a-d are all disabled.
1819 * use it whenever extra hardware buffering can help performance, such
1820 * as before enabling "high bandwidth" interrupt endpoints that use
1821 * maxpacket bigger than 512 (when double buffering would otherwise
1824 int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode)
1827 struct net2280 *dev;
1829 unsigned long flags;
1833 dev = container_of (gadget, struct net2280, gadget);
1835 spin_lock_irqsave (&dev->lock, flags);
1837 for (i = 1; i <= 4; i++)
1838 if (dev->ep [i].desc) {
1842 if (mode < 0 || mode > 2)
1845 set_fifo_mode (dev, mode);
1846 spin_unlock_irqrestore (&dev->lock, flags);
1850 DEBUG (dev, "fifo: ep-a 2K, ep-b 2K\n");
1852 DEBUG (dev, "fifo: ep-a 2K, ep-b 1K, ep-c 1K\n");
1853 /* else all are 1K */
1857 EXPORT_SYMBOL (net2280_set_fifo_mode);
1859 /*-------------------------------------------------------------------------*/
1861 /* keeping it simple:
1862 * - one bus driver, initted first;
1863 * - one function driver, initted second
1865 * most of the work to support multiple net2280 controllers would
1866 * be to associate this gadget driver (yes?) with all of them, or
1867 * perhaps to bind specific drivers to specific devices.
1870 static struct net2280 *the_controller;
1872 static void usb_reset (struct net2280 *dev)
1876 dev->gadget.speed = USB_SPEED_UNKNOWN;
1877 (void) readl (&dev->usb->usbctl);
1879 net2280_led_init (dev);
1881 /* disable automatic responses, and irqs */
1882 writel (0, &dev->usb->stdrsp);
1883 writel (0, &dev->regs->pciirqenb0);
1884 writel (0, &dev->regs->pciirqenb1);
1886 /* clear old dma and irq state */
1887 for (tmp = 0; tmp < 4; tmp++) {
1888 struct net2280_ep *ep = &dev->ep [tmp + 1];
1893 writel (~0, &dev->regs->irqstat0),
1894 writel (~(1 << SUSPEND_REQUEST_INTERRUPT), &dev->regs->irqstat1),
1896 /* reset, and enable pci */
1897 tmp = readl (&dev->regs->devinit)
1899 | (1 << FIFO_SOFT_RESET)
1900 | (1 << USB_SOFT_RESET)
1901 | (1 << M8051_RESET);
1902 writel (tmp, &dev->regs->devinit);
1904 /* standard fifo and endpoint allocations */
1905 set_fifo_mode (dev, (fifo_mode <= 2) ? fifo_mode : 0);
1908 static void usb_reinit (struct net2280 *dev)
1913 /* use_dma changes are ignored till next device re-init */
1916 /* basic endpoint init */
1917 for (tmp = 0; tmp < 7; tmp++) {
1918 struct net2280_ep *ep = &dev->ep [tmp];
1920 ep->ep.name = ep_name [tmp];
1924 if (tmp > 0 && tmp <= 4) {
1925 ep->fifo_size = 1024;
1927 ep->dma = &dev->dma [tmp - 1];
1930 ep->regs = &dev->epregs [tmp];
1931 ep_reset (dev->regs, ep);
1933 dev->ep [0].ep.maxpacket = 64;
1934 dev->ep [5].ep.maxpacket = 64;
1935 dev->ep [6].ep.maxpacket = 64;
1937 dev->gadget.ep0 = &dev->ep [0].ep;
1938 dev->ep [0].stopped = 0;
1939 INIT_LIST_HEAD (&dev->gadget.ep0->ep_list);
1941 /* we want to prevent lowlevel/insecure access from the USB host,
1942 * but erratum 0119 means this enable bit is ignored
1944 for (tmp = 0; tmp < 5; tmp++)
1945 writel (EP_DONTUSE, &dev->dep [tmp].dep_cfg);
1948 static void ep0_start (struct net2280 *dev)
1950 writel ( (1 << CLEAR_EP_HIDE_STATUS_PHASE)
1951 | (1 << CLEAR_NAK_OUT_PACKETS)
1952 | (1 << CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE)
1953 , &dev->epregs [0].ep_rsp);
1956 * hardware optionally handles a bunch of standard requests
1957 * that the API hides from drivers anyway. have it do so.
1958 * endpoint status/features are handled in software, to
1959 * help pass tests for some dubious behavior.
1961 writel ( (1 << SET_TEST_MODE)
1962 | (1 << SET_ADDRESS)
1963 | (1 << DEVICE_SET_CLEAR_DEVICE_REMOTE_WAKEUP)
1964 | (1 << GET_DEVICE_STATUS)
1965 | (1 << GET_INTERFACE_STATUS)
1966 , &dev->usb->stdrsp);
1967 writel ( (1 << USB_ROOT_PORT_WAKEUP_ENABLE)
1968 | (1 << SELF_POWERED_USB_DEVICE)
1969 | (1 << REMOTE_WAKEUP_SUPPORT)
1970 | (dev->softconnect << USB_DETECT_ENABLE)
1971 | (1 << SELF_POWERED_STATUS)
1972 , &dev->usb->usbctl);
1974 /* enable irqs so we can see ep0 and general operation */
1975 writel ( (1 << SETUP_PACKET_INTERRUPT_ENABLE)
1976 | (1 << ENDPOINT_0_INTERRUPT_ENABLE)
1977 , &dev->regs->pciirqenb0);
1978 writel ( (1 << PCI_INTERRUPT_ENABLE)
1979 | (1 << PCI_MASTER_ABORT_RECEIVED_INTERRUPT_ENABLE)
1980 | (1 << PCI_TARGET_ABORT_RECEIVED_INTERRUPT_ENABLE)
1981 | (1 << PCI_RETRY_ABORT_INTERRUPT_ENABLE)
1982 | (1 << VBUS_INTERRUPT_ENABLE)
1983 | (1 << ROOT_PORT_RESET_INTERRUPT_ENABLE)
1984 | (1 << SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE)
1985 , &dev->regs->pciirqenb1);
1987 /* don't leave any writes posted */
1988 (void) readl (&dev->usb->usbctl);
1991 /* when a driver is successfully registered, it will receive
1992 * control requests including set_configuration(), which enables
1993 * non-control requests. then usb traffic follows until a
1994 * disconnect is reported. then a host may connect again, or
1995 * the driver might get unbound.
1997 int usb_gadget_register_driver (struct usb_gadget_driver *driver)
1999 struct net2280 *dev = the_controller;
2003 /* insist on high speed support from the driver, since
2004 * (dev->usb->xcvrdiag & FORCE_FULL_SPEED_MODE)
2005 * "must not be used in normal operation"
2008 || driver->speed != USB_SPEED_HIGH
2018 for (i = 0; i < 7; i++)
2019 dev->ep [i].irqs = 0;
2021 /* hook up the driver ... */
2022 dev->softconnect = 1;
2023 driver->driver.bus = NULL;
2024 dev->driver = driver;
2025 dev->gadget.dev.driver = &driver->driver;
2026 retval = driver->bind (&dev->gadget);
2028 DEBUG (dev, "bind to driver %s --> %d\n",
2029 driver->driver.name, retval);
2031 dev->gadget.dev.driver = NULL;
2035 device_create_file (&dev->pdev->dev, &dev_attr_function);
2036 device_create_file (&dev->pdev->dev, &dev_attr_queues);
2038 /* ... then enable host detection and ep0; and we're ready
2039 * for set_configuration as well as eventual disconnect.
2041 net2280_led_active (dev, 1);
2044 DEBUG (dev, "%s ready, usbctl %08x stdrsp %08x\n",
2045 driver->driver.name,
2046 readl (&dev->usb->usbctl),
2047 readl (&dev->usb->stdrsp));
2049 /* pci writes may still be posted */
2052 EXPORT_SYMBOL (usb_gadget_register_driver);
2055 stop_activity (struct net2280 *dev, struct usb_gadget_driver *driver)
2059 /* don't disconnect if it's not connected */
2060 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
2063 /* stop hardware; prevent new request submissions;
2064 * and kill any outstanding requests.
2067 for (i = 0; i < 7; i++)
2068 nuke (&dev->ep [i]);
2070 /* report disconnect; the driver is already quiesced */
2072 spin_unlock (&dev->lock);
2073 driver->disconnect (&dev->gadget);
2074 spin_lock (&dev->lock);
2080 int usb_gadget_unregister_driver (struct usb_gadget_driver *driver)
2082 struct net2280 *dev = the_controller;
2083 unsigned long flags;
2087 if (!driver || driver != dev->driver)
2090 spin_lock_irqsave (&dev->lock, flags);
2091 stop_activity (dev, driver);
2092 spin_unlock_irqrestore (&dev->lock, flags);
2094 net2280_pullup (&dev->gadget, 0);
2096 driver->unbind (&dev->gadget);
2097 dev->gadget.dev.driver = NULL;
2100 net2280_led_active (dev, 0);
2101 device_remove_file (&dev->pdev->dev, &dev_attr_function);
2102 device_remove_file (&dev->pdev->dev, &dev_attr_queues);
2104 DEBUG (dev, "unregistered driver '%s'\n", driver->driver.name);
2107 EXPORT_SYMBOL (usb_gadget_unregister_driver);
2110 /*-------------------------------------------------------------------------*/
2112 /* handle ep0, ep-e, ep-f with 64 byte packets: packet per irq.
2113 * also works for dma-capable endpoints, in pio mode or just
2114 * to manually advance the queue after short OUT transfers.
2116 static void handle_ep_small (struct net2280_ep *ep)
2118 struct net2280_request *req;
2120 /* 0 error, 1 mid-data, 2 done */
2123 if (!list_empty (&ep->queue))
2124 req = list_entry (ep->queue.next,
2125 struct net2280_request, queue);
2129 /* ack all, and handle what we care about */
2130 t = readl (&ep->regs->ep_stat);
2133 VDEBUG (ep->dev, "%s ack ep_stat %08x, req %p\n",
2134 ep->ep.name, t, req ? &req->req : 0);
2136 if (!ep->is_in || ep->dev->pdev->device == 0x2280)
2137 writel (t & ~(1 << NAK_OUT_PACKETS), &ep->regs->ep_stat);
2139 /* Added for 2282 */
2140 writel (t, &ep->regs->ep_stat);
2142 /* for ep0, monitor token irqs to catch data stage length errors
2143 * and to synchronize on status.
2145 * also, to defer reporting of protocol stalls ... here's where
2146 * data or status first appears, handling stalls here should never
2147 * cause trouble on the host side..
2149 * control requests could be slightly faster without token synch for
2150 * status, but status can jam up that way.
2152 if (unlikely (ep->num == 0)) {
2154 /* status; stop NAKing */
2155 if (t & (1 << DATA_OUT_PING_TOKEN_INTERRUPT)) {
2156 if (ep->dev->protocol_stall) {
2163 /* reply to extra IN data tokens with a zlp */
2164 } else if (t & (1 << DATA_IN_TOKEN_INTERRUPT)) {
2165 if (ep->dev->protocol_stall) {
2169 } else if (!req && ep->stopped)
2170 write_fifo (ep, NULL);
2173 /* status; stop NAKing */
2174 if (t & (1 << DATA_IN_TOKEN_INTERRUPT)) {
2175 if (ep->dev->protocol_stall) {
2180 /* an extra OUT token is an error */
2181 } else if (((t & (1 << DATA_OUT_PING_TOKEN_INTERRUPT))
2183 && req->req.actual == req->req.length)
2185 ep->dev->protocol_stall = 1;
2189 done (ep, req, -EOVERFLOW);
2195 if (unlikely (!req))
2198 /* manual DMA queue advance after short OUT */
2199 if (likely (ep->dma != 0)) {
2200 if (t & (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)) {
2202 int stopped = ep->stopped;
2204 /* TRANSFERRED works around OUT_DONE erratum 0112.
2205 * we expect (N <= maxpacket) bytes; host wrote M.
2206 * iff (M < N) we won't ever see a DMA interrupt.
2209 for (count = 0; ; t = readl (&ep->regs->ep_stat)) {
2211 /* any preceding dma transfers must finish.
2212 * dma handles (M >= N), may empty the queue
2214 scan_dma_completions (ep);
2215 if (unlikely (list_empty (&ep->queue)
2216 || ep->out_overflow)) {
2220 req = list_entry (ep->queue.next,
2221 struct net2280_request, queue);
2223 /* here either (M < N), a "real" short rx;
2224 * or (M == N) and the queue didn't empty
2226 if (likely (t & (1 << FIFO_EMPTY))) {
2227 count = readl (&ep->dma->dmacount);
2228 count &= DMA_BYTE_COUNT_MASK;
2229 if (readl (&ep->dma->dmadesc)
2237 /* stop DMA, leave ep NAKing */
2238 writel ((1 << DMA_ABORT), &ep->dma->dmastat);
2239 spin_stop_dma (ep->dma);
2242 req->td->dmacount = 0;
2243 t = readl (&ep->regs->ep_avail);
2244 dma_done (ep, req, count,
2245 (ep->out_overflow || t) ? -EOVERFLOW : 0);
2248 /* also flush to prevent erratum 0106 trouble */
2249 if (unlikely (ep->out_overflow
2250 || (ep->dev->chiprev == 0x0100
2251 && ep->dev->gadget.speed
2252 == USB_SPEED_FULL))) {
2254 ep->out_overflow = 0;
2257 /* (re)start dma if needed, stop NAKing */
2258 ep->stopped = stopped;
2259 if (!list_empty (&ep->queue))
2262 DEBUG (ep->dev, "%s dma ep_stat %08x ??\n",
2266 /* data packet(s) received (in the fifo, OUT) */
2267 } else if (t & (1 << DATA_PACKET_RECEIVED_INTERRUPT)) {
2268 if (read_fifo (ep, req) && ep->num != 0)
2271 /* data packet(s) transmitted (IN) */
2272 } else if (t & (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)) {
2275 len = req->req.length - req->req.actual;
2276 if (len > ep->ep.maxpacket)
2277 len = ep->ep.maxpacket;
2278 req->req.actual += len;
2280 /* if we wrote it all, we're usually done */
2281 if (req->req.actual == req->req.length) {
2283 /* wait for control status */
2286 } else if (!req->req.zero || len != ep->ep.maxpacket)
2290 /* there was nothing to do ... */
2291 } else if (mode == 1)
2296 /* stream endpoints often resubmit/unlink in completion */
2299 /* maybe advance queue to next request */
2301 /* NOTE: net2280 could let gadget driver start the
2302 * status stage later. since not all controllers let
2303 * them control that, the api doesn't (yet) allow it.
2309 if (!list_empty (&ep->queue) && !ep->stopped)
2310 req = list_entry (ep->queue.next,
2311 struct net2280_request, queue);
2314 if (req && !ep->is_in)
2315 stop_out_naking (ep);
2319 /* is there a buffer for the next packet?
2320 * for best streaming performance, make sure there is one.
2322 if (req && !ep->stopped) {
2324 /* load IN fifo with next packet (may be zlp) */
2325 if (t & (1 << DATA_PACKET_TRANSMITTED_INTERRUPT))
2326 write_fifo (ep, &req->req);
2330 static struct net2280_ep *
2331 get_ep_by_addr (struct net2280 *dev, u16 wIndex)
2333 struct net2280_ep *ep;
2335 if ((wIndex & USB_ENDPOINT_NUMBER_MASK) == 0)
2336 return &dev->ep [0];
2337 list_for_each_entry (ep, &dev->gadget.ep_list, ep.ep_list) {
2338 u8 bEndpointAddress;
2342 bEndpointAddress = ep->desc->bEndpointAddress;
2343 if ((wIndex ^ bEndpointAddress) & USB_DIR_IN)
2345 if ((wIndex & 0x0f) == (bEndpointAddress & 0x0f))
2351 static void handle_stat0_irqs (struct net2280 *dev, u32 stat)
2353 struct net2280_ep *ep;
2356 /* most of these don't need individual acks */
2357 stat &= ~(1 << INTA_ASSERTED);
2360 // DEBUG (dev, "irqstat0 %04x\n", stat);
2362 /* starting a control request? */
2363 if (unlikely (stat & (1 << SETUP_PACKET_INTERRUPT))) {
2366 struct usb_ctrlrequest r;
2369 struct net2280_request *req;
2371 if (dev->gadget.speed == USB_SPEED_UNKNOWN) {
2372 if (readl (&dev->usb->usbstat) & (1 << HIGH_SPEED))
2373 dev->gadget.speed = USB_SPEED_HIGH;
2375 dev->gadget.speed = USB_SPEED_FULL;
2376 net2280_led_speed (dev, dev->gadget.speed);
2377 DEBUG (dev, "%s speed\n",
2378 (dev->gadget.speed == USB_SPEED_HIGH)
2385 /* make sure any leftover request state is cleared */
2386 stat &= ~(1 << ENDPOINT_0_INTERRUPT);
2387 while (!list_empty (&ep->queue)) {
2388 req = list_entry (ep->queue.next,
2389 struct net2280_request, queue);
2390 done (ep, req, (req->req.actual == req->req.length)
2394 dev->protocol_stall = 0;
2396 if (ep->dev->pdev->device == 0x2280)
2397 tmp = (1 << FIFO_OVERFLOW)
2398 | (1 << FIFO_UNDERFLOW);
2402 writel (tmp | (1 << TIMEOUT)
2403 | (1 << USB_STALL_SENT)
2404 | (1 << USB_IN_NAK_SENT)
2405 | (1 << USB_IN_ACK_RCVD)
2406 | (1 << USB_OUT_PING_NAK_SENT)
2407 | (1 << USB_OUT_ACK_SENT)
2408 | (1 << SHORT_PACKET_OUT_DONE_INTERRUPT)
2409 | (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)
2410 | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
2411 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
2412 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
2413 | (1 << DATA_IN_TOKEN_INTERRUPT)
2414 , &ep->regs->ep_stat);
2415 u.raw [0] = readl (&dev->usb->setup0123);
2416 u.raw [1] = readl (&dev->usb->setup4567);
2418 cpu_to_le32s (&u.raw [0]);
2419 cpu_to_le32s (&u.raw [1]);
2423 #define w_value le16_to_cpup (&u.r.wValue)
2424 #define w_index le16_to_cpup (&u.r.wIndex)
2425 #define w_length le16_to_cpup (&u.r.wLength)
2428 writel (1 << SETUP_PACKET_INTERRUPT, &dev->regs->irqstat0);
2429 stat ^= (1 << SETUP_PACKET_INTERRUPT);
2431 /* watch control traffic at the token level, and force
2432 * synchronization before letting the status stage happen.
2433 * FIXME ignore tokens we'll NAK, until driver responds.
2434 * that'll mean a lot less irqs for some drivers.
2436 ep->is_in = (u.r.bRequestType & USB_DIR_IN) != 0;
2438 scratch = (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
2439 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
2440 | (1 << DATA_IN_TOKEN_INTERRUPT);
2441 stop_out_naking (ep);
2443 scratch = (1 << DATA_PACKET_RECEIVED_INTERRUPT)
2444 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
2445 | (1 << DATA_IN_TOKEN_INTERRUPT);
2446 writel (scratch, &dev->epregs [0].ep_irqenb);
2448 /* we made the hardware handle most lowlevel requests;
2449 * everything else goes uplevel to the gadget code.
2451 switch (u.r.bRequest) {
2452 case USB_REQ_GET_STATUS: {
2453 struct net2280_ep *e;
2456 /* hw handles device and interface status */
2457 if (u.r.bRequestType != (USB_DIR_IN|USB_RECIP_ENDPOINT))
2459 if ((e = get_ep_by_addr (dev, w_index)) == 0
2463 if (readl (&e->regs->ep_rsp)
2464 & (1 << SET_ENDPOINT_HALT))
2465 status = __constant_cpu_to_le32 (1);
2467 status = __constant_cpu_to_le32 (0);
2469 /* don't bother with a request object! */
2470 writel (0, &dev->epregs [0].ep_irqenb);
2471 set_fifo_bytecount (ep, w_length);
2472 writel ((__force u32)status, &dev->epregs [0].ep_data);
2474 VDEBUG (dev, "%s stat %02x\n", ep->ep.name, status);
2475 goto next_endpoints;
2478 case USB_REQ_CLEAR_FEATURE: {
2479 struct net2280_ep *e;
2481 /* hw handles device features */
2482 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
2484 if (w_value != USB_ENDPOINT_HALT
2487 if ((e = get_ep_by_addr (dev, w_index)) == 0)
2491 VDEBUG (dev, "%s clear halt\n", ep->ep.name);
2492 goto next_endpoints;
2495 case USB_REQ_SET_FEATURE: {
2496 struct net2280_ep *e;
2498 /* hw handles device features */
2499 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
2501 if (w_value != USB_ENDPOINT_HALT
2504 if ((e = get_ep_by_addr (dev, w_index)) == 0)
2508 VDEBUG (dev, "%s set halt\n", ep->ep.name);
2509 goto next_endpoints;
2514 VDEBUG (dev, "setup %02x.%02x v%04x i%04x l%04x"
2516 u.r.bRequestType, u.r.bRequest,
2517 w_value, w_index, w_length,
2518 readl (&ep->regs->ep_cfg));
2519 spin_unlock (&dev->lock);
2520 tmp = dev->driver->setup (&dev->gadget, &u.r);
2521 spin_lock (&dev->lock);
2524 /* stall ep0 on error */
2527 VDEBUG (dev, "req %02x.%02x protocol STALL; stat %d\n",
2528 u.r.bRequestType, u.r.bRequest, tmp);
2529 dev->protocol_stall = 1;
2532 /* some in/out token irq should follow; maybe stall then.
2533 * driver must queue a request (even zlp) or halt ep0
2534 * before the host times out.
2543 /* endpoint data irq ? */
2544 scratch = stat & 0x7f;
2546 for (num = 0; scratch; num++) {
2549 /* do this endpoint's FIFO and queue need tending? */
2551 if ((scratch & t) == 0)
2555 ep = &dev->ep [num];
2556 handle_ep_small (ep);
2560 DEBUG (dev, "unhandled irqstat0 %08x\n", stat);
2563 #define DMA_INTERRUPTS ( \
2564 (1 << DMA_D_INTERRUPT) \
2565 | (1 << DMA_C_INTERRUPT) \
2566 | (1 << DMA_B_INTERRUPT) \
2567 | (1 << DMA_A_INTERRUPT))
2568 #define PCI_ERROR_INTERRUPTS ( \
2569 (1 << PCI_MASTER_ABORT_RECEIVED_INTERRUPT) \
2570 | (1 << PCI_TARGET_ABORT_RECEIVED_INTERRUPT) \
2571 | (1 << PCI_RETRY_ABORT_INTERRUPT))
2573 static void handle_stat1_irqs (struct net2280 *dev, u32 stat)
2575 struct net2280_ep *ep;
2576 u32 tmp, num, mask, scratch;
2578 /* after disconnect there's nothing else to do! */
2579 tmp = (1 << VBUS_INTERRUPT) | (1 << ROOT_PORT_RESET_INTERRUPT);
2580 mask = (1 << HIGH_SPEED) | (1 << FULL_SPEED);
2582 /* VBUS disconnect is indicated by VBUS_PIN and VBUS_INTERRUPT set.
2583 * Root Port Reset is indicated by ROOT_PORT_RESET_INTERRRUPT set and
2584 * both HIGH_SPEED and FULL_SPEED clear (as ROOT_PORT_RESET_INTERRUPT
2585 * only indicates a change in the reset state).
2588 writel (tmp, &dev->regs->irqstat1);
2589 if ((((stat & (1 << ROOT_PORT_RESET_INTERRUPT)) &&
2590 ((readl (&dev->usb->usbstat) & mask) == 0))
2591 || ((readl (&dev->usb->usbctl) & (1 << VBUS_PIN)) == 0)
2592 ) && ( dev->gadget.speed != USB_SPEED_UNKNOWN)) {
2593 DEBUG (dev, "disconnect %s\n",
2594 dev->driver->driver.name);
2595 stop_activity (dev, dev->driver);
2601 /* vBUS can bounce ... one of many reasons to ignore the
2602 * notion of hotplug events on bus connect/disconnect!
2608 /* NOTE: chip stays in PCI D0 state for now, but it could
2609 * enter D1 to save more power
2611 tmp = (1 << SUSPEND_REQUEST_CHANGE_INTERRUPT);
2613 writel (tmp, &dev->regs->irqstat1);
2614 if (stat & (1 << SUSPEND_REQUEST_INTERRUPT)) {
2615 if (dev->driver->suspend)
2616 dev->driver->suspend (&dev->gadget);
2617 if (!enable_suspend)
2618 stat &= ~(1 << SUSPEND_REQUEST_INTERRUPT);
2620 if (dev->driver->resume)
2621 dev->driver->resume (&dev->gadget);
2622 /* at high speed, note erratum 0133 */
2627 /* clear any other status/irqs */
2629 writel (stat, &dev->regs->irqstat1);
2631 /* some status we can just ignore */
2632 if (dev->pdev->device == 0x2280)
2633 stat &= ~((1 << CONTROL_STATUS_INTERRUPT)
2634 | (1 << SUSPEND_REQUEST_INTERRUPT)
2635 | (1 << RESUME_INTERRUPT)
2636 | (1 << SOF_INTERRUPT));
2638 stat &= ~((1 << CONTROL_STATUS_INTERRUPT)
2639 | (1 << RESUME_INTERRUPT)
2640 | (1 << SOF_DOWN_INTERRUPT)
2641 | (1 << SOF_INTERRUPT));
2645 // DEBUG (dev, "irqstat1 %08x\n", stat);
2647 /* DMA status, for ep-{a,b,c,d} */
2648 scratch = stat & DMA_INTERRUPTS;
2649 stat &= ~DMA_INTERRUPTS;
2651 for (num = 0; scratch; num++) {
2652 struct net2280_dma_regs __iomem *dma;
2655 if ((tmp & scratch) == 0)
2659 ep = &dev->ep [num + 1];
2665 /* clear ep's dma status */
2666 tmp = readl (&dma->dmastat);
2667 writel (tmp, &dma->dmastat);
2669 /* chaining should stop on abort, short OUT from fifo,
2670 * or (stat0 codepath) short OUT transfer.
2672 if (!use_dma_chaining) {
2673 if ((tmp & (1 << DMA_TRANSACTION_DONE_INTERRUPT))
2675 DEBUG (ep->dev, "%s no xact done? %08x\n",
2682 /* OUT transfers terminate when the data from the
2683 * host is in our memory. Process whatever's done.
2684 * On this path, we know transfer's last packet wasn't
2685 * less than req->length. NAK_OUT_PACKETS may be set,
2686 * or the FIFO may already be holding new packets.
2688 * IN transfers can linger in the FIFO for a very
2689 * long time ... we ignore that for now, accounting
2690 * precisely (like PIO does) needs per-packet irqs
2692 scan_dma_completions (ep);
2694 /* disable dma on inactive queues; else maybe restart */
2695 if (list_empty (&ep->queue)) {
2696 if (use_dma_chaining)
2699 tmp = readl (&dma->dmactl);
2700 if (!use_dma_chaining
2701 || (tmp & (1 << DMA_ENABLE)) == 0)
2703 else if (ep->is_in && use_dma_chaining) {
2704 struct net2280_request *req;
2707 /* the descriptor at the head of the chain
2708 * may still have VALID_BIT clear; that's
2709 * used to trigger changing DMA_FIFO_VALIDATE
2710 * (affects automagic zlp writes).
2712 req = list_entry (ep->queue.next,
2713 struct net2280_request, queue);
2714 dmacount = req->td->dmacount;
2715 dmacount &= __constant_cpu_to_le32 (
2717 | DMA_BYTE_COUNT_MASK);
2718 if (dmacount && (dmacount & valid_bit) == 0)
2725 /* NOTE: there are other PCI errors we might usefully notice.
2726 * if they appear very often, here's where to try recovering.
2728 if (stat & PCI_ERROR_INTERRUPTS) {
2729 ERROR (dev, "pci dma error; stat %08x\n", stat);
2730 stat &= ~PCI_ERROR_INTERRUPTS;
2731 /* these are fatal errors, but "maybe" they won't
2734 stop_activity (dev, dev->driver);
2740 DEBUG (dev, "unhandled irqstat1 %08x\n", stat);
2743 static irqreturn_t net2280_irq (int irq, void *_dev, struct pt_regs * r)
2745 struct net2280 *dev = _dev;
2747 spin_lock (&dev->lock);
2749 /* handle disconnect, dma, and more */
2750 handle_stat1_irqs (dev, readl (&dev->regs->irqstat1));
2752 /* control requests and PIO */
2753 handle_stat0_irqs (dev, readl (&dev->regs->irqstat0));
2755 spin_unlock (&dev->lock);
2760 /*-------------------------------------------------------------------------*/
2762 static void gadget_release (struct device *_dev)
2764 struct net2280 *dev = dev_get_drvdata (_dev);
2769 /* tear down the binding between this driver and the pci device */
2771 static void net2280_remove (struct pci_dev *pdev)
2773 struct net2280 *dev = pci_get_drvdata (pdev);
2775 /* start with the driver above us */
2777 /* should have been done already by driver model core */
2778 WARN (dev, "pci remove, driver '%s' is still registered\n",
2779 dev->driver->driver.name);
2780 usb_gadget_unregister_driver (dev->driver);
2783 /* then clean up the resources we allocated during probe() */
2784 net2280_led_shutdown (dev);
2785 if (dev->requests) {
2787 for (i = 1; i < 5; i++) {
2788 if (!dev->ep [i].dummy)
2790 pci_pool_free (dev->requests, dev->ep [i].dummy,
2791 dev->ep [i].td_dma);
2793 pci_pool_destroy (dev->requests);
2796 free_irq (pdev->irq, dev);
2798 iounmap (dev->regs);
2800 release_mem_region (pci_resource_start (pdev, 0),
2801 pci_resource_len (pdev, 0));
2803 pci_disable_device (pdev);
2804 device_unregister (&dev->gadget.dev);
2805 device_remove_file (&pdev->dev, &dev_attr_registers);
2806 pci_set_drvdata (pdev, NULL);
2808 INFO (dev, "unbind\n");
2810 the_controller = NULL;
2813 /* wrap this driver around the specified device, but
2814 * don't respond over USB until a gadget driver binds to us.
2817 static int net2280_probe (struct pci_dev *pdev, const struct pci_device_id *id)
2819 struct net2280 *dev;
2820 unsigned long resource, len;
2821 void __iomem *base = NULL;
2823 char buf [8], *bufp;
2825 /* if you want to support more than one controller in a system,
2826 * usb_gadget_driver_{register,unregister}() must change.
2828 if (the_controller) {
2829 dev_warn (&pdev->dev, "ignoring\n");
2833 /* alloc, and start init */
2834 dev = kmalloc (sizeof *dev, SLAB_KERNEL);
2840 memset (dev, 0, sizeof *dev);
2841 spin_lock_init (&dev->lock);
2843 dev->gadget.ops = &net2280_ops;
2844 dev->gadget.is_dualspeed = 1;
2846 /* the "gadget" abstracts/virtualizes the controller */
2847 strcpy (dev->gadget.dev.bus_id, "gadget");
2848 dev->gadget.dev.parent = &pdev->dev;
2849 dev->gadget.dev.dma_mask = pdev->dev.dma_mask;
2850 dev->gadget.dev.release = gadget_release;
2851 dev->gadget.name = driver_name;
2853 /* now all the pci goodies ... */
2854 if (pci_enable_device (pdev) < 0) {
2860 /* BAR 0 holds all the registers
2861 * BAR 1 is 8051 memory; unused here (note erratum 0103)
2862 * BAR 2 is fifo memory; unused here
2864 resource = pci_resource_start (pdev, 0);
2865 len = pci_resource_len (pdev, 0);
2866 if (!request_mem_region (resource, len, driver_name)) {
2867 DEBUG (dev, "controller already in use\n");
2873 base = ioremap_nocache (resource, len);
2875 DEBUG (dev, "can't map memory\n");
2879 dev->regs = (struct net2280_regs __iomem *) base;
2880 dev->usb = (struct net2280_usb_regs __iomem *) (base + 0x0080);
2881 dev->pci = (struct net2280_pci_regs __iomem *) (base + 0x0100);
2882 dev->dma = (struct net2280_dma_regs __iomem *) (base + 0x0180);
2883 dev->dep = (struct net2280_dep_regs __iomem *) (base + 0x0200);
2884 dev->epregs = (struct net2280_ep_regs __iomem *) (base + 0x0300);
2886 /* put into initial config, link up all endpoints */
2887 writel (0, &dev->usb->usbctl);
2891 /* irq setup after old hardware is cleaned up */
2893 ERROR (dev, "No IRQ. Check PCI setup!\n");
2898 scnprintf (buf, sizeof buf, "%d", pdev->irq);
2901 bufp = __irq_itoa(pdev->irq);
2903 if (request_irq (pdev->irq, net2280_irq, SA_SHIRQ, driver_name, dev)
2905 ERROR (dev, "request interrupt %s failed\n", bufp);
2912 /* NOTE: we know only the 32 LSBs of dma addresses may be nonzero */
2913 dev->requests = pci_pool_create ("requests", pdev,
2914 sizeof (struct net2280_dma),
2915 0 /* no alignment requirements */,
2916 0 /* or page-crossing issues */);
2917 if (!dev->requests) {
2918 DEBUG (dev, "can't get request pool\n");
2922 for (i = 1; i < 5; i++) {
2923 struct net2280_dma *td;
2925 td = pci_pool_alloc (dev->requests, GFP_KERNEL,
2926 &dev->ep [i].td_dma);
2928 DEBUG (dev, "can't get dummy %d\n", i);
2932 td->dmacount = 0; /* not VALID */
2933 td->dmaaddr = __constant_cpu_to_le32 (DMA_ADDR_INVALID);
2934 td->dmadesc = td->dmaaddr;
2935 dev->ep [i].dummy = td;
2938 /* enable lower-overhead pci memory bursts during DMA */
2939 writel ( (1 << DMA_MEMORY_WRITE_AND_INVALIDATE_ENABLE)
2940 // 256 write retries may not be enough...
2941 // | (1 << PCI_RETRY_ABORT_ENABLE)
2942 | (1 << DMA_READ_MULTIPLE_ENABLE)
2943 | (1 << DMA_READ_LINE_ENABLE)
2944 , &dev->pci->pcimstctl);
2945 /* erratum 0115 shouldn't appear: Linux inits PCI_LATENCY_TIMER */
2946 pci_set_master (pdev);
2949 /* ... also flushes any posted pci writes */
2950 dev->chiprev = get_idx_reg (dev->regs, REG_CHIPREV) & 0xffff;
2953 pci_set_drvdata (pdev, dev);
2954 INFO (dev, "%s\n", driver_desc);
2955 INFO (dev, "irq %s, pci mem %p, chip rev %04x\n",
2956 bufp, base, dev->chiprev);
2957 INFO (dev, "version: " DRIVER_VERSION "; dma %s\n",
2959 ? (use_dma_chaining ? "chaining" : "enabled")
2961 the_controller = dev;
2963 device_register (&dev->gadget.dev);
2964 device_create_file (&pdev->dev, &dev_attr_registers);
2970 net2280_remove (pdev);
2975 /*-------------------------------------------------------------------------*/
2977 static struct pci_device_id pci_ids [] = { {
2978 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
2982 .subvendor = PCI_ANY_ID,
2983 .subdevice = PCI_ANY_ID,
2985 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
2989 .subvendor = PCI_ANY_ID,
2990 .subdevice = PCI_ANY_ID,
2992 }, { /* end: all zeroes */ }
2994 MODULE_DEVICE_TABLE (pci, pci_ids);
2996 /* pci driver glue; this is a "new style" PCI driver module */
2997 static struct pci_driver net2280_pci_driver = {
2998 .name = (char *) driver_name,
2999 .id_table = pci_ids,
3001 .probe = net2280_probe,
3002 .remove = net2280_remove,
3004 /* FIXME add power management support */
3007 MODULE_DESCRIPTION (DRIVER_DESC);
3008 MODULE_AUTHOR ("David Brownell");
3009 MODULE_LICENSE ("GPL");
3011 static int __init init (void)
3014 use_dma_chaining = 0;
3015 return pci_register_driver (&net2280_pci_driver);
3019 static void __exit cleanup (void)
3021 pci_unregister_driver (&net2280_pci_driver);
3023 module_exit (cleanup);