9 #include <linux/module.h>
11 #include <linux/slab.h>
45 #define NUM_TXCHAN_BD 64
46 #define NUM_RXCHAN_BD 64
48 static inline void cpu_drain_writebuffer(
void)
51 #ifdef CONFIG_CPU_ARM926T
55 asm(
"mcr p15, 0, r0, c7, c10, 4 @ drain write buffer\n");
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);
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);
105 musb_writel(&tx->tx_complete, 0, ptr);
132 static int cppi_channel_abort(
struct dma_channel *);
148 bd = cppi_bd_alloc(c);
163 controller->
tx[
i].transmit =
true;
164 controller->
tx[
i].index =
i;
167 controller->
rx[
i].transmit =
false;
168 controller->
rx[
i].index =
i;
173 cppi_pool_init(controller, controller->
tx + i);
175 cppi_pool_init(controller, controller->
rx + i);
177 tibase = controller->
tibase;
189 cppi_reset_tx(tx, 0);
233 musb = controller->
musb;
235 tibase = controller->
tibase;
245 controller->
tx[
i].last_processed =
NULL;
246 cppi_pool_free(controller->
tx + i);
249 cppi_pool_free(controller->
rx + i);
270 static inline void core_rxirq_disable(
void __iomem *tibase,
unsigned epnum)
275 static inline void core_rxirq_enable(
void __iomem *tibase,
unsigned epnum)
298 tibase = controller->
tibase;
299 musb = controller->
musb;
302 index = ep->
epnum - 1;
312 cppi_ch = controller->
tx +
index;
318 cppi_ch = controller->
rx +
index;
319 core_rxirq_disable(tibase, ep->
epnum);
327 index, transmit ?
'T' :
'R', cppi_ch);
330 cppi_ch->
channel.max_len = 0x7fffffff;
348 "releasing idle DMA channel %p\n", c);
350 core_rxirq_enable(tibase, c->
index + 1);
367 "RX DMA%d%s: %d left, csr %04x, "
368 "%08x H%08x S%08x C%08x, "
369 "B%08x L%08x %08x .. %08x"
378 musb_readl(&rx->
rx_sop, 0),
390 cppi_dump_tx(
int level,
struct cppi_channel *c,
const char *tag)
398 "TX DMA%d%s: csr %04x, "
399 "H%08x S%08x C%08x %08x, "
400 "F%08x L%08x .. %08x"
406 musb_readl(&tx->
tx_buf, 0),
438 #ifdef CONFIG_USB_MUSB_DEBUG
439 static void cppi_dump_rxbd(
const char *tag,
struct cppi_descriptor *bd)
442 "nxt %08x buf %08x off.blen %08x opt.plen %08x\n",
449 static void cppi_dump_rxq(
int level,
const char *tag,
struct cppi_channel *rx)
451 #ifdef CONFIG_USB_MUSB_DEBUG
454 if (!_dbg_level(level))
456 cppi_dump_rx(level, rx, tag);
459 for (bd = rx->
head; bd; bd = bd->
next)
460 cppi_dump_rxbd(
"active", bd);
468 static inline int cppi_autoreq_update(
struct cppi_channel *rx,
469 void __iomem *tibase,
int onepacket,
unsigned n_bds)
473 #ifdef RNDIS_RX_IS_USABLE
479 val = tmp & ~((0x3) << (rx->
index * 2));
487 val |= ((0x3) << (rx->
index * 2));
491 val |= ((0x1) << (rx->
index * 2));
510 if (n_bds && rx->
channel.actual_len) {
576 cppi_next_tx_segment(
struct musb *musb,
struct cppi_channel *tx)
592 rndis = (maxpacket & 0x3f) == 0
593 && length > maxpacket
595 && (length % maxpacket) != 0;
601 n_bds = length / maxpacket;
602 if (!length || (length % maxpacket))
605 length =
min(n_bds * maxpacket, length);
611 rndis ?
"rndis" :
"transparent",
613 (
unsigned long long)addr, length);
615 cppi_rndis_update(tx, 0, musb->
ctrl_base, rndis);
635 for (i = 0; i < n_bds; ) {
636 if (++i < n_bds && bd->
next)
661 if (partial_len == 0)
675 cpu_drain_writebuffer();
680 cppi_dump_tx(5, tx,
"/S");
754 static bool cppi_rx_rndis = 1;
776 cppi_next_rx_segment(
struct musb *musb,
struct cppi_channel *rx,
int onepacket)
795 && length > maxpacket
796 && (length & ~0xffff) == 0
797 && (length & 0x0fff) != 0
798 && (length & (maxpacket - 1)) == 0) {
804 if (length > 0xffff) {
805 n_bds = 0xffff / maxpacket;
806 length = n_bds * maxpacket;
808 n_bds = length / maxpacket;
809 if (length % maxpacket)
824 n_bds = cppi_autoreq_update(rx, tibase, onepacket, n_bds);
826 cppi_rndis_update(rx, 1, musb->
ctrl_base, is_rndis);
828 length =
min(n_bds * maxpacket, length);
831 "dma 0x%llx len %u %u/%u\n",
832 rx->
index, maxpacket,
834 ? (is_rndis ?
"rndis" :
"onepacket")
840 (
unsigned long long)addr, length,
846 bd = cppi_bd_alloc(rx);
850 for (i = 0, tail =
NULL; bd && i < n_bds; i++, tail = bd) {
854 bd = cppi_bd_alloc(rx);
863 if (maxpacket < length)
881 WARNING(
"rx dma%d -- no BDs? need %d\n", rx->
index, n_bds);
883 }
else if (i < n_bds)
884 WARNING(
"rx dma%d -- only %d of %d BDs\n", rx->
index, i, n_bds);
900 #ifdef CONFIG_USB_MUSB_DEBUG
905 cppi_dump_rxbd(
"S", d);
916 core_rxirq_enable(tibase, rx->
index + 1);
919 cpu_drain_writebuffer();
930 i = musb_readl(tibase,
938 else if (n_bds > (i - 3))
943 i = musb_readl(tibase,
946 if (i < (2 + n_bds)) {
948 rx->
index, i, n_bds);
954 cppi_dump_rx(4, rx,
"/S");
968 static int cppi_channel_program(
struct dma_channel *ch,
978 musb = controller->
musb;
984 WARNING(
"%cX DMA%d not cleaned up after abort!\n",
990 WARNING(
"program active channel? %cX DMA%d\n",
1011 cppi_ch->
channel.actual_len = 0;
1015 cppi_next_tx_segment(musb, cppi_ch);
1017 cppi_next_rx_segment(musb, cppi_ch, mode);
1022 static bool cppi_rx_scan(
struct cppi *cppi,
unsigned ch)
1033 struct musb *musb = cppi->
musb;
1035 cppi_dump_rx(6, rx,
"/K");
1042 for (i = 0, safe2ack = musb_readl(&state->
rx_complete, 0);
1044 i++, bd = bd->
next) {
1053 "off.len %08x opt.len %08x (%d)\n",
1067 if (!completed && len < bd->
buflen) {
1087 if (bd->
dma == safe2ack) {
1091 if (bd->
dma == safe2ack)
1095 rx->
channel.actual_len += len;
1097 cppi_bd_free(rx, last);
1107 if (!acked && last) {
1112 if (safe2ack == 0) {
1113 cppi_bd_free(rx, last);
1128 ? (
unsigned long long)
1131 completed ?
", completed" :
"",
1133 cppi_dump_rxq(4,
"/what?", rx);
1158 cppi_dump_rx(6, rx, completed ?
"/completed" :
"/cleaned");
1164 struct musb *musb =
dev_id;
1183 spin_unlock_irqrestore(&musb->
lock,
flags);
1190 for (index = 0;
tx; tx = tx >> 1, index++) {
1193 bool completed =
false;
1206 cppi_dump_tx(5, tx_ch,
"/E");
1222 i++, bd = bd->
next) {
1231 bd, bd->hw_next, bd->hw_bufp,
1232 bd->hw_off_len, bd->hw_options);
1235 tx_ch->
channel.actual_len += len;
1251 if (bd->hw_next == 0)
1265 hw_ep = tx_ch->
hw_ep;
1273 cppi_next_tx_segment(musb, tx_ch);
1280 for (index = 0;
rx; rx = rx >> 1, index++) {
1288 if (!cppi_rx_scan(cppi, index))
1295 cppi_next_rx_segment(musb, rx_ch, 1);
1302 hw_ep = rx_ch->
hw_ep;
1304 core_rxirq_disable(tibase, index + 1);
1313 spin_unlock_irqrestore(&musb->
lock,
flags);
1327 controller = kzalloc(
sizeof *controller,
GFP_KERNEL);
1331 controller->
mregs = mregs;
1334 controller->
musb = musb;
1335 controller->
controller.start = cppi_controller_start;
1336 controller->
controller.stop = cppi_controller_stop;
1337 controller->
controller.channel_alloc = cppi_channel_allocate;
1338 controller->
controller.channel_release = cppi_channel_release;
1339 controller->
controller.channel_program = cppi_channel_program;
1340 controller->
controller.channel_abort = cppi_channel_abort;
1348 controller->
musb->controller,
1351 if (!controller->
pool) {
1358 dev_err(dev,
"request_irq %d failed!\n", irq);
1362 controller->
irq = irq;
1389 static int cppi_channel_abort(
struct dma_channel *channel)
1403 switch (channel->
status) {
1409 regs = cppi_ch->
hw_ep->regs;
1419 cppi_dump_rxq(3,
"/abort", cppi_ch);
1421 mbase = controller->
mregs;
1422 tibase = controller->
tibase;
1424 queue = cppi_ch->
head;
1438 cppi_dump_tx(6, cppi_ch,
" (teardown)");
1449 }
while (0xFFFFFFFC != value);
1468 cppi_reset_tx(tx_ram, 1);
1471 cppi_dump_tx(5, cppi_ch,
" (done teardown)");
1486 core_rxirq_disable(tibase, cppi_ch->
index + 1);
1491 value &= ~((0x3) << (cppi_ch->
index * 2));
1520 cppi_rx_scan(controller, cppi_ch->
index);
1538 cppi_dump_rx(5, cppi_ch,
" (done abort)");
1547 cppi_bd_free(cppi_ch, queue);