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