Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1 | /* |
| 2 | * MUSB OTG driver host support |
| 3 | * |
| 4 | * Copyright 2005 Mentor Graphics Corporation |
| 5 | * Copyright (C) 2005-2006 by Texas Instruments |
| 6 | * Copyright (C) 2006-2007 Nokia Corporation |
Sergei Shtylyov | c7bbc05 | 2009-03-26 18:26:40 -0700 | [diff] [blame] | 7 | * Copyright (C) 2008-2009 MontaVista Software, Inc. <source@mvista.com> |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 8 | * |
| 9 | * This program is free software; you can redistribute it and/or |
| 10 | * modify it under the terms of the GNU General Public License |
| 11 | * version 2 as published by the Free Software Foundation. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, but |
| 14 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 21 | * 02110-1301 USA |
| 22 | * |
| 23 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 24 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 25 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN |
| 26 | * NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 27 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 28 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
| 29 | * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
| 30 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 31 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 32 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | #include <linux/module.h> |
| 37 | #include <linux/kernel.h> |
| 38 | #include <linux/delay.h> |
| 39 | #include <linux/sched.h> |
| 40 | #include <linux/slab.h> |
| 41 | #include <linux/errno.h> |
| 42 | #include <linux/init.h> |
| 43 | #include <linux/list.h> |
Maulik Mankad | 496dda7 | 2010-09-24 13:44:06 +0300 | [diff] [blame] | 44 | #include <linux/dma-mapping.h> |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 45 | |
| 46 | #include "musb_core.h" |
| 47 | #include "musb_host.h" |
| 48 | |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 49 | /* MUSB HOST status 22-mar-2006 |
| 50 | * |
| 51 | * - There's still lots of partial code duplication for fault paths, so |
| 52 | * they aren't handled as consistently as they need to be. |
| 53 | * |
| 54 | * - PIO mostly behaved when last tested. |
| 55 | * + including ep0, with all usbtest cases 9, 10 |
| 56 | * + usbtest 14 (ep0out) doesn't seem to run at all |
| 57 | * + double buffered OUT/TX endpoints saw stalls(!) with certain usbtest |
| 58 | * configurations, but otherwise double buffering passes basic tests. |
| 59 | * + for 2.6.N, for N > ~10, needs API changes for hcd framework. |
| 60 | * |
| 61 | * - DMA (CPPI) ... partially behaves, not currently recommended |
| 62 | * + about 1/15 the speed of typical EHCI implementations (PCI) |
| 63 | * + RX, all too often reqpkt seems to misbehave after tx |
| 64 | * + TX, no known issues (other than evident silicon issue) |
| 65 | * |
| 66 | * - DMA (Mentor/OMAP) ...has at least toggle update problems |
| 67 | * |
Ajay Kumar Gupta | 1e0320f | 2009-02-24 15:26:13 -0800 | [diff] [blame] | 68 | * - [23-feb-2009] minimal traffic scheduling to avoid bulk RX packet |
| 69 | * starvation ... nothing yet for TX, interrupt, or bulk. |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 70 | * |
| 71 | * - Not tested with HNP, but some SRP paths seem to behave. |
| 72 | * |
| 73 | * NOTE 24-August-2006: |
| 74 | * |
| 75 | * - Bulk traffic finally uses both sides of hardware ep1, freeing up an |
| 76 | * extra endpoint for periodic use enabling hub + keybd + mouse. That |
| 77 | * mostly works, except that with "usbnet" it's easy to trigger cases |
| 78 | * with "ping" where RX loses. (a) ping to davinci, even "ping -f", |
| 79 | * fine; but (b) ping _from_ davinci, even "ping -c 1", ICMP RX loses |
| 80 | * although ARP RX wins. (That test was done with a full speed link.) |
| 81 | */ |
| 82 | |
| 83 | |
| 84 | /* |
| 85 | * NOTE on endpoint usage: |
| 86 | * |
| 87 | * CONTROL transfers all go through ep0. BULK ones go through dedicated IN |
| 88 | * and OUT endpoints ... hardware is dedicated for those "async" queue(s). |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 89 | * (Yes, bulk _could_ use more of the endpoints than that, and would even |
Ajay Kumar Gupta | 1e0320f | 2009-02-24 15:26:13 -0800 | [diff] [blame] | 90 | * benefit from it.) |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 91 | * |
| 92 | * INTERUPPT and ISOCHRONOUS transfers are scheduled to the other endpoints. |
| 93 | * So far that scheduling is both dumb and optimistic: the endpoint will be |
| 94 | * "claimed" until its software queue is no longer refilled. No multiplexing |
| 95 | * of transfers between endpoints, or anything clever. |
| 96 | */ |
| 97 | |
| 98 | |
| 99 | static void musb_ep_program(struct musb *musb, u8 epnum, |
Sergei Shtylyov | 6b6e971 | 2009-03-26 18:29:19 -0700 | [diff] [blame] | 100 | struct urb *urb, int is_out, |
| 101 | u8 *buf, u32 offset, u32 len); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 102 | |
| 103 | /* |
| 104 | * Clear TX fifo. Needed to avoid BABBLE errors. |
| 105 | */ |
David Brownell | c767c1c | 2008-09-11 11:53:23 +0300 | [diff] [blame] | 106 | static void musb_h_tx_flush_fifo(struct musb_hw_ep *ep) |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 107 | { |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 108 | struct musb *musb = ep->musb; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 109 | void __iomem *epio = ep->regs; |
| 110 | u16 csr; |
David Brownell | bb1c9ef | 2008-11-24 13:06:50 +0200 | [diff] [blame] | 111 | u16 lastcsr = 0; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 112 | int retries = 1000; |
| 113 | |
| 114 | csr = musb_readw(epio, MUSB_TXCSR); |
| 115 | while (csr & MUSB_TXCSR_FIFONOTEMPTY) { |
David Brownell | bb1c9ef | 2008-11-24 13:06:50 +0200 | [diff] [blame] | 116 | if (csr != lastcsr) |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 117 | dev_dbg(musb->controller, "Host TX FIFONOTEMPTY csr: %02x\n", csr); |
David Brownell | bb1c9ef | 2008-11-24 13:06:50 +0200 | [diff] [blame] | 118 | lastcsr = csr; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 119 | csr |= MUSB_TXCSR_FLUSHFIFO; |
| 120 | musb_writew(epio, MUSB_TXCSR, csr); |
| 121 | csr = musb_readw(epio, MUSB_TXCSR); |
David Brownell | bb1c9ef | 2008-11-24 13:06:50 +0200 | [diff] [blame] | 122 | if (WARN(retries-- < 1, |
| 123 | "Could not flush host TX%d fifo: csr: %04x\n", |
| 124 | ep->epnum, csr)) |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 125 | return; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 126 | mdelay(1); |
| 127 | } |
| 128 | } |
| 129 | |
David Brownell | 78322c1 | 2009-03-26 17:38:30 -0700 | [diff] [blame] | 130 | static void musb_h_ep0_flush_fifo(struct musb_hw_ep *ep) |
| 131 | { |
| 132 | void __iomem *epio = ep->regs; |
| 133 | u16 csr; |
| 134 | int retries = 5; |
| 135 | |
| 136 | /* scrub any data left in the fifo */ |
| 137 | do { |
| 138 | csr = musb_readw(epio, MUSB_TXCSR); |
| 139 | if (!(csr & (MUSB_CSR0_TXPKTRDY | MUSB_CSR0_RXPKTRDY))) |
| 140 | break; |
| 141 | musb_writew(epio, MUSB_TXCSR, MUSB_CSR0_FLUSHFIFO); |
| 142 | csr = musb_readw(epio, MUSB_TXCSR); |
| 143 | udelay(10); |
| 144 | } while (--retries); |
| 145 | |
| 146 | WARN(!retries, "Could not flush host TX%d fifo: csr: %04x\n", |
| 147 | ep->epnum, csr); |
| 148 | |
| 149 | /* and reset for the next transfer */ |
| 150 | musb_writew(epio, MUSB_TXCSR, 0); |
| 151 | } |
| 152 | |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 153 | /* |
| 154 | * Start transmit. Caller is responsible for locking shared resources. |
| 155 | * musb must be locked. |
| 156 | */ |
| 157 | static inline void musb_h_tx_start(struct musb_hw_ep *ep) |
| 158 | { |
| 159 | u16 txcsr; |
| 160 | |
| 161 | /* NOTE: no locks here; caller should lock and select EP */ |
| 162 | if (ep->epnum) { |
| 163 | txcsr = musb_readw(ep->regs, MUSB_TXCSR); |
| 164 | txcsr |= MUSB_TXCSR_TXPKTRDY | MUSB_TXCSR_H_WZC_BITS; |
| 165 | musb_writew(ep->regs, MUSB_TXCSR, txcsr); |
| 166 | } else { |
| 167 | txcsr = MUSB_CSR0_H_SETUPPKT | MUSB_CSR0_TXPKTRDY; |
| 168 | musb_writew(ep->regs, MUSB_CSR0, txcsr); |
| 169 | } |
| 170 | |
| 171 | } |
| 172 | |
Sergei Shtylyov | c7bbc05 | 2009-03-26 18:26:40 -0700 | [diff] [blame] | 173 | static inline void musb_h_tx_dma_start(struct musb_hw_ep *ep) |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 174 | { |
| 175 | u16 txcsr; |
| 176 | |
| 177 | /* NOTE: no locks here; caller should lock and select EP */ |
| 178 | txcsr = musb_readw(ep->regs, MUSB_TXCSR); |
| 179 | txcsr |= MUSB_TXCSR_DMAENAB | MUSB_TXCSR_H_WZC_BITS; |
Sergei Shtylyov | c7bbc05 | 2009-03-26 18:26:40 -0700 | [diff] [blame] | 180 | if (is_cppi_enabled()) |
| 181 | txcsr |= MUSB_TXCSR_DMAMODE; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 182 | musb_writew(ep->regs, MUSB_TXCSR, txcsr); |
| 183 | } |
| 184 | |
Sergei Shtylyov | 3e5c6dc | 2009-03-27 12:55:16 -0700 | [diff] [blame] | 185 | static void musb_ep_set_qh(struct musb_hw_ep *ep, int is_in, struct musb_qh *qh) |
| 186 | { |
| 187 | if (is_in != 0 || ep->is_shared_fifo) |
| 188 | ep->in_qh = qh; |
| 189 | if (is_in == 0 || ep->is_shared_fifo) |
| 190 | ep->out_qh = qh; |
| 191 | } |
| 192 | |
| 193 | static struct musb_qh *musb_ep_get_qh(struct musb_hw_ep *ep, int is_in) |
| 194 | { |
| 195 | return is_in ? ep->in_qh : ep->out_qh; |
| 196 | } |
| 197 | |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 198 | /* |
| 199 | * Start the URB at the front of an endpoint's queue |
| 200 | * end must be claimed from the caller. |
| 201 | * |
| 202 | * Context: controller locked, irqs blocked |
| 203 | */ |
| 204 | static void |
| 205 | musb_start_urb(struct musb *musb, int is_in, struct musb_qh *qh) |
| 206 | { |
| 207 | u16 frame; |
| 208 | u32 len; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 209 | void __iomem *mbase = musb->mregs; |
| 210 | struct urb *urb = next_urb(qh); |
Sergei Shtylyov | 6b6e971 | 2009-03-26 18:29:19 -0700 | [diff] [blame] | 211 | void *buf = urb->transfer_buffer; |
| 212 | u32 offset = 0; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 213 | struct musb_hw_ep *hw_ep = qh->hw_ep; |
| 214 | unsigned pipe = urb->pipe; |
| 215 | u8 address = usb_pipedevice(pipe); |
| 216 | int epnum = hw_ep->epnum; |
| 217 | |
| 218 | /* initialize software qh state */ |
| 219 | qh->offset = 0; |
| 220 | qh->segsize = 0; |
| 221 | |
| 222 | /* gather right source of data */ |
| 223 | switch (qh->type) { |
| 224 | case USB_ENDPOINT_XFER_CONTROL: |
| 225 | /* control transfers always start with SETUP */ |
| 226 | is_in = 0; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 227 | musb->ep0_stage = MUSB_EP0_START; |
| 228 | buf = urb->setup_packet; |
| 229 | len = 8; |
| 230 | break; |
| 231 | case USB_ENDPOINT_XFER_ISOC: |
| 232 | qh->iso_idx = 0; |
| 233 | qh->frame = 0; |
Sergei Shtylyov | 6b6e971 | 2009-03-26 18:29:19 -0700 | [diff] [blame] | 234 | offset = urb->iso_frame_desc[0].offset; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 235 | len = urb->iso_frame_desc[0].length; |
| 236 | break; |
| 237 | default: /* bulk, interrupt */ |
Ajay Kumar Gupta | 1e0320f | 2009-02-24 15:26:13 -0800 | [diff] [blame] | 238 | /* actual_length may be nonzero on retry paths */ |
| 239 | buf = urb->transfer_buffer + urb->actual_length; |
| 240 | len = urb->transfer_buffer_length - urb->actual_length; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 241 | } |
| 242 | |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 243 | dev_dbg(musb->controller, "qh %p urb %p dev%d ep%d%s%s, hw_ep %d, %p/%d\n", |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 244 | qh, urb, address, qh->epnum, |
| 245 | is_in ? "in" : "out", |
| 246 | ({char *s; switch (qh->type) { |
| 247 | case USB_ENDPOINT_XFER_CONTROL: s = ""; break; |
| 248 | case USB_ENDPOINT_XFER_BULK: s = "-bulk"; break; |
| 249 | case USB_ENDPOINT_XFER_ISOC: s = "-iso"; break; |
| 250 | default: s = "-intr"; break; |
| 251 | }; s; }), |
Sergei Shtylyov | 6b6e971 | 2009-03-26 18:29:19 -0700 | [diff] [blame] | 252 | epnum, buf + offset, len); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 253 | |
| 254 | /* Configure endpoint */ |
Sergei Shtylyov | 3e5c6dc | 2009-03-27 12:55:16 -0700 | [diff] [blame] | 255 | musb_ep_set_qh(hw_ep, is_in, qh); |
Sergei Shtylyov | 6b6e971 | 2009-03-26 18:29:19 -0700 | [diff] [blame] | 256 | musb_ep_program(musb, epnum, urb, !is_in, buf, offset, len); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 257 | |
| 258 | /* transmit may have more work: start it when it is time */ |
| 259 | if (is_in) |
| 260 | return; |
| 261 | |
| 262 | /* determine if the time is right for a periodic transfer */ |
| 263 | switch (qh->type) { |
| 264 | case USB_ENDPOINT_XFER_ISOC: |
| 265 | case USB_ENDPOINT_XFER_INT: |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 266 | dev_dbg(musb->controller, "check whether there's still time for periodic Tx\n"); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 267 | frame = musb_readw(mbase, MUSB_FRAME); |
| 268 | /* FIXME this doesn't implement that scheduling policy ... |
| 269 | * or handle framecounter wrapping |
| 270 | */ |
| 271 | if ((urb->transfer_flags & URB_ISO_ASAP) |
| 272 | || (frame >= urb->start_frame)) { |
| 273 | /* REVISIT the SOF irq handler shouldn't duplicate |
| 274 | * this code; and we don't init urb->start_frame... |
| 275 | */ |
| 276 | qh->frame = 0; |
| 277 | goto start; |
| 278 | } else { |
| 279 | qh->frame = urb->start_frame; |
| 280 | /* enable SOF interrupt so we can count down */ |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 281 | dev_dbg(musb->controller, "SOF for %d\n", epnum); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 282 | #if 1 /* ifndef CONFIG_ARCH_DAVINCI */ |
| 283 | musb_writeb(mbase, MUSB_INTRUSBE, 0xff); |
| 284 | #endif |
| 285 | } |
| 286 | break; |
| 287 | default: |
| 288 | start: |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 289 | dev_dbg(musb->controller, "Start TX%d %s\n", epnum, |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 290 | hw_ep->tx_channel ? "dma" : "pio"); |
| 291 | |
| 292 | if (!hw_ep->tx_channel) |
| 293 | musb_h_tx_start(hw_ep); |
| 294 | else if (is_cppi_enabled() || tusb_dma_omap()) |
Sergei Shtylyov | c7bbc05 | 2009-03-26 18:26:40 -0700 | [diff] [blame] | 295 | musb_h_tx_dma_start(hw_ep); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | |
Sergei Shtylyov | c9cd06b | 2009-03-27 12:58:31 -0700 | [diff] [blame] | 299 | /* Context: caller owns controller lock, IRQs are blocked */ |
| 300 | static void musb_giveback(struct musb *musb, struct urb *urb, int status) |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 301 | __releases(musb->lock) |
| 302 | __acquires(musb->lock) |
| 303 | { |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 304 | dev_dbg(musb->controller, |
David Brownell | bb1c9ef | 2008-11-24 13:06:50 +0200 | [diff] [blame] | 305 | "complete %p %pF (%d), dev%d ep%d%s, %d/%d\n", |
| 306 | urb, urb->complete, status, |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 307 | usb_pipedevice(urb->pipe), |
| 308 | usb_pipeendpoint(urb->pipe), |
| 309 | usb_pipein(urb->pipe) ? "in" : "out", |
| 310 | urb->actual_length, urb->transfer_buffer_length |
| 311 | ); |
| 312 | |
Ajay Kumar Gupta | 2492e67 | 2008-09-11 11:53:21 +0300 | [diff] [blame] | 313 | usb_hcd_unlink_urb_from_ep(musb_to_hcd(musb), urb); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 314 | spin_unlock(&musb->lock); |
| 315 | usb_hcd_giveback_urb(musb_to_hcd(musb), urb, status); |
| 316 | spin_lock(&musb->lock); |
| 317 | } |
| 318 | |
Sergei Shtylyov | 846099a | 2009-03-27 12:54:21 -0700 | [diff] [blame] | 319 | /* For bulk/interrupt endpoints only */ |
| 320 | static inline void musb_save_toggle(struct musb_qh *qh, int is_in, |
| 321 | struct urb *urb) |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 322 | { |
Sergei Shtylyov | 846099a | 2009-03-27 12:54:21 -0700 | [diff] [blame] | 323 | void __iomem *epio = qh->hw_ep->regs; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 324 | u16 csr; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 325 | |
Sergei Shtylyov | 846099a | 2009-03-27 12:54:21 -0700 | [diff] [blame] | 326 | /* |
| 327 | * FIXME: the current Mentor DMA code seems to have |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 328 | * problems getting toggle correct. |
| 329 | */ |
| 330 | |
Sergei Shtylyov | 846099a | 2009-03-27 12:54:21 -0700 | [diff] [blame] | 331 | if (is_in) |
| 332 | csr = musb_readw(epio, MUSB_RXCSR) & MUSB_RXCSR_H_DATATOGGLE; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 333 | else |
Sergei Shtylyov | 846099a | 2009-03-27 12:54:21 -0700 | [diff] [blame] | 334 | csr = musb_readw(epio, MUSB_TXCSR) & MUSB_TXCSR_H_DATATOGGLE; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 335 | |
Sergei Shtylyov | 846099a | 2009-03-27 12:54:21 -0700 | [diff] [blame] | 336 | usb_settoggle(urb->dev, qh->epnum, !is_in, csr ? 1 : 0); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 337 | } |
| 338 | |
Sergei Shtylyov | c9cd06b | 2009-03-27 12:58:31 -0700 | [diff] [blame] | 339 | /* |
| 340 | * Advance this hardware endpoint's queue, completing the specified URB and |
| 341 | * advancing to either the next URB queued to that qh, or else invalidating |
| 342 | * that qh and advancing to the next qh scheduled after the current one. |
| 343 | * |
| 344 | * Context: caller owns controller lock, IRQs are blocked |
| 345 | */ |
| 346 | static void musb_advance_schedule(struct musb *musb, struct urb *urb, |
| 347 | struct musb_hw_ep *hw_ep, int is_in) |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 348 | { |
Sergei Shtylyov | c9cd06b | 2009-03-27 12:58:31 -0700 | [diff] [blame] | 349 | struct musb_qh *qh = musb_ep_get_qh(hw_ep, is_in); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 350 | struct musb_hw_ep *ep = qh->hw_ep; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 351 | int ready = qh->is_ready; |
Sergei Shtylyov | c9cd06b | 2009-03-27 12:58:31 -0700 | [diff] [blame] | 352 | int status; |
| 353 | |
| 354 | status = (urb->status == -EINPROGRESS) ? 0 : urb->status; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 355 | |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 356 | /* save toggle eagerly, for paranoia */ |
| 357 | switch (qh->type) { |
| 358 | case USB_ENDPOINT_XFER_BULK: |
| 359 | case USB_ENDPOINT_XFER_INT: |
Sergei Shtylyov | 846099a | 2009-03-27 12:54:21 -0700 | [diff] [blame] | 360 | musb_save_toggle(qh, is_in, urb); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 361 | break; |
| 362 | case USB_ENDPOINT_XFER_ISOC: |
Sergei Shtylyov | 1fe975f | 2009-07-10 20:02:44 +0300 | [diff] [blame] | 363 | if (status == 0 && urb->error_count) |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 364 | status = -EXDEV; |
| 365 | break; |
| 366 | } |
| 367 | |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 368 | qh->is_ready = 0; |
Sergei Shtylyov | c9cd06b | 2009-03-27 12:58:31 -0700 | [diff] [blame] | 369 | musb_giveback(musb, urb, status); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 370 | qh->is_ready = ready; |
| 371 | |
| 372 | /* reclaim resources (and bandwidth) ASAP; deschedule it, and |
| 373 | * invalidate qh as soon as list_empty(&hep->urb_list) |
| 374 | */ |
| 375 | if (list_empty(&qh->hep->urb_list)) { |
| 376 | struct list_head *head; |
Ajay Kumar Gupta | 8c778db | 2012-06-21 17:18:12 +0530 | [diff] [blame] | 377 | struct dma_controller *dma = musb->dma_controller; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 378 | |
Ajay Kumar Gupta | 8c778db | 2012-06-21 17:18:12 +0530 | [diff] [blame] | 379 | if (is_in) { |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 380 | ep->rx_reinit = 1; |
Ajay Kumar Gupta | 8c778db | 2012-06-21 17:18:12 +0530 | [diff] [blame] | 381 | if (ep->rx_channel) { |
| 382 | dma->channel_release(ep->rx_channel); |
| 383 | ep->rx_channel = NULL; |
| 384 | } |
| 385 | } else { |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 386 | ep->tx_reinit = 1; |
Ajay Kumar Gupta | 8c778db | 2012-06-21 17:18:12 +0530 | [diff] [blame] | 387 | if (ep->tx_channel) { |
| 388 | dma->channel_release(ep->tx_channel); |
| 389 | ep->tx_channel = NULL; |
| 390 | } |
| 391 | } |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 392 | |
Sergei Shtylyov | 3e5c6dc | 2009-03-27 12:55:16 -0700 | [diff] [blame] | 393 | /* Clobber old pointers to this qh */ |
| 394 | musb_ep_set_qh(ep, is_in, NULL); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 395 | qh->hep->hcpriv = NULL; |
| 396 | |
| 397 | switch (qh->type) { |
| 398 | |
Ajay Kumar Gupta | 23d15e0 | 2008-10-29 15:10:35 +0200 | [diff] [blame] | 399 | case USB_ENDPOINT_XFER_CONTROL: |
| 400 | case USB_ENDPOINT_XFER_BULK: |
| 401 | /* fifo policy for these lists, except that NAKing |
| 402 | * should rotate a qh to the end (for fairness). |
| 403 | */ |
| 404 | if (qh->mux == 1) { |
| 405 | head = qh->ring.prev; |
| 406 | list_del(&qh->ring); |
| 407 | kfree(qh); |
| 408 | qh = first_qh(head); |
| 409 | break; |
| 410 | } |
| 411 | |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 412 | case USB_ENDPOINT_XFER_ISOC: |
| 413 | case USB_ENDPOINT_XFER_INT: |
| 414 | /* this is where periodic bandwidth should be |
| 415 | * de-allocated if it's tracked and allocated; |
| 416 | * and where we'd update the schedule tree... |
| 417 | */ |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 418 | kfree(qh); |
| 419 | qh = NULL; |
| 420 | break; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 421 | } |
| 422 | } |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 423 | |
Sergei Shtylyov | a2fd814 | 2009-02-21 15:30:45 -0800 | [diff] [blame] | 424 | if (qh != NULL && qh->is_ready) { |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 425 | dev_dbg(musb->controller, "... next ep%d %cX urb %p\n", |
Sergei Shtylyov | c9cd06b | 2009-03-27 12:58:31 -0700 | [diff] [blame] | 426 | hw_ep->epnum, is_in ? 'R' : 'T', next_urb(qh)); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 427 | musb_start_urb(musb, is_in, qh); |
| 428 | } |
| 429 | } |
| 430 | |
David Brownell | c767c1c | 2008-09-11 11:53:23 +0300 | [diff] [blame] | 431 | static u16 musb_h_flush_rxfifo(struct musb_hw_ep *hw_ep, u16 csr) |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 432 | { |
| 433 | /* we don't want fifo to fill itself again; |
| 434 | * ignore dma (various models), |
| 435 | * leave toggle alone (may not have been saved yet) |
| 436 | */ |
| 437 | csr |= MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_RXPKTRDY; |
| 438 | csr &= ~(MUSB_RXCSR_H_REQPKT |
| 439 | | MUSB_RXCSR_H_AUTOREQ |
| 440 | | MUSB_RXCSR_AUTOCLEAR); |
| 441 | |
| 442 | /* write 2x to allow double buffering */ |
| 443 | musb_writew(hw_ep->regs, MUSB_RXCSR, csr); |
| 444 | musb_writew(hw_ep->regs, MUSB_RXCSR, csr); |
| 445 | |
| 446 | /* flush writebuffer */ |
| 447 | return musb_readw(hw_ep->regs, MUSB_RXCSR); |
| 448 | } |
| 449 | |
| 450 | /* |
| 451 | * PIO RX for a packet (or part of it). |
| 452 | */ |
| 453 | static bool |
| 454 | musb_host_packet_rx(struct musb *musb, struct urb *urb, u8 epnum, u8 iso_err) |
| 455 | { |
| 456 | u16 rx_count; |
| 457 | u8 *buf; |
| 458 | u16 csr; |
| 459 | bool done = false; |
| 460 | u32 length; |
| 461 | int do_flush = 0; |
| 462 | struct musb_hw_ep *hw_ep = musb->endpoints + epnum; |
| 463 | void __iomem *epio = hw_ep->regs; |
| 464 | struct musb_qh *qh = hw_ep->in_qh; |
| 465 | int pipe = urb->pipe; |
| 466 | void *buffer = urb->transfer_buffer; |
| 467 | |
| 468 | /* musb_ep_select(mbase, epnum); */ |
| 469 | rx_count = musb_readw(epio, MUSB_RXCOUNT); |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 470 | dev_dbg(musb->controller, "RX%d count %d, buffer %p len %d/%d\n", epnum, rx_count, |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 471 | urb->transfer_buffer, qh->offset, |
| 472 | urb->transfer_buffer_length); |
| 473 | |
| 474 | /* unload FIFO */ |
| 475 | if (usb_pipeisoc(pipe)) { |
| 476 | int status = 0; |
| 477 | struct usb_iso_packet_descriptor *d; |
| 478 | |
| 479 | if (iso_err) { |
| 480 | status = -EILSEQ; |
| 481 | urb->error_count++; |
| 482 | } |
| 483 | |
| 484 | d = urb->iso_frame_desc + qh->iso_idx; |
| 485 | buf = buffer + d->offset; |
| 486 | length = d->length; |
| 487 | if (rx_count > length) { |
| 488 | if (status == 0) { |
| 489 | status = -EOVERFLOW; |
| 490 | urb->error_count++; |
| 491 | } |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 492 | dev_dbg(musb->controller, "** OVERFLOW %d into %d\n", rx_count, length); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 493 | do_flush = 1; |
| 494 | } else |
| 495 | length = rx_count; |
| 496 | urb->actual_length += length; |
| 497 | d->actual_length = length; |
| 498 | |
| 499 | d->status = status; |
| 500 | |
| 501 | /* see if we are done */ |
| 502 | done = (++qh->iso_idx >= urb->number_of_packets); |
| 503 | } else { |
| 504 | /* non-isoch */ |
| 505 | buf = buffer + qh->offset; |
| 506 | length = urb->transfer_buffer_length - qh->offset; |
| 507 | if (rx_count > length) { |
| 508 | if (urb->status == -EINPROGRESS) |
| 509 | urb->status = -EOVERFLOW; |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 510 | dev_dbg(musb->controller, "** OVERFLOW %d into %d\n", rx_count, length); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 511 | do_flush = 1; |
| 512 | } else |
| 513 | length = rx_count; |
| 514 | urb->actual_length += length; |
| 515 | qh->offset += length; |
| 516 | |
| 517 | /* see if we are done */ |
| 518 | done = (urb->actual_length == urb->transfer_buffer_length) |
| 519 | || (rx_count < qh->maxpacket) |
| 520 | || (urb->status != -EINPROGRESS); |
| 521 | if (done |
| 522 | && (urb->status == -EINPROGRESS) |
| 523 | && (urb->transfer_flags & URB_SHORT_NOT_OK) |
| 524 | && (urb->actual_length |
| 525 | < urb->transfer_buffer_length)) |
| 526 | urb->status = -EREMOTEIO; |
| 527 | } |
| 528 | |
| 529 | musb_read_fifo(hw_ep, length, buf); |
| 530 | |
| 531 | csr = musb_readw(epio, MUSB_RXCSR); |
| 532 | csr |= MUSB_RXCSR_H_WZC_BITS; |
| 533 | if (unlikely(do_flush)) |
| 534 | musb_h_flush_rxfifo(hw_ep, csr); |
| 535 | else { |
| 536 | /* REVISIT this assumes AUTOCLEAR is never set */ |
| 537 | csr &= ~(MUSB_RXCSR_RXPKTRDY | MUSB_RXCSR_H_REQPKT); |
| 538 | if (!done) |
| 539 | csr |= MUSB_RXCSR_H_REQPKT; |
| 540 | musb_writew(epio, MUSB_RXCSR, csr); |
| 541 | } |
| 542 | |
| 543 | return done; |
| 544 | } |
| 545 | |
| 546 | /* we don't always need to reinit a given side of an endpoint... |
| 547 | * when we do, use tx/rx reinit routine and then construct a new CSR |
| 548 | * to address data toggle, NYET, and DMA or PIO. |
| 549 | * |
| 550 | * it's possible that driver bugs (especially for DMA) or aborting a |
| 551 | * transfer might have left the endpoint busier than it should be. |
| 552 | * the busy/not-empty tests are basically paranoia. |
| 553 | */ |
| 554 | static void |
| 555 | musb_rx_reinit(struct musb *musb, struct musb_qh *qh, struct musb_hw_ep *ep) |
| 556 | { |
| 557 | u16 csr; |
| 558 | |
| 559 | /* NOTE: we know the "rx" fifo reinit never triggers for ep0. |
| 560 | * That always uses tx_reinit since ep0 repurposes TX register |
| 561 | * offsets; the initial SETUP packet is also a kind of OUT. |
| 562 | */ |
| 563 | |
| 564 | /* if programmed for Tx, put it in RX mode */ |
| 565 | if (ep->is_shared_fifo) { |
| 566 | csr = musb_readw(ep->regs, MUSB_TXCSR); |
| 567 | if (csr & MUSB_TXCSR_MODE) { |
| 568 | musb_h_tx_flush_fifo(ep); |
Sergei Shtylyov | b6e434a | 2009-03-26 18:27:47 -0700 | [diff] [blame] | 569 | csr = musb_readw(ep->regs, MUSB_TXCSR); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 570 | musb_writew(ep->regs, MUSB_TXCSR, |
Sergei Shtylyov | b6e434a | 2009-03-26 18:27:47 -0700 | [diff] [blame] | 571 | csr | MUSB_TXCSR_FRCDATATOG); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 572 | } |
Sergei Shtylyov | b6e434a | 2009-03-26 18:27:47 -0700 | [diff] [blame] | 573 | |
| 574 | /* |
| 575 | * Clear the MODE bit (and everything else) to enable Rx. |
| 576 | * NOTE: we mustn't clear the DMAMODE bit before DMAENAB. |
| 577 | */ |
| 578 | if (csr & MUSB_TXCSR_DMAMODE) |
| 579 | musb_writew(ep->regs, MUSB_TXCSR, MUSB_TXCSR_DMAMODE); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 580 | musb_writew(ep->regs, MUSB_TXCSR, 0); |
| 581 | |
| 582 | /* scrub all previous state, clearing toggle */ |
| 583 | } else { |
| 584 | csr = musb_readw(ep->regs, MUSB_RXCSR); |
| 585 | if (csr & MUSB_RXCSR_RXPKTRDY) |
| 586 | WARNING("rx%d, packet/%d ready?\n", ep->epnum, |
| 587 | musb_readw(ep->regs, MUSB_RXCOUNT)); |
| 588 | |
| 589 | musb_h_flush_rxfifo(ep, MUSB_RXCSR_CLRDATATOG); |
| 590 | } |
| 591 | |
| 592 | /* target addr and (for multipoint) hub addr/port */ |
| 593 | if (musb->is_multipoint) { |
Bryan Wu | c6cf8b0 | 2008-12-02 21:33:48 +0200 | [diff] [blame] | 594 | musb_write_rxfunaddr(ep->target_regs, qh->addr_reg); |
| 595 | musb_write_rxhubaddr(ep->target_regs, qh->h_addr_reg); |
| 596 | musb_write_rxhubport(ep->target_regs, qh->h_port_reg); |
| 597 | |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 598 | } else |
| 599 | musb_writeb(musb->mregs, MUSB_FADDR, qh->addr_reg); |
| 600 | |
| 601 | /* protocol/endpoint, interval/NAKlimit, i/o size */ |
| 602 | musb_writeb(ep->regs, MUSB_RXTYPE, qh->type_reg); |
| 603 | musb_writeb(ep->regs, MUSB_RXINTERVAL, qh->intv_reg); |
| 604 | /* NOTE: bulk combining rewrites high bits of maxpacket */ |
Cliff Cai | 9f445cb | 2010-01-28 20:44:18 -0500 | [diff] [blame] | 605 | /* Set RXMAXP with the FIFO size of the endpoint |
| 606 | * to disable double buffer mode. |
| 607 | */ |
Felipe Balbi | 0662481 | 2011-01-21 13:39:20 +0800 | [diff] [blame] | 608 | if (musb->double_buffer_not_ok) |
Cliff Cai | 9f445cb | 2010-01-28 20:44:18 -0500 | [diff] [blame] | 609 | musb_writew(ep->regs, MUSB_RXMAXP, ep->max_packet_sz_rx); |
| 610 | else |
| 611 | musb_writew(ep->regs, MUSB_RXMAXP, |
| 612 | qh->maxpacket | ((qh->hb_mult - 1) << 11)); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 613 | |
| 614 | ep->rx_reinit = 0; |
| 615 | } |
| 616 | |
Sergei Shtylyov | 6b6e971 | 2009-03-26 18:29:19 -0700 | [diff] [blame] | 617 | static bool musb_tx_dma_program(struct dma_controller *dma, |
| 618 | struct musb_hw_ep *hw_ep, struct musb_qh *qh, |
| 619 | struct urb *urb, u32 offset, u32 length) |
| 620 | { |
| 621 | struct dma_channel *channel = hw_ep->tx_channel; |
| 622 | void __iomem *epio = hw_ep->regs; |
| 623 | u16 pkt_size = qh->maxpacket; |
| 624 | u16 csr; |
| 625 | u8 mode; |
| 626 | |
| 627 | #ifdef CONFIG_USB_INVENTRA_DMA |
| 628 | if (length > channel->max_len) |
| 629 | length = channel->max_len; |
| 630 | |
| 631 | csr = musb_readw(epio, MUSB_TXCSR); |
| 632 | if (length > pkt_size) { |
| 633 | mode = 1; |
Ajay Kumar Gupta | a483d70 | 2009-04-03 16:16:17 -0700 | [diff] [blame] | 634 | csr |= MUSB_TXCSR_DMAMODE | MUSB_TXCSR_DMAENAB; |
| 635 | /* autoset shouldn't be set in high bandwidth */ |
supriya karanth | f278628 | 2012-12-06 11:16:23 +0530 | [diff] [blame] | 636 | /* |
| 637 | * Enable Autoset according to table |
| 638 | * below |
| 639 | * bulk_split hb_mult Autoset_Enable |
| 640 | * 0 1 Yes(Normal) |
| 641 | * 0 >1 No(High BW ISO) |
| 642 | * 1 1 Yes(HS bulk) |
| 643 | * 1 >1 Yes(FS bulk) |
| 644 | */ |
| 645 | if (qh->hb_mult == 1 || (qh->hb_mult > 1 && |
| 646 | can_bulk_split(hw_ep->musb, qh->type))) |
Ajay Kumar Gupta | a483d70 | 2009-04-03 16:16:17 -0700 | [diff] [blame] | 647 | csr |= MUSB_TXCSR_AUTOSET; |
Sergei Shtylyov | 6b6e971 | 2009-03-26 18:29:19 -0700 | [diff] [blame] | 648 | } else { |
| 649 | mode = 0; |
| 650 | csr &= ~(MUSB_TXCSR_AUTOSET | MUSB_TXCSR_DMAMODE); |
| 651 | csr |= MUSB_TXCSR_DMAENAB; /* against programmer's guide */ |
| 652 | } |
| 653 | channel->desired_mode = mode; |
| 654 | musb_writew(epio, MUSB_TXCSR, csr); |
| 655 | #else |
| 656 | if (!is_cppi_enabled() && !tusb_dma_omap()) |
| 657 | return false; |
| 658 | |
| 659 | channel->actual_len = 0; |
| 660 | |
| 661 | /* |
| 662 | * TX uses "RNDIS" mode automatically but needs help |
| 663 | * to identify the zero-length-final-packet case. |
| 664 | */ |
| 665 | mode = (urb->transfer_flags & URB_ZERO_PACKET) ? 1 : 0; |
| 666 | #endif |
| 667 | |
| 668 | qh->segsize = length; |
| 669 | |
Santosh Shilimkar | 4c64733 | 2010-09-20 10:32:07 +0300 | [diff] [blame] | 670 | /* |
| 671 | * Ensure the data reaches to main memory before starting |
| 672 | * DMA transfer |
| 673 | */ |
| 674 | wmb(); |
| 675 | |
Sergei Shtylyov | 6b6e971 | 2009-03-26 18:29:19 -0700 | [diff] [blame] | 676 | if (!dma->channel_program(channel, pkt_size, mode, |
| 677 | urb->transfer_dma + offset, length)) { |
| 678 | dma->channel_release(channel); |
| 679 | hw_ep->tx_channel = NULL; |
| 680 | |
| 681 | csr = musb_readw(epio, MUSB_TXCSR); |
| 682 | csr &= ~(MUSB_TXCSR_AUTOSET | MUSB_TXCSR_DMAENAB); |
| 683 | musb_writew(epio, MUSB_TXCSR, csr | MUSB_TXCSR_H_WZC_BITS); |
| 684 | return false; |
| 685 | } |
| 686 | return true; |
| 687 | } |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 688 | |
| 689 | /* |
| 690 | * Program an HDRC endpoint as per the given URB |
| 691 | * Context: irqs blocked, controller lock held |
| 692 | */ |
| 693 | static void musb_ep_program(struct musb *musb, u8 epnum, |
Sergei Shtylyov | 6b6e971 | 2009-03-26 18:29:19 -0700 | [diff] [blame] | 694 | struct urb *urb, int is_out, |
| 695 | u8 *buf, u32 offset, u32 len) |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 696 | { |
| 697 | struct dma_controller *dma_controller; |
| 698 | struct dma_channel *dma_channel; |
| 699 | u8 dma_ok; |
| 700 | void __iomem *mbase = musb->mregs; |
| 701 | struct musb_hw_ep *hw_ep = musb->endpoints + epnum; |
| 702 | void __iomem *epio = hw_ep->regs; |
Sergei Shtylyov | 3e5c6dc | 2009-03-27 12:55:16 -0700 | [diff] [blame] | 703 | struct musb_qh *qh = musb_ep_get_qh(hw_ep, !is_out); |
| 704 | u16 packet_sz = qh->maxpacket; |
Ajay Kumar Gupta | 3132122 | 2012-07-20 11:07:22 +0530 | [diff] [blame] | 705 | u8 use_dma = 1; |
| 706 | u16 csr; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 707 | |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 708 | dev_dbg(musb->controller, "%s hw%d urb %p spd%d dev%d ep%d%s " |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 709 | "h_addr%02x h_port%02x bytes %d\n", |
| 710 | is_out ? "-->" : "<--", |
| 711 | epnum, urb, urb->dev->speed, |
| 712 | qh->addr_reg, qh->epnum, is_out ? "out" : "in", |
| 713 | qh->h_addr_reg, qh->h_port_reg, |
| 714 | len); |
| 715 | |
| 716 | musb_ep_select(mbase, epnum); |
| 717 | |
Ajay Kumar Gupta | 3132122 | 2012-07-20 11:07:22 +0530 | [diff] [blame] | 718 | if (is_out && !len) { |
| 719 | use_dma = 0; |
| 720 | csr = musb_readw(epio, MUSB_TXCSR); |
| 721 | csr &= ~MUSB_TXCSR_DMAENAB; |
| 722 | musb_writew(epio, MUSB_TXCSR, csr); |
| 723 | hw_ep->tx_channel = NULL; |
| 724 | } |
| 725 | |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 726 | /* candidate for DMA? */ |
| 727 | dma_controller = musb->dma_controller; |
Ajay Kumar Gupta | 3132122 | 2012-07-20 11:07:22 +0530 | [diff] [blame] | 728 | if (use_dma && is_dma_capable() && epnum && dma_controller) { |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 729 | dma_channel = is_out ? hw_ep->tx_channel : hw_ep->rx_channel; |
| 730 | if (!dma_channel) { |
| 731 | dma_channel = dma_controller->channel_alloc( |
| 732 | dma_controller, hw_ep, is_out); |
| 733 | if (is_out) |
| 734 | hw_ep->tx_channel = dma_channel; |
| 735 | else |
| 736 | hw_ep->rx_channel = dma_channel; |
| 737 | } |
| 738 | } else |
| 739 | dma_channel = NULL; |
| 740 | |
| 741 | /* make sure we clear DMAEnab, autoSet bits from previous run */ |
| 742 | |
| 743 | /* OUT/transmit/EP0 or IN/receive? */ |
| 744 | if (is_out) { |
| 745 | u16 csr; |
| 746 | u16 int_txe; |
| 747 | u16 load_count; |
| 748 | |
| 749 | csr = musb_readw(epio, MUSB_TXCSR); |
| 750 | |
| 751 | /* disable interrupt in case we flush */ |
Sebastian Andrzej Siewior | b18d26f | 2012-10-30 19:52:26 +0100 | [diff] [blame] | 752 | int_txe = musb->intrtxe; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 753 | musb_writew(mbase, MUSB_INTRTXE, int_txe & ~(1 << epnum)); |
| 754 | |
| 755 | /* general endpoint setup */ |
| 756 | if (epnum) { |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 757 | /* flush all old state, set default */ |
supriya karanth | a70b844 | 2013-01-04 17:10:33 +0530 | [diff] [blame] | 758 | /* |
| 759 | * We could be flushing valid |
| 760 | * packets in double buffering |
| 761 | * case |
| 762 | */ |
| 763 | if (!hw_ep->tx_double_buffered) |
| 764 | musb_h_tx_flush_fifo(hw_ep); |
Sergei Shtylyov | b6e434a | 2009-03-26 18:27:47 -0700 | [diff] [blame] | 765 | |
| 766 | /* |
| 767 | * We must not clear the DMAMODE bit before or in |
| 768 | * the same cycle with the DMAENAB bit, so we clear |
| 769 | * the latter first... |
| 770 | */ |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 771 | csr &= ~(MUSB_TXCSR_H_NAKTIMEOUT |
Sergei Shtylyov | b6e434a | 2009-03-26 18:27:47 -0700 | [diff] [blame] | 772 | | MUSB_TXCSR_AUTOSET |
| 773 | | MUSB_TXCSR_DMAENAB |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 774 | | MUSB_TXCSR_FRCDATATOG |
| 775 | | MUSB_TXCSR_H_RXSTALL |
| 776 | | MUSB_TXCSR_H_ERROR |
| 777 | | MUSB_TXCSR_TXPKTRDY |
| 778 | ); |
| 779 | csr |= MUSB_TXCSR_MODE; |
| 780 | |
supriya karanth | a70b844 | 2013-01-04 17:10:33 +0530 | [diff] [blame] | 781 | if (!hw_ep->tx_double_buffered) { |
| 782 | if (usb_gettoggle(urb->dev, qh->epnum, 1)) |
| 783 | csr |= MUSB_TXCSR_H_WR_DATATOGGLE |
| 784 | | MUSB_TXCSR_H_DATATOGGLE; |
| 785 | else |
| 786 | csr |= MUSB_TXCSR_CLRDATATOG; |
| 787 | } |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 788 | |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 789 | musb_writew(epio, MUSB_TXCSR, csr); |
| 790 | /* REVISIT may need to clear FLUSHFIFO ... */ |
Sergei Shtylyov | b6e434a | 2009-03-26 18:27:47 -0700 | [diff] [blame] | 791 | csr &= ~MUSB_TXCSR_DMAMODE; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 792 | musb_writew(epio, MUSB_TXCSR, csr); |
| 793 | csr = musb_readw(epio, MUSB_TXCSR); |
| 794 | } else { |
| 795 | /* endpoint 0: just flush */ |
David Brownell | 78322c1 | 2009-03-26 17:38:30 -0700 | [diff] [blame] | 796 | musb_h_ep0_flush_fifo(hw_ep); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | /* target addr and (for multipoint) hub addr/port */ |
| 800 | if (musb->is_multipoint) { |
Bryan Wu | c6cf8b0 | 2008-12-02 21:33:48 +0200 | [diff] [blame] | 801 | musb_write_txfunaddr(mbase, epnum, qh->addr_reg); |
| 802 | musb_write_txhubaddr(mbase, epnum, qh->h_addr_reg); |
| 803 | musb_write_txhubport(mbase, epnum, qh->h_port_reg); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 804 | /* FIXME if !epnum, do the same for RX ... */ |
| 805 | } else |
| 806 | musb_writeb(mbase, MUSB_FADDR, qh->addr_reg); |
| 807 | |
| 808 | /* protocol/endpoint/interval/NAKlimit */ |
| 809 | if (epnum) { |
| 810 | musb_writeb(epio, MUSB_TXTYPE, qh->type_reg); |
supriya karanth | f278628 | 2012-12-06 11:16:23 +0530 | [diff] [blame] | 811 | if (musb->double_buffer_not_ok) { |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 812 | musb_writew(epio, MUSB_TXMAXP, |
Felipe Balbi | 0662481 | 2011-01-21 13:39:20 +0800 | [diff] [blame] | 813 | hw_ep->max_packet_sz_tx); |
supriya karanth | f278628 | 2012-12-06 11:16:23 +0530 | [diff] [blame] | 814 | } else if (can_bulk_split(musb, qh->type)) { |
| 815 | qh->hb_mult = hw_ep->max_packet_sz_tx |
| 816 | / packet_sz; |
Ajay Kumar Gupta | ccc080c | 2011-12-13 10:32:42 +0530 | [diff] [blame] | 817 | musb_writew(epio, MUSB_TXMAXP, packet_sz |
supriya karanth | f278628 | 2012-12-06 11:16:23 +0530 | [diff] [blame] | 818 | | ((qh->hb_mult) - 1) << 11); |
| 819 | } else { |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 820 | musb_writew(epio, MUSB_TXMAXP, |
Felipe Balbi | 0662481 | 2011-01-21 13:39:20 +0800 | [diff] [blame] | 821 | qh->maxpacket | |
| 822 | ((qh->hb_mult - 1) << 11)); |
supriya karanth | f278628 | 2012-12-06 11:16:23 +0530 | [diff] [blame] | 823 | } |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 824 | musb_writeb(epio, MUSB_TXINTERVAL, qh->intv_reg); |
| 825 | } else { |
| 826 | musb_writeb(epio, MUSB_NAKLIMIT0, qh->intv_reg); |
| 827 | if (musb->is_multipoint) |
| 828 | musb_writeb(epio, MUSB_TYPE0, |
| 829 | qh->type_reg); |
| 830 | } |
| 831 | |
| 832 | if (can_bulk_split(musb, qh->type)) |
| 833 | load_count = min((u32) hw_ep->max_packet_sz_tx, |
| 834 | len); |
| 835 | else |
| 836 | load_count = min((u32) packet_sz, len); |
| 837 | |
Sergei Shtylyov | 6b6e971 | 2009-03-26 18:29:19 -0700 | [diff] [blame] | 838 | if (dma_channel && musb_tx_dma_program(dma_controller, |
| 839 | hw_ep, qh, urb, offset, len)) |
| 840 | load_count = 0; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 841 | |
| 842 | if (load_count) { |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 843 | /* PIO to load FIFO */ |
| 844 | qh->segsize = load_count; |
Virupax Sadashivpetimath | 8e8a551 | 2012-08-07 14:46:20 +0530 | [diff] [blame] | 845 | if (!buf) { |
| 846 | sg_miter_start(&qh->sg_miter, urb->sg, 1, |
| 847 | SG_MITER_ATOMIC |
| 848 | | SG_MITER_FROM_SG); |
| 849 | if (!sg_miter_next(&qh->sg_miter)) { |
| 850 | dev_err(musb->controller, |
| 851 | "error: sg" |
| 852 | "list empty\n"); |
| 853 | sg_miter_stop(&qh->sg_miter); |
| 854 | goto finish; |
| 855 | } |
| 856 | buf = qh->sg_miter.addr + urb->sg->offset + |
| 857 | urb->actual_length; |
| 858 | load_count = min_t(u32, load_count, |
| 859 | qh->sg_miter.length); |
| 860 | musb_write_fifo(hw_ep, load_count, buf); |
| 861 | qh->sg_miter.consumed = load_count; |
| 862 | sg_miter_stop(&qh->sg_miter); |
| 863 | } else |
| 864 | musb_write_fifo(hw_ep, load_count, buf); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 865 | } |
Virupax Sadashivpetimath | 8e8a551 | 2012-08-07 14:46:20 +0530 | [diff] [blame] | 866 | finish: |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 867 | /* re-enable interrupt */ |
| 868 | musb_writew(mbase, MUSB_INTRTXE, int_txe); |
| 869 | |
| 870 | /* IN/receive */ |
| 871 | } else { |
| 872 | u16 csr; |
| 873 | |
| 874 | if (hw_ep->rx_reinit) { |
| 875 | musb_rx_reinit(musb, qh, hw_ep); |
| 876 | |
| 877 | /* init new state: toggle and NYET, maybe DMA later */ |
| 878 | if (usb_gettoggle(urb->dev, qh->epnum, 0)) |
| 879 | csr = MUSB_RXCSR_H_WR_DATATOGGLE |
| 880 | | MUSB_RXCSR_H_DATATOGGLE; |
| 881 | else |
| 882 | csr = 0; |
| 883 | if (qh->type == USB_ENDPOINT_XFER_INT) |
| 884 | csr |= MUSB_RXCSR_DISNYET; |
| 885 | |
| 886 | } else { |
| 887 | csr = musb_readw(hw_ep->regs, MUSB_RXCSR); |
| 888 | |
| 889 | if (csr & (MUSB_RXCSR_RXPKTRDY |
| 890 | | MUSB_RXCSR_DMAENAB |
| 891 | | MUSB_RXCSR_H_REQPKT)) |
| 892 | ERR("broken !rx_reinit, ep%d csr %04x\n", |
| 893 | hw_ep->epnum, csr); |
| 894 | |
| 895 | /* scrub any stale state, leaving toggle alone */ |
| 896 | csr &= MUSB_RXCSR_DISNYET; |
| 897 | } |
| 898 | |
| 899 | /* kick things off */ |
| 900 | |
| 901 | if ((is_cppi_enabled() || tusb_dma_omap()) && dma_channel) { |
Sergei Shtylyov | c51e36d | 2011-05-07 19:44:13 +0400 | [diff] [blame] | 902 | /* Candidate for DMA */ |
| 903 | dma_channel->actual_len = 0L; |
| 904 | qh->segsize = len; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 905 | |
Sergei Shtylyov | c51e36d | 2011-05-07 19:44:13 +0400 | [diff] [blame] | 906 | /* AUTOREQ is in a DMA register */ |
| 907 | musb_writew(hw_ep->regs, MUSB_RXCSR, csr); |
| 908 | csr = musb_readw(hw_ep->regs, MUSB_RXCSR); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 909 | |
Sergei Shtylyov | c51e36d | 2011-05-07 19:44:13 +0400 | [diff] [blame] | 910 | /* |
| 911 | * Unless caller treats short RX transfers as |
| 912 | * errors, we dare not queue multiple transfers. |
| 913 | */ |
| 914 | dma_ok = dma_controller->channel_program(dma_channel, |
| 915 | packet_sz, !(urb->transfer_flags & |
| 916 | URB_SHORT_NOT_OK), |
| 917 | urb->transfer_dma + offset, |
| 918 | qh->segsize); |
| 919 | if (!dma_ok) { |
| 920 | dma_controller->channel_release(dma_channel); |
| 921 | hw_ep->rx_channel = dma_channel = NULL; |
| 922 | } else |
| 923 | csr |= MUSB_RXCSR_DMAENAB; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | csr |= MUSB_RXCSR_H_REQPKT; |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 927 | dev_dbg(musb->controller, "RXCSR%d := %04x\n", epnum, csr); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 928 | musb_writew(hw_ep->regs, MUSB_RXCSR, csr); |
| 929 | csr = musb_readw(hw_ep->regs, MUSB_RXCSR); |
| 930 | } |
| 931 | } |
| 932 | |
Ajay Kumar Gupta | f283862 | 2012-07-19 13:41:59 +0530 | [diff] [blame] | 933 | /* Schedule next QH from musb->in_bulk/out_bulk and move the current qh to |
| 934 | * the end; avoids starvation for other endpoints. |
| 935 | */ |
| 936 | static void musb_bulk_nak_timeout(struct musb *musb, struct musb_hw_ep *ep, |
| 937 | int is_in) |
| 938 | { |
| 939 | struct dma_channel *dma; |
| 940 | struct urb *urb; |
| 941 | void __iomem *mbase = musb->mregs; |
| 942 | void __iomem *epio = ep->regs; |
| 943 | struct musb_qh *cur_qh, *next_qh; |
| 944 | u16 rx_csr, tx_csr; |
| 945 | |
| 946 | musb_ep_select(mbase, ep->epnum); |
| 947 | if (is_in) { |
| 948 | dma = is_dma_capable() ? ep->rx_channel : NULL; |
| 949 | |
| 950 | /* clear nak timeout bit */ |
| 951 | rx_csr = musb_readw(epio, MUSB_RXCSR); |
| 952 | rx_csr |= MUSB_RXCSR_H_WZC_BITS; |
| 953 | rx_csr &= ~MUSB_RXCSR_DATAERROR; |
| 954 | musb_writew(epio, MUSB_RXCSR, rx_csr); |
| 955 | |
| 956 | cur_qh = first_qh(&musb->in_bulk); |
| 957 | } else { |
| 958 | dma = is_dma_capable() ? ep->tx_channel : NULL; |
| 959 | |
| 960 | /* clear nak timeout bit */ |
| 961 | tx_csr = musb_readw(epio, MUSB_TXCSR); |
| 962 | tx_csr |= MUSB_TXCSR_H_WZC_BITS; |
| 963 | tx_csr &= ~MUSB_TXCSR_H_NAKTIMEOUT; |
| 964 | musb_writew(epio, MUSB_TXCSR, tx_csr); |
| 965 | |
| 966 | cur_qh = first_qh(&musb->out_bulk); |
| 967 | } |
| 968 | if (cur_qh) { |
| 969 | urb = next_urb(cur_qh); |
| 970 | if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) { |
| 971 | dma->status = MUSB_DMA_STATUS_CORE_ABORT; |
| 972 | musb->dma_controller->channel_abort(dma); |
| 973 | urb->actual_length += dma->actual_len; |
| 974 | dma->actual_len = 0L; |
| 975 | } |
| 976 | musb_save_toggle(cur_qh, is_in, urb); |
| 977 | |
| 978 | if (is_in) { |
| 979 | /* move cur_qh to end of queue */ |
| 980 | list_move_tail(&cur_qh->ring, &musb->in_bulk); |
| 981 | |
| 982 | /* get the next qh from musb->in_bulk */ |
| 983 | next_qh = first_qh(&musb->in_bulk); |
| 984 | |
| 985 | /* set rx_reinit and schedule the next qh */ |
| 986 | ep->rx_reinit = 1; |
| 987 | } else { |
| 988 | /* move cur_qh to end of queue */ |
| 989 | list_move_tail(&cur_qh->ring, &musb->out_bulk); |
| 990 | |
| 991 | /* get the next qh from musb->out_bulk */ |
| 992 | next_qh = first_qh(&musb->out_bulk); |
| 993 | |
| 994 | /* set tx_reinit and schedule the next qh */ |
| 995 | ep->tx_reinit = 1; |
| 996 | } |
| 997 | musb_start_urb(musb, is_in, next_qh); |
| 998 | } |
| 999 | } |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1000 | |
| 1001 | /* |
| 1002 | * Service the default endpoint (ep0) as host. |
| 1003 | * Return true until it's time to start the status stage. |
| 1004 | */ |
| 1005 | static bool musb_h_ep0_continue(struct musb *musb, u16 len, struct urb *urb) |
| 1006 | { |
| 1007 | bool more = false; |
| 1008 | u8 *fifo_dest = NULL; |
| 1009 | u16 fifo_count = 0; |
| 1010 | struct musb_hw_ep *hw_ep = musb->control_ep; |
| 1011 | struct musb_qh *qh = hw_ep->in_qh; |
| 1012 | struct usb_ctrlrequest *request; |
| 1013 | |
| 1014 | switch (musb->ep0_stage) { |
| 1015 | case MUSB_EP0_IN: |
| 1016 | fifo_dest = urb->transfer_buffer + urb->actual_length; |
Sergei Shtylyov | 3ecdb9a | 2009-02-21 15:31:23 -0800 | [diff] [blame] | 1017 | fifo_count = min_t(size_t, len, urb->transfer_buffer_length - |
| 1018 | urb->actual_length); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1019 | if (fifo_count < len) |
| 1020 | urb->status = -EOVERFLOW; |
| 1021 | |
| 1022 | musb_read_fifo(hw_ep, fifo_count, fifo_dest); |
| 1023 | |
| 1024 | urb->actual_length += fifo_count; |
| 1025 | if (len < qh->maxpacket) { |
| 1026 | /* always terminate on short read; it's |
| 1027 | * rarely reported as an error. |
| 1028 | */ |
| 1029 | } else if (urb->actual_length < |
| 1030 | urb->transfer_buffer_length) |
| 1031 | more = true; |
| 1032 | break; |
| 1033 | case MUSB_EP0_START: |
| 1034 | request = (struct usb_ctrlrequest *) urb->setup_packet; |
| 1035 | |
| 1036 | if (!request->wLength) { |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 1037 | dev_dbg(musb->controller, "start no-DATA\n"); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1038 | break; |
| 1039 | } else if (request->bRequestType & USB_DIR_IN) { |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 1040 | dev_dbg(musb->controller, "start IN-DATA\n"); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1041 | musb->ep0_stage = MUSB_EP0_IN; |
| 1042 | more = true; |
| 1043 | break; |
| 1044 | } else { |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 1045 | dev_dbg(musb->controller, "start OUT-DATA\n"); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1046 | musb->ep0_stage = MUSB_EP0_OUT; |
| 1047 | more = true; |
| 1048 | } |
| 1049 | /* FALLTHROUGH */ |
| 1050 | case MUSB_EP0_OUT: |
Sergei Shtylyov | 3ecdb9a | 2009-02-21 15:31:23 -0800 | [diff] [blame] | 1051 | fifo_count = min_t(size_t, qh->maxpacket, |
| 1052 | urb->transfer_buffer_length - |
| 1053 | urb->actual_length); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1054 | if (fifo_count) { |
| 1055 | fifo_dest = (u8 *) (urb->transfer_buffer |
| 1056 | + urb->actual_length); |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 1057 | dev_dbg(musb->controller, "Sending %d byte%s to ep0 fifo %p\n", |
David Brownell | bb1c9ef | 2008-11-24 13:06:50 +0200 | [diff] [blame] | 1058 | fifo_count, |
| 1059 | (fifo_count == 1) ? "" : "s", |
| 1060 | fifo_dest); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1061 | musb_write_fifo(hw_ep, fifo_count, fifo_dest); |
| 1062 | |
| 1063 | urb->actual_length += fifo_count; |
| 1064 | more = true; |
| 1065 | } |
| 1066 | break; |
| 1067 | default: |
| 1068 | ERR("bogus ep0 stage %d\n", musb->ep0_stage); |
| 1069 | break; |
| 1070 | } |
| 1071 | |
| 1072 | return more; |
| 1073 | } |
| 1074 | |
| 1075 | /* |
| 1076 | * Handle default endpoint interrupt as host. Only called in IRQ time |
David Brownell | c767c1c | 2008-09-11 11:53:23 +0300 | [diff] [blame] | 1077 | * from musb_interrupt(). |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1078 | * |
| 1079 | * called with controller irqlocked |
| 1080 | */ |
| 1081 | irqreturn_t musb_h_ep0_irq(struct musb *musb) |
| 1082 | { |
| 1083 | struct urb *urb; |
| 1084 | u16 csr, len; |
| 1085 | int status = 0; |
| 1086 | void __iomem *mbase = musb->mregs; |
| 1087 | struct musb_hw_ep *hw_ep = musb->control_ep; |
| 1088 | void __iomem *epio = hw_ep->regs; |
| 1089 | struct musb_qh *qh = hw_ep->in_qh; |
| 1090 | bool complete = false; |
| 1091 | irqreturn_t retval = IRQ_NONE; |
| 1092 | |
| 1093 | /* ep0 only has one queue, "in" */ |
| 1094 | urb = next_urb(qh); |
| 1095 | |
| 1096 | musb_ep_select(mbase, 0); |
| 1097 | csr = musb_readw(epio, MUSB_CSR0); |
| 1098 | len = (csr & MUSB_CSR0_RXPKTRDY) |
| 1099 | ? musb_readb(epio, MUSB_COUNT0) |
| 1100 | : 0; |
| 1101 | |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 1102 | dev_dbg(musb->controller, "<== csr0 %04x, qh %p, count %d, urb %p, stage %d\n", |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1103 | csr, qh, len, urb, musb->ep0_stage); |
| 1104 | |
| 1105 | /* if we just did status stage, we are done */ |
| 1106 | if (MUSB_EP0_STATUS == musb->ep0_stage) { |
| 1107 | retval = IRQ_HANDLED; |
| 1108 | complete = true; |
| 1109 | } |
| 1110 | |
| 1111 | /* prepare status */ |
| 1112 | if (csr & MUSB_CSR0_H_RXSTALL) { |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 1113 | dev_dbg(musb->controller, "STALLING ENDPOINT\n"); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1114 | status = -EPIPE; |
| 1115 | |
| 1116 | } else if (csr & MUSB_CSR0_H_ERROR) { |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 1117 | dev_dbg(musb->controller, "no response, csr0 %04x\n", csr); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1118 | status = -EPROTO; |
| 1119 | |
| 1120 | } else if (csr & MUSB_CSR0_H_NAKTIMEOUT) { |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 1121 | dev_dbg(musb->controller, "control NAK timeout\n"); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1122 | |
| 1123 | /* NOTE: this code path would be a good place to PAUSE a |
| 1124 | * control transfer, if another one is queued, so that |
Ajay Kumar Gupta | 1e0320f | 2009-02-24 15:26:13 -0800 | [diff] [blame] | 1125 | * ep0 is more likely to stay busy. That's already done |
| 1126 | * for bulk RX transfers. |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1127 | * |
| 1128 | * if (qh->ring.next != &musb->control), then |
| 1129 | * we have a candidate... NAKing is *NOT* an error |
| 1130 | */ |
| 1131 | musb_writew(epio, MUSB_CSR0, 0); |
| 1132 | retval = IRQ_HANDLED; |
| 1133 | } |
| 1134 | |
| 1135 | if (status) { |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 1136 | dev_dbg(musb->controller, "aborting\n"); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1137 | retval = IRQ_HANDLED; |
| 1138 | if (urb) |
| 1139 | urb->status = status; |
| 1140 | complete = true; |
| 1141 | |
| 1142 | /* use the proper sequence to abort the transfer */ |
| 1143 | if (csr & MUSB_CSR0_H_REQPKT) { |
| 1144 | csr &= ~MUSB_CSR0_H_REQPKT; |
| 1145 | musb_writew(epio, MUSB_CSR0, csr); |
| 1146 | csr &= ~MUSB_CSR0_H_NAKTIMEOUT; |
| 1147 | musb_writew(epio, MUSB_CSR0, csr); |
| 1148 | } else { |
David Brownell | 78322c1 | 2009-03-26 17:38:30 -0700 | [diff] [blame] | 1149 | musb_h_ep0_flush_fifo(hw_ep); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1150 | } |
| 1151 | |
| 1152 | musb_writeb(epio, MUSB_NAKLIMIT0, 0); |
| 1153 | |
| 1154 | /* clear it */ |
| 1155 | musb_writew(epio, MUSB_CSR0, 0); |
| 1156 | } |
| 1157 | |
| 1158 | if (unlikely(!urb)) { |
| 1159 | /* stop endpoint since we have no place for its data, this |
| 1160 | * SHOULD NEVER HAPPEN! */ |
| 1161 | ERR("no URB for end 0\n"); |
| 1162 | |
David Brownell | 78322c1 | 2009-03-26 17:38:30 -0700 | [diff] [blame] | 1163 | musb_h_ep0_flush_fifo(hw_ep); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1164 | goto done; |
| 1165 | } |
| 1166 | |
| 1167 | if (!complete) { |
| 1168 | /* call common logic and prepare response */ |
| 1169 | if (musb_h_ep0_continue(musb, len, urb)) { |
| 1170 | /* more packets required */ |
| 1171 | csr = (MUSB_EP0_IN == musb->ep0_stage) |
| 1172 | ? MUSB_CSR0_H_REQPKT : MUSB_CSR0_TXPKTRDY; |
| 1173 | } else { |
| 1174 | /* data transfer complete; perform status phase */ |
| 1175 | if (usb_pipeout(urb->pipe) |
| 1176 | || !urb->transfer_buffer_length) |
| 1177 | csr = MUSB_CSR0_H_STATUSPKT |
| 1178 | | MUSB_CSR0_H_REQPKT; |
| 1179 | else |
| 1180 | csr = MUSB_CSR0_H_STATUSPKT |
| 1181 | | MUSB_CSR0_TXPKTRDY; |
| 1182 | |
| 1183 | /* flag status stage */ |
| 1184 | musb->ep0_stage = MUSB_EP0_STATUS; |
| 1185 | |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 1186 | dev_dbg(musb->controller, "ep0 STATUS, csr %04x\n", csr); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1187 | |
| 1188 | } |
| 1189 | musb_writew(epio, MUSB_CSR0, csr); |
| 1190 | retval = IRQ_HANDLED; |
| 1191 | } else |
| 1192 | musb->ep0_stage = MUSB_EP0_IDLE; |
| 1193 | |
| 1194 | /* call completion handler if done */ |
| 1195 | if (complete) |
| 1196 | musb_advance_schedule(musb, urb, hw_ep, 1); |
| 1197 | done: |
| 1198 | return retval; |
| 1199 | } |
| 1200 | |
| 1201 | |
| 1202 | #ifdef CONFIG_USB_INVENTRA_DMA |
| 1203 | |
| 1204 | /* Host side TX (OUT) using Mentor DMA works as follows: |
| 1205 | submit_urb -> |
| 1206 | - if queue was empty, Program Endpoint |
| 1207 | - ... which starts DMA to fifo in mode 1 or 0 |
| 1208 | |
| 1209 | DMA Isr (transfer complete) -> TxAvail() |
| 1210 | - Stop DMA (~DmaEnab) (<--- Alert ... currently happens |
| 1211 | only in musb_cleanup_urb) |
| 1212 | - TxPktRdy has to be set in mode 0 or for |
| 1213 | short packets in mode 1. |
| 1214 | */ |
| 1215 | |
| 1216 | #endif |
| 1217 | |
| 1218 | /* Service a Tx-Available or dma completion irq for the endpoint */ |
| 1219 | void musb_host_tx(struct musb *musb, u8 epnum) |
| 1220 | { |
| 1221 | int pipe; |
| 1222 | bool done = false; |
| 1223 | u16 tx_csr; |
Sergei Shtylyov | 6b6e971 | 2009-03-26 18:29:19 -0700 | [diff] [blame] | 1224 | size_t length = 0; |
| 1225 | size_t offset = 0; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1226 | struct musb_hw_ep *hw_ep = musb->endpoints + epnum; |
| 1227 | void __iomem *epio = hw_ep->regs; |
Sergei Shtylyov | 3e5c6dc | 2009-03-27 12:55:16 -0700 | [diff] [blame] | 1228 | struct musb_qh *qh = hw_ep->out_qh; |
| 1229 | struct urb *urb = next_urb(qh); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1230 | u32 status = 0; |
| 1231 | void __iomem *mbase = musb->mregs; |
| 1232 | struct dma_channel *dma; |
T. S., Anil Kumar | f8afbf7f | 2010-09-24 13:44:09 +0300 | [diff] [blame] | 1233 | bool transfer_pending = false; |
Virupax Sadashivpetimath | 8e8a551 | 2012-08-07 14:46:20 +0530 | [diff] [blame] | 1234 | static bool use_sg; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1235 | |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1236 | musb_ep_select(mbase, epnum); |
| 1237 | tx_csr = musb_readw(epio, MUSB_TXCSR); |
| 1238 | |
| 1239 | /* with CPPI, DMA sometimes triggers "extra" irqs */ |
| 1240 | if (!urb) { |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 1241 | dev_dbg(musb->controller, "extra TX%d ready, csr %04x\n", epnum, tx_csr); |
Sergei Shtylyov | 6b6e971 | 2009-03-26 18:29:19 -0700 | [diff] [blame] | 1242 | return; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1243 | } |
| 1244 | |
| 1245 | pipe = urb->pipe; |
| 1246 | dma = is_dma_capable() ? hw_ep->tx_channel : NULL; |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 1247 | dev_dbg(musb->controller, "OUT/TX%d end, csr %04x%s\n", epnum, tx_csr, |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1248 | dma ? ", dma" : ""); |
| 1249 | |
| 1250 | /* check for errors */ |
| 1251 | if (tx_csr & MUSB_TXCSR_H_RXSTALL) { |
| 1252 | /* dma was disabled, fifo flushed */ |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 1253 | dev_dbg(musb->controller, "TX end %d stall\n", epnum); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1254 | |
| 1255 | /* stall; record URB status */ |
| 1256 | status = -EPIPE; |
| 1257 | |
| 1258 | } else if (tx_csr & MUSB_TXCSR_H_ERROR) { |
| 1259 | /* (NON-ISO) dma was disabled, fifo flushed */ |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 1260 | dev_dbg(musb->controller, "TX 3strikes on ep=%d\n", epnum); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1261 | |
| 1262 | status = -ETIMEDOUT; |
| 1263 | |
| 1264 | } else if (tx_csr & MUSB_TXCSR_H_NAKTIMEOUT) { |
Ajay Kumar Gupta | f283862 | 2012-07-19 13:41:59 +0530 | [diff] [blame] | 1265 | if (USB_ENDPOINT_XFER_BULK == qh->type && qh->mux == 1 |
| 1266 | && !list_is_singular(&musb->out_bulk)) { |
| 1267 | dev_dbg(musb->controller, |
| 1268 | "NAK timeout on TX%d ep\n", epnum); |
| 1269 | musb_bulk_nak_timeout(musb, hw_ep, 0); |
| 1270 | } else { |
| 1271 | dev_dbg(musb->controller, |
| 1272 | "TX end=%d device not responding\n", epnum); |
| 1273 | /* NOTE: this code path would be a good place to PAUSE a |
| 1274 | * transfer, if there's some other (nonperiodic) tx urb |
| 1275 | * that could use this fifo. (dma complicates it...) |
| 1276 | * That's already done for bulk RX transfers. |
| 1277 | * |
| 1278 | * if (bulk && qh->ring.next != &musb->out_bulk), then |
| 1279 | * we have a candidate... NAKing is *NOT* an error |
| 1280 | */ |
| 1281 | musb_ep_select(mbase, epnum); |
| 1282 | musb_writew(epio, MUSB_TXCSR, |
| 1283 | MUSB_TXCSR_H_WZC_BITS |
| 1284 | | MUSB_TXCSR_TXPKTRDY); |
| 1285 | } |
| 1286 | return; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1287 | } |
| 1288 | |
Virupax Sadashivpetimath | 8e8a551 | 2012-08-07 14:46:20 +0530 | [diff] [blame] | 1289 | done: |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1290 | if (status) { |
| 1291 | if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) { |
| 1292 | dma->status = MUSB_DMA_STATUS_CORE_ABORT; |
| 1293 | (void) musb->dma_controller->channel_abort(dma); |
| 1294 | } |
| 1295 | |
| 1296 | /* do the proper sequence to abort the transfer in the |
| 1297 | * usb core; the dma engine should already be stopped. |
| 1298 | */ |
| 1299 | musb_h_tx_flush_fifo(hw_ep); |
| 1300 | tx_csr &= ~(MUSB_TXCSR_AUTOSET |
| 1301 | | MUSB_TXCSR_DMAENAB |
| 1302 | | MUSB_TXCSR_H_ERROR |
| 1303 | | MUSB_TXCSR_H_RXSTALL |
| 1304 | | MUSB_TXCSR_H_NAKTIMEOUT |
| 1305 | ); |
| 1306 | |
| 1307 | musb_ep_select(mbase, epnum); |
| 1308 | musb_writew(epio, MUSB_TXCSR, tx_csr); |
| 1309 | /* REVISIT may need to clear FLUSHFIFO ... */ |
| 1310 | musb_writew(epio, MUSB_TXCSR, tx_csr); |
| 1311 | musb_writeb(epio, MUSB_TXINTERVAL, 0); |
| 1312 | |
| 1313 | done = true; |
| 1314 | } |
| 1315 | |
| 1316 | /* second cppi case */ |
| 1317 | if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) { |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 1318 | dev_dbg(musb->controller, "extra TX%d ready, csr %04x\n", epnum, tx_csr); |
Sergei Shtylyov | 6b6e971 | 2009-03-26 18:29:19 -0700 | [diff] [blame] | 1319 | return; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1320 | } |
| 1321 | |
Sergei Shtylyov | c7bbc05 | 2009-03-26 18:26:40 -0700 | [diff] [blame] | 1322 | if (is_dma_capable() && dma && !status) { |
| 1323 | /* |
| 1324 | * DMA has completed. But if we're using DMA mode 1 (multi |
| 1325 | * packet DMA), we need a terminal TXPKTRDY interrupt before |
| 1326 | * we can consider this transfer completed, lest we trash |
| 1327 | * its last packet when writing the next URB's data. So we |
| 1328 | * switch back to mode 0 to get that interrupt; we'll come |
| 1329 | * back here once it happens. |
| 1330 | */ |
| 1331 | if (tx_csr & MUSB_TXCSR_DMAMODE) { |
| 1332 | /* |
| 1333 | * We shouldn't clear DMAMODE with DMAENAB set; so |
| 1334 | * clear them in a safe order. That should be OK |
| 1335 | * once TXPKTRDY has been set (and I've never seen |
| 1336 | * it being 0 at this moment -- DMA interrupt latency |
| 1337 | * is significant) but if it hasn't been then we have |
| 1338 | * no choice but to stop being polite and ignore the |
| 1339 | * programmer's guide... :-) |
| 1340 | * |
| 1341 | * Note that we must write TXCSR with TXPKTRDY cleared |
| 1342 | * in order not to re-trigger the packet send (this bit |
| 1343 | * can't be cleared by CPU), and there's another caveat: |
| 1344 | * TXPKTRDY may be set shortly and then cleared in the |
| 1345 | * double-buffered FIFO mode, so we do an extra TXCSR |
| 1346 | * read for debouncing... |
| 1347 | */ |
| 1348 | tx_csr &= musb_readw(epio, MUSB_TXCSR); |
| 1349 | if (tx_csr & MUSB_TXCSR_TXPKTRDY) { |
| 1350 | tx_csr &= ~(MUSB_TXCSR_DMAENAB | |
| 1351 | MUSB_TXCSR_TXPKTRDY); |
| 1352 | musb_writew(epio, MUSB_TXCSR, |
| 1353 | tx_csr | MUSB_TXCSR_H_WZC_BITS); |
| 1354 | } |
| 1355 | tx_csr &= ~(MUSB_TXCSR_DMAMODE | |
| 1356 | MUSB_TXCSR_TXPKTRDY); |
| 1357 | musb_writew(epio, MUSB_TXCSR, |
| 1358 | tx_csr | MUSB_TXCSR_H_WZC_BITS); |
| 1359 | |
| 1360 | /* |
| 1361 | * There is no guarantee that we'll get an interrupt |
| 1362 | * after clearing DMAMODE as we might have done this |
| 1363 | * too late (after TXPKTRDY was cleared by controller). |
| 1364 | * Re-read TXCSR as we have spoiled its previous value. |
| 1365 | */ |
| 1366 | tx_csr = musb_readw(epio, MUSB_TXCSR); |
| 1367 | } |
| 1368 | |
| 1369 | /* |
| 1370 | * We may get here from a DMA completion or TXPKTRDY interrupt. |
| 1371 | * In any case, we must check the FIFO status here and bail out |
| 1372 | * only if the FIFO still has data -- that should prevent the |
| 1373 | * "missed" TXPKTRDY interrupts and deal with double-buffered |
| 1374 | * FIFO mode too... |
| 1375 | */ |
| 1376 | if (tx_csr & (MUSB_TXCSR_FIFONOTEMPTY | MUSB_TXCSR_TXPKTRDY)) { |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 1377 | dev_dbg(musb->controller, "DMA complete but packet still in FIFO, " |
Sergei Shtylyov | c7bbc05 | 2009-03-26 18:26:40 -0700 | [diff] [blame] | 1378 | "CSR %04x\n", tx_csr); |
| 1379 | return; |
| 1380 | } |
| 1381 | } |
| 1382 | |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1383 | if (!status || dma || usb_pipeisoc(pipe)) { |
| 1384 | if (dma) |
Sergei Shtylyov | 6b6e971 | 2009-03-26 18:29:19 -0700 | [diff] [blame] | 1385 | length = dma->actual_len; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1386 | else |
Sergei Shtylyov | 6b6e971 | 2009-03-26 18:29:19 -0700 | [diff] [blame] | 1387 | length = qh->segsize; |
| 1388 | qh->offset += length; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1389 | |
| 1390 | if (usb_pipeisoc(pipe)) { |
| 1391 | struct usb_iso_packet_descriptor *d; |
| 1392 | |
| 1393 | d = urb->iso_frame_desc + qh->iso_idx; |
Sergei Shtylyov | 6b6e971 | 2009-03-26 18:29:19 -0700 | [diff] [blame] | 1394 | d->actual_length = length; |
| 1395 | d->status = status; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1396 | if (++qh->iso_idx >= urb->number_of_packets) { |
| 1397 | done = true; |
| 1398 | } else { |
| 1399 | d++; |
Sergei Shtylyov | 6b6e971 | 2009-03-26 18:29:19 -0700 | [diff] [blame] | 1400 | offset = d->offset; |
| 1401 | length = d->length; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1402 | } |
T. S., Anil Kumar | f8afbf7f | 2010-09-24 13:44:09 +0300 | [diff] [blame] | 1403 | } else if (dma && urb->transfer_buffer_length == qh->offset) { |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1404 | done = true; |
| 1405 | } else { |
| 1406 | /* see if we need to send more data, or ZLP */ |
| 1407 | if (qh->segsize < qh->maxpacket) |
| 1408 | done = true; |
| 1409 | else if (qh->offset == urb->transfer_buffer_length |
| 1410 | && !(urb->transfer_flags |
| 1411 | & URB_ZERO_PACKET)) |
| 1412 | done = true; |
| 1413 | if (!done) { |
Sergei Shtylyov | 6b6e971 | 2009-03-26 18:29:19 -0700 | [diff] [blame] | 1414 | offset = qh->offset; |
| 1415 | length = urb->transfer_buffer_length - offset; |
T. S., Anil Kumar | f8afbf7f | 2010-09-24 13:44:09 +0300 | [diff] [blame] | 1416 | transfer_pending = true; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1417 | } |
| 1418 | } |
| 1419 | } |
| 1420 | |
| 1421 | /* urb->status != -EINPROGRESS means request has been faulted, |
| 1422 | * so we must abort this transfer after cleanup |
| 1423 | */ |
| 1424 | if (urb->status != -EINPROGRESS) { |
| 1425 | done = true; |
| 1426 | if (status == 0) |
| 1427 | status = urb->status; |
| 1428 | } |
| 1429 | |
| 1430 | if (done) { |
| 1431 | /* set status */ |
| 1432 | urb->status = status; |
| 1433 | urb->actual_length = qh->offset; |
| 1434 | musb_advance_schedule(musb, urb, hw_ep, USB_DIR_OUT); |
Sergei Shtylyov | 6b6e971 | 2009-03-26 18:29:19 -0700 | [diff] [blame] | 1435 | return; |
T. S., Anil Kumar | f8afbf7f | 2010-09-24 13:44:09 +0300 | [diff] [blame] | 1436 | } else if ((usb_pipeisoc(pipe) || transfer_pending) && dma) { |
Sergei Shtylyov | 6b6e971 | 2009-03-26 18:29:19 -0700 | [diff] [blame] | 1437 | if (musb_tx_dma_program(musb->dma_controller, hw_ep, qh, urb, |
Ajay Kumar Gupta | dfeffa5 | 2009-11-17 15:22:55 +0530 | [diff] [blame] | 1438 | offset, length)) { |
| 1439 | if (is_cppi_enabled() || tusb_dma_omap()) |
| 1440 | musb_h_tx_dma_start(hw_ep); |
Sergei Shtylyov | 6b6e971 | 2009-03-26 18:29:19 -0700 | [diff] [blame] | 1441 | return; |
Ajay Kumar Gupta | dfeffa5 | 2009-11-17 15:22:55 +0530 | [diff] [blame] | 1442 | } |
Sergei Shtylyov | 6b6e971 | 2009-03-26 18:29:19 -0700 | [diff] [blame] | 1443 | } else if (tx_csr & MUSB_TXCSR_DMAENAB) { |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 1444 | dev_dbg(musb->controller, "not complete, but DMA enabled?\n"); |
Sergei Shtylyov | 6b6e971 | 2009-03-26 18:29:19 -0700 | [diff] [blame] | 1445 | return; |
| 1446 | } |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1447 | |
Sergei Shtylyov | 6b6e971 | 2009-03-26 18:29:19 -0700 | [diff] [blame] | 1448 | /* |
| 1449 | * PIO: start next packet in this URB. |
| 1450 | * |
| 1451 | * REVISIT: some docs say that when hw_ep->tx_double_buffered, |
| 1452 | * (and presumably, FIFO is not half-full) we should write *two* |
| 1453 | * packets before updating TXCSR; other docs disagree... |
| 1454 | */ |
| 1455 | if (length > qh->maxpacket) |
| 1456 | length = qh->maxpacket; |
Maulik Mankad | 496dda7 | 2010-09-24 13:44:06 +0300 | [diff] [blame] | 1457 | /* Unmap the buffer so that CPU can use it */ |
Robert Morell | c8cf203 | 2011-01-26 19:06:47 -0800 | [diff] [blame] | 1458 | usb_hcd_unmap_urb_for_dma(musb_to_hcd(musb), urb); |
Virupax Sadashivpetimath | 8e8a551 | 2012-08-07 14:46:20 +0530 | [diff] [blame] | 1459 | |
| 1460 | /* |
| 1461 | * We need to map sg if the transfer_buffer is |
| 1462 | * NULL. |
| 1463 | */ |
| 1464 | if (!urb->transfer_buffer) |
| 1465 | use_sg = true; |
| 1466 | |
| 1467 | if (use_sg) { |
| 1468 | /* sg_miter_start is already done in musb_ep_program */ |
| 1469 | if (!sg_miter_next(&qh->sg_miter)) { |
| 1470 | dev_err(musb->controller, "error: sg list empty\n"); |
| 1471 | sg_miter_stop(&qh->sg_miter); |
| 1472 | status = -EINVAL; |
| 1473 | goto done; |
| 1474 | } |
| 1475 | urb->transfer_buffer = qh->sg_miter.addr; |
| 1476 | length = min_t(u32, length, qh->sg_miter.length); |
| 1477 | musb_write_fifo(hw_ep, length, urb->transfer_buffer); |
| 1478 | qh->sg_miter.consumed = length; |
| 1479 | sg_miter_stop(&qh->sg_miter); |
| 1480 | } else { |
| 1481 | musb_write_fifo(hw_ep, length, urb->transfer_buffer + offset); |
| 1482 | } |
| 1483 | |
Sergei Shtylyov | 6b6e971 | 2009-03-26 18:29:19 -0700 | [diff] [blame] | 1484 | qh->segsize = length; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1485 | |
Virupax Sadashivpetimath | 8e8a551 | 2012-08-07 14:46:20 +0530 | [diff] [blame] | 1486 | if (use_sg) { |
| 1487 | if (offset + length >= urb->transfer_buffer_length) |
| 1488 | use_sg = false; |
| 1489 | } |
| 1490 | |
Sergei Shtylyov | 6b6e971 | 2009-03-26 18:29:19 -0700 | [diff] [blame] | 1491 | musb_ep_select(mbase, epnum); |
| 1492 | musb_writew(epio, MUSB_TXCSR, |
| 1493 | MUSB_TXCSR_H_WZC_BITS | MUSB_TXCSR_TXPKTRDY); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1494 | } |
| 1495 | |
| 1496 | |
| 1497 | #ifdef CONFIG_USB_INVENTRA_DMA |
| 1498 | |
| 1499 | /* Host side RX (IN) using Mentor DMA works as follows: |
| 1500 | submit_urb -> |
| 1501 | - if queue was empty, ProgramEndpoint |
| 1502 | - first IN token is sent out (by setting ReqPkt) |
| 1503 | LinuxIsr -> RxReady() |
| 1504 | /\ => first packet is received |
| 1505 | | - Set in mode 0 (DmaEnab, ~ReqPkt) |
| 1506 | | -> DMA Isr (transfer complete) -> RxReady() |
| 1507 | | - Ack receive (~RxPktRdy), turn off DMA (~DmaEnab) |
| 1508 | | - if urb not complete, send next IN token (ReqPkt) |
| 1509 | | | else complete urb. |
| 1510 | | | |
| 1511 | --------------------------- |
| 1512 | * |
| 1513 | * Nuances of mode 1: |
| 1514 | * For short packets, no ack (+RxPktRdy) is sent automatically |
| 1515 | * (even if AutoClear is ON) |
| 1516 | * For full packets, ack (~RxPktRdy) and next IN token (+ReqPkt) is sent |
| 1517 | * automatically => major problem, as collecting the next packet becomes |
| 1518 | * difficult. Hence mode 1 is not used. |
| 1519 | * |
| 1520 | * REVISIT |
| 1521 | * All we care about at this driver level is that |
| 1522 | * (a) all URBs terminate with REQPKT cleared and fifo(s) empty; |
| 1523 | * (b) termination conditions are: short RX, or buffer full; |
| 1524 | * (c) fault modes include |
| 1525 | * - iff URB_SHORT_NOT_OK, short RX status is -EREMOTEIO. |
| 1526 | * (and that endpoint's dma queue stops immediately) |
| 1527 | * - overflow (full, PLUS more bytes in the terminal packet) |
| 1528 | * |
| 1529 | * So for example, usb-storage sets URB_SHORT_NOT_OK, and would |
| 1530 | * thus be a great candidate for using mode 1 ... for all but the |
| 1531 | * last packet of one URB's transfer. |
| 1532 | */ |
| 1533 | |
| 1534 | #endif |
| 1535 | |
| 1536 | /* |
| 1537 | * Service an RX interrupt for the given IN endpoint; docs cover bulk, iso, |
| 1538 | * and high-bandwidth IN transfer cases. |
| 1539 | */ |
| 1540 | void musb_host_rx(struct musb *musb, u8 epnum) |
| 1541 | { |
| 1542 | struct urb *urb; |
| 1543 | struct musb_hw_ep *hw_ep = musb->endpoints + epnum; |
| 1544 | void __iomem *epio = hw_ep->regs; |
| 1545 | struct musb_qh *qh = hw_ep->in_qh; |
| 1546 | size_t xfer_len; |
| 1547 | void __iomem *mbase = musb->mregs; |
| 1548 | int pipe; |
| 1549 | u16 rx_csr, val; |
| 1550 | bool iso_err = false; |
| 1551 | bool done = false; |
| 1552 | u32 status; |
| 1553 | struct dma_channel *dma; |
Virupax Sadashivpetimath | 8e8a551 | 2012-08-07 14:46:20 +0530 | [diff] [blame] | 1554 | static bool use_sg; |
| 1555 | unsigned int sg_flags = SG_MITER_ATOMIC | SG_MITER_TO_SG; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1556 | |
| 1557 | musb_ep_select(mbase, epnum); |
| 1558 | |
| 1559 | urb = next_urb(qh); |
| 1560 | dma = is_dma_capable() ? hw_ep->rx_channel : NULL; |
| 1561 | status = 0; |
| 1562 | xfer_len = 0; |
| 1563 | |
| 1564 | rx_csr = musb_readw(epio, MUSB_RXCSR); |
| 1565 | val = rx_csr; |
| 1566 | |
| 1567 | if (unlikely(!urb)) { |
| 1568 | /* REVISIT -- THIS SHOULD NEVER HAPPEN ... but, at least |
| 1569 | * usbtest #11 (unlinks) triggers it regularly, sometimes |
| 1570 | * with fifo full. (Only with DMA??) |
| 1571 | */ |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 1572 | dev_dbg(musb->controller, "BOGUS RX%d ready, csr %04x, count %d\n", epnum, val, |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1573 | musb_readw(epio, MUSB_RXCOUNT)); |
| 1574 | musb_h_flush_rxfifo(hw_ep, MUSB_RXCSR_CLRDATATOG); |
| 1575 | return; |
| 1576 | } |
| 1577 | |
| 1578 | pipe = urb->pipe; |
| 1579 | |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 1580 | dev_dbg(musb->controller, "<== hw %d rxcsr %04x, urb actual %d (+dma %zu)\n", |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1581 | epnum, rx_csr, urb->actual_length, |
| 1582 | dma ? dma->actual_len : 0); |
| 1583 | |
| 1584 | /* check for errors, concurrent stall & unlink is not really |
| 1585 | * handled yet! */ |
| 1586 | if (rx_csr & MUSB_RXCSR_H_RXSTALL) { |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 1587 | dev_dbg(musb->controller, "RX end %d STALL\n", epnum); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1588 | |
| 1589 | /* stall; record URB status */ |
| 1590 | status = -EPIPE; |
| 1591 | |
| 1592 | } else if (rx_csr & MUSB_RXCSR_H_ERROR) { |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 1593 | dev_dbg(musb->controller, "end %d RX proto error\n", epnum); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1594 | |
| 1595 | status = -EPROTO; |
| 1596 | musb_writeb(epio, MUSB_RXINTERVAL, 0); |
| 1597 | |
| 1598 | } else if (rx_csr & MUSB_RXCSR_DATAERROR) { |
| 1599 | |
| 1600 | if (USB_ENDPOINT_XFER_ISOC != qh->type) { |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 1601 | dev_dbg(musb->controller, "RX end %d NAK timeout\n", epnum); |
Ajay Kumar Gupta | 1e0320f | 2009-02-24 15:26:13 -0800 | [diff] [blame] | 1602 | |
| 1603 | /* NOTE: NAKing is *NOT* an error, so we want to |
| 1604 | * continue. Except ... if there's a request for |
| 1605 | * another QH, use that instead of starving it. |
| 1606 | * |
| 1607 | * Devices like Ethernet and serial adapters keep |
| 1608 | * reads posted at all times, which will starve |
| 1609 | * other devices without this logic. |
| 1610 | */ |
| 1611 | if (usb_pipebulk(urb->pipe) |
| 1612 | && qh->mux == 1 |
| 1613 | && !list_is_singular(&musb->in_bulk)) { |
Ajay Kumar Gupta | f283862 | 2012-07-19 13:41:59 +0530 | [diff] [blame] | 1614 | musb_bulk_nak_timeout(musb, hw_ep, 1); |
Ajay Kumar Gupta | 1e0320f | 2009-02-24 15:26:13 -0800 | [diff] [blame] | 1615 | return; |
| 1616 | } |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1617 | musb_ep_select(mbase, epnum); |
Ajay Kumar Gupta | 1e0320f | 2009-02-24 15:26:13 -0800 | [diff] [blame] | 1618 | rx_csr |= MUSB_RXCSR_H_WZC_BITS; |
| 1619 | rx_csr &= ~MUSB_RXCSR_DATAERROR; |
| 1620 | musb_writew(epio, MUSB_RXCSR, rx_csr); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1621 | |
| 1622 | goto finish; |
| 1623 | } else { |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 1624 | dev_dbg(musb->controller, "RX end %d ISO data error\n", epnum); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1625 | /* packet error reported later */ |
| 1626 | iso_err = true; |
| 1627 | } |
Ajay Kumar Gupta | a483d70 | 2009-04-03 16:16:17 -0700 | [diff] [blame] | 1628 | } else if (rx_csr & MUSB_RXCSR_INCOMPRX) { |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 1629 | dev_dbg(musb->controller, "end %d high bandwidth incomplete ISO packet RX\n", |
Ajay Kumar Gupta | a483d70 | 2009-04-03 16:16:17 -0700 | [diff] [blame] | 1630 | epnum); |
| 1631 | status = -EPROTO; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1632 | } |
| 1633 | |
| 1634 | /* faults abort the transfer */ |
| 1635 | if (status) { |
| 1636 | /* clean up dma and collect transfer count */ |
| 1637 | if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) { |
| 1638 | dma->status = MUSB_DMA_STATUS_CORE_ABORT; |
| 1639 | (void) musb->dma_controller->channel_abort(dma); |
| 1640 | xfer_len = dma->actual_len; |
| 1641 | } |
| 1642 | musb_h_flush_rxfifo(hw_ep, MUSB_RXCSR_CLRDATATOG); |
| 1643 | musb_writeb(epio, MUSB_RXINTERVAL, 0); |
| 1644 | done = true; |
| 1645 | goto finish; |
| 1646 | } |
| 1647 | |
| 1648 | if (unlikely(dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY)) { |
| 1649 | /* SHOULD NEVER HAPPEN ... but at least DaVinci has done it */ |
| 1650 | ERR("RX%d dma busy, csr %04x\n", epnum, rx_csr); |
| 1651 | goto finish; |
| 1652 | } |
| 1653 | |
| 1654 | /* thorough shutdown for now ... given more precise fault handling |
| 1655 | * and better queueing support, we might keep a DMA pipeline going |
| 1656 | * while processing this irq for earlier completions. |
| 1657 | */ |
| 1658 | |
| 1659 | /* FIXME this is _way_ too much in-line logic for Mentor DMA */ |
| 1660 | |
| 1661 | #ifndef CONFIG_USB_INVENTRA_DMA |
| 1662 | if (rx_csr & MUSB_RXCSR_H_REQPKT) { |
| 1663 | /* REVISIT this happened for a while on some short reads... |
| 1664 | * the cleanup still needs investigation... looks bad... |
| 1665 | * and also duplicates dma cleanup code above ... plus, |
| 1666 | * shouldn't this be the "half full" double buffer case? |
| 1667 | */ |
| 1668 | if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) { |
| 1669 | dma->status = MUSB_DMA_STATUS_CORE_ABORT; |
| 1670 | (void) musb->dma_controller->channel_abort(dma); |
| 1671 | xfer_len = dma->actual_len; |
| 1672 | done = true; |
| 1673 | } |
| 1674 | |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 1675 | dev_dbg(musb->controller, "RXCSR%d %04x, reqpkt, len %zu%s\n", epnum, rx_csr, |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1676 | xfer_len, dma ? ", dma" : ""); |
| 1677 | rx_csr &= ~MUSB_RXCSR_H_REQPKT; |
| 1678 | |
| 1679 | musb_ep_select(mbase, epnum); |
| 1680 | musb_writew(epio, MUSB_RXCSR, |
| 1681 | MUSB_RXCSR_H_WZC_BITS | rx_csr); |
| 1682 | } |
| 1683 | #endif |
| 1684 | if (dma && (rx_csr & MUSB_RXCSR_DMAENAB)) { |
| 1685 | xfer_len = dma->actual_len; |
| 1686 | |
| 1687 | val &= ~(MUSB_RXCSR_DMAENAB |
| 1688 | | MUSB_RXCSR_H_AUTOREQ |
| 1689 | | MUSB_RXCSR_AUTOCLEAR |
| 1690 | | MUSB_RXCSR_RXPKTRDY); |
| 1691 | musb_writew(hw_ep->regs, MUSB_RXCSR, val); |
| 1692 | |
| 1693 | #ifdef CONFIG_USB_INVENTRA_DMA |
Ajay Kumar Gupta | f82a689 | 2008-10-29 15:10:31 +0200 | [diff] [blame] | 1694 | if (usb_pipeisoc(pipe)) { |
| 1695 | struct usb_iso_packet_descriptor *d; |
| 1696 | |
| 1697 | d = urb->iso_frame_desc + qh->iso_idx; |
| 1698 | d->actual_length = xfer_len; |
| 1699 | |
| 1700 | /* even if there was an error, we did the dma |
| 1701 | * for iso_frame_desc->length |
| 1702 | */ |
Márton Németh | 72887c8 | 2011-05-30 20:45:42 +0200 | [diff] [blame] | 1703 | if (d->status != -EILSEQ && d->status != -EOVERFLOW) |
Ajay Kumar Gupta | f82a689 | 2008-10-29 15:10:31 +0200 | [diff] [blame] | 1704 | d->status = 0; |
| 1705 | |
| 1706 | if (++qh->iso_idx >= urb->number_of_packets) |
| 1707 | done = true; |
| 1708 | else |
| 1709 | done = false; |
| 1710 | |
| 1711 | } else { |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1712 | /* done if urb buffer is full or short packet is recd */ |
| 1713 | done = (urb->actual_length + xfer_len >= |
| 1714 | urb->transfer_buffer_length |
| 1715 | || dma->actual_len < qh->maxpacket); |
Ajay Kumar Gupta | f82a689 | 2008-10-29 15:10:31 +0200 | [diff] [blame] | 1716 | } |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1717 | |
| 1718 | /* send IN token for next packet, without AUTOREQ */ |
| 1719 | if (!done) { |
| 1720 | val |= MUSB_RXCSR_H_REQPKT; |
| 1721 | musb_writew(epio, MUSB_RXCSR, |
| 1722 | MUSB_RXCSR_H_WZC_BITS | val); |
| 1723 | } |
| 1724 | |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 1725 | dev_dbg(musb->controller, "ep %d dma %s, rxcsr %04x, rxcount %d\n", epnum, |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1726 | done ? "off" : "reset", |
| 1727 | musb_readw(epio, MUSB_RXCSR), |
| 1728 | musb_readw(epio, MUSB_RXCOUNT)); |
| 1729 | #else |
| 1730 | done = true; |
| 1731 | #endif |
| 1732 | } else if (urb->status == -EINPROGRESS) { |
| 1733 | /* if no errors, be sure a packet is ready for unloading */ |
| 1734 | if (unlikely(!(rx_csr & MUSB_RXCSR_RXPKTRDY))) { |
| 1735 | status = -EPROTO; |
| 1736 | ERR("Rx interrupt with no errors or packet!\n"); |
| 1737 | |
| 1738 | /* FIXME this is another "SHOULD NEVER HAPPEN" */ |
| 1739 | |
| 1740 | /* SCRUB (RX) */ |
| 1741 | /* do the proper sequence to abort the transfer */ |
| 1742 | musb_ep_select(mbase, epnum); |
| 1743 | val &= ~MUSB_RXCSR_H_REQPKT; |
| 1744 | musb_writew(epio, MUSB_RXCSR, val); |
| 1745 | goto finish; |
| 1746 | } |
| 1747 | |
| 1748 | /* we are expecting IN packets */ |
| 1749 | #ifdef CONFIG_USB_INVENTRA_DMA |
| 1750 | if (dma) { |
| 1751 | struct dma_controller *c; |
| 1752 | u16 rx_count; |
Ajay Kumar Gupta | f82a689 | 2008-10-29 15:10:31 +0200 | [diff] [blame] | 1753 | int ret, length; |
| 1754 | dma_addr_t buf; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1755 | |
| 1756 | rx_count = musb_readw(epio, MUSB_RXCOUNT); |
| 1757 | |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 1758 | dev_dbg(musb->controller, "RX%d count %d, buffer 0x%x len %d/%d\n", |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1759 | epnum, rx_count, |
| 1760 | urb->transfer_dma |
| 1761 | + urb->actual_length, |
| 1762 | qh->offset, |
| 1763 | urb->transfer_buffer_length); |
| 1764 | |
| 1765 | c = musb->dma_controller; |
| 1766 | |
Ajay Kumar Gupta | f82a689 | 2008-10-29 15:10:31 +0200 | [diff] [blame] | 1767 | if (usb_pipeisoc(pipe)) { |
Felipe Balbi | 8b4959d | 2009-12-04 15:47:47 +0200 | [diff] [blame] | 1768 | int d_status = 0; |
Ajay Kumar Gupta | f82a689 | 2008-10-29 15:10:31 +0200 | [diff] [blame] | 1769 | struct usb_iso_packet_descriptor *d; |
| 1770 | |
| 1771 | d = urb->iso_frame_desc + qh->iso_idx; |
| 1772 | |
| 1773 | if (iso_err) { |
Felipe Balbi | 8b4959d | 2009-12-04 15:47:47 +0200 | [diff] [blame] | 1774 | d_status = -EILSEQ; |
Ajay Kumar Gupta | f82a689 | 2008-10-29 15:10:31 +0200 | [diff] [blame] | 1775 | urb->error_count++; |
| 1776 | } |
| 1777 | if (rx_count > d->length) { |
Felipe Balbi | 8b4959d | 2009-12-04 15:47:47 +0200 | [diff] [blame] | 1778 | if (d_status == 0) { |
| 1779 | d_status = -EOVERFLOW; |
Ajay Kumar Gupta | f82a689 | 2008-10-29 15:10:31 +0200 | [diff] [blame] | 1780 | urb->error_count++; |
| 1781 | } |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 1782 | dev_dbg(musb->controller, "** OVERFLOW %d into %d\n",\ |
Ajay Kumar Gupta | f82a689 | 2008-10-29 15:10:31 +0200 | [diff] [blame] | 1783 | rx_count, d->length); |
| 1784 | |
| 1785 | length = d->length; |
| 1786 | } else |
| 1787 | length = rx_count; |
Felipe Balbi | 8b4959d | 2009-12-04 15:47:47 +0200 | [diff] [blame] | 1788 | d->status = d_status; |
Ajay Kumar Gupta | f82a689 | 2008-10-29 15:10:31 +0200 | [diff] [blame] | 1789 | buf = urb->transfer_dma + d->offset; |
| 1790 | } else { |
| 1791 | length = rx_count; |
| 1792 | buf = urb->transfer_dma + |
| 1793 | urb->actual_length; |
| 1794 | } |
| 1795 | |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1796 | dma->desired_mode = 0; |
| 1797 | #ifdef USE_MODE1 |
| 1798 | /* because of the issue below, mode 1 will |
| 1799 | * only rarely behave with correct semantics. |
| 1800 | */ |
| 1801 | if ((urb->transfer_flags & |
| 1802 | URB_SHORT_NOT_OK) |
| 1803 | && (urb->transfer_buffer_length - |
| 1804 | urb->actual_length) |
| 1805 | > qh->maxpacket) |
| 1806 | dma->desired_mode = 1; |
Ajay Kumar Gupta | f82a689 | 2008-10-29 15:10:31 +0200 | [diff] [blame] | 1807 | if (rx_count < hw_ep->max_packet_sz_rx) { |
| 1808 | length = rx_count; |
Sonic Zhang | ae92697 | 2010-03-08 11:26:01 -0500 | [diff] [blame] | 1809 | dma->desired_mode = 0; |
Ajay Kumar Gupta | f82a689 | 2008-10-29 15:10:31 +0200 | [diff] [blame] | 1810 | } else { |
| 1811 | length = urb->transfer_buffer_length; |
| 1812 | } |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1813 | #endif |
| 1814 | |
| 1815 | /* Disadvantage of using mode 1: |
| 1816 | * It's basically usable only for mass storage class; essentially all |
| 1817 | * other protocols also terminate transfers on short packets. |
| 1818 | * |
| 1819 | * Details: |
| 1820 | * An extra IN token is sent at the end of the transfer (due to AUTOREQ) |
| 1821 | * If you try to use mode 1 for (transfer_buffer_length - 512), and try |
| 1822 | * to use the extra IN token to grab the last packet using mode 0, then |
| 1823 | * the problem is that you cannot be sure when the device will send the |
| 1824 | * last packet and RxPktRdy set. Sometimes the packet is recd too soon |
| 1825 | * such that it gets lost when RxCSR is re-set at the end of the mode 1 |
| 1826 | * transfer, while sometimes it is recd just a little late so that if you |
| 1827 | * try to configure for mode 0 soon after the mode 1 transfer is |
| 1828 | * completed, you will find rxcount 0. Okay, so you might think why not |
| 1829 | * wait for an interrupt when the pkt is recd. Well, you won't get any! |
| 1830 | */ |
| 1831 | |
| 1832 | val = musb_readw(epio, MUSB_RXCSR); |
| 1833 | val &= ~MUSB_RXCSR_H_REQPKT; |
| 1834 | |
| 1835 | if (dma->desired_mode == 0) |
| 1836 | val &= ~MUSB_RXCSR_H_AUTOREQ; |
| 1837 | else |
| 1838 | val |= MUSB_RXCSR_H_AUTOREQ; |
Ajay Kumar Gupta | a483d70 | 2009-04-03 16:16:17 -0700 | [diff] [blame] | 1839 | val |= MUSB_RXCSR_DMAENAB; |
| 1840 | |
| 1841 | /* autoclear shouldn't be set in high bandwidth */ |
| 1842 | if (qh->hb_mult == 1) |
| 1843 | val |= MUSB_RXCSR_AUTOCLEAR; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1844 | |
| 1845 | musb_writew(epio, MUSB_RXCSR, |
| 1846 | MUSB_RXCSR_H_WZC_BITS | val); |
| 1847 | |
| 1848 | /* REVISIT if when actual_length != 0, |
| 1849 | * transfer_buffer_length needs to be |
| 1850 | * adjusted first... |
| 1851 | */ |
| 1852 | ret = c->channel_program( |
| 1853 | dma, qh->maxpacket, |
Ajay Kumar Gupta | f82a689 | 2008-10-29 15:10:31 +0200 | [diff] [blame] | 1854 | dma->desired_mode, buf, length); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1855 | |
| 1856 | if (!ret) { |
| 1857 | c->channel_release(dma); |
| 1858 | hw_ep->rx_channel = NULL; |
| 1859 | dma = NULL; |
Mantesh Sarasetti | 2ed9127 | 2012-06-01 14:54:30 +0300 | [diff] [blame] | 1860 | val = musb_readw(epio, MUSB_RXCSR); |
| 1861 | val &= ~(MUSB_RXCSR_DMAENAB |
| 1862 | | MUSB_RXCSR_H_AUTOREQ |
| 1863 | | MUSB_RXCSR_AUTOCLEAR); |
| 1864 | musb_writew(epio, MUSB_RXCSR, val); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1865 | } |
| 1866 | } |
| 1867 | #endif /* Mentor DMA */ |
| 1868 | |
| 1869 | if (!dma) { |
Virupax Sadashivpetimath | 8e8a551 | 2012-08-07 14:46:20 +0530 | [diff] [blame] | 1870 | unsigned int received_len; |
| 1871 | |
Maulik Mankad | 496dda7 | 2010-09-24 13:44:06 +0300 | [diff] [blame] | 1872 | /* Unmap the buffer so that CPU can use it */ |
Robert Morell | c8cf203 | 2011-01-26 19:06:47 -0800 | [diff] [blame] | 1873 | usb_hcd_unmap_urb_for_dma(musb_to_hcd(musb), urb); |
Virupax Sadashivpetimath | 8e8a551 | 2012-08-07 14:46:20 +0530 | [diff] [blame] | 1874 | |
| 1875 | /* |
| 1876 | * We need to map sg if the transfer_buffer is |
| 1877 | * NULL. |
| 1878 | */ |
| 1879 | if (!urb->transfer_buffer) { |
| 1880 | use_sg = true; |
| 1881 | sg_miter_start(&qh->sg_miter, urb->sg, 1, |
| 1882 | sg_flags); |
| 1883 | } |
| 1884 | |
| 1885 | if (use_sg) { |
| 1886 | if (!sg_miter_next(&qh->sg_miter)) { |
| 1887 | dev_err(musb->controller, "error: sg list empty\n"); |
| 1888 | sg_miter_stop(&qh->sg_miter); |
| 1889 | status = -EINVAL; |
| 1890 | done = true; |
| 1891 | goto finish; |
| 1892 | } |
| 1893 | urb->transfer_buffer = qh->sg_miter.addr; |
| 1894 | received_len = urb->actual_length; |
| 1895 | qh->offset = 0x0; |
| 1896 | done = musb_host_packet_rx(musb, urb, epnum, |
| 1897 | iso_err); |
| 1898 | /* Calculate the number of bytes received */ |
| 1899 | received_len = urb->actual_length - |
| 1900 | received_len; |
| 1901 | qh->sg_miter.consumed = received_len; |
| 1902 | sg_miter_stop(&qh->sg_miter); |
| 1903 | } else { |
| 1904 | done = musb_host_packet_rx(musb, urb, |
| 1905 | epnum, iso_err); |
| 1906 | } |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 1907 | dev_dbg(musb->controller, "read %spacket\n", done ? "last " : ""); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1908 | } |
| 1909 | } |
| 1910 | |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1911 | finish: |
| 1912 | urb->actual_length += xfer_len; |
| 1913 | qh->offset += xfer_len; |
| 1914 | if (done) { |
Virupax Sadashivpetimath | 8e8a551 | 2012-08-07 14:46:20 +0530 | [diff] [blame] | 1915 | if (use_sg) |
| 1916 | use_sg = false; |
| 1917 | |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1918 | if (urb->status == -EINPROGRESS) |
| 1919 | urb->status = status; |
| 1920 | musb_advance_schedule(musb, urb, hw_ep, USB_DIR_IN); |
| 1921 | } |
| 1922 | } |
| 1923 | |
| 1924 | /* schedule nodes correspond to peripheral endpoints, like an OHCI QH. |
| 1925 | * the software schedule associates multiple such nodes with a given |
| 1926 | * host side hardware endpoint + direction; scheduling may activate |
| 1927 | * that hardware endpoint. |
| 1928 | */ |
| 1929 | static int musb_schedule( |
| 1930 | struct musb *musb, |
| 1931 | struct musb_qh *qh, |
| 1932 | int is_in) |
| 1933 | { |
| 1934 | int idle; |
| 1935 | int best_diff; |
| 1936 | int best_end, epnum; |
| 1937 | struct musb_hw_ep *hw_ep = NULL; |
| 1938 | struct list_head *head = NULL; |
Swaminathan S | 5274dab | 2009-12-28 13:40:37 +0200 | [diff] [blame] | 1939 | u8 toggle; |
| 1940 | u8 txtype; |
| 1941 | struct urb *urb = next_urb(qh); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1942 | |
| 1943 | /* use fixed hardware for control and bulk */ |
Ajay Kumar Gupta | 23d15e0 | 2008-10-29 15:10:35 +0200 | [diff] [blame] | 1944 | if (qh->type == USB_ENDPOINT_XFER_CONTROL) { |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1945 | head = &musb->control; |
| 1946 | hw_ep = musb->control_ep; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1947 | goto success; |
| 1948 | } |
| 1949 | |
| 1950 | /* else, periodic transfers get muxed to other endpoints */ |
| 1951 | |
Sergei Shtylyov | 5d67a85 | 2009-02-24 15:23:34 -0800 | [diff] [blame] | 1952 | /* |
| 1953 | * We know this qh hasn't been scheduled, so all we need to do |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1954 | * is choose which hardware endpoint to put it on ... |
| 1955 | * |
| 1956 | * REVISIT what we really want here is a regular schedule tree |
Sergei Shtylyov | 5d67a85 | 2009-02-24 15:23:34 -0800 | [diff] [blame] | 1957 | * like e.g. OHCI uses. |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1958 | */ |
| 1959 | best_diff = 4096; |
| 1960 | best_end = -1; |
| 1961 | |
Sergei Shtylyov | 5d67a85 | 2009-02-24 15:23:34 -0800 | [diff] [blame] | 1962 | for (epnum = 1, hw_ep = musb->endpoints + 1; |
| 1963 | epnum < musb->nr_endpoints; |
| 1964 | epnum++, hw_ep++) { |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1965 | int diff; |
| 1966 | |
Sergei Shtylyov | 3e5c6dc | 2009-03-27 12:55:16 -0700 | [diff] [blame] | 1967 | if (musb_ep_get_qh(hw_ep, is_in) != NULL) |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1968 | continue; |
Sergei Shtylyov | 5d67a85 | 2009-02-24 15:23:34 -0800 | [diff] [blame] | 1969 | |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1970 | if (hw_ep == musb->bulk_ep) |
| 1971 | continue; |
| 1972 | |
| 1973 | if (is_in) |
Ajay Kumar Gupta | a483d70 | 2009-04-03 16:16:17 -0700 | [diff] [blame] | 1974 | diff = hw_ep->max_packet_sz_rx; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1975 | else |
Ajay Kumar Gupta | a483d70 | 2009-04-03 16:16:17 -0700 | [diff] [blame] | 1976 | diff = hw_ep->max_packet_sz_tx; |
| 1977 | diff -= (qh->maxpacket * qh->hb_mult); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1978 | |
Ajay Kumar Gupta | 23d15e0 | 2008-10-29 15:10:35 +0200 | [diff] [blame] | 1979 | if (diff >= 0 && best_diff > diff) { |
Swaminathan S | 5274dab | 2009-12-28 13:40:37 +0200 | [diff] [blame] | 1980 | |
| 1981 | /* |
| 1982 | * Mentor controller has a bug in that if we schedule |
| 1983 | * a BULK Tx transfer on an endpoint that had earlier |
| 1984 | * handled ISOC then the BULK transfer has to start on |
| 1985 | * a zero toggle. If the BULK transfer starts on a 1 |
| 1986 | * toggle then this transfer will fail as the mentor |
| 1987 | * controller starts the Bulk transfer on a 0 toggle |
| 1988 | * irrespective of the programming of the toggle bits |
| 1989 | * in the TXCSR register. Check for this condition |
| 1990 | * while allocating the EP for a Tx Bulk transfer. If |
| 1991 | * so skip this EP. |
| 1992 | */ |
| 1993 | hw_ep = musb->endpoints + epnum; |
| 1994 | toggle = usb_gettoggle(urb->dev, qh->epnum, !is_in); |
| 1995 | txtype = (musb_readb(hw_ep->regs, MUSB_TXTYPE) |
| 1996 | >> 4) & 0x3; |
| 1997 | if (!is_in && (qh->type == USB_ENDPOINT_XFER_BULK) && |
| 1998 | toggle && (txtype == USB_ENDPOINT_XFER_ISOC)) |
| 1999 | continue; |
| 2000 | |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2001 | best_diff = diff; |
| 2002 | best_end = epnum; |
| 2003 | } |
| 2004 | } |
Ajay Kumar Gupta | 23d15e0 | 2008-10-29 15:10:35 +0200 | [diff] [blame] | 2005 | /* use bulk reserved ep1 if no other ep is free */ |
Felipe Balbi | aa5cbbe | 2008-11-17 09:08:16 +0200 | [diff] [blame] | 2006 | if (best_end < 0 && qh->type == USB_ENDPOINT_XFER_BULK) { |
Ajay Kumar Gupta | 23d15e0 | 2008-10-29 15:10:35 +0200 | [diff] [blame] | 2007 | hw_ep = musb->bulk_ep; |
| 2008 | if (is_in) |
| 2009 | head = &musb->in_bulk; |
| 2010 | else |
| 2011 | head = &musb->out_bulk; |
Ajay Kumar Gupta | 1e0320f | 2009-02-24 15:26:13 -0800 | [diff] [blame] | 2012 | |
Ajay Kumar Gupta | f283862 | 2012-07-19 13:41:59 +0530 | [diff] [blame] | 2013 | /* Enable bulk RX/TX NAK timeout scheme when bulk requests are |
Ajay Kumar Gupta | 1e0320f | 2009-02-24 15:26:13 -0800 | [diff] [blame] | 2014 | * multiplexed. This scheme doen't work in high speed to full |
| 2015 | * speed scenario as NAK interrupts are not coming from a |
| 2016 | * full speed device connected to a high speed device. |
| 2017 | * NAK timeout interval is 8 (128 uframe or 16ms) for HS and |
| 2018 | * 4 (8 frame or 8ms) for FS device. |
| 2019 | */ |
Ajay Kumar Gupta | f283862 | 2012-07-19 13:41:59 +0530 | [diff] [blame] | 2020 | if (qh->dev) |
Ajay Kumar Gupta | 1e0320f | 2009-02-24 15:26:13 -0800 | [diff] [blame] | 2021 | qh->intv_reg = |
| 2022 | (USB_SPEED_HIGH == qh->dev->speed) ? 8 : 4; |
Ajay Kumar Gupta | 23d15e0 | 2008-10-29 15:10:35 +0200 | [diff] [blame] | 2023 | goto success; |
| 2024 | } else if (best_end < 0) { |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2025 | return -ENOSPC; |
Ajay Kumar Gupta | 23d15e0 | 2008-10-29 15:10:35 +0200 | [diff] [blame] | 2026 | } |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2027 | |
| 2028 | idle = 1; |
Ajay Kumar Gupta | 23d15e0 | 2008-10-29 15:10:35 +0200 | [diff] [blame] | 2029 | qh->mux = 0; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2030 | hw_ep = musb->endpoints + best_end; |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 2031 | dev_dbg(musb->controller, "qh %p periodic slot %d\n", qh, best_end); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2032 | success: |
Ajay Kumar Gupta | 23d15e0 | 2008-10-29 15:10:35 +0200 | [diff] [blame] | 2033 | if (head) { |
| 2034 | idle = list_empty(head); |
| 2035 | list_add_tail(&qh->ring, head); |
| 2036 | qh->mux = 1; |
| 2037 | } |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2038 | qh->hw_ep = hw_ep; |
| 2039 | qh->hep->hcpriv = qh; |
| 2040 | if (idle) |
| 2041 | musb_start_urb(musb, is_in, qh); |
| 2042 | return 0; |
| 2043 | } |
| 2044 | |
| 2045 | static int musb_urb_enqueue( |
| 2046 | struct usb_hcd *hcd, |
| 2047 | struct urb *urb, |
| 2048 | gfp_t mem_flags) |
| 2049 | { |
| 2050 | unsigned long flags; |
| 2051 | struct musb *musb = hcd_to_musb(hcd); |
| 2052 | struct usb_host_endpoint *hep = urb->ep; |
David Brownell | 74bb350 | 2009-03-26 17:36:57 -0700 | [diff] [blame] | 2053 | struct musb_qh *qh; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2054 | struct usb_endpoint_descriptor *epd = &hep->desc; |
| 2055 | int ret; |
| 2056 | unsigned type_reg; |
| 2057 | unsigned interval; |
| 2058 | |
| 2059 | /* host role must be active */ |
| 2060 | if (!is_host_active(musb) || !musb->is_active) |
| 2061 | return -ENODEV; |
| 2062 | |
| 2063 | spin_lock_irqsave(&musb->lock, flags); |
| 2064 | ret = usb_hcd_link_urb_to_ep(hcd, urb); |
David Brownell | 74bb350 | 2009-03-26 17:36:57 -0700 | [diff] [blame] | 2065 | qh = ret ? NULL : hep->hcpriv; |
| 2066 | if (qh) |
| 2067 | urb->hcpriv = qh; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2068 | spin_unlock_irqrestore(&musb->lock, flags); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2069 | |
| 2070 | /* DMA mapping was already done, if needed, and this urb is on |
David Brownell | 74bb350 | 2009-03-26 17:36:57 -0700 | [diff] [blame] | 2071 | * hep->urb_list now ... so we're done, unless hep wasn't yet |
| 2072 | * scheduled onto a live qh. |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2073 | * |
| 2074 | * REVISIT best to keep hep->hcpriv valid until the endpoint gets |
| 2075 | * disabled, testing for empty qh->ring and avoiding qh setup costs |
| 2076 | * except for the first urb queued after a config change. |
| 2077 | */ |
David Brownell | 74bb350 | 2009-03-26 17:36:57 -0700 | [diff] [blame] | 2078 | if (qh || ret) |
| 2079 | return ret; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2080 | |
| 2081 | /* Allocate and initialize qh, minimizing the work done each time |
| 2082 | * hw_ep gets reprogrammed, or with irqs blocked. Then schedule it. |
| 2083 | * |
| 2084 | * REVISIT consider a dedicated qh kmem_cache, so it's harder |
| 2085 | * for bugs in other kernel code to break this driver... |
| 2086 | */ |
| 2087 | qh = kzalloc(sizeof *qh, mem_flags); |
| 2088 | if (!qh) { |
Ajay Kumar Gupta | 2492e67 | 2008-09-11 11:53:21 +0300 | [diff] [blame] | 2089 | spin_lock_irqsave(&musb->lock, flags); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2090 | usb_hcd_unlink_urb_from_ep(hcd, urb); |
Ajay Kumar Gupta | 2492e67 | 2008-09-11 11:53:21 +0300 | [diff] [blame] | 2091 | spin_unlock_irqrestore(&musb->lock, flags); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2092 | return -ENOMEM; |
| 2093 | } |
| 2094 | |
| 2095 | qh->hep = hep; |
| 2096 | qh->dev = urb->dev; |
| 2097 | INIT_LIST_HEAD(&qh->ring); |
| 2098 | qh->is_ready = 1; |
| 2099 | |
Kuninori Morimoto | 29cc889 | 2011-08-23 03:12:03 -0700 | [diff] [blame] | 2100 | qh->maxpacket = usb_endpoint_maxp(epd); |
Ajay Kumar Gupta | a483d70 | 2009-04-03 16:16:17 -0700 | [diff] [blame] | 2101 | qh->type = usb_endpoint_type(epd); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2102 | |
Ajay Kumar Gupta | a483d70 | 2009-04-03 16:16:17 -0700 | [diff] [blame] | 2103 | /* Bits 11 & 12 of wMaxPacketSize encode high bandwidth multiplier. |
| 2104 | * Some musb cores don't support high bandwidth ISO transfers; and |
| 2105 | * we don't (yet!) support high bandwidth interrupt transfers. |
| 2106 | */ |
| 2107 | qh->hb_mult = 1 + ((qh->maxpacket >> 11) & 0x03); |
| 2108 | if (qh->hb_mult > 1) { |
| 2109 | int ok = (qh->type == USB_ENDPOINT_XFER_ISOC); |
| 2110 | |
| 2111 | if (ok) |
| 2112 | ok = (usb_pipein(urb->pipe) && musb->hb_iso_rx) |
| 2113 | || (usb_pipeout(urb->pipe) && musb->hb_iso_tx); |
| 2114 | if (!ok) { |
| 2115 | ret = -EMSGSIZE; |
| 2116 | goto done; |
| 2117 | } |
| 2118 | qh->maxpacket &= 0x7ff; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2119 | } |
| 2120 | |
Julia Lawall | 96bcd09 | 2009-01-24 17:57:24 -0800 | [diff] [blame] | 2121 | qh->epnum = usb_endpoint_num(epd); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2122 | |
| 2123 | /* NOTE: urb->dev->devnum is wrong during SET_ADDRESS */ |
| 2124 | qh->addr_reg = (u8) usb_pipedevice(urb->pipe); |
| 2125 | |
| 2126 | /* precompute rxtype/txtype/type0 register */ |
| 2127 | type_reg = (qh->type << 4) | qh->epnum; |
| 2128 | switch (urb->dev->speed) { |
| 2129 | case USB_SPEED_LOW: |
| 2130 | type_reg |= 0xc0; |
| 2131 | break; |
| 2132 | case USB_SPEED_FULL: |
| 2133 | type_reg |= 0x80; |
| 2134 | break; |
| 2135 | default: |
| 2136 | type_reg |= 0x40; |
| 2137 | } |
| 2138 | qh->type_reg = type_reg; |
| 2139 | |
Sergei Shtylyov | 136733d | 2009-02-21 15:31:35 -0800 | [diff] [blame] | 2140 | /* Precompute RXINTERVAL/TXINTERVAL register */ |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2141 | switch (qh->type) { |
| 2142 | case USB_ENDPOINT_XFER_INT: |
Sergei Shtylyov | 136733d | 2009-02-21 15:31:35 -0800 | [diff] [blame] | 2143 | /* |
| 2144 | * Full/low speeds use the linear encoding, |
| 2145 | * high speed uses the logarithmic encoding. |
| 2146 | */ |
| 2147 | if (urb->dev->speed <= USB_SPEED_FULL) { |
| 2148 | interval = max_t(u8, epd->bInterval, 1); |
| 2149 | break; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2150 | } |
| 2151 | /* FALLTHROUGH */ |
| 2152 | case USB_ENDPOINT_XFER_ISOC: |
Sergei Shtylyov | 136733d | 2009-02-21 15:31:35 -0800 | [diff] [blame] | 2153 | /* ISO always uses logarithmic encoding */ |
| 2154 | interval = min_t(u8, epd->bInterval, 16); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2155 | break; |
| 2156 | default: |
| 2157 | /* REVISIT we actually want to use NAK limits, hinting to the |
| 2158 | * transfer scheduling logic to try some other qh, e.g. try |
| 2159 | * for 2 msec first: |
| 2160 | * |
| 2161 | * interval = (USB_SPEED_HIGH == urb->dev->speed) ? 16 : 2; |
| 2162 | * |
| 2163 | * The downside of disabling this is that transfer scheduling |
| 2164 | * gets VERY unfair for nonperiodic transfers; a misbehaving |
Ajay Kumar Gupta | 1e0320f | 2009-02-24 15:26:13 -0800 | [diff] [blame] | 2165 | * peripheral could make that hurt. That's perfectly normal |
| 2166 | * for reads from network or serial adapters ... so we have |
| 2167 | * partial NAKlimit support for bulk RX. |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2168 | * |
Ajay Kumar Gupta | 1e0320f | 2009-02-24 15:26:13 -0800 | [diff] [blame] | 2169 | * The upside of disabling it is simpler transfer scheduling. |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2170 | */ |
| 2171 | interval = 0; |
| 2172 | } |
| 2173 | qh->intv_reg = interval; |
| 2174 | |
| 2175 | /* precompute addressing for external hub/tt ports */ |
| 2176 | if (musb->is_multipoint) { |
| 2177 | struct usb_device *parent = urb->dev->parent; |
| 2178 | |
| 2179 | if (parent != hcd->self.root_hub) { |
| 2180 | qh->h_addr_reg = (u8) parent->devnum; |
| 2181 | |
| 2182 | /* set up tt info if needed */ |
| 2183 | if (urb->dev->tt) { |
| 2184 | qh->h_port_reg = (u8) urb->dev->ttport; |
Ajay Kumar Gupta | ae5ad29 | 2008-09-11 11:53:20 +0300 | [diff] [blame] | 2185 | if (urb->dev->tt->hub) |
| 2186 | qh->h_addr_reg = |
| 2187 | (u8) urb->dev->tt->hub->devnum; |
| 2188 | if (urb->dev->tt->multi) |
| 2189 | qh->h_addr_reg |= 0x80; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2190 | } |
| 2191 | } |
| 2192 | } |
| 2193 | |
| 2194 | /* invariant: hep->hcpriv is null OR the qh that's already scheduled. |
| 2195 | * until we get real dma queues (with an entry for each urb/buffer), |
| 2196 | * we only have work to do in the former case. |
| 2197 | */ |
| 2198 | spin_lock_irqsave(&musb->lock, flags); |
yuzheng ma | 3067779 | 2012-08-15 16:11:40 +0800 | [diff] [blame] | 2199 | if (hep->hcpriv || !next_urb(qh)) { |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2200 | /* some concurrent activity submitted another urb to hep... |
| 2201 | * odd, rare, error prone, but legal. |
| 2202 | */ |
| 2203 | kfree(qh); |
Dan Carpenter | 714bc5e | 2010-03-25 13:14:27 +0200 | [diff] [blame] | 2204 | qh = NULL; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2205 | ret = 0; |
| 2206 | } else |
| 2207 | ret = musb_schedule(musb, qh, |
| 2208 | epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK); |
| 2209 | |
| 2210 | if (ret == 0) { |
| 2211 | urb->hcpriv = qh; |
| 2212 | /* FIXME set urb->start_frame for iso/intr, it's tested in |
| 2213 | * musb_start_urb(), but otherwise only konicawc cares ... |
| 2214 | */ |
| 2215 | } |
| 2216 | spin_unlock_irqrestore(&musb->lock, flags); |
| 2217 | |
| 2218 | done: |
| 2219 | if (ret != 0) { |
Ajay Kumar Gupta | 2492e67 | 2008-09-11 11:53:21 +0300 | [diff] [blame] | 2220 | spin_lock_irqsave(&musb->lock, flags); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2221 | usb_hcd_unlink_urb_from_ep(hcd, urb); |
Ajay Kumar Gupta | 2492e67 | 2008-09-11 11:53:21 +0300 | [diff] [blame] | 2222 | spin_unlock_irqrestore(&musb->lock, flags); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2223 | kfree(qh); |
| 2224 | } |
| 2225 | return ret; |
| 2226 | } |
| 2227 | |
| 2228 | |
| 2229 | /* |
| 2230 | * abort a transfer that's at the head of a hardware queue. |
| 2231 | * called with controller locked, irqs blocked |
| 2232 | * that hardware queue advances to the next transfer, unless prevented |
| 2233 | */ |
Sergei Shtylyov | 81ec4e4 | 2009-03-27 12:57:50 -0700 | [diff] [blame] | 2234 | static int musb_cleanup_urb(struct urb *urb, struct musb_qh *qh) |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2235 | { |
| 2236 | struct musb_hw_ep *ep = qh->hw_ep; |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 2237 | struct musb *musb = ep->musb; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2238 | void __iomem *epio = ep->regs; |
| 2239 | unsigned hw_end = ep->epnum; |
| 2240 | void __iomem *regs = ep->musb->mregs; |
Sergei Shtylyov | 81ec4e4 | 2009-03-27 12:57:50 -0700 | [diff] [blame] | 2241 | int is_in = usb_pipein(urb->pipe); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2242 | int status = 0; |
Sergei Shtylyov | 81ec4e4 | 2009-03-27 12:57:50 -0700 | [diff] [blame] | 2243 | u16 csr; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2244 | |
| 2245 | musb_ep_select(regs, hw_end); |
| 2246 | |
| 2247 | if (is_dma_capable()) { |
| 2248 | struct dma_channel *dma; |
| 2249 | |
| 2250 | dma = is_in ? ep->rx_channel : ep->tx_channel; |
| 2251 | if (dma) { |
| 2252 | status = ep->musb->dma_controller->channel_abort(dma); |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 2253 | dev_dbg(musb->controller, |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2254 | "abort %cX%d DMA for urb %p --> %d\n", |
| 2255 | is_in ? 'R' : 'T', ep->epnum, |
| 2256 | urb, status); |
| 2257 | urb->actual_length += dma->actual_len; |
| 2258 | } |
| 2259 | } |
| 2260 | |
| 2261 | /* turn off DMA requests, discard state, stop polling ... */ |
Ajay Kumar Gupta | 692933b | 2012-03-14 17:33:35 +0530 | [diff] [blame] | 2262 | if (ep->epnum && is_in) { |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2263 | /* giveback saves bulk toggle */ |
| 2264 | csr = musb_h_flush_rxfifo(ep, 0); |
| 2265 | |
| 2266 | /* REVISIT we still get an irq; should likely clear the |
| 2267 | * endpoint's irq status here to avoid bogus irqs. |
| 2268 | * clearing that status is platform-specific... |
| 2269 | */ |
David Brownell | 78322c1 | 2009-03-26 17:38:30 -0700 | [diff] [blame] | 2270 | } else if (ep->epnum) { |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2271 | musb_h_tx_flush_fifo(ep); |
| 2272 | csr = musb_readw(epio, MUSB_TXCSR); |
| 2273 | csr &= ~(MUSB_TXCSR_AUTOSET |
| 2274 | | MUSB_TXCSR_DMAENAB |
| 2275 | | MUSB_TXCSR_H_RXSTALL |
| 2276 | | MUSB_TXCSR_H_NAKTIMEOUT |
| 2277 | | MUSB_TXCSR_H_ERROR |
| 2278 | | MUSB_TXCSR_TXPKTRDY); |
| 2279 | musb_writew(epio, MUSB_TXCSR, csr); |
| 2280 | /* REVISIT may need to clear FLUSHFIFO ... */ |
| 2281 | musb_writew(epio, MUSB_TXCSR, csr); |
| 2282 | /* flush cpu writebuffer */ |
| 2283 | csr = musb_readw(epio, MUSB_TXCSR); |
David Brownell | 78322c1 | 2009-03-26 17:38:30 -0700 | [diff] [blame] | 2284 | } else { |
| 2285 | musb_h_ep0_flush_fifo(ep); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2286 | } |
| 2287 | if (status == 0) |
| 2288 | musb_advance_schedule(ep->musb, urb, ep, is_in); |
| 2289 | return status; |
| 2290 | } |
| 2291 | |
| 2292 | static int musb_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status) |
| 2293 | { |
| 2294 | struct musb *musb = hcd_to_musb(hcd); |
| 2295 | struct musb_qh *qh; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2296 | unsigned long flags; |
Sergei Shtylyov | 22a0d6f | 2009-03-27 12:56:26 -0700 | [diff] [blame] | 2297 | int is_in = usb_pipein(urb->pipe); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2298 | int ret; |
| 2299 | |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 2300 | dev_dbg(musb->controller, "urb=%p, dev%d ep%d%s\n", urb, |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2301 | usb_pipedevice(urb->pipe), |
| 2302 | usb_pipeendpoint(urb->pipe), |
Sergei Shtylyov | 22a0d6f | 2009-03-27 12:56:26 -0700 | [diff] [blame] | 2303 | is_in ? "in" : "out"); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2304 | |
| 2305 | spin_lock_irqsave(&musb->lock, flags); |
| 2306 | ret = usb_hcd_check_unlink_urb(hcd, urb, status); |
| 2307 | if (ret) |
| 2308 | goto done; |
| 2309 | |
| 2310 | qh = urb->hcpriv; |
| 2311 | if (!qh) |
| 2312 | goto done; |
| 2313 | |
Sergei Shtylyov | 22a0d6f | 2009-03-27 12:56:26 -0700 | [diff] [blame] | 2314 | /* |
| 2315 | * Any URB not actively programmed into endpoint hardware can be |
Sergei Shtylyov | a2fd814 | 2009-02-21 15:30:45 -0800 | [diff] [blame] | 2316 | * immediately given back; that's any URB not at the head of an |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2317 | * endpoint queue, unless someday we get real DMA queues. And even |
Sergei Shtylyov | a2fd814 | 2009-02-21 15:30:45 -0800 | [diff] [blame] | 2318 | * if it's at the head, it might not be known to the hardware... |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2319 | * |
Sergei Shtylyov | 22a0d6f | 2009-03-27 12:56:26 -0700 | [diff] [blame] | 2320 | * Otherwise abort current transfer, pending DMA, etc.; urb->status |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2321 | * has already been updated. This is a synchronous abort; it'd be |
| 2322 | * OK to hold off until after some IRQ, though. |
Sergei Shtylyov | 22a0d6f | 2009-03-27 12:56:26 -0700 | [diff] [blame] | 2323 | * |
| 2324 | * NOTE: qh is invalid unless !list_empty(&hep->urb_list) |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2325 | */ |
Sergei Shtylyov | 22a0d6f | 2009-03-27 12:56:26 -0700 | [diff] [blame] | 2326 | if (!qh->is_ready |
| 2327 | || urb->urb_list.prev != &qh->hep->urb_list |
| 2328 | || musb_ep_get_qh(qh->hw_ep, is_in) != qh) { |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2329 | int ready = qh->is_ready; |
| 2330 | |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2331 | qh->is_ready = 0; |
Sergei Shtylyov | c9cd06b | 2009-03-27 12:58:31 -0700 | [diff] [blame] | 2332 | musb_giveback(musb, urb, 0); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2333 | qh->is_ready = ready; |
Sergei Shtylyov | a2fd814 | 2009-02-21 15:30:45 -0800 | [diff] [blame] | 2334 | |
| 2335 | /* If nothing else (usually musb_giveback) is using it |
| 2336 | * and its URB list has emptied, recycle this qh. |
| 2337 | */ |
| 2338 | if (ready && list_empty(&qh->hep->urb_list)) { |
| 2339 | qh->hep->hcpriv = NULL; |
| 2340 | list_del(&qh->ring); |
| 2341 | kfree(qh); |
| 2342 | } |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2343 | } else |
Sergei Shtylyov | 81ec4e4 | 2009-03-27 12:57:50 -0700 | [diff] [blame] | 2344 | ret = musb_cleanup_urb(urb, qh); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2345 | done: |
| 2346 | spin_unlock_irqrestore(&musb->lock, flags); |
| 2347 | return ret; |
| 2348 | } |
| 2349 | |
| 2350 | /* disable an endpoint */ |
| 2351 | static void |
| 2352 | musb_h_disable(struct usb_hcd *hcd, struct usb_host_endpoint *hep) |
| 2353 | { |
Sergei Shtylyov | 22a0d6f | 2009-03-27 12:56:26 -0700 | [diff] [blame] | 2354 | u8 is_in = hep->desc.bEndpointAddress & USB_DIR_IN; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2355 | unsigned long flags; |
| 2356 | struct musb *musb = hcd_to_musb(hcd); |
Sergei Shtylyov | dc61d23 | 2009-02-21 15:31:01 -0800 | [diff] [blame] | 2357 | struct musb_qh *qh; |
| 2358 | struct urb *urb; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2359 | |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2360 | spin_lock_irqsave(&musb->lock, flags); |
| 2361 | |
Sergei Shtylyov | dc61d23 | 2009-02-21 15:31:01 -0800 | [diff] [blame] | 2362 | qh = hep->hcpriv; |
| 2363 | if (qh == NULL) |
| 2364 | goto exit; |
| 2365 | |
Sergei Shtylyov | 22a0d6f | 2009-03-27 12:56:26 -0700 | [diff] [blame] | 2366 | /* NOTE: qh is invalid unless !list_empty(&hep->urb_list) */ |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2367 | |
Sergei Shtylyov | 22a0d6f | 2009-03-27 12:56:26 -0700 | [diff] [blame] | 2368 | /* Kick the first URB off the hardware, if needed */ |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2369 | qh->is_ready = 0; |
Sergei Shtylyov | 22a0d6f | 2009-03-27 12:56:26 -0700 | [diff] [blame] | 2370 | if (musb_ep_get_qh(qh->hw_ep, is_in) == qh) { |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2371 | urb = next_urb(qh); |
| 2372 | |
| 2373 | /* make software (then hardware) stop ASAP */ |
| 2374 | if (!urb->unlinked) |
| 2375 | urb->status = -ESHUTDOWN; |
| 2376 | |
| 2377 | /* cleanup */ |
Sergei Shtylyov | 81ec4e4 | 2009-03-27 12:57:50 -0700 | [diff] [blame] | 2378 | musb_cleanup_urb(urb, qh); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2379 | |
Sergei Shtylyov | dc61d23 | 2009-02-21 15:31:01 -0800 | [diff] [blame] | 2380 | /* Then nuke all the others ... and advance the |
| 2381 | * queue on hw_ep (e.g. bulk ring) when we're done. |
| 2382 | */ |
| 2383 | while (!list_empty(&hep->urb_list)) { |
| 2384 | urb = next_urb(qh); |
| 2385 | urb->status = -ESHUTDOWN; |
| 2386 | musb_advance_schedule(musb, urb, qh->hw_ep, is_in); |
| 2387 | } |
| 2388 | } else { |
| 2389 | /* Just empty the queue; the hardware is busy with |
| 2390 | * other transfers, and since !qh->is_ready nothing |
| 2391 | * will activate any of these as it advances. |
| 2392 | */ |
| 2393 | while (!list_empty(&hep->urb_list)) |
Sergei Shtylyov | c9cd06b | 2009-03-27 12:58:31 -0700 | [diff] [blame] | 2394 | musb_giveback(musb, next_urb(qh), -ESHUTDOWN); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2395 | |
Sergei Shtylyov | dc61d23 | 2009-02-21 15:31:01 -0800 | [diff] [blame] | 2396 | hep->hcpriv = NULL; |
| 2397 | list_del(&qh->ring); |
| 2398 | kfree(qh); |
| 2399 | } |
| 2400 | exit: |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2401 | spin_unlock_irqrestore(&musb->lock, flags); |
| 2402 | } |
| 2403 | |
| 2404 | static int musb_h_get_frame_number(struct usb_hcd *hcd) |
| 2405 | { |
| 2406 | struct musb *musb = hcd_to_musb(hcd); |
| 2407 | |
| 2408 | return musb_readw(musb->mregs, MUSB_FRAME); |
| 2409 | } |
| 2410 | |
| 2411 | static int musb_h_start(struct usb_hcd *hcd) |
| 2412 | { |
| 2413 | struct musb *musb = hcd_to_musb(hcd); |
| 2414 | |
| 2415 | /* NOTE: musb_start() is called when the hub driver turns |
| 2416 | * on port power, or when (OTG) peripheral starts. |
| 2417 | */ |
| 2418 | hcd->state = HC_STATE_RUNNING; |
| 2419 | musb->port1_status = 0; |
| 2420 | return 0; |
| 2421 | } |
| 2422 | |
| 2423 | static void musb_h_stop(struct usb_hcd *hcd) |
| 2424 | { |
| 2425 | musb_stop(hcd_to_musb(hcd)); |
| 2426 | hcd->state = HC_STATE_HALT; |
| 2427 | } |
| 2428 | |
| 2429 | static int musb_bus_suspend(struct usb_hcd *hcd) |
| 2430 | { |
| 2431 | struct musb *musb = hcd_to_musb(hcd); |
David Brownell | 89368d3 | 2009-07-01 03:36:16 -0700 | [diff] [blame] | 2432 | u8 devctl; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2433 | |
David Brownell | 89368d3 | 2009-07-01 03:36:16 -0700 | [diff] [blame] | 2434 | if (!is_host_active(musb)) |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2435 | return 0; |
| 2436 | |
David Brownell | 89368d3 | 2009-07-01 03:36:16 -0700 | [diff] [blame] | 2437 | switch (musb->xceiv->state) { |
| 2438 | case OTG_STATE_A_SUSPEND: |
| 2439 | return 0; |
| 2440 | case OTG_STATE_A_WAIT_VRISE: |
| 2441 | /* ID could be grounded even if there's no device |
| 2442 | * on the other end of the cable. NOTE that the |
| 2443 | * A_WAIT_VRISE timers are messy with MUSB... |
| 2444 | */ |
| 2445 | devctl = musb_readb(musb->mregs, MUSB_DEVCTL); |
| 2446 | if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS) |
| 2447 | musb->xceiv->state = OTG_STATE_A_WAIT_BCON; |
| 2448 | break; |
| 2449 | default: |
| 2450 | break; |
| 2451 | } |
| 2452 | |
| 2453 | if (musb->is_active) { |
| 2454 | WARNING("trying to suspend as %s while active\n", |
Felipe Balbi | 42c0bf1 | 2013-03-07 10:39:57 +0200 | [diff] [blame] | 2455 | usb_otg_state_string(musb->xceiv->state)); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2456 | return -EBUSY; |
| 2457 | } else |
| 2458 | return 0; |
| 2459 | } |
| 2460 | |
| 2461 | static int musb_bus_resume(struct usb_hcd *hcd) |
| 2462 | { |
| 2463 | /* resuming child port does the work */ |
| 2464 | return 0; |
| 2465 | } |
| 2466 | |
Ruslan Bilovol | 8408fd1 | 2013-03-29 19:15:21 +0200 | [diff] [blame] | 2467 | |
| 2468 | #ifndef CONFIG_MUSB_PIO_ONLY |
| 2469 | |
| 2470 | #define MUSB_USB_DMA_ALIGN 4 |
| 2471 | |
| 2472 | struct musb_temp_buffer { |
| 2473 | void *kmalloc_ptr; |
| 2474 | void *old_xfer_buffer; |
| 2475 | u8 data[0]; |
| 2476 | }; |
| 2477 | |
| 2478 | static void musb_free_temp_buffer(struct urb *urb) |
| 2479 | { |
| 2480 | enum dma_data_direction dir; |
| 2481 | struct musb_temp_buffer *temp; |
| 2482 | |
| 2483 | if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER)) |
| 2484 | return; |
| 2485 | |
| 2486 | dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE; |
| 2487 | |
| 2488 | temp = container_of(urb->transfer_buffer, struct musb_temp_buffer, |
| 2489 | data); |
| 2490 | |
| 2491 | if (dir == DMA_FROM_DEVICE) { |
| 2492 | memcpy(temp->old_xfer_buffer, temp->data, |
| 2493 | urb->transfer_buffer_length); |
| 2494 | } |
| 2495 | urb->transfer_buffer = temp->old_xfer_buffer; |
| 2496 | kfree(temp->kmalloc_ptr); |
| 2497 | |
| 2498 | urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER; |
| 2499 | } |
| 2500 | |
| 2501 | static int musb_alloc_temp_buffer(struct urb *urb, gfp_t mem_flags) |
| 2502 | { |
| 2503 | enum dma_data_direction dir; |
| 2504 | struct musb_temp_buffer *temp; |
| 2505 | void *kmalloc_ptr; |
| 2506 | size_t kmalloc_size; |
| 2507 | |
| 2508 | if (urb->num_sgs || urb->sg || |
| 2509 | urb->transfer_buffer_length == 0 || |
| 2510 | !((uintptr_t)urb->transfer_buffer & (MUSB_USB_DMA_ALIGN - 1))) |
| 2511 | return 0; |
| 2512 | |
| 2513 | dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE; |
| 2514 | |
| 2515 | /* Allocate a buffer with enough padding for alignment */ |
| 2516 | kmalloc_size = urb->transfer_buffer_length + |
| 2517 | sizeof(struct musb_temp_buffer) + MUSB_USB_DMA_ALIGN - 1; |
| 2518 | |
| 2519 | kmalloc_ptr = kmalloc(kmalloc_size, mem_flags); |
| 2520 | if (!kmalloc_ptr) |
| 2521 | return -ENOMEM; |
| 2522 | |
| 2523 | /* Position our struct temp_buffer such that data is aligned */ |
| 2524 | temp = PTR_ALIGN(kmalloc_ptr, MUSB_USB_DMA_ALIGN); |
| 2525 | |
| 2526 | |
| 2527 | temp->kmalloc_ptr = kmalloc_ptr; |
| 2528 | temp->old_xfer_buffer = urb->transfer_buffer; |
| 2529 | if (dir == DMA_TO_DEVICE) |
| 2530 | memcpy(temp->data, urb->transfer_buffer, |
| 2531 | urb->transfer_buffer_length); |
| 2532 | urb->transfer_buffer = temp->data; |
| 2533 | |
| 2534 | urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER; |
| 2535 | |
| 2536 | return 0; |
| 2537 | } |
| 2538 | |
| 2539 | static int musb_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb, |
| 2540 | gfp_t mem_flags) |
| 2541 | { |
| 2542 | struct musb *musb = hcd_to_musb(hcd); |
| 2543 | int ret; |
| 2544 | |
| 2545 | /* |
| 2546 | * The DMA engine in RTL1.8 and above cannot handle |
| 2547 | * DMA addresses that are not aligned to a 4 byte boundary. |
| 2548 | * For such engine implemented (un)map_urb_for_dma hooks. |
| 2549 | * Do not use these hooks for RTL<1.8 |
| 2550 | */ |
| 2551 | if (musb->hwvers < MUSB_HWVERS_1800) |
| 2552 | return usb_hcd_map_urb_for_dma(hcd, urb, mem_flags); |
| 2553 | |
| 2554 | ret = musb_alloc_temp_buffer(urb, mem_flags); |
| 2555 | if (ret) |
| 2556 | return ret; |
| 2557 | |
| 2558 | ret = usb_hcd_map_urb_for_dma(hcd, urb, mem_flags); |
| 2559 | if (ret) |
| 2560 | musb_free_temp_buffer(urb); |
| 2561 | |
| 2562 | return ret; |
| 2563 | } |
| 2564 | |
| 2565 | static void musb_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb) |
| 2566 | { |
| 2567 | struct musb *musb = hcd_to_musb(hcd); |
| 2568 | |
| 2569 | usb_hcd_unmap_urb_for_dma(hcd, urb); |
| 2570 | |
| 2571 | /* Do not use this hook for RTL<1.8 (see description above) */ |
| 2572 | if (musb->hwvers < MUSB_HWVERS_1800) |
| 2573 | return; |
| 2574 | |
| 2575 | musb_free_temp_buffer(urb); |
| 2576 | } |
| 2577 | #endif /* !CONFIG_MUSB_PIO_ONLY */ |
| 2578 | |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2579 | const struct hc_driver musb_hc_driver = { |
| 2580 | .description = "musb-hcd", |
| 2581 | .product_desc = "MUSB HDRC host driver", |
| 2582 | .hcd_priv_size = sizeof(struct musb), |
| 2583 | .flags = HCD_USB2 | HCD_MEMORY, |
| 2584 | |
| 2585 | /* not using irq handler or reset hooks from usbcore, since |
| 2586 | * those must be shared with peripheral code for OTG configs |
| 2587 | */ |
| 2588 | |
| 2589 | .start = musb_h_start, |
| 2590 | .stop = musb_h_stop, |
| 2591 | |
| 2592 | .get_frame_number = musb_h_get_frame_number, |
| 2593 | |
| 2594 | .urb_enqueue = musb_urb_enqueue, |
| 2595 | .urb_dequeue = musb_urb_dequeue, |
| 2596 | .endpoint_disable = musb_h_disable, |
| 2597 | |
Ruslan Bilovol | 8408fd1 | 2013-03-29 19:15:21 +0200 | [diff] [blame] | 2598 | #ifndef CONFIG_MUSB_PIO_ONLY |
| 2599 | .map_urb_for_dma = musb_map_urb_for_dma, |
| 2600 | .unmap_urb_for_dma = musb_unmap_urb_for_dma, |
| 2601 | #endif |
| 2602 | |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 2603 | .hub_status_data = musb_hub_status_data, |
| 2604 | .hub_control = musb_hub_control, |
| 2605 | .bus_suspend = musb_bus_suspend, |
| 2606 | .bus_resume = musb_bus_resume, |
| 2607 | /* .start_port_reset = NULL, */ |
| 2608 | /* .hub_irq_enable = NULL, */ |
| 2609 | }; |
Daniel Mack | 0b3eba4 | 2013-04-10 21:55:42 +0200 | [diff] [blame] | 2610 | |
| 2611 | void musb_host_resume_root_hub(struct musb *musb) |
| 2612 | { |
| 2613 | usb_hcd_resume_root_hub(musb_to_hcd(musb)); |
| 2614 | } |
| 2615 | |
| 2616 | void musb_host_poke_root_hub(struct musb *musb) |
| 2617 | { |
| 2618 | MUSB_HST_MODE(musb); |
| 2619 | if (musb_to_hcd(musb)->status_urb) |
| 2620 | usb_hcd_poll_rh_status(musb_to_hcd(musb)); |
| 2621 | else |
| 2622 | usb_hcd_resume_root_hub(musb_to_hcd(musb)); |
| 2623 | } |