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