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