blob: 41ed69c96d8c512eab7cd50ea7a9c6d8f5cd25bf [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
David Brownell91987692005-05-07 13:20:19 -07002 * Intel PXA25x and IXP4xx on-chip full speed USB device controllers
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 */
15
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -080016/* #define VERBOSE_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Milan Svoboda9068a4c2007-06-30 06:25:35 -070018#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/module.h>
20#include <linux/kernel.h>
21#include <linux/ioport.h>
22#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/errno.h>
24#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/slab.h>
26#include <linux/init.h>
27#include <linux/timer.h>
28#include <linux/list.h>
29#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/mm.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010031#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/dma-mapping.h>
Nicolas Pitrec7a3bd12006-10-20 14:20:17 -070033#include <linux/irq.h>
Russell King6549e6c2007-08-20 10:33:35 +010034#include <linux/clk.h>
35#include <linux/err.h>
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -080036#include <linux/seq_file.h>
37#include <linux/debugfs.h>
Russell King284d1152008-04-20 17:32:16 +010038#include <linux/io.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -070039#include <linux/prefetch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#include <asm/byteorder.h>
42#include <asm/dma.h>
Milan Svoboda9068a4c2007-06-30 06:25:35 -070043#include <asm/gpio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/mach-types.h>
45#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
David Brownell5f848132006-12-16 15:34:53 -080047#include <linux/usb/ch9.h>
David Brownell9454a572007-10-04 18:05:17 -070048#include <linux/usb/gadget.h>
Philipp Zabelab26d202009-07-01 03:46:25 -070049#include <linux/usb/otg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Russell King284d1152008-04-20 17:32:16 +010051/*
52 * This driver is PXA25x only. Grab the right register definitions.
53 */
54#ifdef CONFIG_ARCH_PXA
Russell Kinga09e64f2008-08-05 16:14:15 +010055#include <mach/pxa25x-udc.h>
Russell King284d1152008-04-20 17:32:16 +010056#endif
57
Eric Miao0dc726b2009-12-27 23:01:25 +080058#ifdef CONFIG_ARCH_LUBBOCK
59#include <mach/lubbock.h>
60#endif
61
Milan Svoboda9068a4c2007-06-30 06:25:35 -070062#include <asm/mach/udc_pxa2xx.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64
65/*
David Brownell91987692005-05-07 13:20:19 -070066 * This driver handles the USB Device Controller (UDC) in Intel's PXA 25x
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 * series processors. The UDC for the IXP 4xx series is very similar.
68 * There are fifteen endpoints, in addition to ep0.
69 *
70 * Such controller drivers work with a gadget driver. The gadget driver
71 * returns descriptors, implements configuration and data protocols used
72 * by the host to interact with this device, and allocates endpoints to
73 * the different protocol interfaces. The controller driver virtualizes
74 * usb hardware so that the gadget drivers will be more portable.
David Brownell34ebcd22007-02-24 12:23:52 -080075 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 * This UDC hardware wants to implement a bit too much USB protocol, so
77 * it constrains the sorts of USB configuration change events that work.
78 * The errata for these chips are misleading; some "fixed" bugs from
79 * pxa250 a0/a1 b0/b1/b2 sure act like they're still there.
David Brownellad8c6232007-06-30 06:30:04 -070080 *
81 * Note that the UDC hardware supports DMA (except on IXP) but that's
82 * not used here. IN-DMA (to host) is simple enough, when the data is
83 * suitably aligned (16 bytes) ... the network stack doesn't do that,
84 * other software can. OUT-DMA is buggy in most chip versions, as well
85 * as poorly designed (data toggle not automatic). So this driver won't
86 * bother using DMA. (Mostly-working IN-DMA support was available in
87 * kernels before 2.6.23, but was never enabled or well tested.)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 */
89
David Brownellad8c6232007-06-30 06:30:04 -070090#define DRIVER_VERSION "30-June-2007"
David Brownell91987692005-05-07 13:20:19 -070091#define DRIVER_DESC "PXA 25x USB Device Controller driver"
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93
Philipp Zabel7a857622008-06-22 23:36:39 +010094static const char driver_name [] = "pxa25x_udc";
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96static const char ep0name [] = "ep0";
97
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099#ifdef CONFIG_ARCH_IXP4XX
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101/* cpu-specific register addresses are compiled in to this code */
102#ifdef CONFIG_ARCH_PXA
103#error "Can't configure both IXP and PXA"
104#endif
105
Dmitry Baryshkov64cc2dd2008-02-22 17:17:19 -0800106/* IXP doesn't yet support <linux/clk.h> */
107#define clk_get(dev,name) NULL
108#define clk_enable(clk) do { } while (0)
109#define clk_disable(clk) do { } while (0)
110#define clk_put(clk) do { } while (0)
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112#endif
113
Philipp Zabel7a857622008-06-22 23:36:39 +0100114#include "pxa25x_udc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116
Philipp Zabel7a857622008-06-22 23:36:39 +0100117#ifdef CONFIG_USB_PXA25X_SMALL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118#define SIZE_STR " (small)"
119#else
120#define SIZE_STR ""
121#endif
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123/* ---------------------------------------------------------------------------
David Brownell34ebcd22007-02-24 12:23:52 -0800124 * endpoint related parts of the api to the usb controller hardware,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 * used by gadget driver; and the inner talker-to-hardware core.
126 * ---------------------------------------------------------------------------
127 */
128
Philipp Zabel7a857622008-06-22 23:36:39 +0100129static void pxa25x_ep_fifo_flush (struct usb_ep *ep);
130static void nuke (struct pxa25x_ep *, int status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
David Brownellb2bbb202006-06-29 12:25:39 -0700132/* one GPIO should control a D+ pullup, so host sees this device (or not) */
133static void pullup_off(void)
134{
135 struct pxa2xx_udc_mach_info *mach = the_controller->mach;
Ian Molton8fb105f2008-06-25 22:34:51 +0100136 int off_level = mach->gpio_pullup_inverted;
David Brownellb2bbb202006-06-29 12:25:39 -0700137
Philipp Zabel56a075d2009-07-01 03:42:45 -0700138 if (gpio_is_valid(mach->gpio_pullup))
Ian Molton8fb105f2008-06-25 22:34:51 +0100139 gpio_set_value(mach->gpio_pullup, off_level);
David Brownellb2bbb202006-06-29 12:25:39 -0700140 else if (mach->udc_command)
141 mach->udc_command(PXA2XX_UDC_CMD_DISCONNECT);
142}
143
144static void pullup_on(void)
145{
146 struct pxa2xx_udc_mach_info *mach = the_controller->mach;
Ian Molton8fb105f2008-06-25 22:34:51 +0100147 int on_level = !mach->gpio_pullup_inverted;
David Brownellb2bbb202006-06-29 12:25:39 -0700148
Philipp Zabel56a075d2009-07-01 03:42:45 -0700149 if (gpio_is_valid(mach->gpio_pullup))
Ian Molton8fb105f2008-06-25 22:34:51 +0100150 gpio_set_value(mach->gpio_pullup, on_level);
David Brownellb2bbb202006-06-29 12:25:39 -0700151 else if (mach->udc_command)
152 mach->udc_command(PXA2XX_UDC_CMD_CONNECT);
153}
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155static void pio_irq_enable(int bEndpointAddress)
156{
157 bEndpointAddress &= 0xf;
158 if (bEndpointAddress < 8)
159 UICR0 &= ~(1 << bEndpointAddress);
160 else {
161 bEndpointAddress -= 8;
162 UICR1 &= ~(1 << bEndpointAddress);
163 }
164}
165
166static void pio_irq_disable(int bEndpointAddress)
167{
168 bEndpointAddress &= 0xf;
169 if (bEndpointAddress < 8)
170 UICR0 |= 1 << bEndpointAddress;
171 else {
172 bEndpointAddress -= 8;
173 UICR1 |= 1 << bEndpointAddress;
174 }
175}
176
177/* The UDCCR reg contains mask and interrupt status bits,
178 * so using '|=' isn't safe as it may ack an interrupt.
179 */
180#define UDCCR_MASK_BITS (UDCCR_REM | UDCCR_SRM | UDCCR_UDE)
181
182static inline void udc_set_mask_UDCCR(int mask)
183{
184 UDCCR = (UDCCR & UDCCR_MASK_BITS) | (mask & UDCCR_MASK_BITS);
185}
186
187static inline void udc_clear_mask_UDCCR(int mask)
188{
189 UDCCR = (UDCCR & UDCCR_MASK_BITS) & ~(mask & UDCCR_MASK_BITS);
190}
191
192static inline void udc_ack_int_UDCCR(int mask)
193{
194 /* udccr contains the bits we dont want to change */
195 __u32 udccr = UDCCR & UDCCR_MASK_BITS;
196
197 UDCCR = udccr | (mask & ~UDCCR_MASK_BITS);
198}
199
200/*
201 * endpoint enable/disable
202 *
Philipp Zabel7a857622008-06-22 23:36:39 +0100203 * we need to verify the descriptors used to enable endpoints. since pxa25x
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 * endpoint configurations are fixed, and are pretty much always enabled,
205 * there's not a lot to manage here.
206 *
Philipp Zabel7a857622008-06-22 23:36:39 +0100207 * because pxa25x can't selectively initialize bulk (or interrupt) endpoints,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 * (resetting endpoint halt and toggle), SET_INTERFACE is unusable except
209 * for a single interface (with only the default altsetting) and for gadget
210 * drivers that don't halt endpoints (not reset by set_interface). that also
211 * means that if you use ISO, you must violate the USB spec rule that all
212 * iso endpoints must be in non-default altsettings.
213 */
Philipp Zabel7a857622008-06-22 23:36:39 +0100214static int pxa25x_ep_enable (struct usb_ep *_ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 const struct usb_endpoint_descriptor *desc)
216{
Philipp Zabel7a857622008-06-22 23:36:39 +0100217 struct pxa25x_ep *ep;
218 struct pxa25x_udc *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Philipp Zabel7a857622008-06-22 23:36:39 +0100220 ep = container_of (_ep, struct pxa25x_ep, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 if (!_ep || !desc || ep->desc || _ep->name == ep0name
222 || desc->bDescriptorType != USB_DT_ENDPOINT
223 || ep->bEndpointAddress != desc->bEndpointAddress
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700224 || ep->fifo_size < usb_endpoint_maxp (desc)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800225 DMSG("%s, bad ep or descriptor\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 return -EINVAL;
227 }
228
229 /* xfer types must match, except that interrupt ~= bulk */
230 if (ep->bmAttributes != desc->bmAttributes
231 && ep->bmAttributes != USB_ENDPOINT_XFER_BULK
232 && desc->bmAttributes != USB_ENDPOINT_XFER_INT) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800233 DMSG("%s, %s type mismatch\n", __func__, _ep->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 return -EINVAL;
235 }
236
237 /* hardware _could_ do smaller, but driver doesn't */
238 if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700239 && usb_endpoint_maxp (desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 != BULK_FIFO_SIZE)
241 || !desc->wMaxPacketSize) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800242 DMSG("%s, bad %s maxpacket\n", __func__, _ep->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return -ERANGE;
244 }
245
246 dev = ep->dev;
247 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800248 DMSG("%s, bogus device state\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return -ESHUTDOWN;
250 }
251
252 ep->desc = desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 ep->stopped = 0;
David Brownellad8c6232007-06-30 06:30:04 -0700254 ep->pio_irqs = 0;
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700255 ep->ep.maxpacket = usb_endpoint_maxp (desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 /* flush fifo (mostly for OUT buffers) */
Philipp Zabel7a857622008-06-22 23:36:39 +0100258 pxa25x_ep_fifo_flush (_ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260 /* ... reset halt state too, if we could ... */
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 DBG(DBG_VERBOSE, "enabled %s\n", _ep->name);
263 return 0;
264}
265
Philipp Zabel7a857622008-06-22 23:36:39 +0100266static int pxa25x_ep_disable (struct usb_ep *_ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Philipp Zabel7a857622008-06-22 23:36:39 +0100268 struct pxa25x_ep *ep;
David Brownell91987692005-05-07 13:20:19 -0700269 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Philipp Zabel7a857622008-06-22 23:36:39 +0100271 ep = container_of (_ep, struct pxa25x_ep, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 if (!_ep || !ep->desc) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800273 DMSG("%s, %s not enabled\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 _ep ? ep->ep.name : NULL);
275 return -EINVAL;
276 }
David Brownell91987692005-05-07 13:20:19 -0700277 local_irq_save(flags);
278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 nuke (ep, -ESHUTDOWN);
280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 /* flush fifo (mostly for IN buffers) */
Philipp Zabel7a857622008-06-22 23:36:39 +0100282 pxa25x_ep_fifo_flush (_ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 ep->desc = NULL;
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +0200285 ep->ep.desc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 ep->stopped = 1;
287
David Brownell91987692005-05-07 13:20:19 -0700288 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 DBG(DBG_VERBOSE, "%s disabled\n", _ep->name);
290 return 0;
291}
292
293/*-------------------------------------------------------------------------*/
294
Philipp Zabel7a857622008-06-22 23:36:39 +0100295/* for the pxa25x, these can just wrap kmalloc/kfree. gadget drivers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 * 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/*
Philipp Zabel7a857622008-06-22 23:36:39 +0100301 * pxa25x_ep_alloc_request - allocate a request data structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 */
303static struct usb_request *
Philipp Zabel7a857622008-06-22 23:36:39 +0100304pxa25x_ep_alloc_request (struct usb_ep *_ep, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
Philipp Zabel7a857622008-06-22 23:36:39 +0100306 struct pxa25x_request *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Eric Sesterhenn7039f422006-02-27 13:34:10 -0800308 req = kzalloc(sizeof(*req), gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 if (!req)
310 return NULL;
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 INIT_LIST_HEAD (&req->queue);
313 return &req->req;
314}
315
316
317/*
Philipp Zabel7a857622008-06-22 23:36:39 +0100318 * pxa25x_ep_free_request - deallocate a request data structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 */
320static void
Philipp Zabel7a857622008-06-22 23:36:39 +0100321pxa25x_ep_free_request (struct usb_ep *_ep, struct usb_request *_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
Philipp Zabel7a857622008-06-22 23:36:39 +0100323 struct pxa25x_request *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Philipp Zabel7a857622008-06-22 23:36:39 +0100325 req = container_of (_req, struct pxa25x_request, req);
Arjan van de Venb6c63932008-07-25 01:45:52 -0700326 WARN_ON(!list_empty (&req->queue));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 kfree(req);
328}
329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330/*-------------------------------------------------------------------------*/
331
332/*
333 * done - retire a request; caller blocked irqs
334 */
Philipp Zabel7a857622008-06-22 23:36:39 +0100335static void done(struct pxa25x_ep *ep, struct pxa25x_request *req, int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
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
Philipp Zabel7a857622008-06-22 23:36:39 +0100358static inline void ep0_idle (struct pxa25x_udc *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
360 dev->ep0state = EP0_IDLE;
361}
362
363static int
Philipp Zabel7a857622008-06-22 23:36:39 +0100364write_packet(volatile u32 *uddr, struct pxa25x_request *req, unsigned max)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
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
Philipp Zabel7a857622008-06-22 23:36:39 +0100389write_fifo (struct pxa25x_ep *ep, struct pxa25x_request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
391 unsigned max;
392
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700393 max = usb_endpoint_maxp(ep->desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 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);
David Brownellad8c6232007-06-30 06:30:04 -0700429 if (list_empty(&ep->queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 pio_irq_disable (ep->bEndpointAddress);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 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
Philipp Zabel7a857622008-06-22 23:36:39 +0100446void ep0start(struct pxa25x_udc *dev, u32 flags, const char *tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
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",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800452 __func__, tag, UDCCS0, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453}
454
455static int
Philipp Zabel7a857622008-06-22 23:36:39 +0100456write_ep0_fifo (struct pxa25x_ep *ep, struct pxa25x_request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
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);
Milan Svoboda043ea182006-05-29 03:34:00 -0700479#ifndef CONFIG_ARCH_IXP4XX
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480#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 }
499#endif
Milan Svoboda043ea182006-05-29 03:34:00 -0700500#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 } 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
Philipp Zabel7a857622008-06-22 23:36:39 +0100516read_fifo (struct pxa25x_ep *ep, struct pxa25x_request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
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
Philipp Zabel7a857622008-06-22 23:36:39 +0100593read_ep0_fifo (struct pxa25x_ep *ep, struct pxa25x_request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629/*-------------------------------------------------------------------------*/
630
631static int
Philipp Zabel7a857622008-06-22 23:36:39 +0100632pxa25x_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
Philipp Zabel7a857622008-06-22 23:36:39 +0100634 struct pxa25x_request *req;
635 struct pxa25x_ep *ep;
636 struct pxa25x_udc *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 unsigned long flags;
638
Philipp Zabel7a857622008-06-22 23:36:39 +0100639 req = container_of(_req, struct pxa25x_request, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 if (unlikely (!_req || !_req->complete || !_req->buf
641 || !list_empty(&req->queue))) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800642 DMSG("%s, bad params\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 return -EINVAL;
644 }
645
Philipp Zabel7a857622008-06-22 23:36:39 +0100646 ep = container_of(_ep, struct pxa25x_ep, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 if (unlikely (!_ep || (!ep->desc && ep->ep.name != ep0name))) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800648 DMSG("%s, bad ep\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 return -EINVAL;
650 }
651
652 dev = ep->dev;
653 if (unlikely (!dev->driver
654 || dev->gadget.speed == USB_SPEED_UNKNOWN)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800655 DMSG("%s, bogus device state\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 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
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700663 && req->req.length > usb_endpoint_maxp (ep->desc)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 return -EMSGSIZE;
665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 DBG(DBG_NOISY, "%s queue req %p, len %d buf %p\n",
David Brownellad8c6232007-06-30 06:30:04 -0700667 _ep->name, _req, _req->length, _req->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
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) {
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -0800676 if (ep->desc == NULL/* ep0 */) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 /* can the FIFO can satisfy the request immediately? */
David Brownell91987692005-05-07 13:20:19 -0700716 } 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 } else if ((*ep->reg_udccs & UDCCS_BO_RFS) != 0
721 && read_fifo(ep, req)) {
722 req = NULL;
723 }
724
David Brownellad8c6232007-06-30 06:30:04 -0700725 if (likely (req && ep->desc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 pio_irq_enable(ep->bEndpointAddress);
727 }
728
729 /* pio or dma irq handler advances the queue. */
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -0800730 if (likely(req != NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 list_add_tail(&req->queue, &ep->queue);
732 local_irq_restore(flags);
733
734 return 0;
735}
736
737
738/*
David Brownell34ebcd22007-02-24 12:23:52 -0800739 * nuke - dequeue ALL requests
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 */
Philipp Zabel7a857622008-06-22 23:36:39 +0100741static void nuke(struct pxa25x_ep *ep, int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
Philipp Zabel7a857622008-06-22 23:36:39 +0100743 struct pxa25x_request *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
745 /* called with irqs blocked */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 while (!list_empty(&ep->queue)) {
747 req = list_entry(ep->queue.next,
Philipp Zabel7a857622008-06-22 23:36:39 +0100748 struct pxa25x_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 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 */
Philipp Zabel7a857622008-06-22 23:36:39 +0100758static int pxa25x_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
Philipp Zabel7a857622008-06-22 23:36:39 +0100760 struct pxa25x_ep *ep;
761 struct pxa25x_request *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 unsigned long flags;
763
Philipp Zabel7a857622008-06-22 23:36:39 +0100764 ep = container_of(_ep, struct pxa25x_ep, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 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
David Brownellad8c6232007-06-30 06:30:04 -0700780 done(ep, req, -ECONNRESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782 local_irq_restore(flags);
783 return 0;
784}
785
786/*-------------------------------------------------------------------------*/
787
Philipp Zabel7a857622008-06-22 23:36:39 +0100788static int pxa25x_ep_set_halt(struct usb_ep *_ep, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
Philipp Zabel7a857622008-06-22 23:36:39 +0100790 struct pxa25x_ep *ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 unsigned long flags;
792
Philipp Zabel7a857622008-06-22 23:36:39 +0100793 ep = container_of(_ep, struct pxa25x_ep, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 if (unlikely (!_ep
795 || (!ep->desc && ep->ep.name != ep0name))
796 || ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800797 DMSG("%s, bad ep\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 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
David Brownell34ebcd22007-02-24 12:23:52 -0800828 /* 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
839 DBG(DBG_VERBOSE, "%s halt\n", _ep->name);
840 return 0;
841}
842
Philipp Zabel7a857622008-06-22 23:36:39 +0100843static int pxa25x_ep_fifo_status(struct usb_ep *_ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
Philipp Zabel7a857622008-06-22 23:36:39 +0100845 struct pxa25x_ep *ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Philipp Zabel7a857622008-06-22 23:36:39 +0100847 ep = container_of(_ep, struct pxa25x_ep, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 if (!_ep) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800849 DMSG("%s, bad ep\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 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
Philipp Zabel7a857622008-06-22 23:36:39 +0100862static void pxa25x_ep_fifo_flush(struct usb_ep *_ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863{
Philipp Zabel7a857622008-06-22 23:36:39 +0100864 struct pxa25x_ep *ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Philipp Zabel7a857622008-06-22 23:36:39 +0100866 ep = container_of(_ep, struct pxa25x_ep, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 if (!_ep || ep->ep.name == ep0name || !list_empty(&ep->queue)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800868 DMSG("%s, bad ep\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 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
Roel Kluin22eb36f2009-02-19 11:57:46 +0100883 | (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
884 ? 0 : UDCCS_BI_SST);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885}
886
887
Philipp Zabel7a857622008-06-22 23:36:39 +0100888static struct usb_ep_ops pxa25x_ep_ops = {
889 .enable = pxa25x_ep_enable,
890 .disable = pxa25x_ep_disable,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Philipp Zabel7a857622008-06-22 23:36:39 +0100892 .alloc_request = pxa25x_ep_alloc_request,
893 .free_request = pxa25x_ep_free_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Philipp Zabel7a857622008-06-22 23:36:39 +0100895 .queue = pxa25x_ep_queue,
896 .dequeue = pxa25x_ep_dequeue,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Philipp Zabel7a857622008-06-22 23:36:39 +0100898 .set_halt = pxa25x_ep_set_halt,
899 .fifo_status = pxa25x_ep_fifo_status,
900 .fifo_flush = pxa25x_ep_fifo_flush,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901};
902
903
904/* ---------------------------------------------------------------------------
David Brownell34ebcd22007-02-24 12:23:52 -0800905 * device-scoped parts of the api to the usb controller hardware
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 * ---------------------------------------------------------------------------
907 */
908
Philipp Zabel7a857622008-06-22 23:36:39 +0100909static int pxa25x_udc_get_frame(struct usb_gadget *_gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
911 return ((UFNRH & 0x07) << 8) | (UFNRL & 0xff);
912}
913
Philipp Zabel7a857622008-06-22 23:36:39 +0100914static int pxa25x_udc_wakeup(struct usb_gadget *_gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
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
Philipp Zabel7a857622008-06-22 23:36:39 +0100923static void stop_activity(struct pxa25x_udc *, struct usb_gadget_driver *);
924static void udc_enable (struct pxa25x_udc *);
925static void udc_disable(struct pxa25x_udc *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
927/* We disable the UDC -- and its 48 MHz clock -- whenever it's not
David Brownell34ebcd22007-02-24 12:23:52 -0800928 * in active use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 */
Philipp Zabel7a857622008-06-22 23:36:39 +0100930static int pullup(struct pxa25x_udc *udc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931{
Dmitry Baryshkov64cc2dd2008-02-22 17:17:19 -0800932 int is_active = udc->vbus && udc->pullup && !udc->suspended;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 DMSG("%s\n", is_active ? "active" : "inactive");
Dmitry Baryshkov64cc2dd2008-02-22 17:17:19 -0800934 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 }
Dmitry Baryshkov64cc2dd2008-02-22 17:17:19 -0800941 } 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 }
956 return 0;
957}
958
959/* VBUS reporting logically comes from a transceiver */
Philipp Zabel7a857622008-06-22 23:36:39 +0100960static int pxa25x_udc_vbus_session(struct usb_gadget *_gadget, int is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961{
Philipp Zabel7a857622008-06-22 23:36:39 +0100962 struct pxa25x_udc *udc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Philipp Zabel7a857622008-06-22 23:36:39 +0100964 udc = container_of(_gadget, struct pxa25x_udc, gadget);
Jaya Kumar47fd6f72008-11-18 02:32:36 +0100965 udc->vbus = is_active;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 DMSG("vbus %s\n", is_active ? "supplied" : "inactive");
Dmitry Baryshkov64cc2dd2008-02-22 17:17:19 -0800967 pullup(udc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 return 0;
969}
970
971/* drivers may have software control over D+ pullup */
Philipp Zabel7a857622008-06-22 23:36:39 +0100972static int pxa25x_udc_pullup(struct usb_gadget *_gadget, int is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973{
Philipp Zabel7a857622008-06-22 23:36:39 +0100974 struct pxa25x_udc *udc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Philipp Zabel7a857622008-06-22 23:36:39 +0100976 udc = container_of(_gadget, struct pxa25x_udc, gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
978 /* not all boards support pullup control */
Philipp Zabel56a075d2009-07-01 03:42:45 -0700979 if (!gpio_is_valid(udc->mach->gpio_pullup) && !udc->mach->udc_command)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 return -EOPNOTSUPP;
981
Dmitry Baryshkov64cc2dd2008-02-22 17:17:19 -0800982 udc->pullup = (is_active != 0);
983 pullup(udc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 return 0;
985}
986
Philipp Zabelab26d202009-07-01 03:46:25 -0700987/* 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)
Heikki Krogerusb96d3b02012-02-13 13:24:18 +0200998 return usb_phy_set_power(udc->transceiver, mA);
Philipp Zabelab26d202009-07-01 03:46:25 -0700999 return -EOPNOTSUPP;
1000}
1001
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001002static int pxa25x_start(struct usb_gadget_driver *driver,
1003 int (*bind)(struct usb_gadget *));
1004static int pxa25x_stop(struct usb_gadget_driver *driver);
1005
Philipp Zabel7a857622008-06-22 23:36:39 +01001006static 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,
Philipp Zabelab26d202009-07-01 03:46:25 -07001011 .vbus_draw = pxa25x_udc_vbus_draw,
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001012 .start = pxa25x_start,
1013 .stop = pxa25x_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014};
1015
1016/*-------------------------------------------------------------------------*/
1017
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -08001018#ifdef CONFIG_USB_GADGET_DEBUG_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
1020static int
Dmitry Baryshkov64cc2dd2008-02-22 17:17:19 -08001021udc_seq_show(struct seq_file *m, void *_d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022{
Philipp Zabel7a857622008-06-22 23:36:39 +01001023 struct pxa25x_udc *dev = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 unsigned long flags;
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -08001025 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 u32 tmp;
1027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 local_irq_save(flags);
1029
1030 /* basic device status */
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -08001031 seq_printf(m, DRIVER_DESC "\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 "%s version: %s\nGadget driver: %s\nHost %s\n\n",
David Brownellad8c6232007-06-30 06:30:04 -07001033 driver_name, DRIVER_VERSION SIZE_STR "(pio)",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 dev->driver ? dev->driver->driver.name : "(none)",
Dmitry Eremin-Solenikova8ecc862011-02-14 15:33:19 +03001035 dev->gadget.speed == USB_SPEED_FULL ? "full speed" : "disconnected");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
1037 /* registers for device and ep0 */
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -08001038 seq_printf(m,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 "uicr %02X.%02X, usir %02X.%02x, ufnr %02X.%02X\n",
1040 UICR1, UICR0, USIR1, USIR0, UFNRH, UFNRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
1042 tmp = UDCCR;
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -08001043 seq_printf(m,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 "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" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
1054 tmp = UDCCS0;
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -08001055 seq_printf(m,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 "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" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
1066 if (dev->has_cfr) {
1067 tmp = UDCCFR;
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -08001068 seq_printf(m,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 "udccfr %02X =%s%s\n", tmp,
1070 (tmp & UDCCFR_AREN) ? " aren" : "",
1071 (tmp & UDCCFR_ACM) ? " acm" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 }
1073
Dmitry Eremin-Solenikova8ecc862011-02-14 15:33:19 +03001074 if (dev->gadget.speed != USB_SPEED_FULL || !dev->driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 goto done;
1076
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -08001077 seq_printf(m, "ep0 IN %lu/%lu, OUT %lu/%lu\nirqs %lu\n\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 dev->stats.write.bytes, dev->stats.write.ops,
1079 dev->stats.read.bytes, dev->stats.read.ops,
1080 dev->stats.irqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
1082 /* dump endpoint queues */
1083 for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
Philipp Zabel7a857622008-06-22 23:36:39 +01001084 struct pxa25x_ep *ep = &dev->ep [i];
1085 struct pxa25x_request *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
1087 if (i != 0) {
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -08001088 const struct usb_endpoint_descriptor *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -08001090 desc = ep->desc;
1091 if (!desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 continue;
1093 tmp = *dev->ep [i].reg_udccs;
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -08001094 seq_printf(m,
David Brownellad8c6232007-06-30 06:30:04 -07001095 "%s max %d %s udccs %02x irqs %lu\n",
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07001096 ep->ep.name, usb_endpoint_maxp(desc),
David Brownellad8c6232007-06-30 06:30:04 -07001097 "pio", tmp, ep->pio_irqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 /* TODO translate all five groups of udccs bits! */
1099
1100 } else /* ep0 should only have one transfer queued */
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -08001101 seq_printf(m, "ep0 max 16 pio irqs %lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 ep->pio_irqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
1104 if (list_empty(&ep->queue)) {
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -08001105 seq_printf(m, "\t(nothing queued)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 continue;
1107 }
1108 list_for_each_entry(req, &ep->queue, queue) {
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -08001109 seq_printf(m,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 "\treq %p len %d/%d buf %p\n",
1111 &req->req, req->req.actual,
1112 req->req.length, req->req.buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 }
1114 }
1115
1116done:
1117 local_irq_restore(flags);
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -08001118 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119}
1120
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -08001121static 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
1146#else /* !CONFIG_USB_GADGET_DEBUG_FILES */
1147
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -08001148#define create_debug_files(dev) do {} while (0)
1149#define remove_debug_files(dev) do {} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
1151#endif /* CONFIG_USB_GADGET_DEBUG_FILES */
1152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153/*-------------------------------------------------------------------------*/
1154
1155/*
David Brownell34ebcd22007-02-24 12:23:52 -08001156 * udc_disable - disable USB device controller
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 */
Philipp Zabel7a857622008-06-22 23:36:39 +01001158static void udc_disable(struct pxa25x_udc *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159{
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 */
David Brownell91987692005-05-07 13:20:19 -07001166 pullup_off();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
1168 udc_clear_mask_UDCCR(UDCCR_UDE);
1169
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 ep0_idle (dev);
1171 dev->gadget.speed = USB_SPEED_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172}
1173
1174
1175/*
David Brownell34ebcd22007-02-24 12:23:52 -08001176 * udc_reinit - initialize software state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 */
Philipp Zabel7a857622008-06-22 23:36:39 +01001178static void udc_reinit(struct pxa25x_udc *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179{
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++) {
Philipp Zabel7a857622008-06-22 23:36:39 +01001189 struct pxa25x_ep *ep = &dev->ep[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
1191 if (i != 0)
1192 list_add_tail (&ep->ep.ep_list, &dev->gadget.ep_list);
1193
1194 ep->desc = NULL;
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +02001195 ep->ep.desc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 ep->stopped = 0;
1197 INIT_LIST_HEAD (&ep->queue);
David Brownellad8c6232007-06-30 06:30:04 -07001198 ep->pio_irqs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 }
1200
1201 /* the rest was statically initialized, and is read-only */
1202}
1203
1204/* until it's enabled, this UDC should be completely invisible
1205 * to any USB host.
1206 */
Philipp Zabel7a857622008-06-22 23:36:39 +01001207static void udc_enable (struct pxa25x_udc *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
1209 udc_clear_mask_UDCCR(UDCCR_UDE);
1210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 /* try to clear these bits before we enable the udc */
1212 udc_ack_int_UDCCR(UDCCR_SUSIR|/*UDCCR_RSTIR|*/UDCCR_RESIR);
1213
1214 ep0_idle(dev);
1215 dev->gadget.speed = USB_SPEED_UNKNOWN;
1216 dev->stats.irqs = 0;
1217
1218 /*
1219 * sequence taken from chapter 12.5.10, PXA250 AppProcDevManual:
1220 * - enable UDC
1221 * - if RESET is already in progress, ack interrupt
1222 * - unmask reset interrupt
1223 */
1224 udc_set_mask_UDCCR(UDCCR_UDE);
1225 if (!(UDCCR & UDCCR_UDA))
1226 udc_ack_int_UDCCR(UDCCR_RSTIR);
1227
1228 if (dev->has_cfr /* UDC_RES2 is defined */) {
1229 /* pxa255 (a0+) can avoid a set_config race that could
1230 * prevent gadget drivers from configuring correctly
1231 */
1232 UDCCFR = UDCCFR_ACM | UDCCFR_MB1;
1233 } else {
1234 /* "USB test mode" for pxa250 errata 40-42 (stepping a0, a1)
1235 * which could result in missing packets and interrupts.
1236 * supposedly one bit per endpoint, controlling whether it
1237 * double buffers or not; ACM/AREN bits fit into the holes.
1238 * zero bits (like USIR0_IRx) disable double buffering.
1239 */
1240 UDC_RES1 = 0x00;
1241 UDC_RES2 = 0x00;
1242 }
1243
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 /* enable suspend/resume and reset irqs */
1245 udc_clear_mask_UDCCR(UDCCR_SRM | UDCCR_REM);
1246
1247 /* enable ep0 irqs */
1248 UICR0 &= ~UICR0_IM0;
1249
1250 /* if hardware supports it, pullup D+ and wait for reset */
David Brownell91987692005-05-07 13:20:19 -07001251 pullup_on();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252}
1253
1254
1255/* when a driver is successfully registered, it will receive
1256 * control requests including set_configuration(), which enables
1257 * non-control requests. then usb traffic follows until a
1258 * disconnect is reported. then a host may connect again, or
1259 * the driver might get unbound.
1260 */
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001261static int pxa25x_start(struct usb_gadget_driver *driver,
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001262 int (*bind)(struct usb_gadget *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263{
Philipp Zabel7a857622008-06-22 23:36:39 +01001264 struct pxa25x_udc *dev = the_controller;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 int retval;
1266
1267 if (!driver
Michal Nazarewicz7177aed2011-11-19 18:27:38 +01001268 || driver->max_speed < USB_SPEED_FULL
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001269 || !bind
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 || !driver->disconnect
1271 || !driver->setup)
1272 return -EINVAL;
1273 if (!dev)
1274 return -ENODEV;
1275 if (dev->driver)
1276 return -EBUSY;
1277
1278 /* first hook up the driver ... */
1279 dev->driver = driver;
1280 dev->gadget.dev.driver = &driver->driver;
1281 dev->pullup = 1;
1282
David Brownell34ebcd22007-02-24 12:23:52 -08001283 retval = device_add (&dev->gadget.dev);
1284 if (retval) {
1285fail:
1286 dev->driver = NULL;
1287 dev->gadget.dev.driver = NULL;
1288 return retval;
1289 }
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001290 retval = bind(&dev->gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 if (retval) {
1292 DMSG("bind to driver %s --> error %d\n",
1293 driver->driver.name, retval);
1294 device_del (&dev->gadget.dev);
David Brownell34ebcd22007-02-24 12:23:52 -08001295 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
1298 /* ... then enable host detection and ep0; and we're ready
1299 * for set_configuration as well as eventual disconnect.
1300 */
1301 DMSG("registered gadget driver '%s'\n", driver->driver.name);
Philipp Zabelab26d202009-07-01 03:46:25 -07001302
1303 /* connect to bus through transceiver */
1304 if (dev->transceiver) {
Heikki Krogerus6e13c652012-02-13 13:24:20 +02001305 retval = otg_set_peripheral(dev->transceiver->otg,
1306 &dev->gadget);
Philipp Zabelab26d202009-07-01 03:46:25 -07001307 if (retval) {
1308 DMSG("can't bind to transceiver\n");
1309 if (driver->unbind)
1310 driver->unbind(&dev->gadget);
1311 goto bind_fail;
1312 }
1313 }
1314
Dmitry Baryshkov64cc2dd2008-02-22 17:17:19 -08001315 pullup(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 dump_state(dev);
1317 return 0;
Philipp Zabelab26d202009-07-01 03:46:25 -07001318bind_fail:
1319 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
1322static void
Philipp Zabel7a857622008-06-22 23:36:39 +01001323stop_activity(struct pxa25x_udc *dev, struct usb_gadget_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324{
1325 int i;
1326
1327 /* don't disconnect drivers more than once */
1328 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1329 driver = NULL;
1330 dev->gadget.speed = USB_SPEED_UNKNOWN;
1331
1332 /* prevent new request submissions, kill any outstanding requests */
1333 for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
Philipp Zabel7a857622008-06-22 23:36:39 +01001334 struct pxa25x_ep *ep = &dev->ep[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
1336 ep->stopped = 1;
1337 nuke(ep, -ESHUTDOWN);
1338 }
1339 del_timer_sync(&dev->timer);
1340
1341 /* report disconnect; the driver is already quiesced */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 if (driver)
1343 driver->disconnect(&dev->gadget);
1344
1345 /* re-init driver-visible data structures */
1346 udc_reinit(dev);
1347}
1348
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001349static int pxa25x_stop(struct usb_gadget_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350{
Philipp Zabel7a857622008-06-22 23:36:39 +01001351 struct pxa25x_udc *dev = the_controller;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353 if (!dev)
1354 return -ENODEV;
David Brownell6bea4762006-12-05 03:15:33 -08001355 if (!driver || driver != dev->driver || !driver->unbind)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 return -EINVAL;
1357
1358 local_irq_disable();
Dmitry Baryshkov64cc2dd2008-02-22 17:17:19 -08001359 dev->pullup = 0;
1360 pullup(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 stop_activity(dev, driver);
1362 local_irq_enable();
1363
Philipp Zabelab26d202009-07-01 03:46:25 -07001364 if (dev->transceiver)
Heikki Krogerus6e13c652012-02-13 13:24:20 +02001365 (void) otg_set_peripheral(dev->transceiver->otg, NULL);
Philipp Zabelab26d202009-07-01 03:46:25 -07001366
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 driver->unbind(&dev->gadget);
Patrik Sevalliuseb0be472007-11-20 09:32:00 -08001368 dev->gadget.dev.driver = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 dev->driver = NULL;
1370
1371 device_del (&dev->gadget.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
1373 DMSG("unregistered gadget driver '%s'\n", driver->driver.name);
1374 dump_state(dev);
1375 return 0;
1376}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
1378/*-------------------------------------------------------------------------*/
1379
1380#ifdef CONFIG_ARCH_LUBBOCK
1381
1382/* Lubbock has separate connect and disconnect irqs. More typical designs
1383 * use one GPIO as the VBUS IRQ, and another to control the D+ pullup.
1384 */
1385
1386static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +01001387lubbock_vbus_irq(int irq, void *_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388{
Philipp Zabel7a857622008-06-22 23:36:39 +01001389 struct pxa25x_udc *dev = _dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 int vbus;
1391
1392 dev->stats.irqs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 switch (irq) {
1394 case LUBBOCK_USB_IRQ:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 vbus = 1;
1396 disable_irq(LUBBOCK_USB_IRQ);
1397 enable_irq(LUBBOCK_USB_DISC_IRQ);
1398 break;
1399 case LUBBOCK_USB_DISC_IRQ:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 vbus = 0;
1401 disable_irq(LUBBOCK_USB_DISC_IRQ);
1402 enable_irq(LUBBOCK_USB_IRQ);
1403 break;
1404 default:
1405 return IRQ_NONE;
1406 }
1407
Philipp Zabel7a857622008-06-22 23:36:39 +01001408 pxa25x_udc_vbus_session(&dev->gadget, vbus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 return IRQ_HANDLED;
1410}
1411
1412#endif
1413
1414
1415/*-------------------------------------------------------------------------*/
1416
Philipp Zabel7a857622008-06-22 23:36:39 +01001417static inline void clear_ep_state (struct pxa25x_udc *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418{
1419 unsigned i;
1420
1421 /* hardware SET_{CONFIGURATION,INTERFACE} automagic resets endpoint
1422 * fifos, and pending transactions mustn't be continued in any case.
1423 */
1424 for (i = 1; i < PXA_UDC_NUM_ENDPOINTS; i++)
1425 nuke(&dev->ep[i], -ECONNABORTED);
1426}
1427
1428static void udc_watchdog(unsigned long _dev)
1429{
Philipp Zabel7a857622008-06-22 23:36:39 +01001430 struct pxa25x_udc *dev = (void *)_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
1432 local_irq_disable();
1433 if (dev->ep0state == EP0_STALL
1434 && (UDCCS0 & UDCCS0_FST) == 0
1435 && (UDCCS0 & UDCCS0_SST) == 0) {
1436 UDCCS0 = UDCCS0_FST|UDCCS0_FTF;
1437 DBG(DBG_VERBOSE, "ep0 re-stall\n");
1438 start_watchdog(dev);
1439 }
1440 local_irq_enable();
1441}
1442
Philipp Zabel7a857622008-06-22 23:36:39 +01001443static void handle_ep0 (struct pxa25x_udc *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444{
1445 u32 udccs0 = UDCCS0;
Philipp Zabel7a857622008-06-22 23:36:39 +01001446 struct pxa25x_ep *ep = &dev->ep [0];
1447 struct pxa25x_request *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 union {
1449 struct usb_ctrlrequest r;
1450 u8 raw [8];
1451 u32 word [2];
1452 } u;
1453
1454 if (list_empty(&ep->queue))
1455 req = NULL;
1456 else
Philipp Zabel7a857622008-06-22 23:36:39 +01001457 req = list_entry(ep->queue.next, struct pxa25x_request, queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
1459 /* clear stall status */
1460 if (udccs0 & UDCCS0_SST) {
1461 nuke(ep, -EPIPE);
1462 UDCCS0 = UDCCS0_SST;
1463 del_timer(&dev->timer);
1464 ep0_idle(dev);
1465 }
1466
1467 /* previous request unfinished? non-error iff back-to-back ... */
1468 if ((udccs0 & UDCCS0_SA) != 0 && dev->ep0state != EP0_IDLE) {
1469 nuke(ep, 0);
1470 del_timer(&dev->timer);
1471 ep0_idle(dev);
1472 }
1473
1474 switch (dev->ep0state) {
1475 case EP0_IDLE:
1476 /* late-breaking status? */
1477 udccs0 = UDCCS0;
1478
1479 /* start control request? */
1480 if (likely((udccs0 & (UDCCS0_OPR|UDCCS0_SA|UDCCS0_RNE))
1481 == (UDCCS0_OPR|UDCCS0_SA|UDCCS0_RNE))) {
1482 int i;
1483
1484 nuke (ep, -EPROTO);
1485
1486 /* read SETUP packet */
1487 for (i = 0; i < 8; i++) {
1488 if (unlikely(!(UDCCS0 & UDCCS0_RNE))) {
1489bad_setup:
1490 DMSG("SETUP %d!\n", i);
1491 goto stall;
1492 }
1493 u.raw [i] = (u8) UDDR0;
1494 }
1495 if (unlikely((UDCCS0 & UDCCS0_RNE) != 0))
1496 goto bad_setup;
1497
1498got_setup:
1499 DBG(DBG_VERBOSE, "SETUP %02x.%02x v%04x i%04x l%04x\n",
1500 u.r.bRequestType, u.r.bRequest,
1501 le16_to_cpu(u.r.wValue),
1502 le16_to_cpu(u.r.wIndex),
1503 le16_to_cpu(u.r.wLength));
1504
1505 /* cope with automagic for some standard requests. */
1506 dev->req_std = (u.r.bRequestType & USB_TYPE_MASK)
1507 == USB_TYPE_STANDARD;
1508 dev->req_config = 0;
1509 dev->req_pending = 1;
1510 switch (u.r.bRequest) {
1511 /* hardware restricts gadget drivers here! */
1512 case USB_REQ_SET_CONFIGURATION:
1513 if (u.r.bRequestType == USB_RECIP_DEVICE) {
1514 /* reflect hardware's automagic
1515 * up to the gadget driver.
1516 */
1517config_change:
1518 dev->req_config = 1;
1519 clear_ep_state(dev);
1520 /* if !has_cfr, there's no synch
1521 * else use AREN (later) not SA|OPR
1522 * USIR0_IR0 acts edge sensitive
1523 */
1524 }
1525 break;
1526 /* ... and here, even more ... */
1527 case USB_REQ_SET_INTERFACE:
1528 if (u.r.bRequestType == USB_RECIP_INTERFACE) {
1529 /* udc hardware is broken by design:
1530 * - altsetting may only be zero;
1531 * - hw resets all interfaces' eps;
1532 * - ep reset doesn't include halt(?).
1533 */
1534 DMSG("broken set_interface (%d/%d)\n",
1535 le16_to_cpu(u.r.wIndex),
1536 le16_to_cpu(u.r.wValue));
1537 goto config_change;
1538 }
1539 break;
1540 /* hardware was supposed to hide this */
1541 case USB_REQ_SET_ADDRESS:
1542 if (u.r.bRequestType == USB_RECIP_DEVICE) {
1543 ep0start(dev, 0, "address");
1544 return;
1545 }
1546 break;
1547 }
1548
1549 if (u.r.bRequestType & USB_DIR_IN)
1550 dev->ep0state = EP0_IN_DATA_PHASE;
1551 else
1552 dev->ep0state = EP0_OUT_DATA_PHASE;
1553
1554 i = dev->driver->setup(&dev->gadget, &u.r);
1555 if (i < 0) {
1556 /* hardware automagic preventing STALL... */
1557 if (dev->req_config) {
1558 /* hardware sometimes neglects to tell
1559 * tell us about config change events,
1560 * so later ones may fail...
1561 */
Arjan van de Venb6c63932008-07-25 01:45:52 -07001562 WARNING("config change %02x fail %d?\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 u.r.bRequest, i);
1564 return;
1565 /* TODO experiment: if has_cfr,
1566 * hardware didn't ACK; maybe we
1567 * could actually STALL!
1568 */
1569 }
1570 DBG(DBG_VERBOSE, "protocol STALL, "
1571 "%02x err %d\n", UDCCS0, i);
1572stall:
1573 /* the watchdog timer helps deal with cases
1574 * where udc seems to clear FST wrongly, and
1575 * then NAKs instead of STALLing.
1576 */
1577 ep0start(dev, UDCCS0_FST|UDCCS0_FTF, "stall");
1578 start_watchdog(dev);
1579 dev->ep0state = EP0_STALL;
1580
1581 /* deferred i/o == no response yet */
1582 } else if (dev->req_pending) {
1583 if (likely(dev->ep0state == EP0_IN_DATA_PHASE
1584 || dev->req_std || u.r.wLength))
1585 ep0start(dev, 0, "defer");
1586 else
1587 ep0start(dev, UDCCS0_IPR, "defer/IPR");
1588 }
1589
1590 /* expect at least one data or status stage irq */
1591 return;
1592
1593 } else if (likely((udccs0 & (UDCCS0_OPR|UDCCS0_SA))
1594 == (UDCCS0_OPR|UDCCS0_SA))) {
1595 unsigned i;
1596
1597 /* pxa210/250 erratum 131 for B0/B1 says RNE lies.
1598 * still observed on a pxa255 a0.
1599 */
1600 DBG(DBG_VERBOSE, "e131\n");
1601 nuke(ep, -EPROTO);
1602
1603 /* read SETUP data, but don't trust it too much */
1604 for (i = 0; i < 8; i++)
1605 u.raw [i] = (u8) UDDR0;
1606 if ((u.r.bRequestType & USB_RECIP_MASK)
1607 > USB_RECIP_OTHER)
1608 goto stall;
1609 if (u.word [0] == 0 && u.word [1] == 0)
1610 goto stall;
1611 goto got_setup;
1612 } else {
1613 /* some random early IRQ:
1614 * - we acked FST
1615 * - IPR cleared
1616 * - OPR got set, without SA (likely status stage)
1617 */
1618 UDCCS0 = udccs0 & (UDCCS0_SA|UDCCS0_OPR);
1619 }
1620 break;
1621 case EP0_IN_DATA_PHASE: /* GET_DESCRIPTOR etc */
1622 if (udccs0 & UDCCS0_OPR) {
1623 UDCCS0 = UDCCS0_OPR|UDCCS0_FTF;
1624 DBG(DBG_VERBOSE, "ep0in premature status\n");
1625 if (req)
1626 done(ep, req, 0);
1627 ep0_idle(dev);
1628 } else /* irq was IPR clearing */ {
1629 if (req) {
1630 /* this IN packet might finish the request */
1631 (void) write_ep0_fifo(ep, req);
1632 } /* else IN token before response was written */
1633 }
1634 break;
1635 case EP0_OUT_DATA_PHASE: /* SET_DESCRIPTOR etc */
1636 if (udccs0 & UDCCS0_OPR) {
1637 if (req) {
1638 /* this OUT packet might finish the request */
1639 if (read_ep0_fifo(ep, req))
1640 done(ep, req, 0);
1641 /* else more OUT packets expected */
1642 } /* else OUT token before read was issued */
1643 } else /* irq was IPR clearing */ {
1644 DBG(DBG_VERBOSE, "ep0out premature status\n");
1645 if (req)
1646 done(ep, req, 0);
1647 ep0_idle(dev);
1648 }
1649 break;
1650 case EP0_END_XFER:
1651 if (req)
1652 done(ep, req, 0);
1653 /* ack control-IN status (maybe in-zlp was skipped)
1654 * also appears after some config change events.
1655 */
1656 if (udccs0 & UDCCS0_OPR)
1657 UDCCS0 = UDCCS0_OPR;
1658 ep0_idle(dev);
1659 break;
1660 case EP0_STALL:
1661 UDCCS0 = UDCCS0_FST;
1662 break;
1663 }
1664 USIR0 = USIR0_IR0;
1665}
1666
Philipp Zabel7a857622008-06-22 23:36:39 +01001667static void handle_ep(struct pxa25x_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668{
Philipp Zabel7a857622008-06-22 23:36:39 +01001669 struct pxa25x_request *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 int is_in = ep->bEndpointAddress & USB_DIR_IN;
1671 int completed;
1672 u32 udccs, tmp;
1673
1674 do {
1675 completed = 0;
1676 if (likely (!list_empty(&ep->queue)))
1677 req = list_entry(ep->queue.next,
Philipp Zabel7a857622008-06-22 23:36:39 +01001678 struct pxa25x_request, queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 else
1680 req = NULL;
1681
1682 // TODO check FST handling
1683
1684 udccs = *ep->reg_udccs;
1685 if (unlikely(is_in)) { /* irq from TPC, SST, or (ISO) TUR */
1686 tmp = UDCCS_BI_TUR;
1687 if (likely(ep->bmAttributes == USB_ENDPOINT_XFER_BULK))
1688 tmp |= UDCCS_BI_SST;
1689 tmp &= udccs;
1690 if (likely (tmp))
1691 *ep->reg_udccs = tmp;
1692 if (req && likely ((udccs & UDCCS_BI_TFS) != 0))
1693 completed = write_fifo(ep, req);
1694
1695 } else { /* irq from RPC (or for ISO, ROF) */
1696 if (likely(ep->bmAttributes == USB_ENDPOINT_XFER_BULK))
1697 tmp = UDCCS_BO_SST | UDCCS_BO_DME;
1698 else
1699 tmp = UDCCS_IO_ROF | UDCCS_IO_DME;
1700 tmp &= udccs;
1701 if (likely(tmp))
1702 *ep->reg_udccs = tmp;
1703
1704 /* fifos can hold packets, ready for reading... */
1705 if (likely(req)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 completed = read_fifo(ep, req);
1707 } else
1708 pio_irq_disable (ep->bEndpointAddress);
1709 }
1710 ep->pio_irqs++;
1711 } while (completed);
1712}
1713
1714/*
Philipp Zabel7a857622008-06-22 23:36:39 +01001715 * pxa25x_udc_irq - interrupt handler
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 *
1717 * avoid delays in ep0 processing. the control handshaking isn't always
1718 * under software control (pxa250c0 and the pxa255 are better), and delays
1719 * could cause usb protocol errors.
1720 */
1721static irqreturn_t
Philipp Zabel7a857622008-06-22 23:36:39 +01001722pxa25x_udc_irq(int irq, void *_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723{
Philipp Zabel7a857622008-06-22 23:36:39 +01001724 struct pxa25x_udc *dev = _dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 int handled;
1726
1727 dev->stats.irqs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 do {
1729 u32 udccr = UDCCR;
1730
1731 handled = 0;
1732
1733 /* SUSpend Interrupt Request */
1734 if (unlikely(udccr & UDCCR_SUSIR)) {
1735 udc_ack_int_UDCCR(UDCCR_SUSIR);
1736 handled = 1;
Dmitry Eremin-Solenikova8ecc862011-02-14 15:33:19 +03001737 DBG(DBG_VERBOSE, "USB suspend\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
Dmitry Eremin-Solenikova8ecc862011-02-14 15:33:19 +03001739 if (dev->gadget.speed != USB_SPEED_UNKNOWN
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 && dev->driver
1741 && dev->driver->suspend)
1742 dev->driver->suspend(&dev->gadget);
1743 ep0_idle (dev);
1744 }
1745
1746 /* RESume Interrupt Request */
1747 if (unlikely(udccr & UDCCR_RESIR)) {
1748 udc_ack_int_UDCCR(UDCCR_RESIR);
1749 handled = 1;
1750 DBG(DBG_VERBOSE, "USB resume\n");
1751
1752 if (dev->gadget.speed != USB_SPEED_UNKNOWN
1753 && dev->driver
Dmitry Eremin-Solenikova8ecc862011-02-14 15:33:19 +03001754 && dev->driver->resume)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 dev->driver->resume(&dev->gadget);
1756 }
1757
1758 /* ReSeT Interrupt Request - USB reset */
1759 if (unlikely(udccr & UDCCR_RSTIR)) {
1760 udc_ack_int_UDCCR(UDCCR_RSTIR);
1761 handled = 1;
1762
1763 if ((UDCCR & UDCCR_UDA) == 0) {
1764 DBG(DBG_VERBOSE, "USB reset start\n");
1765
1766 /* reset driver and endpoints,
1767 * in case that's not yet done
1768 */
1769 stop_activity (dev, dev->driver);
1770
1771 } else {
1772 DBG(DBG_VERBOSE, "USB reset end\n");
1773 dev->gadget.speed = USB_SPEED_FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 memset(&dev->stats, 0, sizeof dev->stats);
1775 /* driver and endpoints are still reset */
1776 }
1777
1778 } else {
1779 u32 usir0 = USIR0 & ~UICR0;
1780 u32 usir1 = USIR1 & ~UICR1;
1781 int i;
1782
1783 if (unlikely (!usir0 && !usir1))
1784 continue;
1785
1786 DBG(DBG_VERY_NOISY, "irq %02x.%02x\n", usir1, usir0);
1787
1788 /* control traffic */
1789 if (usir0 & USIR0_IR0) {
1790 dev->ep[0].pio_irqs++;
1791 handle_ep0(dev);
1792 handled = 1;
1793 }
1794
1795 /* endpoint data transfers */
1796 for (i = 0; i < 8; i++) {
1797 u32 tmp = 1 << i;
1798
1799 if (i && (usir0 & tmp)) {
1800 handle_ep(&dev->ep[i]);
1801 USIR0 |= tmp;
1802 handled = 1;
1803 }
David Brownell6d845992009-07-01 03:43:58 -07001804#ifndef CONFIG_USB_PXA25X_SMALL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 if (usir1 & tmp) {
1806 handle_ep(&dev->ep[i+8]);
1807 USIR1 |= tmp;
1808 handled = 1;
1809 }
David Brownell6d845992009-07-01 03:43:58 -07001810#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 }
1812 }
1813
1814 /* we could also ask for 1 msec SOF (SIR) interrupts */
1815
1816 } while (handled);
1817 return IRQ_HANDLED;
1818}
1819
1820/*-------------------------------------------------------------------------*/
1821
1822static void nop_release (struct device *dev)
1823{
Kay Sievers7071a3c2008-05-02 06:02:41 +02001824 DMSG("%s %s\n", __func__, dev_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825}
1826
1827/* this uses load-time allocation and initialization (instead of
1828 * doing it at run-time) to save code, eliminate fault paths, and
1829 * be more obviously correct.
1830 */
Philipp Zabel7a857622008-06-22 23:36:39 +01001831static struct pxa25x_udc memory = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 .gadget = {
Philipp Zabel7a857622008-06-22 23:36:39 +01001833 .ops = &pxa25x_udc_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 .ep0 = &memory.ep[0].ep,
1835 .name = driver_name,
1836 .dev = {
Kay Sieversc682b172009-01-06 10:44:42 -08001837 .init_name = "gadget",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 .release = nop_release,
1839 },
1840 },
1841
1842 /* control endpoint */
1843 .ep[0] = {
1844 .ep = {
1845 .name = ep0name,
Philipp Zabel7a857622008-06-22 23:36:39 +01001846 .ops = &pxa25x_ep_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 .maxpacket = EP0_FIFO_SIZE,
1848 },
1849 .dev = &memory,
1850 .reg_udccs = &UDCCS0,
1851 .reg_uddr = &UDDR0,
1852 },
1853
1854 /* first group of endpoints */
1855 .ep[1] = {
1856 .ep = {
1857 .name = "ep1in-bulk",
Philipp Zabel7a857622008-06-22 23:36:39 +01001858 .ops = &pxa25x_ep_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 .maxpacket = BULK_FIFO_SIZE,
1860 },
1861 .dev = &memory,
1862 .fifo_size = BULK_FIFO_SIZE,
1863 .bEndpointAddress = USB_DIR_IN | 1,
1864 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1865 .reg_udccs = &UDCCS1,
1866 .reg_uddr = &UDDR1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 },
1868 .ep[2] = {
1869 .ep = {
1870 .name = "ep2out-bulk",
Philipp Zabel7a857622008-06-22 23:36:39 +01001871 .ops = &pxa25x_ep_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 .maxpacket = BULK_FIFO_SIZE,
1873 },
1874 .dev = &memory,
1875 .fifo_size = BULK_FIFO_SIZE,
1876 .bEndpointAddress = 2,
1877 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1878 .reg_udccs = &UDCCS2,
1879 .reg_ubcr = &UBCR2,
1880 .reg_uddr = &UDDR2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 },
Philipp Zabel7a857622008-06-22 23:36:39 +01001882#ifndef CONFIG_USB_PXA25X_SMALL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 .ep[3] = {
1884 .ep = {
1885 .name = "ep3in-iso",
Philipp Zabel7a857622008-06-22 23:36:39 +01001886 .ops = &pxa25x_ep_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 .maxpacket = ISO_FIFO_SIZE,
1888 },
1889 .dev = &memory,
1890 .fifo_size = ISO_FIFO_SIZE,
1891 .bEndpointAddress = USB_DIR_IN | 3,
1892 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
1893 .reg_udccs = &UDCCS3,
1894 .reg_uddr = &UDDR3,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 },
1896 .ep[4] = {
1897 .ep = {
1898 .name = "ep4out-iso",
Philipp Zabel7a857622008-06-22 23:36:39 +01001899 .ops = &pxa25x_ep_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 .maxpacket = ISO_FIFO_SIZE,
1901 },
1902 .dev = &memory,
1903 .fifo_size = ISO_FIFO_SIZE,
1904 .bEndpointAddress = 4,
1905 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
1906 .reg_udccs = &UDCCS4,
1907 .reg_ubcr = &UBCR4,
1908 .reg_uddr = &UDDR4,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 },
1910 .ep[5] = {
1911 .ep = {
1912 .name = "ep5in-int",
Philipp Zabel7a857622008-06-22 23:36:39 +01001913 .ops = &pxa25x_ep_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 .maxpacket = INT_FIFO_SIZE,
1915 },
1916 .dev = &memory,
1917 .fifo_size = INT_FIFO_SIZE,
1918 .bEndpointAddress = USB_DIR_IN | 5,
1919 .bmAttributes = USB_ENDPOINT_XFER_INT,
1920 .reg_udccs = &UDCCS5,
1921 .reg_uddr = &UDDR5,
1922 },
1923
1924 /* second group of endpoints */
1925 .ep[6] = {
1926 .ep = {
1927 .name = "ep6in-bulk",
Philipp Zabel7a857622008-06-22 23:36:39 +01001928 .ops = &pxa25x_ep_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 .maxpacket = BULK_FIFO_SIZE,
1930 },
1931 .dev = &memory,
1932 .fifo_size = BULK_FIFO_SIZE,
1933 .bEndpointAddress = USB_DIR_IN | 6,
1934 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1935 .reg_udccs = &UDCCS6,
1936 .reg_uddr = &UDDR6,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 },
1938 .ep[7] = {
1939 .ep = {
1940 .name = "ep7out-bulk",
Philipp Zabel7a857622008-06-22 23:36:39 +01001941 .ops = &pxa25x_ep_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 .maxpacket = BULK_FIFO_SIZE,
1943 },
1944 .dev = &memory,
1945 .fifo_size = BULK_FIFO_SIZE,
1946 .bEndpointAddress = 7,
1947 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1948 .reg_udccs = &UDCCS7,
1949 .reg_ubcr = &UBCR7,
1950 .reg_uddr = &UDDR7,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 },
1952 .ep[8] = {
1953 .ep = {
1954 .name = "ep8in-iso",
Philipp Zabel7a857622008-06-22 23:36:39 +01001955 .ops = &pxa25x_ep_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 .maxpacket = ISO_FIFO_SIZE,
1957 },
1958 .dev = &memory,
1959 .fifo_size = ISO_FIFO_SIZE,
1960 .bEndpointAddress = USB_DIR_IN | 8,
1961 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
1962 .reg_udccs = &UDCCS8,
1963 .reg_uddr = &UDDR8,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 },
1965 .ep[9] = {
1966 .ep = {
1967 .name = "ep9out-iso",
Philipp Zabel7a857622008-06-22 23:36:39 +01001968 .ops = &pxa25x_ep_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 .maxpacket = ISO_FIFO_SIZE,
1970 },
1971 .dev = &memory,
1972 .fifo_size = ISO_FIFO_SIZE,
1973 .bEndpointAddress = 9,
1974 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
1975 .reg_udccs = &UDCCS9,
1976 .reg_ubcr = &UBCR9,
1977 .reg_uddr = &UDDR9,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 },
1979 .ep[10] = {
1980 .ep = {
1981 .name = "ep10in-int",
Philipp Zabel7a857622008-06-22 23:36:39 +01001982 .ops = &pxa25x_ep_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 .maxpacket = INT_FIFO_SIZE,
1984 },
1985 .dev = &memory,
1986 .fifo_size = INT_FIFO_SIZE,
1987 .bEndpointAddress = USB_DIR_IN | 10,
1988 .bmAttributes = USB_ENDPOINT_XFER_INT,
1989 .reg_udccs = &UDCCS10,
1990 .reg_uddr = &UDDR10,
1991 },
1992
1993 /* third group of endpoints */
1994 .ep[11] = {
1995 .ep = {
1996 .name = "ep11in-bulk",
Philipp Zabel7a857622008-06-22 23:36:39 +01001997 .ops = &pxa25x_ep_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 .maxpacket = BULK_FIFO_SIZE,
1999 },
2000 .dev = &memory,
2001 .fifo_size = BULK_FIFO_SIZE,
2002 .bEndpointAddress = USB_DIR_IN | 11,
2003 .bmAttributes = USB_ENDPOINT_XFER_BULK,
2004 .reg_udccs = &UDCCS11,
2005 .reg_uddr = &UDDR11,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 },
2007 .ep[12] = {
2008 .ep = {
2009 .name = "ep12out-bulk",
Philipp Zabel7a857622008-06-22 23:36:39 +01002010 .ops = &pxa25x_ep_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 .maxpacket = BULK_FIFO_SIZE,
2012 },
2013 .dev = &memory,
2014 .fifo_size = BULK_FIFO_SIZE,
2015 .bEndpointAddress = 12,
2016 .bmAttributes = USB_ENDPOINT_XFER_BULK,
2017 .reg_udccs = &UDCCS12,
2018 .reg_ubcr = &UBCR12,
2019 .reg_uddr = &UDDR12,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 },
2021 .ep[13] = {
2022 .ep = {
2023 .name = "ep13in-iso",
Philipp Zabel7a857622008-06-22 23:36:39 +01002024 .ops = &pxa25x_ep_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 .maxpacket = ISO_FIFO_SIZE,
2026 },
2027 .dev = &memory,
2028 .fifo_size = ISO_FIFO_SIZE,
2029 .bEndpointAddress = USB_DIR_IN | 13,
2030 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
2031 .reg_udccs = &UDCCS13,
2032 .reg_uddr = &UDDR13,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 },
2034 .ep[14] = {
2035 .ep = {
2036 .name = "ep14out-iso",
Philipp Zabel7a857622008-06-22 23:36:39 +01002037 .ops = &pxa25x_ep_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 .maxpacket = ISO_FIFO_SIZE,
2039 },
2040 .dev = &memory,
2041 .fifo_size = ISO_FIFO_SIZE,
2042 .bEndpointAddress = 14,
2043 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
2044 .reg_udccs = &UDCCS14,
2045 .reg_ubcr = &UBCR14,
2046 .reg_uddr = &UDDR14,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 },
2048 .ep[15] = {
2049 .ep = {
2050 .name = "ep15in-int",
Philipp Zabel7a857622008-06-22 23:36:39 +01002051 .ops = &pxa25x_ep_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 .maxpacket = INT_FIFO_SIZE,
2053 },
2054 .dev = &memory,
2055 .fifo_size = INT_FIFO_SIZE,
2056 .bEndpointAddress = USB_DIR_IN | 15,
2057 .bmAttributes = USB_ENDPOINT_XFER_INT,
2058 .reg_udccs = &UDCCS15,
2059 .reg_uddr = &UDDR15,
2060 },
Philipp Zabel7a857622008-06-22 23:36:39 +01002061#endif /* !CONFIG_USB_PXA25X_SMALL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062};
2063
2064#define CP15R0_VENDOR_MASK 0xffffe000
2065
2066#if defined(CONFIG_ARCH_PXA)
2067#define CP15R0_XSCALE_VALUE 0x69052000 /* intel/arm/xscale */
2068
2069#elif defined(CONFIG_ARCH_IXP4XX)
2070#define CP15R0_XSCALE_VALUE 0x69054000 /* intel/arm/ixp4xx */
2071
2072#endif
2073
2074#define CP15R0_PROD_MASK 0x000003f0
2075#define PXA25x 0x00000100 /* and PXA26x */
2076#define PXA210 0x00000120
2077
2078#define CP15R0_REV_MASK 0x0000000f
2079
2080#define CP15R0_PRODREV_MASK (CP15R0_PROD_MASK | CP15R0_REV_MASK)
2081
2082#define PXA255_A0 0x00000106 /* or PXA260_B1 */
2083#define PXA250_C0 0x00000105 /* or PXA26x_B0 */
2084#define PXA250_B2 0x00000104
2085#define PXA250_B1 0x00000103 /* or PXA260_A0 */
2086#define PXA250_B0 0x00000102
2087#define PXA250_A1 0x00000101
2088#define PXA250_A0 0x00000100
2089
2090#define PXA210_C0 0x00000125
2091#define PXA210_B2 0x00000124
2092#define PXA210_B1 0x00000123
2093#define PXA210_B0 0x00000122
2094#define IXP425_A0 0x000001c1
David Brownell827982c2006-11-20 11:38:57 -08002095#define IXP425_B0 0x000001f1
Milan Svoboda043ea182006-05-29 03:34:00 -07002096#define IXP465_AD 0x00000200
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
2098/*
David Brownell34ebcd22007-02-24 12:23:52 -08002099 * probe - binds to the platform device
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 */
Philipp Zabel7a857622008-06-22 23:36:39 +01002101static int __init pxa25x_udc_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102{
Philipp Zabel7a857622008-06-22 23:36:39 +01002103 struct pxa25x_udc *dev = &memory;
Dmitry Eremin-Solenikova8ecc862011-02-14 15:33:19 +03002104 int retval, irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 u32 chiprev;
2106
2107 /* insist on Intel/ARM/XScale */
2108 asm("mrc%? p15, 0, %0, c0, c0" : "=r" (chiprev));
2109 if ((chiprev & CP15R0_VENDOR_MASK) != CP15R0_XSCALE_VALUE) {
David Brownell00274922007-11-19 12:58:36 -08002110 pr_err("%s: not XScale!\n", driver_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 return -ENODEV;
2112 }
2113
2114 /* trigger chiprev-specific logic */
2115 switch (chiprev & CP15R0_PRODREV_MASK) {
2116#if defined(CONFIG_ARCH_PXA)
2117 case PXA255_A0:
2118 dev->has_cfr = 1;
2119 break;
2120 case PXA250_A0:
2121 case PXA250_A1:
2122 /* A0/A1 "not released"; ep 13, 15 unusable */
2123 /* fall through */
2124 case PXA250_B2: case PXA210_B2:
2125 case PXA250_B1: case PXA210_B1:
2126 case PXA250_B0: case PXA210_B0:
David Brownellad8c6232007-06-30 06:30:04 -07002127 /* OUT-DMA is broken ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 /* fall through */
2129 case PXA250_C0: case PXA210_C0:
2130 break;
2131#elif defined(CONFIG_ARCH_IXP4XX)
2132 case IXP425_A0:
David Brownell827982c2006-11-20 11:38:57 -08002133 case IXP425_B0:
Milan Svoboda043ea182006-05-29 03:34:00 -07002134 case IXP465_AD:
2135 dev->has_cfr = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 break;
2137#endif
2138 default:
David Brownell00274922007-11-19 12:58:36 -08002139 pr_err("%s: unrecognized processor: %08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 driver_name, chiprev);
2141 /* iop3xx, ixp4xx, ... */
2142 return -ENODEV;
2143 }
2144
David Brownell34ebcd22007-02-24 12:23:52 -08002145 irq = platform_get_irq(pdev, 0);
2146 if (irq < 0)
2147 return -ENODEV;
2148
Russell Kinge0d8b132008-11-11 17:52:32 +00002149 dev->clk = clk_get(&pdev->dev, NULL);
Russell King6549e6c2007-08-20 10:33:35 +01002150 if (IS_ERR(dev->clk)) {
2151 retval = PTR_ERR(dev->clk);
2152 goto err_clk;
2153 }
Russell King6549e6c2007-08-20 10:33:35 +01002154
David Brownellad8c6232007-06-30 06:30:04 -07002155 pr_debug("%s: IRQ %d%s%s\n", driver_name, irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 dev->has_cfr ? "" : " (!cfr)",
David Brownellad8c6232007-06-30 06:30:04 -07002157 SIZE_STR "(pio)"
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 );
2159
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 /* other non-static parts of init */
Russell King3ae5eae2005-11-09 22:32:44 +00002161 dev->dev = &pdev->dev;
2162 dev->mach = pdev->dev.platform_data;
Milan Svoboda9068a4c2007-06-30 06:25:35 -07002163
Heikki Krogerusb96d3b02012-02-13 13:24:18 +02002164 dev->transceiver = usb_get_transceiver();
Philipp Zabelab26d202009-07-01 03:46:25 -07002165
Philipp Zabel56a075d2009-07-01 03:42:45 -07002166 if (gpio_is_valid(dev->mach->gpio_pullup)) {
Milan Svoboda9068a4c2007-06-30 06:25:35 -07002167 if ((retval = gpio_request(dev->mach->gpio_pullup,
Philipp Zabel7a857622008-06-22 23:36:39 +01002168 "pca25x_udc GPIO PULLUP"))) {
Milan Svoboda9068a4c2007-06-30 06:25:35 -07002169 dev_dbg(&pdev->dev,
2170 "can't get pullup gpio %d, err: %d\n",
2171 dev->mach->gpio_pullup, retval);
Russell King6549e6c2007-08-20 10:33:35 +01002172 goto err_gpio_pullup;
Milan Svoboda9068a4c2007-06-30 06:25:35 -07002173 }
2174 gpio_direction_output(dev->mach->gpio_pullup, 0);
2175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176
2177 init_timer(&dev->timer);
2178 dev->timer.function = udc_watchdog;
2179 dev->timer.data = (unsigned long) dev;
2180
2181 device_initialize(&dev->gadget.dev);
Russell King3ae5eae2005-11-09 22:32:44 +00002182 dev->gadget.dev.parent = &pdev->dev;
2183 dev->gadget.dev.dma_mask = pdev->dev.dma_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
2185 the_controller = dev;
Russell King3ae5eae2005-11-09 22:32:44 +00002186 platform_set_drvdata(pdev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
2188 udc_disable(dev);
2189 udc_reinit(dev);
2190
Dmitry Eremin-Solenikova8ecc862011-02-14 15:33:19 +03002191 dev->vbus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192
2193 /* irq setup after old hardware state is cleaned up */
Philipp Zabel7a857622008-06-22 23:36:39 +01002194 retval = request_irq(irq, pxa25x_udc_irq,
Yong Zhangb5dd18d2011-09-07 16:10:52 +08002195 0, driver_name, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 if (retval != 0) {
David Brownell00274922007-11-19 12:58:36 -08002197 pr_err("%s: can't get irq %d, err %d\n",
David Brownell34ebcd22007-02-24 12:23:52 -08002198 driver_name, irq, retval);
Russell King6549e6c2007-08-20 10:33:35 +01002199 goto err_irq1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 }
2201 dev->got_irq = 1;
2202
2203#ifdef CONFIG_ARCH_LUBBOCK
2204 if (machine_is_lubbock()) {
2205 retval = request_irq(LUBBOCK_USB_DISC_IRQ,
2206 lubbock_vbus_irq,
Yong Zhangb5dd18d2011-09-07 16:10:52 +08002207 IRQF_SAMPLE_RANDOM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 driver_name, dev);
2209 if (retval != 0) {
David Brownell00274922007-11-19 12:58:36 -08002210 pr_err("%s: can't get irq %i, err %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 driver_name, LUBBOCK_USB_DISC_IRQ, retval);
Russell King6549e6c2007-08-20 10:33:35 +01002212 goto err_irq_lub;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 }
2214 retval = request_irq(LUBBOCK_USB_IRQ,
2215 lubbock_vbus_irq,
Yong Zhangb5dd18d2011-09-07 16:10:52 +08002216 IRQF_SAMPLE_RANDOM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 driver_name, dev);
2218 if (retval != 0) {
David Brownell00274922007-11-19 12:58:36 -08002219 pr_err("%s: can't get irq %i, err %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 driver_name, LUBBOCK_USB_IRQ, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 goto lubbock_fail0;
2222 }
David Brownellb2bbb202006-06-29 12:25:39 -07002223 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224#endif
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -08002225 create_debug_files(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03002227 retval = usb_add_gadget_udc(&pdev->dev, &dev->gadget);
2228 if (!retval)
2229 return retval;
Russell King6549e6c2007-08-20 10:33:35 +01002230
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03002231 remove_debug_files(dev);
Russell King6549e6c2007-08-20 10:33:35 +01002232#ifdef CONFIG_ARCH_LUBBOCK
Sebastian Andrzej Siewiora6207b12011-06-03 19:50:46 +02002233lubbock_fail0:
Russell King6549e6c2007-08-20 10:33:35 +01002234 free_irq(LUBBOCK_USB_DISC_IRQ, dev);
2235 err_irq_lub:
Russell King6549e6c2007-08-20 10:33:35 +01002236 free_irq(irq, dev);
Sebastian Andrzej Siewiora6207b12011-06-03 19:50:46 +02002237#endif
Russell King6549e6c2007-08-20 10:33:35 +01002238 err_irq1:
Philipp Zabel56a075d2009-07-01 03:42:45 -07002239 if (gpio_is_valid(dev->mach->gpio_pullup))
Russell King6549e6c2007-08-20 10:33:35 +01002240 gpio_free(dev->mach->gpio_pullup);
2241 err_gpio_pullup:
Philipp Zabelab26d202009-07-01 03:46:25 -07002242 if (dev->transceiver) {
Heikki Krogerusb96d3b02012-02-13 13:24:18 +02002243 usb_put_transceiver(dev->transceiver);
Philipp Zabelab26d202009-07-01 03:46:25 -07002244 dev->transceiver = NULL;
2245 }
Russell King6549e6c2007-08-20 10:33:35 +01002246 clk_put(dev->clk);
2247 err_clk:
Russell King6549e6c2007-08-20 10:33:35 +01002248 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249}
David Brownell91987692005-05-07 13:20:19 -07002250
Philipp Zabel7a857622008-06-22 23:36:39 +01002251static void pxa25x_udc_shutdown(struct platform_device *_dev)
David Brownell91987692005-05-07 13:20:19 -07002252{
2253 pullup_off();
2254}
2255
Philipp Zabel7a857622008-06-22 23:36:39 +01002256static int __exit pxa25x_udc_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257{
Philipp Zabel7a857622008-06-22 23:36:39 +01002258 struct pxa25x_udc *dev = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03002260 usb_del_gadget_udc(&dev->gadget);
David Brownell6bea4762006-12-05 03:15:33 -08002261 if (dev->driver)
2262 return -EBUSY;
2263
Dmitry Baryshkov64cc2dd2008-02-22 17:17:19 -08002264 dev->pullup = 0;
2265 pullup(dev);
2266
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -08002267 remove_debug_files(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
2269 if (dev->got_irq) {
David Brownell34ebcd22007-02-24 12:23:52 -08002270 free_irq(platform_get_irq(pdev, 0), dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 dev->got_irq = 0;
2272 }
Milan Svoboda44df45a2006-05-29 03:34:00 -07002273#ifdef CONFIG_ARCH_LUBBOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 if (machine_is_lubbock()) {
2275 free_irq(LUBBOCK_USB_DISC_IRQ, dev);
2276 free_irq(LUBBOCK_USB_IRQ, dev);
2277 }
Milan Svoboda44df45a2006-05-29 03:34:00 -07002278#endif
Philipp Zabel56a075d2009-07-01 03:42:45 -07002279 if (gpio_is_valid(dev->mach->gpio_pullup))
Milan Svoboda9068a4c2007-06-30 06:25:35 -07002280 gpio_free(dev->mach->gpio_pullup);
2281
Russell King6549e6c2007-08-20 10:33:35 +01002282 clk_put(dev->clk);
Russell King6549e6c2007-08-20 10:33:35 +01002283
Philipp Zabelab26d202009-07-01 03:46:25 -07002284 if (dev->transceiver) {
Heikki Krogerusb96d3b02012-02-13 13:24:18 +02002285 usb_put_transceiver(dev->transceiver);
Philipp Zabelab26d202009-07-01 03:46:25 -07002286 dev->transceiver = NULL;
2287 }
2288
Russell King3ae5eae2005-11-09 22:32:44 +00002289 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 the_controller = NULL;
2291 return 0;
2292}
2293
2294/*-------------------------------------------------------------------------*/
2295
2296#ifdef CONFIG_PM
2297
2298/* USB suspend (controlled by the host) and system suspend (controlled
2299 * by the PXA) don't necessarily work well together. If USB is active,
2300 * the 48 MHz clock is required; so the system can't enter 33 MHz idle
2301 * mode, or any deeper PM saving state.
2302 *
2303 * For now, we punt and forcibly disconnect from the USB host when PXA
2304 * enters any suspend state. While we're disconnected, we always disable
David Brownell34ebcd22007-02-24 12:23:52 -08002305 * the 48MHz USB clock ... allowing PXA sleep and/or 33 MHz idle states.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 * Boards without software pullup control shouldn't use those states.
2307 * VBUS IRQs should probably be ignored so that the PXA device just acts
2308 * "dead" to USB hosts until system resume.
2309 */
Philipp Zabel7a857622008-06-22 23:36:39 +01002310static int pxa25x_udc_suspend(struct platform_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311{
Philipp Zabel7a857622008-06-22 23:36:39 +01002312 struct pxa25x_udc *udc = platform_get_drvdata(dev);
Dmitry Baryshkov64cc2dd2008-02-22 17:17:19 -08002313 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
Philipp Zabel56a075d2009-07-01 03:42:45 -07002315 if (!gpio_is_valid(udc->mach->gpio_pullup) && !udc->mach->udc_command)
Arjan van de Venb6c63932008-07-25 01:45:52 -07002316 WARNING("USB host won't detect disconnect!\n");
Dmitry Baryshkov64cc2dd2008-02-22 17:17:19 -08002317 udc->suspended = 1;
2318
2319 local_irq_save(flags);
2320 pullup(udc);
2321 local_irq_restore(flags);
Russell King9480e302005-10-28 09:52:56 -07002322
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 return 0;
2324}
2325
Philipp Zabel7a857622008-06-22 23:36:39 +01002326static int pxa25x_udc_resume(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327{
Philipp Zabel7a857622008-06-22 23:36:39 +01002328 struct pxa25x_udc *udc = platform_get_drvdata(dev);
Dmitry Baryshkov64cc2dd2008-02-22 17:17:19 -08002329 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330
Dmitry Baryshkov64cc2dd2008-02-22 17:17:19 -08002331 udc->suspended = 0;
2332 local_irq_save(flags);
2333 pullup(udc);
2334 local_irq_restore(flags);
Russell King9480e302005-10-28 09:52:56 -07002335
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 return 0;
2337}
2338
2339#else
Philipp Zabel7a857622008-06-22 23:36:39 +01002340#define pxa25x_udc_suspend NULL
2341#define pxa25x_udc_resume NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342#endif
2343
2344/*-------------------------------------------------------------------------*/
2345
Russell King3ae5eae2005-11-09 22:32:44 +00002346static struct platform_driver udc_driver = {
Philipp Zabel7a857622008-06-22 23:36:39 +01002347 .shutdown = pxa25x_udc_shutdown,
2348 .remove = __exit_p(pxa25x_udc_remove),
2349 .suspend = pxa25x_udc_suspend,
2350 .resume = pxa25x_udc_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00002351 .driver = {
2352 .owner = THIS_MODULE,
Philipp Zabel7a857622008-06-22 23:36:39 +01002353 .name = "pxa25x-udc",
Russell King3ae5eae2005-11-09 22:32:44 +00002354 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355};
2356
2357static int __init udc_init(void)
2358{
David Brownell00274922007-11-19 12:58:36 -08002359 pr_info("%s: version %s\n", driver_name, DRIVER_VERSION);
Philipp Zabel7a857622008-06-22 23:36:39 +01002360 return platform_driver_probe(&udc_driver, pxa25x_udc_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361}
2362module_init(udc_init);
2363
2364static void __exit udc_exit(void)
2365{
Russell King3ae5eae2005-11-09 22:32:44 +00002366 platform_driver_unregister(&udc_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367}
2368module_exit(udc_exit);
2369
2370MODULE_DESCRIPTION(DRIVER_DESC);
2371MODULE_AUTHOR("Frank Becker, Robert Schwebel, David Brownell");
2372MODULE_LICENSE("GPL");
Philipp Zabel7a857622008-06-22 23:36:39 +01002373MODULE_ALIAS("platform:pxa25x-udc");