blob: 1c56001df676c048a8ec30c206e0a215375eada7 [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.
Saeed Bisharaff7b0472008-07-08 11:58:36 -070013 */
14
15#include <linux/init.h>
16#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Saeed Bisharaff7b0472008-07-08 11:58:36 -070018#include <linux/delay.h>
19#include <linux/dma-mapping.h>
20#include <linux/spinlock.h>
21#include <linux/interrupt.h>
22#include <linux/platform_device.h>
23#include <linux/memory.h>
Andrew Lunnc5101822012-02-19 13:30:26 +010024#include <linux/clk.h>
Thomas Petazzonif7d12ef2012-11-15 16:47:58 +010025#include <linux/of.h>
26#include <linux/of_irq.h>
27#include <linux/irqdomain.h>
Arnd Bergmannc02cecb2012-08-24 15:21:54 +020028#include <linux/platform_data/dma-mv_xor.h>
Russell King - ARM Linuxd2ebfb32012-03-06 22:34:26 +000029
30#include "dmaengine.h"
Saeed Bisharaff7b0472008-07-08 11:58:36 -070031#include "mv_xor.h"
32
33static void mv_xor_issue_pending(struct dma_chan *chan);
34
35#define to_mv_xor_chan(chan) \
Thomas Petazzoni98817b92012-11-15 14:57:44 +010036 container_of(chan, struct mv_xor_chan, dmachan)
Saeed Bisharaff7b0472008-07-08 11:58:36 -070037
38#define to_mv_xor_slot(tx) \
39 container_of(tx, struct mv_xor_desc_slot, async_tx)
40
Thomas Petazzonic98c1782012-11-15 14:17:18 +010041#define mv_chan_to_devp(chan) \
Thomas Petazzoni1ef48a22012-11-15 15:17:05 +010042 ((chan)->dmadev.dev)
Thomas Petazzonic98c1782012-11-15 14:17:18 +010043
Lior Amsalemdfc97662014-08-27 10:52:51 -030044static void mv_desc_init(struct mv_xor_desc_slot *desc,
Lior Amsalemba87d132014-08-27 10:52:53 -030045 dma_addr_t addr, u32 byte_count,
46 enum dma_ctrl_flags flags)
Saeed Bisharaff7b0472008-07-08 11:58:36 -070047{
48 struct mv_xor_desc *hw_desc = desc->hw_desc;
49
Ezequiel Garcia0e7488e2014-08-27 10:52:52 -030050 hw_desc->status = XOR_DESC_DMA_OWNED;
Saeed Bisharaff7b0472008-07-08 11:58:36 -070051 hw_desc->phy_next_desc = 0;
Lior Amsalemba87d132014-08-27 10:52:53 -030052 /* Enable end-of-descriptor interrupts only for DMA_PREP_INTERRUPT */
53 hw_desc->desc_command = (flags & DMA_PREP_INTERRUPT) ?
54 XOR_DESC_EOD_INT_EN : 0;
Lior Amsalemdfc97662014-08-27 10:52:51 -030055 hw_desc->phy_dest_addr = addr;
Saeed Bisharaff7b0472008-07-08 11:58:36 -070056 hw_desc->byte_count = byte_count;
57}
58
59static void mv_desc_set_next_desc(struct mv_xor_desc_slot *desc,
60 u32 next_desc_addr)
61{
62 struct mv_xor_desc *hw_desc = desc->hw_desc;
63 BUG_ON(hw_desc->phy_next_desc);
64 hw_desc->phy_next_desc = next_desc_addr;
65}
66
67static void mv_desc_clear_next_desc(struct mv_xor_desc_slot *desc)
68{
69 struct mv_xor_desc *hw_desc = desc->hw_desc;
70 hw_desc->phy_next_desc = 0;
71}
72
Saeed Bisharaff7b0472008-07-08 11:58:36 -070073static void mv_desc_set_src_addr(struct mv_xor_desc_slot *desc,
74 int index, dma_addr_t addr)
75{
76 struct mv_xor_desc *hw_desc = desc->hw_desc;
Thomas Petazzonie03bc652013-07-29 17:42:14 +020077 hw_desc->phy_src_addr[mv_phy_src_idx(index)] = addr;
Saeed Bisharaff7b0472008-07-08 11:58:36 -070078 if (desc->type == DMA_XOR)
79 hw_desc->desc_command |= (1 << index);
80}
81
82static u32 mv_chan_get_current_desc(struct mv_xor_chan *chan)
83{
Thomas Petazzoni5733c382013-07-29 17:42:13 +020084 return readl_relaxed(XOR_CURR_DESC(chan));
Saeed Bisharaff7b0472008-07-08 11:58:36 -070085}
86
87static void mv_chan_set_next_descriptor(struct mv_xor_chan *chan,
88 u32 next_desc_addr)
89{
Thomas Petazzoni5733c382013-07-29 17:42:13 +020090 writel_relaxed(next_desc_addr, XOR_NEXT_DESC(chan));
Saeed Bisharaff7b0472008-07-08 11:58:36 -070091}
92
Saeed Bisharaff7b0472008-07-08 11:58:36 -070093static void mv_chan_unmask_interrupts(struct mv_xor_chan *chan)
94{
Thomas Petazzoni5733c382013-07-29 17:42:13 +020095 u32 val = readl_relaxed(XOR_INTR_MASK(chan));
Saeed Bisharaff7b0472008-07-08 11:58:36 -070096 val |= XOR_INTR_MASK_VALUE << (chan->idx * 16);
Thomas Petazzoni5733c382013-07-29 17:42:13 +020097 writel_relaxed(val, XOR_INTR_MASK(chan));
Saeed Bisharaff7b0472008-07-08 11:58:36 -070098}
99
100static u32 mv_chan_get_intr_cause(struct mv_xor_chan *chan)
101{
Thomas Petazzoni5733c382013-07-29 17:42:13 +0200102 u32 intr_cause = readl_relaxed(XOR_INTR_CAUSE(chan));
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700103 intr_cause = (intr_cause >> (chan->idx * 16)) & 0xFFFF;
104 return intr_cause;
105}
106
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700107static void mv_xor_device_clear_eoc_cause(struct mv_xor_chan *chan)
108{
Lior Amsalemba87d132014-08-27 10:52:53 -0300109 u32 val;
110
111 val = XOR_INT_END_OF_DESC | XOR_INT_END_OF_CHAIN | XOR_INT_STOPPED;
112 val = ~(val << (chan->idx * 16));
Thomas Petazzonic98c1782012-11-15 14:17:18 +0100113 dev_dbg(mv_chan_to_devp(chan), "%s, val 0x%08x\n", __func__, val);
Thomas Petazzoni5733c382013-07-29 17:42:13 +0200114 writel_relaxed(val, XOR_INTR_CAUSE(chan));
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700115}
116
117static void mv_xor_device_clear_err_status(struct mv_xor_chan *chan)
118{
119 u32 val = 0xFFFF0000 >> (chan->idx * 16);
Thomas Petazzoni5733c382013-07-29 17:42:13 +0200120 writel_relaxed(val, XOR_INTR_CAUSE(chan));
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700121}
122
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700123static void mv_set_mode(struct mv_xor_chan *chan,
124 enum dma_transaction_type type)
125{
126 u32 op_mode;
Thomas Petazzoni5733c382013-07-29 17:42:13 +0200127 u32 config = readl_relaxed(XOR_CONFIG(chan));
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700128
129 switch (type) {
130 case DMA_XOR:
131 op_mode = XOR_OPERATION_MODE_XOR;
132 break;
133 case DMA_MEMCPY:
134 op_mode = XOR_OPERATION_MODE_MEMCPY;
135 break;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700136 default:
Thomas Petazzonic98c1782012-11-15 14:17:18 +0100137 dev_err(mv_chan_to_devp(chan),
Joe Perches1ba151c2012-10-28 01:05:44 -0700138 "error: unsupported operation %d\n",
Thomas Petazzonia3fc74b2012-11-15 12:50:27 +0100139 type);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700140 BUG();
141 return;
142 }
143
144 config &= ~0x7;
145 config |= op_mode;
Thomas Petazzonie03bc652013-07-29 17:42:14 +0200146
147#if defined(__BIG_ENDIAN)
148 config |= XOR_DESCRIPTOR_SWAP;
149#else
150 config &= ~XOR_DESCRIPTOR_SWAP;
151#endif
152
Thomas Petazzoni5733c382013-07-29 17:42:13 +0200153 writel_relaxed(config, XOR_CONFIG(chan));
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700154 chan->current_type = type;
155}
156
157static void mv_chan_activate(struct mv_xor_chan *chan)
158{
Thomas Petazzonic98c1782012-11-15 14:17:18 +0100159 dev_dbg(mv_chan_to_devp(chan), " activate chan.\n");
Ezequiel Garcia5a9a55b2014-05-21 14:02:35 -0700160
161 /* writel ensures all descriptors are flushed before activation */
162 writel(BIT(0), XOR_ACTIVATION(chan));
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700163}
164
165static char mv_chan_is_busy(struct mv_xor_chan *chan)
166{
Thomas Petazzoni5733c382013-07-29 17:42:13 +0200167 u32 state = readl_relaxed(XOR_ACTIVATION(chan));
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700168
169 state = (state >> 4) & 0x3;
170
171 return (state == 1) ? 1 : 0;
172}
173
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700174/**
175 * mv_xor_free_slots - flags descriptor slots for reuse
176 * @slot: Slot to free
177 * Caller must hold &mv_chan->lock while calling this function
178 */
179static void mv_xor_free_slots(struct mv_xor_chan *mv_chan,
180 struct mv_xor_desc_slot *slot)
181{
Thomas Petazzonic98c1782012-11-15 14:17:18 +0100182 dev_dbg(mv_chan_to_devp(mv_chan), "%s %d slot %p\n",
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700183 __func__, __LINE__, slot);
184
Lior Amsalemdfc97662014-08-27 10:52:51 -0300185 slot->slot_used = 0;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700186
187}
188
189/*
190 * mv_xor_start_new_chain - program the engine to operate on new chain headed by
191 * sw_desc
192 * Caller must hold &mv_chan->lock while calling this function
193 */
194static void mv_xor_start_new_chain(struct mv_xor_chan *mv_chan,
195 struct mv_xor_desc_slot *sw_desc)
196{
Thomas Petazzonic98c1782012-11-15 14:17:18 +0100197 dev_dbg(mv_chan_to_devp(mv_chan), "%s %d: sw_desc %p\n",
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700198 __func__, __LINE__, sw_desc);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700199
Bartlomiej Zolnierkiewicz48a9db42013-07-03 15:05:06 -0700200 /* set the hardware chain */
201 mv_chan_set_next_descriptor(mv_chan, sw_desc->async_tx.phys);
202
Lior Amsalemdfc97662014-08-27 10:52:51 -0300203 mv_chan->pending++;
Thomas Petazzoni98817b92012-11-15 14:57:44 +0100204 mv_xor_issue_pending(&mv_chan->dmachan);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700205}
206
207static dma_cookie_t
208mv_xor_run_tx_complete_actions(struct mv_xor_desc_slot *desc,
209 struct mv_xor_chan *mv_chan, dma_cookie_t cookie)
210{
211 BUG_ON(desc->async_tx.cookie < 0);
212
213 if (desc->async_tx.cookie > 0) {
214 cookie = desc->async_tx.cookie;
215
216 /* call the callback (must not sleep or submit new
217 * operations to this channel)
218 */
219 if (desc->async_tx.callback)
220 desc->async_tx.callback(
221 desc->async_tx.callback_param);
222
Dan Williamsd38a8c62013-10-18 19:35:23 +0200223 dma_descriptor_unmap(&desc->async_tx);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700224 }
225
226 /* run dependent operations */
Dan Williams07f22112009-01-05 17:14:31 -0700227 dma_run_dependencies(&desc->async_tx);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700228
229 return cookie;
230}
231
232static int
233mv_xor_clean_completed_slots(struct mv_xor_chan *mv_chan)
234{
235 struct mv_xor_desc_slot *iter, *_iter;
236
Thomas Petazzonic98c1782012-11-15 14:17:18 +0100237 dev_dbg(mv_chan_to_devp(mv_chan), "%s %d\n", __func__, __LINE__);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700238 list_for_each_entry_safe(iter, _iter, &mv_chan->completed_slots,
239 completed_node) {
240
241 if (async_tx_test_ack(&iter->async_tx)) {
242 list_del(&iter->completed_node);
243 mv_xor_free_slots(mv_chan, iter);
244 }
245 }
246 return 0;
247}
248
249static int
250mv_xor_clean_slot(struct mv_xor_desc_slot *desc,
251 struct mv_xor_chan *mv_chan)
252{
Thomas Petazzonic98c1782012-11-15 14:17:18 +0100253 dev_dbg(mv_chan_to_devp(mv_chan), "%s %d: desc %p flags %d\n",
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700254 __func__, __LINE__, desc, desc->async_tx.flags);
255 list_del(&desc->chain_node);
256 /* the client is allowed to attach dependent operations
257 * until 'ack' is set
258 */
259 if (!async_tx_test_ack(&desc->async_tx)) {
260 /* move this slot to the completed_slots */
261 list_add_tail(&desc->completed_node, &mv_chan->completed_slots);
262 return 0;
263 }
264
265 mv_xor_free_slots(mv_chan, desc);
266 return 0;
267}
268
Ezequiel Garciafbeec992014-03-07 16:46:47 -0300269/* This function must be called with the mv_xor_chan spinlock held */
270static void mv_xor_slot_cleanup(struct mv_xor_chan *mv_chan)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700271{
272 struct mv_xor_desc_slot *iter, *_iter;
273 dma_cookie_t cookie = 0;
274 int busy = mv_chan_is_busy(mv_chan);
275 u32 current_desc = mv_chan_get_current_desc(mv_chan);
276 int seen_current = 0;
277
Thomas Petazzonic98c1782012-11-15 14:17:18 +0100278 dev_dbg(mv_chan_to_devp(mv_chan), "%s %d\n", __func__, __LINE__);
279 dev_dbg(mv_chan_to_devp(mv_chan), "current_desc %x\n", current_desc);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700280 mv_xor_clean_completed_slots(mv_chan);
281
282 /* free completed slots from the chain starting with
283 * the oldest descriptor
284 */
285
286 list_for_each_entry_safe(iter, _iter, &mv_chan->chain,
287 chain_node) {
288 prefetch(_iter);
289 prefetch(&_iter->async_tx);
290
291 /* do not advance past the current descriptor loaded into the
292 * hardware channel, subsequent descriptors are either in
293 * process or have not been submitted
294 */
295 if (seen_current)
296 break;
297
298 /* stop the search if we reach the current descriptor and the
299 * channel is busy
300 */
301 if (iter->async_tx.phys == current_desc) {
302 seen_current = 1;
303 if (busy)
304 break;
305 }
306
307 cookie = mv_xor_run_tx_complete_actions(iter, mv_chan, cookie);
308
309 if (mv_xor_clean_slot(iter, mv_chan))
310 break;
311 }
312
313 if ((busy == 0) && !list_empty(&mv_chan->chain)) {
314 struct mv_xor_desc_slot *chain_head;
315 chain_head = list_entry(mv_chan->chain.next,
316 struct mv_xor_desc_slot,
317 chain_node);
318
319 mv_xor_start_new_chain(mv_chan, chain_head);
320 }
321
322 if (cookie > 0)
Thomas Petazzoni98817b92012-11-15 14:57:44 +0100323 mv_chan->dmachan.completed_cookie = cookie;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700324}
325
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700326static void mv_xor_tasklet(unsigned long data)
327{
328 struct mv_xor_chan *chan = (struct mv_xor_chan *) data;
Ezequiel Garciae43147a2014-03-07 16:46:46 -0300329
330 spin_lock_bh(&chan->lock);
Saeed Bishara8333f652010-12-21 16:53:39 +0200331 mv_xor_slot_cleanup(chan);
Ezequiel Garciae43147a2014-03-07 16:46:46 -0300332 spin_unlock_bh(&chan->lock);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700333}
334
335static struct mv_xor_desc_slot *
Lior Amsalemdfc97662014-08-27 10:52:51 -0300336mv_xor_alloc_slot(struct mv_xor_chan *mv_chan)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700337{
Lior Amsalemdfc97662014-08-27 10:52:51 -0300338 struct mv_xor_desc_slot *iter, *_iter;
339 int retry = 0;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700340
341 /* start search from the last allocated descrtiptor
342 * if a contiguous allocation can not be found start searching
343 * from the beginning of the list
344 */
345retry:
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700346 if (retry == 0)
347 iter = mv_chan->last_used;
348 else
349 iter = list_entry(&mv_chan->all_slots,
350 struct mv_xor_desc_slot,
351 slot_node);
352
353 list_for_each_entry_safe_continue(
354 iter, _iter, &mv_chan->all_slots, slot_node) {
Lior Amsalemdfc97662014-08-27 10:52:51 -0300355
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700356 prefetch(_iter);
357 prefetch(&_iter->async_tx);
Lior Amsalemdfc97662014-08-27 10:52:51 -0300358 if (iter->slot_used) {
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700359 /* give up after finding the first busy slot
360 * on the second pass through the list
361 */
362 if (retry)
363 break;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700364 continue;
365 }
366
Lior Amsalemdfc97662014-08-27 10:52:51 -0300367 /* pre-ack descriptor */
368 async_tx_ack(&iter->async_tx);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700369
Lior Amsalemdfc97662014-08-27 10:52:51 -0300370 iter->slot_used = 1;
371 INIT_LIST_HEAD(&iter->chain_node);
372 iter->async_tx.cookie = -EBUSY;
373 mv_chan->last_used = iter;
374 mv_desc_clear_next_desc(iter);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700375
Lior Amsalemdfc97662014-08-27 10:52:51 -0300376 return iter;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700377
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700378 }
379 if (!retry++)
380 goto retry;
381
382 /* try to free some slots if the allocation fails */
383 tasklet_schedule(&mv_chan->irq_tasklet);
384
385 return NULL;
386}
387
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700388/************************ DMA engine API functions ****************************/
389static dma_cookie_t
390mv_xor_tx_submit(struct dma_async_tx_descriptor *tx)
391{
392 struct mv_xor_desc_slot *sw_desc = to_mv_xor_slot(tx);
393 struct mv_xor_chan *mv_chan = to_mv_xor_chan(tx->chan);
Lior Amsalemdfc97662014-08-27 10:52:51 -0300394 struct mv_xor_desc_slot *old_chain_tail;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700395 dma_cookie_t cookie;
396 int new_hw_chain = 1;
397
Thomas Petazzonic98c1782012-11-15 14:17:18 +0100398 dev_dbg(mv_chan_to_devp(mv_chan),
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700399 "%s sw_desc %p: async_tx %p\n",
400 __func__, sw_desc, &sw_desc->async_tx);
401
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700402 spin_lock_bh(&mv_chan->lock);
Russell King - ARM Linux884485e2012-03-06 22:34:46 +0000403 cookie = dma_cookie_assign(tx);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700404
405 if (list_empty(&mv_chan->chain))
Lior Amsalemdfc97662014-08-27 10:52:51 -0300406 list_add_tail(&sw_desc->chain_node, &mv_chan->chain);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700407 else {
408 new_hw_chain = 0;
409
410 old_chain_tail = list_entry(mv_chan->chain.prev,
411 struct mv_xor_desc_slot,
412 chain_node);
Lior Amsalemdfc97662014-08-27 10:52:51 -0300413 list_add_tail(&sw_desc->chain_node, &mv_chan->chain);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700414
Olof Johansson31fd8f52014-02-03 17:13:23 -0800415 dev_dbg(mv_chan_to_devp(mv_chan), "Append to last desc %pa\n",
416 &old_chain_tail->async_tx.phys);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700417
418 /* fix up the hardware chain */
Lior Amsalemdfc97662014-08-27 10:52:51 -0300419 mv_desc_set_next_desc(old_chain_tail, sw_desc->async_tx.phys);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700420
421 /* if the channel is not busy */
422 if (!mv_chan_is_busy(mv_chan)) {
423 u32 current_desc = mv_chan_get_current_desc(mv_chan);
424 /*
425 * and the curren desc is the end of the chain before
426 * the append, then we need to start the channel
427 */
428 if (current_desc == old_chain_tail->async_tx.phys)
429 new_hw_chain = 1;
430 }
431 }
432
433 if (new_hw_chain)
Lior Amsalemdfc97662014-08-27 10:52:51 -0300434 mv_xor_start_new_chain(mv_chan, sw_desc);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700435
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700436 spin_unlock_bh(&mv_chan->lock);
437
438 return cookie;
439}
440
441/* returns the number of allocated descriptors */
Dan Williamsaa1e6f12009-01-06 11:38:17 -0700442static int mv_xor_alloc_chan_resources(struct dma_chan *chan)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700443{
Olof Johansson31fd8f52014-02-03 17:13:23 -0800444 void *virt_desc;
445 dma_addr_t dma_desc;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700446 int idx;
447 struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
448 struct mv_xor_desc_slot *slot = NULL;
Thomas Petazzonib503fa02012-11-15 15:55:30 +0100449 int num_descs_in_pool = MV_XOR_POOL_SIZE/MV_XOR_SLOT_SIZE;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700450
451 /* Allocate descriptor slots */
452 idx = mv_chan->slots_allocated;
453 while (idx < num_descs_in_pool) {
454 slot = kzalloc(sizeof(*slot), GFP_KERNEL);
455 if (!slot) {
Ezequiel Garciab8291dd2014-08-27 10:52:49 -0300456 dev_info(mv_chan_to_devp(mv_chan),
457 "channel only initialized %d descriptor slots",
458 idx);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700459 break;
460 }
Olof Johansson31fd8f52014-02-03 17:13:23 -0800461 virt_desc = mv_chan->dma_desc_pool_virt;
462 slot->hw_desc = virt_desc + idx * MV_XOR_SLOT_SIZE;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700463
464 dma_async_tx_descriptor_init(&slot->async_tx, chan);
465 slot->async_tx.tx_submit = mv_xor_tx_submit;
466 INIT_LIST_HEAD(&slot->chain_node);
467 INIT_LIST_HEAD(&slot->slot_node);
Olof Johansson31fd8f52014-02-03 17:13:23 -0800468 dma_desc = mv_chan->dma_desc_pool;
469 slot->async_tx.phys = dma_desc + idx * MV_XOR_SLOT_SIZE;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700470 slot->idx = idx++;
471
472 spin_lock_bh(&mv_chan->lock);
473 mv_chan->slots_allocated = idx;
474 list_add_tail(&slot->slot_node, &mv_chan->all_slots);
475 spin_unlock_bh(&mv_chan->lock);
476 }
477
478 if (mv_chan->slots_allocated && !mv_chan->last_used)
479 mv_chan->last_used = list_entry(mv_chan->all_slots.next,
480 struct mv_xor_desc_slot,
481 slot_node);
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 "allocated %d descriptor slots last_used: %p\n",
485 mv_chan->slots_allocated, mv_chan->last_used);
486
487 return mv_chan->slots_allocated ? : -ENOMEM;
488}
489
490static struct dma_async_tx_descriptor *
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700491mv_xor_prep_dma_xor(struct dma_chan *chan, dma_addr_t dest, dma_addr_t *src,
492 unsigned int src_cnt, size_t len, unsigned long flags)
493{
494 struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
Lior Amsalemdfc97662014-08-27 10:52:51 -0300495 struct mv_xor_desc_slot *sw_desc;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700496
497 if (unlikely(len < MV_XOR_MIN_BYTE_COUNT))
498 return NULL;
499
Coly Li7912d302011-03-27 01:26:53 +0800500 BUG_ON(len > MV_XOR_MAX_BYTE_COUNT);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700501
Thomas Petazzonic98c1782012-11-15 14:17:18 +0100502 dev_dbg(mv_chan_to_devp(mv_chan),
Olof Johansson31fd8f52014-02-03 17:13:23 -0800503 "%s src_cnt: %d len: %u dest %pad flags: %ld\n",
504 __func__, src_cnt, len, &dest, flags);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700505
506 spin_lock_bh(&mv_chan->lock);
Lior Amsalemdfc97662014-08-27 10:52:51 -0300507 sw_desc = mv_xor_alloc_slot(mv_chan);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700508 if (sw_desc) {
509 sw_desc->type = DMA_XOR;
510 sw_desc->async_tx.flags = flags;
Lior Amsalemba87d132014-08-27 10:52:53 -0300511 mv_desc_init(sw_desc, dest, len, flags);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700512 while (src_cnt--)
Lior Amsalemdfc97662014-08-27 10:52:51 -0300513 mv_desc_set_src_addr(sw_desc, src_cnt, src[src_cnt]);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700514 }
515 spin_unlock_bh(&mv_chan->lock);
Thomas Petazzonic98c1782012-11-15 14:17:18 +0100516 dev_dbg(mv_chan_to_devp(mv_chan),
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700517 "%s sw_desc %p async_tx %p \n",
518 __func__, sw_desc, &sw_desc->async_tx);
519 return sw_desc ? &sw_desc->async_tx : NULL;
520}
521
Lior Amsalem3e4f52e2014-08-27 10:52:50 -0300522static struct dma_async_tx_descriptor *
523mv_xor_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
524 size_t len, unsigned long flags)
525{
526 /*
527 * A MEMCPY operation is identical to an XOR operation with only
528 * a single source address.
529 */
530 return mv_xor_prep_dma_xor(chan, dest, &src, 1, len, flags);
531}
532
Lior Amsalem22843542014-08-27 10:52:55 -0300533static struct dma_async_tx_descriptor *
534mv_xor_prep_dma_interrupt(struct dma_chan *chan, unsigned long flags)
535{
536 struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
537 dma_addr_t src, dest;
538 size_t len;
539
540 src = mv_chan->dummy_src_addr;
541 dest = mv_chan->dummy_dst_addr;
542 len = MV_XOR_MIN_BYTE_COUNT;
543
544 /*
545 * We implement the DMA_INTERRUPT operation as a minimum sized
546 * XOR operation with a single dummy source address.
547 */
548 return mv_xor_prep_dma_xor(chan, dest, &src, 1, len, flags);
549}
550
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700551static void mv_xor_free_chan_resources(struct dma_chan *chan)
552{
553 struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
554 struct mv_xor_desc_slot *iter, *_iter;
555 int in_use_descs = 0;
556
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700557 spin_lock_bh(&mv_chan->lock);
Ezequiel Garciae43147a2014-03-07 16:46:46 -0300558
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700559 mv_xor_slot_cleanup(mv_chan);
560
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700561 list_for_each_entry_safe(iter, _iter, &mv_chan->chain,
562 chain_node) {
563 in_use_descs++;
564 list_del(&iter->chain_node);
565 }
566 list_for_each_entry_safe(iter, _iter, &mv_chan->completed_slots,
567 completed_node) {
568 in_use_descs++;
569 list_del(&iter->completed_node);
570 }
571 list_for_each_entry_safe_reverse(
572 iter, _iter, &mv_chan->all_slots, slot_node) {
573 list_del(&iter->slot_node);
574 kfree(iter);
575 mv_chan->slots_allocated--;
576 }
577 mv_chan->last_used = NULL;
578
Thomas Petazzonic98c1782012-11-15 14:17:18 +0100579 dev_dbg(mv_chan_to_devp(mv_chan), "%s slots_allocated %d\n",
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700580 __func__, mv_chan->slots_allocated);
581 spin_unlock_bh(&mv_chan->lock);
582
583 if (in_use_descs)
Thomas Petazzonic98c1782012-11-15 14:17:18 +0100584 dev_err(mv_chan_to_devp(mv_chan),
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700585 "freeing %d in use descriptors!\n", in_use_descs);
586}
587
588/**
Linus Walleij07934482010-03-26 16:50:49 -0700589 * mv_xor_status - poll the status of an XOR transaction
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700590 * @chan: XOR channel handle
591 * @cookie: XOR transaction identifier
Linus Walleij07934482010-03-26 16:50:49 -0700592 * @txstate: XOR transactions state holder (or NULL)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700593 */
Linus Walleij07934482010-03-26 16:50:49 -0700594static enum dma_status mv_xor_status(struct dma_chan *chan,
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700595 dma_cookie_t cookie,
Linus Walleij07934482010-03-26 16:50:49 -0700596 struct dma_tx_state *txstate)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700597{
598 struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700599 enum dma_status ret;
600
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +0000601 ret = dma_cookie_status(chan, cookie, txstate);
Ezequiel Garcia890766d2014-03-07 16:46:45 -0300602 if (ret == DMA_COMPLETE)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700603 return ret;
Ezequiel Garciae43147a2014-03-07 16:46:46 -0300604
605 spin_lock_bh(&mv_chan->lock);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700606 mv_xor_slot_cleanup(mv_chan);
Ezequiel Garciae43147a2014-03-07 16:46:46 -0300607 spin_unlock_bh(&mv_chan->lock);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700608
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +0000609 return dma_cookie_status(chan, cookie, txstate);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700610}
611
612static void mv_dump_xor_regs(struct mv_xor_chan *chan)
613{
614 u32 val;
615
Thomas Petazzoni5733c382013-07-29 17:42:13 +0200616 val = readl_relaxed(XOR_CONFIG(chan));
Joe Perches1ba151c2012-10-28 01:05:44 -0700617 dev_err(mv_chan_to_devp(chan), "config 0x%08x\n", val);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700618
Thomas Petazzoni5733c382013-07-29 17:42:13 +0200619 val = readl_relaxed(XOR_ACTIVATION(chan));
Joe Perches1ba151c2012-10-28 01:05:44 -0700620 dev_err(mv_chan_to_devp(chan), "activation 0x%08x\n", val);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700621
Thomas Petazzoni5733c382013-07-29 17:42:13 +0200622 val = readl_relaxed(XOR_INTR_CAUSE(chan));
Joe Perches1ba151c2012-10-28 01:05:44 -0700623 dev_err(mv_chan_to_devp(chan), "intr cause 0x%08x\n", val);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700624
Thomas Petazzoni5733c382013-07-29 17:42:13 +0200625 val = readl_relaxed(XOR_INTR_MASK(chan));
Joe Perches1ba151c2012-10-28 01:05:44 -0700626 dev_err(mv_chan_to_devp(chan), "intr mask 0x%08x\n", val);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700627
Thomas Petazzoni5733c382013-07-29 17:42:13 +0200628 val = readl_relaxed(XOR_ERROR_CAUSE(chan));
Joe Perches1ba151c2012-10-28 01:05:44 -0700629 dev_err(mv_chan_to_devp(chan), "error cause 0x%08x\n", val);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700630
Thomas Petazzoni5733c382013-07-29 17:42:13 +0200631 val = readl_relaxed(XOR_ERROR_ADDR(chan));
Joe Perches1ba151c2012-10-28 01:05:44 -0700632 dev_err(mv_chan_to_devp(chan), "error addr 0x%08x\n", val);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700633}
634
635static void mv_xor_err_interrupt_handler(struct mv_xor_chan *chan,
636 u32 intr_cause)
637{
Ezequiel Garcia0e7488e2014-08-27 10:52:52 -0300638 if (intr_cause & XOR_INT_ERR_DECODE) {
639 dev_dbg(mv_chan_to_devp(chan), "ignoring address decode error\n");
640 return;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700641 }
642
Ezequiel Garcia0e7488e2014-08-27 10:52:52 -0300643 dev_err(mv_chan_to_devp(chan), "error on chan %d. intr cause 0x%08x\n",
Thomas Petazzonia3fc74b2012-11-15 12:50:27 +0100644 chan->idx, intr_cause);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700645
646 mv_dump_xor_regs(chan);
Ezequiel Garcia0e7488e2014-08-27 10:52:52 -0300647 WARN_ON(1);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700648}
649
650static irqreturn_t mv_xor_interrupt_handler(int irq, void *data)
651{
652 struct mv_xor_chan *chan = data;
653 u32 intr_cause = mv_chan_get_intr_cause(chan);
654
Thomas Petazzonic98c1782012-11-15 14:17:18 +0100655 dev_dbg(mv_chan_to_devp(chan), "intr cause %x\n", intr_cause);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700656
Ezequiel Garcia0e7488e2014-08-27 10:52:52 -0300657 if (intr_cause & XOR_INTR_ERRORS)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700658 mv_xor_err_interrupt_handler(chan, intr_cause);
659
660 tasklet_schedule(&chan->irq_tasklet);
661
662 mv_xor_device_clear_eoc_cause(chan);
663
664 return IRQ_HANDLED;
665}
666
667static void mv_xor_issue_pending(struct dma_chan *chan)
668{
669 struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
670
671 if (mv_chan->pending >= MV_XOR_THRESHOLD) {
672 mv_chan->pending = 0;
673 mv_chan_activate(mv_chan);
674 }
675}
676
677/*
678 * Perform a transaction to verify the HW works.
679 */
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700680
Linus Torvaldsc2714332012-12-14 14:54:26 -0800681static int mv_xor_memcpy_self_test(struct mv_xor_chan *mv_chan)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700682{
Ezequiel Garciab8c01d22013-12-10 09:32:37 -0300683 int i, ret;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700684 void *src, *dest;
685 dma_addr_t src_dma, dest_dma;
686 struct dma_chan *dma_chan;
687 dma_cookie_t cookie;
688 struct dma_async_tx_descriptor *tx;
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300689 struct dmaengine_unmap_data *unmap;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700690 int err = 0;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700691
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300692 src = kmalloc(sizeof(u8) * PAGE_SIZE, GFP_KERNEL);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700693 if (!src)
694 return -ENOMEM;
695
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300696 dest = kzalloc(sizeof(u8) * PAGE_SIZE, GFP_KERNEL);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700697 if (!dest) {
698 kfree(src);
699 return -ENOMEM;
700 }
701
702 /* Fill in src buffer */
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300703 for (i = 0; i < PAGE_SIZE; i++)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700704 ((u8 *) src)[i] = (u8)i;
705
Thomas Petazzoni275cc0c2012-11-15 15:09:42 +0100706 dma_chan = &mv_chan->dmachan;
Dan Williamsaa1e6f12009-01-06 11:38:17 -0700707 if (mv_xor_alloc_chan_resources(dma_chan) < 1) {
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700708 err = -ENODEV;
709 goto out;
710 }
711
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300712 unmap = dmaengine_get_unmap_data(dma_chan->device->dev, 2, GFP_KERNEL);
713 if (!unmap) {
714 err = -ENOMEM;
715 goto free_resources;
716 }
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700717
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300718 src_dma = dma_map_page(dma_chan->device->dev, virt_to_page(src), 0,
719 PAGE_SIZE, DMA_TO_DEVICE);
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300720 unmap->addr[0] = src_dma;
721
Ezequiel Garciab8c01d22013-12-10 09:32:37 -0300722 ret = dma_mapping_error(dma_chan->device->dev, src_dma);
723 if (ret) {
724 err = -ENOMEM;
725 goto free_resources;
726 }
727 unmap->to_cnt = 1;
728
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300729 dest_dma = dma_map_page(dma_chan->device->dev, virt_to_page(dest), 0,
730 PAGE_SIZE, DMA_FROM_DEVICE);
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300731 unmap->addr[1] = dest_dma;
732
Ezequiel Garciab8c01d22013-12-10 09:32:37 -0300733 ret = dma_mapping_error(dma_chan->device->dev, dest_dma);
734 if (ret) {
735 err = -ENOMEM;
736 goto free_resources;
737 }
738 unmap->from_cnt = 1;
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300739 unmap->len = PAGE_SIZE;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700740
741 tx = mv_xor_prep_dma_memcpy(dma_chan, dest_dma, src_dma,
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300742 PAGE_SIZE, 0);
Ezequiel Garciab8c01d22013-12-10 09:32:37 -0300743 if (!tx) {
744 dev_err(dma_chan->device->dev,
745 "Self-test cannot prepare operation, disabling\n");
746 err = -ENODEV;
747 goto free_resources;
748 }
749
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700750 cookie = mv_xor_tx_submit(tx);
Ezequiel Garciab8c01d22013-12-10 09:32:37 -0300751 if (dma_submit_error(cookie)) {
752 dev_err(dma_chan->device->dev,
753 "Self-test submit error, disabling\n");
754 err = -ENODEV;
755 goto free_resources;
756 }
757
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700758 mv_xor_issue_pending(dma_chan);
759 async_tx_ack(tx);
760 msleep(1);
761
Linus Walleij07934482010-03-26 16:50:49 -0700762 if (mv_xor_status(dma_chan, cookie, NULL) !=
Vinod Koulb3efb8f2013-10-16 20:51:04 +0530763 DMA_COMPLETE) {
Thomas Petazzonia3fc74b2012-11-15 12:50:27 +0100764 dev_err(dma_chan->device->dev,
765 "Self-test copy timed out, disabling\n");
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700766 err = -ENODEV;
767 goto free_resources;
768 }
769
Thomas Petazzonic35064c2012-11-15 13:01:59 +0100770 dma_sync_single_for_cpu(dma_chan->device->dev, dest_dma,
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300771 PAGE_SIZE, DMA_FROM_DEVICE);
772 if (memcmp(src, dest, PAGE_SIZE)) {
Thomas Petazzonia3fc74b2012-11-15 12:50:27 +0100773 dev_err(dma_chan->device->dev,
774 "Self-test copy failed compare, disabling\n");
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700775 err = -ENODEV;
776 goto free_resources;
777 }
778
779free_resources:
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300780 dmaengine_unmap_put(unmap);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700781 mv_xor_free_chan_resources(dma_chan);
782out:
783 kfree(src);
784 kfree(dest);
785 return err;
786}
787
788#define MV_XOR_NUM_SRC_TEST 4 /* must be <= 15 */
Bill Pemberton463a1f82012-11-19 13:22:55 -0500789static int
Thomas Petazzoni275cc0c2012-11-15 15:09:42 +0100790mv_xor_xor_self_test(struct mv_xor_chan *mv_chan)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700791{
Ezequiel Garciab8c01d22013-12-10 09:32:37 -0300792 int i, src_idx, ret;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700793 struct page *dest;
794 struct page *xor_srcs[MV_XOR_NUM_SRC_TEST];
795 dma_addr_t dma_srcs[MV_XOR_NUM_SRC_TEST];
796 dma_addr_t dest_dma;
797 struct dma_async_tx_descriptor *tx;
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300798 struct dmaengine_unmap_data *unmap;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700799 struct dma_chan *dma_chan;
800 dma_cookie_t cookie;
801 u8 cmp_byte = 0;
802 u32 cmp_word;
803 int err = 0;
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300804 int src_count = MV_XOR_NUM_SRC_TEST;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700805
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300806 for (src_idx = 0; src_idx < src_count; src_idx++) {
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700807 xor_srcs[src_idx] = alloc_page(GFP_KERNEL);
Roel Kluina09b09a2009-02-25 13:56:21 +0100808 if (!xor_srcs[src_idx]) {
809 while (src_idx--)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700810 __free_page(xor_srcs[src_idx]);
Roel Kluina09b09a2009-02-25 13:56:21 +0100811 return -ENOMEM;
812 }
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700813 }
814
815 dest = alloc_page(GFP_KERNEL);
Roel Kluina09b09a2009-02-25 13:56:21 +0100816 if (!dest) {
817 while (src_idx--)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700818 __free_page(xor_srcs[src_idx]);
Roel Kluina09b09a2009-02-25 13:56:21 +0100819 return -ENOMEM;
820 }
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700821
822 /* Fill in src buffers */
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300823 for (src_idx = 0; src_idx < src_count; src_idx++) {
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700824 u8 *ptr = page_address(xor_srcs[src_idx]);
825 for (i = 0; i < PAGE_SIZE; i++)
826 ptr[i] = (1 << src_idx);
827 }
828
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300829 for (src_idx = 0; src_idx < src_count; src_idx++)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700830 cmp_byte ^= (u8) (1 << src_idx);
831
832 cmp_word = (cmp_byte << 24) | (cmp_byte << 16) |
833 (cmp_byte << 8) | cmp_byte;
834
835 memset(page_address(dest), 0, PAGE_SIZE);
836
Thomas Petazzoni275cc0c2012-11-15 15:09:42 +0100837 dma_chan = &mv_chan->dmachan;
Dan Williamsaa1e6f12009-01-06 11:38:17 -0700838 if (mv_xor_alloc_chan_resources(dma_chan) < 1) {
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700839 err = -ENODEV;
840 goto out;
841 }
842
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300843 unmap = dmaengine_get_unmap_data(dma_chan->device->dev, src_count + 1,
844 GFP_KERNEL);
845 if (!unmap) {
846 err = -ENOMEM;
847 goto free_resources;
848 }
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700849
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300850 /* test xor */
851 for (i = 0; i < src_count; i++) {
852 unmap->addr[i] = dma_map_page(dma_chan->device->dev, xor_srcs[i],
853 0, PAGE_SIZE, DMA_TO_DEVICE);
854 dma_srcs[i] = unmap->addr[i];
Ezequiel Garciab8c01d22013-12-10 09:32:37 -0300855 ret = dma_mapping_error(dma_chan->device->dev, unmap->addr[i]);
856 if (ret) {
857 err = -ENOMEM;
858 goto free_resources;
859 }
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300860 unmap->to_cnt++;
861 }
862
863 unmap->addr[src_count] = dma_map_page(dma_chan->device->dev, dest, 0, PAGE_SIZE,
864 DMA_FROM_DEVICE);
865 dest_dma = unmap->addr[src_count];
Ezequiel Garciab8c01d22013-12-10 09:32:37 -0300866 ret = dma_mapping_error(dma_chan->device->dev, unmap->addr[src_count]);
867 if (ret) {
868 err = -ENOMEM;
869 goto free_resources;
870 }
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300871 unmap->from_cnt = 1;
872 unmap->len = PAGE_SIZE;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700873
874 tx = mv_xor_prep_dma_xor(dma_chan, dest_dma, dma_srcs,
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300875 src_count, PAGE_SIZE, 0);
Ezequiel Garciab8c01d22013-12-10 09:32:37 -0300876 if (!tx) {
877 dev_err(dma_chan->device->dev,
878 "Self-test cannot prepare operation, disabling\n");
879 err = -ENODEV;
880 goto free_resources;
881 }
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700882
883 cookie = mv_xor_tx_submit(tx);
Ezequiel Garciab8c01d22013-12-10 09:32:37 -0300884 if (dma_submit_error(cookie)) {
885 dev_err(dma_chan->device->dev,
886 "Self-test submit error, disabling\n");
887 err = -ENODEV;
888 goto free_resources;
889 }
890
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700891 mv_xor_issue_pending(dma_chan);
892 async_tx_ack(tx);
893 msleep(8);
894
Linus Walleij07934482010-03-26 16:50:49 -0700895 if (mv_xor_status(dma_chan, cookie, NULL) !=
Vinod Koulb3efb8f2013-10-16 20:51:04 +0530896 DMA_COMPLETE) {
Thomas Petazzonia3fc74b2012-11-15 12:50:27 +0100897 dev_err(dma_chan->device->dev,
898 "Self-test xor timed out, disabling\n");
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700899 err = -ENODEV;
900 goto free_resources;
901 }
902
Thomas Petazzonic35064c2012-11-15 13:01:59 +0100903 dma_sync_single_for_cpu(dma_chan->device->dev, dest_dma,
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700904 PAGE_SIZE, DMA_FROM_DEVICE);
905 for (i = 0; i < (PAGE_SIZE / sizeof(u32)); i++) {
906 u32 *ptr = page_address(dest);
907 if (ptr[i] != cmp_word) {
Thomas Petazzonia3fc74b2012-11-15 12:50:27 +0100908 dev_err(dma_chan->device->dev,
Joe Perches1ba151c2012-10-28 01:05:44 -0700909 "Self-test xor failed compare, disabling. index %d, data %x, expected %x\n",
910 i, ptr[i], cmp_word);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700911 err = -ENODEV;
912 goto free_resources;
913 }
914 }
915
916free_resources:
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300917 dmaengine_unmap_put(unmap);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700918 mv_xor_free_chan_resources(dma_chan);
919out:
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300920 src_idx = src_count;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700921 while (src_idx--)
922 __free_page(xor_srcs[src_idx]);
923 __free_page(dest);
924 return err;
925}
926
Thomas Petazzoni1ef48a22012-11-15 15:17:05 +0100927static int mv_xor_channel_remove(struct mv_xor_chan *mv_chan)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700928{
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700929 struct dma_chan *chan, *_chan;
Thomas Petazzoni1ef48a22012-11-15 15:17:05 +0100930 struct device *dev = mv_chan->dmadev.dev;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700931
Thomas Petazzoni1ef48a22012-11-15 15:17:05 +0100932 dma_async_device_unregister(&mv_chan->dmadev);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700933
Thomas Petazzonib503fa02012-11-15 15:55:30 +0100934 dma_free_coherent(dev, MV_XOR_POOL_SIZE,
Thomas Petazzoni1ef48a22012-11-15 15:17:05 +0100935 mv_chan->dma_desc_pool_virt, mv_chan->dma_desc_pool);
Lior Amsalem22843542014-08-27 10:52:55 -0300936 dma_unmap_single(dev, mv_chan->dummy_src_addr,
937 MV_XOR_MIN_BYTE_COUNT, DMA_FROM_DEVICE);
938 dma_unmap_single(dev, mv_chan->dummy_dst_addr,
939 MV_XOR_MIN_BYTE_COUNT, DMA_TO_DEVICE);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700940
Thomas Petazzoni1ef48a22012-11-15 15:17:05 +0100941 list_for_each_entry_safe(chan, _chan, &mv_chan->dmadev.channels,
Thomas Petazzonia6b4a9d2012-10-29 16:45:46 +0100942 device_node) {
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700943 list_del(&chan->device_node);
944 }
945
Thomas Petazzoni88eb92c2012-11-15 16:11:18 +0100946 free_irq(mv_chan->irq, mv_chan);
947
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700948 return 0;
949}
950
Thomas Petazzoni1ef48a22012-11-15 15:17:05 +0100951static struct mv_xor_chan *
Thomas Petazzoni297eedb2012-11-15 15:29:53 +0100952mv_xor_channel_add(struct mv_xor_device *xordev,
Thomas Petazzonia6b4a9d2012-10-29 16:45:46 +0100953 struct platform_device *pdev,
Thomas Petazzonib503fa02012-11-15 15:55:30 +0100954 int idx, dma_cap_mask_t cap_mask, int irq)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700955{
956 int ret = 0;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700957 struct mv_xor_chan *mv_chan;
958 struct dma_device *dma_dev;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700959
Thomas Petazzoni1ef48a22012-11-15 15:17:05 +0100960 mv_chan = devm_kzalloc(&pdev->dev, sizeof(*mv_chan), GFP_KERNEL);
Sachin Kamata5776592013-09-02 13:54:20 +0530961 if (!mv_chan)
962 return ERR_PTR(-ENOMEM);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700963
Thomas Petazzoni9aedbdb2012-11-15 15:36:37 +0100964 mv_chan->idx = idx;
Thomas Petazzoni88eb92c2012-11-15 16:11:18 +0100965 mv_chan->irq = irq;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700966
Thomas Petazzoni1ef48a22012-11-15 15:17:05 +0100967 dma_dev = &mv_chan->dmadev;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700968
Lior Amsalem22843542014-08-27 10:52:55 -0300969 /*
970 * These source and destination dummy buffers are used to implement
971 * a DMA_INTERRUPT operation as a minimum-sized XOR operation.
972 * Hence, we only need to map the buffers at initialization-time.
973 */
974 mv_chan->dummy_src_addr = dma_map_single(dma_dev->dev,
975 mv_chan->dummy_src, MV_XOR_MIN_BYTE_COUNT, DMA_FROM_DEVICE);
976 mv_chan->dummy_dst_addr = dma_map_single(dma_dev->dev,
977 mv_chan->dummy_dst, MV_XOR_MIN_BYTE_COUNT, DMA_TO_DEVICE);
978
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700979 /* allocate coherent memory for hardware descriptors
980 * note: writecombine gives slightly better performance, but
981 * requires that we explicitly flush the writes
982 */
Thomas Petazzoni1ef48a22012-11-15 15:17:05 +0100983 mv_chan->dma_desc_pool_virt =
Thomas Petazzonib503fa02012-11-15 15:55:30 +0100984 dma_alloc_writecombine(&pdev->dev, MV_XOR_POOL_SIZE,
Thomas Petazzoni1ef48a22012-11-15 15:17:05 +0100985 &mv_chan->dma_desc_pool, GFP_KERNEL);
986 if (!mv_chan->dma_desc_pool_virt)
Thomas Petazzonia6b4a9d2012-10-29 16:45:46 +0100987 return ERR_PTR(-ENOMEM);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700988
989 /* discover transaction capabilites from the platform data */
Thomas Petazzonia6b4a9d2012-10-29 16:45:46 +0100990 dma_dev->cap_mask = cap_mask;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700991
992 INIT_LIST_HEAD(&dma_dev->channels);
993
994 /* set base routines */
995 dma_dev->device_alloc_chan_resources = mv_xor_alloc_chan_resources;
996 dma_dev->device_free_chan_resources = mv_xor_free_chan_resources;
Linus Walleij07934482010-03-26 16:50:49 -0700997 dma_dev->device_tx_status = mv_xor_status;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700998 dma_dev->device_issue_pending = mv_xor_issue_pending;
999 dma_dev->dev = &pdev->dev;
1000
1001 /* set prep routines based on capability */
Lior Amsalem22843542014-08-27 10:52:55 -03001002 if (dma_has_cap(DMA_INTERRUPT, dma_dev->cap_mask))
1003 dma_dev->device_prep_dma_interrupt = mv_xor_prep_dma_interrupt;
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001004 if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask))
1005 dma_dev->device_prep_dma_memcpy = mv_xor_prep_dma_memcpy;
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001006 if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
Joe Perchesc0198942009-06-28 09:26:21 -07001007 dma_dev->max_xor = 8;
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001008 dma_dev->device_prep_dma_xor = mv_xor_prep_dma_xor;
1009 }
1010
Thomas Petazzoni297eedb2012-11-15 15:29:53 +01001011 mv_chan->mmr_base = xordev->xor_base;
Ezequiel Garcia82a14022013-10-30 12:01:43 -03001012 mv_chan->mmr_high_base = xordev->xor_high_base;
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001013 tasklet_init(&mv_chan->irq_tasklet, mv_xor_tasklet, (unsigned long)
1014 mv_chan);
1015
1016 /* clear errors before enabling interrupts */
1017 mv_xor_device_clear_err_status(mv_chan);
1018
Thomas Petazzoni2d0a0742012-11-22 18:19:09 +01001019 ret = request_irq(mv_chan->irq, mv_xor_interrupt_handler,
1020 0, dev_name(&pdev->dev), mv_chan);
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001021 if (ret)
1022 goto err_free_dma;
1023
1024 mv_chan_unmask_interrupts(mv_chan);
1025
Lior Amsalem3e4f52e2014-08-27 10:52:50 -03001026 mv_set_mode(mv_chan, DMA_XOR);
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001027
1028 spin_lock_init(&mv_chan->lock);
1029 INIT_LIST_HEAD(&mv_chan->chain);
1030 INIT_LIST_HEAD(&mv_chan->completed_slots);
1031 INIT_LIST_HEAD(&mv_chan->all_slots);
Thomas Petazzoni98817b92012-11-15 14:57:44 +01001032 mv_chan->dmachan.device = dma_dev;
1033 dma_cookie_init(&mv_chan->dmachan);
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001034
Thomas Petazzoni98817b92012-11-15 14:57:44 +01001035 list_add_tail(&mv_chan->dmachan.device_node, &dma_dev->channels);
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001036
1037 if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask)) {
Thomas Petazzoni275cc0c2012-11-15 15:09:42 +01001038 ret = mv_xor_memcpy_self_test(mv_chan);
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001039 dev_dbg(&pdev->dev, "memcpy self test returned %d\n", ret);
1040 if (ret)
Thomas Petazzoni2d0a0742012-11-22 18:19:09 +01001041 goto err_free_irq;
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001042 }
1043
1044 if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
Thomas Petazzoni275cc0c2012-11-15 15:09:42 +01001045 ret = mv_xor_xor_self_test(mv_chan);
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001046 dev_dbg(&pdev->dev, "xor self test returned %d\n", ret);
1047 if (ret)
Thomas Petazzoni2d0a0742012-11-22 18:19:09 +01001048 goto err_free_irq;
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001049 }
1050
Bartlomiej Zolnierkiewicz48a9db42013-07-03 15:05:06 -07001051 dev_info(&pdev->dev, "Marvell XOR: ( %s%s%s)\n",
Joe Perches1ba151c2012-10-28 01:05:44 -07001052 dma_has_cap(DMA_XOR, dma_dev->cap_mask) ? "xor " : "",
Joe Perches1ba151c2012-10-28 01:05:44 -07001053 dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask) ? "cpy " : "",
1054 dma_has_cap(DMA_INTERRUPT, dma_dev->cap_mask) ? "intr " : "");
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001055
1056 dma_async_device_register(dma_dev);
Thomas Petazzoni1ef48a22012-11-15 15:17:05 +01001057 return mv_chan;
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001058
Thomas Petazzoni2d0a0742012-11-22 18:19:09 +01001059err_free_irq:
1060 free_irq(mv_chan->irq, mv_chan);
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001061 err_free_dma:
Thomas Petazzonib503fa02012-11-15 15:55:30 +01001062 dma_free_coherent(&pdev->dev, MV_XOR_POOL_SIZE,
Thomas Petazzoni1ef48a22012-11-15 15:17:05 +01001063 mv_chan->dma_desc_pool_virt, mv_chan->dma_desc_pool);
Thomas Petazzonia6b4a9d2012-10-29 16:45:46 +01001064 return ERR_PTR(ret);
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001065}
1066
1067static void
Thomas Petazzoni297eedb2012-11-15 15:29:53 +01001068mv_xor_conf_mbus_windows(struct mv_xor_device *xordev,
Andrew Lunn63a93322011-12-07 21:48:07 +01001069 const struct mbus_dram_target_info *dram)
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001070{
Ezequiel Garcia82a14022013-10-30 12:01:43 -03001071 void __iomem *base = xordev->xor_high_base;
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001072 u32 win_enable = 0;
1073 int i;
1074
1075 for (i = 0; i < 8; i++) {
1076 writel(0, base + WINDOW_BASE(i));
1077 writel(0, base + WINDOW_SIZE(i));
1078 if (i < 4)
1079 writel(0, base + WINDOW_REMAP_HIGH(i));
1080 }
1081
1082 for (i = 0; i < dram->num_cs; i++) {
Andrew Lunn63a93322011-12-07 21:48:07 +01001083 const struct mbus_dram_window *cs = dram->cs + i;
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001084
1085 writel((cs->base & 0xffff0000) |
1086 (cs->mbus_attr << 8) |
1087 dram->mbus_dram_target_id, base + WINDOW_BASE(i));
1088 writel((cs->size - 1) & 0xffff0000, base + WINDOW_SIZE(i));
1089
1090 win_enable |= (1 << i);
1091 win_enable |= 3 << (16 + (2 * i));
1092 }
1093
1094 writel(win_enable, base + WINDOW_BAR_ENABLE(0));
1095 writel(win_enable, base + WINDOW_BAR_ENABLE(1));
Thomas Petazzonic4b4b732012-11-22 18:16:37 +01001096 writel(0, base + WINDOW_OVERRIDE_CTRL(0));
1097 writel(0, base + WINDOW_OVERRIDE_CTRL(1));
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001098}
1099
Linus Torvaldsc2714332012-12-14 14:54:26 -08001100static int mv_xor_probe(struct platform_device *pdev)
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001101{
Andrew Lunn63a93322011-12-07 21:48:07 +01001102 const struct mbus_dram_target_info *dram;
Thomas Petazzoni297eedb2012-11-15 15:29:53 +01001103 struct mv_xor_device *xordev;
Jingoo Hand4adcc02013-07-30 17:09:11 +09001104 struct mv_xor_platform_data *pdata = dev_get_platdata(&pdev->dev);
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001105 struct resource *res;
Thomas Petazzoni60d151f2012-10-29 16:54:49 +01001106 int i, ret;
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001107
Joe Perches1ba151c2012-10-28 01:05:44 -07001108 dev_notice(&pdev->dev, "Marvell shared XOR driver\n");
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001109
Thomas Petazzoni297eedb2012-11-15 15:29:53 +01001110 xordev = devm_kzalloc(&pdev->dev, sizeof(*xordev), GFP_KERNEL);
1111 if (!xordev)
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001112 return -ENOMEM;
1113
1114 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1115 if (!res)
1116 return -ENODEV;
1117
Thomas Petazzoni297eedb2012-11-15 15:29:53 +01001118 xordev->xor_base = devm_ioremap(&pdev->dev, res->start,
1119 resource_size(res));
1120 if (!xordev->xor_base)
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001121 return -EBUSY;
1122
1123 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1124 if (!res)
1125 return -ENODEV;
1126
Thomas Petazzoni297eedb2012-11-15 15:29:53 +01001127 xordev->xor_high_base = devm_ioremap(&pdev->dev, res->start,
1128 resource_size(res));
1129 if (!xordev->xor_high_base)
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001130 return -EBUSY;
1131
Thomas Petazzoni297eedb2012-11-15 15:29:53 +01001132 platform_set_drvdata(pdev, xordev);
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001133
1134 /*
1135 * (Re-)program MBUS remapping windows if we are asked to.
1136 */
Andrew Lunn63a93322011-12-07 21:48:07 +01001137 dram = mv_mbus_dram_info();
1138 if (dram)
Thomas Petazzoni297eedb2012-11-15 15:29:53 +01001139 mv_xor_conf_mbus_windows(xordev, dram);
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001140
Andrew Lunnc5101822012-02-19 13:30:26 +01001141 /* Not all platforms can gate the clock, so it is not
1142 * an error if the clock does not exists.
1143 */
Thomas Petazzoni297eedb2012-11-15 15:29:53 +01001144 xordev->clk = clk_get(&pdev->dev, NULL);
1145 if (!IS_ERR(xordev->clk))
1146 clk_prepare_enable(xordev->clk);
Andrew Lunnc5101822012-02-19 13:30:26 +01001147
Thomas Petazzonif7d12ef2012-11-15 16:47:58 +01001148 if (pdev->dev.of_node) {
1149 struct device_node *np;
1150 int i = 0;
1151
1152 for_each_child_of_node(pdev->dev.of_node, np) {
Russell King0be82532013-12-12 23:59:08 +00001153 struct mv_xor_chan *chan;
Thomas Petazzonif7d12ef2012-11-15 16:47:58 +01001154 dma_cap_mask_t cap_mask;
1155 int irq;
1156
1157 dma_cap_zero(cap_mask);
1158 if (of_property_read_bool(np, "dmacap,memcpy"))
1159 dma_cap_set(DMA_MEMCPY, cap_mask);
1160 if (of_property_read_bool(np, "dmacap,xor"))
1161 dma_cap_set(DMA_XOR, cap_mask);
Thomas Petazzonif7d12ef2012-11-15 16:47:58 +01001162 if (of_property_read_bool(np, "dmacap,interrupt"))
1163 dma_cap_set(DMA_INTERRUPT, cap_mask);
1164
1165 irq = irq_of_parse_and_map(np, 0);
Thomas Petazzonif8eb9e72012-11-22 18:22:12 +01001166 if (!irq) {
1167 ret = -ENODEV;
Thomas Petazzonif7d12ef2012-11-15 16:47:58 +01001168 goto err_channel_add;
1169 }
1170
Russell King0be82532013-12-12 23:59:08 +00001171 chan = mv_xor_channel_add(xordev, pdev, i,
1172 cap_mask, irq);
1173 if (IS_ERR(chan)) {
1174 ret = PTR_ERR(chan);
Thomas Petazzonif7d12ef2012-11-15 16:47:58 +01001175 irq_dispose_mapping(irq);
1176 goto err_channel_add;
1177 }
1178
Russell King0be82532013-12-12 23:59:08 +00001179 xordev->channels[i] = chan;
Thomas Petazzonif7d12ef2012-11-15 16:47:58 +01001180 i++;
1181 }
1182 } else if (pdata && pdata->channels) {
Thomas Petazzoni60d151f2012-10-29 16:54:49 +01001183 for (i = 0; i < MV_XOR_MAX_CHANNELS; i++) {
Thomas Petazzonie39f6ec2012-10-30 11:56:26 +01001184 struct mv_xor_channel_data *cd;
Russell King0be82532013-12-12 23:59:08 +00001185 struct mv_xor_chan *chan;
Thomas Petazzoni60d151f2012-10-29 16:54:49 +01001186 int irq;
1187
1188 cd = &pdata->channels[i];
1189 if (!cd) {
1190 ret = -ENODEV;
1191 goto err_channel_add;
1192 }
1193
1194 irq = platform_get_irq(pdev, i);
1195 if (irq < 0) {
1196 ret = irq;
1197 goto err_channel_add;
1198 }
1199
Russell King0be82532013-12-12 23:59:08 +00001200 chan = mv_xor_channel_add(xordev, pdev, i,
1201 cd->cap_mask, irq);
1202 if (IS_ERR(chan)) {
1203 ret = PTR_ERR(chan);
Thomas Petazzoni60d151f2012-10-29 16:54:49 +01001204 goto err_channel_add;
1205 }
Russell King0be82532013-12-12 23:59:08 +00001206
1207 xordev->channels[i] = chan;
Thomas Petazzoni60d151f2012-10-29 16:54:49 +01001208 }
1209 }
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001210
1211 return 0;
Thomas Petazzoni60d151f2012-10-29 16:54:49 +01001212
1213err_channel_add:
1214 for (i = 0; i < MV_XOR_MAX_CHANNELS; i++)
Thomas Petazzonif7d12ef2012-11-15 16:47:58 +01001215 if (xordev->channels[i]) {
Thomas Petazzoniab6e4392013-01-06 11:10:43 +01001216 mv_xor_channel_remove(xordev->channels[i]);
Thomas Petazzonif7d12ef2012-11-15 16:47:58 +01001217 if (pdev->dev.of_node)
1218 irq_dispose_mapping(xordev->channels[i]->irq);
Thomas Petazzonif7d12ef2012-11-15 16:47:58 +01001219 }
Thomas Petazzoni60d151f2012-10-29 16:54:49 +01001220
Thomas Petazzonidab92062013-01-06 11:10:44 +01001221 if (!IS_ERR(xordev->clk)) {
1222 clk_disable_unprepare(xordev->clk);
1223 clk_put(xordev->clk);
1224 }
1225
Thomas Petazzoni60d151f2012-10-29 16:54:49 +01001226 return ret;
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001227}
1228
Linus Torvaldsc2714332012-12-14 14:54:26 -08001229static int mv_xor_remove(struct platform_device *pdev)
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001230{
Thomas Petazzoni297eedb2012-11-15 15:29:53 +01001231 struct mv_xor_device *xordev = platform_get_drvdata(pdev);
Thomas Petazzoni60d151f2012-10-29 16:54:49 +01001232 int i;
Andrew Lunnc5101822012-02-19 13:30:26 +01001233
Thomas Petazzoni60d151f2012-10-29 16:54:49 +01001234 for (i = 0; i < MV_XOR_MAX_CHANNELS; i++) {
Thomas Petazzoni297eedb2012-11-15 15:29:53 +01001235 if (xordev->channels[i])
1236 mv_xor_channel_remove(xordev->channels[i]);
Thomas Petazzoni60d151f2012-10-29 16:54:49 +01001237 }
Andrew Lunnc5101822012-02-19 13:30:26 +01001238
Thomas Petazzoni297eedb2012-11-15 15:29:53 +01001239 if (!IS_ERR(xordev->clk)) {
1240 clk_disable_unprepare(xordev->clk);
1241 clk_put(xordev->clk);
Andrew Lunnc5101822012-02-19 13:30:26 +01001242 }
1243
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001244 return 0;
1245}
1246
Thomas Petazzonif7d12ef2012-11-15 16:47:58 +01001247#ifdef CONFIG_OF
Fabian Frederick57c03422015-03-16 20:17:14 +01001248static const struct of_device_id mv_xor_dt_ids[] = {
Thomas Petazzonif7d12ef2012-11-15 16:47:58 +01001249 { .compatible = "marvell,orion-xor", },
1250 {},
1251};
1252MODULE_DEVICE_TABLE(of, mv_xor_dt_ids);
1253#endif
1254
Thomas Petazzoni61971652012-10-30 12:05:40 +01001255static struct platform_driver mv_xor_driver = {
1256 .probe = mv_xor_probe,
Linus Torvaldsc2714332012-12-14 14:54:26 -08001257 .remove = mv_xor_remove,
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001258 .driver = {
Thomas Petazzonif7d12ef2012-11-15 16:47:58 +01001259 .name = MV_XOR_NAME,
1260 .of_match_table = of_match_ptr(mv_xor_dt_ids),
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001261 },
1262};
1263
1264
1265static int __init mv_xor_init(void)
1266{
Thomas Petazzoni61971652012-10-30 12:05:40 +01001267 return platform_driver_register(&mv_xor_driver);
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001268}
1269module_init(mv_xor_init);
1270
1271/* it's currently unsafe to unload this module */
1272#if 0
1273static void __exit mv_xor_exit(void)
1274{
1275 platform_driver_unregister(&mv_xor_driver);
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001276 return;
1277}
1278
1279module_exit(mv_xor_exit);
1280#endif
1281
1282MODULE_AUTHOR("Saeed Bishara <saeed@marvell.com>");
1283MODULE_DESCRIPTION("DMA engine driver for Marvell's XOR engine");
1284MODULE_LICENSE("GPL");