1550a7375SFelipe Balbi /* 2550a7375SFelipe Balbi * MUSB OTG driver peripheral support 3550a7375SFelipe Balbi * 4550a7375SFelipe Balbi * Copyright 2005 Mentor Graphics Corporation 5550a7375SFelipe Balbi * Copyright (C) 2005-2006 by Texas Instruments 6550a7375SFelipe Balbi * Copyright (C) 2006-2007 Nokia Corporation 7cea83241SSergei Shtylyov * Copyright (C) 2009 MontaVista Software, Inc. <source@mvista.com> 8550a7375SFelipe Balbi * 9550a7375SFelipe Balbi * This program is free software; you can redistribute it and/or 10550a7375SFelipe Balbi * modify it under the terms of the GNU General Public License 11550a7375SFelipe Balbi * version 2 as published by the Free Software Foundation. 12550a7375SFelipe Balbi * 13550a7375SFelipe Balbi * This program is distributed in the hope that it will be useful, but 14550a7375SFelipe Balbi * WITHOUT ANY WARRANTY; without even the implied warranty of 15550a7375SFelipe Balbi * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16550a7375SFelipe Balbi * General Public License for more details. 17550a7375SFelipe Balbi * 18550a7375SFelipe Balbi * You should have received a copy of the GNU General Public License 19550a7375SFelipe Balbi * along with this program; if not, write to the Free Software 20550a7375SFelipe Balbi * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 21550a7375SFelipe Balbi * 02110-1301 USA 22550a7375SFelipe Balbi * 23550a7375SFelipe Balbi * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED 24550a7375SFelipe Balbi * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 25550a7375SFelipe Balbi * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 26550a7375SFelipe Balbi * NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, 27550a7375SFelipe Balbi * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 28550a7375SFelipe Balbi * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 29550a7375SFelipe Balbi * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 30550a7375SFelipe Balbi * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31550a7375SFelipe Balbi * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 32550a7375SFelipe Balbi * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33550a7375SFelipe Balbi * 34550a7375SFelipe Balbi */ 35550a7375SFelipe Balbi 36550a7375SFelipe Balbi #include <linux/kernel.h> 37550a7375SFelipe Balbi #include <linux/list.h> 38550a7375SFelipe Balbi #include <linux/timer.h> 39550a7375SFelipe Balbi #include <linux/module.h> 40550a7375SFelipe Balbi #include <linux/smp.h> 41550a7375SFelipe Balbi #include <linux/spinlock.h> 42550a7375SFelipe Balbi #include <linux/delay.h> 43550a7375SFelipe Balbi #include <linux/dma-mapping.h> 445a0e3ad6STejun Heo #include <linux/slab.h> 45550a7375SFelipe Balbi 46550a7375SFelipe Balbi #include "musb_core.h" 47fc78003eSBin Liu #include "musb_trace.h" 48550a7375SFelipe Balbi 49550a7375SFelipe Balbi 50550a7375SFelipe Balbi /* ----------------------------------------------------------------------- */ 51550a7375SFelipe Balbi 52c65bfa62SMian Yousaf Kaukab #define is_buffer_mapped(req) (is_dma_capable() && \ 53c65bfa62SMian Yousaf Kaukab (req->map_state != UN_MAPPED)) 54c65bfa62SMian Yousaf Kaukab 5592d2711fSHema Kalliguddi /* Maps the buffer to dma */ 5692d2711fSHema Kalliguddi 5792d2711fSHema Kalliguddi static inline void map_dma_buffer(struct musb_request *request, 58c65bfa62SMian Yousaf Kaukab struct musb *musb, struct musb_ep *musb_ep) 5992d2711fSHema Kalliguddi { 605f5761cbSMian Yousaf Kaukab int compatible = true; 615f5761cbSMian Yousaf Kaukab struct dma_controller *dma = musb->dma_controller; 625f5761cbSMian Yousaf Kaukab 63c65bfa62SMian Yousaf Kaukab request->map_state = UN_MAPPED; 64c65bfa62SMian Yousaf Kaukab 65c65bfa62SMian Yousaf Kaukab if (!is_dma_capable() || !musb_ep->dma) 66c65bfa62SMian Yousaf Kaukab return; 67c65bfa62SMian Yousaf Kaukab 685f5761cbSMian Yousaf Kaukab /* Check if DMA engine can handle this request. 695f5761cbSMian Yousaf Kaukab * DMA code must reject the USB request explicitly. 705f5761cbSMian Yousaf Kaukab * Default behaviour is to map the request. 715f5761cbSMian Yousaf Kaukab */ 725f5761cbSMian Yousaf Kaukab if (dma->is_compatible) 735f5761cbSMian Yousaf Kaukab compatible = dma->is_compatible(musb_ep->dma, 745f5761cbSMian Yousaf Kaukab musb_ep->packet_sz, request->request.buf, 755f5761cbSMian Yousaf Kaukab request->request.length); 765f5761cbSMian Yousaf Kaukab if (!compatible) 775f5761cbSMian Yousaf Kaukab return; 785f5761cbSMian Yousaf Kaukab 7992d2711fSHema Kalliguddi if (request->request.dma == DMA_ADDR_INVALID) { 807b360f42SSebastian Andrzej Siewior dma_addr_t dma_addr; 817b360f42SSebastian Andrzej Siewior int ret; 827b360f42SSebastian Andrzej Siewior 837b360f42SSebastian Andrzej Siewior dma_addr = dma_map_single( 8492d2711fSHema Kalliguddi musb->controller, 8592d2711fSHema Kalliguddi request->request.buf, 8692d2711fSHema Kalliguddi request->request.length, 8792d2711fSHema Kalliguddi request->tx 8892d2711fSHema Kalliguddi ? DMA_TO_DEVICE 8992d2711fSHema Kalliguddi : DMA_FROM_DEVICE); 907b360f42SSebastian Andrzej Siewior ret = dma_mapping_error(musb->controller, dma_addr); 917b360f42SSebastian Andrzej Siewior if (ret) 927b360f42SSebastian Andrzej Siewior return; 937b360f42SSebastian Andrzej Siewior 947b360f42SSebastian Andrzej Siewior request->request.dma = dma_addr; 95c65bfa62SMian Yousaf Kaukab request->map_state = MUSB_MAPPED; 9692d2711fSHema Kalliguddi } else { 9792d2711fSHema Kalliguddi dma_sync_single_for_device(musb->controller, 9892d2711fSHema Kalliguddi request->request.dma, 9992d2711fSHema Kalliguddi request->request.length, 10092d2711fSHema Kalliguddi request->tx 10192d2711fSHema Kalliguddi ? DMA_TO_DEVICE 10292d2711fSHema Kalliguddi : DMA_FROM_DEVICE); 103c65bfa62SMian Yousaf Kaukab request->map_state = PRE_MAPPED; 10492d2711fSHema Kalliguddi } 10592d2711fSHema Kalliguddi } 10692d2711fSHema Kalliguddi 10792d2711fSHema Kalliguddi /* Unmap the buffer from dma and maps it back to cpu */ 10892d2711fSHema Kalliguddi static inline void unmap_dma_buffer(struct musb_request *request, 10992d2711fSHema Kalliguddi struct musb *musb) 11092d2711fSHema Kalliguddi { 11106d9db72SKishon Vijay Abraham I struct musb_ep *musb_ep = request->ep; 11206d9db72SKishon Vijay Abraham I 11306d9db72SKishon Vijay Abraham I if (!is_buffer_mapped(request) || !musb_ep->dma) 114c65bfa62SMian Yousaf Kaukab return; 115c65bfa62SMian Yousaf Kaukab 11692d2711fSHema Kalliguddi if (request->request.dma == DMA_ADDR_INVALID) { 1175c8a86e1SFelipe Balbi dev_vdbg(musb->controller, 1185c8a86e1SFelipe Balbi "not unmapping a never mapped buffer\n"); 11992d2711fSHema Kalliguddi return; 12092d2711fSHema Kalliguddi } 121c65bfa62SMian Yousaf Kaukab if (request->map_state == MUSB_MAPPED) { 12292d2711fSHema Kalliguddi dma_unmap_single(musb->controller, 12392d2711fSHema Kalliguddi request->request.dma, 12492d2711fSHema Kalliguddi request->request.length, 12592d2711fSHema Kalliguddi request->tx 12692d2711fSHema Kalliguddi ? DMA_TO_DEVICE 12792d2711fSHema Kalliguddi : DMA_FROM_DEVICE); 12892d2711fSHema Kalliguddi request->request.dma = DMA_ADDR_INVALID; 129c65bfa62SMian Yousaf Kaukab } else { /* PRE_MAPPED */ 13092d2711fSHema Kalliguddi dma_sync_single_for_cpu(musb->controller, 13192d2711fSHema Kalliguddi request->request.dma, 13292d2711fSHema Kalliguddi request->request.length, 13392d2711fSHema Kalliguddi request->tx 13492d2711fSHema Kalliguddi ? DMA_TO_DEVICE 13592d2711fSHema Kalliguddi : DMA_FROM_DEVICE); 13692d2711fSHema Kalliguddi } 137c65bfa62SMian Yousaf Kaukab request->map_state = UN_MAPPED; 13892d2711fSHema Kalliguddi } 13992d2711fSHema Kalliguddi 140550a7375SFelipe Balbi /* 141550a7375SFelipe Balbi * Immediately complete a request. 142550a7375SFelipe Balbi * 143550a7375SFelipe Balbi * @param request the request to complete 144550a7375SFelipe Balbi * @param status the status to complete the request with 145550a7375SFelipe Balbi * Context: controller locked, IRQs blocked. 146550a7375SFelipe Balbi */ 147550a7375SFelipe Balbi void musb_g_giveback( 148550a7375SFelipe Balbi struct musb_ep *ep, 149550a7375SFelipe Balbi struct usb_request *request, 150550a7375SFelipe Balbi int status) 151550a7375SFelipe Balbi __releases(ep->musb->lock) 152550a7375SFelipe Balbi __acquires(ep->musb->lock) 153550a7375SFelipe Balbi { 154550a7375SFelipe Balbi struct musb_request *req; 155550a7375SFelipe Balbi struct musb *musb; 156550a7375SFelipe Balbi int busy = ep->busy; 157550a7375SFelipe Balbi 158550a7375SFelipe Balbi req = to_musb_request(request); 159550a7375SFelipe Balbi 160ad1adb89SFelipe Balbi list_del(&req->list); 161550a7375SFelipe Balbi if (req->request.status == -EINPROGRESS) 162550a7375SFelipe Balbi req->request.status = status; 163550a7375SFelipe Balbi musb = req->musb; 164550a7375SFelipe Balbi 165550a7375SFelipe Balbi ep->busy = 1; 166550a7375SFelipe Balbi spin_unlock(&musb->lock); 16706d9db72SKishon Vijay Abraham I 16806d9db72SKishon Vijay Abraham I if (!dma_mapping_error(&musb->g.dev, request->dma)) 16992d2711fSHema Kalliguddi unmap_dma_buffer(req, musb); 17006d9db72SKishon Vijay Abraham I 171fc78003eSBin Liu trace_musb_req_gb(req); 172304f7e5eSMichal Sojka usb_gadget_giveback_request(&req->ep->end_point, &req->request); 173550a7375SFelipe Balbi spin_lock(&musb->lock); 174550a7375SFelipe Balbi ep->busy = busy; 175550a7375SFelipe Balbi } 176550a7375SFelipe Balbi 177550a7375SFelipe Balbi /* ----------------------------------------------------------------------- */ 178550a7375SFelipe Balbi 179550a7375SFelipe Balbi /* 180550a7375SFelipe Balbi * Abort requests queued to an endpoint using the status. Synchronous. 181550a7375SFelipe Balbi * caller locked controller and blocked irqs, and selected this ep. 182550a7375SFelipe Balbi */ 183550a7375SFelipe Balbi static void nuke(struct musb_ep *ep, const int status) 184550a7375SFelipe Balbi { 1855c8a86e1SFelipe Balbi struct musb *musb = ep->musb; 186550a7375SFelipe Balbi struct musb_request *req = NULL; 187550a7375SFelipe Balbi void __iomem *epio = ep->musb->endpoints[ep->current_epnum].regs; 188550a7375SFelipe Balbi 189550a7375SFelipe Balbi ep->busy = 1; 190550a7375SFelipe Balbi 191550a7375SFelipe Balbi if (is_dma_capable() && ep->dma) { 192550a7375SFelipe Balbi struct dma_controller *c = ep->musb->dma_controller; 193550a7375SFelipe Balbi int value; 194b6e434a5SSergei Shtylyov 195550a7375SFelipe Balbi if (ep->is_in) { 196b6e434a5SSergei Shtylyov /* 197b6e434a5SSergei Shtylyov * The programming guide says that we must not clear 198b6e434a5SSergei Shtylyov * the DMAMODE bit before DMAENAB, so we only 199b6e434a5SSergei Shtylyov * clear it in the second write... 200b6e434a5SSergei Shtylyov */ 201550a7375SFelipe Balbi musb_writew(epio, MUSB_TXCSR, 202b6e434a5SSergei Shtylyov MUSB_TXCSR_DMAMODE | MUSB_TXCSR_FLUSHFIFO); 203550a7375SFelipe Balbi musb_writew(epio, MUSB_TXCSR, 204550a7375SFelipe Balbi 0 | MUSB_TXCSR_FLUSHFIFO); 205550a7375SFelipe Balbi } else { 206550a7375SFelipe Balbi musb_writew(epio, MUSB_RXCSR, 207550a7375SFelipe Balbi 0 | MUSB_RXCSR_FLUSHFIFO); 208550a7375SFelipe Balbi musb_writew(epio, MUSB_RXCSR, 209550a7375SFelipe Balbi 0 | MUSB_RXCSR_FLUSHFIFO); 210550a7375SFelipe Balbi } 211550a7375SFelipe Balbi 212550a7375SFelipe Balbi value = c->channel_abort(ep->dma); 213b99d3659SBin Liu musb_dbg(musb, "%s: abort DMA --> %d", ep->name, value); 214550a7375SFelipe Balbi c->channel_release(ep->dma); 215550a7375SFelipe Balbi ep->dma = NULL; 216550a7375SFelipe Balbi } 217550a7375SFelipe Balbi 218ad1adb89SFelipe Balbi while (!list_empty(&ep->req_list)) { 219ad1adb89SFelipe Balbi req = list_first_entry(&ep->req_list, struct musb_request, list); 220550a7375SFelipe Balbi musb_g_giveback(ep, &req->request, status); 221550a7375SFelipe Balbi } 222550a7375SFelipe Balbi } 223550a7375SFelipe Balbi 224550a7375SFelipe Balbi /* ----------------------------------------------------------------------- */ 225550a7375SFelipe Balbi 226550a7375SFelipe Balbi /* Data transfers - pure PIO, pure DMA, or mixed mode */ 227550a7375SFelipe Balbi 228550a7375SFelipe Balbi /* 229550a7375SFelipe Balbi * This assumes the separate CPPI engine is responding to DMA requests 230550a7375SFelipe Balbi * from the usb core ... sequenced a bit differently from mentor dma. 231550a7375SFelipe Balbi */ 232550a7375SFelipe Balbi 233550a7375SFelipe Balbi static inline int max_ep_writesize(struct musb *musb, struct musb_ep *ep) 234550a7375SFelipe Balbi { 235550a7375SFelipe Balbi if (can_bulk_split(musb, ep->type)) 236550a7375SFelipe Balbi return ep->hw_ep->max_packet_sz_tx; 237550a7375SFelipe Balbi else 238550a7375SFelipe Balbi return ep->packet_sz; 239550a7375SFelipe Balbi } 240550a7375SFelipe Balbi 241550a7375SFelipe Balbi /* 242550a7375SFelipe Balbi * An endpoint is transmitting data. This can be called either from 243550a7375SFelipe Balbi * the IRQ routine or from ep.queue() to kickstart a request on an 244550a7375SFelipe Balbi * endpoint. 245550a7375SFelipe Balbi * 246550a7375SFelipe Balbi * Context: controller locked, IRQs blocked, endpoint selected 247550a7375SFelipe Balbi */ 248550a7375SFelipe Balbi static void txstate(struct musb *musb, struct musb_request *req) 249550a7375SFelipe Balbi { 250550a7375SFelipe Balbi u8 epnum = req->epnum; 251550a7375SFelipe Balbi struct musb_ep *musb_ep; 252550a7375SFelipe Balbi void __iomem *epio = musb->endpoints[epnum].regs; 253550a7375SFelipe Balbi struct usb_request *request; 254550a7375SFelipe Balbi u16 fifo_count = 0, csr; 255550a7375SFelipe Balbi int use_dma = 0; 256550a7375SFelipe Balbi 257550a7375SFelipe Balbi musb_ep = req->ep; 258550a7375SFelipe Balbi 259abf710e6SVikram Pandita /* Check if EP is disabled */ 260abf710e6SVikram Pandita if (!musb_ep->desc) { 261b99d3659SBin Liu musb_dbg(musb, "ep:%s disabled - ignore request", 262abf710e6SVikram Pandita musb_ep->end_point.name); 263abf710e6SVikram Pandita return; 264abf710e6SVikram Pandita } 265abf710e6SVikram Pandita 266550a7375SFelipe Balbi /* we shouldn't get here while DMA is active ... but we do ... */ 267550a7375SFelipe Balbi if (dma_channel_status(musb_ep->dma) == MUSB_DMA_STATUS_BUSY) { 268b99d3659SBin Liu musb_dbg(musb, "dma pending..."); 269550a7375SFelipe Balbi return; 270550a7375SFelipe Balbi } 271550a7375SFelipe Balbi 272550a7375SFelipe Balbi /* read TXCSR before */ 273550a7375SFelipe Balbi csr = musb_readw(epio, MUSB_TXCSR); 274550a7375SFelipe Balbi 275550a7375SFelipe Balbi request = &req->request; 276550a7375SFelipe Balbi fifo_count = min(max_ep_writesize(musb, musb_ep), 277550a7375SFelipe Balbi (int)(request->length - request->actual)); 278550a7375SFelipe Balbi 279550a7375SFelipe Balbi if (csr & MUSB_TXCSR_TXPKTRDY) { 280b99d3659SBin Liu musb_dbg(musb, "%s old packet still ready , txcsr %03x", 281550a7375SFelipe Balbi musb_ep->end_point.name, csr); 282550a7375SFelipe Balbi return; 283550a7375SFelipe Balbi } 284550a7375SFelipe Balbi 285550a7375SFelipe Balbi if (csr & MUSB_TXCSR_P_SENDSTALL) { 286b99d3659SBin Liu musb_dbg(musb, "%s stalling, txcsr %03x", 287550a7375SFelipe Balbi musb_ep->end_point.name, csr); 288550a7375SFelipe Balbi return; 289550a7375SFelipe Balbi } 290550a7375SFelipe Balbi 291b99d3659SBin Liu musb_dbg(musb, "hw_ep%d, maxpacket %d, fifo count %d, txcsr %03x", 292550a7375SFelipe Balbi epnum, musb_ep->packet_sz, fifo_count, 293550a7375SFelipe Balbi csr); 294550a7375SFelipe Balbi 295550a7375SFelipe Balbi #ifndef CONFIG_MUSB_PIO_ONLY 296c65bfa62SMian Yousaf Kaukab if (is_buffer_mapped(req)) { 297550a7375SFelipe Balbi struct dma_controller *c = musb->dma_controller; 29866af83ddSMing Lei size_t request_size; 29966af83ddSMing Lei 30066af83ddSMing Lei /* setup DMA, then program endpoint CSR */ 30166af83ddSMing Lei request_size = min_t(size_t, request->length - request->actual, 30266af83ddSMing Lei musb_ep->dma->max_len); 303550a7375SFelipe Balbi 304d17d535fSAjay Kumar Gupta use_dma = (request->dma != DMA_ADDR_INVALID && request_size); 305550a7375SFelipe Balbi 306550a7375SFelipe Balbi /* MUSB_TXCSR_P_ISO is still set correctly */ 307550a7375SFelipe Balbi 30803840fadSFelipe Balbi if (musb_dma_inventra(musb) || musb_dma_ux500(musb)) { 309d1043a26SAnand Gadiyar if (request_size < musb_ep->packet_sz) 310550a7375SFelipe Balbi musb_ep->dma->desired_mode = 0; 311550a7375SFelipe Balbi else 312550a7375SFelipe Balbi musb_ep->dma->desired_mode = 1; 313550a7375SFelipe Balbi 314550a7375SFelipe Balbi use_dma = use_dma && c->channel_program( 315550a7375SFelipe Balbi musb_ep->dma, musb_ep->packet_sz, 316550a7375SFelipe Balbi musb_ep->dma->desired_mode, 317796a83faSCliff Cai request->dma + request->actual, request_size); 318550a7375SFelipe Balbi if (use_dma) { 319550a7375SFelipe Balbi if (musb_ep->dma->desired_mode == 0) { 320b6e434a5SSergei Shtylyov /* 321b6e434a5SSergei Shtylyov * We must not clear the DMAMODE bit 322b6e434a5SSergei Shtylyov * before the DMAENAB bit -- and the 323b6e434a5SSergei Shtylyov * latter doesn't always get cleared 324b6e434a5SSergei Shtylyov * before we get here... 325b6e434a5SSergei Shtylyov */ 326b6e434a5SSergei Shtylyov csr &= ~(MUSB_TXCSR_AUTOSET 327b6e434a5SSergei Shtylyov | MUSB_TXCSR_DMAENAB); 328b6e434a5SSergei Shtylyov musb_writew(epio, MUSB_TXCSR, csr 329b6e434a5SSergei Shtylyov | MUSB_TXCSR_P_WZC_BITS); 330b6e434a5SSergei Shtylyov csr &= ~MUSB_TXCSR_DMAMODE; 331550a7375SFelipe Balbi csr |= (MUSB_TXCSR_DMAENAB | 332550a7375SFelipe Balbi MUSB_TXCSR_MODE); 333550a7375SFelipe Balbi /* against programming guide */ 334f11d893dSMing Lei } else { 335f11d893dSMing Lei csr |= (MUSB_TXCSR_DMAENAB 336550a7375SFelipe Balbi | MUSB_TXCSR_DMAMODE 337550a7375SFelipe Balbi | MUSB_TXCSR_MODE); 338bb3a2ef2Ssupriya karanth /* 339bb3a2ef2Ssupriya karanth * Enable Autoset according to table 340bb3a2ef2Ssupriya karanth * below 341bb3a2ef2Ssupriya karanth * bulk_split hb_mult Autoset_Enable 342bb3a2ef2Ssupriya karanth * 0 0 Yes(Normal) 343bb3a2ef2Ssupriya karanth * 0 >0 No(High BW ISO) 344bb3a2ef2Ssupriya karanth * 1 0 Yes(HS bulk) 345bb3a2ef2Ssupriya karanth * 1 >0 Yes(FS bulk) 346bb3a2ef2Ssupriya karanth */ 347bb3a2ef2Ssupriya karanth if (!musb_ep->hb_mult || 348bb3a2ef2Ssupriya karanth can_bulk_split(musb, 3491a171626SGeyslan G. Bem musb_ep->type)) 350f11d893dSMing Lei csr |= MUSB_TXCSR_AUTOSET; 351f11d893dSMing Lei } 352550a7375SFelipe Balbi csr &= ~MUSB_TXCSR_P_UNDERRUN; 353f11d893dSMing Lei 354550a7375SFelipe Balbi musb_writew(epio, MUSB_TXCSR, csr); 355550a7375SFelipe Balbi } 356550a7375SFelipe Balbi } 357550a7375SFelipe Balbi 358f8e9f34fSTony Lindgren if (is_cppi_enabled(musb)) { 359550a7375SFelipe Balbi /* program endpoint CSR first, then setup DMA */ 360b6e434a5SSergei Shtylyov csr &= ~(MUSB_TXCSR_P_UNDERRUN | MUSB_TXCSR_TXPKTRDY); 36137e3ee99SSergei Shtylyov csr |= MUSB_TXCSR_DMAENAB | MUSB_TXCSR_DMAMODE | 36237e3ee99SSergei Shtylyov MUSB_TXCSR_MODE; 363fc525751SSebastian Andrzej Siewior musb_writew(epio, MUSB_TXCSR, (MUSB_TXCSR_P_WZC_BITS & 364fc525751SSebastian Andrzej Siewior ~MUSB_TXCSR_P_UNDERRUN) | csr); 365550a7375SFelipe Balbi 366550a7375SFelipe Balbi /* ensure writebuffer is empty */ 367550a7375SFelipe Balbi csr = musb_readw(epio, MUSB_TXCSR); 368550a7375SFelipe Balbi 369fc525751SSebastian Andrzej Siewior /* 370fc525751SSebastian Andrzej Siewior * NOTE host side sets DMAENAB later than this; both are 371fc525751SSebastian Andrzej Siewior * OK since the transfer dma glue (between CPPI and 372fc525751SSebastian Andrzej Siewior * Mentor fifos) just tells CPPI it could start. Data 373fc525751SSebastian Andrzej Siewior * only moves to the USB TX fifo when both fifos are 374fc525751SSebastian Andrzej Siewior * ready. 375550a7375SFelipe Balbi */ 376fc525751SSebastian Andrzej Siewior /* 377fc525751SSebastian Andrzej Siewior * "mode" is irrelevant here; handle terminating ZLPs 378fc525751SSebastian Andrzej Siewior * like PIO does, since the hardware RNDIS mode seems 379fc525751SSebastian Andrzej Siewior * unreliable except for the 380fc525751SSebastian Andrzej Siewior * last-packet-is-already-short case. 381550a7375SFelipe Balbi */ 382550a7375SFelipe Balbi use_dma = use_dma && c->channel_program( 383550a7375SFelipe Balbi musb_ep->dma, musb_ep->packet_sz, 384550a7375SFelipe Balbi 0, 38566af83ddSMing Lei request->dma + request->actual, 38666af83ddSMing Lei request_size); 387550a7375SFelipe Balbi if (!use_dma) { 388550a7375SFelipe Balbi c->channel_release(musb_ep->dma); 389550a7375SFelipe Balbi musb_ep->dma = NULL; 390b6e434a5SSergei Shtylyov csr &= ~MUSB_TXCSR_DMAENAB; 391b6e434a5SSergei Shtylyov musb_writew(epio, MUSB_TXCSR, csr); 392550a7375SFelipe Balbi /* invariant: prequest->buf is non-null */ 393550a7375SFelipe Balbi } 394f8e9f34fSTony Lindgren } else if (tusb_dma_omap(musb)) 395550a7375SFelipe Balbi use_dma = use_dma && c->channel_program( 396550a7375SFelipe Balbi musb_ep->dma, musb_ep->packet_sz, 397550a7375SFelipe Balbi request->zero, 39866af83ddSMing Lei request->dma + request->actual, 39966af83ddSMing Lei request_size); 400550a7375SFelipe Balbi } 401550a7375SFelipe Balbi #endif 402550a7375SFelipe Balbi 403550a7375SFelipe Balbi if (!use_dma) { 40492d2711fSHema Kalliguddi /* 40592d2711fSHema Kalliguddi * Unmap the dma buffer back to cpu if dma channel 40692d2711fSHema Kalliguddi * programming fails 40792d2711fSHema Kalliguddi */ 40892d2711fSHema Kalliguddi unmap_dma_buffer(req, musb); 40992d2711fSHema Kalliguddi 410550a7375SFelipe Balbi musb_write_fifo(musb_ep->hw_ep, fifo_count, 411550a7375SFelipe Balbi (u8 *) (request->buf + request->actual)); 412550a7375SFelipe Balbi request->actual += fifo_count; 413550a7375SFelipe Balbi csr |= MUSB_TXCSR_TXPKTRDY; 414550a7375SFelipe Balbi csr &= ~MUSB_TXCSR_P_UNDERRUN; 415550a7375SFelipe Balbi musb_writew(epio, MUSB_TXCSR, csr); 416550a7375SFelipe Balbi } 417550a7375SFelipe Balbi 418550a7375SFelipe Balbi /* host may already have the data when this message shows... */ 419b99d3659SBin Liu musb_dbg(musb, "%s TX/IN %s len %d/%d, txcsr %04x, fifo %d/%d", 420550a7375SFelipe Balbi musb_ep->end_point.name, use_dma ? "dma" : "pio", 421550a7375SFelipe Balbi request->actual, request->length, 422550a7375SFelipe Balbi musb_readw(epio, MUSB_TXCSR), 423550a7375SFelipe Balbi fifo_count, 424550a7375SFelipe Balbi musb_readw(epio, MUSB_TXMAXP)); 425550a7375SFelipe Balbi } 426550a7375SFelipe Balbi 427550a7375SFelipe Balbi /* 428550a7375SFelipe Balbi * FIFO state update (e.g. data ready). 429550a7375SFelipe Balbi * Called from IRQ, with controller locked. 430550a7375SFelipe Balbi */ 431550a7375SFelipe Balbi void musb_g_tx(struct musb *musb, u8 epnum) 432550a7375SFelipe Balbi { 433550a7375SFelipe Balbi u16 csr; 434ad1adb89SFelipe Balbi struct musb_request *req; 435550a7375SFelipe Balbi struct usb_request *request; 436550a7375SFelipe Balbi u8 __iomem *mbase = musb->mregs; 437550a7375SFelipe Balbi struct musb_ep *musb_ep = &musb->endpoints[epnum].ep_in; 438550a7375SFelipe Balbi void __iomem *epio = musb->endpoints[epnum].regs; 439550a7375SFelipe Balbi struct dma_channel *dma; 440550a7375SFelipe Balbi 441550a7375SFelipe Balbi musb_ep_select(mbase, epnum); 442ad1adb89SFelipe Balbi req = next_request(musb_ep); 443ad1adb89SFelipe Balbi request = &req->request; 444550a7375SFelipe Balbi 445fc78003eSBin Liu trace_musb_req_tx(req); 446550a7375SFelipe Balbi csr = musb_readw(epio, MUSB_TXCSR); 447b99d3659SBin Liu musb_dbg(musb, "<== %s, txcsr %04x", musb_ep->end_point.name, csr); 448550a7375SFelipe Balbi 449550a7375SFelipe Balbi dma = is_dma_capable() ? musb_ep->dma : NULL; 4507723de7eSSergei Shtylyov 4517723de7eSSergei Shtylyov /* 4527723de7eSSergei Shtylyov * REVISIT: for high bandwidth, MUSB_TXCSR_P_INCOMPTX 4537723de7eSSergei Shtylyov * probably rates reporting as a host error. 454550a7375SFelipe Balbi */ 455550a7375SFelipe Balbi if (csr & MUSB_TXCSR_P_SENTSTALL) { 456550a7375SFelipe Balbi csr |= MUSB_TXCSR_P_WZC_BITS; 457550a7375SFelipe Balbi csr &= ~MUSB_TXCSR_P_SENTSTALL; 458550a7375SFelipe Balbi musb_writew(epio, MUSB_TXCSR, csr); 4597723de7eSSergei Shtylyov return; 460550a7375SFelipe Balbi } 461550a7375SFelipe Balbi 462550a7375SFelipe Balbi if (csr & MUSB_TXCSR_P_UNDERRUN) { 4637723de7eSSergei Shtylyov /* We NAKed, no big deal... little reason to care. */ 464550a7375SFelipe Balbi csr |= MUSB_TXCSR_P_WZC_BITS; 4657723de7eSSergei Shtylyov csr &= ~(MUSB_TXCSR_P_UNDERRUN | MUSB_TXCSR_TXPKTRDY); 466550a7375SFelipe Balbi musb_writew(epio, MUSB_TXCSR, csr); 4675c8a86e1SFelipe Balbi dev_vdbg(musb->controller, "underrun on ep%d, req %p\n", 4685c8a86e1SFelipe Balbi epnum, request); 469550a7375SFelipe Balbi } 470550a7375SFelipe Balbi 471550a7375SFelipe Balbi if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) { 4727723de7eSSergei Shtylyov /* 4737723de7eSSergei Shtylyov * SHOULD NOT HAPPEN... has with CPPI though, after 474550a7375SFelipe Balbi * changing SENDSTALL (and other cases); harmless? 475550a7375SFelipe Balbi */ 476b99d3659SBin Liu musb_dbg(musb, "%s dma still busy?", musb_ep->end_point.name); 4777723de7eSSergei Shtylyov return; 478550a7375SFelipe Balbi } 479550a7375SFelipe Balbi 480550a7375SFelipe Balbi if (request) { 481550a7375SFelipe Balbi u8 is_dma = 0; 482fb91cddcSTony Lindgren bool short_packet = false; 483550a7375SFelipe Balbi 484550a7375SFelipe Balbi if (dma && (csr & MUSB_TXCSR_DMAENAB)) { 485550a7375SFelipe Balbi is_dma = 1; 486550a7375SFelipe Balbi csr |= MUSB_TXCSR_P_WZC_BITS; 4877723de7eSSergei Shtylyov csr &= ~(MUSB_TXCSR_DMAENAB | MUSB_TXCSR_P_UNDERRUN | 488100d4a9dSMian Yousaf Kaukab MUSB_TXCSR_TXPKTRDY | MUSB_TXCSR_AUTOSET); 489550a7375SFelipe Balbi musb_writew(epio, MUSB_TXCSR, csr); 4907723de7eSSergei Shtylyov /* Ensure writebuffer is empty. */ 491550a7375SFelipe Balbi csr = musb_readw(epio, MUSB_TXCSR); 492550a7375SFelipe Balbi request->actual += musb_ep->dma->actual_len; 493b99d3659SBin Liu musb_dbg(musb, "TXCSR%d %04x, DMA off, len %zu, req %p", 4947723de7eSSergei Shtylyov epnum, csr, musb_ep->dma->actual_len, request); 495550a7375SFelipe Balbi } 496550a7375SFelipe Balbi 4977723de7eSSergei Shtylyov /* 4987723de7eSSergei Shtylyov * First, maybe a terminating short packet. Some DMA 4997723de7eSSergei Shtylyov * engines might handle this by themselves. 500550a7375SFelipe Balbi */ 501fb91cddcSTony Lindgren if ((request->zero && request->length) 502e7379aaaSMing Lei && (request->length % musb_ep->packet_sz == 0) 503e7379aaaSMing Lei && (request->actual == request->length)) 504fb91cddcSTony Lindgren short_packet = true; 505fb91cddcSTony Lindgren 506fb91cddcSTony Lindgren if ((musb_dma_inventra(musb) || musb_dma_ux500(musb)) && 507fb91cddcSTony Lindgren (is_dma && (!dma->desired_mode || 508550a7375SFelipe Balbi (request->actual & 509fb91cddcSTony Lindgren (musb_ep->packet_sz - 1))))) 510fb91cddcSTony Lindgren short_packet = true; 511fb91cddcSTony Lindgren 512fb91cddcSTony Lindgren if (short_packet) { 5137723de7eSSergei Shtylyov /* 5147723de7eSSergei Shtylyov * On DMA completion, FIFO may not be 5157723de7eSSergei Shtylyov * available yet... 516550a7375SFelipe Balbi */ 517550a7375SFelipe Balbi if (csr & MUSB_TXCSR_TXPKTRDY) 5187723de7eSSergei Shtylyov return; 519550a7375SFelipe Balbi 5207723de7eSSergei Shtylyov musb_writew(epio, MUSB_TXCSR, MUSB_TXCSR_MODE 521550a7375SFelipe Balbi | MUSB_TXCSR_TXPKTRDY); 522550a7375SFelipe Balbi request->zero = 0; 523550a7375SFelipe Balbi } 524550a7375SFelipe Balbi 525bb27bc2cSMing Lei if (request->actual == request->length) { 526550a7375SFelipe Balbi musb_g_giveback(musb_ep, request, 0); 52739287076SSupriya Karanth /* 52839287076SSupriya Karanth * In the giveback function the MUSB lock is 52939287076SSupriya Karanth * released and acquired after sometime. During 53039287076SSupriya Karanth * this time period the INDEX register could get 53139287076SSupriya Karanth * changed by the gadget_queue function especially 53239287076SSupriya Karanth * on SMP systems. Reselect the INDEX to be sure 53339287076SSupriya Karanth * we are reading/modifying the right registers 53439287076SSupriya Karanth */ 53539287076SSupriya Karanth musb_ep_select(mbase, epnum); 536ad1adb89SFelipe Balbi req = musb_ep->desc ? next_request(musb_ep) : NULL; 537ad1adb89SFelipe Balbi if (!req) { 538b99d3659SBin Liu musb_dbg(musb, "%s idle now", 539550a7375SFelipe Balbi musb_ep->end_point.name); 5407723de7eSSergei Shtylyov return; 54195962a77SSergei Shtylyov } 542550a7375SFelipe Balbi } 543550a7375SFelipe Balbi 544ad1adb89SFelipe Balbi txstate(musb, req); 545550a7375SFelipe Balbi } 546550a7375SFelipe Balbi } 547550a7375SFelipe Balbi 548550a7375SFelipe Balbi /* ------------------------------------------------------------ */ 549550a7375SFelipe Balbi 550550a7375SFelipe Balbi /* 551550a7375SFelipe Balbi * Context: controller locked, IRQs blocked, endpoint selected 552550a7375SFelipe Balbi */ 553550a7375SFelipe Balbi static void rxstate(struct musb *musb, struct musb_request *req) 554550a7375SFelipe Balbi { 555550a7375SFelipe Balbi const u8 epnum = req->epnum; 556550a7375SFelipe Balbi struct usb_request *request = &req->request; 557bd2e74d6SMing Lei struct musb_ep *musb_ep; 558550a7375SFelipe Balbi void __iomem *epio = musb->endpoints[epnum].regs; 559f0443afdSSergei Shtylyov unsigned len = 0; 560f0443afdSSergei Shtylyov u16 fifo_count; 561cea83241SSergei Shtylyov u16 csr = musb_readw(epio, MUSB_RXCSR); 562bd2e74d6SMing Lei struct musb_hw_ep *hw_ep = &musb->endpoints[epnum]; 5630ae52d54SAnand Gadiyar u8 use_mode_1; 564bd2e74d6SMing Lei 565bd2e74d6SMing Lei if (hw_ep->is_shared_fifo) 566bd2e74d6SMing Lei musb_ep = &hw_ep->ep_in; 567bd2e74d6SMing Lei else 568bd2e74d6SMing Lei musb_ep = &hw_ep->ep_out; 569bd2e74d6SMing Lei 570f0443afdSSergei Shtylyov fifo_count = musb_ep->packet_sz; 571550a7375SFelipe Balbi 572abf710e6SVikram Pandita /* Check if EP is disabled */ 573abf710e6SVikram Pandita if (!musb_ep->desc) { 574b99d3659SBin Liu musb_dbg(musb, "ep:%s disabled - ignore request", 575abf710e6SVikram Pandita musb_ep->end_point.name); 576abf710e6SVikram Pandita return; 577abf710e6SVikram Pandita } 578abf710e6SVikram Pandita 579cea83241SSergei Shtylyov /* We shouldn't get here while DMA is active, but we do... */ 580cea83241SSergei Shtylyov if (dma_channel_status(musb_ep->dma) == MUSB_DMA_STATUS_BUSY) { 581b99d3659SBin Liu musb_dbg(musb, "DMA pending..."); 582cea83241SSergei Shtylyov return; 583cea83241SSergei Shtylyov } 584cea83241SSergei Shtylyov 585cea83241SSergei Shtylyov if (csr & MUSB_RXCSR_P_SENDSTALL) { 586b99d3659SBin Liu musb_dbg(musb, "%s stalling, RXCSR %04x", 587cea83241SSergei Shtylyov musb_ep->end_point.name, csr); 588cea83241SSergei Shtylyov return; 589cea83241SSergei Shtylyov } 590550a7375SFelipe Balbi 591f8e9f34fSTony Lindgren if (is_cppi_enabled(musb) && is_buffer_mapped(req)) { 592550a7375SFelipe Balbi struct dma_controller *c = musb->dma_controller; 593550a7375SFelipe Balbi struct dma_channel *channel = musb_ep->dma; 594550a7375SFelipe Balbi 595550a7375SFelipe Balbi /* NOTE: CPPI won't actually stop advancing the DMA 596550a7375SFelipe Balbi * queue after short packet transfers, so this is almost 597550a7375SFelipe Balbi * always going to run as IRQ-per-packet DMA so that 598550a7375SFelipe Balbi * faults will be handled correctly. 599550a7375SFelipe Balbi */ 600550a7375SFelipe Balbi if (c->channel_program(channel, 601550a7375SFelipe Balbi musb_ep->packet_sz, 602550a7375SFelipe Balbi !request->short_not_ok, 603550a7375SFelipe Balbi request->dma + request->actual, 604550a7375SFelipe Balbi request->length - request->actual)) { 605550a7375SFelipe Balbi 606550a7375SFelipe Balbi /* make sure that if an rxpkt arrived after the irq, 607550a7375SFelipe Balbi * the cppi engine will be ready to take it as soon 608550a7375SFelipe Balbi * as DMA is enabled 609550a7375SFelipe Balbi */ 610550a7375SFelipe Balbi csr &= ~(MUSB_RXCSR_AUTOCLEAR 611550a7375SFelipe Balbi | MUSB_RXCSR_DMAMODE); 612550a7375SFelipe Balbi csr |= MUSB_RXCSR_DMAENAB | MUSB_RXCSR_P_WZC_BITS; 613550a7375SFelipe Balbi musb_writew(epio, MUSB_RXCSR, csr); 614550a7375SFelipe Balbi return; 615550a7375SFelipe Balbi } 616550a7375SFelipe Balbi } 617550a7375SFelipe Balbi 618550a7375SFelipe Balbi if (csr & MUSB_RXCSR_RXPKTRDY) { 619f0443afdSSergei Shtylyov fifo_count = musb_readw(epio, MUSB_RXCOUNT); 6200ae52d54SAnand Gadiyar 6210ae52d54SAnand Gadiyar /* 62200a89180SFelipe Balbi * Enable Mode 1 on RX transfers only when short_not_ok flag 62300a89180SFelipe Balbi * is set. Currently short_not_ok flag is set only from 62400a89180SFelipe Balbi * file_storage and f_mass_storage drivers 6250ae52d54SAnand Gadiyar */ 62600a89180SFelipe Balbi 62700a89180SFelipe Balbi if (request->short_not_ok && fifo_count == musb_ep->packet_sz) 6280ae52d54SAnand Gadiyar use_mode_1 = 1; 6290ae52d54SAnand Gadiyar else 6300ae52d54SAnand Gadiyar use_mode_1 = 0; 6310ae52d54SAnand Gadiyar 632550a7375SFelipe Balbi if (request->actual < request->length) { 63303840fadSFelipe Balbi if (!is_buffer_mapped(req)) 63403840fadSFelipe Balbi goto buffer_aint_mapped; 63503840fadSFelipe Balbi 63603840fadSFelipe Balbi if (musb_dma_inventra(musb)) { 637550a7375SFelipe Balbi struct dma_controller *c; 638550a7375SFelipe Balbi struct dma_channel *channel; 639550a7375SFelipe Balbi int use_dma = 0; 64037730eccSFelipe Balbi unsigned int transfer_size; 641550a7375SFelipe Balbi 642550a7375SFelipe Balbi c = musb->dma_controller; 643550a7375SFelipe Balbi channel = musb_ep->dma; 644550a7375SFelipe Balbi 64500a89180SFelipe Balbi /* We use DMA Req mode 0 in rx_csr, and DMA controller operates in 64600a89180SFelipe Balbi * mode 0 only. So we do not get endpoint interrupts due to DMA 64700a89180SFelipe Balbi * completion. We only get interrupts from DMA controller. 64800a89180SFelipe Balbi * 64900a89180SFelipe Balbi * We could operate in DMA mode 1 if we knew the size of the tranfer 65000a89180SFelipe Balbi * in advance. For mass storage class, request->length = what the host 65100a89180SFelipe Balbi * sends, so that'd work. But for pretty much everything else, 65200a89180SFelipe Balbi * request->length is routinely more than what the host sends. For 65300a89180SFelipe Balbi * most these gadgets, end of is signified either by a short packet, 65400a89180SFelipe Balbi * or filling the last byte of the buffer. (Sending extra data in 65500a89180SFelipe Balbi * that last pckate should trigger an overflow fault.) But in mode 1, 65600a89180SFelipe Balbi * we don't get DMA completion interrupt for short packets. 65700a89180SFelipe Balbi * 65800a89180SFelipe Balbi * Theoretically, we could enable DMAReq irq (MUSB_RXCSR_DMAMODE = 1), 65900a89180SFelipe Balbi * to get endpoint interrupt on every DMA req, but that didn't seem 66000a89180SFelipe Balbi * to work reliably. 66100a89180SFelipe Balbi * 66200a89180SFelipe Balbi * REVISIT an updated g_file_storage can set req->short_not_ok, which 66300a89180SFelipe Balbi * then becomes usable as a runtime "use mode 1" hint... 66400a89180SFelipe Balbi */ 66500a89180SFelipe Balbi 6660ae52d54SAnand Gadiyar /* Experimental: Mode1 works with mass storage use cases */ 6670ae52d54SAnand Gadiyar if (use_mode_1) { 6689001d80dSMing Lei csr |= MUSB_RXCSR_AUTOCLEAR; 6690ae52d54SAnand Gadiyar musb_writew(epio, MUSB_RXCSR, csr); 6700ae52d54SAnand Gadiyar csr |= MUSB_RXCSR_DMAENAB; 6710ae52d54SAnand Gadiyar musb_writew(epio, MUSB_RXCSR, csr); 672550a7375SFelipe Balbi 6730ae52d54SAnand Gadiyar /* 6740ae52d54SAnand Gadiyar * this special sequence (enabling and then 675550a7375SFelipe Balbi * disabling MUSB_RXCSR_DMAMODE) is required 676550a7375SFelipe Balbi * to get DMAReq to activate 677550a7375SFelipe Balbi */ 678550a7375SFelipe Balbi musb_writew(epio, MUSB_RXCSR, 679550a7375SFelipe Balbi csr | MUSB_RXCSR_DMAMODE); 6800ae52d54SAnand Gadiyar musb_writew(epio, MUSB_RXCSR, csr); 6810ae52d54SAnand Gadiyar 68237730eccSFelipe Balbi transfer_size = min_t(unsigned int, 68337730eccSFelipe Balbi request->length - 68437730eccSFelipe Balbi request->actual, 685660fa886SRoger Quadros channel->max_len); 686660fa886SRoger Quadros musb_ep->dma->desired_mode = 1; 6870ae52d54SAnand Gadiyar } else { 6889001d80dSMing Lei if (!musb_ep->hb_mult && 6899001d80dSMing Lei musb_ep->hw_ep->rx_double_buffered) 6909001d80dSMing Lei csr |= MUSB_RXCSR_AUTOCLEAR; 6910ae52d54SAnand Gadiyar csr |= MUSB_RXCSR_DMAENAB; 692550a7375SFelipe Balbi musb_writew(epio, MUSB_RXCSR, csr); 693550a7375SFelipe Balbi 6941018b4e4SMing Lei transfer_size = min(request->length - request->actual, 695f0443afdSSergei Shtylyov (unsigned)fifo_count); 696550a7375SFelipe Balbi musb_ep->dma->desired_mode = 0; 6970ae52d54SAnand Gadiyar } 698550a7375SFelipe Balbi 699550a7375SFelipe Balbi use_dma = c->channel_program( 700550a7375SFelipe Balbi channel, 701550a7375SFelipe Balbi musb_ep->packet_sz, 702550a7375SFelipe Balbi channel->desired_mode, 703550a7375SFelipe Balbi request->dma 704550a7375SFelipe Balbi + request->actual, 705550a7375SFelipe Balbi transfer_size); 706550a7375SFelipe Balbi 707550a7375SFelipe Balbi if (use_dma) 708550a7375SFelipe Balbi return; 709550a7375SFelipe Balbi } 71003840fadSFelipe Balbi 71103840fadSFelipe Balbi if ((musb_dma_ux500(musb)) && 712a48ff906SMian Yousaf Kaukab (request->actual < request->length)) { 713a48ff906SMian Yousaf Kaukab 714a48ff906SMian Yousaf Kaukab struct dma_controller *c; 715a48ff906SMian Yousaf Kaukab struct dma_channel *channel; 71637730eccSFelipe Balbi unsigned int transfer_size = 0; 717a48ff906SMian Yousaf Kaukab 718a48ff906SMian Yousaf Kaukab c = musb->dma_controller; 719a48ff906SMian Yousaf Kaukab channel = musb_ep->dma; 720a48ff906SMian Yousaf Kaukab 721a48ff906SMian Yousaf Kaukab /* In case first packet is short */ 722f0443afdSSergei Shtylyov if (fifo_count < musb_ep->packet_sz) 723f0443afdSSergei Shtylyov transfer_size = fifo_count; 724a48ff906SMian Yousaf Kaukab else if (request->short_not_ok) 72537730eccSFelipe Balbi transfer_size = min_t(unsigned int, 72637730eccSFelipe Balbi request->length - 727a48ff906SMian Yousaf Kaukab request->actual, 728a48ff906SMian Yousaf Kaukab channel->max_len); 729a48ff906SMian Yousaf Kaukab else 73037730eccSFelipe Balbi transfer_size = min_t(unsigned int, 73137730eccSFelipe Balbi request->length - 732a48ff906SMian Yousaf Kaukab request->actual, 733f0443afdSSergei Shtylyov (unsigned)fifo_count); 734a48ff906SMian Yousaf Kaukab 735a48ff906SMian Yousaf Kaukab csr &= ~MUSB_RXCSR_DMAMODE; 736a48ff906SMian Yousaf Kaukab csr |= (MUSB_RXCSR_DMAENAB | 737a48ff906SMian Yousaf Kaukab MUSB_RXCSR_AUTOCLEAR); 738a48ff906SMian Yousaf Kaukab 739a48ff906SMian Yousaf Kaukab musb_writew(epio, MUSB_RXCSR, csr); 740a48ff906SMian Yousaf Kaukab 741a48ff906SMian Yousaf Kaukab if (transfer_size <= musb_ep->packet_sz) { 742a48ff906SMian Yousaf Kaukab musb_ep->dma->desired_mode = 0; 743a48ff906SMian Yousaf Kaukab } else { 744a48ff906SMian Yousaf Kaukab musb_ep->dma->desired_mode = 1; 745a48ff906SMian Yousaf Kaukab /* Mode must be set after DMAENAB */ 746a48ff906SMian Yousaf Kaukab csr |= MUSB_RXCSR_DMAMODE; 747a48ff906SMian Yousaf Kaukab musb_writew(epio, MUSB_RXCSR, csr); 748a48ff906SMian Yousaf Kaukab } 749a48ff906SMian Yousaf Kaukab 750a48ff906SMian Yousaf Kaukab if (c->channel_program(channel, 751a48ff906SMian Yousaf Kaukab musb_ep->packet_sz, 752a48ff906SMian Yousaf Kaukab channel->desired_mode, 753a48ff906SMian Yousaf Kaukab request->dma 754a48ff906SMian Yousaf Kaukab + request->actual, 755a48ff906SMian Yousaf Kaukab transfer_size)) 756a48ff906SMian Yousaf Kaukab 757a48ff906SMian Yousaf Kaukab return; 758a48ff906SMian Yousaf Kaukab } 759550a7375SFelipe Balbi 760f0443afdSSergei Shtylyov len = request->length - request->actual; 761b99d3659SBin Liu musb_dbg(musb, "%s OUT/RX pio fifo %d/%d, maxpacket %d", 762550a7375SFelipe Balbi musb_ep->end_point.name, 763f0443afdSSergei Shtylyov fifo_count, len, 764550a7375SFelipe Balbi musb_ep->packet_sz); 765550a7375SFelipe Balbi 766c2c96321SFelipe Balbi fifo_count = min_t(unsigned, len, fifo_count); 767550a7375SFelipe Balbi 76803840fadSFelipe Balbi if (tusb_dma_omap(musb)) { 769550a7375SFelipe Balbi struct dma_controller *c = musb->dma_controller; 770550a7375SFelipe Balbi struct dma_channel *channel = musb_ep->dma; 771550a7375SFelipe Balbi u32 dma_addr = request->dma + request->actual; 772550a7375SFelipe Balbi int ret; 773550a7375SFelipe Balbi 774550a7375SFelipe Balbi ret = c->channel_program(channel, 775550a7375SFelipe Balbi musb_ep->packet_sz, 776550a7375SFelipe Balbi channel->desired_mode, 777550a7375SFelipe Balbi dma_addr, 778550a7375SFelipe Balbi fifo_count); 779550a7375SFelipe Balbi if (ret) 780550a7375SFelipe Balbi return; 781550a7375SFelipe Balbi } 78203840fadSFelipe Balbi 78392d2711fSHema Kalliguddi /* 78492d2711fSHema Kalliguddi * Unmap the dma buffer back to cpu if dma channel 78592d2711fSHema Kalliguddi * programming fails. This buffer is mapped if the 78692d2711fSHema Kalliguddi * channel allocation is successful 78792d2711fSHema Kalliguddi */ 78892d2711fSHema Kalliguddi unmap_dma_buffer(req, musb); 78992d2711fSHema Kalliguddi 790e75df371SMing Lei /* 791e75df371SMing Lei * Clear DMAENAB and AUTOCLEAR for the 79292d2711fSHema Kalliguddi * PIO mode transfer 79392d2711fSHema Kalliguddi */ 794e75df371SMing Lei csr &= ~(MUSB_RXCSR_DMAENAB | MUSB_RXCSR_AUTOCLEAR); 79592d2711fSHema Kalliguddi musb_writew(epio, MUSB_RXCSR, csr); 796550a7375SFelipe Balbi 79703840fadSFelipe Balbi buffer_aint_mapped: 798550a7375SFelipe Balbi musb_read_fifo(musb_ep->hw_ep, fifo_count, (u8 *) 799550a7375SFelipe Balbi (request->buf + request->actual)); 800550a7375SFelipe Balbi request->actual += fifo_count; 801550a7375SFelipe Balbi 802550a7375SFelipe Balbi /* REVISIT if we left anything in the fifo, flush 803550a7375SFelipe Balbi * it and report -EOVERFLOW 804550a7375SFelipe Balbi */ 805550a7375SFelipe Balbi 806550a7375SFelipe Balbi /* ack the read! */ 807550a7375SFelipe Balbi csr |= MUSB_RXCSR_P_WZC_BITS; 808550a7375SFelipe Balbi csr &= ~MUSB_RXCSR_RXPKTRDY; 809550a7375SFelipe Balbi musb_writew(epio, MUSB_RXCSR, csr); 810550a7375SFelipe Balbi } 811550a7375SFelipe Balbi } 812550a7375SFelipe Balbi 813550a7375SFelipe Balbi /* reach the end or short packet detected */ 814f0443afdSSergei Shtylyov if (request->actual == request->length || 815f0443afdSSergei Shtylyov fifo_count < musb_ep->packet_sz) 816550a7375SFelipe Balbi musb_g_giveback(musb_ep, request, 0); 817550a7375SFelipe Balbi } 818550a7375SFelipe Balbi 819550a7375SFelipe Balbi /* 820550a7375SFelipe Balbi * Data ready for a request; called from IRQ 821550a7375SFelipe Balbi */ 822550a7375SFelipe Balbi void musb_g_rx(struct musb *musb, u8 epnum) 823550a7375SFelipe Balbi { 824550a7375SFelipe Balbi u16 csr; 825ad1adb89SFelipe Balbi struct musb_request *req; 826550a7375SFelipe Balbi struct usb_request *request; 827550a7375SFelipe Balbi void __iomem *mbase = musb->mregs; 828bd2e74d6SMing Lei struct musb_ep *musb_ep; 829550a7375SFelipe Balbi void __iomem *epio = musb->endpoints[epnum].regs; 830550a7375SFelipe Balbi struct dma_channel *dma; 831bd2e74d6SMing Lei struct musb_hw_ep *hw_ep = &musb->endpoints[epnum]; 832bd2e74d6SMing Lei 833bd2e74d6SMing Lei if (hw_ep->is_shared_fifo) 834bd2e74d6SMing Lei musb_ep = &hw_ep->ep_in; 835bd2e74d6SMing Lei else 836bd2e74d6SMing Lei musb_ep = &hw_ep->ep_out; 837550a7375SFelipe Balbi 838550a7375SFelipe Balbi musb_ep_select(mbase, epnum); 839550a7375SFelipe Balbi 840ad1adb89SFelipe Balbi req = next_request(musb_ep); 841ad1adb89SFelipe Balbi if (!req) 8420abdc36fSMaulik Mankad return; 843550a7375SFelipe Balbi 844fc78003eSBin Liu trace_musb_req_rx(req); 845ad1adb89SFelipe Balbi request = &req->request; 846ad1adb89SFelipe Balbi 847550a7375SFelipe Balbi csr = musb_readw(epio, MUSB_RXCSR); 848550a7375SFelipe Balbi dma = is_dma_capable() ? musb_ep->dma : NULL; 849550a7375SFelipe Balbi 850b99d3659SBin Liu musb_dbg(musb, "<== %s, rxcsr %04x%s %p", musb_ep->end_point.name, 851550a7375SFelipe Balbi csr, dma ? " (dma)" : "", request); 852550a7375SFelipe Balbi 853550a7375SFelipe Balbi if (csr & MUSB_RXCSR_P_SENTSTALL) { 854550a7375SFelipe Balbi csr |= MUSB_RXCSR_P_WZC_BITS; 855550a7375SFelipe Balbi csr &= ~MUSB_RXCSR_P_SENTSTALL; 856550a7375SFelipe Balbi musb_writew(epio, MUSB_RXCSR, csr); 857cea83241SSergei Shtylyov return; 858550a7375SFelipe Balbi } 859550a7375SFelipe Balbi 860550a7375SFelipe Balbi if (csr & MUSB_RXCSR_P_OVERRUN) { 861550a7375SFelipe Balbi /* csr |= MUSB_RXCSR_P_WZC_BITS; */ 862550a7375SFelipe Balbi csr &= ~MUSB_RXCSR_P_OVERRUN; 863550a7375SFelipe Balbi musb_writew(epio, MUSB_RXCSR, csr); 864550a7375SFelipe Balbi 865b99d3659SBin Liu musb_dbg(musb, "%s iso overrun on %p", musb_ep->name, request); 86643467868SSergei Shtylyov if (request->status == -EINPROGRESS) 867550a7375SFelipe Balbi request->status = -EOVERFLOW; 868550a7375SFelipe Balbi } 869550a7375SFelipe Balbi if (csr & MUSB_RXCSR_INCOMPRX) { 870550a7375SFelipe Balbi /* REVISIT not necessarily an error */ 871b99d3659SBin Liu musb_dbg(musb, "%s, incomprx", musb_ep->end_point.name); 872550a7375SFelipe Balbi } 873550a7375SFelipe Balbi 874550a7375SFelipe Balbi if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) { 875550a7375SFelipe Balbi /* "should not happen"; likely RXPKTRDY pending for DMA */ 876b99d3659SBin Liu musb_dbg(musb, "%s busy, csr %04x", 877550a7375SFelipe Balbi musb_ep->end_point.name, csr); 878cea83241SSergei Shtylyov return; 879550a7375SFelipe Balbi } 880550a7375SFelipe Balbi 881550a7375SFelipe Balbi if (dma && (csr & MUSB_RXCSR_DMAENAB)) { 882550a7375SFelipe Balbi csr &= ~(MUSB_RXCSR_AUTOCLEAR 883550a7375SFelipe Balbi | MUSB_RXCSR_DMAENAB 884550a7375SFelipe Balbi | MUSB_RXCSR_DMAMODE); 885550a7375SFelipe Balbi musb_writew(epio, MUSB_RXCSR, 886550a7375SFelipe Balbi MUSB_RXCSR_P_WZC_BITS | csr); 887550a7375SFelipe Balbi 888550a7375SFelipe Balbi request->actual += musb_ep->dma->actual_len; 889550a7375SFelipe Balbi 890a48ff906SMian Yousaf Kaukab #if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_TUSB_OMAP_DMA) || \ 891a48ff906SMian Yousaf Kaukab defined(CONFIG_USB_UX500_DMA) 892550a7375SFelipe Balbi /* Autoclear doesn't clear RxPktRdy for short packets */ 8939001d80dSMing Lei if ((dma->desired_mode == 0 && !hw_ep->rx_double_buffered) 894550a7375SFelipe Balbi || (dma->actual_len 895550a7375SFelipe Balbi & (musb_ep->packet_sz - 1))) { 896550a7375SFelipe Balbi /* ack the read! */ 897550a7375SFelipe Balbi csr &= ~MUSB_RXCSR_RXPKTRDY; 898550a7375SFelipe Balbi musb_writew(epio, MUSB_RXCSR, csr); 899550a7375SFelipe Balbi } 900550a7375SFelipe Balbi 901550a7375SFelipe Balbi /* incomplete, and not short? wait for next IN packet */ 902550a7375SFelipe Balbi if ((request->actual < request->length) 903550a7375SFelipe Balbi && (musb_ep->dma->actual_len 9049001d80dSMing Lei == musb_ep->packet_sz)) { 9059001d80dSMing Lei /* In double buffer case, continue to unload fifo if 9069001d80dSMing Lei * there is Rx packet in FIFO. 9079001d80dSMing Lei **/ 9089001d80dSMing Lei csr = musb_readw(epio, MUSB_RXCSR); 9099001d80dSMing Lei if ((csr & MUSB_RXCSR_RXPKTRDY) && 9109001d80dSMing Lei hw_ep->rx_double_buffered) 9119001d80dSMing Lei goto exit; 912cea83241SSergei Shtylyov return; 9139001d80dSMing Lei } 914550a7375SFelipe Balbi #endif 915550a7375SFelipe Balbi musb_g_giveback(musb_ep, request, 0); 91639287076SSupriya Karanth /* 91739287076SSupriya Karanth * In the giveback function the MUSB lock is 91839287076SSupriya Karanth * released and acquired after sometime. During 91939287076SSupriya Karanth * this time period the INDEX register could get 92039287076SSupriya Karanth * changed by the gadget_queue function especially 92139287076SSupriya Karanth * on SMP systems. Reselect the INDEX to be sure 92239287076SSupriya Karanth * we are reading/modifying the right registers 92339287076SSupriya Karanth */ 92439287076SSupriya Karanth musb_ep_select(mbase, epnum); 925550a7375SFelipe Balbi 926ad1adb89SFelipe Balbi req = next_request(musb_ep); 927ad1adb89SFelipe Balbi if (!req) 928cea83241SSergei Shtylyov return; 929550a7375SFelipe Balbi } 930a48ff906SMian Yousaf Kaukab #if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_TUSB_OMAP_DMA) || \ 931a48ff906SMian Yousaf Kaukab defined(CONFIG_USB_UX500_DMA) 9329001d80dSMing Lei exit: 933bb324b08SAjay Kumar Gupta #endif 93443467868SSergei Shtylyov /* Analyze request */ 935ad1adb89SFelipe Balbi rxstate(musb, req); 936550a7375SFelipe Balbi } 937550a7375SFelipe Balbi 938550a7375SFelipe Balbi /* ------------------------------------------------------------ */ 939550a7375SFelipe Balbi 940550a7375SFelipe Balbi static int musb_gadget_enable(struct usb_ep *ep, 941550a7375SFelipe Balbi const struct usb_endpoint_descriptor *desc) 942550a7375SFelipe Balbi { 943550a7375SFelipe Balbi unsigned long flags; 944550a7375SFelipe Balbi struct musb_ep *musb_ep; 945550a7375SFelipe Balbi struct musb_hw_ep *hw_ep; 946550a7375SFelipe Balbi void __iomem *regs; 947550a7375SFelipe Balbi struct musb *musb; 948550a7375SFelipe Balbi void __iomem *mbase; 949550a7375SFelipe Balbi u8 epnum; 950550a7375SFelipe Balbi u16 csr; 951550a7375SFelipe Balbi unsigned tmp; 952550a7375SFelipe Balbi int status = -EINVAL; 953550a7375SFelipe Balbi 954550a7375SFelipe Balbi if (!ep || !desc) 955550a7375SFelipe Balbi return -EINVAL; 956550a7375SFelipe Balbi 957550a7375SFelipe Balbi musb_ep = to_musb_ep(ep); 958550a7375SFelipe Balbi hw_ep = musb_ep->hw_ep; 959550a7375SFelipe Balbi regs = hw_ep->regs; 960550a7375SFelipe Balbi musb = musb_ep->musb; 961550a7375SFelipe Balbi mbase = musb->mregs; 962550a7375SFelipe Balbi epnum = musb_ep->current_epnum; 963550a7375SFelipe Balbi 964550a7375SFelipe Balbi spin_lock_irqsave(&musb->lock, flags); 965550a7375SFelipe Balbi 966550a7375SFelipe Balbi if (musb_ep->desc) { 967550a7375SFelipe Balbi status = -EBUSY; 968550a7375SFelipe Balbi goto fail; 969550a7375SFelipe Balbi } 97096bcd090SJulia Lawall musb_ep->type = usb_endpoint_type(desc); 971550a7375SFelipe Balbi 972550a7375SFelipe Balbi /* check direction and (later) maxpacket size against endpoint */ 97396bcd090SJulia Lawall if (usb_endpoint_num(desc) != epnum) 974550a7375SFelipe Balbi goto fail; 975550a7375SFelipe Balbi 976550a7375SFelipe Balbi /* REVISIT this rules out high bandwidth periodic transfers */ 97729cc8897SKuninori Morimoto tmp = usb_endpoint_maxp(desc); 978f11d893dSMing Lei if (tmp & ~0x07ff) { 979f11d893dSMing Lei int ok; 980f11d893dSMing Lei 981f11d893dSMing Lei if (usb_endpoint_dir_in(desc)) 982f11d893dSMing Lei ok = musb->hb_iso_tx; 983f11d893dSMing Lei else 984f11d893dSMing Lei ok = musb->hb_iso_rx; 985f11d893dSMing Lei 986f11d893dSMing Lei if (!ok) { 987b99d3659SBin Liu musb_dbg(musb, "no support for high bandwidth ISO"); 988550a7375SFelipe Balbi goto fail; 989f11d893dSMing Lei } 990f11d893dSMing Lei musb_ep->hb_mult = (tmp >> 11) & 3; 991f11d893dSMing Lei } else { 992f11d893dSMing Lei musb_ep->hb_mult = 0; 993f11d893dSMing Lei } 994f11d893dSMing Lei 995f11d893dSMing Lei musb_ep->packet_sz = tmp & 0x7ff; 996f11d893dSMing Lei tmp = musb_ep->packet_sz * (musb_ep->hb_mult + 1); 997550a7375SFelipe Balbi 998550a7375SFelipe Balbi /* enable the interrupts for the endpoint, set the endpoint 999550a7375SFelipe Balbi * packet size (or fail), set the mode, clear the fifo 1000550a7375SFelipe Balbi */ 1001550a7375SFelipe Balbi musb_ep_select(mbase, epnum); 100296bcd090SJulia Lawall if (usb_endpoint_dir_in(desc)) { 1003550a7375SFelipe Balbi 1004550a7375SFelipe Balbi if (hw_ep->is_shared_fifo) 1005550a7375SFelipe Balbi musb_ep->is_in = 1; 1006550a7375SFelipe Balbi if (!musb_ep->is_in) 1007550a7375SFelipe Balbi goto fail; 1008f11d893dSMing Lei 1009f11d893dSMing Lei if (tmp > hw_ep->max_packet_sz_tx) { 1010b99d3659SBin Liu musb_dbg(musb, "packet size beyond hardware FIFO size"); 1011550a7375SFelipe Balbi goto fail; 1012f11d893dSMing Lei } 1013550a7375SFelipe Balbi 1014b18d26f6SSebastian Andrzej Siewior musb->intrtxe |= (1 << epnum); 1015b18d26f6SSebastian Andrzej Siewior musb_writew(mbase, MUSB_INTRTXE, musb->intrtxe); 1016550a7375SFelipe Balbi 1017550a7375SFelipe Balbi /* REVISIT if can_bulk_split(), use by updating "tmp"; 1018550a7375SFelipe Balbi * likewise high bandwidth periodic tx 1019550a7375SFelipe Balbi */ 10209f445cb2SCliff Cai /* Set TXMAXP with the FIFO size of the endpoint 102131c9909bSMing Lei * to disable double buffering mode. 10229f445cb2SCliff Cai */ 1023bb3a2ef2Ssupriya karanth if (musb->double_buffer_not_ok) { 102406624818SFelipe Balbi musb_writew(regs, MUSB_TXMAXP, hw_ep->max_packet_sz_tx); 1025bb3a2ef2Ssupriya karanth } else { 1026bb3a2ef2Ssupriya karanth if (can_bulk_split(musb, musb_ep->type)) 1027bb3a2ef2Ssupriya karanth musb_ep->hb_mult = (hw_ep->max_packet_sz_tx / 1028bb3a2ef2Ssupriya karanth musb_ep->packet_sz) - 1; 102906624818SFelipe Balbi musb_writew(regs, MUSB_TXMAXP, musb_ep->packet_sz 103006624818SFelipe Balbi | (musb_ep->hb_mult << 11)); 1031bb3a2ef2Ssupriya karanth } 1032550a7375SFelipe Balbi 1033550a7375SFelipe Balbi csr = MUSB_TXCSR_MODE | MUSB_TXCSR_CLRDATATOG; 1034550a7375SFelipe Balbi if (musb_readw(regs, MUSB_TXCSR) 1035550a7375SFelipe Balbi & MUSB_TXCSR_FIFONOTEMPTY) 1036550a7375SFelipe Balbi csr |= MUSB_TXCSR_FLUSHFIFO; 1037550a7375SFelipe Balbi if (musb_ep->type == USB_ENDPOINT_XFER_ISOC) 1038550a7375SFelipe Balbi csr |= MUSB_TXCSR_P_ISO; 1039550a7375SFelipe Balbi 1040550a7375SFelipe Balbi /* set twice in case of double buffering */ 1041550a7375SFelipe Balbi musb_writew(regs, MUSB_TXCSR, csr); 1042550a7375SFelipe Balbi /* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */ 1043550a7375SFelipe Balbi musb_writew(regs, MUSB_TXCSR, csr); 1044550a7375SFelipe Balbi 1045550a7375SFelipe Balbi } else { 1046550a7375SFelipe Balbi 1047550a7375SFelipe Balbi if (hw_ep->is_shared_fifo) 1048550a7375SFelipe Balbi musb_ep->is_in = 0; 1049550a7375SFelipe Balbi if (musb_ep->is_in) 1050550a7375SFelipe Balbi goto fail; 1051f11d893dSMing Lei 1052f11d893dSMing Lei if (tmp > hw_ep->max_packet_sz_rx) { 1053b99d3659SBin Liu musb_dbg(musb, "packet size beyond hardware FIFO size"); 1054550a7375SFelipe Balbi goto fail; 1055f11d893dSMing Lei } 1056550a7375SFelipe Balbi 1057af5ec14dSSebastian Andrzej Siewior musb->intrrxe |= (1 << epnum); 1058af5ec14dSSebastian Andrzej Siewior musb_writew(mbase, MUSB_INTRRXE, musb->intrrxe); 1059550a7375SFelipe Balbi 1060550a7375SFelipe Balbi /* REVISIT if can_bulk_combine() use by updating "tmp" 1061550a7375SFelipe Balbi * likewise high bandwidth periodic rx 1062550a7375SFelipe Balbi */ 10639f445cb2SCliff Cai /* Set RXMAXP with the FIFO size of the endpoint 10649f445cb2SCliff Cai * to disable double buffering mode. 10659f445cb2SCliff Cai */ 106606624818SFelipe Balbi if (musb->double_buffer_not_ok) 106706624818SFelipe Balbi musb_writew(regs, MUSB_RXMAXP, hw_ep->max_packet_sz_tx); 106806624818SFelipe Balbi else 106906624818SFelipe Balbi musb_writew(regs, MUSB_RXMAXP, musb_ep->packet_sz 107006624818SFelipe Balbi | (musb_ep->hb_mult << 11)); 1071550a7375SFelipe Balbi 1072550a7375SFelipe Balbi /* force shared fifo to OUT-only mode */ 1073550a7375SFelipe Balbi if (hw_ep->is_shared_fifo) { 1074550a7375SFelipe Balbi csr = musb_readw(regs, MUSB_TXCSR); 1075550a7375SFelipe Balbi csr &= ~(MUSB_TXCSR_MODE | MUSB_TXCSR_TXPKTRDY); 1076550a7375SFelipe Balbi musb_writew(regs, MUSB_TXCSR, csr); 1077550a7375SFelipe Balbi } 1078550a7375SFelipe Balbi 1079550a7375SFelipe Balbi csr = MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_CLRDATATOG; 1080550a7375SFelipe Balbi if (musb_ep->type == USB_ENDPOINT_XFER_ISOC) 1081550a7375SFelipe Balbi csr |= MUSB_RXCSR_P_ISO; 1082550a7375SFelipe Balbi else if (musb_ep->type == USB_ENDPOINT_XFER_INT) 1083550a7375SFelipe Balbi csr |= MUSB_RXCSR_DISNYET; 1084550a7375SFelipe Balbi 1085550a7375SFelipe Balbi /* set twice in case of double buffering */ 1086550a7375SFelipe Balbi musb_writew(regs, MUSB_RXCSR, csr); 1087550a7375SFelipe Balbi musb_writew(regs, MUSB_RXCSR, csr); 1088550a7375SFelipe Balbi } 1089550a7375SFelipe Balbi 1090550a7375SFelipe Balbi /* NOTE: all the I/O code _should_ work fine without DMA, in case 1091550a7375SFelipe Balbi * for some reason you run out of channels here. 1092550a7375SFelipe Balbi */ 1093550a7375SFelipe Balbi if (is_dma_capable() && musb->dma_controller) { 1094550a7375SFelipe Balbi struct dma_controller *c = musb->dma_controller; 1095550a7375SFelipe Balbi 1096550a7375SFelipe Balbi musb_ep->dma = c->channel_alloc(c, hw_ep, 1097550a7375SFelipe Balbi (desc->bEndpointAddress & USB_DIR_IN)); 1098550a7375SFelipe Balbi } else 1099550a7375SFelipe Balbi musb_ep->dma = NULL; 1100550a7375SFelipe Balbi 1101550a7375SFelipe Balbi musb_ep->desc = desc; 1102550a7375SFelipe Balbi musb_ep->busy = 0; 110347e97605SSergei Shtylyov musb_ep->wedged = 0; 1104550a7375SFelipe Balbi status = 0; 1105550a7375SFelipe Balbi 1106550a7375SFelipe Balbi pr_debug("%s periph: enabled %s for %s %s, %smaxpacket %d\n", 1107550a7375SFelipe Balbi musb_driver_name, musb_ep->end_point.name, 1108550a7375SFelipe Balbi ({ char *s; switch (musb_ep->type) { 1109550a7375SFelipe Balbi case USB_ENDPOINT_XFER_BULK: s = "bulk"; break; 1110550a7375SFelipe Balbi case USB_ENDPOINT_XFER_INT: s = "int"; break; 1111550a7375SFelipe Balbi default: s = "iso"; break; 11122b84f92bSJoe Perches } s; }), 1113550a7375SFelipe Balbi musb_ep->is_in ? "IN" : "OUT", 1114550a7375SFelipe Balbi musb_ep->dma ? "dma, " : "", 1115550a7375SFelipe Balbi musb_ep->packet_sz); 1116550a7375SFelipe Balbi 1117550a7375SFelipe Balbi schedule_work(&musb->irq_work); 1118550a7375SFelipe Balbi 1119550a7375SFelipe Balbi fail: 1120550a7375SFelipe Balbi spin_unlock_irqrestore(&musb->lock, flags); 1121550a7375SFelipe Balbi return status; 1122550a7375SFelipe Balbi } 1123550a7375SFelipe Balbi 1124550a7375SFelipe Balbi /* 1125550a7375SFelipe Balbi * Disable an endpoint flushing all requests queued. 1126550a7375SFelipe Balbi */ 1127550a7375SFelipe Balbi static int musb_gadget_disable(struct usb_ep *ep) 1128550a7375SFelipe Balbi { 1129550a7375SFelipe Balbi unsigned long flags; 1130550a7375SFelipe Balbi struct musb *musb; 1131550a7375SFelipe Balbi u8 epnum; 1132550a7375SFelipe Balbi struct musb_ep *musb_ep; 1133550a7375SFelipe Balbi void __iomem *epio; 1134550a7375SFelipe Balbi int status = 0; 1135550a7375SFelipe Balbi 1136550a7375SFelipe Balbi musb_ep = to_musb_ep(ep); 1137550a7375SFelipe Balbi musb = musb_ep->musb; 1138550a7375SFelipe Balbi epnum = musb_ep->current_epnum; 1139550a7375SFelipe Balbi epio = musb->endpoints[epnum].regs; 1140550a7375SFelipe Balbi 1141550a7375SFelipe Balbi spin_lock_irqsave(&musb->lock, flags); 1142550a7375SFelipe Balbi musb_ep_select(musb->mregs, epnum); 1143550a7375SFelipe Balbi 1144550a7375SFelipe Balbi /* zero the endpoint sizes */ 1145550a7375SFelipe Balbi if (musb_ep->is_in) { 1146b18d26f6SSebastian Andrzej Siewior musb->intrtxe &= ~(1 << epnum); 1147b18d26f6SSebastian Andrzej Siewior musb_writew(musb->mregs, MUSB_INTRTXE, musb->intrtxe); 1148550a7375SFelipe Balbi musb_writew(epio, MUSB_TXMAXP, 0); 1149550a7375SFelipe Balbi } else { 1150af5ec14dSSebastian Andrzej Siewior musb->intrrxe &= ~(1 << epnum); 1151af5ec14dSSebastian Andrzej Siewior musb_writew(musb->mregs, MUSB_INTRRXE, musb->intrrxe); 1152550a7375SFelipe Balbi musb_writew(epio, MUSB_RXMAXP, 0); 1153550a7375SFelipe Balbi } 1154550a7375SFelipe Balbi 1155550a7375SFelipe Balbi /* abort all pending DMA and requests */ 1156550a7375SFelipe Balbi nuke(musb_ep, -ESHUTDOWN); 1157550a7375SFelipe Balbi 1158607fb0f4STal Shorer musb_ep->desc = NULL; 1159607fb0f4STal Shorer musb_ep->end_point.desc = NULL; 1160607fb0f4STal Shorer 1161550a7375SFelipe Balbi schedule_work(&musb->irq_work); 1162550a7375SFelipe Balbi 1163550a7375SFelipe Balbi spin_unlock_irqrestore(&(musb->lock), flags); 1164550a7375SFelipe Balbi 1165b99d3659SBin Liu musb_dbg(musb, "%s", musb_ep->end_point.name); 1166550a7375SFelipe Balbi 1167550a7375SFelipe Balbi return status; 1168550a7375SFelipe Balbi } 1169550a7375SFelipe Balbi 1170550a7375SFelipe Balbi /* 1171550a7375SFelipe Balbi * Allocate a request for an endpoint. 1172550a7375SFelipe Balbi * Reused by ep0 code. 1173550a7375SFelipe Balbi */ 1174550a7375SFelipe Balbi struct usb_request *musb_alloc_request(struct usb_ep *ep, gfp_t gfp_flags) 1175550a7375SFelipe Balbi { 1176550a7375SFelipe Balbi struct musb_ep *musb_ep = to_musb_ep(ep); 1177550a7375SFelipe Balbi struct musb_request *request = NULL; 1178550a7375SFelipe Balbi 1179550a7375SFelipe Balbi request = kzalloc(sizeof *request, gfp_flags); 1180b99d3659SBin Liu if (!request) 11810607f862SFelipe Balbi return NULL; 11820607f862SFelipe Balbi 1183550a7375SFelipe Balbi request->request.dma = DMA_ADDR_INVALID; 1184550a7375SFelipe Balbi request->epnum = musb_ep->current_epnum; 1185550a7375SFelipe Balbi request->ep = musb_ep; 1186550a7375SFelipe Balbi 1187fc78003eSBin Liu trace_musb_req_alloc(request); 1188550a7375SFelipe Balbi return &request->request; 1189550a7375SFelipe Balbi } 1190550a7375SFelipe Balbi 1191550a7375SFelipe Balbi /* 1192550a7375SFelipe Balbi * Free a request 1193550a7375SFelipe Balbi * Reused by ep0 code. 1194550a7375SFelipe Balbi */ 1195550a7375SFelipe Balbi void musb_free_request(struct usb_ep *ep, struct usb_request *req) 1196550a7375SFelipe Balbi { 1197fc78003eSBin Liu struct musb_request *request = to_musb_request(req); 1198fc78003eSBin Liu 1199fc78003eSBin Liu trace_musb_req_free(request); 1200fc78003eSBin Liu kfree(request); 1201550a7375SFelipe Balbi } 1202550a7375SFelipe Balbi 1203550a7375SFelipe Balbi static LIST_HEAD(buffers); 1204550a7375SFelipe Balbi 1205550a7375SFelipe Balbi struct free_record { 1206550a7375SFelipe Balbi struct list_head list; 1207550a7375SFelipe Balbi struct device *dev; 1208550a7375SFelipe Balbi unsigned bytes; 1209550a7375SFelipe Balbi dma_addr_t dma; 1210550a7375SFelipe Balbi }; 1211550a7375SFelipe Balbi 1212550a7375SFelipe Balbi /* 1213550a7375SFelipe Balbi * Context: controller locked, IRQs blocked. 1214550a7375SFelipe Balbi */ 1215a666e3e6SSergei Shtylyov void musb_ep_restart(struct musb *musb, struct musb_request *req) 1216550a7375SFelipe Balbi { 1217fc78003eSBin Liu trace_musb_req_start(req); 1218550a7375SFelipe Balbi musb_ep_select(musb->mregs, req->epnum); 1219550a7375SFelipe Balbi if (req->tx) 1220550a7375SFelipe Balbi txstate(musb, req); 1221550a7375SFelipe Balbi else 1222550a7375SFelipe Balbi rxstate(musb, req); 1223550a7375SFelipe Balbi } 1224550a7375SFelipe Balbi 1225550a7375SFelipe Balbi static int musb_gadget_queue(struct usb_ep *ep, struct usb_request *req, 1226550a7375SFelipe Balbi gfp_t gfp_flags) 1227550a7375SFelipe Balbi { 1228550a7375SFelipe Balbi struct musb_ep *musb_ep; 1229550a7375SFelipe Balbi struct musb_request *request; 1230550a7375SFelipe Balbi struct musb *musb; 1231550a7375SFelipe Balbi int status = 0; 1232550a7375SFelipe Balbi unsigned long lockflags; 1233550a7375SFelipe Balbi 1234550a7375SFelipe Balbi if (!ep || !req) 1235550a7375SFelipe Balbi return -EINVAL; 1236550a7375SFelipe Balbi if (!req->buf) 1237550a7375SFelipe Balbi return -ENODATA; 1238550a7375SFelipe Balbi 1239550a7375SFelipe Balbi musb_ep = to_musb_ep(ep); 1240550a7375SFelipe Balbi musb = musb_ep->musb; 1241550a7375SFelipe Balbi 1242550a7375SFelipe Balbi request = to_musb_request(req); 1243550a7375SFelipe Balbi request->musb = musb; 1244550a7375SFelipe Balbi 1245550a7375SFelipe Balbi if (request->ep != musb_ep) 1246550a7375SFelipe Balbi return -EINVAL; 1247550a7375SFelipe Balbi 1248fc78003eSBin Liu trace_musb_req_enq(request); 1249550a7375SFelipe Balbi 1250550a7375SFelipe Balbi /* request is mine now... */ 1251550a7375SFelipe Balbi request->request.actual = 0; 1252550a7375SFelipe Balbi request->request.status = -EINPROGRESS; 1253550a7375SFelipe Balbi request->epnum = musb_ep->current_epnum; 1254550a7375SFelipe Balbi request->tx = musb_ep->is_in; 1255550a7375SFelipe Balbi 1256c65bfa62SMian Yousaf Kaukab map_dma_buffer(request, musb, musb_ep); 1257550a7375SFelipe Balbi 1258550a7375SFelipe Balbi spin_lock_irqsave(&musb->lock, lockflags); 1259550a7375SFelipe Balbi 1260550a7375SFelipe Balbi /* don't queue if the ep is down */ 1261550a7375SFelipe Balbi if (!musb_ep->desc) { 1262b99d3659SBin Liu musb_dbg(musb, "req %p queued to %s while ep %s", 1263550a7375SFelipe Balbi req, ep->name, "disabled"); 1264550a7375SFelipe Balbi status = -ESHUTDOWN; 126523a53d90SSebastian Andrzej Siewior unmap_dma_buffer(request, musb); 126623a53d90SSebastian Andrzej Siewior goto unlock; 1267550a7375SFelipe Balbi } 1268550a7375SFelipe Balbi 1269550a7375SFelipe Balbi /* add request to the list */ 1270ad1adb89SFelipe Balbi list_add_tail(&request->list, &musb_ep->req_list); 1271550a7375SFelipe Balbi 1272550a7375SFelipe Balbi /* it this is the head of the queue, start i/o ... */ 1273ad1adb89SFelipe Balbi if (!musb_ep->busy && &request->list == musb_ep->req_list.next) 1274550a7375SFelipe Balbi musb_ep_restart(musb, request); 1275550a7375SFelipe Balbi 127623a53d90SSebastian Andrzej Siewior unlock: 1277550a7375SFelipe Balbi spin_unlock_irqrestore(&musb->lock, lockflags); 1278550a7375SFelipe Balbi return status; 1279550a7375SFelipe Balbi } 1280550a7375SFelipe Balbi 1281550a7375SFelipe Balbi static int musb_gadget_dequeue(struct usb_ep *ep, struct usb_request *request) 1282550a7375SFelipe Balbi { 1283550a7375SFelipe Balbi struct musb_ep *musb_ep = to_musb_ep(ep); 12844cbbf084SFelipe Balbi struct musb_request *req = to_musb_request(request); 12854cbbf084SFelipe Balbi struct musb_request *r; 1286550a7375SFelipe Balbi unsigned long flags; 1287550a7375SFelipe Balbi int status = 0; 1288550a7375SFelipe Balbi struct musb *musb = musb_ep->musb; 1289550a7375SFelipe Balbi 1290fc78003eSBin Liu if (!ep || !request || req->ep != musb_ep) 1291550a7375SFelipe Balbi return -EINVAL; 1292550a7375SFelipe Balbi 1293fc78003eSBin Liu trace_musb_req_deq(req); 1294fc78003eSBin Liu 1295550a7375SFelipe Balbi spin_lock_irqsave(&musb->lock, flags); 1296550a7375SFelipe Balbi 1297550a7375SFelipe Balbi list_for_each_entry(r, &musb_ep->req_list, list) { 12984cbbf084SFelipe Balbi if (r == req) 1299550a7375SFelipe Balbi break; 1300550a7375SFelipe Balbi } 13014cbbf084SFelipe Balbi if (r != req) { 1302b99d3659SBin Liu dev_err(musb->controller, "request %p not queued to %s\n", 1303b99d3659SBin Liu request, ep->name); 1304550a7375SFelipe Balbi status = -EINVAL; 1305550a7375SFelipe Balbi goto done; 1306550a7375SFelipe Balbi } 1307550a7375SFelipe Balbi 1308550a7375SFelipe Balbi /* if the hardware doesn't have the request, easy ... */ 13093d5ad13eSFelipe Balbi if (musb_ep->req_list.next != &req->list || musb_ep->busy) 1310550a7375SFelipe Balbi musb_g_giveback(musb_ep, request, -ECONNRESET); 1311550a7375SFelipe Balbi 1312550a7375SFelipe Balbi /* ... else abort the dma transfer ... */ 1313550a7375SFelipe Balbi else if (is_dma_capable() && musb_ep->dma) { 1314550a7375SFelipe Balbi struct dma_controller *c = musb->dma_controller; 1315550a7375SFelipe Balbi 1316550a7375SFelipe Balbi musb_ep_select(musb->mregs, musb_ep->current_epnum); 1317550a7375SFelipe Balbi if (c->channel_abort) 1318550a7375SFelipe Balbi status = c->channel_abort(musb_ep->dma); 1319550a7375SFelipe Balbi else 1320550a7375SFelipe Balbi status = -EBUSY; 1321550a7375SFelipe Balbi if (status == 0) 1322550a7375SFelipe Balbi musb_g_giveback(musb_ep, request, -ECONNRESET); 1323550a7375SFelipe Balbi } else { 1324550a7375SFelipe Balbi /* NOTE: by sticking to easily tested hardware/driver states, 1325550a7375SFelipe Balbi * we leave counting of in-flight packets imprecise. 1326550a7375SFelipe Balbi */ 1327550a7375SFelipe Balbi musb_g_giveback(musb_ep, request, -ECONNRESET); 1328550a7375SFelipe Balbi } 1329550a7375SFelipe Balbi 1330550a7375SFelipe Balbi done: 1331550a7375SFelipe Balbi spin_unlock_irqrestore(&musb->lock, flags); 1332550a7375SFelipe Balbi return status; 1333550a7375SFelipe Balbi } 1334550a7375SFelipe Balbi 1335550a7375SFelipe Balbi /* 1336550a7375SFelipe Balbi * Set or clear the halt bit of an endpoint. A halted enpoint won't tx/rx any 1337550a7375SFelipe Balbi * data but will queue requests. 1338550a7375SFelipe Balbi * 1339550a7375SFelipe Balbi * exported to ep0 code 1340550a7375SFelipe Balbi */ 13411b6c3b0fSFelipe Balbi static int musb_gadget_set_halt(struct usb_ep *ep, int value) 1342550a7375SFelipe Balbi { 1343550a7375SFelipe Balbi struct musb_ep *musb_ep = to_musb_ep(ep); 1344550a7375SFelipe Balbi u8 epnum = musb_ep->current_epnum; 1345550a7375SFelipe Balbi struct musb *musb = musb_ep->musb; 1346550a7375SFelipe Balbi void __iomem *epio = musb->endpoints[epnum].regs; 1347550a7375SFelipe Balbi void __iomem *mbase; 1348550a7375SFelipe Balbi unsigned long flags; 1349550a7375SFelipe Balbi u16 csr; 1350cea83241SSergei Shtylyov struct musb_request *request; 1351550a7375SFelipe Balbi int status = 0; 1352550a7375SFelipe Balbi 1353550a7375SFelipe Balbi if (!ep) 1354550a7375SFelipe Balbi return -EINVAL; 1355550a7375SFelipe Balbi mbase = musb->mregs; 1356550a7375SFelipe Balbi 1357550a7375SFelipe Balbi spin_lock_irqsave(&musb->lock, flags); 1358550a7375SFelipe Balbi 1359550a7375SFelipe Balbi if ((USB_ENDPOINT_XFER_ISOC == musb_ep->type)) { 1360550a7375SFelipe Balbi status = -EINVAL; 1361550a7375SFelipe Balbi goto done; 1362550a7375SFelipe Balbi } 1363550a7375SFelipe Balbi 1364550a7375SFelipe Balbi musb_ep_select(mbase, epnum); 1365550a7375SFelipe Balbi 1366ad1adb89SFelipe Balbi request = next_request(musb_ep); 1367cea83241SSergei Shtylyov if (value) { 1368cea83241SSergei Shtylyov if (request) { 1369b99d3659SBin Liu musb_dbg(musb, "request in progress, cannot halt %s", 1370cea83241SSergei Shtylyov ep->name); 1371cea83241SSergei Shtylyov status = -EAGAIN; 1372cea83241SSergei Shtylyov goto done; 1373cea83241SSergei Shtylyov } 1374cea83241SSergei Shtylyov /* Cannot portably stall with non-empty FIFO */ 1375cea83241SSergei Shtylyov if (musb_ep->is_in) { 1376550a7375SFelipe Balbi csr = musb_readw(epio, MUSB_TXCSR); 1377550a7375SFelipe Balbi if (csr & MUSB_TXCSR_FIFONOTEMPTY) { 1378b99d3659SBin Liu musb_dbg(musb, "FIFO busy, cannot halt %s", 1379b99d3659SBin Liu ep->name); 1380cea83241SSergei Shtylyov status = -EAGAIN; 1381cea83241SSergei Shtylyov goto done; 1382550a7375SFelipe Balbi } 1383cea83241SSergei Shtylyov } 138447e97605SSergei Shtylyov } else 138547e97605SSergei Shtylyov musb_ep->wedged = 0; 1386550a7375SFelipe Balbi 1387550a7375SFelipe Balbi /* set/clear the stall and toggle bits */ 1388b99d3659SBin Liu musb_dbg(musb, "%s: %s stall", ep->name, value ? "set" : "clear"); 1389550a7375SFelipe Balbi if (musb_ep->is_in) { 1390550a7375SFelipe Balbi csr = musb_readw(epio, MUSB_TXCSR); 1391550a7375SFelipe Balbi csr |= MUSB_TXCSR_P_WZC_BITS 1392550a7375SFelipe Balbi | MUSB_TXCSR_CLRDATATOG; 1393550a7375SFelipe Balbi if (value) 1394550a7375SFelipe Balbi csr |= MUSB_TXCSR_P_SENDSTALL; 1395550a7375SFelipe Balbi else 1396550a7375SFelipe Balbi csr &= ~(MUSB_TXCSR_P_SENDSTALL 1397550a7375SFelipe Balbi | MUSB_TXCSR_P_SENTSTALL); 1398550a7375SFelipe Balbi csr &= ~MUSB_TXCSR_TXPKTRDY; 1399550a7375SFelipe Balbi musb_writew(epio, MUSB_TXCSR, csr); 1400550a7375SFelipe Balbi } else { 1401550a7375SFelipe Balbi csr = musb_readw(epio, MUSB_RXCSR); 1402550a7375SFelipe Balbi csr |= MUSB_RXCSR_P_WZC_BITS 1403550a7375SFelipe Balbi | MUSB_RXCSR_FLUSHFIFO 1404550a7375SFelipe Balbi | MUSB_RXCSR_CLRDATATOG; 1405550a7375SFelipe Balbi if (value) 1406550a7375SFelipe Balbi csr |= MUSB_RXCSR_P_SENDSTALL; 1407550a7375SFelipe Balbi else 1408550a7375SFelipe Balbi csr &= ~(MUSB_RXCSR_P_SENDSTALL 1409550a7375SFelipe Balbi | MUSB_RXCSR_P_SENTSTALL); 1410550a7375SFelipe Balbi musb_writew(epio, MUSB_RXCSR, csr); 1411550a7375SFelipe Balbi } 1412550a7375SFelipe Balbi 1413550a7375SFelipe Balbi /* maybe start the first request in the queue */ 1414550a7375SFelipe Balbi if (!musb_ep->busy && !value && request) { 1415b99d3659SBin Liu musb_dbg(musb, "restarting the request"); 1416550a7375SFelipe Balbi musb_ep_restart(musb, request); 1417550a7375SFelipe Balbi } 1418550a7375SFelipe Balbi 1419cea83241SSergei Shtylyov done: 1420550a7375SFelipe Balbi spin_unlock_irqrestore(&musb->lock, flags); 1421550a7375SFelipe Balbi return status; 1422550a7375SFelipe Balbi } 1423550a7375SFelipe Balbi 142447e97605SSergei Shtylyov /* 142547e97605SSergei Shtylyov * Sets the halt feature with the clear requests ignored 142647e97605SSergei Shtylyov */ 14271b6c3b0fSFelipe Balbi static int musb_gadget_set_wedge(struct usb_ep *ep) 142847e97605SSergei Shtylyov { 142947e97605SSergei Shtylyov struct musb_ep *musb_ep = to_musb_ep(ep); 143047e97605SSergei Shtylyov 143147e97605SSergei Shtylyov if (!ep) 143247e97605SSergei Shtylyov return -EINVAL; 143347e97605SSergei Shtylyov 143447e97605SSergei Shtylyov musb_ep->wedged = 1; 143547e97605SSergei Shtylyov 143647e97605SSergei Shtylyov return usb_ep_set_halt(ep); 143747e97605SSergei Shtylyov } 143847e97605SSergei Shtylyov 1439550a7375SFelipe Balbi static int musb_gadget_fifo_status(struct usb_ep *ep) 1440550a7375SFelipe Balbi { 1441550a7375SFelipe Balbi struct musb_ep *musb_ep = to_musb_ep(ep); 1442550a7375SFelipe Balbi void __iomem *epio = musb_ep->hw_ep->regs; 1443550a7375SFelipe Balbi int retval = -EINVAL; 1444550a7375SFelipe Balbi 1445550a7375SFelipe Balbi if (musb_ep->desc && !musb_ep->is_in) { 1446550a7375SFelipe Balbi struct musb *musb = musb_ep->musb; 1447550a7375SFelipe Balbi int epnum = musb_ep->current_epnum; 1448550a7375SFelipe Balbi void __iomem *mbase = musb->mregs; 1449550a7375SFelipe Balbi unsigned long flags; 1450550a7375SFelipe Balbi 1451550a7375SFelipe Balbi spin_lock_irqsave(&musb->lock, flags); 1452550a7375SFelipe Balbi 1453550a7375SFelipe Balbi musb_ep_select(mbase, epnum); 1454550a7375SFelipe Balbi /* FIXME return zero unless RXPKTRDY is set */ 1455550a7375SFelipe Balbi retval = musb_readw(epio, MUSB_RXCOUNT); 1456550a7375SFelipe Balbi 1457550a7375SFelipe Balbi spin_unlock_irqrestore(&musb->lock, flags); 1458550a7375SFelipe Balbi } 1459550a7375SFelipe Balbi return retval; 1460550a7375SFelipe Balbi } 1461550a7375SFelipe Balbi 1462550a7375SFelipe Balbi static void musb_gadget_fifo_flush(struct usb_ep *ep) 1463550a7375SFelipe Balbi { 1464550a7375SFelipe Balbi struct musb_ep *musb_ep = to_musb_ep(ep); 1465550a7375SFelipe Balbi struct musb *musb = musb_ep->musb; 1466550a7375SFelipe Balbi u8 epnum = musb_ep->current_epnum; 1467550a7375SFelipe Balbi void __iomem *epio = musb->endpoints[epnum].regs; 1468550a7375SFelipe Balbi void __iomem *mbase; 1469550a7375SFelipe Balbi unsigned long flags; 1470b18d26f6SSebastian Andrzej Siewior u16 csr; 1471550a7375SFelipe Balbi 1472550a7375SFelipe Balbi mbase = musb->mregs; 1473550a7375SFelipe Balbi 1474550a7375SFelipe Balbi spin_lock_irqsave(&musb->lock, flags); 1475550a7375SFelipe Balbi musb_ep_select(mbase, (u8) epnum); 1476550a7375SFelipe Balbi 1477550a7375SFelipe Balbi /* disable interrupts */ 1478b18d26f6SSebastian Andrzej Siewior musb_writew(mbase, MUSB_INTRTXE, musb->intrtxe & ~(1 << epnum)); 1479550a7375SFelipe Balbi 1480550a7375SFelipe Balbi if (musb_ep->is_in) { 1481550a7375SFelipe Balbi csr = musb_readw(epio, MUSB_TXCSR); 1482550a7375SFelipe Balbi if (csr & MUSB_TXCSR_FIFONOTEMPTY) { 1483550a7375SFelipe Balbi csr |= MUSB_TXCSR_FLUSHFIFO | MUSB_TXCSR_P_WZC_BITS; 14844858f06eSYauheni Kaliuta /* 14854858f06eSYauheni Kaliuta * Setting both TXPKTRDY and FLUSHFIFO makes controller 14864858f06eSYauheni Kaliuta * to interrupt current FIFO loading, but not flushing 14874858f06eSYauheni Kaliuta * the already loaded ones. 14884858f06eSYauheni Kaliuta */ 14894858f06eSYauheni Kaliuta csr &= ~MUSB_TXCSR_TXPKTRDY; 1490550a7375SFelipe Balbi musb_writew(epio, MUSB_TXCSR, csr); 1491550a7375SFelipe Balbi /* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */ 1492550a7375SFelipe Balbi musb_writew(epio, MUSB_TXCSR, csr); 1493550a7375SFelipe Balbi } 1494550a7375SFelipe Balbi } else { 1495550a7375SFelipe Balbi csr = musb_readw(epio, MUSB_RXCSR); 1496550a7375SFelipe Balbi csr |= MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_P_WZC_BITS; 1497550a7375SFelipe Balbi musb_writew(epio, MUSB_RXCSR, csr); 1498550a7375SFelipe Balbi musb_writew(epio, MUSB_RXCSR, csr); 1499550a7375SFelipe Balbi } 1500550a7375SFelipe Balbi 1501550a7375SFelipe Balbi /* re-enable interrupt */ 1502b18d26f6SSebastian Andrzej Siewior musb_writew(mbase, MUSB_INTRTXE, musb->intrtxe); 1503550a7375SFelipe Balbi spin_unlock_irqrestore(&musb->lock, flags); 1504550a7375SFelipe Balbi } 1505550a7375SFelipe Balbi 1506550a7375SFelipe Balbi static const struct usb_ep_ops musb_ep_ops = { 1507550a7375SFelipe Balbi .enable = musb_gadget_enable, 1508550a7375SFelipe Balbi .disable = musb_gadget_disable, 1509550a7375SFelipe Balbi .alloc_request = musb_alloc_request, 1510550a7375SFelipe Balbi .free_request = musb_free_request, 1511550a7375SFelipe Balbi .queue = musb_gadget_queue, 1512550a7375SFelipe Balbi .dequeue = musb_gadget_dequeue, 1513550a7375SFelipe Balbi .set_halt = musb_gadget_set_halt, 151447e97605SSergei Shtylyov .set_wedge = musb_gadget_set_wedge, 1515550a7375SFelipe Balbi .fifo_status = musb_gadget_fifo_status, 1516550a7375SFelipe Balbi .fifo_flush = musb_gadget_fifo_flush 1517550a7375SFelipe Balbi }; 1518550a7375SFelipe Balbi 1519550a7375SFelipe Balbi /* ----------------------------------------------------------------------- */ 1520550a7375SFelipe Balbi 1521550a7375SFelipe Balbi static int musb_gadget_get_frame(struct usb_gadget *gadget) 1522550a7375SFelipe Balbi { 1523550a7375SFelipe Balbi struct musb *musb = gadget_to_musb(gadget); 1524550a7375SFelipe Balbi 1525550a7375SFelipe Balbi return (int)musb_readw(musb->mregs, MUSB_FRAME); 1526550a7375SFelipe Balbi } 1527550a7375SFelipe Balbi 1528550a7375SFelipe Balbi static int musb_gadget_wakeup(struct usb_gadget *gadget) 1529550a7375SFelipe Balbi { 1530550a7375SFelipe Balbi struct musb *musb = gadget_to_musb(gadget); 1531550a7375SFelipe Balbi void __iomem *mregs = musb->mregs; 1532550a7375SFelipe Balbi unsigned long flags; 1533550a7375SFelipe Balbi int status = -EINVAL; 1534550a7375SFelipe Balbi u8 power, devctl; 1535550a7375SFelipe Balbi int retries; 1536550a7375SFelipe Balbi 1537550a7375SFelipe Balbi spin_lock_irqsave(&musb->lock, flags); 1538550a7375SFelipe Balbi 1539e47d9254SAntoine Tenart switch (musb->xceiv->otg->state) { 1540550a7375SFelipe Balbi case OTG_STATE_B_PERIPHERAL: 1541550a7375SFelipe Balbi /* NOTE: OTG state machine doesn't include B_SUSPENDED; 1542550a7375SFelipe Balbi * that's part of the standard usb 1.1 state machine, and 1543550a7375SFelipe Balbi * doesn't affect OTG transitions. 1544550a7375SFelipe Balbi */ 1545550a7375SFelipe Balbi if (musb->may_wakeup && musb->is_suspended) 1546550a7375SFelipe Balbi break; 1547550a7375SFelipe Balbi goto done; 1548550a7375SFelipe Balbi case OTG_STATE_B_IDLE: 1549550a7375SFelipe Balbi /* Start SRP ... OTG not required. */ 1550550a7375SFelipe Balbi devctl = musb_readb(mregs, MUSB_DEVCTL); 1551b99d3659SBin Liu musb_dbg(musb, "Sending SRP: devctl: %02x", devctl); 1552550a7375SFelipe Balbi devctl |= MUSB_DEVCTL_SESSION; 1553550a7375SFelipe Balbi musb_writeb(mregs, MUSB_DEVCTL, devctl); 1554550a7375SFelipe Balbi devctl = musb_readb(mregs, MUSB_DEVCTL); 1555550a7375SFelipe Balbi retries = 100; 1556550a7375SFelipe Balbi while (!(devctl & MUSB_DEVCTL_SESSION)) { 1557550a7375SFelipe Balbi devctl = musb_readb(mregs, MUSB_DEVCTL); 1558550a7375SFelipe Balbi if (retries-- < 1) 1559550a7375SFelipe Balbi break; 1560550a7375SFelipe Balbi } 1561550a7375SFelipe Balbi retries = 10000; 1562550a7375SFelipe Balbi while (devctl & MUSB_DEVCTL_SESSION) { 1563550a7375SFelipe Balbi devctl = musb_readb(mregs, MUSB_DEVCTL); 1564550a7375SFelipe Balbi if (retries-- < 1) 1565550a7375SFelipe Balbi break; 1566550a7375SFelipe Balbi } 1567550a7375SFelipe Balbi 15688620543eSHema HK spin_unlock_irqrestore(&musb->lock, flags); 15696e13c650SHeikki Krogerus otg_start_srp(musb->xceiv->otg); 15708620543eSHema HK spin_lock_irqsave(&musb->lock, flags); 15718620543eSHema HK 1572550a7375SFelipe Balbi /* Block idling for at least 1s */ 1573550a7375SFelipe Balbi musb_platform_try_idle(musb, 1574550a7375SFelipe Balbi jiffies + msecs_to_jiffies(1 * HZ)); 1575550a7375SFelipe Balbi 1576550a7375SFelipe Balbi status = 0; 1577550a7375SFelipe Balbi goto done; 1578550a7375SFelipe Balbi default: 1579b99d3659SBin Liu musb_dbg(musb, "Unhandled wake: %s", 1580e47d9254SAntoine Tenart usb_otg_state_string(musb->xceiv->otg->state)); 1581550a7375SFelipe Balbi goto done; 1582550a7375SFelipe Balbi } 1583550a7375SFelipe Balbi 1584550a7375SFelipe Balbi status = 0; 1585550a7375SFelipe Balbi 1586550a7375SFelipe Balbi power = musb_readb(mregs, MUSB_POWER); 1587550a7375SFelipe Balbi power |= MUSB_POWER_RESUME; 1588550a7375SFelipe Balbi musb_writeb(mregs, MUSB_POWER, power); 1589b99d3659SBin Liu musb_dbg(musb, "issue wakeup"); 1590550a7375SFelipe Balbi 1591550a7375SFelipe Balbi /* FIXME do this next chunk in a timer callback, no udelay */ 1592550a7375SFelipe Balbi mdelay(2); 1593550a7375SFelipe Balbi 1594550a7375SFelipe Balbi power = musb_readb(mregs, MUSB_POWER); 1595550a7375SFelipe Balbi power &= ~MUSB_POWER_RESUME; 1596550a7375SFelipe Balbi musb_writeb(mregs, MUSB_POWER, power); 1597550a7375SFelipe Balbi done: 1598550a7375SFelipe Balbi spin_unlock_irqrestore(&musb->lock, flags); 1599550a7375SFelipe Balbi return status; 1600550a7375SFelipe Balbi } 1601550a7375SFelipe Balbi 1602550a7375SFelipe Balbi static int 1603550a7375SFelipe Balbi musb_gadget_set_self_powered(struct usb_gadget *gadget, int is_selfpowered) 1604550a7375SFelipe Balbi { 1605dadac986SPeter Chen gadget->is_selfpowered = !!is_selfpowered; 1606550a7375SFelipe Balbi return 0; 1607550a7375SFelipe Balbi } 1608550a7375SFelipe Balbi 1609550a7375SFelipe Balbi static void musb_pullup(struct musb *musb, int is_on) 1610550a7375SFelipe Balbi { 1611550a7375SFelipe Balbi u8 power; 1612550a7375SFelipe Balbi 1613550a7375SFelipe Balbi power = musb_readb(musb->mregs, MUSB_POWER); 1614550a7375SFelipe Balbi if (is_on) 1615550a7375SFelipe Balbi power |= MUSB_POWER_SOFTCONN; 1616550a7375SFelipe Balbi else 1617550a7375SFelipe Balbi power &= ~MUSB_POWER_SOFTCONN; 1618550a7375SFelipe Balbi 1619550a7375SFelipe Balbi /* FIXME if on, HdrcStart; if off, HdrcStop */ 1620550a7375SFelipe Balbi 1621b99d3659SBin Liu musb_dbg(musb, "gadget D+ pullup %s", 1622e71eb392SSebastian Andrzej Siewior is_on ? "on" : "off"); 1623550a7375SFelipe Balbi musb_writeb(musb->mregs, MUSB_POWER, power); 1624550a7375SFelipe Balbi } 1625550a7375SFelipe Balbi 1626550a7375SFelipe Balbi #if 0 1627550a7375SFelipe Balbi static int musb_gadget_vbus_session(struct usb_gadget *gadget, int is_active) 1628550a7375SFelipe Balbi { 1629b99d3659SBin Liu musb_dbg(musb, "<= %s =>\n", __func__); 1630550a7375SFelipe Balbi 1631550a7375SFelipe Balbi /* 1632550a7375SFelipe Balbi * FIXME iff driver's softconnect flag is set (as it is during probe, 1633550a7375SFelipe Balbi * though that can clear it), just musb_pullup(). 1634550a7375SFelipe Balbi */ 1635550a7375SFelipe Balbi 1636550a7375SFelipe Balbi return -EINVAL; 1637550a7375SFelipe Balbi } 1638550a7375SFelipe Balbi #endif 1639550a7375SFelipe Balbi 1640550a7375SFelipe Balbi static int musb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA) 1641550a7375SFelipe Balbi { 1642550a7375SFelipe Balbi struct musb *musb = gadget_to_musb(gadget); 1643550a7375SFelipe Balbi 164484e250ffSDavid Brownell if (!musb->xceiv->set_power) 1645550a7375SFelipe Balbi return -EOPNOTSUPP; 1646b96d3b08SHeikki Krogerus return usb_phy_set_power(musb->xceiv, mA); 1647550a7375SFelipe Balbi } 1648550a7375SFelipe Balbi 1649517bafffSTony Lindgren static void musb_gadget_work(struct work_struct *work) 1650517bafffSTony Lindgren { 1651517bafffSTony Lindgren struct musb *musb; 1652517bafffSTony Lindgren unsigned long flags; 1653517bafffSTony Lindgren 1654517bafffSTony Lindgren musb = container_of(work, struct musb, gadget_work.work); 1655517bafffSTony Lindgren pm_runtime_get_sync(musb->controller); 1656517bafffSTony Lindgren spin_lock_irqsave(&musb->lock, flags); 1657517bafffSTony Lindgren musb_pullup(musb, musb->softconnect); 1658517bafffSTony Lindgren spin_unlock_irqrestore(&musb->lock, flags); 1659517bafffSTony Lindgren pm_runtime_mark_last_busy(musb->controller); 1660517bafffSTony Lindgren pm_runtime_put_autosuspend(musb->controller); 1661517bafffSTony Lindgren } 1662517bafffSTony Lindgren 1663550a7375SFelipe Balbi static int musb_gadget_pullup(struct usb_gadget *gadget, int is_on) 1664550a7375SFelipe Balbi { 1665550a7375SFelipe Balbi struct musb *musb = gadget_to_musb(gadget); 1666550a7375SFelipe Balbi unsigned long flags; 1667550a7375SFelipe Balbi 1668550a7375SFelipe Balbi is_on = !!is_on; 1669550a7375SFelipe Balbi 1670550a7375SFelipe Balbi /* NOTE: this assumes we are sensing vbus; we'd rather 1671550a7375SFelipe Balbi * not pullup unless the B-session is active. 1672550a7375SFelipe Balbi */ 1673550a7375SFelipe Balbi spin_lock_irqsave(&musb->lock, flags); 1674550a7375SFelipe Balbi if (is_on != musb->softconnect) { 1675550a7375SFelipe Balbi musb->softconnect = is_on; 1676517bafffSTony Lindgren schedule_delayed_work(&musb->gadget_work, 0); 1677550a7375SFelipe Balbi } 1678550a7375SFelipe Balbi spin_unlock_irqrestore(&musb->lock, flags); 167993e098a8SJohn Stultz 1680550a7375SFelipe Balbi return 0; 1681550a7375SFelipe Balbi } 1682550a7375SFelipe Balbi 168326b8aa45SRobert Baldyga #ifdef CONFIG_BLACKFIN 168426b8aa45SRobert Baldyga static struct usb_ep *musb_match_ep(struct usb_gadget *g, 168526b8aa45SRobert Baldyga struct usb_endpoint_descriptor *desc, 168626b8aa45SRobert Baldyga struct usb_ss_ep_comp_descriptor *ep_comp) 168726b8aa45SRobert Baldyga { 168826b8aa45SRobert Baldyga struct usb_ep *ep = NULL; 168926b8aa45SRobert Baldyga 169026b8aa45SRobert Baldyga switch (usb_endpoint_type(desc)) { 169126b8aa45SRobert Baldyga case USB_ENDPOINT_XFER_ISOC: 169226b8aa45SRobert Baldyga case USB_ENDPOINT_XFER_BULK: 169326b8aa45SRobert Baldyga if (usb_endpoint_dir_in(desc)) 169426b8aa45SRobert Baldyga ep = gadget_find_ep_by_name(g, "ep5in"); 169526b8aa45SRobert Baldyga else 169626b8aa45SRobert Baldyga ep = gadget_find_ep_by_name(g, "ep6out"); 169726b8aa45SRobert Baldyga break; 169826b8aa45SRobert Baldyga case USB_ENDPOINT_XFER_INT: 169926b8aa45SRobert Baldyga if (usb_endpoint_dir_in(desc)) 170026b8aa45SRobert Baldyga ep = gadget_find_ep_by_name(g, "ep1in"); 170126b8aa45SRobert Baldyga else 170226b8aa45SRobert Baldyga ep = gadget_find_ep_by_name(g, "ep2out"); 170326b8aa45SRobert Baldyga break; 170426b8aa45SRobert Baldyga default: 17052f3cc24fSRobert Baldyga break; 170626b8aa45SRobert Baldyga } 170726b8aa45SRobert Baldyga 170826b8aa45SRobert Baldyga if (ep && usb_gadget_ep_match_desc(g, ep, desc, ep_comp)) 170926b8aa45SRobert Baldyga return ep; 171026b8aa45SRobert Baldyga 171126b8aa45SRobert Baldyga return NULL; 171226b8aa45SRobert Baldyga } 171326b8aa45SRobert Baldyga #else 171426b8aa45SRobert Baldyga #define musb_match_ep NULL 171526b8aa45SRobert Baldyga #endif 171626b8aa45SRobert Baldyga 1717e71eb392SSebastian Andrzej Siewior static int musb_gadget_start(struct usb_gadget *g, 1718e71eb392SSebastian Andrzej Siewior struct usb_gadget_driver *driver); 171922835b80SFelipe Balbi static int musb_gadget_stop(struct usb_gadget *g); 17200f91349bSSebastian Andrzej Siewior 1721550a7375SFelipe Balbi static const struct usb_gadget_ops musb_gadget_operations = { 1722550a7375SFelipe Balbi .get_frame = musb_gadget_get_frame, 1723550a7375SFelipe Balbi .wakeup = musb_gadget_wakeup, 1724550a7375SFelipe Balbi .set_selfpowered = musb_gadget_set_self_powered, 1725550a7375SFelipe Balbi /* .vbus_session = musb_gadget_vbus_session, */ 1726550a7375SFelipe Balbi .vbus_draw = musb_gadget_vbus_draw, 1727550a7375SFelipe Balbi .pullup = musb_gadget_pullup, 1728e71eb392SSebastian Andrzej Siewior .udc_start = musb_gadget_start, 1729e71eb392SSebastian Andrzej Siewior .udc_stop = musb_gadget_stop, 173026b8aa45SRobert Baldyga .match_ep = musb_match_ep, 1731550a7375SFelipe Balbi }; 1732550a7375SFelipe Balbi 1733550a7375SFelipe Balbi /* ----------------------------------------------------------------------- */ 1734550a7375SFelipe Balbi 1735550a7375SFelipe Balbi /* Registration */ 1736550a7375SFelipe Balbi 1737550a7375SFelipe Balbi /* Only this registration code "knows" the rule (from USB standards) 1738550a7375SFelipe Balbi * about there being only one external upstream port. It assumes 1739550a7375SFelipe Balbi * all peripheral ports are external... 1740550a7375SFelipe Balbi */ 1741550a7375SFelipe Balbi 174241ac7b3aSBill Pemberton static void 1743550a7375SFelipe Balbi init_peripheral_ep(struct musb *musb, struct musb_ep *ep, u8 epnum, int is_in) 1744550a7375SFelipe Balbi { 1745550a7375SFelipe Balbi struct musb_hw_ep *hw_ep = musb->endpoints + epnum; 1746550a7375SFelipe Balbi 1747550a7375SFelipe Balbi memset(ep, 0, sizeof *ep); 1748550a7375SFelipe Balbi 1749550a7375SFelipe Balbi ep->current_epnum = epnum; 1750550a7375SFelipe Balbi ep->musb = musb; 1751550a7375SFelipe Balbi ep->hw_ep = hw_ep; 1752550a7375SFelipe Balbi ep->is_in = is_in; 1753550a7375SFelipe Balbi 1754550a7375SFelipe Balbi INIT_LIST_HEAD(&ep->req_list); 1755550a7375SFelipe Balbi 1756550a7375SFelipe Balbi sprintf(ep->name, "ep%d%s", epnum, 1757550a7375SFelipe Balbi (!epnum || hw_ep->is_shared_fifo) ? "" : ( 1758550a7375SFelipe Balbi is_in ? "in" : "out")); 1759550a7375SFelipe Balbi ep->end_point.name = ep->name; 1760550a7375SFelipe Balbi INIT_LIST_HEAD(&ep->end_point.ep_list); 1761550a7375SFelipe Balbi if (!epnum) { 1762e117e742SRobert Baldyga usb_ep_set_maxpacket_limit(&ep->end_point, 64); 17638501955eSRobert Baldyga ep->end_point.caps.type_control = true; 1764550a7375SFelipe Balbi ep->end_point.ops = &musb_g_ep0_ops; 1765550a7375SFelipe Balbi musb->g.ep0 = &ep->end_point; 1766550a7375SFelipe Balbi } else { 1767550a7375SFelipe Balbi if (is_in) 1768e117e742SRobert Baldyga usb_ep_set_maxpacket_limit(&ep->end_point, hw_ep->max_packet_sz_tx); 1769550a7375SFelipe Balbi else 1770e117e742SRobert Baldyga usb_ep_set_maxpacket_limit(&ep->end_point, hw_ep->max_packet_sz_rx); 17718501955eSRobert Baldyga ep->end_point.caps.type_iso = true; 17728501955eSRobert Baldyga ep->end_point.caps.type_bulk = true; 17738501955eSRobert Baldyga ep->end_point.caps.type_int = true; 1774550a7375SFelipe Balbi ep->end_point.ops = &musb_ep_ops; 1775550a7375SFelipe Balbi list_add_tail(&ep->end_point.ep_list, &musb->g.ep_list); 1776550a7375SFelipe Balbi } 17778501955eSRobert Baldyga 17788501955eSRobert Baldyga if (!epnum || hw_ep->is_shared_fifo) { 17798501955eSRobert Baldyga ep->end_point.caps.dir_in = true; 17808501955eSRobert Baldyga ep->end_point.caps.dir_out = true; 17818501955eSRobert Baldyga } else if (is_in) 17828501955eSRobert Baldyga ep->end_point.caps.dir_in = true; 17838501955eSRobert Baldyga else 17848501955eSRobert Baldyga ep->end_point.caps.dir_out = true; 1785550a7375SFelipe Balbi } 1786550a7375SFelipe Balbi 1787550a7375SFelipe Balbi /* 1788550a7375SFelipe Balbi * Initialize the endpoints exposed to peripheral drivers, with backlinks 1789550a7375SFelipe Balbi * to the rest of the driver state. 1790550a7375SFelipe Balbi */ 179141ac7b3aSBill Pemberton static inline void musb_g_init_endpoints(struct musb *musb) 1792550a7375SFelipe Balbi { 1793550a7375SFelipe Balbi u8 epnum; 1794550a7375SFelipe Balbi struct musb_hw_ep *hw_ep; 1795550a7375SFelipe Balbi unsigned count = 0; 1796550a7375SFelipe Balbi 1797b595076aSUwe Kleine-König /* initialize endpoint list just once */ 1798550a7375SFelipe Balbi INIT_LIST_HEAD(&(musb->g.ep_list)); 1799550a7375SFelipe Balbi 1800550a7375SFelipe Balbi for (epnum = 0, hw_ep = musb->endpoints; 1801550a7375SFelipe Balbi epnum < musb->nr_endpoints; 1802550a7375SFelipe Balbi epnum++, hw_ep++) { 1803550a7375SFelipe Balbi if (hw_ep->is_shared_fifo /* || !epnum */) { 1804550a7375SFelipe Balbi init_peripheral_ep(musb, &hw_ep->ep_in, epnum, 0); 1805550a7375SFelipe Balbi count++; 1806550a7375SFelipe Balbi } else { 1807550a7375SFelipe Balbi if (hw_ep->max_packet_sz_tx) { 1808550a7375SFelipe Balbi init_peripheral_ep(musb, &hw_ep->ep_in, 1809550a7375SFelipe Balbi epnum, 1); 1810550a7375SFelipe Balbi count++; 1811550a7375SFelipe Balbi } 1812550a7375SFelipe Balbi if (hw_ep->max_packet_sz_rx) { 1813550a7375SFelipe Balbi init_peripheral_ep(musb, &hw_ep->ep_out, 1814550a7375SFelipe Balbi epnum, 0); 1815550a7375SFelipe Balbi count++; 1816550a7375SFelipe Balbi } 1817550a7375SFelipe Balbi } 1818550a7375SFelipe Balbi } 1819550a7375SFelipe Balbi } 1820550a7375SFelipe Balbi 1821550a7375SFelipe Balbi /* called once during driver setup to initialize and link into 1822550a7375SFelipe Balbi * the driver model; memory is zeroed. 1823550a7375SFelipe Balbi */ 182441ac7b3aSBill Pemberton int musb_gadget_setup(struct musb *musb) 1825550a7375SFelipe Balbi { 1826550a7375SFelipe Balbi int status; 1827550a7375SFelipe Balbi 1828550a7375SFelipe Balbi /* REVISIT minor race: if (erroneously) setting up two 1829550a7375SFelipe Balbi * musb peripherals at the same time, only the bus lock 1830550a7375SFelipe Balbi * is probably held. 1831550a7375SFelipe Balbi */ 1832550a7375SFelipe Balbi 1833550a7375SFelipe Balbi musb->g.ops = &musb_gadget_operations; 1834d327ab5bSMichal Nazarewicz musb->g.max_speed = USB_SPEED_HIGH; 1835550a7375SFelipe Balbi musb->g.speed = USB_SPEED_UNKNOWN; 1836550a7375SFelipe Balbi 18371374a430SBin Liu MUSB_DEV_MODE(musb); 18381374a430SBin Liu musb->xceiv->otg->default_a = 0; 1839e47d9254SAntoine Tenart musb->xceiv->otg->state = OTG_STATE_B_IDLE; 18401374a430SBin Liu 1841550a7375SFelipe Balbi /* this "gadget" abstracts/virtualizes the controller */ 1842550a7375SFelipe Balbi musb->g.name = musb_driver_name; 1843fd3923a9SApelete Seketeli #if IS_ENABLED(CONFIG_USB_MUSB_DUAL_ROLE) 1844550a7375SFelipe Balbi musb->g.is_otg = 1; 1845fd3923a9SApelete Seketeli #elif IS_ENABLED(CONFIG_USB_MUSB_GADGET) 1846fd3923a9SApelete Seketeli musb->g.is_otg = 0; 1847fd3923a9SApelete Seketeli #endif 1848517bafffSTony Lindgren INIT_DELAYED_WORK(&musb->gadget_work, musb_gadget_work); 1849550a7375SFelipe Balbi musb_g_init_endpoints(musb); 1850550a7375SFelipe Balbi 1851550a7375SFelipe Balbi musb->is_active = 0; 1852550a7375SFelipe Balbi musb_platform_try_idle(musb, 0); 1853550a7375SFelipe Balbi 18540f91349bSSebastian Andrzej Siewior status = usb_add_gadget_udc(musb->controller, &musb->g); 18550f91349bSSebastian Andrzej Siewior if (status) 18560f91349bSSebastian Andrzej Siewior goto err; 18570f91349bSSebastian Andrzej Siewior 18580f91349bSSebastian Andrzej Siewior return 0; 18590f91349bSSebastian Andrzej Siewior err: 18606193d699SSebastian Andrzej Siewior musb->g.dev.parent = NULL; 18610f91349bSSebastian Andrzej Siewior device_unregister(&musb->g.dev); 1862550a7375SFelipe Balbi return status; 1863550a7375SFelipe Balbi } 1864550a7375SFelipe Balbi 1865550a7375SFelipe Balbi void musb_gadget_cleanup(struct musb *musb) 1866550a7375SFelipe Balbi { 186790474288SSebastian Andrzej Siewior if (musb->port_mode == MUSB_PORT_MODE_HOST) 186890474288SSebastian Andrzej Siewior return; 1869517bafffSTony Lindgren 1870517bafffSTony Lindgren cancel_delayed_work_sync(&musb->gadget_work); 18710f91349bSSebastian Andrzej Siewior usb_del_gadget_udc(&musb->g); 1872550a7375SFelipe Balbi } 1873550a7375SFelipe Balbi 1874550a7375SFelipe Balbi /* 1875550a7375SFelipe Balbi * Register the gadget driver. Used by gadget drivers when 1876550a7375SFelipe Balbi * registering themselves with the controller. 1877550a7375SFelipe Balbi * 1878550a7375SFelipe Balbi * -EINVAL something went wrong (not driver) 1879550a7375SFelipe Balbi * -EBUSY another gadget is already using the controller 1880b595076aSUwe Kleine-König * -ENOMEM no memory to perform the operation 1881550a7375SFelipe Balbi * 1882550a7375SFelipe Balbi * @param driver the gadget driver 1883550a7375SFelipe Balbi * @return <0 if error, 0 if everything is fine 1884550a7375SFelipe Balbi */ 1885e71eb392SSebastian Andrzej Siewior static int musb_gadget_start(struct usb_gadget *g, 1886e71eb392SSebastian Andrzej Siewior struct usb_gadget_driver *driver) 1887550a7375SFelipe Balbi { 1888e71eb392SSebastian Andrzej Siewior struct musb *musb = gadget_to_musb(g); 1889d445b6daSHeikki Krogerus struct usb_otg *otg = musb->xceiv->otg; 189063eed2b5SFelipe Balbi unsigned long flags; 1891032ec49fSFelipe Balbi int retval = 0; 1892550a7375SFelipe Balbi 1893032ec49fSFelipe Balbi if (driver->max_speed < USB_SPEED_HIGH) { 1894032ec49fSFelipe Balbi retval = -EINVAL; 1895032ec49fSFelipe Balbi goto err; 1896032ec49fSFelipe Balbi } 1897550a7375SFelipe Balbi 18987acc6197SHema HK pm_runtime_get_sync(musb->controller); 18997acc6197SHema HK 1900e71eb392SSebastian Andrzej Siewior musb->softconnect = 0; 1901550a7375SFelipe Balbi musb->gadget_driver = driver; 1902550a7375SFelipe Balbi 1903550a7375SFelipe Balbi spin_lock_irqsave(&musb->lock, flags); 190443e699ceSGreg Kroah-Hartman musb->is_active = 1; 1905550a7375SFelipe Balbi 19066e13c650SHeikki Krogerus otg_set_peripheral(otg, &musb->g); 1907e47d9254SAntoine Tenart musb->xceiv->otg->state = OTG_STATE_B_IDLE; 1908550a7375SFelipe Balbi spin_unlock_irqrestore(&musb->lock, flags); 1909550a7375SFelipe Balbi 1910001dd84aSSebastian Andrzej Siewior musb_start(musb); 1911001dd84aSSebastian Andrzej Siewior 1912550a7375SFelipe Balbi /* REVISIT: funcall to other code, which also 1913550a7375SFelipe Balbi * handles power budgeting ... this way also 1914550a7375SFelipe Balbi * ensures HdrcStart is indirectly called. 1915550a7375SFelipe Balbi */ 1916b65ae0f1SGrazvydas Ignotas if (musb->xceiv->last_event == USB_EVENT_ID) 1917b65ae0f1SGrazvydas Ignotas musb_platform_set_vbus(musb, 1); 1918550a7375SFelipe Balbi 191930647217STony Lindgren pm_runtime_mark_last_busy(musb->controller); 192030647217STony Lindgren pm_runtime_put_autosuspend(musb->controller); 19217acc6197SHema HK 192263eed2b5SFelipe Balbi return 0; 192363eed2b5SFelipe Balbi 1924032ec49fSFelipe Balbi err: 1925550a7375SFelipe Balbi return retval; 1926550a7375SFelipe Balbi } 1927550a7375SFelipe Balbi 1928550a7375SFelipe Balbi /* 1929550a7375SFelipe Balbi * Unregister the gadget driver. Used by gadget drivers when 1930550a7375SFelipe Balbi * unregistering themselves from the controller. 1931550a7375SFelipe Balbi * 1932550a7375SFelipe Balbi * @param driver the gadget driver to unregister 1933550a7375SFelipe Balbi */ 193422835b80SFelipe Balbi static int musb_gadget_stop(struct usb_gadget *g) 1935550a7375SFelipe Balbi { 1936e71eb392SSebastian Andrzej Siewior struct musb *musb = gadget_to_musb(g); 193763eed2b5SFelipe Balbi unsigned long flags; 1938550a7375SFelipe Balbi 19397acc6197SHema HK pm_runtime_get_sync(musb->controller); 19407acc6197SHema HK 194163eed2b5SFelipe Balbi /* 194263eed2b5SFelipe Balbi * REVISIT always use otg_set_peripheral() here too; 1943550a7375SFelipe Balbi * this needs to shut down the OTG engine. 1944550a7375SFelipe Balbi */ 1945550a7375SFelipe Balbi 1946550a7375SFelipe Balbi spin_lock_irqsave(&musb->lock, flags); 1947550a7375SFelipe Balbi 1948550a7375SFelipe Balbi musb_hnp_stop(musb); 1949550a7375SFelipe Balbi 1950550a7375SFelipe Balbi (void) musb_gadget_vbus_draw(&musb->g, 0); 1951550a7375SFelipe Balbi 1952e47d9254SAntoine Tenart musb->xceiv->otg->state = OTG_STATE_UNDEFINED; 1953d5638fcfSFelipe Balbi musb_stop(musb); 19546e13c650SHeikki Krogerus otg_set_peripheral(musb->xceiv->otg, NULL); 1955550a7375SFelipe Balbi 1956550a7375SFelipe Balbi musb->is_active = 0; 1957e21de10cSGrazvydas Ignotas musb->gadget_driver = NULL; 1958550a7375SFelipe Balbi musb_platform_try_idle(musb, 0); 1959550a7375SFelipe Balbi spin_unlock_irqrestore(&musb->lock, flags); 1960550a7375SFelipe Balbi 1961032ec49fSFelipe Balbi /* 1962032ec49fSFelipe Balbi * FIXME we need to be able to register another 1963550a7375SFelipe Balbi * gadget driver here and have everything work; 1964550a7375SFelipe Balbi * that currently misbehaves. 1965550a7375SFelipe Balbi */ 196663eed2b5SFelipe Balbi 1967*4e719183STony Lindgren /* Force check of devctl register for PM runtime */ 1968*4e719183STony Lindgren schedule_work(&musb->irq_work); 1969*4e719183STony Lindgren 19707099dbc5STony Lindgren pm_runtime_mark_last_busy(musb->controller); 19717099dbc5STony Lindgren pm_runtime_put_autosuspend(musb->controller); 19727acc6197SHema HK 197363eed2b5SFelipe Balbi return 0; 1974550a7375SFelipe Balbi } 1975550a7375SFelipe Balbi 1976550a7375SFelipe Balbi /* ----------------------------------------------------------------------- */ 1977550a7375SFelipe Balbi 1978550a7375SFelipe Balbi /* lifecycle operations called through plat_uds.c */ 1979550a7375SFelipe Balbi 1980550a7375SFelipe Balbi void musb_g_resume(struct musb *musb) 1981550a7375SFelipe Balbi { 1982550a7375SFelipe Balbi musb->is_suspended = 0; 1983e47d9254SAntoine Tenart switch (musb->xceiv->otg->state) { 1984550a7375SFelipe Balbi case OTG_STATE_B_IDLE: 1985550a7375SFelipe Balbi break; 1986550a7375SFelipe Balbi case OTG_STATE_B_WAIT_ACON: 1987550a7375SFelipe Balbi case OTG_STATE_B_PERIPHERAL: 1988550a7375SFelipe Balbi musb->is_active = 1; 1989550a7375SFelipe Balbi if (musb->gadget_driver && musb->gadget_driver->resume) { 1990550a7375SFelipe Balbi spin_unlock(&musb->lock); 1991550a7375SFelipe Balbi musb->gadget_driver->resume(&musb->g); 1992550a7375SFelipe Balbi spin_lock(&musb->lock); 1993550a7375SFelipe Balbi } 1994550a7375SFelipe Balbi break; 1995550a7375SFelipe Balbi default: 1996550a7375SFelipe Balbi WARNING("unhandled RESUME transition (%s)\n", 1997e47d9254SAntoine Tenart usb_otg_state_string(musb->xceiv->otg->state)); 1998550a7375SFelipe Balbi } 1999550a7375SFelipe Balbi } 2000550a7375SFelipe Balbi 2001550a7375SFelipe Balbi /* called when SOF packets stop for 3+ msec */ 2002550a7375SFelipe Balbi void musb_g_suspend(struct musb *musb) 2003550a7375SFelipe Balbi { 2004550a7375SFelipe Balbi u8 devctl; 2005550a7375SFelipe Balbi 2006550a7375SFelipe Balbi devctl = musb_readb(musb->mregs, MUSB_DEVCTL); 2007b99d3659SBin Liu musb_dbg(musb, "musb_g_suspend: devctl %02x", devctl); 2008550a7375SFelipe Balbi 2009e47d9254SAntoine Tenart switch (musb->xceiv->otg->state) { 2010550a7375SFelipe Balbi case OTG_STATE_B_IDLE: 2011550a7375SFelipe Balbi if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS) 2012e47d9254SAntoine Tenart musb->xceiv->otg->state = OTG_STATE_B_PERIPHERAL; 2013550a7375SFelipe Balbi break; 2014550a7375SFelipe Balbi case OTG_STATE_B_PERIPHERAL: 2015550a7375SFelipe Balbi musb->is_suspended = 1; 2016550a7375SFelipe Balbi if (musb->gadget_driver && musb->gadget_driver->suspend) { 2017550a7375SFelipe Balbi spin_unlock(&musb->lock); 2018550a7375SFelipe Balbi musb->gadget_driver->suspend(&musb->g); 2019550a7375SFelipe Balbi spin_lock(&musb->lock); 2020550a7375SFelipe Balbi } 2021550a7375SFelipe Balbi break; 2022550a7375SFelipe Balbi default: 2023550a7375SFelipe Balbi /* REVISIT if B_HOST, clear DEVCTL.HOSTREQ; 2024550a7375SFelipe Balbi * A_PERIPHERAL may need care too 2025550a7375SFelipe Balbi */ 2026b99d3659SBin Liu WARNING("unhandled SUSPEND transition (%s)", 2027e47d9254SAntoine Tenart usb_otg_state_string(musb->xceiv->otg->state)); 2028550a7375SFelipe Balbi } 2029550a7375SFelipe Balbi } 2030550a7375SFelipe Balbi 2031550a7375SFelipe Balbi /* Called during SRP */ 2032550a7375SFelipe Balbi void musb_g_wakeup(struct musb *musb) 2033550a7375SFelipe Balbi { 2034550a7375SFelipe Balbi musb_gadget_wakeup(&musb->g); 2035550a7375SFelipe Balbi } 2036550a7375SFelipe Balbi 2037550a7375SFelipe Balbi /* called when VBUS drops below session threshold, and in other cases */ 2038550a7375SFelipe Balbi void musb_g_disconnect(struct musb *musb) 2039550a7375SFelipe Balbi { 2040550a7375SFelipe Balbi void __iomem *mregs = musb->mregs; 2041550a7375SFelipe Balbi u8 devctl = musb_readb(mregs, MUSB_DEVCTL); 2042550a7375SFelipe Balbi 2043b99d3659SBin Liu musb_dbg(musb, "musb_g_disconnect: devctl %02x", devctl); 2044550a7375SFelipe Balbi 2045550a7375SFelipe Balbi /* clear HR */ 2046550a7375SFelipe Balbi musb_writeb(mregs, MUSB_DEVCTL, devctl & MUSB_DEVCTL_SESSION); 2047550a7375SFelipe Balbi 2048550a7375SFelipe Balbi /* don't draw vbus until new b-default session */ 2049550a7375SFelipe Balbi (void) musb_gadget_vbus_draw(&musb->g, 0); 2050550a7375SFelipe Balbi 2051550a7375SFelipe Balbi musb->g.speed = USB_SPEED_UNKNOWN; 2052550a7375SFelipe Balbi if (musb->gadget_driver && musb->gadget_driver->disconnect) { 2053550a7375SFelipe Balbi spin_unlock(&musb->lock); 2054550a7375SFelipe Balbi musb->gadget_driver->disconnect(&musb->g); 2055550a7375SFelipe Balbi spin_lock(&musb->lock); 2056550a7375SFelipe Balbi } 2057550a7375SFelipe Balbi 2058e47d9254SAntoine Tenart switch (musb->xceiv->otg->state) { 2059550a7375SFelipe Balbi default: 2060b99d3659SBin Liu musb_dbg(musb, "Unhandled disconnect %s, setting a_idle", 2061e47d9254SAntoine Tenart usb_otg_state_string(musb->xceiv->otg->state)); 2062e47d9254SAntoine Tenart musb->xceiv->otg->state = OTG_STATE_A_IDLE; 2063ab983f2aSDavid Brownell MUSB_HST_MODE(musb); 2064550a7375SFelipe Balbi break; 2065550a7375SFelipe Balbi case OTG_STATE_A_PERIPHERAL: 2066e47d9254SAntoine Tenart musb->xceiv->otg->state = OTG_STATE_A_WAIT_BCON; 2067ab983f2aSDavid Brownell MUSB_HST_MODE(musb); 2068550a7375SFelipe Balbi break; 2069550a7375SFelipe Balbi case OTG_STATE_B_WAIT_ACON: 2070550a7375SFelipe Balbi case OTG_STATE_B_HOST: 2071550a7375SFelipe Balbi case OTG_STATE_B_PERIPHERAL: 2072550a7375SFelipe Balbi case OTG_STATE_B_IDLE: 2073e47d9254SAntoine Tenart musb->xceiv->otg->state = OTG_STATE_B_IDLE; 2074550a7375SFelipe Balbi break; 2075550a7375SFelipe Balbi case OTG_STATE_B_SRP_INIT: 2076550a7375SFelipe Balbi break; 2077550a7375SFelipe Balbi } 2078550a7375SFelipe Balbi 2079550a7375SFelipe Balbi musb->is_active = 0; 2080550a7375SFelipe Balbi } 2081550a7375SFelipe Balbi 2082550a7375SFelipe Balbi void musb_g_reset(struct musb *musb) 2083550a7375SFelipe Balbi __releases(musb->lock) 2084550a7375SFelipe Balbi __acquires(musb->lock) 2085550a7375SFelipe Balbi { 2086550a7375SFelipe Balbi void __iomem *mbase = musb->mregs; 2087550a7375SFelipe Balbi u8 devctl = musb_readb(mbase, MUSB_DEVCTL); 2088550a7375SFelipe Balbi u8 power; 2089550a7375SFelipe Balbi 2090b99d3659SBin Liu musb_dbg(musb, "<== %s driver '%s'", 2091550a7375SFelipe Balbi (devctl & MUSB_DEVCTL_BDEVICE) 2092550a7375SFelipe Balbi ? "B-Device" : "A-Device", 2093550a7375SFelipe Balbi musb->gadget_driver 2094550a7375SFelipe Balbi ? musb->gadget_driver->driver.name 2095550a7375SFelipe Balbi : NULL 2096550a7375SFelipe Balbi ); 2097550a7375SFelipe Balbi 20981189f7f6SFelipe Balbi /* report reset, if we didn't already (flushing EP state) */ 20991189f7f6SFelipe Balbi if (musb->gadget_driver && musb->g.speed != USB_SPEED_UNKNOWN) { 21001189f7f6SFelipe Balbi spin_unlock(&musb->lock); 21011189f7f6SFelipe Balbi usb_gadget_udc_reset(&musb->g, musb->gadget_driver); 21021189f7f6SFelipe Balbi spin_lock(&musb->lock); 21031189f7f6SFelipe Balbi } 2104550a7375SFelipe Balbi 2105550a7375SFelipe Balbi /* clear HR */ 2106550a7375SFelipe Balbi else if (devctl & MUSB_DEVCTL_HR) 2107550a7375SFelipe Balbi musb_writeb(mbase, MUSB_DEVCTL, MUSB_DEVCTL_SESSION); 2108550a7375SFelipe Balbi 2109550a7375SFelipe Balbi 2110550a7375SFelipe Balbi /* what speed did we negotiate? */ 2111550a7375SFelipe Balbi power = musb_readb(mbase, MUSB_POWER); 2112550a7375SFelipe Balbi musb->g.speed = (power & MUSB_POWER_HSMODE) 2113550a7375SFelipe Balbi ? USB_SPEED_HIGH : USB_SPEED_FULL; 2114550a7375SFelipe Balbi 2115550a7375SFelipe Balbi /* start in USB_STATE_DEFAULT */ 2116550a7375SFelipe Balbi musb->is_active = 1; 2117550a7375SFelipe Balbi musb->is_suspended = 0; 2118550a7375SFelipe Balbi MUSB_DEV_MODE(musb); 2119550a7375SFelipe Balbi musb->address = 0; 2120550a7375SFelipe Balbi musb->ep0_state = MUSB_EP0_STAGE_SETUP; 2121550a7375SFelipe Balbi 2122550a7375SFelipe Balbi musb->may_wakeup = 0; 2123550a7375SFelipe Balbi musb->g.b_hnp_enable = 0; 2124550a7375SFelipe Balbi musb->g.a_alt_hnp_support = 0; 2125550a7375SFelipe Balbi musb->g.a_hnp_support = 0; 2126ca1023c8SRobert Baldyga musb->g.quirk_zlp_not_supp = 1; 2127550a7375SFelipe Balbi 2128550a7375SFelipe Balbi /* Normal reset, as B-Device; 2129550a7375SFelipe Balbi * or else after HNP, as A-Device 2130550a7375SFelipe Balbi */ 213123db9fd2SApelete Seketeli if (!musb->g.is_otg) { 213223db9fd2SApelete Seketeli /* USB device controllers that are not OTG compatible 213323db9fd2SApelete Seketeli * may not have DEVCTL register in silicon. 213423db9fd2SApelete Seketeli * In that case, do not rely on devctl for setting 213523db9fd2SApelete Seketeli * peripheral mode. 213623db9fd2SApelete Seketeli */ 2137e47d9254SAntoine Tenart musb->xceiv->otg->state = OTG_STATE_B_PERIPHERAL; 213823db9fd2SApelete Seketeli musb->g.is_a_peripheral = 0; 213923db9fd2SApelete Seketeli } else if (devctl & MUSB_DEVCTL_BDEVICE) { 2140e47d9254SAntoine Tenart musb->xceiv->otg->state = OTG_STATE_B_PERIPHERAL; 2141550a7375SFelipe Balbi musb->g.is_a_peripheral = 0; 2142032ec49fSFelipe Balbi } else { 2143e47d9254SAntoine Tenart musb->xceiv->otg->state = OTG_STATE_A_PERIPHERAL; 2144550a7375SFelipe Balbi musb->g.is_a_peripheral = 1; 2145032ec49fSFelipe Balbi } 2146550a7375SFelipe Balbi 2147550a7375SFelipe Balbi /* start with default limits on VBUS power draw */ 2148032ec49fSFelipe Balbi (void) musb_gadget_vbus_draw(&musb->g, 8); 2149550a7375SFelipe Balbi } 2150