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