Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Ilya Yanok | 06bb920 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 2 | /* |
| 3 | * MUSB OTG driver peripheral support |
| 4 | * |
| 5 | * Copyright 2005 Mentor Graphics Corporation |
| 6 | * Copyright (C) 2005-2006 by Texas Instruments |
| 7 | * Copyright (C) 2006-2007 Nokia Corporation |
| 8 | * Copyright (C) 2009 MontaVista Software, Inc. <source@mvista.com> |
Ilya Yanok | 06bb920 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Ilya Yanok | 06bb920 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 11 | #ifndef __UBOOT__ |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 13 | #include <dm/device_compat.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 14 | #include <dm/devres.h> |
Ilya Yanok | 06bb920 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 15 | #include <linux/kernel.h> |
| 16 | #include <linux/list.h> |
| 17 | #include <linux/timer.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/smp.h> |
| 20 | #include <linux/spinlock.h> |
| 21 | #include <linux/delay.h> |
| 22 | #include <linux/dma-mapping.h> |
| 23 | #include <linux/slab.h> |
| 24 | #else |
| 25 | #include <common.h> |
Sean Anderson | 3e46486 | 2020-10-04 21:39:54 -0400 | [diff] [blame] | 26 | #include <dm.h> |
| 27 | #include <dm/device_compat.h> |
Simon Glass | c06c1be | 2020-05-10 11:40:08 -0600 | [diff] [blame] | 28 | #include <linux/bug.h> |
Simon Glass | bdd5f81 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 29 | #include <linux/printk.h> |
Ilya Yanok | 06bb920 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 30 | #include <linux/usb/ch9.h> |
| 31 | #include "linux-compat.h" |
| 32 | #endif |
| 33 | |
| 34 | #include "musb_core.h" |
| 35 | |
| 36 | |
| 37 | /* MUSB PERIPHERAL status 3-mar-2006: |
| 38 | * |
| 39 | * - EP0 seems solid. It passes both USBCV and usbtest control cases. |
| 40 | * Minor glitches: |
| 41 | * |
| 42 | * + remote wakeup to Linux hosts work, but saw USBCV failures; |
| 43 | * in one test run (operator error?) |
| 44 | * + endpoint halt tests -- in both usbtest and usbcv -- seem |
| 45 | * to break when dma is enabled ... is something wrongly |
| 46 | * clearing SENDSTALL? |
| 47 | * |
| 48 | * - Mass storage behaved ok when last tested. Network traffic patterns |
| 49 | * (with lots of short transfers etc) need retesting; they turn up the |
| 50 | * worst cases of the DMA, since short packets are typical but are not |
| 51 | * required. |
| 52 | * |
| 53 | * - TX/IN |
| 54 | * + both pio and dma behave in with network and g_zero tests |
| 55 | * + no cppi throughput issues other than no-hw-queueing |
| 56 | * + failed with FLAT_REG (DaVinci) |
| 57 | * + seems to behave with double buffering, PIO -and- CPPI |
| 58 | * + with gadgetfs + AIO, requests got lost? |
| 59 | * |
| 60 | * - RX/OUT |
| 61 | * + both pio and dma behave in with network and g_zero tests |
| 62 | * + dma is slow in typical case (short_not_ok is clear) |
| 63 | * + double buffering ok with PIO |
| 64 | * + double buffering *FAILS* with CPPI, wrong data bytes sometimes |
| 65 | * + request lossage observed with gadgetfs |
| 66 | * |
| 67 | * - ISO not tested ... might work, but only weakly isochronous |
| 68 | * |
| 69 | * - Gadget driver disabling of softconnect during bind() is ignored; so |
| 70 | * drivers can't hold off host requests until userspace is ready. |
| 71 | * (Workaround: they can turn it off later.) |
| 72 | * |
| 73 | * - PORTABILITY (assumes PIO works): |
| 74 | * + DaVinci, basically works with cppi dma |
| 75 | * + OMAP 2430, ditto with mentor dma |
| 76 | * + TUSB 6010, platform-specific dma in the works |
| 77 | */ |
| 78 | |
| 79 | /* ----------------------------------------------------------------------- */ |
| 80 | |
| 81 | #define is_buffer_mapped(req) (is_dma_capable() && \ |
| 82 | (req->map_state != UN_MAPPED)) |
| 83 | |
Paul Kocialkowski | f34dfcb | 2015-08-04 17:04:06 +0200 | [diff] [blame] | 84 | #ifndef CONFIG_USB_MUSB_PIO_ONLY |
Ilya Yanok | 06bb920 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 85 | /* Maps the buffer to dma */ |
| 86 | |
| 87 | static inline void map_dma_buffer(struct musb_request *request, |
| 88 | struct musb *musb, struct musb_ep *musb_ep) |
| 89 | { |
| 90 | int compatible = true; |
| 91 | struct dma_controller *dma = musb->dma_controller; |
| 92 | |
| 93 | request->map_state = UN_MAPPED; |
| 94 | |
| 95 | if (!is_dma_capable() || !musb_ep->dma) |
| 96 | return; |
| 97 | |
| 98 | /* Check if DMA engine can handle this request. |
| 99 | * DMA code must reject the USB request explicitly. |
| 100 | * Default behaviour is to map the request. |
| 101 | */ |
| 102 | if (dma->is_compatible) |
| 103 | compatible = dma->is_compatible(musb_ep->dma, |
| 104 | musb_ep->packet_sz, request->request.buf, |
| 105 | request->request.length); |
| 106 | if (!compatible) |
| 107 | return; |
| 108 | |
| 109 | if (request->request.dma == DMA_ADDR_INVALID) { |
| 110 | request->request.dma = dma_map_single( |
| 111 | musb->controller, |
| 112 | request->request.buf, |
| 113 | request->request.length, |
| 114 | request->tx |
| 115 | ? DMA_TO_DEVICE |
| 116 | : DMA_FROM_DEVICE); |
| 117 | request->map_state = MUSB_MAPPED; |
| 118 | } else { |
| 119 | dma_sync_single_for_device(musb->controller, |
| 120 | request->request.dma, |
| 121 | request->request.length, |
| 122 | request->tx |
| 123 | ? DMA_TO_DEVICE |
| 124 | : DMA_FROM_DEVICE); |
| 125 | request->map_state = PRE_MAPPED; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | /* Unmap the buffer from dma and maps it back to cpu */ |
| 130 | static inline void unmap_dma_buffer(struct musb_request *request, |
| 131 | struct musb *musb) |
| 132 | { |
| 133 | if (!is_buffer_mapped(request)) |
| 134 | return; |
| 135 | |
| 136 | if (request->request.dma == DMA_ADDR_INVALID) { |
| 137 | dev_vdbg(musb->controller, |
| 138 | "not unmapping a never mapped buffer\n"); |
| 139 | return; |
| 140 | } |
| 141 | if (request->map_state == MUSB_MAPPED) { |
| 142 | dma_unmap_single(musb->controller, |
| 143 | request->request.dma, |
| 144 | request->request.length, |
| 145 | request->tx |
| 146 | ? DMA_TO_DEVICE |
| 147 | : DMA_FROM_DEVICE); |
| 148 | request->request.dma = DMA_ADDR_INVALID; |
| 149 | } else { /* PRE_MAPPED */ |
| 150 | dma_sync_single_for_cpu(musb->controller, |
| 151 | request->request.dma, |
| 152 | request->request.length, |
| 153 | request->tx |
| 154 | ? DMA_TO_DEVICE |
| 155 | : DMA_FROM_DEVICE); |
| 156 | } |
| 157 | request->map_state = UN_MAPPED; |
| 158 | } |
| 159 | #else |
| 160 | static inline void map_dma_buffer(struct musb_request *request, |
| 161 | struct musb *musb, struct musb_ep *musb_ep) |
| 162 | { |
| 163 | } |
| 164 | |
| 165 | static inline void unmap_dma_buffer(struct musb_request *request, |
| 166 | struct musb *musb) |
| 167 | { |
| 168 | } |
| 169 | #endif |
| 170 | |
| 171 | /* |
| 172 | * Immediately complete a request. |
| 173 | * |
| 174 | * @param request the request to complete |
| 175 | * @param status the status to complete the request with |
| 176 | * Context: controller locked, IRQs blocked. |
| 177 | */ |
| 178 | void musb_g_giveback( |
| 179 | struct musb_ep *ep, |
| 180 | struct usb_request *request, |
| 181 | int status) |
| 182 | __releases(ep->musb->lock) |
| 183 | __acquires(ep->musb->lock) |
| 184 | { |
| 185 | struct musb_request *req; |
| 186 | struct musb *musb; |
| 187 | int busy = ep->busy; |
| 188 | |
| 189 | req = to_musb_request(request); |
| 190 | |
| 191 | list_del(&req->list); |
| 192 | if (req->request.status == -EINPROGRESS) |
| 193 | req->request.status = status; |
| 194 | musb = req->musb; |
| 195 | |
| 196 | ep->busy = 1; |
| 197 | spin_unlock(&musb->lock); |
| 198 | unmap_dma_buffer(req, musb); |
| 199 | if (request->status == 0) |
| 200 | dev_dbg(musb->controller, "%s done request %p, %d/%d\n", |
| 201 | ep->end_point.name, request, |
| 202 | req->request.actual, req->request.length); |
| 203 | else |
| 204 | dev_dbg(musb->controller, "%s request %p, %d/%d fault %d\n", |
| 205 | ep->end_point.name, request, |
| 206 | req->request.actual, req->request.length, |
| 207 | request->status); |
| 208 | req->request.complete(&req->ep->end_point, &req->request); |
| 209 | spin_lock(&musb->lock); |
| 210 | ep->busy = busy; |
| 211 | } |
| 212 | |
| 213 | /* ----------------------------------------------------------------------- */ |
| 214 | |
| 215 | /* |
| 216 | * Abort requests queued to an endpoint using the status. Synchronous. |
| 217 | * caller locked controller and blocked irqs, and selected this ep. |
| 218 | */ |
| 219 | static void nuke(struct musb_ep *ep, const int status) |
| 220 | { |
| 221 | struct musb *musb = ep->musb; |
| 222 | struct musb_request *req = NULL; |
| 223 | void __iomem *epio = ep->musb->endpoints[ep->current_epnum].regs; |
| 224 | |
| 225 | ep->busy = 1; |
| 226 | |
| 227 | if (is_dma_capable() && ep->dma) { |
| 228 | struct dma_controller *c = ep->musb->dma_controller; |
| 229 | int value; |
| 230 | |
| 231 | if (ep->is_in) { |
| 232 | /* |
| 233 | * The programming guide says that we must not clear |
| 234 | * the DMAMODE bit before DMAENAB, so we only |
| 235 | * clear it in the second write... |
| 236 | */ |
| 237 | musb_writew(epio, MUSB_TXCSR, |
| 238 | MUSB_TXCSR_DMAMODE | MUSB_TXCSR_FLUSHFIFO); |
| 239 | musb_writew(epio, MUSB_TXCSR, |
| 240 | 0 | MUSB_TXCSR_FLUSHFIFO); |
| 241 | } else { |
| 242 | musb_writew(epio, MUSB_RXCSR, |
| 243 | 0 | MUSB_RXCSR_FLUSHFIFO); |
| 244 | musb_writew(epio, MUSB_RXCSR, |
| 245 | 0 | MUSB_RXCSR_FLUSHFIFO); |
| 246 | } |
| 247 | |
| 248 | value = c->channel_abort(ep->dma); |
| 249 | dev_dbg(musb->controller, "%s: abort DMA --> %d\n", |
| 250 | ep->name, value); |
| 251 | c->channel_release(ep->dma); |
| 252 | ep->dma = NULL; |
| 253 | } |
| 254 | |
| 255 | while (!list_empty(&ep->req_list)) { |
| 256 | req = list_first_entry(&ep->req_list, struct musb_request, list); |
| 257 | musb_g_giveback(ep, &req->request, status); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | /* ----------------------------------------------------------------------- */ |
| 262 | |
| 263 | /* Data transfers - pure PIO, pure DMA, or mixed mode */ |
| 264 | |
| 265 | /* |
| 266 | * This assumes the separate CPPI engine is responding to DMA requests |
| 267 | * from the usb core ... sequenced a bit differently from mentor dma. |
| 268 | */ |
| 269 | |
| 270 | static inline int max_ep_writesize(struct musb *musb, struct musb_ep *ep) |
| 271 | { |
| 272 | if (can_bulk_split(musb, ep->type)) |
| 273 | return ep->hw_ep->max_packet_sz_tx; |
| 274 | else |
| 275 | return ep->packet_sz; |
| 276 | } |
| 277 | |
| 278 | |
| 279 | #ifdef CONFIG_USB_INVENTRA_DMA |
| 280 | |
| 281 | /* Peripheral tx (IN) using Mentor DMA works as follows: |
| 282 | Only mode 0 is used for transfers <= wPktSize, |
| 283 | mode 1 is used for larger transfers, |
| 284 | |
| 285 | One of the following happens: |
| 286 | - Host sends IN token which causes an endpoint interrupt |
| 287 | -> TxAvail |
| 288 | -> if DMA is currently busy, exit. |
| 289 | -> if queue is non-empty, txstate(). |
| 290 | |
| 291 | - Request is queued by the gadget driver. |
| 292 | -> if queue was previously empty, txstate() |
| 293 | |
| 294 | txstate() |
| 295 | -> start |
| 296 | /\ -> setup DMA |
| 297 | | (data is transferred to the FIFO, then sent out when |
| 298 | | IN token(s) are recd from Host. |
| 299 | | -> DMA interrupt on completion |
| 300 | | calls TxAvail. |
| 301 | | -> stop DMA, ~DMAENAB, |
| 302 | | -> set TxPktRdy for last short pkt or zlp |
| 303 | | -> Complete Request |
| 304 | | -> Continue next request (call txstate) |
| 305 | |___________________________________| |
| 306 | |
| 307 | * Non-Mentor DMA engines can of course work differently, such as by |
| 308 | * upleveling from irq-per-packet to irq-per-buffer. |
| 309 | */ |
| 310 | |
| 311 | #endif |
| 312 | |
| 313 | /* |
| 314 | * An endpoint is transmitting data. This can be called either from |
| 315 | * the IRQ routine or from ep.queue() to kickstart a request on an |
| 316 | * endpoint. |
| 317 | * |
| 318 | * Context: controller locked, IRQs blocked, endpoint selected |
| 319 | */ |
| 320 | static void txstate(struct musb *musb, struct musb_request *req) |
| 321 | { |
| 322 | u8 epnum = req->epnum; |
| 323 | struct musb_ep *musb_ep; |
| 324 | void __iomem *epio = musb->endpoints[epnum].regs; |
| 325 | struct usb_request *request; |
| 326 | u16 fifo_count = 0, csr; |
| 327 | int use_dma = 0; |
| 328 | |
| 329 | musb_ep = req->ep; |
| 330 | |
| 331 | /* Check if EP is disabled */ |
| 332 | if (!musb_ep->desc) { |
| 333 | dev_dbg(musb->controller, "ep:%s disabled - ignore request\n", |
| 334 | musb_ep->end_point.name); |
| 335 | return; |
| 336 | } |
| 337 | |
| 338 | /* we shouldn't get here while DMA is active ... but we do ... */ |
| 339 | if (dma_channel_status(musb_ep->dma) == MUSB_DMA_STATUS_BUSY) { |
| 340 | dev_dbg(musb->controller, "dma pending...\n"); |
| 341 | return; |
| 342 | } |
| 343 | |
| 344 | /* read TXCSR before */ |
| 345 | csr = musb_readw(epio, MUSB_TXCSR); |
| 346 | |
| 347 | request = &req->request; |
| 348 | fifo_count = min(max_ep_writesize(musb, musb_ep), |
| 349 | (int)(request->length - request->actual)); |
| 350 | |
| 351 | if (csr & MUSB_TXCSR_TXPKTRDY) { |
| 352 | dev_dbg(musb->controller, "%s old packet still ready , txcsr %03x\n", |
| 353 | musb_ep->end_point.name, csr); |
| 354 | return; |
| 355 | } |
| 356 | |
| 357 | if (csr & MUSB_TXCSR_P_SENDSTALL) { |
| 358 | dev_dbg(musb->controller, "%s stalling, txcsr %03x\n", |
| 359 | musb_ep->end_point.name, csr); |
| 360 | return; |
| 361 | } |
| 362 | |
| 363 | dev_dbg(musb->controller, "hw_ep%d, maxpacket %d, fifo count %d, txcsr %03x\n", |
| 364 | epnum, musb_ep->packet_sz, fifo_count, |
| 365 | csr); |
| 366 | |
Paul Kocialkowski | f34dfcb | 2015-08-04 17:04:06 +0200 | [diff] [blame] | 367 | #ifndef CONFIG_USB_MUSB_PIO_ONLY |
Ilya Yanok | 06bb920 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 368 | if (is_buffer_mapped(req)) { |
| 369 | struct dma_controller *c = musb->dma_controller; |
| 370 | size_t request_size; |
| 371 | |
| 372 | /* setup DMA, then program endpoint CSR */ |
| 373 | request_size = min_t(size_t, request->length - request->actual, |
| 374 | musb_ep->dma->max_len); |
| 375 | |
| 376 | use_dma = (request->dma != DMA_ADDR_INVALID); |
| 377 | |
| 378 | /* MUSB_TXCSR_P_ISO is still set correctly */ |
| 379 | |
| 380 | #if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_UX500_DMA) |
| 381 | { |
| 382 | if (request_size < musb_ep->packet_sz) |
| 383 | musb_ep->dma->desired_mode = 0; |
| 384 | else |
| 385 | musb_ep->dma->desired_mode = 1; |
| 386 | |
| 387 | use_dma = use_dma && c->channel_program( |
| 388 | musb_ep->dma, musb_ep->packet_sz, |
| 389 | musb_ep->dma->desired_mode, |
| 390 | request->dma + request->actual, request_size); |
| 391 | if (use_dma) { |
| 392 | if (musb_ep->dma->desired_mode == 0) { |
| 393 | /* |
| 394 | * We must not clear the DMAMODE bit |
| 395 | * before the DMAENAB bit -- and the |
| 396 | * latter doesn't always get cleared |
| 397 | * before we get here... |
| 398 | */ |
| 399 | csr &= ~(MUSB_TXCSR_AUTOSET |
| 400 | | MUSB_TXCSR_DMAENAB); |
| 401 | musb_writew(epio, MUSB_TXCSR, csr |
| 402 | | MUSB_TXCSR_P_WZC_BITS); |
| 403 | csr &= ~MUSB_TXCSR_DMAMODE; |
| 404 | csr |= (MUSB_TXCSR_DMAENAB | |
| 405 | MUSB_TXCSR_MODE); |
| 406 | /* against programming guide */ |
| 407 | } else { |
| 408 | csr |= (MUSB_TXCSR_DMAENAB |
| 409 | | MUSB_TXCSR_DMAMODE |
| 410 | | MUSB_TXCSR_MODE); |
| 411 | if (!musb_ep->hb_mult) |
| 412 | csr |= MUSB_TXCSR_AUTOSET; |
| 413 | } |
| 414 | csr &= ~MUSB_TXCSR_P_UNDERRUN; |
| 415 | |
| 416 | musb_writew(epio, MUSB_TXCSR, csr); |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | #elif defined(CONFIG_USB_TI_CPPI_DMA) |
| 421 | /* program endpoint CSR first, then setup DMA */ |
| 422 | csr &= ~(MUSB_TXCSR_P_UNDERRUN | MUSB_TXCSR_TXPKTRDY); |
| 423 | csr |= MUSB_TXCSR_DMAENAB | MUSB_TXCSR_DMAMODE | |
| 424 | MUSB_TXCSR_MODE; |
| 425 | musb_writew(epio, MUSB_TXCSR, |
| 426 | (MUSB_TXCSR_P_WZC_BITS & ~MUSB_TXCSR_P_UNDERRUN) |
| 427 | | csr); |
| 428 | |
| 429 | /* ensure writebuffer is empty */ |
| 430 | csr = musb_readw(epio, MUSB_TXCSR); |
| 431 | |
| 432 | /* NOTE host side sets DMAENAB later than this; both are |
| 433 | * OK since the transfer dma glue (between CPPI and Mentor |
| 434 | * fifos) just tells CPPI it could start. Data only moves |
| 435 | * to the USB TX fifo when both fifos are ready. |
| 436 | */ |
| 437 | |
| 438 | /* "mode" is irrelevant here; handle terminating ZLPs like |
| 439 | * PIO does, since the hardware RNDIS mode seems unreliable |
| 440 | * except for the last-packet-is-already-short case. |
| 441 | */ |
| 442 | use_dma = use_dma && c->channel_program( |
| 443 | musb_ep->dma, musb_ep->packet_sz, |
| 444 | 0, |
| 445 | request->dma + request->actual, |
| 446 | request_size); |
| 447 | if (!use_dma) { |
| 448 | c->channel_release(musb_ep->dma); |
| 449 | musb_ep->dma = NULL; |
| 450 | csr &= ~MUSB_TXCSR_DMAENAB; |
| 451 | musb_writew(epio, MUSB_TXCSR, csr); |
| 452 | /* invariant: prequest->buf is non-null */ |
| 453 | } |
| 454 | #elif defined(CONFIG_USB_TUSB_OMAP_DMA) |
| 455 | use_dma = use_dma && c->channel_program( |
| 456 | musb_ep->dma, musb_ep->packet_sz, |
| 457 | request->zero, |
| 458 | request->dma + request->actual, |
| 459 | request_size); |
| 460 | #endif |
| 461 | } |
| 462 | #endif |
| 463 | |
| 464 | if (!use_dma) { |
| 465 | /* |
| 466 | * Unmap the dma buffer back to cpu if dma channel |
| 467 | * programming fails |
| 468 | */ |
| 469 | unmap_dma_buffer(req, musb); |
| 470 | |
| 471 | musb_write_fifo(musb_ep->hw_ep, fifo_count, |
| 472 | (u8 *) (request->buf + request->actual)); |
| 473 | request->actual += fifo_count; |
| 474 | csr |= MUSB_TXCSR_TXPKTRDY; |
| 475 | csr &= ~MUSB_TXCSR_P_UNDERRUN; |
| 476 | musb_writew(epio, MUSB_TXCSR, csr); |
| 477 | } |
| 478 | |
| 479 | /* host may already have the data when this message shows... */ |
| 480 | dev_dbg(musb->controller, "%s TX/IN %s len %d/%d, txcsr %04x, fifo %d/%d\n", |
| 481 | musb_ep->end_point.name, use_dma ? "dma" : "pio", |
| 482 | request->actual, request->length, |
| 483 | musb_readw(epio, MUSB_TXCSR), |
| 484 | fifo_count, |
| 485 | musb_readw(epio, MUSB_TXMAXP)); |
| 486 | } |
| 487 | |
| 488 | /* |
| 489 | * FIFO state update (e.g. data ready). |
| 490 | * Called from IRQ, with controller locked. |
| 491 | */ |
| 492 | void musb_g_tx(struct musb *musb, u8 epnum) |
| 493 | { |
| 494 | u16 csr; |
| 495 | struct musb_request *req; |
| 496 | struct usb_request *request; |
| 497 | u8 __iomem *mbase = musb->mregs; |
| 498 | struct musb_ep *musb_ep = &musb->endpoints[epnum].ep_in; |
| 499 | void __iomem *epio = musb->endpoints[epnum].regs; |
| 500 | struct dma_channel *dma; |
| 501 | |
| 502 | musb_ep_select(mbase, epnum); |
| 503 | req = next_request(musb_ep); |
| 504 | request = &req->request; |
| 505 | |
| 506 | csr = musb_readw(epio, MUSB_TXCSR); |
| 507 | dev_dbg(musb->controller, "<== %s, txcsr %04x\n", musb_ep->end_point.name, csr); |
| 508 | |
| 509 | dma = is_dma_capable() ? musb_ep->dma : NULL; |
| 510 | |
| 511 | /* |
| 512 | * REVISIT: for high bandwidth, MUSB_TXCSR_P_INCOMPTX |
| 513 | * probably rates reporting as a host error. |
| 514 | */ |
| 515 | if (csr & MUSB_TXCSR_P_SENTSTALL) { |
| 516 | csr |= MUSB_TXCSR_P_WZC_BITS; |
| 517 | csr &= ~MUSB_TXCSR_P_SENTSTALL; |
| 518 | musb_writew(epio, MUSB_TXCSR, csr); |
| 519 | return; |
| 520 | } |
| 521 | |
| 522 | if (csr & MUSB_TXCSR_P_UNDERRUN) { |
| 523 | /* We NAKed, no big deal... little reason to care. */ |
| 524 | csr |= MUSB_TXCSR_P_WZC_BITS; |
| 525 | csr &= ~(MUSB_TXCSR_P_UNDERRUN | MUSB_TXCSR_TXPKTRDY); |
| 526 | musb_writew(epio, MUSB_TXCSR, csr); |
| 527 | dev_vdbg(musb->controller, "underrun on ep%d, req %p\n", |
| 528 | epnum, request); |
| 529 | } |
| 530 | |
| 531 | if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) { |
| 532 | /* |
| 533 | * SHOULD NOT HAPPEN... has with CPPI though, after |
| 534 | * changing SENDSTALL (and other cases); harmless? |
| 535 | */ |
| 536 | dev_dbg(musb->controller, "%s dma still busy?\n", musb_ep->end_point.name); |
| 537 | return; |
| 538 | } |
| 539 | |
| 540 | if (request) { |
| 541 | u8 is_dma = 0; |
| 542 | |
| 543 | if (dma && (csr & MUSB_TXCSR_DMAENAB)) { |
| 544 | is_dma = 1; |
| 545 | csr |= MUSB_TXCSR_P_WZC_BITS; |
| 546 | csr &= ~(MUSB_TXCSR_DMAENAB | MUSB_TXCSR_P_UNDERRUN | |
| 547 | MUSB_TXCSR_TXPKTRDY | MUSB_TXCSR_AUTOSET); |
| 548 | musb_writew(epio, MUSB_TXCSR, csr); |
| 549 | /* Ensure writebuffer is empty. */ |
| 550 | csr = musb_readw(epio, MUSB_TXCSR); |
| 551 | request->actual += musb_ep->dma->actual_len; |
| 552 | dev_dbg(musb->controller, "TXCSR%d %04x, DMA off, len %zu, req %p\n", |
| 553 | epnum, csr, musb_ep->dma->actual_len, request); |
| 554 | } |
| 555 | |
| 556 | /* |
| 557 | * First, maybe a terminating short packet. Some DMA |
| 558 | * engines might handle this by themselves. |
| 559 | */ |
| 560 | if ((request->zero && request->length |
| 561 | && (request->length % musb_ep->packet_sz == 0) |
| 562 | && (request->actual == request->length)) |
| 563 | #if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_UX500_DMA) |
| 564 | || (is_dma && (!dma->desired_mode || |
| 565 | (request->actual & |
| 566 | (musb_ep->packet_sz - 1)))) |
| 567 | #endif |
| 568 | ) { |
| 569 | /* |
| 570 | * On DMA completion, FIFO may not be |
| 571 | * available yet... |
| 572 | */ |
| 573 | if (csr & MUSB_TXCSR_TXPKTRDY) |
| 574 | return; |
| 575 | |
| 576 | dev_dbg(musb->controller, "sending zero pkt\n"); |
| 577 | musb_writew(epio, MUSB_TXCSR, MUSB_TXCSR_MODE |
| 578 | | MUSB_TXCSR_TXPKTRDY); |
| 579 | request->zero = 0; |
| 580 | } |
| 581 | |
| 582 | if (request->actual == request->length) { |
| 583 | musb_g_giveback(musb_ep, request, 0); |
| 584 | /* |
| 585 | * In the giveback function the MUSB lock is |
| 586 | * released and acquired after sometime. During |
| 587 | * this time period the INDEX register could get |
| 588 | * changed by the gadget_queue function especially |
| 589 | * on SMP systems. Reselect the INDEX to be sure |
| 590 | * we are reading/modifying the right registers |
| 591 | */ |
| 592 | musb_ep_select(mbase, epnum); |
| 593 | req = musb_ep->desc ? next_request(musb_ep) : NULL; |
| 594 | if (!req) { |
| 595 | dev_dbg(musb->controller, "%s idle now\n", |
| 596 | musb_ep->end_point.name); |
| 597 | return; |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | txstate(musb, req); |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | /* ------------------------------------------------------------ */ |
| 606 | |
| 607 | #ifdef CONFIG_USB_INVENTRA_DMA |
| 608 | |
| 609 | /* Peripheral rx (OUT) using Mentor DMA works as follows: |
| 610 | - Only mode 0 is used. |
| 611 | |
| 612 | - Request is queued by the gadget class driver. |
| 613 | -> if queue was previously empty, rxstate() |
| 614 | |
| 615 | - Host sends OUT token which causes an endpoint interrupt |
| 616 | /\ -> RxReady |
| 617 | | -> if request queued, call rxstate |
| 618 | | /\ -> setup DMA |
| 619 | | | -> DMA interrupt on completion |
| 620 | | | -> RxReady |
| 621 | | | -> stop DMA |
| 622 | | | -> ack the read |
| 623 | | | -> if data recd = max expected |
| 624 | | | by the request, or host |
| 625 | | | sent a short packet, |
| 626 | | | complete the request, |
| 627 | | | and start the next one. |
| 628 | | |_____________________________________| |
| 629 | | else just wait for the host |
| 630 | | to send the next OUT token. |
| 631 | |__________________________________________________| |
| 632 | |
| 633 | * Non-Mentor DMA engines can of course work differently. |
| 634 | */ |
| 635 | |
| 636 | #endif |
| 637 | |
| 638 | /* |
| 639 | * Context: controller locked, IRQs blocked, endpoint selected |
| 640 | */ |
| 641 | static void rxstate(struct musb *musb, struct musb_request *req) |
| 642 | { |
| 643 | const u8 epnum = req->epnum; |
| 644 | struct usb_request *request = &req->request; |
| 645 | struct musb_ep *musb_ep; |
| 646 | void __iomem *epio = musb->endpoints[epnum].regs; |
| 647 | unsigned fifo_count = 0; |
| 648 | u16 len; |
| 649 | u16 csr = musb_readw(epio, MUSB_RXCSR); |
| 650 | struct musb_hw_ep *hw_ep = &musb->endpoints[epnum]; |
| 651 | u8 use_mode_1; |
| 652 | |
| 653 | if (hw_ep->is_shared_fifo) |
| 654 | musb_ep = &hw_ep->ep_in; |
| 655 | else |
| 656 | musb_ep = &hw_ep->ep_out; |
| 657 | |
| 658 | len = musb_ep->packet_sz; |
| 659 | |
| 660 | /* Check if EP is disabled */ |
| 661 | if (!musb_ep->desc) { |
| 662 | dev_dbg(musb->controller, "ep:%s disabled - ignore request\n", |
| 663 | musb_ep->end_point.name); |
| 664 | return; |
| 665 | } |
| 666 | |
| 667 | /* We shouldn't get here while DMA is active, but we do... */ |
| 668 | if (dma_channel_status(musb_ep->dma) == MUSB_DMA_STATUS_BUSY) { |
| 669 | dev_dbg(musb->controller, "DMA pending...\n"); |
| 670 | return; |
| 671 | } |
| 672 | |
| 673 | if (csr & MUSB_RXCSR_P_SENDSTALL) { |
| 674 | dev_dbg(musb->controller, "%s stalling, RXCSR %04x\n", |
| 675 | musb_ep->end_point.name, csr); |
| 676 | return; |
| 677 | } |
| 678 | |
| 679 | if (is_cppi_enabled() && is_buffer_mapped(req)) { |
| 680 | struct dma_controller *c = musb->dma_controller; |
| 681 | struct dma_channel *channel = musb_ep->dma; |
| 682 | |
| 683 | /* NOTE: CPPI won't actually stop advancing the DMA |
| 684 | * queue after short packet transfers, so this is almost |
| 685 | * always going to run as IRQ-per-packet DMA so that |
| 686 | * faults will be handled correctly. |
| 687 | */ |
| 688 | if (c->channel_program(channel, |
| 689 | musb_ep->packet_sz, |
| 690 | !request->short_not_ok, |
| 691 | request->dma + request->actual, |
| 692 | request->length - request->actual)) { |
| 693 | |
| 694 | /* make sure that if an rxpkt arrived after the irq, |
| 695 | * the cppi engine will be ready to take it as soon |
| 696 | * as DMA is enabled |
| 697 | */ |
| 698 | csr &= ~(MUSB_RXCSR_AUTOCLEAR |
| 699 | | MUSB_RXCSR_DMAMODE); |
| 700 | csr |= MUSB_RXCSR_DMAENAB | MUSB_RXCSR_P_WZC_BITS; |
| 701 | musb_writew(epio, MUSB_RXCSR, csr); |
| 702 | return; |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | if (csr & MUSB_RXCSR_RXPKTRDY) { |
| 707 | len = musb_readw(epio, MUSB_RXCOUNT); |
| 708 | |
| 709 | /* |
| 710 | * Enable Mode 1 on RX transfers only when short_not_ok flag |
| 711 | * is set. Currently short_not_ok flag is set only from |
| 712 | * file_storage and f_mass_storage drivers |
| 713 | */ |
| 714 | |
| 715 | if (request->short_not_ok && len == musb_ep->packet_sz) |
| 716 | use_mode_1 = 1; |
| 717 | else |
| 718 | use_mode_1 = 0; |
| 719 | |
| 720 | if (request->actual < request->length) { |
| 721 | #ifdef CONFIG_USB_INVENTRA_DMA |
| 722 | if (is_buffer_mapped(req)) { |
| 723 | struct dma_controller *c; |
| 724 | struct dma_channel *channel; |
| 725 | int use_dma = 0; |
| 726 | |
| 727 | c = musb->dma_controller; |
| 728 | channel = musb_ep->dma; |
| 729 | |
| 730 | /* We use DMA Req mode 0 in rx_csr, and DMA controller operates in |
| 731 | * mode 0 only. So we do not get endpoint interrupts due to DMA |
| 732 | * completion. We only get interrupts from DMA controller. |
| 733 | * |
| 734 | * We could operate in DMA mode 1 if we knew the size of the tranfer |
| 735 | * in advance. For mass storage class, request->length = what the host |
| 736 | * sends, so that'd work. But for pretty much everything else, |
| 737 | * request->length is routinely more than what the host sends. For |
| 738 | * most these gadgets, end of is signified either by a short packet, |
| 739 | * or filling the last byte of the buffer. (Sending extra data in |
| 740 | * that last pckate should trigger an overflow fault.) But in mode 1, |
| 741 | * we don't get DMA completion interrupt for short packets. |
| 742 | * |
| 743 | * Theoretically, we could enable DMAReq irq (MUSB_RXCSR_DMAMODE = 1), |
| 744 | * to get endpoint interrupt on every DMA req, but that didn't seem |
| 745 | * to work reliably. |
| 746 | * |
| 747 | * REVISIT an updated g_file_storage can set req->short_not_ok, which |
| 748 | * then becomes usable as a runtime "use mode 1" hint... |
| 749 | */ |
| 750 | |
| 751 | /* Experimental: Mode1 works with mass storage use cases */ |
| 752 | if (use_mode_1) { |
| 753 | csr |= MUSB_RXCSR_AUTOCLEAR; |
| 754 | musb_writew(epio, MUSB_RXCSR, csr); |
| 755 | csr |= MUSB_RXCSR_DMAENAB; |
| 756 | musb_writew(epio, MUSB_RXCSR, csr); |
| 757 | |
| 758 | /* |
| 759 | * this special sequence (enabling and then |
| 760 | * disabling MUSB_RXCSR_DMAMODE) is required |
| 761 | * to get DMAReq to activate |
| 762 | */ |
| 763 | musb_writew(epio, MUSB_RXCSR, |
| 764 | csr | MUSB_RXCSR_DMAMODE); |
| 765 | musb_writew(epio, MUSB_RXCSR, csr); |
| 766 | |
| 767 | } else { |
| 768 | if (!musb_ep->hb_mult && |
| 769 | musb_ep->hw_ep->rx_double_buffered) |
| 770 | csr |= MUSB_RXCSR_AUTOCLEAR; |
| 771 | csr |= MUSB_RXCSR_DMAENAB; |
| 772 | musb_writew(epio, MUSB_RXCSR, csr); |
| 773 | } |
| 774 | |
| 775 | if (request->actual < request->length) { |
| 776 | int transfer_size = 0; |
| 777 | if (use_mode_1) { |
| 778 | transfer_size = min(request->length - request->actual, |
| 779 | channel->max_len); |
| 780 | musb_ep->dma->desired_mode = 1; |
| 781 | } else { |
| 782 | transfer_size = min(request->length - request->actual, |
| 783 | (unsigned)len); |
| 784 | musb_ep->dma->desired_mode = 0; |
| 785 | } |
| 786 | |
| 787 | use_dma = c->channel_program( |
| 788 | channel, |
| 789 | musb_ep->packet_sz, |
| 790 | channel->desired_mode, |
| 791 | request->dma |
| 792 | + request->actual, |
| 793 | transfer_size); |
| 794 | } |
| 795 | |
| 796 | if (use_dma) |
| 797 | return; |
| 798 | } |
| 799 | #elif defined(CONFIG_USB_UX500_DMA) |
| 800 | if ((is_buffer_mapped(req)) && |
| 801 | (request->actual < request->length)) { |
| 802 | |
| 803 | struct dma_controller *c; |
| 804 | struct dma_channel *channel; |
| 805 | int transfer_size = 0; |
| 806 | |
| 807 | c = musb->dma_controller; |
| 808 | channel = musb_ep->dma; |
| 809 | |
| 810 | /* In case first packet is short */ |
| 811 | if (len < musb_ep->packet_sz) |
| 812 | transfer_size = len; |
| 813 | else if (request->short_not_ok) |
| 814 | transfer_size = min(request->length - |
| 815 | request->actual, |
| 816 | channel->max_len); |
| 817 | else |
| 818 | transfer_size = min(request->length - |
| 819 | request->actual, |
| 820 | (unsigned)len); |
| 821 | |
| 822 | csr &= ~MUSB_RXCSR_DMAMODE; |
| 823 | csr |= (MUSB_RXCSR_DMAENAB | |
| 824 | MUSB_RXCSR_AUTOCLEAR); |
| 825 | |
| 826 | musb_writew(epio, MUSB_RXCSR, csr); |
| 827 | |
| 828 | if (transfer_size <= musb_ep->packet_sz) { |
| 829 | musb_ep->dma->desired_mode = 0; |
| 830 | } else { |
| 831 | musb_ep->dma->desired_mode = 1; |
| 832 | /* Mode must be set after DMAENAB */ |
| 833 | csr |= MUSB_RXCSR_DMAMODE; |
| 834 | musb_writew(epio, MUSB_RXCSR, csr); |
| 835 | } |
| 836 | |
| 837 | if (c->channel_program(channel, |
| 838 | musb_ep->packet_sz, |
| 839 | channel->desired_mode, |
| 840 | request->dma |
| 841 | + request->actual, |
| 842 | transfer_size)) |
| 843 | |
| 844 | return; |
| 845 | } |
| 846 | #endif /* Mentor's DMA */ |
| 847 | |
| 848 | fifo_count = request->length - request->actual; |
| 849 | dev_dbg(musb->controller, "%s OUT/RX pio fifo %d/%d, maxpacket %d\n", |
| 850 | musb_ep->end_point.name, |
| 851 | len, fifo_count, |
| 852 | musb_ep->packet_sz); |
| 853 | |
| 854 | fifo_count = min_t(unsigned, len, fifo_count); |
| 855 | |
| 856 | #ifdef CONFIG_USB_TUSB_OMAP_DMA |
| 857 | if (tusb_dma_omap() && is_buffer_mapped(req)) { |
| 858 | struct dma_controller *c = musb->dma_controller; |
| 859 | struct dma_channel *channel = musb_ep->dma; |
| 860 | u32 dma_addr = request->dma + request->actual; |
| 861 | int ret; |
| 862 | |
| 863 | ret = c->channel_program(channel, |
| 864 | musb_ep->packet_sz, |
| 865 | channel->desired_mode, |
| 866 | dma_addr, |
| 867 | fifo_count); |
| 868 | if (ret) |
| 869 | return; |
| 870 | } |
| 871 | #endif |
| 872 | /* |
| 873 | * Unmap the dma buffer back to cpu if dma channel |
| 874 | * programming fails. This buffer is mapped if the |
| 875 | * channel allocation is successful |
| 876 | */ |
| 877 | if (is_buffer_mapped(req)) { |
| 878 | unmap_dma_buffer(req, musb); |
| 879 | |
| 880 | /* |
| 881 | * Clear DMAENAB and AUTOCLEAR for the |
| 882 | * PIO mode transfer |
| 883 | */ |
| 884 | csr &= ~(MUSB_RXCSR_DMAENAB | MUSB_RXCSR_AUTOCLEAR); |
| 885 | musb_writew(epio, MUSB_RXCSR, csr); |
| 886 | } |
| 887 | |
| 888 | musb_read_fifo(musb_ep->hw_ep, fifo_count, (u8 *) |
| 889 | (request->buf + request->actual)); |
| 890 | request->actual += fifo_count; |
| 891 | |
| 892 | /* REVISIT if we left anything in the fifo, flush |
| 893 | * it and report -EOVERFLOW |
| 894 | */ |
| 895 | |
| 896 | /* ack the read! */ |
| 897 | csr |= MUSB_RXCSR_P_WZC_BITS; |
| 898 | csr &= ~MUSB_RXCSR_RXPKTRDY; |
| 899 | musb_writew(epio, MUSB_RXCSR, csr); |
| 900 | } |
| 901 | } |
| 902 | |
| 903 | /* reach the end or short packet detected */ |
| 904 | if (request->actual == request->length || len < musb_ep->packet_sz) |
| 905 | musb_g_giveback(musb_ep, request, 0); |
| 906 | } |
| 907 | |
| 908 | /* |
| 909 | * Data ready for a request; called from IRQ |
| 910 | */ |
| 911 | void musb_g_rx(struct musb *musb, u8 epnum) |
| 912 | { |
| 913 | u16 csr; |
| 914 | struct musb_request *req; |
| 915 | struct usb_request *request; |
| 916 | void __iomem *mbase = musb->mregs; |
| 917 | struct musb_ep *musb_ep; |
| 918 | void __iomem *epio = musb->endpoints[epnum].regs; |
| 919 | struct dma_channel *dma; |
| 920 | struct musb_hw_ep *hw_ep = &musb->endpoints[epnum]; |
| 921 | |
| 922 | if (hw_ep->is_shared_fifo) |
| 923 | musb_ep = &hw_ep->ep_in; |
| 924 | else |
| 925 | musb_ep = &hw_ep->ep_out; |
| 926 | |
| 927 | musb_ep_select(mbase, epnum); |
| 928 | |
| 929 | req = next_request(musb_ep); |
| 930 | if (!req) |
| 931 | return; |
| 932 | |
| 933 | request = &req->request; |
| 934 | |
| 935 | csr = musb_readw(epio, MUSB_RXCSR); |
| 936 | dma = is_dma_capable() ? musb_ep->dma : NULL; |
| 937 | |
| 938 | dev_dbg(musb->controller, "<== %s, rxcsr %04x%s %p\n", musb_ep->end_point.name, |
| 939 | csr, dma ? " (dma)" : "", request); |
| 940 | |
| 941 | if (csr & MUSB_RXCSR_P_SENTSTALL) { |
| 942 | csr |= MUSB_RXCSR_P_WZC_BITS; |
| 943 | csr &= ~MUSB_RXCSR_P_SENTSTALL; |
| 944 | musb_writew(epio, MUSB_RXCSR, csr); |
| 945 | return; |
| 946 | } |
| 947 | |
| 948 | if (csr & MUSB_RXCSR_P_OVERRUN) { |
| 949 | /* csr |= MUSB_RXCSR_P_WZC_BITS; */ |
| 950 | csr &= ~MUSB_RXCSR_P_OVERRUN; |
| 951 | musb_writew(epio, MUSB_RXCSR, csr); |
| 952 | |
| 953 | dev_dbg(musb->controller, "%s iso overrun on %p\n", musb_ep->name, request); |
| 954 | if (request->status == -EINPROGRESS) |
| 955 | request->status = -EOVERFLOW; |
| 956 | } |
| 957 | if (csr & MUSB_RXCSR_INCOMPRX) { |
| 958 | /* REVISIT not necessarily an error */ |
| 959 | dev_dbg(musb->controller, "%s, incomprx\n", musb_ep->end_point.name); |
| 960 | } |
| 961 | |
| 962 | if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) { |
| 963 | /* "should not happen"; likely RXPKTRDY pending for DMA */ |
| 964 | dev_dbg(musb->controller, "%s busy, csr %04x\n", |
| 965 | musb_ep->end_point.name, csr); |
| 966 | return; |
| 967 | } |
| 968 | |
| 969 | if (dma && (csr & MUSB_RXCSR_DMAENAB)) { |
| 970 | csr &= ~(MUSB_RXCSR_AUTOCLEAR |
| 971 | | MUSB_RXCSR_DMAENAB |
| 972 | | MUSB_RXCSR_DMAMODE); |
| 973 | musb_writew(epio, MUSB_RXCSR, |
| 974 | MUSB_RXCSR_P_WZC_BITS | csr); |
| 975 | |
| 976 | request->actual += musb_ep->dma->actual_len; |
| 977 | |
| 978 | dev_dbg(musb->controller, "RXCSR%d %04x, dma off, %04x, len %zu, req %p\n", |
| 979 | epnum, csr, |
| 980 | musb_readw(epio, MUSB_RXCSR), |
| 981 | musb_ep->dma->actual_len, request); |
| 982 | |
| 983 | #if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_TUSB_OMAP_DMA) || \ |
| 984 | defined(CONFIG_USB_UX500_DMA) |
| 985 | /* Autoclear doesn't clear RxPktRdy for short packets */ |
| 986 | if ((dma->desired_mode == 0 && !hw_ep->rx_double_buffered) |
| 987 | || (dma->actual_len |
| 988 | & (musb_ep->packet_sz - 1))) { |
| 989 | /* ack the read! */ |
| 990 | csr &= ~MUSB_RXCSR_RXPKTRDY; |
| 991 | musb_writew(epio, MUSB_RXCSR, csr); |
| 992 | } |
| 993 | |
| 994 | /* incomplete, and not short? wait for next IN packet */ |
| 995 | if ((request->actual < request->length) |
| 996 | && (musb_ep->dma->actual_len |
| 997 | == musb_ep->packet_sz)) { |
| 998 | /* In double buffer case, continue to unload fifo if |
Wolfgang Denk | 62fb2b4 | 2021-09-27 17:42:39 +0200 | [diff] [blame] | 999 | * there is Rx packet in FIFO. |
| 1000 | **/ |
Ilya Yanok | 06bb920 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 1001 | csr = musb_readw(epio, MUSB_RXCSR); |
| 1002 | if ((csr & MUSB_RXCSR_RXPKTRDY) && |
| 1003 | hw_ep->rx_double_buffered) |
| 1004 | goto exit; |
| 1005 | return; |
| 1006 | } |
| 1007 | #endif |
| 1008 | musb_g_giveback(musb_ep, request, 0); |
| 1009 | /* |
| 1010 | * In the giveback function the MUSB lock is |
| 1011 | * released and acquired after sometime. During |
| 1012 | * this time period the INDEX register could get |
| 1013 | * changed by the gadget_queue function especially |
| 1014 | * on SMP systems. Reselect the INDEX to be sure |
| 1015 | * we are reading/modifying the right registers |
| 1016 | */ |
| 1017 | musb_ep_select(mbase, epnum); |
| 1018 | |
| 1019 | req = next_request(musb_ep); |
| 1020 | if (!req) |
| 1021 | return; |
| 1022 | } |
| 1023 | #if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_TUSB_OMAP_DMA) || \ |
| 1024 | defined(CONFIG_USB_UX500_DMA) |
| 1025 | exit: |
| 1026 | #endif |
| 1027 | /* Analyze request */ |
| 1028 | rxstate(musb, req); |
| 1029 | } |
| 1030 | |
| 1031 | /* ------------------------------------------------------------ */ |
| 1032 | |
| 1033 | static int musb_gadget_enable(struct usb_ep *ep, |
| 1034 | const struct usb_endpoint_descriptor *desc) |
| 1035 | { |
| 1036 | unsigned long flags; |
| 1037 | struct musb_ep *musb_ep; |
| 1038 | struct musb_hw_ep *hw_ep; |
| 1039 | void __iomem *regs; |
| 1040 | struct musb *musb; |
| 1041 | void __iomem *mbase; |
| 1042 | u8 epnum; |
| 1043 | u16 csr; |
| 1044 | unsigned tmp; |
| 1045 | int status = -EINVAL; |
| 1046 | |
| 1047 | if (!ep || !desc) |
| 1048 | return -EINVAL; |
| 1049 | |
| 1050 | musb_ep = to_musb_ep(ep); |
| 1051 | hw_ep = musb_ep->hw_ep; |
| 1052 | regs = hw_ep->regs; |
| 1053 | musb = musb_ep->musb; |
| 1054 | mbase = musb->mregs; |
| 1055 | epnum = musb_ep->current_epnum; |
| 1056 | |
| 1057 | spin_lock_irqsave(&musb->lock, flags); |
| 1058 | |
| 1059 | if (musb_ep->desc) { |
| 1060 | status = -EBUSY; |
| 1061 | goto fail; |
| 1062 | } |
| 1063 | musb_ep->type = usb_endpoint_type(desc); |
| 1064 | |
| 1065 | /* check direction and (later) maxpacket size against endpoint */ |
| 1066 | if (usb_endpoint_num(desc) != epnum) |
| 1067 | goto fail; |
| 1068 | |
| 1069 | /* REVISIT this rules out high bandwidth periodic transfers */ |
| 1070 | tmp = usb_endpoint_maxp(desc); |
| 1071 | if (tmp & ~0x07ff) { |
| 1072 | int ok; |
| 1073 | |
| 1074 | if (usb_endpoint_dir_in(desc)) |
| 1075 | ok = musb->hb_iso_tx; |
| 1076 | else |
| 1077 | ok = musb->hb_iso_rx; |
| 1078 | |
| 1079 | if (!ok) { |
| 1080 | dev_dbg(musb->controller, "no support for high bandwidth ISO\n"); |
| 1081 | goto fail; |
| 1082 | } |
| 1083 | musb_ep->hb_mult = (tmp >> 11) & 3; |
| 1084 | } else { |
| 1085 | musb_ep->hb_mult = 0; |
| 1086 | } |
| 1087 | |
| 1088 | musb_ep->packet_sz = tmp & 0x7ff; |
| 1089 | tmp = musb_ep->packet_sz * (musb_ep->hb_mult + 1); |
| 1090 | |
| 1091 | /* enable the interrupts for the endpoint, set the endpoint |
| 1092 | * packet size (or fail), set the mode, clear the fifo |
| 1093 | */ |
| 1094 | musb_ep_select(mbase, epnum); |
| 1095 | if (usb_endpoint_dir_in(desc)) { |
| 1096 | u16 int_txe = musb_readw(mbase, MUSB_INTRTXE); |
| 1097 | |
| 1098 | if (hw_ep->is_shared_fifo) |
| 1099 | musb_ep->is_in = 1; |
| 1100 | if (!musb_ep->is_in) |
| 1101 | goto fail; |
| 1102 | |
| 1103 | if (tmp > hw_ep->max_packet_sz_tx) { |
| 1104 | dev_dbg(musb->controller, "packet size beyond hardware FIFO size\n"); |
| 1105 | goto fail; |
| 1106 | } |
| 1107 | |
| 1108 | int_txe |= (1 << epnum); |
| 1109 | musb_writew(mbase, MUSB_INTRTXE, int_txe); |
| 1110 | |
| 1111 | /* REVISIT if can_bulk_split(), use by updating "tmp"; |
| 1112 | * likewise high bandwidth periodic tx |
| 1113 | */ |
| 1114 | /* Set TXMAXP with the FIFO size of the endpoint |
| 1115 | * to disable double buffering mode. |
| 1116 | */ |
| 1117 | if (musb->double_buffer_not_ok) |
| 1118 | musb_writew(regs, MUSB_TXMAXP, hw_ep->max_packet_sz_tx); |
| 1119 | else |
| 1120 | musb_writew(regs, MUSB_TXMAXP, musb_ep->packet_sz |
| 1121 | | (musb_ep->hb_mult << 11)); |
| 1122 | |
| 1123 | csr = MUSB_TXCSR_MODE | MUSB_TXCSR_CLRDATATOG; |
| 1124 | if (musb_readw(regs, MUSB_TXCSR) |
| 1125 | & MUSB_TXCSR_FIFONOTEMPTY) |
| 1126 | csr |= MUSB_TXCSR_FLUSHFIFO; |
| 1127 | if (musb_ep->type == USB_ENDPOINT_XFER_ISOC) |
| 1128 | csr |= MUSB_TXCSR_P_ISO; |
| 1129 | |
| 1130 | /* set twice in case of double buffering */ |
| 1131 | musb_writew(regs, MUSB_TXCSR, csr); |
| 1132 | /* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */ |
| 1133 | musb_writew(regs, MUSB_TXCSR, csr); |
| 1134 | |
| 1135 | } else { |
| 1136 | u16 int_rxe = musb_readw(mbase, MUSB_INTRRXE); |
| 1137 | |
| 1138 | if (hw_ep->is_shared_fifo) |
| 1139 | musb_ep->is_in = 0; |
| 1140 | if (musb_ep->is_in) |
| 1141 | goto fail; |
| 1142 | |
| 1143 | if (tmp > hw_ep->max_packet_sz_rx) { |
| 1144 | dev_dbg(musb->controller, "packet size beyond hardware FIFO size\n"); |
| 1145 | goto fail; |
| 1146 | } |
| 1147 | |
| 1148 | int_rxe |= (1 << epnum); |
| 1149 | musb_writew(mbase, MUSB_INTRRXE, int_rxe); |
| 1150 | |
| 1151 | /* REVISIT if can_bulk_combine() use by updating "tmp" |
| 1152 | * likewise high bandwidth periodic rx |
| 1153 | */ |
| 1154 | /* Set RXMAXP with the FIFO size of the endpoint |
| 1155 | * to disable double buffering mode. |
| 1156 | */ |
| 1157 | if (musb->double_buffer_not_ok) |
| 1158 | musb_writew(regs, MUSB_RXMAXP, hw_ep->max_packet_sz_tx); |
| 1159 | else |
| 1160 | musb_writew(regs, MUSB_RXMAXP, musb_ep->packet_sz |
| 1161 | | (musb_ep->hb_mult << 11)); |
| 1162 | |
| 1163 | /* force shared fifo to OUT-only mode */ |
| 1164 | if (hw_ep->is_shared_fifo) { |
| 1165 | csr = musb_readw(regs, MUSB_TXCSR); |
| 1166 | csr &= ~(MUSB_TXCSR_MODE | MUSB_TXCSR_TXPKTRDY); |
| 1167 | musb_writew(regs, MUSB_TXCSR, csr); |
| 1168 | } |
| 1169 | |
| 1170 | csr = MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_CLRDATATOG; |
| 1171 | if (musb_ep->type == USB_ENDPOINT_XFER_ISOC) |
| 1172 | csr |= MUSB_RXCSR_P_ISO; |
| 1173 | else if (musb_ep->type == USB_ENDPOINT_XFER_INT) |
| 1174 | csr |= MUSB_RXCSR_DISNYET; |
| 1175 | |
| 1176 | /* set twice in case of double buffering */ |
| 1177 | musb_writew(regs, MUSB_RXCSR, csr); |
| 1178 | musb_writew(regs, MUSB_RXCSR, csr); |
| 1179 | } |
| 1180 | |
| 1181 | /* NOTE: all the I/O code _should_ work fine without DMA, in case |
| 1182 | * for some reason you run out of channels here. |
| 1183 | */ |
| 1184 | if (is_dma_capable() && musb->dma_controller) { |
| 1185 | struct dma_controller *c = musb->dma_controller; |
| 1186 | |
| 1187 | musb_ep->dma = c->channel_alloc(c, hw_ep, |
| 1188 | (desc->bEndpointAddress & USB_DIR_IN)); |
| 1189 | } else |
| 1190 | musb_ep->dma = NULL; |
| 1191 | |
qianfan Zhao | 307be401 | 2021-11-16 08:30:12 +0800 | [diff] [blame] | 1192 | musb_ep->end_point.desc = desc; |
Ilya Yanok | 06bb920 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 1193 | musb_ep->desc = desc; |
| 1194 | musb_ep->busy = 0; |
| 1195 | musb_ep->wedged = 0; |
| 1196 | status = 0; |
| 1197 | |
| 1198 | pr_debug("%s periph: enabled %s for %s %s, %smaxpacket %d\n", |
| 1199 | musb_driver_name, musb_ep->end_point.name, |
| 1200 | ({ char *s; switch (musb_ep->type) { |
| 1201 | case USB_ENDPOINT_XFER_BULK: s = "bulk"; break; |
| 1202 | case USB_ENDPOINT_XFER_INT: s = "int"; break; |
| 1203 | default: s = "iso"; break; |
| 1204 | }; s; }), |
| 1205 | musb_ep->is_in ? "IN" : "OUT", |
| 1206 | musb_ep->dma ? "dma, " : "", |
| 1207 | musb_ep->packet_sz); |
| 1208 | |
| 1209 | schedule_work(&musb->irq_work); |
| 1210 | |
| 1211 | fail: |
| 1212 | spin_unlock_irqrestore(&musb->lock, flags); |
| 1213 | return status; |
| 1214 | } |
| 1215 | |
| 1216 | /* |
| 1217 | * Disable an endpoint flushing all requests queued. |
| 1218 | */ |
| 1219 | static int musb_gadget_disable(struct usb_ep *ep) |
| 1220 | { |
| 1221 | unsigned long flags; |
| 1222 | struct musb *musb; |
| 1223 | u8 epnum; |
| 1224 | struct musb_ep *musb_ep; |
| 1225 | void __iomem *epio; |
| 1226 | int status = 0; |
| 1227 | |
| 1228 | musb_ep = to_musb_ep(ep); |
| 1229 | musb = musb_ep->musb; |
| 1230 | epnum = musb_ep->current_epnum; |
| 1231 | epio = musb->endpoints[epnum].regs; |
| 1232 | |
| 1233 | spin_lock_irqsave(&musb->lock, flags); |
| 1234 | musb_ep_select(musb->mregs, epnum); |
| 1235 | |
| 1236 | /* zero the endpoint sizes */ |
| 1237 | if (musb_ep->is_in) { |
| 1238 | u16 int_txe = musb_readw(musb->mregs, MUSB_INTRTXE); |
| 1239 | int_txe &= ~(1 << epnum); |
| 1240 | musb_writew(musb->mregs, MUSB_INTRTXE, int_txe); |
| 1241 | musb_writew(epio, MUSB_TXMAXP, 0); |
| 1242 | } else { |
| 1243 | u16 int_rxe = musb_readw(musb->mregs, MUSB_INTRRXE); |
| 1244 | int_rxe &= ~(1 << epnum); |
| 1245 | musb_writew(musb->mregs, MUSB_INTRRXE, int_rxe); |
| 1246 | musb_writew(epio, MUSB_RXMAXP, 0); |
| 1247 | } |
| 1248 | |
| 1249 | musb_ep->desc = NULL; |
Ilya Yanok | 06bb920 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 1250 | musb_ep->end_point.desc = NULL; |
Ilya Yanok | 06bb920 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 1251 | |
| 1252 | /* abort all pending DMA and requests */ |
| 1253 | nuke(musb_ep, -ESHUTDOWN); |
| 1254 | |
| 1255 | schedule_work(&musb->irq_work); |
| 1256 | |
| 1257 | spin_unlock_irqrestore(&(musb->lock), flags); |
| 1258 | |
| 1259 | dev_dbg(musb->controller, "%s\n", musb_ep->end_point.name); |
| 1260 | |
| 1261 | return status; |
| 1262 | } |
| 1263 | |
| 1264 | /* |
| 1265 | * Allocate a request for an endpoint. |
| 1266 | * Reused by ep0 code. |
| 1267 | */ |
| 1268 | struct usb_request *musb_alloc_request(struct usb_ep *ep, gfp_t gfp_flags) |
| 1269 | { |
| 1270 | struct musb_ep *musb_ep = to_musb_ep(ep); |
| 1271 | struct musb *musb = musb_ep->musb; |
| 1272 | struct musb_request *request = NULL; |
| 1273 | |
| 1274 | request = kzalloc(sizeof *request, gfp_flags); |
| 1275 | if (!request) { |
| 1276 | dev_dbg(musb->controller, "not enough memory\n"); |
| 1277 | return NULL; |
| 1278 | } |
| 1279 | |
| 1280 | request->request.dma = DMA_ADDR_INVALID; |
| 1281 | request->epnum = musb_ep->current_epnum; |
| 1282 | request->ep = musb_ep; |
| 1283 | |
| 1284 | return &request->request; |
| 1285 | } |
| 1286 | |
| 1287 | /* |
| 1288 | * Free a request |
| 1289 | * Reused by ep0 code. |
| 1290 | */ |
| 1291 | void musb_free_request(struct usb_ep *ep, struct usb_request *req) |
| 1292 | { |
| 1293 | kfree(to_musb_request(req)); |
| 1294 | } |
| 1295 | |
| 1296 | static LIST_HEAD(buffers); |
| 1297 | |
| 1298 | struct free_record { |
| 1299 | struct list_head list; |
| 1300 | struct device *dev; |
| 1301 | unsigned bytes; |
| 1302 | dma_addr_t dma; |
| 1303 | }; |
| 1304 | |
| 1305 | /* |
| 1306 | * Context: controller locked, IRQs blocked. |
| 1307 | */ |
| 1308 | void musb_ep_restart(struct musb *musb, struct musb_request *req) |
| 1309 | { |
| 1310 | dev_dbg(musb->controller, "<== %s request %p len %u on hw_ep%d\n", |
| 1311 | req->tx ? "TX/IN" : "RX/OUT", |
| 1312 | &req->request, req->request.length, req->epnum); |
| 1313 | |
| 1314 | musb_ep_select(musb->mregs, req->epnum); |
| 1315 | if (req->tx) |
| 1316 | txstate(musb, req); |
| 1317 | else |
| 1318 | rxstate(musb, req); |
| 1319 | } |
| 1320 | |
| 1321 | static int musb_gadget_queue(struct usb_ep *ep, struct usb_request *req, |
| 1322 | gfp_t gfp_flags) |
| 1323 | { |
| 1324 | struct musb_ep *musb_ep; |
| 1325 | struct musb_request *request; |
| 1326 | struct musb *musb; |
| 1327 | int status = 0; |
| 1328 | unsigned long lockflags; |
| 1329 | |
| 1330 | if (!ep || !req) |
| 1331 | return -EINVAL; |
| 1332 | if (!req->buf) |
| 1333 | return -ENODATA; |
| 1334 | |
| 1335 | musb_ep = to_musb_ep(ep); |
| 1336 | musb = musb_ep->musb; |
| 1337 | |
| 1338 | request = to_musb_request(req); |
| 1339 | request->musb = musb; |
| 1340 | |
| 1341 | if (request->ep != musb_ep) |
| 1342 | return -EINVAL; |
| 1343 | |
| 1344 | dev_dbg(musb->controller, "<== to %s request=%p\n", ep->name, req); |
| 1345 | |
| 1346 | /* request is mine now... */ |
| 1347 | request->request.actual = 0; |
| 1348 | request->request.status = -EINPROGRESS; |
| 1349 | request->epnum = musb_ep->current_epnum; |
| 1350 | request->tx = musb_ep->is_in; |
| 1351 | |
| 1352 | map_dma_buffer(request, musb, musb_ep); |
| 1353 | |
| 1354 | spin_lock_irqsave(&musb->lock, lockflags); |
| 1355 | |
| 1356 | /* don't queue if the ep is down */ |
| 1357 | if (!musb_ep->desc) { |
| 1358 | dev_dbg(musb->controller, "req %p queued to %s while ep %s\n", |
| 1359 | req, ep->name, "disabled"); |
| 1360 | status = -ESHUTDOWN; |
| 1361 | goto cleanup; |
| 1362 | } |
| 1363 | |
| 1364 | /* add request to the list */ |
| 1365 | list_add_tail(&request->list, &musb_ep->req_list); |
| 1366 | |
| 1367 | /* it this is the head of the queue, start i/o ... */ |
| 1368 | if (!musb_ep->busy && &request->list == musb_ep->req_list.next) |
| 1369 | musb_ep_restart(musb, request); |
| 1370 | |
| 1371 | cleanup: |
| 1372 | spin_unlock_irqrestore(&musb->lock, lockflags); |
| 1373 | return status; |
| 1374 | } |
| 1375 | |
| 1376 | static int musb_gadget_dequeue(struct usb_ep *ep, struct usb_request *request) |
| 1377 | { |
| 1378 | struct musb_ep *musb_ep = to_musb_ep(ep); |
| 1379 | struct musb_request *req = to_musb_request(request); |
| 1380 | struct musb_request *r; |
| 1381 | unsigned long flags; |
| 1382 | int status = 0; |
| 1383 | struct musb *musb = musb_ep->musb; |
| 1384 | |
| 1385 | if (!ep || !request || to_musb_request(request)->ep != musb_ep) |
| 1386 | return -EINVAL; |
| 1387 | |
| 1388 | spin_lock_irqsave(&musb->lock, flags); |
| 1389 | |
| 1390 | list_for_each_entry(r, &musb_ep->req_list, list) { |
| 1391 | if (r == req) |
| 1392 | break; |
| 1393 | } |
| 1394 | if (r != req) { |
| 1395 | dev_dbg(musb->controller, "request %p not queued to %s\n", request, ep->name); |
| 1396 | status = -EINVAL; |
| 1397 | goto done; |
| 1398 | } |
| 1399 | |
| 1400 | /* if the hardware doesn't have the request, easy ... */ |
| 1401 | if (musb_ep->req_list.next != &req->list || musb_ep->busy) |
| 1402 | musb_g_giveback(musb_ep, request, -ECONNRESET); |
| 1403 | |
| 1404 | /* ... else abort the dma transfer ... */ |
| 1405 | else if (is_dma_capable() && musb_ep->dma) { |
| 1406 | struct dma_controller *c = musb->dma_controller; |
| 1407 | |
| 1408 | musb_ep_select(musb->mregs, musb_ep->current_epnum); |
| 1409 | if (c->channel_abort) |
| 1410 | status = c->channel_abort(musb_ep->dma); |
| 1411 | else |
| 1412 | status = -EBUSY; |
| 1413 | if (status == 0) |
| 1414 | musb_g_giveback(musb_ep, request, -ECONNRESET); |
| 1415 | } else { |
| 1416 | /* NOTE: by sticking to easily tested hardware/driver states, |
| 1417 | * we leave counting of in-flight packets imprecise. |
| 1418 | */ |
| 1419 | musb_g_giveback(musb_ep, request, -ECONNRESET); |
| 1420 | } |
| 1421 | |
| 1422 | done: |
| 1423 | spin_unlock_irqrestore(&musb->lock, flags); |
| 1424 | return status; |
| 1425 | } |
| 1426 | |
| 1427 | /* |
| 1428 | * Set or clear the halt bit of an endpoint. A halted enpoint won't tx/rx any |
| 1429 | * data but will queue requests. |
| 1430 | * |
| 1431 | * exported to ep0 code |
| 1432 | */ |
| 1433 | static int musb_gadget_set_halt(struct usb_ep *ep, int value) |
| 1434 | { |
| 1435 | struct musb_ep *musb_ep = to_musb_ep(ep); |
| 1436 | u8 epnum = musb_ep->current_epnum; |
| 1437 | struct musb *musb = musb_ep->musb; |
| 1438 | void __iomem *epio = musb->endpoints[epnum].regs; |
| 1439 | void __iomem *mbase; |
| 1440 | unsigned long flags; |
| 1441 | u16 csr; |
| 1442 | struct musb_request *request; |
| 1443 | int status = 0; |
| 1444 | |
| 1445 | if (!ep) |
| 1446 | return -EINVAL; |
| 1447 | mbase = musb->mregs; |
| 1448 | |
| 1449 | spin_lock_irqsave(&musb->lock, flags); |
| 1450 | |
| 1451 | if ((USB_ENDPOINT_XFER_ISOC == musb_ep->type)) { |
| 1452 | status = -EINVAL; |
| 1453 | goto done; |
| 1454 | } |
| 1455 | |
| 1456 | musb_ep_select(mbase, epnum); |
| 1457 | |
| 1458 | request = next_request(musb_ep); |
| 1459 | if (value) { |
| 1460 | if (request) { |
| 1461 | dev_dbg(musb->controller, "request in progress, cannot halt %s\n", |
| 1462 | ep->name); |
| 1463 | status = -EAGAIN; |
| 1464 | goto done; |
| 1465 | } |
| 1466 | /* Cannot portably stall with non-empty FIFO */ |
| 1467 | if (musb_ep->is_in) { |
| 1468 | csr = musb_readw(epio, MUSB_TXCSR); |
| 1469 | if (csr & MUSB_TXCSR_FIFONOTEMPTY) { |
| 1470 | dev_dbg(musb->controller, "FIFO busy, cannot halt %s\n", ep->name); |
| 1471 | status = -EAGAIN; |
| 1472 | goto done; |
| 1473 | } |
| 1474 | } |
| 1475 | } else |
| 1476 | musb_ep->wedged = 0; |
| 1477 | |
| 1478 | /* set/clear the stall and toggle bits */ |
| 1479 | dev_dbg(musb->controller, "%s: %s stall\n", ep->name, value ? "set" : "clear"); |
| 1480 | if (musb_ep->is_in) { |
| 1481 | csr = musb_readw(epio, MUSB_TXCSR); |
| 1482 | csr |= MUSB_TXCSR_P_WZC_BITS |
| 1483 | | MUSB_TXCSR_CLRDATATOG; |
| 1484 | if (value) |
| 1485 | csr |= MUSB_TXCSR_P_SENDSTALL; |
| 1486 | else |
| 1487 | csr &= ~(MUSB_TXCSR_P_SENDSTALL |
| 1488 | | MUSB_TXCSR_P_SENTSTALL); |
| 1489 | csr &= ~MUSB_TXCSR_TXPKTRDY; |
| 1490 | musb_writew(epio, MUSB_TXCSR, csr); |
| 1491 | } else { |
| 1492 | csr = musb_readw(epio, MUSB_RXCSR); |
| 1493 | csr |= MUSB_RXCSR_P_WZC_BITS |
| 1494 | | MUSB_RXCSR_FLUSHFIFO |
| 1495 | | MUSB_RXCSR_CLRDATATOG; |
| 1496 | if (value) |
| 1497 | csr |= MUSB_RXCSR_P_SENDSTALL; |
| 1498 | else |
| 1499 | csr &= ~(MUSB_RXCSR_P_SENDSTALL |
| 1500 | | MUSB_RXCSR_P_SENTSTALL); |
| 1501 | musb_writew(epio, MUSB_RXCSR, csr); |
| 1502 | } |
| 1503 | |
| 1504 | /* maybe start the first request in the queue */ |
| 1505 | if (!musb_ep->busy && !value && request) { |
| 1506 | dev_dbg(musb->controller, "restarting the request\n"); |
| 1507 | musb_ep_restart(musb, request); |
| 1508 | } |
| 1509 | |
| 1510 | done: |
| 1511 | spin_unlock_irqrestore(&musb->lock, flags); |
| 1512 | return status; |
| 1513 | } |
| 1514 | |
| 1515 | #ifndef __UBOOT__ |
| 1516 | /* |
| 1517 | * Sets the halt feature with the clear requests ignored |
| 1518 | */ |
| 1519 | static int musb_gadget_set_wedge(struct usb_ep *ep) |
| 1520 | { |
| 1521 | struct musb_ep *musb_ep = to_musb_ep(ep); |
| 1522 | |
| 1523 | if (!ep) |
| 1524 | return -EINVAL; |
| 1525 | |
| 1526 | musb_ep->wedged = 1; |
| 1527 | |
| 1528 | return usb_ep_set_halt(ep); |
| 1529 | } |
| 1530 | #endif |
| 1531 | |
| 1532 | static int musb_gadget_fifo_status(struct usb_ep *ep) |
| 1533 | { |
| 1534 | struct musb_ep *musb_ep = to_musb_ep(ep); |
| 1535 | void __iomem *epio = musb_ep->hw_ep->regs; |
| 1536 | int retval = -EINVAL; |
| 1537 | |
| 1538 | if (musb_ep->desc && !musb_ep->is_in) { |
| 1539 | struct musb *musb = musb_ep->musb; |
| 1540 | int epnum = musb_ep->current_epnum; |
| 1541 | void __iomem *mbase = musb->mregs; |
| 1542 | unsigned long flags; |
| 1543 | |
| 1544 | spin_lock_irqsave(&musb->lock, flags); |
| 1545 | |
| 1546 | musb_ep_select(mbase, epnum); |
| 1547 | /* FIXME return zero unless RXPKTRDY is set */ |
| 1548 | retval = musb_readw(epio, MUSB_RXCOUNT); |
| 1549 | |
| 1550 | spin_unlock_irqrestore(&musb->lock, flags); |
| 1551 | } |
| 1552 | return retval; |
| 1553 | } |
| 1554 | |
| 1555 | static void musb_gadget_fifo_flush(struct usb_ep *ep) |
| 1556 | { |
| 1557 | struct musb_ep *musb_ep = to_musb_ep(ep); |
| 1558 | struct musb *musb = musb_ep->musb; |
| 1559 | u8 epnum = musb_ep->current_epnum; |
| 1560 | void __iomem *epio = musb->endpoints[epnum].regs; |
| 1561 | void __iomem *mbase; |
| 1562 | unsigned long flags; |
| 1563 | u16 csr, int_txe; |
| 1564 | |
| 1565 | mbase = musb->mregs; |
| 1566 | |
| 1567 | spin_lock_irqsave(&musb->lock, flags); |
| 1568 | musb_ep_select(mbase, (u8) epnum); |
| 1569 | |
| 1570 | /* disable interrupts */ |
| 1571 | int_txe = musb_readw(mbase, MUSB_INTRTXE); |
| 1572 | musb_writew(mbase, MUSB_INTRTXE, int_txe & ~(1 << epnum)); |
| 1573 | |
| 1574 | if (musb_ep->is_in) { |
| 1575 | csr = musb_readw(epio, MUSB_TXCSR); |
| 1576 | if (csr & MUSB_TXCSR_FIFONOTEMPTY) { |
| 1577 | csr |= MUSB_TXCSR_FLUSHFIFO | MUSB_TXCSR_P_WZC_BITS; |
| 1578 | /* |
| 1579 | * Setting both TXPKTRDY and FLUSHFIFO makes controller |
| 1580 | * to interrupt current FIFO loading, but not flushing |
| 1581 | * the already loaded ones. |
| 1582 | */ |
| 1583 | csr &= ~MUSB_TXCSR_TXPKTRDY; |
| 1584 | musb_writew(epio, MUSB_TXCSR, csr); |
| 1585 | /* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */ |
| 1586 | musb_writew(epio, MUSB_TXCSR, csr); |
| 1587 | } |
| 1588 | } else { |
| 1589 | csr = musb_readw(epio, MUSB_RXCSR); |
| 1590 | csr |= MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_P_WZC_BITS; |
| 1591 | musb_writew(epio, MUSB_RXCSR, csr); |
| 1592 | musb_writew(epio, MUSB_RXCSR, csr); |
| 1593 | } |
| 1594 | |
| 1595 | /* re-enable interrupt */ |
| 1596 | musb_writew(mbase, MUSB_INTRTXE, int_txe); |
| 1597 | spin_unlock_irqrestore(&musb->lock, flags); |
| 1598 | } |
| 1599 | |
| 1600 | static const struct usb_ep_ops musb_ep_ops = { |
| 1601 | .enable = musb_gadget_enable, |
| 1602 | .disable = musb_gadget_disable, |
| 1603 | .alloc_request = musb_alloc_request, |
| 1604 | .free_request = musb_free_request, |
| 1605 | .queue = musb_gadget_queue, |
| 1606 | .dequeue = musb_gadget_dequeue, |
| 1607 | .set_halt = musb_gadget_set_halt, |
| 1608 | #ifndef __UBOOT__ |
| 1609 | .set_wedge = musb_gadget_set_wedge, |
| 1610 | #endif |
| 1611 | .fifo_status = musb_gadget_fifo_status, |
| 1612 | .fifo_flush = musb_gadget_fifo_flush |
| 1613 | }; |
| 1614 | |
| 1615 | /* ----------------------------------------------------------------------- */ |
| 1616 | |
| 1617 | static int musb_gadget_get_frame(struct usb_gadget *gadget) |
| 1618 | { |
| 1619 | struct musb *musb = gadget_to_musb(gadget); |
| 1620 | |
| 1621 | return (int)musb_readw(musb->mregs, MUSB_FRAME); |
| 1622 | } |
| 1623 | |
| 1624 | static int musb_gadget_wakeup(struct usb_gadget *gadget) |
| 1625 | { |
| 1626 | #ifndef __UBOOT__ |
| 1627 | struct musb *musb = gadget_to_musb(gadget); |
| 1628 | void __iomem *mregs = musb->mregs; |
| 1629 | unsigned long flags; |
| 1630 | int status = -EINVAL; |
| 1631 | u8 power, devctl; |
| 1632 | int retries; |
| 1633 | |
| 1634 | spin_lock_irqsave(&musb->lock, flags); |
| 1635 | |
| 1636 | switch (musb->xceiv->state) { |
| 1637 | case OTG_STATE_B_PERIPHERAL: |
| 1638 | /* NOTE: OTG state machine doesn't include B_SUSPENDED; |
| 1639 | * that's part of the standard usb 1.1 state machine, and |
| 1640 | * doesn't affect OTG transitions. |
| 1641 | */ |
| 1642 | if (musb->may_wakeup && musb->is_suspended) |
| 1643 | break; |
| 1644 | goto done; |
| 1645 | case OTG_STATE_B_IDLE: |
| 1646 | /* Start SRP ... OTG not required. */ |
| 1647 | devctl = musb_readb(mregs, MUSB_DEVCTL); |
| 1648 | dev_dbg(musb->controller, "Sending SRP: devctl: %02x\n", devctl); |
| 1649 | devctl |= MUSB_DEVCTL_SESSION; |
| 1650 | musb_writeb(mregs, MUSB_DEVCTL, devctl); |
| 1651 | devctl = musb_readb(mregs, MUSB_DEVCTL); |
| 1652 | retries = 100; |
| 1653 | while (!(devctl & MUSB_DEVCTL_SESSION)) { |
| 1654 | devctl = musb_readb(mregs, MUSB_DEVCTL); |
| 1655 | if (retries-- < 1) |
| 1656 | break; |
| 1657 | } |
| 1658 | retries = 10000; |
| 1659 | while (devctl & MUSB_DEVCTL_SESSION) { |
| 1660 | devctl = musb_readb(mregs, MUSB_DEVCTL); |
| 1661 | if (retries-- < 1) |
| 1662 | break; |
| 1663 | } |
| 1664 | |
| 1665 | spin_unlock_irqrestore(&musb->lock, flags); |
| 1666 | otg_start_srp(musb->xceiv->otg); |
| 1667 | spin_lock_irqsave(&musb->lock, flags); |
| 1668 | |
| 1669 | /* Block idling for at least 1s */ |
| 1670 | musb_platform_try_idle(musb, |
| 1671 | jiffies + msecs_to_jiffies(1 * HZ)); |
| 1672 | |
| 1673 | status = 0; |
| 1674 | goto done; |
| 1675 | default: |
| 1676 | dev_dbg(musb->controller, "Unhandled wake: %s\n", |
| 1677 | otg_state_string(musb->xceiv->state)); |
| 1678 | goto done; |
| 1679 | } |
| 1680 | |
| 1681 | status = 0; |
| 1682 | |
| 1683 | power = musb_readb(mregs, MUSB_POWER); |
| 1684 | power |= MUSB_POWER_RESUME; |
| 1685 | musb_writeb(mregs, MUSB_POWER, power); |
| 1686 | dev_dbg(musb->controller, "issue wakeup\n"); |
| 1687 | |
| 1688 | /* FIXME do this next chunk in a timer callback, no udelay */ |
| 1689 | mdelay(2); |
| 1690 | |
| 1691 | power = musb_readb(mregs, MUSB_POWER); |
| 1692 | power &= ~MUSB_POWER_RESUME; |
| 1693 | musb_writeb(mregs, MUSB_POWER, power); |
| 1694 | done: |
| 1695 | spin_unlock_irqrestore(&musb->lock, flags); |
| 1696 | return status; |
| 1697 | #else |
| 1698 | return 0; |
| 1699 | #endif |
| 1700 | } |
| 1701 | |
| 1702 | static int |
| 1703 | musb_gadget_set_self_powered(struct usb_gadget *gadget, int is_selfpowered) |
| 1704 | { |
| 1705 | struct musb *musb = gadget_to_musb(gadget); |
| 1706 | |
| 1707 | musb->is_self_powered = !!is_selfpowered; |
| 1708 | return 0; |
| 1709 | } |
| 1710 | |
| 1711 | static void musb_pullup(struct musb *musb, int is_on) |
| 1712 | { |
| 1713 | u8 power; |
| 1714 | |
| 1715 | power = musb_readb(musb->mregs, MUSB_POWER); |
| 1716 | if (is_on) |
| 1717 | power |= MUSB_POWER_SOFTCONN; |
| 1718 | else |
| 1719 | power &= ~MUSB_POWER_SOFTCONN; |
| 1720 | |
| 1721 | /* FIXME if on, HdrcStart; if off, HdrcStop */ |
| 1722 | |
| 1723 | dev_dbg(musb->controller, "gadget D+ pullup %s\n", |
| 1724 | is_on ? "on" : "off"); |
| 1725 | musb_writeb(musb->mregs, MUSB_POWER, power); |
| 1726 | } |
| 1727 | |
| 1728 | #if 0 |
| 1729 | static int musb_gadget_vbus_session(struct usb_gadget *gadget, int is_active) |
| 1730 | { |
| 1731 | dev_dbg(musb->controller, "<= %s =>\n", __func__); |
| 1732 | |
| 1733 | /* |
| 1734 | * FIXME iff driver's softconnect flag is set (as it is during probe, |
| 1735 | * though that can clear it), just musb_pullup(). |
| 1736 | */ |
| 1737 | |
| 1738 | return -EINVAL; |
| 1739 | } |
| 1740 | #endif |
| 1741 | |
| 1742 | static int musb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA) |
| 1743 | { |
| 1744 | #ifndef __UBOOT__ |
| 1745 | struct musb *musb = gadget_to_musb(gadget); |
| 1746 | |
| 1747 | if (!musb->xceiv->set_power) |
| 1748 | return -EOPNOTSUPP; |
| 1749 | return usb_phy_set_power(musb->xceiv, mA); |
| 1750 | #else |
| 1751 | return 0; |
| 1752 | #endif |
| 1753 | } |
| 1754 | |
| 1755 | static int musb_gadget_pullup(struct usb_gadget *gadget, int is_on) |
| 1756 | { |
| 1757 | struct musb *musb = gadget_to_musb(gadget); |
| 1758 | unsigned long flags; |
| 1759 | |
| 1760 | is_on = !!is_on; |
| 1761 | |
| 1762 | pm_runtime_get_sync(musb->controller); |
| 1763 | |
| 1764 | /* NOTE: this assumes we are sensing vbus; we'd rather |
| 1765 | * not pullup unless the B-session is active. |
| 1766 | */ |
| 1767 | spin_lock_irqsave(&musb->lock, flags); |
| 1768 | if (is_on != musb->softconnect) { |
| 1769 | musb->softconnect = is_on; |
| 1770 | musb_pullup(musb, is_on); |
| 1771 | } |
| 1772 | spin_unlock_irqrestore(&musb->lock, flags); |
| 1773 | |
| 1774 | pm_runtime_put(musb->controller); |
| 1775 | |
| 1776 | return 0; |
| 1777 | } |
| 1778 | |
| 1779 | #ifndef __UBOOT__ |
| 1780 | static int musb_gadget_start(struct usb_gadget *g, |
| 1781 | struct usb_gadget_driver *driver); |
| 1782 | static int musb_gadget_stop(struct usb_gadget *g, |
| 1783 | struct usb_gadget_driver *driver); |
Jean-Jacques Hiblot | 57118f6 | 2018-12-04 11:30:57 +0100 | [diff] [blame] | 1784 | #else |
| 1785 | static int musb_gadget_stop(struct usb_gadget *g) |
| 1786 | { |
| 1787 | struct musb *musb = gadget_to_musb(g); |
| 1788 | |
| 1789 | musb_stop(musb); |
| 1790 | return 0; |
| 1791 | } |
Ilya Yanok | 06bb920 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 1792 | #endif |
| 1793 | |
| 1794 | static const struct usb_gadget_ops musb_gadget_operations = { |
| 1795 | .get_frame = musb_gadget_get_frame, |
| 1796 | .wakeup = musb_gadget_wakeup, |
| 1797 | .set_selfpowered = musb_gadget_set_self_powered, |
| 1798 | /* .vbus_session = musb_gadget_vbus_session, */ |
| 1799 | .vbus_draw = musb_gadget_vbus_draw, |
| 1800 | .pullup = musb_gadget_pullup, |
| 1801 | #ifndef __UBOOT__ |
| 1802 | .udc_start = musb_gadget_start, |
| 1803 | .udc_stop = musb_gadget_stop, |
Jean-Jacques Hiblot | 57118f6 | 2018-12-04 11:30:57 +0100 | [diff] [blame] | 1804 | #else |
| 1805 | .udc_start = musb_gadget_start, |
| 1806 | .udc_stop = musb_gadget_stop, |
Ilya Yanok | 06bb920 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 1807 | #endif |
| 1808 | }; |
| 1809 | |
| 1810 | /* ----------------------------------------------------------------------- */ |
| 1811 | |
| 1812 | /* Registration */ |
| 1813 | |
| 1814 | /* Only this registration code "knows" the rule (from USB standards) |
| 1815 | * about there being only one external upstream port. It assumes |
| 1816 | * all peripheral ports are external... |
| 1817 | */ |
| 1818 | |
| 1819 | #ifndef __UBOOT__ |
| 1820 | static void musb_gadget_release(struct device *dev) |
| 1821 | { |
| 1822 | /* kref_put(WHAT) */ |
| 1823 | dev_dbg(dev, "%s\n", __func__); |
| 1824 | } |
| 1825 | #endif |
| 1826 | |
| 1827 | |
| 1828 | static void __devinit |
| 1829 | init_peripheral_ep(struct musb *musb, struct musb_ep *ep, u8 epnum, int is_in) |
| 1830 | { |
| 1831 | struct musb_hw_ep *hw_ep = musb->endpoints + epnum; |
| 1832 | |
| 1833 | memset(ep, 0, sizeof *ep); |
| 1834 | |
| 1835 | ep->current_epnum = epnum; |
| 1836 | ep->musb = musb; |
| 1837 | ep->hw_ep = hw_ep; |
| 1838 | ep->is_in = is_in; |
| 1839 | |
| 1840 | INIT_LIST_HEAD(&ep->req_list); |
| 1841 | |
| 1842 | sprintf(ep->name, "ep%d%s", epnum, |
| 1843 | (!epnum || hw_ep->is_shared_fifo) ? "" : ( |
| 1844 | is_in ? "in" : "out")); |
| 1845 | ep->end_point.name = ep->name; |
| 1846 | INIT_LIST_HEAD(&ep->end_point.ep_list); |
| 1847 | if (!epnum) { |
| 1848 | ep->end_point.maxpacket = 64; |
| 1849 | ep->end_point.ops = &musb_g_ep0_ops; |
| 1850 | musb->g.ep0 = &ep->end_point; |
| 1851 | } else { |
| 1852 | if (is_in) |
| 1853 | ep->end_point.maxpacket = hw_ep->max_packet_sz_tx; |
| 1854 | else |
| 1855 | ep->end_point.maxpacket = hw_ep->max_packet_sz_rx; |
| 1856 | ep->end_point.ops = &musb_ep_ops; |
| 1857 | list_add_tail(&ep->end_point.ep_list, &musb->g.ep_list); |
| 1858 | } |
| 1859 | } |
| 1860 | |
| 1861 | /* |
| 1862 | * Initialize the endpoints exposed to peripheral drivers, with backlinks |
| 1863 | * to the rest of the driver state. |
| 1864 | */ |
| 1865 | static inline void __devinit musb_g_init_endpoints(struct musb *musb) |
| 1866 | { |
| 1867 | u8 epnum; |
| 1868 | struct musb_hw_ep *hw_ep; |
| 1869 | unsigned count = 0; |
| 1870 | |
| 1871 | /* initialize endpoint list just once */ |
| 1872 | INIT_LIST_HEAD(&(musb->g.ep_list)); |
| 1873 | |
| 1874 | for (epnum = 0, hw_ep = musb->endpoints; |
| 1875 | epnum < musb->nr_endpoints; |
| 1876 | epnum++, hw_ep++) { |
| 1877 | if (hw_ep->is_shared_fifo /* || !epnum */) { |
| 1878 | init_peripheral_ep(musb, &hw_ep->ep_in, epnum, 0); |
| 1879 | count++; |
| 1880 | } else { |
| 1881 | if (hw_ep->max_packet_sz_tx) { |
| 1882 | init_peripheral_ep(musb, &hw_ep->ep_in, |
| 1883 | epnum, 1); |
| 1884 | count++; |
| 1885 | } |
| 1886 | if (hw_ep->max_packet_sz_rx) { |
| 1887 | init_peripheral_ep(musb, &hw_ep->ep_out, |
| 1888 | epnum, 0); |
| 1889 | count++; |
| 1890 | } |
| 1891 | } |
| 1892 | } |
| 1893 | } |
| 1894 | |
| 1895 | /* called once during driver setup to initialize and link into |
| 1896 | * the driver model; memory is zeroed. |
| 1897 | */ |
| 1898 | int __devinit musb_gadget_setup(struct musb *musb) |
| 1899 | { |
| 1900 | int status; |
| 1901 | |
| 1902 | /* REVISIT minor race: if (erroneously) setting up two |
| 1903 | * musb peripherals at the same time, only the bus lock |
| 1904 | * is probably held. |
| 1905 | */ |
| 1906 | |
| 1907 | musb->g.ops = &musb_gadget_operations; |
| 1908 | #ifndef __UBOOT__ |
| 1909 | musb->g.max_speed = USB_SPEED_HIGH; |
| 1910 | #endif |
| 1911 | musb->g.speed = USB_SPEED_UNKNOWN; |
| 1912 | |
| 1913 | #ifndef __UBOOT__ |
| 1914 | /* this "gadget" abstracts/virtualizes the controller */ |
| 1915 | dev_set_name(&musb->g.dev, "gadget"); |
| 1916 | musb->g.dev.parent = musb->controller; |
| 1917 | musb->g.dev.dma_mask = musb->controller->dma_mask; |
| 1918 | musb->g.dev.release = musb_gadget_release; |
| 1919 | #endif |
| 1920 | musb->g.name = musb_driver_name; |
| 1921 | |
| 1922 | #ifndef __UBOOT__ |
| 1923 | if (is_otg_enabled(musb)) |
| 1924 | musb->g.is_otg = 1; |
| 1925 | #endif |
| 1926 | |
| 1927 | musb_g_init_endpoints(musb); |
| 1928 | |
| 1929 | musb->is_active = 0; |
| 1930 | musb_platform_try_idle(musb, 0); |
| 1931 | |
| 1932 | #ifndef __UBOOT__ |
| 1933 | status = device_register(&musb->g.dev); |
| 1934 | if (status != 0) { |
| 1935 | put_device(&musb->g.dev); |
| 1936 | return status; |
| 1937 | } |
| 1938 | status = usb_add_gadget_udc(musb->controller, &musb->g); |
| 1939 | if (status) |
| 1940 | goto err; |
| 1941 | #endif |
| 1942 | |
| 1943 | return 0; |
| 1944 | #ifndef __UBOOT__ |
| 1945 | err: |
| 1946 | musb->g.dev.parent = NULL; |
| 1947 | device_unregister(&musb->g.dev); |
| 1948 | return status; |
| 1949 | #endif |
| 1950 | } |
| 1951 | |
| 1952 | void musb_gadget_cleanup(struct musb *musb) |
| 1953 | { |
| 1954 | #ifndef __UBOOT__ |
| 1955 | usb_del_gadget_udc(&musb->g); |
| 1956 | if (musb->g.dev.parent) |
| 1957 | device_unregister(&musb->g.dev); |
| 1958 | #endif |
| 1959 | } |
| 1960 | |
| 1961 | /* |
| 1962 | * Register the gadget driver. Used by gadget drivers when |
| 1963 | * registering themselves with the controller. |
| 1964 | * |
| 1965 | * -EINVAL something went wrong (not driver) |
| 1966 | * -EBUSY another gadget is already using the controller |
| 1967 | * -ENOMEM no memory to perform the operation |
| 1968 | * |
| 1969 | * @param driver the gadget driver |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 1970 | * Return: <0 if error, 0 if everything is fine |
Ilya Yanok | 06bb920 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 1971 | */ |
| 1972 | #ifndef __UBOOT__ |
| 1973 | static int musb_gadget_start(struct usb_gadget *g, |
| 1974 | struct usb_gadget_driver *driver) |
| 1975 | #else |
| 1976 | int musb_gadget_start(struct usb_gadget *g, |
| 1977 | struct usb_gadget_driver *driver) |
| 1978 | #endif |
| 1979 | { |
| 1980 | struct musb *musb = gadget_to_musb(g); |
| 1981 | #ifndef __UBOOT__ |
| 1982 | struct usb_otg *otg = musb->xceiv->otg; |
| 1983 | #endif |
| 1984 | unsigned long flags; |
| 1985 | int retval = -EINVAL; |
| 1986 | |
| 1987 | #ifndef __UBOOT__ |
| 1988 | if (driver->max_speed < USB_SPEED_HIGH) |
| 1989 | goto err0; |
| 1990 | #endif |
| 1991 | |
| 1992 | pm_runtime_get_sync(musb->controller); |
| 1993 | |
| 1994 | #ifndef __UBOOT__ |
| 1995 | dev_dbg(musb->controller, "registering driver %s\n", driver->function); |
| 1996 | #endif |
| 1997 | |
| 1998 | musb->softconnect = 0; |
| 1999 | musb->gadget_driver = driver; |
| 2000 | |
| 2001 | spin_lock_irqsave(&musb->lock, flags); |
| 2002 | musb->is_active = 1; |
| 2003 | |
| 2004 | #ifndef __UBOOT__ |
| 2005 | otg_set_peripheral(otg, &musb->g); |
| 2006 | musb->xceiv->state = OTG_STATE_B_IDLE; |
| 2007 | |
| 2008 | /* |
| 2009 | * FIXME this ignores the softconnect flag. Drivers are |
| 2010 | * allowed hold the peripheral inactive until for example |
| 2011 | * userspace hooks up printer hardware or DSP codecs, so |
| 2012 | * hosts only see fully functional devices. |
| 2013 | */ |
| 2014 | |
| 2015 | if (!is_otg_enabled(musb)) |
| 2016 | #endif |
| 2017 | musb_start(musb); |
| 2018 | |
| 2019 | spin_unlock_irqrestore(&musb->lock, flags); |
| 2020 | |
| 2021 | #ifndef __UBOOT__ |
| 2022 | if (is_otg_enabled(musb)) { |
| 2023 | struct usb_hcd *hcd = musb_to_hcd(musb); |
| 2024 | |
| 2025 | dev_dbg(musb->controller, "OTG startup...\n"); |
| 2026 | |
| 2027 | /* REVISIT: funcall to other code, which also |
| 2028 | * handles power budgeting ... this way also |
| 2029 | * ensures HdrcStart is indirectly called. |
| 2030 | */ |
| 2031 | retval = usb_add_hcd(musb_to_hcd(musb), 0, 0); |
| 2032 | if (retval < 0) { |
| 2033 | dev_dbg(musb->controller, "add_hcd failed, %d\n", retval); |
| 2034 | goto err2; |
| 2035 | } |
| 2036 | |
| 2037 | if ((musb->xceiv->last_event == USB_EVENT_ID) |
| 2038 | && otg->set_vbus) |
| 2039 | otg_set_vbus(otg, 1); |
| 2040 | |
| 2041 | hcd->self.uses_pio_for_control = 1; |
| 2042 | } |
| 2043 | if (musb->xceiv->last_event == USB_EVENT_NONE) |
| 2044 | pm_runtime_put(musb->controller); |
| 2045 | #endif |
| 2046 | |
| 2047 | return 0; |
| 2048 | |
| 2049 | #ifndef __UBOOT__ |
| 2050 | err2: |
| 2051 | if (!is_otg_enabled(musb)) |
| 2052 | musb_stop(musb); |
| 2053 | err0: |
| 2054 | return retval; |
| 2055 | #endif |
| 2056 | } |
| 2057 | |
| 2058 | #ifndef __UBOOT__ |
| 2059 | static void stop_activity(struct musb *musb, struct usb_gadget_driver *driver) |
| 2060 | { |
| 2061 | int i; |
| 2062 | struct musb_hw_ep *hw_ep; |
| 2063 | |
| 2064 | /* don't disconnect if it's not connected */ |
| 2065 | if (musb->g.speed == USB_SPEED_UNKNOWN) |
| 2066 | driver = NULL; |
| 2067 | else |
| 2068 | musb->g.speed = USB_SPEED_UNKNOWN; |
| 2069 | |
| 2070 | /* deactivate the hardware */ |
| 2071 | if (musb->softconnect) { |
| 2072 | musb->softconnect = 0; |
| 2073 | musb_pullup(musb, 0); |
| 2074 | } |
| 2075 | musb_stop(musb); |
| 2076 | |
| 2077 | /* killing any outstanding requests will quiesce the driver; |
| 2078 | * then report disconnect |
| 2079 | */ |
| 2080 | if (driver) { |
| 2081 | for (i = 0, hw_ep = musb->endpoints; |
| 2082 | i < musb->nr_endpoints; |
| 2083 | i++, hw_ep++) { |
| 2084 | musb_ep_select(musb->mregs, i); |
| 2085 | if (hw_ep->is_shared_fifo /* || !epnum */) { |
| 2086 | nuke(&hw_ep->ep_in, -ESHUTDOWN); |
| 2087 | } else { |
| 2088 | if (hw_ep->max_packet_sz_tx) |
| 2089 | nuke(&hw_ep->ep_in, -ESHUTDOWN); |
| 2090 | if (hw_ep->max_packet_sz_rx) |
| 2091 | nuke(&hw_ep->ep_out, -ESHUTDOWN); |
| 2092 | } |
| 2093 | } |
| 2094 | } |
| 2095 | } |
| 2096 | |
| 2097 | /* |
| 2098 | * Unregister the gadget driver. Used by gadget drivers when |
| 2099 | * unregistering themselves from the controller. |
| 2100 | * |
| 2101 | * @param driver the gadget driver to unregister |
| 2102 | */ |
| 2103 | static int musb_gadget_stop(struct usb_gadget *g, |
| 2104 | struct usb_gadget_driver *driver) |
| 2105 | { |
| 2106 | struct musb *musb = gadget_to_musb(g); |
| 2107 | unsigned long flags; |
| 2108 | |
| 2109 | if (musb->xceiv->last_event == USB_EVENT_NONE) |
| 2110 | pm_runtime_get_sync(musb->controller); |
| 2111 | |
| 2112 | /* |
| 2113 | * REVISIT always use otg_set_peripheral() here too; |
| 2114 | * this needs to shut down the OTG engine. |
| 2115 | */ |
| 2116 | |
| 2117 | spin_lock_irqsave(&musb->lock, flags); |
| 2118 | |
| 2119 | musb_hnp_stop(musb); |
| 2120 | |
| 2121 | (void) musb_gadget_vbus_draw(&musb->g, 0); |
| 2122 | |
| 2123 | musb->xceiv->state = OTG_STATE_UNDEFINED; |
| 2124 | stop_activity(musb, driver); |
| 2125 | otg_set_peripheral(musb->xceiv->otg, NULL); |
| 2126 | |
| 2127 | dev_dbg(musb->controller, "unregistering driver %s\n", driver->function); |
| 2128 | |
| 2129 | musb->is_active = 0; |
| 2130 | musb_platform_try_idle(musb, 0); |
| 2131 | spin_unlock_irqrestore(&musb->lock, flags); |
| 2132 | |
| 2133 | if (is_otg_enabled(musb)) { |
| 2134 | usb_remove_hcd(musb_to_hcd(musb)); |
| 2135 | /* FIXME we need to be able to register another |
| 2136 | * gadget driver here and have everything work; |
| 2137 | * that currently misbehaves. |
| 2138 | */ |
| 2139 | } |
| 2140 | |
| 2141 | if (!is_otg_enabled(musb)) |
| 2142 | musb_stop(musb); |
| 2143 | |
| 2144 | pm_runtime_put(musb->controller); |
| 2145 | |
| 2146 | return 0; |
| 2147 | } |
| 2148 | #endif |
| 2149 | |
| 2150 | /* ----------------------------------------------------------------------- */ |
| 2151 | |
| 2152 | /* lifecycle operations called through plat_uds.c */ |
| 2153 | |
| 2154 | void musb_g_resume(struct musb *musb) |
| 2155 | { |
| 2156 | #ifndef __UBOOT__ |
| 2157 | musb->is_suspended = 0; |
| 2158 | switch (musb->xceiv->state) { |
| 2159 | case OTG_STATE_B_IDLE: |
| 2160 | break; |
| 2161 | case OTG_STATE_B_WAIT_ACON: |
| 2162 | case OTG_STATE_B_PERIPHERAL: |
| 2163 | musb->is_active = 1; |
| 2164 | if (musb->gadget_driver && musb->gadget_driver->resume) { |
| 2165 | spin_unlock(&musb->lock); |
| 2166 | musb->gadget_driver->resume(&musb->g); |
| 2167 | spin_lock(&musb->lock); |
| 2168 | } |
| 2169 | break; |
| 2170 | default: |
| 2171 | WARNING("unhandled RESUME transition (%s)\n", |
| 2172 | otg_state_string(musb->xceiv->state)); |
| 2173 | } |
| 2174 | #endif |
| 2175 | } |
| 2176 | |
| 2177 | /* called when SOF packets stop for 3+ msec */ |
| 2178 | void musb_g_suspend(struct musb *musb) |
| 2179 | { |
| 2180 | #ifndef __UBOOT__ |
| 2181 | u8 devctl; |
| 2182 | |
| 2183 | devctl = musb_readb(musb->mregs, MUSB_DEVCTL); |
| 2184 | dev_dbg(musb->controller, "devctl %02x\n", devctl); |
| 2185 | |
| 2186 | switch (musb->xceiv->state) { |
| 2187 | case OTG_STATE_B_IDLE: |
| 2188 | if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS) |
| 2189 | musb->xceiv->state = OTG_STATE_B_PERIPHERAL; |
| 2190 | break; |
| 2191 | case OTG_STATE_B_PERIPHERAL: |
| 2192 | musb->is_suspended = 1; |
| 2193 | if (musb->gadget_driver && musb->gadget_driver->suspend) { |
| 2194 | spin_unlock(&musb->lock); |
| 2195 | musb->gadget_driver->suspend(&musb->g); |
| 2196 | spin_lock(&musb->lock); |
| 2197 | } |
| 2198 | break; |
| 2199 | default: |
| 2200 | /* REVISIT if B_HOST, clear DEVCTL.HOSTREQ; |
| 2201 | * A_PERIPHERAL may need care too |
| 2202 | */ |
| 2203 | WARNING("unhandled SUSPEND transition (%s)\n", |
| 2204 | otg_state_string(musb->xceiv->state)); |
| 2205 | } |
| 2206 | #endif |
| 2207 | } |
| 2208 | |
| 2209 | /* Called during SRP */ |
| 2210 | void musb_g_wakeup(struct musb *musb) |
| 2211 | { |
| 2212 | musb_gadget_wakeup(&musb->g); |
| 2213 | } |
| 2214 | |
| 2215 | /* called when VBUS drops below session threshold, and in other cases */ |
| 2216 | void musb_g_disconnect(struct musb *musb) |
| 2217 | { |
| 2218 | void __iomem *mregs = musb->mregs; |
| 2219 | u8 devctl = musb_readb(mregs, MUSB_DEVCTL); |
| 2220 | |
| 2221 | dev_dbg(musb->controller, "devctl %02x\n", devctl); |
| 2222 | |
| 2223 | /* clear HR */ |
| 2224 | musb_writeb(mregs, MUSB_DEVCTL, devctl & MUSB_DEVCTL_SESSION); |
| 2225 | |
| 2226 | /* don't draw vbus until new b-default session */ |
| 2227 | (void) musb_gadget_vbus_draw(&musb->g, 0); |
| 2228 | |
| 2229 | musb->g.speed = USB_SPEED_UNKNOWN; |
| 2230 | if (musb->gadget_driver && musb->gadget_driver->disconnect) { |
| 2231 | spin_unlock(&musb->lock); |
| 2232 | musb->gadget_driver->disconnect(&musb->g); |
| 2233 | spin_lock(&musb->lock); |
| 2234 | } |
| 2235 | |
| 2236 | #ifndef __UBOOT__ |
| 2237 | switch (musb->xceiv->state) { |
| 2238 | default: |
| 2239 | dev_dbg(musb->controller, "Unhandled disconnect %s, setting a_idle\n", |
| 2240 | otg_state_string(musb->xceiv->state)); |
| 2241 | musb->xceiv->state = OTG_STATE_A_IDLE; |
| 2242 | MUSB_HST_MODE(musb); |
| 2243 | break; |
| 2244 | case OTG_STATE_A_PERIPHERAL: |
| 2245 | musb->xceiv->state = OTG_STATE_A_WAIT_BCON; |
| 2246 | MUSB_HST_MODE(musb); |
| 2247 | break; |
| 2248 | case OTG_STATE_B_WAIT_ACON: |
| 2249 | case OTG_STATE_B_HOST: |
| 2250 | case OTG_STATE_B_PERIPHERAL: |
| 2251 | case OTG_STATE_B_IDLE: |
| 2252 | musb->xceiv->state = OTG_STATE_B_IDLE; |
| 2253 | break; |
| 2254 | case OTG_STATE_B_SRP_INIT: |
| 2255 | break; |
| 2256 | } |
| 2257 | #endif |
| 2258 | |
| 2259 | musb->is_active = 0; |
| 2260 | } |
| 2261 | |
| 2262 | void musb_g_reset(struct musb *musb) |
| 2263 | __releases(musb->lock) |
| 2264 | __acquires(musb->lock) |
| 2265 | { |
| 2266 | void __iomem *mbase = musb->mregs; |
| 2267 | u8 devctl = musb_readb(mbase, MUSB_DEVCTL); |
| 2268 | u8 power; |
| 2269 | |
| 2270 | #ifndef __UBOOT__ |
| 2271 | dev_dbg(musb->controller, "<== %s addr=%x driver '%s'\n", |
| 2272 | (devctl & MUSB_DEVCTL_BDEVICE) |
| 2273 | ? "B-Device" : "A-Device", |
| 2274 | musb_readb(mbase, MUSB_FADDR), |
| 2275 | musb->gadget_driver |
| 2276 | ? musb->gadget_driver->driver.name |
| 2277 | : NULL |
| 2278 | ); |
| 2279 | #endif |
| 2280 | |
| 2281 | /* report disconnect, if we didn't already (flushing EP state) */ |
| 2282 | if (musb->g.speed != USB_SPEED_UNKNOWN) |
| 2283 | musb_g_disconnect(musb); |
| 2284 | |
| 2285 | /* clear HR */ |
| 2286 | else if (devctl & MUSB_DEVCTL_HR) |
| 2287 | musb_writeb(mbase, MUSB_DEVCTL, MUSB_DEVCTL_SESSION); |
| 2288 | |
| 2289 | |
| 2290 | /* what speed did we negotiate? */ |
| 2291 | power = musb_readb(mbase, MUSB_POWER); |
| 2292 | musb->g.speed = (power & MUSB_POWER_HSMODE) |
| 2293 | ? USB_SPEED_HIGH : USB_SPEED_FULL; |
| 2294 | |
| 2295 | /* start in USB_STATE_DEFAULT */ |
| 2296 | musb->is_active = 1; |
| 2297 | musb->is_suspended = 0; |
| 2298 | MUSB_DEV_MODE(musb); |
| 2299 | musb->address = 0; |
| 2300 | musb->ep0_state = MUSB_EP0_STAGE_SETUP; |
| 2301 | |
| 2302 | musb->may_wakeup = 0; |
| 2303 | musb->g.b_hnp_enable = 0; |
| 2304 | musb->g.a_alt_hnp_support = 0; |
| 2305 | musb->g.a_hnp_support = 0; |
| 2306 | |
| 2307 | #ifndef __UBOOT__ |
| 2308 | /* Normal reset, as B-Device; |
| 2309 | * or else after HNP, as A-Device |
| 2310 | */ |
| 2311 | if (devctl & MUSB_DEVCTL_BDEVICE) { |
| 2312 | musb->xceiv->state = OTG_STATE_B_PERIPHERAL; |
| 2313 | musb->g.is_a_peripheral = 0; |
| 2314 | } else if (is_otg_enabled(musb)) { |
| 2315 | musb->xceiv->state = OTG_STATE_A_PERIPHERAL; |
| 2316 | musb->g.is_a_peripheral = 1; |
| 2317 | } else |
| 2318 | WARN_ON(1); |
| 2319 | |
| 2320 | /* start with default limits on VBUS power draw */ |
| 2321 | (void) musb_gadget_vbus_draw(&musb->g, |
| 2322 | is_otg_enabled(musb) ? 8 : 100); |
| 2323 | #endif |
| 2324 | } |