2 * Driver for the PLX NET2280 USB device controller.
3 * Specs and errata are available from <http://www.plxtech.com>.
5 * PLX Technology Inc. (formerly NetChip Technology) supported the
6 * development of this driver.
9 * CODE STATUS HIGHLIGHTS
11 * This driver should work well with most "gadget" drivers, including
12 * the Mass Storage, Serial, and Ethernet/RNDIS gadget drivers
13 * as well as Gadget Zero and Gadgetfs.
15 * DMA is enabled by default.
17 * MSI is enabled by default. The legacy IRQ is used if MSI couldn't
20 * Note that almost all the errata workarounds here are only needed for
21 * rev1 chips. Rev1a silicon (0110) fixes almost all of them.
25 * Copyright (C) 2003 David Brownell
26 * Copyright (C) 2003-2005 PLX Technology, Inc.
27 * Copyright (C) 2014 Ricardo Ribalda - Qtechnology/AS
29 * Modified Seth Levy 2005 PLX Technology, Inc. to provide compatibility
32 * Modified Ricardo Ribalda Qtechnology AS to provide compatibility
33 * with usb 338x chip. Based on PLX driver
35 * This program is free software; you can redistribute it and/or modify
36 * it under the terms of the GNU General Public License as published by
37 * the Free Software Foundation; either version 2 of the License, or
38 * (at your option) any later version.
41 #include <linux/module.h>
42 #include <linux/pci.h>
43 #include <linux/dma-mapping.h>
44 #include <linux/kernel.h>
45 #include <linux/delay.h>
46 #include <linux/ioport.h>
47 #include <linux/slab.h>
48 #include <linux/errno.h>
49 #include <linux/init.h>
50 #include <linux/timer.h>
51 #include <linux/list.h>
52 #include <linux/interrupt.h>
53 #include <linux/moduleparam.h>
54 #include <linux/device.h>
55 #include <linux/usb/ch9.h>
56 #include <linux/usb/gadget.h>
57 #include <linux/prefetch.h>
60 #include <asm/byteorder.h>
62 #include <asm/unaligned.h>
64 #define DRIVER_DESC "PLX NET228x/USB338x USB Peripheral Controller"
65 #define DRIVER_VERSION "2005 Sept 27/v3.0"
67 #define EP_DONTUSE 13 /* nonzero */
69 #define USE_RDK_LEDS /* GPIO pins control three LEDs */
72 static const char driver_name
[] = "net2280";
73 static const char driver_desc
[] = DRIVER_DESC
;
75 static const u32 ep_bit
[9] = { 0, 17, 2, 19, 4, 1, 18, 3, 20 };
76 static const char ep0name
[] = "ep0";
78 #define EP_INFO(_name, _caps) \
86 const struct usb_ep_caps caps
;
87 } ep_info_dft
[] = { /* Default endpoint configuration */
89 USB_EP_CAPS(USB_EP_CAPS_TYPE_CONTROL
, 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
)),
103 USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL
, USB_EP_CAPS_DIR_ALL
)),
105 USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL
, USB_EP_CAPS_DIR_ALL
)),
106 }, ep_info_adv
[] = { /* Endpoints for usb3380 advance mode */
108 USB_EP_CAPS(USB_EP_CAPS_TYPE_CONTROL
, USB_EP_CAPS_DIR_ALL
)),
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_IN
)),
116 USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL
, USB_EP_CAPS_DIR_OUT
)),
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
)),
122 USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL
, USB_EP_CAPS_DIR_OUT
)),
124 USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL
, USB_EP_CAPS_DIR_IN
)),
129 /* mode 0 == ep-{a,b,c,d} 1K fifo each
130 * mode 1 == ep-{a,b} 2K fifo each, ep-{c,d} unavailable
131 * mode 2 == ep-a 2K fifo, ep-{b,c} 1K each, ep-d unavailable
133 static ushort fifo_mode
;
135 /* "modprobe net2280 fifo_mode=1" etc */
136 module_param(fifo_mode
, ushort
, 0644);
138 /* enable_suspend -- When enabled, the driver will respond to
139 * USB suspend requests by powering down the NET2280. Otherwise,
140 * USB suspend requests will be ignored. This is acceptable for
141 * self-powered devices
143 static bool enable_suspend
;
145 /* "modprobe net2280 enable_suspend=1" etc */
146 module_param(enable_suspend
, bool, 0444);
148 #define DIR_STRING(bAddress) (((bAddress) & USB_DIR_IN) ? "in" : "out")
150 static char *type_string(u8 bmAttributes
)
152 switch ((bmAttributes
) & USB_ENDPOINT_XFERTYPE_MASK
) {
153 case USB_ENDPOINT_XFER_BULK
: return "bulk";
154 case USB_ENDPOINT_XFER_ISOC
: return "iso";
155 case USB_ENDPOINT_XFER_INT
: return "intr";
162 #define valid_bit cpu_to_le32(BIT(VALID_BIT))
163 #define dma_done_ie cpu_to_le32(BIT(DMA_DONE_INTERRUPT_ENABLE))
165 static void ep_clear_seqnum(struct net2280_ep
*ep
);
166 static void stop_activity(struct net2280
*dev
,
167 struct usb_gadget_driver
*driver
);
168 static void ep0_start(struct net2280
*dev
);
170 /*-------------------------------------------------------------------------*/
171 static inline void enable_pciirqenb(struct net2280_ep
*ep
)
173 u32 tmp
= readl(&ep
->dev
->regs
->pciirqenb0
);
175 if (ep
->dev
->quirks
& PLX_LEGACY
)
178 tmp
|= BIT(ep_bit
[ep
->num
]);
179 writel(tmp
, &ep
->dev
->regs
->pciirqenb0
);
185 net2280_enable(struct usb_ep
*_ep
, const struct usb_endpoint_descriptor
*desc
)
188 struct net2280_ep
*ep
;
193 static const u32 ep_key
[9] = { 1, 0, 1, 0, 1, 1, 0, 1, 0 };
196 ep
= container_of(_ep
, struct net2280_ep
, ep
);
197 if (!_ep
|| !desc
|| ep
->desc
|| _ep
->name
== ep0name
||
198 desc
->bDescriptorType
!= USB_DT_ENDPOINT
) {
199 pr_err("%s: failed at line=%d\n", __func__
, __LINE__
);
203 if (!dev
->driver
|| dev
->gadget
.speed
== USB_SPEED_UNKNOWN
) {
208 /* erratum 0119 workaround ties up an endpoint number */
209 if ((desc
->bEndpointAddress
& 0x0f) == EP_DONTUSE
) {
214 if (dev
->quirks
& PLX_PCIE
) {
215 if ((desc
->bEndpointAddress
& 0x0f) >= 0x0c) {
219 ep
->is_in
= !!usb_endpoint_dir_in(desc
);
220 if (dev
->enhanced_mode
&& ep
->is_in
&& ep_key
[ep
->num
]) {
226 /* sanity check ep-e/ep-f since their fifos are small */
227 max
= usb_endpoint_maxp(desc
) & 0x1fff;
228 if (ep
->num
> 4 && max
> 64 && (dev
->quirks
& PLX_LEGACY
)) {
233 spin_lock_irqsave(&dev
->lock
, flags
);
234 _ep
->maxpacket
= max
& 0x7ff;
237 /* ep_reset() has already been called */
240 ep
->out_overflow
= 0;
242 /* set speed-dependent max packet; may kick in high bandwidth */
243 set_max_speed(ep
, max
);
245 /* set type, direction, address; reset fifo counters */
246 writel(BIT(FIFO_FLUSH
), &ep
->regs
->ep_stat
);
248 if ((dev
->quirks
& PLX_PCIE
) && dev
->enhanced_mode
) {
249 tmp
= readl(&ep
->cfg
->ep_cfg
);
250 /* If USB ep number doesn't match hardware ep number */
251 if ((tmp
& 0xf) != usb_endpoint_num(desc
)) {
253 spin_unlock_irqrestore(&dev
->lock
, flags
);
257 tmp
&= ~USB3380_EP_CFG_MASK_IN
;
259 tmp
&= ~USB3380_EP_CFG_MASK_OUT
;
261 type
= (desc
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
);
262 if (type
== USB_ENDPOINT_XFER_INT
) {
263 /* erratum 0105 workaround prevents hs NYET */
264 if (dev
->chiprev
== 0100 &&
265 dev
->gadget
.speed
== USB_SPEED_HIGH
&&
266 !(desc
->bEndpointAddress
& USB_DIR_IN
))
267 writel(BIT(CLEAR_NAK_OUT_PACKETS_MODE
),
269 } else if (type
== USB_ENDPOINT_XFER_BULK
) {
270 /* catch some particularly blatant driver bugs */
271 if ((dev
->gadget
.speed
== USB_SPEED_SUPER
&& max
!= 1024) ||
272 (dev
->gadget
.speed
== USB_SPEED_HIGH
&& max
!= 512) ||
273 (dev
->gadget
.speed
== USB_SPEED_FULL
&& max
> 64)) {
274 spin_unlock_irqrestore(&dev
->lock
, flags
);
279 ep
->is_iso
= (type
== USB_ENDPOINT_XFER_ISOC
);
280 /* Enable this endpoint */
281 if (dev
->quirks
& PLX_LEGACY
) {
282 tmp
|= type
<< ENDPOINT_TYPE
;
283 tmp
|= desc
->bEndpointAddress
;
284 /* default full fifo lines */
285 tmp
|= (4 << ENDPOINT_BYTE_COUNT
);
286 tmp
|= BIT(ENDPOINT_ENABLE
);
287 ep
->is_in
= (tmp
& USB_DIR_IN
) != 0;
289 /* In Legacy mode, only OUT endpoints are used */
290 if (dev
->enhanced_mode
&& ep
->is_in
) {
291 tmp
|= type
<< IN_ENDPOINT_TYPE
;
292 tmp
|= BIT(IN_ENDPOINT_ENABLE
);
294 tmp
|= type
<< OUT_ENDPOINT_TYPE
;
295 tmp
|= BIT(OUT_ENDPOINT_ENABLE
);
296 tmp
|= (ep
->is_in
<< ENDPOINT_DIRECTION
);
299 tmp
|= (4 << ENDPOINT_BYTE_COUNT
);
300 if (!dev
->enhanced_mode
)
301 tmp
|= usb_endpoint_num(desc
);
302 tmp
|= (ep
->ep
.maxburst
<< MAX_BURST_SIZE
);
305 /* Make sure all the registers are written before ep_rsp*/
308 /* for OUT transfers, block the rx fifo until a read is posted */
310 writel(BIT(SET_NAK_OUT_PACKETS
), &ep
->regs
->ep_rsp
);
311 else if (!(dev
->quirks
& PLX_2280
)) {
312 /* Added for 2282, Don't use nak packets on an in endpoint,
313 * this was ignored on 2280
315 writel(BIT(CLEAR_NAK_OUT_PACKETS
) |
316 BIT(CLEAR_NAK_OUT_PACKETS_MODE
), &ep
->regs
->ep_rsp
);
319 if (dev
->quirks
& PLX_PCIE
)
321 writel(tmp
, &ep
->cfg
->ep_cfg
);
324 if (!ep
->dma
) { /* pio, per-packet */
325 enable_pciirqenb(ep
);
327 tmp
= BIT(DATA_PACKET_RECEIVED_INTERRUPT_ENABLE
) |
328 BIT(DATA_PACKET_TRANSMITTED_INTERRUPT_ENABLE
);
329 if (dev
->quirks
& PLX_2280
)
330 tmp
|= readl(&ep
->regs
->ep_irqenb
);
331 writel(tmp
, &ep
->regs
->ep_irqenb
);
332 } else { /* dma, per-request */
333 tmp
= BIT((8 + ep
->num
)); /* completion */
334 tmp
|= readl(&dev
->regs
->pciirqenb1
);
335 writel(tmp
, &dev
->regs
->pciirqenb1
);
337 /* for short OUT transfers, dma completions can't
338 * advance the queue; do it pio-style, by hand.
339 * NOTE erratum 0112 workaround #2
341 if ((desc
->bEndpointAddress
& USB_DIR_IN
) == 0) {
342 tmp
= BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT_ENABLE
);
343 writel(tmp
, &ep
->regs
->ep_irqenb
);
345 enable_pciirqenb(ep
);
349 tmp
= desc
->bEndpointAddress
;
350 ep_dbg(dev
, "enabled %s (ep%d%s-%s) %s max %04x\n",
351 _ep
->name
, tmp
& 0x0f, DIR_STRING(tmp
),
352 type_string(desc
->bmAttributes
),
353 ep
->dma
? "dma" : "pio", max
);
355 /* pci writes may still be posted */
356 spin_unlock_irqrestore(&dev
->lock
, flags
);
360 dev_err(&ep
->dev
->pdev
->dev
, "%s: error=%d\n", __func__
, ret
);
364 static int handshake(u32 __iomem
*ptr
, u32 mask
, u32 done
, int usec
)
370 if (result
== ~(u32
)0) /* "device unplugged" */
381 static const struct usb_ep_ops net2280_ep_ops
;
383 static void ep_reset_228x(struct net2280_regs __iomem
*regs
,
384 struct net2280_ep
*ep
)
389 INIT_LIST_HEAD(&ep
->queue
);
391 usb_ep_set_maxpacket_limit(&ep
->ep
, ~0);
392 ep
->ep
.ops
= &net2280_ep_ops
;
394 /* disable the dma, irqs, endpoint... */
396 writel(0, &ep
->dma
->dmactl
);
397 writel(BIT(DMA_SCATTER_GATHER_DONE_INTERRUPT
) |
398 BIT(DMA_TRANSACTION_DONE_INTERRUPT
) |
402 tmp
= readl(®s
->pciirqenb0
);
403 tmp
&= ~BIT(ep
->num
);
404 writel(tmp
, ®s
->pciirqenb0
);
406 tmp
= readl(®s
->pciirqenb1
);
407 tmp
&= ~BIT((8 + ep
->num
)); /* completion */
408 writel(tmp
, ®s
->pciirqenb1
);
410 writel(0, &ep
->regs
->ep_irqenb
);
412 /* init to our chosen defaults, notably so that we NAK OUT
413 * packets until the driver queues a read (+note erratum 0112)
415 if (!ep
->is_in
|| (ep
->dev
->quirks
& PLX_2280
)) {
416 tmp
= BIT(SET_NAK_OUT_PACKETS_MODE
) |
417 BIT(SET_NAK_OUT_PACKETS
) |
418 BIT(CLEAR_EP_HIDE_STATUS_PHASE
) |
419 BIT(CLEAR_INTERRUPT_MODE
);
422 tmp
= BIT(CLEAR_NAK_OUT_PACKETS_MODE
) |
423 BIT(CLEAR_NAK_OUT_PACKETS
) |
424 BIT(CLEAR_EP_HIDE_STATUS_PHASE
) |
425 BIT(CLEAR_INTERRUPT_MODE
);
429 tmp
|= BIT(CLEAR_ENDPOINT_TOGGLE
) |
430 BIT(CLEAR_ENDPOINT_HALT
);
432 writel(tmp
, &ep
->regs
->ep_rsp
);
434 /* scrub most status bits, and flush any fifo state */
435 if (ep
->dev
->quirks
& PLX_2280
)
436 tmp
= BIT(FIFO_OVERFLOW
) |
441 writel(tmp
| BIT(TIMEOUT
) |
442 BIT(USB_STALL_SENT
) |
443 BIT(USB_IN_NAK_SENT
) |
444 BIT(USB_IN_ACK_RCVD
) |
445 BIT(USB_OUT_PING_NAK_SENT
) |
446 BIT(USB_OUT_ACK_SENT
) |
448 BIT(SHORT_PACKET_OUT_DONE_INTERRUPT
) |
449 BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT
) |
450 BIT(DATA_PACKET_RECEIVED_INTERRUPT
) |
451 BIT(DATA_PACKET_TRANSMITTED_INTERRUPT
) |
452 BIT(DATA_OUT_PING_TOKEN_INTERRUPT
) |
453 BIT(DATA_IN_TOKEN_INTERRUPT
),
456 /* fifo size is handled separately */
459 static void ep_reset_338x(struct net2280_regs __iomem
*regs
,
460 struct net2280_ep
*ep
)
465 INIT_LIST_HEAD(&ep
->queue
);
467 usb_ep_set_maxpacket_limit(&ep
->ep
, ~0);
468 ep
->ep
.ops
= &net2280_ep_ops
;
470 /* disable the dma, irqs, endpoint... */
472 writel(0, &ep
->dma
->dmactl
);
473 writel(BIT(DMA_ABORT_DONE_INTERRUPT
) |
474 BIT(DMA_PAUSE_DONE_INTERRUPT
) |
475 BIT(DMA_SCATTER_GATHER_DONE_INTERRUPT
) |
476 BIT(DMA_TRANSACTION_DONE_INTERRUPT
),
477 /* | BIT(DMA_ABORT), */
480 dmastat
= readl(&ep
->dma
->dmastat
);
481 if (dmastat
== 0x5002) {
482 ep_warn(ep
->dev
, "The dmastat return = %x!!\n",
484 writel(0x5a, &ep
->dma
->dmastat
);
487 tmp
= readl(®s
->pciirqenb0
);
488 tmp
&= ~BIT(ep_bit
[ep
->num
]);
489 writel(tmp
, ®s
->pciirqenb0
);
492 tmp
= readl(®s
->pciirqenb1
);
493 tmp
&= ~BIT((8 + ep
->num
)); /* completion */
494 writel(tmp
, ®s
->pciirqenb1
);
497 writel(0, &ep
->regs
->ep_irqenb
);
499 writel(BIT(SHORT_PACKET_OUT_DONE_INTERRUPT
) |
500 BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT
) |
502 BIT(DATA_PACKET_RECEIVED_INTERRUPT
) |
503 BIT(DATA_PACKET_TRANSMITTED_INTERRUPT
) |
504 BIT(DATA_OUT_PING_TOKEN_INTERRUPT
) |
505 BIT(DATA_IN_TOKEN_INTERRUPT
), &ep
->regs
->ep_stat
);
507 tmp
= readl(&ep
->cfg
->ep_cfg
);
509 tmp
&= ~USB3380_EP_CFG_MASK_IN
;
511 tmp
&= ~USB3380_EP_CFG_MASK_OUT
;
512 writel(tmp
, &ep
->cfg
->ep_cfg
);
515 static void nuke(struct net2280_ep
*);
517 static int net2280_disable(struct usb_ep
*_ep
)
519 struct net2280_ep
*ep
;
522 ep
= container_of(_ep
, struct net2280_ep
, ep
);
523 if (!_ep
|| !ep
->desc
|| _ep
->name
== ep0name
) {
524 pr_err("%s: Invalid ep=%p or ep->desc\n", __func__
, _ep
);
527 spin_lock_irqsave(&ep
->dev
->lock
, flags
);
530 if (ep
->dev
->quirks
& PLX_PCIE
)
531 ep_reset_338x(ep
->dev
->regs
, ep
);
533 ep_reset_228x(ep
->dev
->regs
, ep
);
535 ep_vdbg(ep
->dev
, "disabled %s %s\n",
536 ep
->dma
? "dma" : "pio", _ep
->name
);
538 /* synch memory views with the device */
539 (void)readl(&ep
->cfg
->ep_cfg
);
541 if (!ep
->dma
&& ep
->num
>= 1 && ep
->num
<= 4)
542 ep
->dma
= &ep
->dev
->dma
[ep
->num
- 1];
544 spin_unlock_irqrestore(&ep
->dev
->lock
, flags
);
548 /*-------------------------------------------------------------------------*/
550 static struct usb_request
551 *net2280_alloc_request(struct usb_ep
*_ep
, gfp_t gfp_flags
)
553 struct net2280_ep
*ep
;
554 struct net2280_request
*req
;
557 pr_err("%s: Invalid ep\n", __func__
);
560 ep
= container_of(_ep
, struct net2280_ep
, ep
);
562 req
= kzalloc(sizeof(*req
), gfp_flags
);
566 INIT_LIST_HEAD(&req
->queue
);
568 /* this dma descriptor may be swapped with the previous dummy */
570 struct net2280_dma
*td
;
572 td
= pci_pool_alloc(ep
->dev
->requests
, gfp_flags
,
578 td
->dmacount
= 0; /* not VALID */
579 td
->dmadesc
= td
->dmaaddr
;
585 static void net2280_free_request(struct usb_ep
*_ep
, struct usb_request
*_req
)
587 struct net2280_ep
*ep
;
588 struct net2280_request
*req
;
590 ep
= container_of(_ep
, struct net2280_ep
, ep
);
592 dev_err(&ep
->dev
->pdev
->dev
, "%s: Invalid ep=%p or req=%p\n",
593 __func__
, _ep
, _req
);
597 req
= container_of(_req
, struct net2280_request
, req
);
598 WARN_ON(!list_empty(&req
->queue
));
600 pci_pool_free(ep
->dev
->requests
, req
->td
, req
->td_dma
);
604 /*-------------------------------------------------------------------------*/
606 /* load a packet into the fifo we use for usb IN transfers.
607 * works for all endpoints.
609 * NOTE: pio with ep-a..ep-d could stuff multiple packets into the fifo
610 * at a time, but this code is simpler because it knows it only writes
611 * one packet. ep-a..ep-d should use dma instead.
613 static void write_fifo(struct net2280_ep
*ep
, struct usb_request
*req
)
615 struct net2280_ep_regs __iomem
*regs
= ep
->regs
;
618 unsigned count
, total
;
620 /* INVARIANT: fifo is currently empty. (testable) */
623 buf
= req
->buf
+ req
->actual
;
625 total
= req
->length
- req
->actual
;
631 /* write just one packet at a time */
632 count
= ep
->ep
.maxpacket
;
633 if (count
> total
) /* min() cannot be used on a bitfield */
636 ep_vdbg(ep
->dev
, "write %s fifo (IN) %d bytes%s req %p\n",
638 (count
!= ep
->ep
.maxpacket
) ? " (short)" : "",
641 /* NOTE be careful if you try to align these. fifo lines
642 * should normally be full (4 bytes) and successive partial
643 * lines are ok only in certain cases.
645 tmp
= get_unaligned((u32
*)buf
);
647 writel(tmp
, ®s
->ep_data
);
652 /* last fifo entry is "short" unless we wrote a full packet.
653 * also explicitly validate last word in (periodic) transfers
654 * when maxpacket is not a multiple of 4 bytes.
656 if (count
|| total
< ep
->ep
.maxpacket
) {
657 tmp
= count
? get_unaligned((u32
*)buf
) : count
;
659 set_fifo_bytecount(ep
, count
& 0x03);
660 writel(tmp
, ®s
->ep_data
);
663 /* pci writes may still be posted */
666 /* work around erratum 0106: PCI and USB race over the OUT fifo.
667 * caller guarantees chiprev 0100, out endpoint is NAKing, and
668 * there's no real data in the fifo.
670 * NOTE: also used in cases where that erratum doesn't apply:
671 * where the host wrote "too much" data to us.
673 static void out_flush(struct net2280_ep
*ep
)
678 statp
= &ep
->regs
->ep_stat
;
681 if (tmp
& BIT(NAK_OUT_PACKETS
)) {
682 ep_dbg(ep
->dev
, "%s %s %08x !NAK\n",
683 ep
->ep
.name
, __func__
, tmp
);
684 writel(BIT(SET_NAK_OUT_PACKETS
), &ep
->regs
->ep_rsp
);
687 writel(BIT(DATA_OUT_PING_TOKEN_INTERRUPT
) |
688 BIT(DATA_PACKET_RECEIVED_INTERRUPT
),
690 writel(BIT(FIFO_FLUSH
), statp
);
691 /* Make sure that stap is written */
694 if (tmp
& BIT(DATA_OUT_PING_TOKEN_INTERRUPT
) &&
695 /* high speed did bulk NYET; fifo isn't filling */
696 ep
->dev
->gadget
.speed
== USB_SPEED_FULL
) {
699 usec
= 50; /* 64 byte bulk/interrupt */
700 handshake(statp
, BIT(USB_OUT_PING_NAK_SENT
),
701 BIT(USB_OUT_PING_NAK_SENT
), usec
);
702 /* NAK done; now CLEAR_NAK_OUT_PACKETS is safe */
706 /* unload packet(s) from the fifo we use for usb OUT transfers.
707 * returns true iff the request completed, because of short packet
708 * or the request buffer having filled with full packets.
710 * for ep-a..ep-d this will read multiple packets out when they
711 * have been accepted.
713 static int read_fifo(struct net2280_ep
*ep
, struct net2280_request
*req
)
715 struct net2280_ep_regs __iomem
*regs
= ep
->regs
;
716 u8
*buf
= req
->req
.buf
+ req
->req
.actual
;
717 unsigned count
, tmp
, is_short
;
718 unsigned cleanup
= 0, prevent
= 0;
720 /* erratum 0106 ... packets coming in during fifo reads might
721 * be incompletely rejected. not all cases have workarounds.
723 if (ep
->dev
->chiprev
== 0x0100 &&
724 ep
->dev
->gadget
.speed
== USB_SPEED_FULL
) {
726 tmp
= readl(&ep
->regs
->ep_stat
);
727 if ((tmp
& BIT(NAK_OUT_PACKETS
)))
729 else if ((tmp
& BIT(FIFO_FULL
))) {
730 start_out_naking(ep
);
733 /* else: hope we don't see the problem */
736 /* never overflow the rx buffer. the fifo reads packets until
737 * it sees a short one; we might not be ready for them all.
740 count
= readl(®s
->ep_avail
);
741 if (unlikely(count
== 0)) {
743 tmp
= readl(&ep
->regs
->ep_stat
);
744 count
= readl(®s
->ep_avail
);
745 /* handled that data already? */
746 if (count
== 0 && (tmp
& BIT(NAK_OUT_PACKETS
)) == 0)
750 tmp
= req
->req
.length
- req
->req
.actual
;
752 /* as with DMA, data overflow gets flushed */
753 if ((tmp
% ep
->ep
.maxpacket
) != 0) {
755 "%s out fifo %d bytes, expected %d\n",
756 ep
->ep
.name
, count
, tmp
);
757 req
->req
.status
= -EOVERFLOW
;
759 /* NAK_OUT_PACKETS will be set, so flushing is safe;
760 * the next read will start with the next packet
762 } /* else it's a ZLP, no worries */
765 req
->req
.actual
+= count
;
767 is_short
= (count
== 0) || ((count
% ep
->ep
.maxpacket
) != 0);
769 ep_vdbg(ep
->dev
, "read %s fifo (OUT) %d bytes%s%s%s req %p %d/%d\n",
770 ep
->ep
.name
, count
, is_short
? " (short)" : "",
771 cleanup
? " flush" : "", prevent
? " nak" : "",
772 req
, req
->req
.actual
, req
->req
.length
);
775 tmp
= readl(®s
->ep_data
);
777 put_unaligned(tmp
, (u32
*)buf
);
782 tmp
= readl(®s
->ep_data
);
783 /* LE conversion is implicit here: */
792 writel(BIT(CLEAR_NAK_OUT_PACKETS
), &ep
->regs
->ep_rsp
);
793 (void) readl(&ep
->regs
->ep_rsp
);
796 return is_short
|| ((req
->req
.actual
== req
->req
.length
) &&
800 /* fill out dma descriptor to match a given request */
801 static void fill_dma_desc(struct net2280_ep
*ep
,
802 struct net2280_request
*req
, int valid
)
804 struct net2280_dma
*td
= req
->td
;
805 u32 dmacount
= req
->req
.length
;
807 /* don't let DMA continue after a short OUT packet,
808 * so overruns can't affect the next transfer.
809 * in case of overruns on max-size packets, we can't
810 * stop the fifo from filling but we can flush it.
813 dmacount
|= BIT(DMA_DIRECTION
);
814 if ((!ep
->is_in
&& (dmacount
% ep
->ep
.maxpacket
) != 0) ||
815 !(ep
->dev
->quirks
& PLX_2280
))
816 dmacount
|= BIT(END_OF_CHAIN
);
820 dmacount
|= BIT(VALID_BIT
);
821 dmacount
|= BIT(DMA_DONE_INTERRUPT_ENABLE
);
823 /* td->dmadesc = previously set by caller */
824 td
->dmaaddr
= cpu_to_le32 (req
->req
.dma
);
826 /* 2280 may be polling VALID_BIT through ep->dma->dmadesc */
828 td
->dmacount
= cpu_to_le32(dmacount
);
831 static const u32 dmactl_default
=
832 BIT(DMA_SCATTER_GATHER_DONE_INTERRUPT
) |
833 BIT(DMA_CLEAR_COUNT_ENABLE
) |
834 /* erratum 0116 workaround part 1 (use POLLING) */
835 (POLL_100_USEC
<< DESCRIPTOR_POLLING_RATE
) |
836 BIT(DMA_VALID_BIT_POLLING_ENABLE
) |
837 BIT(DMA_VALID_BIT_ENABLE
) |
838 BIT(DMA_SCATTER_GATHER_ENABLE
) |
839 /* erratum 0116 workaround part 2 (no AUTOSTART) */
842 static inline void spin_stop_dma(struct net2280_dma_regs __iomem
*dma
)
844 handshake(&dma
->dmactl
, BIT(DMA_ENABLE
), 0, 50);
847 static inline void stop_dma(struct net2280_dma_regs __iomem
*dma
)
849 writel(readl(&dma
->dmactl
) & ~BIT(DMA_ENABLE
), &dma
->dmactl
);
853 static void start_queue(struct net2280_ep
*ep
, u32 dmactl
, u32 td_dma
)
855 struct net2280_dma_regs __iomem
*dma
= ep
->dma
;
856 unsigned int tmp
= BIT(VALID_BIT
) | (ep
->is_in
<< DMA_DIRECTION
);
858 if (!(ep
->dev
->quirks
& PLX_2280
))
859 tmp
|= BIT(END_OF_CHAIN
);
861 writel(tmp
, &dma
->dmacount
);
862 writel(readl(&dma
->dmastat
), &dma
->dmastat
);
864 writel(td_dma
, &dma
->dmadesc
);
865 if (ep
->dev
->quirks
& PLX_PCIE
)
866 dmactl
|= BIT(DMA_REQUEST_OUTSTANDING
);
867 writel(dmactl
, &dma
->dmactl
);
869 /* erratum 0116 workaround part 3: pci arbiter away from net2280 */
870 (void) readl(&ep
->dev
->pci
->pcimstctl
);
872 writel(BIT(DMA_START
), &dma
->dmastat
);
878 static void start_dma(struct net2280_ep
*ep
, struct net2280_request
*req
)
881 struct net2280_dma_regs __iomem
*dma
= ep
->dma
;
883 /* FIXME can't use DMA for ZLPs */
885 /* on this path we "know" there's no dma active (yet) */
886 WARN_ON(readl(&dma
->dmactl
) & BIT(DMA_ENABLE
));
887 writel(0, &ep
->dma
->dmactl
);
889 /* previous OUT packet might have been short */
890 if (!ep
->is_in
&& (readl(&ep
->regs
->ep_stat
) &
891 BIT(NAK_OUT_PACKETS
))) {
892 writel(BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT
),
895 tmp
= readl(&ep
->regs
->ep_avail
);
897 writel(readl(&dma
->dmastat
), &dma
->dmastat
);
899 /* transfer all/some fifo data */
900 writel(req
->req
.dma
, &dma
->dmaaddr
);
901 tmp
= min(tmp
, req
->req
.length
);
903 /* dma irq, faking scatterlist status */
904 req
->td
->dmacount
= cpu_to_le32(req
->req
.length
- tmp
);
905 writel(BIT(DMA_DONE_INTERRUPT_ENABLE
) | tmp
,
907 req
->td
->dmadesc
= 0;
910 writel(BIT(DMA_ENABLE
), &dma
->dmactl
);
911 writel(BIT(DMA_START
), &dma
->dmastat
);
916 tmp
= dmactl_default
;
918 /* force packet boundaries between dma requests, but prevent the
919 * controller from automagically writing a last "short" packet
920 * (zero length) unless the driver explicitly said to do that.
923 if (likely((req
->req
.length
% ep
->ep
.maxpacket
) ||
925 tmp
|= BIT(DMA_FIFO_VALIDATE
);
926 ep
->in_fifo_validate
= 1;
928 ep
->in_fifo_validate
= 0;
931 /* init req->td, pointing to the current dummy */
932 req
->td
->dmadesc
= cpu_to_le32 (ep
->td_dma
);
933 fill_dma_desc(ep
, req
, 1);
935 req
->td
->dmacount
|= cpu_to_le32(BIT(END_OF_CHAIN
));
937 start_queue(ep
, tmp
, req
->td_dma
);
941 queue_dma(struct net2280_ep
*ep
, struct net2280_request
*req
, int valid
)
943 struct net2280_dma
*end
;
946 /* swap new dummy for old, link; fill and maybe activate */
952 ep
->td_dma
= req
->td_dma
;
955 end
->dmadesc
= cpu_to_le32 (ep
->td_dma
);
957 fill_dma_desc(ep
, req
, valid
);
961 done(struct net2280_ep
*ep
, struct net2280_request
*req
, int status
)
964 unsigned stopped
= ep
->stopped
;
966 list_del_init(&req
->queue
);
968 if (req
->req
.status
== -EINPROGRESS
)
969 req
->req
.status
= status
;
971 status
= req
->req
.status
;
975 usb_gadget_unmap_request(&dev
->gadget
, &req
->req
, ep
->is_in
);
977 if (status
&& status
!= -ESHUTDOWN
)
978 ep_vdbg(dev
, "complete %s req %p stat %d len %u/%u\n",
979 ep
->ep
.name
, &req
->req
, status
,
980 req
->req
.actual
, req
->req
.length
);
982 /* don't modify queue heads during completion callback */
984 spin_unlock(&dev
->lock
);
985 usb_gadget_giveback_request(&ep
->ep
, &req
->req
);
986 spin_lock(&dev
->lock
);
987 ep
->stopped
= stopped
;
990 /*-------------------------------------------------------------------------*/
993 net2280_queue(struct usb_ep
*_ep
, struct usb_request
*_req
, gfp_t gfp_flags
)
995 struct net2280_request
*req
;
996 struct net2280_ep
*ep
;
1001 /* we always require a cpu-view buffer, so that we can
1002 * always use pio (as fallback or whatever).
1004 ep
= container_of(_ep
, struct net2280_ep
, ep
);
1005 if (!_ep
|| (!ep
->desc
&& ep
->num
!= 0)) {
1006 pr_err("%s: Invalid ep=%p or ep->desc\n", __func__
, _ep
);
1009 req
= container_of(_req
, struct net2280_request
, req
);
1010 if (!_req
|| !_req
->complete
|| !_req
->buf
||
1011 !list_empty(&req
->queue
)) {
1015 if (_req
->length
> (~0 & DMA_BYTE_COUNT_MASK
)) {
1020 if (!dev
->driver
|| dev
->gadget
.speed
== USB_SPEED_UNKNOWN
) {
1025 /* FIXME implement PIO fallback for ZLPs with DMA */
1026 if (ep
->dma
&& _req
->length
== 0) {
1031 /* set up dma mapping in case the caller didn't */
1033 ret
= usb_gadget_map_request(&dev
->gadget
, _req
,
1039 ep_vdbg(dev
, "%s queue req %p, len %d buf %p\n",
1040 _ep
->name
, _req
, _req
->length
, _req
->buf
);
1042 spin_lock_irqsave(&dev
->lock
, flags
);
1044 _req
->status
= -EINPROGRESS
;
1047 /* kickstart this i/o queue? */
1048 if (list_empty(&ep
->queue
) && !ep
->stopped
&&
1049 !((dev
->quirks
& PLX_PCIE
) && ep
->dma
&&
1050 (readl(&ep
->regs
->ep_rsp
) & BIT(CLEAR_ENDPOINT_HALT
)))) {
1052 /* use DMA if the endpoint supports it, else pio */
1056 /* maybe there's no control data, just status ack */
1057 if (ep
->num
== 0 && _req
->length
== 0) {
1060 ep_vdbg(dev
, "%s status ack\n", ep
->ep
.name
);
1064 /* PIO ... stuff the fifo, or unblock it. */
1066 write_fifo(ep
, _req
);
1067 else if (list_empty(&ep
->queue
)) {
1070 /* OUT FIFO might have packet(s) buffered */
1071 s
= readl(&ep
->regs
->ep_stat
);
1072 if ((s
& BIT(FIFO_EMPTY
)) == 0) {
1073 /* note: _req->short_not_ok is
1074 * ignored here since PIO _always_
1075 * stops queue advance here, and
1076 * _req->status doesn't change for
1077 * short reads (only _req->actual)
1079 if (read_fifo(ep
, req
) &&
1083 /* don't queue it */
1085 } else if (read_fifo(ep
, req
) &&
1090 s
= readl(&ep
->regs
->ep_stat
);
1093 /* don't NAK, let the fifo fill */
1094 if (req
&& (s
& BIT(NAK_OUT_PACKETS
)))
1095 writel(BIT(CLEAR_NAK_OUT_PACKETS
),
1100 } else if (ep
->dma
) {
1106 /* preventing magic zlps is per-engine state, not
1107 * per-transfer; irq logic must recover hiccups.
1109 expect
= likely(req
->req
.zero
||
1110 (req
->req
.length
% ep
->ep
.maxpacket
));
1111 if (expect
!= ep
->in_fifo_validate
)
1114 queue_dma(ep
, req
, valid
);
1116 } /* else the irq handler advances the queue. */
1120 list_add_tail(&req
->queue
, &ep
->queue
);
1122 spin_unlock_irqrestore(&dev
->lock
, flags
);
1124 /* pci writes may still be posted */
1128 dev_err(&ep
->dev
->pdev
->dev
, "%s: error=%d\n", __func__
, ret
);
1133 dma_done(struct net2280_ep
*ep
, struct net2280_request
*req
, u32 dmacount
,
1136 req
->req
.actual
= req
->req
.length
- (DMA_BYTE_COUNT_MASK
& dmacount
);
1137 done(ep
, req
, status
);
1140 static int scan_dma_completions(struct net2280_ep
*ep
)
1142 int num_completed
= 0;
1144 /* only look at descriptors that were "naturally" retired,
1145 * so fifo and list head state won't matter
1147 while (!list_empty(&ep
->queue
)) {
1148 struct net2280_request
*req
;
1151 req
= list_entry(ep
->queue
.next
,
1152 struct net2280_request
, queue
);
1156 req_dma_count
= le32_to_cpup(&req
->td
->dmacount
);
1157 if ((req_dma_count
& BIT(VALID_BIT
)) != 0)
1160 /* SHORT_PACKET_TRANSFERRED_INTERRUPT handles "usb-short"
1161 * cases where DMA must be aborted; this code handles
1162 * all non-abort DMA completions.
1164 if (unlikely(req
->td
->dmadesc
== 0)) {
1166 u32
const ep_dmacount
= readl(&ep
->dma
->dmacount
);
1168 if (ep_dmacount
& DMA_BYTE_COUNT_MASK
)
1170 /* single transfer mode */
1171 dma_done(ep
, req
, req_dma_count
, 0);
1174 } else if (!ep
->is_in
&&
1175 (req
->req
.length
% ep
->ep
.maxpacket
) &&
1176 !(ep
->dev
->quirks
& PLX_PCIE
)) {
1178 u32
const ep_stat
= readl(&ep
->regs
->ep_stat
);
1179 /* AVOID TROUBLE HERE by not issuing short reads from
1180 * your gadget driver. That helps avoids errata 0121,
1181 * 0122, and 0124; not all cases trigger the warning.
1183 if ((ep_stat
& BIT(NAK_OUT_PACKETS
)) == 0) {
1184 ep_warn(ep
->dev
, "%s lost packet sync!\n",
1186 req
->req
.status
= -EOVERFLOW
;
1188 u32
const ep_avail
= readl(&ep
->regs
->ep_avail
);
1190 /* fifo gets flushed later */
1191 ep
->out_overflow
= 1;
1193 "%s dma, discard %d len %d\n",
1194 ep
->ep
.name
, ep_avail
,
1196 req
->req
.status
= -EOVERFLOW
;
1200 dma_done(ep
, req
, req_dma_count
, 0);
1204 return num_completed
;
1207 static void restart_dma(struct net2280_ep
*ep
)
1209 struct net2280_request
*req
;
1213 req
= list_entry(ep
->queue
.next
, struct net2280_request
, queue
);
1218 static void abort_dma(struct net2280_ep
*ep
)
1220 /* abort the current transfer */
1221 if (likely(!list_empty(&ep
->queue
))) {
1222 /* FIXME work around errata 0121, 0122, 0124 */
1223 writel(BIT(DMA_ABORT
), &ep
->dma
->dmastat
);
1224 spin_stop_dma(ep
->dma
);
1227 scan_dma_completions(ep
);
1230 /* dequeue ALL requests */
1231 static void nuke(struct net2280_ep
*ep
)
1233 struct net2280_request
*req
;
1235 /* called with spinlock held */
1239 while (!list_empty(&ep
->queue
)) {
1240 req
= list_entry(ep
->queue
.next
,
1241 struct net2280_request
,
1243 done(ep
, req
, -ESHUTDOWN
);
1247 /* dequeue JUST ONE request */
1248 static int net2280_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
1250 struct net2280_ep
*ep
;
1251 struct net2280_request
*req
;
1252 unsigned long flags
;
1256 ep
= container_of(_ep
, struct net2280_ep
, ep
);
1257 if (!_ep
|| (!ep
->desc
&& ep
->num
!= 0) || !_req
) {
1258 pr_err("%s: Invalid ep=%p or ep->desc or req=%p\n",
1259 __func__
, _ep
, _req
);
1263 spin_lock_irqsave(&ep
->dev
->lock
, flags
);
1264 stopped
= ep
->stopped
;
1266 /* quiesce dma while we patch the queue */
1270 dmactl
= readl(&ep
->dma
->dmactl
);
1271 /* WARNING erratum 0127 may kick in ... */
1273 scan_dma_completions(ep
);
1276 /* make sure it's still queued on this endpoint */
1277 list_for_each_entry(req
, &ep
->queue
, queue
) {
1278 if (&req
->req
== _req
)
1281 if (&req
->req
!= _req
) {
1282 spin_unlock_irqrestore(&ep
->dev
->lock
, flags
);
1283 dev_err(&ep
->dev
->pdev
->dev
, "%s: Request mismatch\n",
1288 /* queue head may be partially complete. */
1289 if (ep
->queue
.next
== &req
->queue
) {
1291 ep_dbg(ep
->dev
, "unlink (%s) dma\n", _ep
->name
);
1292 _req
->status
= -ECONNRESET
;
1294 if (likely(ep
->queue
.next
== &req
->queue
)) {
1295 /* NOTE: misreports single-transfer mode*/
1296 req
->td
->dmacount
= 0; /* invalidate */
1298 readl(&ep
->dma
->dmacount
),
1302 ep_dbg(ep
->dev
, "unlink (%s) pio\n", _ep
->name
);
1303 done(ep
, req
, -ECONNRESET
);
1309 done(ep
, req
, -ECONNRESET
);
1310 ep
->stopped
= stopped
;
1313 /* turn off dma on inactive queues */
1314 if (list_empty(&ep
->queue
))
1316 else if (!ep
->stopped
) {
1317 /* resume current request, or start new one */
1319 writel(dmactl
, &ep
->dma
->dmactl
);
1321 start_dma(ep
, list_entry(ep
->queue
.next
,
1322 struct net2280_request
, queue
));
1326 spin_unlock_irqrestore(&ep
->dev
->lock
, flags
);
1330 /*-------------------------------------------------------------------------*/
1332 static int net2280_fifo_status(struct usb_ep
*_ep
);
1335 net2280_set_halt_and_wedge(struct usb_ep
*_ep
, int value
, int wedged
)
1337 struct net2280_ep
*ep
;
1338 unsigned long flags
;
1341 ep
= container_of(_ep
, struct net2280_ep
, ep
);
1342 if (!_ep
|| (!ep
->desc
&& ep
->num
!= 0)) {
1343 pr_err("%s: Invalid ep=%p or ep->desc\n", __func__
, _ep
);
1346 if (!ep
->dev
->driver
|| ep
->dev
->gadget
.speed
== USB_SPEED_UNKNOWN
) {
1347 retval
= -ESHUTDOWN
;
1350 if (ep
->desc
/* not ep0 */ && (ep
->desc
->bmAttributes
& 0x03)
1351 == USB_ENDPOINT_XFER_ISOC
) {
1356 spin_lock_irqsave(&ep
->dev
->lock
, flags
);
1357 if (!list_empty(&ep
->queue
)) {
1360 } else if (ep
->is_in
&& value
&& net2280_fifo_status(_ep
) != 0) {
1364 ep_vdbg(ep
->dev
, "%s %s %s\n", _ep
->name
,
1365 value
? "set" : "clear",
1366 wedged
? "wedge" : "halt");
1367 /* set/clear, then synch memory views with the device */
1370 ep
->dev
->protocol_stall
= 1;
1377 if (ep
->dev
->quirks
& PLX_PCIE
&&
1378 !list_empty(&ep
->queue
) && ep
->td_dma
)
1382 (void) readl(&ep
->regs
->ep_rsp
);
1384 spin_unlock_irqrestore(&ep
->dev
->lock
, flags
);
1389 spin_unlock_irqrestore(&ep
->dev
->lock
, flags
);
1391 dev_err(&ep
->dev
->pdev
->dev
, "%s: error=%d\n", __func__
, retval
);
1395 static int net2280_set_halt(struct usb_ep
*_ep
, int value
)
1397 return net2280_set_halt_and_wedge(_ep
, value
, 0);
1400 static int net2280_set_wedge(struct usb_ep
*_ep
)
1402 if (!_ep
|| _ep
->name
== ep0name
) {
1403 pr_err("%s: Invalid ep=%p or ep0\n", __func__
, _ep
);
1406 return net2280_set_halt_and_wedge(_ep
, 1, 1);
1409 static int net2280_fifo_status(struct usb_ep
*_ep
)
1411 struct net2280_ep
*ep
;
1414 ep
= container_of(_ep
, struct net2280_ep
, ep
);
1415 if (!_ep
|| (!ep
->desc
&& ep
->num
!= 0)) {
1416 pr_err("%s: Invalid ep=%p or ep->desc\n", __func__
, _ep
);
1419 if (!ep
->dev
->driver
|| ep
->dev
->gadget
.speed
== USB_SPEED_UNKNOWN
) {
1420 dev_err(&ep
->dev
->pdev
->dev
,
1421 "%s: Invalid driver=%p or speed=%d\n",
1422 __func__
, ep
->dev
->driver
, ep
->dev
->gadget
.speed
);
1426 avail
= readl(&ep
->regs
->ep_avail
) & (BIT(12) - 1);
1427 if (avail
> ep
->fifo_size
) {
1428 dev_err(&ep
->dev
->pdev
->dev
, "%s: Fifo overflow\n", __func__
);
1432 avail
= ep
->fifo_size
- avail
;
1436 static void net2280_fifo_flush(struct usb_ep
*_ep
)
1438 struct net2280_ep
*ep
;
1440 ep
= container_of(_ep
, struct net2280_ep
, ep
);
1441 if (!_ep
|| (!ep
->desc
&& ep
->num
!= 0)) {
1442 pr_err("%s: Invalid ep=%p or ep->desc\n", __func__
, _ep
);
1445 if (!ep
->dev
->driver
|| ep
->dev
->gadget
.speed
== USB_SPEED_UNKNOWN
) {
1446 dev_err(&ep
->dev
->pdev
->dev
,
1447 "%s: Invalid driver=%p or speed=%d\n",
1448 __func__
, ep
->dev
->driver
, ep
->dev
->gadget
.speed
);
1452 writel(BIT(FIFO_FLUSH
), &ep
->regs
->ep_stat
);
1453 (void) readl(&ep
->regs
->ep_rsp
);
1456 static const struct usb_ep_ops net2280_ep_ops
= {
1457 .enable
= net2280_enable
,
1458 .disable
= net2280_disable
,
1460 .alloc_request
= net2280_alloc_request
,
1461 .free_request
= net2280_free_request
,
1463 .queue
= net2280_queue
,
1464 .dequeue
= net2280_dequeue
,
1466 .set_halt
= net2280_set_halt
,
1467 .set_wedge
= net2280_set_wedge
,
1468 .fifo_status
= net2280_fifo_status
,
1469 .fifo_flush
= net2280_fifo_flush
,
1472 /*-------------------------------------------------------------------------*/
1474 static int net2280_get_frame(struct usb_gadget
*_gadget
)
1476 struct net2280
*dev
;
1477 unsigned long flags
;
1482 dev
= container_of(_gadget
, struct net2280
, gadget
);
1483 spin_lock_irqsave(&dev
->lock
, flags
);
1484 retval
= get_idx_reg(dev
->regs
, REG_FRAME
) & 0x03ff;
1485 spin_unlock_irqrestore(&dev
->lock
, flags
);
1489 static int net2280_wakeup(struct usb_gadget
*_gadget
)
1491 struct net2280
*dev
;
1493 unsigned long flags
;
1497 dev
= container_of(_gadget
, struct net2280
, gadget
);
1499 spin_lock_irqsave(&dev
->lock
, flags
);
1500 tmp
= readl(&dev
->usb
->usbctl
);
1501 if (tmp
& BIT(DEVICE_REMOTE_WAKEUP_ENABLE
))
1502 writel(BIT(GENERATE_RESUME
), &dev
->usb
->usbstat
);
1503 spin_unlock_irqrestore(&dev
->lock
, flags
);
1505 /* pci writes may still be posted */
1509 static int net2280_set_selfpowered(struct usb_gadget
*_gadget
, int value
)
1511 struct net2280
*dev
;
1513 unsigned long flags
;
1517 dev
= container_of(_gadget
, struct net2280
, gadget
);
1519 spin_lock_irqsave(&dev
->lock
, flags
);
1520 tmp
= readl(&dev
->usb
->usbctl
);
1522 tmp
|= BIT(SELF_POWERED_STATUS
);
1523 _gadget
->is_selfpowered
= 1;
1525 tmp
&= ~BIT(SELF_POWERED_STATUS
);
1526 _gadget
->is_selfpowered
= 0;
1528 writel(tmp
, &dev
->usb
->usbctl
);
1529 spin_unlock_irqrestore(&dev
->lock
, flags
);
1534 static int net2280_pullup(struct usb_gadget
*_gadget
, int is_on
)
1536 struct net2280
*dev
;
1538 unsigned long flags
;
1542 dev
= container_of(_gadget
, struct net2280
, gadget
);
1544 spin_lock_irqsave(&dev
->lock
, flags
);
1545 tmp
= readl(&dev
->usb
->usbctl
);
1546 dev
->softconnect
= (is_on
!= 0);
1549 writel(tmp
| BIT(USB_DETECT_ENABLE
), &dev
->usb
->usbctl
);
1551 writel(tmp
& ~BIT(USB_DETECT_ENABLE
), &dev
->usb
->usbctl
);
1552 stop_activity(dev
, dev
->driver
);
1555 spin_unlock_irqrestore(&dev
->lock
, flags
);
1560 static struct usb_ep
*net2280_match_ep(struct usb_gadget
*_gadget
,
1561 struct usb_endpoint_descriptor
*desc
,
1562 struct usb_ss_ep_comp_descriptor
*ep_comp
)
1567 if (usb_endpoint_type(desc
) == USB_ENDPOINT_XFER_INT
) {
1568 /* ep-e, ep-f are PIO with only 64 byte fifos */
1569 ep
= gadget_find_ep_by_name(_gadget
, "ep-e");
1570 if (ep
&& usb_gadget_ep_match_desc(_gadget
, ep
, desc
, ep_comp
))
1572 ep
= gadget_find_ep_by_name(_gadget
, "ep-f");
1573 if (ep
&& usb_gadget_ep_match_desc(_gadget
, ep
, desc
, ep_comp
))
1577 /* USB3380: Only first four endpoints have DMA channels. Allocate
1578 * slower interrupt endpoints from PIO hw endpoints, to allow bulk/isoc
1579 * endpoints use DMA hw endpoints.
1581 if (usb_endpoint_type(desc
) == USB_ENDPOINT_XFER_INT
&&
1582 usb_endpoint_dir_in(desc
)) {
1583 ep
= gadget_find_ep_by_name(_gadget
, "ep2in");
1584 if (ep
&& usb_gadget_ep_match_desc(_gadget
, ep
, desc
, ep_comp
))
1586 ep
= gadget_find_ep_by_name(_gadget
, "ep4in");
1587 if (ep
&& usb_gadget_ep_match_desc(_gadget
, ep
, desc
, ep_comp
))
1589 } else if (usb_endpoint_type(desc
) == USB_ENDPOINT_XFER_INT
&&
1590 !usb_endpoint_dir_in(desc
)) {
1591 ep
= gadget_find_ep_by_name(_gadget
, "ep1out");
1592 if (ep
&& usb_gadget_ep_match_desc(_gadget
, ep
, desc
, ep_comp
))
1594 ep
= gadget_find_ep_by_name(_gadget
, "ep3out");
1595 if (ep
&& usb_gadget_ep_match_desc(_gadget
, ep
, desc
, ep_comp
))
1597 } else if (usb_endpoint_type(desc
) != USB_ENDPOINT_XFER_BULK
&&
1598 usb_endpoint_dir_in(desc
)) {
1599 ep
= gadget_find_ep_by_name(_gadget
, "ep1in");
1600 if (ep
&& usb_gadget_ep_match_desc(_gadget
, ep
, desc
, ep_comp
))
1602 ep
= gadget_find_ep_by_name(_gadget
, "ep3in");
1603 if (ep
&& usb_gadget_ep_match_desc(_gadget
, ep
, desc
, ep_comp
))
1605 } else if (usb_endpoint_type(desc
) != USB_ENDPOINT_XFER_BULK
&&
1606 !usb_endpoint_dir_in(desc
)) {
1607 ep
= gadget_find_ep_by_name(_gadget
, "ep2out");
1608 if (ep
&& usb_gadget_ep_match_desc(_gadget
, ep
, desc
, ep_comp
))
1610 ep
= gadget_find_ep_by_name(_gadget
, "ep4out");
1611 if (ep
&& usb_gadget_ep_match_desc(_gadget
, ep
, desc
, ep_comp
))
1615 /* USB3380: use same address for usb and hardware endpoints */
1616 snprintf(name
, sizeof(name
), "ep%d%s", usb_endpoint_num(desc
),
1617 usb_endpoint_dir_in(desc
) ? "in" : "out");
1618 ep
= gadget_find_ep_by_name(_gadget
, name
);
1619 if (ep
&& usb_gadget_ep_match_desc(_gadget
, ep
, desc
, ep_comp
))
1625 static int net2280_start(struct usb_gadget
*_gadget
,
1626 struct usb_gadget_driver
*driver
);
1627 static int net2280_stop(struct usb_gadget
*_gadget
);
1629 static const struct usb_gadget_ops net2280_ops
= {
1630 .get_frame
= net2280_get_frame
,
1631 .wakeup
= net2280_wakeup
,
1632 .set_selfpowered
= net2280_set_selfpowered
,
1633 .pullup
= net2280_pullup
,
1634 .udc_start
= net2280_start
,
1635 .udc_stop
= net2280_stop
,
1636 .match_ep
= net2280_match_ep
,
1639 /*-------------------------------------------------------------------------*/
1641 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1643 /* FIXME move these into procfs, and use seq_file.
1644 * Sysfs _still_ doesn't behave for arbitrarily sized files,
1645 * and also doesn't help products using this with 2.4 kernels.
1648 /* "function" sysfs attribute */
1649 static ssize_t
function_show(struct device
*_dev
, struct device_attribute
*attr
,
1652 struct net2280
*dev
= dev_get_drvdata(_dev
);
1654 if (!dev
->driver
|| !dev
->driver
->function
||
1655 strlen(dev
->driver
->function
) > PAGE_SIZE
)
1657 return scnprintf(buf
, PAGE_SIZE
, "%s\n", dev
->driver
->function
);
1659 static DEVICE_ATTR_RO(function
);
1661 static ssize_t
registers_show(struct device
*_dev
,
1662 struct device_attribute
*attr
, char *buf
)
1664 struct net2280
*dev
;
1667 unsigned long flags
;
1672 dev
= dev_get_drvdata(_dev
);
1675 spin_lock_irqsave(&dev
->lock
, flags
);
1678 s
= dev
->driver
->driver
.name
;
1682 /* Main Control Registers */
1683 t
= scnprintf(next
, size
, "%s version " DRIVER_VERSION
1684 ", chiprev %04x\n\n"
1685 "devinit %03x fifoctl %08x gadget '%s'\n"
1686 "pci irqenb0 %02x irqenb1 %08x "
1687 "irqstat0 %04x irqstat1 %08x\n",
1688 driver_name
, dev
->chiprev
,
1689 readl(&dev
->regs
->devinit
),
1690 readl(&dev
->regs
->fifoctl
),
1692 readl(&dev
->regs
->pciirqenb0
),
1693 readl(&dev
->regs
->pciirqenb1
),
1694 readl(&dev
->regs
->irqstat0
),
1695 readl(&dev
->regs
->irqstat1
));
1699 /* USB Control Registers */
1700 t1
= readl(&dev
->usb
->usbctl
);
1701 t2
= readl(&dev
->usb
->usbstat
);
1702 if (t1
& BIT(VBUS_PIN
)) {
1703 if (t2
& BIT(HIGH_SPEED
))
1705 else if (dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
1709 /* full speed bit (6) not working?? */
1712 t
= scnprintf(next
, size
,
1713 "stdrsp %08x usbctl %08x usbstat %08x "
1714 "addr 0x%02x (%s)\n",
1715 readl(&dev
->usb
->stdrsp
), t1
, t2
,
1716 readl(&dev
->usb
->ouraddr
), s
);
1720 /* PCI Master Control Registers */
1722 /* DMA Control Registers */
1724 /* Configurable EP Control Registers */
1725 for (i
= 0; i
< dev
->n_ep
; i
++) {
1726 struct net2280_ep
*ep
;
1732 t1
= readl(&ep
->cfg
->ep_cfg
);
1733 t2
= readl(&ep
->regs
->ep_rsp
) & 0xff;
1734 t
= scnprintf(next
, size
,
1735 "\n%s\tcfg %05x rsp (%02x) %s%s%s%s%s%s%s%s"
1737 ep
->ep
.name
, t1
, t2
,
1738 (t2
& BIT(CLEAR_NAK_OUT_PACKETS
))
1740 (t2
& BIT(CLEAR_EP_HIDE_STATUS_PHASE
))
1742 (t2
& BIT(CLEAR_EP_FORCE_CRC_ERROR
))
1744 (t2
& BIT(CLEAR_INTERRUPT_MODE
))
1745 ? "interrupt " : "",
1746 (t2
& BIT(CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE
))
1748 (t2
& BIT(CLEAR_NAK_OUT_PACKETS_MODE
))
1750 (t2
& BIT(CLEAR_ENDPOINT_TOGGLE
))
1751 ? "DATA1 " : "DATA0 ",
1752 (t2
& BIT(CLEAR_ENDPOINT_HALT
))
1754 readl(&ep
->regs
->ep_irqenb
));
1758 t
= scnprintf(next
, size
,
1759 "\tstat %08x avail %04x "
1761 readl(&ep
->regs
->ep_stat
),
1762 readl(&ep
->regs
->ep_avail
),
1763 t1
& 0x0f, DIR_STRING(t1
),
1764 type_string(t1
>> 8),
1765 ep
->stopped
? "*" : "");
1772 t
= scnprintf(next
, size
,
1773 " dma\tctl %08x stat %08x count %08x\n"
1774 "\taddr %08x desc %08x\n",
1775 readl(&ep
->dma
->dmactl
),
1776 readl(&ep
->dma
->dmastat
),
1777 readl(&ep
->dma
->dmacount
),
1778 readl(&ep
->dma
->dmaaddr
),
1779 readl(&ep
->dma
->dmadesc
));
1785 /* Indexed Registers (none yet) */
1788 t
= scnprintf(next
, size
, "\nirqs: ");
1791 for (i
= 0; i
< dev
->n_ep
; i
++) {
1792 struct net2280_ep
*ep
;
1797 t
= scnprintf(next
, size
, " %s/%lu", ep
->ep
.name
, ep
->irqs
);
1802 t
= scnprintf(next
, size
, "\n");
1806 spin_unlock_irqrestore(&dev
->lock
, flags
);
1808 return PAGE_SIZE
- size
;
1810 static DEVICE_ATTR_RO(registers
);
1812 static ssize_t
queues_show(struct device
*_dev
, struct device_attribute
*attr
,
1815 struct net2280
*dev
;
1818 unsigned long flags
;
1821 dev
= dev_get_drvdata(_dev
);
1824 spin_lock_irqsave(&dev
->lock
, flags
);
1826 for (i
= 0; i
< dev
->n_ep
; i
++) {
1827 struct net2280_ep
*ep
= &dev
->ep
[i
];
1828 struct net2280_request
*req
;
1832 const struct usb_endpoint_descriptor
*d
;
1837 t
= d
->bEndpointAddress
;
1838 t
= scnprintf(next
, size
,
1839 "\n%s (ep%d%s-%s) max %04x %s fifo %d\n",
1840 ep
->ep
.name
, t
& USB_ENDPOINT_NUMBER_MASK
,
1841 (t
& USB_DIR_IN
) ? "in" : "out",
1842 type_string(d
->bmAttributes
),
1843 usb_endpoint_maxp(d
) & 0x1fff,
1844 ep
->dma
? "dma" : "pio", ep
->fifo_size
1846 } else /* ep0 should only have one transfer queued */
1847 t
= scnprintf(next
, size
, "ep0 max 64 pio %s\n",
1848 ep
->is_in
? "in" : "out");
1849 if (t
<= 0 || t
> size
)
1854 if (list_empty(&ep
->queue
)) {
1855 t
= scnprintf(next
, size
, "\t(nothing queued)\n");
1856 if (t
<= 0 || t
> size
)
1862 list_for_each_entry(req
, &ep
->queue
, queue
) {
1863 if (ep
->dma
&& req
->td_dma
== readl(&ep
->dma
->dmadesc
))
1864 t
= scnprintf(next
, size
,
1865 "\treq %p len %d/%d "
1866 "buf %p (dmacount %08x)\n",
1867 &req
->req
, req
->req
.actual
,
1868 req
->req
.length
, req
->req
.buf
,
1869 readl(&ep
->dma
->dmacount
));
1871 t
= scnprintf(next
, size
,
1872 "\treq %p len %d/%d buf %p\n",
1873 &req
->req
, req
->req
.actual
,
1874 req
->req
.length
, req
->req
.buf
);
1875 if (t
<= 0 || t
> size
)
1881 struct net2280_dma
*td
;
1884 t
= scnprintf(next
, size
, "\t td %08x "
1885 " count %08x buf %08x desc %08x\n",
1887 le32_to_cpu(td
->dmacount
),
1888 le32_to_cpu(td
->dmaaddr
),
1889 le32_to_cpu(td
->dmadesc
));
1890 if (t
<= 0 || t
> size
)
1899 spin_unlock_irqrestore(&dev
->lock
, flags
);
1900 return PAGE_SIZE
- size
;
1902 static DEVICE_ATTR_RO(queues
);
1907 #define device_create_file(a, b) (0)
1908 #define device_remove_file(a, b) do { } while (0)
1912 /*-------------------------------------------------------------------------*/
1914 /* another driver-specific mode might be a request type doing dma
1915 * to/from another device fifo instead of to/from memory.
1918 static void set_fifo_mode(struct net2280
*dev
, int mode
)
1920 /* keeping high bits preserves BAR2 */
1921 writel((0xffff << PCI_BASE2_RANGE
) | mode
, &dev
->regs
->fifoctl
);
1923 /* always ep-{a,b,e,f} ... maybe not ep-c or ep-d */
1924 INIT_LIST_HEAD(&dev
->gadget
.ep_list
);
1925 list_add_tail(&dev
->ep
[1].ep
.ep_list
, &dev
->gadget
.ep_list
);
1926 list_add_tail(&dev
->ep
[2].ep
.ep_list
, &dev
->gadget
.ep_list
);
1929 list_add_tail(&dev
->ep
[3].ep
.ep_list
, &dev
->gadget
.ep_list
);
1930 list_add_tail(&dev
->ep
[4].ep
.ep_list
, &dev
->gadget
.ep_list
);
1931 dev
->ep
[1].fifo_size
= dev
->ep
[2].fifo_size
= 1024;
1934 dev
->ep
[1].fifo_size
= dev
->ep
[2].fifo_size
= 2048;
1937 list_add_tail(&dev
->ep
[3].ep
.ep_list
, &dev
->gadget
.ep_list
);
1938 dev
->ep
[1].fifo_size
= 2048;
1939 dev
->ep
[2].fifo_size
= 1024;
1942 /* fifo sizes for ep0, ep-c, ep-d, ep-e, and ep-f never change */
1943 list_add_tail(&dev
->ep
[5].ep
.ep_list
, &dev
->gadget
.ep_list
);
1944 list_add_tail(&dev
->ep
[6].ep
.ep_list
, &dev
->gadget
.ep_list
);
1947 static void defect7374_disable_data_eps(struct net2280
*dev
)
1950 * For Defect 7374, disable data EPs (and more):
1951 * - This phase undoes the earlier phase of the Defect 7374 workaround,
1952 * returing ep regs back to normal.
1954 struct net2280_ep
*ep
;
1956 unsigned char ep_sel
;
1959 for (i
= 1; i
< 5; i
++) {
1961 writel(i
, &ep
->cfg
->ep_cfg
);
1964 /* CSROUT, CSRIN, PCIOUT, PCIIN, STATIN, RCIN */
1965 for (i
= 0; i
< 6; i
++)
1966 writel(0, &dev
->dep
[i
].dep_cfg
);
1968 for (ep_sel
= 0; ep_sel
<= 21; ep_sel
++) {
1969 /* Select an endpoint for subsequent operations: */
1970 tmp_reg
= readl(&dev
->plregs
->pl_ep_ctrl
);
1971 writel(((tmp_reg
& ~0x1f) | ep_sel
), &dev
->plregs
->pl_ep_ctrl
);
1973 if (ep_sel
< 2 || (ep_sel
> 9 && ep_sel
< 14) ||
1974 ep_sel
== 18 || ep_sel
== 20)
1977 /* Change settings on some selected endpoints */
1978 tmp_reg
= readl(&dev
->plregs
->pl_ep_cfg_4
);
1979 tmp_reg
&= ~BIT(NON_CTRL_IN_TOLERATE_BAD_DIR
);
1980 writel(tmp_reg
, &dev
->plregs
->pl_ep_cfg_4
);
1981 tmp_reg
= readl(&dev
->plregs
->pl_ep_ctrl
);
1982 tmp_reg
|= BIT(EP_INITIALIZED
);
1983 writel(tmp_reg
, &dev
->plregs
->pl_ep_ctrl
);
1987 static void defect7374_enable_data_eps_zero(struct net2280
*dev
)
1989 u32 tmp
= 0, tmp_reg
;
1992 unsigned char ep_sel
;
1994 scratch
= get_idx_reg(dev
->regs
, SCRATCH
);
1996 WARN_ON((scratch
& (0xf << DEFECT7374_FSM_FIELD
))
1997 == DEFECT7374_FSM_SS_CONTROL_READ
);
1999 scratch
&= ~(0xf << DEFECT7374_FSM_FIELD
);
2001 ep_warn(dev
, "Operate Defect 7374 workaround soft this time");
2002 ep_warn(dev
, "It will operate on cold-reboot and SS connect");
2005 tmp
= ((0 << ENDPOINT_NUMBER
) | BIT(ENDPOINT_DIRECTION
) |
2006 (2 << OUT_ENDPOINT_TYPE
) | (2 << IN_ENDPOINT_TYPE
) |
2007 ((dev
->enhanced_mode
) ?
2008 BIT(OUT_ENDPOINT_ENABLE
) | BIT(IN_ENDPOINT_ENABLE
) :
2009 BIT(ENDPOINT_ENABLE
)));
2011 for (i
= 1; i
< 5; i
++)
2012 writel(tmp
, &dev
->ep
[i
].cfg
->ep_cfg
);
2014 /* CSRIN, PCIIN, STATIN, RCIN*/
2015 tmp
= ((0 << ENDPOINT_NUMBER
) | BIT(ENDPOINT_ENABLE
));
2016 writel(tmp
, &dev
->dep
[1].dep_cfg
);
2017 writel(tmp
, &dev
->dep
[3].dep_cfg
);
2018 writel(tmp
, &dev
->dep
[4].dep_cfg
);
2019 writel(tmp
, &dev
->dep
[5].dep_cfg
);
2021 /*Implemented for development and debug.
2022 * Can be refined/tuned later.*/
2023 for (ep_sel
= 0; ep_sel
<= 21; ep_sel
++) {
2024 /* Select an endpoint for subsequent operations: */
2025 tmp_reg
= readl(&dev
->plregs
->pl_ep_ctrl
);
2026 writel(((tmp_reg
& ~0x1f) | ep_sel
),
2027 &dev
->plregs
->pl_ep_ctrl
);
2031 (readl(&dev
->plregs
->pl_ep_ctrl
) |
2032 BIT(CLEAR_ACK_ERROR_CODE
) | 0);
2033 writel(tmp
, &dev
->plregs
->pl_ep_ctrl
);
2037 if (ep_sel
== 0 || (ep_sel
> 9 && ep_sel
< 14) ||
2038 ep_sel
== 18 || ep_sel
== 20)
2041 tmp
= (readl(&dev
->plregs
->pl_ep_cfg_4
) |
2042 BIT(NON_CTRL_IN_TOLERATE_BAD_DIR
) | 0);
2043 writel(tmp
, &dev
->plregs
->pl_ep_cfg_4
);
2045 tmp
= readl(&dev
->plregs
->pl_ep_ctrl
) &
2046 ~BIT(EP_INITIALIZED
);
2047 writel(tmp
, &dev
->plregs
->pl_ep_ctrl
);
2051 /* Set FSM to focus on the first Control Read:
2052 * - Tip: Connection speed is known upon the first
2054 scratch
|= DEFECT7374_FSM_WAITING_FOR_CONTROL_READ
;
2055 set_idx_reg(dev
->regs
, SCRATCH
, scratch
);
2059 /* keeping it simple:
2060 * - one bus driver, initted first;
2061 * - one function driver, initted second
2063 * most of the work to support multiple net2280 controllers would
2064 * be to associate this gadget driver (yes?) with all of them, or
2065 * perhaps to bind specific drivers to specific devices.
2068 static void usb_reset_228x(struct net2280
*dev
)
2072 dev
->gadget
.speed
= USB_SPEED_UNKNOWN
;
2073 (void) readl(&dev
->usb
->usbctl
);
2075 net2280_led_init(dev
);
2077 /* disable automatic responses, and irqs */
2078 writel(0, &dev
->usb
->stdrsp
);
2079 writel(0, &dev
->regs
->pciirqenb0
);
2080 writel(0, &dev
->regs
->pciirqenb1
);
2082 /* clear old dma and irq state */
2083 for (tmp
= 0; tmp
< 4; tmp
++) {
2084 struct net2280_ep
*ep
= &dev
->ep
[tmp
+ 1];
2089 writel(~0, &dev
->regs
->irqstat0
),
2090 writel(~(u32
)BIT(SUSPEND_REQUEST_INTERRUPT
), &dev
->regs
->irqstat1
),
2092 /* reset, and enable pci */
2093 tmp
= readl(&dev
->regs
->devinit
) |
2095 BIT(FIFO_SOFT_RESET
) |
2096 BIT(USB_SOFT_RESET
) |
2098 writel(tmp
, &dev
->regs
->devinit
);
2100 /* standard fifo and endpoint allocations */
2101 set_fifo_mode(dev
, (fifo_mode
<= 2) ? fifo_mode
: 0);
2104 static void usb_reset_338x(struct net2280
*dev
)
2108 dev
->gadget
.speed
= USB_SPEED_UNKNOWN
;
2109 (void)readl(&dev
->usb
->usbctl
);
2111 net2280_led_init(dev
);
2113 if (dev
->bug7734_patched
) {
2114 /* disable automatic responses, and irqs */
2115 writel(0, &dev
->usb
->stdrsp
);
2116 writel(0, &dev
->regs
->pciirqenb0
);
2117 writel(0, &dev
->regs
->pciirqenb1
);
2120 /* clear old dma and irq state */
2121 for (tmp
= 0; tmp
< 4; tmp
++) {
2122 struct net2280_ep
*ep
= &dev
->ep
[tmp
+ 1];
2123 struct net2280_dma_regs __iomem
*dma
;
2128 dma
= &dev
->dma
[tmp
];
2129 writel(BIT(DMA_ABORT
), &dma
->dmastat
);
2130 writel(0, &dma
->dmactl
);
2134 writel(~0, &dev
->regs
->irqstat0
), writel(~0, &dev
->regs
->irqstat1
);
2136 if (dev
->bug7734_patched
) {
2137 /* reset, and enable pci */
2138 tmp
= readl(&dev
->regs
->devinit
) |
2140 BIT(FIFO_SOFT_RESET
) |
2141 BIT(USB_SOFT_RESET
) |
2144 writel(tmp
, &dev
->regs
->devinit
);
2147 /* always ep-{1,2,3,4} ... maybe not ep-3 or ep-4 */
2148 INIT_LIST_HEAD(&dev
->gadget
.ep_list
);
2150 for (tmp
= 1; tmp
< dev
->n_ep
; tmp
++)
2151 list_add_tail(&dev
->ep
[tmp
].ep
.ep_list
, &dev
->gadget
.ep_list
);
2155 static void usb_reset(struct net2280
*dev
)
2157 if (dev
->quirks
& PLX_LEGACY
)
2158 return usb_reset_228x(dev
);
2159 return usb_reset_338x(dev
);
2162 static void usb_reinit_228x(struct net2280
*dev
)
2166 /* basic endpoint init */
2167 for (tmp
= 0; tmp
< 7; tmp
++) {
2168 struct net2280_ep
*ep
= &dev
->ep
[tmp
];
2170 ep
->ep
.name
= ep_info_dft
[tmp
].name
;
2171 ep
->ep
.caps
= ep_info_dft
[tmp
].caps
;
2175 if (tmp
> 0 && tmp
<= 4) {
2176 ep
->fifo_size
= 1024;
2177 ep
->dma
= &dev
->dma
[tmp
- 1];
2180 ep
->regs
= &dev
->epregs
[tmp
];
2181 ep
->cfg
= &dev
->epregs
[tmp
];
2182 ep_reset_228x(dev
->regs
, ep
);
2184 usb_ep_set_maxpacket_limit(&dev
->ep
[0].ep
, 64);
2185 usb_ep_set_maxpacket_limit(&dev
->ep
[5].ep
, 64);
2186 usb_ep_set_maxpacket_limit(&dev
->ep
[6].ep
, 64);
2188 dev
->gadget
.ep0
= &dev
->ep
[0].ep
;
2189 dev
->ep
[0].stopped
= 0;
2190 INIT_LIST_HEAD(&dev
->gadget
.ep0
->ep_list
);
2192 /* we want to prevent lowlevel/insecure access from the USB host,
2193 * but erratum 0119 means this enable bit is ignored
2195 for (tmp
= 0; tmp
< 5; tmp
++)
2196 writel(EP_DONTUSE
, &dev
->dep
[tmp
].dep_cfg
);
2199 static void usb_reinit_338x(struct net2280
*dev
)
2203 static const u32 ne
[9] = { 0, 1, 2, 3, 4, 1, 2, 3, 4 };
2204 static const u32 ep_reg_addr
[9] = { 0x00, 0xC0, 0x00, 0xC0, 0x00,
2205 0x00, 0xC0, 0x00, 0xC0 };
2207 /* basic endpoint init */
2208 for (i
= 0; i
< dev
->n_ep
; i
++) {
2209 struct net2280_ep
*ep
= &dev
->ep
[i
];
2211 ep
->ep
.name
= dev
->enhanced_mode
? ep_info_adv
[i
].name
:
2212 ep_info_dft
[i
].name
;
2213 ep
->ep
.caps
= dev
->enhanced_mode
? ep_info_adv
[i
].caps
:
2214 ep_info_dft
[i
].caps
;
2218 if (i
> 0 && i
<= 4)
2219 ep
->dma
= &dev
->dma
[i
- 1];
2221 if (dev
->enhanced_mode
) {
2222 ep
->cfg
= &dev
->epregs
[ne
[i
]];
2224 * Set USB endpoint number, hardware allows same number
2225 * in both directions.
2228 writel(ne
[i
], &ep
->cfg
->ep_cfg
);
2229 ep
->regs
= (struct net2280_ep_regs __iomem
*)
2230 (((void __iomem
*)&dev
->epregs
[ne
[i
]]) +
2233 ep
->cfg
= &dev
->epregs
[i
];
2234 ep
->regs
= &dev
->epregs
[i
];
2237 ep
->fifo_size
= (i
!= 0) ? 2048 : 512;
2239 ep_reset_338x(dev
->regs
, ep
);
2241 usb_ep_set_maxpacket_limit(&dev
->ep
[0].ep
, 512);
2243 dev
->gadget
.ep0
= &dev
->ep
[0].ep
;
2244 dev
->ep
[0].stopped
= 0;
2246 /* Link layer set up */
2247 if (dev
->bug7734_patched
) {
2248 tmp
= readl(&dev
->usb_ext
->usbctl2
) &
2249 ~(BIT(U1_ENABLE
) | BIT(U2_ENABLE
) | BIT(LTM_ENABLE
));
2250 writel(tmp
, &dev
->usb_ext
->usbctl2
);
2253 /* Hardware Defect and Workaround */
2254 val
= readl(&dev
->ll_lfps_regs
->ll_lfps_5
);
2255 val
&= ~(0xf << TIMER_LFPS_6US
);
2256 val
|= 0x5 << TIMER_LFPS_6US
;
2257 writel(val
, &dev
->ll_lfps_regs
->ll_lfps_5
);
2259 val
= readl(&dev
->ll_lfps_regs
->ll_lfps_6
);
2260 val
&= ~(0xffff << TIMER_LFPS_80US
);
2261 val
|= 0x0100 << TIMER_LFPS_80US
;
2262 writel(val
, &dev
->ll_lfps_regs
->ll_lfps_6
);
2265 * AA_AB Errata. Issue 4. Workaround for SuperSpeed USB
2266 * Hot Reset Exit Handshake may Fail in Specific Case using
2267 * Default Register Settings. Workaround for Enumeration test.
2269 val
= readl(&dev
->ll_tsn_regs
->ll_tsn_counters_2
);
2270 val
&= ~(0x1f << HOT_TX_NORESET_TS2
);
2271 val
|= 0x10 << HOT_TX_NORESET_TS2
;
2272 writel(val
, &dev
->ll_tsn_regs
->ll_tsn_counters_2
);
2274 val
= readl(&dev
->ll_tsn_regs
->ll_tsn_counters_3
);
2275 val
&= ~(0x1f << HOT_RX_RESET_TS2
);
2276 val
|= 0x3 << HOT_RX_RESET_TS2
;
2277 writel(val
, &dev
->ll_tsn_regs
->ll_tsn_counters_3
);
2280 * Set Recovery Idle to Recover bit:
2281 * - On SS connections, setting Recovery Idle to Recover Fmw improves
2282 * link robustness with various hosts and hubs.
2283 * - It is safe to set for all connection speeds; all chip revisions.
2284 * - R-M-W to leave other bits undisturbed.
2285 * - Reference PLX TT-7372
2287 val
= readl(&dev
->ll_chicken_reg
->ll_tsn_chicken_bit
);
2288 val
|= BIT(RECOVERY_IDLE_TO_RECOVER_FMW
);
2289 writel(val
, &dev
->ll_chicken_reg
->ll_tsn_chicken_bit
);
2291 INIT_LIST_HEAD(&dev
->gadget
.ep0
->ep_list
);
2293 /* disable dedicated endpoints */
2294 writel(0x0D, &dev
->dep
[0].dep_cfg
);
2295 writel(0x0D, &dev
->dep
[1].dep_cfg
);
2296 writel(0x0E, &dev
->dep
[2].dep_cfg
);
2297 writel(0x0E, &dev
->dep
[3].dep_cfg
);
2298 writel(0x0F, &dev
->dep
[4].dep_cfg
);
2299 writel(0x0C, &dev
->dep
[5].dep_cfg
);
2302 static void usb_reinit(struct net2280
*dev
)
2304 if (dev
->quirks
& PLX_LEGACY
)
2305 return usb_reinit_228x(dev
);
2306 return usb_reinit_338x(dev
);
2309 static void ep0_start_228x(struct net2280
*dev
)
2311 writel(BIT(CLEAR_EP_HIDE_STATUS_PHASE
) |
2312 BIT(CLEAR_NAK_OUT_PACKETS
) |
2313 BIT(CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE
),
2314 &dev
->epregs
[0].ep_rsp
);
2317 * hardware optionally handles a bunch of standard requests
2318 * that the API hides from drivers anyway. have it do so.
2319 * endpoint status/features are handled in software, to
2320 * help pass tests for some dubious behavior.
2322 writel(BIT(SET_TEST_MODE
) |
2324 BIT(DEVICE_SET_CLEAR_DEVICE_REMOTE_WAKEUP
) |
2325 BIT(GET_DEVICE_STATUS
) |
2326 BIT(GET_INTERFACE_STATUS
),
2328 writel(BIT(USB_ROOT_PORT_WAKEUP_ENABLE
) |
2329 BIT(SELF_POWERED_USB_DEVICE
) |
2330 BIT(REMOTE_WAKEUP_SUPPORT
) |
2331 (dev
->softconnect
<< USB_DETECT_ENABLE
) |
2332 BIT(SELF_POWERED_STATUS
),
2335 /* enable irqs so we can see ep0 and general operation */
2336 writel(BIT(SETUP_PACKET_INTERRUPT_ENABLE
) |
2337 BIT(ENDPOINT_0_INTERRUPT_ENABLE
),
2338 &dev
->regs
->pciirqenb0
);
2339 writel(BIT(PCI_INTERRUPT_ENABLE
) |
2340 BIT(PCI_MASTER_ABORT_RECEIVED_INTERRUPT_ENABLE
) |
2341 BIT(PCI_TARGET_ABORT_RECEIVED_INTERRUPT_ENABLE
) |
2342 BIT(PCI_RETRY_ABORT_INTERRUPT_ENABLE
) |
2343 BIT(VBUS_INTERRUPT_ENABLE
) |
2344 BIT(ROOT_PORT_RESET_INTERRUPT_ENABLE
) |
2345 BIT(SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE
),
2346 &dev
->regs
->pciirqenb1
);
2348 /* don't leave any writes posted */
2349 (void) readl(&dev
->usb
->usbctl
);
2352 static void ep0_start_338x(struct net2280
*dev
)
2355 if (dev
->bug7734_patched
)
2356 writel(BIT(CLEAR_NAK_OUT_PACKETS_MODE
) |
2357 BIT(SET_EP_HIDE_STATUS_PHASE
),
2358 &dev
->epregs
[0].ep_rsp
);
2361 * hardware optionally handles a bunch of standard requests
2362 * that the API hides from drivers anyway. have it do so.
2363 * endpoint status/features are handled in software, to
2364 * help pass tests for some dubious behavior.
2366 writel(BIT(SET_ISOCHRONOUS_DELAY
) |
2368 BIT(SET_TEST_MODE
) |
2370 BIT(GET_INTERFACE_STATUS
) |
2371 BIT(GET_DEVICE_STATUS
),
2373 dev
->wakeup_enable
= 1;
2374 writel(BIT(USB_ROOT_PORT_WAKEUP_ENABLE
) |
2375 (dev
->softconnect
<< USB_DETECT_ENABLE
) |
2376 BIT(DEVICE_REMOTE_WAKEUP_ENABLE
),
2379 /* enable irqs so we can see ep0 and general operation */
2380 writel(BIT(SETUP_PACKET_INTERRUPT_ENABLE
) |
2381 BIT(ENDPOINT_0_INTERRUPT_ENABLE
),
2382 &dev
->regs
->pciirqenb0
);
2383 writel(BIT(PCI_INTERRUPT_ENABLE
) |
2384 BIT(ROOT_PORT_RESET_INTERRUPT_ENABLE
) |
2385 BIT(SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE
) |
2386 BIT(VBUS_INTERRUPT_ENABLE
),
2387 &dev
->regs
->pciirqenb1
);
2389 /* don't leave any writes posted */
2390 (void)readl(&dev
->usb
->usbctl
);
2393 static void ep0_start(struct net2280
*dev
)
2395 if (dev
->quirks
& PLX_LEGACY
)
2396 return ep0_start_228x(dev
);
2397 return ep0_start_338x(dev
);
2400 /* when a driver is successfully registered, it will receive
2401 * control requests including set_configuration(), which enables
2402 * non-control requests. then usb traffic follows until a
2403 * disconnect is reported. then a host may connect again, or
2404 * the driver might get unbound.
2406 static int net2280_start(struct usb_gadget
*_gadget
,
2407 struct usb_gadget_driver
*driver
)
2409 struct net2280
*dev
;
2413 /* insist on high speed support from the driver, since
2414 * (dev->usb->xcvrdiag & FORCE_FULL_SPEED_MODE)
2415 * "must not be used in normal operation"
2417 if (!driver
|| driver
->max_speed
< USB_SPEED_HIGH
||
2421 dev
= container_of(_gadget
, struct net2280
, gadget
);
2423 for (i
= 0; i
< dev
->n_ep
; i
++)
2424 dev
->ep
[i
].irqs
= 0;
2426 /* hook up the driver ... */
2427 driver
->driver
.bus
= NULL
;
2428 dev
->driver
= driver
;
2430 retval
= device_create_file(&dev
->pdev
->dev
, &dev_attr_function
);
2433 retval
= device_create_file(&dev
->pdev
->dev
, &dev_attr_queues
);
2437 /* enable host detection and ep0; and we're ready
2438 * for set_configuration as well as eventual disconnect.
2440 net2280_led_active(dev
, 1);
2442 if ((dev
->quirks
& PLX_PCIE
) && !dev
->bug7734_patched
)
2443 defect7374_enable_data_eps_zero(dev
);
2447 /* pci writes may still be posted */
2451 device_remove_file(&dev
->pdev
->dev
, &dev_attr_function
);
2457 static void stop_activity(struct net2280
*dev
, struct usb_gadget_driver
*driver
)
2461 /* don't disconnect if it's not connected */
2462 if (dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
2465 /* stop hardware; prevent new request submissions;
2466 * and kill any outstanding requests.
2469 for (i
= 0; i
< dev
->n_ep
; i
++)
2472 /* report disconnect; the driver is already quiesced */
2474 driver
->disconnect(&dev
->gadget
);
2479 static int net2280_stop(struct usb_gadget
*_gadget
)
2481 struct net2280
*dev
;
2482 unsigned long flags
;
2484 dev
= container_of(_gadget
, struct net2280
, gadget
);
2486 spin_lock_irqsave(&dev
->lock
, flags
);
2487 stop_activity(dev
, NULL
);
2488 spin_unlock_irqrestore(&dev
->lock
, flags
);
2490 net2280_led_active(dev
, 0);
2492 device_remove_file(&dev
->pdev
->dev
, &dev_attr_function
);
2493 device_remove_file(&dev
->pdev
->dev
, &dev_attr_queues
);
2500 /*-------------------------------------------------------------------------*/
2502 /* handle ep0, ep-e, ep-f with 64 byte packets: packet per irq.
2503 * also works for dma-capable endpoints, in pio mode or just
2504 * to manually advance the queue after short OUT transfers.
2506 static void handle_ep_small(struct net2280_ep
*ep
)
2508 struct net2280_request
*req
;
2510 /* 0 error, 1 mid-data, 2 done */
2513 if (!list_empty(&ep
->queue
))
2514 req
= list_entry(ep
->queue
.next
,
2515 struct net2280_request
, queue
);
2519 /* ack all, and handle what we care about */
2520 t
= readl(&ep
->regs
->ep_stat
);
2523 ep_vdbg(ep
->dev
, "%s ack ep_stat %08x, req %p\n",
2524 ep
->ep
.name
, t
, req
? &req
->req
: NULL
);
2526 if (!ep
->is_in
|| (ep
->dev
->quirks
& PLX_2280
))
2527 writel(t
& ~BIT(NAK_OUT_PACKETS
), &ep
->regs
->ep_stat
);
2529 /* Added for 2282 */
2530 writel(t
, &ep
->regs
->ep_stat
);
2532 /* for ep0, monitor token irqs to catch data stage length errors
2533 * and to synchronize on status.
2535 * also, to defer reporting of protocol stalls ... here's where
2536 * data or status first appears, handling stalls here should never
2537 * cause trouble on the host side..
2539 * control requests could be slightly faster without token synch for
2540 * status, but status can jam up that way.
2542 if (unlikely(ep
->num
== 0)) {
2544 /* status; stop NAKing */
2545 if (t
& BIT(DATA_OUT_PING_TOKEN_INTERRUPT
)) {
2546 if (ep
->dev
->protocol_stall
) {
2553 /* reply to extra IN data tokens with a zlp */
2554 } else if (t
& BIT(DATA_IN_TOKEN_INTERRUPT
)) {
2555 if (ep
->dev
->protocol_stall
) {
2559 } else if (ep
->responded
&&
2560 !req
&& !ep
->stopped
)
2561 write_fifo(ep
, NULL
);
2564 /* status; stop NAKing */
2565 if (t
& BIT(DATA_IN_TOKEN_INTERRUPT
)) {
2566 if (ep
->dev
->protocol_stall
) {
2571 /* an extra OUT token is an error */
2572 } else if (((t
& BIT(DATA_OUT_PING_TOKEN_INTERRUPT
)) &&
2574 req
->req
.actual
== req
->req
.length
) ||
2575 (ep
->responded
&& !req
)) {
2576 ep
->dev
->protocol_stall
= 1;
2580 done(ep
, req
, -EOVERFLOW
);
2589 /* manual DMA queue advance after short OUT */
2590 if (likely(ep
->dma
)) {
2591 if (t
& BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT
)) {
2592 struct net2280_request
*stuck_req
= NULL
;
2593 int stopped
= ep
->stopped
;
2598 /* TRANSFERRED works around OUT_DONE erratum 0112.
2599 * we expect (N <= maxpacket) bytes; host wrote M.
2600 * iff (M < N) we won't ever see a DMA interrupt.
2603 for (count
= 0; ; t
= readl(&ep
->regs
->ep_stat
)) {
2605 /* any preceding dma transfers must finish.
2606 * dma handles (M >= N), may empty the queue
2608 num_completed
= scan_dma_completions(ep
);
2609 if (unlikely(list_empty(&ep
->queue
) ||
2610 ep
->out_overflow
)) {
2614 req
= list_entry(ep
->queue
.next
,
2615 struct net2280_request
, queue
);
2617 /* here either (M < N), a "real" short rx;
2618 * or (M == N) and the queue didn't empty
2620 if (likely(t
& BIT(FIFO_EMPTY
))) {
2621 count
= readl(&ep
->dma
->dmacount
);
2622 count
&= DMA_BYTE_COUNT_MASK
;
2623 if (readl(&ep
->dma
->dmadesc
)
2629 /* Escape loop if no dma transfers completed
2630 * after few retries.
2632 if (num_completed
== 0) {
2633 if (stuck_req
== req
&&
2634 readl(&ep
->dma
->dmadesc
) !=
2635 req
->td_dma
&& stuck
++ > 5) {
2637 &ep
->dma
->dmacount
);
2638 count
&= DMA_BYTE_COUNT_MASK
;
2640 ep_dbg(ep
->dev
, "%s escape stuck %d, count %u\n",
2644 } else if (stuck_req
!= req
) {
2656 /* stop DMA, leave ep NAKing */
2657 writel(BIT(DMA_ABORT
), &ep
->dma
->dmastat
);
2658 spin_stop_dma(ep
->dma
);
2661 req
->td
->dmacount
= 0;
2662 t
= readl(&ep
->regs
->ep_avail
);
2663 dma_done(ep
, req
, count
,
2664 (ep
->out_overflow
|| t
)
2668 /* also flush to prevent erratum 0106 trouble */
2669 if (unlikely(ep
->out_overflow
||
2670 (ep
->dev
->chiprev
== 0x0100 &&
2671 ep
->dev
->gadget
.speed
2672 == USB_SPEED_FULL
))) {
2674 ep
->out_overflow
= 0;
2677 /* (re)start dma if needed, stop NAKing */
2678 ep
->stopped
= stopped
;
2679 if (!list_empty(&ep
->queue
))
2682 ep_dbg(ep
->dev
, "%s dma ep_stat %08x ??\n",
2686 /* data packet(s) received (in the fifo, OUT) */
2687 } else if (t
& BIT(DATA_PACKET_RECEIVED_INTERRUPT
)) {
2688 if (read_fifo(ep
, req
) && ep
->num
!= 0)
2691 /* data packet(s) transmitted (IN) */
2692 } else if (t
& BIT(DATA_PACKET_TRANSMITTED_INTERRUPT
)) {
2695 len
= req
->req
.length
- req
->req
.actual
;
2696 if (len
> ep
->ep
.maxpacket
)
2697 len
= ep
->ep
.maxpacket
;
2698 req
->req
.actual
+= len
;
2700 /* if we wrote it all, we're usually done */
2701 /* send zlps until the status stage */
2702 if ((req
->req
.actual
== req
->req
.length
) &&
2703 (!req
->req
.zero
|| len
!= ep
->ep
.maxpacket
) && ep
->num
)
2706 /* there was nothing to do ... */
2707 } else if (mode
== 1)
2712 /* stream endpoints often resubmit/unlink in completion */
2715 /* maybe advance queue to next request */
2717 /* NOTE: net2280 could let gadget driver start the
2718 * status stage later. since not all controllers let
2719 * them control that, the api doesn't (yet) allow it.
2725 if (!list_empty(&ep
->queue
) && !ep
->stopped
)
2726 req
= list_entry(ep
->queue
.next
,
2727 struct net2280_request
, queue
);
2730 if (req
&& !ep
->is_in
)
2731 stop_out_naking(ep
);
2735 /* is there a buffer for the next packet?
2736 * for best streaming performance, make sure there is one.
2738 if (req
&& !ep
->stopped
) {
2740 /* load IN fifo with next packet (may be zlp) */
2741 if (t
& BIT(DATA_PACKET_TRANSMITTED_INTERRUPT
))
2742 write_fifo(ep
, &req
->req
);
2746 static struct net2280_ep
*get_ep_by_addr(struct net2280
*dev
, u16 wIndex
)
2748 struct net2280_ep
*ep
;
2750 if ((wIndex
& USB_ENDPOINT_NUMBER_MASK
) == 0)
2752 list_for_each_entry(ep
, &dev
->gadget
.ep_list
, ep
.ep_list
) {
2753 u8 bEndpointAddress
;
2757 bEndpointAddress
= ep
->desc
->bEndpointAddress
;
2758 if ((wIndex
^ bEndpointAddress
) & USB_DIR_IN
)
2760 if ((wIndex
& 0x0f) == (bEndpointAddress
& 0x0f))
2766 static void defect7374_workaround(struct net2280
*dev
, struct usb_ctrlrequest r
)
2768 u32 scratch
, fsmvalue
;
2769 u32 ack_wait_timeout
, state
;
2771 /* Workaround for Defect 7374 (U1/U2 erroneously rejected): */
2772 scratch
= get_idx_reg(dev
->regs
, SCRATCH
);
2773 fsmvalue
= scratch
& (0xf << DEFECT7374_FSM_FIELD
);
2774 scratch
&= ~(0xf << DEFECT7374_FSM_FIELD
);
2776 if (!((fsmvalue
== DEFECT7374_FSM_WAITING_FOR_CONTROL_READ
) &&
2777 (r
.bRequestType
& USB_DIR_IN
)))
2780 /* This is the first Control Read for this connection: */
2781 if (!(readl(&dev
->usb
->usbstat
) & BIT(SUPER_SPEED_MODE
))) {
2783 * Connection is NOT SS:
2784 * - Connection must be FS or HS.
2785 * - This FSM state should allow workaround software to
2786 * run after the next USB connection.
2788 scratch
|= DEFECT7374_FSM_NON_SS_CONTROL_READ
;
2789 dev
->bug7734_patched
= 1;
2790 goto restore_data_eps
;
2793 /* Connection is SS: */
2794 for (ack_wait_timeout
= 0;
2795 ack_wait_timeout
< DEFECT_7374_NUMBEROF_MAX_WAIT_LOOPS
;
2796 ack_wait_timeout
++) {
2798 state
= readl(&dev
->plregs
->pl_ep_status_1
)
2800 if ((state
>= (ACK_GOOD_NORMAL
<< STATE
)) &&
2801 (state
<= (ACK_GOOD_MORE_ACKS_TO_COME
<< STATE
))) {
2802 scratch
|= DEFECT7374_FSM_SS_CONTROL_READ
;
2803 dev
->bug7734_patched
= 1;
2808 * We have not yet received host's Data Phase ACK
2809 * - Wait and try again.
2811 udelay(DEFECT_7374_PROCESSOR_WAIT_TIME
);
2817 if (ack_wait_timeout
>= DEFECT_7374_NUMBEROF_MAX_WAIT_LOOPS
) {
2818 ep_err(dev
, "FAIL: Defect 7374 workaround waited but failed "
2819 "to detect SS host's data phase ACK.");
2820 ep_err(dev
, "PL_EP_STATUS_1(23:16):.Expected from 0x11 to 0x16"
2821 "got 0x%2.2x.\n", state
>> STATE
);
2823 ep_warn(dev
, "INFO: Defect 7374 workaround waited about\n"
2824 "%duSec for Control Read Data Phase ACK\n",
2825 DEFECT_7374_PROCESSOR_WAIT_TIME
* ack_wait_timeout
);
2830 * Restore data EPs to their pre-workaround settings (disabled,
2831 * initialized, and other details).
2833 defect7374_disable_data_eps(dev
);
2835 set_idx_reg(dev
->regs
, SCRATCH
, scratch
);
2840 static void ep_clear_seqnum(struct net2280_ep
*ep
)
2842 struct net2280
*dev
= ep
->dev
;
2844 static const u32 ep_pl
[9] = { 0, 3, 4, 7, 8, 2, 5, 6, 9 };
2846 val
= readl(&dev
->plregs
->pl_ep_ctrl
) & ~0x1f;
2847 val
|= ep_pl
[ep
->num
];
2848 writel(val
, &dev
->plregs
->pl_ep_ctrl
);
2849 val
|= BIT(SEQUENCE_NUMBER_RESET
);
2850 writel(val
, &dev
->plregs
->pl_ep_ctrl
);
2855 static void handle_stat0_irqs_superspeed(struct net2280
*dev
,
2856 struct net2280_ep
*ep
, struct usb_ctrlrequest r
)
2860 #define w_value le16_to_cpu(r.wValue)
2861 #define w_index le16_to_cpu(r.wIndex)
2862 #define w_length le16_to_cpu(r.wLength)
2864 switch (r
.bRequest
) {
2865 struct net2280_ep
*e
;
2868 case USB_REQ_SET_CONFIGURATION
:
2869 dev
->addressed_state
= !w_value
;
2872 case USB_REQ_GET_STATUS
:
2873 switch (r
.bRequestType
) {
2874 case (USB_DIR_IN
| USB_TYPE_STANDARD
| USB_RECIP_DEVICE
):
2875 status
= dev
->wakeup_enable
? 0x02 : 0x00;
2876 if (dev
->gadget
.is_selfpowered
)
2878 status
|= (dev
->u1_enable
<< 2 | dev
->u2_enable
<< 3 |
2879 dev
->ltm_enable
<< 4);
2880 writel(0, &dev
->epregs
[0].ep_irqenb
);
2881 set_fifo_bytecount(ep
, sizeof(status
));
2882 writel((__force u32
) status
, &dev
->epregs
[0].ep_data
);
2883 allow_status_338x(ep
);
2886 case (USB_DIR_IN
| USB_TYPE_STANDARD
| USB_RECIP_ENDPOINT
):
2887 e
= get_ep_by_addr(dev
, w_index
);
2890 status
= readl(&e
->regs
->ep_rsp
) &
2891 BIT(CLEAR_ENDPOINT_HALT
);
2892 writel(0, &dev
->epregs
[0].ep_irqenb
);
2893 set_fifo_bytecount(ep
, sizeof(status
));
2894 writel((__force u32
) status
, &dev
->epregs
[0].ep_data
);
2895 allow_status_338x(ep
);
2903 case USB_REQ_CLEAR_FEATURE
:
2904 switch (r
.bRequestType
) {
2905 case (USB_DIR_OUT
| USB_TYPE_STANDARD
| USB_RECIP_DEVICE
):
2906 if (!dev
->addressed_state
) {
2908 case USB_DEVICE_U1_ENABLE
:
2910 writel(readl(&dev
->usb_ext
->usbctl2
) &
2912 &dev
->usb_ext
->usbctl2
);
2913 allow_status_338x(ep
);
2914 goto next_endpoints3
;
2916 case USB_DEVICE_U2_ENABLE
:
2918 writel(readl(&dev
->usb_ext
->usbctl2
) &
2920 &dev
->usb_ext
->usbctl2
);
2921 allow_status_338x(ep
);
2922 goto next_endpoints3
;
2924 case USB_DEVICE_LTM_ENABLE
:
2925 dev
->ltm_enable
= 0;
2926 writel(readl(&dev
->usb_ext
->usbctl2
) &
2928 &dev
->usb_ext
->usbctl2
);
2929 allow_status_338x(ep
);
2930 goto next_endpoints3
;
2936 if (w_value
== USB_DEVICE_REMOTE_WAKEUP
) {
2937 dev
->wakeup_enable
= 0;
2938 writel(readl(&dev
->usb
->usbctl
) &
2939 ~BIT(DEVICE_REMOTE_WAKEUP_ENABLE
),
2941 allow_status_338x(ep
);
2946 case (USB_DIR_OUT
| USB_TYPE_STANDARD
| USB_RECIP_ENDPOINT
):
2947 e
= get_ep_by_addr(dev
, w_index
);
2950 if (w_value
!= USB_ENDPOINT_HALT
)
2952 ep_vdbg(dev
, "%s clear halt\n", e
->ep
.name
);
2954 * Workaround for SS SeqNum not cleared via
2955 * Endpoint Halt (Clear) bit. select endpoint
2959 if (!list_empty(&e
->queue
) && e
->td_dma
)
2969 case USB_REQ_SET_FEATURE
:
2970 switch (r
.bRequestType
) {
2971 case (USB_DIR_OUT
| USB_TYPE_STANDARD
| USB_RECIP_DEVICE
):
2972 if (!dev
->addressed_state
) {
2974 case USB_DEVICE_U1_ENABLE
:
2976 writel(readl(&dev
->usb_ext
->usbctl2
) |
2978 &dev
->usb_ext
->usbctl2
);
2979 allow_status_338x(ep
);
2980 goto next_endpoints3
;
2982 case USB_DEVICE_U2_ENABLE
:
2984 writel(readl(&dev
->usb_ext
->usbctl2
) |
2986 &dev
->usb_ext
->usbctl2
);
2987 allow_status_338x(ep
);
2988 goto next_endpoints3
;
2990 case USB_DEVICE_LTM_ENABLE
:
2991 dev
->ltm_enable
= 1;
2992 writel(readl(&dev
->usb_ext
->usbctl2
) |
2994 &dev
->usb_ext
->usbctl2
);
2995 allow_status_338x(ep
);
2996 goto next_endpoints3
;
3002 if (w_value
== USB_DEVICE_REMOTE_WAKEUP
) {
3003 dev
->wakeup_enable
= 1;
3004 writel(readl(&dev
->usb
->usbctl
) |
3005 BIT(DEVICE_REMOTE_WAKEUP_ENABLE
),
3007 allow_status_338x(ep
);
3012 case (USB_DIR_OUT
| USB_TYPE_STANDARD
| USB_RECIP_ENDPOINT
):
3013 e
= get_ep_by_addr(dev
, w_index
);
3014 if (!e
|| (w_value
!= USB_ENDPOINT_HALT
))
3018 ep
->dev
->protocol_stall
= 1;
3024 allow_status_338x(ep
);
3035 ep_vdbg(dev
, "setup %02x.%02x v%04x i%04x l%04x ep_cfg %08x\n",
3036 r
.bRequestType
, r
.bRequest
,
3037 w_value
, w_index
, w_length
,
3038 readl(&ep
->cfg
->ep_cfg
));
3041 spin_unlock(&dev
->lock
);
3042 tmp
= dev
->driver
->setup(&dev
->gadget
, &r
);
3043 spin_lock(&dev
->lock
);
3047 ep_vdbg(dev
, "req %02x.%02x protocol STALL; stat %d\n",
3048 r
.bRequestType
, r
.bRequest
, tmp
);
3049 dev
->protocol_stall
= 1;
3050 /* TD 9.9 Halt Endpoint test. TD 9.22 Set feature test */
3063 static void usb338x_handle_ep_intr(struct net2280
*dev
, u32 stat0
)
3068 for (index
= 0; index
< ARRAY_SIZE(ep_bit
); index
++) {
3069 bit
= BIT(ep_bit
[index
]);
3079 handle_ep_small(&dev
->ep
[index
]);
3083 static void handle_stat0_irqs(struct net2280
*dev
, u32 stat
)
3085 struct net2280_ep
*ep
;
3088 /* most of these don't need individual acks */
3089 stat
&= ~BIT(INTA_ASSERTED
);
3092 /* ep_dbg(dev, "irqstat0 %04x\n", stat); */
3094 /* starting a control request? */
3095 if (unlikely(stat
& BIT(SETUP_PACKET_INTERRUPT
))) {
3098 struct usb_ctrlrequest r
;
3101 struct net2280_request
*req
;
3103 if (dev
->gadget
.speed
== USB_SPEED_UNKNOWN
) {
3104 u32 val
= readl(&dev
->usb
->usbstat
);
3105 if (val
& BIT(SUPER_SPEED
)) {
3106 dev
->gadget
.speed
= USB_SPEED_SUPER
;
3107 usb_ep_set_maxpacket_limit(&dev
->ep
[0].ep
,
3108 EP0_SS_MAX_PACKET_SIZE
);
3109 } else if (val
& BIT(HIGH_SPEED
)) {
3110 dev
->gadget
.speed
= USB_SPEED_HIGH
;
3111 usb_ep_set_maxpacket_limit(&dev
->ep
[0].ep
,
3112 EP0_HS_MAX_PACKET_SIZE
);
3114 dev
->gadget
.speed
= USB_SPEED_FULL
;
3115 usb_ep_set_maxpacket_limit(&dev
->ep
[0].ep
,
3116 EP0_HS_MAX_PACKET_SIZE
);
3118 net2280_led_speed(dev
, dev
->gadget
.speed
);
3120 usb_speed_string(dev
->gadget
.speed
));
3126 /* make sure any leftover request state is cleared */
3127 stat
&= ~BIT(ENDPOINT_0_INTERRUPT
);
3128 while (!list_empty(&ep
->queue
)) {
3129 req
= list_entry(ep
->queue
.next
,
3130 struct net2280_request
, queue
);
3131 done(ep
, req
, (req
->req
.actual
== req
->req
.length
)
3135 dev
->protocol_stall
= 0;
3136 if (!(dev
->quirks
& PLX_PCIE
)) {
3137 if (ep
->dev
->quirks
& PLX_2280
)
3138 tmp
= BIT(FIFO_OVERFLOW
) |
3139 BIT(FIFO_UNDERFLOW
);
3143 writel(tmp
| BIT(TIMEOUT
) |
3144 BIT(USB_STALL_SENT
) |
3145 BIT(USB_IN_NAK_SENT
) |
3146 BIT(USB_IN_ACK_RCVD
) |
3147 BIT(USB_OUT_PING_NAK_SENT
) |
3148 BIT(USB_OUT_ACK_SENT
) |
3149 BIT(SHORT_PACKET_OUT_DONE_INTERRUPT
) |
3150 BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT
) |
3151 BIT(DATA_PACKET_RECEIVED_INTERRUPT
) |
3152 BIT(DATA_PACKET_TRANSMITTED_INTERRUPT
) |
3153 BIT(DATA_OUT_PING_TOKEN_INTERRUPT
) |
3154 BIT(DATA_IN_TOKEN_INTERRUPT
),
3155 &ep
->regs
->ep_stat
);
3157 u
.raw
[0] = readl(&dev
->usb
->setup0123
);
3158 u
.raw
[1] = readl(&dev
->usb
->setup4567
);
3160 cpu_to_le32s(&u
.raw
[0]);
3161 cpu_to_le32s(&u
.raw
[1]);
3163 if ((dev
->quirks
& PLX_PCIE
) && !dev
->bug7734_patched
)
3164 defect7374_workaround(dev
, u
.r
);
3168 #define w_value le16_to_cpu(u.r.wValue)
3169 #define w_index le16_to_cpu(u.r.wIndex)
3170 #define w_length le16_to_cpu(u.r.wLength)
3173 writel(BIT(SETUP_PACKET_INTERRUPT
), &dev
->regs
->irqstat0
);
3174 stat
^= BIT(SETUP_PACKET_INTERRUPT
);
3176 /* watch control traffic at the token level, and force
3177 * synchronization before letting the status stage happen.
3178 * FIXME ignore tokens we'll NAK, until driver responds.
3179 * that'll mean a lot less irqs for some drivers.
3181 ep
->is_in
= (u
.r
.bRequestType
& USB_DIR_IN
) != 0;
3183 scratch
= BIT(DATA_PACKET_TRANSMITTED_INTERRUPT
) |
3184 BIT(DATA_OUT_PING_TOKEN_INTERRUPT
) |
3185 BIT(DATA_IN_TOKEN_INTERRUPT
);
3186 stop_out_naking(ep
);
3188 scratch
= BIT(DATA_PACKET_RECEIVED_INTERRUPT
) |
3189 BIT(DATA_OUT_PING_TOKEN_INTERRUPT
) |
3190 BIT(DATA_IN_TOKEN_INTERRUPT
);
3191 writel(scratch
, &dev
->epregs
[0].ep_irqenb
);
3193 /* we made the hardware handle most lowlevel requests;
3194 * everything else goes uplevel to the gadget code.
3198 if (dev
->gadget
.speed
== USB_SPEED_SUPER
) {
3199 handle_stat0_irqs_superspeed(dev
, ep
, u
.r
);
3200 goto next_endpoints
;
3203 switch (u
.r
.bRequest
) {
3204 case USB_REQ_GET_STATUS
: {
3205 struct net2280_ep
*e
;
3208 /* hw handles device and interface status */
3209 if (u
.r
.bRequestType
!= (USB_DIR_IN
|USB_RECIP_ENDPOINT
))
3211 e
= get_ep_by_addr(dev
, w_index
);
3212 if (!e
|| w_length
> 2)
3215 if (readl(&e
->regs
->ep_rsp
) & BIT(SET_ENDPOINT_HALT
))
3216 status
= cpu_to_le32(1);
3218 status
= cpu_to_le32(0);
3220 /* don't bother with a request object! */
3221 writel(0, &dev
->epregs
[0].ep_irqenb
);
3222 set_fifo_bytecount(ep
, w_length
);
3223 writel((__force u32
)status
, &dev
->epregs
[0].ep_data
);
3225 ep_vdbg(dev
, "%s stat %02x\n", ep
->ep
.name
, status
);
3226 goto next_endpoints
;
3229 case USB_REQ_CLEAR_FEATURE
: {
3230 struct net2280_ep
*e
;
3232 /* hw handles device features */
3233 if (u
.r
.bRequestType
!= USB_RECIP_ENDPOINT
)
3235 if (w_value
!= USB_ENDPOINT_HALT
|| w_length
!= 0)
3237 e
= get_ep_by_addr(dev
, w_index
);
3241 ep_vdbg(dev
, "%s wedged, halt not cleared\n",
3244 ep_vdbg(dev
, "%s clear halt\n", e
->ep
.name
);
3246 if ((ep
->dev
->quirks
& PLX_PCIE
) &&
3247 !list_empty(&e
->queue
) && e
->td_dma
)
3251 goto next_endpoints
;
3254 case USB_REQ_SET_FEATURE
: {
3255 struct net2280_ep
*e
;
3257 /* hw handles device features */
3258 if (u
.r
.bRequestType
!= USB_RECIP_ENDPOINT
)
3260 if (w_value
!= USB_ENDPOINT_HALT
|| w_length
!= 0)
3262 e
= get_ep_by_addr(dev
, w_index
);
3265 if (e
->ep
.name
== ep0name
)
3268 if ((dev
->quirks
& PLX_PCIE
) && e
->dma
)
3271 ep_vdbg(dev
, "%s set halt\n", ep
->ep
.name
);
3272 goto next_endpoints
;
3277 ep_vdbg(dev
, "setup %02x.%02x v%04x i%04x l%04x "
3279 u
.r
.bRequestType
, u
.r
.bRequest
,
3280 w_value
, w_index
, w_length
,
3281 readl(&ep
->cfg
->ep_cfg
));
3283 spin_unlock(&dev
->lock
);
3284 tmp
= dev
->driver
->setup(&dev
->gadget
, &u
.r
);
3285 spin_lock(&dev
->lock
);
3288 /* stall ep0 on error */
3291 ep_vdbg(dev
, "req %02x.%02x protocol STALL; stat %d\n",
3292 u
.r
.bRequestType
, u
.r
.bRequest
, tmp
);
3293 dev
->protocol_stall
= 1;
3296 /* some in/out token irq should follow; maybe stall then.
3297 * driver must queue a request (even zlp) or halt ep0
3298 * before the host times out.
3307 if ((dev
->quirks
& PLX_PCIE
) && dev
->enhanced_mode
) {
3308 u32 mask
= (BIT(ENDPOINT_0_INTERRUPT
) |
3309 USB3380_IRQSTAT0_EP_INTR_MASK_IN
|
3310 USB3380_IRQSTAT0_EP_INTR_MASK_OUT
);
3313 usb338x_handle_ep_intr(dev
, stat
& mask
);
3317 /* endpoint data irq ? */
3318 scratch
= stat
& 0x7f;
3320 for (num
= 0; scratch
; num
++) {
3323 /* do this endpoint's FIFO and queue need tending? */
3325 if ((scratch
& t
) == 0)
3330 handle_ep_small(ep
);
3335 ep_dbg(dev
, "unhandled irqstat0 %08x\n", stat
);
3338 #define DMA_INTERRUPTS (BIT(DMA_D_INTERRUPT) | \
3339 BIT(DMA_C_INTERRUPT) | \
3340 BIT(DMA_B_INTERRUPT) | \
3341 BIT(DMA_A_INTERRUPT))
3342 #define PCI_ERROR_INTERRUPTS ( \
3343 BIT(PCI_MASTER_ABORT_RECEIVED_INTERRUPT) | \
3344 BIT(PCI_TARGET_ABORT_RECEIVED_INTERRUPT) | \
3345 BIT(PCI_RETRY_ABORT_INTERRUPT))
3347 static void handle_stat1_irqs(struct net2280
*dev
, u32 stat
)
3349 struct net2280_ep
*ep
;
3350 u32 tmp
, num
, mask
, scratch
;
3352 /* after disconnect there's nothing else to do! */
3353 tmp
= BIT(VBUS_INTERRUPT
) | BIT(ROOT_PORT_RESET_INTERRUPT
);
3354 mask
= BIT(SUPER_SPEED
) | BIT(HIGH_SPEED
) | BIT(FULL_SPEED
);
3356 /* VBUS disconnect is indicated by VBUS_PIN and VBUS_INTERRUPT set.
3357 * Root Port Reset is indicated by ROOT_PORT_RESET_INTERRUPT set and
3358 * both HIGH_SPEED and FULL_SPEED clear (as ROOT_PORT_RESET_INTERRUPT
3359 * only indicates a change in the reset state).
3363 bool disconnect
= false;
3366 * Ignore disconnects and resets if the speed hasn't been set.
3367 * VBUS can bounce and there's always an initial reset.
3369 writel(tmp
, &dev
->regs
->irqstat1
);
3370 if (dev
->gadget
.speed
!= USB_SPEED_UNKNOWN
) {
3371 if ((stat
& BIT(VBUS_INTERRUPT
)) &&
3372 (readl(&dev
->usb
->usbctl
) &
3373 BIT(VBUS_PIN
)) == 0) {
3375 ep_dbg(dev
, "disconnect %s\n",
3376 dev
->driver
->driver
.name
);
3377 } else if ((stat
& BIT(ROOT_PORT_RESET_INTERRUPT
)) &&
3378 (readl(&dev
->usb
->usbstat
) & mask
)
3381 ep_dbg(dev
, "reset %s\n",
3382 dev
->driver
->driver
.name
);
3385 if (disconnect
|| reset
) {
3386 stop_activity(dev
, dev
->driver
);
3389 usb_gadget_udc_reset
3390 (&dev
->gadget
, dev
->driver
);
3392 (dev
->driver
->disconnect
)
3399 /* vBUS can bounce ... one of many reasons to ignore the
3400 * notion of hotplug events on bus connect/disconnect!
3406 /* NOTE: chip stays in PCI D0 state for now, but it could
3407 * enter D1 to save more power
3409 tmp
= BIT(SUSPEND_REQUEST_CHANGE_INTERRUPT
);
3411 writel(tmp
, &dev
->regs
->irqstat1
);
3412 if (stat
& BIT(SUSPEND_REQUEST_INTERRUPT
)) {
3413 if (dev
->driver
->suspend
)
3414 dev
->driver
->suspend(&dev
->gadget
);
3415 if (!enable_suspend
)
3416 stat
&= ~BIT(SUSPEND_REQUEST_INTERRUPT
);
3418 if (dev
->driver
->resume
)
3419 dev
->driver
->resume(&dev
->gadget
);
3420 /* at high speed, note erratum 0133 */
3425 /* clear any other status/irqs */
3427 writel(stat
, &dev
->regs
->irqstat1
);
3429 /* some status we can just ignore */
3430 if (dev
->quirks
& PLX_2280
)
3431 stat
&= ~(BIT(CONTROL_STATUS_INTERRUPT
) |
3432 BIT(SUSPEND_REQUEST_INTERRUPT
) |
3433 BIT(RESUME_INTERRUPT
) |
3434 BIT(SOF_INTERRUPT
));
3436 stat
&= ~(BIT(CONTROL_STATUS_INTERRUPT
) |
3437 BIT(RESUME_INTERRUPT
) |
3438 BIT(SOF_DOWN_INTERRUPT
) |
3439 BIT(SOF_INTERRUPT
));
3443 /* ep_dbg(dev, "irqstat1 %08x\n", stat);*/
3445 /* DMA status, for ep-{a,b,c,d} */
3446 scratch
= stat
& DMA_INTERRUPTS
;
3447 stat
&= ~DMA_INTERRUPTS
;
3449 for (num
= 0; scratch
; num
++) {
3450 struct net2280_dma_regs __iomem
*dma
;
3453 if ((tmp
& scratch
) == 0)
3457 ep
= &dev
->ep
[num
+ 1];
3463 /* clear ep's dma status */
3464 tmp
= readl(&dma
->dmastat
);
3465 writel(tmp
, &dma
->dmastat
);
3468 if (dev
->quirks
& PLX_PCIE
) {
3469 u32 r_dmacount
= readl(&dma
->dmacount
);
3470 if (!ep
->is_in
&& (r_dmacount
& 0x00FFFFFF) &&
3471 (tmp
& BIT(DMA_TRANSACTION_DONE_INTERRUPT
)))
3475 if (!(tmp
& BIT(DMA_TRANSACTION_DONE_INTERRUPT
))) {
3476 ep_dbg(ep
->dev
, "%s no xact done? %08x\n",
3482 /* OUT transfers terminate when the data from the
3483 * host is in our memory. Process whatever's done.
3484 * On this path, we know transfer's last packet wasn't
3485 * less than req->length. NAK_OUT_PACKETS may be set,
3486 * or the FIFO may already be holding new packets.
3488 * IN transfers can linger in the FIFO for a very
3489 * long time ... we ignore that for now, accounting
3490 * precisely (like PIO does) needs per-packet irqs
3492 scan_dma_completions(ep
);
3494 /* disable dma on inactive queues; else maybe restart */
3495 if (!list_empty(&ep
->queue
)) {
3496 tmp
= readl(&dma
->dmactl
);
3502 /* NOTE: there are other PCI errors we might usefully notice.
3503 * if they appear very often, here's where to try recovering.
3505 if (stat
& PCI_ERROR_INTERRUPTS
) {
3506 ep_err(dev
, "pci dma error; stat %08x\n", stat
);
3507 stat
&= ~PCI_ERROR_INTERRUPTS
;
3508 /* these are fatal errors, but "maybe" they won't
3511 stop_activity(dev
, dev
->driver
);
3517 ep_dbg(dev
, "unhandled irqstat1 %08x\n", stat
);
3520 static irqreturn_t
net2280_irq(int irq
, void *_dev
)
3522 struct net2280
*dev
= _dev
;
3524 /* shared interrupt, not ours */
3525 if ((dev
->quirks
& PLX_LEGACY
) &&
3526 (!(readl(&dev
->regs
->irqstat0
) & BIT(INTA_ASSERTED
))))
3529 spin_lock(&dev
->lock
);
3531 /* handle disconnect, dma, and more */
3532 handle_stat1_irqs(dev
, readl(&dev
->regs
->irqstat1
));
3534 /* control requests and PIO */
3535 handle_stat0_irqs(dev
, readl(&dev
->regs
->irqstat0
));
3537 if (dev
->quirks
& PLX_PCIE
) {
3538 /* re-enable interrupt to trigger any possible new interrupt */
3539 u32 pciirqenb1
= readl(&dev
->regs
->pciirqenb1
);
3540 writel(pciirqenb1
& 0x7FFFFFFF, &dev
->regs
->pciirqenb1
);
3541 writel(pciirqenb1
, &dev
->regs
->pciirqenb1
);
3544 spin_unlock(&dev
->lock
);
3549 /*-------------------------------------------------------------------------*/
3551 static void gadget_release(struct device
*_dev
)
3553 struct net2280
*dev
= dev_get_drvdata(_dev
);
3558 /* tear down the binding between this driver and the pci device */
3560 static void net2280_remove(struct pci_dev
*pdev
)
3562 struct net2280
*dev
= pci_get_drvdata(pdev
);
3564 usb_del_gadget_udc(&dev
->gadget
);
3566 BUG_ON(dev
->driver
);
3568 /* then clean up the resources we allocated during probe() */
3569 net2280_led_shutdown(dev
);
3570 if (dev
->requests
) {
3572 for (i
= 1; i
< 5; i
++) {
3573 if (!dev
->ep
[i
].dummy
)
3575 pci_pool_free(dev
->requests
, dev
->ep
[i
].dummy
,
3578 pci_pool_destroy(dev
->requests
);
3581 free_irq(pdev
->irq
, dev
);
3582 if (dev
->quirks
& PLX_PCIE
)
3583 pci_disable_msi(pdev
);
3587 release_mem_region(pci_resource_start(pdev
, 0),
3588 pci_resource_len(pdev
, 0));
3590 pci_disable_device(pdev
);
3591 device_remove_file(&pdev
->dev
, &dev_attr_registers
);
3593 ep_info(dev
, "unbind\n");
3596 /* wrap this driver around the specified device, but
3597 * don't respond over USB until a gadget driver binds to us.
3600 static int net2280_probe(struct pci_dev
*pdev
, const struct pci_device_id
*id
)
3602 struct net2280
*dev
;
3603 unsigned long resource
, len
;
3604 void __iomem
*base
= NULL
;
3607 /* alloc, and start init */
3608 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
3614 pci_set_drvdata(pdev
, dev
);
3615 spin_lock_init(&dev
->lock
);
3616 dev
->quirks
= id
->driver_data
;
3618 dev
->gadget
.ops
= &net2280_ops
;
3619 dev
->gadget
.max_speed
= (dev
->quirks
& PLX_SUPERSPEED
) ?
3620 USB_SPEED_SUPER
: USB_SPEED_HIGH
;
3622 /* the "gadget" abstracts/virtualizes the controller */
3623 dev
->gadget
.name
= driver_name
;
3625 /* now all the pci goodies ... */
3626 if (pci_enable_device(pdev
) < 0) {
3632 /* BAR 0 holds all the registers
3633 * BAR 1 is 8051 memory; unused here (note erratum 0103)
3634 * BAR 2 is fifo memory; unused here
3636 resource
= pci_resource_start(pdev
, 0);
3637 len
= pci_resource_len(pdev
, 0);
3638 if (!request_mem_region(resource
, len
, driver_name
)) {
3639 ep_dbg(dev
, "controller already in use\n");
3645 /* FIXME provide firmware download interface to put
3646 * 8051 code into the chip, e.g. to turn on PCI PM.
3649 base
= ioremap_nocache(resource
, len
);
3651 ep_dbg(dev
, "can't map memory\n");
3655 dev
->regs
= (struct net2280_regs __iomem
*) base
;
3656 dev
->usb
= (struct net2280_usb_regs __iomem
*) (base
+ 0x0080);
3657 dev
->pci
= (struct net2280_pci_regs __iomem
*) (base
+ 0x0100);
3658 dev
->dma
= (struct net2280_dma_regs __iomem
*) (base
+ 0x0180);
3659 dev
->dep
= (struct net2280_dep_regs __iomem
*) (base
+ 0x0200);
3660 dev
->epregs
= (struct net2280_ep_regs __iomem
*) (base
+ 0x0300);
3662 if (dev
->quirks
& PLX_PCIE
) {
3665 dev
->usb_ext
= (struct usb338x_usb_ext_regs __iomem
*)
3667 dev
->llregs
= (struct usb338x_ll_regs __iomem
*)
3669 dev
->ll_lfps_regs
= (struct usb338x_ll_lfps_regs __iomem
*)
3671 dev
->ll_tsn_regs
= (struct usb338x_ll_tsn_regs __iomem
*)
3673 dev
->ll_chicken_reg
= (struct usb338x_ll_chi_regs __iomem
*)
3675 dev
->plregs
= (struct usb338x_pl_regs __iomem
*)
3677 usbstat
= readl(&dev
->usb
->usbstat
);
3678 dev
->enhanced_mode
= !!(usbstat
& BIT(11));
3679 dev
->n_ep
= (dev
->enhanced_mode
) ? 9 : 5;
3680 /* put into initial config, link up all endpoints */
3681 fsmvalue
= get_idx_reg(dev
->regs
, SCRATCH
) &
3682 (0xf << DEFECT7374_FSM_FIELD
);
3683 /* See if firmware needs to set up for workaround: */
3684 if (fsmvalue
== DEFECT7374_FSM_SS_CONTROL_READ
) {
3685 dev
->bug7734_patched
= 1;
3686 writel(0, &dev
->usb
->usbctl
);
3688 dev
->bug7734_patched
= 0;
3690 dev
->enhanced_mode
= 0;
3692 /* put into initial config, link up all endpoints */
3693 writel(0, &dev
->usb
->usbctl
);
3699 /* irq setup after old hardware is cleaned up */
3701 ep_err(dev
, "No IRQ. Check PCI setup!\n");
3706 if (dev
->quirks
& PLX_PCIE
)
3707 if (pci_enable_msi(pdev
))
3708 ep_err(dev
, "Failed to enable MSI mode\n");
3710 if (request_irq(pdev
->irq
, net2280_irq
, IRQF_SHARED
,
3711 driver_name
, dev
)) {
3712 ep_err(dev
, "request interrupt %d failed\n", pdev
->irq
);
3719 /* NOTE: we know only the 32 LSBs of dma addresses may be nonzero */
3720 dev
->requests
= pci_pool_create("requests", pdev
,
3721 sizeof(struct net2280_dma
),
3722 0 /* no alignment requirements */,
3723 0 /* or page-crossing issues */);
3724 if (!dev
->requests
) {
3725 ep_dbg(dev
, "can't get request pool\n");
3729 for (i
= 1; i
< 5; i
++) {
3730 struct net2280_dma
*td
;
3732 td
= pci_pool_alloc(dev
->requests
, GFP_KERNEL
,
3733 &dev
->ep
[i
].td_dma
);
3735 ep_dbg(dev
, "can't get dummy %d\n", i
);
3739 td
->dmacount
= 0; /* not VALID */
3740 td
->dmadesc
= td
->dmaaddr
;
3741 dev
->ep
[i
].dummy
= td
;
3744 /* enable lower-overhead pci memory bursts during DMA */
3745 if (dev
->quirks
& PLX_LEGACY
)
3746 writel(BIT(DMA_MEMORY_WRITE_AND_INVALIDATE_ENABLE
) |
3748 * 256 write retries may not be enough...
3749 BIT(PCI_RETRY_ABORT_ENABLE) |
3751 BIT(DMA_READ_MULTIPLE_ENABLE
) |
3752 BIT(DMA_READ_LINE_ENABLE
),
3753 &dev
->pci
->pcimstctl
);
3754 /* erratum 0115 shouldn't appear: Linux inits PCI_LATENCY_TIMER */
3755 pci_set_master(pdev
);
3756 pci_try_set_mwi(pdev
);
3758 /* ... also flushes any posted pci writes */
3759 dev
->chiprev
= get_idx_reg(dev
->regs
, REG_CHIPREV
) & 0xffff;
3762 ep_info(dev
, "%s\n", driver_desc
);
3763 ep_info(dev
, "irq %d, pci mem %p, chip rev %04x\n",
3764 pdev
->irq
, base
, dev
->chiprev
);
3765 ep_info(dev
, "version: " DRIVER_VERSION
"; %s\n",
3766 dev
->enhanced_mode
? "enhanced mode" : "legacy mode");
3767 retval
= device_create_file(&pdev
->dev
, &dev_attr_registers
);
3771 retval
= usb_add_gadget_udc_release(&pdev
->dev
, &dev
->gadget
,
3779 net2280_remove(pdev
);
3783 /* make sure the board is quiescent; otherwise it will continue
3784 * generating IRQs across the upcoming reboot.
3787 static void net2280_shutdown(struct pci_dev
*pdev
)
3789 struct net2280
*dev
= pci_get_drvdata(pdev
);
3792 writel(0, &dev
->regs
->pciirqenb0
);
3793 writel(0, &dev
->regs
->pciirqenb1
);
3795 /* disable the pullup so the host will think we're gone */
3796 writel(0, &dev
->usb
->usbctl
);
3801 /*-------------------------------------------------------------------------*/
3803 static const struct pci_device_id pci_ids
[] = { {
3804 .class = PCI_CLASS_SERIAL_USB_DEVICE
,
3806 .vendor
= PCI_VENDOR_ID_PLX_LEGACY
,
3808 .subvendor
= PCI_ANY_ID
,
3809 .subdevice
= PCI_ANY_ID
,
3810 .driver_data
= PLX_LEGACY
| PLX_2280
,
3812 .class = PCI_CLASS_SERIAL_USB_DEVICE
,
3814 .vendor
= PCI_VENDOR_ID_PLX_LEGACY
,
3816 .subvendor
= PCI_ANY_ID
,
3817 .subdevice
= PCI_ANY_ID
,
3818 .driver_data
= PLX_LEGACY
,
3821 .class = PCI_CLASS_SERIAL_USB_DEVICE
,
3823 .vendor
= PCI_VENDOR_ID_PLX
,
3825 .subvendor
= PCI_ANY_ID
,
3826 .subdevice
= PCI_ANY_ID
,
3827 .driver_data
= PLX_PCIE
,
3830 .class = ((PCI_CLASS_SERIAL_USB
<< 8) | 0xfe),
3832 .vendor
= PCI_VENDOR_ID_PLX
,
3834 .subvendor
= PCI_ANY_ID
,
3835 .subdevice
= PCI_ANY_ID
,
3836 .driver_data
= PLX_PCIE
| PLX_SUPERSPEED
,
3839 .class = PCI_CLASS_SERIAL_USB_DEVICE
,
3841 .vendor
= PCI_VENDOR_ID_PLX
,
3843 .subvendor
= PCI_ANY_ID
,
3844 .subdevice
= PCI_ANY_ID
,
3845 .driver_data
= PLX_PCIE
| PLX_SUPERSPEED
,
3847 { /* end: all zeroes */ }
3849 MODULE_DEVICE_TABLE(pci
, pci_ids
);
3851 /* pci driver glue; this is a "new style" PCI driver module */
3852 static struct pci_driver net2280_pci_driver
= {
3853 .name
= (char *) driver_name
,
3854 .id_table
= pci_ids
,
3856 .probe
= net2280_probe
,
3857 .remove
= net2280_remove
,
3858 .shutdown
= net2280_shutdown
,
3860 /* FIXME add power management support */
3863 module_pci_driver(net2280_pci_driver
);
3865 MODULE_DESCRIPTION(DRIVER_DESC
);
3866 MODULE_AUTHOR("David Brownell");
3867 MODULE_LICENSE("GPL");