/drivers/dma/shdma.c
C | 1214 lines | 840 code | 218 blank | 156 comment | 158 complexity | 8e46b84f1f641418459de14b7f65d5c8 MD5 | raw file
Possible License(s): LGPL-2.0, AGPL-1.0, GPL-2.0
- /*
- * Renesas SuperH DMA Engine support
- *
- * base is drivers/dma/flsdma.c
- *
- * Copyright (C) 2009 Nobuhiro Iwamatsu <iwamatsu.nobuhiro@renesas.com>
- * Copyright (C) 2009 Renesas Solutions, Inc. All rights reserved.
- * Copyright (C) 2007 Freescale Semiconductor, Inc. All rights reserved.
- *
- * This is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * - DMA of SuperH does not have Hardware DMA chain mode.
- * - MAX DMA size is 16MB.
- *
- */
- #include <linux/init.h>
- #include <linux/module.h>
- #include <linux/slab.h>
- #include <linux/interrupt.h>
- #include <linux/dmaengine.h>
- #include <linux/delay.h>
- #include <linux/dma-mapping.h>
- #include <linux/platform_device.h>
- #include <linux/pm_runtime.h>
- #include <linux/sh_dma.h>
- #include "shdma.h"
- /* DMA descriptor control */
- enum sh_dmae_desc_status {
- DESC_IDLE,
- DESC_PREPARED,
- DESC_SUBMITTED,
- DESC_COMPLETED, /* completed, have to call callback */
- DESC_WAITING, /* callback called, waiting for ack / re-submit */
- };
- #define NR_DESCS_PER_CHANNEL 32
- /* Default MEMCPY transfer size = 2^2 = 4 bytes */
- #define LOG2_DEFAULT_XFER_SIZE 2
- /* A bitmask with bits enough for enum sh_dmae_slave_chan_id */
- static unsigned long sh_dmae_slave_used[BITS_TO_LONGS(SH_DMA_SLAVE_NUMBER)];
- static void sh_dmae_chan_ld_cleanup(struct sh_dmae_chan *sh_chan, bool all);
- static void sh_dmae_writel(struct sh_dmae_chan *sh_dc, u32 data, u32 reg)
- {
- __raw_writel(data, sh_dc->base + reg / sizeof(u32));
- }
- static u32 sh_dmae_readl(struct sh_dmae_chan *sh_dc, u32 reg)
- {
- return __raw_readl(sh_dc->base + reg / sizeof(u32));
- }
- static u16 dmaor_read(struct sh_dmae_device *shdev)
- {
- return __raw_readw(shdev->chan_reg + DMAOR / sizeof(u32));
- }
- static void dmaor_write(struct sh_dmae_device *shdev, u16 data)
- {
- __raw_writew(data, shdev->chan_reg + DMAOR / sizeof(u32));
- }
- /*
- * Reset DMA controller
- *
- * SH7780 has two DMAOR register
- */
- static void sh_dmae_ctl_stop(struct sh_dmae_device *shdev)
- {
- unsigned short dmaor = dmaor_read(shdev);
- dmaor_write(shdev, dmaor & ~(DMAOR_NMIF | DMAOR_AE | DMAOR_DME));
- }
- static int sh_dmae_rst(struct sh_dmae_device *shdev)
- {
- unsigned short dmaor;
- sh_dmae_ctl_stop(shdev);
- dmaor = dmaor_read(shdev) | shdev->pdata->dmaor_init;
- dmaor_write(shdev, dmaor);
- if (dmaor_read(shdev) & (DMAOR_AE | DMAOR_NMIF)) {
- pr_warning("dma-sh: Can't initialize DMAOR.\n");
- return -EINVAL;
- }
- return 0;
- }
- static bool dmae_is_busy(struct sh_dmae_chan *sh_chan)
- {
- u32 chcr = sh_dmae_readl(sh_chan, CHCR);
- if ((chcr & (CHCR_DE | CHCR_TE)) == CHCR_DE)
- return true; /* working */
- return false; /* waiting */
- }
- static unsigned int calc_xmit_shift(struct sh_dmae_chan *sh_chan, u32 chcr)
- {
- struct sh_dmae_device *shdev = container_of(sh_chan->common.device,
- struct sh_dmae_device, common);
- struct sh_dmae_pdata *pdata = shdev->pdata;
- int cnt = ((chcr & pdata->ts_low_mask) >> pdata->ts_low_shift) |
- ((chcr & pdata->ts_high_mask) >> pdata->ts_high_shift);
- if (cnt >= pdata->ts_shift_num)
- cnt = 0;
- return pdata->ts_shift[cnt];
- }
- static u32 log2size_to_chcr(struct sh_dmae_chan *sh_chan, int l2size)
- {
- struct sh_dmae_device *shdev = container_of(sh_chan->common.device,
- struct sh_dmae_device, common);
- struct sh_dmae_pdata *pdata = shdev->pdata;
- int i;
- for (i = 0; i < pdata->ts_shift_num; i++)
- if (pdata->ts_shift[i] == l2size)
- break;
- if (i == pdata->ts_shift_num)
- i = 0;
- return ((i << pdata->ts_low_shift) & pdata->ts_low_mask) |
- ((i << pdata->ts_high_shift) & pdata->ts_high_mask);
- }
- static void dmae_set_reg(struct sh_dmae_chan *sh_chan, struct sh_dmae_regs *hw)
- {
- sh_dmae_writel(sh_chan, hw->sar, SAR);
- sh_dmae_writel(sh_chan, hw->dar, DAR);
- sh_dmae_writel(sh_chan, hw->tcr >> sh_chan->xmit_shift, TCR);
- }
- static void dmae_start(struct sh_dmae_chan *sh_chan)
- {
- u32 chcr = sh_dmae_readl(sh_chan, CHCR);
- chcr |= CHCR_DE | CHCR_IE;
- sh_dmae_writel(sh_chan, chcr & ~CHCR_TE, CHCR);
- }
- static void dmae_halt(struct sh_dmae_chan *sh_chan)
- {
- u32 chcr = sh_dmae_readl(sh_chan, CHCR);
- chcr &= ~(CHCR_DE | CHCR_TE | CHCR_IE);
- sh_dmae_writel(sh_chan, chcr, CHCR);
- }
- static void dmae_init(struct sh_dmae_chan *sh_chan)
- {
- /*
- * Default configuration for dual address memory-memory transfer.
- * 0x400 represents auto-request.
- */
- u32 chcr = DM_INC | SM_INC | 0x400 | log2size_to_chcr(sh_chan,
- LOG2_DEFAULT_XFER_SIZE);
- sh_chan->xmit_shift = calc_xmit_shift(sh_chan, chcr);
- sh_dmae_writel(sh_chan, chcr, CHCR);
- }
- static int dmae_set_chcr(struct sh_dmae_chan *sh_chan, u32 val)
- {
- /* When DMA was working, can not set data to CHCR */
- if (dmae_is_busy(sh_chan))
- return -EBUSY;
- sh_chan->xmit_shift = calc_xmit_shift(sh_chan, val);
- sh_dmae_writel(sh_chan, val, CHCR);
- return 0;
- }
- static int dmae_set_dmars(struct sh_dmae_chan *sh_chan, u16 val)
- {
- struct sh_dmae_device *shdev = container_of(sh_chan->common.device,
- struct sh_dmae_device, common);
- struct sh_dmae_pdata *pdata = shdev->pdata;
- const struct sh_dmae_channel *chan_pdata = &pdata->channel[sh_chan->id];
- u16 __iomem *addr = shdev->dmars + chan_pdata->dmars / sizeof(u16);
- int shift = chan_pdata->dmars_bit;
- if (dmae_is_busy(sh_chan))
- return -EBUSY;
- __raw_writew((__raw_readw(addr) & (0xff00 >> shift)) | (val << shift),
- addr);
- return 0;
- }
- static dma_cookie_t sh_dmae_tx_submit(struct dma_async_tx_descriptor *tx)
- {
- struct sh_desc *desc = tx_to_sh_desc(tx), *chunk, *last = desc, *c;
- struct sh_dmae_chan *sh_chan = to_sh_chan(tx->chan);
- dma_async_tx_callback callback = tx->callback;
- dma_cookie_t cookie;
- spin_lock_bh(&sh_chan->desc_lock);
- cookie = sh_chan->common.cookie;
- cookie++;
- if (cookie < 0)
- cookie = 1;
- sh_chan->common.cookie = cookie;
- tx->cookie = cookie;
- /* Mark all chunks of this descriptor as submitted, move to the queue */
- list_for_each_entry_safe(chunk, c, desc->node.prev, node) {
- /*
- * All chunks are on the global ld_free, so, we have to find
- * the end of the chain ourselves
- */
- if (chunk != desc && (chunk->mark == DESC_IDLE ||
- chunk->async_tx.cookie > 0 ||
- chunk->async_tx.cookie == -EBUSY ||
- &chunk->node == &sh_chan->ld_free))
- break;
- chunk->mark = DESC_SUBMITTED;
- /* Callback goes to the last chunk */
- chunk->async_tx.callback = NULL;
- chunk->cookie = cookie;
- list_move_tail(&chunk->node, &sh_chan->ld_queue);
- last = chunk;
- }
- last->async_tx.callback = callback;
- last->async_tx.callback_param = tx->callback_param;
- dev_dbg(sh_chan->dev, "submit #%d@%p on %d: %x[%d] -> %x\n",
- tx->cookie, &last->async_tx, sh_chan->id,
- desc->hw.sar, desc->hw.tcr, desc->hw.dar);
-