b43/b43legacy: use RFKILL_STATE_UNBLOCKED instead of RFKILL_STATE_ON
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / usb / gadget / net2280.c
blobe01862300169772d714a6ab24a4db9938825fd9d
1 /*
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
30 * with 2282 chip
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>
68 #include <asm/io.h>
69 #include <asm/irq.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 [] = {
88 ep0name,
89 "ep-a", "ep-b", "ep-c", "ep-d",
90 "ep-e", "ep-f",
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";
139 return "control";
141 #endif
143 #include "net2280.h"
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 /*-------------------------------------------------------------------------*/
150 static int
151 net2280_enable (struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
153 struct net2280 *dev;
154 struct net2280_ep *ep;
155 u32 max, tmp;
156 unsigned long flags;
158 ep = container_of (_ep, struct net2280_ep, ep);
159 if (!_ep || !desc || ep->desc || _ep->name == ep0name
160 || desc->bDescriptorType != USB_DT_ENDPOINT)
161 return -EINVAL;
162 dev = ep->dev;
163 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
164 return -ESHUTDOWN;
166 /* erratum 0119 workaround ties up an endpoint number */
167 if ((desc->bEndpointAddress & 0x0f) == EP_DONTUSE)
168 return -EDOM;
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)
173 return -ERANGE;
175 spin_lock_irqsave (&dev->lock, flags);
176 _ep->maxpacket = max & 0x7ff;
177 ep->desc = desc;
179 /* ep_reset() has already been called */
180 ep->stopped = 0;
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);
192 ep->dma = NULL;
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),
204 &ep->regs->ep_rsp);
205 } else if (tmp == USB_ENDPOINT_XFER_BULK) {
206 /* catch some particularly blatant driver bugs */
207 if ((dev->gadget.speed == USB_SPEED_HIGH
208 && max != 512)
209 || (dev->gadget.speed == USB_SPEED_FULL
210 && max > 64)) {
211 spin_unlock_irqrestore (&dev->lock, flags);
212 return -ERANGE;
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;
220 wmb ();
222 /* for OUT transfers, block the rx fifo until a read is posted */
223 ep->is_in = (tmp & USB_DIR_IN) != 0;
224 if (!ep->is_in)
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);
236 /* enable irqs */
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);
272 return 0;
275 static int handshake (u32 __iomem *ptr, u32 mask, u32 done, int usec)
277 u32 result;
279 do {
280 result = readl (ptr);
281 if (result == ~(u32)0) /* "device unplugged" */
282 return -ENODEV;
283 result &= mask;
284 if (result == done)
285 return 0;
286 udelay (1);
287 usec--;
288 } while (usec > 0);
289 return -ETIMEDOUT;
292 static const struct usb_ep_ops net2280_ep_ops;
294 static void ep_reset (struct net2280_regs __iomem *regs, struct net2280_ep *ep)
296 u32 tmp;
298 ep->desc = NULL;
299 INIT_LIST_HEAD (&ep->queue);
301 ep->ep.maxpacket = ~0;
302 ep->ep.ops = &net2280_ep_ops;
304 /* disable the dma, irqs, endpoint... */
305 if (ep->dma) {
306 writel (0, &ep->dma->dmactl);
307 writel ( (1 << DMA_SCATTER_GATHER_DONE_INTERRUPT)
308 | (1 << DMA_TRANSACTION_DONE_INTERRUPT)
309 | (1 << DMA_ABORT)
310 , &ep->dma->dmastat);
312 tmp = readl (&regs->pciirqenb0);
313 tmp &= ~(1 << ep->num);
314 writel (tmp, &regs->pciirqenb0);
315 } else {
316 tmp = readl (&regs->pciirqenb1);
317 tmp &= ~(1 << (8 + ep->num)); /* completion */
318 writel (tmp, &regs->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);
330 } else {
331 /* added for 2282 */
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);
338 if (ep->num != 0) {
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);
348 else
349 tmp = 0;
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)
357 | (1 << FIFO_FLUSH)
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;
374 unsigned long flags;
376 ep = container_of (_ep, struct net2280_ep, ep);
377 if (!_ep || !ep->desc || _ep->name == ep0name)
378 return -EINVAL;
380 spin_lock_irqsave (&ep->dev->lock, flags);
381 nuke (ep);
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);
394 return 0;
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;
405 if (!_ep)
406 return NULL;
407 ep = container_of (_ep, struct net2280_ep, ep);
409 req = kzalloc(sizeof(*req), gfp_flags);
410 if (!req)
411 return NULL;
413 req->req.dma = DMA_ADDR_INVALID;
414 INIT_LIST_HEAD (&req->queue);
416 /* this dma descriptor may be swapped with the previous dummy */
417 if (ep->dma) {
418 struct net2280_dma *td;
420 td = pci_pool_alloc (ep->dev->requests, gfp_flags,
421 &req->td_dma);
422 if (!td) {
423 kfree (req);
424 return NULL;
426 td->dmacount = 0; /* not VALID */
427 td->dmaaddr = __constant_cpu_to_le32 (DMA_ADDR_INVALID);
428 td->dmadesc = td->dmaaddr;
429 req->td = td;
431 return &req->req;
434 static void
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);
441 if (!_ep || !_req)
442 return;
444 req = container_of (_req, struct net2280_request, req);
445 WARN_ON (!list_empty (&req->queue));
446 if (req->td)
447 pci_pool_free (ep->dev->requests, req->td, req->td_dma);
448 kfree (req);
451 /*-------------------------------------------------------------------------*/
453 /* load a packet into the fifo we use for usb IN transfers.
454 * works for all endpoints.
456 * NOTE: pio with ep-a..ep-d could stuff multiple packets into the fifo
457 * at a time, but this code is simpler because it knows it only writes
458 * one packet. ep-a..ep-d should use dma instead.
460 static void
461 write_fifo (struct net2280_ep *ep, struct usb_request *req)
463 struct net2280_ep_regs __iomem *regs = ep->regs;
464 u8 *buf;
465 u32 tmp;
466 unsigned count, total;
468 /* INVARIANT: fifo is currently empty. (testable) */
470 if (req) {
471 buf = req->buf + req->actual;
472 prefetch (buf);
473 total = req->length - req->actual;
474 } else {
475 total = 0;
476 buf = NULL;
479 /* write just one packet at a time */
480 count = ep->ep.maxpacket;
481 if (count > total) /* min() cannot be used on a bitfield */
482 count = total;
484 VDEBUG (ep->dev, "write %s fifo (IN) %d bytes%s req %p\n",
485 ep->ep.name, count,
486 (count != ep->ep.maxpacket) ? " (short)" : "",
487 req);
488 while (count >= 4) {
489 /* NOTE be careful if you try to align these. fifo lines
490 * should normally be full (4 bytes) and successive partial
491 * lines are ok only in certain cases.
493 tmp = get_unaligned ((u32 *)buf);
494 cpu_to_le32s (&tmp);
495 writel (tmp, &regs->ep_data);
496 buf += 4;
497 count -= 4;
500 /* last fifo entry is "short" unless we wrote a full packet.
501 * also explicitly validate last word in (periodic) transfers
502 * when maxpacket is not a multiple of 4 bytes.
504 if (count || total < ep->ep.maxpacket) {
505 tmp = count ? get_unaligned ((u32 *)buf) : count;
506 cpu_to_le32s (&tmp);
507 set_fifo_bytecount (ep, count & 0x03);
508 writel (tmp, &regs->ep_data);
511 /* pci writes may still be posted */
514 /* work around erratum 0106: PCI and USB race over the OUT fifo.
515 * caller guarantees chiprev 0100, out endpoint is NAKing, and
516 * there's no real data in the fifo.
518 * NOTE: also used in cases where that erratum doesn't apply:
519 * where the host wrote "too much" data to us.
521 static void out_flush (struct net2280_ep *ep)
523 u32 __iomem *statp;
524 u32 tmp;
526 ASSERT_OUT_NAKING (ep);
528 statp = &ep->regs->ep_stat;
529 writel ( (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
530 | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
531 , statp);
532 writel ((1 << FIFO_FLUSH), statp);
533 mb ();
534 tmp = readl (statp);
535 if (tmp & (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
536 /* high speed did bulk NYET; fifo isn't filling */
537 && ep->dev->gadget.speed == USB_SPEED_FULL) {
538 unsigned usec;
540 usec = 50; /* 64 byte bulk/interrupt */
541 handshake (statp, (1 << USB_OUT_PING_NAK_SENT),
542 (1 << USB_OUT_PING_NAK_SENT), usec);
543 /* NAK done; now CLEAR_NAK_OUT_PACKETS is safe */
547 /* unload packet(s) from the fifo we use for usb OUT transfers.
548 * returns true iff the request completed, because of short packet
549 * or the request buffer having filled with full packets.
551 * for ep-a..ep-d this will read multiple packets out when they
552 * have been accepted.
554 static int
555 read_fifo (struct net2280_ep *ep, struct net2280_request *req)
557 struct net2280_ep_regs __iomem *regs = ep->regs;
558 u8 *buf = req->req.buf + req->req.actual;
559 unsigned count, tmp, is_short;
560 unsigned cleanup = 0, prevent = 0;
562 /* erratum 0106 ... packets coming in during fifo reads might
563 * be incompletely rejected. not all cases have workarounds.
565 if (ep->dev->chiprev == 0x0100
566 && ep->dev->gadget.speed == USB_SPEED_FULL) {
567 udelay (1);
568 tmp = readl (&ep->regs->ep_stat);
569 if ((tmp & (1 << NAK_OUT_PACKETS)))
570 cleanup = 1;
571 else if ((tmp & (1 << FIFO_FULL))) {
572 start_out_naking (ep);
573 prevent = 1;
575 /* else: hope we don't see the problem */
578 /* never overflow the rx buffer. the fifo reads packets until
579 * it sees a short one; we might not be ready for them all.
581 prefetchw (buf);
582 count = readl (&regs->ep_avail);
583 if (unlikely (count == 0)) {
584 udelay (1);
585 tmp = readl (&ep->regs->ep_stat);
586 count = readl (&regs->ep_avail);
587 /* handled that data already? */
588 if (count == 0 && (tmp & (1 << NAK_OUT_PACKETS)) == 0)
589 return 0;
592 tmp = req->req.length - req->req.actual;
593 if (count > tmp) {
594 /* as with DMA, data overflow gets flushed */
595 if ((tmp % ep->ep.maxpacket) != 0) {
596 ERROR (ep->dev,
597 "%s out fifo %d bytes, expected %d\n",
598 ep->ep.name, count, tmp);
599 req->req.status = -EOVERFLOW;
600 cleanup = 1;
601 /* NAK_OUT_PACKETS will be set, so flushing is safe;
602 * the next read will start with the next packet
604 } /* else it's a ZLP, no worries */
605 count = tmp;
607 req->req.actual += count;
609 is_short = (count == 0) || ((count % ep->ep.maxpacket) != 0);
611 VDEBUG (ep->dev, "read %s fifo (OUT) %d bytes%s%s%s req %p %d/%d\n",
612 ep->ep.name, count, is_short ? " (short)" : "",
613 cleanup ? " flush" : "", prevent ? " nak" : "",
614 req, req->req.actual, req->req.length);
616 while (count >= 4) {
617 tmp = readl (&regs->ep_data);
618 cpu_to_le32s (&tmp);
619 put_unaligned (tmp, (u32 *)buf);
620 buf += 4;
621 count -= 4;
623 if (count) {
624 tmp = readl (&regs->ep_data);
625 /* LE conversion is implicit here: */
626 do {
627 *buf++ = (u8) tmp;
628 tmp >>= 8;
629 } while (--count);
631 if (cleanup)
632 out_flush (ep);
633 if (prevent) {
634 writel ((1 << CLEAR_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
635 (void) readl (&ep->regs->ep_rsp);
638 return is_short || ((req->req.actual == req->req.length)
639 && !req->req.zero);
642 /* fill out dma descriptor to match a given request */
643 static void
644 fill_dma_desc (struct net2280_ep *ep, struct net2280_request *req, int valid)
646 struct net2280_dma *td = req->td;
647 u32 dmacount = req->req.length;
649 /* don't let DMA continue after a short OUT packet,
650 * so overruns can't affect the next transfer.
651 * in case of overruns on max-size packets, we can't
652 * stop the fifo from filling but we can flush it.
654 if (ep->is_in)
655 dmacount |= (1 << DMA_DIRECTION);
656 if ((!ep->is_in && (dmacount % ep->ep.maxpacket) != 0)
657 || ep->dev->pdev->device != 0x2280)
658 dmacount |= (1 << END_OF_CHAIN);
660 req->valid = valid;
661 if (valid)
662 dmacount |= (1 << VALID_BIT);
663 if (likely(!req->req.no_interrupt || !use_dma_chaining))
664 dmacount |= (1 << DMA_DONE_INTERRUPT_ENABLE);
666 /* td->dmadesc = previously set by caller */
667 td->dmaaddr = cpu_to_le32 (req->req.dma);
669 /* 2280 may be polling VALID_BIT through ep->dma->dmadesc */
670 wmb ();
671 td->dmacount = cpu_to_le32p (&dmacount);
674 static const u32 dmactl_default =
675 (1 << DMA_SCATTER_GATHER_DONE_INTERRUPT)
676 | (1 << DMA_CLEAR_COUNT_ENABLE)
677 /* erratum 0116 workaround part 1 (use POLLING) */
678 | (POLL_100_USEC << DESCRIPTOR_POLLING_RATE)
679 | (1 << DMA_VALID_BIT_POLLING_ENABLE)
680 | (1 << DMA_VALID_BIT_ENABLE)
681 | (1 << DMA_SCATTER_GATHER_ENABLE)
682 /* erratum 0116 workaround part 2 (no AUTOSTART) */
683 | (1 << DMA_ENABLE);
685 static inline void spin_stop_dma (struct net2280_dma_regs __iomem *dma)
687 handshake (&dma->dmactl, (1 << DMA_ENABLE), 0, 50);
690 static inline void stop_dma (struct net2280_dma_regs __iomem *dma)
692 writel (readl (&dma->dmactl) & ~(1 << DMA_ENABLE), &dma->dmactl);
693 spin_stop_dma (dma);
696 static void start_queue (struct net2280_ep *ep, u32 dmactl, u32 td_dma)
698 struct net2280_dma_regs __iomem *dma = ep->dma;
699 unsigned int tmp = (1 << VALID_BIT) | (ep->is_in << DMA_DIRECTION);
701 if (ep->dev->pdev->device != 0x2280)
702 tmp |= (1 << END_OF_CHAIN);
704 writel (tmp, &dma->dmacount);
705 writel (readl (&dma->dmastat), &dma->dmastat);
707 writel (td_dma, &dma->dmadesc);
708 writel (dmactl, &dma->dmactl);
710 /* erratum 0116 workaround part 3: pci arbiter away from net2280 */
711 (void) readl (&ep->dev->pci->pcimstctl);
713 writel ((1 << DMA_START), &dma->dmastat);
715 if (!ep->is_in)
716 stop_out_naking (ep);
719 static void start_dma (struct net2280_ep *ep, struct net2280_request *req)
721 u32 tmp;
722 struct net2280_dma_regs __iomem *dma = ep->dma;
724 /* FIXME can't use DMA for ZLPs */
726 /* on this path we "know" there's no dma active (yet) */
727 WARN_ON (readl (&dma->dmactl) & (1 << DMA_ENABLE));
728 writel (0, &ep->dma->dmactl);
730 /* previous OUT packet might have been short */
731 if (!ep->is_in && ((tmp = readl (&ep->regs->ep_stat))
732 & (1 << NAK_OUT_PACKETS)) != 0) {
733 writel ((1 << SHORT_PACKET_TRANSFERRED_INTERRUPT),
734 &ep->regs->ep_stat);
736 tmp = readl (&ep->regs->ep_avail);
737 if (tmp) {
738 writel (readl (&dma->dmastat), &dma->dmastat);
740 /* transfer all/some fifo data */
741 writel (req->req.dma, &dma->dmaaddr);
742 tmp = min (tmp, req->req.length);
744 /* dma irq, faking scatterlist status */
745 req->td->dmacount = cpu_to_le32 (req->req.length - tmp);
746 writel ((1 << DMA_DONE_INTERRUPT_ENABLE)
747 | tmp, &dma->dmacount);
748 req->td->dmadesc = 0;
749 req->valid = 1;
751 writel ((1 << DMA_ENABLE), &dma->dmactl);
752 writel ((1 << DMA_START), &dma->dmastat);
753 return;
757 tmp = dmactl_default;
759 /* force packet boundaries between dma requests, but prevent the
760 * controller from automagically writing a last "short" packet
761 * (zero length) unless the driver explicitly said to do that.
763 if (ep->is_in) {
764 if (likely ((req->req.length % ep->ep.maxpacket) != 0
765 || req->req.zero)) {
766 tmp |= (1 << DMA_FIFO_VALIDATE);
767 ep->in_fifo_validate = 1;
768 } else
769 ep->in_fifo_validate = 0;
772 /* init req->td, pointing to the current dummy */
773 req->td->dmadesc = cpu_to_le32 (ep->td_dma);
774 fill_dma_desc (ep, req, 1);
776 if (!use_dma_chaining)
777 req->td->dmacount |= __constant_cpu_to_le32 (1 << END_OF_CHAIN);
779 start_queue (ep, tmp, req->td_dma);
782 static inline void
783 queue_dma (struct net2280_ep *ep, struct net2280_request *req, int valid)
785 struct net2280_dma *end;
786 dma_addr_t tmp;
788 /* swap new dummy for old, link; fill and maybe activate */
789 end = ep->dummy;
790 ep->dummy = req->td;
791 req->td = end;
793 tmp = ep->td_dma;
794 ep->td_dma = req->td_dma;
795 req->td_dma = tmp;
797 end->dmadesc = cpu_to_le32 (ep->td_dma);
799 fill_dma_desc (ep, req, valid);
802 static void
803 done (struct net2280_ep *ep, struct net2280_request *req, int status)
805 struct net2280 *dev;
806 unsigned stopped = ep->stopped;
808 list_del_init (&req->queue);
810 if (req->req.status == -EINPROGRESS)
811 req->req.status = status;
812 else
813 status = req->req.status;
815 dev = ep->dev;
816 if (req->mapped) {
817 pci_unmap_single (dev->pdev, req->req.dma, req->req.length,
818 ep->is_in ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
819 req->req.dma = DMA_ADDR_INVALID;
820 req->mapped = 0;
823 if (status && status != -ESHUTDOWN)
824 VDEBUG (dev, "complete %s req %p stat %d len %u/%u\n",
825 ep->ep.name, &req->req, status,
826 req->req.actual, req->req.length);
828 /* don't modify queue heads during completion callback */
829 ep->stopped = 1;
830 spin_unlock (&dev->lock);
831 req->req.complete (&ep->ep, &req->req);
832 spin_lock (&dev->lock);
833 ep->stopped = stopped;
836 /*-------------------------------------------------------------------------*/
838 static int
839 net2280_queue (struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
841 struct net2280_request *req;
842 struct net2280_ep *ep;
843 struct net2280 *dev;
844 unsigned long flags;
846 /* we always require a cpu-view buffer, so that we can
847 * always use pio (as fallback or whatever).
849 req = container_of (_req, struct net2280_request, req);
850 if (!_req || !_req->complete || !_req->buf
851 || !list_empty (&req->queue))
852 return -EINVAL;
853 if (_req->length > (~0 & DMA_BYTE_COUNT_MASK))
854 return -EDOM;
855 ep = container_of (_ep, struct net2280_ep, ep);
856 if (!_ep || (!ep->desc && ep->num != 0))
857 return -EINVAL;
858 dev = ep->dev;
859 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
860 return -ESHUTDOWN;
862 /* FIXME implement PIO fallback for ZLPs with DMA */
863 if (ep->dma && _req->length == 0)
864 return -EOPNOTSUPP;
866 /* set up dma mapping in case the caller didn't */
867 if (ep->dma && _req->dma == DMA_ADDR_INVALID) {
868 _req->dma = pci_map_single (dev->pdev, _req->buf, _req->length,
869 ep->is_in ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
870 req->mapped = 1;
873 #if 0
874 VDEBUG (dev, "%s queue req %p, len %d buf %p\n",
875 _ep->name, _req, _req->length, _req->buf);
876 #endif
878 spin_lock_irqsave (&dev->lock, flags);
880 _req->status = -EINPROGRESS;
881 _req->actual = 0;
883 /* kickstart this i/o queue? */
884 if (list_empty (&ep->queue) && !ep->stopped) {
885 /* use DMA if the endpoint supports it, else pio */
886 if (ep->dma)
887 start_dma (ep, req);
888 else {
889 /* maybe there's no control data, just status ack */
890 if (ep->num == 0 && _req->length == 0) {
891 allow_status (ep);
892 done (ep, req, 0);
893 VDEBUG (dev, "%s status ack\n", ep->ep.name);
894 goto done;
897 /* PIO ... stuff the fifo, or unblock it. */
898 if (ep->is_in)
899 write_fifo (ep, _req);
900 else if (list_empty (&ep->queue)) {
901 u32 s;
903 /* OUT FIFO might have packet(s) buffered */
904 s = readl (&ep->regs->ep_stat);
905 if ((s & (1 << FIFO_EMPTY)) == 0) {
906 /* note: _req->short_not_ok is
907 * ignored here since PIO _always_
908 * stops queue advance here, and
909 * _req->status doesn't change for
910 * short reads (only _req->actual)
912 if (read_fifo (ep, req)) {
913 done (ep, req, 0);
914 if (ep->num == 0)
915 allow_status (ep);
916 /* don't queue it */
917 req = NULL;
918 } else
919 s = readl (&ep->regs->ep_stat);
922 /* don't NAK, let the fifo fill */
923 if (req && (s & (1 << NAK_OUT_PACKETS)))
924 writel ((1 << CLEAR_NAK_OUT_PACKETS),
925 &ep->regs->ep_rsp);
929 } else if (ep->dma) {
930 int valid = 1;
932 if (ep->is_in) {
933 int expect;
935 /* preventing magic zlps is per-engine state, not
936 * per-transfer; irq logic must recover hiccups.
938 expect = likely (req->req.zero
939 || (req->req.length % ep->ep.maxpacket) != 0);
940 if (expect != ep->in_fifo_validate)
941 valid = 0;
943 queue_dma (ep, req, valid);
945 } /* else the irq handler advances the queue. */
947 ep->responded = 1;
948 if (req)
949 list_add_tail (&req->queue, &ep->queue);
950 done:
951 spin_unlock_irqrestore (&dev->lock, flags);
953 /* pci writes may still be posted */
954 return 0;
957 static inline void
958 dma_done (
959 struct net2280_ep *ep,
960 struct net2280_request *req,
961 u32 dmacount,
962 int status
965 req->req.actual = req->req.length - (DMA_BYTE_COUNT_MASK & dmacount);
966 done (ep, req, status);
969 static void restart_dma (struct net2280_ep *ep);
971 static void scan_dma_completions (struct net2280_ep *ep)
973 /* only look at descriptors that were "naturally" retired,
974 * so fifo and list head state won't matter
976 while (!list_empty (&ep->queue)) {
977 struct net2280_request *req;
978 u32 tmp;
980 req = list_entry (ep->queue.next,
981 struct net2280_request, queue);
982 if (!req->valid)
983 break;
984 rmb ();
985 tmp = le32_to_cpup (&req->td->dmacount);
986 if ((tmp & (1 << VALID_BIT)) != 0)
987 break;
989 /* SHORT_PACKET_TRANSFERRED_INTERRUPT handles "usb-short"
990 * cases where DMA must be aborted; this code handles
991 * all non-abort DMA completions.
993 if (unlikely (req->td->dmadesc == 0)) {
994 /* paranoia */
995 tmp = readl (&ep->dma->dmacount);
996 if (tmp & DMA_BYTE_COUNT_MASK)
997 break;
998 /* single transfer mode */
999 dma_done (ep, req, tmp, 0);
1000 break;
1001 } else if (!ep->is_in
1002 && (req->req.length % ep->ep.maxpacket) != 0) {
1003 tmp = readl (&ep->regs->ep_stat);
1005 /* AVOID TROUBLE HERE by not issuing short reads from
1006 * your gadget driver. That helps avoids errata 0121,
1007 * 0122, and 0124; not all cases trigger the warning.
1009 if ((tmp & (1 << NAK_OUT_PACKETS)) == 0) {
1010 WARN (ep->dev, "%s lost packet sync!\n",
1011 ep->ep.name);
1012 req->req.status = -EOVERFLOW;
1013 } else if ((tmp = readl (&ep->regs->ep_avail)) != 0) {
1014 /* fifo gets flushed later */
1015 ep->out_overflow = 1;
1016 DEBUG (ep->dev, "%s dma, discard %d len %d\n",
1017 ep->ep.name, tmp,
1018 req->req.length);
1019 req->req.status = -EOVERFLOW;
1022 dma_done (ep, req, tmp, 0);
1026 static void restart_dma (struct net2280_ep *ep)
1028 struct net2280_request *req;
1029 u32 dmactl = dmactl_default;
1031 if (ep->stopped)
1032 return;
1033 req = list_entry (ep->queue.next, struct net2280_request, queue);
1035 if (!use_dma_chaining) {
1036 start_dma (ep, req);
1037 return;
1040 /* the 2280 will be processing the queue unless queue hiccups after
1041 * the previous transfer:
1042 * IN: wanted automagic zlp, head doesn't (or vice versa)
1043 * DMA_FIFO_VALIDATE doesn't init from dma descriptors.
1044 * OUT: was "usb-short", we must restart.
1046 if (ep->is_in && !req->valid) {
1047 struct net2280_request *entry, *prev = NULL;
1048 int reqmode, done = 0;
1050 DEBUG (ep->dev, "%s dma hiccup td %p\n", ep->ep.name, req->td);
1051 ep->in_fifo_validate = likely (req->req.zero
1052 || (req->req.length % ep->ep.maxpacket) != 0);
1053 if (ep->in_fifo_validate)
1054 dmactl |= (1 << DMA_FIFO_VALIDATE);
1055 list_for_each_entry (entry, &ep->queue, queue) {
1056 __le32 dmacount;
1058 if (entry == req)
1059 continue;
1060 dmacount = entry->td->dmacount;
1061 if (!done) {
1062 reqmode = likely (entry->req.zero
1063 || (entry->req.length
1064 % ep->ep.maxpacket) != 0);
1065 if (reqmode == ep->in_fifo_validate) {
1066 entry->valid = 1;
1067 dmacount |= valid_bit;
1068 entry->td->dmacount = dmacount;
1069 prev = entry;
1070 continue;
1071 } else {
1072 /* force a hiccup */
1073 prev->td->dmacount |= dma_done_ie;
1074 done = 1;
1078 /* walk the rest of the queue so unlinks behave */
1079 entry->valid = 0;
1080 dmacount &= ~valid_bit;
1081 entry->td->dmacount = dmacount;
1082 prev = entry;
1086 writel (0, &ep->dma->dmactl);
1087 start_queue (ep, dmactl, req->td_dma);
1090 static void abort_dma (struct net2280_ep *ep)
1092 /* abort the current transfer */
1093 if (likely (!list_empty (&ep->queue))) {
1094 /* FIXME work around errata 0121, 0122, 0124 */
1095 writel ((1 << DMA_ABORT), &ep->dma->dmastat);
1096 spin_stop_dma (ep->dma);
1097 } else
1098 stop_dma (ep->dma);
1099 scan_dma_completions (ep);
1102 /* dequeue ALL requests */
1103 static void nuke (struct net2280_ep *ep)
1105 struct net2280_request *req;
1107 /* called with spinlock held */
1108 ep->stopped = 1;
1109 if (ep->dma)
1110 abort_dma (ep);
1111 while (!list_empty (&ep->queue)) {
1112 req = list_entry (ep->queue.next,
1113 struct net2280_request,
1114 queue);
1115 done (ep, req, -ESHUTDOWN);
1119 /* dequeue JUST ONE request */
1120 static int net2280_dequeue (struct usb_ep *_ep, struct usb_request *_req)
1122 struct net2280_ep *ep;
1123 struct net2280_request *req;
1124 unsigned long flags;
1125 u32 dmactl;
1126 int stopped;
1128 ep = container_of (_ep, struct net2280_ep, ep);
1129 if (!_ep || (!ep->desc && ep->num != 0) || !_req)
1130 return -EINVAL;
1132 spin_lock_irqsave (&ep->dev->lock, flags);
1133 stopped = ep->stopped;
1135 /* quiesce dma while we patch the queue */
1136 dmactl = 0;
1137 ep->stopped = 1;
1138 if (ep->dma) {
1139 dmactl = readl (&ep->dma->dmactl);
1140 /* WARNING erratum 0127 may kick in ... */
1141 stop_dma (ep->dma);
1142 scan_dma_completions (ep);
1145 /* make sure it's still queued on this endpoint */
1146 list_for_each_entry (req, &ep->queue, queue) {
1147 if (&req->req == _req)
1148 break;
1150 if (&req->req != _req) {
1151 spin_unlock_irqrestore (&ep->dev->lock, flags);
1152 return -EINVAL;
1155 /* queue head may be partially complete. */
1156 if (ep->queue.next == &req->queue) {
1157 if (ep->dma) {
1158 DEBUG (ep->dev, "unlink (%s) dma\n", _ep->name);
1159 _req->status = -ECONNRESET;
1160 abort_dma (ep);
1161 if (likely (ep->queue.next == &req->queue)) {
1162 // NOTE: misreports single-transfer mode
1163 req->td->dmacount = 0; /* invalidate */
1164 dma_done (ep, req,
1165 readl (&ep->dma->dmacount),
1166 -ECONNRESET);
1168 } else {
1169 DEBUG (ep->dev, "unlink (%s) pio\n", _ep->name);
1170 done (ep, req, -ECONNRESET);
1172 req = NULL;
1174 /* patch up hardware chaining data */
1175 } else if (ep->dma && use_dma_chaining) {
1176 if (req->queue.prev == ep->queue.next) {
1177 writel (le32_to_cpu (req->td->dmadesc),
1178 &ep->dma->dmadesc);
1179 if (req->td->dmacount & dma_done_ie)
1180 writel (readl (&ep->dma->dmacount)
1181 | le32_to_cpu(dma_done_ie),
1182 &ep->dma->dmacount);
1183 } else {
1184 struct net2280_request *prev;
1186 prev = list_entry (req->queue.prev,
1187 struct net2280_request, queue);
1188 prev->td->dmadesc = req->td->dmadesc;
1189 if (req->td->dmacount & dma_done_ie)
1190 prev->td->dmacount |= dma_done_ie;
1194 if (req)
1195 done (ep, req, -ECONNRESET);
1196 ep->stopped = stopped;
1198 if (ep->dma) {
1199 /* turn off dma on inactive queues */
1200 if (list_empty (&ep->queue))
1201 stop_dma (ep->dma);
1202 else if (!ep->stopped) {
1203 /* resume current request, or start new one */
1204 if (req)
1205 writel (dmactl, &ep->dma->dmactl);
1206 else
1207 start_dma (ep, list_entry (ep->queue.next,
1208 struct net2280_request, queue));
1212 spin_unlock_irqrestore (&ep->dev->lock, flags);
1213 return 0;
1216 /*-------------------------------------------------------------------------*/
1218 static int net2280_fifo_status (struct usb_ep *_ep);
1220 static int
1221 net2280_set_halt (struct usb_ep *_ep, int value)
1223 struct net2280_ep *ep;
1224 unsigned long flags;
1225 int retval = 0;
1227 ep = container_of (_ep, struct net2280_ep, ep);
1228 if (!_ep || (!ep->desc && ep->num != 0))
1229 return -EINVAL;
1230 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1231 return -ESHUTDOWN;
1232 if (ep->desc /* not ep0 */ && (ep->desc->bmAttributes & 0x03)
1233 == USB_ENDPOINT_XFER_ISOC)
1234 return -EINVAL;
1236 spin_lock_irqsave (&ep->dev->lock, flags);
1237 if (!list_empty (&ep->queue))
1238 retval = -EAGAIN;
1239 else if (ep->is_in && value && net2280_fifo_status (_ep) != 0)
1240 retval = -EAGAIN;
1241 else {
1242 VDEBUG (ep->dev, "%s %s halt\n", _ep->name,
1243 value ? "set" : "clear");
1244 /* set/clear, then synch memory views with the device */
1245 if (value) {
1246 if (ep->num == 0)
1247 ep->dev->protocol_stall = 1;
1248 else
1249 set_halt (ep);
1250 } else
1251 clear_halt (ep);
1252 (void) readl (&ep->regs->ep_rsp);
1254 spin_unlock_irqrestore (&ep->dev->lock, flags);
1256 return retval;
1259 static int
1260 net2280_fifo_status (struct usb_ep *_ep)
1262 struct net2280_ep *ep;
1263 u32 avail;
1265 ep = container_of (_ep, struct net2280_ep, ep);
1266 if (!_ep || (!ep->desc && ep->num != 0))
1267 return -ENODEV;
1268 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1269 return -ESHUTDOWN;
1271 avail = readl (&ep->regs->ep_avail) & ((1 << 12) - 1);
1272 if (avail > ep->fifo_size)
1273 return -EOVERFLOW;
1274 if (ep->is_in)
1275 avail = ep->fifo_size - avail;
1276 return avail;
1279 static void
1280 net2280_fifo_flush (struct usb_ep *_ep)
1282 struct net2280_ep *ep;
1284 ep = container_of (_ep, struct net2280_ep, ep);
1285 if (!_ep || (!ep->desc && ep->num != 0))
1286 return;
1287 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1288 return;
1290 writel ((1 << FIFO_FLUSH), &ep->regs->ep_stat);
1291 (void) readl (&ep->regs->ep_rsp);
1294 static const struct usb_ep_ops net2280_ep_ops = {
1295 .enable = net2280_enable,
1296 .disable = net2280_disable,
1298 .alloc_request = net2280_alloc_request,
1299 .free_request = net2280_free_request,
1301 .queue = net2280_queue,
1302 .dequeue = net2280_dequeue,
1304 .set_halt = net2280_set_halt,
1305 .fifo_status = net2280_fifo_status,
1306 .fifo_flush = net2280_fifo_flush,
1309 /*-------------------------------------------------------------------------*/
1311 static int net2280_get_frame (struct usb_gadget *_gadget)
1313 struct net2280 *dev;
1314 unsigned long flags;
1315 u16 retval;
1317 if (!_gadget)
1318 return -ENODEV;
1319 dev = container_of (_gadget, struct net2280, gadget);
1320 spin_lock_irqsave (&dev->lock, flags);
1321 retval = get_idx_reg (dev->regs, REG_FRAME) & 0x03ff;
1322 spin_unlock_irqrestore (&dev->lock, flags);
1323 return retval;
1326 static int net2280_wakeup (struct usb_gadget *_gadget)
1328 struct net2280 *dev;
1329 u32 tmp;
1330 unsigned long flags;
1332 if (!_gadget)
1333 return 0;
1334 dev = container_of (_gadget, struct net2280, gadget);
1336 spin_lock_irqsave (&dev->lock, flags);
1337 tmp = readl (&dev->usb->usbctl);
1338 if (tmp & (1 << DEVICE_REMOTE_WAKEUP_ENABLE))
1339 writel (1 << GENERATE_RESUME, &dev->usb->usbstat);
1340 spin_unlock_irqrestore (&dev->lock, flags);
1342 /* pci writes may still be posted */
1343 return 0;
1346 static int net2280_set_selfpowered (struct usb_gadget *_gadget, int value)
1348 struct net2280 *dev;
1349 u32 tmp;
1350 unsigned long flags;
1352 if (!_gadget)
1353 return 0;
1354 dev = container_of (_gadget, struct net2280, gadget);
1356 spin_lock_irqsave (&dev->lock, flags);
1357 tmp = readl (&dev->usb->usbctl);
1358 if (value)
1359 tmp |= (1 << SELF_POWERED_STATUS);
1360 else
1361 tmp &= ~(1 << SELF_POWERED_STATUS);
1362 writel (tmp, &dev->usb->usbctl);
1363 spin_unlock_irqrestore (&dev->lock, flags);
1365 return 0;
1368 static int net2280_pullup(struct usb_gadget *_gadget, int is_on)
1370 struct net2280 *dev;
1371 u32 tmp;
1372 unsigned long flags;
1374 if (!_gadget)
1375 return -ENODEV;
1376 dev = container_of (_gadget, struct net2280, gadget);
1378 spin_lock_irqsave (&dev->lock, flags);
1379 tmp = readl (&dev->usb->usbctl);
1380 dev->softconnect = (is_on != 0);
1381 if (is_on)
1382 tmp |= (1 << USB_DETECT_ENABLE);
1383 else
1384 tmp &= ~(1 << USB_DETECT_ENABLE);
1385 writel (tmp, &dev->usb->usbctl);
1386 spin_unlock_irqrestore (&dev->lock, flags);
1388 return 0;
1391 static const struct usb_gadget_ops net2280_ops = {
1392 .get_frame = net2280_get_frame,
1393 .wakeup = net2280_wakeup,
1394 .set_selfpowered = net2280_set_selfpowered,
1395 .pullup = net2280_pullup,
1398 /*-------------------------------------------------------------------------*/
1400 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1402 /* FIXME move these into procfs, and use seq_file.
1403 * Sysfs _still_ doesn't behave for arbitrarily sized files,
1404 * and also doesn't help products using this with 2.4 kernels.
1407 /* "function" sysfs attribute */
1408 static ssize_t
1409 show_function (struct device *_dev, struct device_attribute *attr, char *buf)
1411 struct net2280 *dev = dev_get_drvdata (_dev);
1413 if (!dev->driver
1414 || !dev->driver->function
1415 || strlen (dev->driver->function) > PAGE_SIZE)
1416 return 0;
1417 return scnprintf (buf, PAGE_SIZE, "%s\n", dev->driver->function);
1419 static DEVICE_ATTR (function, S_IRUGO, show_function, NULL);
1421 static ssize_t net2280_show_registers(struct device *_dev,
1422 struct device_attribute *attr, char *buf)
1424 struct net2280 *dev;
1425 char *next;
1426 unsigned size, t;
1427 unsigned long flags;
1428 int i;
1429 u32 t1, t2;
1430 const char *s;
1432 dev = dev_get_drvdata (_dev);
1433 next = buf;
1434 size = PAGE_SIZE;
1435 spin_lock_irqsave (&dev->lock, flags);
1437 if (dev->driver)
1438 s = dev->driver->driver.name;
1439 else
1440 s = "(none)";
1442 /* Main Control Registers */
1443 t = scnprintf (next, size, "%s version " DRIVER_VERSION
1444 ", chiprev %04x, dma %s\n\n"
1445 "devinit %03x fifoctl %08x gadget '%s'\n"
1446 "pci irqenb0 %02x irqenb1 %08x "
1447 "irqstat0 %04x irqstat1 %08x\n",
1448 driver_name, dev->chiprev,
1449 use_dma
1450 ? (use_dma_chaining ? "chaining" : "enabled")
1451 : "disabled",
1452 readl (&dev->regs->devinit),
1453 readl (&dev->regs->fifoctl),
1455 readl (&dev->regs->pciirqenb0),
1456 readl (&dev->regs->pciirqenb1),
1457 readl (&dev->regs->irqstat0),
1458 readl (&dev->regs->irqstat1));
1459 size -= t;
1460 next += t;
1462 /* USB Control Registers */
1463 t1 = readl (&dev->usb->usbctl);
1464 t2 = readl (&dev->usb->usbstat);
1465 if (t1 & (1 << VBUS_PIN)) {
1466 if (t2 & (1 << HIGH_SPEED))
1467 s = "high speed";
1468 else if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1469 s = "powered";
1470 else
1471 s = "full speed";
1472 /* full speed bit (6) not working?? */
1473 } else
1474 s = "not attached";
1475 t = scnprintf (next, size,
1476 "stdrsp %08x usbctl %08x usbstat %08x "
1477 "addr 0x%02x (%s)\n",
1478 readl (&dev->usb->stdrsp), t1, t2,
1479 readl (&dev->usb->ouraddr), s);
1480 size -= t;
1481 next += t;
1483 /* PCI Master Control Registers */
1485 /* DMA Control Registers */
1487 /* Configurable EP Control Registers */
1488 for (i = 0; i < 7; i++) {
1489 struct net2280_ep *ep;
1491 ep = &dev->ep [i];
1492 if (i && !ep->desc)
1493 continue;
1495 t1 = readl (&ep->regs->ep_cfg);
1496 t2 = readl (&ep->regs->ep_rsp) & 0xff;
1497 t = scnprintf (next, size,
1498 "\n%s\tcfg %05x rsp (%02x) %s%s%s%s%s%s%s%s"
1499 "irqenb %02x\n",
1500 ep->ep.name, t1, t2,
1501 (t2 & (1 << CLEAR_NAK_OUT_PACKETS))
1502 ? "NAK " : "",
1503 (t2 & (1 << CLEAR_EP_HIDE_STATUS_PHASE))
1504 ? "hide " : "",
1505 (t2 & (1 << CLEAR_EP_FORCE_CRC_ERROR))
1506 ? "CRC " : "",
1507 (t2 & (1 << CLEAR_INTERRUPT_MODE))
1508 ? "interrupt " : "",
1509 (t2 & (1<<CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE))
1510 ? "status " : "",
1511 (t2 & (1 << CLEAR_NAK_OUT_PACKETS_MODE))
1512 ? "NAKmode " : "",
1513 (t2 & (1 << CLEAR_ENDPOINT_TOGGLE))
1514 ? "DATA1 " : "DATA0 ",
1515 (t2 & (1 << CLEAR_ENDPOINT_HALT))
1516 ? "HALT " : "",
1517 readl (&ep->regs->ep_irqenb));
1518 size -= t;
1519 next += t;
1521 t = scnprintf (next, size,
1522 "\tstat %08x avail %04x "
1523 "(ep%d%s-%s)%s\n",
1524 readl (&ep->regs->ep_stat),
1525 readl (&ep->regs->ep_avail),
1526 t1 & 0x0f, DIR_STRING (t1),
1527 type_string (t1 >> 8),
1528 ep->stopped ? "*" : "");
1529 size -= t;
1530 next += t;
1532 if (!ep->dma)
1533 continue;
1535 t = scnprintf (next, size,
1536 " dma\tctl %08x stat %08x count %08x\n"
1537 "\taddr %08x desc %08x\n",
1538 readl (&ep->dma->dmactl),
1539 readl (&ep->dma->dmastat),
1540 readl (&ep->dma->dmacount),
1541 readl (&ep->dma->dmaaddr),
1542 readl (&ep->dma->dmadesc));
1543 size -= t;
1544 next += t;
1548 /* Indexed Registers */
1549 // none yet
1551 /* Statistics */
1552 t = scnprintf (next, size, "\nirqs: ");
1553 size -= t;
1554 next += t;
1555 for (i = 0; i < 7; i++) {
1556 struct net2280_ep *ep;
1558 ep = &dev->ep [i];
1559 if (i && !ep->irqs)
1560 continue;
1561 t = scnprintf (next, size, " %s/%lu", ep->ep.name, ep->irqs);
1562 size -= t;
1563 next += t;
1566 t = scnprintf (next, size, "\n");
1567 size -= t;
1568 next += t;
1570 spin_unlock_irqrestore (&dev->lock, flags);
1572 return PAGE_SIZE - size;
1574 static DEVICE_ATTR(registers, S_IRUGO, net2280_show_registers, NULL);
1576 static ssize_t
1577 show_queues (struct device *_dev, struct device_attribute *attr, char *buf)
1579 struct net2280 *dev;
1580 char *next;
1581 unsigned size;
1582 unsigned long flags;
1583 int i;
1585 dev = dev_get_drvdata (_dev);
1586 next = buf;
1587 size = PAGE_SIZE;
1588 spin_lock_irqsave (&dev->lock, flags);
1590 for (i = 0; i < 7; i++) {
1591 struct net2280_ep *ep = &dev->ep [i];
1592 struct net2280_request *req;
1593 int t;
1595 if (i != 0) {
1596 const struct usb_endpoint_descriptor *d;
1598 d = ep->desc;
1599 if (!d)
1600 continue;
1601 t = d->bEndpointAddress;
1602 t = scnprintf (next, size,
1603 "\n%s (ep%d%s-%s) max %04x %s fifo %d\n",
1604 ep->ep.name, t & USB_ENDPOINT_NUMBER_MASK,
1605 (t & USB_DIR_IN) ? "in" : "out",
1606 ({ char *val;
1607 switch (d->bmAttributes & 0x03) {
1608 case USB_ENDPOINT_XFER_BULK:
1609 val = "bulk"; break;
1610 case USB_ENDPOINT_XFER_INT:
1611 val = "intr"; break;
1612 default:
1613 val = "iso"; break;
1614 }; val; }),
1615 le16_to_cpu (d->wMaxPacketSize) & 0x1fff,
1616 ep->dma ? "dma" : "pio", ep->fifo_size
1618 } else /* ep0 should only have one transfer queued */
1619 t = scnprintf (next, size, "ep0 max 64 pio %s\n",
1620 ep->is_in ? "in" : "out");
1621 if (t <= 0 || t > size)
1622 goto done;
1623 size -= t;
1624 next += t;
1626 if (list_empty (&ep->queue)) {
1627 t = scnprintf (next, size, "\t(nothing queued)\n");
1628 if (t <= 0 || t > size)
1629 goto done;
1630 size -= t;
1631 next += t;
1632 continue;
1634 list_for_each_entry (req, &ep->queue, queue) {
1635 if (ep->dma && req->td_dma == readl (&ep->dma->dmadesc))
1636 t = scnprintf (next, size,
1637 "\treq %p len %d/%d "
1638 "buf %p (dmacount %08x)\n",
1639 &req->req, req->req.actual,
1640 req->req.length, req->req.buf,
1641 readl (&ep->dma->dmacount));
1642 else
1643 t = scnprintf (next, size,
1644 "\treq %p len %d/%d buf %p\n",
1645 &req->req, req->req.actual,
1646 req->req.length, req->req.buf);
1647 if (t <= 0 || t > size)
1648 goto done;
1649 size -= t;
1650 next += t;
1652 if (ep->dma) {
1653 struct net2280_dma *td;
1655 td = req->td;
1656 t = scnprintf (next, size, "\t td %08x "
1657 " count %08x buf %08x desc %08x\n",
1658 (u32) req->td_dma,
1659 le32_to_cpu (td->dmacount),
1660 le32_to_cpu (td->dmaaddr),
1661 le32_to_cpu (td->dmadesc));
1662 if (t <= 0 || t > size)
1663 goto done;
1664 size -= t;
1665 next += t;
1670 done:
1671 spin_unlock_irqrestore (&dev->lock, flags);
1672 return PAGE_SIZE - size;
1674 static DEVICE_ATTR (queues, S_IRUGO, show_queues, NULL);
1677 #else
1679 #define device_create_file(a,b) (0)
1680 #define device_remove_file(a,b) do { } while (0)
1682 #endif
1684 /*-------------------------------------------------------------------------*/
1686 /* another driver-specific mode might be a request type doing dma
1687 * to/from another device fifo instead of to/from memory.
1690 static void set_fifo_mode (struct net2280 *dev, int mode)
1692 /* keeping high bits preserves BAR2 */
1693 writel ((0xffff << PCI_BASE2_RANGE) | mode, &dev->regs->fifoctl);
1695 /* always ep-{a,b,e,f} ... maybe not ep-c or ep-d */
1696 INIT_LIST_HEAD (&dev->gadget.ep_list);
1697 list_add_tail (&dev->ep [1].ep.ep_list, &dev->gadget.ep_list);
1698 list_add_tail (&dev->ep [2].ep.ep_list, &dev->gadget.ep_list);
1699 switch (mode) {
1700 case 0:
1701 list_add_tail (&dev->ep [3].ep.ep_list, &dev->gadget.ep_list);
1702 list_add_tail (&dev->ep [4].ep.ep_list, &dev->gadget.ep_list);
1703 dev->ep [1].fifo_size = dev->ep [2].fifo_size = 1024;
1704 break;
1705 case 1:
1706 dev->ep [1].fifo_size = dev->ep [2].fifo_size = 2048;
1707 break;
1708 case 2:
1709 list_add_tail (&dev->ep [3].ep.ep_list, &dev->gadget.ep_list);
1710 dev->ep [1].fifo_size = 2048;
1711 dev->ep [2].fifo_size = 1024;
1712 break;
1714 /* fifo sizes for ep0, ep-c, ep-d, ep-e, and ep-f never change */
1715 list_add_tail (&dev->ep [5].ep.ep_list, &dev->gadget.ep_list);
1716 list_add_tail (&dev->ep [6].ep.ep_list, &dev->gadget.ep_list);
1719 /* just declare this in any driver that really need it */
1720 extern int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode);
1723 * net2280_set_fifo_mode - change allocation of fifo buffers
1724 * @gadget: access to the net2280 device that will be updated
1725 * @mode: 0 for default, four 1kB buffers (ep-a through ep-d);
1726 * 1 for two 2kB buffers (ep-a and ep-b only);
1727 * 2 for one 2kB buffer (ep-a) and two 1kB ones (ep-b, ep-c).
1729 * returns zero on success, else negative errno. when this succeeds,
1730 * the contents of gadget->ep_list may have changed.
1732 * you may only call this function when endpoints a-d are all disabled.
1733 * use it whenever extra hardware buffering can help performance, such
1734 * as before enabling "high bandwidth" interrupt endpoints that use
1735 * maxpacket bigger than 512 (when double buffering would otherwise
1736 * be unavailable).
1738 int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode)
1740 int i;
1741 struct net2280 *dev;
1742 int status = 0;
1743 unsigned long flags;
1745 if (!gadget)
1746 return -ENODEV;
1747 dev = container_of (gadget, struct net2280, gadget);
1749 spin_lock_irqsave (&dev->lock, flags);
1751 for (i = 1; i <= 4; i++)
1752 if (dev->ep [i].desc) {
1753 status = -EINVAL;
1754 break;
1756 if (mode < 0 || mode > 2)
1757 status = -EINVAL;
1758 if (status == 0)
1759 set_fifo_mode (dev, mode);
1760 spin_unlock_irqrestore (&dev->lock, flags);
1762 if (status == 0) {
1763 if (mode == 1)
1764 DEBUG (dev, "fifo: ep-a 2K, ep-b 2K\n");
1765 else if (mode == 2)
1766 DEBUG (dev, "fifo: ep-a 2K, ep-b 1K, ep-c 1K\n");
1767 /* else all are 1K */
1769 return status;
1771 EXPORT_SYMBOL (net2280_set_fifo_mode);
1773 /*-------------------------------------------------------------------------*/
1775 /* keeping it simple:
1776 * - one bus driver, initted first;
1777 * - one function driver, initted second
1779 * most of the work to support multiple net2280 controllers would
1780 * be to associate this gadget driver (yes?) with all of them, or
1781 * perhaps to bind specific drivers to specific devices.
1784 static struct net2280 *the_controller;
1786 static void usb_reset (struct net2280 *dev)
1788 u32 tmp;
1790 dev->gadget.speed = USB_SPEED_UNKNOWN;
1791 (void) readl (&dev->usb->usbctl);
1793 net2280_led_init (dev);
1795 /* disable automatic responses, and irqs */
1796 writel (0, &dev->usb->stdrsp);
1797 writel (0, &dev->regs->pciirqenb0);
1798 writel (0, &dev->regs->pciirqenb1);
1800 /* clear old dma and irq state */
1801 for (tmp = 0; tmp < 4; tmp++) {
1802 struct net2280_ep *ep = &dev->ep [tmp + 1];
1804 if (ep->dma)
1805 abort_dma (ep);
1807 writel (~0, &dev->regs->irqstat0),
1808 writel (~(1 << SUSPEND_REQUEST_INTERRUPT), &dev->regs->irqstat1),
1810 /* reset, and enable pci */
1811 tmp = readl (&dev->regs->devinit)
1812 | (1 << PCI_ENABLE)
1813 | (1 << FIFO_SOFT_RESET)
1814 | (1 << USB_SOFT_RESET)
1815 | (1 << M8051_RESET);
1816 writel (tmp, &dev->regs->devinit);
1818 /* standard fifo and endpoint allocations */
1819 set_fifo_mode (dev, (fifo_mode <= 2) ? fifo_mode : 0);
1822 static void usb_reinit (struct net2280 *dev)
1824 u32 tmp;
1825 int init_dma;
1827 /* use_dma changes are ignored till next device re-init */
1828 init_dma = use_dma;
1830 /* basic endpoint init */
1831 for (tmp = 0; tmp < 7; tmp++) {
1832 struct net2280_ep *ep = &dev->ep [tmp];
1834 ep->ep.name = ep_name [tmp];
1835 ep->dev = dev;
1836 ep->num = tmp;
1838 if (tmp > 0 && tmp <= 4) {
1839 ep->fifo_size = 1024;
1840 if (init_dma)
1841 ep->dma = &dev->dma [tmp - 1];
1842 } else
1843 ep->fifo_size = 64;
1844 ep->regs = &dev->epregs [tmp];
1845 ep_reset (dev->regs, ep);
1847 dev->ep [0].ep.maxpacket = 64;
1848 dev->ep [5].ep.maxpacket = 64;
1849 dev->ep [6].ep.maxpacket = 64;
1851 dev->gadget.ep0 = &dev->ep [0].ep;
1852 dev->ep [0].stopped = 0;
1853 INIT_LIST_HEAD (&dev->gadget.ep0->ep_list);
1855 /* we want to prevent lowlevel/insecure access from the USB host,
1856 * but erratum 0119 means this enable bit is ignored
1858 for (tmp = 0; tmp < 5; tmp++)
1859 writel (EP_DONTUSE, &dev->dep [tmp].dep_cfg);
1862 static void ep0_start (struct net2280 *dev)
1864 writel ( (1 << CLEAR_EP_HIDE_STATUS_PHASE)
1865 | (1 << CLEAR_NAK_OUT_PACKETS)
1866 | (1 << CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE)
1867 , &dev->epregs [0].ep_rsp);
1870 * hardware optionally handles a bunch of standard requests
1871 * that the API hides from drivers anyway. have it do so.
1872 * endpoint status/features are handled in software, to
1873 * help pass tests for some dubious behavior.
1875 writel ( (1 << SET_TEST_MODE)
1876 | (1 << SET_ADDRESS)
1877 | (1 << DEVICE_SET_CLEAR_DEVICE_REMOTE_WAKEUP)
1878 | (1 << GET_DEVICE_STATUS)
1879 | (1 << GET_INTERFACE_STATUS)
1880 , &dev->usb->stdrsp);
1881 writel ( (1 << USB_ROOT_PORT_WAKEUP_ENABLE)
1882 | (1 << SELF_POWERED_USB_DEVICE)
1883 | (1 << REMOTE_WAKEUP_SUPPORT)
1884 | (dev->softconnect << USB_DETECT_ENABLE)
1885 | (1 << SELF_POWERED_STATUS)
1886 , &dev->usb->usbctl);
1888 /* enable irqs so we can see ep0 and general operation */
1889 writel ( (1 << SETUP_PACKET_INTERRUPT_ENABLE)
1890 | (1 << ENDPOINT_0_INTERRUPT_ENABLE)
1891 , &dev->regs->pciirqenb0);
1892 writel ( (1 << PCI_INTERRUPT_ENABLE)
1893 | (1 << PCI_MASTER_ABORT_RECEIVED_INTERRUPT_ENABLE)
1894 | (1 << PCI_TARGET_ABORT_RECEIVED_INTERRUPT_ENABLE)
1895 | (1 << PCI_RETRY_ABORT_INTERRUPT_ENABLE)
1896 | (1 << VBUS_INTERRUPT_ENABLE)
1897 | (1 << ROOT_PORT_RESET_INTERRUPT_ENABLE)
1898 | (1 << SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE)
1899 , &dev->regs->pciirqenb1);
1901 /* don't leave any writes posted */
1902 (void) readl (&dev->usb->usbctl);
1905 /* when a driver is successfully registered, it will receive
1906 * control requests including set_configuration(), which enables
1907 * non-control requests. then usb traffic follows until a
1908 * disconnect is reported. then a host may connect again, or
1909 * the driver might get unbound.
1911 int usb_gadget_register_driver (struct usb_gadget_driver *driver)
1913 struct net2280 *dev = the_controller;
1914 int retval;
1915 unsigned i;
1917 /* insist on high speed support from the driver, since
1918 * (dev->usb->xcvrdiag & FORCE_FULL_SPEED_MODE)
1919 * "must not be used in normal operation"
1921 if (!driver
1922 || driver->speed != USB_SPEED_HIGH
1923 || !driver->bind
1924 || !driver->setup)
1925 return -EINVAL;
1926 if (!dev)
1927 return -ENODEV;
1928 if (dev->driver)
1929 return -EBUSY;
1931 for (i = 0; i < 7; i++)
1932 dev->ep [i].irqs = 0;
1934 /* hook up the driver ... */
1935 dev->softconnect = 1;
1936 driver->driver.bus = NULL;
1937 dev->driver = driver;
1938 dev->gadget.dev.driver = &driver->driver;
1939 retval = driver->bind (&dev->gadget);
1940 if (retval) {
1941 DEBUG (dev, "bind to driver %s --> %d\n",
1942 driver->driver.name, retval);
1943 dev->driver = NULL;
1944 dev->gadget.dev.driver = NULL;
1945 return retval;
1948 retval = device_create_file (&dev->pdev->dev, &dev_attr_function);
1949 if (retval) goto err_unbind;
1950 retval = device_create_file (&dev->pdev->dev, &dev_attr_queues);
1951 if (retval) goto err_func;
1953 /* ... then enable host detection and ep0; and we're ready
1954 * for set_configuration as well as eventual disconnect.
1956 net2280_led_active (dev, 1);
1957 ep0_start (dev);
1959 DEBUG (dev, "%s ready, usbctl %08x stdrsp %08x\n",
1960 driver->driver.name,
1961 readl (&dev->usb->usbctl),
1962 readl (&dev->usb->stdrsp));
1964 /* pci writes may still be posted */
1965 return 0;
1967 err_func:
1968 device_remove_file (&dev->pdev->dev, &dev_attr_function);
1969 err_unbind:
1970 driver->unbind (&dev->gadget);
1971 dev->gadget.dev.driver = NULL;
1972 dev->driver = NULL;
1973 return retval;
1975 EXPORT_SYMBOL (usb_gadget_register_driver);
1977 static void
1978 stop_activity (struct net2280 *dev, struct usb_gadget_driver *driver)
1980 int i;
1982 /* don't disconnect if it's not connected */
1983 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1984 driver = NULL;
1986 /* stop hardware; prevent new request submissions;
1987 * and kill any outstanding requests.
1989 usb_reset (dev);
1990 for (i = 0; i < 7; i++)
1991 nuke (&dev->ep [i]);
1993 /* report disconnect; the driver is already quiesced */
1994 if (driver) {
1995 spin_unlock (&dev->lock);
1996 driver->disconnect (&dev->gadget);
1997 spin_lock (&dev->lock);
2000 usb_reinit (dev);
2003 int usb_gadget_unregister_driver (struct usb_gadget_driver *driver)
2005 struct net2280 *dev = the_controller;
2006 unsigned long flags;
2008 if (!dev)
2009 return -ENODEV;
2010 if (!driver || driver != dev->driver || !driver->unbind)
2011 return -EINVAL;
2013 spin_lock_irqsave (&dev->lock, flags);
2014 stop_activity (dev, driver);
2015 spin_unlock_irqrestore (&dev->lock, flags);
2017 net2280_pullup (&dev->gadget, 0);
2019 driver->unbind (&dev->gadget);
2020 dev->gadget.dev.driver = NULL;
2021 dev->driver = NULL;
2023 net2280_led_active (dev, 0);
2024 device_remove_file (&dev->pdev->dev, &dev_attr_function);
2025 device_remove_file (&dev->pdev->dev, &dev_attr_queues);
2027 DEBUG (dev, "unregistered driver '%s'\n", driver->driver.name);
2028 return 0;
2030 EXPORT_SYMBOL (usb_gadget_unregister_driver);
2033 /*-------------------------------------------------------------------------*/
2035 /* handle ep0, ep-e, ep-f with 64 byte packets: packet per irq.
2036 * also works for dma-capable endpoints, in pio mode or just
2037 * to manually advance the queue after short OUT transfers.
2039 static void handle_ep_small (struct net2280_ep *ep)
2041 struct net2280_request *req;
2042 u32 t;
2043 /* 0 error, 1 mid-data, 2 done */
2044 int mode = 1;
2046 if (!list_empty (&ep->queue))
2047 req = list_entry (ep->queue.next,
2048 struct net2280_request, queue);
2049 else
2050 req = NULL;
2052 /* ack all, and handle what we care about */
2053 t = readl (&ep->regs->ep_stat);
2054 ep->irqs++;
2055 #if 0
2056 VDEBUG (ep->dev, "%s ack ep_stat %08x, req %p\n",
2057 ep->ep.name, t, req ? &req->req : 0);
2058 #endif
2059 if (!ep->is_in || ep->dev->pdev->device == 0x2280)
2060 writel (t & ~(1 << NAK_OUT_PACKETS), &ep->regs->ep_stat);
2061 else
2062 /* Added for 2282 */
2063 writel (t, &ep->regs->ep_stat);
2065 /* for ep0, monitor token irqs to catch data stage length errors
2066 * and to synchronize on status.
2068 * also, to defer reporting of protocol stalls ... here's where
2069 * data or status first appears, handling stalls here should never
2070 * cause trouble on the host side..
2072 * control requests could be slightly faster without token synch for
2073 * status, but status can jam up that way.
2075 if (unlikely (ep->num == 0)) {
2076 if (ep->is_in) {
2077 /* status; stop NAKing */
2078 if (t & (1 << DATA_OUT_PING_TOKEN_INTERRUPT)) {
2079 if (ep->dev->protocol_stall) {
2080 ep->stopped = 1;
2081 set_halt (ep);
2083 if (!req)
2084 allow_status (ep);
2085 mode = 2;
2086 /* reply to extra IN data tokens with a zlp */
2087 } else if (t & (1 << DATA_IN_TOKEN_INTERRUPT)) {
2088 if (ep->dev->protocol_stall) {
2089 ep->stopped = 1;
2090 set_halt (ep);
2091 mode = 2;
2092 } else if (ep->responded &&
2093 !req && !ep->stopped)
2094 write_fifo (ep, NULL);
2096 } else {
2097 /* status; stop NAKing */
2098 if (t & (1 << DATA_IN_TOKEN_INTERRUPT)) {
2099 if (ep->dev->protocol_stall) {
2100 ep->stopped = 1;
2101 set_halt (ep);
2103 mode = 2;
2104 /* an extra OUT token is an error */
2105 } else if (((t & (1 << DATA_OUT_PING_TOKEN_INTERRUPT))
2106 && req
2107 && req->req.actual == req->req.length)
2108 || (ep->responded && !req)) {
2109 ep->dev->protocol_stall = 1;
2110 set_halt (ep);
2111 ep->stopped = 1;
2112 if (req)
2113 done (ep, req, -EOVERFLOW);
2114 req = NULL;
2119 if (unlikely (!req))
2120 return;
2122 /* manual DMA queue advance after short OUT */
2123 if (likely (ep->dma != 0)) {
2124 if (t & (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)) {
2125 u32 count;
2126 int stopped = ep->stopped;
2128 /* TRANSFERRED works around OUT_DONE erratum 0112.
2129 * we expect (N <= maxpacket) bytes; host wrote M.
2130 * iff (M < N) we won't ever see a DMA interrupt.
2132 ep->stopped = 1;
2133 for (count = 0; ; t = readl (&ep->regs->ep_stat)) {
2135 /* any preceding dma transfers must finish.
2136 * dma handles (M >= N), may empty the queue
2138 scan_dma_completions (ep);
2139 if (unlikely (list_empty (&ep->queue)
2140 || ep->out_overflow)) {
2141 req = NULL;
2142 break;
2144 req = list_entry (ep->queue.next,
2145 struct net2280_request, queue);
2147 /* here either (M < N), a "real" short rx;
2148 * or (M == N) and the queue didn't empty
2150 if (likely (t & (1 << FIFO_EMPTY))) {
2151 count = readl (&ep->dma->dmacount);
2152 count &= DMA_BYTE_COUNT_MASK;
2153 if (readl (&ep->dma->dmadesc)
2154 != req->td_dma)
2155 req = NULL;
2156 break;
2158 udelay(1);
2161 /* stop DMA, leave ep NAKing */
2162 writel ((1 << DMA_ABORT), &ep->dma->dmastat);
2163 spin_stop_dma (ep->dma);
2165 if (likely (req)) {
2166 req->td->dmacount = 0;
2167 t = readl (&ep->regs->ep_avail);
2168 dma_done (ep, req, count,
2169 (ep->out_overflow || t)
2170 ? -EOVERFLOW : 0);
2173 /* also flush to prevent erratum 0106 trouble */
2174 if (unlikely (ep->out_overflow
2175 || (ep->dev->chiprev == 0x0100
2176 && ep->dev->gadget.speed
2177 == USB_SPEED_FULL))) {
2178 out_flush (ep);
2179 ep->out_overflow = 0;
2182 /* (re)start dma if needed, stop NAKing */
2183 ep->stopped = stopped;
2184 if (!list_empty (&ep->queue))
2185 restart_dma (ep);
2186 } else
2187 DEBUG (ep->dev, "%s dma ep_stat %08x ??\n",
2188 ep->ep.name, t);
2189 return;
2191 /* data packet(s) received (in the fifo, OUT) */
2192 } else if (t & (1 << DATA_PACKET_RECEIVED_INTERRUPT)) {
2193 if (read_fifo (ep, req) && ep->num != 0)
2194 mode = 2;
2196 /* data packet(s) transmitted (IN) */
2197 } else if (t & (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)) {
2198 unsigned len;
2200 len = req->req.length - req->req.actual;
2201 if (len > ep->ep.maxpacket)
2202 len = ep->ep.maxpacket;
2203 req->req.actual += len;
2205 /* if we wrote it all, we're usually done */
2206 if (req->req.actual == req->req.length) {
2207 if (ep->num == 0) {
2208 /* send zlps until the status stage */
2209 } else if (!req->req.zero || len != ep->ep.maxpacket)
2210 mode = 2;
2213 /* there was nothing to do ... */
2214 } else if (mode == 1)
2215 return;
2217 /* done */
2218 if (mode == 2) {
2219 /* stream endpoints often resubmit/unlink in completion */
2220 done (ep, req, 0);
2222 /* maybe advance queue to next request */
2223 if (ep->num == 0) {
2224 /* NOTE: net2280 could let gadget driver start the
2225 * status stage later. since not all controllers let
2226 * them control that, the api doesn't (yet) allow it.
2228 if (!ep->stopped)
2229 allow_status (ep);
2230 req = NULL;
2231 } else {
2232 if (!list_empty (&ep->queue) && !ep->stopped)
2233 req = list_entry (ep->queue.next,
2234 struct net2280_request, queue);
2235 else
2236 req = NULL;
2237 if (req && !ep->is_in)
2238 stop_out_naking (ep);
2242 /* is there a buffer for the next packet?
2243 * for best streaming performance, make sure there is one.
2245 if (req && !ep->stopped) {
2247 /* load IN fifo with next packet (may be zlp) */
2248 if (t & (1 << DATA_PACKET_TRANSMITTED_INTERRUPT))
2249 write_fifo (ep, &req->req);
2253 static struct net2280_ep *
2254 get_ep_by_addr (struct net2280 *dev, u16 wIndex)
2256 struct net2280_ep *ep;
2258 if ((wIndex & USB_ENDPOINT_NUMBER_MASK) == 0)
2259 return &dev->ep [0];
2260 list_for_each_entry (ep, &dev->gadget.ep_list, ep.ep_list) {
2261 u8 bEndpointAddress;
2263 if (!ep->desc)
2264 continue;
2265 bEndpointAddress = ep->desc->bEndpointAddress;
2266 if ((wIndex ^ bEndpointAddress) & USB_DIR_IN)
2267 continue;
2268 if ((wIndex & 0x0f) == (bEndpointAddress & 0x0f))
2269 return ep;
2271 return NULL;
2274 static void handle_stat0_irqs (struct net2280 *dev, u32 stat)
2276 struct net2280_ep *ep;
2277 u32 num, scratch;
2279 /* most of these don't need individual acks */
2280 stat &= ~(1 << INTA_ASSERTED);
2281 if (!stat)
2282 return;
2283 // DEBUG (dev, "irqstat0 %04x\n", stat);
2285 /* starting a control request? */
2286 if (unlikely (stat & (1 << SETUP_PACKET_INTERRUPT))) {
2287 union {
2288 u32 raw [2];
2289 struct usb_ctrlrequest r;
2290 } u;
2291 int tmp;
2292 struct net2280_request *req;
2294 if (dev->gadget.speed == USB_SPEED_UNKNOWN) {
2295 if (readl (&dev->usb->usbstat) & (1 << HIGH_SPEED))
2296 dev->gadget.speed = USB_SPEED_HIGH;
2297 else
2298 dev->gadget.speed = USB_SPEED_FULL;
2299 net2280_led_speed (dev, dev->gadget.speed);
2300 DEBUG (dev, "%s speed\n",
2301 (dev->gadget.speed == USB_SPEED_HIGH)
2302 ? "high" : "full");
2305 ep = &dev->ep [0];
2306 ep->irqs++;
2308 /* make sure any leftover request state is cleared */
2309 stat &= ~(1 << ENDPOINT_0_INTERRUPT);
2310 while (!list_empty (&ep->queue)) {
2311 req = list_entry (ep->queue.next,
2312 struct net2280_request, queue);
2313 done (ep, req, (req->req.actual == req->req.length)
2314 ? 0 : -EPROTO);
2316 ep->stopped = 0;
2317 dev->protocol_stall = 0;
2319 if (ep->dev->pdev->device == 0x2280)
2320 tmp = (1 << FIFO_OVERFLOW)
2321 | (1 << FIFO_UNDERFLOW);
2322 else
2323 tmp = 0;
2325 writel (tmp | (1 << TIMEOUT)
2326 | (1 << USB_STALL_SENT)
2327 | (1 << USB_IN_NAK_SENT)
2328 | (1 << USB_IN_ACK_RCVD)
2329 | (1 << USB_OUT_PING_NAK_SENT)
2330 | (1 << USB_OUT_ACK_SENT)
2331 | (1 << SHORT_PACKET_OUT_DONE_INTERRUPT)
2332 | (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)
2333 | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
2334 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
2335 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
2336 | (1 << DATA_IN_TOKEN_INTERRUPT)
2337 , &ep->regs->ep_stat);
2338 u.raw [0] = readl (&dev->usb->setup0123);
2339 u.raw [1] = readl (&dev->usb->setup4567);
2341 cpu_to_le32s (&u.raw [0]);
2342 cpu_to_le32s (&u.raw [1]);
2344 tmp = 0;
2346 #define w_value le16_to_cpu(u.r.wValue)
2347 #define w_index le16_to_cpu(u.r.wIndex)
2348 #define w_length le16_to_cpu(u.r.wLength)
2350 /* ack the irq */
2351 writel (1 << SETUP_PACKET_INTERRUPT, &dev->regs->irqstat0);
2352 stat ^= (1 << SETUP_PACKET_INTERRUPT);
2354 /* watch control traffic at the token level, and force
2355 * synchronization before letting the status stage happen.
2356 * FIXME ignore tokens we'll NAK, until driver responds.
2357 * that'll mean a lot less irqs for some drivers.
2359 ep->is_in = (u.r.bRequestType & USB_DIR_IN) != 0;
2360 if (ep->is_in) {
2361 scratch = (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
2362 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
2363 | (1 << DATA_IN_TOKEN_INTERRUPT);
2364 stop_out_naking (ep);
2365 } else
2366 scratch = (1 << DATA_PACKET_RECEIVED_INTERRUPT)
2367 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
2368 | (1 << DATA_IN_TOKEN_INTERRUPT);
2369 writel (scratch, &dev->epregs [0].ep_irqenb);
2371 /* we made the hardware handle most lowlevel requests;
2372 * everything else goes uplevel to the gadget code.
2374 ep->responded = 1;
2375 switch (u.r.bRequest) {
2376 case USB_REQ_GET_STATUS: {
2377 struct net2280_ep *e;
2378 __le32 status;
2380 /* hw handles device and interface status */
2381 if (u.r.bRequestType != (USB_DIR_IN|USB_RECIP_ENDPOINT))
2382 goto delegate;
2383 if ((e = get_ep_by_addr (dev, w_index)) == 0
2384 || w_length > 2)
2385 goto do_stall;
2387 if (readl (&e->regs->ep_rsp)
2388 & (1 << SET_ENDPOINT_HALT))
2389 status = __constant_cpu_to_le32 (1);
2390 else
2391 status = __constant_cpu_to_le32 (0);
2393 /* don't bother with a request object! */
2394 writel (0, &dev->epregs [0].ep_irqenb);
2395 set_fifo_bytecount (ep, w_length);
2396 writel ((__force u32)status, &dev->epregs [0].ep_data);
2397 allow_status (ep);
2398 VDEBUG (dev, "%s stat %02x\n", ep->ep.name, status);
2399 goto next_endpoints;
2401 break;
2402 case USB_REQ_CLEAR_FEATURE: {
2403 struct net2280_ep *e;
2405 /* hw handles device features */
2406 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
2407 goto delegate;
2408 if (w_value != USB_ENDPOINT_HALT
2409 || w_length != 0)
2410 goto do_stall;
2411 if ((e = get_ep_by_addr (dev, w_index)) == 0)
2412 goto do_stall;
2413 clear_halt (e);
2414 allow_status (ep);
2415 VDEBUG (dev, "%s clear halt\n", ep->ep.name);
2416 goto next_endpoints;
2418 break;
2419 case USB_REQ_SET_FEATURE: {
2420 struct net2280_ep *e;
2422 /* hw handles device features */
2423 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
2424 goto delegate;
2425 if (w_value != USB_ENDPOINT_HALT
2426 || w_length != 0)
2427 goto do_stall;
2428 if ((e = get_ep_by_addr (dev, w_index)) == 0)
2429 goto do_stall;
2430 set_halt (e);
2431 allow_status (ep);
2432 VDEBUG (dev, "%s set halt\n", ep->ep.name);
2433 goto next_endpoints;
2435 break;
2436 default:
2437 delegate:
2438 VDEBUG (dev, "setup %02x.%02x v%04x i%04x l%04x "
2439 "ep_cfg %08x\n",
2440 u.r.bRequestType, u.r.bRequest,
2441 w_value, w_index, w_length,
2442 readl (&ep->regs->ep_cfg));
2443 ep->responded = 0;
2444 spin_unlock (&dev->lock);
2445 tmp = dev->driver->setup (&dev->gadget, &u.r);
2446 spin_lock (&dev->lock);
2449 /* stall ep0 on error */
2450 if (tmp < 0) {
2451 do_stall:
2452 VDEBUG (dev, "req %02x.%02x protocol STALL; stat %d\n",
2453 u.r.bRequestType, u.r.bRequest, tmp);
2454 dev->protocol_stall = 1;
2457 /* some in/out token irq should follow; maybe stall then.
2458 * driver must queue a request (even zlp) or halt ep0
2459 * before the host times out.
2463 #undef w_value
2464 #undef w_index
2465 #undef w_length
2467 next_endpoints:
2468 /* endpoint data irq ? */
2469 scratch = stat & 0x7f;
2470 stat &= ~0x7f;
2471 for (num = 0; scratch; num++) {
2472 u32 t;
2474 /* do this endpoint's FIFO and queue need tending? */
2475 t = 1 << num;
2476 if ((scratch & t) == 0)
2477 continue;
2478 scratch ^= t;
2480 ep = &dev->ep [num];
2481 handle_ep_small (ep);
2484 if (stat)
2485 DEBUG (dev, "unhandled irqstat0 %08x\n", stat);
2488 #define DMA_INTERRUPTS ( \
2489 (1 << DMA_D_INTERRUPT) \
2490 | (1 << DMA_C_INTERRUPT) \
2491 | (1 << DMA_B_INTERRUPT) \
2492 | (1 << DMA_A_INTERRUPT))
2493 #define PCI_ERROR_INTERRUPTS ( \
2494 (1 << PCI_MASTER_ABORT_RECEIVED_INTERRUPT) \
2495 | (1 << PCI_TARGET_ABORT_RECEIVED_INTERRUPT) \
2496 | (1 << PCI_RETRY_ABORT_INTERRUPT))
2498 static void handle_stat1_irqs (struct net2280 *dev, u32 stat)
2500 struct net2280_ep *ep;
2501 u32 tmp, num, mask, scratch;
2503 /* after disconnect there's nothing else to do! */
2504 tmp = (1 << VBUS_INTERRUPT) | (1 << ROOT_PORT_RESET_INTERRUPT);
2505 mask = (1 << HIGH_SPEED) | (1 << FULL_SPEED);
2507 /* VBUS disconnect is indicated by VBUS_PIN and VBUS_INTERRUPT set.
2508 * Root Port Reset is indicated by ROOT_PORT_RESET_INTERRRUPT set and
2509 * both HIGH_SPEED and FULL_SPEED clear (as ROOT_PORT_RESET_INTERRUPT
2510 * only indicates a change in the reset state).
2512 if (stat & tmp) {
2513 writel (tmp, &dev->regs->irqstat1);
2514 if ((((stat & (1 << ROOT_PORT_RESET_INTERRUPT))
2515 && ((readl (&dev->usb->usbstat) & mask)
2516 == 0))
2517 || ((readl (&dev->usb->usbctl)
2518 & (1 << VBUS_PIN)) == 0)
2519 ) && ( dev->gadget.speed != USB_SPEED_UNKNOWN)) {
2520 DEBUG (dev, "disconnect %s\n",
2521 dev->driver->driver.name);
2522 stop_activity (dev, dev->driver);
2523 ep0_start (dev);
2524 return;
2526 stat &= ~tmp;
2528 /* vBUS can bounce ... one of many reasons to ignore the
2529 * notion of hotplug events on bus connect/disconnect!
2531 if (!stat)
2532 return;
2535 /* NOTE: chip stays in PCI D0 state for now, but it could
2536 * enter D1 to save more power
2538 tmp = (1 << SUSPEND_REQUEST_CHANGE_INTERRUPT);
2539 if (stat & tmp) {
2540 writel (tmp, &dev->regs->irqstat1);
2541 if (stat & (1 << SUSPEND_REQUEST_INTERRUPT)) {
2542 if (dev->driver->suspend)
2543 dev->driver->suspend (&dev->gadget);
2544 if (!enable_suspend)
2545 stat &= ~(1 << SUSPEND_REQUEST_INTERRUPT);
2546 } else {
2547 if (dev->driver->resume)
2548 dev->driver->resume (&dev->gadget);
2549 /* at high speed, note erratum 0133 */
2551 stat &= ~tmp;
2554 /* clear any other status/irqs */
2555 if (stat)
2556 writel (stat, &dev->regs->irqstat1);
2558 /* some status we can just ignore */
2559 if (dev->pdev->device == 0x2280)
2560 stat &= ~((1 << CONTROL_STATUS_INTERRUPT)
2561 | (1 << SUSPEND_REQUEST_INTERRUPT)
2562 | (1 << RESUME_INTERRUPT)
2563 | (1 << SOF_INTERRUPT));
2564 else
2565 stat &= ~((1 << CONTROL_STATUS_INTERRUPT)
2566 | (1 << RESUME_INTERRUPT)
2567 | (1 << SOF_DOWN_INTERRUPT)
2568 | (1 << SOF_INTERRUPT));
2570 if (!stat)
2571 return;
2572 // DEBUG (dev, "irqstat1 %08x\n", stat);
2574 /* DMA status, for ep-{a,b,c,d} */
2575 scratch = stat & DMA_INTERRUPTS;
2576 stat &= ~DMA_INTERRUPTS;
2577 scratch >>= 9;
2578 for (num = 0; scratch; num++) {
2579 struct net2280_dma_regs __iomem *dma;
2581 tmp = 1 << num;
2582 if ((tmp & scratch) == 0)
2583 continue;
2584 scratch ^= tmp;
2586 ep = &dev->ep [num + 1];
2587 dma = ep->dma;
2589 if (!dma)
2590 continue;
2592 /* clear ep's dma status */
2593 tmp = readl (&dma->dmastat);
2594 writel (tmp, &dma->dmastat);
2596 /* chaining should stop on abort, short OUT from fifo,
2597 * or (stat0 codepath) short OUT transfer.
2599 if (!use_dma_chaining) {
2600 if ((tmp & (1 << DMA_TRANSACTION_DONE_INTERRUPT))
2601 == 0) {
2602 DEBUG (ep->dev, "%s no xact done? %08x\n",
2603 ep->ep.name, tmp);
2604 continue;
2606 stop_dma (ep->dma);
2609 /* OUT transfers terminate when the data from the
2610 * host is in our memory. Process whatever's done.
2611 * On this path, we know transfer's last packet wasn't
2612 * less than req->length. NAK_OUT_PACKETS may be set,
2613 * or the FIFO may already be holding new packets.
2615 * IN transfers can linger in the FIFO for a very
2616 * long time ... we ignore that for now, accounting
2617 * precisely (like PIO does) needs per-packet irqs
2619 scan_dma_completions (ep);
2621 /* disable dma on inactive queues; else maybe restart */
2622 if (list_empty (&ep->queue)) {
2623 if (use_dma_chaining)
2624 stop_dma (ep->dma);
2625 } else {
2626 tmp = readl (&dma->dmactl);
2627 if (!use_dma_chaining
2628 || (tmp & (1 << DMA_ENABLE)) == 0)
2629 restart_dma (ep);
2630 else if (ep->is_in && use_dma_chaining) {
2631 struct net2280_request *req;
2632 __le32 dmacount;
2634 /* the descriptor at the head of the chain
2635 * may still have VALID_BIT clear; that's
2636 * used to trigger changing DMA_FIFO_VALIDATE
2637 * (affects automagic zlp writes).
2639 req = list_entry (ep->queue.next,
2640 struct net2280_request, queue);
2641 dmacount = req->td->dmacount;
2642 dmacount &= __constant_cpu_to_le32 (
2643 (1 << VALID_BIT)
2644 | DMA_BYTE_COUNT_MASK);
2645 if (dmacount && (dmacount & valid_bit) == 0)
2646 restart_dma (ep);
2649 ep->irqs++;
2652 /* NOTE: there are other PCI errors we might usefully notice.
2653 * if they appear very often, here's where to try recovering.
2655 if (stat & PCI_ERROR_INTERRUPTS) {
2656 ERROR (dev, "pci dma error; stat %08x\n", stat);
2657 stat &= ~PCI_ERROR_INTERRUPTS;
2658 /* these are fatal errors, but "maybe" they won't
2659 * happen again ...
2661 stop_activity (dev, dev->driver);
2662 ep0_start (dev);
2663 stat = 0;
2666 if (stat)
2667 DEBUG (dev, "unhandled irqstat1 %08x\n", stat);
2670 static irqreturn_t net2280_irq (int irq, void *_dev)
2672 struct net2280 *dev = _dev;
2674 /* shared interrupt, not ours */
2675 if (!(readl(&dev->regs->irqstat0) & (1 << INTA_ASSERTED)))
2676 return IRQ_NONE;
2678 spin_lock (&dev->lock);
2680 /* handle disconnect, dma, and more */
2681 handle_stat1_irqs (dev, readl (&dev->regs->irqstat1));
2683 /* control requests and PIO */
2684 handle_stat0_irqs (dev, readl (&dev->regs->irqstat0));
2686 spin_unlock (&dev->lock);
2688 return IRQ_HANDLED;
2691 /*-------------------------------------------------------------------------*/
2693 static void gadget_release (struct device *_dev)
2695 struct net2280 *dev = dev_get_drvdata (_dev);
2697 kfree (dev);
2700 /* tear down the binding between this driver and the pci device */
2702 static void net2280_remove (struct pci_dev *pdev)
2704 struct net2280 *dev = pci_get_drvdata (pdev);
2706 BUG_ON(dev->driver);
2708 /* then clean up the resources we allocated during probe() */
2709 net2280_led_shutdown (dev);
2710 if (dev->requests) {
2711 int i;
2712 for (i = 1; i < 5; i++) {
2713 if (!dev->ep [i].dummy)
2714 continue;
2715 pci_pool_free (dev->requests, dev->ep [i].dummy,
2716 dev->ep [i].td_dma);
2718 pci_pool_destroy (dev->requests);
2720 if (dev->got_irq)
2721 free_irq (pdev->irq, dev);
2722 if (dev->regs)
2723 iounmap (dev->regs);
2724 if (dev->region)
2725 release_mem_region (pci_resource_start (pdev, 0),
2726 pci_resource_len (pdev, 0));
2727 if (dev->enabled)
2728 pci_disable_device (pdev);
2729 device_unregister (&dev->gadget.dev);
2730 device_remove_file (&pdev->dev, &dev_attr_registers);
2731 pci_set_drvdata (pdev, NULL);
2733 INFO (dev, "unbind\n");
2735 the_controller = NULL;
2738 /* wrap this driver around the specified device, but
2739 * don't respond over USB until a gadget driver binds to us.
2742 static int net2280_probe (struct pci_dev *pdev, const struct pci_device_id *id)
2744 struct net2280 *dev;
2745 unsigned long resource, len;
2746 void __iomem *base = NULL;
2747 int retval, i;
2749 /* if you want to support more than one controller in a system,
2750 * usb_gadget_driver_{register,unregister}() must change.
2752 if (the_controller) {
2753 dev_warn (&pdev->dev, "ignoring\n");
2754 return -EBUSY;
2757 /* alloc, and start init */
2758 dev = kzalloc (sizeof *dev, GFP_KERNEL);
2759 if (dev == NULL){
2760 retval = -ENOMEM;
2761 goto done;
2764 pci_set_drvdata (pdev, dev);
2765 spin_lock_init (&dev->lock);
2766 dev->pdev = pdev;
2767 dev->gadget.ops = &net2280_ops;
2768 dev->gadget.is_dualspeed = 1;
2770 /* the "gadget" abstracts/virtualizes the controller */
2771 strcpy (dev->gadget.dev.bus_id, "gadget");
2772 dev->gadget.dev.parent = &pdev->dev;
2773 dev->gadget.dev.dma_mask = pdev->dev.dma_mask;
2774 dev->gadget.dev.release = gadget_release;
2775 dev->gadget.name = driver_name;
2777 /* now all the pci goodies ... */
2778 if (pci_enable_device (pdev) < 0) {
2779 retval = -ENODEV;
2780 goto done;
2782 dev->enabled = 1;
2784 /* BAR 0 holds all the registers
2785 * BAR 1 is 8051 memory; unused here (note erratum 0103)
2786 * BAR 2 is fifo memory; unused here
2788 resource = pci_resource_start (pdev, 0);
2789 len = pci_resource_len (pdev, 0);
2790 if (!request_mem_region (resource, len, driver_name)) {
2791 DEBUG (dev, "controller already in use\n");
2792 retval = -EBUSY;
2793 goto done;
2795 dev->region = 1;
2797 /* FIXME provide firmware download interface to put
2798 * 8051 code into the chip, e.g. to turn on PCI PM.
2801 base = ioremap_nocache (resource, len);
2802 if (base == NULL) {
2803 DEBUG (dev, "can't map memory\n");
2804 retval = -EFAULT;
2805 goto done;
2807 dev->regs = (struct net2280_regs __iomem *) base;
2808 dev->usb = (struct net2280_usb_regs __iomem *) (base + 0x0080);
2809 dev->pci = (struct net2280_pci_regs __iomem *) (base + 0x0100);
2810 dev->dma = (struct net2280_dma_regs __iomem *) (base + 0x0180);
2811 dev->dep = (struct net2280_dep_regs __iomem *) (base + 0x0200);
2812 dev->epregs = (struct net2280_ep_regs __iomem *) (base + 0x0300);
2814 /* put into initial config, link up all endpoints */
2815 writel (0, &dev->usb->usbctl);
2816 usb_reset (dev);
2817 usb_reinit (dev);
2819 /* irq setup after old hardware is cleaned up */
2820 if (!pdev->irq) {
2821 ERROR (dev, "No IRQ. Check PCI setup!\n");
2822 retval = -ENODEV;
2823 goto done;
2826 if (request_irq (pdev->irq, net2280_irq, IRQF_SHARED, driver_name, dev)
2827 != 0) {
2828 ERROR (dev, "request interrupt %d failed\n", pdev->irq);
2829 retval = -EBUSY;
2830 goto done;
2832 dev->got_irq = 1;
2834 /* DMA setup */
2835 /* NOTE: we know only the 32 LSBs of dma addresses may be nonzero */
2836 dev->requests = pci_pool_create ("requests", pdev,
2837 sizeof (struct net2280_dma),
2838 0 /* no alignment requirements */,
2839 0 /* or page-crossing issues */);
2840 if (!dev->requests) {
2841 DEBUG (dev, "can't get request pool\n");
2842 retval = -ENOMEM;
2843 goto done;
2845 for (i = 1; i < 5; i++) {
2846 struct net2280_dma *td;
2848 td = pci_pool_alloc (dev->requests, GFP_KERNEL,
2849 &dev->ep [i].td_dma);
2850 if (!td) {
2851 DEBUG (dev, "can't get dummy %d\n", i);
2852 retval = -ENOMEM;
2853 goto done;
2855 td->dmacount = 0; /* not VALID */
2856 td->dmaaddr = __constant_cpu_to_le32 (DMA_ADDR_INVALID);
2857 td->dmadesc = td->dmaaddr;
2858 dev->ep [i].dummy = td;
2861 /* enable lower-overhead pci memory bursts during DMA */
2862 writel ( (1 << DMA_MEMORY_WRITE_AND_INVALIDATE_ENABLE)
2863 // 256 write retries may not be enough...
2864 // | (1 << PCI_RETRY_ABORT_ENABLE)
2865 | (1 << DMA_READ_MULTIPLE_ENABLE)
2866 | (1 << DMA_READ_LINE_ENABLE)
2867 , &dev->pci->pcimstctl);
2868 /* erratum 0115 shouldn't appear: Linux inits PCI_LATENCY_TIMER */
2869 pci_set_master (pdev);
2870 pci_try_set_mwi (pdev);
2872 /* ... also flushes any posted pci writes */
2873 dev->chiprev = get_idx_reg (dev->regs, REG_CHIPREV) & 0xffff;
2875 /* done */
2876 INFO (dev, "%s\n", driver_desc);
2877 INFO (dev, "irq %d, pci mem %p, chip rev %04x\n",
2878 pdev->irq, base, dev->chiprev);
2879 INFO (dev, "version: " DRIVER_VERSION "; dma %s\n",
2880 use_dma
2881 ? (use_dma_chaining ? "chaining" : "enabled")
2882 : "disabled");
2883 the_controller = dev;
2885 retval = device_register (&dev->gadget.dev);
2886 if (retval) goto done;
2887 retval = device_create_file (&pdev->dev, &dev_attr_registers);
2888 if (retval) goto done;
2890 return 0;
2892 done:
2893 if (dev)
2894 net2280_remove (pdev);
2895 return retval;
2898 /* make sure the board is quiescent; otherwise it will continue
2899 * generating IRQs across the upcoming reboot.
2902 static void net2280_shutdown (struct pci_dev *pdev)
2904 struct net2280 *dev = pci_get_drvdata (pdev);
2906 /* disable IRQs */
2907 writel (0, &dev->regs->pciirqenb0);
2908 writel (0, &dev->regs->pciirqenb1);
2910 /* disable the pullup so the host will think we're gone */
2911 writel (0, &dev->usb->usbctl);
2915 /*-------------------------------------------------------------------------*/
2917 static const struct pci_device_id pci_ids [] = { {
2918 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
2919 .class_mask = ~0,
2920 .vendor = 0x17cc,
2921 .device = 0x2280,
2922 .subvendor = PCI_ANY_ID,
2923 .subdevice = PCI_ANY_ID,
2924 }, {
2925 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
2926 .class_mask = ~0,
2927 .vendor = 0x17cc,
2928 .device = 0x2282,
2929 .subvendor = PCI_ANY_ID,
2930 .subdevice = PCI_ANY_ID,
2932 }, { /* end: all zeroes */ }
2934 MODULE_DEVICE_TABLE (pci, pci_ids);
2936 /* pci driver glue; this is a "new style" PCI driver module */
2937 static struct pci_driver net2280_pci_driver = {
2938 .name = (char *) driver_name,
2939 .id_table = pci_ids,
2941 .probe = net2280_probe,
2942 .remove = net2280_remove,
2943 .shutdown = net2280_shutdown,
2945 /* FIXME add power management support */
2948 MODULE_DESCRIPTION (DRIVER_DESC);
2949 MODULE_AUTHOR ("David Brownell");
2950 MODULE_LICENSE ("GPL");
2952 static int __init init (void)
2954 if (!use_dma)
2955 use_dma_chaining = 0;
2956 return pci_register_driver (&net2280_pci_driver);
2958 module_init (init);
2960 static void __exit cleanup (void)
2962 pci_unregister_driver (&net2280_pci_driver);
2964 module_exit (cleanup);