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