]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/usb/gadget/pxa25x_udc.c
usb: otg: Convert all users to pass struct usb_otg for OTG functions
[mirror_ubuntu-artful-kernel.git] / drivers / usb / gadget / pxa25x_udc.c
CommitLineData
1da177e4 1/*
91987693 2 * Intel PXA25x and IXP4xx on-chip full speed USB device controllers
1da177e4
LT
3 *
4 * Copyright (C) 2002 Intrinsyc, Inc. (Frank Becker)
5 * Copyright (C) 2003 Robert Schwebel, Pengutronix
6 * Copyright (C) 2003 Benedikt Spranger, Pengutronix
7 * Copyright (C) 2003 David Brownell
8 * Copyright (C) 2003 Joshua Wise
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
1da177e4
LT
14 */
15
040fa1b9 16/* #define VERBOSE_DEBUG */
1da177e4 17
9068a4c6 18#include <linux/device.h>
1da177e4
LT
19#include <linux/module.h>
20#include <linux/kernel.h>
21#include <linux/ioport.h>
22#include <linux/types.h>
1da177e4
LT
23#include <linux/errno.h>
24#include <linux/delay.h>
1da177e4
LT
25#include <linux/slab.h>
26#include <linux/init.h>
27#include <linux/timer.h>
28#include <linux/list.h>
29#include <linux/interrupt.h>
1da177e4 30#include <linux/mm.h>
d052d1be 31#include <linux/platform_device.h>
1da177e4 32#include <linux/dma-mapping.h>
c7a3bd17 33#include <linux/irq.h>
6549e6c9
RK
34#include <linux/clk.h>
35#include <linux/err.h>
040fa1b9
DES
36#include <linux/seq_file.h>
37#include <linux/debugfs.h>
284d115e 38#include <linux/io.h>
268bb0ce 39#include <linux/prefetch.h>
1da177e4
LT
40
41#include <asm/byteorder.h>
42#include <asm/dma.h>
9068a4c6 43#include <asm/gpio.h>
1da177e4
LT
44#include <asm/system.h>
45#include <asm/mach-types.h>
46#include <asm/unaligned.h>
1da177e4 47
5f848137 48#include <linux/usb/ch9.h>
9454a57a 49#include <linux/usb/gadget.h>
ab26d20f 50#include <linux/usb/otg.h>
1da177e4 51
284d115e
RK
52/*
53 * This driver is PXA25x only. Grab the right register definitions.
54 */
55#ifdef CONFIG_ARCH_PXA
a09e64fb 56#include <mach/pxa25x-udc.h>
284d115e
RK
57#endif
58
0dc726bb
EM
59#ifdef CONFIG_ARCH_LUBBOCK
60#include <mach/lubbock.h>
61#endif
62
9068a4c6 63#include <asm/mach/udc_pxa2xx.h>
1da177e4
LT
64
65
66/*
91987693 67 * This driver handles the USB Device Controller (UDC) in Intel's PXA 25x
1da177e4
LT
68 * series processors. The UDC for the IXP 4xx series is very similar.
69 * There are fifteen endpoints, in addition to ep0.
70 *
71 * Such controller drivers work with a gadget driver. The gadget driver
72 * returns descriptors, implements configuration and data protocols used
73 * by the host to interact with this device, and allocates endpoints to
74 * the different protocol interfaces. The controller driver virtualizes
75 * usb hardware so that the gadget drivers will be more portable.
34ebcd28 76 *
1da177e4
LT
77 * This UDC hardware wants to implement a bit too much USB protocol, so
78 * it constrains the sorts of USB configuration change events that work.
79 * The errata for these chips are misleading; some "fixed" bugs from
80 * pxa250 a0/a1 b0/b1/b2 sure act like they're still there.
ad8c623f
DB
81 *
82 * Note that the UDC hardware supports DMA (except on IXP) but that's
83 * not used here. IN-DMA (to host) is simple enough, when the data is
84 * suitably aligned (16 bytes) ... the network stack doesn't do that,
85 * other software can. OUT-DMA is buggy in most chip versions, as well
86 * as poorly designed (data toggle not automatic). So this driver won't
87 * bother using DMA. (Mostly-working IN-DMA support was available in
88 * kernels before 2.6.23, but was never enabled or well tested.)
1da177e4
LT
89 */
90
ad8c623f 91#define DRIVER_VERSION "30-June-2007"
91987693 92#define DRIVER_DESC "PXA 25x USB Device Controller driver"
1da177e4
LT
93
94
7a857620 95static const char driver_name [] = "pxa25x_udc";
1da177e4
LT
96
97static const char ep0name [] = "ep0";
98
99
1da177e4 100#ifdef CONFIG_ARCH_IXP4XX
1da177e4
LT
101
102/* cpu-specific register addresses are compiled in to this code */
103#ifdef CONFIG_ARCH_PXA
104#error "Can't configure both IXP and PXA"
105#endif
106
64cc2dd9
DES
107/* IXP doesn't yet support <linux/clk.h> */
108#define clk_get(dev,name) NULL
109#define clk_enable(clk) do { } while (0)
110#define clk_disable(clk) do { } while (0)
111#define clk_put(clk) do { } while (0)
112
1da177e4
LT
113#endif
114
7a857620 115#include "pxa25x_udc.h"
1da177e4
LT
116
117
7a857620 118#ifdef CONFIG_USB_PXA25X_SMALL
1da177e4
LT
119#define SIZE_STR " (small)"
120#else
121#define SIZE_STR ""
122#endif
123
1da177e4 124/* ---------------------------------------------------------------------------
34ebcd28 125 * endpoint related parts of the api to the usb controller hardware,
1da177e4
LT
126 * used by gadget driver; and the inner talker-to-hardware core.
127 * ---------------------------------------------------------------------------
128 */
129
7a857620
PZ
130static void pxa25x_ep_fifo_flush (struct usb_ep *ep);
131static void nuke (struct pxa25x_ep *, int status);
1da177e4 132
b2bbb20b
DB
133/* one GPIO should control a D+ pullup, so host sees this device (or not) */
134static void pullup_off(void)
135{
136 struct pxa2xx_udc_mach_info *mach = the_controller->mach;
8fb105f5 137 int off_level = mach->gpio_pullup_inverted;
b2bbb20b 138
56a075dc 139 if (gpio_is_valid(mach->gpio_pullup))
8fb105f5 140 gpio_set_value(mach->gpio_pullup, off_level);
b2bbb20b
DB
141 else if (mach->udc_command)
142 mach->udc_command(PXA2XX_UDC_CMD_DISCONNECT);
143}
144
145static void pullup_on(void)
146{
147 struct pxa2xx_udc_mach_info *mach = the_controller->mach;
8fb105f5 148 int on_level = !mach->gpio_pullup_inverted;
b2bbb20b 149
56a075dc 150 if (gpio_is_valid(mach->gpio_pullup))
8fb105f5 151 gpio_set_value(mach->gpio_pullup, on_level);
b2bbb20b
DB
152 else if (mach->udc_command)
153 mach->udc_command(PXA2XX_UDC_CMD_CONNECT);
154}
155
1da177e4
LT
156static void pio_irq_enable(int bEndpointAddress)
157{
158 bEndpointAddress &= 0xf;
159 if (bEndpointAddress < 8)
160 UICR0 &= ~(1 << bEndpointAddress);
161 else {
162 bEndpointAddress -= 8;
163 UICR1 &= ~(1 << bEndpointAddress);
164 }
165}
166
167static void pio_irq_disable(int bEndpointAddress)
168{
169 bEndpointAddress &= 0xf;
170 if (bEndpointAddress < 8)
171 UICR0 |= 1 << bEndpointAddress;
172 else {
173 bEndpointAddress -= 8;
174 UICR1 |= 1 << bEndpointAddress;
175 }
176}
177
178/* The UDCCR reg contains mask and interrupt status bits,
179 * so using '|=' isn't safe as it may ack an interrupt.
180 */
181#define UDCCR_MASK_BITS (UDCCR_REM | UDCCR_SRM | UDCCR_UDE)
182
183static inline void udc_set_mask_UDCCR(int mask)
184{
185 UDCCR = (UDCCR & UDCCR_MASK_BITS) | (mask & UDCCR_MASK_BITS);
186}
187
188static inline void udc_clear_mask_UDCCR(int mask)
189{
190 UDCCR = (UDCCR & UDCCR_MASK_BITS) & ~(mask & UDCCR_MASK_BITS);
191}
192
193static inline void udc_ack_int_UDCCR(int mask)
194{
195 /* udccr contains the bits we dont want to change */
196 __u32 udccr = UDCCR & UDCCR_MASK_BITS;
197
198 UDCCR = udccr | (mask & ~UDCCR_MASK_BITS);
199}
200
201/*
202 * endpoint enable/disable
203 *
7a857620 204 * we need to verify the descriptors used to enable endpoints. since pxa25x
1da177e4
LT
205 * endpoint configurations are fixed, and are pretty much always enabled,
206 * there's not a lot to manage here.
207 *
7a857620 208 * because pxa25x can't selectively initialize bulk (or interrupt) endpoints,
1da177e4
LT
209 * (resetting endpoint halt and toggle), SET_INTERFACE is unusable except
210 * for a single interface (with only the default altsetting) and for gadget
211 * drivers that don't halt endpoints (not reset by set_interface). that also
212 * means that if you use ISO, you must violate the USB spec rule that all
213 * iso endpoints must be in non-default altsettings.
214 */
7a857620 215static int pxa25x_ep_enable (struct usb_ep *_ep,
1da177e4
LT
216 const struct usb_endpoint_descriptor *desc)
217{
7a857620
PZ
218 struct pxa25x_ep *ep;
219 struct pxa25x_udc *dev;
1da177e4 220
7a857620 221 ep = container_of (_ep, struct pxa25x_ep, ep);
1da177e4
LT
222 if (!_ep || !desc || ep->desc || _ep->name == ep0name
223 || desc->bDescriptorType != USB_DT_ENDPOINT
224 || ep->bEndpointAddress != desc->bEndpointAddress
29cc8897 225 || ep->fifo_size < usb_endpoint_maxp (desc)) {
441b62c1 226 DMSG("%s, bad ep or descriptor\n", __func__);
1da177e4
LT
227 return -EINVAL;
228 }
229
230 /* xfer types must match, except that interrupt ~= bulk */
231 if (ep->bmAttributes != desc->bmAttributes
232 && ep->bmAttributes != USB_ENDPOINT_XFER_BULK
233 && desc->bmAttributes != USB_ENDPOINT_XFER_INT) {
441b62c1 234 DMSG("%s, %s type mismatch\n", __func__, _ep->name);
1da177e4
LT
235 return -EINVAL;
236 }
237
238 /* hardware _could_ do smaller, but driver doesn't */
239 if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
29cc8897 240 && usb_endpoint_maxp (desc)
1da177e4
LT
241 != BULK_FIFO_SIZE)
242 || !desc->wMaxPacketSize) {
441b62c1 243 DMSG("%s, bad %s maxpacket\n", __func__, _ep->name);
1da177e4
LT
244 return -ERANGE;
245 }
246
247 dev = ep->dev;
248 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
441b62c1 249 DMSG("%s, bogus device state\n", __func__);
1da177e4
LT
250 return -ESHUTDOWN;
251 }
252
253 ep->desc = desc;
1da177e4 254 ep->stopped = 0;
ad8c623f 255 ep->pio_irqs = 0;
29cc8897 256 ep->ep.maxpacket = usb_endpoint_maxp (desc);
1da177e4
LT
257
258 /* flush fifo (mostly for OUT buffers) */
7a857620 259 pxa25x_ep_fifo_flush (_ep);
1da177e4
LT
260
261 /* ... reset halt state too, if we could ... */
262
1da177e4
LT
263 DBG(DBG_VERBOSE, "enabled %s\n", _ep->name);
264 return 0;
265}
266
7a857620 267static int pxa25x_ep_disable (struct usb_ep *_ep)
1da177e4 268{
7a857620 269 struct pxa25x_ep *ep;
91987693 270 unsigned long flags;
1da177e4 271
7a857620 272 ep = container_of (_ep, struct pxa25x_ep, ep);
1da177e4 273 if (!_ep || !ep->desc) {
441b62c1 274 DMSG("%s, %s not enabled\n", __func__,
1da177e4
LT
275 _ep ? ep->ep.name : NULL);
276 return -EINVAL;
277 }
91987693
DB
278 local_irq_save(flags);
279
1da177e4
LT
280 nuke (ep, -ESHUTDOWN);
281
1da177e4 282 /* flush fifo (mostly for IN buffers) */
7a857620 283 pxa25x_ep_fifo_flush (_ep);
1da177e4
LT
284
285 ep->desc = NULL;
286 ep->stopped = 1;
287
91987693 288 local_irq_restore(flags);
1da177e4
LT
289 DBG(DBG_VERBOSE, "%s disabled\n", _ep->name);
290 return 0;
291}
292
293/*-------------------------------------------------------------------------*/
294
7a857620 295/* for the pxa25x, these can just wrap kmalloc/kfree. gadget drivers
1da177e4
LT
296 * must still pass correctly initialized endpoints, since other controller
297 * drivers may care about how it's currently set up (dma issues etc).
298 */
299
300/*
7a857620 301 * pxa25x_ep_alloc_request - allocate a request data structure
1da177e4
LT
302 */
303static struct usb_request *
7a857620 304pxa25x_ep_alloc_request (struct usb_ep *_ep, gfp_t gfp_flags)
1da177e4 305{
7a857620 306 struct pxa25x_request *req;
1da177e4 307
7039f422 308 req = kzalloc(sizeof(*req), gfp_flags);
1da177e4
LT
309 if (!req)
310 return NULL;
311
1da177e4
LT
312 INIT_LIST_HEAD (&req->queue);
313 return &req->req;
314}
315
316
317/*
7a857620 318 * pxa25x_ep_free_request - deallocate a request data structure
1da177e4
LT
319 */
320static void
7a857620 321pxa25x_ep_free_request (struct usb_ep *_ep, struct usb_request *_req)
1da177e4 322{
7a857620 323 struct pxa25x_request *req;
1da177e4 324
7a857620 325 req = container_of (_req, struct pxa25x_request, req);
b6c63937 326 WARN_ON(!list_empty (&req->queue));
1da177e4
LT
327 kfree(req);
328}
329
1da177e4
LT
330/*-------------------------------------------------------------------------*/
331
332/*
333 * done - retire a request; caller blocked irqs
334 */
7a857620 335static void done(struct pxa25x_ep *ep, struct pxa25x_request *req, int status)
1da177e4
LT
336{
337 unsigned stopped = ep->stopped;
338
339 list_del_init(&req->queue);
340
341 if (likely (req->req.status == -EINPROGRESS))
342 req->req.status = status;
343 else
344 status = req->req.status;
345
346 if (status && status != -ESHUTDOWN)
347 DBG(DBG_VERBOSE, "complete %s req %p stat %d len %u/%u\n",
348 ep->ep.name, &req->req, status,
349 req->req.actual, req->req.length);
350
351 /* don't modify queue heads during completion callback */
352 ep->stopped = 1;
353 req->req.complete(&ep->ep, &req->req);
354 ep->stopped = stopped;
355}
356
357
7a857620 358static inline void ep0_idle (struct pxa25x_udc *dev)
1da177e4
LT
359{
360 dev->ep0state = EP0_IDLE;
361}
362
363static int
7a857620 364write_packet(volatile u32 *uddr, struct pxa25x_request *req, unsigned max)
1da177e4
LT
365{
366 u8 *buf;
367 unsigned length, count;
368
369 buf = req->req.buf + req->req.actual;
370 prefetch(buf);
371
372 /* how big will this packet be? */
373 length = min(req->req.length - req->req.actual, max);
374 req->req.actual += length;
375
376 count = length;
377 while (likely(count--))
378 *uddr = *buf++;
379
380 return length;
381}
382
383/*
384 * write to an IN endpoint fifo, as many packets as possible.
385 * irqs will use this to write the rest later.
386 * caller guarantees at least one packet buffer is ready (or a zlp).
387 */
388static int
7a857620 389write_fifo (struct pxa25x_ep *ep, struct pxa25x_request *req)
1da177e4
LT
390{
391 unsigned max;
392
29cc8897 393 max = usb_endpoint_maxp(ep->desc);
1da177e4
LT
394 do {
395 unsigned count;
396 int is_last, is_short;
397
398 count = write_packet(ep->reg_uddr, req, max);
399
400 /* last packet is usually short (or a zlp) */
401 if (unlikely (count != max))
402 is_last = is_short = 1;
403 else {
404 if (likely(req->req.length != req->req.actual)
405 || req->req.zero)
406 is_last = 0;
407 else
408 is_last = 1;
409 /* interrupt/iso maxpacket may not fill the fifo */
410 is_short = unlikely (max < ep->fifo_size);
411 }
412
413 DBG(DBG_VERY_NOISY, "wrote %s %d bytes%s%s %d left %p\n",
414 ep->ep.name, count,
415 is_last ? "/L" : "", is_short ? "/S" : "",
416 req->req.length - req->req.actual, req);
417
418 /* let loose that packet. maybe try writing another one,
419 * double buffering might work. TSP, TPC, and TFS
420 * bit values are the same for all normal IN endpoints.
421 */
422 *ep->reg_udccs = UDCCS_BI_TPC;
423 if (is_short)
424 *ep->reg_udccs = UDCCS_BI_TSP;
425
426 /* requests complete when all IN data is in the FIFO */
427 if (is_last) {
428 done (ep, req, 0);
ad8c623f 429 if (list_empty(&ep->queue))
1da177e4 430 pio_irq_disable (ep->bEndpointAddress);
1da177e4
LT
431 return 1;
432 }
433
434 // TODO experiment: how robust can fifo mode tweaking be?
435 // double buffering is off in the default fifo mode, which
436 // prevents TFS from being set here.
437
438 } while (*ep->reg_udccs & UDCCS_BI_TFS);
439 return 0;
440}
441
442/* caller asserts req->pending (ep0 irq status nyet cleared); starts
443 * ep0 data stage. these chips want very simple state transitions.
444 */
445static inline
7a857620 446void ep0start(struct pxa25x_udc *dev, u32 flags, const char *tag)
1da177e4
LT
447{
448 UDCCS0 = flags|UDCCS0_SA|UDCCS0_OPR;
449 USIR0 = USIR0_IR0;
450 dev->req_pending = 0;
451 DBG(DBG_VERY_NOISY, "%s %s, %02x/%02x\n",
441b62c1 452 __func__, tag, UDCCS0, flags);
1da177e4
LT
453}
454
455static int
7a857620 456write_ep0_fifo (struct pxa25x_ep *ep, struct pxa25x_request *req)
1da177e4
LT
457{
458 unsigned count;
459 int is_short;
460
461 count = write_packet(&UDDR0, req, EP0_FIFO_SIZE);
462 ep->dev->stats.write.bytes += count;
463
464 /* last packet "must be" short (or a zlp) */
465 is_short = (count != EP0_FIFO_SIZE);
466
467 DBG(DBG_VERY_NOISY, "ep0in %d bytes %d left %p\n", count,
468 req->req.length - req->req.actual, req);
469
470 if (unlikely (is_short)) {
471 if (ep->dev->req_pending)
472 ep0start(ep->dev, UDCCS0_IPR, "short IN");
473 else
474 UDCCS0 = UDCCS0_IPR;
475
476 count = req->req.length;
477 done (ep, req, 0);
478 ep0_idle(ep->dev);
043ea18b 479#ifndef CONFIG_ARCH_IXP4XX
1da177e4
LT
480#if 1
481 /* This seems to get rid of lost status irqs in some cases:
482 * host responds quickly, or next request involves config
483 * change automagic, or should have been hidden, or ...
484 *
485 * FIXME get rid of all udelays possible...
486 */
487 if (count >= EP0_FIFO_SIZE) {
488 count = 100;
489 do {
490 if ((UDCCS0 & UDCCS0_OPR) != 0) {
491 /* clear OPR, generate ack */
492 UDCCS0 = UDCCS0_OPR;
493 break;
494 }
495 count--;
496 udelay(1);
497 } while (count);
498 }
043ea18b 499#endif
1da177e4
LT
500#endif
501 } else if (ep->dev->req_pending)
502 ep0start(ep->dev, 0, "IN");
503 return is_short;
504}
505
506
507/*
508 * read_fifo - unload packet(s) from the fifo we use for usb OUT
509 * transfers and put them into the request. caller should have made
510 * sure there's at least one packet ready.
511 *
512 * returns true if the request completed because of short packet or the
513 * request buffer having filled (and maybe overran till end-of-packet).
514 */
515static int
7a857620 516read_fifo (struct pxa25x_ep *ep, struct pxa25x_request *req)
1da177e4
LT
517{
518 for (;;) {
519 u32 udccs;
520 u8 *buf;
521 unsigned bufferspace, count, is_short;
522
523 /* make sure there's a packet in the FIFO.
524 * UDCCS_{BO,IO}_RPC are all the same bit value.
525 * UDCCS_{BO,IO}_RNE are all the same bit value.
526 */
527 udccs = *ep->reg_udccs;
528 if (unlikely ((udccs & UDCCS_BO_RPC) == 0))
529 break;
530 buf = req->req.buf + req->req.actual;
531 prefetchw(buf);
532 bufferspace = req->req.length - req->req.actual;
533
534 /* read all bytes from this packet */
535 if (likely (udccs & UDCCS_BO_RNE)) {
536 count = 1 + (0x0ff & *ep->reg_ubcr);
537 req->req.actual += min (count, bufferspace);
538 } else /* zlp */
539 count = 0;
540 is_short = (count < ep->ep.maxpacket);
541 DBG(DBG_VERY_NOISY, "read %s %02x, %d bytes%s req %p %d/%d\n",
542 ep->ep.name, udccs, count,
543 is_short ? "/S" : "",
544 req, req->req.actual, req->req.length);
545 while (likely (count-- != 0)) {
546 u8 byte = (u8) *ep->reg_uddr;
547
548 if (unlikely (bufferspace == 0)) {
549 /* this happens when the driver's buffer
550 * is smaller than what the host sent.
551 * discard the extra data.
552 */
553 if (req->req.status != -EOVERFLOW)
554 DMSG("%s overflow %d\n",
555 ep->ep.name, count);
556 req->req.status = -EOVERFLOW;
557 } else {
558 *buf++ = byte;
559 bufferspace--;
560 }
561 }
562 *ep->reg_udccs = UDCCS_BO_RPC;
563 /* RPC/RSP/RNE could now reflect the other packet buffer */
564
565 /* iso is one request per packet */
566 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
567 if (udccs & UDCCS_IO_ROF)
568 req->req.status = -EHOSTUNREACH;
569 /* more like "is_done" */
570 is_short = 1;
571 }
572
573 /* completion */
574 if (is_short || req->req.actual == req->req.length) {
575 done (ep, req, 0);
576 if (list_empty(&ep->queue))
577 pio_irq_disable (ep->bEndpointAddress);
578 return 1;
579 }
580
581 /* finished that packet. the next one may be waiting... */
582 }
583 return 0;
584}
585
586/*
587 * special ep0 version of the above. no UBCR0 or double buffering; status
588 * handshaking is magic. most device protocols don't need control-OUT.
589 * CDC vendor commands (and RNDIS), mass storage CB/CBI, and some other
590 * protocols do use them.
591 */
592static int
7a857620 593read_ep0_fifo (struct pxa25x_ep *ep, struct pxa25x_request *req)
1da177e4
LT
594{
595 u8 *buf, byte;
596 unsigned bufferspace;
597
598 buf = req->req.buf + req->req.actual;
599 bufferspace = req->req.length - req->req.actual;
600
601 while (UDCCS0 & UDCCS0_RNE) {
602 byte = (u8) UDDR0;
603
604 if (unlikely (bufferspace == 0)) {
605 /* this happens when the driver's buffer
606 * is smaller than what the host sent.
607 * discard the extra data.
608 */
609 if (req->req.status != -EOVERFLOW)
610 DMSG("%s overflow\n", ep->ep.name);
611 req->req.status = -EOVERFLOW;
612 } else {
613 *buf++ = byte;
614 req->req.actual++;
615 bufferspace--;
616 }
617 }
618
619 UDCCS0 = UDCCS0_OPR | UDCCS0_IPR;
620
621 /* completion */
622 if (req->req.actual >= req->req.length)
623 return 1;
624
625 /* finished that packet. the next one may be waiting... */
626 return 0;
627}
628
1da177e4
LT
629/*-------------------------------------------------------------------------*/
630
631static int
7a857620 632pxa25x_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
1da177e4 633{
7a857620
PZ
634 struct pxa25x_request *req;
635 struct pxa25x_ep *ep;
636 struct pxa25x_udc *dev;
1da177e4
LT
637 unsigned long flags;
638
7a857620 639 req = container_of(_req, struct pxa25x_request, req);
1da177e4
LT
640 if (unlikely (!_req || !_req->complete || !_req->buf
641 || !list_empty(&req->queue))) {
441b62c1 642 DMSG("%s, bad params\n", __func__);
1da177e4
LT
643 return -EINVAL;
644 }
645
7a857620 646 ep = container_of(_ep, struct pxa25x_ep, ep);
1da177e4 647 if (unlikely (!_ep || (!ep->desc && ep->ep.name != ep0name))) {
441b62c1 648 DMSG("%s, bad ep\n", __func__);
1da177e4
LT
649 return -EINVAL;
650 }
651
652 dev = ep->dev;
653 if (unlikely (!dev->driver
654 || dev->gadget.speed == USB_SPEED_UNKNOWN)) {
441b62c1 655 DMSG("%s, bogus device state\n", __func__);
1da177e4
LT
656 return -ESHUTDOWN;
657 }
658
659 /* iso is always one packet per request, that's the only way
660 * we can report per-packet status. that also helps with dma.
661 */
662 if (unlikely (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
29cc8897 663 && req->req.length > usb_endpoint_maxp (ep->desc)))
1da177e4
LT
664 return -EMSGSIZE;
665
1da177e4 666 DBG(DBG_NOISY, "%s queue req %p, len %d buf %p\n",
ad8c623f 667 _ep->name, _req, _req->length, _req->buf);
1da177e4
LT
668
669 local_irq_save(flags);
670
671 _req->status = -EINPROGRESS;
672 _req->actual = 0;
673
674 /* kickstart this i/o queue? */
675 if (list_empty(&ep->queue) && !ep->stopped) {
040fa1b9 676 if (ep->desc == NULL/* ep0 */) {
1da177e4
LT
677 unsigned length = _req->length;
678
679 switch (dev->ep0state) {
680 case EP0_IN_DATA_PHASE:
681 dev->stats.write.ops++;
682 if (write_ep0_fifo(ep, req))
683 req = NULL;
684 break;
685
686 case EP0_OUT_DATA_PHASE:
687 dev->stats.read.ops++;
688 /* messy ... */
689 if (dev->req_config) {
690 DBG(DBG_VERBOSE, "ep0 config ack%s\n",
691 dev->has_cfr ? "" : " raced");
692 if (dev->has_cfr)
693 UDCCFR = UDCCFR_AREN|UDCCFR_ACM
694 |UDCCFR_MB1;
695 done(ep, req, 0);
696 dev->ep0state = EP0_END_XFER;
697 local_irq_restore (flags);
698 return 0;
699 }
700 if (dev->req_pending)
701 ep0start(dev, UDCCS0_IPR, "OUT");
702 if (length == 0 || ((UDCCS0 & UDCCS0_RNE) != 0
703 && read_ep0_fifo(ep, req))) {
704 ep0_idle(dev);
705 done(ep, req, 0);
706 req = NULL;
707 }
708 break;
709
710 default:
711 DMSG("ep0 i/o, odd state %d\n", dev->ep0state);
712 local_irq_restore (flags);
713 return -EL2HLT;
714 }
1da177e4 715 /* can the FIFO can satisfy the request immediately? */
91987693
DB
716 } else if ((ep->bEndpointAddress & USB_DIR_IN) != 0) {
717 if ((*ep->reg_udccs & UDCCS_BI_TFS) != 0
718 && write_fifo(ep, req))
719 req = NULL;
1da177e4
LT
720 } else if ((*ep->reg_udccs & UDCCS_BO_RFS) != 0
721 && read_fifo(ep, req)) {
722 req = NULL;
723 }
724
ad8c623f 725 if (likely (req && ep->desc))
1da177e4
LT
726 pio_irq_enable(ep->bEndpointAddress);
727 }
728
729 /* pio or dma irq handler advances the queue. */
040fa1b9 730 if (likely(req != NULL))
1da177e4
LT
731 list_add_tail(&req->queue, &ep->queue);
732 local_irq_restore(flags);
733
734 return 0;
735}
736
737
738/*
34ebcd28 739 * nuke - dequeue ALL requests
1da177e4 740 */
7a857620 741static void nuke(struct pxa25x_ep *ep, int status)
1da177e4 742{
7a857620 743 struct pxa25x_request *req;
1da177e4
LT
744
745 /* called with irqs blocked */
1da177e4
LT
746 while (!list_empty(&ep->queue)) {
747 req = list_entry(ep->queue.next,
7a857620 748 struct pxa25x_request,
1da177e4
LT
749 queue);
750 done(ep, req, status);
751 }
752 if (ep->desc)
753 pio_irq_disable (ep->bEndpointAddress);
754}
755
756
757/* dequeue JUST ONE request */
7a857620 758static int pxa25x_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1da177e4 759{
7a857620
PZ
760 struct pxa25x_ep *ep;
761 struct pxa25x_request *req;
1da177e4
LT
762 unsigned long flags;
763
7a857620 764 ep = container_of(_ep, struct pxa25x_ep, ep);
1da177e4
LT
765 if (!_ep || ep->ep.name == ep0name)
766 return -EINVAL;
767
768 local_irq_save(flags);
769
770 /* make sure it's actually queued on this endpoint */
771 list_for_each_entry (req, &ep->queue, queue) {
772 if (&req->req == _req)
773 break;
774 }
775 if (&req->req != _req) {
776 local_irq_restore(flags);
777 return -EINVAL;
778 }
779
ad8c623f 780 done(ep, req, -ECONNRESET);
1da177e4
LT
781
782 local_irq_restore(flags);
783 return 0;
784}
785
786/*-------------------------------------------------------------------------*/
787
7a857620 788static int pxa25x_ep_set_halt(struct usb_ep *_ep, int value)
1da177e4 789{
7a857620 790 struct pxa25x_ep *ep;
1da177e4
LT
791 unsigned long flags;
792
7a857620 793 ep = container_of(_ep, struct pxa25x_ep, ep);
1da177e4
LT
794 if (unlikely (!_ep
795 || (!ep->desc && ep->ep.name != ep0name))
796 || ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
441b62c1 797 DMSG("%s, bad ep\n", __func__);
1da177e4
LT
798 return -EINVAL;
799 }
800 if (value == 0) {
801 /* this path (reset toggle+halt) is needed to implement
802 * SET_INTERFACE on normal hardware. but it can't be
803 * done from software on the PXA UDC, and the hardware
804 * forgets to do it as part of SET_INTERFACE automagic.
805 */
806 DMSG("only host can clear %s halt\n", _ep->name);
807 return -EROFS;
808 }
809
810 local_irq_save(flags);
811
812 if ((ep->bEndpointAddress & USB_DIR_IN) != 0
813 && ((*ep->reg_udccs & UDCCS_BI_TFS) == 0
814 || !list_empty(&ep->queue))) {
815 local_irq_restore(flags);
816 return -EAGAIN;
817 }
818
819 /* FST bit is the same for control, bulk in, bulk out, interrupt in */
820 *ep->reg_udccs = UDCCS_BI_FST|UDCCS_BI_FTF;
821
822 /* ep0 needs special care */
823 if (!ep->desc) {
824 start_watchdog(ep->dev);
825 ep->dev->req_pending = 0;
826 ep->dev->ep0state = EP0_STALL;
827
34ebcd28
DB
828 /* and bulk/intr endpoints like dropping stalls too */
829 } else {
830 unsigned i;
831 for (i = 0; i < 1000; i += 20) {
832 if (*ep->reg_udccs & UDCCS_BI_SST)
833 break;
834 udelay(20);
835 }
836 }
837 local_irq_restore(flags);
1da177e4
LT
838
839 DBG(DBG_VERBOSE, "%s halt\n", _ep->name);
840 return 0;
841}
842
7a857620 843static int pxa25x_ep_fifo_status(struct usb_ep *_ep)
1da177e4 844{
7a857620 845 struct pxa25x_ep *ep;
1da177e4 846
7a857620 847 ep = container_of(_ep, struct pxa25x_ep, ep);
1da177e4 848 if (!_ep) {
441b62c1 849 DMSG("%s, bad ep\n", __func__);
1da177e4
LT
850 return -ENODEV;
851 }
852 /* pxa can't report unclaimed bytes from IN fifos */
853 if ((ep->bEndpointAddress & USB_DIR_IN) != 0)
854 return -EOPNOTSUPP;
855 if (ep->dev->gadget.speed == USB_SPEED_UNKNOWN
856 || (*ep->reg_udccs & UDCCS_BO_RFS) == 0)
857 return 0;
858 else
859 return (*ep->reg_ubcr & 0xfff) + 1;
860}
861
7a857620 862static void pxa25x_ep_fifo_flush(struct usb_ep *_ep)
1da177e4 863{
7a857620 864 struct pxa25x_ep *ep;
1da177e4 865
7a857620 866 ep = container_of(_ep, struct pxa25x_ep, ep);
1da177e4 867 if (!_ep || ep->ep.name == ep0name || !list_empty(&ep->queue)) {
441b62c1 868 DMSG("%s, bad ep\n", __func__);
1da177e4
LT
869 return;
870 }
871
872 /* toggle and halt bits stay unchanged */
873
874 /* for OUT, just read and discard the FIFO contents. */
875 if ((ep->bEndpointAddress & USB_DIR_IN) == 0) {
876 while (((*ep->reg_udccs) & UDCCS_BO_RNE) != 0)
877 (void) *ep->reg_uddr;
878 return;
879 }
880
881 /* most IN status is the same, but ISO can't stall */
882 *ep->reg_udccs = UDCCS_BI_TPC|UDCCS_BI_FTF|UDCCS_BI_TUR
22eb36f4
RK
883 | (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
884 ? 0 : UDCCS_BI_SST);
1da177e4
LT
885}
886
887
7a857620
PZ
888static struct usb_ep_ops pxa25x_ep_ops = {
889 .enable = pxa25x_ep_enable,
890 .disable = pxa25x_ep_disable,
1da177e4 891
7a857620
PZ
892 .alloc_request = pxa25x_ep_alloc_request,
893 .free_request = pxa25x_ep_free_request,
1da177e4 894
7a857620
PZ
895 .queue = pxa25x_ep_queue,
896 .dequeue = pxa25x_ep_dequeue,
1da177e4 897
7a857620
PZ
898 .set_halt = pxa25x_ep_set_halt,
899 .fifo_status = pxa25x_ep_fifo_status,
900 .fifo_flush = pxa25x_ep_fifo_flush,
1da177e4
LT
901};
902
903
904/* ---------------------------------------------------------------------------
34ebcd28 905 * device-scoped parts of the api to the usb controller hardware
1da177e4
LT
906 * ---------------------------------------------------------------------------
907 */
908
7a857620 909static int pxa25x_udc_get_frame(struct usb_gadget *_gadget)
1da177e4
LT
910{
911 return ((UFNRH & 0x07) << 8) | (UFNRL & 0xff);
912}
913
7a857620 914static int pxa25x_udc_wakeup(struct usb_gadget *_gadget)
1da177e4
LT
915{
916 /* host may not have enabled remote wakeup */
917 if ((UDCCS0 & UDCCS0_DRWF) == 0)
918 return -EHOSTUNREACH;
919 udc_set_mask_UDCCR(UDCCR_RSM);
920 return 0;
921}
922
7a857620
PZ
923static void stop_activity(struct pxa25x_udc *, struct usb_gadget_driver *);
924static void udc_enable (struct pxa25x_udc *);
925static void udc_disable(struct pxa25x_udc *);
1da177e4
LT
926
927/* We disable the UDC -- and its 48 MHz clock -- whenever it's not
34ebcd28 928 * in active use.
1da177e4 929 */
7a857620 930static int pullup(struct pxa25x_udc *udc)
1da177e4 931{
64cc2dd9 932 int is_active = udc->vbus && udc->pullup && !udc->suspended;
1da177e4 933 DMSG("%s\n", is_active ? "active" : "inactive");
64cc2dd9
DES
934 if (is_active) {
935 if (!udc->active) {
936 udc->active = 1;
937 /* Enable clock for USB device */
938 clk_enable(udc->clk);
939 udc_enable(udc);
1da177e4 940 }
64cc2dd9
DES
941 } else {
942 if (udc->active) {
943 if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
944 DMSG("disconnect %s\n", udc->driver
945 ? udc->driver->driver.name
946 : "(no driver)");
947 stop_activity(udc, udc->driver);
948 }
949 udc_disable(udc);
950 /* Disable clock for USB device */
951 clk_disable(udc->clk);
952 udc->active = 0;
953 }
954
1da177e4
LT
955 }
956 return 0;
957}
958
959/* VBUS reporting logically comes from a transceiver */
7a857620 960static int pxa25x_udc_vbus_session(struct usb_gadget *_gadget, int is_active)
1da177e4 961{
7a857620 962 struct pxa25x_udc *udc;
1da177e4 963
7a857620 964 udc = container_of(_gadget, struct pxa25x_udc, gadget);
47fd6f7c 965 udc->vbus = is_active;
1da177e4 966 DMSG("vbus %s\n", is_active ? "supplied" : "inactive");
64cc2dd9 967 pullup(udc);
1da177e4
LT
968 return 0;
969}
970
971/* drivers may have software control over D+ pullup */
7a857620 972static int pxa25x_udc_pullup(struct usb_gadget *_gadget, int is_active)
1da177e4 973{
7a857620 974 struct pxa25x_udc *udc;
1da177e4 975
7a857620 976 udc = container_of(_gadget, struct pxa25x_udc, gadget);
1da177e4
LT
977
978 /* not all boards support pullup control */
56a075dc 979 if (!gpio_is_valid(udc->mach->gpio_pullup) && !udc->mach->udc_command)
1da177e4
LT
980 return -EOPNOTSUPP;
981
64cc2dd9
DES
982 udc->pullup = (is_active != 0);
983 pullup(udc);
1da177e4
LT
984 return 0;
985}
986
ab26d20f
PZ
987/* boards may consume current from VBUS, up to 100-500mA based on config.
988 * the 500uA suspend ceiling means that exclusively vbus-powered PXA designs
989 * violate USB specs.
990 */
991static int pxa25x_udc_vbus_draw(struct usb_gadget *_gadget, unsigned mA)
992{
993 struct pxa25x_udc *udc;
994
995 udc = container_of(_gadget, struct pxa25x_udc, gadget);
996
997 if (udc->transceiver)
b96d3b08 998 return usb_phy_set_power(udc->transceiver, mA);
ab26d20f
PZ
999 return -EOPNOTSUPP;
1000}
1001
0f91349b
SAS
1002static int pxa25x_start(struct usb_gadget_driver *driver,
1003 int (*bind)(struct usb_gadget *));
1004static int pxa25x_stop(struct usb_gadget_driver *driver);
1005
7a857620
PZ
1006static const struct usb_gadget_ops pxa25x_udc_ops = {
1007 .get_frame = pxa25x_udc_get_frame,
1008 .wakeup = pxa25x_udc_wakeup,
1009 .vbus_session = pxa25x_udc_vbus_session,
1010 .pullup = pxa25x_udc_pullup,
ab26d20f 1011 .vbus_draw = pxa25x_udc_vbus_draw,
0f91349b
SAS
1012 .start = pxa25x_start,
1013 .stop = pxa25x_stop,
1da177e4
LT
1014};
1015
1016/*-------------------------------------------------------------------------*/
1017
040fa1b9 1018#ifdef CONFIG_USB_GADGET_DEBUG_FS
1da177e4
LT
1019
1020static int
64cc2dd9 1021udc_seq_show(struct seq_file *m, void *_d)
1da177e4 1022{
7a857620 1023 struct pxa25x_udc *dev = m->private;
1da177e4 1024 unsigned long flags;
040fa1b9 1025 int i;
1da177e4
LT
1026 u32 tmp;
1027
1da177e4
LT
1028 local_irq_save(flags);
1029
1030 /* basic device status */
040fa1b9 1031 seq_printf(m, DRIVER_DESC "\n"
1da177e4 1032 "%s version: %s\nGadget driver: %s\nHost %s\n\n",
ad8c623f 1033 driver_name, DRIVER_VERSION SIZE_STR "(pio)",
1da177e4 1034 dev->driver ? dev->driver->driver.name : "(none)",
a8ecc860 1035 dev->gadget.speed == USB_SPEED_FULL ? "full speed" : "disconnected");
1da177e4
LT
1036
1037 /* registers for device and ep0 */
040fa1b9 1038 seq_printf(m,
1da177e4
LT
1039 "uicr %02X.%02X, usir %02X.%02x, ufnr %02X.%02X\n",
1040 UICR1, UICR0, USIR1, USIR0, UFNRH, UFNRL);
1da177e4
LT
1041
1042 tmp = UDCCR;
040fa1b9 1043 seq_printf(m,
1da177e4
LT
1044 "udccr %02X =%s%s%s%s%s%s%s%s\n", tmp,
1045 (tmp & UDCCR_REM) ? " rem" : "",
1046 (tmp & UDCCR_RSTIR) ? " rstir" : "",
1047 (tmp & UDCCR_SRM) ? " srm" : "",
1048 (tmp & UDCCR_SUSIR) ? " susir" : "",
1049 (tmp & UDCCR_RESIR) ? " resir" : "",
1050 (tmp & UDCCR_RSM) ? " rsm" : "",
1051 (tmp & UDCCR_UDA) ? " uda" : "",
1052 (tmp & UDCCR_UDE) ? " ude" : "");
1da177e4
LT
1053
1054 tmp = UDCCS0;
040fa1b9 1055 seq_printf(m,
1da177e4
LT
1056 "udccs0 %02X =%s%s%s%s%s%s%s%s\n", tmp,
1057 (tmp & UDCCS0_SA) ? " sa" : "",
1058 (tmp & UDCCS0_RNE) ? " rne" : "",
1059 (tmp & UDCCS0_FST) ? " fst" : "",
1060 (tmp & UDCCS0_SST) ? " sst" : "",
1061 (tmp & UDCCS0_DRWF) ? " dwrf" : "",
1062 (tmp & UDCCS0_FTF) ? " ftf" : "",
1063 (tmp & UDCCS0_IPR) ? " ipr" : "",
1064 (tmp & UDCCS0_OPR) ? " opr" : "");
1da177e4
LT
1065
1066 if (dev->has_cfr) {
1067 tmp = UDCCFR;
040fa1b9 1068 seq_printf(m,
1da177e4
LT
1069 "udccfr %02X =%s%s\n", tmp,
1070 (tmp & UDCCFR_AREN) ? " aren" : "",
1071 (tmp & UDCCFR_ACM) ? " acm" : "");
1da177e4
LT
1072 }
1073
a8ecc860 1074 if (dev->gadget.speed != USB_SPEED_FULL || !dev->driver)
1da177e4
LT
1075 goto done;
1076
040fa1b9 1077 seq_printf(m, "ep0 IN %lu/%lu, OUT %lu/%lu\nirqs %lu\n\n",
1da177e4
LT
1078 dev->stats.write.bytes, dev->stats.write.ops,
1079 dev->stats.read.bytes, dev->stats.read.ops,
1080 dev->stats.irqs);
1da177e4
LT
1081
1082 /* dump endpoint queues */
1083 for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
7a857620
PZ
1084 struct pxa25x_ep *ep = &dev->ep [i];
1085 struct pxa25x_request *req;
1da177e4
LT
1086
1087 if (i != 0) {
040fa1b9 1088 const struct usb_endpoint_descriptor *desc;
1da177e4 1089
040fa1b9
DES
1090 desc = ep->desc;
1091 if (!desc)
1da177e4
LT
1092 continue;
1093 tmp = *dev->ep [i].reg_udccs;
040fa1b9 1094 seq_printf(m,
ad8c623f 1095 "%s max %d %s udccs %02x irqs %lu\n",
29cc8897 1096 ep->ep.name, usb_endpoint_maxp(desc),
ad8c623f 1097 "pio", tmp, ep->pio_irqs);
1da177e4
LT
1098 /* TODO translate all five groups of udccs bits! */
1099
1100 } else /* ep0 should only have one transfer queued */
040fa1b9 1101 seq_printf(m, "ep0 max 16 pio irqs %lu\n",
1da177e4 1102 ep->pio_irqs);
1da177e4
LT
1103
1104 if (list_empty(&ep->queue)) {
040fa1b9 1105 seq_printf(m, "\t(nothing queued)\n");
1da177e4
LT
1106 continue;
1107 }
1108 list_for_each_entry(req, &ep->queue, queue) {
040fa1b9 1109 seq_printf(m,
1da177e4
LT
1110 "\treq %p len %d/%d buf %p\n",
1111 &req->req, req->req.actual,
1112 req->req.length, req->req.buf);
1da177e4
LT
1113 }
1114 }
1115
1116done:
1117 local_irq_restore(flags);
040fa1b9 1118 return 0;
1da177e4
LT
1119}
1120
040fa1b9
DES
1121static int
1122udc_debugfs_open(struct inode *inode, struct file *file)
1123{
1124 return single_open(file, udc_seq_show, inode->i_private);
1125}
1126
1127static const struct file_operations debug_fops = {
1128 .open = udc_debugfs_open,
1129 .read = seq_read,
1130 .llseek = seq_lseek,
1131 .release = single_release,
1132 .owner = THIS_MODULE,
1133};
1134
1135#define create_debug_files(dev) \
1136 do { \
1137 dev->debugfs_udc = debugfs_create_file(dev->gadget.name, \
1138 S_IRUGO, NULL, dev, &debug_fops); \
1139 } while (0)
1140#define remove_debug_files(dev) \
1141 do { \
1142 if (dev->debugfs_udc) \
1143 debugfs_remove(dev->debugfs_udc); \
1144 } while (0)
1da177e4
LT
1145
1146#else /* !CONFIG_USB_GADGET_DEBUG_FILES */
1147
040fa1b9
DES
1148#define create_debug_files(dev) do {} while (0)
1149#define remove_debug_files(dev) do {} while (0)
1da177e4
LT
1150
1151#endif /* CONFIG_USB_GADGET_DEBUG_FILES */
1152
1da177e4
LT
1153/*-------------------------------------------------------------------------*/
1154
1155/*
34ebcd28 1156 * udc_disable - disable USB device controller
1da177e4 1157 */
7a857620 1158static void udc_disable(struct pxa25x_udc *dev)
1da177e4
LT
1159{
1160 /* block all irqs */
1161 udc_set_mask_UDCCR(UDCCR_SRM|UDCCR_REM);
1162 UICR0 = UICR1 = 0xff;
1163 UFNRH = UFNRH_SIM;
1164
1165 /* if hardware supports it, disconnect from usb */
91987693 1166 pullup_off();
1da177e4
LT
1167
1168 udc_clear_mask_UDCCR(UDCCR_UDE);
1169
1da177e4
LT
1170 ep0_idle (dev);
1171 dev->gadget.speed = USB_SPEED_UNKNOWN;
1da177e4
LT
1172}
1173
1174
1175/*
34ebcd28 1176 * udc_reinit - initialize software state
1da177e4 1177 */
7a857620 1178static void udc_reinit(struct pxa25x_udc *dev)
1da177e4
LT
1179{
1180 u32 i;
1181
1182 /* device/ep0 records init */
1183 INIT_LIST_HEAD (&dev->gadget.ep_list);
1184 INIT_LIST_HEAD (&dev->gadget.ep0->ep_list);
1185 dev->ep0state = EP0_IDLE;
1186
1187 /* basic endpoint records init */
1188 for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
7a857620 1189 struct pxa25x_ep *ep = &dev->ep[i];
1da177e4
LT
1190
1191 if (i != 0)
1192 list_add_tail (&ep->ep.ep_list, &dev->gadget.ep_list);
1193
1194 ep->desc = NULL;
1195 ep->stopped = 0;
1196 INIT_LIST_HEAD (&ep->queue);
ad8c623f 1197 ep->pio_irqs = 0;
1da177e4
LT
1198 }
1199
1200 /* the rest was statically initialized, and is read-only */
1201}
1202
1203/* until it's enabled, this UDC should be completely invisible
1204 * to any USB host.
1205 */
7a857620 1206static void udc_enable (struct pxa25x_udc *dev)
1da177e4
LT
1207{
1208 udc_clear_mask_UDCCR(UDCCR_UDE);
1209
1da177e4
LT
1210 /* try to clear these bits before we enable the udc */
1211 udc_ack_int_UDCCR(UDCCR_SUSIR|/*UDCCR_RSTIR|*/UDCCR_RESIR);
1212
1213 ep0_idle(dev);
1214 dev->gadget.speed = USB_SPEED_UNKNOWN;
1215 dev->stats.irqs = 0;
1216
1217 /*
1218 * sequence taken from chapter 12.5.10, PXA250 AppProcDevManual:
1219 * - enable UDC
1220 * - if RESET is already in progress, ack interrupt
1221 * - unmask reset interrupt
1222 */
1223 udc_set_mask_UDCCR(UDCCR_UDE);
1224 if (!(UDCCR & UDCCR_UDA))
1225 udc_ack_int_UDCCR(UDCCR_RSTIR);
1226
1227 if (dev->has_cfr /* UDC_RES2 is defined */) {
1228 /* pxa255 (a0+) can avoid a set_config race that could
1229 * prevent gadget drivers from configuring correctly
1230 */
1231 UDCCFR = UDCCFR_ACM | UDCCFR_MB1;
1232 } else {
1233 /* "USB test mode" for pxa250 errata 40-42 (stepping a0, a1)
1234 * which could result in missing packets and interrupts.
1235 * supposedly one bit per endpoint, controlling whether it
1236 * double buffers or not; ACM/AREN bits fit into the holes.
1237 * zero bits (like USIR0_IRx) disable double buffering.
1238 */
1239 UDC_RES1 = 0x00;
1240 UDC_RES2 = 0x00;
1241 }
1242
1da177e4
LT
1243 /* enable suspend/resume and reset irqs */
1244 udc_clear_mask_UDCCR(UDCCR_SRM | UDCCR_REM);
1245
1246 /* enable ep0 irqs */
1247 UICR0 &= ~UICR0_IM0;
1248
1249 /* if hardware supports it, pullup D+ and wait for reset */
91987693 1250 pullup_on();
1da177e4
LT
1251}
1252
1253
1254/* when a driver is successfully registered, it will receive
1255 * control requests including set_configuration(), which enables
1256 * non-control requests. then usb traffic follows until a
1257 * disconnect is reported. then a host may connect again, or
1258 * the driver might get unbound.
1259 */
0f91349b 1260static int pxa25x_start(struct usb_gadget_driver *driver,
b0fca50f 1261 int (*bind)(struct usb_gadget *))
1da177e4 1262{
7a857620 1263 struct pxa25x_udc *dev = the_controller;
1da177e4
LT
1264 int retval;
1265
1266 if (!driver
7177aed4 1267 || driver->max_speed < USB_SPEED_FULL
b0fca50f 1268 || !bind
1da177e4
LT
1269 || !driver->disconnect
1270 || !driver->setup)
1271 return -EINVAL;
1272 if (!dev)
1273 return -ENODEV;
1274 if (dev->driver)
1275 return -EBUSY;
1276
1277 /* first hook up the driver ... */
1278 dev->driver = driver;
1279 dev->gadget.dev.driver = &driver->driver;
1280 dev->pullup = 1;
1281
34ebcd28
DB
1282 retval = device_add (&dev->gadget.dev);
1283 if (retval) {
1284fail:
1285 dev->driver = NULL;
1286 dev->gadget.dev.driver = NULL;
1287 return retval;
1288 }
b0fca50f 1289 retval = bind(&dev->gadget);
1da177e4
LT
1290 if (retval) {
1291 DMSG("bind to driver %s --> error %d\n",
1292 driver->driver.name, retval);
1293 device_del (&dev->gadget.dev);
34ebcd28 1294 goto fail;
1da177e4 1295 }
1da177e4
LT
1296
1297 /* ... then enable host detection and ep0; and we're ready
1298 * for set_configuration as well as eventual disconnect.
1299 */
1300 DMSG("registered gadget driver '%s'\n", driver->driver.name);
ab26d20f
PZ
1301
1302 /* connect to bus through transceiver */
1303 if (dev->transceiver) {
6e13c650
HK
1304 retval = otg_set_peripheral(dev->transceiver->otg,
1305 &dev->gadget);
ab26d20f
PZ
1306 if (retval) {
1307 DMSG("can't bind to transceiver\n");
1308 if (driver->unbind)
1309 driver->unbind(&dev->gadget);
1310 goto bind_fail;
1311 }
1312 }
1313
64cc2dd9 1314 pullup(dev);
1da177e4
LT
1315 dump_state(dev);
1316 return 0;
ab26d20f
PZ
1317bind_fail:
1318 return retval;
1da177e4 1319}
1da177e4
LT
1320
1321static void
7a857620 1322stop_activity(struct pxa25x_udc *dev, struct usb_gadget_driver *driver)
1da177e4
LT
1323{
1324 int i;
1325
1326 /* don't disconnect drivers more than once */
1327 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1328 driver = NULL;
1329 dev->gadget.speed = USB_SPEED_UNKNOWN;
1330
1331 /* prevent new request submissions, kill any outstanding requests */
1332 for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
7a857620 1333 struct pxa25x_ep *ep = &dev->ep[i];
1da177e4
LT
1334
1335 ep->stopped = 1;
1336 nuke(ep, -ESHUTDOWN);
1337 }
1338 del_timer_sync(&dev->timer);
1339
1340 /* report disconnect; the driver is already quiesced */
1da177e4
LT
1341 if (driver)
1342 driver->disconnect(&dev->gadget);
1343
1344 /* re-init driver-visible data structures */
1345 udc_reinit(dev);
1346}
1347
0f91349b 1348static int pxa25x_stop(struct usb_gadget_driver *driver)
1da177e4 1349{
7a857620 1350 struct pxa25x_udc *dev = the_controller;
1da177e4
LT
1351
1352 if (!dev)
1353 return -ENODEV;
6bea476c 1354 if (!driver || driver != dev->driver || !driver->unbind)
1da177e4
LT
1355 return -EINVAL;
1356
1357 local_irq_disable();
64cc2dd9
DES
1358 dev->pullup = 0;
1359 pullup(dev);
1da177e4
LT
1360 stop_activity(dev, driver);
1361 local_irq_enable();
1362
ab26d20f 1363 if (dev->transceiver)
6e13c650 1364 (void) otg_set_peripheral(dev->transceiver->otg, NULL);
ab26d20f 1365
1da177e4 1366 driver->unbind(&dev->gadget);
eb0be47d 1367 dev->gadget.dev.driver = NULL;
1da177e4
LT
1368 dev->driver = NULL;
1369
1370 device_del (&dev->gadget.dev);
1da177e4
LT
1371
1372 DMSG("unregistered gadget driver '%s'\n", driver->driver.name);
1373 dump_state(dev);
1374 return 0;
1375}
1da177e4
LT
1376
1377/*-------------------------------------------------------------------------*/
1378
1379#ifdef CONFIG_ARCH_LUBBOCK
1380
1381/* Lubbock has separate connect and disconnect irqs. More typical designs
1382 * use one GPIO as the VBUS IRQ, and another to control the D+ pullup.
1383 */
1384
1385static irqreturn_t
7d12e780 1386lubbock_vbus_irq(int irq, void *_dev)
1da177e4 1387{
7a857620 1388 struct pxa25x_udc *dev = _dev;
1da177e4
LT
1389 int vbus;
1390
1391 dev->stats.irqs++;
1da177e4
LT
1392 switch (irq) {
1393 case LUBBOCK_USB_IRQ:
1da177e4
LT
1394 vbus = 1;
1395 disable_irq(LUBBOCK_USB_IRQ);
1396 enable_irq(LUBBOCK_USB_DISC_IRQ);
1397 break;
1398 case LUBBOCK_USB_DISC_IRQ:
1da177e4
LT
1399 vbus = 0;
1400 disable_irq(LUBBOCK_USB_DISC_IRQ);
1401 enable_irq(LUBBOCK_USB_IRQ);
1402 break;
1403 default:
1404 return IRQ_NONE;
1405 }
1406
7a857620 1407 pxa25x_udc_vbus_session(&dev->gadget, vbus);
1da177e4
LT
1408 return IRQ_HANDLED;
1409}
1410
1411#endif
1412
1413
1414/*-------------------------------------------------------------------------*/
1415
7a857620 1416static inline void clear_ep_state (struct pxa25x_udc *dev)
1da177e4
LT
1417{
1418 unsigned i;
1419
1420 /* hardware SET_{CONFIGURATION,INTERFACE} automagic resets endpoint
1421 * fifos, and pending transactions mustn't be continued in any case.
1422 */
1423 for (i = 1; i < PXA_UDC_NUM_ENDPOINTS; i++)
1424 nuke(&dev->ep[i], -ECONNABORTED);
1425}
1426
1427static void udc_watchdog(unsigned long _dev)
1428{
7a857620 1429 struct pxa25x_udc *dev = (void *)_dev;
1da177e4
LT
1430
1431 local_irq_disable();
1432 if (dev->ep0state == EP0_STALL
1433 && (UDCCS0 & UDCCS0_FST) == 0
1434 && (UDCCS0 & UDCCS0_SST) == 0) {
1435 UDCCS0 = UDCCS0_FST|UDCCS0_FTF;
1436 DBG(DBG_VERBOSE, "ep0 re-stall\n");
1437 start_watchdog(dev);
1438 }
1439 local_irq_enable();
1440}
1441
7a857620 1442static void handle_ep0 (struct pxa25x_udc *dev)
1da177e4
LT
1443{
1444 u32 udccs0 = UDCCS0;
7a857620
PZ
1445 struct pxa25x_ep *ep = &dev->ep [0];
1446 struct pxa25x_request *req;
1da177e4
LT
1447 union {
1448 struct usb_ctrlrequest r;
1449 u8 raw [8];
1450 u32 word [2];
1451 } u;
1452
1453 if (list_empty(&ep->queue))
1454 req = NULL;
1455 else
7a857620 1456 req = list_entry(ep->queue.next, struct pxa25x_request, queue);
1da177e4
LT
1457
1458 /* clear stall status */
1459 if (udccs0 & UDCCS0_SST) {
1460 nuke(ep, -EPIPE);
1461 UDCCS0 = UDCCS0_SST;
1462 del_timer(&dev->timer);
1463 ep0_idle(dev);
1464 }
1465
1466 /* previous request unfinished? non-error iff back-to-back ... */
1467 if ((udccs0 & UDCCS0_SA) != 0 && dev->ep0state != EP0_IDLE) {
1468 nuke(ep, 0);
1469 del_timer(&dev->timer);
1470 ep0_idle(dev);
1471 }
1472
1473 switch (dev->ep0state) {
1474 case EP0_IDLE:
1475 /* late-breaking status? */
1476 udccs0 = UDCCS0;
1477
1478 /* start control request? */
1479 if (likely((udccs0 & (UDCCS0_OPR|UDCCS0_SA|UDCCS0_RNE))
1480 == (UDCCS0_OPR|UDCCS0_SA|UDCCS0_RNE))) {
1481 int i;
1482
1483 nuke (ep, -EPROTO);
1484
1485 /* read SETUP packet */
1486 for (i = 0; i < 8; i++) {
1487 if (unlikely(!(UDCCS0 & UDCCS0_RNE))) {
1488bad_setup:
1489 DMSG("SETUP %d!\n", i);
1490 goto stall;
1491 }
1492 u.raw [i] = (u8) UDDR0;
1493 }
1494 if (unlikely((UDCCS0 & UDCCS0_RNE) != 0))
1495 goto bad_setup;
1496
1497got_setup:
1498 DBG(DBG_VERBOSE, "SETUP %02x.%02x v%04x i%04x l%04x\n",
1499 u.r.bRequestType, u.r.bRequest,
1500 le16_to_cpu(u.r.wValue),
1501 le16_to_cpu(u.r.wIndex),
1502 le16_to_cpu(u.r.wLength));
1503
1504 /* cope with automagic for some standard requests. */
1505 dev->req_std = (u.r.bRequestType & USB_TYPE_MASK)
1506 == USB_TYPE_STANDARD;
1507 dev->req_config = 0;
1508 dev->req_pending = 1;
1509 switch (u.r.bRequest) {
1510 /* hardware restricts gadget drivers here! */
1511 case USB_REQ_SET_CONFIGURATION:
1512 if (u.r.bRequestType == USB_RECIP_DEVICE) {
1513 /* reflect hardware's automagic
1514 * up to the gadget driver.
1515 */
1516config_change:
1517 dev->req_config = 1;
1518 clear_ep_state(dev);
1519 /* if !has_cfr, there's no synch
1520 * else use AREN (later) not SA|OPR
1521 * USIR0_IR0 acts edge sensitive
1522 */
1523 }
1524 break;
1525 /* ... and here, even more ... */
1526 case USB_REQ_SET_INTERFACE:
1527 if (u.r.bRequestType == USB_RECIP_INTERFACE) {
1528 /* udc hardware is broken by design:
1529 * - altsetting may only be zero;
1530 * - hw resets all interfaces' eps;
1531 * - ep reset doesn't include halt(?).
1532 */
1533 DMSG("broken set_interface (%d/%d)\n",
1534 le16_to_cpu(u.r.wIndex),
1535 le16_to_cpu(u.r.wValue));
1536 goto config_change;
1537 }
1538 break;
1539 /* hardware was supposed to hide this */
1540 case USB_REQ_SET_ADDRESS:
1541 if (u.r.bRequestType == USB_RECIP_DEVICE) {
1542 ep0start(dev, 0, "address");
1543 return;
1544 }
1545 break;
1546 }
1547
1548 if (u.r.bRequestType & USB_DIR_IN)
1549 dev->ep0state = EP0_IN_DATA_PHASE;
1550 else
1551 dev->ep0state = EP0_OUT_DATA_PHASE;
1552
1553 i = dev->driver->setup(&dev->gadget, &u.r);
1554 if (i < 0) {
1555 /* hardware automagic preventing STALL... */
1556 if (dev->req_config) {
1557 /* hardware sometimes neglects to tell
1558 * tell us about config change events,
1559 * so later ones may fail...
1560 */
b6c63937 1561 WARNING("config change %02x fail %d?\n",
1da177e4
LT
1562 u.r.bRequest, i);
1563 return;
1564 /* TODO experiment: if has_cfr,
1565 * hardware didn't ACK; maybe we
1566 * could actually STALL!
1567 */
1568 }
1569 DBG(DBG_VERBOSE, "protocol STALL, "
1570 "%02x err %d\n", UDCCS0, i);
1571stall:
1572 /* the watchdog timer helps deal with cases
1573 * where udc seems to clear FST wrongly, and
1574 * then NAKs instead of STALLing.
1575 */
1576 ep0start(dev, UDCCS0_FST|UDCCS0_FTF, "stall");
1577 start_watchdog(dev);
1578 dev->ep0state = EP0_STALL;
1579
1580 /* deferred i/o == no response yet */
1581 } else if (dev->req_pending) {
1582 if (likely(dev->ep0state == EP0_IN_DATA_PHASE
1583 || dev->req_std || u.r.wLength))
1584 ep0start(dev, 0, "defer");
1585 else
1586 ep0start(dev, UDCCS0_IPR, "defer/IPR");
1587 }
1588
1589 /* expect at least one data or status stage irq */
1590 return;
1591
1592 } else if (likely((udccs0 & (UDCCS0_OPR|UDCCS0_SA))
1593 == (UDCCS0_OPR|UDCCS0_SA))) {
1594 unsigned i;
1595
1596 /* pxa210/250 erratum 131 for B0/B1 says RNE lies.
1597 * still observed on a pxa255 a0.
1598 */
1599 DBG(DBG_VERBOSE, "e131\n");
1600 nuke(ep, -EPROTO);
1601
1602 /* read SETUP data, but don't trust it too much */
1603 for (i = 0; i < 8; i++)
1604 u.raw [i] = (u8) UDDR0;
1605 if ((u.r.bRequestType & USB_RECIP_MASK)
1606 > USB_RECIP_OTHER)
1607 goto stall;
1608 if (u.word [0] == 0 && u.word [1] == 0)
1609 goto stall;
1610 goto got_setup;
1611 } else {
1612 /* some random early IRQ:
1613 * - we acked FST
1614 * - IPR cleared
1615 * - OPR got set, without SA (likely status stage)
1616 */
1617 UDCCS0 = udccs0 & (UDCCS0_SA|UDCCS0_OPR);
1618 }
1619 break;
1620 case EP0_IN_DATA_PHASE: /* GET_DESCRIPTOR etc */
1621 if (udccs0 & UDCCS0_OPR) {
1622 UDCCS0 = UDCCS0_OPR|UDCCS0_FTF;
1623 DBG(DBG_VERBOSE, "ep0in premature status\n");
1624 if (req)
1625 done(ep, req, 0);
1626 ep0_idle(dev);
1627 } else /* irq was IPR clearing */ {
1628 if (req) {
1629 /* this IN packet might finish the request */
1630 (void) write_ep0_fifo(ep, req);
1631 } /* else IN token before response was written */
1632 }
1633 break;
1634 case EP0_OUT_DATA_PHASE: /* SET_DESCRIPTOR etc */
1635 if (udccs0 & UDCCS0_OPR) {
1636 if (req) {
1637 /* this OUT packet might finish the request */
1638 if (read_ep0_fifo(ep, req))
1639 done(ep, req, 0);
1640 /* else more OUT packets expected */
1641 } /* else OUT token before read was issued */
1642 } else /* irq was IPR clearing */ {
1643 DBG(DBG_VERBOSE, "ep0out premature status\n");
1644 if (req)
1645 done(ep, req, 0);
1646 ep0_idle(dev);
1647 }
1648 break;
1649 case EP0_END_XFER:
1650 if (req)
1651 done(ep, req, 0);
1652 /* ack control-IN status (maybe in-zlp was skipped)
1653 * also appears after some config change events.
1654 */
1655 if (udccs0 & UDCCS0_OPR)
1656 UDCCS0 = UDCCS0_OPR;
1657 ep0_idle(dev);
1658 break;
1659 case EP0_STALL:
1660 UDCCS0 = UDCCS0_FST;
1661 break;
1662 }
1663 USIR0 = USIR0_IR0;
1664}
1665
7a857620 1666static void handle_ep(struct pxa25x_ep *ep)
1da177e4 1667{
7a857620 1668 struct pxa25x_request *req;
1da177e4
LT
1669 int is_in = ep->bEndpointAddress & USB_DIR_IN;
1670 int completed;
1671 u32 udccs, tmp;
1672
1673 do {
1674 completed = 0;
1675 if (likely (!list_empty(&ep->queue)))
1676 req = list_entry(ep->queue.next,
7a857620 1677 struct pxa25x_request, queue);
1da177e4
LT
1678 else
1679 req = NULL;
1680
1681 // TODO check FST handling
1682
1683 udccs = *ep->reg_udccs;
1684 if (unlikely(is_in)) { /* irq from TPC, SST, or (ISO) TUR */
1685 tmp = UDCCS_BI_TUR;
1686 if (likely(ep->bmAttributes == USB_ENDPOINT_XFER_BULK))
1687 tmp |= UDCCS_BI_SST;
1688 tmp &= udccs;
1689 if (likely (tmp))
1690 *ep->reg_udccs = tmp;
1691 if (req && likely ((udccs & UDCCS_BI_TFS) != 0))
1692 completed = write_fifo(ep, req);
1693
1694 } else { /* irq from RPC (or for ISO, ROF) */
1695 if (likely(ep->bmAttributes == USB_ENDPOINT_XFER_BULK))
1696 tmp = UDCCS_BO_SST | UDCCS_BO_DME;
1697 else
1698 tmp = UDCCS_IO_ROF | UDCCS_IO_DME;
1699 tmp &= udccs;
1700 if (likely(tmp))
1701 *ep->reg_udccs = tmp;
1702
1703 /* fifos can hold packets, ready for reading... */
1704 if (likely(req)) {
1da177e4
LT
1705 completed = read_fifo(ep, req);
1706 } else
1707 pio_irq_disable (ep->bEndpointAddress);
1708 }
1709 ep->pio_irqs++;
1710 } while (completed);
1711}
1712
1713/*
7a857620 1714 * pxa25x_udc_irq - interrupt handler
1da177e4
LT
1715 *
1716 * avoid delays in ep0 processing. the control handshaking isn't always
1717 * under software control (pxa250c0 and the pxa255 are better), and delays
1718 * could cause usb protocol errors.
1719 */
1720static irqreturn_t
7a857620 1721pxa25x_udc_irq(int irq, void *_dev)
1da177e4 1722{
7a857620 1723 struct pxa25x_udc *dev = _dev;
1da177e4
LT
1724 int handled;
1725
1726 dev->stats.irqs++;
1da177e4
LT
1727 do {
1728 u32 udccr = UDCCR;
1729
1730 handled = 0;
1731
1732 /* SUSpend Interrupt Request */
1733 if (unlikely(udccr & UDCCR_SUSIR)) {
1734 udc_ack_int_UDCCR(UDCCR_SUSIR);
1735 handled = 1;
a8ecc860 1736 DBG(DBG_VERBOSE, "USB suspend\n");
1da177e4 1737
a8ecc860 1738 if (dev->gadget.speed != USB_SPEED_UNKNOWN
1da177e4
LT
1739 && dev->driver
1740 && dev->driver->suspend)
1741 dev->driver->suspend(&dev->gadget);
1742 ep0_idle (dev);
1743 }
1744
1745 /* RESume Interrupt Request */
1746 if (unlikely(udccr & UDCCR_RESIR)) {
1747 udc_ack_int_UDCCR(UDCCR_RESIR);
1748 handled = 1;
1749 DBG(DBG_VERBOSE, "USB resume\n");
1750
1751 if (dev->gadget.speed != USB_SPEED_UNKNOWN
1752 && dev->driver
a8ecc860 1753 && dev->driver->resume)
1da177e4
LT
1754 dev->driver->resume(&dev->gadget);
1755 }
1756
1757 /* ReSeT Interrupt Request - USB reset */
1758 if (unlikely(udccr & UDCCR_RSTIR)) {
1759 udc_ack_int_UDCCR(UDCCR_RSTIR);
1760 handled = 1;
1761
1762 if ((UDCCR & UDCCR_UDA) == 0) {
1763 DBG(DBG_VERBOSE, "USB reset start\n");
1764
1765 /* reset driver and endpoints,
1766 * in case that's not yet done
1767 */
1768 stop_activity (dev, dev->driver);
1769
1770 } else {
1771 DBG(DBG_VERBOSE, "USB reset end\n");
1772 dev->gadget.speed = USB_SPEED_FULL;
1da177e4
LT
1773 memset(&dev->stats, 0, sizeof dev->stats);
1774 /* driver and endpoints are still reset */
1775 }
1776
1777 } else {
1778 u32 usir0 = USIR0 & ~UICR0;
1779 u32 usir1 = USIR1 & ~UICR1;
1780 int i;
1781
1782 if (unlikely (!usir0 && !usir1))
1783 continue;
1784
1785 DBG(DBG_VERY_NOISY, "irq %02x.%02x\n", usir1, usir0);
1786
1787 /* control traffic */
1788 if (usir0 & USIR0_IR0) {
1789 dev->ep[0].pio_irqs++;
1790 handle_ep0(dev);
1791 handled = 1;
1792 }
1793
1794 /* endpoint data transfers */
1795 for (i = 0; i < 8; i++) {
1796 u32 tmp = 1 << i;
1797
1798 if (i && (usir0 & tmp)) {
1799 handle_ep(&dev->ep[i]);
1800 USIR0 |= tmp;
1801 handled = 1;
1802 }
6d84599b 1803#ifndef CONFIG_USB_PXA25X_SMALL
1da177e4
LT
1804 if (usir1 & tmp) {
1805 handle_ep(&dev->ep[i+8]);
1806 USIR1 |= tmp;
1807 handled = 1;
1808 }
6d84599b 1809#endif
1da177e4
LT
1810 }
1811 }
1812
1813 /* we could also ask for 1 msec SOF (SIR) interrupts */
1814
1815 } while (handled);
1816 return IRQ_HANDLED;
1817}
1818
1819/*-------------------------------------------------------------------------*/
1820
1821static void nop_release (struct device *dev)
1822{
7071a3ce 1823 DMSG("%s %s\n", __func__, dev_name(dev));
1da177e4
LT
1824}
1825
1826/* this uses load-time allocation and initialization (instead of
1827 * doing it at run-time) to save code, eliminate fault paths, and
1828 * be more obviously correct.
1829 */
7a857620 1830static struct pxa25x_udc memory = {
1da177e4 1831 .gadget = {
7a857620 1832 .ops = &pxa25x_udc_ops,
1da177e4
LT
1833 .ep0 = &memory.ep[0].ep,
1834 .name = driver_name,
1835 .dev = {
c682b170 1836 .init_name = "gadget",
1da177e4
LT
1837 .release = nop_release,
1838 },
1839 },
1840
1841 /* control endpoint */
1842 .ep[0] = {
1843 .ep = {
1844 .name = ep0name,
7a857620 1845 .ops = &pxa25x_ep_ops,
1da177e4
LT
1846 .maxpacket = EP0_FIFO_SIZE,
1847 },
1848 .dev = &memory,
1849 .reg_udccs = &UDCCS0,
1850 .reg_uddr = &UDDR0,
1851 },
1852
1853 /* first group of endpoints */
1854 .ep[1] = {
1855 .ep = {
1856 .name = "ep1in-bulk",
7a857620 1857 .ops = &pxa25x_ep_ops,
1da177e4
LT
1858 .maxpacket = BULK_FIFO_SIZE,
1859 },
1860 .dev = &memory,
1861 .fifo_size = BULK_FIFO_SIZE,
1862 .bEndpointAddress = USB_DIR_IN | 1,
1863 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1864 .reg_udccs = &UDCCS1,
1865 .reg_uddr = &UDDR1,
1da177e4
LT
1866 },
1867 .ep[2] = {
1868 .ep = {
1869 .name = "ep2out-bulk",
7a857620 1870 .ops = &pxa25x_ep_ops,
1da177e4
LT
1871 .maxpacket = BULK_FIFO_SIZE,
1872 },
1873 .dev = &memory,
1874 .fifo_size = BULK_FIFO_SIZE,
1875 .bEndpointAddress = 2,
1876 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1877 .reg_udccs = &UDCCS2,
1878 .reg_ubcr = &UBCR2,
1879 .reg_uddr = &UDDR2,
1da177e4 1880 },
7a857620 1881#ifndef CONFIG_USB_PXA25X_SMALL
1da177e4
LT
1882 .ep[3] = {
1883 .ep = {
1884 .name = "ep3in-iso",
7a857620 1885 .ops = &pxa25x_ep_ops,
1da177e4
LT
1886 .maxpacket = ISO_FIFO_SIZE,
1887 },
1888 .dev = &memory,
1889 .fifo_size = ISO_FIFO_SIZE,
1890 .bEndpointAddress = USB_DIR_IN | 3,
1891 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
1892 .reg_udccs = &UDCCS3,
1893 .reg_uddr = &UDDR3,
1da177e4
LT
1894 },
1895 .ep[4] = {
1896 .ep = {
1897 .name = "ep4out-iso",
7a857620 1898 .ops = &pxa25x_ep_ops,
1da177e4
LT
1899 .maxpacket = ISO_FIFO_SIZE,
1900 },
1901 .dev = &memory,
1902 .fifo_size = ISO_FIFO_SIZE,
1903 .bEndpointAddress = 4,
1904 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
1905 .reg_udccs = &UDCCS4,
1906 .reg_ubcr = &UBCR4,
1907 .reg_uddr = &UDDR4,
1da177e4
LT
1908 },
1909 .ep[5] = {
1910 .ep = {
1911 .name = "ep5in-int",
7a857620 1912 .ops = &pxa25x_ep_ops,
1da177e4
LT
1913 .maxpacket = INT_FIFO_SIZE,
1914 },
1915 .dev = &memory,
1916 .fifo_size = INT_FIFO_SIZE,
1917 .bEndpointAddress = USB_DIR_IN | 5,
1918 .bmAttributes = USB_ENDPOINT_XFER_INT,
1919 .reg_udccs = &UDCCS5,
1920 .reg_uddr = &UDDR5,
1921 },
1922
1923 /* second group of endpoints */
1924 .ep[6] = {
1925 .ep = {
1926 .name = "ep6in-bulk",
7a857620 1927 .ops = &pxa25x_ep_ops,
1da177e4
LT
1928 .maxpacket = BULK_FIFO_SIZE,
1929 },
1930 .dev = &memory,
1931 .fifo_size = BULK_FIFO_SIZE,
1932 .bEndpointAddress = USB_DIR_IN | 6,
1933 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1934 .reg_udccs = &UDCCS6,
1935 .reg_uddr = &UDDR6,
1da177e4
LT
1936 },
1937 .ep[7] = {
1938 .ep = {
1939 .name = "ep7out-bulk",
7a857620 1940 .ops = &pxa25x_ep_ops,
1da177e4
LT
1941 .maxpacket = BULK_FIFO_SIZE,
1942 },
1943 .dev = &memory,
1944 .fifo_size = BULK_FIFO_SIZE,
1945 .bEndpointAddress = 7,
1946 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1947 .reg_udccs = &UDCCS7,
1948 .reg_ubcr = &UBCR7,
1949 .reg_uddr = &UDDR7,
1da177e4
LT
1950 },
1951 .ep[8] = {
1952 .ep = {
1953 .name = "ep8in-iso",
7a857620 1954 .ops = &pxa25x_ep_ops,
1da177e4
LT
1955 .maxpacket = ISO_FIFO_SIZE,
1956 },
1957 .dev = &memory,
1958 .fifo_size = ISO_FIFO_SIZE,
1959 .bEndpointAddress = USB_DIR_IN | 8,
1960 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
1961 .reg_udccs = &UDCCS8,
1962 .reg_uddr = &UDDR8,
1da177e4
LT
1963 },
1964 .ep[9] = {
1965 .ep = {
1966 .name = "ep9out-iso",
7a857620 1967 .ops = &pxa25x_ep_ops,
1da177e4
LT
1968 .maxpacket = ISO_FIFO_SIZE,
1969 },
1970 .dev = &memory,
1971 .fifo_size = ISO_FIFO_SIZE,
1972 .bEndpointAddress = 9,
1973 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
1974 .reg_udccs = &UDCCS9,
1975 .reg_ubcr = &UBCR9,
1976 .reg_uddr = &UDDR9,
1da177e4
LT
1977 },
1978 .ep[10] = {
1979 .ep = {
1980 .name = "ep10in-int",
7a857620 1981 .ops = &pxa25x_ep_ops,
1da177e4
LT
1982 .maxpacket = INT_FIFO_SIZE,
1983 },
1984 .dev = &memory,
1985 .fifo_size = INT_FIFO_SIZE,
1986 .bEndpointAddress = USB_DIR_IN | 10,
1987 .bmAttributes = USB_ENDPOINT_XFER_INT,
1988 .reg_udccs = &UDCCS10,
1989 .reg_uddr = &UDDR10,
1990 },
1991
1992 /* third group of endpoints */
1993 .ep[11] = {
1994 .ep = {
1995 .name = "ep11in-bulk",
7a857620 1996 .ops = &pxa25x_ep_ops,
1da177e4
LT
1997 .maxpacket = BULK_FIFO_SIZE,
1998 },
1999 .dev = &memory,
2000 .fifo_size = BULK_FIFO_SIZE,
2001 .bEndpointAddress = USB_DIR_IN | 11,
2002 .bmAttributes = USB_ENDPOINT_XFER_BULK,
2003 .reg_udccs = &UDCCS11,
2004 .reg_uddr = &UDDR11,
1da177e4
LT
2005 },
2006 .ep[12] = {
2007 .ep = {
2008 .name = "ep12out-bulk",
7a857620 2009 .ops = &pxa25x_ep_ops,
1da177e4
LT
2010 .maxpacket = BULK_FIFO_SIZE,
2011 },
2012 .dev = &memory,
2013 .fifo_size = BULK_FIFO_SIZE,
2014 .bEndpointAddress = 12,
2015 .bmAttributes = USB_ENDPOINT_XFER_BULK,
2016 .reg_udccs = &UDCCS12,
2017 .reg_ubcr = &UBCR12,
2018 .reg_uddr = &UDDR12,
1da177e4
LT
2019 },
2020 .ep[13] = {
2021 .ep = {
2022 .name = "ep13in-iso",
7a857620 2023 .ops = &pxa25x_ep_ops,
1da177e4
LT
2024 .maxpacket = ISO_FIFO_SIZE,
2025 },
2026 .dev = &memory,
2027 .fifo_size = ISO_FIFO_SIZE,
2028 .bEndpointAddress = USB_DIR_IN | 13,
2029 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
2030 .reg_udccs = &UDCCS13,
2031 .reg_uddr = &UDDR13,
1da177e4
LT
2032 },
2033 .ep[14] = {
2034 .ep = {
2035 .name = "ep14out-iso",
7a857620 2036 .ops = &pxa25x_ep_ops,
1da177e4
LT
2037 .maxpacket = ISO_FIFO_SIZE,
2038 },
2039 .dev = &memory,
2040 .fifo_size = ISO_FIFO_SIZE,
2041 .bEndpointAddress = 14,
2042 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
2043 .reg_udccs = &UDCCS14,
2044 .reg_ubcr = &UBCR14,
2045 .reg_uddr = &UDDR14,
1da177e4
LT
2046 },
2047 .ep[15] = {
2048 .ep = {
2049 .name = "ep15in-int",
7a857620 2050 .ops = &pxa25x_ep_ops,
1da177e4
LT
2051 .maxpacket = INT_FIFO_SIZE,
2052 },
2053 .dev = &memory,
2054 .fifo_size = INT_FIFO_SIZE,
2055 .bEndpointAddress = USB_DIR_IN | 15,
2056 .bmAttributes = USB_ENDPOINT_XFER_INT,
2057 .reg_udccs = &UDCCS15,
2058 .reg_uddr = &UDDR15,
2059 },
7a857620 2060#endif /* !CONFIG_USB_PXA25X_SMALL */
1da177e4
LT
2061};
2062
2063#define CP15R0_VENDOR_MASK 0xffffe000
2064
2065#if defined(CONFIG_ARCH_PXA)
2066#define CP15R0_XSCALE_VALUE 0x69052000 /* intel/arm/xscale */
2067
2068#elif defined(CONFIG_ARCH_IXP4XX)
2069#define CP15R0_XSCALE_VALUE 0x69054000 /* intel/arm/ixp4xx */
2070
2071#endif
2072
2073#define CP15R0_PROD_MASK 0x000003f0
2074#define PXA25x 0x00000100 /* and PXA26x */
2075#define PXA210 0x00000120
2076
2077#define CP15R0_REV_MASK 0x0000000f
2078
2079#define CP15R0_PRODREV_MASK (CP15R0_PROD_MASK | CP15R0_REV_MASK)
2080
2081#define PXA255_A0 0x00000106 /* or PXA260_B1 */
2082#define PXA250_C0 0x00000105 /* or PXA26x_B0 */
2083#define PXA250_B2 0x00000104
2084#define PXA250_B1 0x00000103 /* or PXA260_A0 */
2085#define PXA250_B0 0x00000102
2086#define PXA250_A1 0x00000101
2087#define PXA250_A0 0x00000100
2088
2089#define PXA210_C0 0x00000125
2090#define PXA210_B2 0x00000124
2091#define PXA210_B1 0x00000123
2092#define PXA210_B0 0x00000122
2093#define IXP425_A0 0x000001c1
827982c5 2094#define IXP425_B0 0x000001f1
043ea18b 2095#define IXP465_AD 0x00000200
1da177e4
LT
2096
2097/*
34ebcd28 2098 * probe - binds to the platform device
1da177e4 2099 */
7a857620 2100static int __init pxa25x_udc_probe(struct platform_device *pdev)
1da177e4 2101{
7a857620 2102 struct pxa25x_udc *dev = &memory;
a8ecc860 2103 int retval, irq;
1da177e4
LT
2104 u32 chiprev;
2105
2106 /* insist on Intel/ARM/XScale */
2107 asm("mrc%? p15, 0, %0, c0, c0" : "=r" (chiprev));
2108 if ((chiprev & CP15R0_VENDOR_MASK) != CP15R0_XSCALE_VALUE) {
00274921 2109 pr_err("%s: not XScale!\n", driver_name);
1da177e4
LT
2110 return -ENODEV;
2111 }
2112
2113 /* trigger chiprev-specific logic */
2114 switch (chiprev & CP15R0_PRODREV_MASK) {
2115#if defined(CONFIG_ARCH_PXA)
2116 case PXA255_A0:
2117 dev->has_cfr = 1;
2118 break;
2119 case PXA250_A0:
2120 case PXA250_A1:
2121 /* A0/A1 "not released"; ep 13, 15 unusable */
2122 /* fall through */
2123 case PXA250_B2: case PXA210_B2:
2124 case PXA250_B1: case PXA210_B1:
2125 case PXA250_B0: case PXA210_B0:
ad8c623f 2126 /* OUT-DMA is broken ... */
1da177e4
LT
2127 /* fall through */
2128 case PXA250_C0: case PXA210_C0:
2129 break;
2130#elif defined(CONFIG_ARCH_IXP4XX)
2131 case IXP425_A0:
827982c5 2132 case IXP425_B0:
043ea18b
MS
2133 case IXP465_AD:
2134 dev->has_cfr = 1;
1da177e4
LT
2135 break;
2136#endif
2137 default:
00274921 2138 pr_err("%s: unrecognized processor: %08x\n",
1da177e4
LT
2139 driver_name, chiprev);
2140 /* iop3xx, ixp4xx, ... */
2141 return -ENODEV;
2142 }
2143
34ebcd28
DB
2144 irq = platform_get_irq(pdev, 0);
2145 if (irq < 0)
2146 return -ENODEV;
2147
e0d8b13a 2148 dev->clk = clk_get(&pdev->dev, NULL);
6549e6c9
RK
2149 if (IS_ERR(dev->clk)) {
2150 retval = PTR_ERR(dev->clk);
2151 goto err_clk;
2152 }
6549e6c9 2153
ad8c623f 2154 pr_debug("%s: IRQ %d%s%s\n", driver_name, irq,
1da177e4 2155 dev->has_cfr ? "" : " (!cfr)",
ad8c623f 2156 SIZE_STR "(pio)"
1da177e4
LT
2157 );
2158
1da177e4 2159 /* other non-static parts of init */
3ae5eaec
RK
2160 dev->dev = &pdev->dev;
2161 dev->mach = pdev->dev.platform_data;
9068a4c6 2162
b96d3b08 2163 dev->transceiver = usb_get_transceiver();
ab26d20f 2164
56a075dc 2165 if (gpio_is_valid(dev->mach->gpio_pullup)) {
9068a4c6 2166 if ((retval = gpio_request(dev->mach->gpio_pullup,
7a857620 2167 "pca25x_udc GPIO PULLUP"))) {
9068a4c6
MS
2168 dev_dbg(&pdev->dev,
2169 "can't get pullup gpio %d, err: %d\n",
2170 dev->mach->gpio_pullup, retval);
6549e6c9 2171 goto err_gpio_pullup;
9068a4c6
MS
2172 }
2173 gpio_direction_output(dev->mach->gpio_pullup, 0);
2174 }
1da177e4
LT
2175
2176 init_timer(&dev->timer);
2177 dev->timer.function = udc_watchdog;
2178 dev->timer.data = (unsigned long) dev;
2179
2180 device_initialize(&dev->gadget.dev);
3ae5eaec
RK
2181 dev->gadget.dev.parent = &pdev->dev;
2182 dev->gadget.dev.dma_mask = pdev->dev.dma_mask;
1da177e4
LT
2183
2184 the_controller = dev;
3ae5eaec 2185 platform_set_drvdata(pdev, dev);
1da177e4
LT
2186
2187 udc_disable(dev);
2188 udc_reinit(dev);
2189
a8ecc860 2190 dev->vbus = 0;
1da177e4
LT
2191
2192 /* irq setup after old hardware state is cleaned up */
7a857620 2193 retval = request_irq(irq, pxa25x_udc_irq,
b5dd18d8 2194 0, driver_name, dev);
1da177e4 2195 if (retval != 0) {
00274921 2196 pr_err("%s: can't get irq %d, err %d\n",
34ebcd28 2197 driver_name, irq, retval);
6549e6c9 2198 goto err_irq1;
1da177e4
LT
2199 }
2200 dev->got_irq = 1;
2201
2202#ifdef CONFIG_ARCH_LUBBOCK
2203 if (machine_is_lubbock()) {
2204 retval = request_irq(LUBBOCK_USB_DISC_IRQ,
2205 lubbock_vbus_irq,
b5dd18d8 2206 IRQF_SAMPLE_RANDOM,
1da177e4
LT
2207 driver_name, dev);
2208 if (retval != 0) {
00274921 2209 pr_err("%s: can't get irq %i, err %d\n",
1da177e4 2210 driver_name, LUBBOCK_USB_DISC_IRQ, retval);
6549e6c9 2211 goto err_irq_lub;
1da177e4
LT
2212 }
2213 retval = request_irq(LUBBOCK_USB_IRQ,
2214 lubbock_vbus_irq,
b5dd18d8 2215 IRQF_SAMPLE_RANDOM,
1da177e4
LT
2216 driver_name, dev);
2217 if (retval != 0) {
00274921 2218 pr_err("%s: can't get irq %i, err %d\n",
1da177e4 2219 driver_name, LUBBOCK_USB_IRQ, retval);
1da177e4
LT
2220 goto lubbock_fail0;
2221 }
b2bbb20b 2222 } else
1da177e4 2223#endif
040fa1b9 2224 create_debug_files(dev);
1da177e4 2225
0f91349b
SAS
2226 retval = usb_add_gadget_udc(&pdev->dev, &dev->gadget);
2227 if (!retval)
2228 return retval;
6549e6c9 2229
0f91349b 2230 remove_debug_files(dev);
6549e6c9 2231#ifdef CONFIG_ARCH_LUBBOCK
a6207b17 2232lubbock_fail0:
6549e6c9
RK
2233 free_irq(LUBBOCK_USB_DISC_IRQ, dev);
2234 err_irq_lub:
6549e6c9 2235 free_irq(irq, dev);
a6207b17 2236#endif
6549e6c9 2237 err_irq1:
56a075dc 2238 if (gpio_is_valid(dev->mach->gpio_pullup))
6549e6c9
RK
2239 gpio_free(dev->mach->gpio_pullup);
2240 err_gpio_pullup:
ab26d20f 2241 if (dev->transceiver) {
b96d3b08 2242 usb_put_transceiver(dev->transceiver);
ab26d20f
PZ
2243 dev->transceiver = NULL;
2244 }
6549e6c9
RK
2245 clk_put(dev->clk);
2246 err_clk:
6549e6c9 2247 return retval;
1da177e4 2248}
91987693 2249
7a857620 2250static void pxa25x_udc_shutdown(struct platform_device *_dev)
91987693
DB
2251{
2252 pullup_off();
2253}
2254
7a857620 2255static int __exit pxa25x_udc_remove(struct platform_device *pdev)
1da177e4 2256{
7a857620 2257 struct pxa25x_udc *dev = platform_get_drvdata(pdev);
1da177e4 2258
0f91349b 2259 usb_del_gadget_udc(&dev->gadget);
6bea476c
DB
2260 if (dev->driver)
2261 return -EBUSY;
2262
64cc2dd9
DES
2263 dev->pullup = 0;
2264 pullup(dev);
2265
040fa1b9 2266 remove_debug_files(dev);
1da177e4
LT
2267
2268 if (dev->got_irq) {
34ebcd28 2269 free_irq(platform_get_irq(pdev, 0), dev);
1da177e4
LT
2270 dev->got_irq = 0;
2271 }
44df45a0 2272#ifdef CONFIG_ARCH_LUBBOCK
1da177e4
LT
2273 if (machine_is_lubbock()) {
2274 free_irq(LUBBOCK_USB_DISC_IRQ, dev);
2275 free_irq(LUBBOCK_USB_IRQ, dev);
2276 }
44df45a0 2277#endif
56a075dc 2278 if (gpio_is_valid(dev->mach->gpio_pullup))
9068a4c6
MS
2279 gpio_free(dev->mach->gpio_pullup);
2280
6549e6c9 2281 clk_put(dev->clk);
6549e6c9 2282
ab26d20f 2283 if (dev->transceiver) {
b96d3b08 2284 usb_put_transceiver(dev->transceiver);
ab26d20f
PZ
2285 dev->transceiver = NULL;
2286 }
2287
3ae5eaec 2288 platform_set_drvdata(pdev, NULL);
1da177e4
LT
2289 the_controller = NULL;
2290 return 0;
2291}
2292
2293/*-------------------------------------------------------------------------*/
2294
2295#ifdef CONFIG_PM
2296
2297/* USB suspend (controlled by the host) and system suspend (controlled
2298 * by the PXA) don't necessarily work well together. If USB is active,
2299 * the 48 MHz clock is required; so the system can't enter 33 MHz idle
2300 * mode, or any deeper PM saving state.
2301 *
2302 * For now, we punt and forcibly disconnect from the USB host when PXA
2303 * enters any suspend state. While we're disconnected, we always disable
34ebcd28 2304 * the 48MHz USB clock ... allowing PXA sleep and/or 33 MHz idle states.
1da177e4
LT
2305 * Boards without software pullup control shouldn't use those states.
2306 * VBUS IRQs should probably be ignored so that the PXA device just acts
2307 * "dead" to USB hosts until system resume.
2308 */
7a857620 2309static int pxa25x_udc_suspend(struct platform_device *dev, pm_message_t state)
1da177e4 2310{
7a857620 2311 struct pxa25x_udc *udc = platform_get_drvdata(dev);
64cc2dd9 2312 unsigned long flags;
1da177e4 2313
56a075dc 2314 if (!gpio_is_valid(udc->mach->gpio_pullup) && !udc->mach->udc_command)
b6c63937 2315 WARNING("USB host won't detect disconnect!\n");
64cc2dd9
DES
2316 udc->suspended = 1;
2317
2318 local_irq_save(flags);
2319 pullup(udc);
2320 local_irq_restore(flags);
9480e307 2321
1da177e4
LT
2322 return 0;
2323}
2324
7a857620 2325static int pxa25x_udc_resume(struct platform_device *dev)
1da177e4 2326{
7a857620 2327 struct pxa25x_udc *udc = platform_get_drvdata(dev);
64cc2dd9 2328 unsigned long flags;
1da177e4 2329
64cc2dd9
DES
2330 udc->suspended = 0;
2331 local_irq_save(flags);
2332 pullup(udc);
2333 local_irq_restore(flags);
9480e307 2334
1da177e4
LT
2335 return 0;
2336}
2337
2338#else
7a857620
PZ
2339#define pxa25x_udc_suspend NULL
2340#define pxa25x_udc_resume NULL
1da177e4
LT
2341#endif
2342
2343/*-------------------------------------------------------------------------*/
2344
3ae5eaec 2345static struct platform_driver udc_driver = {
7a857620
PZ
2346 .shutdown = pxa25x_udc_shutdown,
2347 .remove = __exit_p(pxa25x_udc_remove),
2348 .suspend = pxa25x_udc_suspend,
2349 .resume = pxa25x_udc_resume,
3ae5eaec
RK
2350 .driver = {
2351 .owner = THIS_MODULE,
7a857620 2352 .name = "pxa25x-udc",
3ae5eaec 2353 },
1da177e4
LT
2354};
2355
2356static int __init udc_init(void)
2357{
00274921 2358 pr_info("%s: version %s\n", driver_name, DRIVER_VERSION);
7a857620 2359 return platform_driver_probe(&udc_driver, pxa25x_udc_probe);
1da177e4
LT
2360}
2361module_init(udc_init);
2362
2363static void __exit udc_exit(void)
2364{
3ae5eaec 2365 platform_driver_unregister(&udc_driver);
1da177e4
LT
2366}
2367module_exit(udc_exit);
2368
2369MODULE_DESCRIPTION(DRIVER_DESC);
2370MODULE_AUTHOR("Frank Becker, Robert Schwebel, David Brownell");
2371MODULE_LICENSE("GPL");
7a857620 2372MODULE_ALIAS("platform:pxa25x-udc");