1ff7b0479SSaeed Bishara /* 2ff7b0479SSaeed Bishara * offload engine driver for the Marvell XOR engine 3ff7b0479SSaeed Bishara * Copyright (C) 2007, 2008, Marvell International Ltd. 4ff7b0479SSaeed Bishara * 5ff7b0479SSaeed Bishara * This program is free software; you can redistribute it and/or modify it 6ff7b0479SSaeed Bishara * under the terms and conditions of the GNU General Public License, 7ff7b0479SSaeed Bishara * version 2, as published by the Free Software Foundation. 8ff7b0479SSaeed Bishara * 9ff7b0479SSaeed Bishara * This program is distributed in the hope it will be useful, but WITHOUT 10ff7b0479SSaeed Bishara * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11ff7b0479SSaeed Bishara * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 12ff7b0479SSaeed Bishara * more details. 13ff7b0479SSaeed Bishara * 14ff7b0479SSaeed Bishara * You should have received a copy of the GNU General Public License along with 15ff7b0479SSaeed Bishara * this program; if not, write to the Free Software Foundation, Inc., 16ff7b0479SSaeed Bishara * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. 17ff7b0479SSaeed Bishara */ 18ff7b0479SSaeed Bishara 19ff7b0479SSaeed Bishara #include <linux/init.h> 20ff7b0479SSaeed Bishara #include <linux/module.h> 215a0e3ad6STejun Heo #include <linux/slab.h> 22ff7b0479SSaeed Bishara #include <linux/delay.h> 23ff7b0479SSaeed Bishara #include <linux/dma-mapping.h> 24ff7b0479SSaeed Bishara #include <linux/spinlock.h> 25ff7b0479SSaeed Bishara #include <linux/interrupt.h> 26ff7b0479SSaeed Bishara #include <linux/platform_device.h> 27ff7b0479SSaeed Bishara #include <linux/memory.h> 28c510182bSAndrew Lunn #include <linux/clk.h> 29f7d12ef5SThomas Petazzoni #include <linux/of.h> 30f7d12ef5SThomas Petazzoni #include <linux/of_irq.h> 31f7d12ef5SThomas Petazzoni #include <linux/irqdomain.h> 32c02cecb9SArnd Bergmann #include <linux/platform_data/dma-mv_xor.h> 33d2ebfb33SRussell King - ARM Linux 34d2ebfb33SRussell King - ARM Linux #include "dmaengine.h" 35ff7b0479SSaeed Bishara #include "mv_xor.h" 36ff7b0479SSaeed Bishara 37ff7b0479SSaeed Bishara static void mv_xor_issue_pending(struct dma_chan *chan); 38ff7b0479SSaeed Bishara 39ff7b0479SSaeed Bishara #define to_mv_xor_chan(chan) \ 4098817b99SThomas Petazzoni container_of(chan, struct mv_xor_chan, dmachan) 41ff7b0479SSaeed Bishara 42ff7b0479SSaeed Bishara #define to_mv_xor_slot(tx) \ 43ff7b0479SSaeed Bishara container_of(tx, struct mv_xor_desc_slot, async_tx) 44ff7b0479SSaeed Bishara 45c98c1781SThomas Petazzoni #define mv_chan_to_devp(chan) \ 461ef48a26SThomas Petazzoni ((chan)->dmadev.dev) 47c98c1781SThomas Petazzoni 48ff7b0479SSaeed Bishara static void mv_desc_init(struct mv_xor_desc_slot *desc, unsigned long flags) 49ff7b0479SSaeed Bishara { 50ff7b0479SSaeed Bishara struct mv_xor_desc *hw_desc = desc->hw_desc; 51ff7b0479SSaeed Bishara 52ff7b0479SSaeed Bishara hw_desc->status = (1 << 31); 53ff7b0479SSaeed Bishara hw_desc->phy_next_desc = 0; 54ff7b0479SSaeed Bishara hw_desc->desc_command = (1 << 31); 55ff7b0479SSaeed Bishara } 56ff7b0479SSaeed Bishara 57ff7b0479SSaeed Bishara static u32 mv_desc_get_dest_addr(struct mv_xor_desc_slot *desc) 58ff7b0479SSaeed Bishara { 59ff7b0479SSaeed Bishara struct mv_xor_desc *hw_desc = desc->hw_desc; 60ff7b0479SSaeed Bishara return hw_desc->phy_dest_addr; 61ff7b0479SSaeed Bishara } 62ff7b0479SSaeed Bishara 63ff7b0479SSaeed Bishara static u32 mv_desc_get_src_addr(struct mv_xor_desc_slot *desc, 64ff7b0479SSaeed Bishara int src_idx) 65ff7b0479SSaeed Bishara { 66ff7b0479SSaeed Bishara struct mv_xor_desc *hw_desc = desc->hw_desc; 67ff7b0479SSaeed Bishara return hw_desc->phy_src_addr[src_idx]; 68ff7b0479SSaeed Bishara } 69ff7b0479SSaeed Bishara 70ff7b0479SSaeed Bishara 71ff7b0479SSaeed Bishara static void mv_desc_set_byte_count(struct mv_xor_desc_slot *desc, 72ff7b0479SSaeed Bishara u32 byte_count) 73ff7b0479SSaeed Bishara { 74ff7b0479SSaeed Bishara struct mv_xor_desc *hw_desc = desc->hw_desc; 75ff7b0479SSaeed Bishara hw_desc->byte_count = byte_count; 76ff7b0479SSaeed Bishara } 77ff7b0479SSaeed Bishara 78ff7b0479SSaeed Bishara static void mv_desc_set_next_desc(struct mv_xor_desc_slot *desc, 79ff7b0479SSaeed Bishara u32 next_desc_addr) 80ff7b0479SSaeed Bishara { 81ff7b0479SSaeed Bishara struct mv_xor_desc *hw_desc = desc->hw_desc; 82ff7b0479SSaeed Bishara BUG_ON(hw_desc->phy_next_desc); 83ff7b0479SSaeed Bishara hw_desc->phy_next_desc = next_desc_addr; 84ff7b0479SSaeed Bishara } 85ff7b0479SSaeed Bishara 86ff7b0479SSaeed Bishara static void mv_desc_clear_next_desc(struct mv_xor_desc_slot *desc) 87ff7b0479SSaeed Bishara { 88ff7b0479SSaeed Bishara struct mv_xor_desc *hw_desc = desc->hw_desc; 89ff7b0479SSaeed Bishara hw_desc->phy_next_desc = 0; 90ff7b0479SSaeed Bishara } 91ff7b0479SSaeed Bishara 92ff7b0479SSaeed Bishara static void mv_desc_set_block_fill_val(struct mv_xor_desc_slot *desc, u32 val) 93ff7b0479SSaeed Bishara { 94ff7b0479SSaeed Bishara desc->value = val; 95ff7b0479SSaeed Bishara } 96ff7b0479SSaeed Bishara 97ff7b0479SSaeed Bishara static void mv_desc_set_dest_addr(struct mv_xor_desc_slot *desc, 98ff7b0479SSaeed Bishara dma_addr_t addr) 99ff7b0479SSaeed Bishara { 100ff7b0479SSaeed Bishara struct mv_xor_desc *hw_desc = desc->hw_desc; 101ff7b0479SSaeed Bishara hw_desc->phy_dest_addr = addr; 102ff7b0479SSaeed Bishara } 103ff7b0479SSaeed Bishara 104ff7b0479SSaeed Bishara static int mv_chan_memset_slot_count(size_t len) 105ff7b0479SSaeed Bishara { 106ff7b0479SSaeed Bishara return 1; 107ff7b0479SSaeed Bishara } 108ff7b0479SSaeed Bishara 109ff7b0479SSaeed Bishara #define mv_chan_memcpy_slot_count(c) mv_chan_memset_slot_count(c) 110ff7b0479SSaeed Bishara 111ff7b0479SSaeed Bishara static void mv_desc_set_src_addr(struct mv_xor_desc_slot *desc, 112ff7b0479SSaeed Bishara int index, dma_addr_t addr) 113ff7b0479SSaeed Bishara { 114ff7b0479SSaeed Bishara struct mv_xor_desc *hw_desc = desc->hw_desc; 115ff7b0479SSaeed Bishara hw_desc->phy_src_addr[index] = addr; 116ff7b0479SSaeed Bishara if (desc->type == DMA_XOR) 117ff7b0479SSaeed Bishara hw_desc->desc_command |= (1 << index); 118ff7b0479SSaeed Bishara } 119ff7b0479SSaeed Bishara 120ff7b0479SSaeed Bishara static u32 mv_chan_get_current_desc(struct mv_xor_chan *chan) 121ff7b0479SSaeed Bishara { 122ff7b0479SSaeed Bishara return __raw_readl(XOR_CURR_DESC(chan)); 123ff7b0479SSaeed Bishara } 124ff7b0479SSaeed Bishara 125ff7b0479SSaeed Bishara static void mv_chan_set_next_descriptor(struct mv_xor_chan *chan, 126ff7b0479SSaeed Bishara u32 next_desc_addr) 127ff7b0479SSaeed Bishara { 128ff7b0479SSaeed Bishara __raw_writel(next_desc_addr, XOR_NEXT_DESC(chan)); 129ff7b0479SSaeed Bishara } 130ff7b0479SSaeed Bishara 131ff7b0479SSaeed Bishara static void mv_chan_set_dest_pointer(struct mv_xor_chan *chan, u32 desc_addr) 132ff7b0479SSaeed Bishara { 133ff7b0479SSaeed Bishara __raw_writel(desc_addr, XOR_DEST_POINTER(chan)); 134ff7b0479SSaeed Bishara } 135ff7b0479SSaeed Bishara 136ff7b0479SSaeed Bishara static void mv_chan_set_block_size(struct mv_xor_chan *chan, u32 block_size) 137ff7b0479SSaeed Bishara { 138ff7b0479SSaeed Bishara __raw_writel(block_size, XOR_BLOCK_SIZE(chan)); 139ff7b0479SSaeed Bishara } 140ff7b0479SSaeed Bishara 141ff7b0479SSaeed Bishara static void mv_chan_set_value(struct mv_xor_chan *chan, u32 value) 142ff7b0479SSaeed Bishara { 143ff7b0479SSaeed Bishara __raw_writel(value, XOR_INIT_VALUE_LOW(chan)); 144ff7b0479SSaeed Bishara __raw_writel(value, XOR_INIT_VALUE_HIGH(chan)); 145ff7b0479SSaeed Bishara } 146ff7b0479SSaeed Bishara 147ff7b0479SSaeed Bishara static void mv_chan_unmask_interrupts(struct mv_xor_chan *chan) 148ff7b0479SSaeed Bishara { 149ff7b0479SSaeed Bishara u32 val = __raw_readl(XOR_INTR_MASK(chan)); 150ff7b0479SSaeed Bishara val |= XOR_INTR_MASK_VALUE << (chan->idx * 16); 151ff7b0479SSaeed Bishara __raw_writel(val, XOR_INTR_MASK(chan)); 152ff7b0479SSaeed Bishara } 153ff7b0479SSaeed Bishara 154ff7b0479SSaeed Bishara static u32 mv_chan_get_intr_cause(struct mv_xor_chan *chan) 155ff7b0479SSaeed Bishara { 156ff7b0479SSaeed Bishara u32 intr_cause = __raw_readl(XOR_INTR_CAUSE(chan)); 157ff7b0479SSaeed Bishara intr_cause = (intr_cause >> (chan->idx * 16)) & 0xFFFF; 158ff7b0479SSaeed Bishara return intr_cause; 159ff7b0479SSaeed Bishara } 160ff7b0479SSaeed Bishara 161ff7b0479SSaeed Bishara static int mv_is_err_intr(u32 intr_cause) 162ff7b0479SSaeed Bishara { 163ff7b0479SSaeed Bishara if (intr_cause & ((1<<4)|(1<<5)|(1<<6)|(1<<7)|(1<<8)|(1<<9))) 164ff7b0479SSaeed Bishara return 1; 165ff7b0479SSaeed Bishara 166ff7b0479SSaeed Bishara return 0; 167ff7b0479SSaeed Bishara } 168ff7b0479SSaeed Bishara 169ff7b0479SSaeed Bishara static void mv_xor_device_clear_eoc_cause(struct mv_xor_chan *chan) 170ff7b0479SSaeed Bishara { 17186363682SSimon Guinot u32 val = ~(1 << (chan->idx * 16)); 172c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(chan), "%s, val 0x%08x\n", __func__, val); 173ff7b0479SSaeed Bishara __raw_writel(val, XOR_INTR_CAUSE(chan)); 174ff7b0479SSaeed Bishara } 175ff7b0479SSaeed Bishara 176ff7b0479SSaeed Bishara static void mv_xor_device_clear_err_status(struct mv_xor_chan *chan) 177ff7b0479SSaeed Bishara { 178ff7b0479SSaeed Bishara u32 val = 0xFFFF0000 >> (chan->idx * 16); 179ff7b0479SSaeed Bishara __raw_writel(val, XOR_INTR_CAUSE(chan)); 180ff7b0479SSaeed Bishara } 181ff7b0479SSaeed Bishara 182ff7b0479SSaeed Bishara static int mv_can_chain(struct mv_xor_desc_slot *desc) 183ff7b0479SSaeed Bishara { 184ff7b0479SSaeed Bishara struct mv_xor_desc_slot *chain_old_tail = list_entry( 185ff7b0479SSaeed Bishara desc->chain_node.prev, struct mv_xor_desc_slot, chain_node); 186ff7b0479SSaeed Bishara 187ff7b0479SSaeed Bishara if (chain_old_tail->type != desc->type) 188ff7b0479SSaeed Bishara return 0; 189ff7b0479SSaeed Bishara if (desc->type == DMA_MEMSET) 190ff7b0479SSaeed Bishara return 0; 191ff7b0479SSaeed Bishara 192ff7b0479SSaeed Bishara return 1; 193ff7b0479SSaeed Bishara } 194ff7b0479SSaeed Bishara 195ff7b0479SSaeed Bishara static void mv_set_mode(struct mv_xor_chan *chan, 196ff7b0479SSaeed Bishara enum dma_transaction_type type) 197ff7b0479SSaeed Bishara { 198ff7b0479SSaeed Bishara u32 op_mode; 199ff7b0479SSaeed Bishara u32 config = __raw_readl(XOR_CONFIG(chan)); 200ff7b0479SSaeed Bishara 201ff7b0479SSaeed Bishara switch (type) { 202ff7b0479SSaeed Bishara case DMA_XOR: 203ff7b0479SSaeed Bishara op_mode = XOR_OPERATION_MODE_XOR; 204ff7b0479SSaeed Bishara break; 205ff7b0479SSaeed Bishara case DMA_MEMCPY: 206ff7b0479SSaeed Bishara op_mode = XOR_OPERATION_MODE_MEMCPY; 207ff7b0479SSaeed Bishara break; 208ff7b0479SSaeed Bishara case DMA_MEMSET: 209ff7b0479SSaeed Bishara op_mode = XOR_OPERATION_MODE_MEMSET; 210ff7b0479SSaeed Bishara break; 211ff7b0479SSaeed Bishara default: 212c98c1781SThomas Petazzoni dev_err(mv_chan_to_devp(chan), 213ff7b0479SSaeed Bishara "error: unsupported operation %d.\n", 214ff7b0479SSaeed Bishara type); 215ff7b0479SSaeed Bishara BUG(); 216ff7b0479SSaeed Bishara return; 217ff7b0479SSaeed Bishara } 218ff7b0479SSaeed Bishara 219ff7b0479SSaeed Bishara config &= ~0x7; 220ff7b0479SSaeed Bishara config |= op_mode; 221ff7b0479SSaeed Bishara __raw_writel(config, XOR_CONFIG(chan)); 222ff7b0479SSaeed Bishara chan->current_type = type; 223ff7b0479SSaeed Bishara } 224ff7b0479SSaeed Bishara 225ff7b0479SSaeed Bishara static void mv_chan_activate(struct mv_xor_chan *chan) 226ff7b0479SSaeed Bishara { 227ff7b0479SSaeed Bishara u32 activation; 228ff7b0479SSaeed Bishara 229c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(chan), " activate chan.\n"); 230ff7b0479SSaeed Bishara activation = __raw_readl(XOR_ACTIVATION(chan)); 231ff7b0479SSaeed Bishara activation |= 0x1; 232ff7b0479SSaeed Bishara __raw_writel(activation, XOR_ACTIVATION(chan)); 233ff7b0479SSaeed Bishara } 234ff7b0479SSaeed Bishara 235ff7b0479SSaeed Bishara static char mv_chan_is_busy(struct mv_xor_chan *chan) 236ff7b0479SSaeed Bishara { 237ff7b0479SSaeed Bishara u32 state = __raw_readl(XOR_ACTIVATION(chan)); 238ff7b0479SSaeed Bishara 239ff7b0479SSaeed Bishara state = (state >> 4) & 0x3; 240ff7b0479SSaeed Bishara 241ff7b0479SSaeed Bishara return (state == 1) ? 1 : 0; 242ff7b0479SSaeed Bishara } 243ff7b0479SSaeed Bishara 244ff7b0479SSaeed Bishara static int mv_chan_xor_slot_count(size_t len, int src_cnt) 245ff7b0479SSaeed Bishara { 246ff7b0479SSaeed Bishara return 1; 247ff7b0479SSaeed Bishara } 248ff7b0479SSaeed Bishara 249ff7b0479SSaeed Bishara /** 250ff7b0479SSaeed Bishara * mv_xor_free_slots - flags descriptor slots for reuse 251ff7b0479SSaeed Bishara * @slot: Slot to free 252ff7b0479SSaeed Bishara * Caller must hold &mv_chan->lock while calling this function 253ff7b0479SSaeed Bishara */ 254ff7b0479SSaeed Bishara static void mv_xor_free_slots(struct mv_xor_chan *mv_chan, 255ff7b0479SSaeed Bishara struct mv_xor_desc_slot *slot) 256ff7b0479SSaeed Bishara { 257c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), "%s %d slot %p\n", 258ff7b0479SSaeed Bishara __func__, __LINE__, slot); 259ff7b0479SSaeed Bishara 260ff7b0479SSaeed Bishara slot->slots_per_op = 0; 261ff7b0479SSaeed Bishara 262ff7b0479SSaeed Bishara } 263ff7b0479SSaeed Bishara 264ff7b0479SSaeed Bishara /* 265ff7b0479SSaeed Bishara * mv_xor_start_new_chain - program the engine to operate on new chain headed by 266ff7b0479SSaeed Bishara * sw_desc 267ff7b0479SSaeed Bishara * Caller must hold &mv_chan->lock while calling this function 268ff7b0479SSaeed Bishara */ 269ff7b0479SSaeed Bishara static void mv_xor_start_new_chain(struct mv_xor_chan *mv_chan, 270ff7b0479SSaeed Bishara struct mv_xor_desc_slot *sw_desc) 271ff7b0479SSaeed Bishara { 272c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), "%s %d: sw_desc %p\n", 273ff7b0479SSaeed Bishara __func__, __LINE__, sw_desc); 274ff7b0479SSaeed Bishara if (sw_desc->type != mv_chan->current_type) 275ff7b0479SSaeed Bishara mv_set_mode(mv_chan, sw_desc->type); 276ff7b0479SSaeed Bishara 277ff7b0479SSaeed Bishara if (sw_desc->type == DMA_MEMSET) { 278ff7b0479SSaeed Bishara /* for memset requests we need to program the engine, no 279ff7b0479SSaeed Bishara * descriptors used. 280ff7b0479SSaeed Bishara */ 281ff7b0479SSaeed Bishara struct mv_xor_desc *hw_desc = sw_desc->hw_desc; 282ff7b0479SSaeed Bishara mv_chan_set_dest_pointer(mv_chan, hw_desc->phy_dest_addr); 283ff7b0479SSaeed Bishara mv_chan_set_block_size(mv_chan, sw_desc->unmap_len); 284ff7b0479SSaeed Bishara mv_chan_set_value(mv_chan, sw_desc->value); 285ff7b0479SSaeed Bishara } else { 286ff7b0479SSaeed Bishara /* set the hardware chain */ 287ff7b0479SSaeed Bishara mv_chan_set_next_descriptor(mv_chan, sw_desc->async_tx.phys); 288ff7b0479SSaeed Bishara } 289ff7b0479SSaeed Bishara mv_chan->pending += sw_desc->slot_cnt; 29098817b99SThomas Petazzoni mv_xor_issue_pending(&mv_chan->dmachan); 291ff7b0479SSaeed Bishara } 292ff7b0479SSaeed Bishara 293ff7b0479SSaeed Bishara static dma_cookie_t 294ff7b0479SSaeed Bishara mv_xor_run_tx_complete_actions(struct mv_xor_desc_slot *desc, 295ff7b0479SSaeed Bishara struct mv_xor_chan *mv_chan, dma_cookie_t cookie) 296ff7b0479SSaeed Bishara { 297ff7b0479SSaeed Bishara BUG_ON(desc->async_tx.cookie < 0); 298ff7b0479SSaeed Bishara 299ff7b0479SSaeed Bishara if (desc->async_tx.cookie > 0) { 300ff7b0479SSaeed Bishara cookie = desc->async_tx.cookie; 301ff7b0479SSaeed Bishara 302ff7b0479SSaeed Bishara /* call the callback (must not sleep or submit new 303ff7b0479SSaeed Bishara * operations to this channel) 304ff7b0479SSaeed Bishara */ 305ff7b0479SSaeed Bishara if (desc->async_tx.callback) 306ff7b0479SSaeed Bishara desc->async_tx.callback( 307ff7b0479SSaeed Bishara desc->async_tx.callback_param); 308ff7b0479SSaeed Bishara 309ff7b0479SSaeed Bishara /* unmap dma addresses 310ff7b0479SSaeed Bishara * (unmap_single vs unmap_page?) 311ff7b0479SSaeed Bishara */ 312ff7b0479SSaeed Bishara if (desc->group_head && desc->unmap_len) { 313ff7b0479SSaeed Bishara struct mv_xor_desc_slot *unmap = desc->group_head; 314ecde6cd4SThomas Petazzoni struct device *dev = mv_chan_to_devp(mv_chan); 315ff7b0479SSaeed Bishara u32 len = unmap->unmap_len; 316e1d181efSDan Williams enum dma_ctrl_flags flags = desc->async_tx.flags; 317e1d181efSDan Williams u32 src_cnt; 318e1d181efSDan Williams dma_addr_t addr; 319a06d568fSDan Williams dma_addr_t dest; 320ff7b0479SSaeed Bishara 321a06d568fSDan Williams src_cnt = unmap->unmap_src_cnt; 322a06d568fSDan Williams dest = mv_desc_get_dest_addr(unmap); 323e1d181efSDan Williams if (!(flags & DMA_COMPL_SKIP_DEST_UNMAP)) { 324a06d568fSDan Williams enum dma_data_direction dir; 325a06d568fSDan Williams 326a06d568fSDan Williams if (src_cnt > 1) /* is xor ? */ 327a06d568fSDan Williams dir = DMA_BIDIRECTIONAL; 328a06d568fSDan Williams else 329a06d568fSDan Williams dir = DMA_FROM_DEVICE; 330a06d568fSDan Williams dma_unmap_page(dev, dest, len, dir); 331e1d181efSDan Williams } 332e1d181efSDan Williams 333e1d181efSDan Williams if (!(flags & DMA_COMPL_SKIP_SRC_UNMAP)) { 334ff7b0479SSaeed Bishara while (src_cnt--) { 335e1d181efSDan Williams addr = mv_desc_get_src_addr(unmap, 336e1d181efSDan Williams src_cnt); 337a06d568fSDan Williams if (addr == dest) 338a06d568fSDan Williams continue; 339e1d181efSDan Williams dma_unmap_page(dev, addr, len, 340e1d181efSDan Williams DMA_TO_DEVICE); 341e1d181efSDan Williams } 342ff7b0479SSaeed Bishara } 343ff7b0479SSaeed Bishara desc->group_head = NULL; 344ff7b0479SSaeed Bishara } 345ff7b0479SSaeed Bishara } 346ff7b0479SSaeed Bishara 347ff7b0479SSaeed Bishara /* run dependent operations */ 34807f2211eSDan Williams dma_run_dependencies(&desc->async_tx); 349ff7b0479SSaeed Bishara 350ff7b0479SSaeed Bishara return cookie; 351ff7b0479SSaeed Bishara } 352ff7b0479SSaeed Bishara 353ff7b0479SSaeed Bishara static int 354ff7b0479SSaeed Bishara mv_xor_clean_completed_slots(struct mv_xor_chan *mv_chan) 355ff7b0479SSaeed Bishara { 356ff7b0479SSaeed Bishara struct mv_xor_desc_slot *iter, *_iter; 357ff7b0479SSaeed Bishara 358c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), "%s %d\n", __func__, __LINE__); 359ff7b0479SSaeed Bishara list_for_each_entry_safe(iter, _iter, &mv_chan->completed_slots, 360ff7b0479SSaeed Bishara completed_node) { 361ff7b0479SSaeed Bishara 362ff7b0479SSaeed Bishara if (async_tx_test_ack(&iter->async_tx)) { 363ff7b0479SSaeed Bishara list_del(&iter->completed_node); 364ff7b0479SSaeed Bishara mv_xor_free_slots(mv_chan, iter); 365ff7b0479SSaeed Bishara } 366ff7b0479SSaeed Bishara } 367ff7b0479SSaeed Bishara return 0; 368ff7b0479SSaeed Bishara } 369ff7b0479SSaeed Bishara 370ff7b0479SSaeed Bishara static int 371ff7b0479SSaeed Bishara mv_xor_clean_slot(struct mv_xor_desc_slot *desc, 372ff7b0479SSaeed Bishara struct mv_xor_chan *mv_chan) 373ff7b0479SSaeed Bishara { 374c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), "%s %d: desc %p flags %d\n", 375ff7b0479SSaeed Bishara __func__, __LINE__, desc, desc->async_tx.flags); 376ff7b0479SSaeed Bishara list_del(&desc->chain_node); 377ff7b0479SSaeed Bishara /* the client is allowed to attach dependent operations 378ff7b0479SSaeed Bishara * until 'ack' is set 379ff7b0479SSaeed Bishara */ 380ff7b0479SSaeed Bishara if (!async_tx_test_ack(&desc->async_tx)) { 381ff7b0479SSaeed Bishara /* move this slot to the completed_slots */ 382ff7b0479SSaeed Bishara list_add_tail(&desc->completed_node, &mv_chan->completed_slots); 383ff7b0479SSaeed Bishara return 0; 384ff7b0479SSaeed Bishara } 385ff7b0479SSaeed Bishara 386ff7b0479SSaeed Bishara mv_xor_free_slots(mv_chan, desc); 387ff7b0479SSaeed Bishara return 0; 388ff7b0479SSaeed Bishara } 389ff7b0479SSaeed Bishara 390ff7b0479SSaeed Bishara static void __mv_xor_slot_cleanup(struct mv_xor_chan *mv_chan) 391ff7b0479SSaeed Bishara { 392ff7b0479SSaeed Bishara struct mv_xor_desc_slot *iter, *_iter; 393ff7b0479SSaeed Bishara dma_cookie_t cookie = 0; 394ff7b0479SSaeed Bishara int busy = mv_chan_is_busy(mv_chan); 395ff7b0479SSaeed Bishara u32 current_desc = mv_chan_get_current_desc(mv_chan); 396ff7b0479SSaeed Bishara int seen_current = 0; 397ff7b0479SSaeed Bishara 398c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), "%s %d\n", __func__, __LINE__); 399c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), "current_desc %x\n", current_desc); 400ff7b0479SSaeed Bishara mv_xor_clean_completed_slots(mv_chan); 401ff7b0479SSaeed Bishara 402ff7b0479SSaeed Bishara /* free completed slots from the chain starting with 403ff7b0479SSaeed Bishara * the oldest descriptor 404ff7b0479SSaeed Bishara */ 405ff7b0479SSaeed Bishara 406ff7b0479SSaeed Bishara list_for_each_entry_safe(iter, _iter, &mv_chan->chain, 407ff7b0479SSaeed Bishara chain_node) { 408ff7b0479SSaeed Bishara prefetch(_iter); 409ff7b0479SSaeed Bishara prefetch(&_iter->async_tx); 410ff7b0479SSaeed Bishara 411ff7b0479SSaeed Bishara /* do not advance past the current descriptor loaded into the 412ff7b0479SSaeed Bishara * hardware channel, subsequent descriptors are either in 413ff7b0479SSaeed Bishara * process or have not been submitted 414ff7b0479SSaeed Bishara */ 415ff7b0479SSaeed Bishara if (seen_current) 416ff7b0479SSaeed Bishara break; 417ff7b0479SSaeed Bishara 418ff7b0479SSaeed Bishara /* stop the search if we reach the current descriptor and the 419ff7b0479SSaeed Bishara * channel is busy 420ff7b0479SSaeed Bishara */ 421ff7b0479SSaeed Bishara if (iter->async_tx.phys == current_desc) { 422ff7b0479SSaeed Bishara seen_current = 1; 423ff7b0479SSaeed Bishara if (busy) 424ff7b0479SSaeed Bishara break; 425ff7b0479SSaeed Bishara } 426ff7b0479SSaeed Bishara 427ff7b0479SSaeed Bishara cookie = mv_xor_run_tx_complete_actions(iter, mv_chan, cookie); 428ff7b0479SSaeed Bishara 429ff7b0479SSaeed Bishara if (mv_xor_clean_slot(iter, mv_chan)) 430ff7b0479SSaeed Bishara break; 431ff7b0479SSaeed Bishara } 432ff7b0479SSaeed Bishara 433ff7b0479SSaeed Bishara if ((busy == 0) && !list_empty(&mv_chan->chain)) { 434ff7b0479SSaeed Bishara struct mv_xor_desc_slot *chain_head; 435ff7b0479SSaeed Bishara chain_head = list_entry(mv_chan->chain.next, 436ff7b0479SSaeed Bishara struct mv_xor_desc_slot, 437ff7b0479SSaeed Bishara chain_node); 438ff7b0479SSaeed Bishara 439ff7b0479SSaeed Bishara mv_xor_start_new_chain(mv_chan, chain_head); 440ff7b0479SSaeed Bishara } 441ff7b0479SSaeed Bishara 442ff7b0479SSaeed Bishara if (cookie > 0) 44398817b99SThomas Petazzoni mv_chan->dmachan.completed_cookie = cookie; 444ff7b0479SSaeed Bishara } 445ff7b0479SSaeed Bishara 446ff7b0479SSaeed Bishara static void 447ff7b0479SSaeed Bishara mv_xor_slot_cleanup(struct mv_xor_chan *mv_chan) 448ff7b0479SSaeed Bishara { 449ff7b0479SSaeed Bishara spin_lock_bh(&mv_chan->lock); 450ff7b0479SSaeed Bishara __mv_xor_slot_cleanup(mv_chan); 451ff7b0479SSaeed Bishara spin_unlock_bh(&mv_chan->lock); 452ff7b0479SSaeed Bishara } 453ff7b0479SSaeed Bishara 454ff7b0479SSaeed Bishara static void mv_xor_tasklet(unsigned long data) 455ff7b0479SSaeed Bishara { 456ff7b0479SSaeed Bishara struct mv_xor_chan *chan = (struct mv_xor_chan *) data; 4578333f65eSSaeed Bishara mv_xor_slot_cleanup(chan); 458ff7b0479SSaeed Bishara } 459ff7b0479SSaeed Bishara 460ff7b0479SSaeed Bishara static struct mv_xor_desc_slot * 461ff7b0479SSaeed Bishara mv_xor_alloc_slots(struct mv_xor_chan *mv_chan, int num_slots, 462ff7b0479SSaeed Bishara int slots_per_op) 463ff7b0479SSaeed Bishara { 464ff7b0479SSaeed Bishara struct mv_xor_desc_slot *iter, *_iter, *alloc_start = NULL; 465ff7b0479SSaeed Bishara LIST_HEAD(chain); 466ff7b0479SSaeed Bishara int slots_found, retry = 0; 467ff7b0479SSaeed Bishara 468ff7b0479SSaeed Bishara /* start search from the last allocated descrtiptor 469ff7b0479SSaeed Bishara * if a contiguous allocation can not be found start searching 470ff7b0479SSaeed Bishara * from the beginning of the list 471ff7b0479SSaeed Bishara */ 472ff7b0479SSaeed Bishara retry: 473ff7b0479SSaeed Bishara slots_found = 0; 474ff7b0479SSaeed Bishara if (retry == 0) 475ff7b0479SSaeed Bishara iter = mv_chan->last_used; 476ff7b0479SSaeed Bishara else 477ff7b0479SSaeed Bishara iter = list_entry(&mv_chan->all_slots, 478ff7b0479SSaeed Bishara struct mv_xor_desc_slot, 479ff7b0479SSaeed Bishara slot_node); 480ff7b0479SSaeed Bishara 481ff7b0479SSaeed Bishara list_for_each_entry_safe_continue( 482ff7b0479SSaeed Bishara iter, _iter, &mv_chan->all_slots, slot_node) { 483ff7b0479SSaeed Bishara prefetch(_iter); 484ff7b0479SSaeed Bishara prefetch(&_iter->async_tx); 485ff7b0479SSaeed Bishara if (iter->slots_per_op) { 486ff7b0479SSaeed Bishara /* give up after finding the first busy slot 487ff7b0479SSaeed Bishara * on the second pass through the list 488ff7b0479SSaeed Bishara */ 489ff7b0479SSaeed Bishara if (retry) 490ff7b0479SSaeed Bishara break; 491ff7b0479SSaeed Bishara 492ff7b0479SSaeed Bishara slots_found = 0; 493ff7b0479SSaeed Bishara continue; 494ff7b0479SSaeed Bishara } 495ff7b0479SSaeed Bishara 496ff7b0479SSaeed Bishara /* start the allocation if the slot is correctly aligned */ 497ff7b0479SSaeed Bishara if (!slots_found++) 498ff7b0479SSaeed Bishara alloc_start = iter; 499ff7b0479SSaeed Bishara 500ff7b0479SSaeed Bishara if (slots_found == num_slots) { 501ff7b0479SSaeed Bishara struct mv_xor_desc_slot *alloc_tail = NULL; 502ff7b0479SSaeed Bishara struct mv_xor_desc_slot *last_used = NULL; 503ff7b0479SSaeed Bishara iter = alloc_start; 504ff7b0479SSaeed Bishara while (num_slots) { 505ff7b0479SSaeed Bishara int i; 506ff7b0479SSaeed Bishara 507ff7b0479SSaeed Bishara /* pre-ack all but the last descriptor */ 508ff7b0479SSaeed Bishara async_tx_ack(&iter->async_tx); 509ff7b0479SSaeed Bishara 510ff7b0479SSaeed Bishara list_add_tail(&iter->chain_node, &chain); 511ff7b0479SSaeed Bishara alloc_tail = iter; 512ff7b0479SSaeed Bishara iter->async_tx.cookie = 0; 513ff7b0479SSaeed Bishara iter->slot_cnt = num_slots; 514ff7b0479SSaeed Bishara iter->xor_check_result = NULL; 515ff7b0479SSaeed Bishara for (i = 0; i < slots_per_op; i++) { 516ff7b0479SSaeed Bishara iter->slots_per_op = slots_per_op - i; 517ff7b0479SSaeed Bishara last_used = iter; 518ff7b0479SSaeed Bishara iter = list_entry(iter->slot_node.next, 519ff7b0479SSaeed Bishara struct mv_xor_desc_slot, 520ff7b0479SSaeed Bishara slot_node); 521ff7b0479SSaeed Bishara } 522ff7b0479SSaeed Bishara num_slots -= slots_per_op; 523ff7b0479SSaeed Bishara } 524ff7b0479SSaeed Bishara alloc_tail->group_head = alloc_start; 525ff7b0479SSaeed Bishara alloc_tail->async_tx.cookie = -EBUSY; 52664203b67SDan Williams list_splice(&chain, &alloc_tail->tx_list); 527ff7b0479SSaeed Bishara mv_chan->last_used = last_used; 528ff7b0479SSaeed Bishara mv_desc_clear_next_desc(alloc_start); 529ff7b0479SSaeed Bishara mv_desc_clear_next_desc(alloc_tail); 530ff7b0479SSaeed Bishara return alloc_tail; 531ff7b0479SSaeed Bishara } 532ff7b0479SSaeed Bishara } 533ff7b0479SSaeed Bishara if (!retry++) 534ff7b0479SSaeed Bishara goto retry; 535ff7b0479SSaeed Bishara 536ff7b0479SSaeed Bishara /* try to free some slots if the allocation fails */ 537ff7b0479SSaeed Bishara tasklet_schedule(&mv_chan->irq_tasklet); 538ff7b0479SSaeed Bishara 539ff7b0479SSaeed Bishara return NULL; 540ff7b0479SSaeed Bishara } 541ff7b0479SSaeed Bishara 542ff7b0479SSaeed Bishara /************************ DMA engine API functions ****************************/ 543ff7b0479SSaeed Bishara static dma_cookie_t 544ff7b0479SSaeed Bishara mv_xor_tx_submit(struct dma_async_tx_descriptor *tx) 545ff7b0479SSaeed Bishara { 546ff7b0479SSaeed Bishara struct mv_xor_desc_slot *sw_desc = to_mv_xor_slot(tx); 547ff7b0479SSaeed Bishara struct mv_xor_chan *mv_chan = to_mv_xor_chan(tx->chan); 548ff7b0479SSaeed Bishara struct mv_xor_desc_slot *grp_start, *old_chain_tail; 549ff7b0479SSaeed Bishara dma_cookie_t cookie; 550ff7b0479SSaeed Bishara int new_hw_chain = 1; 551ff7b0479SSaeed Bishara 552c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), 553ff7b0479SSaeed Bishara "%s sw_desc %p: async_tx %p\n", 554ff7b0479SSaeed Bishara __func__, sw_desc, &sw_desc->async_tx); 555ff7b0479SSaeed Bishara 556ff7b0479SSaeed Bishara grp_start = sw_desc->group_head; 557ff7b0479SSaeed Bishara 558ff7b0479SSaeed Bishara spin_lock_bh(&mv_chan->lock); 559884485e1SRussell King - ARM Linux cookie = dma_cookie_assign(tx); 560ff7b0479SSaeed Bishara 561ff7b0479SSaeed Bishara if (list_empty(&mv_chan->chain)) 56264203b67SDan Williams list_splice_init(&sw_desc->tx_list, &mv_chan->chain); 563ff7b0479SSaeed Bishara else { 564ff7b0479SSaeed Bishara new_hw_chain = 0; 565ff7b0479SSaeed Bishara 566ff7b0479SSaeed Bishara old_chain_tail = list_entry(mv_chan->chain.prev, 567ff7b0479SSaeed Bishara struct mv_xor_desc_slot, 568ff7b0479SSaeed Bishara chain_node); 56964203b67SDan Williams list_splice_init(&grp_start->tx_list, 570ff7b0479SSaeed Bishara &old_chain_tail->chain_node); 571ff7b0479SSaeed Bishara 572ff7b0479SSaeed Bishara if (!mv_can_chain(grp_start)) 573ff7b0479SSaeed Bishara goto submit_done; 574ff7b0479SSaeed Bishara 575c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), "Append to last desc %x\n", 576ff7b0479SSaeed Bishara old_chain_tail->async_tx.phys); 577ff7b0479SSaeed Bishara 578ff7b0479SSaeed Bishara /* fix up the hardware chain */ 579ff7b0479SSaeed Bishara mv_desc_set_next_desc(old_chain_tail, grp_start->async_tx.phys); 580ff7b0479SSaeed Bishara 581ff7b0479SSaeed Bishara /* if the channel is not busy */ 582ff7b0479SSaeed Bishara if (!mv_chan_is_busy(mv_chan)) { 583ff7b0479SSaeed Bishara u32 current_desc = mv_chan_get_current_desc(mv_chan); 584ff7b0479SSaeed Bishara /* 585ff7b0479SSaeed Bishara * and the curren desc is the end of the chain before 586ff7b0479SSaeed Bishara * the append, then we need to start the channel 587ff7b0479SSaeed Bishara */ 588ff7b0479SSaeed Bishara if (current_desc == old_chain_tail->async_tx.phys) 589ff7b0479SSaeed Bishara new_hw_chain = 1; 590ff7b0479SSaeed Bishara } 591ff7b0479SSaeed Bishara } 592ff7b0479SSaeed Bishara 593ff7b0479SSaeed Bishara if (new_hw_chain) 594ff7b0479SSaeed Bishara mv_xor_start_new_chain(mv_chan, grp_start); 595ff7b0479SSaeed Bishara 596ff7b0479SSaeed Bishara submit_done: 597ff7b0479SSaeed Bishara spin_unlock_bh(&mv_chan->lock); 598ff7b0479SSaeed Bishara 599ff7b0479SSaeed Bishara return cookie; 600ff7b0479SSaeed Bishara } 601ff7b0479SSaeed Bishara 602ff7b0479SSaeed Bishara /* returns the number of allocated descriptors */ 603aa1e6f1aSDan Williams static int mv_xor_alloc_chan_resources(struct dma_chan *chan) 604ff7b0479SSaeed Bishara { 605ff7b0479SSaeed Bishara char *hw_desc; 606ff7b0479SSaeed Bishara int idx; 607ff7b0479SSaeed Bishara struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan); 608ff7b0479SSaeed Bishara struct mv_xor_desc_slot *slot = NULL; 609b503fa01SThomas Petazzoni int num_descs_in_pool = MV_XOR_POOL_SIZE/MV_XOR_SLOT_SIZE; 610ff7b0479SSaeed Bishara 611ff7b0479SSaeed Bishara /* Allocate descriptor slots */ 612ff7b0479SSaeed Bishara idx = mv_chan->slots_allocated; 613ff7b0479SSaeed Bishara while (idx < num_descs_in_pool) { 614ff7b0479SSaeed Bishara slot = kzalloc(sizeof(*slot), GFP_KERNEL); 615ff7b0479SSaeed Bishara if (!slot) { 616ff7b0479SSaeed Bishara printk(KERN_INFO "MV XOR Channel only initialized" 617ff7b0479SSaeed Bishara " %d descriptor slots", idx); 618ff7b0479SSaeed Bishara break; 619ff7b0479SSaeed Bishara } 6201ef48a26SThomas Petazzoni hw_desc = (char *) mv_chan->dma_desc_pool_virt; 621ff7b0479SSaeed Bishara slot->hw_desc = (void *) &hw_desc[idx * MV_XOR_SLOT_SIZE]; 622ff7b0479SSaeed Bishara 623ff7b0479SSaeed Bishara dma_async_tx_descriptor_init(&slot->async_tx, chan); 624ff7b0479SSaeed Bishara slot->async_tx.tx_submit = mv_xor_tx_submit; 625ff7b0479SSaeed Bishara INIT_LIST_HEAD(&slot->chain_node); 626ff7b0479SSaeed Bishara INIT_LIST_HEAD(&slot->slot_node); 62764203b67SDan Williams INIT_LIST_HEAD(&slot->tx_list); 6281ef48a26SThomas Petazzoni hw_desc = (char *) mv_chan->dma_desc_pool; 629ff7b0479SSaeed Bishara slot->async_tx.phys = 630ff7b0479SSaeed Bishara (dma_addr_t) &hw_desc[idx * MV_XOR_SLOT_SIZE]; 631ff7b0479SSaeed Bishara slot->idx = idx++; 632ff7b0479SSaeed Bishara 633ff7b0479SSaeed Bishara spin_lock_bh(&mv_chan->lock); 634ff7b0479SSaeed Bishara mv_chan->slots_allocated = idx; 635ff7b0479SSaeed Bishara list_add_tail(&slot->slot_node, &mv_chan->all_slots); 636ff7b0479SSaeed Bishara spin_unlock_bh(&mv_chan->lock); 637ff7b0479SSaeed Bishara } 638ff7b0479SSaeed Bishara 639ff7b0479SSaeed Bishara if (mv_chan->slots_allocated && !mv_chan->last_used) 640ff7b0479SSaeed Bishara mv_chan->last_used = list_entry(mv_chan->all_slots.next, 641ff7b0479SSaeed Bishara struct mv_xor_desc_slot, 642ff7b0479SSaeed Bishara slot_node); 643ff7b0479SSaeed Bishara 644c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), 645ff7b0479SSaeed Bishara "allocated %d descriptor slots last_used: %p\n", 646ff7b0479SSaeed Bishara mv_chan->slots_allocated, mv_chan->last_used); 647ff7b0479SSaeed Bishara 648ff7b0479SSaeed Bishara return mv_chan->slots_allocated ? : -ENOMEM; 649ff7b0479SSaeed Bishara } 650ff7b0479SSaeed Bishara 651ff7b0479SSaeed Bishara static struct dma_async_tx_descriptor * 652ff7b0479SSaeed Bishara mv_xor_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src, 653ff7b0479SSaeed Bishara size_t len, unsigned long flags) 654ff7b0479SSaeed Bishara { 655ff7b0479SSaeed Bishara struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan); 656ff7b0479SSaeed Bishara struct mv_xor_desc_slot *sw_desc, *grp_start; 657ff7b0479SSaeed Bishara int slot_cnt; 658ff7b0479SSaeed Bishara 659c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), 660ff7b0479SSaeed Bishara "%s dest: %x src %x len: %u flags: %ld\n", 661ff7b0479SSaeed Bishara __func__, dest, src, len, flags); 662ff7b0479SSaeed Bishara if (unlikely(len < MV_XOR_MIN_BYTE_COUNT)) 663ff7b0479SSaeed Bishara return NULL; 664ff7b0479SSaeed Bishara 6657912d300SColy Li BUG_ON(len > MV_XOR_MAX_BYTE_COUNT); 666ff7b0479SSaeed Bishara 667ff7b0479SSaeed Bishara spin_lock_bh(&mv_chan->lock); 668ff7b0479SSaeed Bishara slot_cnt = mv_chan_memcpy_slot_count(len); 669ff7b0479SSaeed Bishara sw_desc = mv_xor_alloc_slots(mv_chan, slot_cnt, 1); 670ff7b0479SSaeed Bishara if (sw_desc) { 671ff7b0479SSaeed Bishara sw_desc->type = DMA_MEMCPY; 672ff7b0479SSaeed Bishara sw_desc->async_tx.flags = flags; 673ff7b0479SSaeed Bishara grp_start = sw_desc->group_head; 674ff7b0479SSaeed Bishara mv_desc_init(grp_start, flags); 675ff7b0479SSaeed Bishara mv_desc_set_byte_count(grp_start, len); 676ff7b0479SSaeed Bishara mv_desc_set_dest_addr(sw_desc->group_head, dest); 677ff7b0479SSaeed Bishara mv_desc_set_src_addr(grp_start, 0, src); 678ff7b0479SSaeed Bishara sw_desc->unmap_src_cnt = 1; 679ff7b0479SSaeed Bishara sw_desc->unmap_len = len; 680ff7b0479SSaeed Bishara } 681ff7b0479SSaeed Bishara spin_unlock_bh(&mv_chan->lock); 682ff7b0479SSaeed Bishara 683c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), 684ff7b0479SSaeed Bishara "%s sw_desc %p async_tx %p\n", 685ff7b0479SSaeed Bishara __func__, sw_desc, sw_desc ? &sw_desc->async_tx : 0); 686ff7b0479SSaeed Bishara 687ff7b0479SSaeed Bishara return sw_desc ? &sw_desc->async_tx : NULL; 688ff7b0479SSaeed Bishara } 689ff7b0479SSaeed Bishara 690ff7b0479SSaeed Bishara static struct dma_async_tx_descriptor * 691ff7b0479SSaeed Bishara mv_xor_prep_dma_memset(struct dma_chan *chan, dma_addr_t dest, int value, 692ff7b0479SSaeed Bishara size_t len, unsigned long flags) 693ff7b0479SSaeed Bishara { 694ff7b0479SSaeed Bishara struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan); 695ff7b0479SSaeed Bishara struct mv_xor_desc_slot *sw_desc, *grp_start; 696ff7b0479SSaeed Bishara int slot_cnt; 697ff7b0479SSaeed Bishara 698c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), 699ff7b0479SSaeed Bishara "%s dest: %x len: %u flags: %ld\n", 700ff7b0479SSaeed Bishara __func__, dest, len, flags); 701ff7b0479SSaeed Bishara if (unlikely(len < MV_XOR_MIN_BYTE_COUNT)) 702ff7b0479SSaeed Bishara return NULL; 703ff7b0479SSaeed Bishara 7047912d300SColy Li BUG_ON(len > MV_XOR_MAX_BYTE_COUNT); 705ff7b0479SSaeed Bishara 706ff7b0479SSaeed Bishara spin_lock_bh(&mv_chan->lock); 707ff7b0479SSaeed Bishara slot_cnt = mv_chan_memset_slot_count(len); 708ff7b0479SSaeed Bishara sw_desc = mv_xor_alloc_slots(mv_chan, slot_cnt, 1); 709ff7b0479SSaeed Bishara if (sw_desc) { 710ff7b0479SSaeed Bishara sw_desc->type = DMA_MEMSET; 711ff7b0479SSaeed Bishara sw_desc->async_tx.flags = flags; 712ff7b0479SSaeed Bishara grp_start = sw_desc->group_head; 713ff7b0479SSaeed Bishara mv_desc_init(grp_start, flags); 714ff7b0479SSaeed Bishara mv_desc_set_byte_count(grp_start, len); 715ff7b0479SSaeed Bishara mv_desc_set_dest_addr(sw_desc->group_head, dest); 716ff7b0479SSaeed Bishara mv_desc_set_block_fill_val(grp_start, value); 717ff7b0479SSaeed Bishara sw_desc->unmap_src_cnt = 1; 718ff7b0479SSaeed Bishara sw_desc->unmap_len = len; 719ff7b0479SSaeed Bishara } 720ff7b0479SSaeed Bishara spin_unlock_bh(&mv_chan->lock); 721c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), 722ff7b0479SSaeed Bishara "%s sw_desc %p async_tx %p \n", 723ff7b0479SSaeed Bishara __func__, sw_desc, &sw_desc->async_tx); 724ff7b0479SSaeed Bishara return sw_desc ? &sw_desc->async_tx : NULL; 725ff7b0479SSaeed Bishara } 726ff7b0479SSaeed Bishara 727ff7b0479SSaeed Bishara static struct dma_async_tx_descriptor * 728ff7b0479SSaeed Bishara mv_xor_prep_dma_xor(struct dma_chan *chan, dma_addr_t dest, dma_addr_t *src, 729ff7b0479SSaeed Bishara unsigned int src_cnt, size_t len, unsigned long flags) 730ff7b0479SSaeed Bishara { 731ff7b0479SSaeed Bishara struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan); 732ff7b0479SSaeed Bishara struct mv_xor_desc_slot *sw_desc, *grp_start; 733ff7b0479SSaeed Bishara int slot_cnt; 734ff7b0479SSaeed Bishara 735ff7b0479SSaeed Bishara if (unlikely(len < MV_XOR_MIN_BYTE_COUNT)) 736ff7b0479SSaeed Bishara return NULL; 737ff7b0479SSaeed Bishara 7387912d300SColy Li BUG_ON(len > MV_XOR_MAX_BYTE_COUNT); 739ff7b0479SSaeed Bishara 740c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), 741ff7b0479SSaeed Bishara "%s src_cnt: %d len: dest %x %u flags: %ld\n", 742ff7b0479SSaeed Bishara __func__, src_cnt, len, dest, flags); 743ff7b0479SSaeed Bishara 744ff7b0479SSaeed Bishara spin_lock_bh(&mv_chan->lock); 745ff7b0479SSaeed Bishara slot_cnt = mv_chan_xor_slot_count(len, src_cnt); 746ff7b0479SSaeed Bishara sw_desc = mv_xor_alloc_slots(mv_chan, slot_cnt, 1); 747ff7b0479SSaeed Bishara if (sw_desc) { 748ff7b0479SSaeed Bishara sw_desc->type = DMA_XOR; 749ff7b0479SSaeed Bishara sw_desc->async_tx.flags = flags; 750ff7b0479SSaeed Bishara grp_start = sw_desc->group_head; 751ff7b0479SSaeed Bishara mv_desc_init(grp_start, flags); 752ff7b0479SSaeed Bishara /* the byte count field is the same as in memcpy desc*/ 753ff7b0479SSaeed Bishara mv_desc_set_byte_count(grp_start, len); 754ff7b0479SSaeed Bishara mv_desc_set_dest_addr(sw_desc->group_head, dest); 755ff7b0479SSaeed Bishara sw_desc->unmap_src_cnt = src_cnt; 756ff7b0479SSaeed Bishara sw_desc->unmap_len = len; 757ff7b0479SSaeed Bishara while (src_cnt--) 758ff7b0479SSaeed Bishara mv_desc_set_src_addr(grp_start, src_cnt, src[src_cnt]); 759ff7b0479SSaeed Bishara } 760ff7b0479SSaeed Bishara spin_unlock_bh(&mv_chan->lock); 761c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), 762ff7b0479SSaeed Bishara "%s sw_desc %p async_tx %p \n", 763ff7b0479SSaeed Bishara __func__, sw_desc, &sw_desc->async_tx); 764ff7b0479SSaeed Bishara return sw_desc ? &sw_desc->async_tx : NULL; 765ff7b0479SSaeed Bishara } 766ff7b0479SSaeed Bishara 767ff7b0479SSaeed Bishara static void mv_xor_free_chan_resources(struct dma_chan *chan) 768ff7b0479SSaeed Bishara { 769ff7b0479SSaeed Bishara struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan); 770ff7b0479SSaeed Bishara struct mv_xor_desc_slot *iter, *_iter; 771ff7b0479SSaeed Bishara int in_use_descs = 0; 772ff7b0479SSaeed Bishara 773ff7b0479SSaeed Bishara mv_xor_slot_cleanup(mv_chan); 774ff7b0479SSaeed Bishara 775ff7b0479SSaeed Bishara spin_lock_bh(&mv_chan->lock); 776ff7b0479SSaeed Bishara list_for_each_entry_safe(iter, _iter, &mv_chan->chain, 777ff7b0479SSaeed Bishara chain_node) { 778ff7b0479SSaeed Bishara in_use_descs++; 779ff7b0479SSaeed Bishara list_del(&iter->chain_node); 780ff7b0479SSaeed Bishara } 781ff7b0479SSaeed Bishara list_for_each_entry_safe(iter, _iter, &mv_chan->completed_slots, 782ff7b0479SSaeed Bishara completed_node) { 783ff7b0479SSaeed Bishara in_use_descs++; 784ff7b0479SSaeed Bishara list_del(&iter->completed_node); 785ff7b0479SSaeed Bishara } 786ff7b0479SSaeed Bishara list_for_each_entry_safe_reverse( 787ff7b0479SSaeed Bishara iter, _iter, &mv_chan->all_slots, slot_node) { 788ff7b0479SSaeed Bishara list_del(&iter->slot_node); 789ff7b0479SSaeed Bishara kfree(iter); 790ff7b0479SSaeed Bishara mv_chan->slots_allocated--; 791ff7b0479SSaeed Bishara } 792ff7b0479SSaeed Bishara mv_chan->last_used = NULL; 793ff7b0479SSaeed Bishara 794c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), "%s slots_allocated %d\n", 795ff7b0479SSaeed Bishara __func__, mv_chan->slots_allocated); 796ff7b0479SSaeed Bishara spin_unlock_bh(&mv_chan->lock); 797ff7b0479SSaeed Bishara 798ff7b0479SSaeed Bishara if (in_use_descs) 799c98c1781SThomas Petazzoni dev_err(mv_chan_to_devp(mv_chan), 800ff7b0479SSaeed Bishara "freeing %d in use descriptors!\n", in_use_descs); 801ff7b0479SSaeed Bishara } 802ff7b0479SSaeed Bishara 803ff7b0479SSaeed Bishara /** 80407934481SLinus Walleij * mv_xor_status - poll the status of an XOR transaction 805ff7b0479SSaeed Bishara * @chan: XOR channel handle 806ff7b0479SSaeed Bishara * @cookie: XOR transaction identifier 80707934481SLinus Walleij * @txstate: XOR transactions state holder (or NULL) 808ff7b0479SSaeed Bishara */ 80907934481SLinus Walleij static enum dma_status mv_xor_status(struct dma_chan *chan, 810ff7b0479SSaeed Bishara dma_cookie_t cookie, 81107934481SLinus Walleij struct dma_tx_state *txstate) 812ff7b0479SSaeed Bishara { 813ff7b0479SSaeed Bishara struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan); 814ff7b0479SSaeed Bishara enum dma_status ret; 815ff7b0479SSaeed Bishara 81696a2af41SRussell King - ARM Linux ret = dma_cookie_status(chan, cookie, txstate); 817ff7b0479SSaeed Bishara if (ret == DMA_SUCCESS) { 818ff7b0479SSaeed Bishara mv_xor_clean_completed_slots(mv_chan); 819ff7b0479SSaeed Bishara return ret; 820ff7b0479SSaeed Bishara } 821ff7b0479SSaeed Bishara mv_xor_slot_cleanup(mv_chan); 822ff7b0479SSaeed Bishara 82396a2af41SRussell King - ARM Linux return dma_cookie_status(chan, cookie, txstate); 824ff7b0479SSaeed Bishara } 825ff7b0479SSaeed Bishara 826ff7b0479SSaeed Bishara static void mv_dump_xor_regs(struct mv_xor_chan *chan) 827ff7b0479SSaeed Bishara { 828ff7b0479SSaeed Bishara u32 val; 829ff7b0479SSaeed Bishara 830ff7b0479SSaeed Bishara val = __raw_readl(XOR_CONFIG(chan)); 831c98c1781SThomas Petazzoni dev_err(mv_chan_to_devp(chan), 832ff7b0479SSaeed Bishara "config 0x%08x.\n", val); 833ff7b0479SSaeed Bishara 834ff7b0479SSaeed Bishara val = __raw_readl(XOR_ACTIVATION(chan)); 835c98c1781SThomas Petazzoni dev_err(mv_chan_to_devp(chan), 836ff7b0479SSaeed Bishara "activation 0x%08x.\n", val); 837ff7b0479SSaeed Bishara 838ff7b0479SSaeed Bishara val = __raw_readl(XOR_INTR_CAUSE(chan)); 839c98c1781SThomas Petazzoni dev_err(mv_chan_to_devp(chan), 840ff7b0479SSaeed Bishara "intr cause 0x%08x.\n", val); 841ff7b0479SSaeed Bishara 842ff7b0479SSaeed Bishara val = __raw_readl(XOR_INTR_MASK(chan)); 843c98c1781SThomas Petazzoni dev_err(mv_chan_to_devp(chan), 844ff7b0479SSaeed Bishara "intr mask 0x%08x.\n", val); 845ff7b0479SSaeed Bishara 846ff7b0479SSaeed Bishara val = __raw_readl(XOR_ERROR_CAUSE(chan)); 847c98c1781SThomas Petazzoni dev_err(mv_chan_to_devp(chan), 848ff7b0479SSaeed Bishara "error cause 0x%08x.\n", val); 849ff7b0479SSaeed Bishara 850ff7b0479SSaeed Bishara val = __raw_readl(XOR_ERROR_ADDR(chan)); 851c98c1781SThomas Petazzoni dev_err(mv_chan_to_devp(chan), 852ff7b0479SSaeed Bishara "error addr 0x%08x.\n", val); 853ff7b0479SSaeed Bishara } 854ff7b0479SSaeed Bishara 855ff7b0479SSaeed Bishara static void mv_xor_err_interrupt_handler(struct mv_xor_chan *chan, 856ff7b0479SSaeed Bishara u32 intr_cause) 857ff7b0479SSaeed Bishara { 858ff7b0479SSaeed Bishara if (intr_cause & (1 << 4)) { 859c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(chan), 860ff7b0479SSaeed Bishara "ignore this error\n"); 861ff7b0479SSaeed Bishara return; 862ff7b0479SSaeed Bishara } 863ff7b0479SSaeed Bishara 864c98c1781SThomas Petazzoni dev_err(mv_chan_to_devp(chan), 865ff7b0479SSaeed Bishara "error on chan %d. intr cause 0x%08x.\n", 866ff7b0479SSaeed Bishara chan->idx, intr_cause); 867ff7b0479SSaeed Bishara 868ff7b0479SSaeed Bishara mv_dump_xor_regs(chan); 869ff7b0479SSaeed Bishara BUG(); 870ff7b0479SSaeed Bishara } 871ff7b0479SSaeed Bishara 872ff7b0479SSaeed Bishara static irqreturn_t mv_xor_interrupt_handler(int irq, void *data) 873ff7b0479SSaeed Bishara { 874ff7b0479SSaeed Bishara struct mv_xor_chan *chan = data; 875ff7b0479SSaeed Bishara u32 intr_cause = mv_chan_get_intr_cause(chan); 876ff7b0479SSaeed Bishara 877c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(chan), "intr cause %x\n", intr_cause); 878ff7b0479SSaeed Bishara 879ff7b0479SSaeed Bishara if (mv_is_err_intr(intr_cause)) 880ff7b0479SSaeed Bishara mv_xor_err_interrupt_handler(chan, intr_cause); 881ff7b0479SSaeed Bishara 882ff7b0479SSaeed Bishara tasklet_schedule(&chan->irq_tasklet); 883ff7b0479SSaeed Bishara 884ff7b0479SSaeed Bishara mv_xor_device_clear_eoc_cause(chan); 885ff7b0479SSaeed Bishara 886ff7b0479SSaeed Bishara return IRQ_HANDLED; 887ff7b0479SSaeed Bishara } 888ff7b0479SSaeed Bishara 889ff7b0479SSaeed Bishara static void mv_xor_issue_pending(struct dma_chan *chan) 890ff7b0479SSaeed Bishara { 891ff7b0479SSaeed Bishara struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan); 892ff7b0479SSaeed Bishara 893ff7b0479SSaeed Bishara if (mv_chan->pending >= MV_XOR_THRESHOLD) { 894ff7b0479SSaeed Bishara mv_chan->pending = 0; 895ff7b0479SSaeed Bishara mv_chan_activate(mv_chan); 896ff7b0479SSaeed Bishara } 897ff7b0479SSaeed Bishara } 898ff7b0479SSaeed Bishara 899ff7b0479SSaeed Bishara /* 900ff7b0479SSaeed Bishara * Perform a transaction to verify the HW works. 901ff7b0479SSaeed Bishara */ 902ff7b0479SSaeed Bishara #define MV_XOR_TEST_SIZE 2000 903ff7b0479SSaeed Bishara 904c2714334SLinus Torvalds static int mv_xor_memcpy_self_test(struct mv_xor_chan *mv_chan) 905ff7b0479SSaeed Bishara { 906ff7b0479SSaeed Bishara int i; 907ff7b0479SSaeed Bishara void *src, *dest; 908ff7b0479SSaeed Bishara dma_addr_t src_dma, dest_dma; 909ff7b0479SSaeed Bishara struct dma_chan *dma_chan; 910ff7b0479SSaeed Bishara dma_cookie_t cookie; 911ff7b0479SSaeed Bishara struct dma_async_tx_descriptor *tx; 912ff7b0479SSaeed Bishara int err = 0; 913ff7b0479SSaeed Bishara 914ff7b0479SSaeed Bishara src = kmalloc(sizeof(u8) * MV_XOR_TEST_SIZE, GFP_KERNEL); 915ff7b0479SSaeed Bishara if (!src) 916ff7b0479SSaeed Bishara return -ENOMEM; 917ff7b0479SSaeed Bishara 918ff7b0479SSaeed Bishara dest = kzalloc(sizeof(u8) * MV_XOR_TEST_SIZE, GFP_KERNEL); 919ff7b0479SSaeed Bishara if (!dest) { 920ff7b0479SSaeed Bishara kfree(src); 921ff7b0479SSaeed Bishara return -ENOMEM; 922ff7b0479SSaeed Bishara } 923ff7b0479SSaeed Bishara 924ff7b0479SSaeed Bishara /* Fill in src buffer */ 925ff7b0479SSaeed Bishara for (i = 0; i < MV_XOR_TEST_SIZE; i++) 926ff7b0479SSaeed Bishara ((u8 *) src)[i] = (u8)i; 927ff7b0479SSaeed Bishara 928275cc0c8SThomas Petazzoni dma_chan = &mv_chan->dmachan; 929aa1e6f1aSDan Williams if (mv_xor_alloc_chan_resources(dma_chan) < 1) { 930ff7b0479SSaeed Bishara err = -ENODEV; 931ff7b0479SSaeed Bishara goto out; 932ff7b0479SSaeed Bishara } 933ff7b0479SSaeed Bishara 934ff7b0479SSaeed Bishara dest_dma = dma_map_single(dma_chan->device->dev, dest, 935ff7b0479SSaeed Bishara MV_XOR_TEST_SIZE, DMA_FROM_DEVICE); 936ff7b0479SSaeed Bishara 937ff7b0479SSaeed Bishara src_dma = dma_map_single(dma_chan->device->dev, src, 938ff7b0479SSaeed Bishara MV_XOR_TEST_SIZE, DMA_TO_DEVICE); 939ff7b0479SSaeed Bishara 940ff7b0479SSaeed Bishara tx = mv_xor_prep_dma_memcpy(dma_chan, dest_dma, src_dma, 941ff7b0479SSaeed Bishara MV_XOR_TEST_SIZE, 0); 942ff7b0479SSaeed Bishara cookie = mv_xor_tx_submit(tx); 943ff7b0479SSaeed Bishara mv_xor_issue_pending(dma_chan); 944ff7b0479SSaeed Bishara async_tx_ack(tx); 945ff7b0479SSaeed Bishara msleep(1); 946ff7b0479SSaeed Bishara 94707934481SLinus Walleij if (mv_xor_status(dma_chan, cookie, NULL) != 948ff7b0479SSaeed Bishara DMA_SUCCESS) { 949a3fc74bcSThomas Petazzoni dev_err(dma_chan->device->dev, 950ff7b0479SSaeed Bishara "Self-test copy timed out, disabling\n"); 951ff7b0479SSaeed Bishara err = -ENODEV; 952ff7b0479SSaeed Bishara goto free_resources; 953ff7b0479SSaeed Bishara } 954ff7b0479SSaeed Bishara 955c35064c4SThomas Petazzoni dma_sync_single_for_cpu(dma_chan->device->dev, dest_dma, 956ff7b0479SSaeed Bishara MV_XOR_TEST_SIZE, DMA_FROM_DEVICE); 957ff7b0479SSaeed Bishara if (memcmp(src, dest, MV_XOR_TEST_SIZE)) { 958a3fc74bcSThomas Petazzoni dev_err(dma_chan->device->dev, 959ff7b0479SSaeed Bishara "Self-test copy failed compare, disabling\n"); 960ff7b0479SSaeed Bishara err = -ENODEV; 961ff7b0479SSaeed Bishara goto free_resources; 962ff7b0479SSaeed Bishara } 963ff7b0479SSaeed Bishara 964ff7b0479SSaeed Bishara free_resources: 965ff7b0479SSaeed Bishara mv_xor_free_chan_resources(dma_chan); 966ff7b0479SSaeed Bishara out: 967ff7b0479SSaeed Bishara kfree(src); 968ff7b0479SSaeed Bishara kfree(dest); 969ff7b0479SSaeed Bishara return err; 970ff7b0479SSaeed Bishara } 971ff7b0479SSaeed Bishara 972ff7b0479SSaeed Bishara #define MV_XOR_NUM_SRC_TEST 4 /* must be <= 15 */ 973463a1f8bSBill Pemberton static int 974275cc0c8SThomas Petazzoni mv_xor_xor_self_test(struct mv_xor_chan *mv_chan) 975ff7b0479SSaeed Bishara { 976ff7b0479SSaeed Bishara int i, src_idx; 977ff7b0479SSaeed Bishara struct page *dest; 978ff7b0479SSaeed Bishara struct page *xor_srcs[MV_XOR_NUM_SRC_TEST]; 979ff7b0479SSaeed Bishara dma_addr_t dma_srcs[MV_XOR_NUM_SRC_TEST]; 980ff7b0479SSaeed Bishara dma_addr_t dest_dma; 981ff7b0479SSaeed Bishara struct dma_async_tx_descriptor *tx; 982ff7b0479SSaeed Bishara struct dma_chan *dma_chan; 983ff7b0479SSaeed Bishara dma_cookie_t cookie; 984ff7b0479SSaeed Bishara u8 cmp_byte = 0; 985ff7b0479SSaeed Bishara u32 cmp_word; 986ff7b0479SSaeed Bishara int err = 0; 987ff7b0479SSaeed Bishara 988ff7b0479SSaeed Bishara for (src_idx = 0; src_idx < MV_XOR_NUM_SRC_TEST; src_idx++) { 989ff7b0479SSaeed Bishara xor_srcs[src_idx] = alloc_page(GFP_KERNEL); 990a09b09aeSRoel Kluin if (!xor_srcs[src_idx]) { 991a09b09aeSRoel Kluin while (src_idx--) 992ff7b0479SSaeed Bishara __free_page(xor_srcs[src_idx]); 993ff7b0479SSaeed Bishara return -ENOMEM; 994ff7b0479SSaeed Bishara } 995ff7b0479SSaeed Bishara } 996ff7b0479SSaeed Bishara 997ff7b0479SSaeed Bishara dest = alloc_page(GFP_KERNEL); 998a09b09aeSRoel Kluin if (!dest) { 999a09b09aeSRoel Kluin while (src_idx--) 1000ff7b0479SSaeed Bishara __free_page(xor_srcs[src_idx]); 1001ff7b0479SSaeed Bishara return -ENOMEM; 1002ff7b0479SSaeed Bishara } 1003ff7b0479SSaeed Bishara 1004ff7b0479SSaeed Bishara /* Fill in src buffers */ 1005ff7b0479SSaeed Bishara for (src_idx = 0; src_idx < MV_XOR_NUM_SRC_TEST; src_idx++) { 1006ff7b0479SSaeed Bishara u8 *ptr = page_address(xor_srcs[src_idx]); 1007ff7b0479SSaeed Bishara for (i = 0; i < PAGE_SIZE; i++) 1008ff7b0479SSaeed Bishara ptr[i] = (1 << src_idx); 1009ff7b0479SSaeed Bishara } 1010ff7b0479SSaeed Bishara 1011ff7b0479SSaeed Bishara for (src_idx = 0; src_idx < MV_XOR_NUM_SRC_TEST; src_idx++) 1012ff7b0479SSaeed Bishara cmp_byte ^= (u8) (1 << src_idx); 1013ff7b0479SSaeed Bishara 1014ff7b0479SSaeed Bishara cmp_word = (cmp_byte << 24) | (cmp_byte << 16) | 1015ff7b0479SSaeed Bishara (cmp_byte << 8) | cmp_byte; 1016ff7b0479SSaeed Bishara 1017ff7b0479SSaeed Bishara memset(page_address(dest), 0, PAGE_SIZE); 1018ff7b0479SSaeed Bishara 1019275cc0c8SThomas Petazzoni dma_chan = &mv_chan->dmachan; 1020aa1e6f1aSDan Williams if (mv_xor_alloc_chan_resources(dma_chan) < 1) { 1021ff7b0479SSaeed Bishara err = -ENODEV; 1022ff7b0479SSaeed Bishara goto out; 1023ff7b0479SSaeed Bishara } 1024ff7b0479SSaeed Bishara 1025ff7b0479SSaeed Bishara /* test xor */ 1026ff7b0479SSaeed Bishara dest_dma = dma_map_page(dma_chan->device->dev, dest, 0, PAGE_SIZE, 1027ff7b0479SSaeed Bishara DMA_FROM_DEVICE); 1028ff7b0479SSaeed Bishara 1029ff7b0479SSaeed Bishara for (i = 0; i < MV_XOR_NUM_SRC_TEST; i++) 1030ff7b0479SSaeed Bishara dma_srcs[i] = dma_map_page(dma_chan->device->dev, xor_srcs[i], 1031ff7b0479SSaeed Bishara 0, PAGE_SIZE, DMA_TO_DEVICE); 1032ff7b0479SSaeed Bishara 1033ff7b0479SSaeed Bishara tx = mv_xor_prep_dma_xor(dma_chan, dest_dma, dma_srcs, 1034ff7b0479SSaeed Bishara MV_XOR_NUM_SRC_TEST, PAGE_SIZE, 0); 1035ff7b0479SSaeed Bishara 1036ff7b0479SSaeed Bishara cookie = mv_xor_tx_submit(tx); 1037ff7b0479SSaeed Bishara mv_xor_issue_pending(dma_chan); 1038ff7b0479SSaeed Bishara async_tx_ack(tx); 1039ff7b0479SSaeed Bishara msleep(8); 1040ff7b0479SSaeed Bishara 104107934481SLinus Walleij if (mv_xor_status(dma_chan, cookie, NULL) != 1042ff7b0479SSaeed Bishara DMA_SUCCESS) { 1043a3fc74bcSThomas Petazzoni dev_err(dma_chan->device->dev, 1044ff7b0479SSaeed Bishara "Self-test xor timed out, disabling\n"); 1045ff7b0479SSaeed Bishara err = -ENODEV; 1046ff7b0479SSaeed Bishara goto free_resources; 1047ff7b0479SSaeed Bishara } 1048ff7b0479SSaeed Bishara 1049c35064c4SThomas Petazzoni dma_sync_single_for_cpu(dma_chan->device->dev, dest_dma, 1050ff7b0479SSaeed Bishara PAGE_SIZE, DMA_FROM_DEVICE); 1051ff7b0479SSaeed Bishara for (i = 0; i < (PAGE_SIZE / sizeof(u32)); i++) { 1052ff7b0479SSaeed Bishara u32 *ptr = page_address(dest); 1053ff7b0479SSaeed Bishara if (ptr[i] != cmp_word) { 1054a3fc74bcSThomas Petazzoni dev_err(dma_chan->device->dev, 1055ff7b0479SSaeed Bishara "Self-test xor failed compare, disabling." 1056ff7b0479SSaeed Bishara " index %d, data %x, expected %x\n", i, 1057ff7b0479SSaeed Bishara ptr[i], cmp_word); 1058ff7b0479SSaeed Bishara err = -ENODEV; 1059ff7b0479SSaeed Bishara goto free_resources; 1060ff7b0479SSaeed Bishara } 1061ff7b0479SSaeed Bishara } 1062ff7b0479SSaeed Bishara 1063ff7b0479SSaeed Bishara free_resources: 1064ff7b0479SSaeed Bishara mv_xor_free_chan_resources(dma_chan); 1065ff7b0479SSaeed Bishara out: 1066ff7b0479SSaeed Bishara src_idx = MV_XOR_NUM_SRC_TEST; 1067ff7b0479SSaeed Bishara while (src_idx--) 1068ff7b0479SSaeed Bishara __free_page(xor_srcs[src_idx]); 1069ff7b0479SSaeed Bishara __free_page(dest); 1070ff7b0479SSaeed Bishara return err; 1071ff7b0479SSaeed Bishara } 1072ff7b0479SSaeed Bishara 107334c93c86SAndrew Lunn /* This driver does not implement any of the optional DMA operations. */ 107434c93c86SAndrew Lunn static int 107534c93c86SAndrew Lunn mv_xor_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd, 107634c93c86SAndrew Lunn unsigned long arg) 1077ff7b0479SSaeed Bishara { 107834c93c86SAndrew Lunn return -ENOSYS; 107934c93c86SAndrew Lunn } 108034c93c86SAndrew Lunn 10811ef48a26SThomas Petazzoni static int mv_xor_channel_remove(struct mv_xor_chan *mv_chan) 1082ff7b0479SSaeed Bishara { 1083ff7b0479SSaeed Bishara struct dma_chan *chan, *_chan; 10841ef48a26SThomas Petazzoni struct device *dev = mv_chan->dmadev.dev; 1085ff7b0479SSaeed Bishara 10861ef48a26SThomas Petazzoni dma_async_device_unregister(&mv_chan->dmadev); 1087ff7b0479SSaeed Bishara 1088b503fa01SThomas Petazzoni dma_free_coherent(dev, MV_XOR_POOL_SIZE, 10891ef48a26SThomas Petazzoni mv_chan->dma_desc_pool_virt, mv_chan->dma_desc_pool); 1090ff7b0479SSaeed Bishara 10911ef48a26SThomas Petazzoni list_for_each_entry_safe(chan, _chan, &mv_chan->dmadev.channels, 1092ff7b0479SSaeed Bishara device_node) { 1093ff7b0479SSaeed Bishara list_del(&chan->device_node); 1094ff7b0479SSaeed Bishara } 1095ff7b0479SSaeed Bishara 109688eb92cbSThomas Petazzoni free_irq(mv_chan->irq, mv_chan); 109788eb92cbSThomas Petazzoni 1098ff7b0479SSaeed Bishara return 0; 1099ff7b0479SSaeed Bishara } 1100ff7b0479SSaeed Bishara 11011ef48a26SThomas Petazzoni static struct mv_xor_chan * 1102297eedbaSThomas Petazzoni mv_xor_channel_add(struct mv_xor_device *xordev, 1103a6b4a9d2SThomas Petazzoni struct platform_device *pdev, 1104b503fa01SThomas Petazzoni int idx, dma_cap_mask_t cap_mask, int irq) 1105ff7b0479SSaeed Bishara { 1106ff7b0479SSaeed Bishara int ret = 0; 1107ff7b0479SSaeed Bishara struct mv_xor_chan *mv_chan; 1108ff7b0479SSaeed Bishara struct dma_device *dma_dev; 1109ff7b0479SSaeed Bishara 11101ef48a26SThomas Petazzoni mv_chan = devm_kzalloc(&pdev->dev, sizeof(*mv_chan), GFP_KERNEL); 11111ef48a26SThomas Petazzoni if (!mv_chan) { 11121ef48a26SThomas Petazzoni ret = -ENOMEM; 11131ef48a26SThomas Petazzoni goto err_free_dma; 11141ef48a26SThomas Petazzoni } 1115ff7b0479SSaeed Bishara 11169aedbdbaSThomas Petazzoni mv_chan->idx = idx; 111788eb92cbSThomas Petazzoni mv_chan->irq = irq; 1118ff7b0479SSaeed Bishara 11191ef48a26SThomas Petazzoni dma_dev = &mv_chan->dmadev; 1120ff7b0479SSaeed Bishara 1121ff7b0479SSaeed Bishara /* allocate coherent memory for hardware descriptors 1122ff7b0479SSaeed Bishara * note: writecombine gives slightly better performance, but 1123ff7b0479SSaeed Bishara * requires that we explicitly flush the writes 1124ff7b0479SSaeed Bishara */ 11251ef48a26SThomas Petazzoni mv_chan->dma_desc_pool_virt = 1126b503fa01SThomas Petazzoni dma_alloc_writecombine(&pdev->dev, MV_XOR_POOL_SIZE, 11271ef48a26SThomas Petazzoni &mv_chan->dma_desc_pool, GFP_KERNEL); 11281ef48a26SThomas Petazzoni if (!mv_chan->dma_desc_pool_virt) 1129a6b4a9d2SThomas Petazzoni return ERR_PTR(-ENOMEM); 1130ff7b0479SSaeed Bishara 1131ff7b0479SSaeed Bishara /* discover transaction capabilites from the platform data */ 1132a6b4a9d2SThomas Petazzoni dma_dev->cap_mask = cap_mask; 1133ff7b0479SSaeed Bishara 1134ff7b0479SSaeed Bishara INIT_LIST_HEAD(&dma_dev->channels); 1135ff7b0479SSaeed Bishara 1136ff7b0479SSaeed Bishara /* set base routines */ 1137ff7b0479SSaeed Bishara dma_dev->device_alloc_chan_resources = mv_xor_alloc_chan_resources; 1138ff7b0479SSaeed Bishara dma_dev->device_free_chan_resources = mv_xor_free_chan_resources; 113907934481SLinus Walleij dma_dev->device_tx_status = mv_xor_status; 1140ff7b0479SSaeed Bishara dma_dev->device_issue_pending = mv_xor_issue_pending; 114134c93c86SAndrew Lunn dma_dev->device_control = mv_xor_control; 1142ff7b0479SSaeed Bishara dma_dev->dev = &pdev->dev; 1143ff7b0479SSaeed Bishara 1144ff7b0479SSaeed Bishara /* set prep routines based on capability */ 1145ff7b0479SSaeed Bishara if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask)) 1146ff7b0479SSaeed Bishara dma_dev->device_prep_dma_memcpy = mv_xor_prep_dma_memcpy; 1147ff7b0479SSaeed Bishara if (dma_has_cap(DMA_MEMSET, dma_dev->cap_mask)) 1148ff7b0479SSaeed Bishara dma_dev->device_prep_dma_memset = mv_xor_prep_dma_memset; 1149ff7b0479SSaeed Bishara if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) { 1150c019894eSJoe Perches dma_dev->max_xor = 8; 1151ff7b0479SSaeed Bishara dma_dev->device_prep_dma_xor = mv_xor_prep_dma_xor; 1152ff7b0479SSaeed Bishara } 1153ff7b0479SSaeed Bishara 1154297eedbaSThomas Petazzoni mv_chan->mmr_base = xordev->xor_base; 1155ff7b0479SSaeed Bishara if (!mv_chan->mmr_base) { 1156ff7b0479SSaeed Bishara ret = -ENOMEM; 1157ff7b0479SSaeed Bishara goto err_free_dma; 1158ff7b0479SSaeed Bishara } 1159ff7b0479SSaeed Bishara tasklet_init(&mv_chan->irq_tasklet, mv_xor_tasklet, (unsigned long) 1160ff7b0479SSaeed Bishara mv_chan); 1161ff7b0479SSaeed Bishara 1162ff7b0479SSaeed Bishara /* clear errors before enabling interrupts */ 1163ff7b0479SSaeed Bishara mv_xor_device_clear_err_status(mv_chan); 1164ff7b0479SSaeed Bishara 11652d0a0745SThomas Petazzoni ret = request_irq(mv_chan->irq, mv_xor_interrupt_handler, 1166ff7b0479SSaeed Bishara 0, dev_name(&pdev->dev), mv_chan); 1167ff7b0479SSaeed Bishara if (ret) 1168ff7b0479SSaeed Bishara goto err_free_dma; 1169ff7b0479SSaeed Bishara 1170ff7b0479SSaeed Bishara mv_chan_unmask_interrupts(mv_chan); 1171ff7b0479SSaeed Bishara 1172ff7b0479SSaeed Bishara mv_set_mode(mv_chan, DMA_MEMCPY); 1173ff7b0479SSaeed Bishara 1174ff7b0479SSaeed Bishara spin_lock_init(&mv_chan->lock); 1175ff7b0479SSaeed Bishara INIT_LIST_HEAD(&mv_chan->chain); 1176ff7b0479SSaeed Bishara INIT_LIST_HEAD(&mv_chan->completed_slots); 1177ff7b0479SSaeed Bishara INIT_LIST_HEAD(&mv_chan->all_slots); 117898817b99SThomas Petazzoni mv_chan->dmachan.device = dma_dev; 117998817b99SThomas Petazzoni dma_cookie_init(&mv_chan->dmachan); 1180ff7b0479SSaeed Bishara 118198817b99SThomas Petazzoni list_add_tail(&mv_chan->dmachan.device_node, &dma_dev->channels); 1182ff7b0479SSaeed Bishara 1183ff7b0479SSaeed Bishara if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask)) { 1184275cc0c8SThomas Petazzoni ret = mv_xor_memcpy_self_test(mv_chan); 1185ff7b0479SSaeed Bishara dev_dbg(&pdev->dev, "memcpy self test returned %d\n", ret); 1186ff7b0479SSaeed Bishara if (ret) 11872d0a0745SThomas Petazzoni goto err_free_irq; 1188ff7b0479SSaeed Bishara } 1189ff7b0479SSaeed Bishara 1190ff7b0479SSaeed Bishara if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) { 1191275cc0c8SThomas Petazzoni ret = mv_xor_xor_self_test(mv_chan); 1192ff7b0479SSaeed Bishara dev_dbg(&pdev->dev, "xor self test returned %d\n", ret); 1193ff7b0479SSaeed Bishara if (ret) 11942d0a0745SThomas Petazzoni goto err_free_irq; 1195ff7b0479SSaeed Bishara } 1196ff7b0479SSaeed Bishara 1197a3fc74bcSThomas Petazzoni dev_info(&pdev->dev, "Marvell XOR: " 1198ff7b0479SSaeed Bishara "( %s%s%s%s)\n", 1199ff7b0479SSaeed Bishara dma_has_cap(DMA_XOR, dma_dev->cap_mask) ? "xor " : "", 1200ff7b0479SSaeed Bishara dma_has_cap(DMA_MEMSET, dma_dev->cap_mask) ? "fill " : "", 1201ff7b0479SSaeed Bishara dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask) ? "cpy " : "", 1202ff7b0479SSaeed Bishara dma_has_cap(DMA_INTERRUPT, dma_dev->cap_mask) ? "intr " : ""); 1203ff7b0479SSaeed Bishara 1204ff7b0479SSaeed Bishara dma_async_device_register(dma_dev); 12051ef48a26SThomas Petazzoni return mv_chan; 1206ff7b0479SSaeed Bishara 12072d0a0745SThomas Petazzoni err_free_irq: 12082d0a0745SThomas Petazzoni free_irq(mv_chan->irq, mv_chan); 1209ff7b0479SSaeed Bishara err_free_dma: 1210b503fa01SThomas Petazzoni dma_free_coherent(&pdev->dev, MV_XOR_POOL_SIZE, 12111ef48a26SThomas Petazzoni mv_chan->dma_desc_pool_virt, mv_chan->dma_desc_pool); 1212a6b4a9d2SThomas Petazzoni return ERR_PTR(ret); 1213ff7b0479SSaeed Bishara } 1214ff7b0479SSaeed Bishara 1215ff7b0479SSaeed Bishara static void 1216297eedbaSThomas Petazzoni mv_xor_conf_mbus_windows(struct mv_xor_device *xordev, 121763a9332bSAndrew Lunn const struct mbus_dram_target_info *dram) 1218ff7b0479SSaeed Bishara { 1219297eedbaSThomas Petazzoni void __iomem *base = xordev->xor_base; 1220ff7b0479SSaeed Bishara u32 win_enable = 0; 1221ff7b0479SSaeed Bishara int i; 1222ff7b0479SSaeed Bishara 1223ff7b0479SSaeed Bishara for (i = 0; i < 8; i++) { 1224ff7b0479SSaeed Bishara writel(0, base + WINDOW_BASE(i)); 1225ff7b0479SSaeed Bishara writel(0, base + WINDOW_SIZE(i)); 1226ff7b0479SSaeed Bishara if (i < 4) 1227ff7b0479SSaeed Bishara writel(0, base + WINDOW_REMAP_HIGH(i)); 1228ff7b0479SSaeed Bishara } 1229ff7b0479SSaeed Bishara 1230ff7b0479SSaeed Bishara for (i = 0; i < dram->num_cs; i++) { 123163a9332bSAndrew Lunn const struct mbus_dram_window *cs = dram->cs + i; 1232ff7b0479SSaeed Bishara 1233ff7b0479SSaeed Bishara writel((cs->base & 0xffff0000) | 1234ff7b0479SSaeed Bishara (cs->mbus_attr << 8) | 1235ff7b0479SSaeed Bishara dram->mbus_dram_target_id, base + WINDOW_BASE(i)); 1236ff7b0479SSaeed Bishara writel((cs->size - 1) & 0xffff0000, base + WINDOW_SIZE(i)); 1237ff7b0479SSaeed Bishara 1238ff7b0479SSaeed Bishara win_enable |= (1 << i); 1239ff7b0479SSaeed Bishara win_enable |= 3 << (16 + (2 * i)); 1240ff7b0479SSaeed Bishara } 1241ff7b0479SSaeed Bishara 1242ff7b0479SSaeed Bishara writel(win_enable, base + WINDOW_BAR_ENABLE(0)); 1243ff7b0479SSaeed Bishara writel(win_enable, base + WINDOW_BAR_ENABLE(1)); 1244c4b4b732SThomas Petazzoni writel(0, base + WINDOW_OVERRIDE_CTRL(0)); 1245c4b4b732SThomas Petazzoni writel(0, base + WINDOW_OVERRIDE_CTRL(1)); 1246ff7b0479SSaeed Bishara } 1247ff7b0479SSaeed Bishara 1248c2714334SLinus Torvalds static int mv_xor_probe(struct platform_device *pdev) 1249ff7b0479SSaeed Bishara { 125063a9332bSAndrew Lunn const struct mbus_dram_target_info *dram; 1251297eedbaSThomas Petazzoni struct mv_xor_device *xordev; 12527dde453dSThomas Petazzoni struct mv_xor_platform_data *pdata = pdev->dev.platform_data; 1253ff7b0479SSaeed Bishara struct resource *res; 125460d151f3SThomas Petazzoni int i, ret; 1255ff7b0479SSaeed Bishara 125661971656SThomas Petazzoni dev_notice(&pdev->dev, "Marvell XOR driver\n"); 1257ff7b0479SSaeed Bishara 1258297eedbaSThomas Petazzoni xordev = devm_kzalloc(&pdev->dev, sizeof(*xordev), GFP_KERNEL); 1259297eedbaSThomas Petazzoni if (!xordev) 1260ff7b0479SSaeed Bishara return -ENOMEM; 1261ff7b0479SSaeed Bishara 1262ff7b0479SSaeed Bishara res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1263ff7b0479SSaeed Bishara if (!res) 1264ff7b0479SSaeed Bishara return -ENODEV; 1265ff7b0479SSaeed Bishara 1266297eedbaSThomas Petazzoni xordev->xor_base = devm_ioremap(&pdev->dev, res->start, 12674de1ba15SH Hartley Sweeten resource_size(res)); 1268297eedbaSThomas Petazzoni if (!xordev->xor_base) 1269ff7b0479SSaeed Bishara return -EBUSY; 1270ff7b0479SSaeed Bishara 1271ff7b0479SSaeed Bishara res = platform_get_resource(pdev, IORESOURCE_MEM, 1); 1272ff7b0479SSaeed Bishara if (!res) 1273ff7b0479SSaeed Bishara return -ENODEV; 1274ff7b0479SSaeed Bishara 1275297eedbaSThomas Petazzoni xordev->xor_high_base = devm_ioremap(&pdev->dev, res->start, 12764de1ba15SH Hartley Sweeten resource_size(res)); 1277297eedbaSThomas Petazzoni if (!xordev->xor_high_base) 1278ff7b0479SSaeed Bishara return -EBUSY; 1279ff7b0479SSaeed Bishara 1280297eedbaSThomas Petazzoni platform_set_drvdata(pdev, xordev); 1281ff7b0479SSaeed Bishara 1282ff7b0479SSaeed Bishara /* 1283ff7b0479SSaeed Bishara * (Re-)program MBUS remapping windows if we are asked to. 1284ff7b0479SSaeed Bishara */ 128563a9332bSAndrew Lunn dram = mv_mbus_dram_info(); 128663a9332bSAndrew Lunn if (dram) 1287297eedbaSThomas Petazzoni mv_xor_conf_mbus_windows(xordev, dram); 1288ff7b0479SSaeed Bishara 1289c510182bSAndrew Lunn /* Not all platforms can gate the clock, so it is not 1290c510182bSAndrew Lunn * an error if the clock does not exists. 1291c510182bSAndrew Lunn */ 1292297eedbaSThomas Petazzoni xordev->clk = clk_get(&pdev->dev, NULL); 1293297eedbaSThomas Petazzoni if (!IS_ERR(xordev->clk)) 1294297eedbaSThomas Petazzoni clk_prepare_enable(xordev->clk); 1295c510182bSAndrew Lunn 1296f7d12ef5SThomas Petazzoni if (pdev->dev.of_node) { 1297f7d12ef5SThomas Petazzoni struct device_node *np; 1298f7d12ef5SThomas Petazzoni int i = 0; 1299f7d12ef5SThomas Petazzoni 1300f7d12ef5SThomas Petazzoni for_each_child_of_node(pdev->dev.of_node, np) { 1301f7d12ef5SThomas Petazzoni dma_cap_mask_t cap_mask; 1302f7d12ef5SThomas Petazzoni int irq; 1303f7d12ef5SThomas Petazzoni 1304f7d12ef5SThomas Petazzoni dma_cap_zero(cap_mask); 1305f7d12ef5SThomas Petazzoni if (of_property_read_bool(np, "dmacap,memcpy")) 1306f7d12ef5SThomas Petazzoni dma_cap_set(DMA_MEMCPY, cap_mask); 1307f7d12ef5SThomas Petazzoni if (of_property_read_bool(np, "dmacap,xor")) 1308f7d12ef5SThomas Petazzoni dma_cap_set(DMA_XOR, cap_mask); 1309f7d12ef5SThomas Petazzoni if (of_property_read_bool(np, "dmacap,memset")) 1310f7d12ef5SThomas Petazzoni dma_cap_set(DMA_MEMSET, cap_mask); 1311f7d12ef5SThomas Petazzoni if (of_property_read_bool(np, "dmacap,interrupt")) 1312f7d12ef5SThomas Petazzoni dma_cap_set(DMA_INTERRUPT, cap_mask); 1313f7d12ef5SThomas Petazzoni 1314f7d12ef5SThomas Petazzoni irq = irq_of_parse_and_map(np, 0); 1315f8eb9e7dSThomas Petazzoni if (!irq) { 1316f8eb9e7dSThomas Petazzoni ret = -ENODEV; 1317f7d12ef5SThomas Petazzoni goto err_channel_add; 1318f7d12ef5SThomas Petazzoni } 1319f7d12ef5SThomas Petazzoni 1320f7d12ef5SThomas Petazzoni xordev->channels[i] = 1321f7d12ef5SThomas Petazzoni mv_xor_channel_add(xordev, pdev, i, 1322f7d12ef5SThomas Petazzoni cap_mask, irq); 1323f7d12ef5SThomas Petazzoni if (IS_ERR(xordev->channels[i])) { 1324f7d12ef5SThomas Petazzoni ret = PTR_ERR(xordev->channels[i]); 132573d9cdcaSThomas Petazzoni xordev->channels[i] = NULL; 1326f7d12ef5SThomas Petazzoni irq_dispose_mapping(irq); 1327f7d12ef5SThomas Petazzoni goto err_channel_add; 1328f7d12ef5SThomas Petazzoni } 1329f7d12ef5SThomas Petazzoni 1330f7d12ef5SThomas Petazzoni i++; 1331f7d12ef5SThomas Petazzoni } 1332f7d12ef5SThomas Petazzoni } else if (pdata && pdata->channels) { 133360d151f3SThomas Petazzoni for (i = 0; i < MV_XOR_MAX_CHANNELS; i++) { 1334e39f6ec1SThomas Petazzoni struct mv_xor_channel_data *cd; 133560d151f3SThomas Petazzoni int irq; 133660d151f3SThomas Petazzoni 133760d151f3SThomas Petazzoni cd = &pdata->channels[i]; 133860d151f3SThomas Petazzoni if (!cd) { 133960d151f3SThomas Petazzoni ret = -ENODEV; 134060d151f3SThomas Petazzoni goto err_channel_add; 134160d151f3SThomas Petazzoni } 134260d151f3SThomas Petazzoni 134360d151f3SThomas Petazzoni irq = platform_get_irq(pdev, i); 134460d151f3SThomas Petazzoni if (irq < 0) { 134560d151f3SThomas Petazzoni ret = irq; 134660d151f3SThomas Petazzoni goto err_channel_add; 134760d151f3SThomas Petazzoni } 134860d151f3SThomas Petazzoni 1349297eedbaSThomas Petazzoni xordev->channels[i] = 13509aedbdbaSThomas Petazzoni mv_xor_channel_add(xordev, pdev, i, 1351b503fa01SThomas Petazzoni cd->cap_mask, irq); 1352297eedbaSThomas Petazzoni if (IS_ERR(xordev->channels[i])) { 1353297eedbaSThomas Petazzoni ret = PTR_ERR(xordev->channels[i]); 135460d151f3SThomas Petazzoni goto err_channel_add; 135560d151f3SThomas Petazzoni } 135660d151f3SThomas Petazzoni } 135760d151f3SThomas Petazzoni } 135860d151f3SThomas Petazzoni 1359ff7b0479SSaeed Bishara return 0; 136060d151f3SThomas Petazzoni 136160d151f3SThomas Petazzoni err_channel_add: 136260d151f3SThomas Petazzoni for (i = 0; i < MV_XOR_MAX_CHANNELS; i++) 1363f7d12ef5SThomas Petazzoni if (xordev->channels[i]) { 1364ab6e439fSThomas Petazzoni mv_xor_channel_remove(xordev->channels[i]); 1365f7d12ef5SThomas Petazzoni if (pdev->dev.of_node) 1366f7d12ef5SThomas Petazzoni irq_dispose_mapping(xordev->channels[i]->irq); 1367f7d12ef5SThomas Petazzoni } 136860d151f3SThomas Petazzoni 1369*dab92064SThomas Petazzoni if (!IS_ERR(xordev->clk)) { 1370297eedbaSThomas Petazzoni clk_disable_unprepare(xordev->clk); 1371297eedbaSThomas Petazzoni clk_put(xordev->clk); 1372*dab92064SThomas Petazzoni } 1373*dab92064SThomas Petazzoni 137460d151f3SThomas Petazzoni return ret; 1375ff7b0479SSaeed Bishara } 1376ff7b0479SSaeed Bishara 1377c2714334SLinus Torvalds static int mv_xor_remove(struct platform_device *pdev) 1378ff7b0479SSaeed Bishara { 1379297eedbaSThomas Petazzoni struct mv_xor_device *xordev = platform_get_drvdata(pdev); 138060d151f3SThomas Petazzoni int i; 138160d151f3SThomas Petazzoni 138260d151f3SThomas Petazzoni for (i = 0; i < MV_XOR_MAX_CHANNELS; i++) { 1383297eedbaSThomas Petazzoni if (xordev->channels[i]) 1384297eedbaSThomas Petazzoni mv_xor_channel_remove(xordev->channels[i]); 138560d151f3SThomas Petazzoni } 1386c510182bSAndrew Lunn 1387297eedbaSThomas Petazzoni if (!IS_ERR(xordev->clk)) { 1388297eedbaSThomas Petazzoni clk_disable_unprepare(xordev->clk); 1389297eedbaSThomas Petazzoni clk_put(xordev->clk); 1390c510182bSAndrew Lunn } 1391c510182bSAndrew Lunn 1392ff7b0479SSaeed Bishara return 0; 1393ff7b0479SSaeed Bishara } 1394ff7b0479SSaeed Bishara 1395f7d12ef5SThomas Petazzoni #ifdef CONFIG_OF 1396c2714334SLinus Torvalds static struct of_device_id mv_xor_dt_ids[] = { 1397f7d12ef5SThomas Petazzoni { .compatible = "marvell,orion-xor", }, 1398f7d12ef5SThomas Petazzoni {}, 1399f7d12ef5SThomas Petazzoni }; 1400f7d12ef5SThomas Petazzoni MODULE_DEVICE_TABLE(of, mv_xor_dt_ids); 1401f7d12ef5SThomas Petazzoni #endif 1402f7d12ef5SThomas Petazzoni 1403ff7b0479SSaeed Bishara static struct platform_driver mv_xor_driver = { 1404ff7b0479SSaeed Bishara .probe = mv_xor_probe, 1405a7d6e3ecSBill Pemberton .remove = mv_xor_remove, 1406ff7b0479SSaeed Bishara .driver = { 1407ff7b0479SSaeed Bishara .owner = THIS_MODULE, 1408ff7b0479SSaeed Bishara .name = MV_XOR_NAME, 1409f7d12ef5SThomas Petazzoni .of_match_table = of_match_ptr(mv_xor_dt_ids), 1410ff7b0479SSaeed Bishara }, 1411ff7b0479SSaeed Bishara }; 1412ff7b0479SSaeed Bishara 1413ff7b0479SSaeed Bishara 1414ff7b0479SSaeed Bishara static int __init mv_xor_init(void) 1415ff7b0479SSaeed Bishara { 141661971656SThomas Petazzoni return platform_driver_register(&mv_xor_driver); 1417ff7b0479SSaeed Bishara } 1418ff7b0479SSaeed Bishara module_init(mv_xor_init); 1419ff7b0479SSaeed Bishara 1420ff7b0479SSaeed Bishara /* it's currently unsafe to unload this module */ 1421ff7b0479SSaeed Bishara #if 0 1422ff7b0479SSaeed Bishara static void __exit mv_xor_exit(void) 1423ff7b0479SSaeed Bishara { 1424ff7b0479SSaeed Bishara platform_driver_unregister(&mv_xor_driver); 1425ff7b0479SSaeed Bishara return; 1426ff7b0479SSaeed Bishara } 1427ff7b0479SSaeed Bishara 1428ff7b0479SSaeed Bishara module_exit(mv_xor_exit); 1429ff7b0479SSaeed Bishara #endif 1430ff7b0479SSaeed Bishara 1431ff7b0479SSaeed Bishara MODULE_AUTHOR("Saeed Bishara <saeed@marvell.com>"); 1432ff7b0479SSaeed Bishara MODULE_DESCRIPTION("DMA engine driver for Marvell's XOR engine"); 1433ff7b0479SSaeed Bishara MODULE_LICENSE("GPL"); 1434