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