usb: gadget: net2280: use ep_autoconfig compatible names in advance mode
[deliverable/linux.git] / drivers / usb / gadget / udc / net2280.c
1 /*
2 * Driver for the PLX NET2280 USB device controller.
3 * Specs and errata are available from <http://www.plxtech.com>.
4 *
5 * PLX Technology Inc. (formerly NetChip Technology) supported the
6 * development of this driver.
7 *
8 *
9 * CODE STATUS HIGHLIGHTS
10 *
11 * This driver should work well with most "gadget" drivers, including
12 * the Mass Storage, Serial, and Ethernet/RNDIS gadget drivers
13 * as well as Gadget Zero and Gadgetfs.
14 *
15 * DMA is enabled by default.
16 *
17 * MSI is enabled by default. The legacy IRQ is used if MSI couldn't
18 * be enabled.
19 *
20 * Note that almost all the errata workarounds here are only needed for
21 * rev1 chips. Rev1a silicon (0110) fixes almost all of them.
22 */
23
24 /*
25 * Copyright (C) 2003 David Brownell
26 * Copyright (C) 2003-2005 PLX Technology, Inc.
27 * Copyright (C) 2014 Ricardo Ribalda - Qtechnology/AS
28 *
29 * Modified Seth Levy 2005 PLX Technology, Inc. to provide compatibility
30 * with 2282 chip
31 *
32 * Modified Ricardo Ribalda Qtechnology AS to provide compatibility
33 * with usb 338x chip. Based on PLX driver
34 *
35 * This program is free software; you can redistribute it and/or modify
36 * it under the terms of the GNU General Public License as published by
37 * the Free Software Foundation; either version 2 of the License, or
38 * (at your option) any later version.
39 */
40
41 #include <linux/module.h>
42 #include <linux/pci.h>
43 #include <linux/dma-mapping.h>
44 #include <linux/kernel.h>
45 #include <linux/delay.h>
46 #include <linux/ioport.h>
47 #include <linux/slab.h>
48 #include <linux/errno.h>
49 #include <linux/init.h>
50 #include <linux/timer.h>
51 #include <linux/list.h>
52 #include <linux/interrupt.h>
53 #include <linux/moduleparam.h>
54 #include <linux/device.h>
55 #include <linux/usb/ch9.h>
56 #include <linux/usb/gadget.h>
57 #include <linux/prefetch.h>
58 #include <linux/io.h>
59
60 #include <asm/byteorder.h>
61 #include <asm/irq.h>
62 #include <asm/unaligned.h>
63
64 #define DRIVER_DESC "PLX NET228x/USB338x USB Peripheral Controller"
65 #define DRIVER_VERSION "2005 Sept 27/v3.0"
66
67 #define EP_DONTUSE 13 /* nonzero */
68
69 #define USE_RDK_LEDS /* GPIO pins control three LEDs */
70
71
72 static const char driver_name[] = "net2280";
73 static const char driver_desc[] = DRIVER_DESC;
74
75 static const u32 ep_bit[9] = { 0, 17, 2, 19, 4, 1, 18, 3, 20 };
76 static const char ep0name[] = "ep0";
77 static const char *const ep_name[] = {
78 ep0name,
79 "ep-a", "ep-b", "ep-c", "ep-d",
80 "ep-e", "ep-f", "ep-g", "ep-h",
81 };
82
83 /* Endpoint names for usb3380 advance mode */
84 static const char *const ep_name_adv[] = {
85 ep0name,
86 "ep1in", "ep2out", "ep3in", "ep4out",
87 "ep1out", "ep2in", "ep3out", "ep4in",
88 };
89
90 /* mode 0 == ep-{a,b,c,d} 1K fifo each
91 * mode 1 == ep-{a,b} 2K fifo each, ep-{c,d} unavailable
92 * mode 2 == ep-a 2K fifo, ep-{b,c} 1K each, ep-d unavailable
93 */
94 static ushort fifo_mode;
95
96 /* "modprobe net2280 fifo_mode=1" etc */
97 module_param(fifo_mode, ushort, 0644);
98
99 /* enable_suspend -- When enabled, the driver will respond to
100 * USB suspend requests by powering down the NET2280. Otherwise,
101 * USB suspend requests will be ignored. This is acceptable for
102 * self-powered devices
103 */
104 static bool enable_suspend;
105
106 /* "modprobe net2280 enable_suspend=1" etc */
107 module_param(enable_suspend, bool, 0444);
108
109 #define DIR_STRING(bAddress) (((bAddress) & USB_DIR_IN) ? "in" : "out")
110
111 static char *type_string(u8 bmAttributes)
112 {
113 switch ((bmAttributes) & USB_ENDPOINT_XFERTYPE_MASK) {
114 case USB_ENDPOINT_XFER_BULK: return "bulk";
115 case USB_ENDPOINT_XFER_ISOC: return "iso";
116 case USB_ENDPOINT_XFER_INT: return "intr";
117 }
118 return "control";
119 }
120
121 #include "net2280.h"
122
123 #define valid_bit cpu_to_le32(BIT(VALID_BIT))
124 #define dma_done_ie cpu_to_le32(BIT(DMA_DONE_INTERRUPT_ENABLE))
125
126 /*-------------------------------------------------------------------------*/
127 static inline void enable_pciirqenb(struct net2280_ep *ep)
128 {
129 u32 tmp = readl(&ep->dev->regs->pciirqenb0);
130
131 if (ep->dev->quirks & PLX_LEGACY)
132 tmp |= BIT(ep->num);
133 else
134 tmp |= BIT(ep_bit[ep->num]);
135 writel(tmp, &ep->dev->regs->pciirqenb0);
136
137 return;
138 }
139
140 static int
141 net2280_enable(struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
142 {
143 struct net2280 *dev;
144 struct net2280_ep *ep;
145 u32 max, tmp;
146 unsigned long flags;
147 static const u32 ep_key[9] = { 1, 0, 1, 0, 1, 1, 0, 1, 0 };
148
149 ep = container_of(_ep, struct net2280_ep, ep);
150 if (!_ep || !desc || ep->desc || _ep->name == ep0name ||
151 desc->bDescriptorType != USB_DT_ENDPOINT)
152 return -EINVAL;
153 dev = ep->dev;
154 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
155 return -ESHUTDOWN;
156
157 /* erratum 0119 workaround ties up an endpoint number */
158 if ((desc->bEndpointAddress & 0x0f) == EP_DONTUSE)
159 return -EDOM;
160
161 if (dev->quirks & PLX_SUPERSPEED) {
162 if ((desc->bEndpointAddress & 0x0f) >= 0x0c)
163 return -EDOM;
164 ep->is_in = !!usb_endpoint_dir_in(desc);
165 if (dev->enhanced_mode && ep->is_in && ep_key[ep->num])
166 return -EINVAL;
167 }
168
169 /* sanity check ep-e/ep-f since their fifos are small */
170 max = usb_endpoint_maxp(desc) & 0x1fff;
171 if (ep->num > 4 && max > 64 && (dev->quirks & PLX_LEGACY))
172 return -ERANGE;
173
174 spin_lock_irqsave(&dev->lock, flags);
175 _ep->maxpacket = max & 0x7ff;
176 ep->desc = desc;
177
178 /* ep_reset() has already been called */
179 ep->stopped = 0;
180 ep->wedged = 0;
181 ep->out_overflow = 0;
182
183 /* set speed-dependent max packet; may kick in high bandwidth */
184 set_max_speed(ep, max);
185
186 /* set type, direction, address; reset fifo counters */
187 writel(BIT(FIFO_FLUSH), &ep->regs->ep_stat);
188 tmp = (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK);
189 if (tmp == USB_ENDPOINT_XFER_INT) {
190 /* erratum 0105 workaround prevents hs NYET */
191 if (dev->chiprev == 0100 &&
192 dev->gadget.speed == USB_SPEED_HIGH &&
193 !(desc->bEndpointAddress & USB_DIR_IN))
194 writel(BIT(CLEAR_NAK_OUT_PACKETS_MODE),
195 &ep->regs->ep_rsp);
196 } else if (tmp == USB_ENDPOINT_XFER_BULK) {
197 /* catch some particularly blatant driver bugs */
198 if ((dev->gadget.speed == USB_SPEED_SUPER && max != 1024) ||
199 (dev->gadget.speed == USB_SPEED_HIGH && max != 512) ||
200 (dev->gadget.speed == USB_SPEED_FULL && max > 64)) {
201 spin_unlock_irqrestore(&dev->lock, flags);
202 return -ERANGE;
203 }
204 }
205 ep->is_iso = (tmp == USB_ENDPOINT_XFER_ISOC);
206 /* Enable this endpoint */
207 if (dev->quirks & PLX_LEGACY) {
208 tmp <<= ENDPOINT_TYPE;
209 tmp |= desc->bEndpointAddress;
210 /* default full fifo lines */
211 tmp |= (4 << ENDPOINT_BYTE_COUNT);
212 tmp |= BIT(ENDPOINT_ENABLE);
213 ep->is_in = (tmp & USB_DIR_IN) != 0;
214 } else {
215 /* In Legacy mode, only OUT endpoints are used */
216 if (dev->enhanced_mode && ep->is_in) {
217 tmp <<= IN_ENDPOINT_TYPE;
218 tmp |= BIT(IN_ENDPOINT_ENABLE);
219 /* Not applicable to Legacy */
220 tmp |= BIT(ENDPOINT_DIRECTION);
221 } else {
222 tmp <<= OUT_ENDPOINT_TYPE;
223 tmp |= BIT(OUT_ENDPOINT_ENABLE);
224 tmp |= (ep->is_in << ENDPOINT_DIRECTION);
225 }
226
227 tmp |= usb_endpoint_num(desc);
228 tmp |= (ep->ep.maxburst << MAX_BURST_SIZE);
229 }
230
231 /* Make sure all the registers are written before ep_rsp*/
232 wmb();
233
234 /* for OUT transfers, block the rx fifo until a read is posted */
235 if (!ep->is_in)
236 writel(BIT(SET_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
237 else if (!(dev->quirks & PLX_2280)) {
238 /* Added for 2282, Don't use nak packets on an in endpoint,
239 * this was ignored on 2280
240 */
241 writel(BIT(CLEAR_NAK_OUT_PACKETS) |
242 BIT(CLEAR_NAK_OUT_PACKETS_MODE), &ep->regs->ep_rsp);
243 }
244
245 writel(tmp, &ep->cfg->ep_cfg);
246
247 /* enable irqs */
248 if (!ep->dma) { /* pio, per-packet */
249 enable_pciirqenb(ep);
250
251 tmp = BIT(DATA_PACKET_RECEIVED_INTERRUPT_ENABLE) |
252 BIT(DATA_PACKET_TRANSMITTED_INTERRUPT_ENABLE);
253 if (dev->quirks & PLX_2280)
254 tmp |= readl(&ep->regs->ep_irqenb);
255 writel(tmp, &ep->regs->ep_irqenb);
256 } else { /* dma, per-request */
257 tmp = BIT((8 + ep->num)); /* completion */
258 tmp |= readl(&dev->regs->pciirqenb1);
259 writel(tmp, &dev->regs->pciirqenb1);
260
261 /* for short OUT transfers, dma completions can't
262 * advance the queue; do it pio-style, by hand.
263 * NOTE erratum 0112 workaround #2
264 */
265 if ((desc->bEndpointAddress & USB_DIR_IN) == 0) {
266 tmp = BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT_ENABLE);
267 writel(tmp, &ep->regs->ep_irqenb);
268
269 enable_pciirqenb(ep);
270 }
271 }
272
273 tmp = desc->bEndpointAddress;
274 ep_dbg(dev, "enabled %s (ep%d%s-%s) %s max %04x\n",
275 _ep->name, tmp & 0x0f, DIR_STRING(tmp),
276 type_string(desc->bmAttributes),
277 ep->dma ? "dma" : "pio", max);
278
279 /* pci writes may still be posted */
280 spin_unlock_irqrestore(&dev->lock, flags);
281 return 0;
282 }
283
284 static int handshake(u32 __iomem *ptr, u32 mask, u32 done, int usec)
285 {
286 u32 result;
287
288 do {
289 result = readl(ptr);
290 if (result == ~(u32)0) /* "device unplugged" */
291 return -ENODEV;
292 result &= mask;
293 if (result == done)
294 return 0;
295 udelay(1);
296 usec--;
297 } while (usec > 0);
298 return -ETIMEDOUT;
299 }
300
301 static const struct usb_ep_ops net2280_ep_ops;
302
303 static void ep_reset_228x(struct net2280_regs __iomem *regs,
304 struct net2280_ep *ep)
305 {
306 u32 tmp;
307
308 ep->desc = NULL;
309 INIT_LIST_HEAD(&ep->queue);
310
311 usb_ep_set_maxpacket_limit(&ep->ep, ~0);
312 ep->ep.ops = &net2280_ep_ops;
313
314 /* disable the dma, irqs, endpoint... */
315 if (ep->dma) {
316 writel(0, &ep->dma->dmactl);
317 writel(BIT(DMA_SCATTER_GATHER_DONE_INTERRUPT) |
318 BIT(DMA_TRANSACTION_DONE_INTERRUPT) |
319 BIT(DMA_ABORT),
320 &ep->dma->dmastat);
321
322 tmp = readl(&regs->pciirqenb0);
323 tmp &= ~BIT(ep->num);
324 writel(tmp, &regs->pciirqenb0);
325 } else {
326 tmp = readl(&regs->pciirqenb1);
327 tmp &= ~BIT((8 + ep->num)); /* completion */
328 writel(tmp, &regs->pciirqenb1);
329 }
330 writel(0, &ep->regs->ep_irqenb);
331
332 /* init to our chosen defaults, notably so that we NAK OUT
333 * packets until the driver queues a read (+note erratum 0112)
334 */
335 if (!ep->is_in || (ep->dev->quirks & PLX_2280)) {
336 tmp = BIT(SET_NAK_OUT_PACKETS_MODE) |
337 BIT(SET_NAK_OUT_PACKETS) |
338 BIT(CLEAR_EP_HIDE_STATUS_PHASE) |
339 BIT(CLEAR_INTERRUPT_MODE);
340 } else {
341 /* added for 2282 */
342 tmp = BIT(CLEAR_NAK_OUT_PACKETS_MODE) |
343 BIT(CLEAR_NAK_OUT_PACKETS) |
344 BIT(CLEAR_EP_HIDE_STATUS_PHASE) |
345 BIT(CLEAR_INTERRUPT_MODE);
346 }
347
348 if (ep->num != 0) {
349 tmp |= BIT(CLEAR_ENDPOINT_TOGGLE) |
350 BIT(CLEAR_ENDPOINT_HALT);
351 }
352 writel(tmp, &ep->regs->ep_rsp);
353
354 /* scrub most status bits, and flush any fifo state */
355 if (ep->dev->quirks & PLX_2280)
356 tmp = BIT(FIFO_OVERFLOW) |
357 BIT(FIFO_UNDERFLOW);
358 else
359 tmp = 0;
360
361 writel(tmp | BIT(TIMEOUT) |
362 BIT(USB_STALL_SENT) |
363 BIT(USB_IN_NAK_SENT) |
364 BIT(USB_IN_ACK_RCVD) |
365 BIT(USB_OUT_PING_NAK_SENT) |
366 BIT(USB_OUT_ACK_SENT) |
367 BIT(FIFO_FLUSH) |
368 BIT(SHORT_PACKET_OUT_DONE_INTERRUPT) |
369 BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT) |
370 BIT(DATA_PACKET_RECEIVED_INTERRUPT) |
371 BIT(DATA_PACKET_TRANSMITTED_INTERRUPT) |
372 BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
373 BIT(DATA_IN_TOKEN_INTERRUPT),
374 &ep->regs->ep_stat);
375
376 /* fifo size is handled separately */
377 }
378
379 static void ep_reset_338x(struct net2280_regs __iomem *regs,
380 struct net2280_ep *ep)
381 {
382 u32 tmp, dmastat;
383
384 ep->desc = NULL;
385 INIT_LIST_HEAD(&ep->queue);
386
387 usb_ep_set_maxpacket_limit(&ep->ep, ~0);
388 ep->ep.ops = &net2280_ep_ops;
389
390 /* disable the dma, irqs, endpoint... */
391 if (ep->dma) {
392 writel(0, &ep->dma->dmactl);
393 writel(BIT(DMA_ABORT_DONE_INTERRUPT) |
394 BIT(DMA_PAUSE_DONE_INTERRUPT) |
395 BIT(DMA_SCATTER_GATHER_DONE_INTERRUPT) |
396 BIT(DMA_TRANSACTION_DONE_INTERRUPT),
397 /* | BIT(DMA_ABORT), */
398 &ep->dma->dmastat);
399
400 dmastat = readl(&ep->dma->dmastat);
401 if (dmastat == 0x5002) {
402 ep_warn(ep->dev, "The dmastat return = %x!!\n",
403 dmastat);
404 writel(0x5a, &ep->dma->dmastat);
405 }
406
407 tmp = readl(&regs->pciirqenb0);
408 tmp &= ~BIT(ep_bit[ep->num]);
409 writel(tmp, &regs->pciirqenb0);
410 } else {
411 if (ep->num < 5) {
412 tmp = readl(&regs->pciirqenb1);
413 tmp &= ~BIT((8 + ep->num)); /* completion */
414 writel(tmp, &regs->pciirqenb1);
415 }
416 }
417 writel(0, &ep->regs->ep_irqenb);
418
419 writel(BIT(SHORT_PACKET_OUT_DONE_INTERRUPT) |
420 BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT) |
421 BIT(FIFO_OVERFLOW) |
422 BIT(DATA_PACKET_RECEIVED_INTERRUPT) |
423 BIT(DATA_PACKET_TRANSMITTED_INTERRUPT) |
424 BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
425 BIT(DATA_IN_TOKEN_INTERRUPT), &ep->regs->ep_stat);
426 }
427
428 static void nuke(struct net2280_ep *);
429
430 static int net2280_disable(struct usb_ep *_ep)
431 {
432 struct net2280_ep *ep;
433 unsigned long flags;
434
435 ep = container_of(_ep, struct net2280_ep, ep);
436 if (!_ep || !ep->desc || _ep->name == ep0name)
437 return -EINVAL;
438
439 spin_lock_irqsave(&ep->dev->lock, flags);
440 nuke(ep);
441
442 if (ep->dev->quirks & PLX_SUPERSPEED)
443 ep_reset_338x(ep->dev->regs, ep);
444 else
445 ep_reset_228x(ep->dev->regs, ep);
446
447 ep_vdbg(ep->dev, "disabled %s %s\n",
448 ep->dma ? "dma" : "pio", _ep->name);
449
450 /* synch memory views with the device */
451 (void)readl(&ep->cfg->ep_cfg);
452
453 if (!ep->dma && ep->num >= 1 && ep->num <= 4)
454 ep->dma = &ep->dev->dma[ep->num - 1];
455
456 spin_unlock_irqrestore(&ep->dev->lock, flags);
457 return 0;
458 }
459
460 /*-------------------------------------------------------------------------*/
461
462 static struct usb_request
463 *net2280_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
464 {
465 struct net2280_ep *ep;
466 struct net2280_request *req;
467
468 if (!_ep)
469 return NULL;
470 ep = container_of(_ep, struct net2280_ep, ep);
471
472 req = kzalloc(sizeof(*req), gfp_flags);
473 if (!req)
474 return NULL;
475
476 INIT_LIST_HEAD(&req->queue);
477
478 /* this dma descriptor may be swapped with the previous dummy */
479 if (ep->dma) {
480 struct net2280_dma *td;
481
482 td = pci_pool_alloc(ep->dev->requests, gfp_flags,
483 &req->td_dma);
484 if (!td) {
485 kfree(req);
486 return NULL;
487 }
488 td->dmacount = 0; /* not VALID */
489 td->dmadesc = td->dmaaddr;
490 req->td = td;
491 }
492 return &req->req;
493 }
494
495 static void net2280_free_request(struct usb_ep *_ep, struct usb_request *_req)
496 {
497 struct net2280_ep *ep;
498 struct net2280_request *req;
499
500 ep = container_of(_ep, struct net2280_ep, ep);
501 if (!_ep || !_req)
502 return;
503
504 req = container_of(_req, struct net2280_request, req);
505 WARN_ON(!list_empty(&req->queue));
506 if (req->td)
507 pci_pool_free(ep->dev->requests, req->td, req->td_dma);
508 kfree(req);
509 }
510
511 /*-------------------------------------------------------------------------*/
512
513 /* load a packet into the fifo we use for usb IN transfers.
514 * works for all endpoints.
515 *
516 * NOTE: pio with ep-a..ep-d could stuff multiple packets into the fifo
517 * at a time, but this code is simpler because it knows it only writes
518 * one packet. ep-a..ep-d should use dma instead.
519 */
520 static void write_fifo(struct net2280_ep *ep, struct usb_request *req)
521 {
522 struct net2280_ep_regs __iomem *regs = ep->regs;
523 u8 *buf;
524 u32 tmp;
525 unsigned count, total;
526
527 /* INVARIANT: fifo is currently empty. (testable) */
528
529 if (req) {
530 buf = req->buf + req->actual;
531 prefetch(buf);
532 total = req->length - req->actual;
533 } else {
534 total = 0;
535 buf = NULL;
536 }
537
538 /* write just one packet at a time */
539 count = ep->ep.maxpacket;
540 if (count > total) /* min() cannot be used on a bitfield */
541 count = total;
542
543 ep_vdbg(ep->dev, "write %s fifo (IN) %d bytes%s req %p\n",
544 ep->ep.name, count,
545 (count != ep->ep.maxpacket) ? " (short)" : "",
546 req);
547 while (count >= 4) {
548 /* NOTE be careful if you try to align these. fifo lines
549 * should normally be full (4 bytes) and successive partial
550 * lines are ok only in certain cases.
551 */
552 tmp = get_unaligned((u32 *)buf);
553 cpu_to_le32s(&tmp);
554 writel(tmp, &regs->ep_data);
555 buf += 4;
556 count -= 4;
557 }
558
559 /* last fifo entry is "short" unless we wrote a full packet.
560 * also explicitly validate last word in (periodic) transfers
561 * when maxpacket is not a multiple of 4 bytes.
562 */
563 if (count || total < ep->ep.maxpacket) {
564 tmp = count ? get_unaligned((u32 *)buf) : count;
565 cpu_to_le32s(&tmp);
566 set_fifo_bytecount(ep, count & 0x03);
567 writel(tmp, &regs->ep_data);
568 }
569
570 /* pci writes may still be posted */
571 }
572
573 /* work around erratum 0106: PCI and USB race over the OUT fifo.
574 * caller guarantees chiprev 0100, out endpoint is NAKing, and
575 * there's no real data in the fifo.
576 *
577 * NOTE: also used in cases where that erratum doesn't apply:
578 * where the host wrote "too much" data to us.
579 */
580 static void out_flush(struct net2280_ep *ep)
581 {
582 u32 __iomem *statp;
583 u32 tmp;
584
585 statp = &ep->regs->ep_stat;
586
587 tmp = readl(statp);
588 if (tmp & BIT(NAK_OUT_PACKETS)) {
589 ep_dbg(ep->dev, "%s %s %08x !NAK\n",
590 ep->ep.name, __func__, tmp);
591 writel(BIT(SET_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
592 }
593
594 writel(BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
595 BIT(DATA_PACKET_RECEIVED_INTERRUPT),
596 statp);
597 writel(BIT(FIFO_FLUSH), statp);
598 /* Make sure that stap is written */
599 mb();
600 tmp = readl(statp);
601 if (tmp & BIT(DATA_OUT_PING_TOKEN_INTERRUPT) &&
602 /* high speed did bulk NYET; fifo isn't filling */
603 ep->dev->gadget.speed == USB_SPEED_FULL) {
604 unsigned usec;
605
606 usec = 50; /* 64 byte bulk/interrupt */
607 handshake(statp, BIT(USB_OUT_PING_NAK_SENT),
608 BIT(USB_OUT_PING_NAK_SENT), usec);
609 /* NAK done; now CLEAR_NAK_OUT_PACKETS is safe */
610 }
611 }
612
613 /* unload packet(s) from the fifo we use for usb OUT transfers.
614 * returns true iff the request completed, because of short packet
615 * or the request buffer having filled with full packets.
616 *
617 * for ep-a..ep-d this will read multiple packets out when they
618 * have been accepted.
619 */
620 static int read_fifo(struct net2280_ep *ep, struct net2280_request *req)
621 {
622 struct net2280_ep_regs __iomem *regs = ep->regs;
623 u8 *buf = req->req.buf + req->req.actual;
624 unsigned count, tmp, is_short;
625 unsigned cleanup = 0, prevent = 0;
626
627 /* erratum 0106 ... packets coming in during fifo reads might
628 * be incompletely rejected. not all cases have workarounds.
629 */
630 if (ep->dev->chiprev == 0x0100 &&
631 ep->dev->gadget.speed == USB_SPEED_FULL) {
632 udelay(1);
633 tmp = readl(&ep->regs->ep_stat);
634 if ((tmp & BIT(NAK_OUT_PACKETS)))
635 cleanup = 1;
636 else if ((tmp & BIT(FIFO_FULL))) {
637 start_out_naking(ep);
638 prevent = 1;
639 }
640 /* else: hope we don't see the problem */
641 }
642
643 /* never overflow the rx buffer. the fifo reads packets until
644 * it sees a short one; we might not be ready for them all.
645 */
646 prefetchw(buf);
647 count = readl(&regs->ep_avail);
648 if (unlikely(count == 0)) {
649 udelay(1);
650 tmp = readl(&ep->regs->ep_stat);
651 count = readl(&regs->ep_avail);
652 /* handled that data already? */
653 if (count == 0 && (tmp & BIT(NAK_OUT_PACKETS)) == 0)
654 return 0;
655 }
656
657 tmp = req->req.length - req->req.actual;
658 if (count > tmp) {
659 /* as with DMA, data overflow gets flushed */
660 if ((tmp % ep->ep.maxpacket) != 0) {
661 ep_err(ep->dev,
662 "%s out fifo %d bytes, expected %d\n",
663 ep->ep.name, count, tmp);
664 req->req.status = -EOVERFLOW;
665 cleanup = 1;
666 /* NAK_OUT_PACKETS will be set, so flushing is safe;
667 * the next read will start with the next packet
668 */
669 } /* else it's a ZLP, no worries */
670 count = tmp;
671 }
672 req->req.actual += count;
673
674 is_short = (count == 0) || ((count % ep->ep.maxpacket) != 0);
675
676 ep_vdbg(ep->dev, "read %s fifo (OUT) %d bytes%s%s%s req %p %d/%d\n",
677 ep->ep.name, count, is_short ? " (short)" : "",
678 cleanup ? " flush" : "", prevent ? " nak" : "",
679 req, req->req.actual, req->req.length);
680
681 while (count >= 4) {
682 tmp = readl(&regs->ep_data);
683 cpu_to_le32s(&tmp);
684 put_unaligned(tmp, (u32 *)buf);
685 buf += 4;
686 count -= 4;
687 }
688 if (count) {
689 tmp = readl(&regs->ep_data);
690 /* LE conversion is implicit here: */
691 do {
692 *buf++ = (u8) tmp;
693 tmp >>= 8;
694 } while (--count);
695 }
696 if (cleanup)
697 out_flush(ep);
698 if (prevent) {
699 writel(BIT(CLEAR_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
700 (void) readl(&ep->regs->ep_rsp);
701 }
702
703 return is_short || ((req->req.actual == req->req.length) &&
704 !req->req.zero);
705 }
706
707 /* fill out dma descriptor to match a given request */
708 static void fill_dma_desc(struct net2280_ep *ep,
709 struct net2280_request *req, int valid)
710 {
711 struct net2280_dma *td = req->td;
712 u32 dmacount = req->req.length;
713
714 /* don't let DMA continue after a short OUT packet,
715 * so overruns can't affect the next transfer.
716 * in case of overruns on max-size packets, we can't
717 * stop the fifo from filling but we can flush it.
718 */
719 if (ep->is_in)
720 dmacount |= BIT(DMA_DIRECTION);
721 if ((!ep->is_in && (dmacount % ep->ep.maxpacket) != 0) ||
722 !(ep->dev->quirks & PLX_2280))
723 dmacount |= BIT(END_OF_CHAIN);
724
725 req->valid = valid;
726 if (valid)
727 dmacount |= BIT(VALID_BIT);
728 dmacount |= BIT(DMA_DONE_INTERRUPT_ENABLE);
729
730 /* td->dmadesc = previously set by caller */
731 td->dmaaddr = cpu_to_le32 (req->req.dma);
732
733 /* 2280 may be polling VALID_BIT through ep->dma->dmadesc */
734 wmb();
735 td->dmacount = cpu_to_le32(dmacount);
736 }
737
738 static const u32 dmactl_default =
739 BIT(DMA_SCATTER_GATHER_DONE_INTERRUPT) |
740 BIT(DMA_CLEAR_COUNT_ENABLE) |
741 /* erratum 0116 workaround part 1 (use POLLING) */
742 (POLL_100_USEC << DESCRIPTOR_POLLING_RATE) |
743 BIT(DMA_VALID_BIT_POLLING_ENABLE) |
744 BIT(DMA_VALID_BIT_ENABLE) |
745 BIT(DMA_SCATTER_GATHER_ENABLE) |
746 /* erratum 0116 workaround part 2 (no AUTOSTART) */
747 BIT(DMA_ENABLE);
748
749 static inline void spin_stop_dma(struct net2280_dma_regs __iomem *dma)
750 {
751 handshake(&dma->dmactl, BIT(DMA_ENABLE), 0, 50);
752 }
753
754 static inline void stop_dma(struct net2280_dma_regs __iomem *dma)
755 {
756 writel(readl(&dma->dmactl) & ~BIT(DMA_ENABLE), &dma->dmactl);
757 spin_stop_dma(dma);
758 }
759
760 static void start_queue(struct net2280_ep *ep, u32 dmactl, u32 td_dma)
761 {
762 struct net2280_dma_regs __iomem *dma = ep->dma;
763 unsigned int tmp = BIT(VALID_BIT) | (ep->is_in << DMA_DIRECTION);
764
765 if (!(ep->dev->quirks & PLX_2280))
766 tmp |= BIT(END_OF_CHAIN);
767
768 writel(tmp, &dma->dmacount);
769 writel(readl(&dma->dmastat), &dma->dmastat);
770
771 writel(td_dma, &dma->dmadesc);
772 if (ep->dev->quirks & PLX_SUPERSPEED)
773 dmactl |= BIT(DMA_REQUEST_OUTSTANDING);
774 writel(dmactl, &dma->dmactl);
775
776 /* erratum 0116 workaround part 3: pci arbiter away from net2280 */
777 (void) readl(&ep->dev->pci->pcimstctl);
778
779 writel(BIT(DMA_START), &dma->dmastat);
780
781 if (!ep->is_in)
782 stop_out_naking(ep);
783 }
784
785 static void start_dma(struct net2280_ep *ep, struct net2280_request *req)
786 {
787 u32 tmp;
788 struct net2280_dma_regs __iomem *dma = ep->dma;
789
790 /* FIXME can't use DMA for ZLPs */
791
792 /* on this path we "know" there's no dma active (yet) */
793 WARN_ON(readl(&dma->dmactl) & BIT(DMA_ENABLE));
794 writel(0, &ep->dma->dmactl);
795
796 /* previous OUT packet might have been short */
797 if (!ep->is_in && (readl(&ep->regs->ep_stat) &
798 BIT(NAK_OUT_PACKETS))) {
799 writel(BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT),
800 &ep->regs->ep_stat);
801
802 tmp = readl(&ep->regs->ep_avail);
803 if (tmp) {
804 writel(readl(&dma->dmastat), &dma->dmastat);
805
806 /* transfer all/some fifo data */
807 writel(req->req.dma, &dma->dmaaddr);
808 tmp = min(tmp, req->req.length);
809
810 /* dma irq, faking scatterlist status */
811 req->td->dmacount = cpu_to_le32(req->req.length - tmp);
812 writel(BIT(DMA_DONE_INTERRUPT_ENABLE) | tmp,
813 &dma->dmacount);
814 req->td->dmadesc = 0;
815 req->valid = 1;
816
817 writel(BIT(DMA_ENABLE), &dma->dmactl);
818 writel(BIT(DMA_START), &dma->dmastat);
819 return;
820 }
821 }
822
823 tmp = dmactl_default;
824
825 /* force packet boundaries between dma requests, but prevent the
826 * controller from automagically writing a last "short" packet
827 * (zero length) unless the driver explicitly said to do that.
828 */
829 if (ep->is_in) {
830 if (likely((req->req.length % ep->ep.maxpacket) ||
831 req->req.zero)){
832 tmp |= BIT(DMA_FIFO_VALIDATE);
833 ep->in_fifo_validate = 1;
834 } else
835 ep->in_fifo_validate = 0;
836 }
837
838 /* init req->td, pointing to the current dummy */
839 req->td->dmadesc = cpu_to_le32 (ep->td_dma);
840 fill_dma_desc(ep, req, 1);
841
842 req->td->dmacount |= cpu_to_le32(BIT(END_OF_CHAIN));
843
844 start_queue(ep, tmp, req->td_dma);
845 }
846
847 static inline void
848 queue_dma(struct net2280_ep *ep, struct net2280_request *req, int valid)
849 {
850 struct net2280_dma *end;
851 dma_addr_t tmp;
852
853 /* swap new dummy for old, link; fill and maybe activate */
854 end = ep->dummy;
855 ep->dummy = req->td;
856 req->td = end;
857
858 tmp = ep->td_dma;
859 ep->td_dma = req->td_dma;
860 req->td_dma = tmp;
861
862 end->dmadesc = cpu_to_le32 (ep->td_dma);
863
864 fill_dma_desc(ep, req, valid);
865 }
866
867 static void
868 done(struct net2280_ep *ep, struct net2280_request *req, int status)
869 {
870 struct net2280 *dev;
871 unsigned stopped = ep->stopped;
872
873 list_del_init(&req->queue);
874
875 if (req->req.status == -EINPROGRESS)
876 req->req.status = status;
877 else
878 status = req->req.status;
879
880 dev = ep->dev;
881 if (ep->dma)
882 usb_gadget_unmap_request(&dev->gadget, &req->req, ep->is_in);
883
884 if (status && status != -ESHUTDOWN)
885 ep_vdbg(dev, "complete %s req %p stat %d len %u/%u\n",
886 ep->ep.name, &req->req, status,
887 req->req.actual, req->req.length);
888
889 /* don't modify queue heads during completion callback */
890 ep->stopped = 1;
891 spin_unlock(&dev->lock);
892 usb_gadget_giveback_request(&ep->ep, &req->req);
893 spin_lock(&dev->lock);
894 ep->stopped = stopped;
895 }
896
897 /*-------------------------------------------------------------------------*/
898
899 static int
900 net2280_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
901 {
902 struct net2280_request *req;
903 struct net2280_ep *ep;
904 struct net2280 *dev;
905 unsigned long flags;
906
907 /* we always require a cpu-view buffer, so that we can
908 * always use pio (as fallback or whatever).
909 */
910 req = container_of(_req, struct net2280_request, req);
911 if (!_req || !_req->complete || !_req->buf ||
912 !list_empty(&req->queue))
913 return -EINVAL;
914 if (_req->length > (~0 & DMA_BYTE_COUNT_MASK))
915 return -EDOM;
916 ep = container_of(_ep, struct net2280_ep, ep);
917 if (!_ep || (!ep->desc && ep->num != 0))
918 return -EINVAL;
919 dev = ep->dev;
920 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
921 return -ESHUTDOWN;
922
923 /* FIXME implement PIO fallback for ZLPs with DMA */
924 if (ep->dma && _req->length == 0)
925 return -EOPNOTSUPP;
926
927 /* set up dma mapping in case the caller didn't */
928 if (ep->dma) {
929 int ret;
930
931 ret = usb_gadget_map_request(&dev->gadget, _req,
932 ep->is_in);
933 if (ret)
934 return ret;
935 }
936
937 ep_vdbg(dev, "%s queue req %p, len %d buf %p\n",
938 _ep->name, _req, _req->length, _req->buf);
939
940 spin_lock_irqsave(&dev->lock, flags);
941
942 _req->status = -EINPROGRESS;
943 _req->actual = 0;
944
945 /* kickstart this i/o queue? */
946 if (list_empty(&ep->queue) && !ep->stopped &&
947 !((dev->quirks & PLX_SUPERSPEED) && ep->dma &&
948 (readl(&ep->regs->ep_rsp) & BIT(CLEAR_ENDPOINT_HALT)))) {
949
950 /* use DMA if the endpoint supports it, else pio */
951 if (ep->dma)
952 start_dma(ep, req);
953 else {
954 /* maybe there's no control data, just status ack */
955 if (ep->num == 0 && _req->length == 0) {
956 allow_status(ep);
957 done(ep, req, 0);
958 ep_vdbg(dev, "%s status ack\n", ep->ep.name);
959 goto done;
960 }
961
962 /* PIO ... stuff the fifo, or unblock it. */
963 if (ep->is_in)
964 write_fifo(ep, _req);
965 else if (list_empty(&ep->queue)) {
966 u32 s;
967
968 /* OUT FIFO might have packet(s) buffered */
969 s = readl(&ep->regs->ep_stat);
970 if ((s & BIT(FIFO_EMPTY)) == 0) {
971 /* note: _req->short_not_ok is
972 * ignored here since PIO _always_
973 * stops queue advance here, and
974 * _req->status doesn't change for
975 * short reads (only _req->actual)
976 */
977 if (read_fifo(ep, req) &&
978 ep->num == 0) {
979 done(ep, req, 0);
980 allow_status(ep);
981 /* don't queue it */
982 req = NULL;
983 } else if (read_fifo(ep, req) &&
984 ep->num != 0) {
985 done(ep, req, 0);
986 req = NULL;
987 } else
988 s = readl(&ep->regs->ep_stat);
989 }
990
991 /* don't NAK, let the fifo fill */
992 if (req && (s & BIT(NAK_OUT_PACKETS)))
993 writel(BIT(CLEAR_NAK_OUT_PACKETS),
994 &ep->regs->ep_rsp);
995 }
996 }
997
998 } else if (ep->dma) {
999 int valid = 1;
1000
1001 if (ep->is_in) {
1002 int expect;
1003
1004 /* preventing magic zlps is per-engine state, not
1005 * per-transfer; irq logic must recover hiccups.
1006 */
1007 expect = likely(req->req.zero ||
1008 (req->req.length % ep->ep.maxpacket));
1009 if (expect != ep->in_fifo_validate)
1010 valid = 0;
1011 }
1012 queue_dma(ep, req, valid);
1013
1014 } /* else the irq handler advances the queue. */
1015
1016 ep->responded = 1;
1017 if (req)
1018 list_add_tail(&req->queue, &ep->queue);
1019 done:
1020 spin_unlock_irqrestore(&dev->lock, flags);
1021
1022 /* pci writes may still be posted */
1023 return 0;
1024 }
1025
1026 static inline void
1027 dma_done(struct net2280_ep *ep, struct net2280_request *req, u32 dmacount,
1028 int status)
1029 {
1030 req->req.actual = req->req.length - (DMA_BYTE_COUNT_MASK & dmacount);
1031 done(ep, req, status);
1032 }
1033
1034 static void scan_dma_completions(struct net2280_ep *ep)
1035 {
1036 /* only look at descriptors that were "naturally" retired,
1037 * so fifo and list head state won't matter
1038 */
1039 while (!list_empty(&ep->queue)) {
1040 struct net2280_request *req;
1041 u32 tmp;
1042
1043 req = list_entry(ep->queue.next,
1044 struct net2280_request, queue);
1045 if (!req->valid)
1046 break;
1047 rmb();
1048 tmp = le32_to_cpup(&req->td->dmacount);
1049 if ((tmp & BIT(VALID_BIT)) != 0)
1050 break;
1051
1052 /* SHORT_PACKET_TRANSFERRED_INTERRUPT handles "usb-short"
1053 * cases where DMA must be aborted; this code handles
1054 * all non-abort DMA completions.
1055 */
1056 if (unlikely(req->td->dmadesc == 0)) {
1057 /* paranoia */
1058 tmp = readl(&ep->dma->dmacount);
1059 if (tmp & DMA_BYTE_COUNT_MASK)
1060 break;
1061 /* single transfer mode */
1062 dma_done(ep, req, tmp, 0);
1063 break;
1064 } else if (!ep->is_in &&
1065 (req->req.length % ep->ep.maxpacket) &&
1066 !(ep->dev->quirks & PLX_SUPERSPEED)) {
1067
1068 tmp = readl(&ep->regs->ep_stat);
1069 /* AVOID TROUBLE HERE by not issuing short reads from
1070 * your gadget driver. That helps avoids errata 0121,
1071 * 0122, and 0124; not all cases trigger the warning.
1072 */
1073 if ((tmp & BIT(NAK_OUT_PACKETS)) == 0) {
1074 ep_warn(ep->dev, "%s lost packet sync!\n",
1075 ep->ep.name);
1076 req->req.status = -EOVERFLOW;
1077 } else {
1078 tmp = readl(&ep->regs->ep_avail);
1079 if (tmp) {
1080 /* fifo gets flushed later */
1081 ep->out_overflow = 1;
1082 ep_dbg(ep->dev,
1083 "%s dma, discard %d len %d\n",
1084 ep->ep.name, tmp,
1085 req->req.length);
1086 req->req.status = -EOVERFLOW;
1087 }
1088 }
1089 }
1090 dma_done(ep, req, tmp, 0);
1091 }
1092 }
1093
1094 static void restart_dma(struct net2280_ep *ep)
1095 {
1096 struct net2280_request *req;
1097
1098 if (ep->stopped)
1099 return;
1100 req = list_entry(ep->queue.next, struct net2280_request, queue);
1101
1102 start_dma(ep, req);
1103 }
1104
1105 static void abort_dma(struct net2280_ep *ep)
1106 {
1107 /* abort the current transfer */
1108 if (likely(!list_empty(&ep->queue))) {
1109 /* FIXME work around errata 0121, 0122, 0124 */
1110 writel(BIT(DMA_ABORT), &ep->dma->dmastat);
1111 spin_stop_dma(ep->dma);
1112 } else
1113 stop_dma(ep->dma);
1114 scan_dma_completions(ep);
1115 }
1116
1117 /* dequeue ALL requests */
1118 static void nuke(struct net2280_ep *ep)
1119 {
1120 struct net2280_request *req;
1121
1122 /* called with spinlock held */
1123 ep->stopped = 1;
1124 if (ep->dma)
1125 abort_dma(ep);
1126 while (!list_empty(&ep->queue)) {
1127 req = list_entry(ep->queue.next,
1128 struct net2280_request,
1129 queue);
1130 done(ep, req, -ESHUTDOWN);
1131 }
1132 }
1133
1134 /* dequeue JUST ONE request */
1135 static int net2280_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1136 {
1137 struct net2280_ep *ep;
1138 struct net2280_request *req;
1139 unsigned long flags;
1140 u32 dmactl;
1141 int stopped;
1142
1143 ep = container_of(_ep, struct net2280_ep, ep);
1144 if (!_ep || (!ep->desc && ep->num != 0) || !_req)
1145 return -EINVAL;
1146
1147 spin_lock_irqsave(&ep->dev->lock, flags);
1148 stopped = ep->stopped;
1149
1150 /* quiesce dma while we patch the queue */
1151 dmactl = 0;
1152 ep->stopped = 1;
1153 if (ep->dma) {
1154 dmactl = readl(&ep->dma->dmactl);
1155 /* WARNING erratum 0127 may kick in ... */
1156 stop_dma(ep->dma);
1157 scan_dma_completions(ep);
1158 }
1159
1160 /* make sure it's still queued on this endpoint */
1161 list_for_each_entry(req, &ep->queue, queue) {
1162 if (&req->req == _req)
1163 break;
1164 }
1165 if (&req->req != _req) {
1166 spin_unlock_irqrestore(&ep->dev->lock, flags);
1167 return -EINVAL;
1168 }
1169
1170 /* queue head may be partially complete. */
1171 if (ep->queue.next == &req->queue) {
1172 if (ep->dma) {
1173 ep_dbg(ep->dev, "unlink (%s) dma\n", _ep->name);
1174 _req->status = -ECONNRESET;
1175 abort_dma(ep);
1176 if (likely(ep->queue.next == &req->queue)) {
1177 /* NOTE: misreports single-transfer mode*/
1178 req->td->dmacount = 0; /* invalidate */
1179 dma_done(ep, req,
1180 readl(&ep->dma->dmacount),
1181 -ECONNRESET);
1182 }
1183 } else {
1184 ep_dbg(ep->dev, "unlink (%s) pio\n", _ep->name);
1185 done(ep, req, -ECONNRESET);
1186 }
1187 req = NULL;
1188 }
1189
1190 if (req)
1191 done(ep, req, -ECONNRESET);
1192 ep->stopped = stopped;
1193
1194 if (ep->dma) {
1195 /* turn off dma on inactive queues */
1196 if (list_empty(&ep->queue))
1197 stop_dma(ep->dma);
1198 else if (!ep->stopped) {
1199 /* resume current request, or start new one */
1200 if (req)
1201 writel(dmactl, &ep->dma->dmactl);
1202 else
1203 start_dma(ep, list_entry(ep->queue.next,
1204 struct net2280_request, queue));
1205 }
1206 }
1207
1208 spin_unlock_irqrestore(&ep->dev->lock, flags);
1209 return 0;
1210 }
1211
1212 /*-------------------------------------------------------------------------*/
1213
1214 static int net2280_fifo_status(struct usb_ep *_ep);
1215
1216 static int
1217 net2280_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged)
1218 {
1219 struct net2280_ep *ep;
1220 unsigned long flags;
1221 int retval = 0;
1222
1223 ep = container_of(_ep, struct net2280_ep, ep);
1224 if (!_ep || (!ep->desc && ep->num != 0))
1225 return -EINVAL;
1226 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1227 return -ESHUTDOWN;
1228 if (ep->desc /* not ep0 */ && (ep->desc->bmAttributes & 0x03)
1229 == USB_ENDPOINT_XFER_ISOC)
1230 return -EINVAL;
1231
1232 spin_lock_irqsave(&ep->dev->lock, flags);
1233 if (!list_empty(&ep->queue))
1234 retval = -EAGAIN;
1235 else if (ep->is_in && value && net2280_fifo_status(_ep) != 0)
1236 retval = -EAGAIN;
1237 else {
1238 ep_vdbg(ep->dev, "%s %s %s\n", _ep->name,
1239 value ? "set" : "clear",
1240 wedged ? "wedge" : "halt");
1241 /* set/clear, then synch memory views with the device */
1242 if (value) {
1243 if (ep->num == 0)
1244 ep->dev->protocol_stall = 1;
1245 else
1246 set_halt(ep);
1247 if (wedged)
1248 ep->wedged = 1;
1249 } else {
1250 clear_halt(ep);
1251 if (ep->dev->quirks & PLX_SUPERSPEED &&
1252 !list_empty(&ep->queue) && ep->td_dma)
1253 restart_dma(ep);
1254 ep->wedged = 0;
1255 }
1256 (void) readl(&ep->regs->ep_rsp);
1257 }
1258 spin_unlock_irqrestore(&ep->dev->lock, flags);
1259
1260 return retval;
1261 }
1262
1263 static int net2280_set_halt(struct usb_ep *_ep, int value)
1264 {
1265 return net2280_set_halt_and_wedge(_ep, value, 0);
1266 }
1267
1268 static int net2280_set_wedge(struct usb_ep *_ep)
1269 {
1270 if (!_ep || _ep->name == ep0name)
1271 return -EINVAL;
1272 return net2280_set_halt_and_wedge(_ep, 1, 1);
1273 }
1274
1275 static int net2280_fifo_status(struct usb_ep *_ep)
1276 {
1277 struct net2280_ep *ep;
1278 u32 avail;
1279
1280 ep = container_of(_ep, struct net2280_ep, ep);
1281 if (!_ep || (!ep->desc && ep->num != 0))
1282 return -ENODEV;
1283 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1284 return -ESHUTDOWN;
1285
1286 avail = readl(&ep->regs->ep_avail) & (BIT(12) - 1);
1287 if (avail > ep->fifo_size)
1288 return -EOVERFLOW;
1289 if (ep->is_in)
1290 avail = ep->fifo_size - avail;
1291 return avail;
1292 }
1293
1294 static void net2280_fifo_flush(struct usb_ep *_ep)
1295 {
1296 struct net2280_ep *ep;
1297
1298 ep = container_of(_ep, struct net2280_ep, ep);
1299 if (!_ep || (!ep->desc && ep->num != 0))
1300 return;
1301 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1302 return;
1303
1304 writel(BIT(FIFO_FLUSH), &ep->regs->ep_stat);
1305 (void) readl(&ep->regs->ep_rsp);
1306 }
1307
1308 static const struct usb_ep_ops net2280_ep_ops = {
1309 .enable = net2280_enable,
1310 .disable = net2280_disable,
1311
1312 .alloc_request = net2280_alloc_request,
1313 .free_request = net2280_free_request,
1314
1315 .queue = net2280_queue,
1316 .dequeue = net2280_dequeue,
1317
1318 .set_halt = net2280_set_halt,
1319 .set_wedge = net2280_set_wedge,
1320 .fifo_status = net2280_fifo_status,
1321 .fifo_flush = net2280_fifo_flush,
1322 };
1323
1324 /*-------------------------------------------------------------------------*/
1325
1326 static int net2280_get_frame(struct usb_gadget *_gadget)
1327 {
1328 struct net2280 *dev;
1329 unsigned long flags;
1330 u16 retval;
1331
1332 if (!_gadget)
1333 return -ENODEV;
1334 dev = container_of(_gadget, struct net2280, gadget);
1335 spin_lock_irqsave(&dev->lock, flags);
1336 retval = get_idx_reg(dev->regs, REG_FRAME) & 0x03ff;
1337 spin_unlock_irqrestore(&dev->lock, flags);
1338 return retval;
1339 }
1340
1341 static int net2280_wakeup(struct usb_gadget *_gadget)
1342 {
1343 struct net2280 *dev;
1344 u32 tmp;
1345 unsigned long flags;
1346
1347 if (!_gadget)
1348 return 0;
1349 dev = container_of(_gadget, struct net2280, gadget);
1350
1351 spin_lock_irqsave(&dev->lock, flags);
1352 tmp = readl(&dev->usb->usbctl);
1353 if (tmp & BIT(DEVICE_REMOTE_WAKEUP_ENABLE))
1354 writel(BIT(GENERATE_RESUME), &dev->usb->usbstat);
1355 spin_unlock_irqrestore(&dev->lock, flags);
1356
1357 /* pci writes may still be posted */
1358 return 0;
1359 }
1360
1361 static int net2280_set_selfpowered(struct usb_gadget *_gadget, int value)
1362 {
1363 struct net2280 *dev;
1364 u32 tmp;
1365 unsigned long flags;
1366
1367 if (!_gadget)
1368 return 0;
1369 dev = container_of(_gadget, struct net2280, gadget);
1370
1371 spin_lock_irqsave(&dev->lock, flags);
1372 tmp = readl(&dev->usb->usbctl);
1373 if (value) {
1374 tmp |= BIT(SELF_POWERED_STATUS);
1375 _gadget->is_selfpowered = 1;
1376 } else {
1377 tmp &= ~BIT(SELF_POWERED_STATUS);
1378 _gadget->is_selfpowered = 0;
1379 }
1380 writel(tmp, &dev->usb->usbctl);
1381 spin_unlock_irqrestore(&dev->lock, flags);
1382
1383 return 0;
1384 }
1385
1386 static int net2280_pullup(struct usb_gadget *_gadget, int is_on)
1387 {
1388 struct net2280 *dev;
1389 u32 tmp;
1390 unsigned long flags;
1391
1392 if (!_gadget)
1393 return -ENODEV;
1394 dev = container_of(_gadget, struct net2280, gadget);
1395
1396 spin_lock_irqsave(&dev->lock, flags);
1397 tmp = readl(&dev->usb->usbctl);
1398 dev->softconnect = (is_on != 0);
1399 if (is_on)
1400 tmp |= BIT(USB_DETECT_ENABLE);
1401 else
1402 tmp &= ~BIT(USB_DETECT_ENABLE);
1403 writel(tmp, &dev->usb->usbctl);
1404 spin_unlock_irqrestore(&dev->lock, flags);
1405
1406 return 0;
1407 }
1408
1409 static int net2280_start(struct usb_gadget *_gadget,
1410 struct usb_gadget_driver *driver);
1411 static int net2280_stop(struct usb_gadget *_gadget);
1412
1413 static const struct usb_gadget_ops net2280_ops = {
1414 .get_frame = net2280_get_frame,
1415 .wakeup = net2280_wakeup,
1416 .set_selfpowered = net2280_set_selfpowered,
1417 .pullup = net2280_pullup,
1418 .udc_start = net2280_start,
1419 .udc_stop = net2280_stop,
1420 };
1421
1422 /*-------------------------------------------------------------------------*/
1423
1424 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1425
1426 /* FIXME move these into procfs, and use seq_file.
1427 * Sysfs _still_ doesn't behave for arbitrarily sized files,
1428 * and also doesn't help products using this with 2.4 kernels.
1429 */
1430
1431 /* "function" sysfs attribute */
1432 static ssize_t function_show(struct device *_dev, struct device_attribute *attr,
1433 char *buf)
1434 {
1435 struct net2280 *dev = dev_get_drvdata(_dev);
1436
1437 if (!dev->driver || !dev->driver->function ||
1438 strlen(dev->driver->function) > PAGE_SIZE)
1439 return 0;
1440 return scnprintf(buf, PAGE_SIZE, "%s\n", dev->driver->function);
1441 }
1442 static DEVICE_ATTR_RO(function);
1443
1444 static ssize_t registers_show(struct device *_dev,
1445 struct device_attribute *attr, char *buf)
1446 {
1447 struct net2280 *dev;
1448 char *next;
1449 unsigned size, t;
1450 unsigned long flags;
1451 int i;
1452 u32 t1, t2;
1453 const char *s;
1454
1455 dev = dev_get_drvdata(_dev);
1456 next = buf;
1457 size = PAGE_SIZE;
1458 spin_lock_irqsave(&dev->lock, flags);
1459
1460 if (dev->driver)
1461 s = dev->driver->driver.name;
1462 else
1463 s = "(none)";
1464
1465 /* Main Control Registers */
1466 t = scnprintf(next, size, "%s version " DRIVER_VERSION
1467 ", chiprev %04x\n\n"
1468 "devinit %03x fifoctl %08x gadget '%s'\n"
1469 "pci irqenb0 %02x irqenb1 %08x "
1470 "irqstat0 %04x irqstat1 %08x\n",
1471 driver_name, dev->chiprev,
1472 readl(&dev->regs->devinit),
1473 readl(&dev->regs->fifoctl),
1474 s,
1475 readl(&dev->regs->pciirqenb0),
1476 readl(&dev->regs->pciirqenb1),
1477 readl(&dev->regs->irqstat0),
1478 readl(&dev->regs->irqstat1));
1479 size -= t;
1480 next += t;
1481
1482 /* USB Control Registers */
1483 t1 = readl(&dev->usb->usbctl);
1484 t2 = readl(&dev->usb->usbstat);
1485 if (t1 & BIT(VBUS_PIN)) {
1486 if (t2 & BIT(HIGH_SPEED))
1487 s = "high speed";
1488 else if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1489 s = "powered";
1490 else
1491 s = "full speed";
1492 /* full speed bit (6) not working?? */
1493 } else
1494 s = "not attached";
1495 t = scnprintf(next, size,
1496 "stdrsp %08x usbctl %08x usbstat %08x "
1497 "addr 0x%02x (%s)\n",
1498 readl(&dev->usb->stdrsp), t1, t2,
1499 readl(&dev->usb->ouraddr), s);
1500 size -= t;
1501 next += t;
1502
1503 /* PCI Master Control Registers */
1504
1505 /* DMA Control Registers */
1506
1507 /* Configurable EP Control Registers */
1508 for (i = 0; i < dev->n_ep; i++) {
1509 struct net2280_ep *ep;
1510
1511 ep = &dev->ep[i];
1512 if (i && !ep->desc)
1513 continue;
1514
1515 t1 = readl(&ep->cfg->ep_cfg);
1516 t2 = readl(&ep->regs->ep_rsp) & 0xff;
1517 t = scnprintf(next, size,
1518 "\n%s\tcfg %05x rsp (%02x) %s%s%s%s%s%s%s%s"
1519 "irqenb %02x\n",
1520 ep->ep.name, t1, t2,
1521 (t2 & BIT(CLEAR_NAK_OUT_PACKETS))
1522 ? "NAK " : "",
1523 (t2 & BIT(CLEAR_EP_HIDE_STATUS_PHASE))
1524 ? "hide " : "",
1525 (t2 & BIT(CLEAR_EP_FORCE_CRC_ERROR))
1526 ? "CRC " : "",
1527 (t2 & BIT(CLEAR_INTERRUPT_MODE))
1528 ? "interrupt " : "",
1529 (t2 & BIT(CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE))
1530 ? "status " : "",
1531 (t2 & BIT(CLEAR_NAK_OUT_PACKETS_MODE))
1532 ? "NAKmode " : "",
1533 (t2 & BIT(CLEAR_ENDPOINT_TOGGLE))
1534 ? "DATA1 " : "DATA0 ",
1535 (t2 & BIT(CLEAR_ENDPOINT_HALT))
1536 ? "HALT " : "",
1537 readl(&ep->regs->ep_irqenb));
1538 size -= t;
1539 next += t;
1540
1541 t = scnprintf(next, size,
1542 "\tstat %08x avail %04x "
1543 "(ep%d%s-%s)%s\n",
1544 readl(&ep->regs->ep_stat),
1545 readl(&ep->regs->ep_avail),
1546 t1 & 0x0f, DIR_STRING(t1),
1547 type_string(t1 >> 8),
1548 ep->stopped ? "*" : "");
1549 size -= t;
1550 next += t;
1551
1552 if (!ep->dma)
1553 continue;
1554
1555 t = scnprintf(next, size,
1556 " dma\tctl %08x stat %08x count %08x\n"
1557 "\taddr %08x desc %08x\n",
1558 readl(&ep->dma->dmactl),
1559 readl(&ep->dma->dmastat),
1560 readl(&ep->dma->dmacount),
1561 readl(&ep->dma->dmaaddr),
1562 readl(&ep->dma->dmadesc));
1563 size -= t;
1564 next += t;
1565
1566 }
1567
1568 /* Indexed Registers (none yet) */
1569
1570 /* Statistics */
1571 t = scnprintf(next, size, "\nirqs: ");
1572 size -= t;
1573 next += t;
1574 for (i = 0; i < dev->n_ep; i++) {
1575 struct net2280_ep *ep;
1576
1577 ep = &dev->ep[i];
1578 if (i && !ep->irqs)
1579 continue;
1580 t = scnprintf(next, size, " %s/%lu", ep->ep.name, ep->irqs);
1581 size -= t;
1582 next += t;
1583
1584 }
1585 t = scnprintf(next, size, "\n");
1586 size -= t;
1587 next += t;
1588
1589 spin_unlock_irqrestore(&dev->lock, flags);
1590
1591 return PAGE_SIZE - size;
1592 }
1593 static DEVICE_ATTR_RO(registers);
1594
1595 static ssize_t queues_show(struct device *_dev, struct device_attribute *attr,
1596 char *buf)
1597 {
1598 struct net2280 *dev;
1599 char *next;
1600 unsigned size;
1601 unsigned long flags;
1602 int i;
1603
1604 dev = dev_get_drvdata(_dev);
1605 next = buf;
1606 size = PAGE_SIZE;
1607 spin_lock_irqsave(&dev->lock, flags);
1608
1609 for (i = 0; i < dev->n_ep; i++) {
1610 struct net2280_ep *ep = &dev->ep[i];
1611 struct net2280_request *req;
1612 int t;
1613
1614 if (i != 0) {
1615 const struct usb_endpoint_descriptor *d;
1616
1617 d = ep->desc;
1618 if (!d)
1619 continue;
1620 t = d->bEndpointAddress;
1621 t = scnprintf(next, size,
1622 "\n%s (ep%d%s-%s) max %04x %s fifo %d\n",
1623 ep->ep.name, t & USB_ENDPOINT_NUMBER_MASK,
1624 (t & USB_DIR_IN) ? "in" : "out",
1625 type_string(d->bmAttributes),
1626 usb_endpoint_maxp(d) & 0x1fff,
1627 ep->dma ? "dma" : "pio", ep->fifo_size
1628 );
1629 } else /* ep0 should only have one transfer queued */
1630 t = scnprintf(next, size, "ep0 max 64 pio %s\n",
1631 ep->is_in ? "in" : "out");
1632 if (t <= 0 || t > size)
1633 goto done;
1634 size -= t;
1635 next += t;
1636
1637 if (list_empty(&ep->queue)) {
1638 t = scnprintf(next, size, "\t(nothing queued)\n");
1639 if (t <= 0 || t > size)
1640 goto done;
1641 size -= t;
1642 next += t;
1643 continue;
1644 }
1645 list_for_each_entry(req, &ep->queue, queue) {
1646 if (ep->dma && req->td_dma == readl(&ep->dma->dmadesc))
1647 t = scnprintf(next, size,
1648 "\treq %p len %d/%d "
1649 "buf %p (dmacount %08x)\n",
1650 &req->req, req->req.actual,
1651 req->req.length, req->req.buf,
1652 readl(&ep->dma->dmacount));
1653 else
1654 t = scnprintf(next, size,
1655 "\treq %p len %d/%d buf %p\n",
1656 &req->req, req->req.actual,
1657 req->req.length, req->req.buf);
1658 if (t <= 0 || t > size)
1659 goto done;
1660 size -= t;
1661 next += t;
1662
1663 if (ep->dma) {
1664 struct net2280_dma *td;
1665
1666 td = req->td;
1667 t = scnprintf(next, size, "\t td %08x "
1668 " count %08x buf %08x desc %08x\n",
1669 (u32) req->td_dma,
1670 le32_to_cpu(td->dmacount),
1671 le32_to_cpu(td->dmaaddr),
1672 le32_to_cpu(td->dmadesc));
1673 if (t <= 0 || t > size)
1674 goto done;
1675 size -= t;
1676 next += t;
1677 }
1678 }
1679 }
1680
1681 done:
1682 spin_unlock_irqrestore(&dev->lock, flags);
1683 return PAGE_SIZE - size;
1684 }
1685 static DEVICE_ATTR_RO(queues);
1686
1687
1688 #else
1689
1690 #define device_create_file(a, b) (0)
1691 #define device_remove_file(a, b) do { } while (0)
1692
1693 #endif
1694
1695 /*-------------------------------------------------------------------------*/
1696
1697 /* another driver-specific mode might be a request type doing dma
1698 * to/from another device fifo instead of to/from memory.
1699 */
1700
1701 static void set_fifo_mode(struct net2280 *dev, int mode)
1702 {
1703 /* keeping high bits preserves BAR2 */
1704 writel((0xffff << PCI_BASE2_RANGE) | mode, &dev->regs->fifoctl);
1705
1706 /* always ep-{a,b,e,f} ... maybe not ep-c or ep-d */
1707 INIT_LIST_HEAD(&dev->gadget.ep_list);
1708 list_add_tail(&dev->ep[1].ep.ep_list, &dev->gadget.ep_list);
1709 list_add_tail(&dev->ep[2].ep.ep_list, &dev->gadget.ep_list);
1710 switch (mode) {
1711 case 0:
1712 list_add_tail(&dev->ep[3].ep.ep_list, &dev->gadget.ep_list);
1713 list_add_tail(&dev->ep[4].ep.ep_list, &dev->gadget.ep_list);
1714 dev->ep[1].fifo_size = dev->ep[2].fifo_size = 1024;
1715 break;
1716 case 1:
1717 dev->ep[1].fifo_size = dev->ep[2].fifo_size = 2048;
1718 break;
1719 case 2:
1720 list_add_tail(&dev->ep[3].ep.ep_list, &dev->gadget.ep_list);
1721 dev->ep[1].fifo_size = 2048;
1722 dev->ep[2].fifo_size = 1024;
1723 break;
1724 }
1725 /* fifo sizes for ep0, ep-c, ep-d, ep-e, and ep-f never change */
1726 list_add_tail(&dev->ep[5].ep.ep_list, &dev->gadget.ep_list);
1727 list_add_tail(&dev->ep[6].ep.ep_list, &dev->gadget.ep_list);
1728 }
1729
1730 static void defect7374_disable_data_eps(struct net2280 *dev)
1731 {
1732 /*
1733 * For Defect 7374, disable data EPs (and more):
1734 * - This phase undoes the earlier phase of the Defect 7374 workaround,
1735 * returing ep regs back to normal.
1736 */
1737 struct net2280_ep *ep;
1738 int i;
1739 unsigned char ep_sel;
1740 u32 tmp_reg;
1741
1742 for (i = 1; i < 5; i++) {
1743 ep = &dev->ep[i];
1744 writel(0, &ep->cfg->ep_cfg);
1745 }
1746
1747 /* CSROUT, CSRIN, PCIOUT, PCIIN, STATIN, RCIN */
1748 for (i = 0; i < 6; i++)
1749 writel(0, &dev->dep[i].dep_cfg);
1750
1751 for (ep_sel = 0; ep_sel <= 21; ep_sel++) {
1752 /* Select an endpoint for subsequent operations: */
1753 tmp_reg = readl(&dev->plregs->pl_ep_ctrl);
1754 writel(((tmp_reg & ~0x1f) | ep_sel), &dev->plregs->pl_ep_ctrl);
1755
1756 if (ep_sel < 2 || (ep_sel > 9 && ep_sel < 14) ||
1757 ep_sel == 18 || ep_sel == 20)
1758 continue;
1759
1760 /* Change settings on some selected endpoints */
1761 tmp_reg = readl(&dev->plregs->pl_ep_cfg_4);
1762 tmp_reg &= ~BIT(NON_CTRL_IN_TOLERATE_BAD_DIR);
1763 writel(tmp_reg, &dev->plregs->pl_ep_cfg_4);
1764 tmp_reg = readl(&dev->plregs->pl_ep_ctrl);
1765 tmp_reg |= BIT(EP_INITIALIZED);
1766 writel(tmp_reg, &dev->plregs->pl_ep_ctrl);
1767 }
1768 }
1769
1770 static void defect7374_enable_data_eps_zero(struct net2280 *dev)
1771 {
1772 u32 tmp = 0, tmp_reg;
1773 u32 scratch;
1774 int i;
1775 unsigned char ep_sel;
1776
1777 scratch = get_idx_reg(dev->regs, SCRATCH);
1778
1779 WARN_ON((scratch & (0xf << DEFECT7374_FSM_FIELD))
1780 == DEFECT7374_FSM_SS_CONTROL_READ);
1781
1782 scratch &= ~(0xf << DEFECT7374_FSM_FIELD);
1783
1784 ep_warn(dev, "Operate Defect 7374 workaround soft this time");
1785 ep_warn(dev, "It will operate on cold-reboot and SS connect");
1786
1787 /*GPEPs:*/
1788 tmp = ((0 << ENDPOINT_NUMBER) | BIT(ENDPOINT_DIRECTION) |
1789 (2 << OUT_ENDPOINT_TYPE) | (2 << IN_ENDPOINT_TYPE) |
1790 ((dev->enhanced_mode) ?
1791 BIT(OUT_ENDPOINT_ENABLE) : BIT(ENDPOINT_ENABLE)) |
1792 BIT(IN_ENDPOINT_ENABLE));
1793
1794 for (i = 1; i < 5; i++)
1795 writel(tmp, &dev->ep[i].cfg->ep_cfg);
1796
1797 /* CSRIN, PCIIN, STATIN, RCIN*/
1798 tmp = ((0 << ENDPOINT_NUMBER) | BIT(ENDPOINT_ENABLE));
1799 writel(tmp, &dev->dep[1].dep_cfg);
1800 writel(tmp, &dev->dep[3].dep_cfg);
1801 writel(tmp, &dev->dep[4].dep_cfg);
1802 writel(tmp, &dev->dep[5].dep_cfg);
1803
1804 /*Implemented for development and debug.
1805 * Can be refined/tuned later.*/
1806 for (ep_sel = 0; ep_sel <= 21; ep_sel++) {
1807 /* Select an endpoint for subsequent operations: */
1808 tmp_reg = readl(&dev->plregs->pl_ep_ctrl);
1809 writel(((tmp_reg & ~0x1f) | ep_sel),
1810 &dev->plregs->pl_ep_ctrl);
1811
1812 if (ep_sel == 1) {
1813 tmp =
1814 (readl(&dev->plregs->pl_ep_ctrl) |
1815 BIT(CLEAR_ACK_ERROR_CODE) | 0);
1816 writel(tmp, &dev->plregs->pl_ep_ctrl);
1817 continue;
1818 }
1819
1820 if (ep_sel == 0 || (ep_sel > 9 && ep_sel < 14) ||
1821 ep_sel == 18 || ep_sel == 20)
1822 continue;
1823
1824 tmp = (readl(&dev->plregs->pl_ep_cfg_4) |
1825 BIT(NON_CTRL_IN_TOLERATE_BAD_DIR) | 0);
1826 writel(tmp, &dev->plregs->pl_ep_cfg_4);
1827
1828 tmp = readl(&dev->plregs->pl_ep_ctrl) &
1829 ~BIT(EP_INITIALIZED);
1830 writel(tmp, &dev->plregs->pl_ep_ctrl);
1831
1832 }
1833
1834 /* Set FSM to focus on the first Control Read:
1835 * - Tip: Connection speed is known upon the first
1836 * setup request.*/
1837 scratch |= DEFECT7374_FSM_WAITING_FOR_CONTROL_READ;
1838 set_idx_reg(dev->regs, SCRATCH, scratch);
1839
1840 }
1841
1842 /* keeping it simple:
1843 * - one bus driver, initted first;
1844 * - one function driver, initted second
1845 *
1846 * most of the work to support multiple net2280 controllers would
1847 * be to associate this gadget driver (yes?) with all of them, or
1848 * perhaps to bind specific drivers to specific devices.
1849 */
1850
1851 static void usb_reset_228x(struct net2280 *dev)
1852 {
1853 u32 tmp;
1854
1855 dev->gadget.speed = USB_SPEED_UNKNOWN;
1856 (void) readl(&dev->usb->usbctl);
1857
1858 net2280_led_init(dev);
1859
1860 /* disable automatic responses, and irqs */
1861 writel(0, &dev->usb->stdrsp);
1862 writel(0, &dev->regs->pciirqenb0);
1863 writel(0, &dev->regs->pciirqenb1);
1864
1865 /* clear old dma and irq state */
1866 for (tmp = 0; tmp < 4; tmp++) {
1867 struct net2280_ep *ep = &dev->ep[tmp + 1];
1868 if (ep->dma)
1869 abort_dma(ep);
1870 }
1871
1872 writel(~0, &dev->regs->irqstat0),
1873 writel(~(u32)BIT(SUSPEND_REQUEST_INTERRUPT), &dev->regs->irqstat1),
1874
1875 /* reset, and enable pci */
1876 tmp = readl(&dev->regs->devinit) |
1877 BIT(PCI_ENABLE) |
1878 BIT(FIFO_SOFT_RESET) |
1879 BIT(USB_SOFT_RESET) |
1880 BIT(M8051_RESET);
1881 writel(tmp, &dev->regs->devinit);
1882
1883 /* standard fifo and endpoint allocations */
1884 set_fifo_mode(dev, (fifo_mode <= 2) ? fifo_mode : 0);
1885 }
1886
1887 static void usb_reset_338x(struct net2280 *dev)
1888 {
1889 u32 tmp;
1890
1891 dev->gadget.speed = USB_SPEED_UNKNOWN;
1892 (void)readl(&dev->usb->usbctl);
1893
1894 net2280_led_init(dev);
1895
1896 if (dev->bug7734_patched) {
1897 /* disable automatic responses, and irqs */
1898 writel(0, &dev->usb->stdrsp);
1899 writel(0, &dev->regs->pciirqenb0);
1900 writel(0, &dev->regs->pciirqenb1);
1901 }
1902
1903 /* clear old dma and irq state */
1904 for (tmp = 0; tmp < 4; tmp++) {
1905 struct net2280_ep *ep = &dev->ep[tmp + 1];
1906
1907 if (ep->dma)
1908 abort_dma(ep);
1909 }
1910
1911 writel(~0, &dev->regs->irqstat0), writel(~0, &dev->regs->irqstat1);
1912
1913 if (dev->bug7734_patched) {
1914 /* reset, and enable pci */
1915 tmp = readl(&dev->regs->devinit) |
1916 BIT(PCI_ENABLE) |
1917 BIT(FIFO_SOFT_RESET) |
1918 BIT(USB_SOFT_RESET) |
1919 BIT(M8051_RESET);
1920
1921 writel(tmp, &dev->regs->devinit);
1922 }
1923
1924 /* always ep-{1,2,3,4} ... maybe not ep-3 or ep-4 */
1925 INIT_LIST_HEAD(&dev->gadget.ep_list);
1926
1927 for (tmp = 1; tmp < dev->n_ep; tmp++)
1928 list_add_tail(&dev->ep[tmp].ep.ep_list, &dev->gadget.ep_list);
1929
1930 }
1931
1932 static void usb_reset(struct net2280 *dev)
1933 {
1934 if (dev->quirks & PLX_LEGACY)
1935 return usb_reset_228x(dev);
1936 return usb_reset_338x(dev);
1937 }
1938
1939 static void usb_reinit_228x(struct net2280 *dev)
1940 {
1941 u32 tmp;
1942
1943 /* basic endpoint init */
1944 for (tmp = 0; tmp < 7; tmp++) {
1945 struct net2280_ep *ep = &dev->ep[tmp];
1946
1947 ep->ep.name = ep_name[tmp];
1948 ep->dev = dev;
1949 ep->num = tmp;
1950
1951 if (tmp > 0 && tmp <= 4) {
1952 ep->fifo_size = 1024;
1953 ep->dma = &dev->dma[tmp - 1];
1954 } else
1955 ep->fifo_size = 64;
1956 ep->regs = &dev->epregs[tmp];
1957 ep->cfg = &dev->epregs[tmp];
1958 ep_reset_228x(dev->regs, ep);
1959 }
1960 usb_ep_set_maxpacket_limit(&dev->ep[0].ep, 64);
1961 usb_ep_set_maxpacket_limit(&dev->ep[5].ep, 64);
1962 usb_ep_set_maxpacket_limit(&dev->ep[6].ep, 64);
1963
1964 dev->gadget.ep0 = &dev->ep[0].ep;
1965 dev->ep[0].stopped = 0;
1966 INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
1967
1968 /* we want to prevent lowlevel/insecure access from the USB host,
1969 * but erratum 0119 means this enable bit is ignored
1970 */
1971 for (tmp = 0; tmp < 5; tmp++)
1972 writel(EP_DONTUSE, &dev->dep[tmp].dep_cfg);
1973 }
1974
1975 static void usb_reinit_338x(struct net2280 *dev)
1976 {
1977 int i;
1978 u32 tmp, val;
1979 static const u32 ne[9] = { 0, 1, 2, 3, 4, 1, 2, 3, 4 };
1980 static const u32 ep_reg_addr[9] = { 0x00, 0xC0, 0x00, 0xC0, 0x00,
1981 0x00, 0xC0, 0x00, 0xC0 };
1982
1983 /* basic endpoint init */
1984 for (i = 0; i < dev->n_ep; i++) {
1985 struct net2280_ep *ep = &dev->ep[i];
1986
1987 ep->ep.name = dev->enhanced_mode ? ep_name_adv[i] : ep_name[i];
1988 ep->dev = dev;
1989 ep->num = i;
1990
1991 if (i > 0 && i <= 4)
1992 ep->dma = &dev->dma[i - 1];
1993
1994 if (dev->enhanced_mode) {
1995 ep->cfg = &dev->epregs[ne[i]];
1996 ep->regs = (struct net2280_ep_regs __iomem *)
1997 (((void __iomem *)&dev->epregs[ne[i]]) +
1998 ep_reg_addr[i]);
1999 ep->fiforegs = &dev->fiforegs[i];
2000 } else {
2001 ep->cfg = &dev->epregs[i];
2002 ep->regs = &dev->epregs[i];
2003 ep->fiforegs = &dev->fiforegs[i];
2004 }
2005
2006 ep->fifo_size = (i != 0) ? 2048 : 512;
2007
2008 ep_reset_338x(dev->regs, ep);
2009 }
2010 usb_ep_set_maxpacket_limit(&dev->ep[0].ep, 512);
2011
2012 dev->gadget.ep0 = &dev->ep[0].ep;
2013 dev->ep[0].stopped = 0;
2014
2015 /* Link layer set up */
2016 if (dev->bug7734_patched) {
2017 tmp = readl(&dev->usb_ext->usbctl2) &
2018 ~(BIT(U1_ENABLE) | BIT(U2_ENABLE) | BIT(LTM_ENABLE));
2019 writel(tmp, &dev->usb_ext->usbctl2);
2020 }
2021
2022 /* Hardware Defect and Workaround */
2023 val = readl(&dev->ll_lfps_regs->ll_lfps_5);
2024 val &= ~(0xf << TIMER_LFPS_6US);
2025 val |= 0x5 << TIMER_LFPS_6US;
2026 writel(val, &dev->ll_lfps_regs->ll_lfps_5);
2027
2028 val = readl(&dev->ll_lfps_regs->ll_lfps_6);
2029 val &= ~(0xffff << TIMER_LFPS_80US);
2030 val |= 0x0100 << TIMER_LFPS_80US;
2031 writel(val, &dev->ll_lfps_regs->ll_lfps_6);
2032
2033 /*
2034 * AA_AB Errata. Issue 4. Workaround for SuperSpeed USB
2035 * Hot Reset Exit Handshake may Fail in Specific Case using
2036 * Default Register Settings. Workaround for Enumeration test.
2037 */
2038 val = readl(&dev->ll_tsn_regs->ll_tsn_counters_2);
2039 val &= ~(0x1f << HOT_TX_NORESET_TS2);
2040 val |= 0x10 << HOT_TX_NORESET_TS2;
2041 writel(val, &dev->ll_tsn_regs->ll_tsn_counters_2);
2042
2043 val = readl(&dev->ll_tsn_regs->ll_tsn_counters_3);
2044 val &= ~(0x1f << HOT_RX_RESET_TS2);
2045 val |= 0x3 << HOT_RX_RESET_TS2;
2046 writel(val, &dev->ll_tsn_regs->ll_tsn_counters_3);
2047
2048 /*
2049 * Set Recovery Idle to Recover bit:
2050 * - On SS connections, setting Recovery Idle to Recover Fmw improves
2051 * link robustness with various hosts and hubs.
2052 * - It is safe to set for all connection speeds; all chip revisions.
2053 * - R-M-W to leave other bits undisturbed.
2054 * - Reference PLX TT-7372
2055 */
2056 val = readl(&dev->ll_chicken_reg->ll_tsn_chicken_bit);
2057 val |= BIT(RECOVERY_IDLE_TO_RECOVER_FMW);
2058 writel(val, &dev->ll_chicken_reg->ll_tsn_chicken_bit);
2059
2060 INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
2061
2062 /* disable dedicated endpoints */
2063 writel(0x0D, &dev->dep[0].dep_cfg);
2064 writel(0x0D, &dev->dep[1].dep_cfg);
2065 writel(0x0E, &dev->dep[2].dep_cfg);
2066 writel(0x0E, &dev->dep[3].dep_cfg);
2067 writel(0x0F, &dev->dep[4].dep_cfg);
2068 writel(0x0C, &dev->dep[5].dep_cfg);
2069 }
2070
2071 static void usb_reinit(struct net2280 *dev)
2072 {
2073 if (dev->quirks & PLX_LEGACY)
2074 return usb_reinit_228x(dev);
2075 return usb_reinit_338x(dev);
2076 }
2077
2078 static void ep0_start_228x(struct net2280 *dev)
2079 {
2080 writel(BIT(CLEAR_EP_HIDE_STATUS_PHASE) |
2081 BIT(CLEAR_NAK_OUT_PACKETS) |
2082 BIT(CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE),
2083 &dev->epregs[0].ep_rsp);
2084
2085 /*
2086 * hardware optionally handles a bunch of standard requests
2087 * that the API hides from drivers anyway. have it do so.
2088 * endpoint status/features are handled in software, to
2089 * help pass tests for some dubious behavior.
2090 */
2091 writel(BIT(SET_TEST_MODE) |
2092 BIT(SET_ADDRESS) |
2093 BIT(DEVICE_SET_CLEAR_DEVICE_REMOTE_WAKEUP) |
2094 BIT(GET_DEVICE_STATUS) |
2095 BIT(GET_INTERFACE_STATUS),
2096 &dev->usb->stdrsp);
2097 writel(BIT(USB_ROOT_PORT_WAKEUP_ENABLE) |
2098 BIT(SELF_POWERED_USB_DEVICE) |
2099 BIT(REMOTE_WAKEUP_SUPPORT) |
2100 (dev->softconnect << USB_DETECT_ENABLE) |
2101 BIT(SELF_POWERED_STATUS),
2102 &dev->usb->usbctl);
2103
2104 /* enable irqs so we can see ep0 and general operation */
2105 writel(BIT(SETUP_PACKET_INTERRUPT_ENABLE) |
2106 BIT(ENDPOINT_0_INTERRUPT_ENABLE),
2107 &dev->regs->pciirqenb0);
2108 writel(BIT(PCI_INTERRUPT_ENABLE) |
2109 BIT(PCI_MASTER_ABORT_RECEIVED_INTERRUPT_ENABLE) |
2110 BIT(PCI_TARGET_ABORT_RECEIVED_INTERRUPT_ENABLE) |
2111 BIT(PCI_RETRY_ABORT_INTERRUPT_ENABLE) |
2112 BIT(VBUS_INTERRUPT_ENABLE) |
2113 BIT(ROOT_PORT_RESET_INTERRUPT_ENABLE) |
2114 BIT(SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE),
2115 &dev->regs->pciirqenb1);
2116
2117 /* don't leave any writes posted */
2118 (void) readl(&dev->usb->usbctl);
2119 }
2120
2121 static void ep0_start_338x(struct net2280 *dev)
2122 {
2123
2124 if (dev->bug7734_patched)
2125 writel(BIT(CLEAR_NAK_OUT_PACKETS_MODE) |
2126 BIT(SET_EP_HIDE_STATUS_PHASE),
2127 &dev->epregs[0].ep_rsp);
2128
2129 /*
2130 * hardware optionally handles a bunch of standard requests
2131 * that the API hides from drivers anyway. have it do so.
2132 * endpoint status/features are handled in software, to
2133 * help pass tests for some dubious behavior.
2134 */
2135 writel(BIT(SET_ISOCHRONOUS_DELAY) |
2136 BIT(SET_SEL) |
2137 BIT(SET_TEST_MODE) |
2138 BIT(SET_ADDRESS) |
2139 BIT(GET_INTERFACE_STATUS) |
2140 BIT(GET_DEVICE_STATUS),
2141 &dev->usb->stdrsp);
2142 dev->wakeup_enable = 1;
2143 writel(BIT(USB_ROOT_PORT_WAKEUP_ENABLE) |
2144 (dev->softconnect << USB_DETECT_ENABLE) |
2145 BIT(DEVICE_REMOTE_WAKEUP_ENABLE),
2146 &dev->usb->usbctl);
2147
2148 /* enable irqs so we can see ep0 and general operation */
2149 writel(BIT(SETUP_PACKET_INTERRUPT_ENABLE) |
2150 BIT(ENDPOINT_0_INTERRUPT_ENABLE),
2151 &dev->regs->pciirqenb0);
2152 writel(BIT(PCI_INTERRUPT_ENABLE) |
2153 BIT(ROOT_PORT_RESET_INTERRUPT_ENABLE) |
2154 BIT(SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE) |
2155 BIT(VBUS_INTERRUPT_ENABLE),
2156 &dev->regs->pciirqenb1);
2157
2158 /* don't leave any writes posted */
2159 (void)readl(&dev->usb->usbctl);
2160 }
2161
2162 static void ep0_start(struct net2280 *dev)
2163 {
2164 if (dev->quirks & PLX_LEGACY)
2165 return ep0_start_228x(dev);
2166 return ep0_start_338x(dev);
2167 }
2168
2169 /* when a driver is successfully registered, it will receive
2170 * control requests including set_configuration(), which enables
2171 * non-control requests. then usb traffic follows until a
2172 * disconnect is reported. then a host may connect again, or
2173 * the driver might get unbound.
2174 */
2175 static int net2280_start(struct usb_gadget *_gadget,
2176 struct usb_gadget_driver *driver)
2177 {
2178 struct net2280 *dev;
2179 int retval;
2180 unsigned i;
2181
2182 /* insist on high speed support from the driver, since
2183 * (dev->usb->xcvrdiag & FORCE_FULL_SPEED_MODE)
2184 * "must not be used in normal operation"
2185 */
2186 if (!driver || driver->max_speed < USB_SPEED_HIGH ||
2187 !driver->setup)
2188 return -EINVAL;
2189
2190 dev = container_of(_gadget, struct net2280, gadget);
2191
2192 for (i = 0; i < dev->n_ep; i++)
2193 dev->ep[i].irqs = 0;
2194
2195 /* hook up the driver ... */
2196 dev->softconnect = 1;
2197 driver->driver.bus = NULL;
2198 dev->driver = driver;
2199
2200 retval = device_create_file(&dev->pdev->dev, &dev_attr_function);
2201 if (retval)
2202 goto err_unbind;
2203 retval = device_create_file(&dev->pdev->dev, &dev_attr_queues);
2204 if (retval)
2205 goto err_func;
2206
2207 /* enable host detection and ep0; and we're ready
2208 * for set_configuration as well as eventual disconnect.
2209 */
2210 net2280_led_active(dev, 1);
2211
2212 if ((dev->quirks & PLX_SUPERSPEED) && !dev->bug7734_patched)
2213 defect7374_enable_data_eps_zero(dev);
2214
2215 ep0_start(dev);
2216
2217 /* pci writes may still be posted */
2218 return 0;
2219
2220 err_func:
2221 device_remove_file(&dev->pdev->dev, &dev_attr_function);
2222 err_unbind:
2223 dev->driver = NULL;
2224 return retval;
2225 }
2226
2227 static void stop_activity(struct net2280 *dev, struct usb_gadget_driver *driver)
2228 {
2229 int i;
2230
2231 /* don't disconnect if it's not connected */
2232 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
2233 driver = NULL;
2234
2235 /* stop hardware; prevent new request submissions;
2236 * and kill any outstanding requests.
2237 */
2238 usb_reset(dev);
2239 for (i = 0; i < dev->n_ep; i++)
2240 nuke(&dev->ep[i]);
2241
2242 /* report disconnect; the driver is already quiesced */
2243 if (driver) {
2244 spin_unlock(&dev->lock);
2245 driver->disconnect(&dev->gadget);
2246 spin_lock(&dev->lock);
2247 }
2248
2249 usb_reinit(dev);
2250 }
2251
2252 static int net2280_stop(struct usb_gadget *_gadget)
2253 {
2254 struct net2280 *dev;
2255 unsigned long flags;
2256
2257 dev = container_of(_gadget, struct net2280, gadget);
2258
2259 spin_lock_irqsave(&dev->lock, flags);
2260 stop_activity(dev, NULL);
2261 spin_unlock_irqrestore(&dev->lock, flags);
2262
2263 net2280_led_active(dev, 0);
2264
2265 device_remove_file(&dev->pdev->dev, &dev_attr_function);
2266 device_remove_file(&dev->pdev->dev, &dev_attr_queues);
2267
2268 dev->driver = NULL;
2269
2270 return 0;
2271 }
2272
2273 /*-------------------------------------------------------------------------*/
2274
2275 /* handle ep0, ep-e, ep-f with 64 byte packets: packet per irq.
2276 * also works for dma-capable endpoints, in pio mode or just
2277 * to manually advance the queue after short OUT transfers.
2278 */
2279 static void handle_ep_small(struct net2280_ep *ep)
2280 {
2281 struct net2280_request *req;
2282 u32 t;
2283 /* 0 error, 1 mid-data, 2 done */
2284 int mode = 1;
2285
2286 if (!list_empty(&ep->queue))
2287 req = list_entry(ep->queue.next,
2288 struct net2280_request, queue);
2289 else
2290 req = NULL;
2291
2292 /* ack all, and handle what we care about */
2293 t = readl(&ep->regs->ep_stat);
2294 ep->irqs++;
2295
2296 ep_vdbg(ep->dev, "%s ack ep_stat %08x, req %p\n",
2297 ep->ep.name, t, req ? &req->req : NULL);
2298
2299 if (!ep->is_in || (ep->dev->quirks & PLX_2280))
2300 writel(t & ~BIT(NAK_OUT_PACKETS), &ep->regs->ep_stat);
2301 else
2302 /* Added for 2282 */
2303 writel(t, &ep->regs->ep_stat);
2304
2305 /* for ep0, monitor token irqs to catch data stage length errors
2306 * and to synchronize on status.
2307 *
2308 * also, to defer reporting of protocol stalls ... here's where
2309 * data or status first appears, handling stalls here should never
2310 * cause trouble on the host side..
2311 *
2312 * control requests could be slightly faster without token synch for
2313 * status, but status can jam up that way.
2314 */
2315 if (unlikely(ep->num == 0)) {
2316 if (ep->is_in) {
2317 /* status; stop NAKing */
2318 if (t & BIT(DATA_OUT_PING_TOKEN_INTERRUPT)) {
2319 if (ep->dev->protocol_stall) {
2320 ep->stopped = 1;
2321 set_halt(ep);
2322 }
2323 if (!req)
2324 allow_status(ep);
2325 mode = 2;
2326 /* reply to extra IN data tokens with a zlp */
2327 } else if (t & BIT(DATA_IN_TOKEN_INTERRUPT)) {
2328 if (ep->dev->protocol_stall) {
2329 ep->stopped = 1;
2330 set_halt(ep);
2331 mode = 2;
2332 } else if (ep->responded &&
2333 !req && !ep->stopped)
2334 write_fifo(ep, NULL);
2335 }
2336 } else {
2337 /* status; stop NAKing */
2338 if (t & BIT(DATA_IN_TOKEN_INTERRUPT)) {
2339 if (ep->dev->protocol_stall) {
2340 ep->stopped = 1;
2341 set_halt(ep);
2342 }
2343 mode = 2;
2344 /* an extra OUT token is an error */
2345 } else if (((t & BIT(DATA_OUT_PING_TOKEN_INTERRUPT)) &&
2346 req &&
2347 req->req.actual == req->req.length) ||
2348 (ep->responded && !req)) {
2349 ep->dev->protocol_stall = 1;
2350 set_halt(ep);
2351 ep->stopped = 1;
2352 if (req)
2353 done(ep, req, -EOVERFLOW);
2354 req = NULL;
2355 }
2356 }
2357 }
2358
2359 if (unlikely(!req))
2360 return;
2361
2362 /* manual DMA queue advance after short OUT */
2363 if (likely(ep->dma)) {
2364 if (t & BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT)) {
2365 u32 count;
2366 int stopped = ep->stopped;
2367
2368 /* TRANSFERRED works around OUT_DONE erratum 0112.
2369 * we expect (N <= maxpacket) bytes; host wrote M.
2370 * iff (M < N) we won't ever see a DMA interrupt.
2371 */
2372 ep->stopped = 1;
2373 for (count = 0; ; t = readl(&ep->regs->ep_stat)) {
2374
2375 /* any preceding dma transfers must finish.
2376 * dma handles (M >= N), may empty the queue
2377 */
2378 scan_dma_completions(ep);
2379 if (unlikely(list_empty(&ep->queue) ||
2380 ep->out_overflow)) {
2381 req = NULL;
2382 break;
2383 }
2384 req = list_entry(ep->queue.next,
2385 struct net2280_request, queue);
2386
2387 /* here either (M < N), a "real" short rx;
2388 * or (M == N) and the queue didn't empty
2389 */
2390 if (likely(t & BIT(FIFO_EMPTY))) {
2391 count = readl(&ep->dma->dmacount);
2392 count &= DMA_BYTE_COUNT_MASK;
2393 if (readl(&ep->dma->dmadesc)
2394 != req->td_dma)
2395 req = NULL;
2396 break;
2397 }
2398 udelay(1);
2399 }
2400
2401 /* stop DMA, leave ep NAKing */
2402 writel(BIT(DMA_ABORT), &ep->dma->dmastat);
2403 spin_stop_dma(ep->dma);
2404
2405 if (likely(req)) {
2406 req->td->dmacount = 0;
2407 t = readl(&ep->regs->ep_avail);
2408 dma_done(ep, req, count,
2409 (ep->out_overflow || t)
2410 ? -EOVERFLOW : 0);
2411 }
2412
2413 /* also flush to prevent erratum 0106 trouble */
2414 if (unlikely(ep->out_overflow ||
2415 (ep->dev->chiprev == 0x0100 &&
2416 ep->dev->gadget.speed
2417 == USB_SPEED_FULL))) {
2418 out_flush(ep);
2419 ep->out_overflow = 0;
2420 }
2421
2422 /* (re)start dma if needed, stop NAKing */
2423 ep->stopped = stopped;
2424 if (!list_empty(&ep->queue))
2425 restart_dma(ep);
2426 } else
2427 ep_dbg(ep->dev, "%s dma ep_stat %08x ??\n",
2428 ep->ep.name, t);
2429 return;
2430
2431 /* data packet(s) received (in the fifo, OUT) */
2432 } else if (t & BIT(DATA_PACKET_RECEIVED_INTERRUPT)) {
2433 if (read_fifo(ep, req) && ep->num != 0)
2434 mode = 2;
2435
2436 /* data packet(s) transmitted (IN) */
2437 } else if (t & BIT(DATA_PACKET_TRANSMITTED_INTERRUPT)) {
2438 unsigned len;
2439
2440 len = req->req.length - req->req.actual;
2441 if (len > ep->ep.maxpacket)
2442 len = ep->ep.maxpacket;
2443 req->req.actual += len;
2444
2445 /* if we wrote it all, we're usually done */
2446 /* send zlps until the status stage */
2447 if ((req->req.actual == req->req.length) &&
2448 (!req->req.zero || len != ep->ep.maxpacket) && ep->num)
2449 mode = 2;
2450
2451 /* there was nothing to do ... */
2452 } else if (mode == 1)
2453 return;
2454
2455 /* done */
2456 if (mode == 2) {
2457 /* stream endpoints often resubmit/unlink in completion */
2458 done(ep, req, 0);
2459
2460 /* maybe advance queue to next request */
2461 if (ep->num == 0) {
2462 /* NOTE: net2280 could let gadget driver start the
2463 * status stage later. since not all controllers let
2464 * them control that, the api doesn't (yet) allow it.
2465 */
2466 if (!ep->stopped)
2467 allow_status(ep);
2468 req = NULL;
2469 } else {
2470 if (!list_empty(&ep->queue) && !ep->stopped)
2471 req = list_entry(ep->queue.next,
2472 struct net2280_request, queue);
2473 else
2474 req = NULL;
2475 if (req && !ep->is_in)
2476 stop_out_naking(ep);
2477 }
2478 }
2479
2480 /* is there a buffer for the next packet?
2481 * for best streaming performance, make sure there is one.
2482 */
2483 if (req && !ep->stopped) {
2484
2485 /* load IN fifo with next packet (may be zlp) */
2486 if (t & BIT(DATA_PACKET_TRANSMITTED_INTERRUPT))
2487 write_fifo(ep, &req->req);
2488 }
2489 }
2490
2491 static struct net2280_ep *get_ep_by_addr(struct net2280 *dev, u16 wIndex)
2492 {
2493 struct net2280_ep *ep;
2494
2495 if ((wIndex & USB_ENDPOINT_NUMBER_MASK) == 0)
2496 return &dev->ep[0];
2497 list_for_each_entry(ep, &dev->gadget.ep_list, ep.ep_list) {
2498 u8 bEndpointAddress;
2499
2500 if (!ep->desc)
2501 continue;
2502 bEndpointAddress = ep->desc->bEndpointAddress;
2503 if ((wIndex ^ bEndpointAddress) & USB_DIR_IN)
2504 continue;
2505 if ((wIndex & 0x0f) == (bEndpointAddress & 0x0f))
2506 return ep;
2507 }
2508 return NULL;
2509 }
2510
2511 static void defect7374_workaround(struct net2280 *dev, struct usb_ctrlrequest r)
2512 {
2513 u32 scratch, fsmvalue;
2514 u32 ack_wait_timeout, state;
2515
2516 /* Workaround for Defect 7374 (U1/U2 erroneously rejected): */
2517 scratch = get_idx_reg(dev->regs, SCRATCH);
2518 fsmvalue = scratch & (0xf << DEFECT7374_FSM_FIELD);
2519 scratch &= ~(0xf << DEFECT7374_FSM_FIELD);
2520
2521 if (!((fsmvalue == DEFECT7374_FSM_WAITING_FOR_CONTROL_READ) &&
2522 (r.bRequestType & USB_DIR_IN)))
2523 return;
2524
2525 /* This is the first Control Read for this connection: */
2526 if (!(readl(&dev->usb->usbstat) & BIT(SUPER_SPEED_MODE))) {
2527 /*
2528 * Connection is NOT SS:
2529 * - Connection must be FS or HS.
2530 * - This FSM state should allow workaround software to
2531 * run after the next USB connection.
2532 */
2533 scratch |= DEFECT7374_FSM_NON_SS_CONTROL_READ;
2534 dev->bug7734_patched = 1;
2535 goto restore_data_eps;
2536 }
2537
2538 /* Connection is SS: */
2539 for (ack_wait_timeout = 0;
2540 ack_wait_timeout < DEFECT_7374_NUMBEROF_MAX_WAIT_LOOPS;
2541 ack_wait_timeout++) {
2542
2543 state = readl(&dev->plregs->pl_ep_status_1)
2544 & (0xff << STATE);
2545 if ((state >= (ACK_GOOD_NORMAL << STATE)) &&
2546 (state <= (ACK_GOOD_MORE_ACKS_TO_COME << STATE))) {
2547 scratch |= DEFECT7374_FSM_SS_CONTROL_READ;
2548 dev->bug7734_patched = 1;
2549 break;
2550 }
2551
2552 /*
2553 * We have not yet received host's Data Phase ACK
2554 * - Wait and try again.
2555 */
2556 udelay(DEFECT_7374_PROCESSOR_WAIT_TIME);
2557
2558 continue;
2559 }
2560
2561
2562 if (ack_wait_timeout >= DEFECT_7374_NUMBEROF_MAX_WAIT_LOOPS) {
2563 ep_err(dev, "FAIL: Defect 7374 workaround waited but failed "
2564 "to detect SS host's data phase ACK.");
2565 ep_err(dev, "PL_EP_STATUS_1(23:16):.Expected from 0x11 to 0x16"
2566 "got 0x%2.2x.\n", state >> STATE);
2567 } else {
2568 ep_warn(dev, "INFO: Defect 7374 workaround waited about\n"
2569 "%duSec for Control Read Data Phase ACK\n",
2570 DEFECT_7374_PROCESSOR_WAIT_TIME * ack_wait_timeout);
2571 }
2572
2573 restore_data_eps:
2574 /*
2575 * Restore data EPs to their pre-workaround settings (disabled,
2576 * initialized, and other details).
2577 */
2578 defect7374_disable_data_eps(dev);
2579
2580 set_idx_reg(dev->regs, SCRATCH, scratch);
2581
2582 return;
2583 }
2584
2585 static void ep_clear_seqnum(struct net2280_ep *ep)
2586 {
2587 struct net2280 *dev = ep->dev;
2588 u32 val;
2589 static const u32 ep_pl[9] = { 0, 3, 4, 7, 8, 2, 5, 6, 9 };
2590
2591 val = readl(&dev->plregs->pl_ep_ctrl) & ~0x1f;
2592 val |= ep_pl[ep->num];
2593 writel(val, &dev->plregs->pl_ep_ctrl);
2594 val |= BIT(SEQUENCE_NUMBER_RESET);
2595 writel(val, &dev->plregs->pl_ep_ctrl);
2596
2597 return;
2598 }
2599
2600 static void handle_stat0_irqs_superspeed(struct net2280 *dev,
2601 struct net2280_ep *ep, struct usb_ctrlrequest r)
2602 {
2603 int tmp = 0;
2604
2605 #define w_value le16_to_cpu(r.wValue)
2606 #define w_index le16_to_cpu(r.wIndex)
2607 #define w_length le16_to_cpu(r.wLength)
2608
2609 switch (r.bRequest) {
2610 struct net2280_ep *e;
2611 u16 status;
2612
2613 case USB_REQ_SET_CONFIGURATION:
2614 dev->addressed_state = !w_value;
2615 goto usb3_delegate;
2616
2617 case USB_REQ_GET_STATUS:
2618 switch (r.bRequestType) {
2619 case (USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE):
2620 status = dev->wakeup_enable ? 0x02 : 0x00;
2621 if (dev->gadget.is_selfpowered)
2622 status |= BIT(0);
2623 status |= (dev->u1_enable << 2 | dev->u2_enable << 3 |
2624 dev->ltm_enable << 4);
2625 writel(0, &dev->epregs[0].ep_irqenb);
2626 set_fifo_bytecount(ep, sizeof(status));
2627 writel((__force u32) status, &dev->epregs[0].ep_data);
2628 allow_status_338x(ep);
2629 break;
2630
2631 case (USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_ENDPOINT):
2632 e = get_ep_by_addr(dev, w_index);
2633 if (!e)
2634 goto do_stall3;
2635 status = readl(&e->regs->ep_rsp) &
2636 BIT(CLEAR_ENDPOINT_HALT);
2637 writel(0, &dev->epregs[0].ep_irqenb);
2638 set_fifo_bytecount(ep, sizeof(status));
2639 writel((__force u32) status, &dev->epregs[0].ep_data);
2640 allow_status_338x(ep);
2641 break;
2642
2643 default:
2644 goto usb3_delegate;
2645 }
2646 break;
2647
2648 case USB_REQ_CLEAR_FEATURE:
2649 switch (r.bRequestType) {
2650 case (USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE):
2651 if (!dev->addressed_state) {
2652 switch (w_value) {
2653 case USB_DEVICE_U1_ENABLE:
2654 dev->u1_enable = 0;
2655 writel(readl(&dev->usb_ext->usbctl2) &
2656 ~BIT(U1_ENABLE),
2657 &dev->usb_ext->usbctl2);
2658 allow_status_338x(ep);
2659 goto next_endpoints3;
2660
2661 case USB_DEVICE_U2_ENABLE:
2662 dev->u2_enable = 0;
2663 writel(readl(&dev->usb_ext->usbctl2) &
2664 ~BIT(U2_ENABLE),
2665 &dev->usb_ext->usbctl2);
2666 allow_status_338x(ep);
2667 goto next_endpoints3;
2668
2669 case USB_DEVICE_LTM_ENABLE:
2670 dev->ltm_enable = 0;
2671 writel(readl(&dev->usb_ext->usbctl2) &
2672 ~BIT(LTM_ENABLE),
2673 &dev->usb_ext->usbctl2);
2674 allow_status_338x(ep);
2675 goto next_endpoints3;
2676
2677 default:
2678 break;
2679 }
2680 }
2681 if (w_value == USB_DEVICE_REMOTE_WAKEUP) {
2682 dev->wakeup_enable = 0;
2683 writel(readl(&dev->usb->usbctl) &
2684 ~BIT(DEVICE_REMOTE_WAKEUP_ENABLE),
2685 &dev->usb->usbctl);
2686 allow_status_338x(ep);
2687 break;
2688 }
2689 goto usb3_delegate;
2690
2691 case (USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_ENDPOINT):
2692 e = get_ep_by_addr(dev, w_index);
2693 if (!e)
2694 goto do_stall3;
2695 if (w_value != USB_ENDPOINT_HALT)
2696 goto do_stall3;
2697 ep_vdbg(dev, "%s clear halt\n", e->ep.name);
2698 /*
2699 * Workaround for SS SeqNum not cleared via
2700 * Endpoint Halt (Clear) bit. select endpoint
2701 */
2702 ep_clear_seqnum(e);
2703 clear_halt(e);
2704 if (!list_empty(&e->queue) && e->td_dma)
2705 restart_dma(e);
2706 allow_status(ep);
2707 ep->stopped = 1;
2708 break;
2709
2710 default:
2711 goto usb3_delegate;
2712 }
2713 break;
2714 case USB_REQ_SET_FEATURE:
2715 switch (r.bRequestType) {
2716 case (USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE):
2717 if (!dev->addressed_state) {
2718 switch (w_value) {
2719 case USB_DEVICE_U1_ENABLE:
2720 dev->u1_enable = 1;
2721 writel(readl(&dev->usb_ext->usbctl2) |
2722 BIT(U1_ENABLE),
2723 &dev->usb_ext->usbctl2);
2724 allow_status_338x(ep);
2725 goto next_endpoints3;
2726
2727 case USB_DEVICE_U2_ENABLE:
2728 dev->u2_enable = 1;
2729 writel(readl(&dev->usb_ext->usbctl2) |
2730 BIT(U2_ENABLE),
2731 &dev->usb_ext->usbctl2);
2732 allow_status_338x(ep);
2733 goto next_endpoints3;
2734
2735 case USB_DEVICE_LTM_ENABLE:
2736 dev->ltm_enable = 1;
2737 writel(readl(&dev->usb_ext->usbctl2) |
2738 BIT(LTM_ENABLE),
2739 &dev->usb_ext->usbctl2);
2740 allow_status_338x(ep);
2741 goto next_endpoints3;
2742 default:
2743 break;
2744 }
2745 }
2746
2747 if (w_value == USB_DEVICE_REMOTE_WAKEUP) {
2748 dev->wakeup_enable = 1;
2749 writel(readl(&dev->usb->usbctl) |
2750 BIT(DEVICE_REMOTE_WAKEUP_ENABLE),
2751 &dev->usb->usbctl);
2752 allow_status_338x(ep);
2753 break;
2754 }
2755 goto usb3_delegate;
2756
2757 case (USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_ENDPOINT):
2758 e = get_ep_by_addr(dev, w_index);
2759 if (!e || (w_value != USB_ENDPOINT_HALT))
2760 goto do_stall3;
2761 ep->stopped = 1;
2762 if (ep->num == 0)
2763 ep->dev->protocol_stall = 1;
2764 else {
2765 if (ep->dma)
2766 abort_dma(ep);
2767 set_halt(ep);
2768 }
2769 allow_status_338x(ep);
2770 break;
2771
2772 default:
2773 goto usb3_delegate;
2774 }
2775
2776 break;
2777 default:
2778
2779 usb3_delegate:
2780 ep_vdbg(dev, "setup %02x.%02x v%04x i%04x l%04x ep_cfg %08x\n",
2781 r.bRequestType, r.bRequest,
2782 w_value, w_index, w_length,
2783 readl(&ep->cfg->ep_cfg));
2784
2785 ep->responded = 0;
2786 spin_unlock(&dev->lock);
2787 tmp = dev->driver->setup(&dev->gadget, &r);
2788 spin_lock(&dev->lock);
2789 }
2790 do_stall3:
2791 if (tmp < 0) {
2792 ep_vdbg(dev, "req %02x.%02x protocol STALL; stat %d\n",
2793 r.bRequestType, r.bRequest, tmp);
2794 dev->protocol_stall = 1;
2795 /* TD 9.9 Halt Endpoint test. TD 9.22 Set feature test */
2796 set_halt(ep);
2797 }
2798
2799 next_endpoints3:
2800
2801 #undef w_value
2802 #undef w_index
2803 #undef w_length
2804
2805 return;
2806 }
2807
2808 static void handle_stat0_irqs(struct net2280 *dev, u32 stat)
2809 {
2810 struct net2280_ep *ep;
2811 u32 num, scratch;
2812
2813 /* most of these don't need individual acks */
2814 stat &= ~BIT(INTA_ASSERTED);
2815 if (!stat)
2816 return;
2817 /* ep_dbg(dev, "irqstat0 %04x\n", stat); */
2818
2819 /* starting a control request? */
2820 if (unlikely(stat & BIT(SETUP_PACKET_INTERRUPT))) {
2821 union {
2822 u32 raw[2];
2823 struct usb_ctrlrequest r;
2824 } u;
2825 int tmp;
2826 struct net2280_request *req;
2827
2828 if (dev->gadget.speed == USB_SPEED_UNKNOWN) {
2829 u32 val = readl(&dev->usb->usbstat);
2830 if (val & BIT(SUPER_SPEED)) {
2831 dev->gadget.speed = USB_SPEED_SUPER;
2832 usb_ep_set_maxpacket_limit(&dev->ep[0].ep,
2833 EP0_SS_MAX_PACKET_SIZE);
2834 } else if (val & BIT(HIGH_SPEED)) {
2835 dev->gadget.speed = USB_SPEED_HIGH;
2836 usb_ep_set_maxpacket_limit(&dev->ep[0].ep,
2837 EP0_HS_MAX_PACKET_SIZE);
2838 } else {
2839 dev->gadget.speed = USB_SPEED_FULL;
2840 usb_ep_set_maxpacket_limit(&dev->ep[0].ep,
2841 EP0_HS_MAX_PACKET_SIZE);
2842 }
2843 net2280_led_speed(dev, dev->gadget.speed);
2844 ep_dbg(dev, "%s\n",
2845 usb_speed_string(dev->gadget.speed));
2846 }
2847
2848 ep = &dev->ep[0];
2849 ep->irqs++;
2850
2851 /* make sure any leftover request state is cleared */
2852 stat &= ~BIT(ENDPOINT_0_INTERRUPT);
2853 while (!list_empty(&ep->queue)) {
2854 req = list_entry(ep->queue.next,
2855 struct net2280_request, queue);
2856 done(ep, req, (req->req.actual == req->req.length)
2857 ? 0 : -EPROTO);
2858 }
2859 ep->stopped = 0;
2860 dev->protocol_stall = 0;
2861 if (!(dev->quirks & PLX_SUPERSPEED)) {
2862 if (ep->dev->quirks & PLX_2280)
2863 tmp = BIT(FIFO_OVERFLOW) |
2864 BIT(FIFO_UNDERFLOW);
2865 else
2866 tmp = 0;
2867
2868 writel(tmp | BIT(TIMEOUT) |
2869 BIT(USB_STALL_SENT) |
2870 BIT(USB_IN_NAK_SENT) |
2871 BIT(USB_IN_ACK_RCVD) |
2872 BIT(USB_OUT_PING_NAK_SENT) |
2873 BIT(USB_OUT_ACK_SENT) |
2874 BIT(SHORT_PACKET_OUT_DONE_INTERRUPT) |
2875 BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT) |
2876 BIT(DATA_PACKET_RECEIVED_INTERRUPT) |
2877 BIT(DATA_PACKET_TRANSMITTED_INTERRUPT) |
2878 BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
2879 BIT(DATA_IN_TOKEN_INTERRUPT),
2880 &ep->regs->ep_stat);
2881 }
2882 u.raw[0] = readl(&dev->usb->setup0123);
2883 u.raw[1] = readl(&dev->usb->setup4567);
2884
2885 cpu_to_le32s(&u.raw[0]);
2886 cpu_to_le32s(&u.raw[1]);
2887
2888 if ((dev->quirks & PLX_SUPERSPEED) && !dev->bug7734_patched)
2889 defect7374_workaround(dev, u.r);
2890
2891 tmp = 0;
2892
2893 #define w_value le16_to_cpu(u.r.wValue)
2894 #define w_index le16_to_cpu(u.r.wIndex)
2895 #define w_length le16_to_cpu(u.r.wLength)
2896
2897 /* ack the irq */
2898 writel(BIT(SETUP_PACKET_INTERRUPT), &dev->regs->irqstat0);
2899 stat ^= BIT(SETUP_PACKET_INTERRUPT);
2900
2901 /* watch control traffic at the token level, and force
2902 * synchronization before letting the status stage happen.
2903 * FIXME ignore tokens we'll NAK, until driver responds.
2904 * that'll mean a lot less irqs for some drivers.
2905 */
2906 ep->is_in = (u.r.bRequestType & USB_DIR_IN) != 0;
2907 if (ep->is_in) {
2908 scratch = BIT(DATA_PACKET_TRANSMITTED_INTERRUPT) |
2909 BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
2910 BIT(DATA_IN_TOKEN_INTERRUPT);
2911 stop_out_naking(ep);
2912 } else
2913 scratch = BIT(DATA_PACKET_RECEIVED_INTERRUPT) |
2914 BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
2915 BIT(DATA_IN_TOKEN_INTERRUPT);
2916 writel(scratch, &dev->epregs[0].ep_irqenb);
2917
2918 /* we made the hardware handle most lowlevel requests;
2919 * everything else goes uplevel to the gadget code.
2920 */
2921 ep->responded = 1;
2922
2923 if (dev->gadget.speed == USB_SPEED_SUPER) {
2924 handle_stat0_irqs_superspeed(dev, ep, u.r);
2925 goto next_endpoints;
2926 }
2927
2928 switch (u.r.bRequest) {
2929 case USB_REQ_GET_STATUS: {
2930 struct net2280_ep *e;
2931 __le32 status;
2932
2933 /* hw handles device and interface status */
2934 if (u.r.bRequestType != (USB_DIR_IN|USB_RECIP_ENDPOINT))
2935 goto delegate;
2936 e = get_ep_by_addr(dev, w_index);
2937 if (!e || w_length > 2)
2938 goto do_stall;
2939
2940 if (readl(&e->regs->ep_rsp) & BIT(SET_ENDPOINT_HALT))
2941 status = cpu_to_le32(1);
2942 else
2943 status = cpu_to_le32(0);
2944
2945 /* don't bother with a request object! */
2946 writel(0, &dev->epregs[0].ep_irqenb);
2947 set_fifo_bytecount(ep, w_length);
2948 writel((__force u32)status, &dev->epregs[0].ep_data);
2949 allow_status(ep);
2950 ep_vdbg(dev, "%s stat %02x\n", ep->ep.name, status);
2951 goto next_endpoints;
2952 }
2953 break;
2954 case USB_REQ_CLEAR_FEATURE: {
2955 struct net2280_ep *e;
2956
2957 /* hw handles device features */
2958 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
2959 goto delegate;
2960 if (w_value != USB_ENDPOINT_HALT || w_length != 0)
2961 goto do_stall;
2962 e = get_ep_by_addr(dev, w_index);
2963 if (!e)
2964 goto do_stall;
2965 if (e->wedged) {
2966 ep_vdbg(dev, "%s wedged, halt not cleared\n",
2967 ep->ep.name);
2968 } else {
2969 ep_vdbg(dev, "%s clear halt\n", e->ep.name);
2970 clear_halt(e);
2971 if ((ep->dev->quirks & PLX_SUPERSPEED) &&
2972 !list_empty(&e->queue) && e->td_dma)
2973 restart_dma(e);
2974 }
2975 allow_status(ep);
2976 goto next_endpoints;
2977 }
2978 break;
2979 case USB_REQ_SET_FEATURE: {
2980 struct net2280_ep *e;
2981
2982 /* hw handles device features */
2983 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
2984 goto delegate;
2985 if (w_value != USB_ENDPOINT_HALT || w_length != 0)
2986 goto do_stall;
2987 e = get_ep_by_addr(dev, w_index);
2988 if (!e)
2989 goto do_stall;
2990 if (e->ep.name == ep0name)
2991 goto do_stall;
2992 set_halt(e);
2993 if ((dev->quirks & PLX_SUPERSPEED) && e->dma)
2994 abort_dma(e);
2995 allow_status(ep);
2996 ep_vdbg(dev, "%s set halt\n", ep->ep.name);
2997 goto next_endpoints;
2998 }
2999 break;
3000 default:
3001 delegate:
3002 ep_vdbg(dev, "setup %02x.%02x v%04x i%04x l%04x "
3003 "ep_cfg %08x\n",
3004 u.r.bRequestType, u.r.bRequest,
3005 w_value, w_index, w_length,
3006 readl(&ep->cfg->ep_cfg));
3007 ep->responded = 0;
3008 spin_unlock(&dev->lock);
3009 tmp = dev->driver->setup(&dev->gadget, &u.r);
3010 spin_lock(&dev->lock);
3011 }
3012
3013 /* stall ep0 on error */
3014 if (tmp < 0) {
3015 do_stall:
3016 ep_vdbg(dev, "req %02x.%02x protocol STALL; stat %d\n",
3017 u.r.bRequestType, u.r.bRequest, tmp);
3018 dev->protocol_stall = 1;
3019 }
3020
3021 /* some in/out token irq should follow; maybe stall then.
3022 * driver must queue a request (even zlp) or halt ep0
3023 * before the host times out.
3024 */
3025 }
3026
3027 #undef w_value
3028 #undef w_index
3029 #undef w_length
3030
3031 next_endpoints:
3032 /* endpoint data irq ? */
3033 scratch = stat & 0x7f;
3034 stat &= ~0x7f;
3035 for (num = 0; scratch; num++) {
3036 u32 t;
3037
3038 /* do this endpoint's FIFO and queue need tending? */
3039 t = BIT(num);
3040 if ((scratch & t) == 0)
3041 continue;
3042 scratch ^= t;
3043
3044 ep = &dev->ep[num];
3045 handle_ep_small(ep);
3046 }
3047
3048 if (stat)
3049 ep_dbg(dev, "unhandled irqstat0 %08x\n", stat);
3050 }
3051
3052 #define DMA_INTERRUPTS (BIT(DMA_D_INTERRUPT) | \
3053 BIT(DMA_C_INTERRUPT) | \
3054 BIT(DMA_B_INTERRUPT) | \
3055 BIT(DMA_A_INTERRUPT))
3056 #define PCI_ERROR_INTERRUPTS ( \
3057 BIT(PCI_MASTER_ABORT_RECEIVED_INTERRUPT) | \
3058 BIT(PCI_TARGET_ABORT_RECEIVED_INTERRUPT) | \
3059 BIT(PCI_RETRY_ABORT_INTERRUPT))
3060
3061 static void handle_stat1_irqs(struct net2280 *dev, u32 stat)
3062 {
3063 struct net2280_ep *ep;
3064 u32 tmp, num, mask, scratch;
3065
3066 /* after disconnect there's nothing else to do! */
3067 tmp = BIT(VBUS_INTERRUPT) | BIT(ROOT_PORT_RESET_INTERRUPT);
3068 mask = BIT(SUPER_SPEED) | BIT(HIGH_SPEED) | BIT(FULL_SPEED);
3069
3070 /* VBUS disconnect is indicated by VBUS_PIN and VBUS_INTERRUPT set.
3071 * Root Port Reset is indicated by ROOT_PORT_RESET_INTERRUPT set and
3072 * both HIGH_SPEED and FULL_SPEED clear (as ROOT_PORT_RESET_INTERRUPT
3073 * only indicates a change in the reset state).
3074 */
3075 if (stat & tmp) {
3076 bool reset = false;
3077 bool disconnect = false;
3078
3079 /*
3080 * Ignore disconnects and resets if the speed hasn't been set.
3081 * VBUS can bounce and there's always an initial reset.
3082 */
3083 writel(tmp, &dev->regs->irqstat1);
3084 if (dev->gadget.speed != USB_SPEED_UNKNOWN) {
3085 if ((stat & BIT(VBUS_INTERRUPT)) &&
3086 (readl(&dev->usb->usbctl) &
3087 BIT(VBUS_PIN)) == 0) {
3088 disconnect = true;
3089 ep_dbg(dev, "disconnect %s\n",
3090 dev->driver->driver.name);
3091 } else if ((stat & BIT(ROOT_PORT_RESET_INTERRUPT)) &&
3092 (readl(&dev->usb->usbstat) & mask)
3093 == 0) {
3094 reset = true;
3095 ep_dbg(dev, "reset %s\n",
3096 dev->driver->driver.name);
3097 }
3098
3099 if (disconnect || reset) {
3100 stop_activity(dev, dev->driver);
3101 ep0_start(dev);
3102 spin_unlock(&dev->lock);
3103 if (reset)
3104 usb_gadget_udc_reset
3105 (&dev->gadget, dev->driver);
3106 else
3107 (dev->driver->disconnect)
3108 (&dev->gadget);
3109 spin_lock(&dev->lock);
3110 return;
3111 }
3112 }
3113 stat &= ~tmp;
3114
3115 /* vBUS can bounce ... one of many reasons to ignore the
3116 * notion of hotplug events on bus connect/disconnect!
3117 */
3118 if (!stat)
3119 return;
3120 }
3121
3122 /* NOTE: chip stays in PCI D0 state for now, but it could
3123 * enter D1 to save more power
3124 */
3125 tmp = BIT(SUSPEND_REQUEST_CHANGE_INTERRUPT);
3126 if (stat & tmp) {
3127 writel(tmp, &dev->regs->irqstat1);
3128 if (stat & BIT(SUSPEND_REQUEST_INTERRUPT)) {
3129 if (dev->driver->suspend)
3130 dev->driver->suspend(&dev->gadget);
3131 if (!enable_suspend)
3132 stat &= ~BIT(SUSPEND_REQUEST_INTERRUPT);
3133 } else {
3134 if (dev->driver->resume)
3135 dev->driver->resume(&dev->gadget);
3136 /* at high speed, note erratum 0133 */
3137 }
3138 stat &= ~tmp;
3139 }
3140
3141 /* clear any other status/irqs */
3142 if (stat)
3143 writel(stat, &dev->regs->irqstat1);
3144
3145 /* some status we can just ignore */
3146 if (dev->quirks & PLX_2280)
3147 stat &= ~(BIT(CONTROL_STATUS_INTERRUPT) |
3148 BIT(SUSPEND_REQUEST_INTERRUPT) |
3149 BIT(RESUME_INTERRUPT) |
3150 BIT(SOF_INTERRUPT));
3151 else
3152 stat &= ~(BIT(CONTROL_STATUS_INTERRUPT) |
3153 BIT(RESUME_INTERRUPT) |
3154 BIT(SOF_DOWN_INTERRUPT) |
3155 BIT(SOF_INTERRUPT));
3156
3157 if (!stat)
3158 return;
3159 /* ep_dbg(dev, "irqstat1 %08x\n", stat);*/
3160
3161 /* DMA status, for ep-{a,b,c,d} */
3162 scratch = stat & DMA_INTERRUPTS;
3163 stat &= ~DMA_INTERRUPTS;
3164 scratch >>= 9;
3165 for (num = 0; scratch; num++) {
3166 struct net2280_dma_regs __iomem *dma;
3167
3168 tmp = BIT(num);
3169 if ((tmp & scratch) == 0)
3170 continue;
3171 scratch ^= tmp;
3172
3173 ep = &dev->ep[num + 1];
3174 dma = ep->dma;
3175
3176 if (!dma)
3177 continue;
3178
3179 /* clear ep's dma status */
3180 tmp = readl(&dma->dmastat);
3181 writel(tmp, &dma->dmastat);
3182
3183 /* dma sync*/
3184 if (dev->quirks & PLX_SUPERSPEED) {
3185 u32 r_dmacount = readl(&dma->dmacount);
3186 if (!ep->is_in && (r_dmacount & 0x00FFFFFF) &&
3187 (tmp & BIT(DMA_TRANSACTION_DONE_INTERRUPT)))
3188 continue;
3189 }
3190
3191 if (!(tmp & BIT(DMA_TRANSACTION_DONE_INTERRUPT))) {
3192 ep_dbg(ep->dev, "%s no xact done? %08x\n",
3193 ep->ep.name, tmp);
3194 continue;
3195 }
3196 stop_dma(ep->dma);
3197
3198 /* OUT transfers terminate when the data from the
3199 * host is in our memory. Process whatever's done.
3200 * On this path, we know transfer's last packet wasn't
3201 * less than req->length. NAK_OUT_PACKETS may be set,
3202 * or the FIFO may already be holding new packets.
3203 *
3204 * IN transfers can linger in the FIFO for a very
3205 * long time ... we ignore that for now, accounting
3206 * precisely (like PIO does) needs per-packet irqs
3207 */
3208 scan_dma_completions(ep);
3209
3210 /* disable dma on inactive queues; else maybe restart */
3211 if (!list_empty(&ep->queue)) {
3212 tmp = readl(&dma->dmactl);
3213 restart_dma(ep);
3214 }
3215 ep->irqs++;
3216 }
3217
3218 /* NOTE: there are other PCI errors we might usefully notice.
3219 * if they appear very often, here's where to try recovering.
3220 */
3221 if (stat & PCI_ERROR_INTERRUPTS) {
3222 ep_err(dev, "pci dma error; stat %08x\n", stat);
3223 stat &= ~PCI_ERROR_INTERRUPTS;
3224 /* these are fatal errors, but "maybe" they won't
3225 * happen again ...
3226 */
3227 stop_activity(dev, dev->driver);
3228 ep0_start(dev);
3229 stat = 0;
3230 }
3231
3232 if (stat)
3233 ep_dbg(dev, "unhandled irqstat1 %08x\n", stat);
3234 }
3235
3236 static irqreturn_t net2280_irq(int irq, void *_dev)
3237 {
3238 struct net2280 *dev = _dev;
3239
3240 /* shared interrupt, not ours */
3241 if ((dev->quirks & PLX_LEGACY) &&
3242 (!(readl(&dev->regs->irqstat0) & BIT(INTA_ASSERTED))))
3243 return IRQ_NONE;
3244
3245 spin_lock(&dev->lock);
3246
3247 /* handle disconnect, dma, and more */
3248 handle_stat1_irqs(dev, readl(&dev->regs->irqstat1));
3249
3250 /* control requests and PIO */
3251 handle_stat0_irqs(dev, readl(&dev->regs->irqstat0));
3252
3253 if (dev->quirks & PLX_SUPERSPEED) {
3254 /* re-enable interrupt to trigger any possible new interrupt */
3255 u32 pciirqenb1 = readl(&dev->regs->pciirqenb1);
3256 writel(pciirqenb1 & 0x7FFFFFFF, &dev->regs->pciirqenb1);
3257 writel(pciirqenb1, &dev->regs->pciirqenb1);
3258 }
3259
3260 spin_unlock(&dev->lock);
3261
3262 return IRQ_HANDLED;
3263 }
3264
3265 /*-------------------------------------------------------------------------*/
3266
3267 static void gadget_release(struct device *_dev)
3268 {
3269 struct net2280 *dev = dev_get_drvdata(_dev);
3270
3271 kfree(dev);
3272 }
3273
3274 /* tear down the binding between this driver and the pci device */
3275
3276 static void net2280_remove(struct pci_dev *pdev)
3277 {
3278 struct net2280 *dev = pci_get_drvdata(pdev);
3279
3280 usb_del_gadget_udc(&dev->gadget);
3281
3282 BUG_ON(dev->driver);
3283
3284 /* then clean up the resources we allocated during probe() */
3285 net2280_led_shutdown(dev);
3286 if (dev->requests) {
3287 int i;
3288 for (i = 1; i < 5; i++) {
3289 if (!dev->ep[i].dummy)
3290 continue;
3291 pci_pool_free(dev->requests, dev->ep[i].dummy,
3292 dev->ep[i].td_dma);
3293 }
3294 pci_pool_destroy(dev->requests);
3295 }
3296 if (dev->got_irq)
3297 free_irq(pdev->irq, dev);
3298 if (dev->quirks & PLX_SUPERSPEED)
3299 pci_disable_msi(pdev);
3300 if (dev->regs)
3301 iounmap(dev->regs);
3302 if (dev->region)
3303 release_mem_region(pci_resource_start(pdev, 0),
3304 pci_resource_len(pdev, 0));
3305 if (dev->enabled)
3306 pci_disable_device(pdev);
3307 device_remove_file(&pdev->dev, &dev_attr_registers);
3308
3309 ep_info(dev, "unbind\n");
3310 }
3311
3312 /* wrap this driver around the specified device, but
3313 * don't respond over USB until a gadget driver binds to us.
3314 */
3315
3316 static int net2280_probe(struct pci_dev *pdev, const struct pci_device_id *id)
3317 {
3318 struct net2280 *dev;
3319 unsigned long resource, len;
3320 void __iomem *base = NULL;
3321 int retval, i;
3322
3323 /* alloc, and start init */
3324 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
3325 if (dev == NULL) {
3326 retval = -ENOMEM;
3327 goto done;
3328 }
3329
3330 pci_set_drvdata(pdev, dev);
3331 spin_lock_init(&dev->lock);
3332 dev->quirks = id->driver_data;
3333 dev->pdev = pdev;
3334 dev->gadget.ops = &net2280_ops;
3335 dev->gadget.max_speed = (dev->quirks & PLX_SUPERSPEED) ?
3336 USB_SPEED_SUPER : USB_SPEED_HIGH;
3337
3338 /* the "gadget" abstracts/virtualizes the controller */
3339 dev->gadget.name = driver_name;
3340
3341 /* now all the pci goodies ... */
3342 if (pci_enable_device(pdev) < 0) {
3343 retval = -ENODEV;
3344 goto done;
3345 }
3346 dev->enabled = 1;
3347
3348 /* BAR 0 holds all the registers
3349 * BAR 1 is 8051 memory; unused here (note erratum 0103)
3350 * BAR 2 is fifo memory; unused here
3351 */
3352 resource = pci_resource_start(pdev, 0);
3353 len = pci_resource_len(pdev, 0);
3354 if (!request_mem_region(resource, len, driver_name)) {
3355 ep_dbg(dev, "controller already in use\n");
3356 retval = -EBUSY;
3357 goto done;
3358 }
3359 dev->region = 1;
3360
3361 /* FIXME provide firmware download interface to put
3362 * 8051 code into the chip, e.g. to turn on PCI PM.
3363 */
3364
3365 base = ioremap_nocache(resource, len);
3366 if (base == NULL) {
3367 ep_dbg(dev, "can't map memory\n");
3368 retval = -EFAULT;
3369 goto done;
3370 }
3371 dev->regs = (struct net2280_regs __iomem *) base;
3372 dev->usb = (struct net2280_usb_regs __iomem *) (base + 0x0080);
3373 dev->pci = (struct net2280_pci_regs __iomem *) (base + 0x0100);
3374 dev->dma = (struct net2280_dma_regs __iomem *) (base + 0x0180);
3375 dev->dep = (struct net2280_dep_regs __iomem *) (base + 0x0200);
3376 dev->epregs = (struct net2280_ep_regs __iomem *) (base + 0x0300);
3377
3378 if (dev->quirks & PLX_SUPERSPEED) {
3379 u32 fsmvalue;
3380 u32 usbstat;
3381 dev->usb_ext = (struct usb338x_usb_ext_regs __iomem *)
3382 (base + 0x00b4);
3383 dev->fiforegs = (struct usb338x_fifo_regs __iomem *)
3384 (base + 0x0500);
3385 dev->llregs = (struct usb338x_ll_regs __iomem *)
3386 (base + 0x0700);
3387 dev->ll_lfps_regs = (struct usb338x_ll_lfps_regs __iomem *)
3388 (base + 0x0748);
3389 dev->ll_tsn_regs = (struct usb338x_ll_tsn_regs __iomem *)
3390 (base + 0x077c);
3391 dev->ll_chicken_reg = (struct usb338x_ll_chi_regs __iomem *)
3392 (base + 0x079c);
3393 dev->plregs = (struct usb338x_pl_regs __iomem *)
3394 (base + 0x0800);
3395 usbstat = readl(&dev->usb->usbstat);
3396 dev->enhanced_mode = !!(usbstat & BIT(11));
3397 dev->n_ep = (dev->enhanced_mode) ? 9 : 5;
3398 /* put into initial config, link up all endpoints */
3399 fsmvalue = get_idx_reg(dev->regs, SCRATCH) &
3400 (0xf << DEFECT7374_FSM_FIELD);
3401 /* See if firmware needs to set up for workaround: */
3402 if (fsmvalue == DEFECT7374_FSM_SS_CONTROL_READ) {
3403 dev->bug7734_patched = 1;
3404 writel(0, &dev->usb->usbctl);
3405 } else
3406 dev->bug7734_patched = 0;
3407 } else {
3408 dev->enhanced_mode = 0;
3409 dev->n_ep = 7;
3410 /* put into initial config, link up all endpoints */
3411 writel(0, &dev->usb->usbctl);
3412 }
3413
3414 usb_reset(dev);
3415 usb_reinit(dev);
3416
3417 /* irq setup after old hardware is cleaned up */
3418 if (!pdev->irq) {
3419 ep_err(dev, "No IRQ. Check PCI setup!\n");
3420 retval = -ENODEV;
3421 goto done;
3422 }
3423
3424 if (dev->quirks & PLX_SUPERSPEED)
3425 if (pci_enable_msi(pdev))
3426 ep_err(dev, "Failed to enable MSI mode\n");
3427
3428 if (request_irq(pdev->irq, net2280_irq, IRQF_SHARED,
3429 driver_name, dev)) {
3430 ep_err(dev, "request interrupt %d failed\n", pdev->irq);
3431 retval = -EBUSY;
3432 goto done;
3433 }
3434 dev->got_irq = 1;
3435
3436 /* DMA setup */
3437 /* NOTE: we know only the 32 LSBs of dma addresses may be nonzero */
3438 dev->requests = pci_pool_create("requests", pdev,
3439 sizeof(struct net2280_dma),
3440 0 /* no alignment requirements */,
3441 0 /* or page-crossing issues */);
3442 if (!dev->requests) {
3443 ep_dbg(dev, "can't get request pool\n");
3444 retval = -ENOMEM;
3445 goto done;
3446 }
3447 for (i = 1; i < 5; i++) {
3448 struct net2280_dma *td;
3449
3450 td = pci_pool_alloc(dev->requests, GFP_KERNEL,
3451 &dev->ep[i].td_dma);
3452 if (!td) {
3453 ep_dbg(dev, "can't get dummy %d\n", i);
3454 retval = -ENOMEM;
3455 goto done;
3456 }
3457 td->dmacount = 0; /* not VALID */
3458 td->dmadesc = td->dmaaddr;
3459 dev->ep[i].dummy = td;
3460 }
3461
3462 /* enable lower-overhead pci memory bursts during DMA */
3463 if (dev->quirks & PLX_LEGACY)
3464 writel(BIT(DMA_MEMORY_WRITE_AND_INVALIDATE_ENABLE) |
3465 /*
3466 * 256 write retries may not be enough...
3467 BIT(PCI_RETRY_ABORT_ENABLE) |
3468 */
3469 BIT(DMA_READ_MULTIPLE_ENABLE) |
3470 BIT(DMA_READ_LINE_ENABLE),
3471 &dev->pci->pcimstctl);
3472 /* erratum 0115 shouldn't appear: Linux inits PCI_LATENCY_TIMER */
3473 pci_set_master(pdev);
3474 pci_try_set_mwi(pdev);
3475
3476 /* ... also flushes any posted pci writes */
3477 dev->chiprev = get_idx_reg(dev->regs, REG_CHIPREV) & 0xffff;
3478
3479 /* done */
3480 ep_info(dev, "%s\n", driver_desc);
3481 ep_info(dev, "irq %d, pci mem %p, chip rev %04x\n",
3482 pdev->irq, base, dev->chiprev);
3483 ep_info(dev, "version: " DRIVER_VERSION "; %s\n",
3484 dev->enhanced_mode ? "enhanced mode" : "legacy mode");
3485 retval = device_create_file(&pdev->dev, &dev_attr_registers);
3486 if (retval)
3487 goto done;
3488
3489 retval = usb_add_gadget_udc_release(&pdev->dev, &dev->gadget,
3490 gadget_release);
3491 if (retval)
3492 goto done;
3493 return 0;
3494
3495 done:
3496 if (dev)
3497 net2280_remove(pdev);
3498 return retval;
3499 }
3500
3501 /* make sure the board is quiescent; otherwise it will continue
3502 * generating IRQs across the upcoming reboot.
3503 */
3504
3505 static void net2280_shutdown(struct pci_dev *pdev)
3506 {
3507 struct net2280 *dev = pci_get_drvdata(pdev);
3508
3509 /* disable IRQs */
3510 writel(0, &dev->regs->pciirqenb0);
3511 writel(0, &dev->regs->pciirqenb1);
3512
3513 /* disable the pullup so the host will think we're gone */
3514 writel(0, &dev->usb->usbctl);
3515
3516 }
3517
3518
3519 /*-------------------------------------------------------------------------*/
3520
3521 static const struct pci_device_id pci_ids[] = { {
3522 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
3523 .class_mask = ~0,
3524 .vendor = PCI_VENDOR_ID_PLX_LEGACY,
3525 .device = 0x2280,
3526 .subvendor = PCI_ANY_ID,
3527 .subdevice = PCI_ANY_ID,
3528 .driver_data = PLX_LEGACY | PLX_2280,
3529 }, {
3530 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
3531 .class_mask = ~0,
3532 .vendor = PCI_VENDOR_ID_PLX_LEGACY,
3533 .device = 0x2282,
3534 .subvendor = PCI_ANY_ID,
3535 .subdevice = PCI_ANY_ID,
3536 .driver_data = PLX_LEGACY,
3537 },
3538 {
3539 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
3540 .class_mask = ~0,
3541 .vendor = PCI_VENDOR_ID_PLX,
3542 .device = 0x3380,
3543 .subvendor = PCI_ANY_ID,
3544 .subdevice = PCI_ANY_ID,
3545 .driver_data = PLX_SUPERSPEED,
3546 },
3547 {
3548 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
3549 .class_mask = ~0,
3550 .vendor = PCI_VENDOR_ID_PLX,
3551 .device = 0x3382,
3552 .subvendor = PCI_ANY_ID,
3553 .subdevice = PCI_ANY_ID,
3554 .driver_data = PLX_SUPERSPEED,
3555 },
3556 { /* end: all zeroes */ }
3557 };
3558 MODULE_DEVICE_TABLE(pci, pci_ids);
3559
3560 /* pci driver glue; this is a "new style" PCI driver module */
3561 static struct pci_driver net2280_pci_driver = {
3562 .name = (char *) driver_name,
3563 .id_table = pci_ids,
3564
3565 .probe = net2280_probe,
3566 .remove = net2280_remove,
3567 .shutdown = net2280_shutdown,
3568
3569 /* FIXME add power management support */
3570 };
3571
3572 module_pci_driver(net2280_pci_driver);
3573
3574 MODULE_DESCRIPTION(DRIVER_DESC);
3575 MODULE_AUTHOR("David Brownell");
3576 MODULE_LICENSE("GPL");
This page took 0.11571 seconds and 6 git commands to generate.