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 48dfc97661SLior Amsalem static void mv_desc_init(struct mv_xor_desc_slot *desc, 49*ba87d137SLior Amsalem dma_addr_t addr, u32 byte_count, 50*ba87d137SLior Amsalem enum dma_ctrl_flags flags) 51ff7b0479SSaeed Bishara { 52ff7b0479SSaeed Bishara struct mv_xor_desc *hw_desc = desc->hw_desc; 53ff7b0479SSaeed Bishara 540e7488edSEzequiel Garcia hw_desc->status = XOR_DESC_DMA_OWNED; 55ff7b0479SSaeed Bishara hw_desc->phy_next_desc = 0; 56*ba87d137SLior Amsalem /* Enable end-of-descriptor interrupts only for DMA_PREP_INTERRUPT */ 57*ba87d137SLior Amsalem hw_desc->desc_command = (flags & DMA_PREP_INTERRUPT) ? 58*ba87d137SLior Amsalem XOR_DESC_EOD_INT_EN : 0; 59dfc97661SLior Amsalem hw_desc->phy_dest_addr = addr; 60ff7b0479SSaeed Bishara hw_desc->byte_count = byte_count; 61ff7b0479SSaeed Bishara } 62ff7b0479SSaeed Bishara 63ff7b0479SSaeed Bishara static void mv_desc_set_next_desc(struct mv_xor_desc_slot *desc, 64ff7b0479SSaeed Bishara u32 next_desc_addr) 65ff7b0479SSaeed Bishara { 66ff7b0479SSaeed Bishara struct mv_xor_desc *hw_desc = desc->hw_desc; 67ff7b0479SSaeed Bishara BUG_ON(hw_desc->phy_next_desc); 68ff7b0479SSaeed Bishara hw_desc->phy_next_desc = next_desc_addr; 69ff7b0479SSaeed Bishara } 70ff7b0479SSaeed Bishara 71ff7b0479SSaeed Bishara static void mv_desc_clear_next_desc(struct mv_xor_desc_slot *desc) 72ff7b0479SSaeed Bishara { 73ff7b0479SSaeed Bishara struct mv_xor_desc *hw_desc = desc->hw_desc; 74ff7b0479SSaeed Bishara hw_desc->phy_next_desc = 0; 75ff7b0479SSaeed Bishara } 76ff7b0479SSaeed Bishara 77ff7b0479SSaeed Bishara static void mv_desc_set_src_addr(struct mv_xor_desc_slot *desc, 78ff7b0479SSaeed Bishara int index, dma_addr_t addr) 79ff7b0479SSaeed Bishara { 80ff7b0479SSaeed Bishara struct mv_xor_desc *hw_desc = desc->hw_desc; 81e03bc654SThomas Petazzoni hw_desc->phy_src_addr[mv_phy_src_idx(index)] = addr; 82ff7b0479SSaeed Bishara if (desc->type == DMA_XOR) 83ff7b0479SSaeed Bishara hw_desc->desc_command |= (1 << index); 84ff7b0479SSaeed Bishara } 85ff7b0479SSaeed Bishara 86ff7b0479SSaeed Bishara static u32 mv_chan_get_current_desc(struct mv_xor_chan *chan) 87ff7b0479SSaeed Bishara { 885733c38aSThomas Petazzoni return readl_relaxed(XOR_CURR_DESC(chan)); 89ff7b0479SSaeed Bishara } 90ff7b0479SSaeed Bishara 91ff7b0479SSaeed Bishara static void mv_chan_set_next_descriptor(struct mv_xor_chan *chan, 92ff7b0479SSaeed Bishara u32 next_desc_addr) 93ff7b0479SSaeed Bishara { 945733c38aSThomas Petazzoni writel_relaxed(next_desc_addr, XOR_NEXT_DESC(chan)); 95ff7b0479SSaeed Bishara } 96ff7b0479SSaeed Bishara 97ff7b0479SSaeed Bishara static void mv_chan_unmask_interrupts(struct mv_xor_chan *chan) 98ff7b0479SSaeed Bishara { 995733c38aSThomas Petazzoni u32 val = readl_relaxed(XOR_INTR_MASK(chan)); 100ff7b0479SSaeed Bishara val |= XOR_INTR_MASK_VALUE << (chan->idx * 16); 1015733c38aSThomas Petazzoni writel_relaxed(val, XOR_INTR_MASK(chan)); 102ff7b0479SSaeed Bishara } 103ff7b0479SSaeed Bishara 104ff7b0479SSaeed Bishara static u32 mv_chan_get_intr_cause(struct mv_xor_chan *chan) 105ff7b0479SSaeed Bishara { 1065733c38aSThomas Petazzoni u32 intr_cause = readl_relaxed(XOR_INTR_CAUSE(chan)); 107ff7b0479SSaeed Bishara intr_cause = (intr_cause >> (chan->idx * 16)) & 0xFFFF; 108ff7b0479SSaeed Bishara return intr_cause; 109ff7b0479SSaeed Bishara } 110ff7b0479SSaeed Bishara 111ff7b0479SSaeed Bishara static void mv_xor_device_clear_eoc_cause(struct mv_xor_chan *chan) 112ff7b0479SSaeed Bishara { 113*ba87d137SLior Amsalem u32 val; 114*ba87d137SLior Amsalem 115*ba87d137SLior Amsalem val = XOR_INT_END_OF_DESC | XOR_INT_END_OF_CHAIN | XOR_INT_STOPPED; 116*ba87d137SLior Amsalem val = ~(val << (chan->idx * 16)); 117c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(chan), "%s, val 0x%08x\n", __func__, val); 1185733c38aSThomas Petazzoni writel_relaxed(val, XOR_INTR_CAUSE(chan)); 119ff7b0479SSaeed Bishara } 120ff7b0479SSaeed Bishara 121ff7b0479SSaeed Bishara static void mv_xor_device_clear_err_status(struct mv_xor_chan *chan) 122ff7b0479SSaeed Bishara { 123ff7b0479SSaeed Bishara u32 val = 0xFFFF0000 >> (chan->idx * 16); 1245733c38aSThomas Petazzoni writel_relaxed(val, XOR_INTR_CAUSE(chan)); 125ff7b0479SSaeed Bishara } 126ff7b0479SSaeed Bishara 127ff7b0479SSaeed Bishara static void mv_set_mode(struct mv_xor_chan *chan, 128ff7b0479SSaeed Bishara enum dma_transaction_type type) 129ff7b0479SSaeed Bishara { 130ff7b0479SSaeed Bishara u32 op_mode; 1315733c38aSThomas Petazzoni u32 config = readl_relaxed(XOR_CONFIG(chan)); 132ff7b0479SSaeed Bishara 133ff7b0479SSaeed Bishara switch (type) { 134ff7b0479SSaeed Bishara case DMA_XOR: 135ff7b0479SSaeed Bishara op_mode = XOR_OPERATION_MODE_XOR; 136ff7b0479SSaeed Bishara break; 137ff7b0479SSaeed Bishara case DMA_MEMCPY: 138ff7b0479SSaeed Bishara op_mode = XOR_OPERATION_MODE_MEMCPY; 139ff7b0479SSaeed Bishara break; 140ff7b0479SSaeed Bishara default: 141c98c1781SThomas Petazzoni dev_err(mv_chan_to_devp(chan), 1421ba151cdSJoe Perches "error: unsupported operation %d\n", 143ff7b0479SSaeed Bishara type); 144ff7b0479SSaeed Bishara BUG(); 145ff7b0479SSaeed Bishara return; 146ff7b0479SSaeed Bishara } 147ff7b0479SSaeed Bishara 148ff7b0479SSaeed Bishara config &= ~0x7; 149ff7b0479SSaeed Bishara config |= op_mode; 150e03bc654SThomas Petazzoni 151e03bc654SThomas Petazzoni #if defined(__BIG_ENDIAN) 152e03bc654SThomas Petazzoni config |= XOR_DESCRIPTOR_SWAP; 153e03bc654SThomas Petazzoni #else 154e03bc654SThomas Petazzoni config &= ~XOR_DESCRIPTOR_SWAP; 155e03bc654SThomas Petazzoni #endif 156e03bc654SThomas Petazzoni 1575733c38aSThomas Petazzoni writel_relaxed(config, XOR_CONFIG(chan)); 158ff7b0479SSaeed Bishara chan->current_type = type; 159ff7b0479SSaeed Bishara } 160ff7b0479SSaeed Bishara 161ff7b0479SSaeed Bishara static void mv_chan_activate(struct mv_xor_chan *chan) 162ff7b0479SSaeed Bishara { 163c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(chan), " activate chan.\n"); 1645a9a55bfSEzequiel Garcia 1655a9a55bfSEzequiel Garcia /* writel ensures all descriptors are flushed before activation */ 1665a9a55bfSEzequiel Garcia writel(BIT(0), XOR_ACTIVATION(chan)); 167ff7b0479SSaeed Bishara } 168ff7b0479SSaeed Bishara 169ff7b0479SSaeed Bishara static char mv_chan_is_busy(struct mv_xor_chan *chan) 170ff7b0479SSaeed Bishara { 1715733c38aSThomas Petazzoni u32 state = readl_relaxed(XOR_ACTIVATION(chan)); 172ff7b0479SSaeed Bishara 173ff7b0479SSaeed Bishara state = (state >> 4) & 0x3; 174ff7b0479SSaeed Bishara 175ff7b0479SSaeed Bishara return (state == 1) ? 1 : 0; 176ff7b0479SSaeed Bishara } 177ff7b0479SSaeed Bishara 178ff7b0479SSaeed Bishara /** 179ff7b0479SSaeed Bishara * mv_xor_free_slots - flags descriptor slots for reuse 180ff7b0479SSaeed Bishara * @slot: Slot to free 181ff7b0479SSaeed Bishara * Caller must hold &mv_chan->lock while calling this function 182ff7b0479SSaeed Bishara */ 183ff7b0479SSaeed Bishara static void mv_xor_free_slots(struct mv_xor_chan *mv_chan, 184ff7b0479SSaeed Bishara struct mv_xor_desc_slot *slot) 185ff7b0479SSaeed Bishara { 186c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), "%s %d slot %p\n", 187ff7b0479SSaeed Bishara __func__, __LINE__, slot); 188ff7b0479SSaeed Bishara 189dfc97661SLior Amsalem slot->slot_used = 0; 190ff7b0479SSaeed Bishara 191ff7b0479SSaeed Bishara } 192ff7b0479SSaeed Bishara 193ff7b0479SSaeed Bishara /* 194ff7b0479SSaeed Bishara * mv_xor_start_new_chain - program the engine to operate on new chain headed by 195ff7b0479SSaeed Bishara * sw_desc 196ff7b0479SSaeed Bishara * Caller must hold &mv_chan->lock while calling this function 197ff7b0479SSaeed Bishara */ 198ff7b0479SSaeed Bishara static void mv_xor_start_new_chain(struct mv_xor_chan *mv_chan, 199ff7b0479SSaeed Bishara struct mv_xor_desc_slot *sw_desc) 200ff7b0479SSaeed Bishara { 201c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), "%s %d: sw_desc %p\n", 202ff7b0479SSaeed Bishara __func__, __LINE__, sw_desc); 203ff7b0479SSaeed Bishara 204ff7b0479SSaeed Bishara /* set the hardware chain */ 205ff7b0479SSaeed Bishara mv_chan_set_next_descriptor(mv_chan, sw_desc->async_tx.phys); 20648a9db46SBartlomiej Zolnierkiewicz 207dfc97661SLior Amsalem mv_chan->pending++; 20898817b99SThomas Petazzoni mv_xor_issue_pending(&mv_chan->dmachan); 209ff7b0479SSaeed Bishara } 210ff7b0479SSaeed Bishara 211ff7b0479SSaeed Bishara static dma_cookie_t 212ff7b0479SSaeed Bishara mv_xor_run_tx_complete_actions(struct mv_xor_desc_slot *desc, 213ff7b0479SSaeed Bishara struct mv_xor_chan *mv_chan, dma_cookie_t cookie) 214ff7b0479SSaeed Bishara { 215ff7b0479SSaeed Bishara BUG_ON(desc->async_tx.cookie < 0); 216ff7b0479SSaeed Bishara 217ff7b0479SSaeed Bishara if (desc->async_tx.cookie > 0) { 218ff7b0479SSaeed Bishara cookie = desc->async_tx.cookie; 219ff7b0479SSaeed Bishara 220ff7b0479SSaeed Bishara /* call the callback (must not sleep or submit new 221ff7b0479SSaeed Bishara * operations to this channel) 222ff7b0479SSaeed Bishara */ 223ff7b0479SSaeed Bishara if (desc->async_tx.callback) 224ff7b0479SSaeed Bishara desc->async_tx.callback( 225ff7b0479SSaeed Bishara desc->async_tx.callback_param); 226ff7b0479SSaeed Bishara 227d38a8c62SDan Williams dma_descriptor_unmap(&desc->async_tx); 228ff7b0479SSaeed Bishara } 229ff7b0479SSaeed Bishara 230ff7b0479SSaeed Bishara /* run dependent operations */ 23107f2211eSDan Williams dma_run_dependencies(&desc->async_tx); 232ff7b0479SSaeed Bishara 233ff7b0479SSaeed Bishara return cookie; 234ff7b0479SSaeed Bishara } 235ff7b0479SSaeed Bishara 236ff7b0479SSaeed Bishara static int 237ff7b0479SSaeed Bishara mv_xor_clean_completed_slots(struct mv_xor_chan *mv_chan) 238ff7b0479SSaeed Bishara { 239ff7b0479SSaeed Bishara struct mv_xor_desc_slot *iter, *_iter; 240ff7b0479SSaeed Bishara 241c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), "%s %d\n", __func__, __LINE__); 242ff7b0479SSaeed Bishara list_for_each_entry_safe(iter, _iter, &mv_chan->completed_slots, 243ff7b0479SSaeed Bishara completed_node) { 244ff7b0479SSaeed Bishara 245ff7b0479SSaeed Bishara if (async_tx_test_ack(&iter->async_tx)) { 246ff7b0479SSaeed Bishara list_del(&iter->completed_node); 247ff7b0479SSaeed Bishara mv_xor_free_slots(mv_chan, iter); 248ff7b0479SSaeed Bishara } 249ff7b0479SSaeed Bishara } 250ff7b0479SSaeed Bishara return 0; 251ff7b0479SSaeed Bishara } 252ff7b0479SSaeed Bishara 253ff7b0479SSaeed Bishara static int 254ff7b0479SSaeed Bishara mv_xor_clean_slot(struct mv_xor_desc_slot *desc, 255ff7b0479SSaeed Bishara struct mv_xor_chan *mv_chan) 256ff7b0479SSaeed Bishara { 257c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), "%s %d: desc %p flags %d\n", 258ff7b0479SSaeed Bishara __func__, __LINE__, desc, desc->async_tx.flags); 259ff7b0479SSaeed Bishara list_del(&desc->chain_node); 260ff7b0479SSaeed Bishara /* the client is allowed to attach dependent operations 261ff7b0479SSaeed Bishara * until 'ack' is set 262ff7b0479SSaeed Bishara */ 263ff7b0479SSaeed Bishara if (!async_tx_test_ack(&desc->async_tx)) { 264ff7b0479SSaeed Bishara /* move this slot to the completed_slots */ 265ff7b0479SSaeed Bishara list_add_tail(&desc->completed_node, &mv_chan->completed_slots); 266ff7b0479SSaeed Bishara return 0; 267ff7b0479SSaeed Bishara } 268ff7b0479SSaeed Bishara 269ff7b0479SSaeed Bishara mv_xor_free_slots(mv_chan, desc); 270ff7b0479SSaeed Bishara return 0; 271ff7b0479SSaeed Bishara } 272ff7b0479SSaeed Bishara 273ff7b0479SSaeed Bishara static void __mv_xor_slot_cleanup(struct mv_xor_chan *mv_chan) 274ff7b0479SSaeed Bishara { 275ff7b0479SSaeed Bishara struct mv_xor_desc_slot *iter, *_iter; 276ff7b0479SSaeed Bishara dma_cookie_t cookie = 0; 277ff7b0479SSaeed Bishara int busy = mv_chan_is_busy(mv_chan); 278ff7b0479SSaeed Bishara u32 current_desc = mv_chan_get_current_desc(mv_chan); 279ff7b0479SSaeed Bishara int seen_current = 0; 280ff7b0479SSaeed Bishara 281c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), "%s %d\n", __func__, __LINE__); 282c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), "current_desc %x\n", current_desc); 283ff7b0479SSaeed Bishara mv_xor_clean_completed_slots(mv_chan); 284ff7b0479SSaeed Bishara 285ff7b0479SSaeed Bishara /* free completed slots from the chain starting with 286ff7b0479SSaeed Bishara * the oldest descriptor 287ff7b0479SSaeed Bishara */ 288ff7b0479SSaeed Bishara 289ff7b0479SSaeed Bishara list_for_each_entry_safe(iter, _iter, &mv_chan->chain, 290ff7b0479SSaeed Bishara chain_node) { 291ff7b0479SSaeed Bishara prefetch(_iter); 292ff7b0479SSaeed Bishara prefetch(&_iter->async_tx); 293ff7b0479SSaeed Bishara 294ff7b0479SSaeed Bishara /* do not advance past the current descriptor loaded into the 295ff7b0479SSaeed Bishara * hardware channel, subsequent descriptors are either in 296ff7b0479SSaeed Bishara * process or have not been submitted 297ff7b0479SSaeed Bishara */ 298ff7b0479SSaeed Bishara if (seen_current) 299ff7b0479SSaeed Bishara break; 300ff7b0479SSaeed Bishara 301ff7b0479SSaeed Bishara /* stop the search if we reach the current descriptor and the 302ff7b0479SSaeed Bishara * channel is busy 303ff7b0479SSaeed Bishara */ 304ff7b0479SSaeed Bishara if (iter->async_tx.phys == current_desc) { 305ff7b0479SSaeed Bishara seen_current = 1; 306ff7b0479SSaeed Bishara if (busy) 307ff7b0479SSaeed Bishara break; 308ff7b0479SSaeed Bishara } 309ff7b0479SSaeed Bishara 310ff7b0479SSaeed Bishara cookie = mv_xor_run_tx_complete_actions(iter, mv_chan, cookie); 311ff7b0479SSaeed Bishara 312ff7b0479SSaeed Bishara if (mv_xor_clean_slot(iter, mv_chan)) 313ff7b0479SSaeed Bishara break; 314ff7b0479SSaeed Bishara } 315ff7b0479SSaeed Bishara 316ff7b0479SSaeed Bishara if ((busy == 0) && !list_empty(&mv_chan->chain)) { 317ff7b0479SSaeed Bishara struct mv_xor_desc_slot *chain_head; 318ff7b0479SSaeed Bishara chain_head = list_entry(mv_chan->chain.next, 319ff7b0479SSaeed Bishara struct mv_xor_desc_slot, 320ff7b0479SSaeed Bishara chain_node); 321ff7b0479SSaeed Bishara 322ff7b0479SSaeed Bishara mv_xor_start_new_chain(mv_chan, chain_head); 323ff7b0479SSaeed Bishara } 324ff7b0479SSaeed Bishara 325ff7b0479SSaeed Bishara if (cookie > 0) 32698817b99SThomas Petazzoni mv_chan->dmachan.completed_cookie = cookie; 327ff7b0479SSaeed Bishara } 328ff7b0479SSaeed Bishara 329ff7b0479SSaeed Bishara static void 330ff7b0479SSaeed Bishara mv_xor_slot_cleanup(struct mv_xor_chan *mv_chan) 331ff7b0479SSaeed Bishara { 332ff7b0479SSaeed Bishara spin_lock_bh(&mv_chan->lock); 333ff7b0479SSaeed Bishara __mv_xor_slot_cleanup(mv_chan); 334ff7b0479SSaeed Bishara spin_unlock_bh(&mv_chan->lock); 335ff7b0479SSaeed Bishara } 336ff7b0479SSaeed Bishara 337ff7b0479SSaeed Bishara static void mv_xor_tasklet(unsigned long data) 338ff7b0479SSaeed Bishara { 339ff7b0479SSaeed Bishara struct mv_xor_chan *chan = (struct mv_xor_chan *) data; 3408333f65eSSaeed Bishara mv_xor_slot_cleanup(chan); 341ff7b0479SSaeed Bishara } 342ff7b0479SSaeed Bishara 343ff7b0479SSaeed Bishara static struct mv_xor_desc_slot * 344dfc97661SLior Amsalem mv_xor_alloc_slot(struct mv_xor_chan *mv_chan) 345ff7b0479SSaeed Bishara { 346dfc97661SLior Amsalem struct mv_xor_desc_slot *iter, *_iter; 347dfc97661SLior Amsalem int retry = 0; 348ff7b0479SSaeed Bishara 349ff7b0479SSaeed Bishara /* start search from the last allocated descrtiptor 350ff7b0479SSaeed Bishara * if a contiguous allocation can not be found start searching 351ff7b0479SSaeed Bishara * from the beginning of the list 352ff7b0479SSaeed Bishara */ 353ff7b0479SSaeed Bishara retry: 354ff7b0479SSaeed Bishara if (retry == 0) 355ff7b0479SSaeed Bishara iter = mv_chan->last_used; 356ff7b0479SSaeed Bishara else 357ff7b0479SSaeed Bishara iter = list_entry(&mv_chan->all_slots, 358ff7b0479SSaeed Bishara struct mv_xor_desc_slot, 359ff7b0479SSaeed Bishara slot_node); 360ff7b0479SSaeed Bishara 361ff7b0479SSaeed Bishara list_for_each_entry_safe_continue( 362ff7b0479SSaeed Bishara iter, _iter, &mv_chan->all_slots, slot_node) { 363dfc97661SLior Amsalem 364ff7b0479SSaeed Bishara prefetch(_iter); 365ff7b0479SSaeed Bishara prefetch(&_iter->async_tx); 366dfc97661SLior Amsalem if (iter->slot_used) { 367ff7b0479SSaeed Bishara /* give up after finding the first busy slot 368ff7b0479SSaeed Bishara * on the second pass through the list 369ff7b0479SSaeed Bishara */ 370ff7b0479SSaeed Bishara if (retry) 371ff7b0479SSaeed Bishara break; 372ff7b0479SSaeed Bishara continue; 373ff7b0479SSaeed Bishara } 374ff7b0479SSaeed Bishara 375dfc97661SLior Amsalem /* pre-ack descriptor */ 376ff7b0479SSaeed Bishara async_tx_ack(&iter->async_tx); 377ff7b0479SSaeed Bishara 378dfc97661SLior Amsalem iter->slot_used = 1; 379dfc97661SLior Amsalem INIT_LIST_HEAD(&iter->chain_node); 380dfc97661SLior Amsalem iter->async_tx.cookie = -EBUSY; 381dfc97661SLior Amsalem mv_chan->last_used = iter; 382dfc97661SLior Amsalem mv_desc_clear_next_desc(iter); 383dfc97661SLior Amsalem 384dfc97661SLior Amsalem return iter; 385dfc97661SLior Amsalem 386ff7b0479SSaeed Bishara } 387ff7b0479SSaeed Bishara if (!retry++) 388ff7b0479SSaeed Bishara goto retry; 389ff7b0479SSaeed Bishara 390ff7b0479SSaeed Bishara /* try to free some slots if the allocation fails */ 391ff7b0479SSaeed Bishara tasklet_schedule(&mv_chan->irq_tasklet); 392ff7b0479SSaeed Bishara 393ff7b0479SSaeed Bishara return NULL; 394ff7b0479SSaeed Bishara } 395ff7b0479SSaeed Bishara 396ff7b0479SSaeed Bishara /************************ DMA engine API functions ****************************/ 397ff7b0479SSaeed Bishara static dma_cookie_t 398ff7b0479SSaeed Bishara mv_xor_tx_submit(struct dma_async_tx_descriptor *tx) 399ff7b0479SSaeed Bishara { 400ff7b0479SSaeed Bishara struct mv_xor_desc_slot *sw_desc = to_mv_xor_slot(tx); 401ff7b0479SSaeed Bishara struct mv_xor_chan *mv_chan = to_mv_xor_chan(tx->chan); 402dfc97661SLior Amsalem struct mv_xor_desc_slot *old_chain_tail; 403ff7b0479SSaeed Bishara dma_cookie_t cookie; 404ff7b0479SSaeed Bishara int new_hw_chain = 1; 405ff7b0479SSaeed Bishara 406c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), 407ff7b0479SSaeed Bishara "%s sw_desc %p: async_tx %p\n", 408ff7b0479SSaeed Bishara __func__, sw_desc, &sw_desc->async_tx); 409ff7b0479SSaeed Bishara 410ff7b0479SSaeed Bishara spin_lock_bh(&mv_chan->lock); 411884485e1SRussell King - ARM Linux cookie = dma_cookie_assign(tx); 412ff7b0479SSaeed Bishara 413ff7b0479SSaeed Bishara if (list_empty(&mv_chan->chain)) 414dfc97661SLior Amsalem list_add_tail(&sw_desc->chain_node, &mv_chan->chain); 415ff7b0479SSaeed Bishara else { 416ff7b0479SSaeed Bishara new_hw_chain = 0; 417ff7b0479SSaeed Bishara 418ff7b0479SSaeed Bishara old_chain_tail = list_entry(mv_chan->chain.prev, 419ff7b0479SSaeed Bishara struct mv_xor_desc_slot, 420ff7b0479SSaeed Bishara chain_node); 421dfc97661SLior Amsalem list_add_tail(&sw_desc->chain_node, &mv_chan->chain); 422ff7b0479SSaeed Bishara 42331fd8f5bSOlof Johansson dev_dbg(mv_chan_to_devp(mv_chan), "Append to last desc %pa\n", 42431fd8f5bSOlof Johansson &old_chain_tail->async_tx.phys); 425ff7b0479SSaeed Bishara 426ff7b0479SSaeed Bishara /* fix up the hardware chain */ 427dfc97661SLior Amsalem mv_desc_set_next_desc(old_chain_tail, sw_desc->async_tx.phys); 428ff7b0479SSaeed Bishara 429ff7b0479SSaeed Bishara /* if the channel is not busy */ 430ff7b0479SSaeed Bishara if (!mv_chan_is_busy(mv_chan)) { 431ff7b0479SSaeed Bishara u32 current_desc = mv_chan_get_current_desc(mv_chan); 432ff7b0479SSaeed Bishara /* 433ff7b0479SSaeed Bishara * and the curren desc is the end of the chain before 434ff7b0479SSaeed Bishara * the append, then we need to start the channel 435ff7b0479SSaeed Bishara */ 436ff7b0479SSaeed Bishara if (current_desc == old_chain_tail->async_tx.phys) 437ff7b0479SSaeed Bishara new_hw_chain = 1; 438ff7b0479SSaeed Bishara } 439ff7b0479SSaeed Bishara } 440ff7b0479SSaeed Bishara 441ff7b0479SSaeed Bishara if (new_hw_chain) 442dfc97661SLior Amsalem mv_xor_start_new_chain(mv_chan, sw_desc); 443ff7b0479SSaeed Bishara 444ff7b0479SSaeed Bishara spin_unlock_bh(&mv_chan->lock); 445ff7b0479SSaeed Bishara 446ff7b0479SSaeed Bishara return cookie; 447ff7b0479SSaeed Bishara } 448ff7b0479SSaeed Bishara 449ff7b0479SSaeed Bishara /* returns the number of allocated descriptors */ 450aa1e6f1aSDan Williams static int mv_xor_alloc_chan_resources(struct dma_chan *chan) 451ff7b0479SSaeed Bishara { 45231fd8f5bSOlof Johansson void *virt_desc; 45331fd8f5bSOlof Johansson dma_addr_t dma_desc; 454ff7b0479SSaeed Bishara int idx; 455ff7b0479SSaeed Bishara struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan); 456ff7b0479SSaeed Bishara struct mv_xor_desc_slot *slot = NULL; 457b503fa01SThomas Petazzoni int num_descs_in_pool = MV_XOR_POOL_SIZE/MV_XOR_SLOT_SIZE; 458ff7b0479SSaeed Bishara 459ff7b0479SSaeed Bishara /* Allocate descriptor slots */ 460ff7b0479SSaeed Bishara idx = mv_chan->slots_allocated; 461ff7b0479SSaeed Bishara while (idx < num_descs_in_pool) { 462ff7b0479SSaeed Bishara slot = kzalloc(sizeof(*slot), GFP_KERNEL); 463ff7b0479SSaeed Bishara if (!slot) { 464b8291ddeSEzequiel Garcia dev_info(mv_chan_to_devp(mv_chan), 465b8291ddeSEzequiel Garcia "channel only initialized %d descriptor slots", 466b8291ddeSEzequiel Garcia idx); 467ff7b0479SSaeed Bishara break; 468ff7b0479SSaeed Bishara } 46931fd8f5bSOlof Johansson virt_desc = mv_chan->dma_desc_pool_virt; 47031fd8f5bSOlof Johansson slot->hw_desc = virt_desc + idx * MV_XOR_SLOT_SIZE; 471ff7b0479SSaeed Bishara 472ff7b0479SSaeed Bishara dma_async_tx_descriptor_init(&slot->async_tx, chan); 473ff7b0479SSaeed Bishara slot->async_tx.tx_submit = mv_xor_tx_submit; 474ff7b0479SSaeed Bishara INIT_LIST_HEAD(&slot->chain_node); 475ff7b0479SSaeed Bishara INIT_LIST_HEAD(&slot->slot_node); 47631fd8f5bSOlof Johansson dma_desc = mv_chan->dma_desc_pool; 47731fd8f5bSOlof Johansson slot->async_tx.phys = dma_desc + idx * MV_XOR_SLOT_SIZE; 478ff7b0479SSaeed Bishara slot->idx = idx++; 479ff7b0479SSaeed Bishara 480ff7b0479SSaeed Bishara spin_lock_bh(&mv_chan->lock); 481ff7b0479SSaeed Bishara mv_chan->slots_allocated = idx; 482ff7b0479SSaeed Bishara list_add_tail(&slot->slot_node, &mv_chan->all_slots); 483ff7b0479SSaeed Bishara spin_unlock_bh(&mv_chan->lock); 484ff7b0479SSaeed Bishara } 485ff7b0479SSaeed Bishara 486ff7b0479SSaeed Bishara if (mv_chan->slots_allocated && !mv_chan->last_used) 487ff7b0479SSaeed Bishara mv_chan->last_used = list_entry(mv_chan->all_slots.next, 488ff7b0479SSaeed Bishara struct mv_xor_desc_slot, 489ff7b0479SSaeed Bishara slot_node); 490ff7b0479SSaeed Bishara 491c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), 492ff7b0479SSaeed Bishara "allocated %d descriptor slots last_used: %p\n", 493ff7b0479SSaeed Bishara mv_chan->slots_allocated, mv_chan->last_used); 494ff7b0479SSaeed Bishara 495ff7b0479SSaeed Bishara return mv_chan->slots_allocated ? : -ENOMEM; 496ff7b0479SSaeed Bishara } 497ff7b0479SSaeed Bishara 498ff7b0479SSaeed Bishara static struct dma_async_tx_descriptor * 499ff7b0479SSaeed Bishara mv_xor_prep_dma_xor(struct dma_chan *chan, dma_addr_t dest, dma_addr_t *src, 500ff7b0479SSaeed Bishara unsigned int src_cnt, size_t len, unsigned long flags) 501ff7b0479SSaeed Bishara { 502ff7b0479SSaeed Bishara struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan); 503dfc97661SLior Amsalem struct mv_xor_desc_slot *sw_desc; 504ff7b0479SSaeed Bishara 505ff7b0479SSaeed Bishara if (unlikely(len < MV_XOR_MIN_BYTE_COUNT)) 506ff7b0479SSaeed Bishara return NULL; 507ff7b0479SSaeed Bishara 5087912d300SColy Li BUG_ON(len > MV_XOR_MAX_BYTE_COUNT); 509ff7b0479SSaeed Bishara 510c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), 51131fd8f5bSOlof Johansson "%s src_cnt: %d len: %u dest %pad flags: %ld\n", 51231fd8f5bSOlof Johansson __func__, src_cnt, len, &dest, flags); 513ff7b0479SSaeed Bishara 514ff7b0479SSaeed Bishara spin_lock_bh(&mv_chan->lock); 515dfc97661SLior Amsalem sw_desc = mv_xor_alloc_slot(mv_chan); 516ff7b0479SSaeed Bishara if (sw_desc) { 517ff7b0479SSaeed Bishara sw_desc->type = DMA_XOR; 518ff7b0479SSaeed Bishara sw_desc->async_tx.flags = flags; 519*ba87d137SLior Amsalem mv_desc_init(sw_desc, dest, len, flags); 520ff7b0479SSaeed Bishara sw_desc->unmap_src_cnt = src_cnt; 521ff7b0479SSaeed Bishara sw_desc->unmap_len = len; 522ff7b0479SSaeed Bishara while (src_cnt--) 523dfc97661SLior Amsalem mv_desc_set_src_addr(sw_desc, src_cnt, src[src_cnt]); 524ff7b0479SSaeed Bishara } 525ff7b0479SSaeed Bishara spin_unlock_bh(&mv_chan->lock); 526c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), 527ff7b0479SSaeed Bishara "%s sw_desc %p async_tx %p \n", 528ff7b0479SSaeed Bishara __func__, sw_desc, &sw_desc->async_tx); 529ff7b0479SSaeed Bishara return sw_desc ? &sw_desc->async_tx : NULL; 530ff7b0479SSaeed Bishara } 531ff7b0479SSaeed Bishara 5323e4f52e2SLior Amsalem static struct dma_async_tx_descriptor * 5333e4f52e2SLior Amsalem mv_xor_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src, 5343e4f52e2SLior Amsalem size_t len, unsigned long flags) 5353e4f52e2SLior Amsalem { 5363e4f52e2SLior Amsalem /* 5373e4f52e2SLior Amsalem * A MEMCPY operation is identical to an XOR operation with only 5383e4f52e2SLior Amsalem * a single source address. 5393e4f52e2SLior Amsalem */ 5403e4f52e2SLior Amsalem return mv_xor_prep_dma_xor(chan, dest, &src, 1, len, flags); 5413e4f52e2SLior Amsalem } 5423e4f52e2SLior Amsalem 543ff7b0479SSaeed Bishara static void mv_xor_free_chan_resources(struct dma_chan *chan) 544ff7b0479SSaeed Bishara { 545ff7b0479SSaeed Bishara struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan); 546ff7b0479SSaeed Bishara struct mv_xor_desc_slot *iter, *_iter; 547ff7b0479SSaeed Bishara int in_use_descs = 0; 548ff7b0479SSaeed Bishara 549ff7b0479SSaeed Bishara mv_xor_slot_cleanup(mv_chan); 550ff7b0479SSaeed Bishara 551ff7b0479SSaeed Bishara spin_lock_bh(&mv_chan->lock); 552ff7b0479SSaeed Bishara list_for_each_entry_safe(iter, _iter, &mv_chan->chain, 553ff7b0479SSaeed Bishara chain_node) { 554ff7b0479SSaeed Bishara in_use_descs++; 555ff7b0479SSaeed Bishara list_del(&iter->chain_node); 556ff7b0479SSaeed Bishara } 557ff7b0479SSaeed Bishara list_for_each_entry_safe(iter, _iter, &mv_chan->completed_slots, 558ff7b0479SSaeed Bishara completed_node) { 559ff7b0479SSaeed Bishara in_use_descs++; 560ff7b0479SSaeed Bishara list_del(&iter->completed_node); 561ff7b0479SSaeed Bishara } 562ff7b0479SSaeed Bishara list_for_each_entry_safe_reverse( 563ff7b0479SSaeed Bishara iter, _iter, &mv_chan->all_slots, slot_node) { 564ff7b0479SSaeed Bishara list_del(&iter->slot_node); 565ff7b0479SSaeed Bishara kfree(iter); 566ff7b0479SSaeed Bishara mv_chan->slots_allocated--; 567ff7b0479SSaeed Bishara } 568ff7b0479SSaeed Bishara mv_chan->last_used = NULL; 569ff7b0479SSaeed Bishara 570c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(mv_chan), "%s slots_allocated %d\n", 571ff7b0479SSaeed Bishara __func__, mv_chan->slots_allocated); 572ff7b0479SSaeed Bishara spin_unlock_bh(&mv_chan->lock); 573ff7b0479SSaeed Bishara 574ff7b0479SSaeed Bishara if (in_use_descs) 575c98c1781SThomas Petazzoni dev_err(mv_chan_to_devp(mv_chan), 576ff7b0479SSaeed Bishara "freeing %d in use descriptors!\n", in_use_descs); 577ff7b0479SSaeed Bishara } 578ff7b0479SSaeed Bishara 579ff7b0479SSaeed Bishara /** 58007934481SLinus Walleij * mv_xor_status - poll the status of an XOR transaction 581ff7b0479SSaeed Bishara * @chan: XOR channel handle 582ff7b0479SSaeed Bishara * @cookie: XOR transaction identifier 58307934481SLinus Walleij * @txstate: XOR transactions state holder (or NULL) 584ff7b0479SSaeed Bishara */ 58507934481SLinus Walleij static enum dma_status mv_xor_status(struct dma_chan *chan, 586ff7b0479SSaeed Bishara dma_cookie_t cookie, 58707934481SLinus Walleij struct dma_tx_state *txstate) 588ff7b0479SSaeed Bishara { 589ff7b0479SSaeed Bishara struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan); 590ff7b0479SSaeed Bishara enum dma_status ret; 591ff7b0479SSaeed Bishara 59296a2af41SRussell King - ARM Linux ret = dma_cookie_status(chan, cookie, txstate); 593b3efb8fcSVinod Koul if (ret == DMA_COMPLETE) { 594ff7b0479SSaeed Bishara mv_xor_clean_completed_slots(mv_chan); 595ff7b0479SSaeed Bishara return ret; 596ff7b0479SSaeed Bishara } 597ff7b0479SSaeed Bishara mv_xor_slot_cleanup(mv_chan); 598ff7b0479SSaeed Bishara 59996a2af41SRussell King - ARM Linux return dma_cookie_status(chan, cookie, txstate); 600ff7b0479SSaeed Bishara } 601ff7b0479SSaeed Bishara 602ff7b0479SSaeed Bishara static void mv_dump_xor_regs(struct mv_xor_chan *chan) 603ff7b0479SSaeed Bishara { 604ff7b0479SSaeed Bishara u32 val; 605ff7b0479SSaeed Bishara 6065733c38aSThomas Petazzoni val = readl_relaxed(XOR_CONFIG(chan)); 6071ba151cdSJoe Perches dev_err(mv_chan_to_devp(chan), "config 0x%08x\n", val); 608ff7b0479SSaeed Bishara 6095733c38aSThomas Petazzoni val = readl_relaxed(XOR_ACTIVATION(chan)); 6101ba151cdSJoe Perches dev_err(mv_chan_to_devp(chan), "activation 0x%08x\n", val); 611ff7b0479SSaeed Bishara 6125733c38aSThomas Petazzoni val = readl_relaxed(XOR_INTR_CAUSE(chan)); 6131ba151cdSJoe Perches dev_err(mv_chan_to_devp(chan), "intr cause 0x%08x\n", val); 614ff7b0479SSaeed Bishara 6155733c38aSThomas Petazzoni val = readl_relaxed(XOR_INTR_MASK(chan)); 6161ba151cdSJoe Perches dev_err(mv_chan_to_devp(chan), "intr mask 0x%08x\n", val); 617ff7b0479SSaeed Bishara 6185733c38aSThomas Petazzoni val = readl_relaxed(XOR_ERROR_CAUSE(chan)); 6191ba151cdSJoe Perches dev_err(mv_chan_to_devp(chan), "error cause 0x%08x\n", val); 620ff7b0479SSaeed Bishara 6215733c38aSThomas Petazzoni val = readl_relaxed(XOR_ERROR_ADDR(chan)); 6221ba151cdSJoe Perches dev_err(mv_chan_to_devp(chan), "error addr 0x%08x\n", val); 623ff7b0479SSaeed Bishara } 624ff7b0479SSaeed Bishara 625ff7b0479SSaeed Bishara static void mv_xor_err_interrupt_handler(struct mv_xor_chan *chan, 626ff7b0479SSaeed Bishara u32 intr_cause) 627ff7b0479SSaeed Bishara { 6280e7488edSEzequiel Garcia if (intr_cause & XOR_INT_ERR_DECODE) { 6290e7488edSEzequiel Garcia dev_dbg(mv_chan_to_devp(chan), "ignoring address decode error\n"); 630ff7b0479SSaeed Bishara return; 631ff7b0479SSaeed Bishara } 632ff7b0479SSaeed Bishara 6330e7488edSEzequiel Garcia dev_err(mv_chan_to_devp(chan), "error on chan %d. intr cause 0x%08x\n", 634ff7b0479SSaeed Bishara chan->idx, intr_cause); 635ff7b0479SSaeed Bishara 636ff7b0479SSaeed Bishara mv_dump_xor_regs(chan); 6370e7488edSEzequiel Garcia WARN_ON(1); 638ff7b0479SSaeed Bishara } 639ff7b0479SSaeed Bishara 640ff7b0479SSaeed Bishara static irqreturn_t mv_xor_interrupt_handler(int irq, void *data) 641ff7b0479SSaeed Bishara { 642ff7b0479SSaeed Bishara struct mv_xor_chan *chan = data; 643ff7b0479SSaeed Bishara u32 intr_cause = mv_chan_get_intr_cause(chan); 644ff7b0479SSaeed Bishara 645c98c1781SThomas Petazzoni dev_dbg(mv_chan_to_devp(chan), "intr cause %x\n", intr_cause); 646ff7b0479SSaeed Bishara 6470e7488edSEzequiel Garcia if (intr_cause & XOR_INTR_ERRORS) 648ff7b0479SSaeed Bishara mv_xor_err_interrupt_handler(chan, intr_cause); 649ff7b0479SSaeed Bishara 650ff7b0479SSaeed Bishara tasklet_schedule(&chan->irq_tasklet); 651ff7b0479SSaeed Bishara 652ff7b0479SSaeed Bishara mv_xor_device_clear_eoc_cause(chan); 653ff7b0479SSaeed Bishara 654ff7b0479SSaeed Bishara return IRQ_HANDLED; 655ff7b0479SSaeed Bishara } 656ff7b0479SSaeed Bishara 657ff7b0479SSaeed Bishara static void mv_xor_issue_pending(struct dma_chan *chan) 658ff7b0479SSaeed Bishara { 659ff7b0479SSaeed Bishara struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan); 660ff7b0479SSaeed Bishara 661ff7b0479SSaeed Bishara if (mv_chan->pending >= MV_XOR_THRESHOLD) { 662ff7b0479SSaeed Bishara mv_chan->pending = 0; 663ff7b0479SSaeed Bishara mv_chan_activate(mv_chan); 664ff7b0479SSaeed Bishara } 665ff7b0479SSaeed Bishara } 666ff7b0479SSaeed Bishara 667ff7b0479SSaeed Bishara /* 668ff7b0479SSaeed Bishara * Perform a transaction to verify the HW works. 669ff7b0479SSaeed Bishara */ 670ff7b0479SSaeed Bishara 671c2714334SLinus Torvalds static int mv_xor_memcpy_self_test(struct mv_xor_chan *mv_chan) 672ff7b0479SSaeed Bishara { 673ff7b0479SSaeed Bishara int i; 674ff7b0479SSaeed Bishara void *src, *dest; 675ff7b0479SSaeed Bishara dma_addr_t src_dma, dest_dma; 676ff7b0479SSaeed Bishara struct dma_chan *dma_chan; 677ff7b0479SSaeed Bishara dma_cookie_t cookie; 678ff7b0479SSaeed Bishara struct dma_async_tx_descriptor *tx; 679d16695a7SEzequiel Garcia struct dmaengine_unmap_data *unmap; 680ff7b0479SSaeed Bishara int err = 0; 681ff7b0479SSaeed Bishara 682d16695a7SEzequiel Garcia src = kmalloc(sizeof(u8) * PAGE_SIZE, GFP_KERNEL); 683ff7b0479SSaeed Bishara if (!src) 684ff7b0479SSaeed Bishara return -ENOMEM; 685ff7b0479SSaeed Bishara 686d16695a7SEzequiel Garcia dest = kzalloc(sizeof(u8) * PAGE_SIZE, GFP_KERNEL); 687ff7b0479SSaeed Bishara if (!dest) { 688ff7b0479SSaeed Bishara kfree(src); 689ff7b0479SSaeed Bishara return -ENOMEM; 690ff7b0479SSaeed Bishara } 691ff7b0479SSaeed Bishara 692ff7b0479SSaeed Bishara /* Fill in src buffer */ 693d16695a7SEzequiel Garcia for (i = 0; i < PAGE_SIZE; i++) 694ff7b0479SSaeed Bishara ((u8 *) src)[i] = (u8)i; 695ff7b0479SSaeed Bishara 696275cc0c8SThomas Petazzoni dma_chan = &mv_chan->dmachan; 697aa1e6f1aSDan Williams if (mv_xor_alloc_chan_resources(dma_chan) < 1) { 698ff7b0479SSaeed Bishara err = -ENODEV; 699ff7b0479SSaeed Bishara goto out; 700ff7b0479SSaeed Bishara } 701ff7b0479SSaeed Bishara 702d16695a7SEzequiel Garcia unmap = dmaengine_get_unmap_data(dma_chan->device->dev, 2, GFP_KERNEL); 703d16695a7SEzequiel Garcia if (!unmap) { 704d16695a7SEzequiel Garcia err = -ENOMEM; 705d16695a7SEzequiel Garcia goto free_resources; 706d16695a7SEzequiel Garcia } 707ff7b0479SSaeed Bishara 708d16695a7SEzequiel Garcia src_dma = dma_map_page(dma_chan->device->dev, virt_to_page(src), 0, 709d16695a7SEzequiel Garcia PAGE_SIZE, DMA_TO_DEVICE); 710d16695a7SEzequiel Garcia unmap->to_cnt = 1; 711d16695a7SEzequiel Garcia unmap->addr[0] = src_dma; 712d16695a7SEzequiel Garcia 713d16695a7SEzequiel Garcia dest_dma = dma_map_page(dma_chan->device->dev, virt_to_page(dest), 0, 714d16695a7SEzequiel Garcia PAGE_SIZE, DMA_FROM_DEVICE); 715d16695a7SEzequiel Garcia unmap->from_cnt = 1; 716d16695a7SEzequiel Garcia unmap->addr[1] = dest_dma; 717d16695a7SEzequiel Garcia 718d16695a7SEzequiel Garcia unmap->len = PAGE_SIZE; 719ff7b0479SSaeed Bishara 720ff7b0479SSaeed Bishara tx = mv_xor_prep_dma_memcpy(dma_chan, dest_dma, src_dma, 721d16695a7SEzequiel Garcia PAGE_SIZE, 0); 722ff7b0479SSaeed Bishara cookie = mv_xor_tx_submit(tx); 723ff7b0479SSaeed Bishara mv_xor_issue_pending(dma_chan); 724ff7b0479SSaeed Bishara async_tx_ack(tx); 725ff7b0479SSaeed Bishara msleep(1); 726ff7b0479SSaeed Bishara 72707934481SLinus Walleij if (mv_xor_status(dma_chan, cookie, NULL) != 728b3efb8fcSVinod Koul DMA_COMPLETE) { 729a3fc74bcSThomas Petazzoni dev_err(dma_chan->device->dev, 730ff7b0479SSaeed Bishara "Self-test copy timed out, disabling\n"); 731ff7b0479SSaeed Bishara err = -ENODEV; 732ff7b0479SSaeed Bishara goto free_resources; 733ff7b0479SSaeed Bishara } 734ff7b0479SSaeed Bishara 735c35064c4SThomas Petazzoni dma_sync_single_for_cpu(dma_chan->device->dev, dest_dma, 736d16695a7SEzequiel Garcia PAGE_SIZE, DMA_FROM_DEVICE); 737d16695a7SEzequiel Garcia if (memcmp(src, dest, PAGE_SIZE)) { 738a3fc74bcSThomas Petazzoni dev_err(dma_chan->device->dev, 739ff7b0479SSaeed Bishara "Self-test copy failed compare, disabling\n"); 740ff7b0479SSaeed Bishara err = -ENODEV; 741ff7b0479SSaeed Bishara goto free_resources; 742ff7b0479SSaeed Bishara } 743ff7b0479SSaeed Bishara 744ff7b0479SSaeed Bishara free_resources: 745d16695a7SEzequiel Garcia dmaengine_unmap_put(unmap); 746ff7b0479SSaeed Bishara mv_xor_free_chan_resources(dma_chan); 747ff7b0479SSaeed Bishara out: 748ff7b0479SSaeed Bishara kfree(src); 749ff7b0479SSaeed Bishara kfree(dest); 750ff7b0479SSaeed Bishara return err; 751ff7b0479SSaeed Bishara } 752ff7b0479SSaeed Bishara 753ff7b0479SSaeed Bishara #define MV_XOR_NUM_SRC_TEST 4 /* must be <= 15 */ 754463a1f8bSBill Pemberton static int 755275cc0c8SThomas Petazzoni mv_xor_xor_self_test(struct mv_xor_chan *mv_chan) 756ff7b0479SSaeed Bishara { 757ff7b0479SSaeed Bishara int i, src_idx; 758ff7b0479SSaeed Bishara struct page *dest; 759ff7b0479SSaeed Bishara struct page *xor_srcs[MV_XOR_NUM_SRC_TEST]; 760ff7b0479SSaeed Bishara dma_addr_t dma_srcs[MV_XOR_NUM_SRC_TEST]; 761ff7b0479SSaeed Bishara dma_addr_t dest_dma; 762ff7b0479SSaeed Bishara struct dma_async_tx_descriptor *tx; 763d16695a7SEzequiel Garcia struct dmaengine_unmap_data *unmap; 764ff7b0479SSaeed Bishara struct dma_chan *dma_chan; 765ff7b0479SSaeed Bishara dma_cookie_t cookie; 766ff7b0479SSaeed Bishara u8 cmp_byte = 0; 767ff7b0479SSaeed Bishara u32 cmp_word; 768ff7b0479SSaeed Bishara int err = 0; 769d16695a7SEzequiel Garcia int src_count = MV_XOR_NUM_SRC_TEST; 770ff7b0479SSaeed Bishara 771d16695a7SEzequiel Garcia for (src_idx = 0; src_idx < src_count; src_idx++) { 772ff7b0479SSaeed Bishara xor_srcs[src_idx] = alloc_page(GFP_KERNEL); 773a09b09aeSRoel Kluin if (!xor_srcs[src_idx]) { 774a09b09aeSRoel Kluin while (src_idx--) 775ff7b0479SSaeed Bishara __free_page(xor_srcs[src_idx]); 776ff7b0479SSaeed Bishara return -ENOMEM; 777ff7b0479SSaeed Bishara } 778ff7b0479SSaeed Bishara } 779ff7b0479SSaeed Bishara 780ff7b0479SSaeed Bishara dest = alloc_page(GFP_KERNEL); 781a09b09aeSRoel Kluin if (!dest) { 782a09b09aeSRoel Kluin while (src_idx--) 783ff7b0479SSaeed Bishara __free_page(xor_srcs[src_idx]); 784ff7b0479SSaeed Bishara return -ENOMEM; 785ff7b0479SSaeed Bishara } 786ff7b0479SSaeed Bishara 787ff7b0479SSaeed Bishara /* Fill in src buffers */ 788d16695a7SEzequiel Garcia for (src_idx = 0; src_idx < src_count; src_idx++) { 789ff7b0479SSaeed Bishara u8 *ptr = page_address(xor_srcs[src_idx]); 790ff7b0479SSaeed Bishara for (i = 0; i < PAGE_SIZE; i++) 791ff7b0479SSaeed Bishara ptr[i] = (1 << src_idx); 792ff7b0479SSaeed Bishara } 793ff7b0479SSaeed Bishara 794d16695a7SEzequiel Garcia for (src_idx = 0; src_idx < src_count; src_idx++) 795ff7b0479SSaeed Bishara cmp_byte ^= (u8) (1 << src_idx); 796ff7b0479SSaeed Bishara 797ff7b0479SSaeed Bishara cmp_word = (cmp_byte << 24) | (cmp_byte << 16) | 798ff7b0479SSaeed Bishara (cmp_byte << 8) | cmp_byte; 799ff7b0479SSaeed Bishara 800ff7b0479SSaeed Bishara memset(page_address(dest), 0, PAGE_SIZE); 801ff7b0479SSaeed Bishara 802275cc0c8SThomas Petazzoni dma_chan = &mv_chan->dmachan; 803aa1e6f1aSDan Williams if (mv_xor_alloc_chan_resources(dma_chan) < 1) { 804ff7b0479SSaeed Bishara err = -ENODEV; 805ff7b0479SSaeed Bishara goto out; 806ff7b0479SSaeed Bishara } 807ff7b0479SSaeed Bishara 808d16695a7SEzequiel Garcia unmap = dmaengine_get_unmap_data(dma_chan->device->dev, src_count + 1, 809d16695a7SEzequiel Garcia GFP_KERNEL); 810d16695a7SEzequiel Garcia if (!unmap) { 811d16695a7SEzequiel Garcia err = -ENOMEM; 812d16695a7SEzequiel Garcia goto free_resources; 813d16695a7SEzequiel Garcia } 814ff7b0479SSaeed Bishara 815d16695a7SEzequiel Garcia /* test xor */ 816d16695a7SEzequiel Garcia for (i = 0; i < src_count; i++) { 817d16695a7SEzequiel Garcia unmap->addr[i] = dma_map_page(dma_chan->device->dev, xor_srcs[i], 818ff7b0479SSaeed Bishara 0, PAGE_SIZE, DMA_TO_DEVICE); 819d16695a7SEzequiel Garcia dma_srcs[i] = unmap->addr[i]; 820d16695a7SEzequiel Garcia unmap->to_cnt++; 821d16695a7SEzequiel Garcia } 822d16695a7SEzequiel Garcia 823d16695a7SEzequiel Garcia unmap->addr[src_count] = dma_map_page(dma_chan->device->dev, dest, 0, PAGE_SIZE, 824d16695a7SEzequiel Garcia DMA_FROM_DEVICE); 825d16695a7SEzequiel Garcia dest_dma = unmap->addr[src_count]; 826d16695a7SEzequiel Garcia unmap->from_cnt = 1; 827d16695a7SEzequiel Garcia unmap->len = PAGE_SIZE; 828ff7b0479SSaeed Bishara 829ff7b0479SSaeed Bishara tx = mv_xor_prep_dma_xor(dma_chan, dest_dma, dma_srcs, 830d16695a7SEzequiel Garcia src_count, PAGE_SIZE, 0); 831ff7b0479SSaeed Bishara 832ff7b0479SSaeed Bishara cookie = mv_xor_tx_submit(tx); 833ff7b0479SSaeed Bishara mv_xor_issue_pending(dma_chan); 834ff7b0479SSaeed Bishara async_tx_ack(tx); 835ff7b0479SSaeed Bishara msleep(8); 836ff7b0479SSaeed Bishara 83707934481SLinus Walleij if (mv_xor_status(dma_chan, cookie, NULL) != 838b3efb8fcSVinod Koul DMA_COMPLETE) { 839a3fc74bcSThomas Petazzoni dev_err(dma_chan->device->dev, 840ff7b0479SSaeed Bishara "Self-test xor timed out, disabling\n"); 841ff7b0479SSaeed Bishara err = -ENODEV; 842ff7b0479SSaeed Bishara goto free_resources; 843ff7b0479SSaeed Bishara } 844ff7b0479SSaeed Bishara 845c35064c4SThomas Petazzoni dma_sync_single_for_cpu(dma_chan->device->dev, dest_dma, 846ff7b0479SSaeed Bishara PAGE_SIZE, DMA_FROM_DEVICE); 847ff7b0479SSaeed Bishara for (i = 0; i < (PAGE_SIZE / sizeof(u32)); i++) { 848ff7b0479SSaeed Bishara u32 *ptr = page_address(dest); 849ff7b0479SSaeed Bishara if (ptr[i] != cmp_word) { 850a3fc74bcSThomas Petazzoni dev_err(dma_chan->device->dev, 8511ba151cdSJoe Perches "Self-test xor failed compare, disabling. index %d, data %x, expected %x\n", 8521ba151cdSJoe Perches i, ptr[i], cmp_word); 853ff7b0479SSaeed Bishara err = -ENODEV; 854ff7b0479SSaeed Bishara goto free_resources; 855ff7b0479SSaeed Bishara } 856ff7b0479SSaeed Bishara } 857ff7b0479SSaeed Bishara 858ff7b0479SSaeed Bishara free_resources: 859d16695a7SEzequiel Garcia dmaengine_unmap_put(unmap); 860ff7b0479SSaeed Bishara mv_xor_free_chan_resources(dma_chan); 861ff7b0479SSaeed Bishara out: 862d16695a7SEzequiel Garcia src_idx = src_count; 863ff7b0479SSaeed Bishara while (src_idx--) 864ff7b0479SSaeed Bishara __free_page(xor_srcs[src_idx]); 865ff7b0479SSaeed Bishara __free_page(dest); 866ff7b0479SSaeed Bishara return err; 867ff7b0479SSaeed Bishara } 868ff7b0479SSaeed Bishara 86934c93c86SAndrew Lunn /* This driver does not implement any of the optional DMA operations. */ 87034c93c86SAndrew Lunn static int 87134c93c86SAndrew Lunn mv_xor_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd, 87234c93c86SAndrew Lunn unsigned long arg) 873ff7b0479SSaeed Bishara { 87434c93c86SAndrew Lunn return -ENOSYS; 87534c93c86SAndrew Lunn } 87634c93c86SAndrew Lunn 8771ef48a26SThomas Petazzoni static int mv_xor_channel_remove(struct mv_xor_chan *mv_chan) 878ff7b0479SSaeed Bishara { 879ff7b0479SSaeed Bishara struct dma_chan *chan, *_chan; 8801ef48a26SThomas Petazzoni struct device *dev = mv_chan->dmadev.dev; 881ff7b0479SSaeed Bishara 8821ef48a26SThomas Petazzoni dma_async_device_unregister(&mv_chan->dmadev); 883ff7b0479SSaeed Bishara 884b503fa01SThomas Petazzoni dma_free_coherent(dev, MV_XOR_POOL_SIZE, 8851ef48a26SThomas Petazzoni mv_chan->dma_desc_pool_virt, mv_chan->dma_desc_pool); 886ff7b0479SSaeed Bishara 8871ef48a26SThomas Petazzoni list_for_each_entry_safe(chan, _chan, &mv_chan->dmadev.channels, 888ff7b0479SSaeed Bishara device_node) { 889ff7b0479SSaeed Bishara list_del(&chan->device_node); 890ff7b0479SSaeed Bishara } 891ff7b0479SSaeed Bishara 89288eb92cbSThomas Petazzoni free_irq(mv_chan->irq, mv_chan); 89388eb92cbSThomas Petazzoni 894ff7b0479SSaeed Bishara return 0; 895ff7b0479SSaeed Bishara } 896ff7b0479SSaeed Bishara 8971ef48a26SThomas Petazzoni static struct mv_xor_chan * 898297eedbaSThomas Petazzoni mv_xor_channel_add(struct mv_xor_device *xordev, 899a6b4a9d2SThomas Petazzoni struct platform_device *pdev, 900b503fa01SThomas Petazzoni int idx, dma_cap_mask_t cap_mask, int irq) 901ff7b0479SSaeed Bishara { 902ff7b0479SSaeed Bishara int ret = 0; 903ff7b0479SSaeed Bishara struct mv_xor_chan *mv_chan; 904ff7b0479SSaeed Bishara struct dma_device *dma_dev; 905ff7b0479SSaeed Bishara 9061ef48a26SThomas Petazzoni mv_chan = devm_kzalloc(&pdev->dev, sizeof(*mv_chan), GFP_KERNEL); 907a577659fSSachin Kamat if (!mv_chan) 908a577659fSSachin Kamat return ERR_PTR(-ENOMEM); 909ff7b0479SSaeed Bishara 9109aedbdbaSThomas Petazzoni mv_chan->idx = idx; 91188eb92cbSThomas Petazzoni mv_chan->irq = irq; 912ff7b0479SSaeed Bishara 9131ef48a26SThomas Petazzoni dma_dev = &mv_chan->dmadev; 914ff7b0479SSaeed Bishara 915ff7b0479SSaeed Bishara /* allocate coherent memory for hardware descriptors 916ff7b0479SSaeed Bishara * note: writecombine gives slightly better performance, but 917ff7b0479SSaeed Bishara * requires that we explicitly flush the writes 918ff7b0479SSaeed Bishara */ 9191ef48a26SThomas Petazzoni mv_chan->dma_desc_pool_virt = 920b503fa01SThomas Petazzoni dma_alloc_writecombine(&pdev->dev, MV_XOR_POOL_SIZE, 9211ef48a26SThomas Petazzoni &mv_chan->dma_desc_pool, GFP_KERNEL); 9221ef48a26SThomas Petazzoni if (!mv_chan->dma_desc_pool_virt) 923a6b4a9d2SThomas Petazzoni return ERR_PTR(-ENOMEM); 924ff7b0479SSaeed Bishara 925ff7b0479SSaeed Bishara /* discover transaction capabilites from the platform data */ 926a6b4a9d2SThomas Petazzoni dma_dev->cap_mask = cap_mask; 927ff7b0479SSaeed Bishara 928ff7b0479SSaeed Bishara INIT_LIST_HEAD(&dma_dev->channels); 929ff7b0479SSaeed Bishara 930ff7b0479SSaeed Bishara /* set base routines */ 931ff7b0479SSaeed Bishara dma_dev->device_alloc_chan_resources = mv_xor_alloc_chan_resources; 932ff7b0479SSaeed Bishara dma_dev->device_free_chan_resources = mv_xor_free_chan_resources; 93307934481SLinus Walleij dma_dev->device_tx_status = mv_xor_status; 934ff7b0479SSaeed Bishara dma_dev->device_issue_pending = mv_xor_issue_pending; 93534c93c86SAndrew Lunn dma_dev->device_control = mv_xor_control; 936ff7b0479SSaeed Bishara dma_dev->dev = &pdev->dev; 937ff7b0479SSaeed Bishara 938ff7b0479SSaeed Bishara /* set prep routines based on capability */ 939ff7b0479SSaeed Bishara if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask)) 940ff7b0479SSaeed Bishara dma_dev->device_prep_dma_memcpy = mv_xor_prep_dma_memcpy; 941ff7b0479SSaeed Bishara if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) { 942c019894eSJoe Perches dma_dev->max_xor = 8; 943ff7b0479SSaeed Bishara dma_dev->device_prep_dma_xor = mv_xor_prep_dma_xor; 944ff7b0479SSaeed Bishara } 945ff7b0479SSaeed Bishara 946297eedbaSThomas Petazzoni mv_chan->mmr_base = xordev->xor_base; 94782a1402eSEzequiel Garcia mv_chan->mmr_high_base = xordev->xor_high_base; 948ff7b0479SSaeed Bishara tasklet_init(&mv_chan->irq_tasklet, mv_xor_tasklet, (unsigned long) 949ff7b0479SSaeed Bishara mv_chan); 950ff7b0479SSaeed Bishara 951ff7b0479SSaeed Bishara /* clear errors before enabling interrupts */ 952ff7b0479SSaeed Bishara mv_xor_device_clear_err_status(mv_chan); 953ff7b0479SSaeed Bishara 9542d0a0745SThomas Petazzoni ret = request_irq(mv_chan->irq, mv_xor_interrupt_handler, 955ff7b0479SSaeed Bishara 0, dev_name(&pdev->dev), mv_chan); 956ff7b0479SSaeed Bishara if (ret) 957ff7b0479SSaeed Bishara goto err_free_dma; 958ff7b0479SSaeed Bishara 959ff7b0479SSaeed Bishara mv_chan_unmask_interrupts(mv_chan); 960ff7b0479SSaeed Bishara 9613e4f52e2SLior Amsalem mv_set_mode(mv_chan, DMA_XOR); 962ff7b0479SSaeed Bishara 963ff7b0479SSaeed Bishara spin_lock_init(&mv_chan->lock); 964ff7b0479SSaeed Bishara INIT_LIST_HEAD(&mv_chan->chain); 965ff7b0479SSaeed Bishara INIT_LIST_HEAD(&mv_chan->completed_slots); 966ff7b0479SSaeed Bishara INIT_LIST_HEAD(&mv_chan->all_slots); 96798817b99SThomas Petazzoni mv_chan->dmachan.device = dma_dev; 96898817b99SThomas Petazzoni dma_cookie_init(&mv_chan->dmachan); 969ff7b0479SSaeed Bishara 97098817b99SThomas Petazzoni list_add_tail(&mv_chan->dmachan.device_node, &dma_dev->channels); 971ff7b0479SSaeed Bishara 972ff7b0479SSaeed Bishara if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask)) { 973275cc0c8SThomas Petazzoni ret = mv_xor_memcpy_self_test(mv_chan); 974ff7b0479SSaeed Bishara dev_dbg(&pdev->dev, "memcpy self test returned %d\n", ret); 975ff7b0479SSaeed Bishara if (ret) 9762d0a0745SThomas Petazzoni goto err_free_irq; 977ff7b0479SSaeed Bishara } 978ff7b0479SSaeed Bishara 979ff7b0479SSaeed Bishara if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) { 980275cc0c8SThomas Petazzoni ret = mv_xor_xor_self_test(mv_chan); 981ff7b0479SSaeed Bishara dev_dbg(&pdev->dev, "xor self test returned %d\n", ret); 982ff7b0479SSaeed Bishara if (ret) 9832d0a0745SThomas Petazzoni goto err_free_irq; 984ff7b0479SSaeed Bishara } 985ff7b0479SSaeed Bishara 98648a9db46SBartlomiej Zolnierkiewicz dev_info(&pdev->dev, "Marvell XOR: ( %s%s%s)\n", 987ff7b0479SSaeed Bishara dma_has_cap(DMA_XOR, dma_dev->cap_mask) ? "xor " : "", 988ff7b0479SSaeed Bishara dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask) ? "cpy " : "", 989ff7b0479SSaeed Bishara dma_has_cap(DMA_INTERRUPT, dma_dev->cap_mask) ? "intr " : ""); 990ff7b0479SSaeed Bishara 991ff7b0479SSaeed Bishara dma_async_device_register(dma_dev); 9921ef48a26SThomas Petazzoni return mv_chan; 993ff7b0479SSaeed Bishara 9942d0a0745SThomas Petazzoni err_free_irq: 9952d0a0745SThomas Petazzoni free_irq(mv_chan->irq, mv_chan); 996ff7b0479SSaeed Bishara err_free_dma: 997b503fa01SThomas Petazzoni dma_free_coherent(&pdev->dev, MV_XOR_POOL_SIZE, 9981ef48a26SThomas Petazzoni mv_chan->dma_desc_pool_virt, mv_chan->dma_desc_pool); 999a6b4a9d2SThomas Petazzoni return ERR_PTR(ret); 1000ff7b0479SSaeed Bishara } 1001ff7b0479SSaeed Bishara 1002ff7b0479SSaeed Bishara static void 1003297eedbaSThomas Petazzoni mv_xor_conf_mbus_windows(struct mv_xor_device *xordev, 100463a9332bSAndrew Lunn const struct mbus_dram_target_info *dram) 1005ff7b0479SSaeed Bishara { 100682a1402eSEzequiel Garcia void __iomem *base = xordev->xor_high_base; 1007ff7b0479SSaeed Bishara u32 win_enable = 0; 1008ff7b0479SSaeed Bishara int i; 1009ff7b0479SSaeed Bishara 1010ff7b0479SSaeed Bishara for (i = 0; i < 8; i++) { 1011ff7b0479SSaeed Bishara writel(0, base + WINDOW_BASE(i)); 1012ff7b0479SSaeed Bishara writel(0, base + WINDOW_SIZE(i)); 1013ff7b0479SSaeed Bishara if (i < 4) 1014ff7b0479SSaeed Bishara writel(0, base + WINDOW_REMAP_HIGH(i)); 1015ff7b0479SSaeed Bishara } 1016ff7b0479SSaeed Bishara 1017ff7b0479SSaeed Bishara for (i = 0; i < dram->num_cs; i++) { 101863a9332bSAndrew Lunn const struct mbus_dram_window *cs = dram->cs + i; 1019ff7b0479SSaeed Bishara 1020ff7b0479SSaeed Bishara writel((cs->base & 0xffff0000) | 1021ff7b0479SSaeed Bishara (cs->mbus_attr << 8) | 1022ff7b0479SSaeed Bishara dram->mbus_dram_target_id, base + WINDOW_BASE(i)); 1023ff7b0479SSaeed Bishara writel((cs->size - 1) & 0xffff0000, base + WINDOW_SIZE(i)); 1024ff7b0479SSaeed Bishara 1025ff7b0479SSaeed Bishara win_enable |= (1 << i); 1026ff7b0479SSaeed Bishara win_enable |= 3 << (16 + (2 * i)); 1027ff7b0479SSaeed Bishara } 1028ff7b0479SSaeed Bishara 1029ff7b0479SSaeed Bishara writel(win_enable, base + WINDOW_BAR_ENABLE(0)); 1030ff7b0479SSaeed Bishara writel(win_enable, base + WINDOW_BAR_ENABLE(1)); 1031c4b4b732SThomas Petazzoni writel(0, base + WINDOW_OVERRIDE_CTRL(0)); 1032c4b4b732SThomas Petazzoni writel(0, base + WINDOW_OVERRIDE_CTRL(1)); 1033ff7b0479SSaeed Bishara } 1034ff7b0479SSaeed Bishara 1035c2714334SLinus Torvalds static int mv_xor_probe(struct platform_device *pdev) 1036ff7b0479SSaeed Bishara { 103763a9332bSAndrew Lunn const struct mbus_dram_target_info *dram; 1038297eedbaSThomas Petazzoni struct mv_xor_device *xordev; 1039d4adcc01SJingoo Han struct mv_xor_platform_data *pdata = dev_get_platdata(&pdev->dev); 1040ff7b0479SSaeed Bishara struct resource *res; 104160d151f3SThomas Petazzoni int i, ret; 1042ff7b0479SSaeed Bishara 10431ba151cdSJoe Perches dev_notice(&pdev->dev, "Marvell shared XOR driver\n"); 1044ff7b0479SSaeed Bishara 1045297eedbaSThomas Petazzoni xordev = devm_kzalloc(&pdev->dev, sizeof(*xordev), GFP_KERNEL); 1046297eedbaSThomas Petazzoni if (!xordev) 1047ff7b0479SSaeed Bishara return -ENOMEM; 1048ff7b0479SSaeed Bishara 1049ff7b0479SSaeed Bishara res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1050ff7b0479SSaeed Bishara if (!res) 1051ff7b0479SSaeed Bishara return -ENODEV; 1052ff7b0479SSaeed Bishara 1053297eedbaSThomas Petazzoni xordev->xor_base = devm_ioremap(&pdev->dev, res->start, 10544de1ba15SH Hartley Sweeten resource_size(res)); 1055297eedbaSThomas Petazzoni if (!xordev->xor_base) 1056ff7b0479SSaeed Bishara return -EBUSY; 1057ff7b0479SSaeed Bishara 1058ff7b0479SSaeed Bishara res = platform_get_resource(pdev, IORESOURCE_MEM, 1); 1059ff7b0479SSaeed Bishara if (!res) 1060ff7b0479SSaeed Bishara return -ENODEV; 1061ff7b0479SSaeed Bishara 1062297eedbaSThomas Petazzoni xordev->xor_high_base = devm_ioremap(&pdev->dev, res->start, 10634de1ba15SH Hartley Sweeten resource_size(res)); 1064297eedbaSThomas Petazzoni if (!xordev->xor_high_base) 1065ff7b0479SSaeed Bishara return -EBUSY; 1066ff7b0479SSaeed Bishara 1067297eedbaSThomas Petazzoni platform_set_drvdata(pdev, xordev); 1068ff7b0479SSaeed Bishara 1069ff7b0479SSaeed Bishara /* 1070ff7b0479SSaeed Bishara * (Re-)program MBUS remapping windows if we are asked to. 1071ff7b0479SSaeed Bishara */ 107263a9332bSAndrew Lunn dram = mv_mbus_dram_info(); 107363a9332bSAndrew Lunn if (dram) 1074297eedbaSThomas Petazzoni mv_xor_conf_mbus_windows(xordev, dram); 1075ff7b0479SSaeed Bishara 1076c510182bSAndrew Lunn /* Not all platforms can gate the clock, so it is not 1077c510182bSAndrew Lunn * an error if the clock does not exists. 1078c510182bSAndrew Lunn */ 1079297eedbaSThomas Petazzoni xordev->clk = clk_get(&pdev->dev, NULL); 1080297eedbaSThomas Petazzoni if (!IS_ERR(xordev->clk)) 1081297eedbaSThomas Petazzoni clk_prepare_enable(xordev->clk); 1082c510182bSAndrew Lunn 1083f7d12ef5SThomas Petazzoni if (pdev->dev.of_node) { 1084f7d12ef5SThomas Petazzoni struct device_node *np; 1085f7d12ef5SThomas Petazzoni int i = 0; 1086f7d12ef5SThomas Petazzoni 1087f7d12ef5SThomas Petazzoni for_each_child_of_node(pdev->dev.of_node, np) { 10880be8253fSRussell King struct mv_xor_chan *chan; 1089f7d12ef5SThomas Petazzoni dma_cap_mask_t cap_mask; 1090f7d12ef5SThomas Petazzoni int irq; 1091f7d12ef5SThomas Petazzoni 1092f7d12ef5SThomas Petazzoni dma_cap_zero(cap_mask); 1093f7d12ef5SThomas Petazzoni if (of_property_read_bool(np, "dmacap,memcpy")) 1094f7d12ef5SThomas Petazzoni dma_cap_set(DMA_MEMCPY, cap_mask); 1095f7d12ef5SThomas Petazzoni if (of_property_read_bool(np, "dmacap,xor")) 1096f7d12ef5SThomas Petazzoni dma_cap_set(DMA_XOR, cap_mask); 1097f7d12ef5SThomas Petazzoni if (of_property_read_bool(np, "dmacap,interrupt")) 1098f7d12ef5SThomas Petazzoni dma_cap_set(DMA_INTERRUPT, cap_mask); 1099f7d12ef5SThomas Petazzoni 1100f7d12ef5SThomas Petazzoni irq = irq_of_parse_and_map(np, 0); 1101f8eb9e7dSThomas Petazzoni if (!irq) { 1102f8eb9e7dSThomas Petazzoni ret = -ENODEV; 1103f7d12ef5SThomas Petazzoni goto err_channel_add; 1104f7d12ef5SThomas Petazzoni } 1105f7d12ef5SThomas Petazzoni 11060be8253fSRussell King chan = mv_xor_channel_add(xordev, pdev, i, 1107f7d12ef5SThomas Petazzoni cap_mask, irq); 11080be8253fSRussell King if (IS_ERR(chan)) { 11090be8253fSRussell King ret = PTR_ERR(chan); 1110f7d12ef5SThomas Petazzoni irq_dispose_mapping(irq); 1111f7d12ef5SThomas Petazzoni goto err_channel_add; 1112f7d12ef5SThomas Petazzoni } 1113f7d12ef5SThomas Petazzoni 11140be8253fSRussell King xordev->channels[i] = chan; 1115f7d12ef5SThomas Petazzoni i++; 1116f7d12ef5SThomas Petazzoni } 1117f7d12ef5SThomas Petazzoni } else if (pdata && pdata->channels) { 111860d151f3SThomas Petazzoni for (i = 0; i < MV_XOR_MAX_CHANNELS; i++) { 1119e39f6ec1SThomas Petazzoni struct mv_xor_channel_data *cd; 11200be8253fSRussell King struct mv_xor_chan *chan; 112160d151f3SThomas Petazzoni int irq; 112260d151f3SThomas Petazzoni 112360d151f3SThomas Petazzoni cd = &pdata->channels[i]; 112460d151f3SThomas Petazzoni if (!cd) { 112560d151f3SThomas Petazzoni ret = -ENODEV; 112660d151f3SThomas Petazzoni goto err_channel_add; 112760d151f3SThomas Petazzoni } 112860d151f3SThomas Petazzoni 112960d151f3SThomas Petazzoni irq = platform_get_irq(pdev, i); 113060d151f3SThomas Petazzoni if (irq < 0) { 113160d151f3SThomas Petazzoni ret = irq; 113260d151f3SThomas Petazzoni goto err_channel_add; 113360d151f3SThomas Petazzoni } 113460d151f3SThomas Petazzoni 11350be8253fSRussell King chan = mv_xor_channel_add(xordev, pdev, i, 1136b503fa01SThomas Petazzoni cd->cap_mask, irq); 11370be8253fSRussell King if (IS_ERR(chan)) { 11380be8253fSRussell King ret = PTR_ERR(chan); 113960d151f3SThomas Petazzoni goto err_channel_add; 114060d151f3SThomas Petazzoni } 11410be8253fSRussell King 11420be8253fSRussell King xordev->channels[i] = chan; 114360d151f3SThomas Petazzoni } 114460d151f3SThomas Petazzoni } 114560d151f3SThomas Petazzoni 1146ff7b0479SSaeed Bishara return 0; 114760d151f3SThomas Petazzoni 114860d151f3SThomas Petazzoni err_channel_add: 114960d151f3SThomas Petazzoni for (i = 0; i < MV_XOR_MAX_CHANNELS; i++) 1150f7d12ef5SThomas Petazzoni if (xordev->channels[i]) { 1151ab6e439fSThomas Petazzoni mv_xor_channel_remove(xordev->channels[i]); 1152f7d12ef5SThomas Petazzoni if (pdev->dev.of_node) 1153f7d12ef5SThomas Petazzoni irq_dispose_mapping(xordev->channels[i]->irq); 1154f7d12ef5SThomas Petazzoni } 115560d151f3SThomas Petazzoni 1156dab92064SThomas Petazzoni if (!IS_ERR(xordev->clk)) { 1157297eedbaSThomas Petazzoni clk_disable_unprepare(xordev->clk); 1158297eedbaSThomas Petazzoni clk_put(xordev->clk); 1159dab92064SThomas Petazzoni } 1160dab92064SThomas Petazzoni 116160d151f3SThomas Petazzoni return ret; 1162ff7b0479SSaeed Bishara } 1163ff7b0479SSaeed Bishara 1164c2714334SLinus Torvalds static int mv_xor_remove(struct platform_device *pdev) 1165ff7b0479SSaeed Bishara { 1166297eedbaSThomas Petazzoni struct mv_xor_device *xordev = platform_get_drvdata(pdev); 116760d151f3SThomas Petazzoni int i; 116860d151f3SThomas Petazzoni 116960d151f3SThomas Petazzoni for (i = 0; i < MV_XOR_MAX_CHANNELS; i++) { 1170297eedbaSThomas Petazzoni if (xordev->channels[i]) 1171297eedbaSThomas Petazzoni mv_xor_channel_remove(xordev->channels[i]); 117260d151f3SThomas Petazzoni } 1173c510182bSAndrew Lunn 1174297eedbaSThomas Petazzoni if (!IS_ERR(xordev->clk)) { 1175297eedbaSThomas Petazzoni clk_disable_unprepare(xordev->clk); 1176297eedbaSThomas Petazzoni clk_put(xordev->clk); 1177c510182bSAndrew Lunn } 1178c510182bSAndrew Lunn 1179ff7b0479SSaeed Bishara return 0; 1180ff7b0479SSaeed Bishara } 1181ff7b0479SSaeed Bishara 1182f7d12ef5SThomas Petazzoni #ifdef CONFIG_OF 1183c2714334SLinus Torvalds static struct of_device_id mv_xor_dt_ids[] = { 1184f7d12ef5SThomas Petazzoni { .compatible = "marvell,orion-xor", }, 1185f7d12ef5SThomas Petazzoni {}, 1186f7d12ef5SThomas Petazzoni }; 1187f7d12ef5SThomas Petazzoni MODULE_DEVICE_TABLE(of, mv_xor_dt_ids); 1188f7d12ef5SThomas Petazzoni #endif 1189f7d12ef5SThomas Petazzoni 1190ff7b0479SSaeed Bishara static struct platform_driver mv_xor_driver = { 1191ff7b0479SSaeed Bishara .probe = mv_xor_probe, 1192a7d6e3ecSBill Pemberton .remove = mv_xor_remove, 1193ff7b0479SSaeed Bishara .driver = { 1194ff7b0479SSaeed Bishara .owner = THIS_MODULE, 1195ff7b0479SSaeed Bishara .name = MV_XOR_NAME, 1196f7d12ef5SThomas Petazzoni .of_match_table = of_match_ptr(mv_xor_dt_ids), 1197ff7b0479SSaeed Bishara }, 1198ff7b0479SSaeed Bishara }; 1199ff7b0479SSaeed Bishara 1200ff7b0479SSaeed Bishara 1201ff7b0479SSaeed Bishara static int __init mv_xor_init(void) 1202ff7b0479SSaeed Bishara { 120361971656SThomas Petazzoni return platform_driver_register(&mv_xor_driver); 1204ff7b0479SSaeed Bishara } 1205ff7b0479SSaeed Bishara module_init(mv_xor_init); 1206ff7b0479SSaeed Bishara 1207ff7b0479SSaeed Bishara /* it's currently unsafe to unload this module */ 1208ff7b0479SSaeed Bishara #if 0 1209ff7b0479SSaeed Bishara static void __exit mv_xor_exit(void) 1210ff7b0479SSaeed Bishara { 1211ff7b0479SSaeed Bishara platform_driver_unregister(&mv_xor_driver); 1212ff7b0479SSaeed Bishara return; 1213ff7b0479SSaeed Bishara } 1214ff7b0479SSaeed Bishara 1215ff7b0479SSaeed Bishara module_exit(mv_xor_exit); 1216ff7b0479SSaeed Bishara #endif 1217ff7b0479SSaeed Bishara 1218ff7b0479SSaeed Bishara MODULE_AUTHOR("Saeed Bishara <saeed@marvell.com>"); 1219ff7b0479SSaeed Bishara MODULE_DESCRIPTION("DMA engine driver for Marvell's XOR engine"); 1220ff7b0479SSaeed Bishara MODULE_LICENSE("GPL"); 1221