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