blob: 17326e780e23a4cdcea94ed79a726ea80579ae3a [file] [log] [blame]
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001/*
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.
13 *
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.
17 */
18
19#include <linux/init.h>
20#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Saeed Bisharaff7b0472008-07-08 11:58:36 -070022#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>
Andrew Lunnc5101822012-02-19 13:30:26 +010028#include <linux/clk.h>
Thomas Petazzonif7d12ef2012-11-15 16:47:58 +010029#include <linux/of.h>
30#include <linux/of_irq.h>
31#include <linux/irqdomain.h>
Arnd Bergmannc02cecb2012-08-24 15:21:54 +020032#include <linux/platform_data/dma-mv_xor.h>
Russell King - ARM Linuxd2ebfb32012-03-06 22:34:26 +000033
34#include "dmaengine.h"
Saeed Bisharaff7b0472008-07-08 11:58:36 -070035#include "mv_xor.h"
36
37static void mv_xor_issue_pending(struct dma_chan *chan);
38
39#define to_mv_xor_chan(chan) \
Thomas Petazzoni98817b92012-11-15 14:57:44 +010040 container_of(chan, struct mv_xor_chan, dmachan)
Saeed Bisharaff7b0472008-07-08 11:58:36 -070041
42#define to_mv_xor_slot(tx) \
43 container_of(tx, struct mv_xor_desc_slot, async_tx)
44
Thomas Petazzonic98c1782012-11-15 14:17:18 +010045#define mv_chan_to_devp(chan) \
Thomas Petazzoni1ef48a22012-11-15 15:17:05 +010046 ((chan)->dmadev.dev)
Thomas Petazzonic98c1782012-11-15 14:17:18 +010047
Saeed Bisharaff7b0472008-07-08 11:58:36 -070048static void mv_desc_init(struct mv_xor_desc_slot *desc, unsigned long flags)
49{
50 struct mv_xor_desc *hw_desc = desc->hw_desc;
51
52 hw_desc->status = (1 << 31);
53 hw_desc->phy_next_desc = 0;
54 hw_desc->desc_command = (1 << 31);
55}
56
57static u32 mv_desc_get_dest_addr(struct mv_xor_desc_slot *desc)
58{
59 struct mv_xor_desc *hw_desc = desc->hw_desc;
60 return hw_desc->phy_dest_addr;
61}
62
Saeed Bisharaff7b0472008-07-08 11:58:36 -070063static void mv_desc_set_byte_count(struct mv_xor_desc_slot *desc,
64 u32 byte_count)
65{
66 struct mv_xor_desc *hw_desc = desc->hw_desc;
67 hw_desc->byte_count = byte_count;
68}
69
70static void mv_desc_set_next_desc(struct mv_xor_desc_slot *desc,
71 u32 next_desc_addr)
72{
73 struct mv_xor_desc *hw_desc = desc->hw_desc;
74 BUG_ON(hw_desc->phy_next_desc);
75 hw_desc->phy_next_desc = next_desc_addr;
76}
77
78static void mv_desc_clear_next_desc(struct mv_xor_desc_slot *desc)
79{
80 struct mv_xor_desc *hw_desc = desc->hw_desc;
81 hw_desc->phy_next_desc = 0;
82}
83
Saeed Bisharaff7b0472008-07-08 11:58:36 -070084static void mv_desc_set_dest_addr(struct mv_xor_desc_slot *desc,
85 dma_addr_t addr)
86{
87 struct mv_xor_desc *hw_desc = desc->hw_desc;
88 hw_desc->phy_dest_addr = addr;
89}
90
91static int mv_chan_memset_slot_count(size_t len)
92{
93 return 1;
94}
95
96#define mv_chan_memcpy_slot_count(c) mv_chan_memset_slot_count(c)
97
98static void mv_desc_set_src_addr(struct mv_xor_desc_slot *desc,
99 int index, dma_addr_t addr)
100{
101 struct mv_xor_desc *hw_desc = desc->hw_desc;
Thomas Petazzonie03bc652013-07-29 17:42:14 +0200102 hw_desc->phy_src_addr[mv_phy_src_idx(index)] = addr;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700103 if (desc->type == DMA_XOR)
104 hw_desc->desc_command |= (1 << index);
105}
106
107static u32 mv_chan_get_current_desc(struct mv_xor_chan *chan)
108{
Thomas Petazzoni5733c382013-07-29 17:42:13 +0200109 return readl_relaxed(XOR_CURR_DESC(chan));
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700110}
111
112static void mv_chan_set_next_descriptor(struct mv_xor_chan *chan,
113 u32 next_desc_addr)
114{
Thomas Petazzoni5733c382013-07-29 17:42:13 +0200115 writel_relaxed(next_desc_addr, XOR_NEXT_DESC(chan));
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700116}
117
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700118static void mv_chan_unmask_interrupts(struct mv_xor_chan *chan)
119{
Thomas Petazzoni5733c382013-07-29 17:42:13 +0200120 u32 val = readl_relaxed(XOR_INTR_MASK(chan));
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700121 val |= XOR_INTR_MASK_VALUE << (chan->idx * 16);
Thomas Petazzoni5733c382013-07-29 17:42:13 +0200122 writel_relaxed(val, XOR_INTR_MASK(chan));
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700123}
124
125static u32 mv_chan_get_intr_cause(struct mv_xor_chan *chan)
126{
Thomas Petazzoni5733c382013-07-29 17:42:13 +0200127 u32 intr_cause = readl_relaxed(XOR_INTR_CAUSE(chan));
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700128 intr_cause = (intr_cause >> (chan->idx * 16)) & 0xFFFF;
129 return intr_cause;
130}
131
132static int mv_is_err_intr(u32 intr_cause)
133{
134 if (intr_cause & ((1<<4)|(1<<5)|(1<<6)|(1<<7)|(1<<8)|(1<<9)))
135 return 1;
136
137 return 0;
138}
139
140static void mv_xor_device_clear_eoc_cause(struct mv_xor_chan *chan)
141{
Simon Guinot86363682010-09-17 23:33:51 +0200142 u32 val = ~(1 << (chan->idx * 16));
Thomas Petazzonic98c1782012-11-15 14:17:18 +0100143 dev_dbg(mv_chan_to_devp(chan), "%s, val 0x%08x\n", __func__, val);
Thomas Petazzoni5733c382013-07-29 17:42:13 +0200144 writel_relaxed(val, XOR_INTR_CAUSE(chan));
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700145}
146
147static void mv_xor_device_clear_err_status(struct mv_xor_chan *chan)
148{
149 u32 val = 0xFFFF0000 >> (chan->idx * 16);
Thomas Petazzoni5733c382013-07-29 17:42:13 +0200150 writel_relaxed(val, XOR_INTR_CAUSE(chan));
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700151}
152
153static int mv_can_chain(struct mv_xor_desc_slot *desc)
154{
155 struct mv_xor_desc_slot *chain_old_tail = list_entry(
156 desc->chain_node.prev, struct mv_xor_desc_slot, chain_node);
157
158 if (chain_old_tail->type != desc->type)
159 return 0;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700160
161 return 1;
162}
163
164static void mv_set_mode(struct mv_xor_chan *chan,
165 enum dma_transaction_type type)
166{
167 u32 op_mode;
Thomas Petazzoni5733c382013-07-29 17:42:13 +0200168 u32 config = readl_relaxed(XOR_CONFIG(chan));
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700169
170 switch (type) {
171 case DMA_XOR:
172 op_mode = XOR_OPERATION_MODE_XOR;
173 break;
174 case DMA_MEMCPY:
175 op_mode = XOR_OPERATION_MODE_MEMCPY;
176 break;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700177 default:
Thomas Petazzonic98c1782012-11-15 14:17:18 +0100178 dev_err(mv_chan_to_devp(chan),
Joe Perches1ba151c2012-10-28 01:05:44 -0700179 "error: unsupported operation %d\n",
Thomas Petazzonia3fc74b2012-11-15 12:50:27 +0100180 type);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700181 BUG();
182 return;
183 }
184
185 config &= ~0x7;
186 config |= op_mode;
Thomas Petazzonie03bc652013-07-29 17:42:14 +0200187
188#if defined(__BIG_ENDIAN)
189 config |= XOR_DESCRIPTOR_SWAP;
190#else
191 config &= ~XOR_DESCRIPTOR_SWAP;
192#endif
193
Thomas Petazzoni5733c382013-07-29 17:42:13 +0200194 writel_relaxed(config, XOR_CONFIG(chan));
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700195 chan->current_type = type;
196}
197
198static void mv_chan_activate(struct mv_xor_chan *chan)
199{
200 u32 activation;
201
Thomas Petazzonic98c1782012-11-15 14:17:18 +0100202 dev_dbg(mv_chan_to_devp(chan), " activate chan.\n");
Thomas Petazzoni5733c382013-07-29 17:42:13 +0200203 activation = readl_relaxed(XOR_ACTIVATION(chan));
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700204 activation |= 0x1;
Thomas Petazzoni5733c382013-07-29 17:42:13 +0200205 writel_relaxed(activation, XOR_ACTIVATION(chan));
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700206}
207
208static char mv_chan_is_busy(struct mv_xor_chan *chan)
209{
Thomas Petazzoni5733c382013-07-29 17:42:13 +0200210 u32 state = readl_relaxed(XOR_ACTIVATION(chan));
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700211
212 state = (state >> 4) & 0x3;
213
214 return (state == 1) ? 1 : 0;
215}
216
217static int mv_chan_xor_slot_count(size_t len, int src_cnt)
218{
219 return 1;
220}
221
222/**
223 * mv_xor_free_slots - flags descriptor slots for reuse
224 * @slot: Slot to free
225 * Caller must hold &mv_chan->lock while calling this function
226 */
227static void mv_xor_free_slots(struct mv_xor_chan *mv_chan,
228 struct mv_xor_desc_slot *slot)
229{
Thomas Petazzonic98c1782012-11-15 14:17:18 +0100230 dev_dbg(mv_chan_to_devp(mv_chan), "%s %d slot %p\n",
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700231 __func__, __LINE__, slot);
232
233 slot->slots_per_op = 0;
234
235}
236
237/*
238 * mv_xor_start_new_chain - program the engine to operate on new chain headed by
239 * sw_desc
240 * Caller must hold &mv_chan->lock while calling this function
241 */
242static void mv_xor_start_new_chain(struct mv_xor_chan *mv_chan,
243 struct mv_xor_desc_slot *sw_desc)
244{
Thomas Petazzonic98c1782012-11-15 14:17:18 +0100245 dev_dbg(mv_chan_to_devp(mv_chan), "%s %d: sw_desc %p\n",
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700246 __func__, __LINE__, sw_desc);
247 if (sw_desc->type != mv_chan->current_type)
248 mv_set_mode(mv_chan, sw_desc->type);
249
Bartlomiej Zolnierkiewicz48a9db42013-07-03 15:05:06 -0700250 /* set the hardware chain */
251 mv_chan_set_next_descriptor(mv_chan, sw_desc->async_tx.phys);
252
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700253 mv_chan->pending += sw_desc->slot_cnt;
Thomas Petazzoni98817b92012-11-15 14:57:44 +0100254 mv_xor_issue_pending(&mv_chan->dmachan);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700255}
256
257static dma_cookie_t
258mv_xor_run_tx_complete_actions(struct mv_xor_desc_slot *desc,
259 struct mv_xor_chan *mv_chan, dma_cookie_t cookie)
260{
261 BUG_ON(desc->async_tx.cookie < 0);
262
263 if (desc->async_tx.cookie > 0) {
264 cookie = desc->async_tx.cookie;
265
266 /* call the callback (must not sleep or submit new
267 * operations to this channel)
268 */
269 if (desc->async_tx.callback)
270 desc->async_tx.callback(
271 desc->async_tx.callback_param);
272
Dan Williamsd38a8c62013-10-18 19:35:23 +0200273 dma_descriptor_unmap(&desc->async_tx);
Bartlomiej Zolnierkiewicz54f8d502013-10-18 19:35:32 +0200274 if (desc->group_head)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700275 desc->group_head = NULL;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700276 }
277
278 /* run dependent operations */
Dan Williams07f22112009-01-05 17:14:31 -0700279 dma_run_dependencies(&desc->async_tx);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700280
281 return cookie;
282}
283
284static int
285mv_xor_clean_completed_slots(struct mv_xor_chan *mv_chan)
286{
287 struct mv_xor_desc_slot *iter, *_iter;
288
Thomas Petazzonic98c1782012-11-15 14:17:18 +0100289 dev_dbg(mv_chan_to_devp(mv_chan), "%s %d\n", __func__, __LINE__);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700290 list_for_each_entry_safe(iter, _iter, &mv_chan->completed_slots,
291 completed_node) {
292
293 if (async_tx_test_ack(&iter->async_tx)) {
294 list_del(&iter->completed_node);
295 mv_xor_free_slots(mv_chan, iter);
296 }
297 }
298 return 0;
299}
300
301static int
302mv_xor_clean_slot(struct mv_xor_desc_slot *desc,
303 struct mv_xor_chan *mv_chan)
304{
Thomas Petazzonic98c1782012-11-15 14:17:18 +0100305 dev_dbg(mv_chan_to_devp(mv_chan), "%s %d: desc %p flags %d\n",
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700306 __func__, __LINE__, desc, desc->async_tx.flags);
307 list_del(&desc->chain_node);
308 /* the client is allowed to attach dependent operations
309 * until 'ack' is set
310 */
311 if (!async_tx_test_ack(&desc->async_tx)) {
312 /* move this slot to the completed_slots */
313 list_add_tail(&desc->completed_node, &mv_chan->completed_slots);
314 return 0;
315 }
316
317 mv_xor_free_slots(mv_chan, desc);
318 return 0;
319}
320
321static void __mv_xor_slot_cleanup(struct mv_xor_chan *mv_chan)
322{
323 struct mv_xor_desc_slot *iter, *_iter;
324 dma_cookie_t cookie = 0;
325 int busy = mv_chan_is_busy(mv_chan);
326 u32 current_desc = mv_chan_get_current_desc(mv_chan);
327 int seen_current = 0;
328
Thomas Petazzonic98c1782012-11-15 14:17:18 +0100329 dev_dbg(mv_chan_to_devp(mv_chan), "%s %d\n", __func__, __LINE__);
330 dev_dbg(mv_chan_to_devp(mv_chan), "current_desc %x\n", current_desc);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700331 mv_xor_clean_completed_slots(mv_chan);
332
333 /* free completed slots from the chain starting with
334 * the oldest descriptor
335 */
336
337 list_for_each_entry_safe(iter, _iter, &mv_chan->chain,
338 chain_node) {
339 prefetch(_iter);
340 prefetch(&_iter->async_tx);
341
342 /* do not advance past the current descriptor loaded into the
343 * hardware channel, subsequent descriptors are either in
344 * process or have not been submitted
345 */
346 if (seen_current)
347 break;
348
349 /* stop the search if we reach the current descriptor and the
350 * channel is busy
351 */
352 if (iter->async_tx.phys == current_desc) {
353 seen_current = 1;
354 if (busy)
355 break;
356 }
357
358 cookie = mv_xor_run_tx_complete_actions(iter, mv_chan, cookie);
359
360 if (mv_xor_clean_slot(iter, mv_chan))
361 break;
362 }
363
364 if ((busy == 0) && !list_empty(&mv_chan->chain)) {
365 struct mv_xor_desc_slot *chain_head;
366 chain_head = list_entry(mv_chan->chain.next,
367 struct mv_xor_desc_slot,
368 chain_node);
369
370 mv_xor_start_new_chain(mv_chan, chain_head);
371 }
372
373 if (cookie > 0)
Thomas Petazzoni98817b92012-11-15 14:57:44 +0100374 mv_chan->dmachan.completed_cookie = cookie;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700375}
376
377static void
378mv_xor_slot_cleanup(struct mv_xor_chan *mv_chan)
379{
380 spin_lock_bh(&mv_chan->lock);
381 __mv_xor_slot_cleanup(mv_chan);
382 spin_unlock_bh(&mv_chan->lock);
383}
384
385static void mv_xor_tasklet(unsigned long data)
386{
387 struct mv_xor_chan *chan = (struct mv_xor_chan *) data;
Saeed Bishara8333f652010-12-21 16:53:39 +0200388 mv_xor_slot_cleanup(chan);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700389}
390
391static struct mv_xor_desc_slot *
392mv_xor_alloc_slots(struct mv_xor_chan *mv_chan, int num_slots,
393 int slots_per_op)
394{
395 struct mv_xor_desc_slot *iter, *_iter, *alloc_start = NULL;
396 LIST_HEAD(chain);
397 int slots_found, retry = 0;
398
399 /* start search from the last allocated descrtiptor
400 * if a contiguous allocation can not be found start searching
401 * from the beginning of the list
402 */
403retry:
404 slots_found = 0;
405 if (retry == 0)
406 iter = mv_chan->last_used;
407 else
408 iter = list_entry(&mv_chan->all_slots,
409 struct mv_xor_desc_slot,
410 slot_node);
411
412 list_for_each_entry_safe_continue(
413 iter, _iter, &mv_chan->all_slots, slot_node) {
414 prefetch(_iter);
415 prefetch(&_iter->async_tx);
416 if (iter->slots_per_op) {
417 /* give up after finding the first busy slot
418 * on the second pass through the list
419 */
420 if (retry)
421 break;
422
423 slots_found = 0;
424 continue;
425 }
426
427 /* start the allocation if the slot is correctly aligned */
428 if (!slots_found++)
429 alloc_start = iter;
430
431 if (slots_found == num_slots) {
432 struct mv_xor_desc_slot *alloc_tail = NULL;
433 struct mv_xor_desc_slot *last_used = NULL;
434 iter = alloc_start;
435 while (num_slots) {
436 int i;
437
438 /* pre-ack all but the last descriptor */
439 async_tx_ack(&iter->async_tx);
440
441 list_add_tail(&iter->chain_node, &chain);
442 alloc_tail = iter;
443 iter->async_tx.cookie = 0;
444 iter->slot_cnt = num_slots;
445 iter->xor_check_result = NULL;
446 for (i = 0; i < slots_per_op; i++) {
447 iter->slots_per_op = slots_per_op - i;
448 last_used = iter;
449 iter = list_entry(iter->slot_node.next,
450 struct mv_xor_desc_slot,
451 slot_node);
452 }
453 num_slots -= slots_per_op;
454 }
455 alloc_tail->group_head = alloc_start;
456 alloc_tail->async_tx.cookie = -EBUSY;
Dan Williams64203b62009-09-08 17:53:03 -0700457 list_splice(&chain, &alloc_tail->tx_list);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700458 mv_chan->last_used = last_used;
459 mv_desc_clear_next_desc(alloc_start);
460 mv_desc_clear_next_desc(alloc_tail);
461 return alloc_tail;
462 }
463 }
464 if (!retry++)
465 goto retry;
466
467 /* try to free some slots if the allocation fails */
468 tasklet_schedule(&mv_chan->irq_tasklet);
469
470 return NULL;
471}
472
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700473/************************ DMA engine API functions ****************************/
474static dma_cookie_t
475mv_xor_tx_submit(struct dma_async_tx_descriptor *tx)
476{
477 struct mv_xor_desc_slot *sw_desc = to_mv_xor_slot(tx);
478 struct mv_xor_chan *mv_chan = to_mv_xor_chan(tx->chan);
479 struct mv_xor_desc_slot *grp_start, *old_chain_tail;
480 dma_cookie_t cookie;
481 int new_hw_chain = 1;
482
Thomas Petazzonic98c1782012-11-15 14:17:18 +0100483 dev_dbg(mv_chan_to_devp(mv_chan),
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700484 "%s sw_desc %p: async_tx %p\n",
485 __func__, sw_desc, &sw_desc->async_tx);
486
487 grp_start = sw_desc->group_head;
488
489 spin_lock_bh(&mv_chan->lock);
Russell King - ARM Linux884485e2012-03-06 22:34:46 +0000490 cookie = dma_cookie_assign(tx);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700491
492 if (list_empty(&mv_chan->chain))
Dan Williams64203b62009-09-08 17:53:03 -0700493 list_splice_init(&sw_desc->tx_list, &mv_chan->chain);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700494 else {
495 new_hw_chain = 0;
496
497 old_chain_tail = list_entry(mv_chan->chain.prev,
498 struct mv_xor_desc_slot,
499 chain_node);
Dan Williams64203b62009-09-08 17:53:03 -0700500 list_splice_init(&grp_start->tx_list,
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700501 &old_chain_tail->chain_node);
502
503 if (!mv_can_chain(grp_start))
504 goto submit_done;
505
Thomas Petazzonic98c1782012-11-15 14:17:18 +0100506 dev_dbg(mv_chan_to_devp(mv_chan), "Append to last desc %x\n",
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700507 old_chain_tail->async_tx.phys);
508
509 /* fix up the hardware chain */
510 mv_desc_set_next_desc(old_chain_tail, grp_start->async_tx.phys);
511
512 /* if the channel is not busy */
513 if (!mv_chan_is_busy(mv_chan)) {
514 u32 current_desc = mv_chan_get_current_desc(mv_chan);
515 /*
516 * and the curren desc is the end of the chain before
517 * the append, then we need to start the channel
518 */
519 if (current_desc == old_chain_tail->async_tx.phys)
520 new_hw_chain = 1;
521 }
522 }
523
524 if (new_hw_chain)
525 mv_xor_start_new_chain(mv_chan, grp_start);
526
527submit_done:
528 spin_unlock_bh(&mv_chan->lock);
529
530 return cookie;
531}
532
533/* returns the number of allocated descriptors */
Dan Williamsaa1e6f12009-01-06 11:38:17 -0700534static int mv_xor_alloc_chan_resources(struct dma_chan *chan)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700535{
536 char *hw_desc;
537 int idx;
538 struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
539 struct mv_xor_desc_slot *slot = NULL;
Thomas Petazzonib503fa02012-11-15 15:55:30 +0100540 int num_descs_in_pool = MV_XOR_POOL_SIZE/MV_XOR_SLOT_SIZE;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700541
542 /* Allocate descriptor slots */
543 idx = mv_chan->slots_allocated;
544 while (idx < num_descs_in_pool) {
545 slot = kzalloc(sizeof(*slot), GFP_KERNEL);
546 if (!slot) {
547 printk(KERN_INFO "MV XOR Channel only initialized"
548 " %d descriptor slots", idx);
549 break;
550 }
Thomas Petazzoni1ef48a22012-11-15 15:17:05 +0100551 hw_desc = (char *) mv_chan->dma_desc_pool_virt;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700552 slot->hw_desc = (void *) &hw_desc[idx * MV_XOR_SLOT_SIZE];
553
554 dma_async_tx_descriptor_init(&slot->async_tx, chan);
555 slot->async_tx.tx_submit = mv_xor_tx_submit;
556 INIT_LIST_HEAD(&slot->chain_node);
557 INIT_LIST_HEAD(&slot->slot_node);
Dan Williams64203b62009-09-08 17:53:03 -0700558 INIT_LIST_HEAD(&slot->tx_list);
Thomas Petazzoni1ef48a22012-11-15 15:17:05 +0100559 hw_desc = (char *) mv_chan->dma_desc_pool;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700560 slot->async_tx.phys =
561 (dma_addr_t) &hw_desc[idx * MV_XOR_SLOT_SIZE];
562 slot->idx = idx++;
563
564 spin_lock_bh(&mv_chan->lock);
565 mv_chan->slots_allocated = idx;
566 list_add_tail(&slot->slot_node, &mv_chan->all_slots);
567 spin_unlock_bh(&mv_chan->lock);
568 }
569
570 if (mv_chan->slots_allocated && !mv_chan->last_used)
571 mv_chan->last_used = list_entry(mv_chan->all_slots.next,
572 struct mv_xor_desc_slot,
573 slot_node);
574
Thomas Petazzonic98c1782012-11-15 14:17:18 +0100575 dev_dbg(mv_chan_to_devp(mv_chan),
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700576 "allocated %d descriptor slots last_used: %p\n",
577 mv_chan->slots_allocated, mv_chan->last_used);
578
579 return mv_chan->slots_allocated ? : -ENOMEM;
580}
581
582static struct dma_async_tx_descriptor *
583mv_xor_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
584 size_t len, unsigned long flags)
585{
586 struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
587 struct mv_xor_desc_slot *sw_desc, *grp_start;
588 int slot_cnt;
589
Thomas Petazzonic98c1782012-11-15 14:17:18 +0100590 dev_dbg(mv_chan_to_devp(mv_chan),
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700591 "%s dest: %x src %x len: %u flags: %ld\n",
592 __func__, dest, src, len, flags);
593 if (unlikely(len < MV_XOR_MIN_BYTE_COUNT))
594 return NULL;
595
Coly Li7912d302011-03-27 01:26:53 +0800596 BUG_ON(len > MV_XOR_MAX_BYTE_COUNT);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700597
598 spin_lock_bh(&mv_chan->lock);
599 slot_cnt = mv_chan_memcpy_slot_count(len);
600 sw_desc = mv_xor_alloc_slots(mv_chan, slot_cnt, 1);
601 if (sw_desc) {
602 sw_desc->type = DMA_MEMCPY;
603 sw_desc->async_tx.flags = flags;
604 grp_start = sw_desc->group_head;
605 mv_desc_init(grp_start, flags);
606 mv_desc_set_byte_count(grp_start, len);
607 mv_desc_set_dest_addr(sw_desc->group_head, dest);
608 mv_desc_set_src_addr(grp_start, 0, src);
609 sw_desc->unmap_src_cnt = 1;
610 sw_desc->unmap_len = len;
611 }
612 spin_unlock_bh(&mv_chan->lock);
613
Thomas Petazzonic98c1782012-11-15 14:17:18 +0100614 dev_dbg(mv_chan_to_devp(mv_chan),
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700615 "%s sw_desc %p async_tx %p\n",
Jingoo Han4c143722013-08-06 19:37:08 +0900616 __func__, sw_desc, sw_desc ? &sw_desc->async_tx : NULL);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700617
618 return sw_desc ? &sw_desc->async_tx : NULL;
619}
620
621static struct dma_async_tx_descriptor *
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700622mv_xor_prep_dma_xor(struct dma_chan *chan, dma_addr_t dest, dma_addr_t *src,
623 unsigned int src_cnt, size_t len, unsigned long flags)
624{
625 struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
626 struct mv_xor_desc_slot *sw_desc, *grp_start;
627 int slot_cnt;
628
629 if (unlikely(len < MV_XOR_MIN_BYTE_COUNT))
630 return NULL;
631
Coly Li7912d302011-03-27 01:26:53 +0800632 BUG_ON(len > MV_XOR_MAX_BYTE_COUNT);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700633
Thomas Petazzonic98c1782012-11-15 14:17:18 +0100634 dev_dbg(mv_chan_to_devp(mv_chan),
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700635 "%s src_cnt: %d len: dest %x %u flags: %ld\n",
636 __func__, src_cnt, len, dest, flags);
637
638 spin_lock_bh(&mv_chan->lock);
639 slot_cnt = mv_chan_xor_slot_count(len, src_cnt);
640 sw_desc = mv_xor_alloc_slots(mv_chan, slot_cnt, 1);
641 if (sw_desc) {
642 sw_desc->type = DMA_XOR;
643 sw_desc->async_tx.flags = flags;
644 grp_start = sw_desc->group_head;
645 mv_desc_init(grp_start, flags);
646 /* the byte count field is the same as in memcpy desc*/
647 mv_desc_set_byte_count(grp_start, len);
648 mv_desc_set_dest_addr(sw_desc->group_head, dest);
649 sw_desc->unmap_src_cnt = src_cnt;
650 sw_desc->unmap_len = len;
651 while (src_cnt--)
652 mv_desc_set_src_addr(grp_start, src_cnt, src[src_cnt]);
653 }
654 spin_unlock_bh(&mv_chan->lock);
Thomas Petazzonic98c1782012-11-15 14:17:18 +0100655 dev_dbg(mv_chan_to_devp(mv_chan),
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700656 "%s sw_desc %p async_tx %p \n",
657 __func__, sw_desc, &sw_desc->async_tx);
658 return sw_desc ? &sw_desc->async_tx : NULL;
659}
660
661static void mv_xor_free_chan_resources(struct dma_chan *chan)
662{
663 struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
664 struct mv_xor_desc_slot *iter, *_iter;
665 int in_use_descs = 0;
666
667 mv_xor_slot_cleanup(mv_chan);
668
669 spin_lock_bh(&mv_chan->lock);
670 list_for_each_entry_safe(iter, _iter, &mv_chan->chain,
671 chain_node) {
672 in_use_descs++;
673 list_del(&iter->chain_node);
674 }
675 list_for_each_entry_safe(iter, _iter, &mv_chan->completed_slots,
676 completed_node) {
677 in_use_descs++;
678 list_del(&iter->completed_node);
679 }
680 list_for_each_entry_safe_reverse(
681 iter, _iter, &mv_chan->all_slots, slot_node) {
682 list_del(&iter->slot_node);
683 kfree(iter);
684 mv_chan->slots_allocated--;
685 }
686 mv_chan->last_used = NULL;
687
Thomas Petazzonic98c1782012-11-15 14:17:18 +0100688 dev_dbg(mv_chan_to_devp(mv_chan), "%s slots_allocated %d\n",
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700689 __func__, mv_chan->slots_allocated);
690 spin_unlock_bh(&mv_chan->lock);
691
692 if (in_use_descs)
Thomas Petazzonic98c1782012-11-15 14:17:18 +0100693 dev_err(mv_chan_to_devp(mv_chan),
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700694 "freeing %d in use descriptors!\n", in_use_descs);
695}
696
697/**
Linus Walleij07934482010-03-26 16:50:49 -0700698 * mv_xor_status - poll the status of an XOR transaction
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700699 * @chan: XOR channel handle
700 * @cookie: XOR transaction identifier
Linus Walleij07934482010-03-26 16:50:49 -0700701 * @txstate: XOR transactions state holder (or NULL)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700702 */
Linus Walleij07934482010-03-26 16:50:49 -0700703static enum dma_status mv_xor_status(struct dma_chan *chan,
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700704 dma_cookie_t cookie,
Linus Walleij07934482010-03-26 16:50:49 -0700705 struct dma_tx_state *txstate)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700706{
707 struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700708 enum dma_status ret;
709
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +0000710 ret = dma_cookie_status(chan, cookie, txstate);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700711 if (ret == DMA_SUCCESS) {
712 mv_xor_clean_completed_slots(mv_chan);
713 return ret;
714 }
715 mv_xor_slot_cleanup(mv_chan);
716
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +0000717 return dma_cookie_status(chan, cookie, txstate);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700718}
719
720static void mv_dump_xor_regs(struct mv_xor_chan *chan)
721{
722 u32 val;
723
Thomas Petazzoni5733c382013-07-29 17:42:13 +0200724 val = readl_relaxed(XOR_CONFIG(chan));
Joe Perches1ba151c2012-10-28 01:05:44 -0700725 dev_err(mv_chan_to_devp(chan), "config 0x%08x\n", val);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700726
Thomas Petazzoni5733c382013-07-29 17:42:13 +0200727 val = readl_relaxed(XOR_ACTIVATION(chan));
Joe Perches1ba151c2012-10-28 01:05:44 -0700728 dev_err(mv_chan_to_devp(chan), "activation 0x%08x\n", val);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700729
Thomas Petazzoni5733c382013-07-29 17:42:13 +0200730 val = readl_relaxed(XOR_INTR_CAUSE(chan));
Joe Perches1ba151c2012-10-28 01:05:44 -0700731 dev_err(mv_chan_to_devp(chan), "intr cause 0x%08x\n", val);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700732
Thomas Petazzoni5733c382013-07-29 17:42:13 +0200733 val = readl_relaxed(XOR_INTR_MASK(chan));
Joe Perches1ba151c2012-10-28 01:05:44 -0700734 dev_err(mv_chan_to_devp(chan), "intr mask 0x%08x\n", val);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700735
Thomas Petazzoni5733c382013-07-29 17:42:13 +0200736 val = readl_relaxed(XOR_ERROR_CAUSE(chan));
Joe Perches1ba151c2012-10-28 01:05:44 -0700737 dev_err(mv_chan_to_devp(chan), "error cause 0x%08x\n", val);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700738
Thomas Petazzoni5733c382013-07-29 17:42:13 +0200739 val = readl_relaxed(XOR_ERROR_ADDR(chan));
Joe Perches1ba151c2012-10-28 01:05:44 -0700740 dev_err(mv_chan_to_devp(chan), "error addr 0x%08x\n", val);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700741}
742
743static void mv_xor_err_interrupt_handler(struct mv_xor_chan *chan,
744 u32 intr_cause)
745{
746 if (intr_cause & (1 << 4)) {
Thomas Petazzonic98c1782012-11-15 14:17:18 +0100747 dev_dbg(mv_chan_to_devp(chan),
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700748 "ignore this error\n");
749 return;
750 }
751
Thomas Petazzonic98c1782012-11-15 14:17:18 +0100752 dev_err(mv_chan_to_devp(chan),
Joe Perches1ba151c2012-10-28 01:05:44 -0700753 "error on chan %d. intr cause 0x%08x\n",
Thomas Petazzonia3fc74b2012-11-15 12:50:27 +0100754 chan->idx, intr_cause);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700755
756 mv_dump_xor_regs(chan);
757 BUG();
758}
759
760static irqreturn_t mv_xor_interrupt_handler(int irq, void *data)
761{
762 struct mv_xor_chan *chan = data;
763 u32 intr_cause = mv_chan_get_intr_cause(chan);
764
Thomas Petazzonic98c1782012-11-15 14:17:18 +0100765 dev_dbg(mv_chan_to_devp(chan), "intr cause %x\n", intr_cause);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700766
767 if (mv_is_err_intr(intr_cause))
768 mv_xor_err_interrupt_handler(chan, intr_cause);
769
770 tasklet_schedule(&chan->irq_tasklet);
771
772 mv_xor_device_clear_eoc_cause(chan);
773
774 return IRQ_HANDLED;
775}
776
777static void mv_xor_issue_pending(struct dma_chan *chan)
778{
779 struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
780
781 if (mv_chan->pending >= MV_XOR_THRESHOLD) {
782 mv_chan->pending = 0;
783 mv_chan_activate(mv_chan);
784 }
785}
786
787/*
788 * Perform a transaction to verify the HW works.
789 */
790#define MV_XOR_TEST_SIZE 2000
791
Linus Torvaldsc2714332012-12-14 14:54:26 -0800792static int mv_xor_memcpy_self_test(struct mv_xor_chan *mv_chan)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700793{
794 int i;
795 void *src, *dest;
796 dma_addr_t src_dma, dest_dma;
797 struct dma_chan *dma_chan;
798 dma_cookie_t cookie;
799 struct dma_async_tx_descriptor *tx;
800 int err = 0;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700801
802 src = kmalloc(sizeof(u8) * MV_XOR_TEST_SIZE, GFP_KERNEL);
803 if (!src)
804 return -ENOMEM;
805
806 dest = kzalloc(sizeof(u8) * MV_XOR_TEST_SIZE, GFP_KERNEL);
807 if (!dest) {
808 kfree(src);
809 return -ENOMEM;
810 }
811
812 /* Fill in src buffer */
813 for (i = 0; i < MV_XOR_TEST_SIZE; i++)
814 ((u8 *) src)[i] = (u8)i;
815
Thomas Petazzoni275cc0c2012-11-15 15:09:42 +0100816 dma_chan = &mv_chan->dmachan;
Dan Williamsaa1e6f12009-01-06 11:38:17 -0700817 if (mv_xor_alloc_chan_resources(dma_chan) < 1) {
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700818 err = -ENODEV;
819 goto out;
820 }
821
822 dest_dma = dma_map_single(dma_chan->device->dev, dest,
823 MV_XOR_TEST_SIZE, DMA_FROM_DEVICE);
824
825 src_dma = dma_map_single(dma_chan->device->dev, src,
826 MV_XOR_TEST_SIZE, DMA_TO_DEVICE);
827
828 tx = mv_xor_prep_dma_memcpy(dma_chan, dest_dma, src_dma,
829 MV_XOR_TEST_SIZE, 0);
830 cookie = mv_xor_tx_submit(tx);
831 mv_xor_issue_pending(dma_chan);
832 async_tx_ack(tx);
833 msleep(1);
834
Linus Walleij07934482010-03-26 16:50:49 -0700835 if (mv_xor_status(dma_chan, cookie, NULL) !=
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700836 DMA_SUCCESS) {
Thomas Petazzonia3fc74b2012-11-15 12:50:27 +0100837 dev_err(dma_chan->device->dev,
838 "Self-test copy timed out, disabling\n");
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700839 err = -ENODEV;
840 goto free_resources;
841 }
842
Thomas Petazzonic35064c2012-11-15 13:01:59 +0100843 dma_sync_single_for_cpu(dma_chan->device->dev, dest_dma,
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700844 MV_XOR_TEST_SIZE, DMA_FROM_DEVICE);
845 if (memcmp(src, dest, MV_XOR_TEST_SIZE)) {
Thomas Petazzonia3fc74b2012-11-15 12:50:27 +0100846 dev_err(dma_chan->device->dev,
847 "Self-test copy failed compare, disabling\n");
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700848 err = -ENODEV;
849 goto free_resources;
850 }
851
852free_resources:
853 mv_xor_free_chan_resources(dma_chan);
854out:
855 kfree(src);
856 kfree(dest);
857 return err;
858}
859
860#define MV_XOR_NUM_SRC_TEST 4 /* must be <= 15 */
Bill Pemberton463a1f82012-11-19 13:22:55 -0500861static int
Thomas Petazzoni275cc0c2012-11-15 15:09:42 +0100862mv_xor_xor_self_test(struct mv_xor_chan *mv_chan)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700863{
864 int i, src_idx;
865 struct page *dest;
866 struct page *xor_srcs[MV_XOR_NUM_SRC_TEST];
867 dma_addr_t dma_srcs[MV_XOR_NUM_SRC_TEST];
868 dma_addr_t dest_dma;
869 struct dma_async_tx_descriptor *tx;
870 struct dma_chan *dma_chan;
871 dma_cookie_t cookie;
872 u8 cmp_byte = 0;
873 u32 cmp_word;
874 int err = 0;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700875
876 for (src_idx = 0; src_idx < MV_XOR_NUM_SRC_TEST; src_idx++) {
877 xor_srcs[src_idx] = alloc_page(GFP_KERNEL);
Roel Kluina09b09a2009-02-25 13:56:21 +0100878 if (!xor_srcs[src_idx]) {
879 while (src_idx--)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700880 __free_page(xor_srcs[src_idx]);
Roel Kluina09b09a2009-02-25 13:56:21 +0100881 return -ENOMEM;
882 }
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700883 }
884
885 dest = alloc_page(GFP_KERNEL);
Roel Kluina09b09a2009-02-25 13:56:21 +0100886 if (!dest) {
887 while (src_idx--)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700888 __free_page(xor_srcs[src_idx]);
Roel Kluina09b09a2009-02-25 13:56:21 +0100889 return -ENOMEM;
890 }
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700891
892 /* Fill in src buffers */
893 for (src_idx = 0; src_idx < MV_XOR_NUM_SRC_TEST; src_idx++) {
894 u8 *ptr = page_address(xor_srcs[src_idx]);
895 for (i = 0; i < PAGE_SIZE; i++)
896 ptr[i] = (1 << src_idx);
897 }
898
899 for (src_idx = 0; src_idx < MV_XOR_NUM_SRC_TEST; src_idx++)
900 cmp_byte ^= (u8) (1 << src_idx);
901
902 cmp_word = (cmp_byte << 24) | (cmp_byte << 16) |
903 (cmp_byte << 8) | cmp_byte;
904
905 memset(page_address(dest), 0, PAGE_SIZE);
906
Thomas Petazzoni275cc0c2012-11-15 15:09:42 +0100907 dma_chan = &mv_chan->dmachan;
Dan Williamsaa1e6f12009-01-06 11:38:17 -0700908 if (mv_xor_alloc_chan_resources(dma_chan) < 1) {
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700909 err = -ENODEV;
910 goto out;
911 }
912
913 /* test xor */
914 dest_dma = dma_map_page(dma_chan->device->dev, dest, 0, PAGE_SIZE,
915 DMA_FROM_DEVICE);
916
917 for (i = 0; i < MV_XOR_NUM_SRC_TEST; i++)
918 dma_srcs[i] = dma_map_page(dma_chan->device->dev, xor_srcs[i],
919 0, PAGE_SIZE, DMA_TO_DEVICE);
920
921 tx = mv_xor_prep_dma_xor(dma_chan, dest_dma, dma_srcs,
922 MV_XOR_NUM_SRC_TEST, PAGE_SIZE, 0);
923
924 cookie = mv_xor_tx_submit(tx);
925 mv_xor_issue_pending(dma_chan);
926 async_tx_ack(tx);
927 msleep(8);
928
Linus Walleij07934482010-03-26 16:50:49 -0700929 if (mv_xor_status(dma_chan, cookie, NULL) !=
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700930 DMA_SUCCESS) {
Thomas Petazzonia3fc74b2012-11-15 12:50:27 +0100931 dev_err(dma_chan->device->dev,
932 "Self-test xor timed out, disabling\n");
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700933 err = -ENODEV;
934 goto free_resources;
935 }
936
Thomas Petazzonic35064c2012-11-15 13:01:59 +0100937 dma_sync_single_for_cpu(dma_chan->device->dev, dest_dma,
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700938 PAGE_SIZE, DMA_FROM_DEVICE);
939 for (i = 0; i < (PAGE_SIZE / sizeof(u32)); i++) {
940 u32 *ptr = page_address(dest);
941 if (ptr[i] != cmp_word) {
Thomas Petazzonia3fc74b2012-11-15 12:50:27 +0100942 dev_err(dma_chan->device->dev,
Joe Perches1ba151c2012-10-28 01:05:44 -0700943 "Self-test xor failed compare, disabling. index %d, data %x, expected %x\n",
944 i, ptr[i], cmp_word);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700945 err = -ENODEV;
946 goto free_resources;
947 }
948 }
949
950free_resources:
951 mv_xor_free_chan_resources(dma_chan);
952out:
953 src_idx = MV_XOR_NUM_SRC_TEST;
954 while (src_idx--)
955 __free_page(xor_srcs[src_idx]);
956 __free_page(dest);
957 return err;
958}
959
Andrew Lunn34c93c82012-11-18 11:44:56 +0100960/* This driver does not implement any of the optional DMA operations. */
961static int
962mv_xor_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
963 unsigned long arg)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700964{
Andrew Lunn34c93c82012-11-18 11:44:56 +0100965 return -ENOSYS;
966}
967
Thomas Petazzoni1ef48a22012-11-15 15:17:05 +0100968static int mv_xor_channel_remove(struct mv_xor_chan *mv_chan)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700969{
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700970 struct dma_chan *chan, *_chan;
Thomas Petazzoni1ef48a22012-11-15 15:17:05 +0100971 struct device *dev = mv_chan->dmadev.dev;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700972
Thomas Petazzoni1ef48a22012-11-15 15:17:05 +0100973 dma_async_device_unregister(&mv_chan->dmadev);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700974
Thomas Petazzonib503fa02012-11-15 15:55:30 +0100975 dma_free_coherent(dev, MV_XOR_POOL_SIZE,
Thomas Petazzoni1ef48a22012-11-15 15:17:05 +0100976 mv_chan->dma_desc_pool_virt, mv_chan->dma_desc_pool);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700977
Thomas Petazzoni1ef48a22012-11-15 15:17:05 +0100978 list_for_each_entry_safe(chan, _chan, &mv_chan->dmadev.channels,
Thomas Petazzonia6b4a9d2012-10-29 16:45:46 +0100979 device_node) {
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700980 list_del(&chan->device_node);
981 }
982
Thomas Petazzoni88eb92c2012-11-15 16:11:18 +0100983 free_irq(mv_chan->irq, mv_chan);
984
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700985 return 0;
986}
987
Thomas Petazzoni1ef48a22012-11-15 15:17:05 +0100988static struct mv_xor_chan *
Thomas Petazzoni297eedb2012-11-15 15:29:53 +0100989mv_xor_channel_add(struct mv_xor_device *xordev,
Thomas Petazzonia6b4a9d2012-10-29 16:45:46 +0100990 struct platform_device *pdev,
Thomas Petazzonib503fa02012-11-15 15:55:30 +0100991 int idx, dma_cap_mask_t cap_mask, int irq)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700992{
993 int ret = 0;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700994 struct mv_xor_chan *mv_chan;
995 struct dma_device *dma_dev;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700996
Thomas Petazzoni1ef48a22012-11-15 15:17:05 +0100997 mv_chan = devm_kzalloc(&pdev->dev, sizeof(*mv_chan), GFP_KERNEL);
Sachin Kamata5776592013-09-02 13:54:20 +0530998 if (!mv_chan)
999 return ERR_PTR(-ENOMEM);
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001000
Thomas Petazzoni9aedbdb2012-11-15 15:36:37 +01001001 mv_chan->idx = idx;
Thomas Petazzoni88eb92c2012-11-15 16:11:18 +01001002 mv_chan->irq = irq;
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001003
Thomas Petazzoni1ef48a22012-11-15 15:17:05 +01001004 dma_dev = &mv_chan->dmadev;
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001005
1006 /* allocate coherent memory for hardware descriptors
1007 * note: writecombine gives slightly better performance, but
1008 * requires that we explicitly flush the writes
1009 */
Thomas Petazzoni1ef48a22012-11-15 15:17:05 +01001010 mv_chan->dma_desc_pool_virt =
Thomas Petazzonib503fa02012-11-15 15:55:30 +01001011 dma_alloc_writecombine(&pdev->dev, MV_XOR_POOL_SIZE,
Thomas Petazzoni1ef48a22012-11-15 15:17:05 +01001012 &mv_chan->dma_desc_pool, GFP_KERNEL);
1013 if (!mv_chan->dma_desc_pool_virt)
Thomas Petazzonia6b4a9d2012-10-29 16:45:46 +01001014 return ERR_PTR(-ENOMEM);
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001015
1016 /* discover transaction capabilites from the platform data */
Thomas Petazzonia6b4a9d2012-10-29 16:45:46 +01001017 dma_dev->cap_mask = cap_mask;
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001018
1019 INIT_LIST_HEAD(&dma_dev->channels);
1020
1021 /* set base routines */
1022 dma_dev->device_alloc_chan_resources = mv_xor_alloc_chan_resources;
1023 dma_dev->device_free_chan_resources = mv_xor_free_chan_resources;
Linus Walleij07934482010-03-26 16:50:49 -07001024 dma_dev->device_tx_status = mv_xor_status;
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001025 dma_dev->device_issue_pending = mv_xor_issue_pending;
Andrew Lunn34c93c82012-11-18 11:44:56 +01001026 dma_dev->device_control = mv_xor_control;
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001027 dma_dev->dev = &pdev->dev;
1028
1029 /* set prep routines based on capability */
1030 if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask))
1031 dma_dev->device_prep_dma_memcpy = mv_xor_prep_dma_memcpy;
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001032 if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
Joe Perchesc0198942009-06-28 09:26:21 -07001033 dma_dev->max_xor = 8;
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001034 dma_dev->device_prep_dma_xor = mv_xor_prep_dma_xor;
1035 }
1036
Thomas Petazzoni297eedb2012-11-15 15:29:53 +01001037 mv_chan->mmr_base = xordev->xor_base;
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001038 if (!mv_chan->mmr_base) {
1039 ret = -ENOMEM;
1040 goto err_free_dma;
1041 }
1042 tasklet_init(&mv_chan->irq_tasklet, mv_xor_tasklet, (unsigned long)
1043 mv_chan);
1044
1045 /* clear errors before enabling interrupts */
1046 mv_xor_device_clear_err_status(mv_chan);
1047
Thomas Petazzoni2d0a0742012-11-22 18:19:09 +01001048 ret = request_irq(mv_chan->irq, mv_xor_interrupt_handler,
1049 0, dev_name(&pdev->dev), mv_chan);
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001050 if (ret)
1051 goto err_free_dma;
1052
1053 mv_chan_unmask_interrupts(mv_chan);
1054
1055 mv_set_mode(mv_chan, DMA_MEMCPY);
1056
1057 spin_lock_init(&mv_chan->lock);
1058 INIT_LIST_HEAD(&mv_chan->chain);
1059 INIT_LIST_HEAD(&mv_chan->completed_slots);
1060 INIT_LIST_HEAD(&mv_chan->all_slots);
Thomas Petazzoni98817b92012-11-15 14:57:44 +01001061 mv_chan->dmachan.device = dma_dev;
1062 dma_cookie_init(&mv_chan->dmachan);
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001063
Thomas Petazzoni98817b92012-11-15 14:57:44 +01001064 list_add_tail(&mv_chan->dmachan.device_node, &dma_dev->channels);
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001065
1066 if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask)) {
Thomas Petazzoni275cc0c2012-11-15 15:09:42 +01001067 ret = mv_xor_memcpy_self_test(mv_chan);
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001068 dev_dbg(&pdev->dev, "memcpy self test returned %d\n", ret);
1069 if (ret)
Thomas Petazzoni2d0a0742012-11-22 18:19:09 +01001070 goto err_free_irq;
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001071 }
1072
1073 if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
Thomas Petazzoni275cc0c2012-11-15 15:09:42 +01001074 ret = mv_xor_xor_self_test(mv_chan);
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001075 dev_dbg(&pdev->dev, "xor self test returned %d\n", ret);
1076 if (ret)
Thomas Petazzoni2d0a0742012-11-22 18:19:09 +01001077 goto err_free_irq;
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001078 }
1079
Bartlomiej Zolnierkiewicz48a9db42013-07-03 15:05:06 -07001080 dev_info(&pdev->dev, "Marvell XOR: ( %s%s%s)\n",
Joe Perches1ba151c2012-10-28 01:05:44 -07001081 dma_has_cap(DMA_XOR, dma_dev->cap_mask) ? "xor " : "",
Joe Perches1ba151c2012-10-28 01:05:44 -07001082 dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask) ? "cpy " : "",
1083 dma_has_cap(DMA_INTERRUPT, dma_dev->cap_mask) ? "intr " : "");
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001084
1085 dma_async_device_register(dma_dev);
Thomas Petazzoni1ef48a22012-11-15 15:17:05 +01001086 return mv_chan;
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001087
Thomas Petazzoni2d0a0742012-11-22 18:19:09 +01001088err_free_irq:
1089 free_irq(mv_chan->irq, mv_chan);
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001090 err_free_dma:
Thomas Petazzonib503fa02012-11-15 15:55:30 +01001091 dma_free_coherent(&pdev->dev, MV_XOR_POOL_SIZE,
Thomas Petazzoni1ef48a22012-11-15 15:17:05 +01001092 mv_chan->dma_desc_pool_virt, mv_chan->dma_desc_pool);
Thomas Petazzonia6b4a9d2012-10-29 16:45:46 +01001093 return ERR_PTR(ret);
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001094}
1095
1096static void
Thomas Petazzoni297eedb2012-11-15 15:29:53 +01001097mv_xor_conf_mbus_windows(struct mv_xor_device *xordev,
Andrew Lunn63a93322011-12-07 21:48:07 +01001098 const struct mbus_dram_target_info *dram)
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001099{
Thomas Petazzoni297eedb2012-11-15 15:29:53 +01001100 void __iomem *base = xordev->xor_base;
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001101 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 (i = 0; i < dram->num_cs; i++) {
Andrew Lunn63a93322011-12-07 21:48:07 +01001112 const struct mbus_dram_window *cs = dram->cs + i;
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001113
1114 writel((cs->base & 0xffff0000) |
1115 (cs->mbus_attr << 8) |
1116 dram->mbus_dram_target_id, base + WINDOW_BASE(i));
1117 writel((cs->size - 1) & 0xffff0000, base + WINDOW_SIZE(i));
1118
1119 win_enable |= (1 << i);
1120 win_enable |= 3 << (16 + (2 * i));
1121 }
1122
1123 writel(win_enable, base + WINDOW_BAR_ENABLE(0));
1124 writel(win_enable, base + WINDOW_BAR_ENABLE(1));
Thomas Petazzonic4b4b732012-11-22 18:16:37 +01001125 writel(0, base + WINDOW_OVERRIDE_CTRL(0));
1126 writel(0, base + WINDOW_OVERRIDE_CTRL(1));
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001127}
1128
Linus Torvaldsc2714332012-12-14 14:54:26 -08001129static int mv_xor_probe(struct platform_device *pdev)
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001130{
Andrew Lunn63a93322011-12-07 21:48:07 +01001131 const struct mbus_dram_target_info *dram;
Thomas Petazzoni297eedb2012-11-15 15:29:53 +01001132 struct mv_xor_device *xordev;
Jingoo Hand4adcc02013-07-30 17:09:11 +09001133 struct mv_xor_platform_data *pdata = dev_get_platdata(&pdev->dev);
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001134 struct resource *res;
Thomas Petazzoni60d151f2012-10-29 16:54:49 +01001135 int i, ret;
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001136
Joe Perches1ba151c2012-10-28 01:05:44 -07001137 dev_notice(&pdev->dev, "Marvell shared XOR driver\n");
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001138
Thomas Petazzoni297eedb2012-11-15 15:29:53 +01001139 xordev = devm_kzalloc(&pdev->dev, sizeof(*xordev), GFP_KERNEL);
1140 if (!xordev)
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001141 return -ENOMEM;
1142
1143 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1144 if (!res)
1145 return -ENODEV;
1146
Thomas Petazzoni297eedb2012-11-15 15:29:53 +01001147 xordev->xor_base = devm_ioremap(&pdev->dev, res->start,
1148 resource_size(res));
1149 if (!xordev->xor_base)
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001150 return -EBUSY;
1151
1152 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1153 if (!res)
1154 return -ENODEV;
1155
Thomas Petazzoni297eedb2012-11-15 15:29:53 +01001156 xordev->xor_high_base = devm_ioremap(&pdev->dev, res->start,
1157 resource_size(res));
1158 if (!xordev->xor_high_base)
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001159 return -EBUSY;
1160
Thomas Petazzoni297eedb2012-11-15 15:29:53 +01001161 platform_set_drvdata(pdev, xordev);
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001162
1163 /*
1164 * (Re-)program MBUS remapping windows if we are asked to.
1165 */
Andrew Lunn63a93322011-12-07 21:48:07 +01001166 dram = mv_mbus_dram_info();
1167 if (dram)
Thomas Petazzoni297eedb2012-11-15 15:29:53 +01001168 mv_xor_conf_mbus_windows(xordev, dram);
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001169
Andrew Lunnc5101822012-02-19 13:30:26 +01001170 /* Not all platforms can gate the clock, so it is not
1171 * an error if the clock does not exists.
1172 */
Thomas Petazzoni297eedb2012-11-15 15:29:53 +01001173 xordev->clk = clk_get(&pdev->dev, NULL);
1174 if (!IS_ERR(xordev->clk))
1175 clk_prepare_enable(xordev->clk);
Andrew Lunnc5101822012-02-19 13:30:26 +01001176
Thomas Petazzonif7d12ef2012-11-15 16:47:58 +01001177 if (pdev->dev.of_node) {
1178 struct device_node *np;
1179 int i = 0;
1180
1181 for_each_child_of_node(pdev->dev.of_node, np) {
1182 dma_cap_mask_t cap_mask;
1183 int irq;
1184
1185 dma_cap_zero(cap_mask);
1186 if (of_property_read_bool(np, "dmacap,memcpy"))
1187 dma_cap_set(DMA_MEMCPY, cap_mask);
1188 if (of_property_read_bool(np, "dmacap,xor"))
1189 dma_cap_set(DMA_XOR, cap_mask);
Thomas Petazzonif7d12ef2012-11-15 16:47:58 +01001190 if (of_property_read_bool(np, "dmacap,interrupt"))
1191 dma_cap_set(DMA_INTERRUPT, cap_mask);
1192
1193 irq = irq_of_parse_and_map(np, 0);
Thomas Petazzonif8eb9e72012-11-22 18:22:12 +01001194 if (!irq) {
1195 ret = -ENODEV;
Thomas Petazzonif7d12ef2012-11-15 16:47:58 +01001196 goto err_channel_add;
1197 }
1198
1199 xordev->channels[i] =
1200 mv_xor_channel_add(xordev, pdev, i,
1201 cap_mask, irq);
1202 if (IS_ERR(xordev->channels[i])) {
1203 ret = PTR_ERR(xordev->channels[i]);
Thomas Petazzoni73d9cdc2012-11-22 18:22:59 +01001204 xordev->channels[i] = NULL;
Thomas Petazzonif7d12ef2012-11-15 16:47:58 +01001205 irq_dispose_mapping(irq);
1206 goto err_channel_add;
1207 }
1208
1209 i++;
1210 }
1211 } else if (pdata && pdata->channels) {
Thomas Petazzoni60d151f2012-10-29 16:54:49 +01001212 for (i = 0; i < MV_XOR_MAX_CHANNELS; i++) {
Thomas Petazzonie39f6ec2012-10-30 11:56:26 +01001213 struct mv_xor_channel_data *cd;
Thomas Petazzoni60d151f2012-10-29 16:54:49 +01001214 int irq;
1215
1216 cd = &pdata->channels[i];
1217 if (!cd) {
1218 ret = -ENODEV;
1219 goto err_channel_add;
1220 }
1221
1222 irq = platform_get_irq(pdev, i);
1223 if (irq < 0) {
1224 ret = irq;
1225 goto err_channel_add;
1226 }
1227
Thomas Petazzoni297eedb2012-11-15 15:29:53 +01001228 xordev->channels[i] =
Thomas Petazzoni9aedbdb2012-11-15 15:36:37 +01001229 mv_xor_channel_add(xordev, pdev, i,
Thomas Petazzonib503fa02012-11-15 15:55:30 +01001230 cd->cap_mask, irq);
Thomas Petazzoni297eedb2012-11-15 15:29:53 +01001231 if (IS_ERR(xordev->channels[i])) {
1232 ret = PTR_ERR(xordev->channels[i]);
Thomas Petazzoni60d151f2012-10-29 16:54:49 +01001233 goto err_channel_add;
1234 }
1235 }
1236 }
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001237
1238 return 0;
Thomas Petazzoni60d151f2012-10-29 16:54:49 +01001239
1240err_channel_add:
1241 for (i = 0; i < MV_XOR_MAX_CHANNELS; i++)
Thomas Petazzonif7d12ef2012-11-15 16:47:58 +01001242 if (xordev->channels[i]) {
Thomas Petazzoniab6e4392013-01-06 11:10:43 +01001243 mv_xor_channel_remove(xordev->channels[i]);
Thomas Petazzonif7d12ef2012-11-15 16:47:58 +01001244 if (pdev->dev.of_node)
1245 irq_dispose_mapping(xordev->channels[i]->irq);
Thomas Petazzonif7d12ef2012-11-15 16:47:58 +01001246 }
Thomas Petazzoni60d151f2012-10-29 16:54:49 +01001247
Thomas Petazzonidab92062013-01-06 11:10:44 +01001248 if (!IS_ERR(xordev->clk)) {
1249 clk_disable_unprepare(xordev->clk);
1250 clk_put(xordev->clk);
1251 }
1252
Thomas Petazzoni60d151f2012-10-29 16:54:49 +01001253 return ret;
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001254}
1255
Linus Torvaldsc2714332012-12-14 14:54:26 -08001256static int mv_xor_remove(struct platform_device *pdev)
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001257{
Thomas Petazzoni297eedb2012-11-15 15:29:53 +01001258 struct mv_xor_device *xordev = platform_get_drvdata(pdev);
Thomas Petazzoni60d151f2012-10-29 16:54:49 +01001259 int i;
Andrew Lunnc5101822012-02-19 13:30:26 +01001260
Thomas Petazzoni60d151f2012-10-29 16:54:49 +01001261 for (i = 0; i < MV_XOR_MAX_CHANNELS; i++) {
Thomas Petazzoni297eedb2012-11-15 15:29:53 +01001262 if (xordev->channels[i])
1263 mv_xor_channel_remove(xordev->channels[i]);
Thomas Petazzoni60d151f2012-10-29 16:54:49 +01001264 }
Andrew Lunnc5101822012-02-19 13:30:26 +01001265
Thomas Petazzoni297eedb2012-11-15 15:29:53 +01001266 if (!IS_ERR(xordev->clk)) {
1267 clk_disable_unprepare(xordev->clk);
1268 clk_put(xordev->clk);
Andrew Lunnc5101822012-02-19 13:30:26 +01001269 }
1270
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001271 return 0;
1272}
1273
Thomas Petazzonif7d12ef2012-11-15 16:47:58 +01001274#ifdef CONFIG_OF
Linus Torvaldsc2714332012-12-14 14:54:26 -08001275static struct of_device_id mv_xor_dt_ids[] = {
Thomas Petazzonif7d12ef2012-11-15 16:47:58 +01001276 { .compatible = "marvell,orion-xor", },
1277 {},
1278};
1279MODULE_DEVICE_TABLE(of, mv_xor_dt_ids);
1280#endif
1281
Thomas Petazzoni61971652012-10-30 12:05:40 +01001282static struct platform_driver mv_xor_driver = {
1283 .probe = mv_xor_probe,
Linus Torvaldsc2714332012-12-14 14:54:26 -08001284 .remove = mv_xor_remove,
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001285 .driver = {
Thomas Petazzonif7d12ef2012-11-15 16:47:58 +01001286 .owner = THIS_MODULE,
1287 .name = MV_XOR_NAME,
1288 .of_match_table = of_match_ptr(mv_xor_dt_ids),
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001289 },
1290};
1291
1292
1293static int __init mv_xor_init(void)
1294{
Thomas Petazzoni61971652012-10-30 12:05:40 +01001295 return platform_driver_register(&mv_xor_driver);
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001296}
1297module_init(mv_xor_init);
1298
1299/* it's currently unsafe to unload this module */
1300#if 0
1301static void __exit mv_xor_exit(void)
1302{
1303 platform_driver_unregister(&mv_xor_driver);
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001304 return;
1305}
1306
1307module_exit(mv_xor_exit);
1308#endif
1309
1310MODULE_AUTHOR("Saeed Bishara <saeed@marvell.com>");
1311MODULE_DESCRIPTION("DMA engine driver for Marvell's XOR engine");
1312MODULE_LICENSE("GPL");