xref: /openbmc/linux/drivers/usb/gadget/udc/omap_udc.c (revision bacf743e)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * omap_udc.c -- for OMAP full speed udc; most chips support OTG.
4  *
5  * Copyright (C) 2004 Texas Instruments, Inc.
6  * Copyright (C) 2004-2005 David Brownell
7  *
8  * OMAP2 & DMA support by Kyungmin Park <kyungmin.park@samsung.com>
9  */
10 
11 #undef	DEBUG
12 #undef	VERBOSE
13 
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/ioport.h>
17 #include <linux/types.h>
18 #include <linux/errno.h>
19 #include <linux/delay.h>
20 #include <linux/slab.h>
21 #include <linux/timer.h>
22 #include <linux/list.h>
23 #include <linux/interrupt.h>
24 #include <linux/proc_fs.h>
25 #include <linux/mm.h>
26 #include <linux/moduleparam.h>
27 #include <linux/platform_device.h>
28 #include <linux/usb/ch9.h>
29 #include <linux/usb/gadget.h>
30 #include <linux/usb/otg.h>
31 #include <linux/dma-mapping.h>
32 #include <linux/clk.h>
33 #include <linux/err.h>
34 #include <linux/prefetch.h>
35 #include <linux/io.h>
36 
37 #include <asm/byteorder.h>
38 #include <asm/irq.h>
39 #include <asm/unaligned.h>
40 #include <asm/mach-types.h>
41 
42 #include <linux/omap-dma.h>
43 
44 #include <mach/usb.h>
45 
46 #include "omap_udc.h"
47 
48 #undef	USB_TRACE
49 
50 /* bulk DMA seems to be behaving for both IN and OUT */
51 #define	USE_DMA
52 
53 /* ISO too */
54 #define	USE_ISO
55 
56 #define	DRIVER_DESC	"OMAP UDC driver"
57 #define	DRIVER_VERSION	"4 October 2004"
58 
59 #define OMAP_DMA_USB_W2FC_TX0		29
60 #define OMAP_DMA_USB_W2FC_RX0		26
61 
62 /*
63  * The OMAP UDC needs _very_ early endpoint setup:  before enabling the
64  * D+ pullup to allow enumeration.  That's too early for the gadget
65  * framework to use from usb_endpoint_enable(), which happens after
66  * enumeration as part of activating an interface.  (But if we add an
67  * optional new "UDC not yet running" state to the gadget driver model,
68  * even just during driver binding, the endpoint autoconfig logic is the
69  * natural spot to manufacture new endpoints.)
70  *
71  * So instead of using endpoint enable calls to control the hardware setup,
72  * this driver defines a "fifo mode" parameter.  It's used during driver
73  * initialization to choose among a set of pre-defined endpoint configs.
74  * See omap_udc_setup() for available modes, or to add others.  That code
75  * lives in an init section, so use this driver as a module if you need
76  * to change the fifo mode after the kernel boots.
77  *
78  * Gadget drivers normally ignore endpoints they don't care about, and
79  * won't include them in configuration descriptors.  That means only
80  * misbehaving hosts would even notice they exist.
81  */
82 #ifdef	USE_ISO
83 static unsigned fifo_mode = 3;
84 #else
85 static unsigned fifo_mode;
86 #endif
87 
88 /* "modprobe omap_udc fifo_mode=42", or else as a kernel
89  * boot parameter "omap_udc:fifo_mode=42"
90  */
91 module_param(fifo_mode, uint, 0);
92 MODULE_PARM_DESC(fifo_mode, "endpoint configuration");
93 
94 #ifdef	USE_DMA
95 static bool use_dma = 1;
96 
97 /* "modprobe omap_udc use_dma=y", or else as a kernel
98  * boot parameter "omap_udc:use_dma=y"
99  */
100 module_param(use_dma, bool, 0);
101 MODULE_PARM_DESC(use_dma, "enable/disable DMA");
102 #else	/* !USE_DMA */
103 
104 /* save a bit of code */
105 #define	use_dma		0
106 #endif	/* !USE_DMA */
107 
108 
109 static const char driver_name[] = "omap_udc";
110 static const char driver_desc[] = DRIVER_DESC;
111 
112 /*-------------------------------------------------------------------------*/
113 
114 /* there's a notion of "current endpoint" for modifying endpoint
115  * state, and PIO access to its FIFO.
116  */
117 
118 static void use_ep(struct omap_ep *ep, u16 select)
119 {
120 	u16	num = ep->bEndpointAddress & 0x0f;
121 
122 	if (ep->bEndpointAddress & USB_DIR_IN)
123 		num |= UDC_EP_DIR;
124 	omap_writew(num | select, UDC_EP_NUM);
125 	/* when select, MUST deselect later !! */
126 }
127 
128 static inline void deselect_ep(void)
129 {
130 	u16 w;
131 
132 	w = omap_readw(UDC_EP_NUM);
133 	w &= ~UDC_EP_SEL;
134 	omap_writew(w, UDC_EP_NUM);
135 	/* 6 wait states before TX will happen */
136 }
137 
138 static void dma_channel_claim(struct omap_ep *ep, unsigned preferred);
139 
140 /*-------------------------------------------------------------------------*/
141 
142 static int omap_ep_enable(struct usb_ep *_ep,
143 		const struct usb_endpoint_descriptor *desc)
144 {
145 	struct omap_ep	*ep = container_of(_ep, struct omap_ep, ep);
146 	struct omap_udc	*udc;
147 	unsigned long	flags;
148 	u16		maxp;
149 
150 	/* catch various bogus parameters */
151 	if (!_ep || !desc
152 			|| desc->bDescriptorType != USB_DT_ENDPOINT
153 			|| ep->bEndpointAddress != desc->bEndpointAddress
154 			|| ep->maxpacket < usb_endpoint_maxp(desc)) {
155 		DBG("%s, bad ep or descriptor\n", __func__);
156 		return -EINVAL;
157 	}
158 	maxp = usb_endpoint_maxp(desc);
159 	if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
160 				&& maxp != ep->maxpacket)
161 			|| usb_endpoint_maxp(desc) > ep->maxpacket
162 			|| !desc->wMaxPacketSize) {
163 		DBG("%s, bad %s maxpacket\n", __func__, _ep->name);
164 		return -ERANGE;
165 	}
166 
167 #ifdef	USE_ISO
168 	if ((desc->bmAttributes == USB_ENDPOINT_XFER_ISOC
169 				&& desc->bInterval != 1)) {
170 		/* hardware wants period = 1; USB allows 2^(Interval-1) */
171 		DBG("%s, unsupported ISO period %dms\n", _ep->name,
172 				1 << (desc->bInterval - 1));
173 		return -EDOM;
174 	}
175 #else
176 	if (desc->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
177 		DBG("%s, ISO nyet\n", _ep->name);
178 		return -EDOM;
179 	}
180 #endif
181 
182 	/* xfer types must match, except that interrupt ~= bulk */
183 	if (ep->bmAttributes != desc->bmAttributes
184 			&& ep->bmAttributes != USB_ENDPOINT_XFER_BULK
185 			&& desc->bmAttributes != USB_ENDPOINT_XFER_INT) {
186 		DBG("%s, %s type mismatch\n", __func__, _ep->name);
187 		return -EINVAL;
188 	}
189 
190 	udc = ep->udc;
191 	if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN) {
192 		DBG("%s, bogus device state\n", __func__);
193 		return -ESHUTDOWN;
194 	}
195 
196 	spin_lock_irqsave(&udc->lock, flags);
197 
198 	ep->ep.desc = desc;
199 	ep->irqs = 0;
200 	ep->stopped = 0;
201 	ep->ep.maxpacket = maxp;
202 
203 	/* set endpoint to initial state */
204 	ep->dma_channel = 0;
205 	ep->has_dma = 0;
206 	ep->lch = -1;
207 	use_ep(ep, UDC_EP_SEL);
208 	omap_writew(udc->clr_halt, UDC_CTRL);
209 	ep->ackwait = 0;
210 	deselect_ep();
211 
212 	if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC)
213 		list_add(&ep->iso, &udc->iso);
214 
215 	/* maybe assign a DMA channel to this endpoint */
216 	if (use_dma && desc->bmAttributes == USB_ENDPOINT_XFER_BULK)
217 		/* FIXME ISO can dma, but prefers first channel */
218 		dma_channel_claim(ep, 0);
219 
220 	/* PIO OUT may RX packets */
221 	if (desc->bmAttributes != USB_ENDPOINT_XFER_ISOC
222 			&& !ep->has_dma
223 			&& !(ep->bEndpointAddress & USB_DIR_IN)) {
224 		omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
225 		ep->ackwait = 1 + ep->double_buf;
226 	}
227 
228 	spin_unlock_irqrestore(&udc->lock, flags);
229 	VDBG("%s enabled\n", _ep->name);
230 	return 0;
231 }
232 
233 static void nuke(struct omap_ep *, int status);
234 
235 static int omap_ep_disable(struct usb_ep *_ep)
236 {
237 	struct omap_ep	*ep = container_of(_ep, struct omap_ep, ep);
238 	unsigned long	flags;
239 
240 	if (!_ep || !ep->ep.desc) {
241 		DBG("%s, %s not enabled\n", __func__,
242 			_ep ? ep->ep.name : NULL);
243 		return -EINVAL;
244 	}
245 
246 	spin_lock_irqsave(&ep->udc->lock, flags);
247 	ep->ep.desc = NULL;
248 	nuke(ep, -ESHUTDOWN);
249 	ep->ep.maxpacket = ep->maxpacket;
250 	ep->has_dma = 0;
251 	omap_writew(UDC_SET_HALT, UDC_CTRL);
252 	list_del_init(&ep->iso);
253 	del_timer(&ep->timer);
254 
255 	spin_unlock_irqrestore(&ep->udc->lock, flags);
256 
257 	VDBG("%s disabled\n", _ep->name);
258 	return 0;
259 }
260 
261 /*-------------------------------------------------------------------------*/
262 
263 static struct usb_request *
264 omap_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
265 {
266 	struct omap_req	*req;
267 
268 	req = kzalloc(sizeof(*req), gfp_flags);
269 	if (!req)
270 		return NULL;
271 
272 	INIT_LIST_HEAD(&req->queue);
273 
274 	return &req->req;
275 }
276 
277 static void
278 omap_free_request(struct usb_ep *ep, struct usb_request *_req)
279 {
280 	struct omap_req	*req = container_of(_req, struct omap_req, req);
281 
282 	kfree(req);
283 }
284 
285 /*-------------------------------------------------------------------------*/
286 
287 static void
288 done(struct omap_ep *ep, struct omap_req *req, int status)
289 {
290 	struct omap_udc		*udc = ep->udc;
291 	unsigned		stopped = ep->stopped;
292 
293 	list_del_init(&req->queue);
294 
295 	if (req->req.status == -EINPROGRESS)
296 		req->req.status = status;
297 	else
298 		status = req->req.status;
299 
300 	if (use_dma && ep->has_dma)
301 		usb_gadget_unmap_request(&udc->gadget, &req->req,
302 				(ep->bEndpointAddress & USB_DIR_IN));
303 
304 #ifndef	USB_TRACE
305 	if (status && status != -ESHUTDOWN)
306 #endif
307 		VDBG("complete %s req %p stat %d len %u/%u\n",
308 			ep->ep.name, &req->req, status,
309 			req->req.actual, req->req.length);
310 
311 	/* don't modify queue heads during completion callback */
312 	ep->stopped = 1;
313 	spin_unlock(&ep->udc->lock);
314 	usb_gadget_giveback_request(&ep->ep, &req->req);
315 	spin_lock(&ep->udc->lock);
316 	ep->stopped = stopped;
317 }
318 
319 /*-------------------------------------------------------------------------*/
320 
321 #define UDC_FIFO_FULL		(UDC_NON_ISO_FIFO_FULL | UDC_ISO_FIFO_FULL)
322 #define UDC_FIFO_UNWRITABLE	(UDC_EP_HALTED | UDC_FIFO_FULL)
323 
324 #define FIFO_EMPTY	(UDC_NON_ISO_FIFO_EMPTY | UDC_ISO_FIFO_EMPTY)
325 #define FIFO_UNREADABLE (UDC_EP_HALTED | FIFO_EMPTY)
326 
327 static inline int
328 write_packet(u8 *buf, struct omap_req *req, unsigned max)
329 {
330 	unsigned	len;
331 	u16		*wp;
332 
333 	len = min(req->req.length - req->req.actual, max);
334 	req->req.actual += len;
335 
336 	max = len;
337 	if (likely((((int)buf) & 1) == 0)) {
338 		wp = (u16 *)buf;
339 		while (max >= 2) {
340 			omap_writew(*wp++, UDC_DATA);
341 			max -= 2;
342 		}
343 		buf = (u8 *)wp;
344 	}
345 	while (max--)
346 		omap_writeb(*buf++, UDC_DATA);
347 	return len;
348 }
349 
350 /* FIXME change r/w fifo calling convention */
351 
352 
353 /* return:  0 = still running, 1 = completed, negative = errno */
354 static int write_fifo(struct omap_ep *ep, struct omap_req *req)
355 {
356 	u8		*buf;
357 	unsigned	count;
358 	int		is_last;
359 	u16		ep_stat;
360 
361 	buf = req->req.buf + req->req.actual;
362 	prefetch(buf);
363 
364 	/* PIO-IN isn't double buffered except for iso */
365 	ep_stat = omap_readw(UDC_STAT_FLG);
366 	if (ep_stat & UDC_FIFO_UNWRITABLE)
367 		return 0;
368 
369 	count = ep->ep.maxpacket;
370 	count = write_packet(buf, req, count);
371 	omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
372 	ep->ackwait = 1;
373 
374 	/* last packet is often short (sometimes a zlp) */
375 	if (count != ep->ep.maxpacket)
376 		is_last = 1;
377 	else if (req->req.length == req->req.actual
378 			&& !req->req.zero)
379 		is_last = 1;
380 	else
381 		is_last = 0;
382 
383 	/* NOTE:  requests complete when all IN data is in a
384 	 * FIFO (or sometimes later, if a zlp was needed).
385 	 * Use usb_ep_fifo_status() where needed.
386 	 */
387 	if (is_last)
388 		done(ep, req, 0);
389 	return is_last;
390 }
391 
392 static inline int
393 read_packet(u8 *buf, struct omap_req *req, unsigned avail)
394 {
395 	unsigned	len;
396 	u16		*wp;
397 
398 	len = min(req->req.length - req->req.actual, avail);
399 	req->req.actual += len;
400 	avail = len;
401 
402 	if (likely((((int)buf) & 1) == 0)) {
403 		wp = (u16 *)buf;
404 		while (avail >= 2) {
405 			*wp++ = omap_readw(UDC_DATA);
406 			avail -= 2;
407 		}
408 		buf = (u8 *)wp;
409 	}
410 	while (avail--)
411 		*buf++ = omap_readb(UDC_DATA);
412 	return len;
413 }
414 
415 /* return:  0 = still running, 1 = queue empty, negative = errno */
416 static int read_fifo(struct omap_ep *ep, struct omap_req *req)
417 {
418 	u8		*buf;
419 	unsigned	count, avail;
420 	int		is_last;
421 
422 	buf = req->req.buf + req->req.actual;
423 	prefetchw(buf);
424 
425 	for (;;) {
426 		u16	ep_stat = omap_readw(UDC_STAT_FLG);
427 
428 		is_last = 0;
429 		if (ep_stat & FIFO_EMPTY) {
430 			if (!ep->double_buf)
431 				break;
432 			ep->fnf = 1;
433 		}
434 		if (ep_stat & UDC_EP_HALTED)
435 			break;
436 
437 		if (ep_stat & UDC_FIFO_FULL)
438 			avail = ep->ep.maxpacket;
439 		else  {
440 			avail = omap_readw(UDC_RXFSTAT);
441 			ep->fnf = ep->double_buf;
442 		}
443 		count = read_packet(buf, req, avail);
444 
445 		/* partial packet reads may not be errors */
446 		if (count < ep->ep.maxpacket) {
447 			is_last = 1;
448 			/* overflowed this request?  flush extra data */
449 			if (count != avail) {
450 				req->req.status = -EOVERFLOW;
451 				avail -= count;
452 				while (avail--)
453 					omap_readw(UDC_DATA);
454 			}
455 		} else if (req->req.length == req->req.actual)
456 			is_last = 1;
457 		else
458 			is_last = 0;
459 
460 		if (!ep->bEndpointAddress)
461 			break;
462 		if (is_last)
463 			done(ep, req, 0);
464 		break;
465 	}
466 	return is_last;
467 }
468 
469 /*-------------------------------------------------------------------------*/
470 
471 static u16 dma_src_len(struct omap_ep *ep, dma_addr_t start)
472 {
473 	dma_addr_t	end;
474 
475 	/* IN-DMA needs this on fault/cancel paths, so 15xx misreports
476 	 * the last transfer's bytecount by more than a FIFO's worth.
477 	 */
478 	if (cpu_is_omap15xx())
479 		return 0;
480 
481 	end = omap_get_dma_src_pos(ep->lch);
482 	if (end == ep->dma_counter)
483 		return 0;
484 
485 	end |= start & (0xffff << 16);
486 	if (end < start)
487 		end += 0x10000;
488 	return end - start;
489 }
490 
491 static u16 dma_dest_len(struct omap_ep *ep, dma_addr_t start)
492 {
493 	dma_addr_t	end;
494 
495 	end = omap_get_dma_dst_pos(ep->lch);
496 	if (end == ep->dma_counter)
497 		return 0;
498 
499 	end |= start & (0xffff << 16);
500 	if (cpu_is_omap15xx())
501 		end++;
502 	if (end < start)
503 		end += 0x10000;
504 	return end - start;
505 }
506 
507 
508 /* Each USB transfer request using DMA maps to one or more DMA transfers.
509  * When DMA completion isn't request completion, the UDC continues with
510  * the next DMA transfer for that USB transfer.
511  */
512 
513 static void next_in_dma(struct omap_ep *ep, struct omap_req *req)
514 {
515 	u16		txdma_ctrl, w;
516 	unsigned	length = req->req.length - req->req.actual;
517 	const int	sync_mode = cpu_is_omap15xx()
518 				? OMAP_DMA_SYNC_FRAME
519 				: OMAP_DMA_SYNC_ELEMENT;
520 	int		dma_trigger = 0;
521 
522 	/* measure length in either bytes or packets */
523 	if ((cpu_is_omap16xx() && length <= UDC_TXN_TSC)
524 			|| (cpu_is_omap15xx() && length < ep->maxpacket)) {
525 		txdma_ctrl = UDC_TXN_EOT | length;
526 		omap_set_dma_transfer_params(ep->lch, OMAP_DMA_DATA_TYPE_S8,
527 				length, 1, sync_mode, dma_trigger, 0);
528 	} else {
529 		length = min(length / ep->maxpacket,
530 				(unsigned) UDC_TXN_TSC + 1);
531 		txdma_ctrl = length;
532 		omap_set_dma_transfer_params(ep->lch, OMAP_DMA_DATA_TYPE_S16,
533 				ep->ep.maxpacket >> 1, length, sync_mode,
534 				dma_trigger, 0);
535 		length *= ep->maxpacket;
536 	}
537 	omap_set_dma_src_params(ep->lch, OMAP_DMA_PORT_EMIFF,
538 		OMAP_DMA_AMODE_POST_INC, req->req.dma + req->req.actual,
539 		0, 0);
540 
541 	omap_start_dma(ep->lch);
542 	ep->dma_counter = omap_get_dma_src_pos(ep->lch);
543 	w = omap_readw(UDC_DMA_IRQ_EN);
544 	w |= UDC_TX_DONE_IE(ep->dma_channel);
545 	omap_writew(w, UDC_DMA_IRQ_EN);
546 	omap_writew(UDC_TXN_START | txdma_ctrl, UDC_TXDMA(ep->dma_channel));
547 	req->dma_bytes = length;
548 }
549 
550 static void finish_in_dma(struct omap_ep *ep, struct omap_req *req, int status)
551 {
552 	u16 w;
553 
554 	if (status == 0) {
555 		req->req.actual += req->dma_bytes;
556 
557 		/* return if this request needs to send data or zlp */
558 		if (req->req.actual < req->req.length)
559 			return;
560 		if (req->req.zero
561 				&& req->dma_bytes != 0
562 				&& (req->req.actual % ep->maxpacket) == 0)
563 			return;
564 	} else
565 		req->req.actual += dma_src_len(ep, req->req.dma
566 							+ req->req.actual);
567 
568 	/* tx completion */
569 	omap_stop_dma(ep->lch);
570 	w = omap_readw(UDC_DMA_IRQ_EN);
571 	w &= ~UDC_TX_DONE_IE(ep->dma_channel);
572 	omap_writew(w, UDC_DMA_IRQ_EN);
573 	done(ep, req, status);
574 }
575 
576 static void next_out_dma(struct omap_ep *ep, struct omap_req *req)
577 {
578 	unsigned packets = req->req.length - req->req.actual;
579 	int dma_trigger = 0;
580 	u16 w;
581 
582 	/* set up this DMA transfer, enable the fifo, start */
583 	packets /= ep->ep.maxpacket;
584 	packets = min(packets, (unsigned)UDC_RXN_TC + 1);
585 	req->dma_bytes = packets * ep->ep.maxpacket;
586 	omap_set_dma_transfer_params(ep->lch, OMAP_DMA_DATA_TYPE_S16,
587 			ep->ep.maxpacket >> 1, packets,
588 			OMAP_DMA_SYNC_ELEMENT,
589 			dma_trigger, 0);
590 	omap_set_dma_dest_params(ep->lch, OMAP_DMA_PORT_EMIFF,
591 		OMAP_DMA_AMODE_POST_INC, req->req.dma + req->req.actual,
592 		0, 0);
593 	ep->dma_counter = omap_get_dma_dst_pos(ep->lch);
594 
595 	omap_writew(UDC_RXN_STOP | (packets - 1), UDC_RXDMA(ep->dma_channel));
596 	w = omap_readw(UDC_DMA_IRQ_EN);
597 	w |= UDC_RX_EOT_IE(ep->dma_channel);
598 	omap_writew(w, UDC_DMA_IRQ_EN);
599 	omap_writew(ep->bEndpointAddress & 0xf, UDC_EP_NUM);
600 	omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
601 
602 	omap_start_dma(ep->lch);
603 }
604 
605 static void
606 finish_out_dma(struct omap_ep *ep, struct omap_req *req, int status, int one)
607 {
608 	u16	count, w;
609 
610 	if (status == 0)
611 		ep->dma_counter = (u16) (req->req.dma + req->req.actual);
612 	count = dma_dest_len(ep, req->req.dma + req->req.actual);
613 	count += req->req.actual;
614 	if (one)
615 		count--;
616 	if (count <= req->req.length)
617 		req->req.actual = count;
618 
619 	if (count != req->dma_bytes || status)
620 		omap_stop_dma(ep->lch);
621 
622 	/* if this wasn't short, request may need another transfer */
623 	else if (req->req.actual < req->req.length)
624 		return;
625 
626 	/* rx completion */
627 	w = omap_readw(UDC_DMA_IRQ_EN);
628 	w &= ~UDC_RX_EOT_IE(ep->dma_channel);
629 	omap_writew(w, UDC_DMA_IRQ_EN);
630 	done(ep, req, status);
631 }
632 
633 static void dma_irq(struct omap_udc *udc, u16 irq_src)
634 {
635 	u16		dman_stat = omap_readw(UDC_DMAN_STAT);
636 	struct omap_ep	*ep;
637 	struct omap_req	*req;
638 
639 	/* IN dma: tx to host */
640 	if (irq_src & UDC_TXN_DONE) {
641 		ep = &udc->ep[16 + UDC_DMA_TX_SRC(dman_stat)];
642 		ep->irqs++;
643 		/* can see TXN_DONE after dma abort */
644 		if (!list_empty(&ep->queue)) {
645 			req = container_of(ep->queue.next,
646 						struct omap_req, queue);
647 			finish_in_dma(ep, req, 0);
648 		}
649 		omap_writew(UDC_TXN_DONE, UDC_IRQ_SRC);
650 
651 		if (!list_empty(&ep->queue)) {
652 			req = container_of(ep->queue.next,
653 					struct omap_req, queue);
654 			next_in_dma(ep, req);
655 		}
656 	}
657 
658 	/* OUT dma: rx from host */
659 	if (irq_src & UDC_RXN_EOT) {
660 		ep = &udc->ep[UDC_DMA_RX_SRC(dman_stat)];
661 		ep->irqs++;
662 		/* can see RXN_EOT after dma abort */
663 		if (!list_empty(&ep->queue)) {
664 			req = container_of(ep->queue.next,
665 					struct omap_req, queue);
666 			finish_out_dma(ep, req, 0, dman_stat & UDC_DMA_RX_SB);
667 		}
668 		omap_writew(UDC_RXN_EOT, UDC_IRQ_SRC);
669 
670 		if (!list_empty(&ep->queue)) {
671 			req = container_of(ep->queue.next,
672 					struct omap_req, queue);
673 			next_out_dma(ep, req);
674 		}
675 	}
676 
677 	if (irq_src & UDC_RXN_CNT) {
678 		ep = &udc->ep[UDC_DMA_RX_SRC(dman_stat)];
679 		ep->irqs++;
680 		/* omap15xx does this unasked... */
681 		VDBG("%s, RX_CNT irq?\n", ep->ep.name);
682 		omap_writew(UDC_RXN_CNT, UDC_IRQ_SRC);
683 	}
684 }
685 
686 static void dma_error(int lch, u16 ch_status, void *data)
687 {
688 	struct omap_ep	*ep = data;
689 
690 	/* if ch_status & OMAP_DMA_DROP_IRQ ... */
691 	/* if ch_status & OMAP1_DMA_TOUT_IRQ ... */
692 	ERR("%s dma error, lch %d status %02x\n", ep->ep.name, lch, ch_status);
693 
694 	/* complete current transfer ... */
695 }
696 
697 static void dma_channel_claim(struct omap_ep *ep, unsigned channel)
698 {
699 	u16	reg;
700 	int	status, restart, is_in;
701 	int	dma_channel;
702 
703 	is_in = ep->bEndpointAddress & USB_DIR_IN;
704 	if (is_in)
705 		reg = omap_readw(UDC_TXDMA_CFG);
706 	else
707 		reg = omap_readw(UDC_RXDMA_CFG);
708 	reg |= UDC_DMA_REQ;		/* "pulse" activated */
709 
710 	ep->dma_channel = 0;
711 	ep->lch = -1;
712 	if (channel == 0 || channel > 3) {
713 		if ((reg & 0x0f00) == 0)
714 			channel = 3;
715 		else if ((reg & 0x00f0) == 0)
716 			channel = 2;
717 		else if ((reg & 0x000f) == 0)	/* preferred for ISO */
718 			channel = 1;
719 		else {
720 			status = -EMLINK;
721 			goto just_restart;
722 		}
723 	}
724 	reg |= (0x0f & ep->bEndpointAddress) << (4 * (channel - 1));
725 	ep->dma_channel = channel;
726 
727 	if (is_in) {
728 		dma_channel = OMAP_DMA_USB_W2FC_TX0 - 1 + channel;
729 		status = omap_request_dma(dma_channel,
730 			ep->ep.name, dma_error, ep, &ep->lch);
731 		if (status == 0) {
732 			omap_writew(reg, UDC_TXDMA_CFG);
733 			/* EMIFF or SDRC */
734 			omap_set_dma_src_burst_mode(ep->lch,
735 						OMAP_DMA_DATA_BURST_4);
736 			omap_set_dma_src_data_pack(ep->lch, 1);
737 			/* TIPB */
738 			omap_set_dma_dest_params(ep->lch,
739 				OMAP_DMA_PORT_TIPB,
740 				OMAP_DMA_AMODE_CONSTANT,
741 				UDC_DATA_DMA,
742 				0, 0);
743 		}
744 	} else {
745 		dma_channel = OMAP_DMA_USB_W2FC_RX0 - 1 + channel;
746 		status = omap_request_dma(dma_channel,
747 			ep->ep.name, dma_error, ep, &ep->lch);
748 		if (status == 0) {
749 			omap_writew(reg, UDC_RXDMA_CFG);
750 			/* TIPB */
751 			omap_set_dma_src_params(ep->lch,
752 				OMAP_DMA_PORT_TIPB,
753 				OMAP_DMA_AMODE_CONSTANT,
754 				UDC_DATA_DMA,
755 				0, 0);
756 			/* EMIFF or SDRC */
757 			omap_set_dma_dest_burst_mode(ep->lch,
758 						OMAP_DMA_DATA_BURST_4);
759 			omap_set_dma_dest_data_pack(ep->lch, 1);
760 		}
761 	}
762 	if (status)
763 		ep->dma_channel = 0;
764 	else {
765 		ep->has_dma = 1;
766 		omap_disable_dma_irq(ep->lch, OMAP_DMA_BLOCK_IRQ);
767 
768 		/* channel type P: hw synch (fifo) */
769 		if (!cpu_is_omap15xx())
770 			omap_set_dma_channel_mode(ep->lch, OMAP_DMA_LCH_P);
771 	}
772 
773 just_restart:
774 	/* restart any queue, even if the claim failed  */
775 	restart = !ep->stopped && !list_empty(&ep->queue);
776 
777 	if (status)
778 		DBG("%s no dma channel: %d%s\n", ep->ep.name, status,
779 			restart ? " (restart)" : "");
780 	else
781 		DBG("%s claimed %cxdma%d lch %d%s\n", ep->ep.name,
782 			is_in ? 't' : 'r',
783 			ep->dma_channel - 1, ep->lch,
784 			restart ? " (restart)" : "");
785 
786 	if (restart) {
787 		struct omap_req	*req;
788 		req = container_of(ep->queue.next, struct omap_req, queue);
789 		if (ep->has_dma)
790 			(is_in ? next_in_dma : next_out_dma)(ep, req);
791 		else {
792 			use_ep(ep, UDC_EP_SEL);
793 			(is_in ? write_fifo : read_fifo)(ep, req);
794 			deselect_ep();
795 			if (!is_in) {
796 				omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
797 				ep->ackwait = 1 + ep->double_buf;
798 			}
799 			/* IN: 6 wait states before it'll tx */
800 		}
801 	}
802 }
803 
804 static void dma_channel_release(struct omap_ep *ep)
805 {
806 	int		shift = 4 * (ep->dma_channel - 1);
807 	u16		mask = 0x0f << shift;
808 	struct omap_req	*req;
809 	int		active;
810 
811 	/* abort any active usb transfer request */
812 	if (!list_empty(&ep->queue))
813 		req = container_of(ep->queue.next, struct omap_req, queue);
814 	else
815 		req = NULL;
816 
817 	active = omap_get_dma_active_status(ep->lch);
818 
819 	DBG("%s release %s %cxdma%d %p\n", ep->ep.name,
820 			active ? "active" : "idle",
821 			(ep->bEndpointAddress & USB_DIR_IN) ? 't' : 'r',
822 			ep->dma_channel - 1, req);
823 
824 	/* NOTE: re-setting RX_REQ/TX_REQ because of a chip bug (before
825 	 * OMAP 1710 ES2.0) where reading the DMA_CFG can clear them.
826 	 */
827 
828 	/* wait till current packet DMA finishes, and fifo empties */
829 	if (ep->bEndpointAddress & USB_DIR_IN) {
830 		omap_writew((omap_readw(UDC_TXDMA_CFG) & ~mask) | UDC_DMA_REQ,
831 					UDC_TXDMA_CFG);
832 
833 		if (req) {
834 			finish_in_dma(ep, req, -ECONNRESET);
835 
836 			/* clear FIFO; hosts probably won't empty it */
837 			use_ep(ep, UDC_EP_SEL);
838 			omap_writew(UDC_CLR_EP, UDC_CTRL);
839 			deselect_ep();
840 		}
841 		while (omap_readw(UDC_TXDMA_CFG) & mask)
842 			udelay(10);
843 	} else {
844 		omap_writew((omap_readw(UDC_RXDMA_CFG) & ~mask) | UDC_DMA_REQ,
845 					UDC_RXDMA_CFG);
846 
847 		/* dma empties the fifo */
848 		while (omap_readw(UDC_RXDMA_CFG) & mask)
849 			udelay(10);
850 		if (req)
851 			finish_out_dma(ep, req, -ECONNRESET, 0);
852 	}
853 	omap_free_dma(ep->lch);
854 	ep->dma_channel = 0;
855 	ep->lch = -1;
856 	/* has_dma still set, till endpoint is fully quiesced */
857 }
858 
859 
860 /*-------------------------------------------------------------------------*/
861 
862 static int
863 omap_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
864 {
865 	struct omap_ep	*ep = container_of(_ep, struct omap_ep, ep);
866 	struct omap_req	*req = container_of(_req, struct omap_req, req);
867 	struct omap_udc	*udc;
868 	unsigned long	flags;
869 	int		is_iso = 0;
870 
871 	/* catch various bogus parameters */
872 	if (!_req || !req->req.complete || !req->req.buf
873 			|| !list_empty(&req->queue)) {
874 		DBG("%s, bad params\n", __func__);
875 		return -EINVAL;
876 	}
877 	if (!_ep || (!ep->ep.desc && ep->bEndpointAddress)) {
878 		DBG("%s, bad ep\n", __func__);
879 		return -EINVAL;
880 	}
881 	if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
882 		if (req->req.length > ep->ep.maxpacket)
883 			return -EMSGSIZE;
884 		is_iso = 1;
885 	}
886 
887 	/* this isn't bogus, but OMAP DMA isn't the only hardware to
888 	 * have a hard time with partial packet reads...  reject it.
889 	 */
890 	if (use_dma
891 			&& ep->has_dma
892 			&& ep->bEndpointAddress != 0
893 			&& (ep->bEndpointAddress & USB_DIR_IN) == 0
894 			&& (req->req.length % ep->ep.maxpacket) != 0) {
895 		DBG("%s, no partial packet OUT reads\n", __func__);
896 		return -EMSGSIZE;
897 	}
898 
899 	udc = ep->udc;
900 	if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN)
901 		return -ESHUTDOWN;
902 
903 	if (use_dma && ep->has_dma)
904 		usb_gadget_map_request(&udc->gadget, &req->req,
905 				(ep->bEndpointAddress & USB_DIR_IN));
906 
907 	VDBG("%s queue req %p, len %d buf %p\n",
908 		ep->ep.name, _req, _req->length, _req->buf);
909 
910 	spin_lock_irqsave(&udc->lock, flags);
911 
912 	req->req.status = -EINPROGRESS;
913 	req->req.actual = 0;
914 
915 	/* maybe kickstart non-iso i/o queues */
916 	if (is_iso) {
917 		u16 w;
918 
919 		w = omap_readw(UDC_IRQ_EN);
920 		w |= UDC_SOF_IE;
921 		omap_writew(w, UDC_IRQ_EN);
922 	} else if (list_empty(&ep->queue) && !ep->stopped && !ep->ackwait) {
923 		int	is_in;
924 
925 		if (ep->bEndpointAddress == 0) {
926 			if (!udc->ep0_pending || !list_empty(&ep->queue)) {
927 				spin_unlock_irqrestore(&udc->lock, flags);
928 				return -EL2HLT;
929 			}
930 
931 			/* empty DATA stage? */
932 			is_in = udc->ep0_in;
933 			if (!req->req.length) {
934 
935 				/* chip became CONFIGURED or ADDRESSED
936 				 * earlier; drivers may already have queued
937 				 * requests to non-control endpoints
938 				 */
939 				if (udc->ep0_set_config) {
940 					u16	irq_en = omap_readw(UDC_IRQ_EN);
941 
942 					irq_en |= UDC_DS_CHG_IE | UDC_EP0_IE;
943 					if (!udc->ep0_reset_config)
944 						irq_en |= UDC_EPN_RX_IE
945 							| UDC_EPN_TX_IE;
946 					omap_writew(irq_en, UDC_IRQ_EN);
947 				}
948 
949 				/* STATUS for zero length DATA stages is
950 				 * always an IN ... even for IN transfers,
951 				 * a weird case which seem to stall OMAP.
952 				 */
953 				omap_writew(UDC_EP_SEL | UDC_EP_DIR,
954 						UDC_EP_NUM);
955 				omap_writew(UDC_CLR_EP, UDC_CTRL);
956 				omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
957 				omap_writew(UDC_EP_DIR, UDC_EP_NUM);
958 
959 				/* cleanup */
960 				udc->ep0_pending = 0;
961 				done(ep, req, 0);
962 				req = NULL;
963 
964 			/* non-empty DATA stage */
965 			} else if (is_in) {
966 				omap_writew(UDC_EP_SEL | UDC_EP_DIR,
967 						UDC_EP_NUM);
968 			} else {
969 				if (udc->ep0_setup)
970 					goto irq_wait;
971 				omap_writew(UDC_EP_SEL, UDC_EP_NUM);
972 			}
973 		} else {
974 			is_in = ep->bEndpointAddress & USB_DIR_IN;
975 			if (!ep->has_dma)
976 				use_ep(ep, UDC_EP_SEL);
977 			/* if ISO: SOF IRQs must be enabled/disabled! */
978 		}
979 
980 		if (ep->has_dma)
981 			(is_in ? next_in_dma : next_out_dma)(ep, req);
982 		else if (req) {
983 			if ((is_in ? write_fifo : read_fifo)(ep, req) == 1)
984 				req = NULL;
985 			deselect_ep();
986 			if (!is_in) {
987 				omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
988 				ep->ackwait = 1 + ep->double_buf;
989 			}
990 			/* IN: 6 wait states before it'll tx */
991 		}
992 	}
993 
994 irq_wait:
995 	/* irq handler advances the queue */
996 	if (req != NULL)
997 		list_add_tail(&req->queue, &ep->queue);
998 	spin_unlock_irqrestore(&udc->lock, flags);
999 
1000 	return 0;
1001 }
1002 
1003 static int omap_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1004 {
1005 	struct omap_ep	*ep = container_of(_ep, struct omap_ep, ep);
1006 	struct omap_req	*req = NULL, *iter;
1007 	unsigned long	flags;
1008 
1009 	if (!_ep || !_req)
1010 		return -EINVAL;
1011 
1012 	spin_lock_irqsave(&ep->udc->lock, flags);
1013 
1014 	/* make sure it's actually queued on this endpoint */
1015 	list_for_each_entry(iter, &ep->queue, queue) {
1016 		if (&iter->req != _req)
1017 			continue;
1018 		req = iter;
1019 		break;
1020 	}
1021 	if (!req) {
1022 		spin_unlock_irqrestore(&ep->udc->lock, flags);
1023 		return -EINVAL;
1024 	}
1025 
1026 	if (use_dma && ep->dma_channel && ep->queue.next == &req->queue) {
1027 		int channel = ep->dma_channel;
1028 
1029 		/* releasing the channel cancels the request,
1030 		 * reclaiming the channel restarts the queue
1031 		 */
1032 		dma_channel_release(ep);
1033 		dma_channel_claim(ep, channel);
1034 	} else
1035 		done(ep, req, -ECONNRESET);
1036 	spin_unlock_irqrestore(&ep->udc->lock, flags);
1037 	return 0;
1038 }
1039 
1040 /*-------------------------------------------------------------------------*/
1041 
1042 static int omap_ep_set_halt(struct usb_ep *_ep, int value)
1043 {
1044 	struct omap_ep	*ep = container_of(_ep, struct omap_ep, ep);
1045 	unsigned long	flags;
1046 	int		status = -EOPNOTSUPP;
1047 
1048 	spin_lock_irqsave(&ep->udc->lock, flags);
1049 
1050 	/* just use protocol stalls for ep0; real halts are annoying */
1051 	if (ep->bEndpointAddress == 0) {
1052 		if (!ep->udc->ep0_pending)
1053 			status = -EINVAL;
1054 		else if (value) {
1055 			if (ep->udc->ep0_set_config) {
1056 				WARNING("error changing config?\n");
1057 				omap_writew(UDC_CLR_CFG, UDC_SYSCON2);
1058 			}
1059 			omap_writew(UDC_STALL_CMD, UDC_SYSCON2);
1060 			ep->udc->ep0_pending = 0;
1061 			status = 0;
1062 		} else /* NOP */
1063 			status = 0;
1064 
1065 	/* otherwise, all active non-ISO endpoints can halt */
1066 	} else if (ep->bmAttributes != USB_ENDPOINT_XFER_ISOC && ep->ep.desc) {
1067 
1068 		/* IN endpoints must already be idle */
1069 		if ((ep->bEndpointAddress & USB_DIR_IN)
1070 				&& !list_empty(&ep->queue)) {
1071 			status = -EAGAIN;
1072 			goto done;
1073 		}
1074 
1075 		if (value) {
1076 			int	channel;
1077 
1078 			if (use_dma && ep->dma_channel
1079 					&& !list_empty(&ep->queue)) {
1080 				channel = ep->dma_channel;
1081 				dma_channel_release(ep);
1082 			} else
1083 				channel = 0;
1084 
1085 			use_ep(ep, UDC_EP_SEL);
1086 			if (omap_readw(UDC_STAT_FLG) & UDC_NON_ISO_FIFO_EMPTY) {
1087 				omap_writew(UDC_SET_HALT, UDC_CTRL);
1088 				status = 0;
1089 			} else
1090 				status = -EAGAIN;
1091 			deselect_ep();
1092 
1093 			if (channel)
1094 				dma_channel_claim(ep, channel);
1095 		} else {
1096 			use_ep(ep, 0);
1097 			omap_writew(ep->udc->clr_halt, UDC_CTRL);
1098 			ep->ackwait = 0;
1099 			if (!(ep->bEndpointAddress & USB_DIR_IN)) {
1100 				omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1101 				ep->ackwait = 1 + ep->double_buf;
1102 			}
1103 		}
1104 	}
1105 done:
1106 	VDBG("%s %s halt stat %d\n", ep->ep.name,
1107 		value ? "set" : "clear", status);
1108 
1109 	spin_unlock_irqrestore(&ep->udc->lock, flags);
1110 	return status;
1111 }
1112 
1113 static const struct usb_ep_ops omap_ep_ops = {
1114 	.enable		= omap_ep_enable,
1115 	.disable	= omap_ep_disable,
1116 
1117 	.alloc_request	= omap_alloc_request,
1118 	.free_request	= omap_free_request,
1119 
1120 	.queue		= omap_ep_queue,
1121 	.dequeue	= omap_ep_dequeue,
1122 
1123 	.set_halt	= omap_ep_set_halt,
1124 	/* fifo_status ... report bytes in fifo */
1125 	/* fifo_flush ... flush fifo */
1126 };
1127 
1128 /*-------------------------------------------------------------------------*/
1129 
1130 static int omap_get_frame(struct usb_gadget *gadget)
1131 {
1132 	u16	sof = omap_readw(UDC_SOF);
1133 	return (sof & UDC_TS_OK) ? (sof & UDC_TS) : -EL2NSYNC;
1134 }
1135 
1136 static int omap_wakeup(struct usb_gadget *gadget)
1137 {
1138 	struct omap_udc	*udc;
1139 	unsigned long	flags;
1140 	int		retval = -EHOSTUNREACH;
1141 
1142 	udc = container_of(gadget, struct omap_udc, gadget);
1143 
1144 	spin_lock_irqsave(&udc->lock, flags);
1145 	if (udc->devstat & UDC_SUS) {
1146 		/* NOTE:  OTG spec erratum says that OTG devices may
1147 		 * issue wakeups without host enable.
1148 		 */
1149 		if (udc->devstat & (UDC_B_HNP_ENABLE|UDC_R_WK_OK)) {
1150 			DBG("remote wakeup...\n");
1151 			omap_writew(UDC_RMT_WKP, UDC_SYSCON2);
1152 			retval = 0;
1153 		}
1154 
1155 	/* NOTE:  non-OTG systems may use SRP TOO... */
1156 	} else if (!(udc->devstat & UDC_ATT)) {
1157 		if (!IS_ERR_OR_NULL(udc->transceiver))
1158 			retval = otg_start_srp(udc->transceiver->otg);
1159 	}
1160 	spin_unlock_irqrestore(&udc->lock, flags);
1161 
1162 	return retval;
1163 }
1164 
1165 static int
1166 omap_set_selfpowered(struct usb_gadget *gadget, int is_selfpowered)
1167 {
1168 	struct omap_udc	*udc;
1169 	unsigned long	flags;
1170 	u16		syscon1;
1171 
1172 	gadget->is_selfpowered = (is_selfpowered != 0);
1173 	udc = container_of(gadget, struct omap_udc, gadget);
1174 	spin_lock_irqsave(&udc->lock, flags);
1175 	syscon1 = omap_readw(UDC_SYSCON1);
1176 	if (is_selfpowered)
1177 		syscon1 |= UDC_SELF_PWR;
1178 	else
1179 		syscon1 &= ~UDC_SELF_PWR;
1180 	omap_writew(syscon1, UDC_SYSCON1);
1181 	spin_unlock_irqrestore(&udc->lock, flags);
1182 
1183 	return 0;
1184 }
1185 
1186 static int can_pullup(struct omap_udc *udc)
1187 {
1188 	return udc->driver && udc->softconnect && udc->vbus_active;
1189 }
1190 
1191 static void pullup_enable(struct omap_udc *udc)
1192 {
1193 	u16 w;
1194 
1195 	w = omap_readw(UDC_SYSCON1);
1196 	w |= UDC_PULLUP_EN;
1197 	omap_writew(w, UDC_SYSCON1);
1198 	if (!gadget_is_otg(&udc->gadget) && !cpu_is_omap15xx()) {
1199 		u32 l;
1200 
1201 		l = omap_readl(OTG_CTRL);
1202 		l |= OTG_BSESSVLD;
1203 		omap_writel(l, OTG_CTRL);
1204 	}
1205 	omap_writew(UDC_DS_CHG_IE, UDC_IRQ_EN);
1206 }
1207 
1208 static void pullup_disable(struct omap_udc *udc)
1209 {
1210 	u16 w;
1211 
1212 	if (!gadget_is_otg(&udc->gadget) && !cpu_is_omap15xx()) {
1213 		u32 l;
1214 
1215 		l = omap_readl(OTG_CTRL);
1216 		l &= ~OTG_BSESSVLD;
1217 		omap_writel(l, OTG_CTRL);
1218 	}
1219 	omap_writew(UDC_DS_CHG_IE, UDC_IRQ_EN);
1220 	w = omap_readw(UDC_SYSCON1);
1221 	w &= ~UDC_PULLUP_EN;
1222 	omap_writew(w, UDC_SYSCON1);
1223 }
1224 
1225 static struct omap_udc *udc;
1226 
1227 static void omap_udc_enable_clock(int enable)
1228 {
1229 	if (udc == NULL || udc->dc_clk == NULL || udc->hhc_clk == NULL)
1230 		return;
1231 
1232 	if (enable) {
1233 		clk_enable(udc->dc_clk);
1234 		clk_enable(udc->hhc_clk);
1235 		udelay(100);
1236 	} else {
1237 		clk_disable(udc->hhc_clk);
1238 		clk_disable(udc->dc_clk);
1239 	}
1240 }
1241 
1242 /*
1243  * Called by whatever detects VBUS sessions:  external transceiver
1244  * driver, or maybe GPIO0 VBUS IRQ.  May request 48 MHz clock.
1245  */
1246 static int omap_vbus_session(struct usb_gadget *gadget, int is_active)
1247 {
1248 	struct omap_udc	*udc;
1249 	unsigned long	flags;
1250 	u32 l;
1251 
1252 	udc = container_of(gadget, struct omap_udc, gadget);
1253 	spin_lock_irqsave(&udc->lock, flags);
1254 	VDBG("VBUS %s\n", is_active ? "on" : "off");
1255 	udc->vbus_active = (is_active != 0);
1256 	if (cpu_is_omap15xx()) {
1257 		/* "software" detect, ignored if !VBUS_MODE_1510 */
1258 		l = omap_readl(FUNC_MUX_CTRL_0);
1259 		if (is_active)
1260 			l |= VBUS_CTRL_1510;
1261 		else
1262 			l &= ~VBUS_CTRL_1510;
1263 		omap_writel(l, FUNC_MUX_CTRL_0);
1264 	}
1265 	if (udc->dc_clk != NULL && is_active) {
1266 		if (!udc->clk_requested) {
1267 			omap_udc_enable_clock(1);
1268 			udc->clk_requested = 1;
1269 		}
1270 	}
1271 	if (can_pullup(udc))
1272 		pullup_enable(udc);
1273 	else
1274 		pullup_disable(udc);
1275 	if (udc->dc_clk != NULL && !is_active) {
1276 		if (udc->clk_requested) {
1277 			omap_udc_enable_clock(0);
1278 			udc->clk_requested = 0;
1279 		}
1280 	}
1281 	spin_unlock_irqrestore(&udc->lock, flags);
1282 	return 0;
1283 }
1284 
1285 static int omap_vbus_draw(struct usb_gadget *gadget, unsigned mA)
1286 {
1287 	struct omap_udc	*udc;
1288 
1289 	udc = container_of(gadget, struct omap_udc, gadget);
1290 	if (!IS_ERR_OR_NULL(udc->transceiver))
1291 		return usb_phy_set_power(udc->transceiver, mA);
1292 	return -EOPNOTSUPP;
1293 }
1294 
1295 static int omap_pullup(struct usb_gadget *gadget, int is_on)
1296 {
1297 	struct omap_udc	*udc;
1298 	unsigned long	flags;
1299 
1300 	udc = container_of(gadget, struct omap_udc, gadget);
1301 	spin_lock_irqsave(&udc->lock, flags);
1302 	udc->softconnect = (is_on != 0);
1303 	if (can_pullup(udc))
1304 		pullup_enable(udc);
1305 	else
1306 		pullup_disable(udc);
1307 	spin_unlock_irqrestore(&udc->lock, flags);
1308 	return 0;
1309 }
1310 
1311 static int omap_udc_start(struct usb_gadget *g,
1312 		struct usb_gadget_driver *driver);
1313 static int omap_udc_stop(struct usb_gadget *g);
1314 
1315 static const struct usb_gadget_ops omap_gadget_ops = {
1316 	.get_frame		= omap_get_frame,
1317 	.wakeup			= omap_wakeup,
1318 	.set_selfpowered	= omap_set_selfpowered,
1319 	.vbus_session		= omap_vbus_session,
1320 	.vbus_draw		= omap_vbus_draw,
1321 	.pullup			= omap_pullup,
1322 	.udc_start		= omap_udc_start,
1323 	.udc_stop		= omap_udc_stop,
1324 };
1325 
1326 /*-------------------------------------------------------------------------*/
1327 
1328 /* dequeue ALL requests; caller holds udc->lock */
1329 static void nuke(struct omap_ep *ep, int status)
1330 {
1331 	struct omap_req	*req;
1332 
1333 	ep->stopped = 1;
1334 
1335 	if (use_dma && ep->dma_channel)
1336 		dma_channel_release(ep);
1337 
1338 	use_ep(ep, 0);
1339 	omap_writew(UDC_CLR_EP, UDC_CTRL);
1340 	if (ep->bEndpointAddress && ep->bmAttributes != USB_ENDPOINT_XFER_ISOC)
1341 		omap_writew(UDC_SET_HALT, UDC_CTRL);
1342 
1343 	while (!list_empty(&ep->queue)) {
1344 		req = list_entry(ep->queue.next, struct omap_req, queue);
1345 		done(ep, req, status);
1346 	}
1347 }
1348 
1349 /* caller holds udc->lock */
1350 static void udc_quiesce(struct omap_udc *udc)
1351 {
1352 	struct omap_ep	*ep;
1353 
1354 	udc->gadget.speed = USB_SPEED_UNKNOWN;
1355 	nuke(&udc->ep[0], -ESHUTDOWN);
1356 	list_for_each_entry(ep, &udc->gadget.ep_list, ep.ep_list)
1357 		nuke(ep, -ESHUTDOWN);
1358 }
1359 
1360 /*-------------------------------------------------------------------------*/
1361 
1362 static void update_otg(struct omap_udc *udc)
1363 {
1364 	u16	devstat;
1365 
1366 	if (!gadget_is_otg(&udc->gadget))
1367 		return;
1368 
1369 	if (omap_readl(OTG_CTRL) & OTG_ID)
1370 		devstat = omap_readw(UDC_DEVSTAT);
1371 	else
1372 		devstat = 0;
1373 
1374 	udc->gadget.b_hnp_enable = !!(devstat & UDC_B_HNP_ENABLE);
1375 	udc->gadget.a_hnp_support = !!(devstat & UDC_A_HNP_SUPPORT);
1376 	udc->gadget.a_alt_hnp_support = !!(devstat & UDC_A_ALT_HNP_SUPPORT);
1377 
1378 	/* Enable HNP early, avoiding races on suspend irq path.
1379 	 * ASSUMES OTG state machine B_BUS_REQ input is true.
1380 	 */
1381 	if (udc->gadget.b_hnp_enable) {
1382 		u32 l;
1383 
1384 		l = omap_readl(OTG_CTRL);
1385 		l |= OTG_B_HNPEN | OTG_B_BUSREQ;
1386 		l &= ~OTG_PULLUP;
1387 		omap_writel(l, OTG_CTRL);
1388 	}
1389 }
1390 
1391 static void ep0_irq(struct omap_udc *udc, u16 irq_src)
1392 {
1393 	struct omap_ep	*ep0 = &udc->ep[0];
1394 	struct omap_req	*req = NULL;
1395 
1396 	ep0->irqs++;
1397 
1398 	/* Clear any pending requests and then scrub any rx/tx state
1399 	 * before starting to handle the SETUP request.
1400 	 */
1401 	if (irq_src & UDC_SETUP) {
1402 		u16	ack = irq_src & (UDC_EP0_TX|UDC_EP0_RX);
1403 
1404 		nuke(ep0, 0);
1405 		if (ack) {
1406 			omap_writew(ack, UDC_IRQ_SRC);
1407 			irq_src = UDC_SETUP;
1408 		}
1409 	}
1410 
1411 	/* IN/OUT packets mean we're in the DATA or STATUS stage.
1412 	 * This driver uses only uses protocol stalls (ep0 never halts),
1413 	 * and if we got this far the gadget driver already had a
1414 	 * chance to stall.  Tries to be forgiving of host oddities.
1415 	 *
1416 	 * NOTE:  the last chance gadget drivers have to stall control
1417 	 * requests is during their request completion callback.
1418 	 */
1419 	if (!list_empty(&ep0->queue))
1420 		req = container_of(ep0->queue.next, struct omap_req, queue);
1421 
1422 	/* IN == TX to host */
1423 	if (irq_src & UDC_EP0_TX) {
1424 		int	stat;
1425 
1426 		omap_writew(UDC_EP0_TX, UDC_IRQ_SRC);
1427 		omap_writew(UDC_EP_SEL|UDC_EP_DIR, UDC_EP_NUM);
1428 		stat = omap_readw(UDC_STAT_FLG);
1429 		if (stat & UDC_ACK) {
1430 			if (udc->ep0_in) {
1431 				/* write next IN packet from response,
1432 				 * or set up the status stage.
1433 				 */
1434 				if (req)
1435 					stat = write_fifo(ep0, req);
1436 				omap_writew(UDC_EP_DIR, UDC_EP_NUM);
1437 				if (!req && udc->ep0_pending) {
1438 					omap_writew(UDC_EP_SEL, UDC_EP_NUM);
1439 					omap_writew(UDC_CLR_EP, UDC_CTRL);
1440 					omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1441 					omap_writew(0, UDC_EP_NUM);
1442 					udc->ep0_pending = 0;
1443 				} /* else:  6 wait states before it'll tx */
1444 			} else {
1445 				/* ack status stage of OUT transfer */
1446 				omap_writew(UDC_EP_DIR, UDC_EP_NUM);
1447 				if (req)
1448 					done(ep0, req, 0);
1449 			}
1450 			req = NULL;
1451 		} else if (stat & UDC_STALL) {
1452 			omap_writew(UDC_CLR_HALT, UDC_CTRL);
1453 			omap_writew(UDC_EP_DIR, UDC_EP_NUM);
1454 		} else {
1455 			omap_writew(UDC_EP_DIR, UDC_EP_NUM);
1456 		}
1457 	}
1458 
1459 	/* OUT == RX from host */
1460 	if (irq_src & UDC_EP0_RX) {
1461 		int	stat;
1462 
1463 		omap_writew(UDC_EP0_RX, UDC_IRQ_SRC);
1464 		omap_writew(UDC_EP_SEL, UDC_EP_NUM);
1465 		stat = omap_readw(UDC_STAT_FLG);
1466 		if (stat & UDC_ACK) {
1467 			if (!udc->ep0_in) {
1468 				stat = 0;
1469 				/* read next OUT packet of request, maybe
1470 				 * reactiviting the fifo; stall on errors.
1471 				 */
1472 				stat = read_fifo(ep0, req);
1473 				if (!req || stat < 0) {
1474 					omap_writew(UDC_STALL_CMD, UDC_SYSCON2);
1475 					udc->ep0_pending = 0;
1476 					stat = 0;
1477 				} else if (stat == 0)
1478 					omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1479 				omap_writew(0, UDC_EP_NUM);
1480 
1481 				/* activate status stage */
1482 				if (stat == 1) {
1483 					done(ep0, req, 0);
1484 					/* that may have STALLed ep0... */
1485 					omap_writew(UDC_EP_SEL | UDC_EP_DIR,
1486 							UDC_EP_NUM);
1487 					omap_writew(UDC_CLR_EP, UDC_CTRL);
1488 					omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1489 					omap_writew(UDC_EP_DIR, UDC_EP_NUM);
1490 					udc->ep0_pending = 0;
1491 				}
1492 			} else {
1493 				/* ack status stage of IN transfer */
1494 				omap_writew(0, UDC_EP_NUM);
1495 				if (req)
1496 					done(ep0, req, 0);
1497 			}
1498 		} else if (stat & UDC_STALL) {
1499 			omap_writew(UDC_CLR_HALT, UDC_CTRL);
1500 			omap_writew(0, UDC_EP_NUM);
1501 		} else {
1502 			omap_writew(0, UDC_EP_NUM);
1503 		}
1504 	}
1505 
1506 	/* SETUP starts all control transfers */
1507 	if (irq_src & UDC_SETUP) {
1508 		union u {
1509 			u16			word[4];
1510 			struct usb_ctrlrequest	r;
1511 		} u;
1512 		int			status = -EINVAL;
1513 		struct omap_ep		*ep;
1514 
1515 		/* read the (latest) SETUP message */
1516 		do {
1517 			omap_writew(UDC_SETUP_SEL, UDC_EP_NUM);
1518 			/* two bytes at a time */
1519 			u.word[0] = omap_readw(UDC_DATA);
1520 			u.word[1] = omap_readw(UDC_DATA);
1521 			u.word[2] = omap_readw(UDC_DATA);
1522 			u.word[3] = omap_readw(UDC_DATA);
1523 			omap_writew(0, UDC_EP_NUM);
1524 		} while (omap_readw(UDC_IRQ_SRC) & UDC_SETUP);
1525 
1526 #define	w_value		le16_to_cpu(u.r.wValue)
1527 #define	w_index		le16_to_cpu(u.r.wIndex)
1528 #define	w_length	le16_to_cpu(u.r.wLength)
1529 
1530 		/* Delegate almost all control requests to the gadget driver,
1531 		 * except for a handful of ch9 status/feature requests that
1532 		 * hardware doesn't autodecode _and_ the gadget API hides.
1533 		 */
1534 		udc->ep0_in = (u.r.bRequestType & USB_DIR_IN) != 0;
1535 		udc->ep0_set_config = 0;
1536 		udc->ep0_pending = 1;
1537 		ep0->stopped = 0;
1538 		ep0->ackwait = 0;
1539 		switch (u.r.bRequest) {
1540 		case USB_REQ_SET_CONFIGURATION:
1541 			/* udc needs to know when ep != 0 is valid */
1542 			if (u.r.bRequestType != USB_RECIP_DEVICE)
1543 				goto delegate;
1544 			if (w_length != 0)
1545 				goto do_stall;
1546 			udc->ep0_set_config = 1;
1547 			udc->ep0_reset_config = (w_value == 0);
1548 			VDBG("set config %d\n", w_value);
1549 
1550 			/* update udc NOW since gadget driver may start
1551 			 * queueing requests immediately; clear config
1552 			 * later if it fails the request.
1553 			 */
1554 			if (udc->ep0_reset_config)
1555 				omap_writew(UDC_CLR_CFG, UDC_SYSCON2);
1556 			else
1557 				omap_writew(UDC_DEV_CFG, UDC_SYSCON2);
1558 			update_otg(udc);
1559 			goto delegate;
1560 		case USB_REQ_CLEAR_FEATURE:
1561 			/* clear endpoint halt */
1562 			if (u.r.bRequestType != USB_RECIP_ENDPOINT)
1563 				goto delegate;
1564 			if (w_value != USB_ENDPOINT_HALT
1565 					|| w_length != 0)
1566 				goto do_stall;
1567 			ep = &udc->ep[w_index & 0xf];
1568 			if (ep != ep0) {
1569 				if (w_index & USB_DIR_IN)
1570 					ep += 16;
1571 				if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
1572 						|| !ep->ep.desc)
1573 					goto do_stall;
1574 				use_ep(ep, 0);
1575 				omap_writew(udc->clr_halt, UDC_CTRL);
1576 				ep->ackwait = 0;
1577 				if (!(ep->bEndpointAddress & USB_DIR_IN)) {
1578 					omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1579 					ep->ackwait = 1 + ep->double_buf;
1580 				}
1581 				/* NOTE:  assumes the host behaves sanely,
1582 				 * only clearing real halts.  Else we may
1583 				 * need to kill pending transfers and then
1584 				 * restart the queue... very messy for DMA!
1585 				 */
1586 			}
1587 			VDBG("%s halt cleared by host\n", ep->name);
1588 			goto ep0out_status_stage;
1589 		case USB_REQ_SET_FEATURE:
1590 			/* set endpoint halt */
1591 			if (u.r.bRequestType != USB_RECIP_ENDPOINT)
1592 				goto delegate;
1593 			if (w_value != USB_ENDPOINT_HALT
1594 					|| w_length != 0)
1595 				goto do_stall;
1596 			ep = &udc->ep[w_index & 0xf];
1597 			if (w_index & USB_DIR_IN)
1598 				ep += 16;
1599 			if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
1600 					|| ep == ep0 || !ep->ep.desc)
1601 				goto do_stall;
1602 			if (use_dma && ep->has_dma) {
1603 				/* this has rude side-effects (aborts) and
1604 				 * can't really work if DMA-IN is active
1605 				 */
1606 				DBG("%s host set_halt, NYET\n", ep->name);
1607 				goto do_stall;
1608 			}
1609 			use_ep(ep, 0);
1610 			/* can't halt if fifo isn't empty... */
1611 			omap_writew(UDC_CLR_EP, UDC_CTRL);
1612 			omap_writew(UDC_SET_HALT, UDC_CTRL);
1613 			VDBG("%s halted by host\n", ep->name);
1614 ep0out_status_stage:
1615 			status = 0;
1616 			omap_writew(UDC_EP_SEL|UDC_EP_DIR, UDC_EP_NUM);
1617 			omap_writew(UDC_CLR_EP, UDC_CTRL);
1618 			omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1619 			omap_writew(UDC_EP_DIR, UDC_EP_NUM);
1620 			udc->ep0_pending = 0;
1621 			break;
1622 		case USB_REQ_GET_STATUS:
1623 			/* USB_ENDPOINT_HALT status? */
1624 			if (u.r.bRequestType != (USB_DIR_IN|USB_RECIP_ENDPOINT))
1625 				goto intf_status;
1626 
1627 			/* ep0 never stalls */
1628 			if (!(w_index & 0xf))
1629 				goto zero_status;
1630 
1631 			/* only active endpoints count */
1632 			ep = &udc->ep[w_index & 0xf];
1633 			if (w_index & USB_DIR_IN)
1634 				ep += 16;
1635 			if (!ep->ep.desc)
1636 				goto do_stall;
1637 
1638 			/* iso never stalls */
1639 			if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC)
1640 				goto zero_status;
1641 
1642 			/* FIXME don't assume non-halted endpoints!! */
1643 			ERR("%s status, can't report\n", ep->ep.name);
1644 			goto do_stall;
1645 
1646 intf_status:
1647 			/* return interface status.  if we were pedantic,
1648 			 * we'd detect non-existent interfaces, and stall.
1649 			 */
1650 			if (u.r.bRequestType
1651 					!= (USB_DIR_IN|USB_RECIP_INTERFACE))
1652 				goto delegate;
1653 
1654 zero_status:
1655 			/* return two zero bytes */
1656 			omap_writew(UDC_EP_SEL|UDC_EP_DIR, UDC_EP_NUM);
1657 			omap_writew(0, UDC_DATA);
1658 			omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1659 			omap_writew(UDC_EP_DIR, UDC_EP_NUM);
1660 			status = 0;
1661 			VDBG("GET_STATUS, interface %d\n", w_index);
1662 			/* next, status stage */
1663 			break;
1664 		default:
1665 delegate:
1666 			/* activate the ep0out fifo right away */
1667 			if (!udc->ep0_in && w_length) {
1668 				omap_writew(0, UDC_EP_NUM);
1669 				omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1670 			}
1671 
1672 			/* gadget drivers see class/vendor specific requests,
1673 			 * {SET,GET}_{INTERFACE,DESCRIPTOR,CONFIGURATION},
1674 			 * and more
1675 			 */
1676 			VDBG("SETUP %02x.%02x v%04x i%04x l%04x\n",
1677 				u.r.bRequestType, u.r.bRequest,
1678 				w_value, w_index, w_length);
1679 
1680 #undef	w_value
1681 #undef	w_index
1682 #undef	w_length
1683 
1684 			/* The gadget driver may return an error here,
1685 			 * causing an immediate protocol stall.
1686 			 *
1687 			 * Else it must issue a response, either queueing a
1688 			 * response buffer for the DATA stage, or halting ep0
1689 			 * (causing a protocol stall, not a real halt).  A
1690 			 * zero length buffer means no DATA stage.
1691 			 *
1692 			 * It's fine to issue that response after the setup()
1693 			 * call returns, and this IRQ was handled.
1694 			 */
1695 			udc->ep0_setup = 1;
1696 			spin_unlock(&udc->lock);
1697 			status = udc->driver->setup(&udc->gadget, &u.r);
1698 			spin_lock(&udc->lock);
1699 			udc->ep0_setup = 0;
1700 		}
1701 
1702 		if (status < 0) {
1703 do_stall:
1704 			VDBG("req %02x.%02x protocol STALL; stat %d\n",
1705 					u.r.bRequestType, u.r.bRequest, status);
1706 			if (udc->ep0_set_config) {
1707 				if (udc->ep0_reset_config)
1708 					WARNING("error resetting config?\n");
1709 				else
1710 					omap_writew(UDC_CLR_CFG, UDC_SYSCON2);
1711 			}
1712 			omap_writew(UDC_STALL_CMD, UDC_SYSCON2);
1713 			udc->ep0_pending = 0;
1714 		}
1715 	}
1716 }
1717 
1718 /*-------------------------------------------------------------------------*/
1719 
1720 #define OTG_FLAGS (UDC_B_HNP_ENABLE|UDC_A_HNP_SUPPORT|UDC_A_ALT_HNP_SUPPORT)
1721 
1722 static void devstate_irq(struct omap_udc *udc, u16 irq_src)
1723 {
1724 	u16	devstat, change;
1725 
1726 	devstat = omap_readw(UDC_DEVSTAT);
1727 	change = devstat ^ udc->devstat;
1728 	udc->devstat = devstat;
1729 
1730 	if (change & (UDC_USB_RESET|UDC_ATT)) {
1731 		udc_quiesce(udc);
1732 
1733 		if (change & UDC_ATT) {
1734 			/* driver for any external transceiver will
1735 			 * have called omap_vbus_session() already
1736 			 */
1737 			if (devstat & UDC_ATT) {
1738 				udc->gadget.speed = USB_SPEED_FULL;
1739 				VDBG("connect\n");
1740 				if (IS_ERR_OR_NULL(udc->transceiver))
1741 					pullup_enable(udc);
1742 				/* if (driver->connect) call it */
1743 			} else if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
1744 				udc->gadget.speed = USB_SPEED_UNKNOWN;
1745 				if (IS_ERR_OR_NULL(udc->transceiver))
1746 					pullup_disable(udc);
1747 				DBG("disconnect, gadget %s\n",
1748 					udc->driver->driver.name);
1749 				if (udc->driver->disconnect) {
1750 					spin_unlock(&udc->lock);
1751 					udc->driver->disconnect(&udc->gadget);
1752 					spin_lock(&udc->lock);
1753 				}
1754 			}
1755 			change &= ~UDC_ATT;
1756 		}
1757 
1758 		if (change & UDC_USB_RESET) {
1759 			if (devstat & UDC_USB_RESET) {
1760 				VDBG("RESET=1\n");
1761 			} else {
1762 				udc->gadget.speed = USB_SPEED_FULL;
1763 				INFO("USB reset done, gadget %s\n",
1764 					udc->driver->driver.name);
1765 				/* ep0 traffic is legal from now on */
1766 				omap_writew(UDC_DS_CHG_IE | UDC_EP0_IE,
1767 						UDC_IRQ_EN);
1768 			}
1769 			change &= ~UDC_USB_RESET;
1770 		}
1771 	}
1772 	if (change & UDC_SUS) {
1773 		if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
1774 			/* FIXME tell isp1301 to suspend/resume (?) */
1775 			if (devstat & UDC_SUS) {
1776 				VDBG("suspend\n");
1777 				update_otg(udc);
1778 				/* HNP could be under way already */
1779 				if (udc->gadget.speed == USB_SPEED_FULL
1780 						&& udc->driver->suspend) {
1781 					spin_unlock(&udc->lock);
1782 					udc->driver->suspend(&udc->gadget);
1783 					spin_lock(&udc->lock);
1784 				}
1785 				if (!IS_ERR_OR_NULL(udc->transceiver))
1786 					usb_phy_set_suspend(
1787 							udc->transceiver, 1);
1788 			} else {
1789 				VDBG("resume\n");
1790 				if (!IS_ERR_OR_NULL(udc->transceiver))
1791 					usb_phy_set_suspend(
1792 							udc->transceiver, 0);
1793 				if (udc->gadget.speed == USB_SPEED_FULL
1794 						&& udc->driver->resume) {
1795 					spin_unlock(&udc->lock);
1796 					udc->driver->resume(&udc->gadget);
1797 					spin_lock(&udc->lock);
1798 				}
1799 			}
1800 		}
1801 		change &= ~UDC_SUS;
1802 	}
1803 	if (!cpu_is_omap15xx() && (change & OTG_FLAGS)) {
1804 		update_otg(udc);
1805 		change &= ~OTG_FLAGS;
1806 	}
1807 
1808 	change &= ~(UDC_CFG|UDC_DEF|UDC_ADD);
1809 	if (change)
1810 		VDBG("devstat %03x, ignore change %03x\n",
1811 			devstat,  change);
1812 
1813 	omap_writew(UDC_DS_CHG, UDC_IRQ_SRC);
1814 }
1815 
1816 static irqreturn_t omap_udc_irq(int irq, void *_udc)
1817 {
1818 	struct omap_udc	*udc = _udc;
1819 	u16		irq_src;
1820 	irqreturn_t	status = IRQ_NONE;
1821 	unsigned long	flags;
1822 
1823 	spin_lock_irqsave(&udc->lock, flags);
1824 	irq_src = omap_readw(UDC_IRQ_SRC);
1825 
1826 	/* Device state change (usb ch9 stuff) */
1827 	if (irq_src & UDC_DS_CHG) {
1828 		devstate_irq(_udc, irq_src);
1829 		status = IRQ_HANDLED;
1830 		irq_src &= ~UDC_DS_CHG;
1831 	}
1832 
1833 	/* EP0 control transfers */
1834 	if (irq_src & (UDC_EP0_RX|UDC_SETUP|UDC_EP0_TX)) {
1835 		ep0_irq(_udc, irq_src);
1836 		status = IRQ_HANDLED;
1837 		irq_src &= ~(UDC_EP0_RX|UDC_SETUP|UDC_EP0_TX);
1838 	}
1839 
1840 	/* DMA transfer completion */
1841 	if (use_dma && (irq_src & (UDC_TXN_DONE|UDC_RXN_CNT|UDC_RXN_EOT))) {
1842 		dma_irq(_udc, irq_src);
1843 		status = IRQ_HANDLED;
1844 		irq_src &= ~(UDC_TXN_DONE|UDC_RXN_CNT|UDC_RXN_EOT);
1845 	}
1846 
1847 	irq_src &= ~(UDC_IRQ_SOF | UDC_EPN_TX|UDC_EPN_RX);
1848 	if (irq_src)
1849 		DBG("udc_irq, unhandled %03x\n", irq_src);
1850 	spin_unlock_irqrestore(&udc->lock, flags);
1851 
1852 	return status;
1853 }
1854 
1855 /* workaround for seemingly-lost IRQs for RX ACKs... */
1856 #define PIO_OUT_TIMEOUT	(jiffies + HZ/3)
1857 #define HALF_FULL(f)	(!((f)&(UDC_NON_ISO_FIFO_FULL|UDC_NON_ISO_FIFO_EMPTY)))
1858 
1859 static void pio_out_timer(struct timer_list *t)
1860 {
1861 	struct omap_ep	*ep = from_timer(ep, t, timer);
1862 	unsigned long	flags;
1863 	u16		stat_flg;
1864 
1865 	spin_lock_irqsave(&ep->udc->lock, flags);
1866 	if (!list_empty(&ep->queue) && ep->ackwait) {
1867 		use_ep(ep, UDC_EP_SEL);
1868 		stat_flg = omap_readw(UDC_STAT_FLG);
1869 
1870 		if ((stat_flg & UDC_ACK) && (!(stat_flg & UDC_FIFO_EN)
1871 				|| (ep->double_buf && HALF_FULL(stat_flg)))) {
1872 			struct omap_req	*req;
1873 
1874 			VDBG("%s: lose, %04x\n", ep->ep.name, stat_flg);
1875 			req = container_of(ep->queue.next,
1876 					struct omap_req, queue);
1877 			(void) read_fifo(ep, req);
1878 			omap_writew(ep->bEndpointAddress, UDC_EP_NUM);
1879 			omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1880 			ep->ackwait = 1 + ep->double_buf;
1881 		} else
1882 			deselect_ep();
1883 	}
1884 	mod_timer(&ep->timer, PIO_OUT_TIMEOUT);
1885 	spin_unlock_irqrestore(&ep->udc->lock, flags);
1886 }
1887 
1888 static irqreturn_t omap_udc_pio_irq(int irq, void *_dev)
1889 {
1890 	u16		epn_stat, irq_src;
1891 	irqreturn_t	status = IRQ_NONE;
1892 	struct omap_ep	*ep;
1893 	int		epnum;
1894 	struct omap_udc	*udc = _dev;
1895 	struct omap_req	*req;
1896 	unsigned long	flags;
1897 
1898 	spin_lock_irqsave(&udc->lock, flags);
1899 	epn_stat = omap_readw(UDC_EPN_STAT);
1900 	irq_src = omap_readw(UDC_IRQ_SRC);
1901 
1902 	/* handle OUT first, to avoid some wasteful NAKs */
1903 	if (irq_src & UDC_EPN_RX) {
1904 		epnum = (epn_stat >> 8) & 0x0f;
1905 		omap_writew(UDC_EPN_RX, UDC_IRQ_SRC);
1906 		status = IRQ_HANDLED;
1907 		ep = &udc->ep[epnum];
1908 		ep->irqs++;
1909 
1910 		omap_writew(epnum | UDC_EP_SEL, UDC_EP_NUM);
1911 		ep->fnf = 0;
1912 		if (omap_readw(UDC_STAT_FLG) & UDC_ACK) {
1913 			ep->ackwait--;
1914 			if (!list_empty(&ep->queue)) {
1915 				int stat;
1916 				req = container_of(ep->queue.next,
1917 						struct omap_req, queue);
1918 				stat = read_fifo(ep, req);
1919 				if (!ep->double_buf)
1920 					ep->fnf = 1;
1921 			}
1922 		}
1923 		/* min 6 clock delay before clearing EP_SEL ... */
1924 		epn_stat = omap_readw(UDC_EPN_STAT);
1925 		epn_stat = omap_readw(UDC_EPN_STAT);
1926 		omap_writew(epnum, UDC_EP_NUM);
1927 
1928 		/* enabling fifo _after_ clearing ACK, contrary to docs,
1929 		 * reduces lossage; timer still needed though (sigh).
1930 		 */
1931 		if (ep->fnf) {
1932 			omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1933 			ep->ackwait = 1 + ep->double_buf;
1934 		}
1935 		mod_timer(&ep->timer, PIO_OUT_TIMEOUT);
1936 	}
1937 
1938 	/* then IN transfers */
1939 	else if (irq_src & UDC_EPN_TX) {
1940 		epnum = epn_stat & 0x0f;
1941 		omap_writew(UDC_EPN_TX, UDC_IRQ_SRC);
1942 		status = IRQ_HANDLED;
1943 		ep = &udc->ep[16 + epnum];
1944 		ep->irqs++;
1945 
1946 		omap_writew(epnum | UDC_EP_DIR | UDC_EP_SEL, UDC_EP_NUM);
1947 		if (omap_readw(UDC_STAT_FLG) & UDC_ACK) {
1948 			ep->ackwait = 0;
1949 			if (!list_empty(&ep->queue)) {
1950 				req = container_of(ep->queue.next,
1951 						struct omap_req, queue);
1952 				(void) write_fifo(ep, req);
1953 			}
1954 		}
1955 		/* min 6 clock delay before clearing EP_SEL ... */
1956 		epn_stat = omap_readw(UDC_EPN_STAT);
1957 		epn_stat = omap_readw(UDC_EPN_STAT);
1958 		omap_writew(epnum | UDC_EP_DIR, UDC_EP_NUM);
1959 		/* then 6 clocks before it'd tx */
1960 	}
1961 
1962 	spin_unlock_irqrestore(&udc->lock, flags);
1963 	return status;
1964 }
1965 
1966 #ifdef	USE_ISO
1967 static irqreturn_t omap_udc_iso_irq(int irq, void *_dev)
1968 {
1969 	struct omap_udc	*udc = _dev;
1970 	struct omap_ep	*ep;
1971 	int		pending = 0;
1972 	unsigned long	flags;
1973 
1974 	spin_lock_irqsave(&udc->lock, flags);
1975 
1976 	/* handle all non-DMA ISO transfers */
1977 	list_for_each_entry(ep, &udc->iso, iso) {
1978 		u16		stat;
1979 		struct omap_req	*req;
1980 
1981 		if (ep->has_dma || list_empty(&ep->queue))
1982 			continue;
1983 		req = list_entry(ep->queue.next, struct omap_req, queue);
1984 
1985 		use_ep(ep, UDC_EP_SEL);
1986 		stat = omap_readw(UDC_STAT_FLG);
1987 
1988 		/* NOTE: like the other controller drivers, this isn't
1989 		 * currently reporting lost or damaged frames.
1990 		 */
1991 		if (ep->bEndpointAddress & USB_DIR_IN) {
1992 			if (stat & UDC_MISS_IN)
1993 				/* done(ep, req, -EPROTO) */;
1994 			else
1995 				write_fifo(ep, req);
1996 		} else {
1997 			int	status = 0;
1998 
1999 			if (stat & UDC_NO_RXPACKET)
2000 				status = -EREMOTEIO;
2001 			else if (stat & UDC_ISO_ERR)
2002 				status = -EILSEQ;
2003 			else if (stat & UDC_DATA_FLUSH)
2004 				status = -ENOSR;
2005 
2006 			if (status)
2007 				/* done(ep, req, status) */;
2008 			else
2009 				read_fifo(ep, req);
2010 		}
2011 		deselect_ep();
2012 		/* 6 wait states before next EP */
2013 
2014 		ep->irqs++;
2015 		if (!list_empty(&ep->queue))
2016 			pending = 1;
2017 	}
2018 	if (!pending) {
2019 		u16 w;
2020 
2021 		w = omap_readw(UDC_IRQ_EN);
2022 		w &= ~UDC_SOF_IE;
2023 		omap_writew(w, UDC_IRQ_EN);
2024 	}
2025 	omap_writew(UDC_IRQ_SOF, UDC_IRQ_SRC);
2026 
2027 	spin_unlock_irqrestore(&udc->lock, flags);
2028 	return IRQ_HANDLED;
2029 }
2030 #endif
2031 
2032 /*-------------------------------------------------------------------------*/
2033 
2034 static inline int machine_without_vbus_sense(void)
2035 {
2036 	return machine_is_omap_innovator()
2037 		|| machine_is_omap_osk()
2038 		|| machine_is_omap_palmte()
2039 		|| machine_is_sx1()
2040 		/* No known omap7xx boards with vbus sense */
2041 		|| cpu_is_omap7xx();
2042 }
2043 
2044 static int omap_udc_start(struct usb_gadget *g,
2045 		struct usb_gadget_driver *driver)
2046 {
2047 	int		status;
2048 	struct omap_ep	*ep;
2049 	unsigned long	flags;
2050 
2051 
2052 	spin_lock_irqsave(&udc->lock, flags);
2053 	/* reset state */
2054 	list_for_each_entry(ep, &udc->gadget.ep_list, ep.ep_list) {
2055 		ep->irqs = 0;
2056 		if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC)
2057 			continue;
2058 		use_ep(ep, 0);
2059 		omap_writew(UDC_SET_HALT, UDC_CTRL);
2060 	}
2061 	udc->ep0_pending = 0;
2062 	udc->ep[0].irqs = 0;
2063 	udc->softconnect = 1;
2064 
2065 	/* hook up the driver */
2066 	driver->driver.bus = NULL;
2067 	udc->driver = driver;
2068 	spin_unlock_irqrestore(&udc->lock, flags);
2069 
2070 	if (udc->dc_clk != NULL)
2071 		omap_udc_enable_clock(1);
2072 
2073 	omap_writew(UDC_IRQ_SRC_MASK, UDC_IRQ_SRC);
2074 
2075 	/* connect to bus through transceiver */
2076 	if (!IS_ERR_OR_NULL(udc->transceiver)) {
2077 		status = otg_set_peripheral(udc->transceiver->otg,
2078 						&udc->gadget);
2079 		if (status < 0) {
2080 			ERR("can't bind to transceiver\n");
2081 			udc->driver = NULL;
2082 			goto done;
2083 		}
2084 	} else {
2085 		status = 0;
2086 		if (can_pullup(udc))
2087 			pullup_enable(udc);
2088 		else
2089 			pullup_disable(udc);
2090 	}
2091 
2092 	/* boards that don't have VBUS sensing can't autogate 48MHz;
2093 	 * can't enter deep sleep while a gadget driver is active.
2094 	 */
2095 	if (machine_without_vbus_sense())
2096 		omap_vbus_session(&udc->gadget, 1);
2097 
2098 done:
2099 	if (udc->dc_clk != NULL)
2100 		omap_udc_enable_clock(0);
2101 
2102 	return status;
2103 }
2104 
2105 static int omap_udc_stop(struct usb_gadget *g)
2106 {
2107 	unsigned long	flags;
2108 
2109 	if (udc->dc_clk != NULL)
2110 		omap_udc_enable_clock(1);
2111 
2112 	if (machine_without_vbus_sense())
2113 		omap_vbus_session(&udc->gadget, 0);
2114 
2115 	if (!IS_ERR_OR_NULL(udc->transceiver))
2116 		(void) otg_set_peripheral(udc->transceiver->otg, NULL);
2117 	else
2118 		pullup_disable(udc);
2119 
2120 	spin_lock_irqsave(&udc->lock, flags);
2121 	udc_quiesce(udc);
2122 	spin_unlock_irqrestore(&udc->lock, flags);
2123 
2124 	udc->driver = NULL;
2125 
2126 	if (udc->dc_clk != NULL)
2127 		omap_udc_enable_clock(0);
2128 
2129 	return 0;
2130 }
2131 
2132 /*-------------------------------------------------------------------------*/
2133 
2134 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
2135 
2136 #include <linux/seq_file.h>
2137 
2138 static const char proc_filename[] = "driver/udc";
2139 
2140 #define FOURBITS "%s%s%s%s"
2141 #define EIGHTBITS "%s%s%s%s%s%s%s%s"
2142 
2143 static void proc_ep_show(struct seq_file *s, struct omap_ep *ep)
2144 {
2145 	u16		stat_flg;
2146 	struct omap_req	*req;
2147 	char		buf[20];
2148 
2149 	use_ep(ep, 0);
2150 
2151 	if (use_dma && ep->has_dma)
2152 		snprintf(buf, sizeof buf, "(%cxdma%d lch%d) ",
2153 			(ep->bEndpointAddress & USB_DIR_IN) ? 't' : 'r',
2154 			ep->dma_channel - 1, ep->lch);
2155 	else
2156 		buf[0] = 0;
2157 
2158 	stat_flg = omap_readw(UDC_STAT_FLG);
2159 	seq_printf(s,
2160 		"\n%s %s%s%sirqs %ld stat %04x " EIGHTBITS FOURBITS "%s\n",
2161 		ep->name, buf,
2162 		ep->double_buf ? "dbuf " : "",
2163 		({ char *s;
2164 		switch (ep->ackwait) {
2165 		case 0:
2166 			s = "";
2167 			break;
2168 		case 1:
2169 			s = "(ackw) ";
2170 			break;
2171 		case 2:
2172 			s = "(ackw2) ";
2173 			break;
2174 		default:
2175 			s = "(?) ";
2176 			break;
2177 		} s; }),
2178 		ep->irqs, stat_flg,
2179 		(stat_flg & UDC_NO_RXPACKET) ? "no_rxpacket " : "",
2180 		(stat_flg & UDC_MISS_IN) ? "miss_in " : "",
2181 		(stat_flg & UDC_DATA_FLUSH) ? "data_flush " : "",
2182 		(stat_flg & UDC_ISO_ERR) ? "iso_err " : "",
2183 		(stat_flg & UDC_ISO_FIFO_EMPTY) ? "iso_fifo_empty " : "",
2184 		(stat_flg & UDC_ISO_FIFO_FULL) ? "iso_fifo_full " : "",
2185 		(stat_flg & UDC_EP_HALTED) ? "HALT " : "",
2186 		(stat_flg & UDC_STALL) ? "STALL " : "",
2187 		(stat_flg & UDC_NAK) ? "NAK " : "",
2188 		(stat_flg & UDC_ACK) ? "ACK " : "",
2189 		(stat_flg & UDC_FIFO_EN) ? "fifo_en " : "",
2190 		(stat_flg & UDC_NON_ISO_FIFO_EMPTY) ? "fifo_empty " : "",
2191 		(stat_flg & UDC_NON_ISO_FIFO_FULL) ? "fifo_full " : "");
2192 
2193 	if (list_empty(&ep->queue))
2194 		seq_printf(s, "\t(queue empty)\n");
2195 	else
2196 		list_for_each_entry(req, &ep->queue, queue) {
2197 			unsigned	length = req->req.actual;
2198 
2199 			if (use_dma && buf[0]) {
2200 				length += ((ep->bEndpointAddress & USB_DIR_IN)
2201 						? dma_src_len : dma_dest_len)
2202 					(ep, req->req.dma + length);
2203 				buf[0] = 0;
2204 			}
2205 			seq_printf(s, "\treq %p len %d/%d buf %p\n",
2206 					&req->req, length,
2207 					req->req.length, req->req.buf);
2208 		}
2209 }
2210 
2211 static char *trx_mode(unsigned m, int enabled)
2212 {
2213 	switch (m) {
2214 	case 0:
2215 		return enabled ? "*6wire" : "unused";
2216 	case 1:
2217 		return "4wire";
2218 	case 2:
2219 		return "3wire";
2220 	case 3:
2221 		return "6wire";
2222 	default:
2223 		return "unknown";
2224 	}
2225 }
2226 
2227 static int proc_otg_show(struct seq_file *s)
2228 {
2229 	u32		tmp;
2230 	u32		trans = 0;
2231 	char		*ctrl_name = "(UNKNOWN)";
2232 
2233 	tmp = omap_readl(OTG_REV);
2234 	ctrl_name = "tranceiver_ctrl";
2235 	trans = omap_readw(USB_TRANSCEIVER_CTRL);
2236 	seq_printf(s, "\nOTG rev %d.%d, %s %05x\n",
2237 		tmp >> 4, tmp & 0xf, ctrl_name, trans);
2238 	tmp = omap_readw(OTG_SYSCON_1);
2239 	seq_printf(s, "otg_syscon1 %08x usb2 %s, usb1 %s, usb0 %s,"
2240 			FOURBITS "\n", tmp,
2241 		trx_mode(USB2_TRX_MODE(tmp), trans & CONF_USB2_UNI_R),
2242 		trx_mode(USB1_TRX_MODE(tmp), trans & CONF_USB1_UNI_R),
2243 		(USB0_TRX_MODE(tmp) == 0 && !cpu_is_omap1710())
2244 			? "internal"
2245 			: trx_mode(USB0_TRX_MODE(tmp), 1),
2246 		(tmp & OTG_IDLE_EN) ? " !otg" : "",
2247 		(tmp & HST_IDLE_EN) ? " !host" : "",
2248 		(tmp & DEV_IDLE_EN) ? " !dev" : "",
2249 		(tmp & OTG_RESET_DONE) ? " reset_done" : " reset_active");
2250 	tmp = omap_readl(OTG_SYSCON_2);
2251 	seq_printf(s, "otg_syscon2 %08x%s" EIGHTBITS
2252 			" b_ase_brst=%d hmc=%d\n", tmp,
2253 		(tmp & OTG_EN) ? " otg_en" : "",
2254 		(tmp & USBX_SYNCHRO) ? " synchro" : "",
2255 		/* much more SRP stuff */
2256 		(tmp & SRP_DATA) ? " srp_data" : "",
2257 		(tmp & SRP_VBUS) ? " srp_vbus" : "",
2258 		(tmp & OTG_PADEN) ? " otg_paden" : "",
2259 		(tmp & HMC_PADEN) ? " hmc_paden" : "",
2260 		(tmp & UHOST_EN) ? " uhost_en" : "",
2261 		(tmp & HMC_TLLSPEED) ? " tllspeed" : "",
2262 		(tmp & HMC_TLLATTACH) ? " tllattach" : "",
2263 		B_ASE_BRST(tmp),
2264 		OTG_HMC(tmp));
2265 	tmp = omap_readl(OTG_CTRL);
2266 	seq_printf(s, "otg_ctrl    %06x" EIGHTBITS EIGHTBITS "%s\n", tmp,
2267 		(tmp & OTG_ASESSVLD) ? " asess" : "",
2268 		(tmp & OTG_BSESSEND) ? " bsess_end" : "",
2269 		(tmp & OTG_BSESSVLD) ? " bsess" : "",
2270 		(tmp & OTG_VBUSVLD) ? " vbus" : "",
2271 		(tmp & OTG_ID) ? " id" : "",
2272 		(tmp & OTG_DRIVER_SEL) ? " DEVICE" : " HOST",
2273 		(tmp & OTG_A_SETB_HNPEN) ? " a_setb_hnpen" : "",
2274 		(tmp & OTG_A_BUSREQ) ? " a_bus" : "",
2275 		(tmp & OTG_B_HNPEN) ? " b_hnpen" : "",
2276 		(tmp & OTG_B_BUSREQ) ? " b_bus" : "",
2277 		(tmp & OTG_BUSDROP) ? " busdrop" : "",
2278 		(tmp & OTG_PULLDOWN) ? " down" : "",
2279 		(tmp & OTG_PULLUP) ? " up" : "",
2280 		(tmp & OTG_DRV_VBUS) ? " drv" : "",
2281 		(tmp & OTG_PD_VBUS) ? " pd_vb" : "",
2282 		(tmp & OTG_PU_VBUS) ? " pu_vb" : "",
2283 		(tmp & OTG_PU_ID) ? " pu_id" : ""
2284 		);
2285 	tmp = omap_readw(OTG_IRQ_EN);
2286 	seq_printf(s, "otg_irq_en  %04x" "\n", tmp);
2287 	tmp = omap_readw(OTG_IRQ_SRC);
2288 	seq_printf(s, "otg_irq_src %04x" "\n", tmp);
2289 	tmp = omap_readw(OTG_OUTCTRL);
2290 	seq_printf(s, "otg_outctrl %04x" "\n", tmp);
2291 	tmp = omap_readw(OTG_TEST);
2292 	seq_printf(s, "otg_test    %04x" "\n", tmp);
2293 	return 0;
2294 }
2295 
2296 static int proc_udc_show(struct seq_file *s, void *_)
2297 {
2298 	u32		tmp;
2299 	struct omap_ep	*ep;
2300 	unsigned long	flags;
2301 
2302 	spin_lock_irqsave(&udc->lock, flags);
2303 
2304 	seq_printf(s, "%s, version: " DRIVER_VERSION
2305 #ifdef	USE_ISO
2306 		" (iso)"
2307 #endif
2308 		"%s\n",
2309 		driver_desc,
2310 		use_dma ?  " (dma)" : "");
2311 
2312 	tmp = omap_readw(UDC_REV) & 0xff;
2313 	seq_printf(s,
2314 		"UDC rev %d.%d, fifo mode %d, gadget %s\n"
2315 		"hmc %d, transceiver %s\n",
2316 		tmp >> 4, tmp & 0xf,
2317 		fifo_mode,
2318 		udc->driver ? udc->driver->driver.name : "(none)",
2319 		HMC,
2320 		udc->transceiver
2321 			? udc->transceiver->label
2322 			: (cpu_is_omap1710()
2323 				? "external" : "(none)"));
2324 	seq_printf(s, "ULPD control %04x req %04x status %04x\n",
2325 		omap_readw(ULPD_CLOCK_CTRL),
2326 		omap_readw(ULPD_SOFT_REQ),
2327 		omap_readw(ULPD_STATUS_REQ));
2328 
2329 	/* OTG controller registers */
2330 	if (!cpu_is_omap15xx())
2331 		proc_otg_show(s);
2332 
2333 	tmp = omap_readw(UDC_SYSCON1);
2334 	seq_printf(s, "\nsyscon1     %04x" EIGHTBITS "\n", tmp,
2335 		(tmp & UDC_CFG_LOCK) ? " cfg_lock" : "",
2336 		(tmp & UDC_DATA_ENDIAN) ? " data_endian" : "",
2337 		(tmp & UDC_DMA_ENDIAN) ? " dma_endian" : "",
2338 		(tmp & UDC_NAK_EN) ? " nak" : "",
2339 		(tmp & UDC_AUTODECODE_DIS) ? " autodecode_dis" : "",
2340 		(tmp & UDC_SELF_PWR) ? " self_pwr" : "",
2341 		(tmp & UDC_SOFF_DIS) ? " soff_dis" : "",
2342 		(tmp & UDC_PULLUP_EN) ? " PULLUP" : "");
2343 	/* syscon2 is write-only */
2344 
2345 	/* UDC controller registers */
2346 	if (!(tmp & UDC_PULLUP_EN)) {
2347 		seq_printf(s, "(suspended)\n");
2348 		spin_unlock_irqrestore(&udc->lock, flags);
2349 		return 0;
2350 	}
2351 
2352 	tmp = omap_readw(UDC_DEVSTAT);
2353 	seq_printf(s, "devstat     %04x" EIGHTBITS "%s%s\n", tmp,
2354 		(tmp & UDC_B_HNP_ENABLE) ? " b_hnp" : "",
2355 		(tmp & UDC_A_HNP_SUPPORT) ? " a_hnp" : "",
2356 		(tmp & UDC_A_ALT_HNP_SUPPORT) ? " a_alt_hnp" : "",
2357 		(tmp & UDC_R_WK_OK) ? " r_wk_ok" : "",
2358 		(tmp & UDC_USB_RESET) ? " usb_reset" : "",
2359 		(tmp & UDC_SUS) ? " SUS" : "",
2360 		(tmp & UDC_CFG) ? " CFG" : "",
2361 		(tmp & UDC_ADD) ? " ADD" : "",
2362 		(tmp & UDC_DEF) ? " DEF" : "",
2363 		(tmp & UDC_ATT) ? " ATT" : "");
2364 	seq_printf(s, "sof         %04x\n", omap_readw(UDC_SOF));
2365 	tmp = omap_readw(UDC_IRQ_EN);
2366 	seq_printf(s, "irq_en      %04x" FOURBITS "%s\n", tmp,
2367 		(tmp & UDC_SOF_IE) ? " sof" : "",
2368 		(tmp & UDC_EPN_RX_IE) ? " epn_rx" : "",
2369 		(tmp & UDC_EPN_TX_IE) ? " epn_tx" : "",
2370 		(tmp & UDC_DS_CHG_IE) ? " ds_chg" : "",
2371 		(tmp & UDC_EP0_IE) ? " ep0" : "");
2372 	tmp = omap_readw(UDC_IRQ_SRC);
2373 	seq_printf(s, "irq_src     %04x" EIGHTBITS "%s%s\n", tmp,
2374 		(tmp & UDC_TXN_DONE) ? " txn_done" : "",
2375 		(tmp & UDC_RXN_CNT) ? " rxn_cnt" : "",
2376 		(tmp & UDC_RXN_EOT) ? " rxn_eot" : "",
2377 		(tmp & UDC_IRQ_SOF) ? " sof" : "",
2378 		(tmp & UDC_EPN_RX) ? " epn_rx" : "",
2379 		(tmp & UDC_EPN_TX) ? " epn_tx" : "",
2380 		(tmp & UDC_DS_CHG) ? " ds_chg" : "",
2381 		(tmp & UDC_SETUP) ? " setup" : "",
2382 		(tmp & UDC_EP0_RX) ? " ep0out" : "",
2383 		(tmp & UDC_EP0_TX) ? " ep0in" : "");
2384 	if (use_dma) {
2385 		unsigned i;
2386 
2387 		tmp = omap_readw(UDC_DMA_IRQ_EN);
2388 		seq_printf(s, "dma_irq_en  %04x%s" EIGHTBITS "\n", tmp,
2389 			(tmp & UDC_TX_DONE_IE(3)) ? " tx2_done" : "",
2390 			(tmp & UDC_RX_CNT_IE(3)) ? " rx2_cnt" : "",
2391 			(tmp & UDC_RX_EOT_IE(3)) ? " rx2_eot" : "",
2392 
2393 			(tmp & UDC_TX_DONE_IE(2)) ? " tx1_done" : "",
2394 			(tmp & UDC_RX_CNT_IE(2)) ? " rx1_cnt" : "",
2395 			(tmp & UDC_RX_EOT_IE(2)) ? " rx1_eot" : "",
2396 
2397 			(tmp & UDC_TX_DONE_IE(1)) ? " tx0_done" : "",
2398 			(tmp & UDC_RX_CNT_IE(1)) ? " rx0_cnt" : "",
2399 			(tmp & UDC_RX_EOT_IE(1)) ? " rx0_eot" : "");
2400 
2401 		tmp = omap_readw(UDC_RXDMA_CFG);
2402 		seq_printf(s, "rxdma_cfg   %04x\n", tmp);
2403 		if (tmp) {
2404 			for (i = 0; i < 3; i++) {
2405 				if ((tmp & (0x0f << (i * 4))) == 0)
2406 					continue;
2407 				seq_printf(s, "rxdma[%d]    %04x\n", i,
2408 						omap_readw(UDC_RXDMA(i + 1)));
2409 			}
2410 		}
2411 		tmp = omap_readw(UDC_TXDMA_CFG);
2412 		seq_printf(s, "txdma_cfg   %04x\n", tmp);
2413 		if (tmp) {
2414 			for (i = 0; i < 3; i++) {
2415 				if (!(tmp & (0x0f << (i * 4))))
2416 					continue;
2417 				seq_printf(s, "txdma[%d]    %04x\n", i,
2418 						omap_readw(UDC_TXDMA(i + 1)));
2419 			}
2420 		}
2421 	}
2422 
2423 	tmp = omap_readw(UDC_DEVSTAT);
2424 	if (tmp & UDC_ATT) {
2425 		proc_ep_show(s, &udc->ep[0]);
2426 		if (tmp & UDC_ADD) {
2427 			list_for_each_entry(ep, &udc->gadget.ep_list,
2428 					ep.ep_list) {
2429 				if (ep->ep.desc)
2430 					proc_ep_show(s, ep);
2431 			}
2432 		}
2433 	}
2434 	spin_unlock_irqrestore(&udc->lock, flags);
2435 	return 0;
2436 }
2437 
2438 static void create_proc_file(void)
2439 {
2440 	proc_create_single(proc_filename, 0, NULL, proc_udc_show);
2441 }
2442 
2443 static void remove_proc_file(void)
2444 {
2445 	remove_proc_entry(proc_filename, NULL);
2446 }
2447 
2448 #else
2449 
2450 static inline void create_proc_file(void) {}
2451 static inline void remove_proc_file(void) {}
2452 
2453 #endif
2454 
2455 /*-------------------------------------------------------------------------*/
2456 
2457 /* Before this controller can enumerate, we need to pick an endpoint
2458  * configuration, or "fifo_mode"  That involves allocating 2KB of packet
2459  * buffer space among the endpoints we'll be operating.
2460  *
2461  * NOTE: as of OMAP 1710 ES2.0, writing a new endpoint config when
2462  * UDC_SYSCON_1.CFG_LOCK is set can now work.  We won't use that
2463  * capability yet though.
2464  */
2465 static unsigned
2466 omap_ep_setup(char *name, u8 addr, u8 type,
2467 		unsigned buf, unsigned maxp, int dbuf)
2468 {
2469 	struct omap_ep	*ep;
2470 	u16		epn_rxtx = 0;
2471 
2472 	/* OUT endpoints first, then IN */
2473 	ep = &udc->ep[addr & 0xf];
2474 	if (addr & USB_DIR_IN)
2475 		ep += 16;
2476 
2477 	/* in case of ep init table bugs */
2478 	BUG_ON(ep->name[0]);
2479 
2480 	/* chip setup ... bit values are same for IN, OUT */
2481 	if (type == USB_ENDPOINT_XFER_ISOC) {
2482 		switch (maxp) {
2483 		case 8:
2484 			epn_rxtx = 0 << 12;
2485 			break;
2486 		case 16:
2487 			epn_rxtx = 1 << 12;
2488 			break;
2489 		case 32:
2490 			epn_rxtx = 2 << 12;
2491 			break;
2492 		case 64:
2493 			epn_rxtx = 3 << 12;
2494 			break;
2495 		case 128:
2496 			epn_rxtx = 4 << 12;
2497 			break;
2498 		case 256:
2499 			epn_rxtx = 5 << 12;
2500 			break;
2501 		case 512:
2502 			epn_rxtx = 6 << 12;
2503 			break;
2504 		default:
2505 			BUG();
2506 		}
2507 		epn_rxtx |= UDC_EPN_RX_ISO;
2508 		dbuf = 1;
2509 	} else {
2510 		/* double-buffering "not supported" on 15xx,
2511 		 * and ignored for PIO-IN on newer chips
2512 		 * (for more reliable behavior)
2513 		 */
2514 		if (!use_dma || cpu_is_omap15xx())
2515 			dbuf = 0;
2516 
2517 		switch (maxp) {
2518 		case 8:
2519 			epn_rxtx = 0 << 12;
2520 			break;
2521 		case 16:
2522 			epn_rxtx = 1 << 12;
2523 			break;
2524 		case 32:
2525 			epn_rxtx = 2 << 12;
2526 			break;
2527 		case 64:
2528 			epn_rxtx = 3 << 12;
2529 			break;
2530 		default:
2531 			BUG();
2532 		}
2533 		if (dbuf && addr)
2534 			epn_rxtx |= UDC_EPN_RX_DB;
2535 		timer_setup(&ep->timer, pio_out_timer, 0);
2536 	}
2537 	if (addr)
2538 		epn_rxtx |= UDC_EPN_RX_VALID;
2539 	BUG_ON(buf & 0x07);
2540 	epn_rxtx |= buf >> 3;
2541 
2542 	DBG("%s addr %02x rxtx %04x maxp %d%s buf %d\n",
2543 		name, addr, epn_rxtx, maxp, dbuf ? "x2" : "", buf);
2544 
2545 	if (addr & USB_DIR_IN)
2546 		omap_writew(epn_rxtx, UDC_EP_TX(addr & 0xf));
2547 	else
2548 		omap_writew(epn_rxtx, UDC_EP_RX(addr));
2549 
2550 	/* next endpoint's buffer starts after this one's */
2551 	buf += maxp;
2552 	if (dbuf)
2553 		buf += maxp;
2554 	BUG_ON(buf > 2048);
2555 
2556 	/* set up driver data structures */
2557 	BUG_ON(strlen(name) >= sizeof ep->name);
2558 	strlcpy(ep->name, name, sizeof ep->name);
2559 	INIT_LIST_HEAD(&ep->queue);
2560 	INIT_LIST_HEAD(&ep->iso);
2561 	ep->bEndpointAddress = addr;
2562 	ep->bmAttributes = type;
2563 	ep->double_buf = dbuf;
2564 	ep->udc = udc;
2565 
2566 	switch (type) {
2567 	case USB_ENDPOINT_XFER_CONTROL:
2568 		ep->ep.caps.type_control = true;
2569 		ep->ep.caps.dir_in = true;
2570 		ep->ep.caps.dir_out = true;
2571 		break;
2572 	case USB_ENDPOINT_XFER_ISOC:
2573 		ep->ep.caps.type_iso = true;
2574 		break;
2575 	case USB_ENDPOINT_XFER_BULK:
2576 		ep->ep.caps.type_bulk = true;
2577 		break;
2578 	case USB_ENDPOINT_XFER_INT:
2579 		ep->ep.caps.type_int = true;
2580 		break;
2581 	}
2582 
2583 	if (addr & USB_DIR_IN)
2584 		ep->ep.caps.dir_in = true;
2585 	else
2586 		ep->ep.caps.dir_out = true;
2587 
2588 	ep->ep.name = ep->name;
2589 	ep->ep.ops = &omap_ep_ops;
2590 	ep->maxpacket = maxp;
2591 	usb_ep_set_maxpacket_limit(&ep->ep, ep->maxpacket);
2592 	list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
2593 
2594 	return buf;
2595 }
2596 
2597 static void omap_udc_release(struct device *dev)
2598 {
2599 	pullup_disable(udc);
2600 	if (!IS_ERR_OR_NULL(udc->transceiver)) {
2601 		usb_put_phy(udc->transceiver);
2602 		udc->transceiver = NULL;
2603 	}
2604 	omap_writew(0, UDC_SYSCON1);
2605 	remove_proc_file();
2606 	if (udc->dc_clk) {
2607 		if (udc->clk_requested)
2608 			omap_udc_enable_clock(0);
2609 		clk_put(udc->hhc_clk);
2610 		clk_put(udc->dc_clk);
2611 	}
2612 	if (udc->done)
2613 		complete(udc->done);
2614 	kfree(udc);
2615 }
2616 
2617 static int
2618 omap_udc_setup(struct platform_device *odev, struct usb_phy *xceiv)
2619 {
2620 	unsigned	tmp, buf;
2621 
2622 	/* abolish any previous hardware state */
2623 	omap_writew(0, UDC_SYSCON1);
2624 	omap_writew(0, UDC_IRQ_EN);
2625 	omap_writew(UDC_IRQ_SRC_MASK, UDC_IRQ_SRC);
2626 	omap_writew(0, UDC_DMA_IRQ_EN);
2627 	omap_writew(0, UDC_RXDMA_CFG);
2628 	omap_writew(0, UDC_TXDMA_CFG);
2629 
2630 	/* UDC_PULLUP_EN gates the chip clock */
2631 	/* OTG_SYSCON_1 |= DEV_IDLE_EN; */
2632 
2633 	udc = kzalloc(sizeof(*udc), GFP_KERNEL);
2634 	if (!udc)
2635 		return -ENOMEM;
2636 
2637 	spin_lock_init(&udc->lock);
2638 
2639 	udc->gadget.ops = &omap_gadget_ops;
2640 	udc->gadget.ep0 = &udc->ep[0].ep;
2641 	INIT_LIST_HEAD(&udc->gadget.ep_list);
2642 	INIT_LIST_HEAD(&udc->iso);
2643 	udc->gadget.speed = USB_SPEED_UNKNOWN;
2644 	udc->gadget.max_speed = USB_SPEED_FULL;
2645 	udc->gadget.name = driver_name;
2646 	udc->gadget.quirk_ep_out_aligned_size = 1;
2647 	udc->transceiver = xceiv;
2648 
2649 	/* ep0 is special; put it right after the SETUP buffer */
2650 	buf = omap_ep_setup("ep0", 0, USB_ENDPOINT_XFER_CONTROL,
2651 			8 /* after SETUP */, 64 /* maxpacket */, 0);
2652 	list_del_init(&udc->ep[0].ep.ep_list);
2653 
2654 	/* initially disable all non-ep0 endpoints */
2655 	for (tmp = 1; tmp < 15; tmp++) {
2656 		omap_writew(0, UDC_EP_RX(tmp));
2657 		omap_writew(0, UDC_EP_TX(tmp));
2658 	}
2659 
2660 #define OMAP_BULK_EP(name, addr) \
2661 	buf = omap_ep_setup(name "-bulk", addr, \
2662 			USB_ENDPOINT_XFER_BULK, buf, 64, 1);
2663 #define OMAP_INT_EP(name, addr, maxp) \
2664 	buf = omap_ep_setup(name "-int", addr, \
2665 			USB_ENDPOINT_XFER_INT, buf, maxp, 0);
2666 #define OMAP_ISO_EP(name, addr, maxp) \
2667 	buf = omap_ep_setup(name "-iso", addr, \
2668 			USB_ENDPOINT_XFER_ISOC, buf, maxp, 1);
2669 
2670 	switch (fifo_mode) {
2671 	case 0:
2672 		OMAP_BULK_EP("ep1in",  USB_DIR_IN  | 1);
2673 		OMAP_BULK_EP("ep2out", USB_DIR_OUT | 2);
2674 		OMAP_INT_EP("ep3in",   USB_DIR_IN  | 3, 16);
2675 		break;
2676 	case 1:
2677 		OMAP_BULK_EP("ep1in",  USB_DIR_IN  | 1);
2678 		OMAP_BULK_EP("ep2out", USB_DIR_OUT | 2);
2679 		OMAP_INT_EP("ep9in",   USB_DIR_IN  | 9, 16);
2680 
2681 		OMAP_BULK_EP("ep3in",  USB_DIR_IN  | 3);
2682 		OMAP_BULK_EP("ep4out", USB_DIR_OUT | 4);
2683 		OMAP_INT_EP("ep10in",  USB_DIR_IN  | 10, 16);
2684 
2685 		OMAP_BULK_EP("ep5in",  USB_DIR_IN  | 5);
2686 		OMAP_BULK_EP("ep5out", USB_DIR_OUT | 5);
2687 		OMAP_INT_EP("ep11in",  USB_DIR_IN  | 11, 16);
2688 
2689 		OMAP_BULK_EP("ep6in",  USB_DIR_IN  | 6);
2690 		OMAP_BULK_EP("ep6out", USB_DIR_OUT | 6);
2691 		OMAP_INT_EP("ep12in",  USB_DIR_IN  | 12, 16);
2692 
2693 		OMAP_BULK_EP("ep7in",  USB_DIR_IN  | 7);
2694 		OMAP_BULK_EP("ep7out", USB_DIR_OUT | 7);
2695 		OMAP_INT_EP("ep13in",  USB_DIR_IN  | 13, 16);
2696 		OMAP_INT_EP("ep13out", USB_DIR_OUT | 13, 16);
2697 
2698 		OMAP_BULK_EP("ep8in",  USB_DIR_IN  | 8);
2699 		OMAP_BULK_EP("ep8out", USB_DIR_OUT | 8);
2700 		OMAP_INT_EP("ep14in",  USB_DIR_IN  | 14, 16);
2701 		OMAP_INT_EP("ep14out", USB_DIR_OUT | 14, 16);
2702 
2703 		OMAP_BULK_EP("ep15in",  USB_DIR_IN  | 15);
2704 		OMAP_BULK_EP("ep15out", USB_DIR_OUT | 15);
2705 
2706 		break;
2707 
2708 #ifdef	USE_ISO
2709 	case 2:			/* mixed iso/bulk */
2710 		OMAP_ISO_EP("ep1in",   USB_DIR_IN  | 1, 256);
2711 		OMAP_ISO_EP("ep2out",  USB_DIR_OUT | 2, 256);
2712 		OMAP_ISO_EP("ep3in",   USB_DIR_IN  | 3, 128);
2713 		OMAP_ISO_EP("ep4out",  USB_DIR_OUT | 4, 128);
2714 
2715 		OMAP_INT_EP("ep5in",   USB_DIR_IN  | 5, 16);
2716 
2717 		OMAP_BULK_EP("ep6in",  USB_DIR_IN  | 6);
2718 		OMAP_BULK_EP("ep7out", USB_DIR_OUT | 7);
2719 		OMAP_INT_EP("ep8in",   USB_DIR_IN  | 8, 16);
2720 		break;
2721 	case 3:			/* mixed bulk/iso */
2722 		OMAP_BULK_EP("ep1in",  USB_DIR_IN  | 1);
2723 		OMAP_BULK_EP("ep2out", USB_DIR_OUT | 2);
2724 		OMAP_INT_EP("ep3in",   USB_DIR_IN  | 3, 16);
2725 
2726 		OMAP_BULK_EP("ep4in",  USB_DIR_IN  | 4);
2727 		OMAP_BULK_EP("ep5out", USB_DIR_OUT | 5);
2728 		OMAP_INT_EP("ep6in",   USB_DIR_IN  | 6, 16);
2729 
2730 		OMAP_ISO_EP("ep7in",   USB_DIR_IN  | 7, 256);
2731 		OMAP_ISO_EP("ep8out",  USB_DIR_OUT | 8, 256);
2732 		OMAP_INT_EP("ep9in",   USB_DIR_IN  | 9, 16);
2733 		break;
2734 #endif
2735 
2736 	/* add more modes as needed */
2737 
2738 	default:
2739 		ERR("unsupported fifo_mode #%d\n", fifo_mode);
2740 		return -ENODEV;
2741 	}
2742 	omap_writew(UDC_CFG_LOCK|UDC_SELF_PWR, UDC_SYSCON1);
2743 	INFO("fifo mode %d, %d bytes not used\n", fifo_mode, 2048 - buf);
2744 	return 0;
2745 }
2746 
2747 static int omap_udc_probe(struct platform_device *pdev)
2748 {
2749 	int			status = -ENODEV;
2750 	int			hmc;
2751 	struct usb_phy		*xceiv = NULL;
2752 	const char		*type = NULL;
2753 	struct omap_usb_config	*config = dev_get_platdata(&pdev->dev);
2754 	struct clk		*dc_clk = NULL;
2755 	struct clk		*hhc_clk = NULL;
2756 
2757 	if (cpu_is_omap7xx())
2758 		use_dma = 0;
2759 
2760 	/* NOTE:  "knows" the order of the resources! */
2761 	if (!request_mem_region(pdev->resource[0].start,
2762 			resource_size(&pdev->resource[0]),
2763 			driver_name)) {
2764 		DBG("request_mem_region failed\n");
2765 		return -EBUSY;
2766 	}
2767 
2768 	if (cpu_is_omap16xx()) {
2769 		dc_clk = clk_get(&pdev->dev, "usb_dc_ck");
2770 		hhc_clk = clk_get(&pdev->dev, "usb_hhc_ck");
2771 		BUG_ON(IS_ERR(dc_clk) || IS_ERR(hhc_clk));
2772 		/* can't use omap_udc_enable_clock yet */
2773 		clk_enable(dc_clk);
2774 		clk_enable(hhc_clk);
2775 		udelay(100);
2776 	}
2777 
2778 	if (cpu_is_omap7xx()) {
2779 		dc_clk = clk_get(&pdev->dev, "usb_dc_ck");
2780 		hhc_clk = clk_get(&pdev->dev, "l3_ocpi_ck");
2781 		BUG_ON(IS_ERR(dc_clk) || IS_ERR(hhc_clk));
2782 		/* can't use omap_udc_enable_clock yet */
2783 		clk_enable(dc_clk);
2784 		clk_enable(hhc_clk);
2785 		udelay(100);
2786 	}
2787 
2788 	INFO("OMAP UDC rev %d.%d%s\n",
2789 		omap_readw(UDC_REV) >> 4, omap_readw(UDC_REV) & 0xf,
2790 		config->otg ? ", Mini-AB" : "");
2791 
2792 	/* use the mode given to us by board init code */
2793 	if (cpu_is_omap15xx()) {
2794 		hmc = HMC_1510;
2795 		type = "(unknown)";
2796 
2797 		if (machine_without_vbus_sense()) {
2798 			/* just set up software VBUS detect, and then
2799 			 * later rig it so we always report VBUS.
2800 			 * FIXME without really sensing VBUS, we can't
2801 			 * know when to turn PULLUP_EN on/off; and that
2802 			 * means we always "need" the 48MHz clock.
2803 			 */
2804 			u32 tmp = omap_readl(FUNC_MUX_CTRL_0);
2805 			tmp &= ~VBUS_CTRL_1510;
2806 			omap_writel(tmp, FUNC_MUX_CTRL_0);
2807 			tmp |= VBUS_MODE_1510;
2808 			tmp &= ~VBUS_CTRL_1510;
2809 			omap_writel(tmp, FUNC_MUX_CTRL_0);
2810 		}
2811 	} else {
2812 		/* The transceiver may package some GPIO logic or handle
2813 		 * loopback and/or transceiverless setup; if we find one,
2814 		 * use it.  Except for OTG, we don't _need_ to talk to one;
2815 		 * but not having one probably means no VBUS detection.
2816 		 */
2817 		xceiv = usb_get_phy(USB_PHY_TYPE_USB2);
2818 		if (!IS_ERR_OR_NULL(xceiv))
2819 			type = xceiv->label;
2820 		else if (config->otg) {
2821 			DBG("OTG requires external transceiver!\n");
2822 			goto cleanup0;
2823 		}
2824 
2825 		hmc = HMC_1610;
2826 
2827 		switch (hmc) {
2828 		case 0:			/* POWERUP DEFAULT == 0 */
2829 		case 4:
2830 		case 12:
2831 		case 20:
2832 			if (!cpu_is_omap1710()) {
2833 				type = "integrated";
2834 				break;
2835 			}
2836 			fallthrough;
2837 		case 3:
2838 		case 11:
2839 		case 16:
2840 		case 19:
2841 		case 25:
2842 			if (IS_ERR_OR_NULL(xceiv)) {
2843 				DBG("external transceiver not registered!\n");
2844 				type = "unknown";
2845 			}
2846 			break;
2847 		case 21:			/* internal loopback */
2848 			type = "loopback";
2849 			break;
2850 		case 14:			/* transceiverless */
2851 			if (cpu_is_omap1710())
2852 				goto bad_on_1710;
2853 			fallthrough;
2854 		case 13:
2855 		case 15:
2856 			type = "no";
2857 			break;
2858 
2859 		default:
2860 bad_on_1710:
2861 			ERR("unrecognized UDC HMC mode %d\n", hmc);
2862 			goto cleanup0;
2863 		}
2864 	}
2865 
2866 	INFO("hmc mode %d, %s transceiver\n", hmc, type);
2867 
2868 	/* a "gadget" abstracts/virtualizes the controller */
2869 	status = omap_udc_setup(pdev, xceiv);
2870 	if (status)
2871 		goto cleanup0;
2872 
2873 	xceiv = NULL;
2874 	/* "udc" is now valid */
2875 	pullup_disable(udc);
2876 #if	IS_ENABLED(CONFIG_USB_OHCI_HCD)
2877 	udc->gadget.is_otg = (config->otg != 0);
2878 #endif
2879 
2880 	/* starting with omap1710 es2.0, clear toggle is a separate bit */
2881 	if (omap_readw(UDC_REV) >= 0x61)
2882 		udc->clr_halt = UDC_RESET_EP | UDC_CLRDATA_TOGGLE;
2883 	else
2884 		udc->clr_halt = UDC_RESET_EP;
2885 
2886 	/* USB general purpose IRQ:  ep0, state changes, dma, etc */
2887 	status = devm_request_irq(&pdev->dev, pdev->resource[1].start,
2888 				  omap_udc_irq, 0, driver_name, udc);
2889 	if (status != 0) {
2890 		ERR("can't get irq %d, err %d\n",
2891 			(int) pdev->resource[1].start, status);
2892 		goto cleanup1;
2893 	}
2894 
2895 	/* USB "non-iso" IRQ (PIO for all but ep0) */
2896 	status = devm_request_irq(&pdev->dev, pdev->resource[2].start,
2897 				  omap_udc_pio_irq, 0, "omap_udc pio", udc);
2898 	if (status != 0) {
2899 		ERR("can't get irq %d, err %d\n",
2900 			(int) pdev->resource[2].start, status);
2901 		goto cleanup1;
2902 	}
2903 #ifdef	USE_ISO
2904 	status = devm_request_irq(&pdev->dev, pdev->resource[3].start,
2905 				  omap_udc_iso_irq, 0, "omap_udc iso", udc);
2906 	if (status != 0) {
2907 		ERR("can't get irq %d, err %d\n",
2908 			(int) pdev->resource[3].start, status);
2909 		goto cleanup1;
2910 	}
2911 #endif
2912 	if (cpu_is_omap16xx() || cpu_is_omap7xx()) {
2913 		udc->dc_clk = dc_clk;
2914 		udc->hhc_clk = hhc_clk;
2915 		clk_disable(hhc_clk);
2916 		clk_disable(dc_clk);
2917 	}
2918 
2919 	create_proc_file();
2920 	return usb_add_gadget_udc_release(&pdev->dev, &udc->gadget,
2921 					  omap_udc_release);
2922 
2923 cleanup1:
2924 	kfree(udc);
2925 	udc = NULL;
2926 
2927 cleanup0:
2928 	if (!IS_ERR_OR_NULL(xceiv))
2929 		usb_put_phy(xceiv);
2930 
2931 	if (cpu_is_omap16xx() || cpu_is_omap7xx()) {
2932 		clk_disable(hhc_clk);
2933 		clk_disable(dc_clk);
2934 		clk_put(hhc_clk);
2935 		clk_put(dc_clk);
2936 	}
2937 
2938 	release_mem_region(pdev->resource[0].start,
2939 			   resource_size(&pdev->resource[0]));
2940 
2941 	return status;
2942 }
2943 
2944 static int omap_udc_remove(struct platform_device *pdev)
2945 {
2946 	DECLARE_COMPLETION_ONSTACK(done);
2947 
2948 	udc->done = &done;
2949 
2950 	usb_del_gadget_udc(&udc->gadget);
2951 
2952 	wait_for_completion(&done);
2953 
2954 	release_mem_region(pdev->resource[0].start,
2955 			   resource_size(&pdev->resource[0]));
2956 
2957 	return 0;
2958 }
2959 
2960 /* suspend/resume/wakeup from sysfs (echo > power/state) or when the
2961  * system is forced into deep sleep
2962  *
2963  * REVISIT we should probably reject suspend requests when there's a host
2964  * session active, rather than disconnecting, at least on boards that can
2965  * report VBUS irqs (UDC_DEVSTAT.UDC_ATT).  And in any case, we need to
2966  * make host resumes and VBUS detection trigger OMAP wakeup events; that
2967  * may involve talking to an external transceiver (e.g. isp1301).
2968  */
2969 
2970 static int omap_udc_suspend(struct platform_device *dev, pm_message_t message)
2971 {
2972 	u32	devstat;
2973 
2974 	devstat = omap_readw(UDC_DEVSTAT);
2975 
2976 	/* we're requesting 48 MHz clock if the pullup is enabled
2977 	 * (== we're attached to the host) and we're not suspended,
2978 	 * which would prevent entry to deep sleep...
2979 	 */
2980 	if ((devstat & UDC_ATT) != 0 && (devstat & UDC_SUS) == 0) {
2981 		WARNING("session active; suspend requires disconnect\n");
2982 		omap_pullup(&udc->gadget, 0);
2983 	}
2984 
2985 	return 0;
2986 }
2987 
2988 static int omap_udc_resume(struct platform_device *dev)
2989 {
2990 	DBG("resume + wakeup/SRP\n");
2991 	omap_pullup(&udc->gadget, 1);
2992 
2993 	/* maybe the host would enumerate us if we nudged it */
2994 	msleep(100);
2995 	return omap_wakeup(&udc->gadget);
2996 }
2997 
2998 /*-------------------------------------------------------------------------*/
2999 
3000 static struct platform_driver udc_driver = {
3001 	.probe		= omap_udc_probe,
3002 	.remove		= omap_udc_remove,
3003 	.suspend	= omap_udc_suspend,
3004 	.resume		= omap_udc_resume,
3005 	.driver		= {
3006 		.name	= driver_name,
3007 	},
3008 };
3009 
3010 module_platform_driver(udc_driver);
3011 
3012 MODULE_DESCRIPTION(DRIVER_DESC);
3013 MODULE_LICENSE("GPL");
3014 MODULE_ALIAS("platform:omap_udc");
3015