]> git.kernelconcepts.de Git - karo-tx-uboot.git/blobdiff - drivers/dma/apbh_dma.c
am33xx: Update DT files, add am335x_gp_evm_config target
[karo-tx-uboot.git] / drivers / dma / apbh_dma.c
index 69a10428ef241581a44bf0cd9436acb1987d3e99..4c5732e76e4cffb4bcce8a411d3f98808569b134 100644 (file)
@@ -7,19 +7,7 @@
  * Based on code from LTIB:
  * Copyright (C) 2010 Freescale Semiconductor, Inc. All Rights Reserved.
  *
- * This program 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.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ * SPDX-License-Identifier:    GPL-2.0+
  */
 
 #include <linux/list.h>
 #include <asm/arch/clock.h>
 #include <asm/arch/imx-regs.h>
 #include <asm/arch/sys_proto.h>
-#include <asm/arch/dma.h>
+#include <asm/imx-common/dma.h>
+#include <asm/imx-common/regs-apbh.h>
 
 static struct mxs_dma_chan mxs_dma_channels[MXS_MAX_DMA_CHANNELS];
+static struct mxs_apbh_regs *apbh_regs = (struct mxs_apbh_regs *)MXS_APBH_BASE;
 
 /*
  * Test is the DMA channel is valid channel
@@ -42,16 +32,76 @@ int mxs_dma_validate_chan(int channel)
 {
        struct mxs_dma_chan *pchan;
 
-       if ((channel < 0) || (channel >= MXS_MAX_DMA_CHANNELS))
+       if ((channel < 0) || (channel >= MXS_MAX_DMA_CHANNELS)) {
+               printf("Invalid DMA channel %d\n", channel);
                return -EINVAL;
+       }
 
        pchan = mxs_dma_channels + channel;
-       if (!(pchan->flags & MXS_DMA_FLAGS_ALLOCATED))
+       if (!(pchan->flags & MXS_DMA_FLAGS_ALLOCATED)) {
+               printf("DMA channel %d not allocated\n", channel);
                return -EINVAL;
+       }
 
        return 0;
 }
 
+/*
+ * Return the address of the command within a descriptor.
+ */
+static unsigned int mxs_dma_cmd_address(struct mxs_dma_desc *desc)
+{
+       return desc->address + offsetof(struct mxs_dma_desc, cmd);
+}
+
+/*
+ * Read a DMA channel's hardware semaphore.
+ *
+ * As used by the MXS platform's DMA software, the DMA channel's hardware
+ * semaphore reflects the number of DMA commands the hardware will process, but
+ * has not yet finished. This is a volatile value read directly from hardware,
+ * so it must be be viewed as immediately stale.
+ *
+ * If the channel is not marked busy, or has finished processing all its
+ * commands, this value should be zero.
+ *
+ * See mxs_dma_append() for details on how DMA command blocks must be configured
+ * to maintain the expected behavior of the semaphore's value.
+ */
+static int mxs_dma_read_semaphore(int channel)
+{
+       uint32_t tmp;
+       int ret;
+
+       ret = mxs_dma_validate_chan(channel);
+       if (ret)
+               return ret;
+
+       tmp = readl(&apbh_regs->ch[channel].hw_apbh_ch_sema);
+
+       tmp &= APBH_CHn_SEMA_PHORE_MASK;
+       tmp >>= APBH_CHn_SEMA_PHORE_OFFSET;
+
+       return tmp;
+}
+
+#ifndef        CONFIG_SYS_DCACHE_OFF
+static void mxs_dma_flush_desc(struct mxs_dma_desc *desc)
+{
+       uint32_t addr;
+       uint32_t size;
+
+       addr = (uint32_t)desc;
+       size = roundup(sizeof(struct mxs_dma_desc), MXS_DMA_ALIGNMENT);
+
+       flush_dcache_range(addr, addr + size);
+}
+#else
+static inline void mxs_dma_flush_desc(struct mxs_dma_desc *desc)
+{
+}
+#endif
+
 /*
  * Enable a DMA channel.
  *
@@ -61,10 +111,8 @@ int mxs_dma_validate_chan(int channel)
  * This function marks the DMA channel as "busy," whether or not there are any
  * descriptors to process.
  */
-int mxs_dma_enable(int channel)
+static int mxs_dma_enable(int channel)
 {
-       struct mx28_apbh_regs *apbh_regs =
-               (struct mx28_apbh_regs *)MXS_APBH_BASE;
        unsigned int sem;
        struct mxs_dma_chan *pchan;
        struct mxs_dma_desc *pdesc;
@@ -83,7 +131,7 @@ int mxs_dma_enable(int channel)
 
        pdesc = list_first_entry(&pchan->active, struct mxs_dma_desc, node);
        if (pdesc == NULL)
-               return -EFAULT;
+               return -EINVAL;
 
        if (pchan->flags & MXS_DMA_FLAGS_BUSY) {
                if (!(pdesc->cmd.data & MXS_DMA_DESC_CHAIN))
@@ -106,12 +154,12 @@ int mxs_dma_enable(int channel)
        } else {
                pchan->active_num += pchan->pending_num;
                pchan->pending_num = 0;
+               writel(1 << (channel + APBH_CTRL0_CLKGATE_CHANNEL_OFFSET),
+                       &apbh_regs->hw_apbh_ctrl0_clr);
                writel(mxs_dma_cmd_address(pdesc),
                        &apbh_regs->ch[channel].hw_apbh_ch_nxtcmdar);
                writel(pchan->active_num,
                        &apbh_regs->ch[channel].hw_apbh_ch_sema);
-               writel(1 << (channel + APBH_CTRL0_CLKGATE_CHANNEL_OFFSET),
-                       &apbh_regs->hw_apbh_ctrl0_clr);
        }
 
        pchan->flags |= MXS_DMA_FLAGS_BUSY;
@@ -132,11 +180,9 @@ int mxs_dma_enable(int channel)
  * state. It is unwise to call this function if there is ANY chance the hardware
  * is still processing a command.
  */
-int mxs_dma_disable(int channel)
+static int mxs_dma_disable(int channel)
 {
        struct mxs_dma_chan *pchan;
-       struct mx28_apbh_regs *apbh_regs =
-               (struct mx28_apbh_regs *)MXS_APBH_BASE;
        int ret;
 
        ret = mxs_dma_validate_chan(channel);
@@ -145,13 +191,12 @@ int mxs_dma_disable(int channel)
 
        pchan = mxs_dma_channels + channel;
 
-       if (!(pchan->flags & MXS_DMA_FLAGS_BUSY))
+       if ((pchan->flags & MXS_DMA_FLAGS_BUSY)) {
+               printf("%s: DMA channel %d busy\n", __func__, channel);
                return -EINVAL;
-
+       }
        writel(1 << (channel + APBH_CTRL0_CLKGATE_CHANNEL_OFFSET),
                &apbh_regs->hw_apbh_ctrl0_set);
-
-       pchan->flags &= ~MXS_DMA_FLAGS_BUSY;
        pchan->active_num = 0;
        pchan->pending_num = 0;
        list_splice_init(&pchan->active, &pchan->done);
@@ -162,110 +207,33 @@ int mxs_dma_disable(int channel)
 /*
  * Resets the DMA channel hardware.
  */
-int mxs_dma_reset(int channel)
-{
-       struct mx28_apbh_regs *apbh_regs =
-               (struct mx28_apbh_regs *)MXS_APBH_BASE;
-       int ret;
-
-       ret = mxs_dma_validate_chan(channel);
-       if (ret)
-               return ret;
-
-       writel(1 << (channel + APBH_CHANNEL_CTRL_RESET_CHANNEL_OFFSET),
-               &apbh_regs->hw_apbh_channel_ctrl_set);
-
-       return 0;
-}
-
-/*
- * Freeze a DMA channel.
- *
- * This function causes the channel to continuously fail arbitration for bus
- * access, which halts all forward progress without losing any state. A call to
- * mxs_dma_unfreeze() will cause the channel to continue its current operation
- * with no ill effect.
- */
-int mxs_dma_freeze(int channel)
-{
-       struct mx28_apbh_regs *apbh_regs =
-               (struct mx28_apbh_regs *)MXS_APBH_BASE;
-       int ret;
-
-       ret = mxs_dma_validate_chan(channel);
-       if (ret)
-               return ret;
-
-       writel(1 << (channel + APBH_CHANNEL_CTRL_FREEZE_CHANNEL_OFFSET),
-               &apbh_regs->hw_apbh_channel_ctrl_set);
-
-       return 0;
-}
-
-/*
- * Unfreeze a DMA channel.
- *
- * This function reverses the effect of mxs_dma_freeze(), enabling the DMA
- * channel to continue from where it was frozen.
- */
-int mxs_dma_unfreeze(int channel)
+static int mxs_dma_reset(int channel)
 {
-       struct mx28_apbh_regs *apbh_regs =
-               (struct mx28_apbh_regs *)MXS_APBH_BASE;
        int ret;
+#if defined(CONFIG_SOC_MX23)
+       uint32_t *setreg = &apbh_regs->hw_apbh_ctrl0_set;
+       uint32_t offset = APBH_CTRL0_RESET_CHANNEL_OFFSET;
+#elif (defined(CONFIG_SOC_MX28) || defined(CONFIG_SOC_MX6))
+       uint32_t *setreg = &apbh_regs->hw_apbh_channel_ctrl_set;
+       uint32_t offset = APBH_CHANNEL_CTRL_RESET_CHANNEL_OFFSET;
+#endif
 
        ret = mxs_dma_validate_chan(channel);
        if (ret)
                return ret;
 
-       writel(1 << (channel + APBH_CHANNEL_CTRL_FREEZE_CHANNEL_OFFSET),
-               &apbh_regs->hw_apbh_channel_ctrl_clr);
+       writel(1 << (channel + offset), setreg);
 
        return 0;
 }
 
-/*
- * Read a DMA channel's hardware semaphore.
- *
- * As used by the MXS platform's DMA software, the DMA channel's hardware
- * semaphore reflects the number of DMA commands the hardware will process, but
- * has not yet finished. This is a volatile value read directly from hardware,
- * so it must be be viewed as immediately stale.
- *
- * If the channel is not marked busy, or has finished processing all its
- * commands, this value should be zero.
- *
- * See mxs_dma_append() for details on how DMA command blocks must be configured
- * to maintain the expected behavior of the semaphore's value.
- */
-int mxs_dma_read_semaphore(int channel)
-{
-       struct mx28_apbh_regs *apbh_regs =
-               (struct mx28_apbh_regs *)MXS_APBH_BASE;
-       uint32_t tmp;
-       int ret;
-
-       ret = mxs_dma_validate_chan(channel);
-       if (ret)
-               return ret;
-
-       tmp = readl(&apbh_regs->ch[channel].hw_apbh_ch_sema);
-
-       tmp &= APBH_CHn_SEMA_PHORE_MASK;
-       tmp >>= APBH_CHn_SEMA_PHORE_OFFSET;
-
-       return tmp;
-}
-
 /*
  * Enable or disable DMA interrupt.
  *
  * This function enables the given DMA channel to interrupt the CPU.
  */
-int mxs_dma_enable_irq(int channel, int enable)
+static int mxs_dma_enable_irq(int channel, int enable)
 {
-       struct mx28_apbh_regs *apbh_regs =
-               (struct mx28_apbh_regs *)MXS_APBH_BASE;
        int ret;
 
        ret = mxs_dma_validate_chan(channel);
@@ -282,36 +250,14 @@ int mxs_dma_enable_irq(int channel, int enable)
        return 0;
 }
 
-/*
- * Check if a DMA interrupt is pending.
- */
-int mxs_dma_irq_is_pending(int channel)
-{
-       struct mx28_apbh_regs *apbh_regs =
-               (struct mx28_apbh_regs *)MXS_APBH_BASE;
-       uint32_t tmp;
-       int ret;
-
-       ret = mxs_dma_validate_chan(channel);
-       if (ret)
-               return ret;
-
-       tmp = readl(&apbh_regs->hw_apbh_ctrl1);
-       tmp |= readl(&apbh_regs->hw_apbh_ctrl2);
-
-       return (tmp >> channel) & 1;
-}
-
 /*
  * Clear DMA interrupt.
  *
  * The software that is using the DMA channel must register to receive its
  * interrupts and, when they arrive, must call this function to clear them.
  */
-int mxs_dma_ack_irq(int channel)
+static int mxs_dma_ack_irq(int channel)
 {
-       struct mx28_apbh_regs *apbh_regs =
-               (struct mx28_apbh_regs *)MXS_APBH_BASE;
        int ret;
 
        ret = mxs_dma_validate_chan(channel);
@@ -327,7 +273,7 @@ int mxs_dma_ack_irq(int channel)
 /*
  * Request to reserve a DMA channel
  */
-int mxs_dma_request(int channel)
+static int mxs_dma_request(int channel)
 {
        struct mxs_dma_chan *pchan;
 
@@ -344,6 +290,7 @@ int mxs_dma_request(int channel)
        pchan->flags |= MXS_DMA_FLAGS_ALLOCATED;
        pchan->active_num = 0;
        pchan->pending_num = 0;
+       pchan->timeout = 10000000;
 
        INIT_LIST_HEAD(&pchan->active);
        INIT_LIST_HEAD(&pchan->done);
@@ -381,14 +328,54 @@ int mxs_dma_release(int channel)
        return 0;
 }
 
+/*
+ * Set the timeout for any DMA operation started with mxs_dma_go()
+ * The timeout value given is in milliseconds
+ */
+int mxs_dma_set_timeout(int channel, unsigned long timeout)
+{
+       int ret;
+       struct mxs_dma_chan *pchan;
+
+       ret = mxs_dma_validate_chan(channel);
+       if (ret)
+               return ret;
+
+       pchan = &mxs_dma_channels[channel];
+
+       if (pchan->flags & MXS_DMA_FLAGS_BUSY)
+               return -EBUSY;
+
+       if (timeout > ~0UL / 1000)
+               return -EINVAL;
+
+       pchan->timeout = timeout;
+       return 0;
+}
+
+unsigned long mxs_dma_get_timeout(int channel)
+{
+       int ret;
+       struct mxs_dma_chan *pchan;
+
+       ret = mxs_dma_validate_chan(channel);
+       if (ret)
+               return 0;
+
+       pchan = &mxs_dma_channels[channel];
+       return pchan->timeout;
+}
+
 /*
  * Allocate DMA descriptor
  */
 struct mxs_dma_desc *mxs_dma_desc_alloc(void)
 {
        struct mxs_dma_desc *pdesc;
+       uint32_t size;
 
-       pdesc = memalign(MXS_DMA_ALIGNMENT, sizeof(struct mxs_dma_desc));
+       size = roundup(sizeof(struct mxs_dma_desc), MXS_DMA_ALIGNMENT);
+       pdesc = memalign(MXS_DMA_ALIGNMENT, size);
 
        if (pdesc == NULL)
                return NULL;
@@ -410,32 +397,6 @@ void mxs_dma_desc_free(struct mxs_dma_desc *pdesc)
        free(pdesc);
 }
 
-/*
- * Return the address of the command within a descriptor.
- */
-unsigned int mxs_dma_cmd_address(struct mxs_dma_desc *desc)
-{
-       return desc->address + offsetof(struct mxs_dma_desc, cmd);
-}
-
-/*
- * Check if descriptor is on a channel's active list.
- *
- * This function returns the state of a descriptor's "ready" flag. This flag is
- * usually set only if the descriptor appears on a channel's active list. The
- * descriptor may or may not have already been processed by the hardware.
- *
- * The "ready" flag is set when the descriptor is submitted to a channel by a
- * call to mxs_dma_append() or mxs_dma_append_list(). The "ready" flag is
- * cleared when a processed descriptor is moved off the active list by a call
- * to mxs_dma_finish(). The "ready" flag is NOT cleared if the descriptor is
- * aborted by a call to mxs_dma_disable().
- */
-int mxs_dma_desc_pending(struct mxs_dma_desc *pdesc)
-{
-       return pdesc->flags & MXS_DMA_DESC_READY;
-}
-
 /*
  * Add a DMA descriptor to a channel.
  *
@@ -499,38 +460,17 @@ int mxs_dma_desc_append(int channel, struct mxs_dma_desc *pdesc)
 
                last->cmd.next = mxs_dma_cmd_address(pdesc);
                last->cmd.data |= MXS_DMA_DESC_CHAIN;
+
+               mxs_dma_flush_desc(last);
        }
        pdesc->flags |= MXS_DMA_DESC_READY;
        if (pdesc->flags & MXS_DMA_DESC_FIRST)
                pchan->pending_num++;
        list_add_tail(&pdesc->node, &pchan->active);
 
-       return ret;
-}
+       mxs_dma_flush_desc(pdesc);
 
-/*
- * Retrieve processed DMA descriptors.
- *
- * This function moves all the descriptors from the DMA channel's "done" list to
- * the head of the given list.
- */
-int mxs_dma_get_finished(int channel, struct list_head *head)
-{
-       struct mxs_dma_chan *pchan;
-       int ret;
-
-       ret = mxs_dma_validate_chan(channel);
-       if (ret)
-               return ret;
-
-       if (head == NULL)
-               return 0;
-
-       pchan = mxs_dma_channels + channel;
-
-       list_splice(&pchan->done, head);
-
-       return 0;
+       return ret;
 }
 
 /*
@@ -544,7 +484,7 @@ int mxs_dma_get_finished(int channel, struct list_head *head)
  * This function marks the DMA channel as "not busy" if no unprocessed
  * descriptors remain on the "active" list.
  */
-int mxs_dma_finish(int channel, struct list_head *head)
+static int mxs_dma_finish(int channel, struct list_head *head)
 {
        int sem;
        struct mxs_dma_chan *pchan;
@@ -590,23 +530,21 @@ int mxs_dma_finish(int channel, struct list_head *head)
 /*
  * Wait for DMA channel to complete
  */
-int mxs_dma_wait_complete(uint32_t timeout, unsigned int chan)
+static int mxs_dma_wait_complete(uint32_t timeout, unsigned int chan)
 {
-       struct mx28_apbh_regs *apbh_regs =
-               (struct mx28_apbh_regs *)MXS_APBH_BASE;
        int ret;
 
        ret = mxs_dma_validate_chan(chan);
        if (ret)
                return ret;
 
-       if (mx28_wait_mask_set(&apbh_regs->hw_apbh_ctrl1_reg,
+       if (mxs_wait_mask_set(&apbh_regs->hw_apbh_ctrl1_reg,
                                1 << chan, timeout)) {
                ret = -ETIMEDOUT;
                mxs_dma_reset(chan);
        }
 
-       return 0;
+       return ret;
 }
 
 /*
@@ -614,16 +552,25 @@ int mxs_dma_wait_complete(uint32_t timeout, unsigned int chan)
  */
 int mxs_dma_go(int chan)
 {
-       uint32_t timeout = 10000;
        int ret;
-
+       struct mxs_dma_chan *pchan;
        LIST_HEAD(tmp_desc_list);
 
+       ret = mxs_dma_validate_chan(chan);
+       if (ret)
+               return ret;
+
+       pchan = &mxs_dma_channels[chan];
+
        mxs_dma_enable_irq(chan, 1);
-       mxs_dma_enable(chan);
+       ret = mxs_dma_enable(chan);
+       if (ret) {
+               mxs_dma_enable_irq(chan, 0);
+               return ret;
+       }
 
        /* Wait for DMA to finish. */
-       ret = mxs_dma_wait_complete(timeout, chan);
+       ret = mxs_dma_wait_complete(pchan->timeout * 1000, chan);
 
        /* Clear out the descriptors we just ran. */
        mxs_dma_finish(chan, &tmp_desc_list);
@@ -638,16 +585,33 @@ int mxs_dma_go(int chan)
 }
 
 /*
- * Initialize the DMA hardware
+ * Execute a continuously running circular DMA descriptor.
+ * NOTE: This is not intended for general use, but rather
+ *      for the LCD driver in Smart-LCD mode. It allows
+ *      continuous triggering of the RUN bit there.
  */
-int mxs_dma_init(void)
+void mxs_dma_circ_start(int chan, struct mxs_dma_desc *pdesc)
 {
-       struct mx28_apbh_regs *apbh_regs =
-               (struct mx28_apbh_regs *)MXS_APBH_BASE;
-       struct mxs_dma_chan *pchan;
-       int ret, channel;
+       struct mxs_apbh_regs *apbh_regs =
+               (struct mxs_apbh_regs *)MXS_APBH_BASE;
 
-       mx28_reset_block(&apbh_regs->hw_apbh_ctrl0_reg);
+       mxs_dma_flush_desc(pdesc);
+
+       mxs_dma_enable_irq(chan, 1);
+
+       writel(mxs_dma_cmd_address(pdesc),
+               &apbh_regs->ch[chan].hw_apbh_ch_nxtcmdar);
+       writel(1, &apbh_regs->ch[chan].hw_apbh_ch_sema);
+       writel(1 << (chan + APBH_CTRL0_CLKGATE_CHANNEL_OFFSET),
+               &apbh_regs->hw_apbh_ctrl0_clr);
+}
+
+/*
+ * Initialize the DMA hardware
+ */
+void mxs_dma_init(void)
+{
+       mxs_reset_block(&apbh_regs->hw_apbh_ctrl0_reg);
 
 #ifdef CONFIG_APBH_DMA_BURST8
        writel(APBH_CTRL0_AHB_BURST8_EN,
@@ -664,28 +628,26 @@ int mxs_dma_init(void)
        writel(APBH_CTRL0_APB_BURST_EN,
                &apbh_regs->hw_apbh_ctrl0_clr);
 #endif
+}
 
-       for (channel = 0; channel < MXS_MAX_DMA_CHANNELS; channel++) {
-               pchan = mxs_dma_channels + channel;
-               pchan->flags = MXS_DMA_FLAGS_VALID;
-
-               ret = mxs_dma_request(channel);
+int mxs_dma_init_channel(int channel)
+{
+       struct mxs_dma_chan *pchan;
+       int ret;
 
-               if (ret) {
-                       printf("MXS DMA: Can't acquire DMA channel %i\n",
-                               channel);
+       pchan = mxs_dma_channels + channel;
+       pchan->flags = MXS_DMA_FLAGS_VALID;
 
-                       goto err;
-               }
+       ret = mxs_dma_request(channel);
 
-               mxs_dma_reset(channel);
-               mxs_dma_ack_irq(channel);
+       if (ret) {
+               printf("MXS DMA: Can't acquire DMA channel %i\n",
+                       channel);
+               return ret;
        }
 
-       return 0;
+       mxs_dma_reset(channel);
+       mxs_dma_ack_irq(channel);
 
-err:
-       while (--channel >= 0)
-               mxs_dma_release(channel);
-       return ret;
+       return 0;
 }