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 15ff7b0479SSaeed Bishara #include <linux/init.h> 165a0e3ad6STejun Heo #include <linux/slab.h> 17ff7b0479SSaeed Bishara #include <linux/delay.h> 18ff7b0479SSaeed Bishara #include <linux/dma-mapping.h> 19ff7b0479SSaeed Bishara #include <linux/spinlock.h> 20ff7b0479SSaeed Bishara #include <linux/interrupt.h> 216f166312SLior Amsalem #include <linux/of_device.h> 22ff7b0479SSaeed Bishara #include <linux/platform_device.h> 23ff7b0479SSaeed Bishara #include <linux/memory.h> 24c510182bSAndrew Lunn #include <linux/clk.h> 25f7d12ef5SThomas Petazzoni #include <linux/of.h> 26f7d12ef5SThomas Petazzoni #include <linux/of_irq.h> 27f7d12ef5SThomas Petazzoni #include <linux/irqdomain.h> 2877757291SThomas Petazzoni #include <linux/cpumask.h> 29c02cecb9SArnd Bergmann #include <linux/platform_data/dma-mv_xor.h> 30d2ebfb33SRussell King - ARM Linux 31d2ebfb33SRussell King - ARM Linux #include "dmaengine.h" 32ff7b0479SSaeed Bishara #include "mv_xor.h" 33ff7b0479SSaeed Bishara 346f166312SLior Amsalem enum mv_xor_mode { 356f166312SLior Amsalem XOR_MODE_IN_REG, 366f166312SLior Amsalem XOR_MODE_IN_DESC, 376f166312SLior Amsalem }; 386f166312SLior Amsalem 39ff7b0479SSaeed Bishara static void mv_xor_issue_pending(struct dma_chan *chan); 40ff7b0479SSaeed Bishara 41ff7b0479SSaeed Bishara #define to_mv_xor_chan(chan) \ 4298817b99SThomas Petazzoni container_of(chan, struct mv_xor_chan, dmachan) 43ff7b0479SSaeed Bishara 44ff7b0479SSaeed Bishara #define to_mv_xor_slot(tx) \ 45ff7b0479SSaeed Bishara container_of(tx, struct mv_xor_desc_slot, async_tx) 46ff7b0479SSaeed Bishara 47c98c1781SThomas Petazzoni #define mv_chan_to_devp(chan) \ 481ef48a26SThomas Petazzoni ((chan)->dmadev.dev) 49c98c1781SThomas Petazzoni 50dfc97661SLior Amsalem static void mv_desc_init(struct mv_xor_desc_slot *desc, 51ba87d137SLior Amsalem dma_addr_t addr, u32 byte_count, 52ba87d137SLior Amsalem enum dma_ctrl_flags flags) 53ff7b0479SSaeed Bishara { 54ff7b0479SSaeed Bishara struct mv_xor_desc *hw_desc = desc->hw_desc; 55ff7b0479SSaeed Bishara 560e7488edSEzequiel Garcia hw_desc->status = XOR_DESC_DMA_OWNED; 57ff7b0479SSaeed Bishara hw_desc->phy_next_desc = 0; 58ba87d137SLior Amsalem /* Enable end-of-descriptor interrupts only for DMA_PREP_INTERRUPT */ 59ba87d137SLior Amsalem hw_desc->desc_command = (flags & DMA_PREP_INTERRUPT) ? 60ba87d137SLior Amsalem XOR_DESC_EOD_INT_EN : 0; 61dfc97661SLior Amsalem hw_desc->phy_dest_addr = addr; 62ff7b0479SSaeed Bishara hw_desc->byte_count = byte_count; 63ff7b0479SSaeed Bishara } 64ff7b0479SSaeed Bishara 656f166312SLior Amsalem static void mv_desc_set_mode(struct mv_xor_desc_slot *desc) 666f166312SLior Amsalem { 676f166312SLior Amsalem struct mv_xor_desc *hw_desc = desc->hw_desc; 686f166312SLior Amsalem 696f166312SLior Amsalem switch (desc->type) { 706f166312SLior Amsalem case DMA_XOR: 716f166312SLior Amsalem case DMA_INTERRUPT: 726f166312SLior Amsalem hw_desc->desc_command |= XOR_DESC_OPERATION_XOR; 736f166312SLior Amsalem break; 746f166312SLior Amsalem case DMA_MEMCPY: 756f166312SLior Amsalem hw_desc->desc_command |= XOR_DESC_OPERATION_MEMCPY; 766f166312SLior Amsalem break; 776f166312SLior Amsalem default: 786f166312SLior Amsalem BUG(); 796f166312SLior Amsalem return; 806f166312SLior Amsalem } 816f166312SLior Amsalem } 826f166312SLior Amsalem 83ff7b0479SSaeed Bishara static void mv_desc_set_next_desc(struct mv_xor_desc_slot *desc, 84ff7b0479SSaeed Bishara u32 next_desc_addr) 85ff7b0479SSaeed Bishara { 86ff7b0479SSaeed Bishara struct mv_xor_desc *hw_desc = desc->hw_desc; 87ff7b0479SSaeed Bishara BUG_ON(hw_desc->phy_next_desc); 88ff7b0479SSaeed Bishara hw_desc->phy_next_desc = next_desc_addr; 89ff7b0479SSaeed Bishara } 90ff7b0479SSaeed Bishara 91ff7b0479SSaeed Bishara static void mv_desc_set_src_addr(struct mv_xor_desc_slot *desc, 92ff7b0479SSaeed Bishara int index, dma_addr_t addr) 93ff7b0479SSaeed Bishara { 94ff7b0479SSaeed Bishara struct mv_xor_desc *hw_desc = desc->hw_desc; 95e03bc654SThomas Petazzoni hw_desc->phy_src_addr[mv_phy_src_idx(index)] = addr; 96ff7b0479SSaeed Bishara if (desc->type == DMA_XOR) 97ff7b0479SSaeed Bishara hw_desc->desc_command |= (1 << index); 98ff7b0479SSaeed Bishara } 99ff7b0479SSaeed Bishara 100ff7b0479SSaeed Bishara static u32 mv_chan_get_current_desc(struct mv_xor_chan *chan) 101ff7b0479SSaeed Bishara { 1025733c38aSThomas Petazzoni return readl_relaxed(XOR_CURR_DESC(chan)); 103ff7b0479SSaeed Bishara } 104ff7b0479SSaeed Bishara 105ff7b0479SSaeed Bishara static void mv_chan_set_next_descriptor(struct mv_xor_chan *chan, 106ff7b0479SSaeed Bishara u32 next_desc_addr) 107ff7b0479SSaeed Bishara { 1085733c38aSThomas Petazzoni writel_relaxed(next_desc_addr, XOR_NEXT_DESC(chan)); 109ff7b0479SSaeed Bishara } 110ff7b0479SSaeed Bishara 111ff7b0479SSaeed Bishara static void mv_chan_unmask_interrupts(struct mv_xor_chan *chan) 112ff7b0479SSaeed Bishara { 1135733c38aSThomas Petazzoni u32 val = readl_relaxed(XOR_INTR_MASK(chan)); 114ff7b0479SSaeed Bishara val |= XOR_INTR_MASK_VALUE << (chan->idx * 16); 1155733c38aSThomas Petazzoni writel_relaxed(val, XOR_INTR_MASK(chan)); 116ff7b0479SSaeed Bishara } 117ff7b0479SSaeed Bishara 118ff7b0479SSaeed Bishara static u32 mv_chan_get_intr_cause(struct mv_xor_chan *chan) 119ff7b0479SSaeed Bishara { 1205733c38aSThomas Petazzoni u32 intr_cause = readl_relaxed(XOR_INTR_CAUSE(chan)); 121ff7b0479SSaeed Bishara intr_cause = (intr_cause >> (chan->idx * 16)) & 0xFFFF; 122ff7b0479SSaeed Bishara return intr_cause; 123ff7b0479SSaeed Bishara } 124ff7b0479SSaeed Bishara 1250951e728SMaxime Ripard static void mv_chan_clear_eoc_cause(struct mv_xor_chan *chan) 126ff7b0479SSaeed Bishara { 127ba87d137SLior Amsalem u32 val; 128ba87d137SLior Amsalem 129ba87d137SLior Amsalem val = XOR_INT_END_OF_DESC | XOR_INT_END_OF_CHAIN | XOR_INT_STOPPED; 130ba87d137SLior Amsalem val = ~(val << (chan->idx * 16)); 131c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(chan), "%s, val 0x%08x\n", __func__, val); 1325733c38aSThomas Petazzoni writel_relaxed(val, XOR_INTR_CAUSE(chan)); 133ff7b0479SSaeed Bishara } 134ff7b0479SSaeed Bishara 1350951e728SMaxime Ripard static void mv_chan_clear_err_status(struct mv_xor_chan *chan) 136ff7b0479SSaeed Bishara { 137ff7b0479SSaeed Bishara u32 val = 0xFFFF0000 >> (chan->idx * 16); 1385733c38aSThomas Petazzoni writel_relaxed(val, XOR_INTR_CAUSE(chan)); 139ff7b0479SSaeed Bishara } 140ff7b0479SSaeed Bishara 1410951e728SMaxime Ripard static void mv_chan_set_mode(struct mv_xor_chan *chan, 142ff7b0479SSaeed Bishara enum dma_transaction_type type) 143ff7b0479SSaeed Bishara { 144ff7b0479SSaeed Bishara u32 op_mode; 1455733c38aSThomas Petazzoni u32 config = readl_relaxed(XOR_CONFIG(chan)); 146ff7b0479SSaeed Bishara 147ff7b0479SSaeed Bishara switch (type) { 148ff7b0479SSaeed Bishara case DMA_XOR: 149ff7b0479SSaeed Bishara op_mode = XOR_OPERATION_MODE_XOR; 150ff7b0479SSaeed Bishara break; 151ff7b0479SSaeed Bishara case DMA_MEMCPY: 152ff7b0479SSaeed Bishara op_mode = XOR_OPERATION_MODE_MEMCPY; 153ff7b0479SSaeed Bishara break; 154ff7b0479SSaeed Bishara default: 155c98c1781SThomas Petazzoni dev_err(mv_chan_to_devp(chan), 1561ba151cdSJoe Perches "error: unsupported operation %d\n", 157ff7b0479SSaeed Bishara type); 158ff7b0479SSaeed Bishara BUG(); 159ff7b0479SSaeed Bishara return; 160ff7b0479SSaeed Bishara } 161ff7b0479SSaeed Bishara 162ff7b0479SSaeed Bishara config &= ~0x7; 163ff7b0479SSaeed Bishara config |= op_mode; 164e03bc654SThomas Petazzoni 1656f166312SLior Amsalem if (IS_ENABLED(__BIG_ENDIAN)) 1666f166312SLior Amsalem config |= XOR_DESCRIPTOR_SWAP; 1676f166312SLior Amsalem else 1686f166312SLior Amsalem config &= ~XOR_DESCRIPTOR_SWAP; 1696f166312SLior Amsalem 1706f166312SLior Amsalem writel_relaxed(config, XOR_CONFIG(chan)); 1716f166312SLior Amsalem chan->current_type = type; 1726f166312SLior Amsalem } 1736f166312SLior Amsalem 1746f166312SLior Amsalem static void mv_chan_set_mode_to_desc(struct mv_xor_chan *chan) 1756f166312SLior Amsalem { 1766f166312SLior Amsalem u32 op_mode; 1776f166312SLior Amsalem u32 config = readl_relaxed(XOR_CONFIG(chan)); 1786f166312SLior Amsalem 1796f166312SLior Amsalem op_mode = XOR_OPERATION_MODE_IN_DESC; 1806f166312SLior Amsalem 1816f166312SLior Amsalem config &= ~0x7; 1826f166312SLior Amsalem config |= op_mode; 1836f166312SLior Amsalem 184e03bc654SThomas Petazzoni #if defined(__BIG_ENDIAN) 185e03bc654SThomas Petazzoni config |= XOR_DESCRIPTOR_SWAP; 186e03bc654SThomas Petazzoni #else 187e03bc654SThomas Petazzoni config &= ~XOR_DESCRIPTOR_SWAP; 188e03bc654SThomas Petazzoni #endif 189e03bc654SThomas Petazzoni 1905733c38aSThomas Petazzoni writel_relaxed(config, XOR_CONFIG(chan)); 191ff7b0479SSaeed Bishara } 192ff7b0479SSaeed Bishara 193ff7b0479SSaeed Bishara static void mv_chan_activate(struct mv_xor_chan *chan) 194ff7b0479SSaeed Bishara { 195c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(chan), " activate chan.\n"); 1965a9a55bfSEzequiel Garcia 1975a9a55bfSEzequiel Garcia /* writel ensures all descriptors are flushed before activation */ 1985a9a55bfSEzequiel Garcia writel(BIT(0), XOR_ACTIVATION(chan)); 199ff7b0479SSaeed Bishara } 200ff7b0479SSaeed Bishara 201ff7b0479SSaeed Bishara static char mv_chan_is_busy(struct mv_xor_chan *chan) 202ff7b0479SSaeed Bishara { 2035733c38aSThomas Petazzoni u32 state = readl_relaxed(XOR_ACTIVATION(chan)); 204ff7b0479SSaeed Bishara 205ff7b0479SSaeed Bishara state = (state >> 4) & 0x3; 206ff7b0479SSaeed Bishara 207ff7b0479SSaeed Bishara return (state == 1) ? 1 : 0; 208ff7b0479SSaeed Bishara } 209ff7b0479SSaeed Bishara 210ff7b0479SSaeed Bishara /* 2110951e728SMaxime Ripard * mv_chan_start_new_chain - program the engine to operate on new 2120951e728SMaxime Ripard * chain headed by sw_desc 213ff7b0479SSaeed Bishara * Caller must hold &mv_chan->lock while calling this function 214ff7b0479SSaeed Bishara */ 2150951e728SMaxime Ripard static void mv_chan_start_new_chain(struct mv_xor_chan *mv_chan, 216ff7b0479SSaeed Bishara struct mv_xor_desc_slot *sw_desc) 217ff7b0479SSaeed Bishara { 218c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), "%s %d: sw_desc %p\n", 219ff7b0479SSaeed Bishara __func__, __LINE__, sw_desc); 220ff7b0479SSaeed Bishara 221ff7b0479SSaeed Bishara /* set the hardware chain */ 222ff7b0479SSaeed Bishara mv_chan_set_next_descriptor(mv_chan, sw_desc->async_tx.phys); 22348a9db46SBartlomiej Zolnierkiewicz 224dfc97661SLior Amsalem mv_chan->pending++; 22598817b99SThomas Petazzoni mv_xor_issue_pending(&mv_chan->dmachan); 226ff7b0479SSaeed Bishara } 227ff7b0479SSaeed Bishara 228ff7b0479SSaeed Bishara static dma_cookie_t 2290951e728SMaxime Ripard mv_desc_run_tx_complete_actions(struct mv_xor_desc_slot *desc, 2300951e728SMaxime Ripard struct mv_xor_chan *mv_chan, 2310951e728SMaxime Ripard dma_cookie_t cookie) 232ff7b0479SSaeed Bishara { 233ff7b0479SSaeed Bishara BUG_ON(desc->async_tx.cookie < 0); 234ff7b0479SSaeed Bishara 235ff7b0479SSaeed Bishara if (desc->async_tx.cookie > 0) { 236ff7b0479SSaeed Bishara cookie = desc->async_tx.cookie; 237ff7b0479SSaeed Bishara 238ff7b0479SSaeed Bishara /* call the callback (must not sleep or submit new 239ff7b0479SSaeed Bishara * operations to this channel) 240ff7b0479SSaeed Bishara */ 241ff7b0479SSaeed Bishara if (desc->async_tx.callback) 242ff7b0479SSaeed Bishara desc->async_tx.callback( 243ff7b0479SSaeed Bishara desc->async_tx.callback_param); 244ff7b0479SSaeed Bishara 245d38a8c62SDan Williams dma_descriptor_unmap(&desc->async_tx); 246ff7b0479SSaeed Bishara } 247ff7b0479SSaeed Bishara 248ff7b0479SSaeed Bishara /* run dependent operations */ 24907f2211eSDan Williams dma_run_dependencies(&desc->async_tx); 250ff7b0479SSaeed Bishara 251ff7b0479SSaeed Bishara return cookie; 252ff7b0479SSaeed Bishara } 253ff7b0479SSaeed Bishara 254ff7b0479SSaeed Bishara static int 2550951e728SMaxime Ripard mv_chan_clean_completed_slots(struct mv_xor_chan *mv_chan) 256ff7b0479SSaeed Bishara { 257ff7b0479SSaeed Bishara struct mv_xor_desc_slot *iter, *_iter; 258ff7b0479SSaeed Bishara 259c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), "%s %d\n", __func__, __LINE__); 260ff7b0479SSaeed Bishara list_for_each_entry_safe(iter, _iter, &mv_chan->completed_slots, 261fbea28a2SLior Amsalem node) { 262ff7b0479SSaeed Bishara 263fbea28a2SLior Amsalem if (async_tx_test_ack(&iter->async_tx)) 264fbea28a2SLior Amsalem list_move_tail(&iter->node, &mv_chan->free_slots); 265ff7b0479SSaeed Bishara } 266ff7b0479SSaeed Bishara return 0; 267ff7b0479SSaeed Bishara } 268ff7b0479SSaeed Bishara 269ff7b0479SSaeed Bishara static int 2700951e728SMaxime Ripard mv_desc_clean_slot(struct mv_xor_desc_slot *desc, 271ff7b0479SSaeed Bishara struct mv_xor_chan *mv_chan) 272ff7b0479SSaeed Bishara { 273c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), "%s %d: desc %p flags %d\n", 274ff7b0479SSaeed Bishara __func__, __LINE__, desc, desc->async_tx.flags); 275fbea28a2SLior Amsalem 276ff7b0479SSaeed Bishara /* the client is allowed to attach dependent operations 277ff7b0479SSaeed Bishara * until 'ack' is set 278ff7b0479SSaeed Bishara */ 279fbea28a2SLior Amsalem if (!async_tx_test_ack(&desc->async_tx)) 280ff7b0479SSaeed Bishara /* move this slot to the completed_slots */ 281fbea28a2SLior Amsalem list_move_tail(&desc->node, &mv_chan->completed_slots); 282fbea28a2SLior Amsalem else 283fbea28a2SLior Amsalem list_move_tail(&desc->node, &mv_chan->free_slots); 284ff7b0479SSaeed Bishara 285ff7b0479SSaeed Bishara return 0; 286ff7b0479SSaeed Bishara } 287ff7b0479SSaeed Bishara 288fbeec99aSEzequiel Garcia /* This function must be called with the mv_xor_chan spinlock held */ 2890951e728SMaxime Ripard static void mv_chan_slot_cleanup(struct mv_xor_chan *mv_chan) 290ff7b0479SSaeed Bishara { 291ff7b0479SSaeed Bishara struct mv_xor_desc_slot *iter, *_iter; 292ff7b0479SSaeed Bishara dma_cookie_t cookie = 0; 293ff7b0479SSaeed Bishara int busy = mv_chan_is_busy(mv_chan); 294ff7b0479SSaeed Bishara u32 current_desc = mv_chan_get_current_desc(mv_chan); 2959136291fSLior Amsalem int current_cleaned = 0; 2969136291fSLior Amsalem struct mv_xor_desc *hw_desc; 297ff7b0479SSaeed Bishara 298c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), "%s %d\n", __func__, __LINE__); 299c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), "current_desc %x\n", current_desc); 3000951e728SMaxime Ripard mv_chan_clean_completed_slots(mv_chan); 301ff7b0479SSaeed Bishara 302ff7b0479SSaeed Bishara /* free completed slots from the chain starting with 303ff7b0479SSaeed Bishara * the oldest descriptor 304ff7b0479SSaeed Bishara */ 305ff7b0479SSaeed Bishara 306ff7b0479SSaeed Bishara list_for_each_entry_safe(iter, _iter, &mv_chan->chain, 307fbea28a2SLior Amsalem node) { 308ff7b0479SSaeed Bishara 3099136291fSLior Amsalem /* clean finished descriptors */ 3109136291fSLior Amsalem hw_desc = iter->hw_desc; 3119136291fSLior Amsalem if (hw_desc->status & XOR_DESC_SUCCESS) { 3120951e728SMaxime Ripard cookie = mv_desc_run_tx_complete_actions(iter, mv_chan, 3139136291fSLior Amsalem cookie); 314ff7b0479SSaeed Bishara 3159136291fSLior Amsalem /* done processing desc, clean slot */ 3160951e728SMaxime Ripard mv_desc_clean_slot(iter, mv_chan); 3179136291fSLior Amsalem 3189136291fSLior Amsalem /* break if we did cleaned the current */ 319ff7b0479SSaeed Bishara if (iter->async_tx.phys == current_desc) { 3209136291fSLior Amsalem current_cleaned = 1; 321ff7b0479SSaeed Bishara break; 322ff7b0479SSaeed Bishara } 3239136291fSLior Amsalem } else { 3249136291fSLior Amsalem if (iter->async_tx.phys == current_desc) { 3259136291fSLior Amsalem current_cleaned = 0; 326ff7b0479SSaeed Bishara break; 327ff7b0479SSaeed Bishara } 3289136291fSLior Amsalem } 3299136291fSLior Amsalem } 330ff7b0479SSaeed Bishara 331ff7b0479SSaeed Bishara if ((busy == 0) && !list_empty(&mv_chan->chain)) { 3329136291fSLior Amsalem if (current_cleaned) { 3339136291fSLior Amsalem /* 3349136291fSLior Amsalem * current descriptor cleaned and removed, run 3359136291fSLior Amsalem * from list head 3369136291fSLior Amsalem */ 3379136291fSLior Amsalem iter = list_entry(mv_chan->chain.next, 338ff7b0479SSaeed Bishara struct mv_xor_desc_slot, 339fbea28a2SLior Amsalem node); 3400951e728SMaxime Ripard mv_chan_start_new_chain(mv_chan, iter); 3419136291fSLior Amsalem } else { 342fbea28a2SLior Amsalem if (!list_is_last(&iter->node, &mv_chan->chain)) { 3439136291fSLior Amsalem /* 3449136291fSLior Amsalem * descriptors are still waiting after 3459136291fSLior Amsalem * current, trigger them 3469136291fSLior Amsalem */ 347fbea28a2SLior Amsalem iter = list_entry(iter->node.next, 3489136291fSLior Amsalem struct mv_xor_desc_slot, 349fbea28a2SLior Amsalem node); 3500951e728SMaxime Ripard mv_chan_start_new_chain(mv_chan, iter); 3519136291fSLior Amsalem } else { 3529136291fSLior Amsalem /* 3539136291fSLior Amsalem * some descriptors are still waiting 3549136291fSLior Amsalem * to be cleaned 3559136291fSLior Amsalem */ 3569136291fSLior Amsalem tasklet_schedule(&mv_chan->irq_tasklet); 3579136291fSLior Amsalem } 3589136291fSLior Amsalem } 359ff7b0479SSaeed Bishara } 360ff7b0479SSaeed Bishara 361ff7b0479SSaeed Bishara if (cookie > 0) 36298817b99SThomas Petazzoni mv_chan->dmachan.completed_cookie = cookie; 363ff7b0479SSaeed Bishara } 364ff7b0479SSaeed Bishara 365ff7b0479SSaeed Bishara static void mv_xor_tasklet(unsigned long data) 366ff7b0479SSaeed Bishara { 367ff7b0479SSaeed Bishara struct mv_xor_chan *chan = (struct mv_xor_chan *) data; 368e43147acSEzequiel Garcia 369e43147acSEzequiel Garcia spin_lock_bh(&chan->lock); 3700951e728SMaxime Ripard mv_chan_slot_cleanup(chan); 371e43147acSEzequiel Garcia spin_unlock_bh(&chan->lock); 372ff7b0479SSaeed Bishara } 373ff7b0479SSaeed Bishara 374ff7b0479SSaeed Bishara static struct mv_xor_desc_slot * 3750951e728SMaxime Ripard mv_chan_alloc_slot(struct mv_xor_chan *mv_chan) 376ff7b0479SSaeed Bishara { 377fbea28a2SLior Amsalem struct mv_xor_desc_slot *iter; 378ff7b0479SSaeed Bishara 379fbea28a2SLior Amsalem spin_lock_bh(&mv_chan->lock); 380fbea28a2SLior Amsalem 381fbea28a2SLior Amsalem if (!list_empty(&mv_chan->free_slots)) { 382fbea28a2SLior Amsalem iter = list_first_entry(&mv_chan->free_slots, 383ff7b0479SSaeed Bishara struct mv_xor_desc_slot, 384fbea28a2SLior Amsalem node); 385ff7b0479SSaeed Bishara 386fbea28a2SLior Amsalem list_move_tail(&iter->node, &mv_chan->allocated_slots); 387dfc97661SLior Amsalem 388fbea28a2SLior Amsalem spin_unlock_bh(&mv_chan->lock); 389ff7b0479SSaeed Bishara 390dfc97661SLior Amsalem /* pre-ack descriptor */ 391ff7b0479SSaeed Bishara async_tx_ack(&iter->async_tx); 392dfc97661SLior Amsalem iter->async_tx.cookie = -EBUSY; 393dfc97661SLior Amsalem 394dfc97661SLior Amsalem return iter; 395dfc97661SLior Amsalem 396ff7b0479SSaeed Bishara } 397fbea28a2SLior Amsalem 398fbea28a2SLior Amsalem spin_unlock_bh(&mv_chan->lock); 399ff7b0479SSaeed Bishara 400ff7b0479SSaeed Bishara /* try to free some slots if the allocation fails */ 401ff7b0479SSaeed Bishara tasklet_schedule(&mv_chan->irq_tasklet); 402ff7b0479SSaeed Bishara 403ff7b0479SSaeed Bishara return NULL; 404ff7b0479SSaeed Bishara } 405ff7b0479SSaeed Bishara 406ff7b0479SSaeed Bishara /************************ DMA engine API functions ****************************/ 407ff7b0479SSaeed Bishara static dma_cookie_t 408ff7b0479SSaeed Bishara mv_xor_tx_submit(struct dma_async_tx_descriptor *tx) 409ff7b0479SSaeed Bishara { 410ff7b0479SSaeed Bishara struct mv_xor_desc_slot *sw_desc = to_mv_xor_slot(tx); 411ff7b0479SSaeed Bishara struct mv_xor_chan *mv_chan = to_mv_xor_chan(tx->chan); 412dfc97661SLior Amsalem struct mv_xor_desc_slot *old_chain_tail; 413ff7b0479SSaeed Bishara dma_cookie_t cookie; 414ff7b0479SSaeed Bishara int new_hw_chain = 1; 415ff7b0479SSaeed Bishara 416c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), 417ff7b0479SSaeed Bishara "%s sw_desc %p: async_tx %p\n", 418ff7b0479SSaeed Bishara __func__, sw_desc, &sw_desc->async_tx); 419ff7b0479SSaeed Bishara 420ff7b0479SSaeed Bishara spin_lock_bh(&mv_chan->lock); 421884485e1SRussell King - ARM Linux cookie = dma_cookie_assign(tx); 422ff7b0479SSaeed Bishara 423ff7b0479SSaeed Bishara if (list_empty(&mv_chan->chain)) 424fbea28a2SLior Amsalem list_move_tail(&sw_desc->node, &mv_chan->chain); 425ff7b0479SSaeed Bishara else { 426ff7b0479SSaeed Bishara new_hw_chain = 0; 427ff7b0479SSaeed Bishara 428ff7b0479SSaeed Bishara old_chain_tail = list_entry(mv_chan->chain.prev, 429ff7b0479SSaeed Bishara struct mv_xor_desc_slot, 430fbea28a2SLior Amsalem node); 431fbea28a2SLior Amsalem list_move_tail(&sw_desc->node, &mv_chan->chain); 432ff7b0479SSaeed Bishara 43331fd8f5bSOlof Johansson dev_dbg(mv_chan_to_devp(mv_chan), "Append to last desc %pa\n", 43431fd8f5bSOlof Johansson &old_chain_tail->async_tx.phys); 435ff7b0479SSaeed Bishara 436ff7b0479SSaeed Bishara /* fix up the hardware chain */ 437dfc97661SLior Amsalem mv_desc_set_next_desc(old_chain_tail, sw_desc->async_tx.phys); 438ff7b0479SSaeed Bishara 439ff7b0479SSaeed Bishara /* if the channel is not busy */ 440ff7b0479SSaeed Bishara if (!mv_chan_is_busy(mv_chan)) { 441ff7b0479SSaeed Bishara u32 current_desc = mv_chan_get_current_desc(mv_chan); 442ff7b0479SSaeed Bishara /* 443ff7b0479SSaeed Bishara * and the curren desc is the end of the chain before 444ff7b0479SSaeed Bishara * the append, then we need to start the channel 445ff7b0479SSaeed Bishara */ 446ff7b0479SSaeed Bishara if (current_desc == old_chain_tail->async_tx.phys) 447ff7b0479SSaeed Bishara new_hw_chain = 1; 448ff7b0479SSaeed Bishara } 449ff7b0479SSaeed Bishara } 450ff7b0479SSaeed Bishara 451ff7b0479SSaeed Bishara if (new_hw_chain) 4520951e728SMaxime Ripard mv_chan_start_new_chain(mv_chan, sw_desc); 453ff7b0479SSaeed Bishara 454ff7b0479SSaeed Bishara spin_unlock_bh(&mv_chan->lock); 455ff7b0479SSaeed Bishara 456ff7b0479SSaeed Bishara return cookie; 457ff7b0479SSaeed Bishara } 458ff7b0479SSaeed Bishara 459ff7b0479SSaeed Bishara /* returns the number of allocated descriptors */ 460aa1e6f1aSDan Williams static int mv_xor_alloc_chan_resources(struct dma_chan *chan) 461ff7b0479SSaeed Bishara { 46231fd8f5bSOlof Johansson void *virt_desc; 46331fd8f5bSOlof Johansson dma_addr_t dma_desc; 464ff7b0479SSaeed Bishara int idx; 465ff7b0479SSaeed Bishara struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan); 466ff7b0479SSaeed Bishara struct mv_xor_desc_slot *slot = NULL; 467b503fa01SThomas Petazzoni int num_descs_in_pool = MV_XOR_POOL_SIZE/MV_XOR_SLOT_SIZE; 468ff7b0479SSaeed Bishara 469ff7b0479SSaeed Bishara /* Allocate descriptor slots */ 470ff7b0479SSaeed Bishara idx = mv_chan->slots_allocated; 471ff7b0479SSaeed Bishara while (idx < num_descs_in_pool) { 472ff7b0479SSaeed Bishara slot = kzalloc(sizeof(*slot), GFP_KERNEL); 473ff7b0479SSaeed Bishara if (!slot) { 474b8291ddeSEzequiel Garcia dev_info(mv_chan_to_devp(mv_chan), 475b8291ddeSEzequiel Garcia "channel only initialized %d descriptor slots", 476b8291ddeSEzequiel Garcia idx); 477ff7b0479SSaeed Bishara break; 478ff7b0479SSaeed Bishara } 47931fd8f5bSOlof Johansson virt_desc = mv_chan->dma_desc_pool_virt; 48031fd8f5bSOlof Johansson slot->hw_desc = virt_desc + idx * MV_XOR_SLOT_SIZE; 481ff7b0479SSaeed Bishara 482ff7b0479SSaeed Bishara dma_async_tx_descriptor_init(&slot->async_tx, chan); 483ff7b0479SSaeed Bishara slot->async_tx.tx_submit = mv_xor_tx_submit; 484fbea28a2SLior Amsalem INIT_LIST_HEAD(&slot->node); 48531fd8f5bSOlof Johansson dma_desc = mv_chan->dma_desc_pool; 48631fd8f5bSOlof Johansson slot->async_tx.phys = dma_desc + idx * MV_XOR_SLOT_SIZE; 487ff7b0479SSaeed Bishara slot->idx = idx++; 488ff7b0479SSaeed Bishara 489ff7b0479SSaeed Bishara spin_lock_bh(&mv_chan->lock); 490ff7b0479SSaeed Bishara mv_chan->slots_allocated = idx; 491fbea28a2SLior Amsalem list_add_tail(&slot->node, &mv_chan->free_slots); 492ff7b0479SSaeed Bishara spin_unlock_bh(&mv_chan->lock); 493ff7b0479SSaeed Bishara } 494ff7b0479SSaeed Bishara 495c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), 496fbea28a2SLior Amsalem "allocated %d descriptor slots\n", 497fbea28a2SLior Amsalem mv_chan->slots_allocated); 498ff7b0479SSaeed Bishara 499ff7b0479SSaeed Bishara return mv_chan->slots_allocated ? : -ENOMEM; 500ff7b0479SSaeed Bishara } 501ff7b0479SSaeed Bishara 502ff7b0479SSaeed Bishara static struct dma_async_tx_descriptor * 503ff7b0479SSaeed Bishara mv_xor_prep_dma_xor(struct dma_chan *chan, dma_addr_t dest, dma_addr_t *src, 504ff7b0479SSaeed Bishara unsigned int src_cnt, size_t len, unsigned long flags) 505ff7b0479SSaeed Bishara { 506ff7b0479SSaeed Bishara struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan); 507dfc97661SLior Amsalem struct mv_xor_desc_slot *sw_desc; 508ff7b0479SSaeed Bishara 509ff7b0479SSaeed Bishara if (unlikely(len < MV_XOR_MIN_BYTE_COUNT)) 510ff7b0479SSaeed Bishara return NULL; 511ff7b0479SSaeed Bishara 5127912d300SColy Li BUG_ON(len > MV_XOR_MAX_BYTE_COUNT); 513ff7b0479SSaeed Bishara 514c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), 51531fd8f5bSOlof Johansson "%s src_cnt: %d len: %u dest %pad flags: %ld\n", 51631fd8f5bSOlof Johansson __func__, src_cnt, len, &dest, flags); 517ff7b0479SSaeed Bishara 5180951e728SMaxime Ripard sw_desc = mv_chan_alloc_slot(mv_chan); 519ff7b0479SSaeed Bishara if (sw_desc) { 520ff7b0479SSaeed Bishara sw_desc->type = DMA_XOR; 521ff7b0479SSaeed Bishara sw_desc->async_tx.flags = flags; 522ba87d137SLior Amsalem mv_desc_init(sw_desc, dest, len, flags); 5236f166312SLior Amsalem if (mv_chan->op_in_desc == XOR_MODE_IN_DESC) 5246f166312SLior Amsalem mv_desc_set_mode(sw_desc); 525ff7b0479SSaeed Bishara while (src_cnt--) 526dfc97661SLior Amsalem mv_desc_set_src_addr(sw_desc, src_cnt, src[src_cnt]); 527ff7b0479SSaeed Bishara } 528fbea28a2SLior Amsalem 529c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), 530ff7b0479SSaeed Bishara "%s sw_desc %p async_tx %p \n", 531ff7b0479SSaeed Bishara __func__, sw_desc, &sw_desc->async_tx); 532ff7b0479SSaeed Bishara return sw_desc ? &sw_desc->async_tx : NULL; 533ff7b0479SSaeed Bishara } 534ff7b0479SSaeed Bishara 5353e4f52e2SLior Amsalem static struct dma_async_tx_descriptor * 5363e4f52e2SLior Amsalem mv_xor_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src, 5373e4f52e2SLior Amsalem size_t len, unsigned long flags) 5383e4f52e2SLior Amsalem { 5393e4f52e2SLior Amsalem /* 5403e4f52e2SLior Amsalem * A MEMCPY operation is identical to an XOR operation with only 5413e4f52e2SLior Amsalem * a single source address. 5423e4f52e2SLior Amsalem */ 5433e4f52e2SLior Amsalem return mv_xor_prep_dma_xor(chan, dest, &src, 1, len, flags); 5443e4f52e2SLior Amsalem } 5453e4f52e2SLior Amsalem 54622843545SLior Amsalem static struct dma_async_tx_descriptor * 54722843545SLior Amsalem mv_xor_prep_dma_interrupt(struct dma_chan *chan, unsigned long flags) 54822843545SLior Amsalem { 54922843545SLior Amsalem struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan); 55022843545SLior Amsalem dma_addr_t src, dest; 55122843545SLior Amsalem size_t len; 55222843545SLior Amsalem 55322843545SLior Amsalem src = mv_chan->dummy_src_addr; 55422843545SLior Amsalem dest = mv_chan->dummy_dst_addr; 55522843545SLior Amsalem len = MV_XOR_MIN_BYTE_COUNT; 55622843545SLior Amsalem 55722843545SLior Amsalem /* 55822843545SLior Amsalem * We implement the DMA_INTERRUPT operation as a minimum sized 55922843545SLior Amsalem * XOR operation with a single dummy source address. 56022843545SLior Amsalem */ 56122843545SLior Amsalem return mv_xor_prep_dma_xor(chan, dest, &src, 1, len, flags); 56222843545SLior Amsalem } 56322843545SLior Amsalem 564ff7b0479SSaeed Bishara static void mv_xor_free_chan_resources(struct dma_chan *chan) 565ff7b0479SSaeed Bishara { 566ff7b0479SSaeed Bishara struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan); 567ff7b0479SSaeed Bishara struct mv_xor_desc_slot *iter, *_iter; 568ff7b0479SSaeed Bishara int in_use_descs = 0; 569ff7b0479SSaeed Bishara 570ff7b0479SSaeed Bishara spin_lock_bh(&mv_chan->lock); 571e43147acSEzequiel Garcia 5720951e728SMaxime Ripard mv_chan_slot_cleanup(mv_chan); 573ff7b0479SSaeed Bishara 574ff7b0479SSaeed Bishara list_for_each_entry_safe(iter, _iter, &mv_chan->chain, 575fbea28a2SLior Amsalem node) { 576ff7b0479SSaeed Bishara in_use_descs++; 577fbea28a2SLior Amsalem list_move_tail(&iter->node, &mv_chan->free_slots); 578ff7b0479SSaeed Bishara } 579ff7b0479SSaeed Bishara list_for_each_entry_safe(iter, _iter, &mv_chan->completed_slots, 580fbea28a2SLior Amsalem node) { 581ff7b0479SSaeed Bishara in_use_descs++; 582fbea28a2SLior Amsalem list_move_tail(&iter->node, &mv_chan->free_slots); 583fbea28a2SLior Amsalem } 584fbea28a2SLior Amsalem list_for_each_entry_safe(iter, _iter, &mv_chan->allocated_slots, 585fbea28a2SLior Amsalem node) { 586fbea28a2SLior Amsalem in_use_descs++; 587fbea28a2SLior Amsalem list_move_tail(&iter->node, &mv_chan->free_slots); 588ff7b0479SSaeed Bishara } 589ff7b0479SSaeed Bishara list_for_each_entry_safe_reverse( 590fbea28a2SLior Amsalem iter, _iter, &mv_chan->free_slots, node) { 591fbea28a2SLior Amsalem list_del(&iter->node); 592ff7b0479SSaeed Bishara kfree(iter); 593ff7b0479SSaeed Bishara mv_chan->slots_allocated--; 594ff7b0479SSaeed Bishara } 595ff7b0479SSaeed Bishara 596c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), "%s slots_allocated %d\n", 597ff7b0479SSaeed Bishara __func__, mv_chan->slots_allocated); 598ff7b0479SSaeed Bishara spin_unlock_bh(&mv_chan->lock); 599ff7b0479SSaeed Bishara 600ff7b0479SSaeed Bishara if (in_use_descs) 601c98c1781SThomas Petazzoni dev_err(mv_chan_to_devp(mv_chan), 602ff7b0479SSaeed Bishara "freeing %d in use descriptors!\n", in_use_descs); 603ff7b0479SSaeed Bishara } 604ff7b0479SSaeed Bishara 605ff7b0479SSaeed Bishara /** 60607934481SLinus Walleij * mv_xor_status - poll the status of an XOR transaction 607ff7b0479SSaeed Bishara * @chan: XOR channel handle 608ff7b0479SSaeed Bishara * @cookie: XOR transaction identifier 60907934481SLinus Walleij * @txstate: XOR transactions state holder (or NULL) 610ff7b0479SSaeed Bishara */ 61107934481SLinus Walleij static enum dma_status mv_xor_status(struct dma_chan *chan, 612ff7b0479SSaeed Bishara dma_cookie_t cookie, 61307934481SLinus Walleij struct dma_tx_state *txstate) 614ff7b0479SSaeed Bishara { 615ff7b0479SSaeed Bishara struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan); 616ff7b0479SSaeed Bishara enum dma_status ret; 617ff7b0479SSaeed Bishara 61896a2af41SRussell King - ARM Linux ret = dma_cookie_status(chan, cookie, txstate); 619890766d2SEzequiel Garcia if (ret == DMA_COMPLETE) 620ff7b0479SSaeed Bishara return ret; 621e43147acSEzequiel Garcia 622e43147acSEzequiel Garcia spin_lock_bh(&mv_chan->lock); 6230951e728SMaxime Ripard mv_chan_slot_cleanup(mv_chan); 624e43147acSEzequiel Garcia spin_unlock_bh(&mv_chan->lock); 625ff7b0479SSaeed Bishara 62696a2af41SRussell King - ARM Linux return dma_cookie_status(chan, cookie, txstate); 627ff7b0479SSaeed Bishara } 628ff7b0479SSaeed Bishara 6290951e728SMaxime Ripard static void mv_chan_dump_regs(struct mv_xor_chan *chan) 630ff7b0479SSaeed Bishara { 631ff7b0479SSaeed Bishara u32 val; 632ff7b0479SSaeed Bishara 6335733c38aSThomas Petazzoni val = readl_relaxed(XOR_CONFIG(chan)); 6341ba151cdSJoe Perches dev_err(mv_chan_to_devp(chan), "config 0x%08x\n", val); 635ff7b0479SSaeed Bishara 6365733c38aSThomas Petazzoni val = readl_relaxed(XOR_ACTIVATION(chan)); 6371ba151cdSJoe Perches dev_err(mv_chan_to_devp(chan), "activation 0x%08x\n", val); 638ff7b0479SSaeed Bishara 6395733c38aSThomas Petazzoni val = readl_relaxed(XOR_INTR_CAUSE(chan)); 6401ba151cdSJoe Perches dev_err(mv_chan_to_devp(chan), "intr cause 0x%08x\n", val); 641ff7b0479SSaeed Bishara 6425733c38aSThomas Petazzoni val = readl_relaxed(XOR_INTR_MASK(chan)); 6431ba151cdSJoe Perches dev_err(mv_chan_to_devp(chan), "intr mask 0x%08x\n", val); 644ff7b0479SSaeed Bishara 6455733c38aSThomas Petazzoni val = readl_relaxed(XOR_ERROR_CAUSE(chan)); 6461ba151cdSJoe Perches dev_err(mv_chan_to_devp(chan), "error cause 0x%08x\n", val); 647ff7b0479SSaeed Bishara 6485733c38aSThomas Petazzoni val = readl_relaxed(XOR_ERROR_ADDR(chan)); 6491ba151cdSJoe Perches dev_err(mv_chan_to_devp(chan), "error addr 0x%08x\n", val); 650ff7b0479SSaeed Bishara } 651ff7b0479SSaeed Bishara 6520951e728SMaxime Ripard static void mv_chan_err_interrupt_handler(struct mv_xor_chan *chan, 653ff7b0479SSaeed Bishara u32 intr_cause) 654ff7b0479SSaeed Bishara { 6550e7488edSEzequiel Garcia if (intr_cause & XOR_INT_ERR_DECODE) { 6560e7488edSEzequiel Garcia dev_dbg(mv_chan_to_devp(chan), "ignoring address decode error\n"); 657ff7b0479SSaeed Bishara return; 658ff7b0479SSaeed Bishara } 659ff7b0479SSaeed Bishara 6600e7488edSEzequiel Garcia dev_err(mv_chan_to_devp(chan), "error on chan %d. intr cause 0x%08x\n", 661ff7b0479SSaeed Bishara chan->idx, intr_cause); 662ff7b0479SSaeed Bishara 6630951e728SMaxime Ripard mv_chan_dump_regs(chan); 6640e7488edSEzequiel Garcia WARN_ON(1); 665ff7b0479SSaeed Bishara } 666ff7b0479SSaeed Bishara 667ff7b0479SSaeed Bishara static irqreturn_t mv_xor_interrupt_handler(int irq, void *data) 668ff7b0479SSaeed Bishara { 669ff7b0479SSaeed Bishara struct mv_xor_chan *chan = data; 670ff7b0479SSaeed Bishara u32 intr_cause = mv_chan_get_intr_cause(chan); 671ff7b0479SSaeed Bishara 672c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(chan), "intr cause %x\n", intr_cause); 673ff7b0479SSaeed Bishara 6740e7488edSEzequiel Garcia if (intr_cause & XOR_INTR_ERRORS) 6750951e728SMaxime Ripard mv_chan_err_interrupt_handler(chan, intr_cause); 676ff7b0479SSaeed Bishara 677ff7b0479SSaeed Bishara tasklet_schedule(&chan->irq_tasklet); 678ff7b0479SSaeed Bishara 6790951e728SMaxime Ripard mv_chan_clear_eoc_cause(chan); 680ff7b0479SSaeed Bishara 681ff7b0479SSaeed Bishara return IRQ_HANDLED; 682ff7b0479SSaeed Bishara } 683ff7b0479SSaeed Bishara 684ff7b0479SSaeed Bishara static void mv_xor_issue_pending(struct dma_chan *chan) 685ff7b0479SSaeed Bishara { 686ff7b0479SSaeed Bishara struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan); 687ff7b0479SSaeed Bishara 688ff7b0479SSaeed Bishara if (mv_chan->pending >= MV_XOR_THRESHOLD) { 689ff7b0479SSaeed Bishara mv_chan->pending = 0; 690ff7b0479SSaeed Bishara mv_chan_activate(mv_chan); 691ff7b0479SSaeed Bishara } 692ff7b0479SSaeed Bishara } 693ff7b0479SSaeed Bishara 694ff7b0479SSaeed Bishara /* 695ff7b0479SSaeed Bishara * Perform a transaction to verify the HW works. 696ff7b0479SSaeed Bishara */ 697ff7b0479SSaeed Bishara 6980951e728SMaxime Ripard static int mv_chan_memcpy_self_test(struct mv_xor_chan *mv_chan) 699ff7b0479SSaeed Bishara { 700b8c01d25SEzequiel Garcia int i, ret; 701ff7b0479SSaeed Bishara void *src, *dest; 702ff7b0479SSaeed Bishara dma_addr_t src_dma, dest_dma; 703ff7b0479SSaeed Bishara struct dma_chan *dma_chan; 704ff7b0479SSaeed Bishara dma_cookie_t cookie; 705ff7b0479SSaeed Bishara struct dma_async_tx_descriptor *tx; 706d16695a7SEzequiel Garcia struct dmaengine_unmap_data *unmap; 707ff7b0479SSaeed Bishara int err = 0; 708ff7b0479SSaeed Bishara 709d16695a7SEzequiel Garcia src = kmalloc(sizeof(u8) * PAGE_SIZE, GFP_KERNEL); 710ff7b0479SSaeed Bishara if (!src) 711ff7b0479SSaeed Bishara return -ENOMEM; 712ff7b0479SSaeed Bishara 713d16695a7SEzequiel Garcia dest = kzalloc(sizeof(u8) * PAGE_SIZE, GFP_KERNEL); 714ff7b0479SSaeed Bishara if (!dest) { 715ff7b0479SSaeed Bishara kfree(src); 716ff7b0479SSaeed Bishara return -ENOMEM; 717ff7b0479SSaeed Bishara } 718ff7b0479SSaeed Bishara 719ff7b0479SSaeed Bishara /* Fill in src buffer */ 720d16695a7SEzequiel Garcia for (i = 0; i < PAGE_SIZE; i++) 721ff7b0479SSaeed Bishara ((u8 *) src)[i] = (u8)i; 722ff7b0479SSaeed Bishara 723275cc0c8SThomas Petazzoni dma_chan = &mv_chan->dmachan; 724aa1e6f1aSDan Williams if (mv_xor_alloc_chan_resources(dma_chan) < 1) { 725ff7b0479SSaeed Bishara err = -ENODEV; 726ff7b0479SSaeed Bishara goto out; 727ff7b0479SSaeed Bishara } 728ff7b0479SSaeed Bishara 729d16695a7SEzequiel Garcia unmap = dmaengine_get_unmap_data(dma_chan->device->dev, 2, GFP_KERNEL); 730d16695a7SEzequiel Garcia if (!unmap) { 731d16695a7SEzequiel Garcia err = -ENOMEM; 732d16695a7SEzequiel Garcia goto free_resources; 733d16695a7SEzequiel Garcia } 734ff7b0479SSaeed Bishara 735d16695a7SEzequiel Garcia src_dma = dma_map_page(dma_chan->device->dev, virt_to_page(src), 0, 736d16695a7SEzequiel Garcia PAGE_SIZE, DMA_TO_DEVICE); 737d16695a7SEzequiel Garcia unmap->addr[0] = src_dma; 738d16695a7SEzequiel Garcia 739b8c01d25SEzequiel Garcia ret = dma_mapping_error(dma_chan->device->dev, src_dma); 740b8c01d25SEzequiel Garcia if (ret) { 741b8c01d25SEzequiel Garcia err = -ENOMEM; 742b8c01d25SEzequiel Garcia goto free_resources; 743b8c01d25SEzequiel Garcia } 744b8c01d25SEzequiel Garcia unmap->to_cnt = 1; 745b8c01d25SEzequiel Garcia 746d16695a7SEzequiel Garcia dest_dma = dma_map_page(dma_chan->device->dev, virt_to_page(dest), 0, 747d16695a7SEzequiel Garcia PAGE_SIZE, DMA_FROM_DEVICE); 748d16695a7SEzequiel Garcia unmap->addr[1] = dest_dma; 749d16695a7SEzequiel Garcia 750b8c01d25SEzequiel Garcia ret = dma_mapping_error(dma_chan->device->dev, dest_dma); 751b8c01d25SEzequiel Garcia if (ret) { 752b8c01d25SEzequiel Garcia err = -ENOMEM; 753b8c01d25SEzequiel Garcia goto free_resources; 754b8c01d25SEzequiel Garcia } 755b8c01d25SEzequiel Garcia unmap->from_cnt = 1; 756d16695a7SEzequiel Garcia unmap->len = PAGE_SIZE; 757ff7b0479SSaeed Bishara 758ff7b0479SSaeed Bishara tx = mv_xor_prep_dma_memcpy(dma_chan, dest_dma, src_dma, 759d16695a7SEzequiel Garcia PAGE_SIZE, 0); 760b8c01d25SEzequiel Garcia if (!tx) { 761b8c01d25SEzequiel Garcia dev_err(dma_chan->device->dev, 762b8c01d25SEzequiel Garcia "Self-test cannot prepare operation, disabling\n"); 763b8c01d25SEzequiel Garcia err = -ENODEV; 764b8c01d25SEzequiel Garcia goto free_resources; 765b8c01d25SEzequiel Garcia } 766b8c01d25SEzequiel Garcia 767ff7b0479SSaeed Bishara cookie = mv_xor_tx_submit(tx); 768b8c01d25SEzequiel Garcia if (dma_submit_error(cookie)) { 769b8c01d25SEzequiel Garcia dev_err(dma_chan->device->dev, 770b8c01d25SEzequiel Garcia "Self-test submit error, disabling\n"); 771b8c01d25SEzequiel Garcia err = -ENODEV; 772b8c01d25SEzequiel Garcia goto free_resources; 773b8c01d25SEzequiel Garcia } 774b8c01d25SEzequiel Garcia 775ff7b0479SSaeed Bishara mv_xor_issue_pending(dma_chan); 776ff7b0479SSaeed Bishara async_tx_ack(tx); 777ff7b0479SSaeed Bishara msleep(1); 778ff7b0479SSaeed Bishara 77907934481SLinus Walleij if (mv_xor_status(dma_chan, cookie, NULL) != 780b3efb8fcSVinod Koul DMA_COMPLETE) { 781a3fc74bcSThomas Petazzoni dev_err(dma_chan->device->dev, 782ff7b0479SSaeed Bishara "Self-test copy timed out, disabling\n"); 783ff7b0479SSaeed Bishara err = -ENODEV; 784ff7b0479SSaeed Bishara goto free_resources; 785ff7b0479SSaeed Bishara } 786ff7b0479SSaeed Bishara 787c35064c4SThomas Petazzoni dma_sync_single_for_cpu(dma_chan->device->dev, dest_dma, 788d16695a7SEzequiel Garcia PAGE_SIZE, DMA_FROM_DEVICE); 789d16695a7SEzequiel Garcia if (memcmp(src, dest, PAGE_SIZE)) { 790a3fc74bcSThomas Petazzoni dev_err(dma_chan->device->dev, 791ff7b0479SSaeed Bishara "Self-test copy failed compare, disabling\n"); 792ff7b0479SSaeed Bishara err = -ENODEV; 793ff7b0479SSaeed Bishara goto free_resources; 794ff7b0479SSaeed Bishara } 795ff7b0479SSaeed Bishara 796ff7b0479SSaeed Bishara free_resources: 797d16695a7SEzequiel Garcia dmaengine_unmap_put(unmap); 798ff7b0479SSaeed Bishara mv_xor_free_chan_resources(dma_chan); 799ff7b0479SSaeed Bishara out: 800ff7b0479SSaeed Bishara kfree(src); 801ff7b0479SSaeed Bishara kfree(dest); 802ff7b0479SSaeed Bishara return err; 803ff7b0479SSaeed Bishara } 804ff7b0479SSaeed Bishara 805ff7b0479SSaeed Bishara #define MV_XOR_NUM_SRC_TEST 4 /* must be <= 15 */ 806463a1f8bSBill Pemberton static int 8070951e728SMaxime Ripard mv_chan_xor_self_test(struct mv_xor_chan *mv_chan) 808ff7b0479SSaeed Bishara { 809b8c01d25SEzequiel Garcia int i, src_idx, ret; 810ff7b0479SSaeed Bishara struct page *dest; 811ff7b0479SSaeed Bishara struct page *xor_srcs[MV_XOR_NUM_SRC_TEST]; 812ff7b0479SSaeed Bishara dma_addr_t dma_srcs[MV_XOR_NUM_SRC_TEST]; 813ff7b0479SSaeed Bishara dma_addr_t dest_dma; 814ff7b0479SSaeed Bishara struct dma_async_tx_descriptor *tx; 815d16695a7SEzequiel Garcia struct dmaengine_unmap_data *unmap; 816ff7b0479SSaeed Bishara struct dma_chan *dma_chan; 817ff7b0479SSaeed Bishara dma_cookie_t cookie; 818ff7b0479SSaeed Bishara u8 cmp_byte = 0; 819ff7b0479SSaeed Bishara u32 cmp_word; 820ff7b0479SSaeed Bishara int err = 0; 821d16695a7SEzequiel Garcia int src_count = MV_XOR_NUM_SRC_TEST; 822ff7b0479SSaeed Bishara 823d16695a7SEzequiel Garcia for (src_idx = 0; src_idx < src_count; src_idx++) { 824ff7b0479SSaeed Bishara xor_srcs[src_idx] = alloc_page(GFP_KERNEL); 825a09b09aeSRoel Kluin if (!xor_srcs[src_idx]) { 826a09b09aeSRoel Kluin while (src_idx--) 827ff7b0479SSaeed Bishara __free_page(xor_srcs[src_idx]); 828ff7b0479SSaeed Bishara return -ENOMEM; 829ff7b0479SSaeed Bishara } 830ff7b0479SSaeed Bishara } 831ff7b0479SSaeed Bishara 832ff7b0479SSaeed Bishara dest = alloc_page(GFP_KERNEL); 833a09b09aeSRoel Kluin if (!dest) { 834a09b09aeSRoel Kluin while (src_idx--) 835ff7b0479SSaeed Bishara __free_page(xor_srcs[src_idx]); 836ff7b0479SSaeed Bishara return -ENOMEM; 837ff7b0479SSaeed Bishara } 838ff7b0479SSaeed Bishara 839ff7b0479SSaeed Bishara /* Fill in src buffers */ 840d16695a7SEzequiel Garcia for (src_idx = 0; src_idx < src_count; src_idx++) { 841ff7b0479SSaeed Bishara u8 *ptr = page_address(xor_srcs[src_idx]); 842ff7b0479SSaeed Bishara for (i = 0; i < PAGE_SIZE; i++) 843ff7b0479SSaeed Bishara ptr[i] = (1 << src_idx); 844ff7b0479SSaeed Bishara } 845ff7b0479SSaeed Bishara 846d16695a7SEzequiel Garcia for (src_idx = 0; src_idx < src_count; src_idx++) 847ff7b0479SSaeed Bishara cmp_byte ^= (u8) (1 << src_idx); 848ff7b0479SSaeed Bishara 849ff7b0479SSaeed Bishara cmp_word = (cmp_byte << 24) | (cmp_byte << 16) | 850ff7b0479SSaeed Bishara (cmp_byte << 8) | cmp_byte; 851ff7b0479SSaeed Bishara 852ff7b0479SSaeed Bishara memset(page_address(dest), 0, PAGE_SIZE); 853ff7b0479SSaeed Bishara 854275cc0c8SThomas Petazzoni dma_chan = &mv_chan->dmachan; 855aa1e6f1aSDan Williams if (mv_xor_alloc_chan_resources(dma_chan) < 1) { 856ff7b0479SSaeed Bishara err = -ENODEV; 857ff7b0479SSaeed Bishara goto out; 858ff7b0479SSaeed Bishara } 859ff7b0479SSaeed Bishara 860d16695a7SEzequiel Garcia unmap = dmaengine_get_unmap_data(dma_chan->device->dev, src_count + 1, 861d16695a7SEzequiel Garcia GFP_KERNEL); 862d16695a7SEzequiel Garcia if (!unmap) { 863d16695a7SEzequiel Garcia err = -ENOMEM; 864d16695a7SEzequiel Garcia goto free_resources; 865d16695a7SEzequiel Garcia } 866ff7b0479SSaeed Bishara 867d16695a7SEzequiel Garcia /* test xor */ 868d16695a7SEzequiel Garcia for (i = 0; i < src_count; i++) { 869d16695a7SEzequiel Garcia unmap->addr[i] = dma_map_page(dma_chan->device->dev, xor_srcs[i], 870ff7b0479SSaeed Bishara 0, PAGE_SIZE, DMA_TO_DEVICE); 871d16695a7SEzequiel Garcia dma_srcs[i] = unmap->addr[i]; 872b8c01d25SEzequiel Garcia ret = dma_mapping_error(dma_chan->device->dev, unmap->addr[i]); 873b8c01d25SEzequiel Garcia if (ret) { 874b8c01d25SEzequiel Garcia err = -ENOMEM; 875b8c01d25SEzequiel Garcia goto free_resources; 876b8c01d25SEzequiel Garcia } 877d16695a7SEzequiel Garcia unmap->to_cnt++; 878d16695a7SEzequiel Garcia } 879d16695a7SEzequiel Garcia 880d16695a7SEzequiel Garcia unmap->addr[src_count] = dma_map_page(dma_chan->device->dev, dest, 0, PAGE_SIZE, 881d16695a7SEzequiel Garcia DMA_FROM_DEVICE); 882d16695a7SEzequiel Garcia dest_dma = unmap->addr[src_count]; 883b8c01d25SEzequiel Garcia ret = dma_mapping_error(dma_chan->device->dev, unmap->addr[src_count]); 884b8c01d25SEzequiel Garcia if (ret) { 885b8c01d25SEzequiel Garcia err = -ENOMEM; 886b8c01d25SEzequiel Garcia goto free_resources; 887b8c01d25SEzequiel Garcia } 888d16695a7SEzequiel Garcia unmap->from_cnt = 1; 889d16695a7SEzequiel Garcia unmap->len = PAGE_SIZE; 890ff7b0479SSaeed Bishara 891ff7b0479SSaeed Bishara tx = mv_xor_prep_dma_xor(dma_chan, dest_dma, dma_srcs, 892d16695a7SEzequiel Garcia src_count, PAGE_SIZE, 0); 893b8c01d25SEzequiel Garcia if (!tx) { 894b8c01d25SEzequiel Garcia dev_err(dma_chan->device->dev, 895b8c01d25SEzequiel Garcia "Self-test cannot prepare operation, disabling\n"); 896b8c01d25SEzequiel Garcia err = -ENODEV; 897b8c01d25SEzequiel Garcia goto free_resources; 898b8c01d25SEzequiel Garcia } 899ff7b0479SSaeed Bishara 900ff7b0479SSaeed Bishara cookie = mv_xor_tx_submit(tx); 901b8c01d25SEzequiel Garcia if (dma_submit_error(cookie)) { 902b8c01d25SEzequiel Garcia dev_err(dma_chan->device->dev, 903b8c01d25SEzequiel Garcia "Self-test submit error, disabling\n"); 904b8c01d25SEzequiel Garcia err = -ENODEV; 905b8c01d25SEzequiel Garcia goto free_resources; 906b8c01d25SEzequiel Garcia } 907b8c01d25SEzequiel Garcia 908ff7b0479SSaeed Bishara mv_xor_issue_pending(dma_chan); 909ff7b0479SSaeed Bishara async_tx_ack(tx); 910ff7b0479SSaeed Bishara msleep(8); 911ff7b0479SSaeed Bishara 91207934481SLinus Walleij if (mv_xor_status(dma_chan, cookie, NULL) != 913b3efb8fcSVinod Koul DMA_COMPLETE) { 914a3fc74bcSThomas Petazzoni dev_err(dma_chan->device->dev, 915ff7b0479SSaeed Bishara "Self-test xor timed out, disabling\n"); 916ff7b0479SSaeed Bishara err = -ENODEV; 917ff7b0479SSaeed Bishara goto free_resources; 918ff7b0479SSaeed Bishara } 919ff7b0479SSaeed Bishara 920c35064c4SThomas Petazzoni dma_sync_single_for_cpu(dma_chan->device->dev, dest_dma, 921ff7b0479SSaeed Bishara PAGE_SIZE, DMA_FROM_DEVICE); 922ff7b0479SSaeed Bishara for (i = 0; i < (PAGE_SIZE / sizeof(u32)); i++) { 923ff7b0479SSaeed Bishara u32 *ptr = page_address(dest); 924ff7b0479SSaeed Bishara if (ptr[i] != cmp_word) { 925a3fc74bcSThomas Petazzoni dev_err(dma_chan->device->dev, 9261ba151cdSJoe Perches "Self-test xor failed compare, disabling. index %d, data %x, expected %x\n", 9271ba151cdSJoe Perches i, ptr[i], cmp_word); 928ff7b0479SSaeed Bishara err = -ENODEV; 929ff7b0479SSaeed Bishara goto free_resources; 930ff7b0479SSaeed Bishara } 931ff7b0479SSaeed Bishara } 932ff7b0479SSaeed Bishara 933ff7b0479SSaeed Bishara free_resources: 934d16695a7SEzequiel Garcia dmaengine_unmap_put(unmap); 935ff7b0479SSaeed Bishara mv_xor_free_chan_resources(dma_chan); 936ff7b0479SSaeed Bishara out: 937d16695a7SEzequiel Garcia src_idx = src_count; 938ff7b0479SSaeed Bishara while (src_idx--) 939ff7b0479SSaeed Bishara __free_page(xor_srcs[src_idx]); 940ff7b0479SSaeed Bishara __free_page(dest); 941ff7b0479SSaeed Bishara return err; 942ff7b0479SSaeed Bishara } 943ff7b0479SSaeed Bishara 9441ef48a26SThomas Petazzoni static int mv_xor_channel_remove(struct mv_xor_chan *mv_chan) 945ff7b0479SSaeed Bishara { 946ff7b0479SSaeed Bishara struct dma_chan *chan, *_chan; 9471ef48a26SThomas Petazzoni struct device *dev = mv_chan->dmadev.dev; 948ff7b0479SSaeed Bishara 9491ef48a26SThomas Petazzoni dma_async_device_unregister(&mv_chan->dmadev); 950ff7b0479SSaeed Bishara 951b503fa01SThomas Petazzoni dma_free_coherent(dev, MV_XOR_POOL_SIZE, 9521ef48a26SThomas Petazzoni mv_chan->dma_desc_pool_virt, mv_chan->dma_desc_pool); 95322843545SLior Amsalem dma_unmap_single(dev, mv_chan->dummy_src_addr, 95422843545SLior Amsalem MV_XOR_MIN_BYTE_COUNT, DMA_FROM_DEVICE); 95522843545SLior Amsalem dma_unmap_single(dev, mv_chan->dummy_dst_addr, 95622843545SLior Amsalem MV_XOR_MIN_BYTE_COUNT, DMA_TO_DEVICE); 957ff7b0479SSaeed Bishara 9581ef48a26SThomas Petazzoni list_for_each_entry_safe(chan, _chan, &mv_chan->dmadev.channels, 959ff7b0479SSaeed Bishara device_node) { 960ff7b0479SSaeed Bishara list_del(&chan->device_node); 961ff7b0479SSaeed Bishara } 962ff7b0479SSaeed Bishara 96388eb92cbSThomas Petazzoni free_irq(mv_chan->irq, mv_chan); 96488eb92cbSThomas Petazzoni 965ff7b0479SSaeed Bishara return 0; 966ff7b0479SSaeed Bishara } 967ff7b0479SSaeed Bishara 9681ef48a26SThomas Petazzoni static struct mv_xor_chan * 969297eedbaSThomas Petazzoni mv_xor_channel_add(struct mv_xor_device *xordev, 970a6b4a9d2SThomas Petazzoni struct platform_device *pdev, 9716f166312SLior Amsalem int idx, dma_cap_mask_t cap_mask, int irq, int op_in_desc) 972ff7b0479SSaeed Bishara { 973ff7b0479SSaeed Bishara int ret = 0; 974ff7b0479SSaeed Bishara struct mv_xor_chan *mv_chan; 975ff7b0479SSaeed Bishara struct dma_device *dma_dev; 976ff7b0479SSaeed Bishara 9771ef48a26SThomas Petazzoni mv_chan = devm_kzalloc(&pdev->dev, sizeof(*mv_chan), GFP_KERNEL); 978a577659fSSachin Kamat if (!mv_chan) 979a577659fSSachin Kamat return ERR_PTR(-ENOMEM); 980ff7b0479SSaeed Bishara 9819aedbdbaSThomas Petazzoni mv_chan->idx = idx; 98288eb92cbSThomas Petazzoni mv_chan->irq = irq; 9836f166312SLior Amsalem mv_chan->op_in_desc = op_in_desc; 984ff7b0479SSaeed Bishara 9851ef48a26SThomas Petazzoni dma_dev = &mv_chan->dmadev; 986ff7b0479SSaeed Bishara 98722843545SLior Amsalem /* 98822843545SLior Amsalem * These source and destination dummy buffers are used to implement 98922843545SLior Amsalem * a DMA_INTERRUPT operation as a minimum-sized XOR operation. 99022843545SLior Amsalem * Hence, we only need to map the buffers at initialization-time. 99122843545SLior Amsalem */ 99222843545SLior Amsalem mv_chan->dummy_src_addr = dma_map_single(dma_dev->dev, 99322843545SLior Amsalem mv_chan->dummy_src, MV_XOR_MIN_BYTE_COUNT, DMA_FROM_DEVICE); 99422843545SLior Amsalem mv_chan->dummy_dst_addr = dma_map_single(dma_dev->dev, 99522843545SLior Amsalem mv_chan->dummy_dst, MV_XOR_MIN_BYTE_COUNT, DMA_TO_DEVICE); 99622843545SLior Amsalem 997ff7b0479SSaeed Bishara /* allocate coherent memory for hardware descriptors 998ff7b0479SSaeed Bishara * note: writecombine gives slightly better performance, but 999ff7b0479SSaeed Bishara * requires that we explicitly flush the writes 1000ff7b0479SSaeed Bishara */ 10011ef48a26SThomas Petazzoni mv_chan->dma_desc_pool_virt = 1002b503fa01SThomas Petazzoni dma_alloc_writecombine(&pdev->dev, MV_XOR_POOL_SIZE, 10031ef48a26SThomas Petazzoni &mv_chan->dma_desc_pool, GFP_KERNEL); 10041ef48a26SThomas Petazzoni if (!mv_chan->dma_desc_pool_virt) 1005a6b4a9d2SThomas Petazzoni return ERR_PTR(-ENOMEM); 1006ff7b0479SSaeed Bishara 1007ff7b0479SSaeed Bishara /* discover transaction capabilites from the platform data */ 1008a6b4a9d2SThomas Petazzoni dma_dev->cap_mask = cap_mask; 1009ff7b0479SSaeed Bishara 1010ff7b0479SSaeed Bishara INIT_LIST_HEAD(&dma_dev->channels); 1011ff7b0479SSaeed Bishara 1012ff7b0479SSaeed Bishara /* set base routines */ 1013ff7b0479SSaeed Bishara dma_dev->device_alloc_chan_resources = mv_xor_alloc_chan_resources; 1014ff7b0479SSaeed Bishara dma_dev->device_free_chan_resources = mv_xor_free_chan_resources; 101507934481SLinus Walleij dma_dev->device_tx_status = mv_xor_status; 1016ff7b0479SSaeed Bishara dma_dev->device_issue_pending = mv_xor_issue_pending; 1017ff7b0479SSaeed Bishara dma_dev->dev = &pdev->dev; 1018ff7b0479SSaeed Bishara 1019ff7b0479SSaeed Bishara /* set prep routines based on capability */ 102022843545SLior Amsalem if (dma_has_cap(DMA_INTERRUPT, dma_dev->cap_mask)) 102122843545SLior Amsalem dma_dev->device_prep_dma_interrupt = mv_xor_prep_dma_interrupt; 1022ff7b0479SSaeed Bishara if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask)) 1023ff7b0479SSaeed Bishara dma_dev->device_prep_dma_memcpy = mv_xor_prep_dma_memcpy; 1024ff7b0479SSaeed Bishara if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) { 1025c019894eSJoe Perches dma_dev->max_xor = 8; 1026ff7b0479SSaeed Bishara dma_dev->device_prep_dma_xor = mv_xor_prep_dma_xor; 1027ff7b0479SSaeed Bishara } 1028ff7b0479SSaeed Bishara 1029297eedbaSThomas Petazzoni mv_chan->mmr_base = xordev->xor_base; 103082a1402eSEzequiel Garcia mv_chan->mmr_high_base = xordev->xor_high_base; 1031ff7b0479SSaeed Bishara tasklet_init(&mv_chan->irq_tasklet, mv_xor_tasklet, (unsigned long) 1032ff7b0479SSaeed Bishara mv_chan); 1033ff7b0479SSaeed Bishara 1034ff7b0479SSaeed Bishara /* clear errors before enabling interrupts */ 10350951e728SMaxime Ripard mv_chan_clear_err_status(mv_chan); 1036ff7b0479SSaeed Bishara 10372d0a0745SThomas Petazzoni ret = request_irq(mv_chan->irq, mv_xor_interrupt_handler, 1038ff7b0479SSaeed Bishara 0, dev_name(&pdev->dev), mv_chan); 1039ff7b0479SSaeed Bishara if (ret) 1040ff7b0479SSaeed Bishara goto err_free_dma; 1041ff7b0479SSaeed Bishara 1042ff7b0479SSaeed Bishara mv_chan_unmask_interrupts(mv_chan); 1043ff7b0479SSaeed Bishara 10446f166312SLior Amsalem if (mv_chan->op_in_desc == XOR_MODE_IN_DESC) 10456f166312SLior Amsalem mv_chan_set_mode_to_desc(mv_chan); 10466f166312SLior Amsalem else 10470951e728SMaxime Ripard mv_chan_set_mode(mv_chan, DMA_XOR); 1048ff7b0479SSaeed Bishara 1049ff7b0479SSaeed Bishara spin_lock_init(&mv_chan->lock); 1050ff7b0479SSaeed Bishara INIT_LIST_HEAD(&mv_chan->chain); 1051ff7b0479SSaeed Bishara INIT_LIST_HEAD(&mv_chan->completed_slots); 1052fbea28a2SLior Amsalem INIT_LIST_HEAD(&mv_chan->free_slots); 1053fbea28a2SLior Amsalem INIT_LIST_HEAD(&mv_chan->allocated_slots); 105498817b99SThomas Petazzoni mv_chan->dmachan.device = dma_dev; 105598817b99SThomas Petazzoni dma_cookie_init(&mv_chan->dmachan); 1056ff7b0479SSaeed Bishara 105798817b99SThomas Petazzoni list_add_tail(&mv_chan->dmachan.device_node, &dma_dev->channels); 1058ff7b0479SSaeed Bishara 1059ff7b0479SSaeed Bishara if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask)) { 10600951e728SMaxime Ripard ret = mv_chan_memcpy_self_test(mv_chan); 1061ff7b0479SSaeed Bishara dev_dbg(&pdev->dev, "memcpy self test returned %d\n", ret); 1062ff7b0479SSaeed Bishara if (ret) 10632d0a0745SThomas Petazzoni goto err_free_irq; 1064ff7b0479SSaeed Bishara } 1065ff7b0479SSaeed Bishara 1066ff7b0479SSaeed Bishara if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) { 10670951e728SMaxime Ripard ret = mv_chan_xor_self_test(mv_chan); 1068ff7b0479SSaeed Bishara dev_dbg(&pdev->dev, "xor self test returned %d\n", ret); 1069ff7b0479SSaeed Bishara if (ret) 10702d0a0745SThomas Petazzoni goto err_free_irq; 1071ff7b0479SSaeed Bishara } 1072ff7b0479SSaeed Bishara 10736f166312SLior Amsalem dev_info(&pdev->dev, "Marvell XOR (%s): ( %s%s%s)\n", 10746f166312SLior Amsalem mv_chan->op_in_desc ? "Descriptor Mode" : "Registers Mode", 1075ff7b0479SSaeed Bishara dma_has_cap(DMA_XOR, dma_dev->cap_mask) ? "xor " : "", 1076ff7b0479SSaeed Bishara dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask) ? "cpy " : "", 1077ff7b0479SSaeed Bishara dma_has_cap(DMA_INTERRUPT, dma_dev->cap_mask) ? "intr " : ""); 1078ff7b0479SSaeed Bishara 1079ff7b0479SSaeed Bishara dma_async_device_register(dma_dev); 10801ef48a26SThomas Petazzoni return mv_chan; 1081ff7b0479SSaeed Bishara 10822d0a0745SThomas Petazzoni err_free_irq: 10832d0a0745SThomas Petazzoni free_irq(mv_chan->irq, mv_chan); 1084ff7b0479SSaeed Bishara err_free_dma: 1085b503fa01SThomas Petazzoni dma_free_coherent(&pdev->dev, MV_XOR_POOL_SIZE, 10861ef48a26SThomas Petazzoni mv_chan->dma_desc_pool_virt, mv_chan->dma_desc_pool); 1087a6b4a9d2SThomas Petazzoni return ERR_PTR(ret); 1088ff7b0479SSaeed Bishara } 1089ff7b0479SSaeed Bishara 1090ff7b0479SSaeed Bishara static void 1091297eedbaSThomas Petazzoni mv_xor_conf_mbus_windows(struct mv_xor_device *xordev, 109263a9332bSAndrew Lunn const struct mbus_dram_target_info *dram) 1093ff7b0479SSaeed Bishara { 109482a1402eSEzequiel Garcia void __iomem *base = xordev->xor_high_base; 1095ff7b0479SSaeed Bishara u32 win_enable = 0; 1096ff7b0479SSaeed Bishara int i; 1097ff7b0479SSaeed Bishara 1098ff7b0479SSaeed Bishara for (i = 0; i < 8; i++) { 1099ff7b0479SSaeed Bishara writel(0, base + WINDOW_BASE(i)); 1100ff7b0479SSaeed Bishara writel(0, base + WINDOW_SIZE(i)); 1101ff7b0479SSaeed Bishara if (i < 4) 1102ff7b0479SSaeed Bishara writel(0, base + WINDOW_REMAP_HIGH(i)); 1103ff7b0479SSaeed Bishara } 1104ff7b0479SSaeed Bishara 1105ff7b0479SSaeed Bishara for (i = 0; i < dram->num_cs; i++) { 110663a9332bSAndrew Lunn const struct mbus_dram_window *cs = dram->cs + i; 1107ff7b0479SSaeed Bishara 1108ff7b0479SSaeed Bishara writel((cs->base & 0xffff0000) | 1109ff7b0479SSaeed Bishara (cs->mbus_attr << 8) | 1110ff7b0479SSaeed Bishara dram->mbus_dram_target_id, base + WINDOW_BASE(i)); 1111ff7b0479SSaeed Bishara writel((cs->size - 1) & 0xffff0000, base + WINDOW_SIZE(i)); 1112ff7b0479SSaeed Bishara 1113ff7b0479SSaeed Bishara win_enable |= (1 << i); 1114ff7b0479SSaeed Bishara win_enable |= 3 << (16 + (2 * i)); 1115ff7b0479SSaeed Bishara } 1116ff7b0479SSaeed Bishara 1117ff7b0479SSaeed Bishara writel(win_enable, base + WINDOW_BAR_ENABLE(0)); 1118ff7b0479SSaeed Bishara writel(win_enable, base + WINDOW_BAR_ENABLE(1)); 1119c4b4b732SThomas Petazzoni writel(0, base + WINDOW_OVERRIDE_CTRL(0)); 1120c4b4b732SThomas Petazzoni writel(0, base + WINDOW_OVERRIDE_CTRL(1)); 1121ff7b0479SSaeed Bishara } 1122ff7b0479SSaeed Bishara 11236f166312SLior Amsalem static const struct of_device_id mv_xor_dt_ids[] = { 11246f166312SLior Amsalem { .compatible = "marvell,orion-xor", .data = (void *)XOR_MODE_IN_REG }, 11256f166312SLior Amsalem { .compatible = "marvell,armada-380-xor", .data = (void *)XOR_MODE_IN_DESC }, 11266f166312SLior Amsalem {}, 11276f166312SLior Amsalem }; 11286f166312SLior Amsalem 112977757291SThomas Petazzoni static unsigned int mv_xor_engine_count; 113077757291SThomas Petazzoni 1131c2714334SLinus Torvalds static int mv_xor_probe(struct platform_device *pdev) 1132ff7b0479SSaeed Bishara { 113363a9332bSAndrew Lunn const struct mbus_dram_target_info *dram; 1134297eedbaSThomas Petazzoni struct mv_xor_device *xordev; 1135d4adcc01SJingoo Han struct mv_xor_platform_data *pdata = dev_get_platdata(&pdev->dev); 1136ff7b0479SSaeed Bishara struct resource *res; 113777757291SThomas Petazzoni unsigned int max_engines, max_channels; 113860d151f3SThomas Petazzoni int i, ret; 11396f166312SLior Amsalem int op_in_desc; 1140ff7b0479SSaeed Bishara 11411ba151cdSJoe Perches dev_notice(&pdev->dev, "Marvell shared XOR driver\n"); 1142ff7b0479SSaeed Bishara 1143297eedbaSThomas Petazzoni xordev = devm_kzalloc(&pdev->dev, sizeof(*xordev), GFP_KERNEL); 1144297eedbaSThomas Petazzoni if (!xordev) 1145ff7b0479SSaeed Bishara return -ENOMEM; 1146ff7b0479SSaeed Bishara 1147ff7b0479SSaeed Bishara res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1148ff7b0479SSaeed Bishara if (!res) 1149ff7b0479SSaeed Bishara return -ENODEV; 1150ff7b0479SSaeed Bishara 1151297eedbaSThomas Petazzoni xordev->xor_base = devm_ioremap(&pdev->dev, res->start, 11524de1ba15SH Hartley Sweeten resource_size(res)); 1153297eedbaSThomas Petazzoni if (!xordev->xor_base) 1154ff7b0479SSaeed Bishara return -EBUSY; 1155ff7b0479SSaeed Bishara 1156ff7b0479SSaeed Bishara res = platform_get_resource(pdev, IORESOURCE_MEM, 1); 1157ff7b0479SSaeed Bishara if (!res) 1158ff7b0479SSaeed Bishara return -ENODEV; 1159ff7b0479SSaeed Bishara 1160297eedbaSThomas Petazzoni xordev->xor_high_base = devm_ioremap(&pdev->dev, res->start, 11614de1ba15SH Hartley Sweeten resource_size(res)); 1162297eedbaSThomas Petazzoni if (!xordev->xor_high_base) 1163ff7b0479SSaeed Bishara return -EBUSY; 1164ff7b0479SSaeed Bishara 1165297eedbaSThomas Petazzoni platform_set_drvdata(pdev, xordev); 1166ff7b0479SSaeed Bishara 1167ff7b0479SSaeed Bishara /* 1168ff7b0479SSaeed Bishara * (Re-)program MBUS remapping windows if we are asked to. 1169ff7b0479SSaeed Bishara */ 117063a9332bSAndrew Lunn dram = mv_mbus_dram_info(); 117163a9332bSAndrew Lunn if (dram) 1172297eedbaSThomas Petazzoni mv_xor_conf_mbus_windows(xordev, dram); 1173ff7b0479SSaeed Bishara 1174c510182bSAndrew Lunn /* Not all platforms can gate the clock, so it is not 1175c510182bSAndrew Lunn * an error if the clock does not exists. 1176c510182bSAndrew Lunn */ 1177297eedbaSThomas Petazzoni xordev->clk = clk_get(&pdev->dev, NULL); 1178297eedbaSThomas Petazzoni if (!IS_ERR(xordev->clk)) 1179297eedbaSThomas Petazzoni clk_prepare_enable(xordev->clk); 1180c510182bSAndrew Lunn 118177757291SThomas Petazzoni /* 118277757291SThomas Petazzoni * We don't want to have more than one channel per CPU in 118377757291SThomas Petazzoni * order for async_tx to perform well. So we limit the number 118477757291SThomas Petazzoni * of engines and channels so that we take into account this 118577757291SThomas Petazzoni * constraint. Note that we also want to use channels from 118677757291SThomas Petazzoni * separate engines when possible. 118777757291SThomas Petazzoni */ 118877757291SThomas Petazzoni max_engines = num_present_cpus(); 118977757291SThomas Petazzoni max_channels = min_t(unsigned int, 119077757291SThomas Petazzoni MV_XOR_MAX_CHANNELS, 119177757291SThomas Petazzoni DIV_ROUND_UP(num_present_cpus(), 2)); 119277757291SThomas Petazzoni 119377757291SThomas Petazzoni if (mv_xor_engine_count >= max_engines) 119477757291SThomas Petazzoni return 0; 119577757291SThomas Petazzoni 1196f7d12ef5SThomas Petazzoni if (pdev->dev.of_node) { 1197f7d12ef5SThomas Petazzoni struct device_node *np; 1198f7d12ef5SThomas Petazzoni int i = 0; 11996f166312SLior Amsalem const struct of_device_id *of_id = 12006f166312SLior Amsalem of_match_device(mv_xor_dt_ids, 12016f166312SLior Amsalem &pdev->dev); 1202f7d12ef5SThomas Petazzoni 1203f7d12ef5SThomas Petazzoni for_each_child_of_node(pdev->dev.of_node, np) { 12040be8253fSRussell King struct mv_xor_chan *chan; 1205f7d12ef5SThomas Petazzoni dma_cap_mask_t cap_mask; 1206f7d12ef5SThomas Petazzoni int irq; 12076f166312SLior Amsalem op_in_desc = (int)of_id->data; 1208f7d12ef5SThomas Petazzoni 120977757291SThomas Petazzoni if (i >= max_channels) 121077757291SThomas Petazzoni continue; 121177757291SThomas Petazzoni 1212f7d12ef5SThomas Petazzoni dma_cap_zero(cap_mask); 1213f7d12ef5SThomas Petazzoni dma_cap_set(DMA_MEMCPY, cap_mask); 1214f7d12ef5SThomas Petazzoni dma_cap_set(DMA_XOR, cap_mask); 1215f7d12ef5SThomas Petazzoni dma_cap_set(DMA_INTERRUPT, cap_mask); 1216f7d12ef5SThomas Petazzoni 1217f7d12ef5SThomas Petazzoni irq = irq_of_parse_and_map(np, 0); 1218f8eb9e7dSThomas Petazzoni if (!irq) { 1219f8eb9e7dSThomas Petazzoni ret = -ENODEV; 1220f7d12ef5SThomas Petazzoni goto err_channel_add; 1221f7d12ef5SThomas Petazzoni } 1222f7d12ef5SThomas Petazzoni 12230be8253fSRussell King chan = mv_xor_channel_add(xordev, pdev, i, 12246f166312SLior Amsalem cap_mask, irq, op_in_desc); 12250be8253fSRussell King if (IS_ERR(chan)) { 12260be8253fSRussell King ret = PTR_ERR(chan); 1227f7d12ef5SThomas Petazzoni irq_dispose_mapping(irq); 1228f7d12ef5SThomas Petazzoni goto err_channel_add; 1229f7d12ef5SThomas Petazzoni } 1230f7d12ef5SThomas Petazzoni 12310be8253fSRussell King xordev->channels[i] = chan; 1232f7d12ef5SThomas Petazzoni i++; 1233f7d12ef5SThomas Petazzoni } 1234f7d12ef5SThomas Petazzoni } else if (pdata && pdata->channels) { 123577757291SThomas Petazzoni for (i = 0; i < max_channels; i++) { 1236e39f6ec1SThomas Petazzoni struct mv_xor_channel_data *cd; 12370be8253fSRussell King struct mv_xor_chan *chan; 123860d151f3SThomas Petazzoni int irq; 123960d151f3SThomas Petazzoni 124060d151f3SThomas Petazzoni cd = &pdata->channels[i]; 124160d151f3SThomas Petazzoni if (!cd) { 124260d151f3SThomas Petazzoni ret = -ENODEV; 124360d151f3SThomas Petazzoni goto err_channel_add; 124460d151f3SThomas Petazzoni } 124560d151f3SThomas Petazzoni 124660d151f3SThomas Petazzoni irq = platform_get_irq(pdev, i); 124760d151f3SThomas Petazzoni if (irq < 0) { 124860d151f3SThomas Petazzoni ret = irq; 124960d151f3SThomas Petazzoni goto err_channel_add; 125060d151f3SThomas Petazzoni } 125160d151f3SThomas Petazzoni 12520be8253fSRussell King chan = mv_xor_channel_add(xordev, pdev, i, 12536f166312SLior Amsalem cd->cap_mask, irq, 12546f166312SLior Amsalem XOR_MODE_IN_REG); 12550be8253fSRussell King if (IS_ERR(chan)) { 12560be8253fSRussell King ret = PTR_ERR(chan); 125760d151f3SThomas Petazzoni goto err_channel_add; 125860d151f3SThomas Petazzoni } 12590be8253fSRussell King 12600be8253fSRussell King xordev->channels[i] = chan; 126160d151f3SThomas Petazzoni } 126260d151f3SThomas Petazzoni } 126360d151f3SThomas Petazzoni 1264ff7b0479SSaeed Bishara return 0; 126560d151f3SThomas Petazzoni 126660d151f3SThomas Petazzoni err_channel_add: 126760d151f3SThomas Petazzoni for (i = 0; i < MV_XOR_MAX_CHANNELS; i++) 1268f7d12ef5SThomas Petazzoni if (xordev->channels[i]) { 1269ab6e439fSThomas Petazzoni mv_xor_channel_remove(xordev->channels[i]); 1270f7d12ef5SThomas Petazzoni if (pdev->dev.of_node) 1271f7d12ef5SThomas Petazzoni irq_dispose_mapping(xordev->channels[i]->irq); 1272f7d12ef5SThomas Petazzoni } 127360d151f3SThomas Petazzoni 1274dab92064SThomas Petazzoni if (!IS_ERR(xordev->clk)) { 1275297eedbaSThomas Petazzoni clk_disable_unprepare(xordev->clk); 1276297eedbaSThomas Petazzoni clk_put(xordev->clk); 1277dab92064SThomas Petazzoni } 1278dab92064SThomas Petazzoni 127960d151f3SThomas Petazzoni return ret; 1280ff7b0479SSaeed Bishara } 1281ff7b0479SSaeed Bishara 1282ff7b0479SSaeed Bishara static struct platform_driver mv_xor_driver = { 1283ff7b0479SSaeed Bishara .probe = mv_xor_probe, 1284ff7b0479SSaeed Bishara .driver = { 1285ff7b0479SSaeed Bishara .name = MV_XOR_NAME, 1286f7d12ef5SThomas Petazzoni .of_match_table = of_match_ptr(mv_xor_dt_ids), 1287ff7b0479SSaeed Bishara }, 1288ff7b0479SSaeed Bishara }; 1289ff7b0479SSaeed Bishara 1290ff7b0479SSaeed Bishara 1291ff7b0479SSaeed Bishara static int __init mv_xor_init(void) 1292ff7b0479SSaeed Bishara { 129361971656SThomas Petazzoni return platform_driver_register(&mv_xor_driver); 1294ff7b0479SSaeed Bishara } 1295*25cf68daSPaul Gortmaker device_initcall(mv_xor_init); 1296ff7b0479SSaeed Bishara 1297*25cf68daSPaul Gortmaker /* 1298ff7b0479SSaeed Bishara MODULE_AUTHOR("Saeed Bishara <saeed@marvell.com>"); 1299ff7b0479SSaeed Bishara MODULE_DESCRIPTION("DMA engine driver for Marvell's XOR engine"); 1300ff7b0479SSaeed Bishara MODULE_LICENSE("GPL"); 1301*25cf68daSPaul Gortmaker */ 1302