pxa25x_udc.c 56.3 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
D
David Brownell 已提交
2
 * Intel PXA25x and IXP4xx on-chip full speed USB device controllers
L
Linus Torvalds 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15
 *
 * Copyright (C) 2002 Intrinsyc, Inc. (Frank Becker)
 * Copyright (C) 2003 Robert Schwebel, Pengutronix
 * Copyright (C) 2003 Benedikt Spranger, Pengutronix
 * Copyright (C) 2003 David Brownell
 * Copyright (C) 2003 Joshua Wise
 *
 * 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.
 */

16
/* #define VERBOSE_DEBUG */
L
Linus Torvalds 已提交
17

18
#include <linux/device.h>
19
#include <linux/gpio.h>
L
Linus Torvalds 已提交
20 21 22 23 24
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/ioport.h>
#include <linux/types.h>
#include <linux/errno.h>
25
#include <linux/err.h>
L
Linus Torvalds 已提交
26 27 28 29 30 31
#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/timer.h>
#include <linux/list.h>
#include <linux/interrupt.h>
#include <linux/mm.h>
32
#include <linux/platform_data/pxa2xx_udc.h>
33
#include <linux/platform_device.h>
L
Linus Torvalds 已提交
34
#include <linux/dma-mapping.h>
35
#include <linux/irq.h>
36
#include <linux/clk.h>
37 38
#include <linux/seq_file.h>
#include <linux/debugfs.h>
39
#include <linux/io.h>
40
#include <linux/prefetch.h>
L
Linus Torvalds 已提交
41 42 43 44 45 46

#include <asm/byteorder.h>
#include <asm/dma.h>
#include <asm/mach-types.h>
#include <asm/unaligned.h>

47
#include <linux/usb/ch9.h>
48
#include <linux/usb/gadget.h>
49
#include <linux/usb/otg.h>
L
Linus Torvalds 已提交
50

51 52 53 54
/*
 * This driver is PXA25x only.  Grab the right register definitions.
 */
#ifdef CONFIG_ARCH_PXA
55
#include <mach/pxa25x-udc.h>
56
#include <mach/hardware.h>
57 58
#endif

59 60 61 62
#ifdef CONFIG_ARCH_LUBBOCK
#include <mach/lubbock.h>
#endif

L
Linus Torvalds 已提交
63
/*
D
David Brownell 已提交
64
 * This driver handles the USB Device Controller (UDC) in Intel's PXA 25x
L
Linus Torvalds 已提交
65 66 67 68 69 70 71 72
 * series processors.  The UDC for the IXP 4xx series is very similar.
 * There are fifteen endpoints, in addition to ep0.
 *
 * Such controller drivers work with a gadget driver.  The gadget driver
 * returns descriptors, implements configuration and data protocols used
 * by the host to interact with this device, and allocates endpoints to
 * the different protocol interfaces.  The controller driver virtualizes
 * usb hardware so that the gadget drivers will be more portable.
73
 *
L
Linus Torvalds 已提交
74 75 76 77
 * This UDC hardware wants to implement a bit too much USB protocol, so
 * it constrains the sorts of USB configuration change events that work.
 * The errata for these chips are misleading; some "fixed" bugs from
 * pxa250 a0/a1 b0/b1/b2 sure act like they're still there.
78 79 80 81 82 83 84 85
 *
 * Note that the UDC hardware supports DMA (except on IXP) but that's
 * not used here.  IN-DMA (to host) is simple enough, when the data is
 * suitably aligned (16 bytes) ... the network stack doesn't do that,
 * other software can.  OUT-DMA is buggy in most chip versions, as well
 * as poorly designed (data toggle not automatic).  So this driver won't
 * bother using DMA.  (Mostly-working IN-DMA support was available in
 * kernels before 2.6.23, but was never enabled or well tested.)
L
Linus Torvalds 已提交
86 87
 */

88
#define	DRIVER_VERSION	"30-June-2007"
D
David Brownell 已提交
89
#define	DRIVER_DESC	"PXA 25x USB Device Controller driver"
L
Linus Torvalds 已提交
90 91


92
static const char driver_name [] = "pxa25x_udc";
L
Linus Torvalds 已提交
93 94 95 96 97 98 99 100 101 102 103

static const char ep0name [] = "ep0";


#ifdef CONFIG_ARCH_IXP4XX

/* cpu-specific register addresses are compiled in to this code */
#ifdef CONFIG_ARCH_PXA
#error "Can't configure both IXP and PXA"
#endif

104 105 106 107 108 109
/* IXP doesn't yet support <linux/clk.h> */
#define clk_get(dev,name)	NULL
#define clk_enable(clk)		do { } while (0)
#define clk_disable(clk)	do { } while (0)
#define clk_put(clk)		do { } while (0)

L
Linus Torvalds 已提交
110 111
#endif

112
#include "pxa25x_udc.h"
L
Linus Torvalds 已提交
113 114


115
#ifdef	CONFIG_USB_PXA25X_SMALL
L
Linus Torvalds 已提交
116 117 118 119 120 121
#define SIZE_STR	" (small)"
#else
#define SIZE_STR	""
#endif

/* ---------------------------------------------------------------------------
122
 *	endpoint related parts of the api to the usb controller hardware,
L
Linus Torvalds 已提交
123 124 125 126
 *	used by gadget driver; and the inner talker-to-hardware core.
 * ---------------------------------------------------------------------------
 */

127 128
static void pxa25x_ep_fifo_flush (struct usb_ep *ep);
static void nuke (struct pxa25x_ep *, int status);
L
Linus Torvalds 已提交
129

130 131 132 133
/* one GPIO should control a D+ pullup, so host sees this device (or not) */
static void pullup_off(void)
{
	struct pxa2xx_udc_mach_info		*mach = the_controller->mach;
134
	int off_level = mach->gpio_pullup_inverted;
135

136
	if (gpio_is_valid(mach->gpio_pullup))
137
		gpio_set_value(mach->gpio_pullup, off_level);
138 139 140 141 142 143 144
	else if (mach->udc_command)
		mach->udc_command(PXA2XX_UDC_CMD_DISCONNECT);
}

static void pullup_on(void)
{
	struct pxa2xx_udc_mach_info		*mach = the_controller->mach;
145
	int on_level = !mach->gpio_pullup_inverted;
146

147
	if (gpio_is_valid(mach->gpio_pullup))
148
		gpio_set_value(mach->gpio_pullup, on_level);
149 150 151 152
	else if (mach->udc_command)
		mach->udc_command(PXA2XX_UDC_CMD_CONNECT);
}

L
Linus Torvalds 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
static void pio_irq_enable(int bEndpointAddress)
{
        bEndpointAddress &= 0xf;
        if (bEndpointAddress < 8)
                UICR0 &= ~(1 << bEndpointAddress);
        else {
                bEndpointAddress -= 8;
                UICR1 &= ~(1 << bEndpointAddress);
	}
}

static void pio_irq_disable(int bEndpointAddress)
{
        bEndpointAddress &= 0xf;
        if (bEndpointAddress < 8)
                UICR0 |= 1 << bEndpointAddress;
        else {
                bEndpointAddress -= 8;
                UICR1 |= 1 << bEndpointAddress;
        }
}

/* The UDCCR reg contains mask and interrupt status bits,
 * so using '|=' isn't safe as it may ack an interrupt.
 */
#define UDCCR_MASK_BITS         (UDCCR_REM | UDCCR_SRM | UDCCR_UDE)

static inline void udc_set_mask_UDCCR(int mask)
{
	UDCCR = (UDCCR & UDCCR_MASK_BITS) | (mask & UDCCR_MASK_BITS);
}

static inline void udc_clear_mask_UDCCR(int mask)
{
	UDCCR = (UDCCR & UDCCR_MASK_BITS) & ~(mask & UDCCR_MASK_BITS);
}

static inline void udc_ack_int_UDCCR(int mask)
{
	/* udccr contains the bits we dont want to change */
	__u32 udccr = UDCCR & UDCCR_MASK_BITS;

	UDCCR = udccr | (mask & ~UDCCR_MASK_BITS);
}

/*
 * endpoint enable/disable
 *
201
 * we need to verify the descriptors used to enable endpoints.  since pxa25x
L
Linus Torvalds 已提交
202 203 204
 * endpoint configurations are fixed, and are pretty much always enabled,
 * there's not a lot to manage here.
 *
205
 * because pxa25x can't selectively initialize bulk (or interrupt) endpoints,
L
Linus Torvalds 已提交
206 207 208 209 210 211
 * (resetting endpoint halt and toggle), SET_INTERFACE is unusable except
 * for a single interface (with only the default altsetting) and for gadget
 * drivers that don't halt endpoints (not reset by set_interface).  that also
 * means that if you use ISO, you must violate the USB spec rule that all
 * iso endpoints must be in non-default altsettings.
 */
212
static int pxa25x_ep_enable (struct usb_ep *_ep,
L
Linus Torvalds 已提交
213 214
		const struct usb_endpoint_descriptor *desc)
{
215 216
	struct pxa25x_ep        *ep;
	struct pxa25x_udc       *dev;
L
Linus Torvalds 已提交
217

218
	ep = container_of (_ep, struct pxa25x_ep, ep);
219
	if (!_ep || !desc || _ep->name == ep0name
L
Linus Torvalds 已提交
220 221
			|| desc->bDescriptorType != USB_DT_ENDPOINT
			|| ep->bEndpointAddress != desc->bEndpointAddress
222
			|| ep->fifo_size < usb_endpoint_maxp (desc)) {
223
		DMSG("%s, bad ep or descriptor\n", __func__);
L
Linus Torvalds 已提交
224 225 226 227 228 229 230
		return -EINVAL;
	}

	/* xfer types must match, except that interrupt ~= bulk */
	if (ep->bmAttributes != desc->bmAttributes
			&& ep->bmAttributes != USB_ENDPOINT_XFER_BULK
			&& desc->bmAttributes != USB_ENDPOINT_XFER_INT) {
231
		DMSG("%s, %s type mismatch\n", __func__, _ep->name);
L
Linus Torvalds 已提交
232 233 234 235 236
		return -EINVAL;
	}

	/* hardware _could_ do smaller, but driver doesn't */
	if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
237
				&& usb_endpoint_maxp (desc)
L
Linus Torvalds 已提交
238 239
						!= BULK_FIFO_SIZE)
			|| !desc->wMaxPacketSize) {
240
		DMSG("%s, bad %s maxpacket\n", __func__, _ep->name);
L
Linus Torvalds 已提交
241 242 243 244 245
		return -ERANGE;
	}

	dev = ep->dev;
	if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
246
		DMSG("%s, bogus device state\n", __func__);
L
Linus Torvalds 已提交
247 248 249
		return -ESHUTDOWN;
	}

250
	ep->ep.desc = desc;
L
Linus Torvalds 已提交
251
	ep->stopped = 0;
252
	ep->pio_irqs = 0;
253
	ep->ep.maxpacket = usb_endpoint_maxp (desc);
L
Linus Torvalds 已提交
254 255

	/* flush fifo (mostly for OUT buffers) */
256
	pxa25x_ep_fifo_flush (_ep);
L
Linus Torvalds 已提交
257 258 259 260 261 262 263

	/* ... reset halt state too, if we could ... */

	DBG(DBG_VERBOSE, "enabled %s\n", _ep->name);
	return 0;
}

264
static int pxa25x_ep_disable (struct usb_ep *_ep)
L
Linus Torvalds 已提交
265
{
266
	struct pxa25x_ep	*ep;
D
David Brownell 已提交
267
	unsigned long		flags;
L
Linus Torvalds 已提交
268

269
	ep = container_of (_ep, struct pxa25x_ep, ep);
270
	if (!_ep || !ep->ep.desc) {
271
		DMSG("%s, %s not enabled\n", __func__,
L
Linus Torvalds 已提交
272 273 274
			_ep ? ep->ep.name : NULL);
		return -EINVAL;
	}
D
David Brownell 已提交
275 276
	local_irq_save(flags);

L
Linus Torvalds 已提交
277 278 279
	nuke (ep, -ESHUTDOWN);

	/* flush fifo (mostly for IN buffers) */
280
	pxa25x_ep_fifo_flush (_ep);
L
Linus Torvalds 已提交
281

282
	ep->ep.desc = NULL;
L
Linus Torvalds 已提交
283 284
	ep->stopped = 1;

D
David Brownell 已提交
285
	local_irq_restore(flags);
L
Linus Torvalds 已提交
286 287 288 289 290 291
	DBG(DBG_VERBOSE, "%s disabled\n", _ep->name);
	return 0;
}

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

292
/* for the pxa25x, these can just wrap kmalloc/kfree.  gadget drivers
L
Linus Torvalds 已提交
293 294 295 296 297
 * must still pass correctly initialized endpoints, since other controller
 * drivers may care about how it's currently set up (dma issues etc).
 */

/*
298
 *	pxa25x_ep_alloc_request - allocate a request data structure
L
Linus Torvalds 已提交
299 300
 */
static struct usb_request *
301
pxa25x_ep_alloc_request (struct usb_ep *_ep, gfp_t gfp_flags)
L
Linus Torvalds 已提交
302
{
303
	struct pxa25x_request *req;
L
Linus Torvalds 已提交
304

305
	req = kzalloc(sizeof(*req), gfp_flags);
L
Linus Torvalds 已提交
306 307 308 309 310 311 312 313 314
	if (!req)
		return NULL;

	INIT_LIST_HEAD (&req->queue);
	return &req->req;
}


/*
315
 *	pxa25x_ep_free_request - deallocate a request data structure
L
Linus Torvalds 已提交
316 317
 */
static void
318
pxa25x_ep_free_request (struct usb_ep *_ep, struct usb_request *_req)
L
Linus Torvalds 已提交
319
{
320
	struct pxa25x_request	*req;
L
Linus Torvalds 已提交
321

322
	req = container_of (_req, struct pxa25x_request, req);
323
	WARN_ON(!list_empty (&req->queue));
L
Linus Torvalds 已提交
324 325 326 327 328 329 330 331
	kfree(req);
}

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

/*
 *	done - retire a request; caller blocked irqs
 */
332
static void done(struct pxa25x_ep *ep, struct pxa25x_request *req, int status)
L
Linus Torvalds 已提交
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
{
	unsigned		stopped = ep->stopped;

	list_del_init(&req->queue);

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

	if (status && status != -ESHUTDOWN)
		DBG(DBG_VERBOSE, "complete %s req %p stat %d len %u/%u\n",
			ep->ep.name, &req->req, status,
			req->req.actual, req->req.length);

	/* don't modify queue heads during completion callback */
	ep->stopped = 1;
	req->req.complete(&ep->ep, &req->req);
	ep->stopped = stopped;
}


355
static inline void ep0_idle (struct pxa25x_udc *dev)
L
Linus Torvalds 已提交
356 357 358 359 360
{
	dev->ep0state = EP0_IDLE;
}

static int
361
write_packet(volatile u32 *uddr, struct pxa25x_request *req, unsigned max)
L
Linus Torvalds 已提交
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
{
	u8		*buf;
	unsigned	length, count;

	buf = req->req.buf + req->req.actual;
	prefetch(buf);

	/* how big will this packet be? */
	length = min(req->req.length - req->req.actual, max);
	req->req.actual += length;

	count = length;
	while (likely(count--))
		*uddr = *buf++;

	return length;
}

/*
 * write to an IN endpoint fifo, as many packets as possible.
 * irqs will use this to write the rest later.
 * caller guarantees at least one packet buffer is ready (or a zlp).
 */
static int
386
write_fifo (struct pxa25x_ep *ep, struct pxa25x_request *req)
L
Linus Torvalds 已提交
387 388 389
{
	unsigned		max;

390
	max = usb_endpoint_maxp(ep->ep.desc);
L
Linus Torvalds 已提交
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
	do {
		unsigned	count;
		int		is_last, is_short;

		count = write_packet(ep->reg_uddr, req, max);

		/* last packet is usually short (or a zlp) */
		if (unlikely (count != max))
			is_last = is_short = 1;
		else {
			if (likely(req->req.length != req->req.actual)
					|| req->req.zero)
				is_last = 0;
			else
				is_last = 1;
			/* interrupt/iso maxpacket may not fill the fifo */
			is_short = unlikely (max < ep->fifo_size);
		}

		DBG(DBG_VERY_NOISY, "wrote %s %d bytes%s%s %d left %p\n",
			ep->ep.name, count,
			is_last ? "/L" : "", is_short ? "/S" : "",
			req->req.length - req->req.actual, req);

		/* let loose that packet. maybe try writing another one,
		 * double buffering might work.  TSP, TPC, and TFS
		 * bit values are the same for all normal IN endpoints.
		 */
		*ep->reg_udccs = UDCCS_BI_TPC;
		if (is_short)
			*ep->reg_udccs = UDCCS_BI_TSP;

		/* requests complete when all IN data is in the FIFO */
		if (is_last) {
			done (ep, req, 0);
426
			if (list_empty(&ep->queue))
L
Linus Torvalds 已提交
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
				pio_irq_disable (ep->bEndpointAddress);
			return 1;
		}

		// TODO experiment: how robust can fifo mode tweaking be?
		// double buffering is off in the default fifo mode, which
		// prevents TFS from being set here.

	} while (*ep->reg_udccs & UDCCS_BI_TFS);
	return 0;
}

/* caller asserts req->pending (ep0 irq status nyet cleared); starts
 * ep0 data stage.  these chips want very simple state transitions.
 */
static inline
443
void ep0start(struct pxa25x_udc *dev, u32 flags, const char *tag)
L
Linus Torvalds 已提交
444 445 446 447 448
{
	UDCCS0 = flags|UDCCS0_SA|UDCCS0_OPR;
	USIR0 = USIR0_IR0;
	dev->req_pending = 0;
	DBG(DBG_VERY_NOISY, "%s %s, %02x/%02x\n",
449
		__func__, tag, UDCCS0, flags);
L
Linus Torvalds 已提交
450 451 452
}

static int
453
write_ep0_fifo (struct pxa25x_ep *ep, struct pxa25x_request *req)
L
Linus Torvalds 已提交
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
{
	unsigned	count;
	int		is_short;

	count = write_packet(&UDDR0, req, EP0_FIFO_SIZE);
	ep->dev->stats.write.bytes += count;

	/* last packet "must be" short (or a zlp) */
	is_short = (count != EP0_FIFO_SIZE);

	DBG(DBG_VERY_NOISY, "ep0in %d bytes %d left %p\n", count,
		req->req.length - req->req.actual, req);

	if (unlikely (is_short)) {
		if (ep->dev->req_pending)
			ep0start(ep->dev, UDCCS0_IPR, "short IN");
		else
			UDCCS0 = UDCCS0_IPR;

		count = req->req.length;
		done (ep, req, 0);
		ep0_idle(ep->dev);
476
#ifndef CONFIG_ARCH_IXP4XX
L
Linus Torvalds 已提交
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
#if 1
		/* This seems to get rid of lost status irqs in some cases:
		 * host responds quickly, or next request involves config
		 * change automagic, or should have been hidden, or ...
		 *
		 * FIXME get rid of all udelays possible...
		 */
		if (count >= EP0_FIFO_SIZE) {
			count = 100;
			do {
				if ((UDCCS0 & UDCCS0_OPR) != 0) {
					/* clear OPR, generate ack */
					UDCCS0 = UDCCS0_OPR;
					break;
				}
				count--;
				udelay(1);
			} while (count);
		}
496
#endif
L
Linus Torvalds 已提交
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
#endif
	} else if (ep->dev->req_pending)
		ep0start(ep->dev, 0, "IN");
	return is_short;
}


/*
 * read_fifo -  unload packet(s) from the fifo we use for usb OUT
 * transfers and put them into the request.  caller should have made
 * sure there's at least one packet ready.
 *
 * returns true if the request completed because of short packet or the
 * request buffer having filled (and maybe overran till end-of-packet).
 */
static int
513
read_fifo (struct pxa25x_ep *ep, struct pxa25x_request *req)
L
Linus Torvalds 已提交
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
{
	for (;;) {
		u32		udccs;
		u8		*buf;
		unsigned	bufferspace, count, is_short;

		/* make sure there's a packet in the FIFO.
		 * UDCCS_{BO,IO}_RPC are all the same bit value.
		 * UDCCS_{BO,IO}_RNE are all the same bit value.
		 */
		udccs = *ep->reg_udccs;
		if (unlikely ((udccs & UDCCS_BO_RPC) == 0))
			break;
		buf = req->req.buf + req->req.actual;
		prefetchw(buf);
		bufferspace = req->req.length - req->req.actual;

		/* read all bytes from this packet */
		if (likely (udccs & UDCCS_BO_RNE)) {
			count = 1 + (0x0ff & *ep->reg_ubcr);
			req->req.actual += min (count, bufferspace);
		} else /* zlp */
			count = 0;
		is_short = (count < ep->ep.maxpacket);
		DBG(DBG_VERY_NOISY, "read %s %02x, %d bytes%s req %p %d/%d\n",
			ep->ep.name, udccs, count,
			is_short ? "/S" : "",
			req, req->req.actual, req->req.length);
		while (likely (count-- != 0)) {
			u8	byte = (u8) *ep->reg_uddr;

			if (unlikely (bufferspace == 0)) {
				/* this happens when the driver's buffer
				 * is smaller than what the host sent.
				 * discard the extra data.
				 */
				if (req->req.status != -EOVERFLOW)
					DMSG("%s overflow %d\n",
						ep->ep.name, count);
				req->req.status = -EOVERFLOW;
			} else {
				*buf++ = byte;
				bufferspace--;
			}
		}
		*ep->reg_udccs =  UDCCS_BO_RPC;
		/* RPC/RSP/RNE could now reflect the other packet buffer */

		/* iso is one request per packet */
		if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
			if (udccs & UDCCS_IO_ROF)
				req->req.status = -EHOSTUNREACH;
			/* more like "is_done" */
			is_short = 1;
		}

		/* completion */
		if (is_short || req->req.actual == req->req.length) {
			done (ep, req, 0);
			if (list_empty(&ep->queue))
				pio_irq_disable (ep->bEndpointAddress);
			return 1;
		}

		/* finished that packet.  the next one may be waiting... */
	}
	return 0;
}

/*
 * special ep0 version of the above.  no UBCR0 or double buffering; status
 * handshaking is magic.  most device protocols don't need control-OUT.
 * CDC vendor commands (and RNDIS), mass storage CB/CBI, and some other
 * protocols do use them.
 */
static int
590
read_ep0_fifo (struct pxa25x_ep *ep, struct pxa25x_request *req)
L
Linus Torvalds 已提交
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
{
	u8		*buf, byte;
	unsigned	bufferspace;

	buf = req->req.buf + req->req.actual;
	bufferspace = req->req.length - req->req.actual;

	while (UDCCS0 & UDCCS0_RNE) {
		byte = (u8) UDDR0;

		if (unlikely (bufferspace == 0)) {
			/* this happens when the driver's buffer
			 * is smaller than what the host sent.
			 * discard the extra data.
			 */
			if (req->req.status != -EOVERFLOW)
				DMSG("%s overflow\n", ep->ep.name);
			req->req.status = -EOVERFLOW;
		} else {
			*buf++ = byte;
			req->req.actual++;
			bufferspace--;
		}
	}

	UDCCS0 = UDCCS0_OPR | UDCCS0_IPR;

	/* completion */
	if (req->req.actual >= req->req.length)
		return 1;

	/* finished that packet.  the next one may be waiting... */
	return 0;
}

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

static int
629
pxa25x_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
L
Linus Torvalds 已提交
630
{
631 632 633
	struct pxa25x_request	*req;
	struct pxa25x_ep	*ep;
	struct pxa25x_udc	*dev;
L
Linus Torvalds 已提交
634 635
	unsigned long		flags;

636
	req = container_of(_req, struct pxa25x_request, req);
L
Linus Torvalds 已提交
637 638
	if (unlikely (!_req || !_req->complete || !_req->buf
			|| !list_empty(&req->queue))) {
639
		DMSG("%s, bad params\n", __func__);
L
Linus Torvalds 已提交
640 641 642
		return -EINVAL;
	}

643
	ep = container_of(_ep, struct pxa25x_ep, ep);
644
	if (unlikely(!_ep || (!ep->ep.desc && ep->ep.name != ep0name))) {
645
		DMSG("%s, bad ep\n", __func__);
L
Linus Torvalds 已提交
646 647 648 649 650 651
		return -EINVAL;
	}

	dev = ep->dev;
	if (unlikely (!dev->driver
			|| dev->gadget.speed == USB_SPEED_UNKNOWN)) {
652
		DMSG("%s, bogus device state\n", __func__);
L
Linus Torvalds 已提交
653 654 655 656 657 658 659
		return -ESHUTDOWN;
	}

	/* iso is always one packet per request, that's the only way
	 * we can report per-packet status.  that also helps with dma.
	 */
	if (unlikely (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
660
			&& req->req.length > usb_endpoint_maxp(ep->ep.desc)))
L
Linus Torvalds 已提交
661 662 663
		return -EMSGSIZE;

	DBG(DBG_NOISY, "%s queue req %p, len %d buf %p\n",
664
		_ep->name, _req, _req->length, _req->buf);
L
Linus Torvalds 已提交
665 666 667 668 669 670 671 672

	local_irq_save(flags);

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

	/* kickstart this i/o queue? */
	if (list_empty(&ep->queue) && !ep->stopped) {
673
		if (ep->ep.desc == NULL/* ep0 */) {
L
Linus Torvalds 已提交
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
			unsigned	length = _req->length;

			switch (dev->ep0state) {
			case EP0_IN_DATA_PHASE:
				dev->stats.write.ops++;
				if (write_ep0_fifo(ep, req))
					req = NULL;
				break;

			case EP0_OUT_DATA_PHASE:
				dev->stats.read.ops++;
				/* messy ... */
				if (dev->req_config) {
					DBG(DBG_VERBOSE, "ep0 config ack%s\n",
						dev->has_cfr ?  "" : " raced");
					if (dev->has_cfr)
						UDCCFR = UDCCFR_AREN|UDCCFR_ACM
							|UDCCFR_MB1;
					done(ep, req, 0);
					dev->ep0state = EP0_END_XFER;
					local_irq_restore (flags);
					return 0;
				}
				if (dev->req_pending)
					ep0start(dev, UDCCS0_IPR, "OUT");
				if (length == 0 || ((UDCCS0 & UDCCS0_RNE) != 0
						&& read_ep0_fifo(ep, req))) {
					ep0_idle(dev);
					done(ep, req, 0);
					req = NULL;
				}
				break;

			default:
				DMSG("ep0 i/o, odd state %d\n", dev->ep0state);
				local_irq_restore (flags);
				return -EL2HLT;
			}
		/* can the FIFO can satisfy the request immediately? */
D
David Brownell 已提交
713 714 715 716
		} else if ((ep->bEndpointAddress & USB_DIR_IN) != 0) {
			if ((*ep->reg_udccs & UDCCS_BI_TFS) != 0
					&& write_fifo(ep, req))
				req = NULL;
L
Linus Torvalds 已提交
717 718 719 720 721
		} else if ((*ep->reg_udccs & UDCCS_BO_RFS) != 0
				&& read_fifo(ep, req)) {
			req = NULL;
		}

722
		if (likely(req && ep->ep.desc))
L
Linus Torvalds 已提交
723 724 725 726
			pio_irq_enable(ep->bEndpointAddress);
	}

	/* pio or dma irq handler advances the queue. */
727
	if (likely(req != NULL))
L
Linus Torvalds 已提交
728 729 730 731 732 733 734 735
		list_add_tail(&req->queue, &ep->queue);
	local_irq_restore(flags);

	return 0;
}


/*
736
 *	nuke - dequeue ALL requests
L
Linus Torvalds 已提交
737
 */
738
static void nuke(struct pxa25x_ep *ep, int status)
L
Linus Torvalds 已提交
739
{
740
	struct pxa25x_request *req;
L
Linus Torvalds 已提交
741 742 743 744

	/* called with irqs blocked */
	while (!list_empty(&ep->queue)) {
		req = list_entry(ep->queue.next,
745
				struct pxa25x_request,
L
Linus Torvalds 已提交
746 747 748
				queue);
		done(ep, req, status);
	}
749
	if (ep->ep.desc)
L
Linus Torvalds 已提交
750 751 752 753 754
		pio_irq_disable (ep->bEndpointAddress);
}


/* dequeue JUST ONE request */
755
static int pxa25x_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
L
Linus Torvalds 已提交
756
{
757 758
	struct pxa25x_ep	*ep;
	struct pxa25x_request	*req;
L
Linus Torvalds 已提交
759 760
	unsigned long		flags;

761
	ep = container_of(_ep, struct pxa25x_ep, ep);
L
Linus Torvalds 已提交
762 763 764 765 766 767 768 769 770 771 772 773 774 775 776
	if (!_ep || ep->ep.name == ep0name)
		return -EINVAL;

	local_irq_save(flags);

	/* make sure it's actually queued on this endpoint */
	list_for_each_entry (req, &ep->queue, queue) {
		if (&req->req == _req)
			break;
	}
	if (&req->req != _req) {
		local_irq_restore(flags);
		return -EINVAL;
	}

777
	done(ep, req, -ECONNRESET);
L
Linus Torvalds 已提交
778 779 780 781 782 783 784

	local_irq_restore(flags);
	return 0;
}

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

785
static int pxa25x_ep_set_halt(struct usb_ep *_ep, int value)
L
Linus Torvalds 已提交
786
{
787
	struct pxa25x_ep	*ep;
L
Linus Torvalds 已提交
788 789
	unsigned long		flags;

790
	ep = container_of(_ep, struct pxa25x_ep, ep);
L
Linus Torvalds 已提交
791
	if (unlikely (!_ep
792
			|| (!ep->ep.desc && ep->ep.name != ep0name))
L
Linus Torvalds 已提交
793
			|| ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
794
		DMSG("%s, bad ep\n", __func__);
L
Linus Torvalds 已提交
795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
		return -EINVAL;
	}
	if (value == 0) {
		/* this path (reset toggle+halt) is needed to implement
		 * SET_INTERFACE on normal hardware.  but it can't be
		 * done from software on the PXA UDC, and the hardware
		 * forgets to do it as part of SET_INTERFACE automagic.
		 */
		DMSG("only host can clear %s halt\n", _ep->name);
		return -EROFS;
	}

	local_irq_save(flags);

	if ((ep->bEndpointAddress & USB_DIR_IN) != 0
			&& ((*ep->reg_udccs & UDCCS_BI_TFS) == 0
			   || !list_empty(&ep->queue))) {
		local_irq_restore(flags);
		return -EAGAIN;
	}

	/* FST bit is the same for control, bulk in, bulk out, interrupt in */
	*ep->reg_udccs = UDCCS_BI_FST|UDCCS_BI_FTF;

	/* ep0 needs special care */
820
	if (!ep->ep.desc) {
L
Linus Torvalds 已提交
821 822 823 824
		start_watchdog(ep->dev);
		ep->dev->req_pending = 0;
		ep->dev->ep0state = EP0_STALL;

825 826 827 828 829 830 831 832 833 834
	/* and bulk/intr endpoints like dropping stalls too */
	} else {
		unsigned i;
		for (i = 0; i < 1000; i += 20) {
			if (*ep->reg_udccs & UDCCS_BI_SST)
				break;
			udelay(20);
		}
	}
	local_irq_restore(flags);
L
Linus Torvalds 已提交
835 836 837 838 839

	DBG(DBG_VERBOSE, "%s halt\n", _ep->name);
	return 0;
}

840
static int pxa25x_ep_fifo_status(struct usb_ep *_ep)
L
Linus Torvalds 已提交
841
{
842
	struct pxa25x_ep        *ep;
L
Linus Torvalds 已提交
843

844
	ep = container_of(_ep, struct pxa25x_ep, ep);
L
Linus Torvalds 已提交
845
	if (!_ep) {
846
		DMSG("%s, bad ep\n", __func__);
L
Linus Torvalds 已提交
847 848 849 850 851 852 853 854 855 856 857 858
		return -ENODEV;
	}
	/* pxa can't report unclaimed bytes from IN fifos */
	if ((ep->bEndpointAddress & USB_DIR_IN) != 0)
		return -EOPNOTSUPP;
	if (ep->dev->gadget.speed == USB_SPEED_UNKNOWN
			|| (*ep->reg_udccs & UDCCS_BO_RFS) == 0)
		return 0;
	else
		return (*ep->reg_ubcr & 0xfff) + 1;
}

859
static void pxa25x_ep_fifo_flush(struct usb_ep *_ep)
L
Linus Torvalds 已提交
860
{
861
	struct pxa25x_ep        *ep;
L
Linus Torvalds 已提交
862

863
	ep = container_of(_ep, struct pxa25x_ep, ep);
L
Linus Torvalds 已提交
864
	if (!_ep || ep->ep.name == ep0name || !list_empty(&ep->queue)) {
865
		DMSG("%s, bad ep\n", __func__);
L
Linus Torvalds 已提交
866 867 868 869 870 871 872 873 874 875 876 877 878 879
		return;
	}

	/* toggle and halt bits stay unchanged */

	/* for OUT, just read and discard the FIFO contents. */
	if ((ep->bEndpointAddress & USB_DIR_IN) == 0) {
		while (((*ep->reg_udccs) & UDCCS_BO_RNE) != 0)
			(void) *ep->reg_uddr;
		return;
	}

	/* most IN status is the same, but ISO can't stall */
	*ep->reg_udccs = UDCCS_BI_TPC|UDCCS_BI_FTF|UDCCS_BI_TUR
880 881
		| (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
			? 0 : UDCCS_BI_SST);
L
Linus Torvalds 已提交
882 883 884
}


885 886 887
static struct usb_ep_ops pxa25x_ep_ops = {
	.enable		= pxa25x_ep_enable,
	.disable	= pxa25x_ep_disable,
L
Linus Torvalds 已提交
888

889 890
	.alloc_request	= pxa25x_ep_alloc_request,
	.free_request	= pxa25x_ep_free_request,
L
Linus Torvalds 已提交
891

892 893
	.queue		= pxa25x_ep_queue,
	.dequeue	= pxa25x_ep_dequeue,
L
Linus Torvalds 已提交
894

895 896 897
	.set_halt	= pxa25x_ep_set_halt,
	.fifo_status	= pxa25x_ep_fifo_status,
	.fifo_flush	= pxa25x_ep_fifo_flush,
L
Linus Torvalds 已提交
898 899 900 901
};


/* ---------------------------------------------------------------------------
902
 *	device-scoped parts of the api to the usb controller hardware
L
Linus Torvalds 已提交
903 904 905
 * ---------------------------------------------------------------------------
 */

906
static int pxa25x_udc_get_frame(struct usb_gadget *_gadget)
L
Linus Torvalds 已提交
907 908 909 910
{
	return ((UFNRH & 0x07) << 8) | (UFNRL & 0xff);
}

911
static int pxa25x_udc_wakeup(struct usb_gadget *_gadget)
L
Linus Torvalds 已提交
912 913 914 915 916 917 918 919
{
	/* host may not have enabled remote wakeup */
	if ((UDCCS0 & UDCCS0_DRWF) == 0)
		return -EHOSTUNREACH;
	udc_set_mask_UDCCR(UDCCR_RSM);
	return 0;
}

920 921 922
static void stop_activity(struct pxa25x_udc *, struct usb_gadget_driver *);
static void udc_enable (struct pxa25x_udc *);
static void udc_disable(struct pxa25x_udc *);
L
Linus Torvalds 已提交
923 924

/* We disable the UDC -- and its 48 MHz clock -- whenever it's not
925
 * in active use.
L
Linus Torvalds 已提交
926
 */
927
static int pullup(struct pxa25x_udc *udc)
L
Linus Torvalds 已提交
928
{
929
	int is_active = udc->vbus && udc->pullup && !udc->suspended;
L
Linus Torvalds 已提交
930
	DMSG("%s\n", is_active ? "active" : "inactive");
931 932 933 934 935 936
	if (is_active) {
		if (!udc->active) {
			udc->active = 1;
			/* Enable clock for USB device */
			clk_enable(udc->clk);
			udc_enable(udc);
L
Linus Torvalds 已提交
937
		}
938 939 940 941 942 943 944 945 946 947 948 949 950 951
	} else {
		if (udc->active) {
			if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
				DMSG("disconnect %s\n", udc->driver
					? udc->driver->driver.name
					: "(no driver)");
				stop_activity(udc, udc->driver);
			}
			udc_disable(udc);
			/* Disable clock for USB device */
			clk_disable(udc->clk);
			udc->active = 0;
		}

L
Linus Torvalds 已提交
952 953 954 955 956
	}
	return 0;
}

/* VBUS reporting logically comes from a transceiver */
957
static int pxa25x_udc_vbus_session(struct usb_gadget *_gadget, int is_active)
L
Linus Torvalds 已提交
958
{
959
	struct pxa25x_udc	*udc;
L
Linus Torvalds 已提交
960

961
	udc = container_of(_gadget, struct pxa25x_udc, gadget);
962
	udc->vbus = is_active;
L
Linus Torvalds 已提交
963
	DMSG("vbus %s\n", is_active ? "supplied" : "inactive");
964
	pullup(udc);
L
Linus Torvalds 已提交
965 966 967 968
	return 0;
}

/* drivers may have software control over D+ pullup */
969
static int pxa25x_udc_pullup(struct usb_gadget *_gadget, int is_active)
L
Linus Torvalds 已提交
970
{
971
	struct pxa25x_udc	*udc;
L
Linus Torvalds 已提交
972

973
	udc = container_of(_gadget, struct pxa25x_udc, gadget);
L
Linus Torvalds 已提交
974 975

	/* not all boards support pullup control */
976
	if (!gpio_is_valid(udc->mach->gpio_pullup) && !udc->mach->udc_command)
L
Linus Torvalds 已提交
977 978
		return -EOPNOTSUPP;

979 980
	udc->pullup = (is_active != 0);
	pullup(udc);
L
Linus Torvalds 已提交
981 982 983
	return 0;
}

984 985 986 987 988 989 990 991 992 993
/* boards may consume current from VBUS, up to 100-500mA based on config.
 * the 500uA suspend ceiling means that exclusively vbus-powered PXA designs
 * violate USB specs.
 */
static int pxa25x_udc_vbus_draw(struct usb_gadget *_gadget, unsigned mA)
{
	struct pxa25x_udc	*udc;

	udc = container_of(_gadget, struct pxa25x_udc, gadget);

994
	if (!IS_ERR_OR_NULL(udc->transceiver))
995
		return usb_phy_set_power(udc->transceiver, mA);
996 997 998
	return -EOPNOTSUPP;
}

999 1000 1001 1002
static int pxa25x_udc_start(struct usb_gadget *g,
		struct usb_gadget_driver *driver);
static int pxa25x_udc_stop(struct usb_gadget *g,
		struct usb_gadget_driver *driver);
1003

1004 1005 1006 1007 1008
static const struct usb_gadget_ops pxa25x_udc_ops = {
	.get_frame	= pxa25x_udc_get_frame,
	.wakeup		= pxa25x_udc_wakeup,
	.vbus_session	= pxa25x_udc_vbus_session,
	.pullup		= pxa25x_udc_pullup,
1009
	.vbus_draw	= pxa25x_udc_vbus_draw,
1010 1011
	.udc_start	= pxa25x_udc_start,
	.udc_stop	= pxa25x_udc_stop,
L
Linus Torvalds 已提交
1012 1013 1014 1015
};

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

1016
#ifdef CONFIG_USB_GADGET_DEBUG_FS
L
Linus Torvalds 已提交
1017 1018

static int
1019
udc_seq_show(struct seq_file *m, void *_d)
L
Linus Torvalds 已提交
1020
{
1021
	struct pxa25x_udc	*dev = m->private;
L
Linus Torvalds 已提交
1022
	unsigned long		flags;
1023
	int			i;
L
Linus Torvalds 已提交
1024 1025 1026 1027 1028
	u32			tmp;

	local_irq_save(flags);

	/* basic device status */
1029
	seq_printf(m, DRIVER_DESC "\n"
L
Linus Torvalds 已提交
1030
		"%s version: %s\nGadget driver: %s\nHost %s\n\n",
1031
		driver_name, DRIVER_VERSION SIZE_STR "(pio)",
L
Linus Torvalds 已提交
1032
		dev->driver ? dev->driver->driver.name : "(none)",
1033
		dev->gadget.speed == USB_SPEED_FULL ? "full speed" : "disconnected");
L
Linus Torvalds 已提交
1034 1035

	/* registers for device and ep0 */
1036
	seq_printf(m,
L
Linus Torvalds 已提交
1037 1038 1039 1040
		"uicr %02X.%02X, usir %02X.%02x, ufnr %02X.%02X\n",
		UICR1, UICR0, USIR1, USIR0, UFNRH, UFNRL);

	tmp = UDCCR;
1041
	seq_printf(m,
L
Linus Torvalds 已提交
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
		"udccr %02X =%s%s%s%s%s%s%s%s\n", tmp,
		(tmp & UDCCR_REM) ? " rem" : "",
		(tmp & UDCCR_RSTIR) ? " rstir" : "",
		(tmp & UDCCR_SRM) ? " srm" : "",
		(tmp & UDCCR_SUSIR) ? " susir" : "",
		(tmp & UDCCR_RESIR) ? " resir" : "",
		(tmp & UDCCR_RSM) ? " rsm" : "",
		(tmp & UDCCR_UDA) ? " uda" : "",
		(tmp & UDCCR_UDE) ? " ude" : "");

	tmp = UDCCS0;
1053
	seq_printf(m,
L
Linus Torvalds 已提交
1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
		"udccs0 %02X =%s%s%s%s%s%s%s%s\n", tmp,
		(tmp & UDCCS0_SA) ? " sa" : "",
		(tmp & UDCCS0_RNE) ? " rne" : "",
		(tmp & UDCCS0_FST) ? " fst" : "",
		(tmp & UDCCS0_SST) ? " sst" : "",
		(tmp & UDCCS0_DRWF) ? " dwrf" : "",
		(tmp & UDCCS0_FTF) ? " ftf" : "",
		(tmp & UDCCS0_IPR) ? " ipr" : "",
		(tmp & UDCCS0_OPR) ? " opr" : "");

	if (dev->has_cfr) {
		tmp = UDCCFR;
1066
		seq_printf(m,
L
Linus Torvalds 已提交
1067 1068 1069 1070 1071
			"udccfr %02X =%s%s\n", tmp,
			(tmp & UDCCFR_AREN) ? " aren" : "",
			(tmp & UDCCFR_ACM) ? " acm" : "");
	}

1072
	if (dev->gadget.speed != USB_SPEED_FULL || !dev->driver)
L
Linus Torvalds 已提交
1073 1074
		goto done;

1075
	seq_printf(m, "ep0 IN %lu/%lu, OUT %lu/%lu\nirqs %lu\n\n",
L
Linus Torvalds 已提交
1076 1077 1078 1079 1080 1081
		dev->stats.write.bytes, dev->stats.write.ops,
		dev->stats.read.bytes, dev->stats.read.ops,
		dev->stats.irqs);

	/* dump endpoint queues */
	for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
1082 1083
		struct pxa25x_ep	*ep = &dev->ep [i];
		struct pxa25x_request	*req;
L
Linus Torvalds 已提交
1084 1085

		if (i != 0) {
1086
			const struct usb_endpoint_descriptor	*desc;
L
Linus Torvalds 已提交
1087

1088
			desc = ep->ep.desc;
1089
			if (!desc)
L
Linus Torvalds 已提交
1090 1091
				continue;
			tmp = *dev->ep [i].reg_udccs;
1092
			seq_printf(m,
1093
				"%s max %d %s udccs %02x irqs %lu\n",
1094
				ep->ep.name, usb_endpoint_maxp(desc),
1095
				"pio", tmp, ep->pio_irqs);
L
Linus Torvalds 已提交
1096 1097 1098
			/* TODO translate all five groups of udccs bits! */

		} else /* ep0 should only have one transfer queued */
1099
			seq_printf(m, "ep0 max 16 pio irqs %lu\n",
L
Linus Torvalds 已提交
1100 1101 1102
				ep->pio_irqs);

		if (list_empty(&ep->queue)) {
1103
			seq_printf(m, "\t(nothing queued)\n");
L
Linus Torvalds 已提交
1104 1105 1106
			continue;
		}
		list_for_each_entry(req, &ep->queue, queue) {
1107
			seq_printf(m,
L
Linus Torvalds 已提交
1108 1109 1110 1111 1112 1113 1114 1115
					"\treq %p len %d/%d buf %p\n",
					&req->req, req->req.actual,
					req->req.length, req->req.buf);
		}
	}

done:
	local_irq_restore(flags);
1116
	return 0;
L
Linus Torvalds 已提交
1117 1118
}

1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142
static int
udc_debugfs_open(struct inode *inode, struct file *file)
{
	return single_open(file, udc_seq_show, inode->i_private);
}

static const struct file_operations debug_fops = {
	.open		= udc_debugfs_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= single_release,
	.owner		= THIS_MODULE,
};

#define create_debug_files(dev) \
	do { \
		dev->debugfs_udc = debugfs_create_file(dev->gadget.name, \
			S_IRUGO, NULL, dev, &debug_fops); \
	} while (0)
#define remove_debug_files(dev) \
	do { \
		if (dev->debugfs_udc) \
			debugfs_remove(dev->debugfs_udc); \
	} while (0)
L
Linus Torvalds 已提交
1143 1144 1145

#else	/* !CONFIG_USB_GADGET_DEBUG_FILES */

1146 1147
#define create_debug_files(dev) do {} while (0)
#define remove_debug_files(dev) do {} while (0)
L
Linus Torvalds 已提交
1148 1149 1150 1151 1152 1153

#endif	/* CONFIG_USB_GADGET_DEBUG_FILES */

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

/*
1154
 *	udc_disable - disable USB device controller
L
Linus Torvalds 已提交
1155
 */
1156
static void udc_disable(struct pxa25x_udc *dev)
L
Linus Torvalds 已提交
1157 1158 1159 1160 1161 1162 1163
{
	/* block all irqs */
	udc_set_mask_UDCCR(UDCCR_SRM|UDCCR_REM);
	UICR0 = UICR1 = 0xff;
	UFNRH = UFNRH_SIM;

	/* if hardware supports it, disconnect from usb */
D
David Brownell 已提交
1164
	pullup_off();
L
Linus Torvalds 已提交
1165 1166 1167 1168 1169 1170 1171 1172 1173

	udc_clear_mask_UDCCR(UDCCR_UDE);

	ep0_idle (dev);
	dev->gadget.speed = USB_SPEED_UNKNOWN;
}


/*
1174
 *	udc_reinit - initialize software state
L
Linus Torvalds 已提交
1175
 */
1176
static void udc_reinit(struct pxa25x_udc *dev)
L
Linus Torvalds 已提交
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
{
	u32	i;

	/* device/ep0 records init */
	INIT_LIST_HEAD (&dev->gadget.ep_list);
	INIT_LIST_HEAD (&dev->gadget.ep0->ep_list);
	dev->ep0state = EP0_IDLE;

	/* basic endpoint records init */
	for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
1187
		struct pxa25x_ep *ep = &dev->ep[i];
L
Linus Torvalds 已提交
1188 1189 1190 1191

		if (i != 0)
			list_add_tail (&ep->ep.ep_list, &dev->gadget.ep_list);

1192
		ep->ep.desc = NULL;
L
Linus Torvalds 已提交
1193 1194
		ep->stopped = 0;
		INIT_LIST_HEAD (&ep->queue);
1195
		ep->pio_irqs = 0;
1196
		usb_ep_set_maxpacket_limit(&ep->ep, ep->ep.maxpacket);
L
Linus Torvalds 已提交
1197 1198 1199 1200 1201 1202 1203 1204
	}

	/* the rest was statically initialized, and is read-only */
}

/* until it's enabled, this UDC should be completely invisible
 * to any USB host.
 */
1205
static void udc_enable (struct pxa25x_udc *dev)
L
Linus Torvalds 已提交
1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
{
	udc_clear_mask_UDCCR(UDCCR_UDE);

	/* try to clear these bits before we enable the udc */
	udc_ack_int_UDCCR(UDCCR_SUSIR|/*UDCCR_RSTIR|*/UDCCR_RESIR);

	ep0_idle(dev);
	dev->gadget.speed = USB_SPEED_UNKNOWN;
	dev->stats.irqs = 0;

	/*
	 * sequence taken from chapter 12.5.10, PXA250 AppProcDevManual:
	 * - enable UDC
	 * - if RESET is already in progress, ack interrupt
	 * - unmask reset interrupt
	 */
	udc_set_mask_UDCCR(UDCCR_UDE);
	if (!(UDCCR & UDCCR_UDA))
		udc_ack_int_UDCCR(UDCCR_RSTIR);

	if (dev->has_cfr /* UDC_RES2 is defined */) {
		/* pxa255 (a0+) can avoid a set_config race that could
		 * prevent gadget drivers from configuring correctly
		 */
		UDCCFR = UDCCFR_ACM | UDCCFR_MB1;
	} else {
		/* "USB test mode" for pxa250 errata 40-42 (stepping a0, a1)
		 * which could result in missing packets and interrupts.
		 * supposedly one bit per endpoint, controlling whether it
		 * double buffers or not; ACM/AREN bits fit into the holes.
		 * zero bits (like USIR0_IRx) disable double buffering.
		 */
		UDC_RES1 = 0x00;
		UDC_RES2 = 0x00;
	}

	/* enable suspend/resume and reset irqs */
	udc_clear_mask_UDCCR(UDCCR_SRM | UDCCR_REM);

	/* enable ep0 irqs */
	UICR0 &= ~UICR0_IM0;

	/* if hardware supports it, pullup D+ and wait for reset */
D
David Brownell 已提交
1249
	pullup_on();
L
Linus Torvalds 已提交
1250 1251 1252 1253 1254 1255 1256 1257 1258
}


/* 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.
 */
1259 1260
static int pxa25x_udc_start(struct usb_gadget *g,
		struct usb_gadget_driver *driver)
L
Linus Torvalds 已提交
1261
{
1262
	struct pxa25x_udc	*dev = to_pxa25x(g);
L
Linus Torvalds 已提交
1263 1264 1265 1266 1267 1268 1269 1270 1271
	int			retval;

	/* first hook up the driver ... */
	dev->driver = driver;
	dev->pullup = 1;

	/* ... then enable host detection and ep0; and we're ready
	 * for set_configuration as well as eventual disconnect.
	 */
1272
	/* connect to bus through transceiver */
1273
	if (!IS_ERR_OR_NULL(dev->transceiver)) {
1274 1275
		retval = otg_set_peripheral(dev->transceiver->otg,
						&dev->gadget);
1276
		if (retval)
1277 1278 1279
			goto bind_fail;
	}

1280
	pullup(dev);
L
Linus Torvalds 已提交
1281 1282
	dump_state(dev);
	return 0;
1283 1284
bind_fail:
	return retval;
L
Linus Torvalds 已提交
1285 1286 1287
}

static void
1288
stop_activity(struct pxa25x_udc *dev, struct usb_gadget_driver *driver)
L
Linus Torvalds 已提交
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298
{
	int i;

	/* don't disconnect drivers more than once */
	if (dev->gadget.speed == USB_SPEED_UNKNOWN)
		driver = NULL;
	dev->gadget.speed = USB_SPEED_UNKNOWN;

	/* prevent new request submissions, kill any outstanding requests  */
	for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
1299
		struct pxa25x_ep *ep = &dev->ep[i];
L
Linus Torvalds 已提交
1300 1301 1302 1303 1304 1305

		ep->stopped = 1;
		nuke(ep, -ESHUTDOWN);
	}
	del_timer_sync(&dev->timer);

1306 1307 1308 1309
	/* report disconnect; the driver is already quiesced */
	if (driver)
		driver->disconnect(&dev->gadget);

L
Linus Torvalds 已提交
1310 1311 1312 1313
	/* re-init driver-visible data structures */
	udc_reinit(dev);
}

1314 1315
static int pxa25x_udc_stop(struct usb_gadget*g,
		struct usb_gadget_driver *driver)
L
Linus Torvalds 已提交
1316
{
1317
	struct pxa25x_udc	*dev = to_pxa25x(g);
L
Linus Torvalds 已提交
1318 1319

	local_irq_disable();
1320 1321
	dev->pullup = 0;
	pullup(dev);
L
Linus Torvalds 已提交
1322 1323 1324
	stop_activity(dev, driver);
	local_irq_enable();

1325
	if (!IS_ERR_OR_NULL(dev->transceiver))
1326
		(void) otg_set_peripheral(dev->transceiver->otg, NULL);
1327

L
Linus Torvalds 已提交
1328 1329 1330
	dev->driver = NULL;

	dump_state(dev);
1331

L
Linus Torvalds 已提交
1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343
	return 0;
}

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

#ifdef CONFIG_ARCH_LUBBOCK

/* Lubbock has separate connect and disconnect irqs.  More typical designs
 * use one GPIO as the VBUS IRQ, and another to control the D+ pullup.
 */

static irqreturn_t
1344
lubbock_vbus_irq(int irq, void *_dev)
L
Linus Torvalds 已提交
1345
{
1346
	struct pxa25x_udc	*dev = _dev;
L
Linus Torvalds 已提交
1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364
	int			vbus;

	dev->stats.irqs++;
	switch (irq) {
	case LUBBOCK_USB_IRQ:
		vbus = 1;
		disable_irq(LUBBOCK_USB_IRQ);
		enable_irq(LUBBOCK_USB_DISC_IRQ);
		break;
	case LUBBOCK_USB_DISC_IRQ:
		vbus = 0;
		disable_irq(LUBBOCK_USB_DISC_IRQ);
		enable_irq(LUBBOCK_USB_IRQ);
		break;
	default:
		return IRQ_NONE;
	}

1365
	pxa25x_udc_vbus_session(&dev->gadget, vbus);
L
Linus Torvalds 已提交
1366 1367 1368 1369 1370 1371 1372 1373
	return IRQ_HANDLED;
}

#endif


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

1374
static inline void clear_ep_state (struct pxa25x_udc *dev)
L
Linus Torvalds 已提交
1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386
{
	unsigned i;

	/* hardware SET_{CONFIGURATION,INTERFACE} automagic resets endpoint
	 * fifos, and pending transactions mustn't be continued in any case.
	 */
	for (i = 1; i < PXA_UDC_NUM_ENDPOINTS; i++)
		nuke(&dev->ep[i], -ECONNABORTED);
}

static void udc_watchdog(unsigned long _dev)
{
1387
	struct pxa25x_udc	*dev = (void *)_dev;
L
Linus Torvalds 已提交
1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399

	local_irq_disable();
	if (dev->ep0state == EP0_STALL
			&& (UDCCS0 & UDCCS0_FST) == 0
			&& (UDCCS0 & UDCCS0_SST) == 0) {
		UDCCS0 = UDCCS0_FST|UDCCS0_FTF;
		DBG(DBG_VERBOSE, "ep0 re-stall\n");
		start_watchdog(dev);
	}
	local_irq_enable();
}

1400
static void handle_ep0 (struct pxa25x_udc *dev)
L
Linus Torvalds 已提交
1401 1402
{
	u32			udccs0 = UDCCS0;
1403 1404
	struct pxa25x_ep	*ep = &dev->ep [0];
	struct pxa25x_request	*req;
L
Linus Torvalds 已提交
1405 1406 1407 1408 1409 1410 1411 1412 1413
	union {
		struct usb_ctrlrequest	r;
		u8			raw [8];
		u32			word [2];
	} u;

	if (list_empty(&ep->queue))
		req = NULL;
	else
1414
		req = list_entry(ep->queue.next, struct pxa25x_request, queue);
L
Linus Torvalds 已提交
1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518

	/* clear stall status */
	if (udccs0 & UDCCS0_SST) {
		nuke(ep, -EPIPE);
		UDCCS0 = UDCCS0_SST;
		del_timer(&dev->timer);
		ep0_idle(dev);
	}

	/* previous request unfinished?  non-error iff back-to-back ... */
	if ((udccs0 & UDCCS0_SA) != 0 && dev->ep0state != EP0_IDLE) {
		nuke(ep, 0);
		del_timer(&dev->timer);
		ep0_idle(dev);
	}

	switch (dev->ep0state) {
	case EP0_IDLE:
		/* late-breaking status? */
		udccs0 = UDCCS0;

		/* start control request? */
		if (likely((udccs0 & (UDCCS0_OPR|UDCCS0_SA|UDCCS0_RNE))
				== (UDCCS0_OPR|UDCCS0_SA|UDCCS0_RNE))) {
			int i;

			nuke (ep, -EPROTO);

			/* read SETUP packet */
			for (i = 0; i < 8; i++) {
				if (unlikely(!(UDCCS0 & UDCCS0_RNE))) {
bad_setup:
					DMSG("SETUP %d!\n", i);
					goto stall;
				}
				u.raw [i] = (u8) UDDR0;
			}
			if (unlikely((UDCCS0 & UDCCS0_RNE) != 0))
				goto bad_setup;

got_setup:
			DBG(DBG_VERBOSE, "SETUP %02x.%02x v%04x i%04x l%04x\n",
				u.r.bRequestType, u.r.bRequest,
				le16_to_cpu(u.r.wValue),
				le16_to_cpu(u.r.wIndex),
				le16_to_cpu(u.r.wLength));

			/* cope with automagic for some standard requests. */
			dev->req_std = (u.r.bRequestType & USB_TYPE_MASK)
						== USB_TYPE_STANDARD;
			dev->req_config = 0;
			dev->req_pending = 1;
			switch (u.r.bRequest) {
			/* hardware restricts gadget drivers here! */
			case USB_REQ_SET_CONFIGURATION:
				if (u.r.bRequestType == USB_RECIP_DEVICE) {
					/* reflect hardware's automagic
					 * up to the gadget driver.
					 */
config_change:
					dev->req_config = 1;
					clear_ep_state(dev);
					/* if !has_cfr, there's no synch
					 * else use AREN (later) not SA|OPR
					 * USIR0_IR0 acts edge sensitive
					 */
				}
				break;
			/* ... and here, even more ... */
			case USB_REQ_SET_INTERFACE:
				if (u.r.bRequestType == USB_RECIP_INTERFACE) {
					/* udc hardware is broken by design:
					 *  - altsetting may only be zero;
					 *  - hw resets all interfaces' eps;
					 *  - ep reset doesn't include halt(?).
					 */
					DMSG("broken set_interface (%d/%d)\n",
						le16_to_cpu(u.r.wIndex),
						le16_to_cpu(u.r.wValue));
					goto config_change;
				}
				break;
			/* hardware was supposed to hide this */
			case USB_REQ_SET_ADDRESS:
				if (u.r.bRequestType == USB_RECIP_DEVICE) {
					ep0start(dev, 0, "address");
					return;
				}
				break;
			}

			if (u.r.bRequestType & USB_DIR_IN)
				dev->ep0state = EP0_IN_DATA_PHASE;
			else
				dev->ep0state = EP0_OUT_DATA_PHASE;

			i = dev->driver->setup(&dev->gadget, &u.r);
			if (i < 0) {
				/* hardware automagic preventing STALL... */
				if (dev->req_config) {
					/* hardware sometimes neglects to tell
					 * tell us about config change events,
					 * so later ones may fail...
					 */
1519
					WARNING("config change %02x fail %d?\n",
L
Linus Torvalds 已提交
1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623
						u.r.bRequest, i);
					return;
					/* TODO experiment:  if has_cfr,
					 * hardware didn't ACK; maybe we
					 * could actually STALL!
					 */
				}
				DBG(DBG_VERBOSE, "protocol STALL, "
					"%02x err %d\n", UDCCS0, i);
stall:
				/* the watchdog timer helps deal with cases
				 * where udc seems to clear FST wrongly, and
				 * then NAKs instead of STALLing.
				 */
				ep0start(dev, UDCCS0_FST|UDCCS0_FTF, "stall");
				start_watchdog(dev);
				dev->ep0state = EP0_STALL;

			/* deferred i/o == no response yet */
			} else if (dev->req_pending) {
				if (likely(dev->ep0state == EP0_IN_DATA_PHASE
						|| dev->req_std || u.r.wLength))
					ep0start(dev, 0, "defer");
				else
					ep0start(dev, UDCCS0_IPR, "defer/IPR");
			}

			/* expect at least one data or status stage irq */
			return;

		} else if (likely((udccs0 & (UDCCS0_OPR|UDCCS0_SA))
				== (UDCCS0_OPR|UDCCS0_SA))) {
			unsigned i;

			/* pxa210/250 erratum 131 for B0/B1 says RNE lies.
			 * still observed on a pxa255 a0.
			 */
			DBG(DBG_VERBOSE, "e131\n");
			nuke(ep, -EPROTO);

			/* read SETUP data, but don't trust it too much */
			for (i = 0; i < 8; i++)
				u.raw [i] = (u8) UDDR0;
			if ((u.r.bRequestType & USB_RECIP_MASK)
					> USB_RECIP_OTHER)
				goto stall;
			if (u.word [0] == 0 && u.word [1] == 0)
				goto stall;
			goto got_setup;
		} else {
			/* some random early IRQ:
			 * - we acked FST
			 * - IPR cleared
			 * - OPR got set, without SA (likely status stage)
			 */
			UDCCS0 = udccs0 & (UDCCS0_SA|UDCCS0_OPR);
		}
		break;
	case EP0_IN_DATA_PHASE:			/* GET_DESCRIPTOR etc */
		if (udccs0 & UDCCS0_OPR) {
			UDCCS0 = UDCCS0_OPR|UDCCS0_FTF;
			DBG(DBG_VERBOSE, "ep0in premature status\n");
			if (req)
				done(ep, req, 0);
			ep0_idle(dev);
		} else /* irq was IPR clearing */ {
			if (req) {
				/* this IN packet might finish the request */
				(void) write_ep0_fifo(ep, req);
			} /* else IN token before response was written */
		}
		break;
	case EP0_OUT_DATA_PHASE:		/* SET_DESCRIPTOR etc */
		if (udccs0 & UDCCS0_OPR) {
			if (req) {
				/* this OUT packet might finish the request */
				if (read_ep0_fifo(ep, req))
					done(ep, req, 0);
				/* else more OUT packets expected */
			} /* else OUT token before read was issued */
		} else /* irq was IPR clearing */ {
			DBG(DBG_VERBOSE, "ep0out premature status\n");
			if (req)
				done(ep, req, 0);
			ep0_idle(dev);
		}
		break;
	case EP0_END_XFER:
		if (req)
			done(ep, req, 0);
		/* ack control-IN status (maybe in-zlp was skipped)
		 * also appears after some config change events.
		 */
		if (udccs0 & UDCCS0_OPR)
			UDCCS0 = UDCCS0_OPR;
		ep0_idle(dev);
		break;
	case EP0_STALL:
		UDCCS0 = UDCCS0_FST;
		break;
	}
	USIR0 = USIR0_IR0;
}

1624
static void handle_ep(struct pxa25x_ep *ep)
L
Linus Torvalds 已提交
1625
{
1626
	struct pxa25x_request	*req;
L
Linus Torvalds 已提交
1627 1628 1629 1630 1631 1632 1633 1634
	int			is_in = ep->bEndpointAddress & USB_DIR_IN;
	int			completed;
	u32			udccs, tmp;

	do {
		completed = 0;
		if (likely (!list_empty(&ep->queue)))
			req = list_entry(ep->queue.next,
1635
					struct pxa25x_request, queue);
L
Linus Torvalds 已提交
1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671
		else
			req = NULL;

		// TODO check FST handling

		udccs = *ep->reg_udccs;
		if (unlikely(is_in)) {	/* irq from TPC, SST, or (ISO) TUR */
			tmp = UDCCS_BI_TUR;
			if (likely(ep->bmAttributes == USB_ENDPOINT_XFER_BULK))
				tmp |= UDCCS_BI_SST;
			tmp &= udccs;
			if (likely (tmp))
				*ep->reg_udccs = tmp;
			if (req && likely ((udccs & UDCCS_BI_TFS) != 0))
				completed = write_fifo(ep, req);

		} else {	/* irq from RPC (or for ISO, ROF) */
			if (likely(ep->bmAttributes == USB_ENDPOINT_XFER_BULK))
				tmp = UDCCS_BO_SST | UDCCS_BO_DME;
			else
				tmp = UDCCS_IO_ROF | UDCCS_IO_DME;
			tmp &= udccs;
			if (likely(tmp))
				*ep->reg_udccs = tmp;

			/* fifos can hold packets, ready for reading... */
			if (likely(req)) {
				completed = read_fifo(ep, req);
			} else
				pio_irq_disable (ep->bEndpointAddress);
		}
		ep->pio_irqs++;
	} while (completed);
}

/*
1672
 *	pxa25x_udc_irq - interrupt handler
L
Linus Torvalds 已提交
1673 1674 1675 1676 1677 1678
 *
 * avoid delays in ep0 processing. the control handshaking isn't always
 * under software control (pxa250c0 and the pxa255 are better), and delays
 * could cause usb protocol errors.
 */
static irqreturn_t
1679
pxa25x_udc_irq(int irq, void *_dev)
L
Linus Torvalds 已提交
1680
{
1681
	struct pxa25x_udc	*dev = _dev;
L
Linus Torvalds 已提交
1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693
	int			handled;

	dev->stats.irqs++;
	do {
		u32		udccr = UDCCR;

		handled = 0;

		/* SUSpend Interrupt Request */
		if (unlikely(udccr & UDCCR_SUSIR)) {
			udc_ack_int_UDCCR(UDCCR_SUSIR);
			handled = 1;
1694
			DBG(DBG_VERBOSE, "USB suspend\n");
L
Linus Torvalds 已提交
1695

1696
			if (dev->gadget.speed != USB_SPEED_UNKNOWN
L
Linus Torvalds 已提交
1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710
					&& dev->driver
					&& dev->driver->suspend)
				dev->driver->suspend(&dev->gadget);
			ep0_idle (dev);
		}

		/* RESume Interrupt Request */
		if (unlikely(udccr & UDCCR_RESIR)) {
			udc_ack_int_UDCCR(UDCCR_RESIR);
			handled = 1;
			DBG(DBG_VERBOSE, "USB resume\n");

			if (dev->gadget.speed != USB_SPEED_UNKNOWN
					&& dev->driver
1711
					&& dev->driver->resume)
L
Linus Torvalds 已提交
1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760
				dev->driver->resume(&dev->gadget);
		}

		/* ReSeT Interrupt Request - USB reset */
		if (unlikely(udccr & UDCCR_RSTIR)) {
			udc_ack_int_UDCCR(UDCCR_RSTIR);
			handled = 1;

			if ((UDCCR & UDCCR_UDA) == 0) {
				DBG(DBG_VERBOSE, "USB reset start\n");

				/* reset driver and endpoints,
				 * in case that's not yet done
				 */
				stop_activity (dev, dev->driver);

			} else {
				DBG(DBG_VERBOSE, "USB reset end\n");
				dev->gadget.speed = USB_SPEED_FULL;
				memset(&dev->stats, 0, sizeof dev->stats);
				/* driver and endpoints are still reset */
			}

		} else {
			u32	usir0 = USIR0 & ~UICR0;
			u32	usir1 = USIR1 & ~UICR1;
			int	i;

			if (unlikely (!usir0 && !usir1))
				continue;

			DBG(DBG_VERY_NOISY, "irq %02x.%02x\n", usir1, usir0);

			/* control traffic */
			if (usir0 & USIR0_IR0) {
				dev->ep[0].pio_irqs++;
				handle_ep0(dev);
				handled = 1;
			}

			/* endpoint data transfers */
			for (i = 0; i < 8; i++) {
				u32	tmp = 1 << i;

				if (i && (usir0 & tmp)) {
					handle_ep(&dev->ep[i]);
					USIR0 |= tmp;
					handled = 1;
				}
1761
#ifndef	CONFIG_USB_PXA25X_SMALL
L
Linus Torvalds 已提交
1762 1763 1764 1765 1766
				if (usir1 & tmp) {
					handle_ep(&dev->ep[i+8]);
					USIR1 |= tmp;
					handled = 1;
				}
1767
#endif
L
Linus Torvalds 已提交
1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780
			}
		}

		/* we could also ask for 1 msec SOF (SIR) interrupts */

	} while (handled);
	return IRQ_HANDLED;
}

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

static void nop_release (struct device *dev)
{
1781
	DMSG("%s %s\n", __func__, dev_name(dev));
L
Linus Torvalds 已提交
1782 1783 1784 1785 1786 1787
}

/* this uses load-time allocation and initialization (instead of
 * doing it at run-time) to save code, eliminate fault paths, and
 * be more obviously correct.
 */
1788
static struct pxa25x_udc memory = {
L
Linus Torvalds 已提交
1789
	.gadget = {
1790
		.ops		= &pxa25x_udc_ops,
L
Linus Torvalds 已提交
1791 1792 1793
		.ep0		= &memory.ep[0].ep,
		.name		= driver_name,
		.dev = {
1794
			.init_name	= "gadget",
L
Linus Torvalds 已提交
1795 1796 1797 1798 1799 1800 1801 1802
			.release	= nop_release,
		},
	},

	/* control endpoint */
	.ep[0] = {
		.ep = {
			.name		= ep0name,
1803
			.ops		= &pxa25x_ep_ops,
L
Linus Torvalds 已提交
1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814
			.maxpacket	= EP0_FIFO_SIZE,
		},
		.dev		= &memory,
		.reg_udccs	= &UDCCS0,
		.reg_uddr	= &UDDR0,
	},

	/* first group of endpoints */
	.ep[1] = {
		.ep = {
			.name		= "ep1in-bulk",
1815
			.ops		= &pxa25x_ep_ops,
L
Linus Torvalds 已提交
1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827
			.maxpacket	= BULK_FIFO_SIZE,
		},
		.dev		= &memory,
		.fifo_size	= BULK_FIFO_SIZE,
		.bEndpointAddress = USB_DIR_IN | 1,
		.bmAttributes	= USB_ENDPOINT_XFER_BULK,
		.reg_udccs	= &UDCCS1,
		.reg_uddr	= &UDDR1,
	},
	.ep[2] = {
		.ep = {
			.name		= "ep2out-bulk",
1828
			.ops		= &pxa25x_ep_ops,
L
Linus Torvalds 已提交
1829 1830 1831 1832 1833 1834 1835 1836 1837 1838
			.maxpacket	= BULK_FIFO_SIZE,
		},
		.dev		= &memory,
		.fifo_size	= BULK_FIFO_SIZE,
		.bEndpointAddress = 2,
		.bmAttributes	= USB_ENDPOINT_XFER_BULK,
		.reg_udccs	= &UDCCS2,
		.reg_ubcr	= &UBCR2,
		.reg_uddr	= &UDDR2,
	},
1839
#ifndef CONFIG_USB_PXA25X_SMALL
L
Linus Torvalds 已提交
1840 1841 1842
	.ep[3] = {
		.ep = {
			.name		= "ep3in-iso",
1843
			.ops		= &pxa25x_ep_ops,
L
Linus Torvalds 已提交
1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855
			.maxpacket	= ISO_FIFO_SIZE,
		},
		.dev		= &memory,
		.fifo_size	= ISO_FIFO_SIZE,
		.bEndpointAddress = USB_DIR_IN | 3,
		.bmAttributes	= USB_ENDPOINT_XFER_ISOC,
		.reg_udccs	= &UDCCS3,
		.reg_uddr	= &UDDR3,
	},
	.ep[4] = {
		.ep = {
			.name		= "ep4out-iso",
1856
			.ops		= &pxa25x_ep_ops,
L
Linus Torvalds 已提交
1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869
			.maxpacket	= ISO_FIFO_SIZE,
		},
		.dev		= &memory,
		.fifo_size	= ISO_FIFO_SIZE,
		.bEndpointAddress = 4,
		.bmAttributes	= USB_ENDPOINT_XFER_ISOC,
		.reg_udccs	= &UDCCS4,
		.reg_ubcr	= &UBCR4,
		.reg_uddr	= &UDDR4,
	},
	.ep[5] = {
		.ep = {
			.name		= "ep5in-int",
1870
			.ops		= &pxa25x_ep_ops,
L
Linus Torvalds 已提交
1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884
			.maxpacket	= INT_FIFO_SIZE,
		},
		.dev		= &memory,
		.fifo_size	= INT_FIFO_SIZE,
		.bEndpointAddress = USB_DIR_IN | 5,
		.bmAttributes	= USB_ENDPOINT_XFER_INT,
		.reg_udccs	= &UDCCS5,
		.reg_uddr	= &UDDR5,
	},

	/* second group of endpoints */
	.ep[6] = {
		.ep = {
			.name		= "ep6in-bulk",
1885
			.ops		= &pxa25x_ep_ops,
L
Linus Torvalds 已提交
1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897
			.maxpacket	= BULK_FIFO_SIZE,
		},
		.dev		= &memory,
		.fifo_size	= BULK_FIFO_SIZE,
		.bEndpointAddress = USB_DIR_IN | 6,
		.bmAttributes	= USB_ENDPOINT_XFER_BULK,
		.reg_udccs	= &UDCCS6,
		.reg_uddr	= &UDDR6,
	},
	.ep[7] = {
		.ep = {
			.name		= "ep7out-bulk",
1898
			.ops		= &pxa25x_ep_ops,
L
Linus Torvalds 已提交
1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911
			.maxpacket	= BULK_FIFO_SIZE,
		},
		.dev		= &memory,
		.fifo_size	= BULK_FIFO_SIZE,
		.bEndpointAddress = 7,
		.bmAttributes	= USB_ENDPOINT_XFER_BULK,
		.reg_udccs	= &UDCCS7,
		.reg_ubcr	= &UBCR7,
		.reg_uddr	= &UDDR7,
	},
	.ep[8] = {
		.ep = {
			.name		= "ep8in-iso",
1912
			.ops		= &pxa25x_ep_ops,
L
Linus Torvalds 已提交
1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924
			.maxpacket	= ISO_FIFO_SIZE,
		},
		.dev		= &memory,
		.fifo_size	= ISO_FIFO_SIZE,
		.bEndpointAddress = USB_DIR_IN | 8,
		.bmAttributes	= USB_ENDPOINT_XFER_ISOC,
		.reg_udccs	= &UDCCS8,
		.reg_uddr	= &UDDR8,
	},
	.ep[9] = {
		.ep = {
			.name		= "ep9out-iso",
1925
			.ops		= &pxa25x_ep_ops,
L
Linus Torvalds 已提交
1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938
			.maxpacket	= ISO_FIFO_SIZE,
		},
		.dev		= &memory,
		.fifo_size	= ISO_FIFO_SIZE,
		.bEndpointAddress = 9,
		.bmAttributes	= USB_ENDPOINT_XFER_ISOC,
		.reg_udccs	= &UDCCS9,
		.reg_ubcr	= &UBCR9,
		.reg_uddr	= &UDDR9,
	},
	.ep[10] = {
		.ep = {
			.name		= "ep10in-int",
1939
			.ops		= &pxa25x_ep_ops,
L
Linus Torvalds 已提交
1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953
			.maxpacket	= INT_FIFO_SIZE,
		},
		.dev		= &memory,
		.fifo_size	= INT_FIFO_SIZE,
		.bEndpointAddress = USB_DIR_IN | 10,
		.bmAttributes	= USB_ENDPOINT_XFER_INT,
		.reg_udccs	= &UDCCS10,
		.reg_uddr	= &UDDR10,
	},

	/* third group of endpoints */
	.ep[11] = {
		.ep = {
			.name		= "ep11in-bulk",
1954
			.ops		= &pxa25x_ep_ops,
L
Linus Torvalds 已提交
1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966
			.maxpacket	= BULK_FIFO_SIZE,
		},
		.dev		= &memory,
		.fifo_size	= BULK_FIFO_SIZE,
		.bEndpointAddress = USB_DIR_IN | 11,
		.bmAttributes	= USB_ENDPOINT_XFER_BULK,
		.reg_udccs	= &UDCCS11,
		.reg_uddr	= &UDDR11,
	},
	.ep[12] = {
		.ep = {
			.name		= "ep12out-bulk",
1967
			.ops		= &pxa25x_ep_ops,
L
Linus Torvalds 已提交
1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980
			.maxpacket	= BULK_FIFO_SIZE,
		},
		.dev		= &memory,
		.fifo_size	= BULK_FIFO_SIZE,
		.bEndpointAddress = 12,
		.bmAttributes	= USB_ENDPOINT_XFER_BULK,
		.reg_udccs	= &UDCCS12,
		.reg_ubcr	= &UBCR12,
		.reg_uddr	= &UDDR12,
	},
	.ep[13] = {
		.ep = {
			.name		= "ep13in-iso",
1981
			.ops		= &pxa25x_ep_ops,
L
Linus Torvalds 已提交
1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993
			.maxpacket	= ISO_FIFO_SIZE,
		},
		.dev		= &memory,
		.fifo_size	= ISO_FIFO_SIZE,
		.bEndpointAddress = USB_DIR_IN | 13,
		.bmAttributes	= USB_ENDPOINT_XFER_ISOC,
		.reg_udccs	= &UDCCS13,
		.reg_uddr	= &UDDR13,
	},
	.ep[14] = {
		.ep = {
			.name		= "ep14out-iso",
1994
			.ops		= &pxa25x_ep_ops,
L
Linus Torvalds 已提交
1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007
			.maxpacket	= ISO_FIFO_SIZE,
		},
		.dev		= &memory,
		.fifo_size	= ISO_FIFO_SIZE,
		.bEndpointAddress = 14,
		.bmAttributes	= USB_ENDPOINT_XFER_ISOC,
		.reg_udccs	= &UDCCS14,
		.reg_ubcr	= &UBCR14,
		.reg_uddr	= &UDDR14,
	},
	.ep[15] = {
		.ep = {
			.name		= "ep15in-int",
2008
			.ops		= &pxa25x_ep_ops,
L
Linus Torvalds 已提交
2009 2010 2011 2012 2013 2014 2015 2016 2017
			.maxpacket	= INT_FIFO_SIZE,
		},
		.dev		= &memory,
		.fifo_size	= INT_FIFO_SIZE,
		.bEndpointAddress = USB_DIR_IN | 15,
		.bmAttributes	= USB_ENDPOINT_XFER_INT,
		.reg_udccs	= &UDCCS15,
		.reg_uddr	= &UDDR15,
	},
2018
#endif /* !CONFIG_USB_PXA25X_SMALL */
L
Linus Torvalds 已提交
2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051
};

#define CP15R0_VENDOR_MASK	0xffffe000

#if	defined(CONFIG_ARCH_PXA)
#define CP15R0_XSCALE_VALUE	0x69052000	/* intel/arm/xscale */

#elif	defined(CONFIG_ARCH_IXP4XX)
#define CP15R0_XSCALE_VALUE	0x69054000	/* intel/arm/ixp4xx */

#endif

#define CP15R0_PROD_MASK	0x000003f0
#define PXA25x			0x00000100	/* and PXA26x */
#define PXA210			0x00000120

#define CP15R0_REV_MASK		0x0000000f

#define CP15R0_PRODREV_MASK	(CP15R0_PROD_MASK | CP15R0_REV_MASK)

#define PXA255_A0		0x00000106	/* or PXA260_B1 */
#define PXA250_C0		0x00000105	/* or PXA26x_B0 */
#define PXA250_B2		0x00000104
#define PXA250_B1		0x00000103	/* or PXA260_A0 */
#define PXA250_B0		0x00000102
#define PXA250_A1		0x00000101
#define PXA250_A0		0x00000100

#define PXA210_C0		0x00000125
#define PXA210_B2		0x00000124
#define PXA210_B1		0x00000123
#define PXA210_B0		0x00000122
#define IXP425_A0		0x000001c1
2052
#define IXP425_B0		0x000001f1
2053
#define IXP465_AD		0x00000200
L
Linus Torvalds 已提交
2054 2055

/*
2056
 *	probe - binds to the platform device
L
Linus Torvalds 已提交
2057
 */
2058
static int pxa25x_udc_probe(struct platform_device *pdev)
L
Linus Torvalds 已提交
2059
{
2060
	struct pxa25x_udc *dev = &memory;
2061
	int retval, irq;
L
Linus Torvalds 已提交
2062 2063
	u32 chiprev;

2064 2065
	pr_info("%s: version %s\n", driver_name, DRIVER_VERSION);

L
Linus Torvalds 已提交
2066 2067 2068
	/* insist on Intel/ARM/XScale */
	asm("mrc%? p15, 0, %0, c0, c0" : "=r" (chiprev));
	if ((chiprev & CP15R0_VENDOR_MASK) != CP15R0_XSCALE_VALUE) {
2069
		pr_err("%s: not XScale!\n", driver_name);
L
Linus Torvalds 已提交
2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085
		return -ENODEV;
	}

	/* trigger chiprev-specific logic */
	switch (chiprev & CP15R0_PRODREV_MASK) {
#if	defined(CONFIG_ARCH_PXA)
	case PXA255_A0:
		dev->has_cfr = 1;
		break;
	case PXA250_A0:
	case PXA250_A1:
		/* A0/A1 "not released"; ep 13, 15 unusable */
		/* fall through */
	case PXA250_B2: case PXA210_B2:
	case PXA250_B1: case PXA210_B1:
	case PXA250_B0: case PXA210_B0:
2086
		/* OUT-DMA is broken ... */
L
Linus Torvalds 已提交
2087 2088 2089 2090 2091
		/* fall through */
	case PXA250_C0: case PXA210_C0:
		break;
#elif	defined(CONFIG_ARCH_IXP4XX)
	case IXP425_A0:
2092
	case IXP425_B0:
2093 2094
	case IXP465_AD:
		dev->has_cfr = 1;
L
Linus Torvalds 已提交
2095 2096 2097
		break;
#endif
	default:
2098
		pr_err("%s: unrecognized processor: %08x\n",
L
Linus Torvalds 已提交
2099 2100 2101 2102 2103
			driver_name, chiprev);
		/* iop3xx, ixp4xx, ... */
		return -ENODEV;
	}

2104 2105 2106 2107
	irq = platform_get_irq(pdev, 0);
	if (irq < 0)
		return -ENODEV;

2108 2109 2110
	dev->clk = devm_clk_get(&pdev->dev, NULL);
	if (IS_ERR(dev->clk))
		return PTR_ERR(dev->clk);
2111

2112
	pr_debug("%s: IRQ %d%s%s\n", driver_name, irq,
L
Linus Torvalds 已提交
2113
		dev->has_cfr ? "" : " (!cfr)",
2114
		SIZE_STR "(pio)"
L
Linus Torvalds 已提交
2115 2116 2117
		);

	/* other non-static parts of init */
2118
	dev->dev = &pdev->dev;
J
Jingoo Han 已提交
2119
	dev->mach = dev_get_platdata(&pdev->dev);
2120

2121
	dev->transceiver = devm_usb_get_phy(&pdev->dev, USB_PHY_TYPE_USB2);
2122

2123
	if (gpio_is_valid(dev->mach->gpio_pullup)) {
2124 2125 2126
		retval = devm_gpio_request(&pdev->dev, dev->mach->gpio_pullup,
					   "pca25x_udc GPIO PULLUP");
		if (retval) {
2127 2128 2129
			dev_dbg(&pdev->dev,
				"can't get pullup gpio %d, err: %d\n",
				dev->mach->gpio_pullup, retval);
2130
			goto err;
2131 2132 2133
		}
		gpio_direction_output(dev->mach->gpio_pullup, 0);
	}
L
Linus Torvalds 已提交
2134 2135 2136 2137 2138 2139

	init_timer(&dev->timer);
	dev->timer.function = udc_watchdog;
	dev->timer.data = (unsigned long) dev;

	the_controller = dev;
2140
	platform_set_drvdata(pdev, dev);
L
Linus Torvalds 已提交
2141 2142 2143 2144

	udc_disable(dev);
	udc_reinit(dev);

2145
	dev->vbus = 0;
L
Linus Torvalds 已提交
2146 2147

	/* irq setup after old hardware state is cleaned up */
2148 2149
	retval = devm_request_irq(&pdev->dev, irq, pxa25x_udc_irq, 0,
				  driver_name, dev);
L
Linus Torvalds 已提交
2150
	if (retval != 0) {
2151
		pr_err("%s: can't get irq %d, err %d\n",
2152
			driver_name, irq, retval);
2153
		goto err;
L
Linus Torvalds 已提交
2154 2155 2156 2157 2158
	}
	dev->got_irq = 1;

#ifdef CONFIG_ARCH_LUBBOCK
	if (machine_is_lubbock()) {
2159 2160 2161
		retval = devm_request_irq(&pdev->dev, LUBBOCK_USB_DISC_IRQ,
					  lubbock_vbus_irq, 0, driver_name,
					  dev);
L
Linus Torvalds 已提交
2162
		if (retval != 0) {
2163
			pr_err("%s: can't get irq %i, err %d\n",
L
Linus Torvalds 已提交
2164
				driver_name, LUBBOCK_USB_DISC_IRQ, retval);
2165
			goto err;
L
Linus Torvalds 已提交
2166
		}
2167 2168 2169
		retval = devm_request_irq(&pdev->dev, LUBBOCK_USB_IRQ,
					  lubbock_vbus_irq, 0, driver_name,
					  dev);
L
Linus Torvalds 已提交
2170
		if (retval != 0) {
2171
			pr_err("%s: can't get irq %i, err %d\n",
L
Linus Torvalds 已提交
2172
				driver_name, LUBBOCK_USB_IRQ, retval);
2173
			goto err;
L
Linus Torvalds 已提交
2174
		}
2175
	} else
L
Linus Torvalds 已提交
2176
#endif
2177
	create_debug_files(dev);
L
Linus Torvalds 已提交
2178

2179 2180 2181
	retval = usb_add_gadget_udc(&pdev->dev, &dev->gadget);
	if (!retval)
		return retval;
2182

2183
	remove_debug_files(dev);
2184 2185
 err:
	if (!IS_ERR_OR_NULL(dev->transceiver))
2186
		dev->transceiver = NULL;
2187
	return retval;
L
Linus Torvalds 已提交
2188
}
D
David Brownell 已提交
2189

2190
static void pxa25x_udc_shutdown(struct platform_device *_dev)
D
David Brownell 已提交
2191 2192 2193 2194
{
	pullup_off();
}

2195
static int pxa25x_udc_remove(struct platform_device *pdev)
L
Linus Torvalds 已提交
2196
{
2197
	struct pxa25x_udc *dev = platform_get_drvdata(pdev);
L
Linus Torvalds 已提交
2198

2199 2200 2201
	if (dev->driver)
		return -EBUSY;

2202
	usb_del_gadget_udc(&dev->gadget);
2203 2204 2205
	dev->pullup = 0;
	pullup(dev);

2206
	remove_debug_files(dev);
L
Linus Torvalds 已提交
2207

2208
	if (!IS_ERR_OR_NULL(dev->transceiver))
2209 2210
		dev->transceiver = NULL;

L
Linus Torvalds 已提交
2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225
	the_controller = NULL;
	return 0;
}

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

#ifdef	CONFIG_PM

/* USB suspend (controlled by the host) and system suspend (controlled
 * by the PXA) don't necessarily work well together.  If USB is active,
 * the 48 MHz clock is required; so the system can't enter 33 MHz idle
 * mode, or any deeper PM saving state.
 *
 * For now, we punt and forcibly disconnect from the USB host when PXA
 * enters any suspend state.  While we're disconnected, we always disable
2226
 * the 48MHz USB clock ... allowing PXA sleep and/or 33 MHz idle states.
L
Linus Torvalds 已提交
2227 2228 2229 2230
 * Boards without software pullup control shouldn't use those states.
 * VBUS IRQs should probably be ignored so that the PXA device just acts
 * "dead" to USB hosts until system resume.
 */
2231
static int pxa25x_udc_suspend(struct platform_device *dev, pm_message_t state)
L
Linus Torvalds 已提交
2232
{
2233
	struct pxa25x_udc	*udc = platform_get_drvdata(dev);
2234
	unsigned long flags;
L
Linus Torvalds 已提交
2235

2236
	if (!gpio_is_valid(udc->mach->gpio_pullup) && !udc->mach->udc_command)
2237
		WARNING("USB host won't detect disconnect!\n");
2238 2239 2240 2241 2242
	udc->suspended = 1;

	local_irq_save(flags);
	pullup(udc);
	local_irq_restore(flags);
2243

L
Linus Torvalds 已提交
2244 2245 2246
	return 0;
}

2247
static int pxa25x_udc_resume(struct platform_device *dev)
L
Linus Torvalds 已提交
2248
{
2249
	struct pxa25x_udc	*udc = platform_get_drvdata(dev);
2250
	unsigned long flags;
L
Linus Torvalds 已提交
2251

2252 2253 2254 2255
	udc->suspended = 0;
	local_irq_save(flags);
	pullup(udc);
	local_irq_restore(flags);
2256

L
Linus Torvalds 已提交
2257 2258 2259 2260
	return 0;
}

#else
2261 2262
#define	pxa25x_udc_suspend	NULL
#define	pxa25x_udc_resume	NULL
L
Linus Torvalds 已提交
2263 2264 2265 2266
#endif

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

2267
static struct platform_driver udc_driver = {
2268
	.shutdown	= pxa25x_udc_shutdown,
2269 2270
	.probe		= pxa25x_udc_probe,
	.remove		= pxa25x_udc_remove,
2271 2272
	.suspend	= pxa25x_udc_suspend,
	.resume		= pxa25x_udc_resume,
2273 2274
	.driver		= {
		.owner	= THIS_MODULE,
2275
		.name	= "pxa25x-udc",
2276
	},
L
Linus Torvalds 已提交
2277 2278
};

2279
module_platform_driver(udc_driver);
L
Linus Torvalds 已提交
2280 2281 2282 2283

MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_AUTHOR("Frank Becker, Robert Schwebel, David Brownell");
MODULE_LICENSE("GPL");
2284
MODULE_ALIAS("platform:pxa25x-udc");