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