1 /*
2 * Driver for the NetChip 2280 USB device controller.
3 * Specs and errata are available from <http://www.netchip.com>.
4 *
5 * NetChip Technology Inc. supported the development of this driver.
6 *
7 *
8 * CODE STATUS HIGHLIGHTS
9 *
10 * Used with a gadget driver like "zero.c" this enumerates fine to Windows
11 * or Linux hosts; handles disconnect, reconnect, and reset, for full or
12 * high speed operation; and passes USB-IF "chapter 9" tests.
13 *
14 * Handles standard stress loads from the Linux "usbtest" driver, with
15 * either DMA (default) or PIO (use_dma=n) used for ep-{a,b,c,d}. Testing
16 * with "ttcp" (and the "ether.c" driver) behaves nicely too.
17 *
18 * DMA is enabled by default. Drivers using transfer queues might use
19 * DMA chaining to remove IRQ latencies between transfers. (Except when
20 * short OUT transfers happen.) Drivers can use the req->no_interrupt
21 * hint to completely eliminate some IRQs, if a later IRQ is guaranteed
22 * and DMA chaining is enabled.
23 *
24 * Note that almost all the errata workarounds here are only needed for
25 * rev1 chips. Rev1a silicon (0110) fixes almost all of them.
26 */
27
28 /*
29 * Copyright (C) 2003 David Brownell
30 * Copyright (C) 2003 NetChip Technologies
31 *
32 * This program is free software; you can redistribute it and/or modify
33 * it under the terms of the GNU General Public License as published by
34 * the Free Software Foundation; either version 2 of the License, or
35 * (at your option) any later version.
36 *
37 * This program is distributed in the hope that it will be useful,
38 * but WITHOUT ANY WARRANTY; without even the implied warranty of
39 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
40 * GNU General Public License for more details.
41 *
42 * You should have received a copy of the GNU General Public License
43 * along with this program; if not, write to the Free Software
44 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
45 */
46
47 #undef DEBUG /* messages on error and most fault paths */
48 #undef VERBOSE /* extra debug messages (success too) */
49
50 #include <linux/config.h>
51 #include <linux/module.h>
52 #include <linux/pci.h>
53 #include <linux/kernel.h>
54 #include <linux/delay.h>
55 #include <linux/ioport.h>
56 #include <linux/sched.h>
57 #include <linux/slab.h>
58 #include <linux/smp_lock.h>
59 #include <linux/errno.h>
60 #include <linux/init.h>
61 #include <linux/timer.h>
62 #include <linux/list.h>
63 #include <linux/interrupt.h>
64
65 #include <linux/usb_ch9.h>
66 #include <linux/usb_gadget.h>
67
68 #include <asm/byteorder.h>
69 #include <asm/io.h>
70 #include <asm/irq.h>
71 #include <asm/system.h>
72 #include <asm/unaligned.h>
73
74
75 #define DRIVER_DESC "NetChip 2280 USB Peripheral Controller"
76 #define DRIVER_VERSION "2004 Jan 14"
77
78 #define DMA_ADDR_INVALID (~(dma_addr_t)0)
79 #define EP_DONTUSE 13 /* nonzero */
80
81 #define USE_RDK_LEDS /* GPIO pins control three LEDs */
82
83
84 static const char driver_name [] = "net2280";
85 static const char driver_desc [] = DRIVER_DESC;
86
87 static const char ep0name [] = "ep0";
88 static const char *ep_name [] = {
89 ep0name,
90 "ep-a", "ep-b", "ep-c", "ep-d",
91 "ep-e", "ep-f",
92 };
93
94 /* use_dma -- general goodness, fewer interrupts, less cpu load (vs PIO)
95 * use_dma_chaining -- dma descriptor queueing gives even more irq reduction
96 *
97 * The net2280 DMA engines are not tightly integrated with their FIFOs;
98 * not all cases are (yet) handled well in this driver or the silicon.
99 * Some gadget drivers work better with the dma support here than others.
100 * These two parameters let you use PIO or more aggressive DMA.
101 */
102 static int use_dma = 1;
103 static int use_dma_chaining = 0;
104
105 MODULE_PARM (use_dma, "i");
106 MODULE_PARM_DESC (use_dma, "true to use dma controllers");
107
108 MODULE_PARM (use_dma_chaining, "i");
109 MODULE_PARM_DESC (use_dma_chaining, "true to use dma descriptor queues");
110
111
112 /* mode 0 == ep-{a,b,c,d} 1K fifo each
113 * mode 1 == ep-{a,b} 2K fifo each, ep-{c,d} unavailable
114 * mode 2 == ep-a 2K fifo, ep-{b,c} 1K each, ep-d unavailable
115 */
116 static ushort fifo_mode = 0;
117
118 MODULE_PARM (fifo_mode, "h");
119 MODULE_PARM_DESC (fifo_mode, "net2280 fifo mode");
120
121
122 #define DIR_STRING(bAddress) (((bAddress) & USB_DIR_IN) ? "in" : "out")
123
124 #if defined(USE_SYSFS_DEBUG_FILES) || defined (DEBUG)
type_string(u8 bmAttributes)125 static char *type_string (u8 bmAttributes)
126 {
127 switch ((bmAttributes) & USB_ENDPOINT_XFERTYPE_MASK) {
128 case USB_ENDPOINT_XFER_BULK: return "bulk";
129 case USB_ENDPOINT_XFER_ISOC: return "iso";
130 case USB_ENDPOINT_XFER_INT: return "intr";
131 };
132 return "control";
133 }
134 #endif
135
136 #include "net2280.h"
137
138 #define valid_bit __constant_cpu_to_le32 (1 << VALID_BIT)
139 #define dma_done_ie __constant_cpu_to_le32 (1 << DMA_DONE_INTERRUPT_ENABLE)
140
141 /*-------------------------------------------------------------------------*/
142
143 static int
net2280_enable(struct usb_ep * _ep,const struct usb_endpoint_descriptor * desc)144 net2280_enable (struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
145 {
146 struct net2280 *dev;
147 struct net2280_ep *ep;
148 u32 max, tmp;
149 unsigned long flags;
150
151 ep = container_of (_ep, struct net2280_ep, ep);
152 if (!_ep || !desc || ep->desc || _ep->name == ep0name
153 || desc->bDescriptorType != USB_DT_ENDPOINT)
154 return -EINVAL;
155 dev = ep->dev;
156 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
157 return -ESHUTDOWN;
158
159 /* erratum 0119 workaround ties up an endpoint number */
160 if ((desc->bEndpointAddress & 0x0f) == EP_DONTUSE)
161 return -EDOM;
162
163 /* sanity check ep-e/ep-f since their fifos are small */
164 max = le16_to_cpu (desc->wMaxPacketSize) & 0x1fff;
165 if (ep->num > 4 && max > 64)
166 return -ERANGE;
167
168 spin_lock_irqsave (&dev->lock, flags);
169 _ep->maxpacket = max & 0x7ff;
170 ep->desc = desc;
171
172 /* ep_reset() has already been called */
173 ep->stopped = 0;
174 ep->out_overflow = 0;
175
176 /* set speed-dependent max packet; may kick in high bandwidth */
177 set_idx_reg (dev->regs, REG_EP_MAXPKT (dev, ep->num), max);
178
179 /* FIFO lines can't go to different packets. PIO is ok, so
180 * use it instead of troublesome (non-bulk) multi-packet DMA.
181 */
182 if (ep->dma && (max % 4) != 0 && use_dma_chaining) {
183 DEBUG (ep->dev, "%s, no dma for maxpacket %d\n",
184 ep->ep.name, ep->ep.maxpacket);
185 ep->dma = 0;
186 }
187
188 /* set type, direction, address; reset fifo counters */
189 writel ((1 << FIFO_FLUSH), &ep->regs->ep_stat);
190 tmp = (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK);
191 if (tmp == USB_ENDPOINT_XFER_INT) {
192 /* erratum 0105 workaround prevents hs NYET */
193 if (dev->chiprev == 0100
194 && dev->gadget.speed == USB_SPEED_HIGH
195 && !(desc->bEndpointAddress & USB_DIR_IN))
196 writel ((1 << CLEAR_NAK_OUT_PACKETS_MODE),
197 &ep->regs->ep_rsp);
198 } else if (tmp == USB_ENDPOINT_XFER_BULK) {
199 /* catch some particularly blatant driver bugs */
200 if ((dev->gadget.speed == USB_SPEED_HIGH
201 && max != 512)
202 || (dev->gadget.speed == USB_SPEED_FULL
203 && max > 64)) {
204 spin_unlock_irqrestore (&dev->lock, flags);
205 return -ERANGE;
206 }
207 }
208 ep->is_iso = (tmp == USB_ENDPOINT_XFER_ISOC) ? 1 : 0;
209 tmp <<= ENDPOINT_TYPE;
210 tmp |= desc->bEndpointAddress;
211 tmp |= (4 << ENDPOINT_BYTE_COUNT); /* default full fifo lines */
212 tmp |= 1 << ENDPOINT_ENABLE;
213 wmb ();
214
215 /* for OUT transfers, block the rx fifo until a read is posted */
216 ep->is_in = (tmp & USB_DIR_IN) != 0;
217 if (!ep->is_in)
218 writel ((1 << SET_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
219
220 writel (tmp, &ep->regs->ep_cfg);
221
222 /* enable irqs */
223 if (!ep->dma) { /* pio, per-packet */
224 tmp = (1 << ep->num) | readl (&dev->regs->pciirqenb0);
225 writel (tmp, &dev->regs->pciirqenb0);
226
227 tmp = (1 << DATA_PACKET_RECEIVED_INTERRUPT_ENABLE)
228 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT_ENABLE)
229 | readl (&ep->regs->ep_irqenb);
230 writel (tmp, &ep->regs->ep_irqenb);
231 } else { /* dma, per-request */
232 tmp = (1 << (8 + ep->num)); /* completion */
233 tmp |= readl (&dev->regs->pciirqenb1);
234 writel (tmp, &dev->regs->pciirqenb1);
235
236 /* for short OUT transfers, dma completions can't
237 * advance the queue; do it pio-style, by hand.
238 * NOTE erratum 0112 workaround #2
239 */
240 if ((desc->bEndpointAddress & USB_DIR_IN) == 0) {
241 tmp = (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT_ENABLE);
242 writel (tmp, &ep->regs->ep_irqenb);
243
244 tmp = (1 << ep->num) | readl (&dev->regs->pciirqenb0);
245 writel (tmp, &dev->regs->pciirqenb0);
246 }
247 }
248
249 tmp = desc->bEndpointAddress;
250 DEBUG (dev, "enabled %s (ep%d%s-%s) %s max %04x\n",
251 _ep->name, tmp & 0x0f, DIR_STRING (tmp),
252 type_string (desc->bmAttributes),
253 ep->dma ? "dma" : "pio", max);
254
255 /* pci writes may still be posted */
256 spin_unlock_irqrestore (&dev->lock, flags);
257 return 0;
258 }
259
handshake(u32 * ptr,u32 mask,u32 done,int usec)260 static int handshake (u32 *ptr, u32 mask, u32 done, int usec)
261 {
262 u32 result;
263
264 do {
265 result = readl (ptr);
266 if (result == ~(u32)0) /* "device unplugged" */
267 return -ENODEV;
268 result &= mask;
269 if (result == done)
270 return 0;
271 udelay (1);
272 usec--;
273 } while (usec > 0);
274 return -ETIMEDOUT;
275 }
276
277 static struct usb_ep_ops net2280_ep_ops;
278
ep_reset(struct net2280_regs * regs,struct net2280_ep * ep)279 static void ep_reset (struct net2280_regs *regs, struct net2280_ep *ep)
280 {
281 u32 tmp;
282
283 ep->desc = 0;
284 INIT_LIST_HEAD (&ep->queue);
285
286 ep->ep.maxpacket = ~0;
287 ep->ep.ops = &net2280_ep_ops;
288
289 /* disable the dma, irqs, endpoint... */
290 if (ep->dma) {
291 writel (0, &ep->dma->dmactl);
292 writel ( (1 << DMA_SCATTER_GATHER_DONE_INTERRUPT)
293 | (1 << DMA_TRANSACTION_DONE_INTERRUPT)
294 | (1 << DMA_ABORT)
295 , &ep->dma->dmastat);
296
297 tmp = readl (®s->pciirqenb0);
298 tmp &= ~(1 << ep->num);
299 writel (tmp, ®s->pciirqenb0);
300 } else {
301 tmp = readl (®s->pciirqenb1);
302 tmp &= ~(1 << (8 + ep->num)); /* completion */
303 writel (tmp, ®s->pciirqenb1);
304 }
305 writel (0, &ep->regs->ep_irqenb);
306
307 /* init to our chosen defaults, notably so that we NAK OUT
308 * packets until the driver queues a read (+note erratum 0112)
309 */
310 writel ( (1 << SET_NAK_OUT_PACKETS_MODE)
311 | (1 << SET_NAK_OUT_PACKETS)
312 | (1 << CLEAR_EP_HIDE_STATUS_PHASE)
313 | (1 << CLEAR_INTERRUPT_MODE)
314 | (1 << CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE)
315 | (1 << CLEAR_ENDPOINT_TOGGLE)
316 | (1 << CLEAR_ENDPOINT_HALT)
317 , &ep->regs->ep_rsp);
318
319 /* scrub most status bits, and flush any fifo state */
320 writel ( (1 << TIMEOUT)
321 | (1 << USB_STALL_SENT)
322 | (1 << USB_IN_NAK_SENT)
323 | (1 << USB_IN_ACK_RCVD)
324 | (1 << USB_OUT_PING_NAK_SENT)
325 | (1 << USB_OUT_ACK_SENT)
326 | (1 << FIFO_OVERFLOW)
327 | (1 << FIFO_UNDERFLOW)
328 | (1 << FIFO_FLUSH)
329 | (1 << SHORT_PACKET_OUT_DONE_INTERRUPT)
330 | (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)
331 | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
332 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
333 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
334 | (1 << DATA_IN_TOKEN_INTERRUPT)
335 , &ep->regs->ep_stat);
336
337 /* fifo size is handled separately */
338 }
339
340 static void nuke (struct net2280_ep *);
341
net2280_disable(struct usb_ep * _ep)342 static int net2280_disable (struct usb_ep *_ep)
343 {
344 struct net2280_ep *ep;
345 unsigned long flags;
346
347 ep = container_of (_ep, struct net2280_ep, ep);
348 if (!_ep || !ep->desc || _ep->name == ep0name)
349 return -EINVAL;
350
351 spin_lock_irqsave (&ep->dev->lock, flags);
352 nuke (ep);
353 ep_reset (ep->dev->regs, ep);
354
355 VDEBUG (ep->dev, "disabled %s %s\n",
356 ep->dma ? "dma" : "pio", _ep->name);
357
358 /* synch memory views with the device */
359 (void) readl (&ep->regs->ep_cfg);
360
361 if (use_dma && !ep->dma && ep->num >= 1 && ep->num <= 4)
362 ep->dma = &ep->dev->dma [ep->num - 1];
363
364 spin_unlock_irqrestore (&ep->dev->lock, flags);
365 return 0;
366 }
367
368 /*-------------------------------------------------------------------------*/
369
370 static struct usb_request *
net2280_alloc_request(struct usb_ep * _ep,int gfp_flags)371 net2280_alloc_request (struct usb_ep *_ep, int gfp_flags)
372 {
373 struct net2280_ep *ep;
374 struct net2280_request *req;
375
376 if (!_ep)
377 return 0;
378 ep = container_of (_ep, struct net2280_ep, ep);
379
380 req = kmalloc (sizeof *req, gfp_flags);
381 if (!req)
382 return 0;
383
384 memset (req, 0, sizeof *req);
385 req->req.dma = DMA_ADDR_INVALID;
386 INIT_LIST_HEAD (&req->queue);
387
388 /* this dma descriptor may be swapped with the previous dummy */
389 if (ep->dma) {
390 struct net2280_dma *td;
391
392 td = pci_pool_alloc (ep->dev->requests, gfp_flags,
393 &req->td_dma);
394 if (!td) {
395 kfree (req);
396 return 0;
397 }
398 td->dmacount = 0; /* not VALID */
399 td->dmaaddr = __constant_cpu_to_le32 (DMA_ADDR_INVALID);
400 td->dmadesc = td->dmaaddr;
401 req->td = td;
402 }
403 return &req->req;
404 }
405
406 static void
net2280_free_request(struct usb_ep * _ep,struct usb_request * _req)407 net2280_free_request (struct usb_ep *_ep, struct usb_request *_req)
408 {
409 struct net2280_ep *ep;
410 struct net2280_request *req;
411
412 ep = container_of (_ep, struct net2280_ep, ep);
413 if (!_ep || !_req)
414 return;
415
416 req = container_of (_req, struct net2280_request, req);
417 WARN_ON (!list_empty (&req->queue));
418 if (req->td)
419 pci_pool_free (ep->dev->requests, req->td, req->td_dma);
420 kfree (req);
421 }
422
423 /*-------------------------------------------------------------------------*/
424
425 #undef USE_KMALLOC
426
427 /* many common platforms have dma-coherent caches, which means that it's
428 * safe to use kmalloc() memory for all i/o buffers without using any
429 * cache flushing calls. (unless you're trying to share cache lines
430 * between dma and non-dma activities, which is a slow idea in any case.)
431 *
432 * other platforms need more care, with 2.5 having a moderately general
433 * solution (which falls down for allocations smaller than one page)
434 * that improves significantly on the 2.4 PCI allocators by removing
435 * the restriction that memory never be freed in_interrupt().
436 */
437 #if defined(CONFIG_X86)
438 #define USE_KMALLOC
439
440 #elif defined(CONFIG_PPC) && !defined(CONFIG_NOT_COHERENT_CACHE)
441 #define USE_KMALLOC
442
443 #elif defined(CONFIG_MIPS) && !defined(CONFIG_NONCOHERENT_IO)
444 #define USE_KMALLOC
445
446 /* FIXME there are other cases, including an x86-64 one ... */
447 #endif
448
449 /* allocating buffers this way eliminates dma mapping overhead, which
450 * on some platforms will mean eliminating a per-io buffer copy. with
451 * some kinds of system caches, further tweaks may still be needed.
452 */
453 static void *
net2280_alloc_buffer(struct usb_ep * _ep,unsigned bytes,dma_addr_t * dma,int gfp_flags)454 net2280_alloc_buffer (
455 struct usb_ep *_ep,
456 unsigned bytes,
457 dma_addr_t *dma,
458 int gfp_flags
459 )
460 {
461 void *retval;
462 struct net2280_ep *ep;
463
464 ep = container_of (_ep, struct net2280_ep, ep);
465 if (!_ep)
466 return 0;
467 *dma = DMA_ADDR_INVALID;
468
469 #if defined(USE_KMALLOC)
470 retval = kmalloc(bytes, gfp_flags);
471 if (retval)
472 *dma = virt_to_phys(retval);
473 #else
474 if (ep->dma) {
475 /* one problem with this call is that it wastes memory on
476 * typical 1/N page allocations: it allocates 1..N pages.
477 * another is that it always uses GFP_ATOMIC.
478 */
479 #warning Using pci_alloc_consistent even with buffers smaller than a page.
480 retval = pci_alloc_consistent(ep->dev->pdev, bytes, dma);
481 } else
482 retval = kmalloc(bytes, gfp_flags);
483 #endif
484 return retval;
485 }
486
487 static void
net2280_free_buffer(struct usb_ep * _ep,void * buf,dma_addr_t dma,unsigned bytes)488 net2280_free_buffer (
489 struct usb_ep *_ep,
490 void *buf,
491 dma_addr_t dma,
492 unsigned bytes
493 ) {
494 /* free memory into the right allocator */
495 #ifndef USE_KMALLOC
496 if (dma != DMA_ADDR_INVALID) {
497 struct net2280_ep *ep;
498
499 ep = container_of(_ep, struct net2280_ep, ep);
500 if (!_ep)
501 return;
502 /* one problem with this call is that some platforms
503 * don't allow it to be used in_irq().
504 */
505 pci_free_consistent(ep->dev->pdev, bytes, buf, dma);
506 } else
507 #endif
508 kfree (buf);
509 }
510
511 /*-------------------------------------------------------------------------*/
512
513 /* load a packet into the fifo we use for usb IN transfers.
514 * works for all endpoints.
515 *
516 * NOTE: pio with ep-a..ep-d could stuff multiple packets into the fifo
517 * at a time, but this code is simpler because it knows it only writes
518 * one packet. ep-a..ep-d should use dma instead.
519 */
520 static void
write_fifo(struct net2280_ep * ep,struct usb_request * req)521 write_fifo (struct net2280_ep *ep, struct usb_request *req)
522 {
523 struct net2280_ep_regs *regs = ep->regs;
524 u8 *buf;
525 u32 tmp;
526 unsigned count, total;
527
528 /* INVARIANT: fifo is currently empty. (testable) */
529
530 if (req) {
531 buf = req->buf + req->actual;
532 prefetch (buf);
533 total = req->length - req->actual;
534 } else {
535 total = 0;
536 buf = 0;
537 }
538
539 /* write just one packet at a time */
540 count = ep->ep.maxpacket;
541 if (count > total) /* min() cannot be used on a bitfield */
542 count = total;
543
544 VDEBUG (ep->dev, "write %s fifo (IN) %d bytes%s req %p\n",
545 ep->ep.name, count,
546 (count != ep->ep.maxpacket) ? " (short)" : "",
547 req);
548 while (count >= 4) {
549 /* NOTE be careful if you try to align these. fifo lines
550 * should normally be full (4 bytes) and successive partial
551 * lines are ok only in certain cases.
552 */
553 tmp = get_unaligned ((u32 *)buf);
554 cpu_to_le32s (&tmp);
555 writel (tmp, ®s->ep_data);
556 buf += 4;
557 count -= 4;
558 }
559
560 /* last fifo entry is "short" unless we wrote a full packet.
561 * also explicitly validate last word in (periodic) transfers
562 * when maxpacket is not a multiple of 4 bytes.
563 */
564 if (count || total < ep->ep.maxpacket) {
565 tmp = count ? get_unaligned ((u32 *)buf) : count;
566 cpu_to_le32s (&tmp);
567 set_fifo_bytecount (ep, count & 0x03);
568 writel (tmp, ®s->ep_data);
569 }
570
571 /* pci writes may still be posted */
572 }
573
574 /* work around erratum 0106: PCI and USB race over the OUT fifo.
575 * caller guarantees chiprev 0100, out endpoint is NAKing, and
576 * there's no real data in the fifo.
577 *
578 * NOTE: also used in cases where that erratum doesn't apply:
579 * where the host wrote "too much" data to us.
580 */
out_flush(struct net2280_ep * ep)581 static void out_flush (struct net2280_ep *ep)
582 {
583 u32 *statp, tmp;
584
585 ASSERT_OUT_NAKING (ep);
586
587 statp = &ep->regs->ep_stat;
588 writel ( (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
589 | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
590 , statp);
591 writel ((1 << FIFO_FLUSH), statp);
592 mb ();
593 tmp = readl (statp);
594 if (tmp & (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
595 /* high speed did bulk NYET; fifo isn't filling */
596 && ep->dev->gadget.speed == USB_SPEED_FULL) {
597 unsigned usec;
598
599 usec = 50; /* 64 byte bulk/interrupt */
600 handshake (statp, (1 << USB_OUT_PING_NAK_SENT),
601 (1 << USB_OUT_PING_NAK_SENT), usec);
602 /* NAK done; now CLEAR_NAK_OUT_PACKETS is safe */
603 }
604 }
605
606 /* unload packet(s) from the fifo we use for usb OUT transfers.
607 * returns true iff the request completed, because of short packet
608 * or the request buffer having filled with full packets.
609 *
610 * for ep-a..ep-d this will read multiple packets out when they
611 * have been accepted.
612 */
613 static int
read_fifo(struct net2280_ep * ep,struct net2280_request * req)614 read_fifo (struct net2280_ep *ep, struct net2280_request *req)
615 {
616 struct net2280_ep_regs *regs = ep->regs;
617 u8 *buf = req->req.buf + req->req.actual;
618 unsigned count, tmp, is_short;
619 unsigned cleanup = 0, prevent = 0;
620
621 /* erratum 0106 ... packets coming in during fifo reads might
622 * be incompletely rejected. not all cases have workarounds.
623 */
624 if (ep->dev->chiprev == 0x0100
625 && ep->dev->gadget.speed == USB_SPEED_FULL) {
626 udelay (1);
627 tmp = readl (&ep->regs->ep_stat);
628 if ((tmp & (1 << NAK_OUT_PACKETS)))
629 cleanup = 1;
630 else if ((tmp & (1 << FIFO_FULL))) {
631 start_out_naking (ep);
632 prevent = 1;
633 }
634 /* else: hope we don't see the problem */
635 }
636
637 /* never overflow the rx buffer. the fifo reads packets until
638 * it sees a short one; we might not be ready for them all.
639 */
640 prefetchw (buf);
641 count = readl (®s->ep_avail);
642 if (unlikely (count == 0)) {
643 udelay (1);
644 tmp = readl (&ep->regs->ep_stat);
645 count = readl (®s->ep_avail);
646 /* handled that data already? */
647 if (count == 0 && (tmp & (1 << NAK_OUT_PACKETS)) == 0)
648 return 0;
649 }
650
651 tmp = req->req.length - req->req.actual;
652 if (count > tmp) {
653 /* as with DMA, data overflow gets flushed */
654 if ((tmp % ep->ep.maxpacket) != 0) {
655 ERROR (ep->dev,
656 "%s out fifo %d bytes, expected %d\n",
657 ep->ep.name, count, tmp);
658 req->req.status = -EOVERFLOW;
659 cleanup = 1;
660 /* NAK_OUT_PACKETS will be set, so flushing is safe;
661 * the next read will start with the next packet
662 */
663 } /* else it's a ZLP, no worries */
664 count = tmp;
665 }
666 req->req.actual += count;
667
668 is_short = (count == 0) || ((count % ep->ep.maxpacket) != 0);
669
670 VDEBUG (ep->dev, "read %s fifo (OUT) %d bytes%s%s%s req %p %d/%d\n",
671 ep->ep.name, count, is_short ? " (short)" : "",
672 cleanup ? " flush" : "", prevent ? " nak" : "",
673 req, req->req.actual, req->req.length);
674
675 while (count >= 4) {
676 tmp = readl (®s->ep_data);
677 cpu_to_le32s (&tmp);
678 put_unaligned (tmp, (u32 *)buf);
679 buf += 4;
680 count -= 4;
681 }
682 if (count) {
683 tmp = readl (®s->ep_data);
684 cpu_to_le32s (&tmp);
685 do {
686 *buf++ = (u8) tmp;
687 tmp >>= 8;
688 } while (--count);
689 }
690 if (cleanup)
691 out_flush (ep);
692 if (prevent) {
693 writel ((1 << CLEAR_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
694 (void) readl (&ep->regs->ep_rsp);
695 }
696
697 return is_short || ((req->req.actual == req->req.length)
698 && !req->req.zero);
699 }
700
701 /* fill out dma descriptor to match a given request */
702 static void
fill_dma_desc(struct net2280_ep * ep,struct net2280_request * req,int valid)703 fill_dma_desc (struct net2280_ep *ep, struct net2280_request *req, int valid)
704 {
705 struct net2280_dma *td = req->td;
706 u32 dmacount = req->req.length;
707
708 /* don't let DMA continue after a short OUT packet,
709 * so overruns can't affect the next transfer.
710 * in case of overruns on max-size packets, we can't
711 * stop the fifo from filling but we can flush it.
712 */
713 if (ep->is_in)
714 dmacount |= (1 << DMA_DIRECTION);
715 else if ((dmacount % ep->ep.maxpacket) != 0)
716 dmacount |= (1 << END_OF_CHAIN);
717
718 req->valid = valid;
719 if (valid)
720 dmacount |= (1 << VALID_BIT);
721 if (likely(!req->req.no_interrupt || !use_dma_chaining))
722 dmacount |= (1 << DMA_DONE_INTERRUPT_ENABLE);
723
724 /* td->dmadesc = previously set by caller */
725 td->dmaaddr = cpu_to_le32p (&req->req.dma);
726
727 /* 2280 may be polling VALID_BIT through ep->dma->dmadesc */
728 wmb ();
729 td->dmacount = cpu_to_le32p (&dmacount);
730 }
731
732 static const u32 dmactl_default =
733 (1 << DMA_SCATTER_GATHER_DONE_INTERRUPT)
734 | (1 << DMA_CLEAR_COUNT_ENABLE)
735 /* erratum 0116 workaround part 1 (use POLLING) */
736 | (POLL_100_USEC << DESCRIPTOR_POLLING_RATE)
737 | (1 << DMA_VALID_BIT_POLLING_ENABLE)
738 | (1 << DMA_VALID_BIT_ENABLE)
739 | (1 << DMA_SCATTER_GATHER_ENABLE)
740 /* erratum 0116 workaround part 2 (no AUTOSTART) */
741 | (1 << DMA_ENABLE);
742
spin_stop_dma(struct net2280_dma_regs * dma)743 static inline void spin_stop_dma (struct net2280_dma_regs *dma)
744 {
745 handshake (&dma->dmactl, (1 << DMA_ENABLE), 0, 50);
746 }
747
stop_dma(struct net2280_dma_regs * dma)748 static inline void stop_dma (struct net2280_dma_regs *dma)
749 {
750 writel (readl (&dma->dmactl) & ~(1 << DMA_ENABLE), &dma->dmactl);
751 spin_stop_dma (dma);
752 }
753
start_queue(struct net2280_ep * ep,u32 dmactl,u32 td_dma)754 static void start_queue (struct net2280_ep *ep, u32 dmactl, u32 td_dma)
755 {
756 struct net2280_dma_regs *dma = ep->dma;
757
758 writel ((1 << VALID_BIT) | (ep->is_in << DMA_DIRECTION),
759 &dma->dmacount);
760 writel (readl (&dma->dmastat), &dma->dmastat);
761
762 writel (td_dma, &dma->dmadesc);
763 writel (dmactl, &dma->dmactl);
764
765 /* erratum 0116 workaround part 3: pci arbiter away from net2280 */
766 (void) readl (&ep->dev->pci->pcimstctl);
767
768 writel ((1 << DMA_START), &dma->dmastat);
769
770 if (!ep->is_in)
771 stop_out_naking (ep);
772 }
773
start_dma(struct net2280_ep * ep,struct net2280_request * req)774 static void start_dma (struct net2280_ep *ep, struct net2280_request *req)
775 {
776 u32 tmp;
777 struct net2280_dma_regs *dma = ep->dma;
778
779 /* FIXME can't use DMA for ZLPs */
780
781 /* on this path we "know" there's no dma active (yet) */
782 WARN_ON (readl (&dma->dmactl) & (1 << DMA_ENABLE));
783 writel (0, &ep->dma->dmactl);
784
785 /* previous OUT packet might have been short */
786 if (!ep->is_in && ((tmp = readl (&ep->regs->ep_stat))
787 & (1 << NAK_OUT_PACKETS)) != 0) {
788 writel ((1 << SHORT_PACKET_TRANSFERRED_INTERRUPT),
789 &ep->regs->ep_stat);
790
791 tmp = readl (&ep->regs->ep_avail);
792 if (tmp) {
793 writel (readl (&dma->dmastat), &dma->dmastat);
794
795 /* transfer all/some fifo data */
796 writel (req->req.dma, &dma->dmaaddr);
797 tmp = min (tmp, req->req.length);
798
799 /* dma irq, faking scatterlist status */
800 req->td->dmacount = cpu_to_le32 (req->req.length - tmp);
801 writel ((1 << DMA_DONE_INTERRUPT_ENABLE)
802 | tmp, &dma->dmacount);
803 req->td->dmadesc = 0;
804 req->valid = 1;
805
806 writel ((1 << DMA_ENABLE), &dma->dmactl);
807 writel ((1 << DMA_START), &dma->dmastat);
808 return;
809 }
810 }
811
812 tmp = dmactl_default;
813
814 /* force packet boundaries between dma requests, but prevent the
815 * controller from automagically writing a last "short" packet
816 * (zero length) unless the driver explicitly said to do that.
817 */
818 if (ep->is_in) {
819 if (likely ((req->req.length % ep->ep.maxpacket) != 0
820 || req->req.zero)) {
821 tmp |= (1 << DMA_FIFO_VALIDATE);
822 ep->in_fifo_validate = 1;
823 } else
824 ep->in_fifo_validate = 0;
825 }
826
827 /* init req->td, pointing to the current dummy */
828 req->td->dmadesc = cpu_to_le32 (ep->td_dma);
829 fill_dma_desc (ep, req, 1);
830
831 if (!use_dma_chaining)
832 req->td->dmacount |= __constant_cpu_to_le32 (1 << END_OF_CHAIN);
833
834 start_queue (ep, tmp, req->td_dma);
835 }
836
837 static inline void
queue_dma(struct net2280_ep * ep,struct net2280_request * req,int valid)838 queue_dma (struct net2280_ep *ep, struct net2280_request *req, int valid)
839 {
840 struct net2280_dma *end;
841 dma_addr_t tmp;
842
843 /* swap new dummy for old, link; fill and maybe activate */
844 end = ep->dummy;
845 ep->dummy = req->td;
846 req->td = end;
847
848 tmp = ep->td_dma;
849 ep->td_dma = req->td_dma;
850 req->td_dma = tmp;
851
852 end->dmadesc = cpu_to_le32 (ep->td_dma);
853
854 fill_dma_desc (ep, req, valid);
855 }
856
857 static void
done(struct net2280_ep * ep,struct net2280_request * req,int status)858 done (struct net2280_ep *ep, struct net2280_request *req, int status)
859 {
860 struct net2280 *dev;
861 unsigned stopped = ep->stopped;
862
863 list_del_init (&req->queue);
864
865 if (req->req.status == -EINPROGRESS)
866 req->req.status = status;
867 else
868 status = req->req.status;
869
870 dev = ep->dev;
871 if (req->mapped) {
872 pci_unmap_single (dev->pdev, req->req.dma, req->req.length,
873 ep->is_in ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
874 req->req.dma = DMA_ADDR_INVALID;
875 req->mapped = 0;
876 }
877
878 if (status && status != -ESHUTDOWN)
879 VDEBUG (dev, "complete %s req %p stat %d len %u/%u\n",
880 ep->ep.name, &req->req, status,
881 req->req.actual, req->req.length);
882
883 /* don't modify queue heads during completion callback */
884 ep->stopped = 1;
885 spin_unlock (&dev->lock);
886 req->req.complete (&ep->ep, &req->req);
887 spin_lock (&dev->lock);
888 ep->stopped = stopped;
889 }
890
891 /*-------------------------------------------------------------------------*/
892
893 static int
net2280_queue(struct usb_ep * _ep,struct usb_request * _req,int gfp_flags)894 net2280_queue (struct usb_ep *_ep, struct usb_request *_req, int gfp_flags)
895 {
896 struct net2280_request *req;
897 struct net2280_ep *ep;
898 struct net2280 *dev;
899 unsigned long flags;
900
901 /* we always require a cpu-view buffer, so that we can
902 * always use pio (as fallback or whatever).
903 */
904 req = container_of (_req, struct net2280_request, req);
905 if (!_req || !_req->complete || !_req->buf
906 || !list_empty (&req->queue))
907 return -EINVAL;
908 if (_req->length > (~0 & DMA_BYTE_COUNT_MASK))
909 return -EDOM;
910 ep = container_of (_ep, struct net2280_ep, ep);
911 if (!_ep || (!ep->desc && ep->num != 0))
912 return -EINVAL;
913 dev = ep->dev;
914 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
915 return -ESHUTDOWN;
916
917 /* FIXME implement PIO fallback for ZLPs with DMA */
918 if (ep->dma && _req->length == 0)
919 return -EOPNOTSUPP;
920
921 /* set up dma mapping in case the caller didn't */
922 if (ep->dma && _req->dma == DMA_ADDR_INVALID) {
923 _req->dma = pci_map_single (dev->pdev, _req->buf, _req->length,
924 ep->is_in ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
925 req->mapped = 1;
926 }
927
928 #if 0
929 VDEBUG (dev, "%s queue req %p, len %d buf %p\n",
930 _ep->name, _req, _req->length, _req->buf);
931 #endif
932
933 spin_lock_irqsave (&dev->lock, flags);
934
935 _req->status = -EINPROGRESS;
936 _req->actual = 0;
937
938 /* kickstart this i/o queue? */
939 if (list_empty (&ep->queue) && !ep->stopped) {
940 /* use DMA if the endpoint supports it, else pio */
941 if (ep->dma)
942 start_dma (ep, req);
943 else {
944 /* maybe there's no control data, just status ack */
945 if (ep->num == 0 && _req->length == 0) {
946 allow_status (ep);
947 done (ep, req, 0);
948 VDEBUG (dev, "%s status ack\n", ep->ep.name);
949 goto done;
950 }
951
952 /* PIO ... stuff the fifo, or unblock it. */
953 if (ep->is_in)
954 write_fifo (ep, _req);
955 else if (list_empty (&ep->queue)) {
956 u32 s;
957
958 /* OUT FIFO might have packet(s) buffered */
959 s = readl (&ep->regs->ep_stat);
960 if ((s & (1 << FIFO_EMPTY)) == 0) {
961 /* note: _req->short_not_ok is
962 * ignored here since PIO _always_
963 * stops queue advance here, and
964 * _req->status doesn't change for
965 * short reads (only _req->actual)
966 */
967 if (read_fifo (ep, req)) {
968 done (ep, req, 0);
969 if (ep->num == 0)
970 allow_status (ep);
971 /* don't queue it */
972 req = 0;
973 } else
974 s = readl (&ep->regs->ep_stat);
975 }
976
977 /* don't NAK, let the fifo fill */
978 if (req && (s & (1 << NAK_OUT_PACKETS)))
979 writel ((1 << CLEAR_NAK_OUT_PACKETS),
980 &ep->regs->ep_rsp);
981 }
982 }
983
984 } else if (ep->dma) {
985 int valid = 1;
986
987 if (ep->is_in) {
988 int expect;
989
990 /* preventing magic zlps is per-engine state, not
991 * per-transfer; irq logic must recover hiccups.
992 */
993 expect = likely (req->req.zero
994 || (req->req.length % ep->ep.maxpacket) != 0);
995 if (expect != ep->in_fifo_validate)
996 valid = 0;
997 }
998 queue_dma (ep, req, valid);
999
1000 } /* else the irq handler advances the queue. */
1001
1002 if (req)
1003 list_add_tail (&req->queue, &ep->queue);
1004 done:
1005 spin_unlock_irqrestore (&dev->lock, flags);
1006
1007 /* pci writes may still be posted */
1008 return 0;
1009 }
1010
1011 static inline void
dma_done(struct net2280_ep * ep,struct net2280_request * req,u32 dmacount,int status)1012 dma_done (
1013 struct net2280_ep *ep,
1014 struct net2280_request *req,
1015 u32 dmacount,
1016 int status
1017 )
1018 {
1019 req->req.actual = req->req.length - (DMA_BYTE_COUNT_MASK & dmacount);
1020 done (ep, req, status);
1021 }
1022
1023 static void restart_dma (struct net2280_ep *ep);
1024
scan_dma_completions(struct net2280_ep * ep)1025 static void scan_dma_completions (struct net2280_ep *ep)
1026 {
1027 /* only look at descriptors that were "naturally" retired,
1028 * so fifo and list head state won't matter
1029 */
1030 while (!list_empty (&ep->queue)) {
1031 struct net2280_request *req;
1032 u32 tmp;
1033
1034 req = list_entry (ep->queue.next,
1035 struct net2280_request, queue);
1036 if (!req->valid)
1037 break;
1038 rmb ();
1039 tmp = le32_to_cpup (&req->td->dmacount);
1040 if ((tmp & (1 << VALID_BIT)) != 0)
1041 break;
1042
1043 /* SHORT_PACKET_TRANSFERRED_INTERRUPT handles "usb-short"
1044 * cases where DMA must be aborted; this code handles
1045 * all non-abort DMA completions.
1046 */
1047 if (unlikely (req->td->dmadesc == 0)) {
1048 /* paranoia */
1049 tmp = readl (&ep->dma->dmacount);
1050 if (tmp & DMA_BYTE_COUNT_MASK)
1051 break;
1052 /* single transfer mode */
1053 dma_done (ep, req, tmp, 0);
1054 break;
1055 } else if (!ep->is_in
1056 && (req->req.length % ep->ep.maxpacket) != 0) {
1057 tmp = readl (&ep->regs->ep_stat);
1058
1059 /* AVOID TROUBLE HERE by not issuing short reads from
1060 * your gadget driver. That helps avoids errata 0121,
1061 * 0122, and 0124; not all cases trigger the warning.
1062 */
1063 if ((tmp & (1 << NAK_OUT_PACKETS)) == 0) {
1064 WARN (ep->dev, "%s lost packet sync!\n",
1065 ep->ep.name);
1066 req->req.status = -EOVERFLOW;
1067 } else if ((tmp = readl (&ep->regs->ep_avail)) != 0) {
1068 /* fifo gets flushed later */
1069 ep->out_overflow = 1;
1070 DEBUG (ep->dev, "%s dma, discard %d len %d\n",
1071 ep->ep.name, tmp,
1072 req->req.length);
1073 req->req.status = -EOVERFLOW;
1074 }
1075 }
1076 dma_done (ep, req, tmp, 0);
1077 }
1078 }
1079
restart_dma(struct net2280_ep * ep)1080 static void restart_dma (struct net2280_ep *ep)
1081 {
1082 struct net2280_request *req;
1083 u32 dmactl = dmactl_default;
1084
1085 if (ep->stopped)
1086 return;
1087 req = list_entry (ep->queue.next, struct net2280_request, queue);
1088
1089 if (!use_dma_chaining) {
1090 start_dma (ep, req);
1091 return;
1092 }
1093
1094 /* the 2280 will be processing the queue unless queue hiccups after
1095 * the previous transfer:
1096 * IN: wanted automagic zlp, head doesn't (or vice versa)
1097 * DMA_FIFO_VALIDATE doesn't init from dma descriptors.
1098 * OUT: was "usb-short", we must restart.
1099 */
1100 if (ep->is_in && !req->valid) {
1101 struct net2280_request *entry, *prev = 0;
1102 int reqmode, done = 0;
1103
1104 DEBUG (ep->dev, "%s dma hiccup td %p\n", ep->ep.name, req->td);
1105 ep->in_fifo_validate = likely (req->req.zero
1106 || (req->req.length % ep->ep.maxpacket) != 0);
1107 if (ep->in_fifo_validate)
1108 dmactl |= (1 << DMA_FIFO_VALIDATE);
1109 list_for_each_entry (entry, &ep->queue, queue) {
1110 u32 dmacount;
1111
1112 if (entry == req)
1113 continue;
1114 dmacount = entry->td->dmacount;
1115 if (!done) {
1116 reqmode = likely (entry->req.zero
1117 || (entry->req.length
1118 % ep->ep.maxpacket) != 0);
1119 if (reqmode == ep->in_fifo_validate) {
1120 entry->valid = 1;
1121 dmacount |= valid_bit;
1122 entry->td->dmacount = dmacount;
1123 prev = entry;
1124 continue;
1125 } else {
1126 /* force a hiccup */
1127 prev->td->dmacount |= dma_done_ie;
1128 done = 1;
1129 }
1130 }
1131
1132 /* walk the rest of the queue so unlinks behave */
1133 entry->valid = 0;
1134 dmacount &= ~valid_bit;
1135 entry->td->dmacount = dmacount;
1136 prev = entry;
1137 }
1138 }
1139
1140 writel (0, &ep->dma->dmactl);
1141 start_queue (ep, dmactl, req->td_dma);
1142 }
1143
abort_dma(struct net2280_ep * ep)1144 static void abort_dma (struct net2280_ep *ep)
1145 {
1146 /* abort the current transfer */
1147 if (likely (!list_empty (&ep->queue))) {
1148 /* FIXME work around errata 0121, 0122, 0124 */
1149 writel ((1 << DMA_ABORT), &ep->dma->dmastat);
1150 spin_stop_dma (ep->dma);
1151 } else
1152 stop_dma (ep->dma);
1153 scan_dma_completions (ep);
1154 }
1155
1156 /* dequeue ALL requests */
nuke(struct net2280_ep * ep)1157 static void nuke (struct net2280_ep *ep)
1158 {
1159 struct net2280_request *req;
1160
1161 /* called with spinlock held */
1162 ep->stopped = 1;
1163 if (ep->dma)
1164 abort_dma (ep);
1165 while (!list_empty (&ep->queue)) {
1166 req = list_entry (ep->queue.next,
1167 struct net2280_request,
1168 queue);
1169 done (ep, req, -ESHUTDOWN);
1170 }
1171 }
1172
1173 /* dequeue JUST ONE request */
net2280_dequeue(struct usb_ep * _ep,struct usb_request * _req)1174 static int net2280_dequeue (struct usb_ep *_ep, struct usb_request *_req)
1175 {
1176 struct net2280_ep *ep;
1177 struct net2280_request *req;
1178 unsigned long flags;
1179 u32 dmactl;
1180 int stopped;
1181
1182 ep = container_of (_ep, struct net2280_ep, ep);
1183 if (!_ep || (!ep->desc && ep->num != 0) || !_req)
1184 return -EINVAL;
1185
1186 spin_lock_irqsave (&ep->dev->lock, flags);
1187 stopped = ep->stopped;
1188
1189 /* quiesce dma while we patch the queue */
1190 dmactl = 0;
1191 ep->stopped = 1;
1192 if (ep->dma) {
1193 dmactl = readl (&ep->dma->dmactl);
1194 /* WARNING erratum 0127 may kick in ... */
1195 stop_dma (ep->dma);
1196 scan_dma_completions (ep);
1197 }
1198
1199 /* make sure it's still queued on this endpoint */
1200 list_for_each_entry (req, &ep->queue, queue) {
1201 if (&req->req == _req)
1202 break;
1203 }
1204 if (&req->req != _req) {
1205 spin_unlock_irqrestore (&ep->dev->lock, flags);
1206 return -EINVAL;
1207 }
1208
1209 /* queue head may be partially complete. */
1210 if (ep->queue.next == &req->queue) {
1211 if (ep->dma) {
1212 DEBUG (ep->dev, "unlink (%s) dma\n", _ep->name);
1213 _req->status = -ECONNRESET;
1214 abort_dma (ep);
1215 if (likely (ep->queue.next == &req->queue)) {
1216 // NOTE: misreports single-transfer mode
1217 req->td->dmacount = 0; /* invalidate */
1218 dma_done (ep, req,
1219 readl (&ep->dma->dmacount),
1220 -ECONNRESET);
1221 }
1222 } else {
1223 DEBUG (ep->dev, "unlink (%s) pio\n", _ep->name);
1224 done (ep, req, -ECONNRESET);
1225 }
1226 req = 0;
1227
1228 /* patch up hardware chaining data */
1229 } else if (ep->dma && use_dma_chaining) {
1230 if (req->queue.prev == ep->queue.next) {
1231 writel (le32_to_cpu (req->td->dmadesc),
1232 &ep->dma->dmadesc);
1233 if (req->td->dmacount & dma_done_ie)
1234 writel (readl (&ep->dma->dmacount)
1235 | dma_done_ie,
1236 &ep->dma->dmacount);
1237 } else {
1238 struct net2280_request *prev;
1239
1240 prev = list_entry (req->queue.prev,
1241 struct net2280_request, queue);
1242 prev->td->dmadesc = req->td->dmadesc;
1243 if (req->td->dmacount & dma_done_ie)
1244 prev->td->dmacount |= dma_done_ie;
1245 }
1246 }
1247
1248 if (req)
1249 done (ep, req, -ECONNRESET);
1250 ep->stopped = stopped;
1251
1252 if (ep->dma) {
1253 /* turn off dma on inactive queues */
1254 if (list_empty (&ep->queue))
1255 stop_dma (ep->dma);
1256 else if (!ep->stopped) {
1257 /* resume current request, or start new one */
1258 if (req)
1259 writel (dmactl, &ep->dma->dmactl);
1260 else
1261 start_dma (ep, list_entry (ep->queue.next,
1262 struct net2280_request, queue));
1263 }
1264 }
1265
1266 spin_unlock_irqrestore (&ep->dev->lock, flags);
1267 return req ? 0 : -EOPNOTSUPP;
1268 }
1269
1270 /*-------------------------------------------------------------------------*/
1271
1272 static int net2280_fifo_status (struct usb_ep *_ep);
1273
1274 static int
net2280_set_halt(struct usb_ep * _ep,int value)1275 net2280_set_halt (struct usb_ep *_ep, int value)
1276 {
1277 struct net2280_ep *ep;
1278 unsigned long flags;
1279 int retval = 0;
1280
1281 ep = container_of (_ep, struct net2280_ep, ep);
1282 if (!_ep || (!ep->desc && ep->num != 0))
1283 return -EINVAL;
1284 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1285 return -ESHUTDOWN;
1286 if (ep->desc /* not ep0 */ && (ep->desc->bmAttributes & 0x03)
1287 == USB_ENDPOINT_XFER_ISOC)
1288 return -EINVAL;
1289
1290 spin_lock_irqsave (&ep->dev->lock, flags);
1291 if (!list_empty (&ep->queue))
1292 retval = -EAGAIN;
1293 else if (ep->is_in && value && net2280_fifo_status (_ep) != 0)
1294 retval = -EAGAIN;
1295 else {
1296 VDEBUG (ep->dev, "%s %s halt\n", _ep->name,
1297 value ? "set" : "clear");
1298 /* set/clear, then synch memory views with the device */
1299 if (value) {
1300 if (ep->num == 0)
1301 ep->dev->protocol_stall = 1;
1302 else
1303 set_halt (ep);
1304 } else
1305 clear_halt (ep);
1306 (void) readl (&ep->regs->ep_rsp);
1307 }
1308 spin_unlock_irqrestore (&ep->dev->lock, flags);
1309
1310 return retval;
1311 }
1312
1313 static int
net2280_fifo_status(struct usb_ep * _ep)1314 net2280_fifo_status (struct usb_ep *_ep)
1315 {
1316 struct net2280_ep *ep;
1317 u32 avail;
1318
1319 ep = container_of (_ep, struct net2280_ep, ep);
1320 if (!_ep || (!ep->desc && ep->num != 0))
1321 return -ENODEV;
1322 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1323 return -ESHUTDOWN;
1324
1325 avail = readl (&ep->regs->ep_avail) & ((1 << 12) - 1);
1326 if (avail > ep->fifo_size)
1327 return -EOVERFLOW;
1328 if (ep->is_in)
1329 avail = ep->fifo_size - avail;
1330 return avail;
1331 }
1332
1333 static void
net2280_fifo_flush(struct usb_ep * _ep)1334 net2280_fifo_flush (struct usb_ep *_ep)
1335 {
1336 struct net2280_ep *ep;
1337
1338 ep = container_of (_ep, struct net2280_ep, ep);
1339 if (!_ep || (!ep->desc && ep->num != 0))
1340 return;
1341 if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1342 return;
1343
1344 writel ((1 << FIFO_FLUSH), &ep->regs->ep_stat);
1345 (void) readl (&ep->regs->ep_rsp);
1346 }
1347
1348 static struct usb_ep_ops net2280_ep_ops = {
1349 .enable = net2280_enable,
1350 .disable = net2280_disable,
1351
1352 .alloc_request = net2280_alloc_request,
1353 .free_request = net2280_free_request,
1354
1355 .alloc_buffer = net2280_alloc_buffer,
1356 .free_buffer = net2280_free_buffer,
1357
1358 .queue = net2280_queue,
1359 .dequeue = net2280_dequeue,
1360
1361 .set_halt = net2280_set_halt,
1362 .fifo_status = net2280_fifo_status,
1363 .fifo_flush = net2280_fifo_flush,
1364 };
1365
1366 /*-------------------------------------------------------------------------*/
1367
net2280_get_frame(struct usb_gadget * _gadget)1368 static int net2280_get_frame (struct usb_gadget *_gadget)
1369 {
1370 struct net2280 *dev;
1371 unsigned long flags;
1372 u16 retval;
1373
1374 if (!_gadget)
1375 return -ENODEV;
1376 dev = container_of (_gadget, struct net2280, gadget);
1377 spin_lock_irqsave (&dev->lock, flags);
1378 retval = get_idx_reg (dev->regs, REG_FRAME) & 0x03ff;
1379 spin_unlock_irqrestore (&dev->lock, flags);
1380 return retval;
1381 }
1382
net2280_wakeup(struct usb_gadget * _gadget)1383 static int net2280_wakeup (struct usb_gadget *_gadget)
1384 {
1385 struct net2280 *dev;
1386 u32 tmp;
1387 unsigned long flags;
1388
1389 if (!_gadget)
1390 return 0;
1391 dev = container_of (_gadget, struct net2280, gadget);
1392
1393 spin_lock_irqsave (&dev->lock, flags);
1394 tmp = readl (&dev->usb->usbctl);
1395 if (tmp & (1 << DEVICE_REMOTE_WAKEUP_ENABLE))
1396 writel (1 << GENERATE_RESUME, &dev->usb->usbstat);
1397 spin_unlock_irqrestore (&dev->lock, flags);
1398
1399 /* pci writes may still be posted */
1400 return 0;
1401 }
1402
net2280_set_selfpowered(struct usb_gadget * _gadget,int value)1403 static int net2280_set_selfpowered (struct usb_gadget *_gadget, int value)
1404 {
1405 struct net2280 *dev;
1406 u32 tmp;
1407 unsigned long flags;
1408
1409 if (!_gadget)
1410 return 0;
1411 dev = container_of (_gadget, struct net2280, gadget);
1412
1413 spin_lock_irqsave (&dev->lock, flags);
1414 tmp = readl (&dev->usb->usbctl);
1415 if (value)
1416 tmp |= (1 << SELF_POWERED_STATUS);
1417 else
1418 tmp &= ~(1 << SELF_POWERED_STATUS);
1419 writel (tmp, &dev->usb->usbctl);
1420 spin_unlock_irqrestore (&dev->lock, flags);
1421
1422 return 0;
1423 }
1424
1425 static const struct usb_gadget_ops net2280_ops = {
1426 .get_frame = net2280_get_frame,
1427 .wakeup = net2280_wakeup,
1428 .set_selfpowered = net2280_set_selfpowered,
1429 };
1430
1431 /*-------------------------------------------------------------------------*/
1432
1433 #ifdef USE_SYSFS_DEBUG_FILES
1434
1435 /* "function" sysfs attribute */
1436 static ssize_t
show_function(struct device * _dev,char * buf)1437 show_function (struct device *_dev, char *buf)
1438 {
1439 struct net2280 *dev = dev_get_drvdata (_dev);
1440
1441 if (!dev->driver
1442 || !dev->driver->function
1443 || strlen (dev->driver->function) > PAGE_SIZE)
1444 return 0;
1445 return snprintf (buf, PAGE_SIZE, "%s\n", dev->driver->function);
1446 }
1447 static DEVICE_ATTR (function, S_IRUGO, show_function, NULL);
1448
1449 static ssize_t
show_registers(struct device * _dev,char * buf)1450 show_registers (struct device *_dev, char *buf)
1451 {
1452 struct net2280 *dev;
1453 char *next;
1454 unsigned size, t;
1455 unsigned long flags;
1456 int i;
1457 u32 t1, t2;
1458 char *s;
1459
1460 dev = dev_get_drvdata (_dev);
1461 next = buf;
1462 size = PAGE_SIZE;
1463 spin_lock_irqsave (&dev->lock, flags);
1464
1465 if (dev->driver)
1466 s = dev->driver->driver.name;
1467 else
1468 s = "(none)";
1469
1470 /* Main Control Registers */
1471 t = snprintf (next, size, "%s version " DRIVER_VERSION
1472 ", chiprev %04x, dma %s\n\n"
1473 "devinit %03x fifoctl %08x gadget '%s'\n"
1474 "pci irqenb0 %02x irqenb1 %08x "
1475 "irqstat0 %04x irqstat1 %08x\n",
1476 driver_name, dev->chiprev,
1477 use_dma
1478 ? (use_dma_chaining ? "chaining" : "enabled")
1479 : "disabled",
1480 readl (&dev->regs->devinit),
1481 readl (&dev->regs->fifoctl),
1482 s,
1483 readl (&dev->regs->pciirqenb0),
1484 readl (&dev->regs->pciirqenb1),
1485 readl (&dev->regs->irqstat0),
1486 readl (&dev->regs->irqstat1));
1487 size -= t;
1488 next += t;
1489
1490 /* USB Control Registers */
1491 t1 = readl (&dev->usb->usbctl);
1492 t2 = readl (&dev->usb->usbstat);
1493 if (t1 & (1 << VBUS_PIN)) {
1494 if (t2 & (1 << HIGH_SPEED))
1495 s = "high speed";
1496 else if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1497 s = "powered";
1498 else
1499 s = "full speed";
1500 /* full speed bit (6) not working?? */
1501 } else
1502 s = "not attached";
1503 t = snprintf (next, size,
1504 "stdrsp %08x usbctl %08x usbstat %08x "
1505 "addr 0x%02x (%s)\n",
1506 readl (&dev->usb->stdrsp), t1, t2,
1507 readl (&dev->usb->ouraddr), s);
1508 size -= t;
1509 next += t;
1510
1511 /* PCI Master Control Registers */
1512
1513 /* DMA Control Registers */
1514
1515 /* Configurable EP Control Registers */
1516 for (i = 0; i < 7; i++) {
1517 struct net2280_ep *ep;
1518
1519 ep = &dev->ep [i];
1520 if (i && !ep->desc)
1521 continue;
1522
1523 t1 = readl (&ep->regs->ep_cfg);
1524 t2 = readl (&ep->regs->ep_rsp) & 0xff;
1525 t = snprintf (next, size,
1526 "\n%s\tcfg %05x rsp (%02x) %s%s%s%s%s%s%s%s"
1527 "irqenb %02x\n",
1528 ep->ep.name, t1, t2,
1529 (t2 & (1 << CLEAR_NAK_OUT_PACKETS))
1530 ? "NAK " : "",
1531 (t2 & (1 << CLEAR_EP_HIDE_STATUS_PHASE))
1532 ? "hide " : "",
1533 (t2 & (1 << CLEAR_EP_FORCE_CRC_ERROR))
1534 ? "CRC " : "",
1535 (t2 & (1 << CLEAR_INTERRUPT_MODE))
1536 ? "interrupt " : "",
1537 (t2 & (1<<CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE))
1538 ? "status " : "",
1539 (t2 & (1 << CLEAR_NAK_OUT_PACKETS_MODE))
1540 ? "NAKmode " : "",
1541 (t2 & (1 << CLEAR_ENDPOINT_TOGGLE))
1542 ? "DATA1 " : "DATA0 ",
1543 (t2 & (1 << CLEAR_ENDPOINT_HALT))
1544 ? "HALT " : "",
1545 readl (&ep->regs->ep_irqenb));
1546 size -= t;
1547 next += t;
1548
1549 t = snprintf (next, size,
1550 "\tstat %08x avail %04x "
1551 "(ep%d%s-%s)%s\n",
1552 readl (&ep->regs->ep_stat),
1553 readl (&ep->regs->ep_avail),
1554 t1 & 0x0f, DIR_STRING (t1),
1555 type_string (t1 >> 8),
1556 ep->stopped ? "*" : "");
1557 size -= t;
1558 next += t;
1559
1560 if (!ep->dma)
1561 continue;
1562
1563 t = snprintf (next, size,
1564 " dma\tctl %08x stat %08x count %08x\n"
1565 "\taddr %08x desc %08x\n",
1566 readl (&ep->dma->dmactl),
1567 readl (&ep->dma->dmastat),
1568 readl (&ep->dma->dmacount),
1569 readl (&ep->dma->dmaaddr),
1570 readl (&ep->dma->dmadesc));
1571 size -= t;
1572 next += t;
1573
1574 }
1575
1576 /* Indexed Registers */
1577 // none yet
1578
1579 /* Statistics */
1580 t = snprintf (next, size, "\nirqs: ");
1581 size -= t;
1582 next += t;
1583 for (i = 0; i < 7; i++) {
1584 struct net2280_ep *ep;
1585
1586 ep = &dev->ep [i];
1587 if (i && !ep->irqs)
1588 continue;
1589 t = snprintf (next, size, " %s/%lu", ep->ep.name, ep->irqs);
1590 size -= t;
1591 next += t;
1592
1593 }
1594 t = snprintf (next, size, "\n");
1595 size -= t;
1596 next += t;
1597
1598 spin_unlock_irqrestore (&dev->lock, flags);
1599
1600 return PAGE_SIZE - size;
1601 }
1602 static DEVICE_ATTR (registers, S_IRUGO, show_registers, NULL);
1603
1604 static ssize_t
show_queues(struct device * _dev,char * buf)1605 show_queues (struct device *_dev, char *buf)
1606 {
1607 struct net2280 *dev;
1608 char *next;
1609 unsigned size;
1610 unsigned long flags;
1611 int i;
1612
1613 dev = dev_get_drvdata (_dev);
1614 next = buf;
1615 size = PAGE_SIZE;
1616 spin_lock_irqsave (&dev->lock, flags);
1617
1618 for (i = 0; i < 7; i++) {
1619 struct net2280_ep *ep = &dev->ep [i];
1620 struct net2280_request *req;
1621 int t;
1622
1623 if (i != 0) {
1624 const struct usb_endpoint_descriptor *d;
1625
1626 d = ep->desc;
1627 if (!d)
1628 continue;
1629 t = d->bEndpointAddress;
1630 t = snprintf (next, size,
1631 "\n%s (ep%d%s-%s) max %04x %s fifo %d\n",
1632 ep->ep.name, t & USB_ENDPOINT_NUMBER_MASK,
1633 (t & USB_DIR_IN) ? "in" : "out",
1634 ({ char *val;
1635 switch (d->bmAttributes & 0x03) {
1636 case USB_ENDPOINT_XFER_BULK:
1637 val = "bulk"; break;
1638 case USB_ENDPOINT_XFER_INT:
1639 val = "intr"; break;
1640 default:
1641 val = "iso"; break;
1642 }; val; }),
1643 le16_to_cpu (d->wMaxPacketSize) & 0x1fff,
1644 ep->dma ? "dma" : "pio", ep->fifo_size
1645 );
1646 } else /* ep0 should only have one transfer queued */
1647 t = snprintf (next, size, "ep0 max 64 pio %s\n",
1648 ep->is_in ? "in" : "out");
1649 if (t <= 0 || t > size)
1650 goto done;
1651 size -= t;
1652 next += t;
1653
1654 if (list_empty (&ep->queue)) {
1655 t = snprintf (next, size, "\t(nothing queued)\n");
1656 if (t <= 0 || t > size)
1657 goto done;
1658 size -= t;
1659 next += t;
1660 continue;
1661 }
1662 list_for_each_entry (req, &ep->queue, queue) {
1663 if (ep->dma && req->td_dma == readl (&ep->dma->dmadesc))
1664 t = snprintf (next, size,
1665 "\treq %p len %d/%d "
1666 "buf %p (dmacount %08x)\n",
1667 &req->req, req->req.actual,
1668 req->req.length, req->req.buf,
1669 readl (&ep->dma->dmacount));
1670 else
1671 t = snprintf (next, size,
1672 "\treq %p len %d/%d buf %p\n",
1673 &req->req, req->req.actual,
1674 req->req.length, req->req.buf);
1675 if (t <= 0 || t > size)
1676 goto done;
1677 size -= t;
1678 next += t;
1679
1680 if (ep->dma) {
1681 struct net2280_dma *td;
1682
1683 td = req->td;
1684 t = snprintf (next, size, "\t td %08x "
1685 " count %08x buf %08x desc %08x\n",
1686 req->td_dma, td->dmacount,
1687 td->dmaaddr, td->dmadesc);
1688 if (t <= 0 || t > size)
1689 goto done;
1690 size -= t;
1691 next += t;
1692 }
1693 }
1694 }
1695
1696 done:
1697 spin_unlock_irqrestore (&dev->lock, flags);
1698 return PAGE_SIZE - size;
1699 }
1700 static DEVICE_ATTR (queues, S_IRUGO, show_queues, NULL);
1701
1702
1703 #else
1704
1705 #define device_create_file(a,b) do {} while (0)
1706 #define device_remove_file device_create_file
1707
1708 #endif
1709
1710 /*-------------------------------------------------------------------------*/
1711
1712 /* another driver-specific mode might be a request type doing dma
1713 * to/from another device fifo instead of to/from memory.
1714 */
1715
set_fifo_mode(struct net2280 * dev,int mode)1716 static void set_fifo_mode (struct net2280 *dev, int mode)
1717 {
1718 /* keeping high bits preserves BAR2 */
1719 writel ((0xffff << PCI_BASE2_RANGE) | mode, &dev->regs->fifoctl);
1720
1721 /* always ep-{a,b,e,f} ... maybe not ep-c or ep-d */
1722 INIT_LIST_HEAD (&dev->gadget.ep_list);
1723 list_add_tail (&dev->ep [1].ep.ep_list, &dev->gadget.ep_list);
1724 list_add_tail (&dev->ep [2].ep.ep_list, &dev->gadget.ep_list);
1725 switch (mode) {
1726 case 0:
1727 list_add_tail (&dev->ep [3].ep.ep_list, &dev->gadget.ep_list);
1728 list_add_tail (&dev->ep [4].ep.ep_list, &dev->gadget.ep_list);
1729 dev->ep [1].fifo_size = dev->ep [2].fifo_size = 1024;
1730 break;
1731 case 1:
1732 dev->ep [1].fifo_size = dev->ep [2].fifo_size = 2048;
1733 break;
1734 case 2:
1735 list_add_tail (&dev->ep [3].ep.ep_list, &dev->gadget.ep_list);
1736 dev->ep [1].fifo_size = 2048;
1737 dev->ep [2].fifo_size = 1024;
1738 break;
1739 }
1740 /* fifo sizes for ep0, ep-c, ep-d, ep-e, and ep-f never change */
1741 list_add_tail (&dev->ep [5].ep.ep_list, &dev->gadget.ep_list);
1742 list_add_tail (&dev->ep [6].ep.ep_list, &dev->gadget.ep_list);
1743 }
1744
1745 /**
1746 * net2280_set_fifo_mode - change allocation of fifo buffers
1747 * @gadget: access to the net2280 device that will be updated
1748 * @mode: 0 for default, four 1kB buffers (ep-a through ep-d);
1749 * 1 for two 2kB buffers (ep-a and ep-b only);
1750 * 2 for one 2kB buffer (ep-a) and two 1kB ones (ep-b, ep-c).
1751 *
1752 * returns zero on success, else negative errno. when this succeeds,
1753 * the contents of gadget->ep_list may have changed.
1754 *
1755 * you may only call this function when endpoints a-d are all disabled.
1756 * use it whenever extra hardware buffering can help performance, such
1757 * as before enabling "high bandwidth" interrupt endpoints that use
1758 * maxpacket bigger than 512 (when double buffering would otherwise
1759 * be unavailable).
1760 */
net2280_set_fifo_mode(struct usb_gadget * gadget,int mode)1761 int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode)
1762 {
1763 int i;
1764 struct net2280 *dev;
1765 int status = 0;
1766 unsigned long flags;
1767
1768 if (!gadget)
1769 return -ENODEV;
1770 dev = container_of (gadget, struct net2280, gadget);
1771
1772 spin_lock_irqsave (&dev->lock, flags);
1773
1774 for (i = 1; i <= 4; i++)
1775 if (dev->ep [i].desc) {
1776 status = -EINVAL;
1777 break;
1778 }
1779 if (mode < 0 || mode > 2)
1780 status = -EINVAL;
1781 if (status == 0)
1782 set_fifo_mode (dev, mode);
1783 spin_unlock_irqrestore (&dev->lock, flags);
1784
1785 if (status == 0) {
1786 if (mode == 1)
1787 DEBUG (dev, "fifo: ep-a 2K, ep-b 2K\n");
1788 else if (mode == 2)
1789 DEBUG (dev, "fifo: ep-a 2K, ep-b 1K, ep-c 1K\n");
1790 /* else all are 1K */
1791 }
1792 return status;
1793 }
1794 EXPORT_SYMBOL (net2280_set_fifo_mode);
1795
1796 /*-------------------------------------------------------------------------*/
1797
1798 /* keeping it simple:
1799 * - one bus driver, initted first;
1800 * - one function driver, initted second
1801 *
1802 * most of the work to support multiple net2280 controllers would
1803 * be to associate this gadget driver (yes?) with all of them, or
1804 * perhaps to bind specific drivers to specific devices.
1805 */
1806
1807 static struct net2280 *the_controller;
1808
usb_reset(struct net2280 * dev)1809 static void usb_reset (struct net2280 *dev)
1810 {
1811 u32 tmp;
1812
1813 /* force immediate bus disconnect, and synch through pci */
1814 writel (0, &dev->usb->usbctl);
1815 dev->gadget.speed = USB_SPEED_UNKNOWN;
1816 (void) readl (&dev->usb->usbctl);
1817
1818 net2280_led_init (dev);
1819
1820 /* disable automatic responses, and irqs */
1821 writel (0, &dev->usb->stdrsp);
1822 writel (0, &dev->regs->pciirqenb0);
1823 writel (0, &dev->regs->pciirqenb1);
1824
1825 /* clear old dma and irq state */
1826 for (tmp = 0; tmp < 4; tmp++) {
1827 struct net2280_ep *ep = &dev->ep [tmp + 1];
1828
1829 if (ep->dma)
1830 abort_dma (ep);
1831 }
1832 writel (~0, &dev->regs->irqstat0),
1833 writel (~(1 << SUSPEND_REQUEST_INTERRUPT), &dev->regs->irqstat1),
1834
1835 /* reset, and enable pci */
1836 tmp = readl (&dev->regs->devinit)
1837 | (1 << PCI_ENABLE)
1838 | (1 << FIFO_SOFT_RESET)
1839 | (1 << USB_SOFT_RESET)
1840 | (1 << M8051_RESET);
1841 writel (tmp, &dev->regs->devinit);
1842
1843 /* standard fifo and endpoint allocations */
1844 set_fifo_mode (dev, (fifo_mode <= 2) ? fifo_mode : 0);
1845 }
1846
usb_reinit(struct net2280 * dev)1847 static void usb_reinit (struct net2280 *dev)
1848 {
1849 u32 tmp;
1850 int init_dma;
1851
1852 /* use_dma changes are ignored till next device re-init */
1853 init_dma = use_dma;
1854
1855 /* basic endpoint init */
1856 for (tmp = 0; tmp < 7; tmp++) {
1857 struct net2280_ep *ep = &dev->ep [tmp];
1858
1859 ep->ep.name = ep_name [tmp];
1860 ep->dev = dev;
1861 ep->num = tmp;
1862
1863 if (tmp > 0 && tmp <= 4) {
1864 ep->fifo_size = 1024;
1865 if (init_dma)
1866 ep->dma = &dev->dma [tmp - 1];
1867 } else
1868 ep->fifo_size = 64;
1869 ep->regs = &dev->epregs [tmp];
1870 ep_reset (dev->regs, ep);
1871 }
1872 dev->ep [0].ep.maxpacket = 64;
1873 dev->ep [5].ep.maxpacket = 64;
1874 dev->ep [6].ep.maxpacket = 64;
1875
1876 dev->gadget.ep0 = &dev->ep [0].ep;
1877 dev->ep [0].stopped = 0;
1878 INIT_LIST_HEAD (&dev->gadget.ep0->ep_list);
1879
1880 /* we want to prevent lowlevel/insecure access from the USB host,
1881 * but erratum 0119 means this enable bit is ignored
1882 */
1883 for (tmp = 0; tmp < 5; tmp++)
1884 writel (EP_DONTUSE, &dev->dep [tmp].dep_cfg);
1885 }
1886
ep0_start(struct net2280 * dev)1887 static void ep0_start (struct net2280 *dev)
1888 {
1889 writel ( (1 << CLEAR_EP_HIDE_STATUS_PHASE)
1890 | (1 << CLEAR_NAK_OUT_PACKETS)
1891 | (1 << CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE)
1892 , &dev->epregs [0].ep_rsp);
1893
1894 /*
1895 * hardware optionally handles a bunch of standard requests
1896 * that the API hides from drivers anyway. have it do so.
1897 * endpoint status/features are handled in software, to
1898 * help pass tests for some dubious behavior.
1899 */
1900 writel ( (1 << SET_TEST_MODE)
1901 | (1 << SET_ADDRESS)
1902 | (1 << DEVICE_SET_CLEAR_DEVICE_REMOTE_WAKEUP)
1903 | (1 << GET_DEVICE_STATUS)
1904 | (1 << GET_INTERFACE_STATUS)
1905 , &dev->usb->stdrsp);
1906 writel ( (1 << USB_ROOT_PORT_WAKEUP_ENABLE)
1907 | (1 << SELF_POWERED_USB_DEVICE)
1908 | (1 << REMOTE_WAKEUP_SUPPORT)
1909 | (1 << USB_DETECT_ENABLE)
1910 | (1 << SELF_POWERED_STATUS)
1911 , &dev->usb->usbctl);
1912
1913 /* enable irqs so we can see ep0 and general operation */
1914 writel ( (1 << SETUP_PACKET_INTERRUPT_ENABLE)
1915 | (1 << ENDPOINT_0_INTERRUPT_ENABLE)
1916 , &dev->regs->pciirqenb0);
1917 writel ( (1 << PCI_INTERRUPT_ENABLE)
1918 | (1 << PCI_MASTER_ABORT_RECEIVED_INTERRUPT_ENABLE)
1919 | (1 << PCI_TARGET_ABORT_RECEIVED_INTERRUPT_ENABLE)
1920 | (1 << PCI_RETRY_ABORT_INTERRUPT_ENABLE)
1921 | (1 << VBUS_INTERRUPT_ENABLE)
1922 | (1 << ROOT_PORT_RESET_INTERRUPT_ENABLE)
1923 , &dev->regs->pciirqenb1);
1924
1925 /* don't leave any writes posted */
1926 (void) readl (&dev->usb->usbctl);
1927 }
1928
1929 /* when a driver is successfully registered, it will receive
1930 * control requests including set_configuration(), which enables
1931 * non-control requests. then usb traffic follows until a
1932 * disconnect is reported. then a host may connect again, or
1933 * the driver might get unbound.
1934 */
usb_gadget_register_driver(struct usb_gadget_driver * driver)1935 int usb_gadget_register_driver (struct usb_gadget_driver *driver)
1936 {
1937 struct net2280 *dev = the_controller;
1938 int retval;
1939 unsigned i;
1940
1941 /* insist on high speed support from the driver, since
1942 * (dev->usb->xcvrdiag & FORCE_FULL_SPEED_MODE)
1943 * "must not be used in normal operation"
1944 */
1945 if (!driver
1946 || driver->speed != USB_SPEED_HIGH
1947 || !driver->bind
1948 || !driver->unbind
1949 || !driver->setup)
1950 return -EINVAL;
1951 if (!dev)
1952 return -ENODEV;
1953 if (dev->driver)
1954 return -EBUSY;
1955
1956 for (i = 0; i < 7; i++)
1957 dev->ep [i].irqs = 0;
1958
1959 /* hook up the driver ... */
1960 dev->driver = driver;
1961 retval = driver->bind (&dev->gadget);
1962 if (retval) {
1963 DEBUG (dev, "bind to driver %s --> %d\n",
1964 driver->driver.name, retval);
1965 dev->driver = 0;
1966 return retval;
1967 }
1968
1969 /* ... then enable host detection and ep0; and we're ready
1970 * for set_configuration as well as eventual disconnect.
1971 */
1972 net2280_led_active (dev, 1);
1973 ep0_start (dev);
1974
1975 DEBUG (dev, "%s ready, usbctl %08x stdrsp %08x\n",
1976 driver->driver.name,
1977 readl (&dev->usb->usbctl),
1978 readl (&dev->usb->stdrsp));
1979
1980 /* pci writes may still be posted */
1981 return 0;
1982 }
1983 EXPORT_SYMBOL (usb_gadget_register_driver);
1984
1985 static void
stop_activity(struct net2280 * dev,struct usb_gadget_driver * driver)1986 stop_activity (struct net2280 *dev, struct usb_gadget_driver *driver)
1987 {
1988 int i;
1989
1990 /* don't disconnect if it's not connected */
1991 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1992 driver = 0;
1993
1994 /* stop hardware; prevent new request submissions;
1995 * and kill any outstanding requests.
1996 */
1997 usb_reset (dev);
1998 for (i = 0; i < 7; i++)
1999 nuke (&dev->ep [i]);
2000
2001 /* report disconnect; the driver is already quiesced */
2002 if (driver) {
2003 spin_unlock (&dev->lock);
2004 driver->disconnect (&dev->gadget);
2005 spin_lock (&dev->lock);
2006 }
2007
2008 usb_reinit (dev);
2009 }
2010
usb_gadget_unregister_driver(struct usb_gadget_driver * driver)2011 int usb_gadget_unregister_driver (struct usb_gadget_driver *driver)
2012 {
2013 struct net2280 *dev = the_controller;
2014 unsigned long flags;
2015
2016 if (!dev)
2017 return -ENODEV;
2018 if (!driver || driver != dev->driver)
2019 return -EINVAL;
2020
2021 spin_lock_irqsave (&dev->lock, flags);
2022 stop_activity (dev, driver);
2023 spin_unlock_irqrestore (&dev->lock, flags);
2024
2025 driver->unbind (&dev->gadget);
2026 dev->driver = 0;
2027
2028 net2280_led_active (dev, 0);
2029
2030 DEBUG (dev, "unregistered driver '%s'\n", driver->driver.name);
2031 return 0;
2032 }
2033 EXPORT_SYMBOL (usb_gadget_unregister_driver);
2034
2035
2036 /*-------------------------------------------------------------------------*/
2037
2038 /* handle ep0, ep-e, ep-f with 64 byte packets: packet per irq.
2039 * also works for dma-capable endpoints, in pio mode or just
2040 * to manually advance the queue after short OUT transfers.
2041 */
handle_ep_small(struct net2280_ep * ep)2042 static void handle_ep_small (struct net2280_ep *ep)
2043 {
2044 struct net2280_request *req;
2045 u32 t;
2046 /* 0 error, 1 mid-data, 2 done */
2047 int mode = 1;
2048
2049 if (!list_empty (&ep->queue))
2050 req = list_entry (ep->queue.next,
2051 struct net2280_request, queue);
2052 else
2053 req = 0;
2054
2055 /* ack all, and handle what we care about */
2056 t = readl (&ep->regs->ep_stat);
2057 ep->irqs++;
2058 #if 0
2059 VDEBUG (ep->dev, "%s ack ep_stat %08x, req %p\n",
2060 ep->ep.name, t, req ? &req->req : 0);
2061 #endif
2062 writel (t & ~(1 << NAK_OUT_PACKETS), &ep->regs->ep_stat);
2063
2064 /* for ep0, monitor token irqs to catch data stage length errors
2065 * and to synchronize on status.
2066 *
2067 * also, to defer reporting of protocol stalls ... here's where
2068 * data or status first appears, handling stalls here should never
2069 * cause trouble on the host side..
2070 *
2071 * control requests could be slightly faster without token synch for
2072 * status, but status can jam up that way.
2073 */
2074 if (unlikely (ep->num == 0)) {
2075 if (ep->is_in) {
2076 /* status; stop NAKing */
2077 if (t & (1 << DATA_OUT_PING_TOKEN_INTERRUPT)) {
2078 if (ep->dev->protocol_stall) {
2079 ep->stopped = 1;
2080 set_halt (ep);
2081 }
2082 if (!req)
2083 allow_status (ep);
2084 mode = 2;
2085 /* reply to extra IN data tokens with a zlp */
2086 } else if (t & (1 << DATA_IN_TOKEN_INTERRUPT)) {
2087 if (ep->dev->protocol_stall) {
2088 ep->stopped = 1;
2089 set_halt (ep);
2090 mode = 2;
2091 } else if (!req && ep->stopped)
2092 write_fifo (ep, 0);
2093 }
2094 } else {
2095 /* status; stop NAKing */
2096 if (t & (1 << DATA_IN_TOKEN_INTERRUPT)) {
2097 if (ep->dev->protocol_stall) {
2098 ep->stopped = 1;
2099 set_halt (ep);
2100 }
2101 mode = 2;
2102 /* an extra OUT token is an error */
2103 } else if (((t & (1 << DATA_OUT_PING_TOKEN_INTERRUPT))
2104 && req
2105 && req->req.actual == req->req.length)
2106 || !req) {
2107 ep->dev->protocol_stall = 1;
2108 set_halt (ep);
2109 ep->stopped = 1;
2110 if (req)
2111 done (ep, req, -EOVERFLOW);
2112 req = 0;
2113 }
2114 }
2115 }
2116
2117 if (unlikely (!req))
2118 return;
2119
2120 /* manual DMA queue advance after short OUT */
2121 if (likely (ep->dma != 0)) {
2122 if (t & (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)) {
2123 u32 count;
2124 int stopped = ep->stopped;
2125
2126 /* TRANSFERRED works around OUT_DONE erratum 0112.
2127 * we expect (N <= maxpacket) bytes; host wrote M.
2128 * iff (M < N) we won't ever see a DMA interrupt.
2129 */
2130 ep->stopped = 1;
2131 for (count = 0; ; t = readl (&ep->regs->ep_stat)) {
2132
2133 /* any preceding dma transfers must finish.
2134 * dma handles (M >= N), may empty the queue
2135 */
2136 scan_dma_completions (ep);
2137 if (unlikely (list_empty (&ep->queue)
2138 || ep->out_overflow)) {
2139 req = 0;
2140 break;
2141 }
2142 req = list_entry (ep->queue.next,
2143 struct net2280_request, queue);
2144
2145 /* here either (M < N), a "real" short rx;
2146 * or (M == N) and the queue didn't empty
2147 */
2148 if (likely (t & (1 << FIFO_EMPTY))) {
2149 count = readl (&ep->dma->dmacount);
2150 count &= DMA_BYTE_COUNT_MASK;
2151 if (readl (&ep->dma->dmadesc)
2152 != req->td_dma)
2153 req = 0;
2154 break;
2155 }
2156 udelay(1);
2157 }
2158
2159 /* stop DMA, leave ep NAKing */
2160 writel ((1 << DMA_ABORT), &ep->dma->dmastat);
2161 spin_stop_dma (ep->dma);
2162
2163 if (likely (req != 0)) {
2164 req->td->dmacount = 0;
2165 t = readl (&ep->regs->ep_avail);
2166 dma_done (ep, req, count, t);
2167 }
2168
2169 /* also flush to prevent erratum 0106 trouble */
2170 if (unlikely (ep->out_overflow
2171 || (ep->dev->chiprev == 0x0100
2172 && ep->dev->gadget.speed
2173 == USB_SPEED_FULL))) {
2174 out_flush (ep);
2175 ep->out_overflow = 0;
2176 }
2177
2178 /* (re)start dma if needed, stop NAKing */
2179 ep->stopped = stopped;
2180 if (!list_empty (&ep->queue))
2181 restart_dma (ep);
2182 } else
2183 DEBUG (ep->dev, "%s dma ep_stat %08x ??\n",
2184 ep->ep.name, t);
2185 return;
2186
2187 /* data packet(s) received (in the fifo, OUT) */
2188 } else if (t & (1 << DATA_PACKET_RECEIVED_INTERRUPT)) {
2189 if (read_fifo (ep, req) && ep->num != 0)
2190 mode = 2;
2191
2192 /* data packet(s) transmitted (IN) */
2193 } else if (t & (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)) {
2194 unsigned len;
2195
2196 len = req->req.length - req->req.actual;
2197 if (len > ep->ep.maxpacket)
2198 len = ep->ep.maxpacket;
2199 req->req.actual += len;
2200
2201 /* if we wrote it all, we're usually done */
2202 if (req->req.actual == req->req.length) {
2203 if (ep->num == 0) {
2204 /* wait for control status */
2205 if (mode != 2)
2206 req = 0;
2207 } else if (!req->req.zero || len != ep->ep.maxpacket)
2208 mode = 2;
2209 }
2210
2211 /* there was nothing to do ... */
2212 } else if (mode == 1)
2213 return;
2214
2215 /* done */
2216 if (mode == 2) {
2217 /* stream endpoints often resubmit/unlink in completion */
2218 done (ep, req, 0);
2219
2220 /* maybe advance queue to next request */
2221 if (ep->num == 0) {
2222 /* NOTE: net2280 could let gadget driver start the
2223 * status stage later. since not all controllers let
2224 * them control that, the api doesn't (yet) allow it.
2225 */
2226 if (!ep->stopped)
2227 allow_status (ep);
2228 req = 0;
2229 } else {
2230 if (!list_empty (&ep->queue) && !ep->stopped)
2231 req = list_entry (ep->queue.next,
2232 struct net2280_request, queue);
2233 else
2234 req = 0;
2235 if (req && !ep->is_in)
2236 stop_out_naking (ep);
2237 }
2238 }
2239
2240 /* is there a buffer for the next packet?
2241 * for best streaming performance, make sure there is one.
2242 */
2243 if (req && !ep->stopped) {
2244
2245 /* load IN fifo with next packet (may be zlp) */
2246 if (t & (1 << DATA_PACKET_TRANSMITTED_INTERRUPT))
2247 write_fifo (ep, &req->req);
2248 }
2249 }
2250
2251 static struct net2280_ep *
get_ep_by_addr(struct net2280 * dev,u16 wIndex)2252 get_ep_by_addr (struct net2280 *dev, u16 wIndex)
2253 {
2254 struct net2280_ep *ep;
2255
2256 if ((wIndex & USB_ENDPOINT_NUMBER_MASK) == 0)
2257 return &dev->ep [0];
2258 list_for_each_entry (ep, &dev->gadget.ep_list, ep.ep_list) {
2259 u8 bEndpointAddress;
2260
2261 if (!ep->desc)
2262 continue;
2263 bEndpointAddress = ep->desc->bEndpointAddress;
2264 if ((wIndex ^ bEndpointAddress) & USB_DIR_IN)
2265 continue;
2266 if ((wIndex & 0x0f) == (bEndpointAddress & 0x0f))
2267 return ep;
2268 }
2269 return 0;
2270 }
2271
handle_stat0_irqs(struct net2280 * dev,u32 stat)2272 static void handle_stat0_irqs (struct net2280 *dev, u32 stat)
2273 {
2274 struct net2280_ep *ep;
2275 u32 num, scratch;
2276
2277 /* most of these don't need individual acks */
2278 stat &= ~(1 << INTA_ASSERTED);
2279 if (!stat)
2280 return;
2281 // DEBUG (dev, "irqstat0 %04x\n", stat);
2282
2283 /* starting a control request? */
2284 if (unlikely (stat & (1 << SETUP_PACKET_INTERRUPT))) {
2285 union {
2286 u32 raw [2];
2287 struct usb_ctrlrequest r;
2288 } u;
2289 int tmp = 0;
2290 struct net2280_request *req;
2291
2292 if (dev->gadget.speed == USB_SPEED_UNKNOWN) {
2293 if (readl (&dev->usb->usbstat) & (1 << HIGH_SPEED))
2294 dev->gadget.speed = USB_SPEED_HIGH;
2295 else
2296 dev->gadget.speed = USB_SPEED_FULL;
2297 net2280_led_speed (dev, dev->gadget.speed);
2298 DEBUG (dev, "%s speed\n",
2299 (dev->gadget.speed == USB_SPEED_HIGH)
2300 ? "high" : "full");
2301 }
2302
2303 ep = &dev->ep [0];
2304 ep->irqs++;
2305
2306 /* make sure any leftover request state is cleared */
2307 stat &= ~(1 << ENDPOINT_0_INTERRUPT);
2308 while (!list_empty (&ep->queue)) {
2309 req = list_entry (ep->queue.next,
2310 struct net2280_request, queue);
2311 done (ep, req, (req->req.actual == req->req.length)
2312 ? 0 : -EPROTO);
2313 }
2314 ep->stopped = 0;
2315 dev->protocol_stall = 0;
2316 writel ( (1 << TIMEOUT)
2317 | (1 << USB_STALL_SENT)
2318 | (1 << USB_IN_NAK_SENT)
2319 | (1 << USB_IN_ACK_RCVD)
2320 | (1 << USB_OUT_PING_NAK_SENT)
2321 | (1 << USB_OUT_ACK_SENT)
2322 | (1 << FIFO_OVERFLOW)
2323 | (1 << FIFO_UNDERFLOW)
2324 | (1 << SHORT_PACKET_OUT_DONE_INTERRUPT)
2325 | (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)
2326 | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
2327 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
2328 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
2329 | (1 << DATA_IN_TOKEN_INTERRUPT)
2330 , &ep->regs->ep_stat);
2331 u.raw [0] = readl (&dev->usb->setup0123);
2332 u.raw [1] = readl (&dev->usb->setup4567);
2333
2334 cpu_to_le32s (&u.raw [0]);
2335 cpu_to_le32s (&u.raw [1]);
2336
2337 le16_to_cpus (&u.r.wValue);
2338 le16_to_cpus (&u.r.wIndex);
2339 le16_to_cpus (&u.r.wLength);
2340
2341 /* ack the irq */
2342 writel (1 << SETUP_PACKET_INTERRUPT, &dev->regs->irqstat0);
2343 stat ^= (1 << SETUP_PACKET_INTERRUPT);
2344
2345 /* watch control traffic at the token level, and force
2346 * synchronization before letting the status stage happen.
2347 * FIXME ignore tokens we'll NAK, until driver responds.
2348 * that'll mean a lot less irqs for some drivers.
2349 */
2350 ep->is_in = (u.r.bRequestType & USB_DIR_IN) != 0;
2351 if (ep->is_in) {
2352 scratch = (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
2353 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
2354 | (1 << DATA_IN_TOKEN_INTERRUPT);
2355 stop_out_naking (ep);
2356 } else
2357 scratch = (1 << DATA_PACKET_RECEIVED_INTERRUPT)
2358 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
2359 | (1 << DATA_IN_TOKEN_INTERRUPT);
2360 writel (scratch, &dev->epregs [0].ep_irqenb);
2361
2362 /* we made the hardware handle most lowlevel requests;
2363 * everything else goes uplevel to the gadget code.
2364 */
2365 switch (u.r.bRequest) {
2366 case USB_REQ_GET_STATUS: {
2367 struct net2280_ep *e;
2368 u16 status;
2369
2370 /* hw handles device and interface status */
2371 if (u.r.bRequestType != (USB_DIR_IN|USB_RECIP_ENDPOINT))
2372 goto delegate;
2373 if ((e = get_ep_by_addr (dev, u.r.wIndex)) == 0
2374 || u.r.wLength > 2)
2375 goto do_stall;
2376
2377 if (readl (&e->regs->ep_rsp)
2378 & (1 << SET_ENDPOINT_HALT))
2379 status = __constant_cpu_to_le16 (1);
2380 else
2381 status = __constant_cpu_to_le16 (0);
2382
2383 /* don't bother with a request object! */
2384 writel (0, &dev->epregs [0].ep_irqenb);
2385 set_fifo_bytecount (ep, u.r.wLength);
2386 writel (status, &dev->epregs [0].ep_data);
2387 allow_status (ep);
2388 VDEBUG (dev, "%s stat %02x\n", ep->ep.name, status);
2389 goto next_endpoints;
2390 }
2391 break;
2392 case USB_REQ_CLEAR_FEATURE: {
2393 struct net2280_ep *e;
2394
2395 /* hw handles device features */
2396 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
2397 goto delegate;
2398 if (u.r.wValue != 0 /* HALT feature */
2399 || u.r.wLength != 0)
2400 goto do_stall;
2401 if ((e = get_ep_by_addr (dev, u.r.wIndex)) == 0)
2402 goto do_stall;
2403 clear_halt (e);
2404 allow_status (ep);
2405 VDEBUG (dev, "%s clear halt\n", ep->ep.name);
2406 goto next_endpoints;
2407 }
2408 break;
2409 case USB_REQ_SET_FEATURE: {
2410 struct net2280_ep *e;
2411
2412 /* hw handles device features */
2413 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
2414 goto delegate;
2415 if (u.r.wValue != 0 /* HALT feature */
2416 || u.r.wLength != 0)
2417 goto do_stall;
2418 if ((e = get_ep_by_addr (dev, u.r.wIndex)) == 0)
2419 goto do_stall;
2420 set_halt (e);
2421 allow_status (ep);
2422 VDEBUG (dev, "%s set halt\n", ep->ep.name);
2423 goto next_endpoints;
2424 }
2425 break;
2426 default:
2427 delegate:
2428 VDEBUG (dev, "setup %02x.%02x v%04x i%04x "
2429 "ep_cfg %08x\n",
2430 u.r.bRequestType, u.r.bRequest,
2431 u.r.wValue, u.r.wIndex,
2432 readl (&ep->regs->ep_cfg));
2433 spin_unlock (&dev->lock);
2434 tmp = dev->driver->setup (&dev->gadget, &u.r);
2435 spin_lock (&dev->lock);
2436 }
2437
2438 /* stall ep0 on error */
2439 if (tmp < 0) {
2440 do_stall:
2441 VDEBUG (dev, "req %02x.%02x protocol STALL; stat %d\n",
2442 u.r.bRequestType, u.r.bRequest, tmp);
2443 dev->protocol_stall = 1;
2444 }
2445
2446 /* some in/out token irq should follow; maybe stall then.
2447 * driver must queue a request (even zlp) or halt ep0
2448 * before the host times out.
2449 */
2450 }
2451
2452 next_endpoints:
2453 /* endpoint data irq ? */
2454 scratch = stat & 0x7f;
2455 stat &= ~0x7f;
2456 for (num = 0; scratch; num++) {
2457 u32 t;
2458
2459 /* do this endpoint's FIFO and queue need tending? */
2460 t = 1 << num;
2461 if ((scratch & t) == 0)
2462 continue;
2463 scratch ^= t;
2464
2465 ep = &dev->ep [num];
2466 handle_ep_small (ep);
2467 }
2468
2469 if (stat)
2470 DEBUG (dev, "unhandled irqstat0 %08x\n", stat);
2471 }
2472
2473 #define DMA_INTERRUPTS ( \
2474 (1 << DMA_D_INTERRUPT) \
2475 | (1 << DMA_C_INTERRUPT) \
2476 | (1 << DMA_B_INTERRUPT) \
2477 | (1 << DMA_A_INTERRUPT))
2478 #define PCI_ERROR_INTERRUPTS ( \
2479 (1 << PCI_MASTER_ABORT_RECEIVED_INTERRUPT) \
2480 | (1 << PCI_TARGET_ABORT_RECEIVED_INTERRUPT) \
2481 | (1 << PCI_RETRY_ABORT_INTERRUPT))
2482
handle_stat1_irqs(struct net2280 * dev,u32 stat)2483 static void handle_stat1_irqs (struct net2280 *dev, u32 stat)
2484 {
2485 struct net2280_ep *ep;
2486 u32 tmp, num, scratch;
2487
2488 /* after disconnect there's nothing else to do! */
2489 tmp = (1 << VBUS_INTERRUPT) | (1 << ROOT_PORT_RESET_INTERRUPT);
2490 if (stat & tmp) {
2491 writel (tmp, &dev->regs->irqstat1);
2492 if (((stat & (1 << ROOT_PORT_RESET_INTERRUPT)) != 0
2493 || (readl (&dev->usb->usbctl) & (1 << VBUS_PIN)) == 0
2494 ) && dev->gadget.speed != USB_SPEED_UNKNOWN) {
2495 DEBUG (dev, "disconnect %s\n",
2496 dev->driver->driver.name);
2497 stop_activity (dev, dev->driver);
2498 ep0_start (dev);
2499 return;
2500 }
2501 stat &= ~tmp;
2502
2503 /* vBUS can bounce ... one of many reasons to ignore the
2504 * notion of hotplug events on bus connect/disconnect!
2505 */
2506 if (!stat)
2507 return;
2508 }
2509
2510 /* NOTE: we don't actually suspend the hardware; that starts to
2511 * interact with PCI power management, and needs something like a
2512 * controller->suspend() call to clear SUSPEND_REQUEST_INTERRUPT.
2513 * we shouldn't see resume interrupts.
2514 * for rev 0100, this also avoids erratum 0102.
2515 */
2516 tmp = (1 << SUSPEND_REQUEST_CHANGE_INTERRUPT);
2517 if (stat & tmp) {
2518 if (dev->driver->suspend)
2519 dev->driver->suspend (&dev->gadget);
2520 stat &= ~tmp;
2521 }
2522 stat &= ~(1 << SUSPEND_REQUEST_INTERRUPT);
2523
2524 /* clear any other status/irqs */
2525 if (stat)
2526 writel (stat, &dev->regs->irqstat1);
2527
2528 /* some status we can just ignore */
2529 stat &= ~((1 << CONTROL_STATUS_INTERRUPT)
2530 | (1 << RESUME_INTERRUPT)
2531 | (1 << SOF_INTERRUPT));
2532 if (!stat)
2533 return;
2534 // DEBUG (dev, "irqstat1 %08x\n", stat);
2535
2536 /* DMA status, for ep-{a,b,c,d} */
2537 scratch = stat & DMA_INTERRUPTS;
2538 stat &= ~DMA_INTERRUPTS;
2539 scratch >>= 9;
2540 for (num = 0; scratch; num++) {
2541 struct net2280_dma_regs *dma;
2542
2543 tmp = 1 << num;
2544 if ((tmp & scratch) == 0)
2545 continue;
2546 scratch ^= tmp;
2547
2548 ep = &dev->ep [num + 1];
2549 dma = ep->dma;
2550
2551 if (!dma)
2552 continue;
2553
2554 /* clear ep's dma status */
2555 tmp = readl (&dma->dmastat);
2556 writel (tmp, &dma->dmastat);
2557
2558 /* chaining should stop on abort, short OUT from fifo,
2559 * or (stat0 codepath) short OUT transfer.
2560 */
2561 if (!use_dma_chaining) {
2562 if ((tmp & (1 << DMA_TRANSACTION_DONE_INTERRUPT))
2563 == 0) {
2564 DEBUG (ep->dev, "%s no xact done? %08x\n",
2565 ep->ep.name, tmp);
2566 continue;
2567 }
2568 stop_dma (ep->dma);
2569 }
2570
2571 /* OUT transfers terminate when the data from the
2572 * host is in our memory. Process whatever's done.
2573 * On this path, we know transfer's last packet wasn't
2574 * less than req->length. NAK_OUT_PACKETS may be set,
2575 * or the FIFO may already be holding new packets.
2576 *
2577 * IN transfers can linger in the FIFO for a very
2578 * long time ... we ignore that for now, accounting
2579 * precisely (like PIO does) needs per-packet irqs
2580 */
2581 scan_dma_completions (ep);
2582
2583 /* disable dma on inactive queues; else maybe restart */
2584 if (list_empty (&ep->queue)) {
2585 if (use_dma_chaining)
2586 stop_dma (ep->dma);
2587 } else {
2588 tmp = readl (&dma->dmactl);
2589 if (!use_dma_chaining
2590 || (tmp & (1 << DMA_ENABLE)) == 0)
2591 restart_dma (ep);
2592 else if (ep->is_in && use_dma_chaining) {
2593 struct net2280_request *req;
2594 u32 dmacount;
2595
2596 /* the descriptor at the head of the chain
2597 * may still have VALID_BIT clear; that's
2598 * used to trigger changing DMA_FIFO_VALIDATE
2599 * (affects automagic zlp writes).
2600 */
2601 req = list_entry (ep->queue.next,
2602 struct net2280_request, queue);
2603 dmacount = req->td->dmacount;
2604 dmacount &= __constant_cpu_to_le32 (
2605 (1 << VALID_BIT)
2606 | DMA_BYTE_COUNT_MASK);
2607 if (dmacount && (dmacount & valid_bit) == 0)
2608 restart_dma (ep);
2609 }
2610 }
2611 ep->irqs++;
2612 }
2613
2614 /* NOTE: there are other PCI errors we might usefully notice.
2615 * if they appear very often, here's where to try recovering.
2616 */
2617 if (stat & PCI_ERROR_INTERRUPTS) {
2618 ERROR (dev, "pci dma error; stat %08x\n", stat);
2619 stat &= ~PCI_ERROR_INTERRUPTS;
2620 /* these are fatal errors, but "maybe" they won't
2621 * happen again ...
2622 */
2623 stop_activity (dev, dev->driver);
2624 ep0_start (dev);
2625 stat = 0;
2626 }
2627
2628 if (stat)
2629 DEBUG (dev, "unhandled irqstat1 %08x\n", stat);
2630 }
2631
net2280_irq(int irq,void * _dev,struct pt_regs * r)2632 static irqreturn_t net2280_irq (int irq, void *_dev, struct pt_regs * r)
2633 {
2634 struct net2280 *dev = _dev;
2635
2636 spin_lock (&dev->lock);
2637
2638 /* handle disconnect, dma, and more */
2639 handle_stat1_irqs (dev, readl (&dev->regs->irqstat1));
2640
2641 /* control requests and PIO */
2642 handle_stat0_irqs (dev, readl (&dev->regs->irqstat0));
2643
2644 spin_unlock (&dev->lock);
2645
2646 return IRQ_HANDLED;
2647 }
2648
2649 /*-------------------------------------------------------------------------*/
2650
2651 /* tear down the binding between this driver and the pci device */
2652
net2280_remove(struct pci_dev * pdev)2653 static void net2280_remove (struct pci_dev *pdev)
2654 {
2655 struct net2280 *dev = pci_get_drvdata (pdev);
2656
2657 /* start with the driver above us */
2658 if (dev->driver) {
2659 /* should have been done already by driver model core */
2660 WARN (dev, "pci remove, driver '%s' is still registered\n",
2661 dev->driver->driver.name);
2662 usb_gadget_unregister_driver (dev->driver);
2663 }
2664
2665 /* then clean up the resources we allocated during probe() */
2666 net2280_led_shutdown (dev);
2667 if (dev->requests) {
2668 int i;
2669 for (i = 1; i < 5; i++) {
2670 if (!dev->ep [i].dummy)
2671 continue;
2672 pci_pool_free (dev->requests, dev->ep [i].dummy,
2673 dev->ep [i].td_dma);
2674 }
2675 pci_pool_destroy (dev->requests);
2676 }
2677 if (dev->got_irq)
2678 free_irq (pdev->irq, dev);
2679 if (dev->regs)
2680 iounmap (dev->regs);
2681 if (dev->region)
2682 release_mem_region (pci_resource_start (pdev, 0),
2683 pci_resource_len (pdev, 0));
2684 if (dev->enabled)
2685 pci_disable_device (pdev);
2686 pci_set_drvdata (pdev, 0);
2687
2688 INFO (dev, "unbind from pci %s\n", pdev->slot_name);
2689
2690 kfree (dev);
2691 the_controller = 0;
2692 }
2693
2694 /* wrap this driver around the specified device, but
2695 * don't respond over USB until a gadget driver binds to us.
2696 */
2697
net2280_probe(struct pci_dev * pdev,const struct pci_device_id * id)2698 static int net2280_probe (struct pci_dev *pdev, const struct pci_device_id *id)
2699 {
2700 struct net2280 *dev;
2701 unsigned long resource, len;
2702 void *base = 0;
2703 int retval, i;
2704 char buf [8], *bufp;
2705
2706 /* if you want to support more than one controller in a system,
2707 * usb_gadget_driver_{register,unregister}() must change.
2708 */
2709 if (the_controller) {
2710 WARN (the_controller, "ignoring %s\n", pdev->slot_name);
2711 return -EBUSY;
2712 }
2713
2714 /* alloc, and start init */
2715 dev = kmalloc (sizeof *dev, SLAB_KERNEL);
2716 if (dev == NULL){
2717 retval = -ENOMEM;
2718 goto done;
2719 }
2720
2721 memset (dev, 0, sizeof *dev);
2722 spin_lock_init (&dev->lock);
2723 dev->pdev = pdev;
2724 dev->gadget.ops = &net2280_ops;
2725 dev->gadget.is_dualspeed = 1;
2726
2727 dev->gadget.dev.bus_id = pdev->slot_name;
2728 dev->gadget.name = driver_name;
2729
2730 /* now all the pci goodies ... */
2731 if (pci_enable_device (pdev) < 0) {
2732 retval = -ENODEV;
2733 goto done;
2734 }
2735 dev->enabled = 1;
2736
2737 /* BAR 0 holds all the registers
2738 * BAR 1 is 8051 memory; unused here (note erratum 0103)
2739 * BAR 2 is fifo memory; unused here
2740 */
2741 resource = pci_resource_start (pdev, 0);
2742 len = pci_resource_len (pdev, 0);
2743 if (!request_mem_region (resource, len, driver_name)) {
2744 DEBUG (dev, "controller already in use\n");
2745 retval = -EBUSY;
2746 goto done;
2747 }
2748 dev->region = 1;
2749
2750 base = ioremap_nocache (resource, len);
2751 if (base == NULL) {
2752 DEBUG (dev, "can't map memory\n");
2753 retval = -EFAULT;
2754 goto done;
2755 }
2756 dev->regs = (struct net2280_regs *) base;
2757 dev->usb = (struct net2280_usb_regs *) (base + 0x0080);
2758 dev->pci = (struct net2280_pci_regs *) (base + 0x0100);
2759 dev->dma = (struct net2280_dma_regs *) (base + 0x0180);
2760 dev->dep = (struct net2280_dep_regs *) (base + 0x0200);
2761 dev->epregs = (struct net2280_ep_regs *) (base + 0x0300);
2762
2763 /* put into initial config, link up all endpoints */
2764 usb_reset (dev);
2765 usb_reinit (dev);
2766
2767 /* irq setup after old hardware is cleaned up */
2768 if (!pdev->irq) {
2769 ERROR (dev, "No IRQ. Check PCI setup!\n");
2770 retval = -ENODEV;
2771 goto done;
2772 }
2773 #ifndef __sparc__
2774 snprintf (buf, sizeof buf, "%d", pdev->irq);
2775 bufp = buf;
2776 #else
2777 bufp = __irq_itoa(pdev->irq);
2778 #endif
2779 if (request_irq (pdev->irq, net2280_irq, SA_SHIRQ, driver_name, dev)
2780 != 0) {
2781 ERROR (dev, "request interrupt %s failed\n", bufp);
2782 retval = -EBUSY;
2783 goto done;
2784 }
2785 dev->got_irq = 1;
2786
2787 /* DMA setup */
2788 dev->requests = pci_pool_create ("requests", pdev,
2789 sizeof (struct net2280_dma),
2790 0 /* no alignment requirements */,
2791 0 /* or page-crossing issues */,
2792 SLAB_KERNEL /* 2.4 only */ );
2793 if (!dev->requests) {
2794 DEBUG (dev, "can't get request pool\n");
2795 retval = -ENOMEM;
2796 goto done;
2797 }
2798 for (i = 1; i < 5; i++) {
2799 struct net2280_dma *td;
2800
2801 td = pci_pool_alloc (dev->requests, GFP_KERNEL,
2802 &dev->ep [i].td_dma);
2803 if (!td) {
2804 DEBUG (dev, "can't get dummy %d\n", i);
2805 retval = -ENOMEM;
2806 goto done;
2807 }
2808 td->dmacount = 0; /* not VALID */
2809 td->dmaaddr = __constant_cpu_to_le32 (DMA_ADDR_INVALID);
2810 td->dmadesc = td->dmaaddr;
2811 dev->ep [i].dummy = td;
2812 }
2813
2814 /* enable lower-overhead pci memory bursts during DMA */
2815 writel ( (1 << DMA_MEMORY_WRITE_AND_INVALIDATE_ENABLE)
2816 // 256 write retries may not be enough...
2817 // | (1 << PCI_RETRY_ABORT_ENABLE)
2818 | (1 << DMA_READ_MULTIPLE_ENABLE)
2819 | (1 << DMA_READ_LINE_ENABLE)
2820 , &dev->pci->pcimstctl);
2821 /* erratum 0115 shouldn't appear: Linux inits PCI_LATENCY_TIMER */
2822 pci_set_master (pdev);
2823 pci_set_mwi (pdev);
2824
2825 /* ... also flushes any posted pci writes */
2826 dev->chiprev = get_idx_reg (dev->regs, REG_CHIPREV) & 0xffff;
2827
2828 /* done */
2829 pci_set_drvdata (pdev, dev);
2830 INFO (dev, "%s\n", driver_desc);
2831 INFO (dev, "irq %s, pci mem %p, chip rev %04x\n",
2832 bufp, base, dev->chiprev);
2833 INFO (dev, "version: " DRIVER_VERSION "; dma %s\n",
2834 use_dma
2835 ? (use_dma_chaining ? "chaining" : "enabled")
2836 : "disabled");
2837 the_controller = dev;
2838
2839 return 0;
2840
2841 done:
2842 if (dev)
2843 net2280_remove (pdev);
2844 return retval;
2845 }
2846
2847
2848 /*-------------------------------------------------------------------------*/
2849
2850 static struct pci_device_id pci_ids [] = { {
2851 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
2852 .class_mask = ~0,
2853 .vendor = 0x17cc,
2854 .device = 0x2280,
2855 .subvendor = PCI_ANY_ID,
2856 .subdevice = PCI_ANY_ID,
2857
2858 }, { /* end: all zeroes */ }
2859 };
2860 MODULE_DEVICE_TABLE (pci, pci_ids);
2861
2862 /* pci driver glue; this is a "new style" PCI driver module */
2863 static struct pci_driver net2280_pci_driver = {
2864 .name = (char *) driver_name,
2865 .id_table = pci_ids,
2866
2867 .probe = net2280_probe,
2868 .remove = net2280_remove,
2869
2870 /* FIXME add power management support */
2871 };
2872
2873 MODULE_DESCRIPTION (DRIVER_DESC);
2874 MODULE_AUTHOR ("David Brownell");
2875 MODULE_LICENSE ("GPL");
2876
init(void)2877 static int __init init (void)
2878 {
2879 if (!use_dma)
2880 use_dma_chaining = 0;
2881 return pci_module_init (&net2280_pci_driver);
2882 }
2883 module_init (init);
2884
cleanup(void)2885 static void __exit cleanup (void)
2886 {
2887 pci_unregister_driver (&net2280_pci_driver);
2888 }
2889 module_exit (cleanup);
2890