1550a7375SFelipe Balbi /* 2550a7375SFelipe Balbi * MUSB OTG driver peripheral support 3550a7375SFelipe Balbi * 4550a7375SFelipe Balbi * Copyright 2005 Mentor Graphics Corporation 5550a7375SFelipe Balbi * Copyright (C) 2005-2006 by Texas Instruments 6550a7375SFelipe Balbi * Copyright (C) 2006-2007 Nokia Corporation 7cea83241SSergei Shtylyov * Copyright (C) 2009 MontaVista Software, Inc. <source@mvista.com> 8550a7375SFelipe Balbi * 9550a7375SFelipe Balbi * This program is free software; you can redistribute it and/or 10550a7375SFelipe Balbi * modify it under the terms of the GNU General Public License 11550a7375SFelipe Balbi * version 2 as published by the Free Software Foundation. 12550a7375SFelipe Balbi * 13550a7375SFelipe Balbi * This program is distributed in the hope that it will be useful, but 14550a7375SFelipe Balbi * WITHOUT ANY WARRANTY; without even the implied warranty of 15550a7375SFelipe Balbi * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16550a7375SFelipe Balbi * General Public License for more details. 17550a7375SFelipe Balbi * 18550a7375SFelipe Balbi * You should have received a copy of the GNU General Public License 19550a7375SFelipe Balbi * along with this program; if not, write to the Free Software 20550a7375SFelipe Balbi * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 21550a7375SFelipe Balbi * 02110-1301 USA 22550a7375SFelipe Balbi * 23550a7375SFelipe Balbi * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED 24550a7375SFelipe Balbi * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 25550a7375SFelipe Balbi * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 26550a7375SFelipe Balbi * NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, 27550a7375SFelipe Balbi * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 28550a7375SFelipe Balbi * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 29550a7375SFelipe Balbi * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 30550a7375SFelipe Balbi * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31550a7375SFelipe Balbi * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 32550a7375SFelipe Balbi * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33550a7375SFelipe Balbi * 34550a7375SFelipe Balbi */ 35550a7375SFelipe Balbi 36550a7375SFelipe Balbi #include <linux/kernel.h> 37550a7375SFelipe Balbi #include <linux/list.h> 38550a7375SFelipe Balbi #include <linux/timer.h> 39550a7375SFelipe Balbi #include <linux/module.h> 40550a7375SFelipe Balbi #include <linux/smp.h> 41550a7375SFelipe Balbi #include <linux/spinlock.h> 42550a7375SFelipe Balbi #include <linux/delay.h> 43550a7375SFelipe Balbi #include <linux/moduleparam.h> 44550a7375SFelipe Balbi #include <linux/stat.h> 45550a7375SFelipe Balbi #include <linux/dma-mapping.h> 465a0e3ad6STejun Heo #include <linux/slab.h> 47550a7375SFelipe Balbi 48550a7375SFelipe Balbi #include "musb_core.h" 49550a7375SFelipe Balbi 50550a7375SFelipe Balbi 51550a7375SFelipe Balbi /* MUSB PERIPHERAL status 3-mar-2006: 52550a7375SFelipe Balbi * 53550a7375SFelipe Balbi * - EP0 seems solid. It passes both USBCV and usbtest control cases. 54550a7375SFelipe Balbi * Minor glitches: 55550a7375SFelipe Balbi * 56550a7375SFelipe Balbi * + remote wakeup to Linux hosts work, but saw USBCV failures; 57550a7375SFelipe Balbi * in one test run (operator error?) 58550a7375SFelipe Balbi * + endpoint halt tests -- in both usbtest and usbcv -- seem 59550a7375SFelipe Balbi * to break when dma is enabled ... is something wrongly 60550a7375SFelipe Balbi * clearing SENDSTALL? 61550a7375SFelipe Balbi * 62550a7375SFelipe Balbi * - Mass storage behaved ok when last tested. Network traffic patterns 63550a7375SFelipe Balbi * (with lots of short transfers etc) need retesting; they turn up the 64550a7375SFelipe Balbi * worst cases of the DMA, since short packets are typical but are not 65550a7375SFelipe Balbi * required. 66550a7375SFelipe Balbi * 67550a7375SFelipe Balbi * - TX/IN 68550a7375SFelipe Balbi * + both pio and dma behave in with network and g_zero tests 69550a7375SFelipe Balbi * + no cppi throughput issues other than no-hw-queueing 70550a7375SFelipe Balbi * + failed with FLAT_REG (DaVinci) 71550a7375SFelipe Balbi * + seems to behave with double buffering, PIO -and- CPPI 72550a7375SFelipe Balbi * + with gadgetfs + AIO, requests got lost? 73550a7375SFelipe Balbi * 74550a7375SFelipe Balbi * - RX/OUT 75550a7375SFelipe Balbi * + both pio and dma behave in with network and g_zero tests 76550a7375SFelipe Balbi * + dma is slow in typical case (short_not_ok is clear) 77550a7375SFelipe Balbi * + double buffering ok with PIO 78550a7375SFelipe Balbi * + double buffering *FAILS* with CPPI, wrong data bytes sometimes 79550a7375SFelipe Balbi * + request lossage observed with gadgetfs 80550a7375SFelipe Balbi * 81550a7375SFelipe Balbi * - ISO not tested ... might work, but only weakly isochronous 82550a7375SFelipe Balbi * 83550a7375SFelipe Balbi * - Gadget driver disabling of softconnect during bind() is ignored; so 84550a7375SFelipe Balbi * drivers can't hold off host requests until userspace is ready. 85550a7375SFelipe Balbi * (Workaround: they can turn it off later.) 86550a7375SFelipe Balbi * 87550a7375SFelipe Balbi * - PORTABILITY (assumes PIO works): 88550a7375SFelipe Balbi * + DaVinci, basically works with cppi dma 89550a7375SFelipe Balbi * + OMAP 2430, ditto with mentor dma 90550a7375SFelipe Balbi * + TUSB 6010, platform-specific dma in the works 91550a7375SFelipe Balbi */ 92550a7375SFelipe Balbi 93550a7375SFelipe Balbi /* ----------------------------------------------------------------------- */ 94550a7375SFelipe Balbi 95c65bfa62SMian Yousaf Kaukab #define is_buffer_mapped(req) (is_dma_capable() && \ 96c65bfa62SMian Yousaf Kaukab (req->map_state != UN_MAPPED)) 97c65bfa62SMian Yousaf Kaukab 9892d2711fSHema Kalliguddi /* Maps the buffer to dma */ 9992d2711fSHema Kalliguddi 10092d2711fSHema Kalliguddi static inline void map_dma_buffer(struct musb_request *request, 101c65bfa62SMian Yousaf Kaukab struct musb *musb, struct musb_ep *musb_ep) 10292d2711fSHema Kalliguddi { 1035f5761cbSMian Yousaf Kaukab int compatible = true; 1045f5761cbSMian Yousaf Kaukab struct dma_controller *dma = musb->dma_controller; 1055f5761cbSMian Yousaf Kaukab 106c65bfa62SMian Yousaf Kaukab request->map_state = UN_MAPPED; 107c65bfa62SMian Yousaf Kaukab 108c65bfa62SMian Yousaf Kaukab if (!is_dma_capable() || !musb_ep->dma) 109c65bfa62SMian Yousaf Kaukab return; 110c65bfa62SMian Yousaf Kaukab 1115f5761cbSMian Yousaf Kaukab /* Check if DMA engine can handle this request. 1125f5761cbSMian Yousaf Kaukab * DMA code must reject the USB request explicitly. 1135f5761cbSMian Yousaf Kaukab * Default behaviour is to map the request. 1145f5761cbSMian Yousaf Kaukab */ 1155f5761cbSMian Yousaf Kaukab if (dma->is_compatible) 1165f5761cbSMian Yousaf Kaukab compatible = dma->is_compatible(musb_ep->dma, 1175f5761cbSMian Yousaf Kaukab musb_ep->packet_sz, request->request.buf, 1185f5761cbSMian Yousaf Kaukab request->request.length); 1195f5761cbSMian Yousaf Kaukab if (!compatible) 1205f5761cbSMian Yousaf Kaukab return; 1215f5761cbSMian Yousaf Kaukab 12292d2711fSHema Kalliguddi if (request->request.dma == DMA_ADDR_INVALID) { 12392d2711fSHema Kalliguddi request->request.dma = dma_map_single( 12492d2711fSHema Kalliguddi musb->controller, 12592d2711fSHema Kalliguddi request->request.buf, 12692d2711fSHema Kalliguddi request->request.length, 12792d2711fSHema Kalliguddi request->tx 12892d2711fSHema Kalliguddi ? DMA_TO_DEVICE 12992d2711fSHema Kalliguddi : DMA_FROM_DEVICE); 130c65bfa62SMian Yousaf Kaukab request->map_state = MUSB_MAPPED; 13192d2711fSHema Kalliguddi } else { 13292d2711fSHema Kalliguddi dma_sync_single_for_device(musb->controller, 13392d2711fSHema Kalliguddi request->request.dma, 13492d2711fSHema Kalliguddi request->request.length, 13592d2711fSHema Kalliguddi request->tx 13692d2711fSHema Kalliguddi ? DMA_TO_DEVICE 13792d2711fSHema Kalliguddi : DMA_FROM_DEVICE); 138c65bfa62SMian Yousaf Kaukab request->map_state = PRE_MAPPED; 13992d2711fSHema Kalliguddi } 14092d2711fSHema Kalliguddi } 14192d2711fSHema Kalliguddi 14292d2711fSHema Kalliguddi /* Unmap the buffer from dma and maps it back to cpu */ 14392d2711fSHema Kalliguddi static inline void unmap_dma_buffer(struct musb_request *request, 14492d2711fSHema Kalliguddi struct musb *musb) 14592d2711fSHema Kalliguddi { 146c65bfa62SMian Yousaf Kaukab if (!is_buffer_mapped(request)) 147c65bfa62SMian Yousaf Kaukab return; 148c65bfa62SMian Yousaf Kaukab 14992d2711fSHema Kalliguddi if (request->request.dma == DMA_ADDR_INVALID) { 1505c8a86e1SFelipe Balbi dev_vdbg(musb->controller, 1515c8a86e1SFelipe Balbi "not unmapping a never mapped buffer\n"); 15292d2711fSHema Kalliguddi return; 15392d2711fSHema Kalliguddi } 154c65bfa62SMian Yousaf Kaukab if (request->map_state == MUSB_MAPPED) { 15592d2711fSHema Kalliguddi dma_unmap_single(musb->controller, 15692d2711fSHema Kalliguddi request->request.dma, 15792d2711fSHema Kalliguddi request->request.length, 15892d2711fSHema Kalliguddi request->tx 15992d2711fSHema Kalliguddi ? DMA_TO_DEVICE 16092d2711fSHema Kalliguddi : DMA_FROM_DEVICE); 16192d2711fSHema Kalliguddi request->request.dma = DMA_ADDR_INVALID; 162c65bfa62SMian Yousaf Kaukab } else { /* PRE_MAPPED */ 16392d2711fSHema Kalliguddi dma_sync_single_for_cpu(musb->controller, 16492d2711fSHema Kalliguddi request->request.dma, 16592d2711fSHema Kalliguddi request->request.length, 16692d2711fSHema Kalliguddi request->tx 16792d2711fSHema Kalliguddi ? DMA_TO_DEVICE 16892d2711fSHema Kalliguddi : DMA_FROM_DEVICE); 16992d2711fSHema Kalliguddi } 170c65bfa62SMian Yousaf Kaukab request->map_state = UN_MAPPED; 17192d2711fSHema Kalliguddi } 17292d2711fSHema Kalliguddi 173550a7375SFelipe Balbi /* 174550a7375SFelipe Balbi * Immediately complete a request. 175550a7375SFelipe Balbi * 176550a7375SFelipe Balbi * @param request the request to complete 177550a7375SFelipe Balbi * @param status the status to complete the request with 178550a7375SFelipe Balbi * Context: controller locked, IRQs blocked. 179550a7375SFelipe Balbi */ 180550a7375SFelipe Balbi void musb_g_giveback( 181550a7375SFelipe Balbi struct musb_ep *ep, 182550a7375SFelipe Balbi struct usb_request *request, 183550a7375SFelipe Balbi int status) 184550a7375SFelipe Balbi __releases(ep->musb->lock) 185550a7375SFelipe Balbi __acquires(ep->musb->lock) 186550a7375SFelipe Balbi { 187550a7375SFelipe Balbi struct musb_request *req; 188550a7375SFelipe Balbi struct musb *musb; 189550a7375SFelipe Balbi int busy = ep->busy; 190550a7375SFelipe Balbi 191550a7375SFelipe Balbi req = to_musb_request(request); 192550a7375SFelipe Balbi 193ad1adb89SFelipe Balbi list_del(&req->list); 194550a7375SFelipe Balbi if (req->request.status == -EINPROGRESS) 195550a7375SFelipe Balbi req->request.status = status; 196550a7375SFelipe Balbi musb = req->musb; 197550a7375SFelipe Balbi 198550a7375SFelipe Balbi ep->busy = 1; 199550a7375SFelipe Balbi spin_unlock(&musb->lock); 20092d2711fSHema Kalliguddi unmap_dma_buffer(req, musb); 201550a7375SFelipe Balbi if (request->status == 0) 2025c8a86e1SFelipe Balbi dev_dbg(musb->controller, "%s done request %p, %d/%d\n", 203550a7375SFelipe Balbi ep->end_point.name, request, 204550a7375SFelipe Balbi req->request.actual, req->request.length); 205550a7375SFelipe Balbi else 2065c8a86e1SFelipe Balbi dev_dbg(musb->controller, "%s request %p, %d/%d fault %d\n", 207550a7375SFelipe Balbi ep->end_point.name, request, 208550a7375SFelipe Balbi req->request.actual, req->request.length, 209550a7375SFelipe Balbi request->status); 210550a7375SFelipe Balbi req->request.complete(&req->ep->end_point, &req->request); 211550a7375SFelipe Balbi spin_lock(&musb->lock); 212550a7375SFelipe Balbi ep->busy = busy; 213550a7375SFelipe Balbi } 214550a7375SFelipe Balbi 215550a7375SFelipe Balbi /* ----------------------------------------------------------------------- */ 216550a7375SFelipe Balbi 217550a7375SFelipe Balbi /* 218550a7375SFelipe Balbi * Abort requests queued to an endpoint using the status. Synchronous. 219550a7375SFelipe Balbi * caller locked controller and blocked irqs, and selected this ep. 220550a7375SFelipe Balbi */ 221550a7375SFelipe Balbi static void nuke(struct musb_ep *ep, const int status) 222550a7375SFelipe Balbi { 2235c8a86e1SFelipe Balbi struct musb *musb = ep->musb; 224550a7375SFelipe Balbi struct musb_request *req = NULL; 225550a7375SFelipe Balbi void __iomem *epio = ep->musb->endpoints[ep->current_epnum].regs; 226550a7375SFelipe Balbi 227550a7375SFelipe Balbi ep->busy = 1; 228550a7375SFelipe Balbi 229550a7375SFelipe Balbi if (is_dma_capable() && ep->dma) { 230550a7375SFelipe Balbi struct dma_controller *c = ep->musb->dma_controller; 231550a7375SFelipe Balbi int value; 232b6e434a5SSergei Shtylyov 233550a7375SFelipe Balbi if (ep->is_in) { 234b6e434a5SSergei Shtylyov /* 235b6e434a5SSergei Shtylyov * The programming guide says that we must not clear 236b6e434a5SSergei Shtylyov * the DMAMODE bit before DMAENAB, so we only 237b6e434a5SSergei Shtylyov * clear it in the second write... 238b6e434a5SSergei Shtylyov */ 239550a7375SFelipe Balbi musb_writew(epio, MUSB_TXCSR, 240b6e434a5SSergei Shtylyov MUSB_TXCSR_DMAMODE | MUSB_TXCSR_FLUSHFIFO); 241550a7375SFelipe Balbi musb_writew(epio, MUSB_TXCSR, 242550a7375SFelipe Balbi 0 | MUSB_TXCSR_FLUSHFIFO); 243550a7375SFelipe Balbi } else { 244550a7375SFelipe Balbi musb_writew(epio, MUSB_RXCSR, 245550a7375SFelipe Balbi 0 | MUSB_RXCSR_FLUSHFIFO); 246550a7375SFelipe Balbi musb_writew(epio, MUSB_RXCSR, 247550a7375SFelipe Balbi 0 | MUSB_RXCSR_FLUSHFIFO); 248550a7375SFelipe Balbi } 249550a7375SFelipe Balbi 250550a7375SFelipe Balbi value = c->channel_abort(ep->dma); 2515c8a86e1SFelipe Balbi dev_dbg(musb->controller, "%s: abort DMA --> %d\n", 2525c8a86e1SFelipe Balbi ep->name, value); 253550a7375SFelipe Balbi c->channel_release(ep->dma); 254550a7375SFelipe Balbi ep->dma = NULL; 255550a7375SFelipe Balbi } 256550a7375SFelipe Balbi 257ad1adb89SFelipe Balbi while (!list_empty(&ep->req_list)) { 258ad1adb89SFelipe Balbi req = list_first_entry(&ep->req_list, struct musb_request, list); 259550a7375SFelipe Balbi musb_g_giveback(ep, &req->request, status); 260550a7375SFelipe Balbi } 261550a7375SFelipe Balbi } 262550a7375SFelipe Balbi 263550a7375SFelipe Balbi /* ----------------------------------------------------------------------- */ 264550a7375SFelipe Balbi 265550a7375SFelipe Balbi /* Data transfers - pure PIO, pure DMA, or mixed mode */ 266550a7375SFelipe Balbi 267550a7375SFelipe Balbi /* 268550a7375SFelipe Balbi * This assumes the separate CPPI engine is responding to DMA requests 269550a7375SFelipe Balbi * from the usb core ... sequenced a bit differently from mentor dma. 270550a7375SFelipe Balbi */ 271550a7375SFelipe Balbi 272550a7375SFelipe Balbi static inline int max_ep_writesize(struct musb *musb, struct musb_ep *ep) 273550a7375SFelipe Balbi { 274550a7375SFelipe Balbi if (can_bulk_split(musb, ep->type)) 275550a7375SFelipe Balbi return ep->hw_ep->max_packet_sz_tx; 276550a7375SFelipe Balbi else 277550a7375SFelipe Balbi return ep->packet_sz; 278550a7375SFelipe Balbi } 279550a7375SFelipe Balbi 280550a7375SFelipe Balbi 281550a7375SFelipe Balbi #ifdef CONFIG_USB_INVENTRA_DMA 282550a7375SFelipe Balbi 283550a7375SFelipe Balbi /* Peripheral tx (IN) using Mentor DMA works as follows: 284550a7375SFelipe Balbi Only mode 0 is used for transfers <= wPktSize, 285550a7375SFelipe Balbi mode 1 is used for larger transfers, 286550a7375SFelipe Balbi 287550a7375SFelipe Balbi One of the following happens: 288550a7375SFelipe Balbi - Host sends IN token which causes an endpoint interrupt 289550a7375SFelipe Balbi -> TxAvail 290550a7375SFelipe Balbi -> if DMA is currently busy, exit. 291550a7375SFelipe Balbi -> if queue is non-empty, txstate(). 292550a7375SFelipe Balbi 293550a7375SFelipe Balbi - Request is queued by the gadget driver. 294550a7375SFelipe Balbi -> if queue was previously empty, txstate() 295550a7375SFelipe Balbi 296550a7375SFelipe Balbi txstate() 297550a7375SFelipe Balbi -> start 298550a7375SFelipe Balbi /\ -> setup DMA 299550a7375SFelipe Balbi | (data is transferred to the FIFO, then sent out when 300550a7375SFelipe Balbi | IN token(s) are recd from Host. 301550a7375SFelipe Balbi | -> DMA interrupt on completion 302550a7375SFelipe Balbi | calls TxAvail. 303b6e434a5SSergei Shtylyov | -> stop DMA, ~DMAENAB, 304550a7375SFelipe Balbi | -> set TxPktRdy for last short pkt or zlp 305550a7375SFelipe Balbi | -> Complete Request 306550a7375SFelipe Balbi | -> Continue next request (call txstate) 307550a7375SFelipe Balbi |___________________________________| 308550a7375SFelipe Balbi 309550a7375SFelipe Balbi * Non-Mentor DMA engines can of course work differently, such as by 310550a7375SFelipe Balbi * upleveling from irq-per-packet to irq-per-buffer. 311550a7375SFelipe Balbi */ 312550a7375SFelipe Balbi 313550a7375SFelipe Balbi #endif 314550a7375SFelipe Balbi 315550a7375SFelipe Balbi /* 316550a7375SFelipe Balbi * An endpoint is transmitting data. This can be called either from 317550a7375SFelipe Balbi * the IRQ routine or from ep.queue() to kickstart a request on an 318550a7375SFelipe Balbi * endpoint. 319550a7375SFelipe Balbi * 320550a7375SFelipe Balbi * Context: controller locked, IRQs blocked, endpoint selected 321550a7375SFelipe Balbi */ 322550a7375SFelipe Balbi static void txstate(struct musb *musb, struct musb_request *req) 323550a7375SFelipe Balbi { 324550a7375SFelipe Balbi u8 epnum = req->epnum; 325550a7375SFelipe Balbi struct musb_ep *musb_ep; 326550a7375SFelipe Balbi void __iomem *epio = musb->endpoints[epnum].regs; 327550a7375SFelipe Balbi struct usb_request *request; 328550a7375SFelipe Balbi u16 fifo_count = 0, csr; 329550a7375SFelipe Balbi int use_dma = 0; 330550a7375SFelipe Balbi 331550a7375SFelipe Balbi musb_ep = req->ep; 332550a7375SFelipe Balbi 333550a7375SFelipe Balbi /* we shouldn't get here while DMA is active ... but we do ... */ 334550a7375SFelipe Balbi if (dma_channel_status(musb_ep->dma) == MUSB_DMA_STATUS_BUSY) { 3355c8a86e1SFelipe Balbi dev_dbg(musb->controller, "dma pending...\n"); 336550a7375SFelipe Balbi return; 337550a7375SFelipe Balbi } 338550a7375SFelipe Balbi 339550a7375SFelipe Balbi /* read TXCSR before */ 340550a7375SFelipe Balbi csr = musb_readw(epio, MUSB_TXCSR); 341550a7375SFelipe Balbi 342550a7375SFelipe Balbi request = &req->request; 343550a7375SFelipe Balbi fifo_count = min(max_ep_writesize(musb, musb_ep), 344550a7375SFelipe Balbi (int)(request->length - request->actual)); 345550a7375SFelipe Balbi 346550a7375SFelipe Balbi if (csr & MUSB_TXCSR_TXPKTRDY) { 3475c8a86e1SFelipe Balbi dev_dbg(musb->controller, "%s old packet still ready , txcsr %03x\n", 348550a7375SFelipe Balbi musb_ep->end_point.name, csr); 349550a7375SFelipe Balbi return; 350550a7375SFelipe Balbi } 351550a7375SFelipe Balbi 352550a7375SFelipe Balbi if (csr & MUSB_TXCSR_P_SENDSTALL) { 3535c8a86e1SFelipe Balbi dev_dbg(musb->controller, "%s stalling, txcsr %03x\n", 354550a7375SFelipe Balbi musb_ep->end_point.name, csr); 355550a7375SFelipe Balbi return; 356550a7375SFelipe Balbi } 357550a7375SFelipe Balbi 3585c8a86e1SFelipe Balbi dev_dbg(musb->controller, "hw_ep%d, maxpacket %d, fifo count %d, txcsr %03x\n", 359550a7375SFelipe Balbi epnum, musb_ep->packet_sz, fifo_count, 360550a7375SFelipe Balbi csr); 361550a7375SFelipe Balbi 362550a7375SFelipe Balbi #ifndef CONFIG_MUSB_PIO_ONLY 363c65bfa62SMian Yousaf Kaukab if (is_buffer_mapped(req)) { 364550a7375SFelipe Balbi struct dma_controller *c = musb->dma_controller; 36566af83ddSMing Lei size_t request_size; 36666af83ddSMing Lei 36766af83ddSMing Lei /* setup DMA, then program endpoint CSR */ 36866af83ddSMing Lei request_size = min_t(size_t, request->length - request->actual, 36966af83ddSMing Lei musb_ep->dma->max_len); 370550a7375SFelipe Balbi 371550a7375SFelipe Balbi use_dma = (request->dma != DMA_ADDR_INVALID); 372550a7375SFelipe Balbi 373550a7375SFelipe Balbi /* MUSB_TXCSR_P_ISO is still set correctly */ 374550a7375SFelipe Balbi 375a48ff906SMian Yousaf Kaukab #if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_UX500_DMA) 376550a7375SFelipe Balbi { 377d1043a26SAnand Gadiyar if (request_size < musb_ep->packet_sz) 378550a7375SFelipe Balbi musb_ep->dma->desired_mode = 0; 379550a7375SFelipe Balbi else 380550a7375SFelipe Balbi musb_ep->dma->desired_mode = 1; 381550a7375SFelipe Balbi 382550a7375SFelipe Balbi use_dma = use_dma && c->channel_program( 383550a7375SFelipe Balbi musb_ep->dma, musb_ep->packet_sz, 384550a7375SFelipe Balbi musb_ep->dma->desired_mode, 385796a83faSCliff Cai request->dma + request->actual, request_size); 386550a7375SFelipe Balbi if (use_dma) { 387550a7375SFelipe Balbi if (musb_ep->dma->desired_mode == 0) { 388b6e434a5SSergei Shtylyov /* 389b6e434a5SSergei Shtylyov * We must not clear the DMAMODE bit 390b6e434a5SSergei Shtylyov * before the DMAENAB bit -- and the 391b6e434a5SSergei Shtylyov * latter doesn't always get cleared 392b6e434a5SSergei Shtylyov * before we get here... 393b6e434a5SSergei Shtylyov */ 394b6e434a5SSergei Shtylyov csr &= ~(MUSB_TXCSR_AUTOSET 395b6e434a5SSergei Shtylyov | MUSB_TXCSR_DMAENAB); 396b6e434a5SSergei Shtylyov musb_writew(epio, MUSB_TXCSR, csr 397b6e434a5SSergei Shtylyov | MUSB_TXCSR_P_WZC_BITS); 398b6e434a5SSergei Shtylyov csr &= ~MUSB_TXCSR_DMAMODE; 399550a7375SFelipe Balbi csr |= (MUSB_TXCSR_DMAENAB | 400550a7375SFelipe Balbi MUSB_TXCSR_MODE); 401550a7375SFelipe Balbi /* against programming guide */ 402f11d893dSMing Lei } else { 403f11d893dSMing Lei csr |= (MUSB_TXCSR_DMAENAB 404550a7375SFelipe Balbi | MUSB_TXCSR_DMAMODE 405550a7375SFelipe Balbi | MUSB_TXCSR_MODE); 406f11d893dSMing Lei if (!musb_ep->hb_mult) 407f11d893dSMing Lei csr |= MUSB_TXCSR_AUTOSET; 408f11d893dSMing Lei } 409550a7375SFelipe Balbi csr &= ~MUSB_TXCSR_P_UNDERRUN; 410f11d893dSMing Lei 411550a7375SFelipe Balbi musb_writew(epio, MUSB_TXCSR, csr); 412550a7375SFelipe Balbi } 413550a7375SFelipe Balbi } 414550a7375SFelipe Balbi 415550a7375SFelipe Balbi #elif defined(CONFIG_USB_TI_CPPI_DMA) 416550a7375SFelipe Balbi /* program endpoint CSR first, then setup DMA */ 417b6e434a5SSergei Shtylyov csr &= ~(MUSB_TXCSR_P_UNDERRUN | MUSB_TXCSR_TXPKTRDY); 41837e3ee99SSergei Shtylyov csr |= MUSB_TXCSR_DMAENAB | MUSB_TXCSR_DMAMODE | 41937e3ee99SSergei Shtylyov MUSB_TXCSR_MODE; 420550a7375SFelipe Balbi musb_writew(epio, MUSB_TXCSR, 421550a7375SFelipe Balbi (MUSB_TXCSR_P_WZC_BITS & ~MUSB_TXCSR_P_UNDERRUN) 422550a7375SFelipe Balbi | csr); 423550a7375SFelipe Balbi 424550a7375SFelipe Balbi /* ensure writebuffer is empty */ 425550a7375SFelipe Balbi csr = musb_readw(epio, MUSB_TXCSR); 426550a7375SFelipe Balbi 427550a7375SFelipe Balbi /* NOTE host side sets DMAENAB later than this; both are 428550a7375SFelipe Balbi * OK since the transfer dma glue (between CPPI and Mentor 429550a7375SFelipe Balbi * fifos) just tells CPPI it could start. Data only moves 430550a7375SFelipe Balbi * to the USB TX fifo when both fifos are ready. 431550a7375SFelipe Balbi */ 432550a7375SFelipe Balbi 433550a7375SFelipe Balbi /* "mode" is irrelevant here; handle terminating ZLPs like 434550a7375SFelipe Balbi * PIO does, since the hardware RNDIS mode seems unreliable 435550a7375SFelipe Balbi * except for the last-packet-is-already-short case. 436550a7375SFelipe Balbi */ 437550a7375SFelipe Balbi use_dma = use_dma && c->channel_program( 438550a7375SFelipe Balbi musb_ep->dma, musb_ep->packet_sz, 439550a7375SFelipe Balbi 0, 44066af83ddSMing Lei request->dma + request->actual, 44166af83ddSMing Lei request_size); 442550a7375SFelipe Balbi if (!use_dma) { 443550a7375SFelipe Balbi c->channel_release(musb_ep->dma); 444550a7375SFelipe Balbi musb_ep->dma = NULL; 445b6e434a5SSergei Shtylyov csr &= ~MUSB_TXCSR_DMAENAB; 446b6e434a5SSergei Shtylyov musb_writew(epio, MUSB_TXCSR, csr); 447550a7375SFelipe Balbi /* invariant: prequest->buf is non-null */ 448550a7375SFelipe Balbi } 449550a7375SFelipe Balbi #elif defined(CONFIG_USB_TUSB_OMAP_DMA) 450550a7375SFelipe Balbi use_dma = use_dma && c->channel_program( 451550a7375SFelipe Balbi musb_ep->dma, musb_ep->packet_sz, 452550a7375SFelipe Balbi request->zero, 45366af83ddSMing Lei request->dma + request->actual, 45466af83ddSMing Lei request_size); 455550a7375SFelipe Balbi #endif 456550a7375SFelipe Balbi } 457550a7375SFelipe Balbi #endif 458550a7375SFelipe Balbi 459550a7375SFelipe Balbi if (!use_dma) { 46092d2711fSHema Kalliguddi /* 46192d2711fSHema Kalliguddi * Unmap the dma buffer back to cpu if dma channel 46292d2711fSHema Kalliguddi * programming fails 46392d2711fSHema Kalliguddi */ 46492d2711fSHema Kalliguddi unmap_dma_buffer(req, musb); 46592d2711fSHema Kalliguddi 466550a7375SFelipe Balbi musb_write_fifo(musb_ep->hw_ep, fifo_count, 467550a7375SFelipe Balbi (u8 *) (request->buf + request->actual)); 468550a7375SFelipe Balbi request->actual += fifo_count; 469550a7375SFelipe Balbi csr |= MUSB_TXCSR_TXPKTRDY; 470550a7375SFelipe Balbi csr &= ~MUSB_TXCSR_P_UNDERRUN; 471550a7375SFelipe Balbi musb_writew(epio, MUSB_TXCSR, csr); 472550a7375SFelipe Balbi } 473550a7375SFelipe Balbi 474550a7375SFelipe Balbi /* host may already have the data when this message shows... */ 4755c8a86e1SFelipe Balbi dev_dbg(musb->controller, "%s TX/IN %s len %d/%d, txcsr %04x, fifo %d/%d\n", 476550a7375SFelipe Balbi musb_ep->end_point.name, use_dma ? "dma" : "pio", 477550a7375SFelipe Balbi request->actual, request->length, 478550a7375SFelipe Balbi musb_readw(epio, MUSB_TXCSR), 479550a7375SFelipe Balbi fifo_count, 480550a7375SFelipe Balbi musb_readw(epio, MUSB_TXMAXP)); 481550a7375SFelipe Balbi } 482550a7375SFelipe Balbi 483550a7375SFelipe Balbi /* 484550a7375SFelipe Balbi * FIFO state update (e.g. data ready). 485550a7375SFelipe Balbi * Called from IRQ, with controller locked. 486550a7375SFelipe Balbi */ 487550a7375SFelipe Balbi void musb_g_tx(struct musb *musb, u8 epnum) 488550a7375SFelipe Balbi { 489550a7375SFelipe Balbi u16 csr; 490ad1adb89SFelipe Balbi struct musb_request *req; 491550a7375SFelipe Balbi struct usb_request *request; 492550a7375SFelipe Balbi u8 __iomem *mbase = musb->mregs; 493550a7375SFelipe Balbi struct musb_ep *musb_ep = &musb->endpoints[epnum].ep_in; 494550a7375SFelipe Balbi void __iomem *epio = musb->endpoints[epnum].regs; 495550a7375SFelipe Balbi struct dma_channel *dma; 496550a7375SFelipe Balbi 497550a7375SFelipe Balbi musb_ep_select(mbase, epnum); 498ad1adb89SFelipe Balbi req = next_request(musb_ep); 499ad1adb89SFelipe Balbi request = &req->request; 500550a7375SFelipe Balbi 501550a7375SFelipe Balbi csr = musb_readw(epio, MUSB_TXCSR); 5025c8a86e1SFelipe Balbi dev_dbg(musb->controller, "<== %s, txcsr %04x\n", musb_ep->end_point.name, csr); 503550a7375SFelipe Balbi 504550a7375SFelipe Balbi dma = is_dma_capable() ? musb_ep->dma : NULL; 5057723de7eSSergei Shtylyov 5067723de7eSSergei Shtylyov /* 5077723de7eSSergei Shtylyov * REVISIT: for high bandwidth, MUSB_TXCSR_P_INCOMPTX 5087723de7eSSergei Shtylyov * probably rates reporting as a host error. 509550a7375SFelipe Balbi */ 510550a7375SFelipe Balbi if (csr & MUSB_TXCSR_P_SENTSTALL) { 511550a7375SFelipe Balbi csr |= MUSB_TXCSR_P_WZC_BITS; 512550a7375SFelipe Balbi csr &= ~MUSB_TXCSR_P_SENTSTALL; 513550a7375SFelipe Balbi musb_writew(epio, MUSB_TXCSR, csr); 5147723de7eSSergei Shtylyov return; 515550a7375SFelipe Balbi } 516550a7375SFelipe Balbi 517550a7375SFelipe Balbi if (csr & MUSB_TXCSR_P_UNDERRUN) { 5187723de7eSSergei Shtylyov /* We NAKed, no big deal... little reason to care. */ 519550a7375SFelipe Balbi csr |= MUSB_TXCSR_P_WZC_BITS; 5207723de7eSSergei Shtylyov csr &= ~(MUSB_TXCSR_P_UNDERRUN | MUSB_TXCSR_TXPKTRDY); 521550a7375SFelipe Balbi musb_writew(epio, MUSB_TXCSR, csr); 5225c8a86e1SFelipe Balbi dev_vdbg(musb->controller, "underrun on ep%d, req %p\n", 5235c8a86e1SFelipe Balbi epnum, request); 524550a7375SFelipe Balbi } 525550a7375SFelipe Balbi 526550a7375SFelipe Balbi if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) { 5277723de7eSSergei Shtylyov /* 5287723de7eSSergei Shtylyov * SHOULD NOT HAPPEN... has with CPPI though, after 529550a7375SFelipe Balbi * changing SENDSTALL (and other cases); harmless? 530550a7375SFelipe Balbi */ 5315c8a86e1SFelipe Balbi dev_dbg(musb->controller, "%s dma still busy?\n", musb_ep->end_point.name); 5327723de7eSSergei Shtylyov return; 533550a7375SFelipe Balbi } 534550a7375SFelipe Balbi 535550a7375SFelipe Balbi if (request) { 536550a7375SFelipe Balbi u8 is_dma = 0; 537550a7375SFelipe Balbi 538550a7375SFelipe Balbi if (dma && (csr & MUSB_TXCSR_DMAENAB)) { 539550a7375SFelipe Balbi is_dma = 1; 540550a7375SFelipe Balbi csr |= MUSB_TXCSR_P_WZC_BITS; 5417723de7eSSergei Shtylyov csr &= ~(MUSB_TXCSR_DMAENAB | MUSB_TXCSR_P_UNDERRUN | 542100d4a9dSMian Yousaf Kaukab MUSB_TXCSR_TXPKTRDY | MUSB_TXCSR_AUTOSET); 543550a7375SFelipe Balbi musb_writew(epio, MUSB_TXCSR, csr); 5447723de7eSSergei Shtylyov /* Ensure writebuffer is empty. */ 545550a7375SFelipe Balbi csr = musb_readw(epio, MUSB_TXCSR); 546550a7375SFelipe Balbi request->actual += musb_ep->dma->actual_len; 5475c8a86e1SFelipe Balbi dev_dbg(musb->controller, "TXCSR%d %04x, DMA off, len %zu, req %p\n", 5487723de7eSSergei Shtylyov epnum, csr, musb_ep->dma->actual_len, request); 549550a7375SFelipe Balbi } 550550a7375SFelipe Balbi 5517723de7eSSergei Shtylyov /* 5527723de7eSSergei Shtylyov * First, maybe a terminating short packet. Some DMA 5537723de7eSSergei Shtylyov * engines might handle this by themselves. 554550a7375SFelipe Balbi */ 5557723de7eSSergei Shtylyov if ((request->zero && request->length 556e7379aaaSMing Lei && (request->length % musb_ep->packet_sz == 0) 557e7379aaaSMing Lei && (request->actual == request->length)) 558a48ff906SMian Yousaf Kaukab #if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_UX500_DMA) 5597723de7eSSergei Shtylyov || (is_dma && (!dma->desired_mode || 560550a7375SFelipe Balbi (request->actual & 561550a7375SFelipe Balbi (musb_ep->packet_sz - 1)))) 562550a7375SFelipe Balbi #endif 563550a7375SFelipe Balbi ) { 5647723de7eSSergei Shtylyov /* 5657723de7eSSergei Shtylyov * On DMA completion, FIFO may not be 5667723de7eSSergei Shtylyov * available yet... 567550a7375SFelipe Balbi */ 568550a7375SFelipe Balbi if (csr & MUSB_TXCSR_TXPKTRDY) 5697723de7eSSergei Shtylyov return; 570550a7375SFelipe Balbi 5715c8a86e1SFelipe Balbi dev_dbg(musb->controller, "sending zero pkt\n"); 5727723de7eSSergei Shtylyov musb_writew(epio, MUSB_TXCSR, MUSB_TXCSR_MODE 573550a7375SFelipe Balbi | MUSB_TXCSR_TXPKTRDY); 574550a7375SFelipe Balbi request->zero = 0; 575550a7375SFelipe Balbi } 576550a7375SFelipe Balbi 577bb27bc2cSMing Lei if (request->actual == request->length) { 578550a7375SFelipe Balbi musb_g_giveback(musb_ep, request, 0); 579ad1adb89SFelipe Balbi req = musb_ep->desc ? next_request(musb_ep) : NULL; 580ad1adb89SFelipe Balbi if (!req) { 5815c8a86e1SFelipe Balbi dev_dbg(musb->controller, "%s idle now\n", 582550a7375SFelipe Balbi musb_ep->end_point.name); 5837723de7eSSergei Shtylyov return; 58495962a77SSergei Shtylyov } 585550a7375SFelipe Balbi } 586550a7375SFelipe Balbi 587ad1adb89SFelipe Balbi txstate(musb, req); 588550a7375SFelipe Balbi } 589550a7375SFelipe Balbi } 590550a7375SFelipe Balbi 591550a7375SFelipe Balbi /* ------------------------------------------------------------ */ 592550a7375SFelipe Balbi 593550a7375SFelipe Balbi #ifdef CONFIG_USB_INVENTRA_DMA 594550a7375SFelipe Balbi 595550a7375SFelipe Balbi /* Peripheral rx (OUT) using Mentor DMA works as follows: 596550a7375SFelipe Balbi - Only mode 0 is used. 597550a7375SFelipe Balbi 598550a7375SFelipe Balbi - Request is queued by the gadget class driver. 599550a7375SFelipe Balbi -> if queue was previously empty, rxstate() 600550a7375SFelipe Balbi 601550a7375SFelipe Balbi - Host sends OUT token which causes an endpoint interrupt 602550a7375SFelipe Balbi /\ -> RxReady 603550a7375SFelipe Balbi | -> if request queued, call rxstate 604550a7375SFelipe Balbi | /\ -> setup DMA 605550a7375SFelipe Balbi | | -> DMA interrupt on completion 606550a7375SFelipe Balbi | | -> RxReady 607550a7375SFelipe Balbi | | -> stop DMA 608550a7375SFelipe Balbi | | -> ack the read 609550a7375SFelipe Balbi | | -> if data recd = max expected 610550a7375SFelipe Balbi | | by the request, or host 611550a7375SFelipe Balbi | | sent a short packet, 612550a7375SFelipe Balbi | | complete the request, 613550a7375SFelipe Balbi | | and start the next one. 614550a7375SFelipe Balbi | |_____________________________________| 615550a7375SFelipe Balbi | else just wait for the host 616550a7375SFelipe Balbi | to send the next OUT token. 617550a7375SFelipe Balbi |__________________________________________________| 618550a7375SFelipe Balbi 619550a7375SFelipe Balbi * Non-Mentor DMA engines can of course work differently. 620550a7375SFelipe Balbi */ 621550a7375SFelipe Balbi 622550a7375SFelipe Balbi #endif 623550a7375SFelipe Balbi 624550a7375SFelipe Balbi /* 625550a7375SFelipe Balbi * Context: controller locked, IRQs blocked, endpoint selected 626550a7375SFelipe Balbi */ 627550a7375SFelipe Balbi static void rxstate(struct musb *musb, struct musb_request *req) 628550a7375SFelipe Balbi { 629550a7375SFelipe Balbi const u8 epnum = req->epnum; 630550a7375SFelipe Balbi struct usb_request *request = &req->request; 631bd2e74d6SMing Lei struct musb_ep *musb_ep; 632550a7375SFelipe Balbi void __iomem *epio = musb->endpoints[epnum].regs; 633c2c96321SFelipe Balbi unsigned fifo_count = 0; 634bd2e74d6SMing Lei u16 len; 635cea83241SSergei Shtylyov u16 csr = musb_readw(epio, MUSB_RXCSR); 636bd2e74d6SMing Lei struct musb_hw_ep *hw_ep = &musb->endpoints[epnum]; 6370ae52d54SAnand Gadiyar u8 use_mode_1; 638bd2e74d6SMing Lei 639bd2e74d6SMing Lei if (hw_ep->is_shared_fifo) 640bd2e74d6SMing Lei musb_ep = &hw_ep->ep_in; 641bd2e74d6SMing Lei else 642bd2e74d6SMing Lei musb_ep = &hw_ep->ep_out; 643bd2e74d6SMing Lei 644bd2e74d6SMing Lei len = musb_ep->packet_sz; 645550a7375SFelipe Balbi 646cea83241SSergei Shtylyov /* We shouldn't get here while DMA is active, but we do... */ 647cea83241SSergei Shtylyov if (dma_channel_status(musb_ep->dma) == MUSB_DMA_STATUS_BUSY) { 6485c8a86e1SFelipe Balbi dev_dbg(musb->controller, "DMA pending...\n"); 649cea83241SSergei Shtylyov return; 650cea83241SSergei Shtylyov } 651cea83241SSergei Shtylyov 652cea83241SSergei Shtylyov if (csr & MUSB_RXCSR_P_SENDSTALL) { 6535c8a86e1SFelipe Balbi dev_dbg(musb->controller, "%s stalling, RXCSR %04x\n", 654cea83241SSergei Shtylyov musb_ep->end_point.name, csr); 655cea83241SSergei Shtylyov return; 656cea83241SSergei Shtylyov } 657550a7375SFelipe Balbi 658c65bfa62SMian Yousaf Kaukab if (is_cppi_enabled() && is_buffer_mapped(req)) { 659550a7375SFelipe Balbi struct dma_controller *c = musb->dma_controller; 660550a7375SFelipe Balbi struct dma_channel *channel = musb_ep->dma; 661550a7375SFelipe Balbi 662550a7375SFelipe Balbi /* NOTE: CPPI won't actually stop advancing the DMA 663550a7375SFelipe Balbi * queue after short packet transfers, so this is almost 664550a7375SFelipe Balbi * always going to run as IRQ-per-packet DMA so that 665550a7375SFelipe Balbi * faults will be handled correctly. 666550a7375SFelipe Balbi */ 667550a7375SFelipe Balbi if (c->channel_program(channel, 668550a7375SFelipe Balbi musb_ep->packet_sz, 669550a7375SFelipe Balbi !request->short_not_ok, 670550a7375SFelipe Balbi request->dma + request->actual, 671550a7375SFelipe Balbi request->length - request->actual)) { 672550a7375SFelipe Balbi 673550a7375SFelipe Balbi /* make sure that if an rxpkt arrived after the irq, 674550a7375SFelipe Balbi * the cppi engine will be ready to take it as soon 675550a7375SFelipe Balbi * as DMA is enabled 676550a7375SFelipe Balbi */ 677550a7375SFelipe Balbi csr &= ~(MUSB_RXCSR_AUTOCLEAR 678550a7375SFelipe Balbi | MUSB_RXCSR_DMAMODE); 679550a7375SFelipe Balbi csr |= MUSB_RXCSR_DMAENAB | MUSB_RXCSR_P_WZC_BITS; 680550a7375SFelipe Balbi musb_writew(epio, MUSB_RXCSR, csr); 681550a7375SFelipe Balbi return; 682550a7375SFelipe Balbi } 683550a7375SFelipe Balbi } 684550a7375SFelipe Balbi 685550a7375SFelipe Balbi if (csr & MUSB_RXCSR_RXPKTRDY) { 686550a7375SFelipe Balbi len = musb_readw(epio, MUSB_RXCOUNT); 6870ae52d54SAnand Gadiyar 6880ae52d54SAnand Gadiyar /* 6890ae52d54SAnand Gadiyar * Enable Mode 1 on RX transfers only when short_not_ok flag 6900ae52d54SAnand Gadiyar * is set. Currently short_not_ok flag is set only from 6910ae52d54SAnand Gadiyar * file_storage and f_mass_storage drivers 6920ae52d54SAnand Gadiyar */ 6930ae52d54SAnand Gadiyar 6940ae52d54SAnand Gadiyar if (request->short_not_ok && len == musb_ep->packet_sz) 6950ae52d54SAnand Gadiyar use_mode_1 = 1; 6960ae52d54SAnand Gadiyar else 6970ae52d54SAnand Gadiyar use_mode_1 = 0; 6980ae52d54SAnand Gadiyar 699550a7375SFelipe Balbi if (request->actual < request->length) { 700550a7375SFelipe Balbi #ifdef CONFIG_USB_INVENTRA_DMA 701c65bfa62SMian Yousaf Kaukab if (is_buffer_mapped(req)) { 702550a7375SFelipe Balbi struct dma_controller *c; 703550a7375SFelipe Balbi struct dma_channel *channel; 704550a7375SFelipe Balbi int use_dma = 0; 705550a7375SFelipe Balbi 706550a7375SFelipe Balbi c = musb->dma_controller; 707550a7375SFelipe Balbi channel = musb_ep->dma; 708550a7375SFelipe Balbi 709550a7375SFelipe Balbi /* We use DMA Req mode 0 in rx_csr, and DMA controller operates in 710550a7375SFelipe Balbi * mode 0 only. So we do not get endpoint interrupts due to DMA 711550a7375SFelipe Balbi * completion. We only get interrupts from DMA controller. 712550a7375SFelipe Balbi * 713550a7375SFelipe Balbi * We could operate in DMA mode 1 if we knew the size of the tranfer 714550a7375SFelipe Balbi * in advance. For mass storage class, request->length = what the host 715550a7375SFelipe Balbi * sends, so that'd work. But for pretty much everything else, 716550a7375SFelipe Balbi * request->length is routinely more than what the host sends. For 717550a7375SFelipe Balbi * most these gadgets, end of is signified either by a short packet, 718550a7375SFelipe Balbi * or filling the last byte of the buffer. (Sending extra data in 719550a7375SFelipe Balbi * that last pckate should trigger an overflow fault.) But in mode 1, 720fb914ebfSVitaliy Ivanov * we don't get DMA completion interrupt for short packets. 721550a7375SFelipe Balbi * 722550a7375SFelipe Balbi * Theoretically, we could enable DMAReq irq (MUSB_RXCSR_DMAMODE = 1), 723550a7375SFelipe Balbi * to get endpoint interrupt on every DMA req, but that didn't seem 724550a7375SFelipe Balbi * to work reliably. 725550a7375SFelipe Balbi * 726550a7375SFelipe Balbi * REVISIT an updated g_file_storage can set req->short_not_ok, which 727550a7375SFelipe Balbi * then becomes usable as a runtime "use mode 1" hint... 728550a7375SFelipe Balbi */ 729550a7375SFelipe Balbi 7300ae52d54SAnand Gadiyar /* Experimental: Mode1 works with mass storage use cases */ 7310ae52d54SAnand Gadiyar if (use_mode_1) { 7329001d80dSMing Lei csr |= MUSB_RXCSR_AUTOCLEAR; 7330ae52d54SAnand Gadiyar musb_writew(epio, MUSB_RXCSR, csr); 7340ae52d54SAnand Gadiyar csr |= MUSB_RXCSR_DMAENAB; 7350ae52d54SAnand Gadiyar musb_writew(epio, MUSB_RXCSR, csr); 736550a7375SFelipe Balbi 7370ae52d54SAnand Gadiyar /* 7380ae52d54SAnand Gadiyar * this special sequence (enabling and then 739550a7375SFelipe Balbi * disabling MUSB_RXCSR_DMAMODE) is required 740550a7375SFelipe Balbi * to get DMAReq to activate 741550a7375SFelipe Balbi */ 742550a7375SFelipe Balbi musb_writew(epio, MUSB_RXCSR, 743550a7375SFelipe Balbi csr | MUSB_RXCSR_DMAMODE); 7440ae52d54SAnand Gadiyar musb_writew(epio, MUSB_RXCSR, csr); 7450ae52d54SAnand Gadiyar 7460ae52d54SAnand Gadiyar } else { 7479001d80dSMing Lei if (!musb_ep->hb_mult && 7489001d80dSMing Lei musb_ep->hw_ep->rx_double_buffered) 7499001d80dSMing Lei csr |= MUSB_RXCSR_AUTOCLEAR; 7500ae52d54SAnand Gadiyar csr |= MUSB_RXCSR_DMAENAB; 751550a7375SFelipe Balbi musb_writew(epio, MUSB_RXCSR, csr); 7520ae52d54SAnand Gadiyar } 753550a7375SFelipe Balbi 754550a7375SFelipe Balbi if (request->actual < request->length) { 755550a7375SFelipe Balbi int transfer_size = 0; 7560ae52d54SAnand Gadiyar if (use_mode_1) { 7571018b4e4SMing Lei transfer_size = min(request->length - request->actual, 758550a7375SFelipe Balbi channel->max_len); 7590ae52d54SAnand Gadiyar musb_ep->dma->desired_mode = 1; 7600ae52d54SAnand Gadiyar } else { 7611018b4e4SMing Lei transfer_size = min(request->length - request->actual, 7621018b4e4SMing Lei (unsigned)len); 763550a7375SFelipe Balbi musb_ep->dma->desired_mode = 0; 7640ae52d54SAnand Gadiyar } 765550a7375SFelipe Balbi 766550a7375SFelipe Balbi use_dma = c->channel_program( 767550a7375SFelipe Balbi channel, 768550a7375SFelipe Balbi musb_ep->packet_sz, 769550a7375SFelipe Balbi channel->desired_mode, 770550a7375SFelipe Balbi request->dma 771550a7375SFelipe Balbi + request->actual, 772550a7375SFelipe Balbi transfer_size); 773550a7375SFelipe Balbi } 774550a7375SFelipe Balbi 775550a7375SFelipe Balbi if (use_dma) 776550a7375SFelipe Balbi return; 777550a7375SFelipe Balbi } 778a48ff906SMian Yousaf Kaukab #elif defined(CONFIG_USB_UX500_DMA) 779a48ff906SMian Yousaf Kaukab if ((is_buffer_mapped(req)) && 780a48ff906SMian Yousaf Kaukab (request->actual < request->length)) { 781a48ff906SMian Yousaf Kaukab 782a48ff906SMian Yousaf Kaukab struct dma_controller *c; 783a48ff906SMian Yousaf Kaukab struct dma_channel *channel; 784a48ff906SMian Yousaf Kaukab int transfer_size = 0; 785a48ff906SMian Yousaf Kaukab 786a48ff906SMian Yousaf Kaukab c = musb->dma_controller; 787a48ff906SMian Yousaf Kaukab channel = musb_ep->dma; 788a48ff906SMian Yousaf Kaukab 789a48ff906SMian Yousaf Kaukab /* In case first packet is short */ 790a48ff906SMian Yousaf Kaukab if (len < musb_ep->packet_sz) 791a48ff906SMian Yousaf Kaukab transfer_size = len; 792a48ff906SMian Yousaf Kaukab else if (request->short_not_ok) 793a48ff906SMian Yousaf Kaukab transfer_size = min(request->length - 794a48ff906SMian Yousaf Kaukab request->actual, 795a48ff906SMian Yousaf Kaukab channel->max_len); 796a48ff906SMian Yousaf Kaukab else 797a48ff906SMian Yousaf Kaukab transfer_size = min(request->length - 798a48ff906SMian Yousaf Kaukab request->actual, 799a48ff906SMian Yousaf Kaukab (unsigned)len); 800a48ff906SMian Yousaf Kaukab 801a48ff906SMian Yousaf Kaukab csr &= ~MUSB_RXCSR_DMAMODE; 802a48ff906SMian Yousaf Kaukab csr |= (MUSB_RXCSR_DMAENAB | 803a48ff906SMian Yousaf Kaukab MUSB_RXCSR_AUTOCLEAR); 804a48ff906SMian Yousaf Kaukab 805a48ff906SMian Yousaf Kaukab musb_writew(epio, MUSB_RXCSR, csr); 806a48ff906SMian Yousaf Kaukab 807a48ff906SMian Yousaf Kaukab if (transfer_size <= musb_ep->packet_sz) { 808a48ff906SMian Yousaf Kaukab musb_ep->dma->desired_mode = 0; 809a48ff906SMian Yousaf Kaukab } else { 810a48ff906SMian Yousaf Kaukab musb_ep->dma->desired_mode = 1; 811a48ff906SMian Yousaf Kaukab /* Mode must be set after DMAENAB */ 812a48ff906SMian Yousaf Kaukab csr |= MUSB_RXCSR_DMAMODE; 813a48ff906SMian Yousaf Kaukab musb_writew(epio, MUSB_RXCSR, csr); 814a48ff906SMian Yousaf Kaukab } 815a48ff906SMian Yousaf Kaukab 816a48ff906SMian Yousaf Kaukab if (c->channel_program(channel, 817a48ff906SMian Yousaf Kaukab musb_ep->packet_sz, 818a48ff906SMian Yousaf Kaukab channel->desired_mode, 819a48ff906SMian Yousaf Kaukab request->dma 820a48ff906SMian Yousaf Kaukab + request->actual, 821a48ff906SMian Yousaf Kaukab transfer_size)) 822a48ff906SMian Yousaf Kaukab 823a48ff906SMian Yousaf Kaukab return; 824a48ff906SMian Yousaf Kaukab } 825550a7375SFelipe Balbi #endif /* Mentor's DMA */ 826550a7375SFelipe Balbi 827550a7375SFelipe Balbi fifo_count = request->length - request->actual; 8285c8a86e1SFelipe Balbi dev_dbg(musb->controller, "%s OUT/RX pio fifo %d/%d, maxpacket %d\n", 829550a7375SFelipe Balbi musb_ep->end_point.name, 830550a7375SFelipe Balbi len, fifo_count, 831550a7375SFelipe Balbi musb_ep->packet_sz); 832550a7375SFelipe Balbi 833c2c96321SFelipe Balbi fifo_count = min_t(unsigned, len, fifo_count); 834550a7375SFelipe Balbi 835550a7375SFelipe Balbi #ifdef CONFIG_USB_TUSB_OMAP_DMA 836c65bfa62SMian Yousaf Kaukab if (tusb_dma_omap() && is_buffer_mapped(req)) { 837550a7375SFelipe Balbi struct dma_controller *c = musb->dma_controller; 838550a7375SFelipe Balbi struct dma_channel *channel = musb_ep->dma; 839550a7375SFelipe Balbi u32 dma_addr = request->dma + request->actual; 840550a7375SFelipe Balbi int ret; 841550a7375SFelipe Balbi 842550a7375SFelipe Balbi ret = c->channel_program(channel, 843550a7375SFelipe Balbi musb_ep->packet_sz, 844550a7375SFelipe Balbi channel->desired_mode, 845550a7375SFelipe Balbi dma_addr, 846550a7375SFelipe Balbi fifo_count); 847550a7375SFelipe Balbi if (ret) 848550a7375SFelipe Balbi return; 849550a7375SFelipe Balbi } 850550a7375SFelipe Balbi #endif 85192d2711fSHema Kalliguddi /* 85292d2711fSHema Kalliguddi * Unmap the dma buffer back to cpu if dma channel 85392d2711fSHema Kalliguddi * programming fails. This buffer is mapped if the 85492d2711fSHema Kalliguddi * channel allocation is successful 85592d2711fSHema Kalliguddi */ 856c65bfa62SMian Yousaf Kaukab if (is_buffer_mapped(req)) { 85792d2711fSHema Kalliguddi unmap_dma_buffer(req, musb); 85892d2711fSHema Kalliguddi 859e75df371SMing Lei /* 860e75df371SMing Lei * Clear DMAENAB and AUTOCLEAR for the 86192d2711fSHema Kalliguddi * PIO mode transfer 86292d2711fSHema Kalliguddi */ 863e75df371SMing Lei csr &= ~(MUSB_RXCSR_DMAENAB | MUSB_RXCSR_AUTOCLEAR); 86492d2711fSHema Kalliguddi musb_writew(epio, MUSB_RXCSR, csr); 86592d2711fSHema Kalliguddi } 866550a7375SFelipe Balbi 867550a7375SFelipe Balbi musb_read_fifo(musb_ep->hw_ep, fifo_count, (u8 *) 868550a7375SFelipe Balbi (request->buf + request->actual)); 869550a7375SFelipe Balbi request->actual += fifo_count; 870550a7375SFelipe Balbi 871550a7375SFelipe Balbi /* REVISIT if we left anything in the fifo, flush 872550a7375SFelipe Balbi * it and report -EOVERFLOW 873550a7375SFelipe Balbi */ 874550a7375SFelipe Balbi 875550a7375SFelipe Balbi /* ack the read! */ 876550a7375SFelipe Balbi csr |= MUSB_RXCSR_P_WZC_BITS; 877550a7375SFelipe Balbi csr &= ~MUSB_RXCSR_RXPKTRDY; 878550a7375SFelipe Balbi musb_writew(epio, MUSB_RXCSR, csr); 879550a7375SFelipe Balbi } 880550a7375SFelipe Balbi } 881550a7375SFelipe Balbi 882550a7375SFelipe Balbi /* reach the end or short packet detected */ 883550a7375SFelipe Balbi if (request->actual == request->length || len < musb_ep->packet_sz) 884550a7375SFelipe Balbi musb_g_giveback(musb_ep, request, 0); 885550a7375SFelipe Balbi } 886550a7375SFelipe Balbi 887550a7375SFelipe Balbi /* 888550a7375SFelipe Balbi * Data ready for a request; called from IRQ 889550a7375SFelipe Balbi */ 890550a7375SFelipe Balbi void musb_g_rx(struct musb *musb, u8 epnum) 891550a7375SFelipe Balbi { 892550a7375SFelipe Balbi u16 csr; 893ad1adb89SFelipe Balbi struct musb_request *req; 894550a7375SFelipe Balbi struct usb_request *request; 895550a7375SFelipe Balbi void __iomem *mbase = musb->mregs; 896bd2e74d6SMing Lei struct musb_ep *musb_ep; 897550a7375SFelipe Balbi void __iomem *epio = musb->endpoints[epnum].regs; 898550a7375SFelipe Balbi struct dma_channel *dma; 899bd2e74d6SMing Lei struct musb_hw_ep *hw_ep = &musb->endpoints[epnum]; 900bd2e74d6SMing Lei 901bd2e74d6SMing Lei if (hw_ep->is_shared_fifo) 902bd2e74d6SMing Lei musb_ep = &hw_ep->ep_in; 903bd2e74d6SMing Lei else 904bd2e74d6SMing Lei musb_ep = &hw_ep->ep_out; 905550a7375SFelipe Balbi 906550a7375SFelipe Balbi musb_ep_select(mbase, epnum); 907550a7375SFelipe Balbi 908ad1adb89SFelipe Balbi req = next_request(musb_ep); 909ad1adb89SFelipe Balbi if (!req) 9100abdc36fSMaulik Mankad return; 911550a7375SFelipe Balbi 912ad1adb89SFelipe Balbi request = &req->request; 913ad1adb89SFelipe Balbi 914550a7375SFelipe Balbi csr = musb_readw(epio, MUSB_RXCSR); 915550a7375SFelipe Balbi dma = is_dma_capable() ? musb_ep->dma : NULL; 916550a7375SFelipe Balbi 9175c8a86e1SFelipe Balbi dev_dbg(musb->controller, "<== %s, rxcsr %04x%s %p\n", musb_ep->end_point.name, 918550a7375SFelipe Balbi csr, dma ? " (dma)" : "", request); 919550a7375SFelipe Balbi 920550a7375SFelipe Balbi if (csr & MUSB_RXCSR_P_SENTSTALL) { 921550a7375SFelipe Balbi csr |= MUSB_RXCSR_P_WZC_BITS; 922550a7375SFelipe Balbi csr &= ~MUSB_RXCSR_P_SENTSTALL; 923550a7375SFelipe Balbi musb_writew(epio, MUSB_RXCSR, csr); 924cea83241SSergei Shtylyov return; 925550a7375SFelipe Balbi } 926550a7375SFelipe Balbi 927550a7375SFelipe Balbi if (csr & MUSB_RXCSR_P_OVERRUN) { 928550a7375SFelipe Balbi /* csr |= MUSB_RXCSR_P_WZC_BITS; */ 929550a7375SFelipe Balbi csr &= ~MUSB_RXCSR_P_OVERRUN; 930550a7375SFelipe Balbi musb_writew(epio, MUSB_RXCSR, csr); 931550a7375SFelipe Balbi 9325c8a86e1SFelipe Balbi dev_dbg(musb->controller, "%s iso overrun on %p\n", musb_ep->name, request); 93343467868SSergei Shtylyov if (request->status == -EINPROGRESS) 934550a7375SFelipe Balbi request->status = -EOVERFLOW; 935550a7375SFelipe Balbi } 936550a7375SFelipe Balbi if (csr & MUSB_RXCSR_INCOMPRX) { 937550a7375SFelipe Balbi /* REVISIT not necessarily an error */ 9385c8a86e1SFelipe Balbi dev_dbg(musb->controller, "%s, incomprx\n", musb_ep->end_point.name); 939550a7375SFelipe Balbi } 940550a7375SFelipe Balbi 941550a7375SFelipe Balbi if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) { 942550a7375SFelipe Balbi /* "should not happen"; likely RXPKTRDY pending for DMA */ 9435c8a86e1SFelipe Balbi dev_dbg(musb->controller, "%s busy, csr %04x\n", 944550a7375SFelipe Balbi musb_ep->end_point.name, csr); 945cea83241SSergei Shtylyov return; 946550a7375SFelipe Balbi } 947550a7375SFelipe Balbi 948550a7375SFelipe Balbi if (dma && (csr & MUSB_RXCSR_DMAENAB)) { 949550a7375SFelipe Balbi csr &= ~(MUSB_RXCSR_AUTOCLEAR 950550a7375SFelipe Balbi | MUSB_RXCSR_DMAENAB 951550a7375SFelipe Balbi | MUSB_RXCSR_DMAMODE); 952550a7375SFelipe Balbi musb_writew(epio, MUSB_RXCSR, 953550a7375SFelipe Balbi MUSB_RXCSR_P_WZC_BITS | csr); 954550a7375SFelipe Balbi 955550a7375SFelipe Balbi request->actual += musb_ep->dma->actual_len; 956550a7375SFelipe Balbi 9575c8a86e1SFelipe Balbi dev_dbg(musb->controller, "RXCSR%d %04x, dma off, %04x, len %zu, req %p\n", 958550a7375SFelipe Balbi epnum, csr, 959550a7375SFelipe Balbi musb_readw(epio, MUSB_RXCSR), 960550a7375SFelipe Balbi musb_ep->dma->actual_len, request); 961550a7375SFelipe Balbi 962a48ff906SMian Yousaf Kaukab #if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_TUSB_OMAP_DMA) || \ 963a48ff906SMian Yousaf Kaukab defined(CONFIG_USB_UX500_DMA) 964550a7375SFelipe Balbi /* Autoclear doesn't clear RxPktRdy for short packets */ 9659001d80dSMing Lei if ((dma->desired_mode == 0 && !hw_ep->rx_double_buffered) 966550a7375SFelipe Balbi || (dma->actual_len 967550a7375SFelipe Balbi & (musb_ep->packet_sz - 1))) { 968550a7375SFelipe Balbi /* ack the read! */ 969550a7375SFelipe Balbi csr &= ~MUSB_RXCSR_RXPKTRDY; 970550a7375SFelipe Balbi musb_writew(epio, MUSB_RXCSR, csr); 971550a7375SFelipe Balbi } 972550a7375SFelipe Balbi 973550a7375SFelipe Balbi /* incomplete, and not short? wait for next IN packet */ 974550a7375SFelipe Balbi if ((request->actual < request->length) 975550a7375SFelipe Balbi && (musb_ep->dma->actual_len 9769001d80dSMing Lei == musb_ep->packet_sz)) { 9779001d80dSMing Lei /* In double buffer case, continue to unload fifo if 9789001d80dSMing Lei * there is Rx packet in FIFO. 9799001d80dSMing Lei **/ 9809001d80dSMing Lei csr = musb_readw(epio, MUSB_RXCSR); 9819001d80dSMing Lei if ((csr & MUSB_RXCSR_RXPKTRDY) && 9829001d80dSMing Lei hw_ep->rx_double_buffered) 9839001d80dSMing Lei goto exit; 984cea83241SSergei Shtylyov return; 9859001d80dSMing Lei } 986550a7375SFelipe Balbi #endif 987550a7375SFelipe Balbi musb_g_giveback(musb_ep, request, 0); 988550a7375SFelipe Balbi 989ad1adb89SFelipe Balbi req = next_request(musb_ep); 990ad1adb89SFelipe Balbi if (!req) 991cea83241SSergei Shtylyov return; 992550a7375SFelipe Balbi } 993a48ff906SMian Yousaf Kaukab #if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_TUSB_OMAP_DMA) || \ 994a48ff906SMian Yousaf Kaukab defined(CONFIG_USB_UX500_DMA) 9959001d80dSMing Lei exit: 996bb324b08SAjay Kumar Gupta #endif 99743467868SSergei Shtylyov /* Analyze request */ 998ad1adb89SFelipe Balbi rxstate(musb, req); 999550a7375SFelipe Balbi } 1000550a7375SFelipe Balbi 1001550a7375SFelipe Balbi /* ------------------------------------------------------------ */ 1002550a7375SFelipe Balbi 1003550a7375SFelipe Balbi static int musb_gadget_enable(struct usb_ep *ep, 1004550a7375SFelipe Balbi const struct usb_endpoint_descriptor *desc) 1005550a7375SFelipe Balbi { 1006550a7375SFelipe Balbi unsigned long flags; 1007550a7375SFelipe Balbi struct musb_ep *musb_ep; 1008550a7375SFelipe Balbi struct musb_hw_ep *hw_ep; 1009550a7375SFelipe Balbi void __iomem *regs; 1010550a7375SFelipe Balbi struct musb *musb; 1011550a7375SFelipe Balbi void __iomem *mbase; 1012550a7375SFelipe Balbi u8 epnum; 1013550a7375SFelipe Balbi u16 csr; 1014550a7375SFelipe Balbi unsigned tmp; 1015550a7375SFelipe Balbi int status = -EINVAL; 1016550a7375SFelipe Balbi 1017550a7375SFelipe Balbi if (!ep || !desc) 1018550a7375SFelipe Balbi return -EINVAL; 1019550a7375SFelipe Balbi 1020550a7375SFelipe Balbi musb_ep = to_musb_ep(ep); 1021550a7375SFelipe Balbi hw_ep = musb_ep->hw_ep; 1022550a7375SFelipe Balbi regs = hw_ep->regs; 1023550a7375SFelipe Balbi musb = musb_ep->musb; 1024550a7375SFelipe Balbi mbase = musb->mregs; 1025550a7375SFelipe Balbi epnum = musb_ep->current_epnum; 1026550a7375SFelipe Balbi 1027550a7375SFelipe Balbi spin_lock_irqsave(&musb->lock, flags); 1028550a7375SFelipe Balbi 1029550a7375SFelipe Balbi if (musb_ep->desc) { 1030550a7375SFelipe Balbi status = -EBUSY; 1031550a7375SFelipe Balbi goto fail; 1032550a7375SFelipe Balbi } 103396bcd090SJulia Lawall musb_ep->type = usb_endpoint_type(desc); 1034550a7375SFelipe Balbi 1035550a7375SFelipe Balbi /* check direction and (later) maxpacket size against endpoint */ 103696bcd090SJulia Lawall if (usb_endpoint_num(desc) != epnum) 1037550a7375SFelipe Balbi goto fail; 1038550a7375SFelipe Balbi 1039550a7375SFelipe Balbi /* REVISIT this rules out high bandwidth periodic transfers */ 104029cc8897SKuninori Morimoto tmp = usb_endpoint_maxp(desc); 1041f11d893dSMing Lei if (tmp & ~0x07ff) { 1042f11d893dSMing Lei int ok; 1043f11d893dSMing Lei 1044f11d893dSMing Lei if (usb_endpoint_dir_in(desc)) 1045f11d893dSMing Lei ok = musb->hb_iso_tx; 1046f11d893dSMing Lei else 1047f11d893dSMing Lei ok = musb->hb_iso_rx; 1048f11d893dSMing Lei 1049f11d893dSMing Lei if (!ok) { 10505c8a86e1SFelipe Balbi dev_dbg(musb->controller, "no support for high bandwidth ISO\n"); 1051550a7375SFelipe Balbi goto fail; 1052f11d893dSMing Lei } 1053f11d893dSMing Lei musb_ep->hb_mult = (tmp >> 11) & 3; 1054f11d893dSMing Lei } else { 1055f11d893dSMing Lei musb_ep->hb_mult = 0; 1056f11d893dSMing Lei } 1057f11d893dSMing Lei 1058f11d893dSMing Lei musb_ep->packet_sz = tmp & 0x7ff; 1059f11d893dSMing Lei tmp = musb_ep->packet_sz * (musb_ep->hb_mult + 1); 1060550a7375SFelipe Balbi 1061550a7375SFelipe Balbi /* enable the interrupts for the endpoint, set the endpoint 1062550a7375SFelipe Balbi * packet size (or fail), set the mode, clear the fifo 1063550a7375SFelipe Balbi */ 1064550a7375SFelipe Balbi musb_ep_select(mbase, epnum); 106596bcd090SJulia Lawall if (usb_endpoint_dir_in(desc)) { 1066550a7375SFelipe Balbi u16 int_txe = musb_readw(mbase, MUSB_INTRTXE); 1067550a7375SFelipe Balbi 1068550a7375SFelipe Balbi if (hw_ep->is_shared_fifo) 1069550a7375SFelipe Balbi musb_ep->is_in = 1; 1070550a7375SFelipe Balbi if (!musb_ep->is_in) 1071550a7375SFelipe Balbi goto fail; 1072f11d893dSMing Lei 1073f11d893dSMing Lei if (tmp > hw_ep->max_packet_sz_tx) { 10745c8a86e1SFelipe Balbi dev_dbg(musb->controller, "packet size beyond hardware FIFO size\n"); 1075550a7375SFelipe Balbi goto fail; 1076f11d893dSMing Lei } 1077550a7375SFelipe Balbi 1078550a7375SFelipe Balbi int_txe |= (1 << epnum); 1079550a7375SFelipe Balbi musb_writew(mbase, MUSB_INTRTXE, int_txe); 1080550a7375SFelipe Balbi 1081550a7375SFelipe Balbi /* REVISIT if can_bulk_split(), use by updating "tmp"; 1082550a7375SFelipe Balbi * likewise high bandwidth periodic tx 1083550a7375SFelipe Balbi */ 10849f445cb2SCliff Cai /* Set TXMAXP with the FIFO size of the endpoint 108531c9909bSMing Lei * to disable double buffering mode. 10869f445cb2SCliff Cai */ 108706624818SFelipe Balbi if (musb->double_buffer_not_ok) 108806624818SFelipe Balbi musb_writew(regs, MUSB_TXMAXP, hw_ep->max_packet_sz_tx); 108906624818SFelipe Balbi else 109006624818SFelipe Balbi musb_writew(regs, MUSB_TXMAXP, musb_ep->packet_sz 109106624818SFelipe Balbi | (musb_ep->hb_mult << 11)); 1092550a7375SFelipe Balbi 1093550a7375SFelipe Balbi csr = MUSB_TXCSR_MODE | MUSB_TXCSR_CLRDATATOG; 1094550a7375SFelipe Balbi if (musb_readw(regs, MUSB_TXCSR) 1095550a7375SFelipe Balbi & MUSB_TXCSR_FIFONOTEMPTY) 1096550a7375SFelipe Balbi csr |= MUSB_TXCSR_FLUSHFIFO; 1097550a7375SFelipe Balbi if (musb_ep->type == USB_ENDPOINT_XFER_ISOC) 1098550a7375SFelipe Balbi csr |= MUSB_TXCSR_P_ISO; 1099550a7375SFelipe Balbi 1100550a7375SFelipe Balbi /* set twice in case of double buffering */ 1101550a7375SFelipe Balbi musb_writew(regs, MUSB_TXCSR, csr); 1102550a7375SFelipe Balbi /* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */ 1103550a7375SFelipe Balbi musb_writew(regs, MUSB_TXCSR, csr); 1104550a7375SFelipe Balbi 1105550a7375SFelipe Balbi } else { 1106550a7375SFelipe Balbi u16 int_rxe = musb_readw(mbase, MUSB_INTRRXE); 1107550a7375SFelipe Balbi 1108550a7375SFelipe Balbi if (hw_ep->is_shared_fifo) 1109550a7375SFelipe Balbi musb_ep->is_in = 0; 1110550a7375SFelipe Balbi if (musb_ep->is_in) 1111550a7375SFelipe Balbi goto fail; 1112f11d893dSMing Lei 1113f11d893dSMing Lei if (tmp > hw_ep->max_packet_sz_rx) { 11145c8a86e1SFelipe Balbi dev_dbg(musb->controller, "packet size beyond hardware FIFO size\n"); 1115550a7375SFelipe Balbi goto fail; 1116f11d893dSMing Lei } 1117550a7375SFelipe Balbi 1118550a7375SFelipe Balbi int_rxe |= (1 << epnum); 1119550a7375SFelipe Balbi musb_writew(mbase, MUSB_INTRRXE, int_rxe); 1120550a7375SFelipe Balbi 1121550a7375SFelipe Balbi /* REVISIT if can_bulk_combine() use by updating "tmp" 1122550a7375SFelipe Balbi * likewise high bandwidth periodic rx 1123550a7375SFelipe Balbi */ 11249f445cb2SCliff Cai /* Set RXMAXP with the FIFO size of the endpoint 11259f445cb2SCliff Cai * to disable double buffering mode. 11269f445cb2SCliff Cai */ 112706624818SFelipe Balbi if (musb->double_buffer_not_ok) 112806624818SFelipe Balbi musb_writew(regs, MUSB_RXMAXP, hw_ep->max_packet_sz_tx); 112906624818SFelipe Balbi else 113006624818SFelipe Balbi musb_writew(regs, MUSB_RXMAXP, musb_ep->packet_sz 113106624818SFelipe Balbi | (musb_ep->hb_mult << 11)); 1132550a7375SFelipe Balbi 1133550a7375SFelipe Balbi /* force shared fifo to OUT-only mode */ 1134550a7375SFelipe Balbi if (hw_ep->is_shared_fifo) { 1135550a7375SFelipe Balbi csr = musb_readw(regs, MUSB_TXCSR); 1136550a7375SFelipe Balbi csr &= ~(MUSB_TXCSR_MODE | MUSB_TXCSR_TXPKTRDY); 1137550a7375SFelipe Balbi musb_writew(regs, MUSB_TXCSR, csr); 1138550a7375SFelipe Balbi } 1139550a7375SFelipe Balbi 1140550a7375SFelipe Balbi csr = MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_CLRDATATOG; 1141550a7375SFelipe Balbi if (musb_ep->type == USB_ENDPOINT_XFER_ISOC) 1142550a7375SFelipe Balbi csr |= MUSB_RXCSR_P_ISO; 1143550a7375SFelipe Balbi else if (musb_ep->type == USB_ENDPOINT_XFER_INT) 1144550a7375SFelipe Balbi csr |= MUSB_RXCSR_DISNYET; 1145550a7375SFelipe Balbi 1146550a7375SFelipe Balbi /* set twice in case of double buffering */ 1147550a7375SFelipe Balbi musb_writew(regs, MUSB_RXCSR, csr); 1148550a7375SFelipe Balbi musb_writew(regs, MUSB_RXCSR, csr); 1149550a7375SFelipe Balbi } 1150550a7375SFelipe Balbi 1151550a7375SFelipe Balbi /* NOTE: all the I/O code _should_ work fine without DMA, in case 1152550a7375SFelipe Balbi * for some reason you run out of channels here. 1153550a7375SFelipe Balbi */ 1154550a7375SFelipe Balbi if (is_dma_capable() && musb->dma_controller) { 1155550a7375SFelipe Balbi struct dma_controller *c = musb->dma_controller; 1156550a7375SFelipe Balbi 1157550a7375SFelipe Balbi musb_ep->dma = c->channel_alloc(c, hw_ep, 1158550a7375SFelipe Balbi (desc->bEndpointAddress & USB_DIR_IN)); 1159550a7375SFelipe Balbi } else 1160550a7375SFelipe Balbi musb_ep->dma = NULL; 1161550a7375SFelipe Balbi 1162550a7375SFelipe Balbi musb_ep->desc = desc; 1163550a7375SFelipe Balbi musb_ep->busy = 0; 116447e97605SSergei Shtylyov musb_ep->wedged = 0; 1165550a7375SFelipe Balbi status = 0; 1166550a7375SFelipe Balbi 1167550a7375SFelipe Balbi pr_debug("%s periph: enabled %s for %s %s, %smaxpacket %d\n", 1168550a7375SFelipe Balbi musb_driver_name, musb_ep->end_point.name, 1169550a7375SFelipe Balbi ({ char *s; switch (musb_ep->type) { 1170550a7375SFelipe Balbi case USB_ENDPOINT_XFER_BULK: s = "bulk"; break; 1171550a7375SFelipe Balbi case USB_ENDPOINT_XFER_INT: s = "int"; break; 1172550a7375SFelipe Balbi default: s = "iso"; break; 1173550a7375SFelipe Balbi }; s; }), 1174550a7375SFelipe Balbi musb_ep->is_in ? "IN" : "OUT", 1175550a7375SFelipe Balbi musb_ep->dma ? "dma, " : "", 1176550a7375SFelipe Balbi musb_ep->packet_sz); 1177550a7375SFelipe Balbi 1178550a7375SFelipe Balbi schedule_work(&musb->irq_work); 1179550a7375SFelipe Balbi 1180550a7375SFelipe Balbi fail: 1181550a7375SFelipe Balbi spin_unlock_irqrestore(&musb->lock, flags); 1182550a7375SFelipe Balbi return status; 1183550a7375SFelipe Balbi } 1184550a7375SFelipe Balbi 1185550a7375SFelipe Balbi /* 1186550a7375SFelipe Balbi * Disable an endpoint flushing all requests queued. 1187550a7375SFelipe Balbi */ 1188550a7375SFelipe Balbi static int musb_gadget_disable(struct usb_ep *ep) 1189550a7375SFelipe Balbi { 1190550a7375SFelipe Balbi unsigned long flags; 1191550a7375SFelipe Balbi struct musb *musb; 1192550a7375SFelipe Balbi u8 epnum; 1193550a7375SFelipe Balbi struct musb_ep *musb_ep; 1194550a7375SFelipe Balbi void __iomem *epio; 1195550a7375SFelipe Balbi int status = 0; 1196550a7375SFelipe Balbi 1197550a7375SFelipe Balbi musb_ep = to_musb_ep(ep); 1198550a7375SFelipe Balbi musb = musb_ep->musb; 1199550a7375SFelipe Balbi epnum = musb_ep->current_epnum; 1200550a7375SFelipe Balbi epio = musb->endpoints[epnum].regs; 1201550a7375SFelipe Balbi 1202550a7375SFelipe Balbi spin_lock_irqsave(&musb->lock, flags); 1203550a7375SFelipe Balbi musb_ep_select(musb->mregs, epnum); 1204550a7375SFelipe Balbi 1205550a7375SFelipe Balbi /* zero the endpoint sizes */ 1206550a7375SFelipe Balbi if (musb_ep->is_in) { 1207550a7375SFelipe Balbi u16 int_txe = musb_readw(musb->mregs, MUSB_INTRTXE); 1208550a7375SFelipe Balbi int_txe &= ~(1 << epnum); 1209550a7375SFelipe Balbi musb_writew(musb->mregs, MUSB_INTRTXE, int_txe); 1210550a7375SFelipe Balbi musb_writew(epio, MUSB_TXMAXP, 0); 1211550a7375SFelipe Balbi } else { 1212550a7375SFelipe Balbi u16 int_rxe = musb_readw(musb->mregs, MUSB_INTRRXE); 1213550a7375SFelipe Balbi int_rxe &= ~(1 << epnum); 1214550a7375SFelipe Balbi musb_writew(musb->mregs, MUSB_INTRRXE, int_rxe); 1215550a7375SFelipe Balbi musb_writew(epio, MUSB_RXMAXP, 0); 1216550a7375SFelipe Balbi } 1217550a7375SFelipe Balbi 1218550a7375SFelipe Balbi musb_ep->desc = NULL; 1219550a7375SFelipe Balbi 1220550a7375SFelipe Balbi /* abort all pending DMA and requests */ 1221550a7375SFelipe Balbi nuke(musb_ep, -ESHUTDOWN); 1222550a7375SFelipe Balbi 1223550a7375SFelipe Balbi schedule_work(&musb->irq_work); 1224550a7375SFelipe Balbi 1225550a7375SFelipe Balbi spin_unlock_irqrestore(&(musb->lock), flags); 1226550a7375SFelipe Balbi 12275c8a86e1SFelipe Balbi dev_dbg(musb->controller, "%s\n", musb_ep->end_point.name); 1228550a7375SFelipe Balbi 1229550a7375SFelipe Balbi return status; 1230550a7375SFelipe Balbi } 1231550a7375SFelipe Balbi 1232550a7375SFelipe Balbi /* 1233550a7375SFelipe Balbi * Allocate a request for an endpoint. 1234550a7375SFelipe Balbi * Reused by ep0 code. 1235550a7375SFelipe Balbi */ 1236550a7375SFelipe Balbi struct usb_request *musb_alloc_request(struct usb_ep *ep, gfp_t gfp_flags) 1237550a7375SFelipe Balbi { 1238550a7375SFelipe Balbi struct musb_ep *musb_ep = to_musb_ep(ep); 12395c8a86e1SFelipe Balbi struct musb *musb = musb_ep->musb; 1240550a7375SFelipe Balbi struct musb_request *request = NULL; 1241550a7375SFelipe Balbi 1242550a7375SFelipe Balbi request = kzalloc(sizeof *request, gfp_flags); 12430607f862SFelipe Balbi if (!request) { 12445c8a86e1SFelipe Balbi dev_dbg(musb->controller, "not enough memory\n"); 12450607f862SFelipe Balbi return NULL; 12460607f862SFelipe Balbi } 12470607f862SFelipe Balbi 1248550a7375SFelipe Balbi request->request.dma = DMA_ADDR_INVALID; 1249550a7375SFelipe Balbi request->epnum = musb_ep->current_epnum; 1250550a7375SFelipe Balbi request->ep = musb_ep; 1251550a7375SFelipe Balbi 1252550a7375SFelipe Balbi return &request->request; 1253550a7375SFelipe Balbi } 1254550a7375SFelipe Balbi 1255550a7375SFelipe Balbi /* 1256550a7375SFelipe Balbi * Free a request 1257550a7375SFelipe Balbi * Reused by ep0 code. 1258550a7375SFelipe Balbi */ 1259550a7375SFelipe Balbi void musb_free_request(struct usb_ep *ep, struct usb_request *req) 1260550a7375SFelipe Balbi { 1261550a7375SFelipe Balbi kfree(to_musb_request(req)); 1262550a7375SFelipe Balbi } 1263550a7375SFelipe Balbi 1264550a7375SFelipe Balbi static LIST_HEAD(buffers); 1265550a7375SFelipe Balbi 1266550a7375SFelipe Balbi struct free_record { 1267550a7375SFelipe Balbi struct list_head list; 1268550a7375SFelipe Balbi struct device *dev; 1269550a7375SFelipe Balbi unsigned bytes; 1270550a7375SFelipe Balbi dma_addr_t dma; 1271550a7375SFelipe Balbi }; 1272550a7375SFelipe Balbi 1273550a7375SFelipe Balbi /* 1274550a7375SFelipe Balbi * Context: controller locked, IRQs blocked. 1275550a7375SFelipe Balbi */ 1276a666e3e6SSergei Shtylyov void musb_ep_restart(struct musb *musb, struct musb_request *req) 1277550a7375SFelipe Balbi { 12785c8a86e1SFelipe Balbi dev_dbg(musb->controller, "<== %s request %p len %u on hw_ep%d\n", 1279550a7375SFelipe Balbi req->tx ? "TX/IN" : "RX/OUT", 1280550a7375SFelipe Balbi &req->request, req->request.length, req->epnum); 1281550a7375SFelipe Balbi 1282550a7375SFelipe Balbi musb_ep_select(musb->mregs, req->epnum); 1283550a7375SFelipe Balbi if (req->tx) 1284550a7375SFelipe Balbi txstate(musb, req); 1285550a7375SFelipe Balbi else 1286550a7375SFelipe Balbi rxstate(musb, req); 1287550a7375SFelipe Balbi } 1288550a7375SFelipe Balbi 1289550a7375SFelipe Balbi static int musb_gadget_queue(struct usb_ep *ep, struct usb_request *req, 1290550a7375SFelipe Balbi gfp_t gfp_flags) 1291550a7375SFelipe Balbi { 1292550a7375SFelipe Balbi struct musb_ep *musb_ep; 1293550a7375SFelipe Balbi struct musb_request *request; 1294550a7375SFelipe Balbi struct musb *musb; 1295550a7375SFelipe Balbi int status = 0; 1296550a7375SFelipe Balbi unsigned long lockflags; 1297550a7375SFelipe Balbi 1298550a7375SFelipe Balbi if (!ep || !req) 1299550a7375SFelipe Balbi return -EINVAL; 1300550a7375SFelipe Balbi if (!req->buf) 1301550a7375SFelipe Balbi return -ENODATA; 1302550a7375SFelipe Balbi 1303550a7375SFelipe Balbi musb_ep = to_musb_ep(ep); 1304550a7375SFelipe Balbi musb = musb_ep->musb; 1305550a7375SFelipe Balbi 1306550a7375SFelipe Balbi request = to_musb_request(req); 1307550a7375SFelipe Balbi request->musb = musb; 1308550a7375SFelipe Balbi 1309550a7375SFelipe Balbi if (request->ep != musb_ep) 1310550a7375SFelipe Balbi return -EINVAL; 1311550a7375SFelipe Balbi 13125c8a86e1SFelipe Balbi dev_dbg(musb->controller, "<== to %s request=%p\n", ep->name, req); 1313550a7375SFelipe Balbi 1314550a7375SFelipe Balbi /* request is mine now... */ 1315550a7375SFelipe Balbi request->request.actual = 0; 1316550a7375SFelipe Balbi request->request.status = -EINPROGRESS; 1317550a7375SFelipe Balbi request->epnum = musb_ep->current_epnum; 1318550a7375SFelipe Balbi request->tx = musb_ep->is_in; 1319550a7375SFelipe Balbi 1320c65bfa62SMian Yousaf Kaukab map_dma_buffer(request, musb, musb_ep); 1321550a7375SFelipe Balbi 1322550a7375SFelipe Balbi spin_lock_irqsave(&musb->lock, lockflags); 1323550a7375SFelipe Balbi 1324550a7375SFelipe Balbi /* don't queue if the ep is down */ 1325550a7375SFelipe Balbi if (!musb_ep->desc) { 13265c8a86e1SFelipe Balbi dev_dbg(musb->controller, "req %p queued to %s while ep %s\n", 1327550a7375SFelipe Balbi req, ep->name, "disabled"); 1328550a7375SFelipe Balbi status = -ESHUTDOWN; 1329550a7375SFelipe Balbi goto cleanup; 1330550a7375SFelipe Balbi } 1331550a7375SFelipe Balbi 1332550a7375SFelipe Balbi /* add request to the list */ 1333ad1adb89SFelipe Balbi list_add_tail(&request->list, &musb_ep->req_list); 1334550a7375SFelipe Balbi 1335550a7375SFelipe Balbi /* it this is the head of the queue, start i/o ... */ 1336ad1adb89SFelipe Balbi if (!musb_ep->busy && &request->list == musb_ep->req_list.next) 1337550a7375SFelipe Balbi musb_ep_restart(musb, request); 1338550a7375SFelipe Balbi 1339550a7375SFelipe Balbi cleanup: 1340550a7375SFelipe Balbi spin_unlock_irqrestore(&musb->lock, lockflags); 1341550a7375SFelipe Balbi return status; 1342550a7375SFelipe Balbi } 1343550a7375SFelipe Balbi 1344550a7375SFelipe Balbi static int musb_gadget_dequeue(struct usb_ep *ep, struct usb_request *request) 1345550a7375SFelipe Balbi { 1346550a7375SFelipe Balbi struct musb_ep *musb_ep = to_musb_ep(ep); 13474cbbf084SFelipe Balbi struct musb_request *req = to_musb_request(request); 13484cbbf084SFelipe Balbi struct musb_request *r; 1349550a7375SFelipe Balbi unsigned long flags; 1350550a7375SFelipe Balbi int status = 0; 1351550a7375SFelipe Balbi struct musb *musb = musb_ep->musb; 1352550a7375SFelipe Balbi 1353550a7375SFelipe Balbi if (!ep || !request || to_musb_request(request)->ep != musb_ep) 1354550a7375SFelipe Balbi return -EINVAL; 1355550a7375SFelipe Balbi 1356550a7375SFelipe Balbi spin_lock_irqsave(&musb->lock, flags); 1357550a7375SFelipe Balbi 1358550a7375SFelipe Balbi list_for_each_entry(r, &musb_ep->req_list, list) { 13594cbbf084SFelipe Balbi if (r == req) 1360550a7375SFelipe Balbi break; 1361550a7375SFelipe Balbi } 13624cbbf084SFelipe Balbi if (r != req) { 13635c8a86e1SFelipe Balbi dev_dbg(musb->controller, "request %p not queued to %s\n", request, ep->name); 1364550a7375SFelipe Balbi status = -EINVAL; 1365550a7375SFelipe Balbi goto done; 1366550a7375SFelipe Balbi } 1367550a7375SFelipe Balbi 1368550a7375SFelipe Balbi /* if the hardware doesn't have the request, easy ... */ 13693d5ad13eSFelipe Balbi if (musb_ep->req_list.next != &req->list || musb_ep->busy) 1370550a7375SFelipe Balbi musb_g_giveback(musb_ep, request, -ECONNRESET); 1371550a7375SFelipe Balbi 1372550a7375SFelipe Balbi /* ... else abort the dma transfer ... */ 1373550a7375SFelipe Balbi else if (is_dma_capable() && musb_ep->dma) { 1374550a7375SFelipe Balbi struct dma_controller *c = musb->dma_controller; 1375550a7375SFelipe Balbi 1376550a7375SFelipe Balbi musb_ep_select(musb->mregs, musb_ep->current_epnum); 1377550a7375SFelipe Balbi if (c->channel_abort) 1378550a7375SFelipe Balbi status = c->channel_abort(musb_ep->dma); 1379550a7375SFelipe Balbi else 1380550a7375SFelipe Balbi status = -EBUSY; 1381550a7375SFelipe Balbi if (status == 0) 1382550a7375SFelipe Balbi musb_g_giveback(musb_ep, request, -ECONNRESET); 1383550a7375SFelipe Balbi } else { 1384550a7375SFelipe Balbi /* NOTE: by sticking to easily tested hardware/driver states, 1385550a7375SFelipe Balbi * we leave counting of in-flight packets imprecise. 1386550a7375SFelipe Balbi */ 1387550a7375SFelipe Balbi musb_g_giveback(musb_ep, request, -ECONNRESET); 1388550a7375SFelipe Balbi } 1389550a7375SFelipe Balbi 1390550a7375SFelipe Balbi done: 1391550a7375SFelipe Balbi spin_unlock_irqrestore(&musb->lock, flags); 1392550a7375SFelipe Balbi return status; 1393550a7375SFelipe Balbi } 1394550a7375SFelipe Balbi 1395550a7375SFelipe Balbi /* 1396550a7375SFelipe Balbi * Set or clear the halt bit of an endpoint. A halted enpoint won't tx/rx any 1397550a7375SFelipe Balbi * data but will queue requests. 1398550a7375SFelipe Balbi * 1399550a7375SFelipe Balbi * exported to ep0 code 1400550a7375SFelipe Balbi */ 14011b6c3b0fSFelipe Balbi static int musb_gadget_set_halt(struct usb_ep *ep, int value) 1402550a7375SFelipe Balbi { 1403550a7375SFelipe Balbi struct musb_ep *musb_ep = to_musb_ep(ep); 1404550a7375SFelipe Balbi u8 epnum = musb_ep->current_epnum; 1405550a7375SFelipe Balbi struct musb *musb = musb_ep->musb; 1406550a7375SFelipe Balbi void __iomem *epio = musb->endpoints[epnum].regs; 1407550a7375SFelipe Balbi void __iomem *mbase; 1408550a7375SFelipe Balbi unsigned long flags; 1409550a7375SFelipe Balbi u16 csr; 1410cea83241SSergei Shtylyov struct musb_request *request; 1411550a7375SFelipe Balbi int status = 0; 1412550a7375SFelipe Balbi 1413550a7375SFelipe Balbi if (!ep) 1414550a7375SFelipe Balbi return -EINVAL; 1415550a7375SFelipe Balbi mbase = musb->mregs; 1416550a7375SFelipe Balbi 1417550a7375SFelipe Balbi spin_lock_irqsave(&musb->lock, flags); 1418550a7375SFelipe Balbi 1419550a7375SFelipe Balbi if ((USB_ENDPOINT_XFER_ISOC == musb_ep->type)) { 1420550a7375SFelipe Balbi status = -EINVAL; 1421550a7375SFelipe Balbi goto done; 1422550a7375SFelipe Balbi } 1423550a7375SFelipe Balbi 1424550a7375SFelipe Balbi musb_ep_select(mbase, epnum); 1425550a7375SFelipe Balbi 1426ad1adb89SFelipe Balbi request = next_request(musb_ep); 1427cea83241SSergei Shtylyov if (value) { 1428cea83241SSergei Shtylyov if (request) { 14295c8a86e1SFelipe Balbi dev_dbg(musb->controller, "request in progress, cannot halt %s\n", 1430cea83241SSergei Shtylyov ep->name); 1431cea83241SSergei Shtylyov status = -EAGAIN; 1432cea83241SSergei Shtylyov goto done; 1433cea83241SSergei Shtylyov } 1434cea83241SSergei Shtylyov /* Cannot portably stall with non-empty FIFO */ 1435cea83241SSergei Shtylyov if (musb_ep->is_in) { 1436550a7375SFelipe Balbi csr = musb_readw(epio, MUSB_TXCSR); 1437550a7375SFelipe Balbi if (csr & MUSB_TXCSR_FIFONOTEMPTY) { 14385c8a86e1SFelipe Balbi dev_dbg(musb->controller, "FIFO busy, cannot halt %s\n", ep->name); 1439cea83241SSergei Shtylyov status = -EAGAIN; 1440cea83241SSergei Shtylyov goto done; 1441550a7375SFelipe Balbi } 1442cea83241SSergei Shtylyov } 144347e97605SSergei Shtylyov } else 144447e97605SSergei Shtylyov musb_ep->wedged = 0; 1445550a7375SFelipe Balbi 1446550a7375SFelipe Balbi /* set/clear the stall and toggle bits */ 14475c8a86e1SFelipe Balbi dev_dbg(musb->controller, "%s: %s stall\n", ep->name, value ? "set" : "clear"); 1448550a7375SFelipe Balbi if (musb_ep->is_in) { 1449550a7375SFelipe Balbi csr = musb_readw(epio, MUSB_TXCSR); 1450550a7375SFelipe Balbi csr |= MUSB_TXCSR_P_WZC_BITS 1451550a7375SFelipe Balbi | MUSB_TXCSR_CLRDATATOG; 1452550a7375SFelipe Balbi if (value) 1453550a7375SFelipe Balbi csr |= MUSB_TXCSR_P_SENDSTALL; 1454550a7375SFelipe Balbi else 1455550a7375SFelipe Balbi csr &= ~(MUSB_TXCSR_P_SENDSTALL 1456550a7375SFelipe Balbi | MUSB_TXCSR_P_SENTSTALL); 1457550a7375SFelipe Balbi csr &= ~MUSB_TXCSR_TXPKTRDY; 1458550a7375SFelipe Balbi musb_writew(epio, MUSB_TXCSR, csr); 1459550a7375SFelipe Balbi } else { 1460550a7375SFelipe Balbi csr = musb_readw(epio, MUSB_RXCSR); 1461550a7375SFelipe Balbi csr |= MUSB_RXCSR_P_WZC_BITS 1462550a7375SFelipe Balbi | MUSB_RXCSR_FLUSHFIFO 1463550a7375SFelipe Balbi | MUSB_RXCSR_CLRDATATOG; 1464550a7375SFelipe Balbi if (value) 1465550a7375SFelipe Balbi csr |= MUSB_RXCSR_P_SENDSTALL; 1466550a7375SFelipe Balbi else 1467550a7375SFelipe Balbi csr &= ~(MUSB_RXCSR_P_SENDSTALL 1468550a7375SFelipe Balbi | MUSB_RXCSR_P_SENTSTALL); 1469550a7375SFelipe Balbi musb_writew(epio, MUSB_RXCSR, csr); 1470550a7375SFelipe Balbi } 1471550a7375SFelipe Balbi 1472550a7375SFelipe Balbi /* maybe start the first request in the queue */ 1473550a7375SFelipe Balbi if (!musb_ep->busy && !value && request) { 14745c8a86e1SFelipe Balbi dev_dbg(musb->controller, "restarting the request\n"); 1475550a7375SFelipe Balbi musb_ep_restart(musb, request); 1476550a7375SFelipe Balbi } 1477550a7375SFelipe Balbi 1478cea83241SSergei Shtylyov done: 1479550a7375SFelipe Balbi spin_unlock_irqrestore(&musb->lock, flags); 1480550a7375SFelipe Balbi return status; 1481550a7375SFelipe Balbi } 1482550a7375SFelipe Balbi 148347e97605SSergei Shtylyov /* 148447e97605SSergei Shtylyov * Sets the halt feature with the clear requests ignored 148547e97605SSergei Shtylyov */ 14861b6c3b0fSFelipe Balbi static int musb_gadget_set_wedge(struct usb_ep *ep) 148747e97605SSergei Shtylyov { 148847e97605SSergei Shtylyov struct musb_ep *musb_ep = to_musb_ep(ep); 148947e97605SSergei Shtylyov 149047e97605SSergei Shtylyov if (!ep) 149147e97605SSergei Shtylyov return -EINVAL; 149247e97605SSergei Shtylyov 149347e97605SSergei Shtylyov musb_ep->wedged = 1; 149447e97605SSergei Shtylyov 149547e97605SSergei Shtylyov return usb_ep_set_halt(ep); 149647e97605SSergei Shtylyov } 149747e97605SSergei Shtylyov 1498550a7375SFelipe Balbi static int musb_gadget_fifo_status(struct usb_ep *ep) 1499550a7375SFelipe Balbi { 1500550a7375SFelipe Balbi struct musb_ep *musb_ep = to_musb_ep(ep); 1501550a7375SFelipe Balbi void __iomem *epio = musb_ep->hw_ep->regs; 1502550a7375SFelipe Balbi int retval = -EINVAL; 1503550a7375SFelipe Balbi 1504550a7375SFelipe Balbi if (musb_ep->desc && !musb_ep->is_in) { 1505550a7375SFelipe Balbi struct musb *musb = musb_ep->musb; 1506550a7375SFelipe Balbi int epnum = musb_ep->current_epnum; 1507550a7375SFelipe Balbi void __iomem *mbase = musb->mregs; 1508550a7375SFelipe Balbi unsigned long flags; 1509550a7375SFelipe Balbi 1510550a7375SFelipe Balbi spin_lock_irqsave(&musb->lock, flags); 1511550a7375SFelipe Balbi 1512550a7375SFelipe Balbi musb_ep_select(mbase, epnum); 1513550a7375SFelipe Balbi /* FIXME return zero unless RXPKTRDY is set */ 1514550a7375SFelipe Balbi retval = musb_readw(epio, MUSB_RXCOUNT); 1515550a7375SFelipe Balbi 1516550a7375SFelipe Balbi spin_unlock_irqrestore(&musb->lock, flags); 1517550a7375SFelipe Balbi } 1518550a7375SFelipe Balbi return retval; 1519550a7375SFelipe Balbi } 1520550a7375SFelipe Balbi 1521550a7375SFelipe Balbi static void musb_gadget_fifo_flush(struct usb_ep *ep) 1522550a7375SFelipe Balbi { 1523550a7375SFelipe Balbi struct musb_ep *musb_ep = to_musb_ep(ep); 1524550a7375SFelipe Balbi struct musb *musb = musb_ep->musb; 1525550a7375SFelipe Balbi u8 epnum = musb_ep->current_epnum; 1526550a7375SFelipe Balbi void __iomem *epio = musb->endpoints[epnum].regs; 1527550a7375SFelipe Balbi void __iomem *mbase; 1528550a7375SFelipe Balbi unsigned long flags; 1529550a7375SFelipe Balbi u16 csr, int_txe; 1530550a7375SFelipe Balbi 1531550a7375SFelipe Balbi mbase = musb->mregs; 1532550a7375SFelipe Balbi 1533550a7375SFelipe Balbi spin_lock_irqsave(&musb->lock, flags); 1534550a7375SFelipe Balbi musb_ep_select(mbase, (u8) epnum); 1535550a7375SFelipe Balbi 1536550a7375SFelipe Balbi /* disable interrupts */ 1537550a7375SFelipe Balbi int_txe = musb_readw(mbase, MUSB_INTRTXE); 1538550a7375SFelipe Balbi musb_writew(mbase, MUSB_INTRTXE, int_txe & ~(1 << epnum)); 1539550a7375SFelipe Balbi 1540550a7375SFelipe Balbi if (musb_ep->is_in) { 1541550a7375SFelipe Balbi csr = musb_readw(epio, MUSB_TXCSR); 1542550a7375SFelipe Balbi if (csr & MUSB_TXCSR_FIFONOTEMPTY) { 1543550a7375SFelipe Balbi csr |= MUSB_TXCSR_FLUSHFIFO | MUSB_TXCSR_P_WZC_BITS; 15444858f06eSYauheni Kaliuta /* 15454858f06eSYauheni Kaliuta * Setting both TXPKTRDY and FLUSHFIFO makes controller 15464858f06eSYauheni Kaliuta * to interrupt current FIFO loading, but not flushing 15474858f06eSYauheni Kaliuta * the already loaded ones. 15484858f06eSYauheni Kaliuta */ 15494858f06eSYauheni Kaliuta csr &= ~MUSB_TXCSR_TXPKTRDY; 1550550a7375SFelipe Balbi musb_writew(epio, MUSB_TXCSR, csr); 1551550a7375SFelipe Balbi /* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */ 1552550a7375SFelipe Balbi musb_writew(epio, MUSB_TXCSR, csr); 1553550a7375SFelipe Balbi } 1554550a7375SFelipe Balbi } else { 1555550a7375SFelipe Balbi csr = musb_readw(epio, MUSB_RXCSR); 1556550a7375SFelipe Balbi csr |= MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_P_WZC_BITS; 1557550a7375SFelipe Balbi musb_writew(epio, MUSB_RXCSR, csr); 1558550a7375SFelipe Balbi musb_writew(epio, MUSB_RXCSR, csr); 1559550a7375SFelipe Balbi } 1560550a7375SFelipe Balbi 1561550a7375SFelipe Balbi /* re-enable interrupt */ 1562550a7375SFelipe Balbi musb_writew(mbase, MUSB_INTRTXE, int_txe); 1563550a7375SFelipe Balbi spin_unlock_irqrestore(&musb->lock, flags); 1564550a7375SFelipe Balbi } 1565550a7375SFelipe Balbi 1566550a7375SFelipe Balbi static const struct usb_ep_ops musb_ep_ops = { 1567550a7375SFelipe Balbi .enable = musb_gadget_enable, 1568550a7375SFelipe Balbi .disable = musb_gadget_disable, 1569550a7375SFelipe Balbi .alloc_request = musb_alloc_request, 1570550a7375SFelipe Balbi .free_request = musb_free_request, 1571550a7375SFelipe Balbi .queue = musb_gadget_queue, 1572550a7375SFelipe Balbi .dequeue = musb_gadget_dequeue, 1573550a7375SFelipe Balbi .set_halt = musb_gadget_set_halt, 157447e97605SSergei Shtylyov .set_wedge = musb_gadget_set_wedge, 1575550a7375SFelipe Balbi .fifo_status = musb_gadget_fifo_status, 1576550a7375SFelipe Balbi .fifo_flush = musb_gadget_fifo_flush 1577550a7375SFelipe Balbi }; 1578550a7375SFelipe Balbi 1579550a7375SFelipe Balbi /* ----------------------------------------------------------------------- */ 1580550a7375SFelipe Balbi 1581550a7375SFelipe Balbi static int musb_gadget_get_frame(struct usb_gadget *gadget) 1582550a7375SFelipe Balbi { 1583550a7375SFelipe Balbi struct musb *musb = gadget_to_musb(gadget); 1584550a7375SFelipe Balbi 1585550a7375SFelipe Balbi return (int)musb_readw(musb->mregs, MUSB_FRAME); 1586550a7375SFelipe Balbi } 1587550a7375SFelipe Balbi 1588550a7375SFelipe Balbi static int musb_gadget_wakeup(struct usb_gadget *gadget) 1589550a7375SFelipe Balbi { 1590550a7375SFelipe Balbi struct musb *musb = gadget_to_musb(gadget); 1591550a7375SFelipe Balbi void __iomem *mregs = musb->mregs; 1592550a7375SFelipe Balbi unsigned long flags; 1593550a7375SFelipe Balbi int status = -EINVAL; 1594550a7375SFelipe Balbi u8 power, devctl; 1595550a7375SFelipe Balbi int retries; 1596550a7375SFelipe Balbi 1597550a7375SFelipe Balbi spin_lock_irqsave(&musb->lock, flags); 1598550a7375SFelipe Balbi 159984e250ffSDavid Brownell switch (musb->xceiv->state) { 1600550a7375SFelipe Balbi case OTG_STATE_B_PERIPHERAL: 1601550a7375SFelipe Balbi /* NOTE: OTG state machine doesn't include B_SUSPENDED; 1602550a7375SFelipe Balbi * that's part of the standard usb 1.1 state machine, and 1603550a7375SFelipe Balbi * doesn't affect OTG transitions. 1604550a7375SFelipe Balbi */ 1605550a7375SFelipe Balbi if (musb->may_wakeup && musb->is_suspended) 1606550a7375SFelipe Balbi break; 1607550a7375SFelipe Balbi goto done; 1608550a7375SFelipe Balbi case OTG_STATE_B_IDLE: 1609550a7375SFelipe Balbi /* Start SRP ... OTG not required. */ 1610550a7375SFelipe Balbi devctl = musb_readb(mregs, MUSB_DEVCTL); 16115c8a86e1SFelipe Balbi dev_dbg(musb->controller, "Sending SRP: devctl: %02x\n", devctl); 1612550a7375SFelipe Balbi devctl |= MUSB_DEVCTL_SESSION; 1613550a7375SFelipe Balbi musb_writeb(mregs, MUSB_DEVCTL, devctl); 1614550a7375SFelipe Balbi devctl = musb_readb(mregs, MUSB_DEVCTL); 1615550a7375SFelipe Balbi retries = 100; 1616550a7375SFelipe Balbi while (!(devctl & MUSB_DEVCTL_SESSION)) { 1617550a7375SFelipe Balbi devctl = musb_readb(mregs, MUSB_DEVCTL); 1618550a7375SFelipe Balbi if (retries-- < 1) 1619550a7375SFelipe Balbi break; 1620550a7375SFelipe Balbi } 1621550a7375SFelipe Balbi retries = 10000; 1622550a7375SFelipe Balbi while (devctl & MUSB_DEVCTL_SESSION) { 1623550a7375SFelipe Balbi devctl = musb_readb(mregs, MUSB_DEVCTL); 1624550a7375SFelipe Balbi if (retries-- < 1) 1625550a7375SFelipe Balbi break; 1626550a7375SFelipe Balbi } 1627550a7375SFelipe Balbi 16288620543eSHema HK spin_unlock_irqrestore(&musb->lock, flags); 16298620543eSHema HK otg_start_srp(musb->xceiv); 16308620543eSHema HK spin_lock_irqsave(&musb->lock, flags); 16318620543eSHema HK 1632550a7375SFelipe Balbi /* Block idling for at least 1s */ 1633550a7375SFelipe Balbi musb_platform_try_idle(musb, 1634550a7375SFelipe Balbi jiffies + msecs_to_jiffies(1 * HZ)); 1635550a7375SFelipe Balbi 1636550a7375SFelipe Balbi status = 0; 1637550a7375SFelipe Balbi goto done; 1638550a7375SFelipe Balbi default: 16395c8a86e1SFelipe Balbi dev_dbg(musb->controller, "Unhandled wake: %s\n", 16403df00453SAnatolij Gustschin otg_state_string(musb->xceiv->state)); 1641550a7375SFelipe Balbi goto done; 1642550a7375SFelipe Balbi } 1643550a7375SFelipe Balbi 1644550a7375SFelipe Balbi status = 0; 1645550a7375SFelipe Balbi 1646550a7375SFelipe Balbi power = musb_readb(mregs, MUSB_POWER); 1647550a7375SFelipe Balbi power |= MUSB_POWER_RESUME; 1648550a7375SFelipe Balbi musb_writeb(mregs, MUSB_POWER, power); 16495c8a86e1SFelipe Balbi dev_dbg(musb->controller, "issue wakeup\n"); 1650550a7375SFelipe Balbi 1651550a7375SFelipe Balbi /* FIXME do this next chunk in a timer callback, no udelay */ 1652550a7375SFelipe Balbi mdelay(2); 1653550a7375SFelipe Balbi 1654550a7375SFelipe Balbi power = musb_readb(mregs, MUSB_POWER); 1655550a7375SFelipe Balbi power &= ~MUSB_POWER_RESUME; 1656550a7375SFelipe Balbi musb_writeb(mregs, MUSB_POWER, power); 1657550a7375SFelipe Balbi done: 1658550a7375SFelipe Balbi spin_unlock_irqrestore(&musb->lock, flags); 1659550a7375SFelipe Balbi return status; 1660550a7375SFelipe Balbi } 1661550a7375SFelipe Balbi 1662550a7375SFelipe Balbi static int 1663550a7375SFelipe Balbi musb_gadget_set_self_powered(struct usb_gadget *gadget, int is_selfpowered) 1664550a7375SFelipe Balbi { 1665550a7375SFelipe Balbi struct musb *musb = gadget_to_musb(gadget); 1666550a7375SFelipe Balbi 1667550a7375SFelipe Balbi musb->is_self_powered = !!is_selfpowered; 1668550a7375SFelipe Balbi return 0; 1669550a7375SFelipe Balbi } 1670550a7375SFelipe Balbi 1671550a7375SFelipe Balbi static void musb_pullup(struct musb *musb, int is_on) 1672550a7375SFelipe Balbi { 1673550a7375SFelipe Balbi u8 power; 1674550a7375SFelipe Balbi 1675550a7375SFelipe Balbi power = musb_readb(musb->mregs, MUSB_POWER); 1676550a7375SFelipe Balbi if (is_on) 1677550a7375SFelipe Balbi power |= MUSB_POWER_SOFTCONN; 1678550a7375SFelipe Balbi else 1679550a7375SFelipe Balbi power &= ~MUSB_POWER_SOFTCONN; 1680550a7375SFelipe Balbi 1681550a7375SFelipe Balbi /* FIXME if on, HdrcStart; if off, HdrcStop */ 1682550a7375SFelipe Balbi 1683e71eb392SSebastian Andrzej Siewior dev_dbg(musb->controller, "gadget D+ pullup %s\n", 1684e71eb392SSebastian Andrzej Siewior is_on ? "on" : "off"); 1685550a7375SFelipe Balbi musb_writeb(musb->mregs, MUSB_POWER, power); 1686550a7375SFelipe Balbi } 1687550a7375SFelipe Balbi 1688550a7375SFelipe Balbi #if 0 1689550a7375SFelipe Balbi static int musb_gadget_vbus_session(struct usb_gadget *gadget, int is_active) 1690550a7375SFelipe Balbi { 16915c8a86e1SFelipe Balbi dev_dbg(musb->controller, "<= %s =>\n", __func__); 1692550a7375SFelipe Balbi 1693550a7375SFelipe Balbi /* 1694550a7375SFelipe Balbi * FIXME iff driver's softconnect flag is set (as it is during probe, 1695550a7375SFelipe Balbi * though that can clear it), just musb_pullup(). 1696550a7375SFelipe Balbi */ 1697550a7375SFelipe Balbi 1698550a7375SFelipe Balbi return -EINVAL; 1699550a7375SFelipe Balbi } 1700550a7375SFelipe Balbi #endif 1701550a7375SFelipe Balbi 1702550a7375SFelipe Balbi static int musb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA) 1703550a7375SFelipe Balbi { 1704550a7375SFelipe Balbi struct musb *musb = gadget_to_musb(gadget); 1705550a7375SFelipe Balbi 170684e250ffSDavid Brownell if (!musb->xceiv->set_power) 1707550a7375SFelipe Balbi return -EOPNOTSUPP; 170884e250ffSDavid Brownell return otg_set_power(musb->xceiv, mA); 1709550a7375SFelipe Balbi } 1710550a7375SFelipe Balbi 1711550a7375SFelipe Balbi static int musb_gadget_pullup(struct usb_gadget *gadget, int is_on) 1712550a7375SFelipe Balbi { 1713550a7375SFelipe Balbi struct musb *musb = gadget_to_musb(gadget); 1714550a7375SFelipe Balbi unsigned long flags; 1715550a7375SFelipe Balbi 1716550a7375SFelipe Balbi is_on = !!is_on; 1717550a7375SFelipe Balbi 171893e098a8SJohn Stultz pm_runtime_get_sync(musb->controller); 171993e098a8SJohn Stultz 1720550a7375SFelipe Balbi /* NOTE: this assumes we are sensing vbus; we'd rather 1721550a7375SFelipe Balbi * not pullup unless the B-session is active. 1722550a7375SFelipe Balbi */ 1723550a7375SFelipe Balbi spin_lock_irqsave(&musb->lock, flags); 1724550a7375SFelipe Balbi if (is_on != musb->softconnect) { 1725550a7375SFelipe Balbi musb->softconnect = is_on; 1726550a7375SFelipe Balbi musb_pullup(musb, is_on); 1727550a7375SFelipe Balbi } 1728550a7375SFelipe Balbi spin_unlock_irqrestore(&musb->lock, flags); 172993e098a8SJohn Stultz 173093e098a8SJohn Stultz pm_runtime_put(musb->controller); 173193e098a8SJohn Stultz 1732550a7375SFelipe Balbi return 0; 1733550a7375SFelipe Balbi } 1734550a7375SFelipe Balbi 1735e71eb392SSebastian Andrzej Siewior static int musb_gadget_start(struct usb_gadget *g, 1736e71eb392SSebastian Andrzej Siewior struct usb_gadget_driver *driver); 1737e71eb392SSebastian Andrzej Siewior static int musb_gadget_stop(struct usb_gadget *g, 1738e71eb392SSebastian Andrzej Siewior struct usb_gadget_driver *driver); 17390f91349bSSebastian Andrzej Siewior 1740550a7375SFelipe Balbi static const struct usb_gadget_ops musb_gadget_operations = { 1741550a7375SFelipe Balbi .get_frame = musb_gadget_get_frame, 1742550a7375SFelipe Balbi .wakeup = musb_gadget_wakeup, 1743550a7375SFelipe Balbi .set_selfpowered = musb_gadget_set_self_powered, 1744550a7375SFelipe Balbi /* .vbus_session = musb_gadget_vbus_session, */ 1745550a7375SFelipe Balbi .vbus_draw = musb_gadget_vbus_draw, 1746550a7375SFelipe Balbi .pullup = musb_gadget_pullup, 1747e71eb392SSebastian Andrzej Siewior .udc_start = musb_gadget_start, 1748e71eb392SSebastian Andrzej Siewior .udc_stop = musb_gadget_stop, 1749550a7375SFelipe Balbi }; 1750550a7375SFelipe Balbi 1751550a7375SFelipe Balbi /* ----------------------------------------------------------------------- */ 1752550a7375SFelipe Balbi 1753550a7375SFelipe Balbi /* Registration */ 1754550a7375SFelipe Balbi 1755550a7375SFelipe Balbi /* Only this registration code "knows" the rule (from USB standards) 1756550a7375SFelipe Balbi * about there being only one external upstream port. It assumes 1757550a7375SFelipe Balbi * all peripheral ports are external... 1758550a7375SFelipe Balbi */ 1759550a7375SFelipe Balbi 1760550a7375SFelipe Balbi static void musb_gadget_release(struct device *dev) 1761550a7375SFelipe Balbi { 1762550a7375SFelipe Balbi /* kref_put(WHAT) */ 1763550a7375SFelipe Balbi dev_dbg(dev, "%s\n", __func__); 1764550a7375SFelipe Balbi } 1765550a7375SFelipe Balbi 1766550a7375SFelipe Balbi 1767550a7375SFelipe Balbi static void __init 1768550a7375SFelipe Balbi init_peripheral_ep(struct musb *musb, struct musb_ep *ep, u8 epnum, int is_in) 1769550a7375SFelipe Balbi { 1770550a7375SFelipe Balbi struct musb_hw_ep *hw_ep = musb->endpoints + epnum; 1771550a7375SFelipe Balbi 1772550a7375SFelipe Balbi memset(ep, 0, sizeof *ep); 1773550a7375SFelipe Balbi 1774550a7375SFelipe Balbi ep->current_epnum = epnum; 1775550a7375SFelipe Balbi ep->musb = musb; 1776550a7375SFelipe Balbi ep->hw_ep = hw_ep; 1777550a7375SFelipe Balbi ep->is_in = is_in; 1778550a7375SFelipe Balbi 1779550a7375SFelipe Balbi INIT_LIST_HEAD(&ep->req_list); 1780550a7375SFelipe Balbi 1781550a7375SFelipe Balbi sprintf(ep->name, "ep%d%s", epnum, 1782550a7375SFelipe Balbi (!epnum || hw_ep->is_shared_fifo) ? "" : ( 1783550a7375SFelipe Balbi is_in ? "in" : "out")); 1784550a7375SFelipe Balbi ep->end_point.name = ep->name; 1785550a7375SFelipe Balbi INIT_LIST_HEAD(&ep->end_point.ep_list); 1786550a7375SFelipe Balbi if (!epnum) { 1787550a7375SFelipe Balbi ep->end_point.maxpacket = 64; 1788550a7375SFelipe Balbi ep->end_point.ops = &musb_g_ep0_ops; 1789550a7375SFelipe Balbi musb->g.ep0 = &ep->end_point; 1790550a7375SFelipe Balbi } else { 1791550a7375SFelipe Balbi if (is_in) 1792550a7375SFelipe Balbi ep->end_point.maxpacket = hw_ep->max_packet_sz_tx; 1793550a7375SFelipe Balbi else 1794550a7375SFelipe Balbi ep->end_point.maxpacket = hw_ep->max_packet_sz_rx; 1795550a7375SFelipe Balbi ep->end_point.ops = &musb_ep_ops; 1796550a7375SFelipe Balbi list_add_tail(&ep->end_point.ep_list, &musb->g.ep_list); 1797550a7375SFelipe Balbi } 1798550a7375SFelipe Balbi } 1799550a7375SFelipe Balbi 1800550a7375SFelipe Balbi /* 1801550a7375SFelipe Balbi * Initialize the endpoints exposed to peripheral drivers, with backlinks 1802550a7375SFelipe Balbi * to the rest of the driver state. 1803550a7375SFelipe Balbi */ 1804550a7375SFelipe Balbi static inline void __init musb_g_init_endpoints(struct musb *musb) 1805550a7375SFelipe Balbi { 1806550a7375SFelipe Balbi u8 epnum; 1807550a7375SFelipe Balbi struct musb_hw_ep *hw_ep; 1808550a7375SFelipe Balbi unsigned count = 0; 1809550a7375SFelipe Balbi 1810b595076aSUwe Kleine-König /* initialize endpoint list just once */ 1811550a7375SFelipe Balbi INIT_LIST_HEAD(&(musb->g.ep_list)); 1812550a7375SFelipe Balbi 1813550a7375SFelipe Balbi for (epnum = 0, hw_ep = musb->endpoints; 1814550a7375SFelipe Balbi epnum < musb->nr_endpoints; 1815550a7375SFelipe Balbi epnum++, hw_ep++) { 1816550a7375SFelipe Balbi if (hw_ep->is_shared_fifo /* || !epnum */) { 1817550a7375SFelipe Balbi init_peripheral_ep(musb, &hw_ep->ep_in, epnum, 0); 1818550a7375SFelipe Balbi count++; 1819550a7375SFelipe Balbi } else { 1820550a7375SFelipe Balbi if (hw_ep->max_packet_sz_tx) { 1821550a7375SFelipe Balbi init_peripheral_ep(musb, &hw_ep->ep_in, 1822550a7375SFelipe Balbi epnum, 1); 1823550a7375SFelipe Balbi count++; 1824550a7375SFelipe Balbi } 1825550a7375SFelipe Balbi if (hw_ep->max_packet_sz_rx) { 1826550a7375SFelipe Balbi init_peripheral_ep(musb, &hw_ep->ep_out, 1827550a7375SFelipe Balbi epnum, 0); 1828550a7375SFelipe Balbi count++; 1829550a7375SFelipe Balbi } 1830550a7375SFelipe Balbi } 1831550a7375SFelipe Balbi } 1832550a7375SFelipe Balbi } 1833550a7375SFelipe Balbi 1834550a7375SFelipe Balbi /* called once during driver setup to initialize and link into 1835550a7375SFelipe Balbi * the driver model; memory is zeroed. 1836550a7375SFelipe Balbi */ 1837550a7375SFelipe Balbi int __init musb_gadget_setup(struct musb *musb) 1838550a7375SFelipe Balbi { 1839550a7375SFelipe Balbi int status; 1840550a7375SFelipe Balbi 1841550a7375SFelipe Balbi /* REVISIT minor race: if (erroneously) setting up two 1842550a7375SFelipe Balbi * musb peripherals at the same time, only the bus lock 1843550a7375SFelipe Balbi * is probably held. 1844550a7375SFelipe Balbi */ 1845550a7375SFelipe Balbi 1846550a7375SFelipe Balbi musb->g.ops = &musb_gadget_operations; 1847d327ab5bSMichal Nazarewicz musb->g.max_speed = USB_SPEED_HIGH; 1848550a7375SFelipe Balbi musb->g.speed = USB_SPEED_UNKNOWN; 1849550a7375SFelipe Balbi 1850550a7375SFelipe Balbi /* this "gadget" abstracts/virtualizes the controller */ 1851427c4f33SKay Sievers dev_set_name(&musb->g.dev, "gadget"); 1852550a7375SFelipe Balbi musb->g.dev.parent = musb->controller; 1853550a7375SFelipe Balbi musb->g.dev.dma_mask = musb->controller->dma_mask; 1854550a7375SFelipe Balbi musb->g.dev.release = musb_gadget_release; 1855550a7375SFelipe Balbi musb->g.name = musb_driver_name; 1856550a7375SFelipe Balbi 1857550a7375SFelipe Balbi if (is_otg_enabled(musb)) 1858550a7375SFelipe Balbi musb->g.is_otg = 1; 1859550a7375SFelipe Balbi 1860550a7375SFelipe Balbi musb_g_init_endpoints(musb); 1861550a7375SFelipe Balbi 1862550a7375SFelipe Balbi musb->is_active = 0; 1863550a7375SFelipe Balbi musb_platform_try_idle(musb, 0); 1864550a7375SFelipe Balbi 1865550a7375SFelipe Balbi status = device_register(&musb->g.dev); 1866e2c34045SRahul Ruikar if (status != 0) { 1867e2c34045SRahul Ruikar put_device(&musb->g.dev); 18680f91349bSSebastian Andrzej Siewior return status; 1869e2c34045SRahul Ruikar } 18700f91349bSSebastian Andrzej Siewior status = usb_add_gadget_udc(musb->controller, &musb->g); 18710f91349bSSebastian Andrzej Siewior if (status) 18720f91349bSSebastian Andrzej Siewior goto err; 18730f91349bSSebastian Andrzej Siewior 18740f91349bSSebastian Andrzej Siewior return 0; 18750f91349bSSebastian Andrzej Siewior err: 18766193d699SSebastian Andrzej Siewior musb->g.dev.parent = NULL; 18770f91349bSSebastian Andrzej Siewior device_unregister(&musb->g.dev); 1878550a7375SFelipe Balbi return status; 1879550a7375SFelipe Balbi } 1880550a7375SFelipe Balbi 1881550a7375SFelipe Balbi void musb_gadget_cleanup(struct musb *musb) 1882550a7375SFelipe Balbi { 18830f91349bSSebastian Andrzej Siewior usb_del_gadget_udc(&musb->g); 18846193d699SSebastian Andrzej Siewior if (musb->g.dev.parent) 1885550a7375SFelipe Balbi device_unregister(&musb->g.dev); 1886550a7375SFelipe Balbi } 1887550a7375SFelipe Balbi 1888550a7375SFelipe Balbi /* 1889550a7375SFelipe Balbi * Register the gadget driver. Used by gadget drivers when 1890550a7375SFelipe Balbi * registering themselves with the controller. 1891550a7375SFelipe Balbi * 1892550a7375SFelipe Balbi * -EINVAL something went wrong (not driver) 1893550a7375SFelipe Balbi * -EBUSY another gadget is already using the controller 1894b595076aSUwe Kleine-König * -ENOMEM no memory to perform the operation 1895550a7375SFelipe Balbi * 1896550a7375SFelipe Balbi * @param driver the gadget driver 1897550a7375SFelipe Balbi * @return <0 if error, 0 if everything is fine 1898550a7375SFelipe Balbi */ 1899e71eb392SSebastian Andrzej Siewior static int musb_gadget_start(struct usb_gadget *g, 1900e71eb392SSebastian Andrzej Siewior struct usb_gadget_driver *driver) 1901550a7375SFelipe Balbi { 1902e71eb392SSebastian Andrzej Siewior struct musb *musb = gadget_to_musb(g); 190363eed2b5SFelipe Balbi unsigned long flags; 190463eed2b5SFelipe Balbi int retval = -EINVAL; 1905550a7375SFelipe Balbi 1906*7177aed4SMichal Nazarewicz if (driver->max_speed < USB_SPEED_HIGH) 190763eed2b5SFelipe Balbi goto err0; 1908550a7375SFelipe Balbi 19097acc6197SHema HK pm_runtime_get_sync(musb->controller); 19107acc6197SHema HK 19115c8a86e1SFelipe Balbi dev_dbg(musb->controller, "registering driver %s\n", driver->function); 1912550a7375SFelipe Balbi 1913e71eb392SSebastian Andrzej Siewior musb->softconnect = 0; 1914550a7375SFelipe Balbi musb->gadget_driver = driver; 1915550a7375SFelipe Balbi 1916550a7375SFelipe Balbi spin_lock_irqsave(&musb->lock, flags); 1917e71eb392SSebastian Andrzej Siewior musb->is_active = 1; 1918550a7375SFelipe Balbi 191984e250ffSDavid Brownell otg_set_peripheral(musb->xceiv, &musb->g); 1920d4c433feSArnaud Mandy musb->xceiv->state = OTG_STATE_B_IDLE; 1921550a7375SFelipe Balbi 192263eed2b5SFelipe Balbi /* 192363eed2b5SFelipe Balbi * FIXME this ignores the softconnect flag. Drivers are 1924550a7375SFelipe Balbi * allowed hold the peripheral inactive until for example 1925550a7375SFelipe Balbi * userspace hooks up printer hardware or DSP codecs, so 1926550a7375SFelipe Balbi * hosts only see fully functional devices. 1927550a7375SFelipe Balbi */ 1928550a7375SFelipe Balbi 1929550a7375SFelipe Balbi if (!is_otg_enabled(musb)) 1930550a7375SFelipe Balbi musb_start(musb); 1931550a7375SFelipe Balbi 1932550a7375SFelipe Balbi spin_unlock_irqrestore(&musb->lock, flags); 1933550a7375SFelipe Balbi 1934550a7375SFelipe Balbi if (is_otg_enabled(musb)) { 193507a8cdd2SAnand Gadiyar struct usb_hcd *hcd = musb_to_hcd(musb); 193607a8cdd2SAnand Gadiyar 19375c8a86e1SFelipe Balbi dev_dbg(musb->controller, "OTG startup...\n"); 1938550a7375SFelipe Balbi 1939550a7375SFelipe Balbi /* REVISIT: funcall to other code, which also 1940550a7375SFelipe Balbi * handles power budgeting ... this way also 1941550a7375SFelipe Balbi * ensures HdrcStart is indirectly called. 1942550a7375SFelipe Balbi */ 1943550a7375SFelipe Balbi retval = usb_add_hcd(musb_to_hcd(musb), -1, 0); 1944550a7375SFelipe Balbi if (retval < 0) { 19455c8a86e1SFelipe Balbi dev_dbg(musb->controller, "add_hcd failed, %d\n", retval); 194663eed2b5SFelipe Balbi goto err2; 19475f1e8ce7SHema HK } 1948002eda13SHema HK 1949002eda13SHema HK if ((musb->xceiv->last_event == USB_EVENT_ID) 1950002eda13SHema HK && musb->xceiv->set_vbus) 1951002eda13SHema HK otg_set_vbus(musb->xceiv, 1); 1952550a7375SFelipe Balbi 195363eed2b5SFelipe Balbi hcd->self.uses_pio_for_control = 1; 1954cdefce16SJarkko Nikula } 19557acc6197SHema HK if (musb->xceiv->last_event == USB_EVENT_NONE) 19567acc6197SHema HK pm_runtime_put(musb->controller); 19577acc6197SHema HK 195863eed2b5SFelipe Balbi return 0; 195963eed2b5SFelipe Balbi 196063eed2b5SFelipe Balbi err2: 196163eed2b5SFelipe Balbi if (!is_otg_enabled(musb)) 196263eed2b5SFelipe Balbi musb_stop(musb); 196363eed2b5SFelipe Balbi err0: 1964550a7375SFelipe Balbi return retval; 1965550a7375SFelipe Balbi } 1966550a7375SFelipe Balbi 1967550a7375SFelipe Balbi static void stop_activity(struct musb *musb, struct usb_gadget_driver *driver) 1968550a7375SFelipe Balbi { 1969550a7375SFelipe Balbi int i; 1970550a7375SFelipe Balbi struct musb_hw_ep *hw_ep; 1971550a7375SFelipe Balbi 1972550a7375SFelipe Balbi /* don't disconnect if it's not connected */ 1973550a7375SFelipe Balbi if (musb->g.speed == USB_SPEED_UNKNOWN) 1974550a7375SFelipe Balbi driver = NULL; 1975550a7375SFelipe Balbi else 1976550a7375SFelipe Balbi musb->g.speed = USB_SPEED_UNKNOWN; 1977550a7375SFelipe Balbi 1978550a7375SFelipe Balbi /* deactivate the hardware */ 1979550a7375SFelipe Balbi if (musb->softconnect) { 1980550a7375SFelipe Balbi musb->softconnect = 0; 1981550a7375SFelipe Balbi musb_pullup(musb, 0); 1982550a7375SFelipe Balbi } 1983550a7375SFelipe Balbi musb_stop(musb); 1984550a7375SFelipe Balbi 1985550a7375SFelipe Balbi /* killing any outstanding requests will quiesce the driver; 1986550a7375SFelipe Balbi * then report disconnect 1987550a7375SFelipe Balbi */ 1988550a7375SFelipe Balbi if (driver) { 1989550a7375SFelipe Balbi for (i = 0, hw_ep = musb->endpoints; 1990550a7375SFelipe Balbi i < musb->nr_endpoints; 1991550a7375SFelipe Balbi i++, hw_ep++) { 1992550a7375SFelipe Balbi musb_ep_select(musb->mregs, i); 1993550a7375SFelipe Balbi if (hw_ep->is_shared_fifo /* || !epnum */) { 1994550a7375SFelipe Balbi nuke(&hw_ep->ep_in, -ESHUTDOWN); 1995550a7375SFelipe Balbi } else { 1996550a7375SFelipe Balbi if (hw_ep->max_packet_sz_tx) 1997550a7375SFelipe Balbi nuke(&hw_ep->ep_in, -ESHUTDOWN); 1998550a7375SFelipe Balbi if (hw_ep->max_packet_sz_rx) 1999550a7375SFelipe Balbi nuke(&hw_ep->ep_out, -ESHUTDOWN); 2000550a7375SFelipe Balbi } 2001550a7375SFelipe Balbi } 2002550a7375SFelipe Balbi } 2003550a7375SFelipe Balbi } 2004550a7375SFelipe Balbi 2005550a7375SFelipe Balbi /* 2006550a7375SFelipe Balbi * Unregister the gadget driver. Used by gadget drivers when 2007550a7375SFelipe Balbi * unregistering themselves from the controller. 2008550a7375SFelipe Balbi * 2009550a7375SFelipe Balbi * @param driver the gadget driver to unregister 2010550a7375SFelipe Balbi */ 2011e71eb392SSebastian Andrzej Siewior static int musb_gadget_stop(struct usb_gadget *g, 2012e71eb392SSebastian Andrzej Siewior struct usb_gadget_driver *driver) 2013550a7375SFelipe Balbi { 2014e71eb392SSebastian Andrzej Siewior struct musb *musb = gadget_to_musb(g); 201563eed2b5SFelipe Balbi unsigned long flags; 2016550a7375SFelipe Balbi 20177acc6197SHema HK if (musb->xceiv->last_event == USB_EVENT_NONE) 20187acc6197SHema HK pm_runtime_get_sync(musb->controller); 20197acc6197SHema HK 202063eed2b5SFelipe Balbi /* 202163eed2b5SFelipe Balbi * REVISIT always use otg_set_peripheral() here too; 2022550a7375SFelipe Balbi * this needs to shut down the OTG engine. 2023550a7375SFelipe Balbi */ 2024550a7375SFelipe Balbi 2025550a7375SFelipe Balbi spin_lock_irqsave(&musb->lock, flags); 2026550a7375SFelipe Balbi 2027550a7375SFelipe Balbi musb_hnp_stop(musb); 2028550a7375SFelipe Balbi 2029550a7375SFelipe Balbi (void) musb_gadget_vbus_draw(&musb->g, 0); 2030550a7375SFelipe Balbi 203184e250ffSDavid Brownell musb->xceiv->state = OTG_STATE_UNDEFINED; 2032550a7375SFelipe Balbi stop_activity(musb, driver); 203384e250ffSDavid Brownell otg_set_peripheral(musb->xceiv, NULL); 2034550a7375SFelipe Balbi 20355c8a86e1SFelipe Balbi dev_dbg(musb->controller, "unregistering driver %s\n", driver->function); 203663eed2b5SFelipe Balbi 2037550a7375SFelipe Balbi musb->is_active = 0; 2038550a7375SFelipe Balbi musb_platform_try_idle(musb, 0); 2039550a7375SFelipe Balbi spin_unlock_irqrestore(&musb->lock, flags); 2040550a7375SFelipe Balbi 204163eed2b5SFelipe Balbi if (is_otg_enabled(musb)) { 2042550a7375SFelipe Balbi usb_remove_hcd(musb_to_hcd(musb)); 2043550a7375SFelipe Balbi /* FIXME we need to be able to register another 2044550a7375SFelipe Balbi * gadget driver here and have everything work; 2045550a7375SFelipe Balbi * that currently misbehaves. 2046550a7375SFelipe Balbi */ 2047550a7375SFelipe Balbi } 2048550a7375SFelipe Balbi 204963eed2b5SFelipe Balbi if (!is_otg_enabled(musb)) 205063eed2b5SFelipe Balbi musb_stop(musb); 205163eed2b5SFelipe Balbi 20527acc6197SHema HK pm_runtime_put(musb->controller); 20537acc6197SHema HK 205463eed2b5SFelipe Balbi return 0; 2055550a7375SFelipe Balbi } 2056550a7375SFelipe Balbi 2057550a7375SFelipe Balbi /* ----------------------------------------------------------------------- */ 2058550a7375SFelipe Balbi 2059550a7375SFelipe Balbi /* lifecycle operations called through plat_uds.c */ 2060550a7375SFelipe Balbi 2061550a7375SFelipe Balbi void musb_g_resume(struct musb *musb) 2062550a7375SFelipe Balbi { 2063550a7375SFelipe Balbi musb->is_suspended = 0; 206484e250ffSDavid Brownell switch (musb->xceiv->state) { 2065550a7375SFelipe Balbi case OTG_STATE_B_IDLE: 2066550a7375SFelipe Balbi break; 2067550a7375SFelipe Balbi case OTG_STATE_B_WAIT_ACON: 2068550a7375SFelipe Balbi case OTG_STATE_B_PERIPHERAL: 2069550a7375SFelipe Balbi musb->is_active = 1; 2070550a7375SFelipe Balbi if (musb->gadget_driver && musb->gadget_driver->resume) { 2071550a7375SFelipe Balbi spin_unlock(&musb->lock); 2072550a7375SFelipe Balbi musb->gadget_driver->resume(&musb->g); 2073550a7375SFelipe Balbi spin_lock(&musb->lock); 2074550a7375SFelipe Balbi } 2075550a7375SFelipe Balbi break; 2076550a7375SFelipe Balbi default: 2077550a7375SFelipe Balbi WARNING("unhandled RESUME transition (%s)\n", 20783df00453SAnatolij Gustschin otg_state_string(musb->xceiv->state)); 2079550a7375SFelipe Balbi } 2080550a7375SFelipe Balbi } 2081550a7375SFelipe Balbi 2082550a7375SFelipe Balbi /* called when SOF packets stop for 3+ msec */ 2083550a7375SFelipe Balbi void musb_g_suspend(struct musb *musb) 2084550a7375SFelipe Balbi { 2085550a7375SFelipe Balbi u8 devctl; 2086550a7375SFelipe Balbi 2087550a7375SFelipe Balbi devctl = musb_readb(musb->mregs, MUSB_DEVCTL); 20885c8a86e1SFelipe Balbi dev_dbg(musb->controller, "devctl %02x\n", devctl); 2089550a7375SFelipe Balbi 209084e250ffSDavid Brownell switch (musb->xceiv->state) { 2091550a7375SFelipe Balbi case OTG_STATE_B_IDLE: 2092550a7375SFelipe Balbi if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS) 209384e250ffSDavid Brownell musb->xceiv->state = OTG_STATE_B_PERIPHERAL; 2094550a7375SFelipe Balbi break; 2095550a7375SFelipe Balbi case OTG_STATE_B_PERIPHERAL: 2096550a7375SFelipe Balbi musb->is_suspended = 1; 2097550a7375SFelipe Balbi if (musb->gadget_driver && musb->gadget_driver->suspend) { 2098550a7375SFelipe Balbi spin_unlock(&musb->lock); 2099550a7375SFelipe Balbi musb->gadget_driver->suspend(&musb->g); 2100550a7375SFelipe Balbi spin_lock(&musb->lock); 2101550a7375SFelipe Balbi } 2102550a7375SFelipe Balbi break; 2103550a7375SFelipe Balbi default: 2104550a7375SFelipe Balbi /* REVISIT if B_HOST, clear DEVCTL.HOSTREQ; 2105550a7375SFelipe Balbi * A_PERIPHERAL may need care too 2106550a7375SFelipe Balbi */ 2107550a7375SFelipe Balbi WARNING("unhandled SUSPEND transition (%s)\n", 21083df00453SAnatolij Gustschin otg_state_string(musb->xceiv->state)); 2109550a7375SFelipe Balbi } 2110550a7375SFelipe Balbi } 2111550a7375SFelipe Balbi 2112550a7375SFelipe Balbi /* Called during SRP */ 2113550a7375SFelipe Balbi void musb_g_wakeup(struct musb *musb) 2114550a7375SFelipe Balbi { 2115550a7375SFelipe Balbi musb_gadget_wakeup(&musb->g); 2116550a7375SFelipe Balbi } 2117550a7375SFelipe Balbi 2118550a7375SFelipe Balbi /* called when VBUS drops below session threshold, and in other cases */ 2119550a7375SFelipe Balbi void musb_g_disconnect(struct musb *musb) 2120550a7375SFelipe Balbi { 2121550a7375SFelipe Balbi void __iomem *mregs = musb->mregs; 2122550a7375SFelipe Balbi u8 devctl = musb_readb(mregs, MUSB_DEVCTL); 2123550a7375SFelipe Balbi 21245c8a86e1SFelipe Balbi dev_dbg(musb->controller, "devctl %02x\n", devctl); 2125550a7375SFelipe Balbi 2126550a7375SFelipe Balbi /* clear HR */ 2127550a7375SFelipe Balbi musb_writeb(mregs, MUSB_DEVCTL, devctl & MUSB_DEVCTL_SESSION); 2128550a7375SFelipe Balbi 2129550a7375SFelipe Balbi /* don't draw vbus until new b-default session */ 2130550a7375SFelipe Balbi (void) musb_gadget_vbus_draw(&musb->g, 0); 2131550a7375SFelipe Balbi 2132550a7375SFelipe Balbi musb->g.speed = USB_SPEED_UNKNOWN; 2133550a7375SFelipe Balbi if (musb->gadget_driver && musb->gadget_driver->disconnect) { 2134550a7375SFelipe Balbi spin_unlock(&musb->lock); 2135550a7375SFelipe Balbi musb->gadget_driver->disconnect(&musb->g); 2136550a7375SFelipe Balbi spin_lock(&musb->lock); 2137550a7375SFelipe Balbi } 2138550a7375SFelipe Balbi 213984e250ffSDavid Brownell switch (musb->xceiv->state) { 2140550a7375SFelipe Balbi default: 21415c8a86e1SFelipe Balbi dev_dbg(musb->controller, "Unhandled disconnect %s, setting a_idle\n", 21423df00453SAnatolij Gustschin otg_state_string(musb->xceiv->state)); 214384e250ffSDavid Brownell musb->xceiv->state = OTG_STATE_A_IDLE; 2144ab983f2aSDavid Brownell MUSB_HST_MODE(musb); 2145550a7375SFelipe Balbi break; 2146550a7375SFelipe Balbi case OTG_STATE_A_PERIPHERAL: 21471de00daeSDavid Brownell musb->xceiv->state = OTG_STATE_A_WAIT_BCON; 2148ab983f2aSDavid Brownell MUSB_HST_MODE(musb); 2149550a7375SFelipe Balbi break; 2150550a7375SFelipe Balbi case OTG_STATE_B_WAIT_ACON: 2151550a7375SFelipe Balbi case OTG_STATE_B_HOST: 2152550a7375SFelipe Balbi case OTG_STATE_B_PERIPHERAL: 2153550a7375SFelipe Balbi case OTG_STATE_B_IDLE: 215484e250ffSDavid Brownell musb->xceiv->state = OTG_STATE_B_IDLE; 2155550a7375SFelipe Balbi break; 2156550a7375SFelipe Balbi case OTG_STATE_B_SRP_INIT: 2157550a7375SFelipe Balbi break; 2158550a7375SFelipe Balbi } 2159550a7375SFelipe Balbi 2160550a7375SFelipe Balbi musb->is_active = 0; 2161550a7375SFelipe Balbi } 2162550a7375SFelipe Balbi 2163550a7375SFelipe Balbi void musb_g_reset(struct musb *musb) 2164550a7375SFelipe Balbi __releases(musb->lock) 2165550a7375SFelipe Balbi __acquires(musb->lock) 2166550a7375SFelipe Balbi { 2167550a7375SFelipe Balbi void __iomem *mbase = musb->mregs; 2168550a7375SFelipe Balbi u8 devctl = musb_readb(mbase, MUSB_DEVCTL); 2169550a7375SFelipe Balbi u8 power; 2170550a7375SFelipe Balbi 21715c8a86e1SFelipe Balbi dev_dbg(musb->controller, "<== %s addr=%x driver '%s'\n", 2172550a7375SFelipe Balbi (devctl & MUSB_DEVCTL_BDEVICE) 2173550a7375SFelipe Balbi ? "B-Device" : "A-Device", 2174550a7375SFelipe Balbi musb_readb(mbase, MUSB_FADDR), 2175550a7375SFelipe Balbi musb->gadget_driver 2176550a7375SFelipe Balbi ? musb->gadget_driver->driver.name 2177550a7375SFelipe Balbi : NULL 2178550a7375SFelipe Balbi ); 2179550a7375SFelipe Balbi 2180550a7375SFelipe Balbi /* report disconnect, if we didn't already (flushing EP state) */ 2181550a7375SFelipe Balbi if (musb->g.speed != USB_SPEED_UNKNOWN) 2182550a7375SFelipe Balbi musb_g_disconnect(musb); 2183550a7375SFelipe Balbi 2184550a7375SFelipe Balbi /* clear HR */ 2185550a7375SFelipe Balbi else if (devctl & MUSB_DEVCTL_HR) 2186550a7375SFelipe Balbi musb_writeb(mbase, MUSB_DEVCTL, MUSB_DEVCTL_SESSION); 2187550a7375SFelipe Balbi 2188550a7375SFelipe Balbi 2189550a7375SFelipe Balbi /* what speed did we negotiate? */ 2190550a7375SFelipe Balbi power = musb_readb(mbase, MUSB_POWER); 2191550a7375SFelipe Balbi musb->g.speed = (power & MUSB_POWER_HSMODE) 2192550a7375SFelipe Balbi ? USB_SPEED_HIGH : USB_SPEED_FULL; 2193550a7375SFelipe Balbi 2194550a7375SFelipe Balbi /* start in USB_STATE_DEFAULT */ 2195550a7375SFelipe Balbi musb->is_active = 1; 2196550a7375SFelipe Balbi musb->is_suspended = 0; 2197550a7375SFelipe Balbi MUSB_DEV_MODE(musb); 2198550a7375SFelipe Balbi musb->address = 0; 2199550a7375SFelipe Balbi musb->ep0_state = MUSB_EP0_STAGE_SETUP; 2200550a7375SFelipe Balbi 2201550a7375SFelipe Balbi musb->may_wakeup = 0; 2202550a7375SFelipe Balbi musb->g.b_hnp_enable = 0; 2203550a7375SFelipe Balbi musb->g.a_alt_hnp_support = 0; 2204550a7375SFelipe Balbi musb->g.a_hnp_support = 0; 2205550a7375SFelipe Balbi 2206550a7375SFelipe Balbi /* Normal reset, as B-Device; 2207550a7375SFelipe Balbi * or else after HNP, as A-Device 2208550a7375SFelipe Balbi */ 2209550a7375SFelipe Balbi if (devctl & MUSB_DEVCTL_BDEVICE) { 221084e250ffSDavid Brownell musb->xceiv->state = OTG_STATE_B_PERIPHERAL; 2211550a7375SFelipe Balbi musb->g.is_a_peripheral = 0; 2212550a7375SFelipe Balbi } else if (is_otg_enabled(musb)) { 221384e250ffSDavid Brownell musb->xceiv->state = OTG_STATE_A_PERIPHERAL; 2214550a7375SFelipe Balbi musb->g.is_a_peripheral = 1; 2215550a7375SFelipe Balbi } else 2216550a7375SFelipe Balbi WARN_ON(1); 2217550a7375SFelipe Balbi 2218550a7375SFelipe Balbi /* start with default limits on VBUS power draw */ 2219550a7375SFelipe Balbi (void) musb_gadget_vbus_draw(&musb->g, 2220550a7375SFelipe Balbi is_otg_enabled(musb) ? 8 : 100); 2221550a7375SFelipe Balbi } 2222