net2280.c 99.4 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4
/*
 * Driver for the PLX NET2280 USB device controller.
 * Specs and errata are available from <http://www.plxtech.com>.
 *
5
 * PLX Technology Inc. (formerly NetChip Technology) supported the
L
Linus Torvalds 已提交
6 7 8 9 10 11
 * development of this driver.
 *
 *
 * CODE STATUS HIGHLIGHTS
 *
 * This driver should work well with most "gadget" drivers, including
12
 * the Mass Storage, Serial, and Ethernet/RNDIS gadget drivers
L
Linus Torvalds 已提交
13 14 15 16 17 18 19 20
 * as well as Gadget Zero and Gadgetfs.
 *
 * DMA is enabled by default.  Drivers using transfer queues might use
 * DMA chaining to remove IRQ latencies between transfers.  (Except when
 * short OUT transfers happen.)  Drivers can use the req->no_interrupt
 * hint to completely eliminate some IRQs, if a later IRQ is guaranteed
 * and DMA chaining is enabled.
 *
21 22 23
 * MSI is enabled by default.  The legacy IRQ is used if MSI couldn't
 * be enabled.
 *
L
Linus Torvalds 已提交
24 25 26 27 28 29 30
 * Note that almost all the errata workarounds here are only needed for
 * rev1 chips.  Rev1a silicon (0110) fixes almost all of them.
 */

/*
 * Copyright (C) 2003 David Brownell
 * Copyright (C) 2003-2005 PLX Technology, Inc.
31
 * Copyright (C) 2014 Ricardo Ribalda - Qtechnology/AS
L
Linus Torvalds 已提交
32
 *
33 34
 * Modified Seth Levy 2005 PLX Technology, Inc. to provide compatibility
 *	with 2282 chip
35
 *
36 37 38
 * Modified Ricardo Ribalda Qtechnology AS  to provide compatibility
 *	with usb 338x chip. Based on PLX driver
 *
L
Linus Torvalds 已提交
39 40 41 42 43 44 45 46
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

#include <linux/module.h>
#include <linux/pci.h>
47
#include <linux/dma-mapping.h>
L
Linus Torvalds 已提交
48 49 50 51 52 53 54 55 56 57 58
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ioport.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/timer.h>
#include <linux/list.h>
#include <linux/interrupt.h>
#include <linux/moduleparam.h>
#include <linux/device.h>
59
#include <linux/usb/ch9.h>
60
#include <linux/usb/gadget.h>
61
#include <linux/prefetch.h>
62
#include <linux/io.h>
L
Linus Torvalds 已提交
63 64 65 66 67

#include <asm/byteorder.h>
#include <asm/irq.h>
#include <asm/unaligned.h>

68 69
#define	DRIVER_DESC		"PLX NET228x/USB338x USB Peripheral Controller"
#define	DRIVER_VERSION		"2005 Sept 27/v3.0"
L
Linus Torvalds 已提交
70 71 72 73 74 75

#define	EP_DONTUSE		13	/* nonzero */

#define USE_RDK_LEDS		/* GPIO pins control three LEDs */


76 77
static const char driver_name[] = "net2280";
static const char driver_desc[] = DRIVER_DESC;
L
Linus Torvalds 已提交
78

79
static const u32 ep_bit[9] = { 0, 17, 2, 19, 4, 1, 18, 3, 20 };
80 81
static const char ep0name[] = "ep0";
static const char *const ep_name[] = {
L
Linus Torvalds 已提交
82 83
	ep0name,
	"ep-a", "ep-b", "ep-c", "ep-d",
84
	"ep-e", "ep-f", "ep-g", "ep-h",
L
Linus Torvalds 已提交
85 86 87 88 89 90 91 92 93 94
};

/* use_dma -- general goodness, fewer interrupts, less cpu load (vs PIO)
 * use_dma_chaining -- dma descriptor queueing gives even more irq reduction
 *
 * The net2280 DMA engines are not tightly integrated with their FIFOs;
 * not all cases are (yet) handled well in this driver or the silicon.
 * Some gadget drivers work better with the dma support here than others.
 * These two parameters let you use PIO or more aggressive DMA.
 */
95 96 97
static bool use_dma = true;
static bool use_dma_chaining;
static bool use_msi = true;
L
Linus Torvalds 已提交
98 99

/* "modprobe net2280 use_dma=n" etc */
100 101 102
module_param(use_dma, bool, 0444);
module_param(use_dma_chaining, bool, 0444);
module_param(use_msi, bool, 0444);
L
Linus Torvalds 已提交
103 104 105 106 107

/* mode 0 == ep-{a,b,c,d} 1K fifo each
 * mode 1 == ep-{a,b} 2K fifo each, ep-{c,d} unavailable
 * mode 2 == ep-a 2K fifo, ep-{b,c} 1K each, ep-d unavailable
 */
108
static ushort fifo_mode;
L
Linus Torvalds 已提交
109 110

/* "modprobe net2280 fifo_mode=1" etc */
111
module_param(fifo_mode, ushort, 0644);
L
Linus Torvalds 已提交
112 113 114

/* enable_suspend -- When enabled, the driver will respond to
 * USB suspend requests by powering down the NET2280.  Otherwise,
L
Lucas De Marchi 已提交
115
 * USB suspend requests will be ignored.  This is acceptable for
116
 * self-powered devices
L
Linus Torvalds 已提交
117
 */
118
static bool enable_suspend;
L
Linus Torvalds 已提交
119 120

/* "modprobe net2280 enable_suspend=1" etc */
121
module_param(enable_suspend, bool, 0444);
L
Linus Torvalds 已提交
122

123 124 125 126
/* force full-speed operation */
static bool full_speed;
module_param(full_speed, bool, 0444);
MODULE_PARM_DESC(full_speed, "force full-speed mode -- for testing only!");
L
Linus Torvalds 已提交
127 128 129

#define	DIR_STRING(bAddress) (((bAddress) & USB_DIR_IN) ? "in" : "out")

130
static char *type_string(u8 bmAttributes)
L
Linus Torvalds 已提交
131 132 133 134 135
{
	switch ((bmAttributes) & USB_ENDPOINT_XFERTYPE_MASK) {
	case USB_ENDPOINT_XFER_BULK:	return "bulk";
	case USB_ENDPOINT_XFER_ISOC:	return "iso";
	case USB_ENDPOINT_XFER_INT:	return "intr";
J
Joe Perches 已提交
136
	}
L
Linus Torvalds 已提交
137 138 139 140 141
	return "control";
}

#include "net2280.h"

142 143
#define valid_bit	cpu_to_le32(BIT(VALID_BIT))
#define dma_done_ie	cpu_to_le32(BIT(DMA_DONE_INTERRUPT_ENABLE))
L
Linus Torvalds 已提交
144 145

/*-------------------------------------------------------------------------*/
146 147 148 149
static inline void enable_pciirqenb(struct net2280_ep *ep)
{
	u32 tmp = readl(&ep->dev->regs->pciirqenb0);

150
	if (ep->dev->pdev->vendor == PCI_VENDOR_ID_PLX_LEGACY)
151
		tmp |= BIT(ep->num);
152
	else
153
		tmp |= BIT(ep_bit[ep->num]);
154 155 156 157
	writel(tmp, &ep->dev->regs->pciirqenb0);

	return;
}
L
Linus Torvalds 已提交
158 159

static int
160
net2280_enable(struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
L
Linus Torvalds 已提交
161 162 163 164 165
{
	struct net2280		*dev;
	struct net2280_ep	*ep;
	u32			max, tmp;
	unsigned long		flags;
166
	static const u32 ep_key[9] = { 1, 0, 1, 0, 1, 1, 0, 1, 0 };
L
Linus Torvalds 已提交
167

168
	ep = container_of(_ep, struct net2280_ep, ep);
169 170
	if (!_ep || !desc || ep->desc || _ep->name == ep0name ||
			desc->bDescriptorType != USB_DT_ENDPOINT)
L
Linus Torvalds 已提交
171 172 173 174 175 176 177 178 179
		return -EINVAL;
	dev = ep->dev;
	if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
		return -ESHUTDOWN;

	/* erratum 0119 workaround ties up an endpoint number */
	if ((desc->bEndpointAddress & 0x0f) == EP_DONTUSE)
		return -EDOM;

180
	if (dev->pdev->vendor == PCI_VENDOR_ID_PLX) {
181 182 183 184 185 186 187
		if ((desc->bEndpointAddress & 0x0f) >= 0x0c)
			return -EDOM;
		ep->is_in = !!usb_endpoint_dir_in(desc);
		if (dev->enhanced_mode && ep->is_in && ep_key[ep->num])
			return -EINVAL;
	}

L
Linus Torvalds 已提交
188
	/* sanity check ep-e/ep-f since their fifos are small */
189
	max = usb_endpoint_maxp(desc) & 0x1fff;
190 191
	if (ep->num > 4 && max > 64 &&
			(dev->pdev->vendor == PCI_VENDOR_ID_PLX_LEGACY))
L
Linus Torvalds 已提交
192 193
		return -ERANGE;

194
	spin_lock_irqsave(&dev->lock, flags);
L
Linus Torvalds 已提交
195 196 197 198 199
	_ep->maxpacket = max & 0x7ff;
	ep->desc = desc;

	/* ep_reset() has already been called */
	ep->stopped = 0;
200
	ep->wedged = 0;
L
Linus Torvalds 已提交
201 202 203
	ep->out_overflow = 0;

	/* set speed-dependent max packet; may kick in high bandwidth */
204
	set_max_speed(ep, max);
L
Linus Torvalds 已提交
205 206 207 208 209

	/* FIFO lines can't go to different packets.  PIO is ok, so
	 * use it instead of troublesome (non-bulk) multi-packet DMA.
	 */
	if (ep->dma && (max % 4) != 0 && use_dma_chaining) {
210
		ep_dbg(ep->dev, "%s, no dma for maxpacket %d\n",
L
Linus Torvalds 已提交
211 212 213 214 215
			ep->ep.name, ep->ep.maxpacket);
		ep->dma = NULL;
	}

	/* set type, direction, address; reset fifo counters */
216
	writel(BIT(FIFO_FLUSH), &ep->regs->ep_stat);
L
Linus Torvalds 已提交
217 218 219
	tmp = (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK);
	if (tmp == USB_ENDPOINT_XFER_INT) {
		/* erratum 0105 workaround prevents hs NYET */
220 221 222
		if (dev->chiprev == 0100 &&
				dev->gadget.speed == USB_SPEED_HIGH &&
				!(desc->bEndpointAddress & USB_DIR_IN))
223
			writel(BIT(CLEAR_NAK_OUT_PACKETS_MODE),
L
Linus Torvalds 已提交
224 225 226
				&ep->regs->ep_rsp);
	} else if (tmp == USB_ENDPOINT_XFER_BULK) {
		/* catch some particularly blatant driver bugs */
227 228 229 230
		if ((dev->gadget.speed == USB_SPEED_SUPER && max != 1024) ||
		    (dev->gadget.speed == USB_SPEED_HIGH && max != 512) ||
		    (dev->gadget.speed == USB_SPEED_FULL && max > 64)) {
			spin_unlock_irqrestore(&dev->lock, flags);
L
Linus Torvalds 已提交
231 232 233
			return -ERANGE;
		}
	}
234
	ep->is_iso = (tmp == USB_ENDPOINT_XFER_ISOC);
235
	/* Enable this endpoint */
236
	if (dev->pdev->vendor == PCI_VENDOR_ID_PLX_LEGACY) {
237 238 239 240
		tmp <<= ENDPOINT_TYPE;
		tmp |= desc->bEndpointAddress;
		/* default full fifo lines */
		tmp |= (4 << ENDPOINT_BYTE_COUNT);
241
		tmp |= BIT(ENDPOINT_ENABLE);
242 243 244 245 246
		ep->is_in = (tmp & USB_DIR_IN) != 0;
	} else {
		/* In Legacy mode, only OUT endpoints are used */
		if (dev->enhanced_mode && ep->is_in) {
			tmp <<= IN_ENDPOINT_TYPE;
247
			tmp |= BIT(IN_ENDPOINT_ENABLE);
248
			/* Not applicable to Legacy */
249
			tmp |= BIT(ENDPOINT_DIRECTION);
250 251
		} else {
			tmp <<= OUT_ENDPOINT_TYPE;
252
			tmp |= BIT(OUT_ENDPOINT_ENABLE);
253 254 255 256 257 258 259 260 261
			tmp |= (ep->is_in << ENDPOINT_DIRECTION);
		}

		tmp |= usb_endpoint_num(desc);
		tmp |= (ep->ep.maxburst << MAX_BURST_SIZE);
	}

	/* Make sure all the registers are written before ep_rsp*/
	wmb();
L
Linus Torvalds 已提交
262 263 264

	/* for OUT transfers, block the rx fifo until a read is posted */
	if (!ep->is_in)
265
		writel(BIT(SET_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
266
	else if (dev->pdev->device != 0x2280) {
267 268 269
		/* Added for 2282, Don't use nak packets on an in endpoint,
		 * this was ignored on 2280
		 */
270 271
		writel(BIT(CLEAR_NAK_OUT_PACKETS) |
			BIT(CLEAR_NAK_OUT_PACKETS_MODE), &ep->regs->ep_rsp);
272
	}
L
Linus Torvalds 已提交
273

274
	writel(tmp, &ep->cfg->ep_cfg);
L
Linus Torvalds 已提交
275 276 277

	/* enable irqs */
	if (!ep->dma) {				/* pio, per-packet */
278
		enable_pciirqenb(ep);
L
Linus Torvalds 已提交
279

280 281
		tmp = BIT(DATA_PACKET_RECEIVED_INTERRUPT_ENABLE) |
			BIT(DATA_PACKET_TRANSMITTED_INTERRUPT_ENABLE);
282
		if (dev->pdev->device == 0x2280)
283 284
			tmp |= readl(&ep->regs->ep_irqenb);
		writel(tmp, &ep->regs->ep_irqenb);
L
Linus Torvalds 已提交
285
	} else {				/* dma, per-request */
286
		tmp = BIT((8 + ep->num));	/* completion */
287 288
		tmp |= readl(&dev->regs->pciirqenb1);
		writel(tmp, &dev->regs->pciirqenb1);
L
Linus Torvalds 已提交
289 290 291 292 293 294

		/* for short OUT transfers, dma completions can't
		 * advance the queue; do it pio-style, by hand.
		 * NOTE erratum 0112 workaround #2
		 */
		if ((desc->bEndpointAddress & USB_DIR_IN) == 0) {
295
			tmp = BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT_ENABLE);
296
			writel(tmp, &ep->regs->ep_irqenb);
L
Linus Torvalds 已提交
297

298
			enable_pciirqenb(ep);
L
Linus Torvalds 已提交
299 300 301 302
		}
	}

	tmp = desc->bEndpointAddress;
303
	ep_dbg(dev, "enabled %s (ep%d%s-%s) %s max %04x\n",
304 305
		_ep->name, tmp & 0x0f, DIR_STRING(tmp),
		type_string(desc->bmAttributes),
L
Linus Torvalds 已提交
306 307 308
		ep->dma ? "dma" : "pio", max);

	/* pci writes may still be posted */
309
	spin_unlock_irqrestore(&dev->lock, flags);
L
Linus Torvalds 已提交
310 311 312
	return 0;
}

313
static int handshake(u32 __iomem *ptr, u32 mask, u32 done, int usec)
L
Linus Torvalds 已提交
314 315 316 317
{
	u32	result;

	do {
318
		result = readl(ptr);
L
Linus Torvalds 已提交
319 320 321 322 323
		if (result == ~(u32)0)		/* "device unplugged" */
			return -ENODEV;
		result &= mask;
		if (result == done)
			return 0;
324
		udelay(1);
L
Linus Torvalds 已提交
325 326 327 328 329
		usec--;
	} while (usec > 0);
	return -ETIMEDOUT;
}

330
static const struct usb_ep_ops net2280_ep_ops;
L
Linus Torvalds 已提交
331

332 333
static void ep_reset_228x(struct net2280_regs __iomem *regs,
			  struct net2280_ep *ep)
L
Linus Torvalds 已提交
334 335 336 337
{
	u32		tmp;

	ep->desc = NULL;
338
	INIT_LIST_HEAD(&ep->queue);
L
Linus Torvalds 已提交
339

340
	usb_ep_set_maxpacket_limit(&ep->ep, ~0);
L
Linus Torvalds 已提交
341 342 343 344
	ep->ep.ops = &net2280_ep_ops;

	/* disable the dma, irqs, endpoint... */
	if (ep->dma) {
345
		writel(0, &ep->dma->dmactl);
346 347 348 349
		writel(BIT(DMA_SCATTER_GATHER_DONE_INTERRUPT) |
			BIT(DMA_TRANSACTION_DONE_INTERRUPT) |
			BIT(DMA_ABORT),
			&ep->dma->dmastat);
L
Linus Torvalds 已提交
350

351
		tmp = readl(&regs->pciirqenb0);
352
		tmp &= ~BIT(ep->num);
353
		writel(tmp, &regs->pciirqenb0);
L
Linus Torvalds 已提交
354
	} else {
355
		tmp = readl(&regs->pciirqenb1);
356
		tmp &= ~BIT((8 + ep->num));	/* completion */
357
		writel(tmp, &regs->pciirqenb1);
L
Linus Torvalds 已提交
358
	}
359
	writel(0, &ep->regs->ep_irqenb);
L
Linus Torvalds 已提交
360 361 362 363

	/* init to our chosen defaults, notably so that we NAK OUT
	 * packets until the driver queues a read (+note erratum 0112)
	 */
364
	if (!ep->is_in || ep->dev->pdev->device == 0x2280) {
365 366 367 368
		tmp = BIT(SET_NAK_OUT_PACKETS_MODE) |
		BIT(SET_NAK_OUT_PACKETS) |
		BIT(CLEAR_EP_HIDE_STATUS_PHASE) |
		BIT(CLEAR_INTERRUPT_MODE);
369 370
	} else {
		/* added for 2282 */
371 372 373 374
		tmp = BIT(CLEAR_NAK_OUT_PACKETS_MODE) |
		BIT(CLEAR_NAK_OUT_PACKETS) |
		BIT(CLEAR_EP_HIDE_STATUS_PHASE) |
		BIT(CLEAR_INTERRUPT_MODE);
375
	}
L
Linus Torvalds 已提交
376 377

	if (ep->num != 0) {
378 379
		tmp |= BIT(CLEAR_ENDPOINT_TOGGLE) |
			BIT(CLEAR_ENDPOINT_HALT);
L
Linus Torvalds 已提交
380
	}
381
	writel(tmp, &ep->regs->ep_rsp);
L
Linus Torvalds 已提交
382 383

	/* scrub most status bits, and flush any fifo state */
384
	if (ep->dev->pdev->device == 0x2280)
385 386
		tmp = BIT(FIFO_OVERFLOW) |
			BIT(FIFO_UNDERFLOW);
387 388 389
	else
		tmp = 0;

390 391 392 393 394 395 396 397 398 399 400 401
	writel(tmp | BIT(TIMEOUT) |
		BIT(USB_STALL_SENT) |
		BIT(USB_IN_NAK_SENT) |
		BIT(USB_IN_ACK_RCVD) |
		BIT(USB_OUT_PING_NAK_SENT) |
		BIT(USB_OUT_ACK_SENT) |
		BIT(FIFO_FLUSH) |
		BIT(SHORT_PACKET_OUT_DONE_INTERRUPT) |
		BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT) |
		BIT(DATA_PACKET_RECEIVED_INTERRUPT) |
		BIT(DATA_PACKET_TRANSMITTED_INTERRUPT) |
		BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
402 403
		BIT(DATA_IN_TOKEN_INTERRUPT),
		&ep->regs->ep_stat);
L
Linus Torvalds 已提交
404 405 406 407

	/* fifo size is handled separately */
}

408 409 410 411 412 413 414 415 416 417 418 419 420 421
static void ep_reset_338x(struct net2280_regs __iomem *regs,
					struct net2280_ep *ep)
{
	u32 tmp, dmastat;

	ep->desc = NULL;
	INIT_LIST_HEAD(&ep->queue);

	usb_ep_set_maxpacket_limit(&ep->ep, ~0);
	ep->ep.ops = &net2280_ep_ops;

	/* disable the dma, irqs, endpoint... */
	if (ep->dma) {
		writel(0, &ep->dma->dmactl);
422 423 424
		writel(BIT(DMA_ABORT_DONE_INTERRUPT) |
		       BIT(DMA_PAUSE_DONE_INTERRUPT) |
		       BIT(DMA_SCATTER_GATHER_DONE_INTERRUPT) |
425 426 427
		       BIT(DMA_TRANSACTION_DONE_INTERRUPT),
		       /* | BIT(DMA_ABORT), */
		       &ep->dma->dmastat);
428 429 430

		dmastat = readl(&ep->dma->dmastat);
		if (dmastat == 0x5002) {
431
			ep_warn(ep->dev, "The dmastat return = %x!!\n",
432 433 434 435 436
			       dmastat);
			writel(0x5a, &ep->dma->dmastat);
		}

		tmp = readl(&regs->pciirqenb0);
437
		tmp &= ~BIT(ep_bit[ep->num]);
438 439 440 441
		writel(tmp, &regs->pciirqenb0);
	} else {
		if (ep->num < 5) {
			tmp = readl(&regs->pciirqenb1);
442
			tmp &= ~BIT((8 + ep->num));	/* completion */
443 444 445 446 447
			writel(tmp, &regs->pciirqenb1);
		}
	}
	writel(0, &ep->regs->ep_irqenb);

448 449 450 451 452 453 454
	writel(BIT(SHORT_PACKET_OUT_DONE_INTERRUPT) |
	       BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT) |
	       BIT(FIFO_OVERFLOW) |
	       BIT(DATA_PACKET_RECEIVED_INTERRUPT) |
	       BIT(DATA_PACKET_TRANSMITTED_INTERRUPT) |
	       BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
	       BIT(DATA_IN_TOKEN_INTERRUPT), &ep->regs->ep_stat);
455 456
}

457
static void nuke(struct net2280_ep *);
L
Linus Torvalds 已提交
458

459
static int net2280_disable(struct usb_ep *_ep)
L
Linus Torvalds 已提交
460 461 462 463
{
	struct net2280_ep	*ep;
	unsigned long		flags;

464
	ep = container_of(_ep, struct net2280_ep, ep);
L
Linus Torvalds 已提交
465 466 467
	if (!_ep || !ep->desc || _ep->name == ep0name)
		return -EINVAL;

468 469
	spin_lock_irqsave(&ep->dev->lock, flags);
	nuke(ep);
470

471
	if (ep->dev->pdev->vendor == PCI_VENDOR_ID_PLX)
472 473 474
		ep_reset_338x(ep->dev->regs, ep);
	else
		ep_reset_228x(ep->dev->regs, ep);
L
Linus Torvalds 已提交
475

476
	ep_vdbg(ep->dev, "disabled %s %s\n",
L
Linus Torvalds 已提交
477 478 479
			ep->dma ? "dma" : "pio", _ep->name);

	/* synch memory views with the device */
480
	(void)readl(&ep->cfg->ep_cfg);
L
Linus Torvalds 已提交
481 482

	if (use_dma && !ep->dma && ep->num >= 1 && ep->num <= 4)
483
		ep->dma = &ep->dev->dma[ep->num - 1];
L
Linus Torvalds 已提交
484

485
	spin_unlock_irqrestore(&ep->dev->lock, flags);
L
Linus Torvalds 已提交
486 487 488 489 490
	return 0;
}

/*-------------------------------------------------------------------------*/

491 492
static struct usb_request
*net2280_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
L
Linus Torvalds 已提交
493 494 495 496 497 498
{
	struct net2280_ep	*ep;
	struct net2280_request	*req;

	if (!_ep)
		return NULL;
499
	ep = container_of(_ep, struct net2280_ep, ep);
L
Linus Torvalds 已提交
500

501
	req = kzalloc(sizeof(*req), gfp_flags);
L
Linus Torvalds 已提交
502 503 504
	if (!req)
		return NULL;

505
	INIT_LIST_HEAD(&req->queue);
L
Linus Torvalds 已提交
506 507 508 509 510

	/* this dma descriptor may be swapped with the previous dummy */
	if (ep->dma) {
		struct net2280_dma	*td;

511
		td = pci_pool_alloc(ep->dev->requests, gfp_flags,
L
Linus Torvalds 已提交
512 513
				&req->td_dma);
		if (!td) {
514
			kfree(req);
L
Linus Torvalds 已提交
515 516 517 518 519 520 521 522 523
			return NULL;
		}
		td->dmacount = 0;	/* not VALID */
		td->dmadesc = td->dmaaddr;
		req->td = td;
	}
	return &req->req;
}

524
static void net2280_free_request(struct usb_ep *_ep, struct usb_request *_req)
L
Linus Torvalds 已提交
525 526 527 528
{
	struct net2280_ep	*ep;
	struct net2280_request	*req;

529
	ep = container_of(_ep, struct net2280_ep, ep);
L
Linus Torvalds 已提交
530 531 532
	if (!_ep || !_req)
		return;

533 534
	req = container_of(_req, struct net2280_request, req);
	WARN_ON(!list_empty(&req->queue));
L
Linus Torvalds 已提交
535
	if (req->td)
536 537
		pci_pool_free(ep->dev->requests, req->td, req->td_dma);
	kfree(req);
L
Linus Torvalds 已提交
538 539 540 541 542 543 544 545 546 547 548
}

/*-------------------------------------------------------------------------*/

/* load a packet into the fifo we use for usb IN transfers.
 * works for all endpoints.
 *
 * NOTE: pio with ep-a..ep-d could stuff multiple packets into the fifo
 * at a time, but this code is simpler because it knows it only writes
 * one packet.  ep-a..ep-d should use dma instead.
 */
549
static void write_fifo(struct net2280_ep *ep, struct usb_request *req)
L
Linus Torvalds 已提交
550 551 552 553 554 555 556 557 558 559
{
	struct net2280_ep_regs	__iomem *regs = ep->regs;
	u8			*buf;
	u32			tmp;
	unsigned		count, total;

	/* INVARIANT:  fifo is currently empty. (testable) */

	if (req) {
		buf = req->buf + req->actual;
560
		prefetch(buf);
L
Linus Torvalds 已提交
561 562 563 564 565 566 567 568 569 570 571
		total = req->length - req->actual;
	} else {
		total = 0;
		buf = NULL;
	}

	/* write just one packet at a time */
	count = ep->ep.maxpacket;
	if (count > total)	/* min() cannot be used on a bitfield */
		count = total;

572
	ep_vdbg(ep->dev, "write %s fifo (IN) %d bytes%s req %p\n",
L
Linus Torvalds 已提交
573 574 575 576 577 578 579 580
			ep->ep.name, count,
			(count != ep->ep.maxpacket) ? " (short)" : "",
			req);
	while (count >= 4) {
		/* NOTE be careful if you try to align these. fifo lines
		 * should normally be full (4 bytes) and successive partial
		 * lines are ok only in certain cases.
		 */
581 582 583
		tmp = get_unaligned((u32 *)buf);
		cpu_to_le32s(&tmp);
		writel(tmp, &regs->ep_data);
L
Linus Torvalds 已提交
584 585 586 587 588 589 590 591 592
		buf += 4;
		count -= 4;
	}

	/* last fifo entry is "short" unless we wrote a full packet.
	 * also explicitly validate last word in (periodic) transfers
	 * when maxpacket is not a multiple of 4 bytes.
	 */
	if (count || total < ep->ep.maxpacket) {
593 594 595 596
		tmp = count ? get_unaligned((u32 *)buf) : count;
		cpu_to_le32s(&tmp);
		set_fifo_bytecount(ep, count & 0x03);
		writel(tmp, &regs->ep_data);
L
Linus Torvalds 已提交
597 598 599 600 601 602 603 604 605 606 607 608
	}

	/* pci writes may still be posted */
}

/* work around erratum 0106: PCI and USB race over the OUT fifo.
 * caller guarantees chiprev 0100, out endpoint is NAKing, and
 * there's no real data in the fifo.
 *
 * NOTE:  also used in cases where that erratum doesn't apply:
 * where the host wrote "too much" data to us.
 */
609
static void out_flush(struct net2280_ep *ep)
L
Linus Torvalds 已提交
610 611 612 613
{
	u32	__iomem *statp;
	u32	tmp;

614
	ASSERT_OUT_NAKING(ep);
L
Linus Torvalds 已提交
615 616

	statp = &ep->regs->ep_stat;
617
	writel(BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
618 619
		BIT(DATA_PACKET_RECEIVED_INTERRUPT),
		statp);
620
	writel(BIT(FIFO_FLUSH), statp);
621 622 623
	/* Make sure that stap is written */
	mb();
	tmp = readl(statp);
624
	if (tmp & BIT(DATA_OUT_PING_TOKEN_INTERRUPT) &&
L
Linus Torvalds 已提交
625
			/* high speed did bulk NYET; fifo isn't filling */
626
			ep->dev->gadget.speed == USB_SPEED_FULL) {
L
Linus Torvalds 已提交
627 628 629
		unsigned	usec;

		usec = 50;		/* 64 byte bulk/interrupt */
630 631
		handshake(statp, BIT(USB_OUT_PING_NAK_SENT),
				BIT(USB_OUT_PING_NAK_SENT), usec);
L
Linus Torvalds 已提交
632 633 634 635 636 637 638 639 640 641 642
		/* NAK done; now CLEAR_NAK_OUT_PACKETS is safe */
	}
}

/* unload packet(s) from the fifo we use for usb OUT transfers.
 * returns true iff the request completed, because of short packet
 * or the request buffer having filled with full packets.
 *
 * for ep-a..ep-d this will read multiple packets out when they
 * have been accepted.
 */
643
static int read_fifo(struct net2280_ep *ep, struct net2280_request *req)
L
Linus Torvalds 已提交
644 645 646 647 648 649 650 651 652
{
	struct net2280_ep_regs	__iomem *regs = ep->regs;
	u8			*buf = req->req.buf + req->req.actual;
	unsigned		count, tmp, is_short;
	unsigned		cleanup = 0, prevent = 0;

	/* erratum 0106 ... packets coming in during fifo reads might
	 * be incompletely rejected.  not all cases have workarounds.
	 */
653 654
	if (ep->dev->chiprev == 0x0100 &&
			ep->dev->gadget.speed == USB_SPEED_FULL) {
655 656
		udelay(1);
		tmp = readl(&ep->regs->ep_stat);
657
		if ((tmp & BIT(NAK_OUT_PACKETS)))
L
Linus Torvalds 已提交
658
			cleanup = 1;
659
		else if ((tmp & BIT(FIFO_FULL))) {
660
			start_out_naking(ep);
L
Linus Torvalds 已提交
661 662 663 664 665 666 667 668
			prevent = 1;
		}
		/* else: hope we don't see the problem */
	}

	/* never overflow the rx buffer. the fifo reads packets until
	 * it sees a short one; we might not be ready for them all.
	 */
669 670 671 672 673 674
	prefetchw(buf);
	count = readl(&regs->ep_avail);
	if (unlikely(count == 0)) {
		udelay(1);
		tmp = readl(&ep->regs->ep_stat);
		count = readl(&regs->ep_avail);
L
Linus Torvalds 已提交
675
		/* handled that data already? */
676
		if (count == 0 && (tmp & BIT(NAK_OUT_PACKETS)) == 0)
L
Linus Torvalds 已提交
677 678 679 680 681 682 683
			return 0;
	}

	tmp = req->req.length - req->req.actual;
	if (count > tmp) {
		/* as with DMA, data overflow gets flushed */
		if ((tmp % ep->ep.maxpacket) != 0) {
684
			ep_err(ep->dev,
L
Linus Torvalds 已提交
685 686 687 688 689 690 691 692 693 694 695 696 697 698
				"%s out fifo %d bytes, expected %d\n",
				ep->ep.name, count, tmp);
			req->req.status = -EOVERFLOW;
			cleanup = 1;
			/* NAK_OUT_PACKETS will be set, so flushing is safe;
			 * the next read will start with the next packet
			 */
		} /* else it's a ZLP, no worries */
		count = tmp;
	}
	req->req.actual += count;

	is_short = (count == 0) || ((count % ep->ep.maxpacket) != 0);

699
	ep_vdbg(ep->dev, "read %s fifo (OUT) %d bytes%s%s%s req %p %d/%d\n",
L
Linus Torvalds 已提交
700 701 702 703 704
			ep->ep.name, count, is_short ? " (short)" : "",
			cleanup ? " flush" : "", prevent ? " nak" : "",
			req, req->req.actual, req->req.length);

	while (count >= 4) {
705 706 707
		tmp = readl(&regs->ep_data);
		cpu_to_le32s(&tmp);
		put_unaligned(tmp, (u32 *)buf);
L
Linus Torvalds 已提交
708 709 710 711
		buf += 4;
		count -= 4;
	}
	if (count) {
712
		tmp = readl(&regs->ep_data);
L
Linus Torvalds 已提交
713 714 715 716 717 718 719
		/* LE conversion is implicit here: */
		do {
			*buf++ = (u8) tmp;
			tmp >>= 8;
		} while (--count);
	}
	if (cleanup)
720
		out_flush(ep);
L
Linus Torvalds 已提交
721
	if (prevent) {
722
		writel(BIT(CLEAR_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
723
		(void) readl(&ep->regs->ep_rsp);
L
Linus Torvalds 已提交
724 725
	}

726 727
	return is_short || ((req->req.actual == req->req.length) &&
			!req->req.zero);
L
Linus Torvalds 已提交
728 729 730
}

/* fill out dma descriptor to match a given request */
731 732
static void fill_dma_desc(struct net2280_ep *ep,
					struct net2280_request *req, int valid)
L
Linus Torvalds 已提交
733 734 735 736 737 738 739 740 741 742
{
	struct net2280_dma	*td = req->td;
	u32			dmacount = req->req.length;

	/* don't let DMA continue after a short OUT packet,
	 * so overruns can't affect the next transfer.
	 * in case of overruns on max-size packets, we can't
	 * stop the fifo from filling but we can flush it.
	 */
	if (ep->is_in)
743
		dmacount |= BIT(DMA_DIRECTION);
744 745
	if ((!ep->is_in && (dmacount % ep->ep.maxpacket) != 0) ||
					ep->dev->pdev->device != 0x2280)
746
		dmacount |= BIT(END_OF_CHAIN);
L
Linus Torvalds 已提交
747 748 749

	req->valid = valid;
	if (valid)
750
		dmacount |= BIT(VALID_BIT);
L
Linus Torvalds 已提交
751
	if (likely(!req->req.no_interrupt || !use_dma_chaining))
752
		dmacount |= BIT(DMA_DONE_INTERRUPT_ENABLE);
L
Linus Torvalds 已提交
753 754 755 756 757

	/* td->dmadesc = previously set by caller */
	td->dmaaddr = cpu_to_le32 (req->req.dma);

	/* 2280 may be polling VALID_BIT through ep->dma->dmadesc */
758
	wmb();
759
	td->dmacount = cpu_to_le32(dmacount);
L
Linus Torvalds 已提交
760 761 762
}

static const u32 dmactl_default =
763 764
		BIT(DMA_SCATTER_GATHER_DONE_INTERRUPT) |
		BIT(DMA_CLEAR_COUNT_ENABLE) |
L
Linus Torvalds 已提交
765
		/* erratum 0116 workaround part 1 (use POLLING) */
766 767 768 769
		(POLL_100_USEC << DESCRIPTOR_POLLING_RATE) |
		BIT(DMA_VALID_BIT_POLLING_ENABLE) |
		BIT(DMA_VALID_BIT_ENABLE) |
		BIT(DMA_SCATTER_GATHER_ENABLE) |
L
Linus Torvalds 已提交
770
		/* erratum 0116 workaround part 2 (no AUTOSTART) */
771
		BIT(DMA_ENABLE);
L
Linus Torvalds 已提交
772

773
static inline void spin_stop_dma(struct net2280_dma_regs __iomem *dma)
L
Linus Torvalds 已提交
774
{
775
	handshake(&dma->dmactl, BIT(DMA_ENABLE), 0, 50);
L
Linus Torvalds 已提交
776 777
}

778
static inline void stop_dma(struct net2280_dma_regs __iomem *dma)
L
Linus Torvalds 已提交
779
{
780
	writel(readl(&dma->dmactl) & ~BIT(DMA_ENABLE), &dma->dmactl);
781
	spin_stop_dma(dma);
L
Linus Torvalds 已提交
782 783
}

784
static void start_queue(struct net2280_ep *ep, u32 dmactl, u32 td_dma)
L
Linus Torvalds 已提交
785 786
{
	struct net2280_dma_regs	__iomem *dma = ep->dma;
787
	unsigned int tmp = BIT(VALID_BIT) | (ep->is_in << DMA_DIRECTION);
L
Linus Torvalds 已提交
788

789
	if (ep->dev->pdev->device != 0x2280)
790
		tmp |= BIT(END_OF_CHAIN);
791

792 793
	writel(tmp, &dma->dmacount);
	writel(readl(&dma->dmastat), &dma->dmastat);
L
Linus Torvalds 已提交
794

795
	writel(td_dma, &dma->dmadesc);
796
	if (ep->dev->pdev->vendor == PCI_VENDOR_ID_PLX)
797
		dmactl |= BIT(DMA_REQUEST_OUTSTANDING);
798
	writel(dmactl, &dma->dmactl);
L
Linus Torvalds 已提交
799 800

	/* erratum 0116 workaround part 3:  pci arbiter away from net2280 */
801
	(void) readl(&ep->dev->pci->pcimstctl);
L
Linus Torvalds 已提交
802

803
	writel(BIT(DMA_START), &dma->dmastat);
L
Linus Torvalds 已提交
804 805

	if (!ep->is_in)
806
		stop_out_naking(ep);
L
Linus Torvalds 已提交
807 808
}

809
static void start_dma(struct net2280_ep *ep, struct net2280_request *req)
L
Linus Torvalds 已提交
810 811 812 813 814 815 816
{
	u32			tmp;
	struct net2280_dma_regs	__iomem *dma = ep->dma;

	/* FIXME can't use DMA for ZLPs */

	/* on this path we "know" there's no dma active (yet) */
817
	WARN_ON(readl(&dma->dmactl) & BIT(DMA_ENABLE));
818
	writel(0, &ep->dma->dmactl);
L
Linus Torvalds 已提交
819 820

	/* previous OUT packet might have been short */
821 822
	if (!ep->is_in && (readl(&ep->regs->ep_stat) &
				BIT(NAK_OUT_PACKETS))) {
823
		writel(BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT),
L
Linus Torvalds 已提交
824 825
			&ep->regs->ep_stat);

826
		tmp = readl(&ep->regs->ep_avail);
L
Linus Torvalds 已提交
827
		if (tmp) {
828
			writel(readl(&dma->dmastat), &dma->dmastat);
L
Linus Torvalds 已提交
829 830

			/* transfer all/some fifo data */
831 832
			writel(req->req.dma, &dma->dmaaddr);
			tmp = min(tmp, req->req.length);
L
Linus Torvalds 已提交
833 834

			/* dma irq, faking scatterlist status */
835
			req->td->dmacount = cpu_to_le32(req->req.length - tmp);
836 837
			writel(BIT(DMA_DONE_INTERRUPT_ENABLE) | tmp,
					&dma->dmacount);
L
Linus Torvalds 已提交
838 839 840
			req->td->dmadesc = 0;
			req->valid = 1;

841 842
			writel(BIT(DMA_ENABLE), &dma->dmactl);
			writel(BIT(DMA_START), &dma->dmastat);
L
Linus Torvalds 已提交
843 844 845 846 847 848 849 850 851 852 853
			return;
		}
	}

	tmp = dmactl_default;

	/* force packet boundaries between dma requests, but prevent the
	 * controller from automagically writing a last "short" packet
	 * (zero length) unless the driver explicitly said to do that.
	 */
	if (ep->is_in) {
854 855
		if (likely((req->req.length % ep->ep.maxpacket) ||
							req->req.zero)){
856
			tmp |= BIT(DMA_FIFO_VALIDATE);
L
Linus Torvalds 已提交
857 858 859 860 861 862 863
			ep->in_fifo_validate = 1;
		} else
			ep->in_fifo_validate = 0;
	}

	/* init req->td, pointing to the current dummy */
	req->td->dmadesc = cpu_to_le32 (ep->td_dma);
864
	fill_dma_desc(ep, req, 1);
L
Linus Torvalds 已提交
865 866

	if (!use_dma_chaining)
867
		req->td->dmacount |= cpu_to_le32(BIT(END_OF_CHAIN));
L
Linus Torvalds 已提交
868

869
	start_queue(ep, tmp, req->td_dma);
L
Linus Torvalds 已提交
870 871
}

872 873
static inline void resume_dma(struct net2280_ep *ep)
{
874
	writel(readl(&ep->dma->dmactl) | BIT(DMA_ENABLE), &ep->dma->dmactl);
875 876 877 878 879 880

	ep->dma_started = true;
}

static inline void ep_stop_dma(struct net2280_ep *ep)
{
881
	writel(readl(&ep->dma->dmactl) & ~BIT(DMA_ENABLE), &ep->dma->dmactl);
882 883 884 885 886
	spin_stop_dma(ep->dma);

	ep->dma_started = false;
}

L
Linus Torvalds 已提交
887
static inline void
888
queue_dma(struct net2280_ep *ep, struct net2280_request *req, int valid)
L
Linus Torvalds 已提交
889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
{
	struct net2280_dma	*end;
	dma_addr_t		tmp;

	/* swap new dummy for old, link; fill and maybe activate */
	end = ep->dummy;
	ep->dummy = req->td;
	req->td = end;

	tmp = ep->td_dma;
	ep->td_dma = req->td_dma;
	req->td_dma = tmp;

	end->dmadesc = cpu_to_le32 (ep->td_dma);

904
	fill_dma_desc(ep, req, valid);
L
Linus Torvalds 已提交
905 906 907
}

static void
908
done(struct net2280_ep *ep, struct net2280_request *req, int status)
L
Linus Torvalds 已提交
909 910 911 912
{
	struct net2280		*dev;
	unsigned		stopped = ep->stopped;

913
	list_del_init(&req->queue);
L
Linus Torvalds 已提交
914 915 916 917 918 919 920

	if (req->req.status == -EINPROGRESS)
		req->req.status = status;
	else
		status = req->req.status;

	dev = ep->dev;
921 922
	if (ep->dma)
		usb_gadget_unmap_request(&dev->gadget, &req->req, ep->is_in);
L
Linus Torvalds 已提交
923 924

	if (status && status != -ESHUTDOWN)
925
		ep_vdbg(dev, "complete %s req %p stat %d len %u/%u\n",
L
Linus Torvalds 已提交
926 927 928 929 930
			ep->ep.name, &req->req, status,
			req->req.actual, req->req.length);

	/* don't modify queue heads during completion callback */
	ep->stopped = 1;
931 932 933
	spin_unlock(&dev->lock);
	req->req.complete(&ep->ep, &req->req);
	spin_lock(&dev->lock);
L
Linus Torvalds 已提交
934 935 936 937 938 939
	ep->stopped = stopped;
}

/*-------------------------------------------------------------------------*/

static int
940
net2280_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
L
Linus Torvalds 已提交
941 942 943 944 945 946 947 948 949
{
	struct net2280_request	*req;
	struct net2280_ep	*ep;
	struct net2280		*dev;
	unsigned long		flags;

	/* we always require a cpu-view buffer, so that we can
	 * always use pio (as fallback or whatever).
	 */
950 951 952
	req = container_of(_req, struct net2280_request, req);
	if (!_req || !_req->complete || !_req->buf ||
				!list_empty(&req->queue))
L
Linus Torvalds 已提交
953 954 955
		return -EINVAL;
	if (_req->length > (~0 & DMA_BYTE_COUNT_MASK))
		return -EDOM;
956
	ep = container_of(_ep, struct net2280_ep, ep);
L
Linus Torvalds 已提交
957 958 959 960 961 962 963 964 965 966 967
	if (!_ep || (!ep->desc && ep->num != 0))
		return -EINVAL;
	dev = ep->dev;
	if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
		return -ESHUTDOWN;

	/* FIXME implement PIO fallback for ZLPs with DMA */
	if (ep->dma && _req->length == 0)
		return -EOPNOTSUPP;

	/* set up dma mapping in case the caller didn't */
968 969 970 971 972 973 974
	if (ep->dma) {
		int ret;

		ret = usb_gadget_map_request(&dev->gadget, _req,
				ep->is_in);
		if (ret)
			return ret;
L
Linus Torvalds 已提交
975 976 977
	}

#if 0
978
	ep_vdbg(dev, "%s queue req %p, len %d buf %p\n",
L
Linus Torvalds 已提交
979 980 981
			_ep->name, _req, _req->length, _req->buf);
#endif

982
	spin_lock_irqsave(&dev->lock, flags);
L
Linus Torvalds 已提交
983 984 985 986 987

	_req->status = -EINPROGRESS;
	_req->actual = 0;

	/* kickstart this i/o queue? */
988
	if (list_empty(&ep->queue) && !ep->stopped) {
989 990
		/* DMA request while EP halted */
		if (ep->dma &&
991
		    (readl(&ep->regs->ep_rsp) & BIT(CLEAR_ENDPOINT_HALT)) &&
992
			(dev->pdev->vendor == PCI_VENDOR_ID_PLX)) {
993 994 995 996 997 998 999 1000 1001 1002 1003
			int valid = 1;
			if (ep->is_in) {
				int expect;
				expect = likely(req->req.zero ||
						((req->req.length %
						  ep->ep.maxpacket) != 0));
				if (expect != ep->in_fifo_validate)
					valid = 0;
			}
			queue_dma(ep, req, valid);
		}
L
Linus Torvalds 已提交
1004
		/* use DMA if the endpoint supports it, else pio */
1005
		else if (ep->dma)
1006
			start_dma(ep, req);
L
Linus Torvalds 已提交
1007 1008 1009
		else {
			/* maybe there's no control data, just status ack */
			if (ep->num == 0 && _req->length == 0) {
1010 1011
				allow_status(ep);
				done(ep, req, 0);
1012
				ep_vdbg(dev, "%s status ack\n", ep->ep.name);
L
Linus Torvalds 已提交
1013 1014 1015 1016 1017
				goto done;
			}

			/* PIO ... stuff the fifo, or unblock it.  */
			if (ep->is_in)
1018 1019
				write_fifo(ep, _req);
			else if (list_empty(&ep->queue)) {
L
Linus Torvalds 已提交
1020 1021 1022
				u32	s;

				/* OUT FIFO might have packet(s) buffered */
1023
				s = readl(&ep->regs->ep_stat);
1024
				if ((s & BIT(FIFO_EMPTY)) == 0) {
L
Linus Torvalds 已提交
1025 1026 1027 1028 1029 1030
					/* note:  _req->short_not_ok is
					 * ignored here since PIO _always_
					 * stops queue advance here, and
					 * _req->status doesn't change for
					 * short reads (only _req->actual)
					 */
1031 1032 1033 1034
					if (read_fifo(ep, req) &&
							ep->num == 0) {
						done(ep, req, 0);
						allow_status(ep);
L
Linus Torvalds 已提交
1035 1036
						/* don't queue it */
						req = NULL;
1037 1038 1039 1040
					} else if (read_fifo(ep, req) &&
							ep->num != 0) {
						done(ep, req, 0);
						req = NULL;
L
Linus Torvalds 已提交
1041
					} else
1042
						s = readl(&ep->regs->ep_stat);
L
Linus Torvalds 已提交
1043 1044 1045
				}

				/* don't NAK, let the fifo fill */
1046 1047
				if (req && (s & BIT(NAK_OUT_PACKETS)))
					writel(BIT(CLEAR_NAK_OUT_PACKETS),
L
Linus Torvalds 已提交
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
							&ep->regs->ep_rsp);
			}
		}

	} else if (ep->dma) {
		int	valid = 1;

		if (ep->is_in) {
			int	expect;

			/* preventing magic zlps is per-engine state, not
			 * per-transfer; irq logic must recover hiccups.
			 */
1061 1062
			expect = likely(req->req.zero ||
				(req->req.length % ep->ep.maxpacket));
L
Linus Torvalds 已提交
1063 1064 1065
			if (expect != ep->in_fifo_validate)
				valid = 0;
		}
1066
		queue_dma(ep, req, valid);
L
Linus Torvalds 已提交
1067 1068 1069

	} /* else the irq handler advances the queue. */

1070
	ep->responded = 1;
L
Linus Torvalds 已提交
1071
	if (req)
1072
		list_add_tail(&req->queue, &ep->queue);
L
Linus Torvalds 已提交
1073
done:
1074
	spin_unlock_irqrestore(&dev->lock, flags);
L
Linus Torvalds 已提交
1075 1076 1077 1078 1079 1080

	/* pci writes may still be posted */
	return 0;
}

static inline void
1081 1082
dma_done(struct net2280_ep *ep,	struct net2280_request *req, u32 dmacount,
		int status)
L
Linus Torvalds 已提交
1083 1084
{
	req->req.actual = req->req.length - (DMA_BYTE_COUNT_MASK & dmacount);
1085
	done(ep, req, status);
L
Linus Torvalds 已提交
1086 1087
}

1088
static void restart_dma(struct net2280_ep *ep);
L
Linus Torvalds 已提交
1089

1090
static void scan_dma_completions(struct net2280_ep *ep)
L
Linus Torvalds 已提交
1091 1092 1093 1094
{
	/* only look at descriptors that were "naturally" retired,
	 * so fifo and list head state won't matter
	 */
1095
	while (!list_empty(&ep->queue)) {
L
Linus Torvalds 已提交
1096 1097 1098
		struct net2280_request	*req;
		u32			tmp;

1099
		req = list_entry(ep->queue.next,
L
Linus Torvalds 已提交
1100 1101 1102
				struct net2280_request, queue);
		if (!req->valid)
			break;
1103 1104
		rmb();
		tmp = le32_to_cpup(&req->td->dmacount);
1105
		if ((tmp & BIT(VALID_BIT)) != 0)
L
Linus Torvalds 已提交
1106 1107 1108 1109 1110 1111
			break;

		/* SHORT_PACKET_TRANSFERRED_INTERRUPT handles "usb-short"
		 * cases where DMA must be aborted; this code handles
		 * all non-abort DMA completions.
		 */
1112
		if (unlikely(req->td->dmadesc == 0)) {
L
Linus Torvalds 已提交
1113
			/* paranoia */
1114
			tmp = readl(&ep->dma->dmacount);
L
Linus Torvalds 已提交
1115 1116 1117
			if (tmp & DMA_BYTE_COUNT_MASK)
				break;
			/* single transfer mode */
1118
			dma_done(ep, req, tmp, 0);
L
Linus Torvalds 已提交
1119
			break;
1120 1121
		} else if (!ep->is_in &&
				(req->req.length % ep->ep.maxpacket) != 0) {
1122
			tmp = readl(&ep->regs->ep_stat);
1123
			if (ep->dev->pdev->vendor == PCI_VENDOR_ID_PLX)
1124
				return dma_done(ep, req, tmp, 0);
L
Linus Torvalds 已提交
1125 1126 1127 1128 1129

			/* AVOID TROUBLE HERE by not issuing short reads from
			 * your gadget driver.  That helps avoids errata 0121,
			 * 0122, and 0124; not all cases trigger the warning.
			 */
1130
			if ((tmp & BIT(NAK_OUT_PACKETS)) == 0) {
1131
				ep_warn(ep->dev, "%s lost packet sync!\n",
L
Linus Torvalds 已提交
1132 1133
						ep->ep.name);
				req->req.status = -EOVERFLOW;
1134 1135 1136 1137 1138
			} else {
				tmp = readl(&ep->regs->ep_avail);
				if (tmp) {
					/* fifo gets flushed later */
					ep->out_overflow = 1;
1139
					ep_dbg(ep->dev,
1140
						"%s dma, discard %d len %d\n",
L
Linus Torvalds 已提交
1141 1142
						ep->ep.name, tmp,
						req->req.length);
1143 1144
					req->req.status = -EOVERFLOW;
				}
L
Linus Torvalds 已提交
1145 1146
			}
		}
1147
		dma_done(ep, req, tmp, 0);
L
Linus Torvalds 已提交
1148 1149 1150
	}
}

1151
static void restart_dma(struct net2280_ep *ep)
L
Linus Torvalds 已提交
1152 1153 1154 1155 1156 1157
{
	struct net2280_request	*req;
	u32			dmactl = dmactl_default;

	if (ep->stopped)
		return;
1158
	req = list_entry(ep->queue.next, struct net2280_request, queue);
L
Linus Torvalds 已提交
1159 1160

	if (!use_dma_chaining) {
1161
		start_dma(ep, req);
L
Linus Torvalds 已提交
1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
		return;
	}

	/* the 2280 will be processing the queue unless queue hiccups after
	 * the previous transfer:
	 *  IN:   wanted automagic zlp, head doesn't (or vice versa)
	 *        DMA_FIFO_VALIDATE doesn't init from dma descriptors.
	 *  OUT:  was "usb-short", we must restart.
	 */
	if (ep->is_in && !req->valid) {
		struct net2280_request	*entry, *prev = NULL;
		int			reqmode, done = 0;

1175
		ep_dbg(ep->dev, "%s dma hiccup td %p\n", ep->ep.name, req->td);
1176 1177
		ep->in_fifo_validate = likely(req->req.zero ||
				(req->req.length % ep->ep.maxpacket) != 0);
L
Linus Torvalds 已提交
1178
		if (ep->in_fifo_validate)
1179
			dmactl |= BIT(DMA_FIFO_VALIDATE);
1180
		list_for_each_entry(entry, &ep->queue, queue) {
1181
			__le32		dmacount;
L
Linus Torvalds 已提交
1182 1183 1184 1185 1186

			if (entry == req)
				continue;
			dmacount = entry->td->dmacount;
			if (!done) {
1187 1188
				reqmode = likely(entry->req.zero ||
				   (entry->req.length % ep->ep.maxpacket));
L
Linus Torvalds 已提交
1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209
				if (reqmode == ep->in_fifo_validate) {
					entry->valid = 1;
					dmacount |= valid_bit;
					entry->td->dmacount = dmacount;
					prev = entry;
					continue;
				} else {
					/* force a hiccup */
					prev->td->dmacount |= dma_done_ie;
					done = 1;
				}
			}

			/* walk the rest of the queue so unlinks behave */
			entry->valid = 0;
			dmacount &= ~valid_bit;
			entry->td->dmacount = dmacount;
			prev = entry;
		}
	}

1210 1211
	writel(0, &ep->dma->dmactl);
	start_queue(ep, dmactl, req->td_dma);
L
Linus Torvalds 已提交
1212 1213
}

1214
static void abort_dma_228x(struct net2280_ep *ep)
L
Linus Torvalds 已提交
1215 1216
{
	/* abort the current transfer */
1217
	if (likely(!list_empty(&ep->queue))) {
L
Linus Torvalds 已提交
1218
		/* FIXME work around errata 0121, 0122, 0124 */
1219
		writel(BIT(DMA_ABORT), &ep->dma->dmastat);
1220
		spin_stop_dma(ep->dma);
L
Linus Torvalds 已提交
1221
	} else
1222 1223
		stop_dma(ep->dma);
	scan_dma_completions(ep);
L
Linus Torvalds 已提交
1224 1225
}

1226 1227
static void abort_dma_338x(struct net2280_ep *ep)
{
1228
	writel(BIT(DMA_ABORT), &ep->dma->dmastat);
1229 1230 1231 1232 1233
	spin_stop_dma(ep->dma);
}

static void abort_dma(struct net2280_ep *ep)
{
1234
	if (ep->dev->pdev->vendor == PCI_VENDOR_ID_PLX_LEGACY)
1235 1236 1237 1238
		return abort_dma_228x(ep);
	return abort_dma_338x(ep);
}

L
Linus Torvalds 已提交
1239
/* dequeue ALL requests */
1240
static void nuke(struct net2280_ep *ep)
L
Linus Torvalds 已提交
1241 1242 1243 1244 1245 1246
{
	struct net2280_request	*req;

	/* called with spinlock held */
	ep->stopped = 1;
	if (ep->dma)
1247 1248 1249
		abort_dma(ep);
	while (!list_empty(&ep->queue)) {
		req = list_entry(ep->queue.next,
L
Linus Torvalds 已提交
1250 1251
				struct net2280_request,
				queue);
1252
		done(ep, req, -ESHUTDOWN);
L
Linus Torvalds 已提交
1253 1254 1255 1256
	}
}

/* dequeue JUST ONE request */
1257
static int net2280_dequeue(struct usb_ep *_ep, struct usb_request *_req)
L
Linus Torvalds 已提交
1258 1259 1260 1261 1262 1263 1264
{
	struct net2280_ep	*ep;
	struct net2280_request	*req;
	unsigned long		flags;
	u32			dmactl;
	int			stopped;

1265
	ep = container_of(_ep, struct net2280_ep, ep);
L
Linus Torvalds 已提交
1266 1267 1268
	if (!_ep || (!ep->desc && ep->num != 0) || !_req)
		return -EINVAL;

1269
	spin_lock_irqsave(&ep->dev->lock, flags);
L
Linus Torvalds 已提交
1270 1271 1272 1273 1274 1275
	stopped = ep->stopped;

	/* quiesce dma while we patch the queue */
	dmactl = 0;
	ep->stopped = 1;
	if (ep->dma) {
1276
		dmactl = readl(&ep->dma->dmactl);
L
Linus Torvalds 已提交
1277
		/* WARNING erratum 0127 may kick in ... */
1278 1279
		stop_dma(ep->dma);
		scan_dma_completions(ep);
L
Linus Torvalds 已提交
1280 1281 1282
	}

	/* make sure it's still queued on this endpoint */
1283
	list_for_each_entry(req, &ep->queue, queue) {
L
Linus Torvalds 已提交
1284 1285 1286 1287
		if (&req->req == _req)
			break;
	}
	if (&req->req != _req) {
1288
		spin_unlock_irqrestore(&ep->dev->lock, flags);
L
Linus Torvalds 已提交
1289 1290 1291 1292 1293 1294
		return -EINVAL;
	}

	/* queue head may be partially complete. */
	if (ep->queue.next == &req->queue) {
		if (ep->dma) {
1295
			ep_dbg(ep->dev, "unlink (%s) dma\n", _ep->name);
L
Linus Torvalds 已提交
1296
			_req->status = -ECONNRESET;
1297 1298 1299
			abort_dma(ep);
			if (likely(ep->queue.next == &req->queue)) {
				/* NOTE: misreports single-transfer mode*/
L
Linus Torvalds 已提交
1300
				req->td->dmacount = 0;	/* invalidate */
1301 1302
				dma_done(ep, req,
					readl(&ep->dma->dmacount),
L
Linus Torvalds 已提交
1303 1304 1305
					-ECONNRESET);
			}
		} else {
1306
			ep_dbg(ep->dev, "unlink (%s) pio\n", _ep->name);
1307
			done(ep, req, -ECONNRESET);
L
Linus Torvalds 已提交
1308 1309 1310 1311 1312 1313
		}
		req = NULL;

	/* patch up hardware chaining data */
	} else if (ep->dma && use_dma_chaining) {
		if (req->queue.prev == ep->queue.next) {
1314
			writel(le32_to_cpu(req->td->dmadesc),
L
Linus Torvalds 已提交
1315 1316
				&ep->dma->dmadesc);
			if (req->td->dmacount & dma_done_ie)
1317 1318
				writel(readl(&ep->dma->dmacount) |
						le32_to_cpu(dma_done_ie),
L
Linus Torvalds 已提交
1319 1320 1321 1322
					&ep->dma->dmacount);
		} else {
			struct net2280_request	*prev;

1323
			prev = list_entry(req->queue.prev,
L
Linus Torvalds 已提交
1324 1325 1326 1327 1328 1329 1330 1331
				struct net2280_request, queue);
			prev->td->dmadesc = req->td->dmadesc;
			if (req->td->dmacount & dma_done_ie)
				prev->td->dmacount |= dma_done_ie;
		}
	}

	if (req)
1332
		done(ep, req, -ECONNRESET);
L
Linus Torvalds 已提交
1333 1334 1335 1336
	ep->stopped = stopped;

	if (ep->dma) {
		/* turn off dma on inactive queues */
1337 1338
		if (list_empty(&ep->queue))
			stop_dma(ep->dma);
L
Linus Torvalds 已提交
1339 1340 1341
		else if (!ep->stopped) {
			/* resume current request, or start new one */
			if (req)
1342
				writel(dmactl, &ep->dma->dmactl);
L
Linus Torvalds 已提交
1343
			else
1344
				start_dma(ep, list_entry(ep->queue.next,
L
Linus Torvalds 已提交
1345 1346 1347 1348
					struct net2280_request, queue));
		}
	}

1349
	spin_unlock_irqrestore(&ep->dev->lock, flags);
L
Linus Torvalds 已提交
1350 1351 1352 1353 1354
	return 0;
}

/*-------------------------------------------------------------------------*/

1355
static int net2280_fifo_status(struct usb_ep *_ep);
L
Linus Torvalds 已提交
1356 1357

static int
1358
net2280_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged)
L
Linus Torvalds 已提交
1359 1360 1361 1362 1363
{
	struct net2280_ep	*ep;
	unsigned long		flags;
	int			retval = 0;

1364
	ep = container_of(_ep, struct net2280_ep, ep);
L
Linus Torvalds 已提交
1365 1366 1367 1368 1369 1370 1371 1372
	if (!_ep || (!ep->desc && ep->num != 0))
		return -EINVAL;
	if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
		return -ESHUTDOWN;
	if (ep->desc /* not ep0 */ && (ep->desc->bmAttributes & 0x03)
						== USB_ENDPOINT_XFER_ISOC)
		return -EINVAL;

1373 1374
	spin_lock_irqsave(&ep->dev->lock, flags);
	if (!list_empty(&ep->queue))
L
Linus Torvalds 已提交
1375
		retval = -EAGAIN;
1376
	else if (ep->is_in && value && net2280_fifo_status(_ep) != 0)
L
Linus Torvalds 已提交
1377 1378
		retval = -EAGAIN;
	else {
1379
		ep_vdbg(ep->dev, "%s %s %s\n", _ep->name,
1380 1381
				value ? "set" : "clear",
				wedged ? "wedge" : "halt");
L
Linus Torvalds 已提交
1382 1383 1384 1385 1386
		/* set/clear, then synch memory views with the device */
		if (value) {
			if (ep->num == 0)
				ep->dev->protocol_stall = 1;
			else
1387
				set_halt(ep);
1388 1389 1390
			if (wedged)
				ep->wedged = 1;
		} else {
1391
			clear_halt(ep);
1392
			if (ep->dev->pdev->vendor == PCI_VENDOR_ID_PLX &&
1393 1394
				!list_empty(&ep->queue) && ep->td_dma)
					restart_dma(ep);
1395 1396
			ep->wedged = 0;
		}
1397
		(void) readl(&ep->regs->ep_rsp);
L
Linus Torvalds 已提交
1398
	}
1399
	spin_unlock_irqrestore(&ep->dev->lock, flags);
L
Linus Torvalds 已提交
1400 1401 1402 1403

	return retval;
}

1404
static int net2280_set_halt(struct usb_ep *_ep, int value)
1405 1406 1407 1408
{
	return net2280_set_halt_and_wedge(_ep, value, 0);
}

1409
static int net2280_set_wedge(struct usb_ep *_ep)
1410 1411 1412 1413 1414 1415
{
	if (!_ep || _ep->name == ep0name)
		return -EINVAL;
	return net2280_set_halt_and_wedge(_ep, 1, 1);
}

1416
static int net2280_fifo_status(struct usb_ep *_ep)
L
Linus Torvalds 已提交
1417 1418 1419 1420
{
	struct net2280_ep	*ep;
	u32			avail;

1421
	ep = container_of(_ep, struct net2280_ep, ep);
L
Linus Torvalds 已提交
1422 1423 1424 1425 1426
	if (!_ep || (!ep->desc && ep->num != 0))
		return -ENODEV;
	if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
		return -ESHUTDOWN;

1427
	avail = readl(&ep->regs->ep_avail) & (BIT(12) - 1);
L
Linus Torvalds 已提交
1428 1429 1430 1431 1432 1433 1434
	if (avail > ep->fifo_size)
		return -EOVERFLOW;
	if (ep->is_in)
		avail = ep->fifo_size - avail;
	return avail;
}

1435
static void net2280_fifo_flush(struct usb_ep *_ep)
L
Linus Torvalds 已提交
1436 1437 1438
{
	struct net2280_ep	*ep;

1439
	ep = container_of(_ep, struct net2280_ep, ep);
L
Linus Torvalds 已提交
1440 1441 1442 1443 1444
	if (!_ep || (!ep->desc && ep->num != 0))
		return;
	if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
		return;

1445
	writel(BIT(FIFO_FLUSH), &ep->regs->ep_stat);
1446
	(void) readl(&ep->regs->ep_rsp);
L
Linus Torvalds 已提交
1447 1448
}

1449
static const struct usb_ep_ops net2280_ep_ops = {
L
Linus Torvalds 已提交
1450 1451 1452 1453 1454 1455 1456 1457 1458 1459
	.enable		= net2280_enable,
	.disable	= net2280_disable,

	.alloc_request	= net2280_alloc_request,
	.free_request	= net2280_free_request,

	.queue		= net2280_queue,
	.dequeue	= net2280_dequeue,

	.set_halt	= net2280_set_halt,
1460
	.set_wedge	= net2280_set_wedge,
L
Linus Torvalds 已提交
1461 1462 1463 1464 1465 1466
	.fifo_status	= net2280_fifo_status,
	.fifo_flush	= net2280_fifo_flush,
};

/*-------------------------------------------------------------------------*/

1467
static int net2280_get_frame(struct usb_gadget *_gadget)
L
Linus Torvalds 已提交
1468 1469 1470 1471 1472 1473 1474
{
	struct net2280		*dev;
	unsigned long		flags;
	u16			retval;

	if (!_gadget)
		return -ENODEV;
1475 1476 1477 1478
	dev = container_of(_gadget, struct net2280, gadget);
	spin_lock_irqsave(&dev->lock, flags);
	retval = get_idx_reg(dev->regs, REG_FRAME) & 0x03ff;
	spin_unlock_irqrestore(&dev->lock, flags);
L
Linus Torvalds 已提交
1479 1480 1481
	return retval;
}

1482
static int net2280_wakeup(struct usb_gadget *_gadget)
L
Linus Torvalds 已提交
1483 1484 1485 1486 1487 1488 1489
{
	struct net2280		*dev;
	u32			tmp;
	unsigned long		flags;

	if (!_gadget)
		return 0;
1490
	dev = container_of(_gadget, struct net2280, gadget);
L
Linus Torvalds 已提交
1491

1492 1493
	spin_lock_irqsave(&dev->lock, flags);
	tmp = readl(&dev->usb->usbctl);
1494 1495
	if (tmp & BIT(DEVICE_REMOTE_WAKEUP_ENABLE))
		writel(BIT(GENERATE_RESUME), &dev->usb->usbstat);
1496
	spin_unlock_irqrestore(&dev->lock, flags);
L
Linus Torvalds 已提交
1497 1498 1499 1500 1501

	/* pci writes may still be posted */
	return 0;
}

1502
static int net2280_set_selfpowered(struct usb_gadget *_gadget, int value)
L
Linus Torvalds 已提交
1503 1504 1505 1506 1507 1508 1509
{
	struct net2280		*dev;
	u32			tmp;
	unsigned long		flags;

	if (!_gadget)
		return 0;
1510
	dev = container_of(_gadget, struct net2280, gadget);
L
Linus Torvalds 已提交
1511

1512 1513
	spin_lock_irqsave(&dev->lock, flags);
	tmp = readl(&dev->usb->usbctl);
1514
	if (value) {
1515
		tmp |= BIT(SELF_POWERED_STATUS);
1516 1517
		dev->selfpowered = 1;
	} else {
1518
		tmp &= ~BIT(SELF_POWERED_STATUS);
1519 1520
		dev->selfpowered = 0;
	}
1521 1522
	writel(tmp, &dev->usb->usbctl);
	spin_unlock_irqrestore(&dev->lock, flags);
L
Linus Torvalds 已提交
1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534

	return 0;
}

static int net2280_pullup(struct usb_gadget *_gadget, int is_on)
{
	struct net2280  *dev;
	u32             tmp;
	unsigned long   flags;

	if (!_gadget)
		return -ENODEV;
1535
	dev = container_of(_gadget, struct net2280, gadget);
L
Linus Torvalds 已提交
1536

1537 1538
	spin_lock_irqsave(&dev->lock, flags);
	tmp = readl(&dev->usb->usbctl);
L
Linus Torvalds 已提交
1539 1540
	dev->softconnect = (is_on != 0);
	if (is_on)
1541
		tmp |= BIT(USB_DETECT_ENABLE);
L
Linus Torvalds 已提交
1542
	else
1543
		tmp &= ~BIT(USB_DETECT_ENABLE);
1544 1545
	writel(tmp, &dev->usb->usbctl);
	spin_unlock_irqrestore(&dev->lock, flags);
L
Linus Torvalds 已提交
1546 1547 1548 1549

	return 0;
}

1550 1551 1552 1553
static int net2280_start(struct usb_gadget *_gadget,
		struct usb_gadget_driver *driver);
static int net2280_stop(struct usb_gadget *_gadget,
		struct usb_gadget_driver *driver);
1554

L
Linus Torvalds 已提交
1555 1556 1557 1558 1559
static const struct usb_gadget_ops net2280_ops = {
	.get_frame	= net2280_get_frame,
	.wakeup		= net2280_wakeup,
	.set_selfpowered = net2280_set_selfpowered,
	.pullup		= net2280_pullup,
1560 1561
	.udc_start	= net2280_start,
	.udc_stop	= net2280_stop,
L
Linus Torvalds 已提交
1562 1563 1564 1565
};

/*-------------------------------------------------------------------------*/

1566
#ifdef	CONFIG_USB_GADGET_PDEBUG_FILES
L
Linus Torvalds 已提交
1567 1568 1569 1570 1571 1572 1573

/* FIXME move these into procfs, and use seq_file.
 * Sysfs _still_ doesn't behave for arbitrarily sized files,
 * and also doesn't help products using this with 2.4 kernels.
 */

/* "function" sysfs attribute */
1574 1575
static ssize_t function_show(struct device *_dev, struct device_attribute *attr,
			     char *buf)
L
Linus Torvalds 已提交
1576
{
1577
	struct net2280	*dev = dev_get_drvdata(_dev);
L
Linus Torvalds 已提交
1578

1579 1580
	if (!dev->driver || !dev->driver->function ||
			strlen(dev->driver->function) > PAGE_SIZE)
L
Linus Torvalds 已提交
1581
		return 0;
1582
	return scnprintf(buf, PAGE_SIZE, "%s\n", dev->driver->function);
L
Linus Torvalds 已提交
1583
}
1584
static DEVICE_ATTR_RO(function);
L
Linus Torvalds 已提交
1585

1586 1587
static ssize_t registers_show(struct device *_dev,
			      struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
1588 1589 1590 1591 1592 1593 1594
{
	struct net2280		*dev;
	char			*next;
	unsigned		size, t;
	unsigned long		flags;
	int			i;
	u32			t1, t2;
A
Andrew Morton 已提交
1595
	const char		*s;
L
Linus Torvalds 已提交
1596

1597
	dev = dev_get_drvdata(_dev);
L
Linus Torvalds 已提交
1598 1599
	next = buf;
	size = PAGE_SIZE;
1600
	spin_lock_irqsave(&dev->lock, flags);
L
Linus Torvalds 已提交
1601 1602 1603 1604 1605 1606 1607

	if (dev->driver)
		s = dev->driver->driver.name;
	else
		s = "(none)";

	/* Main Control Registers */
1608
	t = scnprintf(next, size, "%s version " DRIVER_VERSION
L
Linus Torvalds 已提交
1609 1610 1611 1612 1613 1614 1615 1616
			", chiprev %04x, dma %s\n\n"
			"devinit %03x fifoctl %08x gadget '%s'\n"
			"pci irqenb0 %02x irqenb1 %08x "
			"irqstat0 %04x irqstat1 %08x\n",
			driver_name, dev->chiprev,
			use_dma
				? (use_dma_chaining ? "chaining" : "enabled")
				: "disabled",
1617 1618
			readl(&dev->regs->devinit),
			readl(&dev->regs->fifoctl),
L
Linus Torvalds 已提交
1619
			s,
1620 1621 1622 1623
			readl(&dev->regs->pciirqenb0),
			readl(&dev->regs->pciirqenb1),
			readl(&dev->regs->irqstat0),
			readl(&dev->regs->irqstat1));
L
Linus Torvalds 已提交
1624 1625 1626 1627
	size -= t;
	next += t;

	/* USB Control Registers */
1628 1629
	t1 = readl(&dev->usb->usbctl);
	t2 = readl(&dev->usb->usbstat);
1630 1631
	if (t1 & BIT(VBUS_PIN)) {
		if (t2 & BIT(HIGH_SPEED))
L
Linus Torvalds 已提交
1632 1633 1634 1635 1636 1637 1638 1639
			s = "high speed";
		else if (dev->gadget.speed == USB_SPEED_UNKNOWN)
			s = "powered";
		else
			s = "full speed";
		/* full speed bit (6) not working?? */
	} else
			s = "not attached";
1640
	t = scnprintf(next, size,
L
Linus Torvalds 已提交
1641 1642
			"stdrsp %08x usbctl %08x usbstat %08x "
				"addr 0x%02x (%s)\n",
1643 1644
			readl(&dev->usb->stdrsp), t1, t2,
			readl(&dev->usb->ouraddr), s);
L
Linus Torvalds 已提交
1645 1646 1647 1648 1649 1650 1651 1652
	size -= t;
	next += t;

	/* PCI Master Control Registers */

	/* DMA Control Registers */

	/* Configurable EP Control Registers */
1653
	for (i = 0; i < dev->n_ep; i++) {
L
Linus Torvalds 已提交
1654 1655
		struct net2280_ep	*ep;

1656
		ep = &dev->ep[i];
L
Linus Torvalds 已提交
1657 1658 1659
		if (i && !ep->desc)
			continue;

1660
		t1 = readl(&ep->cfg->ep_cfg);
1661 1662
		t2 = readl(&ep->regs->ep_rsp) & 0xff;
		t = scnprintf(next, size,
L
Linus Torvalds 已提交
1663 1664 1665
				"\n%s\tcfg %05x rsp (%02x) %s%s%s%s%s%s%s%s"
					"irqenb %02x\n",
				ep->ep.name, t1, t2,
1666
				(t2 & BIT(CLEAR_NAK_OUT_PACKETS))
L
Linus Torvalds 已提交
1667
					? "NAK " : "",
1668
				(t2 & BIT(CLEAR_EP_HIDE_STATUS_PHASE))
L
Linus Torvalds 已提交
1669
					? "hide " : "",
1670
				(t2 & BIT(CLEAR_EP_FORCE_CRC_ERROR))
L
Linus Torvalds 已提交
1671
					? "CRC " : "",
1672
				(t2 & BIT(CLEAR_INTERRUPT_MODE))
L
Linus Torvalds 已提交
1673
					? "interrupt " : "",
1674
				(t2 & BIT(CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE))
L
Linus Torvalds 已提交
1675
					? "status " : "",
1676
				(t2 & BIT(CLEAR_NAK_OUT_PACKETS_MODE))
L
Linus Torvalds 已提交
1677
					? "NAKmode " : "",
1678
				(t2 & BIT(CLEAR_ENDPOINT_TOGGLE))
L
Linus Torvalds 已提交
1679
					? "DATA1 " : "DATA0 ",
1680
				(t2 & BIT(CLEAR_ENDPOINT_HALT))
L
Linus Torvalds 已提交
1681
					? "HALT " : "",
1682
				readl(&ep->regs->ep_irqenb));
L
Linus Torvalds 已提交
1683 1684 1685
		size -= t;
		next += t;

1686
		t = scnprintf(next, size,
L
Linus Torvalds 已提交
1687 1688
				"\tstat %08x avail %04x "
				"(ep%d%s-%s)%s\n",
1689 1690 1691 1692
				readl(&ep->regs->ep_stat),
				readl(&ep->regs->ep_avail),
				t1 & 0x0f, DIR_STRING(t1),
				type_string(t1 >> 8),
L
Linus Torvalds 已提交
1693 1694 1695 1696 1697 1698 1699
				ep->stopped ? "*" : "");
		size -= t;
		next += t;

		if (!ep->dma)
			continue;

1700
		t = scnprintf(next, size,
L
Linus Torvalds 已提交
1701 1702
				"  dma\tctl %08x stat %08x count %08x\n"
				"\taddr %08x desc %08x\n",
1703 1704 1705 1706 1707
				readl(&ep->dma->dmactl),
				readl(&ep->dma->dmastat),
				readl(&ep->dma->dmacount),
				readl(&ep->dma->dmaaddr),
				readl(&ep->dma->dmadesc));
L
Linus Torvalds 已提交
1708 1709 1710 1711 1712
		size -= t;
		next += t;

	}

1713
	/* Indexed Registers (none yet) */
L
Linus Torvalds 已提交
1714 1715

	/* Statistics */
1716
	t = scnprintf(next, size, "\nirqs:  ");
L
Linus Torvalds 已提交
1717 1718
	size -= t;
	next += t;
1719
	for (i = 0; i < dev->n_ep; i++) {
L
Linus Torvalds 已提交
1720 1721
		struct net2280_ep	*ep;

1722
		ep = &dev->ep[i];
L
Linus Torvalds 已提交
1723 1724
		if (i && !ep->irqs)
			continue;
1725
		t = scnprintf(next, size, " %s/%lu", ep->ep.name, ep->irqs);
L
Linus Torvalds 已提交
1726 1727 1728 1729
		size -= t;
		next += t;

	}
1730
	t = scnprintf(next, size, "\n");
L
Linus Torvalds 已提交
1731 1732 1733
	size -= t;
	next += t;

1734
	spin_unlock_irqrestore(&dev->lock, flags);
L
Linus Torvalds 已提交
1735 1736 1737

	return PAGE_SIZE - size;
}
1738
static DEVICE_ATTR_RO(registers);
L
Linus Torvalds 已提交
1739

1740 1741
static ssize_t queues_show(struct device *_dev, struct device_attribute *attr,
			   char *buf)
L
Linus Torvalds 已提交
1742 1743 1744 1745 1746 1747 1748
{
	struct net2280		*dev;
	char			*next;
	unsigned		size;
	unsigned long		flags;
	int			i;

1749
	dev = dev_get_drvdata(_dev);
L
Linus Torvalds 已提交
1750 1751
	next = buf;
	size = PAGE_SIZE;
1752
	spin_lock_irqsave(&dev->lock, flags);
L
Linus Torvalds 已提交
1753

1754
	for (i = 0; i < dev->n_ep; i++) {
1755
		struct net2280_ep		*ep = &dev->ep[i];
L
Linus Torvalds 已提交
1756 1757 1758 1759 1760 1761 1762 1763 1764 1765
		struct net2280_request		*req;
		int				t;

		if (i != 0) {
			const struct usb_endpoint_descriptor	*d;

			d = ep->desc;
			if (!d)
				continue;
			t = d->bEndpointAddress;
1766
			t = scnprintf(next, size,
L
Linus Torvalds 已提交
1767 1768 1769
				"\n%s (ep%d%s-%s) max %04x %s fifo %d\n",
				ep->ep.name, t & USB_ENDPOINT_NUMBER_MASK,
				(t & USB_DIR_IN) ? "in" : "out",
1770
				type_string(d->bmAttributes),
1771
				usb_endpoint_maxp(d) & 0x1fff,
L
Linus Torvalds 已提交
1772 1773 1774
				ep->dma ? "dma" : "pio", ep->fifo_size
				);
		} else /* ep0 should only have one transfer queued */
1775
			t = scnprintf(next, size, "ep0 max 64 pio %s\n",
L
Linus Torvalds 已提交
1776 1777 1778 1779 1780 1781
					ep->is_in ? "in" : "out");
		if (t <= 0 || t > size)
			goto done;
		size -= t;
		next += t;

1782 1783
		if (list_empty(&ep->queue)) {
			t = scnprintf(next, size, "\t(nothing queued)\n");
L
Linus Torvalds 已提交
1784 1785 1786 1787 1788 1789
			if (t <= 0 || t > size)
				goto done;
			size -= t;
			next += t;
			continue;
		}
1790 1791 1792
		list_for_each_entry(req, &ep->queue, queue) {
			if (ep->dma && req->td_dma == readl(&ep->dma->dmadesc))
				t = scnprintf(next, size,
L
Linus Torvalds 已提交
1793 1794 1795 1796
					"\treq %p len %d/%d "
					"buf %p (dmacount %08x)\n",
					&req->req, req->req.actual,
					req->req.length, req->req.buf,
1797
					readl(&ep->dma->dmacount));
L
Linus Torvalds 已提交
1798
			else
1799
				t = scnprintf(next, size,
L
Linus Torvalds 已提交
1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811
					"\treq %p len %d/%d buf %p\n",
					&req->req, req->req.actual,
					req->req.length, req->req.buf);
			if (t <= 0 || t > size)
				goto done;
			size -= t;
			next += t;

			if (ep->dma) {
				struct net2280_dma	*td;

				td = req->td;
1812
				t = scnprintf(next, size, "\t    td %08x "
L
Linus Torvalds 已提交
1813 1814
					" count %08x buf %08x desc %08x\n",
					(u32) req->td_dma,
1815 1816 1817
					le32_to_cpu(td->dmacount),
					le32_to_cpu(td->dmaaddr),
					le32_to_cpu(td->dmadesc));
L
Linus Torvalds 已提交
1818 1819 1820 1821 1822 1823 1824 1825 1826
				if (t <= 0 || t > size)
					goto done;
				size -= t;
				next += t;
			}
		}
	}

done:
1827
	spin_unlock_irqrestore(&dev->lock, flags);
L
Linus Torvalds 已提交
1828 1829
	return PAGE_SIZE - size;
}
1830
static DEVICE_ATTR_RO(queues);
L
Linus Torvalds 已提交
1831 1832 1833 1834


#else

1835 1836
#define device_create_file(a, b)	(0)
#define device_remove_file(a, b)	do { } while (0)
L
Linus Torvalds 已提交
1837 1838 1839 1840 1841 1842 1843 1844 1845

#endif

/*-------------------------------------------------------------------------*/

/* another driver-specific mode might be a request type doing dma
 * to/from another device fifo instead of to/from memory.
 */

1846
static void set_fifo_mode(struct net2280 *dev, int mode)
L
Linus Torvalds 已提交
1847 1848
{
	/* keeping high bits preserves BAR2 */
1849
	writel((0xffff << PCI_BASE2_RANGE) | mode, &dev->regs->fifoctl);
L
Linus Torvalds 已提交
1850 1851

	/* always ep-{a,b,e,f} ... maybe not ep-c or ep-d */
1852 1853 1854
	INIT_LIST_HEAD(&dev->gadget.ep_list);
	list_add_tail(&dev->ep[1].ep.ep_list, &dev->gadget.ep_list);
	list_add_tail(&dev->ep[2].ep.ep_list, &dev->gadget.ep_list);
L
Linus Torvalds 已提交
1855 1856
	switch (mode) {
	case 0:
1857 1858 1859
		list_add_tail(&dev->ep[3].ep.ep_list, &dev->gadget.ep_list);
		list_add_tail(&dev->ep[4].ep.ep_list, &dev->gadget.ep_list);
		dev->ep[1].fifo_size = dev->ep[2].fifo_size = 1024;
L
Linus Torvalds 已提交
1860 1861
		break;
	case 1:
1862
		dev->ep[1].fifo_size = dev->ep[2].fifo_size = 2048;
L
Linus Torvalds 已提交
1863 1864
		break;
	case 2:
1865 1866 1867
		list_add_tail(&dev->ep[3].ep.ep_list, &dev->gadget.ep_list);
		dev->ep[1].fifo_size = 2048;
		dev->ep[2].fifo_size = 1024;
L
Linus Torvalds 已提交
1868 1869 1870
		break;
	}
	/* fifo sizes for ep0, ep-c, ep-d, ep-e, and ep-f never change */
1871 1872
	list_add_tail(&dev->ep[5].ep.ep_list, &dev->gadget.ep_list);
	list_add_tail(&dev->ep[6].ep.ep_list, &dev->gadget.ep_list);
L
Linus Torvalds 已提交
1873 1874
}

1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906
static void defect7374_disable_data_eps(struct net2280 *dev)
{
	/*
	 * For Defect 7374, disable data EPs (and more):
	 *  - This phase undoes the earlier phase of the Defect 7374 workaround,
	 *    returing ep regs back to normal.
	 */
	struct net2280_ep *ep;
	int i;
	unsigned char ep_sel;
	u32 tmp_reg;

	for (i = 1; i < 5; i++) {
		ep = &dev->ep[i];
		writel(0, &ep->cfg->ep_cfg);
	}

	/* CSROUT, CSRIN, PCIOUT, PCIIN, STATIN, RCIN */
	for (i = 0; i < 6; i++)
		writel(0, &dev->dep[i].dep_cfg);

	for (ep_sel = 0; ep_sel <= 21; ep_sel++) {
		/* Select an endpoint for subsequent operations: */
		tmp_reg = readl(&dev->plregs->pl_ep_ctrl);
		writel(((tmp_reg & ~0x1f) | ep_sel), &dev->plregs->pl_ep_ctrl);

		if (ep_sel < 2 || (ep_sel > 9 && ep_sel < 14) ||
					ep_sel == 18 || ep_sel == 20)
			continue;

		/* Change settings on some selected endpoints */
		tmp_reg = readl(&dev->plregs->pl_ep_cfg_4);
1907
		tmp_reg &= ~BIT(NON_CTRL_IN_TOLERATE_BAD_DIR);
1908 1909
		writel(tmp_reg, &dev->plregs->pl_ep_cfg_4);
		tmp_reg = readl(&dev->plregs->pl_ep_ctrl);
1910
		tmp_reg |= BIT(EP_INITIALIZED);
1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927
		writel(tmp_reg, &dev->plregs->pl_ep_ctrl);
	}
}

static void defect7374_enable_data_eps_zero(struct net2280 *dev)
{
	u32 tmp = 0, tmp_reg;
	u32 fsmvalue, scratch;
	int i;
	unsigned char ep_sel;

	scratch = get_idx_reg(dev->regs, SCRATCH);
	fsmvalue = scratch & (0xf << DEFECT7374_FSM_FIELD);
	scratch &= ~(0xf << DEFECT7374_FSM_FIELD);

	/*See if firmware needs to set up for workaround*/
	if (fsmvalue != DEFECT7374_FSM_SS_CONTROL_READ) {
1928 1929
		ep_warn(dev, "Operate Defect 7374 workaround soft this time");
		ep_warn(dev, "It will operate on cold-reboot and SS connect");
1930 1931

		/*GPEPs:*/
1932
		tmp = ((0 << ENDPOINT_NUMBER) | BIT(ENDPOINT_DIRECTION) |
1933 1934
		       (2 << OUT_ENDPOINT_TYPE) | (2 << IN_ENDPOINT_TYPE) |
		       ((dev->enhanced_mode) ?
1935 1936
		       BIT(OUT_ENDPOINT_ENABLE) : BIT(ENDPOINT_ENABLE)) |
		       BIT(IN_ENDPOINT_ENABLE));
1937 1938 1939 1940 1941

		for (i = 1; i < 5; i++)
			writel(tmp, &dev->ep[i].cfg->ep_cfg);

		/* CSRIN, PCIIN, STATIN, RCIN*/
1942
		tmp = ((0 << ENDPOINT_NUMBER) | BIT(ENDPOINT_ENABLE));
1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958
		writel(tmp, &dev->dep[1].dep_cfg);
		writel(tmp, &dev->dep[3].dep_cfg);
		writel(tmp, &dev->dep[4].dep_cfg);
		writel(tmp, &dev->dep[5].dep_cfg);

		/*Implemented for development and debug.
		 * Can be refined/tuned later.*/
		for (ep_sel = 0; ep_sel <= 21; ep_sel++) {
			/* Select an endpoint for subsequent operations: */
			tmp_reg = readl(&dev->plregs->pl_ep_ctrl);
			writel(((tmp_reg & ~0x1f) | ep_sel),
			       &dev->plregs->pl_ep_ctrl);

			if (ep_sel == 1) {
				tmp =
				    (readl(&dev->plregs->pl_ep_ctrl) |
1959
				     BIT(CLEAR_ACK_ERROR_CODE) | 0);
1960 1961 1962 1963 1964 1965 1966 1967 1968
				writel(tmp, &dev->plregs->pl_ep_ctrl);
				continue;
			}

			if (ep_sel == 0 || (ep_sel > 9 && ep_sel < 14) ||
					ep_sel == 18  || ep_sel == 20)
				continue;

			tmp = (readl(&dev->plregs->pl_ep_cfg_4) |
1969
				 BIT(NON_CTRL_IN_TOLERATE_BAD_DIR) | 0);
1970 1971 1972
			writel(tmp, &dev->plregs->pl_ep_cfg_4);

			tmp = readl(&dev->plregs->pl_ep_ctrl) &
1973
				~BIT(EP_INITIALIZED);
1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984
			writel(tmp, &dev->plregs->pl_ep_ctrl);

		}

		/* Set FSM to focus on the first Control Read:
		 * - Tip: Connection speed is known upon the first
		 * setup request.*/
		scratch |= DEFECT7374_FSM_WAITING_FOR_CONTROL_READ;
		set_idx_reg(dev->regs, SCRATCH, scratch);

	} else{
1985 1986
		ep_warn(dev, "Defect 7374 workaround soft will NOT operate");
		ep_warn(dev, "It will operate on cold-reboot and SS connect");
1987 1988 1989
	}
}

L
Linus Torvalds 已提交
1990 1991 1992 1993 1994 1995 1996 1997 1998
/* keeping it simple:
 * - one bus driver, initted first;
 * - one function driver, initted second
 *
 * most of the work to support multiple net2280 controllers would
 * be to associate this gadget driver (yes?) with all of them, or
 * perhaps to bind specific drivers to specific devices.
 */

1999
static void usb_reset_228x(struct net2280 *dev)
L
Linus Torvalds 已提交
2000 2001 2002 2003
{
	u32	tmp;

	dev->gadget.speed = USB_SPEED_UNKNOWN;
2004
	(void) readl(&dev->usb->usbctl);
L
Linus Torvalds 已提交
2005

2006
	net2280_led_init(dev);
L
Linus Torvalds 已提交
2007 2008

	/* disable automatic responses, and irqs */
2009 2010 2011
	writel(0, &dev->usb->stdrsp);
	writel(0, &dev->regs->pciirqenb0);
	writel(0, &dev->regs->pciirqenb1);
L
Linus Torvalds 已提交
2012 2013 2014

	/* clear old dma and irq state */
	for (tmp = 0; tmp < 4; tmp++) {
2015
		struct net2280_ep       *ep = &dev->ep[tmp + 1];
L
Linus Torvalds 已提交
2016
		if (ep->dma)
2017
			abort_dma(ep);
L
Linus Torvalds 已提交
2018
	}
2019

2020
	writel(~0, &dev->regs->irqstat0),
2021
	writel(~(u32)BIT(SUSPEND_REQUEST_INTERRUPT), &dev->regs->irqstat1),
L
Linus Torvalds 已提交
2022 2023

	/* reset, and enable pci */
2024 2025 2026 2027 2028
	tmp = readl(&dev->regs->devinit) |
		BIT(PCI_ENABLE) |
		BIT(FIFO_SOFT_RESET) |
		BIT(USB_SOFT_RESET) |
		BIT(M8051_RESET);
2029
	writel(tmp, &dev->regs->devinit);
L
Linus Torvalds 已提交
2030 2031

	/* standard fifo and endpoint allocations */
2032
	set_fifo_mode(dev, (fifo_mode <= 2) ? fifo_mode : 0);
L
Linus Torvalds 已提交
2033 2034
}

2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049
static void usb_reset_338x(struct net2280 *dev)
{
	u32 tmp;
	u32 fsmvalue;

	dev->gadget.speed = USB_SPEED_UNKNOWN;
	(void)readl(&dev->usb->usbctl);

	net2280_led_init(dev);

	fsmvalue = get_idx_reg(dev->regs, SCRATCH) &
			(0xf << DEFECT7374_FSM_FIELD);

	/* See if firmware needs to set up for workaround: */
	if (fsmvalue != DEFECT7374_FSM_SS_CONTROL_READ) {
2050
		ep_info(dev, "%s: Defect 7374 FsmValue 0x%08x\n", __func__,
2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071
		     fsmvalue);
	} else {
		/* disable automatic responses, and irqs */
		writel(0, &dev->usb->stdrsp);
		writel(0, &dev->regs->pciirqenb0);
		writel(0, &dev->regs->pciirqenb1);
	}

	/* clear old dma and irq state */
	for (tmp = 0; tmp < 4; tmp++) {
		struct net2280_ep *ep = &dev->ep[tmp + 1];

		if (ep->dma)
			abort_dma(ep);
	}

	writel(~0, &dev->regs->irqstat0), writel(~0, &dev->regs->irqstat1);

	if (fsmvalue == DEFECT7374_FSM_SS_CONTROL_READ) {
		/* reset, and enable pci */
		tmp = readl(&dev->regs->devinit) |
2072 2073 2074 2075
		    BIT(PCI_ENABLE) |
		    BIT(FIFO_SOFT_RESET) |
		    BIT(USB_SOFT_RESET) |
		    BIT(M8051_RESET);
2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089

		writel(tmp, &dev->regs->devinit);
	}

	/* always ep-{1,2,3,4} ... maybe not ep-3 or ep-4 */
	INIT_LIST_HEAD(&dev->gadget.ep_list);

	for (tmp = 1; tmp < dev->n_ep; tmp++)
		list_add_tail(&dev->ep[tmp].ep.ep_list, &dev->gadget.ep_list);

}

static void usb_reset(struct net2280 *dev)
{
2090
	if (dev->pdev->vendor == PCI_VENDOR_ID_PLX_LEGACY)
2091 2092 2093 2094 2095
		return usb_reset_228x(dev);
	return usb_reset_338x(dev);
}

static void usb_reinit_228x(struct net2280 *dev)
L
Linus Torvalds 已提交
2096 2097 2098 2099 2100 2101 2102 2103 2104
{
	u32	tmp;
	int	init_dma;

	/* use_dma changes are ignored till next device re-init */
	init_dma = use_dma;

	/* basic endpoint init */
	for (tmp = 0; tmp < 7; tmp++) {
2105
		struct net2280_ep	*ep = &dev->ep[tmp];
L
Linus Torvalds 已提交
2106

2107
		ep->ep.name = ep_name[tmp];
L
Linus Torvalds 已提交
2108 2109 2110 2111 2112 2113
		ep->dev = dev;
		ep->num = tmp;

		if (tmp > 0 && tmp <= 4) {
			ep->fifo_size = 1024;
			if (init_dma)
2114
				ep->dma = &dev->dma[tmp - 1];
L
Linus Torvalds 已提交
2115 2116
		} else
			ep->fifo_size = 64;
2117
		ep->regs = &dev->epregs[tmp];
2118 2119
		ep->cfg = &dev->epregs[tmp];
		ep_reset_228x(dev->regs, ep);
L
Linus Torvalds 已提交
2120
	}
2121 2122 2123
	usb_ep_set_maxpacket_limit(&dev->ep[0].ep, 64);
	usb_ep_set_maxpacket_limit(&dev->ep[5].ep, 64);
	usb_ep_set_maxpacket_limit(&dev->ep[6].ep, 64);
L
Linus Torvalds 已提交
2124

2125 2126 2127
	dev->gadget.ep0 = &dev->ep[0].ep;
	dev->ep[0].stopped = 0;
	INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
L
Linus Torvalds 已提交
2128 2129 2130 2131 2132

	/* we want to prevent lowlevel/insecure access from the USB host,
	 * but erratum 0119 means this enable bit is ignored
	 */
	for (tmp = 0; tmp < 5; tmp++)
2133
		writel(EP_DONTUSE, &dev->dep[tmp].dep_cfg);
L
Linus Torvalds 已提交
2134 2135
}

2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186
static void usb_reinit_338x(struct net2280 *dev)
{
	int init_dma;
	int i;
	u32 tmp, val;
	u32 fsmvalue;
	static const u32 ne[9] = { 0, 1, 2, 3, 4, 1, 2, 3, 4 };
	static const u32 ep_reg_addr[9] = { 0x00, 0xC0, 0x00, 0xC0, 0x00,
						0x00, 0xC0, 0x00, 0xC0 };

	/* use_dma changes are ignored till next device re-init */
	init_dma = use_dma;

	/* basic endpoint init */
	for (i = 0; i < dev->n_ep; i++) {
		struct net2280_ep *ep = &dev->ep[i];

		ep->ep.name = ep_name[i];
		ep->dev = dev;
		ep->num = i;

		if (i > 0 && i <= 4 && init_dma)
			ep->dma = &dev->dma[i - 1];

		if (dev->enhanced_mode) {
			ep->cfg = &dev->epregs[ne[i]];
			ep->regs = (struct net2280_ep_regs __iomem *)
				(((void *)&dev->epregs[ne[i]]) +
				ep_reg_addr[i]);
			ep->fiforegs = &dev->fiforegs[i];
		} else {
			ep->cfg = &dev->epregs[i];
			ep->regs = &dev->epregs[i];
			ep->fiforegs = &dev->fiforegs[i];
		}

		ep->fifo_size = (i != 0) ? 2048 : 512;

		ep_reset_338x(dev->regs, ep);
	}
	usb_ep_set_maxpacket_limit(&dev->ep[0].ep, 512);

	dev->gadget.ep0 = &dev->ep[0].ep;
	dev->ep[0].stopped = 0;

	/* Link layer set up */
	fsmvalue = get_idx_reg(dev->regs, SCRATCH) &
				(0xf << DEFECT7374_FSM_FIELD);

	/* See if driver needs to set up for workaround: */
	if (fsmvalue != DEFECT7374_FSM_SS_CONTROL_READ)
2187
		ep_info(dev, "%s: Defect 7374 FsmValue %08x\n",
2188 2189 2190
						__func__, fsmvalue);
	else {
		tmp = readl(&dev->usb_ext->usbctl2) &
2191
		    ~(BIT(U1_ENABLE) | BIT(U2_ENABLE) | BIT(LTM_ENABLE));
2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229
		writel(tmp, &dev->usb_ext->usbctl2);
	}

	/* Hardware Defect and Workaround */
	val = readl(&dev->ll_lfps_regs->ll_lfps_5);
	val &= ~(0xf << TIMER_LFPS_6US);
	val |= 0x5 << TIMER_LFPS_6US;
	writel(val, &dev->ll_lfps_regs->ll_lfps_5);

	val = readl(&dev->ll_lfps_regs->ll_lfps_6);
	val &= ~(0xffff << TIMER_LFPS_80US);
	val |= 0x0100 << TIMER_LFPS_80US;
	writel(val, &dev->ll_lfps_regs->ll_lfps_6);

	/*
	 * AA_AB Errata. Issue 4. Workaround for SuperSpeed USB
	 * Hot Reset Exit Handshake may Fail in Specific Case using
	 * Default Register Settings. Workaround for Enumeration test.
	 */
	val = readl(&dev->ll_tsn_regs->ll_tsn_counters_2);
	val &= ~(0x1f << HOT_TX_NORESET_TS2);
	val |= 0x10 << HOT_TX_NORESET_TS2;
	writel(val, &dev->ll_tsn_regs->ll_tsn_counters_2);

	val = readl(&dev->ll_tsn_regs->ll_tsn_counters_3);
	val &= ~(0x1f << HOT_RX_RESET_TS2);
	val |= 0x3 << HOT_RX_RESET_TS2;
	writel(val, &dev->ll_tsn_regs->ll_tsn_counters_3);

	/*
	 * Set Recovery Idle to Recover bit:
	 * - On SS connections, setting Recovery Idle to Recover Fmw improves
	 *   link robustness with various hosts and hubs.
	 * - It is safe to set for all connection speeds; all chip revisions.
	 * - R-M-W to leave other bits undisturbed.
	 * - Reference PLX TT-7372
	*/
	val = readl(&dev->ll_chicken_reg->ll_tsn_chicken_bit);
2230
	val |= BIT(RECOVERY_IDLE_TO_RECOVER_FMW);
2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245
	writel(val, &dev->ll_chicken_reg->ll_tsn_chicken_bit);

	INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);

	/* disable dedicated endpoints */
	writel(0x0D, &dev->dep[0].dep_cfg);
	writel(0x0D, &dev->dep[1].dep_cfg);
	writel(0x0E, &dev->dep[2].dep_cfg);
	writel(0x0E, &dev->dep[3].dep_cfg);
	writel(0x0F, &dev->dep[4].dep_cfg);
	writel(0x0C, &dev->dep[5].dep_cfg);
}

static void usb_reinit(struct net2280 *dev)
{
2246
	if (dev->pdev->vendor == PCI_VENDOR_ID_PLX_LEGACY)
2247 2248 2249 2250 2251
		return usb_reinit_228x(dev);
	return usb_reinit_338x(dev);
}

static void ep0_start_228x(struct net2280 *dev)
L
Linus Torvalds 已提交
2252
{
2253 2254
	writel(BIT(CLEAR_EP_HIDE_STATUS_PHASE) |
		BIT(CLEAR_NAK_OUT_PACKETS) |
2255 2256
		BIT(CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE),
		&dev->epregs[0].ep_rsp);
L
Linus Torvalds 已提交
2257 2258 2259 2260 2261 2262 2263

	/*
	 * hardware optionally handles a bunch of standard requests
	 * that the API hides from drivers anyway.  have it do so.
	 * endpoint status/features are handled in software, to
	 * help pass tests for some dubious behavior.
	 */
2264 2265 2266 2267
	writel(BIT(SET_TEST_MODE) |
		BIT(SET_ADDRESS) |
		BIT(DEVICE_SET_CLEAR_DEVICE_REMOTE_WAKEUP) |
		BIT(GET_DEVICE_STATUS) |
2268 2269
		BIT(GET_INTERFACE_STATUS),
		&dev->usb->stdrsp);
2270 2271 2272 2273 2274 2275
	writel(BIT(USB_ROOT_PORT_WAKEUP_ENABLE) |
		BIT(SELF_POWERED_USB_DEVICE) |
		BIT(REMOTE_WAKEUP_SUPPORT) |
		(dev->softconnect << USB_DETECT_ENABLE) |
		BIT(SELF_POWERED_STATUS),
		&dev->usb->usbctl);
L
Linus Torvalds 已提交
2276 2277

	/* enable irqs so we can see ep0 and general operation  */
2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288
	writel(BIT(SETUP_PACKET_INTERRUPT_ENABLE) |
		BIT(ENDPOINT_0_INTERRUPT_ENABLE),
		&dev->regs->pciirqenb0);
	writel(BIT(PCI_INTERRUPT_ENABLE) |
		BIT(PCI_MASTER_ABORT_RECEIVED_INTERRUPT_ENABLE) |
		BIT(PCI_TARGET_ABORT_RECEIVED_INTERRUPT_ENABLE) |
		BIT(PCI_RETRY_ABORT_INTERRUPT_ENABLE) |
		BIT(VBUS_INTERRUPT_ENABLE) |
		BIT(ROOT_PORT_RESET_INTERRUPT_ENABLE) |
		BIT(SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE),
		&dev->regs->pciirqenb1);
L
Linus Torvalds 已提交
2289 2290

	/* don't leave any writes posted */
2291
	(void) readl(&dev->usb->usbctl);
L
Linus Torvalds 已提交
2292 2293
}

2294 2295 2296 2297 2298 2299 2300 2301
static void ep0_start_338x(struct net2280 *dev)
{
	u32 fsmvalue;

	fsmvalue = get_idx_reg(dev->regs, SCRATCH) &
			(0xf << DEFECT7374_FSM_FIELD);

	if (fsmvalue != DEFECT7374_FSM_SS_CONTROL_READ)
2302
		ep_info(dev, "%s: Defect 7374 FsmValue %08x\n", __func__,
2303 2304
		     fsmvalue);
	else
2305 2306
		writel(BIT(CLEAR_NAK_OUT_PACKETS_MODE) |
		       BIT(SET_EP_HIDE_STATUS_PHASE),
2307 2308 2309 2310 2311 2312 2313 2314
		       &dev->epregs[0].ep_rsp);

	/*
	 * hardware optionally handles a bunch of standard requests
	 * that the API hides from drivers anyway.  have it do so.
	 * endpoint status/features are handled in software, to
	 * help pass tests for some dubious behavior.
	 */
2315 2316 2317 2318 2319 2320
	writel(BIT(SET_ISOCHRONOUS_DELAY) |
	       BIT(SET_SEL) |
	       BIT(SET_TEST_MODE) |
	       BIT(SET_ADDRESS) |
	       BIT(GET_INTERFACE_STATUS) |
	       BIT(GET_DEVICE_STATUS),
2321 2322
		&dev->usb->stdrsp);
	dev->wakeup_enable = 1;
2323
	writel(BIT(USB_ROOT_PORT_WAKEUP_ENABLE) |
2324
	       (dev->softconnect << USB_DETECT_ENABLE) |
2325
	       BIT(DEVICE_REMOTE_WAKEUP_ENABLE),
2326 2327 2328
	       &dev->usb->usbctl);

	/* enable irqs so we can see ep0 and general operation  */
2329
	writel(BIT(SETUP_PACKET_INTERRUPT_ENABLE) |
2330 2331
	       BIT(ENDPOINT_0_INTERRUPT_ENABLE),
	       &dev->regs->pciirqenb0);
2332 2333 2334 2335
	writel(BIT(PCI_INTERRUPT_ENABLE) |
	       BIT(ROOT_PORT_RESET_INTERRUPT_ENABLE) |
	       BIT(SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE) |
	       BIT(VBUS_INTERRUPT_ENABLE),
2336 2337 2338 2339 2340 2341 2342 2343
	       &dev->regs->pciirqenb1);

	/* don't leave any writes posted */
	(void)readl(&dev->usb->usbctl);
}

static void ep0_start(struct net2280 *dev)
{
2344
	if (dev->pdev->vendor == PCI_VENDOR_ID_PLX_LEGACY)
2345 2346 2347 2348
		return ep0_start_228x(dev);
	return ep0_start_338x(dev);
}

L
Linus Torvalds 已提交
2349 2350 2351 2352 2353 2354
/* when a driver is successfully registered, it will receive
 * control requests including set_configuration(), which enables
 * non-control requests.  then usb traffic follows until a
 * disconnect is reported.  then a host may connect again, or
 * the driver might get unbound.
 */
2355 2356
static int net2280_start(struct usb_gadget *_gadget,
		struct usb_gadget_driver *driver)
L
Linus Torvalds 已提交
2357
{
2358
	struct net2280		*dev;
L
Linus Torvalds 已提交
2359 2360 2361 2362 2363 2364 2365
	int			retval;
	unsigned		i;

	/* insist on high speed support from the driver, since
	 * (dev->usb->xcvrdiag & FORCE_FULL_SPEED_MODE)
	 * "must not be used in normal operation"
	 */
2366 2367
	if (!driver || driver->max_speed < USB_SPEED_HIGH ||
			!driver->setup)
L
Linus Torvalds 已提交
2368
		return -EINVAL;
2369

2370
	dev = container_of(_gadget, struct net2280, gadget);
L
Linus Torvalds 已提交
2371

2372
	for (i = 0; i < dev->n_ep; i++)
2373
		dev->ep[i].irqs = 0;
L
Linus Torvalds 已提交
2374 2375 2376 2377 2378 2379

	/* hook up the driver ... */
	dev->softconnect = 1;
	driver->driver.bus = NULL;
	dev->driver = driver;

2380 2381 2382 2383 2384 2385
	retval = device_create_file(&dev->pdev->dev, &dev_attr_function);
	if (retval)
		goto err_unbind;
	retval = device_create_file(&dev->pdev->dev, &dev_attr_queues);
	if (retval)
		goto err_func;
L
Linus Torvalds 已提交
2386

2387
	/* Enable force-full-speed testing mode, if desired */
2388
	if (full_speed && dev->pdev->vendor == PCI_VENDOR_ID_PLX_LEGACY)
2389
		writel(BIT(FORCE_FULL_SPEED_MODE), &dev->usb->xcvrdiag);
2390

L
Linus Torvalds 已提交
2391 2392 2393
	/* ... then enable host detection and ep0; and we're ready
	 * for set_configuration as well as eventual disconnect.
	 */
2394
	net2280_led_active(dev, 1);
2395

2396
	if (dev->pdev->vendor == PCI_VENDOR_ID_PLX)
2397 2398
		defect7374_enable_data_eps_zero(dev);

2399
	ep0_start(dev);
L
Linus Torvalds 已提交
2400

2401
	ep_dbg(dev, "%s ready, usbctl %08x stdrsp %08x\n",
L
Linus Torvalds 已提交
2402
			driver->driver.name,
2403 2404
			readl(&dev->usb->usbctl),
			readl(&dev->usb->stdrsp));
L
Linus Torvalds 已提交
2405 2406 2407

	/* pci writes may still be posted */
	return 0;
2408 2409

err_func:
2410
	device_remove_file(&dev->pdev->dev, &dev_attr_function);
2411 2412 2413
err_unbind:
	dev->driver = NULL;
	return retval;
L
Linus Torvalds 已提交
2414 2415
}

2416
static void stop_activity(struct net2280 *dev, struct usb_gadget_driver *driver)
L
Linus Torvalds 已提交
2417 2418 2419 2420 2421 2422 2423 2424 2425 2426
{
	int			i;

	/* don't disconnect if it's not connected */
	if (dev->gadget.speed == USB_SPEED_UNKNOWN)
		driver = NULL;

	/* stop hardware; prevent new request submissions;
	 * and kill any outstanding requests.
	 */
2427
	usb_reset(dev);
2428
	for (i = 0; i < dev->n_ep; i++)
2429
		nuke(&dev->ep[i]);
L
Linus Torvalds 已提交
2430

2431 2432 2433 2434 2435 2436 2437
	/* report disconnect; the driver is already quiesced */
	if (driver) {
		spin_unlock(&dev->lock);
		driver->disconnect(&dev->gadget);
		spin_lock(&dev->lock);
	}

2438
	usb_reinit(dev);
L
Linus Torvalds 已提交
2439 2440
}

2441 2442
static int net2280_stop(struct usb_gadget *_gadget,
		struct usb_gadget_driver *driver)
L
Linus Torvalds 已提交
2443
{
2444
	struct net2280	*dev;
L
Linus Torvalds 已提交
2445 2446
	unsigned long	flags;

2447
	dev = container_of(_gadget, struct net2280, gadget);
L
Linus Torvalds 已提交
2448

2449 2450 2451
	spin_lock_irqsave(&dev->lock, flags);
	stop_activity(dev, driver);
	spin_unlock_irqrestore(&dev->lock, flags);
L
Linus Torvalds 已提交
2452 2453 2454

	dev->driver = NULL;

2455
	net2280_led_active(dev, 0);
2456 2457

	/* Disable full-speed test mode */
2458
	if (dev->pdev->vendor == PCI_VENDOR_ID_PLX_LEGACY)
2459
		writel(0, &dev->usb->xcvrdiag);
2460

2461 2462
	device_remove_file(&dev->pdev->dev, &dev_attr_function);
	device_remove_file(&dev->pdev->dev, &dev_attr_queues);
L
Linus Torvalds 已提交
2463

2464
	ep_dbg(dev, "unregistered driver '%s'\n",
2465 2466
			driver ? driver->driver.name : "");

L
Linus Torvalds 已提交
2467 2468 2469 2470 2471 2472 2473 2474 2475
	return 0;
}

/*-------------------------------------------------------------------------*/

/* handle ep0, ep-e, ep-f with 64 byte packets: packet per irq.
 * also works for dma-capable endpoints, in pio mode or just
 * to manually advance the queue after short OUT transfers.
 */
2476
static void handle_ep_small(struct net2280_ep *ep)
L
Linus Torvalds 已提交
2477 2478 2479 2480 2481 2482
{
	struct net2280_request	*req;
	u32			t;
	/* 0 error, 1 mid-data, 2 done */
	int			mode = 1;

2483 2484
	if (!list_empty(&ep->queue))
		req = list_entry(ep->queue.next,
L
Linus Torvalds 已提交
2485 2486 2487 2488 2489
			struct net2280_request, queue);
	else
		req = NULL;

	/* ack all, and handle what we care about */
2490
	t = readl(&ep->regs->ep_stat);
L
Linus Torvalds 已提交
2491 2492
	ep->irqs++;
#if 0
2493
	ep_vdbg(ep->dev, "%s ack ep_stat %08x, req %p\n",
L
Linus Torvalds 已提交
2494 2495
			ep->ep.name, t, req ? &req->req : 0);
#endif
2496
	if (!ep->is_in || ep->dev->pdev->device == 0x2280)
2497
		writel(t & ~BIT(NAK_OUT_PACKETS), &ep->regs->ep_stat);
2498 2499
	else
		/* Added for 2282 */
2500
		writel(t, &ep->regs->ep_stat);
L
Linus Torvalds 已提交
2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511

	/* for ep0, monitor token irqs to catch data stage length errors
	 * and to synchronize on status.
	 *
	 * also, to defer reporting of protocol stalls ... here's where
	 * data or status first appears, handling stalls here should never
	 * cause trouble on the host side..
	 *
	 * control requests could be slightly faster without token synch for
	 * status, but status can jam up that way.
	 */
2512
	if (unlikely(ep->num == 0)) {
L
Linus Torvalds 已提交
2513 2514
		if (ep->is_in) {
			/* status; stop NAKing */
2515
			if (t & BIT(DATA_OUT_PING_TOKEN_INTERRUPT)) {
L
Linus Torvalds 已提交
2516 2517
				if (ep->dev->protocol_stall) {
					ep->stopped = 1;
2518
					set_halt(ep);
L
Linus Torvalds 已提交
2519 2520
				}
				if (!req)
2521
					allow_status(ep);
L
Linus Torvalds 已提交
2522 2523
				mode = 2;
			/* reply to extra IN data tokens with a zlp */
2524
			} else if (t & BIT(DATA_IN_TOKEN_INTERRUPT)) {
L
Linus Torvalds 已提交
2525 2526
				if (ep->dev->protocol_stall) {
					ep->stopped = 1;
2527
					set_halt(ep);
L
Linus Torvalds 已提交
2528
					mode = 2;
2529 2530
				} else if (ep->responded &&
						!req && !ep->stopped)
2531
					write_fifo(ep, NULL);
L
Linus Torvalds 已提交
2532 2533 2534
			}
		} else {
			/* status; stop NAKing */
2535
			if (t & BIT(DATA_IN_TOKEN_INTERRUPT)) {
L
Linus Torvalds 已提交
2536 2537
				if (ep->dev->protocol_stall) {
					ep->stopped = 1;
2538
					set_halt(ep);
L
Linus Torvalds 已提交
2539 2540 2541
				}
				mode = 2;
			/* an extra OUT token is an error */
2542 2543 2544 2545
			} else if (((t & BIT(DATA_OUT_PING_TOKEN_INTERRUPT)) &&
					req &&
					req->req.actual == req->req.length) ||
					(ep->responded && !req)) {
L
Linus Torvalds 已提交
2546
				ep->dev->protocol_stall = 1;
2547
				set_halt(ep);
L
Linus Torvalds 已提交
2548 2549
				ep->stopped = 1;
				if (req)
2550
					done(ep, req, -EOVERFLOW);
L
Linus Torvalds 已提交
2551 2552 2553 2554 2555
				req = NULL;
			}
		}
	}

2556
	if (unlikely(!req))
L
Linus Torvalds 已提交
2557 2558 2559
		return;

	/* manual DMA queue advance after short OUT */
2560
	if (likely(ep->dma)) {
2561
		if (t & BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT)) {
L
Linus Torvalds 已提交
2562 2563 2564 2565 2566 2567 2568 2569
			u32	count;
			int	stopped = ep->stopped;

			/* TRANSFERRED works around OUT_DONE erratum 0112.
			 * we expect (N <= maxpacket) bytes; host wrote M.
			 * iff (M < N) we won't ever see a DMA interrupt.
			 */
			ep->stopped = 1;
2570
			for (count = 0; ; t = readl(&ep->regs->ep_stat)) {
L
Linus Torvalds 已提交
2571 2572 2573 2574

				/* any preceding dma transfers must finish.
				 * dma handles (M >= N), may empty the queue
				 */
2575
				scan_dma_completions(ep);
2576 2577
				if (unlikely(list_empty(&ep->queue) ||
						ep->out_overflow)) {
L
Linus Torvalds 已提交
2578 2579 2580
					req = NULL;
					break;
				}
2581
				req = list_entry(ep->queue.next,
L
Linus Torvalds 已提交
2582 2583 2584 2585 2586
					struct net2280_request, queue);

				/* here either (M < N), a "real" short rx;
				 * or (M == N) and the queue didn't empty
				 */
2587
				if (likely(t & BIT(FIFO_EMPTY))) {
2588
					count = readl(&ep->dma->dmacount);
L
Linus Torvalds 已提交
2589
					count &= DMA_BYTE_COUNT_MASK;
2590
					if (readl(&ep->dma->dmadesc)
L
Linus Torvalds 已提交
2591 2592 2593 2594 2595 2596 2597 2598
							!= req->td_dma)
						req = NULL;
					break;
				}
				udelay(1);
			}

			/* stop DMA, leave ep NAKing */
2599
			writel(BIT(DMA_ABORT), &ep->dma->dmastat);
2600
			spin_stop_dma(ep->dma);
L
Linus Torvalds 已提交
2601

2602
			if (likely(req)) {
L
Linus Torvalds 已提交
2603
				req->td->dmacount = 0;
2604 2605
				t = readl(&ep->regs->ep_avail);
				dma_done(ep, req, count,
2606 2607
					(ep->out_overflow || t)
						? -EOVERFLOW : 0);
L
Linus Torvalds 已提交
2608 2609 2610
			}

			/* also flush to prevent erratum 0106 trouble */
2611 2612 2613 2614
			if (unlikely(ep->out_overflow ||
					(ep->dev->chiprev == 0x0100 &&
					ep->dev->gadget.speed
					== USB_SPEED_FULL))) {
2615
				out_flush(ep);
L
Linus Torvalds 已提交
2616 2617 2618 2619 2620
				ep->out_overflow = 0;
			}

			/* (re)start dma if needed, stop NAKing */
			ep->stopped = stopped;
2621 2622
			if (!list_empty(&ep->queue))
				restart_dma(ep);
L
Linus Torvalds 已提交
2623
		} else
2624
			ep_dbg(ep->dev, "%s dma ep_stat %08x ??\n",
L
Linus Torvalds 已提交
2625 2626 2627 2628
					ep->ep.name, t);
		return;

	/* data packet(s) received (in the fifo, OUT) */
2629
	} else if (t & BIT(DATA_PACKET_RECEIVED_INTERRUPT)) {
2630
		if (read_fifo(ep, req) && ep->num != 0)
L
Linus Torvalds 已提交
2631 2632 2633
			mode = 2;

	/* data packet(s) transmitted (IN) */
2634
	} else if (t & BIT(DATA_PACKET_TRANSMITTED_INTERRUPT)) {
L
Linus Torvalds 已提交
2635 2636 2637 2638 2639 2640 2641 2642
		unsigned	len;

		len = req->req.length - req->req.actual;
		if (len > ep->ep.maxpacket)
			len = ep->ep.maxpacket;
		req->req.actual += len;

		/* if we wrote it all, we're usually done */
2643 2644 2645
		/* send zlps until the status stage */
		if ((req->req.actual == req->req.length) &&
			(!req->req.zero || len != ep->ep.maxpacket) && ep->num)
L
Linus Torvalds 已提交
2646 2647 2648 2649 2650 2651 2652 2653 2654
				mode = 2;

	/* there was nothing to do ...  */
	} else if (mode == 1)
		return;

	/* done */
	if (mode == 2) {
		/* stream endpoints often resubmit/unlink in completion */
2655
		done(ep, req, 0);
L
Linus Torvalds 已提交
2656 2657 2658 2659 2660 2661 2662 2663

		/* maybe advance queue to next request */
		if (ep->num == 0) {
			/* NOTE:  net2280 could let gadget driver start the
			 * status stage later. since not all controllers let
			 * them control that, the api doesn't (yet) allow it.
			 */
			if (!ep->stopped)
2664
				allow_status(ep);
L
Linus Torvalds 已提交
2665 2666
			req = NULL;
		} else {
2667 2668
			if (!list_empty(&ep->queue) && !ep->stopped)
				req = list_entry(ep->queue.next,
L
Linus Torvalds 已提交
2669 2670 2671 2672
					struct net2280_request, queue);
			else
				req = NULL;
			if (req && !ep->is_in)
2673
				stop_out_naking(ep);
L
Linus Torvalds 已提交
2674 2675 2676 2677 2678 2679 2680 2681 2682
		}
	}

	/* is there a buffer for the next packet?
	 * for best streaming performance, make sure there is one.
	 */
	if (req && !ep->stopped) {

		/* load IN fifo with next packet (may be zlp) */
2683
		if (t & BIT(DATA_PACKET_TRANSMITTED_INTERRUPT))
2684
			write_fifo(ep, &req->req);
L
Linus Torvalds 已提交
2685 2686 2687
	}
}

2688
static struct net2280_ep *get_ep_by_addr(struct net2280 *dev, u16 wIndex)
L
Linus Torvalds 已提交
2689 2690 2691 2692
{
	struct net2280_ep	*ep;

	if ((wIndex & USB_ENDPOINT_NUMBER_MASK) == 0)
2693 2694
		return &dev->ep[0];
	list_for_each_entry(ep, &dev->gadget.ep_list, ep.ep_list) {
L
Linus Torvalds 已提交
2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707
		u8	bEndpointAddress;

		if (!ep->desc)
			continue;
		bEndpointAddress = ep->desc->bEndpointAddress;
		if ((wIndex ^ bEndpointAddress) & USB_DIR_IN)
			continue;
		if ((wIndex & 0x0f) == (bEndpointAddress & 0x0f))
			return ep;
	}
	return NULL;
}

2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722
static void defect7374_workaround(struct net2280 *dev, struct usb_ctrlrequest r)
{
	u32 scratch, fsmvalue;
	u32 ack_wait_timeout, state;

	/* Workaround for Defect 7374 (U1/U2 erroneously rejected): */
	scratch = get_idx_reg(dev->regs, SCRATCH);
	fsmvalue = scratch & (0xf << DEFECT7374_FSM_FIELD);
	scratch &= ~(0xf << DEFECT7374_FSM_FIELD);

	if (!((fsmvalue == DEFECT7374_FSM_WAITING_FOR_CONTROL_READ) &&
				(r.bRequestType & USB_DIR_IN)))
		return;

	/* This is the first Control Read for this connection: */
2723
	if (!(readl(&dev->usb->usbstat) & BIT(SUPER_SPEED_MODE))) {
2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757
		/*
		 * Connection is NOT SS:
		 * - Connection must be FS or HS.
		 * - This FSM state should allow workaround software to
		 * run after the next USB connection.
		 */
		scratch |= DEFECT7374_FSM_NON_SS_CONTROL_READ;
		goto restore_data_eps;
	}

	/* Connection is SS: */
	for (ack_wait_timeout = 0;
			ack_wait_timeout < DEFECT_7374_NUMBEROF_MAX_WAIT_LOOPS;
			ack_wait_timeout++) {

		state =	readl(&dev->plregs->pl_ep_status_1)
			& (0xff << STATE);
		if ((state >= (ACK_GOOD_NORMAL << STATE)) &&
			(state <= (ACK_GOOD_MORE_ACKS_TO_COME << STATE))) {
			scratch |= DEFECT7374_FSM_SS_CONTROL_READ;
			break;
		}

		/*
		 * We have not yet received host's Data Phase ACK
		 * - Wait and try again.
		 */
		udelay(DEFECT_7374_PROCESSOR_WAIT_TIME);

		continue;
	}


	if (ack_wait_timeout >= DEFECT_7374_NUMBEROF_MAX_WAIT_LOOPS) {
2758
		ep_err(dev, "FAIL: Defect 7374 workaround waited but failed "
2759
		"to detect SS host's data phase ACK.");
2760
		ep_err(dev, "PL_EP_STATUS_1(23:16):.Expected from 0x11 to 0x16"
2761 2762
		"got 0x%2.2x.\n", state >> STATE);
	} else {
2763
		ep_warn(dev, "INFO: Defect 7374 workaround waited about\n"
2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786
		"%duSec for Control Read Data Phase ACK\n",
			DEFECT_7374_PROCESSOR_WAIT_TIME * ack_wait_timeout);
	}

restore_data_eps:
	/*
	 * Restore data EPs to their pre-workaround settings (disabled,
	 * initialized, and other details).
	 */
	defect7374_disable_data_eps(dev);

	set_idx_reg(dev->regs, SCRATCH, scratch);

	return;
}

static void ep_stall(struct net2280_ep *ep, int stall)
{
	struct net2280 *dev = ep->dev;
	u32 val;
	static const u32 ep_pl[9] = { 0, 3, 4, 7, 8, 2, 5, 6, 9 };

	if (stall) {
2787 2788 2789
		writel(BIT(SET_ENDPOINT_HALT) |
		       /* BIT(SET_NAK_PACKETS) | */
		       BIT(CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE),
2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801
		       &ep->regs->ep_rsp);
		ep->is_halt = 1;
	} else {
		if (dev->gadget.speed == USB_SPEED_SUPER) {
			/*
			 * Workaround for SS SeqNum not cleared via
			 * Endpoint Halt (Clear) bit. select endpoint
			 */
			val = readl(&dev->plregs->pl_ep_ctrl);
			val = (val & ~0x1f) | ep_pl[ep->num];
			writel(val, &dev->plregs->pl_ep_ctrl);

2802
			val |= BIT(SEQUENCE_NUMBER_RESET);
2803 2804 2805
			writel(val, &dev->plregs->pl_ep_ctrl);
		}
		val = readl(&ep->regs->ep_rsp);
2806 2807
		val |= BIT(CLEAR_ENDPOINT_HALT) |
			BIT(CLEAR_ENDPOINT_TOGGLE);
2808 2809 2810
		writel(val,
		       /* | BIT(CLEAR_NAK_PACKETS),*/
		       &ep->regs->ep_rsp);
2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877
		ep->is_halt = 0;
		val = readl(&ep->regs->ep_rsp);
	}
}

static void ep_stdrsp(struct net2280_ep *ep, int value, int wedged)
{
	/* set/clear, then synch memory views with the device */
	if (value) {
		ep->stopped = 1;
		if (ep->num == 0)
			ep->dev->protocol_stall = 1;
		else {
			if (ep->dma)
				ep_stop_dma(ep);
			ep_stall(ep, true);
		}

		if (wedged)
			ep->wedged = 1;
	} else {
		ep->stopped = 0;
		ep->wedged = 0;

		ep_stall(ep, false);

		/* Flush the queue */
		if (!list_empty(&ep->queue)) {
			struct net2280_request *req =
			    list_entry(ep->queue.next, struct net2280_request,
				       queue);
			if (ep->dma)
				resume_dma(ep);
			else {
				if (ep->is_in)
					write_fifo(ep, &req->req);
				else {
					if (read_fifo(ep, req))
						done(ep, req, 0);
				}
			}
		}
	}
}

static void handle_stat0_irqs_superspeed(struct net2280 *dev,
		struct net2280_ep *ep, struct usb_ctrlrequest r)
{
	int tmp = 0;

#define	w_value		le16_to_cpu(r.wValue)
#define	w_index		le16_to_cpu(r.wIndex)
#define	w_length	le16_to_cpu(r.wLength)

	switch (r.bRequest) {
		struct net2280_ep *e;
		u16 status;

	case USB_REQ_SET_CONFIGURATION:
		dev->addressed_state = !w_value;
		goto usb3_delegate;

	case USB_REQ_GET_STATUS:
		switch (r.bRequestType) {
		case (USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE):
			status = dev->wakeup_enable ? 0x02 : 0x00;
			if (dev->selfpowered)
2878
				status |= BIT(0);
2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891
			status |= (dev->u1_enable << 2 | dev->u2_enable << 3 |
							dev->ltm_enable << 4);
			writel(0, &dev->epregs[0].ep_irqenb);
			set_fifo_bytecount(ep, sizeof(status));
			writel((__force u32) status, &dev->epregs[0].ep_data);
			allow_status_338x(ep);
			break;

		case (USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_ENDPOINT):
			e = get_ep_by_addr(dev, w_index);
			if (!e)
				goto do_stall3;
			status = readl(&e->regs->ep_rsp) &
2892
						BIT(CLEAR_ENDPOINT_HALT);
2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911
			writel(0, &dev->epregs[0].ep_irqenb);
			set_fifo_bytecount(ep, sizeof(status));
			writel((__force u32) status, &dev->epregs[0].ep_data);
			allow_status_338x(ep);
			break;

		default:
			goto usb3_delegate;
		}
		break;

	case USB_REQ_CLEAR_FEATURE:
		switch (r.bRequestType) {
		case (USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE):
			if (!dev->addressed_state) {
				switch (w_value) {
				case USB_DEVICE_U1_ENABLE:
					dev->u1_enable = 0;
					writel(readl(&dev->usb_ext->usbctl2) &
2912
						~BIT(U1_ENABLE),
2913 2914 2915 2916 2917 2918 2919
						&dev->usb_ext->usbctl2);
					allow_status_338x(ep);
					goto next_endpoints3;

				case USB_DEVICE_U2_ENABLE:
					dev->u2_enable = 0;
					writel(readl(&dev->usb_ext->usbctl2) &
2920
						~BIT(U2_ENABLE),
2921 2922 2923 2924 2925 2926 2927
						&dev->usb_ext->usbctl2);
					allow_status_338x(ep);
					goto next_endpoints3;

				case USB_DEVICE_LTM_ENABLE:
					dev->ltm_enable = 0;
					writel(readl(&dev->usb_ext->usbctl2) &
2928
						~BIT(LTM_ENABLE),
2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939
						&dev->usb_ext->usbctl2);
					allow_status_338x(ep);
					goto next_endpoints3;

				default:
					break;
				}
			}
			if (w_value == USB_DEVICE_REMOTE_WAKEUP) {
				dev->wakeup_enable = 0;
				writel(readl(&dev->usb->usbctl) &
2940
					~BIT(DEVICE_REMOTE_WAKEUP_ENABLE),
2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952
					&dev->usb->usbctl);
				allow_status_338x(ep);
				break;
			}
			goto usb3_delegate;

		case (USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_ENDPOINT):
			e = get_ep_by_addr(dev,	w_index);
			if (!e)
				goto do_stall3;
			if (w_value != USB_ENDPOINT_HALT)
				goto do_stall3;
2953
			ep_vdbg(dev, "%s clear halt\n", e->ep.name);
2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972
			ep_stall(e, false);
			if (!list_empty(&e->queue) && e->td_dma)
				restart_dma(e);
			allow_status(ep);
			ep->stopped = 1;
			break;

		default:
			goto usb3_delegate;
		}
		break;
	case USB_REQ_SET_FEATURE:
		switch (r.bRequestType) {
		case (USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE):
			if (!dev->addressed_state) {
				switch (w_value) {
				case USB_DEVICE_U1_ENABLE:
					dev->u1_enable = 1;
					writel(readl(&dev->usb_ext->usbctl2) |
2973
						BIT(U1_ENABLE),
2974 2975 2976 2977 2978 2979 2980
						&dev->usb_ext->usbctl2);
					allow_status_338x(ep);
					goto next_endpoints3;

				case USB_DEVICE_U2_ENABLE:
					dev->u2_enable = 1;
					writel(readl(&dev->usb_ext->usbctl2) |
2981
						BIT(U2_ENABLE),
2982 2983 2984 2985 2986 2987 2988
						&dev->usb_ext->usbctl2);
					allow_status_338x(ep);
					goto next_endpoints3;

				case USB_DEVICE_LTM_ENABLE:
					dev->ltm_enable = 1;
					writel(readl(&dev->usb_ext->usbctl2) |
2989
						BIT(LTM_ENABLE),
2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000
						&dev->usb_ext->usbctl2);
					allow_status_338x(ep);
					goto next_endpoints3;
				default:
					break;
				}
			}

			if (w_value == USB_DEVICE_REMOTE_WAKEUP) {
				dev->wakeup_enable = 1;
				writel(readl(&dev->usb->usbctl) |
3001
					BIT(DEVICE_REMOTE_WAKEUP_ENABLE),
3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023
					&dev->usb->usbctl);
				allow_status_338x(ep);
				break;
			}
			goto usb3_delegate;

		case (USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_ENDPOINT):
			e = get_ep_by_addr(dev,	w_index);
			if (!e || (w_value != USB_ENDPOINT_HALT))
				goto do_stall3;
			ep_stdrsp(e, true, false);
			allow_status_338x(ep);
			break;

		default:
			goto usb3_delegate;
		}

		break;
	default:

usb3_delegate:
3024
		ep_vdbg(dev, "setup %02x.%02x v%04x i%04x l%04x ep_cfg %08x\n",
3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035
				r.bRequestType, r.bRequest,
				w_value, w_index, w_length,
				readl(&ep->cfg->ep_cfg));

		ep->responded = 0;
		spin_unlock(&dev->lock);
		tmp = dev->driver->setup(&dev->gadget, &r);
		spin_lock(&dev->lock);
	}
do_stall3:
	if (tmp < 0) {
3036
		ep_vdbg(dev, "req %02x.%02x protocol STALL; stat %d\n",
3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051
				r.bRequestType, r.bRequest, tmp);
		dev->protocol_stall = 1;
		/* TD 9.9 Halt Endpoint test. TD 9.22 Set feature test */
		ep_stall(ep, true);
	}

next_endpoints3:

#undef	w_value
#undef	w_index
#undef	w_length

	return;
}

3052
static void handle_stat0_irqs(struct net2280 *dev, u32 stat)
L
Linus Torvalds 已提交
3053 3054 3055 3056 3057
{
	struct net2280_ep	*ep;
	u32			num, scratch;

	/* most of these don't need individual acks */
3058
	stat &= ~BIT(INTA_ASSERTED);
L
Linus Torvalds 已提交
3059 3060
	if (!stat)
		return;
3061
	/* ep_dbg(dev, "irqstat0 %04x\n", stat); */
L
Linus Torvalds 已提交
3062 3063

	/* starting a control request? */
3064
	if (unlikely(stat & BIT(SETUP_PACKET_INTERRUPT))) {
L
Linus Torvalds 已提交
3065
		union {
3066
			u32			raw[2];
L
Linus Torvalds 已提交
3067 3068
			struct usb_ctrlrequest	r;
		} u;
3069
		int				tmp;
L
Linus Torvalds 已提交
3070 3071 3072
		struct net2280_request		*req;

		if (dev->gadget.speed == USB_SPEED_UNKNOWN) {
3073
			u32 val = readl(&dev->usb->usbstat);
3074
			if (val & BIT(SUPER_SPEED)) {
3075 3076 3077
				dev->gadget.speed = USB_SPEED_SUPER;
				usb_ep_set_maxpacket_limit(&dev->ep[0].ep,
						EP0_SS_MAX_PACKET_SIZE);
3078
			} else if (val & BIT(HIGH_SPEED)) {
L
Linus Torvalds 已提交
3079
				dev->gadget.speed = USB_SPEED_HIGH;
3080 3081 3082
				usb_ep_set_maxpacket_limit(&dev->ep[0].ep,
						EP0_HS_MAX_PACKET_SIZE);
			} else {
L
Linus Torvalds 已提交
3083
				dev->gadget.speed = USB_SPEED_FULL;
3084 3085 3086
				usb_ep_set_maxpacket_limit(&dev->ep[0].ep,
						EP0_HS_MAX_PACKET_SIZE);
			}
3087
			net2280_led_speed(dev, dev->gadget.speed);
3088
			ep_dbg(dev, "%s\n",
3089
					usb_speed_string(dev->gadget.speed));
L
Linus Torvalds 已提交
3090 3091
		}

3092
		ep = &dev->ep[0];
L
Linus Torvalds 已提交
3093 3094 3095
		ep->irqs++;

		/* make sure any leftover request state is cleared */
3096
		stat &= ~BIT(ENDPOINT_0_INTERRUPT);
3097 3098
		while (!list_empty(&ep->queue)) {
			req = list_entry(ep->queue.next,
L
Linus Torvalds 已提交
3099
					struct net2280_request, queue);
3100
			done(ep, req, (req->req.actual == req->req.length)
L
Linus Torvalds 已提交
3101 3102 3103 3104
						? 0 : -EPROTO);
		}
		ep->stopped = 0;
		dev->protocol_stall = 0;
3105
		if (dev->pdev->vendor == PCI_VENDOR_ID_PLX)
3106 3107 3108
			ep->is_halt = 0;
		else{
			if (ep->dev->pdev->device == 0x2280)
3109 3110
				tmp = BIT(FIFO_OVERFLOW) |
				    BIT(FIFO_UNDERFLOW);
3111 3112 3113
			else
				tmp = 0;

3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124
			writel(tmp | BIT(TIMEOUT) |
				   BIT(USB_STALL_SENT) |
				   BIT(USB_IN_NAK_SENT) |
				   BIT(USB_IN_ACK_RCVD) |
				   BIT(USB_OUT_PING_NAK_SENT) |
				   BIT(USB_OUT_ACK_SENT) |
				   BIT(SHORT_PACKET_OUT_DONE_INTERRUPT) |
				   BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT) |
				   BIT(DATA_PACKET_RECEIVED_INTERRUPT) |
				   BIT(DATA_PACKET_TRANSMITTED_INTERRUPT) |
				   BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
3125 3126
				   BIT(DATA_IN_TOKEN_INTERRUPT),
				   &ep->regs->ep_stat);
3127 3128 3129
		}
		u.raw[0] = readl(&dev->usb->setup0123);
		u.raw[1] = readl(&dev->usb->setup4567);
3130

3131 3132
		cpu_to_le32s(&u.raw[0]);
		cpu_to_le32s(&u.raw[1]);
L
Linus Torvalds 已提交
3133

3134
		if (dev->pdev->vendor == PCI_VENDOR_ID_PLX)
3135 3136
			defect7374_workaround(dev, u.r);

3137 3138
		tmp = 0;

3139 3140 3141
#define	w_value		le16_to_cpu(u.r.wValue)
#define	w_index		le16_to_cpu(u.r.wIndex)
#define	w_length	le16_to_cpu(u.r.wLength)
L
Linus Torvalds 已提交
3142 3143

		/* ack the irq */
3144 3145
		writel(BIT(SETUP_PACKET_INTERRUPT), &dev->regs->irqstat0);
		stat ^= BIT(SETUP_PACKET_INTERRUPT);
L
Linus Torvalds 已提交
3146 3147 3148 3149 3150 3151 3152 3153

		/* watch control traffic at the token level, and force
		 * synchronization before letting the status stage happen.
		 * FIXME ignore tokens we'll NAK, until driver responds.
		 * that'll mean a lot less irqs for some drivers.
		 */
		ep->is_in = (u.r.bRequestType & USB_DIR_IN) != 0;
		if (ep->is_in) {
3154 3155 3156
			scratch = BIT(DATA_PACKET_TRANSMITTED_INTERRUPT) |
				BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
				BIT(DATA_IN_TOKEN_INTERRUPT);
3157
			stop_out_naking(ep);
L
Linus Torvalds 已提交
3158
		} else
3159 3160 3161
			scratch = BIT(DATA_PACKET_RECEIVED_INTERRUPT) |
				BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
				BIT(DATA_IN_TOKEN_INTERRUPT);
3162
		writel(scratch, &dev->epregs[0].ep_irqenb);
L
Linus Torvalds 已提交
3163 3164 3165 3166

		/* we made the hardware handle most lowlevel requests;
		 * everything else goes uplevel to the gadget code.
		 */
3167
		ep->responded = 1;
3168 3169 3170 3171 3172 3173

		if (dev->gadget.speed == USB_SPEED_SUPER) {
			handle_stat0_irqs_superspeed(dev, ep, u.r);
			goto next_endpoints;
		}

L
Linus Torvalds 已提交
3174 3175 3176
		switch (u.r.bRequest) {
		case USB_REQ_GET_STATUS: {
			struct net2280_ep	*e;
3177
			__le32			status;
L
Linus Torvalds 已提交
3178 3179 3180 3181

			/* hw handles device and interface status */
			if (u.r.bRequestType != (USB_DIR_IN|USB_RECIP_ENDPOINT))
				goto delegate;
3182 3183
			e = get_ep_by_addr(dev, w_index);
			if (!e || w_length > 2)
L
Linus Torvalds 已提交
3184 3185
				goto do_stall;

3186
			if (readl(&e->regs->ep_rsp) & BIT(SET_ENDPOINT_HALT))
3187
				status = cpu_to_le32(1);
L
Linus Torvalds 已提交
3188
			else
3189
				status = cpu_to_le32(0);
L
Linus Torvalds 已提交
3190 3191

			/* don't bother with a request object! */
3192 3193 3194 3195
			writel(0, &dev->epregs[0].ep_irqenb);
			set_fifo_bytecount(ep, w_length);
			writel((__force u32)status, &dev->epregs[0].ep_data);
			allow_status(ep);
3196
			ep_vdbg(dev, "%s stat %02x\n", ep->ep.name, status);
L
Linus Torvalds 已提交
3197 3198 3199 3200 3201 3202 3203 3204 3205
			goto next_endpoints;
			}
			break;
		case USB_REQ_CLEAR_FEATURE: {
			struct net2280_ep	*e;

			/* hw handles device features */
			if (u.r.bRequestType != USB_RECIP_ENDPOINT)
				goto delegate;
3206
			if (w_value != USB_ENDPOINT_HALT || w_length != 0)
L
Linus Torvalds 已提交
3207
				goto do_stall;
3208 3209
			e = get_ep_by_addr(dev, w_index);
			if (!e)
L
Linus Torvalds 已提交
3210
				goto do_stall;
3211
			if (e->wedged) {
3212
				ep_vdbg(dev, "%s wedged, halt not cleared\n",
3213 3214
						ep->ep.name);
			} else {
3215
				ep_vdbg(dev, "%s clear halt\n", e->ep.name);
3216
				clear_halt(e);
3217 3218
				if (ep->dev->pdev->vendor ==
						PCI_VENDOR_ID_PLX &&
3219 3220
					!list_empty(&e->queue) && e->td_dma)
						restart_dma(e);
3221
			}
3222
			allow_status(ep);
L
Linus Torvalds 已提交
3223 3224 3225 3226 3227 3228 3229 3230 3231
			goto next_endpoints;
			}
			break;
		case USB_REQ_SET_FEATURE: {
			struct net2280_ep	*e;

			/* hw handles device features */
			if (u.r.bRequestType != USB_RECIP_ENDPOINT)
				goto delegate;
3232
			if (w_value != USB_ENDPOINT_HALT || w_length != 0)
L
Linus Torvalds 已提交
3233
				goto do_stall;
3234 3235
			e = get_ep_by_addr(dev, w_index);
			if (!e)
L
Linus Torvalds 已提交
3236
				goto do_stall;
3237 3238
			if (e->ep.name == ep0name)
				goto do_stall;
3239
			set_halt(e);
3240
			if (dev->pdev->vendor == PCI_VENDOR_ID_PLX && e->dma)
3241
				abort_dma(e);
3242
			allow_status(ep);
3243
			ep_vdbg(dev, "%s set halt\n", ep->ep.name);
L
Linus Torvalds 已提交
3244 3245 3246 3247 3248
			goto next_endpoints;
			}
			break;
		default:
delegate:
3249
			ep_vdbg(dev, "setup %02x.%02x v%04x i%04x l%04x "
L
Linus Torvalds 已提交
3250 3251
				"ep_cfg %08x\n",
				u.r.bRequestType, u.r.bRequest,
3252
				w_value, w_index, w_length,
3253
				readl(&ep->cfg->ep_cfg));
3254
			ep->responded = 0;
3255 3256 3257
			spin_unlock(&dev->lock);
			tmp = dev->driver->setup(&dev->gadget, &u.r);
			spin_lock(&dev->lock);
L
Linus Torvalds 已提交
3258 3259 3260 3261 3262
		}

		/* stall ep0 on error */
		if (tmp < 0) {
do_stall:
3263
			ep_vdbg(dev, "req %02x.%02x protocol STALL; stat %d\n",
L
Linus Torvalds 已提交
3264 3265 3266 3267 3268 3269 3270 3271 3272 3273
					u.r.bRequestType, u.r.bRequest, tmp);
			dev->protocol_stall = 1;
		}

		/* some in/out token irq should follow; maybe stall then.
		 * driver must queue a request (even zlp) or halt ep0
		 * before the host times out.
		 */
	}

3274 3275 3276 3277
#undef	w_value
#undef	w_index
#undef	w_length

L
Linus Torvalds 已提交
3278 3279 3280 3281 3282 3283 3284 3285
next_endpoints:
	/* endpoint data irq ? */
	scratch = stat & 0x7f;
	stat &= ~0x7f;
	for (num = 0; scratch; num++) {
		u32		t;

		/* do this endpoint's FIFO and queue need tending? */
3286
		t = BIT(num);
L
Linus Torvalds 已提交
3287 3288 3289 3290
		if ((scratch & t) == 0)
			continue;
		scratch ^= t;

3291 3292
		ep = &dev->ep[num];
		handle_ep_small(ep);
L
Linus Torvalds 已提交
3293 3294 3295
	}

	if (stat)
3296
		ep_dbg(dev, "unhandled irqstat0 %08x\n", stat);
L
Linus Torvalds 已提交
3297 3298
}

3299 3300 3301 3302
#define DMA_INTERRUPTS (BIT(DMA_D_INTERRUPT) | \
		BIT(DMA_C_INTERRUPT) | \
		BIT(DMA_B_INTERRUPT) | \
		BIT(DMA_A_INTERRUPT))
L
Linus Torvalds 已提交
3303
#define	PCI_ERROR_INTERRUPTS ( \
3304 3305 3306
		BIT(PCI_MASTER_ABORT_RECEIVED_INTERRUPT) | \
		BIT(PCI_TARGET_ABORT_RECEIVED_INTERRUPT) | \
		BIT(PCI_RETRY_ABORT_INTERRUPT))
L
Linus Torvalds 已提交
3307

3308
static void handle_stat1_irqs(struct net2280 *dev, u32 stat)
L
Linus Torvalds 已提交
3309 3310 3311 3312 3313
{
	struct net2280_ep	*ep;
	u32			tmp, num, mask, scratch;

	/* after disconnect there's nothing else to do! */
3314 3315
	tmp = BIT(VBUS_INTERRUPT) | BIT(ROOT_PORT_RESET_INTERRUPT);
	mask = BIT(SUPER_SPEED) | BIT(HIGH_SPEED) | BIT(FULL_SPEED);
L
Linus Torvalds 已提交
3316 3317

	/* VBUS disconnect is indicated by VBUS_PIN and VBUS_INTERRUPT set.
3318
	 * Root Port Reset is indicated by ROOT_PORT_RESET_INTERRUPT set and
3319
	 * both HIGH_SPEED and FULL_SPEED clear (as ROOT_PORT_RESET_INTERRUPT
L
Linus Torvalds 已提交
3320 3321 3322
	 * only indicates a change in the reset state).
	 */
	if (stat & tmp) {
3323
		writel(tmp, &dev->regs->irqstat1);
3324 3325 3326 3327 3328
		if ((((stat & BIT(ROOT_PORT_RESET_INTERRUPT)) &&
				(readl(&dev->usb->usbstat) & mask)) ||
				((readl(&dev->usb->usbctl) &
					BIT(VBUS_PIN)) == 0)) &&
				(dev->gadget.speed != USB_SPEED_UNKNOWN)) {
3329
			ep_dbg(dev, "disconnect %s\n",
L
Linus Torvalds 已提交
3330
					dev->driver->driver.name);
3331 3332
			stop_activity(dev, dev->driver);
			ep0_start(dev);
L
Linus Torvalds 已提交
3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346
			return;
		}
		stat &= ~tmp;

		/* vBUS can bounce ... one of many reasons to ignore the
		 * notion of hotplug events on bus connect/disconnect!
		 */
		if (!stat)
			return;
	}

	/* NOTE: chip stays in PCI D0 state for now, but it could
	 * enter D1 to save more power
	 */
3347
	tmp = BIT(SUSPEND_REQUEST_CHANGE_INTERRUPT);
L
Linus Torvalds 已提交
3348
	if (stat & tmp) {
3349
		writel(tmp, &dev->regs->irqstat1);
3350
		if (stat & BIT(SUSPEND_REQUEST_INTERRUPT)) {
L
Linus Torvalds 已提交
3351
			if (dev->driver->suspend)
3352
				dev->driver->suspend(&dev->gadget);
L
Linus Torvalds 已提交
3353
			if (!enable_suspend)
3354
				stat &= ~BIT(SUSPEND_REQUEST_INTERRUPT);
L
Linus Torvalds 已提交
3355 3356
		} else {
			if (dev->driver->resume)
3357
				dev->driver->resume(&dev->gadget);
L
Linus Torvalds 已提交
3358 3359 3360 3361 3362 3363 3364
			/* at high speed, note erratum 0133 */
		}
		stat &= ~tmp;
	}

	/* clear any other status/irqs */
	if (stat)
3365
		writel(stat, &dev->regs->irqstat1);
L
Linus Torvalds 已提交
3366 3367

	/* some status we can just ignore */
3368
	if (dev->pdev->device == 0x2280)
3369 3370 3371 3372
		stat &= ~(BIT(CONTROL_STATUS_INTERRUPT) |
			  BIT(SUSPEND_REQUEST_INTERRUPT) |
			  BIT(RESUME_INTERRUPT) |
			  BIT(SOF_INTERRUPT));
3373
	else
3374 3375 3376 3377
		stat &= ~(BIT(CONTROL_STATUS_INTERRUPT) |
			  BIT(RESUME_INTERRUPT) |
			  BIT(SOF_DOWN_INTERRUPT) |
			  BIT(SOF_INTERRUPT));
3378

L
Linus Torvalds 已提交
3379 3380
	if (!stat)
		return;
3381
	/* ep_dbg(dev, "irqstat1 %08x\n", stat);*/
L
Linus Torvalds 已提交
3382 3383 3384 3385 3386 3387 3388 3389

	/* DMA status, for ep-{a,b,c,d} */
	scratch = stat & DMA_INTERRUPTS;
	stat &= ~DMA_INTERRUPTS;
	scratch >>= 9;
	for (num = 0; scratch; num++) {
		struct net2280_dma_regs	__iomem *dma;

3390
		tmp = BIT(num);
L
Linus Torvalds 已提交
3391 3392 3393 3394
		if ((tmp & scratch) == 0)
			continue;
		scratch ^= tmp;

3395
		ep = &dev->ep[num + 1];
L
Linus Torvalds 已提交
3396 3397 3398 3399 3400 3401
		dma = ep->dma;

		if (!dma)
			continue;

		/* clear ep's dma status */
3402 3403
		tmp = readl(&dma->dmastat);
		writel(tmp, &dma->dmastat);
L
Linus Torvalds 已提交
3404

3405
		/* dma sync*/
3406
		if (dev->pdev->vendor == PCI_VENDOR_ID_PLX) {
3407 3408
			u32 r_dmacount = readl(&dma->dmacount);
			if (!ep->is_in &&  (r_dmacount & 0x00FFFFFF) &&
3409
			    (tmp & BIT(DMA_TRANSACTION_DONE_INTERRUPT)))
3410 3411 3412
				continue;
		}

L
Linus Torvalds 已提交
3413 3414 3415 3416
		/* chaining should stop on abort, short OUT from fifo,
		 * or (stat0 codepath) short OUT transfer.
		 */
		if (!use_dma_chaining) {
3417
			if (!(tmp & BIT(DMA_TRANSACTION_DONE_INTERRUPT))) {
3418
				ep_dbg(ep->dev, "%s no xact done? %08x\n",
L
Linus Torvalds 已提交
3419 3420 3421
					ep->ep.name, tmp);
				continue;
			}
3422
			stop_dma(ep->dma);
L
Linus Torvalds 已提交
3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434
		}

		/* OUT transfers terminate when the data from the
		 * host is in our memory.  Process whatever's done.
		 * On this path, we know transfer's last packet wasn't
		 * less than req->length. NAK_OUT_PACKETS may be set,
		 * or the FIFO may already be holding new packets.
		 *
		 * IN transfers can linger in the FIFO for a very
		 * long time ... we ignore that for now, accounting
		 * precisely (like PIO does) needs per-packet irqs
		 */
3435
		scan_dma_completions(ep);
L
Linus Torvalds 已提交
3436 3437

		/* disable dma on inactive queues; else maybe restart */
3438
		if (list_empty(&ep->queue)) {
L
Linus Torvalds 已提交
3439
			if (use_dma_chaining)
3440
				stop_dma(ep->dma);
L
Linus Torvalds 已提交
3441
		} else {
3442
			tmp = readl(&dma->dmactl);
3443
			if (!use_dma_chaining || (tmp & BIT(DMA_ENABLE)) == 0)
3444
				restart_dma(ep);
L
Linus Torvalds 已提交
3445 3446
			else if (ep->is_in && use_dma_chaining) {
				struct net2280_request	*req;
3447
				__le32			dmacount;
L
Linus Torvalds 已提交
3448 3449 3450 3451 3452 3453

				/* the descriptor at the head of the chain
				 * may still have VALID_BIT clear; that's
				 * used to trigger changing DMA_FIFO_VALIDATE
				 * (affects automagic zlp writes).
				 */
3454
				req = list_entry(ep->queue.next,
L
Linus Torvalds 已提交
3455 3456
						struct net2280_request, queue);
				dmacount = req->td->dmacount;
3457 3458
				dmacount &= cpu_to_le32(BIT(VALID_BIT) |
						DMA_BYTE_COUNT_MASK);
L
Linus Torvalds 已提交
3459
				if (dmacount && (dmacount & valid_bit) == 0)
3460
					restart_dma(ep);
L
Linus Torvalds 已提交
3461 3462 3463 3464 3465 3466 3467 3468 3469
			}
		}
		ep->irqs++;
	}

	/* NOTE:  there are other PCI errors we might usefully notice.
	 * if they appear very often, here's where to try recovering.
	 */
	if (stat & PCI_ERROR_INTERRUPTS) {
3470
		ep_err(dev, "pci dma error; stat %08x\n", stat);
L
Linus Torvalds 已提交
3471 3472 3473 3474
		stat &= ~PCI_ERROR_INTERRUPTS;
		/* these are fatal errors, but "maybe" they won't
		 * happen again ...
		 */
3475 3476
		stop_activity(dev, dev->driver);
		ep0_start(dev);
L
Linus Torvalds 已提交
3477 3478 3479 3480
		stat = 0;
	}

	if (stat)
3481
		ep_dbg(dev, "unhandled irqstat1 %08x\n", stat);
L
Linus Torvalds 已提交
3482 3483
}

3484
static irqreturn_t net2280_irq(int irq, void *_dev)
L
Linus Torvalds 已提交
3485 3486 3487
{
	struct net2280		*dev = _dev;

3488
	/* shared interrupt, not ours */
3489
	if (dev->pdev->vendor == PCI_VENDOR_ID_PLX_LEGACY &&
3490
		(!(readl(&dev->regs->irqstat0) & BIT(INTA_ASSERTED))))
3491 3492
		return IRQ_NONE;

3493
	spin_lock(&dev->lock);
L
Linus Torvalds 已提交
3494 3495

	/* handle disconnect, dma, and more */
3496
	handle_stat1_irqs(dev, readl(&dev->regs->irqstat1));
L
Linus Torvalds 已提交
3497 3498

	/* control requests and PIO */
3499
	handle_stat0_irqs(dev, readl(&dev->regs->irqstat0));
L
Linus Torvalds 已提交
3500

3501
	if (dev->pdev->vendor == PCI_VENDOR_ID_PLX) {
3502 3503 3504 3505 3506 3507
		/* re-enable interrupt to trigger any possible new interrupt */
		u32 pciirqenb1 = readl(&dev->regs->pciirqenb1);
		writel(pciirqenb1 & 0x7FFFFFFF, &dev->regs->pciirqenb1);
		writel(pciirqenb1, &dev->regs->pciirqenb1);
	}

3508
	spin_unlock(&dev->lock);
L
Linus Torvalds 已提交
3509 3510 3511 3512 3513 3514

	return IRQ_HANDLED;
}

/*-------------------------------------------------------------------------*/

3515
static void gadget_release(struct device *_dev)
L
Linus Torvalds 已提交
3516
{
3517
	struct net2280	*dev = dev_get_drvdata(_dev);
L
Linus Torvalds 已提交
3518

3519
	kfree(dev);
L
Linus Torvalds 已提交
3520 3521 3522 3523
}

/* tear down the binding between this driver and the pci device */

3524
static void net2280_remove(struct pci_dev *pdev)
L
Linus Torvalds 已提交
3525
{
3526
	struct net2280		*dev = pci_get_drvdata(pdev);
L
Linus Torvalds 已提交
3527

3528 3529
	usb_del_gadget_udc(&dev->gadget);

3530
	BUG_ON(dev->driver);
L
Linus Torvalds 已提交
3531 3532

	/* then clean up the resources we allocated during probe() */
3533
	net2280_led_shutdown(dev);
L
Linus Torvalds 已提交
3534 3535 3536
	if (dev->requests) {
		int		i;
		for (i = 1; i < 5; i++) {
3537
			if (!dev->ep[i].dummy)
L
Linus Torvalds 已提交
3538
				continue;
3539 3540
			pci_pool_free(dev->requests, dev->ep[i].dummy,
					dev->ep[i].td_dma);
L
Linus Torvalds 已提交
3541
		}
3542
		pci_pool_destroy(dev->requests);
L
Linus Torvalds 已提交
3543 3544
	}
	if (dev->got_irq)
3545
		free_irq(pdev->irq, dev);
3546
	if (use_msi && dev->pdev->vendor == PCI_VENDOR_ID_PLX)
3547
		pci_disable_msi(pdev);
L
Linus Torvalds 已提交
3548
	if (dev->regs)
3549
		iounmap(dev->regs);
L
Linus Torvalds 已提交
3550
	if (dev->region)
3551 3552
		release_mem_region(pci_resource_start(pdev, 0),
				pci_resource_len(pdev, 0));
L
Linus Torvalds 已提交
3553
	if (dev->enabled)
3554 3555
		pci_disable_device(pdev);
	device_remove_file(&pdev->dev, &dev_attr_registers);
L
Linus Torvalds 已提交
3556

3557
	ep_info(dev, "unbind\n");
L
Linus Torvalds 已提交
3558 3559 3560 3561 3562 3563
}

/* wrap this driver around the specified device, but
 * don't respond over USB until a gadget driver binds to us.
 */

3564
static int net2280_probe(struct pci_dev *pdev, const struct pci_device_id *id)
L
Linus Torvalds 已提交
3565 3566 3567 3568 3569 3570
{
	struct net2280		*dev;
	unsigned long		resource, len;
	void			__iomem *base = NULL;
	int			retval, i;

3571 3572 3573
	if (!use_dma)
		use_dma_chaining = 0;

L
Linus Torvalds 已提交
3574
	/* alloc, and start init */
3575 3576
	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
	if (dev == NULL) {
L
Linus Torvalds 已提交
3577 3578 3579 3580
		retval = -ENOMEM;
		goto done;
	}

3581 3582
	pci_set_drvdata(pdev, dev);
	spin_lock_init(&dev->lock);
L
Linus Torvalds 已提交
3583 3584
	dev->pdev = pdev;
	dev->gadget.ops = &net2280_ops;
3585
	dev->gadget.max_speed = (dev->pdev->vendor == PCI_VENDOR_ID_PLX) ?
3586
				USB_SPEED_SUPER : USB_SPEED_HIGH;
L
Linus Torvalds 已提交
3587 3588 3589 3590 3591

	/* the "gadget" abstracts/virtualizes the controller */
	dev->gadget.name = driver_name;

	/* now all the pci goodies ... */
3592 3593
	if (pci_enable_device(pdev) < 0) {
		retval = -ENODEV;
L
Linus Torvalds 已提交
3594 3595 3596 3597 3598 3599 3600 3601
		goto done;
	}
	dev->enabled = 1;

	/* BAR 0 holds all the registers
	 * BAR 1 is 8051 memory; unused here (note erratum 0103)
	 * BAR 2 is fifo memory; unused here
	 */
3602 3603 3604
	resource = pci_resource_start(pdev, 0);
	len = pci_resource_len(pdev, 0);
	if (!request_mem_region(resource, len, driver_name)) {
3605
		ep_dbg(dev, "controller already in use\n");
L
Linus Torvalds 已提交
3606 3607 3608 3609 3610
		retval = -EBUSY;
		goto done;
	}
	dev->region = 1;

3611 3612 3613 3614
	/* FIXME provide firmware download interface to put
	 * 8051 code into the chip, e.g. to turn on PCI PM.
	 */

3615
	base = ioremap_nocache(resource, len);
L
Linus Torvalds 已提交
3616
	if (base == NULL) {
3617
		ep_dbg(dev, "can't map memory\n");
L
Linus Torvalds 已提交
3618 3619 3620 3621 3622 3623 3624 3625 3626 3627
		retval = -EFAULT;
		goto done;
	}
	dev->regs = (struct net2280_regs __iomem *) base;
	dev->usb = (struct net2280_usb_regs __iomem *) (base + 0x0080);
	dev->pci = (struct net2280_pci_regs __iomem *) (base + 0x0100);
	dev->dma = (struct net2280_dma_regs __iomem *) (base + 0x0180);
	dev->dep = (struct net2280_dep_regs __iomem *) (base + 0x0200);
	dev->epregs = (struct net2280_ep_regs __iomem *) (base + 0x0300);

3628
	if (dev->pdev->vendor == PCI_VENDOR_ID_PLX) {
3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645
		u32 fsmvalue;
		u32 usbstat;
		dev->usb_ext = (struct usb338x_usb_ext_regs __iomem *)
							(base + 0x00b4);
		dev->fiforegs = (struct usb338x_fifo_regs __iomem *)
							(base + 0x0500);
		dev->llregs = (struct usb338x_ll_regs __iomem *)
							(base + 0x0700);
		dev->ll_lfps_regs = (struct usb338x_ll_lfps_regs __iomem *)
							(base + 0x0748);
		dev->ll_tsn_regs = (struct usb338x_ll_tsn_regs __iomem *)
							(base + 0x077c);
		dev->ll_chicken_reg = (struct usb338x_ll_chi_regs __iomem *)
							(base + 0x079c);
		dev->plregs = (struct usb338x_pl_regs __iomem *)
							(base + 0x0800);
		usbstat = readl(&dev->usb->usbstat);
3646
		dev->enhanced_mode = !!(usbstat & BIT(11));
3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660
		dev->n_ep = (dev->enhanced_mode) ? 9 : 5;
		/* put into initial config, link up all endpoints */
		fsmvalue = get_idx_reg(dev->regs, SCRATCH) &
					(0xf << DEFECT7374_FSM_FIELD);
		/* See if firmware needs to set up for workaround: */
		if (fsmvalue == DEFECT7374_FSM_SS_CONTROL_READ)
			writel(0, &dev->usb->usbctl);
	} else{
		dev->enhanced_mode = 0;
		dev->n_ep = 7;
		/* put into initial config, link up all endpoints */
		writel(0, &dev->usb->usbctl);
	}

3661 3662
	usb_reset(dev);
	usb_reinit(dev);
L
Linus Torvalds 已提交
3663 3664 3665

	/* irq setup after old hardware is cleaned up */
	if (!pdev->irq) {
3666
		ep_err(dev, "No IRQ.  Check PCI setup!\n");
L
Linus Torvalds 已提交
3667 3668 3669
		retval = -ENODEV;
		goto done;
	}
D
David S. Miller 已提交
3670

3671
	if (use_msi && dev->pdev->vendor == PCI_VENDOR_ID_PLX)
3672
		if (pci_enable_msi(pdev))
3673
			ep_err(dev, "Failed to enable MSI mode\n");
3674

3675 3676
	if (request_irq(pdev->irq, net2280_irq, IRQF_SHARED,
							driver_name, dev)) {
3677
		ep_err(dev, "request interrupt %d failed\n", pdev->irq);
L
Linus Torvalds 已提交
3678 3679 3680 3681 3682 3683 3684
		retval = -EBUSY;
		goto done;
	}
	dev->got_irq = 1;

	/* DMA setup */
	/* NOTE:  we know only the 32 LSBs of dma addresses may be nonzero */
3685 3686
	dev->requests = pci_pool_create("requests", pdev,
		sizeof(struct net2280_dma),
L
Linus Torvalds 已提交
3687 3688 3689
		0 /* no alignment requirements */,
		0 /* or page-crossing issues */);
	if (!dev->requests) {
3690
		ep_dbg(dev, "can't get request pool\n");
L
Linus Torvalds 已提交
3691 3692 3693 3694 3695 3696
		retval = -ENOMEM;
		goto done;
	}
	for (i = 1; i < 5; i++) {
		struct net2280_dma	*td;

3697 3698
		td = pci_pool_alloc(dev->requests, GFP_KERNEL,
				&dev->ep[i].td_dma);
L
Linus Torvalds 已提交
3699
		if (!td) {
3700
			ep_dbg(dev, "can't get dummy %d\n", i);
L
Linus Torvalds 已提交
3701 3702 3703 3704 3705
			retval = -ENOMEM;
			goto done;
		}
		td->dmacount = 0;	/* not VALID */
		td->dmadesc = td->dmaaddr;
3706
		dev->ep[i].dummy = td;
L
Linus Torvalds 已提交
3707 3708 3709
	}

	/* enable lower-overhead pci memory bursts during DMA */
3710
	if (dev->pdev->vendor == PCI_VENDOR_ID_PLX_LEGACY)
3711 3712 3713 3714 3715 3716 3717 3718
		writel(BIT(DMA_MEMORY_WRITE_AND_INVALIDATE_ENABLE) |
			/*
			 * 256 write retries may not be enough...
			   BIT(PCI_RETRY_ABORT_ENABLE) |
			*/
			BIT(DMA_READ_MULTIPLE_ENABLE) |
			BIT(DMA_READ_LINE_ENABLE),
			&dev->pci->pcimstctl);
L
Linus Torvalds 已提交
3719
	/* erratum 0115 shouldn't appear: Linux inits PCI_LATENCY_TIMER */
3720 3721
	pci_set_master(pdev);
	pci_try_set_mwi(pdev);
L
Linus Torvalds 已提交
3722 3723

	/* ... also flushes any posted pci writes */
3724
	dev->chiprev = get_idx_reg(dev->regs, REG_CHIPREV) & 0xffff;
L
Linus Torvalds 已提交
3725 3726

	/* done */
3727 3728
	ep_info(dev, "%s\n", driver_desc);
	ep_info(dev, "irq %d, pci mem %p, chip rev %04x\n",
D
David S. Miller 已提交
3729
			pdev->irq, base, dev->chiprev);
3730
	ep_info(dev, "version: " DRIVER_VERSION "; dma %s %s\n",
3731 3732 3733
		use_dma	? (use_dma_chaining ? "chaining" : "enabled")
			: "disabled",
		dev->enhanced_mode ? "enhanced mode" : "legacy mode");
3734 3735 3736
	retval = device_create_file(&pdev->dev, &dev_attr_registers);
	if (retval)
		goto done;
L
Linus Torvalds 已提交
3737

3738 3739
	retval = usb_add_gadget_udc_release(&pdev->dev, &dev->gadget,
			gadget_release);
3740 3741
	if (retval)
		goto done;
L
Linus Torvalds 已提交
3742 3743 3744 3745
	return 0;

done:
	if (dev)
3746
		net2280_remove(pdev);
L
Linus Torvalds 已提交
3747 3748 3749
	return retval;
}

3750 3751 3752 3753
/* make sure the board is quiescent; otherwise it will continue
 * generating IRQs across the upcoming reboot.
 */

3754
static void net2280_shutdown(struct pci_dev *pdev)
3755
{
3756
	struct net2280		*dev = pci_get_drvdata(pdev);
3757 3758

	/* disable IRQs */
3759 3760
	writel(0, &dev->regs->pciirqenb0);
	writel(0, &dev->regs->pciirqenb1);
3761 3762

	/* disable the pullup so the host will think we're gone */
3763
	writel(0, &dev->usb->usbctl);
3764 3765

	/* Disable full-speed test mode */
3766
	if (dev->pdev->vendor == PCI_VENDOR_ID_PLX_LEGACY)
3767
		writel(0, &dev->usb->xcvrdiag);
3768 3769
}

L
Linus Torvalds 已提交
3770 3771 3772

/*-------------------------------------------------------------------------*/

3773
static const struct pci_device_id pci_ids[] = { {
3774 3775
	.class =	((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
	.class_mask =	~0,
3776
	.vendor =	PCI_VENDOR_ID_PLX_LEGACY,
L
Linus Torvalds 已提交
3777 3778 3779
	.device =	0x2280,
	.subvendor =	PCI_ANY_ID,
	.subdevice =	PCI_ANY_ID,
3780
	}, {
3781 3782
	.class =	((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
	.class_mask =	~0,
3783
	.vendor =	PCI_VENDOR_ID_PLX_LEGACY,
3784 3785 3786
	.device =	0x2282,
	.subvendor =	PCI_ANY_ID,
	.subdevice =	PCI_ANY_ID,
3787
	},
3788
	{
3789 3790 3791 3792 3793 3794
	.class =	((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
	.class_mask =	~0,
	.vendor =	PCI_VENDOR_ID_PLX,
	.device =	0x3380,
	.subvendor =	PCI_ANY_ID,
	.subdevice =	PCI_ANY_ID,
3795 3796
	 },
	{
3797 3798 3799 3800 3801 3802
	.class =	((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
	.class_mask =	~0,
	.vendor =	PCI_VENDOR_ID_PLX,
	.device =	0x3382,
	.subvendor =	PCI_ANY_ID,
	.subdevice =	PCI_ANY_ID,
3803 3804
	 },
{ /* end: all zeroes */ }
L
Linus Torvalds 已提交
3805
};
3806
MODULE_DEVICE_TABLE(pci, pci_ids);
L
Linus Torvalds 已提交
3807 3808 3809 3810 3811 3812 3813 3814

/* pci driver glue; this is a "new style" PCI driver module */
static struct pci_driver net2280_pci_driver = {
	.name =		(char *) driver_name,
	.id_table =	pci_ids,

	.probe =	net2280_probe,
	.remove =	net2280_remove,
3815
	.shutdown =	net2280_shutdown,
L
Linus Torvalds 已提交
3816 3817 3818 3819

	/* FIXME add power management support */
};

3820 3821
module_pci_driver(net2280_pci_driver);

3822 3823 3824
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_AUTHOR("David Brownell");
MODULE_LICENSE("GPL");