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