blob: a0e118725ae33db49fcbe0eef07a8561f1f10e12 [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>
Lior Amsalem6f166312015-05-26 15:07:34 +020022#include <linux/of_device.h>
Saeed Bisharaff7b0472008-07-08 11:58:36 -070023#include <linux/platform_device.h>
24#include <linux/memory.h>
Andrew Lunnc5101822012-02-19 13:30:26 +010025#include <linux/clk.h>
Thomas Petazzonif7d12ef2012-11-15 16:47:58 +010026#include <linux/of.h>
27#include <linux/of_irq.h>
28#include <linux/irqdomain.h>
Thomas Petazzoni77757292015-07-08 16:28:19 +020029#include <linux/cpumask.h>
Arnd Bergmannc02cecb2012-08-24 15:21:54 +020030#include <linux/platform_data/dma-mv_xor.h>
Russell King - ARM Linuxd2ebfb32012-03-06 22:34:26 +000031
32#include "dmaengine.h"
Saeed Bisharaff7b0472008-07-08 11:58:36 -070033#include "mv_xor.h"
34
Lior Amsalem6f166312015-05-26 15:07:34 +020035enum mv_xor_mode {
36 XOR_MODE_IN_REG,
37 XOR_MODE_IN_DESC,
38};
39
Saeed Bisharaff7b0472008-07-08 11:58:36 -070040static void mv_xor_issue_pending(struct dma_chan *chan);
41
42#define to_mv_xor_chan(chan) \
Thomas Petazzoni98817b92012-11-15 14:57:44 +010043 container_of(chan, struct mv_xor_chan, dmachan)
Saeed Bisharaff7b0472008-07-08 11:58:36 -070044
45#define to_mv_xor_slot(tx) \
46 container_of(tx, struct mv_xor_desc_slot, async_tx)
47
Thomas Petazzonic98c1782012-11-15 14:17:18 +010048#define mv_chan_to_devp(chan) \
Thomas Petazzoni1ef48a22012-11-15 15:17:05 +010049 ((chan)->dmadev.dev)
Thomas Petazzonic98c1782012-11-15 14:17:18 +010050
Lior Amsalemdfc97662014-08-27 10:52:51 -030051static void mv_desc_init(struct mv_xor_desc_slot *desc,
Lior Amsalemba87d132014-08-27 10:52:53 -030052 dma_addr_t addr, u32 byte_count,
53 enum dma_ctrl_flags flags)
Saeed Bisharaff7b0472008-07-08 11:58:36 -070054{
55 struct mv_xor_desc *hw_desc = desc->hw_desc;
56
Ezequiel Garcia0e7488e2014-08-27 10:52:52 -030057 hw_desc->status = XOR_DESC_DMA_OWNED;
Saeed Bisharaff7b0472008-07-08 11:58:36 -070058 hw_desc->phy_next_desc = 0;
Lior Amsalemba87d132014-08-27 10:52:53 -030059 /* Enable end-of-descriptor interrupts only for DMA_PREP_INTERRUPT */
60 hw_desc->desc_command = (flags & DMA_PREP_INTERRUPT) ?
61 XOR_DESC_EOD_INT_EN : 0;
Lior Amsalemdfc97662014-08-27 10:52:51 -030062 hw_desc->phy_dest_addr = addr;
Saeed Bisharaff7b0472008-07-08 11:58:36 -070063 hw_desc->byte_count = byte_count;
64}
65
Lior Amsalem6f166312015-05-26 15:07:34 +020066static void mv_desc_set_mode(struct mv_xor_desc_slot *desc)
67{
68 struct mv_xor_desc *hw_desc = desc->hw_desc;
69
70 switch (desc->type) {
71 case DMA_XOR:
72 case DMA_INTERRUPT:
73 hw_desc->desc_command |= XOR_DESC_OPERATION_XOR;
74 break;
75 case DMA_MEMCPY:
76 hw_desc->desc_command |= XOR_DESC_OPERATION_MEMCPY;
77 break;
78 default:
79 BUG();
80 return;
81 }
82}
83
Saeed Bisharaff7b0472008-07-08 11:58:36 -070084static void mv_desc_set_next_desc(struct mv_xor_desc_slot *desc,
85 u32 next_desc_addr)
86{
87 struct mv_xor_desc *hw_desc = desc->hw_desc;
88 BUG_ON(hw_desc->phy_next_desc);
89 hw_desc->phy_next_desc = next_desc_addr;
90}
91
Saeed Bisharaff7b0472008-07-08 11:58:36 -070092static void mv_desc_set_src_addr(struct mv_xor_desc_slot *desc,
93 int index, dma_addr_t addr)
94{
95 struct mv_xor_desc *hw_desc = desc->hw_desc;
Thomas Petazzonie03bc652013-07-29 17:42:14 +020096 hw_desc->phy_src_addr[mv_phy_src_idx(index)] = addr;
Saeed Bisharaff7b0472008-07-08 11:58:36 -070097 if (desc->type == DMA_XOR)
98 hw_desc->desc_command |= (1 << index);
99}
100
101static u32 mv_chan_get_current_desc(struct mv_xor_chan *chan)
102{
Thomas Petazzoni5733c382013-07-29 17:42:13 +0200103 return readl_relaxed(XOR_CURR_DESC(chan));
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700104}
105
106static void mv_chan_set_next_descriptor(struct mv_xor_chan *chan,
107 u32 next_desc_addr)
108{
Thomas Petazzoni5733c382013-07-29 17:42:13 +0200109 writel_relaxed(next_desc_addr, XOR_NEXT_DESC(chan));
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700110}
111
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700112static void mv_chan_unmask_interrupts(struct mv_xor_chan *chan)
113{
Thomas Petazzoni5733c382013-07-29 17:42:13 +0200114 u32 val = readl_relaxed(XOR_INTR_MASK(chan));
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700115 val |= XOR_INTR_MASK_VALUE << (chan->idx * 16);
Thomas Petazzoni5733c382013-07-29 17:42:13 +0200116 writel_relaxed(val, XOR_INTR_MASK(chan));
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700117}
118
119static u32 mv_chan_get_intr_cause(struct mv_xor_chan *chan)
120{
Thomas Petazzoni5733c382013-07-29 17:42:13 +0200121 u32 intr_cause = readl_relaxed(XOR_INTR_CAUSE(chan));
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700122 intr_cause = (intr_cause >> (chan->idx * 16)) & 0xFFFF;
123 return intr_cause;
124}
125
Maxime Ripard0951e722015-05-26 15:07:33 +0200126static void mv_chan_clear_eoc_cause(struct mv_xor_chan *chan)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700127{
Lior Amsalemba87d132014-08-27 10:52:53 -0300128 u32 val;
129
130 val = XOR_INT_END_OF_DESC | XOR_INT_END_OF_CHAIN | XOR_INT_STOPPED;
131 val = ~(val << (chan->idx * 16));
Thomas Petazzonic98c1782012-11-15 14:17:18 +0100132 dev_dbg(mv_chan_to_devp(chan), "%s, val 0x%08x\n", __func__, val);
Thomas Petazzoni5733c382013-07-29 17:42:13 +0200133 writel_relaxed(val, XOR_INTR_CAUSE(chan));
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700134}
135
Maxime Ripard0951e722015-05-26 15:07:33 +0200136static void mv_chan_clear_err_status(struct mv_xor_chan *chan)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700137{
138 u32 val = 0xFFFF0000 >> (chan->idx * 16);
Thomas Petazzoni5733c382013-07-29 17:42:13 +0200139 writel_relaxed(val, XOR_INTR_CAUSE(chan));
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700140}
141
Maxime Ripard0951e722015-05-26 15:07:33 +0200142static void mv_chan_set_mode(struct mv_xor_chan *chan,
143 enum dma_transaction_type type)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700144{
145 u32 op_mode;
Thomas Petazzoni5733c382013-07-29 17:42:13 +0200146 u32 config = readl_relaxed(XOR_CONFIG(chan));
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700147
148 switch (type) {
149 case DMA_XOR:
150 op_mode = XOR_OPERATION_MODE_XOR;
151 break;
152 case DMA_MEMCPY:
153 op_mode = XOR_OPERATION_MODE_MEMCPY;
154 break;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700155 default:
Thomas Petazzonic98c1782012-11-15 14:17:18 +0100156 dev_err(mv_chan_to_devp(chan),
Joe Perches1ba151c2012-10-28 01:05:44 -0700157 "error: unsupported operation %d\n",
Thomas Petazzonia3fc74b2012-11-15 12:50:27 +0100158 type);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700159 BUG();
160 return;
161 }
162
163 config &= ~0x7;
164 config |= op_mode;
Thomas Petazzonie03bc652013-07-29 17:42:14 +0200165
Lior Amsalem6f166312015-05-26 15:07:34 +0200166 if (IS_ENABLED(__BIG_ENDIAN))
167 config |= XOR_DESCRIPTOR_SWAP;
168 else
169 config &= ~XOR_DESCRIPTOR_SWAP;
170
171 writel_relaxed(config, XOR_CONFIG(chan));
172 chan->current_type = type;
173}
174
175static void mv_chan_set_mode_to_desc(struct mv_xor_chan *chan)
176{
177 u32 op_mode;
178 u32 config = readl_relaxed(XOR_CONFIG(chan));
179
180 op_mode = XOR_OPERATION_MODE_IN_DESC;
181
182 config &= ~0x7;
183 config |= op_mode;
184
Thomas Petazzonie03bc652013-07-29 17:42:14 +0200185#if defined(__BIG_ENDIAN)
186 config |= XOR_DESCRIPTOR_SWAP;
187#else
188 config &= ~XOR_DESCRIPTOR_SWAP;
189#endif
190
Thomas Petazzoni5733c382013-07-29 17:42:13 +0200191 writel_relaxed(config, XOR_CONFIG(chan));
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700192}
193
194static void mv_chan_activate(struct mv_xor_chan *chan)
195{
Thomas Petazzonic98c1782012-11-15 14:17:18 +0100196 dev_dbg(mv_chan_to_devp(chan), " activate chan.\n");
Ezequiel Garcia5a9a55b2014-05-21 14:02:35 -0700197
198 /* writel ensures all descriptors are flushed before activation */
199 writel(BIT(0), XOR_ACTIVATION(chan));
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700200}
201
202static char mv_chan_is_busy(struct mv_xor_chan *chan)
203{
Thomas Petazzoni5733c382013-07-29 17:42:13 +0200204 u32 state = readl_relaxed(XOR_ACTIVATION(chan));
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700205
206 state = (state >> 4) & 0x3;
207
208 return (state == 1) ? 1 : 0;
209}
210
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700211/*
Maxime Ripard0951e722015-05-26 15:07:33 +0200212 * mv_chan_start_new_chain - program the engine to operate on new
213 * chain headed by sw_desc
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700214 * Caller must hold &mv_chan->lock while calling this function
215 */
Maxime Ripard0951e722015-05-26 15:07:33 +0200216static void mv_chan_start_new_chain(struct mv_xor_chan *mv_chan,
217 struct mv_xor_desc_slot *sw_desc)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700218{
Thomas Petazzonic98c1782012-11-15 14:17:18 +0100219 dev_dbg(mv_chan_to_devp(mv_chan), "%s %d: sw_desc %p\n",
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700220 __func__, __LINE__, sw_desc);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700221
Bartlomiej Zolnierkiewicz48a9db42013-07-03 15:05:06 -0700222 /* set the hardware chain */
223 mv_chan_set_next_descriptor(mv_chan, sw_desc->async_tx.phys);
224
Lior Amsalemdfc97662014-08-27 10:52:51 -0300225 mv_chan->pending++;
Thomas Petazzoni98817b92012-11-15 14:57:44 +0100226 mv_xor_issue_pending(&mv_chan->dmachan);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700227}
228
229static dma_cookie_t
Maxime Ripard0951e722015-05-26 15:07:33 +0200230mv_desc_run_tx_complete_actions(struct mv_xor_desc_slot *desc,
231 struct mv_xor_chan *mv_chan,
232 dma_cookie_t cookie)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700233{
234 BUG_ON(desc->async_tx.cookie < 0);
235
236 if (desc->async_tx.cookie > 0) {
237 cookie = desc->async_tx.cookie;
238
239 /* call the callback (must not sleep or submit new
240 * operations to this channel)
241 */
242 if (desc->async_tx.callback)
243 desc->async_tx.callback(
244 desc->async_tx.callback_param);
245
Dan Williamsd38a8c62013-10-18 19:35:23 +0200246 dma_descriptor_unmap(&desc->async_tx);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700247 }
248
249 /* run dependent operations */
Dan Williams07f22112009-01-05 17:14:31 -0700250 dma_run_dependencies(&desc->async_tx);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700251
252 return cookie;
253}
254
255static int
Maxime Ripard0951e722015-05-26 15:07:33 +0200256mv_chan_clean_completed_slots(struct mv_xor_chan *mv_chan)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700257{
258 struct mv_xor_desc_slot *iter, *_iter;
259
Thomas Petazzonic98c1782012-11-15 14:17:18 +0100260 dev_dbg(mv_chan_to_devp(mv_chan), "%s %d\n", __func__, __LINE__);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700261 list_for_each_entry_safe(iter, _iter, &mv_chan->completed_slots,
Lior Amsalemfbea28a2015-05-26 15:07:36 +0200262 node) {
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700263
Lior Amsalemfbea28a2015-05-26 15:07:36 +0200264 if (async_tx_test_ack(&iter->async_tx))
265 list_move_tail(&iter->node, &mv_chan->free_slots);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700266 }
267 return 0;
268}
269
270static int
Maxime Ripard0951e722015-05-26 15:07:33 +0200271mv_desc_clean_slot(struct mv_xor_desc_slot *desc,
272 struct mv_xor_chan *mv_chan)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700273{
Thomas Petazzonic98c1782012-11-15 14:17:18 +0100274 dev_dbg(mv_chan_to_devp(mv_chan), "%s %d: desc %p flags %d\n",
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700275 __func__, __LINE__, desc, desc->async_tx.flags);
Lior Amsalemfbea28a2015-05-26 15:07:36 +0200276
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700277 /* the client is allowed to attach dependent operations
278 * until 'ack' is set
279 */
Lior Amsalemfbea28a2015-05-26 15:07:36 +0200280 if (!async_tx_test_ack(&desc->async_tx))
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700281 /* move this slot to the completed_slots */
Lior Amsalemfbea28a2015-05-26 15:07:36 +0200282 list_move_tail(&desc->node, &mv_chan->completed_slots);
283 else
284 list_move_tail(&desc->node, &mv_chan->free_slots);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700285
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700286 return 0;
287}
288
Ezequiel Garciafbeec992014-03-07 16:46:47 -0300289/* This function must be called with the mv_xor_chan spinlock held */
Maxime Ripard0951e722015-05-26 15:07:33 +0200290static void mv_chan_slot_cleanup(struct mv_xor_chan *mv_chan)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700291{
292 struct mv_xor_desc_slot *iter, *_iter;
293 dma_cookie_t cookie = 0;
294 int busy = mv_chan_is_busy(mv_chan);
295 u32 current_desc = mv_chan_get_current_desc(mv_chan);
Lior Amsalem91362912015-05-26 15:07:32 +0200296 int current_cleaned = 0;
297 struct mv_xor_desc *hw_desc;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700298
Thomas Petazzonic98c1782012-11-15 14:17:18 +0100299 dev_dbg(mv_chan_to_devp(mv_chan), "%s %d\n", __func__, __LINE__);
300 dev_dbg(mv_chan_to_devp(mv_chan), "current_desc %x\n", current_desc);
Maxime Ripard0951e722015-05-26 15:07:33 +0200301 mv_chan_clean_completed_slots(mv_chan);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700302
303 /* free completed slots from the chain starting with
304 * the oldest descriptor
305 */
306
307 list_for_each_entry_safe(iter, _iter, &mv_chan->chain,
Lior Amsalemfbea28a2015-05-26 15:07:36 +0200308 node) {
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700309
Lior Amsalem91362912015-05-26 15:07:32 +0200310 /* clean finished descriptors */
311 hw_desc = iter->hw_desc;
312 if (hw_desc->status & XOR_DESC_SUCCESS) {
Maxime Ripard0951e722015-05-26 15:07:33 +0200313 cookie = mv_desc_run_tx_complete_actions(iter, mv_chan,
314 cookie);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700315
Lior Amsalem91362912015-05-26 15:07:32 +0200316 /* done processing desc, clean slot */
Maxime Ripard0951e722015-05-26 15:07:33 +0200317 mv_desc_clean_slot(iter, mv_chan);
Lior Amsalem91362912015-05-26 15:07:32 +0200318
319 /* break if we did cleaned the current */
320 if (iter->async_tx.phys == current_desc) {
321 current_cleaned = 1;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700322 break;
Lior Amsalem91362912015-05-26 15:07:32 +0200323 }
324 } else {
325 if (iter->async_tx.phys == current_desc) {
326 current_cleaned = 0;
327 break;
328 }
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700329 }
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700330 }
331
332 if ((busy == 0) && !list_empty(&mv_chan->chain)) {
Lior Amsalem91362912015-05-26 15:07:32 +0200333 if (current_cleaned) {
334 /*
335 * current descriptor cleaned and removed, run
336 * from list head
337 */
338 iter = list_entry(mv_chan->chain.next,
339 struct mv_xor_desc_slot,
Lior Amsalemfbea28a2015-05-26 15:07:36 +0200340 node);
Maxime Ripard0951e722015-05-26 15:07:33 +0200341 mv_chan_start_new_chain(mv_chan, iter);
Lior Amsalem91362912015-05-26 15:07:32 +0200342 } else {
Lior Amsalemfbea28a2015-05-26 15:07:36 +0200343 if (!list_is_last(&iter->node, &mv_chan->chain)) {
Lior Amsalem91362912015-05-26 15:07:32 +0200344 /*
345 * descriptors are still waiting after
346 * current, trigger them
347 */
Lior Amsalemfbea28a2015-05-26 15:07:36 +0200348 iter = list_entry(iter->node.next,
Lior Amsalem91362912015-05-26 15:07:32 +0200349 struct mv_xor_desc_slot,
Lior Amsalemfbea28a2015-05-26 15:07:36 +0200350 node);
Maxime Ripard0951e722015-05-26 15:07:33 +0200351 mv_chan_start_new_chain(mv_chan, iter);
Lior Amsalem91362912015-05-26 15:07:32 +0200352 } else {
353 /*
354 * some descriptors are still waiting
355 * to be cleaned
356 */
357 tasklet_schedule(&mv_chan->irq_tasklet);
358 }
359 }
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700360 }
361
362 if (cookie > 0)
Thomas Petazzoni98817b92012-11-15 14:57:44 +0100363 mv_chan->dmachan.completed_cookie = cookie;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700364}
365
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700366static void mv_xor_tasklet(unsigned long data)
367{
368 struct mv_xor_chan *chan = (struct mv_xor_chan *) data;
Ezequiel Garciae43147a2014-03-07 16:46:46 -0300369
370 spin_lock_bh(&chan->lock);
Maxime Ripard0951e722015-05-26 15:07:33 +0200371 mv_chan_slot_cleanup(chan);
Ezequiel Garciae43147a2014-03-07 16:46:46 -0300372 spin_unlock_bh(&chan->lock);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700373}
374
375static struct mv_xor_desc_slot *
Maxime Ripard0951e722015-05-26 15:07:33 +0200376mv_chan_alloc_slot(struct mv_xor_chan *mv_chan)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700377{
Lior Amsalemfbea28a2015-05-26 15:07:36 +0200378 struct mv_xor_desc_slot *iter;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700379
Lior Amsalemfbea28a2015-05-26 15:07:36 +0200380 spin_lock_bh(&mv_chan->lock);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700381
Lior Amsalemfbea28a2015-05-26 15:07:36 +0200382 if (!list_empty(&mv_chan->free_slots)) {
383 iter = list_first_entry(&mv_chan->free_slots,
384 struct mv_xor_desc_slot,
385 node);
Lior Amsalemdfc97662014-08-27 10:52:51 -0300386
Lior Amsalemfbea28a2015-05-26 15:07:36 +0200387 list_move_tail(&iter->node, &mv_chan->allocated_slots);
388
389 spin_unlock_bh(&mv_chan->lock);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700390
Lior Amsalemdfc97662014-08-27 10:52:51 -0300391 /* pre-ack descriptor */
392 async_tx_ack(&iter->async_tx);
Lior Amsalemdfc97662014-08-27 10:52:51 -0300393 iter->async_tx.cookie = -EBUSY;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700394
Lior Amsalemdfc97662014-08-27 10:52:51 -0300395 return iter;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700396
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700397 }
Lior Amsalemfbea28a2015-05-26 15:07:36 +0200398
399 spin_unlock_bh(&mv_chan->lock);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700400
401 /* try to free some slots if the allocation fails */
402 tasklet_schedule(&mv_chan->irq_tasklet);
403
404 return NULL;
405}
406
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700407/************************ DMA engine API functions ****************************/
408static dma_cookie_t
409mv_xor_tx_submit(struct dma_async_tx_descriptor *tx)
410{
411 struct mv_xor_desc_slot *sw_desc = to_mv_xor_slot(tx);
412 struct mv_xor_chan *mv_chan = to_mv_xor_chan(tx->chan);
Lior Amsalemdfc97662014-08-27 10:52:51 -0300413 struct mv_xor_desc_slot *old_chain_tail;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700414 dma_cookie_t cookie;
415 int new_hw_chain = 1;
416
Thomas Petazzonic98c1782012-11-15 14:17:18 +0100417 dev_dbg(mv_chan_to_devp(mv_chan),
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700418 "%s sw_desc %p: async_tx %p\n",
419 __func__, sw_desc, &sw_desc->async_tx);
420
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700421 spin_lock_bh(&mv_chan->lock);
Russell King - ARM Linux884485e2012-03-06 22:34:46 +0000422 cookie = dma_cookie_assign(tx);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700423
424 if (list_empty(&mv_chan->chain))
Lior Amsalemfbea28a2015-05-26 15:07:36 +0200425 list_move_tail(&sw_desc->node, &mv_chan->chain);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700426 else {
427 new_hw_chain = 0;
428
429 old_chain_tail = list_entry(mv_chan->chain.prev,
430 struct mv_xor_desc_slot,
Lior Amsalemfbea28a2015-05-26 15:07:36 +0200431 node);
432 list_move_tail(&sw_desc->node, &mv_chan->chain);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700433
Olof Johansson31fd8f52014-02-03 17:13:23 -0800434 dev_dbg(mv_chan_to_devp(mv_chan), "Append to last desc %pa\n",
435 &old_chain_tail->async_tx.phys);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700436
437 /* fix up the hardware chain */
Lior Amsalemdfc97662014-08-27 10:52:51 -0300438 mv_desc_set_next_desc(old_chain_tail, sw_desc->async_tx.phys);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700439
440 /* if the channel is not busy */
441 if (!mv_chan_is_busy(mv_chan)) {
442 u32 current_desc = mv_chan_get_current_desc(mv_chan);
443 /*
444 * and the curren desc is the end of the chain before
445 * the append, then we need to start the channel
446 */
447 if (current_desc == old_chain_tail->async_tx.phys)
448 new_hw_chain = 1;
449 }
450 }
451
452 if (new_hw_chain)
Maxime Ripard0951e722015-05-26 15:07:33 +0200453 mv_chan_start_new_chain(mv_chan, sw_desc);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700454
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700455 spin_unlock_bh(&mv_chan->lock);
456
457 return cookie;
458}
459
460/* returns the number of allocated descriptors */
Dan Williamsaa1e6f12009-01-06 11:38:17 -0700461static int mv_xor_alloc_chan_resources(struct dma_chan *chan)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700462{
Olof Johansson31fd8f52014-02-03 17:13:23 -0800463 void *virt_desc;
464 dma_addr_t dma_desc;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700465 int idx;
466 struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
467 struct mv_xor_desc_slot *slot = NULL;
Thomas Petazzonib503fa02012-11-15 15:55:30 +0100468 int num_descs_in_pool = MV_XOR_POOL_SIZE/MV_XOR_SLOT_SIZE;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700469
470 /* Allocate descriptor slots */
471 idx = mv_chan->slots_allocated;
472 while (idx < num_descs_in_pool) {
473 slot = kzalloc(sizeof(*slot), GFP_KERNEL);
474 if (!slot) {
Ezequiel Garciab8291dd2014-08-27 10:52:49 -0300475 dev_info(mv_chan_to_devp(mv_chan),
476 "channel only initialized %d descriptor slots",
477 idx);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700478 break;
479 }
Olof Johansson31fd8f52014-02-03 17:13:23 -0800480 virt_desc = mv_chan->dma_desc_pool_virt;
481 slot->hw_desc = virt_desc + idx * MV_XOR_SLOT_SIZE;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700482
483 dma_async_tx_descriptor_init(&slot->async_tx, chan);
484 slot->async_tx.tx_submit = mv_xor_tx_submit;
Lior Amsalemfbea28a2015-05-26 15:07:36 +0200485 INIT_LIST_HEAD(&slot->node);
Olof Johansson31fd8f52014-02-03 17:13:23 -0800486 dma_desc = mv_chan->dma_desc_pool;
487 slot->async_tx.phys = dma_desc + idx * MV_XOR_SLOT_SIZE;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700488 slot->idx = idx++;
489
490 spin_lock_bh(&mv_chan->lock);
491 mv_chan->slots_allocated = idx;
Lior Amsalemfbea28a2015-05-26 15:07:36 +0200492 list_add_tail(&slot->node, &mv_chan->free_slots);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700493 spin_unlock_bh(&mv_chan->lock);
494 }
495
Thomas Petazzonic98c1782012-11-15 14:17:18 +0100496 dev_dbg(mv_chan_to_devp(mv_chan),
Lior Amsalemfbea28a2015-05-26 15:07:36 +0200497 "allocated %d descriptor slots\n",
498 mv_chan->slots_allocated);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700499
500 return mv_chan->slots_allocated ? : -ENOMEM;
501}
502
503static struct dma_async_tx_descriptor *
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700504mv_xor_prep_dma_xor(struct dma_chan *chan, dma_addr_t dest, dma_addr_t *src,
505 unsigned int src_cnt, size_t len, unsigned long flags)
506{
507 struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
Lior Amsalemdfc97662014-08-27 10:52:51 -0300508 struct mv_xor_desc_slot *sw_desc;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700509
510 if (unlikely(len < MV_XOR_MIN_BYTE_COUNT))
511 return NULL;
512
Coly Li7912d302011-03-27 01:26:53 +0800513 BUG_ON(len > MV_XOR_MAX_BYTE_COUNT);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700514
Thomas Petazzonic98c1782012-11-15 14:17:18 +0100515 dev_dbg(mv_chan_to_devp(mv_chan),
Olof Johansson31fd8f52014-02-03 17:13:23 -0800516 "%s src_cnt: %d len: %u dest %pad flags: %ld\n",
517 __func__, src_cnt, len, &dest, flags);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700518
Maxime Ripard0951e722015-05-26 15:07:33 +0200519 sw_desc = mv_chan_alloc_slot(mv_chan);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700520 if (sw_desc) {
521 sw_desc->type = DMA_XOR;
522 sw_desc->async_tx.flags = flags;
Lior Amsalemba87d132014-08-27 10:52:53 -0300523 mv_desc_init(sw_desc, dest, len, flags);
Lior Amsalem6f166312015-05-26 15:07:34 +0200524 if (mv_chan->op_in_desc == XOR_MODE_IN_DESC)
525 mv_desc_set_mode(sw_desc);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700526 while (src_cnt--)
Lior Amsalemdfc97662014-08-27 10:52:51 -0300527 mv_desc_set_src_addr(sw_desc, src_cnt, src[src_cnt]);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700528 }
Lior Amsalemfbea28a2015-05-26 15:07:36 +0200529
Thomas Petazzonic98c1782012-11-15 14:17:18 +0100530 dev_dbg(mv_chan_to_devp(mv_chan),
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700531 "%s sw_desc %p async_tx %p \n",
532 __func__, sw_desc, &sw_desc->async_tx);
533 return sw_desc ? &sw_desc->async_tx : NULL;
534}
535
Lior Amsalem3e4f52e2014-08-27 10:52:50 -0300536static struct dma_async_tx_descriptor *
537mv_xor_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
538 size_t len, unsigned long flags)
539{
540 /*
541 * A MEMCPY operation is identical to an XOR operation with only
542 * a single source address.
543 */
544 return mv_xor_prep_dma_xor(chan, dest, &src, 1, len, flags);
545}
546
Lior Amsalem22843542014-08-27 10:52:55 -0300547static struct dma_async_tx_descriptor *
548mv_xor_prep_dma_interrupt(struct dma_chan *chan, unsigned long flags)
549{
550 struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
551 dma_addr_t src, dest;
552 size_t len;
553
554 src = mv_chan->dummy_src_addr;
555 dest = mv_chan->dummy_dst_addr;
556 len = MV_XOR_MIN_BYTE_COUNT;
557
558 /*
559 * We implement the DMA_INTERRUPT operation as a minimum sized
560 * XOR operation with a single dummy source address.
561 */
562 return mv_xor_prep_dma_xor(chan, dest, &src, 1, len, flags);
563}
564
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700565static void mv_xor_free_chan_resources(struct dma_chan *chan)
566{
567 struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
568 struct mv_xor_desc_slot *iter, *_iter;
569 int in_use_descs = 0;
570
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700571 spin_lock_bh(&mv_chan->lock);
Ezequiel Garciae43147a2014-03-07 16:46:46 -0300572
Maxime Ripard0951e722015-05-26 15:07:33 +0200573 mv_chan_slot_cleanup(mv_chan);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700574
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700575 list_for_each_entry_safe(iter, _iter, &mv_chan->chain,
Lior Amsalemfbea28a2015-05-26 15:07:36 +0200576 node) {
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700577 in_use_descs++;
Lior Amsalemfbea28a2015-05-26 15:07:36 +0200578 list_move_tail(&iter->node, &mv_chan->free_slots);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700579 }
580 list_for_each_entry_safe(iter, _iter, &mv_chan->completed_slots,
Lior Amsalemfbea28a2015-05-26 15:07:36 +0200581 node) {
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700582 in_use_descs++;
Lior Amsalemfbea28a2015-05-26 15:07:36 +0200583 list_move_tail(&iter->node, &mv_chan->free_slots);
584 }
585 list_for_each_entry_safe(iter, _iter, &mv_chan->allocated_slots,
586 node) {
587 in_use_descs++;
588 list_move_tail(&iter->node, &mv_chan->free_slots);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700589 }
590 list_for_each_entry_safe_reverse(
Lior Amsalemfbea28a2015-05-26 15:07:36 +0200591 iter, _iter, &mv_chan->free_slots, node) {
592 list_del(&iter->node);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700593 kfree(iter);
594 mv_chan->slots_allocated--;
595 }
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700596
Thomas Petazzonic98c1782012-11-15 14:17:18 +0100597 dev_dbg(mv_chan_to_devp(mv_chan), "%s slots_allocated %d\n",
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700598 __func__, mv_chan->slots_allocated);
599 spin_unlock_bh(&mv_chan->lock);
600
601 if (in_use_descs)
Thomas Petazzonic98c1782012-11-15 14:17:18 +0100602 dev_err(mv_chan_to_devp(mv_chan),
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700603 "freeing %d in use descriptors!\n", in_use_descs);
604}
605
606/**
Linus Walleij07934482010-03-26 16:50:49 -0700607 * mv_xor_status - poll the status of an XOR transaction
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700608 * @chan: XOR channel handle
609 * @cookie: XOR transaction identifier
Linus Walleij07934482010-03-26 16:50:49 -0700610 * @txstate: XOR transactions state holder (or NULL)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700611 */
Linus Walleij07934482010-03-26 16:50:49 -0700612static enum dma_status mv_xor_status(struct dma_chan *chan,
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700613 dma_cookie_t cookie,
Linus Walleij07934482010-03-26 16:50:49 -0700614 struct dma_tx_state *txstate)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700615{
616 struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700617 enum dma_status ret;
618
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +0000619 ret = dma_cookie_status(chan, cookie, txstate);
Ezequiel Garcia890766d2014-03-07 16:46:45 -0300620 if (ret == DMA_COMPLETE)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700621 return ret;
Ezequiel Garciae43147a2014-03-07 16:46:46 -0300622
623 spin_lock_bh(&mv_chan->lock);
Maxime Ripard0951e722015-05-26 15:07:33 +0200624 mv_chan_slot_cleanup(mv_chan);
Ezequiel Garciae43147a2014-03-07 16:46:46 -0300625 spin_unlock_bh(&mv_chan->lock);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700626
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +0000627 return dma_cookie_status(chan, cookie, txstate);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700628}
629
Maxime Ripard0951e722015-05-26 15:07:33 +0200630static void mv_chan_dump_regs(struct mv_xor_chan *chan)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700631{
632 u32 val;
633
Thomas Petazzoni5733c382013-07-29 17:42:13 +0200634 val = readl_relaxed(XOR_CONFIG(chan));
Joe Perches1ba151c2012-10-28 01:05:44 -0700635 dev_err(mv_chan_to_devp(chan), "config 0x%08x\n", val);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700636
Thomas Petazzoni5733c382013-07-29 17:42:13 +0200637 val = readl_relaxed(XOR_ACTIVATION(chan));
Joe Perches1ba151c2012-10-28 01:05:44 -0700638 dev_err(mv_chan_to_devp(chan), "activation 0x%08x\n", val);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700639
Thomas Petazzoni5733c382013-07-29 17:42:13 +0200640 val = readl_relaxed(XOR_INTR_CAUSE(chan));
Joe Perches1ba151c2012-10-28 01:05:44 -0700641 dev_err(mv_chan_to_devp(chan), "intr cause 0x%08x\n", val);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700642
Thomas Petazzoni5733c382013-07-29 17:42:13 +0200643 val = readl_relaxed(XOR_INTR_MASK(chan));
Joe Perches1ba151c2012-10-28 01:05:44 -0700644 dev_err(mv_chan_to_devp(chan), "intr mask 0x%08x\n", val);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700645
Thomas Petazzoni5733c382013-07-29 17:42:13 +0200646 val = readl_relaxed(XOR_ERROR_CAUSE(chan));
Joe Perches1ba151c2012-10-28 01:05:44 -0700647 dev_err(mv_chan_to_devp(chan), "error cause 0x%08x\n", val);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700648
Thomas Petazzoni5733c382013-07-29 17:42:13 +0200649 val = readl_relaxed(XOR_ERROR_ADDR(chan));
Joe Perches1ba151c2012-10-28 01:05:44 -0700650 dev_err(mv_chan_to_devp(chan), "error addr 0x%08x\n", val);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700651}
652
Maxime Ripard0951e722015-05-26 15:07:33 +0200653static void mv_chan_err_interrupt_handler(struct mv_xor_chan *chan,
654 u32 intr_cause)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700655{
Ezequiel Garcia0e7488e2014-08-27 10:52:52 -0300656 if (intr_cause & XOR_INT_ERR_DECODE) {
657 dev_dbg(mv_chan_to_devp(chan), "ignoring address decode error\n");
658 return;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700659 }
660
Ezequiel Garcia0e7488e2014-08-27 10:52:52 -0300661 dev_err(mv_chan_to_devp(chan), "error on chan %d. intr cause 0x%08x\n",
Thomas Petazzonia3fc74b2012-11-15 12:50:27 +0100662 chan->idx, intr_cause);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700663
Maxime Ripard0951e722015-05-26 15:07:33 +0200664 mv_chan_dump_regs(chan);
Ezequiel Garcia0e7488e2014-08-27 10:52:52 -0300665 WARN_ON(1);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700666}
667
668static irqreturn_t mv_xor_interrupt_handler(int irq, void *data)
669{
670 struct mv_xor_chan *chan = data;
671 u32 intr_cause = mv_chan_get_intr_cause(chan);
672
Thomas Petazzonic98c1782012-11-15 14:17:18 +0100673 dev_dbg(mv_chan_to_devp(chan), "intr cause %x\n", intr_cause);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700674
Ezequiel Garcia0e7488e2014-08-27 10:52:52 -0300675 if (intr_cause & XOR_INTR_ERRORS)
Maxime Ripard0951e722015-05-26 15:07:33 +0200676 mv_chan_err_interrupt_handler(chan, intr_cause);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700677
678 tasklet_schedule(&chan->irq_tasklet);
679
Maxime Ripard0951e722015-05-26 15:07:33 +0200680 mv_chan_clear_eoc_cause(chan);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700681
682 return IRQ_HANDLED;
683}
684
685static void mv_xor_issue_pending(struct dma_chan *chan)
686{
687 struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
688
689 if (mv_chan->pending >= MV_XOR_THRESHOLD) {
690 mv_chan->pending = 0;
691 mv_chan_activate(mv_chan);
692 }
693}
694
695/*
696 * Perform a transaction to verify the HW works.
697 */
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700698
Maxime Ripard0951e722015-05-26 15:07:33 +0200699static int mv_chan_memcpy_self_test(struct mv_xor_chan *mv_chan)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700700{
Ezequiel Garciab8c01d22013-12-10 09:32:37 -0300701 int i, ret;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700702 void *src, *dest;
703 dma_addr_t src_dma, dest_dma;
704 struct dma_chan *dma_chan;
705 dma_cookie_t cookie;
706 struct dma_async_tx_descriptor *tx;
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300707 struct dmaengine_unmap_data *unmap;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700708 int err = 0;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700709
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300710 src = kmalloc(sizeof(u8) * PAGE_SIZE, GFP_KERNEL);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700711 if (!src)
712 return -ENOMEM;
713
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300714 dest = kzalloc(sizeof(u8) * PAGE_SIZE, GFP_KERNEL);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700715 if (!dest) {
716 kfree(src);
717 return -ENOMEM;
718 }
719
720 /* Fill in src buffer */
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300721 for (i = 0; i < PAGE_SIZE; i++)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700722 ((u8 *) src)[i] = (u8)i;
723
Thomas Petazzoni275cc0c2012-11-15 15:09:42 +0100724 dma_chan = &mv_chan->dmachan;
Dan Williamsaa1e6f12009-01-06 11:38:17 -0700725 if (mv_xor_alloc_chan_resources(dma_chan) < 1) {
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700726 err = -ENODEV;
727 goto out;
728 }
729
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300730 unmap = dmaengine_get_unmap_data(dma_chan->device->dev, 2, GFP_KERNEL);
731 if (!unmap) {
732 err = -ENOMEM;
733 goto free_resources;
734 }
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700735
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300736 src_dma = dma_map_page(dma_chan->device->dev, virt_to_page(src), 0,
737 PAGE_SIZE, DMA_TO_DEVICE);
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300738 unmap->addr[0] = src_dma;
739
Ezequiel Garciab8c01d22013-12-10 09:32:37 -0300740 ret = dma_mapping_error(dma_chan->device->dev, src_dma);
741 if (ret) {
742 err = -ENOMEM;
743 goto free_resources;
744 }
745 unmap->to_cnt = 1;
746
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300747 dest_dma = dma_map_page(dma_chan->device->dev, virt_to_page(dest), 0,
748 PAGE_SIZE, DMA_FROM_DEVICE);
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300749 unmap->addr[1] = dest_dma;
750
Ezequiel Garciab8c01d22013-12-10 09:32:37 -0300751 ret = dma_mapping_error(dma_chan->device->dev, dest_dma);
752 if (ret) {
753 err = -ENOMEM;
754 goto free_resources;
755 }
756 unmap->from_cnt = 1;
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300757 unmap->len = PAGE_SIZE;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700758
759 tx = mv_xor_prep_dma_memcpy(dma_chan, dest_dma, src_dma,
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300760 PAGE_SIZE, 0);
Ezequiel Garciab8c01d22013-12-10 09:32:37 -0300761 if (!tx) {
762 dev_err(dma_chan->device->dev,
763 "Self-test cannot prepare operation, disabling\n");
764 err = -ENODEV;
765 goto free_resources;
766 }
767
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700768 cookie = mv_xor_tx_submit(tx);
Ezequiel Garciab8c01d22013-12-10 09:32:37 -0300769 if (dma_submit_error(cookie)) {
770 dev_err(dma_chan->device->dev,
771 "Self-test submit error, disabling\n");
772 err = -ENODEV;
773 goto free_resources;
774 }
775
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700776 mv_xor_issue_pending(dma_chan);
777 async_tx_ack(tx);
778 msleep(1);
779
Linus Walleij07934482010-03-26 16:50:49 -0700780 if (mv_xor_status(dma_chan, cookie, NULL) !=
Vinod Koulb3efb8f2013-10-16 20:51:04 +0530781 DMA_COMPLETE) {
Thomas Petazzonia3fc74b2012-11-15 12:50:27 +0100782 dev_err(dma_chan->device->dev,
783 "Self-test copy timed out, disabling\n");
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700784 err = -ENODEV;
785 goto free_resources;
786 }
787
Thomas Petazzonic35064c2012-11-15 13:01:59 +0100788 dma_sync_single_for_cpu(dma_chan->device->dev, dest_dma,
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300789 PAGE_SIZE, DMA_FROM_DEVICE);
790 if (memcmp(src, dest, PAGE_SIZE)) {
Thomas Petazzonia3fc74b2012-11-15 12:50:27 +0100791 dev_err(dma_chan->device->dev,
792 "Self-test copy failed compare, disabling\n");
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700793 err = -ENODEV;
794 goto free_resources;
795 }
796
797free_resources:
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300798 dmaengine_unmap_put(unmap);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700799 mv_xor_free_chan_resources(dma_chan);
800out:
801 kfree(src);
802 kfree(dest);
803 return err;
804}
805
806#define MV_XOR_NUM_SRC_TEST 4 /* must be <= 15 */
Bill Pemberton463a1f82012-11-19 13:22:55 -0500807static int
Maxime Ripard0951e722015-05-26 15:07:33 +0200808mv_chan_xor_self_test(struct mv_xor_chan *mv_chan)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700809{
Ezequiel Garciab8c01d22013-12-10 09:32:37 -0300810 int i, src_idx, ret;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700811 struct page *dest;
812 struct page *xor_srcs[MV_XOR_NUM_SRC_TEST];
813 dma_addr_t dma_srcs[MV_XOR_NUM_SRC_TEST];
814 dma_addr_t dest_dma;
815 struct dma_async_tx_descriptor *tx;
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300816 struct dmaengine_unmap_data *unmap;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700817 struct dma_chan *dma_chan;
818 dma_cookie_t cookie;
819 u8 cmp_byte = 0;
820 u32 cmp_word;
821 int err = 0;
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300822 int src_count = MV_XOR_NUM_SRC_TEST;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700823
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300824 for (src_idx = 0; src_idx < src_count; src_idx++) {
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700825 xor_srcs[src_idx] = alloc_page(GFP_KERNEL);
Roel Kluina09b09a2009-02-25 13:56:21 +0100826 if (!xor_srcs[src_idx]) {
827 while (src_idx--)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700828 __free_page(xor_srcs[src_idx]);
Roel Kluina09b09a2009-02-25 13:56:21 +0100829 return -ENOMEM;
830 }
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700831 }
832
833 dest = alloc_page(GFP_KERNEL);
Roel Kluina09b09a2009-02-25 13:56:21 +0100834 if (!dest) {
835 while (src_idx--)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700836 __free_page(xor_srcs[src_idx]);
Roel Kluina09b09a2009-02-25 13:56:21 +0100837 return -ENOMEM;
838 }
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700839
840 /* Fill in src buffers */
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300841 for (src_idx = 0; src_idx < src_count; src_idx++) {
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700842 u8 *ptr = page_address(xor_srcs[src_idx]);
843 for (i = 0; i < PAGE_SIZE; i++)
844 ptr[i] = (1 << src_idx);
845 }
846
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300847 for (src_idx = 0; src_idx < src_count; src_idx++)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700848 cmp_byte ^= (u8) (1 << src_idx);
849
850 cmp_word = (cmp_byte << 24) | (cmp_byte << 16) |
851 (cmp_byte << 8) | cmp_byte;
852
853 memset(page_address(dest), 0, PAGE_SIZE);
854
Thomas Petazzoni275cc0c2012-11-15 15:09:42 +0100855 dma_chan = &mv_chan->dmachan;
Dan Williamsaa1e6f12009-01-06 11:38:17 -0700856 if (mv_xor_alloc_chan_resources(dma_chan) < 1) {
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700857 err = -ENODEV;
858 goto out;
859 }
860
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300861 unmap = dmaengine_get_unmap_data(dma_chan->device->dev, src_count + 1,
862 GFP_KERNEL);
863 if (!unmap) {
864 err = -ENOMEM;
865 goto free_resources;
866 }
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700867
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300868 /* test xor */
869 for (i = 0; i < src_count; i++) {
870 unmap->addr[i] = dma_map_page(dma_chan->device->dev, xor_srcs[i],
871 0, PAGE_SIZE, DMA_TO_DEVICE);
872 dma_srcs[i] = unmap->addr[i];
Ezequiel Garciab8c01d22013-12-10 09:32:37 -0300873 ret = dma_mapping_error(dma_chan->device->dev, unmap->addr[i]);
874 if (ret) {
875 err = -ENOMEM;
876 goto free_resources;
877 }
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300878 unmap->to_cnt++;
879 }
880
881 unmap->addr[src_count] = dma_map_page(dma_chan->device->dev, dest, 0, PAGE_SIZE,
882 DMA_FROM_DEVICE);
883 dest_dma = unmap->addr[src_count];
Ezequiel Garciab8c01d22013-12-10 09:32:37 -0300884 ret = dma_mapping_error(dma_chan->device->dev, unmap->addr[src_count]);
885 if (ret) {
886 err = -ENOMEM;
887 goto free_resources;
888 }
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300889 unmap->from_cnt = 1;
890 unmap->len = PAGE_SIZE;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700891
892 tx = mv_xor_prep_dma_xor(dma_chan, dest_dma, dma_srcs,
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300893 src_count, PAGE_SIZE, 0);
Ezequiel Garciab8c01d22013-12-10 09:32:37 -0300894 if (!tx) {
895 dev_err(dma_chan->device->dev,
896 "Self-test cannot prepare operation, disabling\n");
897 err = -ENODEV;
898 goto free_resources;
899 }
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700900
901 cookie = mv_xor_tx_submit(tx);
Ezequiel Garciab8c01d22013-12-10 09:32:37 -0300902 if (dma_submit_error(cookie)) {
903 dev_err(dma_chan->device->dev,
904 "Self-test submit error, disabling\n");
905 err = -ENODEV;
906 goto free_resources;
907 }
908
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700909 mv_xor_issue_pending(dma_chan);
910 async_tx_ack(tx);
911 msleep(8);
912
Linus Walleij07934482010-03-26 16:50:49 -0700913 if (mv_xor_status(dma_chan, cookie, NULL) !=
Vinod Koulb3efb8f2013-10-16 20:51:04 +0530914 DMA_COMPLETE) {
Thomas Petazzonia3fc74b2012-11-15 12:50:27 +0100915 dev_err(dma_chan->device->dev,
916 "Self-test xor timed out, disabling\n");
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700917 err = -ENODEV;
918 goto free_resources;
919 }
920
Thomas Petazzonic35064c2012-11-15 13:01:59 +0100921 dma_sync_single_for_cpu(dma_chan->device->dev, dest_dma,
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700922 PAGE_SIZE, DMA_FROM_DEVICE);
923 for (i = 0; i < (PAGE_SIZE / sizeof(u32)); i++) {
924 u32 *ptr = page_address(dest);
925 if (ptr[i] != cmp_word) {
Thomas Petazzonia3fc74b2012-11-15 12:50:27 +0100926 dev_err(dma_chan->device->dev,
Joe Perches1ba151c2012-10-28 01:05:44 -0700927 "Self-test xor failed compare, disabling. index %d, data %x, expected %x\n",
928 i, ptr[i], cmp_word);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700929 err = -ENODEV;
930 goto free_resources;
931 }
932 }
933
934free_resources:
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300935 dmaengine_unmap_put(unmap);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700936 mv_xor_free_chan_resources(dma_chan);
937out:
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300938 src_idx = src_count;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700939 while (src_idx--)
940 __free_page(xor_srcs[src_idx]);
941 __free_page(dest);
942 return err;
943}
944
Thomas Petazzoni1ef48a22012-11-15 15:17:05 +0100945static int mv_xor_channel_remove(struct mv_xor_chan *mv_chan)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700946{
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700947 struct dma_chan *chan, *_chan;
Thomas Petazzoni1ef48a22012-11-15 15:17:05 +0100948 struct device *dev = mv_chan->dmadev.dev;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700949
Thomas Petazzoni1ef48a22012-11-15 15:17:05 +0100950 dma_async_device_unregister(&mv_chan->dmadev);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700951
Thomas Petazzonib503fa02012-11-15 15:55:30 +0100952 dma_free_coherent(dev, MV_XOR_POOL_SIZE,
Thomas Petazzoni1ef48a22012-11-15 15:17:05 +0100953 mv_chan->dma_desc_pool_virt, mv_chan->dma_desc_pool);
Lior Amsalem22843542014-08-27 10:52:55 -0300954 dma_unmap_single(dev, mv_chan->dummy_src_addr,
955 MV_XOR_MIN_BYTE_COUNT, DMA_FROM_DEVICE);
956 dma_unmap_single(dev, mv_chan->dummy_dst_addr,
957 MV_XOR_MIN_BYTE_COUNT, DMA_TO_DEVICE);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700958
Thomas Petazzoni1ef48a22012-11-15 15:17:05 +0100959 list_for_each_entry_safe(chan, _chan, &mv_chan->dmadev.channels,
Thomas Petazzonia6b4a9d2012-10-29 16:45:46 +0100960 device_node) {
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700961 list_del(&chan->device_node);
962 }
963
Thomas Petazzoni88eb92c2012-11-15 16:11:18 +0100964 free_irq(mv_chan->irq, mv_chan);
965
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700966 return 0;
967}
968
Thomas Petazzoni1ef48a22012-11-15 15:17:05 +0100969static struct mv_xor_chan *
Thomas Petazzoni297eedb2012-11-15 15:29:53 +0100970mv_xor_channel_add(struct mv_xor_device *xordev,
Thomas Petazzonia6b4a9d2012-10-29 16:45:46 +0100971 struct platform_device *pdev,
Lior Amsalem6f166312015-05-26 15:07:34 +0200972 int idx, dma_cap_mask_t cap_mask, int irq, int op_in_desc)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700973{
974 int ret = 0;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700975 struct mv_xor_chan *mv_chan;
976 struct dma_device *dma_dev;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700977
Thomas Petazzoni1ef48a22012-11-15 15:17:05 +0100978 mv_chan = devm_kzalloc(&pdev->dev, sizeof(*mv_chan), GFP_KERNEL);
Sachin Kamata5776592013-09-02 13:54:20 +0530979 if (!mv_chan)
980 return ERR_PTR(-ENOMEM);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700981
Thomas Petazzoni9aedbdb2012-11-15 15:36:37 +0100982 mv_chan->idx = idx;
Thomas Petazzoni88eb92c2012-11-15 16:11:18 +0100983 mv_chan->irq = irq;
Lior Amsalem6f166312015-05-26 15:07:34 +0200984 mv_chan->op_in_desc = op_in_desc;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700985
Thomas Petazzoni1ef48a22012-11-15 15:17:05 +0100986 dma_dev = &mv_chan->dmadev;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700987
Lior Amsalem22843542014-08-27 10:52:55 -0300988 /*
989 * These source and destination dummy buffers are used to implement
990 * a DMA_INTERRUPT operation as a minimum-sized XOR operation.
991 * Hence, we only need to map the buffers at initialization-time.
992 */
993 mv_chan->dummy_src_addr = dma_map_single(dma_dev->dev,
994 mv_chan->dummy_src, MV_XOR_MIN_BYTE_COUNT, DMA_FROM_DEVICE);
995 mv_chan->dummy_dst_addr = dma_map_single(dma_dev->dev,
996 mv_chan->dummy_dst, MV_XOR_MIN_BYTE_COUNT, DMA_TO_DEVICE);
997
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700998 /* allocate coherent memory for hardware descriptors
999 * note: writecombine gives slightly better performance, but
1000 * requires that we explicitly flush the writes
1001 */
Thomas Petazzoni1ef48a22012-11-15 15:17:05 +01001002 mv_chan->dma_desc_pool_virt =
Thomas Petazzonib503fa02012-11-15 15:55:30 +01001003 dma_alloc_writecombine(&pdev->dev, MV_XOR_POOL_SIZE,
Thomas Petazzoni1ef48a22012-11-15 15:17:05 +01001004 &mv_chan->dma_desc_pool, GFP_KERNEL);
1005 if (!mv_chan->dma_desc_pool_virt)
Thomas Petazzonia6b4a9d2012-10-29 16:45:46 +01001006 return ERR_PTR(-ENOMEM);
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001007
1008 /* discover transaction capabilites from the platform data */
Thomas Petazzonia6b4a9d2012-10-29 16:45:46 +01001009 dma_dev->cap_mask = cap_mask;
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001010
1011 INIT_LIST_HEAD(&dma_dev->channels);
1012
1013 /* set base routines */
1014 dma_dev->device_alloc_chan_resources = mv_xor_alloc_chan_resources;
1015 dma_dev->device_free_chan_resources = mv_xor_free_chan_resources;
Linus Walleij07934482010-03-26 16:50:49 -07001016 dma_dev->device_tx_status = mv_xor_status;
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001017 dma_dev->device_issue_pending = mv_xor_issue_pending;
1018 dma_dev->dev = &pdev->dev;
1019
1020 /* set prep routines based on capability */
Lior Amsalem22843542014-08-27 10:52:55 -03001021 if (dma_has_cap(DMA_INTERRUPT, dma_dev->cap_mask))
1022 dma_dev->device_prep_dma_interrupt = mv_xor_prep_dma_interrupt;
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001023 if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask))
1024 dma_dev->device_prep_dma_memcpy = mv_xor_prep_dma_memcpy;
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001025 if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
Joe Perchesc0198942009-06-28 09:26:21 -07001026 dma_dev->max_xor = 8;
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001027 dma_dev->device_prep_dma_xor = mv_xor_prep_dma_xor;
1028 }
1029
Thomas Petazzoni297eedb2012-11-15 15:29:53 +01001030 mv_chan->mmr_base = xordev->xor_base;
Ezequiel Garcia82a14022013-10-30 12:01:43 -03001031 mv_chan->mmr_high_base = xordev->xor_high_base;
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001032 tasklet_init(&mv_chan->irq_tasklet, mv_xor_tasklet, (unsigned long)
1033 mv_chan);
1034
1035 /* clear errors before enabling interrupts */
Maxime Ripard0951e722015-05-26 15:07:33 +02001036 mv_chan_clear_err_status(mv_chan);
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001037
Thomas Petazzoni2d0a0742012-11-22 18:19:09 +01001038 ret = request_irq(mv_chan->irq, mv_xor_interrupt_handler,
1039 0, dev_name(&pdev->dev), mv_chan);
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001040 if (ret)
1041 goto err_free_dma;
1042
1043 mv_chan_unmask_interrupts(mv_chan);
1044
Lior Amsalem6f166312015-05-26 15:07:34 +02001045 if (mv_chan->op_in_desc == XOR_MODE_IN_DESC)
1046 mv_chan_set_mode_to_desc(mv_chan);
1047 else
1048 mv_chan_set_mode(mv_chan, DMA_XOR);
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001049
1050 spin_lock_init(&mv_chan->lock);
1051 INIT_LIST_HEAD(&mv_chan->chain);
1052 INIT_LIST_HEAD(&mv_chan->completed_slots);
Lior Amsalemfbea28a2015-05-26 15:07:36 +02001053 INIT_LIST_HEAD(&mv_chan->free_slots);
1054 INIT_LIST_HEAD(&mv_chan->allocated_slots);
Thomas Petazzoni98817b92012-11-15 14:57:44 +01001055 mv_chan->dmachan.device = dma_dev;
1056 dma_cookie_init(&mv_chan->dmachan);
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001057
Thomas Petazzoni98817b92012-11-15 14:57:44 +01001058 list_add_tail(&mv_chan->dmachan.device_node, &dma_dev->channels);
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001059
1060 if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask)) {
Maxime Ripard0951e722015-05-26 15:07:33 +02001061 ret = mv_chan_memcpy_self_test(mv_chan);
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001062 dev_dbg(&pdev->dev, "memcpy self test returned %d\n", ret);
1063 if (ret)
Thomas Petazzoni2d0a0742012-11-22 18:19:09 +01001064 goto err_free_irq;
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001065 }
1066
1067 if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
Maxime Ripard0951e722015-05-26 15:07:33 +02001068 ret = mv_chan_xor_self_test(mv_chan);
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001069 dev_dbg(&pdev->dev, "xor self test returned %d\n", ret);
1070 if (ret)
Thomas Petazzoni2d0a0742012-11-22 18:19:09 +01001071 goto err_free_irq;
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001072 }
1073
Lior Amsalem6f166312015-05-26 15:07:34 +02001074 dev_info(&pdev->dev, "Marvell XOR (%s): ( %s%s%s)\n",
1075 mv_chan->op_in_desc ? "Descriptor Mode" : "Registers Mode",
Joe Perches1ba151c2012-10-28 01:05:44 -07001076 dma_has_cap(DMA_XOR, dma_dev->cap_mask) ? "xor " : "",
Joe Perches1ba151c2012-10-28 01:05:44 -07001077 dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask) ? "cpy " : "",
1078 dma_has_cap(DMA_INTERRUPT, dma_dev->cap_mask) ? "intr " : "");
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001079
1080 dma_async_device_register(dma_dev);
Thomas Petazzoni1ef48a22012-11-15 15:17:05 +01001081 return mv_chan;
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001082
Thomas Petazzoni2d0a0742012-11-22 18:19:09 +01001083err_free_irq:
1084 free_irq(mv_chan->irq, mv_chan);
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001085 err_free_dma:
Thomas Petazzonib503fa02012-11-15 15:55:30 +01001086 dma_free_coherent(&pdev->dev, MV_XOR_POOL_SIZE,
Thomas Petazzoni1ef48a22012-11-15 15:17:05 +01001087 mv_chan->dma_desc_pool_virt, mv_chan->dma_desc_pool);
Thomas Petazzonia6b4a9d2012-10-29 16:45:46 +01001088 return ERR_PTR(ret);
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001089}
1090
1091static void
Thomas Petazzoni297eedb2012-11-15 15:29:53 +01001092mv_xor_conf_mbus_windows(struct mv_xor_device *xordev,
Andrew Lunn63a93322011-12-07 21:48:07 +01001093 const struct mbus_dram_target_info *dram)
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001094{
Ezequiel Garcia82a14022013-10-30 12:01:43 -03001095 void __iomem *base = xordev->xor_high_base;
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001096 u32 win_enable = 0;
1097 int i;
1098
1099 for (i = 0; i < 8; i++) {
1100 writel(0, base + WINDOW_BASE(i));
1101 writel(0, base + WINDOW_SIZE(i));
1102 if (i < 4)
1103 writel(0, base + WINDOW_REMAP_HIGH(i));
1104 }
1105
1106 for (i = 0; i < dram->num_cs; i++) {
Andrew Lunn63a93322011-12-07 21:48:07 +01001107 const struct mbus_dram_window *cs = dram->cs + i;
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001108
1109 writel((cs->base & 0xffff0000) |
1110 (cs->mbus_attr << 8) |
1111 dram->mbus_dram_target_id, base + WINDOW_BASE(i));
1112 writel((cs->size - 1) & 0xffff0000, base + WINDOW_SIZE(i));
1113
1114 win_enable |= (1 << i);
1115 win_enable |= 3 << (16 + (2 * i));
1116 }
1117
1118 writel(win_enable, base + WINDOW_BAR_ENABLE(0));
1119 writel(win_enable, base + WINDOW_BAR_ENABLE(1));
Thomas Petazzonic4b4b732012-11-22 18:16:37 +01001120 writel(0, base + WINDOW_OVERRIDE_CTRL(0));
1121 writel(0, base + WINDOW_OVERRIDE_CTRL(1));
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001122}
1123
Lior Amsalem6f166312015-05-26 15:07:34 +02001124static const struct of_device_id mv_xor_dt_ids[] = {
1125 { .compatible = "marvell,orion-xor", .data = (void *)XOR_MODE_IN_REG },
1126 { .compatible = "marvell,armada-380-xor", .data = (void *)XOR_MODE_IN_DESC },
1127 {},
1128};
1129MODULE_DEVICE_TABLE(of, mv_xor_dt_ids);
1130
Thomas Petazzoni77757292015-07-08 16:28:19 +02001131static unsigned int mv_xor_engine_count;
1132
Linus Torvaldsc2714332012-12-14 14:54:26 -08001133static int mv_xor_probe(struct platform_device *pdev)
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001134{
Andrew Lunn63a93322011-12-07 21:48:07 +01001135 const struct mbus_dram_target_info *dram;
Thomas Petazzoni297eedb2012-11-15 15:29:53 +01001136 struct mv_xor_device *xordev;
Jingoo Hand4adcc02013-07-30 17:09:11 +09001137 struct mv_xor_platform_data *pdata = dev_get_platdata(&pdev->dev);
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001138 struct resource *res;
Thomas Petazzoni77757292015-07-08 16:28:19 +02001139 unsigned int max_engines, max_channels;
Thomas Petazzoni60d151f2012-10-29 16:54:49 +01001140 int i, ret;
Lior Amsalem6f166312015-05-26 15:07:34 +02001141 int op_in_desc;
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001142
Joe Perches1ba151c2012-10-28 01:05:44 -07001143 dev_notice(&pdev->dev, "Marvell shared XOR driver\n");
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001144
Thomas Petazzoni297eedb2012-11-15 15:29:53 +01001145 xordev = devm_kzalloc(&pdev->dev, sizeof(*xordev), GFP_KERNEL);
1146 if (!xordev)
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001147 return -ENOMEM;
1148
1149 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1150 if (!res)
1151 return -ENODEV;
1152
Thomas Petazzoni297eedb2012-11-15 15:29:53 +01001153 xordev->xor_base = devm_ioremap(&pdev->dev, res->start,
1154 resource_size(res));
1155 if (!xordev->xor_base)
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001156 return -EBUSY;
1157
1158 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1159 if (!res)
1160 return -ENODEV;
1161
Thomas Petazzoni297eedb2012-11-15 15:29:53 +01001162 xordev->xor_high_base = devm_ioremap(&pdev->dev, res->start,
1163 resource_size(res));
1164 if (!xordev->xor_high_base)
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001165 return -EBUSY;
1166
Thomas Petazzoni297eedb2012-11-15 15:29:53 +01001167 platform_set_drvdata(pdev, xordev);
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001168
1169 /*
1170 * (Re-)program MBUS remapping windows if we are asked to.
1171 */
Andrew Lunn63a93322011-12-07 21:48:07 +01001172 dram = mv_mbus_dram_info();
1173 if (dram)
Thomas Petazzoni297eedb2012-11-15 15:29:53 +01001174 mv_xor_conf_mbus_windows(xordev, dram);
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001175
Andrew Lunnc5101822012-02-19 13:30:26 +01001176 /* Not all platforms can gate the clock, so it is not
1177 * an error if the clock does not exists.
1178 */
Thomas Petazzoni297eedb2012-11-15 15:29:53 +01001179 xordev->clk = clk_get(&pdev->dev, NULL);
1180 if (!IS_ERR(xordev->clk))
1181 clk_prepare_enable(xordev->clk);
Andrew Lunnc5101822012-02-19 13:30:26 +01001182
Thomas Petazzoni77757292015-07-08 16:28:19 +02001183 /*
1184 * We don't want to have more than one channel per CPU in
1185 * order for async_tx to perform well. So we limit the number
1186 * of engines and channels so that we take into account this
1187 * constraint. Note that we also want to use channels from
1188 * separate engines when possible.
1189 */
1190 max_engines = num_present_cpus();
1191 max_channels = min_t(unsigned int,
1192 MV_XOR_MAX_CHANNELS,
1193 DIV_ROUND_UP(num_present_cpus(), 2));
1194
1195 if (mv_xor_engine_count >= max_engines)
1196 return 0;
1197
Thomas Petazzonif7d12ef2012-11-15 16:47:58 +01001198 if (pdev->dev.of_node) {
1199 struct device_node *np;
1200 int i = 0;
Lior Amsalem6f166312015-05-26 15:07:34 +02001201 const struct of_device_id *of_id =
1202 of_match_device(mv_xor_dt_ids,
1203 &pdev->dev);
Thomas Petazzonif7d12ef2012-11-15 16:47:58 +01001204
1205 for_each_child_of_node(pdev->dev.of_node, np) {
Russell King0be82532013-12-12 23:59:08 +00001206 struct mv_xor_chan *chan;
Thomas Petazzonif7d12ef2012-11-15 16:47:58 +01001207 dma_cap_mask_t cap_mask;
1208 int irq;
Lior Amsalem6f166312015-05-26 15:07:34 +02001209 op_in_desc = (int)of_id->data;
Thomas Petazzonif7d12ef2012-11-15 16:47:58 +01001210
Thomas Petazzoni77757292015-07-08 16:28:19 +02001211 if (i >= max_channels)
1212 continue;
1213
Thomas Petazzonif7d12ef2012-11-15 16:47:58 +01001214 dma_cap_zero(cap_mask);
Thomas Petazzoni6d8f7ab2015-07-08 16:28:16 +02001215 dma_cap_set(DMA_MEMCPY, cap_mask);
1216 dma_cap_set(DMA_XOR, cap_mask);
1217 dma_cap_set(DMA_INTERRUPT, cap_mask);
Thomas Petazzonif7d12ef2012-11-15 16:47:58 +01001218
1219 irq = irq_of_parse_and_map(np, 0);
Thomas Petazzonif8eb9e72012-11-22 18:22:12 +01001220 if (!irq) {
1221 ret = -ENODEV;
Thomas Petazzonif7d12ef2012-11-15 16:47:58 +01001222 goto err_channel_add;
1223 }
1224
Russell King0be82532013-12-12 23:59:08 +00001225 chan = mv_xor_channel_add(xordev, pdev, i,
Lior Amsalem6f166312015-05-26 15:07:34 +02001226 cap_mask, irq, op_in_desc);
Russell King0be82532013-12-12 23:59:08 +00001227 if (IS_ERR(chan)) {
1228 ret = PTR_ERR(chan);
Thomas Petazzonif7d12ef2012-11-15 16:47:58 +01001229 irq_dispose_mapping(irq);
1230 goto err_channel_add;
1231 }
1232
Russell King0be82532013-12-12 23:59:08 +00001233 xordev->channels[i] = chan;
Thomas Petazzonif7d12ef2012-11-15 16:47:58 +01001234 i++;
1235 }
1236 } else if (pdata && pdata->channels) {
Thomas Petazzoni77757292015-07-08 16:28:19 +02001237 for (i = 0; i < max_channels; i++) {
Thomas Petazzonie39f6ec2012-10-30 11:56:26 +01001238 struct mv_xor_channel_data *cd;
Russell King0be82532013-12-12 23:59:08 +00001239 struct mv_xor_chan *chan;
Thomas Petazzoni60d151f2012-10-29 16:54:49 +01001240 int irq;
1241
1242 cd = &pdata->channels[i];
1243 if (!cd) {
1244 ret = -ENODEV;
1245 goto err_channel_add;
1246 }
1247
1248 irq = platform_get_irq(pdev, i);
1249 if (irq < 0) {
1250 ret = irq;
1251 goto err_channel_add;
1252 }
1253
Russell King0be82532013-12-12 23:59:08 +00001254 chan = mv_xor_channel_add(xordev, pdev, i,
Lior Amsalem6f166312015-05-26 15:07:34 +02001255 cd->cap_mask, irq,
1256 XOR_MODE_IN_REG);
Russell King0be82532013-12-12 23:59:08 +00001257 if (IS_ERR(chan)) {
1258 ret = PTR_ERR(chan);
Thomas Petazzoni60d151f2012-10-29 16:54:49 +01001259 goto err_channel_add;
1260 }
Russell King0be82532013-12-12 23:59:08 +00001261
1262 xordev->channels[i] = chan;
Thomas Petazzoni60d151f2012-10-29 16:54:49 +01001263 }
1264 }
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001265
1266 return 0;
Thomas Petazzoni60d151f2012-10-29 16:54:49 +01001267
1268err_channel_add:
1269 for (i = 0; i < MV_XOR_MAX_CHANNELS; i++)
Thomas Petazzonif7d12ef2012-11-15 16:47:58 +01001270 if (xordev->channels[i]) {
Thomas Petazzoniab6e4392013-01-06 11:10:43 +01001271 mv_xor_channel_remove(xordev->channels[i]);
Thomas Petazzonif7d12ef2012-11-15 16:47:58 +01001272 if (pdev->dev.of_node)
1273 irq_dispose_mapping(xordev->channels[i]->irq);
Thomas Petazzonif7d12ef2012-11-15 16:47:58 +01001274 }
Thomas Petazzoni60d151f2012-10-29 16:54:49 +01001275
Thomas Petazzonidab92062013-01-06 11:10:44 +01001276 if (!IS_ERR(xordev->clk)) {
1277 clk_disable_unprepare(xordev->clk);
1278 clk_put(xordev->clk);
1279 }
1280
Thomas Petazzoni60d151f2012-10-29 16:54:49 +01001281 return ret;
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001282}
1283
Linus Torvaldsc2714332012-12-14 14:54:26 -08001284static int mv_xor_remove(struct platform_device *pdev)
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001285{
Thomas Petazzoni297eedb2012-11-15 15:29:53 +01001286 struct mv_xor_device *xordev = platform_get_drvdata(pdev);
Thomas Petazzoni60d151f2012-10-29 16:54:49 +01001287 int i;
Andrew Lunnc5101822012-02-19 13:30:26 +01001288
Thomas Petazzoni60d151f2012-10-29 16:54:49 +01001289 for (i = 0; i < MV_XOR_MAX_CHANNELS; i++) {
Thomas Petazzoni297eedb2012-11-15 15:29:53 +01001290 if (xordev->channels[i])
1291 mv_xor_channel_remove(xordev->channels[i]);
Thomas Petazzoni60d151f2012-10-29 16:54:49 +01001292 }
Andrew Lunnc5101822012-02-19 13:30:26 +01001293
Thomas Petazzoni297eedb2012-11-15 15:29:53 +01001294 if (!IS_ERR(xordev->clk)) {
1295 clk_disable_unprepare(xordev->clk);
1296 clk_put(xordev->clk);
Andrew Lunnc5101822012-02-19 13:30:26 +01001297 }
1298
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001299 return 0;
1300}
1301
Thomas Petazzoni61971652012-10-30 12:05:40 +01001302static struct platform_driver mv_xor_driver = {
1303 .probe = mv_xor_probe,
Linus Torvaldsc2714332012-12-14 14:54:26 -08001304 .remove = mv_xor_remove,
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001305 .driver = {
Thomas Petazzonif7d12ef2012-11-15 16:47:58 +01001306 .name = MV_XOR_NAME,
1307 .of_match_table = of_match_ptr(mv_xor_dt_ids),
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001308 },
1309};
1310
1311
1312static int __init mv_xor_init(void)
1313{
Thomas Petazzoni61971652012-10-30 12:05:40 +01001314 return platform_driver_register(&mv_xor_driver);
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001315}
1316module_init(mv_xor_init);
1317
1318/* it's currently unsafe to unload this module */
1319#if 0
1320static void __exit mv_xor_exit(void)
1321{
1322 platform_driver_unregister(&mv_xor_driver);
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001323 return;
1324}
1325
1326module_exit(mv_xor_exit);
1327#endif
1328
1329MODULE_AUTHOR("Saeed Bishara <saeed@marvell.com>");
1330MODULE_DESCRIPTION("DMA engine driver for Marvell's XOR engine");
1331MODULE_LICENSE("GPL");