xref: /openbmc/linux/drivers/dma/mv_xor.c (revision e43147acba6f64ba32664dd0e590657cc49b6940)
1ff7b0479SSaeed Bishara /*
2ff7b0479SSaeed Bishara  * offload engine driver for the Marvell XOR engine
3ff7b0479SSaeed Bishara  * Copyright (C) 2007, 2008, Marvell International Ltd.
4ff7b0479SSaeed Bishara  *
5ff7b0479SSaeed Bishara  * This program is free software; you can redistribute it and/or modify it
6ff7b0479SSaeed Bishara  * under the terms and conditions of the GNU General Public License,
7ff7b0479SSaeed Bishara  * version 2, as published by the Free Software Foundation.
8ff7b0479SSaeed Bishara  *
9ff7b0479SSaeed Bishara  * This program is distributed in the hope it will be useful, but WITHOUT
10ff7b0479SSaeed Bishara  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11ff7b0479SSaeed Bishara  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12ff7b0479SSaeed Bishara  * more details.
13ff7b0479SSaeed Bishara  *
14ff7b0479SSaeed Bishara  * You should have received a copy of the GNU General Public License along with
15ff7b0479SSaeed Bishara  * this program; if not, write to the Free Software Foundation, Inc.,
16ff7b0479SSaeed Bishara  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
17ff7b0479SSaeed Bishara  */
18ff7b0479SSaeed Bishara 
19ff7b0479SSaeed Bishara #include <linux/init.h>
20ff7b0479SSaeed Bishara #include <linux/module.h>
215a0e3ad6STejun Heo #include <linux/slab.h>
22ff7b0479SSaeed Bishara #include <linux/delay.h>
23ff7b0479SSaeed Bishara #include <linux/dma-mapping.h>
24ff7b0479SSaeed Bishara #include <linux/spinlock.h>
25ff7b0479SSaeed Bishara #include <linux/interrupt.h>
26ff7b0479SSaeed Bishara #include <linux/platform_device.h>
27ff7b0479SSaeed Bishara #include <linux/memory.h>
28c510182bSAndrew Lunn #include <linux/clk.h>
29f7d12ef5SThomas Petazzoni #include <linux/of.h>
30f7d12ef5SThomas Petazzoni #include <linux/of_irq.h>
31f7d12ef5SThomas Petazzoni #include <linux/irqdomain.h>
32c02cecb9SArnd Bergmann #include <linux/platform_data/dma-mv_xor.h>
33d2ebfb33SRussell King - ARM Linux 
34d2ebfb33SRussell King - ARM Linux #include "dmaengine.h"
35ff7b0479SSaeed Bishara #include "mv_xor.h"
36ff7b0479SSaeed Bishara 
37ff7b0479SSaeed Bishara static void mv_xor_issue_pending(struct dma_chan *chan);
38ff7b0479SSaeed Bishara 
39ff7b0479SSaeed Bishara #define to_mv_xor_chan(chan)		\
4098817b99SThomas Petazzoni 	container_of(chan, struct mv_xor_chan, dmachan)
41ff7b0479SSaeed Bishara 
42ff7b0479SSaeed Bishara #define to_mv_xor_slot(tx)		\
43ff7b0479SSaeed Bishara 	container_of(tx, struct mv_xor_desc_slot, async_tx)
44ff7b0479SSaeed Bishara 
45c98c1781SThomas Petazzoni #define mv_chan_to_devp(chan)           \
461ef48a26SThomas Petazzoni 	((chan)->dmadev.dev)
47c98c1781SThomas Petazzoni 
48ff7b0479SSaeed Bishara static void mv_desc_init(struct mv_xor_desc_slot *desc, unsigned long flags)
49ff7b0479SSaeed Bishara {
50ff7b0479SSaeed Bishara 	struct mv_xor_desc *hw_desc = desc->hw_desc;
51ff7b0479SSaeed Bishara 
52ff7b0479SSaeed Bishara 	hw_desc->status = (1 << 31);
53ff7b0479SSaeed Bishara 	hw_desc->phy_next_desc = 0;
54ff7b0479SSaeed Bishara 	hw_desc->desc_command = (1 << 31);
55ff7b0479SSaeed Bishara }
56ff7b0479SSaeed Bishara 
57ff7b0479SSaeed Bishara static void mv_desc_set_byte_count(struct mv_xor_desc_slot *desc,
58ff7b0479SSaeed Bishara 				   u32 byte_count)
59ff7b0479SSaeed Bishara {
60ff7b0479SSaeed Bishara 	struct mv_xor_desc *hw_desc = desc->hw_desc;
61ff7b0479SSaeed Bishara 	hw_desc->byte_count = byte_count;
62ff7b0479SSaeed Bishara }
63ff7b0479SSaeed Bishara 
64ff7b0479SSaeed Bishara static void mv_desc_set_next_desc(struct mv_xor_desc_slot *desc,
65ff7b0479SSaeed Bishara 				  u32 next_desc_addr)
66ff7b0479SSaeed Bishara {
67ff7b0479SSaeed Bishara 	struct mv_xor_desc *hw_desc = desc->hw_desc;
68ff7b0479SSaeed Bishara 	BUG_ON(hw_desc->phy_next_desc);
69ff7b0479SSaeed Bishara 	hw_desc->phy_next_desc = next_desc_addr;
70ff7b0479SSaeed Bishara }
71ff7b0479SSaeed Bishara 
72ff7b0479SSaeed Bishara static void mv_desc_clear_next_desc(struct mv_xor_desc_slot *desc)
73ff7b0479SSaeed Bishara {
74ff7b0479SSaeed Bishara 	struct mv_xor_desc *hw_desc = desc->hw_desc;
75ff7b0479SSaeed Bishara 	hw_desc->phy_next_desc = 0;
76ff7b0479SSaeed Bishara }
77ff7b0479SSaeed Bishara 
78ff7b0479SSaeed Bishara static void mv_desc_set_dest_addr(struct mv_xor_desc_slot *desc,
79ff7b0479SSaeed Bishara 				  dma_addr_t addr)
80ff7b0479SSaeed Bishara {
81ff7b0479SSaeed Bishara 	struct mv_xor_desc *hw_desc = desc->hw_desc;
82ff7b0479SSaeed Bishara 	hw_desc->phy_dest_addr = addr;
83ff7b0479SSaeed Bishara }
84ff7b0479SSaeed Bishara 
85ff7b0479SSaeed Bishara static int mv_chan_memset_slot_count(size_t len)
86ff7b0479SSaeed Bishara {
87ff7b0479SSaeed Bishara 	return 1;
88ff7b0479SSaeed Bishara }
89ff7b0479SSaeed Bishara 
90ff7b0479SSaeed Bishara #define mv_chan_memcpy_slot_count(c) mv_chan_memset_slot_count(c)
91ff7b0479SSaeed Bishara 
92ff7b0479SSaeed Bishara static void mv_desc_set_src_addr(struct mv_xor_desc_slot *desc,
93ff7b0479SSaeed Bishara 				 int index, dma_addr_t addr)
94ff7b0479SSaeed Bishara {
95ff7b0479SSaeed Bishara 	struct mv_xor_desc *hw_desc = desc->hw_desc;
96e03bc654SThomas Petazzoni 	hw_desc->phy_src_addr[mv_phy_src_idx(index)] = addr;
97ff7b0479SSaeed Bishara 	if (desc->type == DMA_XOR)
98ff7b0479SSaeed Bishara 		hw_desc->desc_command |= (1 << index);
99ff7b0479SSaeed Bishara }
100ff7b0479SSaeed Bishara 
101ff7b0479SSaeed Bishara static u32 mv_chan_get_current_desc(struct mv_xor_chan *chan)
102ff7b0479SSaeed Bishara {
1035733c38aSThomas Petazzoni 	return readl_relaxed(XOR_CURR_DESC(chan));
104ff7b0479SSaeed Bishara }
105ff7b0479SSaeed Bishara 
106ff7b0479SSaeed Bishara static void mv_chan_set_next_descriptor(struct mv_xor_chan *chan,
107ff7b0479SSaeed Bishara 					u32 next_desc_addr)
108ff7b0479SSaeed Bishara {
1095733c38aSThomas Petazzoni 	writel_relaxed(next_desc_addr, XOR_NEXT_DESC(chan));
110ff7b0479SSaeed Bishara }
111ff7b0479SSaeed Bishara 
112ff7b0479SSaeed Bishara static void mv_chan_unmask_interrupts(struct mv_xor_chan *chan)
113ff7b0479SSaeed Bishara {
1145733c38aSThomas Petazzoni 	u32 val = readl_relaxed(XOR_INTR_MASK(chan));
115ff7b0479SSaeed Bishara 	val |= XOR_INTR_MASK_VALUE << (chan->idx * 16);
1165733c38aSThomas Petazzoni 	writel_relaxed(val, XOR_INTR_MASK(chan));
117ff7b0479SSaeed Bishara }
118ff7b0479SSaeed Bishara 
119ff7b0479SSaeed Bishara static u32 mv_chan_get_intr_cause(struct mv_xor_chan *chan)
120ff7b0479SSaeed Bishara {
1215733c38aSThomas Petazzoni 	u32 intr_cause = readl_relaxed(XOR_INTR_CAUSE(chan));
122ff7b0479SSaeed Bishara 	intr_cause = (intr_cause >> (chan->idx * 16)) & 0xFFFF;
123ff7b0479SSaeed Bishara 	return intr_cause;
124ff7b0479SSaeed Bishara }
125ff7b0479SSaeed Bishara 
126ff7b0479SSaeed Bishara static int mv_is_err_intr(u32 intr_cause)
127ff7b0479SSaeed Bishara {
128ff7b0479SSaeed Bishara 	if (intr_cause & ((1<<4)|(1<<5)|(1<<6)|(1<<7)|(1<<8)|(1<<9)))
129ff7b0479SSaeed Bishara 		return 1;
130ff7b0479SSaeed Bishara 
131ff7b0479SSaeed Bishara 	return 0;
132ff7b0479SSaeed Bishara }
133ff7b0479SSaeed Bishara 
134ff7b0479SSaeed Bishara static void mv_xor_device_clear_eoc_cause(struct mv_xor_chan *chan)
135ff7b0479SSaeed Bishara {
13686363682SSimon Guinot 	u32 val = ~(1 << (chan->idx * 16));
137c98c1781SThomas Petazzoni 	dev_dbg(mv_chan_to_devp(chan), "%s, val 0x%08x\n", __func__, val);
1385733c38aSThomas Petazzoni 	writel_relaxed(val, XOR_INTR_CAUSE(chan));
139ff7b0479SSaeed Bishara }
140ff7b0479SSaeed Bishara 
141ff7b0479SSaeed Bishara static void mv_xor_device_clear_err_status(struct mv_xor_chan *chan)
142ff7b0479SSaeed Bishara {
143ff7b0479SSaeed Bishara 	u32 val = 0xFFFF0000 >> (chan->idx * 16);
1445733c38aSThomas Petazzoni 	writel_relaxed(val, XOR_INTR_CAUSE(chan));
145ff7b0479SSaeed Bishara }
146ff7b0479SSaeed Bishara 
147ff7b0479SSaeed Bishara static int mv_can_chain(struct mv_xor_desc_slot *desc)
148ff7b0479SSaeed Bishara {
149ff7b0479SSaeed Bishara 	struct mv_xor_desc_slot *chain_old_tail = list_entry(
150ff7b0479SSaeed Bishara 		desc->chain_node.prev, struct mv_xor_desc_slot, chain_node);
151ff7b0479SSaeed Bishara 
152ff7b0479SSaeed Bishara 	if (chain_old_tail->type != desc->type)
153ff7b0479SSaeed Bishara 		return 0;
154ff7b0479SSaeed Bishara 
155ff7b0479SSaeed Bishara 	return 1;
156ff7b0479SSaeed Bishara }
157ff7b0479SSaeed Bishara 
158ff7b0479SSaeed Bishara static void mv_set_mode(struct mv_xor_chan *chan,
159ff7b0479SSaeed Bishara 			       enum dma_transaction_type type)
160ff7b0479SSaeed Bishara {
161ff7b0479SSaeed Bishara 	u32 op_mode;
1625733c38aSThomas Petazzoni 	u32 config = readl_relaxed(XOR_CONFIG(chan));
163ff7b0479SSaeed Bishara 
164ff7b0479SSaeed Bishara 	switch (type) {
165ff7b0479SSaeed Bishara 	case DMA_XOR:
166ff7b0479SSaeed Bishara 		op_mode = XOR_OPERATION_MODE_XOR;
167ff7b0479SSaeed Bishara 		break;
168ff7b0479SSaeed Bishara 	case DMA_MEMCPY:
169ff7b0479SSaeed Bishara 		op_mode = XOR_OPERATION_MODE_MEMCPY;
170ff7b0479SSaeed Bishara 		break;
171ff7b0479SSaeed Bishara 	default:
172c98c1781SThomas Petazzoni 		dev_err(mv_chan_to_devp(chan),
1731ba151cdSJoe Perches 			"error: unsupported operation %d\n",
174ff7b0479SSaeed Bishara 			type);
175ff7b0479SSaeed Bishara 		BUG();
176ff7b0479SSaeed Bishara 		return;
177ff7b0479SSaeed Bishara 	}
178ff7b0479SSaeed Bishara 
179ff7b0479SSaeed Bishara 	config &= ~0x7;
180ff7b0479SSaeed Bishara 	config |= op_mode;
181e03bc654SThomas Petazzoni 
182e03bc654SThomas Petazzoni #if defined(__BIG_ENDIAN)
183e03bc654SThomas Petazzoni 	config |= XOR_DESCRIPTOR_SWAP;
184e03bc654SThomas Petazzoni #else
185e03bc654SThomas Petazzoni 	config &= ~XOR_DESCRIPTOR_SWAP;
186e03bc654SThomas Petazzoni #endif
187e03bc654SThomas Petazzoni 
1885733c38aSThomas Petazzoni 	writel_relaxed(config, XOR_CONFIG(chan));
189ff7b0479SSaeed Bishara 	chan->current_type = type;
190ff7b0479SSaeed Bishara }
191ff7b0479SSaeed Bishara 
192ff7b0479SSaeed Bishara static void mv_chan_activate(struct mv_xor_chan *chan)
193ff7b0479SSaeed Bishara {
194ff7b0479SSaeed Bishara 	u32 activation;
195ff7b0479SSaeed Bishara 
196c98c1781SThomas Petazzoni 	dev_dbg(mv_chan_to_devp(chan), " activate chan.\n");
1975733c38aSThomas Petazzoni 	activation = readl_relaxed(XOR_ACTIVATION(chan));
198ff7b0479SSaeed Bishara 	activation |= 0x1;
1995733c38aSThomas Petazzoni 	writel_relaxed(activation, XOR_ACTIVATION(chan));
200ff7b0479SSaeed Bishara }
201ff7b0479SSaeed Bishara 
202ff7b0479SSaeed Bishara static char mv_chan_is_busy(struct mv_xor_chan *chan)
203ff7b0479SSaeed Bishara {
2045733c38aSThomas Petazzoni 	u32 state = readl_relaxed(XOR_ACTIVATION(chan));
205ff7b0479SSaeed Bishara 
206ff7b0479SSaeed Bishara 	state = (state >> 4) & 0x3;
207ff7b0479SSaeed Bishara 
208ff7b0479SSaeed Bishara 	return (state == 1) ? 1 : 0;
209ff7b0479SSaeed Bishara }
210ff7b0479SSaeed Bishara 
211ff7b0479SSaeed Bishara static int mv_chan_xor_slot_count(size_t len, int src_cnt)
212ff7b0479SSaeed Bishara {
213ff7b0479SSaeed Bishara 	return 1;
214ff7b0479SSaeed Bishara }
215ff7b0479SSaeed Bishara 
216ff7b0479SSaeed Bishara /**
217ff7b0479SSaeed Bishara  * mv_xor_free_slots - flags descriptor slots for reuse
218ff7b0479SSaeed Bishara  * @slot: Slot to free
219ff7b0479SSaeed Bishara  * Caller must hold &mv_chan->lock while calling this function
220ff7b0479SSaeed Bishara  */
221ff7b0479SSaeed Bishara static void mv_xor_free_slots(struct mv_xor_chan *mv_chan,
222ff7b0479SSaeed Bishara 			      struct mv_xor_desc_slot *slot)
223ff7b0479SSaeed Bishara {
224c98c1781SThomas Petazzoni 	dev_dbg(mv_chan_to_devp(mv_chan), "%s %d slot %p\n",
225ff7b0479SSaeed Bishara 		__func__, __LINE__, slot);
226ff7b0479SSaeed Bishara 
227ff7b0479SSaeed Bishara 	slot->slots_per_op = 0;
228ff7b0479SSaeed Bishara 
229ff7b0479SSaeed Bishara }
230ff7b0479SSaeed Bishara 
231ff7b0479SSaeed Bishara /*
232ff7b0479SSaeed Bishara  * mv_xor_start_new_chain - program the engine to operate on new chain headed by
233ff7b0479SSaeed Bishara  * sw_desc
234ff7b0479SSaeed Bishara  * Caller must hold &mv_chan->lock while calling this function
235ff7b0479SSaeed Bishara  */
236ff7b0479SSaeed Bishara static void mv_xor_start_new_chain(struct mv_xor_chan *mv_chan,
237ff7b0479SSaeed Bishara 				   struct mv_xor_desc_slot *sw_desc)
238ff7b0479SSaeed Bishara {
239c98c1781SThomas Petazzoni 	dev_dbg(mv_chan_to_devp(mv_chan), "%s %d: sw_desc %p\n",
240ff7b0479SSaeed Bishara 		__func__, __LINE__, sw_desc);
241ff7b0479SSaeed Bishara 	if (sw_desc->type != mv_chan->current_type)
242ff7b0479SSaeed Bishara 		mv_set_mode(mv_chan, sw_desc->type);
243ff7b0479SSaeed Bishara 
244ff7b0479SSaeed Bishara 	/* set the hardware chain */
245ff7b0479SSaeed Bishara 	mv_chan_set_next_descriptor(mv_chan, sw_desc->async_tx.phys);
24648a9db46SBartlomiej Zolnierkiewicz 
247ff7b0479SSaeed Bishara 	mv_chan->pending += sw_desc->slot_cnt;
24898817b99SThomas Petazzoni 	mv_xor_issue_pending(&mv_chan->dmachan);
249ff7b0479SSaeed Bishara }
250ff7b0479SSaeed Bishara 
251ff7b0479SSaeed Bishara static dma_cookie_t
252ff7b0479SSaeed Bishara mv_xor_run_tx_complete_actions(struct mv_xor_desc_slot *desc,
253ff7b0479SSaeed Bishara 	struct mv_xor_chan *mv_chan, dma_cookie_t cookie)
254ff7b0479SSaeed Bishara {
255ff7b0479SSaeed Bishara 	BUG_ON(desc->async_tx.cookie < 0);
256ff7b0479SSaeed Bishara 
257ff7b0479SSaeed Bishara 	if (desc->async_tx.cookie > 0) {
258ff7b0479SSaeed Bishara 		cookie = desc->async_tx.cookie;
259ff7b0479SSaeed Bishara 
260ff7b0479SSaeed Bishara 		/* call the callback (must not sleep or submit new
261ff7b0479SSaeed Bishara 		 * operations to this channel)
262ff7b0479SSaeed Bishara 		 */
263ff7b0479SSaeed Bishara 		if (desc->async_tx.callback)
264ff7b0479SSaeed Bishara 			desc->async_tx.callback(
265ff7b0479SSaeed Bishara 				desc->async_tx.callback_param);
266ff7b0479SSaeed Bishara 
267d38a8c62SDan Williams 		dma_descriptor_unmap(&desc->async_tx);
26854f8d501SBartlomiej Zolnierkiewicz 		if (desc->group_head)
269ff7b0479SSaeed Bishara 			desc->group_head = NULL;
270ff7b0479SSaeed Bishara 	}
271ff7b0479SSaeed Bishara 
272ff7b0479SSaeed Bishara 	/* run dependent operations */
27307f2211eSDan Williams 	dma_run_dependencies(&desc->async_tx);
274ff7b0479SSaeed Bishara 
275ff7b0479SSaeed Bishara 	return cookie;
276ff7b0479SSaeed Bishara }
277ff7b0479SSaeed Bishara 
278ff7b0479SSaeed Bishara static int
279ff7b0479SSaeed Bishara mv_xor_clean_completed_slots(struct mv_xor_chan *mv_chan)
280ff7b0479SSaeed Bishara {
281ff7b0479SSaeed Bishara 	struct mv_xor_desc_slot *iter, *_iter;
282ff7b0479SSaeed Bishara 
283c98c1781SThomas Petazzoni 	dev_dbg(mv_chan_to_devp(mv_chan), "%s %d\n", __func__, __LINE__);
284ff7b0479SSaeed Bishara 	list_for_each_entry_safe(iter, _iter, &mv_chan->completed_slots,
285ff7b0479SSaeed Bishara 				 completed_node) {
286ff7b0479SSaeed Bishara 
287ff7b0479SSaeed Bishara 		if (async_tx_test_ack(&iter->async_tx)) {
288ff7b0479SSaeed Bishara 			list_del(&iter->completed_node);
289ff7b0479SSaeed Bishara 			mv_xor_free_slots(mv_chan, iter);
290ff7b0479SSaeed Bishara 		}
291ff7b0479SSaeed Bishara 	}
292ff7b0479SSaeed Bishara 	return 0;
293ff7b0479SSaeed Bishara }
294ff7b0479SSaeed Bishara 
295ff7b0479SSaeed Bishara static int
296ff7b0479SSaeed Bishara mv_xor_clean_slot(struct mv_xor_desc_slot *desc,
297ff7b0479SSaeed Bishara 	struct mv_xor_chan *mv_chan)
298ff7b0479SSaeed Bishara {
299c98c1781SThomas Petazzoni 	dev_dbg(mv_chan_to_devp(mv_chan), "%s %d: desc %p flags %d\n",
300ff7b0479SSaeed Bishara 		__func__, __LINE__, desc, desc->async_tx.flags);
301ff7b0479SSaeed Bishara 	list_del(&desc->chain_node);
302ff7b0479SSaeed Bishara 	/* the client is allowed to attach dependent operations
303ff7b0479SSaeed Bishara 	 * until 'ack' is set
304ff7b0479SSaeed Bishara 	 */
305ff7b0479SSaeed Bishara 	if (!async_tx_test_ack(&desc->async_tx)) {
306ff7b0479SSaeed Bishara 		/* move this slot to the completed_slots */
307ff7b0479SSaeed Bishara 		list_add_tail(&desc->completed_node, &mv_chan->completed_slots);
308ff7b0479SSaeed Bishara 		return 0;
309ff7b0479SSaeed Bishara 	}
310ff7b0479SSaeed Bishara 
311ff7b0479SSaeed Bishara 	mv_xor_free_slots(mv_chan, desc);
312ff7b0479SSaeed Bishara 	return 0;
313ff7b0479SSaeed Bishara }
314ff7b0479SSaeed Bishara 
315ff7b0479SSaeed Bishara static void __mv_xor_slot_cleanup(struct mv_xor_chan *mv_chan)
316ff7b0479SSaeed Bishara {
317ff7b0479SSaeed Bishara 	struct mv_xor_desc_slot *iter, *_iter;
318ff7b0479SSaeed Bishara 	dma_cookie_t cookie = 0;
319ff7b0479SSaeed Bishara 	int busy = mv_chan_is_busy(mv_chan);
320ff7b0479SSaeed Bishara 	u32 current_desc = mv_chan_get_current_desc(mv_chan);
321ff7b0479SSaeed Bishara 	int seen_current = 0;
322ff7b0479SSaeed Bishara 
323c98c1781SThomas Petazzoni 	dev_dbg(mv_chan_to_devp(mv_chan), "%s %d\n", __func__, __LINE__);
324c98c1781SThomas Petazzoni 	dev_dbg(mv_chan_to_devp(mv_chan), "current_desc %x\n", current_desc);
325ff7b0479SSaeed Bishara 	mv_xor_clean_completed_slots(mv_chan);
326ff7b0479SSaeed Bishara 
327ff7b0479SSaeed Bishara 	/* free completed slots from the chain starting with
328ff7b0479SSaeed Bishara 	 * the oldest descriptor
329ff7b0479SSaeed Bishara 	 */
330ff7b0479SSaeed Bishara 
331ff7b0479SSaeed Bishara 	list_for_each_entry_safe(iter, _iter, &mv_chan->chain,
332ff7b0479SSaeed Bishara 					chain_node) {
333ff7b0479SSaeed Bishara 		prefetch(_iter);
334ff7b0479SSaeed Bishara 		prefetch(&_iter->async_tx);
335ff7b0479SSaeed Bishara 
336ff7b0479SSaeed Bishara 		/* do not advance past the current descriptor loaded into the
337ff7b0479SSaeed Bishara 		 * hardware channel, subsequent descriptors are either in
338ff7b0479SSaeed Bishara 		 * process or have not been submitted
339ff7b0479SSaeed Bishara 		 */
340ff7b0479SSaeed Bishara 		if (seen_current)
341ff7b0479SSaeed Bishara 			break;
342ff7b0479SSaeed Bishara 
343ff7b0479SSaeed Bishara 		/* stop the search if we reach the current descriptor and the
344ff7b0479SSaeed Bishara 		 * channel is busy
345ff7b0479SSaeed Bishara 		 */
346ff7b0479SSaeed Bishara 		if (iter->async_tx.phys == current_desc) {
347ff7b0479SSaeed Bishara 			seen_current = 1;
348ff7b0479SSaeed Bishara 			if (busy)
349ff7b0479SSaeed Bishara 				break;
350ff7b0479SSaeed Bishara 		}
351ff7b0479SSaeed Bishara 
352ff7b0479SSaeed Bishara 		cookie = mv_xor_run_tx_complete_actions(iter, mv_chan, cookie);
353ff7b0479SSaeed Bishara 
354ff7b0479SSaeed Bishara 		if (mv_xor_clean_slot(iter, mv_chan))
355ff7b0479SSaeed Bishara 			break;
356ff7b0479SSaeed Bishara 	}
357ff7b0479SSaeed Bishara 
358ff7b0479SSaeed Bishara 	if ((busy == 0) && !list_empty(&mv_chan->chain)) {
359ff7b0479SSaeed Bishara 		struct mv_xor_desc_slot *chain_head;
360ff7b0479SSaeed Bishara 		chain_head = list_entry(mv_chan->chain.next,
361ff7b0479SSaeed Bishara 					struct mv_xor_desc_slot,
362ff7b0479SSaeed Bishara 					chain_node);
363ff7b0479SSaeed Bishara 
364ff7b0479SSaeed Bishara 		mv_xor_start_new_chain(mv_chan, chain_head);
365ff7b0479SSaeed Bishara 	}
366ff7b0479SSaeed Bishara 
367ff7b0479SSaeed Bishara 	if (cookie > 0)
36898817b99SThomas Petazzoni 		mv_chan->dmachan.completed_cookie = cookie;
369ff7b0479SSaeed Bishara }
370ff7b0479SSaeed Bishara 
371ff7b0479SSaeed Bishara static void
372ff7b0479SSaeed Bishara mv_xor_slot_cleanup(struct mv_xor_chan *mv_chan)
373ff7b0479SSaeed Bishara {
374ff7b0479SSaeed Bishara 	spin_lock_bh(&mv_chan->lock);
375ff7b0479SSaeed Bishara 	__mv_xor_slot_cleanup(mv_chan);
376ff7b0479SSaeed Bishara 	spin_unlock_bh(&mv_chan->lock);
377ff7b0479SSaeed Bishara }
378ff7b0479SSaeed Bishara 
379ff7b0479SSaeed Bishara static void mv_xor_tasklet(unsigned long data)
380ff7b0479SSaeed Bishara {
381ff7b0479SSaeed Bishara 	struct mv_xor_chan *chan = (struct mv_xor_chan *) data;
382*e43147acSEzequiel Garcia 
383*e43147acSEzequiel Garcia 	spin_lock_bh(&chan->lock);
384*e43147acSEzequiel Garcia 	__mv_xor_slot_cleanup(chan);
385*e43147acSEzequiel Garcia 	spin_unlock_bh(&chan->lock);
386ff7b0479SSaeed Bishara }
387ff7b0479SSaeed Bishara 
388ff7b0479SSaeed Bishara static struct mv_xor_desc_slot *
389ff7b0479SSaeed Bishara mv_xor_alloc_slots(struct mv_xor_chan *mv_chan, int num_slots,
390ff7b0479SSaeed Bishara 		    int slots_per_op)
391ff7b0479SSaeed Bishara {
392ff7b0479SSaeed Bishara 	struct mv_xor_desc_slot *iter, *_iter, *alloc_start = NULL;
393ff7b0479SSaeed Bishara 	LIST_HEAD(chain);
394ff7b0479SSaeed Bishara 	int slots_found, retry = 0;
395ff7b0479SSaeed Bishara 
396ff7b0479SSaeed Bishara 	/* start search from the last allocated descrtiptor
397ff7b0479SSaeed Bishara 	 * if a contiguous allocation can not be found start searching
398ff7b0479SSaeed Bishara 	 * from the beginning of the list
399ff7b0479SSaeed Bishara 	 */
400ff7b0479SSaeed Bishara retry:
401ff7b0479SSaeed Bishara 	slots_found = 0;
402ff7b0479SSaeed Bishara 	if (retry == 0)
403ff7b0479SSaeed Bishara 		iter = mv_chan->last_used;
404ff7b0479SSaeed Bishara 	else
405ff7b0479SSaeed Bishara 		iter = list_entry(&mv_chan->all_slots,
406ff7b0479SSaeed Bishara 			struct mv_xor_desc_slot,
407ff7b0479SSaeed Bishara 			slot_node);
408ff7b0479SSaeed Bishara 
409ff7b0479SSaeed Bishara 	list_for_each_entry_safe_continue(
410ff7b0479SSaeed Bishara 		iter, _iter, &mv_chan->all_slots, slot_node) {
411ff7b0479SSaeed Bishara 		prefetch(_iter);
412ff7b0479SSaeed Bishara 		prefetch(&_iter->async_tx);
413ff7b0479SSaeed Bishara 		if (iter->slots_per_op) {
414ff7b0479SSaeed Bishara 			/* give up after finding the first busy slot
415ff7b0479SSaeed Bishara 			 * on the second pass through the list
416ff7b0479SSaeed Bishara 			 */
417ff7b0479SSaeed Bishara 			if (retry)
418ff7b0479SSaeed Bishara 				break;
419ff7b0479SSaeed Bishara 
420ff7b0479SSaeed Bishara 			slots_found = 0;
421ff7b0479SSaeed Bishara 			continue;
422ff7b0479SSaeed Bishara 		}
423ff7b0479SSaeed Bishara 
424ff7b0479SSaeed Bishara 		/* start the allocation if the slot is correctly aligned */
425ff7b0479SSaeed Bishara 		if (!slots_found++)
426ff7b0479SSaeed Bishara 			alloc_start = iter;
427ff7b0479SSaeed Bishara 
428ff7b0479SSaeed Bishara 		if (slots_found == num_slots) {
429ff7b0479SSaeed Bishara 			struct mv_xor_desc_slot *alloc_tail = NULL;
430ff7b0479SSaeed Bishara 			struct mv_xor_desc_slot *last_used = NULL;
431ff7b0479SSaeed Bishara 			iter = alloc_start;
432ff7b0479SSaeed Bishara 			while (num_slots) {
433ff7b0479SSaeed Bishara 				int i;
434ff7b0479SSaeed Bishara 
435ff7b0479SSaeed Bishara 				/* pre-ack all but the last descriptor */
436ff7b0479SSaeed Bishara 				async_tx_ack(&iter->async_tx);
437ff7b0479SSaeed Bishara 
438ff7b0479SSaeed Bishara 				list_add_tail(&iter->chain_node, &chain);
439ff7b0479SSaeed Bishara 				alloc_tail = iter;
440ff7b0479SSaeed Bishara 				iter->async_tx.cookie = 0;
441ff7b0479SSaeed Bishara 				iter->slot_cnt = num_slots;
442ff7b0479SSaeed Bishara 				iter->xor_check_result = NULL;
443ff7b0479SSaeed Bishara 				for (i = 0; i < slots_per_op; i++) {
444ff7b0479SSaeed Bishara 					iter->slots_per_op = slots_per_op - i;
445ff7b0479SSaeed Bishara 					last_used = iter;
446ff7b0479SSaeed Bishara 					iter = list_entry(iter->slot_node.next,
447ff7b0479SSaeed Bishara 						struct mv_xor_desc_slot,
448ff7b0479SSaeed Bishara 						slot_node);
449ff7b0479SSaeed Bishara 				}
450ff7b0479SSaeed Bishara 				num_slots -= slots_per_op;
451ff7b0479SSaeed Bishara 			}
452ff7b0479SSaeed Bishara 			alloc_tail->group_head = alloc_start;
453ff7b0479SSaeed Bishara 			alloc_tail->async_tx.cookie = -EBUSY;
45464203b67SDan Williams 			list_splice(&chain, &alloc_tail->tx_list);
455ff7b0479SSaeed Bishara 			mv_chan->last_used = last_used;
456ff7b0479SSaeed Bishara 			mv_desc_clear_next_desc(alloc_start);
457ff7b0479SSaeed Bishara 			mv_desc_clear_next_desc(alloc_tail);
458ff7b0479SSaeed Bishara 			return alloc_tail;
459ff7b0479SSaeed Bishara 		}
460ff7b0479SSaeed Bishara 	}
461ff7b0479SSaeed Bishara 	if (!retry++)
462ff7b0479SSaeed Bishara 		goto retry;
463ff7b0479SSaeed Bishara 
464ff7b0479SSaeed Bishara 	/* try to free some slots if the allocation fails */
465ff7b0479SSaeed Bishara 	tasklet_schedule(&mv_chan->irq_tasklet);
466ff7b0479SSaeed Bishara 
467ff7b0479SSaeed Bishara 	return NULL;
468ff7b0479SSaeed Bishara }
469ff7b0479SSaeed Bishara 
470ff7b0479SSaeed Bishara /************************ DMA engine API functions ****************************/
471ff7b0479SSaeed Bishara static dma_cookie_t
472ff7b0479SSaeed Bishara mv_xor_tx_submit(struct dma_async_tx_descriptor *tx)
473ff7b0479SSaeed Bishara {
474ff7b0479SSaeed Bishara 	struct mv_xor_desc_slot *sw_desc = to_mv_xor_slot(tx);
475ff7b0479SSaeed Bishara 	struct mv_xor_chan *mv_chan = to_mv_xor_chan(tx->chan);
476ff7b0479SSaeed Bishara 	struct mv_xor_desc_slot *grp_start, *old_chain_tail;
477ff7b0479SSaeed Bishara 	dma_cookie_t cookie;
478ff7b0479SSaeed Bishara 	int new_hw_chain = 1;
479ff7b0479SSaeed Bishara 
480c98c1781SThomas Petazzoni 	dev_dbg(mv_chan_to_devp(mv_chan),
481ff7b0479SSaeed Bishara 		"%s sw_desc %p: async_tx %p\n",
482ff7b0479SSaeed Bishara 		__func__, sw_desc, &sw_desc->async_tx);
483ff7b0479SSaeed Bishara 
484ff7b0479SSaeed Bishara 	grp_start = sw_desc->group_head;
485ff7b0479SSaeed Bishara 
486ff7b0479SSaeed Bishara 	spin_lock_bh(&mv_chan->lock);
487884485e1SRussell King - ARM Linux 	cookie = dma_cookie_assign(tx);
488ff7b0479SSaeed Bishara 
489ff7b0479SSaeed Bishara 	if (list_empty(&mv_chan->chain))
49064203b67SDan Williams 		list_splice_init(&sw_desc->tx_list, &mv_chan->chain);
491ff7b0479SSaeed Bishara 	else {
492ff7b0479SSaeed Bishara 		new_hw_chain = 0;
493ff7b0479SSaeed Bishara 
494ff7b0479SSaeed Bishara 		old_chain_tail = list_entry(mv_chan->chain.prev,
495ff7b0479SSaeed Bishara 					    struct mv_xor_desc_slot,
496ff7b0479SSaeed Bishara 					    chain_node);
49764203b67SDan Williams 		list_splice_init(&grp_start->tx_list,
498ff7b0479SSaeed Bishara 				 &old_chain_tail->chain_node);
499ff7b0479SSaeed Bishara 
500ff7b0479SSaeed Bishara 		if (!mv_can_chain(grp_start))
501ff7b0479SSaeed Bishara 			goto submit_done;
502ff7b0479SSaeed Bishara 
50331fd8f5bSOlof Johansson 		dev_dbg(mv_chan_to_devp(mv_chan), "Append to last desc %pa\n",
50431fd8f5bSOlof Johansson 			&old_chain_tail->async_tx.phys);
505ff7b0479SSaeed Bishara 
506ff7b0479SSaeed Bishara 		/* fix up the hardware chain */
507ff7b0479SSaeed Bishara 		mv_desc_set_next_desc(old_chain_tail, grp_start->async_tx.phys);
508ff7b0479SSaeed Bishara 
509ff7b0479SSaeed Bishara 		/* if the channel is not busy */
510ff7b0479SSaeed Bishara 		if (!mv_chan_is_busy(mv_chan)) {
511ff7b0479SSaeed Bishara 			u32 current_desc = mv_chan_get_current_desc(mv_chan);
512ff7b0479SSaeed Bishara 			/*
513ff7b0479SSaeed Bishara 			 * and the curren desc is the end of the chain before
514ff7b0479SSaeed Bishara 			 * the append, then we need to start the channel
515ff7b0479SSaeed Bishara 			 */
516ff7b0479SSaeed Bishara 			if (current_desc == old_chain_tail->async_tx.phys)
517ff7b0479SSaeed Bishara 				new_hw_chain = 1;
518ff7b0479SSaeed Bishara 		}
519ff7b0479SSaeed Bishara 	}
520ff7b0479SSaeed Bishara 
521ff7b0479SSaeed Bishara 	if (new_hw_chain)
522ff7b0479SSaeed Bishara 		mv_xor_start_new_chain(mv_chan, grp_start);
523ff7b0479SSaeed Bishara 
524ff7b0479SSaeed Bishara submit_done:
525ff7b0479SSaeed Bishara 	spin_unlock_bh(&mv_chan->lock);
526ff7b0479SSaeed Bishara 
527ff7b0479SSaeed Bishara 	return cookie;
528ff7b0479SSaeed Bishara }
529ff7b0479SSaeed Bishara 
530ff7b0479SSaeed Bishara /* returns the number of allocated descriptors */
531aa1e6f1aSDan Williams static int mv_xor_alloc_chan_resources(struct dma_chan *chan)
532ff7b0479SSaeed Bishara {
53331fd8f5bSOlof Johansson 	void *virt_desc;
53431fd8f5bSOlof Johansson 	dma_addr_t dma_desc;
535ff7b0479SSaeed Bishara 	int idx;
536ff7b0479SSaeed Bishara 	struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
537ff7b0479SSaeed Bishara 	struct mv_xor_desc_slot *slot = NULL;
538b503fa01SThomas Petazzoni 	int num_descs_in_pool = MV_XOR_POOL_SIZE/MV_XOR_SLOT_SIZE;
539ff7b0479SSaeed Bishara 
540ff7b0479SSaeed Bishara 	/* Allocate descriptor slots */
541ff7b0479SSaeed Bishara 	idx = mv_chan->slots_allocated;
542ff7b0479SSaeed Bishara 	while (idx < num_descs_in_pool) {
543ff7b0479SSaeed Bishara 		slot = kzalloc(sizeof(*slot), GFP_KERNEL);
544ff7b0479SSaeed Bishara 		if (!slot) {
545ff7b0479SSaeed Bishara 			printk(KERN_INFO "MV XOR Channel only initialized"
546ff7b0479SSaeed Bishara 				" %d descriptor slots", idx);
547ff7b0479SSaeed Bishara 			break;
548ff7b0479SSaeed Bishara 		}
54931fd8f5bSOlof Johansson 		virt_desc = mv_chan->dma_desc_pool_virt;
55031fd8f5bSOlof Johansson 		slot->hw_desc = virt_desc + idx * MV_XOR_SLOT_SIZE;
551ff7b0479SSaeed Bishara 
552ff7b0479SSaeed Bishara 		dma_async_tx_descriptor_init(&slot->async_tx, chan);
553ff7b0479SSaeed Bishara 		slot->async_tx.tx_submit = mv_xor_tx_submit;
554ff7b0479SSaeed Bishara 		INIT_LIST_HEAD(&slot->chain_node);
555ff7b0479SSaeed Bishara 		INIT_LIST_HEAD(&slot->slot_node);
55664203b67SDan Williams 		INIT_LIST_HEAD(&slot->tx_list);
55731fd8f5bSOlof Johansson 		dma_desc = mv_chan->dma_desc_pool;
55831fd8f5bSOlof Johansson 		slot->async_tx.phys = dma_desc + idx * MV_XOR_SLOT_SIZE;
559ff7b0479SSaeed Bishara 		slot->idx = idx++;
560ff7b0479SSaeed Bishara 
561ff7b0479SSaeed Bishara 		spin_lock_bh(&mv_chan->lock);
562ff7b0479SSaeed Bishara 		mv_chan->slots_allocated = idx;
563ff7b0479SSaeed Bishara 		list_add_tail(&slot->slot_node, &mv_chan->all_slots);
564ff7b0479SSaeed Bishara 		spin_unlock_bh(&mv_chan->lock);
565ff7b0479SSaeed Bishara 	}
566ff7b0479SSaeed Bishara 
567ff7b0479SSaeed Bishara 	if (mv_chan->slots_allocated && !mv_chan->last_used)
568ff7b0479SSaeed Bishara 		mv_chan->last_used = list_entry(mv_chan->all_slots.next,
569ff7b0479SSaeed Bishara 					struct mv_xor_desc_slot,
570ff7b0479SSaeed Bishara 					slot_node);
571ff7b0479SSaeed Bishara 
572c98c1781SThomas Petazzoni 	dev_dbg(mv_chan_to_devp(mv_chan),
573ff7b0479SSaeed Bishara 		"allocated %d descriptor slots last_used: %p\n",
574ff7b0479SSaeed Bishara 		mv_chan->slots_allocated, mv_chan->last_used);
575ff7b0479SSaeed Bishara 
576ff7b0479SSaeed Bishara 	return mv_chan->slots_allocated ? : -ENOMEM;
577ff7b0479SSaeed Bishara }
578ff7b0479SSaeed Bishara 
579ff7b0479SSaeed Bishara static struct dma_async_tx_descriptor *
580ff7b0479SSaeed Bishara mv_xor_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
581ff7b0479SSaeed Bishara 		size_t len, unsigned long flags)
582ff7b0479SSaeed Bishara {
583ff7b0479SSaeed Bishara 	struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
584ff7b0479SSaeed Bishara 	struct mv_xor_desc_slot *sw_desc, *grp_start;
585ff7b0479SSaeed Bishara 	int slot_cnt;
586ff7b0479SSaeed Bishara 
587c98c1781SThomas Petazzoni 	dev_dbg(mv_chan_to_devp(mv_chan),
58831fd8f5bSOlof Johansson 		"%s dest: %pad src %pad len: %u flags: %ld\n",
58931fd8f5bSOlof Johansson 		__func__, &dest, &src, len, flags);
590ff7b0479SSaeed Bishara 	if (unlikely(len < MV_XOR_MIN_BYTE_COUNT))
591ff7b0479SSaeed Bishara 		return NULL;
592ff7b0479SSaeed Bishara 
5937912d300SColy Li 	BUG_ON(len > MV_XOR_MAX_BYTE_COUNT);
594ff7b0479SSaeed Bishara 
595ff7b0479SSaeed Bishara 	spin_lock_bh(&mv_chan->lock);
596ff7b0479SSaeed Bishara 	slot_cnt = mv_chan_memcpy_slot_count(len);
597ff7b0479SSaeed Bishara 	sw_desc = mv_xor_alloc_slots(mv_chan, slot_cnt, 1);
598ff7b0479SSaeed Bishara 	if (sw_desc) {
599ff7b0479SSaeed Bishara 		sw_desc->type = DMA_MEMCPY;
600ff7b0479SSaeed Bishara 		sw_desc->async_tx.flags = flags;
601ff7b0479SSaeed Bishara 		grp_start = sw_desc->group_head;
602ff7b0479SSaeed Bishara 		mv_desc_init(grp_start, flags);
603ff7b0479SSaeed Bishara 		mv_desc_set_byte_count(grp_start, len);
604ff7b0479SSaeed Bishara 		mv_desc_set_dest_addr(sw_desc->group_head, dest);
605ff7b0479SSaeed Bishara 		mv_desc_set_src_addr(grp_start, 0, src);
606ff7b0479SSaeed Bishara 		sw_desc->unmap_src_cnt = 1;
607ff7b0479SSaeed Bishara 		sw_desc->unmap_len = len;
608ff7b0479SSaeed Bishara 	}
609ff7b0479SSaeed Bishara 	spin_unlock_bh(&mv_chan->lock);
610ff7b0479SSaeed Bishara 
611c98c1781SThomas Petazzoni 	dev_dbg(mv_chan_to_devp(mv_chan),
612ff7b0479SSaeed Bishara 		"%s sw_desc %p async_tx %p\n",
6134c143725SJingoo Han 		__func__, sw_desc, sw_desc ? &sw_desc->async_tx : NULL);
614ff7b0479SSaeed Bishara 
615ff7b0479SSaeed Bishara 	return sw_desc ? &sw_desc->async_tx : NULL;
616ff7b0479SSaeed Bishara }
617ff7b0479SSaeed Bishara 
618ff7b0479SSaeed Bishara static struct dma_async_tx_descriptor *
619ff7b0479SSaeed Bishara mv_xor_prep_dma_xor(struct dma_chan *chan, dma_addr_t dest, dma_addr_t *src,
620ff7b0479SSaeed Bishara 		    unsigned int src_cnt, size_t len, unsigned long flags)
621ff7b0479SSaeed Bishara {
622ff7b0479SSaeed Bishara 	struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
623ff7b0479SSaeed Bishara 	struct mv_xor_desc_slot *sw_desc, *grp_start;
624ff7b0479SSaeed Bishara 	int slot_cnt;
625ff7b0479SSaeed Bishara 
626ff7b0479SSaeed Bishara 	if (unlikely(len < MV_XOR_MIN_BYTE_COUNT))
627ff7b0479SSaeed Bishara 		return NULL;
628ff7b0479SSaeed Bishara 
6297912d300SColy Li 	BUG_ON(len > MV_XOR_MAX_BYTE_COUNT);
630ff7b0479SSaeed Bishara 
631c98c1781SThomas Petazzoni 	dev_dbg(mv_chan_to_devp(mv_chan),
63231fd8f5bSOlof Johansson 		"%s src_cnt: %d len: %u dest %pad flags: %ld\n",
63331fd8f5bSOlof Johansson 		__func__, src_cnt, len, &dest, flags);
634ff7b0479SSaeed Bishara 
635ff7b0479SSaeed Bishara 	spin_lock_bh(&mv_chan->lock);
636ff7b0479SSaeed Bishara 	slot_cnt = mv_chan_xor_slot_count(len, src_cnt);
637ff7b0479SSaeed Bishara 	sw_desc = mv_xor_alloc_slots(mv_chan, slot_cnt, 1);
638ff7b0479SSaeed Bishara 	if (sw_desc) {
639ff7b0479SSaeed Bishara 		sw_desc->type = DMA_XOR;
640ff7b0479SSaeed Bishara 		sw_desc->async_tx.flags = flags;
641ff7b0479SSaeed Bishara 		grp_start = sw_desc->group_head;
642ff7b0479SSaeed Bishara 		mv_desc_init(grp_start, flags);
643ff7b0479SSaeed Bishara 		/* the byte count field is the same as in memcpy desc*/
644ff7b0479SSaeed Bishara 		mv_desc_set_byte_count(grp_start, len);
645ff7b0479SSaeed Bishara 		mv_desc_set_dest_addr(sw_desc->group_head, dest);
646ff7b0479SSaeed Bishara 		sw_desc->unmap_src_cnt = src_cnt;
647ff7b0479SSaeed Bishara 		sw_desc->unmap_len = len;
648ff7b0479SSaeed Bishara 		while (src_cnt--)
649ff7b0479SSaeed Bishara 			mv_desc_set_src_addr(grp_start, src_cnt, src[src_cnt]);
650ff7b0479SSaeed Bishara 	}
651ff7b0479SSaeed Bishara 	spin_unlock_bh(&mv_chan->lock);
652c98c1781SThomas Petazzoni 	dev_dbg(mv_chan_to_devp(mv_chan),
653ff7b0479SSaeed Bishara 		"%s sw_desc %p async_tx %p \n",
654ff7b0479SSaeed Bishara 		__func__, sw_desc, &sw_desc->async_tx);
655ff7b0479SSaeed Bishara 	return sw_desc ? &sw_desc->async_tx : NULL;
656ff7b0479SSaeed Bishara }
657ff7b0479SSaeed Bishara 
658ff7b0479SSaeed Bishara static void mv_xor_free_chan_resources(struct dma_chan *chan)
659ff7b0479SSaeed Bishara {
660ff7b0479SSaeed Bishara 	struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
661ff7b0479SSaeed Bishara 	struct mv_xor_desc_slot *iter, *_iter;
662ff7b0479SSaeed Bishara 	int in_use_descs = 0;
663ff7b0479SSaeed Bishara 
664ff7b0479SSaeed Bishara 	spin_lock_bh(&mv_chan->lock);
665*e43147acSEzequiel Garcia 
666*e43147acSEzequiel Garcia 	__mv_xor_slot_cleanup(mv_chan);
667*e43147acSEzequiel Garcia 
668ff7b0479SSaeed Bishara 	list_for_each_entry_safe(iter, _iter, &mv_chan->chain,
669ff7b0479SSaeed Bishara 					chain_node) {
670ff7b0479SSaeed Bishara 		in_use_descs++;
671ff7b0479SSaeed Bishara 		list_del(&iter->chain_node);
672ff7b0479SSaeed Bishara 	}
673ff7b0479SSaeed Bishara 	list_for_each_entry_safe(iter, _iter, &mv_chan->completed_slots,
674ff7b0479SSaeed Bishara 				 completed_node) {
675ff7b0479SSaeed Bishara 		in_use_descs++;
676ff7b0479SSaeed Bishara 		list_del(&iter->completed_node);
677ff7b0479SSaeed Bishara 	}
678ff7b0479SSaeed Bishara 	list_for_each_entry_safe_reverse(
679ff7b0479SSaeed Bishara 		iter, _iter, &mv_chan->all_slots, slot_node) {
680ff7b0479SSaeed Bishara 		list_del(&iter->slot_node);
681ff7b0479SSaeed Bishara 		kfree(iter);
682ff7b0479SSaeed Bishara 		mv_chan->slots_allocated--;
683ff7b0479SSaeed Bishara 	}
684ff7b0479SSaeed Bishara 	mv_chan->last_used = NULL;
685ff7b0479SSaeed Bishara 
686c98c1781SThomas Petazzoni 	dev_dbg(mv_chan_to_devp(mv_chan), "%s slots_allocated %d\n",
687ff7b0479SSaeed Bishara 		__func__, mv_chan->slots_allocated);
688ff7b0479SSaeed Bishara 	spin_unlock_bh(&mv_chan->lock);
689ff7b0479SSaeed Bishara 
690ff7b0479SSaeed Bishara 	if (in_use_descs)
691c98c1781SThomas Petazzoni 		dev_err(mv_chan_to_devp(mv_chan),
692ff7b0479SSaeed Bishara 			"freeing %d in use descriptors!\n", in_use_descs);
693ff7b0479SSaeed Bishara }
694ff7b0479SSaeed Bishara 
695ff7b0479SSaeed Bishara /**
69607934481SLinus Walleij  * mv_xor_status - poll the status of an XOR transaction
697ff7b0479SSaeed Bishara  * @chan: XOR channel handle
698ff7b0479SSaeed Bishara  * @cookie: XOR transaction identifier
69907934481SLinus Walleij  * @txstate: XOR transactions state holder (or NULL)
700ff7b0479SSaeed Bishara  */
70107934481SLinus Walleij static enum dma_status mv_xor_status(struct dma_chan *chan,
702ff7b0479SSaeed Bishara 					  dma_cookie_t cookie,
70307934481SLinus Walleij 					  struct dma_tx_state *txstate)
704ff7b0479SSaeed Bishara {
705ff7b0479SSaeed Bishara 	struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
706ff7b0479SSaeed Bishara 	enum dma_status ret;
707ff7b0479SSaeed Bishara 
70896a2af41SRussell King - ARM Linux 	ret = dma_cookie_status(chan, cookie, txstate);
709890766d2SEzequiel Garcia 	if (ret == DMA_COMPLETE)
710ff7b0479SSaeed Bishara 		return ret;
711*e43147acSEzequiel Garcia 
712*e43147acSEzequiel Garcia 	spin_lock_bh(&mv_chan->lock);
713*e43147acSEzequiel Garcia 	__mv_xor_slot_cleanup(mv_chan);
714*e43147acSEzequiel Garcia 	spin_unlock_bh(&mv_chan->lock);
715ff7b0479SSaeed Bishara 
71696a2af41SRussell King - ARM Linux 	return dma_cookie_status(chan, cookie, txstate);
717ff7b0479SSaeed Bishara }
718ff7b0479SSaeed Bishara 
719ff7b0479SSaeed Bishara static void mv_dump_xor_regs(struct mv_xor_chan *chan)
720ff7b0479SSaeed Bishara {
721ff7b0479SSaeed Bishara 	u32 val;
722ff7b0479SSaeed Bishara 
7235733c38aSThomas Petazzoni 	val = readl_relaxed(XOR_CONFIG(chan));
7241ba151cdSJoe Perches 	dev_err(mv_chan_to_devp(chan), "config       0x%08x\n", val);
725ff7b0479SSaeed Bishara 
7265733c38aSThomas Petazzoni 	val = readl_relaxed(XOR_ACTIVATION(chan));
7271ba151cdSJoe Perches 	dev_err(mv_chan_to_devp(chan), "activation   0x%08x\n", val);
728ff7b0479SSaeed Bishara 
7295733c38aSThomas Petazzoni 	val = readl_relaxed(XOR_INTR_CAUSE(chan));
7301ba151cdSJoe Perches 	dev_err(mv_chan_to_devp(chan), "intr cause   0x%08x\n", val);
731ff7b0479SSaeed Bishara 
7325733c38aSThomas Petazzoni 	val = readl_relaxed(XOR_INTR_MASK(chan));
7331ba151cdSJoe Perches 	dev_err(mv_chan_to_devp(chan), "intr mask    0x%08x\n", val);
734ff7b0479SSaeed Bishara 
7355733c38aSThomas Petazzoni 	val = readl_relaxed(XOR_ERROR_CAUSE(chan));
7361ba151cdSJoe Perches 	dev_err(mv_chan_to_devp(chan), "error cause  0x%08x\n", val);
737ff7b0479SSaeed Bishara 
7385733c38aSThomas Petazzoni 	val = readl_relaxed(XOR_ERROR_ADDR(chan));
7391ba151cdSJoe Perches 	dev_err(mv_chan_to_devp(chan), "error addr   0x%08x\n", val);
740ff7b0479SSaeed Bishara }
741ff7b0479SSaeed Bishara 
742ff7b0479SSaeed Bishara static void mv_xor_err_interrupt_handler(struct mv_xor_chan *chan,
743ff7b0479SSaeed Bishara 					 u32 intr_cause)
744ff7b0479SSaeed Bishara {
745ff7b0479SSaeed Bishara 	if (intr_cause & (1 << 4)) {
746c98c1781SThomas Petazzoni 	     dev_dbg(mv_chan_to_devp(chan),
747ff7b0479SSaeed Bishara 		     "ignore this error\n");
748ff7b0479SSaeed Bishara 	     return;
749ff7b0479SSaeed Bishara 	}
750ff7b0479SSaeed Bishara 
751c98c1781SThomas Petazzoni 	dev_err(mv_chan_to_devp(chan),
7521ba151cdSJoe Perches 		"error on chan %d. intr cause 0x%08x\n",
753ff7b0479SSaeed Bishara 		chan->idx, intr_cause);
754ff7b0479SSaeed Bishara 
755ff7b0479SSaeed Bishara 	mv_dump_xor_regs(chan);
756ff7b0479SSaeed Bishara 	BUG();
757ff7b0479SSaeed Bishara }
758ff7b0479SSaeed Bishara 
759ff7b0479SSaeed Bishara static irqreturn_t mv_xor_interrupt_handler(int irq, void *data)
760ff7b0479SSaeed Bishara {
761ff7b0479SSaeed Bishara 	struct mv_xor_chan *chan = data;
762ff7b0479SSaeed Bishara 	u32 intr_cause = mv_chan_get_intr_cause(chan);
763ff7b0479SSaeed Bishara 
764c98c1781SThomas Petazzoni 	dev_dbg(mv_chan_to_devp(chan), "intr cause %x\n", intr_cause);
765ff7b0479SSaeed Bishara 
766ff7b0479SSaeed Bishara 	if (mv_is_err_intr(intr_cause))
767ff7b0479SSaeed Bishara 		mv_xor_err_interrupt_handler(chan, intr_cause);
768ff7b0479SSaeed Bishara 
769ff7b0479SSaeed Bishara 	tasklet_schedule(&chan->irq_tasklet);
770ff7b0479SSaeed Bishara 
771ff7b0479SSaeed Bishara 	mv_xor_device_clear_eoc_cause(chan);
772ff7b0479SSaeed Bishara 
773ff7b0479SSaeed Bishara 	return IRQ_HANDLED;
774ff7b0479SSaeed Bishara }
775ff7b0479SSaeed Bishara 
776ff7b0479SSaeed Bishara static void mv_xor_issue_pending(struct dma_chan *chan)
777ff7b0479SSaeed Bishara {
778ff7b0479SSaeed Bishara 	struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
779ff7b0479SSaeed Bishara 
780ff7b0479SSaeed Bishara 	if (mv_chan->pending >= MV_XOR_THRESHOLD) {
781ff7b0479SSaeed Bishara 		mv_chan->pending = 0;
782ff7b0479SSaeed Bishara 		mv_chan_activate(mv_chan);
783ff7b0479SSaeed Bishara 	}
784ff7b0479SSaeed Bishara }
785ff7b0479SSaeed Bishara 
786ff7b0479SSaeed Bishara /*
787ff7b0479SSaeed Bishara  * Perform a transaction to verify the HW works.
788ff7b0479SSaeed Bishara  */
789ff7b0479SSaeed Bishara 
790c2714334SLinus Torvalds static int mv_xor_memcpy_self_test(struct mv_xor_chan *mv_chan)
791ff7b0479SSaeed Bishara {
792b8c01d25SEzequiel Garcia 	int i, ret;
793ff7b0479SSaeed Bishara 	void *src, *dest;
794ff7b0479SSaeed Bishara 	dma_addr_t src_dma, dest_dma;
795ff7b0479SSaeed Bishara 	struct dma_chan *dma_chan;
796ff7b0479SSaeed Bishara 	dma_cookie_t cookie;
797ff7b0479SSaeed Bishara 	struct dma_async_tx_descriptor *tx;
798d16695a7SEzequiel Garcia 	struct dmaengine_unmap_data *unmap;
799ff7b0479SSaeed Bishara 	int err = 0;
800ff7b0479SSaeed Bishara 
801d16695a7SEzequiel Garcia 	src = kmalloc(sizeof(u8) * PAGE_SIZE, GFP_KERNEL);
802ff7b0479SSaeed Bishara 	if (!src)
803ff7b0479SSaeed Bishara 		return -ENOMEM;
804ff7b0479SSaeed Bishara 
805d16695a7SEzequiel Garcia 	dest = kzalloc(sizeof(u8) * PAGE_SIZE, GFP_KERNEL);
806ff7b0479SSaeed Bishara 	if (!dest) {
807ff7b0479SSaeed Bishara 		kfree(src);
808ff7b0479SSaeed Bishara 		return -ENOMEM;
809ff7b0479SSaeed Bishara 	}
810ff7b0479SSaeed Bishara 
811ff7b0479SSaeed Bishara 	/* Fill in src buffer */
812d16695a7SEzequiel Garcia 	for (i = 0; i < PAGE_SIZE; i++)
813ff7b0479SSaeed Bishara 		((u8 *) src)[i] = (u8)i;
814ff7b0479SSaeed Bishara 
815275cc0c8SThomas Petazzoni 	dma_chan = &mv_chan->dmachan;
816aa1e6f1aSDan Williams 	if (mv_xor_alloc_chan_resources(dma_chan) < 1) {
817ff7b0479SSaeed Bishara 		err = -ENODEV;
818ff7b0479SSaeed Bishara 		goto out;
819ff7b0479SSaeed Bishara 	}
820ff7b0479SSaeed Bishara 
821d16695a7SEzequiel Garcia 	unmap = dmaengine_get_unmap_data(dma_chan->device->dev, 2, GFP_KERNEL);
822d16695a7SEzequiel Garcia 	if (!unmap) {
823d16695a7SEzequiel Garcia 		err = -ENOMEM;
824d16695a7SEzequiel Garcia 		goto free_resources;
825d16695a7SEzequiel Garcia 	}
826ff7b0479SSaeed Bishara 
827d16695a7SEzequiel Garcia 	src_dma = dma_map_page(dma_chan->device->dev, virt_to_page(src), 0,
828d16695a7SEzequiel Garcia 				 PAGE_SIZE, DMA_TO_DEVICE);
829d16695a7SEzequiel Garcia 	unmap->addr[0] = src_dma;
830d16695a7SEzequiel Garcia 
831b8c01d25SEzequiel Garcia 	ret = dma_mapping_error(dma_chan->device->dev, src_dma);
832b8c01d25SEzequiel Garcia 	if (ret) {
833b8c01d25SEzequiel Garcia 		err = -ENOMEM;
834b8c01d25SEzequiel Garcia 		goto free_resources;
835b8c01d25SEzequiel Garcia 	}
836b8c01d25SEzequiel Garcia 	unmap->to_cnt = 1;
837b8c01d25SEzequiel Garcia 
838d16695a7SEzequiel Garcia 	dest_dma = dma_map_page(dma_chan->device->dev, virt_to_page(dest), 0,
839d16695a7SEzequiel Garcia 				  PAGE_SIZE, DMA_FROM_DEVICE);
840d16695a7SEzequiel Garcia 	unmap->addr[1] = dest_dma;
841d16695a7SEzequiel Garcia 
842b8c01d25SEzequiel Garcia 	ret = dma_mapping_error(dma_chan->device->dev, dest_dma);
843b8c01d25SEzequiel Garcia 	if (ret) {
844b8c01d25SEzequiel Garcia 		err = -ENOMEM;
845b8c01d25SEzequiel Garcia 		goto free_resources;
846b8c01d25SEzequiel Garcia 	}
847b8c01d25SEzequiel Garcia 	unmap->from_cnt = 1;
848d16695a7SEzequiel Garcia 	unmap->len = PAGE_SIZE;
849ff7b0479SSaeed Bishara 
850ff7b0479SSaeed Bishara 	tx = mv_xor_prep_dma_memcpy(dma_chan, dest_dma, src_dma,
851d16695a7SEzequiel Garcia 				    PAGE_SIZE, 0);
852b8c01d25SEzequiel Garcia 	if (!tx) {
853b8c01d25SEzequiel Garcia 		dev_err(dma_chan->device->dev,
854b8c01d25SEzequiel Garcia 			"Self-test cannot prepare operation, disabling\n");
855b8c01d25SEzequiel Garcia 		err = -ENODEV;
856b8c01d25SEzequiel Garcia 		goto free_resources;
857b8c01d25SEzequiel Garcia 	}
858b8c01d25SEzequiel Garcia 
859ff7b0479SSaeed Bishara 	cookie = mv_xor_tx_submit(tx);
860b8c01d25SEzequiel Garcia 	if (dma_submit_error(cookie)) {
861b8c01d25SEzequiel Garcia 		dev_err(dma_chan->device->dev,
862b8c01d25SEzequiel Garcia 			"Self-test submit error, disabling\n");
863b8c01d25SEzequiel Garcia 		err = -ENODEV;
864b8c01d25SEzequiel Garcia 		goto free_resources;
865b8c01d25SEzequiel Garcia 	}
866b8c01d25SEzequiel Garcia 
867ff7b0479SSaeed Bishara 	mv_xor_issue_pending(dma_chan);
868ff7b0479SSaeed Bishara 	async_tx_ack(tx);
869ff7b0479SSaeed Bishara 	msleep(1);
870ff7b0479SSaeed Bishara 
87107934481SLinus Walleij 	if (mv_xor_status(dma_chan, cookie, NULL) !=
872b3efb8fcSVinod Koul 	    DMA_COMPLETE) {
873a3fc74bcSThomas Petazzoni 		dev_err(dma_chan->device->dev,
874ff7b0479SSaeed Bishara 			"Self-test copy timed out, disabling\n");
875ff7b0479SSaeed Bishara 		err = -ENODEV;
876ff7b0479SSaeed Bishara 		goto free_resources;
877ff7b0479SSaeed Bishara 	}
878ff7b0479SSaeed Bishara 
879c35064c4SThomas Petazzoni 	dma_sync_single_for_cpu(dma_chan->device->dev, dest_dma,
880d16695a7SEzequiel Garcia 				PAGE_SIZE, DMA_FROM_DEVICE);
881d16695a7SEzequiel Garcia 	if (memcmp(src, dest, PAGE_SIZE)) {
882a3fc74bcSThomas Petazzoni 		dev_err(dma_chan->device->dev,
883ff7b0479SSaeed Bishara 			"Self-test copy failed compare, disabling\n");
884ff7b0479SSaeed Bishara 		err = -ENODEV;
885ff7b0479SSaeed Bishara 		goto free_resources;
886ff7b0479SSaeed Bishara 	}
887ff7b0479SSaeed Bishara 
888ff7b0479SSaeed Bishara free_resources:
889d16695a7SEzequiel Garcia 	dmaengine_unmap_put(unmap);
890ff7b0479SSaeed Bishara 	mv_xor_free_chan_resources(dma_chan);
891ff7b0479SSaeed Bishara out:
892ff7b0479SSaeed Bishara 	kfree(src);
893ff7b0479SSaeed Bishara 	kfree(dest);
894ff7b0479SSaeed Bishara 	return err;
895ff7b0479SSaeed Bishara }
896ff7b0479SSaeed Bishara 
897ff7b0479SSaeed Bishara #define MV_XOR_NUM_SRC_TEST 4 /* must be <= 15 */
898463a1f8bSBill Pemberton static int
899275cc0c8SThomas Petazzoni mv_xor_xor_self_test(struct mv_xor_chan *mv_chan)
900ff7b0479SSaeed Bishara {
901b8c01d25SEzequiel Garcia 	int i, src_idx, ret;
902ff7b0479SSaeed Bishara 	struct page *dest;
903ff7b0479SSaeed Bishara 	struct page *xor_srcs[MV_XOR_NUM_SRC_TEST];
904ff7b0479SSaeed Bishara 	dma_addr_t dma_srcs[MV_XOR_NUM_SRC_TEST];
905ff7b0479SSaeed Bishara 	dma_addr_t dest_dma;
906ff7b0479SSaeed Bishara 	struct dma_async_tx_descriptor *tx;
907d16695a7SEzequiel Garcia 	struct dmaengine_unmap_data *unmap;
908ff7b0479SSaeed Bishara 	struct dma_chan *dma_chan;
909ff7b0479SSaeed Bishara 	dma_cookie_t cookie;
910ff7b0479SSaeed Bishara 	u8 cmp_byte = 0;
911ff7b0479SSaeed Bishara 	u32 cmp_word;
912ff7b0479SSaeed Bishara 	int err = 0;
913d16695a7SEzequiel Garcia 	int src_count = MV_XOR_NUM_SRC_TEST;
914ff7b0479SSaeed Bishara 
915d16695a7SEzequiel Garcia 	for (src_idx = 0; src_idx < src_count; src_idx++) {
916ff7b0479SSaeed Bishara 		xor_srcs[src_idx] = alloc_page(GFP_KERNEL);
917a09b09aeSRoel Kluin 		if (!xor_srcs[src_idx]) {
918a09b09aeSRoel Kluin 			while (src_idx--)
919ff7b0479SSaeed Bishara 				__free_page(xor_srcs[src_idx]);
920ff7b0479SSaeed Bishara 			return -ENOMEM;
921ff7b0479SSaeed Bishara 		}
922ff7b0479SSaeed Bishara 	}
923ff7b0479SSaeed Bishara 
924ff7b0479SSaeed Bishara 	dest = alloc_page(GFP_KERNEL);
925a09b09aeSRoel Kluin 	if (!dest) {
926a09b09aeSRoel Kluin 		while (src_idx--)
927ff7b0479SSaeed Bishara 			__free_page(xor_srcs[src_idx]);
928ff7b0479SSaeed Bishara 		return -ENOMEM;
929ff7b0479SSaeed Bishara 	}
930ff7b0479SSaeed Bishara 
931ff7b0479SSaeed Bishara 	/* Fill in src buffers */
932d16695a7SEzequiel Garcia 	for (src_idx = 0; src_idx < src_count; src_idx++) {
933ff7b0479SSaeed Bishara 		u8 *ptr = page_address(xor_srcs[src_idx]);
934ff7b0479SSaeed Bishara 		for (i = 0; i < PAGE_SIZE; i++)
935ff7b0479SSaeed Bishara 			ptr[i] = (1 << src_idx);
936ff7b0479SSaeed Bishara 	}
937ff7b0479SSaeed Bishara 
938d16695a7SEzequiel Garcia 	for (src_idx = 0; src_idx < src_count; src_idx++)
939ff7b0479SSaeed Bishara 		cmp_byte ^= (u8) (1 << src_idx);
940ff7b0479SSaeed Bishara 
941ff7b0479SSaeed Bishara 	cmp_word = (cmp_byte << 24) | (cmp_byte << 16) |
942ff7b0479SSaeed Bishara 		(cmp_byte << 8) | cmp_byte;
943ff7b0479SSaeed Bishara 
944ff7b0479SSaeed Bishara 	memset(page_address(dest), 0, PAGE_SIZE);
945ff7b0479SSaeed Bishara 
946275cc0c8SThomas Petazzoni 	dma_chan = &mv_chan->dmachan;
947aa1e6f1aSDan Williams 	if (mv_xor_alloc_chan_resources(dma_chan) < 1) {
948ff7b0479SSaeed Bishara 		err = -ENODEV;
949ff7b0479SSaeed Bishara 		goto out;
950ff7b0479SSaeed Bishara 	}
951ff7b0479SSaeed Bishara 
952d16695a7SEzequiel Garcia 	unmap = dmaengine_get_unmap_data(dma_chan->device->dev, src_count + 1,
953d16695a7SEzequiel Garcia 					 GFP_KERNEL);
954d16695a7SEzequiel Garcia 	if (!unmap) {
955d16695a7SEzequiel Garcia 		err = -ENOMEM;
956d16695a7SEzequiel Garcia 		goto free_resources;
957d16695a7SEzequiel Garcia 	}
958ff7b0479SSaeed Bishara 
959d16695a7SEzequiel Garcia 	/* test xor */
960d16695a7SEzequiel Garcia 	for (i = 0; i < src_count; i++) {
961d16695a7SEzequiel Garcia 		unmap->addr[i] = dma_map_page(dma_chan->device->dev, xor_srcs[i],
962ff7b0479SSaeed Bishara 					      0, PAGE_SIZE, DMA_TO_DEVICE);
963d16695a7SEzequiel Garcia 		dma_srcs[i] = unmap->addr[i];
964b8c01d25SEzequiel Garcia 		ret = dma_mapping_error(dma_chan->device->dev, unmap->addr[i]);
965b8c01d25SEzequiel Garcia 		if (ret) {
966b8c01d25SEzequiel Garcia 			err = -ENOMEM;
967b8c01d25SEzequiel Garcia 			goto free_resources;
968b8c01d25SEzequiel Garcia 		}
969d16695a7SEzequiel Garcia 		unmap->to_cnt++;
970d16695a7SEzequiel Garcia 	}
971d16695a7SEzequiel Garcia 
972d16695a7SEzequiel Garcia 	unmap->addr[src_count] = dma_map_page(dma_chan->device->dev, dest, 0, PAGE_SIZE,
973d16695a7SEzequiel Garcia 				      DMA_FROM_DEVICE);
974d16695a7SEzequiel Garcia 	dest_dma = unmap->addr[src_count];
975b8c01d25SEzequiel Garcia 	ret = dma_mapping_error(dma_chan->device->dev, unmap->addr[src_count]);
976b8c01d25SEzequiel Garcia 	if (ret) {
977b8c01d25SEzequiel Garcia 		err = -ENOMEM;
978b8c01d25SEzequiel Garcia 		goto free_resources;
979b8c01d25SEzequiel Garcia 	}
980d16695a7SEzequiel Garcia 	unmap->from_cnt = 1;
981d16695a7SEzequiel Garcia 	unmap->len = PAGE_SIZE;
982ff7b0479SSaeed Bishara 
983ff7b0479SSaeed Bishara 	tx = mv_xor_prep_dma_xor(dma_chan, dest_dma, dma_srcs,
984d16695a7SEzequiel Garcia 				 src_count, PAGE_SIZE, 0);
985b8c01d25SEzequiel Garcia 	if (!tx) {
986b8c01d25SEzequiel Garcia 		dev_err(dma_chan->device->dev,
987b8c01d25SEzequiel Garcia 			"Self-test cannot prepare operation, disabling\n");
988b8c01d25SEzequiel Garcia 		err = -ENODEV;
989b8c01d25SEzequiel Garcia 		goto free_resources;
990b8c01d25SEzequiel Garcia 	}
991ff7b0479SSaeed Bishara 
992ff7b0479SSaeed Bishara 	cookie = mv_xor_tx_submit(tx);
993b8c01d25SEzequiel Garcia 	if (dma_submit_error(cookie)) {
994b8c01d25SEzequiel Garcia 		dev_err(dma_chan->device->dev,
995b8c01d25SEzequiel Garcia 			"Self-test submit error, disabling\n");
996b8c01d25SEzequiel Garcia 		err = -ENODEV;
997b8c01d25SEzequiel Garcia 		goto free_resources;
998b8c01d25SEzequiel Garcia 	}
999b8c01d25SEzequiel Garcia 
1000ff7b0479SSaeed Bishara 	mv_xor_issue_pending(dma_chan);
1001ff7b0479SSaeed Bishara 	async_tx_ack(tx);
1002ff7b0479SSaeed Bishara 	msleep(8);
1003ff7b0479SSaeed Bishara 
100407934481SLinus Walleij 	if (mv_xor_status(dma_chan, cookie, NULL) !=
1005b3efb8fcSVinod Koul 	    DMA_COMPLETE) {
1006a3fc74bcSThomas Petazzoni 		dev_err(dma_chan->device->dev,
1007ff7b0479SSaeed Bishara 			"Self-test xor timed out, disabling\n");
1008ff7b0479SSaeed Bishara 		err = -ENODEV;
1009ff7b0479SSaeed Bishara 		goto free_resources;
1010ff7b0479SSaeed Bishara 	}
1011ff7b0479SSaeed Bishara 
1012c35064c4SThomas Petazzoni 	dma_sync_single_for_cpu(dma_chan->device->dev, dest_dma,
1013ff7b0479SSaeed Bishara 				PAGE_SIZE, DMA_FROM_DEVICE);
1014ff7b0479SSaeed Bishara 	for (i = 0; i < (PAGE_SIZE / sizeof(u32)); i++) {
1015ff7b0479SSaeed Bishara 		u32 *ptr = page_address(dest);
1016ff7b0479SSaeed Bishara 		if (ptr[i] != cmp_word) {
1017a3fc74bcSThomas Petazzoni 			dev_err(dma_chan->device->dev,
10181ba151cdSJoe Perches 				"Self-test xor failed compare, disabling. index %d, data %x, expected %x\n",
10191ba151cdSJoe Perches 				i, ptr[i], cmp_word);
1020ff7b0479SSaeed Bishara 			err = -ENODEV;
1021ff7b0479SSaeed Bishara 			goto free_resources;
1022ff7b0479SSaeed Bishara 		}
1023ff7b0479SSaeed Bishara 	}
1024ff7b0479SSaeed Bishara 
1025ff7b0479SSaeed Bishara free_resources:
1026d16695a7SEzequiel Garcia 	dmaengine_unmap_put(unmap);
1027ff7b0479SSaeed Bishara 	mv_xor_free_chan_resources(dma_chan);
1028ff7b0479SSaeed Bishara out:
1029d16695a7SEzequiel Garcia 	src_idx = src_count;
1030ff7b0479SSaeed Bishara 	while (src_idx--)
1031ff7b0479SSaeed Bishara 		__free_page(xor_srcs[src_idx]);
1032ff7b0479SSaeed Bishara 	__free_page(dest);
1033ff7b0479SSaeed Bishara 	return err;
1034ff7b0479SSaeed Bishara }
1035ff7b0479SSaeed Bishara 
103634c93c86SAndrew Lunn /* This driver does not implement any of the optional DMA operations. */
103734c93c86SAndrew Lunn static int
103834c93c86SAndrew Lunn mv_xor_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
103934c93c86SAndrew Lunn 	       unsigned long arg)
1040ff7b0479SSaeed Bishara {
104134c93c86SAndrew Lunn 	return -ENOSYS;
104234c93c86SAndrew Lunn }
104334c93c86SAndrew Lunn 
10441ef48a26SThomas Petazzoni static int mv_xor_channel_remove(struct mv_xor_chan *mv_chan)
1045ff7b0479SSaeed Bishara {
1046ff7b0479SSaeed Bishara 	struct dma_chan *chan, *_chan;
10471ef48a26SThomas Petazzoni 	struct device *dev = mv_chan->dmadev.dev;
1048ff7b0479SSaeed Bishara 
10491ef48a26SThomas Petazzoni 	dma_async_device_unregister(&mv_chan->dmadev);
1050ff7b0479SSaeed Bishara 
1051b503fa01SThomas Petazzoni 	dma_free_coherent(dev, MV_XOR_POOL_SIZE,
10521ef48a26SThomas Petazzoni 			  mv_chan->dma_desc_pool_virt, mv_chan->dma_desc_pool);
1053ff7b0479SSaeed Bishara 
10541ef48a26SThomas Petazzoni 	list_for_each_entry_safe(chan, _chan, &mv_chan->dmadev.channels,
1055ff7b0479SSaeed Bishara 				 device_node) {
1056ff7b0479SSaeed Bishara 		list_del(&chan->device_node);
1057ff7b0479SSaeed Bishara 	}
1058ff7b0479SSaeed Bishara 
105988eb92cbSThomas Petazzoni 	free_irq(mv_chan->irq, mv_chan);
106088eb92cbSThomas Petazzoni 
1061ff7b0479SSaeed Bishara 	return 0;
1062ff7b0479SSaeed Bishara }
1063ff7b0479SSaeed Bishara 
10641ef48a26SThomas Petazzoni static struct mv_xor_chan *
1065297eedbaSThomas Petazzoni mv_xor_channel_add(struct mv_xor_device *xordev,
1066a6b4a9d2SThomas Petazzoni 		   struct platform_device *pdev,
1067b503fa01SThomas Petazzoni 		   int idx, dma_cap_mask_t cap_mask, int irq)
1068ff7b0479SSaeed Bishara {
1069ff7b0479SSaeed Bishara 	int ret = 0;
1070ff7b0479SSaeed Bishara 	struct mv_xor_chan *mv_chan;
1071ff7b0479SSaeed Bishara 	struct dma_device *dma_dev;
1072ff7b0479SSaeed Bishara 
10731ef48a26SThomas Petazzoni 	mv_chan = devm_kzalloc(&pdev->dev, sizeof(*mv_chan), GFP_KERNEL);
1074a577659fSSachin Kamat 	if (!mv_chan)
1075a577659fSSachin Kamat 		return ERR_PTR(-ENOMEM);
1076ff7b0479SSaeed Bishara 
10779aedbdbaSThomas Petazzoni 	mv_chan->idx = idx;
107888eb92cbSThomas Petazzoni 	mv_chan->irq = irq;
1079ff7b0479SSaeed Bishara 
10801ef48a26SThomas Petazzoni 	dma_dev = &mv_chan->dmadev;
1081ff7b0479SSaeed Bishara 
1082ff7b0479SSaeed Bishara 	/* allocate coherent memory for hardware descriptors
1083ff7b0479SSaeed Bishara 	 * note: writecombine gives slightly better performance, but
1084ff7b0479SSaeed Bishara 	 * requires that we explicitly flush the writes
1085ff7b0479SSaeed Bishara 	 */
10861ef48a26SThomas Petazzoni 	mv_chan->dma_desc_pool_virt =
1087b503fa01SThomas Petazzoni 	  dma_alloc_writecombine(&pdev->dev, MV_XOR_POOL_SIZE,
10881ef48a26SThomas Petazzoni 				 &mv_chan->dma_desc_pool, GFP_KERNEL);
10891ef48a26SThomas Petazzoni 	if (!mv_chan->dma_desc_pool_virt)
1090a6b4a9d2SThomas Petazzoni 		return ERR_PTR(-ENOMEM);
1091ff7b0479SSaeed Bishara 
1092ff7b0479SSaeed Bishara 	/* discover transaction capabilites from the platform data */
1093a6b4a9d2SThomas Petazzoni 	dma_dev->cap_mask = cap_mask;
1094ff7b0479SSaeed Bishara 
1095ff7b0479SSaeed Bishara 	INIT_LIST_HEAD(&dma_dev->channels);
1096ff7b0479SSaeed Bishara 
1097ff7b0479SSaeed Bishara 	/* set base routines */
1098ff7b0479SSaeed Bishara 	dma_dev->device_alloc_chan_resources = mv_xor_alloc_chan_resources;
1099ff7b0479SSaeed Bishara 	dma_dev->device_free_chan_resources = mv_xor_free_chan_resources;
110007934481SLinus Walleij 	dma_dev->device_tx_status = mv_xor_status;
1101ff7b0479SSaeed Bishara 	dma_dev->device_issue_pending = mv_xor_issue_pending;
110234c93c86SAndrew Lunn 	dma_dev->device_control = mv_xor_control;
1103ff7b0479SSaeed Bishara 	dma_dev->dev = &pdev->dev;
1104ff7b0479SSaeed Bishara 
1105ff7b0479SSaeed Bishara 	/* set prep routines based on capability */
1106ff7b0479SSaeed Bishara 	if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask))
1107ff7b0479SSaeed Bishara 		dma_dev->device_prep_dma_memcpy = mv_xor_prep_dma_memcpy;
1108ff7b0479SSaeed Bishara 	if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
1109c019894eSJoe Perches 		dma_dev->max_xor = 8;
1110ff7b0479SSaeed Bishara 		dma_dev->device_prep_dma_xor = mv_xor_prep_dma_xor;
1111ff7b0479SSaeed Bishara 	}
1112ff7b0479SSaeed Bishara 
1113297eedbaSThomas Petazzoni 	mv_chan->mmr_base = xordev->xor_base;
111482a1402eSEzequiel Garcia 	mv_chan->mmr_high_base = xordev->xor_high_base;
1115ff7b0479SSaeed Bishara 	tasklet_init(&mv_chan->irq_tasklet, mv_xor_tasklet, (unsigned long)
1116ff7b0479SSaeed Bishara 		     mv_chan);
1117ff7b0479SSaeed Bishara 
1118ff7b0479SSaeed Bishara 	/* clear errors before enabling interrupts */
1119ff7b0479SSaeed Bishara 	mv_xor_device_clear_err_status(mv_chan);
1120ff7b0479SSaeed Bishara 
11212d0a0745SThomas Petazzoni 	ret = request_irq(mv_chan->irq, mv_xor_interrupt_handler,
1122ff7b0479SSaeed Bishara 			  0, dev_name(&pdev->dev), mv_chan);
1123ff7b0479SSaeed Bishara 	if (ret)
1124ff7b0479SSaeed Bishara 		goto err_free_dma;
1125ff7b0479SSaeed Bishara 
1126ff7b0479SSaeed Bishara 	mv_chan_unmask_interrupts(mv_chan);
1127ff7b0479SSaeed Bishara 
1128ff7b0479SSaeed Bishara 	mv_set_mode(mv_chan, DMA_MEMCPY);
1129ff7b0479SSaeed Bishara 
1130ff7b0479SSaeed Bishara 	spin_lock_init(&mv_chan->lock);
1131ff7b0479SSaeed Bishara 	INIT_LIST_HEAD(&mv_chan->chain);
1132ff7b0479SSaeed Bishara 	INIT_LIST_HEAD(&mv_chan->completed_slots);
1133ff7b0479SSaeed Bishara 	INIT_LIST_HEAD(&mv_chan->all_slots);
113498817b99SThomas Petazzoni 	mv_chan->dmachan.device = dma_dev;
113598817b99SThomas Petazzoni 	dma_cookie_init(&mv_chan->dmachan);
1136ff7b0479SSaeed Bishara 
113798817b99SThomas Petazzoni 	list_add_tail(&mv_chan->dmachan.device_node, &dma_dev->channels);
1138ff7b0479SSaeed Bishara 
1139ff7b0479SSaeed Bishara 	if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask)) {
1140275cc0c8SThomas Petazzoni 		ret = mv_xor_memcpy_self_test(mv_chan);
1141ff7b0479SSaeed Bishara 		dev_dbg(&pdev->dev, "memcpy self test returned %d\n", ret);
1142ff7b0479SSaeed Bishara 		if (ret)
11432d0a0745SThomas Petazzoni 			goto err_free_irq;
1144ff7b0479SSaeed Bishara 	}
1145ff7b0479SSaeed Bishara 
1146ff7b0479SSaeed Bishara 	if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
1147275cc0c8SThomas Petazzoni 		ret = mv_xor_xor_self_test(mv_chan);
1148ff7b0479SSaeed Bishara 		dev_dbg(&pdev->dev, "xor self test returned %d\n", ret);
1149ff7b0479SSaeed Bishara 		if (ret)
11502d0a0745SThomas Petazzoni 			goto err_free_irq;
1151ff7b0479SSaeed Bishara 	}
1152ff7b0479SSaeed Bishara 
115348a9db46SBartlomiej Zolnierkiewicz 	dev_info(&pdev->dev, "Marvell XOR: ( %s%s%s)\n",
1154ff7b0479SSaeed Bishara 		 dma_has_cap(DMA_XOR, dma_dev->cap_mask) ? "xor " : "",
1155ff7b0479SSaeed Bishara 		 dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask) ? "cpy " : "",
1156ff7b0479SSaeed Bishara 		 dma_has_cap(DMA_INTERRUPT, dma_dev->cap_mask) ? "intr " : "");
1157ff7b0479SSaeed Bishara 
1158ff7b0479SSaeed Bishara 	dma_async_device_register(dma_dev);
11591ef48a26SThomas Petazzoni 	return mv_chan;
1160ff7b0479SSaeed Bishara 
11612d0a0745SThomas Petazzoni err_free_irq:
11622d0a0745SThomas Petazzoni 	free_irq(mv_chan->irq, mv_chan);
1163ff7b0479SSaeed Bishara  err_free_dma:
1164b503fa01SThomas Petazzoni 	dma_free_coherent(&pdev->dev, MV_XOR_POOL_SIZE,
11651ef48a26SThomas Petazzoni 			  mv_chan->dma_desc_pool_virt, mv_chan->dma_desc_pool);
1166a6b4a9d2SThomas Petazzoni 	return ERR_PTR(ret);
1167ff7b0479SSaeed Bishara }
1168ff7b0479SSaeed Bishara 
1169ff7b0479SSaeed Bishara static void
1170297eedbaSThomas Petazzoni mv_xor_conf_mbus_windows(struct mv_xor_device *xordev,
117163a9332bSAndrew Lunn 			 const struct mbus_dram_target_info *dram)
1172ff7b0479SSaeed Bishara {
117382a1402eSEzequiel Garcia 	void __iomem *base = xordev->xor_high_base;
1174ff7b0479SSaeed Bishara 	u32 win_enable = 0;
1175ff7b0479SSaeed Bishara 	int i;
1176ff7b0479SSaeed Bishara 
1177ff7b0479SSaeed Bishara 	for (i = 0; i < 8; i++) {
1178ff7b0479SSaeed Bishara 		writel(0, base + WINDOW_BASE(i));
1179ff7b0479SSaeed Bishara 		writel(0, base + WINDOW_SIZE(i));
1180ff7b0479SSaeed Bishara 		if (i < 4)
1181ff7b0479SSaeed Bishara 			writel(0, base + WINDOW_REMAP_HIGH(i));
1182ff7b0479SSaeed Bishara 	}
1183ff7b0479SSaeed Bishara 
1184ff7b0479SSaeed Bishara 	for (i = 0; i < dram->num_cs; i++) {
118563a9332bSAndrew Lunn 		const struct mbus_dram_window *cs = dram->cs + i;
1186ff7b0479SSaeed Bishara 
1187ff7b0479SSaeed Bishara 		writel((cs->base & 0xffff0000) |
1188ff7b0479SSaeed Bishara 		       (cs->mbus_attr << 8) |
1189ff7b0479SSaeed Bishara 		       dram->mbus_dram_target_id, base + WINDOW_BASE(i));
1190ff7b0479SSaeed Bishara 		writel((cs->size - 1) & 0xffff0000, base + WINDOW_SIZE(i));
1191ff7b0479SSaeed Bishara 
1192ff7b0479SSaeed Bishara 		win_enable |= (1 << i);
1193ff7b0479SSaeed Bishara 		win_enable |= 3 << (16 + (2 * i));
1194ff7b0479SSaeed Bishara 	}
1195ff7b0479SSaeed Bishara 
1196ff7b0479SSaeed Bishara 	writel(win_enable, base + WINDOW_BAR_ENABLE(0));
1197ff7b0479SSaeed Bishara 	writel(win_enable, base + WINDOW_BAR_ENABLE(1));
1198c4b4b732SThomas Petazzoni 	writel(0, base + WINDOW_OVERRIDE_CTRL(0));
1199c4b4b732SThomas Petazzoni 	writel(0, base + WINDOW_OVERRIDE_CTRL(1));
1200ff7b0479SSaeed Bishara }
1201ff7b0479SSaeed Bishara 
1202c2714334SLinus Torvalds static int mv_xor_probe(struct platform_device *pdev)
1203ff7b0479SSaeed Bishara {
120463a9332bSAndrew Lunn 	const struct mbus_dram_target_info *dram;
1205297eedbaSThomas Petazzoni 	struct mv_xor_device *xordev;
1206d4adcc01SJingoo Han 	struct mv_xor_platform_data *pdata = dev_get_platdata(&pdev->dev);
1207ff7b0479SSaeed Bishara 	struct resource *res;
120860d151f3SThomas Petazzoni 	int i, ret;
1209ff7b0479SSaeed Bishara 
12101ba151cdSJoe Perches 	dev_notice(&pdev->dev, "Marvell shared XOR driver\n");
1211ff7b0479SSaeed Bishara 
1212297eedbaSThomas Petazzoni 	xordev = devm_kzalloc(&pdev->dev, sizeof(*xordev), GFP_KERNEL);
1213297eedbaSThomas Petazzoni 	if (!xordev)
1214ff7b0479SSaeed Bishara 		return -ENOMEM;
1215ff7b0479SSaeed Bishara 
1216ff7b0479SSaeed Bishara 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1217ff7b0479SSaeed Bishara 	if (!res)
1218ff7b0479SSaeed Bishara 		return -ENODEV;
1219ff7b0479SSaeed Bishara 
1220297eedbaSThomas Petazzoni 	xordev->xor_base = devm_ioremap(&pdev->dev, res->start,
12214de1ba15SH Hartley Sweeten 					resource_size(res));
1222297eedbaSThomas Petazzoni 	if (!xordev->xor_base)
1223ff7b0479SSaeed Bishara 		return -EBUSY;
1224ff7b0479SSaeed Bishara 
1225ff7b0479SSaeed Bishara 	res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1226ff7b0479SSaeed Bishara 	if (!res)
1227ff7b0479SSaeed Bishara 		return -ENODEV;
1228ff7b0479SSaeed Bishara 
1229297eedbaSThomas Petazzoni 	xordev->xor_high_base = devm_ioremap(&pdev->dev, res->start,
12304de1ba15SH Hartley Sweeten 					     resource_size(res));
1231297eedbaSThomas Petazzoni 	if (!xordev->xor_high_base)
1232ff7b0479SSaeed Bishara 		return -EBUSY;
1233ff7b0479SSaeed Bishara 
1234297eedbaSThomas Petazzoni 	platform_set_drvdata(pdev, xordev);
1235ff7b0479SSaeed Bishara 
1236ff7b0479SSaeed Bishara 	/*
1237ff7b0479SSaeed Bishara 	 * (Re-)program MBUS remapping windows if we are asked to.
1238ff7b0479SSaeed Bishara 	 */
123963a9332bSAndrew Lunn 	dram = mv_mbus_dram_info();
124063a9332bSAndrew Lunn 	if (dram)
1241297eedbaSThomas Petazzoni 		mv_xor_conf_mbus_windows(xordev, dram);
1242ff7b0479SSaeed Bishara 
1243c510182bSAndrew Lunn 	/* Not all platforms can gate the clock, so it is not
1244c510182bSAndrew Lunn 	 * an error if the clock does not exists.
1245c510182bSAndrew Lunn 	 */
1246297eedbaSThomas Petazzoni 	xordev->clk = clk_get(&pdev->dev, NULL);
1247297eedbaSThomas Petazzoni 	if (!IS_ERR(xordev->clk))
1248297eedbaSThomas Petazzoni 		clk_prepare_enable(xordev->clk);
1249c510182bSAndrew Lunn 
1250f7d12ef5SThomas Petazzoni 	if (pdev->dev.of_node) {
1251f7d12ef5SThomas Petazzoni 		struct device_node *np;
1252f7d12ef5SThomas Petazzoni 		int i = 0;
1253f7d12ef5SThomas Petazzoni 
1254f7d12ef5SThomas Petazzoni 		for_each_child_of_node(pdev->dev.of_node, np) {
12550be8253fSRussell King 			struct mv_xor_chan *chan;
1256f7d12ef5SThomas Petazzoni 			dma_cap_mask_t cap_mask;
1257f7d12ef5SThomas Petazzoni 			int irq;
1258f7d12ef5SThomas Petazzoni 
1259f7d12ef5SThomas Petazzoni 			dma_cap_zero(cap_mask);
1260f7d12ef5SThomas Petazzoni 			if (of_property_read_bool(np, "dmacap,memcpy"))
1261f7d12ef5SThomas Petazzoni 				dma_cap_set(DMA_MEMCPY, cap_mask);
1262f7d12ef5SThomas Petazzoni 			if (of_property_read_bool(np, "dmacap,xor"))
1263f7d12ef5SThomas Petazzoni 				dma_cap_set(DMA_XOR, cap_mask);
1264f7d12ef5SThomas Petazzoni 			if (of_property_read_bool(np, "dmacap,interrupt"))
1265f7d12ef5SThomas Petazzoni 				dma_cap_set(DMA_INTERRUPT, cap_mask);
1266f7d12ef5SThomas Petazzoni 
1267f7d12ef5SThomas Petazzoni 			irq = irq_of_parse_and_map(np, 0);
1268f8eb9e7dSThomas Petazzoni 			if (!irq) {
1269f8eb9e7dSThomas Petazzoni 				ret = -ENODEV;
1270f7d12ef5SThomas Petazzoni 				goto err_channel_add;
1271f7d12ef5SThomas Petazzoni 			}
1272f7d12ef5SThomas Petazzoni 
12730be8253fSRussell King 			chan = mv_xor_channel_add(xordev, pdev, i,
1274f7d12ef5SThomas Petazzoni 						  cap_mask, irq);
12750be8253fSRussell King 			if (IS_ERR(chan)) {
12760be8253fSRussell King 				ret = PTR_ERR(chan);
1277f7d12ef5SThomas Petazzoni 				irq_dispose_mapping(irq);
1278f7d12ef5SThomas Petazzoni 				goto err_channel_add;
1279f7d12ef5SThomas Petazzoni 			}
1280f7d12ef5SThomas Petazzoni 
12810be8253fSRussell King 			xordev->channels[i] = chan;
1282f7d12ef5SThomas Petazzoni 			i++;
1283f7d12ef5SThomas Petazzoni 		}
1284f7d12ef5SThomas Petazzoni 	} else if (pdata && pdata->channels) {
128560d151f3SThomas Petazzoni 		for (i = 0; i < MV_XOR_MAX_CHANNELS; i++) {
1286e39f6ec1SThomas Petazzoni 			struct mv_xor_channel_data *cd;
12870be8253fSRussell King 			struct mv_xor_chan *chan;
128860d151f3SThomas Petazzoni 			int irq;
128960d151f3SThomas Petazzoni 
129060d151f3SThomas Petazzoni 			cd = &pdata->channels[i];
129160d151f3SThomas Petazzoni 			if (!cd) {
129260d151f3SThomas Petazzoni 				ret = -ENODEV;
129360d151f3SThomas Petazzoni 				goto err_channel_add;
129460d151f3SThomas Petazzoni 			}
129560d151f3SThomas Petazzoni 
129660d151f3SThomas Petazzoni 			irq = platform_get_irq(pdev, i);
129760d151f3SThomas Petazzoni 			if (irq < 0) {
129860d151f3SThomas Petazzoni 				ret = irq;
129960d151f3SThomas Petazzoni 				goto err_channel_add;
130060d151f3SThomas Petazzoni 			}
130160d151f3SThomas Petazzoni 
13020be8253fSRussell King 			chan = mv_xor_channel_add(xordev, pdev, i,
1303b503fa01SThomas Petazzoni 						  cd->cap_mask, irq);
13040be8253fSRussell King 			if (IS_ERR(chan)) {
13050be8253fSRussell King 				ret = PTR_ERR(chan);
130660d151f3SThomas Petazzoni 				goto err_channel_add;
130760d151f3SThomas Petazzoni 			}
13080be8253fSRussell King 
13090be8253fSRussell King 			xordev->channels[i] = chan;
131060d151f3SThomas Petazzoni 		}
131160d151f3SThomas Petazzoni 	}
131260d151f3SThomas Petazzoni 
1313ff7b0479SSaeed Bishara 	return 0;
131460d151f3SThomas Petazzoni 
131560d151f3SThomas Petazzoni err_channel_add:
131660d151f3SThomas Petazzoni 	for (i = 0; i < MV_XOR_MAX_CHANNELS; i++)
1317f7d12ef5SThomas Petazzoni 		if (xordev->channels[i]) {
1318ab6e439fSThomas Petazzoni 			mv_xor_channel_remove(xordev->channels[i]);
1319f7d12ef5SThomas Petazzoni 			if (pdev->dev.of_node)
1320f7d12ef5SThomas Petazzoni 				irq_dispose_mapping(xordev->channels[i]->irq);
1321f7d12ef5SThomas Petazzoni 		}
132260d151f3SThomas Petazzoni 
1323dab92064SThomas Petazzoni 	if (!IS_ERR(xordev->clk)) {
1324297eedbaSThomas Petazzoni 		clk_disable_unprepare(xordev->clk);
1325297eedbaSThomas Petazzoni 		clk_put(xordev->clk);
1326dab92064SThomas Petazzoni 	}
1327dab92064SThomas Petazzoni 
132860d151f3SThomas Petazzoni 	return ret;
1329ff7b0479SSaeed Bishara }
1330ff7b0479SSaeed Bishara 
1331c2714334SLinus Torvalds static int mv_xor_remove(struct platform_device *pdev)
1332ff7b0479SSaeed Bishara {
1333297eedbaSThomas Petazzoni 	struct mv_xor_device *xordev = platform_get_drvdata(pdev);
133460d151f3SThomas Petazzoni 	int i;
133560d151f3SThomas Petazzoni 
133660d151f3SThomas Petazzoni 	for (i = 0; i < MV_XOR_MAX_CHANNELS; i++) {
1337297eedbaSThomas Petazzoni 		if (xordev->channels[i])
1338297eedbaSThomas Petazzoni 			mv_xor_channel_remove(xordev->channels[i]);
133960d151f3SThomas Petazzoni 	}
1340c510182bSAndrew Lunn 
1341297eedbaSThomas Petazzoni 	if (!IS_ERR(xordev->clk)) {
1342297eedbaSThomas Petazzoni 		clk_disable_unprepare(xordev->clk);
1343297eedbaSThomas Petazzoni 		clk_put(xordev->clk);
1344c510182bSAndrew Lunn 	}
1345c510182bSAndrew Lunn 
1346ff7b0479SSaeed Bishara 	return 0;
1347ff7b0479SSaeed Bishara }
1348ff7b0479SSaeed Bishara 
1349f7d12ef5SThomas Petazzoni #ifdef CONFIG_OF
1350c2714334SLinus Torvalds static struct of_device_id mv_xor_dt_ids[] = {
1351f7d12ef5SThomas Petazzoni        { .compatible = "marvell,orion-xor", },
1352f7d12ef5SThomas Petazzoni        {},
1353f7d12ef5SThomas Petazzoni };
1354f7d12ef5SThomas Petazzoni MODULE_DEVICE_TABLE(of, mv_xor_dt_ids);
1355f7d12ef5SThomas Petazzoni #endif
1356f7d12ef5SThomas Petazzoni 
1357ff7b0479SSaeed Bishara static struct platform_driver mv_xor_driver = {
1358ff7b0479SSaeed Bishara 	.probe		= mv_xor_probe,
1359a7d6e3ecSBill Pemberton 	.remove		= mv_xor_remove,
1360ff7b0479SSaeed Bishara 	.driver		= {
1361ff7b0479SSaeed Bishara 		.owner	        = THIS_MODULE,
1362ff7b0479SSaeed Bishara 		.name	        = MV_XOR_NAME,
1363f7d12ef5SThomas Petazzoni 		.of_match_table = of_match_ptr(mv_xor_dt_ids),
1364ff7b0479SSaeed Bishara 	},
1365ff7b0479SSaeed Bishara };
1366ff7b0479SSaeed Bishara 
1367ff7b0479SSaeed Bishara 
1368ff7b0479SSaeed Bishara static int __init mv_xor_init(void)
1369ff7b0479SSaeed Bishara {
137061971656SThomas Petazzoni 	return platform_driver_register(&mv_xor_driver);
1371ff7b0479SSaeed Bishara }
1372ff7b0479SSaeed Bishara module_init(mv_xor_init);
1373ff7b0479SSaeed Bishara 
1374ff7b0479SSaeed Bishara /* it's currently unsafe to unload this module */
1375ff7b0479SSaeed Bishara #if 0
1376ff7b0479SSaeed Bishara static void __exit mv_xor_exit(void)
1377ff7b0479SSaeed Bishara {
1378ff7b0479SSaeed Bishara 	platform_driver_unregister(&mv_xor_driver);
1379ff7b0479SSaeed Bishara 	return;
1380ff7b0479SSaeed Bishara }
1381ff7b0479SSaeed Bishara 
1382ff7b0479SSaeed Bishara module_exit(mv_xor_exit);
1383ff7b0479SSaeed Bishara #endif
1384ff7b0479SSaeed Bishara 
1385ff7b0479SSaeed Bishara MODULE_AUTHOR("Saeed Bishara <saeed@marvell.com>");
1386ff7b0479SSaeed Bishara MODULE_DESCRIPTION("DMA engine driver for Marvell's XOR engine");
1387ff7b0479SSaeed Bishara MODULE_LICENSE("GPL");
1388