omap_udc.c 80.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6
/*
 * omap_udc.c -- for OMAP full speed udc; most chips support OTG.
 *
 * Copyright (C) 2004 Texas Instruments, Inc.
 * Copyright (C) 2004-2005 David Brownell
 *
K
Kyungmin Park 已提交
7 8
 * OMAP2 & DMA support by Kyungmin Park <kyungmin.park@samsung.com>
 *
L
Linus Torvalds 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
 * 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.
 */

#undef	DEBUG
#undef	VERBOSE

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/ioport.h>
#include <linux/types.h>
#include <linux/errno.h>
#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/timer.h>
#include <linux/list.h>
#include <linux/interrupt.h>
#include <linux/proc_fs.h>
#include <linux/mm.h>
#include <linux/moduleparam.h>
32
#include <linux/platform_device.h>
33
#include <linux/usb/ch9.h>
34
#include <linux/usb/gadget.h>
35
#include <linux/usb/otg.h>
L
Linus Torvalds 已提交
36
#include <linux/dma-mapping.h>
37
#include <linux/clk.h>
38
#include <linux/prefetch.h>
L
Linus Torvalds 已提交
39 40 41 42 43 44 45

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

46 47
#include <plat/dma.h>
#include <plat/usb.h>
L
Linus Torvalds 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

#include "omap_udc.h"

#undef	USB_TRACE

/* bulk DMA seems to be behaving for both IN and OUT */
#define	USE_DMA

/* ISO too */
#define	USE_ISO

#define	DRIVER_DESC	"OMAP UDC driver"
#define	DRIVER_VERSION	"4 October 2004"

#define	DMA_ADDR_INVALID	(~(dma_addr_t)0)

K
Kyungmin Park 已提交
64 65
#define OMAP2_DMA_CH(ch)	(((ch) - 1) << 1)
#define OMAP24XX_DMA(name, ch)	(OMAP24XX_DMA_##name + OMAP2_DMA_CH(ch))
L
Linus Torvalds 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96

/*
 * The OMAP UDC needs _very_ early endpoint setup:  before enabling the
 * D+ pullup to allow enumeration.  That's too early for the gadget
 * framework to use from usb_endpoint_enable(), which happens after
 * enumeration as part of activating an interface.  (But if we add an
 * optional new "UDC not yet running" state to the gadget driver model,
 * even just during driver binding, the endpoint autoconfig logic is the
 * natural spot to manufacture new endpoints.)
 *
 * So instead of using endpoint enable calls to control the hardware setup,
 * this driver defines a "fifo mode" parameter.  It's used during driver
 * initialization to choose among a set of pre-defined endpoint configs.
 * See omap_udc_setup() for available modes, or to add others.  That code
 * lives in an init section, so use this driver as a module if you need
 * to change the fifo mode after the kernel boots.
 *
 * Gadget drivers normally ignore endpoints they don't care about, and
 * won't include them in configuration descriptors.  That means only
 * misbehaving hosts would even notice they exist.
 */
#ifdef	USE_ISO
static unsigned fifo_mode = 3;
#else
static unsigned fifo_mode = 0;
#endif

/* "modprobe omap_udc fifo_mode=42", or else as a kernel
 * boot parameter "omap_udc:fifo_mode=42"
 */
module_param (fifo_mode, uint, 0);
97
MODULE_PARM_DESC (fifo_mode, "endpoint configuration");
L
Linus Torvalds 已提交
98 99

#ifdef	USE_DMA
100
static bool use_dma = 1;
L
Linus Torvalds 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119

/* "modprobe omap_udc use_dma=y", or else as a kernel
 * boot parameter "omap_udc:use_dma=y"
 */
module_param (use_dma, bool, 0);
MODULE_PARM_DESC (use_dma, "enable/disable DMA");
#else	/* !USE_DMA */

/* save a bit of code */
#define	use_dma		0
#endif	/* !USE_DMA */


static const char driver_name [] = "omap_udc";
static const char driver_desc [] = DRIVER_DESC;

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

/* there's a notion of "current endpoint" for modifying endpoint
120
 * state, and PIO access to its FIFO.
L
Linus Torvalds 已提交
121 122 123 124 125 126 127 128
 */

static void use_ep(struct omap_ep *ep, u16 select)
{
	u16	num = ep->bEndpointAddress & 0x0f;

	if (ep->bEndpointAddress & USB_DIR_IN)
		num |= UDC_EP_DIR;
129
	omap_writew(num | select, UDC_EP_NUM);
L
Linus Torvalds 已提交
130 131 132 133 134
	/* when select, MUST deselect later !! */
}

static inline void deselect_ep(void)
{
135 136 137 138 139
	u16 w;

	w = omap_readw(UDC_EP_NUM);
	w &= ~UDC_EP_SEL;
	omap_writew(w, UDC_EP_NUM);
L
Linus Torvalds 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
	/* 6 wait states before TX will happen */
}

static void dma_channel_claim(struct omap_ep *ep, unsigned preferred);

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

static int omap_ep_enable(struct usb_ep *_ep,
		const struct usb_endpoint_descriptor *desc)
{
	struct omap_ep	*ep = container_of(_ep, struct omap_ep, ep);
	struct omap_udc	*udc;
	unsigned long	flags;
	u16		maxp;

	/* catch various bogus parameters */
156
	if (!_ep || !desc || ep->ep.desc
L
Linus Torvalds 已提交
157 158
			|| desc->bDescriptorType != USB_DT_ENDPOINT
			|| ep->bEndpointAddress != desc->bEndpointAddress
159
			|| ep->maxpacket < usb_endpoint_maxp(desc)) {
160
		DBG("%s, bad ep or descriptor\n", __func__);
L
Linus Torvalds 已提交
161 162
		return -EINVAL;
	}
163
	maxp = usb_endpoint_maxp(desc);
L
Linus Torvalds 已提交
164 165
	if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
				&& maxp != ep->maxpacket)
166
			|| usb_endpoint_maxp(desc) > ep->maxpacket
L
Linus Torvalds 已提交
167
			|| !desc->wMaxPacketSize) {
168
		DBG("%s, bad %s maxpacket\n", __func__, _ep->name);
L
Linus Torvalds 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
		return -ERANGE;
	}

#ifdef	USE_ISO
	if ((desc->bmAttributes == USB_ENDPOINT_XFER_ISOC
				&& desc->bInterval != 1)) {
		/* hardware wants period = 1; USB allows 2^(Interval-1) */
		DBG("%s, unsupported ISO period %dms\n", _ep->name,
				1 << (desc->bInterval - 1));
		return -EDOM;
	}
#else
	if (desc->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
		DBG("%s, ISO nyet\n", _ep->name);
		return -EDOM;
	}
#endif

	/* 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) {
191
		DBG("%s, %s type mismatch\n", __func__, _ep->name);
L
Linus Torvalds 已提交
192 193 194 195 196
		return -EINVAL;
	}

	udc = ep->udc;
	if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN) {
197
		DBG("%s, bogus device state\n", __func__);
L
Linus Torvalds 已提交
198 199 200 201 202
		return -ESHUTDOWN;
	}

	spin_lock_irqsave(&udc->lock, flags);

203
	ep->ep.desc = desc;
L
Linus Torvalds 已提交
204 205 206 207 208 209 210 211 212
	ep->irqs = 0;
	ep->stopped = 0;
	ep->ep.maxpacket = maxp;

	/* set endpoint to initial state */
	ep->dma_channel = 0;
	ep->has_dma = 0;
	ep->lch = -1;
	use_ep(ep, UDC_EP_SEL);
213
	omap_writew(udc->clr_halt, UDC_CTRL);
L
Linus Torvalds 已提交
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
	ep->ackwait = 0;
	deselect_ep();

	if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC)
		list_add(&ep->iso, &udc->iso);

	/* maybe assign a DMA channel to this endpoint */
	if (use_dma && desc->bmAttributes == USB_ENDPOINT_XFER_BULK)
		/* FIXME ISO can dma, but prefers first channel */
		dma_channel_claim(ep, 0);

	/* PIO OUT may RX packets */
	if (desc->bmAttributes != USB_ENDPOINT_XFER_ISOC
			&& !ep->has_dma
			&& !(ep->bEndpointAddress & USB_DIR_IN)) {
229
		omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
L
Linus Torvalds 已提交
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
		ep->ackwait = 1 + ep->double_buf;
	}

	spin_unlock_irqrestore(&udc->lock, flags);
	VDBG("%s enabled\n", _ep->name);
	return 0;
}

static void nuke(struct omap_ep *, int status);

static int omap_ep_disable(struct usb_ep *_ep)
{
	struct omap_ep	*ep = container_of(_ep, struct omap_ep, ep);
	unsigned long	flags;

245
	if (!_ep || !ep->ep.desc) {
246
		DBG("%s, %s not enabled\n", __func__,
L
Linus Torvalds 已提交
247 248 249 250 251
			_ep ? ep->ep.name : NULL);
		return -EINVAL;
	}

	spin_lock_irqsave(&ep->udc->lock, flags);
252
	ep->ep.desc = NULL;
L
Linus Torvalds 已提交
253 254 255
	nuke (ep, -ESHUTDOWN);
	ep->ep.maxpacket = ep->maxpacket;
	ep->has_dma = 0;
256
	omap_writew(UDC_SET_HALT, UDC_CTRL);
L
Linus Torvalds 已提交
257 258 259 260 261 262 263 264 265 266 267 268
	list_del_init(&ep->iso);
	del_timer(&ep->timer);

	spin_unlock_irqrestore(&ep->udc->lock, flags);

	VDBG("%s disabled\n", _ep->name);
	return 0;
}

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

static struct usb_request *
A
Al Viro 已提交
269
omap_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
L
Linus Torvalds 已提交
270 271 272
{
	struct omap_req	*req;

273
	req = kzalloc(sizeof(*req), gfp_flags);
L
Linus Torvalds 已提交
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 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
	if (req) {
		req->req.dma = DMA_ADDR_INVALID;
		INIT_LIST_HEAD (&req->queue);
	}
	return &req->req;
}

static void
omap_free_request(struct usb_ep *ep, struct usb_request *_req)
{
	struct omap_req	*req = container_of(_req, struct omap_req, req);

	if (_req)
		kfree (req);
}

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

static void
done(struct omap_ep *ep, struct omap_req *req, int status)
{
	unsigned		stopped = ep->stopped;

	list_del_init(&req->queue);

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

	if (use_dma && ep->has_dma) {
		if (req->mapped) {
			dma_unmap_single(ep->udc->gadget.dev.parent,
				req->req.dma, req->req.length,
				(ep->bEndpointAddress & USB_DIR_IN)
					? DMA_TO_DEVICE
					: DMA_FROM_DEVICE);
			req->req.dma = DMA_ADDR_INVALID;
			req->mapped = 0;
		} else
			dma_sync_single_for_cpu(ep->udc->gadget.dev.parent,
				req->req.dma, req->req.length,
				(ep->bEndpointAddress & USB_DIR_IN)
					? DMA_TO_DEVICE
					: DMA_FROM_DEVICE);
	}

#ifndef	USB_TRACE
	if (status && status != -ESHUTDOWN)
#endif
		VDBG("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;
	spin_unlock(&ep->udc->lock);
	req->req.complete(&ep->ep, &req->req);
	spin_lock(&ep->udc->lock);
	ep->stopped = stopped;
}

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

338 339
#define UDC_FIFO_FULL		(UDC_NON_ISO_FIFO_FULL | UDC_ISO_FIFO_FULL)
#define UDC_FIFO_UNWRITABLE	(UDC_EP_HALTED | UDC_FIFO_FULL)
L
Linus Torvalds 已提交
340 341 342 343

#define FIFO_EMPTY	(UDC_NON_ISO_FIFO_EMPTY | UDC_ISO_FIFO_EMPTY)
#define FIFO_UNREADABLE (UDC_EP_HALTED | FIFO_EMPTY)

344
static inline int
L
Linus Torvalds 已提交
345 346 347 348 349 350 351 352 353 354 355 356
write_packet(u8 *buf, struct omap_req *req, unsigned max)
{
	unsigned	len;
	u16		*wp;

	len = min(req->req.length - req->req.actual, max);
	req->req.actual += len;

	max = len;
	if (likely((((int)buf) & 1) == 0)) {
		wp = (u16 *)buf;
		while (max >= 2) {
357
			omap_writew(*wp++, UDC_DATA);
L
Linus Torvalds 已提交
358 359 360 361 362
			max -= 2;
		}
		buf = (u8 *)wp;
	}
	while (max--)
363
		omap_writeb(*buf++, UDC_DATA);
L
Linus Torvalds 已提交
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
	return len;
}

// FIXME change r/w fifo calling convention


// return:  0 = still running, 1 = completed, negative = errno
static int write_fifo(struct omap_ep *ep, struct omap_req *req)
{
	u8		*buf;
	unsigned	count;
	int		is_last;
	u16		ep_stat;

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

	/* PIO-IN isn't double buffered except for iso */
382
	ep_stat = omap_readw(UDC_STAT_FLG);
383
	if (ep_stat & UDC_FIFO_UNWRITABLE)
L
Linus Torvalds 已提交
384 385 386 387
		return 0;

	count = ep->ep.maxpacket;
	count = write_packet(buf, req, count);
388
	omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
L
Linus Torvalds 已提交
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
	ep->ackwait = 1;

	/* last packet is often short (sometimes a zlp) */
	if (count != ep->ep.maxpacket)
		is_last = 1;
	else if (req->req.length == req->req.actual
			&& !req->req.zero)
		is_last = 1;
	else
		is_last = 0;

	/* NOTE:  requests complete when all IN data is in a
	 * FIFO (or sometimes later, if a zlp was needed).
	 * Use usb_ep_fifo_status() where needed.
	 */
	if (is_last)
		done(ep, req, 0);
	return is_last;
}

409
static inline int
L
Linus Torvalds 已提交
410 411 412 413 414 415 416 417 418 419 420 421
read_packet(u8 *buf, struct omap_req *req, unsigned avail)
{
	unsigned	len;
	u16		*wp;

	len = min(req->req.length - req->req.actual, avail);
	req->req.actual += len;
	avail = len;

	if (likely((((int)buf) & 1) == 0)) {
		wp = (u16 *)buf;
		while (avail >= 2) {
422
			*wp++ = omap_readw(UDC_DATA);
L
Linus Torvalds 已提交
423 424 425 426 427
			avail -= 2;
		}
		buf = (u8 *)wp;
	}
	while (avail--)
428
		*buf++ = omap_readb(UDC_DATA);
L
Linus Torvalds 已提交
429 430 431 432 433 434 435 436 437 438 439 440 441 442
	return len;
}

// return:  0 = still running, 1 = queue empty, negative = errno
static int read_fifo(struct omap_ep *ep, struct omap_req *req)
{
	u8		*buf;
	unsigned	count, avail;
	int		is_last;

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

	for (;;) {
443
		u16	ep_stat = omap_readw(UDC_STAT_FLG);
L
Linus Torvalds 已提交
444 445 446 447 448 449 450 451 452 453

		is_last = 0;
		if (ep_stat & FIFO_EMPTY) {
			if (!ep->double_buf)
				break;
			ep->fnf = 1;
		}
		if (ep_stat & UDC_EP_HALTED)
			break;

454
		if (ep_stat & UDC_FIFO_FULL)
L
Linus Torvalds 已提交
455 456
			avail = ep->ep.maxpacket;
		else  {
457
			avail = omap_readw(UDC_RXFSTAT);
L
Linus Torvalds 已提交
458 459 460 461 462 463 464 465 466 467 468 469
			ep->fnf = ep->double_buf;
		}
		count = read_packet(buf, req, avail);

		/* partial packet reads may not be errors */
		if (count < ep->ep.maxpacket) {
			is_last = 1;
			/* overflowed this request?  flush extra data */
			if (count != avail) {
				req->req.status = -EOVERFLOW;
				avail -= count;
				while (avail--)
470
					omap_readw(UDC_DATA);
L
Linus Torvalds 已提交
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
			}
		} else if (req->req.length == req->req.actual)
			is_last = 1;
		else
			is_last = 0;

		if (!ep->bEndpointAddress)
			break;
		if (is_last)
			done(ep, req, 0);
		break;
	}
	return is_last;
}

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

static u16 dma_src_len(struct omap_ep *ep, dma_addr_t start)
{
	dma_addr_t	end;

	/* IN-DMA needs this on fault/cancel paths, so 15xx misreports
	 * the last transfer's bytecount by more than a FIFO's worth.
	 */
	if (cpu_is_omap15xx())
		return 0;

498
	end = omap_get_dma_src_pos(ep->lch);
L
Linus Torvalds 已提交
499 500 501 502 503 504 505 506 507 508 509 510 511
	if (end == ep->dma_counter)
		return 0;

	end |= start & (0xffff << 16);
	if (end < start)
		end += 0x10000;
	return end - start;
}

static u16 dma_dest_len(struct omap_ep *ep, dma_addr_t start)
{
	dma_addr_t	end;

512
	end = omap_get_dma_dst_pos(ep->lch);
L
Linus Torvalds 已提交
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
	if (end == ep->dma_counter)
		return 0;

	end |= start & (0xffff << 16);
	if (cpu_is_omap15xx())
		end++;
	if (end < start)
		end += 0x10000;
	return end - start;
}


/* Each USB transfer request using DMA maps to one or more DMA transfers.
 * When DMA completion isn't request completion, the UDC continues with
 * the next DMA transfer for that USB transfer.
 */

static void next_in_dma(struct omap_ep *ep, struct omap_req *req)
{
532
	u16		txdma_ctrl, w;
L
Linus Torvalds 已提交
533 534 535 536
	unsigned	length = req->req.length - req->req.actual;
	const int	sync_mode = cpu_is_omap15xx()
				? OMAP_DMA_SYNC_FRAME
				: OMAP_DMA_SYNC_ELEMENT;
K
Kyungmin Park 已提交
537 538 539 540
	int		dma_trigger = 0;

	if (cpu_is_omap24xx())
		dma_trigger = OMAP24XX_DMA(USB_W2FC_TX0, ep->dma_channel);
L
Linus Torvalds 已提交
541 542

	/* measure length in either bytes or packets */
543
	if ((cpu_is_omap16xx() && length <= UDC_TXN_TSC)
K
Kyungmin Park 已提交
544
			|| (cpu_is_omap24xx() && length < ep->maxpacket)
L
Linus Torvalds 已提交
545 546 547
			|| (cpu_is_omap15xx() && length < ep->maxpacket)) {
		txdma_ctrl = UDC_TXN_EOT | length;
		omap_set_dma_transfer_params(ep->lch, OMAP_DMA_DATA_TYPE_S8,
K
Kyungmin Park 已提交
548
				length, 1, sync_mode, dma_trigger, 0);
L
Linus Torvalds 已提交
549 550 551
	} else {
		length = min(length / ep->maxpacket,
				(unsigned) UDC_TXN_TSC + 1);
552
		txdma_ctrl = length;
553
		omap_set_dma_transfer_params(ep->lch, OMAP_DMA_DATA_TYPE_S16,
554
				ep->ep.maxpacket >> 1, length, sync_mode,
K
Kyungmin Park 已提交
555
				dma_trigger, 0);
L
Linus Torvalds 已提交
556 557 558
		length *= ep->maxpacket;
	}
	omap_set_dma_src_params(ep->lch, OMAP_DMA_PORT_EMIFF,
559 560
		OMAP_DMA_AMODE_POST_INC, req->req.dma + req->req.actual,
		0, 0);
L
Linus Torvalds 已提交
561 562

	omap_start_dma(ep->lch);
563
	ep->dma_counter = omap_get_dma_src_pos(ep->lch);
564 565 566 567
	w = omap_readw(UDC_DMA_IRQ_EN);
	w |= UDC_TX_DONE_IE(ep->dma_channel);
	omap_writew(w, UDC_DMA_IRQ_EN);
	omap_writew(UDC_TXN_START | txdma_ctrl, UDC_TXDMA(ep->dma_channel));
L
Linus Torvalds 已提交
568 569 570 571 572
	req->dma_bytes = length;
}

static void finish_in_dma(struct omap_ep *ep, struct omap_req *req, int status)
{
573 574
	u16 w;

L
Linus Torvalds 已提交
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
	if (status == 0) {
		req->req.actual += req->dma_bytes;

		/* return if this request needs to send data or zlp */
		if (req->req.actual < req->req.length)
			return;
		if (req->req.zero
				&& req->dma_bytes != 0
				&& (req->req.actual % ep->maxpacket) == 0)
			return;
	} else
		req->req.actual += dma_src_len(ep, req->req.dma
							+ req->req.actual);

	/* tx completion */
	omap_stop_dma(ep->lch);
591 592 593
	w = omap_readw(UDC_DMA_IRQ_EN);
	w &= ~UDC_TX_DONE_IE(ep->dma_channel);
	omap_writew(w, UDC_DMA_IRQ_EN);
L
Linus Torvalds 已提交
594 595 596 597 598
	done(ep, req, status);
}

static void next_out_dma(struct omap_ep *ep, struct omap_req *req)
{
K
Kyungmin Park 已提交
599 600
	unsigned packets = req->req.length - req->req.actual;
	int dma_trigger = 0;
601
	u16 w;
K
Kyungmin Park 已提交
602 603 604

	if (cpu_is_omap24xx())
		dma_trigger = OMAP24XX_DMA(USB_W2FC_RX0, ep->dma_channel);
L
Linus Torvalds 已提交
605 606 607

	/* NOTE:  we filtered out "short reads" before, so we know
	 * the buffer has only whole numbers of packets.
K
Kyungmin Park 已提交
608
	 * except MODE SELECT(6) sent the 24 bytes data in OMAP24XX DMA mode
L
Linus Torvalds 已提交
609
	 */
K
Kyungmin Park 已提交
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
	if (cpu_is_omap24xx() && packets < ep->maxpacket) {
		omap_set_dma_transfer_params(ep->lch, OMAP_DMA_DATA_TYPE_S8,
				packets, 1, OMAP_DMA_SYNC_ELEMENT,
				dma_trigger, 0);
		req->dma_bytes = packets;
	} else {
		/* set up this DMA transfer, enable the fifo, start */
		packets /= ep->ep.maxpacket;
		packets = min(packets, (unsigned)UDC_RXN_TC + 1);
		req->dma_bytes = packets * ep->ep.maxpacket;
		omap_set_dma_transfer_params(ep->lch, OMAP_DMA_DATA_TYPE_S16,
				ep->ep.maxpacket >> 1, packets,
				OMAP_DMA_SYNC_ELEMENT,
				dma_trigger, 0);
	}
L
Linus Torvalds 已提交
625
	omap_set_dma_dest_params(ep->lch, OMAP_DMA_PORT_EMIFF,
626 627
		OMAP_DMA_AMODE_POST_INC, req->req.dma + req->req.actual,
		0, 0);
628
	ep->dma_counter = omap_get_dma_dst_pos(ep->lch);
L
Linus Torvalds 已提交
629

630 631 632 633 634 635
	omap_writew(UDC_RXN_STOP | (packets - 1), UDC_RXDMA(ep->dma_channel));
	w = omap_readw(UDC_DMA_IRQ_EN);
	w |= UDC_RX_EOT_IE(ep->dma_channel);
	omap_writew(w, UDC_DMA_IRQ_EN);
	omap_writew(ep->bEndpointAddress & 0xf, UDC_EP_NUM);
	omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
L
Linus Torvalds 已提交
636 637 638 639 640

	omap_start_dma(ep->lch);
}

static void
641
finish_out_dma(struct omap_ep *ep, struct omap_req *req, int status, int one)
L
Linus Torvalds 已提交
642
{
643
	u16	count, w;
L
Linus Torvalds 已提交
644 645 646 647 648

	if (status == 0)
		ep->dma_counter = (u16) (req->req.dma + req->req.actual);
	count = dma_dest_len(ep, req->req.dma + req->req.actual);
	count += req->req.actual;
649 650
	if (one)
		count--;
L
Linus Torvalds 已提交
651 652 653 654 655 656 657 658 659 660 661
	if (count <= req->req.length)
		req->req.actual = count;

	if (count != req->dma_bytes || status)
		omap_stop_dma(ep->lch);

	/* if this wasn't short, request may need another transfer */
	else if (req->req.actual < req->req.length)
		return;

	/* rx completion */
662 663 664
	w = omap_readw(UDC_DMA_IRQ_EN);
	w &= ~UDC_RX_EOT_IE(ep->dma_channel);
	omap_writew(w, UDC_DMA_IRQ_EN);
L
Linus Torvalds 已提交
665 666 667 668 669
	done(ep, req, status);
}

static void dma_irq(struct omap_udc *udc, u16 irq_src)
{
670
	u16		dman_stat = omap_readw(UDC_DMAN_STAT);
L
Linus Torvalds 已提交
671 672 673 674 675 676 677 678 679 680 681 682 683
	struct omap_ep	*ep;
	struct omap_req	*req;

	/* IN dma: tx to host */
	if (irq_src & UDC_TXN_DONE) {
		ep = &udc->ep[16 + UDC_DMA_TX_SRC(dman_stat)];
		ep->irqs++;
		/* can see TXN_DONE after dma abort */
		if (!list_empty(&ep->queue)) {
			req = container_of(ep->queue.next,
						struct omap_req, queue);
			finish_in_dma(ep, req, 0);
		}
684
		omap_writew(UDC_TXN_DONE, UDC_IRQ_SRC);
L
Linus Torvalds 已提交
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700

		if (!list_empty (&ep->queue)) {
			req = container_of(ep->queue.next,
					struct omap_req, queue);
			next_in_dma(ep, req);
		}
	}

	/* OUT dma: rx from host */
	if (irq_src & UDC_RXN_EOT) {
		ep = &udc->ep[UDC_DMA_RX_SRC(dman_stat)];
		ep->irqs++;
		/* can see RXN_EOT after dma abort */
		if (!list_empty(&ep->queue)) {
			req = container_of(ep->queue.next,
					struct omap_req, queue);
701
			finish_out_dma(ep, req, 0, dman_stat & UDC_DMA_RX_SB);
L
Linus Torvalds 已提交
702
		}
703
		omap_writew(UDC_RXN_EOT, UDC_IRQ_SRC);
L
Linus Torvalds 已提交
704 705 706 707 708 709 710 711 712 713 714 715 716

		if (!list_empty (&ep->queue)) {
			req = container_of(ep->queue.next,
					struct omap_req, queue);
			next_out_dma(ep, req);
		}
	}

	if (irq_src & UDC_RXN_CNT) {
		ep = &udc->ep[UDC_DMA_RX_SRC(dman_stat)];
		ep->irqs++;
		/* omap15xx does this unasked... */
		VDBG("%s, RX_CNT irq?\n", ep->ep.name);
717
		omap_writew(UDC_RXN_CNT, UDC_IRQ_SRC);
L
Linus Torvalds 已提交
718 719 720 721 722 723 724 725
	}
}

static void dma_error(int lch, u16 ch_status, void *data)
{
	struct omap_ep	*ep = data;

	/* if ch_status & OMAP_DMA_DROP_IRQ ... */
726
	/* if ch_status & OMAP1_DMA_TOUT_IRQ ... */
L
Linus Torvalds 已提交
727 728 729 730 731 732 733 734 735
	ERR("%s dma error, lch %d status %02x\n", ep->ep.name, lch, ch_status);

	/* complete current transfer ... */
}

static void dma_channel_claim(struct omap_ep *ep, unsigned channel)
{
	u16	reg;
	int	status, restart, is_in;
K
Kyungmin Park 已提交
736
	int	dma_channel;
L
Linus Torvalds 已提交
737 738 739

	is_in = ep->bEndpointAddress & USB_DIR_IN;
	if (is_in)
740
		reg = omap_readw(UDC_TXDMA_CFG);
L
Linus Torvalds 已提交
741
	else
742
		reg = omap_readw(UDC_RXDMA_CFG);
743
	reg |= UDC_DMA_REQ;		/* "pulse" activated */
L
Linus Torvalds 已提交
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762

	ep->dma_channel = 0;
	ep->lch = -1;
	if (channel == 0 || channel > 3) {
		if ((reg & 0x0f00) == 0)
			channel = 3;
		else if ((reg & 0x00f0) == 0)
			channel = 2;
		else if ((reg & 0x000f) == 0)	/* preferred for ISO */
			channel = 1;
		else {
			status = -EMLINK;
			goto just_restart;
		}
	}
	reg |= (0x0f & ep->bEndpointAddress) << (4 * (channel - 1));
	ep->dma_channel = channel;

	if (is_in) {
K
Kyungmin Park 已提交
763 764 765 766 767
		if (cpu_is_omap24xx())
			dma_channel = OMAP24XX_DMA(USB_W2FC_TX0, channel);
		else
			dma_channel = OMAP_DMA_USB_W2FC_TX0 - 1 + channel;
		status = omap_request_dma(dma_channel,
L
Linus Torvalds 已提交
768 769
			ep->ep.name, dma_error, ep, &ep->lch);
		if (status == 0) {
770
			omap_writew(reg, UDC_TXDMA_CFG);
K
Kyungmin Park 已提交
771
			/* EMIFF or SDRC */
772 773 774 775
			omap_set_dma_src_burst_mode(ep->lch,
						OMAP_DMA_DATA_BURST_4);
			omap_set_dma_src_data_pack(ep->lch, 1);
			/* TIPB */
L
Linus Torvalds 已提交
776 777 778
			omap_set_dma_dest_params(ep->lch,
				OMAP_DMA_PORT_TIPB,
				OMAP_DMA_AMODE_CONSTANT,
779
				UDC_DATA_DMA,
780
				0, 0);
L
Linus Torvalds 已提交
781 782
		}
	} else {
K
Kyungmin Park 已提交
783 784 785 786 787 788
		if (cpu_is_omap24xx())
			dma_channel = OMAP24XX_DMA(USB_W2FC_RX0, channel);
		else
			dma_channel = OMAP_DMA_USB_W2FC_RX0 - 1 + channel;

		status = omap_request_dma(dma_channel,
L
Linus Torvalds 已提交
789 790
			ep->ep.name, dma_error, ep, &ep->lch);
		if (status == 0) {
791
			omap_writew(reg, UDC_RXDMA_CFG);
792
			/* TIPB */
L
Linus Torvalds 已提交
793 794 795
			omap_set_dma_src_params(ep->lch,
				OMAP_DMA_PORT_TIPB,
				OMAP_DMA_AMODE_CONSTANT,
796
				UDC_DATA_DMA,
797
				0, 0);
K
Kyungmin Park 已提交
798
			/* EMIFF or SDRC */
799 800 801
			omap_set_dma_dest_burst_mode(ep->lch,
						OMAP_DMA_DATA_BURST_4);
			omap_set_dma_dest_data_pack(ep->lch, 1);
L
Linus Torvalds 已提交
802 803 804 805 806 807 808 809 810
		}
	}
	if (status)
		ep->dma_channel = 0;
	else {
		ep->has_dma = 1;
		omap_disable_dma_irq(ep->lch, OMAP_DMA_BLOCK_IRQ);

		/* channel type P: hw synch (fifo) */
K
Kyungmin Park 已提交
811
		if (cpu_class_is_omap1() && !cpu_is_omap15xx())
812
			omap_set_dma_channel_mode(ep->lch, OMAP_DMA_LCH_P);
L
Linus Torvalds 已提交
813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
	}

just_restart:
	/* restart any queue, even if the claim failed  */
	restart = !ep->stopped && !list_empty(&ep->queue);

	if (status)
		DBG("%s no dma channel: %d%s\n", ep->ep.name, status,
			restart ? " (restart)" : "");
	else
		DBG("%s claimed %cxdma%d lch %d%s\n", ep->ep.name,
			is_in ? 't' : 'r',
			ep->dma_channel - 1, ep->lch,
			restart ? " (restart)" : "");

	if (restart) {
		struct omap_req	*req;
		req = container_of(ep->queue.next, struct omap_req, queue);
		if (ep->has_dma)
			(is_in ? next_in_dma : next_out_dma)(ep, req);
		else {
			use_ep(ep, UDC_EP_SEL);
			(is_in ? write_fifo : read_fifo)(ep, req);
			deselect_ep();
			if (!is_in) {
838
				omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
L
Linus Torvalds 已提交
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
				ep->ackwait = 1 + ep->double_buf;
			}
			/* IN: 6 wait states before it'll tx */
		}
	}
}

static void dma_channel_release(struct omap_ep *ep)
{
	int		shift = 4 * (ep->dma_channel - 1);
	u16		mask = 0x0f << shift;
	struct omap_req	*req;
	int		active;

	/* abort any active usb transfer request */
	if (!list_empty(&ep->queue))
		req = container_of(ep->queue.next, struct omap_req, queue);
	else
857
		req = NULL;
L
Linus Torvalds 已提交
858

859
	active = omap_get_dma_active_status(ep->lch);
L
Linus Torvalds 已提交
860 861 862 863 864 865

	DBG("%s release %s %cxdma%d %p\n", ep->ep.name,
			active ? "active" : "idle",
			(ep->bEndpointAddress & USB_DIR_IN) ? 't' : 'r',
			ep->dma_channel - 1, req);

866 867 868 869
	/* NOTE: re-setting RX_REQ/TX_REQ because of a chip bug (before
	 * OMAP 1710 ES2.0) where reading the DMA_CFG can clear them.
	 */

L
Linus Torvalds 已提交
870 871
	/* wait till current packet DMA finishes, and fifo empties */
	if (ep->bEndpointAddress & USB_DIR_IN) {
872 873
		omap_writew((omap_readw(UDC_TXDMA_CFG) & ~mask) | UDC_DMA_REQ,
					UDC_TXDMA_CFG);
L
Linus Torvalds 已提交
874 875 876 877 878 879

		if (req) {
			finish_in_dma(ep, req, -ECONNRESET);

			/* clear FIFO; hosts probably won't empty it */
			use_ep(ep, UDC_EP_SEL);
880
			omap_writew(UDC_CLR_EP, UDC_CTRL);
L
Linus Torvalds 已提交
881 882
			deselect_ep();
		}
883
		while (omap_readw(UDC_TXDMA_CFG) & mask)
L
Linus Torvalds 已提交
884 885
			udelay(10);
	} else {
886 887
		omap_writew((omap_readw(UDC_RXDMA_CFG) & ~mask) | UDC_DMA_REQ,
					UDC_RXDMA_CFG);
L
Linus Torvalds 已提交
888 889

		/* dma empties the fifo */
890
		while (omap_readw(UDC_RXDMA_CFG) & mask)
L
Linus Torvalds 已提交
891 892
			udelay(10);
		if (req)
893
			finish_out_dma(ep, req, -ECONNRESET, 0);
L
Linus Torvalds 已提交
894 895 896 897 898 899 900 901 902 903 904
	}
	omap_free_dma(ep->lch);
	ep->dma_channel = 0;
	ep->lch = -1;
	/* has_dma still set, till endpoint is fully quiesced */
}


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

static int
A
Al Viro 已提交
905
omap_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
L
Linus Torvalds 已提交
906 907 908 909 910 911 912 913 914 915
{
	struct omap_ep	*ep = container_of(_ep, struct omap_ep, ep);
	struct omap_req	*req = container_of(_req, struct omap_req, req);
	struct omap_udc	*udc;
	unsigned long	flags;
	int		is_iso = 0;

	/* catch various bogus parameters */
	if (!_req || !req->req.complete || !req->req.buf
			|| !list_empty(&req->queue)) {
916
		DBG("%s, bad params\n", __func__);
L
Linus Torvalds 已提交
917 918
		return -EINVAL;
	}
919
	if (!_ep || (!ep->ep.desc && ep->bEndpointAddress)) {
920
		DBG("%s, bad ep\n", __func__);
L
Linus Torvalds 已提交
921 922 923 924 925 926 927 928 929 930
		return -EINVAL;
	}
	if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
		if (req->req.length > ep->ep.maxpacket)
			return -EMSGSIZE;
		is_iso = 1;
	}

	/* this isn't bogus, but OMAP DMA isn't the only hardware to
	 * have a hard time with partial packet reads...  reject it.
K
Kyungmin Park 已提交
931
	 * Except OMAP2 can handle the small packets.
L
Linus Torvalds 已提交
932 933 934 935 936
	 */
	if (use_dma
			&& ep->has_dma
			&& ep->bEndpointAddress != 0
			&& (ep->bEndpointAddress & USB_DIR_IN) == 0
K
Kyungmin Park 已提交
937
			&& !cpu_class_is_omap2()
L
Linus Torvalds 已提交
938
			&& (req->req.length % ep->ep.maxpacket) != 0) {
939
		DBG("%s, no partial packet OUT reads\n", __func__);
L
Linus Torvalds 已提交
940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976
		return -EMSGSIZE;
	}

	udc = ep->udc;
	if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN)
		return -ESHUTDOWN;

	if (use_dma && ep->has_dma) {
		if (req->req.dma == DMA_ADDR_INVALID) {
			req->req.dma = dma_map_single(
				ep->udc->gadget.dev.parent,
				req->req.buf,
				req->req.length,
				(ep->bEndpointAddress & USB_DIR_IN)
					? DMA_TO_DEVICE
					: DMA_FROM_DEVICE);
			req->mapped = 1;
		} else {
			dma_sync_single_for_device(
				ep->udc->gadget.dev.parent,
				req->req.dma, req->req.length,
				(ep->bEndpointAddress & USB_DIR_IN)
					? DMA_TO_DEVICE
					: DMA_FROM_DEVICE);
			req->mapped = 0;
		}
	}

	VDBG("%s queue req %p, len %d buf %p\n",
		ep->ep.name, _req, _req->length, _req->buf);

	spin_lock_irqsave(&udc->lock, flags);

	req->req.status = -EINPROGRESS;
	req->req.actual = 0;

	/* maybe kickstart non-iso i/o queues */
977 978 979 980 981 982 983
	if (is_iso) {
		u16 w;

		w = omap_readw(UDC_IRQ_EN);
		w |= UDC_SOF_IE;
		omap_writew(w, UDC_IRQ_EN);
	} else if (list_empty(&ep->queue) && !ep->stopped && !ep->ackwait) {
L
Linus Torvalds 已提交
984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
		int	is_in;

		if (ep->bEndpointAddress == 0) {
			if (!udc->ep0_pending || !list_empty (&ep->queue)) {
				spin_unlock_irqrestore(&udc->lock, flags);
				return -EL2HLT;
			}

			/* empty DATA stage? */
			is_in = udc->ep0_in;
			if (!req->req.length) {

				/* chip became CONFIGURED or ADDRESSED
				 * earlier; drivers may already have queued
				 * requests to non-control endpoints
				 */
				if (udc->ep0_set_config) {
1001
					u16	irq_en = omap_readw(UDC_IRQ_EN);
L
Linus Torvalds 已提交
1002 1003 1004 1005 1006

					irq_en |= UDC_DS_CHG_IE | UDC_EP0_IE;
					if (!udc->ep0_reset_config)
						irq_en |= UDC_EPN_RX_IE
							| UDC_EPN_TX_IE;
1007
					omap_writew(irq_en, UDC_IRQ_EN);
L
Linus Torvalds 已提交
1008 1009
				}

1010 1011
				/* STATUS for zero length DATA stages is
				 * always an IN ... even for IN transfers,
J
Joe Perches 已提交
1012
				 * a weird case which seem to stall OMAP.
1013
				 */
1014 1015 1016 1017
				omap_writew(UDC_EP_SEL | UDC_EP_DIR, UDC_EP_NUM);
				omap_writew(UDC_CLR_EP, UDC_CTRL);
				omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
				omap_writew(UDC_EP_DIR, UDC_EP_NUM);
L
Linus Torvalds 已提交
1018 1019 1020 1021

				/* cleanup */
				udc->ep0_pending = 0;
				done(ep, req, 0);
1022
				req = NULL;
L
Linus Torvalds 已提交
1023 1024 1025

			/* non-empty DATA stage */
			} else if (is_in) {
1026
				omap_writew(UDC_EP_SEL | UDC_EP_DIR, UDC_EP_NUM);
L
Linus Torvalds 已提交
1027 1028 1029
			} else {
				if (udc->ep0_setup)
					goto irq_wait;
1030
				omap_writew(UDC_EP_SEL, UDC_EP_NUM);
L
Linus Torvalds 已提交
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
			}
		} else {
			is_in = ep->bEndpointAddress & USB_DIR_IN;
			if (!ep->has_dma)
				use_ep(ep, UDC_EP_SEL);
			/* if ISO: SOF IRQs must be enabled/disabled! */
		}

		if (ep->has_dma)
			(is_in ? next_in_dma : next_out_dma)(ep, req);
		else if (req) {
			if ((is_in ? write_fifo : read_fifo)(ep, req) == 1)
1043
				req = NULL;
L
Linus Torvalds 已提交
1044 1045
			deselect_ep();
			if (!is_in) {
1046
				omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
L
Linus Torvalds 已提交
1047 1048 1049 1050 1051 1052 1053 1054
				ep->ackwait = 1 + ep->double_buf;
			}
			/* IN: 6 wait states before it'll tx */
		}
	}

irq_wait:
	/* irq handler advances the queue */
1055
	if (req != NULL)
L
Linus Torvalds 已提交
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
		list_add_tail(&req->queue, &ep->queue);
	spin_unlock_irqrestore(&udc->lock, flags);

	return 0;
}

static int omap_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
{
	struct omap_ep	*ep = container_of(_ep, struct omap_ep, ep);
	struct omap_req	*req;
	unsigned long	flags;

	if (!_ep || !_req)
		return -EINVAL;

	spin_lock_irqsave(&ep->udc->lock, 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) {
		spin_unlock_irqrestore(&ep->udc->lock, flags);
		return -EINVAL;
	}

	if (use_dma && ep->dma_channel && ep->queue.next == &req->queue) {
		int channel = ep->dma_channel;

		/* releasing the channel cancels the request,
		 * reclaiming the channel restarts the queue
		 */
		dma_channel_release(ep);
		dma_channel_claim(ep, channel);
1091
	} else
L
Linus Torvalds 已提交
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
		done(ep, req, -ECONNRESET);
	spin_unlock_irqrestore(&ep->udc->lock, flags);
	return 0;
}

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

static int omap_ep_set_halt(struct usb_ep *_ep, int value)
{
	struct omap_ep	*ep = container_of(_ep, struct omap_ep, ep);
	unsigned long	flags;
	int		status = -EOPNOTSUPP;

	spin_lock_irqsave(&ep->udc->lock, flags);

	/* just use protocol stalls for ep0; real halts are annoying */
	if (ep->bEndpointAddress == 0) {
		if (!ep->udc->ep0_pending)
			status = -EINVAL;
		else if (value) {
			if (ep->udc->ep0_set_config) {
1113
				WARNING("error changing config?\n");
1114
				omap_writew(UDC_CLR_CFG, UDC_SYSCON2);
L
Linus Torvalds 已提交
1115
			}
1116
			omap_writew(UDC_STALL_CMD, UDC_SYSCON2);
L
Linus Torvalds 已提交
1117 1118 1119 1120 1121 1122
			ep->udc->ep0_pending = 0;
			status = 0;
		} else /* NOP */
			status = 0;

	/* otherwise, all active non-ISO endpoints can halt */
1123
	} else if (ep->bmAttributes != USB_ENDPOINT_XFER_ISOC && ep->ep.desc) {
L
Linus Torvalds 已提交
1124 1125 1126

		/* IN endpoints must already be idle */
		if ((ep->bEndpointAddress & USB_DIR_IN)
1127
				&& !list_empty(&ep->queue)) {
L
Linus Torvalds 已提交
1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142
			status = -EAGAIN;
			goto done;
		}

		if (value) {
			int	channel;

			if (use_dma && ep->dma_channel
					&& !list_empty(&ep->queue)) {
				channel = ep->dma_channel;
				dma_channel_release(ep);
			} else
				channel = 0;

			use_ep(ep, UDC_EP_SEL);
1143 1144
			if (omap_readw(UDC_STAT_FLG) & UDC_NON_ISO_FIFO_EMPTY) {
				omap_writew(UDC_SET_HALT, UDC_CTRL);
L
Linus Torvalds 已提交
1145 1146 1147 1148 1149 1150 1151 1152 1153
				status = 0;
			} else
				status = -EAGAIN;
			deselect_ep();

			if (channel)
				dma_channel_claim(ep, channel);
		} else {
			use_ep(ep, 0);
1154
			omap_writew(ep->udc->clr_halt, UDC_CTRL);
L
Linus Torvalds 已提交
1155 1156
			ep->ackwait = 0;
			if (!(ep->bEndpointAddress & USB_DIR_IN)) {
1157
				omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
L
Linus Torvalds 已提交
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
				ep->ackwait = 1 + ep->double_buf;
			}
		}
	}
done:
	VDBG("%s %s halt stat %d\n", ep->ep.name,
		value ? "set" : "clear", status);

	spin_unlock_irqrestore(&ep->udc->lock, flags);
	return status;
}

static struct usb_ep_ops omap_ep_ops = {
	.enable		= omap_ep_enable,
	.disable	= omap_ep_disable,

	.alloc_request	= omap_alloc_request,
	.free_request	= omap_free_request,

	.queue		= omap_ep_queue,
	.dequeue	= omap_ep_dequeue,

	.set_halt	= omap_ep_set_halt,
	// fifo_status ... report bytes in fifo
	// fifo_flush ... flush fifo
};

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

static int omap_get_frame(struct usb_gadget *gadget)
{
1189
	u16	sof = omap_readw(UDC_SOF);
L
Linus Torvalds 已提交
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
	return (sof & UDC_TS_OK) ? (sof & UDC_TS) : -EL2NSYNC;
}

static int omap_wakeup(struct usb_gadget *gadget)
{
	struct omap_udc	*udc;
	unsigned long	flags;
	int		retval = -EHOSTUNREACH;

	udc = container_of(gadget, struct omap_udc, gadget);

	spin_lock_irqsave(&udc->lock, flags);
	if (udc->devstat & UDC_SUS) {
		/* NOTE:  OTG spec erratum says that OTG devices may
		 * issue wakeups without host enable.
		 */
		if (udc->devstat & (UDC_B_HNP_ENABLE|UDC_R_WK_OK)) {
			DBG("remote wakeup...\n");
1208
			omap_writew(UDC_RMT_WKP, UDC_SYSCON2);
L
Linus Torvalds 已提交
1209 1210 1211 1212 1213 1214
			retval = 0;
		}

	/* NOTE:  non-OTG systems may use SRP TOO... */
	} else if (!(udc->devstat & UDC_ATT)) {
		if (udc->transceiver)
1215
			retval = otg_start_srp(udc->transceiver->otg);
L
Linus Torvalds 已提交
1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230
	}
	spin_unlock_irqrestore(&udc->lock, flags);

	return retval;
}

static int
omap_set_selfpowered(struct usb_gadget *gadget, int is_selfpowered)
{
	struct omap_udc	*udc;
	unsigned long	flags;
	u16		syscon1;

	udc = container_of(gadget, struct omap_udc, gadget);
	spin_lock_irqsave(&udc->lock, flags);
1231
	syscon1 = omap_readw(UDC_SYSCON1);
L
Linus Torvalds 已提交
1232 1233 1234 1235
	if (is_selfpowered)
		syscon1 |= UDC_SELF_PWR;
	else
		syscon1 &= ~UDC_SELF_PWR;
1236
	omap_writew(syscon1, UDC_SYSCON1);
L
Linus Torvalds 已提交
1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
	spin_unlock_irqrestore(&udc->lock, flags);

	return 0;
}

static int can_pullup(struct omap_udc *udc)
{
	return udc->driver && udc->softconnect && udc->vbus_active;
}

static void pullup_enable(struct omap_udc *udc)
{
1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261
	u16 w;

	w = omap_readw(UDC_SYSCON1);
	w |= UDC_PULLUP_EN;
	omap_writew(w, UDC_SYSCON1);
	if (!gadget_is_otg(&udc->gadget) && !cpu_is_omap15xx()) {
		u32 l;

		l = omap_readl(OTG_CTRL);
		l |= OTG_BSESSVLD;
		omap_writel(l, OTG_CTRL);
	}
	omap_writew(UDC_DS_CHG_IE, UDC_IRQ_EN);
L
Linus Torvalds 已提交
1262 1263 1264 1265
}

static void pullup_disable(struct omap_udc *udc)
{
1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
	u16 w;

	if (!gadget_is_otg(&udc->gadget) && !cpu_is_omap15xx()) {
		u32 l;

		l = omap_readl(OTG_CTRL);
		l &= ~OTG_BSESSVLD;
		omap_writel(l, OTG_CTRL);
	}
	omap_writew(UDC_DS_CHG_IE, UDC_IRQ_EN);
	w = omap_readw(UDC_SYSCON1);
	w &= ~UDC_PULLUP_EN;
	omap_writew(w, UDC_SYSCON1);
L
Linus Torvalds 已提交
1279 1280
}

1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297
static struct omap_udc *udc;

static void omap_udc_enable_clock(int enable)
{
	if (udc == NULL || udc->dc_clk == NULL || udc->hhc_clk == NULL)
		return;

	if (enable) {
		clk_enable(udc->dc_clk);
		clk_enable(udc->hhc_clk);
		udelay(100);
	} else {
		clk_disable(udc->hhc_clk);
		clk_disable(udc->dc_clk);
	}
}

L
Linus Torvalds 已提交
1298 1299 1300 1301 1302 1303 1304 1305
/*
 * Called by whatever detects VBUS sessions:  external transceiver
 * driver, or maybe GPIO0 VBUS IRQ.  May request 48 MHz clock.
 */
static int omap_vbus_session(struct usb_gadget *gadget, int is_active)
{
	struct omap_udc	*udc;
	unsigned long	flags;
1306
	u32 l;
L
Linus Torvalds 已提交
1307 1308 1309 1310 1311 1312 1313

	udc = container_of(gadget, struct omap_udc, gadget);
	spin_lock_irqsave(&udc->lock, flags);
	VDBG("VBUS %s\n", is_active ? "on" : "off");
	udc->vbus_active = (is_active != 0);
	if (cpu_is_omap15xx()) {
		/* "software" detect, ignored if !VBUS_MODE_1510 */
1314
		l = omap_readl(FUNC_MUX_CTRL_0);
L
Linus Torvalds 已提交
1315
		if (is_active)
1316
			l |= VBUS_CTRL_1510;
L
Linus Torvalds 已提交
1317
		else
1318 1319
			l &= ~VBUS_CTRL_1510;
		omap_writel(l, FUNC_MUX_CTRL_0);
L
Linus Torvalds 已提交
1320
	}
1321 1322 1323 1324 1325 1326
	if (udc->dc_clk != NULL && is_active) {
		if (!udc->clk_requested) {
			omap_udc_enable_clock(1);
			udc->clk_requested = 1;
		}
	}
L
Linus Torvalds 已提交
1327 1328 1329 1330
	if (can_pullup(udc))
		pullup_enable(udc);
	else
		pullup_disable(udc);
1331 1332 1333 1334 1335 1336
	if (udc->dc_clk != NULL && !is_active) {
		if (udc->clk_requested) {
			omap_udc_enable_clock(0);
			udc->clk_requested = 0;
		}
	}
L
Linus Torvalds 已提交
1337 1338 1339 1340 1341 1342 1343 1344 1345 1346
	spin_unlock_irqrestore(&udc->lock, flags);
	return 0;
}

static int omap_vbus_draw(struct usb_gadget *gadget, unsigned mA)
{
	struct omap_udc	*udc;

	udc = container_of(gadget, struct omap_udc, gadget);
	if (udc->transceiver)
1347
		return usb_phy_set_power(udc->transceiver, mA);
L
Linus Torvalds 已提交
1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366
	return -EOPNOTSUPP;
}

static int omap_pullup(struct usb_gadget *gadget, int is_on)
{
	struct omap_udc	*udc;
	unsigned long	flags;

	udc = container_of(gadget, struct omap_udc, gadget);
	spin_lock_irqsave(&udc->lock, flags);
	udc->softconnect = (is_on != 0);
	if (can_pullup(udc))
		pullup_enable(udc);
	else
		pullup_disable(udc);
	spin_unlock_irqrestore(&udc->lock, flags);
	return 0;
}

1367 1368 1369 1370
static int omap_udc_start(struct usb_gadget_driver *driver,
		int (*bind)(struct usb_gadget *));
static int omap_udc_stop(struct usb_gadget_driver *driver);

L
Linus Torvalds 已提交
1371 1372 1373 1374 1375 1376 1377
static struct usb_gadget_ops omap_gadget_ops = {
	.get_frame		= omap_get_frame,
	.wakeup			= omap_wakeup,
	.set_selfpowered	= omap_set_selfpowered,
	.vbus_session		= omap_vbus_session,
	.vbus_draw		= omap_vbus_draw,
	.pullup			= omap_pullup,
1378 1379
	.start			= omap_udc_start,
	.stop			= omap_udc_stop,
L
Linus Torvalds 已提交
1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394
};

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

/* dequeue ALL requests; caller holds udc->lock */
static void nuke(struct omap_ep *ep, int status)
{
	struct omap_req	*req;

	ep->stopped = 1;

	if (use_dma && ep->dma_channel)
		dma_channel_release(ep);

	use_ep(ep, 0);
1395
	omap_writew(UDC_CLR_EP, UDC_CTRL);
L
Linus Torvalds 已提交
1396
	if (ep->bEndpointAddress && ep->bmAttributes != USB_ENDPOINT_XFER_ISOC)
1397
		omap_writew(UDC_SET_HALT, UDC_CTRL);
L
Linus Torvalds 已提交
1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421

	while (!list_empty(&ep->queue)) {
		req = list_entry(ep->queue.next, struct omap_req, queue);
		done(ep, req, status);
	}
}

/* caller holds udc->lock */
static void udc_quiesce(struct omap_udc *udc)
{
	struct omap_ep	*ep;

	udc->gadget.speed = USB_SPEED_UNKNOWN;
	nuke(&udc->ep[0], -ESHUTDOWN);
	list_for_each_entry (ep, &udc->gadget.ep_list, ep.ep_list)
		nuke(ep, -ESHUTDOWN);
}

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

static void update_otg(struct omap_udc *udc)
{
	u16	devstat;

D
David Brownell 已提交
1422
	if (!gadget_is_otg(&udc->gadget))
L
Linus Torvalds 已提交
1423 1424
		return;

1425 1426
	if (omap_readl(OTG_CTRL) & OTG_ID)
		devstat = omap_readw(UDC_DEVSTAT);
L
Linus Torvalds 已提交
1427 1428 1429 1430 1431 1432 1433 1434 1435 1436
	else
		devstat = 0;

	udc->gadget.b_hnp_enable = !!(devstat & UDC_B_HNP_ENABLE);
	udc->gadget.a_hnp_support = !!(devstat & UDC_A_HNP_SUPPORT);
	udc->gadget.a_alt_hnp_support = !!(devstat & UDC_A_ALT_HNP_SUPPORT);

	/* Enable HNP early, avoiding races on suspend irq path.
	 * ASSUMES OTG state machine B_BUS_REQ input is true.
	 */
1437 1438 1439 1440 1441 1442 1443 1444
	if (udc->gadget.b_hnp_enable) {
		u32 l;

		l = omap_readl(OTG_CTRL);
		l |= OTG_B_HNPEN | OTG_B_BUSREQ;
		l &= ~OTG_PULLUP;
		omap_writel(l, OTG_CTRL);
	}
L
Linus Torvalds 已提交
1445 1446 1447 1448 1449
}

static void ep0_irq(struct omap_udc *udc, u16 irq_src)
{
	struct omap_ep	*ep0 = &udc->ep[0];
1450
	struct omap_req	*req = NULL;
L
Linus Torvalds 已提交
1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461

	ep0->irqs++;

	/* Clear any pending requests and then scrub any rx/tx state
	 * before starting to handle the SETUP request.
	 */
	if (irq_src & UDC_SETUP) {
		u16	ack = irq_src & (UDC_EP0_TX|UDC_EP0_RX);

		nuke(ep0, 0);
		if (ack) {
1462
			omap_writew(ack, UDC_IRQ_SRC);
L
Linus Torvalds 已提交
1463 1464 1465 1466
			irq_src = UDC_SETUP;
		}
	}

1467
	/* IN/OUT packets mean we're in the DATA or STATUS stage.
L
Linus Torvalds 已提交
1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481
	 * This driver uses only uses protocol stalls (ep0 never halts),
	 * and if we got this far the gadget driver already had a
	 * chance to stall.  Tries to be forgiving of host oddities.
	 *
	 * NOTE:  the last chance gadget drivers have to stall control
	 * requests is during their request completion callback.
	 */
	if (!list_empty(&ep0->queue))
		req = container_of(ep0->queue.next, struct omap_req, queue);

	/* IN == TX to host */
	if (irq_src & UDC_EP0_TX) {
		int	stat;

1482 1483 1484
		omap_writew(UDC_EP0_TX, UDC_IRQ_SRC);
		omap_writew(UDC_EP_SEL|UDC_EP_DIR, UDC_EP_NUM);
		stat = omap_readw(UDC_STAT_FLG);
L
Linus Torvalds 已提交
1485 1486 1487 1488 1489 1490 1491
		if (stat & UDC_ACK) {
			if (udc->ep0_in) {
				/* write next IN packet from response,
				 * or set up the status stage.
				 */
				if (req)
					stat = write_fifo(ep0, req);
1492
				omap_writew(UDC_EP_DIR, UDC_EP_NUM);
L
Linus Torvalds 已提交
1493
				if (!req && udc->ep0_pending) {
1494 1495 1496 1497
					omap_writew(UDC_EP_SEL, UDC_EP_NUM);
					omap_writew(UDC_CLR_EP, UDC_CTRL);
					omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
					omap_writew(0, UDC_EP_NUM);
L
Linus Torvalds 已提交
1498 1499 1500 1501
					udc->ep0_pending = 0;
				} /* else:  6 wait states before it'll tx */
			} else {
				/* ack status stage of OUT transfer */
1502
				omap_writew(UDC_EP_DIR, UDC_EP_NUM);
L
Linus Torvalds 已提交
1503 1504 1505
				if (req)
					done(ep0, req, 0);
			}
1506
			req = NULL;
L
Linus Torvalds 已提交
1507
		} else if (stat & UDC_STALL) {
1508 1509
			omap_writew(UDC_CLR_HALT, UDC_CTRL);
			omap_writew(UDC_EP_DIR, UDC_EP_NUM);
L
Linus Torvalds 已提交
1510
		} else {
1511
			omap_writew(UDC_EP_DIR, UDC_EP_NUM);
L
Linus Torvalds 已提交
1512 1513 1514 1515 1516 1517 1518
		}
	}

	/* OUT == RX from host */
	if (irq_src & UDC_EP0_RX) {
		int	stat;

1519 1520 1521
		omap_writew(UDC_EP0_RX, UDC_IRQ_SRC);
		omap_writew(UDC_EP_SEL, UDC_EP_NUM);
		stat = omap_readw(UDC_STAT_FLG);
L
Linus Torvalds 已提交
1522 1523 1524 1525 1526 1527 1528
		if (stat & UDC_ACK) {
			if (!udc->ep0_in) {
				stat = 0;
				/* read next OUT packet of request, maybe
				 * reactiviting the fifo; stall on errors.
				 */
				if (!req || (stat = read_fifo(ep0, req)) < 0) {
1529
					omap_writew(UDC_STALL_CMD, UDC_SYSCON2);
L
Linus Torvalds 已提交
1530 1531 1532
					udc->ep0_pending = 0;
					stat = 0;
				} else if (stat == 0)
1533 1534
					omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
				omap_writew(0, UDC_EP_NUM);
1535

L
Linus Torvalds 已提交
1536 1537 1538 1539
				/* activate status stage */
				if (stat == 1) {
					done(ep0, req, 0);
					/* that may have STALLed ep0... */
1540 1541 1542 1543 1544
					omap_writew(UDC_EP_SEL | UDC_EP_DIR,
							UDC_EP_NUM);
					omap_writew(UDC_CLR_EP, UDC_CTRL);
					omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
					omap_writew(UDC_EP_DIR, UDC_EP_NUM);
L
Linus Torvalds 已提交
1545 1546 1547 1548
					udc->ep0_pending = 0;
				}
			} else {
				/* ack status stage of IN transfer */
1549
				omap_writew(0, UDC_EP_NUM);
L
Linus Torvalds 已提交
1550 1551 1552 1553
				if (req)
					done(ep0, req, 0);
			}
		} else if (stat & UDC_STALL) {
1554 1555
			omap_writew(UDC_CLR_HALT, UDC_CTRL);
			omap_writew(0, UDC_EP_NUM);
L
Linus Torvalds 已提交
1556
		} else {
1557
			omap_writew(0, UDC_EP_NUM);
L
Linus Torvalds 已提交
1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571
		}
	}

	/* SETUP starts all control transfers */
	if (irq_src & UDC_SETUP) {
		union u {
			u16			word[4];
			struct usb_ctrlrequest	r;
		} u;
		int			status = -EINVAL;
		struct omap_ep		*ep;

		/* read the (latest) SETUP message */
		do {
1572
			omap_writew(UDC_SETUP_SEL, UDC_EP_NUM);
L
Linus Torvalds 已提交
1573
			/* two bytes at a time */
1574 1575 1576 1577 1578 1579
			u.word[0] = omap_readw(UDC_DATA);
			u.word[1] = omap_readw(UDC_DATA);
			u.word[2] = omap_readw(UDC_DATA);
			u.word[3] = omap_readw(UDC_DATA);
			omap_writew(0, UDC_EP_NUM);
		} while (omap_readw(UDC_IRQ_SRC) & UDC_SETUP);
L
Linus Torvalds 已提交
1580

1581 1582 1583
#define	w_value		le16_to_cpu(u.r.wValue)
#define	w_index		le16_to_cpu(u.r.wIndex)
#define	w_length	le16_to_cpu(u.r.wLength)
1584

L
Linus Torvalds 已提交
1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598
		/* Delegate almost all control requests to the gadget driver,
		 * except for a handful of ch9 status/feature requests that
		 * hardware doesn't autodecode _and_ the gadget API hides.
		 */
		udc->ep0_in = (u.r.bRequestType & USB_DIR_IN) != 0;
		udc->ep0_set_config = 0;
		udc->ep0_pending = 1;
		ep0->stopped = 0;
		ep0->ackwait = 0;
		switch (u.r.bRequest) {
		case USB_REQ_SET_CONFIGURATION:
			/* udc needs to know when ep != 0 is valid */
			if (u.r.bRequestType != USB_RECIP_DEVICE)
				goto delegate;
1599
			if (w_length != 0)
L
Linus Torvalds 已提交
1600 1601
				goto do_stall;
			udc->ep0_set_config = 1;
1602 1603
			udc->ep0_reset_config = (w_value == 0);
			VDBG("set config %d\n", w_value);
L
Linus Torvalds 已提交
1604 1605 1606 1607 1608 1609

			/* update udc NOW since gadget driver may start
			 * queueing requests immediately; clear config
			 * later if it fails the request.
			 */
			if (udc->ep0_reset_config)
1610
				omap_writew(UDC_CLR_CFG, UDC_SYSCON2);
L
Linus Torvalds 已提交
1611
			else
1612
				omap_writew(UDC_DEV_CFG, UDC_SYSCON2);
L
Linus Torvalds 已提交
1613 1614 1615 1616 1617 1618
			update_otg(udc);
			goto delegate;
		case USB_REQ_CLEAR_FEATURE:
			/* clear endpoint halt */
			if (u.r.bRequestType != USB_RECIP_ENDPOINT)
				goto delegate;
1619 1620
			if (w_value != USB_ENDPOINT_HALT
					|| w_length != 0)
L
Linus Torvalds 已提交
1621
				goto do_stall;
1622
			ep = &udc->ep[w_index & 0xf];
L
Linus Torvalds 已提交
1623
			if (ep != ep0) {
1624
				if (w_index & USB_DIR_IN)
L
Linus Torvalds 已提交
1625 1626
					ep += 16;
				if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
1627
						|| !ep->ep.desc)
L
Linus Torvalds 已提交
1628 1629
					goto do_stall;
				use_ep(ep, 0);
1630
				omap_writew(udc->clr_halt, UDC_CTRL);
L
Linus Torvalds 已提交
1631 1632
				ep->ackwait = 0;
				if (!(ep->bEndpointAddress & USB_DIR_IN)) {
1633
					omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
L
Linus Torvalds 已提交
1634 1635
					ep->ackwait = 1 + ep->double_buf;
				}
1636 1637 1638 1639 1640
				/* NOTE:  assumes the host behaves sanely,
				 * only clearing real halts.  Else we may
				 * need to kill pending transfers and then
				 * restart the queue... very messy for DMA!
				 */
L
Linus Torvalds 已提交
1641 1642 1643 1644 1645 1646 1647
			}
			VDBG("%s halt cleared by host\n", ep->name);
			goto ep0out_status_stage;
		case USB_REQ_SET_FEATURE:
			/* set endpoint halt */
			if (u.r.bRequestType != USB_RECIP_ENDPOINT)
				goto delegate;
1648 1649
			if (w_value != USB_ENDPOINT_HALT
					|| w_length != 0)
L
Linus Torvalds 已提交
1650
				goto do_stall;
1651 1652
			ep = &udc->ep[w_index & 0xf];
			if (w_index & USB_DIR_IN)
L
Linus Torvalds 已提交
1653 1654
				ep += 16;
			if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
1655
					|| ep == ep0 || !ep->ep.desc)
L
Linus Torvalds 已提交
1656 1657 1658 1659 1660 1661 1662 1663 1664 1665
				goto do_stall;
			if (use_dma && ep->has_dma) {
				/* this has rude side-effects (aborts) and
				 * can't really work if DMA-IN is active
				 */
				DBG("%s host set_halt, NYET \n", ep->name);
				goto do_stall;
			}
			use_ep(ep, 0);
			/* can't halt if fifo isn't empty... */
1666 1667
			omap_writew(UDC_CLR_EP, UDC_CTRL);
			omap_writew(UDC_SET_HALT, UDC_CTRL);
L
Linus Torvalds 已提交
1668 1669 1670
			VDBG("%s halted by host\n", ep->name);
ep0out_status_stage:
			status = 0;
1671 1672 1673 1674
			omap_writew(UDC_EP_SEL|UDC_EP_DIR, UDC_EP_NUM);
			omap_writew(UDC_CLR_EP, UDC_CTRL);
			omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
			omap_writew(UDC_EP_DIR, UDC_EP_NUM);
L
Linus Torvalds 已提交
1675 1676 1677
			udc->ep0_pending = 0;
			break;
		case USB_REQ_GET_STATUS:
1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689
			/* USB_ENDPOINT_HALT status? */
			if (u.r.bRequestType != (USB_DIR_IN|USB_RECIP_ENDPOINT))
				goto intf_status;

			/* ep0 never stalls */
			if (!(w_index & 0xf))
				goto zero_status;

			/* only active endpoints count */
			ep = &udc->ep[w_index & 0xf];
			if (w_index & USB_DIR_IN)
				ep += 16;
1690
			if (!ep->ep.desc)
1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701
				goto do_stall;

			/* iso never stalls */
			if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC)
				goto zero_status;

			/* FIXME don't assume non-halted endpoints!! */
			ERR("%s status, can't report\n", ep->ep.name);
			goto do_stall;

intf_status:
L
Linus Torvalds 已提交
1702 1703 1704 1705 1706 1707
			/* return interface status.  if we were pedantic,
			 * we'd detect non-existent interfaces, and stall.
			 */
			if (u.r.bRequestType
					!= (USB_DIR_IN|USB_RECIP_INTERFACE))
				goto delegate;
1708 1709

zero_status:
L
Linus Torvalds 已提交
1710
			/* return two zero bytes */
1711 1712 1713 1714
			omap_writew(UDC_EP_SEL|UDC_EP_DIR, UDC_EP_NUM);
			omap_writew(0, UDC_DATA);
			omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
			omap_writew(UDC_EP_DIR, UDC_EP_NUM);
L
Linus Torvalds 已提交
1715
			status = 0;
1716
			VDBG("GET_STATUS, interface %d\n", w_index);
L
Linus Torvalds 已提交
1717 1718 1719 1720 1721
			/* next, status stage */
			break;
		default:
delegate:
			/* activate the ep0out fifo right away */
1722
			if (!udc->ep0_in && w_length) {
1723 1724
				omap_writew(0, UDC_EP_NUM);
				omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
L
Linus Torvalds 已提交
1725 1726 1727 1728 1729 1730 1731 1732
			}

			/* gadget drivers see class/vendor specific requests,
			 * {SET,GET}_{INTERFACE,DESCRIPTOR,CONFIGURATION},
			 * and more
			 */
			VDBG("SETUP %02x.%02x v%04x i%04x l%04x\n",
				u.r.bRequestType, u.r.bRequest,
1733 1734 1735 1736 1737
				w_value, w_index, w_length);

#undef	w_value
#undef	w_index
#undef	w_length
L
Linus Torvalds 已提交
1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762

			/* The gadget driver may return an error here,
			 * causing an immediate protocol stall.
			 *
			 * Else it must issue a response, either queueing a
			 * response buffer for the DATA stage, or halting ep0
			 * (causing a protocol stall, not a real halt).  A
			 * zero length buffer means no DATA stage.
			 *
			 * It's fine to issue that response after the setup()
			 * call returns, and this IRQ was handled.
			 */
			udc->ep0_setup = 1;
			spin_unlock(&udc->lock);
			status = udc->driver->setup (&udc->gadget, &u.r);
			spin_lock(&udc->lock);
			udc->ep0_setup = 0;
		}

		if (status < 0) {
do_stall:
			VDBG("req %02x.%02x protocol STALL; stat %d\n",
					u.r.bRequestType, u.r.bRequest, status);
			if (udc->ep0_set_config) {
				if (udc->ep0_reset_config)
1763
					WARNING("error resetting config?\n");
L
Linus Torvalds 已提交
1764
				else
1765
					omap_writew(UDC_CLR_CFG, UDC_SYSCON2);
L
Linus Torvalds 已提交
1766
			}
1767
			omap_writew(UDC_STALL_CMD, UDC_SYSCON2);
L
Linus Torvalds 已提交
1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780
			udc->ep0_pending = 0;
		}
	}
}

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

#define OTG_FLAGS (UDC_B_HNP_ENABLE|UDC_A_HNP_SUPPORT|UDC_A_ALT_HNP_SUPPORT)

static void devstate_irq(struct omap_udc *udc, u16 irq_src)
{
	u16	devstat, change;

1781
	devstat = omap_readw(UDC_DEVSTAT);
L
Linus Torvalds 已提交
1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820
	change = devstat ^ udc->devstat;
	udc->devstat = devstat;

	if (change & (UDC_USB_RESET|UDC_ATT)) {
		udc_quiesce(udc);

		if (change & UDC_ATT) {
			/* driver for any external transceiver will
			 * have called omap_vbus_session() already
			 */
			if (devstat & UDC_ATT) {
				udc->gadget.speed = USB_SPEED_FULL;
				VDBG("connect\n");
				if (!udc->transceiver)
					pullup_enable(udc);
				// if (driver->connect) call it
			} else if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
				udc->gadget.speed = USB_SPEED_UNKNOWN;
				if (!udc->transceiver)
					pullup_disable(udc);
				DBG("disconnect, gadget %s\n",
					udc->driver->driver.name);
				if (udc->driver->disconnect) {
					spin_unlock(&udc->lock);
					udc->driver->disconnect(&udc->gadget);
					spin_lock(&udc->lock);
				}
			}
			change &= ~UDC_ATT;
		}

		if (change & UDC_USB_RESET) {
			if (devstat & UDC_USB_RESET) {
				VDBG("RESET=1\n");
			} else {
				udc->gadget.speed = USB_SPEED_FULL;
				INFO("USB reset done, gadget %s\n",
					udc->driver->driver.name);
				/* ep0 traffic is legal from now on */
1821 1822
				omap_writew(UDC_DS_CHG_IE | UDC_EP0_IE,
						UDC_IRQ_EN);
L
Linus Torvalds 已提交
1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839
			}
			change &= ~UDC_USB_RESET;
		}
	}
	if (change & UDC_SUS) {
		if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
			// FIXME tell isp1301 to suspend/resume (?)
			if (devstat & UDC_SUS) {
				VDBG("suspend\n");
				update_otg(udc);
				/* HNP could be under way already */
				if (udc->gadget.speed == USB_SPEED_FULL
						&& udc->driver->suspend) {
					spin_unlock(&udc->lock);
					udc->driver->suspend(&udc->gadget);
					spin_lock(&udc->lock);
				}
1840
				if (udc->transceiver)
1841 1842
					usb_phy_set_suspend(
							udc->transceiver, 1);
L
Linus Torvalds 已提交
1843 1844
			} else {
				VDBG("resume\n");
1845
				if (udc->transceiver)
1846 1847
					usb_phy_set_suspend(
							udc->transceiver, 0);
L
Linus Torvalds 已提交
1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867
				if (udc->gadget.speed == USB_SPEED_FULL
						&& udc->driver->resume) {
					spin_unlock(&udc->lock);
					udc->driver->resume(&udc->gadget);
					spin_lock(&udc->lock);
				}
			}
		}
		change &= ~UDC_SUS;
	}
	if (!cpu_is_omap15xx() && (change & OTG_FLAGS)) {
		update_otg(udc);
		change &= ~OTG_FLAGS;
	}

	change &= ~(UDC_CFG|UDC_DEF|UDC_ADD);
	if (change)
		VDBG("devstat %03x, ignore change %03x\n",
			devstat,  change);

1868
	omap_writew(UDC_DS_CHG, UDC_IRQ_SRC);
L
Linus Torvalds 已提交
1869 1870
}

1871
static irqreturn_t omap_udc_irq(int irq, void *_udc)
L
Linus Torvalds 已提交
1872 1873 1874 1875 1876 1877 1878
{
	struct omap_udc	*udc = _udc;
	u16		irq_src;
	irqreturn_t	status = IRQ_NONE;
	unsigned long	flags;

	spin_lock_irqsave(&udc->lock, flags);
1879
	irq_src = omap_readw(UDC_IRQ_SRC);
L
Linus Torvalds 已提交
1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901

	/* Device state change (usb ch9 stuff) */
	if (irq_src & UDC_DS_CHG) {
		devstate_irq(_udc, irq_src);
		status = IRQ_HANDLED;
		irq_src &= ~UDC_DS_CHG;
	}

	/* EP0 control transfers */
	if (irq_src & (UDC_EP0_RX|UDC_SETUP|UDC_EP0_TX)) {
		ep0_irq(_udc, irq_src);
		status = IRQ_HANDLED;
		irq_src &= ~(UDC_EP0_RX|UDC_SETUP|UDC_EP0_TX);
	}

	/* DMA transfer completion */
	if (use_dma && (irq_src & (UDC_TXN_DONE|UDC_RXN_CNT|UDC_RXN_EOT))) {
		dma_irq(_udc, irq_src);
		status = IRQ_HANDLED;
		irq_src &= ~(UDC_TXN_DONE|UDC_RXN_CNT|UDC_RXN_EOT);
	}

1902
	irq_src &= ~(UDC_IRQ_SOF | UDC_EPN_TX|UDC_EPN_RX);
L
Linus Torvalds 已提交
1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921
	if (irq_src)
		DBG("udc_irq, unhandled %03x\n", irq_src);
	spin_unlock_irqrestore(&udc->lock, flags);

	return status;
}

/* workaround for seemingly-lost IRQs for RX ACKs... */
#define PIO_OUT_TIMEOUT	(jiffies + HZ/3)
#define HALF_FULL(f)	(!((f)&(UDC_NON_ISO_FIFO_FULL|UDC_NON_ISO_FIFO_EMPTY)))

static void pio_out_timer(unsigned long _ep)
{
	struct omap_ep	*ep = (void *) _ep;
	unsigned long	flags;
	u16		stat_flg;

	spin_lock_irqsave(&ep->udc->lock, flags);
	if (!list_empty(&ep->queue) && ep->ackwait) {
1922
		use_ep(ep, UDC_EP_SEL);
1923
		stat_flg = omap_readw(UDC_STAT_FLG);
L
Linus Torvalds 已提交
1924 1925 1926 1927 1928 1929 1930 1931 1932

		if ((stat_flg & UDC_ACK) && (!(stat_flg & UDC_FIFO_EN)
				|| (ep->double_buf && HALF_FULL(stat_flg)))) {
			struct omap_req	*req;

			VDBG("%s: lose, %04x\n", ep->ep.name, stat_flg);
			req = container_of(ep->queue.next,
					struct omap_req, queue);
			(void) read_fifo(ep, req);
1933 1934
			omap_writew(ep->bEndpointAddress, UDC_EP_NUM);
			omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
L
Linus Torvalds 已提交
1935
			ep->ackwait = 1 + ep->double_buf;
1936 1937
		} else
			deselect_ep();
L
Linus Torvalds 已提交
1938 1939 1940 1941 1942
	}
	mod_timer(&ep->timer, PIO_OUT_TIMEOUT);
	spin_unlock_irqrestore(&ep->udc->lock, flags);
}

1943
static irqreturn_t omap_udc_pio_irq(int irq, void *_dev)
L
Linus Torvalds 已提交
1944 1945 1946 1947 1948 1949 1950 1951 1952 1953
{
	u16		epn_stat, irq_src;
	irqreturn_t	status = IRQ_NONE;
	struct omap_ep	*ep;
	int		epnum;
	struct omap_udc	*udc = _dev;
	struct omap_req	*req;
	unsigned long	flags;

	spin_lock_irqsave(&udc->lock, flags);
1954 1955
	epn_stat = omap_readw(UDC_EPN_STAT);
	irq_src = omap_readw(UDC_IRQ_SRC);
L
Linus Torvalds 已提交
1956 1957 1958 1959

	/* handle OUT first, to avoid some wasteful NAKs */
	if (irq_src & UDC_EPN_RX) {
		epnum = (epn_stat >> 8) & 0x0f;
1960
		omap_writew(UDC_EPN_RX, UDC_IRQ_SRC);
L
Linus Torvalds 已提交
1961 1962 1963 1964
		status = IRQ_HANDLED;
		ep = &udc->ep[epnum];
		ep->irqs++;

1965
		omap_writew(epnum | UDC_EP_SEL, UDC_EP_NUM);
L
Linus Torvalds 已提交
1966
		ep->fnf = 0;
1967
		if (omap_readw(UDC_STAT_FLG) & UDC_ACK) {
L
Linus Torvalds 已提交
1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978
			ep->ackwait--;
			if (!list_empty(&ep->queue)) {
				int stat;
				req = container_of(ep->queue.next,
						struct omap_req, queue);
				stat = read_fifo(ep, req);
				if (!ep->double_buf)
					ep->fnf = 1;
			}
		}
		/* min 6 clock delay before clearing EP_SEL ... */
1979 1980 1981
		epn_stat = omap_readw(UDC_EPN_STAT);
		epn_stat = omap_readw(UDC_EPN_STAT);
		omap_writew(epnum, UDC_EP_NUM);
L
Linus Torvalds 已提交
1982 1983 1984 1985 1986

		/* enabling fifo _after_ clearing ACK, contrary to docs,
		 * reduces lossage; timer still needed though (sigh).
		 */
		if (ep->fnf) {
1987
			omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
L
Linus Torvalds 已提交
1988 1989 1990 1991 1992 1993 1994 1995
			ep->ackwait = 1 + ep->double_buf;
		}
		mod_timer(&ep->timer, PIO_OUT_TIMEOUT);
	}

	/* then IN transfers */
	else if (irq_src & UDC_EPN_TX) {
		epnum = epn_stat & 0x0f;
1996
		omap_writew(UDC_EPN_TX, UDC_IRQ_SRC);
L
Linus Torvalds 已提交
1997 1998 1999 2000
		status = IRQ_HANDLED;
		ep = &udc->ep[16 + epnum];
		ep->irqs++;

2001 2002
		omap_writew(epnum | UDC_EP_DIR | UDC_EP_SEL, UDC_EP_NUM);
		if (omap_readw(UDC_STAT_FLG) & UDC_ACK) {
L
Linus Torvalds 已提交
2003 2004 2005 2006 2007 2008 2009 2010
			ep->ackwait = 0;
			if (!list_empty(&ep->queue)) {
				req = container_of(ep->queue.next,
						struct omap_req, queue);
				(void) write_fifo(ep, req);
			}
		}
		/* min 6 clock delay before clearing EP_SEL ... */
2011 2012 2013
		epn_stat = omap_readw(UDC_EPN_STAT);
		epn_stat = omap_readw(UDC_EPN_STAT);
		omap_writew(epnum | UDC_EP_DIR, UDC_EP_NUM);
L
Linus Torvalds 已提交
2014 2015 2016 2017 2018 2019 2020 2021
		/* then 6 clocks before it'd tx */
	}

	spin_unlock_irqrestore(&udc->lock, flags);
	return status;
}

#ifdef	USE_ISO
2022
static irqreturn_t omap_udc_iso_irq(int irq, void *_dev)
L
Linus Torvalds 已提交
2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040
{
	struct omap_udc	*udc = _dev;
	struct omap_ep	*ep;
	int		pending = 0;
	unsigned long	flags;

	spin_lock_irqsave(&udc->lock, flags);

	/* handle all non-DMA ISO transfers */
	list_for_each_entry (ep, &udc->iso, iso) {
		u16		stat;
		struct omap_req	*req;

		if (ep->has_dma || list_empty(&ep->queue))
			continue;
		req = list_entry(ep->queue.next, struct omap_req, queue);

		use_ep(ep, UDC_EP_SEL);
2041
		stat = omap_readw(UDC_STAT_FLG);
L
Linus Torvalds 已提交
2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072

		/* NOTE: like the other controller drivers, this isn't
		 * currently reporting lost or damaged frames.
		 */
		if (ep->bEndpointAddress & USB_DIR_IN) {
			if (stat & UDC_MISS_IN)
				/* done(ep, req, -EPROTO) */;
			else
				write_fifo(ep, req);
		} else {
			int	status = 0;

			if (stat & UDC_NO_RXPACKET)
				status = -EREMOTEIO;
			else if (stat & UDC_ISO_ERR)
				status = -EILSEQ;
			else if (stat & UDC_DATA_FLUSH)
				status = -ENOSR;

			if (status)
				/* done(ep, req, status) */;
			else
				read_fifo(ep, req);
		}
		deselect_ep();
		/* 6 wait states before next EP */

		ep->irqs++;
		if (!list_empty(&ep->queue))
			pending = 1;
	}
2073 2074 2075 2076 2077 2078 2079 2080
	if (!pending) {
		u16 w;

		w = omap_readw(UDC_IRQ_EN);
		w &= ~UDC_SOF_IE;
		omap_writew(w, UDC_IRQ_EN);
	}
	omap_writew(UDC_IRQ_SOF, UDC_IRQ_SRC);
L
Linus Torvalds 已提交
2081 2082 2083 2084 2085 2086 2087 2088

	spin_unlock_irqrestore(&udc->lock, flags);
	return IRQ_HANDLED;
}
#endif

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

2089
static inline int machine_without_vbus_sense(void)
2090 2091 2092 2093 2094 2095 2096 2097
{
	return (machine_is_omap_innovator()
		|| machine_is_omap_osk()
		|| machine_is_omap_apollon()
#ifndef CONFIG_MACH_OMAP_H4_OTG
		|| machine_is_omap_h4()
#endif
		|| machine_is_sx1()
2098
		|| cpu_is_omap7xx() /* No known omap7xx boards with vbus sense */
2099 2100
		);
}
L
Linus Torvalds 已提交
2101

2102
static int omap_udc_start(struct usb_gadget_driver *driver,
2103
		int (*bind)(struct usb_gadget *))
L
Linus Torvalds 已提交
2104 2105 2106 2107 2108 2109 2110 2111 2112 2113
{
	int		status = -ENODEV;
	struct omap_ep	*ep;
	unsigned long	flags;

	/* basic sanity tests */
	if (!udc)
		return -ENODEV;
	if (!driver
			// FIXME if otg, check:  driver->is_otg
2114
			|| driver->max_speed < USB_SPEED_FULL
2115
			|| !bind || !driver->setup)
L
Linus Torvalds 已提交
2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129
		return -EINVAL;

	spin_lock_irqsave(&udc->lock, flags);
	if (udc->driver) {
		spin_unlock_irqrestore(&udc->lock, flags);
		return -EBUSY;
	}

	/* reset state */
	list_for_each_entry (ep, &udc->gadget.ep_list, ep.ep_list) {
		ep->irqs = 0;
		if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC)
			continue;
		use_ep(ep, 0);
2130
		omap_writew(UDC_SET_HALT, UDC_CTRL);
L
Linus Torvalds 已提交
2131 2132 2133 2134 2135 2136
	}
	udc->ep0_pending = 0;
	udc->ep[0].irqs = 0;
	udc->softconnect = 1;

	/* hook up the driver */
2137
	driver->driver.bus = NULL;
L
Linus Torvalds 已提交
2138 2139 2140 2141
	udc->driver = driver;
	udc->gadget.dev.driver = &driver->driver;
	spin_unlock_irqrestore(&udc->lock, flags);

2142 2143 2144
	if (udc->dc_clk != NULL)
		omap_udc_enable_clock(1);

2145
	status = bind(&udc->gadget);
L
Linus Torvalds 已提交
2146 2147
	if (status) {
		DBG("bind to %s --> %d\n", driver->driver.name, status);
2148 2149
		udc->gadget.dev.driver = NULL;
		udc->driver = NULL;
L
Linus Torvalds 已提交
2150 2151 2152 2153
		goto done;
	}
	DBG("bound to driver %s\n", driver->driver.name);

2154
	omap_writew(UDC_IRQ_SRC_MASK, UDC_IRQ_SRC);
L
Linus Torvalds 已提交
2155 2156 2157

	/* connect to bus through transceiver */
	if (udc->transceiver) {
2158 2159
		status = otg_set_peripheral(udc->transceiver->otg,
						&udc->gadget);
L
Linus Torvalds 已提交
2160 2161
		if (status < 0) {
			ERR("can't bind to transceiver\n");
2162 2163 2164 2165 2166
			if (driver->unbind) {
				driver->unbind (&udc->gadget);
				udc->gadget.dev.driver = NULL;
				udc->driver = NULL;
			}
L
Linus Torvalds 已提交
2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178
			goto done;
		}
	} else {
		if (can_pullup(udc))
			pullup_enable (udc);
		else
			pullup_disable (udc);
	}

	/* boards that don't have VBUS sensing can't autogate 48MHz;
	 * can't enter deep sleep while a gadget driver is active.
	 */
2179
	if (machine_without_vbus_sense())
L
Linus Torvalds 已提交
2180 2181 2182
		omap_vbus_session(&udc->gadget, 1);

done:
2183 2184
	if (udc->dc_clk != NULL)
		omap_udc_enable_clock(0);
L
Linus Torvalds 已提交
2185 2186 2187
	return status;
}

2188
static int omap_udc_stop(struct usb_gadget_driver *driver)
L
Linus Torvalds 已提交
2189 2190 2191 2192 2193 2194
{
	unsigned long	flags;
	int		status = -ENODEV;

	if (!udc)
		return -ENODEV;
2195
	if (!driver || driver != udc->driver || !driver->unbind)
L
Linus Torvalds 已提交
2196 2197
		return -EINVAL;

2198 2199 2200
	if (udc->dc_clk != NULL)
		omap_udc_enable_clock(1);

2201
	if (machine_without_vbus_sense())
L
Linus Torvalds 已提交
2202 2203 2204
		omap_vbus_session(&udc->gadget, 0);

	if (udc->transceiver)
2205
		(void) otg_set_peripheral(udc->transceiver->otg, NULL);
L
Linus Torvalds 已提交
2206 2207 2208 2209 2210 2211 2212 2213
	else
		pullup_disable(udc);

	spin_lock_irqsave(&udc->lock, flags);
	udc_quiesce(udc);
	spin_unlock_irqrestore(&udc->lock, flags);

	driver->unbind(&udc->gadget);
2214 2215
	udc->gadget.dev.driver = NULL;
	udc->driver = NULL;
L
Linus Torvalds 已提交
2216

2217 2218
	if (udc->dc_clk != NULL)
		omap_udc_enable_clock(0);
L
Linus Torvalds 已提交
2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248
	DBG("unregistered driver '%s'\n", driver->driver.name);
	return status;
}

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

#ifdef CONFIG_USB_GADGET_DEBUG_FILES

#include <linux/seq_file.h>

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

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

static void proc_ep_show(struct seq_file *s, struct omap_ep *ep)
{
	u16		stat_flg;
	struct omap_req	*req;
	char		buf[20];

	use_ep(ep, 0);

	if (use_dma && ep->has_dma)
		snprintf(buf, sizeof buf, "(%cxdma%d lch%d) ",
			(ep->bEndpointAddress & USB_DIR_IN) ? 't' : 'r',
			ep->dma_channel - 1, ep->lch);
	else
		buf[0] = 0;

2249
	stat_flg = omap_readw(UDC_STAT_FLG);
L
Linus Torvalds 已提交
2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298
	seq_printf(s,
		"\n%s %s%s%sirqs %ld stat %04x " EIGHTBITS FOURBITS "%s\n",
		ep->name, buf,
		ep->double_buf ? "dbuf " : "",
		({char *s; switch(ep->ackwait){
		case 0: s = ""; break;
		case 1: s = "(ackw) "; break;
		case 2: s = "(ackw2) "; break;
		default: s = "(?) "; break;
		} s;}),
		ep->irqs, stat_flg,
		(stat_flg & UDC_NO_RXPACKET) ? "no_rxpacket " : "",
		(stat_flg & UDC_MISS_IN) ? "miss_in " : "",
		(stat_flg & UDC_DATA_FLUSH) ? "data_flush " : "",
		(stat_flg & UDC_ISO_ERR) ? "iso_err " : "",
		(stat_flg & UDC_ISO_FIFO_EMPTY) ? "iso_fifo_empty " : "",
		(stat_flg & UDC_ISO_FIFO_FULL) ? "iso_fifo_full " : "",
		(stat_flg & UDC_EP_HALTED) ? "HALT " : "",
		(stat_flg & UDC_STALL) ? "STALL " : "",
		(stat_flg & UDC_NAK) ? "NAK " : "",
		(stat_flg & UDC_ACK) ? "ACK " : "",
		(stat_flg & UDC_FIFO_EN) ? "fifo_en " : "",
		(stat_flg & UDC_NON_ISO_FIFO_EMPTY) ? "fifo_empty " : "",
		(stat_flg & UDC_NON_ISO_FIFO_FULL) ? "fifo_full " : "");

	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;

			if (use_dma && buf[0]) {
				length += ((ep->bEndpointAddress & USB_DIR_IN)
						? dma_src_len : dma_dest_len)
					(ep, req->req.dma + length);
				buf[0] = 0;
			}
			seq_printf(s, "\treq %p len %d/%d buf %p\n",
					&req->req, length,
					req->req.length, req->req.buf);
		}
}

static char *trx_mode(unsigned m, int enabled)
{
	switch (m) {
	case 0:		return enabled ? "*6wire" : "unused";
	case 1:		return "4wire";
	case 2:		return "3wire";
2299
	case 3:		return "6wire";
L
Linus Torvalds 已提交
2300 2301 2302 2303 2304 2305 2306
	default:	return "unknown";
	}
}

static int proc_otg_show(struct seq_file *s)
{
	u32		tmp;
2307 2308
	u32		trans = 0;
	char		*ctrl_name = "(UNKNOWN)";
L
Linus Torvalds 已提交
2309

2310
	/* XXX This needs major revision for OMAP2+ */
2311
	tmp = omap_readl(OTG_REV);
2312
	if (cpu_class_is_omap1()) {
2313
		ctrl_name = "tranceiver_ctrl";
2314
		trans = omap_readw(USB_TRANSCEIVER_CTRL);
2315 2316 2317
	}
	seq_printf(s, "\nOTG rev %d.%d, %s %05x\n",
		tmp >> 4, tmp & 0xf, ctrl_name, trans);
2318
	tmp = omap_readw(OTG_SYSCON_1);
L
Linus Torvalds 已提交
2319 2320 2321 2322
	seq_printf(s, "otg_syscon1 %08x usb2 %s, usb1 %s, usb0 %s,"
			FOURBITS "\n", tmp,
		trx_mode(USB2_TRX_MODE(tmp), trans & CONF_USB2_UNI_R),
		trx_mode(USB1_TRX_MODE(tmp), trans & CONF_USB1_UNI_R),
2323
		(USB0_TRX_MODE(tmp) == 0 && !cpu_is_omap1710())
L
Linus Torvalds 已提交
2324 2325 2326 2327 2328 2329
			? "internal"
			: trx_mode(USB0_TRX_MODE(tmp), 1),
		(tmp & OTG_IDLE_EN) ? " !otg" : "",
		(tmp & HST_IDLE_EN) ? " !host" : "",
		(tmp & DEV_IDLE_EN) ? " !dev" : "",
		(tmp & OTG_RESET_DONE) ? " reset_done" : " reset_active");
2330
	tmp = omap_readl(OTG_SYSCON_2);
L
Linus Torvalds 已提交
2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344
	seq_printf(s, "otg_syscon2 %08x%s" EIGHTBITS
			" b_ase_brst=%d hmc=%d\n", tmp,
		(tmp & OTG_EN) ? " otg_en" : "",
		(tmp & USBX_SYNCHRO) ? " synchro" : "",
		// much more SRP stuff
		(tmp & SRP_DATA) ? " srp_data" : "",
		(tmp & SRP_VBUS) ? " srp_vbus" : "",
		(tmp & OTG_PADEN) ? " otg_paden" : "",
		(tmp & HMC_PADEN) ? " hmc_paden" : "",
		(tmp & UHOST_EN) ? " uhost_en" : "",
		(tmp & HMC_TLLSPEED) ? " tllspeed" : "",
		(tmp & HMC_TLLATTACH) ? " tllattach" : "",
		B_ASE_BRST(tmp),
		OTG_HMC(tmp));
2345
	tmp = omap_readl(OTG_CTRL);
L
Linus Torvalds 已提交
2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364
	seq_printf(s, "otg_ctrl    %06x" EIGHTBITS EIGHTBITS "%s\n", tmp,
		(tmp & OTG_ASESSVLD) ? " asess" : "",
		(tmp & OTG_BSESSEND) ? " bsess_end" : "",
		(tmp & OTG_BSESSVLD) ? " bsess" : "",
		(tmp & OTG_VBUSVLD) ? " vbus" : "",
		(tmp & OTG_ID) ? " id" : "",
		(tmp & OTG_DRIVER_SEL) ? " DEVICE" : " HOST",
		(tmp & OTG_A_SETB_HNPEN) ? " a_setb_hnpen" : "",
		(tmp & OTG_A_BUSREQ) ? " a_bus" : "",
		(tmp & OTG_B_HNPEN) ? " b_hnpen" : "",
		(tmp & OTG_B_BUSREQ) ? " b_bus" : "",
		(tmp & OTG_BUSDROP) ? " busdrop" : "",
		(tmp & OTG_PULLDOWN) ? " down" : "",
		(tmp & OTG_PULLUP) ? " up" : "",
		(tmp & OTG_DRV_VBUS) ? " drv" : "",
		(tmp & OTG_PD_VBUS) ? " pd_vb" : "",
		(tmp & OTG_PU_VBUS) ? " pu_vb" : "",
		(tmp & OTG_PU_ID) ? " pu_id" : ""
		);
2365
	tmp = omap_readw(OTG_IRQ_EN);
L
Linus Torvalds 已提交
2366
	seq_printf(s, "otg_irq_en  %04x" "\n", tmp);
2367
	tmp = omap_readw(OTG_IRQ_SRC);
L
Linus Torvalds 已提交
2368
	seq_printf(s, "otg_irq_src %04x" "\n", tmp);
2369
	tmp = omap_readw(OTG_OUTCTRL);
L
Linus Torvalds 已提交
2370
	seq_printf(s, "otg_outctrl %04x" "\n", tmp);
2371
	tmp = omap_readw(OTG_TEST);
L
Linus Torvalds 已提交
2372
	seq_printf(s, "otg_test    %04x" "\n", tmp);
2373
	return 0;
L
Linus Torvalds 已提交
2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391
}

static int proc_udc_show(struct seq_file *s, void *_)
{
	u32		tmp;
	struct omap_ep	*ep;
	unsigned long	flags;

	spin_lock_irqsave(&udc->lock, flags);

	seq_printf(s, "%s, version: " DRIVER_VERSION
#ifdef	USE_ISO
		" (iso)"
#endif
		"%s\n",
		driver_desc,
		use_dma ?  " (dma)" : "");

2392
	tmp = omap_readw(UDC_REV) & 0xff;
L
Linus Torvalds 已提交
2393 2394 2395 2396 2397 2398 2399
	seq_printf(s,
		"UDC rev %d.%d, fifo mode %d, gadget %s\n"
		"hmc %d, transceiver %s\n",
		tmp >> 4, tmp & 0xf,
		fifo_mode,
		udc->driver ? udc->driver->driver.name : "(none)",
		HMC,
2400 2401 2402 2403 2404 2405
		udc->transceiver
			? udc->transceiver->label
			: ((cpu_is_omap1710() || cpu_is_omap24xx())
				? "external" : "(none)"));
	if (cpu_class_is_omap1()) {
		seq_printf(s, "ULPD control %04x req %04x status %04x\n",
2406 2407 2408
			omap_readw(ULPD_CLOCK_CTRL),
			omap_readw(ULPD_SOFT_REQ),
			omap_readw(ULPD_STATUS_REQ));
2409
	}
L
Linus Torvalds 已提交
2410 2411 2412 2413 2414

	/* OTG controller registers */
	if (!cpu_is_omap15xx())
		proc_otg_show(s);

2415
	tmp = omap_readw(UDC_SYSCON1);
L
Linus Torvalds 已提交
2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433
	seq_printf(s, "\nsyscon1     %04x" EIGHTBITS "\n", tmp,
		(tmp & UDC_CFG_LOCK) ? " cfg_lock" : "",
		(tmp & UDC_DATA_ENDIAN) ? " data_endian" : "",
		(tmp & UDC_DMA_ENDIAN) ? " dma_endian" : "",
		(tmp & UDC_NAK_EN) ? " nak" : "",
		(tmp & UDC_AUTODECODE_DIS) ? " autodecode_dis" : "",
		(tmp & UDC_SELF_PWR) ? " self_pwr" : "",
		(tmp & UDC_SOFF_DIS) ? " soff_dis" : "",
		(tmp & UDC_PULLUP_EN) ? " PULLUP" : "");
	// syscon2 is write-only

	/* UDC controller registers */
	if (!(tmp & UDC_PULLUP_EN)) {
		seq_printf(s, "(suspended)\n");
		spin_unlock_irqrestore(&udc->lock, flags);
		return 0;
	}

2434
	tmp = omap_readw(UDC_DEVSTAT);
L
Linus Torvalds 已提交
2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445
	seq_printf(s, "devstat     %04x" EIGHTBITS "%s%s\n", tmp,
		(tmp & UDC_B_HNP_ENABLE) ? " b_hnp" : "",
		(tmp & UDC_A_HNP_SUPPORT) ? " a_hnp" : "",
		(tmp & UDC_A_ALT_HNP_SUPPORT) ? " a_alt_hnp" : "",
		(tmp & UDC_R_WK_OK) ? " r_wk_ok" : "",
		(tmp & UDC_USB_RESET) ? " usb_reset" : "",
		(tmp & UDC_SUS) ? " SUS" : "",
		(tmp & UDC_CFG) ? " CFG" : "",
		(tmp & UDC_ADD) ? " ADD" : "",
		(tmp & UDC_DEF) ? " DEF" : "",
		(tmp & UDC_ATT) ? " ATT" : "");
2446 2447
	seq_printf(s, "sof         %04x\n", omap_readw(UDC_SOF));
	tmp = omap_readw(UDC_IRQ_EN);
L
Linus Torvalds 已提交
2448 2449 2450 2451 2452 2453
	seq_printf(s, "irq_en      %04x" FOURBITS "%s\n", tmp,
		(tmp & UDC_SOF_IE) ? " sof" : "",
		(tmp & UDC_EPN_RX_IE) ? " epn_rx" : "",
		(tmp & UDC_EPN_TX_IE) ? " epn_tx" : "",
		(tmp & UDC_DS_CHG_IE) ? " ds_chg" : "",
		(tmp & UDC_EP0_IE) ? " ep0" : "");
2454
	tmp = omap_readw(UDC_IRQ_SRC);
L
Linus Torvalds 已提交
2455 2456 2457 2458
	seq_printf(s, "irq_src     %04x" EIGHTBITS "%s%s\n", tmp,
		(tmp & UDC_TXN_DONE) ? " txn_done" : "",
		(tmp & UDC_RXN_CNT) ? " rxn_cnt" : "",
		(tmp & UDC_RXN_EOT) ? " rxn_eot" : "",
2459
		(tmp & UDC_IRQ_SOF) ? " sof" : "",
L
Linus Torvalds 已提交
2460 2461 2462 2463 2464 2465 2466 2467 2468
		(tmp & UDC_EPN_RX) ? " epn_rx" : "",
		(tmp & UDC_EPN_TX) ? " epn_tx" : "",
		(tmp & UDC_DS_CHG) ? " ds_chg" : "",
		(tmp & UDC_SETUP) ? " setup" : "",
		(tmp & UDC_EP0_RX) ? " ep0out" : "",
		(tmp & UDC_EP0_TX) ? " ep0in" : "");
	if (use_dma) {
		unsigned i;

2469
		tmp = omap_readw(UDC_DMA_IRQ_EN);
L
Linus Torvalds 已提交
2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482
		seq_printf(s, "dma_irq_en  %04x%s" EIGHTBITS "\n", tmp,
			(tmp & UDC_TX_DONE_IE(3)) ? " tx2_done" : "",
			(tmp & UDC_RX_CNT_IE(3)) ? " rx2_cnt" : "",
			(tmp & UDC_RX_EOT_IE(3)) ? " rx2_eot" : "",

			(tmp & UDC_TX_DONE_IE(2)) ? " tx1_done" : "",
			(tmp & UDC_RX_CNT_IE(2)) ? " rx1_cnt" : "",
			(tmp & UDC_RX_EOT_IE(2)) ? " rx1_eot" : "",

			(tmp & UDC_TX_DONE_IE(1)) ? " tx0_done" : "",
			(tmp & UDC_RX_CNT_IE(1)) ? " rx0_cnt" : "",
			(tmp & UDC_RX_EOT_IE(1)) ? " rx0_eot" : "");

2483
		tmp = omap_readw(UDC_RXDMA_CFG);
L
Linus Torvalds 已提交
2484 2485 2486 2487 2488 2489
		seq_printf(s, "rxdma_cfg   %04x\n", tmp);
		if (tmp) {
			for (i = 0; i < 3; i++) {
				if ((tmp & (0x0f << (i * 4))) == 0)
					continue;
				seq_printf(s, "rxdma[%d]    %04x\n", i,
2490
						omap_readw(UDC_RXDMA(i + 1)));
L
Linus Torvalds 已提交
2491 2492
			}
		}
2493
		tmp = omap_readw(UDC_TXDMA_CFG);
L
Linus Torvalds 已提交
2494 2495 2496 2497 2498 2499
		seq_printf(s, "txdma_cfg   %04x\n", tmp);
		if (tmp) {
			for (i = 0; i < 3; i++) {
				if (!(tmp & (0x0f << (i * 4))))
					continue;
				seq_printf(s, "txdma[%d]    %04x\n", i,
2500
						omap_readw(UDC_TXDMA(i + 1)));
L
Linus Torvalds 已提交
2501 2502 2503 2504
			}
		}
	}

2505
	tmp = omap_readw(UDC_DEVSTAT);
L
Linus Torvalds 已提交
2506 2507 2508 2509 2510
	if (tmp & UDC_ATT) {
		proc_ep_show(s, &udc->ep[0]);
		if (tmp & UDC_ADD) {
			list_for_each_entry (ep, &udc->gadget.ep_list,
					ep.ep_list) {
2511
				if (ep->ep.desc)
L
Linus Torvalds 已提交
2512 2513 2514 2515 2516 2517 2518 2519 2520 2521
					proc_ep_show(s, ep);
			}
		}
	}
	spin_unlock_irqrestore(&udc->lock, flags);
	return 0;
}

static int proc_udc_open(struct inode *inode, struct file *file)
{
2522
	return single_open(file, proc_udc_show, NULL);
L
Linus Torvalds 已提交
2523 2524
}

2525
static const struct file_operations proc_ops = {
2526
	.owner		= THIS_MODULE,
L
Linus Torvalds 已提交
2527 2528 2529 2530 2531 2532 2533 2534
	.open		= proc_udc_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= single_release,
};

static void create_proc_file(void)
{
2535
	proc_create(proc_filename, 0, NULL, &proc_ops);
L
Linus Torvalds 已提交
2536 2537 2538 2539
}

static void remove_proc_file(void)
{
2540
	remove_proc_entry(proc_filename, NULL);
L
Linus Torvalds 已提交
2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554
}

#else

static inline void create_proc_file(void) {}
static inline void remove_proc_file(void) {}

#endif

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

/* Before this controller can enumerate, we need to pick an endpoint
 * configuration, or "fifo_mode"  That involves allocating 2KB of packet
 * buffer space among the endpoints we'll be operating.
2555 2556
 *
 * NOTE: as of OMAP 1710 ES2.0, writing a new endpoint config when
2557
 * UDC_SYSCON_1.CFG_LOCK is set can now work.  We won't use that
2558
 * capability yet though.
L
Linus Torvalds 已提交
2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590
 */
static unsigned __init
omap_ep_setup(char *name, u8 addr, u8 type,
		unsigned buf, unsigned maxp, int dbuf)
{
	struct omap_ep	*ep;
	u16		epn_rxtx = 0;

	/* OUT endpoints first, then IN */
	ep = &udc->ep[addr & 0xf];
	if (addr & USB_DIR_IN)
		ep += 16;

	/* in case of ep init table bugs */
	BUG_ON(ep->name[0]);

	/* chip setup ... bit values are same for IN, OUT */
	if (type == USB_ENDPOINT_XFER_ISOC) {
		switch (maxp) {
		case 8:		epn_rxtx = 0 << 12; break;
		case 16:	epn_rxtx = 1 << 12; break;
		case 32:	epn_rxtx = 2 << 12; break;
		case 64:	epn_rxtx = 3 << 12; break;
		case 128:	epn_rxtx = 4 << 12; break;
		case 256:	epn_rxtx = 5 << 12; break;
		case 512:	epn_rxtx = 6 << 12; break;
		default:	BUG();
		}
		epn_rxtx |= UDC_EPN_RX_ISO;
		dbuf = 1;
	} else {
		/* double-buffering "not supported" on 15xx,
2591 2592
		 * and ignored for PIO-IN on newer chips
		 * (for more reliable behavior)
L
Linus Torvalds 已提交
2593
		 */
2594
		if (!use_dma || cpu_is_omap15xx() || cpu_is_omap24xx())
L
Linus Torvalds 已提交
2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618
			dbuf = 0;

		switch (maxp) {
		case 8:		epn_rxtx = 0 << 12; break;
		case 16:	epn_rxtx = 1 << 12; break;
		case 32:	epn_rxtx = 2 << 12; break;
		case 64:	epn_rxtx = 3 << 12; break;
		default:	BUG();
		}
		if (dbuf && addr)
			epn_rxtx |= UDC_EPN_RX_DB;
		init_timer(&ep->timer);
		ep->timer.function = pio_out_timer;
		ep->timer.data = (unsigned long) ep;
	}
	if (addr)
		epn_rxtx |= UDC_EPN_RX_VALID;
	BUG_ON(buf & 0x07);
	epn_rxtx |= buf >> 3;

	DBG("%s addr %02x rxtx %04x maxp %d%s buf %d\n",
		name, addr, epn_rxtx, maxp, dbuf ? "x2" : "", buf);

	if (addr & USB_DIR_IN)
2619
		omap_writew(epn_rxtx, UDC_EP_TX(addr & 0xf));
L
Linus Torvalds 已提交
2620
	else
2621
		omap_writew(epn_rxtx, UDC_EP_RX(addr));
L
Linus Torvalds 已提交
2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636

	/* next endpoint's buffer starts after this one's */
	buf += maxp;
	if (dbuf)
		buf += maxp;
	BUG_ON(buf > 2048);

	/* set up driver data structures */
	BUG_ON(strlen(name) >= sizeof ep->name);
	strlcpy(ep->name, name, sizeof ep->name);
	INIT_LIST_HEAD(&ep->queue);
	INIT_LIST_HEAD(&ep->iso);
	ep->bEndpointAddress = addr;
	ep->bmAttributes = type;
	ep->double_buf = dbuf;
2637
	ep->udc = udc;
L
Linus Torvalds 已提交
2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650

	ep->ep.name = ep->name;
	ep->ep.ops = &omap_ep_ops;
	ep->ep.maxpacket = ep->maxpacket = maxp;
	list_add_tail (&ep->ep.ep_list, &udc->gadget.ep_list);

	return buf;
}

static void omap_udc_release(struct device *dev)
{
	complete(udc->done);
	kfree (udc);
2651
	udc = NULL;
L
Linus Torvalds 已提交
2652 2653 2654
}

static int __init
2655
omap_udc_setup(struct platform_device *odev, struct usb_phy *xceiv)
L
Linus Torvalds 已提交
2656 2657 2658 2659
{
	unsigned	tmp, buf;

	/* abolish any previous hardware state */
2660 2661 2662 2663 2664 2665
	omap_writew(0, UDC_SYSCON1);
	omap_writew(0, UDC_IRQ_EN);
	omap_writew(UDC_IRQ_SRC_MASK, UDC_IRQ_SRC);
	omap_writew(0, UDC_DMA_IRQ_EN);
	omap_writew(0, UDC_RXDMA_CFG);
	omap_writew(0, UDC_TXDMA_CFG);
L
Linus Torvalds 已提交
2666 2667

	/* UDC_PULLUP_EN gates the chip clock */
2668
	// OTG_SYSCON_1 |= DEV_IDLE_EN;
L
Linus Torvalds 已提交
2669

2670
	udc = kzalloc(sizeof(*udc), GFP_KERNEL);
L
Linus Torvalds 已提交
2671 2672 2673 2674 2675 2676 2677 2678 2679 2680
	if (!udc)
		return -ENOMEM;

	spin_lock_init (&udc->lock);

	udc->gadget.ops = &omap_gadget_ops;
	udc->gadget.ep0 = &udc->ep[0].ep;
	INIT_LIST_HEAD(&udc->gadget.ep_list);
	INIT_LIST_HEAD(&udc->iso);
	udc->gadget.speed = USB_SPEED_UNKNOWN;
2681
	udc->gadget.max_speed = USB_SPEED_FULL;
L
Linus Torvalds 已提交
2682 2683 2684
	udc->gadget.name = driver_name;

	device_initialize(&udc->gadget.dev);
2685
	dev_set_name(&udc->gadget.dev, "gadget");
L
Linus Torvalds 已提交
2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699
	udc->gadget.dev.release = omap_udc_release;
	udc->gadget.dev.parent = &odev->dev;
	if (use_dma)
		udc->gadget.dev.dma_mask = odev->dev.dma_mask;

	udc->transceiver = xceiv;

	/* ep0 is special; put it right after the SETUP buffer */
	buf = omap_ep_setup("ep0", 0, USB_ENDPOINT_XFER_CONTROL,
			8 /* after SETUP */, 64 /* maxpacket */, 0);
	list_del_init(&udc->ep[0].ep.ep_list);

	/* initially disable all non-ep0 endpoints */
	for (tmp = 1; tmp < 15; tmp++) {
2700 2701
		omap_writew(0, UDC_EP_RX(tmp));
		omap_writew(0, UDC_EP_TX(tmp));
L
Linus Torvalds 已提交
2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722
	}

#define OMAP_BULK_EP(name,addr) \
	buf = omap_ep_setup(name "-bulk", addr, \
			USB_ENDPOINT_XFER_BULK, buf, 64, 1);
#define OMAP_INT_EP(name,addr, maxp) \
	buf = omap_ep_setup(name "-int", addr, \
			USB_ENDPOINT_XFER_INT, buf, maxp, 0);
#define OMAP_ISO_EP(name,addr, maxp) \
	buf = omap_ep_setup(name "-iso", addr, \
			USB_ENDPOINT_XFER_ISOC, buf, maxp, 1);

	switch (fifo_mode) {
	case 0:
		OMAP_BULK_EP("ep1in",  USB_DIR_IN  | 1);
		OMAP_BULK_EP("ep2out", USB_DIR_OUT | 2);
		OMAP_INT_EP("ep3in",   USB_DIR_IN  | 3, 16);
		break;
	case 1:
		OMAP_BULK_EP("ep1in",  USB_DIR_IN  | 1);
		OMAP_BULK_EP("ep2out", USB_DIR_OUT | 2);
2723 2724
		OMAP_INT_EP("ep9in",   USB_DIR_IN  | 9, 16);

L
Linus Torvalds 已提交
2725 2726
		OMAP_BULK_EP("ep3in",  USB_DIR_IN  | 3);
		OMAP_BULK_EP("ep4out", USB_DIR_OUT | 4);
2727
		OMAP_INT_EP("ep10in",  USB_DIR_IN  | 10, 16);
L
Linus Torvalds 已提交
2728 2729 2730

		OMAP_BULK_EP("ep5in",  USB_DIR_IN  | 5);
		OMAP_BULK_EP("ep5out", USB_DIR_OUT | 5);
2731 2732
		OMAP_INT_EP("ep11in",  USB_DIR_IN  | 11, 16);

L
Linus Torvalds 已提交
2733 2734
		OMAP_BULK_EP("ep6in",  USB_DIR_IN  | 6);
		OMAP_BULK_EP("ep6out", USB_DIR_OUT | 6);
2735
		OMAP_INT_EP("ep12in",  USB_DIR_IN  | 12, 16);
L
Linus Torvalds 已提交
2736 2737 2738

		OMAP_BULK_EP("ep7in",  USB_DIR_IN  | 7);
		OMAP_BULK_EP("ep7out", USB_DIR_OUT | 7);
2739 2740 2741
		OMAP_INT_EP("ep13in",  USB_DIR_IN  | 13, 16);
		OMAP_INT_EP("ep13out", USB_DIR_OUT | 13, 16);

L
Linus Torvalds 已提交
2742 2743
		OMAP_BULK_EP("ep8in",  USB_DIR_IN  | 8);
		OMAP_BULK_EP("ep8out", USB_DIR_OUT | 8);
2744 2745 2746 2747 2748
		OMAP_INT_EP("ep14in",  USB_DIR_IN  | 14, 16);
		OMAP_INT_EP("ep14out", USB_DIR_OUT | 14, 16);

		OMAP_BULK_EP("ep15in",  USB_DIR_IN  | 15);
		OMAP_BULK_EP("ep15out", USB_DIR_OUT | 15);
L
Linus Torvalds 已提交
2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785

		break;

#ifdef	USE_ISO
	case 2:			/* mixed iso/bulk */
		OMAP_ISO_EP("ep1in",   USB_DIR_IN  | 1, 256);
		OMAP_ISO_EP("ep2out",  USB_DIR_OUT | 2, 256);
		OMAP_ISO_EP("ep3in",   USB_DIR_IN  | 3, 128);
		OMAP_ISO_EP("ep4out",  USB_DIR_OUT | 4, 128);

		OMAP_INT_EP("ep5in",   USB_DIR_IN  | 5, 16);

		OMAP_BULK_EP("ep6in",  USB_DIR_IN  | 6);
		OMAP_BULK_EP("ep7out", USB_DIR_OUT | 7);
		OMAP_INT_EP("ep8in",   USB_DIR_IN  | 8, 16);
		break;
	case 3:			/* mixed bulk/iso */
		OMAP_BULK_EP("ep1in",  USB_DIR_IN  | 1);
		OMAP_BULK_EP("ep2out", USB_DIR_OUT | 2);
		OMAP_INT_EP("ep3in",   USB_DIR_IN  | 3, 16);

		OMAP_BULK_EP("ep4in",  USB_DIR_IN  | 4);
		OMAP_BULK_EP("ep5out", USB_DIR_OUT | 5);
		OMAP_INT_EP("ep6in",   USB_DIR_IN  | 6, 16);

		OMAP_ISO_EP("ep7in",   USB_DIR_IN  | 7, 256);
		OMAP_ISO_EP("ep8out",  USB_DIR_OUT | 8, 256);
		OMAP_INT_EP("ep9in",   USB_DIR_IN  | 9, 16);
		break;
#endif

	/* add more modes as needed */

	default:
		ERR("unsupported fifo_mode #%d\n", fifo_mode);
		return -ENODEV;
	}
2786
	omap_writew(UDC_CFG_LOCK|UDC_SELF_PWR, UDC_SYSCON1);
L
Linus Torvalds 已提交
2787 2788 2789 2790
	INFO("fifo mode %d, %d bytes not used\n", fifo_mode, 2048 - buf);
	return 0;
}

2791
static int __init omap_udc_probe(struct platform_device *pdev)
L
Linus Torvalds 已提交
2792 2793 2794
{
	int			status = -ENODEV;
	int			hmc;
2795
	struct usb_phy		*xceiv = NULL;
2796
	const char		*type = NULL;
2797
	struct omap_usb_config	*config = pdev->dev.platform_data;
2798 2799
	struct clk		*dc_clk;
	struct clk		*hhc_clk;
L
Linus Torvalds 已提交
2800 2801

	/* NOTE:  "knows" the order of the resources! */
2802
	if (!request_mem_region(pdev->resource[0].start,
2803
			pdev->resource[0].end - pdev->resource[0].start + 1,
L
Linus Torvalds 已提交
2804 2805 2806 2807 2808
			driver_name)) {
		DBG("request_mem_region failed\n");
		return -EBUSY;
	}

2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828
	if (cpu_is_omap16xx()) {
		dc_clk = clk_get(&pdev->dev, "usb_dc_ck");
		hhc_clk = clk_get(&pdev->dev, "usb_hhc_ck");
		BUG_ON(IS_ERR(dc_clk) || IS_ERR(hhc_clk));
		/* can't use omap_udc_enable_clock yet */
		clk_enable(dc_clk);
		clk_enable(hhc_clk);
		udelay(100);
	}

	if (cpu_is_omap24xx()) {
		dc_clk = clk_get(&pdev->dev, "usb_fck");
		hhc_clk = clk_get(&pdev->dev, "usb_l4_ick");
		BUG_ON(IS_ERR(dc_clk) || IS_ERR(hhc_clk));
		/* can't use omap_udc_enable_clock yet */
		clk_enable(dc_clk);
		clk_enable(hhc_clk);
		udelay(100);
	}

2829 2830 2831 2832 2833 2834 2835 2836 2837 2838
	if (cpu_is_omap7xx()) {
		dc_clk = clk_get(&pdev->dev, "usb_dc_ck");
		hhc_clk = clk_get(&pdev->dev, "l3_ocpi_ck");
		BUG_ON(IS_ERR(dc_clk) || IS_ERR(hhc_clk));
		/* can't use omap_udc_enable_clock yet */
		clk_enable(dc_clk);
		clk_enable(hhc_clk);
		udelay(100);
	}

L
Linus Torvalds 已提交
2839
	INFO("OMAP UDC rev %d.%d%s\n",
2840
		omap_readw(UDC_REV) >> 4, omap_readw(UDC_REV) & 0xf,
L
Linus Torvalds 已提交
2841 2842 2843 2844 2845 2846 2847
		config->otg ? ", Mini-AB" : "");

	/* use the mode given to us by board init code */
	if (cpu_is_omap15xx()) {
		hmc = HMC_1510;
		type = "(unknown)";

2848
		if (machine_without_vbus_sense()) {
L
Linus Torvalds 已提交
2849 2850 2851 2852 2853 2854
			/* just set up software VBUS detect, and then
			 * later rig it so we always report VBUS.
			 * FIXME without really sensing VBUS, we can't
			 * know when to turn PULLUP_EN on/off; and that
			 * means we always "need" the 48MHz clock.
			 */
2855 2856 2857
			u32 tmp = omap_readl(FUNC_MUX_CTRL_0);
			tmp &= ~VBUS_CTRL_1510;
			omap_writel(tmp, FUNC_MUX_CTRL_0);
L
Linus Torvalds 已提交
2858 2859
			tmp |= VBUS_MODE_1510;
			tmp &= ~VBUS_CTRL_1510;
2860
			omap_writel(tmp, FUNC_MUX_CTRL_0);
L
Linus Torvalds 已提交
2861 2862
		}
	} else {
2863 2864 2865 2866 2867
		/* The transceiver may package some GPIO logic or handle
		 * loopback and/or transceiverless setup; if we find one,
		 * use it.  Except for OTG, we don't _need_ to talk to one;
		 * but not having one probably means no VBUS detection.
		 */
2868
		xceiv = usb_get_phy(USB_PHY_TYPE_USB2);
2869 2870 2871 2872 2873 2874 2875
		if (xceiv)
			type = xceiv->label;
		else if (config->otg) {
			DBG("OTG requires external transceiver!\n");
			goto cleanup0;
		}

L
Linus Torvalds 已提交
2876
		hmc = HMC_1610;
2877 2878 2879 2880 2881 2882 2883 2884 2885

		if (cpu_is_omap24xx()) {
			/* this could be transceiverless in one of the
			 * "we don't need to know" modes.
			 */
			type = "external";
			goto known;
		}

L
Linus Torvalds 已提交
2886
		switch (hmc) {
2887 2888 2889 2890 2891 2892 2893 2894 2895
		case 0:			/* POWERUP DEFAULT == 0 */
		case 4:
		case 12:
		case 20:
			if (!cpu_is_omap1710()) {
				type = "integrated";
				break;
			}
			/* FALL THROUGH */
L
Linus Torvalds 已提交
2896 2897 2898 2899 2900 2901 2902
		case 3:
		case 11:
		case 16:
		case 19:
		case 25:
			if (!xceiv) {
				DBG("external transceiver not registered!\n");
2903
				type = "unknown";
2904
			}
L
Linus Torvalds 已提交
2905 2906
			break;
		case 21:			/* internal loopback */
2907
			type = "loopback";
L
Linus Torvalds 已提交
2908 2909
			break;
		case 14:			/* transceiverless */
2910 2911 2912 2913 2914
			if (cpu_is_omap1710())
				goto bad_on_1710;
			/* FALL THROUGH */
		case 13:
		case 15:
2915
			type = "no";
L
Linus Torvalds 已提交
2916 2917 2918
			break;

		default:
2919
bad_on_1710:
L
Linus Torvalds 已提交
2920
			ERR("unrecognized UDC HMC mode %d\n", hmc);
2921
			goto cleanup0;
L
Linus Torvalds 已提交
2922 2923
		}
	}
2924
known:
2925
	INFO("hmc mode %d, %s transceiver\n", hmc, type);
L
Linus Torvalds 已提交
2926 2927

	/* a "gadget" abstracts/virtualizes the controller */
2928
	status = omap_udc_setup(pdev, xceiv);
L
Linus Torvalds 已提交
2929 2930 2931
	if (status) {
		goto cleanup0;
	}
2932
	xceiv = NULL;
L
Linus Torvalds 已提交
2933 2934 2935 2936 2937 2938
	// "udc" is now valid
	pullup_disable(udc);
#if	defined(CONFIG_USB_OHCI_HCD) || defined(CONFIG_USB_OHCI_HCD_MODULE)
	udc->gadget.is_otg = (config->otg != 0);
#endif

2939
	/* starting with omap1710 es2.0, clear toggle is a separate bit */
2940
	if (omap_readw(UDC_REV) >= 0x61)
2941 2942 2943 2944
		udc->clr_halt = UDC_RESET_EP | UDC_CLRDATA_TOGGLE;
	else
		udc->clr_halt = UDC_RESET_EP;

L
Linus Torvalds 已提交
2945
	/* USB general purpose IRQ:  ep0, state changes, dma, etc */
2946
	status = request_irq(pdev->resource[1].start, omap_udc_irq,
2947
			IRQF_SAMPLE_RANDOM, driver_name, udc);
L
Linus Torvalds 已提交
2948
	if (status != 0) {
2949 2950
		ERR("can't get irq %d, err %d\n",
			(int) pdev->resource[1].start, status);
L
Linus Torvalds 已提交
2951 2952 2953 2954
		goto cleanup1;
	}

	/* USB "non-iso" IRQ (PIO for all but ep0) */
2955
	status = request_irq(pdev->resource[2].start, omap_udc_pio_irq,
2956
			IRQF_SAMPLE_RANDOM, "omap_udc pio", udc);
L
Linus Torvalds 已提交
2957
	if (status != 0) {
2958 2959
		ERR("can't get irq %d, err %d\n",
			(int) pdev->resource[2].start, status);
L
Linus Torvalds 已提交
2960 2961 2962
		goto cleanup2;
	}
#ifdef	USE_ISO
2963
	status = request_irq(pdev->resource[3].start, omap_udc_iso_irq,
Y
Yong Zhang 已提交
2964
			0, "omap_udc iso", udc);
L
Linus Torvalds 已提交
2965
	if (status != 0) {
2966 2967
		ERR("can't get irq %d, err %d\n",
			(int) pdev->resource[3].start, status);
L
Linus Torvalds 已提交
2968 2969 2970
		goto cleanup3;
	}
#endif
2971
	if (cpu_is_omap16xx() || cpu_is_omap7xx()) {
2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986
		udc->dc_clk = dc_clk;
		udc->hhc_clk = hhc_clk;
		clk_disable(hhc_clk);
		clk_disable(dc_clk);
	}

	if (cpu_is_omap24xx()) {
		udc->dc_clk = dc_clk;
		udc->hhc_clk = hhc_clk;
		/* FIXME OMAP2 don't release hhc & dc clock */
#if 0
		clk_disable(hhc_clk);
		clk_disable(dc_clk);
#endif
	}
L
Linus Torvalds 已提交
2987 2988

	create_proc_file();
2989
	status = device_add(&udc->gadget.dev);
2990 2991 2992 2993
	if (status)
		goto cleanup4;

	status = usb_add_gadget_udc(&pdev->dev, &udc->gadget);
2994 2995 2996
	if (!status)
		return status;
	/* If fail, fall through */
2997 2998 2999
cleanup4:
	remove_proc_file();

L
Linus Torvalds 已提交
3000 3001
#ifdef	USE_ISO
cleanup3:
3002
	free_irq(pdev->resource[2].start, udc);
L
Linus Torvalds 已提交
3003 3004 3005
#endif

cleanup2:
3006
	free_irq(pdev->resource[1].start, udc);
L
Linus Torvalds 已提交
3007 3008 3009

cleanup1:
	kfree (udc);
3010
	udc = NULL;
L
Linus Torvalds 已提交
3011 3012 3013

cleanup0:
	if (xceiv)
3014
		usb_put_phy(xceiv);
3015

3016
	if (cpu_is_omap16xx() || cpu_is_omap24xx() || cpu_is_omap7xx()) {
3017 3018 3019 3020 3021 3022
		clk_disable(hhc_clk);
		clk_disable(dc_clk);
		clk_put(hhc_clk);
		clk_put(dc_clk);
	}

3023 3024
	release_mem_region(pdev->resource[0].start,
			pdev->resource[0].end - pdev->resource[0].start + 1);
3025

L
Linus Torvalds 已提交
3026 3027 3028
	return status;
}

3029
static int __exit omap_udc_remove(struct platform_device *pdev)
L
Linus Torvalds 已提交
3030
{
3031
	DECLARE_COMPLETION_ONSTACK(done);
L
Linus Torvalds 已提交
3032 3033 3034

	if (!udc)
		return -ENODEV;
3035 3036

	usb_del_gadget_udc(&udc->gadget);
3037 3038
	if (udc->driver)
		return -EBUSY;
L
Linus Torvalds 已提交
3039 3040 3041 3042 3043

	udc->done = &done;

	pullup_disable(udc);
	if (udc->transceiver) {
3044
		usb_put_phy(udc->transceiver);
3045
		udc->transceiver = NULL;
L
Linus Torvalds 已提交
3046
	}
3047
	omap_writew(0, UDC_SYSCON1);
L
Linus Torvalds 已提交
3048 3049 3050 3051

	remove_proc_file();

#ifdef	USE_ISO
3052
	free_irq(pdev->resource[3].start, udc);
L
Linus Torvalds 已提交
3053
#endif
3054 3055
	free_irq(pdev->resource[2].start, udc);
	free_irq(pdev->resource[1].start, udc);
L
Linus Torvalds 已提交
3056

3057 3058 3059 3060 3061 3062 3063
	if (udc->dc_clk) {
		if (udc->clk_requested)
			omap_udc_enable_clock(0);
		clk_put(udc->hhc_clk);
		clk_put(udc->dc_clk);
	}

3064 3065
	release_mem_region(pdev->resource[0].start,
			pdev->resource[0].end - pdev->resource[0].start + 1);
L
Linus Torvalds 已提交
3066 3067 3068 3069 3070 3071 3072

	device_unregister(&udc->gadget.dev);
	wait_for_completion(&done);

	return 0;
}

3073 3074 3075 3076 3077
/* suspend/resume/wakeup from sysfs (echo > power/state) or when the
 * system is forced into deep sleep
 *
 * REVISIT we should probably reject suspend requests when there's a host
 * session active, rather than disconnecting, at least on boards that can
3078
 * report VBUS irqs (UDC_DEVSTAT.UDC_ATT).  And in any case, we need to
3079 3080 3081
 * make host resumes and VBUS detection trigger OMAP wakeup events; that
 * may involve talking to an external transceiver (e.g. isp1301).
 */
3082

3083
static int omap_udc_suspend(struct platform_device *dev, pm_message_t message)
L
Linus Torvalds 已提交
3084
{
3085 3086
	u32	devstat;

3087
	devstat = omap_readw(UDC_DEVSTAT);
3088 3089 3090 3091 3092 3093

	/* we're requesting 48 MHz clock if the pullup is enabled
	 * (== we're attached to the host) and we're not suspended,
	 * which would prevent entry to deep sleep...
	 */
	if ((devstat & UDC_ATT) != 0 && (devstat & UDC_SUS) == 0) {
3094
		WARNING("session active; suspend requires disconnect\n");
3095 3096
		omap_pullup(&udc->gadget, 0);
	}
L
Linus Torvalds 已提交
3097 3098 3099 3100

	return 0;
}

3101
static int omap_udc_resume(struct platform_device *dev)
L
Linus Torvalds 已提交
3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112
{
	DBG("resume + wakeup/SRP\n");
	omap_pullup(&udc->gadget, 1);

	/* maybe the host would enumerate us if we nudged it */
	msleep(100);
	return omap_wakeup(&udc->gadget);
}

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

3113
static struct platform_driver udc_driver = {
L
Linus Torvalds 已提交
3114 3115 3116
	.remove		= __exit_p(omap_udc_remove),
	.suspend	= omap_udc_suspend,
	.resume		= omap_udc_resume,
3117 3118 3119 3120
	.driver		= {
		.owner	= THIS_MODULE,
		.name	= (char *) driver_name,
	},
L
Linus Torvalds 已提交
3121 3122 3123 3124
};

static int __init udc_init(void)
{
3125 3126 3127 3128
	/* Disable DMA for omap7xx -- it doesn't work right. */
	if (cpu_is_omap7xx())
		use_dma = 0;

L
Linus Torvalds 已提交
3129 3130 3131 3132 3133 3134
	INFO("%s, version: " DRIVER_VERSION
#ifdef	USE_ISO
		" (iso)"
#endif
		"%s\n", driver_desc,
		use_dma ?  " (dma)" : "");
3135
	return platform_driver_probe(&udc_driver, omap_udc_probe);
L
Linus Torvalds 已提交
3136 3137 3138 3139 3140
}
module_init(udc_init);

static void __exit udc_exit(void)
{
3141
	platform_driver_unregister(&udc_driver);
L
Linus Torvalds 已提交
3142 3143 3144 3145 3146
}
module_exit(udc_exit);

MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_LICENSE("GPL");
3147
MODULE_ALIAS("platform:omap_udc");