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