1 // SPDX-License-Identifier: GPL-2.0+
3 * Driver for the PLX NET2280 USB device controller.
4 * Specs and errata are available from <http://www.plxtech.com>.
6 * PLX Technology Inc. (formerly NetChip Technology) supported the
7 * development of this driver.
10 * CODE STATUS HIGHLIGHTS
12 * This driver should work well with most "gadget" drivers, including
13 * the Mass Storage, Serial, and Ethernet/RNDIS gadget drivers
14 * as well as Gadget Zero and Gadgetfs.
16 * DMA is enabled by default.
18 * MSI is enabled by default. The legacy IRQ is used if MSI couldn't
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.
28 * Copyright (C) 2014 Ricardo Ribalda - Qtechnology/AS
30 * Modified Seth Levy 2005 PLX Technology, Inc. to provide compatibility
33 * Modified Ricardo Ribalda Qtechnology AS to provide compatibility
34 * with usb 338x chip. Based on PLX driver
37 #include <linux/module.h>
38 #include <linux/pci.h>
39 #include <linux/dma-mapping.h>
40 #include <linux/kernel.h>
41 #include <linux/delay.h>
42 #include <linux/ioport.h>
43 #include <linux/slab.h>
44 #include <linux/errno.h>
45 #include <linux/init.h>
46 #include <linux/timer.h>
47 #include <linux/list.h>
48 #include <linux/interrupt.h>
49 #include <linux/moduleparam.h>
50 #include <linux/device.h>
51 #include <linux/usb/ch9.h>
52 #include <linux/usb/gadget.h>
53 #include <linux/prefetch.h>
56 #include <asm/byteorder.h>
58 #include <asm/unaligned.h>
60 #define DRIVER_DESC "PLX NET228x/USB338x USB Peripheral Controller"
61 #define DRIVER_VERSION "2005 Sept 27/v3.0"
63 #define EP_DONTUSE 13 /* nonzero */
65 #define USE_RDK_LEDS /* GPIO pins control three LEDs */
68 static const char driver_name
[] = "net2280";
69 static const char driver_desc
[] = DRIVER_DESC
;
71 static const u32 ep_bit
[9] = { 0, 17, 2, 19, 4, 1, 18, 3, 20 };
72 static const char ep0name
[] = "ep0";
74 #define EP_INFO(_name, _caps) \
82 const struct usb_ep_caps caps
;
83 } ep_info_dft
[] = { /* Default endpoint configuration */
85 USB_EP_CAPS(USB_EP_CAPS_TYPE_CONTROL
, USB_EP_CAPS_DIR_ALL
)),
87 USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL
, USB_EP_CAPS_DIR_ALL
)),
89 USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL
, USB_EP_CAPS_DIR_ALL
)),
91 USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL
, USB_EP_CAPS_DIR_ALL
)),
93 USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL
, USB_EP_CAPS_DIR_ALL
)),
95 USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL
, USB_EP_CAPS_DIR_ALL
)),
97 USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL
, USB_EP_CAPS_DIR_ALL
)),
99 USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL
, USB_EP_CAPS_DIR_ALL
)),
101 USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL
, USB_EP_CAPS_DIR_ALL
)),
102 }, ep_info_adv
[] = { /* Endpoints for usb3380 advance mode */
104 USB_EP_CAPS(USB_EP_CAPS_TYPE_CONTROL
, USB_EP_CAPS_DIR_ALL
)),
106 USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL
, USB_EP_CAPS_DIR_IN
)),
108 USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL
, USB_EP_CAPS_DIR_OUT
)),
110 USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL
, USB_EP_CAPS_DIR_IN
)),
112 USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL
, USB_EP_CAPS_DIR_OUT
)),
114 USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL
, USB_EP_CAPS_DIR_OUT
)),
116 USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL
, USB_EP_CAPS_DIR_IN
)),
118 USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL
, USB_EP_CAPS_DIR_OUT
)),
120 USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL
, USB_EP_CAPS_DIR_IN
)),
125 /* mode 0 == ep-{a,b,c,d} 1K fifo each
126 * mode 1 == ep-{a,b} 2K fifo each, ep-{c,d} unavailable
127 * mode 2 == ep-a 2K fifo, ep-{b,c} 1K each, ep-d unavailable
129 static ushort fifo_mode
;
131 /* "modprobe net2280 fifo_mode=1" etc */
132 module_param(fifo_mode
, ushort
, 0644);
134 /* enable_suspend -- When enabled, the driver will respond to
135 * USB suspend requests by powering down the NET2280. Otherwise,
136 * USB suspend requests will be ignored. This is acceptable for
137 * self-powered devices
139 static bool enable_suspend
;
141 /* "modprobe net2280 enable_suspend=1" etc */
142 module_param(enable_suspend
, bool, 0444);
144 #define DIR_STRING(bAddress) (((bAddress) & USB_DIR_IN) ? "in" : "out")
146 static char *type_string(u8 bmAttributes
)
148 switch ((bmAttributes
) & USB_ENDPOINT_XFERTYPE_MASK
) {
149 case USB_ENDPOINT_XFER_BULK
: return "bulk";
150 case USB_ENDPOINT_XFER_ISOC
: return "iso";
151 case USB_ENDPOINT_XFER_INT
: return "intr";
158 #define valid_bit cpu_to_le32(BIT(VALID_BIT))
159 #define dma_done_ie cpu_to_le32(BIT(DMA_DONE_INTERRUPT_ENABLE))
161 static void ep_clear_seqnum(struct net2280_ep
*ep
);
162 static void stop_activity(struct net2280
*dev
,
163 struct usb_gadget_driver
*driver
);
164 static void ep0_start(struct net2280
*dev
);
166 /*-------------------------------------------------------------------------*/
167 static inline void enable_pciirqenb(struct net2280_ep
*ep
)
169 u32 tmp
= readl(&ep
->dev
->regs
->pciirqenb0
);
171 if (ep
->dev
->quirks
& PLX_LEGACY
)
174 tmp
|= BIT(ep_bit
[ep
->num
]);
175 writel(tmp
, &ep
->dev
->regs
->pciirqenb0
);
181 net2280_enable(struct usb_ep
*_ep
, const struct usb_endpoint_descriptor
*desc
)
184 struct net2280_ep
*ep
;
189 static const u32 ep_key
[9] = { 1, 0, 1, 0, 1, 1, 0, 1, 0 };
192 ep
= container_of(_ep
, struct net2280_ep
, ep
);
193 if (!_ep
|| !desc
|| ep
->desc
|| _ep
->name
== ep0name
||
194 desc
->bDescriptorType
!= USB_DT_ENDPOINT
) {
195 pr_err("%s: failed at line=%d\n", __func__
, __LINE__
);
199 if (!dev
->driver
|| dev
->gadget
.speed
== USB_SPEED_UNKNOWN
) {
204 /* erratum 0119 workaround ties up an endpoint number */
205 if ((desc
->bEndpointAddress
& 0x0f) == EP_DONTUSE
) {
210 if (dev
->quirks
& PLX_PCIE
) {
211 if ((desc
->bEndpointAddress
& 0x0f) >= 0x0c) {
215 ep
->is_in
= !!usb_endpoint_dir_in(desc
);
216 if (dev
->enhanced_mode
&& ep
->is_in
&& ep_key
[ep
->num
]) {
222 /* sanity check ep-e/ep-f since their fifos are small */
223 max
= usb_endpoint_maxp(desc
);
224 if (ep
->num
> 4 && max
> 64 && (dev
->quirks
& PLX_LEGACY
)) {
229 spin_lock_irqsave(&dev
->lock
, flags
);
230 _ep
->maxpacket
= max
;
233 /* ep_reset() has already been called */
236 ep
->out_overflow
= 0;
238 /* set speed-dependent max packet; may kick in high bandwidth */
239 set_max_speed(ep
, max
);
241 /* set type, direction, address; reset fifo counters */
242 writel(BIT(FIFO_FLUSH
), &ep
->regs
->ep_stat
);
244 if ((dev
->quirks
& PLX_PCIE
) && dev
->enhanced_mode
) {
245 tmp
= readl(&ep
->cfg
->ep_cfg
);
246 /* If USB ep number doesn't match hardware ep number */
247 if ((tmp
& 0xf) != usb_endpoint_num(desc
)) {
249 spin_unlock_irqrestore(&dev
->lock
, flags
);
253 tmp
&= ~USB3380_EP_CFG_MASK_IN
;
255 tmp
&= ~USB3380_EP_CFG_MASK_OUT
;
257 type
= (desc
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
);
258 if (type
== USB_ENDPOINT_XFER_INT
) {
259 /* erratum 0105 workaround prevents hs NYET */
260 if (dev
->chiprev
== 0100 &&
261 dev
->gadget
.speed
== USB_SPEED_HIGH
&&
262 !(desc
->bEndpointAddress
& USB_DIR_IN
))
263 writel(BIT(CLEAR_NAK_OUT_PACKETS_MODE
),
265 } else if (type
== USB_ENDPOINT_XFER_BULK
) {
266 /* catch some particularly blatant driver bugs */
267 if ((dev
->gadget
.speed
== USB_SPEED_SUPER
&& max
!= 1024) ||
268 (dev
->gadget
.speed
== USB_SPEED_HIGH
&& max
!= 512) ||
269 (dev
->gadget
.speed
== USB_SPEED_FULL
&& max
> 64)) {
270 spin_unlock_irqrestore(&dev
->lock
, flags
);
275 ep
->is_iso
= (type
== USB_ENDPOINT_XFER_ISOC
);
276 /* Enable this endpoint */
277 if (dev
->quirks
& PLX_LEGACY
) {
278 tmp
|= type
<< ENDPOINT_TYPE
;
279 tmp
|= desc
->bEndpointAddress
;
280 /* default full fifo lines */
281 tmp
|= (4 << ENDPOINT_BYTE_COUNT
);
282 tmp
|= BIT(ENDPOINT_ENABLE
);
283 ep
->is_in
= (tmp
& USB_DIR_IN
) != 0;
285 /* In Legacy mode, only OUT endpoints are used */
286 if (dev
->enhanced_mode
&& ep
->is_in
) {
287 tmp
|= type
<< IN_ENDPOINT_TYPE
;
288 tmp
|= BIT(IN_ENDPOINT_ENABLE
);
290 tmp
|= type
<< OUT_ENDPOINT_TYPE
;
291 tmp
|= BIT(OUT_ENDPOINT_ENABLE
);
292 tmp
|= (ep
->is_in
<< ENDPOINT_DIRECTION
);
295 tmp
|= (4 << ENDPOINT_BYTE_COUNT
);
296 if (!dev
->enhanced_mode
)
297 tmp
|= usb_endpoint_num(desc
);
298 tmp
|= (ep
->ep
.maxburst
<< MAX_BURST_SIZE
);
301 /* Make sure all the registers are written before ep_rsp*/
304 /* for OUT transfers, block the rx fifo until a read is posted */
306 writel(BIT(SET_NAK_OUT_PACKETS
), &ep
->regs
->ep_rsp
);
307 else if (!(dev
->quirks
& PLX_2280
)) {
308 /* Added for 2282, Don't use nak packets on an in endpoint,
309 * this was ignored on 2280
311 writel(BIT(CLEAR_NAK_OUT_PACKETS
) |
312 BIT(CLEAR_NAK_OUT_PACKETS_MODE
), &ep
->regs
->ep_rsp
);
315 if (dev
->quirks
& PLX_PCIE
)
317 writel(tmp
, &ep
->cfg
->ep_cfg
);
320 if (!ep
->dma
) { /* pio, per-packet */
321 enable_pciirqenb(ep
);
323 tmp
= BIT(DATA_PACKET_RECEIVED_INTERRUPT_ENABLE
) |
324 BIT(DATA_PACKET_TRANSMITTED_INTERRUPT_ENABLE
);
325 if (dev
->quirks
& PLX_2280
)
326 tmp
|= readl(&ep
->regs
->ep_irqenb
);
327 writel(tmp
, &ep
->regs
->ep_irqenb
);
328 } else { /* dma, per-request */
329 tmp
= BIT((8 + ep
->num
)); /* completion */
330 tmp
|= readl(&dev
->regs
->pciirqenb1
);
331 writel(tmp
, &dev
->regs
->pciirqenb1
);
333 /* for short OUT transfers, dma completions can't
334 * advance the queue; do it pio-style, by hand.
335 * NOTE erratum 0112 workaround #2
337 if ((desc
->bEndpointAddress
& USB_DIR_IN
) == 0) {
338 tmp
= BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT_ENABLE
);
339 writel(tmp
, &ep
->regs
->ep_irqenb
);
341 enable_pciirqenb(ep
);
345 tmp
= desc
->bEndpointAddress
;
346 ep_dbg(dev
, "enabled %s (ep%d%s-%s) %s max %04x\n",
347 _ep
->name
, tmp
& 0x0f, DIR_STRING(tmp
),
348 type_string(desc
->bmAttributes
),
349 ep
->dma
? "dma" : "pio", max
);
351 /* pci writes may still be posted */
352 spin_unlock_irqrestore(&dev
->lock
, flags
);
356 dev_err(&ep
->dev
->pdev
->dev
, "%s: error=%d\n", __func__
, ret
);
360 static int handshake(u32 __iomem
*ptr
, u32 mask
, u32 done
, int usec
)
366 if (result
== ~(u32
)0) /* "device unplugged" */
377 static const struct usb_ep_ops net2280_ep_ops
;
379 static void ep_reset_228x(struct net2280_regs __iomem
*regs
,
380 struct net2280_ep
*ep
)
385 INIT_LIST_HEAD(&ep
->queue
);
387 usb_ep_set_maxpacket_limit(&ep
->ep
, ~0);
388 ep
->ep
.ops
= &net2280_ep_ops
;
390 /* disable the dma, irqs, endpoint... */
392 writel(0, &ep
->dma
->dmactl
);
393 writel(BIT(DMA_SCATTER_GATHER_DONE_INTERRUPT
) |
394 BIT(DMA_TRANSACTION_DONE_INTERRUPT
) |
398 tmp
= readl(®s
->pciirqenb0
);
399 tmp
&= ~BIT(ep
->num
);
400 writel(tmp
, ®s
->pciirqenb0
);
402 tmp
= readl(®s
->pciirqenb1
);
403 tmp
&= ~BIT((8 + ep
->num
)); /* completion */
404 writel(tmp
, ®s
->pciirqenb1
);
406 writel(0, &ep
->regs
->ep_irqenb
);
408 /* init to our chosen defaults, notably so that we NAK OUT
409 * packets until the driver queues a read (+note erratum 0112)
411 if (!ep
->is_in
|| (ep
->dev
->quirks
& PLX_2280
)) {
412 tmp
= BIT(SET_NAK_OUT_PACKETS_MODE
) |
413 BIT(SET_NAK_OUT_PACKETS
) |
414 BIT(CLEAR_EP_HIDE_STATUS_PHASE
) |
415 BIT(CLEAR_INTERRUPT_MODE
);
418 tmp
= BIT(CLEAR_NAK_OUT_PACKETS_MODE
) |
419 BIT(CLEAR_NAK_OUT_PACKETS
) |
420 BIT(CLEAR_EP_HIDE_STATUS_PHASE
) |
421 BIT(CLEAR_INTERRUPT_MODE
);
425 tmp
|= BIT(CLEAR_ENDPOINT_TOGGLE
) |
426 BIT(CLEAR_ENDPOINT_HALT
);
428 writel(tmp
, &ep
->regs
->ep_rsp
);
430 /* scrub most status bits, and flush any fifo state */
431 if (ep
->dev
->quirks
& PLX_2280
)
432 tmp
= BIT(FIFO_OVERFLOW
) |
437 writel(tmp
| BIT(TIMEOUT
) |
438 BIT(USB_STALL_SENT
) |
439 BIT(USB_IN_NAK_SENT
) |
440 BIT(USB_IN_ACK_RCVD
) |
441 BIT(USB_OUT_PING_NAK_SENT
) |
442 BIT(USB_OUT_ACK_SENT
) |
444 BIT(SHORT_PACKET_OUT_DONE_INTERRUPT
) |
445 BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT
) |
446 BIT(DATA_PACKET_RECEIVED_INTERRUPT
) |
447 BIT(DATA_PACKET_TRANSMITTED_INTERRUPT
) |
448 BIT(DATA_OUT_PING_TOKEN_INTERRUPT
) |
449 BIT(DATA_IN_TOKEN_INTERRUPT
),
452 /* fifo size is handled separately */
455 static void ep_reset_338x(struct net2280_regs __iomem
*regs
,
456 struct net2280_ep
*ep
)
461 INIT_LIST_HEAD(&ep
->queue
);
463 usb_ep_set_maxpacket_limit(&ep
->ep
, ~0);
464 ep
->ep
.ops
= &net2280_ep_ops
;
466 /* disable the dma, irqs, endpoint... */
468 writel(0, &ep
->dma
->dmactl
);
469 writel(BIT(DMA_ABORT_DONE_INTERRUPT
) |
470 BIT(DMA_PAUSE_DONE_INTERRUPT
) |
471 BIT(DMA_SCATTER_GATHER_DONE_INTERRUPT
) |
472 BIT(DMA_TRANSACTION_DONE_INTERRUPT
),
473 /* | BIT(DMA_ABORT), */
476 dmastat
= readl(&ep
->dma
->dmastat
);
477 if (dmastat
== 0x5002) {
478 ep_warn(ep
->dev
, "The dmastat return = %x!!\n",
480 writel(0x5a, &ep
->dma
->dmastat
);
483 tmp
= readl(®s
->pciirqenb0
);
484 tmp
&= ~BIT(ep_bit
[ep
->num
]);
485 writel(tmp
, ®s
->pciirqenb0
);
488 tmp
= readl(®s
->pciirqenb1
);
489 tmp
&= ~BIT((8 + ep
->num
)); /* completion */
490 writel(tmp
, ®s
->pciirqenb1
);
493 writel(0, &ep
->regs
->ep_irqenb
);
495 writel(BIT(SHORT_PACKET_OUT_DONE_INTERRUPT
) |
496 BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT
) |
498 BIT(DATA_PACKET_RECEIVED_INTERRUPT
) |
499 BIT(DATA_PACKET_TRANSMITTED_INTERRUPT
) |
500 BIT(DATA_OUT_PING_TOKEN_INTERRUPT
) |
501 BIT(DATA_IN_TOKEN_INTERRUPT
), &ep
->regs
->ep_stat
);
503 tmp
= readl(&ep
->cfg
->ep_cfg
);
505 tmp
&= ~USB3380_EP_CFG_MASK_IN
;
507 tmp
&= ~USB3380_EP_CFG_MASK_OUT
;
508 writel(tmp
, &ep
->cfg
->ep_cfg
);
511 static void nuke(struct net2280_ep
*);
513 static int net2280_disable(struct usb_ep
*_ep
)
515 struct net2280_ep
*ep
;
518 ep
= container_of(_ep
, struct net2280_ep
, ep
);
519 if (!_ep
|| _ep
->name
== ep0name
) {
520 pr_err("%s: Invalid ep=%p\n", __func__
, _ep
);
523 spin_lock_irqsave(&ep
->dev
->lock
, flags
);
526 if (ep
->dev
->quirks
& PLX_PCIE
)
527 ep_reset_338x(ep
->dev
->regs
, ep
);
529 ep_reset_228x(ep
->dev
->regs
, ep
);
531 ep_vdbg(ep
->dev
, "disabled %s %s\n",
532 ep
->dma
? "dma" : "pio", _ep
->name
);
534 /* synch memory views with the device */
535 (void)readl(&ep
->cfg
->ep_cfg
);
537 if (!ep
->dma
&& ep
->num
>= 1 && ep
->num
<= 4)
538 ep
->dma
= &ep
->dev
->dma
[ep
->num
- 1];
540 spin_unlock_irqrestore(&ep
->dev
->lock
, flags
);
544 /*-------------------------------------------------------------------------*/
546 static struct usb_request
547 *net2280_alloc_request(struct usb_ep
*_ep
, gfp_t gfp_flags
)
549 struct net2280_ep
*ep
;
550 struct net2280_request
*req
;
553 pr_err("%s: Invalid ep\n", __func__
);
556 ep
= container_of(_ep
, struct net2280_ep
, ep
);
558 req
= kzalloc(sizeof(*req
), gfp_flags
);
562 INIT_LIST_HEAD(&req
->queue
);
564 /* this dma descriptor may be swapped with the previous dummy */
566 struct net2280_dma
*td
;
568 td
= dma_pool_alloc(ep
->dev
->requests
, gfp_flags
,
574 td
->dmacount
= 0; /* not VALID */
575 td
->dmadesc
= td
->dmaaddr
;
581 static void net2280_free_request(struct usb_ep
*_ep
, struct usb_request
*_req
)
583 struct net2280_ep
*ep
;
584 struct net2280_request
*req
;
586 ep
= container_of(_ep
, struct net2280_ep
, ep
);
588 dev_err(&ep
->dev
->pdev
->dev
, "%s: Invalid ep=%p or req=%p\n",
589 __func__
, _ep
, _req
);
593 req
= container_of(_req
, struct net2280_request
, req
);
594 WARN_ON(!list_empty(&req
->queue
));
596 dma_pool_free(ep
->dev
->requests
, req
->td
, req
->td_dma
);
600 /*-------------------------------------------------------------------------*/
602 /* load a packet into the fifo we use for usb IN transfers.
603 * works for all endpoints.
605 * NOTE: pio with ep-a..ep-d could stuff multiple packets into the fifo
606 * at a time, but this code is simpler because it knows it only writes
607 * one packet. ep-a..ep-d should use dma instead.
609 static void write_fifo(struct net2280_ep
*ep
, struct usb_request
*req
)
611 struct net2280_ep_regs __iomem
*regs
= ep
->regs
;
614 unsigned count
, total
;
616 /* INVARIANT: fifo is currently empty. (testable) */
619 buf
= req
->buf
+ req
->actual
;
621 total
= req
->length
- req
->actual
;
627 /* write just one packet at a time */
628 count
= ep
->ep
.maxpacket
;
629 if (count
> total
) /* min() cannot be used on a bitfield */
632 ep_vdbg(ep
->dev
, "write %s fifo (IN) %d bytes%s req %p\n",
634 (count
!= ep
->ep
.maxpacket
) ? " (short)" : "",
637 /* NOTE be careful if you try to align these. fifo lines
638 * should normally be full (4 bytes) and successive partial
639 * lines are ok only in certain cases.
641 tmp
= get_unaligned((u32
*)buf
);
643 writel(tmp
, ®s
->ep_data
);
648 /* last fifo entry is "short" unless we wrote a full packet.
649 * also explicitly validate last word in (periodic) transfers
650 * when maxpacket is not a multiple of 4 bytes.
652 if (count
|| total
< ep
->ep
.maxpacket
) {
653 tmp
= count
? get_unaligned((u32
*)buf
) : count
;
655 set_fifo_bytecount(ep
, count
& 0x03);
656 writel(tmp
, ®s
->ep_data
);
659 /* pci writes may still be posted */
662 /* work around erratum 0106: PCI and USB race over the OUT fifo.
663 * caller guarantees chiprev 0100, out endpoint is NAKing, and
664 * there's no real data in the fifo.
666 * NOTE: also used in cases where that erratum doesn't apply:
667 * where the host wrote "too much" data to us.
669 static void out_flush(struct net2280_ep
*ep
)
674 statp
= &ep
->regs
->ep_stat
;
677 if (tmp
& BIT(NAK_OUT_PACKETS
)) {
678 ep_dbg(ep
->dev
, "%s %s %08x !NAK\n",
679 ep
->ep
.name
, __func__
, tmp
);
680 writel(BIT(SET_NAK_OUT_PACKETS
), &ep
->regs
->ep_rsp
);
683 writel(BIT(DATA_OUT_PING_TOKEN_INTERRUPT
) |
684 BIT(DATA_PACKET_RECEIVED_INTERRUPT
),
686 writel(BIT(FIFO_FLUSH
), statp
);
687 /* Make sure that stap is written */
690 if (tmp
& BIT(DATA_OUT_PING_TOKEN_INTERRUPT
) &&
691 /* high speed did bulk NYET; fifo isn't filling */
692 ep
->dev
->gadget
.speed
== USB_SPEED_FULL
) {
695 usec
= 50; /* 64 byte bulk/interrupt */
696 handshake(statp
, BIT(USB_OUT_PING_NAK_SENT
),
697 BIT(USB_OUT_PING_NAK_SENT
), usec
);
698 /* NAK done; now CLEAR_NAK_OUT_PACKETS is safe */
702 /* unload packet(s) from the fifo we use for usb OUT transfers.
703 * returns true iff the request completed, because of short packet
704 * or the request buffer having filled with full packets.
706 * for ep-a..ep-d this will read multiple packets out when they
707 * have been accepted.
709 static int read_fifo(struct net2280_ep
*ep
, struct net2280_request
*req
)
711 struct net2280_ep_regs __iomem
*regs
= ep
->regs
;
712 u8
*buf
= req
->req
.buf
+ req
->req
.actual
;
713 unsigned count
, tmp
, is_short
;
714 unsigned cleanup
= 0, prevent
= 0;
716 /* erratum 0106 ... packets coming in during fifo reads might
717 * be incompletely rejected. not all cases have workarounds.
719 if (ep
->dev
->chiprev
== 0x0100 &&
720 ep
->dev
->gadget
.speed
== USB_SPEED_FULL
) {
722 tmp
= readl(&ep
->regs
->ep_stat
);
723 if ((tmp
& BIT(NAK_OUT_PACKETS
)))
725 else if ((tmp
& BIT(FIFO_FULL
))) {
726 start_out_naking(ep
);
729 /* else: hope we don't see the problem */
732 /* never overflow the rx buffer. the fifo reads packets until
733 * it sees a short one; we might not be ready for them all.
736 count
= readl(®s
->ep_avail
);
737 if (unlikely(count
== 0)) {
739 tmp
= readl(&ep
->regs
->ep_stat
);
740 count
= readl(®s
->ep_avail
);
741 /* handled that data already? */
742 if (count
== 0 && (tmp
& BIT(NAK_OUT_PACKETS
)) == 0)
746 tmp
= req
->req
.length
- req
->req
.actual
;
748 /* as with DMA, data overflow gets flushed */
749 if ((tmp
% ep
->ep
.maxpacket
) != 0) {
751 "%s out fifo %d bytes, expected %d\n",
752 ep
->ep
.name
, count
, tmp
);
753 req
->req
.status
= -EOVERFLOW
;
755 /* NAK_OUT_PACKETS will be set, so flushing is safe;
756 * the next read will start with the next packet
758 } /* else it's a ZLP, no worries */
761 req
->req
.actual
+= count
;
763 is_short
= (count
== 0) || ((count
% ep
->ep
.maxpacket
) != 0);
765 ep_vdbg(ep
->dev
, "read %s fifo (OUT) %d bytes%s%s%s req %p %d/%d\n",
766 ep
->ep
.name
, count
, is_short
? " (short)" : "",
767 cleanup
? " flush" : "", prevent
? " nak" : "",
768 req
, req
->req
.actual
, req
->req
.length
);
771 tmp
= readl(®s
->ep_data
);
773 put_unaligned(tmp
, (u32
*)buf
);
778 tmp
= readl(®s
->ep_data
);
779 /* LE conversion is implicit here: */
788 writel(BIT(CLEAR_NAK_OUT_PACKETS
), &ep
->regs
->ep_rsp
);
789 (void) readl(&ep
->regs
->ep_rsp
);
792 return is_short
|| req
->req
.actual
== req
->req
.length
;
795 /* fill out dma descriptor to match a given request */
796 static void fill_dma_desc(struct net2280_ep
*ep
,
797 struct net2280_request
*req
, int valid
)
799 struct net2280_dma
*td
= req
->td
;
800 u32 dmacount
= req
->req
.length
;
802 /* don't let DMA continue after a short OUT packet,
803 * so overruns can't affect the next transfer.
804 * in case of overruns on max-size packets, we can't
805 * stop the fifo from filling but we can flush it.
808 dmacount
|= BIT(DMA_DIRECTION
);
809 if ((!ep
->is_in
&& (dmacount
% ep
->ep
.maxpacket
) != 0) ||
810 !(ep
->dev
->quirks
& PLX_2280
))
811 dmacount
|= BIT(END_OF_CHAIN
);
815 dmacount
|= BIT(VALID_BIT
);
816 dmacount
|= BIT(DMA_DONE_INTERRUPT_ENABLE
);
818 /* td->dmadesc = previously set by caller */
819 td
->dmaaddr
= cpu_to_le32 (req
->req
.dma
);
821 /* 2280 may be polling VALID_BIT through ep->dma->dmadesc */
823 td
->dmacount
= cpu_to_le32(dmacount
);
826 static const u32 dmactl_default
=
827 BIT(DMA_SCATTER_GATHER_DONE_INTERRUPT
) |
828 BIT(DMA_CLEAR_COUNT_ENABLE
) |
829 /* erratum 0116 workaround part 1 (use POLLING) */
830 (POLL_100_USEC
<< DESCRIPTOR_POLLING_RATE
) |
831 BIT(DMA_VALID_BIT_POLLING_ENABLE
) |
832 BIT(DMA_VALID_BIT_ENABLE
) |
833 BIT(DMA_SCATTER_GATHER_ENABLE
) |
834 /* erratum 0116 workaround part 2 (no AUTOSTART) */
837 static inline void spin_stop_dma(struct net2280_dma_regs __iomem
*dma
)
839 handshake(&dma
->dmactl
, BIT(DMA_ENABLE
), 0, 50);
842 static inline void stop_dma(struct net2280_dma_regs __iomem
*dma
)
844 writel(readl(&dma
->dmactl
) & ~BIT(DMA_ENABLE
), &dma
->dmactl
);
848 static void start_queue(struct net2280_ep
*ep
, u32 dmactl
, u32 td_dma
)
850 struct net2280_dma_regs __iomem
*dma
= ep
->dma
;
851 unsigned int tmp
= BIT(VALID_BIT
) | (ep
->is_in
<< DMA_DIRECTION
);
853 if (!(ep
->dev
->quirks
& PLX_2280
))
854 tmp
|= BIT(END_OF_CHAIN
);
856 writel(tmp
, &dma
->dmacount
);
857 writel(readl(&dma
->dmastat
), &dma
->dmastat
);
859 writel(td_dma
, &dma
->dmadesc
);
860 if (ep
->dev
->quirks
& PLX_PCIE
)
861 dmactl
|= BIT(DMA_REQUEST_OUTSTANDING
);
862 writel(dmactl
, &dma
->dmactl
);
864 /* erratum 0116 workaround part 3: pci arbiter away from net2280 */
865 (void) readl(&ep
->dev
->pci
->pcimstctl
);
867 writel(BIT(DMA_START
), &dma
->dmastat
);
870 static void start_dma(struct net2280_ep
*ep
, struct net2280_request
*req
)
873 struct net2280_dma_regs __iomem
*dma
= ep
->dma
;
875 /* FIXME can't use DMA for ZLPs */
877 /* on this path we "know" there's no dma active (yet) */
878 WARN_ON(readl(&dma
->dmactl
) & BIT(DMA_ENABLE
));
879 writel(0, &ep
->dma
->dmactl
);
881 /* previous OUT packet might have been short */
882 if (!ep
->is_in
&& (readl(&ep
->regs
->ep_stat
) &
883 BIT(NAK_OUT_PACKETS
))) {
884 writel(BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT
),
887 tmp
= readl(&ep
->regs
->ep_avail
);
889 writel(readl(&dma
->dmastat
), &dma
->dmastat
);
891 /* transfer all/some fifo data */
892 writel(req
->req
.dma
, &dma
->dmaaddr
);
893 tmp
= min(tmp
, req
->req
.length
);
895 /* dma irq, faking scatterlist status */
896 req
->td
->dmacount
= cpu_to_le32(req
->req
.length
- tmp
);
897 writel(BIT(DMA_DONE_INTERRUPT_ENABLE
) | tmp
,
899 req
->td
->dmadesc
= 0;
902 writel(BIT(DMA_ENABLE
), &dma
->dmactl
);
903 writel(BIT(DMA_START
), &dma
->dmastat
);
909 tmp
= dmactl_default
;
911 /* force packet boundaries between dma requests, but prevent the
912 * controller from automagically writing a last "short" packet
913 * (zero length) unless the driver explicitly said to do that.
916 if (likely((req
->req
.length
% ep
->ep
.maxpacket
) ||
918 tmp
|= BIT(DMA_FIFO_VALIDATE
);
919 ep
->in_fifo_validate
= 1;
921 ep
->in_fifo_validate
= 0;
924 /* init req->td, pointing to the current dummy */
925 req
->td
->dmadesc
= cpu_to_le32 (ep
->td_dma
);
926 fill_dma_desc(ep
, req
, 1);
928 req
->td
->dmacount
|= cpu_to_le32(BIT(END_OF_CHAIN
));
930 start_queue(ep
, tmp
, req
->td_dma
);
934 queue_dma(struct net2280_ep
*ep
, struct net2280_request
*req
, int valid
)
936 struct net2280_dma
*end
;
939 /* swap new dummy for old, link; fill and maybe activate */
945 ep
->td_dma
= req
->td_dma
;
948 end
->dmadesc
= cpu_to_le32 (ep
->td_dma
);
950 fill_dma_desc(ep
, req
, valid
);
954 done(struct net2280_ep
*ep
, struct net2280_request
*req
, int status
)
957 unsigned stopped
= ep
->stopped
;
959 list_del_init(&req
->queue
);
961 if (req
->req
.status
== -EINPROGRESS
)
962 req
->req
.status
= status
;
964 status
= req
->req
.status
;
968 usb_gadget_unmap_request(&dev
->gadget
, &req
->req
, ep
->is_in
);
970 if (status
&& status
!= -ESHUTDOWN
)
971 ep_vdbg(dev
, "complete %s req %p stat %d len %u/%u\n",
972 ep
->ep
.name
, &req
->req
, status
,
973 req
->req
.actual
, req
->req
.length
);
975 /* don't modify queue heads during completion callback */
977 spin_unlock(&dev
->lock
);
978 usb_gadget_giveback_request(&ep
->ep
, &req
->req
);
979 spin_lock(&dev
->lock
);
980 ep
->stopped
= stopped
;
983 /*-------------------------------------------------------------------------*/
986 net2280_queue(struct usb_ep
*_ep
, struct usb_request
*_req
, gfp_t gfp_flags
)
988 struct net2280_request
*req
;
989 struct net2280_ep
*ep
;
994 /* we always require a cpu-view buffer, so that we can
995 * always use pio (as fallback or whatever).
997 ep
= container_of(_ep
, struct net2280_ep
, ep
);
998 if (!_ep
|| (!ep
->desc
&& ep
->num
!= 0)) {
999 pr_err("%s: Invalid ep=%p or ep->desc\n", __func__
, _ep
);
1002 req
= container_of(_req
, struct net2280_request
, req
);
1003 if (!_req
|| !_req
->complete
|| !_req
->buf
||
1004 !list_empty(&req
->queue
)) {
1008 if (_req
->length
> (~0 & DMA_BYTE_COUNT_MASK
)) {
1013 if (!dev
->driver
|| dev
->gadget
.speed
== USB_SPEED_UNKNOWN
) {
1018 /* FIXME implement PIO fallback for ZLPs with DMA */
1019 if (ep
->dma
&& _req
->length
== 0) {
1024 /* set up dma mapping in case the caller didn't */
1026 ret
= usb_gadget_map_request(&dev
->gadget
, _req
,
1032 ep_vdbg(dev
, "%s queue req %p, len %d buf %p\n",
1033 _ep
->name
, _req
, _req
->length
, _req
->buf
);
1035 spin_lock_irqsave(&dev
->lock
, flags
);
1037 _req
->status
= -EINPROGRESS
;
1040 /* kickstart this i/o queue? */
1041 if (list_empty(&ep
->queue
) && !ep
->stopped
&&
1042 !((dev
->quirks
& PLX_PCIE
) && ep
->dma
&&
1043 (readl(&ep
->regs
->ep_rsp
) & BIT(CLEAR_ENDPOINT_HALT
)))) {
1045 /* use DMA if the endpoint supports it, else pio */
1049 /* maybe there's no control data, just status ack */
1050 if (ep
->num
== 0 && _req
->length
== 0) {
1053 ep_vdbg(dev
, "%s status ack\n", ep
->ep
.name
);
1057 /* PIO ... stuff the fifo, or unblock it. */
1059 write_fifo(ep
, _req
);
1063 /* OUT FIFO might have packet(s) buffered */
1064 s
= readl(&ep
->regs
->ep_stat
);
1065 if ((s
& BIT(FIFO_EMPTY
)) == 0) {
1066 /* note: _req->short_not_ok is
1067 * ignored here since PIO _always_
1068 * stops queue advance here, and
1069 * _req->status doesn't change for
1070 * short reads (only _req->actual)
1072 if (read_fifo(ep
, req
) &&
1076 /* don't queue it */
1078 } else if (read_fifo(ep
, req
) &&
1083 s
= readl(&ep
->regs
->ep_stat
);
1086 /* don't NAK, let the fifo fill */
1087 if (req
&& (s
& BIT(NAK_OUT_PACKETS
)))
1088 writel(BIT(CLEAR_NAK_OUT_PACKETS
),
1093 } else if (ep
->dma
) {
1099 /* preventing magic zlps is per-engine state, not
1100 * per-transfer; irq logic must recover hiccups.
1102 expect
= likely(req
->req
.zero
||
1103 (req
->req
.length
% ep
->ep
.maxpacket
));
1104 if (expect
!= ep
->in_fifo_validate
)
1107 queue_dma(ep
, req
, valid
);
1109 } /* else the irq handler advances the queue. */
1113 list_add_tail(&req
->queue
, &ep
->queue
);
1115 spin_unlock_irqrestore(&dev
->lock
, flags
);
1117 /* pci writes may still be posted */
1121 dev_err(&ep
->dev
->pdev
->dev
, "%s: error=%d\n", __func__
, ret
);
1126 dma_done(struct net2280_ep
*ep
, struct net2280_request
*req
, u32 dmacount
,
1129 req
->req
.actual
= req
->req
.length
- (DMA_BYTE_COUNT_MASK
& dmacount
);
1130 done(ep
, req
, status
);
1133 static int scan_dma_completions(struct net2280_ep
*ep
)
1135 int num_completed
= 0;
1137 /* only look at descriptors that were "naturally" retired,
1138 * so fifo and list head state won't matter
1140 while (!list_empty(&ep
->queue
)) {
1141 struct net2280_request
*req
;
1144 req
= list_entry(ep
->queue
.next
,
1145 struct net2280_request
, queue
);
1149 req_dma_count
= le32_to_cpup(&req
->td
->dmacount
);
1150 if ((req_dma_count
& BIT(VALID_BIT
)) != 0)
1153 /* SHORT_PACKET_TRANSFERRED_INTERRUPT handles "usb-short"
1154 * cases where DMA must be aborted; this code handles
1155 * all non-abort DMA completions.
1157 if (unlikely(req
->td
->dmadesc
== 0)) {
1159 u32
const ep_dmacount
= readl(&ep
->dma
->dmacount
);
1161 if (ep_dmacount
& DMA_BYTE_COUNT_MASK
)
1163 /* single transfer mode */
1164 dma_done(ep
, req
, req_dma_count
, 0);
1167 } else if (!ep
->is_in
&&
1168 (req
->req
.length
% ep
->ep
.maxpacket
) &&
1169 !(ep
->dev
->quirks
& PLX_PCIE
)) {
1171 u32
const ep_stat
= readl(&ep
->regs
->ep_stat
);
1172 /* AVOID TROUBLE HERE by not issuing short reads from
1173 * your gadget driver. That helps avoids errata 0121,
1174 * 0122, and 0124; not all cases trigger the warning.
1176 if ((ep_stat
& BIT(NAK_OUT_PACKETS
)) == 0) {
1177 ep_warn(ep
->dev
, "%s lost packet sync!\n",
1179 req
->req
.status
= -EOVERFLOW
;
1181 u32
const ep_avail
= readl(&ep
->regs
->ep_avail
);
1183 /* fifo gets flushed later */
1184 ep
->out_overflow
= 1;
1186 "%s dma, discard %d len %d\n",
1187 ep
->ep
.name
, ep_avail
,
1189 req
->req
.status
= -EOVERFLOW
;
1193 dma_done(ep
, req
, req_dma_count
, 0);
1197 return num_completed
;
1200 static void restart_dma(struct net2280_ep
*ep
)
1202 struct net2280_request
*req
;
1206 req
= list_entry(ep
->queue
.next
, struct net2280_request
, queue
);
1211 static void abort_dma(struct net2280_ep
*ep
)
1213 /* abort the current transfer */
1214 if (likely(!list_empty(&ep
->queue
))) {
1215 /* FIXME work around errata 0121, 0122, 0124 */
1216 writel(BIT(DMA_ABORT
), &ep
->dma
->dmastat
);
1217 spin_stop_dma(ep
->dma
);
1220 scan_dma_completions(ep
);
1223 /* dequeue ALL requests */
1224 static void nuke(struct net2280_ep
*ep
)
1226 struct net2280_request
*req
;
1228 /* called with spinlock held */
1232 while (!list_empty(&ep
->queue
)) {
1233 req
= list_entry(ep
->queue
.next
,
1234 struct net2280_request
,
1236 done(ep
, req
, -ESHUTDOWN
);
1240 /* dequeue JUST ONE request */
1241 static int net2280_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
1243 struct net2280_ep
*ep
;
1244 struct net2280_request
*req
;
1245 unsigned long flags
;
1249 ep
= container_of(_ep
, struct net2280_ep
, ep
);
1250 if (!_ep
|| (!ep
->desc
&& ep
->num
!= 0) || !_req
) {
1251 pr_err("%s: Invalid ep=%p or ep->desc or req=%p\n",
1252 __func__
, _ep
, _req
);
1256 spin_lock_irqsave(&ep
->dev
->lock
, flags
);
1257 stopped
= ep
->stopped
;
1259 /* quiesce dma while we patch the queue */
1263 dmactl
= readl(&ep
->dma
->dmactl
);
1264 /* WARNING erratum 0127 may kick in ... */
1266 scan_dma_completions(ep
);
1269 /* make sure it's still queued on this endpoint */
1270 list_for_each_entry(req
, &ep
->queue
, queue
) {
1271 if (&req
->req
== _req
)
1274 if (&req
->req
!= _req
) {
1275 ep
->stopped
= stopped
;
1276 spin_unlock_irqrestore(&ep
->dev
->lock
, flags
);
1277 ep_dbg(ep
->dev
, "%s: Request mismatch\n", __func__
);
1281 /* queue head may be partially complete. */
1282 if (ep
->queue
.next
== &req
->queue
) {
1284 ep_dbg(ep
->dev
, "unlink (%s) dma\n", _ep
->name
);
1285 _req
->status
= -ECONNRESET
;
1287 if (likely(ep
->queue
.next
== &req
->queue
)) {
1288 /* NOTE: misreports single-transfer mode*/
1289 req
->td
->dmacount
= 0; /* invalidate */
1291 readl(&ep
->dma
->dmacount
),
1295 ep_dbg(ep
->dev
, "unlink (%s) pio\n", _ep
->name
);
1296 done(ep
, req
, -ECONNRESET
);
1302 done(ep
, req
, -ECONNRESET
);
1303 ep
->stopped
= stopped
;
1306 /* turn off dma on inactive queues */
1307 if (list_empty(&ep
->queue
))
1309 else if (!ep
->stopped
) {
1310 /* resume current request, or start new one */
1312 writel(dmactl
, &ep
->dma
->dmactl
);
1314 start_dma(ep
, list_entry(ep
->queue
.next
,
1315 struct net2280_request
, queue
));
1319 spin_unlock_irqrestore(&ep
->dev
->lock
, flags
);
1323 /*-------------------------------------------------------------------------*/
1325 static int net2280_fifo_status(struct usb_ep
*_ep
);
1328 net2280_set_halt_and_wedge(struct usb_ep
*_ep
, int value
, int wedged
)
1330 struct net2280_ep
*ep
;
1331 unsigned long flags
;
1334 ep
= container_of(_ep
, struct net2280_ep
, ep
);
1335 if (!_ep
|| (!ep
->desc
&& ep
->num
!= 0)) {
1336 pr_err("%s: Invalid ep=%p or ep->desc\n", __func__
, _ep
);
1339 if (!ep
->dev
->driver
|| ep
->dev
->gadget
.speed
== USB_SPEED_UNKNOWN
) {
1340 retval
= -ESHUTDOWN
;
1343 if (ep
->desc
/* not ep0 */ && (ep
->desc
->bmAttributes
& 0x03)
1344 == USB_ENDPOINT_XFER_ISOC
) {
1349 spin_lock_irqsave(&ep
->dev
->lock
, flags
);
1350 if (!list_empty(&ep
->queue
)) {
1353 } else if (ep
->is_in
&& value
&& net2280_fifo_status(_ep
) != 0) {
1357 ep_vdbg(ep
->dev
, "%s %s %s\n", _ep
->name
,
1358 value
? "set" : "clear",
1359 wedged
? "wedge" : "halt");
1360 /* set/clear, then synch memory views with the device */
1363 ep
->dev
->protocol_stall
= 1;
1370 if (ep
->dev
->quirks
& PLX_PCIE
&&
1371 !list_empty(&ep
->queue
) && ep
->td_dma
)
1375 (void) readl(&ep
->regs
->ep_rsp
);
1377 spin_unlock_irqrestore(&ep
->dev
->lock
, flags
);
1382 spin_unlock_irqrestore(&ep
->dev
->lock
, flags
);
1384 dev_err(&ep
->dev
->pdev
->dev
, "%s: error=%d\n", __func__
, retval
);
1388 static int net2280_set_halt(struct usb_ep
*_ep
, int value
)
1390 return net2280_set_halt_and_wedge(_ep
, value
, 0);
1393 static int net2280_set_wedge(struct usb_ep
*_ep
)
1395 if (!_ep
|| _ep
->name
== ep0name
) {
1396 pr_err("%s: Invalid ep=%p or ep0\n", __func__
, _ep
);
1399 return net2280_set_halt_and_wedge(_ep
, 1, 1);
1402 static int net2280_fifo_status(struct usb_ep
*_ep
)
1404 struct net2280_ep
*ep
;
1407 ep
= container_of(_ep
, struct net2280_ep
, ep
);
1408 if (!_ep
|| (!ep
->desc
&& ep
->num
!= 0)) {
1409 pr_err("%s: Invalid ep=%p or ep->desc\n", __func__
, _ep
);
1412 if (!ep
->dev
->driver
|| ep
->dev
->gadget
.speed
== USB_SPEED_UNKNOWN
) {
1413 dev_err(&ep
->dev
->pdev
->dev
,
1414 "%s: Invalid driver=%p or speed=%d\n",
1415 __func__
, ep
->dev
->driver
, ep
->dev
->gadget
.speed
);
1419 avail
= readl(&ep
->regs
->ep_avail
) & (BIT(12) - 1);
1420 if (avail
> ep
->fifo_size
) {
1421 dev_err(&ep
->dev
->pdev
->dev
, "%s: Fifo overflow\n", __func__
);
1425 avail
= ep
->fifo_size
- avail
;
1429 static void net2280_fifo_flush(struct usb_ep
*_ep
)
1431 struct net2280_ep
*ep
;
1433 ep
= container_of(_ep
, struct net2280_ep
, ep
);
1434 if (!_ep
|| (!ep
->desc
&& ep
->num
!= 0)) {
1435 pr_err("%s: Invalid ep=%p or ep->desc\n", __func__
, _ep
);
1438 if (!ep
->dev
->driver
|| ep
->dev
->gadget
.speed
== USB_SPEED_UNKNOWN
) {
1439 dev_err(&ep
->dev
->pdev
->dev
,
1440 "%s: Invalid driver=%p or speed=%d\n",
1441 __func__
, ep
->dev
->driver
, ep
->dev
->gadget
.speed
);
1445 writel(BIT(FIFO_FLUSH
), &ep
->regs
->ep_stat
);
1446 (void) readl(&ep
->regs
->ep_rsp
);
1449 static const struct usb_ep_ops net2280_ep_ops
= {
1450 .enable
= net2280_enable
,
1451 .disable
= net2280_disable
,
1453 .alloc_request
= net2280_alloc_request
,
1454 .free_request
= net2280_free_request
,
1456 .queue
= net2280_queue
,
1457 .dequeue
= net2280_dequeue
,
1459 .set_halt
= net2280_set_halt
,
1460 .set_wedge
= net2280_set_wedge
,
1461 .fifo_status
= net2280_fifo_status
,
1462 .fifo_flush
= net2280_fifo_flush
,
1465 /*-------------------------------------------------------------------------*/
1467 static int net2280_get_frame(struct usb_gadget
*_gadget
)
1469 struct net2280
*dev
;
1470 unsigned long flags
;
1475 dev
= container_of(_gadget
, struct net2280
, gadget
);
1476 spin_lock_irqsave(&dev
->lock
, flags
);
1477 retval
= get_idx_reg(dev
->regs
, REG_FRAME
) & 0x03ff;
1478 spin_unlock_irqrestore(&dev
->lock
, flags
);
1482 static int net2280_wakeup(struct usb_gadget
*_gadget
)
1484 struct net2280
*dev
;
1486 unsigned long flags
;
1490 dev
= container_of(_gadget
, struct net2280
, gadget
);
1492 spin_lock_irqsave(&dev
->lock
, flags
);
1493 tmp
= readl(&dev
->usb
->usbctl
);
1494 if (tmp
& BIT(DEVICE_REMOTE_WAKEUP_ENABLE
))
1495 writel(BIT(GENERATE_RESUME
), &dev
->usb
->usbstat
);
1496 spin_unlock_irqrestore(&dev
->lock
, flags
);
1498 /* pci writes may still be posted */
1502 static int net2280_set_selfpowered(struct usb_gadget
*_gadget
, int value
)
1504 struct net2280
*dev
;
1506 unsigned long flags
;
1510 dev
= container_of(_gadget
, struct net2280
, gadget
);
1512 spin_lock_irqsave(&dev
->lock
, flags
);
1513 tmp
= readl(&dev
->usb
->usbctl
);
1515 tmp
|= BIT(SELF_POWERED_STATUS
);
1516 _gadget
->is_selfpowered
= 1;
1518 tmp
&= ~BIT(SELF_POWERED_STATUS
);
1519 _gadget
->is_selfpowered
= 0;
1521 writel(tmp
, &dev
->usb
->usbctl
);
1522 spin_unlock_irqrestore(&dev
->lock
, flags
);
1527 static int net2280_pullup(struct usb_gadget
*_gadget
, int is_on
)
1529 struct net2280
*dev
;
1531 unsigned long flags
;
1535 dev
= container_of(_gadget
, struct net2280
, gadget
);
1537 spin_lock_irqsave(&dev
->lock
, flags
);
1538 tmp
= readl(&dev
->usb
->usbctl
);
1539 dev
->softconnect
= (is_on
!= 0);
1542 writel(tmp
| BIT(USB_DETECT_ENABLE
), &dev
->usb
->usbctl
);
1544 writel(tmp
& ~BIT(USB_DETECT_ENABLE
), &dev
->usb
->usbctl
);
1545 stop_activity(dev
, NULL
);
1548 spin_unlock_irqrestore(&dev
->lock
, flags
);
1553 static struct usb_ep
*net2280_match_ep(struct usb_gadget
*_gadget
,
1554 struct usb_endpoint_descriptor
*desc
,
1555 struct usb_ss_ep_comp_descriptor
*ep_comp
)
1560 if (usb_endpoint_type(desc
) == USB_ENDPOINT_XFER_INT
) {
1561 /* ep-e, ep-f are PIO with only 64 byte fifos */
1562 ep
= gadget_find_ep_by_name(_gadget
, "ep-e");
1563 if (ep
&& usb_gadget_ep_match_desc(_gadget
, ep
, desc
, ep_comp
))
1565 ep
= gadget_find_ep_by_name(_gadget
, "ep-f");
1566 if (ep
&& usb_gadget_ep_match_desc(_gadget
, ep
, desc
, ep_comp
))
1570 /* USB3380: Only first four endpoints have DMA channels. Allocate
1571 * slower interrupt endpoints from PIO hw endpoints, to allow bulk/isoc
1572 * endpoints use DMA hw endpoints.
1574 if (usb_endpoint_type(desc
) == USB_ENDPOINT_XFER_INT
&&
1575 usb_endpoint_dir_in(desc
)) {
1576 ep
= gadget_find_ep_by_name(_gadget
, "ep2in");
1577 if (ep
&& usb_gadget_ep_match_desc(_gadget
, ep
, desc
, ep_comp
))
1579 ep
= gadget_find_ep_by_name(_gadget
, "ep4in");
1580 if (ep
&& usb_gadget_ep_match_desc(_gadget
, ep
, desc
, ep_comp
))
1582 } else if (usb_endpoint_type(desc
) == USB_ENDPOINT_XFER_INT
&&
1583 !usb_endpoint_dir_in(desc
)) {
1584 ep
= gadget_find_ep_by_name(_gadget
, "ep1out");
1585 if (ep
&& usb_gadget_ep_match_desc(_gadget
, ep
, desc
, ep_comp
))
1587 ep
= gadget_find_ep_by_name(_gadget
, "ep3out");
1588 if (ep
&& usb_gadget_ep_match_desc(_gadget
, ep
, desc
, ep_comp
))
1590 } else if (usb_endpoint_type(desc
) != USB_ENDPOINT_XFER_BULK
&&
1591 usb_endpoint_dir_in(desc
)) {
1592 ep
= gadget_find_ep_by_name(_gadget
, "ep1in");
1593 if (ep
&& usb_gadget_ep_match_desc(_gadget
, ep
, desc
, ep_comp
))
1595 ep
= gadget_find_ep_by_name(_gadget
, "ep3in");
1596 if (ep
&& usb_gadget_ep_match_desc(_gadget
, ep
, desc
, ep_comp
))
1598 } else if (usb_endpoint_type(desc
) != USB_ENDPOINT_XFER_BULK
&&
1599 !usb_endpoint_dir_in(desc
)) {
1600 ep
= gadget_find_ep_by_name(_gadget
, "ep2out");
1601 if (ep
&& usb_gadget_ep_match_desc(_gadget
, ep
, desc
, ep_comp
))
1603 ep
= gadget_find_ep_by_name(_gadget
, "ep4out");
1604 if (ep
&& usb_gadget_ep_match_desc(_gadget
, ep
, desc
, ep_comp
))
1608 /* USB3380: use same address for usb and hardware endpoints */
1609 snprintf(name
, sizeof(name
), "ep%d%s", usb_endpoint_num(desc
),
1610 usb_endpoint_dir_in(desc
) ? "in" : "out");
1611 ep
= gadget_find_ep_by_name(_gadget
, name
);
1612 if (ep
&& usb_gadget_ep_match_desc(_gadget
, ep
, desc
, ep_comp
))
1618 static int net2280_start(struct usb_gadget
*_gadget
,
1619 struct usb_gadget_driver
*driver
);
1620 static int net2280_stop(struct usb_gadget
*_gadget
);
1622 static const struct usb_gadget_ops net2280_ops
= {
1623 .get_frame
= net2280_get_frame
,
1624 .wakeup
= net2280_wakeup
,
1625 .set_selfpowered
= net2280_set_selfpowered
,
1626 .pullup
= net2280_pullup
,
1627 .udc_start
= net2280_start
,
1628 .udc_stop
= net2280_stop
,
1629 .match_ep
= net2280_match_ep
,
1632 /*-------------------------------------------------------------------------*/
1634 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1636 /* FIXME move these into procfs, and use seq_file.
1637 * Sysfs _still_ doesn't behave for arbitrarily sized files,
1638 * and also doesn't help products using this with 2.4 kernels.
1641 /* "function" sysfs attribute */
1642 static ssize_t
function_show(struct device
*_dev
, struct device_attribute
*attr
,
1645 struct net2280
*dev
= dev_get_drvdata(_dev
);
1647 if (!dev
->driver
|| !dev
->driver
->function
||
1648 strlen(dev
->driver
->function
) > PAGE_SIZE
)
1650 return scnprintf(buf
, PAGE_SIZE
, "%s\n", dev
->driver
->function
);
1652 static DEVICE_ATTR_RO(function
);
1654 static ssize_t
registers_show(struct device
*_dev
,
1655 struct device_attribute
*attr
, char *buf
)
1657 struct net2280
*dev
;
1660 unsigned long flags
;
1665 dev
= dev_get_drvdata(_dev
);
1668 spin_lock_irqsave(&dev
->lock
, flags
);
1671 s
= dev
->driver
->driver
.name
;
1675 /* Main Control Registers */
1676 t
= scnprintf(next
, size
, "%s version " DRIVER_VERSION
1677 ", chiprev %04x\n\n"
1678 "devinit %03x fifoctl %08x gadget '%s'\n"
1679 "pci irqenb0 %02x irqenb1 %08x "
1680 "irqstat0 %04x irqstat1 %08x\n",
1681 driver_name
, dev
->chiprev
,
1682 readl(&dev
->regs
->devinit
),
1683 readl(&dev
->regs
->fifoctl
),
1685 readl(&dev
->regs
->pciirqenb0
),
1686 readl(&dev
->regs
->pciirqenb1
),
1687 readl(&dev
->regs
->irqstat0
),
1688 readl(&dev
->regs
->irqstat1
));
1692 /* USB Control Registers */
1693 t1
= readl(&dev
->usb
->usbctl
);
1694 t2
= readl(&dev
->usb
->usbstat
);
1695 if (t1
& BIT(VBUS_PIN
)) {
1696 if (t2
& BIT(HIGH_SPEED
))
1698 else if (dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
1702 /* full speed bit (6) not working?? */
1705 t
= scnprintf(next
, size
,
1706 "stdrsp %08x usbctl %08x usbstat %08x "
1707 "addr 0x%02x (%s)\n",
1708 readl(&dev
->usb
->stdrsp
), t1
, t2
,
1709 readl(&dev
->usb
->ouraddr
), s
);
1713 /* PCI Master Control Registers */
1715 /* DMA Control Registers */
1717 /* Configurable EP Control Registers */
1718 for (i
= 0; i
< dev
->n_ep
; i
++) {
1719 struct net2280_ep
*ep
;
1725 t1
= readl(&ep
->cfg
->ep_cfg
);
1726 t2
= readl(&ep
->regs
->ep_rsp
) & 0xff;
1727 t
= scnprintf(next
, size
,
1728 "\n%s\tcfg %05x rsp (%02x) %s%s%s%s%s%s%s%s"
1730 ep
->ep
.name
, t1
, t2
,
1731 (t2
& BIT(CLEAR_NAK_OUT_PACKETS
))
1733 (t2
& BIT(CLEAR_EP_HIDE_STATUS_PHASE
))
1735 (t2
& BIT(CLEAR_EP_FORCE_CRC_ERROR
))
1737 (t2
& BIT(CLEAR_INTERRUPT_MODE
))
1738 ? "interrupt " : "",
1739 (t2
& BIT(CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE
))
1741 (t2
& BIT(CLEAR_NAK_OUT_PACKETS_MODE
))
1743 (t2
& BIT(CLEAR_ENDPOINT_TOGGLE
))
1744 ? "DATA1 " : "DATA0 ",
1745 (t2
& BIT(CLEAR_ENDPOINT_HALT
))
1747 readl(&ep
->regs
->ep_irqenb
));
1751 t
= scnprintf(next
, size
,
1752 "\tstat %08x avail %04x "
1754 readl(&ep
->regs
->ep_stat
),
1755 readl(&ep
->regs
->ep_avail
),
1756 t1
& 0x0f, DIR_STRING(t1
),
1757 type_string(t1
>> 8),
1758 ep
->stopped
? "*" : "");
1765 t
= scnprintf(next
, size
,
1766 " dma\tctl %08x stat %08x count %08x\n"
1767 "\taddr %08x desc %08x\n",
1768 readl(&ep
->dma
->dmactl
),
1769 readl(&ep
->dma
->dmastat
),
1770 readl(&ep
->dma
->dmacount
),
1771 readl(&ep
->dma
->dmaaddr
),
1772 readl(&ep
->dma
->dmadesc
));
1778 /* Indexed Registers (none yet) */
1781 t
= scnprintf(next
, size
, "\nirqs: ");
1784 for (i
= 0; i
< dev
->n_ep
; i
++) {
1785 struct net2280_ep
*ep
;
1790 t
= scnprintf(next
, size
, " %s/%lu", ep
->ep
.name
, ep
->irqs
);
1795 t
= scnprintf(next
, size
, "\n");
1799 spin_unlock_irqrestore(&dev
->lock
, flags
);
1801 return PAGE_SIZE
- size
;
1803 static DEVICE_ATTR_RO(registers
);
1805 static ssize_t
queues_show(struct device
*_dev
, struct device_attribute
*attr
,
1808 struct net2280
*dev
;
1811 unsigned long flags
;
1814 dev
= dev_get_drvdata(_dev
);
1817 spin_lock_irqsave(&dev
->lock
, flags
);
1819 for (i
= 0; i
< dev
->n_ep
; i
++) {
1820 struct net2280_ep
*ep
= &dev
->ep
[i
];
1821 struct net2280_request
*req
;
1825 const struct usb_endpoint_descriptor
*d
;
1830 t
= d
->bEndpointAddress
;
1831 t
= scnprintf(next
, size
,
1832 "\n%s (ep%d%s-%s) max %04x %s fifo %d\n",
1833 ep
->ep
.name
, t
& USB_ENDPOINT_NUMBER_MASK
,
1834 (t
& USB_DIR_IN
) ? "in" : "out",
1835 type_string(d
->bmAttributes
),
1836 usb_endpoint_maxp(d
),
1837 ep
->dma
? "dma" : "pio", ep
->fifo_size
1839 } else /* ep0 should only have one transfer queued */
1840 t
= scnprintf(next
, size
, "ep0 max 64 pio %s\n",
1841 ep
->is_in
? "in" : "out");
1842 if (t
<= 0 || t
> size
)
1847 if (list_empty(&ep
->queue
)) {
1848 t
= scnprintf(next
, size
, "\t(nothing queued)\n");
1849 if (t
<= 0 || t
> size
)
1855 list_for_each_entry(req
, &ep
->queue
, queue
) {
1856 if (ep
->dma
&& req
->td_dma
== readl(&ep
->dma
->dmadesc
))
1857 t
= scnprintf(next
, size
,
1858 "\treq %p len %d/%d "
1859 "buf %p (dmacount %08x)\n",
1860 &req
->req
, req
->req
.actual
,
1861 req
->req
.length
, req
->req
.buf
,
1862 readl(&ep
->dma
->dmacount
));
1864 t
= scnprintf(next
, size
,
1865 "\treq %p len %d/%d buf %p\n",
1866 &req
->req
, req
->req
.actual
,
1867 req
->req
.length
, req
->req
.buf
);
1868 if (t
<= 0 || t
> size
)
1874 struct net2280_dma
*td
;
1877 t
= scnprintf(next
, size
, "\t td %08x "
1878 " count %08x buf %08x desc %08x\n",
1880 le32_to_cpu(td
->dmacount
),
1881 le32_to_cpu(td
->dmaaddr
),
1882 le32_to_cpu(td
->dmadesc
));
1883 if (t
<= 0 || t
> size
)
1892 spin_unlock_irqrestore(&dev
->lock
, flags
);
1893 return PAGE_SIZE
- size
;
1895 static DEVICE_ATTR_RO(queues
);
1900 #define device_create_file(a, b) (0)
1901 #define device_remove_file(a, b) do { } while (0)
1905 /*-------------------------------------------------------------------------*/
1907 /* another driver-specific mode might be a request type doing dma
1908 * to/from another device fifo instead of to/from memory.
1911 static void set_fifo_mode(struct net2280
*dev
, int mode
)
1913 /* keeping high bits preserves BAR2 */
1914 writel((0xffff << PCI_BASE2_RANGE
) | mode
, &dev
->regs
->fifoctl
);
1916 /* always ep-{a,b,e,f} ... maybe not ep-c or ep-d */
1917 INIT_LIST_HEAD(&dev
->gadget
.ep_list
);
1918 list_add_tail(&dev
->ep
[1].ep
.ep_list
, &dev
->gadget
.ep_list
);
1919 list_add_tail(&dev
->ep
[2].ep
.ep_list
, &dev
->gadget
.ep_list
);
1922 list_add_tail(&dev
->ep
[3].ep
.ep_list
, &dev
->gadget
.ep_list
);
1923 list_add_tail(&dev
->ep
[4].ep
.ep_list
, &dev
->gadget
.ep_list
);
1924 dev
->ep
[1].fifo_size
= dev
->ep
[2].fifo_size
= 1024;
1927 dev
->ep
[1].fifo_size
= dev
->ep
[2].fifo_size
= 2048;
1930 list_add_tail(&dev
->ep
[3].ep
.ep_list
, &dev
->gadget
.ep_list
);
1931 dev
->ep
[1].fifo_size
= 2048;
1932 dev
->ep
[2].fifo_size
= 1024;
1935 /* fifo sizes for ep0, ep-c, ep-d, ep-e, and ep-f never change */
1936 list_add_tail(&dev
->ep
[5].ep
.ep_list
, &dev
->gadget
.ep_list
);
1937 list_add_tail(&dev
->ep
[6].ep
.ep_list
, &dev
->gadget
.ep_list
);
1940 static void defect7374_disable_data_eps(struct net2280
*dev
)
1943 * For Defect 7374, disable data EPs (and more):
1944 * - This phase undoes the earlier phase of the Defect 7374 workaround,
1945 * returing ep regs back to normal.
1947 struct net2280_ep
*ep
;
1949 unsigned char ep_sel
;
1952 for (i
= 1; i
< 5; i
++) {
1954 writel(i
, &ep
->cfg
->ep_cfg
);
1957 /* CSROUT, CSRIN, PCIOUT, PCIIN, STATIN, RCIN */
1958 for (i
= 0; i
< 6; i
++)
1959 writel(0, &dev
->dep
[i
].dep_cfg
);
1961 for (ep_sel
= 0; ep_sel
<= 21; ep_sel
++) {
1962 /* Select an endpoint for subsequent operations: */
1963 tmp_reg
= readl(&dev
->plregs
->pl_ep_ctrl
);
1964 writel(((tmp_reg
& ~0x1f) | ep_sel
), &dev
->plregs
->pl_ep_ctrl
);
1966 if (ep_sel
< 2 || (ep_sel
> 9 && ep_sel
< 14) ||
1967 ep_sel
== 18 || ep_sel
== 20)
1970 /* Change settings on some selected endpoints */
1971 tmp_reg
= readl(&dev
->plregs
->pl_ep_cfg_4
);
1972 tmp_reg
&= ~BIT(NON_CTRL_IN_TOLERATE_BAD_DIR
);
1973 writel(tmp_reg
, &dev
->plregs
->pl_ep_cfg_4
);
1974 tmp_reg
= readl(&dev
->plregs
->pl_ep_ctrl
);
1975 tmp_reg
|= BIT(EP_INITIALIZED
);
1976 writel(tmp_reg
, &dev
->plregs
->pl_ep_ctrl
);
1980 static void defect7374_enable_data_eps_zero(struct net2280
*dev
)
1982 u32 tmp
= 0, tmp_reg
;
1985 unsigned char ep_sel
;
1987 scratch
= get_idx_reg(dev
->regs
, SCRATCH
);
1989 WARN_ON((scratch
& (0xf << DEFECT7374_FSM_FIELD
))
1990 == DEFECT7374_FSM_SS_CONTROL_READ
);
1992 scratch
&= ~(0xf << DEFECT7374_FSM_FIELD
);
1994 ep_warn(dev
, "Operate Defect 7374 workaround soft this time");
1995 ep_warn(dev
, "It will operate on cold-reboot and SS connect");
1998 tmp
= ((0 << ENDPOINT_NUMBER
) | BIT(ENDPOINT_DIRECTION
) |
1999 (2 << OUT_ENDPOINT_TYPE
) | (2 << IN_ENDPOINT_TYPE
) |
2000 ((dev
->enhanced_mode
) ?
2001 BIT(OUT_ENDPOINT_ENABLE
) | BIT(IN_ENDPOINT_ENABLE
) :
2002 BIT(ENDPOINT_ENABLE
)));
2004 for (i
= 1; i
< 5; i
++)
2005 writel(tmp
, &dev
->ep
[i
].cfg
->ep_cfg
);
2007 /* CSRIN, PCIIN, STATIN, RCIN*/
2008 tmp
= ((0 << ENDPOINT_NUMBER
) | BIT(ENDPOINT_ENABLE
));
2009 writel(tmp
, &dev
->dep
[1].dep_cfg
);
2010 writel(tmp
, &dev
->dep
[3].dep_cfg
);
2011 writel(tmp
, &dev
->dep
[4].dep_cfg
);
2012 writel(tmp
, &dev
->dep
[5].dep_cfg
);
2014 /*Implemented for development and debug.
2015 * Can be refined/tuned later.*/
2016 for (ep_sel
= 0; ep_sel
<= 21; ep_sel
++) {
2017 /* Select an endpoint for subsequent operations: */
2018 tmp_reg
= readl(&dev
->plregs
->pl_ep_ctrl
);
2019 writel(((tmp_reg
& ~0x1f) | ep_sel
),
2020 &dev
->plregs
->pl_ep_ctrl
);
2024 (readl(&dev
->plregs
->pl_ep_ctrl
) |
2025 BIT(CLEAR_ACK_ERROR_CODE
) | 0);
2026 writel(tmp
, &dev
->plregs
->pl_ep_ctrl
);
2030 if (ep_sel
== 0 || (ep_sel
> 9 && ep_sel
< 14) ||
2031 ep_sel
== 18 || ep_sel
== 20)
2034 tmp
= (readl(&dev
->plregs
->pl_ep_cfg_4
) |
2035 BIT(NON_CTRL_IN_TOLERATE_BAD_DIR
) | 0);
2036 writel(tmp
, &dev
->plregs
->pl_ep_cfg_4
);
2038 tmp
= readl(&dev
->plregs
->pl_ep_ctrl
) &
2039 ~BIT(EP_INITIALIZED
);
2040 writel(tmp
, &dev
->plregs
->pl_ep_ctrl
);
2044 /* Set FSM to focus on the first Control Read:
2045 * - Tip: Connection speed is known upon the first
2047 scratch
|= DEFECT7374_FSM_WAITING_FOR_CONTROL_READ
;
2048 set_idx_reg(dev
->regs
, SCRATCH
, scratch
);
2052 /* keeping it simple:
2053 * - one bus driver, initted first;
2054 * - one function driver, initted second
2056 * most of the work to support multiple net2280 controllers would
2057 * be to associate this gadget driver (yes?) with all of them, or
2058 * perhaps to bind specific drivers to specific devices.
2061 static void usb_reset_228x(struct net2280
*dev
)
2065 dev
->gadget
.speed
= USB_SPEED_UNKNOWN
;
2066 (void) readl(&dev
->usb
->usbctl
);
2068 net2280_led_init(dev
);
2070 /* disable automatic responses, and irqs */
2071 writel(0, &dev
->usb
->stdrsp
);
2072 writel(0, &dev
->regs
->pciirqenb0
);
2073 writel(0, &dev
->regs
->pciirqenb1
);
2075 /* clear old dma and irq state */
2076 for (tmp
= 0; tmp
< 4; tmp
++) {
2077 struct net2280_ep
*ep
= &dev
->ep
[tmp
+ 1];
2082 writel(~0, &dev
->regs
->irqstat0
),
2083 writel(~(u32
)BIT(SUSPEND_REQUEST_INTERRUPT
), &dev
->regs
->irqstat1
),
2085 /* reset, and enable pci */
2086 tmp
= readl(&dev
->regs
->devinit
) |
2088 BIT(FIFO_SOFT_RESET
) |
2089 BIT(USB_SOFT_RESET
) |
2091 writel(tmp
, &dev
->regs
->devinit
);
2093 /* standard fifo and endpoint allocations */
2094 set_fifo_mode(dev
, (fifo_mode
<= 2) ? fifo_mode
: 0);
2097 static void usb_reset_338x(struct net2280
*dev
)
2101 dev
->gadget
.speed
= USB_SPEED_UNKNOWN
;
2102 (void)readl(&dev
->usb
->usbctl
);
2104 net2280_led_init(dev
);
2106 if (dev
->bug7734_patched
) {
2107 /* disable automatic responses, and irqs */
2108 writel(0, &dev
->usb
->stdrsp
);
2109 writel(0, &dev
->regs
->pciirqenb0
);
2110 writel(0, &dev
->regs
->pciirqenb1
);
2113 /* clear old dma and irq state */
2114 for (tmp
= 0; tmp
< 4; tmp
++) {
2115 struct net2280_ep
*ep
= &dev
->ep
[tmp
+ 1];
2116 struct net2280_dma_regs __iomem
*dma
;
2121 dma
= &dev
->dma
[tmp
];
2122 writel(BIT(DMA_ABORT
), &dma
->dmastat
);
2123 writel(0, &dma
->dmactl
);
2127 writel(~0, &dev
->regs
->irqstat0
), writel(~0, &dev
->regs
->irqstat1
);
2129 if (dev
->bug7734_patched
) {
2130 /* reset, and enable pci */
2131 tmp
= readl(&dev
->regs
->devinit
) |
2133 BIT(FIFO_SOFT_RESET
) |
2134 BIT(USB_SOFT_RESET
) |
2137 writel(tmp
, &dev
->regs
->devinit
);
2140 /* always ep-{1,2,3,4} ... maybe not ep-3 or ep-4 */
2141 INIT_LIST_HEAD(&dev
->gadget
.ep_list
);
2143 for (tmp
= 1; tmp
< dev
->n_ep
; tmp
++)
2144 list_add_tail(&dev
->ep
[tmp
].ep
.ep_list
, &dev
->gadget
.ep_list
);
2148 static void usb_reset(struct net2280
*dev
)
2150 if (dev
->quirks
& PLX_LEGACY
)
2151 return usb_reset_228x(dev
);
2152 return usb_reset_338x(dev
);
2155 static void usb_reinit_228x(struct net2280
*dev
)
2159 /* basic endpoint init */
2160 for (tmp
= 0; tmp
< 7; tmp
++) {
2161 struct net2280_ep
*ep
= &dev
->ep
[tmp
];
2163 ep
->ep
.name
= ep_info_dft
[tmp
].name
;
2164 ep
->ep
.caps
= ep_info_dft
[tmp
].caps
;
2168 if (tmp
> 0 && tmp
<= 4) {
2169 ep
->fifo_size
= 1024;
2170 ep
->dma
= &dev
->dma
[tmp
- 1];
2173 ep
->regs
= &dev
->epregs
[tmp
];
2174 ep
->cfg
= &dev
->epregs
[tmp
];
2175 ep_reset_228x(dev
->regs
, ep
);
2177 usb_ep_set_maxpacket_limit(&dev
->ep
[0].ep
, 64);
2178 usb_ep_set_maxpacket_limit(&dev
->ep
[5].ep
, 64);
2179 usb_ep_set_maxpacket_limit(&dev
->ep
[6].ep
, 64);
2181 dev
->gadget
.ep0
= &dev
->ep
[0].ep
;
2182 dev
->ep
[0].stopped
= 0;
2183 INIT_LIST_HEAD(&dev
->gadget
.ep0
->ep_list
);
2185 /* we want to prevent lowlevel/insecure access from the USB host,
2186 * but erratum 0119 means this enable bit is ignored
2188 for (tmp
= 0; tmp
< 5; tmp
++)
2189 writel(EP_DONTUSE
, &dev
->dep
[tmp
].dep_cfg
);
2192 static void usb_reinit_338x(struct net2280
*dev
)
2196 static const u32 ne
[9] = { 0, 1, 2, 3, 4, 1, 2, 3, 4 };
2197 static const u32 ep_reg_addr
[9] = { 0x00, 0xC0, 0x00, 0xC0, 0x00,
2198 0x00, 0xC0, 0x00, 0xC0 };
2200 /* basic endpoint init */
2201 for (i
= 0; i
< dev
->n_ep
; i
++) {
2202 struct net2280_ep
*ep
= &dev
->ep
[i
];
2204 ep
->ep
.name
= dev
->enhanced_mode
? ep_info_adv
[i
].name
:
2205 ep_info_dft
[i
].name
;
2206 ep
->ep
.caps
= dev
->enhanced_mode
? ep_info_adv
[i
].caps
:
2207 ep_info_dft
[i
].caps
;
2211 if (i
> 0 && i
<= 4)
2212 ep
->dma
= &dev
->dma
[i
- 1];
2214 if (dev
->enhanced_mode
) {
2215 ep
->cfg
= &dev
->epregs
[ne
[i
]];
2217 * Set USB endpoint number, hardware allows same number
2218 * in both directions.
2221 writel(ne
[i
], &ep
->cfg
->ep_cfg
);
2222 ep
->regs
= (struct net2280_ep_regs __iomem
*)
2223 (((void __iomem
*)&dev
->epregs
[ne
[i
]]) +
2226 ep
->cfg
= &dev
->epregs
[i
];
2227 ep
->regs
= &dev
->epregs
[i
];
2230 ep
->fifo_size
= (i
!= 0) ? 2048 : 512;
2232 ep_reset_338x(dev
->regs
, ep
);
2234 usb_ep_set_maxpacket_limit(&dev
->ep
[0].ep
, 512);
2236 dev
->gadget
.ep0
= &dev
->ep
[0].ep
;
2237 dev
->ep
[0].stopped
= 0;
2239 /* Link layer set up */
2240 if (dev
->bug7734_patched
) {
2241 tmp
= readl(&dev
->usb_ext
->usbctl2
) &
2242 ~(BIT(U1_ENABLE
) | BIT(U2_ENABLE
) | BIT(LTM_ENABLE
));
2243 writel(tmp
, &dev
->usb_ext
->usbctl2
);
2246 /* Hardware Defect and Workaround */
2247 val
= readl(&dev
->llregs
->ll_lfps_5
);
2248 val
&= ~(0xf << TIMER_LFPS_6US
);
2249 val
|= 0x5 << TIMER_LFPS_6US
;
2250 writel(val
, &dev
->llregs
->ll_lfps_5
);
2252 val
= readl(&dev
->llregs
->ll_lfps_6
);
2253 val
&= ~(0xffff << TIMER_LFPS_80US
);
2254 val
|= 0x0100 << TIMER_LFPS_80US
;
2255 writel(val
, &dev
->llregs
->ll_lfps_6
);
2258 * AA_AB Errata. Issue 4. Workaround for SuperSpeed USB
2259 * Hot Reset Exit Handshake may Fail in Specific Case using
2260 * Default Register Settings. Workaround for Enumeration test.
2262 val
= readl(&dev
->llregs
->ll_tsn_counters_2
);
2263 val
&= ~(0x1f << HOT_TX_NORESET_TS2
);
2264 val
|= 0x10 << HOT_TX_NORESET_TS2
;
2265 writel(val
, &dev
->llregs
->ll_tsn_counters_2
);
2267 val
= readl(&dev
->llregs
->ll_tsn_counters_3
);
2268 val
&= ~(0x1f << HOT_RX_RESET_TS2
);
2269 val
|= 0x3 << HOT_RX_RESET_TS2
;
2270 writel(val
, &dev
->llregs
->ll_tsn_counters_3
);
2273 * AB errata. Errata 11. Workaround for Default Duration of LFPS
2274 * Handshake Signaling for Device-Initiated U1 Exit is too short.
2275 * Without this, various enumeration failures observed with
2276 * modern superspeed hosts.
2278 val
= readl(&dev
->llregs
->ll_lfps_timers_2
);
2279 writel((val
& 0xffff0000) | LFPS_TIMERS_2_WORKAROUND_VALUE
,
2280 &dev
->llregs
->ll_lfps_timers_2
);
2283 * Set Recovery Idle to Recover bit:
2284 * - On SS connections, setting Recovery Idle to Recover Fmw improves
2285 * link robustness with various hosts and hubs.
2286 * - It is safe to set for all connection speeds; all chip revisions.
2287 * - R-M-W to leave other bits undisturbed.
2288 * - Reference PLX TT-7372
2290 val
= readl(&dev
->llregs
->ll_tsn_chicken_bit
);
2291 val
|= BIT(RECOVERY_IDLE_TO_RECOVER_FMW
);
2292 writel(val
, &dev
->llregs
->ll_tsn_chicken_bit
);
2294 INIT_LIST_HEAD(&dev
->gadget
.ep0
->ep_list
);
2296 /* disable dedicated endpoints */
2297 writel(0x0D, &dev
->dep
[0].dep_cfg
);
2298 writel(0x0D, &dev
->dep
[1].dep_cfg
);
2299 writel(0x0E, &dev
->dep
[2].dep_cfg
);
2300 writel(0x0E, &dev
->dep
[3].dep_cfg
);
2301 writel(0x0F, &dev
->dep
[4].dep_cfg
);
2302 writel(0x0C, &dev
->dep
[5].dep_cfg
);
2305 static void usb_reinit(struct net2280
*dev
)
2307 if (dev
->quirks
& PLX_LEGACY
)
2308 return usb_reinit_228x(dev
);
2309 return usb_reinit_338x(dev
);
2312 static void ep0_start_228x(struct net2280
*dev
)
2314 writel(BIT(CLEAR_EP_HIDE_STATUS_PHASE
) |
2315 BIT(CLEAR_NAK_OUT_PACKETS
) |
2316 BIT(CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE
),
2317 &dev
->epregs
[0].ep_rsp
);
2320 * hardware optionally handles a bunch of standard requests
2321 * that the API hides from drivers anyway. have it do so.
2322 * endpoint status/features are handled in software, to
2323 * help pass tests for some dubious behavior.
2325 writel(BIT(SET_TEST_MODE
) |
2327 BIT(DEVICE_SET_CLEAR_DEVICE_REMOTE_WAKEUP
) |
2328 BIT(GET_DEVICE_STATUS
) |
2329 BIT(GET_INTERFACE_STATUS
),
2331 writel(BIT(USB_ROOT_PORT_WAKEUP_ENABLE
) |
2332 BIT(SELF_POWERED_USB_DEVICE
) |
2333 BIT(REMOTE_WAKEUP_SUPPORT
) |
2334 (dev
->softconnect
<< USB_DETECT_ENABLE
) |
2335 BIT(SELF_POWERED_STATUS
),
2338 /* enable irqs so we can see ep0 and general operation */
2339 writel(BIT(SETUP_PACKET_INTERRUPT_ENABLE
) |
2340 BIT(ENDPOINT_0_INTERRUPT_ENABLE
),
2341 &dev
->regs
->pciirqenb0
);
2342 writel(BIT(PCI_INTERRUPT_ENABLE
) |
2343 BIT(PCI_MASTER_ABORT_RECEIVED_INTERRUPT_ENABLE
) |
2344 BIT(PCI_TARGET_ABORT_RECEIVED_INTERRUPT_ENABLE
) |
2345 BIT(PCI_RETRY_ABORT_INTERRUPT_ENABLE
) |
2346 BIT(VBUS_INTERRUPT_ENABLE
) |
2347 BIT(ROOT_PORT_RESET_INTERRUPT_ENABLE
) |
2348 BIT(SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE
),
2349 &dev
->regs
->pciirqenb1
);
2351 /* don't leave any writes posted */
2352 (void) readl(&dev
->usb
->usbctl
);
2355 static void ep0_start_338x(struct net2280
*dev
)
2358 if (dev
->bug7734_patched
)
2359 writel(BIT(CLEAR_NAK_OUT_PACKETS_MODE
) |
2360 BIT(SET_EP_HIDE_STATUS_PHASE
),
2361 &dev
->epregs
[0].ep_rsp
);
2364 * hardware optionally handles a bunch of standard requests
2365 * that the API hides from drivers anyway. have it do so.
2366 * endpoint status/features are handled in software, to
2367 * help pass tests for some dubious behavior.
2369 writel(BIT(SET_ISOCHRONOUS_DELAY
) |
2371 BIT(SET_TEST_MODE
) |
2373 BIT(GET_INTERFACE_STATUS
) |
2374 BIT(GET_DEVICE_STATUS
),
2376 dev
->wakeup_enable
= 1;
2377 writel(BIT(USB_ROOT_PORT_WAKEUP_ENABLE
) |
2378 (dev
->softconnect
<< USB_DETECT_ENABLE
) |
2379 BIT(DEVICE_REMOTE_WAKEUP_ENABLE
),
2382 /* enable irqs so we can see ep0 and general operation */
2383 writel(BIT(SETUP_PACKET_INTERRUPT_ENABLE
) |
2384 BIT(ENDPOINT_0_INTERRUPT_ENABLE
),
2385 &dev
->regs
->pciirqenb0
);
2386 writel(BIT(PCI_INTERRUPT_ENABLE
) |
2387 BIT(ROOT_PORT_RESET_INTERRUPT_ENABLE
) |
2388 BIT(SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE
) |
2389 BIT(VBUS_INTERRUPT_ENABLE
),
2390 &dev
->regs
->pciirqenb1
);
2392 /* don't leave any writes posted */
2393 (void)readl(&dev
->usb
->usbctl
);
2396 static void ep0_start(struct net2280
*dev
)
2398 if (dev
->quirks
& PLX_LEGACY
)
2399 return ep0_start_228x(dev
);
2400 return ep0_start_338x(dev
);
2403 /* when a driver is successfully registered, it will receive
2404 * control requests including set_configuration(), which enables
2405 * non-control requests. then usb traffic follows until a
2406 * disconnect is reported. then a host may connect again, or
2407 * the driver might get unbound.
2409 static int net2280_start(struct usb_gadget
*_gadget
,
2410 struct usb_gadget_driver
*driver
)
2412 struct net2280
*dev
;
2416 /* insist on high speed support from the driver, since
2417 * (dev->usb->xcvrdiag & FORCE_FULL_SPEED_MODE)
2418 * "must not be used in normal operation"
2420 if (!driver
|| driver
->max_speed
< USB_SPEED_HIGH
||
2424 dev
= container_of(_gadget
, struct net2280
, gadget
);
2426 for (i
= 0; i
< dev
->n_ep
; i
++)
2427 dev
->ep
[i
].irqs
= 0;
2429 /* hook up the driver ... */
2430 driver
->driver
.bus
= NULL
;
2431 dev
->driver
= driver
;
2433 retval
= device_create_file(&dev
->pdev
->dev
, &dev_attr_function
);
2436 retval
= device_create_file(&dev
->pdev
->dev
, &dev_attr_queues
);
2440 /* enable host detection and ep0; and we're ready
2441 * for set_configuration as well as eventual disconnect.
2443 net2280_led_active(dev
, 1);
2445 if ((dev
->quirks
& PLX_PCIE
) && !dev
->bug7734_patched
)
2446 defect7374_enable_data_eps_zero(dev
);
2450 /* pci writes may still be posted */
2454 device_remove_file(&dev
->pdev
->dev
, &dev_attr_function
);
2460 static void stop_activity(struct net2280
*dev
, struct usb_gadget_driver
*driver
)
2464 /* don't disconnect if it's not connected */
2465 if (dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
2468 /* stop hardware; prevent new request submissions;
2469 * and kill any outstanding requests.
2472 for (i
= 0; i
< dev
->n_ep
; i
++)
2475 /* report disconnect; the driver is already quiesced */
2477 spin_unlock(&dev
->lock
);
2478 driver
->disconnect(&dev
->gadget
);
2479 spin_lock(&dev
->lock
);
2485 static int net2280_stop(struct usb_gadget
*_gadget
)
2487 struct net2280
*dev
;
2488 unsigned long flags
;
2490 dev
= container_of(_gadget
, struct net2280
, gadget
);
2492 spin_lock_irqsave(&dev
->lock
, flags
);
2493 stop_activity(dev
, NULL
);
2494 spin_unlock_irqrestore(&dev
->lock
, flags
);
2496 net2280_led_active(dev
, 0);
2498 device_remove_file(&dev
->pdev
->dev
, &dev_attr_function
);
2499 device_remove_file(&dev
->pdev
->dev
, &dev_attr_queues
);
2506 /*-------------------------------------------------------------------------*/
2508 /* handle ep0, ep-e, ep-f with 64 byte packets: packet per irq.
2509 * also works for dma-capable endpoints, in pio mode or just
2510 * to manually advance the queue after short OUT transfers.
2512 static void handle_ep_small(struct net2280_ep
*ep
)
2514 struct net2280_request
*req
;
2516 /* 0 error, 1 mid-data, 2 done */
2519 if (!list_empty(&ep
->queue
))
2520 req
= list_entry(ep
->queue
.next
,
2521 struct net2280_request
, queue
);
2525 /* ack all, and handle what we care about */
2526 t
= readl(&ep
->regs
->ep_stat
);
2529 ep_vdbg(ep
->dev
, "%s ack ep_stat %08x, req %p\n",
2530 ep
->ep
.name
, t
, req
? &req
->req
: NULL
);
2532 if (!ep
->is_in
|| (ep
->dev
->quirks
& PLX_2280
))
2533 writel(t
& ~BIT(NAK_OUT_PACKETS
), &ep
->regs
->ep_stat
);
2535 /* Added for 2282 */
2536 writel(t
, &ep
->regs
->ep_stat
);
2538 /* for ep0, monitor token irqs to catch data stage length errors
2539 * and to synchronize on status.
2541 * also, to defer reporting of protocol stalls ... here's where
2542 * data or status first appears, handling stalls here should never
2543 * cause trouble on the host side..
2545 * control requests could be slightly faster without token synch for
2546 * status, but status can jam up that way.
2548 if (unlikely(ep
->num
== 0)) {
2550 /* status; stop NAKing */
2551 if (t
& BIT(DATA_OUT_PING_TOKEN_INTERRUPT
)) {
2552 if (ep
->dev
->protocol_stall
) {
2559 /* reply to extra IN data tokens with a zlp */
2560 } else if (t
& BIT(DATA_IN_TOKEN_INTERRUPT
)) {
2561 if (ep
->dev
->protocol_stall
) {
2565 } else if (ep
->responded
&&
2566 !req
&& !ep
->stopped
)
2567 write_fifo(ep
, NULL
);
2570 /* status; stop NAKing */
2571 if (t
& BIT(DATA_IN_TOKEN_INTERRUPT
)) {
2572 if (ep
->dev
->protocol_stall
) {
2577 /* an extra OUT token is an error */
2578 } else if (((t
& BIT(DATA_OUT_PING_TOKEN_INTERRUPT
)) &&
2580 req
->req
.actual
== req
->req
.length
) ||
2581 (ep
->responded
&& !req
)) {
2582 ep
->dev
->protocol_stall
= 1;
2586 done(ep
, req
, -EOVERFLOW
);
2595 /* manual DMA queue advance after short OUT */
2596 if (likely(ep
->dma
)) {
2597 if (t
& BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT
)) {
2598 struct net2280_request
*stuck_req
= NULL
;
2599 int stopped
= ep
->stopped
;
2604 /* TRANSFERRED works around OUT_DONE erratum 0112.
2605 * we expect (N <= maxpacket) bytes; host wrote M.
2606 * iff (M < N) we won't ever see a DMA interrupt.
2609 for (count
= 0; ; t
= readl(&ep
->regs
->ep_stat
)) {
2611 /* any preceding dma transfers must finish.
2612 * dma handles (M >= N), may empty the queue
2614 num_completed
= scan_dma_completions(ep
);
2615 if (unlikely(list_empty(&ep
->queue
) ||
2616 ep
->out_overflow
)) {
2620 req
= list_entry(ep
->queue
.next
,
2621 struct net2280_request
, queue
);
2623 /* here either (M < N), a "real" short rx;
2624 * or (M == N) and the queue didn't empty
2626 if (likely(t
& BIT(FIFO_EMPTY
))) {
2627 count
= readl(&ep
->dma
->dmacount
);
2628 count
&= DMA_BYTE_COUNT_MASK
;
2629 if (readl(&ep
->dma
->dmadesc
)
2635 /* Escape loop if no dma transfers completed
2636 * after few retries.
2638 if (num_completed
== 0) {
2639 if (stuck_req
== req
&&
2640 readl(&ep
->dma
->dmadesc
) !=
2641 req
->td_dma
&& stuck
++ > 5) {
2643 &ep
->dma
->dmacount
);
2644 count
&= DMA_BYTE_COUNT_MASK
;
2646 ep_dbg(ep
->dev
, "%s escape stuck %d, count %u\n",
2650 } else if (stuck_req
!= req
) {
2662 /* stop DMA, leave ep NAKing */
2663 writel(BIT(DMA_ABORT
), &ep
->dma
->dmastat
);
2664 spin_stop_dma(ep
->dma
);
2667 req
->td
->dmacount
= 0;
2668 t
= readl(&ep
->regs
->ep_avail
);
2669 dma_done(ep
, req
, count
,
2670 (ep
->out_overflow
|| t
)
2674 /* also flush to prevent erratum 0106 trouble */
2675 if (unlikely(ep
->out_overflow
||
2676 (ep
->dev
->chiprev
== 0x0100 &&
2677 ep
->dev
->gadget
.speed
2678 == USB_SPEED_FULL
))) {
2680 ep
->out_overflow
= 0;
2683 /* (re)start dma if needed, stop NAKing */
2684 ep
->stopped
= stopped
;
2685 if (!list_empty(&ep
->queue
))
2688 ep_dbg(ep
->dev
, "%s dma ep_stat %08x ??\n",
2692 /* data packet(s) received (in the fifo, OUT) */
2693 } else if (t
& BIT(DATA_PACKET_RECEIVED_INTERRUPT
)) {
2694 if (read_fifo(ep
, req
) && ep
->num
!= 0)
2697 /* data packet(s) transmitted (IN) */
2698 } else if (t
& BIT(DATA_PACKET_TRANSMITTED_INTERRUPT
)) {
2701 len
= req
->req
.length
- req
->req
.actual
;
2702 if (len
> ep
->ep
.maxpacket
)
2703 len
= ep
->ep
.maxpacket
;
2704 req
->req
.actual
+= len
;
2706 /* if we wrote it all, we're usually done */
2707 /* send zlps until the status stage */
2708 if ((req
->req
.actual
== req
->req
.length
) &&
2709 (!req
->req
.zero
|| len
!= ep
->ep
.maxpacket
) && ep
->num
)
2712 /* there was nothing to do ... */
2713 } else if (mode
== 1)
2718 /* stream endpoints often resubmit/unlink in completion */
2721 /* maybe advance queue to next request */
2723 /* NOTE: net2280 could let gadget driver start the
2724 * status stage later. since not all controllers let
2725 * them control that, the api doesn't (yet) allow it.
2731 if (!list_empty(&ep
->queue
) && !ep
->stopped
)
2732 req
= list_entry(ep
->queue
.next
,
2733 struct net2280_request
, queue
);
2736 if (req
&& !ep
->is_in
)
2737 stop_out_naking(ep
);
2741 /* is there a buffer for the next packet?
2742 * for best streaming performance, make sure there is one.
2744 if (req
&& !ep
->stopped
) {
2746 /* load IN fifo with next packet (may be zlp) */
2747 if (t
& BIT(DATA_PACKET_TRANSMITTED_INTERRUPT
))
2748 write_fifo(ep
, &req
->req
);
2752 static struct net2280_ep
*get_ep_by_addr(struct net2280
*dev
, u16 wIndex
)
2754 struct net2280_ep
*ep
;
2756 if ((wIndex
& USB_ENDPOINT_NUMBER_MASK
) == 0)
2758 list_for_each_entry(ep
, &dev
->gadget
.ep_list
, ep
.ep_list
) {
2759 u8 bEndpointAddress
;
2763 bEndpointAddress
= ep
->desc
->bEndpointAddress
;
2764 if ((wIndex
^ bEndpointAddress
) & USB_DIR_IN
)
2766 if ((wIndex
& 0x0f) == (bEndpointAddress
& 0x0f))
2772 static void defect7374_workaround(struct net2280
*dev
, struct usb_ctrlrequest r
)
2774 u32 scratch
, fsmvalue
;
2775 u32 ack_wait_timeout
, state
;
2777 /* Workaround for Defect 7374 (U1/U2 erroneously rejected): */
2778 scratch
= get_idx_reg(dev
->regs
, SCRATCH
);
2779 fsmvalue
= scratch
& (0xf << DEFECT7374_FSM_FIELD
);
2780 scratch
&= ~(0xf << DEFECT7374_FSM_FIELD
);
2782 if (!((fsmvalue
== DEFECT7374_FSM_WAITING_FOR_CONTROL_READ
) &&
2783 (r
.bRequestType
& USB_DIR_IN
)))
2786 /* This is the first Control Read for this connection: */
2787 if (!(readl(&dev
->usb
->usbstat
) & BIT(SUPER_SPEED_MODE
))) {
2789 * Connection is NOT SS:
2790 * - Connection must be FS or HS.
2791 * - This FSM state should allow workaround software to
2792 * run after the next USB connection.
2794 scratch
|= DEFECT7374_FSM_NON_SS_CONTROL_READ
;
2795 dev
->bug7734_patched
= 1;
2796 goto restore_data_eps
;
2799 /* Connection is SS: */
2800 for (ack_wait_timeout
= 0;
2801 ack_wait_timeout
< DEFECT_7374_NUMBEROF_MAX_WAIT_LOOPS
;
2802 ack_wait_timeout
++) {
2804 state
= readl(&dev
->plregs
->pl_ep_status_1
)
2806 if ((state
>= (ACK_GOOD_NORMAL
<< STATE
)) &&
2807 (state
<= (ACK_GOOD_MORE_ACKS_TO_COME
<< STATE
))) {
2808 scratch
|= DEFECT7374_FSM_SS_CONTROL_READ
;
2809 dev
->bug7734_patched
= 1;
2814 * We have not yet received host's Data Phase ACK
2815 * - Wait and try again.
2817 udelay(DEFECT_7374_PROCESSOR_WAIT_TIME
);
2823 if (ack_wait_timeout
>= DEFECT_7374_NUMBEROF_MAX_WAIT_LOOPS
) {
2824 ep_err(dev
, "FAIL: Defect 7374 workaround waited but failed "
2825 "to detect SS host's data phase ACK.");
2826 ep_err(dev
, "PL_EP_STATUS_1(23:16):.Expected from 0x11 to 0x16"
2827 "got 0x%2.2x.\n", state
>> STATE
);
2829 ep_warn(dev
, "INFO: Defect 7374 workaround waited about\n"
2830 "%duSec for Control Read Data Phase ACK\n",
2831 DEFECT_7374_PROCESSOR_WAIT_TIME
* ack_wait_timeout
);
2836 * Restore data EPs to their pre-workaround settings (disabled,
2837 * initialized, and other details).
2839 defect7374_disable_data_eps(dev
);
2841 set_idx_reg(dev
->regs
, SCRATCH
, scratch
);
2846 static void ep_clear_seqnum(struct net2280_ep
*ep
)
2848 struct net2280
*dev
= ep
->dev
;
2850 static const u32 ep_pl
[9] = { 0, 3, 4, 7, 8, 2, 5, 6, 9 };
2852 val
= readl(&dev
->plregs
->pl_ep_ctrl
) & ~0x1f;
2853 val
|= ep_pl
[ep
->num
];
2854 writel(val
, &dev
->plregs
->pl_ep_ctrl
);
2855 val
|= BIT(SEQUENCE_NUMBER_RESET
);
2856 writel(val
, &dev
->plregs
->pl_ep_ctrl
);
2861 static void handle_stat0_irqs_superspeed(struct net2280
*dev
,
2862 struct net2280_ep
*ep
, struct usb_ctrlrequest r
)
2864 struct net2280_ep
*e
;
2868 #define w_value le16_to_cpu(r.wValue)
2869 #define w_index le16_to_cpu(r.wIndex)
2870 #define w_length le16_to_cpu(r.wLength)
2872 switch (r
.bRequest
) {
2873 case USB_REQ_SET_CONFIGURATION
:
2874 dev
->addressed_state
= !w_value
;
2877 case USB_REQ_GET_STATUS
:
2878 switch (r
.bRequestType
) {
2879 case (USB_DIR_IN
| USB_TYPE_STANDARD
| USB_RECIP_DEVICE
):
2880 status
= dev
->wakeup_enable
? 0x02 : 0x00;
2881 if (dev
->gadget
.is_selfpowered
)
2883 status
|= (dev
->u1_enable
<< 2 | dev
->u2_enable
<< 3 |
2884 dev
->ltm_enable
<< 4);
2885 writel(0, &dev
->epregs
[0].ep_irqenb
);
2886 set_fifo_bytecount(ep
, sizeof(status
));
2887 writel((__force u32
) status
, &dev
->epregs
[0].ep_data
);
2888 allow_status_338x(ep
);
2891 case (USB_DIR_IN
| USB_TYPE_STANDARD
| USB_RECIP_ENDPOINT
):
2892 e
= get_ep_by_addr(dev
, w_index
);
2895 status
= readl(&e
->regs
->ep_rsp
) &
2896 BIT(CLEAR_ENDPOINT_HALT
);
2897 writel(0, &dev
->epregs
[0].ep_irqenb
);
2898 set_fifo_bytecount(ep
, sizeof(status
));
2899 writel((__force u32
) status
, &dev
->epregs
[0].ep_data
);
2900 allow_status_338x(ep
);
2908 case USB_REQ_CLEAR_FEATURE
:
2909 switch (r
.bRequestType
) {
2910 case (USB_DIR_OUT
| USB_TYPE_STANDARD
| USB_RECIP_DEVICE
):
2911 if (!dev
->addressed_state
) {
2913 case USB_DEVICE_U1_ENABLE
:
2915 writel(readl(&dev
->usb_ext
->usbctl2
) &
2917 &dev
->usb_ext
->usbctl2
);
2918 allow_status_338x(ep
);
2919 goto next_endpoints3
;
2921 case USB_DEVICE_U2_ENABLE
:
2923 writel(readl(&dev
->usb_ext
->usbctl2
) &
2925 &dev
->usb_ext
->usbctl2
);
2926 allow_status_338x(ep
);
2927 goto next_endpoints3
;
2929 case USB_DEVICE_LTM_ENABLE
:
2930 dev
->ltm_enable
= 0;
2931 writel(readl(&dev
->usb_ext
->usbctl2
) &
2933 &dev
->usb_ext
->usbctl2
);
2934 allow_status_338x(ep
);
2935 goto next_endpoints3
;
2941 if (w_value
== USB_DEVICE_REMOTE_WAKEUP
) {
2942 dev
->wakeup_enable
= 0;
2943 writel(readl(&dev
->usb
->usbctl
) &
2944 ~BIT(DEVICE_REMOTE_WAKEUP_ENABLE
),
2946 allow_status_338x(ep
);
2951 case (USB_DIR_OUT
| USB_TYPE_STANDARD
| USB_RECIP_ENDPOINT
):
2952 e
= get_ep_by_addr(dev
, w_index
);
2955 if (w_value
!= USB_ENDPOINT_HALT
)
2957 ep_vdbg(dev
, "%s clear halt\n", e
->ep
.name
);
2959 * Workaround for SS SeqNum not cleared via
2960 * Endpoint Halt (Clear) bit. select endpoint
2964 if (!list_empty(&e
->queue
) && e
->td_dma
)
2974 case USB_REQ_SET_FEATURE
:
2975 switch (r
.bRequestType
) {
2976 case (USB_DIR_OUT
| USB_TYPE_STANDARD
| USB_RECIP_DEVICE
):
2977 if (!dev
->addressed_state
) {
2979 case USB_DEVICE_U1_ENABLE
:
2981 writel(readl(&dev
->usb_ext
->usbctl2
) |
2983 &dev
->usb_ext
->usbctl2
);
2984 allow_status_338x(ep
);
2985 goto next_endpoints3
;
2987 case USB_DEVICE_U2_ENABLE
:
2989 writel(readl(&dev
->usb_ext
->usbctl2
) |
2991 &dev
->usb_ext
->usbctl2
);
2992 allow_status_338x(ep
);
2993 goto next_endpoints3
;
2995 case USB_DEVICE_LTM_ENABLE
:
2996 dev
->ltm_enable
= 1;
2997 writel(readl(&dev
->usb_ext
->usbctl2
) |
2999 &dev
->usb_ext
->usbctl2
);
3000 allow_status_338x(ep
);
3001 goto next_endpoints3
;
3007 if (w_value
== USB_DEVICE_REMOTE_WAKEUP
) {
3008 dev
->wakeup_enable
= 1;
3009 writel(readl(&dev
->usb
->usbctl
) |
3010 BIT(DEVICE_REMOTE_WAKEUP_ENABLE
),
3012 allow_status_338x(ep
);
3017 case (USB_DIR_OUT
| USB_TYPE_STANDARD
| USB_RECIP_ENDPOINT
):
3018 e
= get_ep_by_addr(dev
, w_index
);
3019 if (!e
|| (w_value
!= USB_ENDPOINT_HALT
))
3023 ep
->dev
->protocol_stall
= 1;
3029 allow_status_338x(ep
);
3040 ep_vdbg(dev
, "setup %02x.%02x v%04x i%04x l%04x ep_cfg %08x\n",
3041 r
.bRequestType
, r
.bRequest
,
3042 w_value
, w_index
, w_length
,
3043 readl(&ep
->cfg
->ep_cfg
));
3046 spin_unlock(&dev
->lock
);
3047 tmp
= dev
->driver
->setup(&dev
->gadget
, &r
);
3048 spin_lock(&dev
->lock
);
3052 ep_vdbg(dev
, "req %02x.%02x protocol STALL; stat %d\n",
3053 r
.bRequestType
, r
.bRequest
, tmp
);
3054 dev
->protocol_stall
= 1;
3055 /* TD 9.9 Halt Endpoint test. TD 9.22 Set feature test */
3068 static void usb338x_handle_ep_intr(struct net2280
*dev
, u32 stat0
)
3073 for (index
= 0; index
< ARRAY_SIZE(ep_bit
); index
++) {
3074 bit
= BIT(ep_bit
[index
]);
3084 handle_ep_small(&dev
->ep
[index
]);
3088 static void handle_stat0_irqs(struct net2280
*dev
, u32 stat
)
3090 struct net2280_ep
*ep
;
3093 /* most of these don't need individual acks */
3094 stat
&= ~BIT(INTA_ASSERTED
);
3097 /* ep_dbg(dev, "irqstat0 %04x\n", stat); */
3099 /* starting a control request? */
3100 if (unlikely(stat
& BIT(SETUP_PACKET_INTERRUPT
))) {
3103 struct usb_ctrlrequest r
;
3106 struct net2280_request
*req
;
3108 if (dev
->gadget
.speed
== USB_SPEED_UNKNOWN
) {
3109 u32 val
= readl(&dev
->usb
->usbstat
);
3110 if (val
& BIT(SUPER_SPEED
)) {
3111 dev
->gadget
.speed
= USB_SPEED_SUPER
;
3112 usb_ep_set_maxpacket_limit(&dev
->ep
[0].ep
,
3113 EP0_SS_MAX_PACKET_SIZE
);
3114 } else if (val
& BIT(HIGH_SPEED
)) {
3115 dev
->gadget
.speed
= USB_SPEED_HIGH
;
3116 usb_ep_set_maxpacket_limit(&dev
->ep
[0].ep
,
3117 EP0_HS_MAX_PACKET_SIZE
);
3119 dev
->gadget
.speed
= USB_SPEED_FULL
;
3120 usb_ep_set_maxpacket_limit(&dev
->ep
[0].ep
,
3121 EP0_HS_MAX_PACKET_SIZE
);
3123 net2280_led_speed(dev
, dev
->gadget
.speed
);
3125 usb_speed_string(dev
->gadget
.speed
));
3131 /* make sure any leftover request state is cleared */
3132 stat
&= ~BIT(ENDPOINT_0_INTERRUPT
);
3133 while (!list_empty(&ep
->queue
)) {
3134 req
= list_entry(ep
->queue
.next
,
3135 struct net2280_request
, queue
);
3136 done(ep
, req
, (req
->req
.actual
== req
->req
.length
)
3140 dev
->protocol_stall
= 0;
3141 if (!(dev
->quirks
& PLX_PCIE
)) {
3142 if (ep
->dev
->quirks
& PLX_2280
)
3143 tmp
= BIT(FIFO_OVERFLOW
) |
3144 BIT(FIFO_UNDERFLOW
);
3148 writel(tmp
| BIT(TIMEOUT
) |
3149 BIT(USB_STALL_SENT
) |
3150 BIT(USB_IN_NAK_SENT
) |
3151 BIT(USB_IN_ACK_RCVD
) |
3152 BIT(USB_OUT_PING_NAK_SENT
) |
3153 BIT(USB_OUT_ACK_SENT
) |
3154 BIT(SHORT_PACKET_OUT_DONE_INTERRUPT
) |
3155 BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT
) |
3156 BIT(DATA_PACKET_RECEIVED_INTERRUPT
) |
3157 BIT(DATA_PACKET_TRANSMITTED_INTERRUPT
) |
3158 BIT(DATA_OUT_PING_TOKEN_INTERRUPT
) |
3159 BIT(DATA_IN_TOKEN_INTERRUPT
),
3160 &ep
->regs
->ep_stat
);
3162 u
.raw
[0] = readl(&dev
->usb
->setup0123
);
3163 u
.raw
[1] = readl(&dev
->usb
->setup4567
);
3165 cpu_to_le32s(&u
.raw
[0]);
3166 cpu_to_le32s(&u
.raw
[1]);
3168 if ((dev
->quirks
& PLX_PCIE
) && !dev
->bug7734_patched
)
3169 defect7374_workaround(dev
, u
.r
);
3173 #define w_value le16_to_cpu(u.r.wValue)
3174 #define w_index le16_to_cpu(u.r.wIndex)
3175 #define w_length le16_to_cpu(u.r.wLength)
3178 writel(BIT(SETUP_PACKET_INTERRUPT
), &dev
->regs
->irqstat0
);
3179 stat
^= BIT(SETUP_PACKET_INTERRUPT
);
3181 /* watch control traffic at the token level, and force
3182 * synchronization before letting the status stage happen.
3183 * FIXME ignore tokens we'll NAK, until driver responds.
3184 * that'll mean a lot less irqs for some drivers.
3186 ep
->is_in
= (u
.r
.bRequestType
& USB_DIR_IN
) != 0;
3188 scratch
= BIT(DATA_PACKET_TRANSMITTED_INTERRUPT
) |
3189 BIT(DATA_OUT_PING_TOKEN_INTERRUPT
) |
3190 BIT(DATA_IN_TOKEN_INTERRUPT
);
3191 stop_out_naking(ep
);
3193 scratch
= BIT(DATA_PACKET_RECEIVED_INTERRUPT
) |
3194 BIT(DATA_OUT_PING_TOKEN_INTERRUPT
) |
3195 BIT(DATA_IN_TOKEN_INTERRUPT
);
3196 writel(scratch
, &dev
->epregs
[0].ep_irqenb
);
3198 /* we made the hardware handle most lowlevel requests;
3199 * everything else goes uplevel to the gadget code.
3203 if (dev
->gadget
.speed
== USB_SPEED_SUPER
) {
3204 handle_stat0_irqs_superspeed(dev
, ep
, u
.r
);
3205 goto next_endpoints
;
3208 switch (u
.r
.bRequest
) {
3209 case USB_REQ_GET_STATUS
: {
3210 struct net2280_ep
*e
;
3213 /* hw handles device and interface status */
3214 if (u
.r
.bRequestType
!= (USB_DIR_IN
|USB_RECIP_ENDPOINT
))
3216 e
= get_ep_by_addr(dev
, w_index
);
3217 if (!e
|| w_length
> 2)
3220 if (readl(&e
->regs
->ep_rsp
) & BIT(SET_ENDPOINT_HALT
))
3221 status
= cpu_to_le32(1);
3223 status
= cpu_to_le32(0);
3225 /* don't bother with a request object! */
3226 writel(0, &dev
->epregs
[0].ep_irqenb
);
3227 set_fifo_bytecount(ep
, w_length
);
3228 writel((__force u32
)status
, &dev
->epregs
[0].ep_data
);
3230 ep_vdbg(dev
, "%s stat %02x\n", ep
->ep
.name
, status
);
3231 goto next_endpoints
;
3234 case USB_REQ_CLEAR_FEATURE
: {
3235 struct net2280_ep
*e
;
3237 /* hw handles device features */
3238 if (u
.r
.bRequestType
!= USB_RECIP_ENDPOINT
)
3240 if (w_value
!= USB_ENDPOINT_HALT
|| w_length
!= 0)
3242 e
= get_ep_by_addr(dev
, w_index
);
3246 ep_vdbg(dev
, "%s wedged, halt not cleared\n",
3249 ep_vdbg(dev
, "%s clear halt\n", e
->ep
.name
);
3251 if ((ep
->dev
->quirks
& PLX_PCIE
) &&
3252 !list_empty(&e
->queue
) && e
->td_dma
)
3256 goto next_endpoints
;
3259 case USB_REQ_SET_FEATURE
: {
3260 struct net2280_ep
*e
;
3262 /* hw handles device features */
3263 if (u
.r
.bRequestType
!= USB_RECIP_ENDPOINT
)
3265 if (w_value
!= USB_ENDPOINT_HALT
|| w_length
!= 0)
3267 e
= get_ep_by_addr(dev
, w_index
);
3270 if (e
->ep
.name
== ep0name
)
3273 if ((dev
->quirks
& PLX_PCIE
) && e
->dma
)
3276 ep_vdbg(dev
, "%s set halt\n", ep
->ep
.name
);
3277 goto next_endpoints
;
3282 ep_vdbg(dev
, "setup %02x.%02x v%04x i%04x l%04x "
3284 u
.r
.bRequestType
, u
.r
.bRequest
,
3285 w_value
, w_index
, w_length
,
3286 readl(&ep
->cfg
->ep_cfg
));
3288 spin_unlock(&dev
->lock
);
3289 tmp
= dev
->driver
->setup(&dev
->gadget
, &u
.r
);
3290 spin_lock(&dev
->lock
);
3293 /* stall ep0 on error */
3296 ep_vdbg(dev
, "req %02x.%02x protocol STALL; stat %d\n",
3297 u
.r
.bRequestType
, u
.r
.bRequest
, tmp
);
3298 dev
->protocol_stall
= 1;
3301 /* some in/out token irq should follow; maybe stall then.
3302 * driver must queue a request (even zlp) or halt ep0
3303 * before the host times out.
3312 if ((dev
->quirks
& PLX_PCIE
) && dev
->enhanced_mode
) {
3313 u32 mask
= (BIT(ENDPOINT_0_INTERRUPT
) |
3314 USB3380_IRQSTAT0_EP_INTR_MASK_IN
|
3315 USB3380_IRQSTAT0_EP_INTR_MASK_OUT
);
3318 usb338x_handle_ep_intr(dev
, stat
& mask
);
3322 /* endpoint data irq ? */
3323 scratch
= stat
& 0x7f;
3325 for (num
= 0; scratch
; num
++) {
3328 /* do this endpoint's FIFO and queue need tending? */
3330 if ((scratch
& t
) == 0)
3335 handle_ep_small(ep
);
3340 ep_dbg(dev
, "unhandled irqstat0 %08x\n", stat
);
3343 #define DMA_INTERRUPTS (BIT(DMA_D_INTERRUPT) | \
3344 BIT(DMA_C_INTERRUPT) | \
3345 BIT(DMA_B_INTERRUPT) | \
3346 BIT(DMA_A_INTERRUPT))
3347 #define PCI_ERROR_INTERRUPTS ( \
3348 BIT(PCI_MASTER_ABORT_RECEIVED_INTERRUPT) | \
3349 BIT(PCI_TARGET_ABORT_RECEIVED_INTERRUPT) | \
3350 BIT(PCI_RETRY_ABORT_INTERRUPT))
3352 static void handle_stat1_irqs(struct net2280
*dev
, u32 stat
)
3353 __releases(dev
->lock
)
3354 __acquires(dev
->lock
)
3356 struct net2280_ep
*ep
;
3357 u32 tmp
, num
, mask
, scratch
;
3359 /* after disconnect there's nothing else to do! */
3360 tmp
= BIT(VBUS_INTERRUPT
) | BIT(ROOT_PORT_RESET_INTERRUPT
);
3361 mask
= BIT(SUPER_SPEED
) | BIT(HIGH_SPEED
) | BIT(FULL_SPEED
);
3363 /* VBUS disconnect is indicated by VBUS_PIN and VBUS_INTERRUPT set.
3364 * Root Port Reset is indicated by ROOT_PORT_RESET_INTERRUPT set and
3365 * both HIGH_SPEED and FULL_SPEED clear (as ROOT_PORT_RESET_INTERRUPT
3366 * only indicates a change in the reset state).
3370 bool disconnect
= false;
3373 * Ignore disconnects and resets if the speed hasn't been set.
3374 * VBUS can bounce and there's always an initial reset.
3376 writel(tmp
, &dev
->regs
->irqstat1
);
3377 if (dev
->gadget
.speed
!= USB_SPEED_UNKNOWN
) {
3378 if ((stat
& BIT(VBUS_INTERRUPT
)) &&
3379 (readl(&dev
->usb
->usbctl
) &
3380 BIT(VBUS_PIN
)) == 0) {
3382 ep_dbg(dev
, "disconnect %s\n",
3383 dev
->driver
->driver
.name
);
3384 } else if ((stat
& BIT(ROOT_PORT_RESET_INTERRUPT
)) &&
3385 (readl(&dev
->usb
->usbstat
) & mask
)
3388 ep_dbg(dev
, "reset %s\n",
3389 dev
->driver
->driver
.name
);
3392 if (disconnect
|| reset
) {
3393 stop_activity(dev
, dev
->driver
);
3395 spin_unlock(&dev
->lock
);
3397 usb_gadget_udc_reset
3398 (&dev
->gadget
, dev
->driver
);
3400 (dev
->driver
->disconnect
)
3402 spin_lock(&dev
->lock
);
3408 /* vBUS can bounce ... one of many reasons to ignore the
3409 * notion of hotplug events on bus connect/disconnect!
3415 /* NOTE: chip stays in PCI D0 state for now, but it could
3416 * enter D1 to save more power
3418 tmp
= BIT(SUSPEND_REQUEST_CHANGE_INTERRUPT
);
3420 writel(tmp
, &dev
->regs
->irqstat1
);
3421 spin_unlock(&dev
->lock
);
3422 if (stat
& BIT(SUSPEND_REQUEST_INTERRUPT
)) {
3423 if (dev
->driver
->suspend
)
3424 dev
->driver
->suspend(&dev
->gadget
);
3425 if (!enable_suspend
)
3426 stat
&= ~BIT(SUSPEND_REQUEST_INTERRUPT
);
3428 if (dev
->driver
->resume
)
3429 dev
->driver
->resume(&dev
->gadget
);
3430 /* at high speed, note erratum 0133 */
3432 spin_lock(&dev
->lock
);
3436 /* clear any other status/irqs */
3438 writel(stat
, &dev
->regs
->irqstat1
);
3440 /* some status we can just ignore */
3441 if (dev
->quirks
& PLX_2280
)
3442 stat
&= ~(BIT(CONTROL_STATUS_INTERRUPT
) |
3443 BIT(SUSPEND_REQUEST_INTERRUPT
) |
3444 BIT(RESUME_INTERRUPT
) |
3445 BIT(SOF_INTERRUPT
));
3447 stat
&= ~(BIT(CONTROL_STATUS_INTERRUPT
) |
3448 BIT(RESUME_INTERRUPT
) |
3449 BIT(SOF_DOWN_INTERRUPT
) |
3450 BIT(SOF_INTERRUPT
));
3454 /* ep_dbg(dev, "irqstat1 %08x\n", stat);*/
3456 /* DMA status, for ep-{a,b,c,d} */
3457 scratch
= stat
& DMA_INTERRUPTS
;
3458 stat
&= ~DMA_INTERRUPTS
;
3460 for (num
= 0; scratch
; num
++) {
3461 struct net2280_dma_regs __iomem
*dma
;
3464 if ((tmp
& scratch
) == 0)
3468 ep
= &dev
->ep
[num
+ 1];
3474 /* clear ep's dma status */
3475 tmp
= readl(&dma
->dmastat
);
3476 writel(tmp
, &dma
->dmastat
);
3479 if (dev
->quirks
& PLX_PCIE
) {
3480 u32 r_dmacount
= readl(&dma
->dmacount
);
3481 if (!ep
->is_in
&& (r_dmacount
& 0x00FFFFFF) &&
3482 (tmp
& BIT(DMA_TRANSACTION_DONE_INTERRUPT
)))
3486 if (!(tmp
& BIT(DMA_TRANSACTION_DONE_INTERRUPT
))) {
3487 ep_dbg(ep
->dev
, "%s no xact done? %08x\n",
3493 /* OUT transfers terminate when the data from the
3494 * host is in our memory. Process whatever's done.
3495 * On this path, we know transfer's last packet wasn't
3496 * less than req->length. NAK_OUT_PACKETS may be set,
3497 * or the FIFO may already be holding new packets.
3499 * IN transfers can linger in the FIFO for a very
3500 * long time ... we ignore that for now, accounting
3501 * precisely (like PIO does) needs per-packet irqs
3503 scan_dma_completions(ep
);
3505 /* disable dma on inactive queues; else maybe restart */
3506 if (!list_empty(&ep
->queue
)) {
3507 tmp
= readl(&dma
->dmactl
);
3513 /* NOTE: there are other PCI errors we might usefully notice.
3514 * if they appear very often, here's where to try recovering.
3516 if (stat
& PCI_ERROR_INTERRUPTS
) {
3517 ep_err(dev
, "pci dma error; stat %08x\n", stat
);
3518 stat
&= ~PCI_ERROR_INTERRUPTS
;
3519 /* these are fatal errors, but "maybe" they won't
3522 stop_activity(dev
, dev
->driver
);
3528 ep_dbg(dev
, "unhandled irqstat1 %08x\n", stat
);
3531 static irqreturn_t
net2280_irq(int irq
, void *_dev
)
3533 struct net2280
*dev
= _dev
;
3535 /* shared interrupt, not ours */
3536 if ((dev
->quirks
& PLX_LEGACY
) &&
3537 (!(readl(&dev
->regs
->irqstat0
) & BIT(INTA_ASSERTED
))))
3540 spin_lock(&dev
->lock
);
3542 /* handle disconnect, dma, and more */
3543 handle_stat1_irqs(dev
, readl(&dev
->regs
->irqstat1
));
3545 /* control requests and PIO */
3546 handle_stat0_irqs(dev
, readl(&dev
->regs
->irqstat0
));
3548 if (dev
->quirks
& PLX_PCIE
) {
3549 /* re-enable interrupt to trigger any possible new interrupt */
3550 u32 pciirqenb1
= readl(&dev
->regs
->pciirqenb1
);
3551 writel(pciirqenb1
& 0x7FFFFFFF, &dev
->regs
->pciirqenb1
);
3552 writel(pciirqenb1
, &dev
->regs
->pciirqenb1
);
3555 spin_unlock(&dev
->lock
);
3560 /*-------------------------------------------------------------------------*/
3562 static void gadget_release(struct device
*_dev
)
3564 struct net2280
*dev
= dev_get_drvdata(_dev
);
3569 /* tear down the binding between this driver and the pci device */
3571 static void net2280_remove(struct pci_dev
*pdev
)
3573 struct net2280
*dev
= pci_get_drvdata(pdev
);
3575 usb_del_gadget_udc(&dev
->gadget
);
3577 BUG_ON(dev
->driver
);
3579 /* then clean up the resources we allocated during probe() */
3580 if (dev
->requests
) {
3582 for (i
= 1; i
< 5; i
++) {
3583 if (!dev
->ep
[i
].dummy
)
3585 dma_pool_free(dev
->requests
, dev
->ep
[i
].dummy
,
3588 dma_pool_destroy(dev
->requests
);
3591 free_irq(pdev
->irq
, dev
);
3592 if (dev
->quirks
& PLX_PCIE
)
3593 pci_disable_msi(pdev
);
3595 net2280_led_shutdown(dev
);
3599 release_mem_region(pci_resource_start(pdev
, 0),
3600 pci_resource_len(pdev
, 0));
3602 pci_disable_device(pdev
);
3603 device_remove_file(&pdev
->dev
, &dev_attr_registers
);
3605 ep_info(dev
, "unbind\n");
3608 /* wrap this driver around the specified device, but
3609 * don't respond over USB until a gadget driver binds to us.
3612 static int net2280_probe(struct pci_dev
*pdev
, const struct pci_device_id
*id
)
3614 struct net2280
*dev
;
3615 unsigned long resource
, len
;
3616 void __iomem
*base
= NULL
;
3619 /* alloc, and start init */
3620 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
3626 pci_set_drvdata(pdev
, dev
);
3627 spin_lock_init(&dev
->lock
);
3628 dev
->quirks
= id
->driver_data
;
3630 dev
->gadget
.ops
= &net2280_ops
;
3631 dev
->gadget
.max_speed
= (dev
->quirks
& PLX_SUPERSPEED
) ?
3632 USB_SPEED_SUPER
: USB_SPEED_HIGH
;
3634 /* the "gadget" abstracts/virtualizes the controller */
3635 dev
->gadget
.name
= driver_name
;
3637 /* now all the pci goodies ... */
3638 if (pci_enable_device(pdev
) < 0) {
3644 /* BAR 0 holds all the registers
3645 * BAR 1 is 8051 memory; unused here (note erratum 0103)
3646 * BAR 2 is fifo memory; unused here
3648 resource
= pci_resource_start(pdev
, 0);
3649 len
= pci_resource_len(pdev
, 0);
3650 if (!request_mem_region(resource
, len
, driver_name
)) {
3651 ep_dbg(dev
, "controller already in use\n");
3657 /* FIXME provide firmware download interface to put
3658 * 8051 code into the chip, e.g. to turn on PCI PM.
3661 base
= ioremap(resource
, len
);
3663 ep_dbg(dev
, "can't map memory\n");
3667 dev
->regs
= (struct net2280_regs __iomem
*) base
;
3668 dev
->usb
= (struct net2280_usb_regs __iomem
*) (base
+ 0x0080);
3669 dev
->pci
= (struct net2280_pci_regs __iomem
*) (base
+ 0x0100);
3670 dev
->dma
= (struct net2280_dma_regs __iomem
*) (base
+ 0x0180);
3671 dev
->dep
= (struct net2280_dep_regs __iomem
*) (base
+ 0x0200);
3672 dev
->epregs
= (struct net2280_ep_regs __iomem
*) (base
+ 0x0300);
3674 if (dev
->quirks
& PLX_PCIE
) {
3677 dev
->usb_ext
= (struct usb338x_usb_ext_regs __iomem
*)
3679 dev
->llregs
= (struct usb338x_ll_regs __iomem
*)
3681 dev
->plregs
= (struct usb338x_pl_regs __iomem
*)
3683 usbstat
= readl(&dev
->usb
->usbstat
);
3684 dev
->enhanced_mode
= !!(usbstat
& BIT(11));
3685 dev
->n_ep
= (dev
->enhanced_mode
) ? 9 : 5;
3686 /* put into initial config, link up all endpoints */
3687 fsmvalue
= get_idx_reg(dev
->regs
, SCRATCH
) &
3688 (0xf << DEFECT7374_FSM_FIELD
);
3689 /* See if firmware needs to set up for workaround: */
3690 if (fsmvalue
== DEFECT7374_FSM_SS_CONTROL_READ
) {
3691 dev
->bug7734_patched
= 1;
3692 writel(0, &dev
->usb
->usbctl
);
3694 dev
->bug7734_patched
= 0;
3696 dev
->enhanced_mode
= 0;
3698 /* put into initial config, link up all endpoints */
3699 writel(0, &dev
->usb
->usbctl
);
3705 /* irq setup after old hardware is cleaned up */
3707 ep_err(dev
, "No IRQ. Check PCI setup!\n");
3712 if (dev
->quirks
& PLX_PCIE
)
3713 if (pci_enable_msi(pdev
))
3714 ep_err(dev
, "Failed to enable MSI mode\n");
3716 if (request_irq(pdev
->irq
, net2280_irq
, IRQF_SHARED
,
3717 driver_name
, dev
)) {
3718 ep_err(dev
, "request interrupt %d failed\n", pdev
->irq
);
3725 /* NOTE: we know only the 32 LSBs of dma addresses may be nonzero */
3726 dev
->requests
= dma_pool_create("requests", &pdev
->dev
,
3727 sizeof(struct net2280_dma
),
3728 0 /* no alignment requirements */,
3729 0 /* or page-crossing issues */);
3730 if (!dev
->requests
) {
3731 ep_dbg(dev
, "can't get request pool\n");
3735 for (i
= 1; i
< 5; i
++) {
3736 struct net2280_dma
*td
;
3738 td
= dma_pool_alloc(dev
->requests
, GFP_KERNEL
,
3739 &dev
->ep
[i
].td_dma
);
3741 ep_dbg(dev
, "can't get dummy %d\n", i
);
3745 td
->dmacount
= 0; /* not VALID */
3746 td
->dmadesc
= td
->dmaaddr
;
3747 dev
->ep
[i
].dummy
= td
;
3750 /* enable lower-overhead pci memory bursts during DMA */
3751 if (dev
->quirks
& PLX_LEGACY
)
3752 writel(BIT(DMA_MEMORY_WRITE_AND_INVALIDATE_ENABLE
) |
3754 * 256 write retries may not be enough...
3755 BIT(PCI_RETRY_ABORT_ENABLE) |
3757 BIT(DMA_READ_MULTIPLE_ENABLE
) |
3758 BIT(DMA_READ_LINE_ENABLE
),
3759 &dev
->pci
->pcimstctl
);
3760 /* erratum 0115 shouldn't appear: Linux inits PCI_LATENCY_TIMER */
3761 pci_set_master(pdev
);
3762 pci_try_set_mwi(pdev
);
3764 /* ... also flushes any posted pci writes */
3765 dev
->chiprev
= get_idx_reg(dev
->regs
, REG_CHIPREV
) & 0xffff;
3768 ep_info(dev
, "%s\n", driver_desc
);
3769 ep_info(dev
, "irq %d, pci mem %p, chip rev %04x\n",
3770 pdev
->irq
, base
, dev
->chiprev
);
3771 ep_info(dev
, "version: " DRIVER_VERSION
"; %s\n",
3772 dev
->enhanced_mode
? "enhanced mode" : "legacy mode");
3773 retval
= device_create_file(&pdev
->dev
, &dev_attr_registers
);
3777 retval
= usb_add_gadget_udc_release(&pdev
->dev
, &dev
->gadget
,
3785 net2280_remove(pdev
);
3789 /* make sure the board is quiescent; otherwise it will continue
3790 * generating IRQs across the upcoming reboot.
3793 static void net2280_shutdown(struct pci_dev
*pdev
)
3795 struct net2280
*dev
= pci_get_drvdata(pdev
);
3798 writel(0, &dev
->regs
->pciirqenb0
);
3799 writel(0, &dev
->regs
->pciirqenb1
);
3801 /* disable the pullup so the host will think we're gone */
3802 writel(0, &dev
->usb
->usbctl
);
3807 /*-------------------------------------------------------------------------*/
3809 static const struct pci_device_id pci_ids
[] = { {
3810 .class = PCI_CLASS_SERIAL_USB_DEVICE
,
3812 .vendor
= PCI_VENDOR_ID_PLX_LEGACY
,
3814 .subvendor
= PCI_ANY_ID
,
3815 .subdevice
= PCI_ANY_ID
,
3816 .driver_data
= PLX_LEGACY
| PLX_2280
,
3818 .class = PCI_CLASS_SERIAL_USB_DEVICE
,
3820 .vendor
= PCI_VENDOR_ID_PLX_LEGACY
,
3822 .subvendor
= PCI_ANY_ID
,
3823 .subdevice
= PCI_ANY_ID
,
3824 .driver_data
= PLX_LEGACY
,
3827 .class = PCI_CLASS_SERIAL_USB_DEVICE
,
3829 .vendor
= PCI_VENDOR_ID_PLX
,
3831 .subvendor
= PCI_ANY_ID
,
3832 .subdevice
= PCI_ANY_ID
,
3833 .driver_data
= PLX_PCIE
,
3836 .class = ((PCI_CLASS_SERIAL_USB
<< 8) | 0xfe),
3838 .vendor
= PCI_VENDOR_ID_PLX
,
3840 .subvendor
= PCI_ANY_ID
,
3841 .subdevice
= PCI_ANY_ID
,
3842 .driver_data
= PLX_PCIE
| PLX_SUPERSPEED
,
3845 .class = PCI_CLASS_SERIAL_USB_DEVICE
,
3847 .vendor
= PCI_VENDOR_ID_PLX
,
3849 .subvendor
= PCI_ANY_ID
,
3850 .subdevice
= PCI_ANY_ID
,
3851 .driver_data
= PLX_PCIE
| PLX_SUPERSPEED
,
3853 { /* end: all zeroes */ }
3855 MODULE_DEVICE_TABLE(pci
, pci_ids
);
3857 /* pci driver glue; this is a "new style" PCI driver module */
3858 static struct pci_driver net2280_pci_driver
= {
3859 .name
= driver_name
,
3860 .id_table
= pci_ids
,
3862 .probe
= net2280_probe
,
3863 .remove
= net2280_remove
,
3864 .shutdown
= net2280_shutdown
,
3866 /* FIXME add power management support */
3869 module_pci_driver(net2280_pci_driver
);
3871 MODULE_DESCRIPTION(DRIVER_DESC
);
3872 MODULE_AUTHOR("David Brownell");
3873 MODULE_LICENSE("GPL");