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
32 * This program is free software; you can redistribute it and/or modify
33 * it under the terms of the GNU General Public License as published by
34 * the Free Software Foundation; either version 2 of the License, or
35 * (at your option) any later version.
37 * This program is distributed in the hope that it will be useful,
38 * but WITHOUT ANY WARRANTY; without even the implied warranty of
39 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
40 * GNU General Public License for more details.
42 * You should have received a copy of the GNU General Public License
43 * along with this program; if not, write to the Free Software
44 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
47 #undef DEBUG /* messages on error and most fault paths */
48 #undef VERBOSE /* extra debug messages (success too) */
50 #include <linux/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/slab.h>
57 #include <linux/errno.h>
58 #include <linux/init.h>
59 #include <linux/timer.h>
60 #include <linux/list.h>
61 #include <linux/interrupt.h>
62 #include <linux/moduleparam.h>
63 #include <linux/device.h>
64 #include <linux/usb/ch9.h>
65 #include <linux/usb_gadget.h>
67 #include <asm/byteorder.h>
70 #include <asm/system.h>
71 #include <asm/unaligned.h>
74 #define DRIVER_DESC "PLX NET228x USB Peripheral Controller"
75 #define DRIVER_VERSION "2005 Sept 27"
77 #define DMA_ADDR_INVALID (~(dma_addr_t)0)
78 #define EP_DONTUSE 13 /* nonzero */
80 #define USE_RDK_LEDS /* GPIO pins control three LEDs */
83 static const char driver_name [] = "net2280";
84 static const char driver_desc [] = DRIVER_DESC;
86 static const char ep0name [] = "ep0";
87 static const char *const ep_name [] = {
89 "ep-a", "ep-b", "ep-c", "ep-d",
93 /* use_dma -- general goodness, fewer interrupts, less cpu load (vs PIO)
94 * use_dma_chaining -- dma descriptor queueing gives even more irq reduction
96 * The net2280 DMA engines are not tightly integrated with their FIFOs;
97 * not all cases are (yet) handled well in this driver or the silicon.
98 * Some gadget drivers work better with the dma support here than others.
99 * These two parameters let you use PIO or more aggressive DMA.
101 static int use_dma = 1;
102 static int use_dma_chaining = 0;
104 /* "modprobe net2280 use_dma=n" etc */
105 module_param (use_dma, bool, S_IRUGO);
106 module_param (use_dma_chaining, bool, S_IRUGO);
109 /* mode 0 == ep-{a,b,c,d} 1K fifo each
110 * mode 1 == ep-{a,b} 2K fifo each, ep-{c,d} unavailable
111 * mode 2 == ep-a 2K fifo, ep-{b,c} 1K each, ep-d unavailable
113 static ushort fifo_mode = 0;
115 /* "modprobe net2280 fifo_mode=1" etc */
116 module_param (fifo_mode, ushort, 0644);
118 /* enable_suspend -- When enabled, the driver will respond to
119 * USB suspend requests by powering down the NET2280. Otherwise,
120 * USB suspend requests will be ignored. This is acceptible for
121 * self-powered devices
123 static int enable_suspend = 0;
125 /* "modprobe net2280 enable_suspend=1" etc */
126 module_param (enable_suspend, bool, S_IRUGO);
129 #define DIR_STRING(bAddress) (((bAddress) & USB_DIR_IN) ? "in" : "out")
131 #if defined(CONFIG_USB_GADGET_DEBUG_FILES) || defined (DEBUG)
132 static char *type_string (u8 bmAttributes)
134 switch ((bmAttributes) & USB_ENDPOINT_XFERTYPE_MASK) {
135 case USB_ENDPOINT_XFER_BULK: return "bulk";
136 case USB_ENDPOINT_XFER_ISOC: return "iso";
137 case USB_ENDPOINT_XFER_INT: return "intr";
145 #define valid_bit __constant_cpu_to_le32 (1 << VALID_BIT)
146 #define dma_done_ie __constant_cpu_to_le32 (1 << DMA_DONE_INTERRUPT_ENABLE)
148 /*-------------------------------------------------------------------------*/
151 net2280_enable (struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
154 struct net2280_ep *ep;
158 ep = container_of (_ep, struct net2280_ep, ep);
159 if (!_ep || !desc || ep->desc || _ep->name == ep0name
160 || desc->bDescriptorType != USB_DT_ENDPOINT)
163 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
166 /* erratum 0119 workaround ties up an endpoint number */
167 if ((desc->bEndpointAddress & 0x0f) == EP_DONTUSE)
170 /* sanity check ep-e/ep-f since their fifos are small */
171 max = le16_to_cpu (desc->wMaxPacketSize) & 0x1fff;
172 if (ep->num > 4 && max > 64)
175 spin_lock_irqsave (&dev->lock, flags);
176 _ep->maxpacket = max & 0x7ff;
179 /* ep_reset() has already been called */
181 ep->out_overflow = 0;
183 /* set speed-dependent max packet; may kick in high bandwidth */
184 set_idx_reg (dev->regs, REG_EP_MAXPKT (dev, ep->num), max);
186 /* FIFO lines can't go to different packets. PIO is ok, so
187 * use it instead of troublesome (non-bulk) multi-packet DMA.
189 if (ep->dma && (max % 4) != 0 && use_dma_chaining) {
190 DEBUG (ep->dev, "%s, no dma for maxpacket %d\n",
191 ep->ep.name, ep->ep.maxpacket);
195 /* set type, direction, address; reset fifo counters */
196 writel ((1 << FIFO_FLUSH), &ep->regs->ep_stat);
197 tmp = (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK);
198 if (tmp == USB_ENDPOINT_XFER_INT) {
199 /* erratum 0105 workaround prevents hs NYET */
200 if (dev->chiprev == 0100
201 && dev->gadget.speed == USB_SPEED_HIGH
202 && !(desc->bEndpointAddress & USB_DIR_IN))
203 writel ((1 << CLEAR_NAK_OUT_PACKETS_MODE),
205 } else if (tmp == USB_ENDPOINT_XFER_BULK) {
206 /* catch some particularly blatant driver bugs */
207 if ((dev->gadget.speed == USB_SPEED_HIGH
209 || (dev->gadget.speed == USB_SPEED_FULL
211 spin_unlock_irqrestore (&dev->lock, flags);
215 ep->is_iso = (tmp == USB_ENDPOINT_XFER_ISOC) ? 1 : 0;
216 tmp <<= ENDPOINT_TYPE;
217 tmp |= desc->bEndpointAddress;
218 tmp |= (4 << ENDPOINT_BYTE_COUNT); /* default full fifo lines */
219 tmp |= 1 << ENDPOINT_ENABLE;
222 /* for OUT transfers, block the rx fifo until a read is posted */
223 ep->is_in = (tmp & USB_DIR_IN) != 0;
225 writel ((1 << SET_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
226 else if (dev->pdev->device != 0x2280) {
227 /* Added for 2282, Don't use nak packets on an in endpoint,
228 * 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 const 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 /*-------------------------------------------------------------------------*/
454 * dma-coherent memory allocation (for dma-capable endpoints)
456 * NOTE: the dma_*_coherent() API calls suck. Most implementations are
457 * (a) page-oriented, so small buffers lose big; and (b) asymmetric with
458 * respect to calls with irqs disabled: alloc is safe, free is not.
459 * We currently work around (b), but not (a).
463 net2280_alloc_buffer (
471 struct net2280_ep *ep;
473 ep = container_of (_ep, struct net2280_ep, ep);
476 *dma = DMA_ADDR_INVALID;
479 retval = dma_alloc_coherent(&ep->dev->pdev->dev,
480 bytes, dma, gfp_flags);
482 retval = kmalloc(bytes, gfp_flags);
486 static DEFINE_SPINLOCK(buflock);
487 static LIST_HEAD(buffers);
490 struct list_head list;
496 static void do_free(unsigned long ignored)
498 spin_lock_irq(&buflock);
499 while (!list_empty(&buffers)) {
500 struct free_record *buf;
502 buf = list_entry(buffers.next, struct free_record, list);
503 list_del(&buf->list);
504 spin_unlock_irq(&buflock);
506 dma_free_coherent(buf->dev, buf->bytes, buf, buf->dma);
508 spin_lock_irq(&buflock);
510 spin_unlock_irq(&buflock);
513 static DECLARE_TASKLET(deferred_free, do_free, 0);
516 net2280_free_buffer (
522 /* free memory into the right allocator */
523 if (dma != DMA_ADDR_INVALID) {
524 struct net2280_ep *ep;
525 struct free_record *buf = address;
528 ep = container_of(_ep, struct net2280_ep, ep);
532 ep = container_of (_ep, struct net2280_ep, ep);
533 buf->dev = &ep->dev->pdev->dev;
537 spin_lock_irqsave(&buflock, flags);
538 list_add_tail(&buf->list, &buffers);
539 tasklet_schedule(&deferred_free);
540 spin_unlock_irqrestore(&buflock, flags);
545 /*-------------------------------------------------------------------------*/
547 /* load a packet into the fifo we use for usb IN transfers.
548 * works for all endpoints.
550 * NOTE: pio with ep-a..ep-d could stuff multiple packets into the fifo
551 * at a time, but this code is simpler because it knows it only writes
552 * one packet. ep-a..ep-d should use dma instead.
555 write_fifo (struct net2280_ep *ep, struct usb_request *req)
557 struct net2280_ep_regs __iomem *regs = ep->regs;
560 unsigned count, total;
562 /* INVARIANT: fifo is currently empty. (testable) */
565 buf = req->buf + req->actual;
567 total = req->length - req->actual;
573 /* write just one packet at a time */
574 count = ep->ep.maxpacket;
575 if (count > total) /* min() cannot be used on a bitfield */
578 VDEBUG (ep->dev, "write %s fifo (IN) %d bytes%s req %p\n",
580 (count != ep->ep.maxpacket) ? " (short)" : "",
583 /* NOTE be careful if you try to align these. fifo lines
584 * should normally be full (4 bytes) and successive partial
585 * lines are ok only in certain cases.
587 tmp = get_unaligned ((u32 *)buf);
589 writel (tmp, ®s->ep_data);
594 /* last fifo entry is "short" unless we wrote a full packet.
595 * also explicitly validate last word in (periodic) transfers
596 * when maxpacket is not a multiple of 4 bytes.
598 if (count || total < ep->ep.maxpacket) {
599 tmp = count ? get_unaligned ((u32 *)buf) : count;
601 set_fifo_bytecount (ep, count & 0x03);
602 writel (tmp, ®s->ep_data);
605 /* pci writes may still be posted */
608 /* work around erratum 0106: PCI and USB race over the OUT fifo.
609 * caller guarantees chiprev 0100, out endpoint is NAKing, and
610 * there's no real data in the fifo.
612 * NOTE: also used in cases where that erratum doesn't apply:
613 * where the host wrote "too much" data to us.
615 static void out_flush (struct net2280_ep *ep)
620 ASSERT_OUT_NAKING (ep);
622 statp = &ep->regs->ep_stat;
623 writel ( (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
624 | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
626 writel ((1 << FIFO_FLUSH), statp);
629 if (tmp & (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
630 /* high speed did bulk NYET; fifo isn't filling */
631 && ep->dev->gadget.speed == USB_SPEED_FULL) {
634 usec = 50; /* 64 byte bulk/interrupt */
635 handshake (statp, (1 << USB_OUT_PING_NAK_SENT),
636 (1 << USB_OUT_PING_NAK_SENT), usec);
637 /* NAK done; now CLEAR_NAK_OUT_PACKETS is safe */
641 /* unload packet(s) from the fifo we use for usb OUT transfers.
642 * returns true iff the request completed, because of short packet
643 * or the request buffer having filled with full packets.
645 * for ep-a..ep-d this will read multiple packets out when they
646 * have been accepted.
649 read_fifo (struct net2280_ep *ep, struct net2280_request *req)
651 struct net2280_ep_regs __iomem *regs = ep->regs;
652 u8 *buf = req->req.buf + req->req.actual;
653 unsigned count, tmp, is_short;
654 unsigned cleanup = 0, prevent = 0;
656 /* erratum 0106 ... packets coming in during fifo reads might
657 * be incompletely rejected. not all cases have workarounds.
659 if (ep->dev->chiprev == 0x0100
660 && ep->dev->gadget.speed == USB_SPEED_FULL) {
662 tmp = readl (&ep->regs->ep_stat);
663 if ((tmp & (1 << NAK_OUT_PACKETS)))
665 else if ((tmp & (1 << FIFO_FULL))) {
666 start_out_naking (ep);
669 /* else: hope we don't see the problem */
672 /* never overflow the rx buffer. the fifo reads packets until
673 * it sees a short one; we might not be ready for them all.
676 count = readl (®s->ep_avail);
677 if (unlikely (count == 0)) {
679 tmp = readl (&ep->regs->ep_stat);
680 count = readl (®s->ep_avail);
681 /* handled that data already? */
682 if (count == 0 && (tmp & (1 << NAK_OUT_PACKETS)) == 0)
686 tmp = req->req.length - req->req.actual;
688 /* as with DMA, data overflow gets flushed */
689 if ((tmp % ep->ep.maxpacket) != 0) {
691 "%s out fifo %d bytes, expected %d\n",
692 ep->ep.name, count, tmp);
693 req->req.status = -EOVERFLOW;
695 /* NAK_OUT_PACKETS will be set, so flushing is safe;
696 * the next read will start with the next packet
698 } /* else it's a ZLP, no worries */
701 req->req.actual += count;
703 is_short = (count == 0) || ((count % ep->ep.maxpacket) != 0);
705 VDEBUG (ep->dev, "read %s fifo (OUT) %d bytes%s%s%s req %p %d/%d\n",
706 ep->ep.name, count, is_short ? " (short)" : "",
707 cleanup ? " flush" : "", prevent ? " nak" : "",
708 req, req->req.actual, req->req.length);
711 tmp = readl (®s->ep_data);
713 put_unaligned (tmp, (u32 *)buf);
718 tmp = readl (®s->ep_data);
719 /* LE conversion is implicit here: */
728 writel ((1 << CLEAR_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
729 (void) readl (&ep->regs->ep_rsp);
732 return is_short || ((req->req.actual == req->req.length)
736 /* fill out dma descriptor to match a given request */
738 fill_dma_desc (struct net2280_ep *ep, struct net2280_request *req, int valid)
740 struct net2280_dma *td = req->td;
741 u32 dmacount = req->req.length;
743 /* don't let DMA continue after a short OUT packet,
744 * so overruns can't affect the next transfer.
745 * in case of overruns on max-size packets, we can't
746 * stop the fifo from filling but we can flush it.
749 dmacount |= (1 << DMA_DIRECTION);
750 if ((!ep->is_in && (dmacount % ep->ep.maxpacket) != 0)
751 || ep->dev->pdev->device != 0x2280)
752 dmacount |= (1 << END_OF_CHAIN);
756 dmacount |= (1 << VALID_BIT);
757 if (likely(!req->req.no_interrupt || !use_dma_chaining))
758 dmacount |= (1 << DMA_DONE_INTERRUPT_ENABLE);
760 /* td->dmadesc = previously set by caller */
761 td->dmaaddr = cpu_to_le32 (req->req.dma);
763 /* 2280 may be polling VALID_BIT through ep->dma->dmadesc */
765 td->dmacount = cpu_to_le32p (&dmacount);
768 static const u32 dmactl_default =
769 (1 << DMA_SCATTER_GATHER_DONE_INTERRUPT)
770 | (1 << DMA_CLEAR_COUNT_ENABLE)
771 /* erratum 0116 workaround part 1 (use POLLING) */
772 | (POLL_100_USEC << DESCRIPTOR_POLLING_RATE)
773 | (1 << DMA_VALID_BIT_POLLING_ENABLE)
774 | (1 << DMA_VALID_BIT_ENABLE)
775 | (1 << DMA_SCATTER_GATHER_ENABLE)
776 /* erratum 0116 workaround part 2 (no AUTOSTART) */
779 static inline void spin_stop_dma (struct net2280_dma_regs __iomem *dma)
781 handshake (&dma->dmactl, (1 << DMA_ENABLE), 0, 50);
784 static inline void stop_dma (struct net2280_dma_regs __iomem *dma)
786 writel (readl (&dma->dmactl) & ~(1 << DMA_ENABLE), &dma->dmactl);
790 static void start_queue (struct net2280_ep *ep, u32 dmactl, u32 td_dma)
792 struct net2280_dma_regs __iomem *dma = ep->dma;
793 unsigned int tmp = (1 << VALID_BIT) | (ep->is_in << DMA_DIRECTION);
795 if (ep->dev->pdev->device != 0x2280)
796 tmp |= (1 << END_OF_CHAIN);
798 writel (tmp, &dma->dmacount);
799 writel (readl (&dma->dmastat), &dma->dmastat);
801 writel (td_dma, &dma->dmadesc);
802 writel (dmactl, &dma->dmactl);
804 /* erratum 0116 workaround part 3: pci arbiter away from net2280 */
805 (void) readl (&ep->dev->pci->pcimstctl);
807 writel ((1 << DMA_START), &dma->dmastat);
810 stop_out_naking (ep);
813 static void start_dma (struct net2280_ep *ep, struct net2280_request *req)
816 struct net2280_dma_regs __iomem *dma = ep->dma;
818 /* FIXME can't use DMA for ZLPs */
820 /* on this path we "know" there's no dma active (yet) */
821 WARN_ON (readl (&dma->dmactl) & (1 << DMA_ENABLE));
822 writel (0, &ep->dma->dmactl);
824 /* previous OUT packet might have been short */
825 if (!ep->is_in && ((tmp = readl (&ep->regs->ep_stat))
826 & (1 << NAK_OUT_PACKETS)) != 0) {
827 writel ((1 << SHORT_PACKET_TRANSFERRED_INTERRUPT),
830 tmp = readl (&ep->regs->ep_avail);
832 writel (readl (&dma->dmastat), &dma->dmastat);
834 /* transfer all/some fifo data */
835 writel (req->req.dma, &dma->dmaaddr);
836 tmp = min (tmp, req->req.length);
838 /* dma irq, faking scatterlist status */
839 req->td->dmacount = cpu_to_le32 (req->req.length - tmp);
840 writel ((1 << DMA_DONE_INTERRUPT_ENABLE)
841 | tmp, &dma->dmacount);
842 req->td->dmadesc = 0;
845 writel ((1 << DMA_ENABLE), &dma->dmactl);
846 writel ((1 << DMA_START), &dma->dmastat);
851 tmp = dmactl_default;
853 /* force packet boundaries between dma requests, but prevent the
854 * controller from automagically writing a last "short" packet
855 * (zero length) unless the driver explicitly said to do that.
858 if (likely ((req->req.length % ep->ep.maxpacket) != 0
860 tmp |= (1 << DMA_FIFO_VALIDATE);
861 ep->in_fifo_validate = 1;
863 ep->in_fifo_validate = 0;
866 /* init req->td, pointing to the current dummy */
867 req->td->dmadesc = cpu_to_le32 (ep->td_dma);
868 fill_dma_desc (ep, req, 1);
870 if (!use_dma_chaining)
871 req->td->dmacount |= __constant_cpu_to_le32 (1 << END_OF_CHAIN);
873 start_queue (ep, tmp, req->td_dma);
877 queue_dma (struct net2280_ep *ep, struct net2280_request *req, int valid)
879 struct net2280_dma *end;
882 /* swap new dummy for old, link; fill and maybe activate */
888 ep->td_dma = req->td_dma;
891 end->dmadesc = cpu_to_le32 (ep->td_dma);
893 fill_dma_desc (ep, req, valid);
897 done (struct net2280_ep *ep, struct net2280_request *req, int status)
900 unsigned stopped = ep->stopped;
902 list_del_init (&req->queue);
904 if (req->req.status == -EINPROGRESS)
905 req->req.status = status;
907 status = req->req.status;
911 pci_unmap_single (dev->pdev, req->req.dma, req->req.length,
912 ep->is_in ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
913 req->req.dma = DMA_ADDR_INVALID;
917 if (status && status != -ESHUTDOWN)
918 VDEBUG (dev, "complete %s req %p stat %d len %u/%u\n",
919 ep->ep.name, &req->req, status,
920 req->req.actual, req->req.length);
922 /* don't modify queue heads during completion callback */
924 spin_unlock (&dev->lock);
925 req->req.complete (&ep->ep, &req->req);
926 spin_lock (&dev->lock);
927 ep->stopped = stopped;
930 /*-------------------------------------------------------------------------*/
933 net2280_queue (struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
935 struct net2280_request *req;
936 struct net2280_ep *ep;
940 /* we always require a cpu-view buffer, so that we can
941 * always use pio (as fallback or whatever).
943 req = container_of (_req, struct net2280_request, req);
944 if (!_req || !_req->complete || !_req->buf
945 || !list_empty (&req->queue))
947 if (_req->length > (~0 & DMA_BYTE_COUNT_MASK))
949 ep = container_of (_ep, struct net2280_ep, ep);
950 if (!_ep || (!ep->desc && ep->num != 0))
953 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
956 /* FIXME implement PIO fallback for ZLPs with DMA */
957 if (ep->dma && _req->length == 0)
960 /* set up dma mapping in case the caller didn't */
961 if (ep->dma && _req->dma == DMA_ADDR_INVALID) {
962 _req->dma = pci_map_single (dev->pdev, _req->buf, _req->length,
963 ep->is_in ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
968 VDEBUG (dev, "%s queue req %p, len %d buf %p\n",
969 _ep->name, _req, _req->length, _req->buf);
972 spin_lock_irqsave (&dev->lock, flags);
974 _req->status = -EINPROGRESS;
977 /* kickstart this i/o queue? */
978 if (list_empty (&ep->queue) && !ep->stopped) {
979 /* use DMA if the endpoint supports it, else pio */
983 /* maybe there's no control data, just status ack */
984 if (ep->num == 0 && _req->length == 0) {
987 VDEBUG (dev, "%s status ack\n", ep->ep.name);
991 /* PIO ... stuff the fifo, or unblock it. */
993 write_fifo (ep, _req);
994 else if (list_empty (&ep->queue)) {
997 /* OUT FIFO might have packet(s) buffered */
998 s = readl (&ep->regs->ep_stat);
999 if ((s & (1 << FIFO_EMPTY)) == 0) {
1000 /* note: _req->short_not_ok is
1001 * ignored here since PIO _always_
1002 * stops queue advance here, and
1003 * _req->status doesn't change for
1004 * short reads (only _req->actual)
1006 if (read_fifo (ep, req)) {
1010 /* don't queue it */
1013 s = readl (&ep->regs->ep_stat);
1016 /* don't NAK, let the fifo fill */
1017 if (req && (s & (1 << NAK_OUT_PACKETS)))
1018 writel ((1 << CLEAR_NAK_OUT_PACKETS),
1023 } else if (ep->dma) {
1029 /* preventing magic zlps is per-engine state, not
1030 * per-transfer; irq logic must recover hiccups.
1032 expect = likely (req->req.zero
1033 || (req->req.length % ep->ep.maxpacket) != 0);
1034 if (expect != ep->in_fifo_validate)
1037 queue_dma (ep, req, valid);
1039 } /* else the irq handler advances the queue. */
1043 list_add_tail (&req->queue, &ep->queue);
1045 spin_unlock_irqrestore (&dev->lock, flags);
1047 /* pci writes may still be posted */
1053 struct net2280_ep *ep,
1054 struct net2280_request *req,
1059 req->req.actual = req->req.length - (DMA_BYTE_COUNT_MASK & dmacount);
1060 done (ep, req, status);
1063 static void restart_dma (struct net2280_ep *ep);
1065 static void scan_dma_completions (struct net2280_ep *ep)
1067 /* only look at descriptors that were "naturally" retired,
1068 * so fifo and list head state won't matter
1070 while (!list_empty (&ep->queue)) {
1071 struct net2280_request *req;
1074 req = list_entry (ep->queue.next,
1075 struct net2280_request, queue);
1079 tmp = le32_to_cpup (&req->td->dmacount);
1080 if ((tmp & (1 << VALID_BIT)) != 0)
1083 /* SHORT_PACKET_TRANSFERRED_INTERRUPT handles "usb-short"
1084 * cases where DMA must be aborted; this code handles
1085 * all non-abort DMA completions.
1087 if (unlikely (req->td->dmadesc == 0)) {
1089 tmp = readl (&ep->dma->dmacount);
1090 if (tmp & DMA_BYTE_COUNT_MASK)
1092 /* single transfer mode */
1093 dma_done (ep, req, tmp, 0);
1095 } else if (!ep->is_in
1096 && (req->req.length % ep->ep.maxpacket) != 0) {
1097 tmp = readl (&ep->regs->ep_stat);
1099 /* AVOID TROUBLE HERE by not issuing short reads from
1100 * your gadget driver. That helps avoids errata 0121,
1101 * 0122, and 0124; not all cases trigger the warning.
1103 if ((tmp & (1 << NAK_OUT_PACKETS)) == 0) {
1104 WARN (ep->dev, "%s lost packet sync!\n",
1106 req->req.status = -EOVERFLOW;
1107 } else if ((tmp = readl (&ep->regs->ep_avail)) != 0) {
1108 /* fifo gets flushed later */
1109 ep->out_overflow = 1;
1110 DEBUG (ep->dev, "%s dma, discard %d len %d\n",
1113 req->req.status = -EOVERFLOW;
1116 dma_done (ep, req, tmp, 0);
1120 static void restart_dma (struct net2280_ep *ep)
1122 struct net2280_request *req;
1123 u32 dmactl = dmactl_default;
1127 req = list_entry (ep->queue.next, struct net2280_request, queue);
1129 if (!use_dma_chaining) {
1130 start_dma (ep, req);
1134 /* the 2280 will be processing the queue unless queue hiccups after
1135 * the previous transfer:
1136 * IN: wanted automagic zlp, head doesn't (or vice versa)
1137 * DMA_FIFO_VALIDATE doesn't init from dma descriptors.
1138 * OUT: was "usb-short", we must restart.
1140 if (ep->is_in && !req->valid) {
1141 struct net2280_request *entry, *prev = NULL;
1142 int reqmode, done = 0;
1144 DEBUG (ep->dev, "%s dma hiccup td %p\n", ep->ep.name, req->td);
1145 ep->in_fifo_validate = likely (req->req.zero
1146 || (req->req.length % ep->ep.maxpacket) != 0);
1147 if (ep->in_fifo_validate)
1148 dmactl |= (1 << DMA_FIFO_VALIDATE);
1149 list_for_each_entry (entry, &ep->queue, queue) {
1154 dmacount = entry->td->dmacount;
1156 reqmode = likely (entry->req.zero
1157 || (entry->req.length
1158 % ep->ep.maxpacket) != 0);
1159 if (reqmode == ep->in_fifo_validate) {
1161 dmacount |= valid_bit;
1162 entry->td->dmacount = dmacount;
1166 /* force a hiccup */
1167 prev->td->dmacount |= dma_done_ie;
1172 /* walk the rest of the queue so unlinks behave */
1174 dmacount &= ~valid_bit;
1175 entry->td->dmacount = dmacount;
1180 writel (0, &ep->dma->dmactl);
1181 start_queue (ep, dmactl, req->td_dma);
1184 static void abort_dma (struct net2280_ep *ep)
1186 /* abort the current transfer */
1187 if (likely (!list_empty (&ep->queue))) {
1188 /* FIXME work around errata 0121, 0122, 0124 */
1189 writel ((1 << DMA_ABORT), &ep->dma->dmastat);
1190 spin_stop_dma (ep->dma);
1193 scan_dma_completions (ep);
1196 /* dequeue ALL requests */
1197 static void nuke (struct net2280_ep *ep)
1199 struct net2280_request *req;
1201 /* called with spinlock held */
1205 while (!list_empty (&ep->queue)) {
1206 req = list_entry (ep->queue.next,
1207 struct net2280_request,
1209 done (ep, req, -ESHUTDOWN);
1213 /* dequeue JUST ONE request */
1214 static int net2280_dequeue (struct usb_ep *_ep, struct usb_request *_req)
1216 struct net2280_ep *ep;
1217 struct net2280_request *req;
1218 unsigned long flags;
1222 ep = container_of (_ep, struct net2280_ep, ep);
1223 if (!_ep || (!ep->desc && ep->num != 0) || !_req)
1226 spin_lock_irqsave (&ep->dev->lock, flags);
1227 stopped = ep->stopped;
1229 /* quiesce dma while we patch the queue */
1233 dmactl = readl (&ep->dma->dmactl);
1234 /* WARNING erratum 0127 may kick in ... */
1236 scan_dma_completions (ep);
1239 /* make sure it's still queued on this endpoint */
1240 list_for_each_entry (req, &ep->queue, queue) {
1241 if (&req->req == _req)
1244 if (&req->req != _req) {
1245 spin_unlock_irqrestore (&ep->dev->lock, flags);
1249 /* queue head may be partially complete. */
1250 if (ep->queue.next == &req->queue) {
1252 DEBUG (ep->dev, "unlink (%s) dma\n", _ep->name);
1253 _req->status = -ECONNRESET;
1255 if (likely (ep->queue.next == &req->queue)) {
1256 // NOTE: misreports single-transfer mode
1257 req->td->dmacount = 0; /* invalidate */
1259 readl (&ep->dma->dmacount),
1263 DEBUG (ep->dev, "unlink (%s) pio\n", _ep->name);
1264 done (ep, req, -ECONNRESET);
1268 /* patch up hardware chaining data */
1269 } else if (ep->dma && use_dma_chaining) {
1270 if (req->queue.prev == ep->queue.next) {
1271 writel (le32_to_cpu (req->td->dmadesc),
1273 if (req->td->dmacount & dma_done_ie)
1274 writel (readl (&ep->dma->dmacount)
1275 | le32_to_cpu(dma_done_ie),
1276 &ep->dma->dmacount);
1278 struct net2280_request *prev;
1280 prev = list_entry (req->queue.prev,
1281 struct net2280_request, queue);
1282 prev->td->dmadesc = req->td->dmadesc;
1283 if (req->td->dmacount & dma_done_ie)
1284 prev->td->dmacount |= dma_done_ie;
1289 done (ep, req, -ECONNRESET);
1290 ep->stopped = stopped;
1293 /* turn off dma on inactive queues */
1294 if (list_empty (&ep->queue))
1296 else if (!ep->stopped) {
1297 /* resume current request, or start new one */
1299 writel (dmactl, &ep->dma->dmactl);
1301 start_dma (ep, list_entry (ep->queue.next,
1302 struct net2280_request, queue));
1306 spin_unlock_irqrestore (&ep->dev->lock, flags);
1310 /*-------------------------------------------------------------------------*/
1312 static int net2280_fifo_status (struct usb_ep *_ep);
1315 net2280_set_halt (struct usb_ep *_ep, int value)
1317 struct net2280_ep *ep;
1318 unsigned long flags;
1321 ep = container_of (_ep, struct net2280_ep, ep);
1322 if (!_ep || (!ep->desc && ep->num != 0))
1324 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1326 if (ep->desc /* not ep0 */ && (ep->desc->bmAttributes & 0x03)
1327 == USB_ENDPOINT_XFER_ISOC)
1330 spin_lock_irqsave (&ep->dev->lock, flags);
1331 if (!list_empty (&ep->queue))
1333 else if (ep->is_in && value && net2280_fifo_status (_ep) != 0)
1336 VDEBUG (ep->dev, "%s %s halt\n", _ep->name,
1337 value ? "set" : "clear");
1338 /* set/clear, then synch memory views with the device */
1341 ep->dev->protocol_stall = 1;
1346 (void) readl (&ep->regs->ep_rsp);
1348 spin_unlock_irqrestore (&ep->dev->lock, flags);
1354 net2280_fifo_status (struct usb_ep *_ep)
1356 struct net2280_ep *ep;
1359 ep = container_of (_ep, struct net2280_ep, ep);
1360 if (!_ep || (!ep->desc && ep->num != 0))
1362 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1365 avail = readl (&ep->regs->ep_avail) & ((1 << 12) - 1);
1366 if (avail > ep->fifo_size)
1369 avail = ep->fifo_size - avail;
1374 net2280_fifo_flush (struct usb_ep *_ep)
1376 struct net2280_ep *ep;
1378 ep = container_of (_ep, struct net2280_ep, ep);
1379 if (!_ep || (!ep->desc && ep->num != 0))
1381 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1384 writel ((1 << FIFO_FLUSH), &ep->regs->ep_stat);
1385 (void) readl (&ep->regs->ep_rsp);
1388 static const struct usb_ep_ops net2280_ep_ops = {
1389 .enable = net2280_enable,
1390 .disable = net2280_disable,
1392 .alloc_request = net2280_alloc_request,
1393 .free_request = net2280_free_request,
1395 .alloc_buffer = net2280_alloc_buffer,
1396 .free_buffer = net2280_free_buffer,
1398 .queue = net2280_queue,
1399 .dequeue = net2280_dequeue,
1401 .set_halt = net2280_set_halt,
1402 .fifo_status = net2280_fifo_status,
1403 .fifo_flush = net2280_fifo_flush,
1406 /*-------------------------------------------------------------------------*/
1408 static int net2280_get_frame (struct usb_gadget *_gadget)
1410 struct net2280 *dev;
1411 unsigned long flags;
1416 dev = container_of (_gadget, struct net2280, gadget);
1417 spin_lock_irqsave (&dev->lock, flags);
1418 retval = get_idx_reg (dev->regs, REG_FRAME) & 0x03ff;
1419 spin_unlock_irqrestore (&dev->lock, flags);
1423 static int net2280_wakeup (struct usb_gadget *_gadget)
1425 struct net2280 *dev;
1427 unsigned long flags;
1431 dev = container_of (_gadget, struct net2280, gadget);
1433 spin_lock_irqsave (&dev->lock, flags);
1434 tmp = readl (&dev->usb->usbctl);
1435 if (tmp & (1 << DEVICE_REMOTE_WAKEUP_ENABLE))
1436 writel (1 << GENERATE_RESUME, &dev->usb->usbstat);
1437 spin_unlock_irqrestore (&dev->lock, flags);
1439 /* pci writes may still be posted */
1443 static int net2280_set_selfpowered (struct usb_gadget *_gadget, int value)
1445 struct net2280 *dev;
1447 unsigned long flags;
1451 dev = container_of (_gadget, struct net2280, gadget);
1453 spin_lock_irqsave (&dev->lock, flags);
1454 tmp = readl (&dev->usb->usbctl);
1456 tmp |= (1 << SELF_POWERED_STATUS);
1458 tmp &= ~(1 << SELF_POWERED_STATUS);
1459 writel (tmp, &dev->usb->usbctl);
1460 spin_unlock_irqrestore (&dev->lock, flags);
1465 static int net2280_pullup(struct usb_gadget *_gadget, int is_on)
1467 struct net2280 *dev;
1469 unsigned long flags;
1473 dev = container_of (_gadget, struct net2280, gadget);
1475 spin_lock_irqsave (&dev->lock, flags);
1476 tmp = readl (&dev->usb->usbctl);
1477 dev->softconnect = (is_on != 0);
1479 tmp |= (1 << USB_DETECT_ENABLE);
1481 tmp &= ~(1 << USB_DETECT_ENABLE);
1482 writel (tmp, &dev->usb->usbctl);
1483 spin_unlock_irqrestore (&dev->lock, flags);
1488 static const struct usb_gadget_ops net2280_ops = {
1489 .get_frame = net2280_get_frame,
1490 .wakeup = net2280_wakeup,
1491 .set_selfpowered = net2280_set_selfpowered,
1492 .pullup = net2280_pullup,
1495 /*-------------------------------------------------------------------------*/
1497 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1499 /* FIXME move these into procfs, and use seq_file.
1500 * Sysfs _still_ doesn't behave for arbitrarily sized files,
1501 * and also doesn't help products using this with 2.4 kernels.
1504 /* "function" sysfs attribute */
1506 show_function (struct device *_dev, struct device_attribute *attr, char *buf)
1508 struct net2280 *dev = dev_get_drvdata (_dev);
1511 || !dev->driver->function
1512 || strlen (dev->driver->function) > PAGE_SIZE)
1514 return scnprintf (buf, PAGE_SIZE, "%s\n", dev->driver->function);
1516 static DEVICE_ATTR (function, S_IRUGO, show_function, NULL);
1519 show_registers (struct device *_dev, struct device_attribute *attr, char *buf)
1521 struct net2280 *dev;
1524 unsigned long flags;
1529 dev = dev_get_drvdata (_dev);
1532 spin_lock_irqsave (&dev->lock, flags);
1535 s = dev->driver->driver.name;
1539 /* Main Control Registers */
1540 t = scnprintf (next, size, "%s version " DRIVER_VERSION
1541 ", chiprev %04x, dma %s\n\n"
1542 "devinit %03x fifoctl %08x gadget '%s'\n"
1543 "pci irqenb0 %02x irqenb1 %08x "
1544 "irqstat0 %04x irqstat1 %08x\n",
1545 driver_name, dev->chiprev,
1547 ? (use_dma_chaining ? "chaining" : "enabled")
1549 readl (&dev->regs->devinit),
1550 readl (&dev->regs->fifoctl),
1552 readl (&dev->regs->pciirqenb0),
1553 readl (&dev->regs->pciirqenb1),
1554 readl (&dev->regs->irqstat0),
1555 readl (&dev->regs->irqstat1));
1559 /* USB Control Registers */
1560 t1 = readl (&dev->usb->usbctl);
1561 t2 = readl (&dev->usb->usbstat);
1562 if (t1 & (1 << VBUS_PIN)) {
1563 if (t2 & (1 << HIGH_SPEED))
1565 else if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1569 /* full speed bit (6) not working?? */
1572 t = scnprintf (next, size,
1573 "stdrsp %08x usbctl %08x usbstat %08x "
1574 "addr 0x%02x (%s)\n",
1575 readl (&dev->usb->stdrsp), t1, t2,
1576 readl (&dev->usb->ouraddr), s);
1580 /* PCI Master Control Registers */
1582 /* DMA Control Registers */
1584 /* Configurable EP Control Registers */
1585 for (i = 0; i < 7; i++) {
1586 struct net2280_ep *ep;
1592 t1 = readl (&ep->regs->ep_cfg);
1593 t2 = readl (&ep->regs->ep_rsp) & 0xff;
1594 t = scnprintf (next, size,
1595 "\n%s\tcfg %05x rsp (%02x) %s%s%s%s%s%s%s%s"
1597 ep->ep.name, t1, t2,
1598 (t2 & (1 << CLEAR_NAK_OUT_PACKETS))
1600 (t2 & (1 << CLEAR_EP_HIDE_STATUS_PHASE))
1602 (t2 & (1 << CLEAR_EP_FORCE_CRC_ERROR))
1604 (t2 & (1 << CLEAR_INTERRUPT_MODE))
1605 ? "interrupt " : "",
1606 (t2 & (1<<CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE))
1608 (t2 & (1 << CLEAR_NAK_OUT_PACKETS_MODE))
1610 (t2 & (1 << CLEAR_ENDPOINT_TOGGLE))
1611 ? "DATA1 " : "DATA0 ",
1612 (t2 & (1 << CLEAR_ENDPOINT_HALT))
1614 readl (&ep->regs->ep_irqenb));
1618 t = scnprintf (next, size,
1619 "\tstat %08x avail %04x "
1621 readl (&ep->regs->ep_stat),
1622 readl (&ep->regs->ep_avail),
1623 t1 & 0x0f, DIR_STRING (t1),
1624 type_string (t1 >> 8),
1625 ep->stopped ? "*" : "");
1632 t = scnprintf (next, size,
1633 " dma\tctl %08x stat %08x count %08x\n"
1634 "\taddr %08x desc %08x\n",
1635 readl (&ep->dma->dmactl),
1636 readl (&ep->dma->dmastat),
1637 readl (&ep->dma->dmacount),
1638 readl (&ep->dma->dmaaddr),
1639 readl (&ep->dma->dmadesc));
1645 /* Indexed Registers */
1649 t = scnprintf (next, size, "\nirqs: ");
1652 for (i = 0; i < 7; i++) {
1653 struct net2280_ep *ep;
1658 t = scnprintf (next, size, " %s/%lu", ep->ep.name, ep->irqs);
1663 t = scnprintf (next, size, "\n");
1667 spin_unlock_irqrestore (&dev->lock, flags);
1669 return PAGE_SIZE - size;
1671 static DEVICE_ATTR (registers, S_IRUGO, show_registers, NULL);
1674 show_queues (struct device *_dev, struct device_attribute *attr, char *buf)
1676 struct net2280 *dev;
1679 unsigned long flags;
1682 dev = dev_get_drvdata (_dev);
1685 spin_lock_irqsave (&dev->lock, flags);
1687 for (i = 0; i < 7; i++) {
1688 struct net2280_ep *ep = &dev->ep [i];
1689 struct net2280_request *req;
1693 const struct usb_endpoint_descriptor *d;
1698 t = d->bEndpointAddress;
1699 t = scnprintf (next, size,
1700 "\n%s (ep%d%s-%s) max %04x %s fifo %d\n",
1701 ep->ep.name, t & USB_ENDPOINT_NUMBER_MASK,
1702 (t & USB_DIR_IN) ? "in" : "out",
1704 switch (d->bmAttributes & 0x03) {
1705 case USB_ENDPOINT_XFER_BULK:
1706 val = "bulk"; break;
1707 case USB_ENDPOINT_XFER_INT:
1708 val = "intr"; break;
1712 le16_to_cpu (d->wMaxPacketSize) & 0x1fff,
1713 ep->dma ? "dma" : "pio", ep->fifo_size
1715 } else /* ep0 should only have one transfer queued */
1716 t = scnprintf (next, size, "ep0 max 64 pio %s\n",
1717 ep->is_in ? "in" : "out");
1718 if (t <= 0 || t > size)
1723 if (list_empty (&ep->queue)) {
1724 t = scnprintf (next, size, "\t(nothing queued)\n");
1725 if (t <= 0 || t > size)
1731 list_for_each_entry (req, &ep->queue, queue) {
1732 if (ep->dma && req->td_dma == readl (&ep->dma->dmadesc))
1733 t = scnprintf (next, size,
1734 "\treq %p len %d/%d "
1735 "buf %p (dmacount %08x)\n",
1736 &req->req, req->req.actual,
1737 req->req.length, req->req.buf,
1738 readl (&ep->dma->dmacount));
1740 t = scnprintf (next, size,
1741 "\treq %p len %d/%d buf %p\n",
1742 &req->req, req->req.actual,
1743 req->req.length, req->req.buf);
1744 if (t <= 0 || t > size)
1750 struct net2280_dma *td;
1753 t = scnprintf (next, size, "\t td %08x "
1754 " count %08x buf %08x desc %08x\n",
1756 le32_to_cpu (td->dmacount),
1757 le32_to_cpu (td->dmaaddr),
1758 le32_to_cpu (td->dmadesc));
1759 if (t <= 0 || t > size)
1768 spin_unlock_irqrestore (&dev->lock, flags);
1769 return PAGE_SIZE - size;
1771 static DEVICE_ATTR (queues, S_IRUGO, show_queues, NULL);
1776 #define device_create_file(a,b) (0)
1777 #define device_remove_file(a,b) do { } while (0)
1781 /*-------------------------------------------------------------------------*/
1783 /* another driver-specific mode might be a request type doing dma
1784 * to/from another device fifo instead of to/from memory.
1787 static void set_fifo_mode (struct net2280 *dev, int mode)
1789 /* keeping high bits preserves BAR2 */
1790 writel ((0xffff << PCI_BASE2_RANGE) | mode, &dev->regs->fifoctl);
1792 /* always ep-{a,b,e,f} ... maybe not ep-c or ep-d */
1793 INIT_LIST_HEAD (&dev->gadget.ep_list);
1794 list_add_tail (&dev->ep [1].ep.ep_list, &dev->gadget.ep_list);
1795 list_add_tail (&dev->ep [2].ep.ep_list, &dev->gadget.ep_list);
1798 list_add_tail (&dev->ep [3].ep.ep_list, &dev->gadget.ep_list);
1799 list_add_tail (&dev->ep [4].ep.ep_list, &dev->gadget.ep_list);
1800 dev->ep [1].fifo_size = dev->ep [2].fifo_size = 1024;
1803 dev->ep [1].fifo_size = dev->ep [2].fifo_size = 2048;
1806 list_add_tail (&dev->ep [3].ep.ep_list, &dev->gadget.ep_list);
1807 dev->ep [1].fifo_size = 2048;
1808 dev->ep [2].fifo_size = 1024;
1811 /* fifo sizes for ep0, ep-c, ep-d, ep-e, and ep-f never change */
1812 list_add_tail (&dev->ep [5].ep.ep_list, &dev->gadget.ep_list);
1813 list_add_tail (&dev->ep [6].ep.ep_list, &dev->gadget.ep_list);
1816 /* just declare this in any driver that really need it */
1817 extern int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode);
1820 * net2280_set_fifo_mode - change allocation of fifo buffers
1821 * @gadget: access to the net2280 device that will be updated
1822 * @mode: 0 for default, four 1kB buffers (ep-a through ep-d);
1823 * 1 for two 2kB buffers (ep-a and ep-b only);
1824 * 2 for one 2kB buffer (ep-a) and two 1kB ones (ep-b, ep-c).
1826 * returns zero on success, else negative errno. when this succeeds,
1827 * the contents of gadget->ep_list may have changed.
1829 * you may only call this function when endpoints a-d are all disabled.
1830 * use it whenever extra hardware buffering can help performance, such
1831 * as before enabling "high bandwidth" interrupt endpoints that use
1832 * maxpacket bigger than 512 (when double buffering would otherwise
1835 int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode)
1838 struct net2280 *dev;
1840 unsigned long flags;
1844 dev = container_of (gadget, struct net2280, gadget);
1846 spin_lock_irqsave (&dev->lock, flags);
1848 for (i = 1; i <= 4; i++)
1849 if (dev->ep [i].desc) {
1853 if (mode < 0 || mode > 2)
1856 set_fifo_mode (dev, mode);
1857 spin_unlock_irqrestore (&dev->lock, flags);
1861 DEBUG (dev, "fifo: ep-a 2K, ep-b 2K\n");
1863 DEBUG (dev, "fifo: ep-a 2K, ep-b 1K, ep-c 1K\n");
1864 /* else all are 1K */
1868 EXPORT_SYMBOL (net2280_set_fifo_mode);
1870 /*-------------------------------------------------------------------------*/
1872 /* keeping it simple:
1873 * - one bus driver, initted first;
1874 * - one function driver, initted second
1876 * most of the work to support multiple net2280 controllers would
1877 * be to associate this gadget driver (yes?) with all of them, or
1878 * perhaps to bind specific drivers to specific devices.
1881 static struct net2280 *the_controller;
1883 static void usb_reset (struct net2280 *dev)
1887 dev->gadget.speed = USB_SPEED_UNKNOWN;
1888 (void) readl (&dev->usb->usbctl);
1890 net2280_led_init (dev);
1892 /* disable automatic responses, and irqs */
1893 writel (0, &dev->usb->stdrsp);
1894 writel (0, &dev->regs->pciirqenb0);
1895 writel (0, &dev->regs->pciirqenb1);
1897 /* clear old dma and irq state */
1898 for (tmp = 0; tmp < 4; tmp++) {
1899 struct net2280_ep *ep = &dev->ep [tmp + 1];
1904 writel (~0, &dev->regs->irqstat0),
1905 writel (~(1 << SUSPEND_REQUEST_INTERRUPT), &dev->regs->irqstat1),
1907 /* reset, and enable pci */
1908 tmp = readl (&dev->regs->devinit)
1910 | (1 << FIFO_SOFT_RESET)
1911 | (1 << USB_SOFT_RESET)
1912 | (1 << M8051_RESET);
1913 writel (tmp, &dev->regs->devinit);
1915 /* standard fifo and endpoint allocations */
1916 set_fifo_mode (dev, (fifo_mode <= 2) ? fifo_mode : 0);
1919 static void usb_reinit (struct net2280 *dev)
1924 /* use_dma changes are ignored till next device re-init */
1927 /* basic endpoint init */
1928 for (tmp = 0; tmp < 7; tmp++) {
1929 struct net2280_ep *ep = &dev->ep [tmp];
1931 ep->ep.name = ep_name [tmp];
1935 if (tmp > 0 && tmp <= 4) {
1936 ep->fifo_size = 1024;
1938 ep->dma = &dev->dma [tmp - 1];
1941 ep->regs = &dev->epregs [tmp];
1942 ep_reset (dev->regs, ep);
1944 dev->ep [0].ep.maxpacket = 64;
1945 dev->ep [5].ep.maxpacket = 64;
1946 dev->ep [6].ep.maxpacket = 64;
1948 dev->gadget.ep0 = &dev->ep [0].ep;
1949 dev->ep [0].stopped = 0;
1950 INIT_LIST_HEAD (&dev->gadget.ep0->ep_list);
1952 /* we want to prevent lowlevel/insecure access from the USB host,
1953 * but erratum 0119 means this enable bit is ignored
1955 for (tmp = 0; tmp < 5; tmp++)
1956 writel (EP_DONTUSE, &dev->dep [tmp].dep_cfg);
1959 static void ep0_start (struct net2280 *dev)
1961 writel ( (1 << CLEAR_EP_HIDE_STATUS_PHASE)
1962 | (1 << CLEAR_NAK_OUT_PACKETS)
1963 | (1 << CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE)
1964 , &dev->epregs [0].ep_rsp);
1967 * hardware optionally handles a bunch of standard requests
1968 * that the API hides from drivers anyway. have it do so.
1969 * endpoint status/features are handled in software, to
1970 * help pass tests for some dubious behavior.
1972 writel ( (1 << SET_TEST_MODE)
1973 | (1 << SET_ADDRESS)
1974 | (1 << DEVICE_SET_CLEAR_DEVICE_REMOTE_WAKEUP)
1975 | (1 << GET_DEVICE_STATUS)
1976 | (1 << GET_INTERFACE_STATUS)
1977 , &dev->usb->stdrsp);
1978 writel ( (1 << USB_ROOT_PORT_WAKEUP_ENABLE)
1979 | (1 << SELF_POWERED_USB_DEVICE)
1980 | (1 << REMOTE_WAKEUP_SUPPORT)
1981 | (dev->softconnect << USB_DETECT_ENABLE)
1982 | (1 << SELF_POWERED_STATUS)
1983 , &dev->usb->usbctl);
1985 /* enable irqs so we can see ep0 and general operation */
1986 writel ( (1 << SETUP_PACKET_INTERRUPT_ENABLE)
1987 | (1 << ENDPOINT_0_INTERRUPT_ENABLE)
1988 , &dev->regs->pciirqenb0);
1989 writel ( (1 << PCI_INTERRUPT_ENABLE)
1990 | (1 << PCI_MASTER_ABORT_RECEIVED_INTERRUPT_ENABLE)
1991 | (1 << PCI_TARGET_ABORT_RECEIVED_INTERRUPT_ENABLE)
1992 | (1 << PCI_RETRY_ABORT_INTERRUPT_ENABLE)
1993 | (1 << VBUS_INTERRUPT_ENABLE)
1994 | (1 << ROOT_PORT_RESET_INTERRUPT_ENABLE)
1995 | (1 << SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE)
1996 , &dev->regs->pciirqenb1);
1998 /* don't leave any writes posted */
1999 (void) readl (&dev->usb->usbctl);
2002 /* when a driver is successfully registered, it will receive
2003 * control requests including set_configuration(), which enables
2004 * non-control requests. then usb traffic follows until a
2005 * disconnect is reported. then a host may connect again, or
2006 * the driver might get unbound.
2008 int usb_gadget_register_driver (struct usb_gadget_driver *driver)
2010 struct net2280 *dev = the_controller;
2014 /* insist on high speed support from the driver, since
2015 * (dev->usb->xcvrdiag & FORCE_FULL_SPEED_MODE)
2016 * "must not be used in normal operation"
2019 || driver->speed != USB_SPEED_HIGH
2028 for (i = 0; i < 7; i++)
2029 dev->ep [i].irqs = 0;
2031 /* hook up the driver ... */
2032 dev->softconnect = 1;
2033 driver->driver.bus = NULL;
2034 dev->driver = driver;
2035 dev->gadget.dev.driver = &driver->driver;
2036 retval = driver->bind (&dev->gadget);
2038 DEBUG (dev, "bind to driver %s --> %d\n",
2039 driver->driver.name, retval);
2041 dev->gadget.dev.driver = NULL;
2045 retval = device_create_file (&dev->pdev->dev, &dev_attr_function);
2046 if (retval) goto err_unbind;
2047 retval = device_create_file (&dev->pdev->dev, &dev_attr_queues);
2048 if (retval) goto err_func;
2050 /* ... then enable host detection and ep0; and we're ready
2051 * for set_configuration as well as eventual disconnect.
2053 net2280_led_active (dev, 1);
2056 DEBUG (dev, "%s ready, usbctl %08x stdrsp %08x\n",
2057 driver->driver.name,
2058 readl (&dev->usb->usbctl),
2059 readl (&dev->usb->stdrsp));
2061 /* pci writes may still be posted */
2065 device_remove_file (&dev->pdev->dev, &dev_attr_function);
2067 driver->unbind (&dev->gadget);
2068 dev->gadget.dev.driver = NULL;
2072 EXPORT_SYMBOL (usb_gadget_register_driver);
2075 stop_activity (struct net2280 *dev, struct usb_gadget_driver *driver)
2079 /* don't disconnect if it's not connected */
2080 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
2083 /* stop hardware; prevent new request submissions;
2084 * and kill any outstanding requests.
2087 for (i = 0; i < 7; i++)
2088 nuke (&dev->ep [i]);
2090 /* report disconnect; the driver is already quiesced */
2092 spin_unlock (&dev->lock);
2093 driver->disconnect (&dev->gadget);
2094 spin_lock (&dev->lock);
2100 int usb_gadget_unregister_driver (struct usb_gadget_driver *driver)
2102 struct net2280 *dev = the_controller;
2103 unsigned long flags;
2107 if (!driver || driver != dev->driver || !driver->unbind)
2110 spin_lock_irqsave (&dev->lock, flags);
2111 stop_activity (dev, driver);
2112 spin_unlock_irqrestore (&dev->lock, flags);
2114 net2280_pullup (&dev->gadget, 0);
2116 driver->unbind (&dev->gadget);
2117 dev->gadget.dev.driver = NULL;
2120 net2280_led_active (dev, 0);
2121 device_remove_file (&dev->pdev->dev, &dev_attr_function);
2122 device_remove_file (&dev->pdev->dev, &dev_attr_queues);
2124 DEBUG (dev, "unregistered driver '%s'\n", driver->driver.name);
2127 EXPORT_SYMBOL (usb_gadget_unregister_driver);
2130 /*-------------------------------------------------------------------------*/
2132 /* handle ep0, ep-e, ep-f with 64 byte packets: packet per irq.
2133 * also works for dma-capable endpoints, in pio mode or just
2134 * to manually advance the queue after short OUT transfers.
2136 static void handle_ep_small (struct net2280_ep *ep)
2138 struct net2280_request *req;
2140 /* 0 error, 1 mid-data, 2 done */
2143 if (!list_empty (&ep->queue))
2144 req = list_entry (ep->queue.next,
2145 struct net2280_request, queue);
2149 /* ack all, and handle what we care about */
2150 t = readl (&ep->regs->ep_stat);
2153 VDEBUG (ep->dev, "%s ack ep_stat %08x, req %p\n",
2154 ep->ep.name, t, req ? &req->req : 0);
2156 if (!ep->is_in || ep->dev->pdev->device == 0x2280)
2157 writel (t & ~(1 << NAK_OUT_PACKETS), &ep->regs->ep_stat);
2159 /* Added for 2282 */
2160 writel (t, &ep->regs->ep_stat);
2162 /* for ep0, monitor token irqs to catch data stage length errors
2163 * and to synchronize on status.
2165 * also, to defer reporting of protocol stalls ... here's where
2166 * data or status first appears, handling stalls here should never
2167 * cause trouble on the host side..
2169 * control requests could be slightly faster without token synch for
2170 * status, but status can jam up that way.
2172 if (unlikely (ep->num == 0)) {
2174 /* status; stop NAKing */
2175 if (t & (1 << DATA_OUT_PING_TOKEN_INTERRUPT)) {
2176 if (ep->dev->protocol_stall) {
2183 /* reply to extra IN data tokens with a zlp */
2184 } else if (t & (1 << DATA_IN_TOKEN_INTERRUPT)) {
2185 if (ep->dev->protocol_stall) {
2189 } else if (ep->responded &&
2190 !req && !ep->stopped)
2191 write_fifo (ep, NULL);
2194 /* status; stop NAKing */
2195 if (t & (1 << DATA_IN_TOKEN_INTERRUPT)) {
2196 if (ep->dev->protocol_stall) {
2201 /* an extra OUT token is an error */
2202 } else if (((t & (1 << DATA_OUT_PING_TOKEN_INTERRUPT))
2204 && req->req.actual == req->req.length)
2205 || (ep->responded && !req)) {
2206 ep->dev->protocol_stall = 1;
2210 done (ep, req, -EOVERFLOW);
2216 if (unlikely (!req))
2219 /* manual DMA queue advance after short OUT */
2220 if (likely (ep->dma != 0)) {
2221 if (t & (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)) {
2223 int stopped = ep->stopped;
2225 /* TRANSFERRED works around OUT_DONE erratum 0112.
2226 * we expect (N <= maxpacket) bytes; host wrote M.
2227 * iff (M < N) we won't ever see a DMA interrupt.
2230 for (count = 0; ; t = readl (&ep->regs->ep_stat)) {
2232 /* any preceding dma transfers must finish.
2233 * dma handles (M >= N), may empty the queue
2235 scan_dma_completions (ep);
2236 if (unlikely (list_empty (&ep->queue)
2237 || ep->out_overflow)) {
2241 req = list_entry (ep->queue.next,
2242 struct net2280_request, queue);
2244 /* here either (M < N), a "real" short rx;
2245 * or (M == N) and the queue didn't empty
2247 if (likely (t & (1 << FIFO_EMPTY))) {
2248 count = readl (&ep->dma->dmacount);
2249 count &= DMA_BYTE_COUNT_MASK;
2250 if (readl (&ep->dma->dmadesc)
2258 /* stop DMA, leave ep NAKing */
2259 writel ((1 << DMA_ABORT), &ep->dma->dmastat);
2260 spin_stop_dma (ep->dma);
2263 req->td->dmacount = 0;
2264 t = readl (&ep->regs->ep_avail);
2265 dma_done (ep, req, count,
2266 (ep->out_overflow || t)
2270 /* also flush to prevent erratum 0106 trouble */
2271 if (unlikely (ep->out_overflow
2272 || (ep->dev->chiprev == 0x0100
2273 && ep->dev->gadget.speed
2274 == USB_SPEED_FULL))) {
2276 ep->out_overflow = 0;
2279 /* (re)start dma if needed, stop NAKing */
2280 ep->stopped = stopped;
2281 if (!list_empty (&ep->queue))
2284 DEBUG (ep->dev, "%s dma ep_stat %08x ??\n",
2288 /* data packet(s) received (in the fifo, OUT) */
2289 } else if (t & (1 << DATA_PACKET_RECEIVED_INTERRUPT)) {
2290 if (read_fifo (ep, req) && ep->num != 0)
2293 /* data packet(s) transmitted (IN) */
2294 } else if (t & (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)) {
2297 len = req->req.length - req->req.actual;
2298 if (len > ep->ep.maxpacket)
2299 len = ep->ep.maxpacket;
2300 req->req.actual += len;
2302 /* if we wrote it all, we're usually done */
2303 if (req->req.actual == req->req.length) {
2305 /* send zlps until the status stage */
2306 } else if (!req->req.zero || len != ep->ep.maxpacket)
2310 /* there was nothing to do ... */
2311 } else if (mode == 1)
2316 /* stream endpoints often resubmit/unlink in completion */
2319 /* maybe advance queue to next request */
2321 /* NOTE: net2280 could let gadget driver start the
2322 * status stage later. since not all controllers let
2323 * them control that, the api doesn't (yet) allow it.
2329 if (!list_empty (&ep->queue) && !ep->stopped)
2330 req = list_entry (ep->queue.next,
2331 struct net2280_request, queue);
2334 if (req && !ep->is_in)
2335 stop_out_naking (ep);
2339 /* is there a buffer for the next packet?
2340 * for best streaming performance, make sure there is one.
2342 if (req && !ep->stopped) {
2344 /* load IN fifo with next packet (may be zlp) */
2345 if (t & (1 << DATA_PACKET_TRANSMITTED_INTERRUPT))
2346 write_fifo (ep, &req->req);
2350 static struct net2280_ep *
2351 get_ep_by_addr (struct net2280 *dev, u16 wIndex)
2353 struct net2280_ep *ep;
2355 if ((wIndex & USB_ENDPOINT_NUMBER_MASK) == 0)
2356 return &dev->ep [0];
2357 list_for_each_entry (ep, &dev->gadget.ep_list, ep.ep_list) {
2358 u8 bEndpointAddress;
2362 bEndpointAddress = ep->desc->bEndpointAddress;
2363 if ((wIndex ^ bEndpointAddress) & USB_DIR_IN)
2365 if ((wIndex & 0x0f) == (bEndpointAddress & 0x0f))
2371 static void handle_stat0_irqs (struct net2280 *dev, u32 stat)
2373 struct net2280_ep *ep;
2376 /* most of these don't need individual acks */
2377 stat &= ~(1 << INTA_ASSERTED);
2380 // DEBUG (dev, "irqstat0 %04x\n", stat);
2382 /* starting a control request? */
2383 if (unlikely (stat & (1 << SETUP_PACKET_INTERRUPT))) {
2386 struct usb_ctrlrequest r;
2389 struct net2280_request *req;
2391 if (dev->gadget.speed == USB_SPEED_UNKNOWN) {
2392 if (readl (&dev->usb->usbstat) & (1 << HIGH_SPEED))
2393 dev->gadget.speed = USB_SPEED_HIGH;
2395 dev->gadget.speed = USB_SPEED_FULL;
2396 net2280_led_speed (dev, dev->gadget.speed);
2397 DEBUG (dev, "%s speed\n",
2398 (dev->gadget.speed == USB_SPEED_HIGH)
2405 /* make sure any leftover request state is cleared */
2406 stat &= ~(1 << ENDPOINT_0_INTERRUPT);
2407 while (!list_empty (&ep->queue)) {
2408 req = list_entry (ep->queue.next,
2409 struct net2280_request, queue);
2410 done (ep, req, (req->req.actual == req->req.length)
2414 dev->protocol_stall = 0;
2416 if (ep->dev->pdev->device == 0x2280)
2417 tmp = (1 << FIFO_OVERFLOW)
2418 | (1 << FIFO_UNDERFLOW);
2422 writel (tmp | (1 << TIMEOUT)
2423 | (1 << USB_STALL_SENT)
2424 | (1 << USB_IN_NAK_SENT)
2425 | (1 << USB_IN_ACK_RCVD)
2426 | (1 << USB_OUT_PING_NAK_SENT)
2427 | (1 << USB_OUT_ACK_SENT)
2428 | (1 << SHORT_PACKET_OUT_DONE_INTERRUPT)
2429 | (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)
2430 | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
2431 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
2432 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
2433 | (1 << DATA_IN_TOKEN_INTERRUPT)
2434 , &ep->regs->ep_stat);
2435 u.raw [0] = readl (&dev->usb->setup0123);
2436 u.raw [1] = readl (&dev->usb->setup4567);
2438 cpu_to_le32s (&u.raw [0]);
2439 cpu_to_le32s (&u.raw [1]);
2443 #define w_value le16_to_cpup (&u.r.wValue)
2444 #define w_index le16_to_cpup (&u.r.wIndex)
2445 #define w_length le16_to_cpup (&u.r.wLength)
2448 writel (1 << SETUP_PACKET_INTERRUPT, &dev->regs->irqstat0);
2449 stat ^= (1 << SETUP_PACKET_INTERRUPT);
2451 /* watch control traffic at the token level, and force
2452 * synchronization before letting the status stage happen.
2453 * FIXME ignore tokens we'll NAK, until driver responds.
2454 * that'll mean a lot less irqs for some drivers.
2456 ep->is_in = (u.r.bRequestType & USB_DIR_IN) != 0;
2458 scratch = (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
2459 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
2460 | (1 << DATA_IN_TOKEN_INTERRUPT);
2461 stop_out_naking (ep);
2463 scratch = (1 << DATA_PACKET_RECEIVED_INTERRUPT)
2464 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
2465 | (1 << DATA_IN_TOKEN_INTERRUPT);
2466 writel (scratch, &dev->epregs [0].ep_irqenb);
2468 /* we made the hardware handle most lowlevel requests;
2469 * everything else goes uplevel to the gadget code.
2472 switch (u.r.bRequest) {
2473 case USB_REQ_GET_STATUS: {
2474 struct net2280_ep *e;
2477 /* hw handles device and interface status */
2478 if (u.r.bRequestType != (USB_DIR_IN|USB_RECIP_ENDPOINT))
2480 if ((e = get_ep_by_addr (dev, w_index)) == 0
2484 if (readl (&e->regs->ep_rsp)
2485 & (1 << SET_ENDPOINT_HALT))
2486 status = __constant_cpu_to_le32 (1);
2488 status = __constant_cpu_to_le32 (0);
2490 /* don't bother with a request object! */
2491 writel (0, &dev->epregs [0].ep_irqenb);
2492 set_fifo_bytecount (ep, w_length);
2493 writel ((__force u32)status, &dev->epregs [0].ep_data);
2495 VDEBUG (dev, "%s stat %02x\n", ep->ep.name, status);
2496 goto next_endpoints;
2499 case USB_REQ_CLEAR_FEATURE: {
2500 struct net2280_ep *e;
2502 /* hw handles device features */
2503 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
2505 if (w_value != USB_ENDPOINT_HALT
2508 if ((e = get_ep_by_addr (dev, w_index)) == 0)
2512 VDEBUG (dev, "%s clear halt\n", ep->ep.name);
2513 goto next_endpoints;
2516 case USB_REQ_SET_FEATURE: {
2517 struct net2280_ep *e;
2519 /* hw handles device features */
2520 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
2522 if (w_value != USB_ENDPOINT_HALT
2525 if ((e = get_ep_by_addr (dev, w_index)) == 0)
2529 VDEBUG (dev, "%s set halt\n", ep->ep.name);
2530 goto next_endpoints;
2535 VDEBUG (dev, "setup %02x.%02x v%04x i%04x l%04x"
2537 u.r.bRequestType, u.r.bRequest,
2538 w_value, w_index, w_length,
2539 readl (&ep->regs->ep_cfg));
2541 spin_unlock (&dev->lock);
2542 tmp = dev->driver->setup (&dev->gadget, &u.r);
2543 spin_lock (&dev->lock);
2546 /* stall ep0 on error */
2549 VDEBUG (dev, "req %02x.%02x protocol STALL; stat %d\n",
2550 u.r.bRequestType, u.r.bRequest, tmp);
2551 dev->protocol_stall = 1;
2554 /* some in/out token irq should follow; maybe stall then.
2555 * driver must queue a request (even zlp) or halt ep0
2556 * before the host times out.
2565 /* endpoint data irq ? */
2566 scratch = stat & 0x7f;
2568 for (num = 0; scratch; num++) {
2571 /* do this endpoint's FIFO and queue need tending? */
2573 if ((scratch & t) == 0)
2577 ep = &dev->ep [num];
2578 handle_ep_small (ep);
2582 DEBUG (dev, "unhandled irqstat0 %08x\n", stat);
2585 #define DMA_INTERRUPTS ( \
2586 (1 << DMA_D_INTERRUPT) \
2587 | (1 << DMA_C_INTERRUPT) \
2588 | (1 << DMA_B_INTERRUPT) \
2589 | (1 << DMA_A_INTERRUPT))
2590 #define PCI_ERROR_INTERRUPTS ( \
2591 (1 << PCI_MASTER_ABORT_RECEIVED_INTERRUPT) \
2592 | (1 << PCI_TARGET_ABORT_RECEIVED_INTERRUPT) \
2593 | (1 << PCI_RETRY_ABORT_INTERRUPT))
2595 static void handle_stat1_irqs (struct net2280 *dev, u32 stat)
2597 struct net2280_ep *ep;
2598 u32 tmp, num, mask, scratch;
2600 /* after disconnect there's nothing else to do! */
2601 tmp = (1 << VBUS_INTERRUPT) | (1 << ROOT_PORT_RESET_INTERRUPT);
2602 mask = (1 << HIGH_SPEED) | (1 << FULL_SPEED);
2604 /* VBUS disconnect is indicated by VBUS_PIN and VBUS_INTERRUPT set.
2605 * Root Port Reset is indicated by ROOT_PORT_RESET_INTERRRUPT set and
2606 * both HIGH_SPEED and FULL_SPEED clear (as ROOT_PORT_RESET_INTERRUPT
2607 * only indicates a change in the reset state).
2610 writel (tmp, &dev->regs->irqstat1);
2611 if ((((stat & (1 << ROOT_PORT_RESET_INTERRUPT))
2612 && ((readl (&dev->usb->usbstat) & mask)
2614 || ((readl (&dev->usb->usbctl)
2615 & (1 << VBUS_PIN)) == 0)
2616 ) && ( dev->gadget.speed != USB_SPEED_UNKNOWN)) {
2617 DEBUG (dev, "disconnect %s\n",
2618 dev->driver->driver.name);
2619 stop_activity (dev, dev->driver);
2625 /* vBUS can bounce ... one of many reasons to ignore the
2626 * notion of hotplug events on bus connect/disconnect!
2632 /* NOTE: chip stays in PCI D0 state for now, but it could
2633 * enter D1 to save more power
2635 tmp = (1 << SUSPEND_REQUEST_CHANGE_INTERRUPT);
2637 writel (tmp, &dev->regs->irqstat1);
2638 if (stat & (1 << SUSPEND_REQUEST_INTERRUPT)) {
2639 if (dev->driver->suspend)
2640 dev->driver->suspend (&dev->gadget);
2641 if (!enable_suspend)
2642 stat &= ~(1 << SUSPEND_REQUEST_INTERRUPT);
2644 if (dev->driver->resume)
2645 dev->driver->resume (&dev->gadget);
2646 /* at high speed, note erratum 0133 */
2651 /* clear any other status/irqs */
2653 writel (stat, &dev->regs->irqstat1);
2655 /* some status we can just ignore */
2656 if (dev->pdev->device == 0x2280)
2657 stat &= ~((1 << CONTROL_STATUS_INTERRUPT)
2658 | (1 << SUSPEND_REQUEST_INTERRUPT)
2659 | (1 << RESUME_INTERRUPT)
2660 | (1 << SOF_INTERRUPT));
2662 stat &= ~((1 << CONTROL_STATUS_INTERRUPT)
2663 | (1 << RESUME_INTERRUPT)
2664 | (1 << SOF_DOWN_INTERRUPT)
2665 | (1 << SOF_INTERRUPT));
2669 // DEBUG (dev, "irqstat1 %08x\n", stat);
2671 /* DMA status, for ep-{a,b,c,d} */
2672 scratch = stat & DMA_INTERRUPTS;
2673 stat &= ~DMA_INTERRUPTS;
2675 for (num = 0; scratch; num++) {
2676 struct net2280_dma_regs __iomem *dma;
2679 if ((tmp & scratch) == 0)
2683 ep = &dev->ep [num + 1];
2689 /* clear ep's dma status */
2690 tmp = readl (&dma->dmastat);
2691 writel (tmp, &dma->dmastat);
2693 /* chaining should stop on abort, short OUT from fifo,
2694 * or (stat0 codepath) short OUT transfer.
2696 if (!use_dma_chaining) {
2697 if ((tmp & (1 << DMA_TRANSACTION_DONE_INTERRUPT))
2699 DEBUG (ep->dev, "%s no xact done? %08x\n",
2706 /* OUT transfers terminate when the data from the
2707 * host is in our memory. Process whatever's done.
2708 * On this path, we know transfer's last packet wasn't
2709 * less than req->length. NAK_OUT_PACKETS may be set,
2710 * or the FIFO may already be holding new packets.
2712 * IN transfers can linger in the FIFO for a very
2713 * long time ... we ignore that for now, accounting
2714 * precisely (like PIO does) needs per-packet irqs
2716 scan_dma_completions (ep);
2718 /* disable dma on inactive queues; else maybe restart */
2719 if (list_empty (&ep->queue)) {
2720 if (use_dma_chaining)
2723 tmp = readl (&dma->dmactl);
2724 if (!use_dma_chaining
2725 || (tmp & (1 << DMA_ENABLE)) == 0)
2727 else if (ep->is_in && use_dma_chaining) {
2728 struct net2280_request *req;
2731 /* the descriptor at the head of the chain
2732 * may still have VALID_BIT clear; that's
2733 * used to trigger changing DMA_FIFO_VALIDATE
2734 * (affects automagic zlp writes).
2736 req = list_entry (ep->queue.next,
2737 struct net2280_request, queue);
2738 dmacount = req->td->dmacount;
2739 dmacount &= __constant_cpu_to_le32 (
2741 | DMA_BYTE_COUNT_MASK);
2742 if (dmacount && (dmacount & valid_bit) == 0)
2749 /* NOTE: there are other PCI errors we might usefully notice.
2750 * if they appear very often, here's where to try recovering.
2752 if (stat & PCI_ERROR_INTERRUPTS) {
2753 ERROR (dev, "pci dma error; stat %08x\n", stat);
2754 stat &= ~PCI_ERROR_INTERRUPTS;
2755 /* these are fatal errors, but "maybe" they won't
2758 stop_activity (dev, dev->driver);
2764 DEBUG (dev, "unhandled irqstat1 %08x\n", stat);
2767 static irqreturn_t net2280_irq (int irq, void *_dev)
2769 struct net2280 *dev = _dev;
2771 /* shared interrupt, not ours */
2772 if (!(readl(&dev->regs->irqstat0) & (1 << INTA_ASSERTED)))
2775 spin_lock (&dev->lock);
2777 /* handle disconnect, dma, and more */
2778 handle_stat1_irqs (dev, readl (&dev->regs->irqstat1));
2780 /* control requests and PIO */
2781 handle_stat0_irqs (dev, readl (&dev->regs->irqstat0));
2783 spin_unlock (&dev->lock);
2788 /*-------------------------------------------------------------------------*/
2790 static void gadget_release (struct device *_dev)
2792 struct net2280 *dev = dev_get_drvdata (_dev);
2797 /* tear down the binding between this driver and the pci device */
2799 static void net2280_remove (struct pci_dev *pdev)
2801 struct net2280 *dev = pci_get_drvdata (pdev);
2803 BUG_ON(dev->driver);
2805 /* then clean up the resources we allocated during probe() */
2806 net2280_led_shutdown (dev);
2807 if (dev->requests) {
2809 for (i = 1; i < 5; i++) {
2810 if (!dev->ep [i].dummy)
2812 pci_pool_free (dev->requests, dev->ep [i].dummy,
2813 dev->ep [i].td_dma);
2815 pci_pool_destroy (dev->requests);
2818 free_irq (pdev->irq, dev);
2820 iounmap (dev->regs);
2822 release_mem_region (pci_resource_start (pdev, 0),
2823 pci_resource_len (pdev, 0));
2825 pci_disable_device (pdev);
2826 device_unregister (&dev->gadget.dev);
2827 device_remove_file (&pdev->dev, &dev_attr_registers);
2828 pci_set_drvdata (pdev, NULL);
2830 INFO (dev, "unbind\n");
2832 the_controller = NULL;
2835 /* wrap this driver around the specified device, but
2836 * don't respond over USB until a gadget driver binds to us.
2839 static int net2280_probe (struct pci_dev *pdev, const struct pci_device_id *id)
2841 struct net2280 *dev;
2842 unsigned long resource, len;
2843 void __iomem *base = NULL;
2846 /* if you want to support more than one controller in a system,
2847 * usb_gadget_driver_{register,unregister}() must change.
2849 if (the_controller) {
2850 dev_warn (&pdev->dev, "ignoring\n");
2854 /* alloc, and start init */
2855 dev = kzalloc (sizeof *dev, GFP_KERNEL);
2861 pci_set_drvdata (pdev, dev);
2862 spin_lock_init (&dev->lock);
2864 dev->gadget.ops = &net2280_ops;
2865 dev->gadget.is_dualspeed = 1;
2867 /* the "gadget" abstracts/virtualizes the controller */
2868 strcpy (dev->gadget.dev.bus_id, "gadget");
2869 dev->gadget.dev.parent = &pdev->dev;
2870 dev->gadget.dev.dma_mask = pdev->dev.dma_mask;
2871 dev->gadget.dev.release = gadget_release;
2872 dev->gadget.name = driver_name;
2874 /* now all the pci goodies ... */
2875 if (pci_enable_device (pdev) < 0) {
2881 /* BAR 0 holds all the registers
2882 * BAR 1 is 8051 memory; unused here (note erratum 0103)
2883 * BAR 2 is fifo memory; unused here
2885 resource = pci_resource_start (pdev, 0);
2886 len = pci_resource_len (pdev, 0);
2887 if (!request_mem_region (resource, len, driver_name)) {
2888 DEBUG (dev, "controller already in use\n");
2894 /* FIXME provide firmware download interface to put
2895 * 8051 code into the chip, e.g. to turn on PCI PM.
2898 base = ioremap_nocache (resource, len);
2900 DEBUG (dev, "can't map memory\n");
2904 dev->regs = (struct net2280_regs __iomem *) base;
2905 dev->usb = (struct net2280_usb_regs __iomem *) (base + 0x0080);
2906 dev->pci = (struct net2280_pci_regs __iomem *) (base + 0x0100);
2907 dev->dma = (struct net2280_dma_regs __iomem *) (base + 0x0180);
2908 dev->dep = (struct net2280_dep_regs __iomem *) (base + 0x0200);
2909 dev->epregs = (struct net2280_ep_regs __iomem *) (base + 0x0300);
2911 /* put into initial config, link up all endpoints */
2912 writel (0, &dev->usb->usbctl);
2916 /* irq setup after old hardware is cleaned up */
2918 ERROR (dev, "No IRQ. Check PCI setup!\n");
2923 if (request_irq (pdev->irq, net2280_irq, IRQF_SHARED, driver_name, dev)
2925 ERROR (dev, "request interrupt %d failed\n", pdev->irq);
2932 /* NOTE: we know only the 32 LSBs of dma addresses may be nonzero */
2933 dev->requests = pci_pool_create ("requests", pdev,
2934 sizeof (struct net2280_dma),
2935 0 /* no alignment requirements */,
2936 0 /* or page-crossing issues */);
2937 if (!dev->requests) {
2938 DEBUG (dev, "can't get request pool\n");
2942 for (i = 1; i < 5; i++) {
2943 struct net2280_dma *td;
2945 td = pci_pool_alloc (dev->requests, GFP_KERNEL,
2946 &dev->ep [i].td_dma);
2948 DEBUG (dev, "can't get dummy %d\n", i);
2952 td->dmacount = 0; /* not VALID */
2953 td->dmaaddr = __constant_cpu_to_le32 (DMA_ADDR_INVALID);
2954 td->dmadesc = td->dmaaddr;
2955 dev->ep [i].dummy = td;
2958 /* enable lower-overhead pci memory bursts during DMA */
2959 writel ( (1 << DMA_MEMORY_WRITE_AND_INVALIDATE_ENABLE)
2960 // 256 write retries may not be enough...
2961 // | (1 << PCI_RETRY_ABORT_ENABLE)
2962 | (1 << DMA_READ_MULTIPLE_ENABLE)
2963 | (1 << DMA_READ_LINE_ENABLE)
2964 , &dev->pci->pcimstctl);
2965 /* erratum 0115 shouldn't appear: Linux inits PCI_LATENCY_TIMER */
2966 pci_set_master (pdev);
2969 /* ... also flushes any posted pci writes */
2970 dev->chiprev = get_idx_reg (dev->regs, REG_CHIPREV) & 0xffff;
2973 INFO (dev, "%s\n", driver_desc);
2974 INFO (dev, "irq %d, pci mem %p, chip rev %04x\n",
2975 pdev->irq, base, dev->chiprev);
2976 INFO (dev, "version: " DRIVER_VERSION "; dma %s\n",
2978 ? (use_dma_chaining ? "chaining" : "enabled")
2980 the_controller = dev;
2982 retval = device_register (&dev->gadget.dev);
2983 if (retval) goto done;
2984 retval = device_create_file (&pdev->dev, &dev_attr_registers);
2985 if (retval) goto done;
2991 net2280_remove (pdev);
2995 /* make sure the board is quiescent; otherwise it will continue
2996 * generating IRQs across the upcoming reboot.
2999 static void net2280_shutdown (struct pci_dev *pdev)
3001 struct net2280 *dev = pci_get_drvdata (pdev);
3004 writel (0, &dev->regs->pciirqenb0);
3005 writel (0, &dev->regs->pciirqenb1);
3007 /* disable the pullup so the host will think we're gone */
3008 writel (0, &dev->usb->usbctl);
3012 /*-------------------------------------------------------------------------*/
3014 static const struct pci_device_id pci_ids [] = { {
3015 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
3019 .subvendor = PCI_ANY_ID,
3020 .subdevice = PCI_ANY_ID,
3022 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
3026 .subvendor = PCI_ANY_ID,
3027 .subdevice = PCI_ANY_ID,
3029 }, { /* end: all zeroes */ }
3031 MODULE_DEVICE_TABLE (pci, pci_ids);
3033 /* pci driver glue; this is a "new style" PCI driver module */
3034 static struct pci_driver net2280_pci_driver = {
3035 .name = (char *) driver_name,
3036 .id_table = pci_ids,
3038 .probe = net2280_probe,
3039 .remove = net2280_remove,
3040 .shutdown = net2280_shutdown,
3042 /* FIXME add power management support */
3045 MODULE_DESCRIPTION (DRIVER_DESC);
3046 MODULE_AUTHOR ("David Brownell");
3047 MODULE_LICENSE ("GPL");
3049 static int __init init (void)
3052 use_dma_chaining = 0;
3053 return pci_register_driver (&net2280_pci_driver);
3057 static void __exit cleanup (void)
3059 pci_unregister_driver (&net2280_pci_driver);
3061 module_exit (cleanup);