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