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