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