blob: 7862465291d11ba8946635d2efc371c72a99336c [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.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 */
25
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -080026/* #define VERBOSE_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Milan Svoboda9068a4c2007-06-30 06:25:35 -070028#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/module.h>
30#include <linux/kernel.h>
31#include <linux/ioport.h>
32#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/errno.h>
34#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/slab.h>
36#include <linux/init.h>
37#include <linux/timer.h>
38#include <linux/list.h>
39#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/mm.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010041#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/dma-mapping.h>
Nicolas Pitrec7a3bd12006-10-20 14:20:17 -070043#include <linux/irq.h>
Russell King6549e6c2007-08-20 10:33:35 +010044#include <linux/clk.h>
45#include <linux/err.h>
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -080046#include <linux/seq_file.h>
47#include <linux/debugfs.h>
Russell King284d1152008-04-20 17:32:16 +010048#include <linux/io.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -070049#include <linux/prefetch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51#include <asm/byteorder.h>
52#include <asm/dma.h>
Milan Svoboda9068a4c2007-06-30 06:25:35 -070053#include <asm/gpio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <asm/system.h>
55#include <asm/mach-types.h>
56#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
David Brownell5f848132006-12-16 15:34:53 -080058#include <linux/usb/ch9.h>
David Brownell9454a572007-10-04 18:05:17 -070059#include <linux/usb/gadget.h>
Philipp Zabelab26d202009-07-01 03:46:25 -070060#include <linux/usb/otg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Russell King284d1152008-04-20 17:32:16 +010062/*
63 * This driver is PXA25x only. Grab the right register definitions.
64 */
65#ifdef CONFIG_ARCH_PXA
Russell Kinga09e64f2008-08-05 16:14:15 +010066#include <mach/pxa25x-udc.h>
Russell King284d1152008-04-20 17:32:16 +010067#endif
68
Eric Miao0dc726b2009-12-27 23:01:25 +080069#ifdef CONFIG_ARCH_LUBBOCK
70#include <mach/lubbock.h>
71#endif
72
Milan Svoboda9068a4c2007-06-30 06:25:35 -070073#include <asm/mach/udc_pxa2xx.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75
76/*
David Brownell91987692005-05-07 13:20:19 -070077 * This driver handles the USB Device Controller (UDC) in Intel's PXA 25x
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 * series processors. The UDC for the IXP 4xx series is very similar.
79 * There are fifteen endpoints, in addition to ep0.
80 *
81 * Such controller drivers work with a gadget driver. The gadget driver
82 * returns descriptors, implements configuration and data protocols used
83 * by the host to interact with this device, and allocates endpoints to
84 * the different protocol interfaces. The controller driver virtualizes
85 * usb hardware so that the gadget drivers will be more portable.
David Brownell34ebcd22007-02-24 12:23:52 -080086 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 * This UDC hardware wants to implement a bit too much USB protocol, so
88 * it constrains the sorts of USB configuration change events that work.
89 * The errata for these chips are misleading; some "fixed" bugs from
90 * pxa250 a0/a1 b0/b1/b2 sure act like they're still there.
David Brownellad8c6232007-06-30 06:30:04 -070091 *
92 * Note that the UDC hardware supports DMA (except on IXP) but that's
93 * not used here. IN-DMA (to host) is simple enough, when the data is
94 * suitably aligned (16 bytes) ... the network stack doesn't do that,
95 * other software can. OUT-DMA is buggy in most chip versions, as well
96 * as poorly designed (data toggle not automatic). So this driver won't
97 * bother using DMA. (Mostly-working IN-DMA support was available in
98 * kernels before 2.6.23, but was never enabled or well tested.)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 */
100
David Brownellad8c6232007-06-30 06:30:04 -0700101#define DRIVER_VERSION "30-June-2007"
David Brownell91987692005-05-07 13:20:19 -0700102#define DRIVER_DESC "PXA 25x USB Device Controller driver"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104
Philipp Zabel7a857622008-06-22 23:36:39 +0100105static const char driver_name [] = "pxa25x_udc";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107static const char ep0name [] = "ep0";
108
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110#ifdef CONFIG_ARCH_IXP4XX
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112/* cpu-specific register addresses are compiled in to this code */
113#ifdef CONFIG_ARCH_PXA
114#error "Can't configure both IXP and PXA"
115#endif
116
Dmitry Baryshkov64cc2dd2008-02-22 17:17:19 -0800117/* IXP doesn't yet support <linux/clk.h> */
118#define clk_get(dev,name) NULL
119#define clk_enable(clk) do { } while (0)
120#define clk_disable(clk) do { } while (0)
121#define clk_put(clk) do { } while (0)
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123#endif
124
Philipp Zabel7a857622008-06-22 23:36:39 +0100125#include "pxa25x_udc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127
Philipp Zabel7a857622008-06-22 23:36:39 +0100128#ifdef CONFIG_USB_PXA25X_SMALL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129#define SIZE_STR " (small)"
130#else
131#define SIZE_STR ""
132#endif
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134/* ---------------------------------------------------------------------------
David Brownell34ebcd22007-02-24 12:23:52 -0800135 * endpoint related parts of the api to the usb controller hardware,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 * used by gadget driver; and the inner talker-to-hardware core.
137 * ---------------------------------------------------------------------------
138 */
139
Philipp Zabel7a857622008-06-22 23:36:39 +0100140static void pxa25x_ep_fifo_flush (struct usb_ep *ep);
141static void nuke (struct pxa25x_ep *, int status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
David Brownellb2bbb202006-06-29 12:25:39 -0700143/* one GPIO should control a D+ pullup, so host sees this device (or not) */
144static void pullup_off(void)
145{
146 struct pxa2xx_udc_mach_info *mach = the_controller->mach;
Ian Molton8fb105f2008-06-25 22:34:51 +0100147 int off_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, off_level);
David Brownellb2bbb202006-06-29 12:25:39 -0700151 else if (mach->udc_command)
152 mach->udc_command(PXA2XX_UDC_CMD_DISCONNECT);
153}
154
155static void pullup_on(void)
156{
157 struct pxa2xx_udc_mach_info *mach = the_controller->mach;
Ian Molton8fb105f2008-06-25 22:34:51 +0100158 int on_level = !mach->gpio_pullup_inverted;
David Brownellb2bbb202006-06-29 12:25:39 -0700159
Philipp Zabel56a075d2009-07-01 03:42:45 -0700160 if (gpio_is_valid(mach->gpio_pullup))
Ian Molton8fb105f2008-06-25 22:34:51 +0100161 gpio_set_value(mach->gpio_pullup, on_level);
David Brownellb2bbb202006-06-29 12:25:39 -0700162 else if (mach->udc_command)
163 mach->udc_command(PXA2XX_UDC_CMD_CONNECT);
164}
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166static void pio_irq_enable(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
177static void pio_irq_disable(int bEndpointAddress)
178{
179 bEndpointAddress &= 0xf;
180 if (bEndpointAddress < 8)
181 UICR0 |= 1 << bEndpointAddress;
182 else {
183 bEndpointAddress -= 8;
184 UICR1 |= 1 << bEndpointAddress;
185 }
186}
187
188/* The UDCCR reg contains mask and interrupt status bits,
189 * so using '|=' isn't safe as it may ack an interrupt.
190 */
191#define UDCCR_MASK_BITS (UDCCR_REM | UDCCR_SRM | UDCCR_UDE)
192
193static inline void udc_set_mask_UDCCR(int mask)
194{
195 UDCCR = (UDCCR & UDCCR_MASK_BITS) | (mask & UDCCR_MASK_BITS);
196}
197
198static inline void udc_clear_mask_UDCCR(int mask)
199{
200 UDCCR = (UDCCR & UDCCR_MASK_BITS) & ~(mask & UDCCR_MASK_BITS);
201}
202
203static inline void udc_ack_int_UDCCR(int mask)
204{
205 /* udccr contains the bits we dont want to change */
206 __u32 udccr = UDCCR & UDCCR_MASK_BITS;
207
208 UDCCR = udccr | (mask & ~UDCCR_MASK_BITS);
209}
210
211/*
212 * endpoint enable/disable
213 *
Philipp Zabel7a857622008-06-22 23:36:39 +0100214 * we need to verify the descriptors used to enable endpoints. since pxa25x
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 * endpoint configurations are fixed, and are pretty much always enabled,
216 * there's not a lot to manage here.
217 *
Philipp Zabel7a857622008-06-22 23:36:39 +0100218 * because pxa25x can't selectively initialize bulk (or interrupt) endpoints,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 * (resetting endpoint halt and toggle), SET_INTERFACE is unusable except
220 * for a single interface (with only the default altsetting) and for gadget
221 * drivers that don't halt endpoints (not reset by set_interface). that also
222 * means that if you use ISO, you must violate the USB spec rule that all
223 * iso endpoints must be in non-default altsettings.
224 */
Philipp Zabel7a857622008-06-22 23:36:39 +0100225static int pxa25x_ep_enable (struct usb_ep *_ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 const struct usb_endpoint_descriptor *desc)
227{
Philipp Zabel7a857622008-06-22 23:36:39 +0100228 struct pxa25x_ep *ep;
229 struct pxa25x_udc *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Philipp Zabel7a857622008-06-22 23:36:39 +0100231 ep = container_of (_ep, struct pxa25x_ep, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 if (!_ep || !desc || ep->desc || _ep->name == ep0name
233 || desc->bDescriptorType != USB_DT_ENDPOINT
234 || ep->bEndpointAddress != desc->bEndpointAddress
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700235 || ep->fifo_size < usb_endpoint_maxp (desc)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800236 DMSG("%s, bad ep or descriptor\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 return -EINVAL;
238 }
239
240 /* xfer types must match, except that interrupt ~= bulk */
241 if (ep->bmAttributes != desc->bmAttributes
242 && ep->bmAttributes != USB_ENDPOINT_XFER_BULK
243 && desc->bmAttributes != USB_ENDPOINT_XFER_INT) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800244 DMSG("%s, %s type mismatch\n", __func__, _ep->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 return -EINVAL;
246 }
247
248 /* hardware _could_ do smaller, but driver doesn't */
249 if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700250 && usb_endpoint_maxp (desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 != BULK_FIFO_SIZE)
252 || !desc->wMaxPacketSize) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800253 DMSG("%s, bad %s maxpacket\n", __func__, _ep->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 return -ERANGE;
255 }
256
257 dev = ep->dev;
258 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800259 DMSG("%s, bogus device state\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return -ESHUTDOWN;
261 }
262
263 ep->desc = desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 ep->stopped = 0;
David Brownellad8c6232007-06-30 06:30:04 -0700265 ep->pio_irqs = 0;
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700266 ep->ep.maxpacket = usb_endpoint_maxp (desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 /* flush fifo (mostly for OUT buffers) */
Philipp Zabel7a857622008-06-22 23:36:39 +0100269 pxa25x_ep_fifo_flush (_ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 /* ... reset halt state too, if we could ... */
272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 DBG(DBG_VERBOSE, "enabled %s\n", _ep->name);
274 return 0;
275}
276
Philipp Zabel7a857622008-06-22 23:36:39 +0100277static int pxa25x_ep_disable (struct usb_ep *_ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
Philipp Zabel7a857622008-06-22 23:36:39 +0100279 struct pxa25x_ep *ep;
David Brownell91987692005-05-07 13:20:19 -0700280 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Philipp Zabel7a857622008-06-22 23:36:39 +0100282 ep = container_of (_ep, struct pxa25x_ep, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 if (!_ep || !ep->desc) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800284 DMSG("%s, %s not enabled\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 _ep ? ep->ep.name : NULL);
286 return -EINVAL;
287 }
David Brownell91987692005-05-07 13:20:19 -0700288 local_irq_save(flags);
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 nuke (ep, -ESHUTDOWN);
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 /* flush fifo (mostly for IN buffers) */
Philipp Zabel7a857622008-06-22 23:36:39 +0100293 pxa25x_ep_fifo_flush (_ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
295 ep->desc = NULL;
296 ep->stopped = 1;
297
David Brownell91987692005-05-07 13:20:19 -0700298 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 DBG(DBG_VERBOSE, "%s disabled\n", _ep->name);
300 return 0;
301}
302
303/*-------------------------------------------------------------------------*/
304
Philipp Zabel7a857622008-06-22 23:36:39 +0100305/* for the pxa25x, these can just wrap kmalloc/kfree. gadget drivers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 * must still pass correctly initialized endpoints, since other controller
307 * drivers may care about how it's currently set up (dma issues etc).
308 */
309
310/*
Philipp Zabel7a857622008-06-22 23:36:39 +0100311 * pxa25x_ep_alloc_request - allocate a request data structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 */
313static struct usb_request *
Philipp Zabel7a857622008-06-22 23:36:39 +0100314pxa25x_ep_alloc_request (struct usb_ep *_ep, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
Philipp Zabel7a857622008-06-22 23:36:39 +0100316 struct pxa25x_request *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Eric Sesterhenn7039f422006-02-27 13:34:10 -0800318 req = kzalloc(sizeof(*req), gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 if (!req)
320 return NULL;
321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 INIT_LIST_HEAD (&req->queue);
323 return &req->req;
324}
325
326
327/*
Philipp Zabel7a857622008-06-22 23:36:39 +0100328 * pxa25x_ep_free_request - deallocate a request data structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 */
330static void
Philipp Zabel7a857622008-06-22 23:36:39 +0100331pxa25x_ep_free_request (struct usb_ep *_ep, struct usb_request *_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
Philipp Zabel7a857622008-06-22 23:36:39 +0100333 struct pxa25x_request *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Philipp Zabel7a857622008-06-22 23:36:39 +0100335 req = container_of (_req, struct pxa25x_request, req);
Arjan van de Venb6c63932008-07-25 01:45:52 -0700336 WARN_ON(!list_empty (&req->queue));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 kfree(req);
338}
339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340/*-------------------------------------------------------------------------*/
341
342/*
343 * done - retire a request; caller blocked irqs
344 */
Philipp Zabel7a857622008-06-22 23:36:39 +0100345static void done(struct pxa25x_ep *ep, struct pxa25x_request *req, int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
347 unsigned stopped = ep->stopped;
348
349 list_del_init(&req->queue);
350
351 if (likely (req->req.status == -EINPROGRESS))
352 req->req.status = status;
353 else
354 status = req->req.status;
355
356 if (status && status != -ESHUTDOWN)
357 DBG(DBG_VERBOSE, "complete %s req %p stat %d len %u/%u\n",
358 ep->ep.name, &req->req, status,
359 req->req.actual, req->req.length);
360
361 /* don't modify queue heads during completion callback */
362 ep->stopped = 1;
363 req->req.complete(&ep->ep, &req->req);
364 ep->stopped = stopped;
365}
366
367
Philipp Zabel7a857622008-06-22 23:36:39 +0100368static inline void ep0_idle (struct pxa25x_udc *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
370 dev->ep0state = EP0_IDLE;
371}
372
373static int
Philipp Zabel7a857622008-06-22 23:36:39 +0100374write_packet(volatile u32 *uddr, struct pxa25x_request *req, unsigned max)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
376 u8 *buf;
377 unsigned length, count;
378
379 buf = req->req.buf + req->req.actual;
380 prefetch(buf);
381
382 /* how big will this packet be? */
383 length = min(req->req.length - req->req.actual, max);
384 req->req.actual += length;
385
386 count = length;
387 while (likely(count--))
388 *uddr = *buf++;
389
390 return length;
391}
392
393/*
394 * write to an IN endpoint fifo, as many packets as possible.
395 * irqs will use this to write the rest later.
396 * caller guarantees at least one packet buffer is ready (or a zlp).
397 */
398static int
Philipp Zabel7a857622008-06-22 23:36:39 +0100399write_fifo (struct pxa25x_ep *ep, struct pxa25x_request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
401 unsigned max;
402
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700403 max = usb_endpoint_maxp(ep->desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 do {
405 unsigned count;
406 int is_last, is_short;
407
408 count = write_packet(ep->reg_uddr, req, max);
409
410 /* last packet is usually short (or a zlp) */
411 if (unlikely (count != max))
412 is_last = is_short = 1;
413 else {
414 if (likely(req->req.length != req->req.actual)
415 || req->req.zero)
416 is_last = 0;
417 else
418 is_last = 1;
419 /* interrupt/iso maxpacket may not fill the fifo */
420 is_short = unlikely (max < ep->fifo_size);
421 }
422
423 DBG(DBG_VERY_NOISY, "wrote %s %d bytes%s%s %d left %p\n",
424 ep->ep.name, count,
425 is_last ? "/L" : "", is_short ? "/S" : "",
426 req->req.length - req->req.actual, req);
427
428 /* let loose that packet. maybe try writing another one,
429 * double buffering might work. TSP, TPC, and TFS
430 * bit values are the same for all normal IN endpoints.
431 */
432 *ep->reg_udccs = UDCCS_BI_TPC;
433 if (is_short)
434 *ep->reg_udccs = UDCCS_BI_TSP;
435
436 /* requests complete when all IN data is in the FIFO */
437 if (is_last) {
438 done (ep, req, 0);
David Brownellad8c6232007-06-30 06:30:04 -0700439 if (list_empty(&ep->queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 pio_irq_disable (ep->bEndpointAddress);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return 1;
442 }
443
444 // TODO experiment: how robust can fifo mode tweaking be?
445 // double buffering is off in the default fifo mode, which
446 // prevents TFS from being set here.
447
448 } while (*ep->reg_udccs & UDCCS_BI_TFS);
449 return 0;
450}
451
452/* caller asserts req->pending (ep0 irq status nyet cleared); starts
453 * ep0 data stage. these chips want very simple state transitions.
454 */
455static inline
Philipp Zabel7a857622008-06-22 23:36:39 +0100456void ep0start(struct pxa25x_udc *dev, u32 flags, const char *tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
458 UDCCS0 = flags|UDCCS0_SA|UDCCS0_OPR;
459 USIR0 = USIR0_IR0;
460 dev->req_pending = 0;
461 DBG(DBG_VERY_NOISY, "%s %s, %02x/%02x\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800462 __func__, tag, UDCCS0, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463}
464
465static int
Philipp Zabel7a857622008-06-22 23:36:39 +0100466write_ep0_fifo (struct pxa25x_ep *ep, struct pxa25x_request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
468 unsigned count;
469 int is_short;
470
471 count = write_packet(&UDDR0, req, EP0_FIFO_SIZE);
472 ep->dev->stats.write.bytes += count;
473
474 /* last packet "must be" short (or a zlp) */
475 is_short = (count != EP0_FIFO_SIZE);
476
477 DBG(DBG_VERY_NOISY, "ep0in %d bytes %d left %p\n", count,
478 req->req.length - req->req.actual, req);
479
480 if (unlikely (is_short)) {
481 if (ep->dev->req_pending)
482 ep0start(ep->dev, UDCCS0_IPR, "short IN");
483 else
484 UDCCS0 = UDCCS0_IPR;
485
486 count = req->req.length;
487 done (ep, req, 0);
488 ep0_idle(ep->dev);
Milan Svoboda043ea182006-05-29 03:34:00 -0700489#ifndef CONFIG_ARCH_IXP4XX
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490#if 1
491 /* This seems to get rid of lost status irqs in some cases:
492 * host responds quickly, or next request involves config
493 * change automagic, or should have been hidden, or ...
494 *
495 * FIXME get rid of all udelays possible...
496 */
497 if (count >= EP0_FIFO_SIZE) {
498 count = 100;
499 do {
500 if ((UDCCS0 & UDCCS0_OPR) != 0) {
501 /* clear OPR, generate ack */
502 UDCCS0 = UDCCS0_OPR;
503 break;
504 }
505 count--;
506 udelay(1);
507 } while (count);
508 }
509#endif
Milan Svoboda043ea182006-05-29 03:34:00 -0700510#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 } else if (ep->dev->req_pending)
512 ep0start(ep->dev, 0, "IN");
513 return is_short;
514}
515
516
517/*
518 * read_fifo - unload packet(s) from the fifo we use for usb OUT
519 * transfers and put them into the request. caller should have made
520 * sure there's at least one packet ready.
521 *
522 * returns true if the request completed because of short packet or the
523 * request buffer having filled (and maybe overran till end-of-packet).
524 */
525static int
Philipp Zabel7a857622008-06-22 23:36:39 +0100526read_fifo (struct pxa25x_ep *ep, struct pxa25x_request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
528 for (;;) {
529 u32 udccs;
530 u8 *buf;
531 unsigned bufferspace, count, is_short;
532
533 /* make sure there's a packet in the FIFO.
534 * UDCCS_{BO,IO}_RPC are all the same bit value.
535 * UDCCS_{BO,IO}_RNE are all the same bit value.
536 */
537 udccs = *ep->reg_udccs;
538 if (unlikely ((udccs & UDCCS_BO_RPC) == 0))
539 break;
540 buf = req->req.buf + req->req.actual;
541 prefetchw(buf);
542 bufferspace = req->req.length - req->req.actual;
543
544 /* read all bytes from this packet */
545 if (likely (udccs & UDCCS_BO_RNE)) {
546 count = 1 + (0x0ff & *ep->reg_ubcr);
547 req->req.actual += min (count, bufferspace);
548 } else /* zlp */
549 count = 0;
550 is_short = (count < ep->ep.maxpacket);
551 DBG(DBG_VERY_NOISY, "read %s %02x, %d bytes%s req %p %d/%d\n",
552 ep->ep.name, udccs, count,
553 is_short ? "/S" : "",
554 req, req->req.actual, req->req.length);
555 while (likely (count-- != 0)) {
556 u8 byte = (u8) *ep->reg_uddr;
557
558 if (unlikely (bufferspace == 0)) {
559 /* this happens when the driver's buffer
560 * is smaller than what the host sent.
561 * discard the extra data.
562 */
563 if (req->req.status != -EOVERFLOW)
564 DMSG("%s overflow %d\n",
565 ep->ep.name, count);
566 req->req.status = -EOVERFLOW;
567 } else {
568 *buf++ = byte;
569 bufferspace--;
570 }
571 }
572 *ep->reg_udccs = UDCCS_BO_RPC;
573 /* RPC/RSP/RNE could now reflect the other packet buffer */
574
575 /* iso is one request per packet */
576 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
577 if (udccs & UDCCS_IO_ROF)
578 req->req.status = -EHOSTUNREACH;
579 /* more like "is_done" */
580 is_short = 1;
581 }
582
583 /* completion */
584 if (is_short || req->req.actual == req->req.length) {
585 done (ep, req, 0);
586 if (list_empty(&ep->queue))
587 pio_irq_disable (ep->bEndpointAddress);
588 return 1;
589 }
590
591 /* finished that packet. the next one may be waiting... */
592 }
593 return 0;
594}
595
596/*
597 * special ep0 version of the above. no UBCR0 or double buffering; status
598 * handshaking is magic. most device protocols don't need control-OUT.
599 * CDC vendor commands (and RNDIS), mass storage CB/CBI, and some other
600 * protocols do use them.
601 */
602static int
Philipp Zabel7a857622008-06-22 23:36:39 +0100603read_ep0_fifo (struct pxa25x_ep *ep, struct pxa25x_request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
605 u8 *buf, byte;
606 unsigned bufferspace;
607
608 buf = req->req.buf + req->req.actual;
609 bufferspace = req->req.length - req->req.actual;
610
611 while (UDCCS0 & UDCCS0_RNE) {
612 byte = (u8) UDDR0;
613
614 if (unlikely (bufferspace == 0)) {
615 /* this happens when the driver's buffer
616 * is smaller than what the host sent.
617 * discard the extra data.
618 */
619 if (req->req.status != -EOVERFLOW)
620 DMSG("%s overflow\n", ep->ep.name);
621 req->req.status = -EOVERFLOW;
622 } else {
623 *buf++ = byte;
624 req->req.actual++;
625 bufferspace--;
626 }
627 }
628
629 UDCCS0 = UDCCS0_OPR | UDCCS0_IPR;
630
631 /* completion */
632 if (req->req.actual >= req->req.length)
633 return 1;
634
635 /* finished that packet. the next one may be waiting... */
636 return 0;
637}
638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639/*-------------------------------------------------------------------------*/
640
641static int
Philipp Zabel7a857622008-06-22 23:36:39 +0100642pxa25x_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
Philipp Zabel7a857622008-06-22 23:36:39 +0100644 struct pxa25x_request *req;
645 struct pxa25x_ep *ep;
646 struct pxa25x_udc *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 unsigned long flags;
648
Philipp Zabel7a857622008-06-22 23:36:39 +0100649 req = container_of(_req, struct pxa25x_request, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 if (unlikely (!_req || !_req->complete || !_req->buf
651 || !list_empty(&req->queue))) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800652 DMSG("%s, bad params\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 return -EINVAL;
654 }
655
Philipp Zabel7a857622008-06-22 23:36:39 +0100656 ep = container_of(_ep, struct pxa25x_ep, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 if (unlikely (!_ep || (!ep->desc && ep->ep.name != ep0name))) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800658 DMSG("%s, bad ep\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 return -EINVAL;
660 }
661
662 dev = ep->dev;
663 if (unlikely (!dev->driver
664 || dev->gadget.speed == USB_SPEED_UNKNOWN)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800665 DMSG("%s, bogus device state\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 return -ESHUTDOWN;
667 }
668
669 /* iso is always one packet per request, that's the only way
670 * we can report per-packet status. that also helps with dma.
671 */
672 if (unlikely (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700673 && req->req.length > usb_endpoint_maxp (ep->desc)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 return -EMSGSIZE;
675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 DBG(DBG_NOISY, "%s queue req %p, len %d buf %p\n",
David Brownellad8c6232007-06-30 06:30:04 -0700677 _ep->name, _req, _req->length, _req->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
679 local_irq_save(flags);
680
681 _req->status = -EINPROGRESS;
682 _req->actual = 0;
683
684 /* kickstart this i/o queue? */
685 if (list_empty(&ep->queue) && !ep->stopped) {
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -0800686 if (ep->desc == NULL/* ep0 */) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 unsigned length = _req->length;
688
689 switch (dev->ep0state) {
690 case EP0_IN_DATA_PHASE:
691 dev->stats.write.ops++;
692 if (write_ep0_fifo(ep, req))
693 req = NULL;
694 break;
695
696 case EP0_OUT_DATA_PHASE:
697 dev->stats.read.ops++;
698 /* messy ... */
699 if (dev->req_config) {
700 DBG(DBG_VERBOSE, "ep0 config ack%s\n",
701 dev->has_cfr ? "" : " raced");
702 if (dev->has_cfr)
703 UDCCFR = UDCCFR_AREN|UDCCFR_ACM
704 |UDCCFR_MB1;
705 done(ep, req, 0);
706 dev->ep0state = EP0_END_XFER;
707 local_irq_restore (flags);
708 return 0;
709 }
710 if (dev->req_pending)
711 ep0start(dev, UDCCS0_IPR, "OUT");
712 if (length == 0 || ((UDCCS0 & UDCCS0_RNE) != 0
713 && read_ep0_fifo(ep, req))) {
714 ep0_idle(dev);
715 done(ep, req, 0);
716 req = NULL;
717 }
718 break;
719
720 default:
721 DMSG("ep0 i/o, odd state %d\n", dev->ep0state);
722 local_irq_restore (flags);
723 return -EL2HLT;
724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 /* can the FIFO can satisfy the request immediately? */
David Brownell91987692005-05-07 13:20:19 -0700726 } else if ((ep->bEndpointAddress & USB_DIR_IN) != 0) {
727 if ((*ep->reg_udccs & UDCCS_BI_TFS) != 0
728 && write_fifo(ep, req))
729 req = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 } else if ((*ep->reg_udccs & UDCCS_BO_RFS) != 0
731 && read_fifo(ep, req)) {
732 req = NULL;
733 }
734
David Brownellad8c6232007-06-30 06:30:04 -0700735 if (likely (req && ep->desc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 pio_irq_enable(ep->bEndpointAddress);
737 }
738
739 /* pio or dma irq handler advances the queue. */
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -0800740 if (likely(req != NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 list_add_tail(&req->queue, &ep->queue);
742 local_irq_restore(flags);
743
744 return 0;
745}
746
747
748/*
David Brownell34ebcd22007-02-24 12:23:52 -0800749 * nuke - dequeue ALL requests
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 */
Philipp Zabel7a857622008-06-22 23:36:39 +0100751static void nuke(struct pxa25x_ep *ep, int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
Philipp Zabel7a857622008-06-22 23:36:39 +0100753 struct pxa25x_request *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
755 /* called with irqs blocked */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 while (!list_empty(&ep->queue)) {
757 req = list_entry(ep->queue.next,
Philipp Zabel7a857622008-06-22 23:36:39 +0100758 struct pxa25x_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 queue);
760 done(ep, req, status);
761 }
762 if (ep->desc)
763 pio_irq_disable (ep->bEndpointAddress);
764}
765
766
767/* dequeue JUST ONE request */
Philipp Zabel7a857622008-06-22 23:36:39 +0100768static int pxa25x_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
Philipp Zabel7a857622008-06-22 23:36:39 +0100770 struct pxa25x_ep *ep;
771 struct pxa25x_request *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 unsigned long flags;
773
Philipp Zabel7a857622008-06-22 23:36:39 +0100774 ep = container_of(_ep, struct pxa25x_ep, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 if (!_ep || ep->ep.name == ep0name)
776 return -EINVAL;
777
778 local_irq_save(flags);
779
780 /* make sure it's actually queued on this endpoint */
781 list_for_each_entry (req, &ep->queue, queue) {
782 if (&req->req == _req)
783 break;
784 }
785 if (&req->req != _req) {
786 local_irq_restore(flags);
787 return -EINVAL;
788 }
789
David Brownellad8c6232007-06-30 06:30:04 -0700790 done(ep, req, -ECONNRESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 local_irq_restore(flags);
793 return 0;
794}
795
796/*-------------------------------------------------------------------------*/
797
Philipp Zabel7a857622008-06-22 23:36:39 +0100798static int pxa25x_ep_set_halt(struct usb_ep *_ep, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
Philipp Zabel7a857622008-06-22 23:36:39 +0100800 struct pxa25x_ep *ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 unsigned long flags;
802
Philipp Zabel7a857622008-06-22 23:36:39 +0100803 ep = container_of(_ep, struct pxa25x_ep, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 if (unlikely (!_ep
805 || (!ep->desc && ep->ep.name != ep0name))
806 || ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800807 DMSG("%s, bad ep\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 return -EINVAL;
809 }
810 if (value == 0) {
811 /* this path (reset toggle+halt) is needed to implement
812 * SET_INTERFACE on normal hardware. but it can't be
813 * done from software on the PXA UDC, and the hardware
814 * forgets to do it as part of SET_INTERFACE automagic.
815 */
816 DMSG("only host can clear %s halt\n", _ep->name);
817 return -EROFS;
818 }
819
820 local_irq_save(flags);
821
822 if ((ep->bEndpointAddress & USB_DIR_IN) != 0
823 && ((*ep->reg_udccs & UDCCS_BI_TFS) == 0
824 || !list_empty(&ep->queue))) {
825 local_irq_restore(flags);
826 return -EAGAIN;
827 }
828
829 /* FST bit is the same for control, bulk in, bulk out, interrupt in */
830 *ep->reg_udccs = UDCCS_BI_FST|UDCCS_BI_FTF;
831
832 /* ep0 needs special care */
833 if (!ep->desc) {
834 start_watchdog(ep->dev);
835 ep->dev->req_pending = 0;
836 ep->dev->ep0state = EP0_STALL;
837
David Brownell34ebcd22007-02-24 12:23:52 -0800838 /* and bulk/intr endpoints like dropping stalls too */
839 } else {
840 unsigned i;
841 for (i = 0; i < 1000; i += 20) {
842 if (*ep->reg_udccs & UDCCS_BI_SST)
843 break;
844 udelay(20);
845 }
846 }
847 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 DBG(DBG_VERBOSE, "%s halt\n", _ep->name);
850 return 0;
851}
852
Philipp Zabel7a857622008-06-22 23:36:39 +0100853static int pxa25x_ep_fifo_status(struct usb_ep *_ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
Philipp Zabel7a857622008-06-22 23:36:39 +0100855 struct pxa25x_ep *ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Philipp Zabel7a857622008-06-22 23:36:39 +0100857 ep = container_of(_ep, struct pxa25x_ep, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 if (!_ep) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800859 DMSG("%s, bad ep\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 return -ENODEV;
861 }
862 /* pxa can't report unclaimed bytes from IN fifos */
863 if ((ep->bEndpointAddress & USB_DIR_IN) != 0)
864 return -EOPNOTSUPP;
865 if (ep->dev->gadget.speed == USB_SPEED_UNKNOWN
866 || (*ep->reg_udccs & UDCCS_BO_RFS) == 0)
867 return 0;
868 else
869 return (*ep->reg_ubcr & 0xfff) + 1;
870}
871
Philipp Zabel7a857622008-06-22 23:36:39 +0100872static void pxa25x_ep_fifo_flush(struct usb_ep *_ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
Philipp Zabel7a857622008-06-22 23:36:39 +0100874 struct pxa25x_ep *ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Philipp Zabel7a857622008-06-22 23:36:39 +0100876 ep = container_of(_ep, struct pxa25x_ep, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 if (!_ep || ep->ep.name == ep0name || !list_empty(&ep->queue)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800878 DMSG("%s, bad ep\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 return;
880 }
881
882 /* toggle and halt bits stay unchanged */
883
884 /* for OUT, just read and discard the FIFO contents. */
885 if ((ep->bEndpointAddress & USB_DIR_IN) == 0) {
886 while (((*ep->reg_udccs) & UDCCS_BO_RNE) != 0)
887 (void) *ep->reg_uddr;
888 return;
889 }
890
891 /* most IN status is the same, but ISO can't stall */
892 *ep->reg_udccs = UDCCS_BI_TPC|UDCCS_BI_FTF|UDCCS_BI_TUR
Roel Kluin22eb36f2009-02-19 11:57:46 +0100893 | (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
894 ? 0 : UDCCS_BI_SST);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895}
896
897
Philipp Zabel7a857622008-06-22 23:36:39 +0100898static struct usb_ep_ops pxa25x_ep_ops = {
899 .enable = pxa25x_ep_enable,
900 .disable = pxa25x_ep_disable,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
Philipp Zabel7a857622008-06-22 23:36:39 +0100902 .alloc_request = pxa25x_ep_alloc_request,
903 .free_request = pxa25x_ep_free_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Philipp Zabel7a857622008-06-22 23:36:39 +0100905 .queue = pxa25x_ep_queue,
906 .dequeue = pxa25x_ep_dequeue,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Philipp Zabel7a857622008-06-22 23:36:39 +0100908 .set_halt = pxa25x_ep_set_halt,
909 .fifo_status = pxa25x_ep_fifo_status,
910 .fifo_flush = pxa25x_ep_fifo_flush,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911};
912
913
914/* ---------------------------------------------------------------------------
David Brownell34ebcd22007-02-24 12:23:52 -0800915 * device-scoped parts of the api to the usb controller hardware
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 * ---------------------------------------------------------------------------
917 */
918
Philipp Zabel7a857622008-06-22 23:36:39 +0100919static int pxa25x_udc_get_frame(struct usb_gadget *_gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
921 return ((UFNRH & 0x07) << 8) | (UFNRL & 0xff);
922}
923
Philipp Zabel7a857622008-06-22 23:36:39 +0100924static int pxa25x_udc_wakeup(struct usb_gadget *_gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
926 /* host may not have enabled remote wakeup */
927 if ((UDCCS0 & UDCCS0_DRWF) == 0)
928 return -EHOSTUNREACH;
929 udc_set_mask_UDCCR(UDCCR_RSM);
930 return 0;
931}
932
Philipp Zabel7a857622008-06-22 23:36:39 +0100933static void stop_activity(struct pxa25x_udc *, struct usb_gadget_driver *);
934static void udc_enable (struct pxa25x_udc *);
935static void udc_disable(struct pxa25x_udc *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
937/* We disable the UDC -- and its 48 MHz clock -- whenever it's not
David Brownell34ebcd22007-02-24 12:23:52 -0800938 * in active use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 */
Philipp Zabel7a857622008-06-22 23:36:39 +0100940static int pullup(struct pxa25x_udc *udc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
Dmitry Baryshkov64cc2dd2008-02-22 17:17:19 -0800942 int is_active = udc->vbus && udc->pullup && !udc->suspended;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 DMSG("%s\n", is_active ? "active" : "inactive");
Dmitry Baryshkov64cc2dd2008-02-22 17:17:19 -0800944 if (is_active) {
945 if (!udc->active) {
946 udc->active = 1;
947 /* Enable clock for USB device */
948 clk_enable(udc->clk);
949 udc_enable(udc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 }
Dmitry Baryshkov64cc2dd2008-02-22 17:17:19 -0800951 } else {
952 if (udc->active) {
953 if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
954 DMSG("disconnect %s\n", udc->driver
955 ? udc->driver->driver.name
956 : "(no driver)");
957 stop_activity(udc, udc->driver);
958 }
959 udc_disable(udc);
960 /* Disable clock for USB device */
961 clk_disable(udc->clk);
962 udc->active = 0;
963 }
964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 }
966 return 0;
967}
968
969/* VBUS reporting logically comes from a transceiver */
Philipp Zabel7a857622008-06-22 23:36:39 +0100970static int pxa25x_udc_vbus_session(struct usb_gadget *_gadget, int is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
Philipp Zabel7a857622008-06-22 23:36:39 +0100972 struct pxa25x_udc *udc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Philipp Zabel7a857622008-06-22 23:36:39 +0100974 udc = container_of(_gadget, struct pxa25x_udc, gadget);
Jaya Kumar47fd6f72008-11-18 02:32:36 +0100975 udc->vbus = is_active;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 DMSG("vbus %s\n", is_active ? "supplied" : "inactive");
Dmitry Baryshkov64cc2dd2008-02-22 17:17:19 -0800977 pullup(udc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 return 0;
979}
980
981/* drivers may have software control over D+ pullup */
Philipp Zabel7a857622008-06-22 23:36:39 +0100982static int pxa25x_udc_pullup(struct usb_gadget *_gadget, int is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983{
Philipp Zabel7a857622008-06-22 23:36:39 +0100984 struct pxa25x_udc *udc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Philipp Zabel7a857622008-06-22 23:36:39 +0100986 udc = container_of(_gadget, struct pxa25x_udc, gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
988 /* not all boards support pullup control */
Philipp Zabel56a075d2009-07-01 03:42:45 -0700989 if (!gpio_is_valid(udc->mach->gpio_pullup) && !udc->mach->udc_command)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 return -EOPNOTSUPP;
991
Dmitry Baryshkov64cc2dd2008-02-22 17:17:19 -0800992 udc->pullup = (is_active != 0);
993 pullup(udc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 return 0;
995}
996
Philipp Zabelab26d202009-07-01 03:46:25 -0700997/* boards may consume current from VBUS, up to 100-500mA based on config.
998 * the 500uA suspend ceiling means that exclusively vbus-powered PXA designs
999 * violate USB specs.
1000 */
1001static int pxa25x_udc_vbus_draw(struct usb_gadget *_gadget, unsigned mA)
1002{
1003 struct pxa25x_udc *udc;
1004
1005 udc = container_of(_gadget, struct pxa25x_udc, gadget);
1006
1007 if (udc->transceiver)
1008 return otg_set_power(udc->transceiver, mA);
1009 return -EOPNOTSUPP;
1010}
1011
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001012static int pxa25x_start(struct usb_gadget_driver *driver,
1013 int (*bind)(struct usb_gadget *));
1014static int pxa25x_stop(struct usb_gadget_driver *driver);
1015
Philipp Zabel7a857622008-06-22 23:36:39 +01001016static const struct usb_gadget_ops pxa25x_udc_ops = {
1017 .get_frame = pxa25x_udc_get_frame,
1018 .wakeup = pxa25x_udc_wakeup,
1019 .vbus_session = pxa25x_udc_vbus_session,
1020 .pullup = pxa25x_udc_pullup,
Philipp Zabelab26d202009-07-01 03:46:25 -07001021 .vbus_draw = pxa25x_udc_vbus_draw,
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001022 .start = pxa25x_start,
1023 .stop = pxa25x_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024};
1025
1026/*-------------------------------------------------------------------------*/
1027
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -08001028#ifdef CONFIG_USB_GADGET_DEBUG_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
1030static int
Dmitry Baryshkov64cc2dd2008-02-22 17:17:19 -08001031udc_seq_show(struct seq_file *m, void *_d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032{
Philipp Zabel7a857622008-06-22 23:36:39 +01001033 struct pxa25x_udc *dev = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 unsigned long flags;
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -08001035 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 u32 tmp;
1037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 local_irq_save(flags);
1039
1040 /* basic device status */
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -08001041 seq_printf(m, DRIVER_DESC "\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 "%s version: %s\nGadget driver: %s\nHost %s\n\n",
David Brownellad8c6232007-06-30 06:30:04 -07001043 driver_name, DRIVER_VERSION SIZE_STR "(pio)",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 dev->driver ? dev->driver->driver.name : "(none)",
Dmitry Eremin-Solenikova8ecc862011-02-14 15:33:19 +03001045 dev->gadget.speed == USB_SPEED_FULL ? "full speed" : "disconnected");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
1047 /* registers for device and ep0 */
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -08001048 seq_printf(m,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 "uicr %02X.%02X, usir %02X.%02x, ufnr %02X.%02X\n",
1050 UICR1, UICR0, USIR1, USIR0, UFNRH, UFNRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
1052 tmp = UDCCR;
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -08001053 seq_printf(m,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 "udccr %02X =%s%s%s%s%s%s%s%s\n", tmp,
1055 (tmp & UDCCR_REM) ? " rem" : "",
1056 (tmp & UDCCR_RSTIR) ? " rstir" : "",
1057 (tmp & UDCCR_SRM) ? " srm" : "",
1058 (tmp & UDCCR_SUSIR) ? " susir" : "",
1059 (tmp & UDCCR_RESIR) ? " resir" : "",
1060 (tmp & UDCCR_RSM) ? " rsm" : "",
1061 (tmp & UDCCR_UDA) ? " uda" : "",
1062 (tmp & UDCCR_UDE) ? " ude" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
1064 tmp = UDCCS0;
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -08001065 seq_printf(m,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 "udccs0 %02X =%s%s%s%s%s%s%s%s\n", tmp,
1067 (tmp & UDCCS0_SA) ? " sa" : "",
1068 (tmp & UDCCS0_RNE) ? " rne" : "",
1069 (tmp & UDCCS0_FST) ? " fst" : "",
1070 (tmp & UDCCS0_SST) ? " sst" : "",
1071 (tmp & UDCCS0_DRWF) ? " dwrf" : "",
1072 (tmp & UDCCS0_FTF) ? " ftf" : "",
1073 (tmp & UDCCS0_IPR) ? " ipr" : "",
1074 (tmp & UDCCS0_OPR) ? " opr" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
1076 if (dev->has_cfr) {
1077 tmp = UDCCFR;
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -08001078 seq_printf(m,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 "udccfr %02X =%s%s\n", tmp,
1080 (tmp & UDCCFR_AREN) ? " aren" : "",
1081 (tmp & UDCCFR_ACM) ? " acm" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 }
1083
Dmitry Eremin-Solenikova8ecc862011-02-14 15:33:19 +03001084 if (dev->gadget.speed != USB_SPEED_FULL || !dev->driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 goto done;
1086
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -08001087 seq_printf(m, "ep0 IN %lu/%lu, OUT %lu/%lu\nirqs %lu\n\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 dev->stats.write.bytes, dev->stats.write.ops,
1089 dev->stats.read.bytes, dev->stats.read.ops,
1090 dev->stats.irqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
1092 /* dump endpoint queues */
1093 for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
Philipp Zabel7a857622008-06-22 23:36:39 +01001094 struct pxa25x_ep *ep = &dev->ep [i];
1095 struct pxa25x_request *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
1097 if (i != 0) {
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -08001098 const struct usb_endpoint_descriptor *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -08001100 desc = ep->desc;
1101 if (!desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 continue;
1103 tmp = *dev->ep [i].reg_udccs;
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -08001104 seq_printf(m,
David Brownellad8c6232007-06-30 06:30:04 -07001105 "%s max %d %s udccs %02x irqs %lu\n",
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07001106 ep->ep.name, usb_endpoint_maxp(desc),
David Brownellad8c6232007-06-30 06:30:04 -07001107 "pio", tmp, ep->pio_irqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 /* TODO translate all five groups of udccs bits! */
1109
1110 } else /* ep0 should only have one transfer queued */
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -08001111 seq_printf(m, "ep0 max 16 pio irqs %lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 ep->pio_irqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
1114 if (list_empty(&ep->queue)) {
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -08001115 seq_printf(m, "\t(nothing queued)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 continue;
1117 }
1118 list_for_each_entry(req, &ep->queue, queue) {
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -08001119 seq_printf(m,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 "\treq %p len %d/%d buf %p\n",
1121 &req->req, req->req.actual,
1122 req->req.length, req->req.buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 }
1124 }
1125
1126done:
1127 local_irq_restore(flags);
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -08001128 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129}
1130
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -08001131static int
1132udc_debugfs_open(struct inode *inode, struct file *file)
1133{
1134 return single_open(file, udc_seq_show, inode->i_private);
1135}
1136
1137static const struct file_operations debug_fops = {
1138 .open = udc_debugfs_open,
1139 .read = seq_read,
1140 .llseek = seq_lseek,
1141 .release = single_release,
1142 .owner = THIS_MODULE,
1143};
1144
1145#define create_debug_files(dev) \
1146 do { \
1147 dev->debugfs_udc = debugfs_create_file(dev->gadget.name, \
1148 S_IRUGO, NULL, dev, &debug_fops); \
1149 } while (0)
1150#define remove_debug_files(dev) \
1151 do { \
1152 if (dev->debugfs_udc) \
1153 debugfs_remove(dev->debugfs_udc); \
1154 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
1156#else /* !CONFIG_USB_GADGET_DEBUG_FILES */
1157
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -08001158#define create_debug_files(dev) do {} while (0)
1159#define remove_debug_files(dev) do {} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
1161#endif /* CONFIG_USB_GADGET_DEBUG_FILES */
1162
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163/*-------------------------------------------------------------------------*/
1164
1165/*
David Brownell34ebcd22007-02-24 12:23:52 -08001166 * udc_disable - disable USB device controller
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 */
Philipp Zabel7a857622008-06-22 23:36:39 +01001168static void udc_disable(struct pxa25x_udc *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169{
1170 /* block all irqs */
1171 udc_set_mask_UDCCR(UDCCR_SRM|UDCCR_REM);
1172 UICR0 = UICR1 = 0xff;
1173 UFNRH = UFNRH_SIM;
1174
1175 /* if hardware supports it, disconnect from usb */
David Brownell91987692005-05-07 13:20:19 -07001176 pullup_off();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
1178 udc_clear_mask_UDCCR(UDCCR_UDE);
1179
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 ep0_idle (dev);
1181 dev->gadget.speed = USB_SPEED_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182}
1183
1184
1185/*
David Brownell34ebcd22007-02-24 12:23:52 -08001186 * udc_reinit - initialize software state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 */
Philipp Zabel7a857622008-06-22 23:36:39 +01001188static void udc_reinit(struct pxa25x_udc *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189{
1190 u32 i;
1191
1192 /* device/ep0 records init */
1193 INIT_LIST_HEAD (&dev->gadget.ep_list);
1194 INIT_LIST_HEAD (&dev->gadget.ep0->ep_list);
1195 dev->ep0state = EP0_IDLE;
1196
1197 /* basic endpoint records init */
1198 for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
Philipp Zabel7a857622008-06-22 23:36:39 +01001199 struct pxa25x_ep *ep = &dev->ep[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
1201 if (i != 0)
1202 list_add_tail (&ep->ep.ep_list, &dev->gadget.ep_list);
1203
1204 ep->desc = NULL;
1205 ep->stopped = 0;
1206 INIT_LIST_HEAD (&ep->queue);
David Brownellad8c6232007-06-30 06:30:04 -07001207 ep->pio_irqs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 }
1209
1210 /* the rest was statically initialized, and is read-only */
1211}
1212
1213/* until it's enabled, this UDC should be completely invisible
1214 * to any USB host.
1215 */
Philipp Zabel7a857622008-06-22 23:36:39 +01001216static void udc_enable (struct pxa25x_udc *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217{
1218 udc_clear_mask_UDCCR(UDCCR_UDE);
1219
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 /* try to clear these bits before we enable the udc */
1221 udc_ack_int_UDCCR(UDCCR_SUSIR|/*UDCCR_RSTIR|*/UDCCR_RESIR);
1222
1223 ep0_idle(dev);
1224 dev->gadget.speed = USB_SPEED_UNKNOWN;
1225 dev->stats.irqs = 0;
1226
1227 /*
1228 * sequence taken from chapter 12.5.10, PXA250 AppProcDevManual:
1229 * - enable UDC
1230 * - if RESET is already in progress, ack interrupt
1231 * - unmask reset interrupt
1232 */
1233 udc_set_mask_UDCCR(UDCCR_UDE);
1234 if (!(UDCCR & UDCCR_UDA))
1235 udc_ack_int_UDCCR(UDCCR_RSTIR);
1236
1237 if (dev->has_cfr /* UDC_RES2 is defined */) {
1238 /* pxa255 (a0+) can avoid a set_config race that could
1239 * prevent gadget drivers from configuring correctly
1240 */
1241 UDCCFR = UDCCFR_ACM | UDCCFR_MB1;
1242 } else {
1243 /* "USB test mode" for pxa250 errata 40-42 (stepping a0, a1)
1244 * which could result in missing packets and interrupts.
1245 * supposedly one bit per endpoint, controlling whether it
1246 * double buffers or not; ACM/AREN bits fit into the holes.
1247 * zero bits (like USIR0_IRx) disable double buffering.
1248 */
1249 UDC_RES1 = 0x00;
1250 UDC_RES2 = 0x00;
1251 }
1252
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 /* enable suspend/resume and reset irqs */
1254 udc_clear_mask_UDCCR(UDCCR_SRM | UDCCR_REM);
1255
1256 /* enable ep0 irqs */
1257 UICR0 &= ~UICR0_IM0;
1258
1259 /* if hardware supports it, pullup D+ and wait for reset */
David Brownell91987692005-05-07 13:20:19 -07001260 pullup_on();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261}
1262
1263
1264/* when a driver is successfully registered, it will receive
1265 * control requests including set_configuration(), which enables
1266 * non-control requests. then usb traffic follows until a
1267 * disconnect is reported. then a host may connect again, or
1268 * the driver might get unbound.
1269 */
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001270static int pxa25x_start(struct usb_gadget_driver *driver,
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001271 int (*bind)(struct usb_gadget *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272{
Philipp Zabel7a857622008-06-22 23:36:39 +01001273 struct pxa25x_udc *dev = the_controller;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 int retval;
1275
1276 if (!driver
Milan Svoboda7c0642c2006-05-29 03:34:00 -07001277 || driver->speed < USB_SPEED_FULL
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001278 || !bind
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 || !driver->disconnect
1280 || !driver->setup)
1281 return -EINVAL;
1282 if (!dev)
1283 return -ENODEV;
1284 if (dev->driver)
1285 return -EBUSY;
1286
1287 /* first hook up the driver ... */
1288 dev->driver = driver;
1289 dev->gadget.dev.driver = &driver->driver;
1290 dev->pullup = 1;
1291
David Brownell34ebcd22007-02-24 12:23:52 -08001292 retval = device_add (&dev->gadget.dev);
1293 if (retval) {
1294fail:
1295 dev->driver = NULL;
1296 dev->gadget.dev.driver = NULL;
1297 return retval;
1298 }
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001299 retval = bind(&dev->gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 if (retval) {
1301 DMSG("bind to driver %s --> error %d\n",
1302 driver->driver.name, retval);
1303 device_del (&dev->gadget.dev);
David Brownell34ebcd22007-02-24 12:23:52 -08001304 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
1307 /* ... then enable host detection and ep0; and we're ready
1308 * for set_configuration as well as eventual disconnect.
1309 */
1310 DMSG("registered gadget driver '%s'\n", driver->driver.name);
Philipp Zabelab26d202009-07-01 03:46:25 -07001311
1312 /* connect to bus through transceiver */
1313 if (dev->transceiver) {
1314 retval = otg_set_peripheral(dev->transceiver, &dev->gadget);
1315 if (retval) {
1316 DMSG("can't bind to transceiver\n");
1317 if (driver->unbind)
1318 driver->unbind(&dev->gadget);
1319 goto bind_fail;
1320 }
1321 }
1322
Dmitry Baryshkov64cc2dd2008-02-22 17:17:19 -08001323 pullup(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 dump_state(dev);
1325 return 0;
Philipp Zabelab26d202009-07-01 03:46:25 -07001326bind_fail:
1327 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
1330static void
Philipp Zabel7a857622008-06-22 23:36:39 +01001331stop_activity(struct pxa25x_udc *dev, struct usb_gadget_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332{
1333 int i;
1334
1335 /* don't disconnect drivers more than once */
1336 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1337 driver = NULL;
1338 dev->gadget.speed = USB_SPEED_UNKNOWN;
1339
1340 /* prevent new request submissions, kill any outstanding requests */
1341 for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
Philipp Zabel7a857622008-06-22 23:36:39 +01001342 struct pxa25x_ep *ep = &dev->ep[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
1344 ep->stopped = 1;
1345 nuke(ep, -ESHUTDOWN);
1346 }
1347 del_timer_sync(&dev->timer);
1348
1349 /* report disconnect; the driver is already quiesced */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 if (driver)
1351 driver->disconnect(&dev->gadget);
1352
1353 /* re-init driver-visible data structures */
1354 udc_reinit(dev);
1355}
1356
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001357static int pxa25x_stop(struct usb_gadget_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358{
Philipp Zabel7a857622008-06-22 23:36:39 +01001359 struct pxa25x_udc *dev = the_controller;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
1361 if (!dev)
1362 return -ENODEV;
David Brownell6bea4762006-12-05 03:15:33 -08001363 if (!driver || driver != dev->driver || !driver->unbind)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 return -EINVAL;
1365
1366 local_irq_disable();
Dmitry Baryshkov64cc2dd2008-02-22 17:17:19 -08001367 dev->pullup = 0;
1368 pullup(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 stop_activity(dev, driver);
1370 local_irq_enable();
1371
Philipp Zabelab26d202009-07-01 03:46:25 -07001372 if (dev->transceiver)
1373 (void) otg_set_peripheral(dev->transceiver, NULL);
1374
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 driver->unbind(&dev->gadget);
Patrik Sevalliuseb0be472007-11-20 09:32:00 -08001376 dev->gadget.dev.driver = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 dev->driver = NULL;
1378
1379 device_del (&dev->gadget.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
1381 DMSG("unregistered gadget driver '%s'\n", driver->driver.name);
1382 dump_state(dev);
1383 return 0;
1384}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
1386/*-------------------------------------------------------------------------*/
1387
1388#ifdef CONFIG_ARCH_LUBBOCK
1389
1390/* Lubbock has separate connect and disconnect irqs. More typical designs
1391 * use one GPIO as the VBUS IRQ, and another to control the D+ pullup.
1392 */
1393
1394static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +01001395lubbock_vbus_irq(int irq, void *_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396{
Philipp Zabel7a857622008-06-22 23:36:39 +01001397 struct pxa25x_udc *dev = _dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 int vbus;
1399
1400 dev->stats.irqs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 switch (irq) {
1402 case LUBBOCK_USB_IRQ:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 vbus = 1;
1404 disable_irq(LUBBOCK_USB_IRQ);
1405 enable_irq(LUBBOCK_USB_DISC_IRQ);
1406 break;
1407 case LUBBOCK_USB_DISC_IRQ:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 vbus = 0;
1409 disable_irq(LUBBOCK_USB_DISC_IRQ);
1410 enable_irq(LUBBOCK_USB_IRQ);
1411 break;
1412 default:
1413 return IRQ_NONE;
1414 }
1415
Philipp Zabel7a857622008-06-22 23:36:39 +01001416 pxa25x_udc_vbus_session(&dev->gadget, vbus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 return IRQ_HANDLED;
1418}
1419
1420#endif
1421
1422
1423/*-------------------------------------------------------------------------*/
1424
Philipp Zabel7a857622008-06-22 23:36:39 +01001425static inline void clear_ep_state (struct pxa25x_udc *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426{
1427 unsigned i;
1428
1429 /* hardware SET_{CONFIGURATION,INTERFACE} automagic resets endpoint
1430 * fifos, and pending transactions mustn't be continued in any case.
1431 */
1432 for (i = 1; i < PXA_UDC_NUM_ENDPOINTS; i++)
1433 nuke(&dev->ep[i], -ECONNABORTED);
1434}
1435
1436static void udc_watchdog(unsigned long _dev)
1437{
Philipp Zabel7a857622008-06-22 23:36:39 +01001438 struct pxa25x_udc *dev = (void *)_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
1440 local_irq_disable();
1441 if (dev->ep0state == EP0_STALL
1442 && (UDCCS0 & UDCCS0_FST) == 0
1443 && (UDCCS0 & UDCCS0_SST) == 0) {
1444 UDCCS0 = UDCCS0_FST|UDCCS0_FTF;
1445 DBG(DBG_VERBOSE, "ep0 re-stall\n");
1446 start_watchdog(dev);
1447 }
1448 local_irq_enable();
1449}
1450
Philipp Zabel7a857622008-06-22 23:36:39 +01001451static void handle_ep0 (struct pxa25x_udc *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452{
1453 u32 udccs0 = UDCCS0;
Philipp Zabel7a857622008-06-22 23:36:39 +01001454 struct pxa25x_ep *ep = &dev->ep [0];
1455 struct pxa25x_request *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 union {
1457 struct usb_ctrlrequest r;
1458 u8 raw [8];
1459 u32 word [2];
1460 } u;
1461
1462 if (list_empty(&ep->queue))
1463 req = NULL;
1464 else
Philipp Zabel7a857622008-06-22 23:36:39 +01001465 req = list_entry(ep->queue.next, struct pxa25x_request, queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
1467 /* clear stall status */
1468 if (udccs0 & UDCCS0_SST) {
1469 nuke(ep, -EPIPE);
1470 UDCCS0 = UDCCS0_SST;
1471 del_timer(&dev->timer);
1472 ep0_idle(dev);
1473 }
1474
1475 /* previous request unfinished? non-error iff back-to-back ... */
1476 if ((udccs0 & UDCCS0_SA) != 0 && dev->ep0state != EP0_IDLE) {
1477 nuke(ep, 0);
1478 del_timer(&dev->timer);
1479 ep0_idle(dev);
1480 }
1481
1482 switch (dev->ep0state) {
1483 case EP0_IDLE:
1484 /* late-breaking status? */
1485 udccs0 = UDCCS0;
1486
1487 /* start control request? */
1488 if (likely((udccs0 & (UDCCS0_OPR|UDCCS0_SA|UDCCS0_RNE))
1489 == (UDCCS0_OPR|UDCCS0_SA|UDCCS0_RNE))) {
1490 int i;
1491
1492 nuke (ep, -EPROTO);
1493
1494 /* read SETUP packet */
1495 for (i = 0; i < 8; i++) {
1496 if (unlikely(!(UDCCS0 & UDCCS0_RNE))) {
1497bad_setup:
1498 DMSG("SETUP %d!\n", i);
1499 goto stall;
1500 }
1501 u.raw [i] = (u8) UDDR0;
1502 }
1503 if (unlikely((UDCCS0 & UDCCS0_RNE) != 0))
1504 goto bad_setup;
1505
1506got_setup:
1507 DBG(DBG_VERBOSE, "SETUP %02x.%02x v%04x i%04x l%04x\n",
1508 u.r.bRequestType, u.r.bRequest,
1509 le16_to_cpu(u.r.wValue),
1510 le16_to_cpu(u.r.wIndex),
1511 le16_to_cpu(u.r.wLength));
1512
1513 /* cope with automagic for some standard requests. */
1514 dev->req_std = (u.r.bRequestType & USB_TYPE_MASK)
1515 == USB_TYPE_STANDARD;
1516 dev->req_config = 0;
1517 dev->req_pending = 1;
1518 switch (u.r.bRequest) {
1519 /* hardware restricts gadget drivers here! */
1520 case USB_REQ_SET_CONFIGURATION:
1521 if (u.r.bRequestType == USB_RECIP_DEVICE) {
1522 /* reflect hardware's automagic
1523 * up to the gadget driver.
1524 */
1525config_change:
1526 dev->req_config = 1;
1527 clear_ep_state(dev);
1528 /* if !has_cfr, there's no synch
1529 * else use AREN (later) not SA|OPR
1530 * USIR0_IR0 acts edge sensitive
1531 */
1532 }
1533 break;
1534 /* ... and here, even more ... */
1535 case USB_REQ_SET_INTERFACE:
1536 if (u.r.bRequestType == USB_RECIP_INTERFACE) {
1537 /* udc hardware is broken by design:
1538 * - altsetting may only be zero;
1539 * - hw resets all interfaces' eps;
1540 * - ep reset doesn't include halt(?).
1541 */
1542 DMSG("broken set_interface (%d/%d)\n",
1543 le16_to_cpu(u.r.wIndex),
1544 le16_to_cpu(u.r.wValue));
1545 goto config_change;
1546 }
1547 break;
1548 /* hardware was supposed to hide this */
1549 case USB_REQ_SET_ADDRESS:
1550 if (u.r.bRequestType == USB_RECIP_DEVICE) {
1551 ep0start(dev, 0, "address");
1552 return;
1553 }
1554 break;
1555 }
1556
1557 if (u.r.bRequestType & USB_DIR_IN)
1558 dev->ep0state = EP0_IN_DATA_PHASE;
1559 else
1560 dev->ep0state = EP0_OUT_DATA_PHASE;
1561
1562 i = dev->driver->setup(&dev->gadget, &u.r);
1563 if (i < 0) {
1564 /* hardware automagic preventing STALL... */
1565 if (dev->req_config) {
1566 /* hardware sometimes neglects to tell
1567 * tell us about config change events,
1568 * so later ones may fail...
1569 */
Arjan van de Venb6c63932008-07-25 01:45:52 -07001570 WARNING("config change %02x fail %d?\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 u.r.bRequest, i);
1572 return;
1573 /* TODO experiment: if has_cfr,
1574 * hardware didn't ACK; maybe we
1575 * could actually STALL!
1576 */
1577 }
1578 DBG(DBG_VERBOSE, "protocol STALL, "
1579 "%02x err %d\n", UDCCS0, i);
1580stall:
1581 /* the watchdog timer helps deal with cases
1582 * where udc seems to clear FST wrongly, and
1583 * then NAKs instead of STALLing.
1584 */
1585 ep0start(dev, UDCCS0_FST|UDCCS0_FTF, "stall");
1586 start_watchdog(dev);
1587 dev->ep0state = EP0_STALL;
1588
1589 /* deferred i/o == no response yet */
1590 } else if (dev->req_pending) {
1591 if (likely(dev->ep0state == EP0_IN_DATA_PHASE
1592 || dev->req_std || u.r.wLength))
1593 ep0start(dev, 0, "defer");
1594 else
1595 ep0start(dev, UDCCS0_IPR, "defer/IPR");
1596 }
1597
1598 /* expect at least one data or status stage irq */
1599 return;
1600
1601 } else if (likely((udccs0 & (UDCCS0_OPR|UDCCS0_SA))
1602 == (UDCCS0_OPR|UDCCS0_SA))) {
1603 unsigned i;
1604
1605 /* pxa210/250 erratum 131 for B0/B1 says RNE lies.
1606 * still observed on a pxa255 a0.
1607 */
1608 DBG(DBG_VERBOSE, "e131\n");
1609 nuke(ep, -EPROTO);
1610
1611 /* read SETUP data, but don't trust it too much */
1612 for (i = 0; i < 8; i++)
1613 u.raw [i] = (u8) UDDR0;
1614 if ((u.r.bRequestType & USB_RECIP_MASK)
1615 > USB_RECIP_OTHER)
1616 goto stall;
1617 if (u.word [0] == 0 && u.word [1] == 0)
1618 goto stall;
1619 goto got_setup;
1620 } else {
1621 /* some random early IRQ:
1622 * - we acked FST
1623 * - IPR cleared
1624 * - OPR got set, without SA (likely status stage)
1625 */
1626 UDCCS0 = udccs0 & (UDCCS0_SA|UDCCS0_OPR);
1627 }
1628 break;
1629 case EP0_IN_DATA_PHASE: /* GET_DESCRIPTOR etc */
1630 if (udccs0 & UDCCS0_OPR) {
1631 UDCCS0 = UDCCS0_OPR|UDCCS0_FTF;
1632 DBG(DBG_VERBOSE, "ep0in premature status\n");
1633 if (req)
1634 done(ep, req, 0);
1635 ep0_idle(dev);
1636 } else /* irq was IPR clearing */ {
1637 if (req) {
1638 /* this IN packet might finish the request */
1639 (void) write_ep0_fifo(ep, req);
1640 } /* else IN token before response was written */
1641 }
1642 break;
1643 case EP0_OUT_DATA_PHASE: /* SET_DESCRIPTOR etc */
1644 if (udccs0 & UDCCS0_OPR) {
1645 if (req) {
1646 /* this OUT packet might finish the request */
1647 if (read_ep0_fifo(ep, req))
1648 done(ep, req, 0);
1649 /* else more OUT packets expected */
1650 } /* else OUT token before read was issued */
1651 } else /* irq was IPR clearing */ {
1652 DBG(DBG_VERBOSE, "ep0out premature status\n");
1653 if (req)
1654 done(ep, req, 0);
1655 ep0_idle(dev);
1656 }
1657 break;
1658 case EP0_END_XFER:
1659 if (req)
1660 done(ep, req, 0);
1661 /* ack control-IN status (maybe in-zlp was skipped)
1662 * also appears after some config change events.
1663 */
1664 if (udccs0 & UDCCS0_OPR)
1665 UDCCS0 = UDCCS0_OPR;
1666 ep0_idle(dev);
1667 break;
1668 case EP0_STALL:
1669 UDCCS0 = UDCCS0_FST;
1670 break;
1671 }
1672 USIR0 = USIR0_IR0;
1673}
1674
Philipp Zabel7a857622008-06-22 23:36:39 +01001675static void handle_ep(struct pxa25x_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676{
Philipp Zabel7a857622008-06-22 23:36:39 +01001677 struct pxa25x_request *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 int is_in = ep->bEndpointAddress & USB_DIR_IN;
1679 int completed;
1680 u32 udccs, tmp;
1681
1682 do {
1683 completed = 0;
1684 if (likely (!list_empty(&ep->queue)))
1685 req = list_entry(ep->queue.next,
Philipp Zabel7a857622008-06-22 23:36:39 +01001686 struct pxa25x_request, queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 else
1688 req = NULL;
1689
1690 // TODO check FST handling
1691
1692 udccs = *ep->reg_udccs;
1693 if (unlikely(is_in)) { /* irq from TPC, SST, or (ISO) TUR */
1694 tmp = UDCCS_BI_TUR;
1695 if (likely(ep->bmAttributes == USB_ENDPOINT_XFER_BULK))
1696 tmp |= UDCCS_BI_SST;
1697 tmp &= udccs;
1698 if (likely (tmp))
1699 *ep->reg_udccs = tmp;
1700 if (req && likely ((udccs & UDCCS_BI_TFS) != 0))
1701 completed = write_fifo(ep, req);
1702
1703 } else { /* irq from RPC (or for ISO, ROF) */
1704 if (likely(ep->bmAttributes == USB_ENDPOINT_XFER_BULK))
1705 tmp = UDCCS_BO_SST | UDCCS_BO_DME;
1706 else
1707 tmp = UDCCS_IO_ROF | UDCCS_IO_DME;
1708 tmp &= udccs;
1709 if (likely(tmp))
1710 *ep->reg_udccs = tmp;
1711
1712 /* fifos can hold packets, ready for reading... */
1713 if (likely(req)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 completed = read_fifo(ep, req);
1715 } else
1716 pio_irq_disable (ep->bEndpointAddress);
1717 }
1718 ep->pio_irqs++;
1719 } while (completed);
1720}
1721
1722/*
Philipp Zabel7a857622008-06-22 23:36:39 +01001723 * pxa25x_udc_irq - interrupt handler
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 *
1725 * avoid delays in ep0 processing. the control handshaking isn't always
1726 * under software control (pxa250c0 and the pxa255 are better), and delays
1727 * could cause usb protocol errors.
1728 */
1729static irqreturn_t
Philipp Zabel7a857622008-06-22 23:36:39 +01001730pxa25x_udc_irq(int irq, void *_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731{
Philipp Zabel7a857622008-06-22 23:36:39 +01001732 struct pxa25x_udc *dev = _dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 int handled;
1734
1735 dev->stats.irqs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 do {
1737 u32 udccr = UDCCR;
1738
1739 handled = 0;
1740
1741 /* SUSpend Interrupt Request */
1742 if (unlikely(udccr & UDCCR_SUSIR)) {
1743 udc_ack_int_UDCCR(UDCCR_SUSIR);
1744 handled = 1;
Dmitry Eremin-Solenikova8ecc862011-02-14 15:33:19 +03001745 DBG(DBG_VERBOSE, "USB suspend\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
Dmitry Eremin-Solenikova8ecc862011-02-14 15:33:19 +03001747 if (dev->gadget.speed != USB_SPEED_UNKNOWN
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 && dev->driver
1749 && dev->driver->suspend)
1750 dev->driver->suspend(&dev->gadget);
1751 ep0_idle (dev);
1752 }
1753
1754 /* RESume Interrupt Request */
1755 if (unlikely(udccr & UDCCR_RESIR)) {
1756 udc_ack_int_UDCCR(UDCCR_RESIR);
1757 handled = 1;
1758 DBG(DBG_VERBOSE, "USB resume\n");
1759
1760 if (dev->gadget.speed != USB_SPEED_UNKNOWN
1761 && dev->driver
Dmitry Eremin-Solenikova8ecc862011-02-14 15:33:19 +03001762 && dev->driver->resume)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 dev->driver->resume(&dev->gadget);
1764 }
1765
1766 /* ReSeT Interrupt Request - USB reset */
1767 if (unlikely(udccr & UDCCR_RSTIR)) {
1768 udc_ack_int_UDCCR(UDCCR_RSTIR);
1769 handled = 1;
1770
1771 if ((UDCCR & UDCCR_UDA) == 0) {
1772 DBG(DBG_VERBOSE, "USB reset start\n");
1773
1774 /* reset driver and endpoints,
1775 * in case that's not yet done
1776 */
1777 stop_activity (dev, dev->driver);
1778
1779 } else {
1780 DBG(DBG_VERBOSE, "USB reset end\n");
1781 dev->gadget.speed = USB_SPEED_FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 memset(&dev->stats, 0, sizeof dev->stats);
1783 /* driver and endpoints are still reset */
1784 }
1785
1786 } else {
1787 u32 usir0 = USIR0 & ~UICR0;
1788 u32 usir1 = USIR1 & ~UICR1;
1789 int i;
1790
1791 if (unlikely (!usir0 && !usir1))
1792 continue;
1793
1794 DBG(DBG_VERY_NOISY, "irq %02x.%02x\n", usir1, usir0);
1795
1796 /* control traffic */
1797 if (usir0 & USIR0_IR0) {
1798 dev->ep[0].pio_irqs++;
1799 handle_ep0(dev);
1800 handled = 1;
1801 }
1802
1803 /* endpoint data transfers */
1804 for (i = 0; i < 8; i++) {
1805 u32 tmp = 1 << i;
1806
1807 if (i && (usir0 & tmp)) {
1808 handle_ep(&dev->ep[i]);
1809 USIR0 |= tmp;
1810 handled = 1;
1811 }
David Brownell6d845992009-07-01 03:43:58 -07001812#ifndef CONFIG_USB_PXA25X_SMALL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 if (usir1 & tmp) {
1814 handle_ep(&dev->ep[i+8]);
1815 USIR1 |= tmp;
1816 handled = 1;
1817 }
David Brownell6d845992009-07-01 03:43:58 -07001818#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 }
1820 }
1821
1822 /* we could also ask for 1 msec SOF (SIR) interrupts */
1823
1824 } while (handled);
1825 return IRQ_HANDLED;
1826}
1827
1828/*-------------------------------------------------------------------------*/
1829
1830static void nop_release (struct device *dev)
1831{
Kay Sievers7071a3c2008-05-02 06:02:41 +02001832 DMSG("%s %s\n", __func__, dev_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833}
1834
1835/* this uses load-time allocation and initialization (instead of
1836 * doing it at run-time) to save code, eliminate fault paths, and
1837 * be more obviously correct.
1838 */
Philipp Zabel7a857622008-06-22 23:36:39 +01001839static struct pxa25x_udc memory = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 .gadget = {
Philipp Zabel7a857622008-06-22 23:36:39 +01001841 .ops = &pxa25x_udc_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 .ep0 = &memory.ep[0].ep,
1843 .name = driver_name,
1844 .dev = {
Kay Sieversc682b172009-01-06 10:44:42 -08001845 .init_name = "gadget",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 .release = nop_release,
1847 },
1848 },
1849
1850 /* control endpoint */
1851 .ep[0] = {
1852 .ep = {
1853 .name = ep0name,
Philipp Zabel7a857622008-06-22 23:36:39 +01001854 .ops = &pxa25x_ep_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 .maxpacket = EP0_FIFO_SIZE,
1856 },
1857 .dev = &memory,
1858 .reg_udccs = &UDCCS0,
1859 .reg_uddr = &UDDR0,
1860 },
1861
1862 /* first group of endpoints */
1863 .ep[1] = {
1864 .ep = {
1865 .name = "ep1in-bulk",
Philipp Zabel7a857622008-06-22 23:36:39 +01001866 .ops = &pxa25x_ep_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 .maxpacket = BULK_FIFO_SIZE,
1868 },
1869 .dev = &memory,
1870 .fifo_size = BULK_FIFO_SIZE,
1871 .bEndpointAddress = USB_DIR_IN | 1,
1872 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1873 .reg_udccs = &UDCCS1,
1874 .reg_uddr = &UDDR1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 },
1876 .ep[2] = {
1877 .ep = {
1878 .name = "ep2out-bulk",
Philipp Zabel7a857622008-06-22 23:36:39 +01001879 .ops = &pxa25x_ep_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 .maxpacket = BULK_FIFO_SIZE,
1881 },
1882 .dev = &memory,
1883 .fifo_size = BULK_FIFO_SIZE,
1884 .bEndpointAddress = 2,
1885 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1886 .reg_udccs = &UDCCS2,
1887 .reg_ubcr = &UBCR2,
1888 .reg_uddr = &UDDR2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 },
Philipp Zabel7a857622008-06-22 23:36:39 +01001890#ifndef CONFIG_USB_PXA25X_SMALL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 .ep[3] = {
1892 .ep = {
1893 .name = "ep3in-iso",
Philipp Zabel7a857622008-06-22 23:36:39 +01001894 .ops = &pxa25x_ep_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 .maxpacket = ISO_FIFO_SIZE,
1896 },
1897 .dev = &memory,
1898 .fifo_size = ISO_FIFO_SIZE,
1899 .bEndpointAddress = USB_DIR_IN | 3,
1900 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
1901 .reg_udccs = &UDCCS3,
1902 .reg_uddr = &UDDR3,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 },
1904 .ep[4] = {
1905 .ep = {
1906 .name = "ep4out-iso",
Philipp Zabel7a857622008-06-22 23:36:39 +01001907 .ops = &pxa25x_ep_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 .maxpacket = ISO_FIFO_SIZE,
1909 },
1910 .dev = &memory,
1911 .fifo_size = ISO_FIFO_SIZE,
1912 .bEndpointAddress = 4,
1913 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
1914 .reg_udccs = &UDCCS4,
1915 .reg_ubcr = &UBCR4,
1916 .reg_uddr = &UDDR4,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 },
1918 .ep[5] = {
1919 .ep = {
1920 .name = "ep5in-int",
Philipp Zabel7a857622008-06-22 23:36:39 +01001921 .ops = &pxa25x_ep_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 .maxpacket = INT_FIFO_SIZE,
1923 },
1924 .dev = &memory,
1925 .fifo_size = INT_FIFO_SIZE,
1926 .bEndpointAddress = USB_DIR_IN | 5,
1927 .bmAttributes = USB_ENDPOINT_XFER_INT,
1928 .reg_udccs = &UDCCS5,
1929 .reg_uddr = &UDDR5,
1930 },
1931
1932 /* second group of endpoints */
1933 .ep[6] = {
1934 .ep = {
1935 .name = "ep6in-bulk",
Philipp Zabel7a857622008-06-22 23:36:39 +01001936 .ops = &pxa25x_ep_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 .maxpacket = BULK_FIFO_SIZE,
1938 },
1939 .dev = &memory,
1940 .fifo_size = BULK_FIFO_SIZE,
1941 .bEndpointAddress = USB_DIR_IN | 6,
1942 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1943 .reg_udccs = &UDCCS6,
1944 .reg_uddr = &UDDR6,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 },
1946 .ep[7] = {
1947 .ep = {
1948 .name = "ep7out-bulk",
Philipp Zabel7a857622008-06-22 23:36:39 +01001949 .ops = &pxa25x_ep_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 .maxpacket = BULK_FIFO_SIZE,
1951 },
1952 .dev = &memory,
1953 .fifo_size = BULK_FIFO_SIZE,
1954 .bEndpointAddress = 7,
1955 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1956 .reg_udccs = &UDCCS7,
1957 .reg_ubcr = &UBCR7,
1958 .reg_uddr = &UDDR7,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 },
1960 .ep[8] = {
1961 .ep = {
1962 .name = "ep8in-iso",
Philipp Zabel7a857622008-06-22 23:36:39 +01001963 .ops = &pxa25x_ep_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 .maxpacket = ISO_FIFO_SIZE,
1965 },
1966 .dev = &memory,
1967 .fifo_size = ISO_FIFO_SIZE,
1968 .bEndpointAddress = USB_DIR_IN | 8,
1969 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
1970 .reg_udccs = &UDCCS8,
1971 .reg_uddr = &UDDR8,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 },
1973 .ep[9] = {
1974 .ep = {
1975 .name = "ep9out-iso",
Philipp Zabel7a857622008-06-22 23:36:39 +01001976 .ops = &pxa25x_ep_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 .maxpacket = ISO_FIFO_SIZE,
1978 },
1979 .dev = &memory,
1980 .fifo_size = ISO_FIFO_SIZE,
1981 .bEndpointAddress = 9,
1982 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
1983 .reg_udccs = &UDCCS9,
1984 .reg_ubcr = &UBCR9,
1985 .reg_uddr = &UDDR9,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 },
1987 .ep[10] = {
1988 .ep = {
1989 .name = "ep10in-int",
Philipp Zabel7a857622008-06-22 23:36:39 +01001990 .ops = &pxa25x_ep_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 .maxpacket = INT_FIFO_SIZE,
1992 },
1993 .dev = &memory,
1994 .fifo_size = INT_FIFO_SIZE,
1995 .bEndpointAddress = USB_DIR_IN | 10,
1996 .bmAttributes = USB_ENDPOINT_XFER_INT,
1997 .reg_udccs = &UDCCS10,
1998 .reg_uddr = &UDDR10,
1999 },
2000
2001 /* third group of endpoints */
2002 .ep[11] = {
2003 .ep = {
2004 .name = "ep11in-bulk",
Philipp Zabel7a857622008-06-22 23:36:39 +01002005 .ops = &pxa25x_ep_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 .maxpacket = BULK_FIFO_SIZE,
2007 },
2008 .dev = &memory,
2009 .fifo_size = BULK_FIFO_SIZE,
2010 .bEndpointAddress = USB_DIR_IN | 11,
2011 .bmAttributes = USB_ENDPOINT_XFER_BULK,
2012 .reg_udccs = &UDCCS11,
2013 .reg_uddr = &UDDR11,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 },
2015 .ep[12] = {
2016 .ep = {
2017 .name = "ep12out-bulk",
Philipp Zabel7a857622008-06-22 23:36:39 +01002018 .ops = &pxa25x_ep_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 .maxpacket = BULK_FIFO_SIZE,
2020 },
2021 .dev = &memory,
2022 .fifo_size = BULK_FIFO_SIZE,
2023 .bEndpointAddress = 12,
2024 .bmAttributes = USB_ENDPOINT_XFER_BULK,
2025 .reg_udccs = &UDCCS12,
2026 .reg_ubcr = &UBCR12,
2027 .reg_uddr = &UDDR12,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 },
2029 .ep[13] = {
2030 .ep = {
2031 .name = "ep13in-iso",
Philipp Zabel7a857622008-06-22 23:36:39 +01002032 .ops = &pxa25x_ep_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 .maxpacket = ISO_FIFO_SIZE,
2034 },
2035 .dev = &memory,
2036 .fifo_size = ISO_FIFO_SIZE,
2037 .bEndpointAddress = USB_DIR_IN | 13,
2038 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
2039 .reg_udccs = &UDCCS13,
2040 .reg_uddr = &UDDR13,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 },
2042 .ep[14] = {
2043 .ep = {
2044 .name = "ep14out-iso",
Philipp Zabel7a857622008-06-22 23:36:39 +01002045 .ops = &pxa25x_ep_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 .maxpacket = ISO_FIFO_SIZE,
2047 },
2048 .dev = &memory,
2049 .fifo_size = ISO_FIFO_SIZE,
2050 .bEndpointAddress = 14,
2051 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
2052 .reg_udccs = &UDCCS14,
2053 .reg_ubcr = &UBCR14,
2054 .reg_uddr = &UDDR14,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 },
2056 .ep[15] = {
2057 .ep = {
2058 .name = "ep15in-int",
Philipp Zabel7a857622008-06-22 23:36:39 +01002059 .ops = &pxa25x_ep_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 .maxpacket = INT_FIFO_SIZE,
2061 },
2062 .dev = &memory,
2063 .fifo_size = INT_FIFO_SIZE,
2064 .bEndpointAddress = USB_DIR_IN | 15,
2065 .bmAttributes = USB_ENDPOINT_XFER_INT,
2066 .reg_udccs = &UDCCS15,
2067 .reg_uddr = &UDDR15,
2068 },
Philipp Zabel7a857622008-06-22 23:36:39 +01002069#endif /* !CONFIG_USB_PXA25X_SMALL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070};
2071
2072#define CP15R0_VENDOR_MASK 0xffffe000
2073
2074#if defined(CONFIG_ARCH_PXA)
2075#define CP15R0_XSCALE_VALUE 0x69052000 /* intel/arm/xscale */
2076
2077#elif defined(CONFIG_ARCH_IXP4XX)
2078#define CP15R0_XSCALE_VALUE 0x69054000 /* intel/arm/ixp4xx */
2079
2080#endif
2081
2082#define CP15R0_PROD_MASK 0x000003f0
2083#define PXA25x 0x00000100 /* and PXA26x */
2084#define PXA210 0x00000120
2085
2086#define CP15R0_REV_MASK 0x0000000f
2087
2088#define CP15R0_PRODREV_MASK (CP15R0_PROD_MASK | CP15R0_REV_MASK)
2089
2090#define PXA255_A0 0x00000106 /* or PXA260_B1 */
2091#define PXA250_C0 0x00000105 /* or PXA26x_B0 */
2092#define PXA250_B2 0x00000104
2093#define PXA250_B1 0x00000103 /* or PXA260_A0 */
2094#define PXA250_B0 0x00000102
2095#define PXA250_A1 0x00000101
2096#define PXA250_A0 0x00000100
2097
2098#define PXA210_C0 0x00000125
2099#define PXA210_B2 0x00000124
2100#define PXA210_B1 0x00000123
2101#define PXA210_B0 0x00000122
2102#define IXP425_A0 0x000001c1
David Brownell827982c2006-11-20 11:38:57 -08002103#define IXP425_B0 0x000001f1
Milan Svoboda043ea182006-05-29 03:34:00 -07002104#define IXP465_AD 0x00000200
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
2106/*
David Brownell34ebcd22007-02-24 12:23:52 -08002107 * probe - binds to the platform device
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 */
Philipp Zabel7a857622008-06-22 23:36:39 +01002109static int __init pxa25x_udc_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110{
Philipp Zabel7a857622008-06-22 23:36:39 +01002111 struct pxa25x_udc *dev = &memory;
Dmitry Eremin-Solenikova8ecc862011-02-14 15:33:19 +03002112 int retval, irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 u32 chiprev;
2114
2115 /* insist on Intel/ARM/XScale */
2116 asm("mrc%? p15, 0, %0, c0, c0" : "=r" (chiprev));
2117 if ((chiprev & CP15R0_VENDOR_MASK) != CP15R0_XSCALE_VALUE) {
David Brownell00274922007-11-19 12:58:36 -08002118 pr_err("%s: not XScale!\n", driver_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 return -ENODEV;
2120 }
2121
2122 /* trigger chiprev-specific logic */
2123 switch (chiprev & CP15R0_PRODREV_MASK) {
2124#if defined(CONFIG_ARCH_PXA)
2125 case PXA255_A0:
2126 dev->has_cfr = 1;
2127 break;
2128 case PXA250_A0:
2129 case PXA250_A1:
2130 /* A0/A1 "not released"; ep 13, 15 unusable */
2131 /* fall through */
2132 case PXA250_B2: case PXA210_B2:
2133 case PXA250_B1: case PXA210_B1:
2134 case PXA250_B0: case PXA210_B0:
David Brownellad8c6232007-06-30 06:30:04 -07002135 /* OUT-DMA is broken ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 /* fall through */
2137 case PXA250_C0: case PXA210_C0:
2138 break;
2139#elif defined(CONFIG_ARCH_IXP4XX)
2140 case IXP425_A0:
David Brownell827982c2006-11-20 11:38:57 -08002141 case IXP425_B0:
Milan Svoboda043ea182006-05-29 03:34:00 -07002142 case IXP465_AD:
2143 dev->has_cfr = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 break;
2145#endif
2146 default:
David Brownell00274922007-11-19 12:58:36 -08002147 pr_err("%s: unrecognized processor: %08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 driver_name, chiprev);
2149 /* iop3xx, ixp4xx, ... */
2150 return -ENODEV;
2151 }
2152
David Brownell34ebcd22007-02-24 12:23:52 -08002153 irq = platform_get_irq(pdev, 0);
2154 if (irq < 0)
2155 return -ENODEV;
2156
Russell Kinge0d8b132008-11-11 17:52:32 +00002157 dev->clk = clk_get(&pdev->dev, NULL);
Russell King6549e6c2007-08-20 10:33:35 +01002158 if (IS_ERR(dev->clk)) {
2159 retval = PTR_ERR(dev->clk);
2160 goto err_clk;
2161 }
Russell King6549e6c2007-08-20 10:33:35 +01002162
David Brownellad8c6232007-06-30 06:30:04 -07002163 pr_debug("%s: IRQ %d%s%s\n", driver_name, irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 dev->has_cfr ? "" : " (!cfr)",
David Brownellad8c6232007-06-30 06:30:04 -07002165 SIZE_STR "(pio)"
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 );
2167
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 /* other non-static parts of init */
Russell King3ae5eae2005-11-09 22:32:44 +00002169 dev->dev = &pdev->dev;
2170 dev->mach = pdev->dev.platform_data;
Milan Svoboda9068a4c2007-06-30 06:25:35 -07002171
Philipp Zabelab26d202009-07-01 03:46:25 -07002172 dev->transceiver = otg_get_transceiver();
2173
Philipp Zabel56a075d2009-07-01 03:42:45 -07002174 if (gpio_is_valid(dev->mach->gpio_pullup)) {
Milan Svoboda9068a4c2007-06-30 06:25:35 -07002175 if ((retval = gpio_request(dev->mach->gpio_pullup,
Philipp Zabel7a857622008-06-22 23:36:39 +01002176 "pca25x_udc GPIO PULLUP"))) {
Milan Svoboda9068a4c2007-06-30 06:25:35 -07002177 dev_dbg(&pdev->dev,
2178 "can't get pullup gpio %d, err: %d\n",
2179 dev->mach->gpio_pullup, retval);
Russell King6549e6c2007-08-20 10:33:35 +01002180 goto err_gpio_pullup;
Milan Svoboda9068a4c2007-06-30 06:25:35 -07002181 }
2182 gpio_direction_output(dev->mach->gpio_pullup, 0);
2183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
2185 init_timer(&dev->timer);
2186 dev->timer.function = udc_watchdog;
2187 dev->timer.data = (unsigned long) dev;
2188
2189 device_initialize(&dev->gadget.dev);
Russell King3ae5eae2005-11-09 22:32:44 +00002190 dev->gadget.dev.parent = &pdev->dev;
2191 dev->gadget.dev.dma_mask = pdev->dev.dma_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192
2193 the_controller = dev;
Russell King3ae5eae2005-11-09 22:32:44 +00002194 platform_set_drvdata(pdev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
2196 udc_disable(dev);
2197 udc_reinit(dev);
2198
Dmitry Eremin-Solenikova8ecc862011-02-14 15:33:19 +03002199 dev->vbus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
2201 /* irq setup after old hardware state is cleaned up */
Philipp Zabel7a857622008-06-22 23:36:39 +01002202 retval = request_irq(irq, pxa25x_udc_irq,
Thomas Gleixnerd54b5ca2006-07-01 19:29:44 -07002203 IRQF_DISABLED, driver_name, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 if (retval != 0) {
David Brownell00274922007-11-19 12:58:36 -08002205 pr_err("%s: can't get irq %d, err %d\n",
David Brownell34ebcd22007-02-24 12:23:52 -08002206 driver_name, irq, retval);
Russell King6549e6c2007-08-20 10:33:35 +01002207 goto err_irq1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 }
2209 dev->got_irq = 1;
2210
2211#ifdef CONFIG_ARCH_LUBBOCK
2212 if (machine_is_lubbock()) {
2213 retval = request_irq(LUBBOCK_USB_DISC_IRQ,
2214 lubbock_vbus_irq,
Thomas Gleixnerd54b5ca2006-07-01 19:29:44 -07002215 IRQF_DISABLED | IRQF_SAMPLE_RANDOM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 driver_name, dev);
2217 if (retval != 0) {
David Brownell00274922007-11-19 12:58:36 -08002218 pr_err("%s: can't get irq %i, err %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 driver_name, LUBBOCK_USB_DISC_IRQ, retval);
Russell King6549e6c2007-08-20 10:33:35 +01002220 goto err_irq_lub;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 }
2222 retval = request_irq(LUBBOCK_USB_IRQ,
2223 lubbock_vbus_irq,
Thomas Gleixnerd54b5ca2006-07-01 19:29:44 -07002224 IRQF_DISABLED | IRQF_SAMPLE_RANDOM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 driver_name, dev);
2226 if (retval != 0) {
David Brownell00274922007-11-19 12:58:36 -08002227 pr_err("%s: can't get irq %i, err %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 driver_name, LUBBOCK_USB_IRQ, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 goto lubbock_fail0;
2230 }
David Brownellb2bbb202006-06-29 12:25:39 -07002231 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232#endif
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -08002233 create_debug_files(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03002235 retval = usb_add_gadget_udc(&pdev->dev, &dev->gadget);
2236 if (!retval)
2237 return retval;
Russell King6549e6c2007-08-20 10:33:35 +01002238
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03002239 remove_debug_files(dev);
Russell King6549e6c2007-08-20 10:33:35 +01002240#ifdef CONFIG_ARCH_LUBBOCK
Sebastian Andrzej Siewiora6207b12011-06-03 19:50:46 +02002241lubbock_fail0:
Russell King6549e6c2007-08-20 10:33:35 +01002242 free_irq(LUBBOCK_USB_DISC_IRQ, dev);
2243 err_irq_lub:
Russell King6549e6c2007-08-20 10:33:35 +01002244 free_irq(irq, dev);
Sebastian Andrzej Siewiora6207b12011-06-03 19:50:46 +02002245#endif
Russell King6549e6c2007-08-20 10:33:35 +01002246 err_irq1:
Philipp Zabel56a075d2009-07-01 03:42:45 -07002247 if (gpio_is_valid(dev->mach->gpio_pullup))
Russell King6549e6c2007-08-20 10:33:35 +01002248 gpio_free(dev->mach->gpio_pullup);
2249 err_gpio_pullup:
Philipp Zabelab26d202009-07-01 03:46:25 -07002250 if (dev->transceiver) {
2251 otg_put_transceiver(dev->transceiver);
2252 dev->transceiver = NULL;
2253 }
Russell King6549e6c2007-08-20 10:33:35 +01002254 clk_put(dev->clk);
2255 err_clk:
Russell King6549e6c2007-08-20 10:33:35 +01002256 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257}
David Brownell91987692005-05-07 13:20:19 -07002258
Philipp Zabel7a857622008-06-22 23:36:39 +01002259static void pxa25x_udc_shutdown(struct platform_device *_dev)
David Brownell91987692005-05-07 13:20:19 -07002260{
2261 pullup_off();
2262}
2263
Philipp Zabel7a857622008-06-22 23:36:39 +01002264static int __exit pxa25x_udc_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265{
Philipp Zabel7a857622008-06-22 23:36:39 +01002266 struct pxa25x_udc *dev = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03002268 usb_del_gadget_udc(&dev->gadget);
David Brownell6bea4762006-12-05 03:15:33 -08002269 if (dev->driver)
2270 return -EBUSY;
2271
Dmitry Baryshkov64cc2dd2008-02-22 17:17:19 -08002272 dev->pullup = 0;
2273 pullup(dev);
2274
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -08002275 remove_debug_files(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276
2277 if (dev->got_irq) {
David Brownell34ebcd22007-02-24 12:23:52 -08002278 free_irq(platform_get_irq(pdev, 0), dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 dev->got_irq = 0;
2280 }
Milan Svoboda44df45a2006-05-29 03:34:00 -07002281#ifdef CONFIG_ARCH_LUBBOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 if (machine_is_lubbock()) {
2283 free_irq(LUBBOCK_USB_DISC_IRQ, dev);
2284 free_irq(LUBBOCK_USB_IRQ, dev);
2285 }
Milan Svoboda44df45a2006-05-29 03:34:00 -07002286#endif
Philipp Zabel56a075d2009-07-01 03:42:45 -07002287 if (gpio_is_valid(dev->mach->gpio_pullup))
Milan Svoboda9068a4c2007-06-30 06:25:35 -07002288 gpio_free(dev->mach->gpio_pullup);
2289
Russell King6549e6c2007-08-20 10:33:35 +01002290 clk_put(dev->clk);
Russell King6549e6c2007-08-20 10:33:35 +01002291
Philipp Zabelab26d202009-07-01 03:46:25 -07002292 if (dev->transceiver) {
2293 otg_put_transceiver(dev->transceiver);
2294 dev->transceiver = NULL;
2295 }
2296
Russell King3ae5eae2005-11-09 22:32:44 +00002297 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 the_controller = NULL;
2299 return 0;
2300}
2301
2302/*-------------------------------------------------------------------------*/
2303
2304#ifdef CONFIG_PM
2305
2306/* USB suspend (controlled by the host) and system suspend (controlled
2307 * by the PXA) don't necessarily work well together. If USB is active,
2308 * the 48 MHz clock is required; so the system can't enter 33 MHz idle
2309 * mode, or any deeper PM saving state.
2310 *
2311 * For now, we punt and forcibly disconnect from the USB host when PXA
2312 * enters any suspend state. While we're disconnected, we always disable
David Brownell34ebcd22007-02-24 12:23:52 -08002313 * the 48MHz USB clock ... allowing PXA sleep and/or 33 MHz idle states.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 * Boards without software pullup control shouldn't use those states.
2315 * VBUS IRQs should probably be ignored so that the PXA device just acts
2316 * "dead" to USB hosts until system resume.
2317 */
Philipp Zabel7a857622008-06-22 23:36:39 +01002318static int pxa25x_udc_suspend(struct platform_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319{
Philipp Zabel7a857622008-06-22 23:36:39 +01002320 struct pxa25x_udc *udc = platform_get_drvdata(dev);
Dmitry Baryshkov64cc2dd2008-02-22 17:17:19 -08002321 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
Philipp Zabel56a075d2009-07-01 03:42:45 -07002323 if (!gpio_is_valid(udc->mach->gpio_pullup) && !udc->mach->udc_command)
Arjan van de Venb6c63932008-07-25 01:45:52 -07002324 WARNING("USB host won't detect disconnect!\n");
Dmitry Baryshkov64cc2dd2008-02-22 17:17:19 -08002325 udc->suspended = 1;
2326
2327 local_irq_save(flags);
2328 pullup(udc);
2329 local_irq_restore(flags);
Russell King9480e302005-10-28 09:52:56 -07002330
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 return 0;
2332}
2333
Philipp Zabel7a857622008-06-22 23:36:39 +01002334static int pxa25x_udc_resume(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335{
Philipp Zabel7a857622008-06-22 23:36:39 +01002336 struct pxa25x_udc *udc = platform_get_drvdata(dev);
Dmitry Baryshkov64cc2dd2008-02-22 17:17:19 -08002337 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338
Dmitry Baryshkov64cc2dd2008-02-22 17:17:19 -08002339 udc->suspended = 0;
2340 local_irq_save(flags);
2341 pullup(udc);
2342 local_irq_restore(flags);
Russell King9480e302005-10-28 09:52:56 -07002343
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 return 0;
2345}
2346
2347#else
Philipp Zabel7a857622008-06-22 23:36:39 +01002348#define pxa25x_udc_suspend NULL
2349#define pxa25x_udc_resume NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350#endif
2351
2352/*-------------------------------------------------------------------------*/
2353
Russell King3ae5eae2005-11-09 22:32:44 +00002354static struct platform_driver udc_driver = {
Philipp Zabel7a857622008-06-22 23:36:39 +01002355 .shutdown = pxa25x_udc_shutdown,
2356 .remove = __exit_p(pxa25x_udc_remove),
2357 .suspend = pxa25x_udc_suspend,
2358 .resume = pxa25x_udc_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00002359 .driver = {
2360 .owner = THIS_MODULE,
Philipp Zabel7a857622008-06-22 23:36:39 +01002361 .name = "pxa25x-udc",
Russell King3ae5eae2005-11-09 22:32:44 +00002362 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363};
2364
2365static int __init udc_init(void)
2366{
David Brownell00274922007-11-19 12:58:36 -08002367 pr_info("%s: version %s\n", driver_name, DRIVER_VERSION);
Philipp Zabel7a857622008-06-22 23:36:39 +01002368 return platform_driver_probe(&udc_driver, pxa25x_udc_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369}
2370module_init(udc_init);
2371
2372static void __exit udc_exit(void)
2373{
Russell King3ae5eae2005-11-09 22:32:44 +00002374 platform_driver_unregister(&udc_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375}
2376module_exit(udc_exit);
2377
2378MODULE_DESCRIPTION(DRIVER_DESC);
2379MODULE_AUTHOR("Frank Becker, Robert Schwebel, David Brownell");
2380MODULE_LICENSE("GPL");
Philipp Zabel7a857622008-06-22 23:36:39 +01002381MODULE_ALIAS("platform:pxa25x-udc");