2 * offload engine driver for the Marvell XOR engine
3 * Copyright (C) 2007, 2008, Marvell International Ltd.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
19 #include <linux/init.h>
20 #include <linux/module.h>
21 #include <linux/slab.h>
22 #include <linux/delay.h>
23 #include <linux/dma-mapping.h>
24 #include <linux/spinlock.h>
25 #include <linux/interrupt.h>
26 #include <linux/platform_device.h>
27 #include <linux/memory.h>
28 #include <linux/clk.h>
30 #include <linux/of_irq.h>
31 #include <linux/irqdomain.h>
32 #include <linux/platform_data/dma-mv_xor.h>
34 #include "dmaengine.h"
37 static void mv_xor_issue_pending(struct dma_chan
*chan
);
39 #define to_mv_xor_chan(chan) \
40 container_of(chan, struct mv_xor_chan, dmachan)
42 #define to_mv_xor_slot(tx) \
43 container_of(tx, struct mv_xor_desc_slot, async_tx)
45 #define mv_chan_to_devp(chan) \
48 static void mv_desc_init(struct mv_xor_desc_slot
*desc
,
49 dma_addr_t addr
, u32 byte_count
,
50 enum dma_ctrl_flags flags
)
52 struct mv_xor_desc
*hw_desc
= desc
->hw_desc
;
54 hw_desc
->status
= XOR_DESC_DMA_OWNED
;
55 hw_desc
->phy_next_desc
= 0;
56 /* Enable end-of-descriptor interrupts only for DMA_PREP_INTERRUPT */
57 hw_desc
->desc_command
= (flags
& DMA_PREP_INTERRUPT
) ?
58 XOR_DESC_EOD_INT_EN
: 0;
59 hw_desc
->phy_dest_addr
= addr
;
60 hw_desc
->byte_count
= byte_count
;
63 static void mv_desc_set_next_desc(struct mv_xor_desc_slot
*desc
,
66 struct mv_xor_desc
*hw_desc
= desc
->hw_desc
;
67 BUG_ON(hw_desc
->phy_next_desc
);
68 hw_desc
->phy_next_desc
= next_desc_addr
;
71 static void mv_desc_clear_next_desc(struct mv_xor_desc_slot
*desc
)
73 struct mv_xor_desc
*hw_desc
= desc
->hw_desc
;
74 hw_desc
->phy_next_desc
= 0;
77 static void mv_desc_set_src_addr(struct mv_xor_desc_slot
*desc
,
78 int index
, dma_addr_t addr
)
80 struct mv_xor_desc
*hw_desc
= desc
->hw_desc
;
81 hw_desc
->phy_src_addr
[mv_phy_src_idx(index
)] = addr
;
82 if (desc
->type
== DMA_XOR
)
83 hw_desc
->desc_command
|= (1 << index
);
86 static u32
mv_chan_get_current_desc(struct mv_xor_chan
*chan
)
88 return readl_relaxed(XOR_CURR_DESC(chan
));
91 static void mv_chan_set_next_descriptor(struct mv_xor_chan
*chan
,
94 writel_relaxed(next_desc_addr
, XOR_NEXT_DESC(chan
));
97 static void mv_chan_unmask_interrupts(struct mv_xor_chan
*chan
)
99 u32 val
= readl_relaxed(XOR_INTR_MASK(chan
));
100 val
|= XOR_INTR_MASK_VALUE
<< (chan
->idx
* 16);
101 writel_relaxed(val
, XOR_INTR_MASK(chan
));
104 static u32
mv_chan_get_intr_cause(struct mv_xor_chan
*chan
)
106 u32 intr_cause
= readl_relaxed(XOR_INTR_CAUSE(chan
));
107 intr_cause
= (intr_cause
>> (chan
->idx
* 16)) & 0xFFFF;
111 static void mv_xor_device_clear_eoc_cause(struct mv_xor_chan
*chan
)
115 val
= XOR_INT_END_OF_DESC
| XOR_INT_END_OF_CHAIN
| XOR_INT_STOPPED
;
116 val
= ~(val
<< (chan
->idx
* 16));
117 dev_dbg(mv_chan_to_devp(chan
), "%s, val 0x%08x\n", __func__
, val
);
118 writel_relaxed(val
, XOR_INTR_CAUSE(chan
));
121 static void mv_xor_device_clear_err_status(struct mv_xor_chan
*chan
)
123 u32 val
= 0xFFFF0000 >> (chan
->idx
* 16);
124 writel_relaxed(val
, XOR_INTR_CAUSE(chan
));
127 static void mv_set_mode(struct mv_xor_chan
*chan
,
128 enum dma_transaction_type type
)
131 u32 config
= readl_relaxed(XOR_CONFIG(chan
));
135 op_mode
= XOR_OPERATION_MODE_XOR
;
138 op_mode
= XOR_OPERATION_MODE_MEMCPY
;
141 dev_err(mv_chan_to_devp(chan
),
142 "error: unsupported operation %d\n",
151 #if defined(__BIG_ENDIAN)
152 config
|= XOR_DESCRIPTOR_SWAP
;
154 config
&= ~XOR_DESCRIPTOR_SWAP
;
157 writel_relaxed(config
, XOR_CONFIG(chan
));
158 chan
->current_type
= type
;
161 static void mv_chan_activate(struct mv_xor_chan
*chan
)
163 dev_dbg(mv_chan_to_devp(chan
), " activate chan.\n");
165 /* writel ensures all descriptors are flushed before activation */
166 writel(BIT(0), XOR_ACTIVATION(chan
));
169 static char mv_chan_is_busy(struct mv_xor_chan
*chan
)
171 u32 state
= readl_relaxed(XOR_ACTIVATION(chan
));
173 state
= (state
>> 4) & 0x3;
175 return (state
== 1) ? 1 : 0;
179 * mv_xor_free_slots - flags descriptor slots for reuse
180 * @slot: Slot to free
181 * Caller must hold &mv_chan->lock while calling this function
183 static void mv_xor_free_slots(struct mv_xor_chan
*mv_chan
,
184 struct mv_xor_desc_slot
*slot
)
186 dev_dbg(mv_chan_to_devp(mv_chan
), "%s %d slot %p\n",
187 __func__
, __LINE__
, slot
);
194 * mv_xor_start_new_chain - program the engine to operate on new chain headed by
196 * Caller must hold &mv_chan->lock while calling this function
198 static void mv_xor_start_new_chain(struct mv_xor_chan
*mv_chan
,
199 struct mv_xor_desc_slot
*sw_desc
)
201 dev_dbg(mv_chan_to_devp(mv_chan
), "%s %d: sw_desc %p\n",
202 __func__
, __LINE__
, sw_desc
);
204 /* set the hardware chain */
205 mv_chan_set_next_descriptor(mv_chan
, sw_desc
->async_tx
.phys
);
208 mv_xor_issue_pending(&mv_chan
->dmachan
);
212 mv_xor_run_tx_complete_actions(struct mv_xor_desc_slot
*desc
,
213 struct mv_xor_chan
*mv_chan
, dma_cookie_t cookie
)
215 BUG_ON(desc
->async_tx
.cookie
< 0);
217 if (desc
->async_tx
.cookie
> 0) {
218 cookie
= desc
->async_tx
.cookie
;
220 /* call the callback (must not sleep or submit new
221 * operations to this channel)
223 if (desc
->async_tx
.callback
)
224 desc
->async_tx
.callback(
225 desc
->async_tx
.callback_param
);
227 dma_descriptor_unmap(&desc
->async_tx
);
230 /* run dependent operations */
231 dma_run_dependencies(&desc
->async_tx
);
237 mv_xor_clean_completed_slots(struct mv_xor_chan
*mv_chan
)
239 struct mv_xor_desc_slot
*iter
, *_iter
;
241 dev_dbg(mv_chan_to_devp(mv_chan
), "%s %d\n", __func__
, __LINE__
);
242 list_for_each_entry_safe(iter
, _iter
, &mv_chan
->completed_slots
,
245 if (async_tx_test_ack(&iter
->async_tx
)) {
246 list_del(&iter
->completed_node
);
247 mv_xor_free_slots(mv_chan
, iter
);
254 mv_xor_clean_slot(struct mv_xor_desc_slot
*desc
,
255 struct mv_xor_chan
*mv_chan
)
257 dev_dbg(mv_chan_to_devp(mv_chan
), "%s %d: desc %p flags %d\n",
258 __func__
, __LINE__
, desc
, desc
->async_tx
.flags
);
259 list_del(&desc
->chain_node
);
260 /* the client is allowed to attach dependent operations
263 if (!async_tx_test_ack(&desc
->async_tx
)) {
264 /* move this slot to the completed_slots */
265 list_add_tail(&desc
->completed_node
, &mv_chan
->completed_slots
);
269 mv_xor_free_slots(mv_chan
, desc
);
273 /* This function must be called with the mv_xor_chan spinlock held */
274 static void mv_xor_slot_cleanup(struct mv_xor_chan
*mv_chan
)
276 struct mv_xor_desc_slot
*iter
, *_iter
;
277 dma_cookie_t cookie
= 0;
278 int busy
= mv_chan_is_busy(mv_chan
);
279 u32 current_desc
= mv_chan_get_current_desc(mv_chan
);
280 int current_cleaned
= 0;
281 struct mv_xor_desc
*hw_desc
;
283 dev_dbg(mv_chan_to_devp(mv_chan
), "%s %d\n", __func__
, __LINE__
);
284 dev_dbg(mv_chan_to_devp(mv_chan
), "current_desc %x\n", current_desc
);
285 mv_xor_clean_completed_slots(mv_chan
);
287 /* free completed slots from the chain starting with
288 * the oldest descriptor
291 list_for_each_entry_safe(iter
, _iter
, &mv_chan
->chain
,
294 /* clean finished descriptors */
295 hw_desc
= iter
->hw_desc
;
296 if (hw_desc
->status
& XOR_DESC_SUCCESS
) {
297 cookie
= mv_xor_run_tx_complete_actions(iter
, mv_chan
,
300 /* done processing desc, clean slot */
301 mv_xor_clean_slot(iter
, mv_chan
);
303 /* break if we did cleaned the current */
304 if (iter
->async_tx
.phys
== current_desc
) {
309 if (iter
->async_tx
.phys
== current_desc
) {
316 if ((busy
== 0) && !list_empty(&mv_chan
->chain
)) {
317 if (current_cleaned
) {
319 * current descriptor cleaned and removed, run
322 iter
= list_entry(mv_chan
->chain
.next
,
323 struct mv_xor_desc_slot
,
325 mv_xor_start_new_chain(mv_chan
, iter
);
327 if (!list_is_last(&iter
->chain_node
, &mv_chan
->chain
)) {
329 * descriptors are still waiting after
330 * current, trigger them
332 iter
= list_entry(iter
->chain_node
.next
,
333 struct mv_xor_desc_slot
,
335 mv_xor_start_new_chain(mv_chan
, iter
);
338 * some descriptors are still waiting
341 tasklet_schedule(&mv_chan
->irq_tasklet
);
347 mv_chan
->dmachan
.completed_cookie
= cookie
;
350 static void mv_xor_tasklet(unsigned long data
)
352 struct mv_xor_chan
*chan
= (struct mv_xor_chan
*) data
;
354 spin_lock_bh(&chan
->lock
);
355 mv_xor_slot_cleanup(chan
);
356 spin_unlock_bh(&chan
->lock
);
359 static struct mv_xor_desc_slot
*
360 mv_xor_alloc_slot(struct mv_xor_chan
*mv_chan
)
362 struct mv_xor_desc_slot
*iter
, *_iter
;
365 /* start search from the last allocated descrtiptor
366 * if a contiguous allocation can not be found start searching
367 * from the beginning of the list
371 iter
= mv_chan
->last_used
;
373 iter
= list_entry(&mv_chan
->all_slots
,
374 struct mv_xor_desc_slot
,
377 list_for_each_entry_safe_continue(
378 iter
, _iter
, &mv_chan
->all_slots
, slot_node
) {
381 prefetch(&_iter
->async_tx
);
382 if (iter
->slot_used
) {
383 /* give up after finding the first busy slot
384 * on the second pass through the list
391 /* pre-ack descriptor */
392 async_tx_ack(&iter
->async_tx
);
395 INIT_LIST_HEAD(&iter
->chain_node
);
396 iter
->async_tx
.cookie
= -EBUSY
;
397 mv_chan
->last_used
= iter
;
398 mv_desc_clear_next_desc(iter
);
406 /* try to free some slots if the allocation fails */
407 tasklet_schedule(&mv_chan
->irq_tasklet
);
412 /************************ DMA engine API functions ****************************/
414 mv_xor_tx_submit(struct dma_async_tx_descriptor
*tx
)
416 struct mv_xor_desc_slot
*sw_desc
= to_mv_xor_slot(tx
);
417 struct mv_xor_chan
*mv_chan
= to_mv_xor_chan(tx
->chan
);
418 struct mv_xor_desc_slot
*old_chain_tail
;
420 int new_hw_chain
= 1;
422 dev_dbg(mv_chan_to_devp(mv_chan
),
423 "%s sw_desc %p: async_tx %p\n",
424 __func__
, sw_desc
, &sw_desc
->async_tx
);
426 spin_lock_bh(&mv_chan
->lock
);
427 cookie
= dma_cookie_assign(tx
);
429 if (list_empty(&mv_chan
->chain
))
430 list_add_tail(&sw_desc
->chain_node
, &mv_chan
->chain
);
434 old_chain_tail
= list_entry(mv_chan
->chain
.prev
,
435 struct mv_xor_desc_slot
,
437 list_add_tail(&sw_desc
->chain_node
, &mv_chan
->chain
);
439 dev_dbg(mv_chan_to_devp(mv_chan
), "Append to last desc %pa\n",
440 &old_chain_tail
->async_tx
.phys
);
442 /* fix up the hardware chain */
443 mv_desc_set_next_desc(old_chain_tail
, sw_desc
->async_tx
.phys
);
445 /* if the channel is not busy */
446 if (!mv_chan_is_busy(mv_chan
)) {
447 u32 current_desc
= mv_chan_get_current_desc(mv_chan
);
449 * and the curren desc is the end of the chain before
450 * the append, then we need to start the channel
452 if (current_desc
== old_chain_tail
->async_tx
.phys
)
458 mv_xor_start_new_chain(mv_chan
, sw_desc
);
460 spin_unlock_bh(&mv_chan
->lock
);
465 /* returns the number of allocated descriptors */
466 static int mv_xor_alloc_chan_resources(struct dma_chan
*chan
)
471 struct mv_xor_chan
*mv_chan
= to_mv_xor_chan(chan
);
472 struct mv_xor_desc_slot
*slot
= NULL
;
473 int num_descs_in_pool
= MV_XOR_POOL_SIZE
/MV_XOR_SLOT_SIZE
;
475 /* Allocate descriptor slots */
476 idx
= mv_chan
->slots_allocated
;
477 while (idx
< num_descs_in_pool
) {
478 slot
= kzalloc(sizeof(*slot
), GFP_KERNEL
);
480 dev_info(mv_chan_to_devp(mv_chan
),
481 "channel only initialized %d descriptor slots",
485 virt_desc
= mv_chan
->dma_desc_pool_virt
;
486 slot
->hw_desc
= virt_desc
+ idx
* MV_XOR_SLOT_SIZE
;
488 dma_async_tx_descriptor_init(&slot
->async_tx
, chan
);
489 slot
->async_tx
.tx_submit
= mv_xor_tx_submit
;
490 INIT_LIST_HEAD(&slot
->chain_node
);
491 INIT_LIST_HEAD(&slot
->slot_node
);
492 dma_desc
= mv_chan
->dma_desc_pool
;
493 slot
->async_tx
.phys
= dma_desc
+ idx
* MV_XOR_SLOT_SIZE
;
496 spin_lock_bh(&mv_chan
->lock
);
497 mv_chan
->slots_allocated
= idx
;
498 list_add_tail(&slot
->slot_node
, &mv_chan
->all_slots
);
499 spin_unlock_bh(&mv_chan
->lock
);
502 if (mv_chan
->slots_allocated
&& !mv_chan
->last_used
)
503 mv_chan
->last_used
= list_entry(mv_chan
->all_slots
.next
,
504 struct mv_xor_desc_slot
,
507 dev_dbg(mv_chan_to_devp(mv_chan
),
508 "allocated %d descriptor slots last_used: %p\n",
509 mv_chan
->slots_allocated
, mv_chan
->last_used
);
511 return mv_chan
->slots_allocated
? : -ENOMEM
;
514 static struct dma_async_tx_descriptor
*
515 mv_xor_prep_dma_xor(struct dma_chan
*chan
, dma_addr_t dest
, dma_addr_t
*src
,
516 unsigned int src_cnt
, size_t len
, unsigned long flags
)
518 struct mv_xor_chan
*mv_chan
= to_mv_xor_chan(chan
);
519 struct mv_xor_desc_slot
*sw_desc
;
521 if (unlikely(len
< MV_XOR_MIN_BYTE_COUNT
))
524 BUG_ON(len
> MV_XOR_MAX_BYTE_COUNT
);
526 dev_dbg(mv_chan_to_devp(mv_chan
),
527 "%s src_cnt: %d len: %u dest %pad flags: %ld\n",
528 __func__
, src_cnt
, len
, &dest
, flags
);
530 spin_lock_bh(&mv_chan
->lock
);
531 sw_desc
= mv_xor_alloc_slot(mv_chan
);
533 sw_desc
->type
= DMA_XOR
;
534 sw_desc
->async_tx
.flags
= flags
;
535 mv_desc_init(sw_desc
, dest
, len
, flags
);
537 mv_desc_set_src_addr(sw_desc
, src_cnt
, src
[src_cnt
]);
539 spin_unlock_bh(&mv_chan
->lock
);
540 dev_dbg(mv_chan_to_devp(mv_chan
),
541 "%s sw_desc %p async_tx %p \n",
542 __func__
, sw_desc
, &sw_desc
->async_tx
);
543 return sw_desc
? &sw_desc
->async_tx
: NULL
;
546 static struct dma_async_tx_descriptor
*
547 mv_xor_prep_dma_memcpy(struct dma_chan
*chan
, dma_addr_t dest
, dma_addr_t src
,
548 size_t len
, unsigned long flags
)
551 * A MEMCPY operation is identical to an XOR operation with only
552 * a single source address.
554 return mv_xor_prep_dma_xor(chan
, dest
, &src
, 1, len
, flags
);
557 static struct dma_async_tx_descriptor
*
558 mv_xor_prep_dma_interrupt(struct dma_chan
*chan
, unsigned long flags
)
560 struct mv_xor_chan
*mv_chan
= to_mv_xor_chan(chan
);
561 dma_addr_t src
, dest
;
564 src
= mv_chan
->dummy_src_addr
;
565 dest
= mv_chan
->dummy_dst_addr
;
566 len
= MV_XOR_MIN_BYTE_COUNT
;
569 * We implement the DMA_INTERRUPT operation as a minimum sized
570 * XOR operation with a single dummy source address.
572 return mv_xor_prep_dma_xor(chan
, dest
, &src
, 1, len
, flags
);
575 static void mv_xor_free_chan_resources(struct dma_chan
*chan
)
577 struct mv_xor_chan
*mv_chan
= to_mv_xor_chan(chan
);
578 struct mv_xor_desc_slot
*iter
, *_iter
;
579 int in_use_descs
= 0;
581 spin_lock_bh(&mv_chan
->lock
);
583 mv_xor_slot_cleanup(mv_chan
);
585 list_for_each_entry_safe(iter
, _iter
, &mv_chan
->chain
,
588 list_del(&iter
->chain_node
);
590 list_for_each_entry_safe(iter
, _iter
, &mv_chan
->completed_slots
,
593 list_del(&iter
->completed_node
);
595 list_for_each_entry_safe_reverse(
596 iter
, _iter
, &mv_chan
->all_slots
, slot_node
) {
597 list_del(&iter
->slot_node
);
599 mv_chan
->slots_allocated
--;
601 mv_chan
->last_used
= NULL
;
603 dev_dbg(mv_chan_to_devp(mv_chan
), "%s slots_allocated %d\n",
604 __func__
, mv_chan
->slots_allocated
);
605 spin_unlock_bh(&mv_chan
->lock
);
608 dev_err(mv_chan_to_devp(mv_chan
),
609 "freeing %d in use descriptors!\n", in_use_descs
);
613 * mv_xor_status - poll the status of an XOR transaction
614 * @chan: XOR channel handle
615 * @cookie: XOR transaction identifier
616 * @txstate: XOR transactions state holder (or NULL)
618 static enum dma_status
mv_xor_status(struct dma_chan
*chan
,
620 struct dma_tx_state
*txstate
)
622 struct mv_xor_chan
*mv_chan
= to_mv_xor_chan(chan
);
625 ret
= dma_cookie_status(chan
, cookie
, txstate
);
626 if (ret
== DMA_COMPLETE
)
629 spin_lock_bh(&mv_chan
->lock
);
630 mv_xor_slot_cleanup(mv_chan
);
631 spin_unlock_bh(&mv_chan
->lock
);
633 return dma_cookie_status(chan
, cookie
, txstate
);
636 static void mv_dump_xor_regs(struct mv_xor_chan
*chan
)
640 val
= readl_relaxed(XOR_CONFIG(chan
));
641 dev_err(mv_chan_to_devp(chan
), "config 0x%08x\n", val
);
643 val
= readl_relaxed(XOR_ACTIVATION(chan
));
644 dev_err(mv_chan_to_devp(chan
), "activation 0x%08x\n", val
);
646 val
= readl_relaxed(XOR_INTR_CAUSE(chan
));
647 dev_err(mv_chan_to_devp(chan
), "intr cause 0x%08x\n", val
);
649 val
= readl_relaxed(XOR_INTR_MASK(chan
));
650 dev_err(mv_chan_to_devp(chan
), "intr mask 0x%08x\n", val
);
652 val
= readl_relaxed(XOR_ERROR_CAUSE(chan
));
653 dev_err(mv_chan_to_devp(chan
), "error cause 0x%08x\n", val
);
655 val
= readl_relaxed(XOR_ERROR_ADDR(chan
));
656 dev_err(mv_chan_to_devp(chan
), "error addr 0x%08x\n", val
);
659 static void mv_xor_err_interrupt_handler(struct mv_xor_chan
*chan
,
662 if (intr_cause
& XOR_INT_ERR_DECODE
) {
663 dev_dbg(mv_chan_to_devp(chan
), "ignoring address decode error\n");
667 dev_err(mv_chan_to_devp(chan
), "error on chan %d. intr cause 0x%08x\n",
668 chan
->idx
, intr_cause
);
670 mv_dump_xor_regs(chan
);
674 static irqreturn_t
mv_xor_interrupt_handler(int irq
, void *data
)
676 struct mv_xor_chan
*chan
= data
;
677 u32 intr_cause
= mv_chan_get_intr_cause(chan
);
679 dev_dbg(mv_chan_to_devp(chan
), "intr cause %x\n", intr_cause
);
681 if (intr_cause
& XOR_INTR_ERRORS
)
682 mv_xor_err_interrupt_handler(chan
, intr_cause
);
684 tasklet_schedule(&chan
->irq_tasklet
);
686 mv_xor_device_clear_eoc_cause(chan
);
691 static void mv_xor_issue_pending(struct dma_chan
*chan
)
693 struct mv_xor_chan
*mv_chan
= to_mv_xor_chan(chan
);
695 if (mv_chan
->pending
>= MV_XOR_THRESHOLD
) {
696 mv_chan
->pending
= 0;
697 mv_chan_activate(mv_chan
);
702 * Perform a transaction to verify the HW works.
705 static int mv_xor_memcpy_self_test(struct mv_xor_chan
*mv_chan
)
709 dma_addr_t src_dma
, dest_dma
;
710 struct dma_chan
*dma_chan
;
712 struct dma_async_tx_descriptor
*tx
;
713 struct dmaengine_unmap_data
*unmap
;
716 src
= kmalloc(sizeof(u8
) * PAGE_SIZE
, GFP_KERNEL
);
720 dest
= kzalloc(sizeof(u8
) * PAGE_SIZE
, GFP_KERNEL
);
726 /* Fill in src buffer */
727 for (i
= 0; i
< PAGE_SIZE
; i
++)
728 ((u8
*) src
)[i
] = (u8
)i
;
730 dma_chan
= &mv_chan
->dmachan
;
731 if (mv_xor_alloc_chan_resources(dma_chan
) < 1) {
736 unmap
= dmaengine_get_unmap_data(dma_chan
->device
->dev
, 2, GFP_KERNEL
);
742 src_dma
= dma_map_page(dma_chan
->device
->dev
, virt_to_page(src
), 0,
743 PAGE_SIZE
, DMA_TO_DEVICE
);
744 unmap
->addr
[0] = src_dma
;
746 ret
= dma_mapping_error(dma_chan
->device
->dev
, src_dma
);
753 dest_dma
= dma_map_page(dma_chan
->device
->dev
, virt_to_page(dest
), 0,
754 PAGE_SIZE
, DMA_FROM_DEVICE
);
755 unmap
->addr
[1] = dest_dma
;
757 ret
= dma_mapping_error(dma_chan
->device
->dev
, dest_dma
);
763 unmap
->len
= PAGE_SIZE
;
765 tx
= mv_xor_prep_dma_memcpy(dma_chan
, dest_dma
, src_dma
,
768 dev_err(dma_chan
->device
->dev
,
769 "Self-test cannot prepare operation, disabling\n");
774 cookie
= mv_xor_tx_submit(tx
);
775 if (dma_submit_error(cookie
)) {
776 dev_err(dma_chan
->device
->dev
,
777 "Self-test submit error, disabling\n");
782 mv_xor_issue_pending(dma_chan
);
786 if (mv_xor_status(dma_chan
, cookie
, NULL
) !=
788 dev_err(dma_chan
->device
->dev
,
789 "Self-test copy timed out, disabling\n");
794 dma_sync_single_for_cpu(dma_chan
->device
->dev
, dest_dma
,
795 PAGE_SIZE
, DMA_FROM_DEVICE
);
796 if (memcmp(src
, dest
, PAGE_SIZE
)) {
797 dev_err(dma_chan
->device
->dev
,
798 "Self-test copy failed compare, disabling\n");
804 dmaengine_unmap_put(unmap
);
805 mv_xor_free_chan_resources(dma_chan
);
812 #define MV_XOR_NUM_SRC_TEST 4 /* must be <= 15 */
814 mv_xor_xor_self_test(struct mv_xor_chan
*mv_chan
)
818 struct page
*xor_srcs
[MV_XOR_NUM_SRC_TEST
];
819 dma_addr_t dma_srcs
[MV_XOR_NUM_SRC_TEST
];
821 struct dma_async_tx_descriptor
*tx
;
822 struct dmaengine_unmap_data
*unmap
;
823 struct dma_chan
*dma_chan
;
828 int src_count
= MV_XOR_NUM_SRC_TEST
;
830 for (src_idx
= 0; src_idx
< src_count
; src_idx
++) {
831 xor_srcs
[src_idx
] = alloc_page(GFP_KERNEL
);
832 if (!xor_srcs
[src_idx
]) {
834 __free_page(xor_srcs
[src_idx
]);
839 dest
= alloc_page(GFP_KERNEL
);
842 __free_page(xor_srcs
[src_idx
]);
846 /* Fill in src buffers */
847 for (src_idx
= 0; src_idx
< src_count
; src_idx
++) {
848 u8
*ptr
= page_address(xor_srcs
[src_idx
]);
849 for (i
= 0; i
< PAGE_SIZE
; i
++)
850 ptr
[i
] = (1 << src_idx
);
853 for (src_idx
= 0; src_idx
< src_count
; src_idx
++)
854 cmp_byte
^= (u8
) (1 << src_idx
);
856 cmp_word
= (cmp_byte
<< 24) | (cmp_byte
<< 16) |
857 (cmp_byte
<< 8) | cmp_byte
;
859 memset(page_address(dest
), 0, PAGE_SIZE
);
861 dma_chan
= &mv_chan
->dmachan
;
862 if (mv_xor_alloc_chan_resources(dma_chan
) < 1) {
867 unmap
= dmaengine_get_unmap_data(dma_chan
->device
->dev
, src_count
+ 1,
875 for (i
= 0; i
< src_count
; i
++) {
876 unmap
->addr
[i
] = dma_map_page(dma_chan
->device
->dev
, xor_srcs
[i
],
877 0, PAGE_SIZE
, DMA_TO_DEVICE
);
878 dma_srcs
[i
] = unmap
->addr
[i
];
879 ret
= dma_mapping_error(dma_chan
->device
->dev
, unmap
->addr
[i
]);
887 unmap
->addr
[src_count
] = dma_map_page(dma_chan
->device
->dev
, dest
, 0, PAGE_SIZE
,
889 dest_dma
= unmap
->addr
[src_count
];
890 ret
= dma_mapping_error(dma_chan
->device
->dev
, unmap
->addr
[src_count
]);
896 unmap
->len
= PAGE_SIZE
;
898 tx
= mv_xor_prep_dma_xor(dma_chan
, dest_dma
, dma_srcs
,
899 src_count
, PAGE_SIZE
, 0);
901 dev_err(dma_chan
->device
->dev
,
902 "Self-test cannot prepare operation, disabling\n");
907 cookie
= mv_xor_tx_submit(tx
);
908 if (dma_submit_error(cookie
)) {
909 dev_err(dma_chan
->device
->dev
,
910 "Self-test submit error, disabling\n");
915 mv_xor_issue_pending(dma_chan
);
919 if (mv_xor_status(dma_chan
, cookie
, NULL
) !=
921 dev_err(dma_chan
->device
->dev
,
922 "Self-test xor timed out, disabling\n");
927 dma_sync_single_for_cpu(dma_chan
->device
->dev
, dest_dma
,
928 PAGE_SIZE
, DMA_FROM_DEVICE
);
929 for (i
= 0; i
< (PAGE_SIZE
/ sizeof(u32
)); i
++) {
930 u32
*ptr
= page_address(dest
);
931 if (ptr
[i
] != cmp_word
) {
932 dev_err(dma_chan
->device
->dev
,
933 "Self-test xor failed compare, disabling. index %d, data %x, expected %x\n",
934 i
, ptr
[i
], cmp_word
);
941 dmaengine_unmap_put(unmap
);
942 mv_xor_free_chan_resources(dma_chan
);
946 __free_page(xor_srcs
[src_idx
]);
951 /* This driver does not implement any of the optional DMA operations. */
953 mv_xor_control(struct dma_chan
*chan
, enum dma_ctrl_cmd cmd
,
959 static int mv_xor_channel_remove(struct mv_xor_chan
*mv_chan
)
961 struct dma_chan
*chan
, *_chan
;
962 struct device
*dev
= mv_chan
->dmadev
.dev
;
964 dma_async_device_unregister(&mv_chan
->dmadev
);
966 dma_free_coherent(dev
, MV_XOR_POOL_SIZE
,
967 mv_chan
->dma_desc_pool_virt
, mv_chan
->dma_desc_pool
);
968 dma_unmap_single(dev
, mv_chan
->dummy_src_addr
,
969 MV_XOR_MIN_BYTE_COUNT
, DMA_FROM_DEVICE
);
970 dma_unmap_single(dev
, mv_chan
->dummy_dst_addr
,
971 MV_XOR_MIN_BYTE_COUNT
, DMA_TO_DEVICE
);
973 list_for_each_entry_safe(chan
, _chan
, &mv_chan
->dmadev
.channels
,
975 list_del(&chan
->device_node
);
978 free_irq(mv_chan
->irq
, mv_chan
);
983 static struct mv_xor_chan
*
984 mv_xor_channel_add(struct mv_xor_device
*xordev
,
985 struct platform_device
*pdev
,
986 int idx
, dma_cap_mask_t cap_mask
, int irq
)
989 struct mv_xor_chan
*mv_chan
;
990 struct dma_device
*dma_dev
;
992 mv_chan
= devm_kzalloc(&pdev
->dev
, sizeof(*mv_chan
), GFP_KERNEL
);
994 return ERR_PTR(-ENOMEM
);
999 dma_dev
= &mv_chan
->dmadev
;
1002 * These source and destination dummy buffers are used to implement
1003 * a DMA_INTERRUPT operation as a minimum-sized XOR operation.
1004 * Hence, we only need to map the buffers at initialization-time.
1006 mv_chan
->dummy_src_addr
= dma_map_single(dma_dev
->dev
,
1007 mv_chan
->dummy_src
, MV_XOR_MIN_BYTE_COUNT
, DMA_FROM_DEVICE
);
1008 mv_chan
->dummy_dst_addr
= dma_map_single(dma_dev
->dev
,
1009 mv_chan
->dummy_dst
, MV_XOR_MIN_BYTE_COUNT
, DMA_TO_DEVICE
);
1011 /* allocate coherent memory for hardware descriptors
1012 * note: writecombine gives slightly better performance, but
1013 * requires that we explicitly flush the writes
1015 mv_chan
->dma_desc_pool_virt
=
1016 dma_alloc_writecombine(&pdev
->dev
, MV_XOR_POOL_SIZE
,
1017 &mv_chan
->dma_desc_pool
, GFP_KERNEL
);
1018 if (!mv_chan
->dma_desc_pool_virt
)
1019 return ERR_PTR(-ENOMEM
);
1021 /* discover transaction capabilites from the platform data */
1022 dma_dev
->cap_mask
= cap_mask
;
1024 INIT_LIST_HEAD(&dma_dev
->channels
);
1026 /* set base routines */
1027 dma_dev
->device_alloc_chan_resources
= mv_xor_alloc_chan_resources
;
1028 dma_dev
->device_free_chan_resources
= mv_xor_free_chan_resources
;
1029 dma_dev
->device_tx_status
= mv_xor_status
;
1030 dma_dev
->device_issue_pending
= mv_xor_issue_pending
;
1031 dma_dev
->device_control
= mv_xor_control
;
1032 dma_dev
->dev
= &pdev
->dev
;
1034 /* set prep routines based on capability */
1035 if (dma_has_cap(DMA_INTERRUPT
, dma_dev
->cap_mask
))
1036 dma_dev
->device_prep_dma_interrupt
= mv_xor_prep_dma_interrupt
;
1037 if (dma_has_cap(DMA_MEMCPY
, dma_dev
->cap_mask
))
1038 dma_dev
->device_prep_dma_memcpy
= mv_xor_prep_dma_memcpy
;
1039 if (dma_has_cap(DMA_XOR
, dma_dev
->cap_mask
)) {
1040 dma_dev
->max_xor
= 8;
1041 dma_dev
->device_prep_dma_xor
= mv_xor_prep_dma_xor
;
1044 mv_chan
->mmr_base
= xordev
->xor_base
;
1045 mv_chan
->mmr_high_base
= xordev
->xor_high_base
;
1046 tasklet_init(&mv_chan
->irq_tasklet
, mv_xor_tasklet
, (unsigned long)
1049 /* clear errors before enabling interrupts */
1050 mv_xor_device_clear_err_status(mv_chan
);
1052 ret
= request_irq(mv_chan
->irq
, mv_xor_interrupt_handler
,
1053 0, dev_name(&pdev
->dev
), mv_chan
);
1057 mv_chan_unmask_interrupts(mv_chan
);
1059 mv_set_mode(mv_chan
, DMA_XOR
);
1061 spin_lock_init(&mv_chan
->lock
);
1062 INIT_LIST_HEAD(&mv_chan
->chain
);
1063 INIT_LIST_HEAD(&mv_chan
->completed_slots
);
1064 INIT_LIST_HEAD(&mv_chan
->all_slots
);
1065 mv_chan
->dmachan
.device
= dma_dev
;
1066 dma_cookie_init(&mv_chan
->dmachan
);
1068 list_add_tail(&mv_chan
->dmachan
.device_node
, &dma_dev
->channels
);
1070 if (dma_has_cap(DMA_MEMCPY
, dma_dev
->cap_mask
)) {
1071 ret
= mv_xor_memcpy_self_test(mv_chan
);
1072 dev_dbg(&pdev
->dev
, "memcpy self test returned %d\n", ret
);
1077 if (dma_has_cap(DMA_XOR
, dma_dev
->cap_mask
)) {
1078 ret
= mv_xor_xor_self_test(mv_chan
);
1079 dev_dbg(&pdev
->dev
, "xor self test returned %d\n", ret
);
1084 dev_info(&pdev
->dev
, "Marvell XOR: ( %s%s%s)\n",
1085 dma_has_cap(DMA_XOR
, dma_dev
->cap_mask
) ? "xor " : "",
1086 dma_has_cap(DMA_MEMCPY
, dma_dev
->cap_mask
) ? "cpy " : "",
1087 dma_has_cap(DMA_INTERRUPT
, dma_dev
->cap_mask
) ? "intr " : "");
1089 dma_async_device_register(dma_dev
);
1093 free_irq(mv_chan
->irq
, mv_chan
);
1095 dma_free_coherent(&pdev
->dev
, MV_XOR_POOL_SIZE
,
1096 mv_chan
->dma_desc_pool_virt
, mv_chan
->dma_desc_pool
);
1097 return ERR_PTR(ret
);
1101 mv_xor_conf_mbus_windows(struct mv_xor_device
*xordev
,
1102 const struct mbus_dram_target_info
*dram
)
1104 void __iomem
*base
= xordev
->xor_high_base
;
1108 for (i
= 0; i
< 8; i
++) {
1109 writel(0, base
+ WINDOW_BASE(i
));
1110 writel(0, base
+ WINDOW_SIZE(i
));
1112 writel(0, base
+ WINDOW_REMAP_HIGH(i
));
1115 for (i
= 0; i
< dram
->num_cs
; i
++) {
1116 const struct mbus_dram_window
*cs
= dram
->cs
+ i
;
1118 writel((cs
->base
& 0xffff0000) |
1119 (cs
->mbus_attr
<< 8) |
1120 dram
->mbus_dram_target_id
, base
+ WINDOW_BASE(i
));
1121 writel((cs
->size
- 1) & 0xffff0000, base
+ WINDOW_SIZE(i
));
1123 win_enable
|= (1 << i
);
1124 win_enable
|= 3 << (16 + (2 * i
));
1127 writel(win_enable
, base
+ WINDOW_BAR_ENABLE(0));
1128 writel(win_enable
, base
+ WINDOW_BAR_ENABLE(1));
1129 writel(0, base
+ WINDOW_OVERRIDE_CTRL(0));
1130 writel(0, base
+ WINDOW_OVERRIDE_CTRL(1));
1133 static int mv_xor_probe(struct platform_device
*pdev
)
1135 const struct mbus_dram_target_info
*dram
;
1136 struct mv_xor_device
*xordev
;
1137 struct mv_xor_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
1138 struct resource
*res
;
1141 dev_notice(&pdev
->dev
, "Marvell shared XOR driver\n");
1143 xordev
= devm_kzalloc(&pdev
->dev
, sizeof(*xordev
), GFP_KERNEL
);
1147 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1151 xordev
->xor_base
= devm_ioremap(&pdev
->dev
, res
->start
,
1152 resource_size(res
));
1153 if (!xordev
->xor_base
)
1156 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
1160 xordev
->xor_high_base
= devm_ioremap(&pdev
->dev
, res
->start
,
1161 resource_size(res
));
1162 if (!xordev
->xor_high_base
)
1165 platform_set_drvdata(pdev
, xordev
);
1168 * (Re-)program MBUS remapping windows if we are asked to.
1170 dram
= mv_mbus_dram_info();
1172 mv_xor_conf_mbus_windows(xordev
, dram
);
1174 /* Not all platforms can gate the clock, so it is not
1175 * an error if the clock does not exists.
1177 xordev
->clk
= clk_get(&pdev
->dev
, NULL
);
1178 if (!IS_ERR(xordev
->clk
))
1179 clk_prepare_enable(xordev
->clk
);
1181 if (pdev
->dev
.of_node
) {
1182 struct device_node
*np
;
1185 for_each_child_of_node(pdev
->dev
.of_node
, np
) {
1186 struct mv_xor_chan
*chan
;
1187 dma_cap_mask_t cap_mask
;
1190 dma_cap_zero(cap_mask
);
1191 if (of_property_read_bool(np
, "dmacap,memcpy"))
1192 dma_cap_set(DMA_MEMCPY
, cap_mask
);
1193 if (of_property_read_bool(np
, "dmacap,xor"))
1194 dma_cap_set(DMA_XOR
, cap_mask
);
1195 if (of_property_read_bool(np
, "dmacap,interrupt"))
1196 dma_cap_set(DMA_INTERRUPT
, cap_mask
);
1198 irq
= irq_of_parse_and_map(np
, 0);
1201 goto err_channel_add
;
1204 chan
= mv_xor_channel_add(xordev
, pdev
, i
,
1207 ret
= PTR_ERR(chan
);
1208 irq_dispose_mapping(irq
);
1209 goto err_channel_add
;
1212 xordev
->channels
[i
] = chan
;
1215 } else if (pdata
&& pdata
->channels
) {
1216 for (i
= 0; i
< MV_XOR_MAX_CHANNELS
; i
++) {
1217 struct mv_xor_channel_data
*cd
;
1218 struct mv_xor_chan
*chan
;
1221 cd
= &pdata
->channels
[i
];
1224 goto err_channel_add
;
1227 irq
= platform_get_irq(pdev
, i
);
1230 goto err_channel_add
;
1233 chan
= mv_xor_channel_add(xordev
, pdev
, i
,
1236 ret
= PTR_ERR(chan
);
1237 goto err_channel_add
;
1240 xordev
->channels
[i
] = chan
;
1247 for (i
= 0; i
< MV_XOR_MAX_CHANNELS
; i
++)
1248 if (xordev
->channels
[i
]) {
1249 mv_xor_channel_remove(xordev
->channels
[i
]);
1250 if (pdev
->dev
.of_node
)
1251 irq_dispose_mapping(xordev
->channels
[i
]->irq
);
1254 if (!IS_ERR(xordev
->clk
)) {
1255 clk_disable_unprepare(xordev
->clk
);
1256 clk_put(xordev
->clk
);
1262 static int mv_xor_remove(struct platform_device
*pdev
)
1264 struct mv_xor_device
*xordev
= platform_get_drvdata(pdev
);
1267 for (i
= 0; i
< MV_XOR_MAX_CHANNELS
; i
++) {
1268 if (xordev
->channels
[i
])
1269 mv_xor_channel_remove(xordev
->channels
[i
]);
1272 if (!IS_ERR(xordev
->clk
)) {
1273 clk_disable_unprepare(xordev
->clk
);
1274 clk_put(xordev
->clk
);
1281 static struct of_device_id mv_xor_dt_ids
[] = {
1282 { .compatible
= "marvell,orion-xor", },
1285 MODULE_DEVICE_TABLE(of
, mv_xor_dt_ids
);
1288 static struct platform_driver mv_xor_driver
= {
1289 .probe
= mv_xor_probe
,
1290 .remove
= mv_xor_remove
,
1292 .owner
= THIS_MODULE
,
1293 .name
= MV_XOR_NAME
,
1294 .of_match_table
= of_match_ptr(mv_xor_dt_ids
),
1299 static int __init
mv_xor_init(void)
1301 return platform_driver_register(&mv_xor_driver
);
1303 module_init(mv_xor_init
);
1305 /* it's currently unsafe to unload this module */
1307 static void __exit
mv_xor_exit(void)
1309 platform_driver_unregister(&mv_xor_driver
);
1313 module_exit(mv_xor_exit
);
1316 MODULE_AUTHOR("Saeed Bishara <saeed@marvell.com>");
1317 MODULE_DESCRIPTION("DMA engine driver for Marvell's XOR engine");
1318 MODULE_LICENSE("GPL");