1ff7b0479SSaeed Bishara /* 2ff7b0479SSaeed Bishara * offload engine driver for the Marvell XOR engine 3ff7b0479SSaeed Bishara * Copyright (C) 2007, 2008, Marvell International Ltd. 4ff7b0479SSaeed Bishara * 5ff7b0479SSaeed Bishara * This program is free software; you can redistribute it and/or modify it 6ff7b0479SSaeed Bishara * under the terms and conditions of the GNU General Public License, 7ff7b0479SSaeed Bishara * version 2, as published by the Free Software Foundation. 8ff7b0479SSaeed Bishara * 9ff7b0479SSaeed Bishara * This program is distributed in the hope it will be useful, but WITHOUT 10ff7b0479SSaeed Bishara * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11ff7b0479SSaeed Bishara * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 12ff7b0479SSaeed Bishara * more details. 13ff7b0479SSaeed Bishara * 14ff7b0479SSaeed Bishara * You should have received a copy of the GNU General Public License along with 15ff7b0479SSaeed Bishara * this program; if not, write to the Free Software Foundation, Inc., 16ff7b0479SSaeed Bishara * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. 17ff7b0479SSaeed Bishara */ 18ff7b0479SSaeed Bishara 19ff7b0479SSaeed Bishara #include <linux/init.h> 20ff7b0479SSaeed Bishara #include <linux/module.h> 215a0e3ad6STejun Heo #include <linux/slab.h> 22ff7b0479SSaeed Bishara #include <linux/delay.h> 23ff7b0479SSaeed Bishara #include <linux/dma-mapping.h> 24ff7b0479SSaeed Bishara #include <linux/spinlock.h> 25ff7b0479SSaeed Bishara #include <linux/interrupt.h> 26ff7b0479SSaeed Bishara #include <linux/platform_device.h> 27ff7b0479SSaeed Bishara #include <linux/memory.h> 28c510182bSAndrew Lunn #include <linux/clk.h> 29f7d12ef5SThomas Petazzoni #include <linux/of.h> 30f7d12ef5SThomas Petazzoni #include <linux/of_irq.h> 31f7d12ef5SThomas Petazzoni #include <linux/irqdomain.h> 32c02cecb9SArnd Bergmann #include <linux/platform_data/dma-mv_xor.h> 33d2ebfb33SRussell King - ARM Linux 34d2ebfb33SRussell King - ARM Linux #include "dmaengine.h" 35ff7b0479SSaeed Bishara #include "mv_xor.h" 36ff7b0479SSaeed Bishara 37ff7b0479SSaeed Bishara static void mv_xor_issue_pending(struct dma_chan *chan); 38ff7b0479SSaeed Bishara 39ff7b0479SSaeed Bishara #define to_mv_xor_chan(chan) \ 4098817b99SThomas Petazzoni container_of(chan, struct mv_xor_chan, dmachan) 41ff7b0479SSaeed Bishara 42ff7b0479SSaeed Bishara #define to_mv_xor_slot(tx) \ 43ff7b0479SSaeed Bishara container_of(tx, struct mv_xor_desc_slot, async_tx) 44ff7b0479SSaeed Bishara 45c98c1781SThomas Petazzoni #define mv_chan_to_devp(chan) \ 461ef48a26SThomas Petazzoni ((chan)->dmadev.dev) 47c98c1781SThomas Petazzoni 48ff7b0479SSaeed Bishara static void mv_desc_init(struct mv_xor_desc_slot *desc, unsigned long flags) 49ff7b0479SSaeed Bishara { 50ff7b0479SSaeed Bishara struct mv_xor_desc *hw_desc = desc->hw_desc; 51ff7b0479SSaeed Bishara 52ff7b0479SSaeed Bishara hw_desc->status = (1 << 31); 53ff7b0479SSaeed Bishara hw_desc->phy_next_desc = 0; 54ff7b0479SSaeed Bishara hw_desc->desc_command = (1 << 31); 55ff7b0479SSaeed Bishara } 56ff7b0479SSaeed Bishara 57ff7b0479SSaeed Bishara static void mv_desc_set_byte_count(struct mv_xor_desc_slot *desc, 58ff7b0479SSaeed Bishara u32 byte_count) 59ff7b0479SSaeed Bishara { 60ff7b0479SSaeed Bishara struct mv_xor_desc *hw_desc = desc->hw_desc; 61ff7b0479SSaeed Bishara hw_desc->byte_count = byte_count; 62ff7b0479SSaeed Bishara } 63ff7b0479SSaeed Bishara 64ff7b0479SSaeed Bishara static void mv_desc_set_next_desc(struct mv_xor_desc_slot *desc, 65ff7b0479SSaeed Bishara u32 next_desc_addr) 66ff7b0479SSaeed Bishara { 67ff7b0479SSaeed Bishara struct mv_xor_desc *hw_desc = desc->hw_desc; 68ff7b0479SSaeed Bishara BUG_ON(hw_desc->phy_next_desc); 69ff7b0479SSaeed Bishara hw_desc->phy_next_desc = next_desc_addr; 70ff7b0479SSaeed Bishara } 71ff7b0479SSaeed Bishara 72ff7b0479SSaeed Bishara static void mv_desc_clear_next_desc(struct mv_xor_desc_slot *desc) 73ff7b0479SSaeed Bishara { 74ff7b0479SSaeed Bishara struct mv_xor_desc *hw_desc = desc->hw_desc; 75ff7b0479SSaeed Bishara hw_desc->phy_next_desc = 0; 76ff7b0479SSaeed Bishara } 77ff7b0479SSaeed Bishara 78ff7b0479SSaeed Bishara static void mv_desc_set_dest_addr(struct mv_xor_desc_slot *desc, 79ff7b0479SSaeed Bishara dma_addr_t addr) 80ff7b0479SSaeed Bishara { 81ff7b0479SSaeed Bishara struct mv_xor_desc *hw_desc = desc->hw_desc; 82ff7b0479SSaeed Bishara hw_desc->phy_dest_addr = addr; 83ff7b0479SSaeed Bishara } 84ff7b0479SSaeed Bishara 85ff7b0479SSaeed Bishara static int mv_chan_memset_slot_count(size_t len) 86ff7b0479SSaeed Bishara { 87ff7b0479SSaeed Bishara return 1; 88ff7b0479SSaeed Bishara } 89ff7b0479SSaeed Bishara 90ff7b0479SSaeed Bishara #define mv_chan_memcpy_slot_count(c) mv_chan_memset_slot_count(c) 91ff7b0479SSaeed Bishara 92ff7b0479SSaeed Bishara static void mv_desc_set_src_addr(struct mv_xor_desc_slot *desc, 93ff7b0479SSaeed Bishara int index, dma_addr_t addr) 94ff7b0479SSaeed Bishara { 95ff7b0479SSaeed Bishara struct mv_xor_desc *hw_desc = desc->hw_desc; 96e03bc654SThomas Petazzoni hw_desc->phy_src_addr[mv_phy_src_idx(index)] = addr; 97ff7b0479SSaeed Bishara if (desc->type == DMA_XOR) 98ff7b0479SSaeed Bishara hw_desc->desc_command |= (1 << index); 99ff7b0479SSaeed Bishara } 100ff7b0479SSaeed Bishara 101ff7b0479SSaeed Bishara static u32 mv_chan_get_current_desc(struct mv_xor_chan *chan) 102ff7b0479SSaeed Bishara { 1035733c38aSThomas Petazzoni return readl_relaxed(XOR_CURR_DESC(chan)); 104ff7b0479SSaeed Bishara } 105ff7b0479SSaeed Bishara 106ff7b0479SSaeed Bishara static void mv_chan_set_next_descriptor(struct mv_xor_chan *chan, 107ff7b0479SSaeed Bishara u32 next_desc_addr) 108ff7b0479SSaeed Bishara { 1095733c38aSThomas Petazzoni writel_relaxed(next_desc_addr, XOR_NEXT_DESC(chan)); 110ff7b0479SSaeed Bishara } 111ff7b0479SSaeed Bishara 112ff7b0479SSaeed Bishara static void mv_chan_unmask_interrupts(struct mv_xor_chan *chan) 113ff7b0479SSaeed Bishara { 1145733c38aSThomas Petazzoni u32 val = readl_relaxed(XOR_INTR_MASK(chan)); 115ff7b0479SSaeed Bishara val |= XOR_INTR_MASK_VALUE << (chan->idx * 16); 1165733c38aSThomas Petazzoni writel_relaxed(val, XOR_INTR_MASK(chan)); 117ff7b0479SSaeed Bishara } 118ff7b0479SSaeed Bishara 119ff7b0479SSaeed Bishara static u32 mv_chan_get_intr_cause(struct mv_xor_chan *chan) 120ff7b0479SSaeed Bishara { 1215733c38aSThomas Petazzoni u32 intr_cause = readl_relaxed(XOR_INTR_CAUSE(chan)); 122ff7b0479SSaeed Bishara intr_cause = (intr_cause >> (chan->idx * 16)) & 0xFFFF; 123ff7b0479SSaeed Bishara return intr_cause; 124ff7b0479SSaeed Bishara } 125ff7b0479SSaeed Bishara 126ff7b0479SSaeed Bishara static int mv_is_err_intr(u32 intr_cause) 127ff7b0479SSaeed Bishara { 128ff7b0479SSaeed Bishara if (intr_cause & ((1<<4)|(1<<5)|(1<<6)|(1<<7)|(1<<8)|(1<<9))) 129ff7b0479SSaeed Bishara return 1; 130ff7b0479SSaeed Bishara 131ff7b0479SSaeed Bishara return 0; 132ff7b0479SSaeed Bishara } 133ff7b0479SSaeed Bishara 134ff7b0479SSaeed Bishara static void mv_xor_device_clear_eoc_cause(struct mv_xor_chan *chan) 135ff7b0479SSaeed Bishara { 13686363682SSimon Guinot u32 val = ~(1 << (chan->idx * 16)); 137c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(chan), "%s, val 0x%08x\n", __func__, val); 1385733c38aSThomas Petazzoni writel_relaxed(val, XOR_INTR_CAUSE(chan)); 139ff7b0479SSaeed Bishara } 140ff7b0479SSaeed Bishara 141ff7b0479SSaeed Bishara static void mv_xor_device_clear_err_status(struct mv_xor_chan *chan) 142ff7b0479SSaeed Bishara { 143ff7b0479SSaeed Bishara u32 val = 0xFFFF0000 >> (chan->idx * 16); 1445733c38aSThomas Petazzoni writel_relaxed(val, XOR_INTR_CAUSE(chan)); 145ff7b0479SSaeed Bishara } 146ff7b0479SSaeed Bishara 147ff7b0479SSaeed Bishara static int mv_can_chain(struct mv_xor_desc_slot *desc) 148ff7b0479SSaeed Bishara { 149ff7b0479SSaeed Bishara struct mv_xor_desc_slot *chain_old_tail = list_entry( 150ff7b0479SSaeed Bishara desc->chain_node.prev, struct mv_xor_desc_slot, chain_node); 151ff7b0479SSaeed Bishara 152ff7b0479SSaeed Bishara if (chain_old_tail->type != desc->type) 153ff7b0479SSaeed Bishara return 0; 154ff7b0479SSaeed Bishara 155ff7b0479SSaeed Bishara return 1; 156ff7b0479SSaeed Bishara } 157ff7b0479SSaeed Bishara 158ff7b0479SSaeed Bishara static void mv_set_mode(struct mv_xor_chan *chan, 159ff7b0479SSaeed Bishara enum dma_transaction_type type) 160ff7b0479SSaeed Bishara { 161ff7b0479SSaeed Bishara u32 op_mode; 1625733c38aSThomas Petazzoni u32 config = readl_relaxed(XOR_CONFIG(chan)); 163ff7b0479SSaeed Bishara 164ff7b0479SSaeed Bishara switch (type) { 165ff7b0479SSaeed Bishara case DMA_XOR: 166ff7b0479SSaeed Bishara op_mode = XOR_OPERATION_MODE_XOR; 167ff7b0479SSaeed Bishara break; 168ff7b0479SSaeed Bishara case DMA_MEMCPY: 169ff7b0479SSaeed Bishara op_mode = XOR_OPERATION_MODE_MEMCPY; 170ff7b0479SSaeed Bishara break; 171ff7b0479SSaeed Bishara default: 172c98c1781SThomas Petazzoni dev_err(mv_chan_to_devp(chan), 1731ba151cdSJoe Perches "error: unsupported operation %d\n", 174ff7b0479SSaeed Bishara type); 175ff7b0479SSaeed Bishara BUG(); 176ff7b0479SSaeed Bishara return; 177ff7b0479SSaeed Bishara } 178ff7b0479SSaeed Bishara 179ff7b0479SSaeed Bishara config &= ~0x7; 180ff7b0479SSaeed Bishara config |= op_mode; 181e03bc654SThomas Petazzoni 182e03bc654SThomas Petazzoni #if defined(__BIG_ENDIAN) 183e03bc654SThomas Petazzoni config |= XOR_DESCRIPTOR_SWAP; 184e03bc654SThomas Petazzoni #else 185e03bc654SThomas Petazzoni config &= ~XOR_DESCRIPTOR_SWAP; 186e03bc654SThomas Petazzoni #endif 187e03bc654SThomas Petazzoni 1885733c38aSThomas Petazzoni writel_relaxed(config, XOR_CONFIG(chan)); 189ff7b0479SSaeed Bishara chan->current_type = type; 190ff7b0479SSaeed Bishara } 191ff7b0479SSaeed Bishara 192ff7b0479SSaeed Bishara static void mv_chan_activate(struct mv_xor_chan *chan) 193ff7b0479SSaeed Bishara { 194c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(chan), " activate chan.\n"); 1955a9a55bfSEzequiel Garcia 1965a9a55bfSEzequiel Garcia /* writel ensures all descriptors are flushed before activation */ 1975a9a55bfSEzequiel Garcia writel(BIT(0), XOR_ACTIVATION(chan)); 198ff7b0479SSaeed Bishara } 199ff7b0479SSaeed Bishara 200ff7b0479SSaeed Bishara static char mv_chan_is_busy(struct mv_xor_chan *chan) 201ff7b0479SSaeed Bishara { 2025733c38aSThomas Petazzoni u32 state = readl_relaxed(XOR_ACTIVATION(chan)); 203ff7b0479SSaeed Bishara 204ff7b0479SSaeed Bishara state = (state >> 4) & 0x3; 205ff7b0479SSaeed Bishara 206ff7b0479SSaeed Bishara return (state == 1) ? 1 : 0; 207ff7b0479SSaeed Bishara } 208ff7b0479SSaeed Bishara 209ff7b0479SSaeed Bishara static int mv_chan_xor_slot_count(size_t len, int src_cnt) 210ff7b0479SSaeed Bishara { 211ff7b0479SSaeed Bishara return 1; 212ff7b0479SSaeed Bishara } 213ff7b0479SSaeed Bishara 214ff7b0479SSaeed Bishara /** 215ff7b0479SSaeed Bishara * mv_xor_free_slots - flags descriptor slots for reuse 216ff7b0479SSaeed Bishara * @slot: Slot to free 217ff7b0479SSaeed Bishara * Caller must hold &mv_chan->lock while calling this function 218ff7b0479SSaeed Bishara */ 219ff7b0479SSaeed Bishara static void mv_xor_free_slots(struct mv_xor_chan *mv_chan, 220ff7b0479SSaeed Bishara struct mv_xor_desc_slot *slot) 221ff7b0479SSaeed Bishara { 222c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), "%s %d slot %p\n", 223ff7b0479SSaeed Bishara __func__, __LINE__, slot); 224ff7b0479SSaeed Bishara 225ff7b0479SSaeed Bishara slot->slots_per_op = 0; 226ff7b0479SSaeed Bishara 227ff7b0479SSaeed Bishara } 228ff7b0479SSaeed Bishara 229ff7b0479SSaeed Bishara /* 230ff7b0479SSaeed Bishara * mv_xor_start_new_chain - program the engine to operate on new chain headed by 231ff7b0479SSaeed Bishara * sw_desc 232ff7b0479SSaeed Bishara * Caller must hold &mv_chan->lock while calling this function 233ff7b0479SSaeed Bishara */ 234ff7b0479SSaeed Bishara static void mv_xor_start_new_chain(struct mv_xor_chan *mv_chan, 235ff7b0479SSaeed Bishara struct mv_xor_desc_slot *sw_desc) 236ff7b0479SSaeed Bishara { 237c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), "%s %d: sw_desc %p\n", 238ff7b0479SSaeed Bishara __func__, __LINE__, sw_desc); 239ff7b0479SSaeed Bishara if (sw_desc->type != mv_chan->current_type) 240ff7b0479SSaeed Bishara mv_set_mode(mv_chan, sw_desc->type); 241ff7b0479SSaeed Bishara 242ff7b0479SSaeed Bishara /* set the hardware chain */ 243ff7b0479SSaeed Bishara mv_chan_set_next_descriptor(mv_chan, sw_desc->async_tx.phys); 24448a9db46SBartlomiej Zolnierkiewicz 245ff7b0479SSaeed Bishara mv_chan->pending += sw_desc->slot_cnt; 24698817b99SThomas Petazzoni mv_xor_issue_pending(&mv_chan->dmachan); 247ff7b0479SSaeed Bishara } 248ff7b0479SSaeed Bishara 249ff7b0479SSaeed Bishara static dma_cookie_t 250ff7b0479SSaeed Bishara mv_xor_run_tx_complete_actions(struct mv_xor_desc_slot *desc, 251ff7b0479SSaeed Bishara struct mv_xor_chan *mv_chan, dma_cookie_t cookie) 252ff7b0479SSaeed Bishara { 253ff7b0479SSaeed Bishara BUG_ON(desc->async_tx.cookie < 0); 254ff7b0479SSaeed Bishara 255ff7b0479SSaeed Bishara if (desc->async_tx.cookie > 0) { 256ff7b0479SSaeed Bishara cookie = desc->async_tx.cookie; 257ff7b0479SSaeed Bishara 258ff7b0479SSaeed Bishara /* call the callback (must not sleep or submit new 259ff7b0479SSaeed Bishara * operations to this channel) 260ff7b0479SSaeed Bishara */ 261ff7b0479SSaeed Bishara if (desc->async_tx.callback) 262ff7b0479SSaeed Bishara desc->async_tx.callback( 263ff7b0479SSaeed Bishara desc->async_tx.callback_param); 264ff7b0479SSaeed Bishara 265d38a8c62SDan Williams dma_descriptor_unmap(&desc->async_tx); 26654f8d501SBartlomiej Zolnierkiewicz if (desc->group_head) 267ff7b0479SSaeed Bishara desc->group_head = NULL; 268ff7b0479SSaeed Bishara } 269ff7b0479SSaeed Bishara 270ff7b0479SSaeed Bishara /* run dependent operations */ 27107f2211eSDan Williams dma_run_dependencies(&desc->async_tx); 272ff7b0479SSaeed Bishara 273ff7b0479SSaeed Bishara return cookie; 274ff7b0479SSaeed Bishara } 275ff7b0479SSaeed Bishara 276ff7b0479SSaeed Bishara static int 277ff7b0479SSaeed Bishara mv_xor_clean_completed_slots(struct mv_xor_chan *mv_chan) 278ff7b0479SSaeed Bishara { 279ff7b0479SSaeed Bishara struct mv_xor_desc_slot *iter, *_iter; 280ff7b0479SSaeed Bishara 281c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), "%s %d\n", __func__, __LINE__); 282ff7b0479SSaeed Bishara list_for_each_entry_safe(iter, _iter, &mv_chan->completed_slots, 283ff7b0479SSaeed Bishara completed_node) { 284ff7b0479SSaeed Bishara 285ff7b0479SSaeed Bishara if (async_tx_test_ack(&iter->async_tx)) { 286ff7b0479SSaeed Bishara list_del(&iter->completed_node); 287ff7b0479SSaeed Bishara mv_xor_free_slots(mv_chan, iter); 288ff7b0479SSaeed Bishara } 289ff7b0479SSaeed Bishara } 290ff7b0479SSaeed Bishara return 0; 291ff7b0479SSaeed Bishara } 292ff7b0479SSaeed Bishara 293ff7b0479SSaeed Bishara static int 294ff7b0479SSaeed Bishara mv_xor_clean_slot(struct mv_xor_desc_slot *desc, 295ff7b0479SSaeed Bishara struct mv_xor_chan *mv_chan) 296ff7b0479SSaeed Bishara { 297c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), "%s %d: desc %p flags %d\n", 298ff7b0479SSaeed Bishara __func__, __LINE__, desc, desc->async_tx.flags); 299ff7b0479SSaeed Bishara list_del(&desc->chain_node); 300ff7b0479SSaeed Bishara /* the client is allowed to attach dependent operations 301ff7b0479SSaeed Bishara * until 'ack' is set 302ff7b0479SSaeed Bishara */ 303ff7b0479SSaeed Bishara if (!async_tx_test_ack(&desc->async_tx)) { 304ff7b0479SSaeed Bishara /* move this slot to the completed_slots */ 305ff7b0479SSaeed Bishara list_add_tail(&desc->completed_node, &mv_chan->completed_slots); 306ff7b0479SSaeed Bishara return 0; 307ff7b0479SSaeed Bishara } 308ff7b0479SSaeed Bishara 309ff7b0479SSaeed Bishara mv_xor_free_slots(mv_chan, desc); 310ff7b0479SSaeed Bishara return 0; 311ff7b0479SSaeed Bishara } 312ff7b0479SSaeed Bishara 313ff7b0479SSaeed Bishara static void __mv_xor_slot_cleanup(struct mv_xor_chan *mv_chan) 314ff7b0479SSaeed Bishara { 315ff7b0479SSaeed Bishara struct mv_xor_desc_slot *iter, *_iter; 316ff7b0479SSaeed Bishara dma_cookie_t cookie = 0; 317ff7b0479SSaeed Bishara int busy = mv_chan_is_busy(mv_chan); 318ff7b0479SSaeed Bishara u32 current_desc = mv_chan_get_current_desc(mv_chan); 319ff7b0479SSaeed Bishara int seen_current = 0; 320ff7b0479SSaeed Bishara 321c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), "%s %d\n", __func__, __LINE__); 322c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), "current_desc %x\n", current_desc); 323ff7b0479SSaeed Bishara mv_xor_clean_completed_slots(mv_chan); 324ff7b0479SSaeed Bishara 325ff7b0479SSaeed Bishara /* free completed slots from the chain starting with 326ff7b0479SSaeed Bishara * the oldest descriptor 327ff7b0479SSaeed Bishara */ 328ff7b0479SSaeed Bishara 329ff7b0479SSaeed Bishara list_for_each_entry_safe(iter, _iter, &mv_chan->chain, 330ff7b0479SSaeed Bishara chain_node) { 331ff7b0479SSaeed Bishara prefetch(_iter); 332ff7b0479SSaeed Bishara prefetch(&_iter->async_tx); 333ff7b0479SSaeed Bishara 334ff7b0479SSaeed Bishara /* do not advance past the current descriptor loaded into the 335ff7b0479SSaeed Bishara * hardware channel, subsequent descriptors are either in 336ff7b0479SSaeed Bishara * process or have not been submitted 337ff7b0479SSaeed Bishara */ 338ff7b0479SSaeed Bishara if (seen_current) 339ff7b0479SSaeed Bishara break; 340ff7b0479SSaeed Bishara 341ff7b0479SSaeed Bishara /* stop the search if we reach the current descriptor and the 342ff7b0479SSaeed Bishara * channel is busy 343ff7b0479SSaeed Bishara */ 344ff7b0479SSaeed Bishara if (iter->async_tx.phys == current_desc) { 345ff7b0479SSaeed Bishara seen_current = 1; 346ff7b0479SSaeed Bishara if (busy) 347ff7b0479SSaeed Bishara break; 348ff7b0479SSaeed Bishara } 349ff7b0479SSaeed Bishara 350ff7b0479SSaeed Bishara cookie = mv_xor_run_tx_complete_actions(iter, mv_chan, cookie); 351ff7b0479SSaeed Bishara 352ff7b0479SSaeed Bishara if (mv_xor_clean_slot(iter, mv_chan)) 353ff7b0479SSaeed Bishara break; 354ff7b0479SSaeed Bishara } 355ff7b0479SSaeed Bishara 356ff7b0479SSaeed Bishara if ((busy == 0) && !list_empty(&mv_chan->chain)) { 357ff7b0479SSaeed Bishara struct mv_xor_desc_slot *chain_head; 358ff7b0479SSaeed Bishara chain_head = list_entry(mv_chan->chain.next, 359ff7b0479SSaeed Bishara struct mv_xor_desc_slot, 360ff7b0479SSaeed Bishara chain_node); 361ff7b0479SSaeed Bishara 362ff7b0479SSaeed Bishara mv_xor_start_new_chain(mv_chan, chain_head); 363ff7b0479SSaeed Bishara } 364ff7b0479SSaeed Bishara 365ff7b0479SSaeed Bishara if (cookie > 0) 36698817b99SThomas Petazzoni mv_chan->dmachan.completed_cookie = cookie; 367ff7b0479SSaeed Bishara } 368ff7b0479SSaeed Bishara 369ff7b0479SSaeed Bishara static void 370ff7b0479SSaeed Bishara mv_xor_slot_cleanup(struct mv_xor_chan *mv_chan) 371ff7b0479SSaeed Bishara { 372ff7b0479SSaeed Bishara spin_lock_bh(&mv_chan->lock); 373ff7b0479SSaeed Bishara __mv_xor_slot_cleanup(mv_chan); 374ff7b0479SSaeed Bishara spin_unlock_bh(&mv_chan->lock); 375ff7b0479SSaeed Bishara } 376ff7b0479SSaeed Bishara 377ff7b0479SSaeed Bishara static void mv_xor_tasklet(unsigned long data) 378ff7b0479SSaeed Bishara { 379ff7b0479SSaeed Bishara struct mv_xor_chan *chan = (struct mv_xor_chan *) data; 3808333f65eSSaeed Bishara mv_xor_slot_cleanup(chan); 381ff7b0479SSaeed Bishara } 382ff7b0479SSaeed Bishara 383ff7b0479SSaeed Bishara static struct mv_xor_desc_slot * 384ff7b0479SSaeed Bishara mv_xor_alloc_slots(struct mv_xor_chan *mv_chan, int num_slots, 385ff7b0479SSaeed Bishara int slots_per_op) 386ff7b0479SSaeed Bishara { 387ff7b0479SSaeed Bishara struct mv_xor_desc_slot *iter, *_iter, *alloc_start = NULL; 388ff7b0479SSaeed Bishara LIST_HEAD(chain); 389ff7b0479SSaeed Bishara int slots_found, retry = 0; 390ff7b0479SSaeed Bishara 391ff7b0479SSaeed Bishara /* start search from the last allocated descrtiptor 392ff7b0479SSaeed Bishara * if a contiguous allocation can not be found start searching 393ff7b0479SSaeed Bishara * from the beginning of the list 394ff7b0479SSaeed Bishara */ 395ff7b0479SSaeed Bishara retry: 396ff7b0479SSaeed Bishara slots_found = 0; 397ff7b0479SSaeed Bishara if (retry == 0) 398ff7b0479SSaeed Bishara iter = mv_chan->last_used; 399ff7b0479SSaeed Bishara else 400ff7b0479SSaeed Bishara iter = list_entry(&mv_chan->all_slots, 401ff7b0479SSaeed Bishara struct mv_xor_desc_slot, 402ff7b0479SSaeed Bishara slot_node); 403ff7b0479SSaeed Bishara 404ff7b0479SSaeed Bishara list_for_each_entry_safe_continue( 405ff7b0479SSaeed Bishara iter, _iter, &mv_chan->all_slots, slot_node) { 406ff7b0479SSaeed Bishara prefetch(_iter); 407ff7b0479SSaeed Bishara prefetch(&_iter->async_tx); 408ff7b0479SSaeed Bishara if (iter->slots_per_op) { 409ff7b0479SSaeed Bishara /* give up after finding the first busy slot 410ff7b0479SSaeed Bishara * on the second pass through the list 411ff7b0479SSaeed Bishara */ 412ff7b0479SSaeed Bishara if (retry) 413ff7b0479SSaeed Bishara break; 414ff7b0479SSaeed Bishara 415ff7b0479SSaeed Bishara slots_found = 0; 416ff7b0479SSaeed Bishara continue; 417ff7b0479SSaeed Bishara } 418ff7b0479SSaeed Bishara 419ff7b0479SSaeed Bishara /* start the allocation if the slot is correctly aligned */ 420ff7b0479SSaeed Bishara if (!slots_found++) 421ff7b0479SSaeed Bishara alloc_start = iter; 422ff7b0479SSaeed Bishara 423ff7b0479SSaeed Bishara if (slots_found == num_slots) { 424ff7b0479SSaeed Bishara struct mv_xor_desc_slot *alloc_tail = NULL; 425ff7b0479SSaeed Bishara struct mv_xor_desc_slot *last_used = NULL; 426ff7b0479SSaeed Bishara iter = alloc_start; 427ff7b0479SSaeed Bishara while (num_slots) { 428ff7b0479SSaeed Bishara int i; 429ff7b0479SSaeed Bishara 430ff7b0479SSaeed Bishara /* pre-ack all but the last descriptor */ 431ff7b0479SSaeed Bishara async_tx_ack(&iter->async_tx); 432ff7b0479SSaeed Bishara 433ff7b0479SSaeed Bishara list_add_tail(&iter->chain_node, &chain); 434ff7b0479SSaeed Bishara alloc_tail = iter; 435ff7b0479SSaeed Bishara iter->async_tx.cookie = 0; 436ff7b0479SSaeed Bishara iter->slot_cnt = num_slots; 437ff7b0479SSaeed Bishara iter->xor_check_result = NULL; 438ff7b0479SSaeed Bishara for (i = 0; i < slots_per_op; i++) { 439ff7b0479SSaeed Bishara iter->slots_per_op = slots_per_op - i; 440ff7b0479SSaeed Bishara last_used = iter; 441ff7b0479SSaeed Bishara iter = list_entry(iter->slot_node.next, 442ff7b0479SSaeed Bishara struct mv_xor_desc_slot, 443ff7b0479SSaeed Bishara slot_node); 444ff7b0479SSaeed Bishara } 445ff7b0479SSaeed Bishara num_slots -= slots_per_op; 446ff7b0479SSaeed Bishara } 447ff7b0479SSaeed Bishara alloc_tail->group_head = alloc_start; 448ff7b0479SSaeed Bishara alloc_tail->async_tx.cookie = -EBUSY; 44964203b67SDan Williams list_splice(&chain, &alloc_tail->tx_list); 450ff7b0479SSaeed Bishara mv_chan->last_used = last_used; 451ff7b0479SSaeed Bishara mv_desc_clear_next_desc(alloc_start); 452ff7b0479SSaeed Bishara mv_desc_clear_next_desc(alloc_tail); 453ff7b0479SSaeed Bishara return alloc_tail; 454ff7b0479SSaeed Bishara } 455ff7b0479SSaeed Bishara } 456ff7b0479SSaeed Bishara if (!retry++) 457ff7b0479SSaeed Bishara goto retry; 458ff7b0479SSaeed Bishara 459ff7b0479SSaeed Bishara /* try to free some slots if the allocation fails */ 460ff7b0479SSaeed Bishara tasklet_schedule(&mv_chan->irq_tasklet); 461ff7b0479SSaeed Bishara 462ff7b0479SSaeed Bishara return NULL; 463ff7b0479SSaeed Bishara } 464ff7b0479SSaeed Bishara 465ff7b0479SSaeed Bishara /************************ DMA engine API functions ****************************/ 466ff7b0479SSaeed Bishara static dma_cookie_t 467ff7b0479SSaeed Bishara mv_xor_tx_submit(struct dma_async_tx_descriptor *tx) 468ff7b0479SSaeed Bishara { 469ff7b0479SSaeed Bishara struct mv_xor_desc_slot *sw_desc = to_mv_xor_slot(tx); 470ff7b0479SSaeed Bishara struct mv_xor_chan *mv_chan = to_mv_xor_chan(tx->chan); 471ff7b0479SSaeed Bishara struct mv_xor_desc_slot *grp_start, *old_chain_tail; 472ff7b0479SSaeed Bishara dma_cookie_t cookie; 473ff7b0479SSaeed Bishara int new_hw_chain = 1; 474ff7b0479SSaeed Bishara 475c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), 476ff7b0479SSaeed Bishara "%s sw_desc %p: async_tx %p\n", 477ff7b0479SSaeed Bishara __func__, sw_desc, &sw_desc->async_tx); 478ff7b0479SSaeed Bishara 479ff7b0479SSaeed Bishara grp_start = sw_desc->group_head; 480ff7b0479SSaeed Bishara 481ff7b0479SSaeed Bishara spin_lock_bh(&mv_chan->lock); 482884485e1SRussell King - ARM Linux cookie = dma_cookie_assign(tx); 483ff7b0479SSaeed Bishara 484ff7b0479SSaeed Bishara if (list_empty(&mv_chan->chain)) 48564203b67SDan Williams list_splice_init(&sw_desc->tx_list, &mv_chan->chain); 486ff7b0479SSaeed Bishara else { 487ff7b0479SSaeed Bishara new_hw_chain = 0; 488ff7b0479SSaeed Bishara 489ff7b0479SSaeed Bishara old_chain_tail = list_entry(mv_chan->chain.prev, 490ff7b0479SSaeed Bishara struct mv_xor_desc_slot, 491ff7b0479SSaeed Bishara chain_node); 49264203b67SDan Williams list_splice_init(&grp_start->tx_list, 493ff7b0479SSaeed Bishara &old_chain_tail->chain_node); 494ff7b0479SSaeed Bishara 495ff7b0479SSaeed Bishara if (!mv_can_chain(grp_start)) 496ff7b0479SSaeed Bishara goto submit_done; 497ff7b0479SSaeed Bishara 49831fd8f5bSOlof Johansson dev_dbg(mv_chan_to_devp(mv_chan), "Append to last desc %pa\n", 49931fd8f5bSOlof Johansson &old_chain_tail->async_tx.phys); 500ff7b0479SSaeed Bishara 501ff7b0479SSaeed Bishara /* fix up the hardware chain */ 502ff7b0479SSaeed Bishara mv_desc_set_next_desc(old_chain_tail, grp_start->async_tx.phys); 503ff7b0479SSaeed Bishara 504ff7b0479SSaeed Bishara /* if the channel is not busy */ 505ff7b0479SSaeed Bishara if (!mv_chan_is_busy(mv_chan)) { 506ff7b0479SSaeed Bishara u32 current_desc = mv_chan_get_current_desc(mv_chan); 507ff7b0479SSaeed Bishara /* 508ff7b0479SSaeed Bishara * and the curren desc is the end of the chain before 509ff7b0479SSaeed Bishara * the append, then we need to start the channel 510ff7b0479SSaeed Bishara */ 511ff7b0479SSaeed Bishara if (current_desc == old_chain_tail->async_tx.phys) 512ff7b0479SSaeed Bishara new_hw_chain = 1; 513ff7b0479SSaeed Bishara } 514ff7b0479SSaeed Bishara } 515ff7b0479SSaeed Bishara 516ff7b0479SSaeed Bishara if (new_hw_chain) 517ff7b0479SSaeed Bishara mv_xor_start_new_chain(mv_chan, grp_start); 518ff7b0479SSaeed Bishara 519ff7b0479SSaeed Bishara submit_done: 520ff7b0479SSaeed Bishara spin_unlock_bh(&mv_chan->lock); 521ff7b0479SSaeed Bishara 522ff7b0479SSaeed Bishara return cookie; 523ff7b0479SSaeed Bishara } 524ff7b0479SSaeed Bishara 525ff7b0479SSaeed Bishara /* returns the number of allocated descriptors */ 526aa1e6f1aSDan Williams static int mv_xor_alloc_chan_resources(struct dma_chan *chan) 527ff7b0479SSaeed Bishara { 52831fd8f5bSOlof Johansson void *virt_desc; 52931fd8f5bSOlof Johansson dma_addr_t dma_desc; 530ff7b0479SSaeed Bishara int idx; 531ff7b0479SSaeed Bishara struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan); 532ff7b0479SSaeed Bishara struct mv_xor_desc_slot *slot = NULL; 533b503fa01SThomas Petazzoni int num_descs_in_pool = MV_XOR_POOL_SIZE/MV_XOR_SLOT_SIZE; 534ff7b0479SSaeed Bishara 535ff7b0479SSaeed Bishara /* Allocate descriptor slots */ 536ff7b0479SSaeed Bishara idx = mv_chan->slots_allocated; 537ff7b0479SSaeed Bishara while (idx < num_descs_in_pool) { 538ff7b0479SSaeed Bishara slot = kzalloc(sizeof(*slot), GFP_KERNEL); 539ff7b0479SSaeed Bishara if (!slot) { 540*b8291ddeSEzequiel Garcia dev_info(mv_chan_to_devp(mv_chan), 541*b8291ddeSEzequiel Garcia "channel only initialized %d descriptor slots", 542*b8291ddeSEzequiel Garcia idx); 543ff7b0479SSaeed Bishara break; 544ff7b0479SSaeed Bishara } 54531fd8f5bSOlof Johansson virt_desc = mv_chan->dma_desc_pool_virt; 54631fd8f5bSOlof Johansson slot->hw_desc = virt_desc + idx * MV_XOR_SLOT_SIZE; 547ff7b0479SSaeed Bishara 548ff7b0479SSaeed Bishara dma_async_tx_descriptor_init(&slot->async_tx, chan); 549ff7b0479SSaeed Bishara slot->async_tx.tx_submit = mv_xor_tx_submit; 550ff7b0479SSaeed Bishara INIT_LIST_HEAD(&slot->chain_node); 551ff7b0479SSaeed Bishara INIT_LIST_HEAD(&slot->slot_node); 55264203b67SDan Williams INIT_LIST_HEAD(&slot->tx_list); 55331fd8f5bSOlof Johansson dma_desc = mv_chan->dma_desc_pool; 55431fd8f5bSOlof Johansson slot->async_tx.phys = dma_desc + idx * MV_XOR_SLOT_SIZE; 555ff7b0479SSaeed Bishara slot->idx = idx++; 556ff7b0479SSaeed Bishara 557ff7b0479SSaeed Bishara spin_lock_bh(&mv_chan->lock); 558ff7b0479SSaeed Bishara mv_chan->slots_allocated = idx; 559ff7b0479SSaeed Bishara list_add_tail(&slot->slot_node, &mv_chan->all_slots); 560ff7b0479SSaeed Bishara spin_unlock_bh(&mv_chan->lock); 561ff7b0479SSaeed Bishara } 562ff7b0479SSaeed Bishara 563ff7b0479SSaeed Bishara if (mv_chan->slots_allocated && !mv_chan->last_used) 564ff7b0479SSaeed Bishara mv_chan->last_used = list_entry(mv_chan->all_slots.next, 565ff7b0479SSaeed Bishara struct mv_xor_desc_slot, 566ff7b0479SSaeed Bishara slot_node); 567ff7b0479SSaeed Bishara 568c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), 569ff7b0479SSaeed Bishara "allocated %d descriptor slots last_used: %p\n", 570ff7b0479SSaeed Bishara mv_chan->slots_allocated, mv_chan->last_used); 571ff7b0479SSaeed Bishara 572ff7b0479SSaeed Bishara return mv_chan->slots_allocated ? : -ENOMEM; 573ff7b0479SSaeed Bishara } 574ff7b0479SSaeed Bishara 575ff7b0479SSaeed Bishara static struct dma_async_tx_descriptor * 576ff7b0479SSaeed Bishara mv_xor_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src, 577ff7b0479SSaeed Bishara size_t len, unsigned long flags) 578ff7b0479SSaeed Bishara { 579ff7b0479SSaeed Bishara struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan); 580ff7b0479SSaeed Bishara struct mv_xor_desc_slot *sw_desc, *grp_start; 581ff7b0479SSaeed Bishara int slot_cnt; 582ff7b0479SSaeed Bishara 583c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), 58431fd8f5bSOlof Johansson "%s dest: %pad src %pad len: %u flags: %ld\n", 58531fd8f5bSOlof Johansson __func__, &dest, &src, len, flags); 586ff7b0479SSaeed Bishara if (unlikely(len < MV_XOR_MIN_BYTE_COUNT)) 587ff7b0479SSaeed Bishara return NULL; 588ff7b0479SSaeed Bishara 5897912d300SColy Li BUG_ON(len > MV_XOR_MAX_BYTE_COUNT); 590ff7b0479SSaeed Bishara 591ff7b0479SSaeed Bishara spin_lock_bh(&mv_chan->lock); 592ff7b0479SSaeed Bishara slot_cnt = mv_chan_memcpy_slot_count(len); 593ff7b0479SSaeed Bishara sw_desc = mv_xor_alloc_slots(mv_chan, slot_cnt, 1); 594ff7b0479SSaeed Bishara if (sw_desc) { 595ff7b0479SSaeed Bishara sw_desc->type = DMA_MEMCPY; 596ff7b0479SSaeed Bishara sw_desc->async_tx.flags = flags; 597ff7b0479SSaeed Bishara grp_start = sw_desc->group_head; 598ff7b0479SSaeed Bishara mv_desc_init(grp_start, flags); 599ff7b0479SSaeed Bishara mv_desc_set_byte_count(grp_start, len); 600ff7b0479SSaeed Bishara mv_desc_set_dest_addr(sw_desc->group_head, dest); 601ff7b0479SSaeed Bishara mv_desc_set_src_addr(grp_start, 0, src); 602ff7b0479SSaeed Bishara sw_desc->unmap_src_cnt = 1; 603ff7b0479SSaeed Bishara sw_desc->unmap_len = len; 604ff7b0479SSaeed Bishara } 605ff7b0479SSaeed Bishara spin_unlock_bh(&mv_chan->lock); 606ff7b0479SSaeed Bishara 607c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), 608ff7b0479SSaeed Bishara "%s sw_desc %p async_tx %p\n", 6094c143725SJingoo Han __func__, sw_desc, sw_desc ? &sw_desc->async_tx : NULL); 610ff7b0479SSaeed Bishara 611ff7b0479SSaeed Bishara return sw_desc ? &sw_desc->async_tx : NULL; 612ff7b0479SSaeed Bishara } 613ff7b0479SSaeed Bishara 614ff7b0479SSaeed Bishara static struct dma_async_tx_descriptor * 615ff7b0479SSaeed Bishara mv_xor_prep_dma_xor(struct dma_chan *chan, dma_addr_t dest, dma_addr_t *src, 616ff7b0479SSaeed Bishara unsigned int src_cnt, size_t len, unsigned long flags) 617ff7b0479SSaeed Bishara { 618ff7b0479SSaeed Bishara struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan); 619ff7b0479SSaeed Bishara struct mv_xor_desc_slot *sw_desc, *grp_start; 620ff7b0479SSaeed Bishara int slot_cnt; 621ff7b0479SSaeed Bishara 622ff7b0479SSaeed Bishara if (unlikely(len < MV_XOR_MIN_BYTE_COUNT)) 623ff7b0479SSaeed Bishara return NULL; 624ff7b0479SSaeed Bishara 6257912d300SColy Li BUG_ON(len > MV_XOR_MAX_BYTE_COUNT); 626ff7b0479SSaeed Bishara 627c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), 62831fd8f5bSOlof Johansson "%s src_cnt: %d len: %u dest %pad flags: %ld\n", 62931fd8f5bSOlof Johansson __func__, src_cnt, len, &dest, flags); 630ff7b0479SSaeed Bishara 631ff7b0479SSaeed Bishara spin_lock_bh(&mv_chan->lock); 632ff7b0479SSaeed Bishara slot_cnt = mv_chan_xor_slot_count(len, src_cnt); 633ff7b0479SSaeed Bishara sw_desc = mv_xor_alloc_slots(mv_chan, slot_cnt, 1); 634ff7b0479SSaeed Bishara if (sw_desc) { 635ff7b0479SSaeed Bishara sw_desc->type = DMA_XOR; 636ff7b0479SSaeed Bishara sw_desc->async_tx.flags = flags; 637ff7b0479SSaeed Bishara grp_start = sw_desc->group_head; 638ff7b0479SSaeed Bishara mv_desc_init(grp_start, flags); 639ff7b0479SSaeed Bishara /* the byte count field is the same as in memcpy desc*/ 640ff7b0479SSaeed Bishara mv_desc_set_byte_count(grp_start, len); 641ff7b0479SSaeed Bishara mv_desc_set_dest_addr(sw_desc->group_head, dest); 642ff7b0479SSaeed Bishara sw_desc->unmap_src_cnt = src_cnt; 643ff7b0479SSaeed Bishara sw_desc->unmap_len = len; 644ff7b0479SSaeed Bishara while (src_cnt--) 645ff7b0479SSaeed Bishara mv_desc_set_src_addr(grp_start, src_cnt, src[src_cnt]); 646ff7b0479SSaeed Bishara } 647ff7b0479SSaeed Bishara spin_unlock_bh(&mv_chan->lock); 648c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), 649ff7b0479SSaeed Bishara "%s sw_desc %p async_tx %p \n", 650ff7b0479SSaeed Bishara __func__, sw_desc, &sw_desc->async_tx); 651ff7b0479SSaeed Bishara return sw_desc ? &sw_desc->async_tx : NULL; 652ff7b0479SSaeed Bishara } 653ff7b0479SSaeed Bishara 654ff7b0479SSaeed Bishara static void mv_xor_free_chan_resources(struct dma_chan *chan) 655ff7b0479SSaeed Bishara { 656ff7b0479SSaeed Bishara struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan); 657ff7b0479SSaeed Bishara struct mv_xor_desc_slot *iter, *_iter; 658ff7b0479SSaeed Bishara int in_use_descs = 0; 659ff7b0479SSaeed Bishara 660ff7b0479SSaeed Bishara mv_xor_slot_cleanup(mv_chan); 661ff7b0479SSaeed Bishara 662ff7b0479SSaeed Bishara spin_lock_bh(&mv_chan->lock); 663ff7b0479SSaeed Bishara list_for_each_entry_safe(iter, _iter, &mv_chan->chain, 664ff7b0479SSaeed Bishara chain_node) { 665ff7b0479SSaeed Bishara in_use_descs++; 666ff7b0479SSaeed Bishara list_del(&iter->chain_node); 667ff7b0479SSaeed Bishara } 668ff7b0479SSaeed Bishara list_for_each_entry_safe(iter, _iter, &mv_chan->completed_slots, 669ff7b0479SSaeed Bishara completed_node) { 670ff7b0479SSaeed Bishara in_use_descs++; 671ff7b0479SSaeed Bishara list_del(&iter->completed_node); 672ff7b0479SSaeed Bishara } 673ff7b0479SSaeed Bishara list_for_each_entry_safe_reverse( 674ff7b0479SSaeed Bishara iter, _iter, &mv_chan->all_slots, slot_node) { 675ff7b0479SSaeed Bishara list_del(&iter->slot_node); 676ff7b0479SSaeed Bishara kfree(iter); 677ff7b0479SSaeed Bishara mv_chan->slots_allocated--; 678ff7b0479SSaeed Bishara } 679ff7b0479SSaeed Bishara mv_chan->last_used = NULL; 680ff7b0479SSaeed Bishara 681c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), "%s slots_allocated %d\n", 682ff7b0479SSaeed Bishara __func__, mv_chan->slots_allocated); 683ff7b0479SSaeed Bishara spin_unlock_bh(&mv_chan->lock); 684ff7b0479SSaeed Bishara 685ff7b0479SSaeed Bishara if (in_use_descs) 686c98c1781SThomas Petazzoni dev_err(mv_chan_to_devp(mv_chan), 687ff7b0479SSaeed Bishara "freeing %d in use descriptors!\n", in_use_descs); 688ff7b0479SSaeed Bishara } 689ff7b0479SSaeed Bishara 690ff7b0479SSaeed Bishara /** 69107934481SLinus Walleij * mv_xor_status - poll the status of an XOR transaction 692ff7b0479SSaeed Bishara * @chan: XOR channel handle 693ff7b0479SSaeed Bishara * @cookie: XOR transaction identifier 69407934481SLinus Walleij * @txstate: XOR transactions state holder (or NULL) 695ff7b0479SSaeed Bishara */ 69607934481SLinus Walleij static enum dma_status mv_xor_status(struct dma_chan *chan, 697ff7b0479SSaeed Bishara dma_cookie_t cookie, 69807934481SLinus Walleij struct dma_tx_state *txstate) 699ff7b0479SSaeed Bishara { 700ff7b0479SSaeed Bishara struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan); 701ff7b0479SSaeed Bishara enum dma_status ret; 702ff7b0479SSaeed Bishara 70396a2af41SRussell King - ARM Linux ret = dma_cookie_status(chan, cookie, txstate); 704b3efb8fcSVinod Koul if (ret == DMA_COMPLETE) { 705ff7b0479SSaeed Bishara mv_xor_clean_completed_slots(mv_chan); 706ff7b0479SSaeed Bishara return ret; 707ff7b0479SSaeed Bishara } 708ff7b0479SSaeed Bishara mv_xor_slot_cleanup(mv_chan); 709ff7b0479SSaeed Bishara 71096a2af41SRussell King - ARM Linux return dma_cookie_status(chan, cookie, txstate); 711ff7b0479SSaeed Bishara } 712ff7b0479SSaeed Bishara 713ff7b0479SSaeed Bishara static void mv_dump_xor_regs(struct mv_xor_chan *chan) 714ff7b0479SSaeed Bishara { 715ff7b0479SSaeed Bishara u32 val; 716ff7b0479SSaeed Bishara 7175733c38aSThomas Petazzoni val = readl_relaxed(XOR_CONFIG(chan)); 7181ba151cdSJoe Perches dev_err(mv_chan_to_devp(chan), "config 0x%08x\n", val); 719ff7b0479SSaeed Bishara 7205733c38aSThomas Petazzoni val = readl_relaxed(XOR_ACTIVATION(chan)); 7211ba151cdSJoe Perches dev_err(mv_chan_to_devp(chan), "activation 0x%08x\n", val); 722ff7b0479SSaeed Bishara 7235733c38aSThomas Petazzoni val = readl_relaxed(XOR_INTR_CAUSE(chan)); 7241ba151cdSJoe Perches dev_err(mv_chan_to_devp(chan), "intr cause 0x%08x\n", val); 725ff7b0479SSaeed Bishara 7265733c38aSThomas Petazzoni val = readl_relaxed(XOR_INTR_MASK(chan)); 7271ba151cdSJoe Perches dev_err(mv_chan_to_devp(chan), "intr mask 0x%08x\n", val); 728ff7b0479SSaeed Bishara 7295733c38aSThomas Petazzoni val = readl_relaxed(XOR_ERROR_CAUSE(chan)); 7301ba151cdSJoe Perches dev_err(mv_chan_to_devp(chan), "error cause 0x%08x\n", val); 731ff7b0479SSaeed Bishara 7325733c38aSThomas Petazzoni val = readl_relaxed(XOR_ERROR_ADDR(chan)); 7331ba151cdSJoe Perches dev_err(mv_chan_to_devp(chan), "error addr 0x%08x\n", val); 734ff7b0479SSaeed Bishara } 735ff7b0479SSaeed Bishara 736ff7b0479SSaeed Bishara static void mv_xor_err_interrupt_handler(struct mv_xor_chan *chan, 737ff7b0479SSaeed Bishara u32 intr_cause) 738ff7b0479SSaeed Bishara { 739ff7b0479SSaeed Bishara if (intr_cause & (1 << 4)) { 740c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(chan), 741ff7b0479SSaeed Bishara "ignore this error\n"); 742ff7b0479SSaeed Bishara return; 743ff7b0479SSaeed Bishara } 744ff7b0479SSaeed Bishara 745c98c1781SThomas Petazzoni dev_err(mv_chan_to_devp(chan), 7461ba151cdSJoe Perches "error on chan %d. intr cause 0x%08x\n", 747ff7b0479SSaeed Bishara chan->idx, intr_cause); 748ff7b0479SSaeed Bishara 749ff7b0479SSaeed Bishara mv_dump_xor_regs(chan); 750ff7b0479SSaeed Bishara BUG(); 751ff7b0479SSaeed Bishara } 752ff7b0479SSaeed Bishara 753ff7b0479SSaeed Bishara static irqreturn_t mv_xor_interrupt_handler(int irq, void *data) 754ff7b0479SSaeed Bishara { 755ff7b0479SSaeed Bishara struct mv_xor_chan *chan = data; 756ff7b0479SSaeed Bishara u32 intr_cause = mv_chan_get_intr_cause(chan); 757ff7b0479SSaeed Bishara 758c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(chan), "intr cause %x\n", intr_cause); 759ff7b0479SSaeed Bishara 760ff7b0479SSaeed Bishara if (mv_is_err_intr(intr_cause)) 761ff7b0479SSaeed Bishara mv_xor_err_interrupt_handler(chan, intr_cause); 762ff7b0479SSaeed Bishara 763ff7b0479SSaeed Bishara tasklet_schedule(&chan->irq_tasklet); 764ff7b0479SSaeed Bishara 765ff7b0479SSaeed Bishara mv_xor_device_clear_eoc_cause(chan); 766ff7b0479SSaeed Bishara 767ff7b0479SSaeed Bishara return IRQ_HANDLED; 768ff7b0479SSaeed Bishara } 769ff7b0479SSaeed Bishara 770ff7b0479SSaeed Bishara static void mv_xor_issue_pending(struct dma_chan *chan) 771ff7b0479SSaeed Bishara { 772ff7b0479SSaeed Bishara struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan); 773ff7b0479SSaeed Bishara 774ff7b0479SSaeed Bishara if (mv_chan->pending >= MV_XOR_THRESHOLD) { 775ff7b0479SSaeed Bishara mv_chan->pending = 0; 776ff7b0479SSaeed Bishara mv_chan_activate(mv_chan); 777ff7b0479SSaeed Bishara } 778ff7b0479SSaeed Bishara } 779ff7b0479SSaeed Bishara 780ff7b0479SSaeed Bishara /* 781ff7b0479SSaeed Bishara * Perform a transaction to verify the HW works. 782ff7b0479SSaeed Bishara */ 783ff7b0479SSaeed Bishara 784c2714334SLinus Torvalds static int mv_xor_memcpy_self_test(struct mv_xor_chan *mv_chan) 785ff7b0479SSaeed Bishara { 786ff7b0479SSaeed Bishara int i; 787ff7b0479SSaeed Bishara void *src, *dest; 788ff7b0479SSaeed Bishara dma_addr_t src_dma, dest_dma; 789ff7b0479SSaeed Bishara struct dma_chan *dma_chan; 790ff7b0479SSaeed Bishara dma_cookie_t cookie; 791ff7b0479SSaeed Bishara struct dma_async_tx_descriptor *tx; 792d16695a7SEzequiel Garcia struct dmaengine_unmap_data *unmap; 793ff7b0479SSaeed Bishara int err = 0; 794ff7b0479SSaeed Bishara 795d16695a7SEzequiel Garcia src = kmalloc(sizeof(u8) * PAGE_SIZE, GFP_KERNEL); 796ff7b0479SSaeed Bishara if (!src) 797ff7b0479SSaeed Bishara return -ENOMEM; 798ff7b0479SSaeed Bishara 799d16695a7SEzequiel Garcia dest = kzalloc(sizeof(u8) * PAGE_SIZE, GFP_KERNEL); 800ff7b0479SSaeed Bishara if (!dest) { 801ff7b0479SSaeed Bishara kfree(src); 802ff7b0479SSaeed Bishara return -ENOMEM; 803ff7b0479SSaeed Bishara } 804ff7b0479SSaeed Bishara 805ff7b0479SSaeed Bishara /* Fill in src buffer */ 806d16695a7SEzequiel Garcia for (i = 0; i < PAGE_SIZE; i++) 807ff7b0479SSaeed Bishara ((u8 *) src)[i] = (u8)i; 808ff7b0479SSaeed Bishara 809275cc0c8SThomas Petazzoni dma_chan = &mv_chan->dmachan; 810aa1e6f1aSDan Williams if (mv_xor_alloc_chan_resources(dma_chan) < 1) { 811ff7b0479SSaeed Bishara err = -ENODEV; 812ff7b0479SSaeed Bishara goto out; 813ff7b0479SSaeed Bishara } 814ff7b0479SSaeed Bishara 815d16695a7SEzequiel Garcia unmap = dmaengine_get_unmap_data(dma_chan->device->dev, 2, GFP_KERNEL); 816d16695a7SEzequiel Garcia if (!unmap) { 817d16695a7SEzequiel Garcia err = -ENOMEM; 818d16695a7SEzequiel Garcia goto free_resources; 819d16695a7SEzequiel Garcia } 820ff7b0479SSaeed Bishara 821d16695a7SEzequiel Garcia src_dma = dma_map_page(dma_chan->device->dev, virt_to_page(src), 0, 822d16695a7SEzequiel Garcia PAGE_SIZE, DMA_TO_DEVICE); 823d16695a7SEzequiel Garcia unmap->to_cnt = 1; 824d16695a7SEzequiel Garcia unmap->addr[0] = src_dma; 825d16695a7SEzequiel Garcia 826d16695a7SEzequiel Garcia dest_dma = dma_map_page(dma_chan->device->dev, virt_to_page(dest), 0, 827d16695a7SEzequiel Garcia PAGE_SIZE, DMA_FROM_DEVICE); 828d16695a7SEzequiel Garcia unmap->from_cnt = 1; 829d16695a7SEzequiel Garcia unmap->addr[1] = dest_dma; 830d16695a7SEzequiel Garcia 831d16695a7SEzequiel Garcia unmap->len = PAGE_SIZE; 832ff7b0479SSaeed Bishara 833ff7b0479SSaeed Bishara tx = mv_xor_prep_dma_memcpy(dma_chan, dest_dma, src_dma, 834d16695a7SEzequiel Garcia PAGE_SIZE, 0); 835ff7b0479SSaeed Bishara cookie = mv_xor_tx_submit(tx); 836ff7b0479SSaeed Bishara mv_xor_issue_pending(dma_chan); 837ff7b0479SSaeed Bishara async_tx_ack(tx); 838ff7b0479SSaeed Bishara msleep(1); 839ff7b0479SSaeed Bishara 84007934481SLinus Walleij if (mv_xor_status(dma_chan, cookie, NULL) != 841b3efb8fcSVinod Koul DMA_COMPLETE) { 842a3fc74bcSThomas Petazzoni dev_err(dma_chan->device->dev, 843ff7b0479SSaeed Bishara "Self-test copy timed out, disabling\n"); 844ff7b0479SSaeed Bishara err = -ENODEV; 845ff7b0479SSaeed Bishara goto free_resources; 846ff7b0479SSaeed Bishara } 847ff7b0479SSaeed Bishara 848c35064c4SThomas Petazzoni dma_sync_single_for_cpu(dma_chan->device->dev, dest_dma, 849d16695a7SEzequiel Garcia PAGE_SIZE, DMA_FROM_DEVICE); 850d16695a7SEzequiel Garcia if (memcmp(src, dest, PAGE_SIZE)) { 851a3fc74bcSThomas Petazzoni dev_err(dma_chan->device->dev, 852ff7b0479SSaeed Bishara "Self-test copy failed compare, disabling\n"); 853ff7b0479SSaeed Bishara err = -ENODEV; 854ff7b0479SSaeed Bishara goto free_resources; 855ff7b0479SSaeed Bishara } 856ff7b0479SSaeed Bishara 857ff7b0479SSaeed Bishara free_resources: 858d16695a7SEzequiel Garcia dmaengine_unmap_put(unmap); 859ff7b0479SSaeed Bishara mv_xor_free_chan_resources(dma_chan); 860ff7b0479SSaeed Bishara out: 861ff7b0479SSaeed Bishara kfree(src); 862ff7b0479SSaeed Bishara kfree(dest); 863ff7b0479SSaeed Bishara return err; 864ff7b0479SSaeed Bishara } 865ff7b0479SSaeed Bishara 866ff7b0479SSaeed Bishara #define MV_XOR_NUM_SRC_TEST 4 /* must be <= 15 */ 867463a1f8bSBill Pemberton static int 868275cc0c8SThomas Petazzoni mv_xor_xor_self_test(struct mv_xor_chan *mv_chan) 869ff7b0479SSaeed Bishara { 870ff7b0479SSaeed Bishara int i, src_idx; 871ff7b0479SSaeed Bishara struct page *dest; 872ff7b0479SSaeed Bishara struct page *xor_srcs[MV_XOR_NUM_SRC_TEST]; 873ff7b0479SSaeed Bishara dma_addr_t dma_srcs[MV_XOR_NUM_SRC_TEST]; 874ff7b0479SSaeed Bishara dma_addr_t dest_dma; 875ff7b0479SSaeed Bishara struct dma_async_tx_descriptor *tx; 876d16695a7SEzequiel Garcia struct dmaengine_unmap_data *unmap; 877ff7b0479SSaeed Bishara struct dma_chan *dma_chan; 878ff7b0479SSaeed Bishara dma_cookie_t cookie; 879ff7b0479SSaeed Bishara u8 cmp_byte = 0; 880ff7b0479SSaeed Bishara u32 cmp_word; 881ff7b0479SSaeed Bishara int err = 0; 882d16695a7SEzequiel Garcia int src_count = MV_XOR_NUM_SRC_TEST; 883ff7b0479SSaeed Bishara 884d16695a7SEzequiel Garcia for (src_idx = 0; src_idx < src_count; src_idx++) { 885ff7b0479SSaeed Bishara xor_srcs[src_idx] = alloc_page(GFP_KERNEL); 886a09b09aeSRoel Kluin if (!xor_srcs[src_idx]) { 887a09b09aeSRoel Kluin while (src_idx--) 888ff7b0479SSaeed Bishara __free_page(xor_srcs[src_idx]); 889ff7b0479SSaeed Bishara return -ENOMEM; 890ff7b0479SSaeed Bishara } 891ff7b0479SSaeed Bishara } 892ff7b0479SSaeed Bishara 893ff7b0479SSaeed Bishara dest = alloc_page(GFP_KERNEL); 894a09b09aeSRoel Kluin if (!dest) { 895a09b09aeSRoel Kluin while (src_idx--) 896ff7b0479SSaeed Bishara __free_page(xor_srcs[src_idx]); 897ff7b0479SSaeed Bishara return -ENOMEM; 898ff7b0479SSaeed Bishara } 899ff7b0479SSaeed Bishara 900ff7b0479SSaeed Bishara /* Fill in src buffers */ 901d16695a7SEzequiel Garcia for (src_idx = 0; src_idx < src_count; src_idx++) { 902ff7b0479SSaeed Bishara u8 *ptr = page_address(xor_srcs[src_idx]); 903ff7b0479SSaeed Bishara for (i = 0; i < PAGE_SIZE; i++) 904ff7b0479SSaeed Bishara ptr[i] = (1 << src_idx); 905ff7b0479SSaeed Bishara } 906ff7b0479SSaeed Bishara 907d16695a7SEzequiel Garcia for (src_idx = 0; src_idx < src_count; src_idx++) 908ff7b0479SSaeed Bishara cmp_byte ^= (u8) (1 << src_idx); 909ff7b0479SSaeed Bishara 910ff7b0479SSaeed Bishara cmp_word = (cmp_byte << 24) | (cmp_byte << 16) | 911ff7b0479SSaeed Bishara (cmp_byte << 8) | cmp_byte; 912ff7b0479SSaeed Bishara 913ff7b0479SSaeed Bishara memset(page_address(dest), 0, PAGE_SIZE); 914ff7b0479SSaeed Bishara 915275cc0c8SThomas Petazzoni dma_chan = &mv_chan->dmachan; 916aa1e6f1aSDan Williams if (mv_xor_alloc_chan_resources(dma_chan) < 1) { 917ff7b0479SSaeed Bishara err = -ENODEV; 918ff7b0479SSaeed Bishara goto out; 919ff7b0479SSaeed Bishara } 920ff7b0479SSaeed Bishara 921d16695a7SEzequiel Garcia unmap = dmaengine_get_unmap_data(dma_chan->device->dev, src_count + 1, 922d16695a7SEzequiel Garcia GFP_KERNEL); 923d16695a7SEzequiel Garcia if (!unmap) { 924d16695a7SEzequiel Garcia err = -ENOMEM; 925d16695a7SEzequiel Garcia goto free_resources; 926d16695a7SEzequiel Garcia } 927ff7b0479SSaeed Bishara 928d16695a7SEzequiel Garcia /* test xor */ 929d16695a7SEzequiel Garcia for (i = 0; i < src_count; i++) { 930d16695a7SEzequiel Garcia unmap->addr[i] = dma_map_page(dma_chan->device->dev, xor_srcs[i], 931ff7b0479SSaeed Bishara 0, PAGE_SIZE, DMA_TO_DEVICE); 932d16695a7SEzequiel Garcia dma_srcs[i] = unmap->addr[i]; 933d16695a7SEzequiel Garcia unmap->to_cnt++; 934d16695a7SEzequiel Garcia } 935d16695a7SEzequiel Garcia 936d16695a7SEzequiel Garcia unmap->addr[src_count] = dma_map_page(dma_chan->device->dev, dest, 0, PAGE_SIZE, 937d16695a7SEzequiel Garcia DMA_FROM_DEVICE); 938d16695a7SEzequiel Garcia dest_dma = unmap->addr[src_count]; 939d16695a7SEzequiel Garcia unmap->from_cnt = 1; 940d16695a7SEzequiel Garcia unmap->len = PAGE_SIZE; 941ff7b0479SSaeed Bishara 942ff7b0479SSaeed Bishara tx = mv_xor_prep_dma_xor(dma_chan, dest_dma, dma_srcs, 943d16695a7SEzequiel Garcia src_count, PAGE_SIZE, 0); 944ff7b0479SSaeed Bishara 945ff7b0479SSaeed Bishara cookie = mv_xor_tx_submit(tx); 946ff7b0479SSaeed Bishara mv_xor_issue_pending(dma_chan); 947ff7b0479SSaeed Bishara async_tx_ack(tx); 948ff7b0479SSaeed Bishara msleep(8); 949ff7b0479SSaeed Bishara 95007934481SLinus Walleij if (mv_xor_status(dma_chan, cookie, NULL) != 951b3efb8fcSVinod Koul DMA_COMPLETE) { 952a3fc74bcSThomas Petazzoni dev_err(dma_chan->device->dev, 953ff7b0479SSaeed Bishara "Self-test xor timed out, disabling\n"); 954ff7b0479SSaeed Bishara err = -ENODEV; 955ff7b0479SSaeed Bishara goto free_resources; 956ff7b0479SSaeed Bishara } 957ff7b0479SSaeed Bishara 958c35064c4SThomas Petazzoni dma_sync_single_for_cpu(dma_chan->device->dev, dest_dma, 959ff7b0479SSaeed Bishara PAGE_SIZE, DMA_FROM_DEVICE); 960ff7b0479SSaeed Bishara for (i = 0; i < (PAGE_SIZE / sizeof(u32)); i++) { 961ff7b0479SSaeed Bishara u32 *ptr = page_address(dest); 962ff7b0479SSaeed Bishara if (ptr[i] != cmp_word) { 963a3fc74bcSThomas Petazzoni dev_err(dma_chan->device->dev, 9641ba151cdSJoe Perches "Self-test xor failed compare, disabling. index %d, data %x, expected %x\n", 9651ba151cdSJoe Perches i, ptr[i], cmp_word); 966ff7b0479SSaeed Bishara err = -ENODEV; 967ff7b0479SSaeed Bishara goto free_resources; 968ff7b0479SSaeed Bishara } 969ff7b0479SSaeed Bishara } 970ff7b0479SSaeed Bishara 971ff7b0479SSaeed Bishara free_resources: 972d16695a7SEzequiel Garcia dmaengine_unmap_put(unmap); 973ff7b0479SSaeed Bishara mv_xor_free_chan_resources(dma_chan); 974ff7b0479SSaeed Bishara out: 975d16695a7SEzequiel Garcia src_idx = src_count; 976ff7b0479SSaeed Bishara while (src_idx--) 977ff7b0479SSaeed Bishara __free_page(xor_srcs[src_idx]); 978ff7b0479SSaeed Bishara __free_page(dest); 979ff7b0479SSaeed Bishara return err; 980ff7b0479SSaeed Bishara } 981ff7b0479SSaeed Bishara 98234c93c86SAndrew Lunn /* This driver does not implement any of the optional DMA operations. */ 98334c93c86SAndrew Lunn static int 98434c93c86SAndrew Lunn mv_xor_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd, 98534c93c86SAndrew Lunn unsigned long arg) 986ff7b0479SSaeed Bishara { 98734c93c86SAndrew Lunn return -ENOSYS; 98834c93c86SAndrew Lunn } 98934c93c86SAndrew Lunn 9901ef48a26SThomas Petazzoni static int mv_xor_channel_remove(struct mv_xor_chan *mv_chan) 991ff7b0479SSaeed Bishara { 992ff7b0479SSaeed Bishara struct dma_chan *chan, *_chan; 9931ef48a26SThomas Petazzoni struct device *dev = mv_chan->dmadev.dev; 994ff7b0479SSaeed Bishara 9951ef48a26SThomas Petazzoni dma_async_device_unregister(&mv_chan->dmadev); 996ff7b0479SSaeed Bishara 997b503fa01SThomas Petazzoni dma_free_coherent(dev, MV_XOR_POOL_SIZE, 9981ef48a26SThomas Petazzoni mv_chan->dma_desc_pool_virt, mv_chan->dma_desc_pool); 999ff7b0479SSaeed Bishara 10001ef48a26SThomas Petazzoni list_for_each_entry_safe(chan, _chan, &mv_chan->dmadev.channels, 1001ff7b0479SSaeed Bishara device_node) { 1002ff7b0479SSaeed Bishara list_del(&chan->device_node); 1003ff7b0479SSaeed Bishara } 1004ff7b0479SSaeed Bishara 100588eb92cbSThomas Petazzoni free_irq(mv_chan->irq, mv_chan); 100688eb92cbSThomas Petazzoni 1007ff7b0479SSaeed Bishara return 0; 1008ff7b0479SSaeed Bishara } 1009ff7b0479SSaeed Bishara 10101ef48a26SThomas Petazzoni static struct mv_xor_chan * 1011297eedbaSThomas Petazzoni mv_xor_channel_add(struct mv_xor_device *xordev, 1012a6b4a9d2SThomas Petazzoni struct platform_device *pdev, 1013b503fa01SThomas Petazzoni int idx, dma_cap_mask_t cap_mask, int irq) 1014ff7b0479SSaeed Bishara { 1015ff7b0479SSaeed Bishara int ret = 0; 1016ff7b0479SSaeed Bishara struct mv_xor_chan *mv_chan; 1017ff7b0479SSaeed Bishara struct dma_device *dma_dev; 1018ff7b0479SSaeed Bishara 10191ef48a26SThomas Petazzoni mv_chan = devm_kzalloc(&pdev->dev, sizeof(*mv_chan), GFP_KERNEL); 1020a577659fSSachin Kamat if (!mv_chan) 1021a577659fSSachin Kamat return ERR_PTR(-ENOMEM); 1022ff7b0479SSaeed Bishara 10239aedbdbaSThomas Petazzoni mv_chan->idx = idx; 102488eb92cbSThomas Petazzoni mv_chan->irq = irq; 1025ff7b0479SSaeed Bishara 10261ef48a26SThomas Petazzoni dma_dev = &mv_chan->dmadev; 1027ff7b0479SSaeed Bishara 1028ff7b0479SSaeed Bishara /* allocate coherent memory for hardware descriptors 1029ff7b0479SSaeed Bishara * note: writecombine gives slightly better performance, but 1030ff7b0479SSaeed Bishara * requires that we explicitly flush the writes 1031ff7b0479SSaeed Bishara */ 10321ef48a26SThomas Petazzoni mv_chan->dma_desc_pool_virt = 1033b503fa01SThomas Petazzoni dma_alloc_writecombine(&pdev->dev, MV_XOR_POOL_SIZE, 10341ef48a26SThomas Petazzoni &mv_chan->dma_desc_pool, GFP_KERNEL); 10351ef48a26SThomas Petazzoni if (!mv_chan->dma_desc_pool_virt) 1036a6b4a9d2SThomas Petazzoni return ERR_PTR(-ENOMEM); 1037ff7b0479SSaeed Bishara 1038ff7b0479SSaeed Bishara /* discover transaction capabilites from the platform data */ 1039a6b4a9d2SThomas Petazzoni dma_dev->cap_mask = cap_mask; 1040ff7b0479SSaeed Bishara 1041ff7b0479SSaeed Bishara INIT_LIST_HEAD(&dma_dev->channels); 1042ff7b0479SSaeed Bishara 1043ff7b0479SSaeed Bishara /* set base routines */ 1044ff7b0479SSaeed Bishara dma_dev->device_alloc_chan_resources = mv_xor_alloc_chan_resources; 1045ff7b0479SSaeed Bishara dma_dev->device_free_chan_resources = mv_xor_free_chan_resources; 104607934481SLinus Walleij dma_dev->device_tx_status = mv_xor_status; 1047ff7b0479SSaeed Bishara dma_dev->device_issue_pending = mv_xor_issue_pending; 104834c93c86SAndrew Lunn dma_dev->device_control = mv_xor_control; 1049ff7b0479SSaeed Bishara dma_dev->dev = &pdev->dev; 1050ff7b0479SSaeed Bishara 1051ff7b0479SSaeed Bishara /* set prep routines based on capability */ 1052ff7b0479SSaeed Bishara if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask)) 1053ff7b0479SSaeed Bishara dma_dev->device_prep_dma_memcpy = mv_xor_prep_dma_memcpy; 1054ff7b0479SSaeed Bishara if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) { 1055c019894eSJoe Perches dma_dev->max_xor = 8; 1056ff7b0479SSaeed Bishara dma_dev->device_prep_dma_xor = mv_xor_prep_dma_xor; 1057ff7b0479SSaeed Bishara } 1058ff7b0479SSaeed Bishara 1059297eedbaSThomas Petazzoni mv_chan->mmr_base = xordev->xor_base; 106082a1402eSEzequiel Garcia mv_chan->mmr_high_base = xordev->xor_high_base; 1061ff7b0479SSaeed Bishara tasklet_init(&mv_chan->irq_tasklet, mv_xor_tasklet, (unsigned long) 1062ff7b0479SSaeed Bishara mv_chan); 1063ff7b0479SSaeed Bishara 1064ff7b0479SSaeed Bishara /* clear errors before enabling interrupts */ 1065ff7b0479SSaeed Bishara mv_xor_device_clear_err_status(mv_chan); 1066ff7b0479SSaeed Bishara 10672d0a0745SThomas Petazzoni ret = request_irq(mv_chan->irq, mv_xor_interrupt_handler, 1068ff7b0479SSaeed Bishara 0, dev_name(&pdev->dev), mv_chan); 1069ff7b0479SSaeed Bishara if (ret) 1070ff7b0479SSaeed Bishara goto err_free_dma; 1071ff7b0479SSaeed Bishara 1072ff7b0479SSaeed Bishara mv_chan_unmask_interrupts(mv_chan); 1073ff7b0479SSaeed Bishara 1074ff7b0479SSaeed Bishara mv_set_mode(mv_chan, DMA_MEMCPY); 1075ff7b0479SSaeed Bishara 1076ff7b0479SSaeed Bishara spin_lock_init(&mv_chan->lock); 1077ff7b0479SSaeed Bishara INIT_LIST_HEAD(&mv_chan->chain); 1078ff7b0479SSaeed Bishara INIT_LIST_HEAD(&mv_chan->completed_slots); 1079ff7b0479SSaeed Bishara INIT_LIST_HEAD(&mv_chan->all_slots); 108098817b99SThomas Petazzoni mv_chan->dmachan.device = dma_dev; 108198817b99SThomas Petazzoni dma_cookie_init(&mv_chan->dmachan); 1082ff7b0479SSaeed Bishara 108398817b99SThomas Petazzoni list_add_tail(&mv_chan->dmachan.device_node, &dma_dev->channels); 1084ff7b0479SSaeed Bishara 1085ff7b0479SSaeed Bishara if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask)) { 1086275cc0c8SThomas Petazzoni ret = mv_xor_memcpy_self_test(mv_chan); 1087ff7b0479SSaeed Bishara dev_dbg(&pdev->dev, "memcpy self test returned %d\n", ret); 1088ff7b0479SSaeed Bishara if (ret) 10892d0a0745SThomas Petazzoni goto err_free_irq; 1090ff7b0479SSaeed Bishara } 1091ff7b0479SSaeed Bishara 1092ff7b0479SSaeed Bishara if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) { 1093275cc0c8SThomas Petazzoni ret = mv_xor_xor_self_test(mv_chan); 1094ff7b0479SSaeed Bishara dev_dbg(&pdev->dev, "xor self test returned %d\n", ret); 1095ff7b0479SSaeed Bishara if (ret) 10962d0a0745SThomas Petazzoni goto err_free_irq; 1097ff7b0479SSaeed Bishara } 1098ff7b0479SSaeed Bishara 109948a9db46SBartlomiej Zolnierkiewicz dev_info(&pdev->dev, "Marvell XOR: ( %s%s%s)\n", 1100ff7b0479SSaeed Bishara dma_has_cap(DMA_XOR, dma_dev->cap_mask) ? "xor " : "", 1101ff7b0479SSaeed Bishara dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask) ? "cpy " : "", 1102ff7b0479SSaeed Bishara dma_has_cap(DMA_INTERRUPT, dma_dev->cap_mask) ? "intr " : ""); 1103ff7b0479SSaeed Bishara 1104ff7b0479SSaeed Bishara dma_async_device_register(dma_dev); 11051ef48a26SThomas Petazzoni return mv_chan; 1106ff7b0479SSaeed Bishara 11072d0a0745SThomas Petazzoni err_free_irq: 11082d0a0745SThomas Petazzoni free_irq(mv_chan->irq, mv_chan); 1109ff7b0479SSaeed Bishara err_free_dma: 1110b503fa01SThomas Petazzoni dma_free_coherent(&pdev->dev, MV_XOR_POOL_SIZE, 11111ef48a26SThomas Petazzoni mv_chan->dma_desc_pool_virt, mv_chan->dma_desc_pool); 1112a6b4a9d2SThomas Petazzoni return ERR_PTR(ret); 1113ff7b0479SSaeed Bishara } 1114ff7b0479SSaeed Bishara 1115ff7b0479SSaeed Bishara static void 1116297eedbaSThomas Petazzoni mv_xor_conf_mbus_windows(struct mv_xor_device *xordev, 111763a9332bSAndrew Lunn const struct mbus_dram_target_info *dram) 1118ff7b0479SSaeed Bishara { 111982a1402eSEzequiel Garcia void __iomem *base = xordev->xor_high_base; 1120ff7b0479SSaeed Bishara u32 win_enable = 0; 1121ff7b0479SSaeed Bishara int i; 1122ff7b0479SSaeed Bishara 1123ff7b0479SSaeed Bishara for (i = 0; i < 8; i++) { 1124ff7b0479SSaeed Bishara writel(0, base + WINDOW_BASE(i)); 1125ff7b0479SSaeed Bishara writel(0, base + WINDOW_SIZE(i)); 1126ff7b0479SSaeed Bishara if (i < 4) 1127ff7b0479SSaeed Bishara writel(0, base + WINDOW_REMAP_HIGH(i)); 1128ff7b0479SSaeed Bishara } 1129ff7b0479SSaeed Bishara 1130ff7b0479SSaeed Bishara for (i = 0; i < dram->num_cs; i++) { 113163a9332bSAndrew Lunn const struct mbus_dram_window *cs = dram->cs + i; 1132ff7b0479SSaeed Bishara 1133ff7b0479SSaeed Bishara writel((cs->base & 0xffff0000) | 1134ff7b0479SSaeed Bishara (cs->mbus_attr << 8) | 1135ff7b0479SSaeed Bishara dram->mbus_dram_target_id, base + WINDOW_BASE(i)); 1136ff7b0479SSaeed Bishara writel((cs->size - 1) & 0xffff0000, base + WINDOW_SIZE(i)); 1137ff7b0479SSaeed Bishara 1138ff7b0479SSaeed Bishara win_enable |= (1 << i); 1139ff7b0479SSaeed Bishara win_enable |= 3 << (16 + (2 * i)); 1140ff7b0479SSaeed Bishara } 1141ff7b0479SSaeed Bishara 1142ff7b0479SSaeed Bishara writel(win_enable, base + WINDOW_BAR_ENABLE(0)); 1143ff7b0479SSaeed Bishara writel(win_enable, base + WINDOW_BAR_ENABLE(1)); 1144c4b4b732SThomas Petazzoni writel(0, base + WINDOW_OVERRIDE_CTRL(0)); 1145c4b4b732SThomas Petazzoni writel(0, base + WINDOW_OVERRIDE_CTRL(1)); 1146ff7b0479SSaeed Bishara } 1147ff7b0479SSaeed Bishara 1148c2714334SLinus Torvalds static int mv_xor_probe(struct platform_device *pdev) 1149ff7b0479SSaeed Bishara { 115063a9332bSAndrew Lunn const struct mbus_dram_target_info *dram; 1151297eedbaSThomas Petazzoni struct mv_xor_device *xordev; 1152d4adcc01SJingoo Han struct mv_xor_platform_data *pdata = dev_get_platdata(&pdev->dev); 1153ff7b0479SSaeed Bishara struct resource *res; 115460d151f3SThomas Petazzoni int i, ret; 1155ff7b0479SSaeed Bishara 11561ba151cdSJoe Perches dev_notice(&pdev->dev, "Marvell shared XOR driver\n"); 1157ff7b0479SSaeed Bishara 1158297eedbaSThomas Petazzoni xordev = devm_kzalloc(&pdev->dev, sizeof(*xordev), GFP_KERNEL); 1159297eedbaSThomas Petazzoni if (!xordev) 1160ff7b0479SSaeed Bishara return -ENOMEM; 1161ff7b0479SSaeed Bishara 1162ff7b0479SSaeed Bishara res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1163ff7b0479SSaeed Bishara if (!res) 1164ff7b0479SSaeed Bishara return -ENODEV; 1165ff7b0479SSaeed Bishara 1166297eedbaSThomas Petazzoni xordev->xor_base = devm_ioremap(&pdev->dev, res->start, 11674de1ba15SH Hartley Sweeten resource_size(res)); 1168297eedbaSThomas Petazzoni if (!xordev->xor_base) 1169ff7b0479SSaeed Bishara return -EBUSY; 1170ff7b0479SSaeed Bishara 1171ff7b0479SSaeed Bishara res = platform_get_resource(pdev, IORESOURCE_MEM, 1); 1172ff7b0479SSaeed Bishara if (!res) 1173ff7b0479SSaeed Bishara return -ENODEV; 1174ff7b0479SSaeed Bishara 1175297eedbaSThomas Petazzoni xordev->xor_high_base = devm_ioremap(&pdev->dev, res->start, 11764de1ba15SH Hartley Sweeten resource_size(res)); 1177297eedbaSThomas Petazzoni if (!xordev->xor_high_base) 1178ff7b0479SSaeed Bishara return -EBUSY; 1179ff7b0479SSaeed Bishara 1180297eedbaSThomas Petazzoni platform_set_drvdata(pdev, xordev); 1181ff7b0479SSaeed Bishara 1182ff7b0479SSaeed Bishara /* 1183ff7b0479SSaeed Bishara * (Re-)program MBUS remapping windows if we are asked to. 1184ff7b0479SSaeed Bishara */ 118563a9332bSAndrew Lunn dram = mv_mbus_dram_info(); 118663a9332bSAndrew Lunn if (dram) 1187297eedbaSThomas Petazzoni mv_xor_conf_mbus_windows(xordev, dram); 1188ff7b0479SSaeed Bishara 1189c510182bSAndrew Lunn /* Not all platforms can gate the clock, so it is not 1190c510182bSAndrew Lunn * an error if the clock does not exists. 1191c510182bSAndrew Lunn */ 1192297eedbaSThomas Petazzoni xordev->clk = clk_get(&pdev->dev, NULL); 1193297eedbaSThomas Petazzoni if (!IS_ERR(xordev->clk)) 1194297eedbaSThomas Petazzoni clk_prepare_enable(xordev->clk); 1195c510182bSAndrew Lunn 1196f7d12ef5SThomas Petazzoni if (pdev->dev.of_node) { 1197f7d12ef5SThomas Petazzoni struct device_node *np; 1198f7d12ef5SThomas Petazzoni int i = 0; 1199f7d12ef5SThomas Petazzoni 1200f7d12ef5SThomas Petazzoni for_each_child_of_node(pdev->dev.of_node, np) { 12010be8253fSRussell King struct mv_xor_chan *chan; 1202f7d12ef5SThomas Petazzoni dma_cap_mask_t cap_mask; 1203f7d12ef5SThomas Petazzoni int irq; 1204f7d12ef5SThomas Petazzoni 1205f7d12ef5SThomas Petazzoni dma_cap_zero(cap_mask); 1206f7d12ef5SThomas Petazzoni if (of_property_read_bool(np, "dmacap,memcpy")) 1207f7d12ef5SThomas Petazzoni dma_cap_set(DMA_MEMCPY, cap_mask); 1208f7d12ef5SThomas Petazzoni if (of_property_read_bool(np, "dmacap,xor")) 1209f7d12ef5SThomas Petazzoni dma_cap_set(DMA_XOR, cap_mask); 1210f7d12ef5SThomas Petazzoni if (of_property_read_bool(np, "dmacap,interrupt")) 1211f7d12ef5SThomas Petazzoni dma_cap_set(DMA_INTERRUPT, cap_mask); 1212f7d12ef5SThomas Petazzoni 1213f7d12ef5SThomas Petazzoni irq = irq_of_parse_and_map(np, 0); 1214f8eb9e7dSThomas Petazzoni if (!irq) { 1215f8eb9e7dSThomas Petazzoni ret = -ENODEV; 1216f7d12ef5SThomas Petazzoni goto err_channel_add; 1217f7d12ef5SThomas Petazzoni } 1218f7d12ef5SThomas Petazzoni 12190be8253fSRussell King chan = mv_xor_channel_add(xordev, pdev, i, 1220f7d12ef5SThomas Petazzoni cap_mask, irq); 12210be8253fSRussell King if (IS_ERR(chan)) { 12220be8253fSRussell King ret = PTR_ERR(chan); 1223f7d12ef5SThomas Petazzoni irq_dispose_mapping(irq); 1224f7d12ef5SThomas Petazzoni goto err_channel_add; 1225f7d12ef5SThomas Petazzoni } 1226f7d12ef5SThomas Petazzoni 12270be8253fSRussell King xordev->channels[i] = chan; 1228f7d12ef5SThomas Petazzoni i++; 1229f7d12ef5SThomas Petazzoni } 1230f7d12ef5SThomas Petazzoni } else if (pdata && pdata->channels) { 123160d151f3SThomas Petazzoni for (i = 0; i < MV_XOR_MAX_CHANNELS; i++) { 1232e39f6ec1SThomas Petazzoni struct mv_xor_channel_data *cd; 12330be8253fSRussell King struct mv_xor_chan *chan; 123460d151f3SThomas Petazzoni int irq; 123560d151f3SThomas Petazzoni 123660d151f3SThomas Petazzoni cd = &pdata->channels[i]; 123760d151f3SThomas Petazzoni if (!cd) { 123860d151f3SThomas Petazzoni ret = -ENODEV; 123960d151f3SThomas Petazzoni goto err_channel_add; 124060d151f3SThomas Petazzoni } 124160d151f3SThomas Petazzoni 124260d151f3SThomas Petazzoni irq = platform_get_irq(pdev, i); 124360d151f3SThomas Petazzoni if (irq < 0) { 124460d151f3SThomas Petazzoni ret = irq; 124560d151f3SThomas Petazzoni goto err_channel_add; 124660d151f3SThomas Petazzoni } 124760d151f3SThomas Petazzoni 12480be8253fSRussell King chan = mv_xor_channel_add(xordev, pdev, i, 1249b503fa01SThomas Petazzoni cd->cap_mask, irq); 12500be8253fSRussell King if (IS_ERR(chan)) { 12510be8253fSRussell King ret = PTR_ERR(chan); 125260d151f3SThomas Petazzoni goto err_channel_add; 125360d151f3SThomas Petazzoni } 12540be8253fSRussell King 12550be8253fSRussell King xordev->channels[i] = chan; 125660d151f3SThomas Petazzoni } 125760d151f3SThomas Petazzoni } 125860d151f3SThomas Petazzoni 1259ff7b0479SSaeed Bishara return 0; 126060d151f3SThomas Petazzoni 126160d151f3SThomas Petazzoni err_channel_add: 126260d151f3SThomas Petazzoni for (i = 0; i < MV_XOR_MAX_CHANNELS; i++) 1263f7d12ef5SThomas Petazzoni if (xordev->channels[i]) { 1264ab6e439fSThomas Petazzoni mv_xor_channel_remove(xordev->channels[i]); 1265f7d12ef5SThomas Petazzoni if (pdev->dev.of_node) 1266f7d12ef5SThomas Petazzoni irq_dispose_mapping(xordev->channels[i]->irq); 1267f7d12ef5SThomas Petazzoni } 126860d151f3SThomas Petazzoni 1269dab92064SThomas Petazzoni if (!IS_ERR(xordev->clk)) { 1270297eedbaSThomas Petazzoni clk_disable_unprepare(xordev->clk); 1271297eedbaSThomas Petazzoni clk_put(xordev->clk); 1272dab92064SThomas Petazzoni } 1273dab92064SThomas Petazzoni 127460d151f3SThomas Petazzoni return ret; 1275ff7b0479SSaeed Bishara } 1276ff7b0479SSaeed Bishara 1277c2714334SLinus Torvalds static int mv_xor_remove(struct platform_device *pdev) 1278ff7b0479SSaeed Bishara { 1279297eedbaSThomas Petazzoni struct mv_xor_device *xordev = platform_get_drvdata(pdev); 128060d151f3SThomas Petazzoni int i; 128160d151f3SThomas Petazzoni 128260d151f3SThomas Petazzoni for (i = 0; i < MV_XOR_MAX_CHANNELS; i++) { 1283297eedbaSThomas Petazzoni if (xordev->channels[i]) 1284297eedbaSThomas Petazzoni mv_xor_channel_remove(xordev->channels[i]); 128560d151f3SThomas Petazzoni } 1286c510182bSAndrew Lunn 1287297eedbaSThomas Petazzoni if (!IS_ERR(xordev->clk)) { 1288297eedbaSThomas Petazzoni clk_disable_unprepare(xordev->clk); 1289297eedbaSThomas Petazzoni clk_put(xordev->clk); 1290c510182bSAndrew Lunn } 1291c510182bSAndrew Lunn 1292ff7b0479SSaeed Bishara return 0; 1293ff7b0479SSaeed Bishara } 1294ff7b0479SSaeed Bishara 1295f7d12ef5SThomas Petazzoni #ifdef CONFIG_OF 1296c2714334SLinus Torvalds static struct of_device_id mv_xor_dt_ids[] = { 1297f7d12ef5SThomas Petazzoni { .compatible = "marvell,orion-xor", }, 1298f7d12ef5SThomas Petazzoni {}, 1299f7d12ef5SThomas Petazzoni }; 1300f7d12ef5SThomas Petazzoni MODULE_DEVICE_TABLE(of, mv_xor_dt_ids); 1301f7d12ef5SThomas Petazzoni #endif 1302f7d12ef5SThomas Petazzoni 1303ff7b0479SSaeed Bishara static struct platform_driver mv_xor_driver = { 1304ff7b0479SSaeed Bishara .probe = mv_xor_probe, 1305a7d6e3ecSBill Pemberton .remove = mv_xor_remove, 1306ff7b0479SSaeed Bishara .driver = { 1307ff7b0479SSaeed Bishara .owner = THIS_MODULE, 1308ff7b0479SSaeed Bishara .name = MV_XOR_NAME, 1309f7d12ef5SThomas Petazzoni .of_match_table = of_match_ptr(mv_xor_dt_ids), 1310ff7b0479SSaeed Bishara }, 1311ff7b0479SSaeed Bishara }; 1312ff7b0479SSaeed Bishara 1313ff7b0479SSaeed Bishara 1314ff7b0479SSaeed Bishara static int __init mv_xor_init(void) 1315ff7b0479SSaeed Bishara { 131661971656SThomas Petazzoni return platform_driver_register(&mv_xor_driver); 1317ff7b0479SSaeed Bishara } 1318ff7b0479SSaeed Bishara module_init(mv_xor_init); 1319ff7b0479SSaeed Bishara 1320ff7b0479SSaeed Bishara /* it's currently unsafe to unload this module */ 1321ff7b0479SSaeed Bishara #if 0 1322ff7b0479SSaeed Bishara static void __exit mv_xor_exit(void) 1323ff7b0479SSaeed Bishara { 1324ff7b0479SSaeed Bishara platform_driver_unregister(&mv_xor_driver); 1325ff7b0479SSaeed Bishara return; 1326ff7b0479SSaeed Bishara } 1327ff7b0479SSaeed Bishara 1328ff7b0479SSaeed Bishara module_exit(mv_xor_exit); 1329ff7b0479SSaeed Bishara #endif 1330ff7b0479SSaeed Bishara 1331ff7b0479SSaeed Bishara MODULE_AUTHOR("Saeed Bishara <saeed@marvell.com>"); 1332ff7b0479SSaeed Bishara MODULE_DESCRIPTION("DMA engine driver for Marvell's XOR engine"); 1333ff7b0479SSaeed Bishara MODULE_LICENSE("GPL"); 1334