1550a7375SFelipe Balbi /* 2550a7375SFelipe Balbi * MUSB OTG driver peripheral support 3550a7375SFelipe Balbi * 4550a7375SFelipe Balbi * Copyright 2005 Mentor Graphics Corporation 5550a7375SFelipe Balbi * Copyright (C) 2005-2006 by Texas Instruments 6550a7375SFelipe Balbi * Copyright (C) 2006-2007 Nokia Corporation 7cea83241SSergei Shtylyov * Copyright (C) 2009 MontaVista Software, Inc. <source@mvista.com> 8550a7375SFelipe Balbi * 9550a7375SFelipe Balbi * This program is free software; you can redistribute it and/or 10550a7375SFelipe Balbi * modify it under the terms of the GNU General Public License 11550a7375SFelipe Balbi * version 2 as published by the Free Software Foundation. 12550a7375SFelipe Balbi * 13550a7375SFelipe Balbi * This program is distributed in the hope that it will be useful, but 14550a7375SFelipe Balbi * WITHOUT ANY WARRANTY; without even the implied warranty of 15550a7375SFelipe Balbi * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16550a7375SFelipe Balbi * General Public License for more details. 17550a7375SFelipe Balbi * 18550a7375SFelipe Balbi * You should have received a copy of the GNU General Public License 19550a7375SFelipe Balbi * along with this program; if not, write to the Free Software 20550a7375SFelipe Balbi * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 21550a7375SFelipe Balbi * 02110-1301 USA 22550a7375SFelipe Balbi * 23550a7375SFelipe Balbi * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED 24550a7375SFelipe Balbi * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 25550a7375SFelipe Balbi * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 26550a7375SFelipe Balbi * NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, 27550a7375SFelipe Balbi * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 28550a7375SFelipe Balbi * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 29550a7375SFelipe Balbi * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 30550a7375SFelipe Balbi * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31550a7375SFelipe Balbi * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 32550a7375SFelipe Balbi * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33550a7375SFelipe Balbi * 34550a7375SFelipe Balbi */ 35550a7375SFelipe Balbi 36550a7375SFelipe Balbi #include <linux/kernel.h> 37550a7375SFelipe Balbi #include <linux/list.h> 38550a7375SFelipe Balbi #include <linux/timer.h> 39550a7375SFelipe Balbi #include <linux/module.h> 40550a7375SFelipe Balbi #include <linux/smp.h> 41550a7375SFelipe Balbi #include <linux/spinlock.h> 42550a7375SFelipe Balbi #include <linux/delay.h> 43550a7375SFelipe Balbi #include <linux/moduleparam.h> 44550a7375SFelipe Balbi #include <linux/stat.h> 45550a7375SFelipe Balbi #include <linux/dma-mapping.h> 465a0e3ad6STejun Heo #include <linux/slab.h> 47550a7375SFelipe Balbi 48550a7375SFelipe Balbi #include "musb_core.h" 49550a7375SFelipe Balbi 50550a7375SFelipe Balbi 51550a7375SFelipe Balbi /* MUSB PERIPHERAL status 3-mar-2006: 52550a7375SFelipe Balbi * 53550a7375SFelipe Balbi * - EP0 seems solid. It passes both USBCV and usbtest control cases. 54550a7375SFelipe Balbi * Minor glitches: 55550a7375SFelipe Balbi * 56550a7375SFelipe Balbi * + remote wakeup to Linux hosts work, but saw USBCV failures; 57550a7375SFelipe Balbi * in one test run (operator error?) 58550a7375SFelipe Balbi * + endpoint halt tests -- in both usbtest and usbcv -- seem 59550a7375SFelipe Balbi * to break when dma is enabled ... is something wrongly 60550a7375SFelipe Balbi * clearing SENDSTALL? 61550a7375SFelipe Balbi * 62550a7375SFelipe Balbi * - Mass storage behaved ok when last tested. Network traffic patterns 63550a7375SFelipe Balbi * (with lots of short transfers etc) need retesting; they turn up the 64550a7375SFelipe Balbi * worst cases of the DMA, since short packets are typical but are not 65550a7375SFelipe Balbi * required. 66550a7375SFelipe Balbi * 67550a7375SFelipe Balbi * - TX/IN 68550a7375SFelipe Balbi * + both pio and dma behave in with network and g_zero tests 69550a7375SFelipe Balbi * + no cppi throughput issues other than no-hw-queueing 70550a7375SFelipe Balbi * + failed with FLAT_REG (DaVinci) 71550a7375SFelipe Balbi * + seems to behave with double buffering, PIO -and- CPPI 72550a7375SFelipe Balbi * + with gadgetfs + AIO, requests got lost? 73550a7375SFelipe Balbi * 74550a7375SFelipe Balbi * - RX/OUT 75550a7375SFelipe Balbi * + both pio and dma behave in with network and g_zero tests 76550a7375SFelipe Balbi * + dma is slow in typical case (short_not_ok is clear) 77550a7375SFelipe Balbi * + double buffering ok with PIO 78550a7375SFelipe Balbi * + double buffering *FAILS* with CPPI, wrong data bytes sometimes 79550a7375SFelipe Balbi * + request lossage observed with gadgetfs 80550a7375SFelipe Balbi * 81550a7375SFelipe Balbi * - ISO not tested ... might work, but only weakly isochronous 82550a7375SFelipe Balbi * 83550a7375SFelipe Balbi * - Gadget driver disabling of softconnect during bind() is ignored; so 84550a7375SFelipe Balbi * drivers can't hold off host requests until userspace is ready. 85550a7375SFelipe Balbi * (Workaround: they can turn it off later.) 86550a7375SFelipe Balbi * 87550a7375SFelipe Balbi * - PORTABILITY (assumes PIO works): 88550a7375SFelipe Balbi * + DaVinci, basically works with cppi dma 89550a7375SFelipe Balbi * + OMAP 2430, ditto with mentor dma 90550a7375SFelipe Balbi * + TUSB 6010, platform-specific dma in the works 91550a7375SFelipe Balbi */ 92550a7375SFelipe Balbi 93550a7375SFelipe Balbi /* ----------------------------------------------------------------------- */ 94550a7375SFelipe Balbi 95550a7375SFelipe Balbi /* 96550a7375SFelipe Balbi * Immediately complete a request. 97550a7375SFelipe Balbi * 98550a7375SFelipe Balbi * @param request the request to complete 99550a7375SFelipe Balbi * @param status the status to complete the request with 100550a7375SFelipe Balbi * Context: controller locked, IRQs blocked. 101550a7375SFelipe Balbi */ 102550a7375SFelipe Balbi void musb_g_giveback( 103550a7375SFelipe Balbi struct musb_ep *ep, 104550a7375SFelipe Balbi struct usb_request *request, 105550a7375SFelipe Balbi int status) 106550a7375SFelipe Balbi __releases(ep->musb->lock) 107550a7375SFelipe Balbi __acquires(ep->musb->lock) 108550a7375SFelipe Balbi { 109550a7375SFelipe Balbi struct musb_request *req; 110550a7375SFelipe Balbi struct musb *musb; 111550a7375SFelipe Balbi int busy = ep->busy; 112550a7375SFelipe Balbi 113550a7375SFelipe Balbi req = to_musb_request(request); 114550a7375SFelipe Balbi 115550a7375SFelipe Balbi list_del(&request->list); 116550a7375SFelipe Balbi if (req->request.status == -EINPROGRESS) 117550a7375SFelipe Balbi req->request.status = status; 118550a7375SFelipe Balbi musb = req->musb; 119550a7375SFelipe Balbi 120550a7375SFelipe Balbi ep->busy = 1; 121550a7375SFelipe Balbi spin_unlock(&musb->lock); 122550a7375SFelipe Balbi if (is_dma_capable()) { 123550a7375SFelipe Balbi if (req->mapped) { 124550a7375SFelipe Balbi dma_unmap_single(musb->controller, 125550a7375SFelipe Balbi req->request.dma, 126550a7375SFelipe Balbi req->request.length, 127550a7375SFelipe Balbi req->tx 128550a7375SFelipe Balbi ? DMA_TO_DEVICE 129550a7375SFelipe Balbi : DMA_FROM_DEVICE); 130550a7375SFelipe Balbi req->request.dma = DMA_ADDR_INVALID; 131550a7375SFelipe Balbi req->mapped = 0; 132550a7375SFelipe Balbi } else if (req->request.dma != DMA_ADDR_INVALID) 133550a7375SFelipe Balbi dma_sync_single_for_cpu(musb->controller, 134550a7375SFelipe Balbi req->request.dma, 135550a7375SFelipe Balbi req->request.length, 136550a7375SFelipe Balbi req->tx 137550a7375SFelipe Balbi ? DMA_TO_DEVICE 138550a7375SFelipe Balbi : DMA_FROM_DEVICE); 139550a7375SFelipe Balbi } 140550a7375SFelipe Balbi if (request->status == 0) 141550a7375SFelipe Balbi DBG(5, "%s done request %p, %d/%d\n", 142550a7375SFelipe Balbi ep->end_point.name, request, 143550a7375SFelipe Balbi req->request.actual, req->request.length); 144550a7375SFelipe Balbi else 145550a7375SFelipe Balbi DBG(2, "%s request %p, %d/%d fault %d\n", 146550a7375SFelipe Balbi ep->end_point.name, request, 147550a7375SFelipe Balbi req->request.actual, req->request.length, 148550a7375SFelipe Balbi request->status); 149550a7375SFelipe Balbi req->request.complete(&req->ep->end_point, &req->request); 150550a7375SFelipe Balbi spin_lock(&musb->lock); 151550a7375SFelipe Balbi ep->busy = busy; 152550a7375SFelipe Balbi } 153550a7375SFelipe Balbi 154550a7375SFelipe Balbi /* ----------------------------------------------------------------------- */ 155550a7375SFelipe Balbi 156550a7375SFelipe Balbi /* 157550a7375SFelipe Balbi * Abort requests queued to an endpoint using the status. Synchronous. 158550a7375SFelipe Balbi * caller locked controller and blocked irqs, and selected this ep. 159550a7375SFelipe Balbi */ 160550a7375SFelipe Balbi static void nuke(struct musb_ep *ep, const int status) 161550a7375SFelipe Balbi { 162550a7375SFelipe Balbi struct musb_request *req = NULL; 163550a7375SFelipe Balbi void __iomem *epio = ep->musb->endpoints[ep->current_epnum].regs; 164550a7375SFelipe Balbi 165550a7375SFelipe Balbi ep->busy = 1; 166550a7375SFelipe Balbi 167550a7375SFelipe Balbi if (is_dma_capable() && ep->dma) { 168550a7375SFelipe Balbi struct dma_controller *c = ep->musb->dma_controller; 169550a7375SFelipe Balbi int value; 170b6e434a5SSergei Shtylyov 171550a7375SFelipe Balbi if (ep->is_in) { 172b6e434a5SSergei Shtylyov /* 173b6e434a5SSergei Shtylyov * The programming guide says that we must not clear 174b6e434a5SSergei Shtylyov * the DMAMODE bit before DMAENAB, so we only 175b6e434a5SSergei Shtylyov * clear it in the second write... 176b6e434a5SSergei Shtylyov */ 177550a7375SFelipe Balbi musb_writew(epio, MUSB_TXCSR, 178b6e434a5SSergei Shtylyov MUSB_TXCSR_DMAMODE | MUSB_TXCSR_FLUSHFIFO); 179550a7375SFelipe Balbi musb_writew(epio, MUSB_TXCSR, 180550a7375SFelipe Balbi 0 | MUSB_TXCSR_FLUSHFIFO); 181550a7375SFelipe Balbi } else { 182550a7375SFelipe Balbi musb_writew(epio, MUSB_RXCSR, 183550a7375SFelipe Balbi 0 | MUSB_RXCSR_FLUSHFIFO); 184550a7375SFelipe Balbi musb_writew(epio, MUSB_RXCSR, 185550a7375SFelipe Balbi 0 | MUSB_RXCSR_FLUSHFIFO); 186550a7375SFelipe Balbi } 187550a7375SFelipe Balbi 188550a7375SFelipe Balbi value = c->channel_abort(ep->dma); 189550a7375SFelipe Balbi DBG(value ? 1 : 6, "%s: abort DMA --> %d\n", ep->name, value); 190550a7375SFelipe Balbi c->channel_release(ep->dma); 191550a7375SFelipe Balbi ep->dma = NULL; 192550a7375SFelipe Balbi } 193550a7375SFelipe Balbi 194550a7375SFelipe Balbi while (!list_empty(&(ep->req_list))) { 195550a7375SFelipe Balbi req = container_of(ep->req_list.next, struct musb_request, 196550a7375SFelipe Balbi request.list); 197550a7375SFelipe Balbi musb_g_giveback(ep, &req->request, status); 198550a7375SFelipe Balbi } 199550a7375SFelipe Balbi } 200550a7375SFelipe Balbi 201550a7375SFelipe Balbi /* ----------------------------------------------------------------------- */ 202550a7375SFelipe Balbi 203550a7375SFelipe Balbi /* Data transfers - pure PIO, pure DMA, or mixed mode */ 204550a7375SFelipe Balbi 205550a7375SFelipe Balbi /* 206550a7375SFelipe Balbi * This assumes the separate CPPI engine is responding to DMA requests 207550a7375SFelipe Balbi * from the usb core ... sequenced a bit differently from mentor dma. 208550a7375SFelipe Balbi */ 209550a7375SFelipe Balbi 210550a7375SFelipe Balbi static inline int max_ep_writesize(struct musb *musb, struct musb_ep *ep) 211550a7375SFelipe Balbi { 212550a7375SFelipe Balbi if (can_bulk_split(musb, ep->type)) 213550a7375SFelipe Balbi return ep->hw_ep->max_packet_sz_tx; 214550a7375SFelipe Balbi else 215550a7375SFelipe Balbi return ep->packet_sz; 216550a7375SFelipe Balbi } 217550a7375SFelipe Balbi 218550a7375SFelipe Balbi 219550a7375SFelipe Balbi #ifdef CONFIG_USB_INVENTRA_DMA 220550a7375SFelipe Balbi 221550a7375SFelipe Balbi /* Peripheral tx (IN) using Mentor DMA works as follows: 222550a7375SFelipe Balbi Only mode 0 is used for transfers <= wPktSize, 223550a7375SFelipe Balbi mode 1 is used for larger transfers, 224550a7375SFelipe Balbi 225550a7375SFelipe Balbi One of the following happens: 226550a7375SFelipe Balbi - Host sends IN token which causes an endpoint interrupt 227550a7375SFelipe Balbi -> TxAvail 228550a7375SFelipe Balbi -> if DMA is currently busy, exit. 229550a7375SFelipe Balbi -> if queue is non-empty, txstate(). 230550a7375SFelipe Balbi 231550a7375SFelipe Balbi - Request is queued by the gadget driver. 232550a7375SFelipe Balbi -> if queue was previously empty, txstate() 233550a7375SFelipe Balbi 234550a7375SFelipe Balbi txstate() 235550a7375SFelipe Balbi -> start 236550a7375SFelipe Balbi /\ -> setup DMA 237550a7375SFelipe Balbi | (data is transferred to the FIFO, then sent out when 238550a7375SFelipe Balbi | IN token(s) are recd from Host. 239550a7375SFelipe Balbi | -> DMA interrupt on completion 240550a7375SFelipe Balbi | calls TxAvail. 241b6e434a5SSergei Shtylyov | -> stop DMA, ~DMAENAB, 242550a7375SFelipe Balbi | -> set TxPktRdy for last short pkt or zlp 243550a7375SFelipe Balbi | -> Complete Request 244550a7375SFelipe Balbi | -> Continue next request (call txstate) 245550a7375SFelipe Balbi |___________________________________| 246550a7375SFelipe Balbi 247550a7375SFelipe Balbi * Non-Mentor DMA engines can of course work differently, such as by 248550a7375SFelipe Balbi * upleveling from irq-per-packet to irq-per-buffer. 249550a7375SFelipe Balbi */ 250550a7375SFelipe Balbi 251550a7375SFelipe Balbi #endif 252550a7375SFelipe Balbi 253550a7375SFelipe Balbi /* 254550a7375SFelipe Balbi * An endpoint is transmitting data. This can be called either from 255550a7375SFelipe Balbi * the IRQ routine or from ep.queue() to kickstart a request on an 256550a7375SFelipe Balbi * endpoint. 257550a7375SFelipe Balbi * 258550a7375SFelipe Balbi * Context: controller locked, IRQs blocked, endpoint selected 259550a7375SFelipe Balbi */ 260550a7375SFelipe Balbi static void txstate(struct musb *musb, struct musb_request *req) 261550a7375SFelipe Balbi { 262550a7375SFelipe Balbi u8 epnum = req->epnum; 263550a7375SFelipe Balbi struct musb_ep *musb_ep; 264550a7375SFelipe Balbi void __iomem *epio = musb->endpoints[epnum].regs; 265550a7375SFelipe Balbi struct usb_request *request; 266550a7375SFelipe Balbi u16 fifo_count = 0, csr; 267550a7375SFelipe Balbi int use_dma = 0; 268550a7375SFelipe Balbi 269550a7375SFelipe Balbi musb_ep = req->ep; 270550a7375SFelipe Balbi 271550a7375SFelipe Balbi /* we shouldn't get here while DMA is active ... but we do ... */ 272550a7375SFelipe Balbi if (dma_channel_status(musb_ep->dma) == MUSB_DMA_STATUS_BUSY) { 273550a7375SFelipe Balbi DBG(4, "dma pending...\n"); 274550a7375SFelipe Balbi return; 275550a7375SFelipe Balbi } 276550a7375SFelipe Balbi 277550a7375SFelipe Balbi /* read TXCSR before */ 278550a7375SFelipe Balbi csr = musb_readw(epio, MUSB_TXCSR); 279550a7375SFelipe Balbi 280550a7375SFelipe Balbi request = &req->request; 281550a7375SFelipe Balbi fifo_count = min(max_ep_writesize(musb, musb_ep), 282550a7375SFelipe Balbi (int)(request->length - request->actual)); 283550a7375SFelipe Balbi 284550a7375SFelipe Balbi if (csr & MUSB_TXCSR_TXPKTRDY) { 285550a7375SFelipe Balbi DBG(5, "%s old packet still ready , txcsr %03x\n", 286550a7375SFelipe Balbi musb_ep->end_point.name, csr); 287550a7375SFelipe Balbi return; 288550a7375SFelipe Balbi } 289550a7375SFelipe Balbi 290550a7375SFelipe Balbi if (csr & MUSB_TXCSR_P_SENDSTALL) { 291550a7375SFelipe Balbi DBG(5, "%s stalling, txcsr %03x\n", 292550a7375SFelipe Balbi musb_ep->end_point.name, csr); 293550a7375SFelipe Balbi return; 294550a7375SFelipe Balbi } 295550a7375SFelipe Balbi 296550a7375SFelipe Balbi DBG(4, "hw_ep%d, maxpacket %d, fifo count %d, txcsr %03x\n", 297550a7375SFelipe Balbi epnum, musb_ep->packet_sz, fifo_count, 298550a7375SFelipe Balbi csr); 299550a7375SFelipe Balbi 300550a7375SFelipe Balbi #ifndef CONFIG_MUSB_PIO_ONLY 301550a7375SFelipe Balbi if (is_dma_capable() && musb_ep->dma) { 302550a7375SFelipe Balbi struct dma_controller *c = musb->dma_controller; 30366af83ddSMing Lei size_t request_size; 30466af83ddSMing Lei 30566af83ddSMing Lei /* setup DMA, then program endpoint CSR */ 30666af83ddSMing Lei request_size = min_t(size_t, request->length - request->actual, 30766af83ddSMing Lei musb_ep->dma->max_len); 308550a7375SFelipe Balbi 309550a7375SFelipe Balbi use_dma = (request->dma != DMA_ADDR_INVALID); 310550a7375SFelipe Balbi 311550a7375SFelipe Balbi /* MUSB_TXCSR_P_ISO is still set correctly */ 312550a7375SFelipe Balbi 313550a7375SFelipe Balbi #ifdef CONFIG_USB_INVENTRA_DMA 314550a7375SFelipe Balbi { 315d1043a26SAnand Gadiyar if (request_size < musb_ep->packet_sz) 316550a7375SFelipe Balbi musb_ep->dma->desired_mode = 0; 317550a7375SFelipe Balbi else 318550a7375SFelipe Balbi musb_ep->dma->desired_mode = 1; 319550a7375SFelipe Balbi 320550a7375SFelipe Balbi use_dma = use_dma && c->channel_program( 321550a7375SFelipe Balbi musb_ep->dma, musb_ep->packet_sz, 322550a7375SFelipe Balbi musb_ep->dma->desired_mode, 323796a83faSCliff Cai request->dma + request->actual, request_size); 324550a7375SFelipe Balbi if (use_dma) { 325550a7375SFelipe Balbi if (musb_ep->dma->desired_mode == 0) { 326b6e434a5SSergei Shtylyov /* 327b6e434a5SSergei Shtylyov * We must not clear the DMAMODE bit 328b6e434a5SSergei Shtylyov * before the DMAENAB bit -- and the 329b6e434a5SSergei Shtylyov * latter doesn't always get cleared 330b6e434a5SSergei Shtylyov * before we get here... 331b6e434a5SSergei Shtylyov */ 332b6e434a5SSergei Shtylyov csr &= ~(MUSB_TXCSR_AUTOSET 333b6e434a5SSergei Shtylyov | MUSB_TXCSR_DMAENAB); 334b6e434a5SSergei Shtylyov musb_writew(epio, MUSB_TXCSR, csr 335b6e434a5SSergei Shtylyov | MUSB_TXCSR_P_WZC_BITS); 336b6e434a5SSergei Shtylyov csr &= ~MUSB_TXCSR_DMAMODE; 337550a7375SFelipe Balbi csr |= (MUSB_TXCSR_DMAENAB | 338550a7375SFelipe Balbi MUSB_TXCSR_MODE); 339550a7375SFelipe Balbi /* against programming guide */ 340*f11d893dSMing Lei } else { 341*f11d893dSMing Lei csr |= (MUSB_TXCSR_DMAENAB 342550a7375SFelipe Balbi | MUSB_TXCSR_DMAMODE 343550a7375SFelipe Balbi | MUSB_TXCSR_MODE); 344*f11d893dSMing Lei if (!musb_ep->hb_mult) 345*f11d893dSMing Lei csr |= MUSB_TXCSR_AUTOSET; 346*f11d893dSMing Lei } 347550a7375SFelipe Balbi csr &= ~MUSB_TXCSR_P_UNDERRUN; 348*f11d893dSMing Lei 349550a7375SFelipe Balbi musb_writew(epio, MUSB_TXCSR, csr); 350550a7375SFelipe Balbi } 351550a7375SFelipe Balbi } 352550a7375SFelipe Balbi 353550a7375SFelipe Balbi #elif defined(CONFIG_USB_TI_CPPI_DMA) 354550a7375SFelipe Balbi /* program endpoint CSR first, then setup DMA */ 355b6e434a5SSergei Shtylyov csr &= ~(MUSB_TXCSR_P_UNDERRUN | MUSB_TXCSR_TXPKTRDY); 35637e3ee99SSergei Shtylyov csr |= MUSB_TXCSR_DMAENAB | MUSB_TXCSR_DMAMODE | 35737e3ee99SSergei Shtylyov MUSB_TXCSR_MODE; 358550a7375SFelipe Balbi musb_writew(epio, MUSB_TXCSR, 359550a7375SFelipe Balbi (MUSB_TXCSR_P_WZC_BITS & ~MUSB_TXCSR_P_UNDERRUN) 360550a7375SFelipe Balbi | csr); 361550a7375SFelipe Balbi 362550a7375SFelipe Balbi /* ensure writebuffer is empty */ 363550a7375SFelipe Balbi csr = musb_readw(epio, MUSB_TXCSR); 364550a7375SFelipe Balbi 365550a7375SFelipe Balbi /* NOTE host side sets DMAENAB later than this; both are 366550a7375SFelipe Balbi * OK since the transfer dma glue (between CPPI and Mentor 367550a7375SFelipe Balbi * fifos) just tells CPPI it could start. Data only moves 368550a7375SFelipe Balbi * to the USB TX fifo when both fifos are ready. 369550a7375SFelipe Balbi */ 370550a7375SFelipe Balbi 371550a7375SFelipe Balbi /* "mode" is irrelevant here; handle terminating ZLPs like 372550a7375SFelipe Balbi * PIO does, since the hardware RNDIS mode seems unreliable 373550a7375SFelipe Balbi * except for the last-packet-is-already-short case. 374550a7375SFelipe Balbi */ 375550a7375SFelipe Balbi use_dma = use_dma && c->channel_program( 376550a7375SFelipe Balbi musb_ep->dma, musb_ep->packet_sz, 377550a7375SFelipe Balbi 0, 37866af83ddSMing Lei request->dma + request->actual, 37966af83ddSMing Lei request_size); 380550a7375SFelipe Balbi if (!use_dma) { 381550a7375SFelipe Balbi c->channel_release(musb_ep->dma); 382550a7375SFelipe Balbi musb_ep->dma = NULL; 383b6e434a5SSergei Shtylyov csr &= ~MUSB_TXCSR_DMAENAB; 384b6e434a5SSergei Shtylyov musb_writew(epio, MUSB_TXCSR, csr); 385550a7375SFelipe Balbi /* invariant: prequest->buf is non-null */ 386550a7375SFelipe Balbi } 387550a7375SFelipe Balbi #elif defined(CONFIG_USB_TUSB_OMAP_DMA) 388550a7375SFelipe Balbi use_dma = use_dma && c->channel_program( 389550a7375SFelipe Balbi musb_ep->dma, musb_ep->packet_sz, 390550a7375SFelipe Balbi request->zero, 39166af83ddSMing Lei request->dma + request->actual, 39266af83ddSMing Lei request_size); 393550a7375SFelipe Balbi #endif 394550a7375SFelipe Balbi } 395550a7375SFelipe Balbi #endif 396550a7375SFelipe Balbi 397550a7375SFelipe Balbi if (!use_dma) { 398550a7375SFelipe Balbi musb_write_fifo(musb_ep->hw_ep, fifo_count, 399550a7375SFelipe Balbi (u8 *) (request->buf + request->actual)); 400550a7375SFelipe Balbi request->actual += fifo_count; 401550a7375SFelipe Balbi csr |= MUSB_TXCSR_TXPKTRDY; 402550a7375SFelipe Balbi csr &= ~MUSB_TXCSR_P_UNDERRUN; 403550a7375SFelipe Balbi musb_writew(epio, MUSB_TXCSR, csr); 404550a7375SFelipe Balbi } 405550a7375SFelipe Balbi 406550a7375SFelipe Balbi /* host may already have the data when this message shows... */ 407550a7375SFelipe Balbi DBG(3, "%s TX/IN %s len %d/%d, txcsr %04x, fifo %d/%d\n", 408550a7375SFelipe Balbi musb_ep->end_point.name, use_dma ? "dma" : "pio", 409550a7375SFelipe Balbi request->actual, request->length, 410550a7375SFelipe Balbi musb_readw(epio, MUSB_TXCSR), 411550a7375SFelipe Balbi fifo_count, 412550a7375SFelipe Balbi musb_readw(epio, MUSB_TXMAXP)); 413550a7375SFelipe Balbi } 414550a7375SFelipe Balbi 415550a7375SFelipe Balbi /* 416550a7375SFelipe Balbi * FIFO state update (e.g. data ready). 417550a7375SFelipe Balbi * Called from IRQ, with controller locked. 418550a7375SFelipe Balbi */ 419550a7375SFelipe Balbi void musb_g_tx(struct musb *musb, u8 epnum) 420550a7375SFelipe Balbi { 421550a7375SFelipe Balbi u16 csr; 422550a7375SFelipe Balbi struct usb_request *request; 423550a7375SFelipe Balbi u8 __iomem *mbase = musb->mregs; 424550a7375SFelipe Balbi struct musb_ep *musb_ep = &musb->endpoints[epnum].ep_in; 425550a7375SFelipe Balbi void __iomem *epio = musb->endpoints[epnum].regs; 426550a7375SFelipe Balbi struct dma_channel *dma; 427550a7375SFelipe Balbi 428550a7375SFelipe Balbi musb_ep_select(mbase, epnum); 429550a7375SFelipe Balbi request = next_request(musb_ep); 430550a7375SFelipe Balbi 431550a7375SFelipe Balbi csr = musb_readw(epio, MUSB_TXCSR); 432550a7375SFelipe Balbi DBG(4, "<== %s, txcsr %04x\n", musb_ep->end_point.name, csr); 433550a7375SFelipe Balbi 434550a7375SFelipe Balbi dma = is_dma_capable() ? musb_ep->dma : NULL; 4357723de7eSSergei Shtylyov 4367723de7eSSergei Shtylyov /* 4377723de7eSSergei Shtylyov * REVISIT: for high bandwidth, MUSB_TXCSR_P_INCOMPTX 4387723de7eSSergei Shtylyov * probably rates reporting as a host error. 439550a7375SFelipe Balbi */ 440550a7375SFelipe Balbi if (csr & MUSB_TXCSR_P_SENTSTALL) { 441550a7375SFelipe Balbi csr |= MUSB_TXCSR_P_WZC_BITS; 442550a7375SFelipe Balbi csr &= ~MUSB_TXCSR_P_SENTSTALL; 443550a7375SFelipe Balbi musb_writew(epio, MUSB_TXCSR, csr); 4447723de7eSSergei Shtylyov return; 445550a7375SFelipe Balbi } 446550a7375SFelipe Balbi 447550a7375SFelipe Balbi if (csr & MUSB_TXCSR_P_UNDERRUN) { 4487723de7eSSergei Shtylyov /* We NAKed, no big deal... little reason to care. */ 449550a7375SFelipe Balbi csr |= MUSB_TXCSR_P_WZC_BITS; 4507723de7eSSergei Shtylyov csr &= ~(MUSB_TXCSR_P_UNDERRUN | MUSB_TXCSR_TXPKTRDY); 451550a7375SFelipe Balbi musb_writew(epio, MUSB_TXCSR, csr); 452550a7375SFelipe Balbi DBG(20, "underrun on ep%d, req %p\n", epnum, request); 453550a7375SFelipe Balbi } 454550a7375SFelipe Balbi 455550a7375SFelipe Balbi if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) { 4567723de7eSSergei Shtylyov /* 4577723de7eSSergei Shtylyov * SHOULD NOT HAPPEN... has with CPPI though, after 458550a7375SFelipe Balbi * changing SENDSTALL (and other cases); harmless? 459550a7375SFelipe Balbi */ 460550a7375SFelipe Balbi DBG(5, "%s dma still busy?\n", musb_ep->end_point.name); 4617723de7eSSergei Shtylyov return; 462550a7375SFelipe Balbi } 463550a7375SFelipe Balbi 464550a7375SFelipe Balbi if (request) { 465550a7375SFelipe Balbi u8 is_dma = 0; 466550a7375SFelipe Balbi 467550a7375SFelipe Balbi if (dma && (csr & MUSB_TXCSR_DMAENAB)) { 468550a7375SFelipe Balbi is_dma = 1; 469550a7375SFelipe Balbi csr |= MUSB_TXCSR_P_WZC_BITS; 4707723de7eSSergei Shtylyov csr &= ~(MUSB_TXCSR_DMAENAB | MUSB_TXCSR_P_UNDERRUN | 4717723de7eSSergei Shtylyov MUSB_TXCSR_TXPKTRDY); 472550a7375SFelipe Balbi musb_writew(epio, MUSB_TXCSR, csr); 4737723de7eSSergei Shtylyov /* Ensure writebuffer is empty. */ 474550a7375SFelipe Balbi csr = musb_readw(epio, MUSB_TXCSR); 475550a7375SFelipe Balbi request->actual += musb_ep->dma->actual_len; 4767723de7eSSergei Shtylyov DBG(4, "TXCSR%d %04x, DMA off, len %zu, req %p\n", 4777723de7eSSergei Shtylyov epnum, csr, musb_ep->dma->actual_len, request); 478550a7375SFelipe Balbi } 479550a7375SFelipe Balbi 480550a7375SFelipe Balbi if (is_dma || request->actual == request->length) { 4817723de7eSSergei Shtylyov /* 4827723de7eSSergei Shtylyov * First, maybe a terminating short packet. Some DMA 4837723de7eSSergei Shtylyov * engines might handle this by themselves. 484550a7375SFelipe Balbi */ 4857723de7eSSergei Shtylyov if ((request->zero && request->length 4867723de7eSSergei Shtylyov && request->length % musb_ep->packet_sz == 0) 487550a7375SFelipe Balbi #ifdef CONFIG_USB_INVENTRA_DMA 4887723de7eSSergei Shtylyov || (is_dma && (!dma->desired_mode || 489550a7375SFelipe Balbi (request->actual & 490550a7375SFelipe Balbi (musb_ep->packet_sz - 1)))) 491550a7375SFelipe Balbi #endif 492550a7375SFelipe Balbi ) { 4937723de7eSSergei Shtylyov /* 4947723de7eSSergei Shtylyov * On DMA completion, FIFO may not be 4957723de7eSSergei Shtylyov * available yet... 496550a7375SFelipe Balbi */ 497550a7375SFelipe Balbi if (csr & MUSB_TXCSR_TXPKTRDY) 4987723de7eSSergei Shtylyov return; 499550a7375SFelipe Balbi 500550a7375SFelipe Balbi DBG(4, "sending zero pkt\n"); 5017723de7eSSergei Shtylyov musb_writew(epio, MUSB_TXCSR, MUSB_TXCSR_MODE 502550a7375SFelipe Balbi | MUSB_TXCSR_TXPKTRDY); 503550a7375SFelipe Balbi request->zero = 0; 504550a7375SFelipe Balbi } 505550a7375SFelipe Balbi 506bb27bc2cSMing Lei if (request->actual == request->length) { 507550a7375SFelipe Balbi musb_g_giveback(musb_ep, request, 0); 50895962a77SSergei Shtylyov request = musb_ep->desc ? next_request(musb_ep) : NULL; 50995962a77SSergei Shtylyov if (!request) { 510550a7375SFelipe Balbi DBG(4, "%s idle now\n", 511550a7375SFelipe Balbi musb_ep->end_point.name); 5127723de7eSSergei Shtylyov return; 51395962a77SSergei Shtylyov } 514550a7375SFelipe Balbi } 515bb27bc2cSMing Lei } 516550a7375SFelipe Balbi 517550a7375SFelipe Balbi txstate(musb, to_musb_request(request)); 518550a7375SFelipe Balbi } 519550a7375SFelipe Balbi } 520550a7375SFelipe Balbi 521550a7375SFelipe Balbi /* ------------------------------------------------------------ */ 522550a7375SFelipe Balbi 523550a7375SFelipe Balbi #ifdef CONFIG_USB_INVENTRA_DMA 524550a7375SFelipe Balbi 525550a7375SFelipe Balbi /* Peripheral rx (OUT) using Mentor DMA works as follows: 526550a7375SFelipe Balbi - Only mode 0 is used. 527550a7375SFelipe Balbi 528550a7375SFelipe Balbi - Request is queued by the gadget class driver. 529550a7375SFelipe Balbi -> if queue was previously empty, rxstate() 530550a7375SFelipe Balbi 531550a7375SFelipe Balbi - Host sends OUT token which causes an endpoint interrupt 532550a7375SFelipe Balbi /\ -> RxReady 533550a7375SFelipe Balbi | -> if request queued, call rxstate 534550a7375SFelipe Balbi | /\ -> setup DMA 535550a7375SFelipe Balbi | | -> DMA interrupt on completion 536550a7375SFelipe Balbi | | -> RxReady 537550a7375SFelipe Balbi | | -> stop DMA 538550a7375SFelipe Balbi | | -> ack the read 539550a7375SFelipe Balbi | | -> if data recd = max expected 540550a7375SFelipe Balbi | | by the request, or host 541550a7375SFelipe Balbi | | sent a short packet, 542550a7375SFelipe Balbi | | complete the request, 543550a7375SFelipe Balbi | | and start the next one. 544550a7375SFelipe Balbi | |_____________________________________| 545550a7375SFelipe Balbi | else just wait for the host 546550a7375SFelipe Balbi | to send the next OUT token. 547550a7375SFelipe Balbi |__________________________________________________| 548550a7375SFelipe Balbi 549550a7375SFelipe Balbi * Non-Mentor DMA engines can of course work differently. 550550a7375SFelipe Balbi */ 551550a7375SFelipe Balbi 552550a7375SFelipe Balbi #endif 553550a7375SFelipe Balbi 554550a7375SFelipe Balbi /* 555550a7375SFelipe Balbi * Context: controller locked, IRQs blocked, endpoint selected 556550a7375SFelipe Balbi */ 557550a7375SFelipe Balbi static void rxstate(struct musb *musb, struct musb_request *req) 558550a7375SFelipe Balbi { 559550a7375SFelipe Balbi const u8 epnum = req->epnum; 560550a7375SFelipe Balbi struct usb_request *request = &req->request; 561bd2e74d6SMing Lei struct musb_ep *musb_ep; 562550a7375SFelipe Balbi void __iomem *epio = musb->endpoints[epnum].regs; 563c2c96321SFelipe Balbi unsigned fifo_count = 0; 564bd2e74d6SMing Lei u16 len; 565cea83241SSergei Shtylyov u16 csr = musb_readw(epio, MUSB_RXCSR); 566bd2e74d6SMing Lei struct musb_hw_ep *hw_ep = &musb->endpoints[epnum]; 567bd2e74d6SMing Lei 568bd2e74d6SMing Lei if (hw_ep->is_shared_fifo) 569bd2e74d6SMing Lei musb_ep = &hw_ep->ep_in; 570bd2e74d6SMing Lei else 571bd2e74d6SMing Lei musb_ep = &hw_ep->ep_out; 572bd2e74d6SMing Lei 573bd2e74d6SMing Lei len = musb_ep->packet_sz; 574550a7375SFelipe Balbi 575cea83241SSergei Shtylyov /* We shouldn't get here while DMA is active, but we do... */ 576cea83241SSergei Shtylyov if (dma_channel_status(musb_ep->dma) == MUSB_DMA_STATUS_BUSY) { 577cea83241SSergei Shtylyov DBG(4, "DMA pending...\n"); 578cea83241SSergei Shtylyov return; 579cea83241SSergei Shtylyov } 580cea83241SSergei Shtylyov 581cea83241SSergei Shtylyov if (csr & MUSB_RXCSR_P_SENDSTALL) { 582cea83241SSergei Shtylyov DBG(5, "%s stalling, RXCSR %04x\n", 583cea83241SSergei Shtylyov musb_ep->end_point.name, csr); 584cea83241SSergei Shtylyov return; 585cea83241SSergei Shtylyov } 586550a7375SFelipe Balbi 587550a7375SFelipe Balbi if (is_cppi_enabled() && musb_ep->dma) { 588550a7375SFelipe Balbi struct dma_controller *c = musb->dma_controller; 589550a7375SFelipe Balbi struct dma_channel *channel = musb_ep->dma; 590550a7375SFelipe Balbi 591550a7375SFelipe Balbi /* NOTE: CPPI won't actually stop advancing the DMA 592550a7375SFelipe Balbi * queue after short packet transfers, so this is almost 593550a7375SFelipe Balbi * always going to run as IRQ-per-packet DMA so that 594550a7375SFelipe Balbi * faults will be handled correctly. 595550a7375SFelipe Balbi */ 596550a7375SFelipe Balbi if (c->channel_program(channel, 597550a7375SFelipe Balbi musb_ep->packet_sz, 598550a7375SFelipe Balbi !request->short_not_ok, 599550a7375SFelipe Balbi request->dma + request->actual, 600550a7375SFelipe Balbi request->length - request->actual)) { 601550a7375SFelipe Balbi 602550a7375SFelipe Balbi /* make sure that if an rxpkt arrived after the irq, 603550a7375SFelipe Balbi * the cppi engine will be ready to take it as soon 604550a7375SFelipe Balbi * as DMA is enabled 605550a7375SFelipe Balbi */ 606550a7375SFelipe Balbi csr &= ~(MUSB_RXCSR_AUTOCLEAR 607550a7375SFelipe Balbi | MUSB_RXCSR_DMAMODE); 608550a7375SFelipe Balbi csr |= MUSB_RXCSR_DMAENAB | MUSB_RXCSR_P_WZC_BITS; 609550a7375SFelipe Balbi musb_writew(epio, MUSB_RXCSR, csr); 610550a7375SFelipe Balbi return; 611550a7375SFelipe Balbi } 612550a7375SFelipe Balbi } 613550a7375SFelipe Balbi 614550a7375SFelipe Balbi if (csr & MUSB_RXCSR_RXPKTRDY) { 615550a7375SFelipe Balbi len = musb_readw(epio, MUSB_RXCOUNT); 616550a7375SFelipe Balbi if (request->actual < request->length) { 617550a7375SFelipe Balbi #ifdef CONFIG_USB_INVENTRA_DMA 618550a7375SFelipe Balbi if (is_dma_capable() && musb_ep->dma) { 619550a7375SFelipe Balbi struct dma_controller *c; 620550a7375SFelipe Balbi struct dma_channel *channel; 621550a7375SFelipe Balbi int use_dma = 0; 622550a7375SFelipe Balbi 623550a7375SFelipe Balbi c = musb->dma_controller; 624550a7375SFelipe Balbi channel = musb_ep->dma; 625550a7375SFelipe Balbi 626550a7375SFelipe Balbi /* We use DMA Req mode 0 in rx_csr, and DMA controller operates in 627550a7375SFelipe Balbi * mode 0 only. So we do not get endpoint interrupts due to DMA 628550a7375SFelipe Balbi * completion. We only get interrupts from DMA controller. 629550a7375SFelipe Balbi * 630550a7375SFelipe Balbi * We could operate in DMA mode 1 if we knew the size of the tranfer 631550a7375SFelipe Balbi * in advance. For mass storage class, request->length = what the host 632550a7375SFelipe Balbi * sends, so that'd work. But for pretty much everything else, 633550a7375SFelipe Balbi * request->length is routinely more than what the host sends. For 634550a7375SFelipe Balbi * most these gadgets, end of is signified either by a short packet, 635550a7375SFelipe Balbi * or filling the last byte of the buffer. (Sending extra data in 636550a7375SFelipe Balbi * that last pckate should trigger an overflow fault.) But in mode 1, 637550a7375SFelipe Balbi * we don't get DMA completion interrrupt for short packets. 638550a7375SFelipe Balbi * 639550a7375SFelipe Balbi * Theoretically, we could enable DMAReq irq (MUSB_RXCSR_DMAMODE = 1), 640550a7375SFelipe Balbi * to get endpoint interrupt on every DMA req, but that didn't seem 641550a7375SFelipe Balbi * to work reliably. 642550a7375SFelipe Balbi * 643550a7375SFelipe Balbi * REVISIT an updated g_file_storage can set req->short_not_ok, which 644550a7375SFelipe Balbi * then becomes usable as a runtime "use mode 1" hint... 645550a7375SFelipe Balbi */ 646550a7375SFelipe Balbi 647550a7375SFelipe Balbi csr |= MUSB_RXCSR_DMAENAB; 648*f11d893dSMing Lei if (!musb_ep->hb_mult) 649550a7375SFelipe Balbi csr |= MUSB_RXCSR_AUTOCLEAR; 650490e5fbeSMing Lei #ifdef USE_MODE1 651550a7375SFelipe Balbi /* csr |= MUSB_RXCSR_DMAMODE; */ 652550a7375SFelipe Balbi 653550a7375SFelipe Balbi /* this special sequence (enabling and then 654550a7375SFelipe Balbi * disabling MUSB_RXCSR_DMAMODE) is required 655550a7375SFelipe Balbi * to get DMAReq to activate 656550a7375SFelipe Balbi */ 657550a7375SFelipe Balbi musb_writew(epio, MUSB_RXCSR, 658550a7375SFelipe Balbi csr | MUSB_RXCSR_DMAMODE); 659550a7375SFelipe Balbi #endif 660550a7375SFelipe Balbi musb_writew(epio, MUSB_RXCSR, csr); 661550a7375SFelipe Balbi 662550a7375SFelipe Balbi if (request->actual < request->length) { 663550a7375SFelipe Balbi int transfer_size = 0; 664550a7375SFelipe Balbi #ifdef USE_MODE1 6651018b4e4SMing Lei transfer_size = min(request->length - request->actual, 666550a7375SFelipe Balbi channel->max_len); 667550a7375SFelipe Balbi #else 6681018b4e4SMing Lei transfer_size = min(request->length - request->actual, 6691018b4e4SMing Lei (unsigned)len); 670550a7375SFelipe Balbi #endif 671550a7375SFelipe Balbi if (transfer_size <= musb_ep->packet_sz) 672550a7375SFelipe Balbi musb_ep->dma->desired_mode = 0; 673550a7375SFelipe Balbi else 674550a7375SFelipe Balbi musb_ep->dma->desired_mode = 1; 675550a7375SFelipe Balbi 676550a7375SFelipe Balbi use_dma = c->channel_program( 677550a7375SFelipe Balbi channel, 678550a7375SFelipe Balbi musb_ep->packet_sz, 679550a7375SFelipe Balbi channel->desired_mode, 680550a7375SFelipe Balbi request->dma 681550a7375SFelipe Balbi + request->actual, 682550a7375SFelipe Balbi transfer_size); 683550a7375SFelipe Balbi } 684550a7375SFelipe Balbi 685550a7375SFelipe Balbi if (use_dma) 686550a7375SFelipe Balbi return; 687550a7375SFelipe Balbi } 688550a7375SFelipe Balbi #endif /* Mentor's DMA */ 689550a7375SFelipe Balbi 690550a7375SFelipe Balbi fifo_count = request->length - request->actual; 691550a7375SFelipe Balbi DBG(3, "%s OUT/RX pio fifo %d/%d, maxpacket %d\n", 692550a7375SFelipe Balbi musb_ep->end_point.name, 693550a7375SFelipe Balbi len, fifo_count, 694550a7375SFelipe Balbi musb_ep->packet_sz); 695550a7375SFelipe Balbi 696c2c96321SFelipe Balbi fifo_count = min_t(unsigned, len, fifo_count); 697550a7375SFelipe Balbi 698550a7375SFelipe Balbi #ifdef CONFIG_USB_TUSB_OMAP_DMA 699550a7375SFelipe Balbi if (tusb_dma_omap() && musb_ep->dma) { 700550a7375SFelipe Balbi struct dma_controller *c = musb->dma_controller; 701550a7375SFelipe Balbi struct dma_channel *channel = musb_ep->dma; 702550a7375SFelipe Balbi u32 dma_addr = request->dma + request->actual; 703550a7375SFelipe Balbi int ret; 704550a7375SFelipe Balbi 705550a7375SFelipe Balbi ret = c->channel_program(channel, 706550a7375SFelipe Balbi musb_ep->packet_sz, 707550a7375SFelipe Balbi channel->desired_mode, 708550a7375SFelipe Balbi dma_addr, 709550a7375SFelipe Balbi fifo_count); 710550a7375SFelipe Balbi if (ret) 711550a7375SFelipe Balbi return; 712550a7375SFelipe Balbi } 713550a7375SFelipe Balbi #endif 714550a7375SFelipe Balbi 715550a7375SFelipe Balbi musb_read_fifo(musb_ep->hw_ep, fifo_count, (u8 *) 716550a7375SFelipe Balbi (request->buf + request->actual)); 717550a7375SFelipe Balbi request->actual += fifo_count; 718550a7375SFelipe Balbi 719550a7375SFelipe Balbi /* REVISIT if we left anything in the fifo, flush 720550a7375SFelipe Balbi * it and report -EOVERFLOW 721550a7375SFelipe Balbi */ 722550a7375SFelipe Balbi 723550a7375SFelipe Balbi /* ack the read! */ 724550a7375SFelipe Balbi csr |= MUSB_RXCSR_P_WZC_BITS; 725550a7375SFelipe Balbi csr &= ~MUSB_RXCSR_RXPKTRDY; 726550a7375SFelipe Balbi musb_writew(epio, MUSB_RXCSR, csr); 727550a7375SFelipe Balbi } 728550a7375SFelipe Balbi } 729550a7375SFelipe Balbi 730550a7375SFelipe Balbi /* reach the end or short packet detected */ 731550a7375SFelipe Balbi if (request->actual == request->length || len < musb_ep->packet_sz) 732550a7375SFelipe Balbi musb_g_giveback(musb_ep, request, 0); 733550a7375SFelipe Balbi } 734550a7375SFelipe Balbi 735550a7375SFelipe Balbi /* 736550a7375SFelipe Balbi * Data ready for a request; called from IRQ 737550a7375SFelipe Balbi */ 738550a7375SFelipe Balbi void musb_g_rx(struct musb *musb, u8 epnum) 739550a7375SFelipe Balbi { 740550a7375SFelipe Balbi u16 csr; 741550a7375SFelipe Balbi struct usb_request *request; 742550a7375SFelipe Balbi void __iomem *mbase = musb->mregs; 743bd2e74d6SMing Lei struct musb_ep *musb_ep; 744550a7375SFelipe Balbi void __iomem *epio = musb->endpoints[epnum].regs; 745550a7375SFelipe Balbi struct dma_channel *dma; 746bd2e74d6SMing Lei struct musb_hw_ep *hw_ep = &musb->endpoints[epnum]; 747bd2e74d6SMing Lei 748bd2e74d6SMing Lei if (hw_ep->is_shared_fifo) 749bd2e74d6SMing Lei musb_ep = &hw_ep->ep_in; 750bd2e74d6SMing Lei else 751bd2e74d6SMing Lei musb_ep = &hw_ep->ep_out; 752550a7375SFelipe Balbi 753550a7375SFelipe Balbi musb_ep_select(mbase, epnum); 754550a7375SFelipe Balbi 755550a7375SFelipe Balbi request = next_request(musb_ep); 7560abdc36fSMaulik Mankad if (!request) 7570abdc36fSMaulik Mankad return; 758550a7375SFelipe Balbi 759550a7375SFelipe Balbi csr = musb_readw(epio, MUSB_RXCSR); 760550a7375SFelipe Balbi dma = is_dma_capable() ? musb_ep->dma : NULL; 761550a7375SFelipe Balbi 762550a7375SFelipe Balbi DBG(4, "<== %s, rxcsr %04x%s %p\n", musb_ep->end_point.name, 763550a7375SFelipe Balbi csr, dma ? " (dma)" : "", request); 764550a7375SFelipe Balbi 765550a7375SFelipe Balbi if (csr & MUSB_RXCSR_P_SENTSTALL) { 766550a7375SFelipe Balbi csr |= MUSB_RXCSR_P_WZC_BITS; 767550a7375SFelipe Balbi csr &= ~MUSB_RXCSR_P_SENTSTALL; 768550a7375SFelipe Balbi musb_writew(epio, MUSB_RXCSR, csr); 769cea83241SSergei Shtylyov return; 770550a7375SFelipe Balbi } 771550a7375SFelipe Balbi 772550a7375SFelipe Balbi if (csr & MUSB_RXCSR_P_OVERRUN) { 773550a7375SFelipe Balbi /* csr |= MUSB_RXCSR_P_WZC_BITS; */ 774550a7375SFelipe Balbi csr &= ~MUSB_RXCSR_P_OVERRUN; 775550a7375SFelipe Balbi musb_writew(epio, MUSB_RXCSR, csr); 776550a7375SFelipe Balbi 777550a7375SFelipe Balbi DBG(3, "%s iso overrun on %p\n", musb_ep->name, request); 778550a7375SFelipe Balbi if (request && request->status == -EINPROGRESS) 779550a7375SFelipe Balbi request->status = -EOVERFLOW; 780550a7375SFelipe Balbi } 781550a7375SFelipe Balbi if (csr & MUSB_RXCSR_INCOMPRX) { 782550a7375SFelipe Balbi /* REVISIT not necessarily an error */ 783550a7375SFelipe Balbi DBG(4, "%s, incomprx\n", musb_ep->end_point.name); 784550a7375SFelipe Balbi } 785550a7375SFelipe Balbi 786550a7375SFelipe Balbi if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) { 787550a7375SFelipe Balbi /* "should not happen"; likely RXPKTRDY pending for DMA */ 788550a7375SFelipe Balbi DBG((csr & MUSB_RXCSR_DMAENAB) ? 4 : 1, 789550a7375SFelipe Balbi "%s busy, csr %04x\n", 790550a7375SFelipe Balbi musb_ep->end_point.name, csr); 791cea83241SSergei Shtylyov return; 792550a7375SFelipe Balbi } 793550a7375SFelipe Balbi 794550a7375SFelipe Balbi if (dma && (csr & MUSB_RXCSR_DMAENAB)) { 795550a7375SFelipe Balbi csr &= ~(MUSB_RXCSR_AUTOCLEAR 796550a7375SFelipe Balbi | MUSB_RXCSR_DMAENAB 797550a7375SFelipe Balbi | MUSB_RXCSR_DMAMODE); 798550a7375SFelipe Balbi musb_writew(epio, MUSB_RXCSR, 799550a7375SFelipe Balbi MUSB_RXCSR_P_WZC_BITS | csr); 800550a7375SFelipe Balbi 801550a7375SFelipe Balbi request->actual += musb_ep->dma->actual_len; 802550a7375SFelipe Balbi 803550a7375SFelipe Balbi DBG(4, "RXCSR%d %04x, dma off, %04x, len %zu, req %p\n", 804550a7375SFelipe Balbi epnum, csr, 805550a7375SFelipe Balbi musb_readw(epio, MUSB_RXCSR), 806550a7375SFelipe Balbi musb_ep->dma->actual_len, request); 807550a7375SFelipe Balbi 808550a7375SFelipe Balbi #if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_TUSB_OMAP_DMA) 809550a7375SFelipe Balbi /* Autoclear doesn't clear RxPktRdy for short packets */ 810550a7375SFelipe Balbi if ((dma->desired_mode == 0) 811550a7375SFelipe Balbi || (dma->actual_len 812550a7375SFelipe Balbi & (musb_ep->packet_sz - 1))) { 813550a7375SFelipe Balbi /* ack the read! */ 814550a7375SFelipe Balbi csr &= ~MUSB_RXCSR_RXPKTRDY; 815550a7375SFelipe Balbi musb_writew(epio, MUSB_RXCSR, csr); 816550a7375SFelipe Balbi } 817550a7375SFelipe Balbi 818550a7375SFelipe Balbi /* incomplete, and not short? wait for next IN packet */ 819550a7375SFelipe Balbi if ((request->actual < request->length) 820550a7375SFelipe Balbi && (musb_ep->dma->actual_len 821550a7375SFelipe Balbi == musb_ep->packet_sz)) 822cea83241SSergei Shtylyov return; 823550a7375SFelipe Balbi #endif 824550a7375SFelipe Balbi musb_g_giveback(musb_ep, request, 0); 825550a7375SFelipe Balbi 826550a7375SFelipe Balbi request = next_request(musb_ep); 827550a7375SFelipe Balbi if (!request) 828cea83241SSergei Shtylyov return; 829550a7375SFelipe Balbi } 830550a7375SFelipe Balbi 831550a7375SFelipe Balbi /* analyze request if the ep is hot */ 832550a7375SFelipe Balbi if (request) 833550a7375SFelipe Balbi rxstate(musb, to_musb_request(request)); 834550a7375SFelipe Balbi else 835550a7375SFelipe Balbi DBG(3, "packet waiting for %s%s request\n", 836550a7375SFelipe Balbi musb_ep->desc ? "" : "inactive ", 837550a7375SFelipe Balbi musb_ep->end_point.name); 838550a7375SFelipe Balbi return; 839550a7375SFelipe Balbi } 840550a7375SFelipe Balbi 841550a7375SFelipe Balbi /* ------------------------------------------------------------ */ 842550a7375SFelipe Balbi 843550a7375SFelipe Balbi static int musb_gadget_enable(struct usb_ep *ep, 844550a7375SFelipe Balbi const struct usb_endpoint_descriptor *desc) 845550a7375SFelipe Balbi { 846550a7375SFelipe Balbi unsigned long flags; 847550a7375SFelipe Balbi struct musb_ep *musb_ep; 848550a7375SFelipe Balbi struct musb_hw_ep *hw_ep; 849550a7375SFelipe Balbi void __iomem *regs; 850550a7375SFelipe Balbi struct musb *musb; 851550a7375SFelipe Balbi void __iomem *mbase; 852550a7375SFelipe Balbi u8 epnum; 853550a7375SFelipe Balbi u16 csr; 854550a7375SFelipe Balbi unsigned tmp; 855550a7375SFelipe Balbi int status = -EINVAL; 856550a7375SFelipe Balbi 857550a7375SFelipe Balbi if (!ep || !desc) 858550a7375SFelipe Balbi return -EINVAL; 859550a7375SFelipe Balbi 860550a7375SFelipe Balbi musb_ep = to_musb_ep(ep); 861550a7375SFelipe Balbi hw_ep = musb_ep->hw_ep; 862550a7375SFelipe Balbi regs = hw_ep->regs; 863550a7375SFelipe Balbi musb = musb_ep->musb; 864550a7375SFelipe Balbi mbase = musb->mregs; 865550a7375SFelipe Balbi epnum = musb_ep->current_epnum; 866550a7375SFelipe Balbi 867550a7375SFelipe Balbi spin_lock_irqsave(&musb->lock, flags); 868550a7375SFelipe Balbi 869550a7375SFelipe Balbi if (musb_ep->desc) { 870550a7375SFelipe Balbi status = -EBUSY; 871550a7375SFelipe Balbi goto fail; 872550a7375SFelipe Balbi } 87396bcd090SJulia Lawall musb_ep->type = usb_endpoint_type(desc); 874550a7375SFelipe Balbi 875550a7375SFelipe Balbi /* check direction and (later) maxpacket size against endpoint */ 87696bcd090SJulia Lawall if (usb_endpoint_num(desc) != epnum) 877550a7375SFelipe Balbi goto fail; 878550a7375SFelipe Balbi 879550a7375SFelipe Balbi /* REVISIT this rules out high bandwidth periodic transfers */ 880550a7375SFelipe Balbi tmp = le16_to_cpu(desc->wMaxPacketSize); 881*f11d893dSMing Lei if (tmp & ~0x07ff) { 882*f11d893dSMing Lei int ok; 883*f11d893dSMing Lei 884*f11d893dSMing Lei if (usb_endpoint_dir_in(desc)) 885*f11d893dSMing Lei ok = musb->hb_iso_tx; 886*f11d893dSMing Lei else 887*f11d893dSMing Lei ok = musb->hb_iso_rx; 888*f11d893dSMing Lei 889*f11d893dSMing Lei if (!ok) { 890*f11d893dSMing Lei DBG(4, "%s: not support ISO high bandwidth\n", __func__); 891550a7375SFelipe Balbi goto fail; 892*f11d893dSMing Lei } 893*f11d893dSMing Lei musb_ep->hb_mult = (tmp >> 11) & 3; 894*f11d893dSMing Lei } else { 895*f11d893dSMing Lei musb_ep->hb_mult = 0; 896*f11d893dSMing Lei } 897*f11d893dSMing Lei 898*f11d893dSMing Lei musb_ep->packet_sz = tmp & 0x7ff; 899*f11d893dSMing Lei tmp = musb_ep->packet_sz * (musb_ep->hb_mult + 1); 900550a7375SFelipe Balbi 901550a7375SFelipe Balbi /* enable the interrupts for the endpoint, set the endpoint 902550a7375SFelipe Balbi * packet size (or fail), set the mode, clear the fifo 903550a7375SFelipe Balbi */ 904550a7375SFelipe Balbi musb_ep_select(mbase, epnum); 90596bcd090SJulia Lawall if (usb_endpoint_dir_in(desc)) { 906550a7375SFelipe Balbi u16 int_txe = musb_readw(mbase, MUSB_INTRTXE); 907550a7375SFelipe Balbi 908550a7375SFelipe Balbi if (hw_ep->is_shared_fifo) 909550a7375SFelipe Balbi musb_ep->is_in = 1; 910550a7375SFelipe Balbi if (!musb_ep->is_in) 911550a7375SFelipe Balbi goto fail; 912*f11d893dSMing Lei 913*f11d893dSMing Lei if (tmp > hw_ep->max_packet_sz_tx) { 914*f11d893dSMing Lei DBG(4, "%s: packet size beyond hw fifo size\n", __func__); 915550a7375SFelipe Balbi goto fail; 916*f11d893dSMing Lei } 917550a7375SFelipe Balbi 918550a7375SFelipe Balbi int_txe |= (1 << epnum); 919550a7375SFelipe Balbi musb_writew(mbase, MUSB_INTRTXE, int_txe); 920550a7375SFelipe Balbi 921550a7375SFelipe Balbi /* REVISIT if can_bulk_split(), use by updating "tmp"; 922550a7375SFelipe Balbi * likewise high bandwidth periodic tx 923550a7375SFelipe Balbi */ 9249f445cb2SCliff Cai /* Set TXMAXP with the FIFO size of the endpoint 9259f445cb2SCliff Cai * to disable double buffering mode. Currently, It seems that double 9269f445cb2SCliff Cai * buffering has problem if musb RTL revision number < 2.0. 9279f445cb2SCliff Cai */ 9289f445cb2SCliff Cai if (musb->hwvers < MUSB_HWVERS_2000) 9299f445cb2SCliff Cai musb_writew(regs, MUSB_TXMAXP, hw_ep->max_packet_sz_tx); 9309f445cb2SCliff Cai else 931*f11d893dSMing Lei musb_writew(regs, MUSB_TXMAXP, musb_ep->packet_sz | (musb_ep->hb_mult << 11)); 932550a7375SFelipe Balbi 933550a7375SFelipe Balbi csr = MUSB_TXCSR_MODE | MUSB_TXCSR_CLRDATATOG; 934550a7375SFelipe Balbi if (musb_readw(regs, MUSB_TXCSR) 935550a7375SFelipe Balbi & MUSB_TXCSR_FIFONOTEMPTY) 936550a7375SFelipe Balbi csr |= MUSB_TXCSR_FLUSHFIFO; 937550a7375SFelipe Balbi if (musb_ep->type == USB_ENDPOINT_XFER_ISOC) 938550a7375SFelipe Balbi csr |= MUSB_TXCSR_P_ISO; 939550a7375SFelipe Balbi 940550a7375SFelipe Balbi /* set twice in case of double buffering */ 941550a7375SFelipe Balbi musb_writew(regs, MUSB_TXCSR, csr); 942550a7375SFelipe Balbi /* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */ 943550a7375SFelipe Balbi musb_writew(regs, MUSB_TXCSR, csr); 944550a7375SFelipe Balbi 945550a7375SFelipe Balbi } else { 946550a7375SFelipe Balbi u16 int_rxe = musb_readw(mbase, MUSB_INTRRXE); 947550a7375SFelipe Balbi 948550a7375SFelipe Balbi if (hw_ep->is_shared_fifo) 949550a7375SFelipe Balbi musb_ep->is_in = 0; 950550a7375SFelipe Balbi if (musb_ep->is_in) 951550a7375SFelipe Balbi goto fail; 952*f11d893dSMing Lei 953*f11d893dSMing Lei if (tmp > hw_ep->max_packet_sz_rx) { 954*f11d893dSMing Lei DBG(4, "%s: packet size beyond hw fifo size\n", __func__); 955550a7375SFelipe Balbi goto fail; 956*f11d893dSMing Lei } 957550a7375SFelipe Balbi 958550a7375SFelipe Balbi int_rxe |= (1 << epnum); 959550a7375SFelipe Balbi musb_writew(mbase, MUSB_INTRRXE, int_rxe); 960550a7375SFelipe Balbi 961550a7375SFelipe Balbi /* REVISIT if can_bulk_combine() use by updating "tmp" 962550a7375SFelipe Balbi * likewise high bandwidth periodic rx 963550a7375SFelipe Balbi */ 9649f445cb2SCliff Cai /* Set RXMAXP with the FIFO size of the endpoint 9659f445cb2SCliff Cai * to disable double buffering mode. 9669f445cb2SCliff Cai */ 9679f445cb2SCliff Cai if (musb->hwvers < MUSB_HWVERS_2000) 9689f445cb2SCliff Cai musb_writew(regs, MUSB_RXMAXP, hw_ep->max_packet_sz_rx); 9699f445cb2SCliff Cai else 970*f11d893dSMing Lei musb_writew(regs, MUSB_RXMAXP, musb_ep->packet_sz | (musb_ep->hb_mult << 11)); 971550a7375SFelipe Balbi 972550a7375SFelipe Balbi /* force shared fifo to OUT-only mode */ 973550a7375SFelipe Balbi if (hw_ep->is_shared_fifo) { 974550a7375SFelipe Balbi csr = musb_readw(regs, MUSB_TXCSR); 975550a7375SFelipe Balbi csr &= ~(MUSB_TXCSR_MODE | MUSB_TXCSR_TXPKTRDY); 976550a7375SFelipe Balbi musb_writew(regs, MUSB_TXCSR, csr); 977550a7375SFelipe Balbi } 978550a7375SFelipe Balbi 979550a7375SFelipe Balbi csr = MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_CLRDATATOG; 980550a7375SFelipe Balbi if (musb_ep->type == USB_ENDPOINT_XFER_ISOC) 981550a7375SFelipe Balbi csr |= MUSB_RXCSR_P_ISO; 982550a7375SFelipe Balbi else if (musb_ep->type == USB_ENDPOINT_XFER_INT) 983550a7375SFelipe Balbi csr |= MUSB_RXCSR_DISNYET; 984550a7375SFelipe Balbi 985550a7375SFelipe Balbi /* set twice in case of double buffering */ 986550a7375SFelipe Balbi musb_writew(regs, MUSB_RXCSR, csr); 987550a7375SFelipe Balbi musb_writew(regs, MUSB_RXCSR, csr); 988550a7375SFelipe Balbi } 989550a7375SFelipe Balbi 990550a7375SFelipe Balbi /* NOTE: all the I/O code _should_ work fine without DMA, in case 991550a7375SFelipe Balbi * for some reason you run out of channels here. 992550a7375SFelipe Balbi */ 993550a7375SFelipe Balbi if (is_dma_capable() && musb->dma_controller) { 994550a7375SFelipe Balbi struct dma_controller *c = musb->dma_controller; 995550a7375SFelipe Balbi 996550a7375SFelipe Balbi musb_ep->dma = c->channel_alloc(c, hw_ep, 997550a7375SFelipe Balbi (desc->bEndpointAddress & USB_DIR_IN)); 998550a7375SFelipe Balbi } else 999550a7375SFelipe Balbi musb_ep->dma = NULL; 1000550a7375SFelipe Balbi 1001550a7375SFelipe Balbi musb_ep->desc = desc; 1002550a7375SFelipe Balbi musb_ep->busy = 0; 100347e97605SSergei Shtylyov musb_ep->wedged = 0; 1004550a7375SFelipe Balbi status = 0; 1005550a7375SFelipe Balbi 1006550a7375SFelipe Balbi pr_debug("%s periph: enabled %s for %s %s, %smaxpacket %d\n", 1007550a7375SFelipe Balbi musb_driver_name, musb_ep->end_point.name, 1008550a7375SFelipe Balbi ({ char *s; switch (musb_ep->type) { 1009550a7375SFelipe Balbi case USB_ENDPOINT_XFER_BULK: s = "bulk"; break; 1010550a7375SFelipe Balbi case USB_ENDPOINT_XFER_INT: s = "int"; break; 1011550a7375SFelipe Balbi default: s = "iso"; break; 1012550a7375SFelipe Balbi }; s; }), 1013550a7375SFelipe Balbi musb_ep->is_in ? "IN" : "OUT", 1014550a7375SFelipe Balbi musb_ep->dma ? "dma, " : "", 1015550a7375SFelipe Balbi musb_ep->packet_sz); 1016550a7375SFelipe Balbi 1017550a7375SFelipe Balbi schedule_work(&musb->irq_work); 1018550a7375SFelipe Balbi 1019550a7375SFelipe Balbi fail: 1020550a7375SFelipe Balbi spin_unlock_irqrestore(&musb->lock, flags); 1021550a7375SFelipe Balbi return status; 1022550a7375SFelipe Balbi } 1023550a7375SFelipe Balbi 1024550a7375SFelipe Balbi /* 1025550a7375SFelipe Balbi * Disable an endpoint flushing all requests queued. 1026550a7375SFelipe Balbi */ 1027550a7375SFelipe Balbi static int musb_gadget_disable(struct usb_ep *ep) 1028550a7375SFelipe Balbi { 1029550a7375SFelipe Balbi unsigned long flags; 1030550a7375SFelipe Balbi struct musb *musb; 1031550a7375SFelipe Balbi u8 epnum; 1032550a7375SFelipe Balbi struct musb_ep *musb_ep; 1033550a7375SFelipe Balbi void __iomem *epio; 1034550a7375SFelipe Balbi int status = 0; 1035550a7375SFelipe Balbi 1036550a7375SFelipe Balbi musb_ep = to_musb_ep(ep); 1037550a7375SFelipe Balbi musb = musb_ep->musb; 1038550a7375SFelipe Balbi epnum = musb_ep->current_epnum; 1039550a7375SFelipe Balbi epio = musb->endpoints[epnum].regs; 1040550a7375SFelipe Balbi 1041550a7375SFelipe Balbi spin_lock_irqsave(&musb->lock, flags); 1042550a7375SFelipe Balbi musb_ep_select(musb->mregs, epnum); 1043550a7375SFelipe Balbi 1044550a7375SFelipe Balbi /* zero the endpoint sizes */ 1045550a7375SFelipe Balbi if (musb_ep->is_in) { 1046550a7375SFelipe Balbi u16 int_txe = musb_readw(musb->mregs, MUSB_INTRTXE); 1047550a7375SFelipe Balbi int_txe &= ~(1 << epnum); 1048550a7375SFelipe Balbi musb_writew(musb->mregs, MUSB_INTRTXE, int_txe); 1049550a7375SFelipe Balbi musb_writew(epio, MUSB_TXMAXP, 0); 1050550a7375SFelipe Balbi } else { 1051550a7375SFelipe Balbi u16 int_rxe = musb_readw(musb->mregs, MUSB_INTRRXE); 1052550a7375SFelipe Balbi int_rxe &= ~(1 << epnum); 1053550a7375SFelipe Balbi musb_writew(musb->mregs, MUSB_INTRRXE, int_rxe); 1054550a7375SFelipe Balbi musb_writew(epio, MUSB_RXMAXP, 0); 1055550a7375SFelipe Balbi } 1056550a7375SFelipe Balbi 1057550a7375SFelipe Balbi musb_ep->desc = NULL; 1058550a7375SFelipe Balbi 1059550a7375SFelipe Balbi /* abort all pending DMA and requests */ 1060550a7375SFelipe Balbi nuke(musb_ep, -ESHUTDOWN); 1061550a7375SFelipe Balbi 1062550a7375SFelipe Balbi schedule_work(&musb->irq_work); 1063550a7375SFelipe Balbi 1064550a7375SFelipe Balbi spin_unlock_irqrestore(&(musb->lock), flags); 1065550a7375SFelipe Balbi 1066550a7375SFelipe Balbi DBG(2, "%s\n", musb_ep->end_point.name); 1067550a7375SFelipe Balbi 1068550a7375SFelipe Balbi return status; 1069550a7375SFelipe Balbi } 1070550a7375SFelipe Balbi 1071550a7375SFelipe Balbi /* 1072550a7375SFelipe Balbi * Allocate a request for an endpoint. 1073550a7375SFelipe Balbi * Reused by ep0 code. 1074550a7375SFelipe Balbi */ 1075550a7375SFelipe Balbi struct usb_request *musb_alloc_request(struct usb_ep *ep, gfp_t gfp_flags) 1076550a7375SFelipe Balbi { 1077550a7375SFelipe Balbi struct musb_ep *musb_ep = to_musb_ep(ep); 1078550a7375SFelipe Balbi struct musb_request *request = NULL; 1079550a7375SFelipe Balbi 1080550a7375SFelipe Balbi request = kzalloc(sizeof *request, gfp_flags); 1081550a7375SFelipe Balbi if (request) { 1082550a7375SFelipe Balbi INIT_LIST_HEAD(&request->request.list); 1083550a7375SFelipe Balbi request->request.dma = DMA_ADDR_INVALID; 1084550a7375SFelipe Balbi request->epnum = musb_ep->current_epnum; 1085550a7375SFelipe Balbi request->ep = musb_ep; 1086550a7375SFelipe Balbi } 1087550a7375SFelipe Balbi 1088550a7375SFelipe Balbi return &request->request; 1089550a7375SFelipe Balbi } 1090550a7375SFelipe Balbi 1091550a7375SFelipe Balbi /* 1092550a7375SFelipe Balbi * Free a request 1093550a7375SFelipe Balbi * Reused by ep0 code. 1094550a7375SFelipe Balbi */ 1095550a7375SFelipe Balbi void musb_free_request(struct usb_ep *ep, struct usb_request *req) 1096550a7375SFelipe Balbi { 1097550a7375SFelipe Balbi kfree(to_musb_request(req)); 1098550a7375SFelipe Balbi } 1099550a7375SFelipe Balbi 1100550a7375SFelipe Balbi static LIST_HEAD(buffers); 1101550a7375SFelipe Balbi 1102550a7375SFelipe Balbi struct free_record { 1103550a7375SFelipe Balbi struct list_head list; 1104550a7375SFelipe Balbi struct device *dev; 1105550a7375SFelipe Balbi unsigned bytes; 1106550a7375SFelipe Balbi dma_addr_t dma; 1107550a7375SFelipe Balbi }; 1108550a7375SFelipe Balbi 1109550a7375SFelipe Balbi /* 1110550a7375SFelipe Balbi * Context: controller locked, IRQs blocked. 1111550a7375SFelipe Balbi */ 1112a666e3e6SSergei Shtylyov void musb_ep_restart(struct musb *musb, struct musb_request *req) 1113550a7375SFelipe Balbi { 1114550a7375SFelipe Balbi DBG(3, "<== %s request %p len %u on hw_ep%d\n", 1115550a7375SFelipe Balbi req->tx ? "TX/IN" : "RX/OUT", 1116550a7375SFelipe Balbi &req->request, req->request.length, req->epnum); 1117550a7375SFelipe Balbi 1118550a7375SFelipe Balbi musb_ep_select(musb->mregs, req->epnum); 1119550a7375SFelipe Balbi if (req->tx) 1120550a7375SFelipe Balbi txstate(musb, req); 1121550a7375SFelipe Balbi else 1122550a7375SFelipe Balbi rxstate(musb, req); 1123550a7375SFelipe Balbi } 1124550a7375SFelipe Balbi 1125550a7375SFelipe Balbi static int musb_gadget_queue(struct usb_ep *ep, struct usb_request *req, 1126550a7375SFelipe Balbi gfp_t gfp_flags) 1127550a7375SFelipe Balbi { 1128550a7375SFelipe Balbi struct musb_ep *musb_ep; 1129550a7375SFelipe Balbi struct musb_request *request; 1130550a7375SFelipe Balbi struct musb *musb; 1131550a7375SFelipe Balbi int status = 0; 1132550a7375SFelipe Balbi unsigned long lockflags; 1133550a7375SFelipe Balbi 1134550a7375SFelipe Balbi if (!ep || !req) 1135550a7375SFelipe Balbi return -EINVAL; 1136550a7375SFelipe Balbi if (!req->buf) 1137550a7375SFelipe Balbi return -ENODATA; 1138550a7375SFelipe Balbi 1139550a7375SFelipe Balbi musb_ep = to_musb_ep(ep); 1140550a7375SFelipe Balbi musb = musb_ep->musb; 1141550a7375SFelipe Balbi 1142550a7375SFelipe Balbi request = to_musb_request(req); 1143550a7375SFelipe Balbi request->musb = musb; 1144550a7375SFelipe Balbi 1145550a7375SFelipe Balbi if (request->ep != musb_ep) 1146550a7375SFelipe Balbi return -EINVAL; 1147550a7375SFelipe Balbi 1148550a7375SFelipe Balbi DBG(4, "<== to %s request=%p\n", ep->name, req); 1149550a7375SFelipe Balbi 1150550a7375SFelipe Balbi /* request is mine now... */ 1151550a7375SFelipe Balbi request->request.actual = 0; 1152550a7375SFelipe Balbi request->request.status = -EINPROGRESS; 1153550a7375SFelipe Balbi request->epnum = musb_ep->current_epnum; 1154550a7375SFelipe Balbi request->tx = musb_ep->is_in; 1155550a7375SFelipe Balbi 1156550a7375SFelipe Balbi if (is_dma_capable() && musb_ep->dma) { 1157550a7375SFelipe Balbi if (request->request.dma == DMA_ADDR_INVALID) { 1158550a7375SFelipe Balbi request->request.dma = dma_map_single( 1159550a7375SFelipe Balbi musb->controller, 1160550a7375SFelipe Balbi request->request.buf, 1161550a7375SFelipe Balbi request->request.length, 1162550a7375SFelipe Balbi request->tx 1163550a7375SFelipe Balbi ? DMA_TO_DEVICE 1164550a7375SFelipe Balbi : DMA_FROM_DEVICE); 1165550a7375SFelipe Balbi request->mapped = 1; 1166550a7375SFelipe Balbi } else { 1167550a7375SFelipe Balbi dma_sync_single_for_device(musb->controller, 1168550a7375SFelipe Balbi request->request.dma, 1169550a7375SFelipe Balbi request->request.length, 1170550a7375SFelipe Balbi request->tx 1171550a7375SFelipe Balbi ? DMA_TO_DEVICE 1172550a7375SFelipe Balbi : DMA_FROM_DEVICE); 1173550a7375SFelipe Balbi request->mapped = 0; 1174550a7375SFelipe Balbi } 1175550a7375SFelipe Balbi } else if (!req->buf) { 1176550a7375SFelipe Balbi return -ENODATA; 1177550a7375SFelipe Balbi } else 1178550a7375SFelipe Balbi request->mapped = 0; 1179550a7375SFelipe Balbi 1180550a7375SFelipe Balbi spin_lock_irqsave(&musb->lock, lockflags); 1181550a7375SFelipe Balbi 1182550a7375SFelipe Balbi /* don't queue if the ep is down */ 1183550a7375SFelipe Balbi if (!musb_ep->desc) { 1184550a7375SFelipe Balbi DBG(4, "req %p queued to %s while ep %s\n", 1185550a7375SFelipe Balbi req, ep->name, "disabled"); 1186550a7375SFelipe Balbi status = -ESHUTDOWN; 1187550a7375SFelipe Balbi goto cleanup; 1188550a7375SFelipe Balbi } 1189550a7375SFelipe Balbi 1190550a7375SFelipe Balbi /* add request to the list */ 1191550a7375SFelipe Balbi list_add_tail(&(request->request.list), &(musb_ep->req_list)); 1192550a7375SFelipe Balbi 1193550a7375SFelipe Balbi /* it this is the head of the queue, start i/o ... */ 1194550a7375SFelipe Balbi if (!musb_ep->busy && &request->request.list == musb_ep->req_list.next) 1195550a7375SFelipe Balbi musb_ep_restart(musb, request); 1196550a7375SFelipe Balbi 1197550a7375SFelipe Balbi cleanup: 1198550a7375SFelipe Balbi spin_unlock_irqrestore(&musb->lock, lockflags); 1199550a7375SFelipe Balbi return status; 1200550a7375SFelipe Balbi } 1201550a7375SFelipe Balbi 1202550a7375SFelipe Balbi static int musb_gadget_dequeue(struct usb_ep *ep, struct usb_request *request) 1203550a7375SFelipe Balbi { 1204550a7375SFelipe Balbi struct musb_ep *musb_ep = to_musb_ep(ep); 1205550a7375SFelipe Balbi struct usb_request *r; 1206550a7375SFelipe Balbi unsigned long flags; 1207550a7375SFelipe Balbi int status = 0; 1208550a7375SFelipe Balbi struct musb *musb = musb_ep->musb; 1209550a7375SFelipe Balbi 1210550a7375SFelipe Balbi if (!ep || !request || to_musb_request(request)->ep != musb_ep) 1211550a7375SFelipe Balbi return -EINVAL; 1212550a7375SFelipe Balbi 1213550a7375SFelipe Balbi spin_lock_irqsave(&musb->lock, flags); 1214550a7375SFelipe Balbi 1215550a7375SFelipe Balbi list_for_each_entry(r, &musb_ep->req_list, list) { 1216550a7375SFelipe Balbi if (r == request) 1217550a7375SFelipe Balbi break; 1218550a7375SFelipe Balbi } 1219550a7375SFelipe Balbi if (r != request) { 1220550a7375SFelipe Balbi DBG(3, "request %p not queued to %s\n", request, ep->name); 1221550a7375SFelipe Balbi status = -EINVAL; 1222550a7375SFelipe Balbi goto done; 1223550a7375SFelipe Balbi } 1224550a7375SFelipe Balbi 1225550a7375SFelipe Balbi /* if the hardware doesn't have the request, easy ... */ 1226550a7375SFelipe Balbi if (musb_ep->req_list.next != &request->list || musb_ep->busy) 1227550a7375SFelipe Balbi musb_g_giveback(musb_ep, request, -ECONNRESET); 1228550a7375SFelipe Balbi 1229550a7375SFelipe Balbi /* ... else abort the dma transfer ... */ 1230550a7375SFelipe Balbi else if (is_dma_capable() && musb_ep->dma) { 1231550a7375SFelipe Balbi struct dma_controller *c = musb->dma_controller; 1232550a7375SFelipe Balbi 1233550a7375SFelipe Balbi musb_ep_select(musb->mregs, musb_ep->current_epnum); 1234550a7375SFelipe Balbi if (c->channel_abort) 1235550a7375SFelipe Balbi status = c->channel_abort(musb_ep->dma); 1236550a7375SFelipe Balbi else 1237550a7375SFelipe Balbi status = -EBUSY; 1238550a7375SFelipe Balbi if (status == 0) 1239550a7375SFelipe Balbi musb_g_giveback(musb_ep, request, -ECONNRESET); 1240550a7375SFelipe Balbi } else { 1241550a7375SFelipe Balbi /* NOTE: by sticking to easily tested hardware/driver states, 1242550a7375SFelipe Balbi * we leave counting of in-flight packets imprecise. 1243550a7375SFelipe Balbi */ 1244550a7375SFelipe Balbi musb_g_giveback(musb_ep, request, -ECONNRESET); 1245550a7375SFelipe Balbi } 1246550a7375SFelipe Balbi 1247550a7375SFelipe Balbi done: 1248550a7375SFelipe Balbi spin_unlock_irqrestore(&musb->lock, flags); 1249550a7375SFelipe Balbi return status; 1250550a7375SFelipe Balbi } 1251550a7375SFelipe Balbi 1252550a7375SFelipe Balbi /* 1253550a7375SFelipe Balbi * Set or clear the halt bit of an endpoint. A halted enpoint won't tx/rx any 1254550a7375SFelipe Balbi * data but will queue requests. 1255550a7375SFelipe Balbi * 1256550a7375SFelipe Balbi * exported to ep0 code 1257550a7375SFelipe Balbi */ 12581b6c3b0fSFelipe Balbi static int musb_gadget_set_halt(struct usb_ep *ep, int value) 1259550a7375SFelipe Balbi { 1260550a7375SFelipe Balbi struct musb_ep *musb_ep = to_musb_ep(ep); 1261550a7375SFelipe Balbi u8 epnum = musb_ep->current_epnum; 1262550a7375SFelipe Balbi struct musb *musb = musb_ep->musb; 1263550a7375SFelipe Balbi void __iomem *epio = musb->endpoints[epnum].regs; 1264550a7375SFelipe Balbi void __iomem *mbase; 1265550a7375SFelipe Balbi unsigned long flags; 1266550a7375SFelipe Balbi u16 csr; 1267cea83241SSergei Shtylyov struct musb_request *request; 1268550a7375SFelipe Balbi int status = 0; 1269550a7375SFelipe Balbi 1270550a7375SFelipe Balbi if (!ep) 1271550a7375SFelipe Balbi return -EINVAL; 1272550a7375SFelipe Balbi mbase = musb->mregs; 1273550a7375SFelipe Balbi 1274550a7375SFelipe Balbi spin_lock_irqsave(&musb->lock, flags); 1275550a7375SFelipe Balbi 1276550a7375SFelipe Balbi if ((USB_ENDPOINT_XFER_ISOC == musb_ep->type)) { 1277550a7375SFelipe Balbi status = -EINVAL; 1278550a7375SFelipe Balbi goto done; 1279550a7375SFelipe Balbi } 1280550a7375SFelipe Balbi 1281550a7375SFelipe Balbi musb_ep_select(mbase, epnum); 1282550a7375SFelipe Balbi 1283550a7375SFelipe Balbi request = to_musb_request(next_request(musb_ep)); 1284cea83241SSergei Shtylyov if (value) { 1285cea83241SSergei Shtylyov if (request) { 1286cea83241SSergei Shtylyov DBG(3, "request in progress, cannot halt %s\n", 1287cea83241SSergei Shtylyov ep->name); 1288cea83241SSergei Shtylyov status = -EAGAIN; 1289cea83241SSergei Shtylyov goto done; 1290cea83241SSergei Shtylyov } 1291cea83241SSergei Shtylyov /* Cannot portably stall with non-empty FIFO */ 1292cea83241SSergei Shtylyov if (musb_ep->is_in) { 1293550a7375SFelipe Balbi csr = musb_readw(epio, MUSB_TXCSR); 1294550a7375SFelipe Balbi if (csr & MUSB_TXCSR_FIFONOTEMPTY) { 1295cea83241SSergei Shtylyov DBG(3, "FIFO busy, cannot halt %s\n", ep->name); 1296cea83241SSergei Shtylyov status = -EAGAIN; 1297cea83241SSergei Shtylyov goto done; 1298550a7375SFelipe Balbi } 1299cea83241SSergei Shtylyov } 130047e97605SSergei Shtylyov } else 130147e97605SSergei Shtylyov musb_ep->wedged = 0; 1302550a7375SFelipe Balbi 1303550a7375SFelipe Balbi /* set/clear the stall and toggle bits */ 1304550a7375SFelipe Balbi DBG(2, "%s: %s stall\n", ep->name, value ? "set" : "clear"); 1305550a7375SFelipe Balbi if (musb_ep->is_in) { 1306550a7375SFelipe Balbi csr = musb_readw(epio, MUSB_TXCSR); 1307550a7375SFelipe Balbi csr |= MUSB_TXCSR_P_WZC_BITS 1308550a7375SFelipe Balbi | MUSB_TXCSR_CLRDATATOG; 1309550a7375SFelipe Balbi if (value) 1310550a7375SFelipe Balbi csr |= MUSB_TXCSR_P_SENDSTALL; 1311550a7375SFelipe Balbi else 1312550a7375SFelipe Balbi csr &= ~(MUSB_TXCSR_P_SENDSTALL 1313550a7375SFelipe Balbi | MUSB_TXCSR_P_SENTSTALL); 1314550a7375SFelipe Balbi csr &= ~MUSB_TXCSR_TXPKTRDY; 1315550a7375SFelipe Balbi musb_writew(epio, MUSB_TXCSR, csr); 1316550a7375SFelipe Balbi } else { 1317550a7375SFelipe Balbi csr = musb_readw(epio, MUSB_RXCSR); 1318550a7375SFelipe Balbi csr |= MUSB_RXCSR_P_WZC_BITS 1319550a7375SFelipe Balbi | MUSB_RXCSR_FLUSHFIFO 1320550a7375SFelipe Balbi | MUSB_RXCSR_CLRDATATOG; 1321550a7375SFelipe Balbi if (value) 1322550a7375SFelipe Balbi csr |= MUSB_RXCSR_P_SENDSTALL; 1323550a7375SFelipe Balbi else 1324550a7375SFelipe Balbi csr &= ~(MUSB_RXCSR_P_SENDSTALL 1325550a7375SFelipe Balbi | MUSB_RXCSR_P_SENTSTALL); 1326550a7375SFelipe Balbi musb_writew(epio, MUSB_RXCSR, csr); 1327550a7375SFelipe Balbi } 1328550a7375SFelipe Balbi 1329550a7375SFelipe Balbi /* maybe start the first request in the queue */ 1330550a7375SFelipe Balbi if (!musb_ep->busy && !value && request) { 1331550a7375SFelipe Balbi DBG(3, "restarting the request\n"); 1332550a7375SFelipe Balbi musb_ep_restart(musb, request); 1333550a7375SFelipe Balbi } 1334550a7375SFelipe Balbi 1335cea83241SSergei Shtylyov done: 1336550a7375SFelipe Balbi spin_unlock_irqrestore(&musb->lock, flags); 1337550a7375SFelipe Balbi return status; 1338550a7375SFelipe Balbi } 1339550a7375SFelipe Balbi 134047e97605SSergei Shtylyov /* 134147e97605SSergei Shtylyov * Sets the halt feature with the clear requests ignored 134247e97605SSergei Shtylyov */ 13431b6c3b0fSFelipe Balbi static int musb_gadget_set_wedge(struct usb_ep *ep) 134447e97605SSergei Shtylyov { 134547e97605SSergei Shtylyov struct musb_ep *musb_ep = to_musb_ep(ep); 134647e97605SSergei Shtylyov 134747e97605SSergei Shtylyov if (!ep) 134847e97605SSergei Shtylyov return -EINVAL; 134947e97605SSergei Shtylyov 135047e97605SSergei Shtylyov musb_ep->wedged = 1; 135147e97605SSergei Shtylyov 135247e97605SSergei Shtylyov return usb_ep_set_halt(ep); 135347e97605SSergei Shtylyov } 135447e97605SSergei Shtylyov 1355550a7375SFelipe Balbi static int musb_gadget_fifo_status(struct usb_ep *ep) 1356550a7375SFelipe Balbi { 1357550a7375SFelipe Balbi struct musb_ep *musb_ep = to_musb_ep(ep); 1358550a7375SFelipe Balbi void __iomem *epio = musb_ep->hw_ep->regs; 1359550a7375SFelipe Balbi int retval = -EINVAL; 1360550a7375SFelipe Balbi 1361550a7375SFelipe Balbi if (musb_ep->desc && !musb_ep->is_in) { 1362550a7375SFelipe Balbi struct musb *musb = musb_ep->musb; 1363550a7375SFelipe Balbi int epnum = musb_ep->current_epnum; 1364550a7375SFelipe Balbi void __iomem *mbase = musb->mregs; 1365550a7375SFelipe Balbi unsigned long flags; 1366550a7375SFelipe Balbi 1367550a7375SFelipe Balbi spin_lock_irqsave(&musb->lock, flags); 1368550a7375SFelipe Balbi 1369550a7375SFelipe Balbi musb_ep_select(mbase, epnum); 1370550a7375SFelipe Balbi /* FIXME return zero unless RXPKTRDY is set */ 1371550a7375SFelipe Balbi retval = musb_readw(epio, MUSB_RXCOUNT); 1372550a7375SFelipe Balbi 1373550a7375SFelipe Balbi spin_unlock_irqrestore(&musb->lock, flags); 1374550a7375SFelipe Balbi } 1375550a7375SFelipe Balbi return retval; 1376550a7375SFelipe Balbi } 1377550a7375SFelipe Balbi 1378550a7375SFelipe Balbi static void musb_gadget_fifo_flush(struct usb_ep *ep) 1379550a7375SFelipe Balbi { 1380550a7375SFelipe Balbi struct musb_ep *musb_ep = to_musb_ep(ep); 1381550a7375SFelipe Balbi struct musb *musb = musb_ep->musb; 1382550a7375SFelipe Balbi u8 epnum = musb_ep->current_epnum; 1383550a7375SFelipe Balbi void __iomem *epio = musb->endpoints[epnum].regs; 1384550a7375SFelipe Balbi void __iomem *mbase; 1385550a7375SFelipe Balbi unsigned long flags; 1386550a7375SFelipe Balbi u16 csr, int_txe; 1387550a7375SFelipe Balbi 1388550a7375SFelipe Balbi mbase = musb->mregs; 1389550a7375SFelipe Balbi 1390550a7375SFelipe Balbi spin_lock_irqsave(&musb->lock, flags); 1391550a7375SFelipe Balbi musb_ep_select(mbase, (u8) epnum); 1392550a7375SFelipe Balbi 1393550a7375SFelipe Balbi /* disable interrupts */ 1394550a7375SFelipe Balbi int_txe = musb_readw(mbase, MUSB_INTRTXE); 1395550a7375SFelipe Balbi musb_writew(mbase, MUSB_INTRTXE, int_txe & ~(1 << epnum)); 1396550a7375SFelipe Balbi 1397550a7375SFelipe Balbi if (musb_ep->is_in) { 1398550a7375SFelipe Balbi csr = musb_readw(epio, MUSB_TXCSR); 1399550a7375SFelipe Balbi if (csr & MUSB_TXCSR_FIFONOTEMPTY) { 1400550a7375SFelipe Balbi csr |= MUSB_TXCSR_FLUSHFIFO | MUSB_TXCSR_P_WZC_BITS; 1401550a7375SFelipe Balbi musb_writew(epio, MUSB_TXCSR, csr); 1402550a7375SFelipe Balbi /* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */ 1403550a7375SFelipe Balbi musb_writew(epio, MUSB_TXCSR, csr); 1404550a7375SFelipe Balbi } 1405550a7375SFelipe Balbi } else { 1406550a7375SFelipe Balbi csr = musb_readw(epio, MUSB_RXCSR); 1407550a7375SFelipe Balbi csr |= MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_P_WZC_BITS; 1408550a7375SFelipe Balbi musb_writew(epio, MUSB_RXCSR, csr); 1409550a7375SFelipe Balbi musb_writew(epio, MUSB_RXCSR, csr); 1410550a7375SFelipe Balbi } 1411550a7375SFelipe Balbi 1412550a7375SFelipe Balbi /* re-enable interrupt */ 1413550a7375SFelipe Balbi musb_writew(mbase, MUSB_INTRTXE, int_txe); 1414550a7375SFelipe Balbi spin_unlock_irqrestore(&musb->lock, flags); 1415550a7375SFelipe Balbi } 1416550a7375SFelipe Balbi 1417550a7375SFelipe Balbi static const struct usb_ep_ops musb_ep_ops = { 1418550a7375SFelipe Balbi .enable = musb_gadget_enable, 1419550a7375SFelipe Balbi .disable = musb_gadget_disable, 1420550a7375SFelipe Balbi .alloc_request = musb_alloc_request, 1421550a7375SFelipe Balbi .free_request = musb_free_request, 1422550a7375SFelipe Balbi .queue = musb_gadget_queue, 1423550a7375SFelipe Balbi .dequeue = musb_gadget_dequeue, 1424550a7375SFelipe Balbi .set_halt = musb_gadget_set_halt, 142547e97605SSergei Shtylyov .set_wedge = musb_gadget_set_wedge, 1426550a7375SFelipe Balbi .fifo_status = musb_gadget_fifo_status, 1427550a7375SFelipe Balbi .fifo_flush = musb_gadget_fifo_flush 1428550a7375SFelipe Balbi }; 1429550a7375SFelipe Balbi 1430550a7375SFelipe Balbi /* ----------------------------------------------------------------------- */ 1431550a7375SFelipe Balbi 1432550a7375SFelipe Balbi static int musb_gadget_get_frame(struct usb_gadget *gadget) 1433550a7375SFelipe Balbi { 1434550a7375SFelipe Balbi struct musb *musb = gadget_to_musb(gadget); 1435550a7375SFelipe Balbi 1436550a7375SFelipe Balbi return (int)musb_readw(musb->mregs, MUSB_FRAME); 1437550a7375SFelipe Balbi } 1438550a7375SFelipe Balbi 1439550a7375SFelipe Balbi static int musb_gadget_wakeup(struct usb_gadget *gadget) 1440550a7375SFelipe Balbi { 1441550a7375SFelipe Balbi struct musb *musb = gadget_to_musb(gadget); 1442550a7375SFelipe Balbi void __iomem *mregs = musb->mregs; 1443550a7375SFelipe Balbi unsigned long flags; 1444550a7375SFelipe Balbi int status = -EINVAL; 1445550a7375SFelipe Balbi u8 power, devctl; 1446550a7375SFelipe Balbi int retries; 1447550a7375SFelipe Balbi 1448550a7375SFelipe Balbi spin_lock_irqsave(&musb->lock, flags); 1449550a7375SFelipe Balbi 145084e250ffSDavid Brownell switch (musb->xceiv->state) { 1451550a7375SFelipe Balbi case OTG_STATE_B_PERIPHERAL: 1452550a7375SFelipe Balbi /* NOTE: OTG state machine doesn't include B_SUSPENDED; 1453550a7375SFelipe Balbi * that's part of the standard usb 1.1 state machine, and 1454550a7375SFelipe Balbi * doesn't affect OTG transitions. 1455550a7375SFelipe Balbi */ 1456550a7375SFelipe Balbi if (musb->may_wakeup && musb->is_suspended) 1457550a7375SFelipe Balbi break; 1458550a7375SFelipe Balbi goto done; 1459550a7375SFelipe Balbi case OTG_STATE_B_IDLE: 1460550a7375SFelipe Balbi /* Start SRP ... OTG not required. */ 1461550a7375SFelipe Balbi devctl = musb_readb(mregs, MUSB_DEVCTL); 1462550a7375SFelipe Balbi DBG(2, "Sending SRP: devctl: %02x\n", devctl); 1463550a7375SFelipe Balbi devctl |= MUSB_DEVCTL_SESSION; 1464550a7375SFelipe Balbi musb_writeb(mregs, MUSB_DEVCTL, devctl); 1465550a7375SFelipe Balbi devctl = musb_readb(mregs, MUSB_DEVCTL); 1466550a7375SFelipe Balbi retries = 100; 1467550a7375SFelipe Balbi while (!(devctl & MUSB_DEVCTL_SESSION)) { 1468550a7375SFelipe Balbi devctl = musb_readb(mregs, MUSB_DEVCTL); 1469550a7375SFelipe Balbi if (retries-- < 1) 1470550a7375SFelipe Balbi break; 1471550a7375SFelipe Balbi } 1472550a7375SFelipe Balbi retries = 10000; 1473550a7375SFelipe Balbi while (devctl & MUSB_DEVCTL_SESSION) { 1474550a7375SFelipe Balbi devctl = musb_readb(mregs, MUSB_DEVCTL); 1475550a7375SFelipe Balbi if (retries-- < 1) 1476550a7375SFelipe Balbi break; 1477550a7375SFelipe Balbi } 1478550a7375SFelipe Balbi 1479550a7375SFelipe Balbi /* Block idling for at least 1s */ 1480550a7375SFelipe Balbi musb_platform_try_idle(musb, 1481550a7375SFelipe Balbi jiffies + msecs_to_jiffies(1 * HZ)); 1482550a7375SFelipe Balbi 1483550a7375SFelipe Balbi status = 0; 1484550a7375SFelipe Balbi goto done; 1485550a7375SFelipe Balbi default: 1486550a7375SFelipe Balbi DBG(2, "Unhandled wake: %s\n", otg_state_string(musb)); 1487550a7375SFelipe Balbi goto done; 1488550a7375SFelipe Balbi } 1489550a7375SFelipe Balbi 1490550a7375SFelipe Balbi status = 0; 1491550a7375SFelipe Balbi 1492550a7375SFelipe Balbi power = musb_readb(mregs, MUSB_POWER); 1493550a7375SFelipe Balbi power |= MUSB_POWER_RESUME; 1494550a7375SFelipe Balbi musb_writeb(mregs, MUSB_POWER, power); 1495550a7375SFelipe Balbi DBG(2, "issue wakeup\n"); 1496550a7375SFelipe Balbi 1497550a7375SFelipe Balbi /* FIXME do this next chunk in a timer callback, no udelay */ 1498550a7375SFelipe Balbi mdelay(2); 1499550a7375SFelipe Balbi 1500550a7375SFelipe Balbi power = musb_readb(mregs, MUSB_POWER); 1501550a7375SFelipe Balbi power &= ~MUSB_POWER_RESUME; 1502550a7375SFelipe Balbi musb_writeb(mregs, MUSB_POWER, power); 1503550a7375SFelipe Balbi done: 1504550a7375SFelipe Balbi spin_unlock_irqrestore(&musb->lock, flags); 1505550a7375SFelipe Balbi return status; 1506550a7375SFelipe Balbi } 1507550a7375SFelipe Balbi 1508550a7375SFelipe Balbi static int 1509550a7375SFelipe Balbi musb_gadget_set_self_powered(struct usb_gadget *gadget, int is_selfpowered) 1510550a7375SFelipe Balbi { 1511550a7375SFelipe Balbi struct musb *musb = gadget_to_musb(gadget); 1512550a7375SFelipe Balbi 1513550a7375SFelipe Balbi musb->is_self_powered = !!is_selfpowered; 1514550a7375SFelipe Balbi return 0; 1515550a7375SFelipe Balbi } 1516550a7375SFelipe Balbi 1517550a7375SFelipe Balbi static void musb_pullup(struct musb *musb, int is_on) 1518550a7375SFelipe Balbi { 1519550a7375SFelipe Balbi u8 power; 1520550a7375SFelipe Balbi 1521550a7375SFelipe Balbi power = musb_readb(musb->mregs, MUSB_POWER); 1522550a7375SFelipe Balbi if (is_on) 1523550a7375SFelipe Balbi power |= MUSB_POWER_SOFTCONN; 1524550a7375SFelipe Balbi else 1525550a7375SFelipe Balbi power &= ~MUSB_POWER_SOFTCONN; 1526550a7375SFelipe Balbi 1527550a7375SFelipe Balbi /* FIXME if on, HdrcStart; if off, HdrcStop */ 1528550a7375SFelipe Balbi 1529550a7375SFelipe Balbi DBG(3, "gadget %s D+ pullup %s\n", 1530550a7375SFelipe Balbi musb->gadget_driver->function, is_on ? "on" : "off"); 1531550a7375SFelipe Balbi musb_writeb(musb->mregs, MUSB_POWER, power); 1532550a7375SFelipe Balbi } 1533550a7375SFelipe Balbi 1534550a7375SFelipe Balbi #if 0 1535550a7375SFelipe Balbi static int musb_gadget_vbus_session(struct usb_gadget *gadget, int is_active) 1536550a7375SFelipe Balbi { 1537550a7375SFelipe Balbi DBG(2, "<= %s =>\n", __func__); 1538550a7375SFelipe Balbi 1539550a7375SFelipe Balbi /* 1540550a7375SFelipe Balbi * FIXME iff driver's softconnect flag is set (as it is during probe, 1541550a7375SFelipe Balbi * though that can clear it), just musb_pullup(). 1542550a7375SFelipe Balbi */ 1543550a7375SFelipe Balbi 1544550a7375SFelipe Balbi return -EINVAL; 1545550a7375SFelipe Balbi } 1546550a7375SFelipe Balbi #endif 1547550a7375SFelipe Balbi 1548550a7375SFelipe Balbi static int musb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA) 1549550a7375SFelipe Balbi { 1550550a7375SFelipe Balbi struct musb *musb = gadget_to_musb(gadget); 1551550a7375SFelipe Balbi 155284e250ffSDavid Brownell if (!musb->xceiv->set_power) 1553550a7375SFelipe Balbi return -EOPNOTSUPP; 155484e250ffSDavid Brownell return otg_set_power(musb->xceiv, mA); 1555550a7375SFelipe Balbi } 1556550a7375SFelipe Balbi 1557550a7375SFelipe Balbi static int musb_gadget_pullup(struct usb_gadget *gadget, int is_on) 1558550a7375SFelipe Balbi { 1559550a7375SFelipe Balbi struct musb *musb = gadget_to_musb(gadget); 1560550a7375SFelipe Balbi unsigned long flags; 1561550a7375SFelipe Balbi 1562550a7375SFelipe Balbi is_on = !!is_on; 1563550a7375SFelipe Balbi 1564550a7375SFelipe Balbi /* NOTE: this assumes we are sensing vbus; we'd rather 1565550a7375SFelipe Balbi * not pullup unless the B-session is active. 1566550a7375SFelipe Balbi */ 1567550a7375SFelipe Balbi spin_lock_irqsave(&musb->lock, flags); 1568550a7375SFelipe Balbi if (is_on != musb->softconnect) { 1569550a7375SFelipe Balbi musb->softconnect = is_on; 1570550a7375SFelipe Balbi musb_pullup(musb, is_on); 1571550a7375SFelipe Balbi } 1572550a7375SFelipe Balbi spin_unlock_irqrestore(&musb->lock, flags); 1573550a7375SFelipe Balbi return 0; 1574550a7375SFelipe Balbi } 1575550a7375SFelipe Balbi 1576550a7375SFelipe Balbi static const struct usb_gadget_ops musb_gadget_operations = { 1577550a7375SFelipe Balbi .get_frame = musb_gadget_get_frame, 1578550a7375SFelipe Balbi .wakeup = musb_gadget_wakeup, 1579550a7375SFelipe Balbi .set_selfpowered = musb_gadget_set_self_powered, 1580550a7375SFelipe Balbi /* .vbus_session = musb_gadget_vbus_session, */ 1581550a7375SFelipe Balbi .vbus_draw = musb_gadget_vbus_draw, 1582550a7375SFelipe Balbi .pullup = musb_gadget_pullup, 1583550a7375SFelipe Balbi }; 1584550a7375SFelipe Balbi 1585550a7375SFelipe Balbi /* ----------------------------------------------------------------------- */ 1586550a7375SFelipe Balbi 1587550a7375SFelipe Balbi /* Registration */ 1588550a7375SFelipe Balbi 1589550a7375SFelipe Balbi /* Only this registration code "knows" the rule (from USB standards) 1590550a7375SFelipe Balbi * about there being only one external upstream port. It assumes 1591550a7375SFelipe Balbi * all peripheral ports are external... 1592550a7375SFelipe Balbi */ 1593550a7375SFelipe Balbi static struct musb *the_gadget; 1594550a7375SFelipe Balbi 1595550a7375SFelipe Balbi static void musb_gadget_release(struct device *dev) 1596550a7375SFelipe Balbi { 1597550a7375SFelipe Balbi /* kref_put(WHAT) */ 1598550a7375SFelipe Balbi dev_dbg(dev, "%s\n", __func__); 1599550a7375SFelipe Balbi } 1600550a7375SFelipe Balbi 1601550a7375SFelipe Balbi 1602550a7375SFelipe Balbi static void __init 1603550a7375SFelipe Balbi init_peripheral_ep(struct musb *musb, struct musb_ep *ep, u8 epnum, int is_in) 1604550a7375SFelipe Balbi { 1605550a7375SFelipe Balbi struct musb_hw_ep *hw_ep = musb->endpoints + epnum; 1606550a7375SFelipe Balbi 1607550a7375SFelipe Balbi memset(ep, 0, sizeof *ep); 1608550a7375SFelipe Balbi 1609550a7375SFelipe Balbi ep->current_epnum = epnum; 1610550a7375SFelipe Balbi ep->musb = musb; 1611550a7375SFelipe Balbi ep->hw_ep = hw_ep; 1612550a7375SFelipe Balbi ep->is_in = is_in; 1613550a7375SFelipe Balbi 1614550a7375SFelipe Balbi INIT_LIST_HEAD(&ep->req_list); 1615550a7375SFelipe Balbi 1616550a7375SFelipe Balbi sprintf(ep->name, "ep%d%s", epnum, 1617550a7375SFelipe Balbi (!epnum || hw_ep->is_shared_fifo) ? "" : ( 1618550a7375SFelipe Balbi is_in ? "in" : "out")); 1619550a7375SFelipe Balbi ep->end_point.name = ep->name; 1620550a7375SFelipe Balbi INIT_LIST_HEAD(&ep->end_point.ep_list); 1621550a7375SFelipe Balbi if (!epnum) { 1622550a7375SFelipe Balbi ep->end_point.maxpacket = 64; 1623550a7375SFelipe Balbi ep->end_point.ops = &musb_g_ep0_ops; 1624550a7375SFelipe Balbi musb->g.ep0 = &ep->end_point; 1625550a7375SFelipe Balbi } else { 1626550a7375SFelipe Balbi if (is_in) 1627550a7375SFelipe Balbi ep->end_point.maxpacket = hw_ep->max_packet_sz_tx; 1628550a7375SFelipe Balbi else 1629550a7375SFelipe Balbi ep->end_point.maxpacket = hw_ep->max_packet_sz_rx; 1630550a7375SFelipe Balbi ep->end_point.ops = &musb_ep_ops; 1631550a7375SFelipe Balbi list_add_tail(&ep->end_point.ep_list, &musb->g.ep_list); 1632550a7375SFelipe Balbi } 1633550a7375SFelipe Balbi } 1634550a7375SFelipe Balbi 1635550a7375SFelipe Balbi /* 1636550a7375SFelipe Balbi * Initialize the endpoints exposed to peripheral drivers, with backlinks 1637550a7375SFelipe Balbi * to the rest of the driver state. 1638550a7375SFelipe Balbi */ 1639550a7375SFelipe Balbi static inline void __init musb_g_init_endpoints(struct musb *musb) 1640550a7375SFelipe Balbi { 1641550a7375SFelipe Balbi u8 epnum; 1642550a7375SFelipe Balbi struct musb_hw_ep *hw_ep; 1643550a7375SFelipe Balbi unsigned count = 0; 1644550a7375SFelipe Balbi 1645550a7375SFelipe Balbi /* intialize endpoint list just once */ 1646550a7375SFelipe Balbi INIT_LIST_HEAD(&(musb->g.ep_list)); 1647550a7375SFelipe Balbi 1648550a7375SFelipe Balbi for (epnum = 0, hw_ep = musb->endpoints; 1649550a7375SFelipe Balbi epnum < musb->nr_endpoints; 1650550a7375SFelipe Balbi epnum++, hw_ep++) { 1651550a7375SFelipe Balbi if (hw_ep->is_shared_fifo /* || !epnum */) { 1652550a7375SFelipe Balbi init_peripheral_ep(musb, &hw_ep->ep_in, epnum, 0); 1653550a7375SFelipe Balbi count++; 1654550a7375SFelipe Balbi } else { 1655550a7375SFelipe Balbi if (hw_ep->max_packet_sz_tx) { 1656550a7375SFelipe Balbi init_peripheral_ep(musb, &hw_ep->ep_in, 1657550a7375SFelipe Balbi epnum, 1); 1658550a7375SFelipe Balbi count++; 1659550a7375SFelipe Balbi } 1660550a7375SFelipe Balbi if (hw_ep->max_packet_sz_rx) { 1661550a7375SFelipe Balbi init_peripheral_ep(musb, &hw_ep->ep_out, 1662550a7375SFelipe Balbi epnum, 0); 1663550a7375SFelipe Balbi count++; 1664550a7375SFelipe Balbi } 1665550a7375SFelipe Balbi } 1666550a7375SFelipe Balbi } 1667550a7375SFelipe Balbi } 1668550a7375SFelipe Balbi 1669550a7375SFelipe Balbi /* called once during driver setup to initialize and link into 1670550a7375SFelipe Balbi * the driver model; memory is zeroed. 1671550a7375SFelipe Balbi */ 1672550a7375SFelipe Balbi int __init musb_gadget_setup(struct musb *musb) 1673550a7375SFelipe Balbi { 1674550a7375SFelipe Balbi int status; 1675550a7375SFelipe Balbi 1676550a7375SFelipe Balbi /* REVISIT minor race: if (erroneously) setting up two 1677550a7375SFelipe Balbi * musb peripherals at the same time, only the bus lock 1678550a7375SFelipe Balbi * is probably held. 1679550a7375SFelipe Balbi */ 1680550a7375SFelipe Balbi if (the_gadget) 1681550a7375SFelipe Balbi return -EBUSY; 1682550a7375SFelipe Balbi the_gadget = musb; 1683550a7375SFelipe Balbi 1684550a7375SFelipe Balbi musb->g.ops = &musb_gadget_operations; 1685550a7375SFelipe Balbi musb->g.is_dualspeed = 1; 1686550a7375SFelipe Balbi musb->g.speed = USB_SPEED_UNKNOWN; 1687550a7375SFelipe Balbi 1688550a7375SFelipe Balbi /* this "gadget" abstracts/virtualizes the controller */ 1689427c4f33SKay Sievers dev_set_name(&musb->g.dev, "gadget"); 1690550a7375SFelipe Balbi musb->g.dev.parent = musb->controller; 1691550a7375SFelipe Balbi musb->g.dev.dma_mask = musb->controller->dma_mask; 1692550a7375SFelipe Balbi musb->g.dev.release = musb_gadget_release; 1693550a7375SFelipe Balbi musb->g.name = musb_driver_name; 1694550a7375SFelipe Balbi 1695550a7375SFelipe Balbi if (is_otg_enabled(musb)) 1696550a7375SFelipe Balbi musb->g.is_otg = 1; 1697550a7375SFelipe Balbi 1698550a7375SFelipe Balbi musb_g_init_endpoints(musb); 1699550a7375SFelipe Balbi 1700550a7375SFelipe Balbi musb->is_active = 0; 1701550a7375SFelipe Balbi musb_platform_try_idle(musb, 0); 1702550a7375SFelipe Balbi 1703550a7375SFelipe Balbi status = device_register(&musb->g.dev); 1704550a7375SFelipe Balbi if (status != 0) 1705550a7375SFelipe Balbi the_gadget = NULL; 1706550a7375SFelipe Balbi return status; 1707550a7375SFelipe Balbi } 1708550a7375SFelipe Balbi 1709550a7375SFelipe Balbi void musb_gadget_cleanup(struct musb *musb) 1710550a7375SFelipe Balbi { 1711550a7375SFelipe Balbi if (musb != the_gadget) 1712550a7375SFelipe Balbi return; 1713550a7375SFelipe Balbi 1714550a7375SFelipe Balbi device_unregister(&musb->g.dev); 1715550a7375SFelipe Balbi the_gadget = NULL; 1716550a7375SFelipe Balbi } 1717550a7375SFelipe Balbi 1718550a7375SFelipe Balbi /* 1719550a7375SFelipe Balbi * Register the gadget driver. Used by gadget drivers when 1720550a7375SFelipe Balbi * registering themselves with the controller. 1721550a7375SFelipe Balbi * 1722550a7375SFelipe Balbi * -EINVAL something went wrong (not driver) 1723550a7375SFelipe Balbi * -EBUSY another gadget is already using the controller 1724550a7375SFelipe Balbi * -ENOMEM no memeory to perform the operation 1725550a7375SFelipe Balbi * 1726550a7375SFelipe Balbi * @param driver the gadget driver 1727b0fca50fSUwe Kleine-König * @param bind the driver's bind function 1728550a7375SFelipe Balbi * @return <0 if error, 0 if everything is fine 1729550a7375SFelipe Balbi */ 1730b0fca50fSUwe Kleine-König int usb_gadget_probe_driver(struct usb_gadget_driver *driver, 1731b0fca50fSUwe Kleine-König int (*bind)(struct usb_gadget *)) 1732550a7375SFelipe Balbi { 1733550a7375SFelipe Balbi int retval; 1734550a7375SFelipe Balbi unsigned long flags; 1735550a7375SFelipe Balbi struct musb *musb = the_gadget; 1736550a7375SFelipe Balbi 1737550a7375SFelipe Balbi if (!driver 1738550a7375SFelipe Balbi || driver->speed != USB_SPEED_HIGH 1739b0fca50fSUwe Kleine-König || !bind || !driver->setup) 1740550a7375SFelipe Balbi return -EINVAL; 1741550a7375SFelipe Balbi 1742550a7375SFelipe Balbi /* driver must be initialized to support peripheral mode */ 174308e6c972SRoel Kluin if (!musb) { 1744550a7375SFelipe Balbi DBG(1, "%s, no dev??\n", __func__); 1745550a7375SFelipe Balbi return -ENODEV; 1746550a7375SFelipe Balbi } 1747550a7375SFelipe Balbi 1748550a7375SFelipe Balbi DBG(3, "registering driver %s\n", driver->function); 1749550a7375SFelipe Balbi spin_lock_irqsave(&musb->lock, flags); 1750550a7375SFelipe Balbi 1751550a7375SFelipe Balbi if (musb->gadget_driver) { 1752550a7375SFelipe Balbi DBG(1, "%s is already bound to %s\n", 1753550a7375SFelipe Balbi musb_driver_name, 1754550a7375SFelipe Balbi musb->gadget_driver->driver.name); 1755550a7375SFelipe Balbi retval = -EBUSY; 1756550a7375SFelipe Balbi } else { 1757550a7375SFelipe Balbi musb->gadget_driver = driver; 1758550a7375SFelipe Balbi musb->g.dev.driver = &driver->driver; 1759550a7375SFelipe Balbi driver->driver.bus = NULL; 1760550a7375SFelipe Balbi musb->softconnect = 1; 1761550a7375SFelipe Balbi retval = 0; 1762550a7375SFelipe Balbi } 1763550a7375SFelipe Balbi 1764550a7375SFelipe Balbi spin_unlock_irqrestore(&musb->lock, flags); 1765550a7375SFelipe Balbi 1766f362a475SFelipe Balbi if (retval == 0) { 1767b0fca50fSUwe Kleine-König retval = bind(&musb->g); 1768550a7375SFelipe Balbi if (retval != 0) { 1769550a7375SFelipe Balbi DBG(3, "bind to driver %s failed --> %d\n", 1770550a7375SFelipe Balbi driver->driver.name, retval); 1771550a7375SFelipe Balbi musb->gadget_driver = NULL; 1772550a7375SFelipe Balbi musb->g.dev.driver = NULL; 1773550a7375SFelipe Balbi } 1774550a7375SFelipe Balbi 1775550a7375SFelipe Balbi spin_lock_irqsave(&musb->lock, flags); 1776550a7375SFelipe Balbi 177784e250ffSDavid Brownell otg_set_peripheral(musb->xceiv, &musb->g); 1778d4c433feSArnaud Mandy musb->xceiv->state = OTG_STATE_B_IDLE; 1779550a7375SFelipe Balbi musb->is_active = 1; 1780550a7375SFelipe Balbi 1781550a7375SFelipe Balbi /* FIXME this ignores the softconnect flag. Drivers are 1782550a7375SFelipe Balbi * allowed hold the peripheral inactive until for example 1783550a7375SFelipe Balbi * userspace hooks up printer hardware or DSP codecs, so 1784550a7375SFelipe Balbi * hosts only see fully functional devices. 1785550a7375SFelipe Balbi */ 1786550a7375SFelipe Balbi 1787550a7375SFelipe Balbi if (!is_otg_enabled(musb)) 1788550a7375SFelipe Balbi musb_start(musb); 1789550a7375SFelipe Balbi 179084e250ffSDavid Brownell otg_set_peripheral(musb->xceiv, &musb->g); 179184e250ffSDavid Brownell 1792550a7375SFelipe Balbi spin_unlock_irqrestore(&musb->lock, flags); 1793550a7375SFelipe Balbi 1794550a7375SFelipe Balbi if (is_otg_enabled(musb)) { 1795550a7375SFelipe Balbi DBG(3, "OTG startup...\n"); 1796550a7375SFelipe Balbi 1797550a7375SFelipe Balbi /* REVISIT: funcall to other code, which also 1798550a7375SFelipe Balbi * handles power budgeting ... this way also 1799550a7375SFelipe Balbi * ensures HdrcStart is indirectly called. 1800550a7375SFelipe Balbi */ 1801550a7375SFelipe Balbi retval = usb_add_hcd(musb_to_hcd(musb), -1, 0); 1802550a7375SFelipe Balbi if (retval < 0) { 1803550a7375SFelipe Balbi DBG(1, "add_hcd failed, %d\n", retval); 1804550a7375SFelipe Balbi spin_lock_irqsave(&musb->lock, flags); 180584e250ffSDavid Brownell otg_set_peripheral(musb->xceiv, NULL); 1806550a7375SFelipe Balbi musb->gadget_driver = NULL; 1807550a7375SFelipe Balbi musb->g.dev.driver = NULL; 1808550a7375SFelipe Balbi spin_unlock_irqrestore(&musb->lock, flags); 1809550a7375SFelipe Balbi } 1810550a7375SFelipe Balbi } 1811550a7375SFelipe Balbi } 1812550a7375SFelipe Balbi 1813550a7375SFelipe Balbi return retval; 1814550a7375SFelipe Balbi } 1815b0fca50fSUwe Kleine-König EXPORT_SYMBOL(usb_gadget_probe_driver); 1816550a7375SFelipe Balbi 1817550a7375SFelipe Balbi static void stop_activity(struct musb *musb, struct usb_gadget_driver *driver) 1818550a7375SFelipe Balbi { 1819550a7375SFelipe Balbi int i; 1820550a7375SFelipe Balbi struct musb_hw_ep *hw_ep; 1821550a7375SFelipe Balbi 1822550a7375SFelipe Balbi /* don't disconnect if it's not connected */ 1823550a7375SFelipe Balbi if (musb->g.speed == USB_SPEED_UNKNOWN) 1824550a7375SFelipe Balbi driver = NULL; 1825550a7375SFelipe Balbi else 1826550a7375SFelipe Balbi musb->g.speed = USB_SPEED_UNKNOWN; 1827550a7375SFelipe Balbi 1828550a7375SFelipe Balbi /* deactivate the hardware */ 1829550a7375SFelipe Balbi if (musb->softconnect) { 1830550a7375SFelipe Balbi musb->softconnect = 0; 1831550a7375SFelipe Balbi musb_pullup(musb, 0); 1832550a7375SFelipe Balbi } 1833550a7375SFelipe Balbi musb_stop(musb); 1834550a7375SFelipe Balbi 1835550a7375SFelipe Balbi /* killing any outstanding requests will quiesce the driver; 1836550a7375SFelipe Balbi * then report disconnect 1837550a7375SFelipe Balbi */ 1838550a7375SFelipe Balbi if (driver) { 1839550a7375SFelipe Balbi for (i = 0, hw_ep = musb->endpoints; 1840550a7375SFelipe Balbi i < musb->nr_endpoints; 1841550a7375SFelipe Balbi i++, hw_ep++) { 1842550a7375SFelipe Balbi musb_ep_select(musb->mregs, i); 1843550a7375SFelipe Balbi if (hw_ep->is_shared_fifo /* || !epnum */) { 1844550a7375SFelipe Balbi nuke(&hw_ep->ep_in, -ESHUTDOWN); 1845550a7375SFelipe Balbi } else { 1846550a7375SFelipe Balbi if (hw_ep->max_packet_sz_tx) 1847550a7375SFelipe Balbi nuke(&hw_ep->ep_in, -ESHUTDOWN); 1848550a7375SFelipe Balbi if (hw_ep->max_packet_sz_rx) 1849550a7375SFelipe Balbi nuke(&hw_ep->ep_out, -ESHUTDOWN); 1850550a7375SFelipe Balbi } 1851550a7375SFelipe Balbi } 1852550a7375SFelipe Balbi 1853550a7375SFelipe Balbi spin_unlock(&musb->lock); 1854550a7375SFelipe Balbi driver->disconnect(&musb->g); 1855550a7375SFelipe Balbi spin_lock(&musb->lock); 1856550a7375SFelipe Balbi } 1857550a7375SFelipe Balbi } 1858550a7375SFelipe Balbi 1859550a7375SFelipe Balbi /* 1860550a7375SFelipe Balbi * Unregister the gadget driver. Used by gadget drivers when 1861550a7375SFelipe Balbi * unregistering themselves from the controller. 1862550a7375SFelipe Balbi * 1863550a7375SFelipe Balbi * @param driver the gadget driver to unregister 1864550a7375SFelipe Balbi */ 1865550a7375SFelipe Balbi int usb_gadget_unregister_driver(struct usb_gadget_driver *driver) 1866550a7375SFelipe Balbi { 1867550a7375SFelipe Balbi unsigned long flags; 1868550a7375SFelipe Balbi int retval = 0; 1869550a7375SFelipe Balbi struct musb *musb = the_gadget; 1870550a7375SFelipe Balbi 1871550a7375SFelipe Balbi if (!driver || !driver->unbind || !musb) 1872550a7375SFelipe Balbi return -EINVAL; 1873550a7375SFelipe Balbi 1874550a7375SFelipe Balbi /* REVISIT always use otg_set_peripheral() here too; 1875550a7375SFelipe Balbi * this needs to shut down the OTG engine. 1876550a7375SFelipe Balbi */ 1877550a7375SFelipe Balbi 1878550a7375SFelipe Balbi spin_lock_irqsave(&musb->lock, flags); 1879550a7375SFelipe Balbi 1880550a7375SFelipe Balbi #ifdef CONFIG_USB_MUSB_OTG 1881550a7375SFelipe Balbi musb_hnp_stop(musb); 1882550a7375SFelipe Balbi #endif 1883550a7375SFelipe Balbi 1884550a7375SFelipe Balbi if (musb->gadget_driver == driver) { 1885550a7375SFelipe Balbi 1886550a7375SFelipe Balbi (void) musb_gadget_vbus_draw(&musb->g, 0); 1887550a7375SFelipe Balbi 188884e250ffSDavid Brownell musb->xceiv->state = OTG_STATE_UNDEFINED; 1889550a7375SFelipe Balbi stop_activity(musb, driver); 189084e250ffSDavid Brownell otg_set_peripheral(musb->xceiv, NULL); 1891550a7375SFelipe Balbi 1892550a7375SFelipe Balbi DBG(3, "unregistering driver %s\n", driver->function); 1893550a7375SFelipe Balbi spin_unlock_irqrestore(&musb->lock, flags); 1894550a7375SFelipe Balbi driver->unbind(&musb->g); 1895550a7375SFelipe Balbi spin_lock_irqsave(&musb->lock, flags); 1896550a7375SFelipe Balbi 1897550a7375SFelipe Balbi musb->gadget_driver = NULL; 1898550a7375SFelipe Balbi musb->g.dev.driver = NULL; 1899550a7375SFelipe Balbi 1900550a7375SFelipe Balbi musb->is_active = 0; 1901550a7375SFelipe Balbi musb_platform_try_idle(musb, 0); 1902550a7375SFelipe Balbi } else 1903550a7375SFelipe Balbi retval = -EINVAL; 1904550a7375SFelipe Balbi spin_unlock_irqrestore(&musb->lock, flags); 1905550a7375SFelipe Balbi 1906550a7375SFelipe Balbi if (is_otg_enabled(musb) && retval == 0) { 1907550a7375SFelipe Balbi usb_remove_hcd(musb_to_hcd(musb)); 1908550a7375SFelipe Balbi /* FIXME we need to be able to register another 1909550a7375SFelipe Balbi * gadget driver here and have everything work; 1910550a7375SFelipe Balbi * that currently misbehaves. 1911550a7375SFelipe Balbi */ 1912550a7375SFelipe Balbi } 1913550a7375SFelipe Balbi 1914550a7375SFelipe Balbi return retval; 1915550a7375SFelipe Balbi } 1916550a7375SFelipe Balbi EXPORT_SYMBOL(usb_gadget_unregister_driver); 1917550a7375SFelipe Balbi 1918550a7375SFelipe Balbi 1919550a7375SFelipe Balbi /* ----------------------------------------------------------------------- */ 1920550a7375SFelipe Balbi 1921550a7375SFelipe Balbi /* lifecycle operations called through plat_uds.c */ 1922550a7375SFelipe Balbi 1923550a7375SFelipe Balbi void musb_g_resume(struct musb *musb) 1924550a7375SFelipe Balbi { 1925550a7375SFelipe Balbi musb->is_suspended = 0; 192684e250ffSDavid Brownell switch (musb->xceiv->state) { 1927550a7375SFelipe Balbi case OTG_STATE_B_IDLE: 1928550a7375SFelipe Balbi break; 1929550a7375SFelipe Balbi case OTG_STATE_B_WAIT_ACON: 1930550a7375SFelipe Balbi case OTG_STATE_B_PERIPHERAL: 1931550a7375SFelipe Balbi musb->is_active = 1; 1932550a7375SFelipe Balbi if (musb->gadget_driver && musb->gadget_driver->resume) { 1933550a7375SFelipe Balbi spin_unlock(&musb->lock); 1934550a7375SFelipe Balbi musb->gadget_driver->resume(&musb->g); 1935550a7375SFelipe Balbi spin_lock(&musb->lock); 1936550a7375SFelipe Balbi } 1937550a7375SFelipe Balbi break; 1938550a7375SFelipe Balbi default: 1939550a7375SFelipe Balbi WARNING("unhandled RESUME transition (%s)\n", 1940550a7375SFelipe Balbi otg_state_string(musb)); 1941550a7375SFelipe Balbi } 1942550a7375SFelipe Balbi } 1943550a7375SFelipe Balbi 1944550a7375SFelipe Balbi /* called when SOF packets stop for 3+ msec */ 1945550a7375SFelipe Balbi void musb_g_suspend(struct musb *musb) 1946550a7375SFelipe Balbi { 1947550a7375SFelipe Balbi u8 devctl; 1948550a7375SFelipe Balbi 1949550a7375SFelipe Balbi devctl = musb_readb(musb->mregs, MUSB_DEVCTL); 1950550a7375SFelipe Balbi DBG(3, "devctl %02x\n", devctl); 1951550a7375SFelipe Balbi 195284e250ffSDavid Brownell switch (musb->xceiv->state) { 1953550a7375SFelipe Balbi case OTG_STATE_B_IDLE: 1954550a7375SFelipe Balbi if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS) 195584e250ffSDavid Brownell musb->xceiv->state = OTG_STATE_B_PERIPHERAL; 1956550a7375SFelipe Balbi break; 1957550a7375SFelipe Balbi case OTG_STATE_B_PERIPHERAL: 1958550a7375SFelipe Balbi musb->is_suspended = 1; 1959550a7375SFelipe Balbi if (musb->gadget_driver && musb->gadget_driver->suspend) { 1960550a7375SFelipe Balbi spin_unlock(&musb->lock); 1961550a7375SFelipe Balbi musb->gadget_driver->suspend(&musb->g); 1962550a7375SFelipe Balbi spin_lock(&musb->lock); 1963550a7375SFelipe Balbi } 1964550a7375SFelipe Balbi break; 1965550a7375SFelipe Balbi default: 1966550a7375SFelipe Balbi /* REVISIT if B_HOST, clear DEVCTL.HOSTREQ; 1967550a7375SFelipe Balbi * A_PERIPHERAL may need care too 1968550a7375SFelipe Balbi */ 1969550a7375SFelipe Balbi WARNING("unhandled SUSPEND transition (%s)\n", 1970550a7375SFelipe Balbi otg_state_string(musb)); 1971550a7375SFelipe Balbi } 1972550a7375SFelipe Balbi } 1973550a7375SFelipe Balbi 1974550a7375SFelipe Balbi /* Called during SRP */ 1975550a7375SFelipe Balbi void musb_g_wakeup(struct musb *musb) 1976550a7375SFelipe Balbi { 1977550a7375SFelipe Balbi musb_gadget_wakeup(&musb->g); 1978550a7375SFelipe Balbi } 1979550a7375SFelipe Balbi 1980550a7375SFelipe Balbi /* called when VBUS drops below session threshold, and in other cases */ 1981550a7375SFelipe Balbi void musb_g_disconnect(struct musb *musb) 1982550a7375SFelipe Balbi { 1983550a7375SFelipe Balbi void __iomem *mregs = musb->mregs; 1984550a7375SFelipe Balbi u8 devctl = musb_readb(mregs, MUSB_DEVCTL); 1985550a7375SFelipe Balbi 1986550a7375SFelipe Balbi DBG(3, "devctl %02x\n", devctl); 1987550a7375SFelipe Balbi 1988550a7375SFelipe Balbi /* clear HR */ 1989550a7375SFelipe Balbi musb_writeb(mregs, MUSB_DEVCTL, devctl & MUSB_DEVCTL_SESSION); 1990550a7375SFelipe Balbi 1991550a7375SFelipe Balbi /* don't draw vbus until new b-default session */ 1992550a7375SFelipe Balbi (void) musb_gadget_vbus_draw(&musb->g, 0); 1993550a7375SFelipe Balbi 1994550a7375SFelipe Balbi musb->g.speed = USB_SPEED_UNKNOWN; 1995550a7375SFelipe Balbi if (musb->gadget_driver && musb->gadget_driver->disconnect) { 1996550a7375SFelipe Balbi spin_unlock(&musb->lock); 1997550a7375SFelipe Balbi musb->gadget_driver->disconnect(&musb->g); 1998550a7375SFelipe Balbi spin_lock(&musb->lock); 1999550a7375SFelipe Balbi } 2000550a7375SFelipe Balbi 200184e250ffSDavid Brownell switch (musb->xceiv->state) { 2002550a7375SFelipe Balbi default: 2003550a7375SFelipe Balbi #ifdef CONFIG_USB_MUSB_OTG 2004550a7375SFelipe Balbi DBG(2, "Unhandled disconnect %s, setting a_idle\n", 2005550a7375SFelipe Balbi otg_state_string(musb)); 200684e250ffSDavid Brownell musb->xceiv->state = OTG_STATE_A_IDLE; 2007ab983f2aSDavid Brownell MUSB_HST_MODE(musb); 2008550a7375SFelipe Balbi break; 2009550a7375SFelipe Balbi case OTG_STATE_A_PERIPHERAL: 20101de00daeSDavid Brownell musb->xceiv->state = OTG_STATE_A_WAIT_BCON; 2011ab983f2aSDavid Brownell MUSB_HST_MODE(musb); 2012550a7375SFelipe Balbi break; 2013550a7375SFelipe Balbi case OTG_STATE_B_WAIT_ACON: 2014550a7375SFelipe Balbi case OTG_STATE_B_HOST: 2015550a7375SFelipe Balbi #endif 2016550a7375SFelipe Balbi case OTG_STATE_B_PERIPHERAL: 2017550a7375SFelipe Balbi case OTG_STATE_B_IDLE: 201884e250ffSDavid Brownell musb->xceiv->state = OTG_STATE_B_IDLE; 2019550a7375SFelipe Balbi break; 2020550a7375SFelipe Balbi case OTG_STATE_B_SRP_INIT: 2021550a7375SFelipe Balbi break; 2022550a7375SFelipe Balbi } 2023550a7375SFelipe Balbi 2024550a7375SFelipe Balbi musb->is_active = 0; 2025550a7375SFelipe Balbi } 2026550a7375SFelipe Balbi 2027550a7375SFelipe Balbi void musb_g_reset(struct musb *musb) 2028550a7375SFelipe Balbi __releases(musb->lock) 2029550a7375SFelipe Balbi __acquires(musb->lock) 2030550a7375SFelipe Balbi { 2031550a7375SFelipe Balbi void __iomem *mbase = musb->mregs; 2032550a7375SFelipe Balbi u8 devctl = musb_readb(mbase, MUSB_DEVCTL); 2033550a7375SFelipe Balbi u8 power; 2034550a7375SFelipe Balbi 2035550a7375SFelipe Balbi DBG(3, "<== %s addr=%x driver '%s'\n", 2036550a7375SFelipe Balbi (devctl & MUSB_DEVCTL_BDEVICE) 2037550a7375SFelipe Balbi ? "B-Device" : "A-Device", 2038550a7375SFelipe Balbi musb_readb(mbase, MUSB_FADDR), 2039550a7375SFelipe Balbi musb->gadget_driver 2040550a7375SFelipe Balbi ? musb->gadget_driver->driver.name 2041550a7375SFelipe Balbi : NULL 2042550a7375SFelipe Balbi ); 2043550a7375SFelipe Balbi 2044550a7375SFelipe Balbi /* report disconnect, if we didn't already (flushing EP state) */ 2045550a7375SFelipe Balbi if (musb->g.speed != USB_SPEED_UNKNOWN) 2046550a7375SFelipe Balbi musb_g_disconnect(musb); 2047550a7375SFelipe Balbi 2048550a7375SFelipe Balbi /* clear HR */ 2049550a7375SFelipe Balbi else if (devctl & MUSB_DEVCTL_HR) 2050550a7375SFelipe Balbi musb_writeb(mbase, MUSB_DEVCTL, MUSB_DEVCTL_SESSION); 2051550a7375SFelipe Balbi 2052550a7375SFelipe Balbi 2053550a7375SFelipe Balbi /* what speed did we negotiate? */ 2054550a7375SFelipe Balbi power = musb_readb(mbase, MUSB_POWER); 2055550a7375SFelipe Balbi musb->g.speed = (power & MUSB_POWER_HSMODE) 2056550a7375SFelipe Balbi ? USB_SPEED_HIGH : USB_SPEED_FULL; 2057550a7375SFelipe Balbi 2058550a7375SFelipe Balbi /* start in USB_STATE_DEFAULT */ 2059550a7375SFelipe Balbi musb->is_active = 1; 2060550a7375SFelipe Balbi musb->is_suspended = 0; 2061550a7375SFelipe Balbi MUSB_DEV_MODE(musb); 2062550a7375SFelipe Balbi musb->address = 0; 2063550a7375SFelipe Balbi musb->ep0_state = MUSB_EP0_STAGE_SETUP; 2064550a7375SFelipe Balbi 2065550a7375SFelipe Balbi musb->may_wakeup = 0; 2066550a7375SFelipe Balbi musb->g.b_hnp_enable = 0; 2067550a7375SFelipe Balbi musb->g.a_alt_hnp_support = 0; 2068550a7375SFelipe Balbi musb->g.a_hnp_support = 0; 2069550a7375SFelipe Balbi 2070550a7375SFelipe Balbi /* Normal reset, as B-Device; 2071550a7375SFelipe Balbi * or else after HNP, as A-Device 2072550a7375SFelipe Balbi */ 2073550a7375SFelipe Balbi if (devctl & MUSB_DEVCTL_BDEVICE) { 207484e250ffSDavid Brownell musb->xceiv->state = OTG_STATE_B_PERIPHERAL; 2075550a7375SFelipe Balbi musb->g.is_a_peripheral = 0; 2076550a7375SFelipe Balbi } else if (is_otg_enabled(musb)) { 207784e250ffSDavid Brownell musb->xceiv->state = OTG_STATE_A_PERIPHERAL; 2078550a7375SFelipe Balbi musb->g.is_a_peripheral = 1; 2079550a7375SFelipe Balbi } else 2080550a7375SFelipe Balbi WARN_ON(1); 2081550a7375SFelipe Balbi 2082550a7375SFelipe Balbi /* start with default limits on VBUS power draw */ 2083550a7375SFelipe Balbi (void) musb_gadget_vbus_draw(&musb->g, 2084550a7375SFelipe Balbi is_otg_enabled(musb) ? 8 : 100); 2085550a7375SFelipe Balbi } 2086