xref: /openbmc/u-boot/drivers/usb/gadget/at91_udc.c (revision ef64e782)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * from linux:
4  * c94e289f195e: usb: gadget: remove incorrect __init/__exit annotations
5  *
6  * at91_udc -- driver for at91-series USB peripheral controller
7  *
8  * Copyright (C) 2004 by Thomas Rathbone
9  * Copyright (C) 2005 by HP Labs
10  * Copyright (C) 2005 by David Brownell
11  */
12 
13 #undef	VERBOSE_DEBUG
14 #undef	PACKET_TRACE
15 
16 #include <common.h>
17 #include <linux/errno.h>
18 #include <asm/io.h>
19 #include <asm/gpio.h>
20 #include <asm/hardware.h>
21 #include <mach/at91_matrix.h>
22 #include <linux/list.h>
23 #include <linux/usb/ch9.h>
24 #include <linux/usb/gadget.h>
25 #include <linux/usb/at91_udc.h>
26 #include <malloc.h>
27 #include <usb/lin_gadget_compat.h>
28 
29 #include "at91_udc.h"
30 
31 /*
32  * This controller is simple and PIO-only.  It's used in many AT91-series
33  * full speed USB controllers, including the at91rm9200 (arm920T, with MMU),
34  * at91sam926x (arm926ejs, with MMU), and several no-mmu versions.
35  *
36  * This driver expects the board has been wired with two GPIOs supporting
37  * a VBUS sensing IRQ, and a D+ pullup.  (They may be omitted, but the
38  * testing hasn't covered such cases.)
39  *
40  * The pullup is most important (so it's integrated on sam926x parts).  It
41  * provides software control over whether the host enumerates the device.
42  *
43  * The VBUS sensing helps during enumeration, and allows both USB clocks
44  * (and the transceiver) to stay gated off until they're necessary, saving
45  * power.  During USB suspend, the 48 MHz clock is gated off in hardware;
46  * it may also be gated off by software during some Linux sleep states.
47  */
48 
49 #define	DRIVER_VERSION	"3 May 2006"
50 
51 static const char driver_name [] = "at91_udc";
52 static const char * const ep_names[] = {
53 	"ep0",
54 	"ep1",
55 	"ep2",
56 	"ep3-int",
57 	"ep4",
58 	"ep5",
59 };
60 #define ep0name		ep_names[0]
61 
62 #define at91_udp_read(udc, reg) \
63 	__raw_readl((udc)->udp_baseaddr + (reg))
64 #define at91_udp_write(udc, reg, val) \
65 	__raw_writel((val), (udc)->udp_baseaddr + (reg))
66 
67 static struct at91_udc *controller;
68 
69 /*-------------------------------------------------------------------------*/
70 
71 static void done(struct at91_ep *ep, struct at91_request *req, int status)
72 {
73 	unsigned	stopped = ep->stopped;
74 	struct at91_udc	*udc = ep->udc;
75 
76 	list_del_init(&req->queue);
77 	if (req->req.status == -EINPROGRESS)
78 		req->req.status = status;
79 	else
80 		status = req->req.status;
81 	if (status && status != -ESHUTDOWN)
82 		VDBG("%s done %p, status %d\n", ep->ep.name, req, status);
83 
84 	ep->stopped = 1;
85 	spin_unlock(&udc->lock);
86 	req->req.complete(&ep->ep, &req->req);
87 	spin_lock(&udc->lock);
88 	ep->stopped = stopped;
89 
90 	/* ep0 is always ready; other endpoints need a non-empty queue */
91 	if (list_empty(&ep->queue) && ep->int_mask != (1 << 0))
92 		at91_udp_write(udc, AT91_UDP_IDR, ep->int_mask);
93 }
94 
95 /*-------------------------------------------------------------------------*/
96 
97 /* bits indicating OUT fifo has data ready */
98 #define	RX_DATA_READY	(AT91_UDP_RX_DATA_BK0 | AT91_UDP_RX_DATA_BK1)
99 
100 /*
101  * Endpoint FIFO CSR bits have a mix of bits, making it unsafe to just write
102  * back most of the value you just read (because of side effects, including
103  * bits that may change after reading and before writing).
104  *
105  * Except when changing a specific bit, always write values which:
106  *  - clear SET_FX bits (setting them could change something)
107  *  - set CLR_FX bits (clearing them could change something)
108  *
109  * There are also state bits like FORCESTALL, EPEDS, DIR, and EPTYPE
110  * that shouldn't normally be changed.
111  *
112  * NOTE at91sam9260 docs mention synch between UDPCK and MCK clock domains,
113  * implying a need to wait for one write to complete (test relevant bits)
114  * before starting the next write.  This shouldn't be an issue given how
115  * infrequently we write, except maybe for write-then-read idioms.
116  */
117 #define	SET_FX	(AT91_UDP_TXPKTRDY)
118 #define	CLR_FX	(RX_DATA_READY | AT91_UDP_RXSETUP \
119 		| AT91_UDP_STALLSENT | AT91_UDP_TXCOMP)
120 
121 /* pull OUT packet data from the endpoint's fifo */
122 static int read_fifo (struct at91_ep *ep, struct at91_request *req)
123 {
124 	u32 __iomem	*creg = ep->creg;
125 	u8 __iomem	*dreg = ep->creg + (AT91_UDP_FDR(0) - AT91_UDP_CSR(0));
126 	u32		csr;
127 	u8		*buf;
128 	unsigned int	count, bufferspace, is_done;
129 
130 	buf = req->req.buf + req->req.actual;
131 	bufferspace = req->req.length - req->req.actual;
132 
133 	/*
134 	 * there might be nothing to read if ep_queue() calls us,
135 	 * or if we already emptied both pingpong buffers
136 	 */
137 rescan:
138 	csr = __raw_readl(creg);
139 	if ((csr & RX_DATA_READY) == 0)
140 		return 0;
141 
142 	count = (csr & AT91_UDP_RXBYTECNT) >> 16;
143 	if (count > ep->ep.maxpacket)
144 		count = ep->ep.maxpacket;
145 	if (count > bufferspace) {
146 		DBG("%s buffer overflow\n", ep->ep.name);
147 		req->req.status = -EOVERFLOW;
148 		count = bufferspace;
149 	}
150 	__raw_readsb((unsigned long)dreg, buf, count);
151 
152 	/* release and swap pingpong mem bank */
153 	csr |= CLR_FX;
154 	if (ep->is_pingpong) {
155 		if (ep->fifo_bank == 0) {
156 			csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK0);
157 			ep->fifo_bank = 1;
158 		} else {
159 			csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK1);
160 			ep->fifo_bank = 0;
161 		}
162 	} else
163 		csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK0);
164 	__raw_writel(csr, creg);
165 
166 	req->req.actual += count;
167 	is_done = (count < ep->ep.maxpacket);
168 	if (count == bufferspace)
169 		is_done = 1;
170 
171 	PACKET("%s %p out/%d%s\n", ep->ep.name, &req->req, count,
172 			is_done ? " (done)" : "");
173 
174 	/*
175 	 * avoid extra trips through IRQ logic for packets already in
176 	 * the fifo ... maybe preventing an extra (expensive) OUT-NAK
177 	 */
178 	if (is_done)
179 		done(ep, req, 0);
180 	else if (ep->is_pingpong) {
181 		/*
182 		 * One dummy read to delay the code because of a HW glitch:
183 		 * CSR returns bad RXCOUNT when read too soon after updating
184 		 * RX_DATA_BK flags.
185 		 */
186 		csr = __raw_readl(creg);
187 
188 		bufferspace -= count;
189 		buf += count;
190 		goto rescan;
191 	}
192 
193 	return is_done;
194 }
195 
196 /* load fifo for an IN packet */
197 static int write_fifo(struct at91_ep *ep, struct at91_request *req)
198 {
199 	u32 __iomem	*creg = ep->creg;
200 	u32		csr = __raw_readl(creg);
201 	u8 __iomem	*dreg = ep->creg + (AT91_UDP_FDR(0) - AT91_UDP_CSR(0));
202 	unsigned	total, count, is_last;
203 	u8		*buf;
204 
205 	/*
206 	 * TODO: allow for writing two packets to the fifo ... that'll
207 	 * reduce the amount of IN-NAKing, but probably won't affect
208 	 * throughput much.  (Unlike preventing OUT-NAKing!)
209 	 */
210 
211 	/*
212 	 * If ep_queue() calls us, the queue is empty and possibly in
213 	 * odd states like TXCOMP not yet cleared (we do it, saving at
214 	 * least one IRQ) or the fifo not yet being free.  Those aren't
215 	 * issues normally (IRQ handler fast path).
216 	 */
217 	if (unlikely(csr & (AT91_UDP_TXCOMP | AT91_UDP_TXPKTRDY))) {
218 		if (csr & AT91_UDP_TXCOMP) {
219 			csr |= CLR_FX;
220 			csr &= ~(SET_FX | AT91_UDP_TXCOMP);
221 			__raw_writel(csr, creg);
222 			csr = __raw_readl(creg);
223 		}
224 		if (csr & AT91_UDP_TXPKTRDY)
225 			return 0;
226 	}
227 
228 	buf = req->req.buf + req->req.actual;
229 	prefetch(buf);
230 	total = req->req.length - req->req.actual;
231 	if (ep->ep.maxpacket < total) {
232 		count = ep->ep.maxpacket;
233 		is_last = 0;
234 	} else {
235 		count = total;
236 		is_last = (count < ep->ep.maxpacket) || !req->req.zero;
237 	}
238 
239 	/*
240 	 * Write the packet, maybe it's a ZLP.
241 	 *
242 	 * NOTE:  incrementing req->actual before we receive the ACK means
243 	 * gadget driver IN bytecounts can be wrong in fault cases.  That's
244 	 * fixable with PIO drivers like this one (save "count" here, and
245 	 * do the increment later on TX irq), but not for most DMA hardware.
246 	 *
247 	 * So all gadget drivers must accept that potential error.  Some
248 	 * hardware supports precise fifo status reporting, letting them
249 	 * recover when the actual bytecount matters (e.g. for USB Test
250 	 * and Measurement Class devices).
251 	 */
252 	__raw_writesb((unsigned long)dreg, buf, count);
253 	csr &= ~SET_FX;
254 	csr |= CLR_FX | AT91_UDP_TXPKTRDY;
255 	__raw_writel(csr, creg);
256 	req->req.actual += count;
257 
258 	PACKET("%s %p in/%d%s\n", ep->ep.name, &req->req, count,
259 			is_last ? " (done)" : "");
260 	if (is_last)
261 		done(ep, req, 0);
262 	return is_last;
263 }
264 
265 static void nuke(struct at91_ep *ep, int status)
266 {
267 	struct at91_request *req;
268 
269 	/* terminate any request in the queue */
270 	ep->stopped = 1;
271 	if (list_empty(&ep->queue))
272 		return;
273 
274 	VDBG("%s %s\n", __func__, ep->ep.name);
275 	while (!list_empty(&ep->queue)) {
276 		req = list_entry(ep->queue.next, struct at91_request, queue);
277 		done(ep, req, status);
278 	}
279 }
280 
281 /*-------------------------------------------------------------------------*/
282 
283 static int at91_ep_enable(struct usb_ep *_ep,
284 				const struct usb_endpoint_descriptor *desc)
285 {
286 	struct at91_ep	*ep = container_of(_ep, struct at91_ep, ep);
287 	struct at91_udc *udc;
288 	u16		maxpacket;
289 	u32		tmp;
290 	unsigned long	flags;
291 
292 	if (!_ep || !ep
293 			|| !desc || _ep->name == ep0name
294 			|| desc->bDescriptorType != USB_DT_ENDPOINT
295 			|| (maxpacket = usb_endpoint_maxp(desc)) == 0
296 			|| maxpacket > ep->maxpacket) {
297 		DBG("bad ep or descriptor\n");
298 		return -EINVAL;
299 	}
300 
301 	udc = ep->udc;
302 	if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN) {
303 		DBG("bogus device state\n");
304 		return -ESHUTDOWN;
305 	}
306 
307 	tmp = usb_endpoint_type(desc);
308 	switch (tmp) {
309 	case USB_ENDPOINT_XFER_CONTROL:
310 		DBG("only one control endpoint\n");
311 		return -EINVAL;
312 	case USB_ENDPOINT_XFER_INT:
313 		if (maxpacket > 64)
314 			goto bogus_max;
315 		break;
316 	case USB_ENDPOINT_XFER_BULK:
317 		switch (maxpacket) {
318 		case 8:
319 		case 16:
320 		case 32:
321 		case 64:
322 			goto ok;
323 		}
324 bogus_max:
325 		DBG("bogus maxpacket %d\n", maxpacket);
326 		return -EINVAL;
327 	case USB_ENDPOINT_XFER_ISOC:
328 		if (!ep->is_pingpong) {
329 			DBG("iso requires double buffering\n");
330 			return -EINVAL;
331 		}
332 		break;
333 	}
334 
335 ok:
336 	spin_lock_irqsave(&udc->lock, flags);
337 
338 	/* initialize endpoint to match this descriptor */
339 	ep->is_in = usb_endpoint_dir_in(desc);
340 	ep->is_iso = (tmp == USB_ENDPOINT_XFER_ISOC);
341 	ep->stopped = 0;
342 	if (ep->is_in)
343 		tmp |= 0x04;
344 	tmp <<= 8;
345 	tmp |= AT91_UDP_EPEDS;
346 	__raw_writel(tmp, ep->creg);
347 
348 	ep->ep.maxpacket = maxpacket;
349 
350 	/*
351 	 * reset/init endpoint fifo.  NOTE:  leaves fifo_bank alone,
352 	 * since endpoint resets don't reset hw pingpong state.
353 	 */
354 	at91_udp_write(udc, AT91_UDP_RST_EP, ep->int_mask);
355 	at91_udp_write(udc, AT91_UDP_RST_EP, 0);
356 
357 	spin_unlock_irqrestore(&udc->lock, flags);
358 	return 0;
359 }
360 
361 static int at91_ep_disable (struct usb_ep * _ep)
362 {
363 	struct at91_ep	*ep = container_of(_ep, struct at91_ep, ep);
364 	struct at91_udc	*udc = ep->udc;
365 	unsigned long	flags;
366 
367 	if (ep == &ep->udc->ep[0])
368 		return -EINVAL;
369 
370 	spin_lock_irqsave(&udc->lock, flags);
371 
372 	nuke(ep, -ESHUTDOWN);
373 
374 	/* restore the endpoint's pristine config */
375 	ep->ep.desc = NULL;
376 	ep->ep.maxpacket = ep->maxpacket;
377 
378 	/* reset fifos and endpoint */
379 	if (ep->udc->clocked) {
380 		at91_udp_write(udc, AT91_UDP_RST_EP, ep->int_mask);
381 		at91_udp_write(udc, AT91_UDP_RST_EP, 0);
382 		__raw_writel(0, ep->creg);
383 	}
384 
385 	spin_unlock_irqrestore(&udc->lock, flags);
386 	return 0;
387 }
388 
389 /*
390  * this is a PIO-only driver, so there's nothing
391  * interesting for request or buffer allocation.
392  */
393 
394 static struct usb_request *
395 at91_ep_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
396 {
397 	struct at91_request *req;
398 
399 	req = kzalloc(sizeof (struct at91_request), gfp_flags);
400 	if (!req)
401 		return NULL;
402 
403 	INIT_LIST_HEAD(&req->queue);
404 	return &req->req;
405 }
406 
407 static void at91_ep_free_request(struct usb_ep *_ep, struct usb_request *_req)
408 {
409 	struct at91_request *req;
410 
411 	req = container_of(_req, struct at91_request, req);
412 	BUG_ON(!list_empty(&req->queue));
413 	kfree(req);
414 }
415 
416 static int at91_ep_queue(struct usb_ep *_ep,
417 			struct usb_request *_req, gfp_t gfp_flags)
418 {
419 	struct at91_request	*req;
420 	struct at91_ep		*ep;
421 	struct at91_udc		*udc;
422 	int			status;
423 	unsigned long		flags;
424 
425 	req = container_of(_req, struct at91_request, req);
426 	ep = container_of(_ep, struct at91_ep, ep);
427 
428 	if (!_req || !_req->complete
429 			|| !_req->buf || !list_empty(&req->queue)) {
430 		DBG("invalid request\n");
431 		return -EINVAL;
432 	}
433 
434 	if (!_ep || (!ep->ep.desc && ep->ep.name != ep0name)) {
435 		DBG("invalid ep\n");
436 		return -EINVAL;
437 	}
438 
439 	udc = ep->udc;
440 
441 	if (!udc || !udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN) {
442 		DBG("invalid device\n");
443 		return -EINVAL;
444 	}
445 
446 	_req->status = -EINPROGRESS;
447 	_req->actual = 0;
448 
449 	spin_lock_irqsave(&udc->lock, flags);
450 
451 	/* try to kickstart any empty and idle queue */
452 	if (list_empty(&ep->queue) && !ep->stopped) {
453 		int	is_ep0;
454 
455 		/*
456 		 * If this control request has a non-empty DATA stage, this
457 		 * will start that stage.  It works just like a non-control
458 		 * request (until the status stage starts, maybe early).
459 		 *
460 		 * If the data stage is empty, then this starts a successful
461 		 * IN/STATUS stage.  (Unsuccessful ones use set_halt.)
462 		 */
463 		is_ep0 = (ep->ep.name == ep0name);
464 		if (is_ep0) {
465 			u32	tmp;
466 
467 			if (!udc->req_pending) {
468 				status = -EINVAL;
469 				goto done;
470 			}
471 
472 			/*
473 			 * defer changing CONFG until after the gadget driver
474 			 * reconfigures the endpoints.
475 			 */
476 			if (udc->wait_for_config_ack) {
477 				tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT);
478 				tmp ^= AT91_UDP_CONFG;
479 				VDBG("toggle config\n");
480 				at91_udp_write(udc, AT91_UDP_GLB_STAT, tmp);
481 			}
482 			if (req->req.length == 0) {
483 ep0_in_status:
484 				PACKET("ep0 in/status\n");
485 				status = 0;
486 				tmp = __raw_readl(ep->creg);
487 				tmp &= ~SET_FX;
488 				tmp |= CLR_FX | AT91_UDP_TXPKTRDY;
489 				__raw_writel(tmp, ep->creg);
490 				udc->req_pending = 0;
491 				goto done;
492 			}
493 		}
494 
495 		if (ep->is_in)
496 			status = write_fifo(ep, req);
497 		else {
498 			status = read_fifo(ep, req);
499 
500 			/* IN/STATUS stage is otherwise triggered by irq */
501 			if (status && is_ep0)
502 				goto ep0_in_status;
503 		}
504 	} else
505 		status = 0;
506 
507 	if (req && !status) {
508 		list_add_tail (&req->queue, &ep->queue);
509 		at91_udp_write(udc, AT91_UDP_IER, ep->int_mask);
510 	}
511 done:
512 	spin_unlock_irqrestore(&udc->lock, flags);
513 	return (status < 0) ? status : 0;
514 }
515 
516 static int at91_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
517 {
518 	struct at91_ep		*ep;
519 	struct at91_request	*req;
520 	unsigned long		flags;
521 
522 	ep = container_of(_ep, struct at91_ep, ep);
523 	if (!_ep || ep->ep.name == ep0name)
524 		return -EINVAL;
525 
526 	spin_lock_irqsave(&udc->lock, flags);
527 
528 	/* make sure it's actually queued on this endpoint */
529 	list_for_each_entry (req, &ep->queue, queue) {
530 		if (&req->req == _req)
531 			break;
532 	}
533 	if (&req->req != _req) {
534 		spin_unlock_irqrestore(&udc->lock, flags);
535 		return -EINVAL;
536 	}
537 
538 	done(ep, req, -ECONNRESET);
539 	spin_unlock_irqrestore(&udc->lock, flags);
540 	return 0;
541 }
542 
543 static int at91_ep_set_halt(struct usb_ep *_ep, int value)
544 {
545 	struct at91_ep	*ep = container_of(_ep, struct at91_ep, ep);
546 	struct at91_udc	*udc = ep->udc;
547 	u32 __iomem	*creg;
548 	u32		csr;
549 	unsigned long	flags;
550 	int		status = 0;
551 
552 	if (!_ep || ep->is_iso || !ep->udc->clocked)
553 		return -EINVAL;
554 
555 	creg = ep->creg;
556 	spin_lock_irqsave(&udc->lock, flags);
557 
558 	csr = __raw_readl(creg);
559 
560 	/*
561 	 * fail with still-busy IN endpoints, ensuring correct sequencing
562 	 * of data tx then stall.  note that the fifo rx bytecount isn't
563 	 * completely accurate as a tx bytecount.
564 	 */
565 	if (ep->is_in && (!list_empty(&ep->queue) || (csr >> 16) != 0))
566 		status = -EAGAIN;
567 	else {
568 		csr |= CLR_FX;
569 		csr &= ~SET_FX;
570 		if (value) {
571 			csr |= AT91_UDP_FORCESTALL;
572 			VDBG("halt %s\n", ep->ep.name);
573 		} else {
574 			at91_udp_write(udc, AT91_UDP_RST_EP, ep->int_mask);
575 			at91_udp_write(udc, AT91_UDP_RST_EP, 0);
576 			csr &= ~AT91_UDP_FORCESTALL;
577 		}
578 		__raw_writel(csr, creg);
579 	}
580 
581 	spin_unlock_irqrestore(&udc->lock, flags);
582 	return status;
583 }
584 
585 static const struct usb_ep_ops at91_ep_ops = {
586 	.enable		= at91_ep_enable,
587 	.disable	= at91_ep_disable,
588 	.alloc_request	= at91_ep_alloc_request,
589 	.free_request	= at91_ep_free_request,
590 	.queue		= at91_ep_queue,
591 	.dequeue	= at91_ep_dequeue,
592 	.set_halt	= at91_ep_set_halt,
593 	/* there's only imprecise fifo status reporting */
594 };
595 
596 /*-------------------------------------------------------------------------*/
597 
598 static int at91_get_frame(struct usb_gadget *gadget)
599 {
600 	struct at91_udc *udc = to_udc(gadget);
601 
602 	if (!to_udc(gadget)->clocked)
603 		return -EINVAL;
604 	return at91_udp_read(udc, AT91_UDP_FRM_NUM) & AT91_UDP_NUM;
605 }
606 
607 static int at91_wakeup(struct usb_gadget *gadget)
608 {
609 	struct at91_udc	*udc = to_udc(gadget);
610 	u32		glbstate;
611 	int		status = -EINVAL;
612 	unsigned long	flags;
613 
614 	DBG("%s\n", __func__ );
615 	spin_lock_irqsave(&udc->lock, flags);
616 
617 	if (!udc->clocked || !udc->suspended)
618 		goto done;
619 
620 	/* NOTE:  some "early versions" handle ESR differently ... */
621 
622 	glbstate = at91_udp_read(udc, AT91_UDP_GLB_STAT);
623 	if (!(glbstate & AT91_UDP_ESR))
624 		goto done;
625 	glbstate |= AT91_UDP_ESR;
626 	at91_udp_write(udc, AT91_UDP_GLB_STAT, glbstate);
627 
628 done:
629 	spin_unlock_irqrestore(&udc->lock, flags);
630 	return status;
631 }
632 
633 /* reinit == restore initial software state */
634 static void udc_reinit(struct at91_udc *udc)
635 {
636 	u32 i;
637 
638 	INIT_LIST_HEAD(&udc->gadget.ep_list);
639 	INIT_LIST_HEAD(&udc->gadget.ep0->ep_list);
640 
641 	for (i = 0; i < NUM_ENDPOINTS; i++) {
642 		struct at91_ep *ep = &udc->ep[i];
643 
644 		if (i != 0)
645 			list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
646 		ep->ep.desc = NULL;
647 		ep->stopped = 0;
648 		ep->fifo_bank = 0;
649 		usb_ep_set_maxpacket_limit(&ep->ep, ep->maxpacket);
650 		ep->creg = (void __iomem *) udc->udp_baseaddr + AT91_UDP_CSR(i);
651 		/* initialize one queue per endpoint */
652 		INIT_LIST_HEAD(&ep->queue);
653 	}
654 }
655 
656 static void reset_gadget(struct at91_udc *udc)
657 {
658 	struct usb_gadget_driver *driver = udc->driver;
659 	int i;
660 
661 	if (udc->gadget.speed == USB_SPEED_UNKNOWN)
662 		driver = NULL;
663 	udc->gadget.speed = USB_SPEED_UNKNOWN;
664 	udc->suspended = 0;
665 
666 	for (i = 0; i < NUM_ENDPOINTS; i++) {
667 		struct at91_ep *ep = &udc->ep[i];
668 
669 		ep->stopped = 1;
670 		nuke(ep, -ESHUTDOWN);
671 	}
672 	if (driver) {
673 		spin_unlock(&udc->lock);
674 		udc->driver->disconnect(&udc->gadget);
675 		spin_lock(&udc->lock);
676 	}
677 
678 	udc_reinit(udc);
679 }
680 
681 static void stop_activity(struct at91_udc *udc)
682 {
683 	struct usb_gadget_driver *driver = udc->driver;
684 	int i;
685 
686 	if (udc->gadget.speed == USB_SPEED_UNKNOWN)
687 		driver = NULL;
688 	udc->gadget.speed = USB_SPEED_UNKNOWN;
689 	udc->suspended = 0;
690 
691 	for (i = 0; i < NUM_ENDPOINTS; i++) {
692 		struct at91_ep *ep = &udc->ep[i];
693 		ep->stopped = 1;
694 		nuke(ep, -ESHUTDOWN);
695 	}
696 	if (driver) {
697 		spin_unlock(&udc->lock);
698 		driver->disconnect(&udc->gadget);
699 		spin_lock(&udc->lock);
700 	}
701 
702 	udc_reinit(udc);
703 }
704 
705 static void clk_on(struct at91_udc *udc)
706 {
707 	if (udc->clocked)
708 		return;
709 	udc->clocked = 1;
710 }
711 
712 static void clk_off(struct at91_udc *udc)
713 {
714 	if (!udc->clocked)
715 		return;
716 	udc->clocked = 0;
717 	udc->gadget.speed = USB_SPEED_UNKNOWN;
718 }
719 
720 /*
721  * activate/deactivate link with host; minimize power usage for
722  * inactive links by cutting clocks and transceiver power.
723  */
724 static void pullup(struct at91_udc *udc, int is_on)
725 {
726 	if (!udc->enabled || !udc->vbus)
727 		is_on = 0;
728 	DBG("%sactive\n", is_on ? "" : "in");
729 
730 	if (is_on) {
731 		clk_on(udc);
732 		at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_RXRSM);
733 		at91_udp_write(udc, AT91_UDP_TXVC, 0);
734 	} else {
735 		stop_activity(udc);
736 		at91_udp_write(udc, AT91_UDP_IDR, AT91_UDP_RXRSM);
737 		at91_udp_write(udc, AT91_UDP_TXVC, AT91_UDP_TXVC_TXVDIS);
738 		clk_off(udc);
739 	}
740 
741 	if (udc->caps && udc->caps->pullup)
742 		udc->caps->pullup(udc, is_on);
743 }
744 
745 /* vbus is here!  turn everything on that's ready */
746 static int at91_vbus_session(struct usb_gadget *gadget, int is_active)
747 {
748 	struct at91_udc	*udc = to_udc(gadget);
749 	unsigned long	flags;
750 
751 	/* VDBG("vbus %s\n", is_active ? "on" : "off"); */
752 	spin_lock_irqsave(&udc->lock, flags);
753 	udc->vbus = (is_active != 0);
754 	if (udc->driver)
755 		pullup(udc, is_active);
756 	else
757 		pullup(udc, 0);
758 	spin_unlock_irqrestore(&udc->lock, flags);
759 	return 0;
760 }
761 
762 static int at91_pullup(struct usb_gadget *gadget, int is_on)
763 {
764 	struct at91_udc	*udc = to_udc(gadget);
765 	unsigned long	flags;
766 
767 	spin_lock_irqsave(&udc->lock, flags);
768 	udc->enabled = is_on = !!is_on;
769 	pullup(udc, is_on);
770 	spin_unlock_irqrestore(&udc->lock, flags);
771 	return 0;
772 }
773 
774 static int at91_set_selfpowered(struct usb_gadget *gadget, int is_on)
775 {
776 	struct at91_udc	*udc = to_udc(gadget);
777 	unsigned long	flags;
778 
779 	spin_lock_irqsave(&udc->lock, flags);
780 	udc->selfpowered = (is_on != 0);
781 	spin_unlock_irqrestore(&udc->lock, flags);
782 	return 0;
783 }
784 
785 static int at91_start(struct usb_gadget *gadget,
786 		struct usb_gadget_driver *driver);
787 static int at91_stop(struct usb_gadget *gadget);
788 
789 static const struct usb_gadget_ops at91_udc_ops = {
790 	.get_frame		= at91_get_frame,
791 	.wakeup			= at91_wakeup,
792 	.set_selfpowered	= at91_set_selfpowered,
793 	.vbus_session		= at91_vbus_session,
794 	.pullup			= at91_pullup,
795 	.udc_start		= at91_start,
796 	.udc_stop		= at91_stop,
797 
798 	/*
799 	 * VBUS-powered devices may also also want to support bigger
800 	 * power budgets after an appropriate SET_CONFIGURATION.
801 	 */
802 	/* .vbus_power		= at91_vbus_power, */
803 };
804 
805 /*-------------------------------------------------------------------------*/
806 
807 static int handle_ep(struct at91_ep *ep)
808 {
809 	struct at91_request	*req;
810 	u32 __iomem		*creg = ep->creg;
811 	u32			csr = __raw_readl(creg);
812 
813 	if (!list_empty(&ep->queue))
814 		req = list_entry(ep->queue.next,
815 			struct at91_request, queue);
816 	else
817 		req = NULL;
818 
819 	if (ep->is_in) {
820 		if (csr & (AT91_UDP_STALLSENT | AT91_UDP_TXCOMP)) {
821 			csr |= CLR_FX;
822 			csr &= ~(SET_FX | AT91_UDP_STALLSENT | AT91_UDP_TXCOMP);
823 			__raw_writel(csr, creg);
824 		}
825 		if (req)
826 			return write_fifo(ep, req);
827 
828 	} else {
829 		if (csr & AT91_UDP_STALLSENT) {
830 			/* STALLSENT bit == ISOERR */
831 			if (ep->is_iso && req)
832 				req->req.status = -EILSEQ;
833 			csr |= CLR_FX;
834 			csr &= ~(SET_FX | AT91_UDP_STALLSENT);
835 			__raw_writel(csr, creg);
836 			csr = __raw_readl(creg);
837 		}
838 		if (req && (csr & RX_DATA_READY))
839 			return read_fifo(ep, req);
840 	}
841 	return 0;
842 }
843 
844 union setup {
845 	u8			raw[8];
846 	struct usb_ctrlrequest	r;
847 };
848 
849 static void handle_setup(struct at91_udc *udc, struct at91_ep *ep, u32 csr)
850 {
851 	u32 __iomem	*creg = ep->creg;
852 	u8 __iomem	*dreg = ep->creg + (AT91_UDP_FDR(0) - AT91_UDP_CSR(0));
853 	unsigned	rxcount, i = 0;
854 	u32		tmp;
855 	union setup	pkt;
856 	int		status = 0;
857 
858 	/* read and ack SETUP; hard-fail for bogus packets */
859 	rxcount = (csr & AT91_UDP_RXBYTECNT) >> 16;
860 	if (likely(rxcount == 8)) {
861 		while (rxcount--)
862 			pkt.raw[i++] = __raw_readb(dreg);
863 		if (pkt.r.bRequestType & USB_DIR_IN) {
864 			csr |= AT91_UDP_DIR;
865 			ep->is_in = 1;
866 		} else {
867 			csr &= ~AT91_UDP_DIR;
868 			ep->is_in = 0;
869 		}
870 	} else {
871 		/* REVISIT this happens sometimes under load; why?? */
872 		ERR("SETUP len %d, csr %08x\n", rxcount, csr);
873 		status = -EINVAL;
874 	}
875 	csr |= CLR_FX;
876 	csr &= ~(SET_FX | AT91_UDP_RXSETUP);
877 	__raw_writel(csr, creg);
878 	udc->wait_for_addr_ack = 0;
879 	udc->wait_for_config_ack = 0;
880 	ep->stopped = 0;
881 	if (unlikely(status != 0))
882 		goto stall;
883 
884 #define w_index		le16_to_cpu(pkt.r.wIndex)
885 #define w_value		le16_to_cpu(pkt.r.wValue)
886 #define w_length	le16_to_cpu(pkt.r.wLength)
887 
888 	VDBG("SETUP %02x.%02x v%04x i%04x l%04x\n",
889 			pkt.r.bRequestType, pkt.r.bRequest,
890 			w_value, w_index, w_length);
891 
892 	/*
893 	 * A few standard requests get handled here, ones that touch
894 	 * hardware ... notably for device and endpoint features.
895 	 */
896 	udc->req_pending = 1;
897 	csr = __raw_readl(creg);
898 	csr |= CLR_FX;
899 	csr &= ~SET_FX;
900 	switch ((pkt.r.bRequestType << 8) | pkt.r.bRequest) {
901 
902 	case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
903 			| USB_REQ_SET_ADDRESS:
904 		__raw_writel(csr | AT91_UDP_TXPKTRDY, creg);
905 		udc->addr = w_value;
906 		udc->wait_for_addr_ack = 1;
907 		udc->req_pending = 0;
908 		/* FADDR is set later, when we ack host STATUS */
909 		return;
910 
911 	case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
912 			| USB_REQ_SET_CONFIGURATION:
913 		tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT) & AT91_UDP_CONFG;
914 		if (pkt.r.wValue)
915 			udc->wait_for_config_ack = (tmp == 0);
916 		else
917 			udc->wait_for_config_ack = (tmp != 0);
918 		if (udc->wait_for_config_ack)
919 			VDBG("wait for config\n");
920 		/* CONFG is toggled later, if gadget driver succeeds */
921 		break;
922 
923 	/*
924 	 * Hosts may set or clear remote wakeup status, and
925 	 * devices may report they're VBUS powered.
926 	 */
927 	case ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
928 			| USB_REQ_GET_STATUS:
929 		tmp = (udc->selfpowered << USB_DEVICE_SELF_POWERED);
930 		if (at91_udp_read(udc, AT91_UDP_GLB_STAT) & AT91_UDP_ESR)
931 			tmp |= (1 << USB_DEVICE_REMOTE_WAKEUP);
932 		PACKET("get device status\n");
933 		__raw_writeb(tmp, dreg);
934 		__raw_writeb(0, dreg);
935 		goto write_in;
936 		/* then STATUS starts later, automatically */
937 	case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
938 			| USB_REQ_SET_FEATURE:
939 		if (w_value != USB_DEVICE_REMOTE_WAKEUP)
940 			goto stall;
941 		tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT);
942 		tmp |= AT91_UDP_ESR;
943 		at91_udp_write(udc, AT91_UDP_GLB_STAT, tmp);
944 		goto succeed;
945 	case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
946 			| USB_REQ_CLEAR_FEATURE:
947 		if (w_value != USB_DEVICE_REMOTE_WAKEUP)
948 			goto stall;
949 		tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT);
950 		tmp &= ~AT91_UDP_ESR;
951 		at91_udp_write(udc, AT91_UDP_GLB_STAT, tmp);
952 		goto succeed;
953 
954 	/*
955 	 * Interfaces have no feature settings; this is pretty useless.
956 	 * we won't even insist the interface exists...
957 	 */
958 	case ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_INTERFACE) << 8)
959 			| USB_REQ_GET_STATUS:
960 		PACKET("get interface status\n");
961 		__raw_writeb(0, dreg);
962 		__raw_writeb(0, dreg);
963 		goto write_in;
964 		/* then STATUS starts later, automatically */
965 	case ((USB_TYPE_STANDARD|USB_RECIP_INTERFACE) << 8)
966 			| USB_REQ_SET_FEATURE:
967 	case ((USB_TYPE_STANDARD|USB_RECIP_INTERFACE) << 8)
968 			| USB_REQ_CLEAR_FEATURE:
969 		goto stall;
970 
971 	/*
972 	 * Hosts may clear bulk/intr endpoint halt after the gadget
973 	 * driver sets it (not widely used); or set it (for testing)
974 	 */
975 	case ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_ENDPOINT) << 8)
976 			| USB_REQ_GET_STATUS:
977 		tmp = w_index & USB_ENDPOINT_NUMBER_MASK;
978 		ep = &udc->ep[tmp];
979 		if (tmp >= NUM_ENDPOINTS || (tmp && !ep->ep.desc))
980 			goto stall;
981 
982 		if (tmp) {
983 			if ((w_index & USB_DIR_IN)) {
984 				if (!ep->is_in)
985 					goto stall;
986 			} else if (ep->is_in)
987 				goto stall;
988 		}
989 		PACKET("get %s status\n", ep->ep.name);
990 		if (__raw_readl(ep->creg) & AT91_UDP_FORCESTALL)
991 			tmp = (1 << USB_ENDPOINT_HALT);
992 		else
993 			tmp = 0;
994 		__raw_writeb(tmp, dreg);
995 		__raw_writeb(0, dreg);
996 		goto write_in;
997 		/* then STATUS starts later, automatically */
998 	case ((USB_TYPE_STANDARD|USB_RECIP_ENDPOINT) << 8)
999 			| USB_REQ_SET_FEATURE:
1000 		tmp = w_index & USB_ENDPOINT_NUMBER_MASK;
1001 		ep = &udc->ep[tmp];
1002 		if (w_value != USB_ENDPOINT_HALT || tmp >= NUM_ENDPOINTS)
1003 			goto stall;
1004 		if (!ep->ep.desc || ep->is_iso)
1005 			goto stall;
1006 		if ((w_index & USB_DIR_IN)) {
1007 			if (!ep->is_in)
1008 				goto stall;
1009 		} else if (ep->is_in)
1010 			goto stall;
1011 
1012 		tmp = __raw_readl(ep->creg);
1013 		tmp &= ~SET_FX;
1014 		tmp |= CLR_FX | AT91_UDP_FORCESTALL;
1015 		__raw_writel(tmp, ep->creg);
1016 		goto succeed;
1017 	case ((USB_TYPE_STANDARD|USB_RECIP_ENDPOINT) << 8)
1018 			| USB_REQ_CLEAR_FEATURE:
1019 		tmp = w_index & USB_ENDPOINT_NUMBER_MASK;
1020 		ep = &udc->ep[tmp];
1021 		if (w_value != USB_ENDPOINT_HALT || tmp >= NUM_ENDPOINTS)
1022 			goto stall;
1023 		if (tmp == 0)
1024 			goto succeed;
1025 		if (!ep->ep.desc || ep->is_iso)
1026 			goto stall;
1027 		if ((w_index & USB_DIR_IN)) {
1028 			if (!ep->is_in)
1029 				goto stall;
1030 		} else if (ep->is_in)
1031 			goto stall;
1032 
1033 		at91_udp_write(udc, AT91_UDP_RST_EP, ep->int_mask);
1034 		at91_udp_write(udc, AT91_UDP_RST_EP, 0);
1035 		tmp = __raw_readl(ep->creg);
1036 		tmp |= CLR_FX;
1037 		tmp &= ~(SET_FX | AT91_UDP_FORCESTALL);
1038 		__raw_writel(tmp, ep->creg);
1039 		if (!list_empty(&ep->queue))
1040 			handle_ep(ep);
1041 		goto succeed;
1042 	}
1043 
1044 #undef w_value
1045 #undef w_index
1046 #undef w_length
1047 
1048 	/* pass request up to the gadget driver */
1049 	if (udc->driver) {
1050 		spin_unlock(&udc->lock);
1051 		status = udc->driver->setup(&udc->gadget, &pkt.r);
1052 		spin_lock(&udc->lock);
1053 	}
1054 	else
1055 		status = -ENODEV;
1056 	if (status < 0) {
1057 stall:
1058 		VDBG("req %02x.%02x protocol STALL; stat %d\n",
1059 				pkt.r.bRequestType, pkt.r.bRequest, status);
1060 		csr |= AT91_UDP_FORCESTALL;
1061 		__raw_writel(csr, creg);
1062 		udc->req_pending = 0;
1063 	}
1064 	return;
1065 
1066 succeed:
1067 	/* immediate successful (IN) STATUS after zero length DATA */
1068 	PACKET("ep0 in/status\n");
1069 write_in:
1070 	csr |= AT91_UDP_TXPKTRDY;
1071 	__raw_writel(csr, creg);
1072 	udc->req_pending = 0;
1073 }
1074 
1075 static void handle_ep0(struct at91_udc *udc)
1076 {
1077 	struct at91_ep		*ep0 = &udc->ep[0];
1078 	u32 __iomem		*creg = ep0->creg;
1079 	u32			csr = __raw_readl(creg);
1080 	struct at91_request	*req;
1081 
1082 	if (unlikely(csr & AT91_UDP_STALLSENT)) {
1083 		nuke(ep0, -EPROTO);
1084 		udc->req_pending = 0;
1085 		csr |= CLR_FX;
1086 		csr &= ~(SET_FX | AT91_UDP_STALLSENT | AT91_UDP_FORCESTALL);
1087 		__raw_writel(csr, creg);
1088 		VDBG("ep0 stalled\n");
1089 		csr = __raw_readl(creg);
1090 	}
1091 	if (csr & AT91_UDP_RXSETUP) {
1092 		nuke(ep0, 0);
1093 		udc->req_pending = 0;
1094 		handle_setup(udc, ep0, csr);
1095 		return;
1096 	}
1097 
1098 	if (list_empty(&ep0->queue))
1099 		req = NULL;
1100 	else
1101 		req = list_entry(ep0->queue.next, struct at91_request, queue);
1102 
1103 	/* host ACKed an IN packet that we sent */
1104 	if (csr & AT91_UDP_TXCOMP) {
1105 		csr |= CLR_FX;
1106 		csr &= ~(SET_FX | AT91_UDP_TXCOMP);
1107 
1108 		/* write more IN DATA? */
1109 		if (req && ep0->is_in) {
1110 			if (handle_ep(ep0))
1111 				udc->req_pending = 0;
1112 
1113 		/*
1114 		 * Ack after:
1115 		 *  - last IN DATA packet (including GET_STATUS)
1116 		 *  - IN/STATUS for OUT DATA
1117 		 *  - IN/STATUS for any zero-length DATA stage
1118 		 * except for the IN DATA case, the host should send
1119 		 * an OUT status later, which we'll ack.
1120 		 */
1121 		} else {
1122 			udc->req_pending = 0;
1123 			__raw_writel(csr, creg);
1124 
1125 			/*
1126 			 * SET_ADDRESS takes effect only after the STATUS
1127 			 * (to the original address) gets acked.
1128 			 */
1129 			if (udc->wait_for_addr_ack) {
1130 				u32	tmp;
1131 
1132 				at91_udp_write(udc, AT91_UDP_FADDR,
1133 						AT91_UDP_FEN | udc->addr);
1134 				tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT);
1135 				tmp &= ~AT91_UDP_FADDEN;
1136 				if (udc->addr)
1137 					tmp |= AT91_UDP_FADDEN;
1138 				at91_udp_write(udc, AT91_UDP_GLB_STAT, tmp);
1139 
1140 				udc->wait_for_addr_ack = 0;
1141 				VDBG("address %d\n", udc->addr);
1142 			}
1143 		}
1144 	}
1145 
1146 	/* OUT packet arrived ... */
1147 	else if (csr & AT91_UDP_RX_DATA_BK0) {
1148 		csr |= CLR_FX;
1149 		csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK0);
1150 
1151 		/* OUT DATA stage */
1152 		if (!ep0->is_in) {
1153 			if (req) {
1154 				if (handle_ep(ep0)) {
1155 					/* send IN/STATUS */
1156 					PACKET("ep0 in/status\n");
1157 					csr = __raw_readl(creg);
1158 					csr &= ~SET_FX;
1159 					csr |= CLR_FX | AT91_UDP_TXPKTRDY;
1160 					__raw_writel(csr, creg);
1161 					udc->req_pending = 0;
1162 				}
1163 			} else if (udc->req_pending) {
1164 				/*
1165 				 * AT91 hardware has a hard time with this
1166 				 * "deferred response" mode for control-OUT
1167 				 * transfers.  (For control-IN it's fine.)
1168 				 *
1169 				 * The normal solution leaves OUT data in the
1170 				 * fifo until the gadget driver is ready.
1171 				 * We couldn't do that here without disabling
1172 				 * the IRQ that tells about SETUP packets,
1173 				 * e.g. when the host gets impatient...
1174 				 *
1175 				 * Working around it by copying into a buffer
1176 				 * would almost be a non-deferred response,
1177 				 * except that it wouldn't permit reliable
1178 				 * stalling of the request.  Instead, demand
1179 				 * that gadget drivers not use this mode.
1180 				 */
1181 				DBG("no control-OUT deferred responses!\n");
1182 				__raw_writel(csr | AT91_UDP_FORCESTALL, creg);
1183 				udc->req_pending = 0;
1184 			}
1185 
1186 		/* STATUS stage for control-IN; ack.  */
1187 		} else {
1188 			PACKET("ep0 out/status ACK\n");
1189 			__raw_writel(csr, creg);
1190 
1191 			/* "early" status stage */
1192 			if (req)
1193 				done(ep0, req, 0);
1194 		}
1195 	}
1196 }
1197 
1198 static irqreturn_t at91_udc_irq(struct at91_udc *udc)
1199 {
1200 	u32			rescans = 5;
1201 	int			disable_clock = 0;
1202 	unsigned long		flags;
1203 
1204 	spin_lock_irqsave(&udc->lock, flags);
1205 
1206 	if (!udc->clocked) {
1207 		clk_on(udc);
1208 		disable_clock = 1;
1209 	}
1210 
1211 	while (rescans--) {
1212 		u32 status;
1213 
1214 		status = at91_udp_read(udc, AT91_UDP_ISR)
1215 			& at91_udp_read(udc, AT91_UDP_IMR);
1216 		if (!status)
1217 			break;
1218 
1219 		/* USB reset irq:  not maskable */
1220 		if (status & AT91_UDP_ENDBUSRES) {
1221 			at91_udp_write(udc, AT91_UDP_IDR, ~MINIMUS_INTERRUPTUS);
1222 			at91_udp_write(udc, AT91_UDP_IER, MINIMUS_INTERRUPTUS);
1223 			/* Atmel code clears this irq twice */
1224 			at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_ENDBUSRES);
1225 			at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_ENDBUSRES);
1226 			VDBG("end bus reset\n");
1227 			udc->addr = 0;
1228 			reset_gadget(udc);
1229 
1230 			/* enable ep0 */
1231 			at91_udp_write(udc, AT91_UDP_CSR(0),
1232 					AT91_UDP_EPEDS | AT91_UDP_EPTYPE_CTRL);
1233 			udc->gadget.speed = USB_SPEED_FULL;
1234 			udc->suspended = 0;
1235 			at91_udp_write(udc, AT91_UDP_IER, AT91_UDP_EP(0));
1236 
1237 			/*
1238 			 * NOTE:  this driver keeps clocks off unless the
1239 			 * USB host is present.  That saves power, but for
1240 			 * boards that don't support VBUS detection, both
1241 			 * clocks need to be active most of the time.
1242 			 */
1243 
1244 		/* host initiated suspend (3+ms bus idle) */
1245 		} else if (status & AT91_UDP_RXSUSP) {
1246 			at91_udp_write(udc, AT91_UDP_IDR, AT91_UDP_RXSUSP);
1247 			at91_udp_write(udc, AT91_UDP_IER, AT91_UDP_RXRSM);
1248 			at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_RXSUSP);
1249 			/* VDBG("bus suspend\n"); */
1250 			if (udc->suspended)
1251 				continue;
1252 			udc->suspended = 1;
1253 
1254 			/*
1255 			 * NOTE:  when suspending a VBUS-powered device, the
1256 			 * gadget driver should switch into slow clock mode
1257 			 * and then into standby to avoid drawing more than
1258 			 * 500uA power (2500uA for some high-power configs).
1259 			 */
1260 			if (udc->driver && udc->driver->suspend) {
1261 				spin_unlock(&udc->lock);
1262 				udc->driver->suspend(&udc->gadget);
1263 				spin_lock(&udc->lock);
1264 			}
1265 
1266 		/* host initiated resume */
1267 		} else if (status & AT91_UDP_RXRSM) {
1268 			at91_udp_write(udc, AT91_UDP_IDR, AT91_UDP_RXRSM);
1269 			at91_udp_write(udc, AT91_UDP_IER, AT91_UDP_RXSUSP);
1270 			at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_RXRSM);
1271 			/* VDBG("bus resume\n"); */
1272 			if (!udc->suspended)
1273 				continue;
1274 			udc->suspended = 0;
1275 
1276 			/*
1277 			 * NOTE:  for a VBUS-powered device, the gadget driver
1278 			 * would normally want to switch out of slow clock
1279 			 * mode into normal mode.
1280 			 */
1281 			if (udc->driver && udc->driver->resume) {
1282 				spin_unlock(&udc->lock);
1283 				udc->driver->resume(&udc->gadget);
1284 				spin_lock(&udc->lock);
1285 			}
1286 
1287 		/* endpoint IRQs are cleared by handling them */
1288 		} else {
1289 			int		i;
1290 			unsigned	mask = 1;
1291 			struct at91_ep	*ep = &udc->ep[1];
1292 
1293 			if (status & mask)
1294 				handle_ep0(udc);
1295 			for (i = 1; i < NUM_ENDPOINTS; i++) {
1296 				mask <<= 1;
1297 				if (status & mask)
1298 					handle_ep(ep);
1299 				ep++;
1300 			}
1301 		}
1302 	}
1303 
1304 	if (disable_clock)
1305 		clk_off(udc);
1306 
1307 	spin_unlock_irqrestore(&udc->lock, flags);
1308 
1309 	return IRQ_HANDLED;
1310 }
1311 
1312 /*-------------------------------------------------------------------------*/
1313 
1314 static int at91_start(struct usb_gadget *gadget,
1315 		struct usb_gadget_driver *driver)
1316 {
1317 	struct at91_udc *udc = controller;
1318 
1319 	udc->driver = driver;
1320 	udc->enabled = 1;
1321 	udc->selfpowered = 1;
1322 
1323 	return 0;
1324 }
1325 
1326 static int at91_stop(struct usb_gadget *gadget)
1327 {
1328 	struct at91_udc *udc = controller;
1329 	unsigned long	flags;
1330 
1331 	spin_lock_irqsave(&udc->lock, flags);
1332 	udc->enabled = 0;
1333 	at91_udp_write(udc, AT91_UDP_IDR, ~0);
1334 	spin_unlock_irqrestore(&udc->lock, flags);
1335 
1336 	udc->driver = NULL;
1337 
1338 	return 0;
1339 }
1340 
1341 /*-------------------------------------------------------------------------*/
1342 
1343 #if defined(CONFIG_AT91SAM9260) || defined(CONFIG_AT91SAM9G20)
1344 static int at91sam9260_udc_init(struct at91_udc *udc)
1345 {
1346 	struct at91_ep *ep;
1347 	int i;
1348 
1349 	for (i = 0; i < NUM_ENDPOINTS; i++) {
1350 		ep = &udc->ep[i];
1351 
1352 		switch (i) {
1353 		case 0 ... 3:
1354 			ep->maxpacket = 64;
1355 			break;
1356 		case 4 ... 5:
1357 			ep->maxpacket = 512;
1358 			break;
1359 		}
1360 	}
1361 
1362 	return 0;
1363 }
1364 
1365 static void at91sam9260_udc_pullup(struct at91_udc *udc, int is_on)
1366 {
1367 	u32 txvc = at91_udp_read(udc, AT91_UDP_TXVC);
1368 
1369 	if (is_on)
1370 		txvc |= AT91_UDP_TXVC_PUON;
1371 	else
1372 		txvc &= ~AT91_UDP_TXVC_PUON;
1373 
1374 	at91_udp_write(udc, AT91_UDP_TXVC, txvc);
1375 }
1376 
1377 static const struct at91_udc_caps at91sam9260_udc_caps = {
1378 	.init = at91sam9260_udc_init,
1379 	.pullup = at91sam9260_udc_pullup,
1380 };
1381 #endif
1382 
1383 #if defined(CONFIG_AT91SAM9261)
1384 static int at91sam9261_udc_init(struct at91_udc *udc)
1385 {
1386 	struct at91_ep *ep;
1387 	int i;
1388 
1389 	for (i = 0; i < NUM_ENDPOINTS; i++) {
1390 		ep = &udc->ep[i];
1391 
1392 		switch (i) {
1393 		case 0:
1394 			ep->maxpacket = 8;
1395 			break;
1396 		case 1 ... 3:
1397 			ep->maxpacket = 64;
1398 			break;
1399 		case 4 ... 5:
1400 			ep->maxpacket = 256;
1401 			break;
1402 		}
1403 	}
1404 
1405 	udc->matrix = (struct at91_matrix *)ATMEL_BASE_MATRIX;
1406 
1407 	if (IS_ERR(udc->matrix))
1408 		return PTR_ERR(udc->matrix);
1409 
1410 	return 0;
1411 }
1412 
1413 static void at91sam9261_udc_pullup(struct at91_udc *udc, int is_on)
1414 {
1415 	u32 usbpucr = 0;
1416 
1417 	usbpucr = readl(&udc->matrix->pucr);
1418 	if (is_on)
1419 		usbpucr |= AT91_MATRIX_USBPUCR_PUON;
1420 
1421 	writel(usbpucr, &udc->matrix->pucr);
1422 }
1423 
1424 static const struct at91_udc_caps at91sam9261_udc_caps = {
1425 	.init = at91sam9261_udc_init,
1426 	.pullup = at91sam9261_udc_pullup,
1427 };
1428 #endif
1429 
1430 int usb_gadget_handle_interrupts(int index)
1431 {
1432 	struct at91_udc *udc = controller;
1433 
1434 	return at91_udc_irq(udc);
1435 }
1436 
1437 int usb_gadget_register_driver(struct usb_gadget_driver *driver)
1438 {
1439 	struct at91_udc *udc = controller;
1440 	int ret;
1441 
1442 	if (!driver || !driver->bind || !driver->setup) {
1443 		printf("bad paramter\n");
1444 		return -EINVAL;
1445 	}
1446 
1447 	if (udc->driver) {
1448 		printf("UDC already has a gadget driver\n");
1449 		return -EBUSY;
1450 	}
1451 
1452 	at91_start(&udc->gadget, driver);
1453 
1454 	udc->driver = driver;
1455 
1456 	ret = driver->bind(&udc->gadget);
1457 	if (ret) {
1458 		pr_err("driver->bind() returned %d\n", ret);
1459 		udc->driver = NULL;
1460 	}
1461 
1462 	return ret;
1463 }
1464 
1465 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1466 {
1467 	struct at91_udc *udc = controller;
1468 
1469 	if (!driver || !driver->unbind || !driver->disconnect) {
1470 		pr_err("bad paramter\n");
1471 		return -EINVAL;
1472 	}
1473 
1474 	driver->disconnect(&udc->gadget);
1475 	driver->unbind(&udc->gadget);
1476 	udc->driver = NULL;
1477 
1478 	at91_stop(&udc->gadget);
1479 
1480 	return 0;
1481 }
1482 
1483 int at91_udc_probe(struct at91_udc_data *pdata)
1484 {
1485 	struct at91_udc	*udc;
1486 	int		retval;
1487 	struct at91_ep	*ep;
1488 	int		i;
1489 
1490 	udc = kzalloc(sizeof(*udc), GFP_KERNEL);
1491 	if (!udc)
1492 		return -ENOMEM;
1493 
1494 	controller = udc;
1495 	memcpy(&udc->board, pdata, sizeof(struct at91_udc_data));
1496 	if (udc->board.vbus_pin) {
1497 		printf("%s: gpio vbus pin not supported yet.\n", __func__);
1498 		return -ENXIO;
1499 	} else {
1500 		DBG("no VBUS detection, assuming always-on\n");
1501 		udc->vbus = 1;
1502 	}
1503 
1504 #if defined(CONFIG_AT91SAM9260) || defined(CONFIG_AT91SAM9G20)
1505 	udc->caps = &at91sam9260_udc_caps;
1506 #endif
1507 
1508 	udc->enabled = 0;
1509 	spin_lock_init(&udc->lock);
1510 
1511 	udc->gadget.ops = &at91_udc_ops;
1512 	udc->gadget.ep0 = &udc->ep[0].ep;
1513 	udc->gadget.name = driver_name;
1514 
1515 	for (i = 0; i < NUM_ENDPOINTS; i++) {
1516 		ep = &udc->ep[i];
1517 		ep->ep.name = ep_names[i];
1518 		ep->ep.ops = &at91_ep_ops;
1519 		ep->udc = udc;
1520 		ep->int_mask = (1 << i);
1521 		if (i != 0 && i != 3)
1522 			ep->is_pingpong = 1;
1523 	}
1524 
1525 	udc->udp_baseaddr = (void *)udc->board.baseaddr;
1526 	if (IS_ERR(udc->udp_baseaddr))
1527 		return PTR_ERR(udc->udp_baseaddr);
1528 
1529 	if (udc->caps && udc->caps->init) {
1530 		retval = udc->caps->init(udc);
1531 		if (retval)
1532 			return retval;
1533 	}
1534 
1535 	udc_reinit(udc);
1536 
1537 	at91_udp_write(udc, AT91_UDP_TXVC, AT91_UDP_TXVC_TXVDIS);
1538 	at91_udp_write(udc, AT91_UDP_IDR, 0xffffffff);
1539 	/* Clear all pending interrupts - UDP may be used by bootloader. */
1540 	at91_udp_write(udc, AT91_UDP_ICR, 0xffffffff);
1541 
1542 	INFO("%s version %s\n", driver_name, DRIVER_VERSION);
1543 	return 0;
1544 }
1545