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