blob: 1b33634f2736b132e835a35de658e8478bb45ea3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
David Brownell91987692005-05-07 13:20:19 -07002 * Intel PXA25x and IXP4xx on-chip full speed USB device controllers
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (C) 2002 Intrinsyc, Inc. (Frank Becker)
5 * Copyright (C) 2003 Robert Schwebel, Pengutronix
6 * Copyright (C) 2003 Benedikt Spranger, Pengutronix
7 * Copyright (C) 2003 David Brownell
8 * Copyright (C) 2003 Joshua Wise
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 */
15
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -080016/* #define VERBOSE_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Milan Svoboda9068a4c2007-06-30 06:25:35 -070018#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/module.h>
20#include <linux/kernel.h>
21#include <linux/ioport.h>
22#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/errno.h>
24#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/slab.h>
26#include <linux/init.h>
27#include <linux/timer.h>
28#include <linux/list.h>
29#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/mm.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010031#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/dma-mapping.h>
Nicolas Pitrec7a3bd12006-10-20 14:20:17 -070033#include <linux/irq.h>
Russell King6549e6c2007-08-20 10:33:35 +010034#include <linux/clk.h>
35#include <linux/err.h>
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -080036#include <linux/seq_file.h>
37#include <linux/debugfs.h>
Russell King284d1152008-04-20 17:32:16 +010038#include <linux/io.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -070039#include <linux/prefetch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#include <asm/byteorder.h>
42#include <asm/dma.h>
Milan Svoboda9068a4c2007-06-30 06:25:35 -070043#include <asm/gpio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/system.h>
45#include <asm/mach-types.h>
46#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
David Brownell5f848132006-12-16 15:34:53 -080048#include <linux/usb/ch9.h>
David Brownell9454a572007-10-04 18:05:17 -070049#include <linux/usb/gadget.h>
Philipp Zabelab26d202009-07-01 03:46:25 -070050#include <linux/usb/otg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Russell King284d1152008-04-20 17:32:16 +010052/*
53 * This driver is PXA25x only. Grab the right register definitions.
54 */
55#ifdef CONFIG_ARCH_PXA
Russell Kinga09e64f2008-08-05 16:14:15 +010056#include <mach/pxa25x-udc.h>
Russell King284d1152008-04-20 17:32:16 +010057#endif
58
Eric Miao0dc726b2009-12-27 23:01:25 +080059#ifdef CONFIG_ARCH_LUBBOCK
60#include <mach/lubbock.h>
61#endif
62
Milan Svoboda9068a4c2007-06-30 06:25:35 -070063#include <asm/mach/udc_pxa2xx.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65
66/*
David Brownell91987692005-05-07 13:20:19 -070067 * This driver handles the USB Device Controller (UDC) in Intel's PXA 25x
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 * series processors. The UDC for the IXP 4xx series is very similar.
69 * There are fifteen endpoints, in addition to ep0.
70 *
71 * Such controller drivers work with a gadget driver. The gadget driver
72 * returns descriptors, implements configuration and data protocols used
73 * by the host to interact with this device, and allocates endpoints to
74 * the different protocol interfaces. The controller driver virtualizes
75 * usb hardware so that the gadget drivers will be more portable.
David Brownell34ebcd22007-02-24 12:23:52 -080076 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 * This UDC hardware wants to implement a bit too much USB protocol, so
78 * it constrains the sorts of USB configuration change events that work.
79 * The errata for these chips are misleading; some "fixed" bugs from
80 * pxa250 a0/a1 b0/b1/b2 sure act like they're still there.
David Brownellad8c6232007-06-30 06:30:04 -070081 *
82 * Note that the UDC hardware supports DMA (except on IXP) but that's
83 * not used here. IN-DMA (to host) is simple enough, when the data is
84 * suitably aligned (16 bytes) ... the network stack doesn't do that,
85 * other software can. OUT-DMA is buggy in most chip versions, as well
86 * as poorly designed (data toggle not automatic). So this driver won't
87 * bother using DMA. (Mostly-working IN-DMA support was available in
88 * kernels before 2.6.23, but was never enabled or well tested.)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 */
90
David Brownellad8c6232007-06-30 06:30:04 -070091#define DRIVER_VERSION "30-June-2007"
David Brownell91987692005-05-07 13:20:19 -070092#define DRIVER_DESC "PXA 25x USB Device Controller driver"
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94
Philipp Zabel7a857622008-06-22 23:36:39 +010095static const char driver_name [] = "pxa25x_udc";
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97static const char ep0name [] = "ep0";
98
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100#ifdef CONFIG_ARCH_IXP4XX
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102/* cpu-specific register addresses are compiled in to this code */
103#ifdef CONFIG_ARCH_PXA
104#error "Can't configure both IXP and PXA"
105#endif
106
Dmitry Baryshkov64cc2dd2008-02-22 17:17:19 -0800107/* IXP doesn't yet support <linux/clk.h> */
108#define clk_get(dev,name) NULL
109#define clk_enable(clk) do { } while (0)
110#define clk_disable(clk) do { } while (0)
111#define clk_put(clk) do { } while (0)
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113#endif
114
Philipp Zabel7a857622008-06-22 23:36:39 +0100115#include "pxa25x_udc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117
Philipp Zabel7a857622008-06-22 23:36:39 +0100118#ifdef CONFIG_USB_PXA25X_SMALL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119#define SIZE_STR " (small)"
120#else
121#define SIZE_STR ""
122#endif
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124/* ---------------------------------------------------------------------------
David Brownell34ebcd22007-02-24 12:23:52 -0800125 * endpoint related parts of the api to the usb controller hardware,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 * used by gadget driver; and the inner talker-to-hardware core.
127 * ---------------------------------------------------------------------------
128 */
129
Philipp Zabel7a857622008-06-22 23:36:39 +0100130static void pxa25x_ep_fifo_flush (struct usb_ep *ep);
131static void nuke (struct pxa25x_ep *, int status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
David Brownellb2bbb202006-06-29 12:25:39 -0700133/* one GPIO should control a D+ pullup, so host sees this device (or not) */
134static void pullup_off(void)
135{
136 struct pxa2xx_udc_mach_info *mach = the_controller->mach;
Ian Molton8fb105f2008-06-25 22:34:51 +0100137 int off_level = mach->gpio_pullup_inverted;
David Brownellb2bbb202006-06-29 12:25:39 -0700138
Philipp Zabel56a075d2009-07-01 03:42:45 -0700139 if (gpio_is_valid(mach->gpio_pullup))
Ian Molton8fb105f2008-06-25 22:34:51 +0100140 gpio_set_value(mach->gpio_pullup, off_level);
David Brownellb2bbb202006-06-29 12:25:39 -0700141 else if (mach->udc_command)
142 mach->udc_command(PXA2XX_UDC_CMD_DISCONNECT);
143}
144
145static void pullup_on(void)
146{
147 struct pxa2xx_udc_mach_info *mach = the_controller->mach;
Ian Molton8fb105f2008-06-25 22:34:51 +0100148 int on_level = !mach->gpio_pullup_inverted;
David Brownellb2bbb202006-06-29 12:25:39 -0700149
Philipp Zabel56a075d2009-07-01 03:42:45 -0700150 if (gpio_is_valid(mach->gpio_pullup))
Ian Molton8fb105f2008-06-25 22:34:51 +0100151 gpio_set_value(mach->gpio_pullup, on_level);
David Brownellb2bbb202006-06-29 12:25:39 -0700152 else if (mach->udc_command)
153 mach->udc_command(PXA2XX_UDC_CMD_CONNECT);
154}
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156static void pio_irq_enable(int bEndpointAddress)
157{
158 bEndpointAddress &= 0xf;
159 if (bEndpointAddress < 8)
160 UICR0 &= ~(1 << bEndpointAddress);
161 else {
162 bEndpointAddress -= 8;
163 UICR1 &= ~(1 << bEndpointAddress);
164 }
165}
166
167static void pio_irq_disable(int bEndpointAddress)
168{
169 bEndpointAddress &= 0xf;
170 if (bEndpointAddress < 8)
171 UICR0 |= 1 << bEndpointAddress;
172 else {
173 bEndpointAddress -= 8;
174 UICR1 |= 1 << bEndpointAddress;
175 }
176}
177
178/* The UDCCR reg contains mask and interrupt status bits,
179 * so using '|=' isn't safe as it may ack an interrupt.
180 */
181#define UDCCR_MASK_BITS (UDCCR_REM | UDCCR_SRM | UDCCR_UDE)
182
183static inline void udc_set_mask_UDCCR(int mask)
184{
185 UDCCR = (UDCCR & UDCCR_MASK_BITS) | (mask & UDCCR_MASK_BITS);
186}
187
188static inline void udc_clear_mask_UDCCR(int mask)
189{
190 UDCCR = (UDCCR & UDCCR_MASK_BITS) & ~(mask & UDCCR_MASK_BITS);
191}
192
193static inline void udc_ack_int_UDCCR(int mask)
194{
195 /* udccr contains the bits we dont want to change */
196 __u32 udccr = UDCCR & UDCCR_MASK_BITS;
197
198 UDCCR = udccr | (mask & ~UDCCR_MASK_BITS);
199}
200
201/*
202 * endpoint enable/disable
203 *
Philipp Zabel7a857622008-06-22 23:36:39 +0100204 * we need to verify the descriptors used to enable endpoints. since pxa25x
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 * endpoint configurations are fixed, and are pretty much always enabled,
206 * there's not a lot to manage here.
207 *
Philipp Zabel7a857622008-06-22 23:36:39 +0100208 * because pxa25x can't selectively initialize bulk (or interrupt) endpoints,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 * (resetting endpoint halt and toggle), SET_INTERFACE is unusable except
210 * for a single interface (with only the default altsetting) and for gadget
211 * drivers that don't halt endpoints (not reset by set_interface). that also
212 * means that if you use ISO, you must violate the USB spec rule that all
213 * iso endpoints must be in non-default altsettings.
214 */
Philipp Zabel7a857622008-06-22 23:36:39 +0100215static int pxa25x_ep_enable (struct usb_ep *_ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 const struct usb_endpoint_descriptor *desc)
217{
Philipp Zabel7a857622008-06-22 23:36:39 +0100218 struct pxa25x_ep *ep;
219 struct pxa25x_udc *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Philipp Zabel7a857622008-06-22 23:36:39 +0100221 ep = container_of (_ep, struct pxa25x_ep, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 if (!_ep || !desc || ep->desc || _ep->name == ep0name
223 || desc->bDescriptorType != USB_DT_ENDPOINT
224 || ep->bEndpointAddress != desc->bEndpointAddress
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700225 || ep->fifo_size < usb_endpoint_maxp (desc)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800226 DMSG("%s, bad ep or descriptor\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 return -EINVAL;
228 }
229
230 /* xfer types must match, except that interrupt ~= bulk */
231 if (ep->bmAttributes != desc->bmAttributes
232 && ep->bmAttributes != USB_ENDPOINT_XFER_BULK
233 && desc->bmAttributes != USB_ENDPOINT_XFER_INT) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800234 DMSG("%s, %s type mismatch\n", __func__, _ep->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 return -EINVAL;
236 }
237
238 /* hardware _could_ do smaller, but driver doesn't */
239 if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700240 && usb_endpoint_maxp (desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 != BULK_FIFO_SIZE)
242 || !desc->wMaxPacketSize) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800243 DMSG("%s, bad %s maxpacket\n", __func__, _ep->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 return -ERANGE;
245 }
246
247 dev = ep->dev;
248 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800249 DMSG("%s, bogus device state\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 return -ESHUTDOWN;
251 }
252
253 ep->desc = desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 ep->stopped = 0;
David Brownellad8c6232007-06-30 06:30:04 -0700255 ep->pio_irqs = 0;
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700256 ep->ep.maxpacket = usb_endpoint_maxp (desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 /* flush fifo (mostly for OUT buffers) */
Philipp Zabel7a857622008-06-22 23:36:39 +0100259 pxa25x_ep_fifo_flush (_ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 /* ... reset halt state too, if we could ... */
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 DBG(DBG_VERBOSE, "enabled %s\n", _ep->name);
264 return 0;
265}
266
Philipp Zabel7a857622008-06-22 23:36:39 +0100267static int pxa25x_ep_disable (struct usb_ep *_ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
Philipp Zabel7a857622008-06-22 23:36:39 +0100269 struct pxa25x_ep *ep;
David Brownell91987692005-05-07 13:20:19 -0700270 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Philipp Zabel7a857622008-06-22 23:36:39 +0100272 ep = container_of (_ep, struct pxa25x_ep, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 if (!_ep || !ep->desc) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800274 DMSG("%s, %s not enabled\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 _ep ? ep->ep.name : NULL);
276 return -EINVAL;
277 }
David Brownell91987692005-05-07 13:20:19 -0700278 local_irq_save(flags);
279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 nuke (ep, -ESHUTDOWN);
281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 /* flush fifo (mostly for IN buffers) */
Philipp Zabel7a857622008-06-22 23:36:39 +0100283 pxa25x_ep_fifo_flush (_ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 ep->desc = NULL;
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +0200286 ep->ep.desc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 ep->stopped = 1;
288
David Brownell91987692005-05-07 13:20:19 -0700289 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 DBG(DBG_VERBOSE, "%s disabled\n", _ep->name);
291 return 0;
292}
293
294/*-------------------------------------------------------------------------*/
295
Philipp Zabel7a857622008-06-22 23:36:39 +0100296/* for the pxa25x, these can just wrap kmalloc/kfree. gadget drivers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 * must still pass correctly initialized endpoints, since other controller
298 * drivers may care about how it's currently set up (dma issues etc).
299 */
300
301/*
Philipp Zabel7a857622008-06-22 23:36:39 +0100302 * pxa25x_ep_alloc_request - allocate a request data structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 */
304static struct usb_request *
Philipp Zabel7a857622008-06-22 23:36:39 +0100305pxa25x_ep_alloc_request (struct usb_ep *_ep, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
Philipp Zabel7a857622008-06-22 23:36:39 +0100307 struct pxa25x_request *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Eric Sesterhenn7039f422006-02-27 13:34:10 -0800309 req = kzalloc(sizeof(*req), gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 if (!req)
311 return NULL;
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 INIT_LIST_HEAD (&req->queue);
314 return &req->req;
315}
316
317
318/*
Philipp Zabel7a857622008-06-22 23:36:39 +0100319 * pxa25x_ep_free_request - deallocate a request data structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 */
321static void
Philipp Zabel7a857622008-06-22 23:36:39 +0100322pxa25x_ep_free_request (struct usb_ep *_ep, struct usb_request *_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
Philipp Zabel7a857622008-06-22 23:36:39 +0100324 struct pxa25x_request *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Philipp Zabel7a857622008-06-22 23:36:39 +0100326 req = container_of (_req, struct pxa25x_request, req);
Arjan van de Venb6c63932008-07-25 01:45:52 -0700327 WARN_ON(!list_empty (&req->queue));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 kfree(req);
329}
330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331/*-------------------------------------------------------------------------*/
332
333/*
334 * done - retire a request; caller blocked irqs
335 */
Philipp Zabel7a857622008-06-22 23:36:39 +0100336static void done(struct pxa25x_ep *ep, struct pxa25x_request *req, int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
338 unsigned stopped = ep->stopped;
339
340 list_del_init(&req->queue);
341
342 if (likely (req->req.status == -EINPROGRESS))
343 req->req.status = status;
344 else
345 status = req->req.status;
346
347 if (status && status != -ESHUTDOWN)
348 DBG(DBG_VERBOSE, "complete %s req %p stat %d len %u/%u\n",
349 ep->ep.name, &req->req, status,
350 req->req.actual, req->req.length);
351
352 /* don't modify queue heads during completion callback */
353 ep->stopped = 1;
354 req->req.complete(&ep->ep, &req->req);
355 ep->stopped = stopped;
356}
357
358
Philipp Zabel7a857622008-06-22 23:36:39 +0100359static inline void ep0_idle (struct pxa25x_udc *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
361 dev->ep0state = EP0_IDLE;
362}
363
364static int
Philipp Zabel7a857622008-06-22 23:36:39 +0100365write_packet(volatile u32 *uddr, struct pxa25x_request *req, unsigned max)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
367 u8 *buf;
368 unsigned length, count;
369
370 buf = req->req.buf + req->req.actual;
371 prefetch(buf);
372
373 /* how big will this packet be? */
374 length = min(req->req.length - req->req.actual, max);
375 req->req.actual += length;
376
377 count = length;
378 while (likely(count--))
379 *uddr = *buf++;
380
381 return length;
382}
383
384/*
385 * write to an IN endpoint fifo, as many packets as possible.
386 * irqs will use this to write the rest later.
387 * caller guarantees at least one packet buffer is ready (or a zlp).
388 */
389static int
Philipp Zabel7a857622008-06-22 23:36:39 +0100390write_fifo (struct pxa25x_ep *ep, struct pxa25x_request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
392 unsigned max;
393
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700394 max = usb_endpoint_maxp(ep->desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 do {
396 unsigned count;
397 int is_last, is_short;
398
399 count = write_packet(ep->reg_uddr, req, max);
400
401 /* last packet is usually short (or a zlp) */
402 if (unlikely (count != max))
403 is_last = is_short = 1;
404 else {
405 if (likely(req->req.length != req->req.actual)
406 || req->req.zero)
407 is_last = 0;
408 else
409 is_last = 1;
410 /* interrupt/iso maxpacket may not fill the fifo */
411 is_short = unlikely (max < ep->fifo_size);
412 }
413
414 DBG(DBG_VERY_NOISY, "wrote %s %d bytes%s%s %d left %p\n",
415 ep->ep.name, count,
416 is_last ? "/L" : "", is_short ? "/S" : "",
417 req->req.length - req->req.actual, req);
418
419 /* let loose that packet. maybe try writing another one,
420 * double buffering might work. TSP, TPC, and TFS
421 * bit values are the same for all normal IN endpoints.
422 */
423 *ep->reg_udccs = UDCCS_BI_TPC;
424 if (is_short)
425 *ep->reg_udccs = UDCCS_BI_TSP;
426
427 /* requests complete when all IN data is in the FIFO */
428 if (is_last) {
429 done (ep, req, 0);
David Brownellad8c6232007-06-30 06:30:04 -0700430 if (list_empty(&ep->queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 pio_irq_disable (ep->bEndpointAddress);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 return 1;
433 }
434
435 // TODO experiment: how robust can fifo mode tweaking be?
436 // double buffering is off in the default fifo mode, which
437 // prevents TFS from being set here.
438
439 } while (*ep->reg_udccs & UDCCS_BI_TFS);
440 return 0;
441}
442
443/* caller asserts req->pending (ep0 irq status nyet cleared); starts
444 * ep0 data stage. these chips want very simple state transitions.
445 */
446static inline
Philipp Zabel7a857622008-06-22 23:36:39 +0100447void ep0start(struct pxa25x_udc *dev, u32 flags, const char *tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
449 UDCCS0 = flags|UDCCS0_SA|UDCCS0_OPR;
450 USIR0 = USIR0_IR0;
451 dev->req_pending = 0;
452 DBG(DBG_VERY_NOISY, "%s %s, %02x/%02x\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800453 __func__, tag, UDCCS0, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
455
456static int
Philipp Zabel7a857622008-06-22 23:36:39 +0100457write_ep0_fifo (struct pxa25x_ep *ep, struct pxa25x_request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
459 unsigned count;
460 int is_short;
461
462 count = write_packet(&UDDR0, req, EP0_FIFO_SIZE);
463 ep->dev->stats.write.bytes += count;
464
465 /* last packet "must be" short (or a zlp) */
466 is_short = (count != EP0_FIFO_SIZE);
467
468 DBG(DBG_VERY_NOISY, "ep0in %d bytes %d left %p\n", count,
469 req->req.length - req->req.actual, req);
470
471 if (unlikely (is_short)) {
472 if (ep->dev->req_pending)
473 ep0start(ep->dev, UDCCS0_IPR, "short IN");
474 else
475 UDCCS0 = UDCCS0_IPR;
476
477 count = req->req.length;
478 done (ep, req, 0);
479 ep0_idle(ep->dev);
Milan Svoboda043ea182006-05-29 03:34:00 -0700480#ifndef CONFIG_ARCH_IXP4XX
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481#if 1
482 /* This seems to get rid of lost status irqs in some cases:
483 * host responds quickly, or next request involves config
484 * change automagic, or should have been hidden, or ...
485 *
486 * FIXME get rid of all udelays possible...
487 */
488 if (count >= EP0_FIFO_SIZE) {
489 count = 100;
490 do {
491 if ((UDCCS0 & UDCCS0_OPR) != 0) {
492 /* clear OPR, generate ack */
493 UDCCS0 = UDCCS0_OPR;
494 break;
495 }
496 count--;
497 udelay(1);
498 } while (count);
499 }
500#endif
Milan Svoboda043ea182006-05-29 03:34:00 -0700501#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 } else if (ep->dev->req_pending)
503 ep0start(ep->dev, 0, "IN");
504 return is_short;
505}
506
507
508/*
509 * read_fifo - unload packet(s) from the fifo we use for usb OUT
510 * transfers and put them into the request. caller should have made
511 * sure there's at least one packet ready.
512 *
513 * returns true if the request completed because of short packet or the
514 * request buffer having filled (and maybe overran till end-of-packet).
515 */
516static int
Philipp Zabel7a857622008-06-22 23:36:39 +0100517read_fifo (struct pxa25x_ep *ep, struct pxa25x_request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
519 for (;;) {
520 u32 udccs;
521 u8 *buf;
522 unsigned bufferspace, count, is_short;
523
524 /* make sure there's a packet in the FIFO.
525 * UDCCS_{BO,IO}_RPC are all the same bit value.
526 * UDCCS_{BO,IO}_RNE are all the same bit value.
527 */
528 udccs = *ep->reg_udccs;
529 if (unlikely ((udccs & UDCCS_BO_RPC) == 0))
530 break;
531 buf = req->req.buf + req->req.actual;
532 prefetchw(buf);
533 bufferspace = req->req.length - req->req.actual;
534
535 /* read all bytes from this packet */
536 if (likely (udccs & UDCCS_BO_RNE)) {
537 count = 1 + (0x0ff & *ep->reg_ubcr);
538 req->req.actual += min (count, bufferspace);
539 } else /* zlp */
540 count = 0;
541 is_short = (count < ep->ep.maxpacket);
542 DBG(DBG_VERY_NOISY, "read %s %02x, %d bytes%s req %p %d/%d\n",
543 ep->ep.name, udccs, count,
544 is_short ? "/S" : "",
545 req, req->req.actual, req->req.length);
546 while (likely (count-- != 0)) {
547 u8 byte = (u8) *ep->reg_uddr;
548
549 if (unlikely (bufferspace == 0)) {
550 /* this happens when the driver's buffer
551 * is smaller than what the host sent.
552 * discard the extra data.
553 */
554 if (req->req.status != -EOVERFLOW)
555 DMSG("%s overflow %d\n",
556 ep->ep.name, count);
557 req->req.status = -EOVERFLOW;
558 } else {
559 *buf++ = byte;
560 bufferspace--;
561 }
562 }
563 *ep->reg_udccs = UDCCS_BO_RPC;
564 /* RPC/RSP/RNE could now reflect the other packet buffer */
565
566 /* iso is one request per packet */
567 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
568 if (udccs & UDCCS_IO_ROF)
569 req->req.status = -EHOSTUNREACH;
570 /* more like "is_done" */
571 is_short = 1;
572 }
573
574 /* completion */
575 if (is_short || req->req.actual == req->req.length) {
576 done (ep, req, 0);
577 if (list_empty(&ep->queue))
578 pio_irq_disable (ep->bEndpointAddress);
579 return 1;
580 }
581
582 /* finished that packet. the next one may be waiting... */
583 }
584 return 0;
585}
586
587/*
588 * special ep0 version of the above. no UBCR0 or double buffering; status
589 * handshaking is magic. most device protocols don't need control-OUT.
590 * CDC vendor commands (and RNDIS), mass storage CB/CBI, and some other
591 * protocols do use them.
592 */
593static int
Philipp Zabel7a857622008-06-22 23:36:39 +0100594read_ep0_fifo (struct pxa25x_ep *ep, struct pxa25x_request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
596 u8 *buf, byte;
597 unsigned bufferspace;
598
599 buf = req->req.buf + req->req.actual;
600 bufferspace = req->req.length - req->req.actual;
601
602 while (UDCCS0 & UDCCS0_RNE) {
603 byte = (u8) UDDR0;
604
605 if (unlikely (bufferspace == 0)) {
606 /* this happens when the driver's buffer
607 * is smaller than what the host sent.
608 * discard the extra data.
609 */
610 if (req->req.status != -EOVERFLOW)
611 DMSG("%s overflow\n", ep->ep.name);
612 req->req.status = -EOVERFLOW;
613 } else {
614 *buf++ = byte;
615 req->req.actual++;
616 bufferspace--;
617 }
618 }
619
620 UDCCS0 = UDCCS0_OPR | UDCCS0_IPR;
621
622 /* completion */
623 if (req->req.actual >= req->req.length)
624 return 1;
625
626 /* finished that packet. the next one may be waiting... */
627 return 0;
628}
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630/*-------------------------------------------------------------------------*/
631
632static int
Philipp Zabel7a857622008-06-22 23:36:39 +0100633pxa25x_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
Philipp Zabel7a857622008-06-22 23:36:39 +0100635 struct pxa25x_request *req;
636 struct pxa25x_ep *ep;
637 struct pxa25x_udc *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 unsigned long flags;
639
Philipp Zabel7a857622008-06-22 23:36:39 +0100640 req = container_of(_req, struct pxa25x_request, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 if (unlikely (!_req || !_req->complete || !_req->buf
642 || !list_empty(&req->queue))) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800643 DMSG("%s, bad params\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 return -EINVAL;
645 }
646
Philipp Zabel7a857622008-06-22 23:36:39 +0100647 ep = container_of(_ep, struct pxa25x_ep, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 if (unlikely (!_ep || (!ep->desc && ep->ep.name != ep0name))) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800649 DMSG("%s, bad ep\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 return -EINVAL;
651 }
652
653 dev = ep->dev;
654 if (unlikely (!dev->driver
655 || dev->gadget.speed == USB_SPEED_UNKNOWN)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800656 DMSG("%s, bogus device state\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 return -ESHUTDOWN;
658 }
659
660 /* iso is always one packet per request, that's the only way
661 * we can report per-packet status. that also helps with dma.
662 */
663 if (unlikely (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700664 && req->req.length > usb_endpoint_maxp (ep->desc)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 return -EMSGSIZE;
666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 DBG(DBG_NOISY, "%s queue req %p, len %d buf %p\n",
David Brownellad8c6232007-06-30 06:30:04 -0700668 _ep->name, _req, _req->length, _req->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670 local_irq_save(flags);
671
672 _req->status = -EINPROGRESS;
673 _req->actual = 0;
674
675 /* kickstart this i/o queue? */
676 if (list_empty(&ep->queue) && !ep->stopped) {
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -0800677 if (ep->desc == NULL/* ep0 */) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 unsigned length = _req->length;
679
680 switch (dev->ep0state) {
681 case EP0_IN_DATA_PHASE:
682 dev->stats.write.ops++;
683 if (write_ep0_fifo(ep, req))
684 req = NULL;
685 break;
686
687 case EP0_OUT_DATA_PHASE:
688 dev->stats.read.ops++;
689 /* messy ... */
690 if (dev->req_config) {
691 DBG(DBG_VERBOSE, "ep0 config ack%s\n",
692 dev->has_cfr ? "" : " raced");
693 if (dev->has_cfr)
694 UDCCFR = UDCCFR_AREN|UDCCFR_ACM
695 |UDCCFR_MB1;
696 done(ep, req, 0);
697 dev->ep0state = EP0_END_XFER;
698 local_irq_restore (flags);
699 return 0;
700 }
701 if (dev->req_pending)
702 ep0start(dev, UDCCS0_IPR, "OUT");
703 if (length == 0 || ((UDCCS0 & UDCCS0_RNE) != 0
704 && read_ep0_fifo(ep, req))) {
705 ep0_idle(dev);
706 done(ep, req, 0);
707 req = NULL;
708 }
709 break;
710
711 default:
712 DMSG("ep0 i/o, odd state %d\n", dev->ep0state);
713 local_irq_restore (flags);
714 return -EL2HLT;
715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 /* can the FIFO can satisfy the request immediately? */
David Brownell91987692005-05-07 13:20:19 -0700717 } else if ((ep->bEndpointAddress & USB_DIR_IN) != 0) {
718 if ((*ep->reg_udccs & UDCCS_BI_TFS) != 0
719 && write_fifo(ep, req))
720 req = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 } else if ((*ep->reg_udccs & UDCCS_BO_RFS) != 0
722 && read_fifo(ep, req)) {
723 req = NULL;
724 }
725
David Brownellad8c6232007-06-30 06:30:04 -0700726 if (likely (req && ep->desc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 pio_irq_enable(ep->bEndpointAddress);
728 }
729
730 /* pio or dma irq handler advances the queue. */
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -0800731 if (likely(req != NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 list_add_tail(&req->queue, &ep->queue);
733 local_irq_restore(flags);
734
735 return 0;
736}
737
738
739/*
David Brownell34ebcd22007-02-24 12:23:52 -0800740 * nuke - dequeue ALL requests
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 */
Philipp Zabel7a857622008-06-22 23:36:39 +0100742static void nuke(struct pxa25x_ep *ep, int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
Philipp Zabel7a857622008-06-22 23:36:39 +0100744 struct pxa25x_request *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746 /* called with irqs blocked */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 while (!list_empty(&ep->queue)) {
748 req = list_entry(ep->queue.next,
Philipp Zabel7a857622008-06-22 23:36:39 +0100749 struct pxa25x_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 queue);
751 done(ep, req, status);
752 }
753 if (ep->desc)
754 pio_irq_disable (ep->bEndpointAddress);
755}
756
757
758/* dequeue JUST ONE request */
Philipp Zabel7a857622008-06-22 23:36:39 +0100759static int pxa25x_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
Philipp Zabel7a857622008-06-22 23:36:39 +0100761 struct pxa25x_ep *ep;
762 struct pxa25x_request *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 unsigned long flags;
764
Philipp Zabel7a857622008-06-22 23:36:39 +0100765 ep = container_of(_ep, struct pxa25x_ep, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 if (!_ep || ep->ep.name == ep0name)
767 return -EINVAL;
768
769 local_irq_save(flags);
770
771 /* make sure it's actually queued on this endpoint */
772 list_for_each_entry (req, &ep->queue, queue) {
773 if (&req->req == _req)
774 break;
775 }
776 if (&req->req != _req) {
777 local_irq_restore(flags);
778 return -EINVAL;
779 }
780
David Brownellad8c6232007-06-30 06:30:04 -0700781 done(ep, req, -ECONNRESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783 local_irq_restore(flags);
784 return 0;
785}
786
787/*-------------------------------------------------------------------------*/
788
Philipp Zabel7a857622008-06-22 23:36:39 +0100789static int pxa25x_ep_set_halt(struct usb_ep *_ep, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
Philipp Zabel7a857622008-06-22 23:36:39 +0100791 struct pxa25x_ep *ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 unsigned long flags;
793
Philipp Zabel7a857622008-06-22 23:36:39 +0100794 ep = container_of(_ep, struct pxa25x_ep, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 if (unlikely (!_ep
796 || (!ep->desc && ep->ep.name != ep0name))
797 || ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800798 DMSG("%s, bad ep\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 return -EINVAL;
800 }
801 if (value == 0) {
802 /* this path (reset toggle+halt) is needed to implement
803 * SET_INTERFACE on normal hardware. but it can't be
804 * done from software on the PXA UDC, and the hardware
805 * forgets to do it as part of SET_INTERFACE automagic.
806 */
807 DMSG("only host can clear %s halt\n", _ep->name);
808 return -EROFS;
809 }
810
811 local_irq_save(flags);
812
813 if ((ep->bEndpointAddress & USB_DIR_IN) != 0
814 && ((*ep->reg_udccs & UDCCS_BI_TFS) == 0
815 || !list_empty(&ep->queue))) {
816 local_irq_restore(flags);
817 return -EAGAIN;
818 }
819
820 /* FST bit is the same for control, bulk in, bulk out, interrupt in */
821 *ep->reg_udccs = UDCCS_BI_FST|UDCCS_BI_FTF;
822
823 /* ep0 needs special care */
824 if (!ep->desc) {
825 start_watchdog(ep->dev);
826 ep->dev->req_pending = 0;
827 ep->dev->ep0state = EP0_STALL;
828
David Brownell34ebcd22007-02-24 12:23:52 -0800829 /* and bulk/intr endpoints like dropping stalls too */
830 } else {
831 unsigned i;
832 for (i = 0; i < 1000; i += 20) {
833 if (*ep->reg_udccs & UDCCS_BI_SST)
834 break;
835 udelay(20);
836 }
837 }
838 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
840 DBG(DBG_VERBOSE, "%s halt\n", _ep->name);
841 return 0;
842}
843
Philipp Zabel7a857622008-06-22 23:36:39 +0100844static int pxa25x_ep_fifo_status(struct usb_ep *_ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
Philipp Zabel7a857622008-06-22 23:36:39 +0100846 struct pxa25x_ep *ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Philipp Zabel7a857622008-06-22 23:36:39 +0100848 ep = container_of(_ep, struct pxa25x_ep, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 if (!_ep) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800850 DMSG("%s, bad ep\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 return -ENODEV;
852 }
853 /* pxa can't report unclaimed bytes from IN fifos */
854 if ((ep->bEndpointAddress & USB_DIR_IN) != 0)
855 return -EOPNOTSUPP;
856 if (ep->dev->gadget.speed == USB_SPEED_UNKNOWN
857 || (*ep->reg_udccs & UDCCS_BO_RFS) == 0)
858 return 0;
859 else
860 return (*ep->reg_ubcr & 0xfff) + 1;
861}
862
Philipp Zabel7a857622008-06-22 23:36:39 +0100863static void pxa25x_ep_fifo_flush(struct usb_ep *_ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
Philipp Zabel7a857622008-06-22 23:36:39 +0100865 struct pxa25x_ep *ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Philipp Zabel7a857622008-06-22 23:36:39 +0100867 ep = container_of(_ep, struct pxa25x_ep, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 if (!_ep || ep->ep.name == ep0name || !list_empty(&ep->queue)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800869 DMSG("%s, bad ep\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 return;
871 }
872
873 /* toggle and halt bits stay unchanged */
874
875 /* for OUT, just read and discard the FIFO contents. */
876 if ((ep->bEndpointAddress & USB_DIR_IN) == 0) {
877 while (((*ep->reg_udccs) & UDCCS_BO_RNE) != 0)
878 (void) *ep->reg_uddr;
879 return;
880 }
881
882 /* most IN status is the same, but ISO can't stall */
883 *ep->reg_udccs = UDCCS_BI_TPC|UDCCS_BI_FTF|UDCCS_BI_TUR
Roel Kluin22eb36f2009-02-19 11:57:46 +0100884 | (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
885 ? 0 : UDCCS_BI_SST);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886}
887
888
Philipp Zabel7a857622008-06-22 23:36:39 +0100889static struct usb_ep_ops pxa25x_ep_ops = {
890 .enable = pxa25x_ep_enable,
891 .disable = pxa25x_ep_disable,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Philipp Zabel7a857622008-06-22 23:36:39 +0100893 .alloc_request = pxa25x_ep_alloc_request,
894 .free_request = pxa25x_ep_free_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Philipp Zabel7a857622008-06-22 23:36:39 +0100896 .queue = pxa25x_ep_queue,
897 .dequeue = pxa25x_ep_dequeue,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Philipp Zabel7a857622008-06-22 23:36:39 +0100899 .set_halt = pxa25x_ep_set_halt,
900 .fifo_status = pxa25x_ep_fifo_status,
901 .fifo_flush = pxa25x_ep_fifo_flush,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902};
903
904
905/* ---------------------------------------------------------------------------
David Brownell34ebcd22007-02-24 12:23:52 -0800906 * device-scoped parts of the api to the usb controller hardware
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 * ---------------------------------------------------------------------------
908 */
909
Philipp Zabel7a857622008-06-22 23:36:39 +0100910static int pxa25x_udc_get_frame(struct usb_gadget *_gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911{
912 return ((UFNRH & 0x07) << 8) | (UFNRL & 0xff);
913}
914
Philipp Zabel7a857622008-06-22 23:36:39 +0100915static int pxa25x_udc_wakeup(struct usb_gadget *_gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
917 /* host may not have enabled remote wakeup */
918 if ((UDCCS0 & UDCCS0_DRWF) == 0)
919 return -EHOSTUNREACH;
920 udc_set_mask_UDCCR(UDCCR_RSM);
921 return 0;
922}
923
Philipp Zabel7a857622008-06-22 23:36:39 +0100924static void stop_activity(struct pxa25x_udc *, struct usb_gadget_driver *);
925static void udc_enable (struct pxa25x_udc *);
926static void udc_disable(struct pxa25x_udc *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
928/* We disable the UDC -- and its 48 MHz clock -- whenever it's not
David Brownell34ebcd22007-02-24 12:23:52 -0800929 * in active use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 */
Philipp Zabel7a857622008-06-22 23:36:39 +0100931static int pullup(struct pxa25x_udc *udc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932{
Dmitry Baryshkov64cc2dd2008-02-22 17:17:19 -0800933 int is_active = udc->vbus && udc->pullup && !udc->suspended;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 DMSG("%s\n", is_active ? "active" : "inactive");
Dmitry Baryshkov64cc2dd2008-02-22 17:17:19 -0800935 if (is_active) {
936 if (!udc->active) {
937 udc->active = 1;
938 /* Enable clock for USB device */
939 clk_enable(udc->clk);
940 udc_enable(udc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 }
Dmitry Baryshkov64cc2dd2008-02-22 17:17:19 -0800942 } else {
943 if (udc->active) {
944 if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
945 DMSG("disconnect %s\n", udc->driver
946 ? udc->driver->driver.name
947 : "(no driver)");
948 stop_activity(udc, udc->driver);
949 }
950 udc_disable(udc);
951 /* Disable clock for USB device */
952 clk_disable(udc->clk);
953 udc->active = 0;
954 }
955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 }
957 return 0;
958}
959
960/* VBUS reporting logically comes from a transceiver */
Philipp Zabel7a857622008-06-22 23:36:39 +0100961static int pxa25x_udc_vbus_session(struct usb_gadget *_gadget, int is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962{
Philipp Zabel7a857622008-06-22 23:36:39 +0100963 struct pxa25x_udc *udc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Philipp Zabel7a857622008-06-22 23:36:39 +0100965 udc = container_of(_gadget, struct pxa25x_udc, gadget);
Jaya Kumar47fd6f72008-11-18 02:32:36 +0100966 udc->vbus = is_active;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 DMSG("vbus %s\n", is_active ? "supplied" : "inactive");
Dmitry Baryshkov64cc2dd2008-02-22 17:17:19 -0800968 pullup(udc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 return 0;
970}
971
972/* drivers may have software control over D+ pullup */
Philipp Zabel7a857622008-06-22 23:36:39 +0100973static int pxa25x_udc_pullup(struct usb_gadget *_gadget, int is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974{
Philipp Zabel7a857622008-06-22 23:36:39 +0100975 struct pxa25x_udc *udc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Philipp Zabel7a857622008-06-22 23:36:39 +0100977 udc = container_of(_gadget, struct pxa25x_udc, gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
979 /* not all boards support pullup control */
Philipp Zabel56a075d2009-07-01 03:42:45 -0700980 if (!gpio_is_valid(udc->mach->gpio_pullup) && !udc->mach->udc_command)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 return -EOPNOTSUPP;
982
Dmitry Baryshkov64cc2dd2008-02-22 17:17:19 -0800983 udc->pullup = (is_active != 0);
984 pullup(udc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 return 0;
986}
987
Philipp Zabelab26d202009-07-01 03:46:25 -0700988/* boards may consume current from VBUS, up to 100-500mA based on config.
989 * the 500uA suspend ceiling means that exclusively vbus-powered PXA designs
990 * violate USB specs.
991 */
992static int pxa25x_udc_vbus_draw(struct usb_gadget *_gadget, unsigned mA)
993{
994 struct pxa25x_udc *udc;
995
996 udc = container_of(_gadget, struct pxa25x_udc, gadget);
997
998 if (udc->transceiver)
Heikki Krogerusb96d3b02012-02-13 13:24:18 +0200999 return usb_phy_set_power(udc->transceiver, mA);
Philipp Zabelab26d202009-07-01 03:46:25 -07001000 return -EOPNOTSUPP;
1001}
1002
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001003static int pxa25x_start(struct usb_gadget_driver *driver,
1004 int (*bind)(struct usb_gadget *));
1005static int pxa25x_stop(struct usb_gadget_driver *driver);
1006
Philipp Zabel7a857622008-06-22 23:36:39 +01001007static const struct usb_gadget_ops pxa25x_udc_ops = {
1008 .get_frame = pxa25x_udc_get_frame,
1009 .wakeup = pxa25x_udc_wakeup,
1010 .vbus_session = pxa25x_udc_vbus_session,
1011 .pullup = pxa25x_udc_pullup,
Philipp Zabelab26d202009-07-01 03:46:25 -07001012 .vbus_draw = pxa25x_udc_vbus_draw,
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001013 .start = pxa25x_start,
1014 .stop = pxa25x_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015};
1016
1017/*-------------------------------------------------------------------------*/
1018
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -08001019#ifdef CONFIG_USB_GADGET_DEBUG_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
1021static int
Dmitry Baryshkov64cc2dd2008-02-22 17:17:19 -08001022udc_seq_show(struct seq_file *m, void *_d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023{
Philipp Zabel7a857622008-06-22 23:36:39 +01001024 struct pxa25x_udc *dev = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 unsigned long flags;
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -08001026 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 u32 tmp;
1028
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 local_irq_save(flags);
1030
1031 /* basic device status */
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -08001032 seq_printf(m, DRIVER_DESC "\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 "%s version: %s\nGadget driver: %s\nHost %s\n\n",
David Brownellad8c6232007-06-30 06:30:04 -07001034 driver_name, DRIVER_VERSION SIZE_STR "(pio)",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 dev->driver ? dev->driver->driver.name : "(none)",
Dmitry Eremin-Solenikova8ecc862011-02-14 15:33:19 +03001036 dev->gadget.speed == USB_SPEED_FULL ? "full speed" : "disconnected");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
1038 /* registers for device and ep0 */
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -08001039 seq_printf(m,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 "uicr %02X.%02X, usir %02X.%02x, ufnr %02X.%02X\n",
1041 UICR1, UICR0, USIR1, USIR0, UFNRH, UFNRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
1043 tmp = UDCCR;
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -08001044 seq_printf(m,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 "udccr %02X =%s%s%s%s%s%s%s%s\n", tmp,
1046 (tmp & UDCCR_REM) ? " rem" : "",
1047 (tmp & UDCCR_RSTIR) ? " rstir" : "",
1048 (tmp & UDCCR_SRM) ? " srm" : "",
1049 (tmp & UDCCR_SUSIR) ? " susir" : "",
1050 (tmp & UDCCR_RESIR) ? " resir" : "",
1051 (tmp & UDCCR_RSM) ? " rsm" : "",
1052 (tmp & UDCCR_UDA) ? " uda" : "",
1053 (tmp & UDCCR_UDE) ? " ude" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
1055 tmp = UDCCS0;
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -08001056 seq_printf(m,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 "udccs0 %02X =%s%s%s%s%s%s%s%s\n", tmp,
1058 (tmp & UDCCS0_SA) ? " sa" : "",
1059 (tmp & UDCCS0_RNE) ? " rne" : "",
1060 (tmp & UDCCS0_FST) ? " fst" : "",
1061 (tmp & UDCCS0_SST) ? " sst" : "",
1062 (tmp & UDCCS0_DRWF) ? " dwrf" : "",
1063 (tmp & UDCCS0_FTF) ? " ftf" : "",
1064 (tmp & UDCCS0_IPR) ? " ipr" : "",
1065 (tmp & UDCCS0_OPR) ? " opr" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
1067 if (dev->has_cfr) {
1068 tmp = UDCCFR;
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -08001069 seq_printf(m,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 "udccfr %02X =%s%s\n", tmp,
1071 (tmp & UDCCFR_AREN) ? " aren" : "",
1072 (tmp & UDCCFR_ACM) ? " acm" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 }
1074
Dmitry Eremin-Solenikova8ecc862011-02-14 15:33:19 +03001075 if (dev->gadget.speed != USB_SPEED_FULL || !dev->driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 goto done;
1077
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -08001078 seq_printf(m, "ep0 IN %lu/%lu, OUT %lu/%lu\nirqs %lu\n\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 dev->stats.write.bytes, dev->stats.write.ops,
1080 dev->stats.read.bytes, dev->stats.read.ops,
1081 dev->stats.irqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
1083 /* dump endpoint queues */
1084 for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
Philipp Zabel7a857622008-06-22 23:36:39 +01001085 struct pxa25x_ep *ep = &dev->ep [i];
1086 struct pxa25x_request *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
1088 if (i != 0) {
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -08001089 const struct usb_endpoint_descriptor *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -08001091 desc = ep->desc;
1092 if (!desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 continue;
1094 tmp = *dev->ep [i].reg_udccs;
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -08001095 seq_printf(m,
David Brownellad8c6232007-06-30 06:30:04 -07001096 "%s max %d %s udccs %02x irqs %lu\n",
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07001097 ep->ep.name, usb_endpoint_maxp(desc),
David Brownellad8c6232007-06-30 06:30:04 -07001098 "pio", tmp, ep->pio_irqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 /* TODO translate all five groups of udccs bits! */
1100
1101 } else /* ep0 should only have one transfer queued */
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -08001102 seq_printf(m, "ep0 max 16 pio irqs %lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 ep->pio_irqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
1105 if (list_empty(&ep->queue)) {
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -08001106 seq_printf(m, "\t(nothing queued)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 continue;
1108 }
1109 list_for_each_entry(req, &ep->queue, queue) {
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -08001110 seq_printf(m,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 "\treq %p len %d/%d buf %p\n",
1112 &req->req, req->req.actual,
1113 req->req.length, req->req.buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 }
1115 }
1116
1117done:
1118 local_irq_restore(flags);
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -08001119 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120}
1121
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -08001122static int
1123udc_debugfs_open(struct inode *inode, struct file *file)
1124{
1125 return single_open(file, udc_seq_show, inode->i_private);
1126}
1127
1128static const struct file_operations debug_fops = {
1129 .open = udc_debugfs_open,
1130 .read = seq_read,
1131 .llseek = seq_lseek,
1132 .release = single_release,
1133 .owner = THIS_MODULE,
1134};
1135
1136#define create_debug_files(dev) \
1137 do { \
1138 dev->debugfs_udc = debugfs_create_file(dev->gadget.name, \
1139 S_IRUGO, NULL, dev, &debug_fops); \
1140 } while (0)
1141#define remove_debug_files(dev) \
1142 do { \
1143 if (dev->debugfs_udc) \
1144 debugfs_remove(dev->debugfs_udc); \
1145 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
1147#else /* !CONFIG_USB_GADGET_DEBUG_FILES */
1148
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -08001149#define create_debug_files(dev) do {} while (0)
1150#define remove_debug_files(dev) do {} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
1152#endif /* CONFIG_USB_GADGET_DEBUG_FILES */
1153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154/*-------------------------------------------------------------------------*/
1155
1156/*
David Brownell34ebcd22007-02-24 12:23:52 -08001157 * udc_disable - disable USB device controller
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 */
Philipp Zabel7a857622008-06-22 23:36:39 +01001159static void udc_disable(struct pxa25x_udc *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160{
1161 /* block all irqs */
1162 udc_set_mask_UDCCR(UDCCR_SRM|UDCCR_REM);
1163 UICR0 = UICR1 = 0xff;
1164 UFNRH = UFNRH_SIM;
1165
1166 /* if hardware supports it, disconnect from usb */
David Brownell91987692005-05-07 13:20:19 -07001167 pullup_off();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
1169 udc_clear_mask_UDCCR(UDCCR_UDE);
1170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 ep0_idle (dev);
1172 dev->gadget.speed = USB_SPEED_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173}
1174
1175
1176/*
David Brownell34ebcd22007-02-24 12:23:52 -08001177 * udc_reinit - initialize software state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 */
Philipp Zabel7a857622008-06-22 23:36:39 +01001179static void udc_reinit(struct pxa25x_udc *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180{
1181 u32 i;
1182
1183 /* device/ep0 records init */
1184 INIT_LIST_HEAD (&dev->gadget.ep_list);
1185 INIT_LIST_HEAD (&dev->gadget.ep0->ep_list);
1186 dev->ep0state = EP0_IDLE;
1187
1188 /* basic endpoint records init */
1189 for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
Philipp Zabel7a857622008-06-22 23:36:39 +01001190 struct pxa25x_ep *ep = &dev->ep[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
1192 if (i != 0)
1193 list_add_tail (&ep->ep.ep_list, &dev->gadget.ep_list);
1194
1195 ep->desc = NULL;
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +02001196 ep->ep.desc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 ep->stopped = 0;
1198 INIT_LIST_HEAD (&ep->queue);
David Brownellad8c6232007-06-30 06:30:04 -07001199 ep->pio_irqs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 }
1201
1202 /* the rest was statically initialized, and is read-only */
1203}
1204
1205/* until it's enabled, this UDC should be completely invisible
1206 * to any USB host.
1207 */
Philipp Zabel7a857622008-06-22 23:36:39 +01001208static void udc_enable (struct pxa25x_udc *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209{
1210 udc_clear_mask_UDCCR(UDCCR_UDE);
1211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 /* try to clear these bits before we enable the udc */
1213 udc_ack_int_UDCCR(UDCCR_SUSIR|/*UDCCR_RSTIR|*/UDCCR_RESIR);
1214
1215 ep0_idle(dev);
1216 dev->gadget.speed = USB_SPEED_UNKNOWN;
1217 dev->stats.irqs = 0;
1218
1219 /*
1220 * sequence taken from chapter 12.5.10, PXA250 AppProcDevManual:
1221 * - enable UDC
1222 * - if RESET is already in progress, ack interrupt
1223 * - unmask reset interrupt
1224 */
1225 udc_set_mask_UDCCR(UDCCR_UDE);
1226 if (!(UDCCR & UDCCR_UDA))
1227 udc_ack_int_UDCCR(UDCCR_RSTIR);
1228
1229 if (dev->has_cfr /* UDC_RES2 is defined */) {
1230 /* pxa255 (a0+) can avoid a set_config race that could
1231 * prevent gadget drivers from configuring correctly
1232 */
1233 UDCCFR = UDCCFR_ACM | UDCCFR_MB1;
1234 } else {
1235 /* "USB test mode" for pxa250 errata 40-42 (stepping a0, a1)
1236 * which could result in missing packets and interrupts.
1237 * supposedly one bit per endpoint, controlling whether it
1238 * double buffers or not; ACM/AREN bits fit into the holes.
1239 * zero bits (like USIR0_IRx) disable double buffering.
1240 */
1241 UDC_RES1 = 0x00;
1242 UDC_RES2 = 0x00;
1243 }
1244
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 /* enable suspend/resume and reset irqs */
1246 udc_clear_mask_UDCCR(UDCCR_SRM | UDCCR_REM);
1247
1248 /* enable ep0 irqs */
1249 UICR0 &= ~UICR0_IM0;
1250
1251 /* if hardware supports it, pullup D+ and wait for reset */
David Brownell91987692005-05-07 13:20:19 -07001252 pullup_on();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253}
1254
1255
1256/* when a driver is successfully registered, it will receive
1257 * control requests including set_configuration(), which enables
1258 * non-control requests. then usb traffic follows until a
1259 * disconnect is reported. then a host may connect again, or
1260 * the driver might get unbound.
1261 */
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001262static int pxa25x_start(struct usb_gadget_driver *driver,
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001263 int (*bind)(struct usb_gadget *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264{
Philipp Zabel7a857622008-06-22 23:36:39 +01001265 struct pxa25x_udc *dev = the_controller;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 int retval;
1267
1268 if (!driver
Michal Nazarewicz7177aed2011-11-19 18:27:38 +01001269 || driver->max_speed < USB_SPEED_FULL
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001270 || !bind
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 || !driver->disconnect
1272 || !driver->setup)
1273 return -EINVAL;
1274 if (!dev)
1275 return -ENODEV;
1276 if (dev->driver)
1277 return -EBUSY;
1278
1279 /* first hook up the driver ... */
1280 dev->driver = driver;
1281 dev->gadget.dev.driver = &driver->driver;
1282 dev->pullup = 1;
1283
David Brownell34ebcd22007-02-24 12:23:52 -08001284 retval = device_add (&dev->gadget.dev);
1285 if (retval) {
1286fail:
1287 dev->driver = NULL;
1288 dev->gadget.dev.driver = NULL;
1289 return retval;
1290 }
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001291 retval = bind(&dev->gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 if (retval) {
1293 DMSG("bind to driver %s --> error %d\n",
1294 driver->driver.name, retval);
1295 device_del (&dev->gadget.dev);
David Brownell34ebcd22007-02-24 12:23:52 -08001296 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
1299 /* ... then enable host detection and ep0; and we're ready
1300 * for set_configuration as well as eventual disconnect.
1301 */
1302 DMSG("registered gadget driver '%s'\n", driver->driver.name);
Philipp Zabelab26d202009-07-01 03:46:25 -07001303
1304 /* connect to bus through transceiver */
1305 if (dev->transceiver) {
Heikki Krogerus6e13c652012-02-13 13:24:20 +02001306 retval = otg_set_peripheral(dev->transceiver->otg,
1307 &dev->gadget);
Philipp Zabelab26d202009-07-01 03:46:25 -07001308 if (retval) {
1309 DMSG("can't bind to transceiver\n");
1310 if (driver->unbind)
1311 driver->unbind(&dev->gadget);
1312 goto bind_fail;
1313 }
1314 }
1315
Dmitry Baryshkov64cc2dd2008-02-22 17:17:19 -08001316 pullup(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 dump_state(dev);
1318 return 0;
Philipp Zabelab26d202009-07-01 03:46:25 -07001319bind_fail:
1320 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
1323static void
Philipp Zabel7a857622008-06-22 23:36:39 +01001324stop_activity(struct pxa25x_udc *dev, struct usb_gadget_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325{
1326 int i;
1327
1328 /* don't disconnect drivers more than once */
1329 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1330 driver = NULL;
1331 dev->gadget.speed = USB_SPEED_UNKNOWN;
1332
1333 /* prevent new request submissions, kill any outstanding requests */
1334 for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
Philipp Zabel7a857622008-06-22 23:36:39 +01001335 struct pxa25x_ep *ep = &dev->ep[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
1337 ep->stopped = 1;
1338 nuke(ep, -ESHUTDOWN);
1339 }
1340 del_timer_sync(&dev->timer);
1341
1342 /* report disconnect; the driver is already quiesced */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 if (driver)
1344 driver->disconnect(&dev->gadget);
1345
1346 /* re-init driver-visible data structures */
1347 udc_reinit(dev);
1348}
1349
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001350static int pxa25x_stop(struct usb_gadget_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
Philipp Zabel7a857622008-06-22 23:36:39 +01001352 struct pxa25x_udc *dev = the_controller;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
1354 if (!dev)
1355 return -ENODEV;
David Brownell6bea4762006-12-05 03:15:33 -08001356 if (!driver || driver != dev->driver || !driver->unbind)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 return -EINVAL;
1358
1359 local_irq_disable();
Dmitry Baryshkov64cc2dd2008-02-22 17:17:19 -08001360 dev->pullup = 0;
1361 pullup(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 stop_activity(dev, driver);
1363 local_irq_enable();
1364
Philipp Zabelab26d202009-07-01 03:46:25 -07001365 if (dev->transceiver)
Heikki Krogerus6e13c652012-02-13 13:24:20 +02001366 (void) otg_set_peripheral(dev->transceiver->otg, NULL);
Philipp Zabelab26d202009-07-01 03:46:25 -07001367
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 driver->unbind(&dev->gadget);
Patrik Sevalliuseb0be472007-11-20 09:32:00 -08001369 dev->gadget.dev.driver = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 dev->driver = NULL;
1371
1372 device_del (&dev->gadget.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
1374 DMSG("unregistered gadget driver '%s'\n", driver->driver.name);
1375 dump_state(dev);
1376 return 0;
1377}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
1379/*-------------------------------------------------------------------------*/
1380
1381#ifdef CONFIG_ARCH_LUBBOCK
1382
1383/* Lubbock has separate connect and disconnect irqs. More typical designs
1384 * use one GPIO as the VBUS IRQ, and another to control the D+ pullup.
1385 */
1386
1387static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +01001388lubbock_vbus_irq(int irq, void *_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389{
Philipp Zabel7a857622008-06-22 23:36:39 +01001390 struct pxa25x_udc *dev = _dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 int vbus;
1392
1393 dev->stats.irqs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 switch (irq) {
1395 case LUBBOCK_USB_IRQ:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 vbus = 1;
1397 disable_irq(LUBBOCK_USB_IRQ);
1398 enable_irq(LUBBOCK_USB_DISC_IRQ);
1399 break;
1400 case LUBBOCK_USB_DISC_IRQ:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 vbus = 0;
1402 disable_irq(LUBBOCK_USB_DISC_IRQ);
1403 enable_irq(LUBBOCK_USB_IRQ);
1404 break;
1405 default:
1406 return IRQ_NONE;
1407 }
1408
Philipp Zabel7a857622008-06-22 23:36:39 +01001409 pxa25x_udc_vbus_session(&dev->gadget, vbus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 return IRQ_HANDLED;
1411}
1412
1413#endif
1414
1415
1416/*-------------------------------------------------------------------------*/
1417
Philipp Zabel7a857622008-06-22 23:36:39 +01001418static inline void clear_ep_state (struct pxa25x_udc *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419{
1420 unsigned i;
1421
1422 /* hardware SET_{CONFIGURATION,INTERFACE} automagic resets endpoint
1423 * fifos, and pending transactions mustn't be continued in any case.
1424 */
1425 for (i = 1; i < PXA_UDC_NUM_ENDPOINTS; i++)
1426 nuke(&dev->ep[i], -ECONNABORTED);
1427}
1428
1429static void udc_watchdog(unsigned long _dev)
1430{
Philipp Zabel7a857622008-06-22 23:36:39 +01001431 struct pxa25x_udc *dev = (void *)_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
1433 local_irq_disable();
1434 if (dev->ep0state == EP0_STALL
1435 && (UDCCS0 & UDCCS0_FST) == 0
1436 && (UDCCS0 & UDCCS0_SST) == 0) {
1437 UDCCS0 = UDCCS0_FST|UDCCS0_FTF;
1438 DBG(DBG_VERBOSE, "ep0 re-stall\n");
1439 start_watchdog(dev);
1440 }
1441 local_irq_enable();
1442}
1443
Philipp Zabel7a857622008-06-22 23:36:39 +01001444static void handle_ep0 (struct pxa25x_udc *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445{
1446 u32 udccs0 = UDCCS0;
Philipp Zabel7a857622008-06-22 23:36:39 +01001447 struct pxa25x_ep *ep = &dev->ep [0];
1448 struct pxa25x_request *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 union {
1450 struct usb_ctrlrequest r;
1451 u8 raw [8];
1452 u32 word [2];
1453 } u;
1454
1455 if (list_empty(&ep->queue))
1456 req = NULL;
1457 else
Philipp Zabel7a857622008-06-22 23:36:39 +01001458 req = list_entry(ep->queue.next, struct pxa25x_request, queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
1460 /* clear stall status */
1461 if (udccs0 & UDCCS0_SST) {
1462 nuke(ep, -EPIPE);
1463 UDCCS0 = UDCCS0_SST;
1464 del_timer(&dev->timer);
1465 ep0_idle(dev);
1466 }
1467
1468 /* previous request unfinished? non-error iff back-to-back ... */
1469 if ((udccs0 & UDCCS0_SA) != 0 && dev->ep0state != EP0_IDLE) {
1470 nuke(ep, 0);
1471 del_timer(&dev->timer);
1472 ep0_idle(dev);
1473 }
1474
1475 switch (dev->ep0state) {
1476 case EP0_IDLE:
1477 /* late-breaking status? */
1478 udccs0 = UDCCS0;
1479
1480 /* start control request? */
1481 if (likely((udccs0 & (UDCCS0_OPR|UDCCS0_SA|UDCCS0_RNE))
1482 == (UDCCS0_OPR|UDCCS0_SA|UDCCS0_RNE))) {
1483 int i;
1484
1485 nuke (ep, -EPROTO);
1486
1487 /* read SETUP packet */
1488 for (i = 0; i < 8; i++) {
1489 if (unlikely(!(UDCCS0 & UDCCS0_RNE))) {
1490bad_setup:
1491 DMSG("SETUP %d!\n", i);
1492 goto stall;
1493 }
1494 u.raw [i] = (u8) UDDR0;
1495 }
1496 if (unlikely((UDCCS0 & UDCCS0_RNE) != 0))
1497 goto bad_setup;
1498
1499got_setup:
1500 DBG(DBG_VERBOSE, "SETUP %02x.%02x v%04x i%04x l%04x\n",
1501 u.r.bRequestType, u.r.bRequest,
1502 le16_to_cpu(u.r.wValue),
1503 le16_to_cpu(u.r.wIndex),
1504 le16_to_cpu(u.r.wLength));
1505
1506 /* cope with automagic for some standard requests. */
1507 dev->req_std = (u.r.bRequestType & USB_TYPE_MASK)
1508 == USB_TYPE_STANDARD;
1509 dev->req_config = 0;
1510 dev->req_pending = 1;
1511 switch (u.r.bRequest) {
1512 /* hardware restricts gadget drivers here! */
1513 case USB_REQ_SET_CONFIGURATION:
1514 if (u.r.bRequestType == USB_RECIP_DEVICE) {
1515 /* reflect hardware's automagic
1516 * up to the gadget driver.
1517 */
1518config_change:
1519 dev->req_config = 1;
1520 clear_ep_state(dev);
1521 /* if !has_cfr, there's no synch
1522 * else use AREN (later) not SA|OPR
1523 * USIR0_IR0 acts edge sensitive
1524 */
1525 }
1526 break;
1527 /* ... and here, even more ... */
1528 case USB_REQ_SET_INTERFACE:
1529 if (u.r.bRequestType == USB_RECIP_INTERFACE) {
1530 /* udc hardware is broken by design:
1531 * - altsetting may only be zero;
1532 * - hw resets all interfaces' eps;
1533 * - ep reset doesn't include halt(?).
1534 */
1535 DMSG("broken set_interface (%d/%d)\n",
1536 le16_to_cpu(u.r.wIndex),
1537 le16_to_cpu(u.r.wValue));
1538 goto config_change;
1539 }
1540 break;
1541 /* hardware was supposed to hide this */
1542 case USB_REQ_SET_ADDRESS:
1543 if (u.r.bRequestType == USB_RECIP_DEVICE) {
1544 ep0start(dev, 0, "address");
1545 return;
1546 }
1547 break;
1548 }
1549
1550 if (u.r.bRequestType & USB_DIR_IN)
1551 dev->ep0state = EP0_IN_DATA_PHASE;
1552 else
1553 dev->ep0state = EP0_OUT_DATA_PHASE;
1554
1555 i = dev->driver->setup(&dev->gadget, &u.r);
1556 if (i < 0) {
1557 /* hardware automagic preventing STALL... */
1558 if (dev->req_config) {
1559 /* hardware sometimes neglects to tell
1560 * tell us about config change events,
1561 * so later ones may fail...
1562 */
Arjan van de Venb6c63932008-07-25 01:45:52 -07001563 WARNING("config change %02x fail %d?\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 u.r.bRequest, i);
1565 return;
1566 /* TODO experiment: if has_cfr,
1567 * hardware didn't ACK; maybe we
1568 * could actually STALL!
1569 */
1570 }
1571 DBG(DBG_VERBOSE, "protocol STALL, "
1572 "%02x err %d\n", UDCCS0, i);
1573stall:
1574 /* the watchdog timer helps deal with cases
1575 * where udc seems to clear FST wrongly, and
1576 * then NAKs instead of STALLing.
1577 */
1578 ep0start(dev, UDCCS0_FST|UDCCS0_FTF, "stall");
1579 start_watchdog(dev);
1580 dev->ep0state = EP0_STALL;
1581
1582 /* deferred i/o == no response yet */
1583 } else if (dev->req_pending) {
1584 if (likely(dev->ep0state == EP0_IN_DATA_PHASE
1585 || dev->req_std || u.r.wLength))
1586 ep0start(dev, 0, "defer");
1587 else
1588 ep0start(dev, UDCCS0_IPR, "defer/IPR");
1589 }
1590
1591 /* expect at least one data or status stage irq */
1592 return;
1593
1594 } else if (likely((udccs0 & (UDCCS0_OPR|UDCCS0_SA))
1595 == (UDCCS0_OPR|UDCCS0_SA))) {
1596 unsigned i;
1597
1598 /* pxa210/250 erratum 131 for B0/B1 says RNE lies.
1599 * still observed on a pxa255 a0.
1600 */
1601 DBG(DBG_VERBOSE, "e131\n");
1602 nuke(ep, -EPROTO);
1603
1604 /* read SETUP data, but don't trust it too much */
1605 for (i = 0; i < 8; i++)
1606 u.raw [i] = (u8) UDDR0;
1607 if ((u.r.bRequestType & USB_RECIP_MASK)
1608 > USB_RECIP_OTHER)
1609 goto stall;
1610 if (u.word [0] == 0 && u.word [1] == 0)
1611 goto stall;
1612 goto got_setup;
1613 } else {
1614 /* some random early IRQ:
1615 * - we acked FST
1616 * - IPR cleared
1617 * - OPR got set, without SA (likely status stage)
1618 */
1619 UDCCS0 = udccs0 & (UDCCS0_SA|UDCCS0_OPR);
1620 }
1621 break;
1622 case EP0_IN_DATA_PHASE: /* GET_DESCRIPTOR etc */
1623 if (udccs0 & UDCCS0_OPR) {
1624 UDCCS0 = UDCCS0_OPR|UDCCS0_FTF;
1625 DBG(DBG_VERBOSE, "ep0in premature status\n");
1626 if (req)
1627 done(ep, req, 0);
1628 ep0_idle(dev);
1629 } else /* irq was IPR clearing */ {
1630 if (req) {
1631 /* this IN packet might finish the request */
1632 (void) write_ep0_fifo(ep, req);
1633 } /* else IN token before response was written */
1634 }
1635 break;
1636 case EP0_OUT_DATA_PHASE: /* SET_DESCRIPTOR etc */
1637 if (udccs0 & UDCCS0_OPR) {
1638 if (req) {
1639 /* this OUT packet might finish the request */
1640 if (read_ep0_fifo(ep, req))
1641 done(ep, req, 0);
1642 /* else more OUT packets expected */
1643 } /* else OUT token before read was issued */
1644 } else /* irq was IPR clearing */ {
1645 DBG(DBG_VERBOSE, "ep0out premature status\n");
1646 if (req)
1647 done(ep, req, 0);
1648 ep0_idle(dev);
1649 }
1650 break;
1651 case EP0_END_XFER:
1652 if (req)
1653 done(ep, req, 0);
1654 /* ack control-IN status (maybe in-zlp was skipped)
1655 * also appears after some config change events.
1656 */
1657 if (udccs0 & UDCCS0_OPR)
1658 UDCCS0 = UDCCS0_OPR;
1659 ep0_idle(dev);
1660 break;
1661 case EP0_STALL:
1662 UDCCS0 = UDCCS0_FST;
1663 break;
1664 }
1665 USIR0 = USIR0_IR0;
1666}
1667
Philipp Zabel7a857622008-06-22 23:36:39 +01001668static void handle_ep(struct pxa25x_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669{
Philipp Zabel7a857622008-06-22 23:36:39 +01001670 struct pxa25x_request *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 int is_in = ep->bEndpointAddress & USB_DIR_IN;
1672 int completed;
1673 u32 udccs, tmp;
1674
1675 do {
1676 completed = 0;
1677 if (likely (!list_empty(&ep->queue)))
1678 req = list_entry(ep->queue.next,
Philipp Zabel7a857622008-06-22 23:36:39 +01001679 struct pxa25x_request, queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 else
1681 req = NULL;
1682
1683 // TODO check FST handling
1684
1685 udccs = *ep->reg_udccs;
1686 if (unlikely(is_in)) { /* irq from TPC, SST, or (ISO) TUR */
1687 tmp = UDCCS_BI_TUR;
1688 if (likely(ep->bmAttributes == USB_ENDPOINT_XFER_BULK))
1689 tmp |= UDCCS_BI_SST;
1690 tmp &= udccs;
1691 if (likely (tmp))
1692 *ep->reg_udccs = tmp;
1693 if (req && likely ((udccs & UDCCS_BI_TFS) != 0))
1694 completed = write_fifo(ep, req);
1695
1696 } else { /* irq from RPC (or for ISO, ROF) */
1697 if (likely(ep->bmAttributes == USB_ENDPOINT_XFER_BULK))
1698 tmp = UDCCS_BO_SST | UDCCS_BO_DME;
1699 else
1700 tmp = UDCCS_IO_ROF | UDCCS_IO_DME;
1701 tmp &= udccs;
1702 if (likely(tmp))
1703 *ep->reg_udccs = tmp;
1704
1705 /* fifos can hold packets, ready for reading... */
1706 if (likely(req)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 completed = read_fifo(ep, req);
1708 } else
1709 pio_irq_disable (ep->bEndpointAddress);
1710 }
1711 ep->pio_irqs++;
1712 } while (completed);
1713}
1714
1715/*
Philipp Zabel7a857622008-06-22 23:36:39 +01001716 * pxa25x_udc_irq - interrupt handler
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 *
1718 * avoid delays in ep0 processing. the control handshaking isn't always
1719 * under software control (pxa250c0 and the pxa255 are better), and delays
1720 * could cause usb protocol errors.
1721 */
1722static irqreturn_t
Philipp Zabel7a857622008-06-22 23:36:39 +01001723pxa25x_udc_irq(int irq, void *_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724{
Philipp Zabel7a857622008-06-22 23:36:39 +01001725 struct pxa25x_udc *dev = _dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 int handled;
1727
1728 dev->stats.irqs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 do {
1730 u32 udccr = UDCCR;
1731
1732 handled = 0;
1733
1734 /* SUSpend Interrupt Request */
1735 if (unlikely(udccr & UDCCR_SUSIR)) {
1736 udc_ack_int_UDCCR(UDCCR_SUSIR);
1737 handled = 1;
Dmitry Eremin-Solenikova8ecc862011-02-14 15:33:19 +03001738 DBG(DBG_VERBOSE, "USB suspend\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
Dmitry Eremin-Solenikova8ecc862011-02-14 15:33:19 +03001740 if (dev->gadget.speed != USB_SPEED_UNKNOWN
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 && dev->driver
1742 && dev->driver->suspend)
1743 dev->driver->suspend(&dev->gadget);
1744 ep0_idle (dev);
1745 }
1746
1747 /* RESume Interrupt Request */
1748 if (unlikely(udccr & UDCCR_RESIR)) {
1749 udc_ack_int_UDCCR(UDCCR_RESIR);
1750 handled = 1;
1751 DBG(DBG_VERBOSE, "USB resume\n");
1752
1753 if (dev->gadget.speed != USB_SPEED_UNKNOWN
1754 && dev->driver
Dmitry Eremin-Solenikova8ecc862011-02-14 15:33:19 +03001755 && dev->driver->resume)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 dev->driver->resume(&dev->gadget);
1757 }
1758
1759 /* ReSeT Interrupt Request - USB reset */
1760 if (unlikely(udccr & UDCCR_RSTIR)) {
1761 udc_ack_int_UDCCR(UDCCR_RSTIR);
1762 handled = 1;
1763
1764 if ((UDCCR & UDCCR_UDA) == 0) {
1765 DBG(DBG_VERBOSE, "USB reset start\n");
1766
1767 /* reset driver and endpoints,
1768 * in case that's not yet done
1769 */
1770 stop_activity (dev, dev->driver);
1771
1772 } else {
1773 DBG(DBG_VERBOSE, "USB reset end\n");
1774 dev->gadget.speed = USB_SPEED_FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 memset(&dev->stats, 0, sizeof dev->stats);
1776 /* driver and endpoints are still reset */
1777 }
1778
1779 } else {
1780 u32 usir0 = USIR0 & ~UICR0;
1781 u32 usir1 = USIR1 & ~UICR1;
1782 int i;
1783
1784 if (unlikely (!usir0 && !usir1))
1785 continue;
1786
1787 DBG(DBG_VERY_NOISY, "irq %02x.%02x\n", usir1, usir0);
1788
1789 /* control traffic */
1790 if (usir0 & USIR0_IR0) {
1791 dev->ep[0].pio_irqs++;
1792 handle_ep0(dev);
1793 handled = 1;
1794 }
1795
1796 /* endpoint data transfers */
1797 for (i = 0; i < 8; i++) {
1798 u32 tmp = 1 << i;
1799
1800 if (i && (usir0 & tmp)) {
1801 handle_ep(&dev->ep[i]);
1802 USIR0 |= tmp;
1803 handled = 1;
1804 }
David Brownell6d845992009-07-01 03:43:58 -07001805#ifndef CONFIG_USB_PXA25X_SMALL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 if (usir1 & tmp) {
1807 handle_ep(&dev->ep[i+8]);
1808 USIR1 |= tmp;
1809 handled = 1;
1810 }
David Brownell6d845992009-07-01 03:43:58 -07001811#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 }
1813 }
1814
1815 /* we could also ask for 1 msec SOF (SIR) interrupts */
1816
1817 } while (handled);
1818 return IRQ_HANDLED;
1819}
1820
1821/*-------------------------------------------------------------------------*/
1822
1823static void nop_release (struct device *dev)
1824{
Kay Sievers7071a3c2008-05-02 06:02:41 +02001825 DMSG("%s %s\n", __func__, dev_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826}
1827
1828/* this uses load-time allocation and initialization (instead of
1829 * doing it at run-time) to save code, eliminate fault paths, and
1830 * be more obviously correct.
1831 */
Philipp Zabel7a857622008-06-22 23:36:39 +01001832static struct pxa25x_udc memory = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 .gadget = {
Philipp Zabel7a857622008-06-22 23:36:39 +01001834 .ops = &pxa25x_udc_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 .ep0 = &memory.ep[0].ep,
1836 .name = driver_name,
1837 .dev = {
Kay Sieversc682b172009-01-06 10:44:42 -08001838 .init_name = "gadget",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 .release = nop_release,
1840 },
1841 },
1842
1843 /* control endpoint */
1844 .ep[0] = {
1845 .ep = {
1846 .name = ep0name,
Philipp Zabel7a857622008-06-22 23:36:39 +01001847 .ops = &pxa25x_ep_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 .maxpacket = EP0_FIFO_SIZE,
1849 },
1850 .dev = &memory,
1851 .reg_udccs = &UDCCS0,
1852 .reg_uddr = &UDDR0,
1853 },
1854
1855 /* first group of endpoints */
1856 .ep[1] = {
1857 .ep = {
1858 .name = "ep1in-bulk",
Philipp Zabel7a857622008-06-22 23:36:39 +01001859 .ops = &pxa25x_ep_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 .maxpacket = BULK_FIFO_SIZE,
1861 },
1862 .dev = &memory,
1863 .fifo_size = BULK_FIFO_SIZE,
1864 .bEndpointAddress = USB_DIR_IN | 1,
1865 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1866 .reg_udccs = &UDCCS1,
1867 .reg_uddr = &UDDR1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 },
1869 .ep[2] = {
1870 .ep = {
1871 .name = "ep2out-bulk",
Philipp Zabel7a857622008-06-22 23:36:39 +01001872 .ops = &pxa25x_ep_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 .maxpacket = BULK_FIFO_SIZE,
1874 },
1875 .dev = &memory,
1876 .fifo_size = BULK_FIFO_SIZE,
1877 .bEndpointAddress = 2,
1878 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1879 .reg_udccs = &UDCCS2,
1880 .reg_ubcr = &UBCR2,
1881 .reg_uddr = &UDDR2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 },
Philipp Zabel7a857622008-06-22 23:36:39 +01001883#ifndef CONFIG_USB_PXA25X_SMALL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 .ep[3] = {
1885 .ep = {
1886 .name = "ep3in-iso",
Philipp Zabel7a857622008-06-22 23:36:39 +01001887 .ops = &pxa25x_ep_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 .maxpacket = ISO_FIFO_SIZE,
1889 },
1890 .dev = &memory,
1891 .fifo_size = ISO_FIFO_SIZE,
1892 .bEndpointAddress = USB_DIR_IN | 3,
1893 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
1894 .reg_udccs = &UDCCS3,
1895 .reg_uddr = &UDDR3,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 },
1897 .ep[4] = {
1898 .ep = {
1899 .name = "ep4out-iso",
Philipp Zabel7a857622008-06-22 23:36:39 +01001900 .ops = &pxa25x_ep_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 .maxpacket = ISO_FIFO_SIZE,
1902 },
1903 .dev = &memory,
1904 .fifo_size = ISO_FIFO_SIZE,
1905 .bEndpointAddress = 4,
1906 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
1907 .reg_udccs = &UDCCS4,
1908 .reg_ubcr = &UBCR4,
1909 .reg_uddr = &UDDR4,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 },
1911 .ep[5] = {
1912 .ep = {
1913 .name = "ep5in-int",
Philipp Zabel7a857622008-06-22 23:36:39 +01001914 .ops = &pxa25x_ep_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 .maxpacket = INT_FIFO_SIZE,
1916 },
1917 .dev = &memory,
1918 .fifo_size = INT_FIFO_SIZE,
1919 .bEndpointAddress = USB_DIR_IN | 5,
1920 .bmAttributes = USB_ENDPOINT_XFER_INT,
1921 .reg_udccs = &UDCCS5,
1922 .reg_uddr = &UDDR5,
1923 },
1924
1925 /* second group of endpoints */
1926 .ep[6] = {
1927 .ep = {
1928 .name = "ep6in-bulk",
Philipp Zabel7a857622008-06-22 23:36:39 +01001929 .ops = &pxa25x_ep_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 .maxpacket = BULK_FIFO_SIZE,
1931 },
1932 .dev = &memory,
1933 .fifo_size = BULK_FIFO_SIZE,
1934 .bEndpointAddress = USB_DIR_IN | 6,
1935 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1936 .reg_udccs = &UDCCS6,
1937 .reg_uddr = &UDDR6,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 },
1939 .ep[7] = {
1940 .ep = {
1941 .name = "ep7out-bulk",
Philipp Zabel7a857622008-06-22 23:36:39 +01001942 .ops = &pxa25x_ep_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 .maxpacket = BULK_FIFO_SIZE,
1944 },
1945 .dev = &memory,
1946 .fifo_size = BULK_FIFO_SIZE,
1947 .bEndpointAddress = 7,
1948 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1949 .reg_udccs = &UDCCS7,
1950 .reg_ubcr = &UBCR7,
1951 .reg_uddr = &UDDR7,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 },
1953 .ep[8] = {
1954 .ep = {
1955 .name = "ep8in-iso",
Philipp Zabel7a857622008-06-22 23:36:39 +01001956 .ops = &pxa25x_ep_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 .maxpacket = ISO_FIFO_SIZE,
1958 },
1959 .dev = &memory,
1960 .fifo_size = ISO_FIFO_SIZE,
1961 .bEndpointAddress = USB_DIR_IN | 8,
1962 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
1963 .reg_udccs = &UDCCS8,
1964 .reg_uddr = &UDDR8,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 },
1966 .ep[9] = {
1967 .ep = {
1968 .name = "ep9out-iso",
Philipp Zabel7a857622008-06-22 23:36:39 +01001969 .ops = &pxa25x_ep_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 .maxpacket = ISO_FIFO_SIZE,
1971 },
1972 .dev = &memory,
1973 .fifo_size = ISO_FIFO_SIZE,
1974 .bEndpointAddress = 9,
1975 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
1976 .reg_udccs = &UDCCS9,
1977 .reg_ubcr = &UBCR9,
1978 .reg_uddr = &UDDR9,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 },
1980 .ep[10] = {
1981 .ep = {
1982 .name = "ep10in-int",
Philipp Zabel7a857622008-06-22 23:36:39 +01001983 .ops = &pxa25x_ep_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 .maxpacket = INT_FIFO_SIZE,
1985 },
1986 .dev = &memory,
1987 .fifo_size = INT_FIFO_SIZE,
1988 .bEndpointAddress = USB_DIR_IN | 10,
1989 .bmAttributes = USB_ENDPOINT_XFER_INT,
1990 .reg_udccs = &UDCCS10,
1991 .reg_uddr = &UDDR10,
1992 },
1993
1994 /* third group of endpoints */
1995 .ep[11] = {
1996 .ep = {
1997 .name = "ep11in-bulk",
Philipp Zabel7a857622008-06-22 23:36:39 +01001998 .ops = &pxa25x_ep_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 .maxpacket = BULK_FIFO_SIZE,
2000 },
2001 .dev = &memory,
2002 .fifo_size = BULK_FIFO_SIZE,
2003 .bEndpointAddress = USB_DIR_IN | 11,
2004 .bmAttributes = USB_ENDPOINT_XFER_BULK,
2005 .reg_udccs = &UDCCS11,
2006 .reg_uddr = &UDDR11,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 },
2008 .ep[12] = {
2009 .ep = {
2010 .name = "ep12out-bulk",
Philipp Zabel7a857622008-06-22 23:36:39 +01002011 .ops = &pxa25x_ep_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 .maxpacket = BULK_FIFO_SIZE,
2013 },
2014 .dev = &memory,
2015 .fifo_size = BULK_FIFO_SIZE,
2016 .bEndpointAddress = 12,
2017 .bmAttributes = USB_ENDPOINT_XFER_BULK,
2018 .reg_udccs = &UDCCS12,
2019 .reg_ubcr = &UBCR12,
2020 .reg_uddr = &UDDR12,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 },
2022 .ep[13] = {
2023 .ep = {
2024 .name = "ep13in-iso",
Philipp Zabel7a857622008-06-22 23:36:39 +01002025 .ops = &pxa25x_ep_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 .maxpacket = ISO_FIFO_SIZE,
2027 },
2028 .dev = &memory,
2029 .fifo_size = ISO_FIFO_SIZE,
2030 .bEndpointAddress = USB_DIR_IN | 13,
2031 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
2032 .reg_udccs = &UDCCS13,
2033 .reg_uddr = &UDDR13,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 },
2035 .ep[14] = {
2036 .ep = {
2037 .name = "ep14out-iso",
Philipp Zabel7a857622008-06-22 23:36:39 +01002038 .ops = &pxa25x_ep_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 .maxpacket = ISO_FIFO_SIZE,
2040 },
2041 .dev = &memory,
2042 .fifo_size = ISO_FIFO_SIZE,
2043 .bEndpointAddress = 14,
2044 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
2045 .reg_udccs = &UDCCS14,
2046 .reg_ubcr = &UBCR14,
2047 .reg_uddr = &UDDR14,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 },
2049 .ep[15] = {
2050 .ep = {
2051 .name = "ep15in-int",
Philipp Zabel7a857622008-06-22 23:36:39 +01002052 .ops = &pxa25x_ep_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 .maxpacket = INT_FIFO_SIZE,
2054 },
2055 .dev = &memory,
2056 .fifo_size = INT_FIFO_SIZE,
2057 .bEndpointAddress = USB_DIR_IN | 15,
2058 .bmAttributes = USB_ENDPOINT_XFER_INT,
2059 .reg_udccs = &UDCCS15,
2060 .reg_uddr = &UDDR15,
2061 },
Philipp Zabel7a857622008-06-22 23:36:39 +01002062#endif /* !CONFIG_USB_PXA25X_SMALL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063};
2064
2065#define CP15R0_VENDOR_MASK 0xffffe000
2066
2067#if defined(CONFIG_ARCH_PXA)
2068#define CP15R0_XSCALE_VALUE 0x69052000 /* intel/arm/xscale */
2069
2070#elif defined(CONFIG_ARCH_IXP4XX)
2071#define CP15R0_XSCALE_VALUE 0x69054000 /* intel/arm/ixp4xx */
2072
2073#endif
2074
2075#define CP15R0_PROD_MASK 0x000003f0
2076#define PXA25x 0x00000100 /* and PXA26x */
2077#define PXA210 0x00000120
2078
2079#define CP15R0_REV_MASK 0x0000000f
2080
2081#define CP15R0_PRODREV_MASK (CP15R0_PROD_MASK | CP15R0_REV_MASK)
2082
2083#define PXA255_A0 0x00000106 /* or PXA260_B1 */
2084#define PXA250_C0 0x00000105 /* or PXA26x_B0 */
2085#define PXA250_B2 0x00000104
2086#define PXA250_B1 0x00000103 /* or PXA260_A0 */
2087#define PXA250_B0 0x00000102
2088#define PXA250_A1 0x00000101
2089#define PXA250_A0 0x00000100
2090
2091#define PXA210_C0 0x00000125
2092#define PXA210_B2 0x00000124
2093#define PXA210_B1 0x00000123
2094#define PXA210_B0 0x00000122
2095#define IXP425_A0 0x000001c1
David Brownell827982c2006-11-20 11:38:57 -08002096#define IXP425_B0 0x000001f1
Milan Svoboda043ea182006-05-29 03:34:00 -07002097#define IXP465_AD 0x00000200
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
2099/*
David Brownell34ebcd22007-02-24 12:23:52 -08002100 * probe - binds to the platform device
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 */
Philipp Zabel7a857622008-06-22 23:36:39 +01002102static int __init pxa25x_udc_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103{
Philipp Zabel7a857622008-06-22 23:36:39 +01002104 struct pxa25x_udc *dev = &memory;
Dmitry Eremin-Solenikova8ecc862011-02-14 15:33:19 +03002105 int retval, irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 u32 chiprev;
2107
2108 /* insist on Intel/ARM/XScale */
2109 asm("mrc%? p15, 0, %0, c0, c0" : "=r" (chiprev));
2110 if ((chiprev & CP15R0_VENDOR_MASK) != CP15R0_XSCALE_VALUE) {
David Brownell00274922007-11-19 12:58:36 -08002111 pr_err("%s: not XScale!\n", driver_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 return -ENODEV;
2113 }
2114
2115 /* trigger chiprev-specific logic */
2116 switch (chiprev & CP15R0_PRODREV_MASK) {
2117#if defined(CONFIG_ARCH_PXA)
2118 case PXA255_A0:
2119 dev->has_cfr = 1;
2120 break;
2121 case PXA250_A0:
2122 case PXA250_A1:
2123 /* A0/A1 "not released"; ep 13, 15 unusable */
2124 /* fall through */
2125 case PXA250_B2: case PXA210_B2:
2126 case PXA250_B1: case PXA210_B1:
2127 case PXA250_B0: case PXA210_B0:
David Brownellad8c6232007-06-30 06:30:04 -07002128 /* OUT-DMA is broken ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 /* fall through */
2130 case PXA250_C0: case PXA210_C0:
2131 break;
2132#elif defined(CONFIG_ARCH_IXP4XX)
2133 case IXP425_A0:
David Brownell827982c2006-11-20 11:38:57 -08002134 case IXP425_B0:
Milan Svoboda043ea182006-05-29 03:34:00 -07002135 case IXP465_AD:
2136 dev->has_cfr = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 break;
2138#endif
2139 default:
David Brownell00274922007-11-19 12:58:36 -08002140 pr_err("%s: unrecognized processor: %08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 driver_name, chiprev);
2142 /* iop3xx, ixp4xx, ... */
2143 return -ENODEV;
2144 }
2145
David Brownell34ebcd22007-02-24 12:23:52 -08002146 irq = platform_get_irq(pdev, 0);
2147 if (irq < 0)
2148 return -ENODEV;
2149
Russell Kinge0d8b132008-11-11 17:52:32 +00002150 dev->clk = clk_get(&pdev->dev, NULL);
Russell King6549e6c2007-08-20 10:33:35 +01002151 if (IS_ERR(dev->clk)) {
2152 retval = PTR_ERR(dev->clk);
2153 goto err_clk;
2154 }
Russell King6549e6c2007-08-20 10:33:35 +01002155
David Brownellad8c6232007-06-30 06:30:04 -07002156 pr_debug("%s: IRQ %d%s%s\n", driver_name, irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 dev->has_cfr ? "" : " (!cfr)",
David Brownellad8c6232007-06-30 06:30:04 -07002158 SIZE_STR "(pio)"
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 );
2160
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 /* other non-static parts of init */
Russell King3ae5eae2005-11-09 22:32:44 +00002162 dev->dev = &pdev->dev;
2163 dev->mach = pdev->dev.platform_data;
Milan Svoboda9068a4c2007-06-30 06:25:35 -07002164
Heikki Krogerusb96d3b02012-02-13 13:24:18 +02002165 dev->transceiver = usb_get_transceiver();
Philipp Zabelab26d202009-07-01 03:46:25 -07002166
Philipp Zabel56a075d2009-07-01 03:42:45 -07002167 if (gpio_is_valid(dev->mach->gpio_pullup)) {
Milan Svoboda9068a4c2007-06-30 06:25:35 -07002168 if ((retval = gpio_request(dev->mach->gpio_pullup,
Philipp Zabel7a857622008-06-22 23:36:39 +01002169 "pca25x_udc GPIO PULLUP"))) {
Milan Svoboda9068a4c2007-06-30 06:25:35 -07002170 dev_dbg(&pdev->dev,
2171 "can't get pullup gpio %d, err: %d\n",
2172 dev->mach->gpio_pullup, retval);
Russell King6549e6c2007-08-20 10:33:35 +01002173 goto err_gpio_pullup;
Milan Svoboda9068a4c2007-06-30 06:25:35 -07002174 }
2175 gpio_direction_output(dev->mach->gpio_pullup, 0);
2176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
2178 init_timer(&dev->timer);
2179 dev->timer.function = udc_watchdog;
2180 dev->timer.data = (unsigned long) dev;
2181
2182 device_initialize(&dev->gadget.dev);
Russell King3ae5eae2005-11-09 22:32:44 +00002183 dev->gadget.dev.parent = &pdev->dev;
2184 dev->gadget.dev.dma_mask = pdev->dev.dma_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
2186 the_controller = dev;
Russell King3ae5eae2005-11-09 22:32:44 +00002187 platform_set_drvdata(pdev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
2189 udc_disable(dev);
2190 udc_reinit(dev);
2191
Dmitry Eremin-Solenikova8ecc862011-02-14 15:33:19 +03002192 dev->vbus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193
2194 /* irq setup after old hardware state is cleaned up */
Philipp Zabel7a857622008-06-22 23:36:39 +01002195 retval = request_irq(irq, pxa25x_udc_irq,
Yong Zhangb5dd18d2011-09-07 16:10:52 +08002196 0, driver_name, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 if (retval != 0) {
David Brownell00274922007-11-19 12:58:36 -08002198 pr_err("%s: can't get irq %d, err %d\n",
David Brownell34ebcd22007-02-24 12:23:52 -08002199 driver_name, irq, retval);
Russell King6549e6c2007-08-20 10:33:35 +01002200 goto err_irq1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 }
2202 dev->got_irq = 1;
2203
2204#ifdef CONFIG_ARCH_LUBBOCK
2205 if (machine_is_lubbock()) {
2206 retval = request_irq(LUBBOCK_USB_DISC_IRQ,
2207 lubbock_vbus_irq,
Yong Zhangb5dd18d2011-09-07 16:10:52 +08002208 IRQF_SAMPLE_RANDOM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 driver_name, dev);
2210 if (retval != 0) {
David Brownell00274922007-11-19 12:58:36 -08002211 pr_err("%s: can't get irq %i, err %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 driver_name, LUBBOCK_USB_DISC_IRQ, retval);
Russell King6549e6c2007-08-20 10:33:35 +01002213 goto err_irq_lub;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 }
2215 retval = request_irq(LUBBOCK_USB_IRQ,
2216 lubbock_vbus_irq,
Yong Zhangb5dd18d2011-09-07 16:10:52 +08002217 IRQF_SAMPLE_RANDOM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 driver_name, dev);
2219 if (retval != 0) {
David Brownell00274922007-11-19 12:58:36 -08002220 pr_err("%s: can't get irq %i, err %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 driver_name, LUBBOCK_USB_IRQ, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 goto lubbock_fail0;
2223 }
David Brownellb2bbb202006-06-29 12:25:39 -07002224 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225#endif
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -08002226 create_debug_files(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03002228 retval = usb_add_gadget_udc(&pdev->dev, &dev->gadget);
2229 if (!retval)
2230 return retval;
Russell King6549e6c2007-08-20 10:33:35 +01002231
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03002232 remove_debug_files(dev);
Russell King6549e6c2007-08-20 10:33:35 +01002233#ifdef CONFIG_ARCH_LUBBOCK
Sebastian Andrzej Siewiora6207b12011-06-03 19:50:46 +02002234lubbock_fail0:
Russell King6549e6c2007-08-20 10:33:35 +01002235 free_irq(LUBBOCK_USB_DISC_IRQ, dev);
2236 err_irq_lub:
Russell King6549e6c2007-08-20 10:33:35 +01002237 free_irq(irq, dev);
Sebastian Andrzej Siewiora6207b12011-06-03 19:50:46 +02002238#endif
Russell King6549e6c2007-08-20 10:33:35 +01002239 err_irq1:
Philipp Zabel56a075d2009-07-01 03:42:45 -07002240 if (gpio_is_valid(dev->mach->gpio_pullup))
Russell King6549e6c2007-08-20 10:33:35 +01002241 gpio_free(dev->mach->gpio_pullup);
2242 err_gpio_pullup:
Philipp Zabelab26d202009-07-01 03:46:25 -07002243 if (dev->transceiver) {
Heikki Krogerusb96d3b02012-02-13 13:24:18 +02002244 usb_put_transceiver(dev->transceiver);
Philipp Zabelab26d202009-07-01 03:46:25 -07002245 dev->transceiver = NULL;
2246 }
Russell King6549e6c2007-08-20 10:33:35 +01002247 clk_put(dev->clk);
2248 err_clk:
Russell King6549e6c2007-08-20 10:33:35 +01002249 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250}
David Brownell91987692005-05-07 13:20:19 -07002251
Philipp Zabel7a857622008-06-22 23:36:39 +01002252static void pxa25x_udc_shutdown(struct platform_device *_dev)
David Brownell91987692005-05-07 13:20:19 -07002253{
2254 pullup_off();
2255}
2256
Philipp Zabel7a857622008-06-22 23:36:39 +01002257static int __exit pxa25x_udc_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258{
Philipp Zabel7a857622008-06-22 23:36:39 +01002259 struct pxa25x_udc *dev = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03002261 usb_del_gadget_udc(&dev->gadget);
David Brownell6bea4762006-12-05 03:15:33 -08002262 if (dev->driver)
2263 return -EBUSY;
2264
Dmitry Baryshkov64cc2dd2008-02-22 17:17:19 -08002265 dev->pullup = 0;
2266 pullup(dev);
2267
Dmitry Baryshkov040fa1b2007-12-30 11:56:27 -08002268 remove_debug_files(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269
2270 if (dev->got_irq) {
David Brownell34ebcd22007-02-24 12:23:52 -08002271 free_irq(platform_get_irq(pdev, 0), dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 dev->got_irq = 0;
2273 }
Milan Svoboda44df45a2006-05-29 03:34:00 -07002274#ifdef CONFIG_ARCH_LUBBOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 if (machine_is_lubbock()) {
2276 free_irq(LUBBOCK_USB_DISC_IRQ, dev);
2277 free_irq(LUBBOCK_USB_IRQ, dev);
2278 }
Milan Svoboda44df45a2006-05-29 03:34:00 -07002279#endif
Philipp Zabel56a075d2009-07-01 03:42:45 -07002280 if (gpio_is_valid(dev->mach->gpio_pullup))
Milan Svoboda9068a4c2007-06-30 06:25:35 -07002281 gpio_free(dev->mach->gpio_pullup);
2282
Russell King6549e6c2007-08-20 10:33:35 +01002283 clk_put(dev->clk);
Russell King6549e6c2007-08-20 10:33:35 +01002284
Philipp Zabelab26d202009-07-01 03:46:25 -07002285 if (dev->transceiver) {
Heikki Krogerusb96d3b02012-02-13 13:24:18 +02002286 usb_put_transceiver(dev->transceiver);
Philipp Zabelab26d202009-07-01 03:46:25 -07002287 dev->transceiver = NULL;
2288 }
2289
Russell King3ae5eae2005-11-09 22:32:44 +00002290 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 the_controller = NULL;
2292 return 0;
2293}
2294
2295/*-------------------------------------------------------------------------*/
2296
2297#ifdef CONFIG_PM
2298
2299/* USB suspend (controlled by the host) and system suspend (controlled
2300 * by the PXA) don't necessarily work well together. If USB is active,
2301 * the 48 MHz clock is required; so the system can't enter 33 MHz idle
2302 * mode, or any deeper PM saving state.
2303 *
2304 * For now, we punt and forcibly disconnect from the USB host when PXA
2305 * enters any suspend state. While we're disconnected, we always disable
David Brownell34ebcd22007-02-24 12:23:52 -08002306 * the 48MHz USB clock ... allowing PXA sleep and/or 33 MHz idle states.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 * Boards without software pullup control shouldn't use those states.
2308 * VBUS IRQs should probably be ignored so that the PXA device just acts
2309 * "dead" to USB hosts until system resume.
2310 */
Philipp Zabel7a857622008-06-22 23:36:39 +01002311static int pxa25x_udc_suspend(struct platform_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312{
Philipp Zabel7a857622008-06-22 23:36:39 +01002313 struct pxa25x_udc *udc = platform_get_drvdata(dev);
Dmitry Baryshkov64cc2dd2008-02-22 17:17:19 -08002314 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
Philipp Zabel56a075d2009-07-01 03:42:45 -07002316 if (!gpio_is_valid(udc->mach->gpio_pullup) && !udc->mach->udc_command)
Arjan van de Venb6c63932008-07-25 01:45:52 -07002317 WARNING("USB host won't detect disconnect!\n");
Dmitry Baryshkov64cc2dd2008-02-22 17:17:19 -08002318 udc->suspended = 1;
2319
2320 local_irq_save(flags);
2321 pullup(udc);
2322 local_irq_restore(flags);
Russell King9480e302005-10-28 09:52:56 -07002323
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 return 0;
2325}
2326
Philipp Zabel7a857622008-06-22 23:36:39 +01002327static int pxa25x_udc_resume(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328{
Philipp Zabel7a857622008-06-22 23:36:39 +01002329 struct pxa25x_udc *udc = platform_get_drvdata(dev);
Dmitry Baryshkov64cc2dd2008-02-22 17:17:19 -08002330 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
Dmitry Baryshkov64cc2dd2008-02-22 17:17:19 -08002332 udc->suspended = 0;
2333 local_irq_save(flags);
2334 pullup(udc);
2335 local_irq_restore(flags);
Russell King9480e302005-10-28 09:52:56 -07002336
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 return 0;
2338}
2339
2340#else
Philipp Zabel7a857622008-06-22 23:36:39 +01002341#define pxa25x_udc_suspend NULL
2342#define pxa25x_udc_resume NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343#endif
2344
2345/*-------------------------------------------------------------------------*/
2346
Russell King3ae5eae2005-11-09 22:32:44 +00002347static struct platform_driver udc_driver = {
Philipp Zabel7a857622008-06-22 23:36:39 +01002348 .shutdown = pxa25x_udc_shutdown,
2349 .remove = __exit_p(pxa25x_udc_remove),
2350 .suspend = pxa25x_udc_suspend,
2351 .resume = pxa25x_udc_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00002352 .driver = {
2353 .owner = THIS_MODULE,
Philipp Zabel7a857622008-06-22 23:36:39 +01002354 .name = "pxa25x-udc",
Russell King3ae5eae2005-11-09 22:32:44 +00002355 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356};
2357
2358static int __init udc_init(void)
2359{
David Brownell00274922007-11-19 12:58:36 -08002360 pr_info("%s: version %s\n", driver_name, DRIVER_VERSION);
Philipp Zabel7a857622008-06-22 23:36:39 +01002361 return platform_driver_probe(&udc_driver, pxa25x_udc_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362}
2363module_init(udc_init);
2364
2365static void __exit udc_exit(void)
2366{
Russell King3ae5eae2005-11-09 22:32:44 +00002367 platform_driver_unregister(&udc_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368}
2369module_exit(udc_exit);
2370
2371MODULE_DESCRIPTION(DRIVER_DESC);
2372MODULE_AUTHOR("Frank Becker, Robert Schwebel, David Brownell");
2373MODULE_LICENSE("GPL");
Philipp Zabel7a857622008-06-22 23:36:39 +01002374MODULE_ALIAS("platform:pxa25x-udc");