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