]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/dma/mv_xor.c
dmaengine: at_xdmac: double FIFO flush needed to compute residue
[mirror_ubuntu-artful-kernel.git] / drivers / dma / mv_xor.c
CommitLineData
ff7b0479
SB
1/*
2 * offload engine driver for the Marvell XOR engine
3 * Copyright (C) 2007, 2008, Marvell International Ltd.
4 *
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.
8 *
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.
ff7b0479
SB
13 */
14
15#include <linux/init.h>
5a0e3ad6 16#include <linux/slab.h>
ff7b0479
SB
17#include <linux/delay.h>
18#include <linux/dma-mapping.h>
19#include <linux/spinlock.h>
20#include <linux/interrupt.h>
6f166312 21#include <linux/of_device.h>
ff7b0479
SB
22#include <linux/platform_device.h>
23#include <linux/memory.h>
c510182b 24#include <linux/clk.h>
f7d12ef5
TP
25#include <linux/of.h>
26#include <linux/of_irq.h>
27#include <linux/irqdomain.h>
77757291 28#include <linux/cpumask.h>
c02cecb9 29#include <linux/platform_data/dma-mv_xor.h>
d2ebfb33
RKAL
30
31#include "dmaengine.h"
ff7b0479
SB
32#include "mv_xor.h"
33
dd130c65
GC
34enum mv_xor_type {
35 XOR_ORION,
36 XOR_ARMADA_38X,
ac5f0f3f 37 XOR_ARMADA_37XX,
dd130c65
GC
38};
39
6f166312
LA
40enum mv_xor_mode {
41 XOR_MODE_IN_REG,
42 XOR_MODE_IN_DESC,
43};
44
ff7b0479
SB
45static void mv_xor_issue_pending(struct dma_chan *chan);
46
47#define to_mv_xor_chan(chan) \
98817b99 48 container_of(chan, struct mv_xor_chan, dmachan)
ff7b0479
SB
49
50#define to_mv_xor_slot(tx) \
51 container_of(tx, struct mv_xor_desc_slot, async_tx)
52
c98c1781 53#define mv_chan_to_devp(chan) \
1ef48a26 54 ((chan)->dmadev.dev)
c98c1781 55
dfc97661 56static void mv_desc_init(struct mv_xor_desc_slot *desc,
ba87d137
LA
57 dma_addr_t addr, u32 byte_count,
58 enum dma_ctrl_flags flags)
ff7b0479
SB
59{
60 struct mv_xor_desc *hw_desc = desc->hw_desc;
61
0e7488ed 62 hw_desc->status = XOR_DESC_DMA_OWNED;
ff7b0479 63 hw_desc->phy_next_desc = 0;
ba87d137
LA
64 /* Enable end-of-descriptor interrupts only for DMA_PREP_INTERRUPT */
65 hw_desc->desc_command = (flags & DMA_PREP_INTERRUPT) ?
66 XOR_DESC_EOD_INT_EN : 0;
dfc97661 67 hw_desc->phy_dest_addr = addr;
ff7b0479
SB
68 hw_desc->byte_count = byte_count;
69}
70
6f166312
LA
71static void mv_desc_set_mode(struct mv_xor_desc_slot *desc)
72{
73 struct mv_xor_desc *hw_desc = desc->hw_desc;
74
75 switch (desc->type) {
76 case DMA_XOR:
77 case DMA_INTERRUPT:
78 hw_desc->desc_command |= XOR_DESC_OPERATION_XOR;
79 break;
80 case DMA_MEMCPY:
81 hw_desc->desc_command |= XOR_DESC_OPERATION_MEMCPY;
82 break;
83 default:
84 BUG();
85 return;
86 }
87}
88
ff7b0479
SB
89static void mv_desc_set_next_desc(struct mv_xor_desc_slot *desc,
90 u32 next_desc_addr)
91{
92 struct mv_xor_desc *hw_desc = desc->hw_desc;
93 BUG_ON(hw_desc->phy_next_desc);
94 hw_desc->phy_next_desc = next_desc_addr;
95}
96
ff7b0479
SB
97static void mv_desc_set_src_addr(struct mv_xor_desc_slot *desc,
98 int index, dma_addr_t addr)
99{
100 struct mv_xor_desc *hw_desc = desc->hw_desc;
e03bc654 101 hw_desc->phy_src_addr[mv_phy_src_idx(index)] = addr;
ff7b0479
SB
102 if (desc->type == DMA_XOR)
103 hw_desc->desc_command |= (1 << index);
104}
105
106static u32 mv_chan_get_current_desc(struct mv_xor_chan *chan)
107{
5733c38a 108 return readl_relaxed(XOR_CURR_DESC(chan));
ff7b0479
SB
109}
110
111static void mv_chan_set_next_descriptor(struct mv_xor_chan *chan,
112 u32 next_desc_addr)
113{
5733c38a 114 writel_relaxed(next_desc_addr, XOR_NEXT_DESC(chan));
ff7b0479
SB
115}
116
ff7b0479
SB
117static void mv_chan_unmask_interrupts(struct mv_xor_chan *chan)
118{
5733c38a 119 u32 val = readl_relaxed(XOR_INTR_MASK(chan));
ff7b0479 120 val |= XOR_INTR_MASK_VALUE << (chan->idx * 16);
5733c38a 121 writel_relaxed(val, XOR_INTR_MASK(chan));
ff7b0479
SB
122}
123
124static u32 mv_chan_get_intr_cause(struct mv_xor_chan *chan)
125{
5733c38a 126 u32 intr_cause = readl_relaxed(XOR_INTR_CAUSE(chan));
ff7b0479
SB
127 intr_cause = (intr_cause >> (chan->idx * 16)) & 0xFFFF;
128 return intr_cause;
129}
130
0951e728 131static void mv_chan_clear_eoc_cause(struct mv_xor_chan *chan)
ff7b0479 132{
ba87d137
LA
133 u32 val;
134
135 val = XOR_INT_END_OF_DESC | XOR_INT_END_OF_CHAIN | XOR_INT_STOPPED;
136 val = ~(val << (chan->idx * 16));
c98c1781 137 dev_dbg(mv_chan_to_devp(chan), "%s, val 0x%08x\n", __func__, val);
5733c38a 138 writel_relaxed(val, XOR_INTR_CAUSE(chan));
ff7b0479
SB
139}
140
0951e728 141static void mv_chan_clear_err_status(struct mv_xor_chan *chan)
ff7b0479
SB
142{
143 u32 val = 0xFFFF0000 >> (chan->idx * 16);
5733c38a 144 writel_relaxed(val, XOR_INTR_CAUSE(chan));
ff7b0479
SB
145}
146
0951e728 147static void mv_chan_set_mode(struct mv_xor_chan *chan,
81aafb3e 148 u32 op_mode)
ff7b0479 149{
5733c38a 150 u32 config = readl_relaxed(XOR_CONFIG(chan));
ff7b0479 151
6f166312
LA
152 config &= ~0x7;
153 config |= op_mode;
154
e03bc654
TP
155#if defined(__BIG_ENDIAN)
156 config |= XOR_DESCRIPTOR_SWAP;
157#else
158 config &= ~XOR_DESCRIPTOR_SWAP;
159#endif
160
5733c38a 161 writel_relaxed(config, XOR_CONFIG(chan));
ff7b0479
SB
162}
163
164static void mv_chan_activate(struct mv_xor_chan *chan)
165{
c98c1781 166 dev_dbg(mv_chan_to_devp(chan), " activate chan.\n");
5a9a55bf
EG
167
168 /* writel ensures all descriptors are flushed before activation */
169 writel(BIT(0), XOR_ACTIVATION(chan));
ff7b0479
SB
170}
171
172static char mv_chan_is_busy(struct mv_xor_chan *chan)
173{
5733c38a 174 u32 state = readl_relaxed(XOR_ACTIVATION(chan));
ff7b0479
SB
175
176 state = (state >> 4) & 0x3;
177
178 return (state == 1) ? 1 : 0;
179}
180
ff7b0479 181/*
0951e728
MR
182 * mv_chan_start_new_chain - program the engine to operate on new
183 * chain headed by sw_desc
ff7b0479
SB
184 * Caller must hold &mv_chan->lock while calling this function
185 */
0951e728
MR
186static void mv_chan_start_new_chain(struct mv_xor_chan *mv_chan,
187 struct mv_xor_desc_slot *sw_desc)
ff7b0479 188{
c98c1781 189 dev_dbg(mv_chan_to_devp(mv_chan), "%s %d: sw_desc %p\n",
ff7b0479 190 __func__, __LINE__, sw_desc);
ff7b0479 191
48a9db46
BZ
192 /* set the hardware chain */
193 mv_chan_set_next_descriptor(mv_chan, sw_desc->async_tx.phys);
194
dfc97661 195 mv_chan->pending++;
98817b99 196 mv_xor_issue_pending(&mv_chan->dmachan);
ff7b0479
SB
197}
198
199static dma_cookie_t
0951e728
MR
200mv_desc_run_tx_complete_actions(struct mv_xor_desc_slot *desc,
201 struct mv_xor_chan *mv_chan,
202 dma_cookie_t cookie)
ff7b0479
SB
203{
204 BUG_ON(desc->async_tx.cookie < 0);
205
206 if (desc->async_tx.cookie > 0) {
207 cookie = desc->async_tx.cookie;
208
209 /* call the callback (must not sleep or submit new
210 * operations to this channel)
211 */
212 if (desc->async_tx.callback)
213 desc->async_tx.callback(
214 desc->async_tx.callback_param);
215
d38a8c62 216 dma_descriptor_unmap(&desc->async_tx);
ff7b0479
SB
217 }
218
219 /* run dependent operations */
07f2211e 220 dma_run_dependencies(&desc->async_tx);
ff7b0479
SB
221
222 return cookie;
223}
224
225static int
0951e728 226mv_chan_clean_completed_slots(struct mv_xor_chan *mv_chan)
ff7b0479
SB
227{
228 struct mv_xor_desc_slot *iter, *_iter;
229
c98c1781 230 dev_dbg(mv_chan_to_devp(mv_chan), "%s %d\n", __func__, __LINE__);
ff7b0479 231 list_for_each_entry_safe(iter, _iter, &mv_chan->completed_slots,
fbea28a2 232 node) {
ff7b0479 233
fbea28a2
LA
234 if (async_tx_test_ack(&iter->async_tx))
235 list_move_tail(&iter->node, &mv_chan->free_slots);
ff7b0479
SB
236 }
237 return 0;
238}
239
240static int
0951e728
MR
241mv_desc_clean_slot(struct mv_xor_desc_slot *desc,
242 struct mv_xor_chan *mv_chan)
ff7b0479 243{
c98c1781 244 dev_dbg(mv_chan_to_devp(mv_chan), "%s %d: desc %p flags %d\n",
ff7b0479 245 __func__, __LINE__, desc, desc->async_tx.flags);
fbea28a2 246
ff7b0479
SB
247 /* the client is allowed to attach dependent operations
248 * until 'ack' is set
249 */
fbea28a2 250 if (!async_tx_test_ack(&desc->async_tx))
ff7b0479 251 /* move this slot to the completed_slots */
fbea28a2
LA
252 list_move_tail(&desc->node, &mv_chan->completed_slots);
253 else
254 list_move_tail(&desc->node, &mv_chan->free_slots);
ff7b0479 255
ff7b0479
SB
256 return 0;
257}
258
fbeec99a 259/* This function must be called with the mv_xor_chan spinlock held */
0951e728 260static void mv_chan_slot_cleanup(struct mv_xor_chan *mv_chan)
ff7b0479
SB
261{
262 struct mv_xor_desc_slot *iter, *_iter;
263 dma_cookie_t cookie = 0;
264 int busy = mv_chan_is_busy(mv_chan);
265 u32 current_desc = mv_chan_get_current_desc(mv_chan);
9136291f
LA
266 int current_cleaned = 0;
267 struct mv_xor_desc *hw_desc;
ff7b0479 268
c98c1781
TP
269 dev_dbg(mv_chan_to_devp(mv_chan), "%s %d\n", __func__, __LINE__);
270 dev_dbg(mv_chan_to_devp(mv_chan), "current_desc %x\n", current_desc);
0951e728 271 mv_chan_clean_completed_slots(mv_chan);
ff7b0479
SB
272
273 /* free completed slots from the chain starting with
274 * the oldest descriptor
275 */
276
277 list_for_each_entry_safe(iter, _iter, &mv_chan->chain,
fbea28a2 278 node) {
ff7b0479 279
9136291f
LA
280 /* clean finished descriptors */
281 hw_desc = iter->hw_desc;
282 if (hw_desc->status & XOR_DESC_SUCCESS) {
0951e728
MR
283 cookie = mv_desc_run_tx_complete_actions(iter, mv_chan,
284 cookie);
ff7b0479 285
9136291f 286 /* done processing desc, clean slot */
0951e728 287 mv_desc_clean_slot(iter, mv_chan);
9136291f
LA
288
289 /* break if we did cleaned the current */
290 if (iter->async_tx.phys == current_desc) {
291 current_cleaned = 1;
292 break;
293 }
294 } else {
295 if (iter->async_tx.phys == current_desc) {
296 current_cleaned = 0;
ff7b0479 297 break;
9136291f 298 }
ff7b0479 299 }
ff7b0479
SB
300 }
301
302 if ((busy == 0) && !list_empty(&mv_chan->chain)) {
9136291f
LA
303 if (current_cleaned) {
304 /*
305 * current descriptor cleaned and removed, run
306 * from list head
307 */
308 iter = list_entry(mv_chan->chain.next,
309 struct mv_xor_desc_slot,
fbea28a2 310 node);
0951e728 311 mv_chan_start_new_chain(mv_chan, iter);
9136291f 312 } else {
fbea28a2 313 if (!list_is_last(&iter->node, &mv_chan->chain)) {
9136291f
LA
314 /*
315 * descriptors are still waiting after
316 * current, trigger them
317 */
fbea28a2 318 iter = list_entry(iter->node.next,
9136291f 319 struct mv_xor_desc_slot,
fbea28a2 320 node);
0951e728 321 mv_chan_start_new_chain(mv_chan, iter);
9136291f
LA
322 } else {
323 /*
324 * some descriptors are still waiting
325 * to be cleaned
326 */
327 tasklet_schedule(&mv_chan->irq_tasklet);
328 }
329 }
ff7b0479
SB
330 }
331
332 if (cookie > 0)
98817b99 333 mv_chan->dmachan.completed_cookie = cookie;
ff7b0479
SB
334}
335
ff7b0479
SB
336static void mv_xor_tasklet(unsigned long data)
337{
338 struct mv_xor_chan *chan = (struct mv_xor_chan *) data;
e43147ac
EG
339
340 spin_lock_bh(&chan->lock);
0951e728 341 mv_chan_slot_cleanup(chan);
e43147ac 342 spin_unlock_bh(&chan->lock);
ff7b0479
SB
343}
344
345static struct mv_xor_desc_slot *
0951e728 346mv_chan_alloc_slot(struct mv_xor_chan *mv_chan)
ff7b0479 347{
fbea28a2 348 struct mv_xor_desc_slot *iter;
ff7b0479 349
fbea28a2
LA
350 spin_lock_bh(&mv_chan->lock);
351
352 if (!list_empty(&mv_chan->free_slots)) {
353 iter = list_first_entry(&mv_chan->free_slots,
354 struct mv_xor_desc_slot,
355 node);
356
357 list_move_tail(&iter->node, &mv_chan->allocated_slots);
358
359 spin_unlock_bh(&mv_chan->lock);
ff7b0479 360
dfc97661
LA
361 /* pre-ack descriptor */
362 async_tx_ack(&iter->async_tx);
dfc97661 363 iter->async_tx.cookie = -EBUSY;
dfc97661
LA
364
365 return iter;
366
ff7b0479 367 }
fbea28a2
LA
368
369 spin_unlock_bh(&mv_chan->lock);
ff7b0479
SB
370
371 /* try to free some slots if the allocation fails */
372 tasklet_schedule(&mv_chan->irq_tasklet);
373
374 return NULL;
375}
376
ff7b0479
SB
377/************************ DMA engine API functions ****************************/
378static dma_cookie_t
379mv_xor_tx_submit(struct dma_async_tx_descriptor *tx)
380{
381 struct mv_xor_desc_slot *sw_desc = to_mv_xor_slot(tx);
382 struct mv_xor_chan *mv_chan = to_mv_xor_chan(tx->chan);
dfc97661 383 struct mv_xor_desc_slot *old_chain_tail;
ff7b0479
SB
384 dma_cookie_t cookie;
385 int new_hw_chain = 1;
386
c98c1781 387 dev_dbg(mv_chan_to_devp(mv_chan),
ff7b0479
SB
388 "%s sw_desc %p: async_tx %p\n",
389 __func__, sw_desc, &sw_desc->async_tx);
390
ff7b0479 391 spin_lock_bh(&mv_chan->lock);
884485e1 392 cookie = dma_cookie_assign(tx);
ff7b0479
SB
393
394 if (list_empty(&mv_chan->chain))
fbea28a2 395 list_move_tail(&sw_desc->node, &mv_chan->chain);
ff7b0479
SB
396 else {
397 new_hw_chain = 0;
398
399 old_chain_tail = list_entry(mv_chan->chain.prev,
400 struct mv_xor_desc_slot,
fbea28a2
LA
401 node);
402 list_move_tail(&sw_desc->node, &mv_chan->chain);
ff7b0479 403
31fd8f5b
OJ
404 dev_dbg(mv_chan_to_devp(mv_chan), "Append to last desc %pa\n",
405 &old_chain_tail->async_tx.phys);
ff7b0479
SB
406
407 /* fix up the hardware chain */
dfc97661 408 mv_desc_set_next_desc(old_chain_tail, sw_desc->async_tx.phys);
ff7b0479
SB
409
410 /* if the channel is not busy */
411 if (!mv_chan_is_busy(mv_chan)) {
412 u32 current_desc = mv_chan_get_current_desc(mv_chan);
413 /*
414 * and the curren desc is the end of the chain before
415 * the append, then we need to start the channel
416 */
417 if (current_desc == old_chain_tail->async_tx.phys)
418 new_hw_chain = 1;
419 }
420 }
421
422 if (new_hw_chain)
0951e728 423 mv_chan_start_new_chain(mv_chan, sw_desc);
ff7b0479 424
ff7b0479
SB
425 spin_unlock_bh(&mv_chan->lock);
426
427 return cookie;
428}
429
430/* returns the number of allocated descriptors */
aa1e6f1a 431static int mv_xor_alloc_chan_resources(struct dma_chan *chan)
ff7b0479 432{
31fd8f5b
OJ
433 void *virt_desc;
434 dma_addr_t dma_desc;
ff7b0479
SB
435 int idx;
436 struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
437 struct mv_xor_desc_slot *slot = NULL;
b503fa01 438 int num_descs_in_pool = MV_XOR_POOL_SIZE/MV_XOR_SLOT_SIZE;
ff7b0479
SB
439
440 /* Allocate descriptor slots */
441 idx = mv_chan->slots_allocated;
442 while (idx < num_descs_in_pool) {
443 slot = kzalloc(sizeof(*slot), GFP_KERNEL);
444 if (!slot) {
b8291dde
EG
445 dev_info(mv_chan_to_devp(mv_chan),
446 "channel only initialized %d descriptor slots",
447 idx);
ff7b0479
SB
448 break;
449 }
31fd8f5b
OJ
450 virt_desc = mv_chan->dma_desc_pool_virt;
451 slot->hw_desc = virt_desc + idx * MV_XOR_SLOT_SIZE;
ff7b0479
SB
452
453 dma_async_tx_descriptor_init(&slot->async_tx, chan);
454 slot->async_tx.tx_submit = mv_xor_tx_submit;
fbea28a2 455 INIT_LIST_HEAD(&slot->node);
31fd8f5b
OJ
456 dma_desc = mv_chan->dma_desc_pool;
457 slot->async_tx.phys = dma_desc + idx * MV_XOR_SLOT_SIZE;
ff7b0479
SB
458 slot->idx = idx++;
459
460 spin_lock_bh(&mv_chan->lock);
461 mv_chan->slots_allocated = idx;
fbea28a2 462 list_add_tail(&slot->node, &mv_chan->free_slots);
ff7b0479
SB
463 spin_unlock_bh(&mv_chan->lock);
464 }
465
c98c1781 466 dev_dbg(mv_chan_to_devp(mv_chan),
fbea28a2
LA
467 "allocated %d descriptor slots\n",
468 mv_chan->slots_allocated);
ff7b0479
SB
469
470 return mv_chan->slots_allocated ? : -ENOMEM;
471}
472
ff7b0479
SB
473static struct dma_async_tx_descriptor *
474mv_xor_prep_dma_xor(struct dma_chan *chan, dma_addr_t dest, dma_addr_t *src,
475 unsigned int src_cnt, size_t len, unsigned long flags)
476{
477 struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
dfc97661 478 struct mv_xor_desc_slot *sw_desc;
ff7b0479
SB
479
480 if (unlikely(len < MV_XOR_MIN_BYTE_COUNT))
481 return NULL;
482
7912d300 483 BUG_ON(len > MV_XOR_MAX_BYTE_COUNT);
ff7b0479 484
c98c1781 485 dev_dbg(mv_chan_to_devp(mv_chan),
bc822e12 486 "%s src_cnt: %d len: %zu dest %pad flags: %ld\n",
31fd8f5b 487 __func__, src_cnt, len, &dest, flags);
ff7b0479 488
0951e728 489 sw_desc = mv_chan_alloc_slot(mv_chan);
ff7b0479
SB
490 if (sw_desc) {
491 sw_desc->type = DMA_XOR;
492 sw_desc->async_tx.flags = flags;
ba87d137 493 mv_desc_init(sw_desc, dest, len, flags);
6f166312
LA
494 if (mv_chan->op_in_desc == XOR_MODE_IN_DESC)
495 mv_desc_set_mode(sw_desc);
ff7b0479 496 while (src_cnt--)
dfc97661 497 mv_desc_set_src_addr(sw_desc, src_cnt, src[src_cnt]);
ff7b0479 498 }
fbea28a2 499
c98c1781 500 dev_dbg(mv_chan_to_devp(mv_chan),
ff7b0479
SB
501 "%s sw_desc %p async_tx %p \n",
502 __func__, sw_desc, &sw_desc->async_tx);
503 return sw_desc ? &sw_desc->async_tx : NULL;
504}
505
3e4f52e2
LA
506static struct dma_async_tx_descriptor *
507mv_xor_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
508 size_t len, unsigned long flags)
509{
510 /*
511 * A MEMCPY operation is identical to an XOR operation with only
512 * a single source address.
513 */
514 return mv_xor_prep_dma_xor(chan, dest, &src, 1, len, flags);
515}
516
22843545
LA
517static struct dma_async_tx_descriptor *
518mv_xor_prep_dma_interrupt(struct dma_chan *chan, unsigned long flags)
519{
520 struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
521 dma_addr_t src, dest;
522 size_t len;
523
524 src = mv_chan->dummy_src_addr;
525 dest = mv_chan->dummy_dst_addr;
526 len = MV_XOR_MIN_BYTE_COUNT;
527
528 /*
529 * We implement the DMA_INTERRUPT operation as a minimum sized
530 * XOR operation with a single dummy source address.
531 */
532 return mv_xor_prep_dma_xor(chan, dest, &src, 1, len, flags);
533}
534
ff7b0479
SB
535static void mv_xor_free_chan_resources(struct dma_chan *chan)
536{
537 struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
538 struct mv_xor_desc_slot *iter, *_iter;
539 int in_use_descs = 0;
540
ff7b0479 541 spin_lock_bh(&mv_chan->lock);
e43147ac 542
0951e728 543 mv_chan_slot_cleanup(mv_chan);
ff7b0479 544
ff7b0479 545 list_for_each_entry_safe(iter, _iter, &mv_chan->chain,
fbea28a2 546 node) {
ff7b0479 547 in_use_descs++;
fbea28a2 548 list_move_tail(&iter->node, &mv_chan->free_slots);
ff7b0479
SB
549 }
550 list_for_each_entry_safe(iter, _iter, &mv_chan->completed_slots,
fbea28a2
LA
551 node) {
552 in_use_descs++;
553 list_move_tail(&iter->node, &mv_chan->free_slots);
554 }
555 list_for_each_entry_safe(iter, _iter, &mv_chan->allocated_slots,
556 node) {
ff7b0479 557 in_use_descs++;
fbea28a2 558 list_move_tail(&iter->node, &mv_chan->free_slots);
ff7b0479
SB
559 }
560 list_for_each_entry_safe_reverse(
fbea28a2
LA
561 iter, _iter, &mv_chan->free_slots, node) {
562 list_del(&iter->node);
ff7b0479
SB
563 kfree(iter);
564 mv_chan->slots_allocated--;
565 }
ff7b0479 566
c98c1781 567 dev_dbg(mv_chan_to_devp(mv_chan), "%s slots_allocated %d\n",
ff7b0479
SB
568 __func__, mv_chan->slots_allocated);
569 spin_unlock_bh(&mv_chan->lock);
570
571 if (in_use_descs)
c98c1781 572 dev_err(mv_chan_to_devp(mv_chan),
ff7b0479
SB
573 "freeing %d in use descriptors!\n", in_use_descs);
574}
575
576/**
07934481 577 * mv_xor_status - poll the status of an XOR transaction
ff7b0479
SB
578 * @chan: XOR channel handle
579 * @cookie: XOR transaction identifier
07934481 580 * @txstate: XOR transactions state holder (or NULL)
ff7b0479 581 */
07934481 582static enum dma_status mv_xor_status(struct dma_chan *chan,
ff7b0479 583 dma_cookie_t cookie,
07934481 584 struct dma_tx_state *txstate)
ff7b0479
SB
585{
586 struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
ff7b0479
SB
587 enum dma_status ret;
588
96a2af41 589 ret = dma_cookie_status(chan, cookie, txstate);
890766d2 590 if (ret == DMA_COMPLETE)
ff7b0479 591 return ret;
e43147ac
EG
592
593 spin_lock_bh(&mv_chan->lock);
0951e728 594 mv_chan_slot_cleanup(mv_chan);
e43147ac 595 spin_unlock_bh(&mv_chan->lock);
ff7b0479 596
96a2af41 597 return dma_cookie_status(chan, cookie, txstate);
ff7b0479
SB
598}
599
0951e728 600static void mv_chan_dump_regs(struct mv_xor_chan *chan)
ff7b0479
SB
601{
602 u32 val;
603
5733c38a 604 val = readl_relaxed(XOR_CONFIG(chan));
1ba151cd 605 dev_err(mv_chan_to_devp(chan), "config 0x%08x\n", val);
ff7b0479 606
5733c38a 607 val = readl_relaxed(XOR_ACTIVATION(chan));
1ba151cd 608 dev_err(mv_chan_to_devp(chan), "activation 0x%08x\n", val);
ff7b0479 609
5733c38a 610 val = readl_relaxed(XOR_INTR_CAUSE(chan));
1ba151cd 611 dev_err(mv_chan_to_devp(chan), "intr cause 0x%08x\n", val);
ff7b0479 612
5733c38a 613 val = readl_relaxed(XOR_INTR_MASK(chan));
1ba151cd 614 dev_err(mv_chan_to_devp(chan), "intr mask 0x%08x\n", val);
ff7b0479 615
5733c38a 616 val = readl_relaxed(XOR_ERROR_CAUSE(chan));
1ba151cd 617 dev_err(mv_chan_to_devp(chan), "error cause 0x%08x\n", val);
ff7b0479 618
5733c38a 619 val = readl_relaxed(XOR_ERROR_ADDR(chan));
1ba151cd 620 dev_err(mv_chan_to_devp(chan), "error addr 0x%08x\n", val);
ff7b0479
SB
621}
622
0951e728
MR
623static void mv_chan_err_interrupt_handler(struct mv_xor_chan *chan,
624 u32 intr_cause)
ff7b0479 625{
0e7488ed
EG
626 if (intr_cause & XOR_INT_ERR_DECODE) {
627 dev_dbg(mv_chan_to_devp(chan), "ignoring address decode error\n");
628 return;
ff7b0479
SB
629 }
630
0e7488ed 631 dev_err(mv_chan_to_devp(chan), "error on chan %d. intr cause 0x%08x\n",
a3fc74bc 632 chan->idx, intr_cause);
ff7b0479 633
0951e728 634 mv_chan_dump_regs(chan);
0e7488ed 635 WARN_ON(1);
ff7b0479
SB
636}
637
638static irqreturn_t mv_xor_interrupt_handler(int irq, void *data)
639{
640 struct mv_xor_chan *chan = data;
641 u32 intr_cause = mv_chan_get_intr_cause(chan);
642
c98c1781 643 dev_dbg(mv_chan_to_devp(chan), "intr cause %x\n", intr_cause);
ff7b0479 644
0e7488ed 645 if (intr_cause & XOR_INTR_ERRORS)
0951e728 646 mv_chan_err_interrupt_handler(chan, intr_cause);
ff7b0479
SB
647
648 tasklet_schedule(&chan->irq_tasklet);
649
0951e728 650 mv_chan_clear_eoc_cause(chan);
ff7b0479
SB
651
652 return IRQ_HANDLED;
653}
654
655static void mv_xor_issue_pending(struct dma_chan *chan)
656{
657 struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
658
659 if (mv_chan->pending >= MV_XOR_THRESHOLD) {
660 mv_chan->pending = 0;
661 mv_chan_activate(mv_chan);
662 }
663}
664
665/*
666 * Perform a transaction to verify the HW works.
667 */
ff7b0479 668
0951e728 669static int mv_chan_memcpy_self_test(struct mv_xor_chan *mv_chan)
ff7b0479 670{
b8c01d25 671 int i, ret;
ff7b0479
SB
672 void *src, *dest;
673 dma_addr_t src_dma, dest_dma;
674 struct dma_chan *dma_chan;
675 dma_cookie_t cookie;
676 struct dma_async_tx_descriptor *tx;
d16695a7 677 struct dmaengine_unmap_data *unmap;
ff7b0479 678 int err = 0;
ff7b0479 679
d16695a7 680 src = kmalloc(sizeof(u8) * PAGE_SIZE, GFP_KERNEL);
ff7b0479
SB
681 if (!src)
682 return -ENOMEM;
683
d16695a7 684 dest = kzalloc(sizeof(u8) * PAGE_SIZE, GFP_KERNEL);
ff7b0479
SB
685 if (!dest) {
686 kfree(src);
687 return -ENOMEM;
688 }
689
690 /* Fill in src buffer */
d16695a7 691 for (i = 0; i < PAGE_SIZE; i++)
ff7b0479
SB
692 ((u8 *) src)[i] = (u8)i;
693
275cc0c8 694 dma_chan = &mv_chan->dmachan;
aa1e6f1a 695 if (mv_xor_alloc_chan_resources(dma_chan) < 1) {
ff7b0479
SB
696 err = -ENODEV;
697 goto out;
698 }
699
d16695a7
EG
700 unmap = dmaengine_get_unmap_data(dma_chan->device->dev, 2, GFP_KERNEL);
701 if (!unmap) {
702 err = -ENOMEM;
703 goto free_resources;
704 }
705
706 src_dma = dma_map_page(dma_chan->device->dev, virt_to_page(src), 0,
707 PAGE_SIZE, DMA_TO_DEVICE);
d16695a7 708 unmap->addr[0] = src_dma;
ff7b0479 709
b8c01d25
EG
710 ret = dma_mapping_error(dma_chan->device->dev, src_dma);
711 if (ret) {
712 err = -ENOMEM;
713 goto free_resources;
714 }
715 unmap->to_cnt = 1;
716
d16695a7
EG
717 dest_dma = dma_map_page(dma_chan->device->dev, virt_to_page(dest), 0,
718 PAGE_SIZE, DMA_FROM_DEVICE);
d16695a7
EG
719 unmap->addr[1] = dest_dma;
720
b8c01d25
EG
721 ret = dma_mapping_error(dma_chan->device->dev, dest_dma);
722 if (ret) {
723 err = -ENOMEM;
724 goto free_resources;
725 }
726 unmap->from_cnt = 1;
d16695a7 727 unmap->len = PAGE_SIZE;
ff7b0479
SB
728
729 tx = mv_xor_prep_dma_memcpy(dma_chan, dest_dma, src_dma,
d16695a7 730 PAGE_SIZE, 0);
b8c01d25
EG
731 if (!tx) {
732 dev_err(dma_chan->device->dev,
733 "Self-test cannot prepare operation, disabling\n");
734 err = -ENODEV;
735 goto free_resources;
736 }
737
ff7b0479 738 cookie = mv_xor_tx_submit(tx);
b8c01d25
EG
739 if (dma_submit_error(cookie)) {
740 dev_err(dma_chan->device->dev,
741 "Self-test submit error, disabling\n");
742 err = -ENODEV;
743 goto free_resources;
744 }
745
ff7b0479
SB
746 mv_xor_issue_pending(dma_chan);
747 async_tx_ack(tx);
748 msleep(1);
749
07934481 750 if (mv_xor_status(dma_chan, cookie, NULL) !=
b3efb8fc 751 DMA_COMPLETE) {
a3fc74bc
TP
752 dev_err(dma_chan->device->dev,
753 "Self-test copy timed out, disabling\n");
ff7b0479
SB
754 err = -ENODEV;
755 goto free_resources;
756 }
757
c35064c4 758 dma_sync_single_for_cpu(dma_chan->device->dev, dest_dma,
d16695a7
EG
759 PAGE_SIZE, DMA_FROM_DEVICE);
760 if (memcmp(src, dest, PAGE_SIZE)) {
a3fc74bc
TP
761 dev_err(dma_chan->device->dev,
762 "Self-test copy failed compare, disabling\n");
ff7b0479
SB
763 err = -ENODEV;
764 goto free_resources;
765 }
766
767free_resources:
d16695a7 768 dmaengine_unmap_put(unmap);
ff7b0479
SB
769 mv_xor_free_chan_resources(dma_chan);
770out:
771 kfree(src);
772 kfree(dest);
773 return err;
774}
775
776#define MV_XOR_NUM_SRC_TEST 4 /* must be <= 15 */
463a1f8b 777static int
0951e728 778mv_chan_xor_self_test(struct mv_xor_chan *mv_chan)
ff7b0479 779{
b8c01d25 780 int i, src_idx, ret;
ff7b0479
SB
781 struct page *dest;
782 struct page *xor_srcs[MV_XOR_NUM_SRC_TEST];
783 dma_addr_t dma_srcs[MV_XOR_NUM_SRC_TEST];
784 dma_addr_t dest_dma;
785 struct dma_async_tx_descriptor *tx;
d16695a7 786 struct dmaengine_unmap_data *unmap;
ff7b0479
SB
787 struct dma_chan *dma_chan;
788 dma_cookie_t cookie;
789 u8 cmp_byte = 0;
790 u32 cmp_word;
791 int err = 0;
d16695a7 792 int src_count = MV_XOR_NUM_SRC_TEST;
ff7b0479 793
d16695a7 794 for (src_idx = 0; src_idx < src_count; src_idx++) {
ff7b0479 795 xor_srcs[src_idx] = alloc_page(GFP_KERNEL);
a09b09ae
RK
796 if (!xor_srcs[src_idx]) {
797 while (src_idx--)
ff7b0479 798 __free_page(xor_srcs[src_idx]);
a09b09ae
RK
799 return -ENOMEM;
800 }
ff7b0479
SB
801 }
802
803 dest = alloc_page(GFP_KERNEL);
a09b09ae
RK
804 if (!dest) {
805 while (src_idx--)
ff7b0479 806 __free_page(xor_srcs[src_idx]);
a09b09ae
RK
807 return -ENOMEM;
808 }
ff7b0479
SB
809
810 /* Fill in src buffers */
d16695a7 811 for (src_idx = 0; src_idx < src_count; src_idx++) {
ff7b0479
SB
812 u8 *ptr = page_address(xor_srcs[src_idx]);
813 for (i = 0; i < PAGE_SIZE; i++)
814 ptr[i] = (1 << src_idx);
815 }
816
d16695a7 817 for (src_idx = 0; src_idx < src_count; src_idx++)
ff7b0479
SB
818 cmp_byte ^= (u8) (1 << src_idx);
819
820 cmp_word = (cmp_byte << 24) | (cmp_byte << 16) |
821 (cmp_byte << 8) | cmp_byte;
822
823 memset(page_address(dest), 0, PAGE_SIZE);
824
275cc0c8 825 dma_chan = &mv_chan->dmachan;
aa1e6f1a 826 if (mv_xor_alloc_chan_resources(dma_chan) < 1) {
ff7b0479
SB
827 err = -ENODEV;
828 goto out;
829 }
830
d16695a7
EG
831 unmap = dmaengine_get_unmap_data(dma_chan->device->dev, src_count + 1,
832 GFP_KERNEL);
833 if (!unmap) {
834 err = -ENOMEM;
835 goto free_resources;
836 }
837
ff7b0479 838 /* test xor */
d16695a7
EG
839 for (i = 0; i < src_count; i++) {
840 unmap->addr[i] = dma_map_page(dma_chan->device->dev, xor_srcs[i],
841 0, PAGE_SIZE, DMA_TO_DEVICE);
842 dma_srcs[i] = unmap->addr[i];
b8c01d25
EG
843 ret = dma_mapping_error(dma_chan->device->dev, unmap->addr[i]);
844 if (ret) {
845 err = -ENOMEM;
846 goto free_resources;
847 }
d16695a7
EG
848 unmap->to_cnt++;
849 }
ff7b0479 850
d16695a7
EG
851 unmap->addr[src_count] = dma_map_page(dma_chan->device->dev, dest, 0, PAGE_SIZE,
852 DMA_FROM_DEVICE);
853 dest_dma = unmap->addr[src_count];
b8c01d25
EG
854 ret = dma_mapping_error(dma_chan->device->dev, unmap->addr[src_count]);
855 if (ret) {
856 err = -ENOMEM;
857 goto free_resources;
858 }
d16695a7
EG
859 unmap->from_cnt = 1;
860 unmap->len = PAGE_SIZE;
ff7b0479
SB
861
862 tx = mv_xor_prep_dma_xor(dma_chan, dest_dma, dma_srcs,
d16695a7 863 src_count, PAGE_SIZE, 0);
b8c01d25
EG
864 if (!tx) {
865 dev_err(dma_chan->device->dev,
866 "Self-test cannot prepare operation, disabling\n");
867 err = -ENODEV;
868 goto free_resources;
869 }
ff7b0479
SB
870
871 cookie = mv_xor_tx_submit(tx);
b8c01d25
EG
872 if (dma_submit_error(cookie)) {
873 dev_err(dma_chan->device->dev,
874 "Self-test submit error, disabling\n");
875 err = -ENODEV;
876 goto free_resources;
877 }
878
ff7b0479
SB
879 mv_xor_issue_pending(dma_chan);
880 async_tx_ack(tx);
881 msleep(8);
882
07934481 883 if (mv_xor_status(dma_chan, cookie, NULL) !=
b3efb8fc 884 DMA_COMPLETE) {
a3fc74bc
TP
885 dev_err(dma_chan->device->dev,
886 "Self-test xor timed out, disabling\n");
ff7b0479
SB
887 err = -ENODEV;
888 goto free_resources;
889 }
890
c35064c4 891 dma_sync_single_for_cpu(dma_chan->device->dev, dest_dma,
ff7b0479
SB
892 PAGE_SIZE, DMA_FROM_DEVICE);
893 for (i = 0; i < (PAGE_SIZE / sizeof(u32)); i++) {
894 u32 *ptr = page_address(dest);
895 if (ptr[i] != cmp_word) {
a3fc74bc 896 dev_err(dma_chan->device->dev,
1ba151cd
JP
897 "Self-test xor failed compare, disabling. index %d, data %x, expected %x\n",
898 i, ptr[i], cmp_word);
ff7b0479
SB
899 err = -ENODEV;
900 goto free_resources;
901 }
902 }
903
904free_resources:
d16695a7 905 dmaengine_unmap_put(unmap);
ff7b0479
SB
906 mv_xor_free_chan_resources(dma_chan);
907out:
d16695a7 908 src_idx = src_count;
ff7b0479
SB
909 while (src_idx--)
910 __free_page(xor_srcs[src_idx]);
911 __free_page(dest);
912 return err;
913}
914
1ef48a26 915static int mv_xor_channel_remove(struct mv_xor_chan *mv_chan)
ff7b0479 916{
ff7b0479 917 struct dma_chan *chan, *_chan;
1ef48a26 918 struct device *dev = mv_chan->dmadev.dev;
ff7b0479 919
1ef48a26 920 dma_async_device_unregister(&mv_chan->dmadev);
ff7b0479 921
b503fa01 922 dma_free_coherent(dev, MV_XOR_POOL_SIZE,
1ef48a26 923 mv_chan->dma_desc_pool_virt, mv_chan->dma_desc_pool);
22843545
LA
924 dma_unmap_single(dev, mv_chan->dummy_src_addr,
925 MV_XOR_MIN_BYTE_COUNT, DMA_FROM_DEVICE);
926 dma_unmap_single(dev, mv_chan->dummy_dst_addr,
927 MV_XOR_MIN_BYTE_COUNT, DMA_TO_DEVICE);
ff7b0479 928
1ef48a26 929 list_for_each_entry_safe(chan, _chan, &mv_chan->dmadev.channels,
a6b4a9d2 930 device_node) {
ff7b0479
SB
931 list_del(&chan->device_node);
932 }
933
88eb92cb
TP
934 free_irq(mv_chan->irq, mv_chan);
935
ff7b0479
SB
936 return 0;
937}
938
1ef48a26 939static struct mv_xor_chan *
297eedba 940mv_xor_channel_add(struct mv_xor_device *xordev,
a6b4a9d2 941 struct platform_device *pdev,
dd130c65 942 int idx, dma_cap_mask_t cap_mask, int irq)
ff7b0479
SB
943{
944 int ret = 0;
ff7b0479
SB
945 struct mv_xor_chan *mv_chan;
946 struct dma_device *dma_dev;
ff7b0479 947
1ef48a26 948 mv_chan = devm_kzalloc(&pdev->dev, sizeof(*mv_chan), GFP_KERNEL);
a577659f
SK
949 if (!mv_chan)
950 return ERR_PTR(-ENOMEM);
ff7b0479 951
9aedbdba 952 mv_chan->idx = idx;
88eb92cb 953 mv_chan->irq = irq;
dd130c65
GC
954 if (xordev->xor_type == XOR_ORION)
955 mv_chan->op_in_desc = XOR_MODE_IN_REG;
956 else
957 mv_chan->op_in_desc = XOR_MODE_IN_DESC;
ff7b0479 958
1ef48a26 959 dma_dev = &mv_chan->dmadev;
ff7b0479 960
22843545
LA
961 /*
962 * These source and destination dummy buffers are used to implement
963 * a DMA_INTERRUPT operation as a minimum-sized XOR operation.
964 * Hence, we only need to map the buffers at initialization-time.
965 */
966 mv_chan->dummy_src_addr = dma_map_single(dma_dev->dev,
967 mv_chan->dummy_src, MV_XOR_MIN_BYTE_COUNT, DMA_FROM_DEVICE);
968 mv_chan->dummy_dst_addr = dma_map_single(dma_dev->dev,
969 mv_chan->dummy_dst, MV_XOR_MIN_BYTE_COUNT, DMA_TO_DEVICE);
970
ff7b0479
SB
971 /* allocate coherent memory for hardware descriptors
972 * note: writecombine gives slightly better performance, but
973 * requires that we explicitly flush the writes
974 */
1ef48a26 975 mv_chan->dma_desc_pool_virt =
f6e45661
LR
976 dma_alloc_wc(&pdev->dev, MV_XOR_POOL_SIZE, &mv_chan->dma_desc_pool,
977 GFP_KERNEL);
1ef48a26 978 if (!mv_chan->dma_desc_pool_virt)
a6b4a9d2 979 return ERR_PTR(-ENOMEM);
ff7b0479
SB
980
981 /* discover transaction capabilites from the platform data */
a6b4a9d2 982 dma_dev->cap_mask = cap_mask;
ff7b0479
SB
983
984 INIT_LIST_HEAD(&dma_dev->channels);
985
986 /* set base routines */
987 dma_dev->device_alloc_chan_resources = mv_xor_alloc_chan_resources;
988 dma_dev->device_free_chan_resources = mv_xor_free_chan_resources;
07934481 989 dma_dev->device_tx_status = mv_xor_status;
ff7b0479
SB
990 dma_dev->device_issue_pending = mv_xor_issue_pending;
991 dma_dev->dev = &pdev->dev;
992
993 /* set prep routines based on capability */
22843545
LA
994 if (dma_has_cap(DMA_INTERRUPT, dma_dev->cap_mask))
995 dma_dev->device_prep_dma_interrupt = mv_xor_prep_dma_interrupt;
ff7b0479
SB
996 if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask))
997 dma_dev->device_prep_dma_memcpy = mv_xor_prep_dma_memcpy;
ff7b0479 998 if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
c019894e 999 dma_dev->max_xor = 8;
ff7b0479
SB
1000 dma_dev->device_prep_dma_xor = mv_xor_prep_dma_xor;
1001 }
1002
297eedba 1003 mv_chan->mmr_base = xordev->xor_base;
82a1402e 1004 mv_chan->mmr_high_base = xordev->xor_high_base;
ff7b0479
SB
1005 tasklet_init(&mv_chan->irq_tasklet, mv_xor_tasklet, (unsigned long)
1006 mv_chan);
1007
1008 /* clear errors before enabling interrupts */
0951e728 1009 mv_chan_clear_err_status(mv_chan);
ff7b0479 1010
2d0a0745
TP
1011 ret = request_irq(mv_chan->irq, mv_xor_interrupt_handler,
1012 0, dev_name(&pdev->dev), mv_chan);
ff7b0479
SB
1013 if (ret)
1014 goto err_free_dma;
1015
1016 mv_chan_unmask_interrupts(mv_chan);
1017
6f166312 1018 if (mv_chan->op_in_desc == XOR_MODE_IN_DESC)
81aafb3e 1019 mv_chan_set_mode(mv_chan, XOR_OPERATION_MODE_IN_DESC);
6f166312 1020 else
81aafb3e 1021 mv_chan_set_mode(mv_chan, XOR_OPERATION_MODE_XOR);
ff7b0479
SB
1022
1023 spin_lock_init(&mv_chan->lock);
1024 INIT_LIST_HEAD(&mv_chan->chain);
1025 INIT_LIST_HEAD(&mv_chan->completed_slots);
fbea28a2
LA
1026 INIT_LIST_HEAD(&mv_chan->free_slots);
1027 INIT_LIST_HEAD(&mv_chan->allocated_slots);
98817b99
TP
1028 mv_chan->dmachan.device = dma_dev;
1029 dma_cookie_init(&mv_chan->dmachan);
ff7b0479 1030
98817b99 1031 list_add_tail(&mv_chan->dmachan.device_node, &dma_dev->channels);
ff7b0479
SB
1032
1033 if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask)) {
0951e728 1034 ret = mv_chan_memcpy_self_test(mv_chan);
ff7b0479
SB
1035 dev_dbg(&pdev->dev, "memcpy self test returned %d\n", ret);
1036 if (ret)
2d0a0745 1037 goto err_free_irq;
ff7b0479
SB
1038 }
1039
1040 if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
0951e728 1041 ret = mv_chan_xor_self_test(mv_chan);
ff7b0479
SB
1042 dev_dbg(&pdev->dev, "xor self test returned %d\n", ret);
1043 if (ret)
2d0a0745 1044 goto err_free_irq;
ff7b0479
SB
1045 }
1046
6f166312
LA
1047 dev_info(&pdev->dev, "Marvell XOR (%s): ( %s%s%s)\n",
1048 mv_chan->op_in_desc ? "Descriptor Mode" : "Registers Mode",
1ba151cd 1049 dma_has_cap(DMA_XOR, dma_dev->cap_mask) ? "xor " : "",
1ba151cd
JP
1050 dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask) ? "cpy " : "",
1051 dma_has_cap(DMA_INTERRUPT, dma_dev->cap_mask) ? "intr " : "");
ff7b0479
SB
1052
1053 dma_async_device_register(dma_dev);
1ef48a26 1054 return mv_chan;
ff7b0479 1055
2d0a0745
TP
1056err_free_irq:
1057 free_irq(mv_chan->irq, mv_chan);
ff7b0479 1058 err_free_dma:
b503fa01 1059 dma_free_coherent(&pdev->dev, MV_XOR_POOL_SIZE,
1ef48a26 1060 mv_chan->dma_desc_pool_virt, mv_chan->dma_desc_pool);
a6b4a9d2 1061 return ERR_PTR(ret);
ff7b0479
SB
1062}
1063
1064static void
297eedba 1065mv_xor_conf_mbus_windows(struct mv_xor_device *xordev,
63a9332b 1066 const struct mbus_dram_target_info *dram)
ff7b0479 1067{
82a1402e 1068 void __iomem *base = xordev->xor_high_base;
ff7b0479
SB
1069 u32 win_enable = 0;
1070 int i;
1071
1072 for (i = 0; i < 8; i++) {
1073 writel(0, base + WINDOW_BASE(i));
1074 writel(0, base + WINDOW_SIZE(i));
1075 if (i < 4)
1076 writel(0, base + WINDOW_REMAP_HIGH(i));
1077 }
1078
1079 for (i = 0; i < dram->num_cs; i++) {
63a9332b 1080 const struct mbus_dram_window *cs = dram->cs + i;
ff7b0479
SB
1081
1082 writel((cs->base & 0xffff0000) |
1083 (cs->mbus_attr << 8) |
1084 dram->mbus_dram_target_id, base + WINDOW_BASE(i));
1085 writel((cs->size - 1) & 0xffff0000, base + WINDOW_SIZE(i));
1086
1087 win_enable |= (1 << i);
1088 win_enable |= 3 << (16 + (2 * i));
1089 }
1090
1091 writel(win_enable, base + WINDOW_BAR_ENABLE(0));
1092 writel(win_enable, base + WINDOW_BAR_ENABLE(1));
c4b4b732
TP
1093 writel(0, base + WINDOW_OVERRIDE_CTRL(0));
1094 writel(0, base + WINDOW_OVERRIDE_CTRL(1));
ff7b0479
SB
1095}
1096
ac5f0f3f
MW
1097static void
1098mv_xor_conf_mbus_windows_a3700(struct mv_xor_device *xordev)
1099{
1100 void __iomem *base = xordev->xor_high_base;
1101 u32 win_enable = 0;
1102 int i;
1103
1104 for (i = 0; i < 8; i++) {
1105 writel(0, base + WINDOW_BASE(i));
1106 writel(0, base + WINDOW_SIZE(i));
1107 if (i < 4)
1108 writel(0, base + WINDOW_REMAP_HIGH(i));
1109 }
1110 /*
1111 * For Armada3700 open default 4GB Mbus window. The dram
1112 * related configuration are done at AXIS level.
1113 */
1114 writel(0xffff0000, base + WINDOW_SIZE(0));
1115 win_enable |= 1;
1116 win_enable |= 3 << 16;
1117
1118 writel(win_enable, base + WINDOW_BAR_ENABLE(0));
1119 writel(win_enable, base + WINDOW_BAR_ENABLE(1));
1120 writel(0, base + WINDOW_OVERRIDE_CTRL(0));
1121 writel(0, base + WINDOW_OVERRIDE_CTRL(1));
1122}
1123
8b648436
TP
1124/*
1125 * Since this XOR driver is basically used only for RAID5, we don't
1126 * need to care about synchronizing ->suspend with DMA activity,
1127 * because the DMA engine will naturally be quiet due to the block
1128 * devices being suspended.
1129 */
1130static int mv_xor_suspend(struct platform_device *pdev, pm_message_t state)
1131{
1132 struct mv_xor_device *xordev = platform_get_drvdata(pdev);
1133 int i;
1134
1135 for (i = 0; i < MV_XOR_MAX_CHANNELS; i++) {
1136 struct mv_xor_chan *mv_chan = xordev->channels[i];
1137
1138 if (!mv_chan)
1139 continue;
1140
1141 mv_chan->saved_config_reg =
1142 readl_relaxed(XOR_CONFIG(mv_chan));
1143 mv_chan->saved_int_mask_reg =
1144 readl_relaxed(XOR_INTR_MASK(mv_chan));
1145 }
1146
1147 return 0;
1148}
1149
1150static int mv_xor_resume(struct platform_device *dev)
1151{
1152 struct mv_xor_device *xordev = platform_get_drvdata(dev);
1153 const struct mbus_dram_target_info *dram;
1154 int i;
1155
1156 for (i = 0; i < MV_XOR_MAX_CHANNELS; i++) {
1157 struct mv_xor_chan *mv_chan = xordev->channels[i];
1158
1159 if (!mv_chan)
1160 continue;
1161
1162 writel_relaxed(mv_chan->saved_config_reg,
1163 XOR_CONFIG(mv_chan));
1164 writel_relaxed(mv_chan->saved_int_mask_reg,
1165 XOR_INTR_MASK(mv_chan));
1166 }
1167
ac5f0f3f
MW
1168 if (xordev->xor_type == XOR_ARMADA_37XX) {
1169 mv_xor_conf_mbus_windows_a3700(xordev);
1170 return 0;
1171 }
1172
8b648436
TP
1173 dram = mv_mbus_dram_info();
1174 if (dram)
1175 mv_xor_conf_mbus_windows(xordev, dram);
1176
1177 return 0;
1178}
1179
6f166312 1180static const struct of_device_id mv_xor_dt_ids[] = {
dd130c65
GC
1181 { .compatible = "marvell,orion-xor", .data = (void *)XOR_ORION },
1182 { .compatible = "marvell,armada-380-xor", .data = (void *)XOR_ARMADA_38X },
ac5f0f3f 1183 { .compatible = "marvell,armada-3700-xor", .data = (void *)XOR_ARMADA_37XX },
6f166312
LA
1184 {},
1185};
6f166312 1186
77757291 1187static unsigned int mv_xor_engine_count;
6f166312 1188
c2714334 1189static int mv_xor_probe(struct platform_device *pdev)
ff7b0479 1190{
63a9332b 1191 const struct mbus_dram_target_info *dram;
297eedba 1192 struct mv_xor_device *xordev;
d4adcc01 1193 struct mv_xor_platform_data *pdata = dev_get_platdata(&pdev->dev);
ff7b0479 1194 struct resource *res;
77757291 1195 unsigned int max_engines, max_channels;
60d151f3 1196 int i, ret;
ff7b0479 1197
1ba151cd 1198 dev_notice(&pdev->dev, "Marvell shared XOR driver\n");
ff7b0479 1199
297eedba
TP
1200 xordev = devm_kzalloc(&pdev->dev, sizeof(*xordev), GFP_KERNEL);
1201 if (!xordev)
ff7b0479
SB
1202 return -ENOMEM;
1203
1204 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1205 if (!res)
1206 return -ENODEV;
1207
297eedba
TP
1208 xordev->xor_base = devm_ioremap(&pdev->dev, res->start,
1209 resource_size(res));
1210 if (!xordev->xor_base)
ff7b0479
SB
1211 return -EBUSY;
1212
1213 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1214 if (!res)
1215 return -ENODEV;
1216
297eedba
TP
1217 xordev->xor_high_base = devm_ioremap(&pdev->dev, res->start,
1218 resource_size(res));
1219 if (!xordev->xor_high_base)
ff7b0479
SB
1220 return -EBUSY;
1221
297eedba 1222 platform_set_drvdata(pdev, xordev);
ff7b0479 1223
dd130c65
GC
1224
1225 /*
1226 * We need to know which type of XOR device we use before
1227 * setting up. In non-dt case it can only be the legacy one.
1228 */
1229 xordev->xor_type = XOR_ORION;
1230 if (pdev->dev.of_node) {
1231 const struct of_device_id *of_id =
1232 of_match_device(mv_xor_dt_ids,
1233 &pdev->dev);
1234
1235 xordev->xor_type = (uintptr_t)of_id->data;
1236 }
1237
ff7b0479
SB
1238 /*
1239 * (Re-)program MBUS remapping windows if we are asked to.
1240 */
ac5f0f3f
MW
1241 if (xordev->xor_type == XOR_ARMADA_37XX) {
1242 mv_xor_conf_mbus_windows_a3700(xordev);
1243 } else {
1244 dram = mv_mbus_dram_info();
1245 if (dram)
1246 mv_xor_conf_mbus_windows(xordev, dram);
1247 }
ff7b0479 1248
c510182b
AL
1249 /* Not all platforms can gate the clock, so it is not
1250 * an error if the clock does not exists.
1251 */
297eedba
TP
1252 xordev->clk = clk_get(&pdev->dev, NULL);
1253 if (!IS_ERR(xordev->clk))
1254 clk_prepare_enable(xordev->clk);
c510182b 1255
77757291
TP
1256 /*
1257 * We don't want to have more than one channel per CPU in
1258 * order for async_tx to perform well. So we limit the number
1259 * of engines and channels so that we take into account this
1260 * constraint. Note that we also want to use channels from
ac5f0f3f
MW
1261 * separate engines when possible. For dual-CPU Armada 3700
1262 * SoC with single XOR engine allow using its both channels.
77757291
TP
1263 */
1264 max_engines = num_present_cpus();
ac5f0f3f
MW
1265 if (xordev->xor_type == XOR_ARMADA_37XX)
1266 max_channels = num_present_cpus();
1267 else
1268 max_channels = min_t(unsigned int,
1269 MV_XOR_MAX_CHANNELS,
1270 DIV_ROUND_UP(num_present_cpus(), 2));
77757291
TP
1271
1272 if (mv_xor_engine_count >= max_engines)
1273 return 0;
1274
f7d12ef5
TP
1275 if (pdev->dev.of_node) {
1276 struct device_node *np;
1277 int i = 0;
1278
1279 for_each_child_of_node(pdev->dev.of_node, np) {
0be8253f 1280 struct mv_xor_chan *chan;
f7d12ef5
TP
1281 dma_cap_mask_t cap_mask;
1282 int irq;
1283
77757291
TP
1284 if (i >= max_channels)
1285 continue;
1286
f7d12ef5 1287 dma_cap_zero(cap_mask);
6d8f7abd
TP
1288 dma_cap_set(DMA_MEMCPY, cap_mask);
1289 dma_cap_set(DMA_XOR, cap_mask);
1290 dma_cap_set(DMA_INTERRUPT, cap_mask);
f7d12ef5
TP
1291
1292 irq = irq_of_parse_and_map(np, 0);
f8eb9e7d
TP
1293 if (!irq) {
1294 ret = -ENODEV;
f7d12ef5
TP
1295 goto err_channel_add;
1296 }
1297
0be8253f 1298 chan = mv_xor_channel_add(xordev, pdev, i,
dd130c65 1299 cap_mask, irq);
0be8253f
RK
1300 if (IS_ERR(chan)) {
1301 ret = PTR_ERR(chan);
f7d12ef5
TP
1302 irq_dispose_mapping(irq);
1303 goto err_channel_add;
1304 }
1305
0be8253f 1306 xordev->channels[i] = chan;
f7d12ef5
TP
1307 i++;
1308 }
1309 } else if (pdata && pdata->channels) {
77757291 1310 for (i = 0; i < max_channels; i++) {
e39f6ec1 1311 struct mv_xor_channel_data *cd;
0be8253f 1312 struct mv_xor_chan *chan;
60d151f3
TP
1313 int irq;
1314
1315 cd = &pdata->channels[i];
1316 if (!cd) {
1317 ret = -ENODEV;
1318 goto err_channel_add;
1319 }
1320
1321 irq = platform_get_irq(pdev, i);
1322 if (irq < 0) {
1323 ret = irq;
1324 goto err_channel_add;
1325 }
1326
0be8253f 1327 chan = mv_xor_channel_add(xordev, pdev, i,
dd130c65 1328 cd->cap_mask, irq);
0be8253f
RK
1329 if (IS_ERR(chan)) {
1330 ret = PTR_ERR(chan);
60d151f3
TP
1331 goto err_channel_add;
1332 }
0be8253f
RK
1333
1334 xordev->channels[i] = chan;
60d151f3
TP
1335 }
1336 }
c510182b 1337
ff7b0479 1338 return 0;
60d151f3
TP
1339
1340err_channel_add:
1341 for (i = 0; i < MV_XOR_MAX_CHANNELS; i++)
f7d12ef5 1342 if (xordev->channels[i]) {
ab6e439f 1343 mv_xor_channel_remove(xordev->channels[i]);
f7d12ef5
TP
1344 if (pdev->dev.of_node)
1345 irq_dispose_mapping(xordev->channels[i]->irq);
f7d12ef5 1346 }
60d151f3 1347
dab92064
TP
1348 if (!IS_ERR(xordev->clk)) {
1349 clk_disable_unprepare(xordev->clk);
1350 clk_put(xordev->clk);
1351 }
1352
60d151f3 1353 return ret;
ff7b0479
SB
1354}
1355
61971656
TP
1356static struct platform_driver mv_xor_driver = {
1357 .probe = mv_xor_probe,
8b648436
TP
1358 .suspend = mv_xor_suspend,
1359 .resume = mv_xor_resume,
ff7b0479 1360 .driver = {
f7d12ef5
TP
1361 .name = MV_XOR_NAME,
1362 .of_match_table = of_match_ptr(mv_xor_dt_ids),
ff7b0479
SB
1363 },
1364};
1365
1366
1367static int __init mv_xor_init(void)
1368{
61971656 1369 return platform_driver_register(&mv_xor_driver);
ff7b0479 1370}
25cf68da 1371device_initcall(mv_xor_init);
ff7b0479 1372
25cf68da 1373/*
ff7b0479
SB
1374MODULE_AUTHOR("Saeed Bishara <saeed@marvell.com>");
1375MODULE_DESCRIPTION("DMA engine driver for Marvell's XOR engine");
1376MODULE_LICENSE("GPL");
25cf68da 1377*/