at91_udc.c 51.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * at91_udc -- driver for at91-series USB peripheral controller
 *
 * Copyright (C) 2004 by Thomas Rathbone
 * Copyright (C) 2005 by HP Labs
 * Copyright (C) 2005 by David Brownell
 *
 * 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.
 */

14
#undef	VERBOSE_DEBUG
15 16 17 18 19 20 21 22 23 24 25 26 27
#undef	PACKET_TRACE

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/delay.h>
#include <linux/ioport.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/list.h>
#include <linux/interrupt.h>
#include <linux/proc_fs.h>
28
#include <linux/prefetch.h>
29
#include <linux/clk.h>
30
#include <linux/usb/ch9.h>
31
#include <linux/usb/gadget.h>
32 33
#include <linux/of.h>
#include <linux/of_gpio.h>
34 35

#include <asm/byteorder.h>
36
#include <mach/hardware.h>
37 38
#include <asm/io.h>
#include <asm/irq.h>
39
#include <asm/gpio.h>
40

41 42 43
#include <mach/board.h>
#include <mach/cpu.h>
#include <mach/at91sam9261_matrix.h>
44
#include <mach/at91_matrix.h>
45 46 47 48 49 50

#include "at91_udc.h"


/*
 * This controller is simple and PIO-only.  It's used in many AT91-series
51 52
 * full speed USB controllers, including the at91rm9200 (arm920T, with MMU),
 * at91sam926x (arm926ejs, with MMU), and several no-mmu versions.
53 54 55
 *
 * This driver expects the board has been wired with two GPIOs suppporting
 * a VBUS sensing IRQ, and a D+ pullup.  (They may be omitted, but the
56 57 58
 * testing hasn't covered such cases.)
 *
 * The pullup is most important (so it's integrated on sam926x parts).  It
59
 * provides software control over whether the host enumerates the device.
60
 *
61 62
 * The VBUS sensing helps during enumeration, and allows both USB clocks
 * (and the transceiver) to stay gated off until they're necessary, saving
63 64
 * power.  During USB suspend, the 48 MHz clock is gated off in hardware;
 * it may also be gated off by software during some Linux sleep states.
65 66
 */

67
#define	DRIVER_VERSION	"3 May 2006"
68 69 70 71

static const char driver_name [] = "at91_udc";
static const char ep0name[] = "ep0";

72
#define VBUS_POLL_TIMEOUT	msecs_to_jiffies(1000)
73

74 75 76 77
#define at91_udp_read(udc, reg) \
	__raw_readl((udc)->udp_baseaddr + (reg))
#define at91_udp_write(udc, reg, val) \
	__raw_writel((val), (udc)->udp_baseaddr + (reg))
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98

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

#ifdef CONFIG_USB_GADGET_DEBUG_FILES

#include <linux/seq_file.h>

static const char debug_filename[] = "driver/udc";

#define FOURBITS "%s%s%s%s"
#define EIGHTBITS FOURBITS FOURBITS

static void proc_ep_show(struct seq_file *s, struct at91_ep *ep)
{
	static char		*types[] = {
		"control", "out-iso", "out-bulk", "out-int",
		"BOGUS",   "in-iso",  "in-bulk",  "in-int"};

	u32			csr;
	struct at91_request	*req;
	unsigned long	flags;
99
	struct at91_udc	*udc = ep->udc;
100

101
	spin_lock_irqsave(&udc->lock, flags);
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144

	csr = __raw_readl(ep->creg);

	/* NOTE:  not collecting per-endpoint irq statistics... */

	seq_printf(s, "\n");
	seq_printf(s, "%s, maxpacket %d %s%s %s%s\n",
			ep->ep.name, ep->ep.maxpacket,
			ep->is_in ? "in" : "out",
			ep->is_iso ? " iso" : "",
			ep->is_pingpong
				? (ep->fifo_bank ? "pong" : "ping")
				: "",
			ep->stopped ? " stopped" : "");
	seq_printf(s, "csr %08x rxbytes=%d %s %s %s" EIGHTBITS "\n",
		csr,
		(csr & 0x07ff0000) >> 16,
		(csr & (1 << 15)) ? "enabled" : "disabled",
		(csr & (1 << 11)) ? "DATA1" : "DATA0",
		types[(csr & 0x700) >> 8],

		/* iff type is control then print current direction */
		(!(csr & 0x700))
			? ((csr & (1 << 7)) ? " IN" : " OUT")
			: "",
		(csr & (1 << 6)) ? " rxdatabk1" : "",
		(csr & (1 << 5)) ? " forcestall" : "",
		(csr & (1 << 4)) ? " txpktrdy" : "",

		(csr & (1 << 3)) ? " stallsent" : "",
		(csr & (1 << 2)) ? " rxsetup" : "",
		(csr & (1 << 1)) ? " rxdatabk0" : "",
		(csr & (1 << 0)) ? " txcomp" : "");
	if (list_empty (&ep->queue))
		seq_printf(s, "\t(queue empty)\n");

	else list_for_each_entry (req, &ep->queue, queue) {
		unsigned	length = req->req.actual;

		seq_printf(s, "\treq %p len %d/%d buf %p\n",
				&req->req, length,
				req->req.length, req->req.buf);
	}
145
	spin_unlock_irqrestore(&udc->lock, flags);
146 147 148 149 150 151 152 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
}

static void proc_irq_show(struct seq_file *s, const char *label, u32 mask)
{
	int i;

	seq_printf(s, "%s %04x:%s%s" FOURBITS, label, mask,
		(mask & (1 << 13)) ? " wakeup" : "",
		(mask & (1 << 12)) ? " endbusres" : "",

		(mask & (1 << 11)) ? " sofint" : "",
		(mask & (1 << 10)) ? " extrsm" : "",
		(mask & (1 << 9)) ? " rxrsm" : "",
		(mask & (1 << 8)) ? " rxsusp" : "");
	for (i = 0; i < 8; i++) {
		if (mask & (1 << i))
			seq_printf(s, " ep%d", i);
	}
	seq_printf(s, "\n");
}

static int proc_udc_show(struct seq_file *s, void *unused)
{
	struct at91_udc	*udc = s->private;
	struct at91_ep	*ep;
	u32		tmp;

	seq_printf(s, "%s: version %s\n", driver_name, DRIVER_VERSION);

	seq_printf(s, "vbus %s, pullup %s, %s powered%s, gadget %s\n\n",
		udc->vbus ? "present" : "off",
		udc->enabled
			? (udc->vbus ? "active" : "enabled")
			: "disabled",
		udc->selfpowered ? "self" : "VBUS",
		udc->suspended ? ", suspended" : "",
		udc->driver ? udc->driver->driver.name : "(none)");

	/* don't access registers when interface isn't clocked */
	if (!udc->clocked) {
		seq_printf(s, "(not clocked)\n");
		return 0;
	}

190
	tmp = at91_udp_read(udc, AT91_UDP_FRM_NUM);
191 192 193 194 195
	seq_printf(s, "frame %05x:%s%s frame=%d\n", tmp,
		(tmp & AT91_UDP_FRM_OK) ? " ok" : "",
		(tmp & AT91_UDP_FRM_ERR) ? " err" : "",
		(tmp & AT91_UDP_NUM));

196
	tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT);
197 198 199 200 201 202 203
	seq_printf(s, "glbstate %02x:%s" FOURBITS "\n", tmp,
		(tmp & AT91_UDP_RMWUPE) ? " rmwupe" : "",
		(tmp & AT91_UDP_RSMINPR) ? " rsminpr" : "",
		(tmp & AT91_UDP_ESR) ? " esr" : "",
		(tmp & AT91_UDP_CONFG) ? " confg" : "",
		(tmp & AT91_UDP_FADDEN) ? " fadden" : "");

204
	tmp = at91_udp_read(udc, AT91_UDP_FADDR);
205 206 207 208
	seq_printf(s, "faddr   %03x:%s fadd=%d\n", tmp,
		(tmp & AT91_UDP_FEN) ? " fen" : "",
		(tmp & AT91_UDP_FADD));

209 210
	proc_irq_show(s, "imr   ", at91_udp_read(udc, AT91_UDP_IMR));
	proc_irq_show(s, "isr   ", at91_udp_read(udc, AT91_UDP_ISR));
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226

	if (udc->enabled && udc->vbus) {
		proc_ep_show(s, &udc->ep[0]);
		list_for_each_entry (ep, &udc->gadget.ep_list, ep.ep_list) {
			if (ep->desc)
				proc_ep_show(s, ep);
		}
	}
	return 0;
}

static int proc_udc_open(struct inode *inode, struct file *file)
{
	return single_open(file, proc_udc_show, PDE(inode)->data);
}

227
static const struct file_operations proc_ops = {
228
	.owner		= THIS_MODULE,
229 230 231 232 233 234 235 236
	.open		= proc_udc_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= single_release,
};

static void create_debug_file(struct at91_udc *udc)
{
237
	udc->pde = proc_create_data(debug_filename, 0, NULL, &proc_ops, udc);
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
}

static void remove_debug_file(struct at91_udc *udc)
{
	if (udc->pde)
		remove_proc_entry(debug_filename, NULL);
}

#else

static inline void create_debug_file(struct at91_udc *udc) {}
static inline void remove_debug_file(struct at91_udc *udc) {}

#endif


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

static void done(struct at91_ep *ep, struct at91_request *req, int status)
{
	unsigned	stopped = ep->stopped;
259
	struct at91_udc	*udc = ep->udc;
260 261 262 263 264 265 266 267 268 269

	list_del_init(&req->queue);
	if (req->req.status == -EINPROGRESS)
		req->req.status = status;
	else
		status = req->req.status;
	if (status && status != -ESHUTDOWN)
		VDBG("%s done %p, status %d\n", ep->ep.name, req, status);

	ep->stopped = 1;
270
	spin_unlock(&udc->lock);
271
	req->req.complete(&ep->ep, &req->req);
272
	spin_lock(&udc->lock);
273 274 275 276
	ep->stopped = stopped;

	/* ep0 is always ready; other endpoints need a non-empty queue */
	if (list_empty(&ep->queue) && ep->int_mask != (1 << 0))
277
		at91_udp_write(udc, AT91_UDP_IDR, ep->int_mask);
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
}

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

/* bits indicating OUT fifo has data ready */
#define	RX_DATA_READY	(AT91_UDP_RX_DATA_BK0 | AT91_UDP_RX_DATA_BK1)

/*
 * Endpoint FIFO CSR bits have a mix of bits, making it unsafe to just write
 * back most of the value you just read (because of side effects, including
 * bits that may change after reading and before writing).
 *
 * Except when changing a specific bit, always write values which:
 *  - clear SET_FX bits (setting them could change something)
 *  - set CLR_FX bits (clearing them could change something)
 *
 * There are also state bits like FORCESTALL, EPEDS, DIR, and EPTYPE
 * that shouldn't normally be changed.
296 297 298 299 300
 *
 * NOTE at91sam9260 docs mention synch between UDPCK and MCK clock domains,
 * implying a need to wait for one write to complete (test relevant bits)
 * before starting the next write.  This shouldn't be an issue given how
 * infrequently we write, except maybe for write-then-read idioms.
301 302
 */
#define	SET_FX	(AT91_UDP_TXPKTRDY)
303 304
#define	CLR_FX	(RX_DATA_READY | AT91_UDP_RXSETUP \
		| AT91_UDP_STALLSENT | AT91_UDP_TXCOMP)
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365

/* pull OUT packet data from the endpoint's fifo */
static int read_fifo (struct at91_ep *ep, struct at91_request *req)
{
	u32 __iomem	*creg = ep->creg;
	u8 __iomem	*dreg = ep->creg + (AT91_UDP_FDR(0) - AT91_UDP_CSR(0));
	u32		csr;
	u8		*buf;
	unsigned int	count, bufferspace, is_done;

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

	/*
	 * there might be nothing to read if ep_queue() calls us,
	 * or if we already emptied both pingpong buffers
	 */
rescan:
	csr = __raw_readl(creg);
	if ((csr & RX_DATA_READY) == 0)
		return 0;

	count = (csr & AT91_UDP_RXBYTECNT) >> 16;
	if (count > ep->ep.maxpacket)
		count = ep->ep.maxpacket;
	if (count > bufferspace) {
		DBG("%s buffer overflow\n", ep->ep.name);
		req->req.status = -EOVERFLOW;
		count = bufferspace;
	}
	__raw_readsb(dreg, buf, count);

	/* release and swap pingpong mem bank */
	csr |= CLR_FX;
	if (ep->is_pingpong) {
		if (ep->fifo_bank == 0) {
			csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK0);
			ep->fifo_bank = 1;
		} else {
			csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK1);
			ep->fifo_bank = 0;
		}
	} else
		csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK0);
	__raw_writel(csr, creg);

	req->req.actual += count;
	is_done = (count < ep->ep.maxpacket);
	if (count == bufferspace)
		is_done = 1;

	PACKET("%s %p out/%d%s\n", ep->ep.name, &req->req, count,
			is_done ? " (done)" : "");

	/*
	 * avoid extra trips through IRQ logic for packets already in
	 * the fifo ... maybe preventing an extra (expensive) OUT-NAK
	 */
	if (is_done)
		done(ep, req, 0);
	else if (ep->is_pingpong) {
H
Harro Haan 已提交
366 367 368 369 370 371 372
		/*
		 * One dummy read to delay the code because of a HW glitch:
		 * CSR returns bad RXCOUNT when read too soon after updating
		 * RX_DATA_BK flags.
		 */
		csr = __raw_readl(creg);

373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
		bufferspace -= count;
		buf += count;
		goto rescan;
	}

	return is_done;
}

/* load fifo for an IN packet */
static int write_fifo(struct at91_ep *ep, struct at91_request *req)
{
	u32 __iomem	*creg = ep->creg;
	u32		csr = __raw_readl(creg);
	u8 __iomem	*dreg = ep->creg + (AT91_UDP_FDR(0) - AT91_UDP_CSR(0));
	unsigned	total, count, is_last;
D
David Brownell 已提交
388
	u8		*buf;
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412

	/*
	 * TODO: allow for writing two packets to the fifo ... that'll
	 * reduce the amount of IN-NAKing, but probably won't affect
	 * throughput much.  (Unlike preventing OUT-NAKing!)
	 */

	/*
	 * If ep_queue() calls us, the queue is empty and possibly in
	 * odd states like TXCOMP not yet cleared (we do it, saving at
	 * least one IRQ) or the fifo not yet being free.  Those aren't
	 * issues normally (IRQ handler fast path).
	 */
	if (unlikely(csr & (AT91_UDP_TXCOMP | AT91_UDP_TXPKTRDY))) {
		if (csr & AT91_UDP_TXCOMP) {
			csr |= CLR_FX;
			csr &= ~(SET_FX | AT91_UDP_TXCOMP);
			__raw_writel(csr, creg);
			csr = __raw_readl(creg);
		}
		if (csr & AT91_UDP_TXPKTRDY)
			return 0;
	}

D
David Brownell 已提交
413 414
	buf = req->req.buf + req->req.actual;
	prefetch(buf);
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
	total = req->req.length - req->req.actual;
	if (ep->ep.maxpacket < total) {
		count = ep->ep.maxpacket;
		is_last = 0;
	} else {
		count = total;
		is_last = (count < ep->ep.maxpacket) || !req->req.zero;
	}

	/*
	 * Write the packet, maybe it's a ZLP.
	 *
	 * NOTE:  incrementing req->actual before we receive the ACK means
	 * gadget driver IN bytecounts can be wrong in fault cases.  That's
	 * fixable with PIO drivers like this one (save "count" here, and
	 * do the increment later on TX irq), but not for most DMA hardware.
	 *
	 * So all gadget drivers must accept that potential error.  Some
	 * hardware supports precise fifo status reporting, letting them
	 * recover when the actual bytecount matters (e.g. for USB Test
	 * and Measurement Class devices).
	 */
D
David Brownell 已提交
437
	__raw_writesb(dreg, buf, count);
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
	csr &= ~SET_FX;
	csr |= CLR_FX | AT91_UDP_TXPKTRDY;
	__raw_writel(csr, creg);
	req->req.actual += count;

	PACKET("%s %p in/%d%s\n", ep->ep.name, &req->req, count,
			is_last ? " (done)" : "");
	if (is_last)
		done(ep, req, 0);
	return is_last;
}

static void nuke(struct at91_ep *ep, int status)
{
	struct at91_request *req;

454
	/* terminate any request in the queue */
455 456 457 458
	ep->stopped = 1;
	if (list_empty(&ep->queue))
		return;

459
	VDBG("%s %s\n", __func__, ep->ep.name);
460 461 462 463 464 465 466 467
	while (!list_empty(&ep->queue)) {
		req = list_entry(ep->queue.next, struct at91_request, queue);
		done(ep, req, status);
	}
}

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

468 469
static int at91_ep_enable(struct usb_ep *_ep,
				const struct usb_endpoint_descriptor *desc)
470 471
{
	struct at91_ep	*ep = container_of(_ep, struct at91_ep, ep);
472
	struct at91_udc	*udc = ep->udc;
473 474 475 476 477 478 479 480
	u16		maxpacket;
	u32		tmp;
	unsigned long	flags;

	if (!_ep || !ep
			|| !desc || ep->desc
			|| _ep->name == ep0name
			|| desc->bDescriptorType != USB_DT_ENDPOINT
481
			|| (maxpacket = usb_endpoint_maxp(desc)) == 0
482 483 484 485 486
			|| maxpacket > ep->maxpacket) {
		DBG("bad ep or descriptor\n");
		return -EINVAL;
	}

487
	if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN) {
488 489 490 491
		DBG("bogus device state\n");
		return -ESHUTDOWN;
	}

492
	tmp = usb_endpoint_type(desc);
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
	switch (tmp) {
	case USB_ENDPOINT_XFER_CONTROL:
		DBG("only one control endpoint\n");
		return -EINVAL;
	case USB_ENDPOINT_XFER_INT:
		if (maxpacket > 64)
			goto bogus_max;
		break;
	case USB_ENDPOINT_XFER_BULK:
		switch (maxpacket) {
		case 8:
		case 16:
		case 32:
		case 64:
			goto ok;
		}
bogus_max:
		DBG("bogus maxpacket %d\n", maxpacket);
		return -EINVAL;
	case USB_ENDPOINT_XFER_ISOC:
		if (!ep->is_pingpong) {
			DBG("iso requires double buffering\n");
			return -EINVAL;
		}
		break;
	}

ok:
521
	spin_lock_irqsave(&udc->lock, flags);
522 523

	/* initialize endpoint to match this descriptor */
524
	ep->is_in = usb_endpoint_dir_in(desc);
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
	ep->is_iso = (tmp == USB_ENDPOINT_XFER_ISOC);
	ep->stopped = 0;
	if (ep->is_in)
		tmp |= 0x04;
	tmp <<= 8;
	tmp |= AT91_UDP_EPEDS;
	__raw_writel(tmp, ep->creg);

	ep->desc = desc;
	ep->ep.maxpacket = maxpacket;

	/*
	 * reset/init endpoint fifo.  NOTE:  leaves fifo_bank alone,
	 * since endpoint resets don't reset hw pingpong state.
	 */
540 541
	at91_udp_write(udc, AT91_UDP_RST_EP, ep->int_mask);
	at91_udp_write(udc, AT91_UDP_RST_EP, 0);
542

543
	spin_unlock_irqrestore(&udc->lock, flags);
544 545 546 547 548 549
	return 0;
}

static int at91_ep_disable (struct usb_ep * _ep)
{
	struct at91_ep	*ep = container_of(_ep, struct at91_ep, ep);
550
	struct at91_udc	*udc = ep->udc;
551 552 553 554 555
	unsigned long	flags;

	if (ep == &ep->udc->ep[0])
		return -EINVAL;

556
	spin_lock_irqsave(&udc->lock, flags);
557 558 559 560 561

	nuke(ep, -ESHUTDOWN);

	/* restore the endpoint's pristine config */
	ep->desc = NULL;
562
	ep->ep.desc = NULL;
563 564 565 566
	ep->ep.maxpacket = ep->maxpacket;

	/* reset fifos and endpoint */
	if (ep->udc->clocked) {
567 568
		at91_udp_write(udc, AT91_UDP_RST_EP, ep->int_mask);
		at91_udp_write(udc, AT91_UDP_RST_EP, 0);
569 570 571
		__raw_writel(0, ep->creg);
	}

572
	spin_unlock_irqrestore(&udc->lock, flags);
573 574 575 576 577 578 579 580
	return 0;
}

/*
 * this is a PIO-only driver, so there's nothing
 * interesting for request or buffer allocation.
 */

581
static struct usb_request *
582
at91_ep_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
583 584 585
{
	struct at91_request *req;

586
	req = kzalloc(sizeof (struct at91_request), gfp_flags);
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
	if (!req)
		return NULL;

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

static void at91_ep_free_request(struct usb_ep *_ep, struct usb_request *_req)
{
	struct at91_request *req;

	req = container_of(_req, struct at91_request, req);
	BUG_ON(!list_empty(&req->queue));
	kfree(req);
}

static int at91_ep_queue(struct usb_ep *_ep,
			struct usb_request *_req, gfp_t gfp_flags)
{
	struct at91_request	*req;
	struct at91_ep		*ep;
608
	struct at91_udc		*udc;
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
	int			status;
	unsigned long		flags;

	req = container_of(_req, struct at91_request, req);
	ep = container_of(_ep, struct at91_ep, ep);

	if (!_req || !_req->complete
			|| !_req->buf || !list_empty(&req->queue)) {
		DBG("invalid request\n");
		return -EINVAL;
	}

	if (!_ep || (!ep->desc && ep->ep.name != ep0name)) {
		DBG("invalid ep\n");
		return -EINVAL;
	}

626
	udc = ep->udc;
627

628
	if (!udc || !udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN) {
629 630 631 632 633 634 635
		DBG("invalid device\n");
		return -EINVAL;
	}

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

636
	spin_lock_irqsave(&udc->lock, flags);
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653

	/* try to kickstart any empty and idle queue */
	if (list_empty(&ep->queue) && !ep->stopped) {
		int	is_ep0;

		/*
		 * If this control request has a non-empty DATA stage, this
		 * will start that stage.  It works just like a non-control
		 * request (until the status stage starts, maybe early).
		 *
		 * If the data stage is empty, then this starts a successful
		 * IN/STATUS stage.  (Unsuccessful ones use set_halt.)
		 */
		is_ep0 = (ep->ep.name == ep0name);
		if (is_ep0) {
			u32	tmp;

654
			if (!udc->req_pending) {
655 656 657 658 659 660 661 662
				status = -EINVAL;
				goto done;
			}

			/*
			 * defer changing CONFG until after the gadget driver
			 * reconfigures the endpoints.
			 */
663 664
			if (udc->wait_for_config_ack) {
				tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT);
665 666
				tmp ^= AT91_UDP_CONFG;
				VDBG("toggle config\n");
667
				at91_udp_write(udc, AT91_UDP_GLB_STAT, tmp);
668 669 670 671 672 673 674 675 676
			}
			if (req->req.length == 0) {
ep0_in_status:
				PACKET("ep0 in/status\n");
				status = 0;
				tmp = __raw_readl(ep->creg);
				tmp &= ~SET_FX;
				tmp |= CLR_FX | AT91_UDP_TXPKTRDY;
				__raw_writel(tmp, ep->creg);
677
				udc->req_pending = 0;
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
				goto done;
			}
		}

		if (ep->is_in)
			status = write_fifo(ep, req);
		else {
			status = read_fifo(ep, req);

			/* IN/STATUS stage is otherwise triggered by irq */
			if (status && is_ep0)
				goto ep0_in_status;
		}
	} else
		status = 0;

	if (req && !status) {
		list_add_tail (&req->queue, &ep->queue);
696
		at91_udp_write(udc, AT91_UDP_IER, ep->int_mask);
697 698
	}
done:
699
	spin_unlock_irqrestore(&udc->lock, flags);
700 701 702 703 704
	return (status < 0) ? status : 0;
}

static int at91_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
{
705
	struct at91_ep		*ep;
706
	struct at91_request	*req;
707 708
	unsigned long		flags;
	struct at91_udc		*udc;
709 710 711 712 713

	ep = container_of(_ep, struct at91_ep, ep);
	if (!_ep || ep->ep.name == ep0name)
		return -EINVAL;

714 715 716 717
	udc = ep->udc;

	spin_lock_irqsave(&udc->lock, flags);

718 719 720 721 722
	/* make sure it's actually queued on this endpoint */
	list_for_each_entry (req, &ep->queue, queue) {
		if (&req->req == _req)
			break;
	}
723 724
	if (&req->req != _req) {
		spin_unlock_irqrestore(&udc->lock, flags);
725
		return -EINVAL;
726
	}
727 728

	done(ep, req, -ECONNRESET);
729
	spin_unlock_irqrestore(&udc->lock, flags);
730 731 732 733 734 735
	return 0;
}

static int at91_ep_set_halt(struct usb_ep *_ep, int value)
{
	struct at91_ep	*ep = container_of(_ep, struct at91_ep, ep);
736
	struct at91_udc	*udc = ep->udc;
737 738 739 740 741 742 743 744 745
	u32 __iomem	*creg;
	u32		csr;
	unsigned long	flags;
	int		status = 0;

	if (!_ep || ep->is_iso || !ep->udc->clocked)
		return -EINVAL;

	creg = ep->creg;
746
	spin_lock_irqsave(&udc->lock, flags);
747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763

	csr = __raw_readl(creg);

	/*
	 * fail with still-busy IN endpoints, ensuring correct sequencing
	 * of data tx then stall.  note that the fifo rx bytecount isn't
	 * completely accurate as a tx bytecount.
	 */
	if (ep->is_in && (!list_empty(&ep->queue) || (csr >> 16) != 0))
		status = -EAGAIN;
	else {
		csr |= CLR_FX;
		csr &= ~SET_FX;
		if (value) {
			csr |= AT91_UDP_FORCESTALL;
			VDBG("halt %s\n", ep->ep.name);
		} else {
764 765
			at91_udp_write(udc, AT91_UDP_RST_EP, ep->int_mask);
			at91_udp_write(udc, AT91_UDP_RST_EP, 0);
766 767 768 769 770
			csr &= ~AT91_UDP_FORCESTALL;
		}
		__raw_writel(csr, creg);
	}

771
	spin_unlock_irqrestore(&udc->lock, flags);
772 773 774
	return status;
}

775
static const struct usb_ep_ops at91_ep_ops = {
776 777 778 779 780 781 782
	.enable		= at91_ep_enable,
	.disable	= at91_ep_disable,
	.alloc_request	= at91_ep_alloc_request,
	.free_request	= at91_ep_free_request,
	.queue		= at91_ep_queue,
	.dequeue	= at91_ep_dequeue,
	.set_halt	= at91_ep_set_halt,
783
	/* there's only imprecise fifo status reporting */
784 785 786 787 788 789
};

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

static int at91_get_frame(struct usb_gadget *gadget)
{
790 791
	struct at91_udc *udc = to_udc(gadget);

792 793
	if (!to_udc(gadget)->clocked)
		return -EINVAL;
794
	return at91_udp_read(udc, AT91_UDP_FRM_NUM) & AT91_UDP_NUM;
795 796 797 798 799 800 801 802 803
}

static int at91_wakeup(struct usb_gadget *gadget)
{
	struct at91_udc	*udc = to_udc(gadget);
	u32		glbstate;
	int		status = -EINVAL;
	unsigned long	flags;

804
	DBG("%s\n", __func__ );
805
	spin_lock_irqsave(&udc->lock, flags);
806 807 808 809 810 811

	if (!udc->clocked || !udc->suspended)
		goto done;

	/* NOTE:  some "early versions" handle ESR differently ... */

812
	glbstate = at91_udp_read(udc, AT91_UDP_GLB_STAT);
813 814 815
	if (!(glbstate & AT91_UDP_ESR))
		goto done;
	glbstate |= AT91_UDP_ESR;
816
	at91_udp_write(udc, AT91_UDP_GLB_STAT, glbstate);
817 818

done:
819
	spin_unlock_irqrestore(&udc->lock, flags);
820 821 822
	return status;
}

L
Lucas De Marchi 已提交
823
/* reinit == restore initial software state */
824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839
static void udc_reinit(struct at91_udc *udc)
{
	u32 i;

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

	for (i = 0; i < NUM_ENDPOINTS; i++) {
		struct at91_ep *ep = &udc->ep[i];

		if (i != 0)
			list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
		ep->desc = NULL;
		ep->stopped = 0;
		ep->fifo_bank = 0;
		ep->ep.maxpacket = ep->maxpacket;
840
		ep->creg = (void __iomem *) udc->udp_baseaddr + AT91_UDP_CSR(i);
841
		/* initialize one queue per endpoint */
842 843 844 845 846 847 848 849 850 851 852 853
		INIT_LIST_HEAD(&ep->queue);
	}
}

static void stop_activity(struct at91_udc *udc)
{
	struct usb_gadget_driver *driver = udc->driver;
	int i;

	if (udc->gadget.speed == USB_SPEED_UNKNOWN)
		driver = NULL;
	udc->gadget.speed = USB_SPEED_UNKNOWN;
854
	udc->suspended = 0;
855 856 857 858 859 860

	for (i = 0; i < NUM_ENDPOINTS; i++) {
		struct at91_ep *ep = &udc->ep[i];
		ep->stopped = 1;
		nuke(ep, -ESHUTDOWN);
	}
861 862
	if (driver) {
		spin_unlock(&udc->lock);
863
		driver->disconnect(&udc->gadget);
864 865
		spin_lock(&udc->lock);
	}
866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885

	udc_reinit(udc);
}

static void clk_on(struct at91_udc *udc)
{
	if (udc->clocked)
		return;
	udc->clocked = 1;
	clk_enable(udc->iclk);
	clk_enable(udc->fclk);
}

static void clk_off(struct at91_udc *udc)
{
	if (!udc->clocked)
		return;
	udc->clocked = 0;
	udc->gadget.speed = USB_SPEED_UNKNOWN;
	clk_disable(udc->fclk);
886
	clk_disable(udc->iclk);
887 888 889 890 891 892 893 894
}

/*
 * activate/deactivate link with host; minimize power usage for
 * inactive links by cutting clocks and transceiver power.
 */
static void pullup(struct at91_udc *udc, int is_on)
{
895 896
	int	active = !udc->board.pullup_active_low;

897 898 899
	if (!udc->enabled || !udc->vbus)
		is_on = 0;
	DBG("%sactive\n", is_on ? "" : "in");
900

901 902
	if (is_on) {
		clk_on(udc);
903
		at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_RXRSM);
904
		at91_udp_write(udc, AT91_UDP_TXVC, 0);
A
Andrew Victor 已提交
905
		if (cpu_is_at91rm9200())
906
			gpio_set_value(udc->board.pullup_pin, active);
907
		else if (cpu_is_at91sam9260() || cpu_is_at91sam9263() || cpu_is_at91sam9g20()) {
A
Andrew Victor 已提交
908 909 910 911
			u32	txvc = at91_udp_read(udc, AT91_UDP_TXVC);

			txvc |= AT91_UDP_TXVC_PUON;
			at91_udp_write(udc, AT91_UDP_TXVC, txvc);
H
Hong Xu 已提交
912
		} else if (cpu_is_at91sam9261() || cpu_is_at91sam9g10()) {
A
Andrew Victor 已提交
913 914
			u32	usbpucr;

915
			usbpucr = at91_matrix_read(AT91_MATRIX_USBPUCR);
A
Andrew Victor 已提交
916
			usbpucr |= AT91_MATRIX_USBPUCR_PUON;
917
			at91_matrix_write(AT91_MATRIX_USBPUCR, usbpucr);
A
Andrew Victor 已提交
918
		}
919
	} else {
920
		stop_activity(udc);
921
		at91_udp_write(udc, AT91_UDP_IDR, AT91_UDP_RXRSM);
922
		at91_udp_write(udc, AT91_UDP_TXVC, AT91_UDP_TXVC_TXVDIS);
A
Andrew Victor 已提交
923
		if (cpu_is_at91rm9200())
924
			gpio_set_value(udc->board.pullup_pin, !active);
925
		else if (cpu_is_at91sam9260() || cpu_is_at91sam9263() || cpu_is_at91sam9g20()) {
A
Andrew Victor 已提交
926 927 928 929
			u32	txvc = at91_udp_read(udc, AT91_UDP_TXVC);

			txvc &= ~AT91_UDP_TXVC_PUON;
			at91_udp_write(udc, AT91_UDP_TXVC, txvc);
H
Hong Xu 已提交
930
		} else if (cpu_is_at91sam9261() || cpu_is_at91sam9g10()) {
A
Andrew Victor 已提交
931 932
			u32	usbpucr;

933
			usbpucr = at91_matrix_read(AT91_MATRIX_USBPUCR);
A
Andrew Victor 已提交
934
			usbpucr &= ~AT91_MATRIX_USBPUCR_PUON;
935
			at91_matrix_write(AT91_MATRIX_USBPUCR, usbpucr);
A
Andrew Victor 已提交
936
		}
937 938 939 940 941 942 943 944 945 946
		clk_off(udc);
	}
}

/* vbus is here!  turn everything on that's ready */
static int at91_vbus_session(struct usb_gadget *gadget, int is_active)
{
	struct at91_udc	*udc = to_udc(gadget);
	unsigned long	flags;

947
	/* VDBG("vbus %s\n", is_active ? "on" : "off"); */
948
	spin_lock_irqsave(&udc->lock, flags);
949
	udc->vbus = (is_active != 0);
950 951 952 953
	if (udc->driver)
		pullup(udc, is_active);
	else
		pullup(udc, 0);
954
	spin_unlock_irqrestore(&udc->lock, flags);
955 956 957 958 959 960 961 962
	return 0;
}

static int at91_pullup(struct usb_gadget *gadget, int is_on)
{
	struct at91_udc	*udc = to_udc(gadget);
	unsigned long	flags;

963
	spin_lock_irqsave(&udc->lock, flags);
964 965
	udc->enabled = is_on = !!is_on;
	pullup(udc, is_on);
966
	spin_unlock_irqrestore(&udc->lock, flags);
967 968 969 970 971 972 973 974
	return 0;
}

static int at91_set_selfpowered(struct usb_gadget *gadget, int is_on)
{
	struct at91_udc	*udc = to_udc(gadget);
	unsigned long	flags;

975
	spin_lock_irqsave(&udc->lock, flags);
976
	udc->selfpowered = (is_on != 0);
977
	spin_unlock_irqrestore(&udc->lock, flags);
978 979 980
	return 0;
}

981 982 983 984
static int at91_start(struct usb_gadget_driver *driver,
		int (*bind)(struct usb_gadget *));
static int at91_stop(struct usb_gadget_driver *driver);

985 986 987 988 989 990
static const struct usb_gadget_ops at91_udc_ops = {
	.get_frame		= at91_get_frame,
	.wakeup			= at91_wakeup,
	.set_selfpowered	= at91_set_selfpowered,
	.vbus_session		= at91_vbus_session,
	.pullup			= at91_pullup,
991 992
	.start			= at91_start,
	.stop			= at91_stop,
993 994 995 996 997

	/*
	 * VBUS-powered devices may also also want to support bigger
	 * power budgets after an appropriate SET_CONFIGURATION.
	 */
998
	/* .vbus_power		= at91_vbus_power, */
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066
};

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

static int handle_ep(struct at91_ep *ep)
{
	struct at91_request	*req;
	u32 __iomem		*creg = ep->creg;
	u32			csr = __raw_readl(creg);

	if (!list_empty(&ep->queue))
		req = list_entry(ep->queue.next,
			struct at91_request, queue);
	else
		req = NULL;

	if (ep->is_in) {
		if (csr & (AT91_UDP_STALLSENT | AT91_UDP_TXCOMP)) {
			csr |= CLR_FX;
			csr &= ~(SET_FX | AT91_UDP_STALLSENT | AT91_UDP_TXCOMP);
			__raw_writel(csr, creg);
		}
		if (req)
			return write_fifo(ep, req);

	} else {
		if (csr & AT91_UDP_STALLSENT) {
			/* STALLSENT bit == ISOERR */
			if (ep->is_iso && req)
				req->req.status = -EILSEQ;
			csr |= CLR_FX;
			csr &= ~(SET_FX | AT91_UDP_STALLSENT);
			__raw_writel(csr, creg);
			csr = __raw_readl(creg);
		}
		if (req && (csr & RX_DATA_READY))
			return read_fifo(ep, req);
	}
	return 0;
}

union setup {
	u8			raw[8];
	struct usb_ctrlrequest	r;
};

static void handle_setup(struct at91_udc *udc, struct at91_ep *ep, u32 csr)
{
	u32 __iomem	*creg = ep->creg;
	u8 __iomem	*dreg = ep->creg + (AT91_UDP_FDR(0) - AT91_UDP_CSR(0));
	unsigned	rxcount, i = 0;
	u32		tmp;
	union setup	pkt;
	int		status = 0;

	/* read and ack SETUP; hard-fail for bogus packets */
	rxcount = (csr & AT91_UDP_RXBYTECNT) >> 16;
	if (likely(rxcount == 8)) {
		while (rxcount--)
			pkt.raw[i++] = __raw_readb(dreg);
		if (pkt.r.bRequestType & USB_DIR_IN) {
			csr |= AT91_UDP_DIR;
			ep->is_in = 1;
		} else {
			csr &= ~AT91_UDP_DIR;
			ep->is_in = 0;
		}
	} else {
1067
		/* REVISIT this happens sometimes under load; why?? */
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
		ERR("SETUP len %d, csr %08x\n", rxcount, csr);
		status = -EINVAL;
	}
	csr |= CLR_FX;
	csr &= ~(SET_FX | AT91_UDP_RXSETUP);
	__raw_writel(csr, creg);
	udc->wait_for_addr_ack = 0;
	udc->wait_for_config_ack = 0;
	ep->stopped = 0;
	if (unlikely(status != 0))
		goto stall;

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

	VDBG("SETUP %02x.%02x v%04x i%04x l%04x\n",
			pkt.r.bRequestType, pkt.r.bRequest,
			w_value, w_index, w_length);

	/*
	 * A few standard requests get handled here, ones that touch
	 * hardware ... notably for device and endpoint features.
	 */
	udc->req_pending = 1;
	csr = __raw_readl(creg);
	csr |= CLR_FX;
	csr &= ~SET_FX;
	switch ((pkt.r.bRequestType << 8) | pkt.r.bRequest) {

	case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
			| USB_REQ_SET_ADDRESS:
		__raw_writel(csr | AT91_UDP_TXPKTRDY, creg);
		udc->addr = w_value;
		udc->wait_for_addr_ack = 1;
		udc->req_pending = 0;
		/* FADDR is set later, when we ack host STATUS */
		return;

	case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
			| USB_REQ_SET_CONFIGURATION:
1109
		tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT) & AT91_UDP_CONFG;
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
		if (pkt.r.wValue)
			udc->wait_for_config_ack = (tmp == 0);
		else
			udc->wait_for_config_ack = (tmp != 0);
		if (udc->wait_for_config_ack)
			VDBG("wait for config\n");
		/* CONFG is toggled later, if gadget driver succeeds */
		break;

	/*
	 * Hosts may set or clear remote wakeup status, and
	 * devices may report they're VBUS powered.
	 */
	case ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
			| USB_REQ_GET_STATUS:
		tmp = (udc->selfpowered << USB_DEVICE_SELF_POWERED);
1126
		if (at91_udp_read(udc, AT91_UDP_GLB_STAT) & AT91_UDP_ESR)
1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
			tmp |= (1 << USB_DEVICE_REMOTE_WAKEUP);
		PACKET("get device status\n");
		__raw_writeb(tmp, dreg);
		__raw_writeb(0, dreg);
		goto write_in;
		/* then STATUS starts later, automatically */
	case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
			| USB_REQ_SET_FEATURE:
		if (w_value != USB_DEVICE_REMOTE_WAKEUP)
			goto stall;
1137
		tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT);
1138
		tmp |= AT91_UDP_ESR;
1139
		at91_udp_write(udc, AT91_UDP_GLB_STAT, tmp);
1140 1141 1142 1143 1144
		goto succeed;
	case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
			| USB_REQ_CLEAR_FEATURE:
		if (w_value != USB_DEVICE_REMOTE_WAKEUP)
			goto stall;
1145
		tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT);
1146
		tmp &= ~AT91_UDP_ESR;
1147
		at91_udp_write(udc, AT91_UDP_GLB_STAT, tmp);
1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
		goto succeed;

	/*
	 * Interfaces have no feature settings; this is pretty useless.
	 * we won't even insist the interface exists...
	 */
	case ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_INTERFACE) << 8)
			| USB_REQ_GET_STATUS:
		PACKET("get interface status\n");
		__raw_writeb(0, dreg);
		__raw_writeb(0, dreg);
		goto write_in;
		/* then STATUS starts later, automatically */
	case ((USB_TYPE_STANDARD|USB_RECIP_INTERFACE) << 8)
			| USB_REQ_SET_FEATURE:
	case ((USB_TYPE_STANDARD|USB_RECIP_INTERFACE) << 8)
			| USB_REQ_CLEAR_FEATURE:
		goto stall;

	/*
	 * Hosts may clear bulk/intr endpoint halt after the gadget
	 * driver sets it (not widely used); or set it (for testing)
	 */
	case ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_ENDPOINT) << 8)
			| USB_REQ_GET_STATUS:
		tmp = w_index & USB_ENDPOINT_NUMBER_MASK;
		ep = &udc->ep[tmp];
1175
		if (tmp >= NUM_ENDPOINTS || (tmp && !ep->desc))
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197
			goto stall;

		if (tmp) {
			if ((w_index & USB_DIR_IN)) {
				if (!ep->is_in)
					goto stall;
			} else if (ep->is_in)
				goto stall;
		}
		PACKET("get %s status\n", ep->ep.name);
		if (__raw_readl(ep->creg) & AT91_UDP_FORCESTALL)
			tmp = (1 << USB_ENDPOINT_HALT);
		else
			tmp = 0;
		__raw_writeb(tmp, dreg);
		__raw_writeb(0, dreg);
		goto write_in;
		/* then STATUS starts later, automatically */
	case ((USB_TYPE_STANDARD|USB_RECIP_ENDPOINT) << 8)
			| USB_REQ_SET_FEATURE:
		tmp = w_index & USB_ENDPOINT_NUMBER_MASK;
		ep = &udc->ep[tmp];
1198
		if (w_value != USB_ENDPOINT_HALT || tmp >= NUM_ENDPOINTS)
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216
			goto stall;
		if (!ep->desc || ep->is_iso)
			goto stall;
		if ((w_index & USB_DIR_IN)) {
			if (!ep->is_in)
				goto stall;
		} else if (ep->is_in)
			goto stall;

		tmp = __raw_readl(ep->creg);
		tmp &= ~SET_FX;
		tmp |= CLR_FX | AT91_UDP_FORCESTALL;
		__raw_writel(tmp, ep->creg);
		goto succeed;
	case ((USB_TYPE_STANDARD|USB_RECIP_ENDPOINT) << 8)
			| USB_REQ_CLEAR_FEATURE:
		tmp = w_index & USB_ENDPOINT_NUMBER_MASK;
		ep = &udc->ep[tmp];
1217
		if (w_value != USB_ENDPOINT_HALT || tmp >= NUM_ENDPOINTS)
1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228
			goto stall;
		if (tmp == 0)
			goto succeed;
		if (!ep->desc || ep->is_iso)
			goto stall;
		if ((w_index & USB_DIR_IN)) {
			if (!ep->is_in)
				goto stall;
		} else if (ep->is_in)
			goto stall;

1229 1230
		at91_udp_write(udc, AT91_UDP_RST_EP, ep->int_mask);
		at91_udp_write(udc, AT91_UDP_RST_EP, 0);
1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244
		tmp = __raw_readl(ep->creg);
		tmp |= CLR_FX;
		tmp &= ~(SET_FX | AT91_UDP_FORCESTALL);
		__raw_writel(tmp, ep->creg);
		if (!list_empty(&ep->queue))
			handle_ep(ep);
		goto succeed;
	}

#undef w_value
#undef w_index
#undef w_length

	/* pass request up to the gadget driver */
1245 1246
	if (udc->driver) {
		spin_unlock(&udc->lock);
1247
		status = udc->driver->setup(&udc->gadget, &pkt.r);
1248 1249
		spin_lock(&udc->lock);
	}
1250 1251
	else
		status = -ENODEV;
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327
	if (status < 0) {
stall:
		VDBG("req %02x.%02x protocol STALL; stat %d\n",
				pkt.r.bRequestType, pkt.r.bRequest, status);
		csr |= AT91_UDP_FORCESTALL;
		__raw_writel(csr, creg);
		udc->req_pending = 0;
	}
	return;

succeed:
	/* immediate successful (IN) STATUS after zero length DATA */
	PACKET("ep0 in/status\n");
write_in:
	csr |= AT91_UDP_TXPKTRDY;
	__raw_writel(csr, creg);
	udc->req_pending = 0;
}

static void handle_ep0(struct at91_udc *udc)
{
	struct at91_ep		*ep0 = &udc->ep[0];
	u32 __iomem		*creg = ep0->creg;
	u32			csr = __raw_readl(creg);
	struct at91_request	*req;

	if (unlikely(csr & AT91_UDP_STALLSENT)) {
		nuke(ep0, -EPROTO);
		udc->req_pending = 0;
		csr |= CLR_FX;
		csr &= ~(SET_FX | AT91_UDP_STALLSENT | AT91_UDP_FORCESTALL);
		__raw_writel(csr, creg);
		VDBG("ep0 stalled\n");
		csr = __raw_readl(creg);
	}
	if (csr & AT91_UDP_RXSETUP) {
		nuke(ep0, 0);
		udc->req_pending = 0;
		handle_setup(udc, ep0, csr);
		return;
	}

	if (list_empty(&ep0->queue))
		req = NULL;
	else
		req = list_entry(ep0->queue.next, struct at91_request, queue);

	/* host ACKed an IN packet that we sent */
	if (csr & AT91_UDP_TXCOMP) {
		csr |= CLR_FX;
		csr &= ~(SET_FX | AT91_UDP_TXCOMP);

		/* write more IN DATA? */
		if (req && ep0->is_in) {
			if (handle_ep(ep0))
				udc->req_pending = 0;

		/*
		 * Ack after:
		 *  - last IN DATA packet (including GET_STATUS)
		 *  - IN/STATUS for OUT DATA
		 *  - IN/STATUS for any zero-length DATA stage
		 * except for the IN DATA case, the host should send
		 * an OUT status later, which we'll ack.
		 */
		} else {
			udc->req_pending = 0;
			__raw_writel(csr, creg);

			/*
			 * SET_ADDRESS takes effect only after the STATUS
			 * (to the original address) gets acked.
			 */
			if (udc->wait_for_addr_ack) {
				u32	tmp;

1328
				at91_udp_write(udc, AT91_UDP_FADDR,
1329
						AT91_UDP_FEN | udc->addr);
1330
				tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT);
1331 1332 1333
				tmp &= ~AT91_UDP_FADDEN;
				if (udc->addr)
					tmp |= AT91_UDP_FADDEN;
1334
				at91_udp_write(udc, AT91_UDP_GLB_STAT, tmp);
1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393

				udc->wait_for_addr_ack = 0;
				VDBG("address %d\n", udc->addr);
			}
		}
	}

	/* OUT packet arrived ... */
	else if (csr & AT91_UDP_RX_DATA_BK0) {
		csr |= CLR_FX;
		csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK0);

		/* OUT DATA stage */
		if (!ep0->is_in) {
			if (req) {
				if (handle_ep(ep0)) {
					/* send IN/STATUS */
					PACKET("ep0 in/status\n");
					csr = __raw_readl(creg);
					csr &= ~SET_FX;
					csr |= CLR_FX | AT91_UDP_TXPKTRDY;
					__raw_writel(csr, creg);
					udc->req_pending = 0;
				}
			} else if (udc->req_pending) {
				/*
				 * AT91 hardware has a hard time with this
				 * "deferred response" mode for control-OUT
				 * transfers.  (For control-IN it's fine.)
				 *
				 * The normal solution leaves OUT data in the
				 * fifo until the gadget driver is ready.
				 * We couldn't do that here without disabling
				 * the IRQ that tells about SETUP packets,
				 * e.g. when the host gets impatient...
				 *
				 * Working around it by copying into a buffer
				 * would almost be a non-deferred response,
				 * except that it wouldn't permit reliable
				 * stalling of the request.  Instead, demand
				 * that gadget drivers not use this mode.
				 */
				DBG("no control-OUT deferred responses!\n");
				__raw_writel(csr | AT91_UDP_FORCESTALL, creg);
				udc->req_pending = 0;
			}

		/* STATUS stage for control-IN; ack.  */
		} else {
			PACKET("ep0 out/status ACK\n");
			__raw_writel(csr, creg);

			/* "early" status stage */
			if (req)
				done(ep0, req, 0);
		}
	}
}

1394
static irqreturn_t at91_udc_irq (int irq, void *_udc)
1395 1396 1397
{
	struct at91_udc		*udc = _udc;
	u32			rescans = 5;
1398
	int			disable_clock = 0;
1399 1400 1401
	unsigned long		flags;

	spin_lock_irqsave(&udc->lock, flags);
1402 1403 1404 1405 1406

	if (!udc->clocked) {
		clk_on(udc);
		disable_clock = 1;
	}
1407 1408

	while (rescans--) {
1409
		u32 status;
1410

1411 1412
		status = at91_udp_read(udc, AT91_UDP_ISR)
			& at91_udp_read(udc, AT91_UDP_IMR);
1413 1414 1415 1416 1417
		if (!status)
			break;

		/* USB reset irq:  not maskable */
		if (status & AT91_UDP_ENDBUSRES) {
1418 1419
			at91_udp_write(udc, AT91_UDP_IDR, ~MINIMUS_INTERRUPTUS);
			at91_udp_write(udc, AT91_UDP_IER, MINIMUS_INTERRUPTUS);
1420
			/* Atmel code clears this irq twice */
1421 1422
			at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_ENDBUSRES);
			at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_ENDBUSRES);
1423 1424 1425 1426 1427
			VDBG("end bus reset\n");
			udc->addr = 0;
			stop_activity(udc);

			/* enable ep0 */
1428
			at91_udp_write(udc, AT91_UDP_CSR(0),
1429
					AT91_UDP_EPEDS | AT91_UDP_EPTYPE_CTRL);
1430 1431
			udc->gadget.speed = USB_SPEED_FULL;
			udc->suspended = 0;
1432
			at91_udp_write(udc, AT91_UDP_IER, AT91_UDP_EP(0));
1433 1434 1435

			/*
			 * NOTE:  this driver keeps clocks off unless the
1436 1437 1438
			 * USB host is present.  That saves power, but for
			 * boards that don't support VBUS detection, both
			 * clocks need to be active most of the time.
1439 1440 1441 1442
			 */

		/* host initiated suspend (3+ms bus idle) */
		} else if (status & AT91_UDP_RXSUSP) {
1443 1444 1445
			at91_udp_write(udc, AT91_UDP_IDR, AT91_UDP_RXSUSP);
			at91_udp_write(udc, AT91_UDP_IER, AT91_UDP_RXRSM);
			at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_RXSUSP);
1446
			/* VDBG("bus suspend\n"); */
1447 1448 1449 1450 1451 1452 1453 1454 1455 1456
			if (udc->suspended)
				continue;
			udc->suspended = 1;

			/*
			 * NOTE:  when suspending a VBUS-powered device, the
			 * gadget driver should switch into slow clock mode
			 * and then into standby to avoid drawing more than
			 * 500uA power (2500uA for some high-power configs).
			 */
1457 1458
			if (udc->driver && udc->driver->suspend) {
				spin_unlock(&udc->lock);
1459
				udc->driver->suspend(&udc->gadget);
1460 1461
				spin_lock(&udc->lock);
			}
1462 1463 1464

		/* host initiated resume */
		} else if (status & AT91_UDP_RXRSM) {
1465 1466 1467
			at91_udp_write(udc, AT91_UDP_IDR, AT91_UDP_RXRSM);
			at91_udp_write(udc, AT91_UDP_IER, AT91_UDP_RXSUSP);
			at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_RXRSM);
1468
			/* VDBG("bus resume\n"); */
1469 1470 1471 1472 1473 1474 1475 1476 1477
			if (!udc->suspended)
				continue;
			udc->suspended = 0;

			/*
			 * NOTE:  for a VBUS-powered device, the gadget driver
			 * would normally want to switch out of slow clock
			 * mode into normal mode.
			 */
1478 1479
			if (udc->driver && udc->driver->resume) {
				spin_unlock(&udc->lock);
1480
				udc->driver->resume(&udc->gadget);
1481 1482
				spin_lock(&udc->lock);
			}
1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500

		/* endpoint IRQs are cleared by handling them */
		} else {
			int		i;
			unsigned	mask = 1;
			struct at91_ep	*ep = &udc->ep[1];

			if (status & mask)
				handle_ep0(udc);
			for (i = 1; i < NUM_ENDPOINTS; i++) {
				mask <<= 1;
				if (status & mask)
					handle_ep(ep);
				ep++;
			}
		}
	}

1501 1502 1503
	if (disable_clock)
		clk_off(udc);

1504 1505
	spin_unlock_irqrestore(&udc->lock, flags);

1506 1507 1508 1509 1510
	return IRQ_HANDLED;
}

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

1511 1512 1513 1514 1515
static void nop_release(struct device *dev)
{
	/* nothing to free */
}

1516 1517
static struct at91_udc controller = {
	.gadget = {
1518 1519 1520 1521
		.ops	= &at91_udc_ops,
		.ep0	= &controller.ep[0].ep,
		.name	= driver_name,
		.dev	= {
1522
			.init_name = "gadget",
1523
			.release = nop_release,
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
		}
	},
	.ep[0] = {
		.ep = {
			.name	= ep0name,
			.ops	= &at91_ep_ops,
		},
		.udc		= &controller,
		.maxpacket	= 8,
		.int_mask	= 1 << 0,
	},
	.ep[1] = {
		.ep = {
			.name	= "ep1",
			.ops	= &at91_ep_ops,
		},
		.udc		= &controller,
		.is_pingpong	= 1,
		.maxpacket	= 64,
		.int_mask	= 1 << 1,
	},
	.ep[2] = {
		.ep = {
			.name	= "ep2",
			.ops	= &at91_ep_ops,
		},
		.udc		= &controller,
		.is_pingpong	= 1,
		.maxpacket	= 64,
		.int_mask	= 1 << 2,
	},
	.ep[3] = {
		.ep = {
			/* could actually do bulk too */
			.name	= "ep3-int",
			.ops	= &at91_ep_ops,
		},
		.udc		= &controller,
		.maxpacket	= 8,
		.int_mask	= 1 << 3,
	},
	.ep[4] = {
		.ep = {
			.name	= "ep4",
			.ops	= &at91_ep_ops,
		},
		.udc		= &controller,
		.is_pingpong	= 1,
		.maxpacket	= 256,
		.int_mask	= 1 << 4,
	},
	.ep[5] = {
		.ep = {
			.name	= "ep5",
			.ops	= &at91_ep_ops,
		},
		.udc		= &controller,
		.is_pingpong	= 1,
		.maxpacket	= 256,
		.int_mask	= 1 << 5,
	},
1585
	/* ep6 and ep7 are also reserved (custom silicon might use them) */
1586 1587
};

1588 1589 1590 1591 1592 1593 1594
static void at91_vbus_update(struct at91_udc *udc, unsigned value)
{
	value ^= udc->board.vbus_active_low;
	if (value != udc->vbus)
		at91_vbus_session(&udc->gadget, value);
}

1595
static irqreturn_t at91_vbus_irq(int irq, void *_udc)
1596 1597 1598 1599 1600
{
	struct at91_udc	*udc = _udc;

	/* vbus needs at least brief debouncing */
	udelay(10);
1601
	at91_vbus_update(udc, gpio_get_value(udc->board.vbus_pin));
1602 1603 1604 1605

	return IRQ_HANDLED;
}

1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629
static void at91_vbus_timer_work(struct work_struct *work)
{
	struct at91_udc *udc = container_of(work, struct at91_udc,
					    vbus_timer_work);

	at91_vbus_update(udc, gpio_get_value_cansleep(udc->board.vbus_pin));

	if (!timer_pending(&udc->vbus_timer))
		mod_timer(&udc->vbus_timer, jiffies + VBUS_POLL_TIMEOUT);
}

static void at91_vbus_timer(unsigned long data)
{
	struct at91_udc *udc = (struct at91_udc *)data;

	/*
	 * If we are polling vbus it is likely that the gpio is on an
	 * bus such as i2c or spi which may sleep, so schedule some work
	 * to read the vbus gpio
	 */
	if (!work_pending(&udc->vbus_timer_work))
		schedule_work(&udc->vbus_timer_work);
}

1630
static int at91_start(struct usb_gadget_driver *driver,
1631
		int (*bind)(struct usb_gadget *))
1632 1633 1634
{
	struct at91_udc	*udc = &controller;
	int		retval;
1635
	unsigned long	flags;
1636 1637

	if (!driver
1638
			|| driver->max_speed < USB_SPEED_FULL
1639
			|| !bind
1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651
			|| !driver->setup) {
		DBG("bad parameter.\n");
		return -EINVAL;
	}

	if (udc->driver) {
		DBG("UDC already has a gadget driver\n");
		return -EBUSY;
	}

	udc->driver = driver;
	udc->gadget.dev.driver = &driver->driver;
1652
	dev_set_drvdata(&udc->gadget.dev, &driver->driver);
1653 1654 1655
	udc->enabled = 1;
	udc->selfpowered = 1;

1656
	retval = bind(&udc->gadget);
1657
	if (retval) {
1658
		DBG("bind() returned %d\n", retval);
1659
		udc->driver = NULL;
1660
		udc->gadget.dev.driver = NULL;
1661
		dev_set_drvdata(&udc->gadget.dev, NULL);
1662 1663
		udc->enabled = 0;
		udc->selfpowered = 0;
1664 1665 1666
		return retval;
	}

1667
	spin_lock_irqsave(&udc->lock, flags);
1668
	pullup(udc, 1);
1669
	spin_unlock_irqrestore(&udc->lock, flags);
1670 1671 1672 1673 1674

	DBG("bound to %s\n", driver->driver.name);
	return 0;
}

1675
static int at91_stop(struct usb_gadget_driver *driver)
1676 1677
{
	struct at91_udc *udc = &controller;
1678
	unsigned long	flags;
1679

1680
	if (!driver || driver != udc->driver || !driver->unbind)
1681 1682
		return -EINVAL;

1683
	spin_lock_irqsave(&udc->lock, flags);
1684
	udc->enabled = 0;
1685
	at91_udp_write(udc, AT91_UDP_IDR, ~0);
1686
	pullup(udc, 0);
1687
	spin_unlock_irqrestore(&udc->lock, flags);
1688 1689

	driver->unbind(&udc->gadget);
1690
	udc->gadget.dev.driver = NULL;
1691
	dev_set_drvdata(&udc->gadget.dev, NULL);
1692 1693 1694 1695 1696 1697 1698 1699 1700 1701
	udc->driver = NULL;

	DBG("unbound from %s\n", driver->driver.name);
	return 0;
}

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

static void at91udc_shutdown(struct platform_device *dev)
{
1702 1703 1704
	struct at91_udc *udc = platform_get_drvdata(dev);
	unsigned long	flags;

1705
	/* force disconnect on reboot */
1706
	spin_lock_irqsave(&udc->lock, flags);
1707
	pullup(platform_get_drvdata(dev), 0);
1708
	spin_unlock_irqrestore(&udc->lock, flags);
1709 1710
}

1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731
static void __devinit at91udc_of_init(struct at91_udc *udc,
				     struct device_node *np)
{
	struct at91_udc_data *board = &udc->board;
	u32 val;
	enum of_gpio_flags flags;

	if (of_property_read_u32(np, "atmel,vbus-polled", &val) == 0)
		board->vbus_polled = 1;

	board->vbus_pin = of_get_named_gpio_flags(np, "atmel,vbus-gpio", 0,
						  &flags);
	board->vbus_active_low = (flags & OF_GPIO_ACTIVE_LOW) ? 1 : 0;

	board->pullup_pin = of_get_named_gpio_flags(np, "atmel,pullup-gpio", 0,
						  &flags);

	board->pullup_active_low = (flags & OF_GPIO_ACTIVE_LOW) ? 1 : 0;
}

static int __devinit at91udc_probe(struct platform_device *pdev)
1732 1733 1734 1735
{
	struct device	*dev = &pdev->dev;
	struct at91_udc	*udc;
	int		retval;
1736
	struct resource	*res;
1737 1738 1739 1740 1741 1742 1743

	if (!dev->platform_data) {
		/* small (so we copy it) but critical! */
		DBG("missing platform_data\n");
		return -ENODEV;
	}

1744
	if (pdev->num_resources != 2) {
1745
		DBG("invalid num_resources\n");
1746 1747 1748 1749
		return -ENODEV;
	}
	if ((pdev->resource[0].flags != IORESOURCE_MEM)
			|| (pdev->resource[1].flags != IORESOURCE_IRQ)) {
1750
		DBG("invalid resource type\n");
1751 1752 1753
		return -ENODEV;
	}

1754 1755 1756 1757
	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!res)
		return -ENXIO;

1758
	if (!request_mem_region(res->start, resource_size(res), driver_name)) {
1759 1760 1761 1762 1763 1764 1765
		DBG("someone's using UDC memory\n");
		return -EBUSY;
	}

	/* init software state */
	udc = &controller;
	udc->gadget.dev.parent = dev;
1766 1767 1768 1769 1770
	if (pdev->dev.of_node)
		at91udc_of_init(udc, pdev->dev.of_node);
	else
		memcpy(&udc->board, dev->platform_data,
		       sizeof(struct at91_udc_data));
1771 1772
	udc->pdev = pdev;
	udc->enabled = 0;
1773
	spin_lock_init(&udc->lock);
1774

1775 1776
	/* rm9200 needs manual D+ pullup; off by default */
	if (cpu_is_at91rm9200()) {
1777
		if (gpio_is_valid(udc->board.pullup_pin)) {
1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790
			DBG("no D+ pullup?\n");
			retval = -ENODEV;
			goto fail0;
		}
		retval = gpio_request(udc->board.pullup_pin, "udc_pullup");
		if (retval) {
			DBG("D+ pullup is busy\n");
			goto fail0;
		}
		gpio_direction_output(udc->board.pullup_pin,
				udc->board.pullup_active_low);
	}

1791
	/* newer chips have more FIFO memory than rm9200 */
1792
	if (cpu_is_at91sam9260() || cpu_is_at91sam9g20()) {
1793 1794 1795 1796
		udc->ep[0].maxpacket = 64;
		udc->ep[3].maxpacket = 64;
		udc->ep[4].maxpacket = 512;
		udc->ep[5].maxpacket = 512;
H
Hong Xu 已提交
1797
	} else if (cpu_is_at91sam9261() || cpu_is_at91sam9g10()) {
1798 1799 1800 1801 1802 1803
		udc->ep[3].maxpacket = 64;
	} else if (cpu_is_at91sam9263()) {
		udc->ep[0].maxpacket = 64;
		udc->ep[3].maxpacket = 64;
	}

1804
	udc->udp_baseaddr = ioremap(res->start, resource_size(res));
1805
	if (!udc->udp_baseaddr) {
1806 1807
		retval = -ENOMEM;
		goto fail0a;
1808 1809 1810 1811
	}

	udc_reinit(udc);

1812 1813 1814 1815 1816
	/* get interface and function clocks */
	udc->iclk = clk_get(dev, "udc_clk");
	udc->fclk = clk_get(dev, "udpck");
	if (IS_ERR(udc->iclk) || IS_ERR(udc->fclk)) {
		DBG("clocks missing\n");
A
Andrew Victor 已提交
1817
		retval = -ENODEV;
1818 1819
		/* NOTE: we "know" here that refcounts on these are NOPs */
		goto fail0b;
1820 1821 1822
	}

	retval = device_register(&udc->gadget.dev);
1823 1824
	if (retval < 0) {
		put_device(&udc->gadget.dev);
1825
		goto fail0b;
1826
	}
1827

1828 1829
	/* don't do anything until we have both gadget driver and VBUS */
	clk_enable(udc->iclk);
1830 1831
	at91_udp_write(udc, AT91_UDP_TXVC, AT91_UDP_TXVC_TXVDIS);
	at91_udp_write(udc, AT91_UDP_IDR, 0xffffffff);
A
Andrew Victor 已提交
1832 1833
	/* Clear all pending interrupts - UDP may be used by bootloader. */
	at91_udp_write(udc, AT91_UDP_ICR, 0xffffffff);
1834
	clk_disable(udc->iclk);
1835 1836

	/* request UDC and maybe VBUS irqs */
1837
	udc->udp_irq = platform_get_irq(pdev, 0);
1838
	retval = request_irq(udc->udp_irq, at91_udc_irq,
Y
Yong Zhang 已提交
1839
			0, driver_name, udc);
1840
	if (retval < 0) {
1841
		DBG("request irq %d failed\n", udc->udp_irq);
1842 1843
		goto fail1;
	}
1844
	if (gpio_is_valid(udc->board.vbus_pin)) {
1845 1846 1847 1848 1849 1850 1851
		retval = gpio_request(udc->board.vbus_pin, "udc_vbus");
		if (retval < 0) {
			DBG("request vbus pin failed\n");
			goto fail2;
		}
		gpio_direction_input(udc->board.vbus_pin);

A
Andrew Victor 已提交
1852 1853 1854 1855
		/*
		 * Get the initial state of VBUS - we cannot expect
		 * a pending interrupt.
		 */
1856 1857 1858 1859 1860 1861 1862 1863 1864 1865
		udc->vbus = gpio_get_value_cansleep(udc->board.vbus_pin) ^
			udc->board.vbus_active_low;

		if (udc->board.vbus_polled) {
			INIT_WORK(&udc->vbus_timer_work, at91_vbus_timer_work);
			setup_timer(&udc->vbus_timer, at91_vbus_timer,
				    (unsigned long)udc);
			mod_timer(&udc->vbus_timer,
				  jiffies + VBUS_POLL_TIMEOUT);
		} else {
1866 1867
			if (request_irq(gpio_to_irq(udc->board.vbus_pin),
					at91_vbus_irq, 0, driver_name, udc)) {
1868 1869 1870 1871 1872
				DBG("request vbus irq %d failed\n",
				    udc->board.vbus_pin);
				retval = -EBUSY;
				goto fail3;
			}
1873 1874 1875 1876 1877
		}
	} else {
		DBG("no VBUS detection, assuming always-on\n");
		udc->vbus = 1;
	}
1878 1879 1880
	retval = usb_add_gadget_udc(dev, &udc->gadget);
	if (retval)
		goto fail4;
1881
	dev_set_drvdata(dev, udc);
1882
	device_init_wakeup(dev, 1);
1883 1884 1885 1886
	create_debug_file(udc);

	INFO("%s version %s\n", driver_name, DRIVER_VERSION);
	return 0;
1887
fail4:
1888
	if (gpio_is_valid(udc->board.vbus_pin) && !udc->board.vbus_polled)
1889
		free_irq(gpio_to_irq(udc->board.vbus_pin), udc);
1890
fail3:
1891
	if (gpio_is_valid(udc->board.vbus_pin))
1892 1893 1894
		gpio_free(udc->board.vbus_pin);
fail2:
	free_irq(udc->udp_irq, udc);
1895 1896
fail1:
	device_unregister(&udc->gadget.dev);
1897 1898 1899 1900 1901
fail0b:
	iounmap(udc->udp_baseaddr);
fail0a:
	if (cpu_is_at91rm9200())
		gpio_free(udc->board.pullup_pin);
1902
fail0:
1903
	release_mem_region(res->start, resource_size(res));
1904 1905 1906 1907
	DBG("%s probe failed, %d\n", driver_name, retval);
	return retval;
}

1908
static int __exit at91udc_remove(struct platform_device *pdev)
1909
{
1910
	struct at91_udc *udc = platform_get_drvdata(pdev);
1911
	struct resource *res;
1912
	unsigned long	flags;
1913 1914 1915

	DBG("remove\n");

1916
	usb_del_gadget_udc(&udc->gadget);
1917 1918
	if (udc->driver)
		return -EBUSY;
1919

1920
	spin_lock_irqsave(&udc->lock, flags);
1921
	pullup(udc, 0);
1922
	spin_unlock_irqrestore(&udc->lock, flags);
1923

1924
	device_init_wakeup(&pdev->dev, 0);
1925
	remove_debug_file(udc);
1926
	if (gpio_is_valid(udc->board.vbus_pin)) {
1927
		free_irq(gpio_to_irq(udc->board.vbus_pin), udc);
1928 1929
		gpio_free(udc->board.vbus_pin);
	}
1930
	free_irq(udc->udp_irq, udc);
1931
	device_unregister(&udc->gadget.dev);
1932 1933

	iounmap(udc->udp_baseaddr);
1934 1935 1936 1937

	if (cpu_is_at91rm9200())
		gpio_free(udc->board.pullup_pin);

1938
	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1939
	release_mem_region(res->start, resource_size(res));
1940 1941 1942 1943 1944 1945 1946 1947

	clk_put(udc->iclk);
	clk_put(udc->fclk);

	return 0;
}

#ifdef CONFIG_PM
1948
static int at91udc_suspend(struct platform_device *pdev, pm_message_t mesg)
1949
{
1950 1951
	struct at91_udc *udc = platform_get_drvdata(pdev);
	int		wake = udc->driver && device_may_wakeup(&pdev->dev);
1952
	unsigned long	flags;
1953

1954 1955 1956 1957
	/* Unless we can act normally to the host (letting it wake us up
	 * whenever it has work for us) force disconnect.  Wakeup requires
	 * PLLB for USB events (signaling for reset, wakeup, or incoming
	 * tokens) and VBUS irqs (on systems which support them).
1958
	 */
1959 1960 1961
	if ((!udc->suspended && udc->addr)
			|| !wake
			|| at91_suspend_entering_slow_clock()) {
1962
		spin_lock_irqsave(&udc->lock, flags);
1963
		pullup(udc, 0);
1964
		wake = 0;
1965
		spin_unlock_irqrestore(&udc->lock, flags);
1966 1967 1968
	} else
		enable_irq_wake(udc->udp_irq);

1969
	udc->active_suspend = wake;
1970
	if (gpio_is_valid(udc->board.vbus_pin) && !udc->board.vbus_polled && wake)
1971
		enable_irq_wake(udc->board.vbus_pin);
1972 1973 1974
	return 0;
}

1975
static int at91udc_resume(struct platform_device *pdev)
1976
{
1977
	struct at91_udc *udc = platform_get_drvdata(pdev);
1978
	unsigned long	flags;
1979

1980
	if (gpio_is_valid(udc->board.vbus_pin) && !udc->board.vbus_polled &&
1981
	    udc->active_suspend)
1982 1983
		disable_irq_wake(udc->board.vbus_pin);

1984
	/* maybe reconnect to host; if so, clocks on */
1985 1986
	if (udc->active_suspend)
		disable_irq_wake(udc->udp_irq);
1987 1988
	else {
		spin_lock_irqsave(&udc->lock, flags);
1989
		pullup(udc, 1);
1990 1991
		spin_unlock_irqrestore(&udc->lock, flags);
	}
1992 1993 1994 1995 1996 1997 1998
	return 0;
}
#else
#define	at91udc_suspend	NULL
#define	at91udc_resume	NULL
#endif

1999 2000 2001 2002 2003 2004 2005 2006 2007
#if defined(CONFIG_OF)
static const struct of_device_id at91_udc_dt_ids[] = {
	{ .compatible = "atmel,at91rm9200-udc" },
	{ /* sentinel */ }
};

MODULE_DEVICE_TABLE(of, at91_udc_dt_ids);
#endif

2008
static struct platform_driver at91_udc_driver = {
2009
	.remove		= __exit_p(at91udc_remove),
2010 2011
	.shutdown	= at91udc_shutdown,
	.suspend	= at91udc_suspend,
2012
	.resume		= at91udc_resume,
2013 2014 2015
	.driver		= {
		.name	= (char *) driver_name,
		.owner	= THIS_MODULE,
2016
		.of_match_table	= of_match_ptr(at91_udc_dt_ids),
2017 2018 2019
	},
};

2020
static int __init udc_init_module(void)
2021
{
2022
	return platform_driver_probe(&at91_udc_driver, at91udc_probe);
2023 2024 2025
}
module_init(udc_init_module);

2026
static void __exit udc_exit_module(void)
2027
{
2028
	platform_driver_unregister(&at91_udc_driver);
2029 2030 2031
}
module_exit(udc_exit_module);

2032
MODULE_DESCRIPTION("AT91 udc driver");
2033 2034
MODULE_AUTHOR("Thomas Rathbone, David Brownell");
MODULE_LICENSE("GPL");
2035
MODULE_ALIAS("platform:at91_udc");