| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * at91_udc -- driver for at91-series USB peripheral controller | 
|  | 3 | * | 
|  | 4 | * Copyright (C) 2004 by Thomas Rathbone | 
|  | 5 | * Copyright (C) 2005 by HP Labs | 
|  | 6 | * Copyright (C) 2005 by David Brownell | 
|  | 7 | * | 
|  | 8 | * This program is free software; you can redistribute it and/or modify | 
|  | 9 | * it under the terms of the GNU General Public License as published by | 
|  | 10 | * the Free Software Foundation; either version 2 of the License, or | 
|  | 11 | * (at your option) any later version. | 
|  | 12 | * | 
|  | 13 | * This program is distributed in the hope that it will be useful, | 
|  | 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|  | 16 | * GNU General Public License for more details. | 
|  | 17 | * | 
|  | 18 | * You should have received a copy of the GNU General Public License | 
|  | 19 | * along with this program; if not, write to the | 
|  | 20 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, | 
|  | 21 | * Boston, MA  02111-1307, USA. | 
|  | 22 | */ | 
|  | 23 |  | 
| David Brownell | f3db6e8 | 2008-01-05 13:21:43 -0800 | [diff] [blame] | 24 | #undef	VERBOSE_DEBUG | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 25 | #undef	PACKET_TRACE | 
|  | 26 |  | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 27 | #include <linux/kernel.h> | 
|  | 28 | #include <linux/module.h> | 
|  | 29 | #include <linux/platform_device.h> | 
|  | 30 | #include <linux/delay.h> | 
|  | 31 | #include <linux/ioport.h> | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 32 | #include <linux/slab.h> | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 33 | #include <linux/errno.h> | 
|  | 34 | #include <linux/init.h> | 
|  | 35 | #include <linux/list.h> | 
|  | 36 | #include <linux/interrupt.h> | 
|  | 37 | #include <linux/proc_fs.h> | 
|  | 38 | #include <linux/clk.h> | 
| David Brownell | 5f84813 | 2006-12-16 15:34:53 -0800 | [diff] [blame] | 39 | #include <linux/usb/ch9.h> | 
| David Brownell | 9454a57 | 2007-10-04 18:05:17 -0700 | [diff] [blame] | 40 | #include <linux/usb/gadget.h> | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 41 |  | 
|  | 42 | #include <asm/byteorder.h> | 
| Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 43 | #include <mach/hardware.h> | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 44 | #include <asm/io.h> | 
|  | 45 | #include <asm/irq.h> | 
|  | 46 | #include <asm/system.h> | 
| David Brownell | f3db6e8 | 2008-01-05 13:21:43 -0800 | [diff] [blame] | 47 | #include <asm/gpio.h> | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 48 |  | 
| Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 49 | #include <mach/board.h> | 
|  | 50 | #include <mach/cpu.h> | 
|  | 51 | #include <mach/at91sam9261_matrix.h> | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 52 |  | 
|  | 53 | #include "at91_udc.h" | 
|  | 54 |  | 
|  | 55 |  | 
|  | 56 | /* | 
|  | 57 | * This controller is simple and PIO-only.  It's used in many AT91-series | 
| David Brownell | 8b2e766 | 2006-07-05 02:38:56 -0700 | [diff] [blame] | 58 | * full speed USB controllers, including the at91rm9200 (arm920T, with MMU), | 
|  | 59 | * at91sam926x (arm926ejs, with MMU), and several no-mmu versions. | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 60 | * | 
|  | 61 | * This driver expects the board has been wired with two GPIOs suppporting | 
|  | 62 | * a VBUS sensing IRQ, and a D+ pullup.  (They may be omitted, but the | 
| David Brownell | 8b2e766 | 2006-07-05 02:38:56 -0700 | [diff] [blame] | 63 | * testing hasn't covered such cases.) | 
|  | 64 | * | 
|  | 65 | * The pullup is most important (so it's integrated on sam926x parts).  It | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 66 | * provides software control over whether the host enumerates the device. | 
| David Brownell | 8b2e766 | 2006-07-05 02:38:56 -0700 | [diff] [blame] | 67 | * | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 68 | * The VBUS sensing helps during enumeration, and allows both USB clocks | 
|  | 69 | * (and the transceiver) to stay gated off until they're necessary, saving | 
| David Brownell | 8b2e766 | 2006-07-05 02:38:56 -0700 | [diff] [blame] | 70 | * power.  During USB suspend, the 48 MHz clock is gated off in hardware; | 
|  | 71 | * it may also be gated off by software during some Linux sleep states. | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 72 | */ | 
|  | 73 |  | 
| David Brownell | 8b2e766 | 2006-07-05 02:38:56 -0700 | [diff] [blame] | 74 | #define	DRIVER_VERSION	"3 May 2006" | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 75 |  | 
|  | 76 | static const char driver_name [] = "at91_udc"; | 
|  | 77 | static const char ep0name[] = "ep0"; | 
|  | 78 |  | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 79 |  | 
| Andrew Victor | ffd3326 | 2006-12-07 22:44:33 -0800 | [diff] [blame] | 80 | #define at91_udp_read(dev, reg) \ | 
|  | 81 | __raw_readl((dev)->udp_baseaddr + (reg)) | 
|  | 82 | #define at91_udp_write(dev, reg, val) \ | 
|  | 83 | __raw_writel((val), (dev)->udp_baseaddr + (reg)) | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 84 |  | 
|  | 85 | /*-------------------------------------------------------------------------*/ | 
|  | 86 |  | 
|  | 87 | #ifdef CONFIG_USB_GADGET_DEBUG_FILES | 
|  | 88 |  | 
|  | 89 | #include <linux/seq_file.h> | 
|  | 90 |  | 
|  | 91 | static const char debug_filename[] = "driver/udc"; | 
|  | 92 |  | 
|  | 93 | #define FOURBITS "%s%s%s%s" | 
|  | 94 | #define EIGHTBITS FOURBITS FOURBITS | 
|  | 95 |  | 
|  | 96 | static void proc_ep_show(struct seq_file *s, struct at91_ep *ep) | 
|  | 97 | { | 
|  | 98 | static char		*types[] = { | 
|  | 99 | "control", "out-iso", "out-bulk", "out-int", | 
|  | 100 | "BOGUS",   "in-iso",  "in-bulk",  "in-int"}; | 
|  | 101 |  | 
|  | 102 | u32			csr; | 
|  | 103 | struct at91_request	*req; | 
|  | 104 | unsigned long	flags; | 
|  | 105 |  | 
|  | 106 | local_irq_save(flags); | 
|  | 107 |  | 
|  | 108 | csr = __raw_readl(ep->creg); | 
|  | 109 |  | 
|  | 110 | /* NOTE:  not collecting per-endpoint irq statistics... */ | 
|  | 111 |  | 
|  | 112 | seq_printf(s, "\n"); | 
|  | 113 | seq_printf(s, "%s, maxpacket %d %s%s %s%s\n", | 
|  | 114 | ep->ep.name, ep->ep.maxpacket, | 
|  | 115 | ep->is_in ? "in" : "out", | 
|  | 116 | ep->is_iso ? " iso" : "", | 
|  | 117 | ep->is_pingpong | 
|  | 118 | ? (ep->fifo_bank ? "pong" : "ping") | 
|  | 119 | : "", | 
|  | 120 | ep->stopped ? " stopped" : ""); | 
|  | 121 | seq_printf(s, "csr %08x rxbytes=%d %s %s %s" EIGHTBITS "\n", | 
|  | 122 | csr, | 
|  | 123 | (csr & 0x07ff0000) >> 16, | 
|  | 124 | (csr & (1 << 15)) ? "enabled" : "disabled", | 
|  | 125 | (csr & (1 << 11)) ? "DATA1" : "DATA0", | 
|  | 126 | types[(csr & 0x700) >> 8], | 
|  | 127 |  | 
|  | 128 | /* iff type is control then print current direction */ | 
|  | 129 | (!(csr & 0x700)) | 
|  | 130 | ? ((csr & (1 << 7)) ? " IN" : " OUT") | 
|  | 131 | : "", | 
|  | 132 | (csr & (1 << 6)) ? " rxdatabk1" : "", | 
|  | 133 | (csr & (1 << 5)) ? " forcestall" : "", | 
|  | 134 | (csr & (1 << 4)) ? " txpktrdy" : "", | 
|  | 135 |  | 
|  | 136 | (csr & (1 << 3)) ? " stallsent" : "", | 
|  | 137 | (csr & (1 << 2)) ? " rxsetup" : "", | 
|  | 138 | (csr & (1 << 1)) ? " rxdatabk0" : "", | 
|  | 139 | (csr & (1 << 0)) ? " txcomp" : ""); | 
|  | 140 | if (list_empty (&ep->queue)) | 
|  | 141 | seq_printf(s, "\t(queue empty)\n"); | 
|  | 142 |  | 
|  | 143 | else list_for_each_entry (req, &ep->queue, queue) { | 
|  | 144 | unsigned	length = req->req.actual; | 
|  | 145 |  | 
|  | 146 | seq_printf(s, "\treq %p len %d/%d buf %p\n", | 
|  | 147 | &req->req, length, | 
|  | 148 | req->req.length, req->req.buf); | 
|  | 149 | } | 
|  | 150 | local_irq_restore(flags); | 
|  | 151 | } | 
|  | 152 |  | 
|  | 153 | static void proc_irq_show(struct seq_file *s, const char *label, u32 mask) | 
|  | 154 | { | 
|  | 155 | int i; | 
|  | 156 |  | 
|  | 157 | seq_printf(s, "%s %04x:%s%s" FOURBITS, label, mask, | 
|  | 158 | (mask & (1 << 13)) ? " wakeup" : "", | 
|  | 159 | (mask & (1 << 12)) ? " endbusres" : "", | 
|  | 160 |  | 
|  | 161 | (mask & (1 << 11)) ? " sofint" : "", | 
|  | 162 | (mask & (1 << 10)) ? " extrsm" : "", | 
|  | 163 | (mask & (1 << 9)) ? " rxrsm" : "", | 
|  | 164 | (mask & (1 << 8)) ? " rxsusp" : ""); | 
|  | 165 | for (i = 0; i < 8; i++) { | 
|  | 166 | if (mask & (1 << i)) | 
|  | 167 | seq_printf(s, " ep%d", i); | 
|  | 168 | } | 
|  | 169 | seq_printf(s, "\n"); | 
|  | 170 | } | 
|  | 171 |  | 
|  | 172 | static int proc_udc_show(struct seq_file *s, void *unused) | 
|  | 173 | { | 
|  | 174 | struct at91_udc	*udc = s->private; | 
|  | 175 | struct at91_ep	*ep; | 
|  | 176 | u32		tmp; | 
|  | 177 |  | 
|  | 178 | seq_printf(s, "%s: version %s\n", driver_name, DRIVER_VERSION); | 
|  | 179 |  | 
|  | 180 | seq_printf(s, "vbus %s, pullup %s, %s powered%s, gadget %s\n\n", | 
|  | 181 | udc->vbus ? "present" : "off", | 
|  | 182 | udc->enabled | 
|  | 183 | ? (udc->vbus ? "active" : "enabled") | 
|  | 184 | : "disabled", | 
|  | 185 | udc->selfpowered ? "self" : "VBUS", | 
|  | 186 | udc->suspended ? ", suspended" : "", | 
|  | 187 | udc->driver ? udc->driver->driver.name : "(none)"); | 
|  | 188 |  | 
|  | 189 | /* don't access registers when interface isn't clocked */ | 
|  | 190 | if (!udc->clocked) { | 
|  | 191 | seq_printf(s, "(not clocked)\n"); | 
|  | 192 | return 0; | 
|  | 193 | } | 
|  | 194 |  | 
| Andrew Victor | ffd3326 | 2006-12-07 22:44:33 -0800 | [diff] [blame] | 195 | tmp = at91_udp_read(udc, AT91_UDP_FRM_NUM); | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 196 | seq_printf(s, "frame %05x:%s%s frame=%d\n", tmp, | 
|  | 197 | (tmp & AT91_UDP_FRM_OK) ? " ok" : "", | 
|  | 198 | (tmp & AT91_UDP_FRM_ERR) ? " err" : "", | 
|  | 199 | (tmp & AT91_UDP_NUM)); | 
|  | 200 |  | 
| Andrew Victor | ffd3326 | 2006-12-07 22:44:33 -0800 | [diff] [blame] | 201 | tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT); | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 202 | seq_printf(s, "glbstate %02x:%s" FOURBITS "\n", tmp, | 
|  | 203 | (tmp & AT91_UDP_RMWUPE) ? " rmwupe" : "", | 
|  | 204 | (tmp & AT91_UDP_RSMINPR) ? " rsminpr" : "", | 
|  | 205 | (tmp & AT91_UDP_ESR) ? " esr" : "", | 
|  | 206 | (tmp & AT91_UDP_CONFG) ? " confg" : "", | 
|  | 207 | (tmp & AT91_UDP_FADDEN) ? " fadden" : ""); | 
|  | 208 |  | 
| Andrew Victor | ffd3326 | 2006-12-07 22:44:33 -0800 | [diff] [blame] | 209 | tmp = at91_udp_read(udc, AT91_UDP_FADDR); | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 210 | seq_printf(s, "faddr   %03x:%s fadd=%d\n", tmp, | 
|  | 211 | (tmp & AT91_UDP_FEN) ? " fen" : "", | 
|  | 212 | (tmp & AT91_UDP_FADD)); | 
|  | 213 |  | 
| Andrew Victor | ffd3326 | 2006-12-07 22:44:33 -0800 | [diff] [blame] | 214 | proc_irq_show(s, "imr   ", at91_udp_read(udc, AT91_UDP_IMR)); | 
|  | 215 | proc_irq_show(s, "isr   ", at91_udp_read(udc, AT91_UDP_ISR)); | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 216 |  | 
|  | 217 | if (udc->enabled && udc->vbus) { | 
|  | 218 | proc_ep_show(s, &udc->ep[0]); | 
|  | 219 | list_for_each_entry (ep, &udc->gadget.ep_list, ep.ep_list) { | 
|  | 220 | if (ep->desc) | 
|  | 221 | proc_ep_show(s, ep); | 
|  | 222 | } | 
|  | 223 | } | 
|  | 224 | return 0; | 
|  | 225 | } | 
|  | 226 |  | 
|  | 227 | static int proc_udc_open(struct inode *inode, struct file *file) | 
|  | 228 | { | 
|  | 229 | return single_open(file, proc_udc_show, PDE(inode)->data); | 
|  | 230 | } | 
|  | 231 |  | 
| Luiz Fernando N. Capitulino | 066202d | 2006-08-05 20:37:11 -0300 | [diff] [blame] | 232 | static const struct file_operations proc_ops = { | 
| Denis V. Lunev | cdefa18 | 2008-04-29 01:02:19 -0700 | [diff] [blame] | 233 | .owner		= THIS_MODULE, | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 234 | .open		= proc_udc_open, | 
|  | 235 | .read		= seq_read, | 
|  | 236 | .llseek		= seq_lseek, | 
|  | 237 | .release	= single_release, | 
|  | 238 | }; | 
|  | 239 |  | 
|  | 240 | static void create_debug_file(struct at91_udc *udc) | 
|  | 241 | { | 
| Denis V. Lunev | cdefa18 | 2008-04-29 01:02:19 -0700 | [diff] [blame] | 242 | udc->pde = proc_create_data(debug_filename, 0, NULL, &proc_ops, udc); | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 243 | } | 
|  | 244 |  | 
|  | 245 | static void remove_debug_file(struct at91_udc *udc) | 
|  | 246 | { | 
|  | 247 | if (udc->pde) | 
|  | 248 | remove_proc_entry(debug_filename, NULL); | 
|  | 249 | } | 
|  | 250 |  | 
|  | 251 | #else | 
|  | 252 |  | 
|  | 253 | static inline void create_debug_file(struct at91_udc *udc) {} | 
|  | 254 | static inline void remove_debug_file(struct at91_udc *udc) {} | 
|  | 255 |  | 
|  | 256 | #endif | 
|  | 257 |  | 
|  | 258 |  | 
|  | 259 | /*-------------------------------------------------------------------------*/ | 
|  | 260 |  | 
|  | 261 | static void done(struct at91_ep *ep, struct at91_request *req, int status) | 
|  | 262 | { | 
|  | 263 | unsigned	stopped = ep->stopped; | 
| Andrew Victor | ffd3326 | 2006-12-07 22:44:33 -0800 | [diff] [blame] | 264 | struct at91_udc	*udc = ep->udc; | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 265 |  | 
|  | 266 | list_del_init(&req->queue); | 
|  | 267 | if (req->req.status == -EINPROGRESS) | 
|  | 268 | req->req.status = status; | 
|  | 269 | else | 
|  | 270 | status = req->req.status; | 
|  | 271 | if (status && status != -ESHUTDOWN) | 
|  | 272 | VDBG("%s done %p, status %d\n", ep->ep.name, req, status); | 
|  | 273 |  | 
|  | 274 | ep->stopped = 1; | 
|  | 275 | req->req.complete(&ep->ep, &req->req); | 
|  | 276 | ep->stopped = stopped; | 
|  | 277 |  | 
|  | 278 | /* ep0 is always ready; other endpoints need a non-empty queue */ | 
|  | 279 | if (list_empty(&ep->queue) && ep->int_mask != (1 << 0)) | 
| Andrew Victor | ffd3326 | 2006-12-07 22:44:33 -0800 | [diff] [blame] | 280 | at91_udp_write(udc, AT91_UDP_IDR, ep->int_mask); | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 281 | } | 
|  | 282 |  | 
|  | 283 | /*-------------------------------------------------------------------------*/ | 
|  | 284 |  | 
|  | 285 | /* bits indicating OUT fifo has data ready */ | 
|  | 286 | #define	RX_DATA_READY	(AT91_UDP_RX_DATA_BK0 | AT91_UDP_RX_DATA_BK1) | 
|  | 287 |  | 
|  | 288 | /* | 
|  | 289 | * Endpoint FIFO CSR bits have a mix of bits, making it unsafe to just write | 
|  | 290 | * back most of the value you just read (because of side effects, including | 
|  | 291 | * bits that may change after reading and before writing). | 
|  | 292 | * | 
|  | 293 | * Except when changing a specific bit, always write values which: | 
|  | 294 | *  - clear SET_FX bits (setting them could change something) | 
|  | 295 | *  - set CLR_FX bits (clearing them could change something) | 
|  | 296 | * | 
|  | 297 | * There are also state bits like FORCESTALL, EPEDS, DIR, and EPTYPE | 
|  | 298 | * that shouldn't normally be changed. | 
| David Brownell | 8b2e766 | 2006-07-05 02:38:56 -0700 | [diff] [blame] | 299 | * | 
|  | 300 | * NOTE at91sam9260 docs mention synch between UDPCK and MCK clock domains, | 
|  | 301 | * implying a need to wait for one write to complete (test relevant bits) | 
|  | 302 | * before starting the next write.  This shouldn't be an issue given how | 
|  | 303 | * infrequently we write, except maybe for write-then-read idioms. | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 304 | */ | 
|  | 305 | #define	SET_FX	(AT91_UDP_TXPKTRDY) | 
| David Brownell | 8b2e766 | 2006-07-05 02:38:56 -0700 | [diff] [blame] | 306 | #define	CLR_FX	(RX_DATA_READY | AT91_UDP_RXSETUP \ | 
|  | 307 | | AT91_UDP_STALLSENT | AT91_UDP_TXCOMP) | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 308 |  | 
|  | 309 | /* pull OUT packet data from the endpoint's fifo */ | 
|  | 310 | static int read_fifo (struct at91_ep *ep, struct at91_request *req) | 
|  | 311 | { | 
|  | 312 | u32 __iomem	*creg = ep->creg; | 
|  | 313 | u8 __iomem	*dreg = ep->creg + (AT91_UDP_FDR(0) - AT91_UDP_CSR(0)); | 
|  | 314 | u32		csr; | 
|  | 315 | u8		*buf; | 
|  | 316 | unsigned int	count, bufferspace, is_done; | 
|  | 317 |  | 
|  | 318 | buf = req->req.buf + req->req.actual; | 
|  | 319 | bufferspace = req->req.length - req->req.actual; | 
|  | 320 |  | 
|  | 321 | /* | 
|  | 322 | * there might be nothing to read if ep_queue() calls us, | 
|  | 323 | * or if we already emptied both pingpong buffers | 
|  | 324 | */ | 
|  | 325 | rescan: | 
|  | 326 | csr = __raw_readl(creg); | 
|  | 327 | if ((csr & RX_DATA_READY) == 0) | 
|  | 328 | return 0; | 
|  | 329 |  | 
|  | 330 | count = (csr & AT91_UDP_RXBYTECNT) >> 16; | 
|  | 331 | if (count > ep->ep.maxpacket) | 
|  | 332 | count = ep->ep.maxpacket; | 
|  | 333 | if (count > bufferspace) { | 
|  | 334 | DBG("%s buffer overflow\n", ep->ep.name); | 
|  | 335 | req->req.status = -EOVERFLOW; | 
|  | 336 | count = bufferspace; | 
|  | 337 | } | 
|  | 338 | __raw_readsb(dreg, buf, count); | 
|  | 339 |  | 
|  | 340 | /* release and swap pingpong mem bank */ | 
|  | 341 | csr |= CLR_FX; | 
|  | 342 | if (ep->is_pingpong) { | 
|  | 343 | if (ep->fifo_bank == 0) { | 
|  | 344 | csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK0); | 
|  | 345 | ep->fifo_bank = 1; | 
|  | 346 | } else { | 
|  | 347 | csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK1); | 
|  | 348 | ep->fifo_bank = 0; | 
|  | 349 | } | 
|  | 350 | } else | 
|  | 351 | csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK0); | 
|  | 352 | __raw_writel(csr, creg); | 
|  | 353 |  | 
|  | 354 | req->req.actual += count; | 
|  | 355 | is_done = (count < ep->ep.maxpacket); | 
|  | 356 | if (count == bufferspace) | 
|  | 357 | is_done = 1; | 
|  | 358 |  | 
|  | 359 | PACKET("%s %p out/%d%s\n", ep->ep.name, &req->req, count, | 
|  | 360 | is_done ? " (done)" : ""); | 
|  | 361 |  | 
|  | 362 | /* | 
|  | 363 | * avoid extra trips through IRQ logic for packets already in | 
|  | 364 | * the fifo ... maybe preventing an extra (expensive) OUT-NAK | 
|  | 365 | */ | 
|  | 366 | if (is_done) | 
|  | 367 | done(ep, req, 0); | 
|  | 368 | else if (ep->is_pingpong) { | 
| Harro Haan | 7622537 | 2010-03-01 17:54:55 +0100 | [diff] [blame] | 369 | /* | 
|  | 370 | * One dummy read to delay the code because of a HW glitch: | 
|  | 371 | * CSR returns bad RXCOUNT when read too soon after updating | 
|  | 372 | * RX_DATA_BK flags. | 
|  | 373 | */ | 
|  | 374 | csr = __raw_readl(creg); | 
|  | 375 |  | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 376 | bufferspace -= count; | 
|  | 377 | buf += count; | 
|  | 378 | goto rescan; | 
|  | 379 | } | 
|  | 380 |  | 
|  | 381 | return is_done; | 
|  | 382 | } | 
|  | 383 |  | 
|  | 384 | /* load fifo for an IN packet */ | 
|  | 385 | static int write_fifo(struct at91_ep *ep, struct at91_request *req) | 
|  | 386 | { | 
|  | 387 | u32 __iomem	*creg = ep->creg; | 
|  | 388 | u32		csr = __raw_readl(creg); | 
|  | 389 | u8 __iomem	*dreg = ep->creg + (AT91_UDP_FDR(0) - AT91_UDP_CSR(0)); | 
|  | 390 | unsigned	total, count, is_last; | 
| David Brownell | 3cf2723 | 2008-04-06 23:32:55 -0700 | [diff] [blame] | 391 | u8		*buf; | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 392 |  | 
|  | 393 | /* | 
|  | 394 | * TODO: allow for writing two packets to the fifo ... that'll | 
|  | 395 | * reduce the amount of IN-NAKing, but probably won't affect | 
|  | 396 | * throughput much.  (Unlike preventing OUT-NAKing!) | 
|  | 397 | */ | 
|  | 398 |  | 
|  | 399 | /* | 
|  | 400 | * If ep_queue() calls us, the queue is empty and possibly in | 
|  | 401 | * odd states like TXCOMP not yet cleared (we do it, saving at | 
|  | 402 | * least one IRQ) or the fifo not yet being free.  Those aren't | 
|  | 403 | * issues normally (IRQ handler fast path). | 
|  | 404 | */ | 
|  | 405 | if (unlikely(csr & (AT91_UDP_TXCOMP | AT91_UDP_TXPKTRDY))) { | 
|  | 406 | if (csr & AT91_UDP_TXCOMP) { | 
|  | 407 | csr |= CLR_FX; | 
|  | 408 | csr &= ~(SET_FX | AT91_UDP_TXCOMP); | 
|  | 409 | __raw_writel(csr, creg); | 
|  | 410 | csr = __raw_readl(creg); | 
|  | 411 | } | 
|  | 412 | if (csr & AT91_UDP_TXPKTRDY) | 
|  | 413 | return 0; | 
|  | 414 | } | 
|  | 415 |  | 
| David Brownell | 3cf2723 | 2008-04-06 23:32:55 -0700 | [diff] [blame] | 416 | buf = req->req.buf + req->req.actual; | 
|  | 417 | prefetch(buf); | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 418 | total = req->req.length - req->req.actual; | 
|  | 419 | if (ep->ep.maxpacket < total) { | 
|  | 420 | count = ep->ep.maxpacket; | 
|  | 421 | is_last = 0; | 
|  | 422 | } else { | 
|  | 423 | count = total; | 
|  | 424 | is_last = (count < ep->ep.maxpacket) || !req->req.zero; | 
|  | 425 | } | 
|  | 426 |  | 
|  | 427 | /* | 
|  | 428 | * Write the packet, maybe it's a ZLP. | 
|  | 429 | * | 
|  | 430 | * NOTE:  incrementing req->actual before we receive the ACK means | 
|  | 431 | * gadget driver IN bytecounts can be wrong in fault cases.  That's | 
|  | 432 | * fixable with PIO drivers like this one (save "count" here, and | 
|  | 433 | * do the increment later on TX irq), but not for most DMA hardware. | 
|  | 434 | * | 
|  | 435 | * So all gadget drivers must accept that potential error.  Some | 
|  | 436 | * hardware supports precise fifo status reporting, letting them | 
|  | 437 | * recover when the actual bytecount matters (e.g. for USB Test | 
|  | 438 | * and Measurement Class devices). | 
|  | 439 | */ | 
| David Brownell | 3cf2723 | 2008-04-06 23:32:55 -0700 | [diff] [blame] | 440 | __raw_writesb(dreg, buf, count); | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 441 | csr &= ~SET_FX; | 
|  | 442 | csr |= CLR_FX | AT91_UDP_TXPKTRDY; | 
|  | 443 | __raw_writel(csr, creg); | 
|  | 444 | req->req.actual += count; | 
|  | 445 |  | 
|  | 446 | PACKET("%s %p in/%d%s\n", ep->ep.name, &req->req, count, | 
|  | 447 | is_last ? " (done)" : ""); | 
|  | 448 | if (is_last) | 
|  | 449 | done(ep, req, 0); | 
|  | 450 | return is_last; | 
|  | 451 | } | 
|  | 452 |  | 
|  | 453 | static void nuke(struct at91_ep *ep, int status) | 
|  | 454 | { | 
|  | 455 | struct at91_request *req; | 
|  | 456 |  | 
|  | 457 | // terminer chaque requete dans la queue | 
|  | 458 | ep->stopped = 1; | 
|  | 459 | if (list_empty(&ep->queue)) | 
|  | 460 | return; | 
|  | 461 |  | 
| Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 462 | VDBG("%s %s\n", __func__, ep->ep.name); | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 463 | while (!list_empty(&ep->queue)) { | 
|  | 464 | req = list_entry(ep->queue.next, struct at91_request, queue); | 
|  | 465 | done(ep, req, status); | 
|  | 466 | } | 
|  | 467 | } | 
|  | 468 |  | 
|  | 469 | /*-------------------------------------------------------------------------*/ | 
|  | 470 |  | 
| David Brownell | 8b2e766 | 2006-07-05 02:38:56 -0700 | [diff] [blame] | 471 | static int at91_ep_enable(struct usb_ep *_ep, | 
|  | 472 | const struct usb_endpoint_descriptor *desc) | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 473 | { | 
|  | 474 | struct at91_ep	*ep = container_of(_ep, struct at91_ep, ep); | 
|  | 475 | struct at91_udc	*dev = ep->udc; | 
|  | 476 | u16		maxpacket; | 
|  | 477 | u32		tmp; | 
|  | 478 | unsigned long	flags; | 
|  | 479 |  | 
|  | 480 | if (!_ep || !ep | 
|  | 481 | || !desc || ep->desc | 
|  | 482 | || _ep->name == ep0name | 
|  | 483 | || desc->bDescriptorType != USB_DT_ENDPOINT | 
|  | 484 | || (maxpacket = le16_to_cpu(desc->wMaxPacketSize)) == 0 | 
|  | 485 | || maxpacket > ep->maxpacket) { | 
|  | 486 | DBG("bad ep or descriptor\n"); | 
|  | 487 | return -EINVAL; | 
|  | 488 | } | 
|  | 489 |  | 
|  | 490 | if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) { | 
|  | 491 | DBG("bogus device state\n"); | 
|  | 492 | return -ESHUTDOWN; | 
|  | 493 | } | 
|  | 494 |  | 
| Matthias Kaehlcke | 81c8d8d | 2009-04-15 22:28:02 +0200 | [diff] [blame] | 495 | tmp = usb_endpoint_type(desc); | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 496 | switch (tmp) { | 
|  | 497 | case USB_ENDPOINT_XFER_CONTROL: | 
|  | 498 | DBG("only one control endpoint\n"); | 
|  | 499 | return -EINVAL; | 
|  | 500 | case USB_ENDPOINT_XFER_INT: | 
|  | 501 | if (maxpacket > 64) | 
|  | 502 | goto bogus_max; | 
|  | 503 | break; | 
|  | 504 | case USB_ENDPOINT_XFER_BULK: | 
|  | 505 | switch (maxpacket) { | 
|  | 506 | case 8: | 
|  | 507 | case 16: | 
|  | 508 | case 32: | 
|  | 509 | case 64: | 
|  | 510 | goto ok; | 
|  | 511 | } | 
|  | 512 | bogus_max: | 
|  | 513 | DBG("bogus maxpacket %d\n", maxpacket); | 
|  | 514 | return -EINVAL; | 
|  | 515 | case USB_ENDPOINT_XFER_ISOC: | 
|  | 516 | if (!ep->is_pingpong) { | 
|  | 517 | DBG("iso requires double buffering\n"); | 
|  | 518 | return -EINVAL; | 
|  | 519 | } | 
|  | 520 | break; | 
|  | 521 | } | 
|  | 522 |  | 
|  | 523 | ok: | 
|  | 524 | local_irq_save(flags); | 
|  | 525 |  | 
|  | 526 | /* initialize endpoint to match this descriptor */ | 
| Matthias Kaehlcke | 81c8d8d | 2009-04-15 22:28:02 +0200 | [diff] [blame] | 527 | ep->is_in = usb_endpoint_dir_in(desc); | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 528 | ep->is_iso = (tmp == USB_ENDPOINT_XFER_ISOC); | 
|  | 529 | ep->stopped = 0; | 
|  | 530 | if (ep->is_in) | 
|  | 531 | tmp |= 0x04; | 
|  | 532 | tmp <<= 8; | 
|  | 533 | tmp |= AT91_UDP_EPEDS; | 
|  | 534 | __raw_writel(tmp, ep->creg); | 
|  | 535 |  | 
|  | 536 | ep->desc = desc; | 
|  | 537 | ep->ep.maxpacket = maxpacket; | 
|  | 538 |  | 
|  | 539 | /* | 
|  | 540 | * reset/init endpoint fifo.  NOTE:  leaves fifo_bank alone, | 
|  | 541 | * since endpoint resets don't reset hw pingpong state. | 
|  | 542 | */ | 
| Andrew Victor | ffd3326 | 2006-12-07 22:44:33 -0800 | [diff] [blame] | 543 | at91_udp_write(dev, AT91_UDP_RST_EP, ep->int_mask); | 
|  | 544 | at91_udp_write(dev, AT91_UDP_RST_EP, 0); | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 545 |  | 
|  | 546 | local_irq_restore(flags); | 
|  | 547 | return 0; | 
|  | 548 | } | 
|  | 549 |  | 
|  | 550 | static int at91_ep_disable (struct usb_ep * _ep) | 
|  | 551 | { | 
|  | 552 | struct at91_ep	*ep = container_of(_ep, struct at91_ep, ep); | 
| Andrew Victor | ffd3326 | 2006-12-07 22:44:33 -0800 | [diff] [blame] | 553 | struct at91_udc	*udc = ep->udc; | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 554 | unsigned long	flags; | 
|  | 555 |  | 
|  | 556 | if (ep == &ep->udc->ep[0]) | 
|  | 557 | return -EINVAL; | 
|  | 558 |  | 
|  | 559 | local_irq_save(flags); | 
|  | 560 |  | 
|  | 561 | nuke(ep, -ESHUTDOWN); | 
|  | 562 |  | 
|  | 563 | /* restore the endpoint's pristine config */ | 
|  | 564 | ep->desc = NULL; | 
|  | 565 | ep->ep.maxpacket = ep->maxpacket; | 
|  | 566 |  | 
|  | 567 | /* reset fifos and endpoint */ | 
|  | 568 | if (ep->udc->clocked) { | 
| Andrew Victor | ffd3326 | 2006-12-07 22:44:33 -0800 | [diff] [blame] | 569 | at91_udp_write(udc, AT91_UDP_RST_EP, ep->int_mask); | 
|  | 570 | at91_udp_write(udc, AT91_UDP_RST_EP, 0); | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 571 | __raw_writel(0, ep->creg); | 
|  | 572 | } | 
|  | 573 |  | 
|  | 574 | local_irq_restore(flags); | 
|  | 575 | return 0; | 
|  | 576 | } | 
|  | 577 |  | 
|  | 578 | /* | 
|  | 579 | * this is a PIO-only driver, so there's nothing | 
|  | 580 | * interesting for request or buffer allocation. | 
|  | 581 | */ | 
|  | 582 |  | 
| David Brownell | 8b2e766 | 2006-07-05 02:38:56 -0700 | [diff] [blame] | 583 | static struct usb_request * | 
| David Brownell | f3db6e8 | 2008-01-05 13:21:43 -0800 | [diff] [blame] | 584 | at91_ep_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags) | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 585 | { | 
|  | 586 | struct at91_request *req; | 
|  | 587 |  | 
| Robert P. J. Day | cd86128 | 2006-12-13 00:34:52 -0800 | [diff] [blame] | 588 | req = kzalloc(sizeof (struct at91_request), gfp_flags); | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 589 | if (!req) | 
|  | 590 | return NULL; | 
|  | 591 |  | 
|  | 592 | INIT_LIST_HEAD(&req->queue); | 
|  | 593 | return &req->req; | 
|  | 594 | } | 
|  | 595 |  | 
|  | 596 | static void at91_ep_free_request(struct usb_ep *_ep, struct usb_request *_req) | 
|  | 597 | { | 
|  | 598 | struct at91_request *req; | 
|  | 599 |  | 
|  | 600 | req = container_of(_req, struct at91_request, req); | 
|  | 601 | BUG_ON(!list_empty(&req->queue)); | 
|  | 602 | kfree(req); | 
|  | 603 | } | 
|  | 604 |  | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 605 | static int at91_ep_queue(struct usb_ep *_ep, | 
|  | 606 | struct usb_request *_req, gfp_t gfp_flags) | 
|  | 607 | { | 
|  | 608 | struct at91_request	*req; | 
|  | 609 | struct at91_ep		*ep; | 
|  | 610 | struct at91_udc		*dev; | 
|  | 611 | int			status; | 
|  | 612 | unsigned long		flags; | 
|  | 613 |  | 
|  | 614 | req = container_of(_req, struct at91_request, req); | 
|  | 615 | ep = container_of(_ep, struct at91_ep, ep); | 
|  | 616 |  | 
|  | 617 | if (!_req || !_req->complete | 
|  | 618 | || !_req->buf || !list_empty(&req->queue)) { | 
|  | 619 | DBG("invalid request\n"); | 
|  | 620 | return -EINVAL; | 
|  | 621 | } | 
|  | 622 |  | 
|  | 623 | if (!_ep || (!ep->desc && ep->ep.name != ep0name)) { | 
|  | 624 | DBG("invalid ep\n"); | 
|  | 625 | return -EINVAL; | 
|  | 626 | } | 
|  | 627 |  | 
|  | 628 | dev = ep->udc; | 
|  | 629 |  | 
|  | 630 | if (!dev || !dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) { | 
|  | 631 | DBG("invalid device\n"); | 
|  | 632 | return -EINVAL; | 
|  | 633 | } | 
|  | 634 |  | 
|  | 635 | _req->status = -EINPROGRESS; | 
|  | 636 | _req->actual = 0; | 
|  | 637 |  | 
|  | 638 | local_irq_save(flags); | 
|  | 639 |  | 
|  | 640 | /* try to kickstart any empty and idle queue */ | 
|  | 641 | if (list_empty(&ep->queue) && !ep->stopped) { | 
|  | 642 | int	is_ep0; | 
|  | 643 |  | 
|  | 644 | /* | 
|  | 645 | * If this control request has a non-empty DATA stage, this | 
|  | 646 | * will start that stage.  It works just like a non-control | 
|  | 647 | * request (until the status stage starts, maybe early). | 
|  | 648 | * | 
|  | 649 | * If the data stage is empty, then this starts a successful | 
|  | 650 | * IN/STATUS stage.  (Unsuccessful ones use set_halt.) | 
|  | 651 | */ | 
|  | 652 | is_ep0 = (ep->ep.name == ep0name); | 
|  | 653 | if (is_ep0) { | 
|  | 654 | u32	tmp; | 
|  | 655 |  | 
|  | 656 | if (!dev->req_pending) { | 
|  | 657 | status = -EINVAL; | 
|  | 658 | goto done; | 
|  | 659 | } | 
|  | 660 |  | 
|  | 661 | /* | 
|  | 662 | * defer changing CONFG until after the gadget driver | 
|  | 663 | * reconfigures the endpoints. | 
|  | 664 | */ | 
|  | 665 | if (dev->wait_for_config_ack) { | 
| Andrew Victor | ffd3326 | 2006-12-07 22:44:33 -0800 | [diff] [blame] | 666 | tmp = at91_udp_read(dev, AT91_UDP_GLB_STAT); | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 667 | tmp ^= AT91_UDP_CONFG; | 
|  | 668 | VDBG("toggle config\n"); | 
| Andrew Victor | ffd3326 | 2006-12-07 22:44:33 -0800 | [diff] [blame] | 669 | at91_udp_write(dev, AT91_UDP_GLB_STAT, tmp); | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 670 | } | 
|  | 671 | if (req->req.length == 0) { | 
|  | 672 | ep0_in_status: | 
|  | 673 | PACKET("ep0 in/status\n"); | 
|  | 674 | status = 0; | 
|  | 675 | tmp = __raw_readl(ep->creg); | 
|  | 676 | tmp &= ~SET_FX; | 
|  | 677 | tmp |= CLR_FX | AT91_UDP_TXPKTRDY; | 
|  | 678 | __raw_writel(tmp, ep->creg); | 
|  | 679 | dev->req_pending = 0; | 
|  | 680 | goto done; | 
|  | 681 | } | 
|  | 682 | } | 
|  | 683 |  | 
|  | 684 | if (ep->is_in) | 
|  | 685 | status = write_fifo(ep, req); | 
|  | 686 | else { | 
|  | 687 | status = read_fifo(ep, req); | 
|  | 688 |  | 
|  | 689 | /* IN/STATUS stage is otherwise triggered by irq */ | 
|  | 690 | if (status && is_ep0) | 
|  | 691 | goto ep0_in_status; | 
|  | 692 | } | 
|  | 693 | } else | 
|  | 694 | status = 0; | 
|  | 695 |  | 
|  | 696 | if (req && !status) { | 
|  | 697 | list_add_tail (&req->queue, &ep->queue); | 
| Andrew Victor | ffd3326 | 2006-12-07 22:44:33 -0800 | [diff] [blame] | 698 | at91_udp_write(dev, AT91_UDP_IER, ep->int_mask); | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 699 | } | 
|  | 700 | done: | 
|  | 701 | local_irq_restore(flags); | 
|  | 702 | return (status < 0) ? status : 0; | 
|  | 703 | } | 
|  | 704 |  | 
|  | 705 | static int at91_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req) | 
|  | 706 | { | 
|  | 707 | struct at91_ep	*ep; | 
|  | 708 | struct at91_request	*req; | 
|  | 709 |  | 
|  | 710 | ep = container_of(_ep, struct at91_ep, ep); | 
|  | 711 | if (!_ep || ep->ep.name == ep0name) | 
|  | 712 | return -EINVAL; | 
|  | 713 |  | 
|  | 714 | /* make sure it's actually queued on this endpoint */ | 
|  | 715 | list_for_each_entry (req, &ep->queue, queue) { | 
|  | 716 | if (&req->req == _req) | 
|  | 717 | break; | 
|  | 718 | } | 
|  | 719 | if (&req->req != _req) | 
|  | 720 | return -EINVAL; | 
|  | 721 |  | 
|  | 722 | done(ep, req, -ECONNRESET); | 
|  | 723 | return 0; | 
|  | 724 | } | 
|  | 725 |  | 
|  | 726 | static int at91_ep_set_halt(struct usb_ep *_ep, int value) | 
|  | 727 | { | 
|  | 728 | struct at91_ep	*ep = container_of(_ep, struct at91_ep, ep); | 
| Andrew Victor | ffd3326 | 2006-12-07 22:44:33 -0800 | [diff] [blame] | 729 | struct at91_udc	*udc = ep->udc; | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 730 | u32 __iomem	*creg; | 
|  | 731 | u32		csr; | 
|  | 732 | unsigned long	flags; | 
|  | 733 | int		status = 0; | 
|  | 734 |  | 
|  | 735 | if (!_ep || ep->is_iso || !ep->udc->clocked) | 
|  | 736 | return -EINVAL; | 
|  | 737 |  | 
|  | 738 | creg = ep->creg; | 
|  | 739 | local_irq_save(flags); | 
|  | 740 |  | 
|  | 741 | csr = __raw_readl(creg); | 
|  | 742 |  | 
|  | 743 | /* | 
|  | 744 | * fail with still-busy IN endpoints, ensuring correct sequencing | 
|  | 745 | * of data tx then stall.  note that the fifo rx bytecount isn't | 
|  | 746 | * completely accurate as a tx bytecount. | 
|  | 747 | */ | 
|  | 748 | if (ep->is_in && (!list_empty(&ep->queue) || (csr >> 16) != 0)) | 
|  | 749 | status = -EAGAIN; | 
|  | 750 | else { | 
|  | 751 | csr |= CLR_FX; | 
|  | 752 | csr &= ~SET_FX; | 
|  | 753 | if (value) { | 
|  | 754 | csr |= AT91_UDP_FORCESTALL; | 
|  | 755 | VDBG("halt %s\n", ep->ep.name); | 
|  | 756 | } else { | 
| Andrew Victor | ffd3326 | 2006-12-07 22:44:33 -0800 | [diff] [blame] | 757 | at91_udp_write(udc, AT91_UDP_RST_EP, ep->int_mask); | 
|  | 758 | at91_udp_write(udc, AT91_UDP_RST_EP, 0); | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 759 | csr &= ~AT91_UDP_FORCESTALL; | 
|  | 760 | } | 
|  | 761 | __raw_writel(csr, creg); | 
|  | 762 | } | 
|  | 763 |  | 
|  | 764 | local_irq_restore(flags); | 
|  | 765 | return status; | 
|  | 766 | } | 
|  | 767 |  | 
| David Brownell | 398acce | 2007-02-15 18:47:17 -0800 | [diff] [blame] | 768 | static const struct usb_ep_ops at91_ep_ops = { | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 769 | .enable		= at91_ep_enable, | 
|  | 770 | .disable	= at91_ep_disable, | 
|  | 771 | .alloc_request	= at91_ep_alloc_request, | 
|  | 772 | .free_request	= at91_ep_free_request, | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 773 | .queue		= at91_ep_queue, | 
|  | 774 | .dequeue	= at91_ep_dequeue, | 
|  | 775 | .set_halt	= at91_ep_set_halt, | 
|  | 776 | // there's only imprecise fifo status reporting | 
|  | 777 | }; | 
|  | 778 |  | 
|  | 779 | /*-------------------------------------------------------------------------*/ | 
|  | 780 |  | 
|  | 781 | static int at91_get_frame(struct usb_gadget *gadget) | 
|  | 782 | { | 
| Andrew Victor | ffd3326 | 2006-12-07 22:44:33 -0800 | [diff] [blame] | 783 | struct at91_udc *udc = to_udc(gadget); | 
|  | 784 |  | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 785 | if (!to_udc(gadget)->clocked) | 
|  | 786 | return -EINVAL; | 
| Andrew Victor | ffd3326 | 2006-12-07 22:44:33 -0800 | [diff] [blame] | 787 | return at91_udp_read(udc, AT91_UDP_FRM_NUM) & AT91_UDP_NUM; | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 788 | } | 
|  | 789 |  | 
|  | 790 | static int at91_wakeup(struct usb_gadget *gadget) | 
|  | 791 | { | 
|  | 792 | struct at91_udc	*udc = to_udc(gadget); | 
|  | 793 | u32		glbstate; | 
|  | 794 | int		status = -EINVAL; | 
|  | 795 | unsigned long	flags; | 
|  | 796 |  | 
| Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 797 | DBG("%s\n", __func__ ); | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 798 | local_irq_save(flags); | 
|  | 799 |  | 
|  | 800 | if (!udc->clocked || !udc->suspended) | 
|  | 801 | goto done; | 
|  | 802 |  | 
|  | 803 | /* NOTE:  some "early versions" handle ESR differently ... */ | 
|  | 804 |  | 
| Andrew Victor | ffd3326 | 2006-12-07 22:44:33 -0800 | [diff] [blame] | 805 | glbstate = at91_udp_read(udc, AT91_UDP_GLB_STAT); | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 806 | if (!(glbstate & AT91_UDP_ESR)) | 
|  | 807 | goto done; | 
|  | 808 | glbstate |= AT91_UDP_ESR; | 
| Andrew Victor | ffd3326 | 2006-12-07 22:44:33 -0800 | [diff] [blame] | 809 | at91_udp_write(udc, AT91_UDP_GLB_STAT, glbstate); | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 810 |  | 
|  | 811 | done: | 
|  | 812 | local_irq_restore(flags); | 
|  | 813 | return status; | 
|  | 814 | } | 
|  | 815 |  | 
|  | 816 | /* reinit == restore inital software state */ | 
|  | 817 | static void udc_reinit(struct at91_udc *udc) | 
|  | 818 | { | 
|  | 819 | u32 i; | 
|  | 820 |  | 
|  | 821 | INIT_LIST_HEAD(&udc->gadget.ep_list); | 
|  | 822 | INIT_LIST_HEAD(&udc->gadget.ep0->ep_list); | 
|  | 823 |  | 
|  | 824 | for (i = 0; i < NUM_ENDPOINTS; i++) { | 
|  | 825 | struct at91_ep *ep = &udc->ep[i]; | 
|  | 826 |  | 
|  | 827 | if (i != 0) | 
|  | 828 | list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list); | 
|  | 829 | ep->desc = NULL; | 
|  | 830 | ep->stopped = 0; | 
|  | 831 | ep->fifo_bank = 0; | 
|  | 832 | ep->ep.maxpacket = ep->maxpacket; | 
| Andrew Victor | ffd3326 | 2006-12-07 22:44:33 -0800 | [diff] [blame] | 833 | ep->creg = (void __iomem *) udc->udp_baseaddr + AT91_UDP_CSR(i); | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 834 | // initialiser une queue par endpoint | 
|  | 835 | INIT_LIST_HEAD(&ep->queue); | 
|  | 836 | } | 
|  | 837 | } | 
|  | 838 |  | 
|  | 839 | static void stop_activity(struct at91_udc *udc) | 
|  | 840 | { | 
|  | 841 | struct usb_gadget_driver *driver = udc->driver; | 
|  | 842 | int i; | 
|  | 843 |  | 
|  | 844 | if (udc->gadget.speed == USB_SPEED_UNKNOWN) | 
|  | 845 | driver = NULL; | 
|  | 846 | udc->gadget.speed = USB_SPEED_UNKNOWN; | 
| David Brownell | 8b2e766 | 2006-07-05 02:38:56 -0700 | [diff] [blame] | 847 | udc->suspended = 0; | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 848 |  | 
|  | 849 | for (i = 0; i < NUM_ENDPOINTS; i++) { | 
|  | 850 | struct at91_ep *ep = &udc->ep[i]; | 
|  | 851 | ep->stopped = 1; | 
|  | 852 | nuke(ep, -ESHUTDOWN); | 
|  | 853 | } | 
|  | 854 | if (driver) | 
|  | 855 | driver->disconnect(&udc->gadget); | 
|  | 856 |  | 
|  | 857 | udc_reinit(udc); | 
|  | 858 | } | 
|  | 859 |  | 
|  | 860 | static void clk_on(struct at91_udc *udc) | 
|  | 861 | { | 
|  | 862 | if (udc->clocked) | 
|  | 863 | return; | 
|  | 864 | udc->clocked = 1; | 
|  | 865 | clk_enable(udc->iclk); | 
|  | 866 | clk_enable(udc->fclk); | 
|  | 867 | } | 
|  | 868 |  | 
|  | 869 | static void clk_off(struct at91_udc *udc) | 
|  | 870 | { | 
|  | 871 | if (!udc->clocked) | 
|  | 872 | return; | 
|  | 873 | udc->clocked = 0; | 
|  | 874 | udc->gadget.speed = USB_SPEED_UNKNOWN; | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 875 | clk_disable(udc->fclk); | 
| David Brownell | 8b2e766 | 2006-07-05 02:38:56 -0700 | [diff] [blame] | 876 | clk_disable(udc->iclk); | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 877 | } | 
|  | 878 |  | 
|  | 879 | /* | 
|  | 880 | * activate/deactivate link with host; minimize power usage for | 
|  | 881 | * inactive links by cutting clocks and transceiver power. | 
|  | 882 | */ | 
|  | 883 | static void pullup(struct at91_udc *udc, int is_on) | 
|  | 884 | { | 
| David Brownell | f3db6e8 | 2008-01-05 13:21:43 -0800 | [diff] [blame] | 885 | int	active = !udc->board.pullup_active_low; | 
|  | 886 |  | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 887 | if (!udc->enabled || !udc->vbus) | 
|  | 888 | is_on = 0; | 
|  | 889 | DBG("%sactive\n", is_on ? "" : "in"); | 
| Andrew Victor | ffd3326 | 2006-12-07 22:44:33 -0800 | [diff] [blame] | 890 |  | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 891 | if (is_on) { | 
|  | 892 | clk_on(udc); | 
| Nicolas Ferre | 08cbc70 | 2007-12-13 15:52:58 -0800 | [diff] [blame] | 893 | at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_RXRSM); | 
| Andrew Victor | ffd3326 | 2006-12-07 22:44:33 -0800 | [diff] [blame] | 894 | at91_udp_write(udc, AT91_UDP_TXVC, 0); | 
| Andrew Victor | 29ba4b5 | 2006-12-07 22:44:38 -0800 | [diff] [blame] | 895 | if (cpu_is_at91rm9200()) | 
| David Brownell | f3db6e8 | 2008-01-05 13:21:43 -0800 | [diff] [blame] | 896 | gpio_set_value(udc->board.pullup_pin, active); | 
| sedji gaouaou | 6135266 | 2008-07-10 10:15:35 +0100 | [diff] [blame] | 897 | else if (cpu_is_at91sam9260() || cpu_is_at91sam9263() || cpu_is_at91sam9g20()) { | 
| Andrew Victor | 29ba4b5 | 2006-12-07 22:44:38 -0800 | [diff] [blame] | 898 | u32	txvc = at91_udp_read(udc, AT91_UDP_TXVC); | 
|  | 899 |  | 
|  | 900 | txvc |= AT91_UDP_TXVC_PUON; | 
|  | 901 | at91_udp_write(udc, AT91_UDP_TXVC, txvc); | 
| Hong Xu | 23f6d91 | 2009-09-25 12:24:12 +0200 | [diff] [blame] | 902 | } else if (cpu_is_at91sam9261() || cpu_is_at91sam9g10()) { | 
| Andrew Victor | 29ba4b5 | 2006-12-07 22:44:38 -0800 | [diff] [blame] | 903 | u32	usbpucr; | 
|  | 904 |  | 
|  | 905 | usbpucr = at91_sys_read(AT91_MATRIX_USBPUCR); | 
|  | 906 | usbpucr |= AT91_MATRIX_USBPUCR_PUON; | 
|  | 907 | at91_sys_write(AT91_MATRIX_USBPUCR, usbpucr); | 
|  | 908 | } | 
| Andrew Victor | ffd3326 | 2006-12-07 22:44:33 -0800 | [diff] [blame] | 909 | } else { | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 910 | stop_activity(udc); | 
| Nicolas Ferre | 08cbc70 | 2007-12-13 15:52:58 -0800 | [diff] [blame] | 911 | at91_udp_write(udc, AT91_UDP_IDR, AT91_UDP_RXRSM); | 
| Andrew Victor | ffd3326 | 2006-12-07 22:44:33 -0800 | [diff] [blame] | 912 | at91_udp_write(udc, AT91_UDP_TXVC, AT91_UDP_TXVC_TXVDIS); | 
| Andrew Victor | 29ba4b5 | 2006-12-07 22:44:38 -0800 | [diff] [blame] | 913 | if (cpu_is_at91rm9200()) | 
| David Brownell | f3db6e8 | 2008-01-05 13:21:43 -0800 | [diff] [blame] | 914 | gpio_set_value(udc->board.pullup_pin, !active); | 
| sedji gaouaou | 6135266 | 2008-07-10 10:15:35 +0100 | [diff] [blame] | 915 | else if (cpu_is_at91sam9260() || cpu_is_at91sam9263() || cpu_is_at91sam9g20()) { | 
| Andrew Victor | 29ba4b5 | 2006-12-07 22:44:38 -0800 | [diff] [blame] | 916 | u32	txvc = at91_udp_read(udc, AT91_UDP_TXVC); | 
|  | 917 |  | 
|  | 918 | txvc &= ~AT91_UDP_TXVC_PUON; | 
|  | 919 | at91_udp_write(udc, AT91_UDP_TXVC, txvc); | 
| Hong Xu | 23f6d91 | 2009-09-25 12:24:12 +0200 | [diff] [blame] | 920 | } else if (cpu_is_at91sam9261() || cpu_is_at91sam9g10()) { | 
| Andrew Victor | 29ba4b5 | 2006-12-07 22:44:38 -0800 | [diff] [blame] | 921 | u32	usbpucr; | 
|  | 922 |  | 
|  | 923 | usbpucr = at91_sys_read(AT91_MATRIX_USBPUCR); | 
|  | 924 | usbpucr &= ~AT91_MATRIX_USBPUCR_PUON; | 
|  | 925 | at91_sys_write(AT91_MATRIX_USBPUCR, usbpucr); | 
|  | 926 | } | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 927 | clk_off(udc); | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 928 | } | 
|  | 929 | } | 
|  | 930 |  | 
|  | 931 | /* vbus is here!  turn everything on that's ready */ | 
|  | 932 | static int at91_vbus_session(struct usb_gadget *gadget, int is_active) | 
|  | 933 | { | 
|  | 934 | struct at91_udc	*udc = to_udc(gadget); | 
|  | 935 | unsigned long	flags; | 
|  | 936 |  | 
|  | 937 | // VDBG("vbus %s\n", is_active ? "on" : "off"); | 
|  | 938 | local_irq_save(flags); | 
|  | 939 | udc->vbus = (is_active != 0); | 
| Wojtek Kaniewski | bfb7fb7 | 2006-12-08 03:26:00 -0800 | [diff] [blame] | 940 | if (udc->driver) | 
|  | 941 | pullup(udc, is_active); | 
|  | 942 | else | 
|  | 943 | pullup(udc, 0); | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 944 | local_irq_restore(flags); | 
|  | 945 | return 0; | 
|  | 946 | } | 
|  | 947 |  | 
|  | 948 | static int at91_pullup(struct usb_gadget *gadget, int is_on) | 
|  | 949 | { | 
|  | 950 | struct at91_udc	*udc = to_udc(gadget); | 
|  | 951 | unsigned long	flags; | 
|  | 952 |  | 
|  | 953 | local_irq_save(flags); | 
|  | 954 | udc->enabled = is_on = !!is_on; | 
|  | 955 | pullup(udc, is_on); | 
|  | 956 | local_irq_restore(flags); | 
|  | 957 | return 0; | 
|  | 958 | } | 
|  | 959 |  | 
|  | 960 | static int at91_set_selfpowered(struct usb_gadget *gadget, int is_on) | 
|  | 961 | { | 
|  | 962 | struct at91_udc	*udc = to_udc(gadget); | 
|  | 963 | unsigned long	flags; | 
|  | 964 |  | 
|  | 965 | local_irq_save(flags); | 
|  | 966 | udc->selfpowered = (is_on != 0); | 
|  | 967 | local_irq_restore(flags); | 
|  | 968 | return 0; | 
|  | 969 | } | 
|  | 970 |  | 
|  | 971 | static const struct usb_gadget_ops at91_udc_ops = { | 
|  | 972 | .get_frame		= at91_get_frame, | 
|  | 973 | .wakeup			= at91_wakeup, | 
|  | 974 | .set_selfpowered	= at91_set_selfpowered, | 
|  | 975 | .vbus_session		= at91_vbus_session, | 
|  | 976 | .pullup			= at91_pullup, | 
|  | 977 |  | 
|  | 978 | /* | 
|  | 979 | * VBUS-powered devices may also also want to support bigger | 
|  | 980 | * power budgets after an appropriate SET_CONFIGURATION. | 
|  | 981 | */ | 
|  | 982 | // .vbus_power		= at91_vbus_power, | 
|  | 983 | }; | 
|  | 984 |  | 
|  | 985 | /*-------------------------------------------------------------------------*/ | 
|  | 986 |  | 
|  | 987 | static int handle_ep(struct at91_ep *ep) | 
|  | 988 | { | 
|  | 989 | struct at91_request	*req; | 
|  | 990 | u32 __iomem		*creg = ep->creg; | 
|  | 991 | u32			csr = __raw_readl(creg); | 
|  | 992 |  | 
|  | 993 | if (!list_empty(&ep->queue)) | 
|  | 994 | req = list_entry(ep->queue.next, | 
|  | 995 | struct at91_request, queue); | 
|  | 996 | else | 
|  | 997 | req = NULL; | 
|  | 998 |  | 
|  | 999 | if (ep->is_in) { | 
|  | 1000 | if (csr & (AT91_UDP_STALLSENT | AT91_UDP_TXCOMP)) { | 
|  | 1001 | csr |= CLR_FX; | 
|  | 1002 | csr &= ~(SET_FX | AT91_UDP_STALLSENT | AT91_UDP_TXCOMP); | 
|  | 1003 | __raw_writel(csr, creg); | 
|  | 1004 | } | 
|  | 1005 | if (req) | 
|  | 1006 | return write_fifo(ep, req); | 
|  | 1007 |  | 
|  | 1008 | } else { | 
|  | 1009 | if (csr & AT91_UDP_STALLSENT) { | 
|  | 1010 | /* STALLSENT bit == ISOERR */ | 
|  | 1011 | if (ep->is_iso && req) | 
|  | 1012 | req->req.status = -EILSEQ; | 
|  | 1013 | csr |= CLR_FX; | 
|  | 1014 | csr &= ~(SET_FX | AT91_UDP_STALLSENT); | 
|  | 1015 | __raw_writel(csr, creg); | 
|  | 1016 | csr = __raw_readl(creg); | 
|  | 1017 | } | 
|  | 1018 | if (req && (csr & RX_DATA_READY)) | 
|  | 1019 | return read_fifo(ep, req); | 
|  | 1020 | } | 
|  | 1021 | return 0; | 
|  | 1022 | } | 
|  | 1023 |  | 
|  | 1024 | union setup { | 
|  | 1025 | u8			raw[8]; | 
|  | 1026 | struct usb_ctrlrequest	r; | 
|  | 1027 | }; | 
|  | 1028 |  | 
|  | 1029 | static void handle_setup(struct at91_udc *udc, struct at91_ep *ep, u32 csr) | 
|  | 1030 | { | 
|  | 1031 | u32 __iomem	*creg = ep->creg; | 
|  | 1032 | u8 __iomem	*dreg = ep->creg + (AT91_UDP_FDR(0) - AT91_UDP_CSR(0)); | 
|  | 1033 | unsigned	rxcount, i = 0; | 
|  | 1034 | u32		tmp; | 
|  | 1035 | union setup	pkt; | 
|  | 1036 | int		status = 0; | 
|  | 1037 |  | 
|  | 1038 | /* read and ack SETUP; hard-fail for bogus packets */ | 
|  | 1039 | rxcount = (csr & AT91_UDP_RXBYTECNT) >> 16; | 
|  | 1040 | if (likely(rxcount == 8)) { | 
|  | 1041 | while (rxcount--) | 
|  | 1042 | pkt.raw[i++] = __raw_readb(dreg); | 
|  | 1043 | if (pkt.r.bRequestType & USB_DIR_IN) { | 
|  | 1044 | csr |= AT91_UDP_DIR; | 
|  | 1045 | ep->is_in = 1; | 
|  | 1046 | } else { | 
|  | 1047 | csr &= ~AT91_UDP_DIR; | 
|  | 1048 | ep->is_in = 0; | 
|  | 1049 | } | 
|  | 1050 | } else { | 
|  | 1051 | // REVISIT this happens sometimes under load; why?? | 
|  | 1052 | ERR("SETUP len %d, csr %08x\n", rxcount, csr); | 
|  | 1053 | status = -EINVAL; | 
|  | 1054 | } | 
|  | 1055 | csr |= CLR_FX; | 
|  | 1056 | csr &= ~(SET_FX | AT91_UDP_RXSETUP); | 
|  | 1057 | __raw_writel(csr, creg); | 
|  | 1058 | udc->wait_for_addr_ack = 0; | 
|  | 1059 | udc->wait_for_config_ack = 0; | 
|  | 1060 | ep->stopped = 0; | 
|  | 1061 | if (unlikely(status != 0)) | 
|  | 1062 | goto stall; | 
|  | 1063 |  | 
|  | 1064 | #define w_index		le16_to_cpu(pkt.r.wIndex) | 
|  | 1065 | #define w_value		le16_to_cpu(pkt.r.wValue) | 
|  | 1066 | #define w_length	le16_to_cpu(pkt.r.wLength) | 
|  | 1067 |  | 
|  | 1068 | VDBG("SETUP %02x.%02x v%04x i%04x l%04x\n", | 
|  | 1069 | pkt.r.bRequestType, pkt.r.bRequest, | 
|  | 1070 | w_value, w_index, w_length); | 
|  | 1071 |  | 
|  | 1072 | /* | 
|  | 1073 | * A few standard requests get handled here, ones that touch | 
|  | 1074 | * hardware ... notably for device and endpoint features. | 
|  | 1075 | */ | 
|  | 1076 | udc->req_pending = 1; | 
|  | 1077 | csr = __raw_readl(creg); | 
|  | 1078 | csr |= CLR_FX; | 
|  | 1079 | csr &= ~SET_FX; | 
|  | 1080 | switch ((pkt.r.bRequestType << 8) | pkt.r.bRequest) { | 
|  | 1081 |  | 
|  | 1082 | case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8) | 
|  | 1083 | | USB_REQ_SET_ADDRESS: | 
|  | 1084 | __raw_writel(csr | AT91_UDP_TXPKTRDY, creg); | 
|  | 1085 | udc->addr = w_value; | 
|  | 1086 | udc->wait_for_addr_ack = 1; | 
|  | 1087 | udc->req_pending = 0; | 
|  | 1088 | /* FADDR is set later, when we ack host STATUS */ | 
|  | 1089 | return; | 
|  | 1090 |  | 
|  | 1091 | case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8) | 
|  | 1092 | | USB_REQ_SET_CONFIGURATION: | 
| Andrew Victor | ffd3326 | 2006-12-07 22:44:33 -0800 | [diff] [blame] | 1093 | tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT) & AT91_UDP_CONFG; | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1094 | if (pkt.r.wValue) | 
|  | 1095 | udc->wait_for_config_ack = (tmp == 0); | 
|  | 1096 | else | 
|  | 1097 | udc->wait_for_config_ack = (tmp != 0); | 
|  | 1098 | if (udc->wait_for_config_ack) | 
|  | 1099 | VDBG("wait for config\n"); | 
|  | 1100 | /* CONFG is toggled later, if gadget driver succeeds */ | 
|  | 1101 | break; | 
|  | 1102 |  | 
|  | 1103 | /* | 
|  | 1104 | * Hosts may set or clear remote wakeup status, and | 
|  | 1105 | * devices may report they're VBUS powered. | 
|  | 1106 | */ | 
|  | 1107 | case ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8) | 
|  | 1108 | | USB_REQ_GET_STATUS: | 
|  | 1109 | tmp = (udc->selfpowered << USB_DEVICE_SELF_POWERED); | 
| Andrew Victor | ffd3326 | 2006-12-07 22:44:33 -0800 | [diff] [blame] | 1110 | if (at91_udp_read(udc, AT91_UDP_GLB_STAT) & AT91_UDP_ESR) | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1111 | tmp |= (1 << USB_DEVICE_REMOTE_WAKEUP); | 
|  | 1112 | PACKET("get device status\n"); | 
|  | 1113 | __raw_writeb(tmp, dreg); | 
|  | 1114 | __raw_writeb(0, dreg); | 
|  | 1115 | goto write_in; | 
|  | 1116 | /* then STATUS starts later, automatically */ | 
|  | 1117 | case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8) | 
|  | 1118 | | USB_REQ_SET_FEATURE: | 
|  | 1119 | if (w_value != USB_DEVICE_REMOTE_WAKEUP) | 
|  | 1120 | goto stall; | 
| Andrew Victor | ffd3326 | 2006-12-07 22:44:33 -0800 | [diff] [blame] | 1121 | tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT); | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1122 | tmp |= AT91_UDP_ESR; | 
| Andrew Victor | ffd3326 | 2006-12-07 22:44:33 -0800 | [diff] [blame] | 1123 | at91_udp_write(udc, AT91_UDP_GLB_STAT, tmp); | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1124 | goto succeed; | 
|  | 1125 | case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8) | 
|  | 1126 | | USB_REQ_CLEAR_FEATURE: | 
|  | 1127 | if (w_value != USB_DEVICE_REMOTE_WAKEUP) | 
|  | 1128 | goto stall; | 
| Andrew Victor | ffd3326 | 2006-12-07 22:44:33 -0800 | [diff] [blame] | 1129 | tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT); | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1130 | tmp &= ~AT91_UDP_ESR; | 
| Andrew Victor | ffd3326 | 2006-12-07 22:44:33 -0800 | [diff] [blame] | 1131 | at91_udp_write(udc, AT91_UDP_GLB_STAT, tmp); | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1132 | goto succeed; | 
|  | 1133 |  | 
|  | 1134 | /* | 
|  | 1135 | * Interfaces have no feature settings; this is pretty useless. | 
|  | 1136 | * we won't even insist the interface exists... | 
|  | 1137 | */ | 
|  | 1138 | case ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_INTERFACE) << 8) | 
|  | 1139 | | USB_REQ_GET_STATUS: | 
|  | 1140 | PACKET("get interface status\n"); | 
|  | 1141 | __raw_writeb(0, dreg); | 
|  | 1142 | __raw_writeb(0, dreg); | 
|  | 1143 | goto write_in; | 
|  | 1144 | /* then STATUS starts later, automatically */ | 
|  | 1145 | case ((USB_TYPE_STANDARD|USB_RECIP_INTERFACE) << 8) | 
|  | 1146 | | USB_REQ_SET_FEATURE: | 
|  | 1147 | case ((USB_TYPE_STANDARD|USB_RECIP_INTERFACE) << 8) | 
|  | 1148 | | USB_REQ_CLEAR_FEATURE: | 
|  | 1149 | goto stall; | 
|  | 1150 |  | 
|  | 1151 | /* | 
|  | 1152 | * Hosts may clear bulk/intr endpoint halt after the gadget | 
|  | 1153 | * driver sets it (not widely used); or set it (for testing) | 
|  | 1154 | */ | 
|  | 1155 | case ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_ENDPOINT) << 8) | 
|  | 1156 | | USB_REQ_GET_STATUS: | 
|  | 1157 | tmp = w_index & USB_ENDPOINT_NUMBER_MASK; | 
|  | 1158 | ep = &udc->ep[tmp]; | 
| David Brownell | 1440e09 | 2007-12-09 22:53:09 -0800 | [diff] [blame] | 1159 | if (tmp >= NUM_ENDPOINTS || (tmp && !ep->desc)) | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1160 | goto stall; | 
|  | 1161 |  | 
|  | 1162 | if (tmp) { | 
|  | 1163 | if ((w_index & USB_DIR_IN)) { | 
|  | 1164 | if (!ep->is_in) | 
|  | 1165 | goto stall; | 
|  | 1166 | } else if (ep->is_in) | 
|  | 1167 | goto stall; | 
|  | 1168 | } | 
|  | 1169 | PACKET("get %s status\n", ep->ep.name); | 
|  | 1170 | if (__raw_readl(ep->creg) & AT91_UDP_FORCESTALL) | 
|  | 1171 | tmp = (1 << USB_ENDPOINT_HALT); | 
|  | 1172 | else | 
|  | 1173 | tmp = 0; | 
|  | 1174 | __raw_writeb(tmp, dreg); | 
|  | 1175 | __raw_writeb(0, dreg); | 
|  | 1176 | goto write_in; | 
|  | 1177 | /* then STATUS starts later, automatically */ | 
|  | 1178 | case ((USB_TYPE_STANDARD|USB_RECIP_ENDPOINT) << 8) | 
|  | 1179 | | USB_REQ_SET_FEATURE: | 
|  | 1180 | tmp = w_index & USB_ENDPOINT_NUMBER_MASK; | 
|  | 1181 | ep = &udc->ep[tmp]; | 
| David Brownell | 1440e09 | 2007-12-09 22:53:09 -0800 | [diff] [blame] | 1182 | if (w_value != USB_ENDPOINT_HALT || tmp >= NUM_ENDPOINTS) | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1183 | goto stall; | 
|  | 1184 | if (!ep->desc || ep->is_iso) | 
|  | 1185 | goto stall; | 
|  | 1186 | if ((w_index & USB_DIR_IN)) { | 
|  | 1187 | if (!ep->is_in) | 
|  | 1188 | goto stall; | 
|  | 1189 | } else if (ep->is_in) | 
|  | 1190 | goto stall; | 
|  | 1191 |  | 
|  | 1192 | tmp = __raw_readl(ep->creg); | 
|  | 1193 | tmp &= ~SET_FX; | 
|  | 1194 | tmp |= CLR_FX | AT91_UDP_FORCESTALL; | 
|  | 1195 | __raw_writel(tmp, ep->creg); | 
|  | 1196 | goto succeed; | 
|  | 1197 | case ((USB_TYPE_STANDARD|USB_RECIP_ENDPOINT) << 8) | 
|  | 1198 | | USB_REQ_CLEAR_FEATURE: | 
|  | 1199 | tmp = w_index & USB_ENDPOINT_NUMBER_MASK; | 
|  | 1200 | ep = &udc->ep[tmp]; | 
| David Brownell | 1440e09 | 2007-12-09 22:53:09 -0800 | [diff] [blame] | 1201 | if (w_value != USB_ENDPOINT_HALT || tmp >= NUM_ENDPOINTS) | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1202 | goto stall; | 
|  | 1203 | if (tmp == 0) | 
|  | 1204 | goto succeed; | 
|  | 1205 | if (!ep->desc || ep->is_iso) | 
|  | 1206 | goto stall; | 
|  | 1207 | if ((w_index & USB_DIR_IN)) { | 
|  | 1208 | if (!ep->is_in) | 
|  | 1209 | goto stall; | 
|  | 1210 | } else if (ep->is_in) | 
|  | 1211 | goto stall; | 
|  | 1212 |  | 
| Andrew Victor | ffd3326 | 2006-12-07 22:44:33 -0800 | [diff] [blame] | 1213 | at91_udp_write(udc, AT91_UDP_RST_EP, ep->int_mask); | 
|  | 1214 | at91_udp_write(udc, AT91_UDP_RST_EP, 0); | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1215 | tmp = __raw_readl(ep->creg); | 
|  | 1216 | tmp |= CLR_FX; | 
|  | 1217 | tmp &= ~(SET_FX | AT91_UDP_FORCESTALL); | 
|  | 1218 | __raw_writel(tmp, ep->creg); | 
|  | 1219 | if (!list_empty(&ep->queue)) | 
|  | 1220 | handle_ep(ep); | 
|  | 1221 | goto succeed; | 
|  | 1222 | } | 
|  | 1223 |  | 
|  | 1224 | #undef w_value | 
|  | 1225 | #undef w_index | 
|  | 1226 | #undef w_length | 
|  | 1227 |  | 
|  | 1228 | /* pass request up to the gadget driver */ | 
| Wojtek Kaniewski | bfb7fb7 | 2006-12-08 03:26:00 -0800 | [diff] [blame] | 1229 | if (udc->driver) | 
|  | 1230 | status = udc->driver->setup(&udc->gadget, &pkt.r); | 
|  | 1231 | else | 
|  | 1232 | status = -ENODEV; | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1233 | if (status < 0) { | 
|  | 1234 | stall: | 
|  | 1235 | VDBG("req %02x.%02x protocol STALL; stat %d\n", | 
|  | 1236 | pkt.r.bRequestType, pkt.r.bRequest, status); | 
|  | 1237 | csr |= AT91_UDP_FORCESTALL; | 
|  | 1238 | __raw_writel(csr, creg); | 
|  | 1239 | udc->req_pending = 0; | 
|  | 1240 | } | 
|  | 1241 | return; | 
|  | 1242 |  | 
|  | 1243 | succeed: | 
|  | 1244 | /* immediate successful (IN) STATUS after zero length DATA */ | 
|  | 1245 | PACKET("ep0 in/status\n"); | 
|  | 1246 | write_in: | 
|  | 1247 | csr |= AT91_UDP_TXPKTRDY; | 
|  | 1248 | __raw_writel(csr, creg); | 
|  | 1249 | udc->req_pending = 0; | 
|  | 1250 | return; | 
|  | 1251 | } | 
|  | 1252 |  | 
|  | 1253 | static void handle_ep0(struct at91_udc *udc) | 
|  | 1254 | { | 
|  | 1255 | struct at91_ep		*ep0 = &udc->ep[0]; | 
|  | 1256 | u32 __iomem		*creg = ep0->creg; | 
|  | 1257 | u32			csr = __raw_readl(creg); | 
|  | 1258 | struct at91_request	*req; | 
|  | 1259 |  | 
|  | 1260 | if (unlikely(csr & AT91_UDP_STALLSENT)) { | 
|  | 1261 | nuke(ep0, -EPROTO); | 
|  | 1262 | udc->req_pending = 0; | 
|  | 1263 | csr |= CLR_FX; | 
|  | 1264 | csr &= ~(SET_FX | AT91_UDP_STALLSENT | AT91_UDP_FORCESTALL); | 
|  | 1265 | __raw_writel(csr, creg); | 
|  | 1266 | VDBG("ep0 stalled\n"); | 
|  | 1267 | csr = __raw_readl(creg); | 
|  | 1268 | } | 
|  | 1269 | if (csr & AT91_UDP_RXSETUP) { | 
|  | 1270 | nuke(ep0, 0); | 
|  | 1271 | udc->req_pending = 0; | 
|  | 1272 | handle_setup(udc, ep0, csr); | 
|  | 1273 | return; | 
|  | 1274 | } | 
|  | 1275 |  | 
|  | 1276 | if (list_empty(&ep0->queue)) | 
|  | 1277 | req = NULL; | 
|  | 1278 | else | 
|  | 1279 | req = list_entry(ep0->queue.next, struct at91_request, queue); | 
|  | 1280 |  | 
|  | 1281 | /* host ACKed an IN packet that we sent */ | 
|  | 1282 | if (csr & AT91_UDP_TXCOMP) { | 
|  | 1283 | csr |= CLR_FX; | 
|  | 1284 | csr &= ~(SET_FX | AT91_UDP_TXCOMP); | 
|  | 1285 |  | 
|  | 1286 | /* write more IN DATA? */ | 
|  | 1287 | if (req && ep0->is_in) { | 
|  | 1288 | if (handle_ep(ep0)) | 
|  | 1289 | udc->req_pending = 0; | 
|  | 1290 |  | 
|  | 1291 | /* | 
|  | 1292 | * Ack after: | 
|  | 1293 | *  - last IN DATA packet (including GET_STATUS) | 
|  | 1294 | *  - IN/STATUS for OUT DATA | 
|  | 1295 | *  - IN/STATUS for any zero-length DATA stage | 
|  | 1296 | * except for the IN DATA case, the host should send | 
|  | 1297 | * an OUT status later, which we'll ack. | 
|  | 1298 | */ | 
|  | 1299 | } else { | 
|  | 1300 | udc->req_pending = 0; | 
|  | 1301 | __raw_writel(csr, creg); | 
|  | 1302 |  | 
|  | 1303 | /* | 
|  | 1304 | * SET_ADDRESS takes effect only after the STATUS | 
|  | 1305 | * (to the original address) gets acked. | 
|  | 1306 | */ | 
|  | 1307 | if (udc->wait_for_addr_ack) { | 
|  | 1308 | u32	tmp; | 
|  | 1309 |  | 
| Andrew Victor | ffd3326 | 2006-12-07 22:44:33 -0800 | [diff] [blame] | 1310 | at91_udp_write(udc, AT91_UDP_FADDR, | 
| David Brownell | 8b2e766 | 2006-07-05 02:38:56 -0700 | [diff] [blame] | 1311 | AT91_UDP_FEN | udc->addr); | 
| Andrew Victor | ffd3326 | 2006-12-07 22:44:33 -0800 | [diff] [blame] | 1312 | tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT); | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1313 | tmp &= ~AT91_UDP_FADDEN; | 
|  | 1314 | if (udc->addr) | 
|  | 1315 | tmp |= AT91_UDP_FADDEN; | 
| Andrew Victor | ffd3326 | 2006-12-07 22:44:33 -0800 | [diff] [blame] | 1316 | at91_udp_write(udc, AT91_UDP_GLB_STAT, tmp); | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1317 |  | 
|  | 1318 | udc->wait_for_addr_ack = 0; | 
|  | 1319 | VDBG("address %d\n", udc->addr); | 
|  | 1320 | } | 
|  | 1321 | } | 
|  | 1322 | } | 
|  | 1323 |  | 
|  | 1324 | /* OUT packet arrived ... */ | 
|  | 1325 | else if (csr & AT91_UDP_RX_DATA_BK0) { | 
|  | 1326 | csr |= CLR_FX; | 
|  | 1327 | csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK0); | 
|  | 1328 |  | 
|  | 1329 | /* OUT DATA stage */ | 
|  | 1330 | if (!ep0->is_in) { | 
|  | 1331 | if (req) { | 
|  | 1332 | if (handle_ep(ep0)) { | 
|  | 1333 | /* send IN/STATUS */ | 
|  | 1334 | PACKET("ep0 in/status\n"); | 
|  | 1335 | csr = __raw_readl(creg); | 
|  | 1336 | csr &= ~SET_FX; | 
|  | 1337 | csr |= CLR_FX | AT91_UDP_TXPKTRDY; | 
|  | 1338 | __raw_writel(csr, creg); | 
|  | 1339 | udc->req_pending = 0; | 
|  | 1340 | } | 
|  | 1341 | } else if (udc->req_pending) { | 
|  | 1342 | /* | 
|  | 1343 | * AT91 hardware has a hard time with this | 
|  | 1344 | * "deferred response" mode for control-OUT | 
|  | 1345 | * transfers.  (For control-IN it's fine.) | 
|  | 1346 | * | 
|  | 1347 | * The normal solution leaves OUT data in the | 
|  | 1348 | * fifo until the gadget driver is ready. | 
|  | 1349 | * We couldn't do that here without disabling | 
|  | 1350 | * the IRQ that tells about SETUP packets, | 
|  | 1351 | * e.g. when the host gets impatient... | 
|  | 1352 | * | 
|  | 1353 | * Working around it by copying into a buffer | 
|  | 1354 | * would almost be a non-deferred response, | 
|  | 1355 | * except that it wouldn't permit reliable | 
|  | 1356 | * stalling of the request.  Instead, demand | 
|  | 1357 | * that gadget drivers not use this mode. | 
|  | 1358 | */ | 
|  | 1359 | DBG("no control-OUT deferred responses!\n"); | 
|  | 1360 | __raw_writel(csr | AT91_UDP_FORCESTALL, creg); | 
|  | 1361 | udc->req_pending = 0; | 
|  | 1362 | } | 
|  | 1363 |  | 
|  | 1364 | /* STATUS stage for control-IN; ack.  */ | 
|  | 1365 | } else { | 
|  | 1366 | PACKET("ep0 out/status ACK\n"); | 
|  | 1367 | __raw_writel(csr, creg); | 
|  | 1368 |  | 
|  | 1369 | /* "early" status stage */ | 
|  | 1370 | if (req) | 
|  | 1371 | done(ep0, req, 0); | 
|  | 1372 | } | 
|  | 1373 | } | 
|  | 1374 | } | 
|  | 1375 |  | 
| David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 1376 | static irqreturn_t at91_udc_irq (int irq, void *_udc) | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1377 | { | 
|  | 1378 | struct at91_udc		*udc = _udc; | 
|  | 1379 | u32			rescans = 5; | 
| Harro Haan | c6c3523 | 2010-03-01 17:38:37 +0100 | [diff] [blame] | 1380 | int			disable_clock = 0; | 
|  | 1381 |  | 
|  | 1382 | if (!udc->clocked) { | 
|  | 1383 | clk_on(udc); | 
|  | 1384 | disable_clock = 1; | 
|  | 1385 | } | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1386 |  | 
|  | 1387 | while (rescans--) { | 
| David Brownell | 8b2e766 | 2006-07-05 02:38:56 -0700 | [diff] [blame] | 1388 | u32 status; | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1389 |  | 
| Andrew Victor | ffd3326 | 2006-12-07 22:44:33 -0800 | [diff] [blame] | 1390 | status = at91_udp_read(udc, AT91_UDP_ISR) | 
|  | 1391 | & at91_udp_read(udc, AT91_UDP_IMR); | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1392 | if (!status) | 
|  | 1393 | break; | 
|  | 1394 |  | 
|  | 1395 | /* USB reset irq:  not maskable */ | 
|  | 1396 | if (status & AT91_UDP_ENDBUSRES) { | 
| Andrew Victor | ffd3326 | 2006-12-07 22:44:33 -0800 | [diff] [blame] | 1397 | at91_udp_write(udc, AT91_UDP_IDR, ~MINIMUS_INTERRUPTUS); | 
|  | 1398 | at91_udp_write(udc, AT91_UDP_IER, MINIMUS_INTERRUPTUS); | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1399 | /* Atmel code clears this irq twice */ | 
| Andrew Victor | ffd3326 | 2006-12-07 22:44:33 -0800 | [diff] [blame] | 1400 | at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_ENDBUSRES); | 
|  | 1401 | at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_ENDBUSRES); | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1402 | VDBG("end bus reset\n"); | 
|  | 1403 | udc->addr = 0; | 
|  | 1404 | stop_activity(udc); | 
|  | 1405 |  | 
|  | 1406 | /* enable ep0 */ | 
| Andrew Victor | ffd3326 | 2006-12-07 22:44:33 -0800 | [diff] [blame] | 1407 | at91_udp_write(udc, AT91_UDP_CSR(0), | 
| David Brownell | 8b2e766 | 2006-07-05 02:38:56 -0700 | [diff] [blame] | 1408 | AT91_UDP_EPEDS | AT91_UDP_EPTYPE_CTRL); | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1409 | udc->gadget.speed = USB_SPEED_FULL; | 
|  | 1410 | udc->suspended = 0; | 
| Andrew Victor | ffd3326 | 2006-12-07 22:44:33 -0800 | [diff] [blame] | 1411 | at91_udp_write(udc, AT91_UDP_IER, AT91_UDP_EP(0)); | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1412 |  | 
|  | 1413 | /* | 
|  | 1414 | * NOTE:  this driver keeps clocks off unless the | 
| David Brownell | 8b2e766 | 2006-07-05 02:38:56 -0700 | [diff] [blame] | 1415 | * USB host is present.  That saves power, but for | 
|  | 1416 | * boards that don't support VBUS detection, both | 
|  | 1417 | * clocks need to be active most of the time. | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1418 | */ | 
|  | 1419 |  | 
|  | 1420 | /* host initiated suspend (3+ms bus idle) */ | 
|  | 1421 | } else if (status & AT91_UDP_RXSUSP) { | 
| Andrew Victor | ffd3326 | 2006-12-07 22:44:33 -0800 | [diff] [blame] | 1422 | at91_udp_write(udc, AT91_UDP_IDR, AT91_UDP_RXSUSP); | 
|  | 1423 | at91_udp_write(udc, AT91_UDP_IER, AT91_UDP_RXRSM); | 
|  | 1424 | at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_RXSUSP); | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1425 | // VDBG("bus suspend\n"); | 
|  | 1426 | if (udc->suspended) | 
|  | 1427 | continue; | 
|  | 1428 | udc->suspended = 1; | 
|  | 1429 |  | 
|  | 1430 | /* | 
|  | 1431 | * NOTE:  when suspending a VBUS-powered device, the | 
|  | 1432 | * gadget driver should switch into slow clock mode | 
|  | 1433 | * and then into standby to avoid drawing more than | 
|  | 1434 | * 500uA power (2500uA for some high-power configs). | 
|  | 1435 | */ | 
|  | 1436 | if (udc->driver && udc->driver->suspend) | 
|  | 1437 | udc->driver->suspend(&udc->gadget); | 
|  | 1438 |  | 
|  | 1439 | /* host initiated resume */ | 
|  | 1440 | } else if (status & AT91_UDP_RXRSM) { | 
| Andrew Victor | ffd3326 | 2006-12-07 22:44:33 -0800 | [diff] [blame] | 1441 | at91_udp_write(udc, AT91_UDP_IDR, AT91_UDP_RXRSM); | 
|  | 1442 | at91_udp_write(udc, AT91_UDP_IER, AT91_UDP_RXSUSP); | 
|  | 1443 | at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_RXRSM); | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1444 | // VDBG("bus resume\n"); | 
|  | 1445 | if (!udc->suspended) | 
|  | 1446 | continue; | 
|  | 1447 | udc->suspended = 0; | 
|  | 1448 |  | 
|  | 1449 | /* | 
|  | 1450 | * NOTE:  for a VBUS-powered device, the gadget driver | 
|  | 1451 | * would normally want to switch out of slow clock | 
|  | 1452 | * mode into normal mode. | 
|  | 1453 | */ | 
|  | 1454 | if (udc->driver && udc->driver->resume) | 
|  | 1455 | udc->driver->resume(&udc->gadget); | 
|  | 1456 |  | 
|  | 1457 | /* endpoint IRQs are cleared by handling them */ | 
|  | 1458 | } else { | 
|  | 1459 | int		i; | 
|  | 1460 | unsigned	mask = 1; | 
|  | 1461 | struct at91_ep	*ep = &udc->ep[1]; | 
|  | 1462 |  | 
|  | 1463 | if (status & mask) | 
|  | 1464 | handle_ep0(udc); | 
|  | 1465 | for (i = 1; i < NUM_ENDPOINTS; i++) { | 
|  | 1466 | mask <<= 1; | 
|  | 1467 | if (status & mask) | 
|  | 1468 | handle_ep(ep); | 
|  | 1469 | ep++; | 
|  | 1470 | } | 
|  | 1471 | } | 
|  | 1472 | } | 
|  | 1473 |  | 
| Harro Haan | c6c3523 | 2010-03-01 17:38:37 +0100 | [diff] [blame] | 1474 | if (disable_clock) | 
|  | 1475 | clk_off(udc); | 
|  | 1476 |  | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1477 | return IRQ_HANDLED; | 
|  | 1478 | } | 
|  | 1479 |  | 
|  | 1480 | /*-------------------------------------------------------------------------*/ | 
|  | 1481 |  | 
| David Brownell | 8b2e766 | 2006-07-05 02:38:56 -0700 | [diff] [blame] | 1482 | static void nop_release(struct device *dev) | 
|  | 1483 | { | 
|  | 1484 | /* nothing to free */ | 
|  | 1485 | } | 
|  | 1486 |  | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1487 | static struct at91_udc controller = { | 
|  | 1488 | .gadget = { | 
| David Brownell | 8b2e766 | 2006-07-05 02:38:56 -0700 | [diff] [blame] | 1489 | .ops	= &at91_udc_ops, | 
|  | 1490 | .ep0	= &controller.ep[0].ep, | 
|  | 1491 | .name	= driver_name, | 
|  | 1492 | .dev	= { | 
| Kay Sievers | c682b17 | 2009-01-06 10:44:42 -0800 | [diff] [blame] | 1493 | .init_name = "gadget", | 
| David Brownell | 8b2e766 | 2006-07-05 02:38:56 -0700 | [diff] [blame] | 1494 | .release = nop_release, | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1495 | } | 
|  | 1496 | }, | 
|  | 1497 | .ep[0] = { | 
|  | 1498 | .ep = { | 
|  | 1499 | .name	= ep0name, | 
|  | 1500 | .ops	= &at91_ep_ops, | 
|  | 1501 | }, | 
|  | 1502 | .udc		= &controller, | 
|  | 1503 | .maxpacket	= 8, | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1504 | .int_mask	= 1 << 0, | 
|  | 1505 | }, | 
|  | 1506 | .ep[1] = { | 
|  | 1507 | .ep = { | 
|  | 1508 | .name	= "ep1", | 
|  | 1509 | .ops	= &at91_ep_ops, | 
|  | 1510 | }, | 
|  | 1511 | .udc		= &controller, | 
|  | 1512 | .is_pingpong	= 1, | 
|  | 1513 | .maxpacket	= 64, | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1514 | .int_mask	= 1 << 1, | 
|  | 1515 | }, | 
|  | 1516 | .ep[2] = { | 
|  | 1517 | .ep = { | 
|  | 1518 | .name	= "ep2", | 
|  | 1519 | .ops	= &at91_ep_ops, | 
|  | 1520 | }, | 
|  | 1521 | .udc		= &controller, | 
|  | 1522 | .is_pingpong	= 1, | 
|  | 1523 | .maxpacket	= 64, | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1524 | .int_mask	= 1 << 2, | 
|  | 1525 | }, | 
|  | 1526 | .ep[3] = { | 
|  | 1527 | .ep = { | 
|  | 1528 | /* could actually do bulk too */ | 
|  | 1529 | .name	= "ep3-int", | 
|  | 1530 | .ops	= &at91_ep_ops, | 
|  | 1531 | }, | 
|  | 1532 | .udc		= &controller, | 
|  | 1533 | .maxpacket	= 8, | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1534 | .int_mask	= 1 << 3, | 
|  | 1535 | }, | 
|  | 1536 | .ep[4] = { | 
|  | 1537 | .ep = { | 
|  | 1538 | .name	= "ep4", | 
|  | 1539 | .ops	= &at91_ep_ops, | 
|  | 1540 | }, | 
|  | 1541 | .udc		= &controller, | 
|  | 1542 | .is_pingpong	= 1, | 
|  | 1543 | .maxpacket	= 256, | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1544 | .int_mask	= 1 << 4, | 
|  | 1545 | }, | 
|  | 1546 | .ep[5] = { | 
|  | 1547 | .ep = { | 
|  | 1548 | .name	= "ep5", | 
|  | 1549 | .ops	= &at91_ep_ops, | 
|  | 1550 | }, | 
|  | 1551 | .udc		= &controller, | 
|  | 1552 | .is_pingpong	= 1, | 
|  | 1553 | .maxpacket	= 256, | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1554 | .int_mask	= 1 << 5, | 
|  | 1555 | }, | 
| David Brownell | 8b2e766 | 2006-07-05 02:38:56 -0700 | [diff] [blame] | 1556 | /* ep6 and ep7 are also reserved (custom silicon might use them) */ | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1557 | }; | 
|  | 1558 |  | 
| David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 1559 | static irqreturn_t at91_vbus_irq(int irq, void *_udc) | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1560 | { | 
|  | 1561 | struct at91_udc	*udc = _udc; | 
|  | 1562 | unsigned	value; | 
|  | 1563 |  | 
|  | 1564 | /* vbus needs at least brief debouncing */ | 
|  | 1565 | udelay(10); | 
| David Brownell | f3db6e8 | 2008-01-05 13:21:43 -0800 | [diff] [blame] | 1566 | value = gpio_get_value(udc->board.vbus_pin); | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1567 | if (value != udc->vbus) | 
|  | 1568 | at91_vbus_session(&udc->gadget, value); | 
|  | 1569 |  | 
|  | 1570 | return IRQ_HANDLED; | 
|  | 1571 | } | 
|  | 1572 |  | 
|  | 1573 | int usb_gadget_register_driver (struct usb_gadget_driver *driver) | 
|  | 1574 | { | 
|  | 1575 | struct at91_udc	*udc = &controller; | 
|  | 1576 | int		retval; | 
|  | 1577 |  | 
|  | 1578 | if (!driver | 
| Wojtek Kaniewski | bc92c32 | 2006-12-08 09:39:36 -0800 | [diff] [blame] | 1579 | || driver->speed < USB_SPEED_FULL | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1580 | || !driver->bind | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1581 | || !driver->setup) { | 
|  | 1582 | DBG("bad parameter.\n"); | 
|  | 1583 | return -EINVAL; | 
|  | 1584 | } | 
|  | 1585 |  | 
|  | 1586 | if (udc->driver) { | 
|  | 1587 | DBG("UDC already has a gadget driver\n"); | 
|  | 1588 | return -EBUSY; | 
|  | 1589 | } | 
|  | 1590 |  | 
|  | 1591 | udc->driver = driver; | 
|  | 1592 | udc->gadget.dev.driver = &driver->driver; | 
| Greg Kroah-Hartman | 839214a | 2009-05-04 12:40:54 -0700 | [diff] [blame] | 1593 | dev_set_drvdata(&udc->gadget.dev, &driver->driver); | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1594 | udc->enabled = 1; | 
|  | 1595 | udc->selfpowered = 1; | 
|  | 1596 |  | 
|  | 1597 | retval = driver->bind(&udc->gadget); | 
|  | 1598 | if (retval) { | 
|  | 1599 | DBG("driver->bind() returned %d\n", retval); | 
|  | 1600 | udc->driver = NULL; | 
| Wojtek Kaniewski | 943c441 | 2006-12-08 03:23:00 -0800 | [diff] [blame] | 1601 | udc->gadget.dev.driver = NULL; | 
| Greg Kroah-Hartman | 839214a | 2009-05-04 12:40:54 -0700 | [diff] [blame] | 1602 | dev_set_drvdata(&udc->gadget.dev, NULL); | 
| Wojtek Kaniewski | 943c441 | 2006-12-08 03:23:00 -0800 | [diff] [blame] | 1603 | udc->enabled = 0; | 
|  | 1604 | udc->selfpowered = 0; | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1605 | return retval; | 
|  | 1606 | } | 
|  | 1607 |  | 
|  | 1608 | local_irq_disable(); | 
|  | 1609 | pullup(udc, 1); | 
|  | 1610 | local_irq_enable(); | 
|  | 1611 |  | 
|  | 1612 | DBG("bound to %s\n", driver->driver.name); | 
|  | 1613 | return 0; | 
|  | 1614 | } | 
|  | 1615 | EXPORT_SYMBOL (usb_gadget_register_driver); | 
|  | 1616 |  | 
|  | 1617 | int usb_gadget_unregister_driver (struct usb_gadget_driver *driver) | 
|  | 1618 | { | 
|  | 1619 | struct at91_udc *udc = &controller; | 
|  | 1620 |  | 
| David Brownell | 6bea476 | 2006-12-05 03:15:33 -0800 | [diff] [blame] | 1621 | if (!driver || driver != udc->driver || !driver->unbind) | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1622 | return -EINVAL; | 
|  | 1623 |  | 
|  | 1624 | local_irq_disable(); | 
|  | 1625 | udc->enabled = 0; | 
| Andrew Victor | ffd3326 | 2006-12-07 22:44:33 -0800 | [diff] [blame] | 1626 | at91_udp_write(udc, AT91_UDP_IDR, ~0); | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1627 | pullup(udc, 0); | 
|  | 1628 | local_irq_enable(); | 
|  | 1629 |  | 
|  | 1630 | driver->unbind(&udc->gadget); | 
| Patrik Sevallius | eb0be47 | 2007-11-20 09:32:00 -0800 | [diff] [blame] | 1631 | udc->gadget.dev.driver = NULL; | 
| Greg Kroah-Hartman | 839214a | 2009-05-04 12:40:54 -0700 | [diff] [blame] | 1632 | dev_set_drvdata(&udc->gadget.dev, NULL); | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1633 | udc->driver = NULL; | 
|  | 1634 |  | 
|  | 1635 | DBG("unbound from %s\n", driver->driver.name); | 
|  | 1636 | return 0; | 
|  | 1637 | } | 
|  | 1638 | EXPORT_SYMBOL (usb_gadget_unregister_driver); | 
|  | 1639 |  | 
|  | 1640 | /*-------------------------------------------------------------------------*/ | 
|  | 1641 |  | 
|  | 1642 | static void at91udc_shutdown(struct platform_device *dev) | 
|  | 1643 | { | 
|  | 1644 | /* force disconnect on reboot */ | 
|  | 1645 | pullup(platform_get_drvdata(dev), 0); | 
|  | 1646 | } | 
|  | 1647 |  | 
| David Brownell | 398acce | 2007-02-15 18:47:17 -0800 | [diff] [blame] | 1648 | static int __init at91udc_probe(struct platform_device *pdev) | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1649 | { | 
|  | 1650 | struct device	*dev = &pdev->dev; | 
|  | 1651 | struct at91_udc	*udc; | 
|  | 1652 | int		retval; | 
| Andrew Victor | ffd3326 | 2006-12-07 22:44:33 -0800 | [diff] [blame] | 1653 | struct resource	*res; | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1654 |  | 
|  | 1655 | if (!dev->platform_data) { | 
|  | 1656 | /* small (so we copy it) but critical! */ | 
|  | 1657 | DBG("missing platform_data\n"); | 
|  | 1658 | return -ENODEV; | 
|  | 1659 | } | 
|  | 1660 |  | 
| David Brownell | 8b2e766 | 2006-07-05 02:38:56 -0700 | [diff] [blame] | 1661 | if (pdev->num_resources != 2) { | 
| David Brownell | f3db6e8 | 2008-01-05 13:21:43 -0800 | [diff] [blame] | 1662 | DBG("invalid num_resources\n"); | 
| David Brownell | 8b2e766 | 2006-07-05 02:38:56 -0700 | [diff] [blame] | 1663 | return -ENODEV; | 
|  | 1664 | } | 
|  | 1665 | if ((pdev->resource[0].flags != IORESOURCE_MEM) | 
|  | 1666 | || (pdev->resource[1].flags != IORESOURCE_IRQ)) { | 
| David Brownell | f3db6e8 | 2008-01-05 13:21:43 -0800 | [diff] [blame] | 1667 | DBG("invalid resource type\n"); | 
| David Brownell | 8b2e766 | 2006-07-05 02:38:56 -0700 | [diff] [blame] | 1668 | return -ENODEV; | 
|  | 1669 | } | 
|  | 1670 |  | 
| Andrew Victor | ffd3326 | 2006-12-07 22:44:33 -0800 | [diff] [blame] | 1671 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | 
|  | 1672 | if (!res) | 
|  | 1673 | return -ENXIO; | 
|  | 1674 |  | 
| H Hartley Sweeten | d8bb0fd | 2009-12-14 17:59:22 -0500 | [diff] [blame] | 1675 | if (!request_mem_region(res->start, resource_size(res), driver_name)) { | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1676 | DBG("someone's using UDC memory\n"); | 
|  | 1677 | return -EBUSY; | 
|  | 1678 | } | 
|  | 1679 |  | 
|  | 1680 | /* init software state */ | 
|  | 1681 | udc = &controller; | 
|  | 1682 | udc->gadget.dev.parent = dev; | 
|  | 1683 | udc->board = *(struct at91_udc_data *) dev->platform_data; | 
|  | 1684 | udc->pdev = pdev; | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1685 | udc->enabled = 0; | 
|  | 1686 |  | 
| David Brownell | f3db6e8 | 2008-01-05 13:21:43 -0800 | [diff] [blame] | 1687 | /* rm9200 needs manual D+ pullup; off by default */ | 
|  | 1688 | if (cpu_is_at91rm9200()) { | 
|  | 1689 | if (udc->board.pullup_pin <= 0) { | 
|  | 1690 | DBG("no D+ pullup?\n"); | 
|  | 1691 | retval = -ENODEV; | 
|  | 1692 | goto fail0; | 
|  | 1693 | } | 
|  | 1694 | retval = gpio_request(udc->board.pullup_pin, "udc_pullup"); | 
|  | 1695 | if (retval) { | 
|  | 1696 | DBG("D+ pullup is busy\n"); | 
|  | 1697 | goto fail0; | 
|  | 1698 | } | 
|  | 1699 | gpio_direction_output(udc->board.pullup_pin, | 
|  | 1700 | udc->board.pullup_active_low); | 
|  | 1701 | } | 
|  | 1702 |  | 
| David Brownell | bb24280 | 2008-05-27 19:24:20 -0700 | [diff] [blame] | 1703 | /* newer chips have more FIFO memory than rm9200 */ | 
|  | 1704 | if (cpu_is_at91sam9260()) { | 
|  | 1705 | udc->ep[0].maxpacket = 64; | 
|  | 1706 | udc->ep[3].maxpacket = 64; | 
|  | 1707 | udc->ep[4].maxpacket = 512; | 
|  | 1708 | udc->ep[5].maxpacket = 512; | 
| Hong Xu | 23f6d91 | 2009-09-25 12:24:12 +0200 | [diff] [blame] | 1709 | } else if (cpu_is_at91sam9261() || cpu_is_at91sam9g10()) { | 
| David Brownell | bb24280 | 2008-05-27 19:24:20 -0700 | [diff] [blame] | 1710 | udc->ep[3].maxpacket = 64; | 
|  | 1711 | } else if (cpu_is_at91sam9263()) { | 
|  | 1712 | udc->ep[0].maxpacket = 64; | 
|  | 1713 | udc->ep[3].maxpacket = 64; | 
|  | 1714 | } | 
|  | 1715 |  | 
| H Hartley Sweeten | d8bb0fd | 2009-12-14 17:59:22 -0500 | [diff] [blame] | 1716 | udc->udp_baseaddr = ioremap(res->start, resource_size(res)); | 
| Andrew Victor | ffd3326 | 2006-12-07 22:44:33 -0800 | [diff] [blame] | 1717 | if (!udc->udp_baseaddr) { | 
| David Brownell | f3db6e8 | 2008-01-05 13:21:43 -0800 | [diff] [blame] | 1718 | retval = -ENOMEM; | 
|  | 1719 | goto fail0a; | 
| Andrew Victor | ffd3326 | 2006-12-07 22:44:33 -0800 | [diff] [blame] | 1720 | } | 
|  | 1721 |  | 
|  | 1722 | udc_reinit(udc); | 
|  | 1723 |  | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1724 | /* get interface and function clocks */ | 
|  | 1725 | udc->iclk = clk_get(dev, "udc_clk"); | 
|  | 1726 | udc->fclk = clk_get(dev, "udpck"); | 
|  | 1727 | if (IS_ERR(udc->iclk) || IS_ERR(udc->fclk)) { | 
|  | 1728 | DBG("clocks missing\n"); | 
| Andrew Victor | 29ba4b5 | 2006-12-07 22:44:38 -0800 | [diff] [blame] | 1729 | retval = -ENODEV; | 
| David Brownell | f3db6e8 | 2008-01-05 13:21:43 -0800 | [diff] [blame] | 1730 | /* NOTE: we "know" here that refcounts on these are NOPs */ | 
|  | 1731 | goto fail0b; | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1732 | } | 
|  | 1733 |  | 
|  | 1734 | retval = device_register(&udc->gadget.dev); | 
|  | 1735 | if (retval < 0) | 
| David Brownell | f3db6e8 | 2008-01-05 13:21:43 -0800 | [diff] [blame] | 1736 | goto fail0b; | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1737 |  | 
| David Brownell | 8b2e766 | 2006-07-05 02:38:56 -0700 | [diff] [blame] | 1738 | /* don't do anything until we have both gadget driver and VBUS */ | 
|  | 1739 | clk_enable(udc->iclk); | 
| Andrew Victor | ffd3326 | 2006-12-07 22:44:33 -0800 | [diff] [blame] | 1740 | at91_udp_write(udc, AT91_UDP_TXVC, AT91_UDP_TXVC_TXVDIS); | 
|  | 1741 | at91_udp_write(udc, AT91_UDP_IDR, 0xffffffff); | 
| Andrew Victor | 29ba4b5 | 2006-12-07 22:44:38 -0800 | [diff] [blame] | 1742 | /* Clear all pending interrupts - UDP may be used by bootloader. */ | 
|  | 1743 | at91_udp_write(udc, AT91_UDP_ICR, 0xffffffff); | 
| David Brownell | 8b2e766 | 2006-07-05 02:38:56 -0700 | [diff] [blame] | 1744 | clk_disable(udc->iclk); | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1745 |  | 
|  | 1746 | /* request UDC and maybe VBUS irqs */ | 
| David Brownell | 8b2e766 | 2006-07-05 02:38:56 -0700 | [diff] [blame] | 1747 | udc->udp_irq = platform_get_irq(pdev, 0); | 
| David Brownell | f3db6e8 | 2008-01-05 13:21:43 -0800 | [diff] [blame] | 1748 | retval = request_irq(udc->udp_irq, at91_udc_irq, | 
|  | 1749 | IRQF_DISABLED, driver_name, udc); | 
|  | 1750 | if (retval < 0) { | 
| David Brownell | 8b2e766 | 2006-07-05 02:38:56 -0700 | [diff] [blame] | 1751 | DBG("request irq %d failed\n", udc->udp_irq); | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1752 | goto fail1; | 
|  | 1753 | } | 
|  | 1754 | if (udc->board.vbus_pin > 0) { | 
| David Brownell | f3db6e8 | 2008-01-05 13:21:43 -0800 | [diff] [blame] | 1755 | retval = gpio_request(udc->board.vbus_pin, "udc_vbus"); | 
|  | 1756 | if (retval < 0) { | 
|  | 1757 | DBG("request vbus pin failed\n"); | 
|  | 1758 | goto fail2; | 
|  | 1759 | } | 
|  | 1760 | gpio_direction_input(udc->board.vbus_pin); | 
|  | 1761 |  | 
| Andrew Victor | 29ba4b5 | 2006-12-07 22:44:38 -0800 | [diff] [blame] | 1762 | /* | 
|  | 1763 | * Get the initial state of VBUS - we cannot expect | 
|  | 1764 | * a pending interrupt. | 
|  | 1765 | */ | 
| David Brownell | f3db6e8 | 2008-01-05 13:21:43 -0800 | [diff] [blame] | 1766 | udc->vbus = gpio_get_value(udc->board.vbus_pin); | 
| David Brownell | 8b2e766 | 2006-07-05 02:38:56 -0700 | [diff] [blame] | 1767 | if (request_irq(udc->board.vbus_pin, at91_vbus_irq, | 
|  | 1768 | IRQF_DISABLED, driver_name, udc)) { | 
|  | 1769 | DBG("request vbus irq %d failed\n", | 
|  | 1770 | udc->board.vbus_pin); | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1771 | retval = -EBUSY; | 
| David Brownell | f3db6e8 | 2008-01-05 13:21:43 -0800 | [diff] [blame] | 1772 | goto fail3; | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1773 | } | 
|  | 1774 | } else { | 
|  | 1775 | DBG("no VBUS detection, assuming always-on\n"); | 
|  | 1776 | udc->vbus = 1; | 
|  | 1777 | } | 
|  | 1778 | dev_set_drvdata(dev, udc); | 
| David Brownell | 8b2e766 | 2006-07-05 02:38:56 -0700 | [diff] [blame] | 1779 | device_init_wakeup(dev, 1); | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1780 | create_debug_file(udc); | 
|  | 1781 |  | 
|  | 1782 | INFO("%s version %s\n", driver_name, DRIVER_VERSION); | 
|  | 1783 | return 0; | 
|  | 1784 |  | 
| David Brownell | f3db6e8 | 2008-01-05 13:21:43 -0800 | [diff] [blame] | 1785 | fail3: | 
|  | 1786 | if (udc->board.vbus_pin > 0) | 
|  | 1787 | gpio_free(udc->board.vbus_pin); | 
|  | 1788 | fail2: | 
|  | 1789 | free_irq(udc->udp_irq, udc); | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1790 | fail1: | 
|  | 1791 | device_unregister(&udc->gadget.dev); | 
| David Brownell | f3db6e8 | 2008-01-05 13:21:43 -0800 | [diff] [blame] | 1792 | fail0b: | 
|  | 1793 | iounmap(udc->udp_baseaddr); | 
|  | 1794 | fail0a: | 
|  | 1795 | if (cpu_is_at91rm9200()) | 
|  | 1796 | gpio_free(udc->board.pullup_pin); | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1797 | fail0: | 
| H Hartley Sweeten | d8bb0fd | 2009-12-14 17:59:22 -0500 | [diff] [blame] | 1798 | release_mem_region(res->start, resource_size(res)); | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1799 | DBG("%s probe failed, %d\n", driver_name, retval); | 
|  | 1800 | return retval; | 
|  | 1801 | } | 
|  | 1802 |  | 
| David Brownell | 398acce | 2007-02-15 18:47:17 -0800 | [diff] [blame] | 1803 | static int __exit at91udc_remove(struct platform_device *pdev) | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1804 | { | 
| David Brownell | 8b2e766 | 2006-07-05 02:38:56 -0700 | [diff] [blame] | 1805 | struct at91_udc *udc = platform_get_drvdata(pdev); | 
| Andrew Victor | ffd3326 | 2006-12-07 22:44:33 -0800 | [diff] [blame] | 1806 | struct resource *res; | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1807 |  | 
|  | 1808 | DBG("remove\n"); | 
|  | 1809 |  | 
| David Brownell | 6bea476 | 2006-12-05 03:15:33 -0800 | [diff] [blame] | 1810 | if (udc->driver) | 
|  | 1811 | return -EBUSY; | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1812 |  | 
| David Brownell | 6bea476 | 2006-12-05 03:15:33 -0800 | [diff] [blame] | 1813 | pullup(udc, 0); | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1814 |  | 
| David Brownell | 8b2e766 | 2006-07-05 02:38:56 -0700 | [diff] [blame] | 1815 | device_init_wakeup(&pdev->dev, 0); | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1816 | remove_debug_file(udc); | 
| David Brownell | f3db6e8 | 2008-01-05 13:21:43 -0800 | [diff] [blame] | 1817 | if (udc->board.vbus_pin > 0) { | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1818 | free_irq(udc->board.vbus_pin, udc); | 
| David Brownell | f3db6e8 | 2008-01-05 13:21:43 -0800 | [diff] [blame] | 1819 | gpio_free(udc->board.vbus_pin); | 
|  | 1820 | } | 
| David Brownell | 8b2e766 | 2006-07-05 02:38:56 -0700 | [diff] [blame] | 1821 | free_irq(udc->udp_irq, udc); | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1822 | device_unregister(&udc->gadget.dev); | 
| Andrew Victor | ffd3326 | 2006-12-07 22:44:33 -0800 | [diff] [blame] | 1823 |  | 
|  | 1824 | iounmap(udc->udp_baseaddr); | 
| David Brownell | f3db6e8 | 2008-01-05 13:21:43 -0800 | [diff] [blame] | 1825 |  | 
|  | 1826 | if (cpu_is_at91rm9200()) | 
|  | 1827 | gpio_free(udc->board.pullup_pin); | 
|  | 1828 |  | 
| Andrew Victor | ffd3326 | 2006-12-07 22:44:33 -0800 | [diff] [blame] | 1829 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | 
| H Hartley Sweeten | d8bb0fd | 2009-12-14 17:59:22 -0500 | [diff] [blame] | 1830 | release_mem_region(res->start, resource_size(res)); | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1831 |  | 
|  | 1832 | clk_put(udc->iclk); | 
|  | 1833 | clk_put(udc->fclk); | 
|  | 1834 |  | 
|  | 1835 | return 0; | 
|  | 1836 | } | 
|  | 1837 |  | 
|  | 1838 | #ifdef CONFIG_PM | 
| David Brownell | 8b2e766 | 2006-07-05 02:38:56 -0700 | [diff] [blame] | 1839 | static int at91udc_suspend(struct platform_device *pdev, pm_message_t mesg) | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1840 | { | 
| David Brownell | 8b2e766 | 2006-07-05 02:38:56 -0700 | [diff] [blame] | 1841 | struct at91_udc *udc = platform_get_drvdata(pdev); | 
|  | 1842 | int		wake = udc->driver && device_may_wakeup(&pdev->dev); | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1843 |  | 
| David Brownell | 8b2e766 | 2006-07-05 02:38:56 -0700 | [diff] [blame] | 1844 | /* Unless we can act normally to the host (letting it wake us up | 
|  | 1845 | * whenever it has work for us) force disconnect.  Wakeup requires | 
|  | 1846 | * PLLB for USB events (signaling for reset, wakeup, or incoming | 
|  | 1847 | * tokens) and VBUS irqs (on systems which support them). | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1848 | */ | 
| David Brownell | 8b2e766 | 2006-07-05 02:38:56 -0700 | [diff] [blame] | 1849 | if ((!udc->suspended && udc->addr) | 
|  | 1850 | || !wake | 
|  | 1851 | || at91_suspend_entering_slow_clock()) { | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1852 | pullup(udc, 0); | 
| David Brownell | 66e56ce | 2007-01-16 12:46:39 -0800 | [diff] [blame] | 1853 | wake = 0; | 
| David Brownell | 8b2e766 | 2006-07-05 02:38:56 -0700 | [diff] [blame] | 1854 | } else | 
|  | 1855 | enable_irq_wake(udc->udp_irq); | 
|  | 1856 |  | 
| David Brownell | 66e56ce | 2007-01-16 12:46:39 -0800 | [diff] [blame] | 1857 | udc->active_suspend = wake; | 
|  | 1858 | if (udc->board.vbus_pin > 0 && wake) | 
|  | 1859 | enable_irq_wake(udc->board.vbus_pin); | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1860 | return 0; | 
|  | 1861 | } | 
|  | 1862 |  | 
| David Brownell | 8b2e766 | 2006-07-05 02:38:56 -0700 | [diff] [blame] | 1863 | static int at91udc_resume(struct platform_device *pdev) | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1864 | { | 
| David Brownell | 8b2e766 | 2006-07-05 02:38:56 -0700 | [diff] [blame] | 1865 | struct at91_udc *udc = platform_get_drvdata(pdev); | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1866 |  | 
| David Brownell | 66e56ce | 2007-01-16 12:46:39 -0800 | [diff] [blame] | 1867 | if (udc->board.vbus_pin > 0 && udc->active_suspend) | 
|  | 1868 | disable_irq_wake(udc->board.vbus_pin); | 
|  | 1869 |  | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1870 | /* maybe reconnect to host; if so, clocks on */ | 
| David Brownell | 66e56ce | 2007-01-16 12:46:39 -0800 | [diff] [blame] | 1871 | if (udc->active_suspend) | 
|  | 1872 | disable_irq_wake(udc->udp_irq); | 
|  | 1873 | else | 
|  | 1874 | pullup(udc, 1); | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1875 | return 0; | 
|  | 1876 | } | 
|  | 1877 | #else | 
|  | 1878 | #define	at91udc_suspend	NULL | 
|  | 1879 | #define	at91udc_resume	NULL | 
|  | 1880 | #endif | 
|  | 1881 |  | 
| David Brownell | dee497d | 2007-02-24 13:54:56 -0800 | [diff] [blame] | 1882 | static struct platform_driver at91_udc_driver = { | 
| David Brownell | 398acce | 2007-02-15 18:47:17 -0800 | [diff] [blame] | 1883 | .remove		= __exit_p(at91udc_remove), | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1884 | .shutdown	= at91udc_shutdown, | 
|  | 1885 | .suspend	= at91udc_suspend, | 
| David Brownell | 8b2e766 | 2006-07-05 02:38:56 -0700 | [diff] [blame] | 1886 | .resume		= at91udc_resume, | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1887 | .driver		= { | 
|  | 1888 | .name	= (char *) driver_name, | 
|  | 1889 | .owner	= THIS_MODULE, | 
|  | 1890 | }, | 
|  | 1891 | }; | 
|  | 1892 |  | 
| David Brownell | 398acce | 2007-02-15 18:47:17 -0800 | [diff] [blame] | 1893 | static int __init udc_init_module(void) | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1894 | { | 
| David Brownell | dee497d | 2007-02-24 13:54:56 -0800 | [diff] [blame] | 1895 | return platform_driver_probe(&at91_udc_driver, at91udc_probe); | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1896 | } | 
|  | 1897 | module_init(udc_init_module); | 
|  | 1898 |  | 
| David Brownell | 398acce | 2007-02-15 18:47:17 -0800 | [diff] [blame] | 1899 | static void __exit udc_exit_module(void) | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1900 | { | 
| David Brownell | dee497d | 2007-02-24 13:54:56 -0800 | [diff] [blame] | 1901 | platform_driver_unregister(&at91_udc_driver); | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1902 | } | 
|  | 1903 | module_exit(udc_exit_module); | 
|  | 1904 |  | 
| David Brownell | 8b2e766 | 2006-07-05 02:38:56 -0700 | [diff] [blame] | 1905 | MODULE_DESCRIPTION("AT91 udc driver"); | 
| David Brownell | bae4bd8 | 2006-01-22 10:32:37 -0800 | [diff] [blame] | 1906 | MODULE_AUTHOR("Thomas Rathbone, David Brownell"); | 
|  | 1907 | MODULE_LICENSE("GPL"); | 
| Kay Sievers | f34c32f | 2008-04-10 21:29:21 -0700 | [diff] [blame] | 1908 | MODULE_ALIAS("platform:at91_udc"); |