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