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.
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
);
120 #define DIR_STRING(bAddress) (((bAddress) & USB_DIR_IN) ? "in" : "out")
122 #if defined(CONFIG_USB_GADGET_DEBUG_FILES) || defined (DEBUG)
123 static char *type_string (u8 bmAttributes
)
125 switch ((bmAttributes
) & USB_ENDPOINT_XFERTYPE_MASK
) {
126 case USB_ENDPOINT_XFER_BULK
: return "bulk";
127 case USB_ENDPOINT_XFER_ISOC
: return "iso";
128 case USB_ENDPOINT_XFER_INT
: return "intr";
136 #define valid_bit cpu_to_le32 (1 << VALID_BIT)
137 #define dma_done_ie cpu_to_le32 (1 << DMA_DONE_INTERRUPT_ENABLE)
139 /*-------------------------------------------------------------------------*/
142 net2280_enable (struct usb_ep
*_ep
, const struct usb_endpoint_descriptor
*desc
)
145 struct net2280_ep
*ep
;
149 ep
= container_of (_ep
, struct net2280_ep
, ep
);
150 if (!_ep
|| !desc
|| ep
->desc
|| _ep
->name
== ep0name
151 || desc
->bDescriptorType
!= USB_DT_ENDPOINT
)
154 if (!dev
->driver
|| dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
157 /* erratum 0119 workaround ties up an endpoint number */
158 if ((desc
->bEndpointAddress
& 0x0f) == EP_DONTUSE
)
161 /* sanity check ep-e/ep-f since their fifos are small */
162 max
= usb_endpoint_maxp (desc
) & 0x1fff;
163 if (ep
->num
> 4 && max
> 64)
166 spin_lock_irqsave (&dev
->lock
, flags
);
167 _ep
->maxpacket
= max
& 0x7ff;
170 /* ep_reset() has already been called */
173 ep
->out_overflow
= 0;
175 /* set speed-dependent max packet; may kick in high bandwidth */
176 set_idx_reg (dev
->regs
, REG_EP_MAXPKT (dev
, ep
->num
), max
);
178 /* FIFO lines can't go to different packets. PIO is ok, so
179 * use it instead of troublesome (non-bulk) multi-packet DMA.
181 if (ep
->dma
&& (max
% 4) != 0 && use_dma_chaining
) {
182 DEBUG (ep
->dev
, "%s, no dma for maxpacket %d\n",
183 ep
->ep
.name
, ep
->ep
.maxpacket
);
187 /* set type, direction, address; reset fifo counters */
188 writel ((1 << FIFO_FLUSH
), &ep
->regs
->ep_stat
);
189 tmp
= (desc
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
);
190 if (tmp
== USB_ENDPOINT_XFER_INT
) {
191 /* erratum 0105 workaround prevents hs NYET */
192 if (dev
->chiprev
== 0100
193 && dev
->gadget
.speed
== USB_SPEED_HIGH
194 && !(desc
->bEndpointAddress
& USB_DIR_IN
))
195 writel ((1 << CLEAR_NAK_OUT_PACKETS_MODE
),
197 } else if (tmp
== USB_ENDPOINT_XFER_BULK
) {
198 /* catch some particularly blatant driver bugs */
199 if ((dev
->gadget
.speed
== USB_SPEED_HIGH
201 || (dev
->gadget
.speed
== USB_SPEED_FULL
203 spin_unlock_irqrestore (&dev
->lock
, flags
);
207 ep
->is_iso
= (tmp
== USB_ENDPOINT_XFER_ISOC
) ? 1 : 0;
208 tmp
<<= ENDPOINT_TYPE
;
209 tmp
|= desc
->bEndpointAddress
;
210 tmp
|= (4 << ENDPOINT_BYTE_COUNT
); /* default full fifo lines */
211 tmp
|= 1 << ENDPOINT_ENABLE
;
214 /* for OUT transfers, block the rx fifo until a read is posted */
215 ep
->is_in
= (tmp
& USB_DIR_IN
) != 0;
217 writel ((1 << SET_NAK_OUT_PACKETS
), &ep
->regs
->ep_rsp
);
218 else if (dev
->pdev
->device
!= 0x2280) {
219 /* Added for 2282, Don't use nak packets on an in endpoint,
220 * this was ignored on 2280
222 writel ((1 << CLEAR_NAK_OUT_PACKETS
)
223 | (1 << CLEAR_NAK_OUT_PACKETS_MODE
), &ep
->regs
->ep_rsp
);
226 writel (tmp
, &ep
->regs
->ep_cfg
);
229 if (!ep
->dma
) { /* pio, per-packet */
230 tmp
= (1 << ep
->num
) | readl (&dev
->regs
->pciirqenb0
);
231 writel (tmp
, &dev
->regs
->pciirqenb0
);
233 tmp
= (1 << DATA_PACKET_RECEIVED_INTERRUPT_ENABLE
)
234 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT_ENABLE
);
235 if (dev
->pdev
->device
== 0x2280)
236 tmp
|= readl (&ep
->regs
->ep_irqenb
);
237 writel (tmp
, &ep
->regs
->ep_irqenb
);
238 } else { /* dma, per-request */
239 tmp
= (1 << (8 + ep
->num
)); /* completion */
240 tmp
|= readl (&dev
->regs
->pciirqenb1
);
241 writel (tmp
, &dev
->regs
->pciirqenb1
);
243 /* for short OUT transfers, dma completions can't
244 * advance the queue; do it pio-style, by hand.
245 * NOTE erratum 0112 workaround #2
247 if ((desc
->bEndpointAddress
& USB_DIR_IN
) == 0) {
248 tmp
= (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT_ENABLE
);
249 writel (tmp
, &ep
->regs
->ep_irqenb
);
251 tmp
= (1 << ep
->num
) | readl (&dev
->regs
->pciirqenb0
);
252 writel (tmp
, &dev
->regs
->pciirqenb0
);
256 tmp
= desc
->bEndpointAddress
;
257 DEBUG (dev
, "enabled %s (ep%d%s-%s) %s max %04x\n",
258 _ep
->name
, tmp
& 0x0f, DIR_STRING (tmp
),
259 type_string (desc
->bmAttributes
),
260 ep
->dma
? "dma" : "pio", max
);
262 /* pci writes may still be posted */
263 spin_unlock_irqrestore (&dev
->lock
, flags
);
267 static int handshake (u32 __iomem
*ptr
, u32 mask
, u32 done
, int usec
)
272 result
= readl (ptr
);
273 if (result
== ~(u32
)0) /* "device unplugged" */
284 static const struct usb_ep_ops net2280_ep_ops
;
286 static void ep_reset (struct net2280_regs __iomem
*regs
, struct net2280_ep
*ep
)
291 INIT_LIST_HEAD (&ep
->queue
);
293 ep
->ep
.maxpacket
= ~0;
294 ep
->ep
.ops
= &net2280_ep_ops
;
296 /* disable the dma, irqs, endpoint... */
298 writel (0, &ep
->dma
->dmactl
);
299 writel ( (1 << DMA_SCATTER_GATHER_DONE_INTERRUPT
)
300 | (1 << DMA_TRANSACTION_DONE_INTERRUPT
)
302 , &ep
->dma
->dmastat
);
304 tmp
= readl (®s
->pciirqenb0
);
305 tmp
&= ~(1 << ep
->num
);
306 writel (tmp
, ®s
->pciirqenb0
);
308 tmp
= readl (®s
->pciirqenb1
);
309 tmp
&= ~(1 << (8 + ep
->num
)); /* completion */
310 writel (tmp
, ®s
->pciirqenb1
);
312 writel (0, &ep
->regs
->ep_irqenb
);
314 /* init to our chosen defaults, notably so that we NAK OUT
315 * packets until the driver queues a read (+note erratum 0112)
317 if (!ep
->is_in
|| ep
->dev
->pdev
->device
== 0x2280) {
318 tmp
= (1 << SET_NAK_OUT_PACKETS_MODE
)
319 | (1 << SET_NAK_OUT_PACKETS
)
320 | (1 << CLEAR_EP_HIDE_STATUS_PHASE
)
321 | (1 << CLEAR_INTERRUPT_MODE
);
324 tmp
= (1 << CLEAR_NAK_OUT_PACKETS_MODE
)
325 | (1 << CLEAR_NAK_OUT_PACKETS
)
326 | (1 << CLEAR_EP_HIDE_STATUS_PHASE
)
327 | (1 << CLEAR_INTERRUPT_MODE
);
331 tmp
|= (1 << CLEAR_ENDPOINT_TOGGLE
)
332 | (1 << CLEAR_ENDPOINT_HALT
);
334 writel (tmp
, &ep
->regs
->ep_rsp
);
336 /* scrub most status bits, and flush any fifo state */
337 if (ep
->dev
->pdev
->device
== 0x2280)
338 tmp
= (1 << FIFO_OVERFLOW
)
339 | (1 << FIFO_UNDERFLOW
);
343 writel (tmp
| (1 << TIMEOUT
)
344 | (1 << USB_STALL_SENT
)
345 | (1 << USB_IN_NAK_SENT
)
346 | (1 << USB_IN_ACK_RCVD
)
347 | (1 << USB_OUT_PING_NAK_SENT
)
348 | (1 << USB_OUT_ACK_SENT
)
350 | (1 << SHORT_PACKET_OUT_DONE_INTERRUPT
)
351 | (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT
)
352 | (1 << DATA_PACKET_RECEIVED_INTERRUPT
)
353 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT
)
354 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT
)
355 | (1 << DATA_IN_TOKEN_INTERRUPT
)
356 , &ep
->regs
->ep_stat
);
358 /* fifo size is handled separately */
361 static void nuke (struct net2280_ep
*);
363 static int net2280_disable (struct usb_ep
*_ep
)
365 struct net2280_ep
*ep
;
368 ep
= container_of (_ep
, struct net2280_ep
, ep
);
369 if (!_ep
|| !ep
->desc
|| _ep
->name
== ep0name
)
372 spin_lock_irqsave (&ep
->dev
->lock
, flags
);
374 ep_reset (ep
->dev
->regs
, ep
);
376 VDEBUG (ep
->dev
, "disabled %s %s\n",
377 ep
->dma
? "dma" : "pio", _ep
->name
);
379 /* synch memory views with the device */
380 (void) readl (&ep
->regs
->ep_cfg
);
382 if (use_dma
&& !ep
->dma
&& ep
->num
>= 1 && ep
->num
<= 4)
383 ep
->dma
= &ep
->dev
->dma
[ep
->num
- 1];
385 spin_unlock_irqrestore (&ep
->dev
->lock
, flags
);
389 /*-------------------------------------------------------------------------*/
391 static struct usb_request
*
392 net2280_alloc_request (struct usb_ep
*_ep
, gfp_t gfp_flags
)
394 struct net2280_ep
*ep
;
395 struct net2280_request
*req
;
399 ep
= container_of (_ep
, struct net2280_ep
, ep
);
401 req
= kzalloc(sizeof(*req
), gfp_flags
);
405 req
->req
.dma
= DMA_ADDR_INVALID
;
406 INIT_LIST_HEAD (&req
->queue
);
408 /* this dma descriptor may be swapped with the previous dummy */
410 struct net2280_dma
*td
;
412 td
= pci_pool_alloc (ep
->dev
->requests
, gfp_flags
,
418 td
->dmacount
= 0; /* not VALID */
419 td
->dmaaddr
= cpu_to_le32 (DMA_ADDR_INVALID
);
420 td
->dmadesc
= td
->dmaaddr
;
427 net2280_free_request (struct usb_ep
*_ep
, struct usb_request
*_req
)
429 struct net2280_ep
*ep
;
430 struct net2280_request
*req
;
432 ep
= container_of (_ep
, struct net2280_ep
, ep
);
436 req
= container_of (_req
, struct net2280_request
, req
);
437 WARN_ON (!list_empty (&req
->queue
));
439 pci_pool_free (ep
->dev
->requests
, req
->td
, req
->td_dma
);
443 /*-------------------------------------------------------------------------*/
445 /* load a packet into the fifo we use for usb IN transfers.
446 * works for all endpoints.
448 * NOTE: pio with ep-a..ep-d could stuff multiple packets into the fifo
449 * at a time, but this code is simpler because it knows it only writes
450 * one packet. ep-a..ep-d should use dma instead.
453 write_fifo (struct net2280_ep
*ep
, struct usb_request
*req
)
455 struct net2280_ep_regs __iomem
*regs
= ep
->regs
;
458 unsigned count
, total
;
460 /* INVARIANT: fifo is currently empty. (testable) */
463 buf
= req
->buf
+ req
->actual
;
465 total
= req
->length
- req
->actual
;
471 /* write just one packet at a time */
472 count
= ep
->ep
.maxpacket
;
473 if (count
> total
) /* min() cannot be used on a bitfield */
476 VDEBUG (ep
->dev
, "write %s fifo (IN) %d bytes%s req %p\n",
478 (count
!= ep
->ep
.maxpacket
) ? " (short)" : "",
481 /* NOTE be careful if you try to align these. fifo lines
482 * should normally be full (4 bytes) and successive partial
483 * lines are ok only in certain cases.
485 tmp
= get_unaligned ((u32
*)buf
);
487 writel (tmp
, ®s
->ep_data
);
492 /* last fifo entry is "short" unless we wrote a full packet.
493 * also explicitly validate last word in (periodic) transfers
494 * when maxpacket is not a multiple of 4 bytes.
496 if (count
|| total
< ep
->ep
.maxpacket
) {
497 tmp
= count
? get_unaligned ((u32
*)buf
) : count
;
499 set_fifo_bytecount (ep
, count
& 0x03);
500 writel (tmp
, ®s
->ep_data
);
503 /* pci writes may still be posted */
506 /* work around erratum 0106: PCI and USB race over the OUT fifo.
507 * caller guarantees chiprev 0100, out endpoint is NAKing, and
508 * there's no real data in the fifo.
510 * NOTE: also used in cases where that erratum doesn't apply:
511 * where the host wrote "too much" data to us.
513 static void out_flush (struct net2280_ep
*ep
)
518 ASSERT_OUT_NAKING (ep
);
520 statp
= &ep
->regs
->ep_stat
;
521 writel ( (1 << DATA_OUT_PING_TOKEN_INTERRUPT
)
522 | (1 << DATA_PACKET_RECEIVED_INTERRUPT
)
524 writel ((1 << FIFO_FLUSH
), statp
);
527 if (tmp
& (1 << DATA_OUT_PING_TOKEN_INTERRUPT
)
528 /* high speed did bulk NYET; fifo isn't filling */
529 && ep
->dev
->gadget
.speed
== USB_SPEED_FULL
) {
532 usec
= 50; /* 64 byte bulk/interrupt */
533 handshake (statp
, (1 << USB_OUT_PING_NAK_SENT
),
534 (1 << USB_OUT_PING_NAK_SENT
), usec
);
535 /* NAK done; now CLEAR_NAK_OUT_PACKETS is safe */
539 /* unload packet(s) from the fifo we use for usb OUT transfers.
540 * returns true iff the request completed, because of short packet
541 * or the request buffer having filled with full packets.
543 * for ep-a..ep-d this will read multiple packets out when they
544 * have been accepted.
547 read_fifo (struct net2280_ep
*ep
, struct net2280_request
*req
)
549 struct net2280_ep_regs __iomem
*regs
= ep
->regs
;
550 u8
*buf
= req
->req
.buf
+ req
->req
.actual
;
551 unsigned count
, tmp
, is_short
;
552 unsigned cleanup
= 0, prevent
= 0;
554 /* erratum 0106 ... packets coming in during fifo reads might
555 * be incompletely rejected. not all cases have workarounds.
557 if (ep
->dev
->chiprev
== 0x0100
558 && ep
->dev
->gadget
.speed
== USB_SPEED_FULL
) {
560 tmp
= readl (&ep
->regs
->ep_stat
);
561 if ((tmp
& (1 << NAK_OUT_PACKETS
)))
563 else if ((tmp
& (1 << FIFO_FULL
))) {
564 start_out_naking (ep
);
567 /* else: hope we don't see the problem */
570 /* never overflow the rx buffer. the fifo reads packets until
571 * it sees a short one; we might not be ready for them all.
574 count
= readl (®s
->ep_avail
);
575 if (unlikely (count
== 0)) {
577 tmp
= readl (&ep
->regs
->ep_stat
);
578 count
= readl (®s
->ep_avail
);
579 /* handled that data already? */
580 if (count
== 0 && (tmp
& (1 << NAK_OUT_PACKETS
)) == 0)
584 tmp
= req
->req
.length
- req
->req
.actual
;
586 /* as with DMA, data overflow gets flushed */
587 if ((tmp
% ep
->ep
.maxpacket
) != 0) {
589 "%s out fifo %d bytes, expected %d\n",
590 ep
->ep
.name
, count
, tmp
);
591 req
->req
.status
= -EOVERFLOW
;
593 /* NAK_OUT_PACKETS will be set, so flushing is safe;
594 * the next read will start with the next packet
596 } /* else it's a ZLP, no worries */
599 req
->req
.actual
+= count
;
601 is_short
= (count
== 0) || ((count
% ep
->ep
.maxpacket
) != 0);
603 VDEBUG (ep
->dev
, "read %s fifo (OUT) %d bytes%s%s%s req %p %d/%d\n",
604 ep
->ep
.name
, count
, is_short
? " (short)" : "",
605 cleanup
? " flush" : "", prevent
? " nak" : "",
606 req
, req
->req
.actual
, req
->req
.length
);
609 tmp
= readl (®s
->ep_data
);
611 put_unaligned (tmp
, (u32
*)buf
);
616 tmp
= readl (®s
->ep_data
);
617 /* LE conversion is implicit here: */
626 writel ((1 << CLEAR_NAK_OUT_PACKETS
), &ep
->regs
->ep_rsp
);
627 (void) readl (&ep
->regs
->ep_rsp
);
630 return is_short
|| ((req
->req
.actual
== req
->req
.length
)
634 /* fill out dma descriptor to match a given request */
636 fill_dma_desc (struct net2280_ep
*ep
, struct net2280_request
*req
, int valid
)
638 struct net2280_dma
*td
= req
->td
;
639 u32 dmacount
= req
->req
.length
;
641 /* don't let DMA continue after a short OUT packet,
642 * so overruns can't affect the next transfer.
643 * in case of overruns on max-size packets, we can't
644 * stop the fifo from filling but we can flush it.
647 dmacount
|= (1 << DMA_DIRECTION
);
648 if ((!ep
->is_in
&& (dmacount
% ep
->ep
.maxpacket
) != 0)
649 || ep
->dev
->pdev
->device
!= 0x2280)
650 dmacount
|= (1 << END_OF_CHAIN
);
654 dmacount
|= (1 << VALID_BIT
);
655 if (likely(!req
->req
.no_interrupt
|| !use_dma_chaining
))
656 dmacount
|= (1 << DMA_DONE_INTERRUPT_ENABLE
);
658 /* td->dmadesc = previously set by caller */
659 td
->dmaaddr
= cpu_to_le32 (req
->req
.dma
);
661 /* 2280 may be polling VALID_BIT through ep->dma->dmadesc */
663 td
->dmacount
= cpu_to_le32(dmacount
);
666 static const u32 dmactl_default
=
667 (1 << DMA_SCATTER_GATHER_DONE_INTERRUPT
)
668 | (1 << DMA_CLEAR_COUNT_ENABLE
)
669 /* erratum 0116 workaround part 1 (use POLLING) */
670 | (POLL_100_USEC
<< DESCRIPTOR_POLLING_RATE
)
671 | (1 << DMA_VALID_BIT_POLLING_ENABLE
)
672 | (1 << DMA_VALID_BIT_ENABLE
)
673 | (1 << DMA_SCATTER_GATHER_ENABLE
)
674 /* erratum 0116 workaround part 2 (no AUTOSTART) */
677 static inline void spin_stop_dma (struct net2280_dma_regs __iomem
*dma
)
679 handshake (&dma
->dmactl
, (1 << DMA_ENABLE
), 0, 50);
682 static inline void stop_dma (struct net2280_dma_regs __iomem
*dma
)
684 writel (readl (&dma
->dmactl
) & ~(1 << DMA_ENABLE
), &dma
->dmactl
);
688 static void start_queue (struct net2280_ep
*ep
, u32 dmactl
, u32 td_dma
)
690 struct net2280_dma_regs __iomem
*dma
= ep
->dma
;
691 unsigned int tmp
= (1 << VALID_BIT
) | (ep
->is_in
<< DMA_DIRECTION
);
693 if (ep
->dev
->pdev
->device
!= 0x2280)
694 tmp
|= (1 << END_OF_CHAIN
);
696 writel (tmp
, &dma
->dmacount
);
697 writel (readl (&dma
->dmastat
), &dma
->dmastat
);
699 writel (td_dma
, &dma
->dmadesc
);
700 writel (dmactl
, &dma
->dmactl
);
702 /* erratum 0116 workaround part 3: pci arbiter away from net2280 */
703 (void) readl (&ep
->dev
->pci
->pcimstctl
);
705 writel ((1 << DMA_START
), &dma
->dmastat
);
708 stop_out_naking (ep
);
711 static void start_dma (struct net2280_ep
*ep
, struct net2280_request
*req
)
714 struct net2280_dma_regs __iomem
*dma
= ep
->dma
;
716 /* FIXME can't use DMA for ZLPs */
718 /* on this path we "know" there's no dma active (yet) */
719 WARN_ON (readl (&dma
->dmactl
) & (1 << DMA_ENABLE
));
720 writel (0, &ep
->dma
->dmactl
);
722 /* previous OUT packet might have been short */
723 if (!ep
->is_in
&& ((tmp
= readl (&ep
->regs
->ep_stat
))
724 & (1 << NAK_OUT_PACKETS
)) != 0) {
725 writel ((1 << SHORT_PACKET_TRANSFERRED_INTERRUPT
),
728 tmp
= readl (&ep
->regs
->ep_avail
);
730 writel (readl (&dma
->dmastat
), &dma
->dmastat
);
732 /* transfer all/some fifo data */
733 writel (req
->req
.dma
, &dma
->dmaaddr
);
734 tmp
= min (tmp
, req
->req
.length
);
736 /* dma irq, faking scatterlist status */
737 req
->td
->dmacount
= cpu_to_le32 (req
->req
.length
- tmp
);
738 writel ((1 << DMA_DONE_INTERRUPT_ENABLE
)
739 | tmp
, &dma
->dmacount
);
740 req
->td
->dmadesc
= 0;
743 writel ((1 << DMA_ENABLE
), &dma
->dmactl
);
744 writel ((1 << DMA_START
), &dma
->dmastat
);
749 tmp
= dmactl_default
;
751 /* force packet boundaries between dma requests, but prevent the
752 * controller from automagically writing a last "short" packet
753 * (zero length) unless the driver explicitly said to do that.
756 if (likely ((req
->req
.length
% ep
->ep
.maxpacket
) != 0
758 tmp
|= (1 << DMA_FIFO_VALIDATE
);
759 ep
->in_fifo_validate
= 1;
761 ep
->in_fifo_validate
= 0;
764 /* init req->td, pointing to the current dummy */
765 req
->td
->dmadesc
= cpu_to_le32 (ep
->td_dma
);
766 fill_dma_desc (ep
, req
, 1);
768 if (!use_dma_chaining
)
769 req
->td
->dmacount
|= cpu_to_le32 (1 << END_OF_CHAIN
);
771 start_queue (ep
, tmp
, req
->td_dma
);
775 queue_dma (struct net2280_ep
*ep
, struct net2280_request
*req
, int valid
)
777 struct net2280_dma
*end
;
780 /* swap new dummy for old, link; fill and maybe activate */
786 ep
->td_dma
= req
->td_dma
;
789 end
->dmadesc
= cpu_to_le32 (ep
->td_dma
);
791 fill_dma_desc (ep
, req
, valid
);
795 done (struct net2280_ep
*ep
, struct net2280_request
*req
, int status
)
798 unsigned stopped
= ep
->stopped
;
800 list_del_init (&req
->queue
);
802 if (req
->req
.status
== -EINPROGRESS
)
803 req
->req
.status
= status
;
805 status
= req
->req
.status
;
809 usb_gadget_unmap_request(&dev
->gadget
, &req
->req
, ep
->is_in
);
811 if (status
&& status
!= -ESHUTDOWN
)
812 VDEBUG (dev
, "complete %s req %p stat %d len %u/%u\n",
813 ep
->ep
.name
, &req
->req
, status
,
814 req
->req
.actual
, req
->req
.length
);
816 /* don't modify queue heads during completion callback */
818 spin_unlock (&dev
->lock
);
819 req
->req
.complete (&ep
->ep
, &req
->req
);
820 spin_lock (&dev
->lock
);
821 ep
->stopped
= stopped
;
824 /*-------------------------------------------------------------------------*/
827 net2280_queue (struct usb_ep
*_ep
, struct usb_request
*_req
, gfp_t gfp_flags
)
829 struct net2280_request
*req
;
830 struct net2280_ep
*ep
;
834 /* we always require a cpu-view buffer, so that we can
835 * always use pio (as fallback or whatever).
837 req
= container_of (_req
, struct net2280_request
, req
);
838 if (!_req
|| !_req
->complete
|| !_req
->buf
839 || !list_empty (&req
->queue
))
841 if (_req
->length
> (~0 & DMA_BYTE_COUNT_MASK
))
843 ep
= container_of (_ep
, struct net2280_ep
, ep
);
844 if (!_ep
|| (!ep
->desc
&& ep
->num
!= 0))
847 if (!dev
->driver
|| dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
850 /* FIXME implement PIO fallback for ZLPs with DMA */
851 if (ep
->dma
&& _req
->length
== 0)
854 /* set up dma mapping in case the caller didn't */
858 ret
= usb_gadget_map_request(&dev
->gadget
, _req
,
865 VDEBUG (dev
, "%s queue req %p, len %d buf %p\n",
866 _ep
->name
, _req
, _req
->length
, _req
->buf
);
869 spin_lock_irqsave (&dev
->lock
, flags
);
871 _req
->status
= -EINPROGRESS
;
874 /* kickstart this i/o queue? */
875 if (list_empty (&ep
->queue
) && !ep
->stopped
) {
876 /* use DMA if the endpoint supports it, else pio */
880 /* maybe there's no control data, just status ack */
881 if (ep
->num
== 0 && _req
->length
== 0) {
884 VDEBUG (dev
, "%s status ack\n", ep
->ep
.name
);
888 /* PIO ... stuff the fifo, or unblock it. */
890 write_fifo (ep
, _req
);
891 else if (list_empty (&ep
->queue
)) {
894 /* OUT FIFO might have packet(s) buffered */
895 s
= readl (&ep
->regs
->ep_stat
);
896 if ((s
& (1 << FIFO_EMPTY
)) == 0) {
897 /* note: _req->short_not_ok is
898 * ignored here since PIO _always_
899 * stops queue advance here, and
900 * _req->status doesn't change for
901 * short reads (only _req->actual)
903 if (read_fifo (ep
, req
)) {
910 s
= readl (&ep
->regs
->ep_stat
);
913 /* don't NAK, let the fifo fill */
914 if (req
&& (s
& (1 << NAK_OUT_PACKETS
)))
915 writel ((1 << CLEAR_NAK_OUT_PACKETS
),
920 } else if (ep
->dma
) {
926 /* preventing magic zlps is per-engine state, not
927 * per-transfer; irq logic must recover hiccups.
929 expect
= likely (req
->req
.zero
930 || (req
->req
.length
% ep
->ep
.maxpacket
) != 0);
931 if (expect
!= ep
->in_fifo_validate
)
934 queue_dma (ep
, req
, valid
);
936 } /* else the irq handler advances the queue. */
940 list_add_tail (&req
->queue
, &ep
->queue
);
942 spin_unlock_irqrestore (&dev
->lock
, flags
);
944 /* pci writes may still be posted */
950 struct net2280_ep
*ep
,
951 struct net2280_request
*req
,
956 req
->req
.actual
= req
->req
.length
- (DMA_BYTE_COUNT_MASK
& dmacount
);
957 done (ep
, req
, status
);
960 static void restart_dma (struct net2280_ep
*ep
);
962 static void scan_dma_completions (struct net2280_ep
*ep
)
964 /* only look at descriptors that were "naturally" retired,
965 * so fifo and list head state won't matter
967 while (!list_empty (&ep
->queue
)) {
968 struct net2280_request
*req
;
971 req
= list_entry (ep
->queue
.next
,
972 struct net2280_request
, queue
);
976 tmp
= le32_to_cpup (&req
->td
->dmacount
);
977 if ((tmp
& (1 << VALID_BIT
)) != 0)
980 /* SHORT_PACKET_TRANSFERRED_INTERRUPT handles "usb-short"
981 * cases where DMA must be aborted; this code handles
982 * all non-abort DMA completions.
984 if (unlikely (req
->td
->dmadesc
== 0)) {
986 tmp
= readl (&ep
->dma
->dmacount
);
987 if (tmp
& DMA_BYTE_COUNT_MASK
)
989 /* single transfer mode */
990 dma_done (ep
, req
, tmp
, 0);
992 } else if (!ep
->is_in
993 && (req
->req
.length
% ep
->ep
.maxpacket
) != 0) {
994 tmp
= readl (&ep
->regs
->ep_stat
);
996 /* AVOID TROUBLE HERE by not issuing short reads from
997 * your gadget driver. That helps avoids errata 0121,
998 * 0122, and 0124; not all cases trigger the warning.
1000 if ((tmp
& (1 << NAK_OUT_PACKETS
)) == 0) {
1001 WARNING (ep
->dev
, "%s lost packet sync!\n",
1003 req
->req
.status
= -EOVERFLOW
;
1004 } else if ((tmp
= readl (&ep
->regs
->ep_avail
)) != 0) {
1005 /* fifo gets flushed later */
1006 ep
->out_overflow
= 1;
1007 DEBUG (ep
->dev
, "%s dma, discard %d len %d\n",
1010 req
->req
.status
= -EOVERFLOW
;
1013 dma_done (ep
, req
, tmp
, 0);
1017 static void restart_dma (struct net2280_ep
*ep
)
1019 struct net2280_request
*req
;
1020 u32 dmactl
= dmactl_default
;
1024 req
= list_entry (ep
->queue
.next
, struct net2280_request
, queue
);
1026 if (!use_dma_chaining
) {
1027 start_dma (ep
, req
);
1031 /* the 2280 will be processing the queue unless queue hiccups after
1032 * the previous transfer:
1033 * IN: wanted automagic zlp, head doesn't (or vice versa)
1034 * DMA_FIFO_VALIDATE doesn't init from dma descriptors.
1035 * OUT: was "usb-short", we must restart.
1037 if (ep
->is_in
&& !req
->valid
) {
1038 struct net2280_request
*entry
, *prev
= NULL
;
1039 int reqmode
, done
= 0;
1041 DEBUG (ep
->dev
, "%s dma hiccup td %p\n", ep
->ep
.name
, req
->td
);
1042 ep
->in_fifo_validate
= likely (req
->req
.zero
1043 || (req
->req
.length
% ep
->ep
.maxpacket
) != 0);
1044 if (ep
->in_fifo_validate
)
1045 dmactl
|= (1 << DMA_FIFO_VALIDATE
);
1046 list_for_each_entry (entry
, &ep
->queue
, queue
) {
1051 dmacount
= entry
->td
->dmacount
;
1053 reqmode
= likely (entry
->req
.zero
1054 || (entry
->req
.length
1055 % ep
->ep
.maxpacket
) != 0);
1056 if (reqmode
== ep
->in_fifo_validate
) {
1058 dmacount
|= valid_bit
;
1059 entry
->td
->dmacount
= dmacount
;
1063 /* force a hiccup */
1064 prev
->td
->dmacount
|= dma_done_ie
;
1069 /* walk the rest of the queue so unlinks behave */
1071 dmacount
&= ~valid_bit
;
1072 entry
->td
->dmacount
= dmacount
;
1077 writel (0, &ep
->dma
->dmactl
);
1078 start_queue (ep
, dmactl
, req
->td_dma
);
1081 static void abort_dma (struct net2280_ep
*ep
)
1083 /* abort the current transfer */
1084 if (likely (!list_empty (&ep
->queue
))) {
1085 /* FIXME work around errata 0121, 0122, 0124 */
1086 writel ((1 << DMA_ABORT
), &ep
->dma
->dmastat
);
1087 spin_stop_dma (ep
->dma
);
1090 scan_dma_completions (ep
);
1093 /* dequeue ALL requests */
1094 static void nuke (struct net2280_ep
*ep
)
1096 struct net2280_request
*req
;
1098 /* called with spinlock held */
1102 while (!list_empty (&ep
->queue
)) {
1103 req
= list_entry (ep
->queue
.next
,
1104 struct net2280_request
,
1106 done (ep
, req
, -ESHUTDOWN
);
1110 /* dequeue JUST ONE request */
1111 static int net2280_dequeue (struct usb_ep
*_ep
, struct usb_request
*_req
)
1113 struct net2280_ep
*ep
;
1114 struct net2280_request
*req
;
1115 unsigned long flags
;
1119 ep
= container_of (_ep
, struct net2280_ep
, ep
);
1120 if (!_ep
|| (!ep
->desc
&& ep
->num
!= 0) || !_req
)
1123 spin_lock_irqsave (&ep
->dev
->lock
, flags
);
1124 stopped
= ep
->stopped
;
1126 /* quiesce dma while we patch the queue */
1130 dmactl
= readl (&ep
->dma
->dmactl
);
1131 /* WARNING erratum 0127 may kick in ... */
1133 scan_dma_completions (ep
);
1136 /* make sure it's still queued on this endpoint */
1137 list_for_each_entry (req
, &ep
->queue
, queue
) {
1138 if (&req
->req
== _req
)
1141 if (&req
->req
!= _req
) {
1142 spin_unlock_irqrestore (&ep
->dev
->lock
, flags
);
1146 /* queue head may be partially complete. */
1147 if (ep
->queue
.next
== &req
->queue
) {
1149 DEBUG (ep
->dev
, "unlink (%s) dma\n", _ep
->name
);
1150 _req
->status
= -ECONNRESET
;
1152 if (likely (ep
->queue
.next
== &req
->queue
)) {
1153 // NOTE: misreports single-transfer mode
1154 req
->td
->dmacount
= 0; /* invalidate */
1156 readl (&ep
->dma
->dmacount
),
1160 DEBUG (ep
->dev
, "unlink (%s) pio\n", _ep
->name
);
1161 done (ep
, req
, -ECONNRESET
);
1165 /* patch up hardware chaining data */
1166 } else if (ep
->dma
&& use_dma_chaining
) {
1167 if (req
->queue
.prev
== ep
->queue
.next
) {
1168 writel (le32_to_cpu (req
->td
->dmadesc
),
1170 if (req
->td
->dmacount
& dma_done_ie
)
1171 writel (readl (&ep
->dma
->dmacount
)
1172 | le32_to_cpu(dma_done_ie
),
1173 &ep
->dma
->dmacount
);
1175 struct net2280_request
*prev
;
1177 prev
= list_entry (req
->queue
.prev
,
1178 struct net2280_request
, queue
);
1179 prev
->td
->dmadesc
= req
->td
->dmadesc
;
1180 if (req
->td
->dmacount
& dma_done_ie
)
1181 prev
->td
->dmacount
|= dma_done_ie
;
1186 done (ep
, req
, -ECONNRESET
);
1187 ep
->stopped
= stopped
;
1190 /* turn off dma on inactive queues */
1191 if (list_empty (&ep
->queue
))
1193 else if (!ep
->stopped
) {
1194 /* resume current request, or start new one */
1196 writel (dmactl
, &ep
->dma
->dmactl
);
1198 start_dma (ep
, list_entry (ep
->queue
.next
,
1199 struct net2280_request
, queue
));
1203 spin_unlock_irqrestore (&ep
->dev
->lock
, flags
);
1207 /*-------------------------------------------------------------------------*/
1209 static int net2280_fifo_status (struct usb_ep
*_ep
);
1212 net2280_set_halt_and_wedge(struct usb_ep
*_ep
, int value
, int wedged
)
1214 struct net2280_ep
*ep
;
1215 unsigned long flags
;
1218 ep
= container_of (_ep
, struct net2280_ep
, ep
);
1219 if (!_ep
|| (!ep
->desc
&& ep
->num
!= 0))
1221 if (!ep
->dev
->driver
|| ep
->dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
1223 if (ep
->desc
/* not ep0 */ && (ep
->desc
->bmAttributes
& 0x03)
1224 == USB_ENDPOINT_XFER_ISOC
)
1227 spin_lock_irqsave (&ep
->dev
->lock
, flags
);
1228 if (!list_empty (&ep
->queue
))
1230 else if (ep
->is_in
&& value
&& net2280_fifo_status (_ep
) != 0)
1233 VDEBUG (ep
->dev
, "%s %s %s\n", _ep
->name
,
1234 value
? "set" : "clear",
1235 wedged
? "wedge" : "halt");
1236 /* set/clear, then synch memory views with the device */
1239 ep
->dev
->protocol_stall
= 1;
1248 (void) readl (&ep
->regs
->ep_rsp
);
1250 spin_unlock_irqrestore (&ep
->dev
->lock
, flags
);
1256 net2280_set_halt(struct usb_ep
*_ep
, int value
)
1258 return net2280_set_halt_and_wedge(_ep
, value
, 0);
1262 net2280_set_wedge(struct usb_ep
*_ep
)
1264 if (!_ep
|| _ep
->name
== ep0name
)
1266 return net2280_set_halt_and_wedge(_ep
, 1, 1);
1270 net2280_fifo_status (struct usb_ep
*_ep
)
1272 struct net2280_ep
*ep
;
1275 ep
= container_of (_ep
, struct net2280_ep
, ep
);
1276 if (!_ep
|| (!ep
->desc
&& ep
->num
!= 0))
1278 if (!ep
->dev
->driver
|| ep
->dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
1281 avail
= readl (&ep
->regs
->ep_avail
) & ((1 << 12) - 1);
1282 if (avail
> ep
->fifo_size
)
1285 avail
= ep
->fifo_size
- avail
;
1290 net2280_fifo_flush (struct usb_ep
*_ep
)
1292 struct net2280_ep
*ep
;
1294 ep
= container_of (_ep
, struct net2280_ep
, ep
);
1295 if (!_ep
|| (!ep
->desc
&& ep
->num
!= 0))
1297 if (!ep
->dev
->driver
|| ep
->dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
1300 writel ((1 << FIFO_FLUSH
), &ep
->regs
->ep_stat
);
1301 (void) readl (&ep
->regs
->ep_rsp
);
1304 static const struct usb_ep_ops net2280_ep_ops
= {
1305 .enable
= net2280_enable
,
1306 .disable
= net2280_disable
,
1308 .alloc_request
= net2280_alloc_request
,
1309 .free_request
= net2280_free_request
,
1311 .queue
= net2280_queue
,
1312 .dequeue
= net2280_dequeue
,
1314 .set_halt
= net2280_set_halt
,
1315 .set_wedge
= net2280_set_wedge
,
1316 .fifo_status
= net2280_fifo_status
,
1317 .fifo_flush
= net2280_fifo_flush
,
1320 /*-------------------------------------------------------------------------*/
1322 static int net2280_get_frame (struct usb_gadget
*_gadget
)
1324 struct net2280
*dev
;
1325 unsigned long flags
;
1330 dev
= container_of (_gadget
, struct net2280
, gadget
);
1331 spin_lock_irqsave (&dev
->lock
, flags
);
1332 retval
= get_idx_reg (dev
->regs
, REG_FRAME
) & 0x03ff;
1333 spin_unlock_irqrestore (&dev
->lock
, flags
);
1337 static int net2280_wakeup (struct usb_gadget
*_gadget
)
1339 struct net2280
*dev
;
1341 unsigned long flags
;
1345 dev
= container_of (_gadget
, struct net2280
, gadget
);
1347 spin_lock_irqsave (&dev
->lock
, flags
);
1348 tmp
= readl (&dev
->usb
->usbctl
);
1349 if (tmp
& (1 << DEVICE_REMOTE_WAKEUP_ENABLE
))
1350 writel (1 << GENERATE_RESUME
, &dev
->usb
->usbstat
);
1351 spin_unlock_irqrestore (&dev
->lock
, flags
);
1353 /* pci writes may still be posted */
1357 static int net2280_set_selfpowered (struct usb_gadget
*_gadget
, int value
)
1359 struct net2280
*dev
;
1361 unsigned long flags
;
1365 dev
= container_of (_gadget
, struct net2280
, gadget
);
1367 spin_lock_irqsave (&dev
->lock
, flags
);
1368 tmp
= readl (&dev
->usb
->usbctl
);
1370 tmp
|= (1 << SELF_POWERED_STATUS
);
1372 tmp
&= ~(1 << SELF_POWERED_STATUS
);
1373 writel (tmp
, &dev
->usb
->usbctl
);
1374 spin_unlock_irqrestore (&dev
->lock
, flags
);
1379 static int net2280_pullup(struct usb_gadget
*_gadget
, int is_on
)
1381 struct net2280
*dev
;
1383 unsigned long flags
;
1387 dev
= container_of (_gadget
, struct net2280
, gadget
);
1389 spin_lock_irqsave (&dev
->lock
, flags
);
1390 tmp
= readl (&dev
->usb
->usbctl
);
1391 dev
->softconnect
= (is_on
!= 0);
1393 tmp
|= (1 << USB_DETECT_ENABLE
);
1395 tmp
&= ~(1 << USB_DETECT_ENABLE
);
1396 writel (tmp
, &dev
->usb
->usbctl
);
1397 spin_unlock_irqrestore (&dev
->lock
, flags
);
1402 static int net2280_start(struct usb_gadget
*_gadget
,
1403 struct usb_gadget_driver
*driver
);
1404 static int net2280_stop(struct usb_gadget
*_gadget
,
1405 struct usb_gadget_driver
*driver
);
1407 static const struct usb_gadget_ops net2280_ops
= {
1408 .get_frame
= net2280_get_frame
,
1409 .wakeup
= net2280_wakeup
,
1410 .set_selfpowered
= net2280_set_selfpowered
,
1411 .pullup
= net2280_pullup
,
1412 .udc_start
= net2280_start
,
1413 .udc_stop
= net2280_stop
,
1416 /*-------------------------------------------------------------------------*/
1418 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1420 /* FIXME move these into procfs, and use seq_file.
1421 * Sysfs _still_ doesn't behave for arbitrarily sized files,
1422 * and also doesn't help products using this with 2.4 kernels.
1425 /* "function" sysfs attribute */
1427 show_function (struct device
*_dev
, struct device_attribute
*attr
, char *buf
)
1429 struct net2280
*dev
= dev_get_drvdata (_dev
);
1432 || !dev
->driver
->function
1433 || strlen (dev
->driver
->function
) > PAGE_SIZE
)
1435 return scnprintf (buf
, PAGE_SIZE
, "%s\n", dev
->driver
->function
);
1437 static DEVICE_ATTR (function
, S_IRUGO
, show_function
, NULL
);
1439 static ssize_t
net2280_show_registers(struct device
*_dev
,
1440 struct device_attribute
*attr
, char *buf
)
1442 struct net2280
*dev
;
1445 unsigned long flags
;
1450 dev
= dev_get_drvdata (_dev
);
1453 spin_lock_irqsave (&dev
->lock
, flags
);
1456 s
= dev
->driver
->driver
.name
;
1460 /* Main Control Registers */
1461 t
= scnprintf (next
, size
, "%s version " DRIVER_VERSION
1462 ", chiprev %04x, dma %s\n\n"
1463 "devinit %03x fifoctl %08x gadget '%s'\n"
1464 "pci irqenb0 %02x irqenb1 %08x "
1465 "irqstat0 %04x irqstat1 %08x\n",
1466 driver_name
, dev
->chiprev
,
1468 ? (use_dma_chaining
? "chaining" : "enabled")
1470 readl (&dev
->regs
->devinit
),
1471 readl (&dev
->regs
->fifoctl
),
1473 readl (&dev
->regs
->pciirqenb0
),
1474 readl (&dev
->regs
->pciirqenb1
),
1475 readl (&dev
->regs
->irqstat0
),
1476 readl (&dev
->regs
->irqstat1
));
1480 /* USB Control Registers */
1481 t1
= readl (&dev
->usb
->usbctl
);
1482 t2
= readl (&dev
->usb
->usbstat
);
1483 if (t1
& (1 << VBUS_PIN
)) {
1484 if (t2
& (1 << HIGH_SPEED
))
1486 else if (dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
1490 /* full speed bit (6) not working?? */
1493 t
= scnprintf (next
, size
,
1494 "stdrsp %08x usbctl %08x usbstat %08x "
1495 "addr 0x%02x (%s)\n",
1496 readl (&dev
->usb
->stdrsp
), t1
, t2
,
1497 readl (&dev
->usb
->ouraddr
), s
);
1501 /* PCI Master Control Registers */
1503 /* DMA Control Registers */
1505 /* Configurable EP Control Registers */
1506 for (i
= 0; i
< 7; i
++) {
1507 struct net2280_ep
*ep
;
1513 t1
= readl (&ep
->regs
->ep_cfg
);
1514 t2
= readl (&ep
->regs
->ep_rsp
) & 0xff;
1515 t
= scnprintf (next
, size
,
1516 "\n%s\tcfg %05x rsp (%02x) %s%s%s%s%s%s%s%s"
1518 ep
->ep
.name
, t1
, t2
,
1519 (t2
& (1 << CLEAR_NAK_OUT_PACKETS
))
1521 (t2
& (1 << CLEAR_EP_HIDE_STATUS_PHASE
))
1523 (t2
& (1 << CLEAR_EP_FORCE_CRC_ERROR
))
1525 (t2
& (1 << CLEAR_INTERRUPT_MODE
))
1526 ? "interrupt " : "",
1527 (t2
& (1<<CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE
))
1529 (t2
& (1 << CLEAR_NAK_OUT_PACKETS_MODE
))
1531 (t2
& (1 << CLEAR_ENDPOINT_TOGGLE
))
1532 ? "DATA1 " : "DATA0 ",
1533 (t2
& (1 << CLEAR_ENDPOINT_HALT
))
1535 readl (&ep
->regs
->ep_irqenb
));
1539 t
= scnprintf (next
, size
,
1540 "\tstat %08x avail %04x "
1542 readl (&ep
->regs
->ep_stat
),
1543 readl (&ep
->regs
->ep_avail
),
1544 t1
& 0x0f, DIR_STRING (t1
),
1545 type_string (t1
>> 8),
1546 ep
->stopped
? "*" : "");
1553 t
= scnprintf (next
, size
,
1554 " dma\tctl %08x stat %08x count %08x\n"
1555 "\taddr %08x desc %08x\n",
1556 readl (&ep
->dma
->dmactl
),
1557 readl (&ep
->dma
->dmastat
),
1558 readl (&ep
->dma
->dmacount
),
1559 readl (&ep
->dma
->dmaaddr
),
1560 readl (&ep
->dma
->dmadesc
));
1566 /* Indexed Registers */
1570 t
= scnprintf (next
, size
, "\nirqs: ");
1573 for (i
= 0; i
< 7; i
++) {
1574 struct net2280_ep
*ep
;
1579 t
= scnprintf (next
, size
, " %s/%lu", ep
->ep
.name
, ep
->irqs
);
1584 t
= scnprintf (next
, size
, "\n");
1588 spin_unlock_irqrestore (&dev
->lock
, flags
);
1590 return PAGE_SIZE
- size
;
1592 static DEVICE_ATTR(registers
, S_IRUGO
, net2280_show_registers
, NULL
);
1595 show_queues (struct device
*_dev
, struct device_attribute
*attr
, char *buf
)
1597 struct net2280
*dev
;
1600 unsigned long flags
;
1603 dev
= dev_get_drvdata (_dev
);
1606 spin_lock_irqsave (&dev
->lock
, flags
);
1608 for (i
= 0; i
< 7; i
++) {
1609 struct net2280_ep
*ep
= &dev
->ep
[i
];
1610 struct net2280_request
*req
;
1614 const struct usb_endpoint_descriptor
*d
;
1619 t
= d
->bEndpointAddress
;
1620 t
= scnprintf (next
, size
,
1621 "\n%s (ep%d%s-%s) max %04x %s fifo %d\n",
1622 ep
->ep
.name
, t
& USB_ENDPOINT_NUMBER_MASK
,
1623 (t
& USB_DIR_IN
) ? "in" : "out",
1625 switch (d
->bmAttributes
& 0x03) {
1626 case USB_ENDPOINT_XFER_BULK
:
1627 val
= "bulk"; break;
1628 case USB_ENDPOINT_XFER_INT
:
1629 val
= "intr"; break;
1633 usb_endpoint_maxp (d
) & 0x1fff,
1634 ep
->dma
? "dma" : "pio", ep
->fifo_size
1636 } else /* ep0 should only have one transfer queued */
1637 t
= scnprintf (next
, size
, "ep0 max 64 pio %s\n",
1638 ep
->is_in
? "in" : "out");
1639 if (t
<= 0 || t
> size
)
1644 if (list_empty (&ep
->queue
)) {
1645 t
= scnprintf (next
, size
, "\t(nothing queued)\n");
1646 if (t
<= 0 || t
> size
)
1652 list_for_each_entry (req
, &ep
->queue
, queue
) {
1653 if (ep
->dma
&& req
->td_dma
== readl (&ep
->dma
->dmadesc
))
1654 t
= scnprintf (next
, size
,
1655 "\treq %p len %d/%d "
1656 "buf %p (dmacount %08x)\n",
1657 &req
->req
, req
->req
.actual
,
1658 req
->req
.length
, req
->req
.buf
,
1659 readl (&ep
->dma
->dmacount
));
1661 t
= scnprintf (next
, size
,
1662 "\treq %p len %d/%d buf %p\n",
1663 &req
->req
, req
->req
.actual
,
1664 req
->req
.length
, req
->req
.buf
);
1665 if (t
<= 0 || t
> size
)
1671 struct net2280_dma
*td
;
1674 t
= scnprintf (next
, size
, "\t td %08x "
1675 " count %08x buf %08x desc %08x\n",
1677 le32_to_cpu (td
->dmacount
),
1678 le32_to_cpu (td
->dmaaddr
),
1679 le32_to_cpu (td
->dmadesc
));
1680 if (t
<= 0 || t
> size
)
1689 spin_unlock_irqrestore (&dev
->lock
, flags
);
1690 return PAGE_SIZE
- size
;
1692 static DEVICE_ATTR (queues
, S_IRUGO
, show_queues
, NULL
);
1697 #define device_create_file(a,b) (0)
1698 #define device_remove_file(a,b) do { } while (0)
1702 /*-------------------------------------------------------------------------*/
1704 /* another driver-specific mode might be a request type doing dma
1705 * to/from another device fifo instead of to/from memory.
1708 static void set_fifo_mode (struct net2280
*dev
, int mode
)
1710 /* keeping high bits preserves BAR2 */
1711 writel ((0xffff << PCI_BASE2_RANGE
) | mode
, &dev
->regs
->fifoctl
);
1713 /* always ep-{a,b,e,f} ... maybe not ep-c or ep-d */
1714 INIT_LIST_HEAD (&dev
->gadget
.ep_list
);
1715 list_add_tail (&dev
->ep
[1].ep
.ep_list
, &dev
->gadget
.ep_list
);
1716 list_add_tail (&dev
->ep
[2].ep
.ep_list
, &dev
->gadget
.ep_list
);
1719 list_add_tail (&dev
->ep
[3].ep
.ep_list
, &dev
->gadget
.ep_list
);
1720 list_add_tail (&dev
->ep
[4].ep
.ep_list
, &dev
->gadget
.ep_list
);
1721 dev
->ep
[1].fifo_size
= dev
->ep
[2].fifo_size
= 1024;
1724 dev
->ep
[1].fifo_size
= dev
->ep
[2].fifo_size
= 2048;
1727 list_add_tail (&dev
->ep
[3].ep
.ep_list
, &dev
->gadget
.ep_list
);
1728 dev
->ep
[1].fifo_size
= 2048;
1729 dev
->ep
[2].fifo_size
= 1024;
1732 /* fifo sizes for ep0, ep-c, ep-d, ep-e, and ep-f never change */
1733 list_add_tail (&dev
->ep
[5].ep
.ep_list
, &dev
->gadget
.ep_list
);
1734 list_add_tail (&dev
->ep
[6].ep
.ep_list
, &dev
->gadget
.ep_list
);
1737 /* keeping it simple:
1738 * - one bus driver, initted first;
1739 * - one function driver, initted second
1741 * most of the work to support multiple net2280 controllers would
1742 * be to associate this gadget driver (yes?) with all of them, or
1743 * perhaps to bind specific drivers to specific devices.
1746 static void usb_reset (struct net2280
*dev
)
1750 dev
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1751 (void) readl (&dev
->usb
->usbctl
);
1753 net2280_led_init (dev
);
1755 /* disable automatic responses, and irqs */
1756 writel (0, &dev
->usb
->stdrsp
);
1757 writel (0, &dev
->regs
->pciirqenb0
);
1758 writel (0, &dev
->regs
->pciirqenb1
);
1760 /* clear old dma and irq state */
1761 for (tmp
= 0; tmp
< 4; tmp
++) {
1762 struct net2280_ep
*ep
= &dev
->ep
[tmp
+ 1];
1767 writel (~0, &dev
->regs
->irqstat0
),
1768 writel (~(1 << SUSPEND_REQUEST_INTERRUPT
), &dev
->regs
->irqstat1
),
1770 /* reset, and enable pci */
1771 tmp
= readl (&dev
->regs
->devinit
)
1773 | (1 << FIFO_SOFT_RESET
)
1774 | (1 << USB_SOFT_RESET
)
1775 | (1 << M8051_RESET
);
1776 writel (tmp
, &dev
->regs
->devinit
);
1778 /* standard fifo and endpoint allocations */
1779 set_fifo_mode (dev
, (fifo_mode
<= 2) ? fifo_mode
: 0);
1782 static void usb_reinit (struct net2280
*dev
)
1787 /* use_dma changes are ignored till next device re-init */
1790 /* basic endpoint init */
1791 for (tmp
= 0; tmp
< 7; tmp
++) {
1792 struct net2280_ep
*ep
= &dev
->ep
[tmp
];
1794 ep
->ep
.name
= ep_name
[tmp
];
1798 if (tmp
> 0 && tmp
<= 4) {
1799 ep
->fifo_size
= 1024;
1801 ep
->dma
= &dev
->dma
[tmp
- 1];
1804 ep
->regs
= &dev
->epregs
[tmp
];
1805 ep_reset (dev
->regs
, ep
);
1807 dev
->ep
[0].ep
.maxpacket
= 64;
1808 dev
->ep
[5].ep
.maxpacket
= 64;
1809 dev
->ep
[6].ep
.maxpacket
= 64;
1811 dev
->gadget
.ep0
= &dev
->ep
[0].ep
;
1812 dev
->ep
[0].stopped
= 0;
1813 INIT_LIST_HEAD (&dev
->gadget
.ep0
->ep_list
);
1815 /* we want to prevent lowlevel/insecure access from the USB host,
1816 * but erratum 0119 means this enable bit is ignored
1818 for (tmp
= 0; tmp
< 5; tmp
++)
1819 writel (EP_DONTUSE
, &dev
->dep
[tmp
].dep_cfg
);
1822 static void ep0_start (struct net2280
*dev
)
1824 writel ( (1 << CLEAR_EP_HIDE_STATUS_PHASE
)
1825 | (1 << CLEAR_NAK_OUT_PACKETS
)
1826 | (1 << CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE
)
1827 , &dev
->epregs
[0].ep_rsp
);
1830 * hardware optionally handles a bunch of standard requests
1831 * that the API hides from drivers anyway. have it do so.
1832 * endpoint status/features are handled in software, to
1833 * help pass tests for some dubious behavior.
1835 writel ( (1 << SET_TEST_MODE
)
1836 | (1 << SET_ADDRESS
)
1837 | (1 << DEVICE_SET_CLEAR_DEVICE_REMOTE_WAKEUP
)
1838 | (1 << GET_DEVICE_STATUS
)
1839 | (1 << GET_INTERFACE_STATUS
)
1840 , &dev
->usb
->stdrsp
);
1841 writel ( (1 << USB_ROOT_PORT_WAKEUP_ENABLE
)
1842 | (1 << SELF_POWERED_USB_DEVICE
)
1843 | (1 << REMOTE_WAKEUP_SUPPORT
)
1844 | (dev
->softconnect
<< USB_DETECT_ENABLE
)
1845 | (1 << SELF_POWERED_STATUS
)
1846 , &dev
->usb
->usbctl
);
1848 /* enable irqs so we can see ep0 and general operation */
1849 writel ( (1 << SETUP_PACKET_INTERRUPT_ENABLE
)
1850 | (1 << ENDPOINT_0_INTERRUPT_ENABLE
)
1851 , &dev
->regs
->pciirqenb0
);
1852 writel ( (1 << PCI_INTERRUPT_ENABLE
)
1853 | (1 << PCI_MASTER_ABORT_RECEIVED_INTERRUPT_ENABLE
)
1854 | (1 << PCI_TARGET_ABORT_RECEIVED_INTERRUPT_ENABLE
)
1855 | (1 << PCI_RETRY_ABORT_INTERRUPT_ENABLE
)
1856 | (1 << VBUS_INTERRUPT_ENABLE
)
1857 | (1 << ROOT_PORT_RESET_INTERRUPT_ENABLE
)
1858 | (1 << SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE
)
1859 , &dev
->regs
->pciirqenb1
);
1861 /* don't leave any writes posted */
1862 (void) readl (&dev
->usb
->usbctl
);
1865 /* when a driver is successfully registered, it will receive
1866 * control requests including set_configuration(), which enables
1867 * non-control requests. then usb traffic follows until a
1868 * disconnect is reported. then a host may connect again, or
1869 * the driver might get unbound.
1871 static int net2280_start(struct usb_gadget
*_gadget
,
1872 struct usb_gadget_driver
*driver
)
1874 struct net2280
*dev
;
1878 /* insist on high speed support from the driver, since
1879 * (dev->usb->xcvrdiag & FORCE_FULL_SPEED_MODE)
1880 * "must not be used in normal operation"
1882 if (!driver
|| driver
->max_speed
< USB_SPEED_HIGH
1886 dev
= container_of (_gadget
, struct net2280
, gadget
);
1888 for (i
= 0; i
< 7; i
++)
1889 dev
->ep
[i
].irqs
= 0;
1891 /* hook up the driver ... */
1892 dev
->softconnect
= 1;
1893 driver
->driver
.bus
= NULL
;
1894 dev
->driver
= driver
;
1895 dev
->gadget
.dev
.driver
= &driver
->driver
;
1897 retval
= device_create_file (&dev
->pdev
->dev
, &dev_attr_function
);
1898 if (retval
) goto err_unbind
;
1899 retval
= device_create_file (&dev
->pdev
->dev
, &dev_attr_queues
);
1900 if (retval
) goto err_func
;
1902 /* ... then enable host detection and ep0; and we're ready
1903 * for set_configuration as well as eventual disconnect.
1905 net2280_led_active (dev
, 1);
1908 DEBUG (dev
, "%s ready, usbctl %08x stdrsp %08x\n",
1909 driver
->driver
.name
,
1910 readl (&dev
->usb
->usbctl
),
1911 readl (&dev
->usb
->stdrsp
));
1913 /* pci writes may still be posted */
1917 device_remove_file (&dev
->pdev
->dev
, &dev_attr_function
);
1919 driver
->unbind (&dev
->gadget
);
1920 dev
->gadget
.dev
.driver
= NULL
;
1926 stop_activity (struct net2280
*dev
, struct usb_gadget_driver
*driver
)
1930 /* don't disconnect if it's not connected */
1931 if (dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
1934 /* stop hardware; prevent new request submissions;
1935 * and kill any outstanding requests.
1938 for (i
= 0; i
< 7; i
++)
1939 nuke (&dev
->ep
[i
]);
1944 static int net2280_stop(struct usb_gadget
*_gadget
,
1945 struct usb_gadget_driver
*driver
)
1947 struct net2280
*dev
;
1948 unsigned long flags
;
1950 dev
= container_of (_gadget
, struct net2280
, gadget
);
1952 spin_lock_irqsave (&dev
->lock
, flags
);
1953 stop_activity (dev
, driver
);
1954 spin_unlock_irqrestore (&dev
->lock
, flags
);
1956 dev
->gadget
.dev
.driver
= NULL
;
1959 net2280_led_active (dev
, 0);
1960 device_remove_file (&dev
->pdev
->dev
, &dev_attr_function
);
1961 device_remove_file (&dev
->pdev
->dev
, &dev_attr_queues
);
1963 DEBUG (dev
, "unregistered driver '%s'\n", driver
->driver
.name
);
1967 /*-------------------------------------------------------------------------*/
1969 /* handle ep0, ep-e, ep-f with 64 byte packets: packet per irq.
1970 * also works for dma-capable endpoints, in pio mode or just
1971 * to manually advance the queue after short OUT transfers.
1973 static void handle_ep_small (struct net2280_ep
*ep
)
1975 struct net2280_request
*req
;
1977 /* 0 error, 1 mid-data, 2 done */
1980 if (!list_empty (&ep
->queue
))
1981 req
= list_entry (ep
->queue
.next
,
1982 struct net2280_request
, queue
);
1986 /* ack all, and handle what we care about */
1987 t
= readl (&ep
->regs
->ep_stat
);
1990 VDEBUG (ep
->dev
, "%s ack ep_stat %08x, req %p\n",
1991 ep
->ep
.name
, t
, req
? &req
->req
: 0);
1993 if (!ep
->is_in
|| ep
->dev
->pdev
->device
== 0x2280)
1994 writel (t
& ~(1 << NAK_OUT_PACKETS
), &ep
->regs
->ep_stat
);
1996 /* Added for 2282 */
1997 writel (t
, &ep
->regs
->ep_stat
);
1999 /* for ep0, monitor token irqs to catch data stage length errors
2000 * and to synchronize on status.
2002 * also, to defer reporting of protocol stalls ... here's where
2003 * data or status first appears, handling stalls here should never
2004 * cause trouble on the host side..
2006 * control requests could be slightly faster without token synch for
2007 * status, but status can jam up that way.
2009 if (unlikely (ep
->num
== 0)) {
2011 /* status; stop NAKing */
2012 if (t
& (1 << DATA_OUT_PING_TOKEN_INTERRUPT
)) {
2013 if (ep
->dev
->protocol_stall
) {
2020 /* reply to extra IN data tokens with a zlp */
2021 } else if (t
& (1 << DATA_IN_TOKEN_INTERRUPT
)) {
2022 if (ep
->dev
->protocol_stall
) {
2026 } else if (ep
->responded
&&
2027 !req
&& !ep
->stopped
)
2028 write_fifo (ep
, NULL
);
2031 /* status; stop NAKing */
2032 if (t
& (1 << DATA_IN_TOKEN_INTERRUPT
)) {
2033 if (ep
->dev
->protocol_stall
) {
2038 /* an extra OUT token is an error */
2039 } else if (((t
& (1 << DATA_OUT_PING_TOKEN_INTERRUPT
))
2041 && req
->req
.actual
== req
->req
.length
)
2042 || (ep
->responded
&& !req
)) {
2043 ep
->dev
->protocol_stall
= 1;
2047 done (ep
, req
, -EOVERFLOW
);
2053 if (unlikely (!req
))
2056 /* manual DMA queue advance after short OUT */
2057 if (likely (ep
->dma
!= 0)) {
2058 if (t
& (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT
)) {
2060 int stopped
= ep
->stopped
;
2062 /* TRANSFERRED works around OUT_DONE erratum 0112.
2063 * we expect (N <= maxpacket) bytes; host wrote M.
2064 * iff (M < N) we won't ever see a DMA interrupt.
2067 for (count
= 0; ; t
= readl (&ep
->regs
->ep_stat
)) {
2069 /* any preceding dma transfers must finish.
2070 * dma handles (M >= N), may empty the queue
2072 scan_dma_completions (ep
);
2073 if (unlikely (list_empty (&ep
->queue
)
2074 || ep
->out_overflow
)) {
2078 req
= list_entry (ep
->queue
.next
,
2079 struct net2280_request
, queue
);
2081 /* here either (M < N), a "real" short rx;
2082 * or (M == N) and the queue didn't empty
2084 if (likely (t
& (1 << FIFO_EMPTY
))) {
2085 count
= readl (&ep
->dma
->dmacount
);
2086 count
&= DMA_BYTE_COUNT_MASK
;
2087 if (readl (&ep
->dma
->dmadesc
)
2095 /* stop DMA, leave ep NAKing */
2096 writel ((1 << DMA_ABORT
), &ep
->dma
->dmastat
);
2097 spin_stop_dma (ep
->dma
);
2100 req
->td
->dmacount
= 0;
2101 t
= readl (&ep
->regs
->ep_avail
);
2102 dma_done (ep
, req
, count
,
2103 (ep
->out_overflow
|| t
)
2107 /* also flush to prevent erratum 0106 trouble */
2108 if (unlikely (ep
->out_overflow
2109 || (ep
->dev
->chiprev
== 0x0100
2110 && ep
->dev
->gadget
.speed
2111 == USB_SPEED_FULL
))) {
2113 ep
->out_overflow
= 0;
2116 /* (re)start dma if needed, stop NAKing */
2117 ep
->stopped
= stopped
;
2118 if (!list_empty (&ep
->queue
))
2121 DEBUG (ep
->dev
, "%s dma ep_stat %08x ??\n",
2125 /* data packet(s) received (in the fifo, OUT) */
2126 } else if (t
& (1 << DATA_PACKET_RECEIVED_INTERRUPT
)) {
2127 if (read_fifo (ep
, req
) && ep
->num
!= 0)
2130 /* data packet(s) transmitted (IN) */
2131 } else if (t
& (1 << DATA_PACKET_TRANSMITTED_INTERRUPT
)) {
2134 len
= req
->req
.length
- req
->req
.actual
;
2135 if (len
> ep
->ep
.maxpacket
)
2136 len
= ep
->ep
.maxpacket
;
2137 req
->req
.actual
+= len
;
2139 /* if we wrote it all, we're usually done */
2140 if (req
->req
.actual
== req
->req
.length
) {
2142 /* send zlps until the status stage */
2143 } else if (!req
->req
.zero
|| len
!= ep
->ep
.maxpacket
)
2147 /* there was nothing to do ... */
2148 } else if (mode
== 1)
2153 /* stream endpoints often resubmit/unlink in completion */
2156 /* maybe advance queue to next request */
2158 /* NOTE: net2280 could let gadget driver start the
2159 * status stage later. since not all controllers let
2160 * them control that, the api doesn't (yet) allow it.
2166 if (!list_empty (&ep
->queue
) && !ep
->stopped
)
2167 req
= list_entry (ep
->queue
.next
,
2168 struct net2280_request
, queue
);
2171 if (req
&& !ep
->is_in
)
2172 stop_out_naking (ep
);
2176 /* is there a buffer for the next packet?
2177 * for best streaming performance, make sure there is one.
2179 if (req
&& !ep
->stopped
) {
2181 /* load IN fifo with next packet (may be zlp) */
2182 if (t
& (1 << DATA_PACKET_TRANSMITTED_INTERRUPT
))
2183 write_fifo (ep
, &req
->req
);
2187 static struct net2280_ep
*
2188 get_ep_by_addr (struct net2280
*dev
, u16 wIndex
)
2190 struct net2280_ep
*ep
;
2192 if ((wIndex
& USB_ENDPOINT_NUMBER_MASK
) == 0)
2193 return &dev
->ep
[0];
2194 list_for_each_entry (ep
, &dev
->gadget
.ep_list
, ep
.ep_list
) {
2195 u8 bEndpointAddress
;
2199 bEndpointAddress
= ep
->desc
->bEndpointAddress
;
2200 if ((wIndex
^ bEndpointAddress
) & USB_DIR_IN
)
2202 if ((wIndex
& 0x0f) == (bEndpointAddress
& 0x0f))
2208 static void handle_stat0_irqs (struct net2280
*dev
, u32 stat
)
2210 struct net2280_ep
*ep
;
2213 /* most of these don't need individual acks */
2214 stat
&= ~(1 << INTA_ASSERTED
);
2217 // DEBUG (dev, "irqstat0 %04x\n", stat);
2219 /* starting a control request? */
2220 if (unlikely (stat
& (1 << SETUP_PACKET_INTERRUPT
))) {
2223 struct usb_ctrlrequest r
;
2226 struct net2280_request
*req
;
2228 if (dev
->gadget
.speed
== USB_SPEED_UNKNOWN
) {
2229 if (readl (&dev
->usb
->usbstat
) & (1 << HIGH_SPEED
))
2230 dev
->gadget
.speed
= USB_SPEED_HIGH
;
2232 dev
->gadget
.speed
= USB_SPEED_FULL
;
2233 net2280_led_speed (dev
, dev
->gadget
.speed
);
2234 DEBUG(dev
, "%s\n", usb_speed_string(dev
->gadget
.speed
));
2240 /* make sure any leftover request state is cleared */
2241 stat
&= ~(1 << ENDPOINT_0_INTERRUPT
);
2242 while (!list_empty (&ep
->queue
)) {
2243 req
= list_entry (ep
->queue
.next
,
2244 struct net2280_request
, queue
);
2245 done (ep
, req
, (req
->req
.actual
== req
->req
.length
)
2249 dev
->protocol_stall
= 0;
2251 if (ep
->dev
->pdev
->device
== 0x2280)
2252 tmp
= (1 << FIFO_OVERFLOW
)
2253 | (1 << FIFO_UNDERFLOW
);
2257 writel (tmp
| (1 << TIMEOUT
)
2258 | (1 << USB_STALL_SENT
)
2259 | (1 << USB_IN_NAK_SENT
)
2260 | (1 << USB_IN_ACK_RCVD
)
2261 | (1 << USB_OUT_PING_NAK_SENT
)
2262 | (1 << USB_OUT_ACK_SENT
)
2263 | (1 << SHORT_PACKET_OUT_DONE_INTERRUPT
)
2264 | (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT
)
2265 | (1 << DATA_PACKET_RECEIVED_INTERRUPT
)
2266 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT
)
2267 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT
)
2268 | (1 << DATA_IN_TOKEN_INTERRUPT
)
2269 , &ep
->regs
->ep_stat
);
2270 u
.raw
[0] = readl (&dev
->usb
->setup0123
);
2271 u
.raw
[1] = readl (&dev
->usb
->setup4567
);
2273 cpu_to_le32s (&u
.raw
[0]);
2274 cpu_to_le32s (&u
.raw
[1]);
2278 #define w_value le16_to_cpu(u.r.wValue)
2279 #define w_index le16_to_cpu(u.r.wIndex)
2280 #define w_length le16_to_cpu(u.r.wLength)
2283 writel (1 << SETUP_PACKET_INTERRUPT
, &dev
->regs
->irqstat0
);
2284 stat
^= (1 << SETUP_PACKET_INTERRUPT
);
2286 /* watch control traffic at the token level, and force
2287 * synchronization before letting the status stage happen.
2288 * FIXME ignore tokens we'll NAK, until driver responds.
2289 * that'll mean a lot less irqs for some drivers.
2291 ep
->is_in
= (u
.r
.bRequestType
& USB_DIR_IN
) != 0;
2293 scratch
= (1 << DATA_PACKET_TRANSMITTED_INTERRUPT
)
2294 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT
)
2295 | (1 << DATA_IN_TOKEN_INTERRUPT
);
2296 stop_out_naking (ep
);
2298 scratch
= (1 << DATA_PACKET_RECEIVED_INTERRUPT
)
2299 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT
)
2300 | (1 << DATA_IN_TOKEN_INTERRUPT
);
2301 writel (scratch
, &dev
->epregs
[0].ep_irqenb
);
2303 /* we made the hardware handle most lowlevel requests;
2304 * everything else goes uplevel to the gadget code.
2307 switch (u
.r
.bRequest
) {
2308 case USB_REQ_GET_STATUS
: {
2309 struct net2280_ep
*e
;
2312 /* hw handles device and interface status */
2313 if (u
.r
.bRequestType
!= (USB_DIR_IN
|USB_RECIP_ENDPOINT
))
2315 if ((e
= get_ep_by_addr (dev
, w_index
)) == 0
2319 if (readl (&e
->regs
->ep_rsp
)
2320 & (1 << SET_ENDPOINT_HALT
))
2321 status
= cpu_to_le32 (1);
2323 status
= cpu_to_le32 (0);
2325 /* don't bother with a request object! */
2326 writel (0, &dev
->epregs
[0].ep_irqenb
);
2327 set_fifo_bytecount (ep
, w_length
);
2328 writel ((__force u32
)status
, &dev
->epregs
[0].ep_data
);
2330 VDEBUG (dev
, "%s stat %02x\n", ep
->ep
.name
, status
);
2331 goto next_endpoints
;
2334 case USB_REQ_CLEAR_FEATURE
: {
2335 struct net2280_ep
*e
;
2337 /* hw handles device features */
2338 if (u
.r
.bRequestType
!= USB_RECIP_ENDPOINT
)
2340 if (w_value
!= USB_ENDPOINT_HALT
2343 if ((e
= get_ep_by_addr (dev
, w_index
)) == 0)
2346 VDEBUG(dev
, "%s wedged, halt not cleared\n",
2349 VDEBUG(dev
, "%s clear halt\n", ep
->ep
.name
);
2353 goto next_endpoints
;
2356 case USB_REQ_SET_FEATURE
: {
2357 struct net2280_ep
*e
;
2359 /* hw handles device features */
2360 if (u
.r
.bRequestType
!= USB_RECIP_ENDPOINT
)
2362 if (w_value
!= USB_ENDPOINT_HALT
2365 if ((e
= get_ep_by_addr (dev
, w_index
)) == 0)
2367 if (e
->ep
.name
== ep0name
)
2371 VDEBUG (dev
, "%s set halt\n", ep
->ep
.name
);
2372 goto next_endpoints
;
2377 VDEBUG (dev
, "setup %02x.%02x v%04x i%04x l%04x "
2379 u
.r
.bRequestType
, u
.r
.bRequest
,
2380 w_value
, w_index
, w_length
,
2381 readl (&ep
->regs
->ep_cfg
));
2383 spin_unlock (&dev
->lock
);
2384 tmp
= dev
->driver
->setup (&dev
->gadget
, &u
.r
);
2385 spin_lock (&dev
->lock
);
2388 /* stall ep0 on error */
2391 VDEBUG (dev
, "req %02x.%02x protocol STALL; stat %d\n",
2392 u
.r
.bRequestType
, u
.r
.bRequest
, tmp
);
2393 dev
->protocol_stall
= 1;
2396 /* some in/out token irq should follow; maybe stall then.
2397 * driver must queue a request (even zlp) or halt ep0
2398 * before the host times out.
2407 /* endpoint data irq ? */
2408 scratch
= stat
& 0x7f;
2410 for (num
= 0; scratch
; num
++) {
2413 /* do this endpoint's FIFO and queue need tending? */
2415 if ((scratch
& t
) == 0)
2419 ep
= &dev
->ep
[num
];
2420 handle_ep_small (ep
);
2424 DEBUG (dev
, "unhandled irqstat0 %08x\n", stat
);
2427 #define DMA_INTERRUPTS ( \
2428 (1 << DMA_D_INTERRUPT) \
2429 | (1 << DMA_C_INTERRUPT) \
2430 | (1 << DMA_B_INTERRUPT) \
2431 | (1 << DMA_A_INTERRUPT))
2432 #define PCI_ERROR_INTERRUPTS ( \
2433 (1 << PCI_MASTER_ABORT_RECEIVED_INTERRUPT) \
2434 | (1 << PCI_TARGET_ABORT_RECEIVED_INTERRUPT) \
2435 | (1 << PCI_RETRY_ABORT_INTERRUPT))
2437 static void handle_stat1_irqs (struct net2280
*dev
, u32 stat
)
2439 struct net2280_ep
*ep
;
2440 u32 tmp
, num
, mask
, scratch
;
2442 /* after disconnect there's nothing else to do! */
2443 tmp
= (1 << VBUS_INTERRUPT
) | (1 << ROOT_PORT_RESET_INTERRUPT
);
2444 mask
= (1 << HIGH_SPEED
) | (1 << FULL_SPEED
);
2446 /* VBUS disconnect is indicated by VBUS_PIN and VBUS_INTERRUPT set.
2447 * Root Port Reset is indicated by ROOT_PORT_RESET_INTERRUPT set and
2448 * both HIGH_SPEED and FULL_SPEED clear (as ROOT_PORT_RESET_INTERRUPT
2449 * only indicates a change in the reset state).
2452 writel (tmp
, &dev
->regs
->irqstat1
);
2453 if ((((stat
& (1 << ROOT_PORT_RESET_INTERRUPT
))
2454 && ((readl (&dev
->usb
->usbstat
) & mask
)
2456 || ((readl (&dev
->usb
->usbctl
)
2457 & (1 << VBUS_PIN
)) == 0)
2458 ) && ( dev
->gadget
.speed
!= USB_SPEED_UNKNOWN
)) {
2459 DEBUG (dev
, "disconnect %s\n",
2460 dev
->driver
->driver
.name
);
2461 stop_activity (dev
, dev
->driver
);
2467 /* vBUS can bounce ... one of many reasons to ignore the
2468 * notion of hotplug events on bus connect/disconnect!
2474 /* NOTE: chip stays in PCI D0 state for now, but it could
2475 * enter D1 to save more power
2477 tmp
= (1 << SUSPEND_REQUEST_CHANGE_INTERRUPT
);
2479 writel (tmp
, &dev
->regs
->irqstat1
);
2480 if (stat
& (1 << SUSPEND_REQUEST_INTERRUPT
)) {
2481 if (dev
->driver
->suspend
)
2482 dev
->driver
->suspend (&dev
->gadget
);
2483 if (!enable_suspend
)
2484 stat
&= ~(1 << SUSPEND_REQUEST_INTERRUPT
);
2486 if (dev
->driver
->resume
)
2487 dev
->driver
->resume (&dev
->gadget
);
2488 /* at high speed, note erratum 0133 */
2493 /* clear any other status/irqs */
2495 writel (stat
, &dev
->regs
->irqstat1
);
2497 /* some status we can just ignore */
2498 if (dev
->pdev
->device
== 0x2280)
2499 stat
&= ~((1 << CONTROL_STATUS_INTERRUPT
)
2500 | (1 << SUSPEND_REQUEST_INTERRUPT
)
2501 | (1 << RESUME_INTERRUPT
)
2502 | (1 << SOF_INTERRUPT
));
2504 stat
&= ~((1 << CONTROL_STATUS_INTERRUPT
)
2505 | (1 << RESUME_INTERRUPT
)
2506 | (1 << SOF_DOWN_INTERRUPT
)
2507 | (1 << SOF_INTERRUPT
));
2511 // DEBUG (dev, "irqstat1 %08x\n", stat);
2513 /* DMA status, for ep-{a,b,c,d} */
2514 scratch
= stat
& DMA_INTERRUPTS
;
2515 stat
&= ~DMA_INTERRUPTS
;
2517 for (num
= 0; scratch
; num
++) {
2518 struct net2280_dma_regs __iomem
*dma
;
2521 if ((tmp
& scratch
) == 0)
2525 ep
= &dev
->ep
[num
+ 1];
2531 /* clear ep's dma status */
2532 tmp
= readl (&dma
->dmastat
);
2533 writel (tmp
, &dma
->dmastat
);
2535 /* chaining should stop on abort, short OUT from fifo,
2536 * or (stat0 codepath) short OUT transfer.
2538 if (!use_dma_chaining
) {
2539 if ((tmp
& (1 << DMA_TRANSACTION_DONE_INTERRUPT
))
2541 DEBUG (ep
->dev
, "%s no xact done? %08x\n",
2548 /* OUT transfers terminate when the data from the
2549 * host is in our memory. Process whatever's done.
2550 * On this path, we know transfer's last packet wasn't
2551 * less than req->length. NAK_OUT_PACKETS may be set,
2552 * or the FIFO may already be holding new packets.
2554 * IN transfers can linger in the FIFO for a very
2555 * long time ... we ignore that for now, accounting
2556 * precisely (like PIO does) needs per-packet irqs
2558 scan_dma_completions (ep
);
2560 /* disable dma on inactive queues; else maybe restart */
2561 if (list_empty (&ep
->queue
)) {
2562 if (use_dma_chaining
)
2565 tmp
= readl (&dma
->dmactl
);
2566 if (!use_dma_chaining
2567 || (tmp
& (1 << DMA_ENABLE
)) == 0)
2569 else if (ep
->is_in
&& use_dma_chaining
) {
2570 struct net2280_request
*req
;
2573 /* the descriptor at the head of the chain
2574 * may still have VALID_BIT clear; that's
2575 * used to trigger changing DMA_FIFO_VALIDATE
2576 * (affects automagic zlp writes).
2578 req
= list_entry (ep
->queue
.next
,
2579 struct net2280_request
, queue
);
2580 dmacount
= req
->td
->dmacount
;
2581 dmacount
&= cpu_to_le32 (
2583 | DMA_BYTE_COUNT_MASK
);
2584 if (dmacount
&& (dmacount
& valid_bit
) == 0)
2591 /* NOTE: there are other PCI errors we might usefully notice.
2592 * if they appear very often, here's where to try recovering.
2594 if (stat
& PCI_ERROR_INTERRUPTS
) {
2595 ERROR (dev
, "pci dma error; stat %08x\n", stat
);
2596 stat
&= ~PCI_ERROR_INTERRUPTS
;
2597 /* these are fatal errors, but "maybe" they won't
2600 stop_activity (dev
, dev
->driver
);
2606 DEBUG (dev
, "unhandled irqstat1 %08x\n", stat
);
2609 static irqreturn_t
net2280_irq (int irq
, void *_dev
)
2611 struct net2280
*dev
= _dev
;
2613 /* shared interrupt, not ours */
2614 if (!(readl(&dev
->regs
->irqstat0
) & (1 << INTA_ASSERTED
)))
2617 spin_lock (&dev
->lock
);
2619 /* handle disconnect, dma, and more */
2620 handle_stat1_irqs (dev
, readl (&dev
->regs
->irqstat1
));
2622 /* control requests and PIO */
2623 handle_stat0_irqs (dev
, readl (&dev
->regs
->irqstat0
));
2625 spin_unlock (&dev
->lock
);
2630 /*-------------------------------------------------------------------------*/
2632 static void gadget_release (struct device
*_dev
)
2634 struct net2280
*dev
= dev_get_drvdata (_dev
);
2639 /* tear down the binding between this driver and the pci device */
2641 static void net2280_remove (struct pci_dev
*pdev
)
2643 struct net2280
*dev
= pci_get_drvdata (pdev
);
2645 usb_del_gadget_udc(&dev
->gadget
);
2647 BUG_ON(dev
->driver
);
2649 /* then clean up the resources we allocated during probe() */
2650 net2280_led_shutdown (dev
);
2651 if (dev
->requests
) {
2653 for (i
= 1; i
< 5; i
++) {
2654 if (!dev
->ep
[i
].dummy
)
2656 pci_pool_free (dev
->requests
, dev
->ep
[i
].dummy
,
2657 dev
->ep
[i
].td_dma
);
2659 pci_pool_destroy (dev
->requests
);
2662 free_irq (pdev
->irq
, dev
);
2664 iounmap (dev
->regs
);
2666 release_mem_region (pci_resource_start (pdev
, 0),
2667 pci_resource_len (pdev
, 0));
2669 pci_disable_device (pdev
);
2670 device_unregister (&dev
->gadget
.dev
);
2671 device_remove_file (&pdev
->dev
, &dev_attr_registers
);
2672 pci_set_drvdata (pdev
, NULL
);
2674 INFO (dev
, "unbind\n");
2677 /* wrap this driver around the specified device, but
2678 * don't respond over USB until a gadget driver binds to us.
2681 static int net2280_probe (struct pci_dev
*pdev
, const struct pci_device_id
*id
)
2683 struct net2280
*dev
;
2684 unsigned long resource
, len
;
2685 void __iomem
*base
= NULL
;
2688 /* alloc, and start init */
2689 dev
= kzalloc (sizeof *dev
, GFP_KERNEL
);
2695 pci_set_drvdata (pdev
, dev
);
2696 spin_lock_init (&dev
->lock
);
2698 dev
->gadget
.ops
= &net2280_ops
;
2699 dev
->gadget
.max_speed
= USB_SPEED_HIGH
;
2701 /* the "gadget" abstracts/virtualizes the controller */
2702 dev_set_name(&dev
->gadget
.dev
, "gadget");
2703 dev
->gadget
.dev
.parent
= &pdev
->dev
;
2704 dev
->gadget
.dev
.dma_mask
= pdev
->dev
.dma_mask
;
2705 dev
->gadget
.dev
.release
= gadget_release
;
2706 dev
->gadget
.name
= driver_name
;
2708 /* now all the pci goodies ... */
2709 if (pci_enable_device (pdev
) < 0) {
2715 /* BAR 0 holds all the registers
2716 * BAR 1 is 8051 memory; unused here (note erratum 0103)
2717 * BAR 2 is fifo memory; unused here
2719 resource
= pci_resource_start (pdev
, 0);
2720 len
= pci_resource_len (pdev
, 0);
2721 if (!request_mem_region (resource
, len
, driver_name
)) {
2722 DEBUG (dev
, "controller already in use\n");
2728 /* FIXME provide firmware download interface to put
2729 * 8051 code into the chip, e.g. to turn on PCI PM.
2732 base
= ioremap_nocache (resource
, len
);
2734 DEBUG (dev
, "can't map memory\n");
2738 dev
->regs
= (struct net2280_regs __iomem
*) base
;
2739 dev
->usb
= (struct net2280_usb_regs __iomem
*) (base
+ 0x0080);
2740 dev
->pci
= (struct net2280_pci_regs __iomem
*) (base
+ 0x0100);
2741 dev
->dma
= (struct net2280_dma_regs __iomem
*) (base
+ 0x0180);
2742 dev
->dep
= (struct net2280_dep_regs __iomem
*) (base
+ 0x0200);
2743 dev
->epregs
= (struct net2280_ep_regs __iomem
*) (base
+ 0x0300);
2745 /* put into initial config, link up all endpoints */
2746 writel (0, &dev
->usb
->usbctl
);
2750 /* irq setup after old hardware is cleaned up */
2752 ERROR (dev
, "No IRQ. Check PCI setup!\n");
2757 if (request_irq (pdev
->irq
, net2280_irq
, IRQF_SHARED
, driver_name
, dev
)
2759 ERROR (dev
, "request interrupt %d failed\n", pdev
->irq
);
2766 /* NOTE: we know only the 32 LSBs of dma addresses may be nonzero */
2767 dev
->requests
= pci_pool_create ("requests", pdev
,
2768 sizeof (struct net2280_dma
),
2769 0 /* no alignment requirements */,
2770 0 /* or page-crossing issues */);
2771 if (!dev
->requests
) {
2772 DEBUG (dev
, "can't get request pool\n");
2776 for (i
= 1; i
< 5; i
++) {
2777 struct net2280_dma
*td
;
2779 td
= pci_pool_alloc (dev
->requests
, GFP_KERNEL
,
2780 &dev
->ep
[i
].td_dma
);
2782 DEBUG (dev
, "can't get dummy %d\n", i
);
2786 td
->dmacount
= 0; /* not VALID */
2787 td
->dmaaddr
= cpu_to_le32 (DMA_ADDR_INVALID
);
2788 td
->dmadesc
= td
->dmaaddr
;
2789 dev
->ep
[i
].dummy
= td
;
2792 /* enable lower-overhead pci memory bursts during DMA */
2793 writel ( (1 << DMA_MEMORY_WRITE_AND_INVALIDATE_ENABLE
)
2794 // 256 write retries may not be enough...
2795 // | (1 << PCI_RETRY_ABORT_ENABLE)
2796 | (1 << DMA_READ_MULTIPLE_ENABLE
)
2797 | (1 << DMA_READ_LINE_ENABLE
)
2798 , &dev
->pci
->pcimstctl
);
2799 /* erratum 0115 shouldn't appear: Linux inits PCI_LATENCY_TIMER */
2800 pci_set_master (pdev
);
2801 pci_try_set_mwi (pdev
);
2803 /* ... also flushes any posted pci writes */
2804 dev
->chiprev
= get_idx_reg (dev
->regs
, REG_CHIPREV
) & 0xffff;
2807 INFO (dev
, "%s\n", driver_desc
);
2808 INFO (dev
, "irq %d, pci mem %p, chip rev %04x\n",
2809 pdev
->irq
, base
, dev
->chiprev
);
2810 INFO (dev
, "version: " DRIVER_VERSION
"; dma %s\n",
2812 ? (use_dma_chaining
? "chaining" : "enabled")
2814 retval
= device_register (&dev
->gadget
.dev
);
2815 if (retval
) goto done
;
2816 retval
= device_create_file (&pdev
->dev
, &dev_attr_registers
);
2817 if (retval
) goto done
;
2819 retval
= usb_add_gadget_udc(&pdev
->dev
, &dev
->gadget
);
2826 net2280_remove (pdev
);
2830 /* make sure the board is quiescent; otherwise it will continue
2831 * generating IRQs across the upcoming reboot.
2834 static void net2280_shutdown (struct pci_dev
*pdev
)
2836 struct net2280
*dev
= pci_get_drvdata (pdev
);
2839 writel (0, &dev
->regs
->pciirqenb0
);
2840 writel (0, &dev
->regs
->pciirqenb1
);
2842 /* disable the pullup so the host will think we're gone */
2843 writel (0, &dev
->usb
->usbctl
);
2847 /*-------------------------------------------------------------------------*/
2849 static const struct pci_device_id pci_ids
[] = { {
2850 .class = ((PCI_CLASS_SERIAL_USB
<< 8) | 0xfe),
2854 .subvendor
= PCI_ANY_ID
,
2855 .subdevice
= PCI_ANY_ID
,
2857 .class = ((PCI_CLASS_SERIAL_USB
<< 8) | 0xfe),
2861 .subvendor
= PCI_ANY_ID
,
2862 .subdevice
= PCI_ANY_ID
,
2864 }, { /* end: all zeroes */ }
2866 MODULE_DEVICE_TABLE (pci
, pci_ids
);
2868 /* pci driver glue; this is a "new style" PCI driver module */
2869 static struct pci_driver net2280_pci_driver
= {
2870 .name
= (char *) driver_name
,
2871 .id_table
= pci_ids
,
2873 .probe
= net2280_probe
,
2874 .remove
= net2280_remove
,
2875 .shutdown
= net2280_shutdown
,
2877 /* FIXME add power management support */
2880 MODULE_DESCRIPTION (DRIVER_DESC
);
2881 MODULE_AUTHOR ("David Brownell");
2882 MODULE_LICENSE ("GPL");
2884 static int __init
init (void)
2887 use_dma_chaining
= 0;
2888 return pci_register_driver (&net2280_pci_driver
);
2892 static void __exit
cleanup (void)
2894 pci_unregister_driver (&net2280_pci_driver
);
2896 module_exit (cleanup
);