Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 17 | #include <linux/slab.h> |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 18 | #include <linux/delay.h> |
| 19 | #include <linux/dma-mapping.h> |
| 20 | #include <linux/spinlock.h> |
| 21 | #include <linux/interrupt.h> |
| 22 | #include <linux/platform_device.h> |
| 23 | #include <linux/memory.h> |
Andrew Lunn | c510182 | 2012-02-19 13:30:26 +0100 | [diff] [blame] | 24 | #include <linux/clk.h> |
Thomas Petazzoni | f7d12ef | 2012-11-15 16:47:58 +0100 | [diff] [blame] | 25 | #include <linux/of.h> |
| 26 | #include <linux/of_irq.h> |
| 27 | #include <linux/irqdomain.h> |
Arnd Bergmann | c02cecb | 2012-08-24 15:21:54 +0200 | [diff] [blame] | 28 | #include <linux/platform_data/dma-mv_xor.h> |
Russell King - ARM Linux | d2ebfb3 | 2012-03-06 22:34:26 +0000 | [diff] [blame] | 29 | |
| 30 | #include "dmaengine.h" |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 31 | #include "mv_xor.h" |
| 32 | |
| 33 | static void mv_xor_issue_pending(struct dma_chan *chan); |
| 34 | |
| 35 | #define to_mv_xor_chan(chan) \ |
Thomas Petazzoni | 98817b9 | 2012-11-15 14:57:44 +0100 | [diff] [blame] | 36 | container_of(chan, struct mv_xor_chan, dmachan) |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 37 | |
| 38 | #define to_mv_xor_slot(tx) \ |
| 39 | container_of(tx, struct mv_xor_desc_slot, async_tx) |
| 40 | |
Thomas Petazzoni | c98c178 | 2012-11-15 14:17:18 +0100 | [diff] [blame] | 41 | #define mv_chan_to_devp(chan) \ |
Thomas Petazzoni | 1ef48a2 | 2012-11-15 15:17:05 +0100 | [diff] [blame] | 42 | ((chan)->dmadev.dev) |
Thomas Petazzoni | c98c178 | 2012-11-15 14:17:18 +0100 | [diff] [blame] | 43 | |
Lior Amsalem | dfc9766 | 2014-08-27 10:52:51 -0300 | [diff] [blame] | 44 | static void mv_desc_init(struct mv_xor_desc_slot *desc, |
Lior Amsalem | ba87d13 | 2014-08-27 10:52:53 -0300 | [diff] [blame] | 45 | dma_addr_t addr, u32 byte_count, |
| 46 | enum dma_ctrl_flags flags) |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 47 | { |
| 48 | struct mv_xor_desc *hw_desc = desc->hw_desc; |
| 49 | |
Ezequiel Garcia | 0e7488e | 2014-08-27 10:52:52 -0300 | [diff] [blame] | 50 | hw_desc->status = XOR_DESC_DMA_OWNED; |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 51 | hw_desc->phy_next_desc = 0; |
Lior Amsalem | ba87d13 | 2014-08-27 10:52:53 -0300 | [diff] [blame] | 52 | /* Enable end-of-descriptor interrupts only for DMA_PREP_INTERRUPT */ |
| 53 | hw_desc->desc_command = (flags & DMA_PREP_INTERRUPT) ? |
| 54 | XOR_DESC_EOD_INT_EN : 0; |
Lior Amsalem | dfc9766 | 2014-08-27 10:52:51 -0300 | [diff] [blame] | 55 | hw_desc->phy_dest_addr = addr; |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 56 | hw_desc->byte_count = byte_count; |
| 57 | } |
| 58 | |
| 59 | static void mv_desc_set_next_desc(struct mv_xor_desc_slot *desc, |
| 60 | u32 next_desc_addr) |
| 61 | { |
| 62 | struct mv_xor_desc *hw_desc = desc->hw_desc; |
| 63 | BUG_ON(hw_desc->phy_next_desc); |
| 64 | hw_desc->phy_next_desc = next_desc_addr; |
| 65 | } |
| 66 | |
| 67 | static void mv_desc_clear_next_desc(struct mv_xor_desc_slot *desc) |
| 68 | { |
| 69 | struct mv_xor_desc *hw_desc = desc->hw_desc; |
| 70 | hw_desc->phy_next_desc = 0; |
| 71 | } |
| 72 | |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 73 | static void mv_desc_set_src_addr(struct mv_xor_desc_slot *desc, |
| 74 | int index, dma_addr_t addr) |
| 75 | { |
| 76 | struct mv_xor_desc *hw_desc = desc->hw_desc; |
Thomas Petazzoni | e03bc65 | 2013-07-29 17:42:14 +0200 | [diff] [blame] | 77 | hw_desc->phy_src_addr[mv_phy_src_idx(index)] = addr; |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 78 | if (desc->type == DMA_XOR) |
| 79 | hw_desc->desc_command |= (1 << index); |
| 80 | } |
| 81 | |
| 82 | static u32 mv_chan_get_current_desc(struct mv_xor_chan *chan) |
| 83 | { |
Thomas Petazzoni | 5733c38 | 2013-07-29 17:42:13 +0200 | [diff] [blame] | 84 | return readl_relaxed(XOR_CURR_DESC(chan)); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | static void mv_chan_set_next_descriptor(struct mv_xor_chan *chan, |
| 88 | u32 next_desc_addr) |
| 89 | { |
Thomas Petazzoni | 5733c38 | 2013-07-29 17:42:13 +0200 | [diff] [blame] | 90 | writel_relaxed(next_desc_addr, XOR_NEXT_DESC(chan)); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 93 | static void mv_chan_unmask_interrupts(struct mv_xor_chan *chan) |
| 94 | { |
Thomas Petazzoni | 5733c38 | 2013-07-29 17:42:13 +0200 | [diff] [blame] | 95 | u32 val = readl_relaxed(XOR_INTR_MASK(chan)); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 96 | val |= XOR_INTR_MASK_VALUE << (chan->idx * 16); |
Thomas Petazzoni | 5733c38 | 2013-07-29 17:42:13 +0200 | [diff] [blame] | 97 | writel_relaxed(val, XOR_INTR_MASK(chan)); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | static u32 mv_chan_get_intr_cause(struct mv_xor_chan *chan) |
| 101 | { |
Thomas Petazzoni | 5733c38 | 2013-07-29 17:42:13 +0200 | [diff] [blame] | 102 | u32 intr_cause = readl_relaxed(XOR_INTR_CAUSE(chan)); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 103 | intr_cause = (intr_cause >> (chan->idx * 16)) & 0xFFFF; |
| 104 | return intr_cause; |
| 105 | } |
| 106 | |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 107 | static void mv_xor_device_clear_eoc_cause(struct mv_xor_chan *chan) |
| 108 | { |
Lior Amsalem | ba87d13 | 2014-08-27 10:52:53 -0300 | [diff] [blame] | 109 | u32 val; |
| 110 | |
| 111 | val = XOR_INT_END_OF_DESC | XOR_INT_END_OF_CHAIN | XOR_INT_STOPPED; |
| 112 | val = ~(val << (chan->idx * 16)); |
Thomas Petazzoni | c98c178 | 2012-11-15 14:17:18 +0100 | [diff] [blame] | 113 | dev_dbg(mv_chan_to_devp(chan), "%s, val 0x%08x\n", __func__, val); |
Thomas Petazzoni | 5733c38 | 2013-07-29 17:42:13 +0200 | [diff] [blame] | 114 | writel_relaxed(val, XOR_INTR_CAUSE(chan)); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | static void mv_xor_device_clear_err_status(struct mv_xor_chan *chan) |
| 118 | { |
| 119 | u32 val = 0xFFFF0000 >> (chan->idx * 16); |
Thomas Petazzoni | 5733c38 | 2013-07-29 17:42:13 +0200 | [diff] [blame] | 120 | writel_relaxed(val, XOR_INTR_CAUSE(chan)); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 123 | static void mv_set_mode(struct mv_xor_chan *chan, |
| 124 | enum dma_transaction_type type) |
| 125 | { |
| 126 | u32 op_mode; |
Thomas Petazzoni | 5733c38 | 2013-07-29 17:42:13 +0200 | [diff] [blame] | 127 | u32 config = readl_relaxed(XOR_CONFIG(chan)); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 128 | |
| 129 | switch (type) { |
| 130 | case DMA_XOR: |
| 131 | op_mode = XOR_OPERATION_MODE_XOR; |
| 132 | break; |
| 133 | case DMA_MEMCPY: |
| 134 | op_mode = XOR_OPERATION_MODE_MEMCPY; |
| 135 | break; |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 136 | default: |
Thomas Petazzoni | c98c178 | 2012-11-15 14:17:18 +0100 | [diff] [blame] | 137 | dev_err(mv_chan_to_devp(chan), |
Joe Perches | 1ba151c | 2012-10-28 01:05:44 -0700 | [diff] [blame] | 138 | "error: unsupported operation %d\n", |
Thomas Petazzoni | a3fc74b | 2012-11-15 12:50:27 +0100 | [diff] [blame] | 139 | type); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 140 | BUG(); |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | config &= ~0x7; |
| 145 | config |= op_mode; |
Thomas Petazzoni | e03bc65 | 2013-07-29 17:42:14 +0200 | [diff] [blame] | 146 | |
| 147 | #if defined(__BIG_ENDIAN) |
| 148 | config |= XOR_DESCRIPTOR_SWAP; |
| 149 | #else |
| 150 | config &= ~XOR_DESCRIPTOR_SWAP; |
| 151 | #endif |
| 152 | |
Thomas Petazzoni | 5733c38 | 2013-07-29 17:42:13 +0200 | [diff] [blame] | 153 | writel_relaxed(config, XOR_CONFIG(chan)); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 154 | chan->current_type = type; |
| 155 | } |
| 156 | |
| 157 | static void mv_chan_activate(struct mv_xor_chan *chan) |
| 158 | { |
Thomas Petazzoni | c98c178 | 2012-11-15 14:17:18 +0100 | [diff] [blame] | 159 | dev_dbg(mv_chan_to_devp(chan), " activate chan.\n"); |
Ezequiel Garcia | 5a9a55b | 2014-05-21 14:02:35 -0700 | [diff] [blame] | 160 | |
| 161 | /* writel ensures all descriptors are flushed before activation */ |
| 162 | writel(BIT(0), XOR_ACTIVATION(chan)); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | static char mv_chan_is_busy(struct mv_xor_chan *chan) |
| 166 | { |
Thomas Petazzoni | 5733c38 | 2013-07-29 17:42:13 +0200 | [diff] [blame] | 167 | u32 state = readl_relaxed(XOR_ACTIVATION(chan)); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 168 | |
| 169 | state = (state >> 4) & 0x3; |
| 170 | |
| 171 | return (state == 1) ? 1 : 0; |
| 172 | } |
| 173 | |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 174 | /** |
| 175 | * mv_xor_free_slots - flags descriptor slots for reuse |
| 176 | * @slot: Slot to free |
| 177 | * Caller must hold &mv_chan->lock while calling this function |
| 178 | */ |
| 179 | static void mv_xor_free_slots(struct mv_xor_chan *mv_chan, |
| 180 | struct mv_xor_desc_slot *slot) |
| 181 | { |
Thomas Petazzoni | c98c178 | 2012-11-15 14:17:18 +0100 | [diff] [blame] | 182 | dev_dbg(mv_chan_to_devp(mv_chan), "%s %d slot %p\n", |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 183 | __func__, __LINE__, slot); |
| 184 | |
Lior Amsalem | dfc9766 | 2014-08-27 10:52:51 -0300 | [diff] [blame] | 185 | slot->slot_used = 0; |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 186 | |
| 187 | } |
| 188 | |
| 189 | /* |
| 190 | * mv_xor_start_new_chain - program the engine to operate on new chain headed by |
| 191 | * sw_desc |
| 192 | * Caller must hold &mv_chan->lock while calling this function |
| 193 | */ |
| 194 | static void mv_xor_start_new_chain(struct mv_xor_chan *mv_chan, |
| 195 | struct mv_xor_desc_slot *sw_desc) |
| 196 | { |
Thomas Petazzoni | c98c178 | 2012-11-15 14:17:18 +0100 | [diff] [blame] | 197 | dev_dbg(mv_chan_to_devp(mv_chan), "%s %d: sw_desc %p\n", |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 198 | __func__, __LINE__, sw_desc); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 199 | |
Bartlomiej Zolnierkiewicz | 48a9db4 | 2013-07-03 15:05:06 -0700 | [diff] [blame] | 200 | /* set the hardware chain */ |
| 201 | mv_chan_set_next_descriptor(mv_chan, sw_desc->async_tx.phys); |
| 202 | |
Lior Amsalem | dfc9766 | 2014-08-27 10:52:51 -0300 | [diff] [blame] | 203 | mv_chan->pending++; |
Thomas Petazzoni | 98817b9 | 2012-11-15 14:57:44 +0100 | [diff] [blame] | 204 | mv_xor_issue_pending(&mv_chan->dmachan); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | static dma_cookie_t |
| 208 | mv_xor_run_tx_complete_actions(struct mv_xor_desc_slot *desc, |
| 209 | struct mv_xor_chan *mv_chan, dma_cookie_t cookie) |
| 210 | { |
| 211 | BUG_ON(desc->async_tx.cookie < 0); |
| 212 | |
| 213 | if (desc->async_tx.cookie > 0) { |
| 214 | cookie = desc->async_tx.cookie; |
| 215 | |
| 216 | /* call the callback (must not sleep or submit new |
| 217 | * operations to this channel) |
| 218 | */ |
| 219 | if (desc->async_tx.callback) |
| 220 | desc->async_tx.callback( |
| 221 | desc->async_tx.callback_param); |
| 222 | |
Dan Williams | d38a8c6 | 2013-10-18 19:35:23 +0200 | [diff] [blame] | 223 | dma_descriptor_unmap(&desc->async_tx); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | /* run dependent operations */ |
Dan Williams | 07f2211 | 2009-01-05 17:14:31 -0700 | [diff] [blame] | 227 | dma_run_dependencies(&desc->async_tx); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 228 | |
| 229 | return cookie; |
| 230 | } |
| 231 | |
| 232 | static int |
| 233 | mv_xor_clean_completed_slots(struct mv_xor_chan *mv_chan) |
| 234 | { |
| 235 | struct mv_xor_desc_slot *iter, *_iter; |
| 236 | |
Thomas Petazzoni | c98c178 | 2012-11-15 14:17:18 +0100 | [diff] [blame] | 237 | dev_dbg(mv_chan_to_devp(mv_chan), "%s %d\n", __func__, __LINE__); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 238 | list_for_each_entry_safe(iter, _iter, &mv_chan->completed_slots, |
| 239 | completed_node) { |
| 240 | |
| 241 | if (async_tx_test_ack(&iter->async_tx)) { |
| 242 | list_del(&iter->completed_node); |
| 243 | mv_xor_free_slots(mv_chan, iter); |
| 244 | } |
| 245 | } |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | static int |
| 250 | mv_xor_clean_slot(struct mv_xor_desc_slot *desc, |
| 251 | struct mv_xor_chan *mv_chan) |
| 252 | { |
Thomas Petazzoni | c98c178 | 2012-11-15 14:17:18 +0100 | [diff] [blame] | 253 | dev_dbg(mv_chan_to_devp(mv_chan), "%s %d: desc %p flags %d\n", |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 254 | __func__, __LINE__, desc, desc->async_tx.flags); |
| 255 | list_del(&desc->chain_node); |
| 256 | /* the client is allowed to attach dependent operations |
| 257 | * until 'ack' is set |
| 258 | */ |
| 259 | if (!async_tx_test_ack(&desc->async_tx)) { |
| 260 | /* move this slot to the completed_slots */ |
| 261 | list_add_tail(&desc->completed_node, &mv_chan->completed_slots); |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | mv_xor_free_slots(mv_chan, desc); |
| 266 | return 0; |
| 267 | } |
| 268 | |
Ezequiel Garcia | fbeec99 | 2014-03-07 16:46:47 -0300 | [diff] [blame] | 269 | /* This function must be called with the mv_xor_chan spinlock held */ |
| 270 | static void mv_xor_slot_cleanup(struct mv_xor_chan *mv_chan) |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 271 | { |
| 272 | struct mv_xor_desc_slot *iter, *_iter; |
| 273 | dma_cookie_t cookie = 0; |
| 274 | int busy = mv_chan_is_busy(mv_chan); |
| 275 | u32 current_desc = mv_chan_get_current_desc(mv_chan); |
Lior Amsalem | 9136291 | 2015-05-26 15:07:32 +0200 | [diff] [blame^] | 276 | int current_cleaned = 0; |
| 277 | struct mv_xor_desc *hw_desc; |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 278 | |
Thomas Petazzoni | c98c178 | 2012-11-15 14:17:18 +0100 | [diff] [blame] | 279 | dev_dbg(mv_chan_to_devp(mv_chan), "%s %d\n", __func__, __LINE__); |
| 280 | dev_dbg(mv_chan_to_devp(mv_chan), "current_desc %x\n", current_desc); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 281 | mv_xor_clean_completed_slots(mv_chan); |
| 282 | |
| 283 | /* free completed slots from the chain starting with |
| 284 | * the oldest descriptor |
| 285 | */ |
| 286 | |
| 287 | list_for_each_entry_safe(iter, _iter, &mv_chan->chain, |
| 288 | chain_node) { |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 289 | |
Lior Amsalem | 9136291 | 2015-05-26 15:07:32 +0200 | [diff] [blame^] | 290 | /* clean finished descriptors */ |
| 291 | hw_desc = iter->hw_desc; |
| 292 | if (hw_desc->status & XOR_DESC_SUCCESS) { |
| 293 | cookie = mv_xor_run_tx_complete_actions(iter, mv_chan, |
| 294 | cookie); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 295 | |
Lior Amsalem | 9136291 | 2015-05-26 15:07:32 +0200 | [diff] [blame^] | 296 | /* done processing desc, clean slot */ |
| 297 | mv_xor_clean_slot(iter, mv_chan); |
| 298 | |
| 299 | /* break if we did cleaned the current */ |
| 300 | if (iter->async_tx.phys == current_desc) { |
| 301 | current_cleaned = 1; |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 302 | break; |
Lior Amsalem | 9136291 | 2015-05-26 15:07:32 +0200 | [diff] [blame^] | 303 | } |
| 304 | } else { |
| 305 | if (iter->async_tx.phys == current_desc) { |
| 306 | current_cleaned = 0; |
| 307 | break; |
| 308 | } |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 309 | } |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | if ((busy == 0) && !list_empty(&mv_chan->chain)) { |
Lior Amsalem | 9136291 | 2015-05-26 15:07:32 +0200 | [diff] [blame^] | 313 | if (current_cleaned) { |
| 314 | /* |
| 315 | * current descriptor cleaned and removed, run |
| 316 | * from list head |
| 317 | */ |
| 318 | iter = list_entry(mv_chan->chain.next, |
| 319 | struct mv_xor_desc_slot, |
| 320 | chain_node); |
| 321 | mv_xor_start_new_chain(mv_chan, iter); |
| 322 | } else { |
| 323 | if (!list_is_last(&iter->chain_node, &mv_chan->chain)) { |
| 324 | /* |
| 325 | * descriptors are still waiting after |
| 326 | * current, trigger them |
| 327 | */ |
| 328 | iter = list_entry(iter->chain_node.next, |
| 329 | struct mv_xor_desc_slot, |
| 330 | chain_node); |
| 331 | mv_xor_start_new_chain(mv_chan, iter); |
| 332 | } else { |
| 333 | /* |
| 334 | * some descriptors are still waiting |
| 335 | * to be cleaned |
| 336 | */ |
| 337 | tasklet_schedule(&mv_chan->irq_tasklet); |
| 338 | } |
| 339 | } |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | if (cookie > 0) |
Thomas Petazzoni | 98817b9 | 2012-11-15 14:57:44 +0100 | [diff] [blame] | 343 | mv_chan->dmachan.completed_cookie = cookie; |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 344 | } |
| 345 | |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 346 | static void mv_xor_tasklet(unsigned long data) |
| 347 | { |
| 348 | struct mv_xor_chan *chan = (struct mv_xor_chan *) data; |
Ezequiel Garcia | e43147a | 2014-03-07 16:46:46 -0300 | [diff] [blame] | 349 | |
| 350 | spin_lock_bh(&chan->lock); |
Saeed Bishara | 8333f65 | 2010-12-21 16:53:39 +0200 | [diff] [blame] | 351 | mv_xor_slot_cleanup(chan); |
Ezequiel Garcia | e43147a | 2014-03-07 16:46:46 -0300 | [diff] [blame] | 352 | spin_unlock_bh(&chan->lock); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | static struct mv_xor_desc_slot * |
Lior Amsalem | dfc9766 | 2014-08-27 10:52:51 -0300 | [diff] [blame] | 356 | mv_xor_alloc_slot(struct mv_xor_chan *mv_chan) |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 357 | { |
Lior Amsalem | dfc9766 | 2014-08-27 10:52:51 -0300 | [diff] [blame] | 358 | struct mv_xor_desc_slot *iter, *_iter; |
| 359 | int retry = 0; |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 360 | |
| 361 | /* start search from the last allocated descrtiptor |
| 362 | * if a contiguous allocation can not be found start searching |
| 363 | * from the beginning of the list |
| 364 | */ |
| 365 | retry: |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 366 | if (retry == 0) |
| 367 | iter = mv_chan->last_used; |
| 368 | else |
| 369 | iter = list_entry(&mv_chan->all_slots, |
| 370 | struct mv_xor_desc_slot, |
| 371 | slot_node); |
| 372 | |
| 373 | list_for_each_entry_safe_continue( |
| 374 | iter, _iter, &mv_chan->all_slots, slot_node) { |
Lior Amsalem | dfc9766 | 2014-08-27 10:52:51 -0300 | [diff] [blame] | 375 | |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 376 | prefetch(_iter); |
| 377 | prefetch(&_iter->async_tx); |
Lior Amsalem | dfc9766 | 2014-08-27 10:52:51 -0300 | [diff] [blame] | 378 | if (iter->slot_used) { |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 379 | /* give up after finding the first busy slot |
| 380 | * on the second pass through the list |
| 381 | */ |
| 382 | if (retry) |
| 383 | break; |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 384 | continue; |
| 385 | } |
| 386 | |
Lior Amsalem | dfc9766 | 2014-08-27 10:52:51 -0300 | [diff] [blame] | 387 | /* pre-ack descriptor */ |
| 388 | async_tx_ack(&iter->async_tx); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 389 | |
Lior Amsalem | dfc9766 | 2014-08-27 10:52:51 -0300 | [diff] [blame] | 390 | iter->slot_used = 1; |
| 391 | INIT_LIST_HEAD(&iter->chain_node); |
| 392 | iter->async_tx.cookie = -EBUSY; |
| 393 | mv_chan->last_used = iter; |
| 394 | mv_desc_clear_next_desc(iter); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 395 | |
Lior Amsalem | dfc9766 | 2014-08-27 10:52:51 -0300 | [diff] [blame] | 396 | return iter; |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 397 | |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 398 | } |
| 399 | if (!retry++) |
| 400 | goto retry; |
| 401 | |
| 402 | /* try to free some slots if the allocation fails */ |
| 403 | tasklet_schedule(&mv_chan->irq_tasklet); |
| 404 | |
| 405 | return NULL; |
| 406 | } |
| 407 | |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 408 | /************************ DMA engine API functions ****************************/ |
| 409 | static dma_cookie_t |
| 410 | mv_xor_tx_submit(struct dma_async_tx_descriptor *tx) |
| 411 | { |
| 412 | struct mv_xor_desc_slot *sw_desc = to_mv_xor_slot(tx); |
| 413 | struct mv_xor_chan *mv_chan = to_mv_xor_chan(tx->chan); |
Lior Amsalem | dfc9766 | 2014-08-27 10:52:51 -0300 | [diff] [blame] | 414 | struct mv_xor_desc_slot *old_chain_tail; |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 415 | dma_cookie_t cookie; |
| 416 | int new_hw_chain = 1; |
| 417 | |
Thomas Petazzoni | c98c178 | 2012-11-15 14:17:18 +0100 | [diff] [blame] | 418 | dev_dbg(mv_chan_to_devp(mv_chan), |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 419 | "%s sw_desc %p: async_tx %p\n", |
| 420 | __func__, sw_desc, &sw_desc->async_tx); |
| 421 | |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 422 | spin_lock_bh(&mv_chan->lock); |
Russell King - ARM Linux | 884485e | 2012-03-06 22:34:46 +0000 | [diff] [blame] | 423 | cookie = dma_cookie_assign(tx); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 424 | |
| 425 | if (list_empty(&mv_chan->chain)) |
Lior Amsalem | dfc9766 | 2014-08-27 10:52:51 -0300 | [diff] [blame] | 426 | list_add_tail(&sw_desc->chain_node, &mv_chan->chain); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 427 | else { |
| 428 | new_hw_chain = 0; |
| 429 | |
| 430 | old_chain_tail = list_entry(mv_chan->chain.prev, |
| 431 | struct mv_xor_desc_slot, |
| 432 | chain_node); |
Lior Amsalem | dfc9766 | 2014-08-27 10:52:51 -0300 | [diff] [blame] | 433 | list_add_tail(&sw_desc->chain_node, &mv_chan->chain); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 434 | |
Olof Johansson | 31fd8f5 | 2014-02-03 17:13:23 -0800 | [diff] [blame] | 435 | dev_dbg(mv_chan_to_devp(mv_chan), "Append to last desc %pa\n", |
| 436 | &old_chain_tail->async_tx.phys); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 437 | |
| 438 | /* fix up the hardware chain */ |
Lior Amsalem | dfc9766 | 2014-08-27 10:52:51 -0300 | [diff] [blame] | 439 | mv_desc_set_next_desc(old_chain_tail, sw_desc->async_tx.phys); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 440 | |
| 441 | /* if the channel is not busy */ |
| 442 | if (!mv_chan_is_busy(mv_chan)) { |
| 443 | u32 current_desc = mv_chan_get_current_desc(mv_chan); |
| 444 | /* |
| 445 | * and the curren desc is the end of the chain before |
| 446 | * the append, then we need to start the channel |
| 447 | */ |
| 448 | if (current_desc == old_chain_tail->async_tx.phys) |
| 449 | new_hw_chain = 1; |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | if (new_hw_chain) |
Lior Amsalem | dfc9766 | 2014-08-27 10:52:51 -0300 | [diff] [blame] | 454 | mv_xor_start_new_chain(mv_chan, sw_desc); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 455 | |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 456 | spin_unlock_bh(&mv_chan->lock); |
| 457 | |
| 458 | return cookie; |
| 459 | } |
| 460 | |
| 461 | /* returns the number of allocated descriptors */ |
Dan Williams | aa1e6f1 | 2009-01-06 11:38:17 -0700 | [diff] [blame] | 462 | static int mv_xor_alloc_chan_resources(struct dma_chan *chan) |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 463 | { |
Olof Johansson | 31fd8f5 | 2014-02-03 17:13:23 -0800 | [diff] [blame] | 464 | void *virt_desc; |
| 465 | dma_addr_t dma_desc; |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 466 | int idx; |
| 467 | struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan); |
| 468 | struct mv_xor_desc_slot *slot = NULL; |
Thomas Petazzoni | b503fa0 | 2012-11-15 15:55:30 +0100 | [diff] [blame] | 469 | int num_descs_in_pool = MV_XOR_POOL_SIZE/MV_XOR_SLOT_SIZE; |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 470 | |
| 471 | /* Allocate descriptor slots */ |
| 472 | idx = mv_chan->slots_allocated; |
| 473 | while (idx < num_descs_in_pool) { |
| 474 | slot = kzalloc(sizeof(*slot), GFP_KERNEL); |
| 475 | if (!slot) { |
Ezequiel Garcia | b8291dd | 2014-08-27 10:52:49 -0300 | [diff] [blame] | 476 | dev_info(mv_chan_to_devp(mv_chan), |
| 477 | "channel only initialized %d descriptor slots", |
| 478 | idx); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 479 | break; |
| 480 | } |
Olof Johansson | 31fd8f5 | 2014-02-03 17:13:23 -0800 | [diff] [blame] | 481 | virt_desc = mv_chan->dma_desc_pool_virt; |
| 482 | slot->hw_desc = virt_desc + idx * MV_XOR_SLOT_SIZE; |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 483 | |
| 484 | dma_async_tx_descriptor_init(&slot->async_tx, chan); |
| 485 | slot->async_tx.tx_submit = mv_xor_tx_submit; |
| 486 | INIT_LIST_HEAD(&slot->chain_node); |
| 487 | INIT_LIST_HEAD(&slot->slot_node); |
Olof Johansson | 31fd8f5 | 2014-02-03 17:13:23 -0800 | [diff] [blame] | 488 | dma_desc = mv_chan->dma_desc_pool; |
| 489 | slot->async_tx.phys = dma_desc + idx * MV_XOR_SLOT_SIZE; |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 490 | slot->idx = idx++; |
| 491 | |
| 492 | spin_lock_bh(&mv_chan->lock); |
| 493 | mv_chan->slots_allocated = idx; |
| 494 | list_add_tail(&slot->slot_node, &mv_chan->all_slots); |
| 495 | spin_unlock_bh(&mv_chan->lock); |
| 496 | } |
| 497 | |
| 498 | if (mv_chan->slots_allocated && !mv_chan->last_used) |
| 499 | mv_chan->last_used = list_entry(mv_chan->all_slots.next, |
| 500 | struct mv_xor_desc_slot, |
| 501 | slot_node); |
| 502 | |
Thomas Petazzoni | c98c178 | 2012-11-15 14:17:18 +0100 | [diff] [blame] | 503 | dev_dbg(mv_chan_to_devp(mv_chan), |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 504 | "allocated %d descriptor slots last_used: %p\n", |
| 505 | mv_chan->slots_allocated, mv_chan->last_used); |
| 506 | |
| 507 | return mv_chan->slots_allocated ? : -ENOMEM; |
| 508 | } |
| 509 | |
| 510 | static struct dma_async_tx_descriptor * |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 511 | mv_xor_prep_dma_xor(struct dma_chan *chan, dma_addr_t dest, dma_addr_t *src, |
| 512 | unsigned int src_cnt, size_t len, unsigned long flags) |
| 513 | { |
| 514 | struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan); |
Lior Amsalem | dfc9766 | 2014-08-27 10:52:51 -0300 | [diff] [blame] | 515 | struct mv_xor_desc_slot *sw_desc; |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 516 | |
| 517 | if (unlikely(len < MV_XOR_MIN_BYTE_COUNT)) |
| 518 | return NULL; |
| 519 | |
Coly Li | 7912d30 | 2011-03-27 01:26:53 +0800 | [diff] [blame] | 520 | BUG_ON(len > MV_XOR_MAX_BYTE_COUNT); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 521 | |
Thomas Petazzoni | c98c178 | 2012-11-15 14:17:18 +0100 | [diff] [blame] | 522 | dev_dbg(mv_chan_to_devp(mv_chan), |
Olof Johansson | 31fd8f5 | 2014-02-03 17:13:23 -0800 | [diff] [blame] | 523 | "%s src_cnt: %d len: %u dest %pad flags: %ld\n", |
| 524 | __func__, src_cnt, len, &dest, flags); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 525 | |
| 526 | spin_lock_bh(&mv_chan->lock); |
Lior Amsalem | dfc9766 | 2014-08-27 10:52:51 -0300 | [diff] [blame] | 527 | sw_desc = mv_xor_alloc_slot(mv_chan); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 528 | if (sw_desc) { |
| 529 | sw_desc->type = DMA_XOR; |
| 530 | sw_desc->async_tx.flags = flags; |
Lior Amsalem | ba87d13 | 2014-08-27 10:52:53 -0300 | [diff] [blame] | 531 | mv_desc_init(sw_desc, dest, len, flags); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 532 | while (src_cnt--) |
Lior Amsalem | dfc9766 | 2014-08-27 10:52:51 -0300 | [diff] [blame] | 533 | mv_desc_set_src_addr(sw_desc, src_cnt, src[src_cnt]); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 534 | } |
| 535 | spin_unlock_bh(&mv_chan->lock); |
Thomas Petazzoni | c98c178 | 2012-11-15 14:17:18 +0100 | [diff] [blame] | 536 | dev_dbg(mv_chan_to_devp(mv_chan), |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 537 | "%s sw_desc %p async_tx %p \n", |
| 538 | __func__, sw_desc, &sw_desc->async_tx); |
| 539 | return sw_desc ? &sw_desc->async_tx : NULL; |
| 540 | } |
| 541 | |
Lior Amsalem | 3e4f52e | 2014-08-27 10:52:50 -0300 | [diff] [blame] | 542 | static struct dma_async_tx_descriptor * |
| 543 | mv_xor_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src, |
| 544 | size_t len, unsigned long flags) |
| 545 | { |
| 546 | /* |
| 547 | * A MEMCPY operation is identical to an XOR operation with only |
| 548 | * a single source address. |
| 549 | */ |
| 550 | return mv_xor_prep_dma_xor(chan, dest, &src, 1, len, flags); |
| 551 | } |
| 552 | |
Lior Amsalem | 2284354 | 2014-08-27 10:52:55 -0300 | [diff] [blame] | 553 | static struct dma_async_tx_descriptor * |
| 554 | mv_xor_prep_dma_interrupt(struct dma_chan *chan, unsigned long flags) |
| 555 | { |
| 556 | struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan); |
| 557 | dma_addr_t src, dest; |
| 558 | size_t len; |
| 559 | |
| 560 | src = mv_chan->dummy_src_addr; |
| 561 | dest = mv_chan->dummy_dst_addr; |
| 562 | len = MV_XOR_MIN_BYTE_COUNT; |
| 563 | |
| 564 | /* |
| 565 | * We implement the DMA_INTERRUPT operation as a minimum sized |
| 566 | * XOR operation with a single dummy source address. |
| 567 | */ |
| 568 | return mv_xor_prep_dma_xor(chan, dest, &src, 1, len, flags); |
| 569 | } |
| 570 | |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 571 | static void mv_xor_free_chan_resources(struct dma_chan *chan) |
| 572 | { |
| 573 | struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan); |
| 574 | struct mv_xor_desc_slot *iter, *_iter; |
| 575 | int in_use_descs = 0; |
| 576 | |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 577 | spin_lock_bh(&mv_chan->lock); |
Ezequiel Garcia | e43147a | 2014-03-07 16:46:46 -0300 | [diff] [blame] | 578 | |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 579 | mv_xor_slot_cleanup(mv_chan); |
| 580 | |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 581 | list_for_each_entry_safe(iter, _iter, &mv_chan->chain, |
| 582 | chain_node) { |
| 583 | in_use_descs++; |
| 584 | list_del(&iter->chain_node); |
| 585 | } |
| 586 | list_for_each_entry_safe(iter, _iter, &mv_chan->completed_slots, |
| 587 | completed_node) { |
| 588 | in_use_descs++; |
| 589 | list_del(&iter->completed_node); |
| 590 | } |
| 591 | list_for_each_entry_safe_reverse( |
| 592 | iter, _iter, &mv_chan->all_slots, slot_node) { |
| 593 | list_del(&iter->slot_node); |
| 594 | kfree(iter); |
| 595 | mv_chan->slots_allocated--; |
| 596 | } |
| 597 | mv_chan->last_used = NULL; |
| 598 | |
Thomas Petazzoni | c98c178 | 2012-11-15 14:17:18 +0100 | [diff] [blame] | 599 | dev_dbg(mv_chan_to_devp(mv_chan), "%s slots_allocated %d\n", |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 600 | __func__, mv_chan->slots_allocated); |
| 601 | spin_unlock_bh(&mv_chan->lock); |
| 602 | |
| 603 | if (in_use_descs) |
Thomas Petazzoni | c98c178 | 2012-11-15 14:17:18 +0100 | [diff] [blame] | 604 | dev_err(mv_chan_to_devp(mv_chan), |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 605 | "freeing %d in use descriptors!\n", in_use_descs); |
| 606 | } |
| 607 | |
| 608 | /** |
Linus Walleij | 0793448 | 2010-03-26 16:50:49 -0700 | [diff] [blame] | 609 | * mv_xor_status - poll the status of an XOR transaction |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 610 | * @chan: XOR channel handle |
| 611 | * @cookie: XOR transaction identifier |
Linus Walleij | 0793448 | 2010-03-26 16:50:49 -0700 | [diff] [blame] | 612 | * @txstate: XOR transactions state holder (or NULL) |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 613 | */ |
Linus Walleij | 0793448 | 2010-03-26 16:50:49 -0700 | [diff] [blame] | 614 | static enum dma_status mv_xor_status(struct dma_chan *chan, |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 615 | dma_cookie_t cookie, |
Linus Walleij | 0793448 | 2010-03-26 16:50:49 -0700 | [diff] [blame] | 616 | struct dma_tx_state *txstate) |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 617 | { |
| 618 | struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 619 | enum dma_status ret; |
| 620 | |
Russell King - ARM Linux | 96a2af4 | 2012-03-06 22:35:27 +0000 | [diff] [blame] | 621 | ret = dma_cookie_status(chan, cookie, txstate); |
Ezequiel Garcia | 890766d | 2014-03-07 16:46:45 -0300 | [diff] [blame] | 622 | if (ret == DMA_COMPLETE) |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 623 | return ret; |
Ezequiel Garcia | e43147a | 2014-03-07 16:46:46 -0300 | [diff] [blame] | 624 | |
| 625 | spin_lock_bh(&mv_chan->lock); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 626 | mv_xor_slot_cleanup(mv_chan); |
Ezequiel Garcia | e43147a | 2014-03-07 16:46:46 -0300 | [diff] [blame] | 627 | spin_unlock_bh(&mv_chan->lock); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 628 | |
Russell King - ARM Linux | 96a2af4 | 2012-03-06 22:35:27 +0000 | [diff] [blame] | 629 | return dma_cookie_status(chan, cookie, txstate); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | static void mv_dump_xor_regs(struct mv_xor_chan *chan) |
| 633 | { |
| 634 | u32 val; |
| 635 | |
Thomas Petazzoni | 5733c38 | 2013-07-29 17:42:13 +0200 | [diff] [blame] | 636 | val = readl_relaxed(XOR_CONFIG(chan)); |
Joe Perches | 1ba151c | 2012-10-28 01:05:44 -0700 | [diff] [blame] | 637 | dev_err(mv_chan_to_devp(chan), "config 0x%08x\n", val); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 638 | |
Thomas Petazzoni | 5733c38 | 2013-07-29 17:42:13 +0200 | [diff] [blame] | 639 | val = readl_relaxed(XOR_ACTIVATION(chan)); |
Joe Perches | 1ba151c | 2012-10-28 01:05:44 -0700 | [diff] [blame] | 640 | dev_err(mv_chan_to_devp(chan), "activation 0x%08x\n", val); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 641 | |
Thomas Petazzoni | 5733c38 | 2013-07-29 17:42:13 +0200 | [diff] [blame] | 642 | val = readl_relaxed(XOR_INTR_CAUSE(chan)); |
Joe Perches | 1ba151c | 2012-10-28 01:05:44 -0700 | [diff] [blame] | 643 | dev_err(mv_chan_to_devp(chan), "intr cause 0x%08x\n", val); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 644 | |
Thomas Petazzoni | 5733c38 | 2013-07-29 17:42:13 +0200 | [diff] [blame] | 645 | val = readl_relaxed(XOR_INTR_MASK(chan)); |
Joe Perches | 1ba151c | 2012-10-28 01:05:44 -0700 | [diff] [blame] | 646 | dev_err(mv_chan_to_devp(chan), "intr mask 0x%08x\n", val); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 647 | |
Thomas Petazzoni | 5733c38 | 2013-07-29 17:42:13 +0200 | [diff] [blame] | 648 | val = readl_relaxed(XOR_ERROR_CAUSE(chan)); |
Joe Perches | 1ba151c | 2012-10-28 01:05:44 -0700 | [diff] [blame] | 649 | dev_err(mv_chan_to_devp(chan), "error cause 0x%08x\n", val); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 650 | |
Thomas Petazzoni | 5733c38 | 2013-07-29 17:42:13 +0200 | [diff] [blame] | 651 | val = readl_relaxed(XOR_ERROR_ADDR(chan)); |
Joe Perches | 1ba151c | 2012-10-28 01:05:44 -0700 | [diff] [blame] | 652 | dev_err(mv_chan_to_devp(chan), "error addr 0x%08x\n", val); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | static void mv_xor_err_interrupt_handler(struct mv_xor_chan *chan, |
| 656 | u32 intr_cause) |
| 657 | { |
Ezequiel Garcia | 0e7488e | 2014-08-27 10:52:52 -0300 | [diff] [blame] | 658 | if (intr_cause & XOR_INT_ERR_DECODE) { |
| 659 | dev_dbg(mv_chan_to_devp(chan), "ignoring address decode error\n"); |
| 660 | return; |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 661 | } |
| 662 | |
Ezequiel Garcia | 0e7488e | 2014-08-27 10:52:52 -0300 | [diff] [blame] | 663 | dev_err(mv_chan_to_devp(chan), "error on chan %d. intr cause 0x%08x\n", |
Thomas Petazzoni | a3fc74b | 2012-11-15 12:50:27 +0100 | [diff] [blame] | 664 | chan->idx, intr_cause); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 665 | |
| 666 | mv_dump_xor_regs(chan); |
Ezequiel Garcia | 0e7488e | 2014-08-27 10:52:52 -0300 | [diff] [blame] | 667 | WARN_ON(1); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | static irqreturn_t mv_xor_interrupt_handler(int irq, void *data) |
| 671 | { |
| 672 | struct mv_xor_chan *chan = data; |
| 673 | u32 intr_cause = mv_chan_get_intr_cause(chan); |
| 674 | |
Thomas Petazzoni | c98c178 | 2012-11-15 14:17:18 +0100 | [diff] [blame] | 675 | dev_dbg(mv_chan_to_devp(chan), "intr cause %x\n", intr_cause); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 676 | |
Ezequiel Garcia | 0e7488e | 2014-08-27 10:52:52 -0300 | [diff] [blame] | 677 | if (intr_cause & XOR_INTR_ERRORS) |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 678 | mv_xor_err_interrupt_handler(chan, intr_cause); |
| 679 | |
| 680 | tasklet_schedule(&chan->irq_tasklet); |
| 681 | |
| 682 | mv_xor_device_clear_eoc_cause(chan); |
| 683 | |
| 684 | return IRQ_HANDLED; |
| 685 | } |
| 686 | |
| 687 | static void mv_xor_issue_pending(struct dma_chan *chan) |
| 688 | { |
| 689 | struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan); |
| 690 | |
| 691 | if (mv_chan->pending >= MV_XOR_THRESHOLD) { |
| 692 | mv_chan->pending = 0; |
| 693 | mv_chan_activate(mv_chan); |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | /* |
| 698 | * Perform a transaction to verify the HW works. |
| 699 | */ |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 700 | |
Linus Torvalds | c271433 | 2012-12-14 14:54:26 -0800 | [diff] [blame] | 701 | static int mv_xor_memcpy_self_test(struct mv_xor_chan *mv_chan) |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 702 | { |
Ezequiel Garcia | b8c01d2 | 2013-12-10 09:32:37 -0300 | [diff] [blame] | 703 | int i, ret; |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 704 | void *src, *dest; |
| 705 | dma_addr_t src_dma, dest_dma; |
| 706 | struct dma_chan *dma_chan; |
| 707 | dma_cookie_t cookie; |
| 708 | struct dma_async_tx_descriptor *tx; |
Ezequiel Garcia | d16695a | 2013-12-10 09:32:36 -0300 | [diff] [blame] | 709 | struct dmaengine_unmap_data *unmap; |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 710 | int err = 0; |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 711 | |
Ezequiel Garcia | d16695a | 2013-12-10 09:32:36 -0300 | [diff] [blame] | 712 | src = kmalloc(sizeof(u8) * PAGE_SIZE, GFP_KERNEL); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 713 | if (!src) |
| 714 | return -ENOMEM; |
| 715 | |
Ezequiel Garcia | d16695a | 2013-12-10 09:32:36 -0300 | [diff] [blame] | 716 | dest = kzalloc(sizeof(u8) * PAGE_SIZE, GFP_KERNEL); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 717 | if (!dest) { |
| 718 | kfree(src); |
| 719 | return -ENOMEM; |
| 720 | } |
| 721 | |
| 722 | /* Fill in src buffer */ |
Ezequiel Garcia | d16695a | 2013-12-10 09:32:36 -0300 | [diff] [blame] | 723 | for (i = 0; i < PAGE_SIZE; i++) |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 724 | ((u8 *) src)[i] = (u8)i; |
| 725 | |
Thomas Petazzoni | 275cc0c | 2012-11-15 15:09:42 +0100 | [diff] [blame] | 726 | dma_chan = &mv_chan->dmachan; |
Dan Williams | aa1e6f1 | 2009-01-06 11:38:17 -0700 | [diff] [blame] | 727 | if (mv_xor_alloc_chan_resources(dma_chan) < 1) { |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 728 | err = -ENODEV; |
| 729 | goto out; |
| 730 | } |
| 731 | |
Ezequiel Garcia | d16695a | 2013-12-10 09:32:36 -0300 | [diff] [blame] | 732 | unmap = dmaengine_get_unmap_data(dma_chan->device->dev, 2, GFP_KERNEL); |
| 733 | if (!unmap) { |
| 734 | err = -ENOMEM; |
| 735 | goto free_resources; |
| 736 | } |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 737 | |
Ezequiel Garcia | d16695a | 2013-12-10 09:32:36 -0300 | [diff] [blame] | 738 | src_dma = dma_map_page(dma_chan->device->dev, virt_to_page(src), 0, |
| 739 | PAGE_SIZE, DMA_TO_DEVICE); |
Ezequiel Garcia | d16695a | 2013-12-10 09:32:36 -0300 | [diff] [blame] | 740 | unmap->addr[0] = src_dma; |
| 741 | |
Ezequiel Garcia | b8c01d2 | 2013-12-10 09:32:37 -0300 | [diff] [blame] | 742 | ret = dma_mapping_error(dma_chan->device->dev, src_dma); |
| 743 | if (ret) { |
| 744 | err = -ENOMEM; |
| 745 | goto free_resources; |
| 746 | } |
| 747 | unmap->to_cnt = 1; |
| 748 | |
Ezequiel Garcia | d16695a | 2013-12-10 09:32:36 -0300 | [diff] [blame] | 749 | dest_dma = dma_map_page(dma_chan->device->dev, virt_to_page(dest), 0, |
| 750 | PAGE_SIZE, DMA_FROM_DEVICE); |
Ezequiel Garcia | d16695a | 2013-12-10 09:32:36 -0300 | [diff] [blame] | 751 | unmap->addr[1] = dest_dma; |
| 752 | |
Ezequiel Garcia | b8c01d2 | 2013-12-10 09:32:37 -0300 | [diff] [blame] | 753 | ret = dma_mapping_error(dma_chan->device->dev, dest_dma); |
| 754 | if (ret) { |
| 755 | err = -ENOMEM; |
| 756 | goto free_resources; |
| 757 | } |
| 758 | unmap->from_cnt = 1; |
Ezequiel Garcia | d16695a | 2013-12-10 09:32:36 -0300 | [diff] [blame] | 759 | unmap->len = PAGE_SIZE; |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 760 | |
| 761 | tx = mv_xor_prep_dma_memcpy(dma_chan, dest_dma, src_dma, |
Ezequiel Garcia | d16695a | 2013-12-10 09:32:36 -0300 | [diff] [blame] | 762 | PAGE_SIZE, 0); |
Ezequiel Garcia | b8c01d2 | 2013-12-10 09:32:37 -0300 | [diff] [blame] | 763 | if (!tx) { |
| 764 | dev_err(dma_chan->device->dev, |
| 765 | "Self-test cannot prepare operation, disabling\n"); |
| 766 | err = -ENODEV; |
| 767 | goto free_resources; |
| 768 | } |
| 769 | |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 770 | cookie = mv_xor_tx_submit(tx); |
Ezequiel Garcia | b8c01d2 | 2013-12-10 09:32:37 -0300 | [diff] [blame] | 771 | if (dma_submit_error(cookie)) { |
| 772 | dev_err(dma_chan->device->dev, |
| 773 | "Self-test submit error, disabling\n"); |
| 774 | err = -ENODEV; |
| 775 | goto free_resources; |
| 776 | } |
| 777 | |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 778 | mv_xor_issue_pending(dma_chan); |
| 779 | async_tx_ack(tx); |
| 780 | msleep(1); |
| 781 | |
Linus Walleij | 0793448 | 2010-03-26 16:50:49 -0700 | [diff] [blame] | 782 | if (mv_xor_status(dma_chan, cookie, NULL) != |
Vinod Koul | b3efb8f | 2013-10-16 20:51:04 +0530 | [diff] [blame] | 783 | DMA_COMPLETE) { |
Thomas Petazzoni | a3fc74b | 2012-11-15 12:50:27 +0100 | [diff] [blame] | 784 | dev_err(dma_chan->device->dev, |
| 785 | "Self-test copy timed out, disabling\n"); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 786 | err = -ENODEV; |
| 787 | goto free_resources; |
| 788 | } |
| 789 | |
Thomas Petazzoni | c35064c | 2012-11-15 13:01:59 +0100 | [diff] [blame] | 790 | dma_sync_single_for_cpu(dma_chan->device->dev, dest_dma, |
Ezequiel Garcia | d16695a | 2013-12-10 09:32:36 -0300 | [diff] [blame] | 791 | PAGE_SIZE, DMA_FROM_DEVICE); |
| 792 | if (memcmp(src, dest, PAGE_SIZE)) { |
Thomas Petazzoni | a3fc74b | 2012-11-15 12:50:27 +0100 | [diff] [blame] | 793 | dev_err(dma_chan->device->dev, |
| 794 | "Self-test copy failed compare, disabling\n"); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 795 | err = -ENODEV; |
| 796 | goto free_resources; |
| 797 | } |
| 798 | |
| 799 | free_resources: |
Ezequiel Garcia | d16695a | 2013-12-10 09:32:36 -0300 | [diff] [blame] | 800 | dmaengine_unmap_put(unmap); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 801 | mv_xor_free_chan_resources(dma_chan); |
| 802 | out: |
| 803 | kfree(src); |
| 804 | kfree(dest); |
| 805 | return err; |
| 806 | } |
| 807 | |
| 808 | #define MV_XOR_NUM_SRC_TEST 4 /* must be <= 15 */ |
Bill Pemberton | 463a1f8 | 2012-11-19 13:22:55 -0500 | [diff] [blame] | 809 | static int |
Thomas Petazzoni | 275cc0c | 2012-11-15 15:09:42 +0100 | [diff] [blame] | 810 | mv_xor_xor_self_test(struct mv_xor_chan *mv_chan) |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 811 | { |
Ezequiel Garcia | b8c01d2 | 2013-12-10 09:32:37 -0300 | [diff] [blame] | 812 | int i, src_idx, ret; |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 813 | struct page *dest; |
| 814 | struct page *xor_srcs[MV_XOR_NUM_SRC_TEST]; |
| 815 | dma_addr_t dma_srcs[MV_XOR_NUM_SRC_TEST]; |
| 816 | dma_addr_t dest_dma; |
| 817 | struct dma_async_tx_descriptor *tx; |
Ezequiel Garcia | d16695a | 2013-12-10 09:32:36 -0300 | [diff] [blame] | 818 | struct dmaengine_unmap_data *unmap; |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 819 | struct dma_chan *dma_chan; |
| 820 | dma_cookie_t cookie; |
| 821 | u8 cmp_byte = 0; |
| 822 | u32 cmp_word; |
| 823 | int err = 0; |
Ezequiel Garcia | d16695a | 2013-12-10 09:32:36 -0300 | [diff] [blame] | 824 | int src_count = MV_XOR_NUM_SRC_TEST; |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 825 | |
Ezequiel Garcia | d16695a | 2013-12-10 09:32:36 -0300 | [diff] [blame] | 826 | for (src_idx = 0; src_idx < src_count; src_idx++) { |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 827 | xor_srcs[src_idx] = alloc_page(GFP_KERNEL); |
Roel Kluin | a09b09a | 2009-02-25 13:56:21 +0100 | [diff] [blame] | 828 | if (!xor_srcs[src_idx]) { |
| 829 | while (src_idx--) |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 830 | __free_page(xor_srcs[src_idx]); |
Roel Kluin | a09b09a | 2009-02-25 13:56:21 +0100 | [diff] [blame] | 831 | return -ENOMEM; |
| 832 | } |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | dest = alloc_page(GFP_KERNEL); |
Roel Kluin | a09b09a | 2009-02-25 13:56:21 +0100 | [diff] [blame] | 836 | if (!dest) { |
| 837 | while (src_idx--) |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 838 | __free_page(xor_srcs[src_idx]); |
Roel Kluin | a09b09a | 2009-02-25 13:56:21 +0100 | [diff] [blame] | 839 | return -ENOMEM; |
| 840 | } |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 841 | |
| 842 | /* Fill in src buffers */ |
Ezequiel Garcia | d16695a | 2013-12-10 09:32:36 -0300 | [diff] [blame] | 843 | for (src_idx = 0; src_idx < src_count; src_idx++) { |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 844 | u8 *ptr = page_address(xor_srcs[src_idx]); |
| 845 | for (i = 0; i < PAGE_SIZE; i++) |
| 846 | ptr[i] = (1 << src_idx); |
| 847 | } |
| 848 | |
Ezequiel Garcia | d16695a | 2013-12-10 09:32:36 -0300 | [diff] [blame] | 849 | for (src_idx = 0; src_idx < src_count; src_idx++) |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 850 | cmp_byte ^= (u8) (1 << src_idx); |
| 851 | |
| 852 | cmp_word = (cmp_byte << 24) | (cmp_byte << 16) | |
| 853 | (cmp_byte << 8) | cmp_byte; |
| 854 | |
| 855 | memset(page_address(dest), 0, PAGE_SIZE); |
| 856 | |
Thomas Petazzoni | 275cc0c | 2012-11-15 15:09:42 +0100 | [diff] [blame] | 857 | dma_chan = &mv_chan->dmachan; |
Dan Williams | aa1e6f1 | 2009-01-06 11:38:17 -0700 | [diff] [blame] | 858 | if (mv_xor_alloc_chan_resources(dma_chan) < 1) { |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 859 | err = -ENODEV; |
| 860 | goto out; |
| 861 | } |
| 862 | |
Ezequiel Garcia | d16695a | 2013-12-10 09:32:36 -0300 | [diff] [blame] | 863 | unmap = dmaengine_get_unmap_data(dma_chan->device->dev, src_count + 1, |
| 864 | GFP_KERNEL); |
| 865 | if (!unmap) { |
| 866 | err = -ENOMEM; |
| 867 | goto free_resources; |
| 868 | } |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 869 | |
Ezequiel Garcia | d16695a | 2013-12-10 09:32:36 -0300 | [diff] [blame] | 870 | /* test xor */ |
| 871 | for (i = 0; i < src_count; i++) { |
| 872 | unmap->addr[i] = dma_map_page(dma_chan->device->dev, xor_srcs[i], |
| 873 | 0, PAGE_SIZE, DMA_TO_DEVICE); |
| 874 | dma_srcs[i] = unmap->addr[i]; |
Ezequiel Garcia | b8c01d2 | 2013-12-10 09:32:37 -0300 | [diff] [blame] | 875 | ret = dma_mapping_error(dma_chan->device->dev, unmap->addr[i]); |
| 876 | if (ret) { |
| 877 | err = -ENOMEM; |
| 878 | goto free_resources; |
| 879 | } |
Ezequiel Garcia | d16695a | 2013-12-10 09:32:36 -0300 | [diff] [blame] | 880 | unmap->to_cnt++; |
| 881 | } |
| 882 | |
| 883 | unmap->addr[src_count] = dma_map_page(dma_chan->device->dev, dest, 0, PAGE_SIZE, |
| 884 | DMA_FROM_DEVICE); |
| 885 | dest_dma = unmap->addr[src_count]; |
Ezequiel Garcia | b8c01d2 | 2013-12-10 09:32:37 -0300 | [diff] [blame] | 886 | ret = dma_mapping_error(dma_chan->device->dev, unmap->addr[src_count]); |
| 887 | if (ret) { |
| 888 | err = -ENOMEM; |
| 889 | goto free_resources; |
| 890 | } |
Ezequiel Garcia | d16695a | 2013-12-10 09:32:36 -0300 | [diff] [blame] | 891 | unmap->from_cnt = 1; |
| 892 | unmap->len = PAGE_SIZE; |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 893 | |
| 894 | tx = mv_xor_prep_dma_xor(dma_chan, dest_dma, dma_srcs, |
Ezequiel Garcia | d16695a | 2013-12-10 09:32:36 -0300 | [diff] [blame] | 895 | src_count, PAGE_SIZE, 0); |
Ezequiel Garcia | b8c01d2 | 2013-12-10 09:32:37 -0300 | [diff] [blame] | 896 | if (!tx) { |
| 897 | dev_err(dma_chan->device->dev, |
| 898 | "Self-test cannot prepare operation, disabling\n"); |
| 899 | err = -ENODEV; |
| 900 | goto free_resources; |
| 901 | } |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 902 | |
| 903 | cookie = mv_xor_tx_submit(tx); |
Ezequiel Garcia | b8c01d2 | 2013-12-10 09:32:37 -0300 | [diff] [blame] | 904 | if (dma_submit_error(cookie)) { |
| 905 | dev_err(dma_chan->device->dev, |
| 906 | "Self-test submit error, disabling\n"); |
| 907 | err = -ENODEV; |
| 908 | goto free_resources; |
| 909 | } |
| 910 | |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 911 | mv_xor_issue_pending(dma_chan); |
| 912 | async_tx_ack(tx); |
| 913 | msleep(8); |
| 914 | |
Linus Walleij | 0793448 | 2010-03-26 16:50:49 -0700 | [diff] [blame] | 915 | if (mv_xor_status(dma_chan, cookie, NULL) != |
Vinod Koul | b3efb8f | 2013-10-16 20:51:04 +0530 | [diff] [blame] | 916 | DMA_COMPLETE) { |
Thomas Petazzoni | a3fc74b | 2012-11-15 12:50:27 +0100 | [diff] [blame] | 917 | dev_err(dma_chan->device->dev, |
| 918 | "Self-test xor timed out, disabling\n"); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 919 | err = -ENODEV; |
| 920 | goto free_resources; |
| 921 | } |
| 922 | |
Thomas Petazzoni | c35064c | 2012-11-15 13:01:59 +0100 | [diff] [blame] | 923 | dma_sync_single_for_cpu(dma_chan->device->dev, dest_dma, |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 924 | PAGE_SIZE, DMA_FROM_DEVICE); |
| 925 | for (i = 0; i < (PAGE_SIZE / sizeof(u32)); i++) { |
| 926 | u32 *ptr = page_address(dest); |
| 927 | if (ptr[i] != cmp_word) { |
Thomas Petazzoni | a3fc74b | 2012-11-15 12:50:27 +0100 | [diff] [blame] | 928 | dev_err(dma_chan->device->dev, |
Joe Perches | 1ba151c | 2012-10-28 01:05:44 -0700 | [diff] [blame] | 929 | "Self-test xor failed compare, disabling. index %d, data %x, expected %x\n", |
| 930 | i, ptr[i], cmp_word); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 931 | err = -ENODEV; |
| 932 | goto free_resources; |
| 933 | } |
| 934 | } |
| 935 | |
| 936 | free_resources: |
Ezequiel Garcia | d16695a | 2013-12-10 09:32:36 -0300 | [diff] [blame] | 937 | dmaengine_unmap_put(unmap); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 938 | mv_xor_free_chan_resources(dma_chan); |
| 939 | out: |
Ezequiel Garcia | d16695a | 2013-12-10 09:32:36 -0300 | [diff] [blame] | 940 | src_idx = src_count; |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 941 | while (src_idx--) |
| 942 | __free_page(xor_srcs[src_idx]); |
| 943 | __free_page(dest); |
| 944 | return err; |
| 945 | } |
| 946 | |
Thomas Petazzoni | 1ef48a2 | 2012-11-15 15:17:05 +0100 | [diff] [blame] | 947 | static int mv_xor_channel_remove(struct mv_xor_chan *mv_chan) |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 948 | { |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 949 | struct dma_chan *chan, *_chan; |
Thomas Petazzoni | 1ef48a2 | 2012-11-15 15:17:05 +0100 | [diff] [blame] | 950 | struct device *dev = mv_chan->dmadev.dev; |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 951 | |
Thomas Petazzoni | 1ef48a2 | 2012-11-15 15:17:05 +0100 | [diff] [blame] | 952 | dma_async_device_unregister(&mv_chan->dmadev); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 953 | |
Thomas Petazzoni | b503fa0 | 2012-11-15 15:55:30 +0100 | [diff] [blame] | 954 | dma_free_coherent(dev, MV_XOR_POOL_SIZE, |
Thomas Petazzoni | 1ef48a2 | 2012-11-15 15:17:05 +0100 | [diff] [blame] | 955 | mv_chan->dma_desc_pool_virt, mv_chan->dma_desc_pool); |
Lior Amsalem | 2284354 | 2014-08-27 10:52:55 -0300 | [diff] [blame] | 956 | dma_unmap_single(dev, mv_chan->dummy_src_addr, |
| 957 | MV_XOR_MIN_BYTE_COUNT, DMA_FROM_DEVICE); |
| 958 | dma_unmap_single(dev, mv_chan->dummy_dst_addr, |
| 959 | MV_XOR_MIN_BYTE_COUNT, DMA_TO_DEVICE); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 960 | |
Thomas Petazzoni | 1ef48a2 | 2012-11-15 15:17:05 +0100 | [diff] [blame] | 961 | list_for_each_entry_safe(chan, _chan, &mv_chan->dmadev.channels, |
Thomas Petazzoni | a6b4a9d | 2012-10-29 16:45:46 +0100 | [diff] [blame] | 962 | device_node) { |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 963 | list_del(&chan->device_node); |
| 964 | } |
| 965 | |
Thomas Petazzoni | 88eb92c | 2012-11-15 16:11:18 +0100 | [diff] [blame] | 966 | free_irq(mv_chan->irq, mv_chan); |
| 967 | |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 968 | return 0; |
| 969 | } |
| 970 | |
Thomas Petazzoni | 1ef48a2 | 2012-11-15 15:17:05 +0100 | [diff] [blame] | 971 | static struct mv_xor_chan * |
Thomas Petazzoni | 297eedba | 2012-11-15 15:29:53 +0100 | [diff] [blame] | 972 | mv_xor_channel_add(struct mv_xor_device *xordev, |
Thomas Petazzoni | a6b4a9d | 2012-10-29 16:45:46 +0100 | [diff] [blame] | 973 | struct platform_device *pdev, |
Thomas Petazzoni | b503fa0 | 2012-11-15 15:55:30 +0100 | [diff] [blame] | 974 | int idx, dma_cap_mask_t cap_mask, int irq) |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 975 | { |
| 976 | int ret = 0; |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 977 | struct mv_xor_chan *mv_chan; |
| 978 | struct dma_device *dma_dev; |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 979 | |
Thomas Petazzoni | 1ef48a2 | 2012-11-15 15:17:05 +0100 | [diff] [blame] | 980 | mv_chan = devm_kzalloc(&pdev->dev, sizeof(*mv_chan), GFP_KERNEL); |
Sachin Kamat | a577659 | 2013-09-02 13:54:20 +0530 | [diff] [blame] | 981 | if (!mv_chan) |
| 982 | return ERR_PTR(-ENOMEM); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 983 | |
Thomas Petazzoni | 9aedbdb | 2012-11-15 15:36:37 +0100 | [diff] [blame] | 984 | mv_chan->idx = idx; |
Thomas Petazzoni | 88eb92c | 2012-11-15 16:11:18 +0100 | [diff] [blame] | 985 | mv_chan->irq = irq; |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 986 | |
Thomas Petazzoni | 1ef48a2 | 2012-11-15 15:17:05 +0100 | [diff] [blame] | 987 | dma_dev = &mv_chan->dmadev; |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 988 | |
Lior Amsalem | 2284354 | 2014-08-27 10:52:55 -0300 | [diff] [blame] | 989 | /* |
| 990 | * These source and destination dummy buffers are used to implement |
| 991 | * a DMA_INTERRUPT operation as a minimum-sized XOR operation. |
| 992 | * Hence, we only need to map the buffers at initialization-time. |
| 993 | */ |
| 994 | mv_chan->dummy_src_addr = dma_map_single(dma_dev->dev, |
| 995 | mv_chan->dummy_src, MV_XOR_MIN_BYTE_COUNT, DMA_FROM_DEVICE); |
| 996 | mv_chan->dummy_dst_addr = dma_map_single(dma_dev->dev, |
| 997 | mv_chan->dummy_dst, MV_XOR_MIN_BYTE_COUNT, DMA_TO_DEVICE); |
| 998 | |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 999 | /* allocate coherent memory for hardware descriptors |
| 1000 | * note: writecombine gives slightly better performance, but |
| 1001 | * requires that we explicitly flush the writes |
| 1002 | */ |
Thomas Petazzoni | 1ef48a2 | 2012-11-15 15:17:05 +0100 | [diff] [blame] | 1003 | mv_chan->dma_desc_pool_virt = |
Thomas Petazzoni | b503fa0 | 2012-11-15 15:55:30 +0100 | [diff] [blame] | 1004 | dma_alloc_writecombine(&pdev->dev, MV_XOR_POOL_SIZE, |
Thomas Petazzoni | 1ef48a2 | 2012-11-15 15:17:05 +0100 | [diff] [blame] | 1005 | &mv_chan->dma_desc_pool, GFP_KERNEL); |
| 1006 | if (!mv_chan->dma_desc_pool_virt) |
Thomas Petazzoni | a6b4a9d | 2012-10-29 16:45:46 +0100 | [diff] [blame] | 1007 | return ERR_PTR(-ENOMEM); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 1008 | |
| 1009 | /* discover transaction capabilites from the platform data */ |
Thomas Petazzoni | a6b4a9d | 2012-10-29 16:45:46 +0100 | [diff] [blame] | 1010 | dma_dev->cap_mask = cap_mask; |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 1011 | |
| 1012 | INIT_LIST_HEAD(&dma_dev->channels); |
| 1013 | |
| 1014 | /* set base routines */ |
| 1015 | dma_dev->device_alloc_chan_resources = mv_xor_alloc_chan_resources; |
| 1016 | dma_dev->device_free_chan_resources = mv_xor_free_chan_resources; |
Linus Walleij | 0793448 | 2010-03-26 16:50:49 -0700 | [diff] [blame] | 1017 | dma_dev->device_tx_status = mv_xor_status; |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 1018 | dma_dev->device_issue_pending = mv_xor_issue_pending; |
| 1019 | dma_dev->dev = &pdev->dev; |
| 1020 | |
| 1021 | /* set prep routines based on capability */ |
Lior Amsalem | 2284354 | 2014-08-27 10:52:55 -0300 | [diff] [blame] | 1022 | if (dma_has_cap(DMA_INTERRUPT, dma_dev->cap_mask)) |
| 1023 | dma_dev->device_prep_dma_interrupt = mv_xor_prep_dma_interrupt; |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 1024 | if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask)) |
| 1025 | dma_dev->device_prep_dma_memcpy = mv_xor_prep_dma_memcpy; |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 1026 | if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) { |
Joe Perches | c019894 | 2009-06-28 09:26:21 -0700 | [diff] [blame] | 1027 | dma_dev->max_xor = 8; |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 1028 | dma_dev->device_prep_dma_xor = mv_xor_prep_dma_xor; |
| 1029 | } |
| 1030 | |
Thomas Petazzoni | 297eedba | 2012-11-15 15:29:53 +0100 | [diff] [blame] | 1031 | mv_chan->mmr_base = xordev->xor_base; |
Ezequiel Garcia | 82a1402 | 2013-10-30 12:01:43 -0300 | [diff] [blame] | 1032 | mv_chan->mmr_high_base = xordev->xor_high_base; |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 1033 | tasklet_init(&mv_chan->irq_tasklet, mv_xor_tasklet, (unsigned long) |
| 1034 | mv_chan); |
| 1035 | |
| 1036 | /* clear errors before enabling interrupts */ |
| 1037 | mv_xor_device_clear_err_status(mv_chan); |
| 1038 | |
Thomas Petazzoni | 2d0a074 | 2012-11-22 18:19:09 +0100 | [diff] [blame] | 1039 | ret = request_irq(mv_chan->irq, mv_xor_interrupt_handler, |
| 1040 | 0, dev_name(&pdev->dev), mv_chan); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 1041 | if (ret) |
| 1042 | goto err_free_dma; |
| 1043 | |
| 1044 | mv_chan_unmask_interrupts(mv_chan); |
| 1045 | |
Lior Amsalem | 3e4f52e | 2014-08-27 10:52:50 -0300 | [diff] [blame] | 1046 | mv_set_mode(mv_chan, DMA_XOR); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 1047 | |
| 1048 | spin_lock_init(&mv_chan->lock); |
| 1049 | INIT_LIST_HEAD(&mv_chan->chain); |
| 1050 | INIT_LIST_HEAD(&mv_chan->completed_slots); |
| 1051 | INIT_LIST_HEAD(&mv_chan->all_slots); |
Thomas Petazzoni | 98817b9 | 2012-11-15 14:57:44 +0100 | [diff] [blame] | 1052 | mv_chan->dmachan.device = dma_dev; |
| 1053 | dma_cookie_init(&mv_chan->dmachan); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 1054 | |
Thomas Petazzoni | 98817b9 | 2012-11-15 14:57:44 +0100 | [diff] [blame] | 1055 | list_add_tail(&mv_chan->dmachan.device_node, &dma_dev->channels); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 1056 | |
| 1057 | if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask)) { |
Thomas Petazzoni | 275cc0c | 2012-11-15 15:09:42 +0100 | [diff] [blame] | 1058 | ret = mv_xor_memcpy_self_test(mv_chan); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 1059 | dev_dbg(&pdev->dev, "memcpy self test returned %d\n", ret); |
| 1060 | if (ret) |
Thomas Petazzoni | 2d0a074 | 2012-11-22 18:19:09 +0100 | [diff] [blame] | 1061 | goto err_free_irq; |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 1062 | } |
| 1063 | |
| 1064 | if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) { |
Thomas Petazzoni | 275cc0c | 2012-11-15 15:09:42 +0100 | [diff] [blame] | 1065 | ret = mv_xor_xor_self_test(mv_chan); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 1066 | dev_dbg(&pdev->dev, "xor self test returned %d\n", ret); |
| 1067 | if (ret) |
Thomas Petazzoni | 2d0a074 | 2012-11-22 18:19:09 +0100 | [diff] [blame] | 1068 | goto err_free_irq; |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 1069 | } |
| 1070 | |
Bartlomiej Zolnierkiewicz | 48a9db4 | 2013-07-03 15:05:06 -0700 | [diff] [blame] | 1071 | dev_info(&pdev->dev, "Marvell XOR: ( %s%s%s)\n", |
Joe Perches | 1ba151c | 2012-10-28 01:05:44 -0700 | [diff] [blame] | 1072 | dma_has_cap(DMA_XOR, dma_dev->cap_mask) ? "xor " : "", |
Joe Perches | 1ba151c | 2012-10-28 01:05:44 -0700 | [diff] [blame] | 1073 | dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask) ? "cpy " : "", |
| 1074 | dma_has_cap(DMA_INTERRUPT, dma_dev->cap_mask) ? "intr " : ""); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 1075 | |
| 1076 | dma_async_device_register(dma_dev); |
Thomas Petazzoni | 1ef48a2 | 2012-11-15 15:17:05 +0100 | [diff] [blame] | 1077 | return mv_chan; |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 1078 | |
Thomas Petazzoni | 2d0a074 | 2012-11-22 18:19:09 +0100 | [diff] [blame] | 1079 | err_free_irq: |
| 1080 | free_irq(mv_chan->irq, mv_chan); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 1081 | err_free_dma: |
Thomas Petazzoni | b503fa0 | 2012-11-15 15:55:30 +0100 | [diff] [blame] | 1082 | dma_free_coherent(&pdev->dev, MV_XOR_POOL_SIZE, |
Thomas Petazzoni | 1ef48a2 | 2012-11-15 15:17:05 +0100 | [diff] [blame] | 1083 | mv_chan->dma_desc_pool_virt, mv_chan->dma_desc_pool); |
Thomas Petazzoni | a6b4a9d | 2012-10-29 16:45:46 +0100 | [diff] [blame] | 1084 | return ERR_PTR(ret); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 1085 | } |
| 1086 | |
| 1087 | static void |
Thomas Petazzoni | 297eedba | 2012-11-15 15:29:53 +0100 | [diff] [blame] | 1088 | mv_xor_conf_mbus_windows(struct mv_xor_device *xordev, |
Andrew Lunn | 63a9332 | 2011-12-07 21:48:07 +0100 | [diff] [blame] | 1089 | const struct mbus_dram_target_info *dram) |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 1090 | { |
Ezequiel Garcia | 82a1402 | 2013-10-30 12:01:43 -0300 | [diff] [blame] | 1091 | void __iomem *base = xordev->xor_high_base; |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 1092 | u32 win_enable = 0; |
| 1093 | int i; |
| 1094 | |
| 1095 | for (i = 0; i < 8; i++) { |
| 1096 | writel(0, base + WINDOW_BASE(i)); |
| 1097 | writel(0, base + WINDOW_SIZE(i)); |
| 1098 | if (i < 4) |
| 1099 | writel(0, base + WINDOW_REMAP_HIGH(i)); |
| 1100 | } |
| 1101 | |
| 1102 | for (i = 0; i < dram->num_cs; i++) { |
Andrew Lunn | 63a9332 | 2011-12-07 21:48:07 +0100 | [diff] [blame] | 1103 | const struct mbus_dram_window *cs = dram->cs + i; |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 1104 | |
| 1105 | writel((cs->base & 0xffff0000) | |
| 1106 | (cs->mbus_attr << 8) | |
| 1107 | dram->mbus_dram_target_id, base + WINDOW_BASE(i)); |
| 1108 | writel((cs->size - 1) & 0xffff0000, base + WINDOW_SIZE(i)); |
| 1109 | |
| 1110 | win_enable |= (1 << i); |
| 1111 | win_enable |= 3 << (16 + (2 * i)); |
| 1112 | } |
| 1113 | |
| 1114 | writel(win_enable, base + WINDOW_BAR_ENABLE(0)); |
| 1115 | writel(win_enable, base + WINDOW_BAR_ENABLE(1)); |
Thomas Petazzoni | c4b4b73 | 2012-11-22 18:16:37 +0100 | [diff] [blame] | 1116 | writel(0, base + WINDOW_OVERRIDE_CTRL(0)); |
| 1117 | writel(0, base + WINDOW_OVERRIDE_CTRL(1)); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 1118 | } |
| 1119 | |
Linus Torvalds | c271433 | 2012-12-14 14:54:26 -0800 | [diff] [blame] | 1120 | static int mv_xor_probe(struct platform_device *pdev) |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 1121 | { |
Andrew Lunn | 63a9332 | 2011-12-07 21:48:07 +0100 | [diff] [blame] | 1122 | const struct mbus_dram_target_info *dram; |
Thomas Petazzoni | 297eedba | 2012-11-15 15:29:53 +0100 | [diff] [blame] | 1123 | struct mv_xor_device *xordev; |
Jingoo Han | d4adcc0 | 2013-07-30 17:09:11 +0900 | [diff] [blame] | 1124 | struct mv_xor_platform_data *pdata = dev_get_platdata(&pdev->dev); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 1125 | struct resource *res; |
Thomas Petazzoni | 60d151f | 2012-10-29 16:54:49 +0100 | [diff] [blame] | 1126 | int i, ret; |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 1127 | |
Joe Perches | 1ba151c | 2012-10-28 01:05:44 -0700 | [diff] [blame] | 1128 | dev_notice(&pdev->dev, "Marvell shared XOR driver\n"); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 1129 | |
Thomas Petazzoni | 297eedba | 2012-11-15 15:29:53 +0100 | [diff] [blame] | 1130 | xordev = devm_kzalloc(&pdev->dev, sizeof(*xordev), GFP_KERNEL); |
| 1131 | if (!xordev) |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 1132 | return -ENOMEM; |
| 1133 | |
| 1134 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1135 | if (!res) |
| 1136 | return -ENODEV; |
| 1137 | |
Thomas Petazzoni | 297eedba | 2012-11-15 15:29:53 +0100 | [diff] [blame] | 1138 | xordev->xor_base = devm_ioremap(&pdev->dev, res->start, |
| 1139 | resource_size(res)); |
| 1140 | if (!xordev->xor_base) |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 1141 | return -EBUSY; |
| 1142 | |
| 1143 | res = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
| 1144 | if (!res) |
| 1145 | return -ENODEV; |
| 1146 | |
Thomas Petazzoni | 297eedba | 2012-11-15 15:29:53 +0100 | [diff] [blame] | 1147 | xordev->xor_high_base = devm_ioremap(&pdev->dev, res->start, |
| 1148 | resource_size(res)); |
| 1149 | if (!xordev->xor_high_base) |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 1150 | return -EBUSY; |
| 1151 | |
Thomas Petazzoni | 297eedba | 2012-11-15 15:29:53 +0100 | [diff] [blame] | 1152 | platform_set_drvdata(pdev, xordev); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 1153 | |
| 1154 | /* |
| 1155 | * (Re-)program MBUS remapping windows if we are asked to. |
| 1156 | */ |
Andrew Lunn | 63a9332 | 2011-12-07 21:48:07 +0100 | [diff] [blame] | 1157 | dram = mv_mbus_dram_info(); |
| 1158 | if (dram) |
Thomas Petazzoni | 297eedba | 2012-11-15 15:29:53 +0100 | [diff] [blame] | 1159 | mv_xor_conf_mbus_windows(xordev, dram); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 1160 | |
Andrew Lunn | c510182 | 2012-02-19 13:30:26 +0100 | [diff] [blame] | 1161 | /* Not all platforms can gate the clock, so it is not |
| 1162 | * an error if the clock does not exists. |
| 1163 | */ |
Thomas Petazzoni | 297eedba | 2012-11-15 15:29:53 +0100 | [diff] [blame] | 1164 | xordev->clk = clk_get(&pdev->dev, NULL); |
| 1165 | if (!IS_ERR(xordev->clk)) |
| 1166 | clk_prepare_enable(xordev->clk); |
Andrew Lunn | c510182 | 2012-02-19 13:30:26 +0100 | [diff] [blame] | 1167 | |
Thomas Petazzoni | f7d12ef | 2012-11-15 16:47:58 +0100 | [diff] [blame] | 1168 | if (pdev->dev.of_node) { |
| 1169 | struct device_node *np; |
| 1170 | int i = 0; |
| 1171 | |
| 1172 | for_each_child_of_node(pdev->dev.of_node, np) { |
Russell King | 0be8253 | 2013-12-12 23:59:08 +0000 | [diff] [blame] | 1173 | struct mv_xor_chan *chan; |
Thomas Petazzoni | f7d12ef | 2012-11-15 16:47:58 +0100 | [diff] [blame] | 1174 | dma_cap_mask_t cap_mask; |
| 1175 | int irq; |
| 1176 | |
| 1177 | dma_cap_zero(cap_mask); |
| 1178 | if (of_property_read_bool(np, "dmacap,memcpy")) |
| 1179 | dma_cap_set(DMA_MEMCPY, cap_mask); |
| 1180 | if (of_property_read_bool(np, "dmacap,xor")) |
| 1181 | dma_cap_set(DMA_XOR, cap_mask); |
Thomas Petazzoni | f7d12ef | 2012-11-15 16:47:58 +0100 | [diff] [blame] | 1182 | if (of_property_read_bool(np, "dmacap,interrupt")) |
| 1183 | dma_cap_set(DMA_INTERRUPT, cap_mask); |
| 1184 | |
| 1185 | irq = irq_of_parse_and_map(np, 0); |
Thomas Petazzoni | f8eb9e7 | 2012-11-22 18:22:12 +0100 | [diff] [blame] | 1186 | if (!irq) { |
| 1187 | ret = -ENODEV; |
Thomas Petazzoni | f7d12ef | 2012-11-15 16:47:58 +0100 | [diff] [blame] | 1188 | goto err_channel_add; |
| 1189 | } |
| 1190 | |
Russell King | 0be8253 | 2013-12-12 23:59:08 +0000 | [diff] [blame] | 1191 | chan = mv_xor_channel_add(xordev, pdev, i, |
| 1192 | cap_mask, irq); |
| 1193 | if (IS_ERR(chan)) { |
| 1194 | ret = PTR_ERR(chan); |
Thomas Petazzoni | f7d12ef | 2012-11-15 16:47:58 +0100 | [diff] [blame] | 1195 | irq_dispose_mapping(irq); |
| 1196 | goto err_channel_add; |
| 1197 | } |
| 1198 | |
Russell King | 0be8253 | 2013-12-12 23:59:08 +0000 | [diff] [blame] | 1199 | xordev->channels[i] = chan; |
Thomas Petazzoni | f7d12ef | 2012-11-15 16:47:58 +0100 | [diff] [blame] | 1200 | i++; |
| 1201 | } |
| 1202 | } else if (pdata && pdata->channels) { |
Thomas Petazzoni | 60d151f | 2012-10-29 16:54:49 +0100 | [diff] [blame] | 1203 | for (i = 0; i < MV_XOR_MAX_CHANNELS; i++) { |
Thomas Petazzoni | e39f6ec | 2012-10-30 11:56:26 +0100 | [diff] [blame] | 1204 | struct mv_xor_channel_data *cd; |
Russell King | 0be8253 | 2013-12-12 23:59:08 +0000 | [diff] [blame] | 1205 | struct mv_xor_chan *chan; |
Thomas Petazzoni | 60d151f | 2012-10-29 16:54:49 +0100 | [diff] [blame] | 1206 | int irq; |
| 1207 | |
| 1208 | cd = &pdata->channels[i]; |
| 1209 | if (!cd) { |
| 1210 | ret = -ENODEV; |
| 1211 | goto err_channel_add; |
| 1212 | } |
| 1213 | |
| 1214 | irq = platform_get_irq(pdev, i); |
| 1215 | if (irq < 0) { |
| 1216 | ret = irq; |
| 1217 | goto err_channel_add; |
| 1218 | } |
| 1219 | |
Russell King | 0be8253 | 2013-12-12 23:59:08 +0000 | [diff] [blame] | 1220 | chan = mv_xor_channel_add(xordev, pdev, i, |
| 1221 | cd->cap_mask, irq); |
| 1222 | if (IS_ERR(chan)) { |
| 1223 | ret = PTR_ERR(chan); |
Thomas Petazzoni | 60d151f | 2012-10-29 16:54:49 +0100 | [diff] [blame] | 1224 | goto err_channel_add; |
| 1225 | } |
Russell King | 0be8253 | 2013-12-12 23:59:08 +0000 | [diff] [blame] | 1226 | |
| 1227 | xordev->channels[i] = chan; |
Thomas Petazzoni | 60d151f | 2012-10-29 16:54:49 +0100 | [diff] [blame] | 1228 | } |
| 1229 | } |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 1230 | |
| 1231 | return 0; |
Thomas Petazzoni | 60d151f | 2012-10-29 16:54:49 +0100 | [diff] [blame] | 1232 | |
| 1233 | err_channel_add: |
| 1234 | for (i = 0; i < MV_XOR_MAX_CHANNELS; i++) |
Thomas Petazzoni | f7d12ef | 2012-11-15 16:47:58 +0100 | [diff] [blame] | 1235 | if (xordev->channels[i]) { |
Thomas Petazzoni | ab6e439 | 2013-01-06 11:10:43 +0100 | [diff] [blame] | 1236 | mv_xor_channel_remove(xordev->channels[i]); |
Thomas Petazzoni | f7d12ef | 2012-11-15 16:47:58 +0100 | [diff] [blame] | 1237 | if (pdev->dev.of_node) |
| 1238 | irq_dispose_mapping(xordev->channels[i]->irq); |
Thomas Petazzoni | f7d12ef | 2012-11-15 16:47:58 +0100 | [diff] [blame] | 1239 | } |
Thomas Petazzoni | 60d151f | 2012-10-29 16:54:49 +0100 | [diff] [blame] | 1240 | |
Thomas Petazzoni | dab9206 | 2013-01-06 11:10:44 +0100 | [diff] [blame] | 1241 | if (!IS_ERR(xordev->clk)) { |
| 1242 | clk_disable_unprepare(xordev->clk); |
| 1243 | clk_put(xordev->clk); |
| 1244 | } |
| 1245 | |
Thomas Petazzoni | 60d151f | 2012-10-29 16:54:49 +0100 | [diff] [blame] | 1246 | return ret; |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 1247 | } |
| 1248 | |
Linus Torvalds | c271433 | 2012-12-14 14:54:26 -0800 | [diff] [blame] | 1249 | static int mv_xor_remove(struct platform_device *pdev) |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 1250 | { |
Thomas Petazzoni | 297eedba | 2012-11-15 15:29:53 +0100 | [diff] [blame] | 1251 | struct mv_xor_device *xordev = platform_get_drvdata(pdev); |
Thomas Petazzoni | 60d151f | 2012-10-29 16:54:49 +0100 | [diff] [blame] | 1252 | int i; |
Andrew Lunn | c510182 | 2012-02-19 13:30:26 +0100 | [diff] [blame] | 1253 | |
Thomas Petazzoni | 60d151f | 2012-10-29 16:54:49 +0100 | [diff] [blame] | 1254 | for (i = 0; i < MV_XOR_MAX_CHANNELS; i++) { |
Thomas Petazzoni | 297eedba | 2012-11-15 15:29:53 +0100 | [diff] [blame] | 1255 | if (xordev->channels[i]) |
| 1256 | mv_xor_channel_remove(xordev->channels[i]); |
Thomas Petazzoni | 60d151f | 2012-10-29 16:54:49 +0100 | [diff] [blame] | 1257 | } |
Andrew Lunn | c510182 | 2012-02-19 13:30:26 +0100 | [diff] [blame] | 1258 | |
Thomas Petazzoni | 297eedba | 2012-11-15 15:29:53 +0100 | [diff] [blame] | 1259 | if (!IS_ERR(xordev->clk)) { |
| 1260 | clk_disable_unprepare(xordev->clk); |
| 1261 | clk_put(xordev->clk); |
Andrew Lunn | c510182 | 2012-02-19 13:30:26 +0100 | [diff] [blame] | 1262 | } |
| 1263 | |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 1264 | return 0; |
| 1265 | } |
| 1266 | |
Thomas Petazzoni | f7d12ef | 2012-11-15 16:47:58 +0100 | [diff] [blame] | 1267 | #ifdef CONFIG_OF |
Fabian Frederick | 57c0342 | 2015-03-16 20:17:14 +0100 | [diff] [blame] | 1268 | static const struct of_device_id mv_xor_dt_ids[] = { |
Thomas Petazzoni | f7d12ef | 2012-11-15 16:47:58 +0100 | [diff] [blame] | 1269 | { .compatible = "marvell,orion-xor", }, |
| 1270 | {}, |
| 1271 | }; |
| 1272 | MODULE_DEVICE_TABLE(of, mv_xor_dt_ids); |
| 1273 | #endif |
| 1274 | |
Thomas Petazzoni | 6197165 | 2012-10-30 12:05:40 +0100 | [diff] [blame] | 1275 | static struct platform_driver mv_xor_driver = { |
| 1276 | .probe = mv_xor_probe, |
Linus Torvalds | c271433 | 2012-12-14 14:54:26 -0800 | [diff] [blame] | 1277 | .remove = mv_xor_remove, |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 1278 | .driver = { |
Thomas Petazzoni | f7d12ef | 2012-11-15 16:47:58 +0100 | [diff] [blame] | 1279 | .name = MV_XOR_NAME, |
| 1280 | .of_match_table = of_match_ptr(mv_xor_dt_ids), |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 1281 | }, |
| 1282 | }; |
| 1283 | |
| 1284 | |
| 1285 | static int __init mv_xor_init(void) |
| 1286 | { |
Thomas Petazzoni | 6197165 | 2012-10-30 12:05:40 +0100 | [diff] [blame] | 1287 | return platform_driver_register(&mv_xor_driver); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 1288 | } |
| 1289 | module_init(mv_xor_init); |
| 1290 | |
| 1291 | /* it's currently unsafe to unload this module */ |
| 1292 | #if 0 |
| 1293 | static void __exit mv_xor_exit(void) |
| 1294 | { |
| 1295 | platform_driver_unregister(&mv_xor_driver); |
Saeed Bishara | ff7b047 | 2008-07-08 11:58:36 -0700 | [diff] [blame] | 1296 | return; |
| 1297 | } |
| 1298 | |
| 1299 | module_exit(mv_xor_exit); |
| 1300 | #endif |
| 1301 | |
| 1302 | MODULE_AUTHOR("Saeed Bishara <saeed@marvell.com>"); |
| 1303 | MODULE_DESCRIPTION("DMA engine driver for Marvell's XOR engine"); |
| 1304 | MODULE_LICENSE("GPL"); |