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