2 * Copyright (C) 2015-2016 Marvell International Ltd.
4 * This program is free software: you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation, either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
15 #include <linux/clk.h>
16 #include <linux/dma-mapping.h>
17 #include <linux/interrupt.h>
19 #include <linux/module.h>
20 #include <linux/msi.h>
22 #include <linux/of_irq.h>
23 #include <linux/platform_device.h>
24 #include <linux/spinlock.h>
26 #include "dmaengine.h"
28 /* DMA Engine Registers */
29 #define MV_XOR_V2_DMA_DESQ_BALR_OFF 0x000
30 #define MV_XOR_V2_DMA_DESQ_BAHR_OFF 0x004
31 #define MV_XOR_V2_DMA_DESQ_SIZE_OFF 0x008
32 #define MV_XOR_V2_DMA_DESQ_DONE_OFF 0x00C
33 #define MV_XOR_V2_DMA_DESQ_DONE_PENDING_MASK 0x7FFF
34 #define MV_XOR_V2_DMA_DESQ_DONE_PENDING_SHIFT 0
35 #define MV_XOR_V2_DMA_DESQ_DONE_READ_PTR_MASK 0x1FFF
36 #define MV_XOR_V2_DMA_DESQ_DONE_READ_PTR_SHIFT 16
37 #define MV_XOR_V2_DMA_DESQ_ARATTR_OFF 0x010
38 #define MV_XOR_V2_DMA_DESQ_ATTR_CACHE_MASK 0x3F3F
39 #define MV_XOR_V2_DMA_DESQ_ATTR_OUTER_SHAREABLE 0x202
40 #define MV_XOR_V2_DMA_DESQ_ATTR_CACHEABLE 0x3C3C
41 #define MV_XOR_V2_DMA_IMSG_CDAT_OFF 0x014
42 #define MV_XOR_V2_DMA_IMSG_THRD_OFF 0x018
43 #define MV_XOR_V2_DMA_IMSG_THRD_MASK 0x7FFF
44 #define MV_XOR_V2_DMA_IMSG_THRD_SHIFT 0x0
45 #define MV_XOR_V2_DMA_DESQ_AWATTR_OFF 0x01C
46 /* Same flags as MV_XOR_V2_DMA_DESQ_ARATTR_OFF */
47 #define MV_XOR_V2_DMA_DESQ_ALLOC_OFF 0x04C
48 #define MV_XOR_V2_DMA_DESQ_ALLOC_WRPTR_MASK 0xFFFF
49 #define MV_XOR_V2_DMA_DESQ_ALLOC_WRPTR_SHIFT 16
50 #define MV_XOR_V2_DMA_IMSG_BALR_OFF 0x050
51 #define MV_XOR_V2_DMA_IMSG_BAHR_OFF 0x054
52 #define MV_XOR_V2_DMA_DESQ_CTRL_OFF 0x100
53 #define MV_XOR_V2_DMA_DESQ_CTRL_32B 1
54 #define MV_XOR_V2_DMA_DESQ_CTRL_128B 7
55 #define MV_XOR_V2_DMA_DESQ_STOP_OFF 0x800
56 #define MV_XOR_V2_DMA_DESQ_DEALLOC_OFF 0x804
57 #define MV_XOR_V2_DMA_DESQ_ADD_OFF 0x808
59 /* XOR Global registers */
60 #define MV_XOR_V2_GLOB_BW_CTRL 0x4
61 #define MV_XOR_V2_GLOB_BW_CTRL_NUM_OSTD_RD_SHIFT 0
62 #define MV_XOR_V2_GLOB_BW_CTRL_NUM_OSTD_RD_VAL 64
63 #define MV_XOR_V2_GLOB_BW_CTRL_NUM_OSTD_WR_SHIFT 8
64 #define MV_XOR_V2_GLOB_BW_CTRL_NUM_OSTD_WR_VAL 8
65 #define MV_XOR_V2_GLOB_BW_CTRL_RD_BURST_LEN_SHIFT 12
66 #define MV_XOR_V2_GLOB_BW_CTRL_RD_BURST_LEN_VAL 4
67 #define MV_XOR_V2_GLOB_BW_CTRL_WR_BURST_LEN_SHIFT 16
68 #define MV_XOR_V2_GLOB_BW_CTRL_WR_BURST_LEN_VAL 4
69 #define MV_XOR_V2_GLOB_PAUSE 0x014
70 #define MV_XOR_V2_GLOB_PAUSE_AXI_TIME_DIS_VAL 0x8
71 #define MV_XOR_V2_GLOB_SYS_INT_CAUSE 0x200
72 #define MV_XOR_V2_GLOB_SYS_INT_MASK 0x204
73 #define MV_XOR_V2_GLOB_MEM_INT_CAUSE 0x220
74 #define MV_XOR_V2_GLOB_MEM_INT_MASK 0x224
76 #define MV_XOR_V2_MIN_DESC_SIZE 32
77 #define MV_XOR_V2_EXT_DESC_SIZE 128
79 #define MV_XOR_V2_DESC_RESERVED_SIZE 12
80 #define MV_XOR_V2_DESC_BUFF_D_ADDR_SIZE 12
82 #define MV_XOR_V2_CMD_LINE_NUM_MAX_D_BUF 8
85 * Descriptors queue size. With 32 bytes descriptors, up to 2^14
86 * descriptors are allowed, with 128 bytes descriptors, up to 2^12
87 * descriptors are allowed. This driver uses 128 bytes descriptors,
88 * but experimentation has shown that a set of 1024 descriptors is
89 * sufficient to reach a good level of performance.
91 #define MV_XOR_V2_DESC_NUM 1024
94 * struct mv_xor_v2_descriptor - DMA HW descriptor
95 * @desc_id: used by S/W and is not affected by H/W.
96 * @flags: error and status flags
97 * @crc32_result: CRC32 calculation result
98 * @desc_ctrl: operation mode and control flags
99 * @buff_size: amount of bytes to be processed
100 * @fill_pattern_src_addr: Fill-Pattern or Source-Address and
102 * @data_buff_addr: Source (and might be RAID6 destination)
103 * addresses of data buffers in RAID5 and RAID6
104 * @reserved: reserved
106 struct mv_xor_v2_descriptor {
112 /* Definitions for desc_ctrl */
113 #define DESC_NUM_ACTIVE_D_BUF_SHIFT 22
114 #define DESC_OP_MODE_SHIFT 28
115 #define DESC_OP_MODE_NOP 0 /* Idle operation */
116 #define DESC_OP_MODE_MEMCPY 1 /* Pure-DMA operation */
117 #define DESC_OP_MODE_MEMSET 2 /* Mem-Fill operation */
118 #define DESC_OP_MODE_MEMINIT 3 /* Mem-Init operation */
119 #define DESC_OP_MODE_MEM_COMPARE 4 /* Mem-Compare operation */
120 #define DESC_OP_MODE_CRC32 5 /* CRC32 calculation */
121 #define DESC_OP_MODE_XOR 6 /* RAID5 (XOR) operation */
122 #define DESC_OP_MODE_RAID6 7 /* RAID6 P&Q-generation */
123 #define DESC_OP_MODE_RAID6_REC 8 /* RAID6 Recovery */
124 #define DESC_Q_BUFFER_ENABLE BIT(16)
125 #define DESC_P_BUFFER_ENABLE BIT(17)
126 #define DESC_IOD BIT(27)
129 u32 fill_pattern_src_addr[4];
130 u32 data_buff_addr[MV_XOR_V2_DESC_BUFF_D_ADDR_SIZE];
131 u32 reserved[MV_XOR_V2_DESC_RESERVED_SIZE];
135 * struct mv_xor_v2_device - implements a xor device
136 * @lock: lock for the engine
137 * @dma_base: memory mapped DMA register base
138 * @glob_base: memory mapped global register base
140 * @free_sw_desc: linked list of free SW descriptors
141 * @dmadev: dma device
142 * @dmachan: dma channel
143 * @hw_desq: HW descriptors queue
144 * @hw_desq_virt: virtual address of DESCQ
145 * @sw_desq: SW descriptors queue
146 * @desc_size: HW descriptor size
147 * @npendings: number of pending descriptors (for which tx_submit has
148 * been called, but not yet issue_pending)
150 struct mv_xor_v2_device {
152 void __iomem *dma_base;
153 void __iomem *glob_base;
155 struct tasklet_struct irq_tasklet;
156 struct list_head free_sw_desc;
157 struct dma_device dmadev;
158 struct dma_chan dmachan;
160 struct mv_xor_v2_descriptor *hw_desq_virt;
161 struct mv_xor_v2_sw_desc *sw_desq;
163 unsigned int npendings;
167 * struct mv_xor_v2_sw_desc - implements a xor SW descriptor
168 * @idx: descriptor index
169 * @async_tx: support for the async_tx api
170 * @hw_desc: assosiated HW descriptor
171 * @free_list: node of the free SW descriprots list
173 struct mv_xor_v2_sw_desc {
175 struct dma_async_tx_descriptor async_tx;
176 struct mv_xor_v2_descriptor hw_desc;
177 struct list_head free_list;
181 * Fill the data buffers to a HW descriptor
183 static void mv_xor_v2_set_data_buffers(struct mv_xor_v2_device *xor_dev,
184 struct mv_xor_v2_descriptor *desc,
185 dma_addr_t src, int index)
187 int arr_index = ((index >> 1) * 3);
190 * Fill the buffer's addresses to the descriptor.
192 * The format of the buffers address for 2 sequential buffers
195 * First word: Buffer-DX-Address-Low[31:0]
196 * Second word: Buffer-DX+1-Address-Low[31:0]
197 * Third word: DX+1-Buffer-Address-High[47:32] [31:16]
198 * DX-Buffer-Address-High[47:32] [15:0]
200 if ((index & 0x1) == 0) {
201 desc->data_buff_addr[arr_index] = lower_32_bits(src);
203 desc->data_buff_addr[arr_index + 2] &= ~0xFFFF;
204 desc->data_buff_addr[arr_index + 2] |=
205 upper_32_bits(src) & 0xFFFF;
207 desc->data_buff_addr[arr_index + 1] =
210 desc->data_buff_addr[arr_index + 2] &= ~0xFFFF0000;
211 desc->data_buff_addr[arr_index + 2] |=
212 (upper_32_bits(src) & 0xFFFF) << 16;
217 * Return the next available index in the DESQ.
219 static int mv_xor_v2_get_desq_write_ptr(struct mv_xor_v2_device *xor_dev)
221 /* read the index for the next available descriptor in the DESQ */
222 u32 reg = readl(xor_dev->dma_base + MV_XOR_V2_DMA_DESQ_ALLOC_OFF);
224 return ((reg >> MV_XOR_V2_DMA_DESQ_ALLOC_WRPTR_SHIFT)
225 & MV_XOR_V2_DMA_DESQ_ALLOC_WRPTR_MASK);
229 * notify the engine of new descriptors, and update the available index.
231 static void mv_xor_v2_add_desc_to_desq(struct mv_xor_v2_device *xor_dev,
234 /* write the number of new descriptors in the DESQ. */
235 writel(num_of_desc, xor_dev->dma_base + MV_XOR_V2_DMA_DESQ_ADD_OFF);
239 * free HW descriptors
241 static void mv_xor_v2_free_desc_from_desq(struct mv_xor_v2_device *xor_dev,
244 /* write the number of new descriptors in the DESQ. */
245 writel(num_of_desc, xor_dev->dma_base + MV_XOR_V2_DMA_DESQ_DEALLOC_OFF);
249 * Set descriptor size
250 * Return the HW descriptor size in bytes
252 static int mv_xor_v2_set_desc_size(struct mv_xor_v2_device *xor_dev)
254 writel(MV_XOR_V2_DMA_DESQ_CTRL_128B,
255 xor_dev->dma_base + MV_XOR_V2_DMA_DESQ_CTRL_OFF);
257 return MV_XOR_V2_EXT_DESC_SIZE;
261 * Set the IMSG threshold
264 void mv_xor_v2_set_imsg_thrd(struct mv_xor_v2_device *xor_dev, int thrd_val)
268 reg = readl(xor_dev->dma_base + MV_XOR_V2_DMA_IMSG_THRD_OFF);
270 reg &= (~MV_XOR_V2_DMA_IMSG_THRD_MASK << MV_XOR_V2_DMA_IMSG_THRD_SHIFT);
271 reg |= (thrd_val << MV_XOR_V2_DMA_IMSG_THRD_SHIFT);
273 writel(reg, xor_dev->dma_base + MV_XOR_V2_DMA_IMSG_THRD_OFF);
276 static irqreturn_t mv_xor_v2_interrupt_handler(int irq, void *data)
278 struct mv_xor_v2_device *xor_dev = data;
282 reg = readl(xor_dev->dma_base + MV_XOR_V2_DMA_DESQ_DONE_OFF);
284 ndescs = ((reg >> MV_XOR_V2_DMA_DESQ_DONE_PENDING_SHIFT) &
285 MV_XOR_V2_DMA_DESQ_DONE_PENDING_MASK);
287 /* No descriptors to process */
292 * Update IMSG threshold, to disable new IMSG interrupts until
295 mv_xor_v2_set_imsg_thrd(xor_dev, MV_XOR_V2_DESC_NUM);
297 /* schedule a tasklet to handle descriptors callbacks */
298 tasklet_schedule(&xor_dev->irq_tasklet);
304 * submit a descriptor to the DMA engine
307 mv_xor_v2_tx_submit(struct dma_async_tx_descriptor *tx)
312 struct mv_xor_v2_sw_desc *sw_desc =
313 container_of(tx, struct mv_xor_v2_sw_desc, async_tx);
314 struct mv_xor_v2_device *xor_dev =
315 container_of(tx->chan, struct mv_xor_v2_device, dmachan);
317 dev_dbg(xor_dev->dmadev.dev,
318 "%s sw_desc %p: async_tx %p\n",
319 __func__, sw_desc, &sw_desc->async_tx);
322 spin_lock_bh(&xor_dev->lock);
323 cookie = dma_cookie_assign(tx);
325 /* get the next available slot in the DESQ */
326 desq_ptr = mv_xor_v2_get_desq_write_ptr(xor_dev);
328 /* copy the HW descriptor from the SW descriptor to the DESQ */
329 dest_hw_desc = xor_dev->hw_desq_virt + desq_ptr;
331 memcpy(dest_hw_desc, &sw_desc->hw_desc, xor_dev->desc_size);
333 xor_dev->npendings++;
335 spin_unlock_bh(&xor_dev->lock);
341 * Prepare a SW descriptor
343 static struct mv_xor_v2_sw_desc *
344 mv_xor_v2_prep_sw_desc(struct mv_xor_v2_device *xor_dev)
346 struct mv_xor_v2_sw_desc *sw_desc;
349 /* Lock the channel */
350 spin_lock_bh(&xor_dev->lock);
352 if (list_empty(&xor_dev->free_sw_desc)) {
353 spin_unlock_bh(&xor_dev->lock);
354 /* schedule tasklet to free some descriptors */
355 tasklet_schedule(&xor_dev->irq_tasklet);
359 list_for_each_entry(sw_desc, &xor_dev->free_sw_desc, free_list) {
360 if (async_tx_test_ack(&sw_desc->async_tx)) {
367 spin_unlock_bh(&xor_dev->lock);
371 list_del(&sw_desc->free_list);
373 /* Release the channel */
374 spin_unlock_bh(&xor_dev->lock);
380 * Prepare a HW descriptor for a memcpy operation
382 static struct dma_async_tx_descriptor *
383 mv_xor_v2_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest,
384 dma_addr_t src, size_t len, unsigned long flags)
386 struct mv_xor_v2_sw_desc *sw_desc;
387 struct mv_xor_v2_descriptor *hw_descriptor;
388 struct mv_xor_v2_device *xor_dev;
390 xor_dev = container_of(chan, struct mv_xor_v2_device, dmachan);
392 dev_dbg(xor_dev->dmadev.dev,
393 "%s len: %zu src %pad dest %pad flags: %ld\n",
394 __func__, len, &src, &dest, flags);
396 sw_desc = mv_xor_v2_prep_sw_desc(xor_dev);
400 sw_desc->async_tx.flags = flags;
402 /* set the HW descriptor */
403 hw_descriptor = &sw_desc->hw_desc;
405 /* save the SW descriptor ID to restore when operation is done */
406 hw_descriptor->desc_id = sw_desc->idx;
408 /* Set the MEMCPY control word */
409 hw_descriptor->desc_ctrl =
410 DESC_OP_MODE_MEMCPY << DESC_OP_MODE_SHIFT;
412 if (flags & DMA_PREP_INTERRUPT)
413 hw_descriptor->desc_ctrl |= DESC_IOD;
415 /* Set source address */
416 hw_descriptor->fill_pattern_src_addr[0] = lower_32_bits(src);
417 hw_descriptor->fill_pattern_src_addr[1] =
418 upper_32_bits(src) & 0xFFFF;
420 /* Set Destination address */
421 hw_descriptor->fill_pattern_src_addr[2] = lower_32_bits(dest);
422 hw_descriptor->fill_pattern_src_addr[3] =
423 upper_32_bits(dest) & 0xFFFF;
425 /* Set buffers size */
426 hw_descriptor->buff_size = len;
428 /* return the async tx descriptor */
429 return &sw_desc->async_tx;
433 * Prepare a HW descriptor for a XOR operation
435 static struct dma_async_tx_descriptor *
436 mv_xor_v2_prep_dma_xor(struct dma_chan *chan, dma_addr_t dest, dma_addr_t *src,
437 unsigned int src_cnt, size_t len, unsigned long flags)
439 struct mv_xor_v2_sw_desc *sw_desc;
440 struct mv_xor_v2_descriptor *hw_descriptor;
441 struct mv_xor_v2_device *xor_dev =
442 container_of(chan, struct mv_xor_v2_device, dmachan);
445 if (src_cnt > MV_XOR_V2_CMD_LINE_NUM_MAX_D_BUF || src_cnt < 1)
448 dev_dbg(xor_dev->dmadev.dev,
449 "%s src_cnt: %d len: %zu dest %pad flags: %ld\n",
450 __func__, src_cnt, len, &dest, flags);
452 sw_desc = mv_xor_v2_prep_sw_desc(xor_dev);
456 sw_desc->async_tx.flags = flags;
458 /* set the HW descriptor */
459 hw_descriptor = &sw_desc->hw_desc;
461 /* save the SW descriptor ID to restore when operation is done */
462 hw_descriptor->desc_id = sw_desc->idx;
464 /* Set the XOR control word */
465 hw_descriptor->desc_ctrl =
466 DESC_OP_MODE_XOR << DESC_OP_MODE_SHIFT;
467 hw_descriptor->desc_ctrl |= DESC_P_BUFFER_ENABLE;
469 if (flags & DMA_PREP_INTERRUPT)
470 hw_descriptor->desc_ctrl |= DESC_IOD;
472 /* Set the data buffers */
473 for (i = 0; i < src_cnt; i++)
474 mv_xor_v2_set_data_buffers(xor_dev, hw_descriptor, src[i], i);
476 hw_descriptor->desc_ctrl |=
477 src_cnt << DESC_NUM_ACTIVE_D_BUF_SHIFT;
479 /* Set Destination address */
480 hw_descriptor->fill_pattern_src_addr[2] = lower_32_bits(dest);
481 hw_descriptor->fill_pattern_src_addr[3] =
482 upper_32_bits(dest) & 0xFFFF;
484 /* Set buffers size */
485 hw_descriptor->buff_size = len;
487 /* return the async tx descriptor */
488 return &sw_desc->async_tx;
492 * Prepare a HW descriptor for interrupt operation.
494 static struct dma_async_tx_descriptor *
495 mv_xor_v2_prep_dma_interrupt(struct dma_chan *chan, unsigned long flags)
497 struct mv_xor_v2_sw_desc *sw_desc;
498 struct mv_xor_v2_descriptor *hw_descriptor;
499 struct mv_xor_v2_device *xor_dev =
500 container_of(chan, struct mv_xor_v2_device, dmachan);
502 sw_desc = mv_xor_v2_prep_sw_desc(xor_dev);
506 /* set the HW descriptor */
507 hw_descriptor = &sw_desc->hw_desc;
509 /* save the SW descriptor ID to restore when operation is done */
510 hw_descriptor->desc_id = sw_desc->idx;
512 /* Set the INTERRUPT control word */
513 hw_descriptor->desc_ctrl =
514 DESC_OP_MODE_NOP << DESC_OP_MODE_SHIFT;
515 hw_descriptor->desc_ctrl |= DESC_IOD;
517 /* return the async tx descriptor */
518 return &sw_desc->async_tx;
522 * push pending transactions to hardware
524 static void mv_xor_v2_issue_pending(struct dma_chan *chan)
526 struct mv_xor_v2_device *xor_dev =
527 container_of(chan, struct mv_xor_v2_device, dmachan);
529 spin_lock_bh(&xor_dev->lock);
532 * update the engine with the number of descriptors to
535 mv_xor_v2_add_desc_to_desq(xor_dev, xor_dev->npendings);
536 xor_dev->npendings = 0;
538 /* Activate the channel */
539 writel(0, xor_dev->dma_base + MV_XOR_V2_DMA_DESQ_STOP_OFF);
541 spin_unlock_bh(&xor_dev->lock);
545 int mv_xor_v2_get_pending_params(struct mv_xor_v2_device *xor_dev,
550 reg = readl(xor_dev->dma_base + MV_XOR_V2_DMA_DESQ_DONE_OFF);
552 /* get the next pending descriptor index */
553 *pending_ptr = ((reg >> MV_XOR_V2_DMA_DESQ_DONE_READ_PTR_SHIFT) &
554 MV_XOR_V2_DMA_DESQ_DONE_READ_PTR_MASK);
556 /* get the number of descriptors pending handle */
557 return ((reg >> MV_XOR_V2_DMA_DESQ_DONE_PENDING_SHIFT) &
558 MV_XOR_V2_DMA_DESQ_DONE_PENDING_MASK);
562 * handle the descriptors after HW process
564 static void mv_xor_v2_tasklet(unsigned long data)
566 struct mv_xor_v2_device *xor_dev = (struct mv_xor_v2_device *) data;
567 int pending_ptr, num_of_pending, i;
568 struct mv_xor_v2_sw_desc *next_pending_sw_desc = NULL;
570 dev_dbg(xor_dev->dmadev.dev, "%s %d\n", __func__, __LINE__);
572 /* get the pending descriptors parameters */
573 num_of_pending = mv_xor_v2_get_pending_params(xor_dev, &pending_ptr);
575 /* loop over free descriptors */
576 for (i = 0; i < num_of_pending; i++) {
577 struct mv_xor_v2_descriptor *next_pending_hw_desc =
578 xor_dev->hw_desq_virt + pending_ptr;
580 /* get the SW descriptor related to the HW descriptor */
581 next_pending_sw_desc =
582 &xor_dev->sw_desq[next_pending_hw_desc->desc_id];
584 /* call the callback */
585 if (next_pending_sw_desc->async_tx.cookie > 0) {
587 * update the channel's completed cookie - no
588 * lock is required the IMSG threshold provide
591 dma_cookie_complete(&next_pending_sw_desc->async_tx);
593 if (next_pending_sw_desc->async_tx.callback)
594 next_pending_sw_desc->async_tx.callback(
595 next_pending_sw_desc->async_tx.callback_param);
597 dma_descriptor_unmap(&next_pending_sw_desc->async_tx);
600 dma_run_dependencies(&next_pending_sw_desc->async_tx);
602 /* Lock the channel */
603 spin_lock_bh(&xor_dev->lock);
605 /* add the SW descriptor to the free descriptors list */
606 list_add(&next_pending_sw_desc->free_list,
607 &xor_dev->free_sw_desc);
609 /* Release the channel */
610 spin_unlock_bh(&xor_dev->lock);
612 /* increment the next descriptor */
614 if (pending_ptr >= MV_XOR_V2_DESC_NUM)
618 if (num_of_pending != 0) {
619 /* free the descriptores */
620 mv_xor_v2_free_desc_from_desq(xor_dev, num_of_pending);
623 /* Update IMSG threshold, to enable new IMSG interrupts */
624 mv_xor_v2_set_imsg_thrd(xor_dev, 0);
628 * Set DMA Interrupt-message (IMSG) parameters
630 static void mv_xor_v2_set_msi_msg(struct msi_desc *desc, struct msi_msg *msg)
632 struct mv_xor_v2_device *xor_dev = dev_get_drvdata(desc->dev);
634 writel(msg->address_lo,
635 xor_dev->dma_base + MV_XOR_V2_DMA_IMSG_BALR_OFF);
636 writel(msg->address_hi & 0xFFFF,
637 xor_dev->dma_base + MV_XOR_V2_DMA_IMSG_BAHR_OFF);
639 xor_dev->dma_base + MV_XOR_V2_DMA_IMSG_CDAT_OFF);
642 static int mv_xor_v2_descq_init(struct mv_xor_v2_device *xor_dev)
646 /* write the DESQ size to the DMA engine */
647 writel(MV_XOR_V2_DESC_NUM,
648 xor_dev->dma_base + MV_XOR_V2_DMA_DESQ_SIZE_OFF);
650 /* write the DESQ address to the DMA enngine*/
651 writel(xor_dev->hw_desq & 0xFFFFFFFF,
652 xor_dev->dma_base + MV_XOR_V2_DMA_DESQ_BALR_OFF);
653 writel((xor_dev->hw_desq & 0xFFFF00000000) >> 32,
654 xor_dev->dma_base + MV_XOR_V2_DMA_DESQ_BAHR_OFF);
656 /* enable the DMA engine */
657 writel(0, xor_dev->dma_base + MV_XOR_V2_DMA_DESQ_STOP_OFF);
660 * This is a temporary solution, until we activate the
661 * SMMU. Set the attributes for reading & writing data buffers
664 * - OuterShareable - Snoops will be performed on CPU caches
665 * - Enable cacheable - Bufferable, Modifiable, Other Allocate
668 reg = readl(xor_dev->dma_base + MV_XOR_V2_DMA_DESQ_ARATTR_OFF);
669 reg &= ~MV_XOR_V2_DMA_DESQ_ATTR_CACHE_MASK;
670 reg |= MV_XOR_V2_DMA_DESQ_ATTR_OUTER_SHAREABLE |
671 MV_XOR_V2_DMA_DESQ_ATTR_CACHEABLE;
672 writel(reg, xor_dev->dma_base + MV_XOR_V2_DMA_DESQ_ARATTR_OFF);
674 reg = readl(xor_dev->dma_base + MV_XOR_V2_DMA_DESQ_AWATTR_OFF);
675 reg &= ~MV_XOR_V2_DMA_DESQ_ATTR_CACHE_MASK;
676 reg |= MV_XOR_V2_DMA_DESQ_ATTR_OUTER_SHAREABLE |
677 MV_XOR_V2_DMA_DESQ_ATTR_CACHEABLE;
678 writel(reg, xor_dev->dma_base + MV_XOR_V2_DMA_DESQ_AWATTR_OFF);
680 /* BW CTRL - set values to optimize the XOR performance:
682 * - Set WrBurstLen & RdBurstLen - the unit will issue
683 * maximum of 256B write/read transactions.
684 * - Limit the number of outstanding write & read data
685 * (OBB/IBB) requests to the maximal value.
687 reg = ((MV_XOR_V2_GLOB_BW_CTRL_NUM_OSTD_RD_VAL <<
688 MV_XOR_V2_GLOB_BW_CTRL_NUM_OSTD_RD_SHIFT) |
689 (MV_XOR_V2_GLOB_BW_CTRL_NUM_OSTD_WR_VAL <<
690 MV_XOR_V2_GLOB_BW_CTRL_NUM_OSTD_WR_SHIFT) |
691 (MV_XOR_V2_GLOB_BW_CTRL_RD_BURST_LEN_VAL <<
692 MV_XOR_V2_GLOB_BW_CTRL_RD_BURST_LEN_SHIFT) |
693 (MV_XOR_V2_GLOB_BW_CTRL_WR_BURST_LEN_VAL <<
694 MV_XOR_V2_GLOB_BW_CTRL_WR_BURST_LEN_SHIFT));
695 writel(reg, xor_dev->glob_base + MV_XOR_V2_GLOB_BW_CTRL);
697 /* Disable the AXI timer feature */
698 reg = readl(xor_dev->glob_base + MV_XOR_V2_GLOB_PAUSE);
699 reg |= MV_XOR_V2_GLOB_PAUSE_AXI_TIME_DIS_VAL;
700 writel(reg, xor_dev->glob_base + MV_XOR_V2_GLOB_PAUSE);
705 static int mv_xor_v2_probe(struct platform_device *pdev)
707 struct mv_xor_v2_device *xor_dev;
708 struct resource *res;
710 struct dma_device *dma_dev;
711 struct mv_xor_v2_sw_desc *sw_desc;
712 struct msi_desc *msi_desc;
714 BUILD_BUG_ON(sizeof(struct mv_xor_v2_descriptor) !=
715 MV_XOR_V2_EXT_DESC_SIZE);
717 xor_dev = devm_kzalloc(&pdev->dev, sizeof(*xor_dev), GFP_KERNEL);
721 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
722 xor_dev->dma_base = devm_ioremap_resource(&pdev->dev, res);
723 if (IS_ERR(xor_dev->dma_base))
724 return PTR_ERR(xor_dev->dma_base);
726 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
727 xor_dev->glob_base = devm_ioremap_resource(&pdev->dev, res);
728 if (IS_ERR(xor_dev->glob_base))
729 return PTR_ERR(xor_dev->glob_base);
731 platform_set_drvdata(pdev, xor_dev);
733 xor_dev->clk = devm_clk_get(&pdev->dev, NULL);
734 if (IS_ERR(xor_dev->clk) && PTR_ERR(xor_dev->clk) == -EPROBE_DEFER)
735 return -EPROBE_DEFER;
736 if (!IS_ERR(xor_dev->clk)) {
737 ret = clk_prepare_enable(xor_dev->clk);
742 ret = platform_msi_domain_alloc_irqs(&pdev->dev, 1,
743 mv_xor_v2_set_msi_msg);
747 msi_desc = first_msi_entry(&pdev->dev);
751 ret = devm_request_irq(&pdev->dev, msi_desc->irq,
752 mv_xor_v2_interrupt_handler, 0,
753 dev_name(&pdev->dev), xor_dev);
757 tasklet_init(&xor_dev->irq_tasklet, mv_xor_v2_tasklet,
758 (unsigned long) xor_dev);
760 xor_dev->desc_size = mv_xor_v2_set_desc_size(xor_dev);
762 dma_cookie_init(&xor_dev->dmachan);
765 * allocate coherent memory for hardware descriptors
766 * note: writecombine gives slightly better performance, but
767 * requires that we explicitly flush the writes
769 xor_dev->hw_desq_virt =
770 dma_alloc_coherent(&pdev->dev,
771 xor_dev->desc_size * MV_XOR_V2_DESC_NUM,
772 &xor_dev->hw_desq, GFP_KERNEL);
773 if (!xor_dev->hw_desq_virt) {
778 /* alloc memory for the SW descriptors */
779 xor_dev->sw_desq = devm_kzalloc(&pdev->dev, sizeof(*sw_desc) *
780 MV_XOR_V2_DESC_NUM, GFP_KERNEL);
781 if (!xor_dev->sw_desq) {
786 spin_lock_init(&xor_dev->lock);
788 /* init the free SW descriptors list */
789 INIT_LIST_HEAD(&xor_dev->free_sw_desc);
791 /* add all SW descriptors to the free list */
792 for (i = 0; i < MV_XOR_V2_DESC_NUM; i++) {
793 struct mv_xor_v2_sw_desc *sw_desc =
794 xor_dev->sw_desq + i;
796 dma_async_tx_descriptor_init(&sw_desc->async_tx,
798 sw_desc->async_tx.tx_submit = mv_xor_v2_tx_submit;
799 async_tx_ack(&sw_desc->async_tx);
801 list_add(&sw_desc->free_list,
802 &xor_dev->free_sw_desc);
805 dma_dev = &xor_dev->dmadev;
807 /* set DMA capabilities */
808 dma_cap_zero(dma_dev->cap_mask);
809 dma_cap_set(DMA_MEMCPY, dma_dev->cap_mask);
810 dma_cap_set(DMA_XOR, dma_dev->cap_mask);
811 dma_cap_set(DMA_INTERRUPT, dma_dev->cap_mask);
813 /* init dma link list */
814 INIT_LIST_HEAD(&dma_dev->channels);
816 /* set base routines */
817 dma_dev->device_tx_status = dma_cookie_status;
818 dma_dev->device_issue_pending = mv_xor_v2_issue_pending;
819 dma_dev->dev = &pdev->dev;
821 dma_dev->device_prep_dma_memcpy = mv_xor_v2_prep_dma_memcpy;
822 dma_dev->device_prep_dma_interrupt = mv_xor_v2_prep_dma_interrupt;
823 dma_dev->max_xor = 8;
824 dma_dev->device_prep_dma_xor = mv_xor_v2_prep_dma_xor;
826 xor_dev->dmachan.device = dma_dev;
828 list_add_tail(&xor_dev->dmachan.device_node,
831 mv_xor_v2_descq_init(xor_dev);
833 ret = dma_async_device_register(dma_dev);
837 dev_notice(&pdev->dev, "Marvell Version 2 XOR driver\n");
842 dma_free_coherent(&pdev->dev,
843 xor_dev->desc_size * MV_XOR_V2_DESC_NUM,
844 xor_dev->hw_desq_virt, xor_dev->hw_desq);
846 platform_msi_domain_free_irqs(&pdev->dev);
848 if (!IS_ERR(xor_dev->clk))
849 clk_disable_unprepare(xor_dev->clk);
853 static int mv_xor_v2_remove(struct platform_device *pdev)
855 struct mv_xor_v2_device *xor_dev = platform_get_drvdata(pdev);
857 dma_async_device_unregister(&xor_dev->dmadev);
859 dma_free_coherent(&pdev->dev,
860 xor_dev->desc_size * MV_XOR_V2_DESC_NUM,
861 xor_dev->hw_desq_virt, xor_dev->hw_desq);
863 platform_msi_domain_free_irqs(&pdev->dev);
865 clk_disable_unprepare(xor_dev->clk);
871 static const struct of_device_id mv_xor_v2_dt_ids[] = {
872 { .compatible = "marvell,xor-v2", },
875 MODULE_DEVICE_TABLE(of, mv_xor_v2_dt_ids);
878 static struct platform_driver mv_xor_v2_driver = {
879 .probe = mv_xor_v2_probe,
880 .remove = mv_xor_v2_remove,
883 .of_match_table = of_match_ptr(mv_xor_v2_dt_ids),
887 module_platform_driver(mv_xor_v2_driver);
889 MODULE_DESCRIPTION("DMA engine driver for Marvell's Version 2 of XOR engine");
890 MODULE_LICENSE("GPL");