blob: 6d012a56b97b822640b794d56c2d2eafd633285e [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Saeed Bisharaff7b0472008-07-08 11:58:36 -070017#include <linux/delay.h>
18#include <linux/dma-mapping.h>
19#include <linux/spinlock.h>
20#include <linux/interrupt.h>
Lior Amsalem6f166312015-05-26 15:07:34 +020021#include <linux/of_device.h>
Saeed Bisharaff7b0472008-07-08 11:58:36 -070022#include <linux/platform_device.h>
23#include <linux/memory.h>
Andrew Lunnc5101822012-02-19 13:30:26 +010024#include <linux/clk.h>
Thomas Petazzonif7d12ef2012-11-15 16:47:58 +010025#include <linux/of.h>
26#include <linux/of_irq.h>
27#include <linux/irqdomain.h>
Thomas Petazzoni77757292015-07-08 16:28:19 +020028#include <linux/cpumask.h>
Arnd Bergmannc02cecb2012-08-24 15:21:54 +020029#include <linux/platform_data/dma-mv_xor.h>
Russell King - ARM Linuxd2ebfb32012-03-06 22:34:26 +000030
31#include "dmaengine.h"
Saeed Bisharaff7b0472008-07-08 11:58:36 -070032#include "mv_xor.h"
33
Gregory CLEMENTdd130c62016-04-29 09:49:06 +020034enum mv_xor_type {
35 XOR_ORION,
36 XOR_ARMADA_38X,
37};
38
Lior Amsalem6f166312015-05-26 15:07:34 +020039enum mv_xor_mode {
40 XOR_MODE_IN_REG,
41 XOR_MODE_IN_DESC,
42};
43
Saeed Bisharaff7b0472008-07-08 11:58:36 -070044static void mv_xor_issue_pending(struct dma_chan *chan);
45
46#define to_mv_xor_chan(chan) \
Thomas Petazzoni98817b92012-11-15 14:57:44 +010047 container_of(chan, struct mv_xor_chan, dmachan)
Saeed Bisharaff7b0472008-07-08 11:58:36 -070048
49#define to_mv_xor_slot(tx) \
50 container_of(tx, struct mv_xor_desc_slot, async_tx)
51
Thomas Petazzonic98c1782012-11-15 14:17:18 +010052#define mv_chan_to_devp(chan) \
Thomas Petazzoni1ef48a22012-11-15 15:17:05 +010053 ((chan)->dmadev.dev)
Thomas Petazzonic98c1782012-11-15 14:17:18 +010054
Lior Amsalemdfc97662014-08-27 10:52:51 -030055static void mv_desc_init(struct mv_xor_desc_slot *desc,
Lior Amsalemba87d132014-08-27 10:52:53 -030056 dma_addr_t addr, u32 byte_count,
57 enum dma_ctrl_flags flags)
Saeed Bisharaff7b0472008-07-08 11:58:36 -070058{
59 struct mv_xor_desc *hw_desc = desc->hw_desc;
60
Ezequiel Garcia0e7488e2014-08-27 10:52:52 -030061 hw_desc->status = XOR_DESC_DMA_OWNED;
Saeed Bisharaff7b0472008-07-08 11:58:36 -070062 hw_desc->phy_next_desc = 0;
Lior Amsalemba87d132014-08-27 10:52:53 -030063 /* Enable end-of-descriptor interrupts only for DMA_PREP_INTERRUPT */
64 hw_desc->desc_command = (flags & DMA_PREP_INTERRUPT) ?
65 XOR_DESC_EOD_INT_EN : 0;
Lior Amsalemdfc97662014-08-27 10:52:51 -030066 hw_desc->phy_dest_addr = addr;
Saeed Bisharaff7b0472008-07-08 11:58:36 -070067 hw_desc->byte_count = byte_count;
68}
69
Lior Amsalem6f166312015-05-26 15:07:34 +020070static void mv_desc_set_mode(struct mv_xor_desc_slot *desc)
71{
72 struct mv_xor_desc *hw_desc = desc->hw_desc;
73
74 switch (desc->type) {
75 case DMA_XOR:
76 case DMA_INTERRUPT:
77 hw_desc->desc_command |= XOR_DESC_OPERATION_XOR;
78 break;
79 case DMA_MEMCPY:
80 hw_desc->desc_command |= XOR_DESC_OPERATION_MEMCPY;
81 break;
82 default:
83 BUG();
84 return;
85 }
86}
87
Saeed Bisharaff7b0472008-07-08 11:58:36 -070088static void mv_desc_set_next_desc(struct mv_xor_desc_slot *desc,
89 u32 next_desc_addr)
90{
91 struct mv_xor_desc *hw_desc = desc->hw_desc;
92 BUG_ON(hw_desc->phy_next_desc);
93 hw_desc->phy_next_desc = next_desc_addr;
94}
95
Saeed Bisharaff7b0472008-07-08 11:58:36 -070096static void mv_desc_set_src_addr(struct mv_xor_desc_slot *desc,
97 int index, dma_addr_t addr)
98{
99 struct mv_xor_desc *hw_desc = desc->hw_desc;
Thomas Petazzonie03bc652013-07-29 17:42:14 +0200100 hw_desc->phy_src_addr[mv_phy_src_idx(index)] = addr;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700101 if (desc->type == DMA_XOR)
102 hw_desc->desc_command |= (1 << index);
103}
104
105static u32 mv_chan_get_current_desc(struct mv_xor_chan *chan)
106{
Thomas Petazzoni5733c382013-07-29 17:42:13 +0200107 return readl_relaxed(XOR_CURR_DESC(chan));
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700108}
109
110static void mv_chan_set_next_descriptor(struct mv_xor_chan *chan,
111 u32 next_desc_addr)
112{
Thomas Petazzoni5733c382013-07-29 17:42:13 +0200113 writel_relaxed(next_desc_addr, XOR_NEXT_DESC(chan));
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700114}
115
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700116static void mv_chan_unmask_interrupts(struct mv_xor_chan *chan)
117{
Thomas Petazzoni5733c382013-07-29 17:42:13 +0200118 u32 val = readl_relaxed(XOR_INTR_MASK(chan));
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700119 val |= XOR_INTR_MASK_VALUE << (chan->idx * 16);
Thomas Petazzoni5733c382013-07-29 17:42:13 +0200120 writel_relaxed(val, XOR_INTR_MASK(chan));
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700121}
122
123static u32 mv_chan_get_intr_cause(struct mv_xor_chan *chan)
124{
Thomas Petazzoni5733c382013-07-29 17:42:13 +0200125 u32 intr_cause = readl_relaxed(XOR_INTR_CAUSE(chan));
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700126 intr_cause = (intr_cause >> (chan->idx * 16)) & 0xFFFF;
127 return intr_cause;
128}
129
Maxime Ripard0951e722015-05-26 15:07:33 +0200130static void mv_chan_clear_eoc_cause(struct mv_xor_chan *chan)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700131{
Lior Amsalemba87d132014-08-27 10:52:53 -0300132 u32 val;
133
134 val = XOR_INT_END_OF_DESC | XOR_INT_END_OF_CHAIN | XOR_INT_STOPPED;
135 val = ~(val << (chan->idx * 16));
Thomas Petazzonic98c1782012-11-15 14:17:18 +0100136 dev_dbg(mv_chan_to_devp(chan), "%s, val 0x%08x\n", __func__, val);
Thomas Petazzoni5733c382013-07-29 17:42:13 +0200137 writel_relaxed(val, XOR_INTR_CAUSE(chan));
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700138}
139
Maxime Ripard0951e722015-05-26 15:07:33 +0200140static void mv_chan_clear_err_status(struct mv_xor_chan *chan)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700141{
142 u32 val = 0xFFFF0000 >> (chan->idx * 16);
Thomas Petazzoni5733c382013-07-29 17:42:13 +0200143 writel_relaxed(val, XOR_INTR_CAUSE(chan));
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700144}
145
Maxime Ripard0951e722015-05-26 15:07:33 +0200146static void mv_chan_set_mode(struct mv_xor_chan *chan,
Thomas Petazzoni81aafb32015-12-22 11:43:28 +0100147 u32 op_mode)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700148{
Thomas Petazzoni5733c382013-07-29 17:42:13 +0200149 u32 config = readl_relaxed(XOR_CONFIG(chan));
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700150
Lior Amsalem6f166312015-05-26 15:07:34 +0200151 config &= ~0x7;
152 config |= op_mode;
153
Thomas Petazzonie03bc652013-07-29 17:42:14 +0200154#if defined(__BIG_ENDIAN)
155 config |= XOR_DESCRIPTOR_SWAP;
156#else
157 config &= ~XOR_DESCRIPTOR_SWAP;
158#endif
159
Thomas Petazzoni5733c382013-07-29 17:42:13 +0200160 writel_relaxed(config, XOR_CONFIG(chan));
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700161}
162
163static void mv_chan_activate(struct mv_xor_chan *chan)
164{
Thomas Petazzonic98c1782012-11-15 14:17:18 +0100165 dev_dbg(mv_chan_to_devp(chan), " activate chan.\n");
Ezequiel Garcia5a9a55b2014-05-21 14:02:35 -0700166
167 /* writel ensures all descriptors are flushed before activation */
168 writel(BIT(0), XOR_ACTIVATION(chan));
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700169}
170
171static char mv_chan_is_busy(struct mv_xor_chan *chan)
172{
Thomas Petazzoni5733c382013-07-29 17:42:13 +0200173 u32 state = readl_relaxed(XOR_ACTIVATION(chan));
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700174
175 state = (state >> 4) & 0x3;
176
177 return (state == 1) ? 1 : 0;
178}
179
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700180/*
Maxime Ripard0951e722015-05-26 15:07:33 +0200181 * mv_chan_start_new_chain - program the engine to operate on new
182 * chain headed by sw_desc
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700183 * Caller must hold &mv_chan->lock while calling this function
184 */
Maxime Ripard0951e722015-05-26 15:07:33 +0200185static void mv_chan_start_new_chain(struct mv_xor_chan *mv_chan,
186 struct mv_xor_desc_slot *sw_desc)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700187{
Thomas Petazzonic98c1782012-11-15 14:17:18 +0100188 dev_dbg(mv_chan_to_devp(mv_chan), "%s %d: sw_desc %p\n",
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700189 __func__, __LINE__, sw_desc);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700190
Bartlomiej Zolnierkiewicz48a9db42013-07-03 15:05:06 -0700191 /* set the hardware chain */
192 mv_chan_set_next_descriptor(mv_chan, sw_desc->async_tx.phys);
193
Lior Amsalemdfc97662014-08-27 10:52:51 -0300194 mv_chan->pending++;
Thomas Petazzoni98817b92012-11-15 14:57:44 +0100195 mv_xor_issue_pending(&mv_chan->dmachan);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700196}
197
198static dma_cookie_t
Maxime Ripard0951e722015-05-26 15:07:33 +0200199mv_desc_run_tx_complete_actions(struct mv_xor_desc_slot *desc,
200 struct mv_xor_chan *mv_chan,
201 dma_cookie_t cookie)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700202{
203 BUG_ON(desc->async_tx.cookie < 0);
204
205 if (desc->async_tx.cookie > 0) {
206 cookie = desc->async_tx.cookie;
207
208 /* call the callback (must not sleep or submit new
209 * operations to this channel)
210 */
211 if (desc->async_tx.callback)
212 desc->async_tx.callback(
213 desc->async_tx.callback_param);
214
Dan Williamsd38a8c62013-10-18 19:35:23 +0200215 dma_descriptor_unmap(&desc->async_tx);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700216 }
217
218 /* run dependent operations */
Dan Williams07f22112009-01-05 17:14:31 -0700219 dma_run_dependencies(&desc->async_tx);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700220
221 return cookie;
222}
223
224static int
Maxime Ripard0951e722015-05-26 15:07:33 +0200225mv_chan_clean_completed_slots(struct mv_xor_chan *mv_chan)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700226{
227 struct mv_xor_desc_slot *iter, *_iter;
228
Thomas Petazzonic98c1782012-11-15 14:17:18 +0100229 dev_dbg(mv_chan_to_devp(mv_chan), "%s %d\n", __func__, __LINE__);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700230 list_for_each_entry_safe(iter, _iter, &mv_chan->completed_slots,
Lior Amsalemfbea28a2015-05-26 15:07:36 +0200231 node) {
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700232
Lior Amsalemfbea28a2015-05-26 15:07:36 +0200233 if (async_tx_test_ack(&iter->async_tx))
234 list_move_tail(&iter->node, &mv_chan->free_slots);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700235 }
236 return 0;
237}
238
239static int
Maxime Ripard0951e722015-05-26 15:07:33 +0200240mv_desc_clean_slot(struct mv_xor_desc_slot *desc,
241 struct mv_xor_chan *mv_chan)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700242{
Thomas Petazzonic98c1782012-11-15 14:17:18 +0100243 dev_dbg(mv_chan_to_devp(mv_chan), "%s %d: desc %p flags %d\n",
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700244 __func__, __LINE__, desc, desc->async_tx.flags);
Lior Amsalemfbea28a2015-05-26 15:07:36 +0200245
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700246 /* the client is allowed to attach dependent operations
247 * until 'ack' is set
248 */
Lior Amsalemfbea28a2015-05-26 15:07:36 +0200249 if (!async_tx_test_ack(&desc->async_tx))
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700250 /* move this slot to the completed_slots */
Lior Amsalemfbea28a2015-05-26 15:07:36 +0200251 list_move_tail(&desc->node, &mv_chan->completed_slots);
252 else
253 list_move_tail(&desc->node, &mv_chan->free_slots);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700254
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700255 return 0;
256}
257
Ezequiel Garciafbeec992014-03-07 16:46:47 -0300258/* This function must be called with the mv_xor_chan spinlock held */
Maxime Ripard0951e722015-05-26 15:07:33 +0200259static void mv_chan_slot_cleanup(struct mv_xor_chan *mv_chan)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700260{
261 struct mv_xor_desc_slot *iter, *_iter;
262 dma_cookie_t cookie = 0;
263 int busy = mv_chan_is_busy(mv_chan);
264 u32 current_desc = mv_chan_get_current_desc(mv_chan);
Lior Amsalem91362912015-05-26 15:07:32 +0200265 int current_cleaned = 0;
266 struct mv_xor_desc *hw_desc;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700267
Thomas Petazzonic98c1782012-11-15 14:17:18 +0100268 dev_dbg(mv_chan_to_devp(mv_chan), "%s %d\n", __func__, __LINE__);
269 dev_dbg(mv_chan_to_devp(mv_chan), "current_desc %x\n", current_desc);
Maxime Ripard0951e722015-05-26 15:07:33 +0200270 mv_chan_clean_completed_slots(mv_chan);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700271
272 /* free completed slots from the chain starting with
273 * the oldest descriptor
274 */
275
276 list_for_each_entry_safe(iter, _iter, &mv_chan->chain,
Lior Amsalemfbea28a2015-05-26 15:07:36 +0200277 node) {
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700278
Lior Amsalem91362912015-05-26 15:07:32 +0200279 /* clean finished descriptors */
280 hw_desc = iter->hw_desc;
281 if (hw_desc->status & XOR_DESC_SUCCESS) {
Maxime Ripard0951e722015-05-26 15:07:33 +0200282 cookie = mv_desc_run_tx_complete_actions(iter, mv_chan,
283 cookie);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700284
Lior Amsalem91362912015-05-26 15:07:32 +0200285 /* done processing desc, clean slot */
Maxime Ripard0951e722015-05-26 15:07:33 +0200286 mv_desc_clean_slot(iter, mv_chan);
Lior Amsalem91362912015-05-26 15:07:32 +0200287
288 /* break if we did cleaned the current */
289 if (iter->async_tx.phys == current_desc) {
290 current_cleaned = 1;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700291 break;
Lior Amsalem91362912015-05-26 15:07:32 +0200292 }
293 } else {
294 if (iter->async_tx.phys == current_desc) {
295 current_cleaned = 0;
296 break;
297 }
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700298 }
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700299 }
300
301 if ((busy == 0) && !list_empty(&mv_chan->chain)) {
Lior Amsalem91362912015-05-26 15:07:32 +0200302 if (current_cleaned) {
303 /*
304 * current descriptor cleaned and removed, run
305 * from list head
306 */
307 iter = list_entry(mv_chan->chain.next,
308 struct mv_xor_desc_slot,
Lior Amsalemfbea28a2015-05-26 15:07:36 +0200309 node);
Maxime Ripard0951e722015-05-26 15:07:33 +0200310 mv_chan_start_new_chain(mv_chan, iter);
Lior Amsalem91362912015-05-26 15:07:32 +0200311 } else {
Lior Amsalemfbea28a2015-05-26 15:07:36 +0200312 if (!list_is_last(&iter->node, &mv_chan->chain)) {
Lior Amsalem91362912015-05-26 15:07:32 +0200313 /*
314 * descriptors are still waiting after
315 * current, trigger them
316 */
Lior Amsalemfbea28a2015-05-26 15:07:36 +0200317 iter = list_entry(iter->node.next,
Lior Amsalem91362912015-05-26 15:07:32 +0200318 struct mv_xor_desc_slot,
Lior Amsalemfbea28a2015-05-26 15:07:36 +0200319 node);
Maxime Ripard0951e722015-05-26 15:07:33 +0200320 mv_chan_start_new_chain(mv_chan, iter);
Lior Amsalem91362912015-05-26 15:07:32 +0200321 } else {
322 /*
323 * some descriptors are still waiting
324 * to be cleaned
325 */
326 tasklet_schedule(&mv_chan->irq_tasklet);
327 }
328 }
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700329 }
330
331 if (cookie > 0)
Thomas Petazzoni98817b92012-11-15 14:57:44 +0100332 mv_chan->dmachan.completed_cookie = cookie;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700333}
334
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700335static void mv_xor_tasklet(unsigned long data)
336{
337 struct mv_xor_chan *chan = (struct mv_xor_chan *) data;
Ezequiel Garciae43147a2014-03-07 16:46:46 -0300338
339 spin_lock_bh(&chan->lock);
Maxime Ripard0951e722015-05-26 15:07:33 +0200340 mv_chan_slot_cleanup(chan);
Ezequiel Garciae43147a2014-03-07 16:46:46 -0300341 spin_unlock_bh(&chan->lock);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700342}
343
344static struct mv_xor_desc_slot *
Maxime Ripard0951e722015-05-26 15:07:33 +0200345mv_chan_alloc_slot(struct mv_xor_chan *mv_chan)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700346{
Lior Amsalemfbea28a2015-05-26 15:07:36 +0200347 struct mv_xor_desc_slot *iter;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700348
Lior Amsalemfbea28a2015-05-26 15:07:36 +0200349 spin_lock_bh(&mv_chan->lock);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700350
Lior Amsalemfbea28a2015-05-26 15:07:36 +0200351 if (!list_empty(&mv_chan->free_slots)) {
352 iter = list_first_entry(&mv_chan->free_slots,
353 struct mv_xor_desc_slot,
354 node);
Lior Amsalemdfc97662014-08-27 10:52:51 -0300355
Lior Amsalemfbea28a2015-05-26 15:07:36 +0200356 list_move_tail(&iter->node, &mv_chan->allocated_slots);
357
358 spin_unlock_bh(&mv_chan->lock);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700359
Lior Amsalemdfc97662014-08-27 10:52:51 -0300360 /* pre-ack descriptor */
361 async_tx_ack(&iter->async_tx);
Lior Amsalemdfc97662014-08-27 10:52:51 -0300362 iter->async_tx.cookie = -EBUSY;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700363
Lior Amsalemdfc97662014-08-27 10:52:51 -0300364 return iter;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700365
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700366 }
Lior Amsalemfbea28a2015-05-26 15:07:36 +0200367
368 spin_unlock_bh(&mv_chan->lock);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700369
370 /* try to free some slots if the allocation fails */
371 tasklet_schedule(&mv_chan->irq_tasklet);
372
373 return NULL;
374}
375
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700376/************************ DMA engine API functions ****************************/
377static dma_cookie_t
378mv_xor_tx_submit(struct dma_async_tx_descriptor *tx)
379{
380 struct mv_xor_desc_slot *sw_desc = to_mv_xor_slot(tx);
381 struct mv_xor_chan *mv_chan = to_mv_xor_chan(tx->chan);
Lior Amsalemdfc97662014-08-27 10:52:51 -0300382 struct mv_xor_desc_slot *old_chain_tail;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700383 dma_cookie_t cookie;
384 int new_hw_chain = 1;
385
Thomas Petazzonic98c1782012-11-15 14:17:18 +0100386 dev_dbg(mv_chan_to_devp(mv_chan),
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700387 "%s sw_desc %p: async_tx %p\n",
388 __func__, sw_desc, &sw_desc->async_tx);
389
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700390 spin_lock_bh(&mv_chan->lock);
Russell King - ARM Linux884485e2012-03-06 22:34:46 +0000391 cookie = dma_cookie_assign(tx);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700392
393 if (list_empty(&mv_chan->chain))
Lior Amsalemfbea28a2015-05-26 15:07:36 +0200394 list_move_tail(&sw_desc->node, &mv_chan->chain);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700395 else {
396 new_hw_chain = 0;
397
398 old_chain_tail = list_entry(mv_chan->chain.prev,
399 struct mv_xor_desc_slot,
Lior Amsalemfbea28a2015-05-26 15:07:36 +0200400 node);
401 list_move_tail(&sw_desc->node, &mv_chan->chain);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700402
Olof Johansson31fd8f52014-02-03 17:13:23 -0800403 dev_dbg(mv_chan_to_devp(mv_chan), "Append to last desc %pa\n",
404 &old_chain_tail->async_tx.phys);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700405
406 /* fix up the hardware chain */
Lior Amsalemdfc97662014-08-27 10:52:51 -0300407 mv_desc_set_next_desc(old_chain_tail, sw_desc->async_tx.phys);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700408
409 /* if the channel is not busy */
410 if (!mv_chan_is_busy(mv_chan)) {
411 u32 current_desc = mv_chan_get_current_desc(mv_chan);
412 /*
413 * and the curren desc is the end of the chain before
414 * the append, then we need to start the channel
415 */
416 if (current_desc == old_chain_tail->async_tx.phys)
417 new_hw_chain = 1;
418 }
419 }
420
421 if (new_hw_chain)
Maxime Ripard0951e722015-05-26 15:07:33 +0200422 mv_chan_start_new_chain(mv_chan, sw_desc);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700423
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700424 spin_unlock_bh(&mv_chan->lock);
425
426 return cookie;
427}
428
429/* returns the number of allocated descriptors */
Dan Williamsaa1e6f12009-01-06 11:38:17 -0700430static int mv_xor_alloc_chan_resources(struct dma_chan *chan)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700431{
Olof Johansson31fd8f52014-02-03 17:13:23 -0800432 void *virt_desc;
433 dma_addr_t dma_desc;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700434 int idx;
435 struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
436 struct mv_xor_desc_slot *slot = NULL;
Thomas Petazzonib503fa02012-11-15 15:55:30 +0100437 int num_descs_in_pool = MV_XOR_POOL_SIZE/MV_XOR_SLOT_SIZE;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700438
439 /* Allocate descriptor slots */
440 idx = mv_chan->slots_allocated;
441 while (idx < num_descs_in_pool) {
442 slot = kzalloc(sizeof(*slot), GFP_KERNEL);
443 if (!slot) {
Ezequiel Garciab8291dd2014-08-27 10:52:49 -0300444 dev_info(mv_chan_to_devp(mv_chan),
445 "channel only initialized %d descriptor slots",
446 idx);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700447 break;
448 }
Olof Johansson31fd8f52014-02-03 17:13:23 -0800449 virt_desc = mv_chan->dma_desc_pool_virt;
450 slot->hw_desc = virt_desc + idx * MV_XOR_SLOT_SIZE;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700451
452 dma_async_tx_descriptor_init(&slot->async_tx, chan);
453 slot->async_tx.tx_submit = mv_xor_tx_submit;
Lior Amsalemfbea28a2015-05-26 15:07:36 +0200454 INIT_LIST_HEAD(&slot->node);
Olof Johansson31fd8f52014-02-03 17:13:23 -0800455 dma_desc = mv_chan->dma_desc_pool;
456 slot->async_tx.phys = dma_desc + idx * MV_XOR_SLOT_SIZE;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700457 slot->idx = idx++;
458
459 spin_lock_bh(&mv_chan->lock);
460 mv_chan->slots_allocated = idx;
Lior Amsalemfbea28a2015-05-26 15:07:36 +0200461 list_add_tail(&slot->node, &mv_chan->free_slots);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700462 spin_unlock_bh(&mv_chan->lock);
463 }
464
Thomas Petazzonic98c1782012-11-15 14:17:18 +0100465 dev_dbg(mv_chan_to_devp(mv_chan),
Lior Amsalemfbea28a2015-05-26 15:07:36 +0200466 "allocated %d descriptor slots\n",
467 mv_chan->slots_allocated);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700468
469 return mv_chan->slots_allocated ? : -ENOMEM;
470}
471
472static struct dma_async_tx_descriptor *
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700473mv_xor_prep_dma_xor(struct dma_chan *chan, dma_addr_t dest, dma_addr_t *src,
474 unsigned int src_cnt, size_t len, unsigned long flags)
475{
476 struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
Lior Amsalemdfc97662014-08-27 10:52:51 -0300477 struct mv_xor_desc_slot *sw_desc;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700478
479 if (unlikely(len < MV_XOR_MIN_BYTE_COUNT))
480 return NULL;
481
Coly Li7912d302011-03-27 01:26:53 +0800482 BUG_ON(len > MV_XOR_MAX_BYTE_COUNT);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700483
Thomas Petazzonic98c1782012-11-15 14:17:18 +0100484 dev_dbg(mv_chan_to_devp(mv_chan),
Gregory CLEMENTbc822e12016-04-29 09:49:05 +0200485 "%s src_cnt: %d len: %zu dest %pad flags: %ld\n",
Olof Johansson31fd8f52014-02-03 17:13:23 -0800486 __func__, src_cnt, len, &dest, flags);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700487
Maxime Ripard0951e722015-05-26 15:07:33 +0200488 sw_desc = mv_chan_alloc_slot(mv_chan);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700489 if (sw_desc) {
490 sw_desc->type = DMA_XOR;
491 sw_desc->async_tx.flags = flags;
Lior Amsalemba87d132014-08-27 10:52:53 -0300492 mv_desc_init(sw_desc, dest, len, flags);
Lior Amsalem6f166312015-05-26 15:07:34 +0200493 if (mv_chan->op_in_desc == XOR_MODE_IN_DESC)
494 mv_desc_set_mode(sw_desc);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700495 while (src_cnt--)
Lior Amsalemdfc97662014-08-27 10:52:51 -0300496 mv_desc_set_src_addr(sw_desc, src_cnt, src[src_cnt]);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700497 }
Lior Amsalemfbea28a2015-05-26 15:07:36 +0200498
Thomas Petazzonic98c1782012-11-15 14:17:18 +0100499 dev_dbg(mv_chan_to_devp(mv_chan),
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700500 "%s sw_desc %p async_tx %p \n",
501 __func__, sw_desc, &sw_desc->async_tx);
502 return sw_desc ? &sw_desc->async_tx : NULL;
503}
504
Lior Amsalem3e4f52e2014-08-27 10:52:50 -0300505static struct dma_async_tx_descriptor *
506mv_xor_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
507 size_t len, unsigned long flags)
508{
509 /*
510 * A MEMCPY operation is identical to an XOR operation with only
511 * a single source address.
512 */
513 return mv_xor_prep_dma_xor(chan, dest, &src, 1, len, flags);
514}
515
Lior Amsalem22843542014-08-27 10:52:55 -0300516static struct dma_async_tx_descriptor *
517mv_xor_prep_dma_interrupt(struct dma_chan *chan, unsigned long flags)
518{
519 struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
520 dma_addr_t src, dest;
521 size_t len;
522
523 src = mv_chan->dummy_src_addr;
524 dest = mv_chan->dummy_dst_addr;
525 len = MV_XOR_MIN_BYTE_COUNT;
526
527 /*
528 * We implement the DMA_INTERRUPT operation as a minimum sized
529 * XOR operation with a single dummy source address.
530 */
531 return mv_xor_prep_dma_xor(chan, dest, &src, 1, len, flags);
532}
533
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700534static void mv_xor_free_chan_resources(struct dma_chan *chan)
535{
536 struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
537 struct mv_xor_desc_slot *iter, *_iter;
538 int in_use_descs = 0;
539
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700540 spin_lock_bh(&mv_chan->lock);
Ezequiel Garciae43147a2014-03-07 16:46:46 -0300541
Maxime Ripard0951e722015-05-26 15:07:33 +0200542 mv_chan_slot_cleanup(mv_chan);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700543
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700544 list_for_each_entry_safe(iter, _iter, &mv_chan->chain,
Lior Amsalemfbea28a2015-05-26 15:07:36 +0200545 node) {
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700546 in_use_descs++;
Lior Amsalemfbea28a2015-05-26 15:07:36 +0200547 list_move_tail(&iter->node, &mv_chan->free_slots);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700548 }
549 list_for_each_entry_safe(iter, _iter, &mv_chan->completed_slots,
Lior Amsalemfbea28a2015-05-26 15:07:36 +0200550 node) {
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700551 in_use_descs++;
Lior Amsalemfbea28a2015-05-26 15:07:36 +0200552 list_move_tail(&iter->node, &mv_chan->free_slots);
553 }
554 list_for_each_entry_safe(iter, _iter, &mv_chan->allocated_slots,
555 node) {
556 in_use_descs++;
557 list_move_tail(&iter->node, &mv_chan->free_slots);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700558 }
559 list_for_each_entry_safe_reverse(
Lior Amsalemfbea28a2015-05-26 15:07:36 +0200560 iter, _iter, &mv_chan->free_slots, node) {
561 list_del(&iter->node);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700562 kfree(iter);
563 mv_chan->slots_allocated--;
564 }
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700565
Thomas Petazzonic98c1782012-11-15 14:17:18 +0100566 dev_dbg(mv_chan_to_devp(mv_chan), "%s slots_allocated %d\n",
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700567 __func__, mv_chan->slots_allocated);
568 spin_unlock_bh(&mv_chan->lock);
569
570 if (in_use_descs)
Thomas Petazzonic98c1782012-11-15 14:17:18 +0100571 dev_err(mv_chan_to_devp(mv_chan),
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700572 "freeing %d in use descriptors!\n", in_use_descs);
573}
574
575/**
Linus Walleij07934482010-03-26 16:50:49 -0700576 * mv_xor_status - poll the status of an XOR transaction
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700577 * @chan: XOR channel handle
578 * @cookie: XOR transaction identifier
Linus Walleij07934482010-03-26 16:50:49 -0700579 * @txstate: XOR transactions state holder (or NULL)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700580 */
Linus Walleij07934482010-03-26 16:50:49 -0700581static enum dma_status mv_xor_status(struct dma_chan *chan,
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700582 dma_cookie_t cookie,
Linus Walleij07934482010-03-26 16:50:49 -0700583 struct dma_tx_state *txstate)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700584{
585 struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700586 enum dma_status ret;
587
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +0000588 ret = dma_cookie_status(chan, cookie, txstate);
Ezequiel Garcia890766d2014-03-07 16:46:45 -0300589 if (ret == DMA_COMPLETE)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700590 return ret;
Ezequiel Garciae43147a2014-03-07 16:46:46 -0300591
592 spin_lock_bh(&mv_chan->lock);
Maxime Ripard0951e722015-05-26 15:07:33 +0200593 mv_chan_slot_cleanup(mv_chan);
Ezequiel Garciae43147a2014-03-07 16:46:46 -0300594 spin_unlock_bh(&mv_chan->lock);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700595
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +0000596 return dma_cookie_status(chan, cookie, txstate);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700597}
598
Maxime Ripard0951e722015-05-26 15:07:33 +0200599static void mv_chan_dump_regs(struct mv_xor_chan *chan)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700600{
601 u32 val;
602
Thomas Petazzoni5733c382013-07-29 17:42:13 +0200603 val = readl_relaxed(XOR_CONFIG(chan));
Joe Perches1ba151c2012-10-28 01:05:44 -0700604 dev_err(mv_chan_to_devp(chan), "config 0x%08x\n", val);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700605
Thomas Petazzoni5733c382013-07-29 17:42:13 +0200606 val = readl_relaxed(XOR_ACTIVATION(chan));
Joe Perches1ba151c2012-10-28 01:05:44 -0700607 dev_err(mv_chan_to_devp(chan), "activation 0x%08x\n", val);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700608
Thomas Petazzoni5733c382013-07-29 17:42:13 +0200609 val = readl_relaxed(XOR_INTR_CAUSE(chan));
Joe Perches1ba151c2012-10-28 01:05:44 -0700610 dev_err(mv_chan_to_devp(chan), "intr cause 0x%08x\n", val);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700611
Thomas Petazzoni5733c382013-07-29 17:42:13 +0200612 val = readl_relaxed(XOR_INTR_MASK(chan));
Joe Perches1ba151c2012-10-28 01:05:44 -0700613 dev_err(mv_chan_to_devp(chan), "intr mask 0x%08x\n", val);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700614
Thomas Petazzoni5733c382013-07-29 17:42:13 +0200615 val = readl_relaxed(XOR_ERROR_CAUSE(chan));
Joe Perches1ba151c2012-10-28 01:05:44 -0700616 dev_err(mv_chan_to_devp(chan), "error cause 0x%08x\n", val);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700617
Thomas Petazzoni5733c382013-07-29 17:42:13 +0200618 val = readl_relaxed(XOR_ERROR_ADDR(chan));
Joe Perches1ba151c2012-10-28 01:05:44 -0700619 dev_err(mv_chan_to_devp(chan), "error addr 0x%08x\n", val);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700620}
621
Maxime Ripard0951e722015-05-26 15:07:33 +0200622static void mv_chan_err_interrupt_handler(struct mv_xor_chan *chan,
623 u32 intr_cause)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700624{
Ezequiel Garcia0e7488e2014-08-27 10:52:52 -0300625 if (intr_cause & XOR_INT_ERR_DECODE) {
626 dev_dbg(mv_chan_to_devp(chan), "ignoring address decode error\n");
627 return;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700628 }
629
Ezequiel Garcia0e7488e2014-08-27 10:52:52 -0300630 dev_err(mv_chan_to_devp(chan), "error on chan %d. intr cause 0x%08x\n",
Thomas Petazzonia3fc74b2012-11-15 12:50:27 +0100631 chan->idx, intr_cause);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700632
Maxime Ripard0951e722015-05-26 15:07:33 +0200633 mv_chan_dump_regs(chan);
Ezequiel Garcia0e7488e2014-08-27 10:52:52 -0300634 WARN_ON(1);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700635}
636
637static irqreturn_t mv_xor_interrupt_handler(int irq, void *data)
638{
639 struct mv_xor_chan *chan = data;
640 u32 intr_cause = mv_chan_get_intr_cause(chan);
641
Thomas Petazzonic98c1782012-11-15 14:17:18 +0100642 dev_dbg(mv_chan_to_devp(chan), "intr cause %x\n", intr_cause);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700643
Ezequiel Garcia0e7488e2014-08-27 10:52:52 -0300644 if (intr_cause & XOR_INTR_ERRORS)
Maxime Ripard0951e722015-05-26 15:07:33 +0200645 mv_chan_err_interrupt_handler(chan, intr_cause);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700646
647 tasklet_schedule(&chan->irq_tasklet);
648
Maxime Ripard0951e722015-05-26 15:07:33 +0200649 mv_chan_clear_eoc_cause(chan);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700650
651 return IRQ_HANDLED;
652}
653
654static void mv_xor_issue_pending(struct dma_chan *chan)
655{
656 struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
657
658 if (mv_chan->pending >= MV_XOR_THRESHOLD) {
659 mv_chan->pending = 0;
660 mv_chan_activate(mv_chan);
661 }
662}
663
664/*
665 * Perform a transaction to verify the HW works.
666 */
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700667
Maxime Ripard0951e722015-05-26 15:07:33 +0200668static int mv_chan_memcpy_self_test(struct mv_xor_chan *mv_chan)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700669{
Ezequiel Garciab8c01d22013-12-10 09:32:37 -0300670 int i, ret;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700671 void *src, *dest;
672 dma_addr_t src_dma, dest_dma;
673 struct dma_chan *dma_chan;
674 dma_cookie_t cookie;
675 struct dma_async_tx_descriptor *tx;
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300676 struct dmaengine_unmap_data *unmap;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700677 int err = 0;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700678
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300679 src = kmalloc(sizeof(u8) * PAGE_SIZE, GFP_KERNEL);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700680 if (!src)
681 return -ENOMEM;
682
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300683 dest = kzalloc(sizeof(u8) * PAGE_SIZE, GFP_KERNEL);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700684 if (!dest) {
685 kfree(src);
686 return -ENOMEM;
687 }
688
689 /* Fill in src buffer */
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300690 for (i = 0; i < PAGE_SIZE; i++)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700691 ((u8 *) src)[i] = (u8)i;
692
Thomas Petazzoni275cc0c2012-11-15 15:09:42 +0100693 dma_chan = &mv_chan->dmachan;
Dan Williamsaa1e6f12009-01-06 11:38:17 -0700694 if (mv_xor_alloc_chan_resources(dma_chan) < 1) {
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700695 err = -ENODEV;
696 goto out;
697 }
698
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300699 unmap = dmaengine_get_unmap_data(dma_chan->device->dev, 2, GFP_KERNEL);
700 if (!unmap) {
701 err = -ENOMEM;
702 goto free_resources;
703 }
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700704
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300705 src_dma = dma_map_page(dma_chan->device->dev, virt_to_page(src), 0,
706 PAGE_SIZE, DMA_TO_DEVICE);
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300707 unmap->addr[0] = src_dma;
708
Ezequiel Garciab8c01d22013-12-10 09:32:37 -0300709 ret = dma_mapping_error(dma_chan->device->dev, src_dma);
710 if (ret) {
711 err = -ENOMEM;
712 goto free_resources;
713 }
714 unmap->to_cnt = 1;
715
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300716 dest_dma = dma_map_page(dma_chan->device->dev, virt_to_page(dest), 0,
717 PAGE_SIZE, DMA_FROM_DEVICE);
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300718 unmap->addr[1] = dest_dma;
719
Ezequiel Garciab8c01d22013-12-10 09:32:37 -0300720 ret = dma_mapping_error(dma_chan->device->dev, dest_dma);
721 if (ret) {
722 err = -ENOMEM;
723 goto free_resources;
724 }
725 unmap->from_cnt = 1;
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300726 unmap->len = PAGE_SIZE;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700727
728 tx = mv_xor_prep_dma_memcpy(dma_chan, dest_dma, src_dma,
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300729 PAGE_SIZE, 0);
Ezequiel Garciab8c01d22013-12-10 09:32:37 -0300730 if (!tx) {
731 dev_err(dma_chan->device->dev,
732 "Self-test cannot prepare operation, disabling\n");
733 err = -ENODEV;
734 goto free_resources;
735 }
736
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700737 cookie = mv_xor_tx_submit(tx);
Ezequiel Garciab8c01d22013-12-10 09:32:37 -0300738 if (dma_submit_error(cookie)) {
739 dev_err(dma_chan->device->dev,
740 "Self-test submit error, disabling\n");
741 err = -ENODEV;
742 goto free_resources;
743 }
744
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700745 mv_xor_issue_pending(dma_chan);
746 async_tx_ack(tx);
747 msleep(1);
748
Linus Walleij07934482010-03-26 16:50:49 -0700749 if (mv_xor_status(dma_chan, cookie, NULL) !=
Vinod Koulb3efb8f2013-10-16 20:51:04 +0530750 DMA_COMPLETE) {
Thomas Petazzonia3fc74b2012-11-15 12:50:27 +0100751 dev_err(dma_chan->device->dev,
752 "Self-test copy timed out, disabling\n");
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700753 err = -ENODEV;
754 goto free_resources;
755 }
756
Thomas Petazzonic35064c2012-11-15 13:01:59 +0100757 dma_sync_single_for_cpu(dma_chan->device->dev, dest_dma,
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300758 PAGE_SIZE, DMA_FROM_DEVICE);
759 if (memcmp(src, dest, PAGE_SIZE)) {
Thomas Petazzonia3fc74b2012-11-15 12:50:27 +0100760 dev_err(dma_chan->device->dev,
761 "Self-test copy failed compare, disabling\n");
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700762 err = -ENODEV;
763 goto free_resources;
764 }
765
766free_resources:
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300767 dmaengine_unmap_put(unmap);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700768 mv_xor_free_chan_resources(dma_chan);
769out:
770 kfree(src);
771 kfree(dest);
772 return err;
773}
774
775#define MV_XOR_NUM_SRC_TEST 4 /* must be <= 15 */
Bill Pemberton463a1f82012-11-19 13:22:55 -0500776static int
Maxime Ripard0951e722015-05-26 15:07:33 +0200777mv_chan_xor_self_test(struct mv_xor_chan *mv_chan)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700778{
Ezequiel Garciab8c01d22013-12-10 09:32:37 -0300779 int i, src_idx, ret;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700780 struct page *dest;
781 struct page *xor_srcs[MV_XOR_NUM_SRC_TEST];
782 dma_addr_t dma_srcs[MV_XOR_NUM_SRC_TEST];
783 dma_addr_t dest_dma;
784 struct dma_async_tx_descriptor *tx;
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300785 struct dmaengine_unmap_data *unmap;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700786 struct dma_chan *dma_chan;
787 dma_cookie_t cookie;
788 u8 cmp_byte = 0;
789 u32 cmp_word;
790 int err = 0;
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300791 int src_count = MV_XOR_NUM_SRC_TEST;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700792
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300793 for (src_idx = 0; src_idx < src_count; src_idx++) {
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700794 xor_srcs[src_idx] = alloc_page(GFP_KERNEL);
Roel Kluina09b09a2009-02-25 13:56:21 +0100795 if (!xor_srcs[src_idx]) {
796 while (src_idx--)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700797 __free_page(xor_srcs[src_idx]);
Roel Kluina09b09a2009-02-25 13:56:21 +0100798 return -ENOMEM;
799 }
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700800 }
801
802 dest = alloc_page(GFP_KERNEL);
Roel Kluina09b09a2009-02-25 13:56:21 +0100803 if (!dest) {
804 while (src_idx--)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700805 __free_page(xor_srcs[src_idx]);
Roel Kluina09b09a2009-02-25 13:56:21 +0100806 return -ENOMEM;
807 }
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700808
809 /* Fill in src buffers */
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300810 for (src_idx = 0; src_idx < src_count; src_idx++) {
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700811 u8 *ptr = page_address(xor_srcs[src_idx]);
812 for (i = 0; i < PAGE_SIZE; i++)
813 ptr[i] = (1 << src_idx);
814 }
815
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300816 for (src_idx = 0; src_idx < src_count; src_idx++)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700817 cmp_byte ^= (u8) (1 << src_idx);
818
819 cmp_word = (cmp_byte << 24) | (cmp_byte << 16) |
820 (cmp_byte << 8) | cmp_byte;
821
822 memset(page_address(dest), 0, PAGE_SIZE);
823
Thomas Petazzoni275cc0c2012-11-15 15:09:42 +0100824 dma_chan = &mv_chan->dmachan;
Dan Williamsaa1e6f12009-01-06 11:38:17 -0700825 if (mv_xor_alloc_chan_resources(dma_chan) < 1) {
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700826 err = -ENODEV;
827 goto out;
828 }
829
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300830 unmap = dmaengine_get_unmap_data(dma_chan->device->dev, src_count + 1,
831 GFP_KERNEL);
832 if (!unmap) {
833 err = -ENOMEM;
834 goto free_resources;
835 }
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700836
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300837 /* test xor */
838 for (i = 0; i < src_count; i++) {
839 unmap->addr[i] = dma_map_page(dma_chan->device->dev, xor_srcs[i],
840 0, PAGE_SIZE, DMA_TO_DEVICE);
841 dma_srcs[i] = unmap->addr[i];
Ezequiel Garciab8c01d22013-12-10 09:32:37 -0300842 ret = dma_mapping_error(dma_chan->device->dev, unmap->addr[i]);
843 if (ret) {
844 err = -ENOMEM;
845 goto free_resources;
846 }
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300847 unmap->to_cnt++;
848 }
849
850 unmap->addr[src_count] = dma_map_page(dma_chan->device->dev, dest, 0, PAGE_SIZE,
851 DMA_FROM_DEVICE);
852 dest_dma = unmap->addr[src_count];
Ezequiel Garciab8c01d22013-12-10 09:32:37 -0300853 ret = dma_mapping_error(dma_chan->device->dev, unmap->addr[src_count]);
854 if (ret) {
855 err = -ENOMEM;
856 goto free_resources;
857 }
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300858 unmap->from_cnt = 1;
859 unmap->len = PAGE_SIZE;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700860
861 tx = mv_xor_prep_dma_xor(dma_chan, dest_dma, dma_srcs,
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300862 src_count, PAGE_SIZE, 0);
Ezequiel Garciab8c01d22013-12-10 09:32:37 -0300863 if (!tx) {
864 dev_err(dma_chan->device->dev,
865 "Self-test cannot prepare operation, disabling\n");
866 err = -ENODEV;
867 goto free_resources;
868 }
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700869
870 cookie = mv_xor_tx_submit(tx);
Ezequiel Garciab8c01d22013-12-10 09:32:37 -0300871 if (dma_submit_error(cookie)) {
872 dev_err(dma_chan->device->dev,
873 "Self-test submit error, disabling\n");
874 err = -ENODEV;
875 goto free_resources;
876 }
877
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700878 mv_xor_issue_pending(dma_chan);
879 async_tx_ack(tx);
880 msleep(8);
881
Linus Walleij07934482010-03-26 16:50:49 -0700882 if (mv_xor_status(dma_chan, cookie, NULL) !=
Vinod Koulb3efb8f2013-10-16 20:51:04 +0530883 DMA_COMPLETE) {
Thomas Petazzonia3fc74b2012-11-15 12:50:27 +0100884 dev_err(dma_chan->device->dev,
885 "Self-test xor timed out, disabling\n");
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700886 err = -ENODEV;
887 goto free_resources;
888 }
889
Thomas Petazzonic35064c2012-11-15 13:01:59 +0100890 dma_sync_single_for_cpu(dma_chan->device->dev, dest_dma,
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700891 PAGE_SIZE, DMA_FROM_DEVICE);
892 for (i = 0; i < (PAGE_SIZE / sizeof(u32)); i++) {
893 u32 *ptr = page_address(dest);
894 if (ptr[i] != cmp_word) {
Thomas Petazzonia3fc74b2012-11-15 12:50:27 +0100895 dev_err(dma_chan->device->dev,
Joe Perches1ba151c2012-10-28 01:05:44 -0700896 "Self-test xor failed compare, disabling. index %d, data %x, expected %x\n",
897 i, ptr[i], cmp_word);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700898 err = -ENODEV;
899 goto free_resources;
900 }
901 }
902
903free_resources:
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300904 dmaengine_unmap_put(unmap);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700905 mv_xor_free_chan_resources(dma_chan);
906out:
Ezequiel Garciad16695a2013-12-10 09:32:36 -0300907 src_idx = src_count;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700908 while (src_idx--)
909 __free_page(xor_srcs[src_idx]);
910 __free_page(dest);
911 return err;
912}
913
Thomas Petazzoni1ef48a22012-11-15 15:17:05 +0100914static int mv_xor_channel_remove(struct mv_xor_chan *mv_chan)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700915{
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700916 struct dma_chan *chan, *_chan;
Thomas Petazzoni1ef48a22012-11-15 15:17:05 +0100917 struct device *dev = mv_chan->dmadev.dev;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700918
Thomas Petazzoni1ef48a22012-11-15 15:17:05 +0100919 dma_async_device_unregister(&mv_chan->dmadev);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700920
Thomas Petazzonib503fa02012-11-15 15:55:30 +0100921 dma_free_coherent(dev, MV_XOR_POOL_SIZE,
Thomas Petazzoni1ef48a22012-11-15 15:17:05 +0100922 mv_chan->dma_desc_pool_virt, mv_chan->dma_desc_pool);
Lior Amsalem22843542014-08-27 10:52:55 -0300923 dma_unmap_single(dev, mv_chan->dummy_src_addr,
924 MV_XOR_MIN_BYTE_COUNT, DMA_FROM_DEVICE);
925 dma_unmap_single(dev, mv_chan->dummy_dst_addr,
926 MV_XOR_MIN_BYTE_COUNT, DMA_TO_DEVICE);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700927
Thomas Petazzoni1ef48a22012-11-15 15:17:05 +0100928 list_for_each_entry_safe(chan, _chan, &mv_chan->dmadev.channels,
Thomas Petazzonia6b4a9d2012-10-29 16:45:46 +0100929 device_node) {
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700930 list_del(&chan->device_node);
931 }
932
Thomas Petazzoni88eb92c2012-11-15 16:11:18 +0100933 free_irq(mv_chan->irq, mv_chan);
934
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700935 return 0;
936}
937
Thomas Petazzoni1ef48a22012-11-15 15:17:05 +0100938static struct mv_xor_chan *
Thomas Petazzoni297eedb2012-11-15 15:29:53 +0100939mv_xor_channel_add(struct mv_xor_device *xordev,
Thomas Petazzonia6b4a9d2012-10-29 16:45:46 +0100940 struct platform_device *pdev,
Gregory CLEMENTdd130c62016-04-29 09:49:06 +0200941 int idx, dma_cap_mask_t cap_mask, int irq)
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700942{
943 int ret = 0;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700944 struct mv_xor_chan *mv_chan;
945 struct dma_device *dma_dev;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700946
Thomas Petazzoni1ef48a22012-11-15 15:17:05 +0100947 mv_chan = devm_kzalloc(&pdev->dev, sizeof(*mv_chan), GFP_KERNEL);
Sachin Kamata5776592013-09-02 13:54:20 +0530948 if (!mv_chan)
949 return ERR_PTR(-ENOMEM);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700950
Thomas Petazzoni9aedbdb2012-11-15 15:36:37 +0100951 mv_chan->idx = idx;
Thomas Petazzoni88eb92c2012-11-15 16:11:18 +0100952 mv_chan->irq = irq;
Gregory CLEMENTdd130c62016-04-29 09:49:06 +0200953 if (xordev->xor_type == XOR_ORION)
954 mv_chan->op_in_desc = XOR_MODE_IN_REG;
955 else
956 mv_chan->op_in_desc = XOR_MODE_IN_DESC;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700957
Thomas Petazzoni1ef48a22012-11-15 15:17:05 +0100958 dma_dev = &mv_chan->dmadev;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700959
Lior Amsalem22843542014-08-27 10:52:55 -0300960 /*
961 * These source and destination dummy buffers are used to implement
962 * a DMA_INTERRUPT operation as a minimum-sized XOR operation.
963 * Hence, we only need to map the buffers at initialization-time.
964 */
965 mv_chan->dummy_src_addr = dma_map_single(dma_dev->dev,
966 mv_chan->dummy_src, MV_XOR_MIN_BYTE_COUNT, DMA_FROM_DEVICE);
967 mv_chan->dummy_dst_addr = dma_map_single(dma_dev->dev,
968 mv_chan->dummy_dst, MV_XOR_MIN_BYTE_COUNT, DMA_TO_DEVICE);
969
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700970 /* allocate coherent memory for hardware descriptors
971 * note: writecombine gives slightly better performance, but
972 * requires that we explicitly flush the writes
973 */
Thomas Petazzoni1ef48a22012-11-15 15:17:05 +0100974 mv_chan->dma_desc_pool_virt =
Luis R. Rodriguezf6e45662016-01-22 18:34:22 -0800975 dma_alloc_wc(&pdev->dev, MV_XOR_POOL_SIZE, &mv_chan->dma_desc_pool,
976 GFP_KERNEL);
Thomas Petazzoni1ef48a22012-11-15 15:17:05 +0100977 if (!mv_chan->dma_desc_pool_virt)
Thomas Petazzonia6b4a9d2012-10-29 16:45:46 +0100978 return ERR_PTR(-ENOMEM);
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700979
980 /* discover transaction capabilites from the platform data */
Thomas Petazzonia6b4a9d2012-10-29 16:45:46 +0100981 dma_dev->cap_mask = cap_mask;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700982
983 INIT_LIST_HEAD(&dma_dev->channels);
984
985 /* set base routines */
986 dma_dev->device_alloc_chan_resources = mv_xor_alloc_chan_resources;
987 dma_dev->device_free_chan_resources = mv_xor_free_chan_resources;
Linus Walleij07934482010-03-26 16:50:49 -0700988 dma_dev->device_tx_status = mv_xor_status;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700989 dma_dev->device_issue_pending = mv_xor_issue_pending;
990 dma_dev->dev = &pdev->dev;
991
992 /* set prep routines based on capability */
Lior Amsalem22843542014-08-27 10:52:55 -0300993 if (dma_has_cap(DMA_INTERRUPT, dma_dev->cap_mask))
994 dma_dev->device_prep_dma_interrupt = mv_xor_prep_dma_interrupt;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700995 if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask))
996 dma_dev->device_prep_dma_memcpy = mv_xor_prep_dma_memcpy;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700997 if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
Joe Perchesc0198942009-06-28 09:26:21 -0700998 dma_dev->max_xor = 8;
Saeed Bisharaff7b0472008-07-08 11:58:36 -0700999 dma_dev->device_prep_dma_xor = mv_xor_prep_dma_xor;
1000 }
1001
Thomas Petazzoni297eedb2012-11-15 15:29:53 +01001002 mv_chan->mmr_base = xordev->xor_base;
Ezequiel Garcia82a14022013-10-30 12:01:43 -03001003 mv_chan->mmr_high_base = xordev->xor_high_base;
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001004 tasklet_init(&mv_chan->irq_tasklet, mv_xor_tasklet, (unsigned long)
1005 mv_chan);
1006
1007 /* clear errors before enabling interrupts */
Maxime Ripard0951e722015-05-26 15:07:33 +02001008 mv_chan_clear_err_status(mv_chan);
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001009
Thomas Petazzoni2d0a0742012-11-22 18:19:09 +01001010 ret = request_irq(mv_chan->irq, mv_xor_interrupt_handler,
1011 0, dev_name(&pdev->dev), mv_chan);
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001012 if (ret)
1013 goto err_free_dma;
1014
1015 mv_chan_unmask_interrupts(mv_chan);
1016
Lior Amsalem6f166312015-05-26 15:07:34 +02001017 if (mv_chan->op_in_desc == XOR_MODE_IN_DESC)
Thomas Petazzoni81aafb32015-12-22 11:43:28 +01001018 mv_chan_set_mode(mv_chan, XOR_OPERATION_MODE_IN_DESC);
Lior Amsalem6f166312015-05-26 15:07:34 +02001019 else
Thomas Petazzoni81aafb32015-12-22 11:43:28 +01001020 mv_chan_set_mode(mv_chan, XOR_OPERATION_MODE_XOR);
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001021
1022 spin_lock_init(&mv_chan->lock);
1023 INIT_LIST_HEAD(&mv_chan->chain);
1024 INIT_LIST_HEAD(&mv_chan->completed_slots);
Lior Amsalemfbea28a2015-05-26 15:07:36 +02001025 INIT_LIST_HEAD(&mv_chan->free_slots);
1026 INIT_LIST_HEAD(&mv_chan->allocated_slots);
Thomas Petazzoni98817b92012-11-15 14:57:44 +01001027 mv_chan->dmachan.device = dma_dev;
1028 dma_cookie_init(&mv_chan->dmachan);
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001029
Thomas Petazzoni98817b92012-11-15 14:57:44 +01001030 list_add_tail(&mv_chan->dmachan.device_node, &dma_dev->channels);
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001031
1032 if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask)) {
Maxime Ripard0951e722015-05-26 15:07:33 +02001033 ret = mv_chan_memcpy_self_test(mv_chan);
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001034 dev_dbg(&pdev->dev, "memcpy self test returned %d\n", ret);
1035 if (ret)
Thomas Petazzoni2d0a0742012-11-22 18:19:09 +01001036 goto err_free_irq;
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001037 }
1038
1039 if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
Maxime Ripard0951e722015-05-26 15:07:33 +02001040 ret = mv_chan_xor_self_test(mv_chan);
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001041 dev_dbg(&pdev->dev, "xor self test returned %d\n", ret);
1042 if (ret)
Thomas Petazzoni2d0a0742012-11-22 18:19:09 +01001043 goto err_free_irq;
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001044 }
1045
Lior Amsalem6f166312015-05-26 15:07:34 +02001046 dev_info(&pdev->dev, "Marvell XOR (%s): ( %s%s%s)\n",
1047 mv_chan->op_in_desc ? "Descriptor Mode" : "Registers Mode",
Joe Perches1ba151c2012-10-28 01:05:44 -07001048 dma_has_cap(DMA_XOR, dma_dev->cap_mask) ? "xor " : "",
Joe Perches1ba151c2012-10-28 01:05:44 -07001049 dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask) ? "cpy " : "",
1050 dma_has_cap(DMA_INTERRUPT, dma_dev->cap_mask) ? "intr " : "");
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001051
1052 dma_async_device_register(dma_dev);
Thomas Petazzoni1ef48a22012-11-15 15:17:05 +01001053 return mv_chan;
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001054
Thomas Petazzoni2d0a0742012-11-22 18:19:09 +01001055err_free_irq:
1056 free_irq(mv_chan->irq, mv_chan);
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001057 err_free_dma:
Thomas Petazzonib503fa02012-11-15 15:55:30 +01001058 dma_free_coherent(&pdev->dev, MV_XOR_POOL_SIZE,
Thomas Petazzoni1ef48a22012-11-15 15:17:05 +01001059 mv_chan->dma_desc_pool_virt, mv_chan->dma_desc_pool);
Thomas Petazzonia6b4a9d2012-10-29 16:45:46 +01001060 return ERR_PTR(ret);
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001061}
1062
1063static void
Thomas Petazzoni297eedb2012-11-15 15:29:53 +01001064mv_xor_conf_mbus_windows(struct mv_xor_device *xordev,
Andrew Lunn63a93322011-12-07 21:48:07 +01001065 const struct mbus_dram_target_info *dram)
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001066{
Ezequiel Garcia82a14022013-10-30 12:01:43 -03001067 void __iomem *base = xordev->xor_high_base;
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001068 u32 win_enable = 0;
1069 int i;
1070
1071 for (i = 0; i < 8; i++) {
1072 writel(0, base + WINDOW_BASE(i));
1073 writel(0, base + WINDOW_SIZE(i));
1074 if (i < 4)
1075 writel(0, base + WINDOW_REMAP_HIGH(i));
1076 }
1077
1078 for (i = 0; i < dram->num_cs; i++) {
Andrew Lunn63a93322011-12-07 21:48:07 +01001079 const struct mbus_dram_window *cs = dram->cs + i;
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001080
1081 writel((cs->base & 0xffff0000) |
1082 (cs->mbus_attr << 8) |
1083 dram->mbus_dram_target_id, base + WINDOW_BASE(i));
1084 writel((cs->size - 1) & 0xffff0000, base + WINDOW_SIZE(i));
1085
1086 win_enable |= (1 << i);
1087 win_enable |= 3 << (16 + (2 * i));
1088 }
1089
1090 writel(win_enable, base + WINDOW_BAR_ENABLE(0));
1091 writel(win_enable, base + WINDOW_BAR_ENABLE(1));
Thomas Petazzonic4b4b732012-11-22 18:16:37 +01001092 writel(0, base + WINDOW_OVERRIDE_CTRL(0));
1093 writel(0, base + WINDOW_OVERRIDE_CTRL(1));
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001094}
1095
Thomas Petazzoni8b648432015-12-22 11:43:29 +01001096/*
1097 * Since this XOR driver is basically used only for RAID5, we don't
1098 * need to care about synchronizing ->suspend with DMA activity,
1099 * because the DMA engine will naturally be quiet due to the block
1100 * devices being suspended.
1101 */
1102static int mv_xor_suspend(struct platform_device *pdev, pm_message_t state)
1103{
1104 struct mv_xor_device *xordev = platform_get_drvdata(pdev);
1105 int i;
1106
1107 for (i = 0; i < MV_XOR_MAX_CHANNELS; i++) {
1108 struct mv_xor_chan *mv_chan = xordev->channels[i];
1109
1110 if (!mv_chan)
1111 continue;
1112
1113 mv_chan->saved_config_reg =
1114 readl_relaxed(XOR_CONFIG(mv_chan));
1115 mv_chan->saved_int_mask_reg =
1116 readl_relaxed(XOR_INTR_MASK(mv_chan));
1117 }
1118
1119 return 0;
1120}
1121
1122static int mv_xor_resume(struct platform_device *dev)
1123{
1124 struct mv_xor_device *xordev = platform_get_drvdata(dev);
1125 const struct mbus_dram_target_info *dram;
1126 int i;
1127
1128 for (i = 0; i < MV_XOR_MAX_CHANNELS; i++) {
1129 struct mv_xor_chan *mv_chan = xordev->channels[i];
1130
1131 if (!mv_chan)
1132 continue;
1133
1134 writel_relaxed(mv_chan->saved_config_reg,
1135 XOR_CONFIG(mv_chan));
1136 writel_relaxed(mv_chan->saved_int_mask_reg,
1137 XOR_INTR_MASK(mv_chan));
1138 }
1139
1140 dram = mv_mbus_dram_info();
1141 if (dram)
1142 mv_xor_conf_mbus_windows(xordev, dram);
1143
1144 return 0;
1145}
1146
Lior Amsalem6f166312015-05-26 15:07:34 +02001147static const struct of_device_id mv_xor_dt_ids[] = {
Gregory CLEMENTdd130c62016-04-29 09:49:06 +02001148 { .compatible = "marvell,orion-xor", .data = (void *)XOR_ORION },
1149 { .compatible = "marvell,armada-380-xor", .data = (void *)XOR_ARMADA_38X },
Lior Amsalem6f166312015-05-26 15:07:34 +02001150 {},
1151};
Lior Amsalem6f166312015-05-26 15:07:34 +02001152
Thomas Petazzoni77757292015-07-08 16:28:19 +02001153static unsigned int mv_xor_engine_count;
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001154
Linus Torvaldsc2714332012-12-14 14:54:26 -08001155static int mv_xor_probe(struct platform_device *pdev)
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001156{
Andrew Lunn63a93322011-12-07 21:48:07 +01001157 const struct mbus_dram_target_info *dram;
Thomas Petazzoni297eedb2012-11-15 15:29:53 +01001158 struct mv_xor_device *xordev;
Jingoo Hand4adcc02013-07-30 17:09:11 +09001159 struct mv_xor_platform_data *pdata = dev_get_platdata(&pdev->dev);
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001160 struct resource *res;
Thomas Petazzoni77757292015-07-08 16:28:19 +02001161 unsigned int max_engines, max_channels;
Thomas Petazzoni60d151f2012-10-29 16:54:49 +01001162 int i, ret;
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001163
Joe Perches1ba151c2012-10-28 01:05:44 -07001164 dev_notice(&pdev->dev, "Marvell shared XOR driver\n");
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001165
Thomas Petazzoni297eedb2012-11-15 15:29:53 +01001166 xordev = devm_kzalloc(&pdev->dev, sizeof(*xordev), GFP_KERNEL);
1167 if (!xordev)
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001168 return -ENOMEM;
1169
1170 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1171 if (!res)
1172 return -ENODEV;
1173
Thomas Petazzoni297eedb2012-11-15 15:29:53 +01001174 xordev->xor_base = devm_ioremap(&pdev->dev, res->start,
1175 resource_size(res));
1176 if (!xordev->xor_base)
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001177 return -EBUSY;
1178
1179 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1180 if (!res)
1181 return -ENODEV;
1182
Thomas Petazzoni297eedb2012-11-15 15:29:53 +01001183 xordev->xor_high_base = devm_ioremap(&pdev->dev, res->start,
1184 resource_size(res));
1185 if (!xordev->xor_high_base)
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001186 return -EBUSY;
1187
Thomas Petazzoni297eedb2012-11-15 15:29:53 +01001188 platform_set_drvdata(pdev, xordev);
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001189
Gregory CLEMENTdd130c62016-04-29 09:49:06 +02001190
1191 /*
1192 * We need to know which type of XOR device we use before
1193 * setting up. In non-dt case it can only be the legacy one.
1194 */
1195 xordev->xor_type = XOR_ORION;
1196 if (pdev->dev.of_node) {
1197 const struct of_device_id *of_id =
1198 of_match_device(mv_xor_dt_ids,
1199 &pdev->dev);
1200
1201 xordev->xor_type = (uintptr_t)of_id->data;
1202 }
1203
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001204 /*
1205 * (Re-)program MBUS remapping windows if we are asked to.
1206 */
Andrew Lunn63a93322011-12-07 21:48:07 +01001207 dram = mv_mbus_dram_info();
1208 if (dram)
Thomas Petazzoni297eedb2012-11-15 15:29:53 +01001209 mv_xor_conf_mbus_windows(xordev, dram);
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001210
Andrew Lunnc5101822012-02-19 13:30:26 +01001211 /* Not all platforms can gate the clock, so it is not
1212 * an error if the clock does not exists.
1213 */
Thomas Petazzoni297eedb2012-11-15 15:29:53 +01001214 xordev->clk = clk_get(&pdev->dev, NULL);
1215 if (!IS_ERR(xordev->clk))
1216 clk_prepare_enable(xordev->clk);
Andrew Lunnc5101822012-02-19 13:30:26 +01001217
Thomas Petazzoni77757292015-07-08 16:28:19 +02001218 /*
1219 * We don't want to have more than one channel per CPU in
1220 * order for async_tx to perform well. So we limit the number
1221 * of engines and channels so that we take into account this
1222 * constraint. Note that we also want to use channels from
1223 * separate engines when possible.
1224 */
1225 max_engines = num_present_cpus();
1226 max_channels = min_t(unsigned int,
1227 MV_XOR_MAX_CHANNELS,
1228 DIV_ROUND_UP(num_present_cpus(), 2));
1229
1230 if (mv_xor_engine_count >= max_engines)
1231 return 0;
1232
Thomas Petazzonif7d12ef2012-11-15 16:47:58 +01001233 if (pdev->dev.of_node) {
1234 struct device_node *np;
1235 int i = 0;
1236
1237 for_each_child_of_node(pdev->dev.of_node, np) {
Russell King0be82532013-12-12 23:59:08 +00001238 struct mv_xor_chan *chan;
Thomas Petazzonif7d12ef2012-11-15 16:47:58 +01001239 dma_cap_mask_t cap_mask;
1240 int irq;
1241
Thomas Petazzoni77757292015-07-08 16:28:19 +02001242 if (i >= max_channels)
1243 continue;
1244
Thomas Petazzonif7d12ef2012-11-15 16:47:58 +01001245 dma_cap_zero(cap_mask);
Thomas Petazzoni6d8f7ab2015-07-08 16:28:16 +02001246 dma_cap_set(DMA_MEMCPY, cap_mask);
1247 dma_cap_set(DMA_XOR, cap_mask);
1248 dma_cap_set(DMA_INTERRUPT, cap_mask);
Thomas Petazzonif7d12ef2012-11-15 16:47:58 +01001249
1250 irq = irq_of_parse_and_map(np, 0);
Thomas Petazzonif8eb9e72012-11-22 18:22:12 +01001251 if (!irq) {
1252 ret = -ENODEV;
Thomas Petazzonif7d12ef2012-11-15 16:47:58 +01001253 goto err_channel_add;
1254 }
1255
Russell King0be82532013-12-12 23:59:08 +00001256 chan = mv_xor_channel_add(xordev, pdev, i,
Gregory CLEMENTdd130c62016-04-29 09:49:06 +02001257 cap_mask, irq);
Russell King0be82532013-12-12 23:59:08 +00001258 if (IS_ERR(chan)) {
1259 ret = PTR_ERR(chan);
Thomas Petazzonif7d12ef2012-11-15 16:47:58 +01001260 irq_dispose_mapping(irq);
1261 goto err_channel_add;
1262 }
1263
Russell King0be82532013-12-12 23:59:08 +00001264 xordev->channels[i] = chan;
Thomas Petazzonif7d12ef2012-11-15 16:47:58 +01001265 i++;
1266 }
1267 } else if (pdata && pdata->channels) {
Thomas Petazzoni77757292015-07-08 16:28:19 +02001268 for (i = 0; i < max_channels; i++) {
Thomas Petazzonie39f6ec2012-10-30 11:56:26 +01001269 struct mv_xor_channel_data *cd;
Russell King0be82532013-12-12 23:59:08 +00001270 struct mv_xor_chan *chan;
Thomas Petazzoni60d151f2012-10-29 16:54:49 +01001271 int irq;
1272
1273 cd = &pdata->channels[i];
1274 if (!cd) {
1275 ret = -ENODEV;
1276 goto err_channel_add;
1277 }
1278
1279 irq = platform_get_irq(pdev, i);
1280 if (irq < 0) {
1281 ret = irq;
1282 goto err_channel_add;
1283 }
1284
Russell King0be82532013-12-12 23:59:08 +00001285 chan = mv_xor_channel_add(xordev, pdev, i,
Gregory CLEMENTdd130c62016-04-29 09:49:06 +02001286 cd->cap_mask, irq);
Russell King0be82532013-12-12 23:59:08 +00001287 if (IS_ERR(chan)) {
1288 ret = PTR_ERR(chan);
Thomas Petazzoni60d151f2012-10-29 16:54:49 +01001289 goto err_channel_add;
1290 }
Russell King0be82532013-12-12 23:59:08 +00001291
1292 xordev->channels[i] = chan;
Thomas Petazzoni60d151f2012-10-29 16:54:49 +01001293 }
1294 }
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001295
1296 return 0;
Thomas Petazzoni60d151f2012-10-29 16:54:49 +01001297
1298err_channel_add:
1299 for (i = 0; i < MV_XOR_MAX_CHANNELS; i++)
Thomas Petazzonif7d12ef2012-11-15 16:47:58 +01001300 if (xordev->channels[i]) {
Thomas Petazzoniab6e4392013-01-06 11:10:43 +01001301 mv_xor_channel_remove(xordev->channels[i]);
Thomas Petazzonif7d12ef2012-11-15 16:47:58 +01001302 if (pdev->dev.of_node)
1303 irq_dispose_mapping(xordev->channels[i]->irq);
Thomas Petazzonif7d12ef2012-11-15 16:47:58 +01001304 }
Thomas Petazzoni60d151f2012-10-29 16:54:49 +01001305
Thomas Petazzonidab92062013-01-06 11:10:44 +01001306 if (!IS_ERR(xordev->clk)) {
1307 clk_disable_unprepare(xordev->clk);
1308 clk_put(xordev->clk);
1309 }
1310
Thomas Petazzoni60d151f2012-10-29 16:54:49 +01001311 return ret;
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001312}
1313
Thomas Petazzoni61971652012-10-30 12:05:40 +01001314static struct platform_driver mv_xor_driver = {
1315 .probe = mv_xor_probe,
Thomas Petazzoni8b648432015-12-22 11:43:29 +01001316 .suspend = mv_xor_suspend,
1317 .resume = mv_xor_resume,
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001318 .driver = {
Thomas Petazzonif7d12ef2012-11-15 16:47:58 +01001319 .name = MV_XOR_NAME,
1320 .of_match_table = of_match_ptr(mv_xor_dt_ids),
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001321 },
1322};
1323
1324
1325static int __init mv_xor_init(void)
1326{
Thomas Petazzoni61971652012-10-30 12:05:40 +01001327 return platform_driver_register(&mv_xor_driver);
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001328}
Paul Gortmaker25cf68d2015-08-21 16:27:49 -04001329device_initcall(mv_xor_init);
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001330
Paul Gortmaker25cf68d2015-08-21 16:27:49 -04001331/*
Saeed Bisharaff7b0472008-07-08 11:58:36 -07001332MODULE_AUTHOR("Saeed Bishara <saeed@marvell.com>");
1333MODULE_DESCRIPTION("DMA engine driver for Marvell's XOR engine");
1334MODULE_LICENSE("GPL");
Paul Gortmaker25cf68d2015-08-21 16:27:49 -04001335*/