2 * Texas Instruments CPDMA Driver
4 * Copyright (C) 2010 Texas Instruments
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation version 2.
10 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
11 * kind, whether express or implied; without even the implied warranty
12 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 #include <linux/kernel.h>
16 #include <linux/spinlock.h>
17 #include <linux/device.h>
18 #include <linux/module.h>
19 #include <linux/slab.h>
20 #include <linux/err.h>
21 #include <linux/dma-mapping.h>
24 #include "davinci_cpdma.h"
27 #define CPDMA_TXIDVER 0x00
28 #define CPDMA_TXCONTROL 0x04
29 #define CPDMA_TXTEARDOWN 0x08
30 #define CPDMA_RXIDVER 0x10
31 #define CPDMA_RXCONTROL 0x14
32 #define CPDMA_SOFTRESET 0x1c
33 #define CPDMA_RXTEARDOWN 0x18
34 #define CPDMA_TXINTSTATRAW 0x80
35 #define CPDMA_TXINTSTATMASKED 0x84
36 #define CPDMA_TXINTMASKSET 0x88
37 #define CPDMA_TXINTMASKCLEAR 0x8c
38 #define CPDMA_MACINVECTOR 0x90
39 #define CPDMA_MACEOIVECTOR 0x94
40 #define CPDMA_RXINTSTATRAW 0xa0
41 #define CPDMA_RXINTSTATMASKED 0xa4
42 #define CPDMA_RXINTMASKSET 0xa8
43 #define CPDMA_RXINTMASKCLEAR 0xac
44 #define CPDMA_DMAINTSTATRAW 0xb0
45 #define CPDMA_DMAINTSTATMASKED 0xb4
46 #define CPDMA_DMAINTMASKSET 0xb8
47 #define CPDMA_DMAINTMASKCLEAR 0xbc
48 #define CPDMA_DMAINT_HOSTERR BIT(1)
50 /* the following exist only if has_ext_regs is set */
51 #define CPDMA_DMACONTROL 0x20
52 #define CPDMA_DMASTATUS 0x24
53 #define CPDMA_RXBUFFOFS 0x28
54 #define CPDMA_EM_CONTROL 0x2c
56 /* Descriptor mode bits */
57 #define CPDMA_DESC_SOP BIT(31)
58 #define CPDMA_DESC_EOP BIT(30)
59 #define CPDMA_DESC_OWNER BIT(29)
60 #define CPDMA_DESC_EOQ BIT(28)
61 #define CPDMA_DESC_TD_COMPLETE BIT(27)
62 #define CPDMA_DESC_PASS_CRC BIT(26)
64 #define CPDMA_TEARDOWN_VALUE 0xfffffffc
78 struct cpdma_desc_pool
{
81 void __iomem
*iomap
; /* ioremap map */
82 void *cpumap
; /* dma_alloc map */
83 int desc_size
, mem_size
;
84 int num_desc
, used_desc
;
85 unsigned long *bitmap
;
96 static const char *cpdma_state_str
[] = { "idle", "active", "teardown" };
99 enum cpdma_state state
;
100 struct cpdma_params params
;
102 struct cpdma_desc_pool
*pool
;
104 struct cpdma_chan
*channels
[2 * CPDMA_MAX_CHANNELS
];
108 enum cpdma_state state
;
109 struct cpdma_ctlr
*ctlr
;
112 struct cpdma_desc __iomem
*head
, *tail
;
114 void __iomem
*hdp
, *cp
, *rxfree
;
116 cpdma_handler_fn handler
;
117 enum dma_data_direction dir
;
118 struct cpdma_chan_stats stats
;
119 /* offsets into dmaregs */
120 int int_set
, int_clear
, td
;
123 /* The following make access to common cpdma_ctlr params more readable */
124 #define dmaregs params.dmaregs
125 #define num_chan params.num_chan
127 /* various accessors */
128 #define dma_reg_read(ctlr, ofs) __raw_readl((ctlr)->dmaregs + (ofs))
129 #define chan_read(chan, fld) __raw_readl((chan)->fld)
130 #define desc_read(desc, fld) __raw_readl(&(desc)->fld)
131 #define dma_reg_write(ctlr, ofs, v) __raw_writel(v, (ctlr)->dmaregs + (ofs))
132 #define chan_write(chan, fld, v) __raw_writel(v, (chan)->fld)
133 #define desc_write(desc, fld, v) __raw_writel((u32)(v), &(desc)->fld)
136 * Utility constructs for a cpdma descriptor pool. Some devices (e.g. davinci
137 * emac) have dedicated on-chip memory for these descriptors. Some other
138 * devices (e.g. cpsw switches) use plain old memory. Descriptor pools
139 * abstract out these details
141 static struct cpdma_desc_pool
*
142 cpdma_desc_pool_create(struct device
*dev
, u32 phys
, u32 hw_addr
,
146 struct cpdma_desc_pool
*pool
;
148 pool
= kzalloc(sizeof(*pool
), GFP_KERNEL
);
152 spin_lock_init(&pool
->lock
);
155 pool
->mem_size
= size
;
156 pool
->desc_size
= ALIGN(sizeof(struct cpdma_desc
), align
);
157 pool
->num_desc
= size
/ pool
->desc_size
;
159 bitmap_size
= (pool
->num_desc
/ BITS_PER_LONG
) * sizeof(long);
160 pool
->bitmap
= kzalloc(bitmap_size
, GFP_KERNEL
);
166 pool
->iomap
= ioremap(phys
, size
);
167 pool
->hw_addr
= hw_addr
;
169 pool
->cpumap
= dma_alloc_coherent(dev
, size
, &pool
->phys
,
171 pool
->iomap
= pool
->cpumap
;
172 pool
->hw_addr
= pool
->phys
;
184 static void cpdma_desc_pool_destroy(struct cpdma_desc_pool
*pool
)
191 spin_lock_irqsave(&pool
->lock
, flags
);
192 WARN_ON(pool
->used_desc
);
195 dma_free_coherent(pool
->dev
, pool
->mem_size
, pool
->cpumap
,
198 iounmap(pool
->iomap
);
200 spin_unlock_irqrestore(&pool
->lock
, flags
);
204 static inline dma_addr_t
desc_phys(struct cpdma_desc_pool
*pool
,
205 struct cpdma_desc __iomem
*desc
)
209 return pool
->hw_addr
+ (__force dma_addr_t
)desc
-
210 (__force dma_addr_t
)pool
->iomap
;
213 static inline struct cpdma_desc __iomem
*
214 desc_from_phys(struct cpdma_desc_pool
*pool
, dma_addr_t dma
)
216 return dma
? pool
->iomap
+ dma
- pool
->hw_addr
: NULL
;
219 static struct cpdma_desc __iomem
*
220 cpdma_desc_alloc(struct cpdma_desc_pool
*pool
, int num_desc
)
224 struct cpdma_desc __iomem
*desc
= NULL
;
226 spin_lock_irqsave(&pool
->lock
, flags
);
228 index
= bitmap_find_next_zero_area(pool
->bitmap
, pool
->num_desc
, 0,
230 if (index
< pool
->num_desc
) {
231 bitmap_set(pool
->bitmap
, index
, num_desc
);
232 desc
= pool
->iomap
+ pool
->desc_size
* index
;
236 spin_unlock_irqrestore(&pool
->lock
, flags
);
240 static void cpdma_desc_free(struct cpdma_desc_pool
*pool
,
241 struct cpdma_desc __iomem
*desc
, int num_desc
)
243 unsigned long flags
, index
;
245 index
= ((unsigned long)desc
- (unsigned long)pool
->iomap
) /
247 spin_lock_irqsave(&pool
->lock
, flags
);
248 bitmap_clear(pool
->bitmap
, index
, num_desc
);
250 spin_unlock_irqrestore(&pool
->lock
, flags
);
253 struct cpdma_ctlr
*cpdma_ctlr_create(struct cpdma_params
*params
)
255 struct cpdma_ctlr
*ctlr
;
257 ctlr
= kzalloc(sizeof(*ctlr
), GFP_KERNEL
);
261 ctlr
->state
= CPDMA_STATE_IDLE
;
262 ctlr
->params
= *params
;
263 ctlr
->dev
= params
->dev
;
264 spin_lock_init(&ctlr
->lock
);
266 ctlr
->pool
= cpdma_desc_pool_create(ctlr
->dev
,
267 ctlr
->params
.desc_mem_phys
,
268 ctlr
->params
.desc_hw_addr
,
269 ctlr
->params
.desc_mem_size
,
270 ctlr
->params
.desc_align
);
276 if (WARN_ON(ctlr
->num_chan
> CPDMA_MAX_CHANNELS
))
277 ctlr
->num_chan
= CPDMA_MAX_CHANNELS
;
280 EXPORT_SYMBOL_GPL(cpdma_ctlr_create
);
282 int cpdma_ctlr_start(struct cpdma_ctlr
*ctlr
)
287 spin_lock_irqsave(&ctlr
->lock
, flags
);
288 if (ctlr
->state
!= CPDMA_STATE_IDLE
) {
289 spin_unlock_irqrestore(&ctlr
->lock
, flags
);
293 if (ctlr
->params
.has_soft_reset
) {
294 unsigned long timeout
= jiffies
+ HZ
/10;
296 dma_reg_write(ctlr
, CPDMA_SOFTRESET
, 1);
297 while (time_before(jiffies
, timeout
)) {
298 if (dma_reg_read(ctlr
, CPDMA_SOFTRESET
) == 0)
301 WARN_ON(!time_before(jiffies
, timeout
));
304 for (i
= 0; i
< ctlr
->num_chan
; i
++) {
305 __raw_writel(0, ctlr
->params
.txhdp
+ 4 * i
);
306 __raw_writel(0, ctlr
->params
.rxhdp
+ 4 * i
);
307 __raw_writel(0, ctlr
->params
.txcp
+ 4 * i
);
308 __raw_writel(0, ctlr
->params
.rxcp
+ 4 * i
);
311 dma_reg_write(ctlr
, CPDMA_RXINTMASKCLEAR
, 0xffffffff);
312 dma_reg_write(ctlr
, CPDMA_TXINTMASKCLEAR
, 0xffffffff);
314 dma_reg_write(ctlr
, CPDMA_TXCONTROL
, 1);
315 dma_reg_write(ctlr
, CPDMA_RXCONTROL
, 1);
317 ctlr
->state
= CPDMA_STATE_ACTIVE
;
319 for (i
= 0; i
< ARRAY_SIZE(ctlr
->channels
); i
++) {
320 if (ctlr
->channels
[i
])
321 cpdma_chan_start(ctlr
->channels
[i
]);
323 spin_unlock_irqrestore(&ctlr
->lock
, flags
);
326 EXPORT_SYMBOL_GPL(cpdma_ctlr_start
);
328 int cpdma_ctlr_stop(struct cpdma_ctlr
*ctlr
)
333 spin_lock_irqsave(&ctlr
->lock
, flags
);
334 if (ctlr
->state
!= CPDMA_STATE_ACTIVE
) {
335 spin_unlock_irqrestore(&ctlr
->lock
, flags
);
339 ctlr
->state
= CPDMA_STATE_TEARDOWN
;
341 for (i
= 0; i
< ARRAY_SIZE(ctlr
->channels
); i
++) {
342 if (ctlr
->channels
[i
])
343 cpdma_chan_stop(ctlr
->channels
[i
]);
346 dma_reg_write(ctlr
, CPDMA_RXINTMASKCLEAR
, 0xffffffff);
347 dma_reg_write(ctlr
, CPDMA_TXINTMASKCLEAR
, 0xffffffff);
349 dma_reg_write(ctlr
, CPDMA_TXCONTROL
, 0);
350 dma_reg_write(ctlr
, CPDMA_RXCONTROL
, 0);
352 ctlr
->state
= CPDMA_STATE_IDLE
;
354 spin_unlock_irqrestore(&ctlr
->lock
, flags
);
357 EXPORT_SYMBOL_GPL(cpdma_ctlr_stop
);
359 int cpdma_ctlr_dump(struct cpdma_ctlr
*ctlr
)
361 struct device
*dev
= ctlr
->dev
;
365 spin_lock_irqsave(&ctlr
->lock
, flags
);
367 dev_info(dev
, "CPDMA: state: %s", cpdma_state_str
[ctlr
->state
]);
369 dev_info(dev
, "CPDMA: txidver: %x",
370 dma_reg_read(ctlr
, CPDMA_TXIDVER
));
371 dev_info(dev
, "CPDMA: txcontrol: %x",
372 dma_reg_read(ctlr
, CPDMA_TXCONTROL
));
373 dev_info(dev
, "CPDMA: txteardown: %x",
374 dma_reg_read(ctlr
, CPDMA_TXTEARDOWN
));
375 dev_info(dev
, "CPDMA: rxidver: %x",
376 dma_reg_read(ctlr
, CPDMA_RXIDVER
));
377 dev_info(dev
, "CPDMA: rxcontrol: %x",
378 dma_reg_read(ctlr
, CPDMA_RXCONTROL
));
379 dev_info(dev
, "CPDMA: softreset: %x",
380 dma_reg_read(ctlr
, CPDMA_SOFTRESET
));
381 dev_info(dev
, "CPDMA: rxteardown: %x",
382 dma_reg_read(ctlr
, CPDMA_RXTEARDOWN
));
383 dev_info(dev
, "CPDMA: txintstatraw: %x",
384 dma_reg_read(ctlr
, CPDMA_TXINTSTATRAW
));
385 dev_info(dev
, "CPDMA: txintstatmasked: %x",
386 dma_reg_read(ctlr
, CPDMA_TXINTSTATMASKED
));
387 dev_info(dev
, "CPDMA: txintmaskset: %x",
388 dma_reg_read(ctlr
, CPDMA_TXINTMASKSET
));
389 dev_info(dev
, "CPDMA: txintmaskclear: %x",
390 dma_reg_read(ctlr
, CPDMA_TXINTMASKCLEAR
));
391 dev_info(dev
, "CPDMA: macinvector: %x",
392 dma_reg_read(ctlr
, CPDMA_MACINVECTOR
));
393 dev_info(dev
, "CPDMA: maceoivector: %x",
394 dma_reg_read(ctlr
, CPDMA_MACEOIVECTOR
));
395 dev_info(dev
, "CPDMA: rxintstatraw: %x",
396 dma_reg_read(ctlr
, CPDMA_RXINTSTATRAW
));
397 dev_info(dev
, "CPDMA: rxintstatmasked: %x",
398 dma_reg_read(ctlr
, CPDMA_RXINTSTATMASKED
));
399 dev_info(dev
, "CPDMA: rxintmaskset: %x",
400 dma_reg_read(ctlr
, CPDMA_RXINTMASKSET
));
401 dev_info(dev
, "CPDMA: rxintmaskclear: %x",
402 dma_reg_read(ctlr
, CPDMA_RXINTMASKCLEAR
));
403 dev_info(dev
, "CPDMA: dmaintstatraw: %x",
404 dma_reg_read(ctlr
, CPDMA_DMAINTSTATRAW
));
405 dev_info(dev
, "CPDMA: dmaintstatmasked: %x",
406 dma_reg_read(ctlr
, CPDMA_DMAINTSTATMASKED
));
407 dev_info(dev
, "CPDMA: dmaintmaskset: %x",
408 dma_reg_read(ctlr
, CPDMA_DMAINTMASKSET
));
409 dev_info(dev
, "CPDMA: dmaintmaskclear: %x",
410 dma_reg_read(ctlr
, CPDMA_DMAINTMASKCLEAR
));
412 if (!ctlr
->params
.has_ext_regs
) {
413 dev_info(dev
, "CPDMA: dmacontrol: %x",
414 dma_reg_read(ctlr
, CPDMA_DMACONTROL
));
415 dev_info(dev
, "CPDMA: dmastatus: %x",
416 dma_reg_read(ctlr
, CPDMA_DMASTATUS
));
417 dev_info(dev
, "CPDMA: rxbuffofs: %x",
418 dma_reg_read(ctlr
, CPDMA_RXBUFFOFS
));
421 for (i
= 0; i
< ARRAY_SIZE(ctlr
->channels
); i
++)
422 if (ctlr
->channels
[i
])
423 cpdma_chan_dump(ctlr
->channels
[i
]);
425 spin_unlock_irqrestore(&ctlr
->lock
, flags
);
428 EXPORT_SYMBOL_GPL(cpdma_ctlr_dump
);
430 int cpdma_ctlr_destroy(struct cpdma_ctlr
*ctlr
)
438 spin_lock_irqsave(&ctlr
->lock
, flags
);
439 if (ctlr
->state
!= CPDMA_STATE_IDLE
)
440 cpdma_ctlr_stop(ctlr
);
442 for (i
= 0; i
< ARRAY_SIZE(ctlr
->channels
); i
++) {
443 if (ctlr
->channels
[i
])
444 cpdma_chan_destroy(ctlr
->channels
[i
]);
447 cpdma_desc_pool_destroy(ctlr
->pool
);
448 spin_unlock_irqrestore(&ctlr
->lock
, flags
);
452 EXPORT_SYMBOL_GPL(cpdma_ctlr_destroy
);
454 int cpdma_ctlr_int_ctrl(struct cpdma_ctlr
*ctlr
, bool enable
)
459 spin_lock_irqsave(&ctlr
->lock
, flags
);
460 if (ctlr
->state
!= CPDMA_STATE_ACTIVE
) {
461 spin_unlock_irqrestore(&ctlr
->lock
, flags
);
465 reg
= enable
? CPDMA_DMAINTMASKSET
: CPDMA_DMAINTMASKCLEAR
;
466 dma_reg_write(ctlr
, reg
, CPDMA_DMAINT_HOSTERR
);
468 for (i
= 0; i
< ARRAY_SIZE(ctlr
->channels
); i
++) {
469 if (ctlr
->channels
[i
])
470 cpdma_chan_int_ctrl(ctlr
->channels
[i
], enable
);
473 spin_unlock_irqrestore(&ctlr
->lock
, flags
);
477 void cpdma_ctlr_eoi(struct cpdma_ctlr
*ctlr
)
479 dma_reg_write(ctlr
, CPDMA_MACEOIVECTOR
, 0);
482 struct cpdma_chan
*cpdma_chan_create(struct cpdma_ctlr
*ctlr
, int chan_num
,
483 cpdma_handler_fn handler
)
485 struct cpdma_chan
*chan
;
486 int ret
, offset
= (chan_num
% CPDMA_MAX_CHANNELS
) * 4;
489 if (__chan_linear(chan_num
) >= ctlr
->num_chan
)
493 chan
= kzalloc(sizeof(*chan
), GFP_KERNEL
);
497 spin_lock_irqsave(&ctlr
->lock
, flags
);
499 if (ctlr
->channels
[chan_num
])
503 chan
->state
= CPDMA_STATE_IDLE
;
504 chan
->chan_num
= chan_num
;
505 chan
->handler
= handler
;
507 if (is_rx_chan(chan
)) {
508 chan
->hdp
= ctlr
->params
.rxhdp
+ offset
;
509 chan
->cp
= ctlr
->params
.rxcp
+ offset
;
510 chan
->rxfree
= ctlr
->params
.rxfree
+ offset
;
511 chan
->int_set
= CPDMA_RXINTMASKSET
;
512 chan
->int_clear
= CPDMA_RXINTMASKCLEAR
;
513 chan
->td
= CPDMA_RXTEARDOWN
;
514 chan
->dir
= DMA_FROM_DEVICE
;
516 chan
->hdp
= ctlr
->params
.txhdp
+ offset
;
517 chan
->cp
= ctlr
->params
.txcp
+ offset
;
518 chan
->int_set
= CPDMA_TXINTMASKSET
;
519 chan
->int_clear
= CPDMA_TXINTMASKCLEAR
;
520 chan
->td
= CPDMA_TXTEARDOWN
;
521 chan
->dir
= DMA_TO_DEVICE
;
523 chan
->mask
= BIT(chan_linear(chan
));
525 spin_lock_init(&chan
->lock
);
527 ctlr
->channels
[chan_num
] = chan
;
528 spin_unlock_irqrestore(&ctlr
->lock
, flags
);
532 spin_unlock_irqrestore(&ctlr
->lock
, flags
);
537 EXPORT_SYMBOL_GPL(cpdma_chan_create
);
539 int cpdma_chan_destroy(struct cpdma_chan
*chan
)
541 struct cpdma_ctlr
*ctlr
;
548 spin_lock_irqsave(&ctlr
->lock
, flags
);
549 if (chan
->state
!= CPDMA_STATE_IDLE
)
550 cpdma_chan_stop(chan
);
551 ctlr
->channels
[chan
->chan_num
] = NULL
;
552 spin_unlock_irqrestore(&ctlr
->lock
, flags
);
556 EXPORT_SYMBOL_GPL(cpdma_chan_destroy
);
558 int cpdma_chan_get_stats(struct cpdma_chan
*chan
,
559 struct cpdma_chan_stats
*stats
)
564 spin_lock_irqsave(&chan
->lock
, flags
);
565 memcpy(stats
, &chan
->stats
, sizeof(*stats
));
566 spin_unlock_irqrestore(&chan
->lock
, flags
);
570 int cpdma_chan_dump(struct cpdma_chan
*chan
)
573 struct device
*dev
= chan
->ctlr
->dev
;
575 spin_lock_irqsave(&chan
->lock
, flags
);
577 dev_info(dev
, "channel %d (%s %d) state %s",
578 chan
->chan_num
, is_rx_chan(chan
) ? "rx" : "tx",
579 chan_linear(chan
), cpdma_state_str
[chan
->state
]);
580 dev_info(dev
, "\thdp: %x\n", chan_read(chan
, hdp
));
581 dev_info(dev
, "\tcp: %x\n", chan_read(chan
, cp
));
583 dev_info(dev
, "\trxfree: %x\n",
584 chan_read(chan
, rxfree
));
587 dev_info(dev
, "\tstats head_enqueue: %d\n",
588 chan
->stats
.head_enqueue
);
589 dev_info(dev
, "\tstats tail_enqueue: %d\n",
590 chan
->stats
.tail_enqueue
);
591 dev_info(dev
, "\tstats pad_enqueue: %d\n",
592 chan
->stats
.pad_enqueue
);
593 dev_info(dev
, "\tstats misqueued: %d\n",
594 chan
->stats
.misqueued
);
595 dev_info(dev
, "\tstats desc_alloc_fail: %d\n",
596 chan
->stats
.desc_alloc_fail
);
597 dev_info(dev
, "\tstats pad_alloc_fail: %d\n",
598 chan
->stats
.pad_alloc_fail
);
599 dev_info(dev
, "\tstats runt_receive_buff: %d\n",
600 chan
->stats
.runt_receive_buff
);
601 dev_info(dev
, "\tstats runt_transmit_buff: %d\n",
602 chan
->stats
.runt_transmit_buff
);
603 dev_info(dev
, "\tstats empty_dequeue: %d\n",
604 chan
->stats
.empty_dequeue
);
605 dev_info(dev
, "\tstats busy_dequeue: %d\n",
606 chan
->stats
.busy_dequeue
);
607 dev_info(dev
, "\tstats good_dequeue: %d\n",
608 chan
->stats
.good_dequeue
);
609 dev_info(dev
, "\tstats requeue: %d\n",
610 chan
->stats
.requeue
);
611 dev_info(dev
, "\tstats teardown_dequeue: %d\n",
612 chan
->stats
.teardown_dequeue
);
614 spin_unlock_irqrestore(&chan
->lock
, flags
);
618 static void __cpdma_chan_submit(struct cpdma_chan
*chan
,
619 struct cpdma_desc __iomem
*desc
)
621 struct cpdma_ctlr
*ctlr
= chan
->ctlr
;
622 struct cpdma_desc __iomem
*prev
= chan
->tail
;
623 struct cpdma_desc_pool
*pool
= ctlr
->pool
;
627 desc_dma
= desc_phys(pool
, desc
);
629 /* simple case - idle channel */
631 chan
->stats
.head_enqueue
++;
634 if (chan
->state
== CPDMA_STATE_ACTIVE
)
635 chan_write(chan
, hdp
, desc_dma
);
639 /* first chain the descriptor at the tail of the list */
640 desc_write(prev
, hw_next
, desc_dma
);
642 chan
->stats
.tail_enqueue
++;
644 /* next check if EOQ has been triggered already */
645 mode
= desc_read(prev
, hw_mode
);
646 if (((mode
& (CPDMA_DESC_EOQ
| CPDMA_DESC_OWNER
)) == CPDMA_DESC_EOQ
) &&
647 (chan
->state
== CPDMA_STATE_ACTIVE
)) {
648 desc_write(prev
, hw_mode
, mode
& ~CPDMA_DESC_EOQ
);
649 chan_write(chan
, hdp
, desc_dma
);
650 chan
->stats
.misqueued
++;
654 int cpdma_chan_submit(struct cpdma_chan
*chan
, void *token
, void *data
,
655 int len
, gfp_t gfp_mask
)
657 struct cpdma_ctlr
*ctlr
= chan
->ctlr
;
658 struct cpdma_desc __iomem
*desc
;
664 spin_lock_irqsave(&chan
->lock
, flags
);
666 if (chan
->state
== CPDMA_STATE_TEARDOWN
) {
671 desc
= cpdma_desc_alloc(ctlr
->pool
, 1);
673 chan
->stats
.desc_alloc_fail
++;
678 if (len
< ctlr
->params
.min_packet_size
) {
679 len
= ctlr
->params
.min_packet_size
;
680 chan
->stats
.runt_transmit_buff
++;
683 buffer
= dma_map_single(ctlr
->dev
, data
, len
, chan
->dir
);
684 mode
= CPDMA_DESC_OWNER
| CPDMA_DESC_SOP
| CPDMA_DESC_EOP
;
686 desc_write(desc
, hw_next
, 0);
687 desc_write(desc
, hw_buffer
, buffer
);
688 desc_write(desc
, hw_len
, len
);
689 desc_write(desc
, hw_mode
, mode
| len
);
690 desc_write(desc
, sw_token
, token
);
691 desc_write(desc
, sw_buffer
, buffer
);
692 desc_write(desc
, sw_len
, len
);
694 __cpdma_chan_submit(chan
, desc
);
696 if (chan
->state
== CPDMA_STATE_ACTIVE
&& chan
->rxfree
)
697 chan_write(chan
, rxfree
, 1);
702 spin_unlock_irqrestore(&chan
->lock
, flags
);
705 EXPORT_SYMBOL_GPL(cpdma_chan_submit
);
707 static void __cpdma_chan_free(struct cpdma_chan
*chan
,
708 struct cpdma_desc __iomem
*desc
,
709 int outlen
, int status
)
711 struct cpdma_ctlr
*ctlr
= chan
->ctlr
;
712 struct cpdma_desc_pool
*pool
= ctlr
->pool
;
717 token
= (void *)desc_read(desc
, sw_token
);
718 buff_dma
= desc_read(desc
, sw_buffer
);
719 origlen
= desc_read(desc
, sw_len
);
721 dma_unmap_single(ctlr
->dev
, buff_dma
, origlen
, chan
->dir
);
722 cpdma_desc_free(pool
, desc
, 1);
723 (*chan
->handler
)(token
, outlen
, status
);
726 static int __cpdma_chan_process(struct cpdma_chan
*chan
)
728 struct cpdma_ctlr
*ctlr
= chan
->ctlr
;
729 struct cpdma_desc __iomem
*desc
;
731 struct cpdma_desc_pool
*pool
= ctlr
->pool
;
735 spin_lock_irqsave(&chan
->lock
, flags
);
739 chan
->stats
.empty_dequeue
++;
743 desc_dma
= desc_phys(pool
, desc
);
745 status
= __raw_readl(&desc
->hw_mode
);
746 outlen
= status
& 0x7ff;
747 if (status
& CPDMA_DESC_OWNER
) {
748 chan
->stats
.busy_dequeue
++;
752 status
= status
& (CPDMA_DESC_EOQ
| CPDMA_DESC_TD_COMPLETE
);
754 chan
->head
= desc_from_phys(pool
, desc_read(desc
, hw_next
));
755 chan_write(chan
, cp
, desc_dma
);
757 chan
->stats
.good_dequeue
++;
759 if (status
& CPDMA_DESC_EOQ
) {
760 chan
->stats
.requeue
++;
761 chan_write(chan
, hdp
, desc_phys(pool
, chan
->head
));
764 spin_unlock_irqrestore(&chan
->lock
, flags
);
766 __cpdma_chan_free(chan
, desc
, outlen
, status
);
770 spin_unlock_irqrestore(&chan
->lock
, flags
);
774 int cpdma_chan_process(struct cpdma_chan
*chan
, int quota
)
776 int used
= 0, ret
= 0;
778 if (chan
->state
!= CPDMA_STATE_ACTIVE
)
781 while (used
< quota
) {
782 ret
= __cpdma_chan_process(chan
);
789 EXPORT_SYMBOL_GPL(cpdma_chan_process
);
791 int cpdma_chan_start(struct cpdma_chan
*chan
)
793 struct cpdma_ctlr
*ctlr
= chan
->ctlr
;
794 struct cpdma_desc_pool
*pool
= ctlr
->pool
;
797 spin_lock_irqsave(&chan
->lock
, flags
);
798 if (chan
->state
!= CPDMA_STATE_IDLE
) {
799 spin_unlock_irqrestore(&chan
->lock
, flags
);
802 if (ctlr
->state
!= CPDMA_STATE_ACTIVE
) {
803 spin_unlock_irqrestore(&chan
->lock
, flags
);
806 dma_reg_write(ctlr
, chan
->int_set
, chan
->mask
);
807 chan
->state
= CPDMA_STATE_ACTIVE
;
809 chan_write(chan
, hdp
, desc_phys(pool
, chan
->head
));
811 chan_write(chan
, rxfree
, chan
->count
);
814 spin_unlock_irqrestore(&chan
->lock
, flags
);
817 EXPORT_SYMBOL_GPL(cpdma_chan_start
);
819 int cpdma_chan_stop(struct cpdma_chan
*chan
)
821 struct cpdma_ctlr
*ctlr
= chan
->ctlr
;
822 struct cpdma_desc_pool
*pool
= ctlr
->pool
;
825 unsigned long timeout
;
827 spin_lock_irqsave(&chan
->lock
, flags
);
828 if (chan
->state
!= CPDMA_STATE_ACTIVE
) {
829 spin_unlock_irqrestore(&chan
->lock
, flags
);
833 chan
->state
= CPDMA_STATE_TEARDOWN
;
834 dma_reg_write(ctlr
, chan
->int_clear
, chan
->mask
);
836 /* trigger teardown */
837 dma_reg_write(ctlr
, chan
->td
, chan_linear(chan
));
839 /* wait for teardown complete */
840 timeout
= jiffies
+ HZ
/10; /* 100 msec */
841 while (time_before(jiffies
, timeout
)) {
842 u32 cp
= chan_read(chan
, cp
);
843 if ((cp
& CPDMA_TEARDOWN_VALUE
) == CPDMA_TEARDOWN_VALUE
)
847 WARN_ON(!time_before(jiffies
, timeout
));
848 chan_write(chan
, cp
, CPDMA_TEARDOWN_VALUE
);
850 /* handle completed packets */
851 spin_unlock_irqrestore(&chan
->lock
, flags
);
853 ret
= __cpdma_chan_process(chan
);
856 } while ((ret
& CPDMA_DESC_TD_COMPLETE
) == 0);
857 spin_lock_irqsave(&chan
->lock
, flags
);
859 /* remaining packets haven't been tx/rx'ed, clean them up */
861 struct cpdma_desc __iomem
*desc
= chan
->head
;
864 next_dma
= desc_read(desc
, hw_next
);
865 chan
->head
= desc_from_phys(pool
, next_dma
);
867 chan
->stats
.teardown_dequeue
++;
869 /* issue callback without locks held */
870 spin_unlock_irqrestore(&chan
->lock
, flags
);
871 __cpdma_chan_free(chan
, desc
, 0, -ENOSYS
);
872 spin_lock_irqsave(&chan
->lock
, flags
);
875 chan
->state
= CPDMA_STATE_IDLE
;
876 spin_unlock_irqrestore(&chan
->lock
, flags
);
879 EXPORT_SYMBOL_GPL(cpdma_chan_stop
);
881 int cpdma_chan_int_ctrl(struct cpdma_chan
*chan
, bool enable
)
885 spin_lock_irqsave(&chan
->lock
, flags
);
886 if (chan
->state
!= CPDMA_STATE_ACTIVE
) {
887 spin_unlock_irqrestore(&chan
->lock
, flags
);
891 dma_reg_write(chan
->ctlr
, enable
? chan
->int_set
: chan
->int_clear
,
893 spin_unlock_irqrestore(&chan
->lock
, flags
);
898 struct cpdma_control_info
{
902 #define ACCESS_RO BIT(0)
903 #define ACCESS_WO BIT(1)
904 #define ACCESS_RW (ACCESS_RO | ACCESS_WO)
907 struct cpdma_control_info controls
[] = {
908 [CPDMA_CMD_IDLE
] = {CPDMA_DMACONTROL
, 3, 1, ACCESS_WO
},
909 [CPDMA_COPY_ERROR_FRAMES
] = {CPDMA_DMACONTROL
, 4, 1, ACCESS_RW
},
910 [CPDMA_RX_OFF_LEN_UPDATE
] = {CPDMA_DMACONTROL
, 2, 1, ACCESS_RW
},
911 [CPDMA_RX_OWNERSHIP_FLIP
] = {CPDMA_DMACONTROL
, 1, 1, ACCESS_RW
},
912 [CPDMA_TX_PRIO_FIXED
] = {CPDMA_DMACONTROL
, 0, 1, ACCESS_RW
},
913 [CPDMA_STAT_IDLE
] = {CPDMA_DMASTATUS
, 31, 1, ACCESS_RO
},
914 [CPDMA_STAT_TX_ERR_CODE
] = {CPDMA_DMASTATUS
, 20, 0xf, ACCESS_RW
},
915 [CPDMA_STAT_TX_ERR_CHAN
] = {CPDMA_DMASTATUS
, 16, 0x7, ACCESS_RW
},
916 [CPDMA_STAT_RX_ERR_CODE
] = {CPDMA_DMASTATUS
, 12, 0xf, ACCESS_RW
},
917 [CPDMA_STAT_RX_ERR_CHAN
] = {CPDMA_DMASTATUS
, 8, 0x7, ACCESS_RW
},
918 [CPDMA_RX_BUFFER_OFFSET
] = {CPDMA_RXBUFFOFS
, 0, 0xffff, ACCESS_RW
},
921 int cpdma_control_get(struct cpdma_ctlr
*ctlr
, int control
)
924 struct cpdma_control_info
*info
= &controls
[control
];
927 spin_lock_irqsave(&ctlr
->lock
, flags
);
930 if (!ctlr
->params
.has_ext_regs
)
934 if (ctlr
->state
!= CPDMA_STATE_ACTIVE
)
938 if (control
< 0 || control
>= ARRAY_SIZE(controls
))
942 if ((info
->access
& ACCESS_RO
) != ACCESS_RO
)
945 ret
= (dma_reg_read(ctlr
, info
->reg
) >> info
->shift
) & info
->mask
;
948 spin_unlock_irqrestore(&ctlr
->lock
, flags
);
952 int cpdma_control_set(struct cpdma_ctlr
*ctlr
, int control
, int value
)
955 struct cpdma_control_info
*info
= &controls
[control
];
959 spin_lock_irqsave(&ctlr
->lock
, flags
);
962 if (!ctlr
->params
.has_ext_regs
)
966 if (ctlr
->state
!= CPDMA_STATE_ACTIVE
)
970 if (control
< 0 || control
>= ARRAY_SIZE(controls
))
974 if ((info
->access
& ACCESS_WO
) != ACCESS_WO
)
977 val
= dma_reg_read(ctlr
, info
->reg
);
978 val
&= ~(info
->mask
<< info
->shift
);
979 val
|= (value
& info
->mask
) << info
->shift
;
980 dma_reg_write(ctlr
, info
->reg
, val
);
984 spin_unlock_irqrestore(&ctlr
->lock
, flags
);