2 * Driver for the PLX NET2280 USB device controller.
3 * Specs and errata are available from <http://www.plxtech.com>.
5 * PLX Technology Inc. (formerly NetChip Technology) supported the
6 * development of this driver.
9 * CODE STATUS HIGHLIGHTS
11 * This driver should work well with most "gadget" drivers, including
12 * the File Storage, Serial, and Ethernet/RNDIS gadget drivers
13 * as well as Gadget Zero and Gadgetfs.
15 * DMA is enabled by default. Drivers using transfer queues might use
16 * DMA chaining to remove IRQ latencies between transfers. (Except when
17 * short OUT transfers happen.) Drivers can use the req->no_interrupt
18 * hint to completely eliminate some IRQs, if a later IRQ is guaranteed
19 * and DMA chaining is enabled.
21 * Note that almost all the errata workarounds here are only needed for
22 * rev1 chips. Rev1a silicon (0110) fixes almost all of them.
26 * Copyright (C) 2003 David Brownell
27 * Copyright (C) 2003-2005 PLX Technology, Inc.
29 * Modified Seth Levy 2005 PLX Technology, Inc. to provide compatibility
32 * This program is free software; you can redistribute it and/or modify
33 * it under the terms of the GNU General Public License as published by
34 * the Free Software Foundation; either version 2 of the License, or
35 * (at your option) any later version.
37 * This program is distributed in the hope that it will be useful,
38 * but WITHOUT ANY WARRANTY; without even the implied warranty of
39 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
40 * GNU General Public License for more details.
42 * You should have received a copy of the GNU General Public License
43 * along with this program; if not, write to the Free Software
44 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
47 #undef DEBUG /* messages on error and most fault paths */
48 #undef VERBOSE /* extra debug messages (success too) */
50 #include <linux/module.h>
51 #include <linux/pci.h>
52 #include <linux/dma-mapping.h>
53 #include <linux/kernel.h>
54 #include <linux/delay.h>
55 #include <linux/ioport.h>
56 #include <linux/slab.h>
57 #include <linux/errno.h>
58 #include <linux/init.h>
59 #include <linux/timer.h>
60 #include <linux/list.h>
61 #include <linux/interrupt.h>
62 #include <linux/moduleparam.h>
63 #include <linux/device.h>
64 #include <linux/usb/ch9.h>
65 #include <linux/usb/gadget.h>
66 #include <linux/prefetch.h>
68 #include <asm/byteorder.h>
71 #include <asm/system.h>
72 #include <asm/unaligned.h>
75 #define DRIVER_DESC "PLX NET228x USB Peripheral Controller"
76 #define DRIVER_VERSION "2005 Sept 27"
78 #define DMA_ADDR_INVALID (~(dma_addr_t)0)
79 #define EP_DONTUSE 13 /* nonzero */
81 #define USE_RDK_LEDS /* GPIO pins control three LEDs */
84 static const char driver_name
[] = "net2280";
85 static const char driver_desc
[] = DRIVER_DESC
;
87 static const char ep0name
[] = "ep0";
88 static const char *const ep_name
[] = {
90 "ep-a", "ep-b", "ep-c", "ep-d",
94 /* use_dma -- general goodness, fewer interrupts, less cpu load (vs PIO)
95 * use_dma_chaining -- dma descriptor queueing gives even more irq reduction
97 * The net2280 DMA engines are not tightly integrated with their FIFOs;
98 * not all cases are (yet) handled well in this driver or the silicon.
99 * Some gadget drivers work better with the dma support here than others.
100 * These two parameters let you use PIO or more aggressive DMA.
102 static int use_dma
= 1;
103 static int use_dma_chaining
= 0;
105 /* "modprobe net2280 use_dma=n" etc */
106 module_param (use_dma
, bool, S_IRUGO
);
107 module_param (use_dma_chaining
, bool, S_IRUGO
);
110 /* mode 0 == ep-{a,b,c,d} 1K fifo each
111 * mode 1 == ep-{a,b} 2K fifo each, ep-{c,d} unavailable
112 * mode 2 == ep-a 2K fifo, ep-{b,c} 1K each, ep-d unavailable
114 static ushort fifo_mode
= 0;
116 /* "modprobe net2280 fifo_mode=1" etc */
117 module_param (fifo_mode
, ushort
, 0644);
119 /* enable_suspend -- When enabled, the driver will respond to
120 * USB suspend requests by powering down the NET2280. Otherwise,
121 * USB suspend requests will be ignored. This is acceptable for
122 * self-powered devices
124 static int enable_suspend
= 0;
126 /* "modprobe net2280 enable_suspend=1" etc */
127 module_param (enable_suspend
, bool, S_IRUGO
);
130 #define DIR_STRING(bAddress) (((bAddress) & USB_DIR_IN) ? "in" : "out")
132 #if defined(CONFIG_USB_GADGET_DEBUG_FILES) || defined (DEBUG)
133 static char *type_string (u8 bmAttributes
)
135 switch ((bmAttributes
) & USB_ENDPOINT_XFERTYPE_MASK
) {
136 case USB_ENDPOINT_XFER_BULK
: return "bulk";
137 case USB_ENDPOINT_XFER_ISOC
: return "iso";
138 case USB_ENDPOINT_XFER_INT
: return "intr";
146 #define valid_bit cpu_to_le32 (1 << VALID_BIT)
147 #define dma_done_ie cpu_to_le32 (1 << DMA_DONE_INTERRUPT_ENABLE)
149 /*-------------------------------------------------------------------------*/
152 net2280_enable (struct usb_ep
*_ep
, const struct usb_endpoint_descriptor
*desc
)
155 struct net2280_ep
*ep
;
159 ep
= container_of (_ep
, struct net2280_ep
, ep
);
160 if (!_ep
|| !desc
|| ep
->desc
|| _ep
->name
== ep0name
161 || desc
->bDescriptorType
!= USB_DT_ENDPOINT
)
164 if (!dev
->driver
|| dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
167 /* erratum 0119 workaround ties up an endpoint number */
168 if ((desc
->bEndpointAddress
& 0x0f) == EP_DONTUSE
)
171 /* sanity check ep-e/ep-f since their fifos are small */
172 max
= usb_endpoint_maxp (desc
) & 0x1fff;
173 if (ep
->num
> 4 && max
> 64)
176 spin_lock_irqsave (&dev
->lock
, flags
);
177 _ep
->maxpacket
= max
& 0x7ff;
180 /* ep_reset() has already been called */
183 ep
->out_overflow
= 0;
185 /* set speed-dependent max packet; may kick in high bandwidth */
186 set_idx_reg (dev
->regs
, REG_EP_MAXPKT (dev
, ep
->num
), max
);
188 /* FIFO lines can't go to different packets. PIO is ok, so
189 * use it instead of troublesome (non-bulk) multi-packet DMA.
191 if (ep
->dma
&& (max
% 4) != 0 && use_dma_chaining
) {
192 DEBUG (ep
->dev
, "%s, no dma for maxpacket %d\n",
193 ep
->ep
.name
, ep
->ep
.maxpacket
);
197 /* set type, direction, address; reset fifo counters */
198 writel ((1 << FIFO_FLUSH
), &ep
->regs
->ep_stat
);
199 tmp
= (desc
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
);
200 if (tmp
== USB_ENDPOINT_XFER_INT
) {
201 /* erratum 0105 workaround prevents hs NYET */
202 if (dev
->chiprev
== 0100
203 && dev
->gadget
.speed
== USB_SPEED_HIGH
204 && !(desc
->bEndpointAddress
& USB_DIR_IN
))
205 writel ((1 << CLEAR_NAK_OUT_PACKETS_MODE
),
207 } else if (tmp
== USB_ENDPOINT_XFER_BULK
) {
208 /* catch some particularly blatant driver bugs */
209 if ((dev
->gadget
.speed
== USB_SPEED_HIGH
211 || (dev
->gadget
.speed
== USB_SPEED_FULL
213 spin_unlock_irqrestore (&dev
->lock
, flags
);
217 ep
->is_iso
= (tmp
== USB_ENDPOINT_XFER_ISOC
) ? 1 : 0;
218 tmp
<<= ENDPOINT_TYPE
;
219 tmp
|= desc
->bEndpointAddress
;
220 tmp
|= (4 << ENDPOINT_BYTE_COUNT
); /* default full fifo lines */
221 tmp
|= 1 << ENDPOINT_ENABLE
;
224 /* for OUT transfers, block the rx fifo until a read is posted */
225 ep
->is_in
= (tmp
& USB_DIR_IN
) != 0;
227 writel ((1 << SET_NAK_OUT_PACKETS
), &ep
->regs
->ep_rsp
);
228 else if (dev
->pdev
->device
!= 0x2280) {
229 /* Added for 2282, Don't use nak packets on an in endpoint,
230 * this was ignored on 2280
232 writel ((1 << CLEAR_NAK_OUT_PACKETS
)
233 | (1 << CLEAR_NAK_OUT_PACKETS_MODE
), &ep
->regs
->ep_rsp
);
236 writel (tmp
, &ep
->regs
->ep_cfg
);
239 if (!ep
->dma
) { /* pio, per-packet */
240 tmp
= (1 << ep
->num
) | readl (&dev
->regs
->pciirqenb0
);
241 writel (tmp
, &dev
->regs
->pciirqenb0
);
243 tmp
= (1 << DATA_PACKET_RECEIVED_INTERRUPT_ENABLE
)
244 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT_ENABLE
);
245 if (dev
->pdev
->device
== 0x2280)
246 tmp
|= readl (&ep
->regs
->ep_irqenb
);
247 writel (tmp
, &ep
->regs
->ep_irqenb
);
248 } else { /* dma, per-request */
249 tmp
= (1 << (8 + ep
->num
)); /* completion */
250 tmp
|= readl (&dev
->regs
->pciirqenb1
);
251 writel (tmp
, &dev
->regs
->pciirqenb1
);
253 /* for short OUT transfers, dma completions can't
254 * advance the queue; do it pio-style, by hand.
255 * NOTE erratum 0112 workaround #2
257 if ((desc
->bEndpointAddress
& USB_DIR_IN
) == 0) {
258 tmp
= (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT_ENABLE
);
259 writel (tmp
, &ep
->regs
->ep_irqenb
);
261 tmp
= (1 << ep
->num
) | readl (&dev
->regs
->pciirqenb0
);
262 writel (tmp
, &dev
->regs
->pciirqenb0
);
266 tmp
= desc
->bEndpointAddress
;
267 DEBUG (dev
, "enabled %s (ep%d%s-%s) %s max %04x\n",
268 _ep
->name
, tmp
& 0x0f, DIR_STRING (tmp
),
269 type_string (desc
->bmAttributes
),
270 ep
->dma
? "dma" : "pio", max
);
272 /* pci writes may still be posted */
273 spin_unlock_irqrestore (&dev
->lock
, flags
);
277 static int handshake (u32 __iomem
*ptr
, u32 mask
, u32 done
, int usec
)
282 result
= readl (ptr
);
283 if (result
== ~(u32
)0) /* "device unplugged" */
294 static const struct usb_ep_ops net2280_ep_ops
;
296 static void ep_reset (struct net2280_regs __iomem
*regs
, struct net2280_ep
*ep
)
301 INIT_LIST_HEAD (&ep
->queue
);
303 ep
->ep
.maxpacket
= ~0;
304 ep
->ep
.ops
= &net2280_ep_ops
;
306 /* disable the dma, irqs, endpoint... */
308 writel (0, &ep
->dma
->dmactl
);
309 writel ( (1 << DMA_SCATTER_GATHER_DONE_INTERRUPT
)
310 | (1 << DMA_TRANSACTION_DONE_INTERRUPT
)
312 , &ep
->dma
->dmastat
);
314 tmp
= readl (®s
->pciirqenb0
);
315 tmp
&= ~(1 << ep
->num
);
316 writel (tmp
, ®s
->pciirqenb0
);
318 tmp
= readl (®s
->pciirqenb1
);
319 tmp
&= ~(1 << (8 + ep
->num
)); /* completion */
320 writel (tmp
, ®s
->pciirqenb1
);
322 writel (0, &ep
->regs
->ep_irqenb
);
324 /* init to our chosen defaults, notably so that we NAK OUT
325 * packets until the driver queues a read (+note erratum 0112)
327 if (!ep
->is_in
|| ep
->dev
->pdev
->device
== 0x2280) {
328 tmp
= (1 << SET_NAK_OUT_PACKETS_MODE
)
329 | (1 << SET_NAK_OUT_PACKETS
)
330 | (1 << CLEAR_EP_HIDE_STATUS_PHASE
)
331 | (1 << CLEAR_INTERRUPT_MODE
);
334 tmp
= (1 << CLEAR_NAK_OUT_PACKETS_MODE
)
335 | (1 << CLEAR_NAK_OUT_PACKETS
)
336 | (1 << CLEAR_EP_HIDE_STATUS_PHASE
)
337 | (1 << CLEAR_INTERRUPT_MODE
);
341 tmp
|= (1 << CLEAR_ENDPOINT_TOGGLE
)
342 | (1 << CLEAR_ENDPOINT_HALT
);
344 writel (tmp
, &ep
->regs
->ep_rsp
);
346 /* scrub most status bits, and flush any fifo state */
347 if (ep
->dev
->pdev
->device
== 0x2280)
348 tmp
= (1 << FIFO_OVERFLOW
)
349 | (1 << FIFO_UNDERFLOW
);
353 writel (tmp
| (1 << TIMEOUT
)
354 | (1 << USB_STALL_SENT
)
355 | (1 << USB_IN_NAK_SENT
)
356 | (1 << USB_IN_ACK_RCVD
)
357 | (1 << USB_OUT_PING_NAK_SENT
)
358 | (1 << USB_OUT_ACK_SENT
)
360 | (1 << SHORT_PACKET_OUT_DONE_INTERRUPT
)
361 | (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT
)
362 | (1 << DATA_PACKET_RECEIVED_INTERRUPT
)
363 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT
)
364 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT
)
365 | (1 << DATA_IN_TOKEN_INTERRUPT
)
366 , &ep
->regs
->ep_stat
);
368 /* fifo size is handled separately */
371 static void nuke (struct net2280_ep
*);
373 static int net2280_disable (struct usb_ep
*_ep
)
375 struct net2280_ep
*ep
;
378 ep
= container_of (_ep
, struct net2280_ep
, ep
);
379 if (!_ep
|| !ep
->desc
|| _ep
->name
== ep0name
)
382 spin_lock_irqsave (&ep
->dev
->lock
, flags
);
384 ep_reset (ep
->dev
->regs
, ep
);
386 VDEBUG (ep
->dev
, "disabled %s %s\n",
387 ep
->dma
? "dma" : "pio", _ep
->name
);
389 /* synch memory views with the device */
390 (void) readl (&ep
->regs
->ep_cfg
);
392 if (use_dma
&& !ep
->dma
&& ep
->num
>= 1 && ep
->num
<= 4)
393 ep
->dma
= &ep
->dev
->dma
[ep
->num
- 1];
395 spin_unlock_irqrestore (&ep
->dev
->lock
, flags
);
399 /*-------------------------------------------------------------------------*/
401 static struct usb_request
*
402 net2280_alloc_request (struct usb_ep
*_ep
, gfp_t gfp_flags
)
404 struct net2280_ep
*ep
;
405 struct net2280_request
*req
;
409 ep
= container_of (_ep
, struct net2280_ep
, ep
);
411 req
= kzalloc(sizeof(*req
), gfp_flags
);
415 req
->req
.dma
= DMA_ADDR_INVALID
;
416 INIT_LIST_HEAD (&req
->queue
);
418 /* this dma descriptor may be swapped with the previous dummy */
420 struct net2280_dma
*td
;
422 td
= pci_pool_alloc (ep
->dev
->requests
, gfp_flags
,
428 td
->dmacount
= 0; /* not VALID */
429 td
->dmaaddr
= cpu_to_le32 (DMA_ADDR_INVALID
);
430 td
->dmadesc
= td
->dmaaddr
;
437 net2280_free_request (struct usb_ep
*_ep
, struct usb_request
*_req
)
439 struct net2280_ep
*ep
;
440 struct net2280_request
*req
;
442 ep
= container_of (_ep
, struct net2280_ep
, ep
);
446 req
= container_of (_req
, struct net2280_request
, req
);
447 WARN_ON (!list_empty (&req
->queue
));
449 pci_pool_free (ep
->dev
->requests
, req
->td
, req
->td_dma
);
453 /*-------------------------------------------------------------------------*/
455 /* load a packet into the fifo we use for usb IN transfers.
456 * works for all endpoints.
458 * NOTE: pio with ep-a..ep-d could stuff multiple packets into the fifo
459 * at a time, but this code is simpler because it knows it only writes
460 * one packet. ep-a..ep-d should use dma instead.
463 write_fifo (struct net2280_ep
*ep
, struct usb_request
*req
)
465 struct net2280_ep_regs __iomem
*regs
= ep
->regs
;
468 unsigned count
, total
;
470 /* INVARIANT: fifo is currently empty. (testable) */
473 buf
= req
->buf
+ req
->actual
;
475 total
= req
->length
- req
->actual
;
481 /* write just one packet at a time */
482 count
= ep
->ep
.maxpacket
;
483 if (count
> total
) /* min() cannot be used on a bitfield */
486 VDEBUG (ep
->dev
, "write %s fifo (IN) %d bytes%s req %p\n",
488 (count
!= ep
->ep
.maxpacket
) ? " (short)" : "",
491 /* NOTE be careful if you try to align these. fifo lines
492 * should normally be full (4 bytes) and successive partial
493 * lines are ok only in certain cases.
495 tmp
= get_unaligned ((u32
*)buf
);
497 writel (tmp
, ®s
->ep_data
);
502 /* last fifo entry is "short" unless we wrote a full packet.
503 * also explicitly validate last word in (periodic) transfers
504 * when maxpacket is not a multiple of 4 bytes.
506 if (count
|| total
< ep
->ep
.maxpacket
) {
507 tmp
= count
? get_unaligned ((u32
*)buf
) : count
;
509 set_fifo_bytecount (ep
, count
& 0x03);
510 writel (tmp
, ®s
->ep_data
);
513 /* pci writes may still be posted */
516 /* work around erratum 0106: PCI and USB race over the OUT fifo.
517 * caller guarantees chiprev 0100, out endpoint is NAKing, and
518 * there's no real data in the fifo.
520 * NOTE: also used in cases where that erratum doesn't apply:
521 * where the host wrote "too much" data to us.
523 static void out_flush (struct net2280_ep
*ep
)
528 ASSERT_OUT_NAKING (ep
);
530 statp
= &ep
->regs
->ep_stat
;
531 writel ( (1 << DATA_OUT_PING_TOKEN_INTERRUPT
)
532 | (1 << DATA_PACKET_RECEIVED_INTERRUPT
)
534 writel ((1 << FIFO_FLUSH
), statp
);
537 if (tmp
& (1 << DATA_OUT_PING_TOKEN_INTERRUPT
)
538 /* high speed did bulk NYET; fifo isn't filling */
539 && ep
->dev
->gadget
.speed
== USB_SPEED_FULL
) {
542 usec
= 50; /* 64 byte bulk/interrupt */
543 handshake (statp
, (1 << USB_OUT_PING_NAK_SENT
),
544 (1 << USB_OUT_PING_NAK_SENT
), usec
);
545 /* NAK done; now CLEAR_NAK_OUT_PACKETS is safe */
549 /* unload packet(s) from the fifo we use for usb OUT transfers.
550 * returns true iff the request completed, because of short packet
551 * or the request buffer having filled with full packets.
553 * for ep-a..ep-d this will read multiple packets out when they
554 * have been accepted.
557 read_fifo (struct net2280_ep
*ep
, struct net2280_request
*req
)
559 struct net2280_ep_regs __iomem
*regs
= ep
->regs
;
560 u8
*buf
= req
->req
.buf
+ req
->req
.actual
;
561 unsigned count
, tmp
, is_short
;
562 unsigned cleanup
= 0, prevent
= 0;
564 /* erratum 0106 ... packets coming in during fifo reads might
565 * be incompletely rejected. not all cases have workarounds.
567 if (ep
->dev
->chiprev
== 0x0100
568 && ep
->dev
->gadget
.speed
== USB_SPEED_FULL
) {
570 tmp
= readl (&ep
->regs
->ep_stat
);
571 if ((tmp
& (1 << NAK_OUT_PACKETS
)))
573 else if ((tmp
& (1 << FIFO_FULL
))) {
574 start_out_naking (ep
);
577 /* else: hope we don't see the problem */
580 /* never overflow the rx buffer. the fifo reads packets until
581 * it sees a short one; we might not be ready for them all.
584 count
= readl (®s
->ep_avail
);
585 if (unlikely (count
== 0)) {
587 tmp
= readl (&ep
->regs
->ep_stat
);
588 count
= readl (®s
->ep_avail
);
589 /* handled that data already? */
590 if (count
== 0 && (tmp
& (1 << NAK_OUT_PACKETS
)) == 0)
594 tmp
= req
->req
.length
- req
->req
.actual
;
596 /* as with DMA, data overflow gets flushed */
597 if ((tmp
% ep
->ep
.maxpacket
) != 0) {
599 "%s out fifo %d bytes, expected %d\n",
600 ep
->ep
.name
, count
, tmp
);
601 req
->req
.status
= -EOVERFLOW
;
603 /* NAK_OUT_PACKETS will be set, so flushing is safe;
604 * the next read will start with the next packet
606 } /* else it's a ZLP, no worries */
609 req
->req
.actual
+= count
;
611 is_short
= (count
== 0) || ((count
% ep
->ep
.maxpacket
) != 0);
613 VDEBUG (ep
->dev
, "read %s fifo (OUT) %d bytes%s%s%s req %p %d/%d\n",
614 ep
->ep
.name
, count
, is_short
? " (short)" : "",
615 cleanup
? " flush" : "", prevent
? " nak" : "",
616 req
, req
->req
.actual
, req
->req
.length
);
619 tmp
= readl (®s
->ep_data
);
621 put_unaligned (tmp
, (u32
*)buf
);
626 tmp
= readl (®s
->ep_data
);
627 /* LE conversion is implicit here: */
636 writel ((1 << CLEAR_NAK_OUT_PACKETS
), &ep
->regs
->ep_rsp
);
637 (void) readl (&ep
->regs
->ep_rsp
);
640 return is_short
|| ((req
->req
.actual
== req
->req
.length
)
644 /* fill out dma descriptor to match a given request */
646 fill_dma_desc (struct net2280_ep
*ep
, struct net2280_request
*req
, int valid
)
648 struct net2280_dma
*td
= req
->td
;
649 u32 dmacount
= req
->req
.length
;
651 /* don't let DMA continue after a short OUT packet,
652 * so overruns can't affect the next transfer.
653 * in case of overruns on max-size packets, we can't
654 * stop the fifo from filling but we can flush it.
657 dmacount
|= (1 << DMA_DIRECTION
);
658 if ((!ep
->is_in
&& (dmacount
% ep
->ep
.maxpacket
) != 0)
659 || ep
->dev
->pdev
->device
!= 0x2280)
660 dmacount
|= (1 << END_OF_CHAIN
);
664 dmacount
|= (1 << VALID_BIT
);
665 if (likely(!req
->req
.no_interrupt
|| !use_dma_chaining
))
666 dmacount
|= (1 << DMA_DONE_INTERRUPT_ENABLE
);
668 /* td->dmadesc = previously set by caller */
669 td
->dmaaddr
= cpu_to_le32 (req
->req
.dma
);
671 /* 2280 may be polling VALID_BIT through ep->dma->dmadesc */
673 td
->dmacount
= cpu_to_le32(dmacount
);
676 static const u32 dmactl_default
=
677 (1 << DMA_SCATTER_GATHER_DONE_INTERRUPT
)
678 | (1 << DMA_CLEAR_COUNT_ENABLE
)
679 /* erratum 0116 workaround part 1 (use POLLING) */
680 | (POLL_100_USEC
<< DESCRIPTOR_POLLING_RATE
)
681 | (1 << DMA_VALID_BIT_POLLING_ENABLE
)
682 | (1 << DMA_VALID_BIT_ENABLE
)
683 | (1 << DMA_SCATTER_GATHER_ENABLE
)
684 /* erratum 0116 workaround part 2 (no AUTOSTART) */
687 static inline void spin_stop_dma (struct net2280_dma_regs __iomem
*dma
)
689 handshake (&dma
->dmactl
, (1 << DMA_ENABLE
), 0, 50);
692 static inline void stop_dma (struct net2280_dma_regs __iomem
*dma
)
694 writel (readl (&dma
->dmactl
) & ~(1 << DMA_ENABLE
), &dma
->dmactl
);
698 static void start_queue (struct net2280_ep
*ep
, u32 dmactl
, u32 td_dma
)
700 struct net2280_dma_regs __iomem
*dma
= ep
->dma
;
701 unsigned int tmp
= (1 << VALID_BIT
) | (ep
->is_in
<< DMA_DIRECTION
);
703 if (ep
->dev
->pdev
->device
!= 0x2280)
704 tmp
|= (1 << END_OF_CHAIN
);
706 writel (tmp
, &dma
->dmacount
);
707 writel (readl (&dma
->dmastat
), &dma
->dmastat
);
709 writel (td_dma
, &dma
->dmadesc
);
710 writel (dmactl
, &dma
->dmactl
);
712 /* erratum 0116 workaround part 3: pci arbiter away from net2280 */
713 (void) readl (&ep
->dev
->pci
->pcimstctl
);
715 writel ((1 << DMA_START
), &dma
->dmastat
);
718 stop_out_naking (ep
);
721 static void start_dma (struct net2280_ep
*ep
, struct net2280_request
*req
)
724 struct net2280_dma_regs __iomem
*dma
= ep
->dma
;
726 /* FIXME can't use DMA for ZLPs */
728 /* on this path we "know" there's no dma active (yet) */
729 WARN_ON (readl (&dma
->dmactl
) & (1 << DMA_ENABLE
));
730 writel (0, &ep
->dma
->dmactl
);
732 /* previous OUT packet might have been short */
733 if (!ep
->is_in
&& ((tmp
= readl (&ep
->regs
->ep_stat
))
734 & (1 << NAK_OUT_PACKETS
)) != 0) {
735 writel ((1 << SHORT_PACKET_TRANSFERRED_INTERRUPT
),
738 tmp
= readl (&ep
->regs
->ep_avail
);
740 writel (readl (&dma
->dmastat
), &dma
->dmastat
);
742 /* transfer all/some fifo data */
743 writel (req
->req
.dma
, &dma
->dmaaddr
);
744 tmp
= min (tmp
, req
->req
.length
);
746 /* dma irq, faking scatterlist status */
747 req
->td
->dmacount
= cpu_to_le32 (req
->req
.length
- tmp
);
748 writel ((1 << DMA_DONE_INTERRUPT_ENABLE
)
749 | tmp
, &dma
->dmacount
);
750 req
->td
->dmadesc
= 0;
753 writel ((1 << DMA_ENABLE
), &dma
->dmactl
);
754 writel ((1 << DMA_START
), &dma
->dmastat
);
759 tmp
= dmactl_default
;
761 /* force packet boundaries between dma requests, but prevent the
762 * controller from automagically writing a last "short" packet
763 * (zero length) unless the driver explicitly said to do that.
766 if (likely ((req
->req
.length
% ep
->ep
.maxpacket
) != 0
768 tmp
|= (1 << DMA_FIFO_VALIDATE
);
769 ep
->in_fifo_validate
= 1;
771 ep
->in_fifo_validate
= 0;
774 /* init req->td, pointing to the current dummy */
775 req
->td
->dmadesc
= cpu_to_le32 (ep
->td_dma
);
776 fill_dma_desc (ep
, req
, 1);
778 if (!use_dma_chaining
)
779 req
->td
->dmacount
|= cpu_to_le32 (1 << END_OF_CHAIN
);
781 start_queue (ep
, tmp
, req
->td_dma
);
785 queue_dma (struct net2280_ep
*ep
, struct net2280_request
*req
, int valid
)
787 struct net2280_dma
*end
;
790 /* swap new dummy for old, link; fill and maybe activate */
796 ep
->td_dma
= req
->td_dma
;
799 end
->dmadesc
= cpu_to_le32 (ep
->td_dma
);
801 fill_dma_desc (ep
, req
, valid
);
805 done (struct net2280_ep
*ep
, struct net2280_request
*req
, int status
)
808 unsigned stopped
= ep
->stopped
;
810 list_del_init (&req
->queue
);
812 if (req
->req
.status
== -EINPROGRESS
)
813 req
->req
.status
= status
;
815 status
= req
->req
.status
;
819 pci_unmap_single (dev
->pdev
, req
->req
.dma
, req
->req
.length
,
820 ep
->is_in
? PCI_DMA_TODEVICE
: PCI_DMA_FROMDEVICE
);
821 req
->req
.dma
= DMA_ADDR_INVALID
;
825 if (status
&& status
!= -ESHUTDOWN
)
826 VDEBUG (dev
, "complete %s req %p stat %d len %u/%u\n",
827 ep
->ep
.name
, &req
->req
, status
,
828 req
->req
.actual
, req
->req
.length
);
830 /* don't modify queue heads during completion callback */
832 spin_unlock (&dev
->lock
);
833 req
->req
.complete (&ep
->ep
, &req
->req
);
834 spin_lock (&dev
->lock
);
835 ep
->stopped
= stopped
;
838 /*-------------------------------------------------------------------------*/
841 net2280_queue (struct usb_ep
*_ep
, struct usb_request
*_req
, gfp_t gfp_flags
)
843 struct net2280_request
*req
;
844 struct net2280_ep
*ep
;
848 /* we always require a cpu-view buffer, so that we can
849 * always use pio (as fallback or whatever).
851 req
= container_of (_req
, struct net2280_request
, req
);
852 if (!_req
|| !_req
->complete
|| !_req
->buf
853 || !list_empty (&req
->queue
))
855 if (_req
->length
> (~0 & DMA_BYTE_COUNT_MASK
))
857 ep
= container_of (_ep
, struct net2280_ep
, ep
);
858 if (!_ep
|| (!ep
->desc
&& ep
->num
!= 0))
861 if (!dev
->driver
|| dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
864 /* FIXME implement PIO fallback for ZLPs with DMA */
865 if (ep
->dma
&& _req
->length
== 0)
868 /* set up dma mapping in case the caller didn't */
869 if (ep
->dma
&& _req
->dma
== DMA_ADDR_INVALID
) {
870 _req
->dma
= pci_map_single (dev
->pdev
, _req
->buf
, _req
->length
,
871 ep
->is_in
? PCI_DMA_TODEVICE
: PCI_DMA_FROMDEVICE
);
876 VDEBUG (dev
, "%s queue req %p, len %d buf %p\n",
877 _ep
->name
, _req
, _req
->length
, _req
->buf
);
880 spin_lock_irqsave (&dev
->lock
, flags
);
882 _req
->status
= -EINPROGRESS
;
885 /* kickstart this i/o queue? */
886 if (list_empty (&ep
->queue
) && !ep
->stopped
) {
887 /* use DMA if the endpoint supports it, else pio */
891 /* maybe there's no control data, just status ack */
892 if (ep
->num
== 0 && _req
->length
== 0) {
895 VDEBUG (dev
, "%s status ack\n", ep
->ep
.name
);
899 /* PIO ... stuff the fifo, or unblock it. */
901 write_fifo (ep
, _req
);
902 else if (list_empty (&ep
->queue
)) {
905 /* OUT FIFO might have packet(s) buffered */
906 s
= readl (&ep
->regs
->ep_stat
);
907 if ((s
& (1 << FIFO_EMPTY
)) == 0) {
908 /* note: _req->short_not_ok is
909 * ignored here since PIO _always_
910 * stops queue advance here, and
911 * _req->status doesn't change for
912 * short reads (only _req->actual)
914 if (read_fifo (ep
, req
)) {
921 s
= readl (&ep
->regs
->ep_stat
);
924 /* don't NAK, let the fifo fill */
925 if (req
&& (s
& (1 << NAK_OUT_PACKETS
)))
926 writel ((1 << CLEAR_NAK_OUT_PACKETS
),
931 } else if (ep
->dma
) {
937 /* preventing magic zlps is per-engine state, not
938 * per-transfer; irq logic must recover hiccups.
940 expect
= likely (req
->req
.zero
941 || (req
->req
.length
% ep
->ep
.maxpacket
) != 0);
942 if (expect
!= ep
->in_fifo_validate
)
945 queue_dma (ep
, req
, valid
);
947 } /* else the irq handler advances the queue. */
951 list_add_tail (&req
->queue
, &ep
->queue
);
953 spin_unlock_irqrestore (&dev
->lock
, flags
);
955 /* pci writes may still be posted */
961 struct net2280_ep
*ep
,
962 struct net2280_request
*req
,
967 req
->req
.actual
= req
->req
.length
- (DMA_BYTE_COUNT_MASK
& dmacount
);
968 done (ep
, req
, status
);
971 static void restart_dma (struct net2280_ep
*ep
);
973 static void scan_dma_completions (struct net2280_ep
*ep
)
975 /* only look at descriptors that were "naturally" retired,
976 * so fifo and list head state won't matter
978 while (!list_empty (&ep
->queue
)) {
979 struct net2280_request
*req
;
982 req
= list_entry (ep
->queue
.next
,
983 struct net2280_request
, queue
);
987 tmp
= le32_to_cpup (&req
->td
->dmacount
);
988 if ((tmp
& (1 << VALID_BIT
)) != 0)
991 /* SHORT_PACKET_TRANSFERRED_INTERRUPT handles "usb-short"
992 * cases where DMA must be aborted; this code handles
993 * all non-abort DMA completions.
995 if (unlikely (req
->td
->dmadesc
== 0)) {
997 tmp
= readl (&ep
->dma
->dmacount
);
998 if (tmp
& DMA_BYTE_COUNT_MASK
)
1000 /* single transfer mode */
1001 dma_done (ep
, req
, tmp
, 0);
1003 } else if (!ep
->is_in
1004 && (req
->req
.length
% ep
->ep
.maxpacket
) != 0) {
1005 tmp
= readl (&ep
->regs
->ep_stat
);
1007 /* AVOID TROUBLE HERE by not issuing short reads from
1008 * your gadget driver. That helps avoids errata 0121,
1009 * 0122, and 0124; not all cases trigger the warning.
1011 if ((tmp
& (1 << NAK_OUT_PACKETS
)) == 0) {
1012 WARNING (ep
->dev
, "%s lost packet sync!\n",
1014 req
->req
.status
= -EOVERFLOW
;
1015 } else if ((tmp
= readl (&ep
->regs
->ep_avail
)) != 0) {
1016 /* fifo gets flushed later */
1017 ep
->out_overflow
= 1;
1018 DEBUG (ep
->dev
, "%s dma, discard %d len %d\n",
1021 req
->req
.status
= -EOVERFLOW
;
1024 dma_done (ep
, req
, tmp
, 0);
1028 static void restart_dma (struct net2280_ep
*ep
)
1030 struct net2280_request
*req
;
1031 u32 dmactl
= dmactl_default
;
1035 req
= list_entry (ep
->queue
.next
, struct net2280_request
, queue
);
1037 if (!use_dma_chaining
) {
1038 start_dma (ep
, req
);
1042 /* the 2280 will be processing the queue unless queue hiccups after
1043 * the previous transfer:
1044 * IN: wanted automagic zlp, head doesn't (or vice versa)
1045 * DMA_FIFO_VALIDATE doesn't init from dma descriptors.
1046 * OUT: was "usb-short", we must restart.
1048 if (ep
->is_in
&& !req
->valid
) {
1049 struct net2280_request
*entry
, *prev
= NULL
;
1050 int reqmode
, done
= 0;
1052 DEBUG (ep
->dev
, "%s dma hiccup td %p\n", ep
->ep
.name
, req
->td
);
1053 ep
->in_fifo_validate
= likely (req
->req
.zero
1054 || (req
->req
.length
% ep
->ep
.maxpacket
) != 0);
1055 if (ep
->in_fifo_validate
)
1056 dmactl
|= (1 << DMA_FIFO_VALIDATE
);
1057 list_for_each_entry (entry
, &ep
->queue
, queue
) {
1062 dmacount
= entry
->td
->dmacount
;
1064 reqmode
= likely (entry
->req
.zero
1065 || (entry
->req
.length
1066 % ep
->ep
.maxpacket
) != 0);
1067 if (reqmode
== ep
->in_fifo_validate
) {
1069 dmacount
|= valid_bit
;
1070 entry
->td
->dmacount
= dmacount
;
1074 /* force a hiccup */
1075 prev
->td
->dmacount
|= dma_done_ie
;
1080 /* walk the rest of the queue so unlinks behave */
1082 dmacount
&= ~valid_bit
;
1083 entry
->td
->dmacount
= dmacount
;
1088 writel (0, &ep
->dma
->dmactl
);
1089 start_queue (ep
, dmactl
, req
->td_dma
);
1092 static void abort_dma (struct net2280_ep
*ep
)
1094 /* abort the current transfer */
1095 if (likely (!list_empty (&ep
->queue
))) {
1096 /* FIXME work around errata 0121, 0122, 0124 */
1097 writel ((1 << DMA_ABORT
), &ep
->dma
->dmastat
);
1098 spin_stop_dma (ep
->dma
);
1101 scan_dma_completions (ep
);
1104 /* dequeue ALL requests */
1105 static void nuke (struct net2280_ep
*ep
)
1107 struct net2280_request
*req
;
1109 /* called with spinlock held */
1113 while (!list_empty (&ep
->queue
)) {
1114 req
= list_entry (ep
->queue
.next
,
1115 struct net2280_request
,
1117 done (ep
, req
, -ESHUTDOWN
);
1121 /* dequeue JUST ONE request */
1122 static int net2280_dequeue (struct usb_ep
*_ep
, struct usb_request
*_req
)
1124 struct net2280_ep
*ep
;
1125 struct net2280_request
*req
;
1126 unsigned long flags
;
1130 ep
= container_of (_ep
, struct net2280_ep
, ep
);
1131 if (!_ep
|| (!ep
->desc
&& ep
->num
!= 0) || !_req
)
1134 spin_lock_irqsave (&ep
->dev
->lock
, flags
);
1135 stopped
= ep
->stopped
;
1137 /* quiesce dma while we patch the queue */
1141 dmactl
= readl (&ep
->dma
->dmactl
);
1142 /* WARNING erratum 0127 may kick in ... */
1144 scan_dma_completions (ep
);
1147 /* make sure it's still queued on this endpoint */
1148 list_for_each_entry (req
, &ep
->queue
, queue
) {
1149 if (&req
->req
== _req
)
1152 if (&req
->req
!= _req
) {
1153 spin_unlock_irqrestore (&ep
->dev
->lock
, flags
);
1157 /* queue head may be partially complete. */
1158 if (ep
->queue
.next
== &req
->queue
) {
1160 DEBUG (ep
->dev
, "unlink (%s) dma\n", _ep
->name
);
1161 _req
->status
= -ECONNRESET
;
1163 if (likely (ep
->queue
.next
== &req
->queue
)) {
1164 // NOTE: misreports single-transfer mode
1165 req
->td
->dmacount
= 0; /* invalidate */
1167 readl (&ep
->dma
->dmacount
),
1171 DEBUG (ep
->dev
, "unlink (%s) pio\n", _ep
->name
);
1172 done (ep
, req
, -ECONNRESET
);
1176 /* patch up hardware chaining data */
1177 } else if (ep
->dma
&& use_dma_chaining
) {
1178 if (req
->queue
.prev
== ep
->queue
.next
) {
1179 writel (le32_to_cpu (req
->td
->dmadesc
),
1181 if (req
->td
->dmacount
& dma_done_ie
)
1182 writel (readl (&ep
->dma
->dmacount
)
1183 | le32_to_cpu(dma_done_ie
),
1184 &ep
->dma
->dmacount
);
1186 struct net2280_request
*prev
;
1188 prev
= list_entry (req
->queue
.prev
,
1189 struct net2280_request
, queue
);
1190 prev
->td
->dmadesc
= req
->td
->dmadesc
;
1191 if (req
->td
->dmacount
& dma_done_ie
)
1192 prev
->td
->dmacount
|= dma_done_ie
;
1197 done (ep
, req
, -ECONNRESET
);
1198 ep
->stopped
= stopped
;
1201 /* turn off dma on inactive queues */
1202 if (list_empty (&ep
->queue
))
1204 else if (!ep
->stopped
) {
1205 /* resume current request, or start new one */
1207 writel (dmactl
, &ep
->dma
->dmactl
);
1209 start_dma (ep
, list_entry (ep
->queue
.next
,
1210 struct net2280_request
, queue
));
1214 spin_unlock_irqrestore (&ep
->dev
->lock
, flags
);
1218 /*-------------------------------------------------------------------------*/
1220 static int net2280_fifo_status (struct usb_ep
*_ep
);
1223 net2280_set_halt_and_wedge(struct usb_ep
*_ep
, int value
, int wedged
)
1225 struct net2280_ep
*ep
;
1226 unsigned long flags
;
1229 ep
= container_of (_ep
, struct net2280_ep
, ep
);
1230 if (!_ep
|| (!ep
->desc
&& ep
->num
!= 0))
1232 if (!ep
->dev
->driver
|| ep
->dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
1234 if (ep
->desc
/* not ep0 */ && (ep
->desc
->bmAttributes
& 0x03)
1235 == USB_ENDPOINT_XFER_ISOC
)
1238 spin_lock_irqsave (&ep
->dev
->lock
, flags
);
1239 if (!list_empty (&ep
->queue
))
1241 else if (ep
->is_in
&& value
&& net2280_fifo_status (_ep
) != 0)
1244 VDEBUG (ep
->dev
, "%s %s %s\n", _ep
->name
,
1245 value
? "set" : "clear",
1246 wedged
? "wedge" : "halt");
1247 /* set/clear, then synch memory views with the device */
1250 ep
->dev
->protocol_stall
= 1;
1259 (void) readl (&ep
->regs
->ep_rsp
);
1261 spin_unlock_irqrestore (&ep
->dev
->lock
, flags
);
1267 net2280_set_halt(struct usb_ep
*_ep
, int value
)
1269 return net2280_set_halt_and_wedge(_ep
, value
, 0);
1273 net2280_set_wedge(struct usb_ep
*_ep
)
1275 if (!_ep
|| _ep
->name
== ep0name
)
1277 return net2280_set_halt_and_wedge(_ep
, 1, 1);
1281 net2280_fifo_status (struct usb_ep
*_ep
)
1283 struct net2280_ep
*ep
;
1286 ep
= container_of (_ep
, struct net2280_ep
, ep
);
1287 if (!_ep
|| (!ep
->desc
&& ep
->num
!= 0))
1289 if (!ep
->dev
->driver
|| ep
->dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
1292 avail
= readl (&ep
->regs
->ep_avail
) & ((1 << 12) - 1);
1293 if (avail
> ep
->fifo_size
)
1296 avail
= ep
->fifo_size
- avail
;
1301 net2280_fifo_flush (struct usb_ep
*_ep
)
1303 struct net2280_ep
*ep
;
1305 ep
= container_of (_ep
, struct net2280_ep
, ep
);
1306 if (!_ep
|| (!ep
->desc
&& ep
->num
!= 0))
1308 if (!ep
->dev
->driver
|| ep
->dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
1311 writel ((1 << FIFO_FLUSH
), &ep
->regs
->ep_stat
);
1312 (void) readl (&ep
->regs
->ep_rsp
);
1315 static const struct usb_ep_ops net2280_ep_ops
= {
1316 .enable
= net2280_enable
,
1317 .disable
= net2280_disable
,
1319 .alloc_request
= net2280_alloc_request
,
1320 .free_request
= net2280_free_request
,
1322 .queue
= net2280_queue
,
1323 .dequeue
= net2280_dequeue
,
1325 .set_halt
= net2280_set_halt
,
1326 .set_wedge
= net2280_set_wedge
,
1327 .fifo_status
= net2280_fifo_status
,
1328 .fifo_flush
= net2280_fifo_flush
,
1331 /*-------------------------------------------------------------------------*/
1333 static int net2280_get_frame (struct usb_gadget
*_gadget
)
1335 struct net2280
*dev
;
1336 unsigned long flags
;
1341 dev
= container_of (_gadget
, struct net2280
, gadget
);
1342 spin_lock_irqsave (&dev
->lock
, flags
);
1343 retval
= get_idx_reg (dev
->regs
, REG_FRAME
) & 0x03ff;
1344 spin_unlock_irqrestore (&dev
->lock
, flags
);
1348 static int net2280_wakeup (struct usb_gadget
*_gadget
)
1350 struct net2280
*dev
;
1352 unsigned long flags
;
1356 dev
= container_of (_gadget
, struct net2280
, gadget
);
1358 spin_lock_irqsave (&dev
->lock
, flags
);
1359 tmp
= readl (&dev
->usb
->usbctl
);
1360 if (tmp
& (1 << DEVICE_REMOTE_WAKEUP_ENABLE
))
1361 writel (1 << GENERATE_RESUME
, &dev
->usb
->usbstat
);
1362 spin_unlock_irqrestore (&dev
->lock
, flags
);
1364 /* pci writes may still be posted */
1368 static int net2280_set_selfpowered (struct usb_gadget
*_gadget
, int value
)
1370 struct net2280
*dev
;
1372 unsigned long flags
;
1376 dev
= container_of (_gadget
, struct net2280
, gadget
);
1378 spin_lock_irqsave (&dev
->lock
, flags
);
1379 tmp
= readl (&dev
->usb
->usbctl
);
1381 tmp
|= (1 << SELF_POWERED_STATUS
);
1383 tmp
&= ~(1 << SELF_POWERED_STATUS
);
1384 writel (tmp
, &dev
->usb
->usbctl
);
1385 spin_unlock_irqrestore (&dev
->lock
, flags
);
1390 static int net2280_pullup(struct usb_gadget
*_gadget
, int is_on
)
1392 struct net2280
*dev
;
1394 unsigned long flags
;
1398 dev
= container_of (_gadget
, struct net2280
, gadget
);
1400 spin_lock_irqsave (&dev
->lock
, flags
);
1401 tmp
= readl (&dev
->usb
->usbctl
);
1402 dev
->softconnect
= (is_on
!= 0);
1404 tmp
|= (1 << USB_DETECT_ENABLE
);
1406 tmp
&= ~(1 << USB_DETECT_ENABLE
);
1407 writel (tmp
, &dev
->usb
->usbctl
);
1408 spin_unlock_irqrestore (&dev
->lock
, flags
);
1413 static int net2280_start(struct usb_gadget_driver
*driver
,
1414 int (*bind
)(struct usb_gadget
*));
1415 static int net2280_stop(struct usb_gadget_driver
*driver
);
1417 static const struct usb_gadget_ops net2280_ops
= {
1418 .get_frame
= net2280_get_frame
,
1419 .wakeup
= net2280_wakeup
,
1420 .set_selfpowered
= net2280_set_selfpowered
,
1421 .pullup
= net2280_pullup
,
1422 .start
= net2280_start
,
1423 .stop
= net2280_stop
,
1426 /*-------------------------------------------------------------------------*/
1428 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1430 /* FIXME move these into procfs, and use seq_file.
1431 * Sysfs _still_ doesn't behave for arbitrarily sized files,
1432 * and also doesn't help products using this with 2.4 kernels.
1435 /* "function" sysfs attribute */
1437 show_function (struct device
*_dev
, struct device_attribute
*attr
, char *buf
)
1439 struct net2280
*dev
= dev_get_drvdata (_dev
);
1442 || !dev
->driver
->function
1443 || strlen (dev
->driver
->function
) > PAGE_SIZE
)
1445 return scnprintf (buf
, PAGE_SIZE
, "%s\n", dev
->driver
->function
);
1447 static DEVICE_ATTR (function
, S_IRUGO
, show_function
, NULL
);
1449 static ssize_t
net2280_show_registers(struct device
*_dev
,
1450 struct device_attribute
*attr
, char *buf
)
1452 struct net2280
*dev
;
1455 unsigned long flags
;
1460 dev
= dev_get_drvdata (_dev
);
1463 spin_lock_irqsave (&dev
->lock
, flags
);
1466 s
= dev
->driver
->driver
.name
;
1470 /* Main Control Registers */
1471 t
= scnprintf (next
, size
, "%s version " DRIVER_VERSION
1472 ", chiprev %04x, dma %s\n\n"
1473 "devinit %03x fifoctl %08x gadget '%s'\n"
1474 "pci irqenb0 %02x irqenb1 %08x "
1475 "irqstat0 %04x irqstat1 %08x\n",
1476 driver_name
, dev
->chiprev
,
1478 ? (use_dma_chaining
? "chaining" : "enabled")
1480 readl (&dev
->regs
->devinit
),
1481 readl (&dev
->regs
->fifoctl
),
1483 readl (&dev
->regs
->pciirqenb0
),
1484 readl (&dev
->regs
->pciirqenb1
),
1485 readl (&dev
->regs
->irqstat0
),
1486 readl (&dev
->regs
->irqstat1
));
1490 /* USB Control Registers */
1491 t1
= readl (&dev
->usb
->usbctl
);
1492 t2
= readl (&dev
->usb
->usbstat
);
1493 if (t1
& (1 << VBUS_PIN
)) {
1494 if (t2
& (1 << HIGH_SPEED
))
1496 else if (dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
1500 /* full speed bit (6) not working?? */
1503 t
= scnprintf (next
, size
,
1504 "stdrsp %08x usbctl %08x usbstat %08x "
1505 "addr 0x%02x (%s)\n",
1506 readl (&dev
->usb
->stdrsp
), t1
, t2
,
1507 readl (&dev
->usb
->ouraddr
), s
);
1511 /* PCI Master Control Registers */
1513 /* DMA Control Registers */
1515 /* Configurable EP Control Registers */
1516 for (i
= 0; i
< 7; i
++) {
1517 struct net2280_ep
*ep
;
1523 t1
= readl (&ep
->regs
->ep_cfg
);
1524 t2
= readl (&ep
->regs
->ep_rsp
) & 0xff;
1525 t
= scnprintf (next
, size
,
1526 "\n%s\tcfg %05x rsp (%02x) %s%s%s%s%s%s%s%s"
1528 ep
->ep
.name
, t1
, t2
,
1529 (t2
& (1 << CLEAR_NAK_OUT_PACKETS
))
1531 (t2
& (1 << CLEAR_EP_HIDE_STATUS_PHASE
))
1533 (t2
& (1 << CLEAR_EP_FORCE_CRC_ERROR
))
1535 (t2
& (1 << CLEAR_INTERRUPT_MODE
))
1536 ? "interrupt " : "",
1537 (t2
& (1<<CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE
))
1539 (t2
& (1 << CLEAR_NAK_OUT_PACKETS_MODE
))
1541 (t2
& (1 << CLEAR_ENDPOINT_TOGGLE
))
1542 ? "DATA1 " : "DATA0 ",
1543 (t2
& (1 << CLEAR_ENDPOINT_HALT
))
1545 readl (&ep
->regs
->ep_irqenb
));
1549 t
= scnprintf (next
, size
,
1550 "\tstat %08x avail %04x "
1552 readl (&ep
->regs
->ep_stat
),
1553 readl (&ep
->regs
->ep_avail
),
1554 t1
& 0x0f, DIR_STRING (t1
),
1555 type_string (t1
>> 8),
1556 ep
->stopped
? "*" : "");
1563 t
= scnprintf (next
, size
,
1564 " dma\tctl %08x stat %08x count %08x\n"
1565 "\taddr %08x desc %08x\n",
1566 readl (&ep
->dma
->dmactl
),
1567 readl (&ep
->dma
->dmastat
),
1568 readl (&ep
->dma
->dmacount
),
1569 readl (&ep
->dma
->dmaaddr
),
1570 readl (&ep
->dma
->dmadesc
));
1576 /* Indexed Registers */
1580 t
= scnprintf (next
, size
, "\nirqs: ");
1583 for (i
= 0; i
< 7; i
++) {
1584 struct net2280_ep
*ep
;
1589 t
= scnprintf (next
, size
, " %s/%lu", ep
->ep
.name
, ep
->irqs
);
1594 t
= scnprintf (next
, size
, "\n");
1598 spin_unlock_irqrestore (&dev
->lock
, flags
);
1600 return PAGE_SIZE
- size
;
1602 static DEVICE_ATTR(registers
, S_IRUGO
, net2280_show_registers
, NULL
);
1605 show_queues (struct device
*_dev
, struct device_attribute
*attr
, char *buf
)
1607 struct net2280
*dev
;
1610 unsigned long flags
;
1613 dev
= dev_get_drvdata (_dev
);
1616 spin_lock_irqsave (&dev
->lock
, flags
);
1618 for (i
= 0; i
< 7; i
++) {
1619 struct net2280_ep
*ep
= &dev
->ep
[i
];
1620 struct net2280_request
*req
;
1624 const struct usb_endpoint_descriptor
*d
;
1629 t
= d
->bEndpointAddress
;
1630 t
= scnprintf (next
, size
,
1631 "\n%s (ep%d%s-%s) max %04x %s fifo %d\n",
1632 ep
->ep
.name
, t
& USB_ENDPOINT_NUMBER_MASK
,
1633 (t
& USB_DIR_IN
) ? "in" : "out",
1635 switch (d
->bmAttributes
& 0x03) {
1636 case USB_ENDPOINT_XFER_BULK
:
1637 val
= "bulk"; break;
1638 case USB_ENDPOINT_XFER_INT
:
1639 val
= "intr"; break;
1643 usb_endpoint_maxp (d
) & 0x1fff,
1644 ep
->dma
? "dma" : "pio", ep
->fifo_size
1646 } else /* ep0 should only have one transfer queued */
1647 t
= scnprintf (next
, size
, "ep0 max 64 pio %s\n",
1648 ep
->is_in
? "in" : "out");
1649 if (t
<= 0 || t
> size
)
1654 if (list_empty (&ep
->queue
)) {
1655 t
= scnprintf (next
, size
, "\t(nothing queued)\n");
1656 if (t
<= 0 || t
> size
)
1662 list_for_each_entry (req
, &ep
->queue
, queue
) {
1663 if (ep
->dma
&& req
->td_dma
== readl (&ep
->dma
->dmadesc
))
1664 t
= scnprintf (next
, size
,
1665 "\treq %p len %d/%d "
1666 "buf %p (dmacount %08x)\n",
1667 &req
->req
, req
->req
.actual
,
1668 req
->req
.length
, req
->req
.buf
,
1669 readl (&ep
->dma
->dmacount
));
1671 t
= scnprintf (next
, size
,
1672 "\treq %p len %d/%d buf %p\n",
1673 &req
->req
, req
->req
.actual
,
1674 req
->req
.length
, req
->req
.buf
);
1675 if (t
<= 0 || t
> size
)
1681 struct net2280_dma
*td
;
1684 t
= scnprintf (next
, size
, "\t td %08x "
1685 " count %08x buf %08x desc %08x\n",
1687 le32_to_cpu (td
->dmacount
),
1688 le32_to_cpu (td
->dmaaddr
),
1689 le32_to_cpu (td
->dmadesc
));
1690 if (t
<= 0 || t
> size
)
1699 spin_unlock_irqrestore (&dev
->lock
, flags
);
1700 return PAGE_SIZE
- size
;
1702 static DEVICE_ATTR (queues
, S_IRUGO
, show_queues
, NULL
);
1707 #define device_create_file(a,b) (0)
1708 #define device_remove_file(a,b) do { } while (0)
1712 /*-------------------------------------------------------------------------*/
1714 /* another driver-specific mode might be a request type doing dma
1715 * to/from another device fifo instead of to/from memory.
1718 static void set_fifo_mode (struct net2280
*dev
, int mode
)
1720 /* keeping high bits preserves BAR2 */
1721 writel ((0xffff << PCI_BASE2_RANGE
) | mode
, &dev
->regs
->fifoctl
);
1723 /* always ep-{a,b,e,f} ... maybe not ep-c or ep-d */
1724 INIT_LIST_HEAD (&dev
->gadget
.ep_list
);
1725 list_add_tail (&dev
->ep
[1].ep
.ep_list
, &dev
->gadget
.ep_list
);
1726 list_add_tail (&dev
->ep
[2].ep
.ep_list
, &dev
->gadget
.ep_list
);
1729 list_add_tail (&dev
->ep
[3].ep
.ep_list
, &dev
->gadget
.ep_list
);
1730 list_add_tail (&dev
->ep
[4].ep
.ep_list
, &dev
->gadget
.ep_list
);
1731 dev
->ep
[1].fifo_size
= dev
->ep
[2].fifo_size
= 1024;
1734 dev
->ep
[1].fifo_size
= dev
->ep
[2].fifo_size
= 2048;
1737 list_add_tail (&dev
->ep
[3].ep
.ep_list
, &dev
->gadget
.ep_list
);
1738 dev
->ep
[1].fifo_size
= 2048;
1739 dev
->ep
[2].fifo_size
= 1024;
1742 /* fifo sizes for ep0, ep-c, ep-d, ep-e, and ep-f never change */
1743 list_add_tail (&dev
->ep
[5].ep
.ep_list
, &dev
->gadget
.ep_list
);
1744 list_add_tail (&dev
->ep
[6].ep
.ep_list
, &dev
->gadget
.ep_list
);
1747 /* keeping it simple:
1748 * - one bus driver, initted first;
1749 * - one function driver, initted second
1751 * most of the work to support multiple net2280 controllers would
1752 * be to associate this gadget driver (yes?) with all of them, or
1753 * perhaps to bind specific drivers to specific devices.
1756 static struct net2280
*the_controller
;
1758 static void usb_reset (struct net2280
*dev
)
1762 dev
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1763 (void) readl (&dev
->usb
->usbctl
);
1765 net2280_led_init (dev
);
1767 /* disable automatic responses, and irqs */
1768 writel (0, &dev
->usb
->stdrsp
);
1769 writel (0, &dev
->regs
->pciirqenb0
);
1770 writel (0, &dev
->regs
->pciirqenb1
);
1772 /* clear old dma and irq state */
1773 for (tmp
= 0; tmp
< 4; tmp
++) {
1774 struct net2280_ep
*ep
= &dev
->ep
[tmp
+ 1];
1779 writel (~0, &dev
->regs
->irqstat0
),
1780 writel (~(1 << SUSPEND_REQUEST_INTERRUPT
), &dev
->regs
->irqstat1
),
1782 /* reset, and enable pci */
1783 tmp
= readl (&dev
->regs
->devinit
)
1785 | (1 << FIFO_SOFT_RESET
)
1786 | (1 << USB_SOFT_RESET
)
1787 | (1 << M8051_RESET
);
1788 writel (tmp
, &dev
->regs
->devinit
);
1790 /* standard fifo and endpoint allocations */
1791 set_fifo_mode (dev
, (fifo_mode
<= 2) ? fifo_mode
: 0);
1794 static void usb_reinit (struct net2280
*dev
)
1799 /* use_dma changes are ignored till next device re-init */
1802 /* basic endpoint init */
1803 for (tmp
= 0; tmp
< 7; tmp
++) {
1804 struct net2280_ep
*ep
= &dev
->ep
[tmp
];
1806 ep
->ep
.name
= ep_name
[tmp
];
1810 if (tmp
> 0 && tmp
<= 4) {
1811 ep
->fifo_size
= 1024;
1813 ep
->dma
= &dev
->dma
[tmp
- 1];
1816 ep
->regs
= &dev
->epregs
[tmp
];
1817 ep_reset (dev
->regs
, ep
);
1819 dev
->ep
[0].ep
.maxpacket
= 64;
1820 dev
->ep
[5].ep
.maxpacket
= 64;
1821 dev
->ep
[6].ep
.maxpacket
= 64;
1823 dev
->gadget
.ep0
= &dev
->ep
[0].ep
;
1824 dev
->ep
[0].stopped
= 0;
1825 INIT_LIST_HEAD (&dev
->gadget
.ep0
->ep_list
);
1827 /* we want to prevent lowlevel/insecure access from the USB host,
1828 * but erratum 0119 means this enable bit is ignored
1830 for (tmp
= 0; tmp
< 5; tmp
++)
1831 writel (EP_DONTUSE
, &dev
->dep
[tmp
].dep_cfg
);
1834 static void ep0_start (struct net2280
*dev
)
1836 writel ( (1 << CLEAR_EP_HIDE_STATUS_PHASE
)
1837 | (1 << CLEAR_NAK_OUT_PACKETS
)
1838 | (1 << CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE
)
1839 , &dev
->epregs
[0].ep_rsp
);
1842 * hardware optionally handles a bunch of standard requests
1843 * that the API hides from drivers anyway. have it do so.
1844 * endpoint status/features are handled in software, to
1845 * help pass tests for some dubious behavior.
1847 writel ( (1 << SET_TEST_MODE
)
1848 | (1 << SET_ADDRESS
)
1849 | (1 << DEVICE_SET_CLEAR_DEVICE_REMOTE_WAKEUP
)
1850 | (1 << GET_DEVICE_STATUS
)
1851 | (1 << GET_INTERFACE_STATUS
)
1852 , &dev
->usb
->stdrsp
);
1853 writel ( (1 << USB_ROOT_PORT_WAKEUP_ENABLE
)
1854 | (1 << SELF_POWERED_USB_DEVICE
)
1855 | (1 << REMOTE_WAKEUP_SUPPORT
)
1856 | (dev
->softconnect
<< USB_DETECT_ENABLE
)
1857 | (1 << SELF_POWERED_STATUS
)
1858 , &dev
->usb
->usbctl
);
1860 /* enable irqs so we can see ep0 and general operation */
1861 writel ( (1 << SETUP_PACKET_INTERRUPT_ENABLE
)
1862 | (1 << ENDPOINT_0_INTERRUPT_ENABLE
)
1863 , &dev
->regs
->pciirqenb0
);
1864 writel ( (1 << PCI_INTERRUPT_ENABLE
)
1865 | (1 << PCI_MASTER_ABORT_RECEIVED_INTERRUPT_ENABLE
)
1866 | (1 << PCI_TARGET_ABORT_RECEIVED_INTERRUPT_ENABLE
)
1867 | (1 << PCI_RETRY_ABORT_INTERRUPT_ENABLE
)
1868 | (1 << VBUS_INTERRUPT_ENABLE
)
1869 | (1 << ROOT_PORT_RESET_INTERRUPT_ENABLE
)
1870 | (1 << SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE
)
1871 , &dev
->regs
->pciirqenb1
);
1873 /* don't leave any writes posted */
1874 (void) readl (&dev
->usb
->usbctl
);
1877 /* when a driver is successfully registered, it will receive
1878 * control requests including set_configuration(), which enables
1879 * non-control requests. then usb traffic follows until a
1880 * disconnect is reported. then a host may connect again, or
1881 * the driver might get unbound.
1883 static int net2280_start(struct usb_gadget_driver
*driver
,
1884 int (*bind
)(struct usb_gadget
*))
1886 struct net2280
*dev
= the_controller
;
1890 /* insist on high speed support from the driver, since
1891 * (dev->usb->xcvrdiag & FORCE_FULL_SPEED_MODE)
1892 * "must not be used in normal operation"
1895 || driver
->speed
!= USB_SPEED_HIGH
1896 || !bind
|| !driver
->setup
)
1903 for (i
= 0; i
< 7; i
++)
1904 dev
->ep
[i
].irqs
= 0;
1906 /* hook up the driver ... */
1907 dev
->softconnect
= 1;
1908 driver
->driver
.bus
= NULL
;
1909 dev
->driver
= driver
;
1910 dev
->gadget
.dev
.driver
= &driver
->driver
;
1911 retval
= bind(&dev
->gadget
);
1913 DEBUG (dev
, "bind to driver %s --> %d\n",
1914 driver
->driver
.name
, retval
);
1916 dev
->gadget
.dev
.driver
= NULL
;
1920 retval
= device_create_file (&dev
->pdev
->dev
, &dev_attr_function
);
1921 if (retval
) goto err_unbind
;
1922 retval
= device_create_file (&dev
->pdev
->dev
, &dev_attr_queues
);
1923 if (retval
) goto err_func
;
1925 /* ... then enable host detection and ep0; and we're ready
1926 * for set_configuration as well as eventual disconnect.
1928 net2280_led_active (dev
, 1);
1931 DEBUG (dev
, "%s ready, usbctl %08x stdrsp %08x\n",
1932 driver
->driver
.name
,
1933 readl (&dev
->usb
->usbctl
),
1934 readl (&dev
->usb
->stdrsp
));
1936 /* pci writes may still be posted */
1940 device_remove_file (&dev
->pdev
->dev
, &dev_attr_function
);
1942 driver
->unbind (&dev
->gadget
);
1943 dev
->gadget
.dev
.driver
= NULL
;
1949 stop_activity (struct net2280
*dev
, struct usb_gadget_driver
*driver
)
1953 /* don't disconnect if it's not connected */
1954 if (dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
1957 /* stop hardware; prevent new request submissions;
1958 * and kill any outstanding requests.
1961 for (i
= 0; i
< 7; i
++)
1962 nuke (&dev
->ep
[i
]);
1964 /* report disconnect; the driver is already quiesced */
1966 spin_unlock (&dev
->lock
);
1967 driver
->disconnect (&dev
->gadget
);
1968 spin_lock (&dev
->lock
);
1974 static int net2280_stop(struct usb_gadget_driver
*driver
)
1976 struct net2280
*dev
= the_controller
;
1977 unsigned long flags
;
1981 if (!driver
|| driver
!= dev
->driver
|| !driver
->unbind
)
1984 spin_lock_irqsave (&dev
->lock
, flags
);
1985 stop_activity (dev
, driver
);
1986 spin_unlock_irqrestore (&dev
->lock
, flags
);
1988 net2280_pullup (&dev
->gadget
, 0);
1990 driver
->unbind (&dev
->gadget
);
1991 dev
->gadget
.dev
.driver
= NULL
;
1994 net2280_led_active (dev
, 0);
1995 device_remove_file (&dev
->pdev
->dev
, &dev_attr_function
);
1996 device_remove_file (&dev
->pdev
->dev
, &dev_attr_queues
);
1998 DEBUG (dev
, "unregistered driver '%s'\n", driver
->driver
.name
);
2002 /*-------------------------------------------------------------------------*/
2004 /* handle ep0, ep-e, ep-f with 64 byte packets: packet per irq.
2005 * also works for dma-capable endpoints, in pio mode or just
2006 * to manually advance the queue after short OUT transfers.
2008 static void handle_ep_small (struct net2280_ep
*ep
)
2010 struct net2280_request
*req
;
2012 /* 0 error, 1 mid-data, 2 done */
2015 if (!list_empty (&ep
->queue
))
2016 req
= list_entry (ep
->queue
.next
,
2017 struct net2280_request
, queue
);
2021 /* ack all, and handle what we care about */
2022 t
= readl (&ep
->regs
->ep_stat
);
2025 VDEBUG (ep
->dev
, "%s ack ep_stat %08x, req %p\n",
2026 ep
->ep
.name
, t
, req
? &req
->req
: 0);
2028 if (!ep
->is_in
|| ep
->dev
->pdev
->device
== 0x2280)
2029 writel (t
& ~(1 << NAK_OUT_PACKETS
), &ep
->regs
->ep_stat
);
2031 /* Added for 2282 */
2032 writel (t
, &ep
->regs
->ep_stat
);
2034 /* for ep0, monitor token irqs to catch data stage length errors
2035 * and to synchronize on status.
2037 * also, to defer reporting of protocol stalls ... here's where
2038 * data or status first appears, handling stalls here should never
2039 * cause trouble on the host side..
2041 * control requests could be slightly faster without token synch for
2042 * status, but status can jam up that way.
2044 if (unlikely (ep
->num
== 0)) {
2046 /* status; stop NAKing */
2047 if (t
& (1 << DATA_OUT_PING_TOKEN_INTERRUPT
)) {
2048 if (ep
->dev
->protocol_stall
) {
2055 /* reply to extra IN data tokens with a zlp */
2056 } else if (t
& (1 << DATA_IN_TOKEN_INTERRUPT
)) {
2057 if (ep
->dev
->protocol_stall
) {
2061 } else if (ep
->responded
&&
2062 !req
&& !ep
->stopped
)
2063 write_fifo (ep
, NULL
);
2066 /* status; stop NAKing */
2067 if (t
& (1 << DATA_IN_TOKEN_INTERRUPT
)) {
2068 if (ep
->dev
->protocol_stall
) {
2073 /* an extra OUT token is an error */
2074 } else if (((t
& (1 << DATA_OUT_PING_TOKEN_INTERRUPT
))
2076 && req
->req
.actual
== req
->req
.length
)
2077 || (ep
->responded
&& !req
)) {
2078 ep
->dev
->protocol_stall
= 1;
2082 done (ep
, req
, -EOVERFLOW
);
2088 if (unlikely (!req
))
2091 /* manual DMA queue advance after short OUT */
2092 if (likely (ep
->dma
!= 0)) {
2093 if (t
& (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT
)) {
2095 int stopped
= ep
->stopped
;
2097 /* TRANSFERRED works around OUT_DONE erratum 0112.
2098 * we expect (N <= maxpacket) bytes; host wrote M.
2099 * iff (M < N) we won't ever see a DMA interrupt.
2102 for (count
= 0; ; t
= readl (&ep
->regs
->ep_stat
)) {
2104 /* any preceding dma transfers must finish.
2105 * dma handles (M >= N), may empty the queue
2107 scan_dma_completions (ep
);
2108 if (unlikely (list_empty (&ep
->queue
)
2109 || ep
->out_overflow
)) {
2113 req
= list_entry (ep
->queue
.next
,
2114 struct net2280_request
, queue
);
2116 /* here either (M < N), a "real" short rx;
2117 * or (M == N) and the queue didn't empty
2119 if (likely (t
& (1 << FIFO_EMPTY
))) {
2120 count
= readl (&ep
->dma
->dmacount
);
2121 count
&= DMA_BYTE_COUNT_MASK
;
2122 if (readl (&ep
->dma
->dmadesc
)
2130 /* stop DMA, leave ep NAKing */
2131 writel ((1 << DMA_ABORT
), &ep
->dma
->dmastat
);
2132 spin_stop_dma (ep
->dma
);
2135 req
->td
->dmacount
= 0;
2136 t
= readl (&ep
->regs
->ep_avail
);
2137 dma_done (ep
, req
, count
,
2138 (ep
->out_overflow
|| t
)
2142 /* also flush to prevent erratum 0106 trouble */
2143 if (unlikely (ep
->out_overflow
2144 || (ep
->dev
->chiprev
== 0x0100
2145 && ep
->dev
->gadget
.speed
2146 == USB_SPEED_FULL
))) {
2148 ep
->out_overflow
= 0;
2151 /* (re)start dma if needed, stop NAKing */
2152 ep
->stopped
= stopped
;
2153 if (!list_empty (&ep
->queue
))
2156 DEBUG (ep
->dev
, "%s dma ep_stat %08x ??\n",
2160 /* data packet(s) received (in the fifo, OUT) */
2161 } else if (t
& (1 << DATA_PACKET_RECEIVED_INTERRUPT
)) {
2162 if (read_fifo (ep
, req
) && ep
->num
!= 0)
2165 /* data packet(s) transmitted (IN) */
2166 } else if (t
& (1 << DATA_PACKET_TRANSMITTED_INTERRUPT
)) {
2169 len
= req
->req
.length
- req
->req
.actual
;
2170 if (len
> ep
->ep
.maxpacket
)
2171 len
= ep
->ep
.maxpacket
;
2172 req
->req
.actual
+= len
;
2174 /* if we wrote it all, we're usually done */
2175 if (req
->req
.actual
== req
->req
.length
) {
2177 /* send zlps until the status stage */
2178 } else if (!req
->req
.zero
|| len
!= ep
->ep
.maxpacket
)
2182 /* there was nothing to do ... */
2183 } else if (mode
== 1)
2188 /* stream endpoints often resubmit/unlink in completion */
2191 /* maybe advance queue to next request */
2193 /* NOTE: net2280 could let gadget driver start the
2194 * status stage later. since not all controllers let
2195 * them control that, the api doesn't (yet) allow it.
2201 if (!list_empty (&ep
->queue
) && !ep
->stopped
)
2202 req
= list_entry (ep
->queue
.next
,
2203 struct net2280_request
, queue
);
2206 if (req
&& !ep
->is_in
)
2207 stop_out_naking (ep
);
2211 /* is there a buffer for the next packet?
2212 * for best streaming performance, make sure there is one.
2214 if (req
&& !ep
->stopped
) {
2216 /* load IN fifo with next packet (may be zlp) */
2217 if (t
& (1 << DATA_PACKET_TRANSMITTED_INTERRUPT
))
2218 write_fifo (ep
, &req
->req
);
2222 static struct net2280_ep
*
2223 get_ep_by_addr (struct net2280
*dev
, u16 wIndex
)
2225 struct net2280_ep
*ep
;
2227 if ((wIndex
& USB_ENDPOINT_NUMBER_MASK
) == 0)
2228 return &dev
->ep
[0];
2229 list_for_each_entry (ep
, &dev
->gadget
.ep_list
, ep
.ep_list
) {
2230 u8 bEndpointAddress
;
2234 bEndpointAddress
= ep
->desc
->bEndpointAddress
;
2235 if ((wIndex
^ bEndpointAddress
) & USB_DIR_IN
)
2237 if ((wIndex
& 0x0f) == (bEndpointAddress
& 0x0f))
2243 static void handle_stat0_irqs (struct net2280
*dev
, u32 stat
)
2245 struct net2280_ep
*ep
;
2248 /* most of these don't need individual acks */
2249 stat
&= ~(1 << INTA_ASSERTED
);
2252 // DEBUG (dev, "irqstat0 %04x\n", stat);
2254 /* starting a control request? */
2255 if (unlikely (stat
& (1 << SETUP_PACKET_INTERRUPT
))) {
2258 struct usb_ctrlrequest r
;
2261 struct net2280_request
*req
;
2263 if (dev
->gadget
.speed
== USB_SPEED_UNKNOWN
) {
2264 if (readl (&dev
->usb
->usbstat
) & (1 << HIGH_SPEED
))
2265 dev
->gadget
.speed
= USB_SPEED_HIGH
;
2267 dev
->gadget
.speed
= USB_SPEED_FULL
;
2268 net2280_led_speed (dev
, dev
->gadget
.speed
);
2269 DEBUG (dev
, "%s speed\n",
2270 (dev
->gadget
.speed
== USB_SPEED_HIGH
)
2277 /* make sure any leftover request state is cleared */
2278 stat
&= ~(1 << ENDPOINT_0_INTERRUPT
);
2279 while (!list_empty (&ep
->queue
)) {
2280 req
= list_entry (ep
->queue
.next
,
2281 struct net2280_request
, queue
);
2282 done (ep
, req
, (req
->req
.actual
== req
->req
.length
)
2286 dev
->protocol_stall
= 0;
2288 if (ep
->dev
->pdev
->device
== 0x2280)
2289 tmp
= (1 << FIFO_OVERFLOW
)
2290 | (1 << FIFO_UNDERFLOW
);
2294 writel (tmp
| (1 << TIMEOUT
)
2295 | (1 << USB_STALL_SENT
)
2296 | (1 << USB_IN_NAK_SENT
)
2297 | (1 << USB_IN_ACK_RCVD
)
2298 | (1 << USB_OUT_PING_NAK_SENT
)
2299 | (1 << USB_OUT_ACK_SENT
)
2300 | (1 << SHORT_PACKET_OUT_DONE_INTERRUPT
)
2301 | (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT
)
2302 | (1 << DATA_PACKET_RECEIVED_INTERRUPT
)
2303 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT
)
2304 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT
)
2305 | (1 << DATA_IN_TOKEN_INTERRUPT
)
2306 , &ep
->regs
->ep_stat
);
2307 u
.raw
[0] = readl (&dev
->usb
->setup0123
);
2308 u
.raw
[1] = readl (&dev
->usb
->setup4567
);
2310 cpu_to_le32s (&u
.raw
[0]);
2311 cpu_to_le32s (&u
.raw
[1]);
2315 #define w_value le16_to_cpu(u.r.wValue)
2316 #define w_index le16_to_cpu(u.r.wIndex)
2317 #define w_length le16_to_cpu(u.r.wLength)
2320 writel (1 << SETUP_PACKET_INTERRUPT
, &dev
->regs
->irqstat0
);
2321 stat
^= (1 << SETUP_PACKET_INTERRUPT
);
2323 /* watch control traffic at the token level, and force
2324 * synchronization before letting the status stage happen.
2325 * FIXME ignore tokens we'll NAK, until driver responds.
2326 * that'll mean a lot less irqs for some drivers.
2328 ep
->is_in
= (u
.r
.bRequestType
& USB_DIR_IN
) != 0;
2330 scratch
= (1 << DATA_PACKET_TRANSMITTED_INTERRUPT
)
2331 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT
)
2332 | (1 << DATA_IN_TOKEN_INTERRUPT
);
2333 stop_out_naking (ep
);
2335 scratch
= (1 << DATA_PACKET_RECEIVED_INTERRUPT
)
2336 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT
)
2337 | (1 << DATA_IN_TOKEN_INTERRUPT
);
2338 writel (scratch
, &dev
->epregs
[0].ep_irqenb
);
2340 /* we made the hardware handle most lowlevel requests;
2341 * everything else goes uplevel to the gadget code.
2344 switch (u
.r
.bRequest
) {
2345 case USB_REQ_GET_STATUS
: {
2346 struct net2280_ep
*e
;
2349 /* hw handles device and interface status */
2350 if (u
.r
.bRequestType
!= (USB_DIR_IN
|USB_RECIP_ENDPOINT
))
2352 if ((e
= get_ep_by_addr (dev
, w_index
)) == 0
2356 if (readl (&e
->regs
->ep_rsp
)
2357 & (1 << SET_ENDPOINT_HALT
))
2358 status
= cpu_to_le32 (1);
2360 status
= cpu_to_le32 (0);
2362 /* don't bother with a request object! */
2363 writel (0, &dev
->epregs
[0].ep_irqenb
);
2364 set_fifo_bytecount (ep
, w_length
);
2365 writel ((__force u32
)status
, &dev
->epregs
[0].ep_data
);
2367 VDEBUG (dev
, "%s stat %02x\n", ep
->ep
.name
, status
);
2368 goto next_endpoints
;
2371 case USB_REQ_CLEAR_FEATURE
: {
2372 struct net2280_ep
*e
;
2374 /* hw handles device features */
2375 if (u
.r
.bRequestType
!= USB_RECIP_ENDPOINT
)
2377 if (w_value
!= USB_ENDPOINT_HALT
2380 if ((e
= get_ep_by_addr (dev
, w_index
)) == 0)
2383 VDEBUG(dev
, "%s wedged, halt not cleared\n",
2386 VDEBUG(dev
, "%s clear halt\n", ep
->ep
.name
);
2390 goto next_endpoints
;
2393 case USB_REQ_SET_FEATURE
: {
2394 struct net2280_ep
*e
;
2396 /* hw handles device features */
2397 if (u
.r
.bRequestType
!= USB_RECIP_ENDPOINT
)
2399 if (w_value
!= USB_ENDPOINT_HALT
2402 if ((e
= get_ep_by_addr (dev
, w_index
)) == 0)
2404 if (e
->ep
.name
== ep0name
)
2408 VDEBUG (dev
, "%s set halt\n", ep
->ep
.name
);
2409 goto next_endpoints
;
2414 VDEBUG (dev
, "setup %02x.%02x v%04x i%04x l%04x "
2416 u
.r
.bRequestType
, u
.r
.bRequest
,
2417 w_value
, w_index
, w_length
,
2418 readl (&ep
->regs
->ep_cfg
));
2420 spin_unlock (&dev
->lock
);
2421 tmp
= dev
->driver
->setup (&dev
->gadget
, &u
.r
);
2422 spin_lock (&dev
->lock
);
2425 /* stall ep0 on error */
2428 VDEBUG (dev
, "req %02x.%02x protocol STALL; stat %d\n",
2429 u
.r
.bRequestType
, u
.r
.bRequest
, tmp
);
2430 dev
->protocol_stall
= 1;
2433 /* some in/out token irq should follow; maybe stall then.
2434 * driver must queue a request (even zlp) or halt ep0
2435 * before the host times out.
2444 /* endpoint data irq ? */
2445 scratch
= stat
& 0x7f;
2447 for (num
= 0; scratch
; num
++) {
2450 /* do this endpoint's FIFO and queue need tending? */
2452 if ((scratch
& t
) == 0)
2456 ep
= &dev
->ep
[num
];
2457 handle_ep_small (ep
);
2461 DEBUG (dev
, "unhandled irqstat0 %08x\n", stat
);
2464 #define DMA_INTERRUPTS ( \
2465 (1 << DMA_D_INTERRUPT) \
2466 | (1 << DMA_C_INTERRUPT) \
2467 | (1 << DMA_B_INTERRUPT) \
2468 | (1 << DMA_A_INTERRUPT))
2469 #define PCI_ERROR_INTERRUPTS ( \
2470 (1 << PCI_MASTER_ABORT_RECEIVED_INTERRUPT) \
2471 | (1 << PCI_TARGET_ABORT_RECEIVED_INTERRUPT) \
2472 | (1 << PCI_RETRY_ABORT_INTERRUPT))
2474 static void handle_stat1_irqs (struct net2280
*dev
, u32 stat
)
2476 struct net2280_ep
*ep
;
2477 u32 tmp
, num
, mask
, scratch
;
2479 /* after disconnect there's nothing else to do! */
2480 tmp
= (1 << VBUS_INTERRUPT
) | (1 << ROOT_PORT_RESET_INTERRUPT
);
2481 mask
= (1 << HIGH_SPEED
) | (1 << FULL_SPEED
);
2483 /* VBUS disconnect is indicated by VBUS_PIN and VBUS_INTERRUPT set.
2484 * Root Port Reset is indicated by ROOT_PORT_RESET_INTERRRUPT set and
2485 * both HIGH_SPEED and FULL_SPEED clear (as ROOT_PORT_RESET_INTERRUPT
2486 * only indicates a change in the reset state).
2489 writel (tmp
, &dev
->regs
->irqstat1
);
2490 if ((((stat
& (1 << ROOT_PORT_RESET_INTERRUPT
))
2491 && ((readl (&dev
->usb
->usbstat
) & mask
)
2493 || ((readl (&dev
->usb
->usbctl
)
2494 & (1 << VBUS_PIN
)) == 0)
2495 ) && ( dev
->gadget
.speed
!= USB_SPEED_UNKNOWN
)) {
2496 DEBUG (dev
, "disconnect %s\n",
2497 dev
->driver
->driver
.name
);
2498 stop_activity (dev
, dev
->driver
);
2504 /* vBUS can bounce ... one of many reasons to ignore the
2505 * notion of hotplug events on bus connect/disconnect!
2511 /* NOTE: chip stays in PCI D0 state for now, but it could
2512 * enter D1 to save more power
2514 tmp
= (1 << SUSPEND_REQUEST_CHANGE_INTERRUPT
);
2516 writel (tmp
, &dev
->regs
->irqstat1
);
2517 if (stat
& (1 << SUSPEND_REQUEST_INTERRUPT
)) {
2518 if (dev
->driver
->suspend
)
2519 dev
->driver
->suspend (&dev
->gadget
);
2520 if (!enable_suspend
)
2521 stat
&= ~(1 << SUSPEND_REQUEST_INTERRUPT
);
2523 if (dev
->driver
->resume
)
2524 dev
->driver
->resume (&dev
->gadget
);
2525 /* at high speed, note erratum 0133 */
2530 /* clear any other status/irqs */
2532 writel (stat
, &dev
->regs
->irqstat1
);
2534 /* some status we can just ignore */
2535 if (dev
->pdev
->device
== 0x2280)
2536 stat
&= ~((1 << CONTROL_STATUS_INTERRUPT
)
2537 | (1 << SUSPEND_REQUEST_INTERRUPT
)
2538 | (1 << RESUME_INTERRUPT
)
2539 | (1 << SOF_INTERRUPT
));
2541 stat
&= ~((1 << CONTROL_STATUS_INTERRUPT
)
2542 | (1 << RESUME_INTERRUPT
)
2543 | (1 << SOF_DOWN_INTERRUPT
)
2544 | (1 << SOF_INTERRUPT
));
2548 // DEBUG (dev, "irqstat1 %08x\n", stat);
2550 /* DMA status, for ep-{a,b,c,d} */
2551 scratch
= stat
& DMA_INTERRUPTS
;
2552 stat
&= ~DMA_INTERRUPTS
;
2554 for (num
= 0; scratch
; num
++) {
2555 struct net2280_dma_regs __iomem
*dma
;
2558 if ((tmp
& scratch
) == 0)
2562 ep
= &dev
->ep
[num
+ 1];
2568 /* clear ep's dma status */
2569 tmp
= readl (&dma
->dmastat
);
2570 writel (tmp
, &dma
->dmastat
);
2572 /* chaining should stop on abort, short OUT from fifo,
2573 * or (stat0 codepath) short OUT transfer.
2575 if (!use_dma_chaining
) {
2576 if ((tmp
& (1 << DMA_TRANSACTION_DONE_INTERRUPT
))
2578 DEBUG (ep
->dev
, "%s no xact done? %08x\n",
2585 /* OUT transfers terminate when the data from the
2586 * host is in our memory. Process whatever's done.
2587 * On this path, we know transfer's last packet wasn't
2588 * less than req->length. NAK_OUT_PACKETS may be set,
2589 * or the FIFO may already be holding new packets.
2591 * IN transfers can linger in the FIFO for a very
2592 * long time ... we ignore that for now, accounting
2593 * precisely (like PIO does) needs per-packet irqs
2595 scan_dma_completions (ep
);
2597 /* disable dma on inactive queues; else maybe restart */
2598 if (list_empty (&ep
->queue
)) {
2599 if (use_dma_chaining
)
2602 tmp
= readl (&dma
->dmactl
);
2603 if (!use_dma_chaining
2604 || (tmp
& (1 << DMA_ENABLE
)) == 0)
2606 else if (ep
->is_in
&& use_dma_chaining
) {
2607 struct net2280_request
*req
;
2610 /* the descriptor at the head of the chain
2611 * may still have VALID_BIT clear; that's
2612 * used to trigger changing DMA_FIFO_VALIDATE
2613 * (affects automagic zlp writes).
2615 req
= list_entry (ep
->queue
.next
,
2616 struct net2280_request
, queue
);
2617 dmacount
= req
->td
->dmacount
;
2618 dmacount
&= cpu_to_le32 (
2620 | DMA_BYTE_COUNT_MASK
);
2621 if (dmacount
&& (dmacount
& valid_bit
) == 0)
2628 /* NOTE: there are other PCI errors we might usefully notice.
2629 * if they appear very often, here's where to try recovering.
2631 if (stat
& PCI_ERROR_INTERRUPTS
) {
2632 ERROR (dev
, "pci dma error; stat %08x\n", stat
);
2633 stat
&= ~PCI_ERROR_INTERRUPTS
;
2634 /* these are fatal errors, but "maybe" they won't
2637 stop_activity (dev
, dev
->driver
);
2643 DEBUG (dev
, "unhandled irqstat1 %08x\n", stat
);
2646 static irqreturn_t
net2280_irq (int irq
, void *_dev
)
2648 struct net2280
*dev
= _dev
;
2650 /* shared interrupt, not ours */
2651 if (!(readl(&dev
->regs
->irqstat0
) & (1 << INTA_ASSERTED
)))
2654 spin_lock (&dev
->lock
);
2656 /* handle disconnect, dma, and more */
2657 handle_stat1_irqs (dev
, readl (&dev
->regs
->irqstat1
));
2659 /* control requests and PIO */
2660 handle_stat0_irqs (dev
, readl (&dev
->regs
->irqstat0
));
2662 spin_unlock (&dev
->lock
);
2667 /*-------------------------------------------------------------------------*/
2669 static void gadget_release (struct device
*_dev
)
2671 struct net2280
*dev
= dev_get_drvdata (_dev
);
2676 /* tear down the binding between this driver and the pci device */
2678 static void net2280_remove (struct pci_dev
*pdev
)
2680 struct net2280
*dev
= pci_get_drvdata (pdev
);
2682 usb_del_gadget_udc(&dev
->gadget
);
2684 BUG_ON(dev
->driver
);
2686 /* then clean up the resources we allocated during probe() */
2687 net2280_led_shutdown (dev
);
2688 if (dev
->requests
) {
2690 for (i
= 1; i
< 5; i
++) {
2691 if (!dev
->ep
[i
].dummy
)
2693 pci_pool_free (dev
->requests
, dev
->ep
[i
].dummy
,
2694 dev
->ep
[i
].td_dma
);
2696 pci_pool_destroy (dev
->requests
);
2699 free_irq (pdev
->irq
, dev
);
2701 iounmap (dev
->regs
);
2703 release_mem_region (pci_resource_start (pdev
, 0),
2704 pci_resource_len (pdev
, 0));
2706 pci_disable_device (pdev
);
2707 device_unregister (&dev
->gadget
.dev
);
2708 device_remove_file (&pdev
->dev
, &dev_attr_registers
);
2709 pci_set_drvdata (pdev
, NULL
);
2711 INFO (dev
, "unbind\n");
2713 the_controller
= NULL
;
2716 /* wrap this driver around the specified device, but
2717 * don't respond over USB until a gadget driver binds to us.
2720 static int net2280_probe (struct pci_dev
*pdev
, const struct pci_device_id
*id
)
2722 struct net2280
*dev
;
2723 unsigned long resource
, len
;
2724 void __iomem
*base
= NULL
;
2727 /* if you want to support more than one controller in a system,
2728 * usb_gadget_driver_{register,unregister}() must change.
2730 if (the_controller
) {
2731 dev_warn (&pdev
->dev
, "ignoring\n");
2735 /* alloc, and start init */
2736 dev
= kzalloc (sizeof *dev
, GFP_KERNEL
);
2742 pci_set_drvdata (pdev
, dev
);
2743 spin_lock_init (&dev
->lock
);
2745 dev
->gadget
.ops
= &net2280_ops
;
2746 dev
->gadget
.is_dualspeed
= 1;
2748 /* the "gadget" abstracts/virtualizes the controller */
2749 dev_set_name(&dev
->gadget
.dev
, "gadget");
2750 dev
->gadget
.dev
.parent
= &pdev
->dev
;
2751 dev
->gadget
.dev
.dma_mask
= pdev
->dev
.dma_mask
;
2752 dev
->gadget
.dev
.release
= gadget_release
;
2753 dev
->gadget
.name
= driver_name
;
2755 /* now all the pci goodies ... */
2756 if (pci_enable_device (pdev
) < 0) {
2762 /* BAR 0 holds all the registers
2763 * BAR 1 is 8051 memory; unused here (note erratum 0103)
2764 * BAR 2 is fifo memory; unused here
2766 resource
= pci_resource_start (pdev
, 0);
2767 len
= pci_resource_len (pdev
, 0);
2768 if (!request_mem_region (resource
, len
, driver_name
)) {
2769 DEBUG (dev
, "controller already in use\n");
2775 /* FIXME provide firmware download interface to put
2776 * 8051 code into the chip, e.g. to turn on PCI PM.
2779 base
= ioremap_nocache (resource
, len
);
2781 DEBUG (dev
, "can't map memory\n");
2785 dev
->regs
= (struct net2280_regs __iomem
*) base
;
2786 dev
->usb
= (struct net2280_usb_regs __iomem
*) (base
+ 0x0080);
2787 dev
->pci
= (struct net2280_pci_regs __iomem
*) (base
+ 0x0100);
2788 dev
->dma
= (struct net2280_dma_regs __iomem
*) (base
+ 0x0180);
2789 dev
->dep
= (struct net2280_dep_regs __iomem
*) (base
+ 0x0200);
2790 dev
->epregs
= (struct net2280_ep_regs __iomem
*) (base
+ 0x0300);
2792 /* put into initial config, link up all endpoints */
2793 writel (0, &dev
->usb
->usbctl
);
2797 /* irq setup after old hardware is cleaned up */
2799 ERROR (dev
, "No IRQ. Check PCI setup!\n");
2804 if (request_irq (pdev
->irq
, net2280_irq
, IRQF_SHARED
, driver_name
, dev
)
2806 ERROR (dev
, "request interrupt %d failed\n", pdev
->irq
);
2813 /* NOTE: we know only the 32 LSBs of dma addresses may be nonzero */
2814 dev
->requests
= pci_pool_create ("requests", pdev
,
2815 sizeof (struct net2280_dma
),
2816 0 /* no alignment requirements */,
2817 0 /* or page-crossing issues */);
2818 if (!dev
->requests
) {
2819 DEBUG (dev
, "can't get request pool\n");
2823 for (i
= 1; i
< 5; i
++) {
2824 struct net2280_dma
*td
;
2826 td
= pci_pool_alloc (dev
->requests
, GFP_KERNEL
,
2827 &dev
->ep
[i
].td_dma
);
2829 DEBUG (dev
, "can't get dummy %d\n", i
);
2833 td
->dmacount
= 0; /* not VALID */
2834 td
->dmaaddr
= cpu_to_le32 (DMA_ADDR_INVALID
);
2835 td
->dmadesc
= td
->dmaaddr
;
2836 dev
->ep
[i
].dummy
= td
;
2839 /* enable lower-overhead pci memory bursts during DMA */
2840 writel ( (1 << DMA_MEMORY_WRITE_AND_INVALIDATE_ENABLE
)
2841 // 256 write retries may not be enough...
2842 // | (1 << PCI_RETRY_ABORT_ENABLE)
2843 | (1 << DMA_READ_MULTIPLE_ENABLE
)
2844 | (1 << DMA_READ_LINE_ENABLE
)
2845 , &dev
->pci
->pcimstctl
);
2846 /* erratum 0115 shouldn't appear: Linux inits PCI_LATENCY_TIMER */
2847 pci_set_master (pdev
);
2848 pci_try_set_mwi (pdev
);
2850 /* ... also flushes any posted pci writes */
2851 dev
->chiprev
= get_idx_reg (dev
->regs
, REG_CHIPREV
) & 0xffff;
2854 INFO (dev
, "%s\n", driver_desc
);
2855 INFO (dev
, "irq %d, pci mem %p, chip rev %04x\n",
2856 pdev
->irq
, base
, dev
->chiprev
);
2857 INFO (dev
, "version: " DRIVER_VERSION
"; dma %s\n",
2859 ? (use_dma_chaining
? "chaining" : "enabled")
2861 the_controller
= dev
;
2863 retval
= device_register (&dev
->gadget
.dev
);
2864 if (retval
) goto done
;
2865 retval
= device_create_file (&pdev
->dev
, &dev_attr_registers
);
2866 if (retval
) goto done
;
2868 retval
= usb_add_gadget_udc(&pdev
->dev
, &dev
->gadget
);
2875 net2280_remove (pdev
);
2879 /* make sure the board is quiescent; otherwise it will continue
2880 * generating IRQs across the upcoming reboot.
2883 static void net2280_shutdown (struct pci_dev
*pdev
)
2885 struct net2280
*dev
= pci_get_drvdata (pdev
);
2888 writel (0, &dev
->regs
->pciirqenb0
);
2889 writel (0, &dev
->regs
->pciirqenb1
);
2891 /* disable the pullup so the host will think we're gone */
2892 writel (0, &dev
->usb
->usbctl
);
2896 /*-------------------------------------------------------------------------*/
2898 static const struct pci_device_id pci_ids
[] = { {
2899 .class = ((PCI_CLASS_SERIAL_USB
<< 8) | 0xfe),
2903 .subvendor
= PCI_ANY_ID
,
2904 .subdevice
= PCI_ANY_ID
,
2906 .class = ((PCI_CLASS_SERIAL_USB
<< 8) | 0xfe),
2910 .subvendor
= PCI_ANY_ID
,
2911 .subdevice
= PCI_ANY_ID
,
2913 }, { /* end: all zeroes */ }
2915 MODULE_DEVICE_TABLE (pci
, pci_ids
);
2917 /* pci driver glue; this is a "new style" PCI driver module */
2918 static struct pci_driver net2280_pci_driver
= {
2919 .name
= (char *) driver_name
,
2920 .id_table
= pci_ids
,
2922 .probe
= net2280_probe
,
2923 .remove
= net2280_remove
,
2924 .shutdown
= net2280_shutdown
,
2926 /* FIXME add power management support */
2929 MODULE_DESCRIPTION (DRIVER_DESC
);
2930 MODULE_AUTHOR ("David Brownell");
2931 MODULE_LICENSE ("GPL");
2933 static int __init
init (void)
2936 use_dma_chaining
= 0;
2937 return pci_register_driver (&net2280_pci_driver
);
2941 static void __exit
cleanup (void)
2943 pci_unregister_driver (&net2280_pci_driver
);
2945 module_exit (cleanup
);