usb: musb: pass platform_ops via platform_data
[linux/fpc-iii.git] / drivers / usb / musb / cppi_dma.c
blobde55a3c3259ac1a403e3c4d730781eeb63fda77c
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/platform_device.h>
10 #include <linux/slab.h>
11 #include <linux/usb.h>
13 #include "musb_core.h"
14 #include "musb_debug.h"
15 #include "cppi_dma.h"
18 /* CPPI DMA status 7-mar-2006:
20 * - See musb_{host,gadget}.c for more info
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.
25 * NOTES 24-aug-2006 (2.6.18-rc4):
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?
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.
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.
44 #define NUM_TXCHAN_BD 64
45 #define NUM_RXCHAN_BD 64
47 static inline void cpu_drain_writebuffer(void)
49 wmb();
50 #ifdef CONFIG_CPU_ARM926T
51 /* REVISIT this "should not be needed",
52 * but lack of it sure seemed to hurt ...
54 asm("mcr p15, 0, r0, c7, c10, 4 @ drain write buffer\n");
55 #endif
58 static inline struct cppi_descriptor *cppi_bd_alloc(struct cppi_channel *c)
60 struct cppi_descriptor *bd = c->freelist;
62 if (bd)
63 c->freelist = bd->next;
64 return bd;
67 static inline void
68 cppi_bd_free(struct cppi_channel *c, struct cppi_descriptor *bd)
70 if (!bd)
71 return;
72 bd->next = c->freelist;
73 c->freelist = bd;
77 * Start DMA controller
79 * Initialize the DMA controller as necessary.
82 /* zero out entire rx state RAM entry for the channel */
83 static void cppi_reset_rx(struct cppi_rx_stateram __iomem *rx)
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);
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)
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);
107 static void __init cppi_pool_init(struct cppi *cppi, struct cppi_channel *c)
109 int j;
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;
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;
125 bd = dma_pool_alloc(cppi->pool, GFP_KERNEL, &dma);
126 bd->dma = dma;
127 cppi_bd_free(c, bd);
131 static int cppi_channel_abort(struct dma_channel *);
133 static void cppi_pool_free(struct cppi_channel *c)
135 struct cppi *cppi = c->controller;
136 struct cppi_descriptor *bd;
138 (void) cppi_channel_abort(&c->channel);
139 c->channel.status = MUSB_DMA_STATUS_UNKNOWN;
140 c->controller = NULL;
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;
152 static int __init cppi_controller_start(struct dma_controller *c)
154 struct cppi *controller;
155 void __iomem *tibase;
156 int i;
158 controller = container_of(c, struct cppi, controller);
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;
165 for (i = 0; i < ARRAY_SIZE(controller->rx); i++) {
166 controller->rx[i].transmit = false;
167 controller->rx[i].index = i;
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);
176 tibase = controller->tibase;
177 INIT_LIST_HEAD(&controller->tx_complete);
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;
184 INIT_LIST_HEAD(&tx_ch->tx_complete);
186 tx = tibase + DAVINCI_TXCPPI_STATERAM_OFFSET(i);
187 tx_ch->state_ram = tx;
188 cppi_reset_tx(tx, 0);
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;
194 INIT_LIST_HEAD(&rx_ch->tx_complete);
196 rx = tibase + DAVINCI_RXCPPI_STATERAM_OFFSET(i);
197 rx_ch->state_ram = rx;
198 cppi_reset_rx(rx);
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);
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);
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);
215 return 0;
219 * Stop DMA controller
221 * De-Init the DMA controller as necessary.
224 static int cppi_controller_stop(struct dma_controller *c)
226 struct cppi *controller;
227 void __iomem *tibase;
228 int i;
230 controller = container_of(c, struct cppi, controller);
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);
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);
245 for (i = 0; i < ARRAY_SIZE(controller->rx); i++)
246 cppi_pool_free(controller->rx + i);
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.
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);
256 return 0;
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 ...
263 * NOTE: docs don't say either way, but irq masking **enables** irqs.
265 * REVISIT same issue applies to pure PIO usage too, and non-cppi dma...
267 static inline void core_rxirq_disable(void __iomem *tibase, unsigned epnum)
269 musb_writel(tibase, DAVINCI_USB_INT_MASK_CLR_REG, 1 << (epnum + 8));
272 static inline void core_rxirq_enable(void __iomem *tibase, unsigned epnum)
274 musb_writel(tibase, DAVINCI_USB_INT_MASK_SET_REG, 1 << (epnum + 8));
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.
284 static struct dma_channel *
285 cppi_channel_allocate(struct dma_controller *c,
286 struct musb_hw_ep *ep, u8 transmit)
288 struct cppi *controller;
289 u8 index;
290 struct cppi_channel *cppi_ch;
291 void __iomem *tibase;
293 controller = container_of(c, struct cppi, controller);
294 tibase = controller->tibase;
296 /* ep0 doesn't use DMA; remember cppi indices are 0..N-1 */
297 index = ep->epnum - 1;
299 /* return the corresponding CPPI Channel Handle, and
300 * probably disable the non-CPPI irq until we need it.
302 if (transmit) {
303 if (index >= ARRAY_SIZE(controller->tx)) {
304 DBG(1, "no %cX%d CPPI channel\n", 'T', index);
305 return NULL;
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;
313 cppi_ch = controller->rx + index;
314 core_rxirq_disable(tibase, ep->epnum);
317 /* REVISIT make this an error later once the same driver code works
318 * with the other DMA engine too
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 cppi_ch->channel.max_len = 0x7fffffff;
327 DBG(4, "Allocate CPPI%d %cX\n", index, transmit ? 'T' : 'R');
328 return &cppi_ch->channel;
331 /* Release a CPPI Channel. */
332 static void cppi_channel_release(struct dma_channel *channel)
334 struct cppi_channel *c;
335 void __iomem *tibase;
337 /* REVISIT: for paranoia, check state and abort if needed... */
339 c = container_of(channel, struct cppi_channel, channel);
340 tibase = c->controller->tibase;
341 if (!c->hw_ep)
342 DBG(1, "releasing idle DMA channel %p\n", c);
343 else if (!c->transmit)
344 core_rxirq_enable(tibase, c->index + 1);
346 /* for now, leave its cppi IRQ enabled (we won't trigger it) */
347 c->hw_ep = NULL;
348 channel->status = MUSB_DMA_STATUS_UNKNOWN;
351 /* Context: controller irqlocked */
352 static void
353 cppi_dump_rx(int level, struct cppi_channel *c, const char *tag)
355 void __iomem *base = c->controller->mregs;
356 struct cppi_rx_stateram __iomem *rx = c->state_ram;
358 musb_ep_select(base, c->index + 1);
360 DBG(level, "RX DMA%d%s: %d left, csr %04x, "
361 "%08x H%08x S%08x C%08x, "
362 "B%08x L%08x %08x .. %08x"
363 "\n",
364 c->index, tag,
365 musb_readl(c->controller->tibase,
366 DAVINCI_RXCPPI_BUFCNT0_REG + 4 * c->index),
367 musb_readw(c->hw_ep->regs, MUSB_RXCSR),
369 musb_readl(&rx->rx_skipbytes, 0),
370 musb_readl(&rx->rx_head, 0),
371 musb_readl(&rx->rx_sop, 0),
372 musb_readl(&rx->rx_current, 0),
374 musb_readl(&rx->rx_buf_current, 0),
375 musb_readl(&rx->rx_len_len, 0),
376 musb_readl(&rx->rx_cnt_cnt, 0),
377 musb_readl(&rx->rx_complete, 0)
381 /* Context: controller irqlocked */
382 static void
383 cppi_dump_tx(int level, struct cppi_channel *c, const char *tag)
385 void __iomem *base = c->controller->mregs;
386 struct cppi_tx_stateram __iomem *tx = c->state_ram;
388 musb_ep_select(base, c->index + 1);
390 DBG(level, "TX DMA%d%s: csr %04x, "
391 "H%08x S%08x C%08x %08x, "
392 "F%08x L%08x .. %08x"
393 "\n",
394 c->index, tag,
395 musb_readw(c->hw_ep->regs, MUSB_TXCSR),
397 musb_readl(&tx->tx_head, 0),
398 musb_readl(&tx->tx_buf, 0),
399 musb_readl(&tx->tx_current, 0),
400 musb_readl(&tx->tx_buf_current, 0),
402 musb_readl(&tx->tx_info, 0),
403 musb_readl(&tx->tx_rem_len, 0),
404 /* dummy/unused word 6 */
405 musb_readl(&tx->tx_complete, 0)
409 /* Context: controller irqlocked */
410 static inline void
411 cppi_rndis_update(struct cppi_channel *c, int is_rx,
412 void __iomem *tibase, int is_rndis)
414 /* we may need to change the rndis flag for this cppi channel */
415 if (c->is_rndis != is_rndis) {
416 u32 value = musb_readl(tibase, DAVINCI_RNDIS_REG);
417 u32 temp = 1 << (c->index);
419 if (is_rx)
420 temp <<= 16;
421 if (is_rndis)
422 value |= temp;
423 else
424 value &= ~temp;
425 musb_writel(tibase, DAVINCI_RNDIS_REG, value);
426 c->is_rndis = is_rndis;
430 #ifdef CONFIG_USB_MUSB_DEBUG
431 static void cppi_dump_rxbd(const char *tag, struct cppi_descriptor *bd)
433 pr_debug("RXBD/%s %08x: "
434 "nxt %08x buf %08x off.blen %08x opt.plen %08x\n",
435 tag, bd->dma,
436 bd->hw_next, bd->hw_bufp, bd->hw_off_len,
437 bd->hw_options);
439 #endif
441 static void cppi_dump_rxq(int level, const char *tag, struct cppi_channel *rx)
443 #ifdef CONFIG_USB_MUSB_DEBUG
444 struct cppi_descriptor *bd;
446 if (!_dbg_level(level))
447 return;
448 cppi_dump_rx(level, rx, tag);
449 if (rx->last_processed)
450 cppi_dump_rxbd("last", rx->last_processed);
451 for (bd = rx->head; bd; bd = bd->next)
452 cppi_dump_rxbd("active", bd);
453 #endif
457 /* NOTE: DaVinci autoreq is ignored except for host side "RNDIS" mode RX;
458 * so we won't ever use it (see "CPPI RX Woes" below).
460 static inline int cppi_autoreq_update(struct cppi_channel *rx,
461 void __iomem *tibase, int onepacket, unsigned n_bds)
463 u32 val;
465 #ifdef RNDIS_RX_IS_USABLE
466 u32 tmp;
467 /* assert(is_host_active(musb)) */
469 /* start from "AutoReq never" */
470 tmp = musb_readl(tibase, DAVINCI_AUTOREQ_REG);
471 val = tmp & ~((0x3) << (rx->index * 2));
473 /* HCD arranged reqpkt for packet #1. we arrange int
474 * for all but the last one, maybe in two segments.
476 if (!onepacket) {
477 #if 0
478 /* use two segments, autoreq "all" then the last "never" */
479 val |= ((0x3) << (rx->index * 2));
480 n_bds--;
481 #else
482 /* one segment, autoreq "all-but-last" */
483 val |= ((0x1) << (rx->index * 2));
484 #endif
487 if (val != tmp) {
488 int n = 100;
490 /* make sure that autoreq is updated before continuing */
491 musb_writel(tibase, DAVINCI_AUTOREQ_REG, val);
492 do {
493 tmp = musb_readl(tibase, DAVINCI_AUTOREQ_REG);
494 if (tmp == val)
495 break;
496 cpu_relax();
497 } while (n-- > 0);
499 #endif
501 /* REQPKT is turned off after each segment */
502 if (n_bds && rx->channel.actual_len) {
503 void __iomem *regs = rx->hw_ep->regs;
505 val = musb_readw(regs, MUSB_RXCSR);
506 if (!(val & MUSB_RXCSR_H_REQPKT)) {
507 val |= MUSB_RXCSR_H_REQPKT | MUSB_RXCSR_H_WZC_BITS;
508 musb_writew(regs, MUSB_RXCSR, val);
509 /* flush writebufer */
510 val = musb_readw(regs, MUSB_RXCSR);
513 return n_bds;
517 /* Buffer enqueuing Logic:
519 * - RX builds new queues each time, to help handle routine "early
520 * termination" cases (faults, including errors and short reads)
521 * more correctly.
523 * - for now, TX reuses the same queue of BDs every time
525 * REVISIT long term, we want a normal dynamic model.
526 * ... the goal will be to append to the
527 * existing queue, processing completed "dma buffers" (segments) on the fly.
529 * Otherwise we force an IRQ latency between requests, which slows us a lot
530 * (especially in "transparent" dma). Unfortunately that model seems to be
531 * inherent in the DMA model from the Mentor code, except in the rare case
532 * of transfers big enough (~128+ KB) that we could append "middle" segments
533 * in the TX paths. (RX can't do this, see below.)
535 * That's true even in the CPPI- friendly iso case, where most urbs have
536 * several small segments provided in a group and where the "packet at a time"
537 * "transparent" DMA model is always correct, even on the RX side.
541 * CPPI TX:
542 * ========
543 * TX is a lot more reasonable than RX; it doesn't need to run in
544 * irq-per-packet mode very often. RNDIS mode seems to behave too
545 * (except how it handles the exactly-N-packets case). Building a
546 * txdma queue with multiple requests (urb or usb_request) looks
547 * like it would work ... but fault handling would need much testing.
549 * The main issue with TX mode RNDIS relates to transfer lengths that
550 * are an exact multiple of the packet length. It appears that there's
551 * a hiccup in that case (maybe the DMA completes before the ZLP gets
552 * written?) boiling down to not being able to rely on CPPI writing any
553 * terminating zero length packet before the next transfer is written.
554 * So that's punted to PIO; better yet, gadget drivers can avoid it.
556 * Plus, there's allegedly an undocumented constraint that rndis transfer
557 * length be a multiple of 64 bytes ... but the chip doesn't act that
558 * way, and we really don't _want_ that behavior anyway.
560 * On TX, "transparent" mode works ... although experiments have shown
561 * problems trying to use the SOP/EOP bits in different USB packets.
563 * REVISIT try to handle terminating zero length packets using CPPI
564 * instead of doing it by PIO after an IRQ. (Meanwhile, make Ethernet
565 * links avoid that issue by forcing them to avoid zlps.)
567 static void
568 cppi_next_tx_segment(struct musb *musb, struct cppi_channel *tx)
570 unsigned maxpacket = tx->maxpacket;
571 dma_addr_t addr = tx->buf_dma + tx->offset;
572 size_t length = tx->buf_len - tx->offset;
573 struct cppi_descriptor *bd;
574 unsigned n_bds;
575 unsigned i;
576 struct cppi_tx_stateram __iomem *tx_ram = tx->state_ram;
577 int rndis;
579 /* TX can use the CPPI "rndis" mode, where we can probably fit this
580 * transfer in one BD and one IRQ. The only time we would NOT want
581 * to use it is when hardware constraints prevent it, or if we'd
582 * trigger the "send a ZLP?" confusion.
584 rndis = (maxpacket & 0x3f) == 0
585 && length > maxpacket
586 && length < 0xffff
587 && (length % maxpacket) != 0;
589 if (rndis) {
590 maxpacket = length;
591 n_bds = 1;
592 } else {
593 n_bds = length / maxpacket;
594 if (!length || (length % maxpacket))
595 n_bds++;
596 n_bds = min(n_bds, (unsigned) NUM_TXCHAN_BD);
597 length = min(n_bds * maxpacket, length);
600 DBG(4, "TX DMA%d, pktSz %d %s bds %d dma 0x%x len %u\n",
601 tx->index,
602 maxpacket,
603 rndis ? "rndis" : "transparent",
604 n_bds,
605 addr, length);
607 cppi_rndis_update(tx, 0, musb->ctrl_base, rndis);
609 /* assuming here that channel_program is called during
610 * transfer initiation ... current code maintains state
611 * for one outstanding request only (no queues, not even
612 * the implicit ones of an iso urb).
615 bd = tx->freelist;
616 tx->head = bd;
617 tx->last_processed = NULL;
619 /* FIXME use BD pool like RX side does, and just queue
620 * the minimum number for this request.
623 /* Prepare queue of BDs first, then hand it to hardware.
624 * All BDs except maybe the last should be of full packet
625 * size; for RNDIS there _is_ only that last packet.
627 for (i = 0; i < n_bds; ) {
628 if (++i < n_bds && bd->next)
629 bd->hw_next = bd->next->dma;
630 else
631 bd->hw_next = 0;
633 bd->hw_bufp = tx->buf_dma + tx->offset;
635 /* FIXME set EOP only on the last packet,
636 * SOP only on the first ... avoid IRQs
638 if ((tx->offset + maxpacket) <= tx->buf_len) {
639 tx->offset += maxpacket;
640 bd->hw_off_len = maxpacket;
641 bd->hw_options = CPPI_SOP_SET | CPPI_EOP_SET
642 | CPPI_OWN_SET | maxpacket;
643 } else {
644 /* only this one may be a partial USB Packet */
645 u32 partial_len;
647 partial_len = tx->buf_len - tx->offset;
648 tx->offset = tx->buf_len;
649 bd->hw_off_len = partial_len;
651 bd->hw_options = CPPI_SOP_SET | CPPI_EOP_SET
652 | CPPI_OWN_SET | partial_len;
653 if (partial_len == 0)
654 bd->hw_options |= CPPI_ZERO_SET;
657 DBG(5, "TXBD %p: nxt %08x buf %08x len %04x opt %08x\n",
658 bd, bd->hw_next, bd->hw_bufp,
659 bd->hw_off_len, bd->hw_options);
661 /* update the last BD enqueued to the list */
662 tx->tail = bd;
663 bd = bd->next;
666 /* BDs live in DMA-coherent memory, but writes might be pending */
667 cpu_drain_writebuffer();
669 /* Write to the HeadPtr in state RAM to trigger */
670 musb_writel(&tx_ram->tx_head, 0, (u32)tx->freelist->dma);
672 cppi_dump_tx(5, tx, "/S");
676 * CPPI RX Woes:
677 * =============
678 * Consider a 1KB bulk RX buffer in two scenarios: (a) it's fed two 300 byte
679 * packets back-to-back, and (b) it's fed two 512 byte packets back-to-back.
680 * (Full speed transfers have similar scenarios.)
682 * The correct behavior for Linux is that (a) fills the buffer with 300 bytes,
683 * and the next packet goes into a buffer that's queued later; while (b) fills
684 * the buffer with 1024 bytes. How to do that with CPPI?
686 * - RX queues in "rndis" mode -- one single BD -- handle (a) correctly, but
687 * (b) loses **BADLY** because nothing (!) happens when that second packet
688 * fills the buffer, much less when a third one arrives. (Which makes this
689 * not a "true" RNDIS mode. In the RNDIS protocol short-packet termination
690 * is optional, and it's fine if peripherals -- not hosts! -- pad messages
691 * out to end-of-buffer. Standard PCI host controller DMA descriptors
692 * implement that mode by default ... which is no accident.)
694 * - RX queues in "transparent" mode -- two BDs with 512 bytes each -- have
695 * converse problems: (b) is handled right, but (a) loses badly. CPPI RX
696 * ignores SOP/EOP markings and processes both of those BDs; so both packets
697 * are loaded into the buffer (with a 212 byte gap between them), and the next
698 * buffer queued will NOT get its 300 bytes of data. (It seems like SOP/EOP
699 * are intended as outputs for RX queues, not inputs...)
701 * - A variant of "transparent" mode -- one BD at a time -- is the only way to
702 * reliably make both cases work, with software handling both cases correctly
703 * and at the significant penalty of needing an IRQ per packet. (The lack of
704 * I/O overlap can be slightly ameliorated by enabling double buffering.)
706 * So how to get rid of IRQ-per-packet? The transparent multi-BD case could
707 * be used in special cases like mass storage, which sets URB_SHORT_NOT_OK
708 * (or maybe its peripheral side counterpart) to flag (a) scenarios as errors
709 * with guaranteed driver level fault recovery and scrubbing out what's left
710 * of that garbaged datastream.
712 * But there seems to be no way to identify the cases where CPPI RNDIS mode
713 * is appropriate -- which do NOT include RNDIS host drivers, but do include
714 * the CDC Ethernet driver! -- and the documentation is incomplete/wrong.
715 * So we can't _ever_ use RX RNDIS mode ... except by using a heuristic
716 * that applies best on the peripheral side (and which could fail rudely).
718 * Leaving only "transparent" mode; we avoid multi-bd modes in almost all
719 * cases other than mass storage class. Otherwise we're correct but slow,
720 * since CPPI penalizes our need for a "true RNDIS" default mode.
724 /* Heuristic, intended to kick in for ethernet/rndis peripheral ONLY
726 * IFF
727 * (a) peripheral mode ... since rndis peripherals could pad their
728 * writes to hosts, causing i/o failure; or we'd have to cope with
729 * a largely unknowable variety of host side protocol variants
730 * (b) and short reads are NOT errors ... since full reads would
731 * cause those same i/o failures
732 * (c) and read length is
733 * - less than 64KB (max per cppi descriptor)
734 * - not a multiple of 4096 (g_zero default, full reads typical)
735 * - N (>1) packets long, ditto (full reads not EXPECTED)
736 * THEN
737 * try rx rndis mode
739 * Cost of heuristic failing: RXDMA wedges at the end of transfers that
740 * fill out the whole buffer. Buggy host side usb network drivers could
741 * trigger that, but "in the field" such bugs seem to be all but unknown.
743 * So this module parameter lets the heuristic be disabled. When using
744 * gadgetfs, the heuristic will probably need to be disabled.
746 static int cppi_rx_rndis = 1;
748 module_param(cppi_rx_rndis, bool, 0);
749 MODULE_PARM_DESC(cppi_rx_rndis, "enable/disable RX RNDIS heuristic");
753 * cppi_next_rx_segment - dma read for the next chunk of a buffer
754 * @musb: the controller
755 * @rx: dma channel
756 * @onepacket: true unless caller treats short reads as errors, and
757 * performs fault recovery above usbcore.
758 * Context: controller irqlocked
760 * See above notes about why we can't use multi-BD RX queues except in
761 * rare cases (mass storage class), and can never use the hardware "rndis"
762 * mode (since it's not a "true" RNDIS mode) with complete safety..
764 * It's ESSENTIAL that callers specify "onepacket" mode unless they kick in
765 * code to recover from corrupted datastreams after each short transfer.
767 static void
768 cppi_next_rx_segment(struct musb *musb, struct cppi_channel *rx, int onepacket)
770 unsigned maxpacket = rx->maxpacket;
771 dma_addr_t addr = rx->buf_dma + rx->offset;
772 size_t length = rx->buf_len - rx->offset;
773 struct cppi_descriptor *bd, *tail;
774 unsigned n_bds;
775 unsigned i;
776 void __iomem *tibase = musb->ctrl_base;
777 int is_rndis = 0;
778 struct cppi_rx_stateram __iomem *rx_ram = rx->state_ram;
780 if (onepacket) {
781 /* almost every USB driver, host or peripheral side */
782 n_bds = 1;
784 /* maybe apply the heuristic above */
785 if (cppi_rx_rndis
786 && is_peripheral_active(musb)
787 && length > maxpacket
788 && (length & ~0xffff) == 0
789 && (length & 0x0fff) != 0
790 && (length & (maxpacket - 1)) == 0) {
791 maxpacket = length;
792 is_rndis = 1;
794 } else {
795 /* virtually nothing except mass storage class */
796 if (length > 0xffff) {
797 n_bds = 0xffff / maxpacket;
798 length = n_bds * maxpacket;
799 } else {
800 n_bds = length / maxpacket;
801 if (length % maxpacket)
802 n_bds++;
804 if (n_bds == 1)
805 onepacket = 1;
806 else
807 n_bds = min(n_bds, (unsigned) NUM_RXCHAN_BD);
810 /* In host mode, autorequest logic can generate some IN tokens; it's
811 * tricky since we can't leave REQPKT set in RXCSR after the transfer
812 * finishes. So: multipacket transfers involve two or more segments.
813 * And always at least two IRQs ... RNDIS mode is not an option.
815 if (is_host_active(musb))
816 n_bds = cppi_autoreq_update(rx, tibase, onepacket, n_bds);
818 cppi_rndis_update(rx, 1, musb->ctrl_base, is_rndis);
820 length = min(n_bds * maxpacket, length);
822 DBG(4, "RX DMA%d seg, maxp %d %s bds %d (cnt %d) "
823 "dma 0x%x len %u %u/%u\n",
824 rx->index, maxpacket,
825 onepacket
826 ? (is_rndis ? "rndis" : "onepacket")
827 : "multipacket",
828 n_bds,
829 musb_readl(tibase,
830 DAVINCI_RXCPPI_BUFCNT0_REG + (rx->index * 4))
831 & 0xffff,
832 addr, length, rx->channel.actual_len, rx->buf_len);
834 /* only queue one segment at a time, since the hardware prevents
835 * correct queue shutdown after unexpected short packets
837 bd = cppi_bd_alloc(rx);
838 rx->head = bd;
840 /* Build BDs for all packets in this segment */
841 for (i = 0, tail = NULL; bd && i < n_bds; i++, tail = bd) {
842 u32 bd_len;
844 if (i) {
845 bd = cppi_bd_alloc(rx);
846 if (!bd)
847 break;
848 tail->next = bd;
849 tail->hw_next = bd->dma;
851 bd->hw_next = 0;
853 /* all but the last packet will be maxpacket size */
854 if (maxpacket < length)
855 bd_len = maxpacket;
856 else
857 bd_len = length;
859 bd->hw_bufp = addr;
860 addr += bd_len;
861 rx->offset += bd_len;
863 bd->hw_off_len = (0 /*offset*/ << 16) + bd_len;
864 bd->buflen = bd_len;
866 bd->hw_options = CPPI_OWN_SET | (i == 0 ? length : 0);
867 length -= bd_len;
870 /* we always expect at least one reusable BD! */
871 if (!tail) {
872 WARNING("rx dma%d -- no BDs? need %d\n", rx->index, n_bds);
873 return;
874 } else if (i < n_bds)
875 WARNING("rx dma%d -- only %d of %d BDs\n", rx->index, i, n_bds);
877 tail->next = NULL;
878 tail->hw_next = 0;
880 bd = rx->head;
881 rx->tail = tail;
883 /* short reads and other faults should terminate this entire
884 * dma segment. we want one "dma packet" per dma segment, not
885 * one per USB packet, terminating the whole queue at once...
886 * NOTE that current hardware seems to ignore SOP and EOP.
888 bd->hw_options |= CPPI_SOP_SET;
889 tail->hw_options |= CPPI_EOP_SET;
891 #ifdef CONFIG_USB_MUSB_DEBUG
892 if (_dbg_level(5)) {
893 struct cppi_descriptor *d;
895 for (d = rx->head; d; d = d->next)
896 cppi_dump_rxbd("S", d);
898 #endif
900 /* in case the preceding transfer left some state... */
901 tail = rx->last_processed;
902 if (tail) {
903 tail->next = bd;
904 tail->hw_next = bd->dma;
907 core_rxirq_enable(tibase, rx->index + 1);
909 /* BDs live in DMA-coherent memory, but writes might be pending */
910 cpu_drain_writebuffer();
912 /* REVISIT specs say to write this AFTER the BUFCNT register
913 * below ... but that loses badly.
915 musb_writel(&rx_ram->rx_head, 0, bd->dma);
917 /* bufferCount must be at least 3, and zeroes on completion
918 * unless it underflows below zero, or stops at two, or keeps
919 * growing ... grr.
921 i = musb_readl(tibase,
922 DAVINCI_RXCPPI_BUFCNT0_REG + (rx->index * 4))
923 & 0xffff;
925 if (!i)
926 musb_writel(tibase,
927 DAVINCI_RXCPPI_BUFCNT0_REG + (rx->index * 4),
928 n_bds + 2);
929 else if (n_bds > (i - 3))
930 musb_writel(tibase,
931 DAVINCI_RXCPPI_BUFCNT0_REG + (rx->index * 4),
932 n_bds - (i - 3));
934 i = musb_readl(tibase,
935 DAVINCI_RXCPPI_BUFCNT0_REG + (rx->index * 4))
936 & 0xffff;
937 if (i < (2 + n_bds)) {
938 DBG(2, "bufcnt%d underrun - %d (for %d)\n",
939 rx->index, i, n_bds);
940 musb_writel(tibase,
941 DAVINCI_RXCPPI_BUFCNT0_REG + (rx->index * 4),
942 n_bds + 2);
945 cppi_dump_rx(4, rx, "/S");
949 * cppi_channel_program - program channel for data transfer
950 * @ch: the channel
951 * @maxpacket: max packet size
952 * @mode: For RX, 1 unless the usb protocol driver promised to treat
953 * all short reads as errors and kick in high level fault recovery.
954 * For TX, ignored because of RNDIS mode races/glitches.
955 * @dma_addr: dma address of buffer
956 * @len: length of buffer
957 * Context: controller irqlocked
959 static int cppi_channel_program(struct dma_channel *ch,
960 u16 maxpacket, u8 mode,
961 dma_addr_t dma_addr, u32 len)
963 struct cppi_channel *cppi_ch;
964 struct cppi *controller;
965 struct musb *musb;
967 cppi_ch = container_of(ch, struct cppi_channel, channel);
968 controller = cppi_ch->controller;
969 musb = controller->musb;
971 switch (ch->status) {
972 case MUSB_DMA_STATUS_BUS_ABORT:
973 case MUSB_DMA_STATUS_CORE_ABORT:
974 /* fault irq handler should have handled cleanup */
975 WARNING("%cX DMA%d not cleaned up after abort!\n",
976 cppi_ch->transmit ? 'T' : 'R',
977 cppi_ch->index);
978 /* WARN_ON(1); */
979 break;
980 case MUSB_DMA_STATUS_BUSY:
981 WARNING("program active channel? %cX DMA%d\n",
982 cppi_ch->transmit ? 'T' : 'R',
983 cppi_ch->index);
984 /* WARN_ON(1); */
985 break;
986 case MUSB_DMA_STATUS_UNKNOWN:
987 DBG(1, "%cX DMA%d not allocated!\n",
988 cppi_ch->transmit ? 'T' : 'R',
989 cppi_ch->index);
990 /* FALLTHROUGH */
991 case MUSB_DMA_STATUS_FREE:
992 break;
995 ch->status = MUSB_DMA_STATUS_BUSY;
997 /* set transfer parameters, then queue up its first segment */
998 cppi_ch->buf_dma = dma_addr;
999 cppi_ch->offset = 0;
1000 cppi_ch->maxpacket = maxpacket;
1001 cppi_ch->buf_len = len;
1002 cppi_ch->channel.actual_len = 0;
1004 /* TX channel? or RX? */
1005 if (cppi_ch->transmit)
1006 cppi_next_tx_segment(musb, cppi_ch);
1007 else
1008 cppi_next_rx_segment(musb, cppi_ch, mode);
1010 return true;
1013 static bool cppi_rx_scan(struct cppi *cppi, unsigned ch)
1015 struct cppi_channel *rx = &cppi->rx[ch];
1016 struct cppi_rx_stateram __iomem *state = rx->state_ram;
1017 struct cppi_descriptor *bd;
1018 struct cppi_descriptor *last = rx->last_processed;
1019 bool completed = false;
1020 bool acked = false;
1021 int i;
1022 dma_addr_t safe2ack;
1023 void __iomem *regs = rx->hw_ep->regs;
1025 cppi_dump_rx(6, rx, "/K");
1027 bd = last ? last->next : rx->head;
1028 if (!bd)
1029 return false;
1031 /* run through all completed BDs */
1032 for (i = 0, safe2ack = musb_readl(&state->rx_complete, 0);
1033 (safe2ack || completed) && bd && i < NUM_RXCHAN_BD;
1034 i++, bd = bd->next) {
1035 u16 len;
1037 /* catch latest BD writes from CPPI */
1038 rmb();
1039 if (!completed && (bd->hw_options & CPPI_OWN_SET))
1040 break;
1042 DBG(5, "C/RXBD %08x: nxt %08x buf %08x "
1043 "off.len %08x opt.len %08x (%d)\n",
1044 bd->dma, bd->hw_next, bd->hw_bufp,
1045 bd->hw_off_len, bd->hw_options,
1046 rx->channel.actual_len);
1048 /* actual packet received length */
1049 if ((bd->hw_options & CPPI_SOP_SET) && !completed)
1050 len = bd->hw_off_len & CPPI_RECV_PKTLEN_MASK;
1051 else
1052 len = 0;
1054 if (bd->hw_options & CPPI_EOQ_MASK)
1055 completed = true;
1057 if (!completed && len < bd->buflen) {
1058 /* NOTE: when we get a short packet, RXCSR_H_REQPKT
1059 * must have been cleared, and no more DMA packets may
1060 * active be in the queue... TI docs didn't say, but
1061 * CPPI ignores those BDs even though OWN is still set.
1063 completed = true;
1064 DBG(3, "rx short %d/%d (%d)\n",
1065 len, bd->buflen,
1066 rx->channel.actual_len);
1069 /* If we got here, we expect to ack at least one BD; meanwhile
1070 * CPPI may completing other BDs while we scan this list...
1072 * RACE: we can notice OWN cleared before CPPI raises the
1073 * matching irq by writing that BD as the completion pointer.
1074 * In such cases, stop scanning and wait for the irq, avoiding
1075 * lost acks and states where BD ownership is unclear.
1077 if (bd->dma == safe2ack) {
1078 musb_writel(&state->rx_complete, 0, safe2ack);
1079 safe2ack = musb_readl(&state->rx_complete, 0);
1080 acked = true;
1081 if (bd->dma == safe2ack)
1082 safe2ack = 0;
1085 rx->channel.actual_len += len;
1087 cppi_bd_free(rx, last);
1088 last = bd;
1090 /* stop scanning on end-of-segment */
1091 if (bd->hw_next == 0)
1092 completed = true;
1094 rx->last_processed = last;
1096 /* dma abort, lost ack, or ... */
1097 if (!acked && last) {
1098 int csr;
1100 if (safe2ack == 0 || safe2ack == rx->last_processed->dma)
1101 musb_writel(&state->rx_complete, 0, safe2ack);
1102 if (safe2ack == 0) {
1103 cppi_bd_free(rx, last);
1104 rx->last_processed = NULL;
1106 /* if we land here on the host side, H_REQPKT will
1107 * be clear and we need to restart the queue...
1109 WARN_ON(rx->head);
1111 musb_ep_select(cppi->mregs, rx->index + 1);
1112 csr = musb_readw(regs, MUSB_RXCSR);
1113 if (csr & MUSB_RXCSR_DMAENAB) {
1114 DBG(4, "list%d %p/%p, last %08x%s, csr %04x\n",
1115 rx->index,
1116 rx->head, rx->tail,
1117 rx->last_processed
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 return IRQ_NONE;
1173 DBG(4, "CPPI IRQ Tx%x Rx%x\n", tx, rx);
1175 /* process TX channels */
1176 for (index = 0; tx; tx = tx >> 1, index++) {
1177 struct cppi_channel *tx_ch;
1178 struct cppi_tx_stateram __iomem *tx_ram;
1179 bool completed = false;
1180 struct cppi_descriptor *bd;
1182 if (!(tx & 1))
1183 continue;
1185 tx_ch = cppi->tx + index;
1186 tx_ram = tx_ch->state_ram;
1188 /* FIXME need a cppi_tx_scan() routine, which
1189 * can also be called from abort code
1192 cppi_dump_tx(5, tx_ch, "/E");
1194 bd = tx_ch->head;
1197 * If Head is null then this could mean that a abort interrupt
1198 * that needs to be acknowledged.
1200 if (NULL == bd) {
1201 DBG(1, "null BD\n");
1202 tx_ram->tx_complete = 0;
1203 continue;
1206 /* run through all completed BDs */
1207 for (i = 0; !completed && bd && i < NUM_TXCHAN_BD;
1208 i++, bd = bd->next) {
1209 u16 len;
1211 /* catch latest BD writes from CPPI */
1212 rmb();
1213 if (bd->hw_options & CPPI_OWN_SET)
1214 break;
1216 DBG(5, "C/TXBD %p n %x b %x off %x opt %x\n",
1217 bd, bd->hw_next, bd->hw_bufp,
1218 bd->hw_off_len, bd->hw_options);
1220 len = bd->hw_off_len & CPPI_BUFFER_LEN_MASK;
1221 tx_ch->channel.actual_len += len;
1223 tx_ch->last_processed = bd;
1225 /* write completion register to acknowledge
1226 * processing of completed BDs, and possibly
1227 * release the IRQ; EOQ might not be set ...
1229 * REVISIT use the same ack strategy as rx
1231 * REVISIT have observed bit 18 set; huh??
1233 /* if ((bd->hw_options & CPPI_EOQ_MASK)) */
1234 musb_writel(&tx_ram->tx_complete, 0, bd->dma);
1236 /* stop scanning on end-of-segment */
1237 if (bd->hw_next == 0)
1238 completed = true;
1241 /* on end of segment, maybe go to next one */
1242 if (completed) {
1243 /* cppi_dump_tx(4, tx_ch, "/complete"); */
1245 /* transfer more, or report completion */
1246 if (tx_ch->offset >= tx_ch->buf_len) {
1247 tx_ch->head = NULL;
1248 tx_ch->tail = NULL;
1249 tx_ch->channel.status = MUSB_DMA_STATUS_FREE;
1251 hw_ep = tx_ch->hw_ep;
1253 musb_dma_completion(musb, index + 1, 1);
1255 } else {
1256 /* Bigger transfer than we could fit in
1257 * that first batch of descriptors...
1259 cppi_next_tx_segment(musb, tx_ch);
1261 } else
1262 tx_ch->head = bd;
1265 /* Start processing the RX block */
1266 for (index = 0; rx; rx = rx >> 1, index++) {
1268 if (rx & 1) {
1269 struct cppi_channel *rx_ch;
1271 rx_ch = cppi->rx + index;
1273 /* let incomplete dma segments finish */
1274 if (!cppi_rx_scan(cppi, index))
1275 continue;
1277 /* start another dma segment if needed */
1278 if (rx_ch->channel.actual_len != rx_ch->buf_len
1279 && rx_ch->channel.actual_len
1280 == rx_ch->offset) {
1281 cppi_next_rx_segment(musb, rx_ch, 1);
1282 continue;
1285 /* all segments completed! */
1286 rx_ch->channel.status = MUSB_DMA_STATUS_FREE;
1288 hw_ep = rx_ch->hw_ep;
1290 core_rxirq_disable(tibase, index + 1);
1291 musb_dma_completion(musb, index + 1, 0);
1295 /* write to CPPI EOI register to re-enable interrupts */
1296 musb_writel(tibase, DAVINCI_CPPI_EOI_REG, 0);
1298 if (cppi->irq)
1299 spin_unlock_irqrestore(&musb->lock, flags);
1301 return IRQ_HANDLED;
1304 /* Instantiate a software object representing a DMA controller. */
1305 struct dma_controller *__init
1306 dma_controller_create(struct musb *musb, void __iomem *mregs)
1308 struct cppi *controller;
1309 struct device *dev = musb->controller;
1310 struct platform_device *pdev = to_platform_device(dev);
1311 int irq = platform_get_irq_byname(pdev, "dma");
1313 controller = kzalloc(sizeof *controller, GFP_KERNEL);
1314 if (!controller)
1315 return NULL;
1317 controller->mregs = mregs;
1318 controller->tibase = mregs - DAVINCI_BASE_OFFSET;
1320 controller->musb = musb;
1321 controller->controller.start = cppi_controller_start;
1322 controller->controller.stop = cppi_controller_stop;
1323 controller->controller.channel_alloc = cppi_channel_allocate;
1324 controller->controller.channel_release = cppi_channel_release;
1325 controller->controller.channel_program = cppi_channel_program;
1326 controller->controller.channel_abort = cppi_channel_abort;
1328 /* NOTE: allocating from on-chip SRAM would give the least
1329 * contention for memory access, if that ever matters here.
1332 /* setup BufferPool */
1333 controller->pool = dma_pool_create("cppi",
1334 controller->musb->controller,
1335 sizeof(struct cppi_descriptor),
1336 CPPI_DESCRIPTOR_ALIGN, 0);
1337 if (!controller->pool) {
1338 kfree(controller);
1339 return NULL;
1342 if (irq > 0) {
1343 if (request_irq(irq, cppi_interrupt, 0, "cppi-dma", musb)) {
1344 dev_err(dev, "request_irq %d failed!\n", irq);
1345 dma_controller_destroy(&controller->controller);
1346 return NULL;
1348 controller->irq = irq;
1351 return &controller->controller;
1355 * Destroy a previously-instantiated DMA controller.
1357 void dma_controller_destroy(struct dma_controller *c)
1359 struct cppi *cppi;
1361 cppi = container_of(c, struct cppi, controller);
1363 if (cppi->irq)
1364 free_irq(cppi->irq, cppi->musb);
1366 /* assert: caller stopped the controller first */
1367 dma_pool_destroy(cppi->pool);
1369 kfree(cppi);
1373 * Context: controller irqlocked, endpoint selected
1375 static int cppi_channel_abort(struct dma_channel *channel)
1377 struct cppi_channel *cppi_ch;
1378 struct cppi *controller;
1379 void __iomem *mbase;
1380 void __iomem *tibase;
1381 void __iomem *regs;
1382 u32 value;
1383 struct cppi_descriptor *queue;
1385 cppi_ch = container_of(channel, struct cppi_channel, channel);
1387 controller = cppi_ch->controller;
1389 switch (channel->status) {
1390 case MUSB_DMA_STATUS_BUS_ABORT:
1391 case MUSB_DMA_STATUS_CORE_ABORT:
1392 /* from RX or TX fault irq handler */
1393 case MUSB_DMA_STATUS_BUSY:
1394 /* the hardware needs shutting down */
1395 regs = cppi_ch->hw_ep->regs;
1396 break;
1397 case MUSB_DMA_STATUS_UNKNOWN:
1398 case MUSB_DMA_STATUS_FREE:
1399 return 0;
1400 default:
1401 return -EINVAL;
1404 if (!cppi_ch->transmit && cppi_ch->head)
1405 cppi_dump_rxq(3, "/abort", cppi_ch);
1407 mbase = controller->mregs;
1408 tibase = controller->tibase;
1410 queue = cppi_ch->head;
1411 cppi_ch->head = NULL;
1412 cppi_ch->tail = NULL;
1414 /* REVISIT should rely on caller having done this,
1415 * and caller should rely on us not changing it.
1416 * peripheral code is safe ... check host too.
1418 musb_ep_select(mbase, cppi_ch->index + 1);
1420 if (cppi_ch->transmit) {
1421 struct cppi_tx_stateram __iomem *tx_ram;
1422 /* REVISIT put timeouts on these controller handshakes */
1424 cppi_dump_tx(6, cppi_ch, " (teardown)");
1426 /* teardown DMA engine then usb core */
1427 do {
1428 value = musb_readl(tibase, DAVINCI_TXCPPI_TEAR_REG);
1429 } while (!(value & CPPI_TEAR_READY));
1430 musb_writel(tibase, DAVINCI_TXCPPI_TEAR_REG, cppi_ch->index);
1432 tx_ram = cppi_ch->state_ram;
1433 do {
1434 value = musb_readl(&tx_ram->tx_complete, 0);
1435 } while (0xFFFFFFFC != value);
1437 /* FIXME clean up the transfer state ... here?
1438 * the completion routine should get called with
1439 * an appropriate status code.
1442 value = musb_readw(regs, MUSB_TXCSR);
1443 value &= ~MUSB_TXCSR_DMAENAB;
1444 value |= MUSB_TXCSR_FLUSHFIFO;
1445 musb_writew(regs, MUSB_TXCSR, value);
1446 musb_writew(regs, MUSB_TXCSR, value);
1449 * 1. Write to completion Ptr value 0x1(bit 0 set)
1450 * (write back mode)
1451 * 2. Wait for abort interrupt and then put the channel in
1452 * compare mode by writing 1 to the tx_complete register.
1454 cppi_reset_tx(tx_ram, 1);
1455 cppi_ch->head = 0;
1456 musb_writel(&tx_ram->tx_complete, 0, 1);
1457 cppi_dump_tx(5, cppi_ch, " (done teardown)");
1459 /* REVISIT tx side _should_ clean up the same way
1460 * as the RX side ... this does no cleanup at all!
1463 } else /* RX */ {
1464 u16 csr;
1466 /* NOTE: docs don't guarantee any of this works ... we
1467 * expect that if the usb core stops telling the cppi core
1468 * to pull more data from it, then it'll be safe to flush
1469 * current RX DMA state iff any pending fifo transfer is done.
1472 core_rxirq_disable(tibase, cppi_ch->index + 1);
1474 /* for host, ensure ReqPkt is never set again */
1475 if (is_host_active(cppi_ch->controller->musb)) {
1476 value = musb_readl(tibase, DAVINCI_AUTOREQ_REG);
1477 value &= ~((0x3) << (cppi_ch->index * 2));
1478 musb_writel(tibase, DAVINCI_AUTOREQ_REG, value);
1481 csr = musb_readw(regs, MUSB_RXCSR);
1483 /* for host, clear (just) ReqPkt at end of current packet(s) */
1484 if (is_host_active(cppi_ch->controller->musb)) {
1485 csr |= MUSB_RXCSR_H_WZC_BITS;
1486 csr &= ~MUSB_RXCSR_H_REQPKT;
1487 } else
1488 csr |= MUSB_RXCSR_P_WZC_BITS;
1490 /* clear dma enable */
1491 csr &= ~(MUSB_RXCSR_DMAENAB);
1492 musb_writew(regs, MUSB_RXCSR, csr);
1493 csr = musb_readw(regs, MUSB_RXCSR);
1495 /* Quiesce: wait for current dma to finish (if not cleanup).
1496 * We can't use bit zero of stateram->rx_sop, since that
1497 * refers to an entire "DMA packet" not just emptying the
1498 * current fifo. Most segments need multiple usb packets.
1500 if (channel->status == MUSB_DMA_STATUS_BUSY)
1501 udelay(50);
1503 /* scan the current list, reporting any data that was
1504 * transferred and acking any IRQ
1506 cppi_rx_scan(controller, cppi_ch->index);
1508 /* clobber the existing state once it's idle
1510 * NOTE: arguably, we should also wait for all the other
1511 * RX channels to quiesce (how??) and then temporarily
1512 * disable RXCPPI_CTRL_REG ... but it seems that we can
1513 * rely on the controller restarting from state ram, with
1514 * only RXCPPI_BUFCNT state being bogus. BUFCNT will
1515 * correct itself after the next DMA transfer though.
1517 * REVISIT does using rndis mode change that?
1519 cppi_reset_rx(cppi_ch->state_ram);
1521 /* next DMA request _should_ load cppi head ptr */
1523 /* ... we don't "free" that list, only mutate it in place. */
1524 cppi_dump_rx(5, cppi_ch, " (done abort)");
1526 /* clean up previously pending bds */
1527 cppi_bd_free(cppi_ch, cppi_ch->last_processed);
1528 cppi_ch->last_processed = NULL;
1530 while (queue) {
1531 struct cppi_descriptor *tmp = queue->next;
1533 cppi_bd_free(cppi_ch, queue);
1534 queue = tmp;
1538 channel->status = MUSB_DMA_STATUS_FREE;
1539 cppi_ch->buf_dma = 0;
1540 cppi_ch->offset = 0;
1541 cppi_ch->buf_len = 0;
1542 cppi_ch->maxpacket = 0;
1543 return 0;
1546 /* TBD Queries:
1548 * Power Management ... probably turn off cppi during suspend, restart;
1549 * check state ram? Clocking is presumably shared with usb core.