Commit | Line | Data |
---|---|---|
550a7375 FB |
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 | |
c7bbc056 | 7 | * Copyright (C) 2008-2009 MontaVista Software, Inc. <source@mvista.com> |
550a7375 FB |
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> | |
496dda70 | 44 | #include <linux/dma-mapping.h> |
550a7375 FB |
45 | |
46 | #include "musb_core.h" | |
47 | #include "musb_host.h" | |
48 | ||
550a7375 FB |
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 | * | |
1e0320f0 AKG |
68 | * - [23-feb-2009] minimal traffic scheduling to avoid bulk RX packet |
69 | * starvation ... nothing yet for TX, interrupt, or bulk. | |
550a7375 FB |
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). | |
550a7375 | 89 | * (Yes, bulk _could_ use more of the endpoints than that, and would even |
1e0320f0 | 90 | * benefit from it.) |
550a7375 FB |
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, | |
6b6e9710 SS |
100 | struct urb *urb, int is_out, |
101 | u8 *buf, u32 offset, u32 len); | |
550a7375 FB |
102 | |
103 | /* | |
104 | * Clear TX fifo. Needed to avoid BABBLE errors. | |
105 | */ | |
c767c1c6 | 106 | static void musb_h_tx_flush_fifo(struct musb_hw_ep *ep) |
550a7375 | 107 | { |
5c8a86e1 | 108 | struct musb *musb = ep->musb; |
550a7375 FB |
109 | void __iomem *epio = ep->regs; |
110 | u16 csr; | |
bb1c9ef1 | 111 | u16 lastcsr = 0; |
550a7375 FB |
112 | int retries = 1000; |
113 | ||
114 | csr = musb_readw(epio, MUSB_TXCSR); | |
115 | while (csr & MUSB_TXCSR_FIFONOTEMPTY) { | |
bb1c9ef1 | 116 | if (csr != lastcsr) |
5c8a86e1 | 117 | dev_dbg(musb->controller, "Host TX FIFONOTEMPTY csr: %02x\n", csr); |
bb1c9ef1 | 118 | lastcsr = csr; |
550a7375 FB |
119 | csr |= MUSB_TXCSR_FLUSHFIFO; |
120 | musb_writew(epio, MUSB_TXCSR, csr); | |
121 | csr = musb_readw(epio, MUSB_TXCSR); | |
bb1c9ef1 DB |
122 | if (WARN(retries-- < 1, |
123 | "Could not flush host TX%d fifo: csr: %04x\n", | |
124 | ep->epnum, csr)) | |
550a7375 | 125 | return; |
550a7375 FB |
126 | mdelay(1); |
127 | } | |
128 | } | |
129 | ||
78322c1a DB |
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 | ||
550a7375 FB |
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 | ||
c7bbc056 | 173 | static inline void musb_h_tx_dma_start(struct musb_hw_ep *ep) |
550a7375 FB |
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; | |
c7bbc056 SS |
180 | if (is_cppi_enabled()) |
181 | txcsr |= MUSB_TXCSR_DMAMODE; | |
550a7375 FB |
182 | musb_writew(ep->regs, MUSB_TXCSR, txcsr); |
183 | } | |
184 | ||
3e5c6dc7 SS |
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 | ||
550a7375 FB |
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; | |
550a7375 FB |
209 | void __iomem *mbase = musb->mregs; |
210 | struct urb *urb = next_urb(qh); | |
6b6e9710 SS |
211 | void *buf = urb->transfer_buffer; |
212 | u32 offset = 0; | |
550a7375 FB |
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; | |
550a7375 FB |
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; | |
6b6e9710 | 234 | offset = urb->iso_frame_desc[0].offset; |
550a7375 FB |
235 | len = urb->iso_frame_desc[0].length; |
236 | break; | |
237 | default: /* bulk, interrupt */ | |
1e0320f0 AKG |
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; | |
550a7375 FB |
241 | } |
242 | ||
5c8a86e1 | 243 | dev_dbg(musb->controller, "qh %p urb %p dev%d ep%d%s%s, hw_ep %d, %p/%d\n", |
550a7375 FB |
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; }), | |
6b6e9710 | 252 | epnum, buf + offset, len); |
550a7375 FB |
253 | |
254 | /* Configure endpoint */ | |
3e5c6dc7 | 255 | musb_ep_set_qh(hw_ep, is_in, qh); |
6b6e9710 | 256 | musb_ep_program(musb, epnum, urb, !is_in, buf, offset, len); |
550a7375 FB |
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: | |
5c8a86e1 | 266 | dev_dbg(musb->controller, "check whether there's still time for periodic Tx\n"); |
550a7375 FB |
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 */ | |
5c8a86e1 | 281 | dev_dbg(musb->controller, "SOF for %d\n", epnum); |
550a7375 FB |
282 | #if 1 /* ifndef CONFIG_ARCH_DAVINCI */ |
283 | musb_writeb(mbase, MUSB_INTRUSBE, 0xff); | |
284 | #endif | |
285 | } | |
286 | break; | |
287 | default: | |
288 | start: | |
5c8a86e1 | 289 | dev_dbg(musb->controller, "Start TX%d %s\n", epnum, |
550a7375 FB |
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()) | |
c7bbc056 | 295 | musb_h_tx_dma_start(hw_ep); |
550a7375 FB |
296 | } |
297 | } | |
298 | ||
c9cd06b3 SS |
299 | /* Context: caller owns controller lock, IRQs are blocked */ |
300 | static void musb_giveback(struct musb *musb, struct urb *urb, int status) | |
550a7375 FB |
301 | __releases(musb->lock) |
302 | __acquires(musb->lock) | |
303 | { | |
5c8a86e1 | 304 | dev_dbg(musb->controller, |
bb1c9ef1 DB |
305 | "complete %p %pF (%d), dev%d ep%d%s, %d/%d\n", |
306 | urb, urb->complete, status, | |
550a7375 FB |
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 | ||
2492e674 | 313 | usb_hcd_unlink_urb_from_ep(musb_to_hcd(musb), urb); |
550a7375 FB |
314 | spin_unlock(&musb->lock); |
315 | usb_hcd_giveback_urb(musb_to_hcd(musb), urb, status); | |
316 | spin_lock(&musb->lock); | |
317 | } | |
318 | ||
846099a6 SS |
319 | /* For bulk/interrupt endpoints only */ |
320 | static inline void musb_save_toggle(struct musb_qh *qh, int is_in, | |
321 | struct urb *urb) | |
550a7375 | 322 | { |
846099a6 | 323 | void __iomem *epio = qh->hw_ep->regs; |
550a7375 | 324 | u16 csr; |
550a7375 | 325 | |
846099a6 SS |
326 | /* |
327 | * FIXME: the current Mentor DMA code seems to have | |
550a7375 FB |
328 | * problems getting toggle correct. |
329 | */ | |
330 | ||
846099a6 SS |
331 | if (is_in) |
332 | csr = musb_readw(epio, MUSB_RXCSR) & MUSB_RXCSR_H_DATATOGGLE; | |
550a7375 | 333 | else |
846099a6 | 334 | csr = musb_readw(epio, MUSB_TXCSR) & MUSB_TXCSR_H_DATATOGGLE; |
550a7375 | 335 | |
846099a6 | 336 | usb_settoggle(urb->dev, qh->epnum, !is_in, csr ? 1 : 0); |
550a7375 FB |
337 | } |
338 | ||
c9cd06b3 SS |
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) | |
550a7375 | 348 | { |
c9cd06b3 | 349 | struct musb_qh *qh = musb_ep_get_qh(hw_ep, is_in); |
550a7375 | 350 | struct musb_hw_ep *ep = qh->hw_ep; |
550a7375 | 351 | int ready = qh->is_ready; |
c9cd06b3 SS |
352 | int status; |
353 | ||
354 | status = (urb->status == -EINPROGRESS) ? 0 : urb->status; | |
550a7375 | 355 | |
550a7375 FB |
356 | /* save toggle eagerly, for paranoia */ |
357 | switch (qh->type) { | |
358 | case USB_ENDPOINT_XFER_BULK: | |
359 | case USB_ENDPOINT_XFER_INT: | |
846099a6 | 360 | musb_save_toggle(qh, is_in, urb); |
550a7375 FB |
361 | break; |
362 | case USB_ENDPOINT_XFER_ISOC: | |
1fe975f9 | 363 | if (status == 0 && urb->error_count) |
550a7375 FB |
364 | status = -EXDEV; |
365 | break; | |
366 | } | |
367 | ||
550a7375 | 368 | qh->is_ready = 0; |
c9cd06b3 | 369 | musb_giveback(musb, urb, status); |
550a7375 FB |
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; | |
8c778db9 | 377 | struct dma_controller *dma = musb->dma_controller; |
550a7375 | 378 | |
8c778db9 | 379 | if (is_in) { |
550a7375 | 380 | ep->rx_reinit = 1; |
8c778db9 AKG |
381 | if (ep->rx_channel) { |
382 | dma->channel_release(ep->rx_channel); | |
383 | ep->rx_channel = NULL; | |
384 | } | |
385 | } else { | |
550a7375 | 386 | ep->tx_reinit = 1; |
8c778db9 AKG |
387 | if (ep->tx_channel) { |
388 | dma->channel_release(ep->tx_channel); | |
389 | ep->tx_channel = NULL; | |
390 | } | |
391 | } | |
550a7375 | 392 | |
3e5c6dc7 SS |
393 | /* Clobber old pointers to this qh */ |
394 | musb_ep_set_qh(ep, is_in, NULL); | |
550a7375 FB |
395 | qh->hep->hcpriv = NULL; |
396 | ||
397 | switch (qh->type) { | |
398 | ||
23d15e07 AKG |
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 | ||
550a7375 FB |
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 | */ | |
550a7375 FB |
418 | kfree(qh); |
419 | qh = NULL; | |
420 | break; | |
550a7375 FB |
421 | } |
422 | } | |
550a7375 | 423 | |
a2fd814e | 424 | if (qh != NULL && qh->is_ready) { |
5c8a86e1 | 425 | dev_dbg(musb->controller, "... next ep%d %cX urb %p\n", |
c9cd06b3 | 426 | hw_ep->epnum, is_in ? 'R' : 'T', next_urb(qh)); |
550a7375 FB |
427 | musb_start_urb(musb, is_in, qh); |
428 | } | |
429 | } | |
430 | ||
c767c1c6 | 431 | static u16 musb_h_flush_rxfifo(struct musb_hw_ep *hw_ep, u16 csr) |
550a7375 FB |
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); | |
5c8a86e1 | 470 | dev_dbg(musb->controller, "RX%d count %d, buffer %p len %d/%d\n", epnum, rx_count, |
550a7375 FB |
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 | } | |
5c8a86e1 | 492 | dev_dbg(musb->controller, "** OVERFLOW %d into %d\n", rx_count, length); |
550a7375 FB |
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; | |
5c8a86e1 | 510 | dev_dbg(musb->controller, "** OVERFLOW %d into %d\n", rx_count, length); |
550a7375 FB |
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); | |
b6e434a5 | 569 | csr = musb_readw(ep->regs, MUSB_TXCSR); |
550a7375 | 570 | musb_writew(ep->regs, MUSB_TXCSR, |
b6e434a5 | 571 | csr | MUSB_TXCSR_FRCDATATOG); |
550a7375 | 572 | } |
b6e434a5 SS |
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); | |
550a7375 FB |
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) { | |
c6cf8b00 BW |
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 | ||
550a7375 FB |
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 */ | |
9f445cb2 CC |
605 | /* Set RXMAXP with the FIFO size of the endpoint |
606 | * to disable double buffer mode. | |
607 | */ | |
06624818 | 608 | if (musb->double_buffer_not_ok) |
9f445cb2 CC |
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)); | |
550a7375 FB |
613 | |
614 | ep->rx_reinit = 0; | |
615 | } | |
616 | ||
6b6e9710 SS |
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; | |
a483d706 AKG |
634 | csr |= MUSB_TXCSR_DMAMODE | MUSB_TXCSR_DMAENAB; |
635 | /* autoset shouldn't be set in high bandwidth */ | |
f2786281 | 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))) | |
a483d706 | 647 | csr |= MUSB_TXCSR_AUTOSET; |
6b6e9710 SS |
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 | ||
4c647338 SS |
670 | /* |
671 | * Ensure the data reaches to main memory before starting | |
672 | * DMA transfer | |
673 | */ | |
674 | wmb(); | |
675 | ||
6b6e9710 SS |
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 | } | |
550a7375 FB |
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, | |
6b6e9710 SS |
694 | struct urb *urb, int is_out, |
695 | u8 *buf, u32 offset, u32 len) | |
550a7375 FB |
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; | |
3e5c6dc7 SS |
703 | struct musb_qh *qh = musb_ep_get_qh(hw_ep, !is_out); |
704 | u16 packet_sz = qh->maxpacket; | |
3132122c AKG |
705 | u8 use_dma = 1; |
706 | u16 csr; | |
550a7375 | 707 | |
5c8a86e1 | 708 | dev_dbg(musb->controller, "%s hw%d urb %p spd%d dev%d ep%d%s " |
550a7375 FB |
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 | ||
3132122c AKG |
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 | ||
550a7375 FB |
726 | /* candidate for DMA? */ |
727 | dma_controller = musb->dma_controller; | |
3132122c | 728 | if (use_dma && is_dma_capable() && epnum && dma_controller) { |
550a7375 FB |
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 */ | |
b18d26f6 | 752 | int_txe = musb->intrtxe; |
550a7375 FB |
753 | musb_writew(mbase, MUSB_INTRTXE, int_txe & ~(1 << epnum)); |
754 | ||
755 | /* general endpoint setup */ | |
756 | if (epnum) { | |
550a7375 | 757 | /* flush all old state, set default */ |
a70b8442 | 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); | |
b6e434a5 SS |
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 | */ | |
550a7375 | 771 | csr &= ~(MUSB_TXCSR_H_NAKTIMEOUT |
b6e434a5 SS |
772 | | MUSB_TXCSR_AUTOSET |
773 | | MUSB_TXCSR_DMAENAB | |
550a7375 FB |
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 | ||
a70b8442 | 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 | } | |
550a7375 | 788 | |
550a7375 FB |
789 | musb_writew(epio, MUSB_TXCSR, csr); |
790 | /* REVISIT may need to clear FLUSHFIFO ... */ | |
b6e434a5 | 791 | csr &= ~MUSB_TXCSR_DMAMODE; |
550a7375 FB |
792 | musb_writew(epio, MUSB_TXCSR, csr); |
793 | csr = musb_readw(epio, MUSB_TXCSR); | |
794 | } else { | |
795 | /* endpoint 0: just flush */ | |
78322c1a | 796 | musb_h_ep0_flush_fifo(hw_ep); |
550a7375 FB |
797 | } |
798 | ||
799 | /* target addr and (for multipoint) hub addr/port */ | |
800 | if (musb->is_multipoint) { | |
c6cf8b00 BW |
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); | |
550a7375 FB |
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); | |
f2786281 | 811 | if (musb->double_buffer_not_ok) { |
550a7375 | 812 | musb_writew(epio, MUSB_TXMAXP, |
06624818 | 813 | hw_ep->max_packet_sz_tx); |
f2786281 | 814 | } else if (can_bulk_split(musb, qh->type)) { |
815 | qh->hb_mult = hw_ep->max_packet_sz_tx | |
816 | / packet_sz; | |
ccc080c7 | 817 | musb_writew(epio, MUSB_TXMAXP, packet_sz |
f2786281 | 818 | | ((qh->hb_mult) - 1) << 11); |
819 | } else { | |
550a7375 | 820 | musb_writew(epio, MUSB_TXMAXP, |
06624818 FB |
821 | qh->maxpacket | |
822 | ((qh->hb_mult - 1) << 11)); | |
f2786281 | 823 | } |
550a7375 FB |
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 | ||
6b6e9710 SS |
838 | if (dma_channel && musb_tx_dma_program(dma_controller, |
839 | hw_ep, qh, urb, offset, len)) | |
840 | load_count = 0; | |
550a7375 FB |
841 | |
842 | if (load_count) { | |
550a7375 FB |
843 | /* PIO to load FIFO */ |
844 | qh->segsize = load_count; | |
8e8a5516 VS |
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); | |
550a7375 | 865 | } |
8e8a5516 | 866 | finish: |
550a7375 FB |
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) { | |
c51e36dc SS |
902 | /* Candidate for DMA */ |
903 | dma_channel->actual_len = 0L; | |
904 | qh->segsize = len; | |
905 | ||
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); | |
909 | ||
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; | |
550a7375 FB |
924 | } |
925 | ||
926 | csr |= MUSB_RXCSR_H_REQPKT; | |
5c8a86e1 | 927 | dev_dbg(musb->controller, "RXCSR%d := %04x\n", epnum, csr); |
550a7375 FB |
928 | musb_writew(hw_ep->regs, MUSB_RXCSR, csr); |
929 | csr = musb_readw(hw_ep->regs, MUSB_RXCSR); | |
930 | } | |
931 | } | |
932 | ||
f283862f AKG |
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 | } | |
550a7375 FB |
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; | |
3ecdb9ac SS |
1017 | fifo_count = min_t(size_t, len, urb->transfer_buffer_length - |
1018 | urb->actual_length); | |
550a7375 FB |
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) { | |
5c8a86e1 | 1037 | dev_dbg(musb->controller, "start no-DATA\n"); |
550a7375 FB |
1038 | break; |
1039 | } else if (request->bRequestType & USB_DIR_IN) { | |
5c8a86e1 | 1040 | dev_dbg(musb->controller, "start IN-DATA\n"); |
550a7375 FB |
1041 | musb->ep0_stage = MUSB_EP0_IN; |
1042 | more = true; | |
1043 | break; | |
1044 | } else { | |
5c8a86e1 | 1045 | dev_dbg(musb->controller, "start OUT-DATA\n"); |
550a7375 FB |
1046 | musb->ep0_stage = MUSB_EP0_OUT; |
1047 | more = true; | |
1048 | } | |
1049 | /* FALLTHROUGH */ | |
1050 | case MUSB_EP0_OUT: | |
3ecdb9ac SS |
1051 | fifo_count = min_t(size_t, qh->maxpacket, |
1052 | urb->transfer_buffer_length - | |
1053 | urb->actual_length); | |
550a7375 FB |
1054 | if (fifo_count) { |
1055 | fifo_dest = (u8 *) (urb->transfer_buffer | |
1056 | + urb->actual_length); | |
5c8a86e1 | 1057 | dev_dbg(musb->controller, "Sending %d byte%s to ep0 fifo %p\n", |
bb1c9ef1 DB |
1058 | fifo_count, |
1059 | (fifo_count == 1) ? "" : "s", | |
1060 | fifo_dest); | |
550a7375 FB |
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 | |
c767c1c6 | 1077 | * from musb_interrupt(). |
550a7375 FB |
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 | ||
5c8a86e1 | 1102 | dev_dbg(musb->controller, "<== csr0 %04x, qh %p, count %d, urb %p, stage %d\n", |
550a7375 FB |
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) { | |
5c8a86e1 | 1113 | dev_dbg(musb->controller, "STALLING ENDPOINT\n"); |
550a7375 FB |
1114 | status = -EPIPE; |
1115 | ||
1116 | } else if (csr & MUSB_CSR0_H_ERROR) { | |
5c8a86e1 | 1117 | dev_dbg(musb->controller, "no response, csr0 %04x\n", csr); |
550a7375 FB |
1118 | status = -EPROTO; |
1119 | ||
1120 | } else if (csr & MUSB_CSR0_H_NAKTIMEOUT) { | |
5c8a86e1 | 1121 | dev_dbg(musb->controller, "control NAK timeout\n"); |
550a7375 FB |
1122 | |
1123 | /* NOTE: this code path would be a good place to PAUSE a | |
1124 | * control transfer, if another one is queued, so that | |
1e0320f0 AKG |
1125 | * ep0 is more likely to stay busy. That's already done |
1126 | * for bulk RX transfers. | |
550a7375 FB |
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) { | |
5c8a86e1 | 1136 | dev_dbg(musb->controller, "aborting\n"); |
550a7375 FB |
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 { | |
78322c1a | 1149 | musb_h_ep0_flush_fifo(hw_ep); |
550a7375 FB |
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 | ||
78322c1a | 1163 | musb_h_ep0_flush_fifo(hw_ep); |
550a7375 FB |
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 | ||
5c8a86e1 | 1186 | dev_dbg(musb->controller, "ep0 STATUS, csr %04x\n", csr); |
550a7375 FB |
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; | |
6b6e9710 SS |
1224 | size_t length = 0; |
1225 | size_t offset = 0; | |
550a7375 FB |
1226 | struct musb_hw_ep *hw_ep = musb->endpoints + epnum; |
1227 | void __iomem *epio = hw_ep->regs; | |
3e5c6dc7 SS |
1228 | struct musb_qh *qh = hw_ep->out_qh; |
1229 | struct urb *urb = next_urb(qh); | |
550a7375 FB |
1230 | u32 status = 0; |
1231 | void __iomem *mbase = musb->mregs; | |
1232 | struct dma_channel *dma; | |
f8afbf7f | 1233 | bool transfer_pending = false; |
8e8a5516 | 1234 | static bool use_sg; |
550a7375 | 1235 | |
550a7375 FB |
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) { | |
5c8a86e1 | 1241 | dev_dbg(musb->controller, "extra TX%d ready, csr %04x\n", epnum, tx_csr); |
6b6e9710 | 1242 | return; |
550a7375 FB |
1243 | } |
1244 | ||
1245 | pipe = urb->pipe; | |
1246 | dma = is_dma_capable() ? hw_ep->tx_channel : NULL; | |
5c8a86e1 | 1247 | dev_dbg(musb->controller, "OUT/TX%d end, csr %04x%s\n", epnum, tx_csr, |
550a7375 FB |
1248 | dma ? ", dma" : ""); |
1249 | ||
1250 | /* check for errors */ | |
1251 | if (tx_csr & MUSB_TXCSR_H_RXSTALL) { | |
1252 | /* dma was disabled, fifo flushed */ | |
5c8a86e1 | 1253 | dev_dbg(musb->controller, "TX end %d stall\n", epnum); |
550a7375 FB |
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 */ | |
5c8a86e1 | 1260 | dev_dbg(musb->controller, "TX 3strikes on ep=%d\n", epnum); |
550a7375 FB |
1261 | |
1262 | status = -ETIMEDOUT; | |
1263 | ||
1264 | } else if (tx_csr & MUSB_TXCSR_H_NAKTIMEOUT) { | |
f283862f AKG |
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; | |
550a7375 FB |
1287 | } |
1288 | ||
8e8a5516 | 1289 | done: |
550a7375 FB |
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) { | |
5c8a86e1 | 1318 | dev_dbg(musb->controller, "extra TX%d ready, csr %04x\n", epnum, tx_csr); |
6b6e9710 | 1319 | return; |
550a7375 FB |
1320 | } |
1321 | ||
c7bbc056 SS |
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)) { | |
5c8a86e1 | 1377 | dev_dbg(musb->controller, "DMA complete but packet still in FIFO, " |
c7bbc056 SS |
1378 | "CSR %04x\n", tx_csr); |
1379 | return; | |
1380 | } | |
1381 | } | |
1382 | ||
550a7375 FB |
1383 | if (!status || dma || usb_pipeisoc(pipe)) { |
1384 | if (dma) | |
6b6e9710 | 1385 | length = dma->actual_len; |
550a7375 | 1386 | else |
6b6e9710 SS |
1387 | length = qh->segsize; |
1388 | qh->offset += length; | |
550a7375 FB |
1389 | |
1390 | if (usb_pipeisoc(pipe)) { | |
1391 | struct usb_iso_packet_descriptor *d; | |
1392 | ||
1393 | d = urb->iso_frame_desc + qh->iso_idx; | |
6b6e9710 SS |
1394 | d->actual_length = length; |
1395 | d->status = status; | |
550a7375 FB |
1396 | if (++qh->iso_idx >= urb->number_of_packets) { |
1397 | done = true; | |
1398 | } else { | |
1399 | d++; | |
6b6e9710 SS |
1400 | offset = d->offset; |
1401 | length = d->length; | |
550a7375 | 1402 | } |
f8afbf7f | 1403 | } else if (dma && urb->transfer_buffer_length == qh->offset) { |
550a7375 FB |
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) { | |
6b6e9710 SS |
1414 | offset = qh->offset; |
1415 | length = urb->transfer_buffer_length - offset; | |
f8afbf7f | 1416 | transfer_pending = true; |
550a7375 FB |
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); | |
6b6e9710 | 1435 | return; |
f8afbf7f | 1436 | } else if ((usb_pipeisoc(pipe) || transfer_pending) && dma) { |
6b6e9710 | 1437 | if (musb_tx_dma_program(musb->dma_controller, hw_ep, qh, urb, |
dfeffa53 AKG |
1438 | offset, length)) { |
1439 | if (is_cppi_enabled() || tusb_dma_omap()) | |
1440 | musb_h_tx_dma_start(hw_ep); | |
6b6e9710 | 1441 | return; |
dfeffa53 | 1442 | } |
6b6e9710 | 1443 | } else if (tx_csr & MUSB_TXCSR_DMAENAB) { |
5c8a86e1 | 1444 | dev_dbg(musb->controller, "not complete, but DMA enabled?\n"); |
6b6e9710 SS |
1445 | return; |
1446 | } | |
550a7375 | 1447 | |
6b6e9710 SS |
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; | |
496dda70 | 1457 | /* Unmap the buffer so that CPU can use it */ |
c8cf203a | 1458 | usb_hcd_unmap_urb_for_dma(musb_to_hcd(musb), urb); |
8e8a5516 VS |
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 | ||
6b6e9710 | 1484 | qh->segsize = length; |
550a7375 | 1485 | |
8e8a5516 VS |
1486 | if (use_sg) { |
1487 | if (offset + length >= urb->transfer_buffer_length) | |
1488 | use_sg = false; | |
1489 | } | |
1490 | ||
6b6e9710 SS |
1491 | musb_ep_select(mbase, epnum); |
1492 | musb_writew(epio, MUSB_TXCSR, | |
1493 | MUSB_TXCSR_H_WZC_BITS | MUSB_TXCSR_TXPKTRDY); | |
550a7375 FB |
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; | |
8e8a5516 VS |
1554 | static bool use_sg; |
1555 | unsigned int sg_flags = SG_MITER_ATOMIC | SG_MITER_TO_SG; | |
550a7375 FB |
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 | */ | |
5c8a86e1 | 1572 | dev_dbg(musb->controller, "BOGUS RX%d ready, csr %04x, count %d\n", epnum, val, |
550a7375 FB |
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 | ||
5c8a86e1 | 1580 | dev_dbg(musb->controller, "<== hw %d rxcsr %04x, urb actual %d (+dma %zu)\n", |
550a7375 FB |
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) { | |
5c8a86e1 | 1587 | dev_dbg(musb->controller, "RX end %d STALL\n", epnum); |
550a7375 FB |
1588 | |
1589 | /* stall; record URB status */ | |
1590 | status = -EPIPE; | |
1591 | ||
1592 | } else if (rx_csr & MUSB_RXCSR_H_ERROR) { | |
5c8a86e1 | 1593 | dev_dbg(musb->controller, "end %d RX proto error\n", epnum); |
550a7375 FB |
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) { | |
5c8a86e1 | 1601 | dev_dbg(musb->controller, "RX end %d NAK timeout\n", epnum); |
1e0320f0 AKG |
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. | |
550a7375 | 1606 | * |
1e0320f0 AKG |
1607 | * Devices like Ethernet and serial adapters keep |
1608 | * reads posted at all times, which will starve | |
1609 | * other devices without this logic. | |
550a7375 | 1610 | */ |
1e0320f0 AKG |
1611 | if (usb_pipebulk(urb->pipe) |
1612 | && qh->mux == 1 | |
1613 | && !list_is_singular(&musb->in_bulk)) { | |
f283862f | 1614 | musb_bulk_nak_timeout(musb, hw_ep, 1); |
1e0320f0 AKG |
1615 | return; |
1616 | } | |
550a7375 | 1617 | musb_ep_select(mbase, epnum); |
1e0320f0 AKG |
1618 | rx_csr |= MUSB_RXCSR_H_WZC_BITS; |
1619 | rx_csr &= ~MUSB_RXCSR_DATAERROR; | |
1620 | musb_writew(epio, MUSB_RXCSR, rx_csr); | |
550a7375 FB |
1621 | |
1622 | goto finish; | |
1623 | } else { | |
5c8a86e1 | 1624 | dev_dbg(musb->controller, "RX end %d ISO data error\n", epnum); |
550a7375 FB |
1625 | /* packet error reported later */ |
1626 | iso_err = true; | |
1627 | } | |
a483d706 | 1628 | } else if (rx_csr & MUSB_RXCSR_INCOMPRX) { |
5c8a86e1 | 1629 | dev_dbg(musb->controller, "end %d high bandwidth incomplete ISO packet RX\n", |
a483d706 AKG |
1630 | epnum); |
1631 | status = -EPROTO; | |
550a7375 FB |
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 | ||
5c8a86e1 | 1675 | dev_dbg(musb->controller, "RXCSR%d %04x, reqpkt, len %zu%s\n", epnum, rx_csr, |
550a7375 FB |
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 | |
f82a689f AKG |
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 | */ | |
72887c86 | 1703 | if (d->status != -EILSEQ && d->status != -EOVERFLOW) |
f82a689f AKG |
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 { | |
550a7375 FB |
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); | |
f82a689f | 1716 | } |
550a7375 FB |
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 | ||
5c8a86e1 | 1725 | dev_dbg(musb->controller, "ep %d dma %s, rxcsr %04x, rxcount %d\n", epnum, |
550a7375 FB |
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; | |
f82a689f AKG |
1753 | int ret, length; |
1754 | dma_addr_t buf; | |
550a7375 FB |
1755 | |
1756 | rx_count = musb_readw(epio, MUSB_RXCOUNT); | |
1757 | ||
5c8a86e1 | 1758 | dev_dbg(musb->controller, "RX%d count %d, buffer 0x%x len %d/%d\n", |
550a7375 FB |
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 | ||
f82a689f | 1767 | if (usb_pipeisoc(pipe)) { |
8b4959d6 | 1768 | int d_status = 0; |
f82a689f AKG |
1769 | struct usb_iso_packet_descriptor *d; |
1770 | ||
1771 | d = urb->iso_frame_desc + qh->iso_idx; | |
1772 | ||
1773 | if (iso_err) { | |
8b4959d6 | 1774 | d_status = -EILSEQ; |
f82a689f AKG |
1775 | urb->error_count++; |
1776 | } | |
1777 | if (rx_count > d->length) { | |
8b4959d6 FB |
1778 | if (d_status == 0) { |
1779 | d_status = -EOVERFLOW; | |
f82a689f AKG |
1780 | urb->error_count++; |
1781 | } | |
5c8a86e1 | 1782 | dev_dbg(musb->controller, "** OVERFLOW %d into %d\n",\ |
f82a689f AKG |
1783 | rx_count, d->length); |
1784 | ||
1785 | length = d->length; | |
1786 | } else | |
1787 | length = rx_count; | |
8b4959d6 | 1788 | d->status = d_status; |
f82a689f AKG |
1789 | buf = urb->transfer_dma + d->offset; |
1790 | } else { | |
1791 | length = rx_count; | |
1792 | buf = urb->transfer_dma + | |
1793 | urb->actual_length; | |
1794 | } | |
1795 | ||
550a7375 FB |
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; | |
f82a689f AKG |
1807 | if (rx_count < hw_ep->max_packet_sz_rx) { |
1808 | length = rx_count; | |
ae926976 | 1809 | dma->desired_mode = 0; |
f82a689f AKG |
1810 | } else { |
1811 | length = urb->transfer_buffer_length; | |
1812 | } | |
550a7375 FB |
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; | |
a483d706 AKG |
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; | |
550a7375 FB |
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, | |
f82a689f | 1854 | dma->desired_mode, buf, length); |
550a7375 FB |
1855 | |
1856 | if (!ret) { | |
1857 | c->channel_release(dma); | |
1858 | hw_ep->rx_channel = NULL; | |
1859 | dma = NULL; | |
2ed9127c MS |
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); | |
550a7375 FB |
1865 | } |
1866 | } | |
1867 | #endif /* Mentor DMA */ | |
1868 | ||
1869 | if (!dma) { | |
8e8a5516 VS |
1870 | unsigned int received_len; |
1871 | ||
496dda70 | 1872 | /* Unmap the buffer so that CPU can use it */ |
c8cf203a | 1873 | usb_hcd_unmap_urb_for_dma(musb_to_hcd(musb), urb); |
8e8a5516 VS |
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 | } | |
5c8a86e1 | 1907 | dev_dbg(musb->controller, "read %spacket\n", done ? "last " : ""); |
550a7375 FB |
1908 | } |
1909 | } | |
1910 | ||
550a7375 FB |
1911 | finish: |
1912 | urb->actual_length += xfer_len; | |
1913 | qh->offset += xfer_len; | |
1914 | if (done) { | |
8e8a5516 VS |
1915 | if (use_sg) |
1916 | use_sg = false; | |
1917 | ||
550a7375 FB |
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; | |
5274dab6 S |
1939 | u8 toggle; |
1940 | u8 txtype; | |
1941 | struct urb *urb = next_urb(qh); | |
550a7375 FB |
1942 | |
1943 | /* use fixed hardware for control and bulk */ | |
23d15e07 | 1944 | if (qh->type == USB_ENDPOINT_XFER_CONTROL) { |
550a7375 FB |
1945 | head = &musb->control; |
1946 | hw_ep = musb->control_ep; | |
550a7375 FB |
1947 | goto success; |
1948 | } | |
1949 | ||
1950 | /* else, periodic transfers get muxed to other endpoints */ | |
1951 | ||
5d67a851 SS |
1952 | /* |
1953 | * We know this qh hasn't been scheduled, so all we need to do | |
550a7375 FB |
1954 | * is choose which hardware endpoint to put it on ... |
1955 | * | |
1956 | * REVISIT what we really want here is a regular schedule tree | |
5d67a851 | 1957 | * like e.g. OHCI uses. |
550a7375 FB |
1958 | */ |
1959 | best_diff = 4096; | |
1960 | best_end = -1; | |
1961 | ||
5d67a851 SS |
1962 | for (epnum = 1, hw_ep = musb->endpoints + 1; |
1963 | epnum < musb->nr_endpoints; | |
1964 | epnum++, hw_ep++) { | |
550a7375 FB |
1965 | int diff; |
1966 | ||
3e5c6dc7 | 1967 | if (musb_ep_get_qh(hw_ep, is_in) != NULL) |
550a7375 | 1968 | continue; |
5d67a851 | 1969 | |
550a7375 FB |
1970 | if (hw_ep == musb->bulk_ep) |
1971 | continue; | |
1972 | ||
1973 | if (is_in) | |
a483d706 | 1974 | diff = hw_ep->max_packet_sz_rx; |
550a7375 | 1975 | else |
a483d706 AKG |
1976 | diff = hw_ep->max_packet_sz_tx; |
1977 | diff -= (qh->maxpacket * qh->hb_mult); | |
550a7375 | 1978 | |
23d15e07 | 1979 | if (diff >= 0 && best_diff > diff) { |
5274dab6 S |
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 | ||
550a7375 FB |
2001 | best_diff = diff; |
2002 | best_end = epnum; | |
2003 | } | |
2004 | } | |
23d15e07 | 2005 | /* use bulk reserved ep1 if no other ep is free */ |
aa5cbbec | 2006 | if (best_end < 0 && qh->type == USB_ENDPOINT_XFER_BULK) { |
23d15e07 AKG |
2007 | hw_ep = musb->bulk_ep; |
2008 | if (is_in) | |
2009 | head = &musb->in_bulk; | |
2010 | else | |
2011 | head = &musb->out_bulk; | |
1e0320f0 | 2012 | |
f283862f | 2013 | /* Enable bulk RX/TX NAK timeout scheme when bulk requests are |
1e0320f0 AKG |
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 | */ | |
f283862f | 2020 | if (qh->dev) |
1e0320f0 AKG |
2021 | qh->intv_reg = |
2022 | (USB_SPEED_HIGH == qh->dev->speed) ? 8 : 4; | |
23d15e07 AKG |
2023 | goto success; |
2024 | } else if (best_end < 0) { | |
550a7375 | 2025 | return -ENOSPC; |
23d15e07 | 2026 | } |
550a7375 FB |
2027 | |
2028 | idle = 1; | |
23d15e07 | 2029 | qh->mux = 0; |
550a7375 | 2030 | hw_ep = musb->endpoints + best_end; |
5c8a86e1 | 2031 | dev_dbg(musb->controller, "qh %p periodic slot %d\n", qh, best_end); |
550a7375 | 2032 | success: |
23d15e07 AKG |
2033 | if (head) { |
2034 | idle = list_empty(head); | |
2035 | list_add_tail(&qh->ring, head); | |
2036 | qh->mux = 1; | |
2037 | } | |
550a7375 FB |
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; | |
74bb3508 | 2053 | struct musb_qh *qh; |
550a7375 FB |
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); | |
74bb3508 DB |
2065 | qh = ret ? NULL : hep->hcpriv; |
2066 | if (qh) | |
2067 | urb->hcpriv = qh; | |
550a7375 | 2068 | spin_unlock_irqrestore(&musb->lock, flags); |
550a7375 FB |
2069 | |
2070 | /* DMA mapping was already done, if needed, and this urb is on | |
74bb3508 DB |
2071 | * hep->urb_list now ... so we're done, unless hep wasn't yet |
2072 | * scheduled onto a live qh. | |
550a7375 FB |
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 | */ | |
74bb3508 DB |
2078 | if (qh || ret) |
2079 | return ret; | |
550a7375 FB |
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) { | |
2492e674 | 2089 | spin_lock_irqsave(&musb->lock, flags); |
550a7375 | 2090 | usb_hcd_unlink_urb_from_ep(hcd, urb); |
2492e674 | 2091 | spin_unlock_irqrestore(&musb->lock, flags); |
550a7375 FB |
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 | ||
29cc8897 | 2100 | qh->maxpacket = usb_endpoint_maxp(epd); |
a483d706 | 2101 | qh->type = usb_endpoint_type(epd); |
550a7375 | 2102 | |
a483d706 AKG |
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; | |
550a7375 FB |
2119 | } |
2120 | ||
96bcd090 | 2121 | qh->epnum = usb_endpoint_num(epd); |
550a7375 FB |
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 | ||
136733d6 | 2140 | /* Precompute RXINTERVAL/TXINTERVAL register */ |
550a7375 FB |
2141 | switch (qh->type) { |
2142 | case USB_ENDPOINT_XFER_INT: | |
136733d6 SS |
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; | |
550a7375 FB |
2150 | } |
2151 | /* FALLTHROUGH */ | |
2152 | case USB_ENDPOINT_XFER_ISOC: | |
136733d6 SS |
2153 | /* ISO always uses logarithmic encoding */ |
2154 | interval = min_t(u8, epd->bInterval, 16); | |
550a7375 FB |
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 | |
1e0320f0 AKG |
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. | |
550a7375 | 2168 | * |
1e0320f0 | 2169 | * The upside of disabling it is simpler transfer scheduling. |
550a7375 FB |
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; | |
ae5ad296 AKG |
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; | |
550a7375 FB |
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); | |
3067779b | 2199 | if (hep->hcpriv || !next_urb(qh)) { |
550a7375 FB |
2200 | /* some concurrent activity submitted another urb to hep... |
2201 | * odd, rare, error prone, but legal. | |
2202 | */ | |
2203 | kfree(qh); | |
714bc5ef | 2204 | qh = NULL; |
550a7375 FB |
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) { | |
2492e674 | 2220 | spin_lock_irqsave(&musb->lock, flags); |
550a7375 | 2221 | usb_hcd_unlink_urb_from_ep(hcd, urb); |
2492e674 | 2222 | spin_unlock_irqrestore(&musb->lock, flags); |
550a7375 FB |
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 | */ | |
81ec4e4a | 2234 | static int musb_cleanup_urb(struct urb *urb, struct musb_qh *qh) |
550a7375 FB |
2235 | { |
2236 | struct musb_hw_ep *ep = qh->hw_ep; | |
5c8a86e1 | 2237 | struct musb *musb = ep->musb; |
550a7375 FB |
2238 | void __iomem *epio = ep->regs; |
2239 | unsigned hw_end = ep->epnum; | |
2240 | void __iomem *regs = ep->musb->mregs; | |
81ec4e4a | 2241 | int is_in = usb_pipein(urb->pipe); |
550a7375 | 2242 | int status = 0; |
81ec4e4a | 2243 | u16 csr; |
550a7375 FB |
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); | |
5c8a86e1 | 2253 | dev_dbg(musb->controller, |
550a7375 FB |
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 ... */ | |
692933b2 | 2262 | if (ep->epnum && is_in) { |
550a7375 FB |
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 | */ | |
78322c1a | 2270 | } else if (ep->epnum) { |
550a7375 FB |
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); | |
78322c1a DB |
2284 | } else { |
2285 | musb_h_ep0_flush_fifo(ep); | |
550a7375 FB |
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; | |
550a7375 | 2296 | unsigned long flags; |
22a0d6f1 | 2297 | int is_in = usb_pipein(urb->pipe); |
550a7375 FB |
2298 | int ret; |
2299 | ||
5c8a86e1 | 2300 | dev_dbg(musb->controller, "urb=%p, dev%d ep%d%s\n", urb, |
550a7375 FB |
2301 | usb_pipedevice(urb->pipe), |
2302 | usb_pipeendpoint(urb->pipe), | |
22a0d6f1 | 2303 | is_in ? "in" : "out"); |
550a7375 FB |
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 | ||
22a0d6f1 SS |
2314 | /* |
2315 | * Any URB not actively programmed into endpoint hardware can be | |
a2fd814e | 2316 | * immediately given back; that's any URB not at the head of an |
550a7375 | 2317 | * endpoint queue, unless someday we get real DMA queues. And even |
a2fd814e | 2318 | * if it's at the head, it might not be known to the hardware... |
550a7375 | 2319 | * |
22a0d6f1 | 2320 | * Otherwise abort current transfer, pending DMA, etc.; urb->status |
550a7375 FB |
2321 | * has already been updated. This is a synchronous abort; it'd be |
2322 | * OK to hold off until after some IRQ, though. | |
22a0d6f1 SS |
2323 | * |
2324 | * NOTE: qh is invalid unless !list_empty(&hep->urb_list) | |
550a7375 | 2325 | */ |
22a0d6f1 SS |
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) { | |
550a7375 FB |
2329 | int ready = qh->is_ready; |
2330 | ||
550a7375 | 2331 | qh->is_ready = 0; |
c9cd06b3 | 2332 | musb_giveback(musb, urb, 0); |
550a7375 | 2333 | qh->is_ready = ready; |
a2fd814e SS |
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 | } | |
550a7375 | 2343 | } else |
81ec4e4a | 2344 | ret = musb_cleanup_urb(urb, qh); |
550a7375 FB |
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 | { | |
22a0d6f1 | 2354 | u8 is_in = hep->desc.bEndpointAddress & USB_DIR_IN; |
550a7375 FB |
2355 | unsigned long flags; |
2356 | struct musb *musb = hcd_to_musb(hcd); | |
dc61d238 SS |
2357 | struct musb_qh *qh; |
2358 | struct urb *urb; | |
550a7375 | 2359 | |
550a7375 FB |
2360 | spin_lock_irqsave(&musb->lock, flags); |
2361 | ||
dc61d238 SS |
2362 | qh = hep->hcpriv; |
2363 | if (qh == NULL) | |
2364 | goto exit; | |
2365 | ||
22a0d6f1 | 2366 | /* NOTE: qh is invalid unless !list_empty(&hep->urb_list) */ |
550a7375 | 2367 | |
22a0d6f1 | 2368 | /* Kick the first URB off the hardware, if needed */ |
550a7375 | 2369 | qh->is_ready = 0; |
22a0d6f1 | 2370 | if (musb_ep_get_qh(qh->hw_ep, is_in) == qh) { |
550a7375 FB |
2371 | urb = next_urb(qh); |
2372 | ||
2373 | /* make software (then hardware) stop ASAP */ | |
2374 | if (!urb->unlinked) | |
2375 | urb->status = -ESHUTDOWN; | |
2376 | ||
2377 | /* cleanup */ | |
81ec4e4a | 2378 | musb_cleanup_urb(urb, qh); |
550a7375 | 2379 | |
dc61d238 SS |
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)) | |
c9cd06b3 | 2394 | musb_giveback(musb, next_urb(qh), -ESHUTDOWN); |
550a7375 | 2395 | |
dc61d238 SS |
2396 | hep->hcpriv = NULL; |
2397 | list_del(&qh->ring); | |
2398 | kfree(qh); | |
2399 | } | |
2400 | exit: | |
550a7375 FB |
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); | |
89368d3d | 2432 | u8 devctl; |
550a7375 | 2433 | |
89368d3d | 2434 | if (!is_host_active(musb)) |
550a7375 FB |
2435 | return 0; |
2436 | ||
89368d3d DB |
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", | |
42c0bf1c | 2455 | usb_otg_state_string(musb->xceiv->state)); |
550a7375 FB |
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 | ||
8408fd1d RB |
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 | ||
550a7375 FB |
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 | ||
8408fd1d RB |
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 | ||
550a7375 FB |
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 | }; | |
0b3eba44 DM |
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 | } |