15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 2550a7375SFelipe Balbi /* 3550a7375SFelipe Balbi * MUSB OTG driver peripheral support 4550a7375SFelipe Balbi * 5550a7375SFelipe Balbi * Copyright 2005 Mentor Graphics Corporation 6550a7375SFelipe Balbi * Copyright (C) 2005-2006 by Texas Instruments 7550a7375SFelipe Balbi * Copyright (C) 2006-2007 Nokia Corporation 8cea83241SSergei Shtylyov * Copyright (C) 2009 MontaVista Software, Inc. <source@mvista.com> 9550a7375SFelipe Balbi */ 10550a7375SFelipe Balbi 11550a7375SFelipe Balbi #include <linux/kernel.h> 12550a7375SFelipe Balbi #include <linux/list.h> 13550a7375SFelipe Balbi #include <linux/timer.h> 14550a7375SFelipe Balbi #include <linux/module.h> 15550a7375SFelipe Balbi #include <linux/smp.h> 16550a7375SFelipe Balbi #include <linux/spinlock.h> 17550a7375SFelipe Balbi #include <linux/delay.h> 18550a7375SFelipe Balbi #include <linux/dma-mapping.h> 195a0e3ad6STejun Heo #include <linux/slab.h> 20550a7375SFelipe Balbi 21550a7375SFelipe Balbi #include "musb_core.h" 22fc78003eSBin Liu #include "musb_trace.h" 23550a7375SFelipe Balbi 24550a7375SFelipe Balbi 25550a7375SFelipe Balbi /* ----------------------------------------------------------------------- */ 26550a7375SFelipe Balbi 27c65bfa62SMian Yousaf Kaukab #define is_buffer_mapped(req) (is_dma_capable() && \ 28c65bfa62SMian Yousaf Kaukab (req->map_state != UN_MAPPED)) 29c65bfa62SMian Yousaf Kaukab 3092d2711fSHema Kalliguddi /* Maps the buffer to dma */ 3192d2711fSHema Kalliguddi 3292d2711fSHema Kalliguddi static inline void map_dma_buffer(struct musb_request *request, 33c65bfa62SMian Yousaf Kaukab struct musb *musb, struct musb_ep *musb_ep) 3492d2711fSHema Kalliguddi { 355f5761cbSMian Yousaf Kaukab int compatible = true; 365f5761cbSMian Yousaf Kaukab struct dma_controller *dma = musb->dma_controller; 375f5761cbSMian Yousaf Kaukab 38c65bfa62SMian Yousaf Kaukab request->map_state = UN_MAPPED; 39c65bfa62SMian Yousaf Kaukab 40c65bfa62SMian Yousaf Kaukab if (!is_dma_capable() || !musb_ep->dma) 41c65bfa62SMian Yousaf Kaukab return; 42c65bfa62SMian Yousaf Kaukab 435f5761cbSMian Yousaf Kaukab /* Check if DMA engine can handle this request. 445f5761cbSMian Yousaf Kaukab * DMA code must reject the USB request explicitly. 455f5761cbSMian Yousaf Kaukab * Default behaviour is to map the request. 465f5761cbSMian Yousaf Kaukab */ 475f5761cbSMian Yousaf Kaukab if (dma->is_compatible) 485f5761cbSMian Yousaf Kaukab compatible = dma->is_compatible(musb_ep->dma, 495f5761cbSMian Yousaf Kaukab musb_ep->packet_sz, request->request.buf, 505f5761cbSMian Yousaf Kaukab request->request.length); 515f5761cbSMian Yousaf Kaukab if (!compatible) 525f5761cbSMian Yousaf Kaukab return; 535f5761cbSMian Yousaf Kaukab 5492d2711fSHema Kalliguddi if (request->request.dma == DMA_ADDR_INVALID) { 557b360f42SSebastian Andrzej Siewior dma_addr_t dma_addr; 567b360f42SSebastian Andrzej Siewior int ret; 577b360f42SSebastian Andrzej Siewior 587b360f42SSebastian Andrzej Siewior dma_addr = dma_map_single( 5992d2711fSHema Kalliguddi musb->controller, 6092d2711fSHema Kalliguddi request->request.buf, 6192d2711fSHema Kalliguddi request->request.length, 6292d2711fSHema Kalliguddi request->tx 6392d2711fSHema Kalliguddi ? DMA_TO_DEVICE 6492d2711fSHema Kalliguddi : DMA_FROM_DEVICE); 657b360f42SSebastian Andrzej Siewior ret = dma_mapping_error(musb->controller, dma_addr); 667b360f42SSebastian Andrzej Siewior if (ret) 677b360f42SSebastian Andrzej Siewior return; 687b360f42SSebastian Andrzej Siewior 697b360f42SSebastian Andrzej Siewior request->request.dma = dma_addr; 70c65bfa62SMian Yousaf Kaukab request->map_state = MUSB_MAPPED; 7192d2711fSHema Kalliguddi } else { 7292d2711fSHema Kalliguddi dma_sync_single_for_device(musb->controller, 7392d2711fSHema Kalliguddi request->request.dma, 7492d2711fSHema Kalliguddi request->request.length, 7592d2711fSHema Kalliguddi request->tx 7692d2711fSHema Kalliguddi ? DMA_TO_DEVICE 7792d2711fSHema Kalliguddi : DMA_FROM_DEVICE); 78c65bfa62SMian Yousaf Kaukab request->map_state = PRE_MAPPED; 7992d2711fSHema Kalliguddi } 8092d2711fSHema Kalliguddi } 8192d2711fSHema Kalliguddi 8292d2711fSHema Kalliguddi /* Unmap the buffer from dma and maps it back to cpu */ 8392d2711fSHema Kalliguddi static inline void unmap_dma_buffer(struct musb_request *request, 8492d2711fSHema Kalliguddi struct musb *musb) 8592d2711fSHema Kalliguddi { 8606d9db72SKishon Vijay Abraham I struct musb_ep *musb_ep = request->ep; 8706d9db72SKishon Vijay Abraham I 8806d9db72SKishon Vijay Abraham I if (!is_buffer_mapped(request) || !musb_ep->dma) 89c65bfa62SMian Yousaf Kaukab return; 90c65bfa62SMian Yousaf Kaukab 9192d2711fSHema Kalliguddi if (request->request.dma == DMA_ADDR_INVALID) { 925c8a86e1SFelipe Balbi dev_vdbg(musb->controller, 935c8a86e1SFelipe Balbi "not unmapping a never mapped buffer\n"); 9492d2711fSHema Kalliguddi return; 9592d2711fSHema Kalliguddi } 96c65bfa62SMian Yousaf Kaukab if (request->map_state == MUSB_MAPPED) { 9792d2711fSHema Kalliguddi dma_unmap_single(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); 10392d2711fSHema Kalliguddi request->request.dma = DMA_ADDR_INVALID; 104c65bfa62SMian Yousaf Kaukab } else { /* PRE_MAPPED */ 10592d2711fSHema Kalliguddi dma_sync_single_for_cpu(musb->controller, 10692d2711fSHema Kalliguddi request->request.dma, 10792d2711fSHema Kalliguddi request->request.length, 10892d2711fSHema Kalliguddi request->tx 10992d2711fSHema Kalliguddi ? DMA_TO_DEVICE 11092d2711fSHema Kalliguddi : DMA_FROM_DEVICE); 11192d2711fSHema Kalliguddi } 112c65bfa62SMian Yousaf Kaukab request->map_state = UN_MAPPED; 11392d2711fSHema Kalliguddi } 11492d2711fSHema Kalliguddi 115550a7375SFelipe Balbi /* 116550a7375SFelipe Balbi * Immediately complete a request. 117550a7375SFelipe Balbi * 118550a7375SFelipe Balbi * @param request the request to complete 119550a7375SFelipe Balbi * @param status the status to complete the request with 120550a7375SFelipe Balbi * Context: controller locked, IRQs blocked. 121550a7375SFelipe Balbi */ 122550a7375SFelipe Balbi void musb_g_giveback( 123550a7375SFelipe Balbi struct musb_ep *ep, 124550a7375SFelipe Balbi struct usb_request *request, 125550a7375SFelipe Balbi int status) 126550a7375SFelipe Balbi __releases(ep->musb->lock) 127550a7375SFelipe Balbi __acquires(ep->musb->lock) 128550a7375SFelipe Balbi { 129550a7375SFelipe Balbi struct musb_request *req; 130550a7375SFelipe Balbi struct musb *musb; 131550a7375SFelipe Balbi int busy = ep->busy; 132550a7375SFelipe Balbi 133550a7375SFelipe Balbi req = to_musb_request(request); 134550a7375SFelipe Balbi 135ad1adb89SFelipe Balbi list_del(&req->list); 136550a7375SFelipe Balbi if (req->request.status == -EINPROGRESS) 137550a7375SFelipe Balbi req->request.status = status; 138550a7375SFelipe Balbi musb = req->musb; 139550a7375SFelipe Balbi 140550a7375SFelipe Balbi ep->busy = 1; 141550a7375SFelipe Balbi spin_unlock(&musb->lock); 14206d9db72SKishon Vijay Abraham I 14306d9db72SKishon Vijay Abraham I if (!dma_mapping_error(&musb->g.dev, request->dma)) 14492d2711fSHema Kalliguddi unmap_dma_buffer(req, musb); 14506d9db72SKishon Vijay Abraham I 146fc78003eSBin Liu trace_musb_req_gb(req); 147304f7e5eSMichal Sojka usb_gadget_giveback_request(&req->ep->end_point, &req->request); 148550a7375SFelipe Balbi spin_lock(&musb->lock); 149550a7375SFelipe Balbi ep->busy = busy; 150550a7375SFelipe Balbi } 151550a7375SFelipe Balbi 152550a7375SFelipe Balbi /* ----------------------------------------------------------------------- */ 153550a7375SFelipe Balbi 154550a7375SFelipe Balbi /* 155550a7375SFelipe Balbi * Abort requests queued to an endpoint using the status. Synchronous. 156550a7375SFelipe Balbi * caller locked controller and blocked irqs, and selected this ep. 157550a7375SFelipe Balbi */ 158550a7375SFelipe Balbi static void nuke(struct musb_ep *ep, const int status) 159550a7375SFelipe Balbi { 1605c8a86e1SFelipe Balbi struct musb *musb = ep->musb; 161550a7375SFelipe Balbi struct musb_request *req = NULL; 162550a7375SFelipe Balbi void __iomem *epio = ep->musb->endpoints[ep->current_epnum].regs; 163550a7375SFelipe Balbi 164550a7375SFelipe Balbi ep->busy = 1; 165550a7375SFelipe Balbi 166550a7375SFelipe Balbi if (is_dma_capable() && ep->dma) { 167550a7375SFelipe Balbi struct dma_controller *c = ep->musb->dma_controller; 168550a7375SFelipe Balbi int value; 169b6e434a5SSergei Shtylyov 170550a7375SFelipe Balbi if (ep->is_in) { 171b6e434a5SSergei Shtylyov /* 172b6e434a5SSergei Shtylyov * The programming guide says that we must not clear 173b6e434a5SSergei Shtylyov * the DMAMODE bit before DMAENAB, so we only 174b6e434a5SSergei Shtylyov * clear it in the second write... 175b6e434a5SSergei Shtylyov */ 176550a7375SFelipe Balbi musb_writew(epio, MUSB_TXCSR, 177b6e434a5SSergei Shtylyov MUSB_TXCSR_DMAMODE | MUSB_TXCSR_FLUSHFIFO); 178550a7375SFelipe Balbi musb_writew(epio, MUSB_TXCSR, 179550a7375SFelipe Balbi 0 | MUSB_TXCSR_FLUSHFIFO); 180550a7375SFelipe Balbi } else { 181550a7375SFelipe Balbi musb_writew(epio, MUSB_RXCSR, 182550a7375SFelipe Balbi 0 | MUSB_RXCSR_FLUSHFIFO); 183550a7375SFelipe Balbi musb_writew(epio, MUSB_RXCSR, 184550a7375SFelipe Balbi 0 | MUSB_RXCSR_FLUSHFIFO); 185550a7375SFelipe Balbi } 186550a7375SFelipe Balbi 187550a7375SFelipe Balbi value = c->channel_abort(ep->dma); 188b99d3659SBin Liu musb_dbg(musb, "%s: abort DMA --> %d", ep->name, value); 189550a7375SFelipe Balbi c->channel_release(ep->dma); 190550a7375SFelipe Balbi ep->dma = NULL; 191550a7375SFelipe Balbi } 192550a7375SFelipe Balbi 193ad1adb89SFelipe Balbi while (!list_empty(&ep->req_list)) { 194ad1adb89SFelipe Balbi req = list_first_entry(&ep->req_list, struct musb_request, list); 195550a7375SFelipe Balbi musb_g_giveback(ep, &req->request, status); 196550a7375SFelipe Balbi } 197550a7375SFelipe Balbi } 198550a7375SFelipe Balbi 199550a7375SFelipe Balbi /* ----------------------------------------------------------------------- */ 200550a7375SFelipe Balbi 201550a7375SFelipe Balbi /* Data transfers - pure PIO, pure DMA, or mixed mode */ 202550a7375SFelipe Balbi 203550a7375SFelipe Balbi /* 204550a7375SFelipe Balbi * This assumes the separate CPPI engine is responding to DMA requests 205550a7375SFelipe Balbi * from the usb core ... sequenced a bit differently from mentor dma. 206550a7375SFelipe Balbi */ 207550a7375SFelipe Balbi 208550a7375SFelipe Balbi static inline int max_ep_writesize(struct musb *musb, struct musb_ep *ep) 209550a7375SFelipe Balbi { 210550a7375SFelipe Balbi if (can_bulk_split(musb, ep->type)) 211550a7375SFelipe Balbi return ep->hw_ep->max_packet_sz_tx; 212550a7375SFelipe Balbi else 213550a7375SFelipe Balbi return ep->packet_sz; 214550a7375SFelipe Balbi } 215550a7375SFelipe Balbi 216550a7375SFelipe Balbi /* 217550a7375SFelipe Balbi * An endpoint is transmitting data. This can be called either from 218550a7375SFelipe Balbi * the IRQ routine or from ep.queue() to kickstart a request on an 219550a7375SFelipe Balbi * endpoint. 220550a7375SFelipe Balbi * 221550a7375SFelipe Balbi * Context: controller locked, IRQs blocked, endpoint selected 222550a7375SFelipe Balbi */ 223550a7375SFelipe Balbi static void txstate(struct musb *musb, struct musb_request *req) 224550a7375SFelipe Balbi { 225550a7375SFelipe Balbi u8 epnum = req->epnum; 226550a7375SFelipe Balbi struct musb_ep *musb_ep; 227550a7375SFelipe Balbi void __iomem *epio = musb->endpoints[epnum].regs; 228550a7375SFelipe Balbi struct usb_request *request; 229550a7375SFelipe Balbi u16 fifo_count = 0, csr; 230550a7375SFelipe Balbi int use_dma = 0; 231550a7375SFelipe Balbi 232550a7375SFelipe Balbi musb_ep = req->ep; 233550a7375SFelipe Balbi 234abf710e6SVikram Pandita /* Check if EP is disabled */ 235abf710e6SVikram Pandita if (!musb_ep->desc) { 236b99d3659SBin Liu musb_dbg(musb, "ep:%s disabled - ignore request", 237abf710e6SVikram Pandita musb_ep->end_point.name); 238abf710e6SVikram Pandita return; 239abf710e6SVikram Pandita } 240abf710e6SVikram Pandita 241550a7375SFelipe Balbi /* we shouldn't get here while DMA is active ... but we do ... */ 242550a7375SFelipe Balbi if (dma_channel_status(musb_ep->dma) == MUSB_DMA_STATUS_BUSY) { 243b99d3659SBin Liu musb_dbg(musb, "dma pending..."); 244550a7375SFelipe Balbi return; 245550a7375SFelipe Balbi } 246550a7375SFelipe Balbi 247550a7375SFelipe Balbi /* read TXCSR before */ 248550a7375SFelipe Balbi csr = musb_readw(epio, MUSB_TXCSR); 249550a7375SFelipe Balbi 250550a7375SFelipe Balbi request = &req->request; 251550a7375SFelipe Balbi fifo_count = min(max_ep_writesize(musb, musb_ep), 252550a7375SFelipe Balbi (int)(request->length - request->actual)); 253550a7375SFelipe Balbi 254550a7375SFelipe Balbi if (csr & MUSB_TXCSR_TXPKTRDY) { 255b99d3659SBin Liu musb_dbg(musb, "%s old packet still ready , txcsr %03x", 256550a7375SFelipe Balbi musb_ep->end_point.name, csr); 257550a7375SFelipe Balbi return; 258550a7375SFelipe Balbi } 259550a7375SFelipe Balbi 260550a7375SFelipe Balbi if (csr & MUSB_TXCSR_P_SENDSTALL) { 261b99d3659SBin Liu musb_dbg(musb, "%s stalling, txcsr %03x", 262550a7375SFelipe Balbi musb_ep->end_point.name, csr); 263550a7375SFelipe Balbi return; 264550a7375SFelipe Balbi } 265550a7375SFelipe Balbi 266b99d3659SBin Liu musb_dbg(musb, "hw_ep%d, maxpacket %d, fifo count %d, txcsr %03x", 267550a7375SFelipe Balbi epnum, musb_ep->packet_sz, fifo_count, 268550a7375SFelipe Balbi csr); 269550a7375SFelipe Balbi 270550a7375SFelipe Balbi #ifndef CONFIG_MUSB_PIO_ONLY 271c65bfa62SMian Yousaf Kaukab if (is_buffer_mapped(req)) { 272550a7375SFelipe Balbi struct dma_controller *c = musb->dma_controller; 27366af83ddSMing Lei size_t request_size; 27466af83ddSMing Lei 27566af83ddSMing Lei /* setup DMA, then program endpoint CSR */ 27666af83ddSMing Lei request_size = min_t(size_t, request->length - request->actual, 27766af83ddSMing Lei musb_ep->dma->max_len); 278550a7375SFelipe Balbi 279d17d535fSAjay Kumar Gupta use_dma = (request->dma != DMA_ADDR_INVALID && request_size); 280550a7375SFelipe Balbi 281550a7375SFelipe Balbi /* MUSB_TXCSR_P_ISO is still set correctly */ 282550a7375SFelipe Balbi 28303840fadSFelipe Balbi if (musb_dma_inventra(musb) || musb_dma_ux500(musb)) { 284d1043a26SAnand Gadiyar if (request_size < musb_ep->packet_sz) 285550a7375SFelipe Balbi musb_ep->dma->desired_mode = 0; 286550a7375SFelipe Balbi else 287550a7375SFelipe Balbi musb_ep->dma->desired_mode = 1; 288550a7375SFelipe Balbi 289550a7375SFelipe Balbi use_dma = use_dma && c->channel_program( 290550a7375SFelipe Balbi musb_ep->dma, musb_ep->packet_sz, 291550a7375SFelipe Balbi musb_ep->dma->desired_mode, 292796a83faSCliff Cai request->dma + request->actual, request_size); 293550a7375SFelipe Balbi if (use_dma) { 294550a7375SFelipe Balbi if (musb_ep->dma->desired_mode == 0) { 295b6e434a5SSergei Shtylyov /* 296b6e434a5SSergei Shtylyov * We must not clear the DMAMODE bit 297b6e434a5SSergei Shtylyov * before the DMAENAB bit -- and the 298b6e434a5SSergei Shtylyov * latter doesn't always get cleared 299b6e434a5SSergei Shtylyov * before we get here... 300b6e434a5SSergei Shtylyov */ 301b6e434a5SSergei Shtylyov csr &= ~(MUSB_TXCSR_AUTOSET 302b6e434a5SSergei Shtylyov | MUSB_TXCSR_DMAENAB); 303b6e434a5SSergei Shtylyov musb_writew(epio, MUSB_TXCSR, csr 304b6e434a5SSergei Shtylyov | MUSB_TXCSR_P_WZC_BITS); 305b6e434a5SSergei Shtylyov csr &= ~MUSB_TXCSR_DMAMODE; 306550a7375SFelipe Balbi csr |= (MUSB_TXCSR_DMAENAB | 307550a7375SFelipe Balbi MUSB_TXCSR_MODE); 308550a7375SFelipe Balbi /* against programming guide */ 309f11d893dSMing Lei } else { 310f11d893dSMing Lei csr |= (MUSB_TXCSR_DMAENAB 311550a7375SFelipe Balbi | MUSB_TXCSR_DMAMODE 312550a7375SFelipe Balbi | MUSB_TXCSR_MODE); 313bb3a2ef2Ssupriya karanth /* 314bb3a2ef2Ssupriya karanth * Enable Autoset according to table 315bb3a2ef2Ssupriya karanth * below 316bb3a2ef2Ssupriya karanth * bulk_split hb_mult Autoset_Enable 317bb3a2ef2Ssupriya karanth * 0 0 Yes(Normal) 318bb3a2ef2Ssupriya karanth * 0 >0 No(High BW ISO) 319bb3a2ef2Ssupriya karanth * 1 0 Yes(HS bulk) 320bb3a2ef2Ssupriya karanth * 1 >0 Yes(FS bulk) 321bb3a2ef2Ssupriya karanth */ 322bb3a2ef2Ssupriya karanth if (!musb_ep->hb_mult || 323bb3a2ef2Ssupriya karanth can_bulk_split(musb, 3241a171626SGeyslan G. Bem musb_ep->type)) 325f11d893dSMing Lei csr |= MUSB_TXCSR_AUTOSET; 326f11d893dSMing Lei } 327550a7375SFelipe Balbi csr &= ~MUSB_TXCSR_P_UNDERRUN; 328f11d893dSMing Lei 329550a7375SFelipe Balbi musb_writew(epio, MUSB_TXCSR, csr); 330550a7375SFelipe Balbi } 331550a7375SFelipe Balbi } 332550a7375SFelipe Balbi 333f8e9f34fSTony Lindgren if (is_cppi_enabled(musb)) { 334550a7375SFelipe Balbi /* program endpoint CSR first, then setup DMA */ 335b6e434a5SSergei Shtylyov csr &= ~(MUSB_TXCSR_P_UNDERRUN | MUSB_TXCSR_TXPKTRDY); 33637e3ee99SSergei Shtylyov csr |= MUSB_TXCSR_DMAENAB | MUSB_TXCSR_DMAMODE | 33737e3ee99SSergei Shtylyov MUSB_TXCSR_MODE; 338fc525751SSebastian Andrzej Siewior musb_writew(epio, MUSB_TXCSR, (MUSB_TXCSR_P_WZC_BITS & 339fc525751SSebastian Andrzej Siewior ~MUSB_TXCSR_P_UNDERRUN) | csr); 340550a7375SFelipe Balbi 341550a7375SFelipe Balbi /* ensure writebuffer is empty */ 342550a7375SFelipe Balbi csr = musb_readw(epio, MUSB_TXCSR); 343550a7375SFelipe Balbi 344fc525751SSebastian Andrzej Siewior /* 345fc525751SSebastian Andrzej Siewior * NOTE host side sets DMAENAB later than this; both are 346fc525751SSebastian Andrzej Siewior * OK since the transfer dma glue (between CPPI and 347fc525751SSebastian Andrzej Siewior * Mentor fifos) just tells CPPI it could start. Data 348fc525751SSebastian Andrzej Siewior * only moves to the USB TX fifo when both fifos are 349fc525751SSebastian Andrzej Siewior * ready. 350550a7375SFelipe Balbi */ 351fc525751SSebastian Andrzej Siewior /* 352fc525751SSebastian Andrzej Siewior * "mode" is irrelevant here; handle terminating ZLPs 353fc525751SSebastian Andrzej Siewior * like PIO does, since the hardware RNDIS mode seems 354fc525751SSebastian Andrzej Siewior * unreliable except for the 355fc525751SSebastian Andrzej Siewior * last-packet-is-already-short case. 356550a7375SFelipe Balbi */ 357550a7375SFelipe Balbi use_dma = use_dma && c->channel_program( 358550a7375SFelipe Balbi musb_ep->dma, musb_ep->packet_sz, 359550a7375SFelipe Balbi 0, 36066af83ddSMing Lei request->dma + request->actual, 36166af83ddSMing Lei request_size); 362550a7375SFelipe Balbi if (!use_dma) { 363550a7375SFelipe Balbi c->channel_release(musb_ep->dma); 364550a7375SFelipe Balbi musb_ep->dma = NULL; 365b6e434a5SSergei Shtylyov csr &= ~MUSB_TXCSR_DMAENAB; 366b6e434a5SSergei Shtylyov musb_writew(epio, MUSB_TXCSR, csr); 367550a7375SFelipe Balbi /* invariant: prequest->buf is non-null */ 368550a7375SFelipe Balbi } 369f8e9f34fSTony Lindgren } else if (tusb_dma_omap(musb)) 370550a7375SFelipe Balbi use_dma = use_dma && c->channel_program( 371550a7375SFelipe Balbi musb_ep->dma, musb_ep->packet_sz, 372550a7375SFelipe Balbi request->zero, 37366af83ddSMing Lei request->dma + request->actual, 37466af83ddSMing Lei request_size); 375550a7375SFelipe Balbi } 376550a7375SFelipe Balbi #endif 377550a7375SFelipe Balbi 378550a7375SFelipe Balbi if (!use_dma) { 37992d2711fSHema Kalliguddi /* 38092d2711fSHema Kalliguddi * Unmap the dma buffer back to cpu if dma channel 38192d2711fSHema Kalliguddi * programming fails 38292d2711fSHema Kalliguddi */ 38392d2711fSHema Kalliguddi unmap_dma_buffer(req, musb); 38492d2711fSHema Kalliguddi 385550a7375SFelipe Balbi musb_write_fifo(musb_ep->hw_ep, fifo_count, 386550a7375SFelipe Balbi (u8 *) (request->buf + request->actual)); 387550a7375SFelipe Balbi request->actual += fifo_count; 388550a7375SFelipe Balbi csr |= MUSB_TXCSR_TXPKTRDY; 389550a7375SFelipe Balbi csr &= ~MUSB_TXCSR_P_UNDERRUN; 390550a7375SFelipe Balbi musb_writew(epio, MUSB_TXCSR, csr); 391550a7375SFelipe Balbi } 392550a7375SFelipe Balbi 393550a7375SFelipe Balbi /* host may already have the data when this message shows... */ 394b99d3659SBin Liu musb_dbg(musb, "%s TX/IN %s len %d/%d, txcsr %04x, fifo %d/%d", 395550a7375SFelipe Balbi musb_ep->end_point.name, use_dma ? "dma" : "pio", 396550a7375SFelipe Balbi request->actual, request->length, 397550a7375SFelipe Balbi musb_readw(epio, MUSB_TXCSR), 398550a7375SFelipe Balbi fifo_count, 399550a7375SFelipe Balbi musb_readw(epio, MUSB_TXMAXP)); 400550a7375SFelipe Balbi } 401550a7375SFelipe Balbi 402550a7375SFelipe Balbi /* 403550a7375SFelipe Balbi * FIFO state update (e.g. data ready). 404550a7375SFelipe Balbi * Called from IRQ, with controller locked. 405550a7375SFelipe Balbi */ 406550a7375SFelipe Balbi void musb_g_tx(struct musb *musb, u8 epnum) 407550a7375SFelipe Balbi { 408550a7375SFelipe Balbi u16 csr; 409ad1adb89SFelipe Balbi struct musb_request *req; 410550a7375SFelipe Balbi struct usb_request *request; 411550a7375SFelipe Balbi u8 __iomem *mbase = musb->mregs; 412550a7375SFelipe Balbi struct musb_ep *musb_ep = &musb->endpoints[epnum].ep_in; 413550a7375SFelipe Balbi void __iomem *epio = musb->endpoints[epnum].regs; 414550a7375SFelipe Balbi struct dma_channel *dma; 415550a7375SFelipe Balbi 416550a7375SFelipe Balbi musb_ep_select(mbase, epnum); 417ad1adb89SFelipe Balbi req = next_request(musb_ep); 418ad1adb89SFelipe Balbi request = &req->request; 419550a7375SFelipe Balbi 420550a7375SFelipe Balbi csr = musb_readw(epio, MUSB_TXCSR); 421b99d3659SBin Liu musb_dbg(musb, "<== %s, txcsr %04x", musb_ep->end_point.name, csr); 422550a7375SFelipe Balbi 423550a7375SFelipe Balbi dma = is_dma_capable() ? musb_ep->dma : NULL; 4247723de7eSSergei Shtylyov 4257723de7eSSergei Shtylyov /* 4267723de7eSSergei Shtylyov * REVISIT: for high bandwidth, MUSB_TXCSR_P_INCOMPTX 4277723de7eSSergei Shtylyov * probably rates reporting as a host error. 428550a7375SFelipe Balbi */ 429550a7375SFelipe Balbi if (csr & MUSB_TXCSR_P_SENTSTALL) { 430550a7375SFelipe Balbi csr |= MUSB_TXCSR_P_WZC_BITS; 431550a7375SFelipe Balbi csr &= ~MUSB_TXCSR_P_SENTSTALL; 432550a7375SFelipe Balbi musb_writew(epio, MUSB_TXCSR, csr); 4337723de7eSSergei Shtylyov return; 434550a7375SFelipe Balbi } 435550a7375SFelipe Balbi 436550a7375SFelipe Balbi if (csr & MUSB_TXCSR_P_UNDERRUN) { 4377723de7eSSergei Shtylyov /* We NAKed, no big deal... little reason to care. */ 438550a7375SFelipe Balbi csr |= MUSB_TXCSR_P_WZC_BITS; 4397723de7eSSergei Shtylyov csr &= ~(MUSB_TXCSR_P_UNDERRUN | MUSB_TXCSR_TXPKTRDY); 440550a7375SFelipe Balbi musb_writew(epio, MUSB_TXCSR, csr); 4415c8a86e1SFelipe Balbi dev_vdbg(musb->controller, "underrun on ep%d, req %p\n", 4425c8a86e1SFelipe Balbi epnum, request); 443550a7375SFelipe Balbi } 444550a7375SFelipe Balbi 445550a7375SFelipe Balbi if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) { 4467723de7eSSergei Shtylyov /* 4477723de7eSSergei Shtylyov * SHOULD NOT HAPPEN... has with CPPI though, after 448550a7375SFelipe Balbi * changing SENDSTALL (and other cases); harmless? 449550a7375SFelipe Balbi */ 450b99d3659SBin Liu musb_dbg(musb, "%s dma still busy?", musb_ep->end_point.name); 4517723de7eSSergei Shtylyov return; 452550a7375SFelipe Balbi } 453550a7375SFelipe Balbi 454550a7375SFelipe Balbi if (request) { 455550a7375SFelipe Balbi u8 is_dma = 0; 456fb91cddcSTony Lindgren bool short_packet = false; 457550a7375SFelipe Balbi 4589aea9b6cSBin Liu trace_musb_req_tx(req); 4599aea9b6cSBin Liu 460550a7375SFelipe Balbi if (dma && (csr & MUSB_TXCSR_DMAENAB)) { 461550a7375SFelipe Balbi is_dma = 1; 462550a7375SFelipe Balbi csr |= MUSB_TXCSR_P_WZC_BITS; 4637723de7eSSergei Shtylyov csr &= ~(MUSB_TXCSR_DMAENAB | MUSB_TXCSR_P_UNDERRUN | 464100d4a9dSMian Yousaf Kaukab MUSB_TXCSR_TXPKTRDY | MUSB_TXCSR_AUTOSET); 465550a7375SFelipe Balbi musb_writew(epio, MUSB_TXCSR, csr); 4667723de7eSSergei Shtylyov /* Ensure writebuffer is empty. */ 467550a7375SFelipe Balbi csr = musb_readw(epio, MUSB_TXCSR); 468550a7375SFelipe Balbi request->actual += musb_ep->dma->actual_len; 469b99d3659SBin Liu musb_dbg(musb, "TXCSR%d %04x, DMA off, len %zu, req %p", 4707723de7eSSergei Shtylyov epnum, csr, musb_ep->dma->actual_len, request); 471550a7375SFelipe Balbi } 472550a7375SFelipe Balbi 4737723de7eSSergei Shtylyov /* 4747723de7eSSergei Shtylyov * First, maybe a terminating short packet. Some DMA 4757723de7eSSergei Shtylyov * engines might handle this by themselves. 476550a7375SFelipe Balbi */ 477fb91cddcSTony Lindgren if ((request->zero && request->length) 478e7379aaaSMing Lei && (request->length % musb_ep->packet_sz == 0) 479e7379aaaSMing Lei && (request->actual == request->length)) 480fb91cddcSTony Lindgren short_packet = true; 481fb91cddcSTony Lindgren 482fb91cddcSTony Lindgren if ((musb_dma_inventra(musb) || musb_dma_ux500(musb)) && 483fb91cddcSTony Lindgren (is_dma && (!dma->desired_mode || 484550a7375SFelipe Balbi (request->actual & 485fb91cddcSTony Lindgren (musb_ep->packet_sz - 1))))) 486fb91cddcSTony Lindgren short_packet = true; 487fb91cddcSTony Lindgren 488fb91cddcSTony Lindgren if (short_packet) { 4897723de7eSSergei Shtylyov /* 4907723de7eSSergei Shtylyov * On DMA completion, FIFO may not be 4917723de7eSSergei Shtylyov * available yet... 492550a7375SFelipe Balbi */ 493550a7375SFelipe Balbi if (csr & MUSB_TXCSR_TXPKTRDY) 4947723de7eSSergei Shtylyov return; 495550a7375SFelipe Balbi 4967723de7eSSergei Shtylyov musb_writew(epio, MUSB_TXCSR, MUSB_TXCSR_MODE 497550a7375SFelipe Balbi | MUSB_TXCSR_TXPKTRDY); 498550a7375SFelipe Balbi request->zero = 0; 499550a7375SFelipe Balbi } 500550a7375SFelipe Balbi 501bb27bc2cSMing Lei if (request->actual == request->length) { 502550a7375SFelipe Balbi musb_g_giveback(musb_ep, request, 0); 50339287076SSupriya Karanth /* 50439287076SSupriya Karanth * In the giveback function the MUSB lock is 50539287076SSupriya Karanth * released and acquired after sometime. During 50639287076SSupriya Karanth * this time period the INDEX register could get 50739287076SSupriya Karanth * changed by the gadget_queue function especially 50839287076SSupriya Karanth * on SMP systems. Reselect the INDEX to be sure 50939287076SSupriya Karanth * we are reading/modifying the right registers 51039287076SSupriya Karanth */ 51139287076SSupriya Karanth musb_ep_select(mbase, epnum); 512ad1adb89SFelipe Balbi req = musb_ep->desc ? next_request(musb_ep) : NULL; 513ad1adb89SFelipe Balbi if (!req) { 514b99d3659SBin Liu musb_dbg(musb, "%s idle now", 515550a7375SFelipe Balbi musb_ep->end_point.name); 5167723de7eSSergei Shtylyov return; 51795962a77SSergei Shtylyov } 518550a7375SFelipe Balbi } 519550a7375SFelipe Balbi 520ad1adb89SFelipe Balbi txstate(musb, req); 521550a7375SFelipe Balbi } 522550a7375SFelipe Balbi } 523550a7375SFelipe Balbi 524550a7375SFelipe Balbi /* ------------------------------------------------------------ */ 525550a7375SFelipe Balbi 526550a7375SFelipe Balbi /* 527550a7375SFelipe Balbi * Context: controller locked, IRQs blocked, endpoint selected 528550a7375SFelipe Balbi */ 529550a7375SFelipe Balbi static void rxstate(struct musb *musb, struct musb_request *req) 530550a7375SFelipe Balbi { 531550a7375SFelipe Balbi const u8 epnum = req->epnum; 532550a7375SFelipe Balbi struct usb_request *request = &req->request; 533bd2e74d6SMing Lei struct musb_ep *musb_ep; 534550a7375SFelipe Balbi void __iomem *epio = musb->endpoints[epnum].regs; 535f0443afdSSergei Shtylyov unsigned len = 0; 536f0443afdSSergei Shtylyov u16 fifo_count; 537cea83241SSergei Shtylyov u16 csr = musb_readw(epio, MUSB_RXCSR); 538bd2e74d6SMing Lei struct musb_hw_ep *hw_ep = &musb->endpoints[epnum]; 5390ae52d54SAnand Gadiyar u8 use_mode_1; 540bd2e74d6SMing Lei 541bd2e74d6SMing Lei if (hw_ep->is_shared_fifo) 542bd2e74d6SMing Lei musb_ep = &hw_ep->ep_in; 543bd2e74d6SMing Lei else 544bd2e74d6SMing Lei musb_ep = &hw_ep->ep_out; 545bd2e74d6SMing Lei 546f0443afdSSergei Shtylyov fifo_count = musb_ep->packet_sz; 547550a7375SFelipe Balbi 548abf710e6SVikram Pandita /* Check if EP is disabled */ 549abf710e6SVikram Pandita if (!musb_ep->desc) { 550b99d3659SBin Liu musb_dbg(musb, "ep:%s disabled - ignore request", 551abf710e6SVikram Pandita musb_ep->end_point.name); 552abf710e6SVikram Pandita return; 553abf710e6SVikram Pandita } 554abf710e6SVikram Pandita 555cea83241SSergei Shtylyov /* We shouldn't get here while DMA is active, but we do... */ 556cea83241SSergei Shtylyov if (dma_channel_status(musb_ep->dma) == MUSB_DMA_STATUS_BUSY) { 557b99d3659SBin Liu musb_dbg(musb, "DMA pending..."); 558cea83241SSergei Shtylyov return; 559cea83241SSergei Shtylyov } 560cea83241SSergei Shtylyov 561cea83241SSergei Shtylyov if (csr & MUSB_RXCSR_P_SENDSTALL) { 562b99d3659SBin Liu musb_dbg(musb, "%s stalling, RXCSR %04x", 563cea83241SSergei Shtylyov musb_ep->end_point.name, csr); 564cea83241SSergei Shtylyov return; 565cea83241SSergei Shtylyov } 566550a7375SFelipe Balbi 567f8e9f34fSTony Lindgren if (is_cppi_enabled(musb) && is_buffer_mapped(req)) { 568550a7375SFelipe Balbi struct dma_controller *c = musb->dma_controller; 569550a7375SFelipe Balbi struct dma_channel *channel = musb_ep->dma; 570550a7375SFelipe Balbi 571550a7375SFelipe Balbi /* NOTE: CPPI won't actually stop advancing the DMA 572550a7375SFelipe Balbi * queue after short packet transfers, so this is almost 573550a7375SFelipe Balbi * always going to run as IRQ-per-packet DMA so that 574550a7375SFelipe Balbi * faults will be handled correctly. 575550a7375SFelipe Balbi */ 576550a7375SFelipe Balbi if (c->channel_program(channel, 577550a7375SFelipe Balbi musb_ep->packet_sz, 578550a7375SFelipe Balbi !request->short_not_ok, 579550a7375SFelipe Balbi request->dma + request->actual, 580550a7375SFelipe Balbi request->length - request->actual)) { 581550a7375SFelipe Balbi 582550a7375SFelipe Balbi /* make sure that if an rxpkt arrived after the irq, 583550a7375SFelipe Balbi * the cppi engine will be ready to take it as soon 584550a7375SFelipe Balbi * as DMA is enabled 585550a7375SFelipe Balbi */ 586550a7375SFelipe Balbi csr &= ~(MUSB_RXCSR_AUTOCLEAR 587550a7375SFelipe Balbi | MUSB_RXCSR_DMAMODE); 588550a7375SFelipe Balbi csr |= MUSB_RXCSR_DMAENAB | MUSB_RXCSR_P_WZC_BITS; 589550a7375SFelipe Balbi musb_writew(epio, MUSB_RXCSR, csr); 590550a7375SFelipe Balbi return; 591550a7375SFelipe Balbi } 592550a7375SFelipe Balbi } 593550a7375SFelipe Balbi 594550a7375SFelipe Balbi if (csr & MUSB_RXCSR_RXPKTRDY) { 595f0443afdSSergei Shtylyov fifo_count = musb_readw(epio, MUSB_RXCOUNT); 5960ae52d54SAnand Gadiyar 5970ae52d54SAnand Gadiyar /* 59800a89180SFelipe Balbi * Enable Mode 1 on RX transfers only when short_not_ok flag 59900a89180SFelipe Balbi * is set. Currently short_not_ok flag is set only from 60000a89180SFelipe Balbi * file_storage and f_mass_storage drivers 6010ae52d54SAnand Gadiyar */ 60200a89180SFelipe Balbi 60300a89180SFelipe Balbi if (request->short_not_ok && fifo_count == musb_ep->packet_sz) 6040ae52d54SAnand Gadiyar use_mode_1 = 1; 6050ae52d54SAnand Gadiyar else 6060ae52d54SAnand Gadiyar use_mode_1 = 0; 6070ae52d54SAnand Gadiyar 608550a7375SFelipe Balbi if (request->actual < request->length) { 60903840fadSFelipe Balbi if (!is_buffer_mapped(req)) 61003840fadSFelipe Balbi goto buffer_aint_mapped; 61103840fadSFelipe Balbi 61203840fadSFelipe Balbi if (musb_dma_inventra(musb)) { 613550a7375SFelipe Balbi struct dma_controller *c; 614550a7375SFelipe Balbi struct dma_channel *channel; 615550a7375SFelipe Balbi int use_dma = 0; 61637730eccSFelipe Balbi unsigned int transfer_size; 617550a7375SFelipe Balbi 618550a7375SFelipe Balbi c = musb->dma_controller; 619550a7375SFelipe Balbi channel = musb_ep->dma; 620550a7375SFelipe Balbi 62100a89180SFelipe Balbi /* We use DMA Req mode 0 in rx_csr, and DMA controller operates in 62200a89180SFelipe Balbi * mode 0 only. So we do not get endpoint interrupts due to DMA 62300a89180SFelipe Balbi * completion. We only get interrupts from DMA controller. 62400a89180SFelipe Balbi * 62500a89180SFelipe Balbi * We could operate in DMA mode 1 if we knew the size of the tranfer 62600a89180SFelipe Balbi * in advance. For mass storage class, request->length = what the host 62700a89180SFelipe Balbi * sends, so that'd work. But for pretty much everything else, 62800a89180SFelipe Balbi * request->length is routinely more than what the host sends. For 62900a89180SFelipe Balbi * most these gadgets, end of is signified either by a short packet, 63000a89180SFelipe Balbi * or filling the last byte of the buffer. (Sending extra data in 63100a89180SFelipe Balbi * that last pckate should trigger an overflow fault.) But in mode 1, 63200a89180SFelipe Balbi * we don't get DMA completion interrupt for short packets. 63300a89180SFelipe Balbi * 63400a89180SFelipe Balbi * Theoretically, we could enable DMAReq irq (MUSB_RXCSR_DMAMODE = 1), 63500a89180SFelipe Balbi * to get endpoint interrupt on every DMA req, but that didn't seem 63600a89180SFelipe Balbi * to work reliably. 63700a89180SFelipe Balbi * 63800a89180SFelipe Balbi * REVISIT an updated g_file_storage can set req->short_not_ok, which 63900a89180SFelipe Balbi * then becomes usable as a runtime "use mode 1" hint... 64000a89180SFelipe Balbi */ 64100a89180SFelipe Balbi 6420ae52d54SAnand Gadiyar /* Experimental: Mode1 works with mass storage use cases */ 6430ae52d54SAnand Gadiyar if (use_mode_1) { 6449001d80dSMing Lei csr |= MUSB_RXCSR_AUTOCLEAR; 6450ae52d54SAnand Gadiyar musb_writew(epio, MUSB_RXCSR, csr); 6460ae52d54SAnand Gadiyar csr |= MUSB_RXCSR_DMAENAB; 6470ae52d54SAnand Gadiyar musb_writew(epio, MUSB_RXCSR, csr); 648550a7375SFelipe Balbi 6490ae52d54SAnand Gadiyar /* 6500ae52d54SAnand Gadiyar * this special sequence (enabling and then 651550a7375SFelipe Balbi * disabling MUSB_RXCSR_DMAMODE) is required 652550a7375SFelipe Balbi * to get DMAReq to activate 653550a7375SFelipe Balbi */ 654550a7375SFelipe Balbi musb_writew(epio, MUSB_RXCSR, 655550a7375SFelipe Balbi csr | MUSB_RXCSR_DMAMODE); 6560ae52d54SAnand Gadiyar musb_writew(epio, MUSB_RXCSR, csr); 6570ae52d54SAnand Gadiyar 65837730eccSFelipe Balbi transfer_size = min_t(unsigned int, 65937730eccSFelipe Balbi request->length - 66037730eccSFelipe Balbi request->actual, 661660fa886SRoger Quadros channel->max_len); 662660fa886SRoger Quadros musb_ep->dma->desired_mode = 1; 6630ae52d54SAnand Gadiyar } else { 6649001d80dSMing Lei if (!musb_ep->hb_mult && 6659001d80dSMing Lei musb_ep->hw_ep->rx_double_buffered) 6669001d80dSMing Lei csr |= MUSB_RXCSR_AUTOCLEAR; 6670ae52d54SAnand Gadiyar csr |= MUSB_RXCSR_DMAENAB; 668550a7375SFelipe Balbi musb_writew(epio, MUSB_RXCSR, csr); 669550a7375SFelipe Balbi 6701018b4e4SMing Lei transfer_size = min(request->length - request->actual, 671f0443afdSSergei Shtylyov (unsigned)fifo_count); 672550a7375SFelipe Balbi musb_ep->dma->desired_mode = 0; 6730ae52d54SAnand Gadiyar } 674550a7375SFelipe Balbi 675550a7375SFelipe Balbi use_dma = c->channel_program( 676550a7375SFelipe Balbi channel, 677550a7375SFelipe Balbi musb_ep->packet_sz, 678550a7375SFelipe Balbi channel->desired_mode, 679550a7375SFelipe Balbi request->dma 680550a7375SFelipe Balbi + request->actual, 681550a7375SFelipe Balbi transfer_size); 682550a7375SFelipe Balbi 683550a7375SFelipe Balbi if (use_dma) 684550a7375SFelipe Balbi return; 685550a7375SFelipe Balbi } 68603840fadSFelipe Balbi 68703840fadSFelipe Balbi if ((musb_dma_ux500(musb)) && 688a48ff906SMian Yousaf Kaukab (request->actual < request->length)) { 689a48ff906SMian Yousaf Kaukab 690a48ff906SMian Yousaf Kaukab struct dma_controller *c; 691a48ff906SMian Yousaf Kaukab struct dma_channel *channel; 69237730eccSFelipe Balbi unsigned int transfer_size = 0; 693a48ff906SMian Yousaf Kaukab 694a48ff906SMian Yousaf Kaukab c = musb->dma_controller; 695a48ff906SMian Yousaf Kaukab channel = musb_ep->dma; 696a48ff906SMian Yousaf Kaukab 697a48ff906SMian Yousaf Kaukab /* In case first packet is short */ 698f0443afdSSergei Shtylyov if (fifo_count < musb_ep->packet_sz) 699f0443afdSSergei Shtylyov transfer_size = fifo_count; 700a48ff906SMian Yousaf Kaukab else if (request->short_not_ok) 70137730eccSFelipe Balbi transfer_size = min_t(unsigned int, 70237730eccSFelipe Balbi request->length - 703a48ff906SMian Yousaf Kaukab request->actual, 704a48ff906SMian Yousaf Kaukab channel->max_len); 705a48ff906SMian Yousaf Kaukab else 70637730eccSFelipe Balbi transfer_size = min_t(unsigned int, 70737730eccSFelipe Balbi request->length - 708a48ff906SMian Yousaf Kaukab request->actual, 709f0443afdSSergei Shtylyov (unsigned)fifo_count); 710a48ff906SMian Yousaf Kaukab 711a48ff906SMian Yousaf Kaukab csr &= ~MUSB_RXCSR_DMAMODE; 712a48ff906SMian Yousaf Kaukab csr |= (MUSB_RXCSR_DMAENAB | 713a48ff906SMian Yousaf Kaukab MUSB_RXCSR_AUTOCLEAR); 714a48ff906SMian Yousaf Kaukab 715a48ff906SMian Yousaf Kaukab musb_writew(epio, MUSB_RXCSR, csr); 716a48ff906SMian Yousaf Kaukab 717a48ff906SMian Yousaf Kaukab if (transfer_size <= musb_ep->packet_sz) { 718a48ff906SMian Yousaf Kaukab musb_ep->dma->desired_mode = 0; 719a48ff906SMian Yousaf Kaukab } else { 720a48ff906SMian Yousaf Kaukab musb_ep->dma->desired_mode = 1; 721a48ff906SMian Yousaf Kaukab /* Mode must be set after DMAENAB */ 722a48ff906SMian Yousaf Kaukab csr |= MUSB_RXCSR_DMAMODE; 723a48ff906SMian Yousaf Kaukab musb_writew(epio, MUSB_RXCSR, csr); 724a48ff906SMian Yousaf Kaukab } 725a48ff906SMian Yousaf Kaukab 726a48ff906SMian Yousaf Kaukab if (c->channel_program(channel, 727a48ff906SMian Yousaf Kaukab musb_ep->packet_sz, 728a48ff906SMian Yousaf Kaukab channel->desired_mode, 729a48ff906SMian Yousaf Kaukab request->dma 730a48ff906SMian Yousaf Kaukab + request->actual, 731a48ff906SMian Yousaf Kaukab transfer_size)) 732a48ff906SMian Yousaf Kaukab 733a48ff906SMian Yousaf Kaukab return; 734a48ff906SMian Yousaf Kaukab } 735550a7375SFelipe Balbi 736f0443afdSSergei Shtylyov len = request->length - request->actual; 737b99d3659SBin Liu musb_dbg(musb, "%s OUT/RX pio fifo %d/%d, maxpacket %d", 738550a7375SFelipe Balbi musb_ep->end_point.name, 739f0443afdSSergei Shtylyov fifo_count, len, 740550a7375SFelipe Balbi musb_ep->packet_sz); 741550a7375SFelipe Balbi 742c2c96321SFelipe Balbi fifo_count = min_t(unsigned, len, fifo_count); 743550a7375SFelipe Balbi 74403840fadSFelipe Balbi if (tusb_dma_omap(musb)) { 745550a7375SFelipe Balbi struct dma_controller *c = musb->dma_controller; 746550a7375SFelipe Balbi struct dma_channel *channel = musb_ep->dma; 747550a7375SFelipe Balbi u32 dma_addr = request->dma + request->actual; 748550a7375SFelipe Balbi int ret; 749550a7375SFelipe Balbi 750550a7375SFelipe Balbi ret = c->channel_program(channel, 751550a7375SFelipe Balbi musb_ep->packet_sz, 752550a7375SFelipe Balbi channel->desired_mode, 753550a7375SFelipe Balbi dma_addr, 754550a7375SFelipe Balbi fifo_count); 755550a7375SFelipe Balbi if (ret) 756550a7375SFelipe Balbi return; 757550a7375SFelipe Balbi } 75803840fadSFelipe Balbi 75992d2711fSHema Kalliguddi /* 76092d2711fSHema Kalliguddi * Unmap the dma buffer back to cpu if dma channel 76192d2711fSHema Kalliguddi * programming fails. This buffer is mapped if the 76292d2711fSHema Kalliguddi * channel allocation is successful 76392d2711fSHema Kalliguddi */ 76492d2711fSHema Kalliguddi unmap_dma_buffer(req, musb); 76592d2711fSHema Kalliguddi 766e75df371SMing Lei /* 767e75df371SMing Lei * Clear DMAENAB and AUTOCLEAR for the 76892d2711fSHema Kalliguddi * PIO mode transfer 76992d2711fSHema Kalliguddi */ 770e75df371SMing Lei csr &= ~(MUSB_RXCSR_DMAENAB | MUSB_RXCSR_AUTOCLEAR); 77192d2711fSHema Kalliguddi musb_writew(epio, MUSB_RXCSR, csr); 772550a7375SFelipe Balbi 77303840fadSFelipe Balbi buffer_aint_mapped: 774550a7375SFelipe Balbi musb_read_fifo(musb_ep->hw_ep, fifo_count, (u8 *) 775550a7375SFelipe Balbi (request->buf + request->actual)); 776550a7375SFelipe Balbi request->actual += fifo_count; 777550a7375SFelipe Balbi 778550a7375SFelipe Balbi /* REVISIT if we left anything in the fifo, flush 779550a7375SFelipe Balbi * it and report -EOVERFLOW 780550a7375SFelipe Balbi */ 781550a7375SFelipe Balbi 782550a7375SFelipe Balbi /* ack the read! */ 783550a7375SFelipe Balbi csr |= MUSB_RXCSR_P_WZC_BITS; 784550a7375SFelipe Balbi csr &= ~MUSB_RXCSR_RXPKTRDY; 785550a7375SFelipe Balbi musb_writew(epio, MUSB_RXCSR, csr); 786550a7375SFelipe Balbi } 787550a7375SFelipe Balbi } 788550a7375SFelipe Balbi 789550a7375SFelipe Balbi /* reach the end or short packet detected */ 790f0443afdSSergei Shtylyov if (request->actual == request->length || 791f0443afdSSergei Shtylyov fifo_count < musb_ep->packet_sz) 792550a7375SFelipe Balbi musb_g_giveback(musb_ep, request, 0); 793550a7375SFelipe Balbi } 794550a7375SFelipe Balbi 795550a7375SFelipe Balbi /* 796550a7375SFelipe Balbi * Data ready for a request; called from IRQ 797550a7375SFelipe Balbi */ 798550a7375SFelipe Balbi void musb_g_rx(struct musb *musb, u8 epnum) 799550a7375SFelipe Balbi { 800550a7375SFelipe Balbi u16 csr; 801ad1adb89SFelipe Balbi struct musb_request *req; 802550a7375SFelipe Balbi struct usb_request *request; 803550a7375SFelipe Balbi void __iomem *mbase = musb->mregs; 804bd2e74d6SMing Lei struct musb_ep *musb_ep; 805550a7375SFelipe Balbi void __iomem *epio = musb->endpoints[epnum].regs; 806550a7375SFelipe Balbi struct dma_channel *dma; 807bd2e74d6SMing Lei struct musb_hw_ep *hw_ep = &musb->endpoints[epnum]; 808bd2e74d6SMing Lei 809bd2e74d6SMing Lei if (hw_ep->is_shared_fifo) 810bd2e74d6SMing Lei musb_ep = &hw_ep->ep_in; 811bd2e74d6SMing Lei else 812bd2e74d6SMing Lei musb_ep = &hw_ep->ep_out; 813550a7375SFelipe Balbi 814550a7375SFelipe Balbi musb_ep_select(mbase, epnum); 815550a7375SFelipe Balbi 816ad1adb89SFelipe Balbi req = next_request(musb_ep); 817ad1adb89SFelipe Balbi if (!req) 8180abdc36fSMaulik Mankad return; 819550a7375SFelipe Balbi 820fc78003eSBin Liu trace_musb_req_rx(req); 821ad1adb89SFelipe Balbi request = &req->request; 822ad1adb89SFelipe Balbi 823550a7375SFelipe Balbi csr = musb_readw(epio, MUSB_RXCSR); 824550a7375SFelipe Balbi dma = is_dma_capable() ? musb_ep->dma : NULL; 825550a7375SFelipe Balbi 826b99d3659SBin Liu musb_dbg(musb, "<== %s, rxcsr %04x%s %p", musb_ep->end_point.name, 827550a7375SFelipe Balbi csr, dma ? " (dma)" : "", request); 828550a7375SFelipe Balbi 829550a7375SFelipe Balbi if (csr & MUSB_RXCSR_P_SENTSTALL) { 830550a7375SFelipe Balbi csr |= MUSB_RXCSR_P_WZC_BITS; 831550a7375SFelipe Balbi csr &= ~MUSB_RXCSR_P_SENTSTALL; 832550a7375SFelipe Balbi musb_writew(epio, MUSB_RXCSR, csr); 833cea83241SSergei Shtylyov return; 834550a7375SFelipe Balbi } 835550a7375SFelipe Balbi 836550a7375SFelipe Balbi if (csr & MUSB_RXCSR_P_OVERRUN) { 837550a7375SFelipe Balbi /* csr |= MUSB_RXCSR_P_WZC_BITS; */ 838550a7375SFelipe Balbi csr &= ~MUSB_RXCSR_P_OVERRUN; 839550a7375SFelipe Balbi musb_writew(epio, MUSB_RXCSR, csr); 840550a7375SFelipe Balbi 841b99d3659SBin Liu musb_dbg(musb, "%s iso overrun on %p", musb_ep->name, request); 84243467868SSergei Shtylyov if (request->status == -EINPROGRESS) 843550a7375SFelipe Balbi request->status = -EOVERFLOW; 844550a7375SFelipe Balbi } 845550a7375SFelipe Balbi if (csr & MUSB_RXCSR_INCOMPRX) { 846550a7375SFelipe Balbi /* REVISIT not necessarily an error */ 847b99d3659SBin Liu musb_dbg(musb, "%s, incomprx", musb_ep->end_point.name); 848550a7375SFelipe Balbi } 849550a7375SFelipe Balbi 850550a7375SFelipe Balbi if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) { 851550a7375SFelipe Balbi /* "should not happen"; likely RXPKTRDY pending for DMA */ 852b99d3659SBin Liu musb_dbg(musb, "%s busy, csr %04x", 853550a7375SFelipe Balbi musb_ep->end_point.name, csr); 854cea83241SSergei Shtylyov return; 855550a7375SFelipe Balbi } 856550a7375SFelipe Balbi 857550a7375SFelipe Balbi if (dma && (csr & MUSB_RXCSR_DMAENAB)) { 858550a7375SFelipe Balbi csr &= ~(MUSB_RXCSR_AUTOCLEAR 859550a7375SFelipe Balbi | MUSB_RXCSR_DMAENAB 860550a7375SFelipe Balbi | MUSB_RXCSR_DMAMODE); 861550a7375SFelipe Balbi musb_writew(epio, MUSB_RXCSR, 862550a7375SFelipe Balbi MUSB_RXCSR_P_WZC_BITS | csr); 863550a7375SFelipe Balbi 864550a7375SFelipe Balbi request->actual += musb_ep->dma->actual_len; 865550a7375SFelipe Balbi 866a48ff906SMian Yousaf Kaukab #if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_TUSB_OMAP_DMA) || \ 867a48ff906SMian Yousaf Kaukab defined(CONFIG_USB_UX500_DMA) 868550a7375SFelipe Balbi /* Autoclear doesn't clear RxPktRdy for short packets */ 8699001d80dSMing Lei if ((dma->desired_mode == 0 && !hw_ep->rx_double_buffered) 870550a7375SFelipe Balbi || (dma->actual_len 871550a7375SFelipe Balbi & (musb_ep->packet_sz - 1))) { 872550a7375SFelipe Balbi /* ack the read! */ 873550a7375SFelipe Balbi csr &= ~MUSB_RXCSR_RXPKTRDY; 874550a7375SFelipe Balbi musb_writew(epio, MUSB_RXCSR, csr); 875550a7375SFelipe Balbi } 876550a7375SFelipe Balbi 877550a7375SFelipe Balbi /* incomplete, and not short? wait for next IN packet */ 878550a7375SFelipe Balbi if ((request->actual < request->length) 879550a7375SFelipe Balbi && (musb_ep->dma->actual_len 8809001d80dSMing Lei == musb_ep->packet_sz)) { 8819001d80dSMing Lei /* In double buffer case, continue to unload fifo if 8829001d80dSMing Lei * there is Rx packet in FIFO. 8839001d80dSMing Lei **/ 8849001d80dSMing Lei csr = musb_readw(epio, MUSB_RXCSR); 8859001d80dSMing Lei if ((csr & MUSB_RXCSR_RXPKTRDY) && 8869001d80dSMing Lei hw_ep->rx_double_buffered) 8879001d80dSMing Lei goto exit; 888cea83241SSergei Shtylyov return; 8899001d80dSMing Lei } 890550a7375SFelipe Balbi #endif 891550a7375SFelipe Balbi musb_g_giveback(musb_ep, request, 0); 89239287076SSupriya Karanth /* 89339287076SSupriya Karanth * In the giveback function the MUSB lock is 89439287076SSupriya Karanth * released and acquired after sometime. During 89539287076SSupriya Karanth * this time period the INDEX register could get 89639287076SSupriya Karanth * changed by the gadget_queue function especially 89739287076SSupriya Karanth * on SMP systems. Reselect the INDEX to be sure 89839287076SSupriya Karanth * we are reading/modifying the right registers 89939287076SSupriya Karanth */ 90039287076SSupriya Karanth musb_ep_select(mbase, epnum); 901550a7375SFelipe Balbi 902ad1adb89SFelipe Balbi req = next_request(musb_ep); 903ad1adb89SFelipe Balbi if (!req) 904cea83241SSergei Shtylyov return; 905550a7375SFelipe Balbi } 906a48ff906SMian Yousaf Kaukab #if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_TUSB_OMAP_DMA) || \ 907a48ff906SMian Yousaf Kaukab defined(CONFIG_USB_UX500_DMA) 9089001d80dSMing Lei exit: 909bb324b08SAjay Kumar Gupta #endif 91043467868SSergei Shtylyov /* Analyze request */ 911ad1adb89SFelipe Balbi rxstate(musb, req); 912550a7375SFelipe Balbi } 913550a7375SFelipe Balbi 914550a7375SFelipe Balbi /* ------------------------------------------------------------ */ 915550a7375SFelipe Balbi 916550a7375SFelipe Balbi static int musb_gadget_enable(struct usb_ep *ep, 917550a7375SFelipe Balbi const struct usb_endpoint_descriptor *desc) 918550a7375SFelipe Balbi { 919550a7375SFelipe Balbi unsigned long flags; 920550a7375SFelipe Balbi struct musb_ep *musb_ep; 921550a7375SFelipe Balbi struct musb_hw_ep *hw_ep; 922550a7375SFelipe Balbi void __iomem *regs; 923550a7375SFelipe Balbi struct musb *musb; 924550a7375SFelipe Balbi void __iomem *mbase; 925550a7375SFelipe Balbi u8 epnum; 926550a7375SFelipe Balbi u16 csr; 927550a7375SFelipe Balbi unsigned tmp; 928550a7375SFelipe Balbi int status = -EINVAL; 929550a7375SFelipe Balbi 930550a7375SFelipe Balbi if (!ep || !desc) 931550a7375SFelipe Balbi return -EINVAL; 932550a7375SFelipe Balbi 933550a7375SFelipe Balbi musb_ep = to_musb_ep(ep); 934550a7375SFelipe Balbi hw_ep = musb_ep->hw_ep; 935550a7375SFelipe Balbi regs = hw_ep->regs; 936550a7375SFelipe Balbi musb = musb_ep->musb; 937550a7375SFelipe Balbi mbase = musb->mregs; 938550a7375SFelipe Balbi epnum = musb_ep->current_epnum; 939550a7375SFelipe Balbi 940550a7375SFelipe Balbi spin_lock_irqsave(&musb->lock, flags); 941550a7375SFelipe Balbi 942550a7375SFelipe Balbi if (musb_ep->desc) { 943550a7375SFelipe Balbi status = -EBUSY; 944550a7375SFelipe Balbi goto fail; 945550a7375SFelipe Balbi } 94696bcd090SJulia Lawall musb_ep->type = usb_endpoint_type(desc); 947550a7375SFelipe Balbi 948550a7375SFelipe Balbi /* check direction and (later) maxpacket size against endpoint */ 94996bcd090SJulia Lawall if (usb_endpoint_num(desc) != epnum) 950550a7375SFelipe Balbi goto fail; 951550a7375SFelipe Balbi 952550a7375SFelipe Balbi /* REVISIT this rules out high bandwidth periodic transfers */ 9536ddcabc2SFelipe Balbi tmp = usb_endpoint_maxp_mult(desc) - 1; 9546ddcabc2SFelipe Balbi if (tmp) { 955f11d893dSMing Lei int ok; 956f11d893dSMing Lei 957f11d893dSMing Lei if (usb_endpoint_dir_in(desc)) 958f11d893dSMing Lei ok = musb->hb_iso_tx; 959f11d893dSMing Lei else 960f11d893dSMing Lei ok = musb->hb_iso_rx; 961f11d893dSMing Lei 962f11d893dSMing Lei if (!ok) { 963b99d3659SBin Liu musb_dbg(musb, "no support for high bandwidth ISO"); 964550a7375SFelipe Balbi goto fail; 965f11d893dSMing Lei } 9666ddcabc2SFelipe Balbi musb_ep->hb_mult = tmp; 967f11d893dSMing Lei } else { 968f11d893dSMing Lei musb_ep->hb_mult = 0; 969f11d893dSMing Lei } 970f11d893dSMing Lei 9716ddcabc2SFelipe Balbi musb_ep->packet_sz = usb_endpoint_maxp(desc); 972f11d893dSMing Lei tmp = musb_ep->packet_sz * (musb_ep->hb_mult + 1); 973550a7375SFelipe Balbi 974550a7375SFelipe Balbi /* enable the interrupts for the endpoint, set the endpoint 975550a7375SFelipe Balbi * packet size (or fail), set the mode, clear the fifo 976550a7375SFelipe Balbi */ 977550a7375SFelipe Balbi musb_ep_select(mbase, epnum); 97896bcd090SJulia Lawall if (usb_endpoint_dir_in(desc)) { 979550a7375SFelipe Balbi 980550a7375SFelipe Balbi if (hw_ep->is_shared_fifo) 981550a7375SFelipe Balbi musb_ep->is_in = 1; 982550a7375SFelipe Balbi if (!musb_ep->is_in) 983550a7375SFelipe Balbi goto fail; 984f11d893dSMing Lei 985f11d893dSMing Lei if (tmp > hw_ep->max_packet_sz_tx) { 986b99d3659SBin Liu musb_dbg(musb, "packet size beyond hardware FIFO size"); 987550a7375SFelipe Balbi goto fail; 988f11d893dSMing Lei } 989550a7375SFelipe Balbi 990b18d26f6SSebastian Andrzej Siewior musb->intrtxe |= (1 << epnum); 991b18d26f6SSebastian Andrzej Siewior musb_writew(mbase, MUSB_INTRTXE, musb->intrtxe); 992550a7375SFelipe Balbi 993550a7375SFelipe Balbi /* REVISIT if can_bulk_split(), use by updating "tmp"; 994550a7375SFelipe Balbi * likewise high bandwidth periodic tx 995550a7375SFelipe Balbi */ 9969f445cb2SCliff Cai /* Set TXMAXP with the FIFO size of the endpoint 99731c9909bSMing Lei * to disable double buffering mode. 9989f445cb2SCliff Cai */ 999bb3a2ef2Ssupriya karanth if (can_bulk_split(musb, musb_ep->type)) 1000bb3a2ef2Ssupriya karanth musb_ep->hb_mult = (hw_ep->max_packet_sz_tx / 1001bb3a2ef2Ssupriya karanth musb_ep->packet_sz) - 1; 100206624818SFelipe Balbi musb_writew(regs, MUSB_TXMAXP, musb_ep->packet_sz 100306624818SFelipe Balbi | (musb_ep->hb_mult << 11)); 1004550a7375SFelipe Balbi 1005550a7375SFelipe Balbi csr = MUSB_TXCSR_MODE | MUSB_TXCSR_CLRDATATOG; 1006550a7375SFelipe Balbi if (musb_readw(regs, MUSB_TXCSR) 1007550a7375SFelipe Balbi & MUSB_TXCSR_FIFONOTEMPTY) 1008550a7375SFelipe Balbi csr |= MUSB_TXCSR_FLUSHFIFO; 1009550a7375SFelipe Balbi if (musb_ep->type == USB_ENDPOINT_XFER_ISOC) 1010550a7375SFelipe Balbi csr |= MUSB_TXCSR_P_ISO; 1011550a7375SFelipe Balbi 1012550a7375SFelipe Balbi /* set twice in case of double buffering */ 1013550a7375SFelipe Balbi musb_writew(regs, MUSB_TXCSR, csr); 1014550a7375SFelipe Balbi /* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */ 1015550a7375SFelipe Balbi musb_writew(regs, MUSB_TXCSR, csr); 1016550a7375SFelipe Balbi 1017550a7375SFelipe Balbi } else { 1018550a7375SFelipe Balbi 1019550a7375SFelipe Balbi if (hw_ep->is_shared_fifo) 1020550a7375SFelipe Balbi musb_ep->is_in = 0; 1021550a7375SFelipe Balbi if (musb_ep->is_in) 1022550a7375SFelipe Balbi goto fail; 1023f11d893dSMing Lei 1024f11d893dSMing Lei if (tmp > hw_ep->max_packet_sz_rx) { 1025b99d3659SBin Liu musb_dbg(musb, "packet size beyond hardware FIFO size"); 1026550a7375SFelipe Balbi goto fail; 1027f11d893dSMing Lei } 1028550a7375SFelipe Balbi 1029af5ec14dSSebastian Andrzej Siewior musb->intrrxe |= (1 << epnum); 1030af5ec14dSSebastian Andrzej Siewior musb_writew(mbase, MUSB_INTRRXE, musb->intrrxe); 1031550a7375SFelipe Balbi 1032550a7375SFelipe Balbi /* REVISIT if can_bulk_combine() use by updating "tmp" 1033550a7375SFelipe Balbi * likewise high bandwidth periodic rx 1034550a7375SFelipe Balbi */ 10359f445cb2SCliff Cai /* Set RXMAXP with the FIFO size of the endpoint 10369f445cb2SCliff Cai * to disable double buffering mode. 10379f445cb2SCliff Cai */ 103806624818SFelipe Balbi musb_writew(regs, MUSB_RXMAXP, musb_ep->packet_sz 103906624818SFelipe Balbi | (musb_ep->hb_mult << 11)); 1040550a7375SFelipe Balbi 1041550a7375SFelipe Balbi /* force shared fifo to OUT-only mode */ 1042550a7375SFelipe Balbi if (hw_ep->is_shared_fifo) { 1043550a7375SFelipe Balbi csr = musb_readw(regs, MUSB_TXCSR); 1044550a7375SFelipe Balbi csr &= ~(MUSB_TXCSR_MODE | MUSB_TXCSR_TXPKTRDY); 1045550a7375SFelipe Balbi musb_writew(regs, MUSB_TXCSR, csr); 1046550a7375SFelipe Balbi } 1047550a7375SFelipe Balbi 1048550a7375SFelipe Balbi csr = MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_CLRDATATOG; 1049550a7375SFelipe Balbi if (musb_ep->type == USB_ENDPOINT_XFER_ISOC) 1050550a7375SFelipe Balbi csr |= MUSB_RXCSR_P_ISO; 1051550a7375SFelipe Balbi else if (musb_ep->type == USB_ENDPOINT_XFER_INT) 1052550a7375SFelipe Balbi csr |= MUSB_RXCSR_DISNYET; 1053550a7375SFelipe Balbi 1054550a7375SFelipe Balbi /* set twice in case of double buffering */ 1055550a7375SFelipe Balbi musb_writew(regs, MUSB_RXCSR, csr); 1056550a7375SFelipe Balbi musb_writew(regs, MUSB_RXCSR, csr); 1057550a7375SFelipe Balbi } 1058550a7375SFelipe Balbi 1059550a7375SFelipe Balbi /* NOTE: all the I/O code _should_ work fine without DMA, in case 1060550a7375SFelipe Balbi * for some reason you run out of channels here. 1061550a7375SFelipe Balbi */ 1062550a7375SFelipe Balbi if (is_dma_capable() && musb->dma_controller) { 1063550a7375SFelipe Balbi struct dma_controller *c = musb->dma_controller; 1064550a7375SFelipe Balbi 1065550a7375SFelipe Balbi musb_ep->dma = c->channel_alloc(c, hw_ep, 1066550a7375SFelipe Balbi (desc->bEndpointAddress & USB_DIR_IN)); 1067550a7375SFelipe Balbi } else 1068550a7375SFelipe Balbi musb_ep->dma = NULL; 1069550a7375SFelipe Balbi 1070550a7375SFelipe Balbi musb_ep->desc = desc; 1071550a7375SFelipe Balbi musb_ep->busy = 0; 107247e97605SSergei Shtylyov musb_ep->wedged = 0; 1073550a7375SFelipe Balbi status = 0; 1074550a7375SFelipe Balbi 1075550a7375SFelipe Balbi pr_debug("%s periph: enabled %s for %s %s, %smaxpacket %d\n", 1076550a7375SFelipe Balbi musb_driver_name, musb_ep->end_point.name, 10770ccbadafSBin Liu musb_ep_xfertype_string(musb_ep->type), 1078550a7375SFelipe Balbi musb_ep->is_in ? "IN" : "OUT", 1079550a7375SFelipe Balbi musb_ep->dma ? "dma, " : "", 1080550a7375SFelipe Balbi musb_ep->packet_sz); 1081550a7375SFelipe Balbi 10822bff3916STony Lindgren schedule_delayed_work(&musb->irq_work, 0); 1083550a7375SFelipe Balbi 1084550a7375SFelipe Balbi fail: 1085550a7375SFelipe Balbi spin_unlock_irqrestore(&musb->lock, flags); 1086550a7375SFelipe Balbi return status; 1087550a7375SFelipe Balbi } 1088550a7375SFelipe Balbi 1089550a7375SFelipe Balbi /* 1090550a7375SFelipe Balbi * Disable an endpoint flushing all requests queued. 1091550a7375SFelipe Balbi */ 1092550a7375SFelipe Balbi static int musb_gadget_disable(struct usb_ep *ep) 1093550a7375SFelipe Balbi { 1094550a7375SFelipe Balbi unsigned long flags; 1095550a7375SFelipe Balbi struct musb *musb; 1096550a7375SFelipe Balbi u8 epnum; 1097550a7375SFelipe Balbi struct musb_ep *musb_ep; 1098550a7375SFelipe Balbi void __iomem *epio; 1099550a7375SFelipe Balbi int status = 0; 1100550a7375SFelipe Balbi 1101550a7375SFelipe Balbi musb_ep = to_musb_ep(ep); 1102550a7375SFelipe Balbi musb = musb_ep->musb; 1103550a7375SFelipe Balbi epnum = musb_ep->current_epnum; 1104550a7375SFelipe Balbi epio = musb->endpoints[epnum].regs; 1105550a7375SFelipe Balbi 1106550a7375SFelipe Balbi spin_lock_irqsave(&musb->lock, flags); 1107550a7375SFelipe Balbi musb_ep_select(musb->mregs, epnum); 1108550a7375SFelipe Balbi 1109550a7375SFelipe Balbi /* zero the endpoint sizes */ 1110550a7375SFelipe Balbi if (musb_ep->is_in) { 1111b18d26f6SSebastian Andrzej Siewior musb->intrtxe &= ~(1 << epnum); 1112b18d26f6SSebastian Andrzej Siewior musb_writew(musb->mregs, MUSB_INTRTXE, musb->intrtxe); 1113550a7375SFelipe Balbi musb_writew(epio, MUSB_TXMAXP, 0); 1114550a7375SFelipe Balbi } else { 1115af5ec14dSSebastian Andrzej Siewior musb->intrrxe &= ~(1 << epnum); 1116af5ec14dSSebastian Andrzej Siewior musb_writew(musb->mregs, MUSB_INTRRXE, musb->intrrxe); 1117550a7375SFelipe Balbi musb_writew(epio, MUSB_RXMAXP, 0); 1118550a7375SFelipe Balbi } 1119550a7375SFelipe Balbi 1120550a7375SFelipe Balbi /* abort all pending DMA and requests */ 1121550a7375SFelipe Balbi nuke(musb_ep, -ESHUTDOWN); 1122550a7375SFelipe Balbi 1123607fb0f4STal Shorer musb_ep->desc = NULL; 1124607fb0f4STal Shorer musb_ep->end_point.desc = NULL; 1125607fb0f4STal Shorer 11262bff3916STony Lindgren schedule_delayed_work(&musb->irq_work, 0); 1127550a7375SFelipe Balbi 1128550a7375SFelipe Balbi spin_unlock_irqrestore(&(musb->lock), flags); 1129550a7375SFelipe Balbi 1130b99d3659SBin Liu musb_dbg(musb, "%s", musb_ep->end_point.name); 1131550a7375SFelipe Balbi 1132550a7375SFelipe Balbi return status; 1133550a7375SFelipe Balbi } 1134550a7375SFelipe Balbi 1135550a7375SFelipe Balbi /* 1136550a7375SFelipe Balbi * Allocate a request for an endpoint. 1137550a7375SFelipe Balbi * Reused by ep0 code. 1138550a7375SFelipe Balbi */ 1139550a7375SFelipe Balbi struct usb_request *musb_alloc_request(struct usb_ep *ep, gfp_t gfp_flags) 1140550a7375SFelipe Balbi { 1141550a7375SFelipe Balbi struct musb_ep *musb_ep = to_musb_ep(ep); 1142550a7375SFelipe Balbi struct musb_request *request = NULL; 1143550a7375SFelipe Balbi 1144550a7375SFelipe Balbi request = kzalloc(sizeof *request, gfp_flags); 1145b99d3659SBin Liu if (!request) 11460607f862SFelipe Balbi return NULL; 11470607f862SFelipe Balbi 1148550a7375SFelipe Balbi request->request.dma = DMA_ADDR_INVALID; 1149550a7375SFelipe Balbi request->epnum = musb_ep->current_epnum; 1150550a7375SFelipe Balbi request->ep = musb_ep; 1151550a7375SFelipe Balbi 1152fc78003eSBin Liu trace_musb_req_alloc(request); 1153550a7375SFelipe Balbi return &request->request; 1154550a7375SFelipe Balbi } 1155550a7375SFelipe Balbi 1156550a7375SFelipe Balbi /* 1157550a7375SFelipe Balbi * Free a request 1158550a7375SFelipe Balbi * Reused by ep0 code. 1159550a7375SFelipe Balbi */ 1160550a7375SFelipe Balbi void musb_free_request(struct usb_ep *ep, struct usb_request *req) 1161550a7375SFelipe Balbi { 1162fc78003eSBin Liu struct musb_request *request = to_musb_request(req); 1163fc78003eSBin Liu 1164fc78003eSBin Liu trace_musb_req_free(request); 1165fc78003eSBin Liu kfree(request); 1166550a7375SFelipe Balbi } 1167550a7375SFelipe Balbi 1168550a7375SFelipe Balbi static LIST_HEAD(buffers); 1169550a7375SFelipe Balbi 1170550a7375SFelipe Balbi struct free_record { 1171550a7375SFelipe Balbi struct list_head list; 1172550a7375SFelipe Balbi struct device *dev; 1173550a7375SFelipe Balbi unsigned bytes; 1174550a7375SFelipe Balbi dma_addr_t dma; 1175550a7375SFelipe Balbi }; 1176550a7375SFelipe Balbi 1177550a7375SFelipe Balbi /* 1178550a7375SFelipe Balbi * Context: controller locked, IRQs blocked. 1179550a7375SFelipe Balbi */ 1180a666e3e6SSergei Shtylyov void musb_ep_restart(struct musb *musb, struct musb_request *req) 1181550a7375SFelipe Balbi { 1182fc78003eSBin Liu trace_musb_req_start(req); 1183550a7375SFelipe Balbi musb_ep_select(musb->mregs, req->epnum); 1184550a7375SFelipe Balbi if (req->tx) 1185550a7375SFelipe Balbi txstate(musb, req); 1186550a7375SFelipe Balbi else 1187550a7375SFelipe Balbi rxstate(musb, req); 1188550a7375SFelipe Balbi } 1189550a7375SFelipe Balbi 1190ea2f35c0STony Lindgren static int musb_ep_restart_resume_work(struct musb *musb, void *data) 1191ea2f35c0STony Lindgren { 1192ea2f35c0STony Lindgren struct musb_request *req = data; 1193ea2f35c0STony Lindgren 1194ea2f35c0STony Lindgren musb_ep_restart(musb, req); 1195ea2f35c0STony Lindgren 1196ea2f35c0STony Lindgren return 0; 1197ea2f35c0STony Lindgren } 1198ea2f35c0STony Lindgren 1199550a7375SFelipe Balbi static int musb_gadget_queue(struct usb_ep *ep, struct usb_request *req, 1200550a7375SFelipe Balbi gfp_t gfp_flags) 1201550a7375SFelipe Balbi { 1202550a7375SFelipe Balbi struct musb_ep *musb_ep; 1203550a7375SFelipe Balbi struct musb_request *request; 1204550a7375SFelipe Balbi struct musb *musb; 1205ea2f35c0STony Lindgren int status; 1206550a7375SFelipe Balbi unsigned long lockflags; 1207550a7375SFelipe Balbi 1208550a7375SFelipe Balbi if (!ep || !req) 1209550a7375SFelipe Balbi return -EINVAL; 1210550a7375SFelipe Balbi if (!req->buf) 1211550a7375SFelipe Balbi return -ENODATA; 1212550a7375SFelipe Balbi 1213550a7375SFelipe Balbi musb_ep = to_musb_ep(ep); 1214550a7375SFelipe Balbi musb = musb_ep->musb; 1215550a7375SFelipe Balbi 1216550a7375SFelipe Balbi request = to_musb_request(req); 1217550a7375SFelipe Balbi request->musb = musb; 1218550a7375SFelipe Balbi 1219550a7375SFelipe Balbi if (request->ep != musb_ep) 1220550a7375SFelipe Balbi return -EINVAL; 1221550a7375SFelipe Balbi 1222ea2f35c0STony Lindgren status = pm_runtime_get(musb->controller); 1223ea2f35c0STony Lindgren if ((status != -EINPROGRESS) && status < 0) { 1224ea2f35c0STony Lindgren dev_err(musb->controller, 1225ea2f35c0STony Lindgren "pm runtime get failed in %s\n", 1226ea2f35c0STony Lindgren __func__); 1227ea2f35c0STony Lindgren pm_runtime_put_noidle(musb->controller); 1228ea2f35c0STony Lindgren 1229ea2f35c0STony Lindgren return status; 1230ea2f35c0STony Lindgren } 1231ea2f35c0STony Lindgren status = 0; 1232ea2f35c0STony Lindgren 1233fc78003eSBin Liu trace_musb_req_enq(request); 1234550a7375SFelipe Balbi 1235550a7375SFelipe Balbi /* request is mine now... */ 1236550a7375SFelipe Balbi request->request.actual = 0; 1237550a7375SFelipe Balbi request->request.status = -EINPROGRESS; 1238550a7375SFelipe Balbi request->epnum = musb_ep->current_epnum; 1239550a7375SFelipe Balbi request->tx = musb_ep->is_in; 1240550a7375SFelipe Balbi 1241c65bfa62SMian Yousaf Kaukab map_dma_buffer(request, musb, musb_ep); 1242550a7375SFelipe Balbi 1243550a7375SFelipe Balbi spin_lock_irqsave(&musb->lock, lockflags); 1244550a7375SFelipe Balbi 1245550a7375SFelipe Balbi /* don't queue if the ep is down */ 1246550a7375SFelipe Balbi if (!musb_ep->desc) { 1247b99d3659SBin Liu musb_dbg(musb, "req %p queued to %s while ep %s", 1248550a7375SFelipe Balbi req, ep->name, "disabled"); 1249550a7375SFelipe Balbi status = -ESHUTDOWN; 125023a53d90SSebastian Andrzej Siewior unmap_dma_buffer(request, musb); 125123a53d90SSebastian Andrzej Siewior goto unlock; 1252550a7375SFelipe Balbi } 1253550a7375SFelipe Balbi 1254550a7375SFelipe Balbi /* add request to the list */ 1255ad1adb89SFelipe Balbi list_add_tail(&request->list, &musb_ep->req_list); 1256550a7375SFelipe Balbi 1257550a7375SFelipe Balbi /* it this is the head of the queue, start i/o ... */ 1258ea2f35c0STony Lindgren if (!musb_ep->busy && &request->list == musb_ep->req_list.next) { 1259ea2f35c0STony Lindgren status = musb_queue_resume_work(musb, 1260ea2f35c0STony Lindgren musb_ep_restart_resume_work, 1261ea2f35c0STony Lindgren request); 1262ea2f35c0STony Lindgren if (status < 0) 1263ea2f35c0STony Lindgren dev_err(musb->controller, "%s resume work: %i\n", 1264ea2f35c0STony Lindgren __func__, status); 1265ea2f35c0STony Lindgren } 1266550a7375SFelipe Balbi 126723a53d90SSebastian Andrzej Siewior unlock: 1268550a7375SFelipe Balbi spin_unlock_irqrestore(&musb->lock, lockflags); 1269cacaaf80STony Lindgren pm_runtime_mark_last_busy(musb->controller); 1270cacaaf80STony Lindgren pm_runtime_put_autosuspend(musb->controller); 1271cacaaf80STony Lindgren 1272550a7375SFelipe Balbi return status; 1273550a7375SFelipe Balbi } 1274550a7375SFelipe Balbi 1275550a7375SFelipe Balbi static int musb_gadget_dequeue(struct usb_ep *ep, struct usb_request *request) 1276550a7375SFelipe Balbi { 1277550a7375SFelipe Balbi struct musb_ep *musb_ep = to_musb_ep(ep); 12784cbbf084SFelipe Balbi struct musb_request *req = to_musb_request(request); 12794cbbf084SFelipe Balbi struct musb_request *r; 1280550a7375SFelipe Balbi unsigned long flags; 1281550a7375SFelipe Balbi int status = 0; 1282550a7375SFelipe Balbi struct musb *musb = musb_ep->musb; 1283550a7375SFelipe Balbi 1284fc78003eSBin Liu if (!ep || !request || req->ep != musb_ep) 1285550a7375SFelipe Balbi return -EINVAL; 1286550a7375SFelipe Balbi 1287fc78003eSBin Liu trace_musb_req_deq(req); 1288fc78003eSBin Liu 1289550a7375SFelipe Balbi spin_lock_irqsave(&musb->lock, flags); 1290550a7375SFelipe Balbi 1291550a7375SFelipe Balbi list_for_each_entry(r, &musb_ep->req_list, list) { 12924cbbf084SFelipe Balbi if (r == req) 1293550a7375SFelipe Balbi break; 1294550a7375SFelipe Balbi } 12954cbbf084SFelipe Balbi if (r != req) { 1296b99d3659SBin Liu dev_err(musb->controller, "request %p not queued to %s\n", 1297b99d3659SBin Liu request, ep->name); 1298550a7375SFelipe Balbi status = -EINVAL; 1299550a7375SFelipe Balbi goto done; 1300550a7375SFelipe Balbi } 1301550a7375SFelipe Balbi 1302550a7375SFelipe Balbi /* if the hardware doesn't have the request, easy ... */ 13033d5ad13eSFelipe Balbi if (musb_ep->req_list.next != &req->list || musb_ep->busy) 1304550a7375SFelipe Balbi musb_g_giveback(musb_ep, request, -ECONNRESET); 1305550a7375SFelipe Balbi 1306550a7375SFelipe Balbi /* ... else abort the dma transfer ... */ 1307550a7375SFelipe Balbi else if (is_dma_capable() && musb_ep->dma) { 1308550a7375SFelipe Balbi struct dma_controller *c = musb->dma_controller; 1309550a7375SFelipe Balbi 1310550a7375SFelipe Balbi musb_ep_select(musb->mregs, musb_ep->current_epnum); 1311550a7375SFelipe Balbi if (c->channel_abort) 1312550a7375SFelipe Balbi status = c->channel_abort(musb_ep->dma); 1313550a7375SFelipe Balbi else 1314550a7375SFelipe Balbi status = -EBUSY; 1315550a7375SFelipe Balbi if (status == 0) 1316550a7375SFelipe Balbi musb_g_giveback(musb_ep, request, -ECONNRESET); 1317550a7375SFelipe Balbi } else { 1318550a7375SFelipe Balbi /* NOTE: by sticking to easily tested hardware/driver states, 1319550a7375SFelipe Balbi * we leave counting of in-flight packets imprecise. 1320550a7375SFelipe Balbi */ 1321550a7375SFelipe Balbi musb_g_giveback(musb_ep, request, -ECONNRESET); 1322550a7375SFelipe Balbi } 1323550a7375SFelipe Balbi 1324550a7375SFelipe Balbi done: 1325550a7375SFelipe Balbi spin_unlock_irqrestore(&musb->lock, flags); 1326550a7375SFelipe Balbi return status; 1327550a7375SFelipe Balbi } 1328550a7375SFelipe Balbi 1329550a7375SFelipe Balbi /* 1330550a7375SFelipe Balbi * Set or clear the halt bit of an endpoint. A halted enpoint won't tx/rx any 1331550a7375SFelipe Balbi * data but will queue requests. 1332550a7375SFelipe Balbi * 1333550a7375SFelipe Balbi * exported to ep0 code 1334550a7375SFelipe Balbi */ 13351b6c3b0fSFelipe Balbi static int musb_gadget_set_halt(struct usb_ep *ep, int value) 1336550a7375SFelipe Balbi { 1337550a7375SFelipe Balbi struct musb_ep *musb_ep = to_musb_ep(ep); 1338550a7375SFelipe Balbi u8 epnum = musb_ep->current_epnum; 1339550a7375SFelipe Balbi struct musb *musb = musb_ep->musb; 1340550a7375SFelipe Balbi void __iomem *epio = musb->endpoints[epnum].regs; 1341550a7375SFelipe Balbi void __iomem *mbase; 1342550a7375SFelipe Balbi unsigned long flags; 1343550a7375SFelipe Balbi u16 csr; 1344cea83241SSergei Shtylyov struct musb_request *request; 1345550a7375SFelipe Balbi int status = 0; 1346550a7375SFelipe Balbi 1347550a7375SFelipe Balbi if (!ep) 1348550a7375SFelipe Balbi return -EINVAL; 1349550a7375SFelipe Balbi mbase = musb->mregs; 1350550a7375SFelipe Balbi 1351550a7375SFelipe Balbi spin_lock_irqsave(&musb->lock, flags); 1352550a7375SFelipe Balbi 1353550a7375SFelipe Balbi if ((USB_ENDPOINT_XFER_ISOC == musb_ep->type)) { 1354550a7375SFelipe Balbi status = -EINVAL; 1355550a7375SFelipe Balbi goto done; 1356550a7375SFelipe Balbi } 1357550a7375SFelipe Balbi 1358550a7375SFelipe Balbi musb_ep_select(mbase, epnum); 1359550a7375SFelipe Balbi 1360ad1adb89SFelipe Balbi request = next_request(musb_ep); 1361cea83241SSergei Shtylyov if (value) { 1362cea83241SSergei Shtylyov if (request) { 1363b99d3659SBin Liu musb_dbg(musb, "request in progress, cannot halt %s", 1364cea83241SSergei Shtylyov ep->name); 1365cea83241SSergei Shtylyov status = -EAGAIN; 1366cea83241SSergei Shtylyov goto done; 1367cea83241SSergei Shtylyov } 1368cea83241SSergei Shtylyov /* Cannot portably stall with non-empty FIFO */ 1369cea83241SSergei Shtylyov if (musb_ep->is_in) { 1370550a7375SFelipe Balbi csr = musb_readw(epio, MUSB_TXCSR); 1371550a7375SFelipe Balbi if (csr & MUSB_TXCSR_FIFONOTEMPTY) { 1372b99d3659SBin Liu musb_dbg(musb, "FIFO busy, cannot halt %s", 1373b99d3659SBin Liu ep->name); 1374cea83241SSergei Shtylyov status = -EAGAIN; 1375cea83241SSergei Shtylyov goto done; 1376550a7375SFelipe Balbi } 1377cea83241SSergei Shtylyov } 137847e97605SSergei Shtylyov } else 137947e97605SSergei Shtylyov musb_ep->wedged = 0; 1380550a7375SFelipe Balbi 1381550a7375SFelipe Balbi /* set/clear the stall and toggle bits */ 1382b99d3659SBin Liu musb_dbg(musb, "%s: %s stall", ep->name, value ? "set" : "clear"); 1383550a7375SFelipe Balbi if (musb_ep->is_in) { 1384550a7375SFelipe Balbi csr = musb_readw(epio, MUSB_TXCSR); 1385550a7375SFelipe Balbi csr |= MUSB_TXCSR_P_WZC_BITS 1386550a7375SFelipe Balbi | MUSB_TXCSR_CLRDATATOG; 1387550a7375SFelipe Balbi if (value) 1388550a7375SFelipe Balbi csr |= MUSB_TXCSR_P_SENDSTALL; 1389550a7375SFelipe Balbi else 1390550a7375SFelipe Balbi csr &= ~(MUSB_TXCSR_P_SENDSTALL 1391550a7375SFelipe Balbi | MUSB_TXCSR_P_SENTSTALL); 1392550a7375SFelipe Balbi csr &= ~MUSB_TXCSR_TXPKTRDY; 1393550a7375SFelipe Balbi musb_writew(epio, MUSB_TXCSR, csr); 1394550a7375SFelipe Balbi } else { 1395550a7375SFelipe Balbi csr = musb_readw(epio, MUSB_RXCSR); 1396550a7375SFelipe Balbi csr |= MUSB_RXCSR_P_WZC_BITS 1397550a7375SFelipe Balbi | MUSB_RXCSR_FLUSHFIFO 1398550a7375SFelipe Balbi | MUSB_RXCSR_CLRDATATOG; 1399550a7375SFelipe Balbi if (value) 1400550a7375SFelipe Balbi csr |= MUSB_RXCSR_P_SENDSTALL; 1401550a7375SFelipe Balbi else 1402550a7375SFelipe Balbi csr &= ~(MUSB_RXCSR_P_SENDSTALL 1403550a7375SFelipe Balbi | MUSB_RXCSR_P_SENTSTALL); 1404550a7375SFelipe Balbi musb_writew(epio, MUSB_RXCSR, csr); 1405550a7375SFelipe Balbi } 1406550a7375SFelipe Balbi 1407550a7375SFelipe Balbi /* maybe start the first request in the queue */ 1408550a7375SFelipe Balbi if (!musb_ep->busy && !value && request) { 1409b99d3659SBin Liu musb_dbg(musb, "restarting the request"); 1410550a7375SFelipe Balbi musb_ep_restart(musb, request); 1411550a7375SFelipe Balbi } 1412550a7375SFelipe Balbi 1413cea83241SSergei Shtylyov done: 1414550a7375SFelipe Balbi spin_unlock_irqrestore(&musb->lock, flags); 1415550a7375SFelipe Balbi return status; 1416550a7375SFelipe Balbi } 1417550a7375SFelipe Balbi 141847e97605SSergei Shtylyov /* 141947e97605SSergei Shtylyov * Sets the halt feature with the clear requests ignored 142047e97605SSergei Shtylyov */ 14211b6c3b0fSFelipe Balbi static int musb_gadget_set_wedge(struct usb_ep *ep) 142247e97605SSergei Shtylyov { 142347e97605SSergei Shtylyov struct musb_ep *musb_ep = to_musb_ep(ep); 142447e97605SSergei Shtylyov 142547e97605SSergei Shtylyov if (!ep) 142647e97605SSergei Shtylyov return -EINVAL; 142747e97605SSergei Shtylyov 142847e97605SSergei Shtylyov musb_ep->wedged = 1; 142947e97605SSergei Shtylyov 143047e97605SSergei Shtylyov return usb_ep_set_halt(ep); 143147e97605SSergei Shtylyov } 143247e97605SSergei Shtylyov 1433550a7375SFelipe Balbi static int musb_gadget_fifo_status(struct usb_ep *ep) 1434550a7375SFelipe Balbi { 1435550a7375SFelipe Balbi struct musb_ep *musb_ep = to_musb_ep(ep); 1436550a7375SFelipe Balbi void __iomem *epio = musb_ep->hw_ep->regs; 1437550a7375SFelipe Balbi int retval = -EINVAL; 1438550a7375SFelipe Balbi 1439550a7375SFelipe Balbi if (musb_ep->desc && !musb_ep->is_in) { 1440550a7375SFelipe Balbi struct musb *musb = musb_ep->musb; 1441550a7375SFelipe Balbi int epnum = musb_ep->current_epnum; 1442550a7375SFelipe Balbi void __iomem *mbase = musb->mregs; 1443550a7375SFelipe Balbi unsigned long flags; 1444550a7375SFelipe Balbi 1445550a7375SFelipe Balbi spin_lock_irqsave(&musb->lock, flags); 1446550a7375SFelipe Balbi 1447550a7375SFelipe Balbi musb_ep_select(mbase, epnum); 1448550a7375SFelipe Balbi /* FIXME return zero unless RXPKTRDY is set */ 1449550a7375SFelipe Balbi retval = musb_readw(epio, MUSB_RXCOUNT); 1450550a7375SFelipe Balbi 1451550a7375SFelipe Balbi spin_unlock_irqrestore(&musb->lock, flags); 1452550a7375SFelipe Balbi } 1453550a7375SFelipe Balbi return retval; 1454550a7375SFelipe Balbi } 1455550a7375SFelipe Balbi 1456550a7375SFelipe Balbi static void musb_gadget_fifo_flush(struct usb_ep *ep) 1457550a7375SFelipe Balbi { 1458550a7375SFelipe Balbi struct musb_ep *musb_ep = to_musb_ep(ep); 1459550a7375SFelipe Balbi struct musb *musb = musb_ep->musb; 1460550a7375SFelipe Balbi u8 epnum = musb_ep->current_epnum; 1461550a7375SFelipe Balbi void __iomem *epio = musb->endpoints[epnum].regs; 1462550a7375SFelipe Balbi void __iomem *mbase; 1463550a7375SFelipe Balbi unsigned long flags; 1464b18d26f6SSebastian Andrzej Siewior u16 csr; 1465550a7375SFelipe Balbi 1466550a7375SFelipe Balbi mbase = musb->mregs; 1467550a7375SFelipe Balbi 1468550a7375SFelipe Balbi spin_lock_irqsave(&musb->lock, flags); 1469550a7375SFelipe Balbi musb_ep_select(mbase, (u8) epnum); 1470550a7375SFelipe Balbi 1471550a7375SFelipe Balbi /* disable interrupts */ 1472b18d26f6SSebastian Andrzej Siewior musb_writew(mbase, MUSB_INTRTXE, musb->intrtxe & ~(1 << epnum)); 1473550a7375SFelipe Balbi 1474550a7375SFelipe Balbi if (musb_ep->is_in) { 1475550a7375SFelipe Balbi csr = musb_readw(epio, MUSB_TXCSR); 1476550a7375SFelipe Balbi if (csr & MUSB_TXCSR_FIFONOTEMPTY) { 1477550a7375SFelipe Balbi csr |= MUSB_TXCSR_FLUSHFIFO | MUSB_TXCSR_P_WZC_BITS; 14784858f06eSYauheni Kaliuta /* 14794858f06eSYauheni Kaliuta * Setting both TXPKTRDY and FLUSHFIFO makes controller 14804858f06eSYauheni Kaliuta * to interrupt current FIFO loading, but not flushing 14814858f06eSYauheni Kaliuta * the already loaded ones. 14824858f06eSYauheni Kaliuta */ 14834858f06eSYauheni Kaliuta csr &= ~MUSB_TXCSR_TXPKTRDY; 1484550a7375SFelipe Balbi musb_writew(epio, MUSB_TXCSR, csr); 1485550a7375SFelipe Balbi /* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */ 1486550a7375SFelipe Balbi musb_writew(epio, MUSB_TXCSR, csr); 1487550a7375SFelipe Balbi } 1488550a7375SFelipe Balbi } else { 1489550a7375SFelipe Balbi csr = musb_readw(epio, MUSB_RXCSR); 1490550a7375SFelipe Balbi csr |= MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_P_WZC_BITS; 1491550a7375SFelipe Balbi musb_writew(epio, MUSB_RXCSR, csr); 1492550a7375SFelipe Balbi musb_writew(epio, MUSB_RXCSR, csr); 1493550a7375SFelipe Balbi } 1494550a7375SFelipe Balbi 1495550a7375SFelipe Balbi /* re-enable interrupt */ 1496b18d26f6SSebastian Andrzej Siewior musb_writew(mbase, MUSB_INTRTXE, musb->intrtxe); 1497550a7375SFelipe Balbi spin_unlock_irqrestore(&musb->lock, flags); 1498550a7375SFelipe Balbi } 1499550a7375SFelipe Balbi 1500550a7375SFelipe Balbi static const struct usb_ep_ops musb_ep_ops = { 1501550a7375SFelipe Balbi .enable = musb_gadget_enable, 1502550a7375SFelipe Balbi .disable = musb_gadget_disable, 1503550a7375SFelipe Balbi .alloc_request = musb_alloc_request, 1504550a7375SFelipe Balbi .free_request = musb_free_request, 1505550a7375SFelipe Balbi .queue = musb_gadget_queue, 1506550a7375SFelipe Balbi .dequeue = musb_gadget_dequeue, 1507550a7375SFelipe Balbi .set_halt = musb_gadget_set_halt, 150847e97605SSergei Shtylyov .set_wedge = musb_gadget_set_wedge, 1509550a7375SFelipe Balbi .fifo_status = musb_gadget_fifo_status, 1510550a7375SFelipe Balbi .fifo_flush = musb_gadget_fifo_flush 1511550a7375SFelipe Balbi }; 1512550a7375SFelipe Balbi 1513550a7375SFelipe Balbi /* ----------------------------------------------------------------------- */ 1514550a7375SFelipe Balbi 1515550a7375SFelipe Balbi static int musb_gadget_get_frame(struct usb_gadget *gadget) 1516550a7375SFelipe Balbi { 1517550a7375SFelipe Balbi struct musb *musb = gadget_to_musb(gadget); 1518550a7375SFelipe Balbi 1519550a7375SFelipe Balbi return (int)musb_readw(musb->mregs, MUSB_FRAME); 1520550a7375SFelipe Balbi } 1521550a7375SFelipe Balbi 1522550a7375SFelipe Balbi static int musb_gadget_wakeup(struct usb_gadget *gadget) 1523550a7375SFelipe Balbi { 1524550a7375SFelipe Balbi struct musb *musb = gadget_to_musb(gadget); 1525550a7375SFelipe Balbi void __iomem *mregs = musb->mregs; 1526550a7375SFelipe Balbi unsigned long flags; 1527550a7375SFelipe Balbi int status = -EINVAL; 1528550a7375SFelipe Balbi u8 power, devctl; 1529550a7375SFelipe Balbi int retries; 1530550a7375SFelipe Balbi 1531550a7375SFelipe Balbi spin_lock_irqsave(&musb->lock, flags); 1532550a7375SFelipe Balbi 1533e47d9254SAntoine Tenart switch (musb->xceiv->otg->state) { 1534550a7375SFelipe Balbi case OTG_STATE_B_PERIPHERAL: 1535550a7375SFelipe Balbi /* NOTE: OTG state machine doesn't include B_SUSPENDED; 1536550a7375SFelipe Balbi * that's part of the standard usb 1.1 state machine, and 1537550a7375SFelipe Balbi * doesn't affect OTG transitions. 1538550a7375SFelipe Balbi */ 1539550a7375SFelipe Balbi if (musb->may_wakeup && musb->is_suspended) 1540550a7375SFelipe Balbi break; 1541550a7375SFelipe Balbi goto done; 1542550a7375SFelipe Balbi case OTG_STATE_B_IDLE: 1543550a7375SFelipe Balbi /* Start SRP ... OTG not required. */ 1544550a7375SFelipe Balbi devctl = musb_readb(mregs, MUSB_DEVCTL); 1545b99d3659SBin Liu musb_dbg(musb, "Sending SRP: devctl: %02x", devctl); 1546550a7375SFelipe Balbi devctl |= MUSB_DEVCTL_SESSION; 1547550a7375SFelipe Balbi musb_writeb(mregs, MUSB_DEVCTL, devctl); 1548550a7375SFelipe Balbi devctl = musb_readb(mregs, MUSB_DEVCTL); 1549550a7375SFelipe Balbi retries = 100; 1550550a7375SFelipe Balbi while (!(devctl & MUSB_DEVCTL_SESSION)) { 1551550a7375SFelipe Balbi devctl = musb_readb(mregs, MUSB_DEVCTL); 1552550a7375SFelipe Balbi if (retries-- < 1) 1553550a7375SFelipe Balbi break; 1554550a7375SFelipe Balbi } 1555550a7375SFelipe Balbi retries = 10000; 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 15628620543eSHema HK spin_unlock_irqrestore(&musb->lock, flags); 15636e13c650SHeikki Krogerus otg_start_srp(musb->xceiv->otg); 15648620543eSHema HK spin_lock_irqsave(&musb->lock, flags); 15658620543eSHema HK 1566550a7375SFelipe Balbi /* Block idling for at least 1s */ 1567550a7375SFelipe Balbi musb_platform_try_idle(musb, 1568550a7375SFelipe Balbi jiffies + msecs_to_jiffies(1 * HZ)); 1569550a7375SFelipe Balbi 1570550a7375SFelipe Balbi status = 0; 1571550a7375SFelipe Balbi goto done; 1572550a7375SFelipe Balbi default: 1573b99d3659SBin Liu musb_dbg(musb, "Unhandled wake: %s", 1574e47d9254SAntoine Tenart usb_otg_state_string(musb->xceiv->otg->state)); 1575550a7375SFelipe Balbi goto done; 1576550a7375SFelipe Balbi } 1577550a7375SFelipe Balbi 1578550a7375SFelipe Balbi status = 0; 1579550a7375SFelipe Balbi 1580550a7375SFelipe Balbi power = musb_readb(mregs, MUSB_POWER); 1581550a7375SFelipe Balbi power |= MUSB_POWER_RESUME; 1582550a7375SFelipe Balbi musb_writeb(mregs, MUSB_POWER, power); 1583b99d3659SBin Liu musb_dbg(musb, "issue wakeup"); 1584550a7375SFelipe Balbi 1585550a7375SFelipe Balbi /* FIXME do this next chunk in a timer callback, no udelay */ 1586550a7375SFelipe Balbi mdelay(2); 1587550a7375SFelipe Balbi 1588550a7375SFelipe Balbi power = musb_readb(mregs, MUSB_POWER); 1589550a7375SFelipe Balbi power &= ~MUSB_POWER_RESUME; 1590550a7375SFelipe Balbi musb_writeb(mregs, MUSB_POWER, power); 1591550a7375SFelipe Balbi done: 1592550a7375SFelipe Balbi spin_unlock_irqrestore(&musb->lock, flags); 1593550a7375SFelipe Balbi return status; 1594550a7375SFelipe Balbi } 1595550a7375SFelipe Balbi 1596550a7375SFelipe Balbi static int 1597550a7375SFelipe Balbi musb_gadget_set_self_powered(struct usb_gadget *gadget, int is_selfpowered) 1598550a7375SFelipe Balbi { 1599dadac986SPeter Chen gadget->is_selfpowered = !!is_selfpowered; 1600550a7375SFelipe Balbi return 0; 1601550a7375SFelipe Balbi } 1602550a7375SFelipe Balbi 1603550a7375SFelipe Balbi static void musb_pullup(struct musb *musb, int is_on) 1604550a7375SFelipe Balbi { 1605550a7375SFelipe Balbi u8 power; 1606550a7375SFelipe Balbi 1607550a7375SFelipe Balbi power = musb_readb(musb->mregs, MUSB_POWER); 1608550a7375SFelipe Balbi if (is_on) 1609550a7375SFelipe Balbi power |= MUSB_POWER_SOFTCONN; 1610550a7375SFelipe Balbi else 1611550a7375SFelipe Balbi power &= ~MUSB_POWER_SOFTCONN; 1612550a7375SFelipe Balbi 1613550a7375SFelipe Balbi /* FIXME if on, HdrcStart; if off, HdrcStop */ 1614550a7375SFelipe Balbi 1615b99d3659SBin Liu musb_dbg(musb, "gadget D+ pullup %s", 1616e71eb392SSebastian Andrzej Siewior is_on ? "on" : "off"); 1617550a7375SFelipe Balbi musb_writeb(musb->mregs, MUSB_POWER, power); 1618550a7375SFelipe Balbi } 1619550a7375SFelipe Balbi 1620550a7375SFelipe Balbi #if 0 1621550a7375SFelipe Balbi static int musb_gadget_vbus_session(struct usb_gadget *gadget, int is_active) 1622550a7375SFelipe Balbi { 1623b99d3659SBin Liu musb_dbg(musb, "<= %s =>\n", __func__); 1624550a7375SFelipe Balbi 1625550a7375SFelipe Balbi /* 1626550a7375SFelipe Balbi * FIXME iff driver's softconnect flag is set (as it is during probe, 1627550a7375SFelipe Balbi * though that can clear it), just musb_pullup(). 1628550a7375SFelipe Balbi */ 1629550a7375SFelipe Balbi 1630550a7375SFelipe Balbi return -EINVAL; 1631550a7375SFelipe Balbi } 1632550a7375SFelipe Balbi #endif 1633550a7375SFelipe Balbi 1634550a7375SFelipe Balbi static int musb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA) 1635550a7375SFelipe Balbi { 1636550a7375SFelipe Balbi struct musb *musb = gadget_to_musb(gadget); 1637550a7375SFelipe Balbi 163884e250ffSDavid Brownell if (!musb->xceiv->set_power) 1639550a7375SFelipe Balbi return -EOPNOTSUPP; 1640b96d3b08SHeikki Krogerus return usb_phy_set_power(musb->xceiv, mA); 1641550a7375SFelipe Balbi } 1642550a7375SFelipe Balbi 1643517bafffSTony Lindgren static void musb_gadget_work(struct work_struct *work) 1644517bafffSTony Lindgren { 1645517bafffSTony Lindgren struct musb *musb; 1646517bafffSTony Lindgren unsigned long flags; 1647517bafffSTony Lindgren 1648517bafffSTony Lindgren musb = container_of(work, struct musb, gadget_work.work); 1649517bafffSTony Lindgren pm_runtime_get_sync(musb->controller); 1650517bafffSTony Lindgren spin_lock_irqsave(&musb->lock, flags); 1651517bafffSTony Lindgren musb_pullup(musb, musb->softconnect); 1652517bafffSTony Lindgren spin_unlock_irqrestore(&musb->lock, flags); 1653517bafffSTony Lindgren pm_runtime_mark_last_busy(musb->controller); 1654517bafffSTony Lindgren pm_runtime_put_autosuspend(musb->controller); 1655517bafffSTony Lindgren } 1656517bafffSTony Lindgren 1657550a7375SFelipe Balbi static int musb_gadget_pullup(struct usb_gadget *gadget, int is_on) 1658550a7375SFelipe Balbi { 1659550a7375SFelipe Balbi struct musb *musb = gadget_to_musb(gadget); 1660550a7375SFelipe Balbi unsigned long flags; 1661550a7375SFelipe Balbi 1662550a7375SFelipe Balbi is_on = !!is_on; 1663550a7375SFelipe Balbi 1664550a7375SFelipe Balbi /* NOTE: this assumes we are sensing vbus; we'd rather 1665550a7375SFelipe Balbi * not pullup unless the B-session is active. 1666550a7375SFelipe Balbi */ 1667550a7375SFelipe Balbi spin_lock_irqsave(&musb->lock, flags); 1668550a7375SFelipe Balbi if (is_on != musb->softconnect) { 1669550a7375SFelipe Balbi musb->softconnect = is_on; 1670517bafffSTony Lindgren schedule_delayed_work(&musb->gadget_work, 0); 1671550a7375SFelipe Balbi } 1672550a7375SFelipe Balbi spin_unlock_irqrestore(&musb->lock, flags); 167393e098a8SJohn Stultz 1674550a7375SFelipe Balbi return 0; 1675550a7375SFelipe Balbi } 1676550a7375SFelipe Balbi 1677e71eb392SSebastian Andrzej Siewior static int musb_gadget_start(struct usb_gadget *g, 1678e71eb392SSebastian Andrzej Siewior struct usb_gadget_driver *driver); 167922835b80SFelipe Balbi static int musb_gadget_stop(struct usb_gadget *g); 16800f91349bSSebastian Andrzej Siewior 1681550a7375SFelipe Balbi static const struct usb_gadget_ops musb_gadget_operations = { 1682550a7375SFelipe Balbi .get_frame = musb_gadget_get_frame, 1683550a7375SFelipe Balbi .wakeup = musb_gadget_wakeup, 1684550a7375SFelipe Balbi .set_selfpowered = musb_gadget_set_self_powered, 1685550a7375SFelipe Balbi /* .vbus_session = musb_gadget_vbus_session, */ 1686550a7375SFelipe Balbi .vbus_draw = musb_gadget_vbus_draw, 1687550a7375SFelipe Balbi .pullup = musb_gadget_pullup, 1688e71eb392SSebastian Andrzej Siewior .udc_start = musb_gadget_start, 1689e71eb392SSebastian Andrzej Siewior .udc_stop = musb_gadget_stop, 1690550a7375SFelipe Balbi }; 1691550a7375SFelipe Balbi 1692550a7375SFelipe Balbi /* ----------------------------------------------------------------------- */ 1693550a7375SFelipe Balbi 1694550a7375SFelipe Balbi /* Registration */ 1695550a7375SFelipe Balbi 1696550a7375SFelipe Balbi /* Only this registration code "knows" the rule (from USB standards) 1697550a7375SFelipe Balbi * about there being only one external upstream port. It assumes 1698550a7375SFelipe Balbi * all peripheral ports are external... 1699550a7375SFelipe Balbi */ 1700550a7375SFelipe Balbi 170141ac7b3aSBill Pemberton static void 1702550a7375SFelipe Balbi init_peripheral_ep(struct musb *musb, struct musb_ep *ep, u8 epnum, int is_in) 1703550a7375SFelipe Balbi { 1704550a7375SFelipe Balbi struct musb_hw_ep *hw_ep = musb->endpoints + epnum; 1705550a7375SFelipe Balbi 1706550a7375SFelipe Balbi memset(ep, 0, sizeof *ep); 1707550a7375SFelipe Balbi 1708550a7375SFelipe Balbi ep->current_epnum = epnum; 1709550a7375SFelipe Balbi ep->musb = musb; 1710550a7375SFelipe Balbi ep->hw_ep = hw_ep; 1711550a7375SFelipe Balbi ep->is_in = is_in; 1712550a7375SFelipe Balbi 1713550a7375SFelipe Balbi INIT_LIST_HEAD(&ep->req_list); 1714550a7375SFelipe Balbi 1715550a7375SFelipe Balbi sprintf(ep->name, "ep%d%s", epnum, 1716550a7375SFelipe Balbi (!epnum || hw_ep->is_shared_fifo) ? "" : ( 1717550a7375SFelipe Balbi is_in ? "in" : "out")); 1718550a7375SFelipe Balbi ep->end_point.name = ep->name; 1719550a7375SFelipe Balbi INIT_LIST_HEAD(&ep->end_point.ep_list); 1720550a7375SFelipe Balbi if (!epnum) { 1721e117e742SRobert Baldyga usb_ep_set_maxpacket_limit(&ep->end_point, 64); 17228501955eSRobert Baldyga ep->end_point.caps.type_control = true; 1723550a7375SFelipe Balbi ep->end_point.ops = &musb_g_ep0_ops; 1724550a7375SFelipe Balbi musb->g.ep0 = &ep->end_point; 1725550a7375SFelipe Balbi } else { 1726550a7375SFelipe Balbi if (is_in) 1727e117e742SRobert Baldyga usb_ep_set_maxpacket_limit(&ep->end_point, hw_ep->max_packet_sz_tx); 1728550a7375SFelipe Balbi else 1729e117e742SRobert Baldyga usb_ep_set_maxpacket_limit(&ep->end_point, hw_ep->max_packet_sz_rx); 17308501955eSRobert Baldyga ep->end_point.caps.type_iso = true; 17318501955eSRobert Baldyga ep->end_point.caps.type_bulk = true; 17328501955eSRobert Baldyga ep->end_point.caps.type_int = true; 1733550a7375SFelipe Balbi ep->end_point.ops = &musb_ep_ops; 1734550a7375SFelipe Balbi list_add_tail(&ep->end_point.ep_list, &musb->g.ep_list); 1735550a7375SFelipe Balbi } 17368501955eSRobert Baldyga 17378501955eSRobert Baldyga if (!epnum || hw_ep->is_shared_fifo) { 17388501955eSRobert Baldyga ep->end_point.caps.dir_in = true; 17398501955eSRobert Baldyga ep->end_point.caps.dir_out = true; 17408501955eSRobert Baldyga } else if (is_in) 17418501955eSRobert Baldyga ep->end_point.caps.dir_in = true; 17428501955eSRobert Baldyga else 17438501955eSRobert Baldyga ep->end_point.caps.dir_out = true; 1744550a7375SFelipe Balbi } 1745550a7375SFelipe Balbi 1746550a7375SFelipe Balbi /* 1747550a7375SFelipe Balbi * Initialize the endpoints exposed to peripheral drivers, with backlinks 1748550a7375SFelipe Balbi * to the rest of the driver state. 1749550a7375SFelipe Balbi */ 175041ac7b3aSBill Pemberton static inline void musb_g_init_endpoints(struct musb *musb) 1751550a7375SFelipe Balbi { 1752550a7375SFelipe Balbi u8 epnum; 1753550a7375SFelipe Balbi struct musb_hw_ep *hw_ep; 1754550a7375SFelipe Balbi unsigned count = 0; 1755550a7375SFelipe Balbi 1756b595076aSUwe Kleine-König /* initialize endpoint list just once */ 1757550a7375SFelipe Balbi INIT_LIST_HEAD(&(musb->g.ep_list)); 1758550a7375SFelipe Balbi 1759550a7375SFelipe Balbi for (epnum = 0, hw_ep = musb->endpoints; 1760550a7375SFelipe Balbi epnum < musb->nr_endpoints; 1761550a7375SFelipe Balbi epnum++, hw_ep++) { 1762550a7375SFelipe Balbi if (hw_ep->is_shared_fifo /* || !epnum */) { 1763550a7375SFelipe Balbi init_peripheral_ep(musb, &hw_ep->ep_in, epnum, 0); 1764550a7375SFelipe Balbi count++; 1765550a7375SFelipe Balbi } else { 1766550a7375SFelipe Balbi if (hw_ep->max_packet_sz_tx) { 1767550a7375SFelipe Balbi init_peripheral_ep(musb, &hw_ep->ep_in, 1768550a7375SFelipe Balbi epnum, 1); 1769550a7375SFelipe Balbi count++; 1770550a7375SFelipe Balbi } 1771550a7375SFelipe Balbi if (hw_ep->max_packet_sz_rx) { 1772550a7375SFelipe Balbi init_peripheral_ep(musb, &hw_ep->ep_out, 1773550a7375SFelipe Balbi epnum, 0); 1774550a7375SFelipe Balbi count++; 1775550a7375SFelipe Balbi } 1776550a7375SFelipe Balbi } 1777550a7375SFelipe Balbi } 1778550a7375SFelipe Balbi } 1779550a7375SFelipe Balbi 1780550a7375SFelipe Balbi /* called once during driver setup to initialize and link into 1781550a7375SFelipe Balbi * the driver model; memory is zeroed. 1782550a7375SFelipe Balbi */ 178341ac7b3aSBill Pemberton int musb_gadget_setup(struct musb *musb) 1784550a7375SFelipe Balbi { 1785550a7375SFelipe Balbi int status; 1786550a7375SFelipe Balbi 1787550a7375SFelipe Balbi /* REVISIT minor race: if (erroneously) setting up two 1788550a7375SFelipe Balbi * musb peripherals at the same time, only the bus lock 1789550a7375SFelipe Balbi * is probably held. 1790550a7375SFelipe Balbi */ 1791550a7375SFelipe Balbi 1792550a7375SFelipe Balbi musb->g.ops = &musb_gadget_operations; 1793d327ab5bSMichal Nazarewicz musb->g.max_speed = USB_SPEED_HIGH; 1794550a7375SFelipe Balbi musb->g.speed = USB_SPEED_UNKNOWN; 1795550a7375SFelipe Balbi 17961374a430SBin Liu MUSB_DEV_MODE(musb); 1797e47d9254SAntoine Tenart musb->xceiv->otg->state = OTG_STATE_B_IDLE; 17981374a430SBin Liu 1799550a7375SFelipe Balbi /* this "gadget" abstracts/virtualizes the controller */ 1800550a7375SFelipe Balbi musb->g.name = musb_driver_name; 1801*0a9134bdSBin Liu /* don't support otg protocols */ 1802fd3923a9SApelete Seketeli musb->g.is_otg = 0; 1803517bafffSTony Lindgren INIT_DELAYED_WORK(&musb->gadget_work, musb_gadget_work); 1804550a7375SFelipe Balbi musb_g_init_endpoints(musb); 1805550a7375SFelipe Balbi 1806550a7375SFelipe Balbi musb->is_active = 0; 1807550a7375SFelipe Balbi musb_platform_try_idle(musb, 0); 1808550a7375SFelipe Balbi 18090f91349bSSebastian Andrzej Siewior status = usb_add_gadget_udc(musb->controller, &musb->g); 18100f91349bSSebastian Andrzej Siewior if (status) 18110f91349bSSebastian Andrzej Siewior goto err; 18120f91349bSSebastian Andrzej Siewior 18130f91349bSSebastian Andrzej Siewior return 0; 18140f91349bSSebastian Andrzej Siewior err: 18156193d699SSebastian Andrzej Siewior musb->g.dev.parent = NULL; 18160f91349bSSebastian Andrzej Siewior device_unregister(&musb->g.dev); 1817550a7375SFelipe Balbi return status; 1818550a7375SFelipe Balbi } 1819550a7375SFelipe Balbi 1820550a7375SFelipe Balbi void musb_gadget_cleanup(struct musb *musb) 1821550a7375SFelipe Balbi { 18227ad76955SBin Liu if (musb->port_mode == MUSB_HOST) 182390474288SSebastian Andrzej Siewior return; 1824517bafffSTony Lindgren 1825517bafffSTony Lindgren cancel_delayed_work_sync(&musb->gadget_work); 18260f91349bSSebastian Andrzej Siewior usb_del_gadget_udc(&musb->g); 1827550a7375SFelipe Balbi } 1828550a7375SFelipe Balbi 1829550a7375SFelipe Balbi /* 1830550a7375SFelipe Balbi * Register the gadget driver. Used by gadget drivers when 1831550a7375SFelipe Balbi * registering themselves with the controller. 1832550a7375SFelipe Balbi * 1833550a7375SFelipe Balbi * -EINVAL something went wrong (not driver) 1834550a7375SFelipe Balbi * -EBUSY another gadget is already using the controller 1835b595076aSUwe Kleine-König * -ENOMEM no memory to perform the operation 1836550a7375SFelipe Balbi * 1837550a7375SFelipe Balbi * @param driver the gadget driver 1838550a7375SFelipe Balbi * @return <0 if error, 0 if everything is fine 1839550a7375SFelipe Balbi */ 1840e71eb392SSebastian Andrzej Siewior static int musb_gadget_start(struct usb_gadget *g, 1841e71eb392SSebastian Andrzej Siewior struct usb_gadget_driver *driver) 1842550a7375SFelipe Balbi { 1843e71eb392SSebastian Andrzej Siewior struct musb *musb = gadget_to_musb(g); 1844d445b6daSHeikki Krogerus struct usb_otg *otg = musb->xceiv->otg; 184563eed2b5SFelipe Balbi unsigned long flags; 1846032ec49fSFelipe Balbi int retval = 0; 1847550a7375SFelipe Balbi 1848032ec49fSFelipe Balbi if (driver->max_speed < USB_SPEED_HIGH) { 1849032ec49fSFelipe Balbi retval = -EINVAL; 1850032ec49fSFelipe Balbi goto err; 1851032ec49fSFelipe Balbi } 1852550a7375SFelipe Balbi 18537acc6197SHema HK pm_runtime_get_sync(musb->controller); 18547acc6197SHema HK 1855e71eb392SSebastian Andrzej Siewior musb->softconnect = 0; 1856550a7375SFelipe Balbi musb->gadget_driver = driver; 1857550a7375SFelipe Balbi 1858550a7375SFelipe Balbi spin_lock_irqsave(&musb->lock, flags); 185943e699ceSGreg Kroah-Hartman musb->is_active = 1; 1860550a7375SFelipe Balbi 18616e13c650SHeikki Krogerus otg_set_peripheral(otg, &musb->g); 1862e47d9254SAntoine Tenart musb->xceiv->otg->state = OTG_STATE_B_IDLE; 1863550a7375SFelipe Balbi spin_unlock_irqrestore(&musb->lock, flags); 1864550a7375SFelipe Balbi 1865001dd84aSSebastian Andrzej Siewior musb_start(musb); 1866001dd84aSSebastian Andrzej Siewior 1867550a7375SFelipe Balbi /* REVISIT: funcall to other code, which also 1868550a7375SFelipe Balbi * handles power budgeting ... this way also 1869550a7375SFelipe Balbi * ensures HdrcStart is indirectly called. 1870550a7375SFelipe Balbi */ 1871b65ae0f1SGrazvydas Ignotas if (musb->xceiv->last_event == USB_EVENT_ID) 1872b65ae0f1SGrazvydas Ignotas musb_platform_set_vbus(musb, 1); 1873550a7375SFelipe Balbi 187430647217STony Lindgren pm_runtime_mark_last_busy(musb->controller); 187530647217STony Lindgren pm_runtime_put_autosuspend(musb->controller); 18767acc6197SHema HK 187763eed2b5SFelipe Balbi return 0; 187863eed2b5SFelipe Balbi 1879032ec49fSFelipe Balbi err: 1880550a7375SFelipe Balbi return retval; 1881550a7375SFelipe Balbi } 1882550a7375SFelipe Balbi 1883550a7375SFelipe Balbi /* 1884550a7375SFelipe Balbi * Unregister the gadget driver. Used by gadget drivers when 1885550a7375SFelipe Balbi * unregistering themselves from the controller. 1886550a7375SFelipe Balbi * 1887550a7375SFelipe Balbi * @param driver the gadget driver to unregister 1888550a7375SFelipe Balbi */ 188922835b80SFelipe Balbi static int musb_gadget_stop(struct usb_gadget *g) 1890550a7375SFelipe Balbi { 1891e71eb392SSebastian Andrzej Siewior struct musb *musb = gadget_to_musb(g); 189263eed2b5SFelipe Balbi unsigned long flags; 1893550a7375SFelipe Balbi 18947acc6197SHema HK pm_runtime_get_sync(musb->controller); 18957acc6197SHema HK 189663eed2b5SFelipe Balbi /* 189763eed2b5SFelipe Balbi * REVISIT always use otg_set_peripheral() here too; 1898550a7375SFelipe Balbi * this needs to shut down the OTG engine. 1899550a7375SFelipe Balbi */ 1900550a7375SFelipe Balbi 1901550a7375SFelipe Balbi spin_lock_irqsave(&musb->lock, flags); 1902550a7375SFelipe Balbi 1903550a7375SFelipe Balbi musb_hnp_stop(musb); 1904550a7375SFelipe Balbi 1905550a7375SFelipe Balbi (void) musb_gadget_vbus_draw(&musb->g, 0); 1906550a7375SFelipe Balbi 1907e47d9254SAntoine Tenart musb->xceiv->otg->state = OTG_STATE_UNDEFINED; 1908d5638fcfSFelipe Balbi musb_stop(musb); 19096e13c650SHeikki Krogerus otg_set_peripheral(musb->xceiv->otg, NULL); 1910550a7375SFelipe Balbi 1911550a7375SFelipe Balbi musb->is_active = 0; 1912e21de10cSGrazvydas Ignotas musb->gadget_driver = NULL; 1913550a7375SFelipe Balbi musb_platform_try_idle(musb, 0); 1914550a7375SFelipe Balbi spin_unlock_irqrestore(&musb->lock, flags); 1915550a7375SFelipe Balbi 1916032ec49fSFelipe Balbi /* 1917032ec49fSFelipe Balbi * FIXME we need to be able to register another 1918550a7375SFelipe Balbi * gadget driver here and have everything work; 1919550a7375SFelipe Balbi * that currently misbehaves. 1920550a7375SFelipe Balbi */ 192163eed2b5SFelipe Balbi 19224e719183STony Lindgren /* Force check of devctl register for PM runtime */ 19232bff3916STony Lindgren schedule_delayed_work(&musb->irq_work, 0); 19244e719183STony Lindgren 19257099dbc5STony Lindgren pm_runtime_mark_last_busy(musb->controller); 19267099dbc5STony Lindgren pm_runtime_put_autosuspend(musb->controller); 19277acc6197SHema HK 192863eed2b5SFelipe Balbi return 0; 1929550a7375SFelipe Balbi } 1930550a7375SFelipe Balbi 1931550a7375SFelipe Balbi /* ----------------------------------------------------------------------- */ 1932550a7375SFelipe Balbi 1933550a7375SFelipe Balbi /* lifecycle operations called through plat_uds.c */ 1934550a7375SFelipe Balbi 1935550a7375SFelipe Balbi void musb_g_resume(struct musb *musb) 1936550a7375SFelipe Balbi { 1937550a7375SFelipe Balbi musb->is_suspended = 0; 1938e47d9254SAntoine Tenart switch (musb->xceiv->otg->state) { 1939550a7375SFelipe Balbi case OTG_STATE_B_IDLE: 1940550a7375SFelipe Balbi break; 1941550a7375SFelipe Balbi case OTG_STATE_B_WAIT_ACON: 1942550a7375SFelipe Balbi case OTG_STATE_B_PERIPHERAL: 1943550a7375SFelipe Balbi musb->is_active = 1; 1944550a7375SFelipe Balbi if (musb->gadget_driver && musb->gadget_driver->resume) { 1945550a7375SFelipe Balbi spin_unlock(&musb->lock); 1946550a7375SFelipe Balbi musb->gadget_driver->resume(&musb->g); 1947550a7375SFelipe Balbi spin_lock(&musb->lock); 1948550a7375SFelipe Balbi } 1949550a7375SFelipe Balbi break; 1950550a7375SFelipe Balbi default: 1951550a7375SFelipe Balbi WARNING("unhandled RESUME transition (%s)\n", 1952e47d9254SAntoine Tenart usb_otg_state_string(musb->xceiv->otg->state)); 1953550a7375SFelipe Balbi } 1954550a7375SFelipe Balbi } 1955550a7375SFelipe Balbi 1956550a7375SFelipe Balbi /* called when SOF packets stop for 3+ msec */ 1957550a7375SFelipe Balbi void musb_g_suspend(struct musb *musb) 1958550a7375SFelipe Balbi { 1959550a7375SFelipe Balbi u8 devctl; 1960550a7375SFelipe Balbi 1961550a7375SFelipe Balbi devctl = musb_readb(musb->mregs, MUSB_DEVCTL); 1962b99d3659SBin Liu musb_dbg(musb, "musb_g_suspend: devctl %02x", devctl); 1963550a7375SFelipe Balbi 1964e47d9254SAntoine Tenart switch (musb->xceiv->otg->state) { 1965550a7375SFelipe Balbi case OTG_STATE_B_IDLE: 1966550a7375SFelipe Balbi if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS) 1967e47d9254SAntoine Tenart musb->xceiv->otg->state = OTG_STATE_B_PERIPHERAL; 1968550a7375SFelipe Balbi break; 1969550a7375SFelipe Balbi case OTG_STATE_B_PERIPHERAL: 1970550a7375SFelipe Balbi musb->is_suspended = 1; 1971550a7375SFelipe Balbi if (musb->gadget_driver && musb->gadget_driver->suspend) { 1972550a7375SFelipe Balbi spin_unlock(&musb->lock); 1973550a7375SFelipe Balbi musb->gadget_driver->suspend(&musb->g); 1974550a7375SFelipe Balbi spin_lock(&musb->lock); 1975550a7375SFelipe Balbi } 1976550a7375SFelipe Balbi break; 1977550a7375SFelipe Balbi default: 1978550a7375SFelipe Balbi /* REVISIT if B_HOST, clear DEVCTL.HOSTREQ; 1979550a7375SFelipe Balbi * A_PERIPHERAL may need care too 1980550a7375SFelipe Balbi */ 1981b99d3659SBin Liu WARNING("unhandled SUSPEND transition (%s)", 1982e47d9254SAntoine Tenart usb_otg_state_string(musb->xceiv->otg->state)); 1983550a7375SFelipe Balbi } 1984550a7375SFelipe Balbi } 1985550a7375SFelipe Balbi 1986550a7375SFelipe Balbi /* Called during SRP */ 1987550a7375SFelipe Balbi void musb_g_wakeup(struct musb *musb) 1988550a7375SFelipe Balbi { 1989550a7375SFelipe Balbi musb_gadget_wakeup(&musb->g); 1990550a7375SFelipe Balbi } 1991550a7375SFelipe Balbi 1992550a7375SFelipe Balbi /* called when VBUS drops below session threshold, and in other cases */ 1993550a7375SFelipe Balbi void musb_g_disconnect(struct musb *musb) 1994550a7375SFelipe Balbi { 1995550a7375SFelipe Balbi void __iomem *mregs = musb->mregs; 1996550a7375SFelipe Balbi u8 devctl = musb_readb(mregs, MUSB_DEVCTL); 1997550a7375SFelipe Balbi 1998b99d3659SBin Liu musb_dbg(musb, "musb_g_disconnect: devctl %02x", devctl); 1999550a7375SFelipe Balbi 2000550a7375SFelipe Balbi /* clear HR */ 2001550a7375SFelipe Balbi musb_writeb(mregs, MUSB_DEVCTL, devctl & MUSB_DEVCTL_SESSION); 2002550a7375SFelipe Balbi 2003550a7375SFelipe Balbi /* don't draw vbus until new b-default session */ 2004550a7375SFelipe Balbi (void) musb_gadget_vbus_draw(&musb->g, 0); 2005550a7375SFelipe Balbi 2006550a7375SFelipe Balbi musb->g.speed = USB_SPEED_UNKNOWN; 2007550a7375SFelipe Balbi if (musb->gadget_driver && musb->gadget_driver->disconnect) { 2008550a7375SFelipe Balbi spin_unlock(&musb->lock); 2009550a7375SFelipe Balbi musb->gadget_driver->disconnect(&musb->g); 2010550a7375SFelipe Balbi spin_lock(&musb->lock); 2011550a7375SFelipe Balbi } 2012550a7375SFelipe Balbi 2013e47d9254SAntoine Tenart switch (musb->xceiv->otg->state) { 2014550a7375SFelipe Balbi default: 2015b99d3659SBin Liu musb_dbg(musb, "Unhandled disconnect %s, setting a_idle", 2016e47d9254SAntoine Tenart usb_otg_state_string(musb->xceiv->otg->state)); 2017e47d9254SAntoine Tenart musb->xceiv->otg->state = OTG_STATE_A_IDLE; 2018ab983f2aSDavid Brownell MUSB_HST_MODE(musb); 2019550a7375SFelipe Balbi break; 2020550a7375SFelipe Balbi case OTG_STATE_A_PERIPHERAL: 2021e47d9254SAntoine Tenart musb->xceiv->otg->state = OTG_STATE_A_WAIT_BCON; 2022ab983f2aSDavid Brownell MUSB_HST_MODE(musb); 2023550a7375SFelipe Balbi break; 2024550a7375SFelipe Balbi case OTG_STATE_B_WAIT_ACON: 2025550a7375SFelipe Balbi case OTG_STATE_B_HOST: 2026550a7375SFelipe Balbi case OTG_STATE_B_PERIPHERAL: 2027550a7375SFelipe Balbi case OTG_STATE_B_IDLE: 2028e47d9254SAntoine Tenart musb->xceiv->otg->state = OTG_STATE_B_IDLE; 2029550a7375SFelipe Balbi break; 2030550a7375SFelipe Balbi case OTG_STATE_B_SRP_INIT: 2031550a7375SFelipe Balbi break; 2032550a7375SFelipe Balbi } 2033550a7375SFelipe Balbi 2034550a7375SFelipe Balbi musb->is_active = 0; 2035550a7375SFelipe Balbi } 2036550a7375SFelipe Balbi 2037550a7375SFelipe Balbi void musb_g_reset(struct musb *musb) 2038550a7375SFelipe Balbi __releases(musb->lock) 2039550a7375SFelipe Balbi __acquires(musb->lock) 2040550a7375SFelipe Balbi { 2041550a7375SFelipe Balbi void __iomem *mbase = musb->mregs; 2042550a7375SFelipe Balbi u8 devctl = musb_readb(mbase, MUSB_DEVCTL); 2043550a7375SFelipe Balbi u8 power; 2044550a7375SFelipe Balbi 2045b99d3659SBin Liu musb_dbg(musb, "<== %s driver '%s'", 2046550a7375SFelipe Balbi (devctl & MUSB_DEVCTL_BDEVICE) 2047550a7375SFelipe Balbi ? "B-Device" : "A-Device", 2048550a7375SFelipe Balbi musb->gadget_driver 2049550a7375SFelipe Balbi ? musb->gadget_driver->driver.name 2050550a7375SFelipe Balbi : NULL 2051550a7375SFelipe Balbi ); 2052550a7375SFelipe Balbi 20531189f7f6SFelipe Balbi /* report reset, if we didn't already (flushing EP state) */ 20541189f7f6SFelipe Balbi if (musb->gadget_driver && musb->g.speed != USB_SPEED_UNKNOWN) { 20551189f7f6SFelipe Balbi spin_unlock(&musb->lock); 20561189f7f6SFelipe Balbi usb_gadget_udc_reset(&musb->g, musb->gadget_driver); 20571189f7f6SFelipe Balbi spin_lock(&musb->lock); 20581189f7f6SFelipe Balbi } 2059550a7375SFelipe Balbi 2060550a7375SFelipe Balbi /* clear HR */ 2061550a7375SFelipe Balbi else if (devctl & MUSB_DEVCTL_HR) 2062550a7375SFelipe Balbi musb_writeb(mbase, MUSB_DEVCTL, MUSB_DEVCTL_SESSION); 2063550a7375SFelipe Balbi 2064550a7375SFelipe Balbi 2065550a7375SFelipe Balbi /* what speed did we negotiate? */ 2066550a7375SFelipe Balbi power = musb_readb(mbase, MUSB_POWER); 2067550a7375SFelipe Balbi musb->g.speed = (power & MUSB_POWER_HSMODE) 2068550a7375SFelipe Balbi ? USB_SPEED_HIGH : USB_SPEED_FULL; 2069550a7375SFelipe Balbi 2070550a7375SFelipe Balbi /* start in USB_STATE_DEFAULT */ 2071550a7375SFelipe Balbi musb->is_active = 1; 2072550a7375SFelipe Balbi musb->is_suspended = 0; 2073550a7375SFelipe Balbi MUSB_DEV_MODE(musb); 2074550a7375SFelipe Balbi musb->address = 0; 2075550a7375SFelipe Balbi musb->ep0_state = MUSB_EP0_STAGE_SETUP; 2076550a7375SFelipe Balbi 2077550a7375SFelipe Balbi musb->may_wakeup = 0; 2078550a7375SFelipe Balbi musb->g.b_hnp_enable = 0; 2079550a7375SFelipe Balbi musb->g.a_alt_hnp_support = 0; 2080550a7375SFelipe Balbi musb->g.a_hnp_support = 0; 2081ca1023c8SRobert Baldyga musb->g.quirk_zlp_not_supp = 1; 2082550a7375SFelipe Balbi 2083550a7375SFelipe Balbi /* Normal reset, as B-Device; 2084550a7375SFelipe Balbi * or else after HNP, as A-Device 2085550a7375SFelipe Balbi */ 208623db9fd2SApelete Seketeli if (!musb->g.is_otg) { 208723db9fd2SApelete Seketeli /* USB device controllers that are not OTG compatible 208823db9fd2SApelete Seketeli * may not have DEVCTL register in silicon. 208923db9fd2SApelete Seketeli * In that case, do not rely on devctl for setting 209023db9fd2SApelete Seketeli * peripheral mode. 209123db9fd2SApelete Seketeli */ 2092e47d9254SAntoine Tenart musb->xceiv->otg->state = OTG_STATE_B_PERIPHERAL; 209323db9fd2SApelete Seketeli musb->g.is_a_peripheral = 0; 209423db9fd2SApelete Seketeli } else if (devctl & MUSB_DEVCTL_BDEVICE) { 2095e47d9254SAntoine Tenart musb->xceiv->otg->state = OTG_STATE_B_PERIPHERAL; 2096550a7375SFelipe Balbi musb->g.is_a_peripheral = 0; 2097032ec49fSFelipe Balbi } else { 2098e47d9254SAntoine Tenart musb->xceiv->otg->state = OTG_STATE_A_PERIPHERAL; 2099550a7375SFelipe Balbi musb->g.is_a_peripheral = 1; 2100032ec49fSFelipe Balbi } 2101550a7375SFelipe Balbi 2102550a7375SFelipe Balbi /* start with default limits on VBUS power draw */ 2103032ec49fSFelipe Balbi (void) musb_gadget_vbus_draw(&musb->g, 8); 2104550a7375SFelipe Balbi } 2105