2 * Driver for the PLX NET2280 USB device controller.
3 * Specs and errata are available from <http://www.plxtech.com>.
5 * PLX Technology Inc. (formerly NetChip Technology) supported the
6 * development of this driver.
9 * CODE STATUS HIGHLIGHTS
11 * This driver should work well with most "gadget" drivers, including
12 * the Mass Storage, Serial, and Ethernet/RNDIS gadget drivers
13 * as well as Gadget Zero and Gadgetfs.
15 * DMA is enabled by default. Drivers using transfer queues might use
16 * DMA chaining to remove IRQ latencies between transfers. (Except when
17 * short OUT transfers happen.) Drivers can use the req->no_interrupt
18 * hint to completely eliminate some IRQs, if a later IRQ is guaranteed
19 * and DMA chaining is enabled.
21 * Note that almost all the errata workarounds here are only needed for
22 * rev1 chips. Rev1a silicon (0110) fixes almost all of them.
26 * Copyright (C) 2003 David Brownell
27 * Copyright (C) 2003-2005 PLX Technology, Inc.
29 * Modified Seth Levy 2005 PLX Technology, Inc. to provide compatibility
32 * This program is free software; you can redistribute it and/or modify
33 * it under the terms of the GNU General Public License as published by
34 * the Free Software Foundation; either version 2 of the License, or
35 * (at your option) any later version.
38 #undef DEBUG /* messages on error and most fault paths */
39 #undef VERBOSE /* extra debug messages (success too) */
41 #include <linux/module.h>
42 #include <linux/pci.h>
43 #include <linux/dma-mapping.h>
44 #include <linux/kernel.h>
45 #include <linux/delay.h>
46 #include <linux/ioport.h>
47 #include <linux/slab.h>
48 #include <linux/errno.h>
49 #include <linux/init.h>
50 #include <linux/timer.h>
51 #include <linux/list.h>
52 #include <linux/interrupt.h>
53 #include <linux/moduleparam.h>
54 #include <linux/device.h>
55 #include <linux/usb/ch9.h>
56 #include <linux/usb/gadget.h>
57 #include <linux/prefetch.h>
59 #include <asm/byteorder.h>
62 #include <asm/unaligned.h>
65 #define DRIVER_DESC "PLX NET228x USB Peripheral Controller"
66 #define DRIVER_VERSION "2005 Sept 27"
68 #define DMA_ADDR_INVALID (~(dma_addr_t)0)
69 #define EP_DONTUSE 13 /* nonzero */
71 #define USE_RDK_LEDS /* GPIO pins control three LEDs */
74 static const char driver_name
[] = "net2280";
75 static const char driver_desc
[] = DRIVER_DESC
;
77 static const char ep0name
[] = "ep0";
78 static const char *const ep_name
[] = {
80 "ep-a", "ep-b", "ep-c", "ep-d",
84 /* use_dma -- general goodness, fewer interrupts, less cpu load (vs PIO)
85 * use_dma_chaining -- dma descriptor queueing gives even more irq reduction
87 * The net2280 DMA engines are not tightly integrated with their FIFOs;
88 * not all cases are (yet) handled well in this driver or the silicon.
89 * Some gadget drivers work better with the dma support here than others.
90 * These two parameters let you use PIO or more aggressive DMA.
92 static bool use_dma
= 1;
93 static bool use_dma_chaining
= 0;
95 /* "modprobe net2280 use_dma=n" etc */
96 module_param (use_dma
, bool, S_IRUGO
);
97 module_param (use_dma_chaining
, bool, S_IRUGO
);
100 /* mode 0 == ep-{a,b,c,d} 1K fifo each
101 * mode 1 == ep-{a,b} 2K fifo each, ep-{c,d} unavailable
102 * mode 2 == ep-a 2K fifo, ep-{b,c} 1K each, ep-d unavailable
104 static ushort fifo_mode
= 0;
106 /* "modprobe net2280 fifo_mode=1" etc */
107 module_param (fifo_mode
, ushort
, 0644);
109 /* enable_suspend -- When enabled, the driver will respond to
110 * USB suspend requests by powering down the NET2280. Otherwise,
111 * USB suspend requests will be ignored. This is acceptable for
112 * self-powered devices
114 static bool enable_suspend
= 0;
116 /* "modprobe net2280 enable_suspend=1" etc */
117 module_param (enable_suspend
, bool, S_IRUGO
);
119 /* force full-speed operation */
120 static bool full_speed
;
121 module_param(full_speed
, bool, 0444);
122 MODULE_PARM_DESC(full_speed
, "force full-speed mode -- for testing only!");
124 #define DIR_STRING(bAddress) (((bAddress) & USB_DIR_IN) ? "in" : "out")
126 #if defined(CONFIG_USB_GADGET_DEBUG_FILES) || defined (DEBUG)
127 static char *type_string (u8 bmAttributes
)
129 switch ((bmAttributes
) & USB_ENDPOINT_XFERTYPE_MASK
) {
130 case USB_ENDPOINT_XFER_BULK
: return "bulk";
131 case USB_ENDPOINT_XFER_ISOC
: return "iso";
132 case USB_ENDPOINT_XFER_INT
: return "intr";
140 #define valid_bit cpu_to_le32 (1 << VALID_BIT)
141 #define dma_done_ie cpu_to_le32 (1 << DMA_DONE_INTERRUPT_ENABLE)
143 /*-------------------------------------------------------------------------*/
146 net2280_enable (struct usb_ep
*_ep
, const struct usb_endpoint_descriptor
*desc
)
149 struct net2280_ep
*ep
;
153 ep
= container_of (_ep
, struct net2280_ep
, ep
);
154 if (!_ep
|| !desc
|| ep
->desc
|| _ep
->name
== ep0name
155 || desc
->bDescriptorType
!= USB_DT_ENDPOINT
)
158 if (!dev
->driver
|| dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
161 /* erratum 0119 workaround ties up an endpoint number */
162 if ((desc
->bEndpointAddress
& 0x0f) == EP_DONTUSE
)
165 /* sanity check ep-e/ep-f since their fifos are small */
166 max
= usb_endpoint_maxp (desc
) & 0x1fff;
167 if (ep
->num
> 4 && max
> 64)
170 spin_lock_irqsave (&dev
->lock
, flags
);
171 _ep
->maxpacket
= max
& 0x7ff;
174 /* ep_reset() has already been called */
177 ep
->out_overflow
= 0;
179 /* set speed-dependent max packet; may kick in high bandwidth */
180 set_idx_reg (dev
->regs
, REG_EP_MAXPKT (dev
, ep
->num
), max
);
182 /* FIFO lines can't go to different packets. PIO is ok, so
183 * use it instead of troublesome (non-bulk) multi-packet DMA.
185 if (ep
->dma
&& (max
% 4) != 0 && use_dma_chaining
) {
186 DEBUG (ep
->dev
, "%s, no dma for maxpacket %d\n",
187 ep
->ep
.name
, ep
->ep
.maxpacket
);
191 /* set type, direction, address; reset fifo counters */
192 writel ((1 << FIFO_FLUSH
), &ep
->regs
->ep_stat
);
193 tmp
= (desc
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
);
194 if (tmp
== USB_ENDPOINT_XFER_INT
) {
195 /* erratum 0105 workaround prevents hs NYET */
196 if (dev
->chiprev
== 0100
197 && dev
->gadget
.speed
== USB_SPEED_HIGH
198 && !(desc
->bEndpointAddress
& USB_DIR_IN
))
199 writel ((1 << CLEAR_NAK_OUT_PACKETS_MODE
),
201 } else if (tmp
== USB_ENDPOINT_XFER_BULK
) {
202 /* catch some particularly blatant driver bugs */
203 if ((dev
->gadget
.speed
== USB_SPEED_HIGH
205 || (dev
->gadget
.speed
== USB_SPEED_FULL
207 spin_unlock_irqrestore (&dev
->lock
, flags
);
211 ep
->is_iso
= (tmp
== USB_ENDPOINT_XFER_ISOC
) ? 1 : 0;
212 tmp
<<= ENDPOINT_TYPE
;
213 tmp
|= desc
->bEndpointAddress
;
214 tmp
|= (4 << ENDPOINT_BYTE_COUNT
); /* default full fifo lines */
215 tmp
|= 1 << ENDPOINT_ENABLE
;
218 /* for OUT transfers, block the rx fifo until a read is posted */
219 ep
->is_in
= (tmp
& USB_DIR_IN
) != 0;
221 writel ((1 << SET_NAK_OUT_PACKETS
), &ep
->regs
->ep_rsp
);
222 else if (dev
->pdev
->device
!= 0x2280) {
223 /* Added for 2282, Don't use nak packets on an in endpoint,
224 * this was ignored on 2280
226 writel ((1 << CLEAR_NAK_OUT_PACKETS
)
227 | (1 << CLEAR_NAK_OUT_PACKETS_MODE
), &ep
->regs
->ep_rsp
);
230 writel (tmp
, &ep
->regs
->ep_cfg
);
233 if (!ep
->dma
) { /* pio, per-packet */
234 tmp
= (1 << ep
->num
) | readl (&dev
->regs
->pciirqenb0
);
235 writel (tmp
, &dev
->regs
->pciirqenb0
);
237 tmp
= (1 << DATA_PACKET_RECEIVED_INTERRUPT_ENABLE
)
238 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT_ENABLE
);
239 if (dev
->pdev
->device
== 0x2280)
240 tmp
|= readl (&ep
->regs
->ep_irqenb
);
241 writel (tmp
, &ep
->regs
->ep_irqenb
);
242 } else { /* dma, per-request */
243 tmp
= (1 << (8 + ep
->num
)); /* completion */
244 tmp
|= readl (&dev
->regs
->pciirqenb1
);
245 writel (tmp
, &dev
->regs
->pciirqenb1
);
247 /* for short OUT transfers, dma completions can't
248 * advance the queue; do it pio-style, by hand.
249 * NOTE erratum 0112 workaround #2
251 if ((desc
->bEndpointAddress
& USB_DIR_IN
) == 0) {
252 tmp
= (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT_ENABLE
);
253 writel (tmp
, &ep
->regs
->ep_irqenb
);
255 tmp
= (1 << ep
->num
) | readl (&dev
->regs
->pciirqenb0
);
256 writel (tmp
, &dev
->regs
->pciirqenb0
);
260 tmp
= desc
->bEndpointAddress
;
261 DEBUG (dev
, "enabled %s (ep%d%s-%s) %s max %04x\n",
262 _ep
->name
, tmp
& 0x0f, DIR_STRING (tmp
),
263 type_string (desc
->bmAttributes
),
264 ep
->dma
? "dma" : "pio", max
);
266 /* pci writes may still be posted */
267 spin_unlock_irqrestore (&dev
->lock
, flags
);
271 static int handshake (u32 __iomem
*ptr
, u32 mask
, u32 done
, int usec
)
276 result
= readl (ptr
);
277 if (result
== ~(u32
)0) /* "device unplugged" */
288 static const struct usb_ep_ops net2280_ep_ops
;
290 static void ep_reset (struct net2280_regs __iomem
*regs
, struct net2280_ep
*ep
)
295 INIT_LIST_HEAD (&ep
->queue
);
297 ep
->ep
.maxpacket
= ~0;
298 ep
->ep
.ops
= &net2280_ep_ops
;
300 /* disable the dma, irqs, endpoint... */
302 writel (0, &ep
->dma
->dmactl
);
303 writel ( (1 << DMA_SCATTER_GATHER_DONE_INTERRUPT
)
304 | (1 << DMA_TRANSACTION_DONE_INTERRUPT
)
306 , &ep
->dma
->dmastat
);
308 tmp
= readl (®s
->pciirqenb0
);
309 tmp
&= ~(1 << ep
->num
);
310 writel (tmp
, ®s
->pciirqenb0
);
312 tmp
= readl (®s
->pciirqenb1
);
313 tmp
&= ~(1 << (8 + ep
->num
)); /* completion */
314 writel (tmp
, ®s
->pciirqenb1
);
316 writel (0, &ep
->regs
->ep_irqenb
);
318 /* init to our chosen defaults, notably so that we NAK OUT
319 * packets until the driver queues a read (+note erratum 0112)
321 if (!ep
->is_in
|| ep
->dev
->pdev
->device
== 0x2280) {
322 tmp
= (1 << SET_NAK_OUT_PACKETS_MODE
)
323 | (1 << SET_NAK_OUT_PACKETS
)
324 | (1 << CLEAR_EP_HIDE_STATUS_PHASE
)
325 | (1 << CLEAR_INTERRUPT_MODE
);
328 tmp
= (1 << CLEAR_NAK_OUT_PACKETS_MODE
)
329 | (1 << CLEAR_NAK_OUT_PACKETS
)
330 | (1 << CLEAR_EP_HIDE_STATUS_PHASE
)
331 | (1 << CLEAR_INTERRUPT_MODE
);
335 tmp
|= (1 << CLEAR_ENDPOINT_TOGGLE
)
336 | (1 << CLEAR_ENDPOINT_HALT
);
338 writel (tmp
, &ep
->regs
->ep_rsp
);
340 /* scrub most status bits, and flush any fifo state */
341 if (ep
->dev
->pdev
->device
== 0x2280)
342 tmp
= (1 << FIFO_OVERFLOW
)
343 | (1 << FIFO_UNDERFLOW
);
347 writel (tmp
| (1 << TIMEOUT
)
348 | (1 << USB_STALL_SENT
)
349 | (1 << USB_IN_NAK_SENT
)
350 | (1 << USB_IN_ACK_RCVD
)
351 | (1 << USB_OUT_PING_NAK_SENT
)
352 | (1 << USB_OUT_ACK_SENT
)
354 | (1 << SHORT_PACKET_OUT_DONE_INTERRUPT
)
355 | (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT
)
356 | (1 << DATA_PACKET_RECEIVED_INTERRUPT
)
357 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT
)
358 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT
)
359 | (1 << DATA_IN_TOKEN_INTERRUPT
)
360 , &ep
->regs
->ep_stat
);
362 /* fifo size is handled separately */
365 static void nuke (struct net2280_ep
*);
367 static int net2280_disable (struct usb_ep
*_ep
)
369 struct net2280_ep
*ep
;
372 ep
= container_of (_ep
, struct net2280_ep
, ep
);
373 if (!_ep
|| !ep
->desc
|| _ep
->name
== ep0name
)
376 spin_lock_irqsave (&ep
->dev
->lock
, flags
);
378 ep_reset (ep
->dev
->regs
, ep
);
380 VDEBUG (ep
->dev
, "disabled %s %s\n",
381 ep
->dma
? "dma" : "pio", _ep
->name
);
383 /* synch memory views with the device */
384 (void) readl (&ep
->regs
->ep_cfg
);
386 if (use_dma
&& !ep
->dma
&& ep
->num
>= 1 && ep
->num
<= 4)
387 ep
->dma
= &ep
->dev
->dma
[ep
->num
- 1];
389 spin_unlock_irqrestore (&ep
->dev
->lock
, flags
);
393 /*-------------------------------------------------------------------------*/
395 static struct usb_request
*
396 net2280_alloc_request (struct usb_ep
*_ep
, gfp_t gfp_flags
)
398 struct net2280_ep
*ep
;
399 struct net2280_request
*req
;
403 ep
= container_of (_ep
, struct net2280_ep
, ep
);
405 req
= kzalloc(sizeof(*req
), gfp_flags
);
409 req
->req
.dma
= DMA_ADDR_INVALID
;
410 INIT_LIST_HEAD (&req
->queue
);
412 /* this dma descriptor may be swapped with the previous dummy */
414 struct net2280_dma
*td
;
416 td
= pci_pool_alloc (ep
->dev
->requests
, gfp_flags
,
422 td
->dmacount
= 0; /* not VALID */
423 td
->dmaaddr
= cpu_to_le32 (DMA_ADDR_INVALID
);
424 td
->dmadesc
= td
->dmaaddr
;
431 net2280_free_request (struct usb_ep
*_ep
, struct usb_request
*_req
)
433 struct net2280_ep
*ep
;
434 struct net2280_request
*req
;
436 ep
= container_of (_ep
, struct net2280_ep
, ep
);
440 req
= container_of (_req
, struct net2280_request
, req
);
441 WARN_ON (!list_empty (&req
->queue
));
443 pci_pool_free (ep
->dev
->requests
, req
->td
, req
->td_dma
);
447 /*-------------------------------------------------------------------------*/
449 /* load a packet into the fifo we use for usb IN transfers.
450 * works for all endpoints.
452 * NOTE: pio with ep-a..ep-d could stuff multiple packets into the fifo
453 * at a time, but this code is simpler because it knows it only writes
454 * one packet. ep-a..ep-d should use dma instead.
457 write_fifo (struct net2280_ep
*ep
, struct usb_request
*req
)
459 struct net2280_ep_regs __iomem
*regs
= ep
->regs
;
462 unsigned count
, total
;
464 /* INVARIANT: fifo is currently empty. (testable) */
467 buf
= req
->buf
+ req
->actual
;
469 total
= req
->length
- req
->actual
;
475 /* write just one packet at a time */
476 count
= ep
->ep
.maxpacket
;
477 if (count
> total
) /* min() cannot be used on a bitfield */
480 VDEBUG (ep
->dev
, "write %s fifo (IN) %d bytes%s req %p\n",
482 (count
!= ep
->ep
.maxpacket
) ? " (short)" : "",
485 /* NOTE be careful if you try to align these. fifo lines
486 * should normally be full (4 bytes) and successive partial
487 * lines are ok only in certain cases.
489 tmp
= get_unaligned ((u32
*)buf
);
491 writel (tmp
, ®s
->ep_data
);
496 /* last fifo entry is "short" unless we wrote a full packet.
497 * also explicitly validate last word in (periodic) transfers
498 * when maxpacket is not a multiple of 4 bytes.
500 if (count
|| total
< ep
->ep
.maxpacket
) {
501 tmp
= count
? get_unaligned ((u32
*)buf
) : count
;
503 set_fifo_bytecount (ep
, count
& 0x03);
504 writel (tmp
, ®s
->ep_data
);
507 /* pci writes may still be posted */
510 /* work around erratum 0106: PCI and USB race over the OUT fifo.
511 * caller guarantees chiprev 0100, out endpoint is NAKing, and
512 * there's no real data in the fifo.
514 * NOTE: also used in cases where that erratum doesn't apply:
515 * where the host wrote "too much" data to us.
517 static void out_flush (struct net2280_ep
*ep
)
522 ASSERT_OUT_NAKING (ep
);
524 statp
= &ep
->regs
->ep_stat
;
525 writel ( (1 << DATA_OUT_PING_TOKEN_INTERRUPT
)
526 | (1 << DATA_PACKET_RECEIVED_INTERRUPT
)
528 writel ((1 << FIFO_FLUSH
), statp
);
531 if (tmp
& (1 << DATA_OUT_PING_TOKEN_INTERRUPT
)
532 /* high speed did bulk NYET; fifo isn't filling */
533 && ep
->dev
->gadget
.speed
== USB_SPEED_FULL
) {
536 usec
= 50; /* 64 byte bulk/interrupt */
537 handshake (statp
, (1 << USB_OUT_PING_NAK_SENT
),
538 (1 << USB_OUT_PING_NAK_SENT
), usec
);
539 /* NAK done; now CLEAR_NAK_OUT_PACKETS is safe */
543 /* unload packet(s) from the fifo we use for usb OUT transfers.
544 * returns true iff the request completed, because of short packet
545 * or the request buffer having filled with full packets.
547 * for ep-a..ep-d this will read multiple packets out when they
548 * have been accepted.
551 read_fifo (struct net2280_ep
*ep
, struct net2280_request
*req
)
553 struct net2280_ep_regs __iomem
*regs
= ep
->regs
;
554 u8
*buf
= req
->req
.buf
+ req
->req
.actual
;
555 unsigned count
, tmp
, is_short
;
556 unsigned cleanup
= 0, prevent
= 0;
558 /* erratum 0106 ... packets coming in during fifo reads might
559 * be incompletely rejected. not all cases have workarounds.
561 if (ep
->dev
->chiprev
== 0x0100
562 && ep
->dev
->gadget
.speed
== USB_SPEED_FULL
) {
564 tmp
= readl (&ep
->regs
->ep_stat
);
565 if ((tmp
& (1 << NAK_OUT_PACKETS
)))
567 else if ((tmp
& (1 << FIFO_FULL
))) {
568 start_out_naking (ep
);
571 /* else: hope we don't see the problem */
574 /* never overflow the rx buffer. the fifo reads packets until
575 * it sees a short one; we might not be ready for them all.
578 count
= readl (®s
->ep_avail
);
579 if (unlikely (count
== 0)) {
581 tmp
= readl (&ep
->regs
->ep_stat
);
582 count
= readl (®s
->ep_avail
);
583 /* handled that data already? */
584 if (count
== 0 && (tmp
& (1 << NAK_OUT_PACKETS
)) == 0)
588 tmp
= req
->req
.length
- req
->req
.actual
;
590 /* as with DMA, data overflow gets flushed */
591 if ((tmp
% ep
->ep
.maxpacket
) != 0) {
593 "%s out fifo %d bytes, expected %d\n",
594 ep
->ep
.name
, count
, tmp
);
595 req
->req
.status
= -EOVERFLOW
;
597 /* NAK_OUT_PACKETS will be set, so flushing is safe;
598 * the next read will start with the next packet
600 } /* else it's a ZLP, no worries */
603 req
->req
.actual
+= count
;
605 is_short
= (count
== 0) || ((count
% ep
->ep
.maxpacket
) != 0);
607 VDEBUG (ep
->dev
, "read %s fifo (OUT) %d bytes%s%s%s req %p %d/%d\n",
608 ep
->ep
.name
, count
, is_short
? " (short)" : "",
609 cleanup
? " flush" : "", prevent
? " nak" : "",
610 req
, req
->req
.actual
, req
->req
.length
);
613 tmp
= readl (®s
->ep_data
);
615 put_unaligned (tmp
, (u32
*)buf
);
620 tmp
= readl (®s
->ep_data
);
621 /* LE conversion is implicit here: */
630 writel ((1 << CLEAR_NAK_OUT_PACKETS
), &ep
->regs
->ep_rsp
);
631 (void) readl (&ep
->regs
->ep_rsp
);
634 return is_short
|| ((req
->req
.actual
== req
->req
.length
)
638 /* fill out dma descriptor to match a given request */
640 fill_dma_desc (struct net2280_ep
*ep
, struct net2280_request
*req
, int valid
)
642 struct net2280_dma
*td
= req
->td
;
643 u32 dmacount
= req
->req
.length
;
645 /* don't let DMA continue after a short OUT packet,
646 * so overruns can't affect the next transfer.
647 * in case of overruns on max-size packets, we can't
648 * stop the fifo from filling but we can flush it.
651 dmacount
|= (1 << DMA_DIRECTION
);
652 if ((!ep
->is_in
&& (dmacount
% ep
->ep
.maxpacket
) != 0)
653 || ep
->dev
->pdev
->device
!= 0x2280)
654 dmacount
|= (1 << END_OF_CHAIN
);
658 dmacount
|= (1 << VALID_BIT
);
659 if (likely(!req
->req
.no_interrupt
|| !use_dma_chaining
))
660 dmacount
|= (1 << DMA_DONE_INTERRUPT_ENABLE
);
662 /* td->dmadesc = previously set by caller */
663 td
->dmaaddr
= cpu_to_le32 (req
->req
.dma
);
665 /* 2280 may be polling VALID_BIT through ep->dma->dmadesc */
667 td
->dmacount
= cpu_to_le32(dmacount
);
670 static const u32 dmactl_default
=
671 (1 << DMA_SCATTER_GATHER_DONE_INTERRUPT
)
672 | (1 << DMA_CLEAR_COUNT_ENABLE
)
673 /* erratum 0116 workaround part 1 (use POLLING) */
674 | (POLL_100_USEC
<< DESCRIPTOR_POLLING_RATE
)
675 | (1 << DMA_VALID_BIT_POLLING_ENABLE
)
676 | (1 << DMA_VALID_BIT_ENABLE
)
677 | (1 << DMA_SCATTER_GATHER_ENABLE
)
678 /* erratum 0116 workaround part 2 (no AUTOSTART) */
681 static inline void spin_stop_dma (struct net2280_dma_regs __iomem
*dma
)
683 handshake (&dma
->dmactl
, (1 << DMA_ENABLE
), 0, 50);
686 static inline void stop_dma (struct net2280_dma_regs __iomem
*dma
)
688 writel (readl (&dma
->dmactl
) & ~(1 << DMA_ENABLE
), &dma
->dmactl
);
692 static void start_queue (struct net2280_ep
*ep
, u32 dmactl
, u32 td_dma
)
694 struct net2280_dma_regs __iomem
*dma
= ep
->dma
;
695 unsigned int tmp
= (1 << VALID_BIT
) | (ep
->is_in
<< DMA_DIRECTION
);
697 if (ep
->dev
->pdev
->device
!= 0x2280)
698 tmp
|= (1 << END_OF_CHAIN
);
700 writel (tmp
, &dma
->dmacount
);
701 writel (readl (&dma
->dmastat
), &dma
->dmastat
);
703 writel (td_dma
, &dma
->dmadesc
);
704 writel (dmactl
, &dma
->dmactl
);
706 /* erratum 0116 workaround part 3: pci arbiter away from net2280 */
707 (void) readl (&ep
->dev
->pci
->pcimstctl
);
709 writel ((1 << DMA_START
), &dma
->dmastat
);
712 stop_out_naking (ep
);
715 static void start_dma (struct net2280_ep
*ep
, struct net2280_request
*req
)
718 struct net2280_dma_regs __iomem
*dma
= ep
->dma
;
720 /* FIXME can't use DMA for ZLPs */
722 /* on this path we "know" there's no dma active (yet) */
723 WARN_ON (readl (&dma
->dmactl
) & (1 << DMA_ENABLE
));
724 writel (0, &ep
->dma
->dmactl
);
726 /* previous OUT packet might have been short */
727 if (!ep
->is_in
&& ((tmp
= readl (&ep
->regs
->ep_stat
))
728 & (1 << NAK_OUT_PACKETS
)) != 0) {
729 writel ((1 << SHORT_PACKET_TRANSFERRED_INTERRUPT
),
732 tmp
= readl (&ep
->regs
->ep_avail
);
734 writel (readl (&dma
->dmastat
), &dma
->dmastat
);
736 /* transfer all/some fifo data */
737 writel (req
->req
.dma
, &dma
->dmaaddr
);
738 tmp
= min (tmp
, req
->req
.length
);
740 /* dma irq, faking scatterlist status */
741 req
->td
->dmacount
= cpu_to_le32 (req
->req
.length
- tmp
);
742 writel ((1 << DMA_DONE_INTERRUPT_ENABLE
)
743 | tmp
, &dma
->dmacount
);
744 req
->td
->dmadesc
= 0;
747 writel ((1 << DMA_ENABLE
), &dma
->dmactl
);
748 writel ((1 << DMA_START
), &dma
->dmastat
);
753 tmp
= dmactl_default
;
755 /* force packet boundaries between dma requests, but prevent the
756 * controller from automagically writing a last "short" packet
757 * (zero length) unless the driver explicitly said to do that.
760 if (likely ((req
->req
.length
% ep
->ep
.maxpacket
) != 0
762 tmp
|= (1 << DMA_FIFO_VALIDATE
);
763 ep
->in_fifo_validate
= 1;
765 ep
->in_fifo_validate
= 0;
768 /* init req->td, pointing to the current dummy */
769 req
->td
->dmadesc
= cpu_to_le32 (ep
->td_dma
);
770 fill_dma_desc (ep
, req
, 1);
772 if (!use_dma_chaining
)
773 req
->td
->dmacount
|= cpu_to_le32 (1 << END_OF_CHAIN
);
775 start_queue (ep
, tmp
, req
->td_dma
);
779 queue_dma (struct net2280_ep
*ep
, struct net2280_request
*req
, int valid
)
781 struct net2280_dma
*end
;
784 /* swap new dummy for old, link; fill and maybe activate */
790 ep
->td_dma
= req
->td_dma
;
793 end
->dmadesc
= cpu_to_le32 (ep
->td_dma
);
795 fill_dma_desc (ep
, req
, valid
);
799 done (struct net2280_ep
*ep
, struct net2280_request
*req
, int status
)
802 unsigned stopped
= ep
->stopped
;
804 list_del_init (&req
->queue
);
806 if (req
->req
.status
== -EINPROGRESS
)
807 req
->req
.status
= status
;
809 status
= req
->req
.status
;
813 usb_gadget_unmap_request(&dev
->gadget
, &req
->req
, ep
->is_in
);
815 if (status
&& status
!= -ESHUTDOWN
)
816 VDEBUG (dev
, "complete %s req %p stat %d len %u/%u\n",
817 ep
->ep
.name
, &req
->req
, status
,
818 req
->req
.actual
, req
->req
.length
);
820 /* don't modify queue heads during completion callback */
822 spin_unlock (&dev
->lock
);
823 req
->req
.complete (&ep
->ep
, &req
->req
);
824 spin_lock (&dev
->lock
);
825 ep
->stopped
= stopped
;
828 /*-------------------------------------------------------------------------*/
831 net2280_queue (struct usb_ep
*_ep
, struct usb_request
*_req
, gfp_t gfp_flags
)
833 struct net2280_request
*req
;
834 struct net2280_ep
*ep
;
838 /* we always require a cpu-view buffer, so that we can
839 * always use pio (as fallback or whatever).
841 req
= container_of (_req
, struct net2280_request
, req
);
842 if (!_req
|| !_req
->complete
|| !_req
->buf
843 || !list_empty (&req
->queue
))
845 if (_req
->length
> (~0 & DMA_BYTE_COUNT_MASK
))
847 ep
= container_of (_ep
, struct net2280_ep
, ep
);
848 if (!_ep
|| (!ep
->desc
&& ep
->num
!= 0))
851 if (!dev
->driver
|| dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
854 /* FIXME implement PIO fallback for ZLPs with DMA */
855 if (ep
->dma
&& _req
->length
== 0)
858 /* set up dma mapping in case the caller didn't */
862 ret
= usb_gadget_map_request(&dev
->gadget
, _req
,
869 VDEBUG (dev
, "%s queue req %p, len %d buf %p\n",
870 _ep
->name
, _req
, _req
->length
, _req
->buf
);
873 spin_lock_irqsave (&dev
->lock
, flags
);
875 _req
->status
= -EINPROGRESS
;
878 /* kickstart this i/o queue? */
879 if (list_empty (&ep
->queue
) && !ep
->stopped
) {
880 /* use DMA if the endpoint supports it, else pio */
884 /* maybe there's no control data, just status ack */
885 if (ep
->num
== 0 && _req
->length
== 0) {
888 VDEBUG (dev
, "%s status ack\n", ep
->ep
.name
);
892 /* PIO ... stuff the fifo, or unblock it. */
894 write_fifo (ep
, _req
);
895 else if (list_empty (&ep
->queue
)) {
898 /* OUT FIFO might have packet(s) buffered */
899 s
= readl (&ep
->regs
->ep_stat
);
900 if ((s
& (1 << FIFO_EMPTY
)) == 0) {
901 /* note: _req->short_not_ok is
902 * ignored here since PIO _always_
903 * stops queue advance here, and
904 * _req->status doesn't change for
905 * short reads (only _req->actual)
907 if (read_fifo (ep
, req
)) {
914 s
= readl (&ep
->regs
->ep_stat
);
917 /* don't NAK, let the fifo fill */
918 if (req
&& (s
& (1 << NAK_OUT_PACKETS
)))
919 writel ((1 << CLEAR_NAK_OUT_PACKETS
),
924 } else if (ep
->dma
) {
930 /* preventing magic zlps is per-engine state, not
931 * per-transfer; irq logic must recover hiccups.
933 expect
= likely (req
->req
.zero
934 || (req
->req
.length
% ep
->ep
.maxpacket
) != 0);
935 if (expect
!= ep
->in_fifo_validate
)
938 queue_dma (ep
, req
, valid
);
940 } /* else the irq handler advances the queue. */
944 list_add_tail (&req
->queue
, &ep
->queue
);
946 spin_unlock_irqrestore (&dev
->lock
, flags
);
948 /* pci writes may still be posted */
954 struct net2280_ep
*ep
,
955 struct net2280_request
*req
,
960 req
->req
.actual
= req
->req
.length
- (DMA_BYTE_COUNT_MASK
& dmacount
);
961 done (ep
, req
, status
);
964 static void restart_dma (struct net2280_ep
*ep
);
966 static void scan_dma_completions (struct net2280_ep
*ep
)
968 /* only look at descriptors that were "naturally" retired,
969 * so fifo and list head state won't matter
971 while (!list_empty (&ep
->queue
)) {
972 struct net2280_request
*req
;
975 req
= list_entry (ep
->queue
.next
,
976 struct net2280_request
, queue
);
980 tmp
= le32_to_cpup (&req
->td
->dmacount
);
981 if ((tmp
& (1 << VALID_BIT
)) != 0)
984 /* SHORT_PACKET_TRANSFERRED_INTERRUPT handles "usb-short"
985 * cases where DMA must be aborted; this code handles
986 * all non-abort DMA completions.
988 if (unlikely (req
->td
->dmadesc
== 0)) {
990 tmp
= readl (&ep
->dma
->dmacount
);
991 if (tmp
& DMA_BYTE_COUNT_MASK
)
993 /* single transfer mode */
994 dma_done (ep
, req
, tmp
, 0);
996 } else if (!ep
->is_in
997 && (req
->req
.length
% ep
->ep
.maxpacket
) != 0) {
998 tmp
= readl (&ep
->regs
->ep_stat
);
1000 /* AVOID TROUBLE HERE by not issuing short reads from
1001 * your gadget driver. That helps avoids errata 0121,
1002 * 0122, and 0124; not all cases trigger the warning.
1004 if ((tmp
& (1 << NAK_OUT_PACKETS
)) == 0) {
1005 WARNING (ep
->dev
, "%s lost packet sync!\n",
1007 req
->req
.status
= -EOVERFLOW
;
1008 } else if ((tmp
= readl (&ep
->regs
->ep_avail
)) != 0) {
1009 /* fifo gets flushed later */
1010 ep
->out_overflow
= 1;
1011 DEBUG (ep
->dev
, "%s dma, discard %d len %d\n",
1014 req
->req
.status
= -EOVERFLOW
;
1017 dma_done (ep
, req
, tmp
, 0);
1021 static void restart_dma (struct net2280_ep
*ep
)
1023 struct net2280_request
*req
;
1024 u32 dmactl
= dmactl_default
;
1028 req
= list_entry (ep
->queue
.next
, struct net2280_request
, queue
);
1030 if (!use_dma_chaining
) {
1031 start_dma (ep
, req
);
1035 /* the 2280 will be processing the queue unless queue hiccups after
1036 * the previous transfer:
1037 * IN: wanted automagic zlp, head doesn't (or vice versa)
1038 * DMA_FIFO_VALIDATE doesn't init from dma descriptors.
1039 * OUT: was "usb-short", we must restart.
1041 if (ep
->is_in
&& !req
->valid
) {
1042 struct net2280_request
*entry
, *prev
= NULL
;
1043 int reqmode
, done
= 0;
1045 DEBUG (ep
->dev
, "%s dma hiccup td %p\n", ep
->ep
.name
, req
->td
);
1046 ep
->in_fifo_validate
= likely (req
->req
.zero
1047 || (req
->req
.length
% ep
->ep
.maxpacket
) != 0);
1048 if (ep
->in_fifo_validate
)
1049 dmactl
|= (1 << DMA_FIFO_VALIDATE
);
1050 list_for_each_entry (entry
, &ep
->queue
, queue
) {
1055 dmacount
= entry
->td
->dmacount
;
1057 reqmode
= likely (entry
->req
.zero
1058 || (entry
->req
.length
1059 % ep
->ep
.maxpacket
) != 0);
1060 if (reqmode
== ep
->in_fifo_validate
) {
1062 dmacount
|= valid_bit
;
1063 entry
->td
->dmacount
= dmacount
;
1067 /* force a hiccup */
1068 prev
->td
->dmacount
|= dma_done_ie
;
1073 /* walk the rest of the queue so unlinks behave */
1075 dmacount
&= ~valid_bit
;
1076 entry
->td
->dmacount
= dmacount
;
1081 writel (0, &ep
->dma
->dmactl
);
1082 start_queue (ep
, dmactl
, req
->td_dma
);
1085 static void abort_dma (struct net2280_ep
*ep
)
1087 /* abort the current transfer */
1088 if (likely (!list_empty (&ep
->queue
))) {
1089 /* FIXME work around errata 0121, 0122, 0124 */
1090 writel ((1 << DMA_ABORT
), &ep
->dma
->dmastat
);
1091 spin_stop_dma (ep
->dma
);
1094 scan_dma_completions (ep
);
1097 /* dequeue ALL requests */
1098 static void nuke (struct net2280_ep
*ep
)
1100 struct net2280_request
*req
;
1102 /* called with spinlock held */
1106 while (!list_empty (&ep
->queue
)) {
1107 req
= list_entry (ep
->queue
.next
,
1108 struct net2280_request
,
1110 done (ep
, req
, -ESHUTDOWN
);
1114 /* dequeue JUST ONE request */
1115 static int net2280_dequeue (struct usb_ep
*_ep
, struct usb_request
*_req
)
1117 struct net2280_ep
*ep
;
1118 struct net2280_request
*req
;
1119 unsigned long flags
;
1123 ep
= container_of (_ep
, struct net2280_ep
, ep
);
1124 if (!_ep
|| (!ep
->desc
&& ep
->num
!= 0) || !_req
)
1127 spin_lock_irqsave (&ep
->dev
->lock
, flags
);
1128 stopped
= ep
->stopped
;
1130 /* quiesce dma while we patch the queue */
1134 dmactl
= readl (&ep
->dma
->dmactl
);
1135 /* WARNING erratum 0127 may kick in ... */
1137 scan_dma_completions (ep
);
1140 /* make sure it's still queued on this endpoint */
1141 list_for_each_entry (req
, &ep
->queue
, queue
) {
1142 if (&req
->req
== _req
)
1145 if (&req
->req
!= _req
) {
1146 spin_unlock_irqrestore (&ep
->dev
->lock
, flags
);
1150 /* queue head may be partially complete. */
1151 if (ep
->queue
.next
== &req
->queue
) {
1153 DEBUG (ep
->dev
, "unlink (%s) dma\n", _ep
->name
);
1154 _req
->status
= -ECONNRESET
;
1156 if (likely (ep
->queue
.next
== &req
->queue
)) {
1157 // NOTE: misreports single-transfer mode
1158 req
->td
->dmacount
= 0; /* invalidate */
1160 readl (&ep
->dma
->dmacount
),
1164 DEBUG (ep
->dev
, "unlink (%s) pio\n", _ep
->name
);
1165 done (ep
, req
, -ECONNRESET
);
1169 /* patch up hardware chaining data */
1170 } else if (ep
->dma
&& use_dma_chaining
) {
1171 if (req
->queue
.prev
== ep
->queue
.next
) {
1172 writel (le32_to_cpu (req
->td
->dmadesc
),
1174 if (req
->td
->dmacount
& dma_done_ie
)
1175 writel (readl (&ep
->dma
->dmacount
)
1176 | le32_to_cpu(dma_done_ie
),
1177 &ep
->dma
->dmacount
);
1179 struct net2280_request
*prev
;
1181 prev
= list_entry (req
->queue
.prev
,
1182 struct net2280_request
, queue
);
1183 prev
->td
->dmadesc
= req
->td
->dmadesc
;
1184 if (req
->td
->dmacount
& dma_done_ie
)
1185 prev
->td
->dmacount
|= dma_done_ie
;
1190 done (ep
, req
, -ECONNRESET
);
1191 ep
->stopped
= stopped
;
1194 /* turn off dma on inactive queues */
1195 if (list_empty (&ep
->queue
))
1197 else if (!ep
->stopped
) {
1198 /* resume current request, or start new one */
1200 writel (dmactl
, &ep
->dma
->dmactl
);
1202 start_dma (ep
, list_entry (ep
->queue
.next
,
1203 struct net2280_request
, queue
));
1207 spin_unlock_irqrestore (&ep
->dev
->lock
, flags
);
1211 /*-------------------------------------------------------------------------*/
1213 static int net2280_fifo_status (struct usb_ep
*_ep
);
1216 net2280_set_halt_and_wedge(struct usb_ep
*_ep
, int value
, int wedged
)
1218 struct net2280_ep
*ep
;
1219 unsigned long flags
;
1222 ep
= container_of (_ep
, struct net2280_ep
, ep
);
1223 if (!_ep
|| (!ep
->desc
&& ep
->num
!= 0))
1225 if (!ep
->dev
->driver
|| ep
->dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
1227 if (ep
->desc
/* not ep0 */ && (ep
->desc
->bmAttributes
& 0x03)
1228 == USB_ENDPOINT_XFER_ISOC
)
1231 spin_lock_irqsave (&ep
->dev
->lock
, flags
);
1232 if (!list_empty (&ep
->queue
))
1234 else if (ep
->is_in
&& value
&& net2280_fifo_status (_ep
) != 0)
1237 VDEBUG (ep
->dev
, "%s %s %s\n", _ep
->name
,
1238 value
? "set" : "clear",
1239 wedged
? "wedge" : "halt");
1240 /* set/clear, then synch memory views with the device */
1243 ep
->dev
->protocol_stall
= 1;
1252 (void) readl (&ep
->regs
->ep_rsp
);
1254 spin_unlock_irqrestore (&ep
->dev
->lock
, flags
);
1260 net2280_set_halt(struct usb_ep
*_ep
, int value
)
1262 return net2280_set_halt_and_wedge(_ep
, value
, 0);
1266 net2280_set_wedge(struct usb_ep
*_ep
)
1268 if (!_ep
|| _ep
->name
== ep0name
)
1270 return net2280_set_halt_and_wedge(_ep
, 1, 1);
1274 net2280_fifo_status (struct usb_ep
*_ep
)
1276 struct net2280_ep
*ep
;
1279 ep
= container_of (_ep
, struct net2280_ep
, ep
);
1280 if (!_ep
|| (!ep
->desc
&& ep
->num
!= 0))
1282 if (!ep
->dev
->driver
|| ep
->dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
1285 avail
= readl (&ep
->regs
->ep_avail
) & ((1 << 12) - 1);
1286 if (avail
> ep
->fifo_size
)
1289 avail
= ep
->fifo_size
- avail
;
1294 net2280_fifo_flush (struct usb_ep
*_ep
)
1296 struct net2280_ep
*ep
;
1298 ep
= container_of (_ep
, struct net2280_ep
, ep
);
1299 if (!_ep
|| (!ep
->desc
&& ep
->num
!= 0))
1301 if (!ep
->dev
->driver
|| ep
->dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
1304 writel ((1 << FIFO_FLUSH
), &ep
->regs
->ep_stat
);
1305 (void) readl (&ep
->regs
->ep_rsp
);
1308 static const struct usb_ep_ops net2280_ep_ops
= {
1309 .enable
= net2280_enable
,
1310 .disable
= net2280_disable
,
1312 .alloc_request
= net2280_alloc_request
,
1313 .free_request
= net2280_free_request
,
1315 .queue
= net2280_queue
,
1316 .dequeue
= net2280_dequeue
,
1318 .set_halt
= net2280_set_halt
,
1319 .set_wedge
= net2280_set_wedge
,
1320 .fifo_status
= net2280_fifo_status
,
1321 .fifo_flush
= net2280_fifo_flush
,
1324 /*-------------------------------------------------------------------------*/
1326 static int net2280_get_frame (struct usb_gadget
*_gadget
)
1328 struct net2280
*dev
;
1329 unsigned long flags
;
1334 dev
= container_of (_gadget
, struct net2280
, gadget
);
1335 spin_lock_irqsave (&dev
->lock
, flags
);
1336 retval
= get_idx_reg (dev
->regs
, REG_FRAME
) & 0x03ff;
1337 spin_unlock_irqrestore (&dev
->lock
, flags
);
1341 static int net2280_wakeup (struct usb_gadget
*_gadget
)
1343 struct net2280
*dev
;
1345 unsigned long flags
;
1349 dev
= container_of (_gadget
, struct net2280
, gadget
);
1351 spin_lock_irqsave (&dev
->lock
, flags
);
1352 tmp
= readl (&dev
->usb
->usbctl
);
1353 if (tmp
& (1 << DEVICE_REMOTE_WAKEUP_ENABLE
))
1354 writel (1 << GENERATE_RESUME
, &dev
->usb
->usbstat
);
1355 spin_unlock_irqrestore (&dev
->lock
, flags
);
1357 /* pci writes may still be posted */
1361 static int net2280_set_selfpowered (struct usb_gadget
*_gadget
, int value
)
1363 struct net2280
*dev
;
1365 unsigned long flags
;
1369 dev
= container_of (_gadget
, struct net2280
, gadget
);
1371 spin_lock_irqsave (&dev
->lock
, flags
);
1372 tmp
= readl (&dev
->usb
->usbctl
);
1374 tmp
|= (1 << SELF_POWERED_STATUS
);
1376 tmp
&= ~(1 << SELF_POWERED_STATUS
);
1377 writel (tmp
, &dev
->usb
->usbctl
);
1378 spin_unlock_irqrestore (&dev
->lock
, flags
);
1383 static int net2280_pullup(struct usb_gadget
*_gadget
, int is_on
)
1385 struct net2280
*dev
;
1387 unsigned long flags
;
1391 dev
= container_of (_gadget
, struct net2280
, gadget
);
1393 spin_lock_irqsave (&dev
->lock
, flags
);
1394 tmp
= readl (&dev
->usb
->usbctl
);
1395 dev
->softconnect
= (is_on
!= 0);
1397 tmp
|= (1 << USB_DETECT_ENABLE
);
1399 tmp
&= ~(1 << USB_DETECT_ENABLE
);
1400 writel (tmp
, &dev
->usb
->usbctl
);
1401 spin_unlock_irqrestore (&dev
->lock
, flags
);
1406 static int net2280_start(struct usb_gadget
*_gadget
,
1407 struct usb_gadget_driver
*driver
);
1408 static int net2280_stop(struct usb_gadget
*_gadget
,
1409 struct usb_gadget_driver
*driver
);
1411 static const struct usb_gadget_ops net2280_ops
= {
1412 .get_frame
= net2280_get_frame
,
1413 .wakeup
= net2280_wakeup
,
1414 .set_selfpowered
= net2280_set_selfpowered
,
1415 .pullup
= net2280_pullup
,
1416 .udc_start
= net2280_start
,
1417 .udc_stop
= net2280_stop
,
1420 /*-------------------------------------------------------------------------*/
1422 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1424 /* FIXME move these into procfs, and use seq_file.
1425 * Sysfs _still_ doesn't behave for arbitrarily sized files,
1426 * and also doesn't help products using this with 2.4 kernels.
1429 /* "function" sysfs attribute */
1431 show_function (struct device
*_dev
, struct device_attribute
*attr
, char *buf
)
1433 struct net2280
*dev
= dev_get_drvdata (_dev
);
1436 || !dev
->driver
->function
1437 || strlen (dev
->driver
->function
) > PAGE_SIZE
)
1439 return scnprintf (buf
, PAGE_SIZE
, "%s\n", dev
->driver
->function
);
1441 static DEVICE_ATTR (function
, S_IRUGO
, show_function
, NULL
);
1443 static ssize_t
net2280_show_registers(struct device
*_dev
,
1444 struct device_attribute
*attr
, char *buf
)
1446 struct net2280
*dev
;
1449 unsigned long flags
;
1454 dev
= dev_get_drvdata (_dev
);
1457 spin_lock_irqsave (&dev
->lock
, flags
);
1460 s
= dev
->driver
->driver
.name
;
1464 /* Main Control Registers */
1465 t
= scnprintf (next
, size
, "%s version " DRIVER_VERSION
1466 ", chiprev %04x, dma %s\n\n"
1467 "devinit %03x fifoctl %08x gadget '%s'\n"
1468 "pci irqenb0 %02x irqenb1 %08x "
1469 "irqstat0 %04x irqstat1 %08x\n",
1470 driver_name
, dev
->chiprev
,
1472 ? (use_dma_chaining
? "chaining" : "enabled")
1474 readl (&dev
->regs
->devinit
),
1475 readl (&dev
->regs
->fifoctl
),
1477 readl (&dev
->regs
->pciirqenb0
),
1478 readl (&dev
->regs
->pciirqenb1
),
1479 readl (&dev
->regs
->irqstat0
),
1480 readl (&dev
->regs
->irqstat1
));
1484 /* USB Control Registers */
1485 t1
= readl (&dev
->usb
->usbctl
);
1486 t2
= readl (&dev
->usb
->usbstat
);
1487 if (t1
& (1 << VBUS_PIN
)) {
1488 if (t2
& (1 << HIGH_SPEED
))
1490 else if (dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
1494 /* full speed bit (6) not working?? */
1497 t
= scnprintf (next
, size
,
1498 "stdrsp %08x usbctl %08x usbstat %08x "
1499 "addr 0x%02x (%s)\n",
1500 readl (&dev
->usb
->stdrsp
), t1
, t2
,
1501 readl (&dev
->usb
->ouraddr
), s
);
1505 /* PCI Master Control Registers */
1507 /* DMA Control Registers */
1509 /* Configurable EP Control Registers */
1510 for (i
= 0; i
< 7; i
++) {
1511 struct net2280_ep
*ep
;
1517 t1
= readl (&ep
->regs
->ep_cfg
);
1518 t2
= readl (&ep
->regs
->ep_rsp
) & 0xff;
1519 t
= scnprintf (next
, size
,
1520 "\n%s\tcfg %05x rsp (%02x) %s%s%s%s%s%s%s%s"
1522 ep
->ep
.name
, t1
, t2
,
1523 (t2
& (1 << CLEAR_NAK_OUT_PACKETS
))
1525 (t2
& (1 << CLEAR_EP_HIDE_STATUS_PHASE
))
1527 (t2
& (1 << CLEAR_EP_FORCE_CRC_ERROR
))
1529 (t2
& (1 << CLEAR_INTERRUPT_MODE
))
1530 ? "interrupt " : "",
1531 (t2
& (1<<CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE
))
1533 (t2
& (1 << CLEAR_NAK_OUT_PACKETS_MODE
))
1535 (t2
& (1 << CLEAR_ENDPOINT_TOGGLE
))
1536 ? "DATA1 " : "DATA0 ",
1537 (t2
& (1 << CLEAR_ENDPOINT_HALT
))
1539 readl (&ep
->regs
->ep_irqenb
));
1543 t
= scnprintf (next
, size
,
1544 "\tstat %08x avail %04x "
1546 readl (&ep
->regs
->ep_stat
),
1547 readl (&ep
->regs
->ep_avail
),
1548 t1
& 0x0f, DIR_STRING (t1
),
1549 type_string (t1
>> 8),
1550 ep
->stopped
? "*" : "");
1557 t
= scnprintf (next
, size
,
1558 " dma\tctl %08x stat %08x count %08x\n"
1559 "\taddr %08x desc %08x\n",
1560 readl (&ep
->dma
->dmactl
),
1561 readl (&ep
->dma
->dmastat
),
1562 readl (&ep
->dma
->dmacount
),
1563 readl (&ep
->dma
->dmaaddr
),
1564 readl (&ep
->dma
->dmadesc
));
1570 /* Indexed Registers */
1574 t
= scnprintf (next
, size
, "\nirqs: ");
1577 for (i
= 0; i
< 7; i
++) {
1578 struct net2280_ep
*ep
;
1583 t
= scnprintf (next
, size
, " %s/%lu", ep
->ep
.name
, ep
->irqs
);
1588 t
= scnprintf (next
, size
, "\n");
1592 spin_unlock_irqrestore (&dev
->lock
, flags
);
1594 return PAGE_SIZE
- size
;
1596 static DEVICE_ATTR(registers
, S_IRUGO
, net2280_show_registers
, NULL
);
1599 show_queues (struct device
*_dev
, struct device_attribute
*attr
, char *buf
)
1601 struct net2280
*dev
;
1604 unsigned long flags
;
1607 dev
= dev_get_drvdata (_dev
);
1610 spin_lock_irqsave (&dev
->lock
, flags
);
1612 for (i
= 0; i
< 7; i
++) {
1613 struct net2280_ep
*ep
= &dev
->ep
[i
];
1614 struct net2280_request
*req
;
1618 const struct usb_endpoint_descriptor
*d
;
1623 t
= d
->bEndpointAddress
;
1624 t
= scnprintf (next
, size
,
1625 "\n%s (ep%d%s-%s) max %04x %s fifo %d\n",
1626 ep
->ep
.name
, t
& USB_ENDPOINT_NUMBER_MASK
,
1627 (t
& USB_DIR_IN
) ? "in" : "out",
1629 switch (d
->bmAttributes
& 0x03) {
1630 case USB_ENDPOINT_XFER_BULK
:
1631 val
= "bulk"; break;
1632 case USB_ENDPOINT_XFER_INT
:
1633 val
= "intr"; break;
1637 usb_endpoint_maxp (d
) & 0x1fff,
1638 ep
->dma
? "dma" : "pio", ep
->fifo_size
1640 } else /* ep0 should only have one transfer queued */
1641 t
= scnprintf (next
, size
, "ep0 max 64 pio %s\n",
1642 ep
->is_in
? "in" : "out");
1643 if (t
<= 0 || t
> size
)
1648 if (list_empty (&ep
->queue
)) {
1649 t
= scnprintf (next
, size
, "\t(nothing queued)\n");
1650 if (t
<= 0 || t
> size
)
1656 list_for_each_entry (req
, &ep
->queue
, queue
) {
1657 if (ep
->dma
&& req
->td_dma
== readl (&ep
->dma
->dmadesc
))
1658 t
= scnprintf (next
, size
,
1659 "\treq %p len %d/%d "
1660 "buf %p (dmacount %08x)\n",
1661 &req
->req
, req
->req
.actual
,
1662 req
->req
.length
, req
->req
.buf
,
1663 readl (&ep
->dma
->dmacount
));
1665 t
= scnprintf (next
, size
,
1666 "\treq %p len %d/%d buf %p\n",
1667 &req
->req
, req
->req
.actual
,
1668 req
->req
.length
, req
->req
.buf
);
1669 if (t
<= 0 || t
> size
)
1675 struct net2280_dma
*td
;
1678 t
= scnprintf (next
, size
, "\t td %08x "
1679 " count %08x buf %08x desc %08x\n",
1681 le32_to_cpu (td
->dmacount
),
1682 le32_to_cpu (td
->dmaaddr
),
1683 le32_to_cpu (td
->dmadesc
));
1684 if (t
<= 0 || t
> size
)
1693 spin_unlock_irqrestore (&dev
->lock
, flags
);
1694 return PAGE_SIZE
- size
;
1696 static DEVICE_ATTR (queues
, S_IRUGO
, show_queues
, NULL
);
1701 #define device_create_file(a,b) (0)
1702 #define device_remove_file(a,b) do { } while (0)
1706 /*-------------------------------------------------------------------------*/
1708 /* another driver-specific mode might be a request type doing dma
1709 * to/from another device fifo instead of to/from memory.
1712 static void set_fifo_mode (struct net2280
*dev
, int mode
)
1714 /* keeping high bits preserves BAR2 */
1715 writel ((0xffff << PCI_BASE2_RANGE
) | mode
, &dev
->regs
->fifoctl
);
1717 /* always ep-{a,b,e,f} ... maybe not ep-c or ep-d */
1718 INIT_LIST_HEAD (&dev
->gadget
.ep_list
);
1719 list_add_tail (&dev
->ep
[1].ep
.ep_list
, &dev
->gadget
.ep_list
);
1720 list_add_tail (&dev
->ep
[2].ep
.ep_list
, &dev
->gadget
.ep_list
);
1723 list_add_tail (&dev
->ep
[3].ep
.ep_list
, &dev
->gadget
.ep_list
);
1724 list_add_tail (&dev
->ep
[4].ep
.ep_list
, &dev
->gadget
.ep_list
);
1725 dev
->ep
[1].fifo_size
= dev
->ep
[2].fifo_size
= 1024;
1728 dev
->ep
[1].fifo_size
= dev
->ep
[2].fifo_size
= 2048;
1731 list_add_tail (&dev
->ep
[3].ep
.ep_list
, &dev
->gadget
.ep_list
);
1732 dev
->ep
[1].fifo_size
= 2048;
1733 dev
->ep
[2].fifo_size
= 1024;
1736 /* fifo sizes for ep0, ep-c, ep-d, ep-e, and ep-f never change */
1737 list_add_tail (&dev
->ep
[5].ep
.ep_list
, &dev
->gadget
.ep_list
);
1738 list_add_tail (&dev
->ep
[6].ep
.ep_list
, &dev
->gadget
.ep_list
);
1741 /* keeping it simple:
1742 * - one bus driver, initted first;
1743 * - one function driver, initted second
1745 * most of the work to support multiple net2280 controllers would
1746 * be to associate this gadget driver (yes?) with all of them, or
1747 * perhaps to bind specific drivers to specific devices.
1750 static void usb_reset (struct net2280
*dev
)
1754 dev
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1755 (void) readl (&dev
->usb
->usbctl
);
1757 net2280_led_init (dev
);
1759 /* disable automatic responses, and irqs */
1760 writel (0, &dev
->usb
->stdrsp
);
1761 writel (0, &dev
->regs
->pciirqenb0
);
1762 writel (0, &dev
->regs
->pciirqenb1
);
1764 /* clear old dma and irq state */
1765 for (tmp
= 0; tmp
< 4; tmp
++) {
1766 struct net2280_ep
*ep
= &dev
->ep
[tmp
+ 1];
1771 writel (~0, &dev
->regs
->irqstat0
),
1772 writel (~(1 << SUSPEND_REQUEST_INTERRUPT
), &dev
->regs
->irqstat1
),
1774 /* reset, and enable pci */
1775 tmp
= readl (&dev
->regs
->devinit
)
1777 | (1 << FIFO_SOFT_RESET
)
1778 | (1 << USB_SOFT_RESET
)
1779 | (1 << M8051_RESET
);
1780 writel (tmp
, &dev
->regs
->devinit
);
1782 /* standard fifo and endpoint allocations */
1783 set_fifo_mode (dev
, (fifo_mode
<= 2) ? fifo_mode
: 0);
1786 static void usb_reinit (struct net2280
*dev
)
1791 /* use_dma changes are ignored till next device re-init */
1794 /* basic endpoint init */
1795 for (tmp
= 0; tmp
< 7; tmp
++) {
1796 struct net2280_ep
*ep
= &dev
->ep
[tmp
];
1798 ep
->ep
.name
= ep_name
[tmp
];
1802 if (tmp
> 0 && tmp
<= 4) {
1803 ep
->fifo_size
= 1024;
1805 ep
->dma
= &dev
->dma
[tmp
- 1];
1808 ep
->regs
= &dev
->epregs
[tmp
];
1809 ep_reset (dev
->regs
, ep
);
1811 dev
->ep
[0].ep
.maxpacket
= 64;
1812 dev
->ep
[5].ep
.maxpacket
= 64;
1813 dev
->ep
[6].ep
.maxpacket
= 64;
1815 dev
->gadget
.ep0
= &dev
->ep
[0].ep
;
1816 dev
->ep
[0].stopped
= 0;
1817 INIT_LIST_HEAD (&dev
->gadget
.ep0
->ep_list
);
1819 /* we want to prevent lowlevel/insecure access from the USB host,
1820 * but erratum 0119 means this enable bit is ignored
1822 for (tmp
= 0; tmp
< 5; tmp
++)
1823 writel (EP_DONTUSE
, &dev
->dep
[tmp
].dep_cfg
);
1826 static void ep0_start (struct net2280
*dev
)
1828 writel ( (1 << CLEAR_EP_HIDE_STATUS_PHASE
)
1829 | (1 << CLEAR_NAK_OUT_PACKETS
)
1830 | (1 << CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE
)
1831 , &dev
->epregs
[0].ep_rsp
);
1834 * hardware optionally handles a bunch of standard requests
1835 * that the API hides from drivers anyway. have it do so.
1836 * endpoint status/features are handled in software, to
1837 * help pass tests for some dubious behavior.
1839 writel ( (1 << SET_TEST_MODE
)
1840 | (1 << SET_ADDRESS
)
1841 | (1 << DEVICE_SET_CLEAR_DEVICE_REMOTE_WAKEUP
)
1842 | (1 << GET_DEVICE_STATUS
)
1843 | (1 << GET_INTERFACE_STATUS
)
1844 , &dev
->usb
->stdrsp
);
1845 writel ( (1 << USB_ROOT_PORT_WAKEUP_ENABLE
)
1846 | (1 << SELF_POWERED_USB_DEVICE
)
1847 | (1 << REMOTE_WAKEUP_SUPPORT
)
1848 | (dev
->softconnect
<< USB_DETECT_ENABLE
)
1849 | (1 << SELF_POWERED_STATUS
)
1850 , &dev
->usb
->usbctl
);
1852 /* enable irqs so we can see ep0 and general operation */
1853 writel ( (1 << SETUP_PACKET_INTERRUPT_ENABLE
)
1854 | (1 << ENDPOINT_0_INTERRUPT_ENABLE
)
1855 , &dev
->regs
->pciirqenb0
);
1856 writel ( (1 << PCI_INTERRUPT_ENABLE
)
1857 | (1 << PCI_MASTER_ABORT_RECEIVED_INTERRUPT_ENABLE
)
1858 | (1 << PCI_TARGET_ABORT_RECEIVED_INTERRUPT_ENABLE
)
1859 | (1 << PCI_RETRY_ABORT_INTERRUPT_ENABLE
)
1860 | (1 << VBUS_INTERRUPT_ENABLE
)
1861 | (1 << ROOT_PORT_RESET_INTERRUPT_ENABLE
)
1862 | (1 << SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE
)
1863 , &dev
->regs
->pciirqenb1
);
1865 /* don't leave any writes posted */
1866 (void) readl (&dev
->usb
->usbctl
);
1869 /* when a driver is successfully registered, it will receive
1870 * control requests including set_configuration(), which enables
1871 * non-control requests. then usb traffic follows until a
1872 * disconnect is reported. then a host may connect again, or
1873 * the driver might get unbound.
1875 static int net2280_start(struct usb_gadget
*_gadget
,
1876 struct usb_gadget_driver
*driver
)
1878 struct net2280
*dev
;
1882 /* insist on high speed support from the driver, since
1883 * (dev->usb->xcvrdiag & FORCE_FULL_SPEED_MODE)
1884 * "must not be used in normal operation"
1886 if (!driver
|| driver
->max_speed
< USB_SPEED_HIGH
1890 dev
= container_of (_gadget
, struct net2280
, gadget
);
1892 for (i
= 0; i
< 7; i
++)
1893 dev
->ep
[i
].irqs
= 0;
1895 /* hook up the driver ... */
1896 dev
->softconnect
= 1;
1897 driver
->driver
.bus
= NULL
;
1898 dev
->driver
= driver
;
1899 dev
->gadget
.dev
.driver
= &driver
->driver
;
1901 retval
= device_create_file (&dev
->pdev
->dev
, &dev_attr_function
);
1902 if (retval
) goto err_unbind
;
1903 retval
= device_create_file (&dev
->pdev
->dev
, &dev_attr_queues
);
1904 if (retval
) goto err_func
;
1906 /* Enable force-full-speed testing mode, if desired */
1908 writel(1 << FORCE_FULL_SPEED_MODE
, &dev
->usb
->xcvrdiag
);
1910 /* ... then enable host detection and ep0; and we're ready
1911 * for set_configuration as well as eventual disconnect.
1913 net2280_led_active (dev
, 1);
1916 DEBUG (dev
, "%s ready, usbctl %08x stdrsp %08x\n",
1917 driver
->driver
.name
,
1918 readl (&dev
->usb
->usbctl
),
1919 readl (&dev
->usb
->stdrsp
));
1921 /* pci writes may still be posted */
1925 device_remove_file (&dev
->pdev
->dev
, &dev_attr_function
);
1927 driver
->unbind (&dev
->gadget
);
1928 dev
->gadget
.dev
.driver
= NULL
;
1934 stop_activity (struct net2280
*dev
, struct usb_gadget_driver
*driver
)
1938 /* don't disconnect if it's not connected */
1939 if (dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
1942 /* stop hardware; prevent new request submissions;
1943 * and kill any outstanding requests.
1946 for (i
= 0; i
< 7; i
++)
1947 nuke (&dev
->ep
[i
]);
1952 static int net2280_stop(struct usb_gadget
*_gadget
,
1953 struct usb_gadget_driver
*driver
)
1955 struct net2280
*dev
;
1956 unsigned long flags
;
1958 dev
= container_of (_gadget
, struct net2280
, gadget
);
1960 spin_lock_irqsave (&dev
->lock
, flags
);
1961 stop_activity (dev
, driver
);
1962 spin_unlock_irqrestore (&dev
->lock
, flags
);
1964 dev
->gadget
.dev
.driver
= NULL
;
1967 net2280_led_active (dev
, 0);
1969 /* Disable full-speed test mode */
1970 writel(0, &dev
->usb
->xcvrdiag
);
1972 device_remove_file (&dev
->pdev
->dev
, &dev_attr_function
);
1973 device_remove_file (&dev
->pdev
->dev
, &dev_attr_queues
);
1975 DEBUG (dev
, "unregistered driver '%s'\n", driver
->driver
.name
);
1979 /*-------------------------------------------------------------------------*/
1981 /* handle ep0, ep-e, ep-f with 64 byte packets: packet per irq.
1982 * also works for dma-capable endpoints, in pio mode or just
1983 * to manually advance the queue after short OUT transfers.
1985 static void handle_ep_small (struct net2280_ep
*ep
)
1987 struct net2280_request
*req
;
1989 /* 0 error, 1 mid-data, 2 done */
1992 if (!list_empty (&ep
->queue
))
1993 req
= list_entry (ep
->queue
.next
,
1994 struct net2280_request
, queue
);
1998 /* ack all, and handle what we care about */
1999 t
= readl (&ep
->regs
->ep_stat
);
2002 VDEBUG (ep
->dev
, "%s ack ep_stat %08x, req %p\n",
2003 ep
->ep
.name
, t
, req
? &req
->req
: 0);
2005 if (!ep
->is_in
|| ep
->dev
->pdev
->device
== 0x2280)
2006 writel (t
& ~(1 << NAK_OUT_PACKETS
), &ep
->regs
->ep_stat
);
2008 /* Added for 2282 */
2009 writel (t
, &ep
->regs
->ep_stat
);
2011 /* for ep0, monitor token irqs to catch data stage length errors
2012 * and to synchronize on status.
2014 * also, to defer reporting of protocol stalls ... here's where
2015 * data or status first appears, handling stalls here should never
2016 * cause trouble on the host side..
2018 * control requests could be slightly faster without token synch for
2019 * status, but status can jam up that way.
2021 if (unlikely (ep
->num
== 0)) {
2023 /* status; stop NAKing */
2024 if (t
& (1 << DATA_OUT_PING_TOKEN_INTERRUPT
)) {
2025 if (ep
->dev
->protocol_stall
) {
2032 /* reply to extra IN data tokens with a zlp */
2033 } else if (t
& (1 << DATA_IN_TOKEN_INTERRUPT
)) {
2034 if (ep
->dev
->protocol_stall
) {
2038 } else if (ep
->responded
&&
2039 !req
&& !ep
->stopped
)
2040 write_fifo (ep
, NULL
);
2043 /* status; stop NAKing */
2044 if (t
& (1 << DATA_IN_TOKEN_INTERRUPT
)) {
2045 if (ep
->dev
->protocol_stall
) {
2050 /* an extra OUT token is an error */
2051 } else if (((t
& (1 << DATA_OUT_PING_TOKEN_INTERRUPT
))
2053 && req
->req
.actual
== req
->req
.length
)
2054 || (ep
->responded
&& !req
)) {
2055 ep
->dev
->protocol_stall
= 1;
2059 done (ep
, req
, -EOVERFLOW
);
2065 if (unlikely (!req
))
2068 /* manual DMA queue advance after short OUT */
2069 if (likely (ep
->dma
!= 0)) {
2070 if (t
& (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT
)) {
2072 int stopped
= ep
->stopped
;
2074 /* TRANSFERRED works around OUT_DONE erratum 0112.
2075 * we expect (N <= maxpacket) bytes; host wrote M.
2076 * iff (M < N) we won't ever see a DMA interrupt.
2079 for (count
= 0; ; t
= readl (&ep
->regs
->ep_stat
)) {
2081 /* any preceding dma transfers must finish.
2082 * dma handles (M >= N), may empty the queue
2084 scan_dma_completions (ep
);
2085 if (unlikely (list_empty (&ep
->queue
)
2086 || ep
->out_overflow
)) {
2090 req
= list_entry (ep
->queue
.next
,
2091 struct net2280_request
, queue
);
2093 /* here either (M < N), a "real" short rx;
2094 * or (M == N) and the queue didn't empty
2096 if (likely (t
& (1 << FIFO_EMPTY
))) {
2097 count
= readl (&ep
->dma
->dmacount
);
2098 count
&= DMA_BYTE_COUNT_MASK
;
2099 if (readl (&ep
->dma
->dmadesc
)
2107 /* stop DMA, leave ep NAKing */
2108 writel ((1 << DMA_ABORT
), &ep
->dma
->dmastat
);
2109 spin_stop_dma (ep
->dma
);
2112 req
->td
->dmacount
= 0;
2113 t
= readl (&ep
->regs
->ep_avail
);
2114 dma_done (ep
, req
, count
,
2115 (ep
->out_overflow
|| t
)
2119 /* also flush to prevent erratum 0106 trouble */
2120 if (unlikely (ep
->out_overflow
2121 || (ep
->dev
->chiprev
== 0x0100
2122 && ep
->dev
->gadget
.speed
2123 == USB_SPEED_FULL
))) {
2125 ep
->out_overflow
= 0;
2128 /* (re)start dma if needed, stop NAKing */
2129 ep
->stopped
= stopped
;
2130 if (!list_empty (&ep
->queue
))
2133 DEBUG (ep
->dev
, "%s dma ep_stat %08x ??\n",
2137 /* data packet(s) received (in the fifo, OUT) */
2138 } else if (t
& (1 << DATA_PACKET_RECEIVED_INTERRUPT
)) {
2139 if (read_fifo (ep
, req
) && ep
->num
!= 0)
2142 /* data packet(s) transmitted (IN) */
2143 } else if (t
& (1 << DATA_PACKET_TRANSMITTED_INTERRUPT
)) {
2146 len
= req
->req
.length
- req
->req
.actual
;
2147 if (len
> ep
->ep
.maxpacket
)
2148 len
= ep
->ep
.maxpacket
;
2149 req
->req
.actual
+= len
;
2151 /* if we wrote it all, we're usually done */
2152 if (req
->req
.actual
== req
->req
.length
) {
2154 /* send zlps until the status stage */
2155 } else if (!req
->req
.zero
|| len
!= ep
->ep
.maxpacket
)
2159 /* there was nothing to do ... */
2160 } else if (mode
== 1)
2165 /* stream endpoints often resubmit/unlink in completion */
2168 /* maybe advance queue to next request */
2170 /* NOTE: net2280 could let gadget driver start the
2171 * status stage later. since not all controllers let
2172 * them control that, the api doesn't (yet) allow it.
2178 if (!list_empty (&ep
->queue
) && !ep
->stopped
)
2179 req
= list_entry (ep
->queue
.next
,
2180 struct net2280_request
, queue
);
2183 if (req
&& !ep
->is_in
)
2184 stop_out_naking (ep
);
2188 /* is there a buffer for the next packet?
2189 * for best streaming performance, make sure there is one.
2191 if (req
&& !ep
->stopped
) {
2193 /* load IN fifo with next packet (may be zlp) */
2194 if (t
& (1 << DATA_PACKET_TRANSMITTED_INTERRUPT
))
2195 write_fifo (ep
, &req
->req
);
2199 static struct net2280_ep
*
2200 get_ep_by_addr (struct net2280
*dev
, u16 wIndex
)
2202 struct net2280_ep
*ep
;
2204 if ((wIndex
& USB_ENDPOINT_NUMBER_MASK
) == 0)
2205 return &dev
->ep
[0];
2206 list_for_each_entry (ep
, &dev
->gadget
.ep_list
, ep
.ep_list
) {
2207 u8 bEndpointAddress
;
2211 bEndpointAddress
= ep
->desc
->bEndpointAddress
;
2212 if ((wIndex
^ bEndpointAddress
) & USB_DIR_IN
)
2214 if ((wIndex
& 0x0f) == (bEndpointAddress
& 0x0f))
2220 static void handle_stat0_irqs (struct net2280
*dev
, u32 stat
)
2222 struct net2280_ep
*ep
;
2225 /* most of these don't need individual acks */
2226 stat
&= ~(1 << INTA_ASSERTED
);
2229 // DEBUG (dev, "irqstat0 %04x\n", stat);
2231 /* starting a control request? */
2232 if (unlikely (stat
& (1 << SETUP_PACKET_INTERRUPT
))) {
2235 struct usb_ctrlrequest r
;
2238 struct net2280_request
*req
;
2240 if (dev
->gadget
.speed
== USB_SPEED_UNKNOWN
) {
2241 if (readl (&dev
->usb
->usbstat
) & (1 << HIGH_SPEED
))
2242 dev
->gadget
.speed
= USB_SPEED_HIGH
;
2244 dev
->gadget
.speed
= USB_SPEED_FULL
;
2245 net2280_led_speed (dev
, dev
->gadget
.speed
);
2246 DEBUG(dev
, "%s\n", usb_speed_string(dev
->gadget
.speed
));
2252 /* make sure any leftover request state is cleared */
2253 stat
&= ~(1 << ENDPOINT_0_INTERRUPT
);
2254 while (!list_empty (&ep
->queue
)) {
2255 req
= list_entry (ep
->queue
.next
,
2256 struct net2280_request
, queue
);
2257 done (ep
, req
, (req
->req
.actual
== req
->req
.length
)
2261 dev
->protocol_stall
= 0;
2263 if (ep
->dev
->pdev
->device
== 0x2280)
2264 tmp
= (1 << FIFO_OVERFLOW
)
2265 | (1 << FIFO_UNDERFLOW
);
2269 writel (tmp
| (1 << TIMEOUT
)
2270 | (1 << USB_STALL_SENT
)
2271 | (1 << USB_IN_NAK_SENT
)
2272 | (1 << USB_IN_ACK_RCVD
)
2273 | (1 << USB_OUT_PING_NAK_SENT
)
2274 | (1 << USB_OUT_ACK_SENT
)
2275 | (1 << SHORT_PACKET_OUT_DONE_INTERRUPT
)
2276 | (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT
)
2277 | (1 << DATA_PACKET_RECEIVED_INTERRUPT
)
2278 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT
)
2279 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT
)
2280 | (1 << DATA_IN_TOKEN_INTERRUPT
)
2281 , &ep
->regs
->ep_stat
);
2282 u
.raw
[0] = readl (&dev
->usb
->setup0123
);
2283 u
.raw
[1] = readl (&dev
->usb
->setup4567
);
2285 cpu_to_le32s (&u
.raw
[0]);
2286 cpu_to_le32s (&u
.raw
[1]);
2290 #define w_value le16_to_cpu(u.r.wValue)
2291 #define w_index le16_to_cpu(u.r.wIndex)
2292 #define w_length le16_to_cpu(u.r.wLength)
2295 writel (1 << SETUP_PACKET_INTERRUPT
, &dev
->regs
->irqstat0
);
2296 stat
^= (1 << SETUP_PACKET_INTERRUPT
);
2298 /* watch control traffic at the token level, and force
2299 * synchronization before letting the status stage happen.
2300 * FIXME ignore tokens we'll NAK, until driver responds.
2301 * that'll mean a lot less irqs for some drivers.
2303 ep
->is_in
= (u
.r
.bRequestType
& USB_DIR_IN
) != 0;
2305 scratch
= (1 << DATA_PACKET_TRANSMITTED_INTERRUPT
)
2306 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT
)
2307 | (1 << DATA_IN_TOKEN_INTERRUPT
);
2308 stop_out_naking (ep
);
2310 scratch
= (1 << DATA_PACKET_RECEIVED_INTERRUPT
)
2311 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT
)
2312 | (1 << DATA_IN_TOKEN_INTERRUPT
);
2313 writel (scratch
, &dev
->epregs
[0].ep_irqenb
);
2315 /* we made the hardware handle most lowlevel requests;
2316 * everything else goes uplevel to the gadget code.
2319 switch (u
.r
.bRequest
) {
2320 case USB_REQ_GET_STATUS
: {
2321 struct net2280_ep
*e
;
2324 /* hw handles device and interface status */
2325 if (u
.r
.bRequestType
!= (USB_DIR_IN
|USB_RECIP_ENDPOINT
))
2327 if ((e
= get_ep_by_addr (dev
, w_index
)) == 0
2331 if (readl (&e
->regs
->ep_rsp
)
2332 & (1 << SET_ENDPOINT_HALT
))
2333 status
= cpu_to_le32 (1);
2335 status
= cpu_to_le32 (0);
2337 /* don't bother with a request object! */
2338 writel (0, &dev
->epregs
[0].ep_irqenb
);
2339 set_fifo_bytecount (ep
, w_length
);
2340 writel ((__force u32
)status
, &dev
->epregs
[0].ep_data
);
2342 VDEBUG (dev
, "%s stat %02x\n", ep
->ep
.name
, status
);
2343 goto next_endpoints
;
2346 case USB_REQ_CLEAR_FEATURE
: {
2347 struct net2280_ep
*e
;
2349 /* hw handles device features */
2350 if (u
.r
.bRequestType
!= USB_RECIP_ENDPOINT
)
2352 if (w_value
!= USB_ENDPOINT_HALT
2355 if ((e
= get_ep_by_addr (dev
, w_index
)) == 0)
2358 VDEBUG(dev
, "%s wedged, halt not cleared\n",
2361 VDEBUG(dev
, "%s clear halt\n", ep
->ep
.name
);
2365 goto next_endpoints
;
2368 case USB_REQ_SET_FEATURE
: {
2369 struct net2280_ep
*e
;
2371 /* hw handles device features */
2372 if (u
.r
.bRequestType
!= USB_RECIP_ENDPOINT
)
2374 if (w_value
!= USB_ENDPOINT_HALT
2377 if ((e
= get_ep_by_addr (dev
, w_index
)) == 0)
2379 if (e
->ep
.name
== ep0name
)
2383 VDEBUG (dev
, "%s set halt\n", ep
->ep
.name
);
2384 goto next_endpoints
;
2389 VDEBUG (dev
, "setup %02x.%02x v%04x i%04x l%04x "
2391 u
.r
.bRequestType
, u
.r
.bRequest
,
2392 w_value
, w_index
, w_length
,
2393 readl (&ep
->regs
->ep_cfg
));
2395 spin_unlock (&dev
->lock
);
2396 tmp
= dev
->driver
->setup (&dev
->gadget
, &u
.r
);
2397 spin_lock (&dev
->lock
);
2400 /* stall ep0 on error */
2403 VDEBUG (dev
, "req %02x.%02x protocol STALL; stat %d\n",
2404 u
.r
.bRequestType
, u
.r
.bRequest
, tmp
);
2405 dev
->protocol_stall
= 1;
2408 /* some in/out token irq should follow; maybe stall then.
2409 * driver must queue a request (even zlp) or halt ep0
2410 * before the host times out.
2419 /* endpoint data irq ? */
2420 scratch
= stat
& 0x7f;
2422 for (num
= 0; scratch
; num
++) {
2425 /* do this endpoint's FIFO and queue need tending? */
2427 if ((scratch
& t
) == 0)
2431 ep
= &dev
->ep
[num
];
2432 handle_ep_small (ep
);
2436 DEBUG (dev
, "unhandled irqstat0 %08x\n", stat
);
2439 #define DMA_INTERRUPTS ( \
2440 (1 << DMA_D_INTERRUPT) \
2441 | (1 << DMA_C_INTERRUPT) \
2442 | (1 << DMA_B_INTERRUPT) \
2443 | (1 << DMA_A_INTERRUPT))
2444 #define PCI_ERROR_INTERRUPTS ( \
2445 (1 << PCI_MASTER_ABORT_RECEIVED_INTERRUPT) \
2446 | (1 << PCI_TARGET_ABORT_RECEIVED_INTERRUPT) \
2447 | (1 << PCI_RETRY_ABORT_INTERRUPT))
2449 static void handle_stat1_irqs (struct net2280
*dev
, u32 stat
)
2451 struct net2280_ep
*ep
;
2452 u32 tmp
, num
, mask
, scratch
;
2454 /* after disconnect there's nothing else to do! */
2455 tmp
= (1 << VBUS_INTERRUPT
) | (1 << ROOT_PORT_RESET_INTERRUPT
);
2456 mask
= (1 << HIGH_SPEED
) | (1 << FULL_SPEED
);
2458 /* VBUS disconnect is indicated by VBUS_PIN and VBUS_INTERRUPT set.
2459 * Root Port Reset is indicated by ROOT_PORT_RESET_INTERRUPT set and
2460 * both HIGH_SPEED and FULL_SPEED clear (as ROOT_PORT_RESET_INTERRUPT
2461 * only indicates a change in the reset state).
2464 writel (tmp
, &dev
->regs
->irqstat1
);
2465 if ((((stat
& (1 << ROOT_PORT_RESET_INTERRUPT
))
2466 && ((readl (&dev
->usb
->usbstat
) & mask
)
2468 || ((readl (&dev
->usb
->usbctl
)
2469 & (1 << VBUS_PIN
)) == 0)
2470 ) && ( dev
->gadget
.speed
!= USB_SPEED_UNKNOWN
)) {
2471 DEBUG (dev
, "disconnect %s\n",
2472 dev
->driver
->driver
.name
);
2473 stop_activity (dev
, dev
->driver
);
2479 /* vBUS can bounce ... one of many reasons to ignore the
2480 * notion of hotplug events on bus connect/disconnect!
2486 /* NOTE: chip stays in PCI D0 state for now, but it could
2487 * enter D1 to save more power
2489 tmp
= (1 << SUSPEND_REQUEST_CHANGE_INTERRUPT
);
2491 writel (tmp
, &dev
->regs
->irqstat1
);
2492 if (stat
& (1 << SUSPEND_REQUEST_INTERRUPT
)) {
2493 if (dev
->driver
->suspend
)
2494 dev
->driver
->suspend (&dev
->gadget
);
2495 if (!enable_suspend
)
2496 stat
&= ~(1 << SUSPEND_REQUEST_INTERRUPT
);
2498 if (dev
->driver
->resume
)
2499 dev
->driver
->resume (&dev
->gadget
);
2500 /* at high speed, note erratum 0133 */
2505 /* clear any other status/irqs */
2507 writel (stat
, &dev
->regs
->irqstat1
);
2509 /* some status we can just ignore */
2510 if (dev
->pdev
->device
== 0x2280)
2511 stat
&= ~((1 << CONTROL_STATUS_INTERRUPT
)
2512 | (1 << SUSPEND_REQUEST_INTERRUPT
)
2513 | (1 << RESUME_INTERRUPT
)
2514 | (1 << SOF_INTERRUPT
));
2516 stat
&= ~((1 << CONTROL_STATUS_INTERRUPT
)
2517 | (1 << RESUME_INTERRUPT
)
2518 | (1 << SOF_DOWN_INTERRUPT
)
2519 | (1 << SOF_INTERRUPT
));
2523 // DEBUG (dev, "irqstat1 %08x\n", stat);
2525 /* DMA status, for ep-{a,b,c,d} */
2526 scratch
= stat
& DMA_INTERRUPTS
;
2527 stat
&= ~DMA_INTERRUPTS
;
2529 for (num
= 0; scratch
; num
++) {
2530 struct net2280_dma_regs __iomem
*dma
;
2533 if ((tmp
& scratch
) == 0)
2537 ep
= &dev
->ep
[num
+ 1];
2543 /* clear ep's dma status */
2544 tmp
= readl (&dma
->dmastat
);
2545 writel (tmp
, &dma
->dmastat
);
2547 /* chaining should stop on abort, short OUT from fifo,
2548 * or (stat0 codepath) short OUT transfer.
2550 if (!use_dma_chaining
) {
2551 if ((tmp
& (1 << DMA_TRANSACTION_DONE_INTERRUPT
))
2553 DEBUG (ep
->dev
, "%s no xact done? %08x\n",
2560 /* OUT transfers terminate when the data from the
2561 * host is in our memory. Process whatever's done.
2562 * On this path, we know transfer's last packet wasn't
2563 * less than req->length. NAK_OUT_PACKETS may be set,
2564 * or the FIFO may already be holding new packets.
2566 * IN transfers can linger in the FIFO for a very
2567 * long time ... we ignore that for now, accounting
2568 * precisely (like PIO does) needs per-packet irqs
2570 scan_dma_completions (ep
);
2572 /* disable dma on inactive queues; else maybe restart */
2573 if (list_empty (&ep
->queue
)) {
2574 if (use_dma_chaining
)
2577 tmp
= readl (&dma
->dmactl
);
2578 if (!use_dma_chaining
2579 || (tmp
& (1 << DMA_ENABLE
)) == 0)
2581 else if (ep
->is_in
&& use_dma_chaining
) {
2582 struct net2280_request
*req
;
2585 /* the descriptor at the head of the chain
2586 * may still have VALID_BIT clear; that's
2587 * used to trigger changing DMA_FIFO_VALIDATE
2588 * (affects automagic zlp writes).
2590 req
= list_entry (ep
->queue
.next
,
2591 struct net2280_request
, queue
);
2592 dmacount
= req
->td
->dmacount
;
2593 dmacount
&= cpu_to_le32 (
2595 | DMA_BYTE_COUNT_MASK
);
2596 if (dmacount
&& (dmacount
& valid_bit
) == 0)
2603 /* NOTE: there are other PCI errors we might usefully notice.
2604 * if they appear very often, here's where to try recovering.
2606 if (stat
& PCI_ERROR_INTERRUPTS
) {
2607 ERROR (dev
, "pci dma error; stat %08x\n", stat
);
2608 stat
&= ~PCI_ERROR_INTERRUPTS
;
2609 /* these are fatal errors, but "maybe" they won't
2612 stop_activity (dev
, dev
->driver
);
2618 DEBUG (dev
, "unhandled irqstat1 %08x\n", stat
);
2621 static irqreturn_t
net2280_irq (int irq
, void *_dev
)
2623 struct net2280
*dev
= _dev
;
2625 /* shared interrupt, not ours */
2626 if (!(readl(&dev
->regs
->irqstat0
) & (1 << INTA_ASSERTED
)))
2629 spin_lock (&dev
->lock
);
2631 /* handle disconnect, dma, and more */
2632 handle_stat1_irqs (dev
, readl (&dev
->regs
->irqstat1
));
2634 /* control requests and PIO */
2635 handle_stat0_irqs (dev
, readl (&dev
->regs
->irqstat0
));
2637 spin_unlock (&dev
->lock
);
2642 /*-------------------------------------------------------------------------*/
2644 static void gadget_release (struct device
*_dev
)
2646 struct net2280
*dev
= dev_get_drvdata (_dev
);
2651 /* tear down the binding between this driver and the pci device */
2653 static void net2280_remove (struct pci_dev
*pdev
)
2655 struct net2280
*dev
= pci_get_drvdata (pdev
);
2657 usb_del_gadget_udc(&dev
->gadget
);
2659 BUG_ON(dev
->driver
);
2661 /* then clean up the resources we allocated during probe() */
2662 net2280_led_shutdown (dev
);
2663 if (dev
->requests
) {
2665 for (i
= 1; i
< 5; i
++) {
2666 if (!dev
->ep
[i
].dummy
)
2668 pci_pool_free (dev
->requests
, dev
->ep
[i
].dummy
,
2669 dev
->ep
[i
].td_dma
);
2671 pci_pool_destroy (dev
->requests
);
2674 free_irq (pdev
->irq
, dev
);
2676 iounmap (dev
->regs
);
2678 release_mem_region (pci_resource_start (pdev
, 0),
2679 pci_resource_len (pdev
, 0));
2681 pci_disable_device (pdev
);
2682 device_unregister (&dev
->gadget
.dev
);
2683 device_remove_file (&pdev
->dev
, &dev_attr_registers
);
2684 pci_set_drvdata (pdev
, NULL
);
2686 INFO (dev
, "unbind\n");
2689 /* wrap this driver around the specified device, but
2690 * don't respond over USB until a gadget driver binds to us.
2693 static int net2280_probe (struct pci_dev
*pdev
, const struct pci_device_id
*id
)
2695 struct net2280
*dev
;
2696 unsigned long resource
, len
;
2697 void __iomem
*base
= NULL
;
2700 /* alloc, and start init */
2701 dev
= kzalloc (sizeof *dev
, GFP_KERNEL
);
2707 pci_set_drvdata (pdev
, dev
);
2708 spin_lock_init (&dev
->lock
);
2710 dev
->gadget
.ops
= &net2280_ops
;
2711 dev
->gadget
.max_speed
= USB_SPEED_HIGH
;
2713 /* the "gadget" abstracts/virtualizes the controller */
2714 dev_set_name(&dev
->gadget
.dev
, "gadget");
2715 dev
->gadget
.dev
.parent
= &pdev
->dev
;
2716 dev
->gadget
.dev
.dma_mask
= pdev
->dev
.dma_mask
;
2717 dev
->gadget
.dev
.release
= gadget_release
;
2718 dev
->gadget
.name
= driver_name
;
2720 /* now all the pci goodies ... */
2721 if (pci_enable_device (pdev
) < 0) {
2727 /* BAR 0 holds all the registers
2728 * BAR 1 is 8051 memory; unused here (note erratum 0103)
2729 * BAR 2 is fifo memory; unused here
2731 resource
= pci_resource_start (pdev
, 0);
2732 len
= pci_resource_len (pdev
, 0);
2733 if (!request_mem_region (resource
, len
, driver_name
)) {
2734 DEBUG (dev
, "controller already in use\n");
2740 /* FIXME provide firmware download interface to put
2741 * 8051 code into the chip, e.g. to turn on PCI PM.
2744 base
= ioremap_nocache (resource
, len
);
2746 DEBUG (dev
, "can't map memory\n");
2750 dev
->regs
= (struct net2280_regs __iomem
*) base
;
2751 dev
->usb
= (struct net2280_usb_regs __iomem
*) (base
+ 0x0080);
2752 dev
->pci
= (struct net2280_pci_regs __iomem
*) (base
+ 0x0100);
2753 dev
->dma
= (struct net2280_dma_regs __iomem
*) (base
+ 0x0180);
2754 dev
->dep
= (struct net2280_dep_regs __iomem
*) (base
+ 0x0200);
2755 dev
->epregs
= (struct net2280_ep_regs __iomem
*) (base
+ 0x0300);
2757 /* put into initial config, link up all endpoints */
2758 writel (0, &dev
->usb
->usbctl
);
2762 /* irq setup after old hardware is cleaned up */
2764 ERROR (dev
, "No IRQ. Check PCI setup!\n");
2769 if (request_irq (pdev
->irq
, net2280_irq
, IRQF_SHARED
, driver_name
, dev
)
2771 ERROR (dev
, "request interrupt %d failed\n", pdev
->irq
);
2778 /* NOTE: we know only the 32 LSBs of dma addresses may be nonzero */
2779 dev
->requests
= pci_pool_create ("requests", pdev
,
2780 sizeof (struct net2280_dma
),
2781 0 /* no alignment requirements */,
2782 0 /* or page-crossing issues */);
2783 if (!dev
->requests
) {
2784 DEBUG (dev
, "can't get request pool\n");
2788 for (i
= 1; i
< 5; i
++) {
2789 struct net2280_dma
*td
;
2791 td
= pci_pool_alloc (dev
->requests
, GFP_KERNEL
,
2792 &dev
->ep
[i
].td_dma
);
2794 DEBUG (dev
, "can't get dummy %d\n", i
);
2798 td
->dmacount
= 0; /* not VALID */
2799 td
->dmaaddr
= cpu_to_le32 (DMA_ADDR_INVALID
);
2800 td
->dmadesc
= td
->dmaaddr
;
2801 dev
->ep
[i
].dummy
= td
;
2804 /* enable lower-overhead pci memory bursts during DMA */
2805 writel ( (1 << DMA_MEMORY_WRITE_AND_INVALIDATE_ENABLE
)
2806 // 256 write retries may not be enough...
2807 // | (1 << PCI_RETRY_ABORT_ENABLE)
2808 | (1 << DMA_READ_MULTIPLE_ENABLE
)
2809 | (1 << DMA_READ_LINE_ENABLE
)
2810 , &dev
->pci
->pcimstctl
);
2811 /* erratum 0115 shouldn't appear: Linux inits PCI_LATENCY_TIMER */
2812 pci_set_master (pdev
);
2813 pci_try_set_mwi (pdev
);
2815 /* ... also flushes any posted pci writes */
2816 dev
->chiprev
= get_idx_reg (dev
->regs
, REG_CHIPREV
) & 0xffff;
2819 INFO (dev
, "%s\n", driver_desc
);
2820 INFO (dev
, "irq %d, pci mem %p, chip rev %04x\n",
2821 pdev
->irq
, base
, dev
->chiprev
);
2822 INFO (dev
, "version: " DRIVER_VERSION
"; dma %s\n",
2824 ? (use_dma_chaining
? "chaining" : "enabled")
2826 retval
= device_register (&dev
->gadget
.dev
);
2827 if (retval
) goto done
;
2828 retval
= device_create_file (&pdev
->dev
, &dev_attr_registers
);
2829 if (retval
) goto done
;
2831 retval
= usb_add_gadget_udc(&pdev
->dev
, &dev
->gadget
);
2838 net2280_remove (pdev
);
2842 /* make sure the board is quiescent; otherwise it will continue
2843 * generating IRQs across the upcoming reboot.
2846 static void net2280_shutdown (struct pci_dev
*pdev
)
2848 struct net2280
*dev
= pci_get_drvdata (pdev
);
2851 writel (0, &dev
->regs
->pciirqenb0
);
2852 writel (0, &dev
->regs
->pciirqenb1
);
2854 /* disable the pullup so the host will think we're gone */
2855 writel (0, &dev
->usb
->usbctl
);
2857 /* Disable full-speed test mode */
2858 writel(0, &dev
->usb
->xcvrdiag
);
2862 /*-------------------------------------------------------------------------*/
2864 static const struct pci_device_id pci_ids
[] = { {
2865 .class = ((PCI_CLASS_SERIAL_USB
<< 8) | 0xfe),
2869 .subvendor
= PCI_ANY_ID
,
2870 .subdevice
= PCI_ANY_ID
,
2872 .class = ((PCI_CLASS_SERIAL_USB
<< 8) | 0xfe),
2876 .subvendor
= PCI_ANY_ID
,
2877 .subdevice
= PCI_ANY_ID
,
2879 }, { /* end: all zeroes */ }
2881 MODULE_DEVICE_TABLE (pci
, pci_ids
);
2883 /* pci driver glue; this is a "new style" PCI driver module */
2884 static struct pci_driver net2280_pci_driver
= {
2885 .name
= (char *) driver_name
,
2886 .id_table
= pci_ids
,
2888 .probe
= net2280_probe
,
2889 .remove
= net2280_remove
,
2890 .shutdown
= net2280_shutdown
,
2892 /* FIXME add power management support */
2895 MODULE_DESCRIPTION (DRIVER_DESC
);
2896 MODULE_AUTHOR ("David Brownell");
2897 MODULE_LICENSE ("GPL");
2899 static int __init
init (void)
2902 use_dma_chaining
= 0;
2903 return pci_register_driver (&net2280_pci_driver
);
2907 static void __exit
cleanup (void)
2909 pci_unregister_driver (&net2280_pci_driver
);
2911 module_exit (cleanup
);