]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/usb/musb/musb_host.c
musb_hdrc: Fixes before pulling from mainline tree
[linux-2.6-omap-h63xx.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  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * version 2 as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA
21  *
22  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
23  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
25  * NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
28  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
29  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  */
34
35 #include <linux/module.h>
36 #include <linux/kernel.h>
37 #include <linux/delay.h>
38 #include <linux/sched.h>
39 #include <linux/slab.h>
40 #include <linux/errno.h>
41 #include <linux/init.h>
42 #include <linux/list.h>
43
44 #include "musb_core.h"
45 #include "musb_host.h"
46
47
48 /* MUSB HOST status 22-mar-2006
49  *
50  * - There's still lots of partial code duplication for fault paths, so
51  *   they aren't handled as consistently as they need to be.
52  *
53  * - PIO mostly behaved when last tested.
54  *     + including ep0, with all usbtest cases 9, 10
55  *     + usbtest 14 (ep0out) doesn't seem to run at all
56  *     + double buffered OUT/TX endpoints saw stalls(!) with certain usbtest
57  *       configurations, but otherwise double buffering passes basic tests.
58  *     + for 2.6.N, for N > ~10, needs API changes for hcd framework.
59  *
60  * - DMA (CPPI) ... partially behaves, not currently recommended
61  *     + about 1/15 the speed of typical EHCI implementations (PCI)
62  *     + RX, all too often reqpkt seems to misbehave after tx
63  *     + TX, no known issues (other than evident silicon issue)
64  *
65  * - DMA (Mentor/OMAP) ...has at least toggle update problems
66  *
67  * - Still no traffic scheduling code to make NAKing for bulk or control
68  *   transfers unable to starve other requests; or to make efficient use
69  *   of hardware with periodic transfers.  (Note that network drivers
70  *   commonly post bulk reads that stay pending for a long time; these
71  *   would make very visible trouble.)
72  *
73  * - Not tested with HNP, but some SRP paths seem to behave.
74  *
75  * NOTE 24-August-2006:
76  *
77  * - Bulk traffic finally uses both sides of hardware ep1, freeing up an
78  *   extra endpoint for periodic use enabling hub + keybd + mouse.  That
79  *   mostly works, except that with "usbnet" it's easy to trigger cases
80  *   with "ping" where RX loses.  (a) ping to davinci, even "ping -f",
81  *   fine; but (b) ping _from_ davinci, even "ping -c 1", ICMP RX loses
82  *   although ARP RX wins.  (That test was done with a full speed link.)
83  */
84
85
86 /*
87  * NOTE on endpoint usage:
88  *
89  * CONTROL transfers all go through ep0.  BULK ones go through dedicated IN
90  * and OUT endpoints ... hardware is dedicated for those "async" queue(s).
91  *
92  * (Yes, bulk _could_ use more of the endpoints than that, and would even
93  * benefit from it ... one remote device may easily be NAKing while others
94  * need to perform transfers in that same direction.  The same thing could
95  * be done in software though, assuming dma cooperates.)
96  *
97  * INTERUPPT and ISOCHRONOUS transfers are scheduled to the other endpoints.
98  * So far that scheduling is both dumb and optimistic:  the endpoint will be
99  * "claimed" until its software queue is no longer refilled.  No multiplexing
100  * of transfers between endpoints, or anything clever.
101  */
102
103
104 static void musb_ep_program(struct musb *musb, u8 epnum,
105                         struct urb *urb, unsigned int nOut,
106                         u8 *buf, u32 len);
107
108 /*
109  * Clear TX fifo. Needed to avoid BABBLE errors.
110  */
111 static inline void musb_h_tx_flush_fifo(struct musb_hw_ep *ep)
112 {
113         void __iomem    *epio = ep->regs;
114         u16             csr;
115         int             retries = 1000;
116
117         csr = musb_readw(epio, MUSB_TXCSR);
118         while (csr & MUSB_TXCSR_FIFONOTEMPTY) {
119                 DBG(5, "Host TX FIFONOTEMPTY csr: %02x\n", csr);
120                 csr |= MUSB_TXCSR_FLUSHFIFO;
121                 musb_writew(epio, MUSB_TXCSR, csr);
122                 csr = musb_readw(epio, MUSB_TXCSR);
123                 if (retries-- < 1) {
124                         ERR("Could not flush host TX fifo: csr: %04x\n", csr);
125                         return;
126                 }
127                 mdelay(1);
128         }
129 }
130
131 /*
132  * Start transmit. Caller is responsible for locking shared resources.
133  * musb must be locked.
134  */
135 static inline void musb_h_tx_start(struct musb_hw_ep *ep)
136 {
137         u16     txcsr;
138
139         /* NOTE: no locks here; caller should lock and select EP */
140         if (ep->epnum) {
141                 txcsr = musb_readw(ep->regs, MUSB_TXCSR);
142                 txcsr |= MUSB_TXCSR_TXPKTRDY | MUSB_TXCSR_H_WZC_BITS;
143                 musb_writew(ep->regs, MUSB_TXCSR, txcsr);
144         } else {
145                 txcsr = MUSB_CSR0_H_SETUPPKT | MUSB_CSR0_TXPKTRDY;
146                 musb_writew(ep->regs, MUSB_CSR0, txcsr);
147         }
148
149 }
150
151 static inline void cppi_host_txdma_start(struct musb_hw_ep *ep)
152 {
153         u16     txcsr;
154
155         /* NOTE: no locks here; caller should lock and select EP */
156         txcsr = musb_readw(ep->regs, MUSB_TXCSR);
157         txcsr |= MUSB_TXCSR_DMAENAB | MUSB_TXCSR_H_WZC_BITS;
158         musb_writew(ep->regs, MUSB_TXCSR, txcsr);
159 }
160
161 /*
162  * Start the URB at the front of an endpoint's queue
163  * end must be claimed from the caller.
164  *
165  * Context: controller locked, irqs blocked
166  */
167 static void
168 musb_start_urb(struct musb *musb, int is_in, struct musb_qh *qh)
169 {
170         u16                     frame;
171         u32                     len;
172         void                    *buf;
173         void __iomem            *mbase =  musb->mregs;
174         struct urb              *urb = next_urb(qh);
175         struct musb_hw_ep       *hw_ep = qh->hw_ep;
176         unsigned                pipe = urb->pipe;
177         u8                      address = usb_pipedevice(pipe);
178         int                     epnum = hw_ep->epnum;
179
180         /* initialize software qh state */
181         qh->offset = 0;
182         qh->segsize = 0;
183
184         /* gather right source of data */
185         switch (qh->type) {
186         case USB_ENDPOINT_XFER_CONTROL:
187                 /* control transfers always start with SETUP */
188                 is_in = 0;
189                 hw_ep->out_qh = qh;
190                 musb->ep0_stage = MUSB_EP0_START;
191                 buf = urb->setup_packet;
192                 len = 8;
193                 break;
194         case USB_ENDPOINT_XFER_ISOC:
195                 qh->iso_idx = 0;
196                 qh->frame = 0;
197                 buf = urb->transfer_buffer + urb->iso_frame_desc[0].offset;
198                 len = urb->iso_frame_desc[0].length;
199                 break;
200         default:                /* bulk, interrupt */
201                 buf = urb->transfer_buffer;
202                 len = urb->transfer_buffer_length;
203         }
204
205         DBG(4, "qh %p urb %p dev%d ep%d%s%s, hw_ep %d, %p/%d\n",
206                         qh, urb, address, qh->epnum,
207                         is_in ? "in" : "out",
208                         ({char *s; switch (qh->type) {
209                         case USB_ENDPOINT_XFER_CONTROL: s = ""; break;
210                         case USB_ENDPOINT_XFER_BULK:    s = "-bulk"; break;
211                         case USB_ENDPOINT_XFER_ISOC:    s = "-iso"; break;
212                         default:                        s = "-intr"; break;
213                         }; s; }),
214                         epnum, buf, len);
215
216         /* Configure endpoint */
217         if (is_in || hw_ep->is_shared_fifo)
218                 hw_ep->in_qh = qh;
219         else
220                 hw_ep->out_qh = qh;
221         musb_ep_program(musb, epnum, urb, !is_in, buf, len);
222
223         /* transmit may have more work: start it when it is time */
224         if (is_in)
225                 return;
226
227         /* determine if the time is right for a periodic transfer */
228         switch (qh->type) {
229         case USB_ENDPOINT_XFER_ISOC:
230         case USB_ENDPOINT_XFER_INT:
231                 DBG(3, "check whether there's still time for periodic Tx\n");
232                 qh->iso_idx = 0;
233                 frame = musb_readw(mbase, MUSB_FRAME);
234                 /* FIXME this doesn't implement that scheduling policy ...
235                  * or handle framecounter wrapping
236                  */
237                 if ((urb->transfer_flags & URB_ISO_ASAP)
238                                 || (frame >= urb->start_frame)) {
239                         /* REVISIT the SOF irq handler shouldn't duplicate
240                          * this code; and we don't init urb->start_frame...
241                          */
242                         qh->frame = 0;
243                         goto start;
244                 } else {
245                         qh->frame = urb->start_frame;
246                         /* enable SOF interrupt so we can count down */
247                         DBG(1, "SOF for %d\n", epnum);
248 #if 1 /* ifndef CONFIG_ARCH_DAVINCI */
249                         musb_writeb(mbase, MUSB_INTRUSBE, 0xff);
250 #endif
251                 }
252                 break;
253         default:
254 start:
255                 DBG(4, "Start TX%d %s\n", epnum,
256                         hw_ep->tx_channel ? "dma" : "pio");
257
258                 if (!hw_ep->tx_channel)
259                         musb_h_tx_start(hw_ep);
260                 else if (is_cppi_enabled() || tusb_dma_omap())
261                         cppi_host_txdma_start(hw_ep);
262         }
263 }
264
265 /* caller owns controller lock, irqs are blocked */
266 static void
267 __musb_giveback(struct musb *musb, struct urb *urb, int status)
268 __releases(musb->lock)
269 __acquires(musb->lock)
270 {
271         DBG(({ int level; switch (urb->status) {
272                                 case 0:
273                                         level = 4;
274                                         break;
275                                 /* common/boring faults */
276                                 case -EREMOTEIO:
277                                 case -ESHUTDOWN:
278                                 case -ECONNRESET:
279                                 case -EPIPE:
280                                         level = 3;
281                                         break;
282                                 default:
283                                         level = 2;
284                                         break;
285                                 }; level; }),
286                         "complete %p (%d), dev%d ep%d%s, %d/%d\n",
287                         urb, urb->status,
288                         usb_pipedevice(urb->pipe),
289                         usb_pipeendpoint(urb->pipe),
290                         usb_pipein(urb->pipe) ? "in" : "out",
291                         urb->actual_length, urb->transfer_buffer_length
292                         );
293
294         spin_unlock(&musb->lock);
295         usb_hcd_giveback_urb(musb_to_hcd(musb), urb, status);
296         spin_lock(&musb->lock);
297 }
298
299 /* for bulk/interrupt endpoints only */
300 static inline void
301 musb_save_toggle(struct musb_hw_ep *ep, int is_in, struct urb *urb)
302 {
303         struct usb_device       *udev = urb->dev;
304         u16                     csr;
305         void __iomem            *epio = ep->regs;
306         struct musb_qh          *qh;
307
308         /* FIXME:  the current Mentor DMA code seems to have
309          * problems getting toggle correct.
310          */
311
312         if (is_in || ep->is_shared_fifo)
313                 qh = ep->in_qh;
314         else
315                 qh = ep->out_qh;
316
317         if (!is_in) {
318                 csr = musb_readw(epio, MUSB_TXCSR);
319                 usb_settoggle(udev, qh->epnum, 1,
320                         (csr & MUSB_TXCSR_H_DATATOGGLE)
321                                 ? 1 : 0);
322         } else {
323                 csr = musb_readw(epio, MUSB_RXCSR);
324                 usb_settoggle(udev, qh->epnum, 0,
325                         (csr & MUSB_RXCSR_H_DATATOGGLE)
326                                 ? 1 : 0);
327         }
328 }
329
330 /* caller owns controller lock, irqs are blocked */
331 static struct musb_qh *
332 musb_giveback(struct musb_qh *qh, struct urb *urb, int status)
333 {
334         int                     is_in;
335         struct musb_hw_ep       *ep = qh->hw_ep;
336         struct musb             *musb = ep->musb;
337         int                     ready = qh->is_ready;
338
339         if (ep->is_shared_fifo)
340                 is_in = 1;
341         else
342                 is_in = usb_pipein(urb->pipe);
343
344         /* save toggle eagerly, for paranoia */
345         switch (qh->type) {
346         case USB_ENDPOINT_XFER_BULK:
347         case USB_ENDPOINT_XFER_INT:
348                 musb_save_toggle(ep, is_in, urb);
349                 break;
350         case USB_ENDPOINT_XFER_ISOC:
351                 if (status == 0 && urb->error_count)
352                         status = -EXDEV;
353                 break;
354         }
355
356         usb_hcd_unlink_urb_from_ep(musb_to_hcd(musb), urb);
357
358         qh->is_ready = 0;
359         __musb_giveback(musb, urb, status);
360         qh->is_ready = ready;
361
362         /* reclaim resources (and bandwidth) ASAP; deschedule it, and
363          * invalidate qh as soon as list_empty(&hep->urb_list)
364          */
365         if (list_empty(&qh->hep->urb_list)) {
366                 struct list_head        *head;
367
368                 if (is_in)
369                         ep->rx_reinit = 1;
370                 else
371                         ep->tx_reinit = 1;
372
373                 /* clobber old pointers to this qh */
374                 if (is_in || ep->is_shared_fifo)
375                         ep->in_qh = NULL;
376                 else
377                         ep->out_qh = NULL;
378                 qh->hep->hcpriv = NULL;
379
380                 switch (qh->type) {
381
382                 case USB_ENDPOINT_XFER_ISOC:
383                 case USB_ENDPOINT_XFER_INT:
384                         /* this is where periodic bandwidth should be
385                          * de-allocated if it's tracked and allocated;
386                          * and where we'd update the schedule tree...
387                          */
388                         musb->periodic[ep->epnum] = NULL;
389                         kfree(qh);
390                         qh = NULL;
391                         break;
392
393                 case USB_ENDPOINT_XFER_CONTROL:
394                 case USB_ENDPOINT_XFER_BULK:
395                         /* fifo policy for these lists, except that NAKing
396                          * should rotate a qh to the end (for fairness).
397                          */
398                         head = qh->ring.prev;
399                         list_del(&qh->ring);
400                         kfree(qh);
401                         qh = first_qh(head);
402                         break;
403                 }
404         }
405         return qh;
406 }
407
408 /*
409  * Advance this hardware endpoint's queue, completing the specified urb and
410  * advancing to either the next urb queued to that qh, or else invalidating
411  * that qh and advancing to the next qh scheduled after the current one.
412  *
413  * Context: caller owns controller lock, irqs are blocked
414  */
415 static void
416 musb_advance_schedule(struct musb *musb, struct urb *urb,
417                 struct musb_hw_ep *hw_ep, int is_in)
418 {
419         struct musb_qh  *qh;
420
421         if (is_in || hw_ep->is_shared_fifo)
422                 qh = hw_ep->in_qh;
423         else
424                 qh = hw_ep->out_qh;
425
426         if (urb->status == -EINPROGRESS)
427                 qh = musb_giveback(qh, urb, 0);
428         else
429                 qh = musb_giveback(qh, urb, urb->status);
430
431         if (qh && qh->is_ready && !list_empty(&qh->hep->urb_list)) {
432                 DBG(4, "... next ep%d %cX urb %p\n",
433                                 hw_ep->epnum, is_in ? 'R' : 'T',
434                                 next_urb(qh));
435                 musb_start_urb(musb, is_in, qh);
436         }
437 }
438
439 static inline u16 musb_h_flush_rxfifo(struct musb_hw_ep *hw_ep, u16 csr)
440 {
441         /* we don't want fifo to fill itself again;
442          * ignore dma (various models),
443          * leave toggle alone (may not have been saved yet)
444          */
445         csr |= MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_RXPKTRDY;
446         csr &= ~(MUSB_RXCSR_H_REQPKT
447                 | MUSB_RXCSR_H_AUTOREQ
448                 | MUSB_RXCSR_AUTOCLEAR);
449
450         /* write 2x to allow double buffering */
451         musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
452         musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
453
454         /* flush writebuffer */
455         return musb_readw(hw_ep->regs, MUSB_RXCSR);
456 }
457
458 /*
459  * PIO RX for a packet (or part of it).
460  */
461 static bool
462 musb_host_packet_rx(struct musb *musb, struct urb *urb, u8 epnum, u8 iso_err)
463 {
464         u16                     rx_count;
465         u8                      *buf;
466         u16                     csr;
467         bool                    done = false;
468         u32                     length;
469         int                     do_flush = 0;
470         struct musb_hw_ep       *hw_ep = musb->endpoints + epnum;
471         void __iomem            *epio = hw_ep->regs;
472         struct musb_qh          *qh = hw_ep->in_qh;
473         int                     pipe = urb->pipe;
474         void                    *buffer = urb->transfer_buffer;
475
476         /* musb_ep_select(mbase, epnum); */
477         rx_count = musb_readw(epio, MUSB_RXCOUNT);
478         DBG(3, "RX%d count %d, buffer %p len %d/%d\n", epnum, rx_count,
479                         urb->transfer_buffer, qh->offset,
480                         urb->transfer_buffer_length);
481
482         /* unload FIFO */
483         if (usb_pipeisoc(pipe)) {
484                 int                                     status = 0;
485                 struct usb_iso_packet_descriptor        *d;
486
487                 if (iso_err) {
488                         status = -EILSEQ;
489                         urb->error_count++;
490                 }
491
492                 d = urb->iso_frame_desc + qh->iso_idx;
493                 buf = buffer + d->offset;
494                 length = d->length;
495                 if (rx_count > length) {
496                         if (status == 0) {
497                                 status = -EOVERFLOW;
498                                 urb->error_count++;
499                         }
500                         DBG(2, "** OVERFLOW %d into %d\n", rx_count, length);
501                         do_flush = 1;
502                 } else
503                         length = rx_count;
504                 urb->actual_length += length;
505                 d->actual_length = length;
506
507                 d->status = status;
508
509                 /* see if we are done */
510                 done = (++qh->iso_idx >= urb->number_of_packets);
511         } else {
512                 /* non-isoch */
513                 buf = buffer + qh->offset;
514                 length = urb->transfer_buffer_length - qh->offset;
515                 if (rx_count > length) {
516                         if (urb->status == -EINPROGRESS)
517                                 urb->status = -EOVERFLOW;
518                         DBG(2, "** OVERFLOW %d into %d\n", rx_count, length);
519                         do_flush = 1;
520                 } else
521                         length = rx_count;
522                 urb->actual_length += length;
523                 qh->offset += length;
524
525                 /* see if we are done */
526                 done = (urb->actual_length == urb->transfer_buffer_length)
527                         || (rx_count < qh->maxpacket)
528                         || (urb->status != -EINPROGRESS);
529                 if (done
530                                 && (urb->status == -EINPROGRESS)
531                                 && (urb->transfer_flags & URB_SHORT_NOT_OK)
532                                 && (urb->actual_length
533                                         < urb->transfer_buffer_length))
534                         urb->status = -EREMOTEIO;
535         }
536
537         musb_read_fifo(hw_ep, length, buf);
538
539         csr = musb_readw(epio, MUSB_RXCSR);
540         csr |= MUSB_RXCSR_H_WZC_BITS;
541         if (unlikely(do_flush))
542                 musb_h_flush_rxfifo(hw_ep, csr);
543         else {
544                 /* REVISIT this assumes AUTOCLEAR is never set */
545                 csr &= ~(MUSB_RXCSR_RXPKTRDY | MUSB_RXCSR_H_REQPKT);
546                 if (!done)
547                         csr |= MUSB_RXCSR_H_REQPKT;
548                 musb_writew(epio, MUSB_RXCSR, csr);
549         }
550
551         return done;
552 }
553
554 /* we don't always need to reinit a given side of an endpoint...
555  * when we do, use tx/rx reinit routine and then construct a new CSR
556  * to address data toggle, NYET, and DMA or PIO.
557  *
558  * it's possible that driver bugs (especially for DMA) or aborting a
559  * transfer might have left the endpoint busier than it should be.
560  * the busy/not-empty tests are basically paranoia.
561  */
562 static void
563 musb_rx_reinit(struct musb *musb, struct musb_qh *qh, struct musb_hw_ep *ep)
564 {
565         u16     csr;
566
567         /* NOTE:  we know the "rx" fifo reinit never triggers for ep0.
568          * That always uses tx_reinit since ep0 repurposes TX register
569          * offsets; the initial SETUP packet is also a kind of OUT.
570          */
571
572         /* if programmed for Tx, put it in RX mode */
573         if (ep->is_shared_fifo) {
574                 csr = musb_readw(ep->regs, MUSB_TXCSR);
575                 if (csr & MUSB_TXCSR_MODE) {
576                         musb_h_tx_flush_fifo(ep);
577                         musb_writew(ep->regs, MUSB_TXCSR,
578                                         MUSB_TXCSR_FRCDATATOG);
579                 }
580                 /* clear mode (and everything else) to enable Rx */
581                 musb_writew(ep->regs, MUSB_TXCSR, 0);
582
583         /* scrub all previous state, clearing toggle */
584         } else {
585                 csr = musb_readw(ep->regs, MUSB_RXCSR);
586                 if (csr & MUSB_RXCSR_RXPKTRDY)
587                         WARNING("rx%d, packet/%d ready?\n", ep->epnum,
588                                 musb_readw(ep->regs, MUSB_RXCOUNT));
589
590                 musb_h_flush_rxfifo(ep, MUSB_RXCSR_CLRDATATOG);
591         }
592
593         /* target addr and (for multipoint) hub addr/port */
594         if (musb->is_multipoint) {
595                 musb_writeb(ep->target_regs, MUSB_RXFUNCADDR,
596                         qh->addr_reg);
597                 musb_writeb(ep->target_regs, MUSB_RXHUBADDR,
598                         qh->h_addr_reg);
599                 musb_writeb(ep->target_regs, MUSB_RXHUBPORT,
600                         qh->h_port_reg);
601         } else
602                 musb_writeb(musb->mregs, MUSB_FADDR, qh->addr_reg);
603
604         /* protocol/endpoint, interval/NAKlimit, i/o size */
605         musb_writeb(ep->regs, MUSB_RXTYPE, qh->type_reg);
606         musb_writeb(ep->regs, MUSB_RXINTERVAL, qh->intv_reg);
607         /* NOTE: bulk combining rewrites high bits of maxpacket */
608         musb_writew(ep->regs, MUSB_RXMAXP, qh->maxpacket);
609
610         ep->rx_reinit = 0;
611 }
612
613
614 /*
615  * Program an HDRC endpoint as per the given URB
616  * Context: irqs blocked, controller lock held
617  */
618 static void musb_ep_program(struct musb *musb, u8 epnum,
619                         struct urb *urb, unsigned int is_out,
620                         u8 *buf, u32 len)
621 {
622         struct dma_controller   *dma_controller;
623         struct dma_channel      *dma_channel;
624         u8                      dma_ok;
625         void __iomem            *mbase = musb->mregs;
626         struct musb_hw_ep       *hw_ep = musb->endpoints + epnum;
627         void __iomem            *epio = hw_ep->regs;
628         struct musb_qh          *qh;
629         u16                     packet_sz;
630
631         if (!is_out || hw_ep->is_shared_fifo)
632                 qh = hw_ep->in_qh;
633         else
634                 qh = hw_ep->out_qh;
635
636         packet_sz = qh->maxpacket;
637
638         DBG(3, "%s hw%d urb %p spd%d dev%d ep%d%s "
639                                 "h_addr%02x h_port%02x bytes %d\n",
640                         is_out ? "-->" : "<--",
641                         epnum, urb, urb->dev->speed,
642                         qh->addr_reg, qh->epnum, is_out ? "out" : "in",
643                         qh->h_addr_reg, qh->h_port_reg,
644                         len);
645
646         musb_ep_select(mbase, epnum);
647
648         /* candidate for DMA? */
649         dma_controller = musb->dma_controller;
650         if (is_dma_capable() && epnum && dma_controller) {
651                 dma_channel = is_out ? hw_ep->tx_channel : hw_ep->rx_channel;
652                 if (!dma_channel) {
653                         dma_channel = dma_controller->channel_alloc(
654                                         dma_controller, hw_ep, is_out);
655                         if (is_out)
656                                 hw_ep->tx_channel = dma_channel;
657                         else
658                                 hw_ep->rx_channel = dma_channel;
659                 }
660         } else
661                 dma_channel = NULL;
662
663         /* make sure we clear DMAEnab, autoSet bits from previous run */
664
665         /* OUT/transmit/EP0 or IN/receive? */
666         if (is_out) {
667                 u16     csr;
668                 u16     int_txe;
669                 u16     load_count;
670
671                 csr = musb_readw(epio, MUSB_TXCSR);
672
673                 /* disable interrupt in case we flush */
674                 int_txe = musb_readw(mbase, MUSB_INTRTXE);
675                 musb_writew(mbase, MUSB_INTRTXE, int_txe & ~(1 << epnum));
676
677                 /* general endpoint setup */
678                 if (epnum) {
679                         /* ASSERT:  TXCSR_DMAENAB was already cleared */
680
681                         /* flush all old state, set default */
682                         musb_h_tx_flush_fifo(hw_ep);
683                         csr &= ~(MUSB_TXCSR_H_NAKTIMEOUT
684                                         | MUSB_TXCSR_DMAMODE
685                                         | MUSB_TXCSR_FRCDATATOG
686                                         | MUSB_TXCSR_H_RXSTALL
687                                         | MUSB_TXCSR_H_ERROR
688                                         | MUSB_TXCSR_TXPKTRDY
689                                         );
690                         csr |= MUSB_TXCSR_MODE;
691
692                         if (usb_gettoggle(urb->dev,
693                                         qh->epnum, 1))
694                                 csr |= MUSB_TXCSR_H_WR_DATATOGGLE
695                                         | MUSB_TXCSR_H_DATATOGGLE;
696                         else
697                                 csr |= MUSB_TXCSR_CLRDATATOG;
698
699                         /* twice in case of double packet buffering */
700                         musb_writew(epio, MUSB_TXCSR, csr);
701                         /* REVISIT may need to clear FLUSHFIFO ... */
702                         musb_writew(epio, MUSB_TXCSR, csr);
703                         csr = musb_readw(epio, MUSB_TXCSR);
704                 } else {
705                         /* endpoint 0: just flush */
706                         musb_writew(epio, MUSB_CSR0,
707                                 csr | MUSB_CSR0_FLUSHFIFO);
708                         musb_writew(epio, MUSB_CSR0,
709                                 csr | MUSB_CSR0_FLUSHFIFO);
710                 }
711
712                 /* target addr and (for multipoint) hub addr/port */
713                 if (musb->is_multipoint) {
714                         musb_writeb(mbase,
715                                 MUSB_BUSCTL_OFFSET(epnum, MUSB_TXFUNCADDR),
716                                 qh->addr_reg);
717                         musb_writeb(mbase,
718                                 MUSB_BUSCTL_OFFSET(epnum, MUSB_TXHUBADDR),
719                                 qh->h_addr_reg);
720                         musb_writeb(mbase,
721                                 MUSB_BUSCTL_OFFSET(epnum, MUSB_TXHUBPORT),
722                                 qh->h_port_reg);
723 /* FIXME if !epnum, do the same for RX ... */
724                 } else
725                         musb_writeb(mbase, MUSB_FADDR, qh->addr_reg);
726
727                 /* protocol/endpoint/interval/NAKlimit */
728                 if (epnum) {
729                         musb_writeb(epio, MUSB_TXTYPE, qh->type_reg);
730                         if (can_bulk_split(musb, qh->type))
731                                 musb_writew(epio, MUSB_TXMAXP,
732                                         packet_sz
733                                         | ((hw_ep->max_packet_sz_tx /
734                                                 packet_sz) - 1) << 11);
735                         else
736                                 musb_writew(epio, MUSB_TXMAXP,
737                                         packet_sz);
738                         musb_writeb(epio, MUSB_TXINTERVAL, qh->intv_reg);
739                 } else {
740                         musb_writeb(epio, MUSB_NAKLIMIT0, qh->intv_reg);
741                         if (musb->is_multipoint)
742                                 musb_writeb(epio, MUSB_TYPE0,
743                                                 qh->type_reg);
744                 }
745
746                 if (can_bulk_split(musb, qh->type))
747                         load_count = min((u32) hw_ep->max_packet_sz_tx,
748                                                 len);
749                 else
750                         load_count = min((u32) packet_sz, len);
751
752 #ifdef CONFIG_USB_INVENTRA_DMA
753                 if (dma_channel) {
754
755                         /* clear previous state */
756                         csr = musb_readw(epio, MUSB_TXCSR);
757                         csr &= ~(MUSB_TXCSR_AUTOSET
758                                 | MUSB_TXCSR_DMAMODE
759                                 | MUSB_TXCSR_DMAENAB);
760                         csr |= MUSB_TXCSR_MODE;
761                         musb_writew(epio, MUSB_TXCSR,
762                                 csr | MUSB_TXCSR_MODE);
763
764                         qh->segsize = min(len, dma_channel->max_len);
765
766                         if (qh->segsize <= packet_sz)
767                                 dma_channel->desired_mode = 0;
768                         else
769                                 dma_channel->desired_mode = 1;
770
771
772                         if (dma_channel->desired_mode == 0) {
773                                 csr &= ~(MUSB_TXCSR_AUTOSET
774                                         | MUSB_TXCSR_DMAMODE);
775                                 csr |= (MUSB_TXCSR_DMAENAB);
776                                         /* against programming guide */
777                         } else
778                                 csr |= (MUSB_TXCSR_AUTOSET
779                                         | MUSB_TXCSR_DMAENAB
780                                         | MUSB_TXCSR_DMAMODE);
781
782                         musb_writew(epio, MUSB_TXCSR, csr);
783
784                         dma_ok = dma_controller->channel_program(
785                                         dma_channel, packet_sz,
786                                         dma_channel->desired_mode,
787                                         urb->transfer_dma,
788                                         qh->segsize);
789                         if (dma_ok) {
790                                 load_count = 0;
791                         } else {
792                                 dma_controller->channel_release(dma_channel);
793                                 if (is_out)
794                                         hw_ep->tx_channel = NULL;
795                                 else
796                                         hw_ep->rx_channel = NULL;
797                                 dma_channel = NULL;
798                         }
799                 }
800 #endif
801
802                 /* candidate for DMA */
803                 if ((is_cppi_enabled() || tusb_dma_omap()) && dma_channel) {
804
805                         /* program endpoint CSRs first, then setup DMA.
806                          * assume CPPI setup succeeds.
807                          * defer enabling dma.
808                          */
809                         csr = musb_readw(epio, MUSB_TXCSR);
810                         csr &= ~(MUSB_TXCSR_AUTOSET
811                                         | MUSB_TXCSR_DMAMODE
812                                         | MUSB_TXCSR_DMAENAB);
813                         csr |= MUSB_TXCSR_MODE;
814                         musb_writew(epio, MUSB_TXCSR,
815                                 csr | MUSB_TXCSR_MODE);
816
817                         dma_channel->actual_len = 0L;
818                         qh->segsize = len;
819
820                         /* TX uses "rndis" mode automatically, but needs help
821                          * to identify the zero-length-final-packet case.
822                          */
823                         dma_ok = dma_controller->channel_program(
824                                         dma_channel, packet_sz,
825                                         (urb->transfer_flags
826                                                         & URB_ZERO_PACKET)
827                                                 == URB_ZERO_PACKET,
828                                         urb->transfer_dma,
829                                         qh->segsize);
830                         if (dma_ok) {
831                                 load_count = 0;
832                         } else {
833                                 dma_controller->channel_release(dma_channel);
834                                 dma_channel = hw_ep->tx_channel = NULL;
835
836                                 /* REVISIT there's an error path here that
837                                  * needs handling:  can't do dma, but
838                                  * there's no pio buffer address...
839                                  */
840                         }
841                 }
842
843                 if (load_count) {
844                         /* ASSERT:  TXCSR_DMAENAB was already cleared */
845
846                         /* PIO to load FIFO */
847                         qh->segsize = load_count;
848                         musb_write_fifo(hw_ep, load_count, buf);
849                         csr = musb_readw(epio, MUSB_TXCSR);
850                         csr &= ~(MUSB_TXCSR_DMAENAB
851                                 | MUSB_TXCSR_DMAMODE
852                                 | MUSB_TXCSR_AUTOSET);
853                         /* write CSR */
854                         csr |= MUSB_TXCSR_MODE;
855
856                         if (epnum)
857                                 musb_writew(epio, MUSB_TXCSR, csr);
858                 }
859
860                 /* re-enable interrupt */
861                 musb_writew(mbase, MUSB_INTRTXE, int_txe);
862
863         /* IN/receive */
864         } else {
865                 u16     csr;
866
867                 if (hw_ep->rx_reinit) {
868                         musb_rx_reinit(musb, qh, hw_ep);
869
870                         /* init new state: toggle and NYET, maybe DMA later */
871                         if (usb_gettoggle(urb->dev, qh->epnum, 0))
872                                 csr = MUSB_RXCSR_H_WR_DATATOGGLE
873                                         | MUSB_RXCSR_H_DATATOGGLE;
874                         else
875                                 csr = 0;
876                         if (qh->type == USB_ENDPOINT_XFER_INT)
877                                 csr |= MUSB_RXCSR_DISNYET;
878
879                 } else {
880                         csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
881
882                         if (csr & (MUSB_RXCSR_RXPKTRDY
883                                         | MUSB_RXCSR_DMAENAB
884                                         | MUSB_RXCSR_H_REQPKT))
885                                 ERR("broken !rx_reinit, ep%d csr %04x\n",
886                                                 hw_ep->epnum, csr);
887
888                         /* scrub any stale state, leaving toggle alone */
889                         csr &= MUSB_RXCSR_DISNYET;
890                 }
891
892                 /* kick things off */
893
894                 if ((is_cppi_enabled() || tusb_dma_omap()) && dma_channel) {
895                         /* candidate for DMA */
896                         if (dma_channel) {
897                                 dma_channel->actual_len = 0L;
898                                 qh->segsize = len;
899
900                                 /* AUTOREQ is in a DMA register */
901                                 musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
902                                 csr = musb_readw(hw_ep->regs,
903                                                 MUSB_RXCSR);
904
905                                 /* unless caller treats short rx transfers as
906                                  * errors, we dare not queue multiple transfers.
907                                  */
908                                 dma_ok = dma_controller->channel_program(
909                                                 dma_channel, packet_sz,
910                                                 !(urb->transfer_flags
911                                                         & URB_SHORT_NOT_OK),
912                                                 urb->transfer_dma,
913                                                 qh->segsize);
914                                 if (!dma_ok) {
915                                         dma_controller->channel_release(
916                                                         dma_channel);
917                                         dma_channel = hw_ep->rx_channel = NULL;
918                                 } else
919                                         csr |= MUSB_RXCSR_DMAENAB;
920                         }
921                 }
922
923                 csr |= MUSB_RXCSR_H_REQPKT;
924                 DBG(7, "RXCSR%d := %04x\n", epnum, csr);
925                 musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
926                 csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
927         }
928 }
929
930
931 /*
932  * Service the default endpoint (ep0) as host.
933  * Return true until it's time to start the status stage.
934  */
935 static bool musb_h_ep0_continue(struct musb *musb, u16 len, struct urb *urb)
936 {
937         bool                     more = false;
938         u8                      *fifo_dest = NULL;
939         u16                     fifo_count = 0;
940         struct musb_hw_ep       *hw_ep = musb->control_ep;
941         struct musb_qh          *qh = hw_ep->in_qh;
942         struct usb_ctrlrequest  *request;
943
944         switch (musb->ep0_stage) {
945         case MUSB_EP0_IN:
946                 fifo_dest = urb->transfer_buffer + urb->actual_length;
947                 fifo_count = min(len, ((u16) (urb->transfer_buffer_length
948                                         - urb->actual_length)));
949                 if (fifo_count < len)
950                         urb->status = -EOVERFLOW;
951
952                 musb_read_fifo(hw_ep, fifo_count, fifo_dest);
953
954                 urb->actual_length += fifo_count;
955                 if (len < qh->maxpacket) {
956                         /* always terminate on short read; it's
957                          * rarely reported as an error.
958                          */
959                 } else if (urb->actual_length <
960                                 urb->transfer_buffer_length)
961                         more = true;
962                 break;
963         case MUSB_EP0_START:
964                 request = (struct usb_ctrlrequest *) urb->setup_packet;
965
966                 if (!request->wLength) {
967                         DBG(4, "start no-DATA\n");
968                         break;
969                 } else if (request->bRequestType & USB_DIR_IN) {
970                         DBG(4, "start IN-DATA\n");
971                         musb->ep0_stage = MUSB_EP0_IN;
972                         more = true;
973                         break;
974                 } else {
975                         DBG(4, "start OUT-DATA\n");
976                         musb->ep0_stage = MUSB_EP0_OUT;
977                         more = true;
978                 }
979                 /* FALLTHROUGH */
980         case MUSB_EP0_OUT:
981                 fifo_count = min(qh->maxpacket, ((u16)
982                                 (urb->transfer_buffer_length
983                                 - urb->actual_length)));
984
985                 if (fifo_count) {
986                         fifo_dest = (u8 *) (urb->transfer_buffer
987                                         + urb->actual_length);
988                         DBG(3, "Sending %d bytes to %p\n",
989                                         fifo_count, fifo_dest);
990                         musb_write_fifo(hw_ep, fifo_count, fifo_dest);
991
992                         urb->actual_length += fifo_count;
993                         more = true;
994                 }
995                 break;
996         default:
997                 ERR("bogus ep0 stage %d\n", musb->ep0_stage);
998                 break;
999         }
1000
1001         return more;
1002 }
1003
1004 /*
1005  * Handle default endpoint interrupt as host. Only called in IRQ time
1006  * from the LinuxIsr() interrupt service routine.
1007  *
1008  * called with controller irqlocked
1009  */
1010 irqreturn_t musb_h_ep0_irq(struct musb *musb)
1011 {
1012         struct urb              *urb;
1013         u16                     csr, len;
1014         int                     status = 0;
1015         void __iomem            *mbase = musb->mregs;
1016         struct musb_hw_ep       *hw_ep = musb->control_ep;
1017         void __iomem            *epio = hw_ep->regs;
1018         struct musb_qh          *qh = hw_ep->in_qh;
1019         bool                    complete = false;
1020         irqreturn_t             retval = IRQ_NONE;
1021
1022         /* ep0 only has one queue, "in" */
1023         urb = next_urb(qh);
1024
1025         musb_ep_select(mbase, 0);
1026         csr = musb_readw(epio, MUSB_CSR0);
1027         len = (csr & MUSB_CSR0_RXPKTRDY)
1028                         ? musb_readb(epio, MUSB_COUNT0)
1029                         : 0;
1030
1031         DBG(4, "<== csr0 %04x, qh %p, count %d, urb %p, stage %d\n",
1032                 csr, qh, len, urb, musb->ep0_stage);
1033
1034         /* if we just did status stage, we are done */
1035         if (MUSB_EP0_STATUS == musb->ep0_stage) {
1036                 retval = IRQ_HANDLED;
1037                 complete = true;
1038         }
1039
1040         /* prepare status */
1041         if (csr & MUSB_CSR0_H_RXSTALL) {
1042                 DBG(6, "STALLING ENDPOINT\n");
1043                 status = -EPIPE;
1044
1045         } else if (csr & MUSB_CSR0_H_ERROR) {
1046                 DBG(2, "no response, csr0 %04x\n", csr);
1047                 status = -EPROTO;
1048
1049         } else if (csr & MUSB_CSR0_H_NAKTIMEOUT) {
1050                 DBG(2, "control NAK timeout\n");
1051
1052                 /* NOTE:  this code path would be a good place to PAUSE a
1053                  * control transfer, if another one is queued, so that
1054                  * ep0 is more likely to stay busy.
1055                  *
1056                  * if (qh->ring.next != &musb->control), then
1057                  * we have a candidate... NAKing is *NOT* an error
1058                  */
1059                 musb_writew(epio, MUSB_CSR0, 0);
1060                 retval = IRQ_HANDLED;
1061         }
1062
1063         if (status) {
1064                 DBG(6, "aborting\n");
1065                 retval = IRQ_HANDLED;
1066                 if (urb)
1067                         urb->status = status;
1068                 complete = true;
1069
1070                 /* use the proper sequence to abort the transfer */
1071                 if (csr & MUSB_CSR0_H_REQPKT) {
1072                         csr &= ~MUSB_CSR0_H_REQPKT;
1073                         musb_writew(epio, MUSB_CSR0, csr);
1074                         csr &= ~MUSB_CSR0_H_NAKTIMEOUT;
1075                         musb_writew(epio, MUSB_CSR0, csr);
1076                 } else {
1077                         csr |= MUSB_CSR0_FLUSHFIFO;
1078                         musb_writew(epio, MUSB_CSR0, csr);
1079                         musb_writew(epio, MUSB_CSR0, csr);
1080                         csr &= ~MUSB_CSR0_H_NAKTIMEOUT;
1081                         musb_writew(epio, MUSB_CSR0, csr);
1082                 }
1083
1084                 musb_writeb(epio, MUSB_NAKLIMIT0, 0);
1085
1086                 /* clear it */
1087                 musb_writew(epio, MUSB_CSR0, 0);
1088         }
1089
1090         if (unlikely(!urb)) {
1091                 /* stop endpoint since we have no place for its data, this
1092                  * SHOULD NEVER HAPPEN! */
1093                 ERR("no URB for end 0\n");
1094
1095                 musb_writew(epio, MUSB_CSR0, MUSB_CSR0_FLUSHFIFO);
1096                 musb_writew(epio, MUSB_CSR0, MUSB_CSR0_FLUSHFIFO);
1097                 musb_writew(epio, MUSB_CSR0, 0);
1098
1099                 goto done;
1100         }
1101
1102         if (!complete) {
1103                 /* call common logic and prepare response */
1104                 if (musb_h_ep0_continue(musb, len, urb)) {
1105                         /* more packets required */
1106                         csr = (MUSB_EP0_IN == musb->ep0_stage)
1107                                 ?  MUSB_CSR0_H_REQPKT : MUSB_CSR0_TXPKTRDY;
1108                 } else {
1109                         /* data transfer complete; perform status phase */
1110                         if (usb_pipeout(urb->pipe)
1111                                         || !urb->transfer_buffer_length)
1112                                 csr = MUSB_CSR0_H_STATUSPKT
1113                                         | MUSB_CSR0_H_REQPKT;
1114                         else
1115                                 csr = MUSB_CSR0_H_STATUSPKT
1116                                         | MUSB_CSR0_TXPKTRDY;
1117
1118                         /* flag status stage */
1119                         musb->ep0_stage = MUSB_EP0_STATUS;
1120
1121                         DBG(5, "ep0 STATUS, csr %04x\n", csr);
1122
1123                 }
1124                 musb_writew(epio, MUSB_CSR0, csr);
1125                 retval = IRQ_HANDLED;
1126         } else
1127                 musb->ep0_stage = MUSB_EP0_IDLE;
1128
1129         /* call completion handler if done */
1130         if (complete)
1131                 musb_advance_schedule(musb, urb, hw_ep, 1);
1132 done:
1133         return retval;
1134 }
1135
1136
1137 #ifdef CONFIG_USB_INVENTRA_DMA
1138
1139 /* Host side TX (OUT) using Mentor DMA works as follows:
1140         submit_urb ->
1141                 - if queue was empty, Program Endpoint
1142                 - ... which starts DMA to fifo in mode 1 or 0
1143
1144         DMA Isr (transfer complete) -> TxAvail()
1145                 - Stop DMA (~DmaEnab)   (<--- Alert ... currently happens
1146                                         only in musb_cleanup_urb)
1147                 - TxPktRdy has to be set in mode 0 or for
1148                         short packets in mode 1.
1149 */
1150
1151 #endif
1152
1153 /* Service a Tx-Available or dma completion irq for the endpoint */
1154 void musb_host_tx(struct musb *musb, u8 epnum)
1155 {
1156         int                     pipe;
1157         bool                    done = false;
1158         u16                     tx_csr;
1159         size_t                  wLength = 0;
1160         u8                      *buf = NULL;
1161         struct urb              *urb;
1162         struct musb_hw_ep       *hw_ep = musb->endpoints + epnum;
1163         void __iomem            *epio = hw_ep->regs;
1164         struct musb_qh          *qh = hw_ep->out_qh;
1165         u32                     status = 0;
1166         void __iomem            *mbase = musb->mregs;
1167         struct dma_channel      *dma;
1168
1169         urb = next_urb(qh);
1170
1171         musb_ep_select(mbase, epnum);
1172         tx_csr = musb_readw(epio, MUSB_TXCSR);
1173
1174         /* with CPPI, DMA sometimes triggers "extra" irqs */
1175         if (!urb) {
1176                 DBG(4, "extra TX%d ready, csr %04x\n", epnum, tx_csr);
1177                 goto finish;
1178         }
1179
1180         pipe = urb->pipe;
1181         dma = is_dma_capable() ? hw_ep->tx_channel : NULL;
1182         DBG(4, "OUT/TX%d end, csr %04x%s\n", epnum, tx_csr,
1183                         dma ? ", dma" : "");
1184
1185         /* check for errors */
1186         if (tx_csr & MUSB_TXCSR_H_RXSTALL) {
1187                 /* dma was disabled, fifo flushed */
1188                 DBG(3, "TX end %d stall\n", epnum);
1189
1190                 /* stall; record URB status */
1191                 status = -EPIPE;
1192
1193         } else if (tx_csr & MUSB_TXCSR_H_ERROR) {
1194                 /* (NON-ISO) dma was disabled, fifo flushed */
1195                 DBG(3, "TX 3strikes on ep=%d\n", epnum);
1196
1197                 status = -ETIMEDOUT;
1198
1199         } else if (tx_csr & MUSB_TXCSR_H_NAKTIMEOUT) {
1200                 DBG(6, "TX end=%d device not responding\n", epnum);
1201
1202                 /* NOTE:  this code path would be a good place to PAUSE a
1203                  * transfer, if there's some other (nonperiodic) tx urb
1204                  * that could use this fifo.  (dma complicates it...)
1205                  *
1206                  * if (bulk && qh->ring.next != &musb->out_bulk), then
1207                  * we have a candidate... NAKing is *NOT* an error
1208                  */
1209                 musb_ep_select(mbase, epnum);
1210                 musb_writew(epio, MUSB_TXCSR,
1211                                 MUSB_TXCSR_H_WZC_BITS
1212                                 | MUSB_TXCSR_TXPKTRDY);
1213                 goto finish;
1214         }
1215
1216         if (status) {
1217                 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1218                         dma->status = MUSB_DMA_STATUS_CORE_ABORT;
1219                         (void) musb->dma_controller->channel_abort(dma);
1220                 }
1221
1222                 /* do the proper sequence to abort the transfer in the
1223                  * usb core; the dma engine should already be stopped.
1224                  */
1225                 musb_h_tx_flush_fifo(hw_ep);
1226                 tx_csr &= ~(MUSB_TXCSR_AUTOSET
1227                                 | MUSB_TXCSR_DMAENAB
1228                                 | MUSB_TXCSR_H_ERROR
1229                                 | MUSB_TXCSR_H_RXSTALL
1230                                 | MUSB_TXCSR_H_NAKTIMEOUT
1231                                 );
1232
1233                 musb_ep_select(mbase, epnum);
1234                 musb_writew(epio, MUSB_TXCSR, tx_csr);
1235                 /* REVISIT may need to clear FLUSHFIFO ... */
1236                 musb_writew(epio, MUSB_TXCSR, tx_csr);
1237                 musb_writeb(epio, MUSB_TXINTERVAL, 0);
1238
1239                 done = true;
1240         }
1241
1242         /* second cppi case */
1243         if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1244                 DBG(4, "extra TX%d ready, csr %04x\n", epnum, tx_csr);
1245                 goto finish;
1246
1247         }
1248
1249         /* REVISIT this looks wrong... */
1250         if (!status || dma || usb_pipeisoc(pipe)) {
1251                 if (dma)
1252                         wLength = dma->actual_len;
1253                 else
1254                         wLength = qh->segsize;
1255                 qh->offset += wLength;
1256
1257                 if (usb_pipeisoc(pipe)) {
1258                         struct usb_iso_packet_descriptor        *d;
1259
1260                         d = urb->iso_frame_desc + qh->iso_idx;
1261                         d->actual_length = qh->segsize;
1262                         if (++qh->iso_idx >= urb->number_of_packets) {
1263                                 done = true;
1264                         } else {
1265                                 d++;
1266                                 buf = urb->transfer_buffer + d->offset;
1267                                 wLength = d->length;
1268                         }
1269                 } else if (dma) {
1270                         done = true;
1271                 } else {
1272                         /* see if we need to send more data, or ZLP */
1273                         if (qh->segsize < qh->maxpacket)
1274                                 done = true;
1275                         else if (qh->offset == urb->transfer_buffer_length
1276                                         && !(urb->transfer_flags
1277                                                 & URB_ZERO_PACKET))
1278                                 done = true;
1279                         if (!done) {
1280                                 buf = urb->transfer_buffer
1281                                                 + qh->offset;
1282                                 wLength = urb->transfer_buffer_length
1283                                                 - qh->offset;
1284                         }
1285                 }
1286         }
1287
1288         /* urb->status != -EINPROGRESS means request has been faulted,
1289          * so we must abort this transfer after cleanup
1290          */
1291         if (urb->status != -EINPROGRESS) {
1292                 done = true;
1293                 if (status == 0)
1294                         status = urb->status;
1295         }
1296
1297         if (done) {
1298                 /* set status */
1299                 urb->status = status;
1300                 urb->actual_length = qh->offset;
1301                 musb_advance_schedule(musb, urb, hw_ep, USB_DIR_OUT);
1302
1303         } else if (!(tx_csr & MUSB_TXCSR_DMAENAB)) {
1304                 /* WARN_ON(!buf); */
1305
1306                 /* REVISIT:  some docs say that when hw_ep->tx_double_buffered,
1307                  * (and presumably, fifo is not half-full) we should write TWO
1308                  * packets before updating TXCSR ... other docs disagree ...
1309                  */
1310                 /* PIO:  start next packet in this URB */
1311                 wLength = min(qh->maxpacket, (u16) wLength);
1312                 musb_write_fifo(hw_ep, wLength, buf);
1313                 qh->segsize = wLength;
1314
1315                 musb_ep_select(mbase, epnum);
1316                 musb_writew(epio, MUSB_TXCSR,
1317                                 MUSB_TXCSR_H_WZC_BITS | MUSB_TXCSR_TXPKTRDY);
1318         } else
1319                 DBG(1, "not complete, but dma enabled?\n");
1320
1321 finish:
1322         return;
1323 }
1324
1325
1326 #ifdef CONFIG_USB_INVENTRA_DMA
1327
1328 /* Host side RX (IN) using Mentor DMA works as follows:
1329         submit_urb ->
1330                 - if queue was empty, ProgramEndpoint
1331                 - first IN token is sent out (by setting ReqPkt)
1332         LinuxIsr -> RxReady()
1333         /\      => first packet is received
1334         |       - Set in mode 0 (DmaEnab, ~ReqPkt)
1335         |               -> DMA Isr (transfer complete) -> RxReady()
1336         |                   - Ack receive (~RxPktRdy), turn off DMA (~DmaEnab)
1337         |                   - if urb not complete, send next IN token (ReqPkt)
1338         |                          |            else complete urb.
1339         |                          |
1340         ---------------------------
1341  *
1342  * Nuances of mode 1:
1343  *      For short packets, no ack (+RxPktRdy) is sent automatically
1344  *      (even if AutoClear is ON)
1345  *      For full packets, ack (~RxPktRdy) and next IN token (+ReqPkt) is sent
1346  *      automatically => major problem, as collecting the next packet becomes
1347  *      difficult. Hence mode 1 is not used.
1348  *
1349  * REVISIT
1350  *      All we care about at this driver level is that
1351  *       (a) all URBs terminate with REQPKT cleared and fifo(s) empty;
1352  *       (b) termination conditions are: short RX, or buffer full;
1353  *       (c) fault modes include
1354  *           - iff URB_SHORT_NOT_OK, short RX status is -EREMOTEIO.
1355  *             (and that endpoint's dma queue stops immediately)
1356  *           - overflow (full, PLUS more bytes in the terminal packet)
1357  *
1358  *      So for example, usb-storage sets URB_SHORT_NOT_OK, and would
1359  *      thus be a great candidate for using mode 1 ... for all but the
1360  *      last packet of one URB's transfer.
1361  */
1362
1363 #endif
1364
1365 /*
1366  * Service an RX interrupt for the given IN endpoint; docs cover bulk, iso,
1367  * and high-bandwidth IN transfer cases.
1368  */
1369 void musb_host_rx(struct musb *musb, u8 epnum)
1370 {
1371         struct urb              *urb;
1372         struct musb_hw_ep       *hw_ep = musb->endpoints + epnum;
1373         void __iomem            *epio = hw_ep->regs;
1374         struct musb_qh          *qh = hw_ep->in_qh;
1375         size_t                  xfer_len;
1376         void __iomem            *mbase = musb->mregs;
1377         int                     pipe;
1378         u16                     rx_csr, val;
1379         bool                    iso_err = false;
1380         bool                    done = false;
1381         u32                     status;
1382         struct dma_channel      *dma;
1383
1384         musb_ep_select(mbase, epnum);
1385
1386         urb = next_urb(qh);
1387         dma = is_dma_capable() ? hw_ep->rx_channel : NULL;
1388         status = 0;
1389         xfer_len = 0;
1390
1391         val = rx_csr = musb_readw(epio, MUSB_RXCSR);
1392
1393         if (unlikely(!urb)) {
1394                 /* REVISIT -- THIS SHOULD NEVER HAPPEN ... but, at least
1395                  * usbtest #11 (unlinks) triggers it regularly, sometimes
1396                  * with fifo full.  (Only with DMA??)
1397                  */
1398                 DBG(3, "BOGUS RX%d ready, csr %04x, count %d\n", epnum, val,
1399                         musb_readw(epio, MUSB_RXCOUNT));
1400                 musb_h_flush_rxfifo(hw_ep, MUSB_RXCSR_CLRDATATOG);
1401                 return;
1402         }
1403
1404         pipe = urb->pipe;
1405
1406         DBG(5, "<== hw %d rxcsr %04x, urb actual %d (+dma %zu)\n",
1407                 epnum, rx_csr, urb->actual_length,
1408                 dma ? dma->actual_len : 0);
1409
1410         /* check for errors, concurrent stall & unlink is not really
1411          * handled yet! */
1412         if (rx_csr & MUSB_RXCSR_H_RXSTALL) {
1413                 DBG(3, "RX end %d STALL\n", epnum);
1414
1415                 /* stall; record URB status */
1416                 status = -EPIPE;
1417
1418         } else if (rx_csr & MUSB_RXCSR_H_ERROR) {
1419                 DBG(3, "end %d RX proto error\n", epnum);
1420
1421                 status = -EPROTO;
1422                 musb_writeb(epio, MUSB_RXINTERVAL, 0);
1423
1424         } else if (rx_csr & MUSB_RXCSR_DATAERROR) {
1425
1426                 if (USB_ENDPOINT_XFER_ISOC != qh->type) {
1427                         /* NOTE this code path would be a good place to PAUSE a
1428                          * transfer, if there's some other (nonperiodic) rx urb
1429                          * that could use this fifo.  (dma complicates it...)
1430                          *
1431                          * if (bulk && qh->ring.next != &musb->in_bulk), then
1432                          * we have a candidate... NAKing is *NOT* an error
1433                          */
1434                         DBG(6, "RX end %d NAK timeout\n", epnum);
1435                         musb_ep_select(mbase, epnum);
1436                         musb_writew(epio, MUSB_RXCSR,
1437                                         MUSB_RXCSR_H_WZC_BITS
1438                                         | MUSB_RXCSR_H_REQPKT);
1439
1440                         goto finish;
1441                 } else {
1442                         DBG(4, "RX end %d ISO data error\n", epnum);
1443                         /* packet error reported later */
1444                         iso_err = true;
1445                 }
1446         }
1447
1448         /* faults abort the transfer */
1449         if (status) {
1450                 /* clean up dma and collect transfer count */
1451                 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1452                         dma->status = MUSB_DMA_STATUS_CORE_ABORT;
1453                         (void) musb->dma_controller->channel_abort(dma);
1454                         xfer_len = dma->actual_len;
1455                 }
1456                 musb_h_flush_rxfifo(hw_ep, MUSB_RXCSR_CLRDATATOG);
1457                 musb_writeb(epio, MUSB_RXINTERVAL, 0);
1458                 done = true;
1459                 goto finish;
1460         }
1461
1462         if (unlikely(dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY)) {
1463                 /* SHOULD NEVER HAPPEN ... but at least DaVinci has done it */
1464                 ERR("RX%d dma busy, csr %04x\n", epnum, rx_csr);
1465                 goto finish;
1466         }
1467
1468         /* thorough shutdown for now ... given more precise fault handling
1469          * and better queueing support, we might keep a DMA pipeline going
1470          * while processing this irq for earlier completions.
1471          */
1472
1473         /* FIXME this is _way_ too much in-line logic for Mentor DMA */
1474
1475 #ifndef CONFIG_USB_INVENTRA_DMA
1476         if (rx_csr & MUSB_RXCSR_H_REQPKT)  {
1477                 /* REVISIT this happened for a while on some short reads...
1478                  * the cleanup still needs investigation... looks bad...
1479                  * and also duplicates dma cleanup code above ... plus,
1480                  * shouldn't this be the "half full" double buffer case?
1481                  */
1482                 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1483                         dma->status = MUSB_DMA_STATUS_CORE_ABORT;
1484                         (void) musb->dma_controller->channel_abort(dma);
1485                         xfer_len = dma->actual_len;
1486                         done = true;
1487                 }
1488
1489                 DBG(2, "RXCSR%d %04x, reqpkt, len %zu%s\n", epnum, rx_csr,
1490                                 xfer_len, dma ? ", dma" : "");
1491                 rx_csr &= ~MUSB_RXCSR_H_REQPKT;
1492
1493                 musb_ep_select(mbase, epnum);
1494                 musb_writew(epio, MUSB_RXCSR,
1495                                 MUSB_RXCSR_H_WZC_BITS | rx_csr);
1496         }
1497 #endif
1498         if (dma && (rx_csr & MUSB_RXCSR_DMAENAB)) {
1499                 xfer_len = dma->actual_len;
1500
1501                 val &= ~(MUSB_RXCSR_DMAENAB
1502                         | MUSB_RXCSR_H_AUTOREQ
1503                         | MUSB_RXCSR_AUTOCLEAR
1504                         | MUSB_RXCSR_RXPKTRDY);
1505                 musb_writew(hw_ep->regs, MUSB_RXCSR, val);
1506
1507 #ifdef CONFIG_USB_INVENTRA_DMA
1508                 /* done if urb buffer is full or short packet is recd */
1509                 done = (urb->actual_length + xfer_len >=
1510                                 urb->transfer_buffer_length
1511                         || dma->actual_len < qh->maxpacket);
1512
1513                 /* send IN token for next packet, without AUTOREQ */
1514                 if (!done) {
1515                         val |= MUSB_RXCSR_H_REQPKT;
1516                         musb_writew(epio, MUSB_RXCSR,
1517                                 MUSB_RXCSR_H_WZC_BITS | val);
1518                 }
1519
1520                 DBG(4, "ep %d dma %s, rxcsr %04x, rxcount %d\n", epnum,
1521                         done ? "off" : "reset",
1522                         musb_readw(epio, MUSB_RXCSR),
1523                         musb_readw(epio, MUSB_RXCOUNT));
1524 #else
1525                 done = true;
1526 #endif
1527         } else if (urb->status == -EINPROGRESS) {
1528                 /* if no errors, be sure a packet is ready for unloading */
1529                 if (unlikely(!(rx_csr & MUSB_RXCSR_RXPKTRDY))) {
1530                         status = -EPROTO;
1531                         ERR("Rx interrupt with no errors or packet!\n");
1532
1533                         /* FIXME this is another "SHOULD NEVER HAPPEN" */
1534
1535 /* SCRUB (RX) */
1536                         /* do the proper sequence to abort the transfer */
1537                         musb_ep_select(mbase, epnum);
1538                         val &= ~MUSB_RXCSR_H_REQPKT;
1539                         musb_writew(epio, MUSB_RXCSR, val);
1540                         goto finish;
1541                 }
1542
1543                 /* we are expecting IN packets */
1544 #ifdef CONFIG_USB_INVENTRA_DMA
1545                 if (dma) {
1546                         struct dma_controller   *c;
1547                         u16                     rx_count;
1548                         int                     ret;
1549
1550                         rx_count = musb_readw(epio, MUSB_RXCOUNT);
1551
1552                         DBG(2, "RX%d count %d, buffer 0x%x len %d/%d\n",
1553                                         epnum, rx_count,
1554                                         urb->transfer_dma
1555                                                 + urb->actual_length,
1556                                         qh->offset,
1557                                         urb->transfer_buffer_length);
1558
1559                         c = musb->dma_controller;
1560
1561                         dma->desired_mode = 0;
1562 #ifdef USE_MODE1
1563                         /* because of the issue below, mode 1 will
1564                          * only rarely behave with correct semantics.
1565                          */
1566                         if ((urb->transfer_flags &
1567                                                 URB_SHORT_NOT_OK)
1568                                 && (urb->transfer_buffer_length -
1569                                                 urb->actual_length)
1570                                         > qh->maxpacket)
1571                                 dma->desired_mode = 1;
1572 #endif
1573
1574 /* Disadvantage of using mode 1:
1575  *      It's basically usable only for mass storage class; essentially all
1576  *      other protocols also terminate transfers on short packets.
1577  *
1578  * Details:
1579  *      An extra IN token is sent at the end of the transfer (due to AUTOREQ)
1580  *      If you try to use mode 1 for (transfer_buffer_length - 512), and try
1581  *      to use the extra IN token to grab the last packet using mode 0, then
1582  *      the problem is that you cannot be sure when the device will send the
1583  *      last packet and RxPktRdy set. Sometimes the packet is recd too soon
1584  *      such that it gets lost when RxCSR is re-set at the end of the mode 1
1585  *      transfer, while sometimes it is recd just a little late so that if you
1586  *      try to configure for mode 0 soon after the mode 1 transfer is
1587  *      completed, you will find rxcount 0. Okay, so you might think why not
1588  *      wait for an interrupt when the pkt is recd. Well, you won't get any!
1589  */
1590
1591                         val = musb_readw(epio, MUSB_RXCSR);
1592                         val &= ~MUSB_RXCSR_H_REQPKT;
1593
1594                         if (dma->desired_mode == 0)
1595                                 val &= ~MUSB_RXCSR_H_AUTOREQ;
1596                         else
1597                                 val |= MUSB_RXCSR_H_AUTOREQ;
1598                         val |= MUSB_RXCSR_AUTOCLEAR | MUSB_RXCSR_DMAENAB;
1599
1600                         musb_writew(epio, MUSB_RXCSR,
1601                                 MUSB_RXCSR_H_WZC_BITS | val);
1602
1603                         /* REVISIT if when actual_length != 0,
1604                          * transfer_buffer_length needs to be
1605                          * adjusted first...
1606                          */
1607                         ret = c->channel_program(
1608                                 dma, qh->maxpacket,
1609                                 dma->desired_mode,
1610                                 urb->transfer_dma
1611                                         + urb->actual_length,
1612                                 (dma->desired_mode == 0)
1613                                         ? rx_count
1614                                         : urb->transfer_buffer_length);
1615
1616                         if (!ret) {
1617                                 c->channel_release(dma);
1618                                 dma = hw_ep->rx_channel = NULL;
1619                                 /* REVISIT reset CSR */
1620                         }
1621                 }
1622 #endif  /* Mentor DMA */
1623
1624                 if (!dma) {
1625                         done = musb_host_packet_rx(musb, urb,
1626                                         epnum, iso_err);
1627                         DBG(6, "read %spacket\n", done ? "last " : "");
1628                 }
1629         }
1630
1631         if (dma && usb_pipeisoc(pipe)) {
1632                 struct usb_iso_packet_descriptor        *d;
1633                 int                                     iso_stat = status;
1634
1635                 d = urb->iso_frame_desc + qh->iso_idx;
1636                 d->actual_length += xfer_len;
1637                 if (iso_err) {
1638                         iso_stat = -EILSEQ;
1639                         urb->error_count++;
1640                 }
1641                 d->status = iso_stat;
1642         }
1643
1644 finish:
1645         urb->actual_length += xfer_len;
1646         qh->offset += xfer_len;
1647         if (done) {
1648                 if (urb->status == -EINPROGRESS)
1649                         urb->status = status;
1650                 musb_advance_schedule(musb, urb, hw_ep, USB_DIR_IN);
1651         }
1652 }
1653
1654 /* schedule nodes correspond to peripheral endpoints, like an OHCI QH.
1655  * the software schedule associates multiple such nodes with a given
1656  * host side hardware endpoint + direction; scheduling may activate
1657  * that hardware endpoint.
1658  */
1659 static int musb_schedule(
1660         struct musb             *musb,
1661         struct musb_qh          *qh,
1662         int                     is_in)
1663 {
1664         int                     idle;
1665         int                     best_diff;
1666         int                     best_end, epnum;
1667         struct musb_hw_ep       *hw_ep = NULL;
1668         struct list_head        *head = NULL;
1669
1670         /* use fixed hardware for control and bulk */
1671         switch (qh->type) {
1672         case USB_ENDPOINT_XFER_CONTROL:
1673                 head = &musb->control;
1674                 hw_ep = musb->control_ep;
1675                 break;
1676         case USB_ENDPOINT_XFER_BULK:
1677                 hw_ep = musb->bulk_ep;
1678                 if (is_in)
1679                         head = &musb->in_bulk;
1680                 else
1681                         head = &musb->out_bulk;
1682                 break;
1683         }
1684         if (head) {
1685                 idle = list_empty(head);
1686                 list_add_tail(&qh->ring, head);
1687                 goto success;
1688         }
1689
1690         /* else, periodic transfers get muxed to other endpoints */
1691
1692         /* FIXME this doesn't consider direction, so it can only
1693          * work for one half of the endpoint hardware, and assumes
1694          * the previous cases handled all non-shared endpoints...
1695          */
1696
1697         /* we know this qh hasn't been scheduled, so all we need to do
1698          * is choose which hardware endpoint to put it on ...
1699          *
1700          * REVISIT what we really want here is a regular schedule tree
1701          * like e.g. OHCI uses, but for now musb->periodic is just an
1702          * array of the _single_ logical endpoint associated with a
1703          * given physical one (identity mapping logical->physical).
1704          *
1705          * that simplistic approach makes TT scheduling a lot simpler;
1706          * there is none, and thus none of its complexity...
1707          */
1708         best_diff = 4096;
1709         best_end = -1;
1710
1711         for (epnum = 1; epnum < musb->nr_endpoints; epnum++) {
1712                 int     diff;
1713
1714                 if (musb->periodic[epnum])
1715                         continue;
1716                 hw_ep = &musb->endpoints[epnum];
1717                 if (hw_ep == musb->bulk_ep)
1718                         continue;
1719
1720                 if (is_in)
1721                         diff = hw_ep->max_packet_sz_rx - qh->maxpacket;
1722                 else
1723                         diff = hw_ep->max_packet_sz_tx - qh->maxpacket;
1724
1725                 if (diff > 0 && best_diff > diff) {
1726                         best_diff = diff;
1727                         best_end = epnum;
1728                 }
1729         }
1730         if (best_end < 0)
1731                 return -ENOSPC;
1732
1733         idle = 1;
1734         hw_ep = musb->endpoints + best_end;
1735         musb->periodic[best_end] = qh;
1736         DBG(4, "qh %p periodic slot %d\n", qh, best_end);
1737 success:
1738         qh->hw_ep = hw_ep;
1739         qh->hep->hcpriv = qh;
1740         if (idle)
1741                 musb_start_urb(musb, is_in, qh);
1742         return 0;
1743 }
1744
1745 static int musb_urb_enqueue(
1746         struct usb_hcd                  *hcd,
1747         struct urb                      *urb,
1748         gfp_t                           mem_flags)
1749 {
1750         unsigned long                   flags;
1751         struct musb                     *musb = hcd_to_musb(hcd);
1752         struct usb_host_endpoint        *hep = urb->ep;
1753         struct musb_qh                  *qh = hep->hcpriv;
1754         struct usb_endpoint_descriptor  *epd = &hep->desc;
1755         int                             ret;
1756         unsigned                        type_reg;
1757         unsigned                        interval;
1758
1759         /* host role must be active */
1760         if (!is_host_active(musb) || !musb->is_active)
1761                 return -ENODEV;
1762
1763         spin_lock_irqsave(&musb->lock, flags);
1764         ret = usb_hcd_link_urb_to_ep(hcd, urb);
1765         spin_unlock_irqrestore(&musb->lock, flags);
1766         if (ret)
1767                 return ret;
1768
1769         /* DMA mapping was already done, if needed, and this urb is on
1770          * hep->urb_list ... so there's little to do unless hep wasn't
1771          * yet scheduled onto a live qh.
1772          *
1773          * REVISIT best to keep hep->hcpriv valid until the endpoint gets
1774          * disabled, testing for empty qh->ring and avoiding qh setup costs
1775          * except for the first urb queued after a config change.
1776          */
1777         if (qh) {
1778                 urb->hcpriv = qh;
1779                 return 0;
1780         }
1781
1782         /* Allocate and initialize qh, minimizing the work done each time
1783          * hw_ep gets reprogrammed, or with irqs blocked.  Then schedule it.
1784          *
1785          * REVISIT consider a dedicated qh kmem_cache, so it's harder
1786          * for bugs in other kernel code to break this driver...
1787          */
1788         qh = kzalloc(sizeof *qh, mem_flags);
1789         if (!qh) {
1790                 usb_hcd_unlink_urb_from_ep(hcd, urb);
1791                 return -ENOMEM;
1792         }
1793
1794         qh->hep = hep;
1795         qh->dev = urb->dev;
1796         INIT_LIST_HEAD(&qh->ring);
1797         qh->is_ready = 1;
1798
1799         qh->maxpacket = le16_to_cpu(epd->wMaxPacketSize);
1800
1801         /* no high bandwidth support yet */
1802         if (qh->maxpacket & ~0x7ff) {
1803                 ret = -EMSGSIZE;
1804                 goto done;
1805         }
1806
1807         qh->epnum = epd->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1808         qh->type = epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
1809
1810         /* NOTE: urb->dev->devnum is wrong during SET_ADDRESS */
1811         qh->addr_reg = (u8) usb_pipedevice(urb->pipe);
1812
1813         /* precompute rxtype/txtype/type0 register */
1814         type_reg = (qh->type << 4) | qh->epnum;
1815         switch (urb->dev->speed) {
1816         case USB_SPEED_LOW:
1817                 type_reg |= 0xc0;
1818                 break;
1819         case USB_SPEED_FULL:
1820                 type_reg |= 0x80;
1821                 break;
1822         default:
1823                 type_reg |= 0x40;
1824         }
1825         qh->type_reg = type_reg;
1826
1827         /* precompute rxinterval/txinterval register */
1828         interval = min((u8)16, epd->bInterval); /* log encoding */
1829         switch (qh->type) {
1830         case USB_ENDPOINT_XFER_INT:
1831                 /* fullspeed uses linear encoding */
1832                 if (USB_SPEED_FULL == urb->dev->speed) {
1833                         interval = epd->bInterval;
1834                         if (!interval)
1835                                 interval = 1;
1836                 }
1837                 /* FALLTHROUGH */
1838         case USB_ENDPOINT_XFER_ISOC:
1839                 /* iso always uses log encoding */
1840                 break;
1841         default:
1842                 /* REVISIT we actually want to use NAK limits, hinting to the
1843                  * transfer scheduling logic to try some other qh, e.g. try
1844                  * for 2 msec first:
1845                  *
1846                  * interval = (USB_SPEED_HIGH == urb->dev->speed) ? 16 : 2;
1847                  *
1848                  * The downside of disabling this is that transfer scheduling
1849                  * gets VERY unfair for nonperiodic transfers; a misbehaving
1850                  * peripheral could make that hurt.  Or for reads, one that's
1851                  * perfectly normal:  network and other drivers keep reads
1852                  * posted at all times, having one pending for a week should
1853                  * be perfectly safe.
1854                  *
1855                  * The upside of disabling it is avoidng transfer scheduling
1856                  * code to put this aside for while.
1857                  */
1858                 interval = 0;
1859         }
1860         qh->intv_reg = interval;
1861
1862         /* precompute addressing for external hub/tt ports */
1863         if (musb->is_multipoint) {
1864                 struct usb_device       *parent = urb->dev->parent;
1865
1866                 if (parent != hcd->self.root_hub) {
1867                         qh->h_addr_reg = (u8) parent->devnum;
1868
1869                         /* set up tt info if needed */
1870                         if (urb->dev->tt) {
1871                                 qh->h_port_reg = (u8) urb->dev->ttport;
1872                                 qh->h_addr_reg |= 0x80;
1873                         }
1874                 }
1875         }
1876
1877         /* invariant: hep->hcpriv is null OR the qh that's already scheduled.
1878          * until we get real dma queues (with an entry for each urb/buffer),
1879          * we only have work to do in the former case.
1880          */
1881         spin_lock_irqsave(&musb->lock, flags);
1882         if (hep->hcpriv) {
1883                 /* some concurrent activity submitted another urb to hep...
1884                  * odd, rare, error prone, but legal.
1885                  */
1886                 kfree(qh);
1887                 ret = 0;
1888         } else
1889                 ret = musb_schedule(musb, qh,
1890                                 epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK);
1891
1892         if (ret == 0) {
1893                 urb->hcpriv = qh;
1894                 /* FIXME set urb->start_frame for iso/intr, it's tested in
1895                  * musb_start_urb(), but otherwise only konicawc cares ...
1896                  */
1897         }
1898         spin_unlock_irqrestore(&musb->lock, flags);
1899
1900 done:
1901         if (ret != 0) {
1902                 usb_hcd_unlink_urb_from_ep(hcd, urb);
1903                 kfree(qh);
1904         }
1905         return ret;
1906 }
1907
1908
1909 /*
1910  * abort a transfer that's at the head of a hardware queue.
1911  * called with controller locked, irqs blocked
1912  * that hardware queue advances to the next transfer, unless prevented
1913  */
1914 static int musb_cleanup_urb(struct urb *urb, struct musb_qh *qh, int is_in)
1915 {
1916         struct musb_hw_ep       *ep = qh->hw_ep;
1917         void __iomem            *epio = ep->regs;
1918         unsigned                hw_end = ep->epnum;
1919         void __iomem            *regs = ep->musb->mregs;
1920         u16                     csr;
1921         int                     status = 0;
1922
1923         musb_ep_select(regs, hw_end);
1924
1925         if (is_dma_capable()) {
1926                 struct dma_channel      *dma;
1927
1928                 dma = is_in ? ep->rx_channel : ep->tx_channel;
1929                 if (dma) {
1930                         status = ep->musb->dma_controller->channel_abort(dma);
1931                         DBG(status ? 1 : 3,
1932                                 "abort %cX%d DMA for urb %p --> %d\n",
1933                                 is_in ? 'R' : 'T', ep->epnum,
1934                                 urb, status);
1935                         urb->actual_length += dma->actual_len;
1936                 }
1937         }
1938
1939         /* turn off DMA requests, discard state, stop polling ... */
1940         if (is_in) {
1941                 /* giveback saves bulk toggle */
1942                 csr = musb_h_flush_rxfifo(ep, 0);
1943
1944                 /* REVISIT we still get an irq; should likely clear the
1945                  * endpoint's irq status here to avoid bogus irqs.
1946                  * clearing that status is platform-specific...
1947                  */
1948         } else {
1949                 musb_h_tx_flush_fifo(ep);
1950                 csr = musb_readw(epio, MUSB_TXCSR);
1951                 csr &= ~(MUSB_TXCSR_AUTOSET
1952                         | MUSB_TXCSR_DMAENAB
1953                         | MUSB_TXCSR_H_RXSTALL
1954                         | MUSB_TXCSR_H_NAKTIMEOUT
1955                         | MUSB_TXCSR_H_ERROR
1956                         | MUSB_TXCSR_TXPKTRDY);
1957                 musb_writew(epio, MUSB_TXCSR, csr);
1958                 /* REVISIT may need to clear FLUSHFIFO ... */
1959                 musb_writew(epio, MUSB_TXCSR, csr);
1960                 /* flush cpu writebuffer */
1961                 csr = musb_readw(epio, MUSB_TXCSR);
1962         }
1963         if (status == 0)
1964                 musb_advance_schedule(ep->musb, urb, ep, is_in);
1965         return status;
1966 }
1967
1968 static int musb_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
1969 {
1970         struct musb             *musb = hcd_to_musb(hcd);
1971         struct musb_qh          *qh;
1972         struct list_head        *sched;
1973         unsigned long           flags;
1974         int                     ret;
1975
1976         DBG(4, "urb=%p, dev%d ep%d%s\n", urb,
1977                         usb_pipedevice(urb->pipe),
1978                         usb_pipeendpoint(urb->pipe),
1979                         usb_pipein(urb->pipe) ? "in" : "out");
1980
1981         spin_lock_irqsave(&musb->lock, flags);
1982         ret = usb_hcd_check_unlink_urb(hcd, urb, status);
1983         if (ret)
1984                 goto done;
1985
1986         qh = urb->hcpriv;
1987         if (!qh)
1988                 goto done;
1989
1990         /* Any URB not actively programmed into endpoint hardware can be
1991          * immediately given back.  Such an URB must be at the head of its
1992          * endpoint queue, unless someday we get real DMA queues.  And even
1993          * then, it might not be known to the hardware...
1994          *
1995          * Otherwise abort current transfer, pending dma, etc.; urb->status
1996          * has already been updated.  This is a synchronous abort; it'd be
1997          * OK to hold off until after some IRQ, though.
1998          */
1999         if (!qh->is_ready || urb->urb_list.prev != &qh->hep->urb_list)
2000                 ret = -EINPROGRESS;
2001         else {
2002                 switch (qh->type) {
2003                 case USB_ENDPOINT_XFER_CONTROL:
2004                         sched = &musb->control;
2005                         break;
2006                 case USB_ENDPOINT_XFER_BULK:
2007                         if (usb_pipein(urb->pipe))
2008                                 sched = &musb->in_bulk;
2009                         else
2010                                 sched = &musb->out_bulk;
2011                         break;
2012                 default:
2013                         /* REVISIT when we get a schedule tree, periodic
2014                          * transfers won't always be at the head of a
2015                          * singleton queue...
2016                          */
2017                         sched = NULL;
2018                         break;
2019                 }
2020         }
2021
2022         /* NOTE:  qh is invalid unless !list_empty(&hep->urb_list) */
2023         if (ret < 0 || (sched && qh != first_qh(sched))) {
2024                 int     ready = qh->is_ready;
2025
2026                 ret = 0;
2027                 qh->is_ready = 0;
2028                 __musb_giveback(musb, urb, 0);
2029                 qh->is_ready = ready;
2030         } else
2031                 ret = musb_cleanup_urb(urb, qh, urb->pipe & USB_DIR_IN);
2032 done:
2033         spin_unlock_irqrestore(&musb->lock, flags);
2034         return ret;
2035 }
2036
2037 /* disable an endpoint */
2038 static void
2039 musb_h_disable(struct usb_hcd *hcd, struct usb_host_endpoint *hep)
2040 {
2041         u8                      epnum = hep->desc.bEndpointAddress;
2042         unsigned long           flags;
2043         struct musb             *musb = hcd_to_musb(hcd);
2044         u8                      is_in = epnum & USB_DIR_IN;
2045         struct musb_qh          *qh = hep->hcpriv;
2046         struct urb              *urb, *tmp;
2047         struct list_head        *sched;
2048
2049         if (!qh)
2050                 return;
2051
2052         spin_lock_irqsave(&musb->lock, flags);
2053
2054         switch (qh->type) {
2055         case USB_ENDPOINT_XFER_CONTROL:
2056                 sched = &musb->control;
2057                 break;
2058         case USB_ENDPOINT_XFER_BULK:
2059                 if (is_in)
2060                         sched = &musb->in_bulk;
2061                 else
2062                         sched = &musb->out_bulk;
2063                 break;
2064         default:
2065                 /* REVISIT when we get a schedule tree, periodic transfers
2066                  * won't always be at the head of a singleton queue...
2067                  */
2068                 sched = NULL;
2069                 break;
2070         }
2071
2072         /* NOTE:  qh is invalid unless !list_empty(&hep->urb_list) */
2073
2074         /* kick first urb off the hardware, if needed */
2075         qh->is_ready = 0;
2076         if (!sched || qh == first_qh(sched)) {
2077                 urb = next_urb(qh);
2078
2079                 /* make software (then hardware) stop ASAP */
2080                 if (!urb->unlinked)
2081                         urb->status = -ESHUTDOWN;
2082
2083                 /* cleanup */
2084                 musb_cleanup_urb(urb, qh, urb->pipe & USB_DIR_IN);
2085         } else
2086                 urb = NULL;
2087
2088         /* then just nuke all the others */
2089         list_for_each_entry_safe_from(urb, tmp, &hep->urb_list, urb_list)
2090                 musb_giveback(qh, urb, -ESHUTDOWN);
2091
2092         spin_unlock_irqrestore(&musb->lock, flags);
2093 }
2094
2095 static int musb_h_get_frame_number(struct usb_hcd *hcd)
2096 {
2097         struct musb     *musb = hcd_to_musb(hcd);
2098
2099         return musb_readw(musb->mregs, MUSB_FRAME);
2100 }
2101
2102 static int musb_h_start(struct usb_hcd *hcd)
2103 {
2104         struct musb     *musb = hcd_to_musb(hcd);
2105
2106         /* NOTE: musb_start() is called when the hub driver turns
2107          * on port power, or when (OTG) peripheral starts.
2108          */
2109         hcd->state = HC_STATE_RUNNING;
2110         musb->port1_status = 0;
2111         return 0;
2112 }
2113
2114 static void musb_h_stop(struct usb_hcd *hcd)
2115 {
2116         musb_stop(hcd_to_musb(hcd));
2117         hcd->state = HC_STATE_HALT;
2118 }
2119
2120 static int musb_bus_suspend(struct usb_hcd *hcd)
2121 {
2122         struct musb     *musb = hcd_to_musb(hcd);
2123
2124         if (musb->xceiv.state == OTG_STATE_A_SUSPEND)
2125                 return 0;
2126
2127         if (is_host_active(musb) && musb->is_active) {
2128                 WARNING("trying to suspend as %s is_active=%i\n",
2129                         otg_state_string(musb), musb->is_active);
2130                 return -EBUSY;
2131         } else
2132                 return 0;
2133 }
2134
2135 static int musb_bus_resume(struct usb_hcd *hcd)
2136 {
2137         /* resuming child port does the work */
2138         return 0;
2139 }
2140
2141 const struct hc_driver musb_hc_driver = {
2142         .description            = "musb-hcd",
2143         .product_desc           = "MUSB HDRC host driver",
2144         .hcd_priv_size          = sizeof(struct musb),
2145         .flags                  = HCD_USB2 | HCD_MEMORY,
2146
2147         /* not using irq handler or reset hooks from usbcore, since
2148          * those must be shared with peripheral code for OTG configs
2149          */
2150
2151         .start                  = musb_h_start,
2152         .stop                   = musb_h_stop,
2153
2154         .get_frame_number       = musb_h_get_frame_number,
2155
2156         .urb_enqueue            = musb_urb_enqueue,
2157         .urb_dequeue            = musb_urb_dequeue,
2158         .endpoint_disable       = musb_h_disable,
2159
2160         .hub_status_data        = musb_hub_status_data,
2161         .hub_control            = musb_hub_control,
2162         .bus_suspend            = musb_bus_suspend,
2163         .bus_resume             = musb_bus_resume,
2164         /* .start_port_reset    = NULL, */
2165         /* .hub_irq_enable      = NULL, */
2166 };