Merge branch 'slabh' of git://git.kernel.org/pub/scm/linux/kernel/git/tj/misc
[sfrench/cifs-2.6.git] / drivers / usb / musb / cppi_dma.c
1 /*
2  * Copyright (C) 2005-2006 by Texas Instruments
3  *
4  * This file implements a DMA  interface using TI's CPPI DMA.
5  * For now it's DaVinci-only, but CPPI isn't specific to DaVinci or USB.
6  * The TUSB6020, using VLYNQ, has CPPI that looks much like DaVinci.
7  */
8
9 #include <linux/platform_device.h>
10 #include <linux/slab.h>
11 #include <linux/usb.h>
12
13 #include "musb_core.h"
14 #include "musb_debug.h"
15 #include "cppi_dma.h"
16
17
18 /* CPPI DMA status 7-mar-2006:
19  *
20  * - See musb_{host,gadget}.c for more info
21  *
22  * - Correct RX DMA generally forces the engine into irq-per-packet mode,
23  *   which can easily saturate the CPU under non-mass-storage loads.
24  *
25  * NOTES 24-aug-2006 (2.6.18-rc4):
26  *
27  * - peripheral RXDMA wedged in a test with packets of length 512/512/1.
28  *   evidently after the 1 byte packet was received and acked, the queue
29  *   of BDs got garbaged so it wouldn't empty the fifo.  (rxcsr 0x2003,
30  *   and RX DMA0: 4 left, 80000000 8feff880, 8feff860 8feff860; 8f321401
31  *   004001ff 00000001 .. 8feff860)  Host was just getting NAKed on tx
32  *   of its next (512 byte) packet.  IRQ issues?
33  *
34  * REVISIT:  the "transfer DMA" glue between CPPI and USB fifos will
35  * evidently also directly update the RX and TX CSRs ... so audit all
36  * host and peripheral side DMA code to avoid CSR access after DMA has
37  * been started.
38  */
39
40 /* REVISIT now we can avoid preallocating these descriptors; or
41  * more simply, switch to a global freelist not per-channel ones.
42  * Note: at full speed, 64 descriptors == 4K bulk data.
43  */
44 #define NUM_TXCHAN_BD       64
45 #define NUM_RXCHAN_BD       64
46
47 static inline void cpu_drain_writebuffer(void)
48 {
49         wmb();
50 #ifdef  CONFIG_CPU_ARM926T
51         /* REVISIT this "should not be needed",
52          * but lack of it sure seemed to hurt ...
53          */
54         asm("mcr p15, 0, r0, c7, c10, 4 @ drain write buffer\n");
55 #endif
56 }
57
58 static inline struct cppi_descriptor *cppi_bd_alloc(struct cppi_channel *c)
59 {
60         struct cppi_descriptor  *bd = c->freelist;
61
62         if (bd)
63                 c->freelist = bd->next;
64         return bd;
65 }
66
67 static inline void
68 cppi_bd_free(struct cppi_channel *c, struct cppi_descriptor *bd)
69 {
70         if (!bd)
71                 return;
72         bd->next = c->freelist;
73         c->freelist = bd;
74 }
75
76 /*
77  *  Start DMA controller
78  *
79  *  Initialize the DMA controller as necessary.
80  */
81
82 /* zero out entire rx state RAM entry for the channel */
83 static void cppi_reset_rx(struct cppi_rx_stateram __iomem *rx)
84 {
85         musb_writel(&rx->rx_skipbytes, 0, 0);
86         musb_writel(&rx->rx_head, 0, 0);
87         musb_writel(&rx->rx_sop, 0, 0);
88         musb_writel(&rx->rx_current, 0, 0);
89         musb_writel(&rx->rx_buf_current, 0, 0);
90         musb_writel(&rx->rx_len_len, 0, 0);
91         musb_writel(&rx->rx_cnt_cnt, 0, 0);
92 }
93
94 /* zero out entire tx state RAM entry for the channel */
95 static void cppi_reset_tx(struct cppi_tx_stateram __iomem *tx, u32 ptr)
96 {
97         musb_writel(&tx->tx_head, 0, 0);
98         musb_writel(&tx->tx_buf, 0, 0);
99         musb_writel(&tx->tx_current, 0, 0);
100         musb_writel(&tx->tx_buf_current, 0, 0);
101         musb_writel(&tx->tx_info, 0, 0);
102         musb_writel(&tx->tx_rem_len, 0, 0);
103         /* musb_writel(&tx->tx_dummy, 0, 0); */
104         musb_writel(&tx->tx_complete, 0, ptr);
105 }
106
107 static void __init cppi_pool_init(struct cppi *cppi, struct cppi_channel *c)
108 {
109         int     j;
110
111         /* initialize channel fields */
112         c->head = NULL;
113         c->tail = NULL;
114         c->last_processed = NULL;
115         c->channel.status = MUSB_DMA_STATUS_UNKNOWN;
116         c->controller = cppi;
117         c->is_rndis = 0;
118         c->freelist = NULL;
119
120         /* build the BD Free list for the channel */
121         for (j = 0; j < NUM_TXCHAN_BD + 1; j++) {
122                 struct cppi_descriptor  *bd;
123                 dma_addr_t              dma;
124
125                 bd = dma_pool_alloc(cppi->pool, GFP_KERNEL, &dma);
126                 bd->dma = dma;
127                 cppi_bd_free(c, bd);
128         }
129 }
130
131 static int cppi_channel_abort(struct dma_channel *);
132
133 static void cppi_pool_free(struct cppi_channel *c)
134 {
135         struct cppi             *cppi = c->controller;
136         struct cppi_descriptor  *bd;
137
138         (void) cppi_channel_abort(&c->channel);
139         c->channel.status = MUSB_DMA_STATUS_UNKNOWN;
140         c->controller = NULL;
141
142         /* free all its bds */
143         bd = c->last_processed;
144         do {
145                 if (bd)
146                         dma_pool_free(cppi->pool, bd, bd->dma);
147                 bd = cppi_bd_alloc(c);
148         } while (bd);
149         c->last_processed = NULL;
150 }
151
152 static int __init cppi_controller_start(struct dma_controller *c)
153 {
154         struct cppi     *controller;
155         void __iomem    *tibase;
156         int             i;
157
158         controller = container_of(c, struct cppi, controller);
159
160         /* do whatever is necessary to start controller */
161         for (i = 0; i < ARRAY_SIZE(controller->tx); i++) {
162                 controller->tx[i].transmit = true;
163                 controller->tx[i].index = i;
164         }
165         for (i = 0; i < ARRAY_SIZE(controller->rx); i++) {
166                 controller->rx[i].transmit = false;
167                 controller->rx[i].index = i;
168         }
169
170         /* setup BD list on a per channel basis */
171         for (i = 0; i < ARRAY_SIZE(controller->tx); i++)
172                 cppi_pool_init(controller, controller->tx + i);
173         for (i = 0; i < ARRAY_SIZE(controller->rx); i++)
174                 cppi_pool_init(controller, controller->rx + i);
175
176         tibase =  controller->tibase;
177         INIT_LIST_HEAD(&controller->tx_complete);
178
179         /* initialise tx/rx channel head pointers to zero */
180         for (i = 0; i < ARRAY_SIZE(controller->tx); i++) {
181                 struct cppi_channel     *tx_ch = controller->tx + i;
182                 struct cppi_tx_stateram __iomem *tx;
183
184                 INIT_LIST_HEAD(&tx_ch->tx_complete);
185
186                 tx = tibase + DAVINCI_TXCPPI_STATERAM_OFFSET(i);
187                 tx_ch->state_ram = tx;
188                 cppi_reset_tx(tx, 0);
189         }
190         for (i = 0; i < ARRAY_SIZE(controller->rx); i++) {
191                 struct cppi_channel     *rx_ch = controller->rx + i;
192                 struct cppi_rx_stateram __iomem *rx;
193
194                 INIT_LIST_HEAD(&rx_ch->tx_complete);
195
196                 rx = tibase + DAVINCI_RXCPPI_STATERAM_OFFSET(i);
197                 rx_ch->state_ram = rx;
198                 cppi_reset_rx(rx);
199         }
200
201         /* enable individual cppi channels */
202         musb_writel(tibase, DAVINCI_TXCPPI_INTENAB_REG,
203                         DAVINCI_DMA_ALL_CHANNELS_ENABLE);
204         musb_writel(tibase, DAVINCI_RXCPPI_INTENAB_REG,
205                         DAVINCI_DMA_ALL_CHANNELS_ENABLE);
206
207         /* enable tx/rx CPPI control */
208         musb_writel(tibase, DAVINCI_TXCPPI_CTRL_REG, DAVINCI_DMA_CTRL_ENABLE);
209         musb_writel(tibase, DAVINCI_RXCPPI_CTRL_REG, DAVINCI_DMA_CTRL_ENABLE);
210
211         /* disable RNDIS mode, also host rx RNDIS autorequest */
212         musb_writel(tibase, DAVINCI_RNDIS_REG, 0);
213         musb_writel(tibase, DAVINCI_AUTOREQ_REG, 0);
214
215         return 0;
216 }
217
218 /*
219  *  Stop DMA controller
220  *
221  *  De-Init the DMA controller as necessary.
222  */
223
224 static int cppi_controller_stop(struct dma_controller *c)
225 {
226         struct cppi             *controller;
227         void __iomem            *tibase;
228         int                     i;
229
230         controller = container_of(c, struct cppi, controller);
231
232         tibase = controller->tibase;
233         /* DISABLE INDIVIDUAL CHANNEL Interrupts */
234         musb_writel(tibase, DAVINCI_TXCPPI_INTCLR_REG,
235                         DAVINCI_DMA_ALL_CHANNELS_ENABLE);
236         musb_writel(tibase, DAVINCI_RXCPPI_INTCLR_REG,
237                         DAVINCI_DMA_ALL_CHANNELS_ENABLE);
238
239         DBG(1, "Tearing down RX and TX Channels\n");
240         for (i = 0; i < ARRAY_SIZE(controller->tx); i++) {
241                 /* FIXME restructure of txdma to use bds like rxdma */
242                 controller->tx[i].last_processed = NULL;
243                 cppi_pool_free(controller->tx + i);
244         }
245         for (i = 0; i < ARRAY_SIZE(controller->rx); i++)
246                 cppi_pool_free(controller->rx + i);
247
248         /* in Tx Case proper teardown is supported. We resort to disabling
249          * Tx/Rx CPPI after cleanup of Tx channels. Before TX teardown is
250          * complete TX CPPI cannot be disabled.
251          */
252         /*disable tx/rx cppi */
253         musb_writel(tibase, DAVINCI_TXCPPI_CTRL_REG, DAVINCI_DMA_CTRL_DISABLE);
254         musb_writel(tibase, DAVINCI_RXCPPI_CTRL_REG, DAVINCI_DMA_CTRL_DISABLE);
255
256         return 0;
257 }
258
259 /* While dma channel is allocated, we only want the core irqs active
260  * for fault reports, otherwise we'd get irqs that we don't care about.
261  * Except for TX irqs, where dma done != fifo empty and reusable ...
262  *
263  * NOTE: docs don't say either way, but irq masking **enables** irqs.
264  *
265  * REVISIT same issue applies to pure PIO usage too, and non-cppi dma...
266  */
267 static inline void core_rxirq_disable(void __iomem *tibase, unsigned epnum)
268 {
269         musb_writel(tibase, DAVINCI_USB_INT_MASK_CLR_REG, 1 << (epnum + 8));
270 }
271
272 static inline void core_rxirq_enable(void __iomem *tibase, unsigned epnum)
273 {
274         musb_writel(tibase, DAVINCI_USB_INT_MASK_SET_REG, 1 << (epnum + 8));
275 }
276
277
278 /*
279  * Allocate a CPPI Channel for DMA.  With CPPI, channels are bound to
280  * each transfer direction of a non-control endpoint, so allocating
281  * (and deallocating) is mostly a way to notice bad housekeeping on
282  * the software side.  We assume the irqs are always active.
283  */
284 static struct dma_channel *
285 cppi_channel_allocate(struct dma_controller *c,
286                 struct musb_hw_ep *ep, u8 transmit)
287 {
288         struct cppi             *controller;
289         u8                      index;
290         struct cppi_channel     *cppi_ch;
291         void __iomem            *tibase;
292
293         controller = container_of(c, struct cppi, controller);
294         tibase = controller->tibase;
295
296         /* ep0 doesn't use DMA; remember cppi indices are 0..N-1 */
297         index = ep->epnum - 1;
298
299         /* return the corresponding CPPI Channel Handle, and
300          * probably disable the non-CPPI irq until we need it.
301          */
302         if (transmit) {
303                 if (index >= ARRAY_SIZE(controller->tx)) {
304                         DBG(1, "no %cX%d CPPI channel\n", 'T', index);
305                         return NULL;
306                 }
307                 cppi_ch = controller->tx + index;
308         } else {
309                 if (index >= ARRAY_SIZE(controller->rx)) {
310                         DBG(1, "no %cX%d CPPI channel\n", 'R', index);
311                         return NULL;
312                 }
313                 cppi_ch = controller->rx + index;
314                 core_rxirq_disable(tibase, ep->epnum);
315         }
316
317         /* REVISIT make this an error later once the same driver code works
318          * with the other DMA engine too
319          */
320         if (cppi_ch->hw_ep)
321                 DBG(1, "re-allocating DMA%d %cX channel %p\n",
322                                 index, transmit ? 'T' : 'R', cppi_ch);
323         cppi_ch->hw_ep = ep;
324         cppi_ch->channel.status = MUSB_DMA_STATUS_FREE;
325
326         DBG(4, "Allocate CPPI%d %cX\n", index, transmit ? 'T' : 'R');
327         return &cppi_ch->channel;
328 }
329
330 /* Release a CPPI Channel.  */
331 static void cppi_channel_release(struct dma_channel *channel)
332 {
333         struct cppi_channel     *c;
334         void __iomem            *tibase;
335
336         /* REVISIT:  for paranoia, check state and abort if needed... */
337
338         c = container_of(channel, struct cppi_channel, channel);
339         tibase = c->controller->tibase;
340         if (!c->hw_ep)
341                 DBG(1, "releasing idle DMA channel %p\n", c);
342         else if (!c->transmit)
343                 core_rxirq_enable(tibase, c->index + 1);
344
345         /* for now, leave its cppi IRQ enabled (we won't trigger it) */
346         c->hw_ep = NULL;
347         channel->status = MUSB_DMA_STATUS_UNKNOWN;
348 }
349
350 /* Context: controller irqlocked */
351 static void
352 cppi_dump_rx(int level, struct cppi_channel *c, const char *tag)
353 {
354         void __iomem                    *base = c->controller->mregs;
355         struct cppi_rx_stateram __iomem *rx = c->state_ram;
356
357         musb_ep_select(base, c->index + 1);
358
359         DBG(level, "RX DMA%d%s: %d left, csr %04x, "
360                         "%08x H%08x S%08x C%08x, "
361                         "B%08x L%08x %08x .. %08x"
362                         "\n",
363                 c->index, tag,
364                 musb_readl(c->controller->tibase,
365                         DAVINCI_RXCPPI_BUFCNT0_REG + 4 * c->index),
366                 musb_readw(c->hw_ep->regs, MUSB_RXCSR),
367
368                 musb_readl(&rx->rx_skipbytes, 0),
369                 musb_readl(&rx->rx_head, 0),
370                 musb_readl(&rx->rx_sop, 0),
371                 musb_readl(&rx->rx_current, 0),
372
373                 musb_readl(&rx->rx_buf_current, 0),
374                 musb_readl(&rx->rx_len_len, 0),
375                 musb_readl(&rx->rx_cnt_cnt, 0),
376                 musb_readl(&rx->rx_complete, 0)
377                 );
378 }
379
380 /* Context: controller irqlocked */
381 static void
382 cppi_dump_tx(int level, struct cppi_channel *c, const char *tag)
383 {
384         void __iomem                    *base = c->controller->mregs;
385         struct cppi_tx_stateram __iomem *tx = c->state_ram;
386
387         musb_ep_select(base, c->index + 1);
388
389         DBG(level, "TX DMA%d%s: csr %04x, "
390                         "H%08x S%08x C%08x %08x, "
391                         "F%08x L%08x .. %08x"
392                         "\n",
393                 c->index, tag,
394                 musb_readw(c->hw_ep->regs, MUSB_TXCSR),
395
396                 musb_readl(&tx->tx_head, 0),
397                 musb_readl(&tx->tx_buf, 0),
398                 musb_readl(&tx->tx_current, 0),
399                 musb_readl(&tx->tx_buf_current, 0),
400
401                 musb_readl(&tx->tx_info, 0),
402                 musb_readl(&tx->tx_rem_len, 0),
403                 /* dummy/unused word 6 */
404                 musb_readl(&tx->tx_complete, 0)
405                 );
406 }
407
408 /* Context: controller irqlocked */
409 static inline void
410 cppi_rndis_update(struct cppi_channel *c, int is_rx,
411                 void __iomem *tibase, int is_rndis)
412 {
413         /* we may need to change the rndis flag for this cppi channel */
414         if (c->is_rndis != is_rndis) {
415                 u32     value = musb_readl(tibase, DAVINCI_RNDIS_REG);
416                 u32     temp = 1 << (c->index);
417
418                 if (is_rx)
419                         temp <<= 16;
420                 if (is_rndis)
421                         value |= temp;
422                 else
423                         value &= ~temp;
424                 musb_writel(tibase, DAVINCI_RNDIS_REG, value);
425                 c->is_rndis = is_rndis;
426         }
427 }
428
429 #ifdef CONFIG_USB_MUSB_DEBUG
430 static void cppi_dump_rxbd(const char *tag, struct cppi_descriptor *bd)
431 {
432         pr_debug("RXBD/%s %08x: "
433                         "nxt %08x buf %08x off.blen %08x opt.plen %08x\n",
434                         tag, bd->dma,
435                         bd->hw_next, bd->hw_bufp, bd->hw_off_len,
436                         bd->hw_options);
437 }
438 #endif
439
440 static void cppi_dump_rxq(int level, const char *tag, struct cppi_channel *rx)
441 {
442 #ifdef CONFIG_USB_MUSB_DEBUG
443         struct cppi_descriptor  *bd;
444
445         if (!_dbg_level(level))
446                 return;
447         cppi_dump_rx(level, rx, tag);
448         if (rx->last_processed)
449                 cppi_dump_rxbd("last", rx->last_processed);
450         for (bd = rx->head; bd; bd = bd->next)
451                 cppi_dump_rxbd("active", bd);
452 #endif
453 }
454
455
456 /* NOTE:  DaVinci autoreq is ignored except for host side "RNDIS" mode RX;
457  * so we won't ever use it (see "CPPI RX Woes" below).
458  */
459 static inline int cppi_autoreq_update(struct cppi_channel *rx,
460                 void __iomem *tibase, int onepacket, unsigned n_bds)
461 {
462         u32     val;
463
464 #ifdef  RNDIS_RX_IS_USABLE
465         u32     tmp;
466         /* assert(is_host_active(musb)) */
467
468         /* start from "AutoReq never" */
469         tmp = musb_readl(tibase, DAVINCI_AUTOREQ_REG);
470         val = tmp & ~((0x3) << (rx->index * 2));
471
472         /* HCD arranged reqpkt for packet #1.  we arrange int
473          * for all but the last one, maybe in two segments.
474          */
475         if (!onepacket) {
476 #if 0
477                 /* use two segments, autoreq "all" then the last "never" */
478                 val |= ((0x3) << (rx->index * 2));
479                 n_bds--;
480 #else
481                 /* one segment, autoreq "all-but-last" */
482                 val |= ((0x1) << (rx->index * 2));
483 #endif
484         }
485
486         if (val != tmp) {
487                 int n = 100;
488
489                 /* make sure that autoreq is updated before continuing */
490                 musb_writel(tibase, DAVINCI_AUTOREQ_REG, val);
491                 do {
492                         tmp = musb_readl(tibase, DAVINCI_AUTOREQ_REG);
493                         if (tmp == val)
494                                 break;
495                         cpu_relax();
496                 } while (n-- > 0);
497         }
498 #endif
499
500         /* REQPKT is turned off after each segment */
501         if (n_bds && rx->channel.actual_len) {
502                 void __iomem    *regs = rx->hw_ep->regs;
503
504                 val = musb_readw(regs, MUSB_RXCSR);
505                 if (!(val & MUSB_RXCSR_H_REQPKT)) {
506                         val |= MUSB_RXCSR_H_REQPKT | MUSB_RXCSR_H_WZC_BITS;
507                         musb_writew(regs, MUSB_RXCSR, val);
508                         /* flush writebufer */
509                         val = musb_readw(regs, MUSB_RXCSR);
510                 }
511         }
512         return n_bds;
513 }
514
515
516 /* Buffer enqueuing Logic:
517  *
518  *  - RX builds new queues each time, to help handle routine "early
519  *    termination" cases (faults, including errors and short reads)
520  *    more correctly.
521  *
522  *  - for now, TX reuses the same queue of BDs every time
523  *
524  * REVISIT long term, we want a normal dynamic model.
525  * ... the goal will be to append to the
526  * existing queue, processing completed "dma buffers" (segments) on the fly.
527  *
528  * Otherwise we force an IRQ latency between requests, which slows us a lot
529  * (especially in "transparent" dma).  Unfortunately that model seems to be
530  * inherent in the DMA model from the Mentor code, except in the rare case
531  * of transfers big enough (~128+ KB) that we could append "middle" segments
532  * in the TX paths.  (RX can't do this, see below.)
533  *
534  * That's true even in the CPPI- friendly iso case, where most urbs have
535  * several small segments provided in a group and where the "packet at a time"
536  * "transparent" DMA model is always correct, even on the RX side.
537  */
538
539 /*
540  * CPPI TX:
541  * ========
542  * TX is a lot more reasonable than RX; it doesn't need to run in
543  * irq-per-packet mode very often.  RNDIS mode seems to behave too
544  * (except how it handles the exactly-N-packets case).  Building a
545  * txdma queue with multiple requests (urb or usb_request) looks
546  * like it would work ... but fault handling would need much testing.
547  *
548  * The main issue with TX mode RNDIS relates to transfer lengths that
549  * are an exact multiple of the packet length.  It appears that there's
550  * a hiccup in that case (maybe the DMA completes before the ZLP gets
551  * written?) boiling down to not being able to rely on CPPI writing any
552  * terminating zero length packet before the next transfer is written.
553  * So that's punted to PIO; better yet, gadget drivers can avoid it.
554  *
555  * Plus, there's allegedly an undocumented constraint that rndis transfer
556  * length be a multiple of 64 bytes ... but the chip doesn't act that
557  * way, and we really don't _want_ that behavior anyway.
558  *
559  * On TX, "transparent" mode works ... although experiments have shown
560  * problems trying to use the SOP/EOP bits in different USB packets.
561  *
562  * REVISIT try to handle terminating zero length packets using CPPI
563  * instead of doing it by PIO after an IRQ.  (Meanwhile, make Ethernet
564  * links avoid that issue by forcing them to avoid zlps.)
565  */
566 static void
567 cppi_next_tx_segment(struct musb *musb, struct cppi_channel *tx)
568 {
569         unsigned                maxpacket = tx->maxpacket;
570         dma_addr_t              addr = tx->buf_dma + tx->offset;
571         size_t                  length = tx->buf_len - tx->offset;
572         struct cppi_descriptor  *bd;
573         unsigned                n_bds;
574         unsigned                i;
575         struct cppi_tx_stateram __iomem *tx_ram = tx->state_ram;
576         int                     rndis;
577
578         /* TX can use the CPPI "rndis" mode, where we can probably fit this
579          * transfer in one BD and one IRQ.  The only time we would NOT want
580          * to use it is when hardware constraints prevent it, or if we'd
581          * trigger the "send a ZLP?" confusion.
582          */
583         rndis = (maxpacket & 0x3f) == 0
584                 && length > maxpacket
585                 && length < 0xffff
586                 && (length % maxpacket) != 0;
587
588         if (rndis) {
589                 maxpacket = length;
590                 n_bds = 1;
591         } else {
592                 n_bds = length / maxpacket;
593                 if (!length || (length % maxpacket))
594                         n_bds++;
595                 n_bds = min(n_bds, (unsigned) NUM_TXCHAN_BD);
596                 length = min(n_bds * maxpacket, length);
597         }
598
599         DBG(4, "TX DMA%d, pktSz %d %s bds %d dma 0x%x len %u\n",
600                         tx->index,
601                         maxpacket,
602                         rndis ? "rndis" : "transparent",
603                         n_bds,
604                         addr, length);
605
606         cppi_rndis_update(tx, 0, musb->ctrl_base, rndis);
607
608         /* assuming here that channel_program is called during
609          * transfer initiation ... current code maintains state
610          * for one outstanding request only (no queues, not even
611          * the implicit ones of an iso urb).
612          */
613
614         bd = tx->freelist;
615         tx->head = bd;
616         tx->last_processed = NULL;
617
618         /* FIXME use BD pool like RX side does, and just queue
619          * the minimum number for this request.
620          */
621
622         /* Prepare queue of BDs first, then hand it to hardware.
623          * All BDs except maybe the last should be of full packet
624          * size; for RNDIS there _is_ only that last packet.
625          */
626         for (i = 0; i < n_bds; ) {
627                 if (++i < n_bds && bd->next)
628                         bd->hw_next = bd->next->dma;
629                 else
630                         bd->hw_next = 0;
631
632                 bd->hw_bufp = tx->buf_dma + tx->offset;
633
634                 /* FIXME set EOP only on the last packet,
635                  * SOP only on the first ... avoid IRQs
636                  */
637                 if ((tx->offset + maxpacket) <= tx->buf_len) {
638                         tx->offset += maxpacket;
639                         bd->hw_off_len = maxpacket;
640                         bd->hw_options = CPPI_SOP_SET | CPPI_EOP_SET
641                                 | CPPI_OWN_SET | maxpacket;
642                 } else {
643                         /* only this one may be a partial USB Packet */
644                         u32             partial_len;
645
646                         partial_len = tx->buf_len - tx->offset;
647                         tx->offset = tx->buf_len;
648                         bd->hw_off_len = partial_len;
649
650                         bd->hw_options = CPPI_SOP_SET | CPPI_EOP_SET
651                                 | CPPI_OWN_SET | partial_len;
652                         if (partial_len == 0)
653                                 bd->hw_options |= CPPI_ZERO_SET;
654                 }
655
656                 DBG(5, "TXBD %p: nxt %08x buf %08x len %04x opt %08x\n",
657                                 bd, bd->hw_next, bd->hw_bufp,
658                                 bd->hw_off_len, bd->hw_options);
659
660                 /* update the last BD enqueued to the list */
661                 tx->tail = bd;
662                 bd = bd->next;
663         }
664
665         /* BDs live in DMA-coherent memory, but writes might be pending */
666         cpu_drain_writebuffer();
667
668         /* Write to the HeadPtr in state RAM to trigger */
669         musb_writel(&tx_ram->tx_head, 0, (u32)tx->freelist->dma);
670
671         cppi_dump_tx(5, tx, "/S");
672 }
673
674 /*
675  * CPPI RX Woes:
676  * =============
677  * Consider a 1KB bulk RX buffer in two scenarios:  (a) it's fed two 300 byte
678  * packets back-to-back, and (b) it's fed two 512 byte packets back-to-back.
679  * (Full speed transfers have similar scenarios.)
680  *
681  * The correct behavior for Linux is that (a) fills the buffer with 300 bytes,
682  * and the next packet goes into a buffer that's queued later; while (b) fills
683  * the buffer with 1024 bytes.  How to do that with CPPI?
684  *
685  * - RX queues in "rndis" mode -- one single BD -- handle (a) correctly, but
686  *   (b) loses **BADLY** because nothing (!) happens when that second packet
687  *   fills the buffer, much less when a third one arrives.  (Which makes this
688  *   not a "true" RNDIS mode.  In the RNDIS protocol short-packet termination
689  *   is optional, and it's fine if peripherals -- not hosts! -- pad messages
690  *   out to end-of-buffer.  Standard PCI host controller DMA descriptors
691  *   implement that mode by default ... which is no accident.)
692  *
693  * - RX queues in "transparent" mode -- two BDs with 512 bytes each -- have
694  *   converse problems:  (b) is handled right, but (a) loses badly.  CPPI RX
695  *   ignores SOP/EOP markings and processes both of those BDs; so both packets
696  *   are loaded into the buffer (with a 212 byte gap between them), and the next
697  *   buffer queued will NOT get its 300 bytes of data. (It seems like SOP/EOP
698  *   are intended as outputs for RX queues, not inputs...)
699  *
700  * - A variant of "transparent" mode -- one BD at a time -- is the only way to
701  *   reliably make both cases work, with software handling both cases correctly
702  *   and at the significant penalty of needing an IRQ per packet.  (The lack of
703  *   I/O overlap can be slightly ameliorated by enabling double buffering.)
704  *
705  * So how to get rid of IRQ-per-packet?  The transparent multi-BD case could
706  * be used in special cases like mass storage, which sets URB_SHORT_NOT_OK
707  * (or maybe its peripheral side counterpart) to flag (a) scenarios as errors
708  * with guaranteed driver level fault recovery and scrubbing out what's left
709  * of that garbaged datastream.
710  *
711  * But there seems to be no way to identify the cases where CPPI RNDIS mode
712  * is appropriate -- which do NOT include RNDIS host drivers, but do include
713  * the CDC Ethernet driver! -- and the documentation is incomplete/wrong.
714  * So we can't _ever_ use RX RNDIS mode ... except by using a heuristic
715  * that applies best on the peripheral side (and which could fail rudely).
716  *
717  * Leaving only "transparent" mode; we avoid multi-bd modes in almost all
718  * cases other than mass storage class.  Otherwise we're correct but slow,
719  * since CPPI penalizes our need for a "true RNDIS" default mode.
720  */
721
722
723 /* Heuristic, intended to kick in for ethernet/rndis peripheral ONLY
724  *
725  * IFF
726  *  (a) peripheral mode ... since rndis peripherals could pad their
727  *      writes to hosts, causing i/o failure; or we'd have to cope with
728  *      a largely unknowable variety of host side protocol variants
729  *  (b) and short reads are NOT errors ... since full reads would
730  *      cause those same i/o failures
731  *  (c) and read length is
732  *      - less than 64KB (max per cppi descriptor)
733  *      - not a multiple of 4096 (g_zero default, full reads typical)
734  *      - N (>1) packets long, ditto (full reads not EXPECTED)
735  * THEN
736  *   try rx rndis mode
737  *
738  * Cost of heuristic failing:  RXDMA wedges at the end of transfers that
739  * fill out the whole buffer.  Buggy host side usb network drivers could
740  * trigger that, but "in the field" such bugs seem to be all but unknown.
741  *
742  * So this module parameter lets the heuristic be disabled.  When using
743  * gadgetfs, the heuristic will probably need to be disabled.
744  */
745 static int cppi_rx_rndis = 1;
746
747 module_param(cppi_rx_rndis, bool, 0);
748 MODULE_PARM_DESC(cppi_rx_rndis, "enable/disable RX RNDIS heuristic");
749
750
751 /**
752  * cppi_next_rx_segment - dma read for the next chunk of a buffer
753  * @musb: the controller
754  * @rx: dma channel
755  * @onepacket: true unless caller treats short reads as errors, and
756  *      performs fault recovery above usbcore.
757  * Context: controller irqlocked
758  *
759  * See above notes about why we can't use multi-BD RX queues except in
760  * rare cases (mass storage class), and can never use the hardware "rndis"
761  * mode (since it's not a "true" RNDIS mode) with complete safety..
762  *
763  * It's ESSENTIAL that callers specify "onepacket" mode unless they kick in
764  * code to recover from corrupted datastreams after each short transfer.
765  */
766 static void
767 cppi_next_rx_segment(struct musb *musb, struct cppi_channel *rx, int onepacket)
768 {
769         unsigned                maxpacket = rx->maxpacket;
770         dma_addr_t              addr = rx->buf_dma + rx->offset;
771         size_t                  length = rx->buf_len - rx->offset;
772         struct cppi_descriptor  *bd, *tail;
773         unsigned                n_bds;
774         unsigned                i;
775         void __iomem            *tibase = musb->ctrl_base;
776         int                     is_rndis = 0;
777         struct cppi_rx_stateram __iomem *rx_ram = rx->state_ram;
778
779         if (onepacket) {
780                 /* almost every USB driver, host or peripheral side */
781                 n_bds = 1;
782
783                 /* maybe apply the heuristic above */
784                 if (cppi_rx_rndis
785                                 && is_peripheral_active(musb)
786                                 && length > maxpacket
787                                 && (length & ~0xffff) == 0
788                                 && (length & 0x0fff) != 0
789                                 && (length & (maxpacket - 1)) == 0) {
790                         maxpacket = length;
791                         is_rndis = 1;
792                 }
793         } else {
794                 /* virtually nothing except mass storage class */
795                 if (length > 0xffff) {
796                         n_bds = 0xffff / maxpacket;
797                         length = n_bds * maxpacket;
798                 } else {
799                         n_bds = length / maxpacket;
800                         if (length % maxpacket)
801                                 n_bds++;
802                 }
803                 if (n_bds == 1)
804                         onepacket = 1;
805                 else
806                         n_bds = min(n_bds, (unsigned) NUM_RXCHAN_BD);
807         }
808
809         /* In host mode, autorequest logic can generate some IN tokens; it's
810          * tricky since we can't leave REQPKT set in RXCSR after the transfer
811          * finishes. So:  multipacket transfers involve two or more segments.
812          * And always at least two IRQs ... RNDIS mode is not an option.
813          */
814         if (is_host_active(musb))
815                 n_bds = cppi_autoreq_update(rx, tibase, onepacket, n_bds);
816
817         cppi_rndis_update(rx, 1, musb->ctrl_base, is_rndis);
818
819         length = min(n_bds * maxpacket, length);
820
821         DBG(4, "RX DMA%d seg, maxp %d %s bds %d (cnt %d) "
822                         "dma 0x%x len %u %u/%u\n",
823                         rx->index, maxpacket,
824                         onepacket
825                                 ? (is_rndis ? "rndis" : "onepacket")
826                                 : "multipacket",
827                         n_bds,
828                         musb_readl(tibase,
829                                 DAVINCI_RXCPPI_BUFCNT0_REG + (rx->index * 4))
830                                         & 0xffff,
831                         addr, length, rx->channel.actual_len, rx->buf_len);
832
833         /* only queue one segment at a time, since the hardware prevents
834          * correct queue shutdown after unexpected short packets
835          */
836         bd = cppi_bd_alloc(rx);
837         rx->head = bd;
838
839         /* Build BDs for all packets in this segment */
840         for (i = 0, tail = NULL; bd && i < n_bds; i++, tail = bd) {
841                 u32     bd_len;
842
843                 if (i) {
844                         bd = cppi_bd_alloc(rx);
845                         if (!bd)
846                                 break;
847                         tail->next = bd;
848                         tail->hw_next = bd->dma;
849                 }
850                 bd->hw_next = 0;
851
852                 /* all but the last packet will be maxpacket size */
853                 if (maxpacket < length)
854                         bd_len = maxpacket;
855                 else
856                         bd_len = length;
857
858                 bd->hw_bufp = addr;
859                 addr += bd_len;
860                 rx->offset += bd_len;
861
862                 bd->hw_off_len = (0 /*offset*/ << 16) + bd_len;
863                 bd->buflen = bd_len;
864
865                 bd->hw_options = CPPI_OWN_SET | (i == 0 ? length : 0);
866                 length -= bd_len;
867         }
868
869         /* we always expect at least one reusable BD! */
870         if (!tail) {
871                 WARNING("rx dma%d -- no BDs? need %d\n", rx->index, n_bds);
872                 return;
873         } else if (i < n_bds)
874                 WARNING("rx dma%d -- only %d of %d BDs\n", rx->index, i, n_bds);
875
876         tail->next = NULL;
877         tail->hw_next = 0;
878
879         bd = rx->head;
880         rx->tail = tail;
881
882         /* short reads and other faults should terminate this entire
883          * dma segment.  we want one "dma packet" per dma segment, not
884          * one per USB packet, terminating the whole queue at once...
885          * NOTE that current hardware seems to ignore SOP and EOP.
886          */
887         bd->hw_options |= CPPI_SOP_SET;
888         tail->hw_options |= CPPI_EOP_SET;
889
890 #ifdef CONFIG_USB_MUSB_DEBUG
891         if (_dbg_level(5)) {
892                 struct cppi_descriptor  *d;
893
894                 for (d = rx->head; d; d = d->next)
895                         cppi_dump_rxbd("S", d);
896         }
897 #endif
898
899         /* in case the preceding transfer left some state... */
900         tail = rx->last_processed;
901         if (tail) {
902                 tail->next = bd;
903                 tail->hw_next = bd->dma;
904         }
905
906         core_rxirq_enable(tibase, rx->index + 1);
907
908         /* BDs live in DMA-coherent memory, but writes might be pending */
909         cpu_drain_writebuffer();
910
911         /* REVISIT specs say to write this AFTER the BUFCNT register
912          * below ... but that loses badly.
913          */
914         musb_writel(&rx_ram->rx_head, 0, bd->dma);
915
916         /* bufferCount must be at least 3, and zeroes on completion
917          * unless it underflows below zero, or stops at two, or keeps
918          * growing ... grr.
919          */
920         i = musb_readl(tibase,
921                         DAVINCI_RXCPPI_BUFCNT0_REG + (rx->index * 4))
922                         & 0xffff;
923
924         if (!i)
925                 musb_writel(tibase,
926                         DAVINCI_RXCPPI_BUFCNT0_REG + (rx->index * 4),
927                         n_bds + 2);
928         else if (n_bds > (i - 3))
929                 musb_writel(tibase,
930                         DAVINCI_RXCPPI_BUFCNT0_REG + (rx->index * 4),
931                         n_bds - (i - 3));
932
933         i = musb_readl(tibase,
934                         DAVINCI_RXCPPI_BUFCNT0_REG + (rx->index * 4))
935                         & 0xffff;
936         if (i < (2 + n_bds)) {
937                 DBG(2, "bufcnt%d underrun - %d (for %d)\n",
938                                         rx->index, i, n_bds);
939                 musb_writel(tibase,
940                         DAVINCI_RXCPPI_BUFCNT0_REG + (rx->index * 4),
941                         n_bds + 2);
942         }
943
944         cppi_dump_rx(4, rx, "/S");
945 }
946
947 /**
948  * cppi_channel_program - program channel for data transfer
949  * @ch: the channel
950  * @maxpacket: max packet size
951  * @mode: For RX, 1 unless the usb protocol driver promised to treat
952  *      all short reads as errors and kick in high level fault recovery.
953  *      For TX, ignored because of RNDIS mode races/glitches.
954  * @dma_addr: dma address of buffer
955  * @len: length of buffer
956  * Context: controller irqlocked
957  */
958 static int cppi_channel_program(struct dma_channel *ch,
959                 u16 maxpacket, u8 mode,
960                 dma_addr_t dma_addr, u32 len)
961 {
962         struct cppi_channel     *cppi_ch;
963         struct cppi             *controller;
964         struct musb             *musb;
965
966         cppi_ch = container_of(ch, struct cppi_channel, channel);
967         controller = cppi_ch->controller;
968         musb = controller->musb;
969
970         switch (ch->status) {
971         case MUSB_DMA_STATUS_BUS_ABORT:
972         case MUSB_DMA_STATUS_CORE_ABORT:
973                 /* fault irq handler should have handled cleanup */
974                 WARNING("%cX DMA%d not cleaned up after abort!\n",
975                                 cppi_ch->transmit ? 'T' : 'R',
976                                 cppi_ch->index);
977                 /* WARN_ON(1); */
978                 break;
979         case MUSB_DMA_STATUS_BUSY:
980                 WARNING("program active channel?  %cX DMA%d\n",
981                                 cppi_ch->transmit ? 'T' : 'R',
982                                 cppi_ch->index);
983                 /* WARN_ON(1); */
984                 break;
985         case MUSB_DMA_STATUS_UNKNOWN:
986                 DBG(1, "%cX DMA%d not allocated!\n",
987                                 cppi_ch->transmit ? 'T' : 'R',
988                                 cppi_ch->index);
989                 /* FALLTHROUGH */
990         case MUSB_DMA_STATUS_FREE:
991                 break;
992         }
993
994         ch->status = MUSB_DMA_STATUS_BUSY;
995
996         /* set transfer parameters, then queue up its first segment */
997         cppi_ch->buf_dma = dma_addr;
998         cppi_ch->offset = 0;
999         cppi_ch->maxpacket = maxpacket;
1000         cppi_ch->buf_len = len;
1001         cppi_ch->channel.actual_len = 0;
1002
1003         /* TX channel? or RX? */
1004         if (cppi_ch->transmit)
1005                 cppi_next_tx_segment(musb, cppi_ch);
1006         else
1007                 cppi_next_rx_segment(musb, cppi_ch, mode);
1008
1009         return true;
1010 }
1011
1012 static bool cppi_rx_scan(struct cppi *cppi, unsigned ch)
1013 {
1014         struct cppi_channel             *rx = &cppi->rx[ch];
1015         struct cppi_rx_stateram __iomem *state = rx->state_ram;
1016         struct cppi_descriptor          *bd;
1017         struct cppi_descriptor          *last = rx->last_processed;
1018         bool                            completed = false;
1019         bool                            acked = false;
1020         int                             i;
1021         dma_addr_t                      safe2ack;
1022         void __iomem                    *regs = rx->hw_ep->regs;
1023
1024         cppi_dump_rx(6, rx, "/K");
1025
1026         bd = last ? last->next : rx->head;
1027         if (!bd)
1028                 return false;
1029
1030         /* run through all completed BDs */
1031         for (i = 0, safe2ack = musb_readl(&state->rx_complete, 0);
1032                         (safe2ack || completed) && bd && i < NUM_RXCHAN_BD;
1033                         i++, bd = bd->next) {
1034                 u16     len;
1035
1036                 /* catch latest BD writes from CPPI */
1037                 rmb();
1038                 if (!completed && (bd->hw_options & CPPI_OWN_SET))
1039                         break;
1040
1041                 DBG(5, "C/RXBD %08x: nxt %08x buf %08x "
1042                         "off.len %08x opt.len %08x (%d)\n",
1043                         bd->dma, bd->hw_next, bd->hw_bufp,
1044                         bd->hw_off_len, bd->hw_options,
1045                         rx->channel.actual_len);
1046
1047                 /* actual packet received length */
1048                 if ((bd->hw_options & CPPI_SOP_SET) && !completed)
1049                         len = bd->hw_off_len & CPPI_RECV_PKTLEN_MASK;
1050                 else
1051                         len = 0;
1052
1053                 if (bd->hw_options & CPPI_EOQ_MASK)
1054                         completed = true;
1055
1056                 if (!completed && len < bd->buflen) {
1057                         /* NOTE:  when we get a short packet, RXCSR_H_REQPKT
1058                          * must have been cleared, and no more DMA packets may
1059                          * active be in the queue... TI docs didn't say, but
1060                          * CPPI ignores those BDs even though OWN is still set.
1061                          */
1062                         completed = true;
1063                         DBG(3, "rx short %d/%d (%d)\n",
1064                                         len, bd->buflen,
1065                                         rx->channel.actual_len);
1066                 }
1067
1068                 /* If we got here, we expect to ack at least one BD; meanwhile
1069                  * CPPI may completing other BDs while we scan this list...
1070                  *
1071                  * RACE: we can notice OWN cleared before CPPI raises the
1072                  * matching irq by writing that BD as the completion pointer.
1073                  * In such cases, stop scanning and wait for the irq, avoiding
1074                  * lost acks and states where BD ownership is unclear.
1075                  */
1076                 if (bd->dma == safe2ack) {
1077                         musb_writel(&state->rx_complete, 0, safe2ack);
1078                         safe2ack = musb_readl(&state->rx_complete, 0);
1079                         acked = true;
1080                         if (bd->dma == safe2ack)
1081                                 safe2ack = 0;
1082                 }
1083
1084                 rx->channel.actual_len += len;
1085
1086                 cppi_bd_free(rx, last);
1087                 last = bd;
1088
1089                 /* stop scanning on end-of-segment */
1090                 if (bd->hw_next == 0)
1091                         completed = true;
1092         }
1093         rx->last_processed = last;
1094
1095         /* dma abort, lost ack, or ... */
1096         if (!acked && last) {
1097                 int     csr;
1098
1099                 if (safe2ack == 0 || safe2ack == rx->last_processed->dma)
1100                         musb_writel(&state->rx_complete, 0, safe2ack);
1101                 if (safe2ack == 0) {
1102                         cppi_bd_free(rx, last);
1103                         rx->last_processed = NULL;
1104
1105                         /* if we land here on the host side, H_REQPKT will
1106                          * be clear and we need to restart the queue...
1107                          */
1108                         WARN_ON(rx->head);
1109                 }
1110                 musb_ep_select(cppi->mregs, rx->index + 1);
1111                 csr = musb_readw(regs, MUSB_RXCSR);
1112                 if (csr & MUSB_RXCSR_DMAENAB) {
1113                         DBG(4, "list%d %p/%p, last %08x%s, csr %04x\n",
1114                                 rx->index,
1115                                 rx->head, rx->tail,
1116                                 rx->last_processed
1117                                         ? rx->last_processed->dma
1118                                         : 0,
1119                                 completed ? ", completed" : "",
1120                                 csr);
1121                         cppi_dump_rxq(4, "/what?", rx);
1122                 }
1123         }
1124         if (!completed) {
1125                 int     csr;
1126
1127                 rx->head = bd;
1128
1129                 /* REVISIT seems like "autoreq all but EOP" doesn't...
1130                  * setting it here "should" be racey, but seems to work
1131                  */
1132                 csr = musb_readw(rx->hw_ep->regs, MUSB_RXCSR);
1133                 if (is_host_active(cppi->musb)
1134                                 && bd
1135                                 && !(csr & MUSB_RXCSR_H_REQPKT)) {
1136                         csr |= MUSB_RXCSR_H_REQPKT;
1137                         musb_writew(regs, MUSB_RXCSR,
1138                                         MUSB_RXCSR_H_WZC_BITS | csr);
1139                         csr = musb_readw(rx->hw_ep->regs, MUSB_RXCSR);
1140                 }
1141         } else {
1142                 rx->head = NULL;
1143                 rx->tail = NULL;
1144         }
1145
1146         cppi_dump_rx(6, rx, completed ? "/completed" : "/cleaned");
1147         return completed;
1148 }
1149
1150 irqreturn_t cppi_interrupt(int irq, void *dev_id)
1151 {
1152         struct musb             *musb = dev_id;
1153         struct cppi             *cppi;
1154         void __iomem            *tibase;
1155         struct musb_hw_ep       *hw_ep = NULL;
1156         u32                     rx, tx;
1157         int                     i, index;
1158         unsigned long           flags;
1159
1160         cppi = container_of(musb->dma_controller, struct cppi, controller);
1161         if (cppi->irq)
1162                 spin_lock_irqsave(&musb->lock, flags);
1163
1164         tibase = musb->ctrl_base;
1165
1166         tx = musb_readl(tibase, DAVINCI_TXCPPI_MASKED_REG);
1167         rx = musb_readl(tibase, DAVINCI_RXCPPI_MASKED_REG);
1168
1169         if (!tx && !rx)
1170                 return IRQ_NONE;
1171
1172         DBG(4, "CPPI IRQ Tx%x Rx%x\n", tx, rx);
1173
1174         /* process TX channels */
1175         for (index = 0; tx; tx = tx >> 1, index++) {
1176                 struct cppi_channel             *tx_ch;
1177                 struct cppi_tx_stateram __iomem *tx_ram;
1178                 bool                            completed = false;
1179                 struct cppi_descriptor          *bd;
1180
1181                 if (!(tx & 1))
1182                         continue;
1183
1184                 tx_ch = cppi->tx + index;
1185                 tx_ram = tx_ch->state_ram;
1186
1187                 /* FIXME  need a cppi_tx_scan() routine, which
1188                  * can also be called from abort code
1189                  */
1190
1191                 cppi_dump_tx(5, tx_ch, "/E");
1192
1193                 bd = tx_ch->head;
1194
1195                 /*
1196                  * If Head is null then this could mean that a abort interrupt
1197                  * that needs to be acknowledged.
1198                  */
1199                 if (NULL == bd) {
1200                         DBG(1, "null BD\n");
1201                         tx_ram->tx_complete = 0;
1202                         continue;
1203                 }
1204
1205                 /* run through all completed BDs */
1206                 for (i = 0; !completed && bd && i < NUM_TXCHAN_BD;
1207                                 i++, bd = bd->next) {
1208                         u16     len;
1209
1210                         /* catch latest BD writes from CPPI */
1211                         rmb();
1212                         if (bd->hw_options & CPPI_OWN_SET)
1213                                 break;
1214
1215                         DBG(5, "C/TXBD %p n %x b %x off %x opt %x\n",
1216                                         bd, bd->hw_next, bd->hw_bufp,
1217                                         bd->hw_off_len, bd->hw_options);
1218
1219                         len = bd->hw_off_len & CPPI_BUFFER_LEN_MASK;
1220                         tx_ch->channel.actual_len += len;
1221
1222                         tx_ch->last_processed = bd;
1223
1224                         /* write completion register to acknowledge
1225                          * processing of completed BDs, and possibly
1226                          * release the IRQ; EOQ might not be set ...
1227                          *
1228                          * REVISIT use the same ack strategy as rx
1229                          *
1230                          * REVISIT have observed bit 18 set; huh??
1231                          */
1232                         /* if ((bd->hw_options & CPPI_EOQ_MASK)) */
1233                                 musb_writel(&tx_ram->tx_complete, 0, bd->dma);
1234
1235                         /* stop scanning on end-of-segment */
1236                         if (bd->hw_next == 0)
1237                                 completed = true;
1238                 }
1239
1240                 /* on end of segment, maybe go to next one */
1241                 if (completed) {
1242                         /* cppi_dump_tx(4, tx_ch, "/complete"); */
1243
1244                         /* transfer more, or report completion */
1245                         if (tx_ch->offset >= tx_ch->buf_len) {
1246                                 tx_ch->head = NULL;
1247                                 tx_ch->tail = NULL;
1248                                 tx_ch->channel.status = MUSB_DMA_STATUS_FREE;
1249
1250                                 hw_ep = tx_ch->hw_ep;
1251
1252                                 musb_dma_completion(musb, index + 1, 1);
1253
1254                         } else {
1255                                 /* Bigger transfer than we could fit in
1256                                  * that first batch of descriptors...
1257                                  */
1258                                 cppi_next_tx_segment(musb, tx_ch);
1259                         }
1260                 } else
1261                         tx_ch->head = bd;
1262         }
1263
1264         /* Start processing the RX block */
1265         for (index = 0; rx; rx = rx >> 1, index++) {
1266
1267                 if (rx & 1) {
1268                         struct cppi_channel             *rx_ch;
1269
1270                         rx_ch = cppi->rx + index;
1271
1272                         /* let incomplete dma segments finish */
1273                         if (!cppi_rx_scan(cppi, index))
1274                                 continue;
1275
1276                         /* start another dma segment if needed */
1277                         if (rx_ch->channel.actual_len != rx_ch->buf_len
1278                                         && rx_ch->channel.actual_len
1279                                                 == rx_ch->offset) {
1280                                 cppi_next_rx_segment(musb, rx_ch, 1);
1281                                 continue;
1282                         }
1283
1284                         /* all segments completed! */
1285                         rx_ch->channel.status = MUSB_DMA_STATUS_FREE;
1286
1287                         hw_ep = rx_ch->hw_ep;
1288
1289                         core_rxirq_disable(tibase, index + 1);
1290                         musb_dma_completion(musb, index + 1, 0);
1291                 }
1292         }
1293
1294         /* write to CPPI EOI register to re-enable interrupts */
1295         musb_writel(tibase, DAVINCI_CPPI_EOI_REG, 0);
1296
1297         if (cppi->irq)
1298                 spin_unlock_irqrestore(&musb->lock, flags);
1299
1300         return IRQ_HANDLED;
1301 }
1302
1303 /* Instantiate a software object representing a DMA controller. */
1304 struct dma_controller *__init
1305 dma_controller_create(struct musb *musb, void __iomem *mregs)
1306 {
1307         struct cppi             *controller;
1308         struct device           *dev = musb->controller;
1309         struct platform_device  *pdev = to_platform_device(dev);
1310         int                     irq = platform_get_irq(pdev, 1);
1311
1312         controller = kzalloc(sizeof *controller, GFP_KERNEL);
1313         if (!controller)
1314                 return NULL;
1315
1316         controller->mregs = mregs;
1317         controller->tibase = mregs - DAVINCI_BASE_OFFSET;
1318
1319         controller->musb = musb;
1320         controller->controller.start = cppi_controller_start;
1321         controller->controller.stop = cppi_controller_stop;
1322         controller->controller.channel_alloc = cppi_channel_allocate;
1323         controller->controller.channel_release = cppi_channel_release;
1324         controller->controller.channel_program = cppi_channel_program;
1325         controller->controller.channel_abort = cppi_channel_abort;
1326
1327         /* NOTE: allocating from on-chip SRAM would give the least
1328          * contention for memory access, if that ever matters here.
1329          */
1330
1331         /* setup BufferPool */
1332         controller->pool = dma_pool_create("cppi",
1333                         controller->musb->controller,
1334                         sizeof(struct cppi_descriptor),
1335                         CPPI_DESCRIPTOR_ALIGN, 0);
1336         if (!controller->pool) {
1337                 kfree(controller);
1338                 return NULL;
1339         }
1340
1341         if (irq > 0) {
1342                 if (request_irq(irq, cppi_interrupt, 0, "cppi-dma", musb)) {
1343                         dev_err(dev, "request_irq %d failed!\n", irq);
1344                         dma_controller_destroy(&controller->controller);
1345                         return NULL;
1346                 }
1347                 controller->irq = irq;
1348         }
1349
1350         return &controller->controller;
1351 }
1352
1353 /*
1354  *  Destroy a previously-instantiated DMA controller.
1355  */
1356 void dma_controller_destroy(struct dma_controller *c)
1357 {
1358         struct cppi     *cppi;
1359
1360         cppi = container_of(c, struct cppi, controller);
1361
1362         if (cppi->irq)
1363                 free_irq(cppi->irq, cppi->musb);
1364
1365         /* assert:  caller stopped the controller first */
1366         dma_pool_destroy(cppi->pool);
1367
1368         kfree(cppi);
1369 }
1370
1371 /*
1372  * Context: controller irqlocked, endpoint selected
1373  */
1374 static int cppi_channel_abort(struct dma_channel *channel)
1375 {
1376         struct cppi_channel     *cppi_ch;
1377         struct cppi             *controller;
1378         void __iomem            *mbase;
1379         void __iomem            *tibase;
1380         void __iomem            *regs;
1381         u32                     value;
1382         struct cppi_descriptor  *queue;
1383
1384         cppi_ch = container_of(channel, struct cppi_channel, channel);
1385
1386         controller = cppi_ch->controller;
1387
1388         switch (channel->status) {
1389         case MUSB_DMA_STATUS_BUS_ABORT:
1390         case MUSB_DMA_STATUS_CORE_ABORT:
1391                 /* from RX or TX fault irq handler */
1392         case MUSB_DMA_STATUS_BUSY:
1393                 /* the hardware needs shutting down */
1394                 regs = cppi_ch->hw_ep->regs;
1395                 break;
1396         case MUSB_DMA_STATUS_UNKNOWN:
1397         case MUSB_DMA_STATUS_FREE:
1398                 return 0;
1399         default:
1400                 return -EINVAL;
1401         }
1402
1403         if (!cppi_ch->transmit && cppi_ch->head)
1404                 cppi_dump_rxq(3, "/abort", cppi_ch);
1405
1406         mbase = controller->mregs;
1407         tibase = controller->tibase;
1408
1409         queue = cppi_ch->head;
1410         cppi_ch->head = NULL;
1411         cppi_ch->tail = NULL;
1412
1413         /* REVISIT should rely on caller having done this,
1414          * and caller should rely on us not changing it.
1415          * peripheral code is safe ... check host too.
1416          */
1417         musb_ep_select(mbase, cppi_ch->index + 1);
1418
1419         if (cppi_ch->transmit) {
1420                 struct cppi_tx_stateram __iomem *tx_ram;
1421                 /* REVISIT put timeouts on these controller handshakes */
1422
1423                 cppi_dump_tx(6, cppi_ch, " (teardown)");
1424
1425                 /* teardown DMA engine then usb core */
1426                 do {
1427                         value = musb_readl(tibase, DAVINCI_TXCPPI_TEAR_REG);
1428                 } while (!(value & CPPI_TEAR_READY));
1429                 musb_writel(tibase, DAVINCI_TXCPPI_TEAR_REG, cppi_ch->index);
1430
1431                 tx_ram = cppi_ch->state_ram;
1432                 do {
1433                         value = musb_readl(&tx_ram->tx_complete, 0);
1434                 } while (0xFFFFFFFC != value);
1435
1436                 /* FIXME clean up the transfer state ... here?
1437                  * the completion routine should get called with
1438                  * an appropriate status code.
1439                  */
1440
1441                 value = musb_readw(regs, MUSB_TXCSR);
1442                 value &= ~MUSB_TXCSR_DMAENAB;
1443                 value |= MUSB_TXCSR_FLUSHFIFO;
1444                 musb_writew(regs, MUSB_TXCSR, value);
1445                 musb_writew(regs, MUSB_TXCSR, value);
1446
1447                 /*
1448                  * 1. Write to completion Ptr value 0x1(bit 0 set)
1449                  *    (write back mode)
1450                  * 2. Wait for abort interrupt and then put the channel in
1451                  *    compare mode by writing 1 to the tx_complete register.
1452                  */
1453                 cppi_reset_tx(tx_ram, 1);
1454                 cppi_ch->head = 0;
1455                 musb_writel(&tx_ram->tx_complete, 0, 1);
1456                 cppi_dump_tx(5, cppi_ch, " (done teardown)");
1457
1458                 /* REVISIT tx side _should_ clean up the same way
1459                  * as the RX side ... this does no cleanup at all!
1460                  */
1461
1462         } else /* RX */ {
1463                 u16                     csr;
1464
1465                 /* NOTE: docs don't guarantee any of this works ...  we
1466                  * expect that if the usb core stops telling the cppi core
1467                  * to pull more data from it, then it'll be safe to flush
1468                  * current RX DMA state iff any pending fifo transfer is done.
1469                  */
1470
1471                 core_rxirq_disable(tibase, cppi_ch->index + 1);
1472
1473                 /* for host, ensure ReqPkt is never set again */
1474                 if (is_host_active(cppi_ch->controller->musb)) {
1475                         value = musb_readl(tibase, DAVINCI_AUTOREQ_REG);
1476                         value &= ~((0x3) << (cppi_ch->index * 2));
1477                         musb_writel(tibase, DAVINCI_AUTOREQ_REG, value);
1478                 }
1479
1480                 csr = musb_readw(regs, MUSB_RXCSR);
1481
1482                 /* for host, clear (just) ReqPkt at end of current packet(s) */
1483                 if (is_host_active(cppi_ch->controller->musb)) {
1484                         csr |= MUSB_RXCSR_H_WZC_BITS;
1485                         csr &= ~MUSB_RXCSR_H_REQPKT;
1486                 } else
1487                         csr |= MUSB_RXCSR_P_WZC_BITS;
1488
1489                 /* clear dma enable */
1490                 csr &= ~(MUSB_RXCSR_DMAENAB);
1491                 musb_writew(regs, MUSB_RXCSR, csr);
1492                 csr = musb_readw(regs, MUSB_RXCSR);
1493
1494                 /* Quiesce: wait for current dma to finish (if not cleanup).
1495                  * We can't use bit zero of stateram->rx_sop, since that
1496                  * refers to an entire "DMA packet" not just emptying the
1497                  * current fifo.  Most segments need multiple usb packets.
1498                  */
1499                 if (channel->status == MUSB_DMA_STATUS_BUSY)
1500                         udelay(50);
1501
1502                 /* scan the current list, reporting any data that was
1503                  * transferred and acking any IRQ
1504                  */
1505                 cppi_rx_scan(controller, cppi_ch->index);
1506
1507                 /* clobber the existing state once it's idle
1508                  *
1509                  * NOTE:  arguably, we should also wait for all the other
1510                  * RX channels to quiesce (how??) and then temporarily
1511                  * disable RXCPPI_CTRL_REG ... but it seems that we can
1512                  * rely on the controller restarting from state ram, with
1513                  * only RXCPPI_BUFCNT state being bogus.  BUFCNT will
1514                  * correct itself after the next DMA transfer though.
1515                  *
1516                  * REVISIT does using rndis mode change that?
1517                  */
1518                 cppi_reset_rx(cppi_ch->state_ram);
1519
1520                 /* next DMA request _should_ load cppi head ptr */
1521
1522                 /* ... we don't "free" that list, only mutate it in place.  */
1523                 cppi_dump_rx(5, cppi_ch, " (done abort)");
1524
1525                 /* clean up previously pending bds */
1526                 cppi_bd_free(cppi_ch, cppi_ch->last_processed);
1527                 cppi_ch->last_processed = NULL;
1528
1529                 while (queue) {
1530                         struct cppi_descriptor  *tmp = queue->next;
1531
1532                         cppi_bd_free(cppi_ch, queue);
1533                         queue = tmp;
1534                 }
1535         }
1536
1537         channel->status = MUSB_DMA_STATUS_FREE;
1538         cppi_ch->buf_dma = 0;
1539         cppi_ch->offset = 0;
1540         cppi_ch->buf_len = 0;
1541         cppi_ch->maxpacket = 0;
1542         return 0;
1543 }
1544
1545 /* TBD Queries:
1546  *
1547  * Power Management ... probably turn off cppi during suspend, restart;
1548  * check state ram?  Clocking is presumably shared with usb core.
1549  */