]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/usb/musb/musb_host.c
2ad2ed95404aa23c039ef5fde6979a54ddebd357
[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         qh = musb_giveback(qh, urb, 0);
426
427         if (qh && qh->is_ready && !list_empty(&qh->hep->urb_list)) {
428                 DBG(4, "... next ep%d %cX urb %p\n",
429                                 hw_ep->epnum, is_in ? 'R' : 'T',
430                                 next_urb(qh));
431                 musb_start_urb(musb, is_in, qh);
432         }
433 }
434
435 static inline u16 musb_h_flush_rxfifo(struct musb_hw_ep *hw_ep, u16 csr)
436 {
437         /* we don't want fifo to fill itself again;
438          * ignore dma (various models),
439          * leave toggle alone (may not have been saved yet)
440          */
441         csr |= MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_RXPKTRDY;
442         csr &= ~( MUSB_RXCSR_H_REQPKT
443                 | MUSB_RXCSR_H_AUTOREQ
444                 | MUSB_RXCSR_AUTOCLEAR
445                 );
446
447         /* write 2x to allow double buffering */
448         musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
449         musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
450
451         /* flush writebuffer */
452         return musb_readw(hw_ep->regs, MUSB_RXCSR);
453 }
454
455 /*
456  * PIO RX for a packet (or part of it).
457  */
458 static bool
459 musb_host_packet_rx(struct musb *musb, struct urb *urb, u8 epnum, u8 iso_err)
460 {
461         u16                     rx_count;
462         u8                      *buf;
463         u16                     csr;
464         bool                    done = false;
465         u32                     length;
466         int                     do_flush = 0;
467         struct musb_hw_ep       *hw_ep = musb->endpoints + epnum;
468         void __iomem            *epio = hw_ep->regs;
469         struct musb_qh          *qh = hw_ep->in_qh;
470         int                     pipe = urb->pipe;
471         void                    *buffer = urb->transfer_buffer;
472
473         /* musb_ep_select(mbase, epnum); */
474         rx_count = musb_readw(epio, MUSB_RXCOUNT);
475         DBG(3, "RX%d count %d, buffer %p len %d/%d\n", epnum, rx_count,
476                         urb->transfer_buffer, qh->offset,
477                         urb->transfer_buffer_length);
478
479         /* unload FIFO */
480         if (usb_pipeisoc(pipe)) {
481                 int                                     status = 0;
482                 struct usb_iso_packet_descriptor        *d;
483
484                 if (iso_err) {
485                         status = -EILSEQ;
486                         urb->error_count++;
487                 }
488
489                 d = urb->iso_frame_desc + qh->iso_idx;
490                 buf = buffer + d->offset;
491                 length = d->length;
492                 if (rx_count > length) {
493                         if (status == 0) {
494                                 status = -EOVERFLOW;
495                                 urb->error_count++;
496                         }
497                         DBG(2, "** OVERFLOW %d into %d\n", rx_count, length);
498                         do_flush = 1;
499                 } else
500                         length = rx_count;
501                 urb->actual_length += length;
502                 d->actual_length = length;
503
504                 d->status = status;
505
506                 /* see if we are done */
507                 done = (++qh->iso_idx >= urb->number_of_packets);
508         } else {
509                 /* non-isoch */
510                 buf = buffer + qh->offset;
511                 length = urb->transfer_buffer_length - qh->offset;
512                 if (rx_count > length) {
513                         if (urb->status == -EINPROGRESS)
514                                 urb->status = -EOVERFLOW;
515                         DBG(2, "** OVERFLOW %d into %d\n", rx_count, length);
516                         do_flush = 1;
517                 } else
518                         length = rx_count;
519                 urb->actual_length += length;
520                 qh->offset += length;
521
522                 /* see if we are done */
523                 done = (urb->actual_length == urb->transfer_buffer_length)
524                         || (rx_count < qh->maxpacket)
525                         || (urb->status != -EINPROGRESS);
526                 if (done
527                                 && (urb->status == -EINPROGRESS)
528                                 && (urb->transfer_flags & URB_SHORT_NOT_OK)
529                                 && (urb->actual_length
530                                         < urb->transfer_buffer_length))
531                         urb->status = -EREMOTEIO;
532         }
533
534         musb_read_fifo(hw_ep, length, buf);
535
536         csr = musb_readw(epio, MUSB_RXCSR);
537         csr |= MUSB_RXCSR_H_WZC_BITS;
538         if (unlikely(do_flush))
539                 musb_h_flush_rxfifo(hw_ep, csr);
540         else {
541                 /* REVISIT this assumes AUTOCLEAR is never set */
542                 csr &= ~(MUSB_RXCSR_RXPKTRDY | MUSB_RXCSR_H_REQPKT);
543                 if (!done)
544                         csr |= MUSB_RXCSR_H_REQPKT;
545                 musb_writew(epio, MUSB_RXCSR, csr);
546         }
547
548         return done;
549 }
550
551 /* we don't always need to reinit a given side of an endpoint...
552  * when we do, use tx/rx reinit routine and then construct a new CSR
553  * to address data toggle, NYET, and DMA or PIO.
554  *
555  * it's possible that driver bugs (especially for DMA) or aborting a
556  * transfer might have left the endpoint busier than it should be.
557  * the busy/not-empty tests are basically paranoia.
558  */
559 static void
560 musb_rx_reinit(struct musb *musb, struct musb_qh *qh, struct musb_hw_ep *ep)
561 {
562         u16     csr;
563
564         /* NOTE:  we know the "rx" fifo reinit never triggers for ep0.
565          * That always uses tx_reinit since ep0 repurposes TX register
566          * offsets; the initial SETUP packet is also a kind of OUT.
567          */
568
569         /* if programmed for Tx, put it in RX mode */
570         if (ep->is_shared_fifo) {
571                 csr = musb_readw(ep->regs, MUSB_TXCSR);
572                 if (csr & MUSB_TXCSR_MODE) {
573                         musb_h_tx_flush_fifo(ep);
574                         musb_writew(ep->regs, MUSB_TXCSR,
575                                         MUSB_TXCSR_FRCDATATOG);
576                 }
577                 /* clear mode (and everything else) to enable Rx */
578                 musb_writew(ep->regs, MUSB_TXCSR, 0);
579
580         /* scrub all previous state, clearing toggle */
581         } else {
582                 csr = musb_readw(ep->regs, MUSB_RXCSR);
583                 if (csr & MUSB_RXCSR_RXPKTRDY)
584                         WARN("rx%d, packet/%d ready?\n", ep->epnum,
585                                 musb_readw(ep->regs, MUSB_RXCOUNT));
586
587                 musb_h_flush_rxfifo(ep, MUSB_RXCSR_CLRDATATOG);
588         }
589
590         /* target addr and (for multipoint) hub addr/port */
591         if (musb->is_multipoint) {
592                 musb_writeb(ep->target_regs, MUSB_RXFUNCADDR,
593                         qh->addr_reg);
594                 musb_writeb(ep->target_regs, MUSB_RXHUBADDR,
595                         qh->h_addr_reg);
596                 musb_writeb(ep->target_regs, MUSB_RXHUBPORT,
597                         qh->h_port_reg);
598         } else
599                 musb_writeb(musb->mregs, MUSB_FADDR, qh->addr_reg);
600
601         /* protocol/endpoint, interval/NAKlimit, i/o size */
602         musb_writeb(ep->regs, MUSB_RXTYPE, qh->type_reg);
603         musb_writeb(ep->regs, MUSB_RXINTERVAL, qh->intv_reg);
604         /* NOTE: bulk combining rewrites high bits of maxpacket */
605         musb_writew(ep->regs, MUSB_RXMAXP, qh->maxpacket);
606
607         ep->rx_reinit = 0;
608 }
609
610
611 /*
612  * Program an HDRC endpoint as per the given URB
613  * Context: irqs blocked, controller lock held
614  */
615 static void musb_ep_program(struct musb *musb, u8 epnum,
616                         struct urb *urb, unsigned int is_out,
617                         u8 * buf, u32 len)
618 {
619         struct dma_controller   *dma_controller;
620         struct dma_channel      *dma_channel;
621         u8                      dma_ok;
622         void __iomem            *mbase = musb->mregs;
623         struct musb_hw_ep       *hw_ep = musb->endpoints + epnum;
624         void __iomem            *epio = hw_ep->regs;
625         struct musb_qh          *qh;
626         u16                     packet_sz;
627
628         if (!is_out || hw_ep->is_shared_fifo)
629                 qh = hw_ep->in_qh;
630         else
631                 qh = hw_ep->out_qh;
632
633         packet_sz = qh->maxpacket;
634
635         DBG(3, "%s hw%d urb %p spd%d dev%d ep%d%s "
636                                 "h_addr%02x h_port%02x bytes %d\n",
637                         is_out ? "-->" : "<--",
638                         epnum, urb, urb->dev->speed,
639                         qh->addr_reg, qh->epnum, is_out ? "out" : "in",
640                         qh->h_addr_reg, qh->h_port_reg,
641                         len);
642
643         musb_ep_select(mbase, epnum);
644
645         /* candidate for DMA? */
646         dma_controller = musb->dma_controller;
647         if (is_dma_capable() && epnum && dma_controller) {
648                 dma_channel = is_out ? hw_ep->tx_channel : hw_ep->rx_channel;
649                 if (!dma_channel) {
650                         dma_channel = dma_controller->channel_alloc(
651                                         dma_controller, hw_ep, is_out);
652                         if (is_out)
653                                 hw_ep->tx_channel = dma_channel;
654                         else
655                                 hw_ep->rx_channel = dma_channel;
656                 }
657         } else
658                 dma_channel = NULL;
659
660         /* make sure we clear DMAEnab, autoSet bits from previous run */
661
662         /* OUT/transmit/EP0 or IN/receive? */
663         if (is_out) {
664                 u16     csr;
665                 u16     int_txe;
666                 u16     load_count;
667
668                 csr = musb_readw(epio, MUSB_TXCSR);
669
670                 /* disable interrupt in case we flush */
671                 int_txe = musb_readw(mbase, MUSB_INTRTXE);
672                 musb_writew(mbase, MUSB_INTRTXE, int_txe & ~(1 << epnum));
673
674                 /* general endpoint setup */
675                 if (epnum) {
676                         /* ASSERT:  TXCSR_DMAENAB was already cleared */
677
678                         /* flush all old state, set default */
679                         musb_h_tx_flush_fifo(hw_ep);
680                         csr &= ~(MUSB_TXCSR_H_NAKTIMEOUT
681                                         | MUSB_TXCSR_DMAMODE
682                                         | MUSB_TXCSR_FRCDATATOG
683                                         | MUSB_TXCSR_H_RXSTALL
684                                         | MUSB_TXCSR_H_ERROR
685                                         | MUSB_TXCSR_TXPKTRDY
686                                         );
687                         csr |= MUSB_TXCSR_MODE;
688
689                         if (usb_gettoggle(urb->dev,
690                                         qh->epnum, 1))
691                                 csr |= MUSB_TXCSR_H_WR_DATATOGGLE
692                                         | MUSB_TXCSR_H_DATATOGGLE;
693                         else
694                                 csr |= MUSB_TXCSR_CLRDATATOG;
695
696                         /* twice in case of double packet buffering */
697                         musb_writew(epio, MUSB_TXCSR, csr);
698                         /* REVISIT may need to clear FLUSHFIFO ... */
699                         musb_writew(epio, MUSB_TXCSR, csr);
700                         csr = musb_readw(epio, MUSB_TXCSR);
701                 } else {
702                         /* endpoint 0: just flush */
703                         musb_writew(epio, MUSB_CSR0,
704                                 csr | MUSB_CSR0_FLUSHFIFO);
705                         musb_writew(epio, MUSB_CSR0,
706                                 csr | MUSB_CSR0_FLUSHFIFO);
707                 }
708
709                 /* target addr and (for multipoint) hub addr/port */
710                 if (musb->is_multipoint) {
711                         musb_writeb(mbase,
712                                 MUSB_BUSCTL_OFFSET(epnum, MUSB_TXFUNCADDR),
713                                 qh->addr_reg);
714                         musb_writeb(mbase,
715                                 MUSB_BUSCTL_OFFSET(epnum, MUSB_TXHUBADDR),
716                                 qh->h_addr_reg);
717                         musb_writeb(mbase,
718                                 MUSB_BUSCTL_OFFSET(epnum, MUSB_TXHUBPORT),
719                                 qh->h_port_reg);
720 /* FIXME if !epnum, do the same for RX ... */
721                 } else
722                         musb_writeb(mbase, MUSB_FADDR, qh->addr_reg);
723
724                 /* protocol/endpoint/interval/NAKlimit */
725                 if (epnum) {
726                         musb_writeb(epio, MUSB_TXTYPE, qh->type_reg);
727                         if (can_bulk_split(musb, qh->type))
728                                 musb_writew(epio, MUSB_TXMAXP,
729                                         packet_sz
730                                         | ((hw_ep->max_packet_sz_tx /
731                                                 packet_sz) - 1) << 11);
732                         else
733                                 musb_writew(epio, MUSB_TXMAXP,
734                                         packet_sz);
735                         musb_writeb(epio, MUSB_TXINTERVAL, qh->intv_reg);
736                 } else {
737                         musb_writeb(epio, MUSB_NAKLIMIT0, qh->intv_reg);
738                         if (musb->is_multipoint)
739                                 musb_writeb(epio, MUSB_TYPE0,
740                                                 qh->type_reg);
741                 }
742
743                 if (can_bulk_split(musb, qh->type))
744                         load_count = min((u32) hw_ep->max_packet_sz_tx,
745                                                 len);
746                 else
747                         load_count = min((u32) packet_sz, len);
748
749 #ifdef CONFIG_USB_INVENTRA_DMA
750                 if (dma_channel) {
751
752                         /* clear previous state */
753                         csr = musb_readw(epio, MUSB_TXCSR);
754                         csr &= ~(MUSB_TXCSR_AUTOSET
755                                 | MUSB_TXCSR_DMAMODE
756                                 | MUSB_TXCSR_DMAENAB);
757                         csr |= MUSB_TXCSR_MODE;
758                         musb_writew(epio, MUSB_TXCSR,
759                                 csr | MUSB_TXCSR_MODE);
760
761                         qh->segsize = min(len, dma_channel->max_len);
762
763                         if (qh->segsize <= packet_sz)
764                                 dma_channel->desired_mode = 0;
765                         else
766                                 dma_channel->desired_mode = 1;
767
768
769                         if (dma_channel->desired_mode == 0) {
770                                 csr &= ~(MUSB_TXCSR_AUTOSET
771                                         | MUSB_TXCSR_DMAMODE);
772                                 csr |= (MUSB_TXCSR_DMAENAB);
773                                         /* against programming guide */
774                         } else
775                                 csr |= (MUSB_TXCSR_AUTOSET
776                                         | MUSB_TXCSR_DMAENAB
777                                         | MUSB_TXCSR_DMAMODE);
778
779                         musb_writew(epio, MUSB_TXCSR, csr);
780
781                         dma_ok = dma_controller->channel_program(
782                                         dma_channel, packet_sz,
783                                         dma_channel->desired_mode,
784                                         urb->transfer_dma,
785                                         qh->segsize);
786                         if (dma_ok) {
787                                 load_count = 0;
788                         } else {
789                                 dma_controller->channel_release(dma_channel);
790                                 if (is_out)
791                                         hw_ep->tx_channel = NULL;
792                                 else
793                                         hw_ep->rx_channel = NULL;
794                                 dma_channel = NULL;
795                         }
796                 }
797 #endif
798
799                 /* candidate for DMA */
800                 if ((is_cppi_enabled() || tusb_dma_omap()) && dma_channel) {
801
802                         /* program endpoint CSRs first, then setup DMA.
803                          * assume CPPI setup succeeds.
804                          * defer enabling dma.
805                          */
806                         csr = musb_readw(epio, MUSB_TXCSR);
807                         csr &= ~(MUSB_TXCSR_AUTOSET
808                                         | MUSB_TXCSR_DMAMODE
809                                         | MUSB_TXCSR_DMAENAB);
810                         csr |= MUSB_TXCSR_MODE;
811                         musb_writew(epio, MUSB_TXCSR,
812                                 csr | MUSB_TXCSR_MODE);
813
814                         dma_channel->actual_len = 0L;
815                         qh->segsize = len;
816
817                         /* TX uses "rndis" mode automatically, but needs help
818                          * to identify the zero-length-final-packet case.
819                          */
820                         dma_ok = dma_controller->channel_program(
821                                         dma_channel, packet_sz,
822                                         (urb->transfer_flags
823                                                         & URB_ZERO_PACKET)
824                                                 == URB_ZERO_PACKET,
825                                         urb->transfer_dma,
826                                         qh->segsize);
827                         if (dma_ok) {
828                                 load_count = 0;
829                         } else {
830                                 dma_controller->channel_release(dma_channel);
831                                 dma_channel = hw_ep->tx_channel = NULL;
832
833                                 /* REVISIT there's an error path here that
834                                  * needs handling:  can't do dma, but
835                                  * there's no pio buffer address...
836                                  */
837                         }
838                 }
839
840                 if (load_count) {
841                         /* ASSERT:  TXCSR_DMAENAB was already cleared */
842
843                         /* PIO to load FIFO */
844                         qh->segsize = load_count;
845                         musb_write_fifo(hw_ep, load_count, buf);
846                         csr = musb_readw(epio, MUSB_TXCSR);
847                         csr &= ~(MUSB_TXCSR_DMAENAB
848                                 | MUSB_TXCSR_DMAMODE
849                                 | MUSB_TXCSR_AUTOSET);
850                         /* write CSR */
851                         csr |= MUSB_TXCSR_MODE;
852
853                         if (epnum)
854                                 musb_writew(epio, MUSB_TXCSR, csr);
855                 }
856
857                 /* re-enable interrupt */
858                 musb_writew(mbase, MUSB_INTRTXE, int_txe);
859
860         /* IN/receive */
861         } else {
862                 u16     csr;
863
864                 if (hw_ep->rx_reinit) {
865                         musb_rx_reinit(musb, qh, hw_ep);
866
867                         /* init new state: toggle and NYET, maybe DMA later */
868                         if (usb_gettoggle(urb->dev, qh->epnum, 0))
869                                 csr = MUSB_RXCSR_H_WR_DATATOGGLE
870                                         | MUSB_RXCSR_H_DATATOGGLE;
871                         else
872                                 csr = 0;
873                         if (qh->type == USB_ENDPOINT_XFER_INT)
874                                 csr |= MUSB_RXCSR_DISNYET;
875
876                 } else {
877                         csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
878
879                         if (csr & (MUSB_RXCSR_RXPKTRDY
880                                         | MUSB_RXCSR_DMAENAB
881                                         | MUSB_RXCSR_H_REQPKT))
882                                 ERR("broken !rx_reinit, ep%d csr %04x\n",
883                                                 hw_ep->epnum, csr);
884
885                         /* scrub any stale state, leaving toggle alone */
886                         csr &= MUSB_RXCSR_DISNYET;
887                 }
888
889                 /* kick things off */
890
891                 if ((is_cppi_enabled() || tusb_dma_omap()) && dma_channel) {
892                         /* candidate for DMA */
893                         if (dma_channel) {
894                                 dma_channel->actual_len = 0L;
895                                 qh->segsize = len;
896
897                                 /* AUTOREQ is in a DMA register */
898                                 musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
899                                 csr = musb_readw(hw_ep->regs,
900                                                 MUSB_RXCSR);
901
902                                 /* unless caller treats short rx transfers as
903                                  * errors, we dare not queue multiple transfers.
904                                  */
905                                 dma_ok = dma_controller->channel_program(
906                                                 dma_channel, packet_sz,
907                                                 !(urb->transfer_flags
908                                                         & URB_SHORT_NOT_OK),
909                                                 urb->transfer_dma,
910                                                 qh->segsize);
911                                 if (!dma_ok) {
912                                         dma_controller->channel_release(
913                                                         dma_channel);
914                                         dma_channel = hw_ep->rx_channel = NULL;
915                                 } else
916                                         csr |= MUSB_RXCSR_DMAENAB;
917                         }
918                 }
919
920                 csr |= MUSB_RXCSR_H_REQPKT;
921                 DBG(7, "RXCSR%d := %04x\n", epnum, csr);
922                 musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
923                 csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
924         }
925 }
926
927
928 /*
929  * Service the default endpoint (ep0) as host.
930  * Return true until it's time to start the status stage.
931  */
932 static bool musb_h_ep0_continue(struct musb *musb, u16 len, struct urb *urb)
933 {
934         bool                     more = false;
935         u8                      *fifo_dest = NULL;
936         u16                     fifo_count = 0;
937         struct musb_hw_ep       *hw_ep = musb->control_ep;
938         struct musb_qh          *qh = hw_ep->in_qh;
939         struct usb_ctrlrequest  *request;
940
941         switch (musb->ep0_stage) {
942         case MUSB_EP0_IN:
943                 fifo_dest = urb->transfer_buffer + urb->actual_length;
944                 fifo_count = min(len, ((u16) (urb->transfer_buffer_length
945                                         - urb->actual_length)));
946                 if (fifo_count < len)
947                         urb->status = -EOVERFLOW;
948
949                 musb_read_fifo(hw_ep, fifo_count, fifo_dest);
950
951                 urb->actual_length += fifo_count;
952                 if (len < qh->maxpacket) {
953                         /* always terminate on short read; it's
954                          * rarely reported as an error.
955                          */
956                 } else if (urb->actual_length <
957                                 urb->transfer_buffer_length)
958                         more = true;
959                 break;
960         case MUSB_EP0_START:
961                 request = (struct usb_ctrlrequest *) urb->setup_packet;
962
963                 if (!request->wLength) {
964                         DBG(4, "start no-DATA\n");
965                         break;
966                 } else if (request->bRequestType & USB_DIR_IN) {
967                         DBG(4, "start IN-DATA\n");
968                         musb->ep0_stage = MUSB_EP0_IN;
969                         more = true;
970                         break;
971                 } else {
972                         DBG(4, "start OUT-DATA\n");
973                         musb->ep0_stage = MUSB_EP0_OUT;
974                         more = true;
975                 }
976                 /* FALLTHROUGH */
977         case MUSB_EP0_OUT:
978                 fifo_count = min(qh->maxpacket, ((u16)
979                                 (urb->transfer_buffer_length
980                                 - urb->actual_length)));
981
982                 if (fifo_count) {
983                         fifo_dest = (u8 *) (urb->transfer_buffer
984                                         + urb->actual_length);
985                         DBG(3, "Sending %d bytes to %p\n",
986                                         fifo_count, fifo_dest);
987                         musb_write_fifo(hw_ep, fifo_count, fifo_dest);
988
989                         urb->actual_length += fifo_count;
990                         more = true;
991                 }
992                 break;
993         default:
994                 ERR("bogus ep0 stage %d\n", musb->ep0_stage);
995                 break;
996         }
997
998         return more;
999 }
1000
1001 /*
1002  * Handle default endpoint interrupt as host. Only called in IRQ time
1003  * from the LinuxIsr() interrupt service routine.
1004  *
1005  * called with controller irqlocked
1006  */
1007 irqreturn_t musb_h_ep0_irq(struct musb *musb)
1008 {
1009         struct urb              *urb;
1010         u16                     csr, len;
1011         int                     status = 0;
1012         void __iomem            *mbase = musb->mregs;
1013         struct musb_hw_ep       *hw_ep = musb->control_ep;
1014         void __iomem            *epio = hw_ep->regs;
1015         struct musb_qh          *qh = hw_ep->in_qh;
1016         bool                    complete = false;
1017         irqreturn_t             retval = IRQ_NONE;
1018
1019         /* ep0 only has one queue, "in" */
1020         urb = next_urb(qh);
1021
1022         musb_ep_select(mbase, 0);
1023         csr = musb_readw(epio, MUSB_CSR0);
1024         len = (csr & MUSB_CSR0_RXPKTRDY)
1025                         ? musb_readb(epio, MUSB_COUNT0)
1026                         : 0;
1027
1028         DBG(4, "<== csr0 %04x, qh %p, count %d, urb %p, stage %d\n",
1029                 csr, qh, len, urb, musb->ep0_stage);
1030
1031         /* if we just did status stage, we are done */
1032         if (MUSB_EP0_STATUS == musb->ep0_stage) {
1033                 retval = IRQ_HANDLED;
1034                 complete = true;
1035         }
1036
1037         /* prepare status */
1038         if (csr & MUSB_CSR0_H_RXSTALL) {
1039                 DBG(6, "STALLING ENDPOINT\n");
1040                 status = -EPIPE;
1041
1042         } else if (csr & MUSB_CSR0_H_ERROR) {
1043                 DBG(2, "no response, csr0 %04x\n", csr);
1044                 status = -EPROTO;
1045
1046         } else if (csr & MUSB_CSR0_H_NAKTIMEOUT) {
1047                 DBG(2, "control NAK timeout\n");
1048
1049                 /* NOTE:  this code path would be a good place to PAUSE a
1050                  * control transfer, if another one is queued, so that
1051                  * ep0 is more likely to stay busy.
1052                  *
1053                  * if (qh->ring.next != &musb->control), then
1054                  * we have a candidate... NAKing is *NOT* an error
1055                  */
1056                 musb_writew(epio, MUSB_CSR0, 0);
1057                 retval = IRQ_HANDLED;
1058         }
1059
1060         if (status) {
1061                 DBG(6, "aborting\n");
1062                 retval = IRQ_HANDLED;
1063                 if (urb)
1064                         urb->status = status;
1065                 complete = true;
1066
1067                 /* use the proper sequence to abort the transfer */
1068                 if (csr & MUSB_CSR0_H_REQPKT) {
1069                         csr &= ~MUSB_CSR0_H_REQPKT;
1070                         musb_writew(epio, MUSB_CSR0, csr);
1071                         csr &= ~MUSB_CSR0_H_NAKTIMEOUT;
1072                         musb_writew(epio, MUSB_CSR0, csr);
1073                 } else {
1074                         csr |= MUSB_CSR0_FLUSHFIFO;
1075                         musb_writew(epio, MUSB_CSR0, csr);
1076                         musb_writew(epio, MUSB_CSR0, csr);
1077                         csr &= ~MUSB_CSR0_H_NAKTIMEOUT;
1078                         musb_writew(epio, MUSB_CSR0, csr);
1079                 }
1080
1081                 musb_writeb(epio, MUSB_NAKLIMIT0, 0);
1082
1083                 /* clear it */
1084                 musb_writew(epio, MUSB_CSR0, 0);
1085         }
1086
1087         if (unlikely(!urb)) {
1088                 /* stop endpoint since we have no place for its data, this
1089                  * SHOULD NEVER HAPPEN! */
1090                 ERR("no URB for end 0\n");
1091
1092                 musb_writew(epio, MUSB_CSR0, MUSB_CSR0_FLUSHFIFO);
1093                 musb_writew(epio, MUSB_CSR0, MUSB_CSR0_FLUSHFIFO);
1094                 musb_writew(epio, MUSB_CSR0, 0);
1095
1096                 goto done;
1097         }
1098
1099         if (!complete) {
1100                 /* call common logic and prepare response */
1101                 if (musb_h_ep0_continue(musb, len, urb)) {
1102                         /* more packets required */
1103                         csr = (MUSB_EP0_IN == musb->ep0_stage)
1104                                 ?  MUSB_CSR0_H_REQPKT : MUSB_CSR0_TXPKTRDY;
1105                 } else {
1106                         /* data transfer complete; perform status phase */
1107                         if (usb_pipeout(urb->pipe)
1108                                         || !urb->transfer_buffer_length)
1109                                 csr = MUSB_CSR0_H_STATUSPKT
1110                                         | MUSB_CSR0_H_REQPKT;
1111                         else
1112                                 csr = MUSB_CSR0_H_STATUSPKT
1113                                         | MUSB_CSR0_TXPKTRDY;
1114
1115                         /* flag status stage */
1116                         musb->ep0_stage = MUSB_EP0_STATUS;
1117
1118                         DBG(5, "ep0 STATUS, csr %04x\n", csr);
1119
1120                 }
1121                 musb_writew(epio, MUSB_CSR0, csr);
1122                 retval = IRQ_HANDLED;
1123         } else
1124                 musb->ep0_stage = MUSB_EP0_IDLE;
1125
1126         /* call completion handler if done */
1127         if (complete)
1128                 musb_advance_schedule(musb, urb, hw_ep, 1);
1129 done:
1130         return retval;
1131 }
1132
1133
1134 #ifdef CONFIG_USB_INVENTRA_DMA
1135
1136 /* Host side TX (OUT) using Mentor DMA works as follows:
1137         submit_urb ->
1138                 - if queue was empty, Program Endpoint
1139                 - ... which starts DMA to fifo in mode 1 or 0
1140
1141         DMA Isr (transfer complete) -> TxAvail()
1142                 - Stop DMA (~DmaEnab)   (<--- Alert ... currently happens
1143                                         only in musb_cleanup_urb)
1144                 - TxPktRdy has to be set in mode 0 or for
1145                         short packets in mode 1.
1146 */
1147
1148 #endif
1149
1150 /* Service a Tx-Available or dma completion irq for the endpoint */
1151 void musb_host_tx(struct musb *musb, u8 epnum)
1152 {
1153         int                     pipe;
1154         bool                    done = false;
1155         u16                     tx_csr;
1156         size_t                  wLength = 0;
1157         u8                      *buf = NULL;
1158         struct urb              *urb;
1159         struct musb_hw_ep       *hw_ep = musb->endpoints + epnum;
1160         void __iomem            *epio = hw_ep->regs;
1161         struct musb_qh          *qh = hw_ep->out_qh;
1162         u32                     status = 0;
1163         void __iomem            *mbase = musb->mregs;
1164         struct dma_channel      *dma;
1165
1166         urb = next_urb(qh);
1167
1168         musb_ep_select(mbase, epnum);
1169         tx_csr = musb_readw(epio, MUSB_TXCSR);
1170
1171         /* with CPPI, DMA sometimes triggers "extra" irqs */
1172         if (!urb) {
1173                 DBG(4, "extra TX%d ready, csr %04x\n", epnum, tx_csr);
1174                 goto finish;
1175         }
1176
1177         pipe = urb->pipe;
1178         dma = is_dma_capable() ? hw_ep->tx_channel : NULL;
1179         DBG(4, "OUT/TX%d end, csr %04x%s\n", epnum, tx_csr,
1180                         dma ? ", dma" : "");
1181
1182         /* check for errors */
1183         if (tx_csr & MUSB_TXCSR_H_RXSTALL) {
1184                 /* dma was disabled, fifo flushed */
1185                 DBG(3, "TX end %d stall\n", epnum);
1186
1187                 /* stall; record URB status */
1188                 status = -EPIPE;
1189
1190         } else if (tx_csr & MUSB_TXCSR_H_ERROR) {
1191                 /* (NON-ISO) dma was disabled, fifo flushed */
1192                 DBG(3, "TX 3strikes on ep=%d\n", epnum);
1193
1194                 status = -ETIMEDOUT;
1195
1196         } else if (tx_csr & MUSB_TXCSR_H_NAKTIMEOUT) {
1197                 DBG(6, "TX end=%d device not responding\n", epnum);
1198
1199                 /* NOTE:  this code path would be a good place to PAUSE a
1200                  * transfer, if there's some other (nonperiodic) tx urb
1201                  * that could use this fifo.  (dma complicates it...)
1202                  *
1203                  * if (bulk && qh->ring.next != &musb->out_bulk), then
1204                  * we have a candidate... NAKing is *NOT* an error
1205                  */
1206                 musb_ep_select(mbase, epnum);
1207                 musb_writew(epio, MUSB_CSR0,
1208                                 MUSB_TXCSR_H_WZC_BITS
1209                                 | MUSB_TXCSR_TXPKTRDY);
1210                 goto finish;
1211         }
1212
1213         if (status) {
1214                 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1215                         dma->status = MUSB_DMA_STATUS_CORE_ABORT;
1216                         (void) musb->dma_controller->channel_abort(dma);
1217                 }
1218
1219                 /* do the proper sequence to abort the transfer in the
1220                  * usb core; the dma engine should already be stopped.
1221                  */
1222                 musb_h_tx_flush_fifo(hw_ep);
1223                 tx_csr &= ~(MUSB_TXCSR_AUTOSET
1224                                 | MUSB_TXCSR_DMAENAB
1225                                 | MUSB_TXCSR_H_ERROR
1226                                 | MUSB_TXCSR_H_RXSTALL
1227                                 | MUSB_TXCSR_H_NAKTIMEOUT
1228                                 );
1229
1230                 musb_ep_select(mbase, epnum);
1231                 musb_writew(epio, MUSB_TXCSR, tx_csr);
1232                 /* REVISIT may need to clear FLUSHFIFO ... */
1233                 musb_writew(epio, MUSB_TXCSR, tx_csr);
1234                 musb_writeb(epio, MUSB_TXINTERVAL, 0);
1235
1236                 done = true;
1237         }
1238
1239         /* second cppi case */
1240         if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1241                 DBG(4, "extra TX%d ready, csr %04x\n", epnum, tx_csr);
1242                 goto finish;
1243
1244         }
1245
1246         /* REVISIT this looks wrong... */
1247         if (!status || dma || usb_pipeisoc(pipe)) {
1248                 if (dma)
1249                         wLength = dma->actual_len;
1250                 else
1251                         wLength = qh->segsize;
1252                 qh->offset += wLength;
1253
1254                 if (usb_pipeisoc(pipe)) {
1255                         struct usb_iso_packet_descriptor        *d;
1256
1257                         d = urb->iso_frame_desc + qh->iso_idx;
1258                         d->actual_length = qh->segsize;
1259                         if (++qh->iso_idx >= urb->number_of_packets) {
1260                                 done = true;
1261                         } else {
1262                                 d++;
1263                                 buf = urb->transfer_buffer + d->offset;
1264                                 wLength = d->length;
1265                         }
1266                 } else if (dma) {
1267                         done = true;
1268                 } else {
1269                         /* see if we need to send more data, or ZLP */
1270                         if (qh->segsize < qh->maxpacket)
1271                                 done = true;
1272                         else if (qh->offset == urb->transfer_buffer_length
1273                                         && !(urb-> transfer_flags
1274                                                         & URB_ZERO_PACKET))
1275                                 done = true;
1276                         if (!done) {
1277                                 buf = urb->transfer_buffer
1278                                                 + qh->offset;
1279                                 wLength = urb->transfer_buffer_length
1280                                                 - qh->offset;
1281                         }
1282                 }
1283         }
1284
1285         /* urb->status != -EINPROGRESS means request has been faulted,
1286          * so we must abort this transfer after cleanup
1287          */
1288         if (urb->status != -EINPROGRESS) {
1289                 done = true;
1290                 if (status == 0)
1291                         status = urb->status;
1292         }
1293
1294         if (done) {
1295                 /* set status */
1296                 urb->status = status;
1297                 urb->actual_length = qh->offset;
1298                 musb_advance_schedule(musb, urb, hw_ep, USB_DIR_OUT);
1299
1300         } else if (!(tx_csr & MUSB_TXCSR_DMAENAB)) {
1301                 /* WARN_ON(!buf); */
1302
1303                 /* REVISIT:  some docs say that when hw_ep->tx_double_buffered,
1304                  * (and presumably, fifo is not half-full) we should write TWO
1305                  * packets before updating TXCSR ... other docs disagree ...
1306                  */
1307                 /* PIO:  start next packet in this URB */
1308                 wLength = min(qh->maxpacket, (u16) wLength);
1309                 musb_write_fifo(hw_ep, wLength, buf);
1310                 qh->segsize = wLength;
1311
1312                 musb_ep_select(mbase, epnum);
1313                 musb_writew(epio, MUSB_TXCSR,
1314                                 MUSB_TXCSR_H_WZC_BITS | MUSB_TXCSR_TXPKTRDY);
1315         } else
1316                 DBG(1, "not complete, but dma enabled?\n");
1317
1318 finish:
1319         return;
1320 }
1321
1322
1323 #ifdef CONFIG_USB_INVENTRA_DMA
1324
1325 /* Host side RX (IN) using Mentor DMA works as follows:
1326         submit_urb ->
1327                 - if queue was empty, ProgramEndpoint
1328                 - first IN token is sent out (by setting ReqPkt)
1329         LinuxIsr -> RxReady()
1330         /\      => first packet is received
1331         |       - Set in mode 0 (DmaEnab, ~ReqPkt)
1332         |               -> DMA Isr (transfer complete) -> RxReady()
1333         |                   - Ack receive (~RxPktRdy), turn off DMA (~DmaEnab)
1334         |                   - if urb not complete, send next IN token (ReqPkt)
1335         |                          |            else complete urb.
1336         |                          |
1337         ---------------------------
1338  *
1339  * Nuances of mode 1:
1340  *      For short packets, no ack (+RxPktRdy) is sent automatically
1341  *      (even if AutoClear is ON)
1342  *      For full packets, ack (~RxPktRdy) and next IN token (+ReqPkt) is sent
1343  *      automatically => major problem, as collecting the next packet becomes
1344  *      difficult. Hence mode 1 is not used.
1345  *
1346  * REVISIT
1347  *      All we care about at this driver level is that
1348  *       (a) all URBs terminate with REQPKT cleared and fifo(s) empty;
1349  *       (b) termination conditions are: short RX, or buffer full;
1350  *       (c) fault modes include
1351  *           - iff URB_SHORT_NOT_OK, short RX status is -EREMOTEIO.
1352  *             (and that endpoint's dma queue stops immediately)
1353  *           - overflow (full, PLUS more bytes in the terminal packet)
1354  *
1355  *      So for example, usb-storage sets URB_SHORT_NOT_OK, and would
1356  *      thus be a great candidate for using mode 1 ... for all but the
1357  *      last packet of one URB's transfer.
1358  */
1359
1360 #endif
1361
1362 /*
1363  * Service an RX interrupt for the given IN endpoint; docs cover bulk, iso,
1364  * and high-bandwidth IN transfer cases.
1365  */
1366 void musb_host_rx(struct musb *musb, u8 epnum)
1367 {
1368         struct urb              *urb;
1369         struct musb_hw_ep       *hw_ep = musb->endpoints + epnum;
1370         void __iomem            *epio = hw_ep->regs;
1371         struct musb_qh          *qh = hw_ep->in_qh;
1372         size_t                  xfer_len;
1373         void __iomem            *mbase = musb->mregs;
1374         int                     pipe;
1375         u16                     rx_csr, val;
1376         bool                    iso_err = false;
1377         bool                    done = false;
1378         u32                     status;
1379         struct dma_channel      *dma;
1380
1381         musb_ep_select(mbase, epnum);
1382
1383         urb = next_urb(qh);
1384         dma = is_dma_capable() ? hw_ep->rx_channel : NULL;
1385         status = 0;
1386         xfer_len = 0;
1387
1388         val = rx_csr = musb_readw(epio, MUSB_RXCSR);
1389
1390         if (unlikely(!urb)) {
1391                 /* REVISIT -- THIS SHOULD NEVER HAPPEN ... but, at least
1392                  * usbtest #11 (unlinks) triggers it regularly, sometimes
1393                  * with fifo full.  (Only with DMA??)
1394                  */
1395                 DBG(3, "BOGUS RX%d ready, csr %04x, count %d\n", epnum, val,
1396                         musb_readw(epio, MUSB_RXCOUNT));
1397                 musb_h_flush_rxfifo(hw_ep, MUSB_RXCSR_CLRDATATOG);
1398                 return;
1399         }
1400
1401         pipe = urb->pipe;
1402
1403         DBG(5, "<== hw %d rxcsr %04x, urb actual %d (+dma %zd)\n",
1404                 epnum, rx_csr, urb->actual_length,
1405                 dma ? dma->actual_len : 0);
1406
1407         /* check for errors, concurrent stall & unlink is not really
1408          * handled yet! */
1409         if (rx_csr & MUSB_RXCSR_H_RXSTALL) {
1410                 DBG(3, "RX end %d STALL\n", epnum);
1411
1412                 /* stall; record URB status */
1413                 status = -EPIPE;
1414
1415         } else if (rx_csr & MUSB_RXCSR_H_ERROR) {
1416                 DBG(3, "end %d RX proto error\n", epnum);
1417
1418                 status = -EPROTO;
1419                 musb_writeb(epio, MUSB_RXINTERVAL, 0);
1420
1421         } else if (rx_csr & MUSB_RXCSR_DATAERROR) {
1422
1423                 if (USB_ENDPOINT_XFER_ISOC != qh->type) {
1424                         /* NOTE this code path would be a good place to PAUSE a
1425                          * transfer, if there's some other (nonperiodic) rx urb
1426                          * that could use this fifo.  (dma complicates it...)
1427                          *
1428                          * if (bulk && qh->ring.next != &musb->in_bulk), then
1429                          * we have a candidate... NAKing is *NOT* an error
1430                          */
1431                         DBG(6, "RX end %d NAK timeout\n", epnum);
1432                         musb_ep_select(mbase, epnum);
1433                         musb_writew(epio, MUSB_RXCSR,
1434                                         MUSB_RXCSR_H_WZC_BITS
1435                                         | MUSB_RXCSR_H_REQPKT);
1436
1437                         goto finish;
1438                 } else {
1439                         DBG(4, "RX end %d ISO data error\n", epnum);
1440                         /* packet error reported later */
1441                         iso_err = true;
1442                 }
1443         }
1444
1445         /* faults abort the transfer */
1446         if (status) {
1447                 /* clean up dma and collect transfer count */
1448                 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1449                         dma->status = MUSB_DMA_STATUS_CORE_ABORT;
1450                         (void) musb->dma_controller->channel_abort(dma);
1451                         xfer_len = dma->actual_len;
1452                 }
1453                 musb_h_flush_rxfifo(hw_ep, 0);
1454                 musb_writeb(epio, MUSB_RXINTERVAL, 0);
1455                 done = true;
1456                 goto finish;
1457         }
1458
1459         if (unlikely(dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY)) {
1460                 /* SHOULD NEVER HAPPEN ... but at least DaVinci has done it */
1461                 ERR("RX%d dma busy, csr %04x\n", epnum, rx_csr);
1462                 goto finish;
1463         }
1464
1465         /* thorough shutdown for now ... given more precise fault handling
1466          * and better queueing support, we might keep a DMA pipeline going
1467          * while processing this irq for earlier completions.
1468          */
1469
1470         /* FIXME this is _way_ too much in-line logic for Mentor DMA */
1471
1472 #ifndef CONFIG_USB_INVENTRA_DMA
1473         if (rx_csr & MUSB_RXCSR_H_REQPKT)  {
1474                 /* REVISIT this happened for a while on some short reads...
1475                  * the cleanup still needs investigation... looks bad...
1476                  * and also duplicates dma cleanup code above ... plus,
1477                  * shouldn't this be the "half full" double buffer case?
1478                  */
1479                 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1480                         dma->status = MUSB_DMA_STATUS_CORE_ABORT;
1481                         (void) musb->dma_controller->channel_abort(dma);
1482                         xfer_len = dma->actual_len;
1483                         done = true;
1484                 }
1485
1486                 DBG(2, "RXCSR%d %04x, reqpkt, len %zd%s\n", epnum, rx_csr,
1487                                 xfer_len, dma ? ", dma" : "");
1488                 rx_csr &= ~MUSB_RXCSR_H_REQPKT;
1489
1490                 musb_ep_select(mbase, epnum);
1491                 musb_writew(epio, MUSB_RXCSR,
1492                                 MUSB_RXCSR_H_WZC_BITS | rx_csr);
1493         }
1494 #endif
1495         if (dma && (rx_csr & MUSB_RXCSR_DMAENAB)) {
1496                 xfer_len = dma->actual_len;
1497
1498                 val &= ~(MUSB_RXCSR_DMAENAB
1499                         | MUSB_RXCSR_H_AUTOREQ
1500                         | MUSB_RXCSR_AUTOCLEAR
1501                         | MUSB_RXCSR_RXPKTRDY);
1502                 musb_writew(hw_ep->regs, MUSB_RXCSR, val);
1503
1504 #ifdef CONFIG_USB_INVENTRA_DMA
1505                 /* done if urb buffer is full or short packet is recd */
1506                 done = (urb->actual_length + xfer_len >=
1507                                 urb->transfer_buffer_length
1508                         || dma->actual_len < qh->maxpacket);
1509
1510                 /* send IN token for next packet, without AUTOREQ */
1511                 if (!done) {
1512                         val |= MUSB_RXCSR_H_REQPKT;
1513                         musb_writew(epio, MUSB_RXCSR,
1514                                 MUSB_RXCSR_H_WZC_BITS | val);
1515                 }
1516
1517                 DBG(4, "ep %d dma %s, rxcsr %04x, rxcount %d\n", epnum,
1518                         done ? "off" : "reset",
1519                         musb_readw(epio, MUSB_RXCSR),
1520                         musb_readw(epio, MUSB_RXCOUNT));
1521 #else
1522                 done = true;
1523 #endif
1524         } else if (urb->status == -EINPROGRESS) {
1525                 /* if no errors, be sure a packet is ready for unloading */
1526                 if (unlikely(!(rx_csr & MUSB_RXCSR_RXPKTRDY))) {
1527                         status = -EPROTO;
1528                         ERR("Rx interrupt with no errors or packet!\n");
1529
1530                         /* FIXME this is another "SHOULD NEVER HAPPEN" */
1531
1532 /* SCRUB (RX) */
1533                         /* do the proper sequence to abort the transfer */
1534                         musb_ep_select(mbase, epnum);
1535                         val &= ~MUSB_RXCSR_H_REQPKT;
1536                         musb_writew(epio, MUSB_RXCSR, val);
1537                         goto finish;
1538                 }
1539
1540                 /* we are expecting IN packets */
1541 #ifdef CONFIG_USB_INVENTRA_DMA
1542                 if (dma) {
1543                         struct dma_controller   *c;
1544                         u16                     rx_count;
1545                         int                     status;
1546
1547                         rx_count = musb_readw(epio, MUSB_RXCOUNT);
1548
1549                         DBG(2, "RX%d count %d, buffer 0x%x len %d/%d\n",
1550                                         epnum, rx_count,
1551                                         urb->transfer_dma
1552                                                 + urb->actual_length,
1553                                         qh->offset,
1554                                         urb->transfer_buffer_length);
1555
1556                         c = musb->dma_controller;
1557
1558                         dma->desired_mode = 0;
1559 #ifdef USE_MODE1
1560                         /* because of the issue below, mode 1 will
1561                          * only rarely behave with correct semantics.
1562                          */
1563                         if ((urb->transfer_flags &
1564                                                 URB_SHORT_NOT_OK)
1565                                 && (urb->transfer_buffer_length -
1566                                                 urb->actual_length)
1567                                         > qh->maxpacket)
1568                                 dma->desired_mode = 1;
1569 #endif
1570
1571 /* Disadvantage of using mode 1:
1572  *      It's basically usable only for mass storage class; essentially all
1573  *      other protocols also terminate transfers on short packets.
1574  *
1575  * Details:
1576  *      An extra IN token is sent at the end of the transfer (due to AUTOREQ)
1577  *      If you try to use mode 1 for (transfer_buffer_length - 512), and try
1578  *      to use the extra IN token to grab the last packet using mode 0, then
1579  *      the problem is that you cannot be sure when the device will send the
1580  *      last packet and RxPktRdy set. Sometimes the packet is recd too soon
1581  *      such that it gets lost when RxCSR is re-set at the end of the mode 1
1582  *      transfer, while sometimes it is recd just a little late so that if you
1583  *      try to configure for mode 0 soon after the mode 1 transfer is
1584  *      completed, you will find rxcount 0. Okay, so you might think why not
1585  *      wait for an interrupt when the pkt is recd. Well, you won't get any!
1586  */
1587
1588                         val = musb_readw(epio, MUSB_RXCSR);
1589                         val &= ~MUSB_RXCSR_H_REQPKT;
1590
1591                         if (dma->desired_mode == 0)
1592                                 val &= ~MUSB_RXCSR_H_AUTOREQ;
1593                         else
1594                                 val |= MUSB_RXCSR_H_AUTOREQ;
1595                         val |= MUSB_RXCSR_AUTOCLEAR | MUSB_RXCSR_DMAENAB;
1596
1597                         musb_writew(epio, MUSB_RXCSR,
1598                                 MUSB_RXCSR_H_WZC_BITS | val);
1599
1600                         /* REVISIT if when actual_length != 0,
1601                          * transfer_buffer_length needs to be
1602                          * adjusted first...
1603                          */
1604                         status = c->channel_program(
1605                                 dma, qh->maxpacket,
1606                                 dma->desired_mode,
1607                                 urb->transfer_dma
1608                                         + urb->actual_length,
1609                                 (dma->desired_mode == 0)
1610                                         ? rx_count
1611                                         : urb->transfer_buffer_length);
1612
1613                         if (!status) {
1614                                 c->channel_release(dma);
1615                                 dma = hw_ep->rx_channel = NULL;
1616                                 /* REVISIT reset CSR */
1617                         }
1618                 }
1619 #endif  /* Mentor DMA */
1620
1621                 if (!dma) {
1622                         done = musb_host_packet_rx(musb, urb,
1623                                         epnum, iso_err);
1624                         DBG(6, "read %spacket\n", done ? "last " : "");
1625                 }
1626         }
1627
1628         if (dma && usb_pipeisoc(pipe)) {
1629                 struct usb_iso_packet_descriptor        *d;
1630                 int                                     iso_stat = status;
1631
1632                 d = urb->iso_frame_desc + qh->iso_idx;
1633                 d->actual_length += xfer_len;
1634                 if (iso_err) {
1635                         iso_stat = -EILSEQ;
1636                         urb->error_count++;
1637                 }
1638                 d->status = iso_stat;
1639         }
1640
1641 finish:
1642         urb->actual_length += xfer_len;
1643         qh->offset += xfer_len;
1644         if (done) {
1645                 if (urb->status == -EINPROGRESS)
1646                         urb->status = status;
1647                 musb_advance_schedule(musb, urb, hw_ep, USB_DIR_IN);
1648         }
1649 }
1650
1651 /* schedule nodes correspond to peripheral endpoints, like an OHCI QH.
1652  * the software schedule associates multiple such nodes with a given
1653  * host side hardware endpoint + direction; scheduling may activate
1654  * that hardware endpoint.
1655  */
1656 static int musb_schedule(
1657         struct musb             *musb,
1658         struct musb_qh          *qh,
1659         int                     is_in)
1660 {
1661         int                     idle;
1662         int                     best_diff;
1663         int                     best_end, epnum;
1664         struct musb_hw_ep       *hw_ep = NULL;
1665         struct list_head        *head = NULL;
1666
1667         /* use fixed hardware for control and bulk */
1668         switch (qh->type) {
1669         case USB_ENDPOINT_XFER_CONTROL:
1670                 head = &musb->control;
1671                 hw_ep = musb->control_ep;
1672                 break;
1673         case USB_ENDPOINT_XFER_BULK:
1674                 hw_ep = musb->bulk_ep;
1675                 if (is_in)
1676                         head = &musb->in_bulk;
1677                 else
1678                         head = &musb->out_bulk;
1679                 break;
1680         }
1681         if (head) {
1682                 idle = list_empty(head);
1683                 list_add_tail(&qh->ring, head);
1684                 goto success;
1685         }
1686
1687         /* else, periodic transfers get muxed to other endpoints */
1688
1689         /* FIXME this doesn't consider direction, so it can only
1690          * work for one half of the endpoint hardware, and assumes
1691          * the previous cases handled all non-shared endpoints...
1692          */
1693
1694         /* we know this qh hasn't been scheduled, so all we need to do
1695          * is choose which hardware endpoint to put it on ...
1696          *
1697          * REVISIT what we really want here is a regular schedule tree
1698          * like e.g. OHCI uses, but for now musb->periodic is just an
1699          * array of the _single_ logical endpoint associated with a
1700          * given physical one (identity mapping logical->physical).
1701          *
1702          * that simplistic approach makes TT scheduling a lot simpler;
1703          * there is none, and thus none of its complexity...
1704          */
1705         best_diff = 4096;
1706         best_end = -1;
1707
1708         for (epnum = 1; epnum < musb->nr_endpoints; epnum++) {
1709                 int     diff;
1710
1711                 if (musb->periodic[epnum])
1712                         continue;
1713                 hw_ep = &musb->endpoints[epnum];
1714                 if (hw_ep == musb->bulk_ep)
1715                         continue;
1716
1717                 if (is_in)
1718                         diff = hw_ep->max_packet_sz_rx - qh->maxpacket;
1719                 else
1720                         diff = hw_ep->max_packet_sz_tx - qh->maxpacket;
1721
1722                 if (diff > 0 && best_diff > diff) {
1723                         best_diff = diff;
1724                         best_end = epnum;
1725                 }
1726         }
1727         if (best_end < 0)
1728                 return -ENOSPC;
1729
1730         idle = 1;
1731         hw_ep = musb->endpoints + best_end;
1732         musb->periodic[best_end] = qh;
1733         DBG(4, "qh %p periodic slot %d\n", qh, best_end);
1734 success:
1735         qh->hw_ep = hw_ep;
1736         qh->hep->hcpriv = qh;
1737         if (idle)
1738                 musb_start_urb(musb, is_in, qh);
1739         return 0;
1740 }
1741
1742 static int musb_urb_enqueue(
1743         struct usb_hcd                  *hcd,
1744         struct urb                      *urb,
1745         gfp_t                           mem_flags)
1746 {
1747         unsigned long                   flags;
1748         struct musb                     *musb = hcd_to_musb(hcd);
1749         struct usb_host_endpoint        *hep = urb->ep;
1750         struct musb_qh                  *qh = hep->hcpriv;
1751         struct usb_endpoint_descriptor  *epd = &hep->desc;
1752         int                             ret;
1753         unsigned                        type_reg;
1754         unsigned                        interval;
1755
1756         /* host role must be active */
1757         if (!is_host_active(musb) || !musb->is_active)
1758                 return -ENODEV;
1759
1760         ret = usb_hcd_link_urb_to_ep(hcd, urb);
1761         if (ret)
1762                 return ret;
1763
1764         /* DMA mapping was already done, if needed, and this urb is on
1765          * hep->urb_list ... so there's little to do unless hep wasn't
1766          * yet scheduled onto a live qh.
1767          *
1768          * REVISIT best to keep hep->hcpriv valid until the endpoint gets
1769          * disabled, testing for empty qh->ring and avoiding qh setup costs
1770          * except for the first urb queued after a config change.
1771          */
1772         if (qh) {
1773                 urb->hcpriv = qh;
1774                 return 0;
1775         }
1776
1777         /* Allocate and initialize qh, minimizing the work done each time
1778          * hw_ep gets reprogrammed, or with irqs blocked.  Then schedule it.
1779          *
1780          * REVISIT consider a dedicated qh kmem_cache, so it's harder
1781          * for bugs in other kernel code to break this driver...
1782          */
1783         qh = kzalloc(sizeof *qh, mem_flags);
1784         if (!qh) {
1785                 usb_hcd_unlink_urb_from_ep(hcd, urb);
1786                 return -ENOMEM;
1787         }
1788
1789         qh->hep = hep;
1790         qh->dev = urb->dev;
1791         INIT_LIST_HEAD(&qh->ring);
1792         qh->is_ready = 1;
1793
1794         qh->maxpacket = le16_to_cpu(epd->wMaxPacketSize);
1795
1796         /* no high bandwidth support yet */
1797         if (qh->maxpacket & ~0x7ff) {
1798                 ret = -EMSGSIZE;
1799                 goto done;
1800         }
1801
1802         qh->epnum = epd->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1803         qh->type = epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
1804
1805         /* NOTE: urb->dev->devnum is wrong during SET_ADDRESS */
1806         qh->addr_reg = (u8) usb_pipedevice(urb->pipe);
1807
1808         /* precompute rxtype/txtype/type0 register */
1809         type_reg = (qh->type << 4) | qh->epnum;
1810         switch (urb->dev->speed) {
1811         case USB_SPEED_LOW:
1812                 type_reg |= 0xc0;
1813                 break;
1814         case USB_SPEED_FULL:
1815                 type_reg |= 0x80;
1816                 break;
1817         default:
1818                 type_reg |= 0x40;
1819         }
1820         qh->type_reg = type_reg;
1821
1822         /* precompute rxinterval/txinterval register */
1823         interval = min((u8)16, epd->bInterval); /* log encoding */
1824         switch (qh->type) {
1825         case USB_ENDPOINT_XFER_INT:
1826                 /* fullspeed uses linear encoding */
1827                 if (USB_SPEED_FULL == urb->dev->speed) {
1828                         interval = epd->bInterval;
1829                         if (!interval)
1830                                 interval = 1;
1831                 }
1832                 /* FALLTHROUGH */
1833         case USB_ENDPOINT_XFER_ISOC:
1834                 /* iso always uses log encoding */
1835                 break;
1836         default:
1837                 /* REVISIT we actually want to use NAK limits, hinting to the
1838                  * transfer scheduling logic to try some other qh, e.g. try
1839                  * for 2 msec first:
1840                  *
1841                  * interval = (USB_SPEED_HIGH == urb->dev->speed) ? 16 : 2;
1842                  *
1843                  * The downside of disabling this is that transfer scheduling
1844                  * gets VERY unfair for nonperiodic transfers; a misbehaving
1845                  * peripheral could make that hurt.  Or for reads, one that's
1846                  * perfectly normal:  network and other drivers keep reads
1847                  * posted at all times, having one pending for a week should
1848                  * be perfectly safe.
1849                  *
1850                  * The upside of disabling it is avoidng transfer scheduling
1851                  * code to put this aside for while.
1852                  */
1853                 interval = 0;
1854         }
1855         qh->intv_reg = interval;
1856
1857         /* precompute addressing for external hub/tt ports */
1858         if (musb->is_multipoint) {
1859                 struct usb_device       *parent = urb->dev->parent;
1860
1861                 if (parent != hcd->self.root_hub) {
1862                         qh->h_addr_reg = (u8) parent->devnum;
1863
1864                         /* set up tt info if needed */
1865                         if (urb->dev->tt) {
1866                                 qh->h_port_reg = (u8) urb->dev->ttport;
1867                                 qh->h_addr_reg |= 0x80;
1868                         }
1869                 }
1870         }
1871
1872         /* invariant: hep->hcpriv is null OR the qh that's already scheduled.
1873          * until we get real dma queues (with an entry for each urb/buffer),
1874          * we only have work to do in the former case.
1875          */
1876         spin_lock_irqsave(&musb->lock, flags);
1877         if (hep->hcpriv) {
1878                 /* some concurrent activity submitted another urb to hep...
1879                  * odd, rare, error prone, but legal.
1880                  */
1881                 kfree(qh);
1882                 ret = 0;
1883         } else
1884                 ret = musb_schedule(musb, qh,
1885                                 epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK);
1886
1887         if (ret == 0) {
1888                 urb->hcpriv = qh;
1889                 /* FIXME set urb->start_frame for iso/intr, it's tested in
1890                  * musb_start_urb(), but otherwise only konicawc cares ...
1891                  */
1892         }
1893         spin_unlock_irqrestore(&musb->lock, flags);
1894
1895 done:
1896         if (ret != 0) {
1897                 usb_hcd_unlink_urb_from_ep(hcd, urb);
1898                 kfree(qh);
1899         }
1900         return ret;
1901 }
1902
1903
1904 /*
1905  * abort a transfer that's at the head of a hardware queue.
1906  * called with controller locked, irqs blocked
1907  * that hardware queue advances to the next transfer, unless prevented
1908  */
1909 static int musb_cleanup_urb(struct urb *urb, struct musb_qh *qh, int is_in)
1910 {
1911         struct musb_hw_ep       *ep = qh->hw_ep;
1912         void __iomem            *epio = ep->regs;
1913         unsigned                hw_end = ep->epnum;
1914         void __iomem            *regs = ep->musb->mregs;
1915         u16                     csr;
1916         int                     status = 0;
1917
1918         musb_ep_select(regs, hw_end);
1919
1920         if (is_dma_capable()) {
1921                 struct dma_channel      *dma;
1922
1923                 dma = is_in ? ep->rx_channel : ep->tx_channel;
1924                 if (dma) {
1925                         status = ep->musb->dma_controller->channel_abort(dma);
1926                         DBG(status ? 1 : 3,
1927                                 "abort %cX%d DMA for urb %p --> %d\n",
1928                                 is_in ? 'R' : 'T', ep->epnum,
1929                                 urb, status);
1930                         urb->actual_length += dma->actual_len;
1931                 }
1932         }
1933
1934         /* turn off DMA requests, discard state, stop polling ... */
1935         if (is_in) {
1936                 /* giveback saves bulk toggle */
1937                 csr = musb_h_flush_rxfifo(ep, 0);
1938
1939                 /* REVISIT we still get an irq; should likely clear the
1940                  * endpoint's irq status here to avoid bogus irqs.
1941                  * clearing that status is platform-specific...
1942                  */
1943         } else {
1944                 musb_h_tx_flush_fifo(ep);
1945                 csr = musb_readw(epio, MUSB_TXCSR);
1946                 csr &= ~( MUSB_TXCSR_AUTOSET
1947                         | MUSB_TXCSR_DMAENAB
1948                         | MUSB_TXCSR_H_RXSTALL
1949                         | MUSB_TXCSR_H_NAKTIMEOUT
1950                         | MUSB_TXCSR_H_ERROR
1951                         | MUSB_TXCSR_TXPKTRDY
1952                         );
1953                 musb_writew(epio, MUSB_TXCSR, csr);
1954                 /* REVISIT may need to clear FLUSHFIFO ... */
1955                 musb_writew(epio, MUSB_TXCSR, csr);
1956                 /* flush cpu writebuffer */
1957                 csr = musb_readw(epio, MUSB_TXCSR);
1958         }
1959         if (status == 0)
1960                 musb_advance_schedule(ep->musb, urb, ep, is_in);
1961         return status;
1962 }
1963
1964 static int musb_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
1965 {
1966         struct musb             *musb = hcd_to_musb(hcd);
1967         struct musb_qh          *qh;
1968         struct list_head        *sched;
1969         unsigned long           flags;
1970         int                     ret;
1971
1972         DBG(4, "urb=%p, dev%d ep%d%s\n", urb,
1973                         usb_pipedevice(urb->pipe),
1974                         usb_pipeendpoint(urb->pipe),
1975                         usb_pipein(urb->pipe) ? "in" : "out");
1976
1977         spin_lock_irqsave(&musb->lock, flags);
1978         ret = usb_hcd_check_unlink_urb(hcd, urb, status);
1979         if (ret)
1980                 goto done;
1981
1982         qh = urb->hcpriv;
1983         if (!qh)
1984                 goto done;
1985
1986         /* Any URB not actively programmed into endpoint hardware can be
1987          * immediately given back.  Such an URB must be at the head of its
1988          * endpoint queue, unless someday we get real DMA queues.  And even
1989          * then, it might not be known to the hardware...
1990          *
1991          * Otherwise abort current transfer, pending dma, etc.; urb->status
1992          * has already been updated.  This is a synchronous abort; it'd be
1993          * OK to hold off until after some IRQ, though.
1994          */
1995         if (!qh->is_ready || urb->urb_list.prev != &qh->hep->urb_list)
1996                 ret = -EINPROGRESS;
1997         else {
1998                 switch (qh->type) {
1999                 case USB_ENDPOINT_XFER_CONTROL:
2000                         sched = &musb->control;
2001                         break;
2002                 case USB_ENDPOINT_XFER_BULK:
2003                         if (usb_pipein(urb->pipe))
2004                                 sched = &musb->in_bulk;
2005                         else
2006                                 sched = &musb->out_bulk;
2007                         break;
2008                 default:
2009                         /* REVISIT when we get a schedule tree, periodic
2010                          * transfers won't always be at the head of a
2011                          * singleton queue...
2012                          */
2013                         sched = NULL;
2014                         break;
2015                 }
2016         }
2017
2018         /* NOTE:  qh is invalid unless !list_empty(&hep->urb_list) */
2019         if (ret < 0 || (sched && qh != first_qh(sched))) {
2020                 int     ready = qh->is_ready;
2021
2022                 ret = 0;
2023                 qh->is_ready = 0;
2024                 __musb_giveback(musb, urb, 0);
2025                 qh->is_ready = ready;
2026         } else
2027                 ret = musb_cleanup_urb(urb, qh, urb->pipe & USB_DIR_IN);
2028 done:
2029         spin_unlock_irqrestore(&musb->lock, flags);
2030         return ret;
2031 }
2032
2033 /* disable an endpoint */
2034 static void
2035 musb_h_disable(struct usb_hcd *hcd, struct usb_host_endpoint *hep)
2036 {
2037         u8                      epnum = hep->desc.bEndpointAddress;
2038         unsigned long           flags;
2039         struct musb             *musb = hcd_to_musb(hcd);
2040         u8                      is_in = epnum & USB_DIR_IN;
2041         struct musb_qh          *qh = hep->hcpriv;
2042         struct urb              *urb, *tmp;
2043         struct list_head        *sched;
2044
2045         if (!qh)
2046                 return;
2047
2048         spin_lock_irqsave(&musb->lock, flags);
2049
2050         switch (qh->type) {
2051         case USB_ENDPOINT_XFER_CONTROL:
2052                 sched = &musb->control;
2053                 break;
2054         case USB_ENDPOINT_XFER_BULK:
2055                 if (is_in)
2056                         sched = &musb->in_bulk;
2057                 else
2058                         sched = &musb->out_bulk;
2059                 break;
2060         default:
2061                 /* REVISIT when we get a schedule tree, periodic transfers
2062                  * won't always be at the head of a singleton queue...
2063                  */
2064                 sched = NULL;
2065                 break;
2066         }
2067
2068         /* NOTE:  qh is invalid unless !list_empty(&hep->urb_list) */
2069
2070         /* kick first urb off the hardware, if needed */
2071         qh->is_ready = 0;
2072         if (!sched || qh == first_qh(sched)) {
2073                 urb = next_urb(qh);
2074
2075                 /* make software (then hardware) stop ASAP */
2076                 if (!urb->unlinked)
2077                         urb->status = -ESHUTDOWN;
2078
2079                 /* cleanup */
2080                 musb_cleanup_urb(urb, qh, urb->pipe & USB_DIR_IN);
2081         } else
2082                 urb = NULL;
2083
2084         /* then just nuke all the others */
2085         list_for_each_entry_safe_from(urb, tmp, &hep->urb_list, urb_list)
2086                 musb_giveback(qh, urb, -ESHUTDOWN);
2087
2088         spin_unlock_irqrestore(&musb->lock, flags);
2089 }
2090
2091 static int musb_h_get_frame_number(struct usb_hcd *hcd)
2092 {
2093         struct musb     *musb = hcd_to_musb(hcd);
2094
2095         return musb_readw(musb->mregs, MUSB_FRAME);
2096 }
2097
2098 static int musb_h_start(struct usb_hcd *hcd)
2099 {
2100         struct musb     *musb = hcd_to_musb(hcd);
2101
2102         /* NOTE: musb_start() is called when the hub driver turns
2103          * on port power, or when (OTG) peripheral starts.
2104          */
2105         hcd->state = HC_STATE_RUNNING;
2106         musb->port1_status = 0;
2107         return 0;
2108 }
2109
2110 static void musb_h_stop(struct usb_hcd *hcd)
2111 {
2112         musb_stop(hcd_to_musb(hcd));
2113         hcd->state = HC_STATE_HALT;
2114 }
2115
2116 static int musb_bus_suspend(struct usb_hcd *hcd)
2117 {
2118         struct musb     *musb = hcd_to_musb(hcd);
2119
2120         if (musb->xceiv.state == OTG_STATE_A_SUSPEND)
2121                 return 0;
2122
2123         if (is_host_active(musb) && musb->is_active) {
2124                 WARN("trying to suspend as %s is_active=%i\n",
2125                         otg_state_string(musb), musb->is_active);
2126                 return -EBUSY;
2127         } else
2128                 return 0;
2129 }
2130
2131 static int musb_bus_resume(struct usb_hcd *hcd)
2132 {
2133         /* resuming child port does the work */
2134         return 0;
2135 }
2136
2137 const struct hc_driver musb_hc_driver = {
2138         .description            = "musb-hcd",
2139         .product_desc           = "MUSB HDRC host driver",
2140         .hcd_priv_size          = sizeof (struct musb),
2141         .flags                  = HCD_USB2 | HCD_MEMORY,
2142
2143         /* not using irq handler or reset hooks from usbcore, since
2144          * those must be shared with peripheral code for OTG configs
2145          */
2146
2147         .start                  = musb_h_start,
2148         .stop                   = musb_h_stop,
2149
2150         .get_frame_number       = musb_h_get_frame_number,
2151
2152         .urb_enqueue            = musb_urb_enqueue,
2153         .urb_dequeue            = musb_urb_dequeue,
2154         .endpoint_disable       = musb_h_disable,
2155
2156         .hub_status_data        = musb_hub_status_data,
2157         .hub_control            = musb_hub_control,
2158         .bus_suspend            = musb_bus_suspend,
2159         .bus_resume             = musb_bus_resume,
2160         /* .start_port_reset    = NULL, */
2161         /* .hub_irq_enable      = NULL, */
2162 };