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