Revert "genirq: Use irqd_get_trigger_type to compare the trigger type for shared...
[linux/fpc-iii.git] / drivers / dma / mv_xor.c
blobede8e868016d6d558d063ef7b6333a4ef5de3c79
1 /*
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
12 * more details.
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>
29 #include <linux/of.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"
35 #include "mv_xor.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) \
46 ((chan)->dmadev.dev)
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,
64 u32 next_desc_addr)
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,
92 u32 next_desc_addr)
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;
108 return intr_cause;
111 static void mv_xor_device_clear_eoc_cause(struct mv_xor_chan *chan)
113 u32 val;
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)
130 u32 op_mode;
131 u32 config = readl_relaxed(XOR_CONFIG(chan));
133 switch (type) {
134 case DMA_XOR:
135 op_mode = XOR_OPERATION_MODE_XOR;
136 break;
137 case DMA_MEMCPY:
138 op_mode = XOR_OPERATION_MODE_MEMCPY;
139 break;
140 default:
141 dev_err(mv_chan_to_devp(chan),
142 "error: unsupported operation %d\n",
143 type);
144 BUG();
145 return;
148 config &= ~0x7;
149 config |= op_mode;
151 #if defined(__BIG_ENDIAN)
152 config |= XOR_DESCRIPTOR_SWAP;
153 #else
154 config &= ~XOR_DESCRIPTOR_SWAP;
155 #endif
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);
189 slot->slot_used = 0;
194 * mv_xor_start_new_chain - program the engine to operate on new chain headed by
195 * sw_desc
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);
207 mv_chan->pending++;
208 mv_xor_issue_pending(&mv_chan->dmachan);
211 static dma_cookie_t
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);
233 return cookie;
236 static int
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,
243 completed_node) {
245 if (async_tx_test_ack(&iter->async_tx)) {
246 list_del(&iter->completed_node);
247 mv_xor_free_slots(mv_chan, iter);
250 return 0;
253 static int
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
261 * until 'ack' is set
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);
266 return 0;
269 mv_xor_free_slots(mv_chan, desc);
270 return 0;
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,
292 chain_node) {
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,
298 cookie);
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) {
305 current_cleaned = 1;
306 break;
308 } else {
309 if (iter->async_tx.phys == current_desc) {
310 current_cleaned = 0;
311 break;
316 if ((busy == 0) && !list_empty(&mv_chan->chain)) {
317 if (current_cleaned) {
319 * current descriptor cleaned and removed, run
320 * from list head
322 iter = list_entry(mv_chan->chain.next,
323 struct mv_xor_desc_slot,
324 chain_node);
325 mv_xor_start_new_chain(mv_chan, iter);
326 } else {
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,
334 chain_node);
335 mv_xor_start_new_chain(mv_chan, iter);
336 } else {
338 * some descriptors are still waiting
339 * to be cleaned
341 tasklet_schedule(&mv_chan->irq_tasklet);
346 if (cookie > 0)
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;
363 int retry = 0;
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
369 retry:
370 if (retry == 0)
371 iter = mv_chan->last_used;
372 else
373 iter = list_entry(&mv_chan->all_slots,
374 struct mv_xor_desc_slot,
375 slot_node);
377 list_for_each_entry_safe_continue(
378 iter, _iter, &mv_chan->all_slots, slot_node) {
380 prefetch(_iter);
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
386 if (retry)
387 break;
388 continue;
391 /* pre-ack descriptor */
392 async_tx_ack(&iter->async_tx);
394 iter->slot_used = 1;
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);
400 return iter;
403 if (!retry++)
404 goto retry;
406 /* try to free some slots if the allocation fails */
407 tasklet_schedule(&mv_chan->irq_tasklet);
409 return NULL;
412 /************************ DMA engine API functions ****************************/
413 static dma_cookie_t
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;
419 dma_cookie_t cookie;
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);
431 else {
432 new_hw_chain = 0;
434 old_chain_tail = list_entry(mv_chan->chain.prev,
435 struct mv_xor_desc_slot,
436 chain_node);
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)
453 new_hw_chain = 1;
457 if (new_hw_chain)
458 mv_xor_start_new_chain(mv_chan, sw_desc);
460 spin_unlock_bh(&mv_chan->lock);
462 return cookie;
465 /* returns the number of allocated descriptors */
466 static int mv_xor_alloc_chan_resources(struct dma_chan *chan)
468 void *virt_desc;
469 dma_addr_t dma_desc;
470 int idx;
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);
479 if (!slot) {
480 dev_info(mv_chan_to_devp(mv_chan),
481 "channel only initialized %d descriptor slots",
482 idx);
483 break;
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;
494 slot->idx = idx++;
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,
505 slot_node);
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))
522 return NULL;
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);
532 if (sw_desc) {
533 sw_desc->type = DMA_XOR;
534 sw_desc->async_tx.flags = flags;
535 mv_desc_init(sw_desc, dest, len, flags);
536 while (src_cnt--)
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;
562 size_t len;
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,
586 chain_node) {
587 in_use_descs++;
588 list_del(&iter->chain_node);
590 list_for_each_entry_safe(iter, _iter, &mv_chan->completed_slots,
591 completed_node) {
592 in_use_descs++;
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);
598 kfree(iter);
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);
607 if (in_use_descs)
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,
619 dma_cookie_t cookie,
620 struct dma_tx_state *txstate)
622 struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
623 enum dma_status ret;
625 ret = dma_cookie_status(chan, cookie, txstate);
626 if (ret == DMA_COMPLETE)
627 return ret;
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)
638 u32 val;
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,
660 u32 intr_cause)
662 if (intr_cause & XOR_INT_ERR_DECODE) {
663 dev_dbg(mv_chan_to_devp(chan), "ignoring address decode error\n");
664 return;
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);
671 WARN_ON(1);
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);
688 return IRQ_HANDLED;
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)
707 int i, ret;
708 void *src, *dest;
709 dma_addr_t src_dma, dest_dma;
710 struct dma_chan *dma_chan;
711 dma_cookie_t cookie;
712 struct dma_async_tx_descriptor *tx;
713 struct dmaengine_unmap_data *unmap;
714 int err = 0;
716 src = kmalloc(sizeof(u8) * PAGE_SIZE, GFP_KERNEL);
717 if (!src)
718 return -ENOMEM;
720 dest = kzalloc(sizeof(u8) * PAGE_SIZE, GFP_KERNEL);
721 if (!dest) {
722 kfree(src);
723 return -ENOMEM;
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) {
732 err = -ENODEV;
733 goto out;
736 unmap = dmaengine_get_unmap_data(dma_chan->device->dev, 2, GFP_KERNEL);
737 if (!unmap) {
738 err = -ENOMEM;
739 goto free_resources;
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);
747 if (ret) {
748 err = -ENOMEM;
749 goto free_resources;
751 unmap->to_cnt = 1;
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);
758 if (ret) {
759 err = -ENOMEM;
760 goto free_resources;
762 unmap->from_cnt = 1;
763 unmap->len = PAGE_SIZE;
765 tx = mv_xor_prep_dma_memcpy(dma_chan, dest_dma, src_dma,
766 PAGE_SIZE, 0);
767 if (!tx) {
768 dev_err(dma_chan->device->dev,
769 "Self-test cannot prepare operation, disabling\n");
770 err = -ENODEV;
771 goto free_resources;
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");
778 err = -ENODEV;
779 goto free_resources;
782 mv_xor_issue_pending(dma_chan);
783 async_tx_ack(tx);
784 msleep(1);
786 if (mv_xor_status(dma_chan, cookie, NULL) !=
787 DMA_COMPLETE) {
788 dev_err(dma_chan->device->dev,
789 "Self-test copy timed out, disabling\n");
790 err = -ENODEV;
791 goto free_resources;
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");
799 err = -ENODEV;
800 goto free_resources;
803 free_resources:
804 dmaengine_unmap_put(unmap);
805 mv_xor_free_chan_resources(dma_chan);
806 out:
807 kfree(src);
808 kfree(dest);
809 return err;
812 #define MV_XOR_NUM_SRC_TEST 4 /* must be <= 15 */
813 static int
814 mv_xor_xor_self_test(struct mv_xor_chan *mv_chan)
816 int i, src_idx, ret;
817 struct page *dest;
818 struct page *xor_srcs[MV_XOR_NUM_SRC_TEST];
819 dma_addr_t dma_srcs[MV_XOR_NUM_SRC_TEST];
820 dma_addr_t dest_dma;
821 struct dma_async_tx_descriptor *tx;
822 struct dmaengine_unmap_data *unmap;
823 struct dma_chan *dma_chan;
824 dma_cookie_t cookie;
825 u8 cmp_byte = 0;
826 u32 cmp_word;
827 int err = 0;
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]) {
833 while (src_idx--)
834 __free_page(xor_srcs[src_idx]);
835 return -ENOMEM;
839 dest = alloc_page(GFP_KERNEL);
840 if (!dest) {
841 while (src_idx--)
842 __free_page(xor_srcs[src_idx]);
843 return -ENOMEM;
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) {
863 err = -ENODEV;
864 goto out;
867 unmap = dmaengine_get_unmap_data(dma_chan->device->dev, src_count + 1,
868 GFP_KERNEL);
869 if (!unmap) {
870 err = -ENOMEM;
871 goto free_resources;
874 /* test xor */
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]);
880 if (ret) {
881 err = -ENOMEM;
882 goto free_resources;
884 unmap->to_cnt++;
887 unmap->addr[src_count] = dma_map_page(dma_chan->device->dev, dest, 0, PAGE_SIZE,
888 DMA_FROM_DEVICE);
889 dest_dma = unmap->addr[src_count];
890 ret = dma_mapping_error(dma_chan->device->dev, unmap->addr[src_count]);
891 if (ret) {
892 err = -ENOMEM;
893 goto free_resources;
895 unmap->from_cnt = 1;
896 unmap->len = PAGE_SIZE;
898 tx = mv_xor_prep_dma_xor(dma_chan, dest_dma, dma_srcs,
899 src_count, PAGE_SIZE, 0);
900 if (!tx) {
901 dev_err(dma_chan->device->dev,
902 "Self-test cannot prepare operation, disabling\n");
903 err = -ENODEV;
904 goto free_resources;
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");
911 err = -ENODEV;
912 goto free_resources;
915 mv_xor_issue_pending(dma_chan);
916 async_tx_ack(tx);
917 msleep(8);
919 if (mv_xor_status(dma_chan, cookie, NULL) !=
920 DMA_COMPLETE) {
921 dev_err(dma_chan->device->dev,
922 "Self-test xor timed out, disabling\n");
923 err = -ENODEV;
924 goto free_resources;
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);
935 err = -ENODEV;
936 goto free_resources;
940 free_resources:
941 dmaengine_unmap_put(unmap);
942 mv_xor_free_chan_resources(dma_chan);
943 out:
944 src_idx = src_count;
945 while (src_idx--)
946 __free_page(xor_srcs[src_idx]);
947 __free_page(dest);
948 return err;
951 /* This driver does not implement any of the optional DMA operations. */
952 static int
953 mv_xor_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
954 unsigned long arg)
956 return -ENOSYS;
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,
974 device_node) {
975 list_del(&chan->device_node);
978 free_irq(mv_chan->irq, mv_chan);
980 return 0;
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)
988 int ret = 0;
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);
993 if (!mv_chan)
994 return ERR_PTR(-ENOMEM);
996 mv_chan->idx = idx;
997 mv_chan->irq = irq;
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)
1047 mv_chan);
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);
1054 if (ret)
1055 goto err_free_dma;
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);
1073 if (ret)
1074 goto err_free_irq;
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);
1080 if (ret)
1081 goto err_free_irq;
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);
1090 return mv_chan;
1092 err_free_irq:
1093 free_irq(mv_chan->irq, mv_chan);
1094 err_free_dma:
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);
1100 static void
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;
1105 u32 win_enable = 0;
1106 int i;
1108 for (i = 0; i < 8; i++) {
1109 writel(0, base + WINDOW_BASE(i));
1110 writel(0, base + WINDOW_SIZE(i));
1111 if (i < 4)
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;
1139 int i, ret;
1141 dev_notice(&pdev->dev, "Marvell shared XOR driver\n");
1143 xordev = devm_kzalloc(&pdev->dev, sizeof(*xordev), GFP_KERNEL);
1144 if (!xordev)
1145 return -ENOMEM;
1147 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1148 if (!res)
1149 return -ENODEV;
1151 xordev->xor_base = devm_ioremap(&pdev->dev, res->start,
1152 resource_size(res));
1153 if (!xordev->xor_base)
1154 return -EBUSY;
1156 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1157 if (!res)
1158 return -ENODEV;
1160 xordev->xor_high_base = devm_ioremap(&pdev->dev, res->start,
1161 resource_size(res));
1162 if (!xordev->xor_high_base)
1163 return -EBUSY;
1165 platform_set_drvdata(pdev, xordev);
1168 * (Re-)program MBUS remapping windows if we are asked to.
1170 dram = mv_mbus_dram_info();
1171 if (dram)
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;
1183 int i = 0;
1185 for_each_child_of_node(pdev->dev.of_node, np) {
1186 struct mv_xor_chan *chan;
1187 dma_cap_mask_t cap_mask;
1188 int irq;
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);
1199 if (!irq) {
1200 ret = -ENODEV;
1201 goto err_channel_add;
1204 chan = mv_xor_channel_add(xordev, pdev, i,
1205 cap_mask, irq);
1206 if (IS_ERR(chan)) {
1207 ret = PTR_ERR(chan);
1208 irq_dispose_mapping(irq);
1209 goto err_channel_add;
1212 xordev->channels[i] = chan;
1213 i++;
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;
1219 int irq;
1221 cd = &pdata->channels[i];
1222 if (!cd) {
1223 ret = -ENODEV;
1224 goto err_channel_add;
1227 irq = platform_get_irq(pdev, i);
1228 if (irq < 0) {
1229 ret = irq;
1230 goto err_channel_add;
1233 chan = mv_xor_channel_add(xordev, pdev, i,
1234 cd->cap_mask, irq);
1235 if (IS_ERR(chan)) {
1236 ret = PTR_ERR(chan);
1237 goto err_channel_add;
1240 xordev->channels[i] = chan;
1244 return 0;
1246 err_channel_add:
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);
1259 return ret;
1262 static int mv_xor_remove(struct platform_device *pdev)
1264 struct mv_xor_device *xordev = platform_get_drvdata(pdev);
1265 int i;
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);
1277 return 0;
1280 #ifdef CONFIG_OF
1281 static struct of_device_id mv_xor_dt_ids[] = {
1282 { .compatible = "marvell,orion-xor", },
1285 MODULE_DEVICE_TABLE(of, mv_xor_dt_ids);
1286 #endif
1288 static struct platform_driver mv_xor_driver = {
1289 .probe = mv_xor_probe,
1290 .remove = mv_xor_remove,
1291 .driver = {
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 */
1306 #if 0
1307 static void __exit mv_xor_exit(void)
1309 platform_driver_unregister(&mv_xor_driver);
1310 return;
1313 module_exit(mv_xor_exit);
1314 #endif
1316 MODULE_AUTHOR("Saeed Bishara <saeed@marvell.com>");
1317 MODULE_DESCRIPTION("DMA engine driver for Marvell's XOR engine");
1318 MODULE_LICENSE("GPL");