blob: a6adf7e0f6f8de4cc49595211dc5de7e6d4e779e [file] [log] [blame]
David Brownellbae4bd82006-01-22 10:32:37 -08001/*
2 * at91_udc -- driver for at91-series USB peripheral controller
3 *
4 * Copyright (C) 2004 by Thomas Rathbone
5 * Copyright (C) 2005 by HP Labs
6 * Copyright (C) 2005 by David Brownell
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
22 */
23
24#undef DEBUG
25#undef VERBOSE
26#undef PACKET_TRACE
27
David Brownellbae4bd82006-01-22 10:32:37 -080028#include <linux/kernel.h>
29#include <linux/module.h>
30#include <linux/platform_device.h>
31#include <linux/delay.h>
32#include <linux/ioport.h>
David Brownellbae4bd82006-01-22 10:32:37 -080033#include <linux/slab.h>
David Brownellbae4bd82006-01-22 10:32:37 -080034#include <linux/errno.h>
35#include <linux/init.h>
36#include <linux/list.h>
37#include <linux/interrupt.h>
38#include <linux/proc_fs.h>
39#include <linux/clk.h>
David Brownell5f848132006-12-16 15:34:53 -080040#include <linux/usb/ch9.h>
David Brownell9454a572007-10-04 18:05:17 -070041#include <linux/usb/gadget.h>
David Brownellbae4bd82006-01-22 10:32:37 -080042
43#include <asm/byteorder.h>
Andrew Victorffd33262006-12-07 22:44:33 -080044#include <asm/hardware.h>
David Brownellbae4bd82006-01-22 10:32:37 -080045#include <asm/io.h>
46#include <asm/irq.h>
47#include <asm/system.h>
48#include <asm/mach-types.h>
49
David Brownellbae4bd82006-01-22 10:32:37 -080050#include <asm/arch/gpio.h>
51#include <asm/arch/board.h>
Andrew Victor29ba4b52006-12-07 22:44:38 -080052#include <asm/arch/cpu.h>
53#include <asm/arch/at91sam9261_matrix.h>
David Brownellbae4bd82006-01-22 10:32:37 -080054
55#include "at91_udc.h"
56
57
58/*
59 * This controller is simple and PIO-only. It's used in many AT91-series
David Brownell8b2e7662006-07-05 02:38:56 -070060 * full speed USB controllers, including the at91rm9200 (arm920T, with MMU),
61 * at91sam926x (arm926ejs, with MMU), and several no-mmu versions.
David Brownellbae4bd82006-01-22 10:32:37 -080062 *
63 * This driver expects the board has been wired with two GPIOs suppporting
64 * a VBUS sensing IRQ, and a D+ pullup. (They may be omitted, but the
David Brownell8b2e7662006-07-05 02:38:56 -070065 * testing hasn't covered such cases.)
66 *
67 * The pullup is most important (so it's integrated on sam926x parts). It
David Brownellbae4bd82006-01-22 10:32:37 -080068 * provides software control over whether the host enumerates the device.
David Brownell8b2e7662006-07-05 02:38:56 -070069 *
David Brownellbae4bd82006-01-22 10:32:37 -080070 * The VBUS sensing helps during enumeration, and allows both USB clocks
71 * (and the transceiver) to stay gated off until they're necessary, saving
David Brownell8b2e7662006-07-05 02:38:56 -070072 * power. During USB suspend, the 48 MHz clock is gated off in hardware;
73 * it may also be gated off by software during some Linux sleep states.
David Brownellbae4bd82006-01-22 10:32:37 -080074 */
75
David Brownell8b2e7662006-07-05 02:38:56 -070076#define DRIVER_VERSION "3 May 2006"
David Brownellbae4bd82006-01-22 10:32:37 -080077
78static const char driver_name [] = "at91_udc";
79static const char ep0name[] = "ep0";
80
David Brownellbae4bd82006-01-22 10:32:37 -080081
Andrew Victorffd33262006-12-07 22:44:33 -080082#define at91_udp_read(dev, reg) \
83 __raw_readl((dev)->udp_baseaddr + (reg))
84#define at91_udp_write(dev, reg, val) \
85 __raw_writel((val), (dev)->udp_baseaddr + (reg))
David Brownellbae4bd82006-01-22 10:32:37 -080086
87/*-------------------------------------------------------------------------*/
88
89#ifdef CONFIG_USB_GADGET_DEBUG_FILES
90
91#include <linux/seq_file.h>
92
93static const char debug_filename[] = "driver/udc";
94
95#define FOURBITS "%s%s%s%s"
96#define EIGHTBITS FOURBITS FOURBITS
97
98static void proc_ep_show(struct seq_file *s, struct at91_ep *ep)
99{
100 static char *types[] = {
101 "control", "out-iso", "out-bulk", "out-int",
102 "BOGUS", "in-iso", "in-bulk", "in-int"};
103
104 u32 csr;
105 struct at91_request *req;
106 unsigned long flags;
107
108 local_irq_save(flags);
109
110 csr = __raw_readl(ep->creg);
111
112 /* NOTE: not collecting per-endpoint irq statistics... */
113
114 seq_printf(s, "\n");
115 seq_printf(s, "%s, maxpacket %d %s%s %s%s\n",
116 ep->ep.name, ep->ep.maxpacket,
117 ep->is_in ? "in" : "out",
118 ep->is_iso ? " iso" : "",
119 ep->is_pingpong
120 ? (ep->fifo_bank ? "pong" : "ping")
121 : "",
122 ep->stopped ? " stopped" : "");
123 seq_printf(s, "csr %08x rxbytes=%d %s %s %s" EIGHTBITS "\n",
124 csr,
125 (csr & 0x07ff0000) >> 16,
126 (csr & (1 << 15)) ? "enabled" : "disabled",
127 (csr & (1 << 11)) ? "DATA1" : "DATA0",
128 types[(csr & 0x700) >> 8],
129
130 /* iff type is control then print current direction */
131 (!(csr & 0x700))
132 ? ((csr & (1 << 7)) ? " IN" : " OUT")
133 : "",
134 (csr & (1 << 6)) ? " rxdatabk1" : "",
135 (csr & (1 << 5)) ? " forcestall" : "",
136 (csr & (1 << 4)) ? " txpktrdy" : "",
137
138 (csr & (1 << 3)) ? " stallsent" : "",
139 (csr & (1 << 2)) ? " rxsetup" : "",
140 (csr & (1 << 1)) ? " rxdatabk0" : "",
141 (csr & (1 << 0)) ? " txcomp" : "");
142 if (list_empty (&ep->queue))
143 seq_printf(s, "\t(queue empty)\n");
144
145 else list_for_each_entry (req, &ep->queue, queue) {
146 unsigned length = req->req.actual;
147
148 seq_printf(s, "\treq %p len %d/%d buf %p\n",
149 &req->req, length,
150 req->req.length, req->req.buf);
151 }
152 local_irq_restore(flags);
153}
154
155static void proc_irq_show(struct seq_file *s, const char *label, u32 mask)
156{
157 int i;
158
159 seq_printf(s, "%s %04x:%s%s" FOURBITS, label, mask,
160 (mask & (1 << 13)) ? " wakeup" : "",
161 (mask & (1 << 12)) ? " endbusres" : "",
162
163 (mask & (1 << 11)) ? " sofint" : "",
164 (mask & (1 << 10)) ? " extrsm" : "",
165 (mask & (1 << 9)) ? " rxrsm" : "",
166 (mask & (1 << 8)) ? " rxsusp" : "");
167 for (i = 0; i < 8; i++) {
168 if (mask & (1 << i))
169 seq_printf(s, " ep%d", i);
170 }
171 seq_printf(s, "\n");
172}
173
174static int proc_udc_show(struct seq_file *s, void *unused)
175{
176 struct at91_udc *udc = s->private;
177 struct at91_ep *ep;
178 u32 tmp;
179
180 seq_printf(s, "%s: version %s\n", driver_name, DRIVER_VERSION);
181
182 seq_printf(s, "vbus %s, pullup %s, %s powered%s, gadget %s\n\n",
183 udc->vbus ? "present" : "off",
184 udc->enabled
185 ? (udc->vbus ? "active" : "enabled")
186 : "disabled",
187 udc->selfpowered ? "self" : "VBUS",
188 udc->suspended ? ", suspended" : "",
189 udc->driver ? udc->driver->driver.name : "(none)");
190
191 /* don't access registers when interface isn't clocked */
192 if (!udc->clocked) {
193 seq_printf(s, "(not clocked)\n");
194 return 0;
195 }
196
Andrew Victorffd33262006-12-07 22:44:33 -0800197 tmp = at91_udp_read(udc, AT91_UDP_FRM_NUM);
David Brownellbae4bd82006-01-22 10:32:37 -0800198 seq_printf(s, "frame %05x:%s%s frame=%d\n", tmp,
199 (tmp & AT91_UDP_FRM_OK) ? " ok" : "",
200 (tmp & AT91_UDP_FRM_ERR) ? " err" : "",
201 (tmp & AT91_UDP_NUM));
202
Andrew Victorffd33262006-12-07 22:44:33 -0800203 tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT);
David Brownellbae4bd82006-01-22 10:32:37 -0800204 seq_printf(s, "glbstate %02x:%s" FOURBITS "\n", tmp,
205 (tmp & AT91_UDP_RMWUPE) ? " rmwupe" : "",
206 (tmp & AT91_UDP_RSMINPR) ? " rsminpr" : "",
207 (tmp & AT91_UDP_ESR) ? " esr" : "",
208 (tmp & AT91_UDP_CONFG) ? " confg" : "",
209 (tmp & AT91_UDP_FADDEN) ? " fadden" : "");
210
Andrew Victorffd33262006-12-07 22:44:33 -0800211 tmp = at91_udp_read(udc, AT91_UDP_FADDR);
David Brownellbae4bd82006-01-22 10:32:37 -0800212 seq_printf(s, "faddr %03x:%s fadd=%d\n", tmp,
213 (tmp & AT91_UDP_FEN) ? " fen" : "",
214 (tmp & AT91_UDP_FADD));
215
Andrew Victorffd33262006-12-07 22:44:33 -0800216 proc_irq_show(s, "imr ", at91_udp_read(udc, AT91_UDP_IMR));
217 proc_irq_show(s, "isr ", at91_udp_read(udc, AT91_UDP_ISR));
David Brownellbae4bd82006-01-22 10:32:37 -0800218
219 if (udc->enabled && udc->vbus) {
220 proc_ep_show(s, &udc->ep[0]);
221 list_for_each_entry (ep, &udc->gadget.ep_list, ep.ep_list) {
222 if (ep->desc)
223 proc_ep_show(s, ep);
224 }
225 }
226 return 0;
227}
228
229static int proc_udc_open(struct inode *inode, struct file *file)
230{
231 return single_open(file, proc_udc_show, PDE(inode)->data);
232}
233
Luiz Fernando N. Capitulino066202d2006-08-05 20:37:11 -0300234static const struct file_operations proc_ops = {
David Brownellbae4bd82006-01-22 10:32:37 -0800235 .open = proc_udc_open,
236 .read = seq_read,
237 .llseek = seq_lseek,
238 .release = single_release,
239};
240
241static void create_debug_file(struct at91_udc *udc)
242{
243 struct proc_dir_entry *pde;
244
245 pde = create_proc_entry (debug_filename, 0, NULL);
246 udc->pde = pde;
247 if (pde == NULL)
248 return;
249
250 pde->proc_fops = &proc_ops;
251 pde->data = udc;
252}
253
254static void remove_debug_file(struct at91_udc *udc)
255{
256 if (udc->pde)
257 remove_proc_entry(debug_filename, NULL);
258}
259
260#else
261
262static inline void create_debug_file(struct at91_udc *udc) {}
263static inline void remove_debug_file(struct at91_udc *udc) {}
264
265#endif
266
267
268/*-------------------------------------------------------------------------*/
269
270static void done(struct at91_ep *ep, struct at91_request *req, int status)
271{
272 unsigned stopped = ep->stopped;
Andrew Victorffd33262006-12-07 22:44:33 -0800273 struct at91_udc *udc = ep->udc;
David Brownellbae4bd82006-01-22 10:32:37 -0800274
275 list_del_init(&req->queue);
276 if (req->req.status == -EINPROGRESS)
277 req->req.status = status;
278 else
279 status = req->req.status;
280 if (status && status != -ESHUTDOWN)
281 VDBG("%s done %p, status %d\n", ep->ep.name, req, status);
282
283 ep->stopped = 1;
284 req->req.complete(&ep->ep, &req->req);
285 ep->stopped = stopped;
286
287 /* ep0 is always ready; other endpoints need a non-empty queue */
288 if (list_empty(&ep->queue) && ep->int_mask != (1 << 0))
Andrew Victorffd33262006-12-07 22:44:33 -0800289 at91_udp_write(udc, AT91_UDP_IDR, ep->int_mask);
David Brownellbae4bd82006-01-22 10:32:37 -0800290}
291
292/*-------------------------------------------------------------------------*/
293
294/* bits indicating OUT fifo has data ready */
295#define RX_DATA_READY (AT91_UDP_RX_DATA_BK0 | AT91_UDP_RX_DATA_BK1)
296
297/*
298 * Endpoint FIFO CSR bits have a mix of bits, making it unsafe to just write
299 * back most of the value you just read (because of side effects, including
300 * bits that may change after reading and before writing).
301 *
302 * Except when changing a specific bit, always write values which:
303 * - clear SET_FX bits (setting them could change something)
304 * - set CLR_FX bits (clearing them could change something)
305 *
306 * There are also state bits like FORCESTALL, EPEDS, DIR, and EPTYPE
307 * that shouldn't normally be changed.
David Brownell8b2e7662006-07-05 02:38:56 -0700308 *
309 * NOTE at91sam9260 docs mention synch between UDPCK and MCK clock domains,
310 * implying a need to wait for one write to complete (test relevant bits)
311 * before starting the next write. This shouldn't be an issue given how
312 * infrequently we write, except maybe for write-then-read idioms.
David Brownellbae4bd82006-01-22 10:32:37 -0800313 */
314#define SET_FX (AT91_UDP_TXPKTRDY)
David Brownell8b2e7662006-07-05 02:38:56 -0700315#define CLR_FX (RX_DATA_READY | AT91_UDP_RXSETUP \
316 | AT91_UDP_STALLSENT | AT91_UDP_TXCOMP)
David Brownellbae4bd82006-01-22 10:32:37 -0800317
318/* pull OUT packet data from the endpoint's fifo */
319static int read_fifo (struct at91_ep *ep, struct at91_request *req)
320{
321 u32 __iomem *creg = ep->creg;
322 u8 __iomem *dreg = ep->creg + (AT91_UDP_FDR(0) - AT91_UDP_CSR(0));
323 u32 csr;
324 u8 *buf;
325 unsigned int count, bufferspace, is_done;
326
327 buf = req->req.buf + req->req.actual;
328 bufferspace = req->req.length - req->req.actual;
329
330 /*
331 * there might be nothing to read if ep_queue() calls us,
332 * or if we already emptied both pingpong buffers
333 */
334rescan:
335 csr = __raw_readl(creg);
336 if ((csr & RX_DATA_READY) == 0)
337 return 0;
338
339 count = (csr & AT91_UDP_RXBYTECNT) >> 16;
340 if (count > ep->ep.maxpacket)
341 count = ep->ep.maxpacket;
342 if (count > bufferspace) {
343 DBG("%s buffer overflow\n", ep->ep.name);
344 req->req.status = -EOVERFLOW;
345 count = bufferspace;
346 }
347 __raw_readsb(dreg, buf, count);
348
349 /* release and swap pingpong mem bank */
350 csr |= CLR_FX;
351 if (ep->is_pingpong) {
352 if (ep->fifo_bank == 0) {
353 csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK0);
354 ep->fifo_bank = 1;
355 } else {
356 csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK1);
357 ep->fifo_bank = 0;
358 }
359 } else
360 csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK0);
361 __raw_writel(csr, creg);
362
363 req->req.actual += count;
364 is_done = (count < ep->ep.maxpacket);
365 if (count == bufferspace)
366 is_done = 1;
367
368 PACKET("%s %p out/%d%s\n", ep->ep.name, &req->req, count,
369 is_done ? " (done)" : "");
370
371 /*
372 * avoid extra trips through IRQ logic for packets already in
373 * the fifo ... maybe preventing an extra (expensive) OUT-NAK
374 */
375 if (is_done)
376 done(ep, req, 0);
377 else if (ep->is_pingpong) {
378 bufferspace -= count;
379 buf += count;
380 goto rescan;
381 }
382
383 return is_done;
384}
385
386/* load fifo for an IN packet */
387static int write_fifo(struct at91_ep *ep, struct at91_request *req)
388{
389 u32 __iomem *creg = ep->creg;
390 u32 csr = __raw_readl(creg);
391 u8 __iomem *dreg = ep->creg + (AT91_UDP_FDR(0) - AT91_UDP_CSR(0));
392 unsigned total, count, is_last;
393
394 /*
395 * TODO: allow for writing two packets to the fifo ... that'll
396 * reduce the amount of IN-NAKing, but probably won't affect
397 * throughput much. (Unlike preventing OUT-NAKing!)
398 */
399
400 /*
401 * If ep_queue() calls us, the queue is empty and possibly in
402 * odd states like TXCOMP not yet cleared (we do it, saving at
403 * least one IRQ) or the fifo not yet being free. Those aren't
404 * issues normally (IRQ handler fast path).
405 */
406 if (unlikely(csr & (AT91_UDP_TXCOMP | AT91_UDP_TXPKTRDY))) {
407 if (csr & AT91_UDP_TXCOMP) {
408 csr |= CLR_FX;
409 csr &= ~(SET_FX | AT91_UDP_TXCOMP);
410 __raw_writel(csr, creg);
411 csr = __raw_readl(creg);
412 }
413 if (csr & AT91_UDP_TXPKTRDY)
414 return 0;
415 }
416
417 total = req->req.length - req->req.actual;
418 if (ep->ep.maxpacket < total) {
419 count = ep->ep.maxpacket;
420 is_last = 0;
421 } else {
422 count = total;
423 is_last = (count < ep->ep.maxpacket) || !req->req.zero;
424 }
425
426 /*
427 * Write the packet, maybe it's a ZLP.
428 *
429 * NOTE: incrementing req->actual before we receive the ACK means
430 * gadget driver IN bytecounts can be wrong in fault cases. That's
431 * fixable with PIO drivers like this one (save "count" here, and
432 * do the increment later on TX irq), but not for most DMA hardware.
433 *
434 * So all gadget drivers must accept that potential error. Some
435 * hardware supports precise fifo status reporting, letting them
436 * recover when the actual bytecount matters (e.g. for USB Test
437 * and Measurement Class devices).
438 */
439 __raw_writesb(dreg, req->req.buf + req->req.actual, count);
440 csr &= ~SET_FX;
441 csr |= CLR_FX | AT91_UDP_TXPKTRDY;
442 __raw_writel(csr, creg);
443 req->req.actual += count;
444
445 PACKET("%s %p in/%d%s\n", ep->ep.name, &req->req, count,
446 is_last ? " (done)" : "");
447 if (is_last)
448 done(ep, req, 0);
449 return is_last;
450}
451
452static void nuke(struct at91_ep *ep, int status)
453{
454 struct at91_request *req;
455
456 // terminer chaque requete dans la queue
457 ep->stopped = 1;
458 if (list_empty(&ep->queue))
459 return;
460
461 VDBG("%s %s\n", __FUNCTION__, ep->ep.name);
462 while (!list_empty(&ep->queue)) {
463 req = list_entry(ep->queue.next, struct at91_request, queue);
464 done(ep, req, status);
465 }
466}
467
468/*-------------------------------------------------------------------------*/
469
David Brownell8b2e7662006-07-05 02:38:56 -0700470static int at91_ep_enable(struct usb_ep *_ep,
471 const struct usb_endpoint_descriptor *desc)
David Brownellbae4bd82006-01-22 10:32:37 -0800472{
473 struct at91_ep *ep = container_of(_ep, struct at91_ep, ep);
474 struct at91_udc *dev = ep->udc;
475 u16 maxpacket;
476 u32 tmp;
477 unsigned long flags;
478
479 if (!_ep || !ep
480 || !desc || ep->desc
481 || _ep->name == ep0name
482 || desc->bDescriptorType != USB_DT_ENDPOINT
483 || (maxpacket = le16_to_cpu(desc->wMaxPacketSize)) == 0
484 || maxpacket > ep->maxpacket) {
485 DBG("bad ep or descriptor\n");
486 return -EINVAL;
487 }
488
489 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
490 DBG("bogus device state\n");
491 return -ESHUTDOWN;
492 }
493
494 tmp = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
495 switch (tmp) {
496 case USB_ENDPOINT_XFER_CONTROL:
497 DBG("only one control endpoint\n");
498 return -EINVAL;
499 case USB_ENDPOINT_XFER_INT:
500 if (maxpacket > 64)
501 goto bogus_max;
502 break;
503 case USB_ENDPOINT_XFER_BULK:
504 switch (maxpacket) {
505 case 8:
506 case 16:
507 case 32:
508 case 64:
509 goto ok;
510 }
511bogus_max:
512 DBG("bogus maxpacket %d\n", maxpacket);
513 return -EINVAL;
514 case USB_ENDPOINT_XFER_ISOC:
515 if (!ep->is_pingpong) {
516 DBG("iso requires double buffering\n");
517 return -EINVAL;
518 }
519 break;
520 }
521
522ok:
523 local_irq_save(flags);
524
525 /* initialize endpoint to match this descriptor */
526 ep->is_in = (desc->bEndpointAddress & USB_DIR_IN) != 0;
527 ep->is_iso = (tmp == USB_ENDPOINT_XFER_ISOC);
528 ep->stopped = 0;
529 if (ep->is_in)
530 tmp |= 0x04;
531 tmp <<= 8;
532 tmp |= AT91_UDP_EPEDS;
533 __raw_writel(tmp, ep->creg);
534
535 ep->desc = desc;
536 ep->ep.maxpacket = maxpacket;
537
538 /*
539 * reset/init endpoint fifo. NOTE: leaves fifo_bank alone,
540 * since endpoint resets don't reset hw pingpong state.
541 */
Andrew Victorffd33262006-12-07 22:44:33 -0800542 at91_udp_write(dev, AT91_UDP_RST_EP, ep->int_mask);
543 at91_udp_write(dev, AT91_UDP_RST_EP, 0);
David Brownellbae4bd82006-01-22 10:32:37 -0800544
545 local_irq_restore(flags);
546 return 0;
547}
548
549static int at91_ep_disable (struct usb_ep * _ep)
550{
551 struct at91_ep *ep = container_of(_ep, struct at91_ep, ep);
Andrew Victorffd33262006-12-07 22:44:33 -0800552 struct at91_udc *udc = ep->udc;
David Brownellbae4bd82006-01-22 10:32:37 -0800553 unsigned long flags;
554
555 if (ep == &ep->udc->ep[0])
556 return -EINVAL;
557
558 local_irq_save(flags);
559
560 nuke(ep, -ESHUTDOWN);
561
562 /* restore the endpoint's pristine config */
563 ep->desc = NULL;
564 ep->ep.maxpacket = ep->maxpacket;
565
566 /* reset fifos and endpoint */
567 if (ep->udc->clocked) {
Andrew Victorffd33262006-12-07 22:44:33 -0800568 at91_udp_write(udc, AT91_UDP_RST_EP, ep->int_mask);
569 at91_udp_write(udc, AT91_UDP_RST_EP, 0);
David Brownellbae4bd82006-01-22 10:32:37 -0800570 __raw_writel(0, ep->creg);
571 }
572
573 local_irq_restore(flags);
574 return 0;
575}
576
577/*
578 * this is a PIO-only driver, so there's nothing
579 * interesting for request or buffer allocation.
580 */
581
David Brownell8b2e7662006-07-05 02:38:56 -0700582static struct usb_request *
583at91_ep_alloc_request(struct usb_ep *_ep, unsigned int gfp_flags)
David Brownellbae4bd82006-01-22 10:32:37 -0800584{
585 struct at91_request *req;
586
Robert P. J. Daycd861282006-12-13 00:34:52 -0800587 req = kzalloc(sizeof (struct at91_request), gfp_flags);
David Brownellbae4bd82006-01-22 10:32:37 -0800588 if (!req)
589 return NULL;
590
591 INIT_LIST_HEAD(&req->queue);
592 return &req->req;
593}
594
595static void at91_ep_free_request(struct usb_ep *_ep, struct usb_request *_req)
596{
597 struct at91_request *req;
598
599 req = container_of(_req, struct at91_request, req);
600 BUG_ON(!list_empty(&req->queue));
601 kfree(req);
602}
603
David Brownellbae4bd82006-01-22 10:32:37 -0800604static int at91_ep_queue(struct usb_ep *_ep,
605 struct usb_request *_req, gfp_t gfp_flags)
606{
607 struct at91_request *req;
608 struct at91_ep *ep;
609 struct at91_udc *dev;
610 int status;
611 unsigned long flags;
612
613 req = container_of(_req, struct at91_request, req);
614 ep = container_of(_ep, struct at91_ep, ep);
615
616 if (!_req || !_req->complete
617 || !_req->buf || !list_empty(&req->queue)) {
618 DBG("invalid request\n");
619 return -EINVAL;
620 }
621
622 if (!_ep || (!ep->desc && ep->ep.name != ep0name)) {
623 DBG("invalid ep\n");
624 return -EINVAL;
625 }
626
627 dev = ep->udc;
628
629 if (!dev || !dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
630 DBG("invalid device\n");
631 return -EINVAL;
632 }
633
634 _req->status = -EINPROGRESS;
635 _req->actual = 0;
636
637 local_irq_save(flags);
638
639 /* try to kickstart any empty and idle queue */
640 if (list_empty(&ep->queue) && !ep->stopped) {
641 int is_ep0;
642
643 /*
644 * If this control request has a non-empty DATA stage, this
645 * will start that stage. It works just like a non-control
646 * request (until the status stage starts, maybe early).
647 *
648 * If the data stage is empty, then this starts a successful
649 * IN/STATUS stage. (Unsuccessful ones use set_halt.)
650 */
651 is_ep0 = (ep->ep.name == ep0name);
652 if (is_ep0) {
653 u32 tmp;
654
655 if (!dev->req_pending) {
656 status = -EINVAL;
657 goto done;
658 }
659
660 /*
661 * defer changing CONFG until after the gadget driver
662 * reconfigures the endpoints.
663 */
664 if (dev->wait_for_config_ack) {
Andrew Victorffd33262006-12-07 22:44:33 -0800665 tmp = at91_udp_read(dev, AT91_UDP_GLB_STAT);
David Brownellbae4bd82006-01-22 10:32:37 -0800666 tmp ^= AT91_UDP_CONFG;
667 VDBG("toggle config\n");
Andrew Victorffd33262006-12-07 22:44:33 -0800668 at91_udp_write(dev, AT91_UDP_GLB_STAT, tmp);
David Brownellbae4bd82006-01-22 10:32:37 -0800669 }
670 if (req->req.length == 0) {
671ep0_in_status:
672 PACKET("ep0 in/status\n");
673 status = 0;
674 tmp = __raw_readl(ep->creg);
675 tmp &= ~SET_FX;
676 tmp |= CLR_FX | AT91_UDP_TXPKTRDY;
677 __raw_writel(tmp, ep->creg);
678 dev->req_pending = 0;
679 goto done;
680 }
681 }
682
683 if (ep->is_in)
684 status = write_fifo(ep, req);
685 else {
686 status = read_fifo(ep, req);
687
688 /* IN/STATUS stage is otherwise triggered by irq */
689 if (status && is_ep0)
690 goto ep0_in_status;
691 }
692 } else
693 status = 0;
694
695 if (req && !status) {
696 list_add_tail (&req->queue, &ep->queue);
Andrew Victorffd33262006-12-07 22:44:33 -0800697 at91_udp_write(dev, AT91_UDP_IER, ep->int_mask);
David Brownellbae4bd82006-01-22 10:32:37 -0800698 }
699done:
700 local_irq_restore(flags);
701 return (status < 0) ? status : 0;
702}
703
704static int at91_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
705{
706 struct at91_ep *ep;
707 struct at91_request *req;
708
709 ep = container_of(_ep, struct at91_ep, ep);
710 if (!_ep || ep->ep.name == ep0name)
711 return -EINVAL;
712
713 /* make sure it's actually queued on this endpoint */
714 list_for_each_entry (req, &ep->queue, queue) {
715 if (&req->req == _req)
716 break;
717 }
718 if (&req->req != _req)
719 return -EINVAL;
720
721 done(ep, req, -ECONNRESET);
722 return 0;
723}
724
725static int at91_ep_set_halt(struct usb_ep *_ep, int value)
726{
727 struct at91_ep *ep = container_of(_ep, struct at91_ep, ep);
Andrew Victorffd33262006-12-07 22:44:33 -0800728 struct at91_udc *udc = ep->udc;
David Brownellbae4bd82006-01-22 10:32:37 -0800729 u32 __iomem *creg;
730 u32 csr;
731 unsigned long flags;
732 int status = 0;
733
734 if (!_ep || ep->is_iso || !ep->udc->clocked)
735 return -EINVAL;
736
737 creg = ep->creg;
738 local_irq_save(flags);
739
740 csr = __raw_readl(creg);
741
742 /*
743 * fail with still-busy IN endpoints, ensuring correct sequencing
744 * of data tx then stall. note that the fifo rx bytecount isn't
745 * completely accurate as a tx bytecount.
746 */
747 if (ep->is_in && (!list_empty(&ep->queue) || (csr >> 16) != 0))
748 status = -EAGAIN;
749 else {
750 csr |= CLR_FX;
751 csr &= ~SET_FX;
752 if (value) {
753 csr |= AT91_UDP_FORCESTALL;
754 VDBG("halt %s\n", ep->ep.name);
755 } else {
Andrew Victorffd33262006-12-07 22:44:33 -0800756 at91_udp_write(udc, AT91_UDP_RST_EP, ep->int_mask);
757 at91_udp_write(udc, AT91_UDP_RST_EP, 0);
David Brownellbae4bd82006-01-22 10:32:37 -0800758 csr &= ~AT91_UDP_FORCESTALL;
759 }
760 __raw_writel(csr, creg);
761 }
762
763 local_irq_restore(flags);
764 return status;
765}
766
David Brownell398acce2007-02-15 18:47:17 -0800767static const struct usb_ep_ops at91_ep_ops = {
David Brownellbae4bd82006-01-22 10:32:37 -0800768 .enable = at91_ep_enable,
769 .disable = at91_ep_disable,
770 .alloc_request = at91_ep_alloc_request,
771 .free_request = at91_ep_free_request,
David Brownellbae4bd82006-01-22 10:32:37 -0800772 .queue = at91_ep_queue,
773 .dequeue = at91_ep_dequeue,
774 .set_halt = at91_ep_set_halt,
775 // there's only imprecise fifo status reporting
776};
777
778/*-------------------------------------------------------------------------*/
779
780static int at91_get_frame(struct usb_gadget *gadget)
781{
Andrew Victorffd33262006-12-07 22:44:33 -0800782 struct at91_udc *udc = to_udc(gadget);
783
David Brownellbae4bd82006-01-22 10:32:37 -0800784 if (!to_udc(gadget)->clocked)
785 return -EINVAL;
Andrew Victorffd33262006-12-07 22:44:33 -0800786 return at91_udp_read(udc, AT91_UDP_FRM_NUM) & AT91_UDP_NUM;
David Brownellbae4bd82006-01-22 10:32:37 -0800787}
788
789static int at91_wakeup(struct usb_gadget *gadget)
790{
791 struct at91_udc *udc = to_udc(gadget);
792 u32 glbstate;
793 int status = -EINVAL;
794 unsigned long flags;
795
796 DBG("%s\n", __FUNCTION__ );
797 local_irq_save(flags);
798
799 if (!udc->clocked || !udc->suspended)
800 goto done;
801
802 /* NOTE: some "early versions" handle ESR differently ... */
803
Andrew Victorffd33262006-12-07 22:44:33 -0800804 glbstate = at91_udp_read(udc, AT91_UDP_GLB_STAT);
David Brownellbae4bd82006-01-22 10:32:37 -0800805 if (!(glbstate & AT91_UDP_ESR))
806 goto done;
807 glbstate |= AT91_UDP_ESR;
Andrew Victorffd33262006-12-07 22:44:33 -0800808 at91_udp_write(udc, AT91_UDP_GLB_STAT, glbstate);
David Brownellbae4bd82006-01-22 10:32:37 -0800809
810done:
811 local_irq_restore(flags);
812 return status;
813}
814
815/* reinit == restore inital software state */
816static void udc_reinit(struct at91_udc *udc)
817{
818 u32 i;
819
820 INIT_LIST_HEAD(&udc->gadget.ep_list);
821 INIT_LIST_HEAD(&udc->gadget.ep0->ep_list);
822
823 for (i = 0; i < NUM_ENDPOINTS; i++) {
824 struct at91_ep *ep = &udc->ep[i];
825
826 if (i != 0)
827 list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
828 ep->desc = NULL;
829 ep->stopped = 0;
830 ep->fifo_bank = 0;
831 ep->ep.maxpacket = ep->maxpacket;
Andrew Victorffd33262006-12-07 22:44:33 -0800832 ep->creg = (void __iomem *) udc->udp_baseaddr + AT91_UDP_CSR(i);
David Brownellbae4bd82006-01-22 10:32:37 -0800833 // initialiser une queue par endpoint
834 INIT_LIST_HEAD(&ep->queue);
835 }
836}
837
838static void stop_activity(struct at91_udc *udc)
839{
840 struct usb_gadget_driver *driver = udc->driver;
841 int i;
842
843 if (udc->gadget.speed == USB_SPEED_UNKNOWN)
844 driver = NULL;
845 udc->gadget.speed = USB_SPEED_UNKNOWN;
David Brownell8b2e7662006-07-05 02:38:56 -0700846 udc->suspended = 0;
David Brownellbae4bd82006-01-22 10:32:37 -0800847
848 for (i = 0; i < NUM_ENDPOINTS; i++) {
849 struct at91_ep *ep = &udc->ep[i];
850 ep->stopped = 1;
851 nuke(ep, -ESHUTDOWN);
852 }
853 if (driver)
854 driver->disconnect(&udc->gadget);
855
856 udc_reinit(udc);
857}
858
859static void clk_on(struct at91_udc *udc)
860{
861 if (udc->clocked)
862 return;
863 udc->clocked = 1;
864 clk_enable(udc->iclk);
865 clk_enable(udc->fclk);
866}
867
868static void clk_off(struct at91_udc *udc)
869{
870 if (!udc->clocked)
871 return;
872 udc->clocked = 0;
873 udc->gadget.speed = USB_SPEED_UNKNOWN;
David Brownellbae4bd82006-01-22 10:32:37 -0800874 clk_disable(udc->fclk);
David Brownell8b2e7662006-07-05 02:38:56 -0700875 clk_disable(udc->iclk);
David Brownellbae4bd82006-01-22 10:32:37 -0800876}
877
878/*
879 * activate/deactivate link with host; minimize power usage for
880 * inactive links by cutting clocks and transceiver power.
881 */
882static void pullup(struct at91_udc *udc, int is_on)
883{
884 if (!udc->enabled || !udc->vbus)
885 is_on = 0;
886 DBG("%sactive\n", is_on ? "" : "in");
Andrew Victorffd33262006-12-07 22:44:33 -0800887
David Brownellbae4bd82006-01-22 10:32:37 -0800888 if (is_on) {
889 clk_on(udc);
Andrew Victorffd33262006-12-07 22:44:33 -0800890 at91_udp_write(udc, AT91_UDP_TXVC, 0);
Andrew Victor29ba4b52006-12-07 22:44:38 -0800891 if (cpu_is_at91rm9200())
892 at91_set_gpio_value(udc->board.pullup_pin, 1);
Andrew Victorb2c65612007-02-08 09:42:40 +0100893 else if (cpu_is_at91sam9260() || cpu_is_at91sam9263()) {
Andrew Victor29ba4b52006-12-07 22:44:38 -0800894 u32 txvc = at91_udp_read(udc, AT91_UDP_TXVC);
895
896 txvc |= AT91_UDP_TXVC_PUON;
897 at91_udp_write(udc, AT91_UDP_TXVC, txvc);
898 } else if (cpu_is_at91sam9261()) {
899 u32 usbpucr;
900
901 usbpucr = at91_sys_read(AT91_MATRIX_USBPUCR);
902 usbpucr |= AT91_MATRIX_USBPUCR_PUON;
903 at91_sys_write(AT91_MATRIX_USBPUCR, usbpucr);
904 }
Andrew Victorffd33262006-12-07 22:44:33 -0800905 } else {
David Brownellbae4bd82006-01-22 10:32:37 -0800906 stop_activity(udc);
Andrew Victorffd33262006-12-07 22:44:33 -0800907 at91_udp_write(udc, AT91_UDP_TXVC, AT91_UDP_TXVC_TXVDIS);
Andrew Victor29ba4b52006-12-07 22:44:38 -0800908 if (cpu_is_at91rm9200())
909 at91_set_gpio_value(udc->board.pullup_pin, 0);
Andrew Victorb2c65612007-02-08 09:42:40 +0100910 else if (cpu_is_at91sam9260() || cpu_is_at91sam9263()) {
Andrew Victor29ba4b52006-12-07 22:44:38 -0800911 u32 txvc = at91_udp_read(udc, AT91_UDP_TXVC);
912
913 txvc &= ~AT91_UDP_TXVC_PUON;
914 at91_udp_write(udc, AT91_UDP_TXVC, txvc);
915 } else if (cpu_is_at91sam9261()) {
916 u32 usbpucr;
917
918 usbpucr = at91_sys_read(AT91_MATRIX_USBPUCR);
919 usbpucr &= ~AT91_MATRIX_USBPUCR_PUON;
920 at91_sys_write(AT91_MATRIX_USBPUCR, usbpucr);
921 }
David Brownellbae4bd82006-01-22 10:32:37 -0800922 clk_off(udc);
David Brownellbae4bd82006-01-22 10:32:37 -0800923 }
924}
925
926/* vbus is here! turn everything on that's ready */
927static int at91_vbus_session(struct usb_gadget *gadget, int is_active)
928{
929 struct at91_udc *udc = to_udc(gadget);
930 unsigned long flags;
931
932 // VDBG("vbus %s\n", is_active ? "on" : "off");
933 local_irq_save(flags);
934 udc->vbus = (is_active != 0);
Wojtek Kaniewskibfb7fb72006-12-08 03:26:00 -0800935 if (udc->driver)
936 pullup(udc, is_active);
937 else
938 pullup(udc, 0);
David Brownellbae4bd82006-01-22 10:32:37 -0800939 local_irq_restore(flags);
940 return 0;
941}
942
943static int at91_pullup(struct usb_gadget *gadget, int is_on)
944{
945 struct at91_udc *udc = to_udc(gadget);
946 unsigned long flags;
947
948 local_irq_save(flags);
949 udc->enabled = is_on = !!is_on;
950 pullup(udc, is_on);
951 local_irq_restore(flags);
952 return 0;
953}
954
955static int at91_set_selfpowered(struct usb_gadget *gadget, int is_on)
956{
957 struct at91_udc *udc = to_udc(gadget);
958 unsigned long flags;
959
960 local_irq_save(flags);
961 udc->selfpowered = (is_on != 0);
962 local_irq_restore(flags);
963 return 0;
964}
965
966static const struct usb_gadget_ops at91_udc_ops = {
967 .get_frame = at91_get_frame,
968 .wakeup = at91_wakeup,
969 .set_selfpowered = at91_set_selfpowered,
970 .vbus_session = at91_vbus_session,
971 .pullup = at91_pullup,
972
973 /*
974 * VBUS-powered devices may also also want to support bigger
975 * power budgets after an appropriate SET_CONFIGURATION.
976 */
977 // .vbus_power = at91_vbus_power,
978};
979
980/*-------------------------------------------------------------------------*/
981
982static int handle_ep(struct at91_ep *ep)
983{
984 struct at91_request *req;
985 u32 __iomem *creg = ep->creg;
986 u32 csr = __raw_readl(creg);
987
988 if (!list_empty(&ep->queue))
989 req = list_entry(ep->queue.next,
990 struct at91_request, queue);
991 else
992 req = NULL;
993
994 if (ep->is_in) {
995 if (csr & (AT91_UDP_STALLSENT | AT91_UDP_TXCOMP)) {
996 csr |= CLR_FX;
997 csr &= ~(SET_FX | AT91_UDP_STALLSENT | AT91_UDP_TXCOMP);
998 __raw_writel(csr, creg);
999 }
1000 if (req)
1001 return write_fifo(ep, req);
1002
1003 } else {
1004 if (csr & AT91_UDP_STALLSENT) {
1005 /* STALLSENT bit == ISOERR */
1006 if (ep->is_iso && req)
1007 req->req.status = -EILSEQ;
1008 csr |= CLR_FX;
1009 csr &= ~(SET_FX | AT91_UDP_STALLSENT);
1010 __raw_writel(csr, creg);
1011 csr = __raw_readl(creg);
1012 }
1013 if (req && (csr & RX_DATA_READY))
1014 return read_fifo(ep, req);
1015 }
1016 return 0;
1017}
1018
1019union setup {
1020 u8 raw[8];
1021 struct usb_ctrlrequest r;
1022};
1023
1024static void handle_setup(struct at91_udc *udc, struct at91_ep *ep, u32 csr)
1025{
1026 u32 __iomem *creg = ep->creg;
1027 u8 __iomem *dreg = ep->creg + (AT91_UDP_FDR(0) - AT91_UDP_CSR(0));
1028 unsigned rxcount, i = 0;
1029 u32 tmp;
1030 union setup pkt;
1031 int status = 0;
1032
1033 /* read and ack SETUP; hard-fail for bogus packets */
1034 rxcount = (csr & AT91_UDP_RXBYTECNT) >> 16;
1035 if (likely(rxcount == 8)) {
1036 while (rxcount--)
1037 pkt.raw[i++] = __raw_readb(dreg);
1038 if (pkt.r.bRequestType & USB_DIR_IN) {
1039 csr |= AT91_UDP_DIR;
1040 ep->is_in = 1;
1041 } else {
1042 csr &= ~AT91_UDP_DIR;
1043 ep->is_in = 0;
1044 }
1045 } else {
1046 // REVISIT this happens sometimes under load; why??
1047 ERR("SETUP len %d, csr %08x\n", rxcount, csr);
1048 status = -EINVAL;
1049 }
1050 csr |= CLR_FX;
1051 csr &= ~(SET_FX | AT91_UDP_RXSETUP);
1052 __raw_writel(csr, creg);
1053 udc->wait_for_addr_ack = 0;
1054 udc->wait_for_config_ack = 0;
1055 ep->stopped = 0;
1056 if (unlikely(status != 0))
1057 goto stall;
1058
1059#define w_index le16_to_cpu(pkt.r.wIndex)
1060#define w_value le16_to_cpu(pkt.r.wValue)
1061#define w_length le16_to_cpu(pkt.r.wLength)
1062
1063 VDBG("SETUP %02x.%02x v%04x i%04x l%04x\n",
1064 pkt.r.bRequestType, pkt.r.bRequest,
1065 w_value, w_index, w_length);
1066
1067 /*
1068 * A few standard requests get handled here, ones that touch
1069 * hardware ... notably for device and endpoint features.
1070 */
1071 udc->req_pending = 1;
1072 csr = __raw_readl(creg);
1073 csr |= CLR_FX;
1074 csr &= ~SET_FX;
1075 switch ((pkt.r.bRequestType << 8) | pkt.r.bRequest) {
1076
1077 case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
1078 | USB_REQ_SET_ADDRESS:
1079 __raw_writel(csr | AT91_UDP_TXPKTRDY, creg);
1080 udc->addr = w_value;
1081 udc->wait_for_addr_ack = 1;
1082 udc->req_pending = 0;
1083 /* FADDR is set later, when we ack host STATUS */
1084 return;
1085
1086 case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
1087 | USB_REQ_SET_CONFIGURATION:
Andrew Victorffd33262006-12-07 22:44:33 -08001088 tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT) & AT91_UDP_CONFG;
David Brownellbae4bd82006-01-22 10:32:37 -08001089 if (pkt.r.wValue)
1090 udc->wait_for_config_ack = (tmp == 0);
1091 else
1092 udc->wait_for_config_ack = (tmp != 0);
1093 if (udc->wait_for_config_ack)
1094 VDBG("wait for config\n");
1095 /* CONFG is toggled later, if gadget driver succeeds */
1096 break;
1097
1098 /*
1099 * Hosts may set or clear remote wakeup status, and
1100 * devices may report they're VBUS powered.
1101 */
1102 case ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
1103 | USB_REQ_GET_STATUS:
1104 tmp = (udc->selfpowered << USB_DEVICE_SELF_POWERED);
Andrew Victorffd33262006-12-07 22:44:33 -08001105 if (at91_udp_read(udc, AT91_UDP_GLB_STAT) & AT91_UDP_ESR)
David Brownellbae4bd82006-01-22 10:32:37 -08001106 tmp |= (1 << USB_DEVICE_REMOTE_WAKEUP);
1107 PACKET("get device status\n");
1108 __raw_writeb(tmp, dreg);
1109 __raw_writeb(0, dreg);
1110 goto write_in;
1111 /* then STATUS starts later, automatically */
1112 case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
1113 | USB_REQ_SET_FEATURE:
1114 if (w_value != USB_DEVICE_REMOTE_WAKEUP)
1115 goto stall;
Andrew Victorffd33262006-12-07 22:44:33 -08001116 tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT);
David Brownellbae4bd82006-01-22 10:32:37 -08001117 tmp |= AT91_UDP_ESR;
Andrew Victorffd33262006-12-07 22:44:33 -08001118 at91_udp_write(udc, AT91_UDP_GLB_STAT, tmp);
David Brownellbae4bd82006-01-22 10:32:37 -08001119 goto succeed;
1120 case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
1121 | USB_REQ_CLEAR_FEATURE:
1122 if (w_value != USB_DEVICE_REMOTE_WAKEUP)
1123 goto stall;
Andrew Victorffd33262006-12-07 22:44:33 -08001124 tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT);
David Brownellbae4bd82006-01-22 10:32:37 -08001125 tmp &= ~AT91_UDP_ESR;
Andrew Victorffd33262006-12-07 22:44:33 -08001126 at91_udp_write(udc, AT91_UDP_GLB_STAT, tmp);
David Brownellbae4bd82006-01-22 10:32:37 -08001127 goto succeed;
1128
1129 /*
1130 * Interfaces have no feature settings; this is pretty useless.
1131 * we won't even insist the interface exists...
1132 */
1133 case ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_INTERFACE) << 8)
1134 | USB_REQ_GET_STATUS:
1135 PACKET("get interface status\n");
1136 __raw_writeb(0, dreg);
1137 __raw_writeb(0, dreg);
1138 goto write_in;
1139 /* then STATUS starts later, automatically */
1140 case ((USB_TYPE_STANDARD|USB_RECIP_INTERFACE) << 8)
1141 | USB_REQ_SET_FEATURE:
1142 case ((USB_TYPE_STANDARD|USB_RECIP_INTERFACE) << 8)
1143 | USB_REQ_CLEAR_FEATURE:
1144 goto stall;
1145
1146 /*
1147 * Hosts may clear bulk/intr endpoint halt after the gadget
1148 * driver sets it (not widely used); or set it (for testing)
1149 */
1150 case ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_ENDPOINT) << 8)
1151 | USB_REQ_GET_STATUS:
1152 tmp = w_index & USB_ENDPOINT_NUMBER_MASK;
1153 ep = &udc->ep[tmp];
1154 if (tmp > NUM_ENDPOINTS || (tmp && !ep->desc))
1155 goto stall;
1156
1157 if (tmp) {
1158 if ((w_index & USB_DIR_IN)) {
1159 if (!ep->is_in)
1160 goto stall;
1161 } else if (ep->is_in)
1162 goto stall;
1163 }
1164 PACKET("get %s status\n", ep->ep.name);
1165 if (__raw_readl(ep->creg) & AT91_UDP_FORCESTALL)
1166 tmp = (1 << USB_ENDPOINT_HALT);
1167 else
1168 tmp = 0;
1169 __raw_writeb(tmp, dreg);
1170 __raw_writeb(0, dreg);
1171 goto write_in;
1172 /* then STATUS starts later, automatically */
1173 case ((USB_TYPE_STANDARD|USB_RECIP_ENDPOINT) << 8)
1174 | USB_REQ_SET_FEATURE:
1175 tmp = w_index & USB_ENDPOINT_NUMBER_MASK;
1176 ep = &udc->ep[tmp];
1177 if (w_value != USB_ENDPOINT_HALT || tmp > NUM_ENDPOINTS)
1178 goto stall;
1179 if (!ep->desc || ep->is_iso)
1180 goto stall;
1181 if ((w_index & USB_DIR_IN)) {
1182 if (!ep->is_in)
1183 goto stall;
1184 } else if (ep->is_in)
1185 goto stall;
1186
1187 tmp = __raw_readl(ep->creg);
1188 tmp &= ~SET_FX;
1189 tmp |= CLR_FX | AT91_UDP_FORCESTALL;
1190 __raw_writel(tmp, ep->creg);
1191 goto succeed;
1192 case ((USB_TYPE_STANDARD|USB_RECIP_ENDPOINT) << 8)
1193 | USB_REQ_CLEAR_FEATURE:
1194 tmp = w_index & USB_ENDPOINT_NUMBER_MASK;
1195 ep = &udc->ep[tmp];
1196 if (w_value != USB_ENDPOINT_HALT || tmp > NUM_ENDPOINTS)
1197 goto stall;
1198 if (tmp == 0)
1199 goto succeed;
1200 if (!ep->desc || ep->is_iso)
1201 goto stall;
1202 if ((w_index & USB_DIR_IN)) {
1203 if (!ep->is_in)
1204 goto stall;
1205 } else if (ep->is_in)
1206 goto stall;
1207
Andrew Victorffd33262006-12-07 22:44:33 -08001208 at91_udp_write(udc, AT91_UDP_RST_EP, ep->int_mask);
1209 at91_udp_write(udc, AT91_UDP_RST_EP, 0);
David Brownellbae4bd82006-01-22 10:32:37 -08001210 tmp = __raw_readl(ep->creg);
1211 tmp |= CLR_FX;
1212 tmp &= ~(SET_FX | AT91_UDP_FORCESTALL);
1213 __raw_writel(tmp, ep->creg);
1214 if (!list_empty(&ep->queue))
1215 handle_ep(ep);
1216 goto succeed;
1217 }
1218
1219#undef w_value
1220#undef w_index
1221#undef w_length
1222
1223 /* pass request up to the gadget driver */
Wojtek Kaniewskibfb7fb72006-12-08 03:26:00 -08001224 if (udc->driver)
1225 status = udc->driver->setup(&udc->gadget, &pkt.r);
1226 else
1227 status = -ENODEV;
David Brownellbae4bd82006-01-22 10:32:37 -08001228 if (status < 0) {
1229stall:
1230 VDBG("req %02x.%02x protocol STALL; stat %d\n",
1231 pkt.r.bRequestType, pkt.r.bRequest, status);
1232 csr |= AT91_UDP_FORCESTALL;
1233 __raw_writel(csr, creg);
1234 udc->req_pending = 0;
1235 }
1236 return;
1237
1238succeed:
1239 /* immediate successful (IN) STATUS after zero length DATA */
1240 PACKET("ep0 in/status\n");
1241write_in:
1242 csr |= AT91_UDP_TXPKTRDY;
1243 __raw_writel(csr, creg);
1244 udc->req_pending = 0;
1245 return;
1246}
1247
1248static void handle_ep0(struct at91_udc *udc)
1249{
1250 struct at91_ep *ep0 = &udc->ep[0];
1251 u32 __iomem *creg = ep0->creg;
1252 u32 csr = __raw_readl(creg);
1253 struct at91_request *req;
1254
1255 if (unlikely(csr & AT91_UDP_STALLSENT)) {
1256 nuke(ep0, -EPROTO);
1257 udc->req_pending = 0;
1258 csr |= CLR_FX;
1259 csr &= ~(SET_FX | AT91_UDP_STALLSENT | AT91_UDP_FORCESTALL);
1260 __raw_writel(csr, creg);
1261 VDBG("ep0 stalled\n");
1262 csr = __raw_readl(creg);
1263 }
1264 if (csr & AT91_UDP_RXSETUP) {
1265 nuke(ep0, 0);
1266 udc->req_pending = 0;
1267 handle_setup(udc, ep0, csr);
1268 return;
1269 }
1270
1271 if (list_empty(&ep0->queue))
1272 req = NULL;
1273 else
1274 req = list_entry(ep0->queue.next, struct at91_request, queue);
1275
1276 /* host ACKed an IN packet that we sent */
1277 if (csr & AT91_UDP_TXCOMP) {
1278 csr |= CLR_FX;
1279 csr &= ~(SET_FX | AT91_UDP_TXCOMP);
1280
1281 /* write more IN DATA? */
1282 if (req && ep0->is_in) {
1283 if (handle_ep(ep0))
1284 udc->req_pending = 0;
1285
1286 /*
1287 * Ack after:
1288 * - last IN DATA packet (including GET_STATUS)
1289 * - IN/STATUS for OUT DATA
1290 * - IN/STATUS for any zero-length DATA stage
1291 * except for the IN DATA case, the host should send
1292 * an OUT status later, which we'll ack.
1293 */
1294 } else {
1295 udc->req_pending = 0;
1296 __raw_writel(csr, creg);
1297
1298 /*
1299 * SET_ADDRESS takes effect only after the STATUS
1300 * (to the original address) gets acked.
1301 */
1302 if (udc->wait_for_addr_ack) {
1303 u32 tmp;
1304
Andrew Victorffd33262006-12-07 22:44:33 -08001305 at91_udp_write(udc, AT91_UDP_FADDR,
David Brownell8b2e7662006-07-05 02:38:56 -07001306 AT91_UDP_FEN | udc->addr);
Andrew Victorffd33262006-12-07 22:44:33 -08001307 tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT);
David Brownellbae4bd82006-01-22 10:32:37 -08001308 tmp &= ~AT91_UDP_FADDEN;
1309 if (udc->addr)
1310 tmp |= AT91_UDP_FADDEN;
Andrew Victorffd33262006-12-07 22:44:33 -08001311 at91_udp_write(udc, AT91_UDP_GLB_STAT, tmp);
David Brownellbae4bd82006-01-22 10:32:37 -08001312
1313 udc->wait_for_addr_ack = 0;
1314 VDBG("address %d\n", udc->addr);
1315 }
1316 }
1317 }
1318
1319 /* OUT packet arrived ... */
1320 else if (csr & AT91_UDP_RX_DATA_BK0) {
1321 csr |= CLR_FX;
1322 csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK0);
1323
1324 /* OUT DATA stage */
1325 if (!ep0->is_in) {
1326 if (req) {
1327 if (handle_ep(ep0)) {
1328 /* send IN/STATUS */
1329 PACKET("ep0 in/status\n");
1330 csr = __raw_readl(creg);
1331 csr &= ~SET_FX;
1332 csr |= CLR_FX | AT91_UDP_TXPKTRDY;
1333 __raw_writel(csr, creg);
1334 udc->req_pending = 0;
1335 }
1336 } else if (udc->req_pending) {
1337 /*
1338 * AT91 hardware has a hard time with this
1339 * "deferred response" mode for control-OUT
1340 * transfers. (For control-IN it's fine.)
1341 *
1342 * The normal solution leaves OUT data in the
1343 * fifo until the gadget driver is ready.
1344 * We couldn't do that here without disabling
1345 * the IRQ that tells about SETUP packets,
1346 * e.g. when the host gets impatient...
1347 *
1348 * Working around it by copying into a buffer
1349 * would almost be a non-deferred response,
1350 * except that it wouldn't permit reliable
1351 * stalling of the request. Instead, demand
1352 * that gadget drivers not use this mode.
1353 */
1354 DBG("no control-OUT deferred responses!\n");
1355 __raw_writel(csr | AT91_UDP_FORCESTALL, creg);
1356 udc->req_pending = 0;
1357 }
1358
1359 /* STATUS stage for control-IN; ack. */
1360 } else {
1361 PACKET("ep0 out/status ACK\n");
1362 __raw_writel(csr, creg);
1363
1364 /* "early" status stage */
1365 if (req)
1366 done(ep0, req, 0);
1367 }
1368 }
1369}
1370
David Howells7d12e782006-10-05 14:55:46 +01001371static irqreturn_t at91_udc_irq (int irq, void *_udc)
David Brownellbae4bd82006-01-22 10:32:37 -08001372{
1373 struct at91_udc *udc = _udc;
1374 u32 rescans = 5;
1375
1376 while (rescans--) {
David Brownell8b2e7662006-07-05 02:38:56 -07001377 u32 status;
David Brownellbae4bd82006-01-22 10:32:37 -08001378
Andrew Victorffd33262006-12-07 22:44:33 -08001379 status = at91_udp_read(udc, AT91_UDP_ISR)
1380 & at91_udp_read(udc, AT91_UDP_IMR);
David Brownellbae4bd82006-01-22 10:32:37 -08001381 if (!status)
1382 break;
1383
1384 /* USB reset irq: not maskable */
1385 if (status & AT91_UDP_ENDBUSRES) {
Andrew Victorffd33262006-12-07 22:44:33 -08001386 at91_udp_write(udc, AT91_UDP_IDR, ~MINIMUS_INTERRUPTUS);
1387 at91_udp_write(udc, AT91_UDP_IER, MINIMUS_INTERRUPTUS);
David Brownellbae4bd82006-01-22 10:32:37 -08001388 /* Atmel code clears this irq twice */
Andrew Victorffd33262006-12-07 22:44:33 -08001389 at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_ENDBUSRES);
1390 at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_ENDBUSRES);
David Brownellbae4bd82006-01-22 10:32:37 -08001391 VDBG("end bus reset\n");
1392 udc->addr = 0;
1393 stop_activity(udc);
1394
1395 /* enable ep0 */
Andrew Victorffd33262006-12-07 22:44:33 -08001396 at91_udp_write(udc, AT91_UDP_CSR(0),
David Brownell8b2e7662006-07-05 02:38:56 -07001397 AT91_UDP_EPEDS | AT91_UDP_EPTYPE_CTRL);
David Brownellbae4bd82006-01-22 10:32:37 -08001398 udc->gadget.speed = USB_SPEED_FULL;
1399 udc->suspended = 0;
Andrew Victorffd33262006-12-07 22:44:33 -08001400 at91_udp_write(udc, AT91_UDP_IER, AT91_UDP_EP(0));
David Brownellbae4bd82006-01-22 10:32:37 -08001401
1402 /*
1403 * NOTE: this driver keeps clocks off unless the
David Brownell8b2e7662006-07-05 02:38:56 -07001404 * USB host is present. That saves power, but for
1405 * boards that don't support VBUS detection, both
1406 * clocks need to be active most of the time.
David Brownellbae4bd82006-01-22 10:32:37 -08001407 */
1408
1409 /* host initiated suspend (3+ms bus idle) */
1410 } else if (status & AT91_UDP_RXSUSP) {
Andrew Victorffd33262006-12-07 22:44:33 -08001411 at91_udp_write(udc, AT91_UDP_IDR, AT91_UDP_RXSUSP);
1412 at91_udp_write(udc, AT91_UDP_IER, AT91_UDP_RXRSM);
1413 at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_RXSUSP);
David Brownellbae4bd82006-01-22 10:32:37 -08001414 // VDBG("bus suspend\n");
1415 if (udc->suspended)
1416 continue;
1417 udc->suspended = 1;
1418
1419 /*
1420 * NOTE: when suspending a VBUS-powered device, the
1421 * gadget driver should switch into slow clock mode
1422 * and then into standby to avoid drawing more than
1423 * 500uA power (2500uA for some high-power configs).
1424 */
1425 if (udc->driver && udc->driver->suspend)
1426 udc->driver->suspend(&udc->gadget);
1427
1428 /* host initiated resume */
1429 } else if (status & AT91_UDP_RXRSM) {
Andrew Victorffd33262006-12-07 22:44:33 -08001430 at91_udp_write(udc, AT91_UDP_IDR, AT91_UDP_RXRSM);
1431 at91_udp_write(udc, AT91_UDP_IER, AT91_UDP_RXSUSP);
1432 at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_RXRSM);
David Brownellbae4bd82006-01-22 10:32:37 -08001433 // VDBG("bus resume\n");
1434 if (!udc->suspended)
1435 continue;
1436 udc->suspended = 0;
1437
1438 /*
1439 * NOTE: for a VBUS-powered device, the gadget driver
1440 * would normally want to switch out of slow clock
1441 * mode into normal mode.
1442 */
1443 if (udc->driver && udc->driver->resume)
1444 udc->driver->resume(&udc->gadget);
1445
1446 /* endpoint IRQs are cleared by handling them */
1447 } else {
1448 int i;
1449 unsigned mask = 1;
1450 struct at91_ep *ep = &udc->ep[1];
1451
1452 if (status & mask)
1453 handle_ep0(udc);
1454 for (i = 1; i < NUM_ENDPOINTS; i++) {
1455 mask <<= 1;
1456 if (status & mask)
1457 handle_ep(ep);
1458 ep++;
1459 }
1460 }
1461 }
1462
1463 return IRQ_HANDLED;
1464}
1465
1466/*-------------------------------------------------------------------------*/
1467
David Brownell8b2e7662006-07-05 02:38:56 -07001468static void nop_release(struct device *dev)
1469{
1470 /* nothing to free */
1471}
1472
David Brownellbae4bd82006-01-22 10:32:37 -08001473static struct at91_udc controller = {
1474 .gadget = {
David Brownell8b2e7662006-07-05 02:38:56 -07001475 .ops = &at91_udc_ops,
1476 .ep0 = &controller.ep[0].ep,
1477 .name = driver_name,
1478 .dev = {
1479 .bus_id = "gadget",
1480 .release = nop_release,
David Brownellbae4bd82006-01-22 10:32:37 -08001481 }
1482 },
1483 .ep[0] = {
1484 .ep = {
1485 .name = ep0name,
1486 .ops = &at91_ep_ops,
1487 },
1488 .udc = &controller,
1489 .maxpacket = 8,
David Brownellbae4bd82006-01-22 10:32:37 -08001490 .int_mask = 1 << 0,
1491 },
1492 .ep[1] = {
1493 .ep = {
1494 .name = "ep1",
1495 .ops = &at91_ep_ops,
1496 },
1497 .udc = &controller,
1498 .is_pingpong = 1,
1499 .maxpacket = 64,
David Brownellbae4bd82006-01-22 10:32:37 -08001500 .int_mask = 1 << 1,
1501 },
1502 .ep[2] = {
1503 .ep = {
1504 .name = "ep2",
1505 .ops = &at91_ep_ops,
1506 },
1507 .udc = &controller,
1508 .is_pingpong = 1,
1509 .maxpacket = 64,
David Brownellbae4bd82006-01-22 10:32:37 -08001510 .int_mask = 1 << 2,
1511 },
1512 .ep[3] = {
1513 .ep = {
1514 /* could actually do bulk too */
1515 .name = "ep3-int",
1516 .ops = &at91_ep_ops,
1517 },
1518 .udc = &controller,
1519 .maxpacket = 8,
David Brownellbae4bd82006-01-22 10:32:37 -08001520 .int_mask = 1 << 3,
1521 },
1522 .ep[4] = {
1523 .ep = {
1524 .name = "ep4",
1525 .ops = &at91_ep_ops,
1526 },
1527 .udc = &controller,
1528 .is_pingpong = 1,
1529 .maxpacket = 256,
David Brownellbae4bd82006-01-22 10:32:37 -08001530 .int_mask = 1 << 4,
1531 },
1532 .ep[5] = {
1533 .ep = {
1534 .name = "ep5",
1535 .ops = &at91_ep_ops,
1536 },
1537 .udc = &controller,
1538 .is_pingpong = 1,
1539 .maxpacket = 256,
David Brownellbae4bd82006-01-22 10:32:37 -08001540 .int_mask = 1 << 5,
1541 },
David Brownell8b2e7662006-07-05 02:38:56 -07001542 /* ep6 and ep7 are also reserved (custom silicon might use them) */
David Brownellbae4bd82006-01-22 10:32:37 -08001543};
1544
David Howells7d12e782006-10-05 14:55:46 +01001545static irqreturn_t at91_vbus_irq(int irq, void *_udc)
David Brownellbae4bd82006-01-22 10:32:37 -08001546{
1547 struct at91_udc *udc = _udc;
1548 unsigned value;
1549
1550 /* vbus needs at least brief debouncing */
1551 udelay(10);
1552 value = at91_get_gpio_value(udc->board.vbus_pin);
1553 if (value != udc->vbus)
1554 at91_vbus_session(&udc->gadget, value);
1555
1556 return IRQ_HANDLED;
1557}
1558
1559int usb_gadget_register_driver (struct usb_gadget_driver *driver)
1560{
1561 struct at91_udc *udc = &controller;
1562 int retval;
1563
1564 if (!driver
Wojtek Kaniewskibc92c322006-12-08 09:39:36 -08001565 || driver->speed < USB_SPEED_FULL
David Brownellbae4bd82006-01-22 10:32:37 -08001566 || !driver->bind
David Brownellbae4bd82006-01-22 10:32:37 -08001567 || !driver->setup) {
1568 DBG("bad parameter.\n");
1569 return -EINVAL;
1570 }
1571
1572 if (udc->driver) {
1573 DBG("UDC already has a gadget driver\n");
1574 return -EBUSY;
1575 }
1576
1577 udc->driver = driver;
1578 udc->gadget.dev.driver = &driver->driver;
1579 udc->gadget.dev.driver_data = &driver->driver;
1580 udc->enabled = 1;
1581 udc->selfpowered = 1;
1582
1583 retval = driver->bind(&udc->gadget);
1584 if (retval) {
1585 DBG("driver->bind() returned %d\n", retval);
1586 udc->driver = NULL;
Wojtek Kaniewski943c4412006-12-08 03:23:00 -08001587 udc->gadget.dev.driver = NULL;
1588 udc->gadget.dev.driver_data = NULL;
1589 udc->enabled = 0;
1590 udc->selfpowered = 0;
David Brownellbae4bd82006-01-22 10:32:37 -08001591 return retval;
1592 }
1593
1594 local_irq_disable();
1595 pullup(udc, 1);
1596 local_irq_enable();
1597
1598 DBG("bound to %s\n", driver->driver.name);
1599 return 0;
1600}
1601EXPORT_SYMBOL (usb_gadget_register_driver);
1602
1603int usb_gadget_unregister_driver (struct usb_gadget_driver *driver)
1604{
1605 struct at91_udc *udc = &controller;
1606
David Brownell6bea4762006-12-05 03:15:33 -08001607 if (!driver || driver != udc->driver || !driver->unbind)
David Brownellbae4bd82006-01-22 10:32:37 -08001608 return -EINVAL;
1609
1610 local_irq_disable();
1611 udc->enabled = 0;
Andrew Victorffd33262006-12-07 22:44:33 -08001612 at91_udp_write(udc, AT91_UDP_IDR, ~0);
David Brownellbae4bd82006-01-22 10:32:37 -08001613 pullup(udc, 0);
1614 local_irq_enable();
1615
1616 driver->unbind(&udc->gadget);
1617 udc->driver = NULL;
1618
1619 DBG("unbound from %s\n", driver->driver.name);
1620 return 0;
1621}
1622EXPORT_SYMBOL (usb_gadget_unregister_driver);
1623
1624/*-------------------------------------------------------------------------*/
1625
1626static void at91udc_shutdown(struct platform_device *dev)
1627{
1628 /* force disconnect on reboot */
1629 pullup(platform_get_drvdata(dev), 0);
1630}
1631
David Brownell398acce2007-02-15 18:47:17 -08001632static int __init at91udc_probe(struct platform_device *pdev)
David Brownellbae4bd82006-01-22 10:32:37 -08001633{
1634 struct device *dev = &pdev->dev;
1635 struct at91_udc *udc;
1636 int retval;
Andrew Victorffd33262006-12-07 22:44:33 -08001637 struct resource *res;
David Brownellbae4bd82006-01-22 10:32:37 -08001638
1639 if (!dev->platform_data) {
1640 /* small (so we copy it) but critical! */
1641 DBG("missing platform_data\n");
1642 return -ENODEV;
1643 }
1644
David Brownell8b2e7662006-07-05 02:38:56 -07001645 if (pdev->num_resources != 2) {
1646 DBG("invalid num_resources");
1647 return -ENODEV;
1648 }
1649 if ((pdev->resource[0].flags != IORESOURCE_MEM)
1650 || (pdev->resource[1].flags != IORESOURCE_IRQ)) {
1651 DBG("invalid resource type");
1652 return -ENODEV;
1653 }
1654
Andrew Victorffd33262006-12-07 22:44:33 -08001655 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1656 if (!res)
1657 return -ENXIO;
1658
1659 if (!request_mem_region(res->start,
1660 res->end - res->start + 1,
1661 driver_name)) {
David Brownellbae4bd82006-01-22 10:32:37 -08001662 DBG("someone's using UDC memory\n");
1663 return -EBUSY;
1664 }
1665
1666 /* init software state */
1667 udc = &controller;
1668 udc->gadget.dev.parent = dev;
1669 udc->board = *(struct at91_udc_data *) dev->platform_data;
1670 udc->pdev = pdev;
David Brownellbae4bd82006-01-22 10:32:37 -08001671 udc->enabled = 0;
1672
Andrew Victorffd33262006-12-07 22:44:33 -08001673 udc->udp_baseaddr = ioremap(res->start, res->end - res->start + 1);
1674 if (!udc->udp_baseaddr) {
1675 release_mem_region(res->start, res->end - res->start + 1);
1676 return -ENOMEM;
1677 }
1678
1679 udc_reinit(udc);
1680
David Brownellbae4bd82006-01-22 10:32:37 -08001681 /* get interface and function clocks */
1682 udc->iclk = clk_get(dev, "udc_clk");
1683 udc->fclk = clk_get(dev, "udpck");
1684 if (IS_ERR(udc->iclk) || IS_ERR(udc->fclk)) {
1685 DBG("clocks missing\n");
Andrew Victor29ba4b52006-12-07 22:44:38 -08001686 retval = -ENODEV;
1687 goto fail0;
David Brownellbae4bd82006-01-22 10:32:37 -08001688 }
1689
1690 retval = device_register(&udc->gadget.dev);
1691 if (retval < 0)
1692 goto fail0;
1693
David Brownell8b2e7662006-07-05 02:38:56 -07001694 /* don't do anything until we have both gadget driver and VBUS */
1695 clk_enable(udc->iclk);
Andrew Victorffd33262006-12-07 22:44:33 -08001696 at91_udp_write(udc, AT91_UDP_TXVC, AT91_UDP_TXVC_TXVDIS);
1697 at91_udp_write(udc, AT91_UDP_IDR, 0xffffffff);
Andrew Victor29ba4b52006-12-07 22:44:38 -08001698 /* Clear all pending interrupts - UDP may be used by bootloader. */
1699 at91_udp_write(udc, AT91_UDP_ICR, 0xffffffff);
David Brownell8b2e7662006-07-05 02:38:56 -07001700 clk_disable(udc->iclk);
David Brownellbae4bd82006-01-22 10:32:37 -08001701
1702 /* request UDC and maybe VBUS irqs */
David Brownell8b2e7662006-07-05 02:38:56 -07001703 udc->udp_irq = platform_get_irq(pdev, 0);
1704 if (request_irq(udc->udp_irq, at91_udc_irq,
1705 IRQF_DISABLED, driver_name, udc)) {
1706 DBG("request irq %d failed\n", udc->udp_irq);
David Brownellbae4bd82006-01-22 10:32:37 -08001707 retval = -EBUSY;
1708 goto fail1;
1709 }
1710 if (udc->board.vbus_pin > 0) {
Andrew Victor29ba4b52006-12-07 22:44:38 -08001711 /*
1712 * Get the initial state of VBUS - we cannot expect
1713 * a pending interrupt.
1714 */
1715 udc->vbus = at91_get_gpio_value(udc->board.vbus_pin);
David Brownell8b2e7662006-07-05 02:38:56 -07001716 if (request_irq(udc->board.vbus_pin, at91_vbus_irq,
1717 IRQF_DISABLED, driver_name, udc)) {
1718 DBG("request vbus irq %d failed\n",
1719 udc->board.vbus_pin);
1720 free_irq(udc->udp_irq, udc);
David Brownellbae4bd82006-01-22 10:32:37 -08001721 retval = -EBUSY;
1722 goto fail1;
1723 }
1724 } else {
1725 DBG("no VBUS detection, assuming always-on\n");
1726 udc->vbus = 1;
1727 }
1728 dev_set_drvdata(dev, udc);
David Brownell8b2e7662006-07-05 02:38:56 -07001729 device_init_wakeup(dev, 1);
David Brownellbae4bd82006-01-22 10:32:37 -08001730 create_debug_file(udc);
1731
1732 INFO("%s version %s\n", driver_name, DRIVER_VERSION);
1733 return 0;
1734
1735fail1:
1736 device_unregister(&udc->gadget.dev);
1737fail0:
Andrew Victorffd33262006-12-07 22:44:33 -08001738 release_mem_region(res->start, res->end - res->start + 1);
David Brownellbae4bd82006-01-22 10:32:37 -08001739 DBG("%s probe failed, %d\n", driver_name, retval);
1740 return retval;
1741}
1742
David Brownell398acce2007-02-15 18:47:17 -08001743static int __exit at91udc_remove(struct platform_device *pdev)
David Brownellbae4bd82006-01-22 10:32:37 -08001744{
David Brownell8b2e7662006-07-05 02:38:56 -07001745 struct at91_udc *udc = platform_get_drvdata(pdev);
Andrew Victorffd33262006-12-07 22:44:33 -08001746 struct resource *res;
David Brownellbae4bd82006-01-22 10:32:37 -08001747
1748 DBG("remove\n");
1749
David Brownell6bea4762006-12-05 03:15:33 -08001750 if (udc->driver)
1751 return -EBUSY;
David Brownellbae4bd82006-01-22 10:32:37 -08001752
David Brownell6bea4762006-12-05 03:15:33 -08001753 pullup(udc, 0);
David Brownellbae4bd82006-01-22 10:32:37 -08001754
David Brownell8b2e7662006-07-05 02:38:56 -07001755 device_init_wakeup(&pdev->dev, 0);
David Brownellbae4bd82006-01-22 10:32:37 -08001756 remove_debug_file(udc);
1757 if (udc->board.vbus_pin > 0)
1758 free_irq(udc->board.vbus_pin, udc);
David Brownell8b2e7662006-07-05 02:38:56 -07001759 free_irq(udc->udp_irq, udc);
David Brownellbae4bd82006-01-22 10:32:37 -08001760 device_unregister(&udc->gadget.dev);
Andrew Victorffd33262006-12-07 22:44:33 -08001761
1762 iounmap(udc->udp_baseaddr);
1763 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1764 release_mem_region(res->start, res->end - res->start + 1);
David Brownellbae4bd82006-01-22 10:32:37 -08001765
1766 clk_put(udc->iclk);
1767 clk_put(udc->fclk);
1768
1769 return 0;
1770}
1771
1772#ifdef CONFIG_PM
David Brownell8b2e7662006-07-05 02:38:56 -07001773static int at91udc_suspend(struct platform_device *pdev, pm_message_t mesg)
David Brownellbae4bd82006-01-22 10:32:37 -08001774{
David Brownell8b2e7662006-07-05 02:38:56 -07001775 struct at91_udc *udc = platform_get_drvdata(pdev);
1776 int wake = udc->driver && device_may_wakeup(&pdev->dev);
David Brownellbae4bd82006-01-22 10:32:37 -08001777
David Brownell8b2e7662006-07-05 02:38:56 -07001778 /* Unless we can act normally to the host (letting it wake us up
1779 * whenever it has work for us) force disconnect. Wakeup requires
1780 * PLLB for USB events (signaling for reset, wakeup, or incoming
1781 * tokens) and VBUS irqs (on systems which support them).
David Brownellbae4bd82006-01-22 10:32:37 -08001782 */
David Brownell8b2e7662006-07-05 02:38:56 -07001783 if ((!udc->suspended && udc->addr)
1784 || !wake
1785 || at91_suspend_entering_slow_clock()) {
David Brownellbae4bd82006-01-22 10:32:37 -08001786 pullup(udc, 0);
David Brownell66e56ce2007-01-16 12:46:39 -08001787 wake = 0;
David Brownell8b2e7662006-07-05 02:38:56 -07001788 } else
1789 enable_irq_wake(udc->udp_irq);
1790
David Brownell66e56ce2007-01-16 12:46:39 -08001791 udc->active_suspend = wake;
1792 if (udc->board.vbus_pin > 0 && wake)
1793 enable_irq_wake(udc->board.vbus_pin);
David Brownellbae4bd82006-01-22 10:32:37 -08001794 return 0;
1795}
1796
David Brownell8b2e7662006-07-05 02:38:56 -07001797static int at91udc_resume(struct platform_device *pdev)
David Brownellbae4bd82006-01-22 10:32:37 -08001798{
David Brownell8b2e7662006-07-05 02:38:56 -07001799 struct at91_udc *udc = platform_get_drvdata(pdev);
David Brownellbae4bd82006-01-22 10:32:37 -08001800
David Brownell66e56ce2007-01-16 12:46:39 -08001801 if (udc->board.vbus_pin > 0 && udc->active_suspend)
1802 disable_irq_wake(udc->board.vbus_pin);
1803
David Brownellbae4bd82006-01-22 10:32:37 -08001804 /* maybe reconnect to host; if so, clocks on */
David Brownell66e56ce2007-01-16 12:46:39 -08001805 if (udc->active_suspend)
1806 disable_irq_wake(udc->udp_irq);
1807 else
1808 pullup(udc, 1);
David Brownellbae4bd82006-01-22 10:32:37 -08001809 return 0;
1810}
1811#else
1812#define at91udc_suspend NULL
1813#define at91udc_resume NULL
1814#endif
1815
David Brownelldee497d2007-02-24 13:54:56 -08001816static struct platform_driver at91_udc_driver = {
David Brownell398acce2007-02-15 18:47:17 -08001817 .remove = __exit_p(at91udc_remove),
David Brownellbae4bd82006-01-22 10:32:37 -08001818 .shutdown = at91udc_shutdown,
1819 .suspend = at91udc_suspend,
David Brownell8b2e7662006-07-05 02:38:56 -07001820 .resume = at91udc_resume,
David Brownellbae4bd82006-01-22 10:32:37 -08001821 .driver = {
1822 .name = (char *) driver_name,
1823 .owner = THIS_MODULE,
1824 },
1825};
1826
David Brownell398acce2007-02-15 18:47:17 -08001827static int __init udc_init_module(void)
David Brownellbae4bd82006-01-22 10:32:37 -08001828{
David Brownelldee497d2007-02-24 13:54:56 -08001829 return platform_driver_probe(&at91_udc_driver, at91udc_probe);
David Brownellbae4bd82006-01-22 10:32:37 -08001830}
1831module_init(udc_init_module);
1832
David Brownell398acce2007-02-15 18:47:17 -08001833static void __exit udc_exit_module(void)
David Brownellbae4bd82006-01-22 10:32:37 -08001834{
David Brownelldee497d2007-02-24 13:54:56 -08001835 platform_driver_unregister(&at91_udc_driver);
David Brownellbae4bd82006-01-22 10:32:37 -08001836}
1837module_exit(udc_exit_module);
1838
David Brownell8b2e7662006-07-05 02:38:56 -07001839MODULE_DESCRIPTION("AT91 udc driver");
David Brownellbae4bd82006-01-22 10:32:37 -08001840MODULE_AUTHOR("Thomas Rathbone, David Brownell");
1841MODULE_LICENSE("GPL");