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 Mass 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
32 * This program is free software; you can redistribute it and/or modify
33 * it under the terms of the GNU General Public License as published by
34 * the Free Software Foundation; either version 2 of the License, or
35 * (at your option) any later version.
38 #undef DEBUG /* messages on error and most fault paths */
39 #undef VERBOSE /* extra debug messages (success too) */
41 #include <linux/module.h>
42 #include <linux/pci.h>
43 #include <linux/dma-mapping.h>
44 #include <linux/kernel.h>
45 #include <linux/delay.h>
46 #include <linux/ioport.h>
47 #include <linux/slab.h>
48 #include <linux/errno.h>
49 #include <linux/init.h>
50 #include <linux/timer.h>
51 #include <linux/list.h>
52 #include <linux/interrupt.h>
53 #include <linux/moduleparam.h>
54 #include <linux/device.h>
55 #include <linux/usb/ch9.h>
56 #include <linux/usb/gadget.h>
57 #include <linux/prefetch.h>
59 #include <asm/byteorder.h>
62 #include <asm/unaligned.h>
65 #define DRIVER_DESC "PLX NET228x USB Peripheral Controller"
66 #define DRIVER_VERSION "2005 Sept 27"
68 #define EP_DONTUSE 13 /* nonzero */
70 #define USE_RDK_LEDS /* GPIO pins control three LEDs */
73 static const char driver_name [] = "net2280";
74 static const char driver_desc [] = DRIVER_DESC;
76 static const char ep0name [] = "ep0";
77 static const char *const ep_name [] = {
79 "ep-a", "ep-b", "ep-c", "ep-d",
83 /* use_dma -- general goodness, fewer interrupts, less cpu load (vs PIO)
84 * use_dma_chaining -- dma descriptor queueing gives even more irq reduction
86 * The net2280 DMA engines are not tightly integrated with their FIFOs;
87 * not all cases are (yet) handled well in this driver or the silicon.
88 * Some gadget drivers work better with the dma support here than others.
89 * These two parameters let you use PIO or more aggressive DMA.
91 static bool use_dma = 1;
92 static bool use_dma_chaining = 0;
94 /* "modprobe net2280 use_dma=n" etc */
95 module_param (use_dma, bool, S_IRUGO);
96 module_param (use_dma_chaining, bool, S_IRUGO);
99 /* mode 0 == ep-{a,b,c,d} 1K fifo each
100 * mode 1 == ep-{a,b} 2K fifo each, ep-{c,d} unavailable
101 * mode 2 == ep-a 2K fifo, ep-{b,c} 1K each, ep-d unavailable
103 static ushort fifo_mode = 0;
105 /* "modprobe net2280 fifo_mode=1" etc */
106 module_param (fifo_mode, ushort, 0644);
108 /* enable_suspend -- When enabled, the driver will respond to
109 * USB suspend requests by powering down the NET2280. Otherwise,
110 * USB suspend requests will be ignored. This is acceptable for
111 * self-powered devices
113 static bool enable_suspend = 0;
115 /* "modprobe net2280 enable_suspend=1" etc */
116 module_param (enable_suspend, bool, S_IRUGO);
118 /* force full-speed operation */
119 static bool full_speed;
120 module_param(full_speed, bool, 0444);
121 MODULE_PARM_DESC(full_speed, "force full-speed mode -- for testing only!");
123 #define DIR_STRING(bAddress) (((bAddress) & USB_DIR_IN) ? "in" : "out")
125 #if defined(CONFIG_USB_GADGET_DEBUG_FILES) || defined (DEBUG)
126 static char *type_string (u8 bmAttributes)
128 switch ((bmAttributes) & USB_ENDPOINT_XFERTYPE_MASK) {
129 case USB_ENDPOINT_XFER_BULK: return "bulk";
130 case USB_ENDPOINT_XFER_ISOC: return "iso";
131 case USB_ENDPOINT_XFER_INT: return "intr";
139 #define valid_bit cpu_to_le32 (1 << VALID_BIT)
140 #define dma_done_ie cpu_to_le32 (1 << DMA_DONE_INTERRUPT_ENABLE)
142 /*-------------------------------------------------------------------------*/
145 net2280_enable (struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
148 struct net2280_ep *ep;
152 ep = container_of (_ep, struct net2280_ep, ep);
153 if (!_ep || !desc || ep->desc || _ep->name == ep0name
154 || desc->bDescriptorType != USB_DT_ENDPOINT)
157 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
160 /* erratum 0119 workaround ties up an endpoint number */
161 if ((desc->bEndpointAddress & 0x0f) == EP_DONTUSE)
164 /* sanity check ep-e/ep-f since their fifos are small */
165 max = usb_endpoint_maxp (desc) & 0x1fff;
166 if (ep->num > 4 && max > 64)
169 spin_lock_irqsave (&dev->lock, flags);
170 _ep->maxpacket = max & 0x7ff;
173 /* ep_reset() has already been called */
176 ep->out_overflow = 0;
178 /* set speed-dependent max packet; may kick in high bandwidth */
179 set_idx_reg (dev->regs, REG_EP_MAXPKT (dev, ep->num), max);
181 /* FIFO lines can't go to different packets. PIO is ok, so
182 * use it instead of troublesome (non-bulk) multi-packet DMA.
184 if (ep->dma && (max % 4) != 0 && use_dma_chaining) {
185 DEBUG (ep->dev, "%s, no dma for maxpacket %d\n",
186 ep->ep.name, ep->ep.maxpacket);
190 /* set type, direction, address; reset fifo counters */
191 writel ((1 << FIFO_FLUSH), &ep->regs->ep_stat);
192 tmp = (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK);
193 if (tmp == USB_ENDPOINT_XFER_INT) {
194 /* erratum 0105 workaround prevents hs NYET */
195 if (dev->chiprev == 0100
196 && dev->gadget.speed == USB_SPEED_HIGH
197 && !(desc->bEndpointAddress & USB_DIR_IN))
198 writel ((1 << CLEAR_NAK_OUT_PACKETS_MODE),
200 } else if (tmp == USB_ENDPOINT_XFER_BULK) {
201 /* catch some particularly blatant driver bugs */
202 if ((dev->gadget.speed == USB_SPEED_HIGH
204 || (dev->gadget.speed == USB_SPEED_FULL
206 spin_unlock_irqrestore (&dev->lock, flags);
210 ep->is_iso = (tmp == USB_ENDPOINT_XFER_ISOC) ? 1 : 0;
211 tmp <<= ENDPOINT_TYPE;
212 tmp |= desc->bEndpointAddress;
213 tmp |= (4 << ENDPOINT_BYTE_COUNT); /* default full fifo lines */
214 tmp |= 1 << ENDPOINT_ENABLE;
217 /* for OUT transfers, block the rx fifo until a read is posted */
218 ep->is_in = (tmp & USB_DIR_IN) != 0;
220 writel ((1 << SET_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
221 else if (dev->pdev->device != 0x2280) {
222 /* Added for 2282, Don't use nak packets on an in endpoint,
223 * this was ignored on 2280
225 writel ((1 << CLEAR_NAK_OUT_PACKETS)
226 | (1 << CLEAR_NAK_OUT_PACKETS_MODE), &ep->regs->ep_rsp);
229 writel (tmp, &ep->regs->ep_cfg);
232 if (!ep->dma) { /* pio, per-packet */
233 tmp = (1 << ep->num) | readl (&dev->regs->pciirqenb0);
234 writel (tmp, &dev->regs->pciirqenb0);
236 tmp = (1 << DATA_PACKET_RECEIVED_INTERRUPT_ENABLE)
237 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT_ENABLE);
238 if (dev->pdev->device == 0x2280)
239 tmp |= readl (&ep->regs->ep_irqenb);
240 writel (tmp, &ep->regs->ep_irqenb);
241 } else { /* dma, per-request */
242 tmp = (1 << (8 + ep->num)); /* completion */
243 tmp |= readl (&dev->regs->pciirqenb1);
244 writel (tmp, &dev->regs->pciirqenb1);
246 /* for short OUT transfers, dma completions can't
247 * advance the queue; do it pio-style, by hand.
248 * NOTE erratum 0112 workaround #2
250 if ((desc->bEndpointAddress & USB_DIR_IN) == 0) {
251 tmp = (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT_ENABLE);
252 writel (tmp, &ep->regs->ep_irqenb);
254 tmp = (1 << ep->num) | readl (&dev->regs->pciirqenb0);
255 writel (tmp, &dev->regs->pciirqenb0);
259 tmp = desc->bEndpointAddress;
260 DEBUG (dev, "enabled %s (ep%d%s-%s) %s max %04x\n",
261 _ep->name, tmp & 0x0f, DIR_STRING (tmp),
262 type_string (desc->bmAttributes),
263 ep->dma ? "dma" : "pio", max);
265 /* pci writes may still be posted */
266 spin_unlock_irqrestore (&dev->lock, flags);
270 static int handshake (u32 __iomem *ptr, u32 mask, u32 done, int usec)
275 result = readl (ptr);
276 if (result == ~(u32)0) /* "device unplugged" */
287 static const struct usb_ep_ops net2280_ep_ops;
289 static void ep_reset (struct net2280_regs __iomem *regs, struct net2280_ep *ep)
294 INIT_LIST_HEAD (&ep->queue);
296 ep->ep.maxpacket = ~0;
297 ep->ep.ops = &net2280_ep_ops;
299 /* disable the dma, irqs, endpoint... */
301 writel (0, &ep->dma->dmactl);
302 writel ( (1 << DMA_SCATTER_GATHER_DONE_INTERRUPT)
303 | (1 << DMA_TRANSACTION_DONE_INTERRUPT)
305 , &ep->dma->dmastat);
307 tmp = readl (®s->pciirqenb0);
308 tmp &= ~(1 << ep->num);
309 writel (tmp, ®s->pciirqenb0);
311 tmp = readl (®s->pciirqenb1);
312 tmp &= ~(1 << (8 + ep->num)); /* completion */
313 writel (tmp, ®s->pciirqenb1);
315 writel (0, &ep->regs->ep_irqenb);
317 /* init to our chosen defaults, notably so that we NAK OUT
318 * packets until the driver queues a read (+note erratum 0112)
320 if (!ep->is_in || ep->dev->pdev->device == 0x2280) {
321 tmp = (1 << SET_NAK_OUT_PACKETS_MODE)
322 | (1 << SET_NAK_OUT_PACKETS)
323 | (1 << CLEAR_EP_HIDE_STATUS_PHASE)
324 | (1 << CLEAR_INTERRUPT_MODE);
327 tmp = (1 << CLEAR_NAK_OUT_PACKETS_MODE)
328 | (1 << CLEAR_NAK_OUT_PACKETS)
329 | (1 << CLEAR_EP_HIDE_STATUS_PHASE)
330 | (1 << CLEAR_INTERRUPT_MODE);
334 tmp |= (1 << CLEAR_ENDPOINT_TOGGLE)
335 | (1 << CLEAR_ENDPOINT_HALT);
337 writel (tmp, &ep->regs->ep_rsp);
339 /* scrub most status bits, and flush any fifo state */
340 if (ep->dev->pdev->device == 0x2280)
341 tmp = (1 << FIFO_OVERFLOW)
342 | (1 << FIFO_UNDERFLOW);
346 writel (tmp | (1 << TIMEOUT)
347 | (1 << USB_STALL_SENT)
348 | (1 << USB_IN_NAK_SENT)
349 | (1 << USB_IN_ACK_RCVD)
350 | (1 << USB_OUT_PING_NAK_SENT)
351 | (1 << USB_OUT_ACK_SENT)
353 | (1 << SHORT_PACKET_OUT_DONE_INTERRUPT)
354 | (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)
355 | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
356 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
357 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
358 | (1 << DATA_IN_TOKEN_INTERRUPT)
359 , &ep->regs->ep_stat);
361 /* fifo size is handled separately */
364 static void nuke (struct net2280_ep *);
366 static int net2280_disable (struct usb_ep *_ep)
368 struct net2280_ep *ep;
371 ep = container_of (_ep, struct net2280_ep, ep);
372 if (!_ep || !ep->desc || _ep->name == ep0name)
375 spin_lock_irqsave (&ep->dev->lock, flags);
377 ep_reset (ep->dev->regs, ep);
379 VDEBUG (ep->dev, "disabled %s %s\n",
380 ep->dma ? "dma" : "pio", _ep->name);
382 /* synch memory views with the device */
383 (void) readl (&ep->regs->ep_cfg);
385 if (use_dma && !ep->dma && ep->num >= 1 && ep->num <= 4)
386 ep->dma = &ep->dev->dma [ep->num - 1];
388 spin_unlock_irqrestore (&ep->dev->lock, flags);
392 /*-------------------------------------------------------------------------*/
394 static struct usb_request *
395 net2280_alloc_request (struct usb_ep *_ep, gfp_t gfp_flags)
397 struct net2280_ep *ep;
398 struct net2280_request *req;
402 ep = container_of (_ep, struct net2280_ep, ep);
404 req = kzalloc(sizeof(*req), gfp_flags);
408 INIT_LIST_HEAD (&req->queue);
410 /* this dma descriptor may be swapped with the previous dummy */
412 struct net2280_dma *td;
414 td = pci_pool_alloc (ep->dev->requests, gfp_flags,
420 td->dmacount = 0; /* not VALID */
421 td->dmadesc = td->dmaaddr;
428 net2280_free_request (struct usb_ep *_ep, struct usb_request *_req)
430 struct net2280_ep *ep;
431 struct net2280_request *req;
433 ep = container_of (_ep, struct net2280_ep, ep);
437 req = container_of (_req, struct net2280_request, req);
438 WARN_ON (!list_empty (&req->queue));
440 pci_pool_free (ep->dev->requests, req->td, req->td_dma);
444 /*-------------------------------------------------------------------------*/
446 /* load a packet into the fifo we use for usb IN transfers.
447 * works for all endpoints.
449 * NOTE: pio with ep-a..ep-d could stuff multiple packets into the fifo
450 * at a time, but this code is simpler because it knows it only writes
451 * one packet. ep-a..ep-d should use dma instead.
454 write_fifo (struct net2280_ep *ep, struct usb_request *req)
456 struct net2280_ep_regs __iomem *regs = ep->regs;
459 unsigned count, total;
461 /* INVARIANT: fifo is currently empty. (testable) */
464 buf = req->buf + req->actual;
466 total = req->length - req->actual;
472 /* write just one packet at a time */
473 count = ep->ep.maxpacket;
474 if (count > total) /* min() cannot be used on a bitfield */
477 VDEBUG (ep->dev, "write %s fifo (IN) %d bytes%s req %p\n",
479 (count != ep->ep.maxpacket) ? " (short)" : "",
482 /* NOTE be careful if you try to align these. fifo lines
483 * should normally be full (4 bytes) and successive partial
484 * lines are ok only in certain cases.
486 tmp = get_unaligned ((u32 *)buf);
488 writel (tmp, ®s->ep_data);
493 /* last fifo entry is "short" unless we wrote a full packet.
494 * also explicitly validate last word in (periodic) transfers
495 * when maxpacket is not a multiple of 4 bytes.
497 if (count || total < ep->ep.maxpacket) {
498 tmp = count ? get_unaligned ((u32 *)buf) : count;
500 set_fifo_bytecount (ep, count & 0x03);
501 writel (tmp, ®s->ep_data);
504 /* pci writes may still be posted */
507 /* work around erratum 0106: PCI and USB race over the OUT fifo.
508 * caller guarantees chiprev 0100, out endpoint is NAKing, and
509 * there's no real data in the fifo.
511 * NOTE: also used in cases where that erratum doesn't apply:
512 * where the host wrote "too much" data to us.
514 static void out_flush (struct net2280_ep *ep)
519 ASSERT_OUT_NAKING (ep);
521 statp = &ep->regs->ep_stat;
522 writel ( (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
523 | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
525 writel ((1 << FIFO_FLUSH), statp);
528 if (tmp & (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
529 /* high speed did bulk NYET; fifo isn't filling */
530 && ep->dev->gadget.speed == USB_SPEED_FULL) {
533 usec = 50; /* 64 byte bulk/interrupt */
534 handshake (statp, (1 << USB_OUT_PING_NAK_SENT),
535 (1 << USB_OUT_PING_NAK_SENT), usec);
536 /* NAK done; now CLEAR_NAK_OUT_PACKETS is safe */
540 /* unload packet(s) from the fifo we use for usb OUT transfers.
541 * returns true iff the request completed, because of short packet
542 * or the request buffer having filled with full packets.
544 * for ep-a..ep-d this will read multiple packets out when they
545 * have been accepted.
548 read_fifo (struct net2280_ep *ep, struct net2280_request *req)
550 struct net2280_ep_regs __iomem *regs = ep->regs;
551 u8 *buf = req->req.buf + req->req.actual;
552 unsigned count, tmp, is_short;
553 unsigned cleanup = 0, prevent = 0;
555 /* erratum 0106 ... packets coming in during fifo reads might
556 * be incompletely rejected. not all cases have workarounds.
558 if (ep->dev->chiprev == 0x0100
559 && ep->dev->gadget.speed == USB_SPEED_FULL) {
561 tmp = readl (&ep->regs->ep_stat);
562 if ((tmp & (1 << NAK_OUT_PACKETS)))
564 else if ((tmp & (1 << FIFO_FULL))) {
565 start_out_naking (ep);
568 /* else: hope we don't see the problem */
571 /* never overflow the rx buffer. the fifo reads packets until
572 * it sees a short one; we might not be ready for them all.
575 count = readl (®s->ep_avail);
576 if (unlikely (count == 0)) {
578 tmp = readl (&ep->regs->ep_stat);
579 count = readl (®s->ep_avail);
580 /* handled that data already? */
581 if (count == 0 && (tmp & (1 << NAK_OUT_PACKETS)) == 0)
585 tmp = req->req.length - req->req.actual;
587 /* as with DMA, data overflow gets flushed */
588 if ((tmp % ep->ep.maxpacket) != 0) {
590 "%s out fifo %d bytes, expected %d\n",
591 ep->ep.name, count, tmp);
592 req->req.status = -EOVERFLOW;
594 /* NAK_OUT_PACKETS will be set, so flushing is safe;
595 * the next read will start with the next packet
597 } /* else it's a ZLP, no worries */
600 req->req.actual += count;
602 is_short = (count == 0) || ((count % ep->ep.maxpacket) != 0);
604 VDEBUG (ep->dev, "read %s fifo (OUT) %d bytes%s%s%s req %p %d/%d\n",
605 ep->ep.name, count, is_short ? " (short)" : "",
606 cleanup ? " flush" : "", prevent ? " nak" : "",
607 req, req->req.actual, req->req.length);
610 tmp = readl (®s->ep_data);
612 put_unaligned (tmp, (u32 *)buf);
617 tmp = readl (®s->ep_data);
618 /* LE conversion is implicit here: */
627 writel ((1 << CLEAR_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
628 (void) readl (&ep->regs->ep_rsp);
631 return is_short || ((req->req.actual == req->req.length)
635 /* fill out dma descriptor to match a given request */
637 fill_dma_desc (struct net2280_ep *ep, struct net2280_request *req, int valid)
639 struct net2280_dma *td = req->td;
640 u32 dmacount = req->req.length;
642 /* don't let DMA continue after a short OUT packet,
643 * so overruns can't affect the next transfer.
644 * in case of overruns on max-size packets, we can't
645 * stop the fifo from filling but we can flush it.
648 dmacount |= (1 << DMA_DIRECTION);
649 if ((!ep->is_in && (dmacount % ep->ep.maxpacket) != 0)
650 || ep->dev->pdev->device != 0x2280)
651 dmacount |= (1 << END_OF_CHAIN);
655 dmacount |= (1 << VALID_BIT);
656 if (likely(!req->req.no_interrupt || !use_dma_chaining))
657 dmacount |= (1 << DMA_DONE_INTERRUPT_ENABLE);
659 /* td->dmadesc = previously set by caller */
660 td->dmaaddr = cpu_to_le32 (req->req.dma);
662 /* 2280 may be polling VALID_BIT through ep->dma->dmadesc */
664 td->dmacount = cpu_to_le32(dmacount);
667 static const u32 dmactl_default =
668 (1 << DMA_SCATTER_GATHER_DONE_INTERRUPT)
669 | (1 << DMA_CLEAR_COUNT_ENABLE)
670 /* erratum 0116 workaround part 1 (use POLLING) */
671 | (POLL_100_USEC << DESCRIPTOR_POLLING_RATE)
672 | (1 << DMA_VALID_BIT_POLLING_ENABLE)
673 | (1 << DMA_VALID_BIT_ENABLE)
674 | (1 << DMA_SCATTER_GATHER_ENABLE)
675 /* erratum 0116 workaround part 2 (no AUTOSTART) */
678 static inline void spin_stop_dma (struct net2280_dma_regs __iomem *dma)
680 handshake (&dma->dmactl, (1 << DMA_ENABLE), 0, 50);
683 static inline void stop_dma (struct net2280_dma_regs __iomem *dma)
685 writel (readl (&dma->dmactl) & ~(1 << DMA_ENABLE), &dma->dmactl);
689 static void start_queue (struct net2280_ep *ep, u32 dmactl, u32 td_dma)
691 struct net2280_dma_regs __iomem *dma = ep->dma;
692 unsigned int tmp = (1 << VALID_BIT) | (ep->is_in << DMA_DIRECTION);
694 if (ep->dev->pdev->device != 0x2280)
695 tmp |= (1 << END_OF_CHAIN);
697 writel (tmp, &dma->dmacount);
698 writel (readl (&dma->dmastat), &dma->dmastat);
700 writel (td_dma, &dma->dmadesc);
701 writel (dmactl, &dma->dmactl);
703 /* erratum 0116 workaround part 3: pci arbiter away from net2280 */
704 (void) readl (&ep->dev->pci->pcimstctl);
706 writel ((1 << DMA_START), &dma->dmastat);
709 stop_out_naking (ep);
712 static void start_dma (struct net2280_ep *ep, struct net2280_request *req)
715 struct net2280_dma_regs __iomem *dma = ep->dma;
717 /* FIXME can't use DMA for ZLPs */
719 /* on this path we "know" there's no dma active (yet) */
720 WARN_ON (readl (&dma->dmactl) & (1 << DMA_ENABLE));
721 writel (0, &ep->dma->dmactl);
723 /* previous OUT packet might have been short */
724 if (!ep->is_in && ((tmp = readl (&ep->regs->ep_stat))
725 & (1 << NAK_OUT_PACKETS)) != 0) {
726 writel ((1 << SHORT_PACKET_TRANSFERRED_INTERRUPT),
729 tmp = readl (&ep->regs->ep_avail);
731 writel (readl (&dma->dmastat), &dma->dmastat);
733 /* transfer all/some fifo data */
734 writel (req->req.dma, &dma->dmaaddr);
735 tmp = min (tmp, req->req.length);
737 /* dma irq, faking scatterlist status */
738 req->td->dmacount = cpu_to_le32 (req->req.length - tmp);
739 writel ((1 << DMA_DONE_INTERRUPT_ENABLE)
740 | tmp, &dma->dmacount);
741 req->td->dmadesc = 0;
744 writel ((1 << DMA_ENABLE), &dma->dmactl);
745 writel ((1 << DMA_START), &dma->dmastat);
750 tmp = dmactl_default;
752 /* force packet boundaries between dma requests, but prevent the
753 * controller from automagically writing a last "short" packet
754 * (zero length) unless the driver explicitly said to do that.
757 if (likely ((req->req.length % ep->ep.maxpacket) != 0
759 tmp |= (1 << DMA_FIFO_VALIDATE);
760 ep->in_fifo_validate = 1;
762 ep->in_fifo_validate = 0;
765 /* init req->td, pointing to the current dummy */
766 req->td->dmadesc = cpu_to_le32 (ep->td_dma);
767 fill_dma_desc (ep, req, 1);
769 if (!use_dma_chaining)
770 req->td->dmacount |= cpu_to_le32 (1 << END_OF_CHAIN);
772 start_queue (ep, tmp, req->td_dma);
776 queue_dma (struct net2280_ep *ep, struct net2280_request *req, int valid)
778 struct net2280_dma *end;
781 /* swap new dummy for old, link; fill and maybe activate */
787 ep->td_dma = req->td_dma;
790 end->dmadesc = cpu_to_le32 (ep->td_dma);
792 fill_dma_desc (ep, req, valid);
796 done (struct net2280_ep *ep, struct net2280_request *req, int status)
799 unsigned stopped = ep->stopped;
801 list_del_init (&req->queue);
803 if (req->req.status == -EINPROGRESS)
804 req->req.status = status;
806 status = req->req.status;
810 usb_gadget_unmap_request(&dev->gadget, &req->req, ep->is_in);
812 if (status && status != -ESHUTDOWN)
813 VDEBUG (dev, "complete %s req %p stat %d len %u/%u\n",
814 ep->ep.name, &req->req, status,
815 req->req.actual, req->req.length);
817 /* don't modify queue heads during completion callback */
819 spin_unlock (&dev->lock);
820 req->req.complete (&ep->ep, &req->req);
821 spin_lock (&dev->lock);
822 ep->stopped = stopped;
825 /*-------------------------------------------------------------------------*/
828 net2280_queue (struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
830 struct net2280_request *req;
831 struct net2280_ep *ep;
835 /* we always require a cpu-view buffer, so that we can
836 * always use pio (as fallback or whatever).
838 req = container_of (_req, struct net2280_request, req);
839 if (!_req || !_req->complete || !_req->buf
840 || !list_empty (&req->queue))
842 if (_req->length > (~0 & DMA_BYTE_COUNT_MASK))
844 ep = container_of (_ep, struct net2280_ep, ep);
845 if (!_ep || (!ep->desc && ep->num != 0))
848 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
851 /* FIXME implement PIO fallback for ZLPs with DMA */
852 if (ep->dma && _req->length == 0)
855 /* set up dma mapping in case the caller didn't */
859 ret = usb_gadget_map_request(&dev->gadget, _req,
866 VDEBUG (dev, "%s queue req %p, len %d buf %p\n",
867 _ep->name, _req, _req->length, _req->buf);
870 spin_lock_irqsave (&dev->lock, flags);
872 _req->status = -EINPROGRESS;
875 /* kickstart this i/o queue? */
876 if (list_empty (&ep->queue) && !ep->stopped) {
877 /* use DMA if the endpoint supports it, else pio */
881 /* maybe there's no control data, just status ack */
882 if (ep->num == 0 && _req->length == 0) {
885 VDEBUG (dev, "%s status ack\n", ep->ep.name);
889 /* PIO ... stuff the fifo, or unblock it. */
891 write_fifo (ep, _req);
892 else if (list_empty (&ep->queue)) {
895 /* OUT FIFO might have packet(s) buffered */
896 s = readl (&ep->regs->ep_stat);
897 if ((s & (1 << FIFO_EMPTY)) == 0) {
898 /* note: _req->short_not_ok is
899 * ignored here since PIO _always_
900 * stops queue advance here, and
901 * _req->status doesn't change for
902 * short reads (only _req->actual)
904 if (read_fifo (ep, req)) {
911 s = readl (&ep->regs->ep_stat);
914 /* don't NAK, let the fifo fill */
915 if (req && (s & (1 << NAK_OUT_PACKETS)))
916 writel ((1 << CLEAR_NAK_OUT_PACKETS),
921 } else if (ep->dma) {
927 /* preventing magic zlps is per-engine state, not
928 * per-transfer; irq logic must recover hiccups.
930 expect = likely (req->req.zero
931 || (req->req.length % ep->ep.maxpacket) != 0);
932 if (expect != ep->in_fifo_validate)
935 queue_dma (ep, req, valid);
937 } /* else the irq handler advances the queue. */
941 list_add_tail (&req->queue, &ep->queue);
943 spin_unlock_irqrestore (&dev->lock, flags);
945 /* pci writes may still be posted */
951 struct net2280_ep *ep,
952 struct net2280_request *req,
957 req->req.actual = req->req.length - (DMA_BYTE_COUNT_MASK & dmacount);
958 done (ep, req, status);
961 static void restart_dma (struct net2280_ep *ep);
963 static void scan_dma_completions (struct net2280_ep *ep)
965 /* only look at descriptors that were "naturally" retired,
966 * so fifo and list head state won't matter
968 while (!list_empty (&ep->queue)) {
969 struct net2280_request *req;
972 req = list_entry (ep->queue.next,
973 struct net2280_request, queue);
977 tmp = le32_to_cpup (&req->td->dmacount);
978 if ((tmp & (1 << VALID_BIT)) != 0)
981 /* SHORT_PACKET_TRANSFERRED_INTERRUPT handles "usb-short"
982 * cases where DMA must be aborted; this code handles
983 * all non-abort DMA completions.
985 if (unlikely (req->td->dmadesc == 0)) {
987 tmp = readl (&ep->dma->dmacount);
988 if (tmp & DMA_BYTE_COUNT_MASK)
990 /* single transfer mode */
991 dma_done (ep, req, tmp, 0);
993 } else if (!ep->is_in
994 && (req->req.length % ep->ep.maxpacket) != 0) {
995 tmp = readl (&ep->regs->ep_stat);
997 /* AVOID TROUBLE HERE by not issuing short reads from
998 * your gadget driver. That helps avoids errata 0121,
999 * 0122, and 0124; not all cases trigger the warning.
1001 if ((tmp & (1 << NAK_OUT_PACKETS)) == 0) {
1002 WARNING (ep->dev, "%s lost packet sync!\n",
1004 req->req.status = -EOVERFLOW;
1005 } else if ((tmp = readl (&ep->regs->ep_avail)) != 0) {
1006 /* fifo gets flushed later */
1007 ep->out_overflow = 1;
1008 DEBUG (ep->dev, "%s dma, discard %d len %d\n",
1011 req->req.status = -EOVERFLOW;
1014 dma_done (ep, req, tmp, 0);
1018 static void restart_dma (struct net2280_ep *ep)
1020 struct net2280_request *req;
1021 u32 dmactl = dmactl_default;
1025 req = list_entry (ep->queue.next, struct net2280_request, queue);
1027 if (!use_dma_chaining) {
1028 start_dma (ep, req);
1032 /* the 2280 will be processing the queue unless queue hiccups after
1033 * the previous transfer:
1034 * IN: wanted automagic zlp, head doesn't (or vice versa)
1035 * DMA_FIFO_VALIDATE doesn't init from dma descriptors.
1036 * OUT: was "usb-short", we must restart.
1038 if (ep->is_in && !req->valid) {
1039 struct net2280_request *entry, *prev = NULL;
1040 int reqmode, done = 0;
1042 DEBUG (ep->dev, "%s dma hiccup td %p\n", ep->ep.name, req->td);
1043 ep->in_fifo_validate = likely (req->req.zero
1044 || (req->req.length % ep->ep.maxpacket) != 0);
1045 if (ep->in_fifo_validate)
1046 dmactl |= (1 << DMA_FIFO_VALIDATE);
1047 list_for_each_entry (entry, &ep->queue, queue) {
1052 dmacount = entry->td->dmacount;
1054 reqmode = likely (entry->req.zero
1055 || (entry->req.length
1056 % ep->ep.maxpacket) != 0);
1057 if (reqmode == ep->in_fifo_validate) {
1059 dmacount |= valid_bit;
1060 entry->td->dmacount = dmacount;
1064 /* force a hiccup */
1065 prev->td->dmacount |= dma_done_ie;
1070 /* walk the rest of the queue so unlinks behave */
1072 dmacount &= ~valid_bit;
1073 entry->td->dmacount = dmacount;
1078 writel (0, &ep->dma->dmactl);
1079 start_queue (ep, dmactl, req->td_dma);
1082 static void abort_dma (struct net2280_ep *ep)
1084 /* abort the current transfer */
1085 if (likely (!list_empty (&ep->queue))) {
1086 /* FIXME work around errata 0121, 0122, 0124 */
1087 writel ((1 << DMA_ABORT), &ep->dma->dmastat);
1088 spin_stop_dma (ep->dma);
1091 scan_dma_completions (ep);
1094 /* dequeue ALL requests */
1095 static void nuke (struct net2280_ep *ep)
1097 struct net2280_request *req;
1099 /* called with spinlock held */
1103 while (!list_empty (&ep->queue)) {
1104 req = list_entry (ep->queue.next,
1105 struct net2280_request,
1107 done (ep, req, -ESHUTDOWN);
1111 /* dequeue JUST ONE request */
1112 static int net2280_dequeue (struct usb_ep *_ep, struct usb_request *_req)
1114 struct net2280_ep *ep;
1115 struct net2280_request *req;
1116 unsigned long flags;
1120 ep = container_of (_ep, struct net2280_ep, ep);
1121 if (!_ep || (!ep->desc && ep->num != 0) || !_req)
1124 spin_lock_irqsave (&ep->dev->lock, flags);
1125 stopped = ep->stopped;
1127 /* quiesce dma while we patch the queue */
1131 dmactl = readl (&ep->dma->dmactl);
1132 /* WARNING erratum 0127 may kick in ... */
1134 scan_dma_completions (ep);
1137 /* make sure it's still queued on this endpoint */
1138 list_for_each_entry (req, &ep->queue, queue) {
1139 if (&req->req == _req)
1142 if (&req->req != _req) {
1143 spin_unlock_irqrestore (&ep->dev->lock, flags);
1147 /* queue head may be partially complete. */
1148 if (ep->queue.next == &req->queue) {
1150 DEBUG (ep->dev, "unlink (%s) dma\n", _ep->name);
1151 _req->status = -ECONNRESET;
1153 if (likely (ep->queue.next == &req->queue)) {
1154 // NOTE: misreports single-transfer mode
1155 req->td->dmacount = 0; /* invalidate */
1157 readl (&ep->dma->dmacount),
1161 DEBUG (ep->dev, "unlink (%s) pio\n", _ep->name);
1162 done (ep, req, -ECONNRESET);
1166 /* patch up hardware chaining data */
1167 } else if (ep->dma && use_dma_chaining) {
1168 if (req->queue.prev == ep->queue.next) {
1169 writel (le32_to_cpu (req->td->dmadesc),
1171 if (req->td->dmacount & dma_done_ie)
1172 writel (readl (&ep->dma->dmacount)
1173 | le32_to_cpu(dma_done_ie),
1174 &ep->dma->dmacount);
1176 struct net2280_request *prev;
1178 prev = list_entry (req->queue.prev,
1179 struct net2280_request, queue);
1180 prev->td->dmadesc = req->td->dmadesc;
1181 if (req->td->dmacount & dma_done_ie)
1182 prev->td->dmacount |= dma_done_ie;
1187 done (ep, req, -ECONNRESET);
1188 ep->stopped = stopped;
1191 /* turn off dma on inactive queues */
1192 if (list_empty (&ep->queue))
1194 else if (!ep->stopped) {
1195 /* resume current request, or start new one */
1197 writel (dmactl, &ep->dma->dmactl);
1199 start_dma (ep, list_entry (ep->queue.next,
1200 struct net2280_request, queue));
1204 spin_unlock_irqrestore (&ep->dev->lock, flags);
1208 /*-------------------------------------------------------------------------*/
1210 static int net2280_fifo_status (struct usb_ep *_ep);
1213 net2280_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged)
1215 struct net2280_ep *ep;
1216 unsigned long flags;
1219 ep = container_of (_ep, struct net2280_ep, ep);
1220 if (!_ep || (!ep->desc && ep->num != 0))
1222 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1224 if (ep->desc /* not ep0 */ && (ep->desc->bmAttributes & 0x03)
1225 == USB_ENDPOINT_XFER_ISOC)
1228 spin_lock_irqsave (&ep->dev->lock, flags);
1229 if (!list_empty (&ep->queue))
1231 else if (ep->is_in && value && net2280_fifo_status (_ep) != 0)
1234 VDEBUG (ep->dev, "%s %s %s\n", _ep->name,
1235 value ? "set" : "clear",
1236 wedged ? "wedge" : "halt");
1237 /* set/clear, then synch memory views with the device */
1240 ep->dev->protocol_stall = 1;
1249 (void) readl (&ep->regs->ep_rsp);
1251 spin_unlock_irqrestore (&ep->dev->lock, flags);
1257 net2280_set_halt(struct usb_ep *_ep, int value)
1259 return net2280_set_halt_and_wedge(_ep, value, 0);
1263 net2280_set_wedge(struct usb_ep *_ep)
1265 if (!_ep || _ep->name == ep0name)
1267 return net2280_set_halt_and_wedge(_ep, 1, 1);
1271 net2280_fifo_status (struct usb_ep *_ep)
1273 struct net2280_ep *ep;
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)
1282 avail = readl (&ep->regs->ep_avail) & ((1 << 12) - 1);
1283 if (avail > ep->fifo_size)
1286 avail = ep->fifo_size - avail;
1291 net2280_fifo_flush (struct usb_ep *_ep)
1293 struct net2280_ep *ep;
1295 ep = container_of (_ep, struct net2280_ep, ep);
1296 if (!_ep || (!ep->desc && ep->num != 0))
1298 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1301 writel ((1 << FIFO_FLUSH), &ep->regs->ep_stat);
1302 (void) readl (&ep->regs->ep_rsp);
1305 static const struct usb_ep_ops net2280_ep_ops = {
1306 .enable = net2280_enable,
1307 .disable = net2280_disable,
1309 .alloc_request = net2280_alloc_request,
1310 .free_request = net2280_free_request,
1312 .queue = net2280_queue,
1313 .dequeue = net2280_dequeue,
1315 .set_halt = net2280_set_halt,
1316 .set_wedge = net2280_set_wedge,
1317 .fifo_status = net2280_fifo_status,
1318 .fifo_flush = net2280_fifo_flush,
1321 /*-------------------------------------------------------------------------*/
1323 static int net2280_get_frame (struct usb_gadget *_gadget)
1325 struct net2280 *dev;
1326 unsigned long flags;
1331 dev = container_of (_gadget, struct net2280, gadget);
1332 spin_lock_irqsave (&dev->lock, flags);
1333 retval = get_idx_reg (dev->regs, REG_FRAME) & 0x03ff;
1334 spin_unlock_irqrestore (&dev->lock, flags);
1338 static int net2280_wakeup (struct usb_gadget *_gadget)
1340 struct net2280 *dev;
1342 unsigned long flags;
1346 dev = container_of (_gadget, struct net2280, gadget);
1348 spin_lock_irqsave (&dev->lock, flags);
1349 tmp = readl (&dev->usb->usbctl);
1350 if (tmp & (1 << DEVICE_REMOTE_WAKEUP_ENABLE))
1351 writel (1 << GENERATE_RESUME, &dev->usb->usbstat);
1352 spin_unlock_irqrestore (&dev->lock, flags);
1354 /* pci writes may still be posted */
1358 static int net2280_set_selfpowered (struct usb_gadget *_gadget, int value)
1360 struct net2280 *dev;
1362 unsigned long flags;
1366 dev = container_of (_gadget, struct net2280, gadget);
1368 spin_lock_irqsave (&dev->lock, flags);
1369 tmp = readl (&dev->usb->usbctl);
1371 tmp |= (1 << SELF_POWERED_STATUS);
1373 tmp &= ~(1 << SELF_POWERED_STATUS);
1374 writel (tmp, &dev->usb->usbctl);
1375 spin_unlock_irqrestore (&dev->lock, flags);
1380 static int net2280_pullup(struct usb_gadget *_gadget, int is_on)
1382 struct net2280 *dev;
1384 unsigned long flags;
1388 dev = container_of (_gadget, struct net2280, gadget);
1390 spin_lock_irqsave (&dev->lock, flags);
1391 tmp = readl (&dev->usb->usbctl);
1392 dev->softconnect = (is_on != 0);
1394 tmp |= (1 << USB_DETECT_ENABLE);
1396 tmp &= ~(1 << USB_DETECT_ENABLE);
1397 writel (tmp, &dev->usb->usbctl);
1398 spin_unlock_irqrestore (&dev->lock, flags);
1403 static int net2280_start(struct usb_gadget *_gadget,
1404 struct usb_gadget_driver *driver);
1405 static int net2280_stop(struct usb_gadget *_gadget,
1406 struct usb_gadget_driver *driver);
1408 static const struct usb_gadget_ops net2280_ops = {
1409 .get_frame = net2280_get_frame,
1410 .wakeup = net2280_wakeup,
1411 .set_selfpowered = net2280_set_selfpowered,
1412 .pullup = net2280_pullup,
1413 .udc_start = net2280_start,
1414 .udc_stop = net2280_stop,
1417 /*-------------------------------------------------------------------------*/
1419 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1421 /* FIXME move these into procfs, and use seq_file.
1422 * Sysfs _still_ doesn't behave for arbitrarily sized files,
1423 * and also doesn't help products using this with 2.4 kernels.
1426 /* "function" sysfs attribute */
1427 static ssize_t function_show(struct device *_dev, struct device_attribute *attr,
1430 struct net2280 *dev = dev_get_drvdata (_dev);
1433 || !dev->driver->function
1434 || strlen (dev->driver->function) > PAGE_SIZE)
1436 return scnprintf (buf, PAGE_SIZE, "%s\n", dev->driver->function);
1438 static DEVICE_ATTR_RO(function);
1440 static ssize_t registers_show(struct device *_dev,
1441 struct device_attribute *attr, char *buf)
1443 struct net2280 *dev;
1446 unsigned long flags;
1451 dev = dev_get_drvdata (_dev);
1454 spin_lock_irqsave (&dev->lock, flags);
1457 s = dev->driver->driver.name;
1461 /* Main Control Registers */
1462 t = scnprintf (next, size, "%s version " DRIVER_VERSION
1463 ", chiprev %04x, dma %s\n\n"
1464 "devinit %03x fifoctl %08x gadget '%s'\n"
1465 "pci irqenb0 %02x irqenb1 %08x "
1466 "irqstat0 %04x irqstat1 %08x\n",
1467 driver_name, dev->chiprev,
1469 ? (use_dma_chaining ? "chaining" : "enabled")
1471 readl (&dev->regs->devinit),
1472 readl (&dev->regs->fifoctl),
1474 readl (&dev->regs->pciirqenb0),
1475 readl (&dev->regs->pciirqenb1),
1476 readl (&dev->regs->irqstat0),
1477 readl (&dev->regs->irqstat1));
1481 /* USB Control Registers */
1482 t1 = readl (&dev->usb->usbctl);
1483 t2 = readl (&dev->usb->usbstat);
1484 if (t1 & (1 << VBUS_PIN)) {
1485 if (t2 & (1 << HIGH_SPEED))
1487 else if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1491 /* full speed bit (6) not working?? */
1494 t = scnprintf (next, size,
1495 "stdrsp %08x usbctl %08x usbstat %08x "
1496 "addr 0x%02x (%s)\n",
1497 readl (&dev->usb->stdrsp), t1, t2,
1498 readl (&dev->usb->ouraddr), s);
1502 /* PCI Master Control Registers */
1504 /* DMA Control Registers */
1506 /* Configurable EP Control Registers */
1507 for (i = 0; i < 7; i++) {
1508 struct net2280_ep *ep;
1514 t1 = readl (&ep->regs->ep_cfg);
1515 t2 = readl (&ep->regs->ep_rsp) & 0xff;
1516 t = scnprintf (next, size,
1517 "\n%s\tcfg %05x rsp (%02x) %s%s%s%s%s%s%s%s"
1519 ep->ep.name, t1, t2,
1520 (t2 & (1 << CLEAR_NAK_OUT_PACKETS))
1522 (t2 & (1 << CLEAR_EP_HIDE_STATUS_PHASE))
1524 (t2 & (1 << CLEAR_EP_FORCE_CRC_ERROR))
1526 (t2 & (1 << CLEAR_INTERRUPT_MODE))
1527 ? "interrupt " : "",
1528 (t2 & (1<<CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE))
1530 (t2 & (1 << CLEAR_NAK_OUT_PACKETS_MODE))
1532 (t2 & (1 << CLEAR_ENDPOINT_TOGGLE))
1533 ? "DATA1 " : "DATA0 ",
1534 (t2 & (1 << CLEAR_ENDPOINT_HALT))
1536 readl (&ep->regs->ep_irqenb));
1540 t = scnprintf (next, size,
1541 "\tstat %08x avail %04x "
1543 readl (&ep->regs->ep_stat),
1544 readl (&ep->regs->ep_avail),
1545 t1 & 0x0f, DIR_STRING (t1),
1546 type_string (t1 >> 8),
1547 ep->stopped ? "*" : "");
1554 t = scnprintf (next, size,
1555 " dma\tctl %08x stat %08x count %08x\n"
1556 "\taddr %08x desc %08x\n",
1557 readl (&ep->dma->dmactl),
1558 readl (&ep->dma->dmastat),
1559 readl (&ep->dma->dmacount),
1560 readl (&ep->dma->dmaaddr),
1561 readl (&ep->dma->dmadesc));
1567 /* Indexed Registers */
1571 t = scnprintf (next, size, "\nirqs: ");
1574 for (i = 0; i < 7; i++) {
1575 struct net2280_ep *ep;
1580 t = scnprintf (next, size, " %s/%lu", ep->ep.name, ep->irqs);
1585 t = scnprintf (next, size, "\n");
1589 spin_unlock_irqrestore (&dev->lock, flags);
1591 return PAGE_SIZE - size;
1593 static DEVICE_ATTR_RO(registers);
1595 static ssize_t queues_show(struct device *_dev, struct device_attribute *attr,
1598 struct net2280 *dev;
1601 unsigned long flags;
1604 dev = dev_get_drvdata (_dev);
1607 spin_lock_irqsave (&dev->lock, flags);
1609 for (i = 0; i < 7; i++) {
1610 struct net2280_ep *ep = &dev->ep [i];
1611 struct net2280_request *req;
1615 const struct usb_endpoint_descriptor *d;
1620 t = d->bEndpointAddress;
1621 t = scnprintf (next, size,
1622 "\n%s (ep%d%s-%s) max %04x %s fifo %d\n",
1623 ep->ep.name, t & USB_ENDPOINT_NUMBER_MASK,
1624 (t & USB_DIR_IN) ? "in" : "out",
1626 switch (d->bmAttributes & 0x03) {
1627 case USB_ENDPOINT_XFER_BULK:
1628 val = "bulk"; break;
1629 case USB_ENDPOINT_XFER_INT:
1630 val = "intr"; break;
1634 usb_endpoint_maxp (d) & 0x1fff,
1635 ep->dma ? "dma" : "pio", ep->fifo_size
1637 } else /* ep0 should only have one transfer queued */
1638 t = scnprintf (next, size, "ep0 max 64 pio %s\n",
1639 ep->is_in ? "in" : "out");
1640 if (t <= 0 || t > size)
1645 if (list_empty (&ep->queue)) {
1646 t = scnprintf (next, size, "\t(nothing queued)\n");
1647 if (t <= 0 || t > size)
1653 list_for_each_entry (req, &ep->queue, queue) {
1654 if (ep->dma && req->td_dma == readl (&ep->dma->dmadesc))
1655 t = scnprintf (next, size,
1656 "\treq %p len %d/%d "
1657 "buf %p (dmacount %08x)\n",
1658 &req->req, req->req.actual,
1659 req->req.length, req->req.buf,
1660 readl (&ep->dma->dmacount));
1662 t = scnprintf (next, size,
1663 "\treq %p len %d/%d buf %p\n",
1664 &req->req, req->req.actual,
1665 req->req.length, req->req.buf);
1666 if (t <= 0 || t > size)
1672 struct net2280_dma *td;
1675 t = scnprintf (next, size, "\t td %08x "
1676 " count %08x buf %08x desc %08x\n",
1678 le32_to_cpu (td->dmacount),
1679 le32_to_cpu (td->dmaaddr),
1680 le32_to_cpu (td->dmadesc));
1681 if (t <= 0 || t > size)
1690 spin_unlock_irqrestore (&dev->lock, flags);
1691 return PAGE_SIZE - size;
1693 static DEVICE_ATTR_RO(queues);
1698 #define device_create_file(a,b) (0)
1699 #define device_remove_file(a,b) do { } while (0)
1703 /*-------------------------------------------------------------------------*/
1705 /* another driver-specific mode might be a request type doing dma
1706 * to/from another device fifo instead of to/from memory.
1709 static void set_fifo_mode (struct net2280 *dev, int mode)
1711 /* keeping high bits preserves BAR2 */
1712 writel ((0xffff << PCI_BASE2_RANGE) | mode, &dev->regs->fifoctl);
1714 /* always ep-{a,b,e,f} ... maybe not ep-c or ep-d */
1715 INIT_LIST_HEAD (&dev->gadget.ep_list);
1716 list_add_tail (&dev->ep [1].ep.ep_list, &dev->gadget.ep_list);
1717 list_add_tail (&dev->ep [2].ep.ep_list, &dev->gadget.ep_list);
1720 list_add_tail (&dev->ep [3].ep.ep_list, &dev->gadget.ep_list);
1721 list_add_tail (&dev->ep [4].ep.ep_list, &dev->gadget.ep_list);
1722 dev->ep [1].fifo_size = dev->ep [2].fifo_size = 1024;
1725 dev->ep [1].fifo_size = dev->ep [2].fifo_size = 2048;
1728 list_add_tail (&dev->ep [3].ep.ep_list, &dev->gadget.ep_list);
1729 dev->ep [1].fifo_size = 2048;
1730 dev->ep [2].fifo_size = 1024;
1733 /* fifo sizes for ep0, ep-c, ep-d, ep-e, and ep-f never change */
1734 list_add_tail (&dev->ep [5].ep.ep_list, &dev->gadget.ep_list);
1735 list_add_tail (&dev->ep [6].ep.ep_list, &dev->gadget.ep_list);
1738 /* keeping it simple:
1739 * - one bus driver, initted first;
1740 * - one function driver, initted second
1742 * most of the work to support multiple net2280 controllers would
1743 * be to associate this gadget driver (yes?) with all of them, or
1744 * perhaps to bind specific drivers to specific devices.
1747 static void usb_reset (struct net2280 *dev)
1751 dev->gadget.speed = USB_SPEED_UNKNOWN;
1752 (void) readl (&dev->usb->usbctl);
1754 net2280_led_init (dev);
1756 /* disable automatic responses, and irqs */
1757 writel (0, &dev->usb->stdrsp);
1758 writel (0, &dev->regs->pciirqenb0);
1759 writel (0, &dev->regs->pciirqenb1);
1761 /* clear old dma and irq state */
1762 for (tmp = 0; tmp < 4; tmp++) {
1763 struct net2280_ep *ep = &dev->ep [tmp + 1];
1768 writel (~0, &dev->regs->irqstat0),
1769 writel (~(1 << SUSPEND_REQUEST_INTERRUPT), &dev->regs->irqstat1),
1771 /* reset, and enable pci */
1772 tmp = readl (&dev->regs->devinit)
1774 | (1 << FIFO_SOFT_RESET)
1775 | (1 << USB_SOFT_RESET)
1776 | (1 << M8051_RESET);
1777 writel (tmp, &dev->regs->devinit);
1779 /* standard fifo and endpoint allocations */
1780 set_fifo_mode (dev, (fifo_mode <= 2) ? fifo_mode : 0);
1783 static void usb_reinit (struct net2280 *dev)
1788 /* use_dma changes are ignored till next device re-init */
1791 /* basic endpoint init */
1792 for (tmp = 0; tmp < 7; tmp++) {
1793 struct net2280_ep *ep = &dev->ep [tmp];
1795 ep->ep.name = ep_name [tmp];
1799 if (tmp > 0 && tmp <= 4) {
1800 ep->fifo_size = 1024;
1802 ep->dma = &dev->dma [tmp - 1];
1805 ep->regs = &dev->epregs [tmp];
1806 ep_reset (dev->regs, ep);
1808 dev->ep [0].ep.maxpacket = 64;
1809 dev->ep [5].ep.maxpacket = 64;
1810 dev->ep [6].ep.maxpacket = 64;
1812 dev->gadget.ep0 = &dev->ep [0].ep;
1813 dev->ep [0].stopped = 0;
1814 INIT_LIST_HEAD (&dev->gadget.ep0->ep_list);
1816 /* we want to prevent lowlevel/insecure access from the USB host,
1817 * but erratum 0119 means this enable bit is ignored
1819 for (tmp = 0; tmp < 5; tmp++)
1820 writel (EP_DONTUSE, &dev->dep [tmp].dep_cfg);
1823 static void ep0_start (struct net2280 *dev)
1825 writel ( (1 << CLEAR_EP_HIDE_STATUS_PHASE)
1826 | (1 << CLEAR_NAK_OUT_PACKETS)
1827 | (1 << CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE)
1828 , &dev->epregs [0].ep_rsp);
1831 * hardware optionally handles a bunch of standard requests
1832 * that the API hides from drivers anyway. have it do so.
1833 * endpoint status/features are handled in software, to
1834 * help pass tests for some dubious behavior.
1836 writel ( (1 << SET_TEST_MODE)
1837 | (1 << SET_ADDRESS)
1838 | (1 << DEVICE_SET_CLEAR_DEVICE_REMOTE_WAKEUP)
1839 | (1 << GET_DEVICE_STATUS)
1840 | (1 << GET_INTERFACE_STATUS)
1841 , &dev->usb->stdrsp);
1842 writel ( (1 << USB_ROOT_PORT_WAKEUP_ENABLE)
1843 | (1 << SELF_POWERED_USB_DEVICE)
1844 | (1 << REMOTE_WAKEUP_SUPPORT)
1845 | (dev->softconnect << USB_DETECT_ENABLE)
1846 | (1 << SELF_POWERED_STATUS)
1847 , &dev->usb->usbctl);
1849 /* enable irqs so we can see ep0 and general operation */
1850 writel ( (1 << SETUP_PACKET_INTERRUPT_ENABLE)
1851 | (1 << ENDPOINT_0_INTERRUPT_ENABLE)
1852 , &dev->regs->pciirqenb0);
1853 writel ( (1 << PCI_INTERRUPT_ENABLE)
1854 | (1 << PCI_MASTER_ABORT_RECEIVED_INTERRUPT_ENABLE)
1855 | (1 << PCI_TARGET_ABORT_RECEIVED_INTERRUPT_ENABLE)
1856 | (1 << PCI_RETRY_ABORT_INTERRUPT_ENABLE)
1857 | (1 << VBUS_INTERRUPT_ENABLE)
1858 | (1 << ROOT_PORT_RESET_INTERRUPT_ENABLE)
1859 | (1 << SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE)
1860 , &dev->regs->pciirqenb1);
1862 /* don't leave any writes posted */
1863 (void) readl (&dev->usb->usbctl);
1866 /* when a driver is successfully registered, it will receive
1867 * control requests including set_configuration(), which enables
1868 * non-control requests. then usb traffic follows until a
1869 * disconnect is reported. then a host may connect again, or
1870 * the driver might get unbound.
1872 static int net2280_start(struct usb_gadget *_gadget,
1873 struct usb_gadget_driver *driver)
1875 struct net2280 *dev;
1879 /* insist on high speed support from the driver, since
1880 * (dev->usb->xcvrdiag & FORCE_FULL_SPEED_MODE)
1881 * "must not be used in normal operation"
1883 if (!driver || driver->max_speed < USB_SPEED_HIGH
1887 dev = container_of (_gadget, struct net2280, gadget);
1889 for (i = 0; i < 7; i++)
1890 dev->ep [i].irqs = 0;
1892 /* hook up the driver ... */
1893 dev->softconnect = 1;
1894 driver->driver.bus = NULL;
1895 dev->driver = driver;
1897 retval = device_create_file (&dev->pdev->dev, &dev_attr_function);
1898 if (retval) goto err_unbind;
1899 retval = device_create_file (&dev->pdev->dev, &dev_attr_queues);
1900 if (retval) goto err_func;
1902 /* Enable force-full-speed testing mode, if desired */
1904 writel(1 << FORCE_FULL_SPEED_MODE, &dev->usb->xcvrdiag);
1906 /* ... then enable host detection and ep0; and we're ready
1907 * for set_configuration as well as eventual disconnect.
1909 net2280_led_active (dev, 1);
1912 DEBUG (dev, "%s ready, usbctl %08x stdrsp %08x\n",
1913 driver->driver.name,
1914 readl (&dev->usb->usbctl),
1915 readl (&dev->usb->stdrsp));
1917 /* pci writes may still be posted */
1921 device_remove_file (&dev->pdev->dev, &dev_attr_function);
1928 stop_activity (struct net2280 *dev, struct usb_gadget_driver *driver)
1932 /* don't disconnect if it's not connected */
1933 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1936 /* stop hardware; prevent new request submissions;
1937 * and kill any outstanding requests.
1940 for (i = 0; i < 7; i++)
1941 nuke (&dev->ep [i]);
1943 /* report disconnect; the driver is already quiesced */
1945 spin_unlock(&dev->lock);
1946 driver->disconnect(&dev->gadget);
1947 spin_lock(&dev->lock);
1953 static int net2280_stop(struct usb_gadget *_gadget,
1954 struct usb_gadget_driver *driver)
1956 struct net2280 *dev;
1957 unsigned long flags;
1959 dev = container_of (_gadget, struct net2280, gadget);
1961 spin_lock_irqsave (&dev->lock, flags);
1962 stop_activity (dev, driver);
1963 spin_unlock_irqrestore (&dev->lock, flags);
1967 net2280_led_active (dev, 0);
1969 /* Disable full-speed test mode */
1970 writel(0, &dev->usb->xcvrdiag);
1972 device_remove_file (&dev->pdev->dev, &dev_attr_function);
1973 device_remove_file (&dev->pdev->dev, &dev_attr_queues);
1975 DEBUG (dev, "unregistered driver '%s'\n", driver->driver.name);
1979 /*-------------------------------------------------------------------------*/
1981 /* handle ep0, ep-e, ep-f with 64 byte packets: packet per irq.
1982 * also works for dma-capable endpoints, in pio mode or just
1983 * to manually advance the queue after short OUT transfers.
1985 static void handle_ep_small (struct net2280_ep *ep)
1987 struct net2280_request *req;
1989 /* 0 error, 1 mid-data, 2 done */
1992 if (!list_empty (&ep->queue))
1993 req = list_entry (ep->queue.next,
1994 struct net2280_request, queue);
1998 /* ack all, and handle what we care about */
1999 t = readl (&ep->regs->ep_stat);
2002 VDEBUG (ep->dev, "%s ack ep_stat %08x, req %p\n",
2003 ep->ep.name, t, req ? &req->req : 0);
2005 if (!ep->is_in || ep->dev->pdev->device == 0x2280)
2006 writel (t & ~(1 << NAK_OUT_PACKETS), &ep->regs->ep_stat);
2008 /* Added for 2282 */
2009 writel (t, &ep->regs->ep_stat);
2011 /* for ep0, monitor token irqs to catch data stage length errors
2012 * and to synchronize on status.
2014 * also, to defer reporting of protocol stalls ... here's where
2015 * data or status first appears, handling stalls here should never
2016 * cause trouble on the host side..
2018 * control requests could be slightly faster without token synch for
2019 * status, but status can jam up that way.
2021 if (unlikely (ep->num == 0)) {
2023 /* status; stop NAKing */
2024 if (t & (1 << DATA_OUT_PING_TOKEN_INTERRUPT)) {
2025 if (ep->dev->protocol_stall) {
2032 /* reply to extra IN data tokens with a zlp */
2033 } else if (t & (1 << DATA_IN_TOKEN_INTERRUPT)) {
2034 if (ep->dev->protocol_stall) {
2038 } else if (ep->responded &&
2039 !req && !ep->stopped)
2040 write_fifo (ep, NULL);
2043 /* status; stop NAKing */
2044 if (t & (1 << DATA_IN_TOKEN_INTERRUPT)) {
2045 if (ep->dev->protocol_stall) {
2050 /* an extra OUT token is an error */
2051 } else if (((t & (1 << DATA_OUT_PING_TOKEN_INTERRUPT))
2053 && req->req.actual == req->req.length)
2054 || (ep->responded && !req)) {
2055 ep->dev->protocol_stall = 1;
2059 done (ep, req, -EOVERFLOW);
2065 if (unlikely (!req))
2068 /* manual DMA queue advance after short OUT */
2069 if (likely (ep->dma)) {
2070 if (t & (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)) {
2072 int stopped = ep->stopped;
2074 /* TRANSFERRED works around OUT_DONE erratum 0112.
2075 * we expect (N <= maxpacket) bytes; host wrote M.
2076 * iff (M < N) we won't ever see a DMA interrupt.
2079 for (count = 0; ; t = readl (&ep->regs->ep_stat)) {
2081 /* any preceding dma transfers must finish.
2082 * dma handles (M >= N), may empty the queue
2084 scan_dma_completions (ep);
2085 if (unlikely (list_empty (&ep->queue)
2086 || ep->out_overflow)) {
2090 req = list_entry (ep->queue.next,
2091 struct net2280_request, queue);
2093 /* here either (M < N), a "real" short rx;
2094 * or (M == N) and the queue didn't empty
2096 if (likely (t & (1 << FIFO_EMPTY))) {
2097 count = readl (&ep->dma->dmacount);
2098 count &= DMA_BYTE_COUNT_MASK;
2099 if (readl (&ep->dma->dmadesc)
2107 /* stop DMA, leave ep NAKing */
2108 writel ((1 << DMA_ABORT), &ep->dma->dmastat);
2109 spin_stop_dma (ep->dma);
2112 req->td->dmacount = 0;
2113 t = readl (&ep->regs->ep_avail);
2114 dma_done (ep, req, count,
2115 (ep->out_overflow || t)
2119 /* also flush to prevent erratum 0106 trouble */
2120 if (unlikely (ep->out_overflow
2121 || (ep->dev->chiprev == 0x0100
2122 && ep->dev->gadget.speed
2123 == USB_SPEED_FULL))) {
2125 ep->out_overflow = 0;
2128 /* (re)start dma if needed, stop NAKing */
2129 ep->stopped = stopped;
2130 if (!list_empty (&ep->queue))
2133 DEBUG (ep->dev, "%s dma ep_stat %08x ??\n",
2137 /* data packet(s) received (in the fifo, OUT) */
2138 } else if (t & (1 << DATA_PACKET_RECEIVED_INTERRUPT)) {
2139 if (read_fifo (ep, req) && ep->num != 0)
2142 /* data packet(s) transmitted (IN) */
2143 } else if (t & (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)) {
2146 len = req->req.length - req->req.actual;
2147 if (len > ep->ep.maxpacket)
2148 len = ep->ep.maxpacket;
2149 req->req.actual += len;
2151 /* if we wrote it all, we're usually done */
2152 if (req->req.actual == req->req.length) {
2154 /* send zlps until the status stage */
2155 } else if (!req->req.zero || len != ep->ep.maxpacket)
2159 /* there was nothing to do ... */
2160 } else if (mode == 1)
2165 /* stream endpoints often resubmit/unlink in completion */
2168 /* maybe advance queue to next request */
2170 /* NOTE: net2280 could let gadget driver start the
2171 * status stage later. since not all controllers let
2172 * them control that, the api doesn't (yet) allow it.
2178 if (!list_empty (&ep->queue) && !ep->stopped)
2179 req = list_entry (ep->queue.next,
2180 struct net2280_request, queue);
2183 if (req && !ep->is_in)
2184 stop_out_naking (ep);
2188 /* is there a buffer for the next packet?
2189 * for best streaming performance, make sure there is one.
2191 if (req && !ep->stopped) {
2193 /* load IN fifo with next packet (may be zlp) */
2194 if (t & (1 << DATA_PACKET_TRANSMITTED_INTERRUPT))
2195 write_fifo (ep, &req->req);
2199 static struct net2280_ep *
2200 get_ep_by_addr (struct net2280 *dev, u16 wIndex)
2202 struct net2280_ep *ep;
2204 if ((wIndex & USB_ENDPOINT_NUMBER_MASK) == 0)
2205 return &dev->ep [0];
2206 list_for_each_entry (ep, &dev->gadget.ep_list, ep.ep_list) {
2207 u8 bEndpointAddress;
2211 bEndpointAddress = ep->desc->bEndpointAddress;
2212 if ((wIndex ^ bEndpointAddress) & USB_DIR_IN)
2214 if ((wIndex & 0x0f) == (bEndpointAddress & 0x0f))
2220 static void handle_stat0_irqs (struct net2280 *dev, u32 stat)
2222 struct net2280_ep *ep;
2225 /* most of these don't need individual acks */
2226 stat &= ~(1 << INTA_ASSERTED);
2229 // DEBUG (dev, "irqstat0 %04x\n", stat);
2231 /* starting a control request? */
2232 if (unlikely (stat & (1 << SETUP_PACKET_INTERRUPT))) {
2235 struct usb_ctrlrequest r;
2238 struct net2280_request *req;
2240 if (dev->gadget.speed == USB_SPEED_UNKNOWN) {
2241 if (readl (&dev->usb->usbstat) & (1 << HIGH_SPEED))
2242 dev->gadget.speed = USB_SPEED_HIGH;
2244 dev->gadget.speed = USB_SPEED_FULL;
2245 net2280_led_speed (dev, dev->gadget.speed);
2246 DEBUG(dev, "%s\n", usb_speed_string(dev->gadget.speed));
2252 /* make sure any leftover request state is cleared */
2253 stat &= ~(1 << ENDPOINT_0_INTERRUPT);
2254 while (!list_empty (&ep->queue)) {
2255 req = list_entry (ep->queue.next,
2256 struct net2280_request, queue);
2257 done (ep, req, (req->req.actual == req->req.length)
2261 dev->protocol_stall = 0;
2263 if (ep->dev->pdev->device == 0x2280)
2264 tmp = (1 << FIFO_OVERFLOW)
2265 | (1 << FIFO_UNDERFLOW);
2269 writel (tmp | (1 << TIMEOUT)
2270 | (1 << USB_STALL_SENT)
2271 | (1 << USB_IN_NAK_SENT)
2272 | (1 << USB_IN_ACK_RCVD)
2273 | (1 << USB_OUT_PING_NAK_SENT)
2274 | (1 << USB_OUT_ACK_SENT)
2275 | (1 << SHORT_PACKET_OUT_DONE_INTERRUPT)
2276 | (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)
2277 | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
2278 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
2279 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
2280 | (1 << DATA_IN_TOKEN_INTERRUPT)
2281 , &ep->regs->ep_stat);
2282 u.raw [0] = readl (&dev->usb->setup0123);
2283 u.raw [1] = readl (&dev->usb->setup4567);
2285 cpu_to_le32s (&u.raw [0]);
2286 cpu_to_le32s (&u.raw [1]);
2290 #define w_value le16_to_cpu(u.r.wValue)
2291 #define w_index le16_to_cpu(u.r.wIndex)
2292 #define w_length le16_to_cpu(u.r.wLength)
2295 writel (1 << SETUP_PACKET_INTERRUPT, &dev->regs->irqstat0);
2296 stat ^= (1 << SETUP_PACKET_INTERRUPT);
2298 /* watch control traffic at the token level, and force
2299 * synchronization before letting the status stage happen.
2300 * FIXME ignore tokens we'll NAK, until driver responds.
2301 * that'll mean a lot less irqs for some drivers.
2303 ep->is_in = (u.r.bRequestType & USB_DIR_IN) != 0;
2305 scratch = (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
2306 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
2307 | (1 << DATA_IN_TOKEN_INTERRUPT);
2308 stop_out_naking (ep);
2310 scratch = (1 << DATA_PACKET_RECEIVED_INTERRUPT)
2311 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
2312 | (1 << DATA_IN_TOKEN_INTERRUPT);
2313 writel (scratch, &dev->epregs [0].ep_irqenb);
2315 /* we made the hardware handle most lowlevel requests;
2316 * everything else goes uplevel to the gadget code.
2319 switch (u.r.bRequest) {
2320 case USB_REQ_GET_STATUS: {
2321 struct net2280_ep *e;
2324 /* hw handles device and interface status */
2325 if (u.r.bRequestType != (USB_DIR_IN|USB_RECIP_ENDPOINT))
2327 if ((e = get_ep_by_addr (dev, w_index)) == NULL
2331 if (readl (&e->regs->ep_rsp)
2332 & (1 << SET_ENDPOINT_HALT))
2333 status = cpu_to_le32 (1);
2335 status = cpu_to_le32 (0);
2337 /* don't bother with a request object! */
2338 writel (0, &dev->epregs [0].ep_irqenb);
2339 set_fifo_bytecount (ep, w_length);
2340 writel ((__force u32)status, &dev->epregs [0].ep_data);
2342 VDEBUG (dev, "%s stat %02x\n", ep->ep.name, status);
2343 goto next_endpoints;
2346 case USB_REQ_CLEAR_FEATURE: {
2347 struct net2280_ep *e;
2349 /* hw handles device features */
2350 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
2352 if (w_value != USB_ENDPOINT_HALT
2355 if ((e = get_ep_by_addr (dev, w_index)) == NULL)
2358 VDEBUG(dev, "%s wedged, halt not cleared\n",
2361 VDEBUG(dev, "%s clear halt\n", ep->ep.name);
2365 goto next_endpoints;
2368 case USB_REQ_SET_FEATURE: {
2369 struct net2280_ep *e;
2371 /* hw handles device features */
2372 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
2374 if (w_value != USB_ENDPOINT_HALT
2377 if ((e = get_ep_by_addr (dev, w_index)) == NULL)
2379 if (e->ep.name == ep0name)
2383 VDEBUG (dev, "%s set halt\n", ep->ep.name);
2384 goto next_endpoints;
2389 VDEBUG (dev, "setup %02x.%02x v%04x i%04x l%04x "
2391 u.r.bRequestType, u.r.bRequest,
2392 w_value, w_index, w_length,
2393 readl (&ep->regs->ep_cfg));
2395 spin_unlock (&dev->lock);
2396 tmp = dev->driver->setup (&dev->gadget, &u.r);
2397 spin_lock (&dev->lock);
2400 /* stall ep0 on error */
2403 VDEBUG (dev, "req %02x.%02x protocol STALL; stat %d\n",
2404 u.r.bRequestType, u.r.bRequest, tmp);
2405 dev->protocol_stall = 1;
2408 /* some in/out token irq should follow; maybe stall then.
2409 * driver must queue a request (even zlp) or halt ep0
2410 * before the host times out.
2419 /* endpoint data irq ? */
2420 scratch = stat & 0x7f;
2422 for (num = 0; scratch; num++) {
2425 /* do this endpoint's FIFO and queue need tending? */
2427 if ((scratch & t) == 0)
2431 ep = &dev->ep [num];
2432 handle_ep_small (ep);
2436 DEBUG (dev, "unhandled irqstat0 %08x\n", stat);
2439 #define DMA_INTERRUPTS ( \
2440 (1 << DMA_D_INTERRUPT) \
2441 | (1 << DMA_C_INTERRUPT) \
2442 | (1 << DMA_B_INTERRUPT) \
2443 | (1 << DMA_A_INTERRUPT))
2444 #define PCI_ERROR_INTERRUPTS ( \
2445 (1 << PCI_MASTER_ABORT_RECEIVED_INTERRUPT) \
2446 | (1 << PCI_TARGET_ABORT_RECEIVED_INTERRUPT) \
2447 | (1 << PCI_RETRY_ABORT_INTERRUPT))
2449 static void handle_stat1_irqs (struct net2280 *dev, u32 stat)
2451 struct net2280_ep *ep;
2452 u32 tmp, num, mask, scratch;
2454 /* after disconnect there's nothing else to do! */
2455 tmp = (1 << VBUS_INTERRUPT) | (1 << ROOT_PORT_RESET_INTERRUPT);
2456 mask = (1 << HIGH_SPEED) | (1 << FULL_SPEED);
2458 /* VBUS disconnect is indicated by VBUS_PIN and VBUS_INTERRUPT set.
2459 * Root Port Reset is indicated by ROOT_PORT_RESET_INTERRUPT set and
2460 * both HIGH_SPEED and FULL_SPEED clear (as ROOT_PORT_RESET_INTERRUPT
2461 * only indicates a change in the reset state).
2464 writel (tmp, &dev->regs->irqstat1);
2465 if ((((stat & (1 << ROOT_PORT_RESET_INTERRUPT))
2466 && ((readl (&dev->usb->usbstat) & mask)
2468 || ((readl (&dev->usb->usbctl)
2469 & (1 << VBUS_PIN)) == 0)
2470 ) && ( dev->gadget.speed != USB_SPEED_UNKNOWN)) {
2471 DEBUG (dev, "disconnect %s\n",
2472 dev->driver->driver.name);
2473 stop_activity (dev, dev->driver);
2479 /* vBUS can bounce ... one of many reasons to ignore the
2480 * notion of hotplug events on bus connect/disconnect!
2486 /* NOTE: chip stays in PCI D0 state for now, but it could
2487 * enter D1 to save more power
2489 tmp = (1 << SUSPEND_REQUEST_CHANGE_INTERRUPT);
2491 writel (tmp, &dev->regs->irqstat1);
2492 if (stat & (1 << SUSPEND_REQUEST_INTERRUPT)) {
2493 if (dev->driver->suspend)
2494 dev->driver->suspend (&dev->gadget);
2495 if (!enable_suspend)
2496 stat &= ~(1 << SUSPEND_REQUEST_INTERRUPT);
2498 if (dev->driver->resume)
2499 dev->driver->resume (&dev->gadget);
2500 /* at high speed, note erratum 0133 */
2505 /* clear any other status/irqs */
2507 writel (stat, &dev->regs->irqstat1);
2509 /* some status we can just ignore */
2510 if (dev->pdev->device == 0x2280)
2511 stat &= ~((1 << CONTROL_STATUS_INTERRUPT)
2512 | (1 << SUSPEND_REQUEST_INTERRUPT)
2513 | (1 << RESUME_INTERRUPT)
2514 | (1 << SOF_INTERRUPT));
2516 stat &= ~((1 << CONTROL_STATUS_INTERRUPT)
2517 | (1 << RESUME_INTERRUPT)
2518 | (1 << SOF_DOWN_INTERRUPT)
2519 | (1 << SOF_INTERRUPT));
2523 // DEBUG (dev, "irqstat1 %08x\n", stat);
2525 /* DMA status, for ep-{a,b,c,d} */
2526 scratch = stat & DMA_INTERRUPTS;
2527 stat &= ~DMA_INTERRUPTS;
2529 for (num = 0; scratch; num++) {
2530 struct net2280_dma_regs __iomem *dma;
2533 if ((tmp & scratch) == 0)
2537 ep = &dev->ep [num + 1];
2543 /* clear ep's dma status */
2544 tmp = readl (&dma->dmastat);
2545 writel (tmp, &dma->dmastat);
2547 /* chaining should stop on abort, short OUT from fifo,
2548 * or (stat0 codepath) short OUT transfer.
2550 if (!use_dma_chaining) {
2551 if ((tmp & (1 << DMA_TRANSACTION_DONE_INTERRUPT))
2553 DEBUG (ep->dev, "%s no xact done? %08x\n",
2560 /* OUT transfers terminate when the data from the
2561 * host is in our memory. Process whatever's done.
2562 * On this path, we know transfer's last packet wasn't
2563 * less than req->length. NAK_OUT_PACKETS may be set,
2564 * or the FIFO may already be holding new packets.
2566 * IN transfers can linger in the FIFO for a very
2567 * long time ... we ignore that for now, accounting
2568 * precisely (like PIO does) needs per-packet irqs
2570 scan_dma_completions (ep);
2572 /* disable dma on inactive queues; else maybe restart */
2573 if (list_empty (&ep->queue)) {
2574 if (use_dma_chaining)
2577 tmp = readl (&dma->dmactl);
2578 if (!use_dma_chaining
2579 || (tmp & (1 << DMA_ENABLE)) == 0)
2581 else if (ep->is_in && use_dma_chaining) {
2582 struct net2280_request *req;
2585 /* the descriptor at the head of the chain
2586 * may still have VALID_BIT clear; that's
2587 * used to trigger changing DMA_FIFO_VALIDATE
2588 * (affects automagic zlp writes).
2590 req = list_entry (ep->queue.next,
2591 struct net2280_request, queue);
2592 dmacount = req->td->dmacount;
2593 dmacount &= cpu_to_le32 (
2595 | DMA_BYTE_COUNT_MASK);
2596 if (dmacount && (dmacount & valid_bit) == 0)
2603 /* NOTE: there are other PCI errors we might usefully notice.
2604 * if they appear very often, here's where to try recovering.
2606 if (stat & PCI_ERROR_INTERRUPTS) {
2607 ERROR (dev, "pci dma error; stat %08x\n", stat);
2608 stat &= ~PCI_ERROR_INTERRUPTS;
2609 /* these are fatal errors, but "maybe" they won't
2612 stop_activity (dev, dev->driver);
2618 DEBUG (dev, "unhandled irqstat1 %08x\n", stat);
2621 static irqreturn_t net2280_irq (int irq, void *_dev)
2623 struct net2280 *dev = _dev;
2625 /* shared interrupt, not ours */
2626 if (!(readl(&dev->regs->irqstat0) & (1 << INTA_ASSERTED)))
2629 spin_lock (&dev->lock);
2631 /* handle disconnect, dma, and more */
2632 handle_stat1_irqs (dev, readl (&dev->regs->irqstat1));
2634 /* control requests and PIO */
2635 handle_stat0_irqs (dev, readl (&dev->regs->irqstat0));
2637 spin_unlock (&dev->lock);
2642 /*-------------------------------------------------------------------------*/
2644 static void gadget_release (struct device *_dev)
2646 struct net2280 *dev = dev_get_drvdata (_dev);
2651 /* tear down the binding between this driver and the pci device */
2653 static void net2280_remove (struct pci_dev *pdev)
2655 struct net2280 *dev = pci_get_drvdata (pdev);
2657 usb_del_gadget_udc(&dev->gadget);
2659 BUG_ON(dev->driver);
2661 /* then clean up the resources we allocated during probe() */
2662 net2280_led_shutdown (dev);
2663 if (dev->requests) {
2665 for (i = 1; i < 5; i++) {
2666 if (!dev->ep [i].dummy)
2668 pci_pool_free (dev->requests, dev->ep [i].dummy,
2669 dev->ep [i].td_dma);
2671 pci_pool_destroy (dev->requests);
2674 free_irq (pdev->irq, dev);
2676 iounmap (dev->regs);
2678 release_mem_region (pci_resource_start (pdev, 0),
2679 pci_resource_len (pdev, 0));
2681 pci_disable_device (pdev);
2682 device_remove_file (&pdev->dev, &dev_attr_registers);
2684 INFO (dev, "unbind\n");
2687 /* wrap this driver around the specified device, but
2688 * don't respond over USB until a gadget driver binds to us.
2691 static int net2280_probe (struct pci_dev *pdev, const struct pci_device_id *id)
2693 struct net2280 *dev;
2694 unsigned long resource, len;
2695 void __iomem *base = NULL;
2698 /* alloc, and start init */
2699 dev = kzalloc (sizeof *dev, GFP_KERNEL);
2705 pci_set_drvdata (pdev, dev);
2706 spin_lock_init (&dev->lock);
2708 dev->gadget.ops = &net2280_ops;
2709 dev->gadget.max_speed = USB_SPEED_HIGH;
2711 /* the "gadget" abstracts/virtualizes the controller */
2712 dev->gadget.name = driver_name;
2714 /* now all the pci goodies ... */
2715 if (pci_enable_device (pdev) < 0) {
2721 /* BAR 0 holds all the registers
2722 * BAR 1 is 8051 memory; unused here (note erratum 0103)
2723 * BAR 2 is fifo memory; unused here
2725 resource = pci_resource_start (pdev, 0);
2726 len = pci_resource_len (pdev, 0);
2727 if (!request_mem_region (resource, len, driver_name)) {
2728 DEBUG (dev, "controller already in use\n");
2734 /* FIXME provide firmware download interface to put
2735 * 8051 code into the chip, e.g. to turn on PCI PM.
2738 base = ioremap_nocache (resource, len);
2740 DEBUG (dev, "can't map memory\n");
2744 dev->regs = (struct net2280_regs __iomem *) base;
2745 dev->usb = (struct net2280_usb_regs __iomem *) (base + 0x0080);
2746 dev->pci = (struct net2280_pci_regs __iomem *) (base + 0x0100);
2747 dev->dma = (struct net2280_dma_regs __iomem *) (base + 0x0180);
2748 dev->dep = (struct net2280_dep_regs __iomem *) (base + 0x0200);
2749 dev->epregs = (struct net2280_ep_regs __iomem *) (base + 0x0300);
2751 /* put into initial config, link up all endpoints */
2752 writel (0, &dev->usb->usbctl);
2756 /* irq setup after old hardware is cleaned up */
2758 ERROR (dev, "No IRQ. Check PCI setup!\n");
2763 if (request_irq (pdev->irq, net2280_irq, IRQF_SHARED, driver_name, dev)
2765 ERROR (dev, "request interrupt %d failed\n", pdev->irq);
2772 /* NOTE: we know only the 32 LSBs of dma addresses may be nonzero */
2773 dev->requests = pci_pool_create ("requests", pdev,
2774 sizeof (struct net2280_dma),
2775 0 /* no alignment requirements */,
2776 0 /* or page-crossing issues */);
2777 if (!dev->requests) {
2778 DEBUG (dev, "can't get request pool\n");
2782 for (i = 1; i < 5; i++) {
2783 struct net2280_dma *td;
2785 td = pci_pool_alloc (dev->requests, GFP_KERNEL,
2786 &dev->ep [i].td_dma);
2788 DEBUG (dev, "can't get dummy %d\n", i);
2792 td->dmacount = 0; /* not VALID */
2793 td->dmadesc = td->dmaaddr;
2794 dev->ep [i].dummy = td;
2797 /* enable lower-overhead pci memory bursts during DMA */
2798 writel ( (1 << DMA_MEMORY_WRITE_AND_INVALIDATE_ENABLE)
2799 // 256 write retries may not be enough...
2800 // | (1 << PCI_RETRY_ABORT_ENABLE)
2801 | (1 << DMA_READ_MULTIPLE_ENABLE)
2802 | (1 << DMA_READ_LINE_ENABLE)
2803 , &dev->pci->pcimstctl);
2804 /* erratum 0115 shouldn't appear: Linux inits PCI_LATENCY_TIMER */
2805 pci_set_master (pdev);
2806 pci_try_set_mwi (pdev);
2808 /* ... also flushes any posted pci writes */
2809 dev->chiprev = get_idx_reg (dev->regs, REG_CHIPREV) & 0xffff;
2812 INFO (dev, "%s\n", driver_desc);
2813 INFO (dev, "irq %d, pci mem %p, chip rev %04x\n",
2814 pdev->irq, base, dev->chiprev);
2815 INFO (dev, "version: " DRIVER_VERSION "; dma %s\n",
2817 ? (use_dma_chaining ? "chaining" : "enabled")
2819 retval = device_create_file (&pdev->dev, &dev_attr_registers);
2820 if (retval) goto done;
2822 retval = usb_add_gadget_udc_release(&pdev->dev, &dev->gadget,
2830 net2280_remove (pdev);
2834 /* make sure the board is quiescent; otherwise it will continue
2835 * generating IRQs across the upcoming reboot.
2838 static void net2280_shutdown (struct pci_dev *pdev)
2840 struct net2280 *dev = pci_get_drvdata (pdev);
2843 writel (0, &dev->regs->pciirqenb0);
2844 writel (0, &dev->regs->pciirqenb1);
2846 /* disable the pullup so the host will think we're gone */
2847 writel (0, &dev->usb->usbctl);
2849 /* Disable full-speed test mode */
2850 writel(0, &dev->usb->xcvrdiag);
2854 /*-------------------------------------------------------------------------*/
2856 static const struct pci_device_id pci_ids [] = { {
2857 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
2861 .subvendor = PCI_ANY_ID,
2862 .subdevice = PCI_ANY_ID,
2864 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
2868 .subvendor = PCI_ANY_ID,
2869 .subdevice = PCI_ANY_ID,
2871 }, { /* end: all zeroes */ }
2873 MODULE_DEVICE_TABLE (pci, pci_ids);
2875 /* pci driver glue; this is a "new style" PCI driver module */
2876 static struct pci_driver net2280_pci_driver = {
2877 .name = (char *) driver_name,
2878 .id_table = pci_ids,
2880 .probe = net2280_probe,
2881 .remove = net2280_remove,
2882 .shutdown = net2280_shutdown,
2884 /* FIXME add power management support */
2887 MODULE_DESCRIPTION (DRIVER_DESC);
2888 MODULE_AUTHOR ("David Brownell");
2889 MODULE_LICENSE ("GPL");
2891 static int __init init (void)
2894 use_dma_chaining = 0;
2895 return pci_register_driver (&net2280_pci_driver);
2899 static void __exit cleanup (void)
2901 pci_unregister_driver (&net2280_pci_driver);
2903 module_exit (cleanup);