1550a7375SFelipe Balbi /* 2550a7375SFelipe Balbi * MUSB OTG driver host 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 7c7bbc056SSergei Shtylyov * Copyright (C) 2008-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/module.h> 37550a7375SFelipe Balbi #include <linux/kernel.h> 38550a7375SFelipe Balbi #include <linux/delay.h> 39550a7375SFelipe Balbi #include <linux/sched.h> 40550a7375SFelipe Balbi #include <linux/slab.h> 41550a7375SFelipe Balbi #include <linux/errno.h> 42550a7375SFelipe Balbi #include <linux/list.h> 43496dda70SMaulik Mankad #include <linux/dma-mapping.h> 44550a7375SFelipe Balbi 45550a7375SFelipe Balbi #include "musb_core.h" 46550a7375SFelipe Balbi #include "musb_host.h" 47550a7375SFelipe Balbi 48550a7375SFelipe Balbi /* MUSB HOST status 22-mar-2006 49550a7375SFelipe Balbi * 50550a7375SFelipe Balbi * - There's still lots of partial code duplication for fault paths, so 51550a7375SFelipe Balbi * they aren't handled as consistently as they need to be. 52550a7375SFelipe Balbi * 53550a7375SFelipe Balbi * - PIO mostly behaved when last tested. 54550a7375SFelipe Balbi * + including ep0, with all usbtest cases 9, 10 55550a7375SFelipe Balbi * + usbtest 14 (ep0out) doesn't seem to run at all 56550a7375SFelipe Balbi * + double buffered OUT/TX endpoints saw stalls(!) with certain usbtest 57550a7375SFelipe Balbi * configurations, but otherwise double buffering passes basic tests. 58550a7375SFelipe Balbi * + for 2.6.N, for N > ~10, needs API changes for hcd framework. 59550a7375SFelipe Balbi * 60550a7375SFelipe Balbi * - DMA (CPPI) ... partially behaves, not currently recommended 61550a7375SFelipe Balbi * + about 1/15 the speed of typical EHCI implementations (PCI) 62550a7375SFelipe Balbi * + RX, all too often reqpkt seems to misbehave after tx 63550a7375SFelipe Balbi * + TX, no known issues (other than evident silicon issue) 64550a7375SFelipe Balbi * 65550a7375SFelipe Balbi * - DMA (Mentor/OMAP) ...has at least toggle update problems 66550a7375SFelipe Balbi * 671e0320f0SAjay Kumar Gupta * - [23-feb-2009] minimal traffic scheduling to avoid bulk RX packet 681e0320f0SAjay Kumar Gupta * starvation ... nothing yet for TX, interrupt, or bulk. 69550a7375SFelipe Balbi * 70550a7375SFelipe Balbi * - Not tested with HNP, but some SRP paths seem to behave. 71550a7375SFelipe Balbi * 72550a7375SFelipe Balbi * NOTE 24-August-2006: 73550a7375SFelipe Balbi * 74550a7375SFelipe Balbi * - Bulk traffic finally uses both sides of hardware ep1, freeing up an 75550a7375SFelipe Balbi * extra endpoint for periodic use enabling hub + keybd + mouse. That 76550a7375SFelipe Balbi * mostly works, except that with "usbnet" it's easy to trigger cases 77550a7375SFelipe Balbi * with "ping" where RX loses. (a) ping to davinci, even "ping -f", 78550a7375SFelipe Balbi * fine; but (b) ping _from_ davinci, even "ping -c 1", ICMP RX loses 79550a7375SFelipe Balbi * although ARP RX wins. (That test was done with a full speed link.) 80550a7375SFelipe Balbi */ 81550a7375SFelipe Balbi 82550a7375SFelipe Balbi 83550a7375SFelipe Balbi /* 84550a7375SFelipe Balbi * NOTE on endpoint usage: 85550a7375SFelipe Balbi * 86550a7375SFelipe Balbi * CONTROL transfers all go through ep0. BULK ones go through dedicated IN 87550a7375SFelipe Balbi * and OUT endpoints ... hardware is dedicated for those "async" queue(s). 88550a7375SFelipe Balbi * (Yes, bulk _could_ use more of the endpoints than that, and would even 891e0320f0SAjay Kumar Gupta * benefit from it.) 90550a7375SFelipe Balbi * 91550a7375SFelipe Balbi * INTERUPPT and ISOCHRONOUS transfers are scheduled to the other endpoints. 92550a7375SFelipe Balbi * So far that scheduling is both dumb and optimistic: the endpoint will be 93550a7375SFelipe Balbi * "claimed" until its software queue is no longer refilled. No multiplexing 94550a7375SFelipe Balbi * of transfers between endpoints, or anything clever. 95550a7375SFelipe Balbi */ 96550a7375SFelipe Balbi 9774c2e936SDaniel Mack struct musb *hcd_to_musb(struct usb_hcd *hcd) 9874c2e936SDaniel Mack { 9974c2e936SDaniel Mack return *(struct musb **) hcd->hcd_priv; 10074c2e936SDaniel Mack } 10174c2e936SDaniel Mack 102550a7375SFelipe Balbi 103550a7375SFelipe Balbi static void musb_ep_program(struct musb *musb, u8 epnum, 1046b6e9710SSergei Shtylyov struct urb *urb, int is_out, 1056b6e9710SSergei Shtylyov u8 *buf, u32 offset, u32 len); 106550a7375SFelipe Balbi 107550a7375SFelipe Balbi /* 108550a7375SFelipe Balbi * Clear TX fifo. Needed to avoid BABBLE errors. 109550a7375SFelipe Balbi */ 110c767c1c6SDavid Brownell static void musb_h_tx_flush_fifo(struct musb_hw_ep *ep) 111550a7375SFelipe Balbi { 1125c8a86e1SFelipe Balbi struct musb *musb = ep->musb; 113550a7375SFelipe Balbi void __iomem *epio = ep->regs; 114550a7375SFelipe Balbi u16 csr; 115bb1c9ef1SDavid Brownell u16 lastcsr = 0; 116550a7375SFelipe Balbi int retries = 1000; 117550a7375SFelipe Balbi 118550a7375SFelipe Balbi csr = musb_readw(epio, MUSB_TXCSR); 119550a7375SFelipe Balbi while (csr & MUSB_TXCSR_FIFONOTEMPTY) { 120bb1c9ef1SDavid Brownell if (csr != lastcsr) 1215c8a86e1SFelipe Balbi dev_dbg(musb->controller, "Host TX FIFONOTEMPTY csr: %02x\n", csr); 122bb1c9ef1SDavid Brownell lastcsr = csr; 1232ccc6d30SDaniel Mack csr |= MUSB_TXCSR_FLUSHFIFO | MUSB_TXCSR_TXPKTRDY; 124550a7375SFelipe Balbi musb_writew(epio, MUSB_TXCSR, csr); 125550a7375SFelipe Balbi csr = musb_readw(epio, MUSB_TXCSR); 126bb1c9ef1SDavid Brownell if (WARN(retries-- < 1, 127bb1c9ef1SDavid Brownell "Could not flush host TX%d fifo: csr: %04x\n", 128bb1c9ef1SDavid Brownell ep->epnum, csr)) 129550a7375SFelipe Balbi return; 130550a7375SFelipe Balbi mdelay(1); 131550a7375SFelipe Balbi } 132550a7375SFelipe Balbi } 133550a7375SFelipe Balbi 13478322c1aSDavid Brownell static void musb_h_ep0_flush_fifo(struct musb_hw_ep *ep) 13578322c1aSDavid Brownell { 13678322c1aSDavid Brownell void __iomem *epio = ep->regs; 13778322c1aSDavid Brownell u16 csr; 13878322c1aSDavid Brownell int retries = 5; 13978322c1aSDavid Brownell 14078322c1aSDavid Brownell /* scrub any data left in the fifo */ 14178322c1aSDavid Brownell do { 14278322c1aSDavid Brownell csr = musb_readw(epio, MUSB_TXCSR); 14378322c1aSDavid Brownell if (!(csr & (MUSB_CSR0_TXPKTRDY | MUSB_CSR0_RXPKTRDY))) 14478322c1aSDavid Brownell break; 14578322c1aSDavid Brownell musb_writew(epio, MUSB_TXCSR, MUSB_CSR0_FLUSHFIFO); 14678322c1aSDavid Brownell csr = musb_readw(epio, MUSB_TXCSR); 14778322c1aSDavid Brownell udelay(10); 14878322c1aSDavid Brownell } while (--retries); 14978322c1aSDavid Brownell 15078322c1aSDavid Brownell WARN(!retries, "Could not flush host TX%d fifo: csr: %04x\n", 15178322c1aSDavid Brownell ep->epnum, csr); 15278322c1aSDavid Brownell 15378322c1aSDavid Brownell /* and reset for the next transfer */ 15478322c1aSDavid Brownell musb_writew(epio, MUSB_TXCSR, 0); 15578322c1aSDavid Brownell } 15678322c1aSDavid Brownell 157550a7375SFelipe Balbi /* 158550a7375SFelipe Balbi * Start transmit. Caller is responsible for locking shared resources. 159550a7375SFelipe Balbi * musb must be locked. 160550a7375SFelipe Balbi */ 161550a7375SFelipe Balbi static inline void musb_h_tx_start(struct musb_hw_ep *ep) 162550a7375SFelipe Balbi { 163550a7375SFelipe Balbi u16 txcsr; 164550a7375SFelipe Balbi 165550a7375SFelipe Balbi /* NOTE: no locks here; caller should lock and select EP */ 166550a7375SFelipe Balbi if (ep->epnum) { 167550a7375SFelipe Balbi txcsr = musb_readw(ep->regs, MUSB_TXCSR); 168550a7375SFelipe Balbi txcsr |= MUSB_TXCSR_TXPKTRDY | MUSB_TXCSR_H_WZC_BITS; 169550a7375SFelipe Balbi musb_writew(ep->regs, MUSB_TXCSR, txcsr); 170550a7375SFelipe Balbi } else { 171550a7375SFelipe Balbi txcsr = MUSB_CSR0_H_SETUPPKT | MUSB_CSR0_TXPKTRDY; 172550a7375SFelipe Balbi musb_writew(ep->regs, MUSB_CSR0, txcsr); 173550a7375SFelipe Balbi } 174550a7375SFelipe Balbi 175550a7375SFelipe Balbi } 176550a7375SFelipe Balbi 177c7bbc056SSergei Shtylyov static inline void musb_h_tx_dma_start(struct musb_hw_ep *ep) 178550a7375SFelipe Balbi { 179550a7375SFelipe Balbi u16 txcsr; 180550a7375SFelipe Balbi 181550a7375SFelipe Balbi /* NOTE: no locks here; caller should lock and select EP */ 182550a7375SFelipe Balbi txcsr = musb_readw(ep->regs, MUSB_TXCSR); 183550a7375SFelipe Balbi txcsr |= MUSB_TXCSR_DMAENAB | MUSB_TXCSR_H_WZC_BITS; 184f8e9f34fSTony Lindgren if (is_cppi_enabled(ep->musb)) 185c7bbc056SSergei Shtylyov txcsr |= MUSB_TXCSR_DMAMODE; 186550a7375SFelipe Balbi musb_writew(ep->regs, MUSB_TXCSR, txcsr); 187550a7375SFelipe Balbi } 188550a7375SFelipe Balbi 1893e5c6dc7SSergei Shtylyov static void musb_ep_set_qh(struct musb_hw_ep *ep, int is_in, struct musb_qh *qh) 1903e5c6dc7SSergei Shtylyov { 1913e5c6dc7SSergei Shtylyov if (is_in != 0 || ep->is_shared_fifo) 1923e5c6dc7SSergei Shtylyov ep->in_qh = qh; 1933e5c6dc7SSergei Shtylyov if (is_in == 0 || ep->is_shared_fifo) 1943e5c6dc7SSergei Shtylyov ep->out_qh = qh; 1953e5c6dc7SSergei Shtylyov } 1963e5c6dc7SSergei Shtylyov 1973e5c6dc7SSergei Shtylyov static struct musb_qh *musb_ep_get_qh(struct musb_hw_ep *ep, int is_in) 1983e5c6dc7SSergei Shtylyov { 1993e5c6dc7SSergei Shtylyov return is_in ? ep->in_qh : ep->out_qh; 2003e5c6dc7SSergei Shtylyov } 2013e5c6dc7SSergei Shtylyov 202550a7375SFelipe Balbi /* 203550a7375SFelipe Balbi * Start the URB at the front of an endpoint's queue 204550a7375SFelipe Balbi * end must be claimed from the caller. 205550a7375SFelipe Balbi * 206550a7375SFelipe Balbi * Context: controller locked, irqs blocked 207550a7375SFelipe Balbi */ 208550a7375SFelipe Balbi static void 209550a7375SFelipe Balbi musb_start_urb(struct musb *musb, int is_in, struct musb_qh *qh) 210550a7375SFelipe Balbi { 211550a7375SFelipe Balbi u16 frame; 212550a7375SFelipe Balbi u32 len; 213550a7375SFelipe Balbi void __iomem *mbase = musb->mregs; 214550a7375SFelipe Balbi struct urb *urb = next_urb(qh); 2156b6e9710SSergei Shtylyov void *buf = urb->transfer_buffer; 2166b6e9710SSergei Shtylyov u32 offset = 0; 217550a7375SFelipe Balbi struct musb_hw_ep *hw_ep = qh->hw_ep; 218550a7375SFelipe Balbi unsigned pipe = urb->pipe; 219550a7375SFelipe Balbi u8 address = usb_pipedevice(pipe); 220550a7375SFelipe Balbi int epnum = hw_ep->epnum; 221550a7375SFelipe Balbi 222550a7375SFelipe Balbi /* initialize software qh state */ 223550a7375SFelipe Balbi qh->offset = 0; 224550a7375SFelipe Balbi qh->segsize = 0; 225550a7375SFelipe Balbi 226550a7375SFelipe Balbi /* gather right source of data */ 227550a7375SFelipe Balbi switch (qh->type) { 228550a7375SFelipe Balbi case USB_ENDPOINT_XFER_CONTROL: 229550a7375SFelipe Balbi /* control transfers always start with SETUP */ 230550a7375SFelipe Balbi is_in = 0; 231550a7375SFelipe Balbi musb->ep0_stage = MUSB_EP0_START; 232550a7375SFelipe Balbi buf = urb->setup_packet; 233550a7375SFelipe Balbi len = 8; 234550a7375SFelipe Balbi break; 235550a7375SFelipe Balbi case USB_ENDPOINT_XFER_ISOC: 236550a7375SFelipe Balbi qh->iso_idx = 0; 237550a7375SFelipe Balbi qh->frame = 0; 2386b6e9710SSergei Shtylyov offset = urb->iso_frame_desc[0].offset; 239550a7375SFelipe Balbi len = urb->iso_frame_desc[0].length; 240550a7375SFelipe Balbi break; 241550a7375SFelipe Balbi default: /* bulk, interrupt */ 2421e0320f0SAjay Kumar Gupta /* actual_length may be nonzero on retry paths */ 2431e0320f0SAjay Kumar Gupta buf = urb->transfer_buffer + urb->actual_length; 2441e0320f0SAjay Kumar Gupta len = urb->transfer_buffer_length - urb->actual_length; 245550a7375SFelipe Balbi } 246550a7375SFelipe Balbi 2475c8a86e1SFelipe Balbi dev_dbg(musb->controller, "qh %p urb %p dev%d ep%d%s%s, hw_ep %d, %p/%d\n", 248550a7375SFelipe Balbi qh, urb, address, qh->epnum, 249550a7375SFelipe Balbi is_in ? "in" : "out", 250550a7375SFelipe Balbi ({char *s; switch (qh->type) { 251550a7375SFelipe Balbi case USB_ENDPOINT_XFER_CONTROL: s = ""; break; 252550a7375SFelipe Balbi case USB_ENDPOINT_XFER_BULK: s = "-bulk"; break; 253550a7375SFelipe Balbi case USB_ENDPOINT_XFER_ISOC: s = "-iso"; break; 254550a7375SFelipe Balbi default: s = "-intr"; break; 2552b84f92bSJoe Perches } s; }), 2566b6e9710SSergei Shtylyov epnum, buf + offset, len); 257550a7375SFelipe Balbi 258550a7375SFelipe Balbi /* Configure endpoint */ 2593e5c6dc7SSergei Shtylyov musb_ep_set_qh(hw_ep, is_in, qh); 2606b6e9710SSergei Shtylyov musb_ep_program(musb, epnum, urb, !is_in, buf, offset, len); 261550a7375SFelipe Balbi 262550a7375SFelipe Balbi /* transmit may have more work: start it when it is time */ 263550a7375SFelipe Balbi if (is_in) 264550a7375SFelipe Balbi return; 265550a7375SFelipe Balbi 266550a7375SFelipe Balbi /* determine if the time is right for a periodic transfer */ 267550a7375SFelipe Balbi switch (qh->type) { 268550a7375SFelipe Balbi case USB_ENDPOINT_XFER_ISOC: 269550a7375SFelipe Balbi case USB_ENDPOINT_XFER_INT: 2705c8a86e1SFelipe Balbi dev_dbg(musb->controller, "check whether there's still time for periodic Tx\n"); 271550a7375SFelipe Balbi frame = musb_readw(mbase, MUSB_FRAME); 272550a7375SFelipe Balbi /* FIXME this doesn't implement that scheduling policy ... 273550a7375SFelipe Balbi * or handle framecounter wrapping 274550a7375SFelipe Balbi */ 2758a1ea51fSAlan Stern if (1) { /* Always assume URB_ISO_ASAP */ 276550a7375SFelipe Balbi /* REVISIT the SOF irq handler shouldn't duplicate 277550a7375SFelipe Balbi * this code; and we don't init urb->start_frame... 278550a7375SFelipe Balbi */ 279550a7375SFelipe Balbi qh->frame = 0; 280550a7375SFelipe Balbi goto start; 281550a7375SFelipe Balbi } else { 282550a7375SFelipe Balbi qh->frame = urb->start_frame; 283550a7375SFelipe Balbi /* enable SOF interrupt so we can count down */ 2845c8a86e1SFelipe Balbi dev_dbg(musb->controller, "SOF for %d\n", epnum); 285550a7375SFelipe Balbi #if 1 /* ifndef CONFIG_ARCH_DAVINCI */ 286550a7375SFelipe Balbi musb_writeb(mbase, MUSB_INTRUSBE, 0xff); 287550a7375SFelipe Balbi #endif 288550a7375SFelipe Balbi } 289550a7375SFelipe Balbi break; 290550a7375SFelipe Balbi default: 291550a7375SFelipe Balbi start: 2925c8a86e1SFelipe Balbi dev_dbg(musb->controller, "Start TX%d %s\n", epnum, 293550a7375SFelipe Balbi hw_ep->tx_channel ? "dma" : "pio"); 294550a7375SFelipe Balbi 295550a7375SFelipe Balbi if (!hw_ep->tx_channel) 296550a7375SFelipe Balbi musb_h_tx_start(hw_ep); 297f8e9f34fSTony Lindgren else if (is_cppi_enabled(musb) || tusb_dma_omap(musb)) 298c7bbc056SSergei Shtylyov musb_h_tx_dma_start(hw_ep); 299550a7375SFelipe Balbi } 300550a7375SFelipe Balbi } 301550a7375SFelipe Balbi 302c9cd06b3SSergei Shtylyov /* Context: caller owns controller lock, IRQs are blocked */ 303c9cd06b3SSergei Shtylyov static void musb_giveback(struct musb *musb, struct urb *urb, int status) 304550a7375SFelipe Balbi __releases(musb->lock) 305550a7375SFelipe Balbi __acquires(musb->lock) 306550a7375SFelipe Balbi { 3075c8a86e1SFelipe Balbi dev_dbg(musb->controller, 308bb1c9ef1SDavid Brownell "complete %p %pF (%d), dev%d ep%d%s, %d/%d\n", 309bb1c9ef1SDavid Brownell urb, urb->complete, status, 310550a7375SFelipe Balbi usb_pipedevice(urb->pipe), 311550a7375SFelipe Balbi usb_pipeendpoint(urb->pipe), 312550a7375SFelipe Balbi usb_pipein(urb->pipe) ? "in" : "out", 313550a7375SFelipe Balbi urb->actual_length, urb->transfer_buffer_length 314550a7375SFelipe Balbi ); 315550a7375SFelipe Balbi 3168b125df5SDaniel Mack usb_hcd_unlink_urb_from_ep(musb->hcd, urb); 317550a7375SFelipe Balbi spin_unlock(&musb->lock); 3188b125df5SDaniel Mack usb_hcd_giveback_urb(musb->hcd, urb, status); 319550a7375SFelipe Balbi spin_lock(&musb->lock); 320550a7375SFelipe Balbi } 321550a7375SFelipe Balbi 322846099a6SSergei Shtylyov /* For bulk/interrupt endpoints only */ 323846099a6SSergei Shtylyov static inline void musb_save_toggle(struct musb_qh *qh, int is_in, 324846099a6SSergei Shtylyov struct urb *urb) 325550a7375SFelipe Balbi { 326846099a6SSergei Shtylyov void __iomem *epio = qh->hw_ep->regs; 327550a7375SFelipe Balbi u16 csr; 328550a7375SFelipe Balbi 329846099a6SSergei Shtylyov /* 330846099a6SSergei Shtylyov * FIXME: the current Mentor DMA code seems to have 331550a7375SFelipe Balbi * problems getting toggle correct. 332550a7375SFelipe Balbi */ 333550a7375SFelipe Balbi 334846099a6SSergei Shtylyov if (is_in) 335846099a6SSergei Shtylyov csr = musb_readw(epio, MUSB_RXCSR) & MUSB_RXCSR_H_DATATOGGLE; 336550a7375SFelipe Balbi else 337846099a6SSergei Shtylyov csr = musb_readw(epio, MUSB_TXCSR) & MUSB_TXCSR_H_DATATOGGLE; 338550a7375SFelipe Balbi 339846099a6SSergei Shtylyov usb_settoggle(urb->dev, qh->epnum, !is_in, csr ? 1 : 0); 340550a7375SFelipe Balbi } 341550a7375SFelipe Balbi 342c9cd06b3SSergei Shtylyov /* 343c9cd06b3SSergei Shtylyov * Advance this hardware endpoint's queue, completing the specified URB and 344c9cd06b3SSergei Shtylyov * advancing to either the next URB queued to that qh, or else invalidating 345c9cd06b3SSergei Shtylyov * that qh and advancing to the next qh scheduled after the current one. 346c9cd06b3SSergei Shtylyov * 347c9cd06b3SSergei Shtylyov * Context: caller owns controller lock, IRQs are blocked 348c9cd06b3SSergei Shtylyov */ 349c9cd06b3SSergei Shtylyov static void musb_advance_schedule(struct musb *musb, struct urb *urb, 350c9cd06b3SSergei Shtylyov struct musb_hw_ep *hw_ep, int is_in) 351550a7375SFelipe Balbi { 352c9cd06b3SSergei Shtylyov struct musb_qh *qh = musb_ep_get_qh(hw_ep, is_in); 353550a7375SFelipe Balbi struct musb_hw_ep *ep = qh->hw_ep; 354550a7375SFelipe Balbi int ready = qh->is_ready; 355c9cd06b3SSergei Shtylyov int status; 356c9cd06b3SSergei Shtylyov 357c9cd06b3SSergei Shtylyov status = (urb->status == -EINPROGRESS) ? 0 : urb->status; 358550a7375SFelipe Balbi 359550a7375SFelipe Balbi /* save toggle eagerly, for paranoia */ 360550a7375SFelipe Balbi switch (qh->type) { 361550a7375SFelipe Balbi case USB_ENDPOINT_XFER_BULK: 362550a7375SFelipe Balbi case USB_ENDPOINT_XFER_INT: 363846099a6SSergei Shtylyov musb_save_toggle(qh, is_in, urb); 364550a7375SFelipe Balbi break; 365550a7375SFelipe Balbi case USB_ENDPOINT_XFER_ISOC: 3661fe975f9SSergei Shtylyov if (status == 0 && urb->error_count) 367550a7375SFelipe Balbi status = -EXDEV; 368550a7375SFelipe Balbi break; 369550a7375SFelipe Balbi } 370550a7375SFelipe Balbi 371550a7375SFelipe Balbi qh->is_ready = 0; 372c9cd06b3SSergei Shtylyov musb_giveback(musb, urb, status); 373550a7375SFelipe Balbi qh->is_ready = ready; 374550a7375SFelipe Balbi 375550a7375SFelipe Balbi /* reclaim resources (and bandwidth) ASAP; deschedule it, and 376550a7375SFelipe Balbi * invalidate qh as soon as list_empty(&hep->urb_list) 377550a7375SFelipe Balbi */ 378550a7375SFelipe Balbi if (list_empty(&qh->hep->urb_list)) { 379550a7375SFelipe Balbi struct list_head *head; 3808c778db9SAjay Kumar Gupta struct dma_controller *dma = musb->dma_controller; 381550a7375SFelipe Balbi 3828c778db9SAjay Kumar Gupta if (is_in) { 383550a7375SFelipe Balbi ep->rx_reinit = 1; 3848c778db9SAjay Kumar Gupta if (ep->rx_channel) { 3858c778db9SAjay Kumar Gupta dma->channel_release(ep->rx_channel); 3868c778db9SAjay Kumar Gupta ep->rx_channel = NULL; 3878c778db9SAjay Kumar Gupta } 3888c778db9SAjay Kumar Gupta } else { 389550a7375SFelipe Balbi ep->tx_reinit = 1; 3908c778db9SAjay Kumar Gupta if (ep->tx_channel) { 3918c778db9SAjay Kumar Gupta dma->channel_release(ep->tx_channel); 3928c778db9SAjay Kumar Gupta ep->tx_channel = NULL; 3938c778db9SAjay Kumar Gupta } 3948c778db9SAjay Kumar Gupta } 395550a7375SFelipe Balbi 3963e5c6dc7SSergei Shtylyov /* Clobber old pointers to this qh */ 3973e5c6dc7SSergei Shtylyov musb_ep_set_qh(ep, is_in, NULL); 398550a7375SFelipe Balbi qh->hep->hcpriv = NULL; 399550a7375SFelipe Balbi 400550a7375SFelipe Balbi switch (qh->type) { 401550a7375SFelipe Balbi 40223d15e07SAjay Kumar Gupta case USB_ENDPOINT_XFER_CONTROL: 40323d15e07SAjay Kumar Gupta case USB_ENDPOINT_XFER_BULK: 40423d15e07SAjay Kumar Gupta /* fifo policy for these lists, except that NAKing 40523d15e07SAjay Kumar Gupta * should rotate a qh to the end (for fairness). 40623d15e07SAjay Kumar Gupta */ 40723d15e07SAjay Kumar Gupta if (qh->mux == 1) { 40823d15e07SAjay Kumar Gupta head = qh->ring.prev; 40923d15e07SAjay Kumar Gupta list_del(&qh->ring); 41023d15e07SAjay Kumar Gupta kfree(qh); 41123d15e07SAjay Kumar Gupta qh = first_qh(head); 41223d15e07SAjay Kumar Gupta break; 41323d15e07SAjay Kumar Gupta } 41423d15e07SAjay Kumar Gupta 415550a7375SFelipe Balbi case USB_ENDPOINT_XFER_ISOC: 416550a7375SFelipe Balbi case USB_ENDPOINT_XFER_INT: 417550a7375SFelipe Balbi /* this is where periodic bandwidth should be 418550a7375SFelipe Balbi * de-allocated if it's tracked and allocated; 419550a7375SFelipe Balbi * and where we'd update the schedule tree... 420550a7375SFelipe Balbi */ 421550a7375SFelipe Balbi kfree(qh); 422550a7375SFelipe Balbi qh = NULL; 423550a7375SFelipe Balbi break; 424550a7375SFelipe Balbi } 425550a7375SFelipe Balbi } 426550a7375SFelipe Balbi 427a2fd814eSSergei Shtylyov if (qh != NULL && qh->is_ready) { 4285c8a86e1SFelipe Balbi dev_dbg(musb->controller, "... next ep%d %cX urb %p\n", 429c9cd06b3SSergei Shtylyov hw_ep->epnum, is_in ? 'R' : 'T', next_urb(qh)); 430550a7375SFelipe Balbi musb_start_urb(musb, is_in, qh); 431550a7375SFelipe Balbi } 432550a7375SFelipe Balbi } 433550a7375SFelipe Balbi 434c767c1c6SDavid Brownell static u16 musb_h_flush_rxfifo(struct musb_hw_ep *hw_ep, u16 csr) 435550a7375SFelipe Balbi { 436550a7375SFelipe Balbi /* we don't want fifo to fill itself again; 437550a7375SFelipe Balbi * ignore dma (various models), 438550a7375SFelipe Balbi * leave toggle alone (may not have been saved yet) 439550a7375SFelipe Balbi */ 440550a7375SFelipe Balbi csr |= MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_RXPKTRDY; 441550a7375SFelipe Balbi csr &= ~(MUSB_RXCSR_H_REQPKT 442550a7375SFelipe Balbi | MUSB_RXCSR_H_AUTOREQ 443550a7375SFelipe Balbi | MUSB_RXCSR_AUTOCLEAR); 444550a7375SFelipe Balbi 445550a7375SFelipe Balbi /* write 2x to allow double buffering */ 446550a7375SFelipe Balbi musb_writew(hw_ep->regs, MUSB_RXCSR, csr); 447550a7375SFelipe Balbi musb_writew(hw_ep->regs, MUSB_RXCSR, csr); 448550a7375SFelipe Balbi 449550a7375SFelipe Balbi /* flush writebuffer */ 450550a7375SFelipe Balbi return musb_readw(hw_ep->regs, MUSB_RXCSR); 451550a7375SFelipe Balbi } 452550a7375SFelipe Balbi 453550a7375SFelipe Balbi /* 454550a7375SFelipe Balbi * PIO RX for a packet (or part of it). 455550a7375SFelipe Balbi */ 456550a7375SFelipe Balbi static bool 457550a7375SFelipe Balbi musb_host_packet_rx(struct musb *musb, struct urb *urb, u8 epnum, u8 iso_err) 458550a7375SFelipe Balbi { 459550a7375SFelipe Balbi u16 rx_count; 460550a7375SFelipe Balbi u8 *buf; 461550a7375SFelipe Balbi u16 csr; 462550a7375SFelipe Balbi bool done = false; 463550a7375SFelipe Balbi u32 length; 464550a7375SFelipe Balbi int do_flush = 0; 465550a7375SFelipe Balbi struct musb_hw_ep *hw_ep = musb->endpoints + epnum; 466550a7375SFelipe Balbi void __iomem *epio = hw_ep->regs; 467550a7375SFelipe Balbi struct musb_qh *qh = hw_ep->in_qh; 468550a7375SFelipe Balbi int pipe = urb->pipe; 469550a7375SFelipe Balbi void *buffer = urb->transfer_buffer; 470550a7375SFelipe Balbi 471550a7375SFelipe Balbi /* musb_ep_select(mbase, epnum); */ 472550a7375SFelipe Balbi rx_count = musb_readw(epio, MUSB_RXCOUNT); 4735c8a86e1SFelipe Balbi dev_dbg(musb->controller, "RX%d count %d, buffer %p len %d/%d\n", epnum, rx_count, 474550a7375SFelipe Balbi urb->transfer_buffer, qh->offset, 475550a7375SFelipe Balbi urb->transfer_buffer_length); 476550a7375SFelipe Balbi 477550a7375SFelipe Balbi /* unload FIFO */ 478550a7375SFelipe Balbi if (usb_pipeisoc(pipe)) { 479550a7375SFelipe Balbi int status = 0; 480550a7375SFelipe Balbi struct usb_iso_packet_descriptor *d; 481550a7375SFelipe Balbi 482550a7375SFelipe Balbi if (iso_err) { 483550a7375SFelipe Balbi status = -EILSEQ; 484550a7375SFelipe Balbi urb->error_count++; 485550a7375SFelipe Balbi } 486550a7375SFelipe Balbi 487550a7375SFelipe Balbi d = urb->iso_frame_desc + qh->iso_idx; 488550a7375SFelipe Balbi buf = buffer + d->offset; 489550a7375SFelipe Balbi length = d->length; 490550a7375SFelipe Balbi if (rx_count > length) { 491550a7375SFelipe Balbi if (status == 0) { 492550a7375SFelipe Balbi status = -EOVERFLOW; 493550a7375SFelipe Balbi urb->error_count++; 494550a7375SFelipe Balbi } 4955c8a86e1SFelipe Balbi dev_dbg(musb->controller, "** OVERFLOW %d into %d\n", rx_count, length); 496550a7375SFelipe Balbi do_flush = 1; 497550a7375SFelipe Balbi } else 498550a7375SFelipe Balbi length = rx_count; 499550a7375SFelipe Balbi urb->actual_length += length; 500550a7375SFelipe Balbi d->actual_length = length; 501550a7375SFelipe Balbi 502550a7375SFelipe Balbi d->status = status; 503550a7375SFelipe Balbi 504550a7375SFelipe Balbi /* see if we are done */ 505550a7375SFelipe Balbi done = (++qh->iso_idx >= urb->number_of_packets); 506550a7375SFelipe Balbi } else { 507550a7375SFelipe Balbi /* non-isoch */ 508550a7375SFelipe Balbi buf = buffer + qh->offset; 509550a7375SFelipe Balbi length = urb->transfer_buffer_length - qh->offset; 510550a7375SFelipe Balbi if (rx_count > length) { 511550a7375SFelipe Balbi if (urb->status == -EINPROGRESS) 512550a7375SFelipe Balbi urb->status = -EOVERFLOW; 5135c8a86e1SFelipe Balbi dev_dbg(musb->controller, "** OVERFLOW %d into %d\n", rx_count, length); 514550a7375SFelipe Balbi do_flush = 1; 515550a7375SFelipe Balbi } else 516550a7375SFelipe Balbi length = rx_count; 517550a7375SFelipe Balbi urb->actual_length += length; 518550a7375SFelipe Balbi qh->offset += length; 519550a7375SFelipe Balbi 520550a7375SFelipe Balbi /* see if we are done */ 521550a7375SFelipe Balbi done = (urb->actual_length == urb->transfer_buffer_length) 522550a7375SFelipe Balbi || (rx_count < qh->maxpacket) 523550a7375SFelipe Balbi || (urb->status != -EINPROGRESS); 524550a7375SFelipe Balbi if (done 525550a7375SFelipe Balbi && (urb->status == -EINPROGRESS) 526550a7375SFelipe Balbi && (urb->transfer_flags & URB_SHORT_NOT_OK) 527550a7375SFelipe Balbi && (urb->actual_length 528550a7375SFelipe Balbi < urb->transfer_buffer_length)) 529550a7375SFelipe Balbi urb->status = -EREMOTEIO; 530550a7375SFelipe Balbi } 531550a7375SFelipe Balbi 532550a7375SFelipe Balbi musb_read_fifo(hw_ep, length, buf); 533550a7375SFelipe Balbi 534550a7375SFelipe Balbi csr = musb_readw(epio, MUSB_RXCSR); 535550a7375SFelipe Balbi csr |= MUSB_RXCSR_H_WZC_BITS; 536550a7375SFelipe Balbi if (unlikely(do_flush)) 537550a7375SFelipe Balbi musb_h_flush_rxfifo(hw_ep, csr); 538550a7375SFelipe Balbi else { 539550a7375SFelipe Balbi /* REVISIT this assumes AUTOCLEAR is never set */ 540550a7375SFelipe Balbi csr &= ~(MUSB_RXCSR_RXPKTRDY | MUSB_RXCSR_H_REQPKT); 541550a7375SFelipe Balbi if (!done) 542550a7375SFelipe Balbi csr |= MUSB_RXCSR_H_REQPKT; 543550a7375SFelipe Balbi musb_writew(epio, MUSB_RXCSR, csr); 544550a7375SFelipe Balbi } 545550a7375SFelipe Balbi 546550a7375SFelipe Balbi return done; 547550a7375SFelipe Balbi } 548550a7375SFelipe Balbi 549550a7375SFelipe Balbi /* we don't always need to reinit a given side of an endpoint... 550550a7375SFelipe Balbi * when we do, use tx/rx reinit routine and then construct a new CSR 551550a7375SFelipe Balbi * to address data toggle, NYET, and DMA or PIO. 552550a7375SFelipe Balbi * 553550a7375SFelipe Balbi * it's possible that driver bugs (especially for DMA) or aborting a 554550a7375SFelipe Balbi * transfer might have left the endpoint busier than it should be. 555550a7375SFelipe Balbi * the busy/not-empty tests are basically paranoia. 556550a7375SFelipe Balbi */ 557550a7375SFelipe Balbi static void 558550a7375SFelipe Balbi musb_rx_reinit(struct musb *musb, struct musb_qh *qh, struct musb_hw_ep *ep) 559550a7375SFelipe Balbi { 560550a7375SFelipe Balbi u16 csr; 561550a7375SFelipe Balbi 562550a7375SFelipe Balbi /* NOTE: we know the "rx" fifo reinit never triggers for ep0. 563550a7375SFelipe Balbi * That always uses tx_reinit since ep0 repurposes TX register 564550a7375SFelipe Balbi * offsets; the initial SETUP packet is also a kind of OUT. 565550a7375SFelipe Balbi */ 566550a7375SFelipe Balbi 567550a7375SFelipe Balbi /* if programmed for Tx, put it in RX mode */ 568550a7375SFelipe Balbi if (ep->is_shared_fifo) { 569550a7375SFelipe Balbi csr = musb_readw(ep->regs, MUSB_TXCSR); 570550a7375SFelipe Balbi if (csr & MUSB_TXCSR_MODE) { 571550a7375SFelipe Balbi musb_h_tx_flush_fifo(ep); 572b6e434a5SSergei Shtylyov csr = musb_readw(ep->regs, MUSB_TXCSR); 573550a7375SFelipe Balbi musb_writew(ep->regs, MUSB_TXCSR, 574b6e434a5SSergei Shtylyov csr | MUSB_TXCSR_FRCDATATOG); 575550a7375SFelipe Balbi } 576b6e434a5SSergei Shtylyov 577b6e434a5SSergei Shtylyov /* 578b6e434a5SSergei Shtylyov * Clear the MODE bit (and everything else) to enable Rx. 579b6e434a5SSergei Shtylyov * NOTE: we mustn't clear the DMAMODE bit before DMAENAB. 580b6e434a5SSergei Shtylyov */ 581b6e434a5SSergei Shtylyov if (csr & MUSB_TXCSR_DMAMODE) 582b6e434a5SSergei Shtylyov musb_writew(ep->regs, MUSB_TXCSR, MUSB_TXCSR_DMAMODE); 583550a7375SFelipe Balbi musb_writew(ep->regs, MUSB_TXCSR, 0); 584550a7375SFelipe Balbi 585550a7375SFelipe Balbi /* scrub all previous state, clearing toggle */ 586550a7375SFelipe Balbi } else { 587550a7375SFelipe Balbi csr = musb_readw(ep->regs, MUSB_RXCSR); 588550a7375SFelipe Balbi if (csr & MUSB_RXCSR_RXPKTRDY) 589550a7375SFelipe Balbi WARNING("rx%d, packet/%d ready?\n", ep->epnum, 590550a7375SFelipe Balbi musb_readw(ep->regs, MUSB_RXCOUNT)); 591550a7375SFelipe Balbi 592550a7375SFelipe Balbi musb_h_flush_rxfifo(ep, MUSB_RXCSR_CLRDATATOG); 593550a7375SFelipe Balbi } 594550a7375SFelipe Balbi 595550a7375SFelipe Balbi /* target addr and (for multipoint) hub addr/port */ 596550a7375SFelipe Balbi if (musb->is_multipoint) { 597c6cf8b00SBryan Wu musb_write_rxfunaddr(ep->target_regs, qh->addr_reg); 598c6cf8b00SBryan Wu musb_write_rxhubaddr(ep->target_regs, qh->h_addr_reg); 599c6cf8b00SBryan Wu musb_write_rxhubport(ep->target_regs, qh->h_port_reg); 600c6cf8b00SBryan Wu 601550a7375SFelipe Balbi } else 602550a7375SFelipe Balbi musb_writeb(musb->mregs, MUSB_FADDR, qh->addr_reg); 603550a7375SFelipe Balbi 604550a7375SFelipe Balbi /* protocol/endpoint, interval/NAKlimit, i/o size */ 605550a7375SFelipe Balbi musb_writeb(ep->regs, MUSB_RXTYPE, qh->type_reg); 606550a7375SFelipe Balbi musb_writeb(ep->regs, MUSB_RXINTERVAL, qh->intv_reg); 607550a7375SFelipe Balbi /* NOTE: bulk combining rewrites high bits of maxpacket */ 6089f445cb2SCliff Cai /* Set RXMAXP with the FIFO size of the endpoint 6099f445cb2SCliff Cai * to disable double buffer mode. 6109f445cb2SCliff Cai */ 61106624818SFelipe Balbi if (musb->double_buffer_not_ok) 6129f445cb2SCliff Cai musb_writew(ep->regs, MUSB_RXMAXP, ep->max_packet_sz_rx); 6139f445cb2SCliff Cai else 614a483d706SAjay Kumar Gupta musb_writew(ep->regs, MUSB_RXMAXP, 615a483d706SAjay Kumar Gupta qh->maxpacket | ((qh->hb_mult - 1) << 11)); 616550a7375SFelipe Balbi 617550a7375SFelipe Balbi ep->rx_reinit = 0; 618550a7375SFelipe Balbi } 619550a7375SFelipe Balbi 620754fe4a9STony Lindgren static int musb_tx_dma_set_mode_mentor(struct dma_controller *dma, 6216b6e9710SSergei Shtylyov struct musb_hw_ep *hw_ep, struct musb_qh *qh, 622754fe4a9STony Lindgren struct urb *urb, u32 offset, 623754fe4a9STony Lindgren u32 *length, u8 *mode) 6246b6e9710SSergei Shtylyov { 6256b6e9710SSergei Shtylyov struct dma_channel *channel = hw_ep->tx_channel; 6266b6e9710SSergei Shtylyov void __iomem *epio = hw_ep->regs; 6276b6e9710SSergei Shtylyov u16 pkt_size = qh->maxpacket; 6286b6e9710SSergei Shtylyov u16 csr; 6296b6e9710SSergei Shtylyov 630754fe4a9STony Lindgren if (*length > channel->max_len) 631754fe4a9STony Lindgren *length = channel->max_len; 6326b6e9710SSergei Shtylyov 6336b6e9710SSergei Shtylyov csr = musb_readw(epio, MUSB_TXCSR); 634754fe4a9STony Lindgren if (*length > pkt_size) { 635754fe4a9STony Lindgren *mode = 1; 636a483d706SAjay Kumar Gupta csr |= MUSB_TXCSR_DMAMODE | MUSB_TXCSR_DMAENAB; 637a483d706SAjay Kumar Gupta /* autoset shouldn't be set in high bandwidth */ 638f2786281Ssupriya karanth /* 639f2786281Ssupriya karanth * Enable Autoset according to table 640f2786281Ssupriya karanth * below 641f2786281Ssupriya karanth * bulk_split hb_mult Autoset_Enable 642f2786281Ssupriya karanth * 0 1 Yes(Normal) 643f2786281Ssupriya karanth * 0 >1 No(High BW ISO) 644f2786281Ssupriya karanth * 1 1 Yes(HS bulk) 645f2786281Ssupriya karanth * 1 >1 Yes(FS bulk) 646f2786281Ssupriya karanth */ 647f2786281Ssupriya karanth if (qh->hb_mult == 1 || (qh->hb_mult > 1 && 648f2786281Ssupriya karanth can_bulk_split(hw_ep->musb, qh->type))) 649a483d706SAjay Kumar Gupta csr |= MUSB_TXCSR_AUTOSET; 6506b6e9710SSergei Shtylyov } else { 651754fe4a9STony Lindgren *mode = 0; 6526b6e9710SSergei Shtylyov csr &= ~(MUSB_TXCSR_AUTOSET | MUSB_TXCSR_DMAMODE); 6536b6e9710SSergei Shtylyov csr |= MUSB_TXCSR_DMAENAB; /* against programmer's guide */ 6546b6e9710SSergei Shtylyov } 6556b6e9710SSergei Shtylyov channel->desired_mode = mode; 6566b6e9710SSergei Shtylyov musb_writew(epio, MUSB_TXCSR, csr); 657754fe4a9STony Lindgren 658754fe4a9STony Lindgren return 0; 659754fe4a9STony Lindgren } 660754fe4a9STony Lindgren 661754fe4a9STony Lindgren static int musb_tx_dma_set_mode_cppi_tusb(struct dma_controller *dma, 662754fe4a9STony Lindgren struct musb_hw_ep *hw_ep, 663754fe4a9STony Lindgren struct musb_qh *qh, 664754fe4a9STony Lindgren struct urb *urb, 665754fe4a9STony Lindgren u32 offset, 666754fe4a9STony Lindgren u32 *length, 667754fe4a9STony Lindgren u8 *mode) 668754fe4a9STony Lindgren { 669754fe4a9STony Lindgren struct dma_channel *channel = hw_ep->tx_channel; 670754fe4a9STony Lindgren 671f8e9f34fSTony Lindgren if (!is_cppi_enabled(hw_ep->musb) && !tusb_dma_omap(hw_ep->musb)) 672754fe4a9STony Lindgren return -ENODEV; 6736b6e9710SSergei Shtylyov 6746b6e9710SSergei Shtylyov channel->actual_len = 0; 6756b6e9710SSergei Shtylyov 6766b6e9710SSergei Shtylyov /* 6776b6e9710SSergei Shtylyov * TX uses "RNDIS" mode automatically but needs help 6786b6e9710SSergei Shtylyov * to identify the zero-length-final-packet case. 6796b6e9710SSergei Shtylyov */ 680754fe4a9STony Lindgren *mode = (urb->transfer_flags & URB_ZERO_PACKET) ? 1 : 0; 681754fe4a9STony Lindgren 682754fe4a9STony Lindgren return 0; 683754fe4a9STony Lindgren } 684754fe4a9STony Lindgren 685754fe4a9STony Lindgren static bool musb_tx_dma_program(struct dma_controller *dma, 686754fe4a9STony Lindgren struct musb_hw_ep *hw_ep, struct musb_qh *qh, 687754fe4a9STony Lindgren struct urb *urb, u32 offset, u32 length) 688754fe4a9STony Lindgren { 689754fe4a9STony Lindgren struct dma_channel *channel = hw_ep->tx_channel; 690754fe4a9STony Lindgren u16 pkt_size = qh->maxpacket; 691754fe4a9STony Lindgren u8 mode; 692754fe4a9STony Lindgren int res; 693754fe4a9STony Lindgren 694754fe4a9STony Lindgren if (musb_dma_inventra(hw_ep->musb) || musb_dma_ux500(hw_ep->musb)) 695754fe4a9STony Lindgren res = musb_tx_dma_set_mode_mentor(dma, hw_ep, qh, urb, 696754fe4a9STony Lindgren offset, &length, &mode); 697754fe4a9STony Lindgren else 698754fe4a9STony Lindgren res = musb_tx_dma_set_mode_cppi_tusb(dma, hw_ep, qh, urb, 699754fe4a9STony Lindgren offset, &length, &mode); 700754fe4a9STony Lindgren if (res) 701754fe4a9STony Lindgren return false; 7026b6e9710SSergei Shtylyov 7036b6e9710SSergei Shtylyov qh->segsize = length; 7046b6e9710SSergei Shtylyov 7054c647338SSantosh Shilimkar /* 7064c647338SSantosh Shilimkar * Ensure the data reaches to main memory before starting 7074c647338SSantosh Shilimkar * DMA transfer 7084c647338SSantosh Shilimkar */ 7094c647338SSantosh Shilimkar wmb(); 7104c647338SSantosh Shilimkar 7116b6e9710SSergei Shtylyov if (!dma->channel_program(channel, pkt_size, mode, 7126b6e9710SSergei Shtylyov urb->transfer_dma + offset, length)) { 713754fe4a9STony Lindgren void __iomem *epio = hw_ep->regs; 714754fe4a9STony Lindgren u16 csr; 715754fe4a9STony Lindgren 7166b6e9710SSergei Shtylyov dma->channel_release(channel); 7176b6e9710SSergei Shtylyov hw_ep->tx_channel = NULL; 7186b6e9710SSergei Shtylyov 7196b6e9710SSergei Shtylyov csr = musb_readw(epio, MUSB_TXCSR); 7206b6e9710SSergei Shtylyov csr &= ~(MUSB_TXCSR_AUTOSET | MUSB_TXCSR_DMAENAB); 7216b6e9710SSergei Shtylyov musb_writew(epio, MUSB_TXCSR, csr | MUSB_TXCSR_H_WZC_BITS); 7226b6e9710SSergei Shtylyov return false; 7236b6e9710SSergei Shtylyov } 7246b6e9710SSergei Shtylyov return true; 7256b6e9710SSergei Shtylyov } 726550a7375SFelipe Balbi 727550a7375SFelipe Balbi /* 728550a7375SFelipe Balbi * Program an HDRC endpoint as per the given URB 729550a7375SFelipe Balbi * Context: irqs blocked, controller lock held 730550a7375SFelipe Balbi */ 731550a7375SFelipe Balbi static void musb_ep_program(struct musb *musb, u8 epnum, 7326b6e9710SSergei Shtylyov struct urb *urb, int is_out, 7336b6e9710SSergei Shtylyov u8 *buf, u32 offset, u32 len) 734550a7375SFelipe Balbi { 735550a7375SFelipe Balbi struct dma_controller *dma_controller; 736550a7375SFelipe Balbi struct dma_channel *dma_channel; 737550a7375SFelipe Balbi u8 dma_ok; 738550a7375SFelipe Balbi void __iomem *mbase = musb->mregs; 739550a7375SFelipe Balbi struct musb_hw_ep *hw_ep = musb->endpoints + epnum; 740550a7375SFelipe Balbi void __iomem *epio = hw_ep->regs; 7413e5c6dc7SSergei Shtylyov struct musb_qh *qh = musb_ep_get_qh(hw_ep, !is_out); 7423e5c6dc7SSergei Shtylyov u16 packet_sz = qh->maxpacket; 7433132122cSAjay Kumar Gupta u8 use_dma = 1; 7443132122cSAjay Kumar Gupta u16 csr; 745550a7375SFelipe Balbi 7465c8a86e1SFelipe Balbi dev_dbg(musb->controller, "%s hw%d urb %p spd%d dev%d ep%d%s " 747550a7375SFelipe Balbi "h_addr%02x h_port%02x bytes %d\n", 748550a7375SFelipe Balbi is_out ? "-->" : "<--", 749550a7375SFelipe Balbi epnum, urb, urb->dev->speed, 750550a7375SFelipe Balbi qh->addr_reg, qh->epnum, is_out ? "out" : "in", 751550a7375SFelipe Balbi qh->h_addr_reg, qh->h_port_reg, 752550a7375SFelipe Balbi len); 753550a7375SFelipe Balbi 754550a7375SFelipe Balbi musb_ep_select(mbase, epnum); 755550a7375SFelipe Balbi 7563132122cSAjay Kumar Gupta if (is_out && !len) { 7573132122cSAjay Kumar Gupta use_dma = 0; 7583132122cSAjay Kumar Gupta csr = musb_readw(epio, MUSB_TXCSR); 7593132122cSAjay Kumar Gupta csr &= ~MUSB_TXCSR_DMAENAB; 7603132122cSAjay Kumar Gupta musb_writew(epio, MUSB_TXCSR, csr); 7613132122cSAjay Kumar Gupta hw_ep->tx_channel = NULL; 7623132122cSAjay Kumar Gupta } 7633132122cSAjay Kumar Gupta 764550a7375SFelipe Balbi /* candidate for DMA? */ 765550a7375SFelipe Balbi dma_controller = musb->dma_controller; 7663132122cSAjay Kumar Gupta if (use_dma && is_dma_capable() && epnum && dma_controller) { 767550a7375SFelipe Balbi dma_channel = is_out ? hw_ep->tx_channel : hw_ep->rx_channel; 768550a7375SFelipe Balbi if (!dma_channel) { 769550a7375SFelipe Balbi dma_channel = dma_controller->channel_alloc( 770550a7375SFelipe Balbi dma_controller, hw_ep, is_out); 771550a7375SFelipe Balbi if (is_out) 772550a7375SFelipe Balbi hw_ep->tx_channel = dma_channel; 773550a7375SFelipe Balbi else 774550a7375SFelipe Balbi hw_ep->rx_channel = dma_channel; 775550a7375SFelipe Balbi } 776550a7375SFelipe Balbi } else 777550a7375SFelipe Balbi dma_channel = NULL; 778550a7375SFelipe Balbi 779550a7375SFelipe Balbi /* make sure we clear DMAEnab, autoSet bits from previous run */ 780550a7375SFelipe Balbi 781550a7375SFelipe Balbi /* OUT/transmit/EP0 or IN/receive? */ 782550a7375SFelipe Balbi if (is_out) { 783550a7375SFelipe Balbi u16 csr; 784550a7375SFelipe Balbi u16 int_txe; 785550a7375SFelipe Balbi u16 load_count; 786550a7375SFelipe Balbi 787550a7375SFelipe Balbi csr = musb_readw(epio, MUSB_TXCSR); 788550a7375SFelipe Balbi 789550a7375SFelipe Balbi /* disable interrupt in case we flush */ 790b18d26f6SSebastian Andrzej Siewior int_txe = musb->intrtxe; 791550a7375SFelipe Balbi musb_writew(mbase, MUSB_INTRTXE, int_txe & ~(1 << epnum)); 792550a7375SFelipe Balbi 793550a7375SFelipe Balbi /* general endpoint setup */ 794550a7375SFelipe Balbi if (epnum) { 795550a7375SFelipe Balbi /* flush all old state, set default */ 796a70b8442Ssupriya karanth /* 797a70b8442Ssupriya karanth * We could be flushing valid 798a70b8442Ssupriya karanth * packets in double buffering 799a70b8442Ssupriya karanth * case 800a70b8442Ssupriya karanth */ 801a70b8442Ssupriya karanth if (!hw_ep->tx_double_buffered) 802550a7375SFelipe Balbi musb_h_tx_flush_fifo(hw_ep); 803b6e434a5SSergei Shtylyov 804b6e434a5SSergei Shtylyov /* 805b6e434a5SSergei Shtylyov * We must not clear the DMAMODE bit before or in 806b6e434a5SSergei Shtylyov * the same cycle with the DMAENAB bit, so we clear 807b6e434a5SSergei Shtylyov * the latter first... 808b6e434a5SSergei Shtylyov */ 809550a7375SFelipe Balbi csr &= ~(MUSB_TXCSR_H_NAKTIMEOUT 810b6e434a5SSergei Shtylyov | MUSB_TXCSR_AUTOSET 811b6e434a5SSergei Shtylyov | MUSB_TXCSR_DMAENAB 812550a7375SFelipe Balbi | MUSB_TXCSR_FRCDATATOG 813550a7375SFelipe Balbi | MUSB_TXCSR_H_RXSTALL 814550a7375SFelipe Balbi | MUSB_TXCSR_H_ERROR 815550a7375SFelipe Balbi | MUSB_TXCSR_TXPKTRDY 816550a7375SFelipe Balbi ); 817550a7375SFelipe Balbi csr |= MUSB_TXCSR_MODE; 818550a7375SFelipe Balbi 819a70b8442Ssupriya karanth if (!hw_ep->tx_double_buffered) { 820b6e434a5SSergei Shtylyov if (usb_gettoggle(urb->dev, qh->epnum, 1)) 821550a7375SFelipe Balbi csr |= MUSB_TXCSR_H_WR_DATATOGGLE 822550a7375SFelipe Balbi | MUSB_TXCSR_H_DATATOGGLE; 823550a7375SFelipe Balbi else 824550a7375SFelipe Balbi csr |= MUSB_TXCSR_CLRDATATOG; 825a70b8442Ssupriya karanth } 826550a7375SFelipe Balbi 827550a7375SFelipe Balbi musb_writew(epio, MUSB_TXCSR, csr); 828550a7375SFelipe Balbi /* REVISIT may need to clear FLUSHFIFO ... */ 829b6e434a5SSergei Shtylyov csr &= ~MUSB_TXCSR_DMAMODE; 830550a7375SFelipe Balbi musb_writew(epio, MUSB_TXCSR, csr); 831550a7375SFelipe Balbi csr = musb_readw(epio, MUSB_TXCSR); 832550a7375SFelipe Balbi } else { 833550a7375SFelipe Balbi /* endpoint 0: just flush */ 83478322c1aSDavid Brownell musb_h_ep0_flush_fifo(hw_ep); 835550a7375SFelipe Balbi } 836550a7375SFelipe Balbi 837550a7375SFelipe Balbi /* target addr and (for multipoint) hub addr/port */ 838550a7375SFelipe Balbi if (musb->is_multipoint) { 839c6cf8b00SBryan Wu musb_write_txfunaddr(mbase, epnum, qh->addr_reg); 840c6cf8b00SBryan Wu musb_write_txhubaddr(mbase, epnum, qh->h_addr_reg); 841c6cf8b00SBryan Wu musb_write_txhubport(mbase, epnum, qh->h_port_reg); 842550a7375SFelipe Balbi /* FIXME if !epnum, do the same for RX ... */ 843550a7375SFelipe Balbi } else 844550a7375SFelipe Balbi musb_writeb(mbase, MUSB_FADDR, qh->addr_reg); 845550a7375SFelipe Balbi 846550a7375SFelipe Balbi /* protocol/endpoint/interval/NAKlimit */ 847550a7375SFelipe Balbi if (epnum) { 848550a7375SFelipe Balbi musb_writeb(epio, MUSB_TXTYPE, qh->type_reg); 849f2786281Ssupriya karanth if (musb->double_buffer_not_ok) { 850550a7375SFelipe Balbi musb_writew(epio, MUSB_TXMAXP, 85106624818SFelipe Balbi hw_ep->max_packet_sz_tx); 852f2786281Ssupriya karanth } else if (can_bulk_split(musb, qh->type)) { 853f2786281Ssupriya karanth qh->hb_mult = hw_ep->max_packet_sz_tx 854f2786281Ssupriya karanth / packet_sz; 855ccc080c7SAjay Kumar Gupta musb_writew(epio, MUSB_TXMAXP, packet_sz 856f2786281Ssupriya karanth | ((qh->hb_mult) - 1) << 11); 857f2786281Ssupriya karanth } else { 858550a7375SFelipe Balbi musb_writew(epio, MUSB_TXMAXP, 85906624818SFelipe Balbi qh->maxpacket | 86006624818SFelipe Balbi ((qh->hb_mult - 1) << 11)); 861f2786281Ssupriya karanth } 862550a7375SFelipe Balbi musb_writeb(epio, MUSB_TXINTERVAL, qh->intv_reg); 863550a7375SFelipe Balbi } else { 864550a7375SFelipe Balbi musb_writeb(epio, MUSB_NAKLIMIT0, qh->intv_reg); 865550a7375SFelipe Balbi if (musb->is_multipoint) 866550a7375SFelipe Balbi musb_writeb(epio, MUSB_TYPE0, 867550a7375SFelipe Balbi qh->type_reg); 868550a7375SFelipe Balbi } 869550a7375SFelipe Balbi 870550a7375SFelipe Balbi if (can_bulk_split(musb, qh->type)) 871550a7375SFelipe Balbi load_count = min((u32) hw_ep->max_packet_sz_tx, 872550a7375SFelipe Balbi len); 873550a7375SFelipe Balbi else 874550a7375SFelipe Balbi load_count = min((u32) packet_sz, len); 875550a7375SFelipe Balbi 8766b6e9710SSergei Shtylyov if (dma_channel && musb_tx_dma_program(dma_controller, 8776b6e9710SSergei Shtylyov hw_ep, qh, urb, offset, len)) 878550a7375SFelipe Balbi load_count = 0; 879550a7375SFelipe Balbi 880550a7375SFelipe Balbi if (load_count) { 881550a7375SFelipe Balbi /* PIO to load FIFO */ 882550a7375SFelipe Balbi qh->segsize = load_count; 8838e8a5516SVirupax Sadashivpetimath if (!buf) { 8848e8a5516SVirupax Sadashivpetimath sg_miter_start(&qh->sg_miter, urb->sg, 1, 8858e8a5516SVirupax Sadashivpetimath SG_MITER_ATOMIC 8868e8a5516SVirupax Sadashivpetimath | SG_MITER_FROM_SG); 8878e8a5516SVirupax Sadashivpetimath if (!sg_miter_next(&qh->sg_miter)) { 8888e8a5516SVirupax Sadashivpetimath dev_err(musb->controller, 8898e8a5516SVirupax Sadashivpetimath "error: sg" 8908e8a5516SVirupax Sadashivpetimath "list empty\n"); 8918e8a5516SVirupax Sadashivpetimath sg_miter_stop(&qh->sg_miter); 8928e8a5516SVirupax Sadashivpetimath goto finish; 8938e8a5516SVirupax Sadashivpetimath } 8948e8a5516SVirupax Sadashivpetimath buf = qh->sg_miter.addr + urb->sg->offset + 8958e8a5516SVirupax Sadashivpetimath urb->actual_length; 8968e8a5516SVirupax Sadashivpetimath load_count = min_t(u32, load_count, 8978e8a5516SVirupax Sadashivpetimath qh->sg_miter.length); 8988e8a5516SVirupax Sadashivpetimath musb_write_fifo(hw_ep, load_count, buf); 8998e8a5516SVirupax Sadashivpetimath qh->sg_miter.consumed = load_count; 9008e8a5516SVirupax Sadashivpetimath sg_miter_stop(&qh->sg_miter); 9018e8a5516SVirupax Sadashivpetimath } else 902550a7375SFelipe Balbi musb_write_fifo(hw_ep, load_count, buf); 903550a7375SFelipe Balbi } 9048e8a5516SVirupax Sadashivpetimath finish: 905550a7375SFelipe Balbi /* re-enable interrupt */ 906550a7375SFelipe Balbi musb_writew(mbase, MUSB_INTRTXE, int_txe); 907550a7375SFelipe Balbi 908550a7375SFelipe Balbi /* IN/receive */ 909550a7375SFelipe Balbi } else { 910550a7375SFelipe Balbi u16 csr; 911550a7375SFelipe Balbi 912550a7375SFelipe Balbi if (hw_ep->rx_reinit) { 913550a7375SFelipe Balbi musb_rx_reinit(musb, qh, hw_ep); 914550a7375SFelipe Balbi 915550a7375SFelipe Balbi /* init new state: toggle and NYET, maybe DMA later */ 916550a7375SFelipe Balbi if (usb_gettoggle(urb->dev, qh->epnum, 0)) 917550a7375SFelipe Balbi csr = MUSB_RXCSR_H_WR_DATATOGGLE 918550a7375SFelipe Balbi | MUSB_RXCSR_H_DATATOGGLE; 919550a7375SFelipe Balbi else 920550a7375SFelipe Balbi csr = 0; 921550a7375SFelipe Balbi if (qh->type == USB_ENDPOINT_XFER_INT) 922550a7375SFelipe Balbi csr |= MUSB_RXCSR_DISNYET; 923550a7375SFelipe Balbi 924550a7375SFelipe Balbi } else { 925550a7375SFelipe Balbi csr = musb_readw(hw_ep->regs, MUSB_RXCSR); 926550a7375SFelipe Balbi 927550a7375SFelipe Balbi if (csr & (MUSB_RXCSR_RXPKTRDY 928550a7375SFelipe Balbi | MUSB_RXCSR_DMAENAB 929550a7375SFelipe Balbi | MUSB_RXCSR_H_REQPKT)) 930550a7375SFelipe Balbi ERR("broken !rx_reinit, ep%d csr %04x\n", 931550a7375SFelipe Balbi hw_ep->epnum, csr); 932550a7375SFelipe Balbi 933550a7375SFelipe Balbi /* scrub any stale state, leaving toggle alone */ 934550a7375SFelipe Balbi csr &= MUSB_RXCSR_DISNYET; 935550a7375SFelipe Balbi } 936550a7375SFelipe Balbi 937550a7375SFelipe Balbi /* kick things off */ 938550a7375SFelipe Balbi 939f8e9f34fSTony Lindgren if ((is_cppi_enabled(musb) || tusb_dma_omap(musb)) && dma_channel) { 940c51e36dcSSergei Shtylyov /* Candidate for DMA */ 941550a7375SFelipe Balbi dma_channel->actual_len = 0L; 942550a7375SFelipe Balbi qh->segsize = len; 943550a7375SFelipe Balbi 944550a7375SFelipe Balbi /* AUTOREQ is in a DMA register */ 945550a7375SFelipe Balbi musb_writew(hw_ep->regs, MUSB_RXCSR, csr); 946c51e36dcSSergei Shtylyov csr = musb_readw(hw_ep->regs, MUSB_RXCSR); 947550a7375SFelipe Balbi 948c51e36dcSSergei Shtylyov /* 949c51e36dcSSergei Shtylyov * Unless caller treats short RX transfers as 950550a7375SFelipe Balbi * errors, we dare not queue multiple transfers. 951550a7375SFelipe Balbi */ 952c51e36dcSSergei Shtylyov dma_ok = dma_controller->channel_program(dma_channel, 953c51e36dcSSergei Shtylyov packet_sz, !(urb->transfer_flags & 954c51e36dcSSergei Shtylyov URB_SHORT_NOT_OK), 9556b6e9710SSergei Shtylyov urb->transfer_dma + offset, 956550a7375SFelipe Balbi qh->segsize); 957550a7375SFelipe Balbi if (!dma_ok) { 958c51e36dcSSergei Shtylyov dma_controller->channel_release(dma_channel); 959c51e36dcSSergei Shtylyov hw_ep->rx_channel = dma_channel = NULL; 960550a7375SFelipe Balbi } else 961550a7375SFelipe Balbi csr |= MUSB_RXCSR_DMAENAB; 962550a7375SFelipe Balbi } 963550a7375SFelipe Balbi 964550a7375SFelipe Balbi csr |= MUSB_RXCSR_H_REQPKT; 9655c8a86e1SFelipe Balbi dev_dbg(musb->controller, "RXCSR%d := %04x\n", epnum, csr); 966550a7375SFelipe Balbi musb_writew(hw_ep->regs, MUSB_RXCSR, csr); 967550a7375SFelipe Balbi csr = musb_readw(hw_ep->regs, MUSB_RXCSR); 968550a7375SFelipe Balbi } 969550a7375SFelipe Balbi } 970550a7375SFelipe Balbi 971f283862fSAjay Kumar Gupta /* Schedule next QH from musb->in_bulk/out_bulk and move the current qh to 972f283862fSAjay Kumar Gupta * the end; avoids starvation for other endpoints. 973f283862fSAjay Kumar Gupta */ 974f283862fSAjay Kumar Gupta static void musb_bulk_nak_timeout(struct musb *musb, struct musb_hw_ep *ep, 975f283862fSAjay Kumar Gupta int is_in) 976f283862fSAjay Kumar Gupta { 977f283862fSAjay Kumar Gupta struct dma_channel *dma; 978f283862fSAjay Kumar Gupta struct urb *urb; 979f283862fSAjay Kumar Gupta void __iomem *mbase = musb->mregs; 980f283862fSAjay Kumar Gupta void __iomem *epio = ep->regs; 981f283862fSAjay Kumar Gupta struct musb_qh *cur_qh, *next_qh; 982f283862fSAjay Kumar Gupta u16 rx_csr, tx_csr; 983f283862fSAjay Kumar Gupta 984f283862fSAjay Kumar Gupta musb_ep_select(mbase, ep->epnum); 985f283862fSAjay Kumar Gupta if (is_in) { 986f283862fSAjay Kumar Gupta dma = is_dma_capable() ? ep->rx_channel : NULL; 987f283862fSAjay Kumar Gupta 988f283862fSAjay Kumar Gupta /* clear nak timeout bit */ 989f283862fSAjay Kumar Gupta rx_csr = musb_readw(epio, MUSB_RXCSR); 990f283862fSAjay Kumar Gupta rx_csr |= MUSB_RXCSR_H_WZC_BITS; 991f283862fSAjay Kumar Gupta rx_csr &= ~MUSB_RXCSR_DATAERROR; 992f283862fSAjay Kumar Gupta musb_writew(epio, MUSB_RXCSR, rx_csr); 993f283862fSAjay Kumar Gupta 994f283862fSAjay Kumar Gupta cur_qh = first_qh(&musb->in_bulk); 995f283862fSAjay Kumar Gupta } else { 996f283862fSAjay Kumar Gupta dma = is_dma_capable() ? ep->tx_channel : NULL; 997f283862fSAjay Kumar Gupta 998f283862fSAjay Kumar Gupta /* clear nak timeout bit */ 999f283862fSAjay Kumar Gupta tx_csr = musb_readw(epio, MUSB_TXCSR); 1000f283862fSAjay Kumar Gupta tx_csr |= MUSB_TXCSR_H_WZC_BITS; 1001f283862fSAjay Kumar Gupta tx_csr &= ~MUSB_TXCSR_H_NAKTIMEOUT; 1002f283862fSAjay Kumar Gupta musb_writew(epio, MUSB_TXCSR, tx_csr); 1003f283862fSAjay Kumar Gupta 1004f283862fSAjay Kumar Gupta cur_qh = first_qh(&musb->out_bulk); 1005f283862fSAjay Kumar Gupta } 1006f283862fSAjay Kumar Gupta if (cur_qh) { 1007f283862fSAjay Kumar Gupta urb = next_urb(cur_qh); 1008f283862fSAjay Kumar Gupta if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) { 1009f283862fSAjay Kumar Gupta dma->status = MUSB_DMA_STATUS_CORE_ABORT; 1010f283862fSAjay Kumar Gupta musb->dma_controller->channel_abort(dma); 1011f283862fSAjay Kumar Gupta urb->actual_length += dma->actual_len; 1012f283862fSAjay Kumar Gupta dma->actual_len = 0L; 1013f283862fSAjay Kumar Gupta } 1014f283862fSAjay Kumar Gupta musb_save_toggle(cur_qh, is_in, urb); 1015f283862fSAjay Kumar Gupta 1016f283862fSAjay Kumar Gupta if (is_in) { 1017f283862fSAjay Kumar Gupta /* move cur_qh to end of queue */ 1018f283862fSAjay Kumar Gupta list_move_tail(&cur_qh->ring, &musb->in_bulk); 1019f283862fSAjay Kumar Gupta 1020f283862fSAjay Kumar Gupta /* get the next qh from musb->in_bulk */ 1021f283862fSAjay Kumar Gupta next_qh = first_qh(&musb->in_bulk); 1022f283862fSAjay Kumar Gupta 1023f283862fSAjay Kumar Gupta /* set rx_reinit and schedule the next qh */ 1024f283862fSAjay Kumar Gupta ep->rx_reinit = 1; 1025f283862fSAjay Kumar Gupta } else { 1026f283862fSAjay Kumar Gupta /* move cur_qh to end of queue */ 1027f283862fSAjay Kumar Gupta list_move_tail(&cur_qh->ring, &musb->out_bulk); 1028f283862fSAjay Kumar Gupta 1029f283862fSAjay Kumar Gupta /* get the next qh from musb->out_bulk */ 1030f283862fSAjay Kumar Gupta next_qh = first_qh(&musb->out_bulk); 1031f283862fSAjay Kumar Gupta 1032f283862fSAjay Kumar Gupta /* set tx_reinit and schedule the next qh */ 1033f283862fSAjay Kumar Gupta ep->tx_reinit = 1; 1034f283862fSAjay Kumar Gupta } 1035f283862fSAjay Kumar Gupta musb_start_urb(musb, is_in, next_qh); 1036f283862fSAjay Kumar Gupta } 1037f283862fSAjay Kumar Gupta } 1038550a7375SFelipe Balbi 1039550a7375SFelipe Balbi /* 1040550a7375SFelipe Balbi * Service the default endpoint (ep0) as host. 1041550a7375SFelipe Balbi * Return true until it's time to start the status stage. 1042550a7375SFelipe Balbi */ 1043550a7375SFelipe Balbi static bool musb_h_ep0_continue(struct musb *musb, u16 len, struct urb *urb) 1044550a7375SFelipe Balbi { 1045550a7375SFelipe Balbi bool more = false; 1046550a7375SFelipe Balbi u8 *fifo_dest = NULL; 1047550a7375SFelipe Balbi u16 fifo_count = 0; 1048550a7375SFelipe Balbi struct musb_hw_ep *hw_ep = musb->control_ep; 1049550a7375SFelipe Balbi struct musb_qh *qh = hw_ep->in_qh; 1050550a7375SFelipe Balbi struct usb_ctrlrequest *request; 1051550a7375SFelipe Balbi 1052550a7375SFelipe Balbi switch (musb->ep0_stage) { 1053550a7375SFelipe Balbi case MUSB_EP0_IN: 1054550a7375SFelipe Balbi fifo_dest = urb->transfer_buffer + urb->actual_length; 10553ecdb9acSSergei Shtylyov fifo_count = min_t(size_t, len, urb->transfer_buffer_length - 10563ecdb9acSSergei Shtylyov urb->actual_length); 1057550a7375SFelipe Balbi if (fifo_count < len) 1058550a7375SFelipe Balbi urb->status = -EOVERFLOW; 1059550a7375SFelipe Balbi 1060550a7375SFelipe Balbi musb_read_fifo(hw_ep, fifo_count, fifo_dest); 1061550a7375SFelipe Balbi 1062550a7375SFelipe Balbi urb->actual_length += fifo_count; 1063550a7375SFelipe Balbi if (len < qh->maxpacket) { 1064550a7375SFelipe Balbi /* always terminate on short read; it's 1065550a7375SFelipe Balbi * rarely reported as an error. 1066550a7375SFelipe Balbi */ 1067550a7375SFelipe Balbi } else if (urb->actual_length < 1068550a7375SFelipe Balbi urb->transfer_buffer_length) 1069550a7375SFelipe Balbi more = true; 1070550a7375SFelipe Balbi break; 1071550a7375SFelipe Balbi case MUSB_EP0_START: 1072550a7375SFelipe Balbi request = (struct usb_ctrlrequest *) urb->setup_packet; 1073550a7375SFelipe Balbi 1074550a7375SFelipe Balbi if (!request->wLength) { 10755c8a86e1SFelipe Balbi dev_dbg(musb->controller, "start no-DATA\n"); 1076550a7375SFelipe Balbi break; 1077550a7375SFelipe Balbi } else if (request->bRequestType & USB_DIR_IN) { 10785c8a86e1SFelipe Balbi dev_dbg(musb->controller, "start IN-DATA\n"); 1079550a7375SFelipe Balbi musb->ep0_stage = MUSB_EP0_IN; 1080550a7375SFelipe Balbi more = true; 1081550a7375SFelipe Balbi break; 1082550a7375SFelipe Balbi } else { 10835c8a86e1SFelipe Balbi dev_dbg(musb->controller, "start OUT-DATA\n"); 1084550a7375SFelipe Balbi musb->ep0_stage = MUSB_EP0_OUT; 1085550a7375SFelipe Balbi more = true; 1086550a7375SFelipe Balbi } 1087550a7375SFelipe Balbi /* FALLTHROUGH */ 1088550a7375SFelipe Balbi case MUSB_EP0_OUT: 10893ecdb9acSSergei Shtylyov fifo_count = min_t(size_t, qh->maxpacket, 10903ecdb9acSSergei Shtylyov urb->transfer_buffer_length - 10913ecdb9acSSergei Shtylyov urb->actual_length); 1092550a7375SFelipe Balbi if (fifo_count) { 1093550a7375SFelipe Balbi fifo_dest = (u8 *) (urb->transfer_buffer 1094550a7375SFelipe Balbi + urb->actual_length); 10955c8a86e1SFelipe Balbi dev_dbg(musb->controller, "Sending %d byte%s to ep0 fifo %p\n", 1096bb1c9ef1SDavid Brownell fifo_count, 1097bb1c9ef1SDavid Brownell (fifo_count == 1) ? "" : "s", 1098bb1c9ef1SDavid Brownell fifo_dest); 1099550a7375SFelipe Balbi musb_write_fifo(hw_ep, fifo_count, fifo_dest); 1100550a7375SFelipe Balbi 1101550a7375SFelipe Balbi urb->actual_length += fifo_count; 1102550a7375SFelipe Balbi more = true; 1103550a7375SFelipe Balbi } 1104550a7375SFelipe Balbi break; 1105550a7375SFelipe Balbi default: 1106550a7375SFelipe Balbi ERR("bogus ep0 stage %d\n", musb->ep0_stage); 1107550a7375SFelipe Balbi break; 1108550a7375SFelipe Balbi } 1109550a7375SFelipe Balbi 1110550a7375SFelipe Balbi return more; 1111550a7375SFelipe Balbi } 1112550a7375SFelipe Balbi 1113550a7375SFelipe Balbi /* 1114550a7375SFelipe Balbi * Handle default endpoint interrupt as host. Only called in IRQ time 1115c767c1c6SDavid Brownell * from musb_interrupt(). 1116550a7375SFelipe Balbi * 1117550a7375SFelipe Balbi * called with controller irqlocked 1118550a7375SFelipe Balbi */ 1119550a7375SFelipe Balbi irqreturn_t musb_h_ep0_irq(struct musb *musb) 1120550a7375SFelipe Balbi { 1121550a7375SFelipe Balbi struct urb *urb; 1122550a7375SFelipe Balbi u16 csr, len; 1123550a7375SFelipe Balbi int status = 0; 1124550a7375SFelipe Balbi void __iomem *mbase = musb->mregs; 1125550a7375SFelipe Balbi struct musb_hw_ep *hw_ep = musb->control_ep; 1126550a7375SFelipe Balbi void __iomem *epio = hw_ep->regs; 1127550a7375SFelipe Balbi struct musb_qh *qh = hw_ep->in_qh; 1128550a7375SFelipe Balbi bool complete = false; 1129550a7375SFelipe Balbi irqreturn_t retval = IRQ_NONE; 1130550a7375SFelipe Balbi 1131550a7375SFelipe Balbi /* ep0 only has one queue, "in" */ 1132550a7375SFelipe Balbi urb = next_urb(qh); 1133550a7375SFelipe Balbi 1134550a7375SFelipe Balbi musb_ep_select(mbase, 0); 1135550a7375SFelipe Balbi csr = musb_readw(epio, MUSB_CSR0); 1136550a7375SFelipe Balbi len = (csr & MUSB_CSR0_RXPKTRDY) 1137550a7375SFelipe Balbi ? musb_readb(epio, MUSB_COUNT0) 1138550a7375SFelipe Balbi : 0; 1139550a7375SFelipe Balbi 11405c8a86e1SFelipe Balbi dev_dbg(musb->controller, "<== csr0 %04x, qh %p, count %d, urb %p, stage %d\n", 1141550a7375SFelipe Balbi csr, qh, len, urb, musb->ep0_stage); 1142550a7375SFelipe Balbi 1143550a7375SFelipe Balbi /* if we just did status stage, we are done */ 1144550a7375SFelipe Balbi if (MUSB_EP0_STATUS == musb->ep0_stage) { 1145550a7375SFelipe Balbi retval = IRQ_HANDLED; 1146550a7375SFelipe Balbi complete = true; 1147550a7375SFelipe Balbi } 1148550a7375SFelipe Balbi 1149550a7375SFelipe Balbi /* prepare status */ 1150550a7375SFelipe Balbi if (csr & MUSB_CSR0_H_RXSTALL) { 11515c8a86e1SFelipe Balbi dev_dbg(musb->controller, "STALLING ENDPOINT\n"); 1152550a7375SFelipe Balbi status = -EPIPE; 1153550a7375SFelipe Balbi 1154550a7375SFelipe Balbi } else if (csr & MUSB_CSR0_H_ERROR) { 11555c8a86e1SFelipe Balbi dev_dbg(musb->controller, "no response, csr0 %04x\n", csr); 1156550a7375SFelipe Balbi status = -EPROTO; 1157550a7375SFelipe Balbi 1158550a7375SFelipe Balbi } else if (csr & MUSB_CSR0_H_NAKTIMEOUT) { 11595c8a86e1SFelipe Balbi dev_dbg(musb->controller, "control NAK timeout\n"); 1160550a7375SFelipe Balbi 1161550a7375SFelipe Balbi /* NOTE: this code path would be a good place to PAUSE a 1162550a7375SFelipe Balbi * control transfer, if another one is queued, so that 11631e0320f0SAjay Kumar Gupta * ep0 is more likely to stay busy. That's already done 11641e0320f0SAjay Kumar Gupta * for bulk RX transfers. 1165550a7375SFelipe Balbi * 1166550a7375SFelipe Balbi * if (qh->ring.next != &musb->control), then 1167550a7375SFelipe Balbi * we have a candidate... NAKing is *NOT* an error 1168550a7375SFelipe Balbi */ 1169550a7375SFelipe Balbi musb_writew(epio, MUSB_CSR0, 0); 1170550a7375SFelipe Balbi retval = IRQ_HANDLED; 1171550a7375SFelipe Balbi } 1172550a7375SFelipe Balbi 1173550a7375SFelipe Balbi if (status) { 11745c8a86e1SFelipe Balbi dev_dbg(musb->controller, "aborting\n"); 1175550a7375SFelipe Balbi retval = IRQ_HANDLED; 1176550a7375SFelipe Balbi if (urb) 1177550a7375SFelipe Balbi urb->status = status; 1178550a7375SFelipe Balbi complete = true; 1179550a7375SFelipe Balbi 1180550a7375SFelipe Balbi /* use the proper sequence to abort the transfer */ 1181550a7375SFelipe Balbi if (csr & MUSB_CSR0_H_REQPKT) { 1182550a7375SFelipe Balbi csr &= ~MUSB_CSR0_H_REQPKT; 1183550a7375SFelipe Balbi musb_writew(epio, MUSB_CSR0, csr); 1184550a7375SFelipe Balbi csr &= ~MUSB_CSR0_H_NAKTIMEOUT; 1185550a7375SFelipe Balbi musb_writew(epio, MUSB_CSR0, csr); 1186550a7375SFelipe Balbi } else { 118778322c1aSDavid Brownell musb_h_ep0_flush_fifo(hw_ep); 1188550a7375SFelipe Balbi } 1189550a7375SFelipe Balbi 1190550a7375SFelipe Balbi musb_writeb(epio, MUSB_NAKLIMIT0, 0); 1191550a7375SFelipe Balbi 1192550a7375SFelipe Balbi /* clear it */ 1193550a7375SFelipe Balbi musb_writew(epio, MUSB_CSR0, 0); 1194550a7375SFelipe Balbi } 1195550a7375SFelipe Balbi 1196550a7375SFelipe Balbi if (unlikely(!urb)) { 1197550a7375SFelipe Balbi /* stop endpoint since we have no place for its data, this 1198550a7375SFelipe Balbi * SHOULD NEVER HAPPEN! */ 1199550a7375SFelipe Balbi ERR("no URB for end 0\n"); 1200550a7375SFelipe Balbi 120178322c1aSDavid Brownell musb_h_ep0_flush_fifo(hw_ep); 1202550a7375SFelipe Balbi goto done; 1203550a7375SFelipe Balbi } 1204550a7375SFelipe Balbi 1205550a7375SFelipe Balbi if (!complete) { 1206550a7375SFelipe Balbi /* call common logic and prepare response */ 1207550a7375SFelipe Balbi if (musb_h_ep0_continue(musb, len, urb)) { 1208550a7375SFelipe Balbi /* more packets required */ 1209550a7375SFelipe Balbi csr = (MUSB_EP0_IN == musb->ep0_stage) 1210550a7375SFelipe Balbi ? MUSB_CSR0_H_REQPKT : MUSB_CSR0_TXPKTRDY; 1211550a7375SFelipe Balbi } else { 1212550a7375SFelipe Balbi /* data transfer complete; perform status phase */ 1213550a7375SFelipe Balbi if (usb_pipeout(urb->pipe) 1214550a7375SFelipe Balbi || !urb->transfer_buffer_length) 1215550a7375SFelipe Balbi csr = MUSB_CSR0_H_STATUSPKT 1216550a7375SFelipe Balbi | MUSB_CSR0_H_REQPKT; 1217550a7375SFelipe Balbi else 1218550a7375SFelipe Balbi csr = MUSB_CSR0_H_STATUSPKT 1219550a7375SFelipe Balbi | MUSB_CSR0_TXPKTRDY; 1220550a7375SFelipe Balbi 12213c4653c1SAjay Kumar Gupta /* disable ping token in status phase */ 12223c4653c1SAjay Kumar Gupta csr |= MUSB_CSR0_H_DIS_PING; 12233c4653c1SAjay Kumar Gupta 1224550a7375SFelipe Balbi /* flag status stage */ 1225550a7375SFelipe Balbi musb->ep0_stage = MUSB_EP0_STATUS; 1226550a7375SFelipe Balbi 12275c8a86e1SFelipe Balbi dev_dbg(musb->controller, "ep0 STATUS, csr %04x\n", csr); 1228550a7375SFelipe Balbi 1229550a7375SFelipe Balbi } 1230550a7375SFelipe Balbi musb_writew(epio, MUSB_CSR0, csr); 1231550a7375SFelipe Balbi retval = IRQ_HANDLED; 1232550a7375SFelipe Balbi } else 1233550a7375SFelipe Balbi musb->ep0_stage = MUSB_EP0_IDLE; 1234550a7375SFelipe Balbi 1235550a7375SFelipe Balbi /* call completion handler if done */ 1236550a7375SFelipe Balbi if (complete) 1237550a7375SFelipe Balbi musb_advance_schedule(musb, urb, hw_ep, 1); 1238550a7375SFelipe Balbi done: 1239550a7375SFelipe Balbi return retval; 1240550a7375SFelipe Balbi } 1241550a7375SFelipe Balbi 1242550a7375SFelipe Balbi 1243550a7375SFelipe Balbi #ifdef CONFIG_USB_INVENTRA_DMA 1244550a7375SFelipe Balbi 1245550a7375SFelipe Balbi /* Host side TX (OUT) using Mentor DMA works as follows: 1246550a7375SFelipe Balbi submit_urb -> 1247550a7375SFelipe Balbi - if queue was empty, Program Endpoint 1248550a7375SFelipe Balbi - ... which starts DMA to fifo in mode 1 or 0 1249550a7375SFelipe Balbi 1250550a7375SFelipe Balbi DMA Isr (transfer complete) -> TxAvail() 1251550a7375SFelipe Balbi - Stop DMA (~DmaEnab) (<--- Alert ... currently happens 1252550a7375SFelipe Balbi only in musb_cleanup_urb) 1253550a7375SFelipe Balbi - TxPktRdy has to be set in mode 0 or for 1254550a7375SFelipe Balbi short packets in mode 1. 1255550a7375SFelipe Balbi */ 1256550a7375SFelipe Balbi 1257550a7375SFelipe Balbi #endif 1258550a7375SFelipe Balbi 1259550a7375SFelipe Balbi /* Service a Tx-Available or dma completion irq for the endpoint */ 1260550a7375SFelipe Balbi void musb_host_tx(struct musb *musb, u8 epnum) 1261550a7375SFelipe Balbi { 1262550a7375SFelipe Balbi int pipe; 1263550a7375SFelipe Balbi bool done = false; 1264550a7375SFelipe Balbi u16 tx_csr; 12656b6e9710SSergei Shtylyov size_t length = 0; 12666b6e9710SSergei Shtylyov size_t offset = 0; 1267550a7375SFelipe Balbi struct musb_hw_ep *hw_ep = musb->endpoints + epnum; 1268550a7375SFelipe Balbi void __iomem *epio = hw_ep->regs; 12693e5c6dc7SSergei Shtylyov struct musb_qh *qh = hw_ep->out_qh; 12703e5c6dc7SSergei Shtylyov struct urb *urb = next_urb(qh); 1271550a7375SFelipe Balbi u32 status = 0; 1272550a7375SFelipe Balbi void __iomem *mbase = musb->mregs; 1273550a7375SFelipe Balbi struct dma_channel *dma; 1274f8afbf7fST. S., Anil Kumar bool transfer_pending = false; 1275550a7375SFelipe Balbi 1276550a7375SFelipe Balbi musb_ep_select(mbase, epnum); 1277550a7375SFelipe Balbi tx_csr = musb_readw(epio, MUSB_TXCSR); 1278550a7375SFelipe Balbi 1279550a7375SFelipe Balbi /* with CPPI, DMA sometimes triggers "extra" irqs */ 1280550a7375SFelipe Balbi if (!urb) { 12815c8a86e1SFelipe Balbi dev_dbg(musb->controller, "extra TX%d ready, csr %04x\n", epnum, tx_csr); 12826b6e9710SSergei Shtylyov return; 1283550a7375SFelipe Balbi } 1284550a7375SFelipe Balbi 1285550a7375SFelipe Balbi pipe = urb->pipe; 1286550a7375SFelipe Balbi dma = is_dma_capable() ? hw_ep->tx_channel : NULL; 12875c8a86e1SFelipe Balbi dev_dbg(musb->controller, "OUT/TX%d end, csr %04x%s\n", epnum, tx_csr, 1288550a7375SFelipe Balbi dma ? ", dma" : ""); 1289550a7375SFelipe Balbi 1290550a7375SFelipe Balbi /* check for errors */ 1291550a7375SFelipe Balbi if (tx_csr & MUSB_TXCSR_H_RXSTALL) { 1292550a7375SFelipe Balbi /* dma was disabled, fifo flushed */ 12935c8a86e1SFelipe Balbi dev_dbg(musb->controller, "TX end %d stall\n", epnum); 1294550a7375SFelipe Balbi 1295550a7375SFelipe Balbi /* stall; record URB status */ 1296550a7375SFelipe Balbi status = -EPIPE; 1297550a7375SFelipe Balbi 1298550a7375SFelipe Balbi } else if (tx_csr & MUSB_TXCSR_H_ERROR) { 1299550a7375SFelipe Balbi /* (NON-ISO) dma was disabled, fifo flushed */ 13005c8a86e1SFelipe Balbi dev_dbg(musb->controller, "TX 3strikes on ep=%d\n", epnum); 1301550a7375SFelipe Balbi 1302550a7375SFelipe Balbi status = -ETIMEDOUT; 1303550a7375SFelipe Balbi 1304550a7375SFelipe Balbi } else if (tx_csr & MUSB_TXCSR_H_NAKTIMEOUT) { 1305f283862fSAjay Kumar Gupta if (USB_ENDPOINT_XFER_BULK == qh->type && qh->mux == 1 1306f283862fSAjay Kumar Gupta && !list_is_singular(&musb->out_bulk)) { 1307f283862fSAjay Kumar Gupta dev_dbg(musb->controller, 1308f283862fSAjay Kumar Gupta "NAK timeout on TX%d ep\n", epnum); 1309f283862fSAjay Kumar Gupta musb_bulk_nak_timeout(musb, hw_ep, 0); 1310f283862fSAjay Kumar Gupta } else { 1311f283862fSAjay Kumar Gupta dev_dbg(musb->controller, 1312f283862fSAjay Kumar Gupta "TX end=%d device not responding\n", epnum); 1313550a7375SFelipe Balbi /* NOTE: this code path would be a good place to PAUSE a 1314550a7375SFelipe Balbi * transfer, if there's some other (nonperiodic) tx urb 1315550a7375SFelipe Balbi * that could use this fifo. (dma complicates it...) 13161e0320f0SAjay Kumar Gupta * That's already done for bulk RX transfers. 1317550a7375SFelipe Balbi * 1318550a7375SFelipe Balbi * if (bulk && qh->ring.next != &musb->out_bulk), then 1319550a7375SFelipe Balbi * we have a candidate... NAKing is *NOT* an error 1320550a7375SFelipe Balbi */ 1321550a7375SFelipe Balbi musb_ep_select(mbase, epnum); 1322550a7375SFelipe Balbi musb_writew(epio, MUSB_TXCSR, 1323550a7375SFelipe Balbi MUSB_TXCSR_H_WZC_BITS 1324550a7375SFelipe Balbi | MUSB_TXCSR_TXPKTRDY); 1325f283862fSAjay Kumar Gupta } 13266b6e9710SSergei Shtylyov return; 1327550a7375SFelipe Balbi } 1328550a7375SFelipe Balbi 13298e8a5516SVirupax Sadashivpetimath done: 1330550a7375SFelipe Balbi if (status) { 1331550a7375SFelipe Balbi if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) { 1332550a7375SFelipe Balbi dma->status = MUSB_DMA_STATUS_CORE_ABORT; 13339c547699SDaniel Mack musb->dma_controller->channel_abort(dma); 1334550a7375SFelipe Balbi } 1335550a7375SFelipe Balbi 1336550a7375SFelipe Balbi /* do the proper sequence to abort the transfer in the 1337550a7375SFelipe Balbi * usb core; the dma engine should already be stopped. 1338550a7375SFelipe Balbi */ 1339550a7375SFelipe Balbi musb_h_tx_flush_fifo(hw_ep); 1340550a7375SFelipe Balbi tx_csr &= ~(MUSB_TXCSR_AUTOSET 1341550a7375SFelipe Balbi | MUSB_TXCSR_DMAENAB 1342550a7375SFelipe Balbi | MUSB_TXCSR_H_ERROR 1343550a7375SFelipe Balbi | MUSB_TXCSR_H_RXSTALL 1344550a7375SFelipe Balbi | MUSB_TXCSR_H_NAKTIMEOUT 1345550a7375SFelipe Balbi ); 1346550a7375SFelipe Balbi 1347550a7375SFelipe Balbi musb_ep_select(mbase, epnum); 1348550a7375SFelipe Balbi musb_writew(epio, MUSB_TXCSR, tx_csr); 1349550a7375SFelipe Balbi /* REVISIT may need to clear FLUSHFIFO ... */ 1350550a7375SFelipe Balbi musb_writew(epio, MUSB_TXCSR, tx_csr); 1351550a7375SFelipe Balbi musb_writeb(epio, MUSB_TXINTERVAL, 0); 1352550a7375SFelipe Balbi 1353550a7375SFelipe Balbi done = true; 1354550a7375SFelipe Balbi } 1355550a7375SFelipe Balbi 1356550a7375SFelipe Balbi /* second cppi case */ 1357550a7375SFelipe Balbi if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) { 13585c8a86e1SFelipe Balbi dev_dbg(musb->controller, "extra TX%d ready, csr %04x\n", epnum, tx_csr); 13596b6e9710SSergei Shtylyov return; 1360550a7375SFelipe Balbi } 1361550a7375SFelipe Balbi 1362c7bbc056SSergei Shtylyov if (is_dma_capable() && dma && !status) { 1363c7bbc056SSergei Shtylyov /* 1364c7bbc056SSergei Shtylyov * DMA has completed. But if we're using DMA mode 1 (multi 1365c7bbc056SSergei Shtylyov * packet DMA), we need a terminal TXPKTRDY interrupt before 1366c7bbc056SSergei Shtylyov * we can consider this transfer completed, lest we trash 1367c7bbc056SSergei Shtylyov * its last packet when writing the next URB's data. So we 1368c7bbc056SSergei Shtylyov * switch back to mode 0 to get that interrupt; we'll come 1369c7bbc056SSergei Shtylyov * back here once it happens. 1370c7bbc056SSergei Shtylyov */ 1371c7bbc056SSergei Shtylyov if (tx_csr & MUSB_TXCSR_DMAMODE) { 1372c7bbc056SSergei Shtylyov /* 1373c7bbc056SSergei Shtylyov * We shouldn't clear DMAMODE with DMAENAB set; so 1374c7bbc056SSergei Shtylyov * clear them in a safe order. That should be OK 1375c7bbc056SSergei Shtylyov * once TXPKTRDY has been set (and I've never seen 1376c7bbc056SSergei Shtylyov * it being 0 at this moment -- DMA interrupt latency 1377c7bbc056SSergei Shtylyov * is significant) but if it hasn't been then we have 1378c7bbc056SSergei Shtylyov * no choice but to stop being polite and ignore the 1379c7bbc056SSergei Shtylyov * programmer's guide... :-) 1380c7bbc056SSergei Shtylyov * 1381c7bbc056SSergei Shtylyov * Note that we must write TXCSR with TXPKTRDY cleared 1382c7bbc056SSergei Shtylyov * in order not to re-trigger the packet send (this bit 1383c7bbc056SSergei Shtylyov * can't be cleared by CPU), and there's another caveat: 1384c7bbc056SSergei Shtylyov * TXPKTRDY may be set shortly and then cleared in the 1385c7bbc056SSergei Shtylyov * double-buffered FIFO mode, so we do an extra TXCSR 1386c7bbc056SSergei Shtylyov * read for debouncing... 1387c7bbc056SSergei Shtylyov */ 1388c7bbc056SSergei Shtylyov tx_csr &= musb_readw(epio, MUSB_TXCSR); 1389c7bbc056SSergei Shtylyov if (tx_csr & MUSB_TXCSR_TXPKTRDY) { 1390c7bbc056SSergei Shtylyov tx_csr &= ~(MUSB_TXCSR_DMAENAB | 1391c7bbc056SSergei Shtylyov MUSB_TXCSR_TXPKTRDY); 1392c7bbc056SSergei Shtylyov musb_writew(epio, MUSB_TXCSR, 1393c7bbc056SSergei Shtylyov tx_csr | MUSB_TXCSR_H_WZC_BITS); 1394c7bbc056SSergei Shtylyov } 1395c7bbc056SSergei Shtylyov tx_csr &= ~(MUSB_TXCSR_DMAMODE | 1396c7bbc056SSergei Shtylyov MUSB_TXCSR_TXPKTRDY); 1397c7bbc056SSergei Shtylyov musb_writew(epio, MUSB_TXCSR, 1398c7bbc056SSergei Shtylyov tx_csr | MUSB_TXCSR_H_WZC_BITS); 1399c7bbc056SSergei Shtylyov 1400c7bbc056SSergei Shtylyov /* 1401c7bbc056SSergei Shtylyov * There is no guarantee that we'll get an interrupt 1402c7bbc056SSergei Shtylyov * after clearing DMAMODE as we might have done this 1403c7bbc056SSergei Shtylyov * too late (after TXPKTRDY was cleared by controller). 1404c7bbc056SSergei Shtylyov * Re-read TXCSR as we have spoiled its previous value. 1405c7bbc056SSergei Shtylyov */ 1406c7bbc056SSergei Shtylyov tx_csr = musb_readw(epio, MUSB_TXCSR); 1407c7bbc056SSergei Shtylyov } 1408c7bbc056SSergei Shtylyov 1409c7bbc056SSergei Shtylyov /* 1410c7bbc056SSergei Shtylyov * We may get here from a DMA completion or TXPKTRDY interrupt. 1411c7bbc056SSergei Shtylyov * In any case, we must check the FIFO status here and bail out 1412c7bbc056SSergei Shtylyov * only if the FIFO still has data -- that should prevent the 1413c7bbc056SSergei Shtylyov * "missed" TXPKTRDY interrupts and deal with double-buffered 1414c7bbc056SSergei Shtylyov * FIFO mode too... 1415c7bbc056SSergei Shtylyov */ 1416c7bbc056SSergei Shtylyov if (tx_csr & (MUSB_TXCSR_FIFONOTEMPTY | MUSB_TXCSR_TXPKTRDY)) { 14175c8a86e1SFelipe Balbi dev_dbg(musb->controller, "DMA complete but packet still in FIFO, " 1418c7bbc056SSergei Shtylyov "CSR %04x\n", tx_csr); 1419c7bbc056SSergei Shtylyov return; 1420c7bbc056SSergei Shtylyov } 1421c7bbc056SSergei Shtylyov } 1422c7bbc056SSergei Shtylyov 1423550a7375SFelipe Balbi if (!status || dma || usb_pipeisoc(pipe)) { 1424550a7375SFelipe Balbi if (dma) 14256b6e9710SSergei Shtylyov length = dma->actual_len; 1426550a7375SFelipe Balbi else 14276b6e9710SSergei Shtylyov length = qh->segsize; 14286b6e9710SSergei Shtylyov qh->offset += length; 1429550a7375SFelipe Balbi 1430550a7375SFelipe Balbi if (usb_pipeisoc(pipe)) { 1431550a7375SFelipe Balbi struct usb_iso_packet_descriptor *d; 1432550a7375SFelipe Balbi 1433550a7375SFelipe Balbi d = urb->iso_frame_desc + qh->iso_idx; 14346b6e9710SSergei Shtylyov d->actual_length = length; 14356b6e9710SSergei Shtylyov d->status = status; 1436550a7375SFelipe Balbi if (++qh->iso_idx >= urb->number_of_packets) { 1437550a7375SFelipe Balbi done = true; 1438550a7375SFelipe Balbi } else { 1439550a7375SFelipe Balbi d++; 14406b6e9710SSergei Shtylyov offset = d->offset; 14416b6e9710SSergei Shtylyov length = d->length; 1442550a7375SFelipe Balbi } 1443f8afbf7fST. S., Anil Kumar } else if (dma && urb->transfer_buffer_length == qh->offset) { 1444550a7375SFelipe Balbi done = true; 1445550a7375SFelipe Balbi } else { 1446550a7375SFelipe Balbi /* see if we need to send more data, or ZLP */ 1447550a7375SFelipe Balbi if (qh->segsize < qh->maxpacket) 1448550a7375SFelipe Balbi done = true; 1449550a7375SFelipe Balbi else if (qh->offset == urb->transfer_buffer_length 1450550a7375SFelipe Balbi && !(urb->transfer_flags 1451550a7375SFelipe Balbi & URB_ZERO_PACKET)) 1452550a7375SFelipe Balbi done = true; 1453550a7375SFelipe Balbi if (!done) { 14546b6e9710SSergei Shtylyov offset = qh->offset; 14556b6e9710SSergei Shtylyov length = urb->transfer_buffer_length - offset; 1456f8afbf7fST. S., Anil Kumar transfer_pending = true; 1457550a7375SFelipe Balbi } 1458550a7375SFelipe Balbi } 1459550a7375SFelipe Balbi } 1460550a7375SFelipe Balbi 1461550a7375SFelipe Balbi /* urb->status != -EINPROGRESS means request has been faulted, 1462550a7375SFelipe Balbi * so we must abort this transfer after cleanup 1463550a7375SFelipe Balbi */ 1464550a7375SFelipe Balbi if (urb->status != -EINPROGRESS) { 1465550a7375SFelipe Balbi done = true; 1466550a7375SFelipe Balbi if (status == 0) 1467550a7375SFelipe Balbi status = urb->status; 1468550a7375SFelipe Balbi } 1469550a7375SFelipe Balbi 1470550a7375SFelipe Balbi if (done) { 1471550a7375SFelipe Balbi /* set status */ 1472550a7375SFelipe Balbi urb->status = status; 1473550a7375SFelipe Balbi urb->actual_length = qh->offset; 1474550a7375SFelipe Balbi musb_advance_schedule(musb, urb, hw_ep, USB_DIR_OUT); 14756b6e9710SSergei Shtylyov return; 1476f8afbf7fST. S., Anil Kumar } else if ((usb_pipeisoc(pipe) || transfer_pending) && dma) { 14776b6e9710SSergei Shtylyov if (musb_tx_dma_program(musb->dma_controller, hw_ep, qh, urb, 1478dfeffa53SAjay Kumar Gupta offset, length)) { 1479f8e9f34fSTony Lindgren if (is_cppi_enabled(musb) || tusb_dma_omap(musb)) 1480dfeffa53SAjay Kumar Gupta musb_h_tx_dma_start(hw_ep); 14816b6e9710SSergei Shtylyov return; 1482dfeffa53SAjay Kumar Gupta } 14836b6e9710SSergei Shtylyov } else if (tx_csr & MUSB_TXCSR_DMAENAB) { 14845c8a86e1SFelipe Balbi dev_dbg(musb->controller, "not complete, but DMA enabled?\n"); 14856b6e9710SSergei Shtylyov return; 14866b6e9710SSergei Shtylyov } 1487550a7375SFelipe Balbi 14886b6e9710SSergei Shtylyov /* 14896b6e9710SSergei Shtylyov * PIO: start next packet in this URB. 14906b6e9710SSergei Shtylyov * 14916b6e9710SSergei Shtylyov * REVISIT: some docs say that when hw_ep->tx_double_buffered, 14926b6e9710SSergei Shtylyov * (and presumably, FIFO is not half-full) we should write *two* 14936b6e9710SSergei Shtylyov * packets before updating TXCSR; other docs disagree... 1494550a7375SFelipe Balbi */ 14956b6e9710SSergei Shtylyov if (length > qh->maxpacket) 14966b6e9710SSergei Shtylyov length = qh->maxpacket; 1497496dda70SMaulik Mankad /* Unmap the buffer so that CPU can use it */ 14988b125df5SDaniel Mack usb_hcd_unmap_urb_for_dma(musb->hcd, urb); 14998e8a5516SVirupax Sadashivpetimath 15008e8a5516SVirupax Sadashivpetimath /* 15018e8a5516SVirupax Sadashivpetimath * We need to map sg if the transfer_buffer is 15028e8a5516SVirupax Sadashivpetimath * NULL. 15038e8a5516SVirupax Sadashivpetimath */ 15048e8a5516SVirupax Sadashivpetimath if (!urb->transfer_buffer) 1505ed74df12SVirupax Sadashivpetimath qh->use_sg = true; 15068e8a5516SVirupax Sadashivpetimath 1507ed74df12SVirupax Sadashivpetimath if (qh->use_sg) { 15088e8a5516SVirupax Sadashivpetimath /* sg_miter_start is already done in musb_ep_program */ 15098e8a5516SVirupax Sadashivpetimath if (!sg_miter_next(&qh->sg_miter)) { 15108e8a5516SVirupax Sadashivpetimath dev_err(musb->controller, "error: sg list empty\n"); 15118e8a5516SVirupax Sadashivpetimath sg_miter_stop(&qh->sg_miter); 15128e8a5516SVirupax Sadashivpetimath status = -EINVAL; 15138e8a5516SVirupax Sadashivpetimath goto done; 15148e8a5516SVirupax Sadashivpetimath } 15158e8a5516SVirupax Sadashivpetimath urb->transfer_buffer = qh->sg_miter.addr; 15168e8a5516SVirupax Sadashivpetimath length = min_t(u32, length, qh->sg_miter.length); 15178e8a5516SVirupax Sadashivpetimath musb_write_fifo(hw_ep, length, urb->transfer_buffer); 15188e8a5516SVirupax Sadashivpetimath qh->sg_miter.consumed = length; 15198e8a5516SVirupax Sadashivpetimath sg_miter_stop(&qh->sg_miter); 15208e8a5516SVirupax Sadashivpetimath } else { 15216b6e9710SSergei Shtylyov musb_write_fifo(hw_ep, length, urb->transfer_buffer + offset); 15228e8a5516SVirupax Sadashivpetimath } 15238e8a5516SVirupax Sadashivpetimath 15246b6e9710SSergei Shtylyov qh->segsize = length; 1525550a7375SFelipe Balbi 1526ed74df12SVirupax Sadashivpetimath if (qh->use_sg) { 15278e8a5516SVirupax Sadashivpetimath if (offset + length >= urb->transfer_buffer_length) 1528ed74df12SVirupax Sadashivpetimath qh->use_sg = false; 15298e8a5516SVirupax Sadashivpetimath } 15308e8a5516SVirupax Sadashivpetimath 1531550a7375SFelipe Balbi musb_ep_select(mbase, epnum); 1532550a7375SFelipe Balbi musb_writew(epio, MUSB_TXCSR, 1533550a7375SFelipe Balbi MUSB_TXCSR_H_WZC_BITS | MUSB_TXCSR_TXPKTRDY); 1534550a7375SFelipe Balbi } 1535550a7375SFelipe Balbi 1536069a3fd1STony Lindgren #ifdef CONFIG_USB_TI_CPPI41_DMA 1537069a3fd1STony Lindgren /* Seems to set up ISO for cppi41 and not advance len. See commit c57c41d */ 1538069a3fd1STony Lindgren static int musb_rx_dma_iso_cppi41(struct dma_controller *dma, 1539069a3fd1STony Lindgren struct musb_hw_ep *hw_ep, 1540069a3fd1STony Lindgren struct musb_qh *qh, 1541069a3fd1STony Lindgren struct urb *urb, 1542069a3fd1STony Lindgren size_t len) 1543069a3fd1STony Lindgren { 1544069a3fd1STony Lindgren struct dma_channel *channel = hw_ep->tx_channel; 1545069a3fd1STony Lindgren void __iomem *epio = hw_ep->regs; 1546069a3fd1STony Lindgren dma_addr_t *buf; 1547069a3fd1STony Lindgren u32 length, res; 1548069a3fd1STony Lindgren u16 val; 1549069a3fd1STony Lindgren 1550069a3fd1STony Lindgren buf = (void *)urb->iso_frame_desc[qh->iso_idx].offset + 1551069a3fd1STony Lindgren (u32)urb->transfer_dma; 1552069a3fd1STony Lindgren 1553069a3fd1STony Lindgren length = urb->iso_frame_desc[qh->iso_idx].length; 1554069a3fd1STony Lindgren 1555069a3fd1STony Lindgren val = musb_readw(epio, MUSB_RXCSR); 1556069a3fd1STony Lindgren val |= MUSB_RXCSR_DMAENAB; 1557069a3fd1STony Lindgren musb_writew(hw_ep->regs, MUSB_RXCSR, val); 1558069a3fd1STony Lindgren 1559069a3fd1STony Lindgren res = dma->channel_program(channel, qh->maxpacket, 0, 1560069a3fd1STony Lindgren (u32)buf, length); 1561069a3fd1STony Lindgren 1562069a3fd1STony Lindgren return res; 1563069a3fd1STony Lindgren } 1564069a3fd1STony Lindgren #else 1565069a3fd1STony Lindgren static inline int musb_rx_dma_iso_cppi41(struct dma_controller *dma, 1566069a3fd1STony Lindgren struct musb_hw_ep *hw_ep, 1567069a3fd1STony Lindgren struct musb_qh *qh, 1568069a3fd1STony Lindgren struct urb *urb, 1569069a3fd1STony Lindgren size_t len) 1570069a3fd1STony Lindgren { 1571069a3fd1STony Lindgren return false; 1572069a3fd1STony Lindgren } 1573069a3fd1STony Lindgren #endif 1574550a7375SFelipe Balbi 1575*cff84bdbSTony Lindgren #if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_UX500_DMA) || \ 1576*cff84bdbSTony Lindgren defined(CONFIG_USB_TI_CPPI41_DMA) 1577550a7375SFelipe Balbi /* Host side RX (IN) using Mentor DMA works as follows: 1578550a7375SFelipe Balbi submit_urb -> 1579550a7375SFelipe Balbi - if queue was empty, ProgramEndpoint 1580550a7375SFelipe Balbi - first IN token is sent out (by setting ReqPkt) 1581550a7375SFelipe Balbi LinuxIsr -> RxReady() 1582550a7375SFelipe Balbi /\ => first packet is received 1583550a7375SFelipe Balbi | - Set in mode 0 (DmaEnab, ~ReqPkt) 1584550a7375SFelipe Balbi | -> DMA Isr (transfer complete) -> RxReady() 1585550a7375SFelipe Balbi | - Ack receive (~RxPktRdy), turn off DMA (~DmaEnab) 1586550a7375SFelipe Balbi | - if urb not complete, send next IN token (ReqPkt) 1587550a7375SFelipe Balbi | | else complete urb. 1588550a7375SFelipe Balbi | | 1589550a7375SFelipe Balbi --------------------------- 1590550a7375SFelipe Balbi * 1591550a7375SFelipe Balbi * Nuances of mode 1: 1592550a7375SFelipe Balbi * For short packets, no ack (+RxPktRdy) is sent automatically 1593550a7375SFelipe Balbi * (even if AutoClear is ON) 1594550a7375SFelipe Balbi * For full packets, ack (~RxPktRdy) and next IN token (+ReqPkt) is sent 1595550a7375SFelipe Balbi * automatically => major problem, as collecting the next packet becomes 1596550a7375SFelipe Balbi * difficult. Hence mode 1 is not used. 1597550a7375SFelipe Balbi * 1598550a7375SFelipe Balbi * REVISIT 1599550a7375SFelipe Balbi * All we care about at this driver level is that 1600550a7375SFelipe Balbi * (a) all URBs terminate with REQPKT cleared and fifo(s) empty; 1601550a7375SFelipe Balbi * (b) termination conditions are: short RX, or buffer full; 1602550a7375SFelipe Balbi * (c) fault modes include 1603550a7375SFelipe Balbi * - iff URB_SHORT_NOT_OK, short RX status is -EREMOTEIO. 1604550a7375SFelipe Balbi * (and that endpoint's dma queue stops immediately) 1605550a7375SFelipe Balbi * - overflow (full, PLUS more bytes in the terminal packet) 1606550a7375SFelipe Balbi * 1607550a7375SFelipe Balbi * So for example, usb-storage sets URB_SHORT_NOT_OK, and would 1608550a7375SFelipe Balbi * thus be a great candidate for using mode 1 ... for all but the 1609550a7375SFelipe Balbi * last packet of one URB's transfer. 1610550a7375SFelipe Balbi */ 1611*cff84bdbSTony Lindgren static int musb_rx_dma_inventra_cppi41(struct dma_controller *dma, 1612*cff84bdbSTony Lindgren struct musb_hw_ep *hw_ep, 1613*cff84bdbSTony Lindgren struct musb_qh *qh, 1614*cff84bdbSTony Lindgren struct urb *urb, 1615*cff84bdbSTony Lindgren size_t len) 1616*cff84bdbSTony Lindgren { 1617*cff84bdbSTony Lindgren struct dma_channel *channel = hw_ep->rx_channel; 1618*cff84bdbSTony Lindgren void __iomem *epio = hw_ep->regs; 1619*cff84bdbSTony Lindgren u16 val; 1620*cff84bdbSTony Lindgren int pipe; 1621*cff84bdbSTony Lindgren bool done; 1622550a7375SFelipe Balbi 1623*cff84bdbSTony Lindgren pipe = urb->pipe; 1624*cff84bdbSTony Lindgren 1625*cff84bdbSTony Lindgren if (usb_pipeisoc(pipe)) { 1626*cff84bdbSTony Lindgren struct usb_iso_packet_descriptor *d; 1627*cff84bdbSTony Lindgren 1628*cff84bdbSTony Lindgren d = urb->iso_frame_desc + qh->iso_idx; 1629*cff84bdbSTony Lindgren d->actual_length = len; 1630*cff84bdbSTony Lindgren 1631*cff84bdbSTony Lindgren /* even if there was an error, we did the dma 1632*cff84bdbSTony Lindgren * for iso_frame_desc->length 1633*cff84bdbSTony Lindgren */ 1634*cff84bdbSTony Lindgren if (d->status != -EILSEQ && d->status != -EOVERFLOW) 1635*cff84bdbSTony Lindgren d->status = 0; 1636*cff84bdbSTony Lindgren 1637*cff84bdbSTony Lindgren if (++qh->iso_idx >= urb->number_of_packets) { 1638*cff84bdbSTony Lindgren done = true; 1639*cff84bdbSTony Lindgren } else { 1640*cff84bdbSTony Lindgren /* REVISIT: Why ignore return value here? */ 1641*cff84bdbSTony Lindgren if (musb_dma_cppi41(hw_ep->musb)) 1642*cff84bdbSTony Lindgren done = musb_rx_dma_iso_cppi41(dma, hw_ep, qh, 1643*cff84bdbSTony Lindgren urb, len); 1644*cff84bdbSTony Lindgren done = false; 1645*cff84bdbSTony Lindgren } 1646*cff84bdbSTony Lindgren 1647*cff84bdbSTony Lindgren } else { 1648*cff84bdbSTony Lindgren /* done if urb buffer is full or short packet is recd */ 1649*cff84bdbSTony Lindgren done = (urb->actual_length + len >= 1650*cff84bdbSTony Lindgren urb->transfer_buffer_length 1651*cff84bdbSTony Lindgren || channel->actual_len < qh->maxpacket 1652*cff84bdbSTony Lindgren || channel->rx_packet_done); 1653*cff84bdbSTony Lindgren } 1654*cff84bdbSTony Lindgren 1655*cff84bdbSTony Lindgren /* send IN token for next packet, without AUTOREQ */ 1656*cff84bdbSTony Lindgren if (!done) { 1657*cff84bdbSTony Lindgren val = musb_readw(epio, MUSB_RXCSR); 1658*cff84bdbSTony Lindgren val |= MUSB_RXCSR_H_REQPKT; 1659*cff84bdbSTony Lindgren musb_writew(epio, MUSB_RXCSR, MUSB_RXCSR_H_WZC_BITS | val); 1660*cff84bdbSTony Lindgren } 1661*cff84bdbSTony Lindgren 1662*cff84bdbSTony Lindgren return done; 1663*cff84bdbSTony Lindgren } 1664*cff84bdbSTony Lindgren #else 1665*cff84bdbSTony Lindgren static inline int musb_rx_dma_inventra_cppi41(struct dma_controller *dma, 1666*cff84bdbSTony Lindgren struct musb_hw_ep *hw_ep, 1667*cff84bdbSTony Lindgren struct musb_qh *qh, 1668*cff84bdbSTony Lindgren struct urb *urb, 1669*cff84bdbSTony Lindgren size_t len) 1670*cff84bdbSTony Lindgren { 1671*cff84bdbSTony Lindgren return false; 1672*cff84bdbSTony Lindgren } 1673550a7375SFelipe Balbi #endif 1674550a7375SFelipe Balbi 1675550a7375SFelipe Balbi /* 1676550a7375SFelipe Balbi * Service an RX interrupt for the given IN endpoint; docs cover bulk, iso, 1677550a7375SFelipe Balbi * and high-bandwidth IN transfer cases. 1678550a7375SFelipe Balbi */ 1679550a7375SFelipe Balbi void musb_host_rx(struct musb *musb, u8 epnum) 1680550a7375SFelipe Balbi { 1681550a7375SFelipe Balbi struct urb *urb; 1682550a7375SFelipe Balbi struct musb_hw_ep *hw_ep = musb->endpoints + epnum; 1683*cff84bdbSTony Lindgren struct dma_controller *c = musb->dma_controller; 1684550a7375SFelipe Balbi void __iomem *epio = hw_ep->regs; 1685550a7375SFelipe Balbi struct musb_qh *qh = hw_ep->in_qh; 1686550a7375SFelipe Balbi size_t xfer_len; 1687550a7375SFelipe Balbi void __iomem *mbase = musb->mregs; 1688550a7375SFelipe Balbi int pipe; 1689550a7375SFelipe Balbi u16 rx_csr, val; 1690550a7375SFelipe Balbi bool iso_err = false; 1691550a7375SFelipe Balbi bool done = false; 1692550a7375SFelipe Balbi u32 status; 1693550a7375SFelipe Balbi struct dma_channel *dma; 16948e8a5516SVirupax Sadashivpetimath unsigned int sg_flags = SG_MITER_ATOMIC | SG_MITER_TO_SG; 1695550a7375SFelipe Balbi 1696550a7375SFelipe Balbi musb_ep_select(mbase, epnum); 1697550a7375SFelipe Balbi 1698550a7375SFelipe Balbi urb = next_urb(qh); 1699550a7375SFelipe Balbi dma = is_dma_capable() ? hw_ep->rx_channel : NULL; 1700550a7375SFelipe Balbi status = 0; 1701550a7375SFelipe Balbi xfer_len = 0; 1702550a7375SFelipe Balbi 1703550a7375SFelipe Balbi rx_csr = musb_readw(epio, MUSB_RXCSR); 1704550a7375SFelipe Balbi val = rx_csr; 1705550a7375SFelipe Balbi 1706550a7375SFelipe Balbi if (unlikely(!urb)) { 1707550a7375SFelipe Balbi /* REVISIT -- THIS SHOULD NEVER HAPPEN ... but, at least 1708550a7375SFelipe Balbi * usbtest #11 (unlinks) triggers it regularly, sometimes 1709550a7375SFelipe Balbi * with fifo full. (Only with DMA??) 1710550a7375SFelipe Balbi */ 17115c8a86e1SFelipe Balbi dev_dbg(musb->controller, "BOGUS RX%d ready, csr %04x, count %d\n", epnum, val, 1712550a7375SFelipe Balbi musb_readw(epio, MUSB_RXCOUNT)); 1713550a7375SFelipe Balbi musb_h_flush_rxfifo(hw_ep, MUSB_RXCSR_CLRDATATOG); 1714550a7375SFelipe Balbi return; 1715550a7375SFelipe Balbi } 1716550a7375SFelipe Balbi 1717550a7375SFelipe Balbi pipe = urb->pipe; 1718550a7375SFelipe Balbi 17195c8a86e1SFelipe Balbi dev_dbg(musb->controller, "<== hw %d rxcsr %04x, urb actual %d (+dma %zu)\n", 1720550a7375SFelipe Balbi epnum, rx_csr, urb->actual_length, 1721550a7375SFelipe Balbi dma ? dma->actual_len : 0); 1722550a7375SFelipe Balbi 1723550a7375SFelipe Balbi /* check for errors, concurrent stall & unlink is not really 1724550a7375SFelipe Balbi * handled yet! */ 1725550a7375SFelipe Balbi if (rx_csr & MUSB_RXCSR_H_RXSTALL) { 17265c8a86e1SFelipe Balbi dev_dbg(musb->controller, "RX end %d STALL\n", epnum); 1727550a7375SFelipe Balbi 1728550a7375SFelipe Balbi /* stall; record URB status */ 1729550a7375SFelipe Balbi status = -EPIPE; 1730550a7375SFelipe Balbi 1731550a7375SFelipe Balbi } else if (rx_csr & MUSB_RXCSR_H_ERROR) { 17325c8a86e1SFelipe Balbi dev_dbg(musb->controller, "end %d RX proto error\n", epnum); 1733550a7375SFelipe Balbi 1734550a7375SFelipe Balbi status = -EPROTO; 1735550a7375SFelipe Balbi musb_writeb(epio, MUSB_RXINTERVAL, 0); 1736550a7375SFelipe Balbi 1737550a7375SFelipe Balbi } else if (rx_csr & MUSB_RXCSR_DATAERROR) { 1738550a7375SFelipe Balbi 1739550a7375SFelipe Balbi if (USB_ENDPOINT_XFER_ISOC != qh->type) { 17405c8a86e1SFelipe Balbi dev_dbg(musb->controller, "RX end %d NAK timeout\n", epnum); 17411e0320f0SAjay Kumar Gupta 17421e0320f0SAjay Kumar Gupta /* NOTE: NAKing is *NOT* an error, so we want to 17431e0320f0SAjay Kumar Gupta * continue. Except ... if there's a request for 17441e0320f0SAjay Kumar Gupta * another QH, use that instead of starving it. 17451e0320f0SAjay Kumar Gupta * 17461e0320f0SAjay Kumar Gupta * Devices like Ethernet and serial adapters keep 17471e0320f0SAjay Kumar Gupta * reads posted at all times, which will starve 17481e0320f0SAjay Kumar Gupta * other devices without this logic. 17491e0320f0SAjay Kumar Gupta */ 17501e0320f0SAjay Kumar Gupta if (usb_pipebulk(urb->pipe) 17511e0320f0SAjay Kumar Gupta && qh->mux == 1 17521e0320f0SAjay Kumar Gupta && !list_is_singular(&musb->in_bulk)) { 1753f283862fSAjay Kumar Gupta musb_bulk_nak_timeout(musb, hw_ep, 1); 17541e0320f0SAjay Kumar Gupta return; 17551e0320f0SAjay Kumar Gupta } 1756550a7375SFelipe Balbi musb_ep_select(mbase, epnum); 17571e0320f0SAjay Kumar Gupta rx_csr |= MUSB_RXCSR_H_WZC_BITS; 17581e0320f0SAjay Kumar Gupta rx_csr &= ~MUSB_RXCSR_DATAERROR; 17591e0320f0SAjay Kumar Gupta musb_writew(epio, MUSB_RXCSR, rx_csr); 1760550a7375SFelipe Balbi 1761550a7375SFelipe Balbi goto finish; 1762550a7375SFelipe Balbi } else { 17635c8a86e1SFelipe Balbi dev_dbg(musb->controller, "RX end %d ISO data error\n", epnum); 1764550a7375SFelipe Balbi /* packet error reported later */ 1765550a7375SFelipe Balbi iso_err = true; 1766550a7375SFelipe Balbi } 1767a483d706SAjay Kumar Gupta } else if (rx_csr & MUSB_RXCSR_INCOMPRX) { 17685c8a86e1SFelipe Balbi dev_dbg(musb->controller, "end %d high bandwidth incomplete ISO packet RX\n", 1769a483d706SAjay Kumar Gupta epnum); 1770a483d706SAjay Kumar Gupta status = -EPROTO; 1771550a7375SFelipe Balbi } 1772550a7375SFelipe Balbi 1773550a7375SFelipe Balbi /* faults abort the transfer */ 1774550a7375SFelipe Balbi if (status) { 1775550a7375SFelipe Balbi /* clean up dma and collect transfer count */ 1776550a7375SFelipe Balbi if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) { 1777550a7375SFelipe Balbi dma->status = MUSB_DMA_STATUS_CORE_ABORT; 17789c547699SDaniel Mack musb->dma_controller->channel_abort(dma); 1779550a7375SFelipe Balbi xfer_len = dma->actual_len; 1780550a7375SFelipe Balbi } 1781550a7375SFelipe Balbi musb_h_flush_rxfifo(hw_ep, MUSB_RXCSR_CLRDATATOG); 1782550a7375SFelipe Balbi musb_writeb(epio, MUSB_RXINTERVAL, 0); 1783550a7375SFelipe Balbi done = true; 1784550a7375SFelipe Balbi goto finish; 1785550a7375SFelipe Balbi } 1786550a7375SFelipe Balbi 1787550a7375SFelipe Balbi if (unlikely(dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY)) { 1788550a7375SFelipe Balbi /* SHOULD NEVER HAPPEN ... but at least DaVinci has done it */ 1789550a7375SFelipe Balbi ERR("RX%d dma busy, csr %04x\n", epnum, rx_csr); 1790550a7375SFelipe Balbi goto finish; 1791550a7375SFelipe Balbi } 1792550a7375SFelipe Balbi 1793550a7375SFelipe Balbi /* thorough shutdown for now ... given more precise fault handling 1794550a7375SFelipe Balbi * and better queueing support, we might keep a DMA pipeline going 1795550a7375SFelipe Balbi * while processing this irq for earlier completions. 1796550a7375SFelipe Balbi */ 1797550a7375SFelipe Balbi 1798550a7375SFelipe Balbi /* FIXME this is _way_ too much in-line logic for Mentor DMA */ 1799557d543eSTony Lindgren if (!musb_dma_inventra(musb) && !musb_dma_ux500(musb) && 1800557d543eSTony Lindgren (rx_csr & MUSB_RXCSR_H_REQPKT)) { 1801550a7375SFelipe Balbi /* REVISIT this happened for a while on some short reads... 1802550a7375SFelipe Balbi * the cleanup still needs investigation... looks bad... 1803550a7375SFelipe Balbi * and also duplicates dma cleanup code above ... plus, 1804550a7375SFelipe Balbi * shouldn't this be the "half full" double buffer case? 1805550a7375SFelipe Balbi */ 1806550a7375SFelipe Balbi if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) { 1807550a7375SFelipe Balbi dma->status = MUSB_DMA_STATUS_CORE_ABORT; 18089c547699SDaniel Mack musb->dma_controller->channel_abort(dma); 1809550a7375SFelipe Balbi xfer_len = dma->actual_len; 1810550a7375SFelipe Balbi done = true; 1811550a7375SFelipe Balbi } 1812550a7375SFelipe Balbi 18135c8a86e1SFelipe Balbi dev_dbg(musb->controller, "RXCSR%d %04x, reqpkt, len %zu%s\n", epnum, rx_csr, 1814550a7375SFelipe Balbi xfer_len, dma ? ", dma" : ""); 1815550a7375SFelipe Balbi rx_csr &= ~MUSB_RXCSR_H_REQPKT; 1816550a7375SFelipe Balbi 1817550a7375SFelipe Balbi musb_ep_select(mbase, epnum); 1818550a7375SFelipe Balbi musb_writew(epio, MUSB_RXCSR, 1819550a7375SFelipe Balbi MUSB_RXCSR_H_WZC_BITS | rx_csr); 1820550a7375SFelipe Balbi } 1821557d543eSTony Lindgren 1822550a7375SFelipe Balbi if (dma && (rx_csr & MUSB_RXCSR_DMAENAB)) { 1823550a7375SFelipe Balbi xfer_len = dma->actual_len; 1824550a7375SFelipe Balbi 1825550a7375SFelipe Balbi val &= ~(MUSB_RXCSR_DMAENAB 1826550a7375SFelipe Balbi | MUSB_RXCSR_H_AUTOREQ 1827550a7375SFelipe Balbi | MUSB_RXCSR_AUTOCLEAR 1828550a7375SFelipe Balbi | MUSB_RXCSR_RXPKTRDY); 1829550a7375SFelipe Balbi musb_writew(hw_ep->regs, MUSB_RXCSR, val); 1830550a7375SFelipe Balbi 1831*cff84bdbSTony Lindgren if (musb_dma_inventra(musb) || musb_dma_ux500(musb) || 1832*cff84bdbSTony Lindgren musb_dma_cppi41(musb)) { 1833*cff84bdbSTony Lindgren done = musb_rx_dma_inventra_cppi41(c, hw_ep, qh, urb, xfer_len); 1834*cff84bdbSTony Lindgren dev_dbg(hw_ep->musb->controller, 1835*cff84bdbSTony Lindgren "ep %d dma %s, rxcsr %04x, rxcount %d\n", 1836*cff84bdbSTony Lindgren epnum, done ? "off" : "reset", 1837550a7375SFelipe Balbi musb_readw(epio, MUSB_RXCSR), 1838550a7375SFelipe Balbi musb_readw(epio, MUSB_RXCOUNT)); 1839*cff84bdbSTony Lindgren } else { 1840550a7375SFelipe Balbi done = true; 1841*cff84bdbSTony Lindgren } 1842*cff84bdbSTony Lindgren 1843550a7375SFelipe Balbi } else if (urb->status == -EINPROGRESS) { 1844550a7375SFelipe Balbi /* if no errors, be sure a packet is ready for unloading */ 1845550a7375SFelipe Balbi if (unlikely(!(rx_csr & MUSB_RXCSR_RXPKTRDY))) { 1846550a7375SFelipe Balbi status = -EPROTO; 1847550a7375SFelipe Balbi ERR("Rx interrupt with no errors or packet!\n"); 1848550a7375SFelipe Balbi 1849550a7375SFelipe Balbi /* FIXME this is another "SHOULD NEVER HAPPEN" */ 1850550a7375SFelipe Balbi 1851550a7375SFelipe Balbi /* SCRUB (RX) */ 1852550a7375SFelipe Balbi /* do the proper sequence to abort the transfer */ 1853550a7375SFelipe Balbi musb_ep_select(mbase, epnum); 1854550a7375SFelipe Balbi val &= ~MUSB_RXCSR_H_REQPKT; 1855550a7375SFelipe Balbi musb_writew(epio, MUSB_RXCSR, val); 1856550a7375SFelipe Balbi goto finish; 1857550a7375SFelipe Balbi } 1858550a7375SFelipe Balbi 1859550a7375SFelipe Balbi /* we are expecting IN packets */ 1860c57c41d2SGeorge Cherian #if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_UX500_DMA) || \ 1861c57c41d2SGeorge Cherian defined(CONFIG_USB_TI_CPPI41_DMA) 1862550a7375SFelipe Balbi if (dma) { 1863550a7375SFelipe Balbi struct dma_controller *c; 1864550a7375SFelipe Balbi u16 rx_count; 1865f82a689fSAjay Kumar Gupta int ret, length; 1866f82a689fSAjay Kumar Gupta dma_addr_t buf; 1867550a7375SFelipe Balbi 1868550a7375SFelipe Balbi rx_count = musb_readw(epio, MUSB_RXCOUNT); 1869550a7375SFelipe Balbi 187091e3af64SFelipe Balbi dev_dbg(musb->controller, "RX%d count %d, buffer 0x%llx len %d/%d\n", 1871550a7375SFelipe Balbi epnum, rx_count, 187291e3af64SFelipe Balbi (unsigned long long) urb->transfer_dma 1873550a7375SFelipe Balbi + urb->actual_length, 1874550a7375SFelipe Balbi qh->offset, 1875550a7375SFelipe Balbi urb->transfer_buffer_length); 1876550a7375SFelipe Balbi 1877550a7375SFelipe Balbi c = musb->dma_controller; 1878550a7375SFelipe Balbi 1879f82a689fSAjay Kumar Gupta if (usb_pipeisoc(pipe)) { 18808b4959d6SFelipe Balbi int d_status = 0; 1881f82a689fSAjay Kumar Gupta struct usb_iso_packet_descriptor *d; 1882f82a689fSAjay Kumar Gupta 1883f82a689fSAjay Kumar Gupta d = urb->iso_frame_desc + qh->iso_idx; 1884f82a689fSAjay Kumar Gupta 1885f82a689fSAjay Kumar Gupta if (iso_err) { 18868b4959d6SFelipe Balbi d_status = -EILSEQ; 1887f82a689fSAjay Kumar Gupta urb->error_count++; 1888f82a689fSAjay Kumar Gupta } 1889f82a689fSAjay Kumar Gupta if (rx_count > d->length) { 18908b4959d6SFelipe Balbi if (d_status == 0) { 18918b4959d6SFelipe Balbi d_status = -EOVERFLOW; 1892f82a689fSAjay Kumar Gupta urb->error_count++; 1893f82a689fSAjay Kumar Gupta } 18945c8a86e1SFelipe Balbi dev_dbg(musb->controller, "** OVERFLOW %d into %d\n",\ 1895f82a689fSAjay Kumar Gupta rx_count, d->length); 1896f82a689fSAjay Kumar Gupta 1897f82a689fSAjay Kumar Gupta length = d->length; 1898f82a689fSAjay Kumar Gupta } else 1899f82a689fSAjay Kumar Gupta length = rx_count; 19008b4959d6SFelipe Balbi d->status = d_status; 1901f82a689fSAjay Kumar Gupta buf = urb->transfer_dma + d->offset; 1902f82a689fSAjay Kumar Gupta } else { 1903f82a689fSAjay Kumar Gupta length = rx_count; 1904f82a689fSAjay Kumar Gupta buf = urb->transfer_dma + 1905f82a689fSAjay Kumar Gupta urb->actual_length; 1906f82a689fSAjay Kumar Gupta } 1907f82a689fSAjay Kumar Gupta 1908550a7375SFelipe Balbi dma->desired_mode = 0; 1909550a7375SFelipe Balbi #ifdef USE_MODE1 1910550a7375SFelipe Balbi /* because of the issue below, mode 1 will 1911550a7375SFelipe Balbi * only rarely behave with correct semantics. 1912550a7375SFelipe Balbi */ 1913550a7375SFelipe Balbi if ((urb->transfer_flags & 1914550a7375SFelipe Balbi URB_SHORT_NOT_OK) 1915550a7375SFelipe Balbi && (urb->transfer_buffer_length - 1916550a7375SFelipe Balbi urb->actual_length) 1917550a7375SFelipe Balbi > qh->maxpacket) 1918550a7375SFelipe Balbi dma->desired_mode = 1; 1919f82a689fSAjay Kumar Gupta if (rx_count < hw_ep->max_packet_sz_rx) { 1920f82a689fSAjay Kumar Gupta length = rx_count; 1921ae926976SSonic Zhang dma->desired_mode = 0; 1922f82a689fSAjay Kumar Gupta } else { 1923f82a689fSAjay Kumar Gupta length = urb->transfer_buffer_length; 1924f82a689fSAjay Kumar Gupta } 1925550a7375SFelipe Balbi #endif 1926550a7375SFelipe Balbi 1927550a7375SFelipe Balbi /* Disadvantage of using mode 1: 1928550a7375SFelipe Balbi * It's basically usable only for mass storage class; essentially all 1929550a7375SFelipe Balbi * other protocols also terminate transfers on short packets. 1930550a7375SFelipe Balbi * 1931550a7375SFelipe Balbi * Details: 1932550a7375SFelipe Balbi * An extra IN token is sent at the end of the transfer (due to AUTOREQ) 1933550a7375SFelipe Balbi * If you try to use mode 1 for (transfer_buffer_length - 512), and try 1934550a7375SFelipe Balbi * to use the extra IN token to grab the last packet using mode 0, then 1935550a7375SFelipe Balbi * the problem is that you cannot be sure when the device will send the 1936550a7375SFelipe Balbi * last packet and RxPktRdy set. Sometimes the packet is recd too soon 1937550a7375SFelipe Balbi * such that it gets lost when RxCSR is re-set at the end of the mode 1 1938550a7375SFelipe Balbi * transfer, while sometimes it is recd just a little late so that if you 1939550a7375SFelipe Balbi * try to configure for mode 0 soon after the mode 1 transfer is 1940550a7375SFelipe Balbi * completed, you will find rxcount 0. Okay, so you might think why not 1941550a7375SFelipe Balbi * wait for an interrupt when the pkt is recd. Well, you won't get any! 1942550a7375SFelipe Balbi */ 1943550a7375SFelipe Balbi 1944550a7375SFelipe Balbi val = musb_readw(epio, MUSB_RXCSR); 1945550a7375SFelipe Balbi val &= ~MUSB_RXCSR_H_REQPKT; 1946550a7375SFelipe Balbi 1947550a7375SFelipe Balbi if (dma->desired_mode == 0) 1948550a7375SFelipe Balbi val &= ~MUSB_RXCSR_H_AUTOREQ; 1949550a7375SFelipe Balbi else 1950550a7375SFelipe Balbi val |= MUSB_RXCSR_H_AUTOREQ; 1951a483d706SAjay Kumar Gupta val |= MUSB_RXCSR_DMAENAB; 1952a483d706SAjay Kumar Gupta 1953a483d706SAjay Kumar Gupta /* autoclear shouldn't be set in high bandwidth */ 1954a483d706SAjay Kumar Gupta if (qh->hb_mult == 1) 1955a483d706SAjay Kumar Gupta val |= MUSB_RXCSR_AUTOCLEAR; 1956550a7375SFelipe Balbi 1957550a7375SFelipe Balbi musb_writew(epio, MUSB_RXCSR, 1958550a7375SFelipe Balbi MUSB_RXCSR_H_WZC_BITS | val); 1959550a7375SFelipe Balbi 1960550a7375SFelipe Balbi /* REVISIT if when actual_length != 0, 1961550a7375SFelipe Balbi * transfer_buffer_length needs to be 1962550a7375SFelipe Balbi * adjusted first... 1963550a7375SFelipe Balbi */ 1964550a7375SFelipe Balbi ret = c->channel_program( 1965550a7375SFelipe Balbi dma, qh->maxpacket, 1966f82a689fSAjay Kumar Gupta dma->desired_mode, buf, length); 1967550a7375SFelipe Balbi 1968550a7375SFelipe Balbi if (!ret) { 1969550a7375SFelipe Balbi c->channel_release(dma); 1970550a7375SFelipe Balbi hw_ep->rx_channel = NULL; 1971550a7375SFelipe Balbi dma = NULL; 19722ed9127cSMantesh Sarasetti val = musb_readw(epio, MUSB_RXCSR); 19732ed9127cSMantesh Sarasetti val &= ~(MUSB_RXCSR_DMAENAB 19742ed9127cSMantesh Sarasetti | MUSB_RXCSR_H_AUTOREQ 19752ed9127cSMantesh Sarasetti | MUSB_RXCSR_AUTOCLEAR); 19762ed9127cSMantesh Sarasetti musb_writew(epio, MUSB_RXCSR, val); 1977550a7375SFelipe Balbi } 1978550a7375SFelipe Balbi } 1979550a7375SFelipe Balbi #endif /* Mentor DMA */ 1980550a7375SFelipe Balbi 1981550a7375SFelipe Balbi if (!dma) { 19828e8a5516SVirupax Sadashivpetimath unsigned int received_len; 19838e8a5516SVirupax Sadashivpetimath 1984496dda70SMaulik Mankad /* Unmap the buffer so that CPU can use it */ 19858b125df5SDaniel Mack usb_hcd_unmap_urb_for_dma(musb->hcd, urb); 19868e8a5516SVirupax Sadashivpetimath 19878e8a5516SVirupax Sadashivpetimath /* 19888e8a5516SVirupax Sadashivpetimath * We need to map sg if the transfer_buffer is 19898e8a5516SVirupax Sadashivpetimath * NULL. 19908e8a5516SVirupax Sadashivpetimath */ 19918e8a5516SVirupax Sadashivpetimath if (!urb->transfer_buffer) { 1992ed74df12SVirupax Sadashivpetimath qh->use_sg = true; 19938e8a5516SVirupax Sadashivpetimath sg_miter_start(&qh->sg_miter, urb->sg, 1, 19948e8a5516SVirupax Sadashivpetimath sg_flags); 19958e8a5516SVirupax Sadashivpetimath } 19968e8a5516SVirupax Sadashivpetimath 1997ed74df12SVirupax Sadashivpetimath if (qh->use_sg) { 19988e8a5516SVirupax Sadashivpetimath if (!sg_miter_next(&qh->sg_miter)) { 19998e8a5516SVirupax Sadashivpetimath dev_err(musb->controller, "error: sg list empty\n"); 20008e8a5516SVirupax Sadashivpetimath sg_miter_stop(&qh->sg_miter); 20018e8a5516SVirupax Sadashivpetimath status = -EINVAL; 20028e8a5516SVirupax Sadashivpetimath done = true; 20038e8a5516SVirupax Sadashivpetimath goto finish; 20048e8a5516SVirupax Sadashivpetimath } 20058e8a5516SVirupax Sadashivpetimath urb->transfer_buffer = qh->sg_miter.addr; 20068e8a5516SVirupax Sadashivpetimath received_len = urb->actual_length; 20078e8a5516SVirupax Sadashivpetimath qh->offset = 0x0; 20088e8a5516SVirupax Sadashivpetimath done = musb_host_packet_rx(musb, urb, epnum, 20098e8a5516SVirupax Sadashivpetimath iso_err); 20108e8a5516SVirupax Sadashivpetimath /* Calculate the number of bytes received */ 20118e8a5516SVirupax Sadashivpetimath received_len = urb->actual_length - 20128e8a5516SVirupax Sadashivpetimath received_len; 20138e8a5516SVirupax Sadashivpetimath qh->sg_miter.consumed = received_len; 20148e8a5516SVirupax Sadashivpetimath sg_miter_stop(&qh->sg_miter); 20158e8a5516SVirupax Sadashivpetimath } else { 2016550a7375SFelipe Balbi done = musb_host_packet_rx(musb, urb, 2017550a7375SFelipe Balbi epnum, iso_err); 20188e8a5516SVirupax Sadashivpetimath } 20195c8a86e1SFelipe Balbi dev_dbg(musb->controller, "read %spacket\n", done ? "last " : ""); 2020550a7375SFelipe Balbi } 2021550a7375SFelipe Balbi } 2022550a7375SFelipe Balbi 2023550a7375SFelipe Balbi finish: 2024550a7375SFelipe Balbi urb->actual_length += xfer_len; 2025550a7375SFelipe Balbi qh->offset += xfer_len; 2026550a7375SFelipe Balbi if (done) { 2027ed74df12SVirupax Sadashivpetimath if (qh->use_sg) 2028ed74df12SVirupax Sadashivpetimath qh->use_sg = false; 20298e8a5516SVirupax Sadashivpetimath 2030550a7375SFelipe Balbi if (urb->status == -EINPROGRESS) 2031550a7375SFelipe Balbi urb->status = status; 2032550a7375SFelipe Balbi musb_advance_schedule(musb, urb, hw_ep, USB_DIR_IN); 2033550a7375SFelipe Balbi } 2034550a7375SFelipe Balbi } 2035550a7375SFelipe Balbi 2036550a7375SFelipe Balbi /* schedule nodes correspond to peripheral endpoints, like an OHCI QH. 2037550a7375SFelipe Balbi * the software schedule associates multiple such nodes with a given 2038550a7375SFelipe Balbi * host side hardware endpoint + direction; scheduling may activate 2039550a7375SFelipe Balbi * that hardware endpoint. 2040550a7375SFelipe Balbi */ 2041550a7375SFelipe Balbi static int musb_schedule( 2042550a7375SFelipe Balbi struct musb *musb, 2043550a7375SFelipe Balbi struct musb_qh *qh, 2044550a7375SFelipe Balbi int is_in) 2045550a7375SFelipe Balbi { 2046eac44dc4SRickard Strandqvist int idle = 0; 2047550a7375SFelipe Balbi int best_diff; 2048550a7375SFelipe Balbi int best_end, epnum; 2049550a7375SFelipe Balbi struct musb_hw_ep *hw_ep = NULL; 2050550a7375SFelipe Balbi struct list_head *head = NULL; 20515274dab6SSwaminathan S u8 toggle; 20525274dab6SSwaminathan S u8 txtype; 20535274dab6SSwaminathan S struct urb *urb = next_urb(qh); 2054550a7375SFelipe Balbi 2055550a7375SFelipe Balbi /* use fixed hardware for control and bulk */ 205623d15e07SAjay Kumar Gupta if (qh->type == USB_ENDPOINT_XFER_CONTROL) { 2057550a7375SFelipe Balbi head = &musb->control; 2058550a7375SFelipe Balbi hw_ep = musb->control_ep; 2059550a7375SFelipe Balbi goto success; 2060550a7375SFelipe Balbi } 2061550a7375SFelipe Balbi 2062550a7375SFelipe Balbi /* else, periodic transfers get muxed to other endpoints */ 2063550a7375SFelipe Balbi 20645d67a851SSergei Shtylyov /* 20655d67a851SSergei Shtylyov * We know this qh hasn't been scheduled, so all we need to do 2066550a7375SFelipe Balbi * is choose which hardware endpoint to put it on ... 2067550a7375SFelipe Balbi * 2068550a7375SFelipe Balbi * REVISIT what we really want here is a regular schedule tree 20695d67a851SSergei Shtylyov * like e.g. OHCI uses. 2070550a7375SFelipe Balbi */ 2071550a7375SFelipe Balbi best_diff = 4096; 2072550a7375SFelipe Balbi best_end = -1; 2073550a7375SFelipe Balbi 20745d67a851SSergei Shtylyov for (epnum = 1, hw_ep = musb->endpoints + 1; 20755d67a851SSergei Shtylyov epnum < musb->nr_endpoints; 20765d67a851SSergei Shtylyov epnum++, hw_ep++) { 2077550a7375SFelipe Balbi int diff; 2078550a7375SFelipe Balbi 20793e5c6dc7SSergei Shtylyov if (musb_ep_get_qh(hw_ep, is_in) != NULL) 20805d67a851SSergei Shtylyov continue; 20815d67a851SSergei Shtylyov 2082550a7375SFelipe Balbi if (hw_ep == musb->bulk_ep) 2083550a7375SFelipe Balbi continue; 2084550a7375SFelipe Balbi 2085550a7375SFelipe Balbi if (is_in) 2086a483d706SAjay Kumar Gupta diff = hw_ep->max_packet_sz_rx; 2087550a7375SFelipe Balbi else 2088a483d706SAjay Kumar Gupta diff = hw_ep->max_packet_sz_tx; 2089a483d706SAjay Kumar Gupta diff -= (qh->maxpacket * qh->hb_mult); 2090550a7375SFelipe Balbi 209123d15e07SAjay Kumar Gupta if (diff >= 0 && best_diff > diff) { 20925274dab6SSwaminathan S 20935274dab6SSwaminathan S /* 20945274dab6SSwaminathan S * Mentor controller has a bug in that if we schedule 20955274dab6SSwaminathan S * a BULK Tx transfer on an endpoint that had earlier 20965274dab6SSwaminathan S * handled ISOC then the BULK transfer has to start on 20975274dab6SSwaminathan S * a zero toggle. If the BULK transfer starts on a 1 20985274dab6SSwaminathan S * toggle then this transfer will fail as the mentor 20995274dab6SSwaminathan S * controller starts the Bulk transfer on a 0 toggle 21005274dab6SSwaminathan S * irrespective of the programming of the toggle bits 21015274dab6SSwaminathan S * in the TXCSR register. Check for this condition 21025274dab6SSwaminathan S * while allocating the EP for a Tx Bulk transfer. If 21035274dab6SSwaminathan S * so skip this EP. 21045274dab6SSwaminathan S */ 21055274dab6SSwaminathan S hw_ep = musb->endpoints + epnum; 21065274dab6SSwaminathan S toggle = usb_gettoggle(urb->dev, qh->epnum, !is_in); 21075274dab6SSwaminathan S txtype = (musb_readb(hw_ep->regs, MUSB_TXTYPE) 21085274dab6SSwaminathan S >> 4) & 0x3; 21095274dab6SSwaminathan S if (!is_in && (qh->type == USB_ENDPOINT_XFER_BULK) && 21105274dab6SSwaminathan S toggle && (txtype == USB_ENDPOINT_XFER_ISOC)) 21115274dab6SSwaminathan S continue; 21125274dab6SSwaminathan S 2113550a7375SFelipe Balbi best_diff = diff; 2114550a7375SFelipe Balbi best_end = epnum; 2115550a7375SFelipe Balbi } 2116550a7375SFelipe Balbi } 211723d15e07SAjay Kumar Gupta /* use bulk reserved ep1 if no other ep is free */ 2118aa5cbbecSFelipe Balbi if (best_end < 0 && qh->type == USB_ENDPOINT_XFER_BULK) { 211923d15e07SAjay Kumar Gupta hw_ep = musb->bulk_ep; 212023d15e07SAjay Kumar Gupta if (is_in) 212123d15e07SAjay Kumar Gupta head = &musb->in_bulk; 212223d15e07SAjay Kumar Gupta else 212323d15e07SAjay Kumar Gupta head = &musb->out_bulk; 21241e0320f0SAjay Kumar Gupta 2125f283862fSAjay Kumar Gupta /* Enable bulk RX/TX NAK timeout scheme when bulk requests are 21265ae477b0SRahul Bedarkar * multiplexed. This scheme does not work in high speed to full 21271e0320f0SAjay Kumar Gupta * speed scenario as NAK interrupts are not coming from a 21281e0320f0SAjay Kumar Gupta * full speed device connected to a high speed device. 21291e0320f0SAjay Kumar Gupta * NAK timeout interval is 8 (128 uframe or 16ms) for HS and 21301e0320f0SAjay Kumar Gupta * 4 (8 frame or 8ms) for FS device. 21311e0320f0SAjay Kumar Gupta */ 2132f283862fSAjay Kumar Gupta if (qh->dev) 21331e0320f0SAjay Kumar Gupta qh->intv_reg = 21341e0320f0SAjay Kumar Gupta (USB_SPEED_HIGH == qh->dev->speed) ? 8 : 4; 213523d15e07SAjay Kumar Gupta goto success; 213623d15e07SAjay Kumar Gupta } else if (best_end < 0) { 2137550a7375SFelipe Balbi return -ENOSPC; 213823d15e07SAjay Kumar Gupta } 2139550a7375SFelipe Balbi 2140550a7375SFelipe Balbi idle = 1; 214123d15e07SAjay Kumar Gupta qh->mux = 0; 2142550a7375SFelipe Balbi hw_ep = musb->endpoints + best_end; 21435c8a86e1SFelipe Balbi dev_dbg(musb->controller, "qh %p periodic slot %d\n", qh, best_end); 2144550a7375SFelipe Balbi success: 214523d15e07SAjay Kumar Gupta if (head) { 214623d15e07SAjay Kumar Gupta idle = list_empty(head); 214723d15e07SAjay Kumar Gupta list_add_tail(&qh->ring, head); 214823d15e07SAjay Kumar Gupta qh->mux = 1; 214923d15e07SAjay Kumar Gupta } 2150550a7375SFelipe Balbi qh->hw_ep = hw_ep; 2151550a7375SFelipe Balbi qh->hep->hcpriv = qh; 2152550a7375SFelipe Balbi if (idle) 2153550a7375SFelipe Balbi musb_start_urb(musb, is_in, qh); 2154550a7375SFelipe Balbi return 0; 2155550a7375SFelipe Balbi } 2156550a7375SFelipe Balbi 2157550a7375SFelipe Balbi static int musb_urb_enqueue( 2158550a7375SFelipe Balbi struct usb_hcd *hcd, 2159550a7375SFelipe Balbi struct urb *urb, 2160550a7375SFelipe Balbi gfp_t mem_flags) 2161550a7375SFelipe Balbi { 2162550a7375SFelipe Balbi unsigned long flags; 2163550a7375SFelipe Balbi struct musb *musb = hcd_to_musb(hcd); 2164550a7375SFelipe Balbi struct usb_host_endpoint *hep = urb->ep; 216574bb3508SDavid Brownell struct musb_qh *qh; 2166550a7375SFelipe Balbi struct usb_endpoint_descriptor *epd = &hep->desc; 2167550a7375SFelipe Balbi int ret; 2168550a7375SFelipe Balbi unsigned type_reg; 2169550a7375SFelipe Balbi unsigned interval; 2170550a7375SFelipe Balbi 2171550a7375SFelipe Balbi /* host role must be active */ 2172550a7375SFelipe Balbi if (!is_host_active(musb) || !musb->is_active) 2173550a7375SFelipe Balbi return -ENODEV; 2174550a7375SFelipe Balbi 2175550a7375SFelipe Balbi spin_lock_irqsave(&musb->lock, flags); 2176550a7375SFelipe Balbi ret = usb_hcd_link_urb_to_ep(hcd, urb); 217774bb3508SDavid Brownell qh = ret ? NULL : hep->hcpriv; 217874bb3508SDavid Brownell if (qh) 217974bb3508SDavid Brownell urb->hcpriv = qh; 2180550a7375SFelipe Balbi spin_unlock_irqrestore(&musb->lock, flags); 2181550a7375SFelipe Balbi 2182550a7375SFelipe Balbi /* DMA mapping was already done, if needed, and this urb is on 218374bb3508SDavid Brownell * hep->urb_list now ... so we're done, unless hep wasn't yet 218474bb3508SDavid Brownell * scheduled onto a live qh. 2185550a7375SFelipe Balbi * 2186550a7375SFelipe Balbi * REVISIT best to keep hep->hcpriv valid until the endpoint gets 2187550a7375SFelipe Balbi * disabled, testing for empty qh->ring and avoiding qh setup costs 2188550a7375SFelipe Balbi * except for the first urb queued after a config change. 2189550a7375SFelipe Balbi */ 219074bb3508SDavid Brownell if (qh || ret) 219174bb3508SDavid Brownell return ret; 2192550a7375SFelipe Balbi 2193550a7375SFelipe Balbi /* Allocate and initialize qh, minimizing the work done each time 2194550a7375SFelipe Balbi * hw_ep gets reprogrammed, or with irqs blocked. Then schedule it. 2195550a7375SFelipe Balbi * 2196550a7375SFelipe Balbi * REVISIT consider a dedicated qh kmem_cache, so it's harder 2197550a7375SFelipe Balbi * for bugs in other kernel code to break this driver... 2198550a7375SFelipe Balbi */ 2199550a7375SFelipe Balbi qh = kzalloc(sizeof *qh, mem_flags); 2200550a7375SFelipe Balbi if (!qh) { 22012492e674SAjay Kumar Gupta spin_lock_irqsave(&musb->lock, flags); 2202550a7375SFelipe Balbi usb_hcd_unlink_urb_from_ep(hcd, urb); 22032492e674SAjay Kumar Gupta spin_unlock_irqrestore(&musb->lock, flags); 2204550a7375SFelipe Balbi return -ENOMEM; 2205550a7375SFelipe Balbi } 2206550a7375SFelipe Balbi 2207550a7375SFelipe Balbi qh->hep = hep; 2208550a7375SFelipe Balbi qh->dev = urb->dev; 2209550a7375SFelipe Balbi INIT_LIST_HEAD(&qh->ring); 2210550a7375SFelipe Balbi qh->is_ready = 1; 2211550a7375SFelipe Balbi 221229cc8897SKuninori Morimoto qh->maxpacket = usb_endpoint_maxp(epd); 2213a483d706SAjay Kumar Gupta qh->type = usb_endpoint_type(epd); 2214550a7375SFelipe Balbi 2215a483d706SAjay Kumar Gupta /* Bits 11 & 12 of wMaxPacketSize encode high bandwidth multiplier. 2216a483d706SAjay Kumar Gupta * Some musb cores don't support high bandwidth ISO transfers; and 2217a483d706SAjay Kumar Gupta * we don't (yet!) support high bandwidth interrupt transfers. 2218a483d706SAjay Kumar Gupta */ 2219a483d706SAjay Kumar Gupta qh->hb_mult = 1 + ((qh->maxpacket >> 11) & 0x03); 2220a483d706SAjay Kumar Gupta if (qh->hb_mult > 1) { 2221a483d706SAjay Kumar Gupta int ok = (qh->type == USB_ENDPOINT_XFER_ISOC); 2222a483d706SAjay Kumar Gupta 2223a483d706SAjay Kumar Gupta if (ok) 2224a483d706SAjay Kumar Gupta ok = (usb_pipein(urb->pipe) && musb->hb_iso_rx) 2225a483d706SAjay Kumar Gupta || (usb_pipeout(urb->pipe) && musb->hb_iso_tx); 2226a483d706SAjay Kumar Gupta if (!ok) { 2227550a7375SFelipe Balbi ret = -EMSGSIZE; 2228550a7375SFelipe Balbi goto done; 2229550a7375SFelipe Balbi } 2230a483d706SAjay Kumar Gupta qh->maxpacket &= 0x7ff; 2231a483d706SAjay Kumar Gupta } 2232550a7375SFelipe Balbi 223396bcd090SJulia Lawall qh->epnum = usb_endpoint_num(epd); 2234550a7375SFelipe Balbi 2235550a7375SFelipe Balbi /* NOTE: urb->dev->devnum is wrong during SET_ADDRESS */ 2236550a7375SFelipe Balbi qh->addr_reg = (u8) usb_pipedevice(urb->pipe); 2237550a7375SFelipe Balbi 2238550a7375SFelipe Balbi /* precompute rxtype/txtype/type0 register */ 2239550a7375SFelipe Balbi type_reg = (qh->type << 4) | qh->epnum; 2240550a7375SFelipe Balbi switch (urb->dev->speed) { 2241550a7375SFelipe Balbi case USB_SPEED_LOW: 2242550a7375SFelipe Balbi type_reg |= 0xc0; 2243550a7375SFelipe Balbi break; 2244550a7375SFelipe Balbi case USB_SPEED_FULL: 2245550a7375SFelipe Balbi type_reg |= 0x80; 2246550a7375SFelipe Balbi break; 2247550a7375SFelipe Balbi default: 2248550a7375SFelipe Balbi type_reg |= 0x40; 2249550a7375SFelipe Balbi } 2250550a7375SFelipe Balbi qh->type_reg = type_reg; 2251550a7375SFelipe Balbi 2252136733d6SSergei Shtylyov /* Precompute RXINTERVAL/TXINTERVAL register */ 2253550a7375SFelipe Balbi switch (qh->type) { 2254550a7375SFelipe Balbi case USB_ENDPOINT_XFER_INT: 2255136733d6SSergei Shtylyov /* 2256136733d6SSergei Shtylyov * Full/low speeds use the linear encoding, 2257136733d6SSergei Shtylyov * high speed uses the logarithmic encoding. 2258136733d6SSergei Shtylyov */ 2259136733d6SSergei Shtylyov if (urb->dev->speed <= USB_SPEED_FULL) { 2260136733d6SSergei Shtylyov interval = max_t(u8, epd->bInterval, 1); 2261136733d6SSergei Shtylyov break; 2262550a7375SFelipe Balbi } 2263550a7375SFelipe Balbi /* FALLTHROUGH */ 2264550a7375SFelipe Balbi case USB_ENDPOINT_XFER_ISOC: 2265136733d6SSergei Shtylyov /* ISO always uses logarithmic encoding */ 2266136733d6SSergei Shtylyov interval = min_t(u8, epd->bInterval, 16); 2267550a7375SFelipe Balbi break; 2268550a7375SFelipe Balbi default: 2269550a7375SFelipe Balbi /* REVISIT we actually want to use NAK limits, hinting to the 2270550a7375SFelipe Balbi * transfer scheduling logic to try some other qh, e.g. try 2271550a7375SFelipe Balbi * for 2 msec first: 2272550a7375SFelipe Balbi * 2273550a7375SFelipe Balbi * interval = (USB_SPEED_HIGH == urb->dev->speed) ? 16 : 2; 2274550a7375SFelipe Balbi * 2275550a7375SFelipe Balbi * The downside of disabling this is that transfer scheduling 2276550a7375SFelipe Balbi * gets VERY unfair for nonperiodic transfers; a misbehaving 22771e0320f0SAjay Kumar Gupta * peripheral could make that hurt. That's perfectly normal 22781e0320f0SAjay Kumar Gupta * for reads from network or serial adapters ... so we have 22791e0320f0SAjay Kumar Gupta * partial NAKlimit support for bulk RX. 2280550a7375SFelipe Balbi * 22811e0320f0SAjay Kumar Gupta * The upside of disabling it is simpler transfer scheduling. 2282550a7375SFelipe Balbi */ 2283550a7375SFelipe Balbi interval = 0; 2284550a7375SFelipe Balbi } 2285550a7375SFelipe Balbi qh->intv_reg = interval; 2286550a7375SFelipe Balbi 2287550a7375SFelipe Balbi /* precompute addressing for external hub/tt ports */ 2288550a7375SFelipe Balbi if (musb->is_multipoint) { 2289550a7375SFelipe Balbi struct usb_device *parent = urb->dev->parent; 2290550a7375SFelipe Balbi 2291550a7375SFelipe Balbi if (parent != hcd->self.root_hub) { 2292550a7375SFelipe Balbi qh->h_addr_reg = (u8) parent->devnum; 2293550a7375SFelipe Balbi 2294550a7375SFelipe Balbi /* set up tt info if needed */ 2295550a7375SFelipe Balbi if (urb->dev->tt) { 2296550a7375SFelipe Balbi qh->h_port_reg = (u8) urb->dev->ttport; 2297ae5ad296SAjay Kumar Gupta if (urb->dev->tt->hub) 2298ae5ad296SAjay Kumar Gupta qh->h_addr_reg = 2299ae5ad296SAjay Kumar Gupta (u8) urb->dev->tt->hub->devnum; 2300ae5ad296SAjay Kumar Gupta if (urb->dev->tt->multi) 2301550a7375SFelipe Balbi qh->h_addr_reg |= 0x80; 2302550a7375SFelipe Balbi } 2303550a7375SFelipe Balbi } 2304550a7375SFelipe Balbi } 2305550a7375SFelipe Balbi 2306550a7375SFelipe Balbi /* invariant: hep->hcpriv is null OR the qh that's already scheduled. 2307550a7375SFelipe Balbi * until we get real dma queues (with an entry for each urb/buffer), 2308550a7375SFelipe Balbi * we only have work to do in the former case. 2309550a7375SFelipe Balbi */ 2310550a7375SFelipe Balbi spin_lock_irqsave(&musb->lock, flags); 23113067779bSyuzheng ma if (hep->hcpriv || !next_urb(qh)) { 2312550a7375SFelipe Balbi /* some concurrent activity submitted another urb to hep... 2313550a7375SFelipe Balbi * odd, rare, error prone, but legal. 2314550a7375SFelipe Balbi */ 2315550a7375SFelipe Balbi kfree(qh); 2316714bc5efSDan Carpenter qh = NULL; 2317550a7375SFelipe Balbi ret = 0; 2318550a7375SFelipe Balbi } else 2319550a7375SFelipe Balbi ret = musb_schedule(musb, qh, 2320550a7375SFelipe Balbi epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK); 2321550a7375SFelipe Balbi 2322550a7375SFelipe Balbi if (ret == 0) { 2323550a7375SFelipe Balbi urb->hcpriv = qh; 2324550a7375SFelipe Balbi /* FIXME set urb->start_frame for iso/intr, it's tested in 2325550a7375SFelipe Balbi * musb_start_urb(), but otherwise only konicawc cares ... 2326550a7375SFelipe Balbi */ 2327550a7375SFelipe Balbi } 2328550a7375SFelipe Balbi spin_unlock_irqrestore(&musb->lock, flags); 2329550a7375SFelipe Balbi 2330550a7375SFelipe Balbi done: 2331550a7375SFelipe Balbi if (ret != 0) { 23322492e674SAjay Kumar Gupta spin_lock_irqsave(&musb->lock, flags); 2333550a7375SFelipe Balbi usb_hcd_unlink_urb_from_ep(hcd, urb); 23342492e674SAjay Kumar Gupta spin_unlock_irqrestore(&musb->lock, flags); 2335550a7375SFelipe Balbi kfree(qh); 2336550a7375SFelipe Balbi } 2337550a7375SFelipe Balbi return ret; 2338550a7375SFelipe Balbi } 2339550a7375SFelipe Balbi 2340550a7375SFelipe Balbi 2341550a7375SFelipe Balbi /* 2342550a7375SFelipe Balbi * abort a transfer that's at the head of a hardware queue. 2343550a7375SFelipe Balbi * called with controller locked, irqs blocked 2344550a7375SFelipe Balbi * that hardware queue advances to the next transfer, unless prevented 2345550a7375SFelipe Balbi */ 234681ec4e4aSSergei Shtylyov static int musb_cleanup_urb(struct urb *urb, struct musb_qh *qh) 2347550a7375SFelipe Balbi { 2348550a7375SFelipe Balbi struct musb_hw_ep *ep = qh->hw_ep; 23495c8a86e1SFelipe Balbi struct musb *musb = ep->musb; 2350550a7375SFelipe Balbi void __iomem *epio = ep->regs; 2351550a7375SFelipe Balbi unsigned hw_end = ep->epnum; 2352550a7375SFelipe Balbi void __iomem *regs = ep->musb->mregs; 235381ec4e4aSSergei Shtylyov int is_in = usb_pipein(urb->pipe); 2354550a7375SFelipe Balbi int status = 0; 235581ec4e4aSSergei Shtylyov u16 csr; 2356550a7375SFelipe Balbi 2357550a7375SFelipe Balbi musb_ep_select(regs, hw_end); 2358550a7375SFelipe Balbi 2359550a7375SFelipe Balbi if (is_dma_capable()) { 2360550a7375SFelipe Balbi struct dma_channel *dma; 2361550a7375SFelipe Balbi 2362550a7375SFelipe Balbi dma = is_in ? ep->rx_channel : ep->tx_channel; 2363550a7375SFelipe Balbi if (dma) { 2364550a7375SFelipe Balbi status = ep->musb->dma_controller->channel_abort(dma); 23655c8a86e1SFelipe Balbi dev_dbg(musb->controller, 2366550a7375SFelipe Balbi "abort %cX%d DMA for urb %p --> %d\n", 2367550a7375SFelipe Balbi is_in ? 'R' : 'T', ep->epnum, 2368550a7375SFelipe Balbi urb, status); 2369550a7375SFelipe Balbi urb->actual_length += dma->actual_len; 2370550a7375SFelipe Balbi } 2371550a7375SFelipe Balbi } 2372550a7375SFelipe Balbi 2373550a7375SFelipe Balbi /* turn off DMA requests, discard state, stop polling ... */ 2374692933b2SAjay Kumar Gupta if (ep->epnum && is_in) { 2375550a7375SFelipe Balbi /* giveback saves bulk toggle */ 2376550a7375SFelipe Balbi csr = musb_h_flush_rxfifo(ep, 0); 2377550a7375SFelipe Balbi 2378550a7375SFelipe Balbi /* REVISIT we still get an irq; should likely clear the 2379550a7375SFelipe Balbi * endpoint's irq status here to avoid bogus irqs. 2380550a7375SFelipe Balbi * clearing that status is platform-specific... 2381550a7375SFelipe Balbi */ 238278322c1aSDavid Brownell } else if (ep->epnum) { 2383550a7375SFelipe Balbi musb_h_tx_flush_fifo(ep); 2384550a7375SFelipe Balbi csr = musb_readw(epio, MUSB_TXCSR); 2385550a7375SFelipe Balbi csr &= ~(MUSB_TXCSR_AUTOSET 2386550a7375SFelipe Balbi | MUSB_TXCSR_DMAENAB 2387550a7375SFelipe Balbi | MUSB_TXCSR_H_RXSTALL 2388550a7375SFelipe Balbi | MUSB_TXCSR_H_NAKTIMEOUT 2389550a7375SFelipe Balbi | MUSB_TXCSR_H_ERROR 2390550a7375SFelipe Balbi | MUSB_TXCSR_TXPKTRDY); 2391550a7375SFelipe Balbi musb_writew(epio, MUSB_TXCSR, csr); 2392550a7375SFelipe Balbi /* REVISIT may need to clear FLUSHFIFO ... */ 2393550a7375SFelipe Balbi musb_writew(epio, MUSB_TXCSR, csr); 2394550a7375SFelipe Balbi /* flush cpu writebuffer */ 2395550a7375SFelipe Balbi csr = musb_readw(epio, MUSB_TXCSR); 239678322c1aSDavid Brownell } else { 239778322c1aSDavid Brownell musb_h_ep0_flush_fifo(ep); 2398550a7375SFelipe Balbi } 2399550a7375SFelipe Balbi if (status == 0) 2400550a7375SFelipe Balbi musb_advance_schedule(ep->musb, urb, ep, is_in); 2401550a7375SFelipe Balbi return status; 2402550a7375SFelipe Balbi } 2403550a7375SFelipe Balbi 2404550a7375SFelipe Balbi static int musb_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status) 2405550a7375SFelipe Balbi { 2406550a7375SFelipe Balbi struct musb *musb = hcd_to_musb(hcd); 2407550a7375SFelipe Balbi struct musb_qh *qh; 2408550a7375SFelipe Balbi unsigned long flags; 240922a0d6f1SSergei Shtylyov int is_in = usb_pipein(urb->pipe); 2410550a7375SFelipe Balbi int ret; 2411550a7375SFelipe Balbi 24125c8a86e1SFelipe Balbi dev_dbg(musb->controller, "urb=%p, dev%d ep%d%s\n", urb, 2413550a7375SFelipe Balbi usb_pipedevice(urb->pipe), 2414550a7375SFelipe Balbi usb_pipeendpoint(urb->pipe), 241522a0d6f1SSergei Shtylyov is_in ? "in" : "out"); 2416550a7375SFelipe Balbi 2417550a7375SFelipe Balbi spin_lock_irqsave(&musb->lock, flags); 2418550a7375SFelipe Balbi ret = usb_hcd_check_unlink_urb(hcd, urb, status); 2419550a7375SFelipe Balbi if (ret) 2420550a7375SFelipe Balbi goto done; 2421550a7375SFelipe Balbi 2422550a7375SFelipe Balbi qh = urb->hcpriv; 2423550a7375SFelipe Balbi if (!qh) 2424550a7375SFelipe Balbi goto done; 2425550a7375SFelipe Balbi 242622a0d6f1SSergei Shtylyov /* 242722a0d6f1SSergei Shtylyov * Any URB not actively programmed into endpoint hardware can be 2428a2fd814eSSergei Shtylyov * immediately given back; that's any URB not at the head of an 2429550a7375SFelipe Balbi * endpoint queue, unless someday we get real DMA queues. And even 2430a2fd814eSSergei Shtylyov * if it's at the head, it might not be known to the hardware... 2431550a7375SFelipe Balbi * 243222a0d6f1SSergei Shtylyov * Otherwise abort current transfer, pending DMA, etc.; urb->status 2433550a7375SFelipe Balbi * has already been updated. This is a synchronous abort; it'd be 2434550a7375SFelipe Balbi * OK to hold off until after some IRQ, though. 243522a0d6f1SSergei Shtylyov * 243622a0d6f1SSergei Shtylyov * NOTE: qh is invalid unless !list_empty(&hep->urb_list) 2437550a7375SFelipe Balbi */ 243822a0d6f1SSergei Shtylyov if (!qh->is_ready 243922a0d6f1SSergei Shtylyov || urb->urb_list.prev != &qh->hep->urb_list 244022a0d6f1SSergei Shtylyov || musb_ep_get_qh(qh->hw_ep, is_in) != qh) { 2441550a7375SFelipe Balbi int ready = qh->is_ready; 2442550a7375SFelipe Balbi 2443550a7375SFelipe Balbi qh->is_ready = 0; 2444c9cd06b3SSergei Shtylyov musb_giveback(musb, urb, 0); 2445550a7375SFelipe Balbi qh->is_ready = ready; 2446a2fd814eSSergei Shtylyov 2447a2fd814eSSergei Shtylyov /* If nothing else (usually musb_giveback) is using it 2448a2fd814eSSergei Shtylyov * and its URB list has emptied, recycle this qh. 2449a2fd814eSSergei Shtylyov */ 2450a2fd814eSSergei Shtylyov if (ready && list_empty(&qh->hep->urb_list)) { 2451a2fd814eSSergei Shtylyov qh->hep->hcpriv = NULL; 2452a2fd814eSSergei Shtylyov list_del(&qh->ring); 2453a2fd814eSSergei Shtylyov kfree(qh); 2454a2fd814eSSergei Shtylyov } 2455550a7375SFelipe Balbi } else 245681ec4e4aSSergei Shtylyov ret = musb_cleanup_urb(urb, qh); 2457550a7375SFelipe Balbi done: 2458550a7375SFelipe Balbi spin_unlock_irqrestore(&musb->lock, flags); 2459550a7375SFelipe Balbi return ret; 2460550a7375SFelipe Balbi } 2461550a7375SFelipe Balbi 2462550a7375SFelipe Balbi /* disable an endpoint */ 2463550a7375SFelipe Balbi static void 2464550a7375SFelipe Balbi musb_h_disable(struct usb_hcd *hcd, struct usb_host_endpoint *hep) 2465550a7375SFelipe Balbi { 246622a0d6f1SSergei Shtylyov u8 is_in = hep->desc.bEndpointAddress & USB_DIR_IN; 2467550a7375SFelipe Balbi unsigned long flags; 2468550a7375SFelipe Balbi struct musb *musb = hcd_to_musb(hcd); 2469dc61d238SSergei Shtylyov struct musb_qh *qh; 2470dc61d238SSergei Shtylyov struct urb *urb; 2471550a7375SFelipe Balbi 2472550a7375SFelipe Balbi spin_lock_irqsave(&musb->lock, flags); 2473550a7375SFelipe Balbi 2474dc61d238SSergei Shtylyov qh = hep->hcpriv; 2475dc61d238SSergei Shtylyov if (qh == NULL) 2476dc61d238SSergei Shtylyov goto exit; 2477dc61d238SSergei Shtylyov 2478550a7375SFelipe Balbi /* NOTE: qh is invalid unless !list_empty(&hep->urb_list) */ 2479550a7375SFelipe Balbi 248022a0d6f1SSergei Shtylyov /* Kick the first URB off the hardware, if needed */ 2481550a7375SFelipe Balbi qh->is_ready = 0; 248222a0d6f1SSergei Shtylyov if (musb_ep_get_qh(qh->hw_ep, is_in) == qh) { 2483550a7375SFelipe Balbi urb = next_urb(qh); 2484550a7375SFelipe Balbi 2485550a7375SFelipe Balbi /* make software (then hardware) stop ASAP */ 2486550a7375SFelipe Balbi if (!urb->unlinked) 2487550a7375SFelipe Balbi urb->status = -ESHUTDOWN; 2488550a7375SFelipe Balbi 2489550a7375SFelipe Balbi /* cleanup */ 249081ec4e4aSSergei Shtylyov musb_cleanup_urb(urb, qh); 2491550a7375SFelipe Balbi 2492dc61d238SSergei Shtylyov /* Then nuke all the others ... and advance the 2493dc61d238SSergei Shtylyov * queue on hw_ep (e.g. bulk ring) when we're done. 2494dc61d238SSergei Shtylyov */ 2495dc61d238SSergei Shtylyov while (!list_empty(&hep->urb_list)) { 2496dc61d238SSergei Shtylyov urb = next_urb(qh); 2497dc61d238SSergei Shtylyov urb->status = -ESHUTDOWN; 2498dc61d238SSergei Shtylyov musb_advance_schedule(musb, urb, qh->hw_ep, is_in); 2499dc61d238SSergei Shtylyov } 2500dc61d238SSergei Shtylyov } else { 2501dc61d238SSergei Shtylyov /* Just empty the queue; the hardware is busy with 2502dc61d238SSergei Shtylyov * other transfers, and since !qh->is_ready nothing 2503dc61d238SSergei Shtylyov * will activate any of these as it advances. 2504dc61d238SSergei Shtylyov */ 2505dc61d238SSergei Shtylyov while (!list_empty(&hep->urb_list)) 2506c9cd06b3SSergei Shtylyov musb_giveback(musb, next_urb(qh), -ESHUTDOWN); 2507550a7375SFelipe Balbi 2508dc61d238SSergei Shtylyov hep->hcpriv = NULL; 2509dc61d238SSergei Shtylyov list_del(&qh->ring); 2510dc61d238SSergei Shtylyov kfree(qh); 2511dc61d238SSergei Shtylyov } 2512dc61d238SSergei Shtylyov exit: 2513550a7375SFelipe Balbi spin_unlock_irqrestore(&musb->lock, flags); 2514550a7375SFelipe Balbi } 2515550a7375SFelipe Balbi 2516550a7375SFelipe Balbi static int musb_h_get_frame_number(struct usb_hcd *hcd) 2517550a7375SFelipe Balbi { 2518550a7375SFelipe Balbi struct musb *musb = hcd_to_musb(hcd); 2519550a7375SFelipe Balbi 2520550a7375SFelipe Balbi return musb_readw(musb->mregs, MUSB_FRAME); 2521550a7375SFelipe Balbi } 2522550a7375SFelipe Balbi 2523550a7375SFelipe Balbi static int musb_h_start(struct usb_hcd *hcd) 2524550a7375SFelipe Balbi { 2525550a7375SFelipe Balbi struct musb *musb = hcd_to_musb(hcd); 2526550a7375SFelipe Balbi 2527550a7375SFelipe Balbi /* NOTE: musb_start() is called when the hub driver turns 2528550a7375SFelipe Balbi * on port power, or when (OTG) peripheral starts. 2529550a7375SFelipe Balbi */ 2530550a7375SFelipe Balbi hcd->state = HC_STATE_RUNNING; 2531550a7375SFelipe Balbi musb->port1_status = 0; 2532550a7375SFelipe Balbi return 0; 2533550a7375SFelipe Balbi } 2534550a7375SFelipe Balbi 2535550a7375SFelipe Balbi static void musb_h_stop(struct usb_hcd *hcd) 2536550a7375SFelipe Balbi { 2537550a7375SFelipe Balbi musb_stop(hcd_to_musb(hcd)); 2538550a7375SFelipe Balbi hcd->state = HC_STATE_HALT; 2539550a7375SFelipe Balbi } 2540550a7375SFelipe Balbi 2541550a7375SFelipe Balbi static int musb_bus_suspend(struct usb_hcd *hcd) 2542550a7375SFelipe Balbi { 2543550a7375SFelipe Balbi struct musb *musb = hcd_to_musb(hcd); 254489368d3dSDavid Brownell u8 devctl; 2545550a7375SFelipe Balbi 254694f72136SDaniel Mack musb_port_suspend(musb, true); 254794f72136SDaniel Mack 254889368d3dSDavid Brownell if (!is_host_active(musb)) 2549550a7375SFelipe Balbi return 0; 2550550a7375SFelipe Balbi 2551e47d9254SAntoine Tenart switch (musb->xceiv->otg->state) { 255289368d3dSDavid Brownell case OTG_STATE_A_SUSPEND: 255389368d3dSDavid Brownell return 0; 255489368d3dSDavid Brownell case OTG_STATE_A_WAIT_VRISE: 255589368d3dSDavid Brownell /* ID could be grounded even if there's no device 255689368d3dSDavid Brownell * on the other end of the cable. NOTE that the 255789368d3dSDavid Brownell * A_WAIT_VRISE timers are messy with MUSB... 255889368d3dSDavid Brownell */ 255989368d3dSDavid Brownell devctl = musb_readb(musb->mregs, MUSB_DEVCTL); 256089368d3dSDavid Brownell if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS) 2561e47d9254SAntoine Tenart musb->xceiv->otg->state = OTG_STATE_A_WAIT_BCON; 256289368d3dSDavid Brownell break; 256389368d3dSDavid Brownell default: 256489368d3dSDavid Brownell break; 256589368d3dSDavid Brownell } 256689368d3dSDavid Brownell 256789368d3dSDavid Brownell if (musb->is_active) { 256889368d3dSDavid Brownell WARNING("trying to suspend as %s while active\n", 2569e47d9254SAntoine Tenart usb_otg_state_string(musb->xceiv->otg->state)); 2570550a7375SFelipe Balbi return -EBUSY; 2571550a7375SFelipe Balbi } else 2572550a7375SFelipe Balbi return 0; 2573550a7375SFelipe Balbi } 2574550a7375SFelipe Balbi 2575550a7375SFelipe Balbi static int musb_bus_resume(struct usb_hcd *hcd) 2576550a7375SFelipe Balbi { 2577869c5978SDaniel Mack struct musb *musb = hcd_to_musb(hcd); 2578869c5978SDaniel Mack 2579869c5978SDaniel Mack if (musb->config && 2580869c5978SDaniel Mack musb->config->host_port_deassert_reset_at_resume) 2581869c5978SDaniel Mack musb_port_reset(musb, false); 2582869c5978SDaniel Mack 2583550a7375SFelipe Balbi return 0; 2584550a7375SFelipe Balbi } 2585550a7375SFelipe Balbi 25868408fd1dSRuslan Bilovol #ifndef CONFIG_MUSB_PIO_ONLY 25878408fd1dSRuslan Bilovol 25888408fd1dSRuslan Bilovol #define MUSB_USB_DMA_ALIGN 4 25898408fd1dSRuslan Bilovol 25908408fd1dSRuslan Bilovol struct musb_temp_buffer { 25918408fd1dSRuslan Bilovol void *kmalloc_ptr; 25928408fd1dSRuslan Bilovol void *old_xfer_buffer; 25938408fd1dSRuslan Bilovol u8 data[0]; 25948408fd1dSRuslan Bilovol }; 25958408fd1dSRuslan Bilovol 25968408fd1dSRuslan Bilovol static void musb_free_temp_buffer(struct urb *urb) 25978408fd1dSRuslan Bilovol { 25988408fd1dSRuslan Bilovol enum dma_data_direction dir; 25998408fd1dSRuslan Bilovol struct musb_temp_buffer *temp; 2600d72348fbSJohan Hovold size_t length; 26018408fd1dSRuslan Bilovol 26028408fd1dSRuslan Bilovol if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER)) 26038408fd1dSRuslan Bilovol return; 26048408fd1dSRuslan Bilovol 26058408fd1dSRuslan Bilovol dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE; 26068408fd1dSRuslan Bilovol 26078408fd1dSRuslan Bilovol temp = container_of(urb->transfer_buffer, struct musb_temp_buffer, 26088408fd1dSRuslan Bilovol data); 26098408fd1dSRuslan Bilovol 26108408fd1dSRuslan Bilovol if (dir == DMA_FROM_DEVICE) { 2611d72348fbSJohan Hovold if (usb_pipeisoc(urb->pipe)) 2612d72348fbSJohan Hovold length = urb->transfer_buffer_length; 2613d72348fbSJohan Hovold else 2614d72348fbSJohan Hovold length = urb->actual_length; 2615d72348fbSJohan Hovold 2616d72348fbSJohan Hovold memcpy(temp->old_xfer_buffer, temp->data, length); 26178408fd1dSRuslan Bilovol } 26188408fd1dSRuslan Bilovol urb->transfer_buffer = temp->old_xfer_buffer; 26198408fd1dSRuslan Bilovol kfree(temp->kmalloc_ptr); 26208408fd1dSRuslan Bilovol 26218408fd1dSRuslan Bilovol urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER; 26228408fd1dSRuslan Bilovol } 26238408fd1dSRuslan Bilovol 26248408fd1dSRuslan Bilovol static int musb_alloc_temp_buffer(struct urb *urb, gfp_t mem_flags) 26258408fd1dSRuslan Bilovol { 26268408fd1dSRuslan Bilovol enum dma_data_direction dir; 26278408fd1dSRuslan Bilovol struct musb_temp_buffer *temp; 26288408fd1dSRuslan Bilovol void *kmalloc_ptr; 26298408fd1dSRuslan Bilovol size_t kmalloc_size; 26308408fd1dSRuslan Bilovol 26318408fd1dSRuslan Bilovol if (urb->num_sgs || urb->sg || 26328408fd1dSRuslan Bilovol urb->transfer_buffer_length == 0 || 26338408fd1dSRuslan Bilovol !((uintptr_t)urb->transfer_buffer & (MUSB_USB_DMA_ALIGN - 1))) 26348408fd1dSRuslan Bilovol return 0; 26358408fd1dSRuslan Bilovol 26368408fd1dSRuslan Bilovol dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE; 26378408fd1dSRuslan Bilovol 26388408fd1dSRuslan Bilovol /* Allocate a buffer with enough padding for alignment */ 26398408fd1dSRuslan Bilovol kmalloc_size = urb->transfer_buffer_length + 26408408fd1dSRuslan Bilovol sizeof(struct musb_temp_buffer) + MUSB_USB_DMA_ALIGN - 1; 26418408fd1dSRuslan Bilovol 26428408fd1dSRuslan Bilovol kmalloc_ptr = kmalloc(kmalloc_size, mem_flags); 26438408fd1dSRuslan Bilovol if (!kmalloc_ptr) 26448408fd1dSRuslan Bilovol return -ENOMEM; 26458408fd1dSRuslan Bilovol 26468408fd1dSRuslan Bilovol /* Position our struct temp_buffer such that data is aligned */ 26478408fd1dSRuslan Bilovol temp = PTR_ALIGN(kmalloc_ptr, MUSB_USB_DMA_ALIGN); 26488408fd1dSRuslan Bilovol 26498408fd1dSRuslan Bilovol 26508408fd1dSRuslan Bilovol temp->kmalloc_ptr = kmalloc_ptr; 26518408fd1dSRuslan Bilovol temp->old_xfer_buffer = urb->transfer_buffer; 26528408fd1dSRuslan Bilovol if (dir == DMA_TO_DEVICE) 26538408fd1dSRuslan Bilovol memcpy(temp->data, urb->transfer_buffer, 26548408fd1dSRuslan Bilovol urb->transfer_buffer_length); 26558408fd1dSRuslan Bilovol urb->transfer_buffer = temp->data; 26568408fd1dSRuslan Bilovol 26578408fd1dSRuslan Bilovol urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER; 26588408fd1dSRuslan Bilovol 26598408fd1dSRuslan Bilovol return 0; 26608408fd1dSRuslan Bilovol } 26618408fd1dSRuslan Bilovol 26628408fd1dSRuslan Bilovol static int musb_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb, 26638408fd1dSRuslan Bilovol gfp_t mem_flags) 26648408fd1dSRuslan Bilovol { 26658408fd1dSRuslan Bilovol struct musb *musb = hcd_to_musb(hcd); 26668408fd1dSRuslan Bilovol int ret; 26678408fd1dSRuslan Bilovol 26688408fd1dSRuslan Bilovol /* 26698408fd1dSRuslan Bilovol * The DMA engine in RTL1.8 and above cannot handle 26708408fd1dSRuslan Bilovol * DMA addresses that are not aligned to a 4 byte boundary. 26718408fd1dSRuslan Bilovol * For such engine implemented (un)map_urb_for_dma hooks. 26728408fd1dSRuslan Bilovol * Do not use these hooks for RTL<1.8 26738408fd1dSRuslan Bilovol */ 26748408fd1dSRuslan Bilovol if (musb->hwvers < MUSB_HWVERS_1800) 26758408fd1dSRuslan Bilovol return usb_hcd_map_urb_for_dma(hcd, urb, mem_flags); 26768408fd1dSRuslan Bilovol 26778408fd1dSRuslan Bilovol ret = musb_alloc_temp_buffer(urb, mem_flags); 26788408fd1dSRuslan Bilovol if (ret) 26798408fd1dSRuslan Bilovol return ret; 26808408fd1dSRuslan Bilovol 26818408fd1dSRuslan Bilovol ret = usb_hcd_map_urb_for_dma(hcd, urb, mem_flags); 26828408fd1dSRuslan Bilovol if (ret) 26838408fd1dSRuslan Bilovol musb_free_temp_buffer(urb); 26848408fd1dSRuslan Bilovol 26858408fd1dSRuslan Bilovol return ret; 26868408fd1dSRuslan Bilovol } 26878408fd1dSRuslan Bilovol 26888408fd1dSRuslan Bilovol static void musb_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb) 26898408fd1dSRuslan Bilovol { 26908408fd1dSRuslan Bilovol struct musb *musb = hcd_to_musb(hcd); 26918408fd1dSRuslan Bilovol 26928408fd1dSRuslan Bilovol usb_hcd_unmap_urb_for_dma(hcd, urb); 26938408fd1dSRuslan Bilovol 26948408fd1dSRuslan Bilovol /* Do not use this hook for RTL<1.8 (see description above) */ 26958408fd1dSRuslan Bilovol if (musb->hwvers < MUSB_HWVERS_1800) 26968408fd1dSRuslan Bilovol return; 26978408fd1dSRuslan Bilovol 26988408fd1dSRuslan Bilovol musb_free_temp_buffer(urb); 26998408fd1dSRuslan Bilovol } 27008408fd1dSRuslan Bilovol #endif /* !CONFIG_MUSB_PIO_ONLY */ 27018408fd1dSRuslan Bilovol 270274c2e936SDaniel Mack static const struct hc_driver musb_hc_driver = { 2703550a7375SFelipe Balbi .description = "musb-hcd", 2704550a7375SFelipe Balbi .product_desc = "MUSB HDRC host driver", 270574c2e936SDaniel Mack .hcd_priv_size = sizeof(struct musb *), 270620357720SGeorge Cherian .flags = HCD_USB2 | HCD_MEMORY | HCD_BH, 2707550a7375SFelipe Balbi 2708550a7375SFelipe Balbi /* not using irq handler or reset hooks from usbcore, since 2709550a7375SFelipe Balbi * those must be shared with peripheral code for OTG configs 2710550a7375SFelipe Balbi */ 2711550a7375SFelipe Balbi 2712550a7375SFelipe Balbi .start = musb_h_start, 2713550a7375SFelipe Balbi .stop = musb_h_stop, 2714550a7375SFelipe Balbi 2715550a7375SFelipe Balbi .get_frame_number = musb_h_get_frame_number, 2716550a7375SFelipe Balbi 2717550a7375SFelipe Balbi .urb_enqueue = musb_urb_enqueue, 2718550a7375SFelipe Balbi .urb_dequeue = musb_urb_dequeue, 2719550a7375SFelipe Balbi .endpoint_disable = musb_h_disable, 2720550a7375SFelipe Balbi 27218408fd1dSRuslan Bilovol #ifndef CONFIG_MUSB_PIO_ONLY 27228408fd1dSRuslan Bilovol .map_urb_for_dma = musb_map_urb_for_dma, 27238408fd1dSRuslan Bilovol .unmap_urb_for_dma = musb_unmap_urb_for_dma, 27248408fd1dSRuslan Bilovol #endif 27258408fd1dSRuslan Bilovol 2726550a7375SFelipe Balbi .hub_status_data = musb_hub_status_data, 2727550a7375SFelipe Balbi .hub_control = musb_hub_control, 2728550a7375SFelipe Balbi .bus_suspend = musb_bus_suspend, 2729550a7375SFelipe Balbi .bus_resume = musb_bus_resume, 2730550a7375SFelipe Balbi /* .start_port_reset = NULL, */ 2731550a7375SFelipe Balbi /* .hub_irq_enable = NULL, */ 2732550a7375SFelipe Balbi }; 27330b3eba44SDaniel Mack 273474c2e936SDaniel Mack int musb_host_alloc(struct musb *musb) 273574c2e936SDaniel Mack { 273674c2e936SDaniel Mack struct device *dev = musb->controller; 273774c2e936SDaniel Mack 273874c2e936SDaniel Mack /* usbcore sets dev->driver_data to hcd, and sometimes uses that... */ 273974c2e936SDaniel Mack musb->hcd = usb_create_hcd(&musb_hc_driver, dev, dev_name(dev)); 274074c2e936SDaniel Mack if (!musb->hcd) 274174c2e936SDaniel Mack return -EINVAL; 274274c2e936SDaniel Mack 274374c2e936SDaniel Mack *musb->hcd->hcd_priv = (unsigned long) musb; 274474c2e936SDaniel Mack musb->hcd->self.uses_pio_for_control = 1; 274574c2e936SDaniel Mack musb->hcd->uses_new_polling = 1; 274674c2e936SDaniel Mack musb->hcd->has_tt = 1; 274774c2e936SDaniel Mack 274874c2e936SDaniel Mack return 0; 274974c2e936SDaniel Mack } 275074c2e936SDaniel Mack 275174c2e936SDaniel Mack void musb_host_cleanup(struct musb *musb) 275274c2e936SDaniel Mack { 275390474288SSebastian Andrzej Siewior if (musb->port_mode == MUSB_PORT_MODE_GADGET) 275490474288SSebastian Andrzej Siewior return; 275574c2e936SDaniel Mack usb_remove_hcd(musb->hcd); 275674c2e936SDaniel Mack } 275774c2e936SDaniel Mack 275874c2e936SDaniel Mack void musb_host_free(struct musb *musb) 275974c2e936SDaniel Mack { 276074c2e936SDaniel Mack usb_put_hcd(musb->hcd); 276174c2e936SDaniel Mack } 276274c2e936SDaniel Mack 27632cc65feaSDaniel Mack int musb_host_setup(struct musb *musb, int power_budget) 27642cc65feaSDaniel Mack { 27652cc65feaSDaniel Mack int ret; 27662cc65feaSDaniel Mack struct usb_hcd *hcd = musb->hcd; 27672cc65feaSDaniel Mack 27682cc65feaSDaniel Mack MUSB_HST_MODE(musb); 27692cc65feaSDaniel Mack musb->xceiv->otg->default_a = 1; 2770e47d9254SAntoine Tenart musb->xceiv->otg->state = OTG_STATE_A_IDLE; 27712cc65feaSDaniel Mack 27722cc65feaSDaniel Mack otg_set_host(musb->xceiv->otg, &hcd->self); 27732cc65feaSDaniel Mack hcd->self.otg_port = 1; 27742cc65feaSDaniel Mack musb->xceiv->otg->host = &hcd->self; 27752cc65feaSDaniel Mack hcd->power_budget = 2 * (power_budget ? : 250); 27762cc65feaSDaniel Mack 27772cc65feaSDaniel Mack ret = usb_add_hcd(hcd, 0, 0); 27782cc65feaSDaniel Mack if (ret < 0) 27792cc65feaSDaniel Mack return ret; 27802cc65feaSDaniel Mack 27813c9740a1SPeter Chen device_wakeup_enable(hcd->self.controller); 27822cc65feaSDaniel Mack return 0; 27832cc65feaSDaniel Mack } 27842cc65feaSDaniel Mack 27850b3eba44SDaniel Mack void musb_host_resume_root_hub(struct musb *musb) 27860b3eba44SDaniel Mack { 278774c2e936SDaniel Mack usb_hcd_resume_root_hub(musb->hcd); 27880b3eba44SDaniel Mack } 27890b3eba44SDaniel Mack 27900b3eba44SDaniel Mack void musb_host_poke_root_hub(struct musb *musb) 27910b3eba44SDaniel Mack { 27920b3eba44SDaniel Mack MUSB_HST_MODE(musb); 279374c2e936SDaniel Mack if (musb->hcd->status_urb) 279474c2e936SDaniel Mack usb_hcd_poll_rh_status(musb->hcd); 27950b3eba44SDaniel Mack else 279674c2e936SDaniel Mack usb_hcd_resume_root_hub(musb->hcd); 27970b3eba44SDaniel Mack } 2798