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