···11+/*22+ * Copyright 2004-2009 Freescale Semiconductor, Inc. All Rights Reserved.33+ *44+ * This program is free software; you can redistribute it and/or modify55+ * it under the terms of the GNU General Public License version 2 as66+ * published by the Free Software Foundation.77+ */88+99+#ifndef __ASM_ARCH_MXC_DMA_H__1010+#define __ASM_ARCH_MXC_DMA_H__1111+1212+#include <linux/scatterlist.h>1313+#include <linux/device.h>1414+#include <linux/dmaengine.h>1515+1616+/*1717+ * This enumerates peripheral types. Used for SDMA.1818+ */1919+enum sdma_peripheral_type {2020+ IMX_DMATYPE_SSI, /* MCU domain SSI */2121+ IMX_DMATYPE_SSI_SP, /* Shared SSI */2222+ IMX_DMATYPE_MMC, /* MMC */2323+ IMX_DMATYPE_SDHC, /* SDHC */2424+ IMX_DMATYPE_UART, /* MCU domain UART */2525+ IMX_DMATYPE_UART_SP, /* Shared UART */2626+ IMX_DMATYPE_FIRI, /* FIRI */2727+ IMX_DMATYPE_CSPI, /* MCU domain CSPI */2828+ IMX_DMATYPE_CSPI_SP, /* Shared CSPI */2929+ IMX_DMATYPE_SIM, /* SIM */3030+ IMX_DMATYPE_ATA, /* ATA */3131+ IMX_DMATYPE_CCM, /* CCM */3232+ IMX_DMATYPE_EXT, /* External peripheral */3333+ IMX_DMATYPE_MSHC, /* Memory Stick Host Controller */3434+ IMX_DMATYPE_MSHC_SP, /* Shared Memory Stick Host Controller */3535+ IMX_DMATYPE_DSP, /* DSP */3636+ IMX_DMATYPE_MEMORY, /* Memory */3737+ IMX_DMATYPE_FIFO_MEMORY,/* FIFO type Memory */3838+ IMX_DMATYPE_SPDIF, /* SPDIF */3939+ IMX_DMATYPE_IPU_MEMORY, /* IPU Memory */4040+ IMX_DMATYPE_ASRC, /* ASRC */4141+ IMX_DMATYPE_ESAI, /* ESAI */4242+};4343+4444+enum imx_dma_prio {4545+ DMA_PRIO_HIGH = 0,4646+ DMA_PRIO_MEDIUM = 1,4747+ DMA_PRIO_LOW = 24848+};4949+5050+struct imx_dma_data {5151+ int dma_request; /* DMA request line */5252+ enum sdma_peripheral_type peripheral_type;5353+ int priority;5454+};5555+5656+static inline int imx_dma_is_ipu(struct dma_chan *chan)5757+{5858+ return !strcmp(dev_name(chan->device->dev), "ipu-core");5959+}6060+6161+static inline int imx_dma_is_general_purpose(struct dma_chan *chan)6262+{6363+ return !strcmp(dev_name(chan->device->dev), "imx-sdma") ||6464+ !strcmp(dev_name(chan->device->dev), "imx-dma");6565+}6666+6767+#endif
+17
arch/arm/plat-mxc/include/mach/sdma.h
···11+#ifndef __MACH_MXC_SDMA_H__22+#define __MACH_MXC_SDMA_H__33+44+/**55+ * struct sdma_platform_data - platform specific data for SDMA engine66+ *77+ * @sdma_version The version of this SDMA engine88+ * @cpu_name used to generate the firmware name99+ * @to_version CPU Tape out version1010+ */1111+struct sdma_platform_data {1212+ int sdma_version;1313+ char *cpu_name;1414+ int to_version;1515+};1616+1717+#endif /* __MACH_MXC_SDMA_H__ */
-137
arch/powerpc/include/asm/fsldma.h
···11-/*22- * Freescale MPC83XX / MPC85XX DMA Controller33- *44- * Copyright (c) 2009 Ira W. Snyder <iws@ovro.caltech.edu>55- *66- * This file is licensed under the terms of the GNU General Public License77- * version 2. This program is licensed "as is" without any warranty of any88- * kind, whether express or implied.99- */1010-1111-#ifndef __ARCH_POWERPC_ASM_FSLDMA_H__1212-#define __ARCH_POWERPC_ASM_FSLDMA_H__1313-1414-#include <linux/slab.h>1515-#include <linux/dmaengine.h>1616-1717-/*1818- * Definitions for the Freescale DMA controller's DMA_SLAVE implemention1919- *2020- * The Freescale DMA_SLAVE implementation was designed to handle many-to-many2121- * transfers. An example usage would be an accelerated copy between two2222- * scatterlists. Another example use would be an accelerated copy from2323- * multiple non-contiguous device buffers into a single scatterlist.2424- *2525- * A DMA_SLAVE transaction is defined by a struct fsl_dma_slave. This2626- * structure contains a list of hardware addresses that should be copied2727- * to/from the scatterlist passed into device_prep_slave_sg(). The structure2828- * also has some fields to enable hardware-specific features.2929- */3030-3131-/**3232- * struct fsl_dma_hw_addr3333- * @entry: linked list entry3434- * @address: the hardware address3535- * @length: length to transfer3636- *3737- * Holds a single physical hardware address / length pair for use3838- * with the DMAEngine DMA_SLAVE API.3939- */4040-struct fsl_dma_hw_addr {4141- struct list_head entry;4242-4343- dma_addr_t address;4444- size_t length;4545-};4646-4747-/**4848- * struct fsl_dma_slave4949- * @addresses: a linked list of struct fsl_dma_hw_addr structures5050- * @request_count: value for DMA request count5151- * @src_loop_size: setup and enable constant source-address DMA transfers5252- * @dst_loop_size: setup and enable constant destination address DMA transfers5353- * @external_start: enable externally started DMA transfers5454- * @external_pause: enable externally paused DMA transfers5555- *5656- * Holds a list of address / length pairs for use with the DMAEngine5757- * DMA_SLAVE API implementation for the Freescale DMA controller.5858- */5959-struct fsl_dma_slave {6060-6161- /* List of hardware address/length pairs */6262- struct list_head addresses;6363-6464- /* Support for extra controller features */6565- unsigned int request_count;6666- unsigned int src_loop_size;6767- unsigned int dst_loop_size;6868- bool external_start;6969- bool external_pause;7070-};7171-7272-/**7373- * fsl_dma_slave_append - add an address/length pair to a struct fsl_dma_slave7474- * @slave: the &struct fsl_dma_slave to add to7575- * @address: the hardware address to add7676- * @length: the length of bytes to transfer from @address7777- *7878- * Add a hardware address/length pair to a struct fsl_dma_slave. Returns 0 on7979- * success, -ERRNO otherwise.8080- */8181-static inline int fsl_dma_slave_append(struct fsl_dma_slave *slave,8282- dma_addr_t address, size_t length)8383-{8484- struct fsl_dma_hw_addr *addr;8585-8686- addr = kzalloc(sizeof(*addr), GFP_ATOMIC);8787- if (!addr)8888- return -ENOMEM;8989-9090- INIT_LIST_HEAD(&addr->entry);9191- addr->address = address;9292- addr->length = length;9393-9494- list_add_tail(&addr->entry, &slave->addresses);9595- return 0;9696-}9797-9898-/**9999- * fsl_dma_slave_free - free a struct fsl_dma_slave100100- * @slave: the struct fsl_dma_slave to free101101- *102102- * Free a struct fsl_dma_slave and all associated address/length pairs103103- */104104-static inline void fsl_dma_slave_free(struct fsl_dma_slave *slave)105105-{106106- struct fsl_dma_hw_addr *addr, *tmp;107107-108108- if (slave) {109109- list_for_each_entry_safe(addr, tmp, &slave->addresses, entry) {110110- list_del(&addr->entry);111111- kfree(addr);112112- }113113-114114- kfree(slave);115115- }116116-}117117-118118-/**119119- * fsl_dma_slave_alloc - allocate a struct fsl_dma_slave120120- * @gfp: the flags to pass to kmalloc when allocating this structure121121- *122122- * Allocate a struct fsl_dma_slave for use by the DMA_SLAVE API. Returns a new123123- * struct fsl_dma_slave on success, or NULL on failure.124124- */125125-static inline struct fsl_dma_slave *fsl_dma_slave_alloc(gfp_t gfp)126126-{127127- struct fsl_dma_slave *slave;128128-129129- slave = kzalloc(sizeof(*slave), gfp);130130- if (!slave)131131- return NULL;132132-133133- INIT_LIST_HEAD(&slave->addresses);134134- return slave;135135-}136136-137137-#endif /* __ARCH_POWERPC_ASM_FSLDMA_H__ */
+24
drivers/dma/Kconfig
···4949config ASYNC_TX_DISABLE_CHANNEL_SWITCH5050 bool51515252+config AMBA_PL08X5353+ bool "ARM PrimeCell PL080 or PL081 support"5454+ depends on ARM_AMBA && EXPERIMENTAL5555+ select DMA_ENGINE5656+ help5757+ Platform has a PL08x DMAC device5858+ which can provide DMA engine support5959+5260config INTEL_IOATDMA5361 tristate "Intel I/OAT DMA support"5462 depends on PCI && X86···202194 select DMA_ENGINE203195 help204196 Enable support for the Topcliff PCH DMA engine.197197+198198+config IMX_SDMA199199+ tristate "i.MX SDMA support"200200+ depends on ARCH_MX25 || ARCH_MX3 || ARCH_MX5201201+ select DMA_ENGINE202202+ help203203+ Support the i.MX SDMA engine. This engine is integrated into204204+ Freescale i.MX25/31/35/51 chips.205205+206206+config IMX_DMA207207+ tristate "i.MX DMA support"208208+ depends on ARCH_MX1 || ARCH_MX21 || MACH_MX27209209+ select DMA_ENGINE210210+ help211211+ Support the i.MX DMA engine. This engine is integrated into212212+ Freescale i.MX1/21/27 chips.205213206214config DMA_ENGINE207215 bool
···11+/*22+ * Copyright (c) 2006 ARM Ltd.33+ * Copyright (c) 2010 ST-Ericsson SA44+ *55+ * Author: Peter Pearse <peter.pearse@arm.com>66+ * Author: Linus Walleij <linus.walleij@stericsson.com>77+ *88+ * This program is free software; you can redistribute it and/or modify it99+ * under the terms of the GNU General Public License as published by the Free1010+ * Software Foundation; either version 2 of the License, or (at your option)1111+ * any later version.1212+ *1313+ * This program is distributed in the hope that it will be useful, but WITHOUT1414+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or1515+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for1616+ * more details.1717+ *1818+ * You should have received a copy of the GNU General Public License along with1919+ * this program; if not, write to the Free Software Foundation, Inc., 592020+ * Temple Place - Suite 330, Boston, MA 02111-1307, USA.2121+ *2222+ * The full GNU General Public License is iin this distribution in the2323+ * file called COPYING.2424+ *2525+ * Documentation: ARM DDI 0196G == PL0802626+ * Documentation: ARM DDI 0218E == PL0812727+ *2828+ * PL080 & PL081 both have 16 sets of DMA signals that can be routed to2929+ * any channel.3030+ *3131+ * The PL080 has 8 channels available for simultaneous use, and the PL0813232+ * has only two channels. So on these DMA controllers the number of channels3333+ * and the number of incoming DMA signals are two totally different things.3434+ * It is usually not possible to theoretically handle all physical signals,3535+ * so a multiplexing scheme with possible denial of use is necessary.3636+ *3737+ * The PL080 has a dual bus master, PL081 has a single master.3838+ *3939+ * Memory to peripheral transfer may be visualized as4040+ * Get data from memory to DMAC4141+ * Until no data left4242+ * On burst request from peripheral4343+ * Destination burst from DMAC to peripheral4444+ * Clear burst request4545+ * Raise terminal count interrupt4646+ *4747+ * For peripherals with a FIFO:4848+ * Source burst size == half the depth of the peripheral FIFO4949+ * Destination burst size == the depth of the peripheral FIFO5050+ *5151+ * (Bursts are irrelevant for mem to mem transfers - there are no burst5252+ * signals, the DMA controller will simply facilitate its AHB master.)5353+ *5454+ * ASSUMES default (little) endianness for DMA transfers5555+ *5656+ * Only DMAC flow control is implemented5757+ *5858+ * Global TODO:5959+ * - Break out common code from arch/arm/mach-s3c64xx and share6060+ */6161+#include <linux/device.h>6262+#include <linux/init.h>6363+#include <linux/module.h>6464+#include <linux/pci.h>6565+#include <linux/interrupt.h>6666+#include <linux/slab.h>6767+#include <linux/dmapool.h>6868+#include <linux/amba/bus.h>6969+#include <linux/dmaengine.h>7070+#include <linux/amba/pl08x.h>7171+#include <linux/debugfs.h>7272+#include <linux/seq_file.h>7373+7474+#include <asm/hardware/pl080.h>7575+#include <asm/dma.h>7676+#include <asm/mach/dma.h>7777+#include <asm/atomic.h>7878+#include <asm/processor.h>7979+#include <asm/cacheflush.h>8080+8181+#define DRIVER_NAME "pl08xdmac"8282+8383+/**8484+ * struct vendor_data - vendor-specific config parameters8585+ * for PL08x derivates8686+ * @name: the name of this specific variant8787+ * @channels: the number of channels available in this variant8888+ * @dualmaster: whether this version supports dual AHB masters8989+ * or not.9090+ */9191+struct vendor_data {9292+ char *name;9393+ u8 channels;9494+ bool dualmaster;9595+};9696+9797+/*9898+ * PL08X private data structures9999+ * An LLI struct - see pl08x TRM100100+ * Note that next uses bit[0] as a bus bit,101101+ * start & end do not - their bus bit info102102+ * is in cctl103103+ */104104+struct lli {105105+ dma_addr_t src;106106+ dma_addr_t dst;107107+ dma_addr_t next;108108+ u32 cctl;109109+};110110+111111+/**112112+ * struct pl08x_driver_data - the local state holder for the PL08x113113+ * @slave: slave engine for this instance114114+ * @memcpy: memcpy engine for this instance115115+ * @base: virtual memory base (remapped) for the PL08x116116+ * @adev: the corresponding AMBA (PrimeCell) bus entry117117+ * @vd: vendor data for this PL08x variant118118+ * @pd: platform data passed in from the platform/machine119119+ * @phy_chans: array of data for the physical channels120120+ * @pool: a pool for the LLI descriptors121121+ * @pool_ctr: counter of LLIs in the pool122122+ * @lock: a spinlock for this struct123123+ */124124+struct pl08x_driver_data {125125+ struct dma_device slave;126126+ struct dma_device memcpy;127127+ void __iomem *base;128128+ struct amba_device *adev;129129+ struct vendor_data *vd;130130+ struct pl08x_platform_data *pd;131131+ struct pl08x_phy_chan *phy_chans;132132+ struct dma_pool *pool;133133+ int pool_ctr;134134+ spinlock_t lock;135135+};136136+137137+/*138138+ * PL08X specific defines139139+ */140140+141141+/*142142+ * Memory boundaries: the manual for PL08x says that the controller143143+ * cannot read past a 1KiB boundary, so these defines are used to144144+ * create transfer LLIs that do not cross such boundaries.145145+ */146146+#define PL08X_BOUNDARY_SHIFT (10) /* 1KB 0x400 */147147+#define PL08X_BOUNDARY_SIZE (1 << PL08X_BOUNDARY_SHIFT)148148+149149+/* Minimum period between work queue runs */150150+#define PL08X_WQ_PERIODMIN 20151151+152152+/* Size (bytes) of each LLI buffer allocated for one transfer */153153+# define PL08X_LLI_TSFR_SIZE 0x2000154154+155155+/* Maximimum times we call dma_pool_alloc on this pool without freeing */156156+#define PL08X_MAX_ALLOCS 0x40157157+#define MAX_NUM_TSFR_LLIS (PL08X_LLI_TSFR_SIZE/sizeof(struct lli))158158+#define PL08X_ALIGN 8159159+160160+static inline struct pl08x_dma_chan *to_pl08x_chan(struct dma_chan *chan)161161+{162162+ return container_of(chan, struct pl08x_dma_chan, chan);163163+}164164+165165+/*166166+ * Physical channel handling167167+ */168168+169169+/* Whether a certain channel is busy or not */170170+static int pl08x_phy_channel_busy(struct pl08x_phy_chan *ch)171171+{172172+ unsigned int val;173173+174174+ val = readl(ch->base + PL080_CH_CONFIG);175175+ return val & PL080_CONFIG_ACTIVE;176176+}177177+178178+/*179179+ * Set the initial DMA register values i.e. those for the first LLI180180+ * The next lli pointer and the configuration interrupt bit have181181+ * been set when the LLIs were constructed182182+ */183183+static void pl08x_set_cregs(struct pl08x_driver_data *pl08x,184184+ struct pl08x_phy_chan *ch)185185+{186186+ /* Wait for channel inactive */187187+ while (pl08x_phy_channel_busy(ch))188188+ ;189189+190190+ dev_vdbg(&pl08x->adev->dev,191191+ "WRITE channel %d: csrc=%08x, cdst=%08x, "192192+ "cctl=%08x, clli=%08x, ccfg=%08x\n",193193+ ch->id,194194+ ch->csrc,195195+ ch->cdst,196196+ ch->cctl,197197+ ch->clli,198198+ ch->ccfg);199199+200200+ writel(ch->csrc, ch->base + PL080_CH_SRC_ADDR);201201+ writel(ch->cdst, ch->base + PL080_CH_DST_ADDR);202202+ writel(ch->clli, ch->base + PL080_CH_LLI);203203+ writel(ch->cctl, ch->base + PL080_CH_CONTROL);204204+ writel(ch->ccfg, ch->base + PL080_CH_CONFIG);205205+}206206+207207+static inline void pl08x_config_phychan_for_txd(struct pl08x_dma_chan *plchan)208208+{209209+ struct pl08x_channel_data *cd = plchan->cd;210210+ struct pl08x_phy_chan *phychan = plchan->phychan;211211+ struct pl08x_txd *txd = plchan->at;212212+213213+ /* Copy the basic control register calculated at transfer config */214214+ phychan->csrc = txd->csrc;215215+ phychan->cdst = txd->cdst;216216+ phychan->clli = txd->clli;217217+ phychan->cctl = txd->cctl;218218+219219+ /* Assign the signal to the proper control registers */220220+ phychan->ccfg = cd->ccfg;221221+ phychan->ccfg &= ~PL080_CONFIG_SRC_SEL_MASK;222222+ phychan->ccfg &= ~PL080_CONFIG_DST_SEL_MASK;223223+ /* If it wasn't set from AMBA, ignore it */224224+ if (txd->direction == DMA_TO_DEVICE)225225+ /* Select signal as destination */226226+ phychan->ccfg |=227227+ (phychan->signal << PL080_CONFIG_DST_SEL_SHIFT);228228+ else if (txd->direction == DMA_FROM_DEVICE)229229+ /* Select signal as source */230230+ phychan->ccfg |=231231+ (phychan->signal << PL080_CONFIG_SRC_SEL_SHIFT);232232+ /* Always enable error interrupts */233233+ phychan->ccfg |= PL080_CONFIG_ERR_IRQ_MASK;234234+ /* Always enable terminal interrupts */235235+ phychan->ccfg |= PL080_CONFIG_TC_IRQ_MASK;236236+}237237+238238+/*239239+ * Enable the DMA channel240240+ * Assumes all other configuration bits have been set241241+ * as desired before this code is called242242+ */243243+static void pl08x_enable_phy_chan(struct pl08x_driver_data *pl08x,244244+ struct pl08x_phy_chan *ch)245245+{246246+ u32 val;247247+248248+ /*249249+ * Do not access config register until channel shows as disabled250250+ */251251+ while (readl(pl08x->base + PL080_EN_CHAN) & (1 << ch->id))252252+ ;253253+254254+ /*255255+ * Do not access config register until channel shows as inactive256256+ */257257+ val = readl(ch->base + PL080_CH_CONFIG);258258+ while ((val & PL080_CONFIG_ACTIVE) || (val & PL080_CONFIG_ENABLE))259259+ val = readl(ch->base + PL080_CH_CONFIG);260260+261261+ writel(val | PL080_CONFIG_ENABLE, ch->base + PL080_CH_CONFIG);262262+}263263+264264+/*265265+ * Overall DMAC remains enabled always.266266+ *267267+ * Disabling individual channels could lose data.268268+ *269269+ * Disable the peripheral DMA after disabling the DMAC270270+ * in order to allow the DMAC FIFO to drain, and271271+ * hence allow the channel to show inactive272272+ *273273+ */274274+static void pl08x_pause_phy_chan(struct pl08x_phy_chan *ch)275275+{276276+ u32 val;277277+278278+ /* Set the HALT bit and wait for the FIFO to drain */279279+ val = readl(ch->base + PL080_CH_CONFIG);280280+ val |= PL080_CONFIG_HALT;281281+ writel(val, ch->base + PL080_CH_CONFIG);282282+283283+ /* Wait for channel inactive */284284+ while (pl08x_phy_channel_busy(ch))285285+ ;286286+}287287+288288+static void pl08x_resume_phy_chan(struct pl08x_phy_chan *ch)289289+{290290+ u32 val;291291+292292+ /* Clear the HALT bit */293293+ val = readl(ch->base + PL080_CH_CONFIG);294294+ val &= ~PL080_CONFIG_HALT;295295+ writel(val, ch->base + PL080_CH_CONFIG);296296+}297297+298298+299299+/* Stops the channel */300300+static void pl08x_stop_phy_chan(struct pl08x_phy_chan *ch)301301+{302302+ u32 val;303303+304304+ pl08x_pause_phy_chan(ch);305305+306306+ /* Disable channel */307307+ val = readl(ch->base + PL080_CH_CONFIG);308308+ val &= ~PL080_CONFIG_ENABLE;309309+ val &= ~PL080_CONFIG_ERR_IRQ_MASK;310310+ val &= ~PL080_CONFIG_TC_IRQ_MASK;311311+ writel(val, ch->base + PL080_CH_CONFIG);312312+}313313+314314+static inline u32 get_bytes_in_cctl(u32 cctl)315315+{316316+ /* The source width defines the number of bytes */317317+ u32 bytes = cctl & PL080_CONTROL_TRANSFER_SIZE_MASK;318318+319319+ switch (cctl >> PL080_CONTROL_SWIDTH_SHIFT) {320320+ case PL080_WIDTH_8BIT:321321+ break;322322+ case PL080_WIDTH_16BIT:323323+ bytes *= 2;324324+ break;325325+ case PL080_WIDTH_32BIT:326326+ bytes *= 4;327327+ break;328328+ }329329+ return bytes;330330+}331331+332332+/* The channel should be paused when calling this */333333+static u32 pl08x_getbytes_chan(struct pl08x_dma_chan *plchan)334334+{335335+ struct pl08x_phy_chan *ch;336336+ struct pl08x_txd *txdi = NULL;337337+ struct pl08x_txd *txd;338338+ unsigned long flags;339339+ u32 bytes = 0;340340+341341+ spin_lock_irqsave(&plchan->lock, flags);342342+343343+ ch = plchan->phychan;344344+ txd = plchan->at;345345+346346+ /*347347+ * Next follow the LLIs to get the number of pending bytes in the348348+ * currently active transaction.349349+ */350350+ if (ch && txd) {351351+ struct lli *llis_va = txd->llis_va;352352+ struct lli *llis_bus = (struct lli *) txd->llis_bus;353353+ u32 clli = readl(ch->base + PL080_CH_LLI);354354+355355+ /* First get the bytes in the current active LLI */356356+ bytes = get_bytes_in_cctl(readl(ch->base + PL080_CH_CONTROL));357357+358358+ if (clli) {359359+ int i = 0;360360+361361+ /* Forward to the LLI pointed to by clli */362362+ while ((clli != (u32) &(llis_bus[i])) &&363363+ (i < MAX_NUM_TSFR_LLIS))364364+ i++;365365+366366+ while (clli) {367367+ bytes += get_bytes_in_cctl(llis_va[i].cctl);368368+ /*369369+ * A clli of 0x00000000 will terminate the370370+ * LLI list371371+ */372372+ clli = llis_va[i].next;373373+ i++;374374+ }375375+ }376376+ }377377+378378+ /* Sum up all queued transactions */379379+ if (!list_empty(&plchan->desc_list)) {380380+ list_for_each_entry(txdi, &plchan->desc_list, node) {381381+ bytes += txdi->len;382382+ }383383+384384+ }385385+386386+ spin_unlock_irqrestore(&plchan->lock, flags);387387+388388+ return bytes;389389+}390390+391391+/*392392+ * Allocate a physical channel for a virtual channel393393+ */394394+static struct pl08x_phy_chan *395395+pl08x_get_phy_channel(struct pl08x_driver_data *pl08x,396396+ struct pl08x_dma_chan *virt_chan)397397+{398398+ struct pl08x_phy_chan *ch = NULL;399399+ unsigned long flags;400400+ int i;401401+402402+ /*403403+ * Try to locate a physical channel to be used for404404+ * this transfer. If all are taken return NULL and405405+ * the requester will have to cope by using some fallback406406+ * PIO mode or retrying later.407407+ */408408+ for (i = 0; i < pl08x->vd->channels; i++) {409409+ ch = &pl08x->phy_chans[i];410410+411411+ spin_lock_irqsave(&ch->lock, flags);412412+413413+ if (!ch->serving) {414414+ ch->serving = virt_chan;415415+ ch->signal = -1;416416+ spin_unlock_irqrestore(&ch->lock, flags);417417+ break;418418+ }419419+420420+ spin_unlock_irqrestore(&ch->lock, flags);421421+ }422422+423423+ if (i == pl08x->vd->channels) {424424+ /* No physical channel available, cope with it */425425+ return NULL;426426+ }427427+428428+ return ch;429429+}430430+431431+static inline void pl08x_put_phy_channel(struct pl08x_driver_data *pl08x,432432+ struct pl08x_phy_chan *ch)433433+{434434+ unsigned long flags;435435+436436+ /* Stop the channel and clear its interrupts */437437+ pl08x_stop_phy_chan(ch);438438+ writel((1 << ch->id), pl08x->base + PL080_ERR_CLEAR);439439+ writel((1 << ch->id), pl08x->base + PL080_TC_CLEAR);440440+441441+ /* Mark it as free */442442+ spin_lock_irqsave(&ch->lock, flags);443443+ ch->serving = NULL;444444+ spin_unlock_irqrestore(&ch->lock, flags);445445+}446446+447447+/*448448+ * LLI handling449449+ */450450+451451+static inline unsigned int pl08x_get_bytes_for_cctl(unsigned int coded)452452+{453453+ switch (coded) {454454+ case PL080_WIDTH_8BIT:455455+ return 1;456456+ case PL080_WIDTH_16BIT:457457+ return 2;458458+ case PL080_WIDTH_32BIT:459459+ return 4;460460+ default:461461+ break;462462+ }463463+ BUG();464464+ return 0;465465+}466466+467467+static inline u32 pl08x_cctl_bits(u32 cctl, u8 srcwidth, u8 dstwidth,468468+ u32 tsize)469469+{470470+ u32 retbits = cctl;471471+472472+ /* Remove all src, dst and transfersize bits */473473+ retbits &= ~PL080_CONTROL_DWIDTH_MASK;474474+ retbits &= ~PL080_CONTROL_SWIDTH_MASK;475475+ retbits &= ~PL080_CONTROL_TRANSFER_SIZE_MASK;476476+477477+ /* Then set the bits according to the parameters */478478+ switch (srcwidth) {479479+ case 1:480480+ retbits |= PL080_WIDTH_8BIT << PL080_CONTROL_SWIDTH_SHIFT;481481+ break;482482+ case 2:483483+ retbits |= PL080_WIDTH_16BIT << PL080_CONTROL_SWIDTH_SHIFT;484484+ break;485485+ case 4:486486+ retbits |= PL080_WIDTH_32BIT << PL080_CONTROL_SWIDTH_SHIFT;487487+ break;488488+ default:489489+ BUG();490490+ break;491491+ }492492+493493+ switch (dstwidth) {494494+ case 1:495495+ retbits |= PL080_WIDTH_8BIT << PL080_CONTROL_DWIDTH_SHIFT;496496+ break;497497+ case 2:498498+ retbits |= PL080_WIDTH_16BIT << PL080_CONTROL_DWIDTH_SHIFT;499499+ break;500500+ case 4:501501+ retbits |= PL080_WIDTH_32BIT << PL080_CONTROL_DWIDTH_SHIFT;502502+ break;503503+ default:504504+ BUG();505505+ break;506506+ }507507+508508+ retbits |= tsize << PL080_CONTROL_TRANSFER_SIZE_SHIFT;509509+ return retbits;510510+}511511+512512+/*513513+ * Autoselect a master bus to use for the transfer514514+ * this prefers the destination bus if both available515515+ * if fixed address on one bus the other will be chosen516516+ */517517+void pl08x_choose_master_bus(struct pl08x_bus_data *src_bus,518518+ struct pl08x_bus_data *dst_bus, struct pl08x_bus_data **mbus,519519+ struct pl08x_bus_data **sbus, u32 cctl)520520+{521521+ if (!(cctl & PL080_CONTROL_DST_INCR)) {522522+ *mbus = src_bus;523523+ *sbus = dst_bus;524524+ } else if (!(cctl & PL080_CONTROL_SRC_INCR)) {525525+ *mbus = dst_bus;526526+ *sbus = src_bus;527527+ } else {528528+ if (dst_bus->buswidth == 4) {529529+ *mbus = dst_bus;530530+ *sbus = src_bus;531531+ } else if (src_bus->buswidth == 4) {532532+ *mbus = src_bus;533533+ *sbus = dst_bus;534534+ } else if (dst_bus->buswidth == 2) {535535+ *mbus = dst_bus;536536+ *sbus = src_bus;537537+ } else if (src_bus->buswidth == 2) {538538+ *mbus = src_bus;539539+ *sbus = dst_bus;540540+ } else {541541+ /* src_bus->buswidth == 1 */542542+ *mbus = dst_bus;543543+ *sbus = src_bus;544544+ }545545+ }546546+}547547+548548+/*549549+ * Fills in one LLI for a certain transfer descriptor550550+ * and advance the counter551551+ */552552+int pl08x_fill_lli_for_desc(struct pl08x_driver_data *pl08x,553553+ struct pl08x_txd *txd, int num_llis, int len,554554+ u32 cctl, u32 *remainder)555555+{556556+ struct lli *llis_va = txd->llis_va;557557+ struct lli *llis_bus = (struct lli *) txd->llis_bus;558558+559559+ BUG_ON(num_llis >= MAX_NUM_TSFR_LLIS);560560+561561+ llis_va[num_llis].cctl = cctl;562562+ llis_va[num_llis].src = txd->srcbus.addr;563563+ llis_va[num_llis].dst = txd->dstbus.addr;564564+565565+ /*566566+ * On versions with dual masters, you can optionally AND on567567+ * PL080_LLI_LM_AHB2 to the LLI to tell the hardware to read568568+ * in new LLIs with that controller, but we always try to569569+ * choose AHB1 to point into memory. The idea is to have AHB2570570+ * fixed on the peripheral and AHB1 messing around in the571571+ * memory. So we don't manipulate this bit currently.572572+ */573573+574574+ llis_va[num_llis].next =575575+ (dma_addr_t)((u32) &(llis_bus[num_llis + 1]));576576+577577+ if (cctl & PL080_CONTROL_SRC_INCR)578578+ txd->srcbus.addr += len;579579+ if (cctl & PL080_CONTROL_DST_INCR)580580+ txd->dstbus.addr += len;581581+582582+ *remainder -= len;583583+584584+ return num_llis + 1;585585+}586586+587587+/*588588+ * Return number of bytes to fill to boundary, or len589589+ */590590+static inline u32 pl08x_pre_boundary(u32 addr, u32 len)591591+{592592+ u32 boundary;593593+594594+ boundary = ((addr >> PL08X_BOUNDARY_SHIFT) + 1)595595+ << PL08X_BOUNDARY_SHIFT;596596+597597+ if (boundary < addr + len)598598+ return boundary - addr;599599+ else600600+ return len;601601+}602602+603603+/*604604+ * This fills in the table of LLIs for the transfer descriptor605605+ * Note that we assume we never have to change the burst sizes606606+ * Return 0 for error607607+ */608608+static int pl08x_fill_llis_for_desc(struct pl08x_driver_data *pl08x,609609+ struct pl08x_txd *txd)610610+{611611+ struct pl08x_channel_data *cd = txd->cd;612612+ struct pl08x_bus_data *mbus, *sbus;613613+ u32 remainder;614614+ int num_llis = 0;615615+ u32 cctl;616616+ int max_bytes_per_lli;617617+ int total_bytes = 0;618618+ struct lli *llis_va;619619+ struct lli *llis_bus;620620+621621+ if (!txd) {622622+ dev_err(&pl08x->adev->dev, "%s no descriptor\n", __func__);623623+ return 0;624624+ }625625+626626+ txd->llis_va = dma_pool_alloc(pl08x->pool, GFP_NOWAIT,627627+ &txd->llis_bus);628628+ if (!txd->llis_va) {629629+ dev_err(&pl08x->adev->dev, "%s no memory for llis\n", __func__);630630+ return 0;631631+ }632632+633633+ pl08x->pool_ctr++;634634+635635+ /*636636+ * Initialize bus values for this transfer637637+ * from the passed optimal values638638+ */639639+ if (!cd) {640640+ dev_err(&pl08x->adev->dev, "%s no channel data\n", __func__);641641+ return 0;642642+ }643643+644644+ /* Get the default CCTL from the platform data */645645+ cctl = cd->cctl;646646+647647+ /*648648+ * On the PL080 we have two bus masters and we649649+ * should select one for source and one for650650+ * destination. We try to use AHB2 for the651651+ * bus which does not increment (typically the652652+ * peripheral) else we just choose something.653653+ */654654+ cctl &= ~(PL080_CONTROL_DST_AHB2 | PL080_CONTROL_SRC_AHB2);655655+ if (pl08x->vd->dualmaster) {656656+ if (cctl & PL080_CONTROL_SRC_INCR)657657+ /* Source increments, use AHB2 for destination */658658+ cctl |= PL080_CONTROL_DST_AHB2;659659+ else if (cctl & PL080_CONTROL_DST_INCR)660660+ /* Destination increments, use AHB2 for source */661661+ cctl |= PL080_CONTROL_SRC_AHB2;662662+ else663663+ /* Just pick something, source AHB1 dest AHB2 */664664+ cctl |= PL080_CONTROL_DST_AHB2;665665+ }666666+667667+ /* Find maximum width of the source bus */668668+ txd->srcbus.maxwidth =669669+ pl08x_get_bytes_for_cctl((cctl & PL080_CONTROL_SWIDTH_MASK) >>670670+ PL080_CONTROL_SWIDTH_SHIFT);671671+672672+ /* Find maximum width of the destination bus */673673+ txd->dstbus.maxwidth =674674+ pl08x_get_bytes_for_cctl((cctl & PL080_CONTROL_DWIDTH_MASK) >>675675+ PL080_CONTROL_DWIDTH_SHIFT);676676+677677+ /* Set up the bus widths to the maximum */678678+ txd->srcbus.buswidth = txd->srcbus.maxwidth;679679+ txd->dstbus.buswidth = txd->dstbus.maxwidth;680680+ dev_vdbg(&pl08x->adev->dev,681681+ "%s source bus is %d bytes wide, dest bus is %d bytes wide\n",682682+ __func__, txd->srcbus.buswidth, txd->dstbus.buswidth);683683+684684+685685+ /*686686+ * Bytes transferred == tsize * MIN(buswidths), not max(buswidths)687687+ */688688+ max_bytes_per_lli = min(txd->srcbus.buswidth, txd->dstbus.buswidth) *689689+ PL080_CONTROL_TRANSFER_SIZE_MASK;690690+ dev_vdbg(&pl08x->adev->dev,691691+ "%s max bytes per lli = %d\n",692692+ __func__, max_bytes_per_lli);693693+694694+ /* We need to count this down to zero */695695+ remainder = txd->len;696696+ dev_vdbg(&pl08x->adev->dev,697697+ "%s remainder = %d\n",698698+ __func__, remainder);699699+700700+ /*701701+ * Choose bus to align to702702+ * - prefers destination bus if both available703703+ * - if fixed address on one bus chooses other704704+ * - modifies cctl to choose an apropriate master705705+ */706706+ pl08x_choose_master_bus(&txd->srcbus, &txd->dstbus,707707+ &mbus, &sbus, cctl);708708+709709+710710+ /*711711+ * The lowest bit of the LLI register712712+ * is also used to indicate which master to713713+ * use for reading the LLIs.714714+ */715715+716716+ if (txd->len < mbus->buswidth) {717717+ /*718718+ * Less than a bus width available719719+ * - send as single bytes720720+ */721721+ while (remainder) {722722+ dev_vdbg(&pl08x->adev->dev,723723+ "%s single byte LLIs for a transfer of "724724+ "less than a bus width (remain %08x)\n",725725+ __func__, remainder);726726+ cctl = pl08x_cctl_bits(cctl, 1, 1, 1);727727+ num_llis =728728+ pl08x_fill_lli_for_desc(pl08x, txd, num_llis, 1,729729+ cctl, &remainder);730730+ total_bytes++;731731+ }732732+ } else {733733+ /*734734+ * Make one byte LLIs until master bus is aligned735735+ * - slave will then be aligned also736736+ */737737+ while ((mbus->addr) % (mbus->buswidth)) {738738+ dev_vdbg(&pl08x->adev->dev,739739+ "%s adjustment lli for less than bus width "740740+ "(remain %08x)\n",741741+ __func__, remainder);742742+ cctl = pl08x_cctl_bits(cctl, 1, 1, 1);743743+ num_llis = pl08x_fill_lli_for_desc744744+ (pl08x, txd, num_llis, 1, cctl, &remainder);745745+ total_bytes++;746746+ }747747+748748+ /*749749+ * Master now aligned750750+ * - if slave is not then we must set its width down751751+ */752752+ if (sbus->addr % sbus->buswidth) {753753+ dev_dbg(&pl08x->adev->dev,754754+ "%s set down bus width to one byte\n",755755+ __func__);756756+757757+ sbus->buswidth = 1;758758+ }759759+760760+ /*761761+ * Make largest possible LLIs until less than one bus762762+ * width left763763+ */764764+ while (remainder > (mbus->buswidth - 1)) {765765+ int lli_len, target_len;766766+ int tsize;767767+ int odd_bytes;768768+769769+ /*770770+ * If enough left try to send max possible,771771+ * otherwise try to send the remainder772772+ */773773+ target_len = remainder;774774+ if (remainder > max_bytes_per_lli)775775+ target_len = max_bytes_per_lli;776776+777777+ /*778778+ * Set bus lengths for incrementing busses779779+ * to number of bytes which fill to next memory780780+ * boundary781781+ */782782+ if (cctl & PL080_CONTROL_SRC_INCR)783783+ txd->srcbus.fill_bytes =784784+ pl08x_pre_boundary(785785+ txd->srcbus.addr,786786+ remainder);787787+ else788788+ txd->srcbus.fill_bytes =789789+ max_bytes_per_lli;790790+791791+ if (cctl & PL080_CONTROL_DST_INCR)792792+ txd->dstbus.fill_bytes =793793+ pl08x_pre_boundary(794794+ txd->dstbus.addr,795795+ remainder);796796+ else797797+ txd->dstbus.fill_bytes =798798+ max_bytes_per_lli;799799+800800+ /*801801+ * Find the nearest802802+ */803803+ lli_len = min(txd->srcbus.fill_bytes,804804+ txd->dstbus.fill_bytes);805805+806806+ BUG_ON(lli_len > remainder);807807+808808+ if (lli_len <= 0) {809809+ dev_err(&pl08x->adev->dev,810810+ "%s lli_len is %d, <= 0\n",811811+ __func__, lli_len);812812+ return 0;813813+ }814814+815815+ if (lli_len == target_len) {816816+ /*817817+ * Can send what we wanted818818+ */819819+ /*820820+ * Maintain alignment821821+ */822822+ lli_len = (lli_len/mbus->buswidth) *823823+ mbus->buswidth;824824+ odd_bytes = 0;825825+ } else {826826+ /*827827+ * So now we know how many bytes to transfer828828+ * to get to the nearest boundary829829+ * The next lli will past the boundary830830+ * - however we may be working to a boundary831831+ * on the slave bus832832+ * We need to ensure the master stays aligned833833+ */834834+ odd_bytes = lli_len % mbus->buswidth;835835+ /*836836+ * - and that we are working in multiples837837+ * of the bus widths838838+ */839839+ lli_len -= odd_bytes;840840+841841+ }842842+843843+ if (lli_len) {844844+ /*845845+ * Check against minimum bus alignment:846846+ * Calculate actual transfer size in relation847847+ * to bus width an get a maximum remainder of848848+ * the smallest bus width - 1849849+ */850850+ /* FIXME: use round_down()? */851851+ tsize = lli_len / min(mbus->buswidth,852852+ sbus->buswidth);853853+ lli_len = tsize * min(mbus->buswidth,854854+ sbus->buswidth);855855+856856+ if (target_len != lli_len) {857857+ dev_vdbg(&pl08x->adev->dev,858858+ "%s can't send what we want. Desired %08x, lli of %08x bytes in txd of %08x\n",859859+ __func__, target_len, lli_len, txd->len);860860+ }861861+862862+ cctl = pl08x_cctl_bits(cctl,863863+ txd->srcbus.buswidth,864864+ txd->dstbus.buswidth,865865+ tsize);866866+867867+ dev_vdbg(&pl08x->adev->dev,868868+ "%s fill lli with single lli chunk of size %08x (remainder %08x)\n",869869+ __func__, lli_len, remainder);870870+ num_llis = pl08x_fill_lli_for_desc(pl08x, txd,871871+ num_llis, lli_len, cctl,872872+ &remainder);873873+ total_bytes += lli_len;874874+ }875875+876876+877877+ if (odd_bytes) {878878+ /*879879+ * Creep past the boundary,880880+ * maintaining master alignment881881+ */882882+ int j;883883+ for (j = 0; (j < mbus->buswidth)884884+ && (remainder); j++) {885885+ cctl = pl08x_cctl_bits(cctl, 1, 1, 1);886886+ dev_vdbg(&pl08x->adev->dev,887887+ "%s align with boundardy, single byte (remain %08x)\n",888888+ __func__, remainder);889889+ num_llis =890890+ pl08x_fill_lli_for_desc(pl08x,891891+ txd, num_llis, 1,892892+ cctl, &remainder);893893+ total_bytes++;894894+ }895895+ }896896+ }897897+898898+ /*899899+ * Send any odd bytes900900+ */901901+ if (remainder < 0) {902902+ dev_err(&pl08x->adev->dev, "%s remainder not fitted 0x%08x bytes\n",903903+ __func__, remainder);904904+ return 0;905905+ }906906+907907+ while (remainder) {908908+ cctl = pl08x_cctl_bits(cctl, 1, 1, 1);909909+ dev_vdbg(&pl08x->adev->dev,910910+ "%s align with boundardy, single odd byte (remain %d)\n",911911+ __func__, remainder);912912+ num_llis = pl08x_fill_lli_for_desc(pl08x, txd, num_llis,913913+ 1, cctl, &remainder);914914+ total_bytes++;915915+ }916916+ }917917+ if (total_bytes != txd->len) {918918+ dev_err(&pl08x->adev->dev,919919+ "%s size of encoded lli:s don't match total txd, transferred 0x%08x from size 0x%08x\n",920920+ __func__, total_bytes, txd->len);921921+ return 0;922922+ }923923+924924+ if (num_llis >= MAX_NUM_TSFR_LLIS) {925925+ dev_err(&pl08x->adev->dev,926926+ "%s need to increase MAX_NUM_TSFR_LLIS from 0x%08x\n",927927+ __func__, (u32) MAX_NUM_TSFR_LLIS);928928+ return 0;929929+ }930930+ /*931931+ * Decide whether this is a loop or a terminated transfer932932+ */933933+ llis_va = txd->llis_va;934934+ llis_bus = (struct lli *) txd->llis_bus;935935+936936+ if (cd->circular_buffer) {937937+ /*938938+ * Loop the circular buffer so that the next element939939+ * points back to the beginning of the LLI.940940+ */941941+ llis_va[num_llis - 1].next =942942+ (dma_addr_t)((unsigned int)&(llis_bus[0]));943943+ } else {944944+ /*945945+ * On non-circular buffers, the final LLI terminates946946+ * the LLI.947947+ */948948+ llis_va[num_llis - 1].next = 0;949949+ /*950950+ * The final LLI element shall also fire an interrupt951951+ */952952+ llis_va[num_llis - 1].cctl |= PL080_CONTROL_TC_IRQ_EN;953953+ }954954+955955+ /* Now store the channel register values */956956+ txd->csrc = llis_va[0].src;957957+ txd->cdst = llis_va[0].dst;958958+ if (num_llis > 1)959959+ txd->clli = llis_va[0].next;960960+ else961961+ txd->clli = 0;962962+963963+ txd->cctl = llis_va[0].cctl;964964+ /* ccfg will be set at physical channel allocation time */965965+966966+#ifdef VERBOSE_DEBUG967967+ {968968+ int i;969969+970970+ for (i = 0; i < num_llis; i++) {971971+ dev_vdbg(&pl08x->adev->dev,972972+ "lli %d @%p: csrc=%08x, cdst=%08x, cctl=%08x, clli=%08x\n",973973+ i,974974+ &llis_va[i],975975+ llis_va[i].src,976976+ llis_va[i].dst,977977+ llis_va[i].cctl,978978+ llis_va[i].next979979+ );980980+ }981981+ }982982+#endif983983+984984+ return num_llis;985985+}986986+987987+/* You should call this with the struct pl08x lock held */988988+static void pl08x_free_txd(struct pl08x_driver_data *pl08x,989989+ struct pl08x_txd *txd)990990+{991991+ if (!txd)992992+ dev_err(&pl08x->adev->dev,993993+ "%s no descriptor to free\n",994994+ __func__);995995+996996+ /* Free the LLI */997997+ dma_pool_free(pl08x->pool, txd->llis_va,998998+ txd->llis_bus);999999+10001000+ pl08x->pool_ctr--;10011001+10021002+ kfree(txd);10031003+}10041004+10051005+static void pl08x_free_txd_list(struct pl08x_driver_data *pl08x,10061006+ struct pl08x_dma_chan *plchan)10071007+{10081008+ struct pl08x_txd *txdi = NULL;10091009+ struct pl08x_txd *next;10101010+10111011+ if (!list_empty(&plchan->desc_list)) {10121012+ list_for_each_entry_safe(txdi,10131013+ next, &plchan->desc_list, node) {10141014+ list_del(&txdi->node);10151015+ pl08x_free_txd(pl08x, txdi);10161016+ }10171017+10181018+ }10191019+}10201020+10211021+/*10221022+ * The DMA ENGINE API10231023+ */10241024+static int pl08x_alloc_chan_resources(struct dma_chan *chan)10251025+{10261026+ return 0;10271027+}10281028+10291029+static void pl08x_free_chan_resources(struct dma_chan *chan)10301030+{10311031+}10321032+10331033+/*10341034+ * This should be called with the channel plchan->lock held10351035+ */10361036+static int prep_phy_channel(struct pl08x_dma_chan *plchan,10371037+ struct pl08x_txd *txd)10381038+{10391039+ struct pl08x_driver_data *pl08x = plchan->host;10401040+ struct pl08x_phy_chan *ch;10411041+ int ret;10421042+10431043+ /* Check if we already have a channel */10441044+ if (plchan->phychan)10451045+ return 0;10461046+10471047+ ch = pl08x_get_phy_channel(pl08x, plchan);10481048+ if (!ch) {10491049+ /* No physical channel available, cope with it */10501050+ dev_dbg(&pl08x->adev->dev, "no physical channel available for xfer on %s\n", plchan->name);10511051+ return -EBUSY;10521052+ }10531053+10541054+ /*10551055+ * OK we have a physical channel: for memcpy() this is all we10561056+ * need, but for slaves the physical signals may be muxed!10571057+ * Can the platform allow us to use this channel?10581058+ */10591059+ if (plchan->slave &&10601060+ ch->signal < 0 &&10611061+ pl08x->pd->get_signal) {10621062+ ret = pl08x->pd->get_signal(plchan);10631063+ if (ret < 0) {10641064+ dev_dbg(&pl08x->adev->dev,10651065+ "unable to use physical channel %d for transfer on %s due to platform restrictions\n",10661066+ ch->id, plchan->name);10671067+ /* Release physical channel & return */10681068+ pl08x_put_phy_channel(pl08x, ch);10691069+ return -EBUSY;10701070+ }10711071+ ch->signal = ret;10721072+ }10731073+10741074+ dev_dbg(&pl08x->adev->dev, "allocated physical channel %d and signal %d for xfer on %s\n",10751075+ ch->id,10761076+ ch->signal,10771077+ plchan->name);10781078+10791079+ plchan->phychan = ch;10801080+10811081+ return 0;10821082+}10831083+10841084+static dma_cookie_t pl08x_tx_submit(struct dma_async_tx_descriptor *tx)10851085+{10861086+ struct pl08x_dma_chan *plchan = to_pl08x_chan(tx->chan);10871087+10881088+ atomic_inc(&plchan->last_issued);10891089+ tx->cookie = atomic_read(&plchan->last_issued);10901090+ /* This unlock follows the lock in the prep() function */10911091+ spin_unlock_irqrestore(&plchan->lock, plchan->lockflags);10921092+10931093+ return tx->cookie;10941094+}10951095+10961096+static struct dma_async_tx_descriptor *pl08x_prep_dma_interrupt(10971097+ struct dma_chan *chan, unsigned long flags)10981098+{10991099+ struct dma_async_tx_descriptor *retval = NULL;11001100+11011101+ return retval;11021102+}11031103+11041104+/*11051105+ * Code accessing dma_async_is_complete() in a tight loop11061106+ * may give problems - could schedule where indicated.11071107+ * If slaves are relying on interrupts to signal completion this11081108+ * function must not be called with interrupts disabled11091109+ */11101110+static enum dma_status11111111+pl08x_dma_tx_status(struct dma_chan *chan,11121112+ dma_cookie_t cookie,11131113+ struct dma_tx_state *txstate)11141114+{11151115+ struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);11161116+ dma_cookie_t last_used;11171117+ dma_cookie_t last_complete;11181118+ enum dma_status ret;11191119+ u32 bytesleft = 0;11201120+11211121+ last_used = atomic_read(&plchan->last_issued);11221122+ last_complete = plchan->lc;11231123+11241124+ ret = dma_async_is_complete(cookie, last_complete, last_used);11251125+ if (ret == DMA_SUCCESS) {11261126+ dma_set_tx_state(txstate, last_complete, last_used, 0);11271127+ return ret;11281128+ }11291129+11301130+ /*11311131+ * schedule(); could be inserted here11321132+ */11331133+11341134+ /*11351135+ * This cookie not complete yet11361136+ */11371137+ last_used = atomic_read(&plchan->last_issued);11381138+ last_complete = plchan->lc;11391139+11401140+ /* Get number of bytes left in the active transactions and queue */11411141+ bytesleft = pl08x_getbytes_chan(plchan);11421142+11431143+ dma_set_tx_state(txstate, last_complete, last_used,11441144+ bytesleft);11451145+11461146+ if (plchan->state == PL08X_CHAN_PAUSED)11471147+ return DMA_PAUSED;11481148+11491149+ /* Whether waiting or running, we're in progress */11501150+ return DMA_IN_PROGRESS;11511151+}11521152+11531153+/* PrimeCell DMA extension */11541154+struct burst_table {11551155+ int burstwords;11561156+ u32 reg;11571157+};11581158+11591159+static const struct burst_table burst_sizes[] = {11601160+ {11611161+ .burstwords = 256,11621162+ .reg = (PL080_BSIZE_256 << PL080_CONTROL_SB_SIZE_SHIFT) |11631163+ (PL080_BSIZE_256 << PL080_CONTROL_DB_SIZE_SHIFT),11641164+ },11651165+ {11661166+ .burstwords = 128,11671167+ .reg = (PL080_BSIZE_128 << PL080_CONTROL_SB_SIZE_SHIFT) |11681168+ (PL080_BSIZE_128 << PL080_CONTROL_DB_SIZE_SHIFT),11691169+ },11701170+ {11711171+ .burstwords = 64,11721172+ .reg = (PL080_BSIZE_64 << PL080_CONTROL_SB_SIZE_SHIFT) |11731173+ (PL080_BSIZE_64 << PL080_CONTROL_DB_SIZE_SHIFT),11741174+ },11751175+ {11761176+ .burstwords = 32,11771177+ .reg = (PL080_BSIZE_32 << PL080_CONTROL_SB_SIZE_SHIFT) |11781178+ (PL080_BSIZE_32 << PL080_CONTROL_DB_SIZE_SHIFT),11791179+ },11801180+ {11811181+ .burstwords = 16,11821182+ .reg = (PL080_BSIZE_16 << PL080_CONTROL_SB_SIZE_SHIFT) |11831183+ (PL080_BSIZE_16 << PL080_CONTROL_DB_SIZE_SHIFT),11841184+ },11851185+ {11861186+ .burstwords = 8,11871187+ .reg = (PL080_BSIZE_8 << PL080_CONTROL_SB_SIZE_SHIFT) |11881188+ (PL080_BSIZE_8 << PL080_CONTROL_DB_SIZE_SHIFT),11891189+ },11901190+ {11911191+ .burstwords = 4,11921192+ .reg = (PL080_BSIZE_4 << PL080_CONTROL_SB_SIZE_SHIFT) |11931193+ (PL080_BSIZE_4 << PL080_CONTROL_DB_SIZE_SHIFT),11941194+ },11951195+ {11961196+ .burstwords = 1,11971197+ .reg = (PL080_BSIZE_1 << PL080_CONTROL_SB_SIZE_SHIFT) |11981198+ (PL080_BSIZE_1 << PL080_CONTROL_DB_SIZE_SHIFT),11991199+ },12001200+};12011201+12021202+static void dma_set_runtime_config(struct dma_chan *chan,12031203+ struct dma_slave_config *config)12041204+{12051205+ struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);12061206+ struct pl08x_driver_data *pl08x = plchan->host;12071207+ struct pl08x_channel_data *cd = plchan->cd;12081208+ enum dma_slave_buswidth addr_width;12091209+ u32 maxburst;12101210+ u32 cctl = 0;12111211+ /* Mask out all except src and dst channel */12121212+ u32 ccfg = cd->ccfg & 0x000003DEU;12131213+ int i = 0;12141214+12151215+ /* Transfer direction */12161216+ plchan->runtime_direction = config->direction;12171217+ if (config->direction == DMA_TO_DEVICE) {12181218+ plchan->runtime_addr = config->dst_addr;12191219+ cctl |= PL080_CONTROL_SRC_INCR;12201220+ ccfg |= PL080_FLOW_MEM2PER << PL080_CONFIG_FLOW_CONTROL_SHIFT;12211221+ addr_width = config->dst_addr_width;12221222+ maxburst = config->dst_maxburst;12231223+ } else if (config->direction == DMA_FROM_DEVICE) {12241224+ plchan->runtime_addr = config->src_addr;12251225+ cctl |= PL080_CONTROL_DST_INCR;12261226+ ccfg |= PL080_FLOW_PER2MEM << PL080_CONFIG_FLOW_CONTROL_SHIFT;12271227+ addr_width = config->src_addr_width;12281228+ maxburst = config->src_maxburst;12291229+ } else {12301230+ dev_err(&pl08x->adev->dev,12311231+ "bad runtime_config: alien transfer direction\n");12321232+ return;12331233+ }12341234+12351235+ switch (addr_width) {12361236+ case DMA_SLAVE_BUSWIDTH_1_BYTE:12371237+ cctl |= (PL080_WIDTH_8BIT << PL080_CONTROL_SWIDTH_SHIFT) |12381238+ (PL080_WIDTH_8BIT << PL080_CONTROL_DWIDTH_SHIFT);12391239+ break;12401240+ case DMA_SLAVE_BUSWIDTH_2_BYTES:12411241+ cctl |= (PL080_WIDTH_16BIT << PL080_CONTROL_SWIDTH_SHIFT) |12421242+ (PL080_WIDTH_16BIT << PL080_CONTROL_DWIDTH_SHIFT);12431243+ break;12441244+ case DMA_SLAVE_BUSWIDTH_4_BYTES:12451245+ cctl |= (PL080_WIDTH_32BIT << PL080_CONTROL_SWIDTH_SHIFT) |12461246+ (PL080_WIDTH_32BIT << PL080_CONTROL_DWIDTH_SHIFT);12471247+ break;12481248+ default:12491249+ dev_err(&pl08x->adev->dev,12501250+ "bad runtime_config: alien address width\n");12511251+ return;12521252+ }12531253+12541254+ /*12551255+ * Now decide on a maxburst:12561256+ * If this channel will only request single transfers, set12571257+ * this down to ONE element.12581258+ */12591259+ if (plchan->cd->single) {12601260+ cctl |= (PL080_BSIZE_1 << PL080_CONTROL_SB_SIZE_SHIFT) |12611261+ (PL080_BSIZE_1 << PL080_CONTROL_DB_SIZE_SHIFT);12621262+ } else {12631263+ while (i < ARRAY_SIZE(burst_sizes)) {12641264+ if (burst_sizes[i].burstwords <= maxburst)12651265+ break;12661266+ i++;12671267+ }12681268+ cctl |= burst_sizes[i].reg;12691269+ }12701270+12711271+ /* Access the cell in privileged mode, non-bufferable, non-cacheable */12721272+ cctl &= ~PL080_CONTROL_PROT_MASK;12731273+ cctl |= PL080_CONTROL_PROT_SYS;12741274+12751275+ /* Modify the default channel data to fit PrimeCell request */12761276+ cd->cctl = cctl;12771277+ cd->ccfg = ccfg;12781278+12791279+ dev_dbg(&pl08x->adev->dev,12801280+ "configured channel %s (%s) for %s, data width %d, "12811281+ "maxburst %d words, LE, CCTL=%08x, CCFG=%08x\n",12821282+ dma_chan_name(chan), plchan->name,12831283+ (config->direction == DMA_FROM_DEVICE) ? "RX" : "TX",12841284+ addr_width,12851285+ maxburst,12861286+ cctl, ccfg);12871287+}12881288+12891289+/*12901290+ * Slave transactions callback to the slave device to allow12911291+ * synchronization of slave DMA signals with the DMAC enable12921292+ */12931293+static void pl08x_issue_pending(struct dma_chan *chan)12941294+{12951295+ struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);12961296+ struct pl08x_driver_data *pl08x = plchan->host;12971297+ unsigned long flags;12981298+12991299+ spin_lock_irqsave(&plchan->lock, flags);13001300+ /* Something is already active */13011301+ if (plchan->at) {13021302+ spin_unlock_irqrestore(&plchan->lock, flags);13031303+ return;13041304+ }13051305+13061306+ /* Didn't get a physical channel so waiting for it ... */13071307+ if (plchan->state == PL08X_CHAN_WAITING)13081308+ return;13091309+13101310+ /* Take the first element in the queue and execute it */13111311+ if (!list_empty(&plchan->desc_list)) {13121312+ struct pl08x_txd *next;13131313+13141314+ next = list_first_entry(&plchan->desc_list,13151315+ struct pl08x_txd,13161316+ node);13171317+ list_del(&next->node);13181318+ plchan->at = next;13191319+ plchan->state = PL08X_CHAN_RUNNING;13201320+13211321+ /* Configure the physical channel for the active txd */13221322+ pl08x_config_phychan_for_txd(plchan);13231323+ pl08x_set_cregs(pl08x, plchan->phychan);13241324+ pl08x_enable_phy_chan(pl08x, plchan->phychan);13251325+ }13261326+13271327+ spin_unlock_irqrestore(&plchan->lock, flags);13281328+}13291329+13301330+static int pl08x_prep_channel_resources(struct pl08x_dma_chan *plchan,13311331+ struct pl08x_txd *txd)13321332+{13331333+ int num_llis;13341334+ struct pl08x_driver_data *pl08x = plchan->host;13351335+ int ret;13361336+13371337+ num_llis = pl08x_fill_llis_for_desc(pl08x, txd);13381338+13391339+ if (!num_llis)13401340+ return -EINVAL;13411341+13421342+ spin_lock_irqsave(&plchan->lock, plchan->lockflags);13431343+13441344+ /*13451345+ * If this device is not using a circular buffer then13461346+ * queue this new descriptor for transfer.13471347+ * The descriptor for a circular buffer continues13481348+ * to be used until the channel is freed.13491349+ */13501350+ if (txd->cd->circular_buffer)13511351+ dev_err(&pl08x->adev->dev,13521352+ "%s attempting to queue a circular buffer\n",13531353+ __func__);13541354+ else13551355+ list_add_tail(&txd->node,13561356+ &plchan->desc_list);13571357+13581358+ /*13591359+ * See if we already have a physical channel allocated,13601360+ * else this is the time to try to get one.13611361+ */13621362+ ret = prep_phy_channel(plchan, txd);13631363+ if (ret) {13641364+ /*13651365+ * No physical channel available, we will13661366+ * stack up the memcpy channels until there is a channel13671367+ * available to handle it whereas slave transfers may13681368+ * have been denied due to platform channel muxing restrictions13691369+ * and since there is no guarantee that this will ever be13701370+ * resolved, and since the signal must be aquired AFTER13711371+ * aquiring the physical channel, we will let them be NACK:ed13721372+ * with -EBUSY here. The drivers can alway retry the prep()13731373+ * call if they are eager on doing this using DMA.13741374+ */13751375+ if (plchan->slave) {13761376+ pl08x_free_txd_list(pl08x, plchan);13771377+ spin_unlock_irqrestore(&plchan->lock, plchan->lockflags);13781378+ return -EBUSY;13791379+ }13801380+ /* Do this memcpy whenever there is a channel ready */13811381+ plchan->state = PL08X_CHAN_WAITING;13821382+ plchan->waiting = txd;13831383+ } else13841384+ /*13851385+ * Else we're all set, paused and ready to roll,13861386+ * status will switch to PL08X_CHAN_RUNNING when13871387+ * we call issue_pending(). If there is something13881388+ * running on the channel already we don't change13891389+ * its state.13901390+ */13911391+ if (plchan->state == PL08X_CHAN_IDLE)13921392+ plchan->state = PL08X_CHAN_PAUSED;13931393+13941394+ /*13951395+ * Notice that we leave plchan->lock locked on purpose:13961396+ * it will be unlocked in the subsequent tx_submit()13971397+ * call. This is a consequence of the current API.13981398+ */13991399+14001400+ return 0;14011401+}14021402+14031403+/*14041404+ * Initialize a descriptor to be used by memcpy submit14051405+ */14061406+static struct dma_async_tx_descriptor *pl08x_prep_dma_memcpy(14071407+ struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,14081408+ size_t len, unsigned long flags)14091409+{14101410+ struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);14111411+ struct pl08x_driver_data *pl08x = plchan->host;14121412+ struct pl08x_txd *txd;14131413+ int ret;14141414+14151415+ txd = kzalloc(sizeof(struct pl08x_txd), GFP_NOWAIT);14161416+ if (!txd) {14171417+ dev_err(&pl08x->adev->dev,14181418+ "%s no memory for descriptor\n", __func__);14191419+ return NULL;14201420+ }14211421+14221422+ dma_async_tx_descriptor_init(&txd->tx, chan);14231423+ txd->direction = DMA_NONE;14241424+ txd->srcbus.addr = src;14251425+ txd->dstbus.addr = dest;14261426+14271427+ /* Set platform data for m2m */14281428+ txd->cd = &pl08x->pd->memcpy_channel;14291429+ /* Both to be incremented or the code will break */14301430+ txd->cd->cctl |= PL080_CONTROL_SRC_INCR | PL080_CONTROL_DST_INCR;14311431+ txd->tx.tx_submit = pl08x_tx_submit;14321432+ txd->tx.callback = NULL;14331433+ txd->tx.callback_param = NULL;14341434+ txd->len = len;14351435+14361436+ INIT_LIST_HEAD(&txd->node);14371437+ ret = pl08x_prep_channel_resources(plchan, txd);14381438+ if (ret)14391439+ return NULL;14401440+ /*14411441+ * NB: the channel lock is held at this point so tx_submit()14421442+ * must be called in direct succession.14431443+ */14441444+14451445+ return &txd->tx;14461446+}14471447+14481448+struct dma_async_tx_descriptor *pl08x_prep_slave_sg(14491449+ struct dma_chan *chan, struct scatterlist *sgl,14501450+ unsigned int sg_len, enum dma_data_direction direction,14511451+ unsigned long flags)14521452+{14531453+ struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);14541454+ struct pl08x_driver_data *pl08x = plchan->host;14551455+ struct pl08x_txd *txd;14561456+ int ret;14571457+14581458+ /*14591459+ * Current implementation ASSUMES only one sg14601460+ */14611461+ if (sg_len != 1) {14621462+ dev_err(&pl08x->adev->dev, "%s prepared too long sglist\n",14631463+ __func__);14641464+ BUG();14651465+ }14661466+14671467+ dev_dbg(&pl08x->adev->dev, "%s prepare transaction of %d bytes from %s\n",14681468+ __func__, sgl->length, plchan->name);14691469+14701470+ txd = kzalloc(sizeof(struct pl08x_txd), GFP_NOWAIT);14711471+ if (!txd) {14721472+ dev_err(&pl08x->adev->dev, "%s no txd\n", __func__);14731473+ return NULL;14741474+ }14751475+14761476+ dma_async_tx_descriptor_init(&txd->tx, chan);14771477+14781478+ if (direction != plchan->runtime_direction)14791479+ dev_err(&pl08x->adev->dev, "%s DMA setup does not match "14801480+ "the direction configured for the PrimeCell\n",14811481+ __func__);14821482+14831483+ /*14841484+ * Set up addresses, the PrimeCell configured address14851485+ * will take precedence since this may configure the14861486+ * channel target address dynamically at runtime.14871487+ */14881488+ txd->direction = direction;14891489+ if (direction == DMA_TO_DEVICE) {14901490+ txd->srcbus.addr = sgl->dma_address;14911491+ if (plchan->runtime_addr)14921492+ txd->dstbus.addr = plchan->runtime_addr;14931493+ else14941494+ txd->dstbus.addr = plchan->cd->addr;14951495+ } else if (direction == DMA_FROM_DEVICE) {14961496+ if (plchan->runtime_addr)14971497+ txd->srcbus.addr = plchan->runtime_addr;14981498+ else14991499+ txd->srcbus.addr = plchan->cd->addr;15001500+ txd->dstbus.addr = sgl->dma_address;15011501+ } else {15021502+ dev_err(&pl08x->adev->dev,15031503+ "%s direction unsupported\n", __func__);15041504+ return NULL;15051505+ }15061506+ txd->cd = plchan->cd;15071507+ txd->tx.tx_submit = pl08x_tx_submit;15081508+ txd->tx.callback = NULL;15091509+ txd->tx.callback_param = NULL;15101510+ txd->len = sgl->length;15111511+ INIT_LIST_HEAD(&txd->node);15121512+15131513+ ret = pl08x_prep_channel_resources(plchan, txd);15141514+ if (ret)15151515+ return NULL;15161516+ /*15171517+ * NB: the channel lock is held at this point so tx_submit()15181518+ * must be called in direct succession.15191519+ */15201520+15211521+ return &txd->tx;15221522+}15231523+15241524+static int pl08x_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,15251525+ unsigned long arg)15261526+{15271527+ struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);15281528+ struct pl08x_driver_data *pl08x = plchan->host;15291529+ unsigned long flags;15301530+ int ret = 0;15311531+15321532+ /* Controls applicable to inactive channels */15331533+ if (cmd == DMA_SLAVE_CONFIG) {15341534+ dma_set_runtime_config(chan,15351535+ (struct dma_slave_config *)15361536+ arg);15371537+ return 0;15381538+ }15391539+15401540+ /*15411541+ * Anything succeeds on channels with no physical allocation and15421542+ * no queued transfers.15431543+ */15441544+ spin_lock_irqsave(&plchan->lock, flags);15451545+ if (!plchan->phychan && !plchan->at) {15461546+ spin_unlock_irqrestore(&plchan->lock, flags);15471547+ return 0;15481548+ }15491549+15501550+ switch (cmd) {15511551+ case DMA_TERMINATE_ALL:15521552+ plchan->state = PL08X_CHAN_IDLE;15531553+15541554+ if (plchan->phychan) {15551555+ pl08x_stop_phy_chan(plchan->phychan);15561556+15571557+ /*15581558+ * Mark physical channel as free and free any slave15591559+ * signal15601560+ */15611561+ if ((plchan->phychan->signal >= 0) &&15621562+ pl08x->pd->put_signal) {15631563+ pl08x->pd->put_signal(plchan);15641564+ plchan->phychan->signal = -1;15651565+ }15661566+ pl08x_put_phy_channel(pl08x, plchan->phychan);15671567+ plchan->phychan = NULL;15681568+ }15691569+ /* Stop any pending tasklet */15701570+ tasklet_disable(&plchan->tasklet);15711571+ /* Dequeue jobs and free LLIs */15721572+ if (plchan->at) {15731573+ pl08x_free_txd(pl08x, plchan->at);15741574+ plchan->at = NULL;15751575+ }15761576+ /* Dequeue jobs not yet fired as well */15771577+ pl08x_free_txd_list(pl08x, plchan);15781578+ break;15791579+ case DMA_PAUSE:15801580+ pl08x_pause_phy_chan(plchan->phychan);15811581+ plchan->state = PL08X_CHAN_PAUSED;15821582+ break;15831583+ case DMA_RESUME:15841584+ pl08x_resume_phy_chan(plchan->phychan);15851585+ plchan->state = PL08X_CHAN_RUNNING;15861586+ break;15871587+ default:15881588+ /* Unknown command */15891589+ ret = -ENXIO;15901590+ break;15911591+ }15921592+15931593+ spin_unlock_irqrestore(&plchan->lock, flags);15941594+15951595+ return ret;15961596+}15971597+15981598+bool pl08x_filter_id(struct dma_chan *chan, void *chan_id)15991599+{16001600+ struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);16011601+ char *name = chan_id;16021602+16031603+ /* Check that the channel is not taken! */16041604+ if (!strcmp(plchan->name, name))16051605+ return true;16061606+16071607+ return false;16081608+}16091609+16101610+/*16111611+ * Just check that the device is there and active16121612+ * TODO: turn this bit on/off depending on the number of16131613+ * physical channels actually used, if it is zero... well16141614+ * shut it off. That will save some power. Cut the clock16151615+ * at the same time.16161616+ */16171617+static void pl08x_ensure_on(struct pl08x_driver_data *pl08x)16181618+{16191619+ u32 val;16201620+16211621+ val = readl(pl08x->base + PL080_CONFIG);16221622+ val &= ~(PL080_CONFIG_M2_BE | PL080_CONFIG_M1_BE | PL080_CONFIG_ENABLE);16231623+ /* We implictly clear bit 1 and that means little-endian mode */16241624+ val |= PL080_CONFIG_ENABLE;16251625+ writel(val, pl08x->base + PL080_CONFIG);16261626+}16271627+16281628+static void pl08x_tasklet(unsigned long data)16291629+{16301630+ struct pl08x_dma_chan *plchan = (struct pl08x_dma_chan *) data;16311631+ struct pl08x_phy_chan *phychan = plchan->phychan;16321632+ struct pl08x_driver_data *pl08x = plchan->host;16331633+16341634+ if (!plchan)16351635+ BUG();16361636+16371637+ spin_lock(&plchan->lock);16381638+16391639+ if (plchan->at) {16401640+ dma_async_tx_callback callback =16411641+ plchan->at->tx.callback;16421642+ void *callback_param =16431643+ plchan->at->tx.callback_param;16441644+16451645+ /*16461646+ * Update last completed16471647+ */16481648+ plchan->lc =16491649+ (plchan->at->tx.cookie);16501650+16511651+ /*16521652+ * Callback to signal completion16531653+ */16541654+ if (callback)16551655+ callback(callback_param);16561656+16571657+ /*16581658+ * Device callbacks should NOT clear16591659+ * the current transaction on the channel16601660+ * Linus: sometimes they should?16611661+ */16621662+ if (!plchan->at)16631663+ BUG();16641664+16651665+ /*16661666+ * Free the descriptor if it's not for a device16671667+ * using a circular buffer16681668+ */16691669+ if (!plchan->at->cd->circular_buffer) {16701670+ pl08x_free_txd(pl08x, plchan->at);16711671+ plchan->at = NULL;16721672+ }16731673+ /*16741674+ * else descriptor for circular16751675+ * buffers only freed when16761676+ * client has disabled dma16771677+ */16781678+ }16791679+ /*16801680+ * If a new descriptor is queued, set it up16811681+ * plchan->at is NULL here16821682+ */16831683+ if (!list_empty(&plchan->desc_list)) {16841684+ struct pl08x_txd *next;16851685+16861686+ next = list_first_entry(&plchan->desc_list,16871687+ struct pl08x_txd,16881688+ node);16891689+ list_del(&next->node);16901690+ plchan->at = next;16911691+ /* Configure the physical channel for the next txd */16921692+ pl08x_config_phychan_for_txd(plchan);16931693+ pl08x_set_cregs(pl08x, plchan->phychan);16941694+ pl08x_enable_phy_chan(pl08x, plchan->phychan);16951695+ } else {16961696+ struct pl08x_dma_chan *waiting = NULL;16971697+16981698+ /*16991699+ * No more jobs, so free up the physical channel17001700+ * Free any allocated signal on slave transfers too17011701+ */17021702+ if ((phychan->signal >= 0) && pl08x->pd->put_signal) {17031703+ pl08x->pd->put_signal(plchan);17041704+ phychan->signal = -1;17051705+ }17061706+ pl08x_put_phy_channel(pl08x, phychan);17071707+ plchan->phychan = NULL;17081708+ plchan->state = PL08X_CHAN_IDLE;17091709+17101710+ /*17111711+ * And NOW before anyone else can grab that free:d17121712+ * up physical channel, see if there is some memcpy17131713+ * pending that seriously needs to start because of17141714+ * being stacked up while we were choking the17151715+ * physical channels with data.17161716+ */17171717+ list_for_each_entry(waiting, &pl08x->memcpy.channels,17181718+ chan.device_node) {17191719+ if (waiting->state == PL08X_CHAN_WAITING &&17201720+ waiting->waiting != NULL) {17211721+ int ret;17221722+17231723+ /* This should REALLY not fail now */17241724+ ret = prep_phy_channel(waiting,17251725+ waiting->waiting);17261726+ BUG_ON(ret);17271727+ waiting->state = PL08X_CHAN_RUNNING;17281728+ waiting->waiting = NULL;17291729+ pl08x_issue_pending(&waiting->chan);17301730+ break;17311731+ }17321732+ }17331733+ }17341734+17351735+ spin_unlock(&plchan->lock);17361736+}17371737+17381738+static irqreturn_t pl08x_irq(int irq, void *dev)17391739+{17401740+ struct pl08x_driver_data *pl08x = dev;17411741+ u32 mask = 0;17421742+ u32 val;17431743+ int i;17441744+17451745+ val = readl(pl08x->base + PL080_ERR_STATUS);17461746+ if (val) {17471747+ /*17481748+ * An error interrupt (on one or more channels)17491749+ */17501750+ dev_err(&pl08x->adev->dev,17511751+ "%s error interrupt, register value 0x%08x\n",17521752+ __func__, val);17531753+ /*17541754+ * Simply clear ALL PL08X error interrupts,17551755+ * regardless of channel and cause17561756+ * FIXME: should be 0x00000003 on PL081 really.17571757+ */17581758+ writel(0x000000FF, pl08x->base + PL080_ERR_CLEAR);17591759+ }17601760+ val = readl(pl08x->base + PL080_INT_STATUS);17611761+ for (i = 0; i < pl08x->vd->channels; i++) {17621762+ if ((1 << i) & val) {17631763+ /* Locate physical channel */17641764+ struct pl08x_phy_chan *phychan = &pl08x->phy_chans[i];17651765+ struct pl08x_dma_chan *plchan = phychan->serving;17661766+17671767+ /* Schedule tasklet on this channel */17681768+ tasklet_schedule(&plchan->tasklet);17691769+17701770+ mask |= (1 << i);17711771+ }17721772+ }17731773+ /*17741774+ * Clear only the terminal interrupts on channels we processed17751775+ */17761776+ writel(mask, pl08x->base + PL080_TC_CLEAR);17771777+17781778+ return mask ? IRQ_HANDLED : IRQ_NONE;17791779+}17801780+17811781+/*17821782+ * Initialise the DMAC memcpy/slave channels.17831783+ * Make a local wrapper to hold required data17841784+ */17851785+static int pl08x_dma_init_virtual_channels(struct pl08x_driver_data *pl08x,17861786+ struct dma_device *dmadev,17871787+ unsigned int channels,17881788+ bool slave)17891789+{17901790+ struct pl08x_dma_chan *chan;17911791+ int i;17921792+17931793+ INIT_LIST_HEAD(&dmadev->channels);17941794+ /*17951795+ * Register as many many memcpy as we have physical channels,17961796+ * we won't always be able to use all but the code will have17971797+ * to cope with that situation.17981798+ */17991799+ for (i = 0; i < channels; i++) {18001800+ chan = kzalloc(sizeof(struct pl08x_dma_chan), GFP_KERNEL);18011801+ if (!chan) {18021802+ dev_err(&pl08x->adev->dev,18031803+ "%s no memory for channel\n", __func__);18041804+ return -ENOMEM;18051805+ }18061806+18071807+ chan->host = pl08x;18081808+ chan->state = PL08X_CHAN_IDLE;18091809+18101810+ if (slave) {18111811+ chan->slave = true;18121812+ chan->name = pl08x->pd->slave_channels[i].bus_id;18131813+ chan->cd = &pl08x->pd->slave_channels[i];18141814+ } else {18151815+ chan->cd = &pl08x->pd->memcpy_channel;18161816+ chan->name = kasprintf(GFP_KERNEL, "memcpy%d", i);18171817+ if (!chan->name) {18181818+ kfree(chan);18191819+ return -ENOMEM;18201820+ }18211821+ }18221822+ dev_info(&pl08x->adev->dev,18231823+ "initialize virtual channel \"%s\"\n",18241824+ chan->name);18251825+18261826+ chan->chan.device = dmadev;18271827+ atomic_set(&chan->last_issued, 0);18281828+ chan->lc = atomic_read(&chan->last_issued);18291829+18301830+ spin_lock_init(&chan->lock);18311831+ INIT_LIST_HEAD(&chan->desc_list);18321832+ tasklet_init(&chan->tasklet, pl08x_tasklet,18331833+ (unsigned long) chan);18341834+18351835+ list_add_tail(&chan->chan.device_node, &dmadev->channels);18361836+ }18371837+ dev_info(&pl08x->adev->dev, "initialized %d virtual %s channels\n",18381838+ i, slave ? "slave" : "memcpy");18391839+ return i;18401840+}18411841+18421842+static void pl08x_free_virtual_channels(struct dma_device *dmadev)18431843+{18441844+ struct pl08x_dma_chan *chan = NULL;18451845+ struct pl08x_dma_chan *next;18461846+18471847+ list_for_each_entry_safe(chan,18481848+ next, &dmadev->channels, chan.device_node) {18491849+ list_del(&chan->chan.device_node);18501850+ kfree(chan);18511851+ }18521852+}18531853+18541854+#ifdef CONFIG_DEBUG_FS18551855+static const char *pl08x_state_str(enum pl08x_dma_chan_state state)18561856+{18571857+ switch (state) {18581858+ case PL08X_CHAN_IDLE:18591859+ return "idle";18601860+ case PL08X_CHAN_RUNNING:18611861+ return "running";18621862+ case PL08X_CHAN_PAUSED:18631863+ return "paused";18641864+ case PL08X_CHAN_WAITING:18651865+ return "waiting";18661866+ default:18671867+ break;18681868+ }18691869+ return "UNKNOWN STATE";18701870+}18711871+18721872+static int pl08x_debugfs_show(struct seq_file *s, void *data)18731873+{18741874+ struct pl08x_driver_data *pl08x = s->private;18751875+ struct pl08x_dma_chan *chan;18761876+ struct pl08x_phy_chan *ch;18771877+ unsigned long flags;18781878+ int i;18791879+18801880+ seq_printf(s, "PL08x physical channels:\n");18811881+ seq_printf(s, "CHANNEL:\tUSER:\n");18821882+ seq_printf(s, "--------\t-----\n");18831883+ for (i = 0; i < pl08x->vd->channels; i++) {18841884+ struct pl08x_dma_chan *virt_chan;18851885+18861886+ ch = &pl08x->phy_chans[i];18871887+18881888+ spin_lock_irqsave(&ch->lock, flags);18891889+ virt_chan = ch->serving;18901890+18911891+ seq_printf(s, "%d\t\t%s\n",18921892+ ch->id, virt_chan ? virt_chan->name : "(none)");18931893+18941894+ spin_unlock_irqrestore(&ch->lock, flags);18951895+ }18961896+18971897+ seq_printf(s, "\nPL08x virtual memcpy channels:\n");18981898+ seq_printf(s, "CHANNEL:\tSTATE:\n");18991899+ seq_printf(s, "--------\t------\n");19001900+ list_for_each_entry(chan, &pl08x->memcpy.channels, chan.device_node) {19011901+ seq_printf(s, "%s\t\t\%s\n", chan->name,19021902+ pl08x_state_str(chan->state));19031903+ }19041904+19051905+ seq_printf(s, "\nPL08x virtual slave channels:\n");19061906+ seq_printf(s, "CHANNEL:\tSTATE:\n");19071907+ seq_printf(s, "--------\t------\n");19081908+ list_for_each_entry(chan, &pl08x->slave.channels, chan.device_node) {19091909+ seq_printf(s, "%s\t\t\%s\n", chan->name,19101910+ pl08x_state_str(chan->state));19111911+ }19121912+19131913+ return 0;19141914+}19151915+19161916+static int pl08x_debugfs_open(struct inode *inode, struct file *file)19171917+{19181918+ return single_open(file, pl08x_debugfs_show, inode->i_private);19191919+}19201920+19211921+static const struct file_operations pl08x_debugfs_operations = {19221922+ .open = pl08x_debugfs_open,19231923+ .read = seq_read,19241924+ .llseek = seq_lseek,19251925+ .release = single_release,19261926+};19271927+19281928+static void init_pl08x_debugfs(struct pl08x_driver_data *pl08x)19291929+{19301930+ /* Expose a simple debugfs interface to view all clocks */19311931+ (void) debugfs_create_file(dev_name(&pl08x->adev->dev), S_IFREG | S_IRUGO,19321932+ NULL, pl08x,19331933+ &pl08x_debugfs_operations);19341934+}19351935+19361936+#else19371937+static inline void init_pl08x_debugfs(struct pl08x_driver_data *pl08x)19381938+{19391939+}19401940+#endif19411941+19421942+static int pl08x_probe(struct amba_device *adev, struct amba_id *id)19431943+{19441944+ struct pl08x_driver_data *pl08x;19451945+ struct vendor_data *vd = id->data;19461946+ int ret = 0;19471947+ int i;19481948+19491949+ ret = amba_request_regions(adev, NULL);19501950+ if (ret)19511951+ return ret;19521952+19531953+ /* Create the driver state holder */19541954+ pl08x = kzalloc(sizeof(struct pl08x_driver_data), GFP_KERNEL);19551955+ if (!pl08x) {19561956+ ret = -ENOMEM;19571957+ goto out_no_pl08x;19581958+ }19591959+19601960+ /* Initialize memcpy engine */19611961+ dma_cap_set(DMA_MEMCPY, pl08x->memcpy.cap_mask);19621962+ pl08x->memcpy.dev = &adev->dev;19631963+ pl08x->memcpy.device_alloc_chan_resources = pl08x_alloc_chan_resources;19641964+ pl08x->memcpy.device_free_chan_resources = pl08x_free_chan_resources;19651965+ pl08x->memcpy.device_prep_dma_memcpy = pl08x_prep_dma_memcpy;19661966+ pl08x->memcpy.device_prep_dma_interrupt = pl08x_prep_dma_interrupt;19671967+ pl08x->memcpy.device_tx_status = pl08x_dma_tx_status;19681968+ pl08x->memcpy.device_issue_pending = pl08x_issue_pending;19691969+ pl08x->memcpy.device_control = pl08x_control;19701970+19711971+ /* Initialize slave engine */19721972+ dma_cap_set(DMA_SLAVE, pl08x->slave.cap_mask);19731973+ pl08x->slave.dev = &adev->dev;19741974+ pl08x->slave.device_alloc_chan_resources = pl08x_alloc_chan_resources;19751975+ pl08x->slave.device_free_chan_resources = pl08x_free_chan_resources;19761976+ pl08x->slave.device_prep_dma_interrupt = pl08x_prep_dma_interrupt;19771977+ pl08x->slave.device_tx_status = pl08x_dma_tx_status;19781978+ pl08x->slave.device_issue_pending = pl08x_issue_pending;19791979+ pl08x->slave.device_prep_slave_sg = pl08x_prep_slave_sg;19801980+ pl08x->slave.device_control = pl08x_control;19811981+19821982+ /* Get the platform data */19831983+ pl08x->pd = dev_get_platdata(&adev->dev);19841984+ if (!pl08x->pd) {19851985+ dev_err(&adev->dev, "no platform data supplied\n");19861986+ goto out_no_platdata;19871987+ }19881988+19891989+ /* Assign useful pointers to the driver state */19901990+ pl08x->adev = adev;19911991+ pl08x->vd = vd;19921992+19931993+ /* A DMA memory pool for LLIs, align on 1-byte boundary */19941994+ pl08x->pool = dma_pool_create(DRIVER_NAME, &pl08x->adev->dev,19951995+ PL08X_LLI_TSFR_SIZE, PL08X_ALIGN, 0);19961996+ if (!pl08x->pool) {19971997+ ret = -ENOMEM;19981998+ goto out_no_lli_pool;19991999+ }20002000+20012001+ spin_lock_init(&pl08x->lock);20022002+20032003+ pl08x->base = ioremap(adev->res.start, resource_size(&adev->res));20042004+ if (!pl08x->base) {20052005+ ret = -ENOMEM;20062006+ goto out_no_ioremap;20072007+ }20082008+20092009+ /* Turn on the PL08x */20102010+ pl08x_ensure_on(pl08x);20112011+20122012+ /*20132013+ * Attach the interrupt handler20142014+ */20152015+ writel(0x000000FF, pl08x->base + PL080_ERR_CLEAR);20162016+ writel(0x000000FF, pl08x->base + PL080_TC_CLEAR);20172017+20182018+ ret = request_irq(adev->irq[0], pl08x_irq, IRQF_DISABLED,20192019+ vd->name, pl08x);20202020+ if (ret) {20212021+ dev_err(&adev->dev, "%s failed to request interrupt %d\n",20222022+ __func__, adev->irq[0]);20232023+ goto out_no_irq;20242024+ }20252025+20262026+ /* Initialize physical channels */20272027+ pl08x->phy_chans = kmalloc((vd->channels * sizeof(struct pl08x_phy_chan)),20282028+ GFP_KERNEL);20292029+ if (!pl08x->phy_chans) {20302030+ dev_err(&adev->dev, "%s failed to allocate "20312031+ "physical channel holders\n",20322032+ __func__);20332033+ goto out_no_phychans;20342034+ }20352035+20362036+ for (i = 0; i < vd->channels; i++) {20372037+ struct pl08x_phy_chan *ch = &pl08x->phy_chans[i];20382038+20392039+ ch->id = i;20402040+ ch->base = pl08x->base + PL080_Cx_BASE(i);20412041+ spin_lock_init(&ch->lock);20422042+ ch->serving = NULL;20432043+ ch->signal = -1;20442044+ dev_info(&adev->dev,20452045+ "physical channel %d is %s\n", i,20462046+ pl08x_phy_channel_busy(ch) ? "BUSY" : "FREE");20472047+ }20482048+20492049+ /* Register as many memcpy channels as there are physical channels */20502050+ ret = pl08x_dma_init_virtual_channels(pl08x, &pl08x->memcpy,20512051+ pl08x->vd->channels, false);20522052+ if (ret <= 0) {20532053+ dev_warn(&pl08x->adev->dev,20542054+ "%s failed to enumerate memcpy channels - %d\n",20552055+ __func__, ret);20562056+ goto out_no_memcpy;20572057+ }20582058+ pl08x->memcpy.chancnt = ret;20592059+20602060+ /* Register slave channels */20612061+ ret = pl08x_dma_init_virtual_channels(pl08x, &pl08x->slave,20622062+ pl08x->pd->num_slave_channels,20632063+ true);20642064+ if (ret <= 0) {20652065+ dev_warn(&pl08x->adev->dev,20662066+ "%s failed to enumerate slave channels - %d\n",20672067+ __func__, ret);20682068+ goto out_no_slave;20692069+ }20702070+ pl08x->slave.chancnt = ret;20712071+20722072+ ret = dma_async_device_register(&pl08x->memcpy);20732073+ if (ret) {20742074+ dev_warn(&pl08x->adev->dev,20752075+ "%s failed to register memcpy as an async device - %d\n",20762076+ __func__, ret);20772077+ goto out_no_memcpy_reg;20782078+ }20792079+20802080+ ret = dma_async_device_register(&pl08x->slave);20812081+ if (ret) {20822082+ dev_warn(&pl08x->adev->dev,20832083+ "%s failed to register slave as an async device - %d\n",20842084+ __func__, ret);20852085+ goto out_no_slave_reg;20862086+ }20872087+20882088+ amba_set_drvdata(adev, pl08x);20892089+ init_pl08x_debugfs(pl08x);20902090+ dev_info(&pl08x->adev->dev, "ARM(R) %s DMA block initialized @%08x\n",20912091+ vd->name, adev->res.start);20922092+ return 0;20932093+20942094+out_no_slave_reg:20952095+ dma_async_device_unregister(&pl08x->memcpy);20962096+out_no_memcpy_reg:20972097+ pl08x_free_virtual_channels(&pl08x->slave);20982098+out_no_slave:20992099+ pl08x_free_virtual_channels(&pl08x->memcpy);21002100+out_no_memcpy:21012101+ kfree(pl08x->phy_chans);21022102+out_no_phychans:21032103+ free_irq(adev->irq[0], pl08x);21042104+out_no_irq:21052105+ iounmap(pl08x->base);21062106+out_no_ioremap:21072107+ dma_pool_destroy(pl08x->pool);21082108+out_no_lli_pool:21092109+out_no_platdata:21102110+ kfree(pl08x);21112111+out_no_pl08x:21122112+ amba_release_regions(adev);21132113+ return ret;21142114+}21152115+21162116+/* PL080 has 8 channels and the PL080 have just 2 */21172117+static struct vendor_data vendor_pl080 = {21182118+ .name = "PL080",21192119+ .channels = 8,21202120+ .dualmaster = true,21212121+};21222122+21232123+static struct vendor_data vendor_pl081 = {21242124+ .name = "PL081",21252125+ .channels = 2,21262126+ .dualmaster = false,21272127+};21282128+21292129+static struct amba_id pl08x_ids[] = {21302130+ /* PL080 */21312131+ {21322132+ .id = 0x00041080,21332133+ .mask = 0x000fffff,21342134+ .data = &vendor_pl080,21352135+ },21362136+ /* PL081 */21372137+ {21382138+ .id = 0x00041081,21392139+ .mask = 0x000fffff,21402140+ .data = &vendor_pl081,21412141+ },21422142+ /* Nomadik 8815 PL080 variant */21432143+ {21442144+ .id = 0x00280880,21452145+ .mask = 0x00ffffff,21462146+ .data = &vendor_pl080,21472147+ },21482148+ { 0, 0 },21492149+};21502150+21512151+static struct amba_driver pl08x_amba_driver = {21522152+ .drv.name = DRIVER_NAME,21532153+ .id_table = pl08x_ids,21542154+ .probe = pl08x_probe,21552155+};21562156+21572157+static int __init pl08x_init(void)21582158+{21592159+ int retval;21602160+ retval = amba_driver_register(&pl08x_amba_driver);21612161+ if (retval)21622162+ printk(KERN_WARNING DRIVER_NAME21632163+ "failed to register as an amba device (%d)\n",21642164+ retval);21652165+ return retval;21662166+}21672167+subsys_initcall(pl08x_init);
···3535#include <linux/dmapool.h>3636#include <linux/of_platform.h>37373838-#include <asm/fsldma.h>3938#include "fsldma.h"3939+4040+static const char msg_ld_oom[] = "No free memory for link descriptor\n";40414142static void dma_init(struct fsldma_chan *chan)4243{···500499501500 new = fsl_dma_alloc_descriptor(chan);502501 if (!new) {503503- dev_err(chan->dev, "No free memory for link descriptor\n");502502+ dev_err(chan->dev, msg_ld_oom);504503 return NULL;505504 }506505···537536 /* Allocate the link descriptor from DMA pool */538537 new = fsl_dma_alloc_descriptor(chan);539538 if (!new) {540540- dev_err(chan->dev,541541- "No free memory for link descriptor\n");539539+ dev_err(chan->dev, msg_ld_oom);542540 goto fail;543541 }544542#ifdef FSL_DMA_LD_DEBUG···583583 return NULL;584584}585585586586+static struct dma_async_tx_descriptor *fsl_dma_prep_sg(struct dma_chan *dchan,587587+ struct scatterlist *dst_sg, unsigned int dst_nents,588588+ struct scatterlist *src_sg, unsigned int src_nents,589589+ unsigned long flags)590590+{591591+ struct fsl_desc_sw *first = NULL, *prev = NULL, *new = NULL;592592+ struct fsldma_chan *chan = to_fsl_chan(dchan);593593+ size_t dst_avail, src_avail;594594+ dma_addr_t dst, src;595595+ size_t len;596596+597597+ /* basic sanity checks */598598+ if (dst_nents == 0 || src_nents == 0)599599+ return NULL;600600+601601+ if (dst_sg == NULL || src_sg == NULL)602602+ return NULL;603603+604604+ /*605605+ * TODO: should we check that both scatterlists have the same606606+ * TODO: number of bytes in total? Is that really an error?607607+ */608608+609609+ /* get prepared for the loop */610610+ dst_avail = sg_dma_len(dst_sg);611611+ src_avail = sg_dma_len(src_sg);612612+613613+ /* run until we are out of scatterlist entries */614614+ while (true) {615615+616616+ /* create the largest transaction possible */617617+ len = min_t(size_t, src_avail, dst_avail);618618+ len = min_t(size_t, len, FSL_DMA_BCR_MAX_CNT);619619+ if (len == 0)620620+ goto fetch;621621+622622+ dst = sg_dma_address(dst_sg) + sg_dma_len(dst_sg) - dst_avail;623623+ src = sg_dma_address(src_sg) + sg_dma_len(src_sg) - src_avail;624624+625625+ /* allocate and populate the descriptor */626626+ new = fsl_dma_alloc_descriptor(chan);627627+ if (!new) {628628+ dev_err(chan->dev, msg_ld_oom);629629+ goto fail;630630+ }631631+#ifdef FSL_DMA_LD_DEBUG632632+ dev_dbg(chan->dev, "new link desc alloc %p\n", new);633633+#endif634634+635635+ set_desc_cnt(chan, &new->hw, len);636636+ set_desc_src(chan, &new->hw, src);637637+ set_desc_dst(chan, &new->hw, dst);638638+639639+ if (!first)640640+ first = new;641641+ else642642+ set_desc_next(chan, &prev->hw, new->async_tx.phys);643643+644644+ new->async_tx.cookie = 0;645645+ async_tx_ack(&new->async_tx);646646+ prev = new;647647+648648+ /* Insert the link descriptor to the LD ring */649649+ list_add_tail(&new->node, &first->tx_list);650650+651651+ /* update metadata */652652+ dst_avail -= len;653653+ src_avail -= len;654654+655655+fetch:656656+ /* fetch the next dst scatterlist entry */657657+ if (dst_avail == 0) {658658+659659+ /* no more entries: we're done */660660+ if (dst_nents == 0)661661+ break;662662+663663+ /* fetch the next entry: if there are no more: done */664664+ dst_sg = sg_next(dst_sg);665665+ if (dst_sg == NULL)666666+ break;667667+668668+ dst_nents--;669669+ dst_avail = sg_dma_len(dst_sg);670670+ }671671+672672+ /* fetch the next src scatterlist entry */673673+ if (src_avail == 0) {674674+675675+ /* no more entries: we're done */676676+ if (src_nents == 0)677677+ break;678678+679679+ /* fetch the next entry: if there are no more: done */680680+ src_sg = sg_next(src_sg);681681+ if (src_sg == NULL)682682+ break;683683+684684+ src_nents--;685685+ src_avail = sg_dma_len(src_sg);686686+ }687687+ }688688+689689+ new->async_tx.flags = flags; /* client is in control of this ack */690690+ new->async_tx.cookie = -EBUSY;691691+692692+ /* Set End-of-link to the last link descriptor of new list */693693+ set_ld_eol(chan, new);694694+695695+ return &first->async_tx;696696+697697+fail:698698+ if (!first)699699+ return NULL;700700+701701+ fsldma_free_desc_list_reverse(chan, &first->tx_list);702702+ return NULL;703703+}704704+586705/**587706 * fsl_dma_prep_slave_sg - prepare descriptors for a DMA_SLAVE transaction588707 * @chan: DMA channel···718599 struct dma_chan *dchan, struct scatterlist *sgl, unsigned int sg_len,719600 enum dma_data_direction direction, unsigned long flags)720601{721721- struct fsldma_chan *chan;722722- struct fsl_desc_sw *first = NULL, *prev = NULL, *new = NULL;723723- struct fsl_dma_slave *slave;724724- size_t copy;725725-726726- int i;727727- struct scatterlist *sg;728728- size_t sg_used;729729- size_t hw_used;730730- struct fsl_dma_hw_addr *hw;731731- dma_addr_t dma_dst, dma_src;732732-733733- if (!dchan)734734- return NULL;735735-736736- if (!dchan->private)737737- return NULL;738738-739739- chan = to_fsl_chan(dchan);740740- slave = dchan->private;741741-742742- if (list_empty(&slave->addresses))743743- return NULL;744744-745745- hw = list_first_entry(&slave->addresses, struct fsl_dma_hw_addr, entry);746746- hw_used = 0;747747-748602 /*749749- * Build the hardware transaction to copy from the scatterlist to750750- * the hardware, or from the hardware to the scatterlist603603+ * This operation is not supported on the Freescale DMA controller751604 *752752- * If you are copying from the hardware to the scatterlist and it753753- * takes two hardware entries to fill an entire page, then both754754- * hardware entries will be coalesced into the same page755755- *756756- * If you are copying from the scatterlist to the hardware and a757757- * single page can fill two hardware entries, then the data will758758- * be read out of the page into the first hardware entry, and so on605605+ * However, we need to provide the function pointer to allow the606606+ * device_control() method to work.759607 */760760- for_each_sg(sgl, sg, sg_len, i) {761761- sg_used = 0;762762-763763- /* Loop until the entire scatterlist entry is used */764764- while (sg_used < sg_dma_len(sg)) {765765-766766- /*767767- * If we've used up the current hardware address/length768768- * pair, we need to load a new one769769- *770770- * This is done in a while loop so that descriptors with771771- * length == 0 will be skipped772772- */773773- while (hw_used >= hw->length) {774774-775775- /*776776- * If the current hardware entry is the last777777- * entry in the list, we're finished778778- */779779- if (list_is_last(&hw->entry, &slave->addresses))780780- goto finished;781781-782782- /* Get the next hardware address/length pair */783783- hw = list_entry(hw->entry.next,784784- struct fsl_dma_hw_addr, entry);785785- hw_used = 0;786786- }787787-788788- /* Allocate the link descriptor from DMA pool */789789- new = fsl_dma_alloc_descriptor(chan);790790- if (!new) {791791- dev_err(chan->dev, "No free memory for "792792- "link descriptor\n");793793- goto fail;794794- }795795-#ifdef FSL_DMA_LD_DEBUG796796- dev_dbg(chan->dev, "new link desc alloc %p\n", new);797797-#endif798798-799799- /*800800- * Calculate the maximum number of bytes to transfer,801801- * making sure it is less than the DMA controller limit802802- */803803- copy = min_t(size_t, sg_dma_len(sg) - sg_used,804804- hw->length - hw_used);805805- copy = min_t(size_t, copy, FSL_DMA_BCR_MAX_CNT);806806-807807- /*808808- * DMA_FROM_DEVICE809809- * from the hardware to the scatterlist810810- *811811- * DMA_TO_DEVICE812812- * from the scatterlist to the hardware813813- */814814- if (direction == DMA_FROM_DEVICE) {815815- dma_src = hw->address + hw_used;816816- dma_dst = sg_dma_address(sg) + sg_used;817817- } else {818818- dma_src = sg_dma_address(sg) + sg_used;819819- dma_dst = hw->address + hw_used;820820- }821821-822822- /* Fill in the descriptor */823823- set_desc_cnt(chan, &new->hw, copy);824824- set_desc_src(chan, &new->hw, dma_src);825825- set_desc_dst(chan, &new->hw, dma_dst);826826-827827- /*828828- * If this is not the first descriptor, chain the829829- * current descriptor after the previous descriptor830830- */831831- if (!first) {832832- first = new;833833- } else {834834- set_desc_next(chan, &prev->hw,835835- new->async_tx.phys);836836- }837837-838838- new->async_tx.cookie = 0;839839- async_tx_ack(&new->async_tx);840840-841841- prev = new;842842- sg_used += copy;843843- hw_used += copy;844844-845845- /* Insert the link descriptor into the LD ring */846846- list_add_tail(&new->node, &first->tx_list);847847- }848848- }849849-850850-finished:851851-852852- /* All of the hardware address/length pairs had length == 0 */853853- if (!first || !new)854854- return NULL;855855-856856- new->async_tx.flags = flags;857857- new->async_tx.cookie = -EBUSY;858858-859859- /* Set End-of-link to the last link descriptor of new list */860860- set_ld_eol(chan, new);861861-862862- /* Enable extra controller features */863863- if (chan->set_src_loop_size)864864- chan->set_src_loop_size(chan, slave->src_loop_size);865865-866866- if (chan->set_dst_loop_size)867867- chan->set_dst_loop_size(chan, slave->dst_loop_size);868868-869869- if (chan->toggle_ext_start)870870- chan->toggle_ext_start(chan, slave->external_start);871871-872872- if (chan->toggle_ext_pause)873873- chan->toggle_ext_pause(chan, slave->external_pause);874874-875875- if (chan->set_request_count)876876- chan->set_request_count(chan, slave->request_count);877877-878878- return &first->async_tx;879879-880880-fail:881881- /* If first was not set, then we failed to allocate the very first882882- * descriptor, and we're done */883883- if (!first)884884- return NULL;885885-886886- /*887887- * First is set, so all of the descriptors we allocated have been added888888- * to first->tx_list, INCLUDING "first" itself. Therefore we889889- * must traverse the list backwards freeing each descriptor in turn890890- *891891- * We're re-using variables for the loop, oh well892892- */893893- fsldma_free_desc_list_reverse(chan, &first->tx_list);894608 return NULL;895609}896610897611static int fsl_dma_device_control(struct dma_chan *dchan,898612 enum dma_ctrl_cmd cmd, unsigned long arg)899613{614614+ struct dma_slave_config *config;900615 struct fsldma_chan *chan;901616 unsigned long flags;902902-903903- /* Only supports DMA_TERMINATE_ALL */904904- if (cmd != DMA_TERMINATE_ALL)905905- return -ENXIO;617617+ int size;906618907619 if (!dchan)908620 return -EINVAL;909621910622 chan = to_fsl_chan(dchan);911623912912- /* Halt the DMA engine */913913- dma_halt(chan);624624+ switch (cmd) {625625+ case DMA_TERMINATE_ALL:626626+ /* Halt the DMA engine */627627+ dma_halt(chan);914628915915- spin_lock_irqsave(&chan->desc_lock, flags);629629+ spin_lock_irqsave(&chan->desc_lock, flags);916630917917- /* Remove and free all of the descriptors in the LD queue */918918- fsldma_free_desc_list(chan, &chan->ld_pending);919919- fsldma_free_desc_list(chan, &chan->ld_running);631631+ /* Remove and free all of the descriptors in the LD queue */632632+ fsldma_free_desc_list(chan, &chan->ld_pending);633633+ fsldma_free_desc_list(chan, &chan->ld_running);920634921921- spin_unlock_irqrestore(&chan->desc_lock, flags);635635+ spin_unlock_irqrestore(&chan->desc_lock, flags);636636+ return 0;637637+638638+ case DMA_SLAVE_CONFIG:639639+ config = (struct dma_slave_config *)arg;640640+641641+ /* make sure the channel supports setting burst size */642642+ if (!chan->set_request_count)643643+ return -ENXIO;644644+645645+ /* we set the controller burst size depending on direction */646646+ if (config->direction == DMA_TO_DEVICE)647647+ size = config->dst_addr_width * config->dst_maxburst;648648+ else649649+ size = config->src_addr_width * config->src_maxburst;650650+651651+ chan->set_request_count(chan, size);652652+ return 0;653653+654654+ case FSLDMA_EXTERNAL_START:655655+656656+ /* make sure the channel supports external start */657657+ if (!chan->toggle_ext_start)658658+ return -ENXIO;659659+660660+ chan->toggle_ext_start(chan, arg);661661+ return 0;662662+663663+ default:664664+ return -ENXIO;665665+ }922666923667 return 0;924668}···1309132713101328 dma_cap_set(DMA_MEMCPY, fdev->common.cap_mask);13111329 dma_cap_set(DMA_INTERRUPT, fdev->common.cap_mask);13301330+ dma_cap_set(DMA_SG, fdev->common.cap_mask);13121331 dma_cap_set(DMA_SLAVE, fdev->common.cap_mask);13131332 fdev->common.device_alloc_chan_resources = fsl_dma_alloc_chan_resources;13141333 fdev->common.device_free_chan_resources = fsl_dma_free_chan_resources;13151334 fdev->common.device_prep_dma_interrupt = fsl_dma_prep_interrupt;13161335 fdev->common.device_prep_dma_memcpy = fsl_dma_prep_memcpy;13361336+ fdev->common.device_prep_dma_sg = fsl_dma_prep_sg;13171337 fdev->common.device_tx_status = fsl_tx_status;13181338 fdev->common.device_issue_pending = fsl_dma_memcpy_issue_pending;13191339 fdev->common.device_prep_slave_sg = fsl_dma_prep_slave_sg;
+422
drivers/dma/imx-dma.c
···11+/*22+ * drivers/dma/imx-dma.c33+ *44+ * This file contains a driver for the Freescale i.MX DMA engine55+ * found on i.MX1/21/2766+ *77+ * Copyright 2010 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>88+ *99+ * The code contained herein is licensed under the GNU General Public1010+ * License. You may obtain a copy of the GNU General Public License1111+ * Version 2 or later at the following locations:1212+ *1313+ * http://www.opensource.org/licenses/gpl-license.html1414+ * http://www.gnu.org/copyleft/gpl.html1515+ */1616+#include <linux/init.h>1717+#include <linux/types.h>1818+#include <linux/mm.h>1919+#include <linux/interrupt.h>2020+#include <linux/spinlock.h>2121+#include <linux/device.h>2222+#include <linux/dma-mapping.h>2323+#include <linux/slab.h>2424+#include <linux/platform_device.h>2525+#include <linux/dmaengine.h>2626+2727+#include <asm/irq.h>2828+#include <mach/dma-v1.h>2929+#include <mach/hardware.h>3030+3131+struct imxdma_channel {3232+ struct imxdma_engine *imxdma;3333+ unsigned int channel;3434+ unsigned int imxdma_channel;3535+3636+ enum dma_slave_buswidth word_size;3737+ dma_addr_t per_address;3838+ u32 watermark_level;3939+ struct dma_chan chan;4040+ spinlock_t lock;4141+ struct dma_async_tx_descriptor desc;4242+ dma_cookie_t last_completed;4343+ enum dma_status status;4444+ int dma_request;4545+ struct scatterlist *sg_list;4646+};4747+4848+#define MAX_DMA_CHANNELS 84949+5050+struct imxdma_engine {5151+ struct device *dev;5252+ struct dma_device dma_device;5353+ struct imxdma_channel channel[MAX_DMA_CHANNELS];5454+};5555+5656+static struct imxdma_channel *to_imxdma_chan(struct dma_chan *chan)5757+{5858+ return container_of(chan, struct imxdma_channel, chan);5959+}6060+6161+static void imxdma_handle(struct imxdma_channel *imxdmac)6262+{6363+ if (imxdmac->desc.callback)6464+ imxdmac->desc.callback(imxdmac->desc.callback_param);6565+ imxdmac->last_completed = imxdmac->desc.cookie;6666+}6767+6868+static void imxdma_irq_handler(int channel, void *data)6969+{7070+ struct imxdma_channel *imxdmac = data;7171+7272+ imxdmac->status = DMA_SUCCESS;7373+ imxdma_handle(imxdmac);7474+}7575+7676+static void imxdma_err_handler(int channel, void *data, int error)7777+{7878+ struct imxdma_channel *imxdmac = data;7979+8080+ imxdmac->status = DMA_ERROR;8181+ imxdma_handle(imxdmac);8282+}8383+8484+static void imxdma_progression(int channel, void *data,8585+ struct scatterlist *sg)8686+{8787+ struct imxdma_channel *imxdmac = data;8888+8989+ imxdmac->status = DMA_SUCCESS;9090+ imxdma_handle(imxdmac);9191+}9292+9393+static int imxdma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,9494+ unsigned long arg)9595+{9696+ struct imxdma_channel *imxdmac = to_imxdma_chan(chan);9797+ struct dma_slave_config *dmaengine_cfg = (void *)arg;9898+ int ret;9999+ unsigned int mode = 0;100100+101101+ switch (cmd) {102102+ case DMA_TERMINATE_ALL:103103+ imxdmac->status = DMA_ERROR;104104+ imx_dma_disable(imxdmac->imxdma_channel);105105+ return 0;106106+ case DMA_SLAVE_CONFIG:107107+ if (dmaengine_cfg->direction == DMA_FROM_DEVICE) {108108+ imxdmac->per_address = dmaengine_cfg->src_addr;109109+ imxdmac->watermark_level = dmaengine_cfg->src_maxburst;110110+ imxdmac->word_size = dmaengine_cfg->src_addr_width;111111+ } else {112112+ imxdmac->per_address = dmaengine_cfg->dst_addr;113113+ imxdmac->watermark_level = dmaengine_cfg->dst_maxburst;114114+ imxdmac->word_size = dmaengine_cfg->dst_addr_width;115115+ }116116+117117+ switch (imxdmac->word_size) {118118+ case DMA_SLAVE_BUSWIDTH_1_BYTE:119119+ mode = IMX_DMA_MEMSIZE_8;120120+ break;121121+ case DMA_SLAVE_BUSWIDTH_2_BYTES:122122+ mode = IMX_DMA_MEMSIZE_16;123123+ break;124124+ default:125125+ case DMA_SLAVE_BUSWIDTH_4_BYTES:126126+ mode = IMX_DMA_MEMSIZE_32;127127+ break;128128+ }129129+ ret = imx_dma_config_channel(imxdmac->imxdma_channel,130130+ mode | IMX_DMA_TYPE_FIFO,131131+ IMX_DMA_MEMSIZE_32 | IMX_DMA_TYPE_LINEAR,132132+ imxdmac->dma_request, 1);133133+134134+ if (ret)135135+ return ret;136136+137137+ imx_dma_config_burstlen(imxdmac->imxdma_channel, imxdmac->watermark_level);138138+139139+ return 0;140140+ default:141141+ return -ENOSYS;142142+ }143143+144144+ return -EINVAL;145145+}146146+147147+static enum dma_status imxdma_tx_status(struct dma_chan *chan,148148+ dma_cookie_t cookie,149149+ struct dma_tx_state *txstate)150150+{151151+ struct imxdma_channel *imxdmac = to_imxdma_chan(chan);152152+ dma_cookie_t last_used;153153+ enum dma_status ret;154154+155155+ last_used = chan->cookie;156156+157157+ ret = dma_async_is_complete(cookie, imxdmac->last_completed, last_used);158158+ dma_set_tx_state(txstate, imxdmac->last_completed, last_used, 0);159159+160160+ return ret;161161+}162162+163163+static dma_cookie_t imxdma_assign_cookie(struct imxdma_channel *imxdma)164164+{165165+ dma_cookie_t cookie = imxdma->chan.cookie;166166+167167+ if (++cookie < 0)168168+ cookie = 1;169169+170170+ imxdma->chan.cookie = cookie;171171+ imxdma->desc.cookie = cookie;172172+173173+ return cookie;174174+}175175+176176+static dma_cookie_t imxdma_tx_submit(struct dma_async_tx_descriptor *tx)177177+{178178+ struct imxdma_channel *imxdmac = to_imxdma_chan(tx->chan);179179+ dma_cookie_t cookie;180180+181181+ spin_lock_irq(&imxdmac->lock);182182+183183+ cookie = imxdma_assign_cookie(imxdmac);184184+185185+ imx_dma_enable(imxdmac->imxdma_channel);186186+187187+ spin_unlock_irq(&imxdmac->lock);188188+189189+ return cookie;190190+}191191+192192+static int imxdma_alloc_chan_resources(struct dma_chan *chan)193193+{194194+ struct imxdma_channel *imxdmac = to_imxdma_chan(chan);195195+ struct imx_dma_data *data = chan->private;196196+197197+ imxdmac->dma_request = data->dma_request;198198+199199+ dma_async_tx_descriptor_init(&imxdmac->desc, chan);200200+ imxdmac->desc.tx_submit = imxdma_tx_submit;201201+ /* txd.flags will be overwritten in prep funcs */202202+ imxdmac->desc.flags = DMA_CTRL_ACK;203203+204204+ imxdmac->status = DMA_SUCCESS;205205+206206+ return 0;207207+}208208+209209+static void imxdma_free_chan_resources(struct dma_chan *chan)210210+{211211+ struct imxdma_channel *imxdmac = to_imxdma_chan(chan);212212+213213+ imx_dma_disable(imxdmac->imxdma_channel);214214+215215+ if (imxdmac->sg_list) {216216+ kfree(imxdmac->sg_list);217217+ imxdmac->sg_list = NULL;218218+ }219219+}220220+221221+static struct dma_async_tx_descriptor *imxdma_prep_slave_sg(222222+ struct dma_chan *chan, struct scatterlist *sgl,223223+ unsigned int sg_len, enum dma_data_direction direction,224224+ unsigned long flags)225225+{226226+ struct imxdma_channel *imxdmac = to_imxdma_chan(chan);227227+ struct scatterlist *sg;228228+ int i, ret, dma_length = 0;229229+ unsigned int dmamode;230230+231231+ if (imxdmac->status == DMA_IN_PROGRESS)232232+ return NULL;233233+234234+ imxdmac->status = DMA_IN_PROGRESS;235235+236236+ for_each_sg(sgl, sg, sg_len, i) {237237+ dma_length += sg->length;238238+ }239239+240240+ if (direction == DMA_FROM_DEVICE)241241+ dmamode = DMA_MODE_READ;242242+ else243243+ dmamode = DMA_MODE_WRITE;244244+245245+ ret = imx_dma_setup_sg(imxdmac->imxdma_channel, sgl, sg_len,246246+ dma_length, imxdmac->per_address, dmamode);247247+ if (ret)248248+ return NULL;249249+250250+ return &imxdmac->desc;251251+}252252+253253+static struct dma_async_tx_descriptor *imxdma_prep_dma_cyclic(254254+ struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len,255255+ size_t period_len, enum dma_data_direction direction)256256+{257257+ struct imxdma_channel *imxdmac = to_imxdma_chan(chan);258258+ struct imxdma_engine *imxdma = imxdmac->imxdma;259259+ int i, ret;260260+ unsigned int periods = buf_len / period_len;261261+ unsigned int dmamode;262262+263263+ dev_dbg(imxdma->dev, "%s channel: %d buf_len=%d period_len=%d\n",264264+ __func__, imxdmac->channel, buf_len, period_len);265265+266266+ if (imxdmac->status == DMA_IN_PROGRESS)267267+ return NULL;268268+ imxdmac->status = DMA_IN_PROGRESS;269269+270270+ ret = imx_dma_setup_progression_handler(imxdmac->imxdma_channel,271271+ imxdma_progression);272272+ if (ret) {273273+ dev_err(imxdma->dev, "Failed to setup the DMA handler\n");274274+ return NULL;275275+ }276276+277277+ if (imxdmac->sg_list)278278+ kfree(imxdmac->sg_list);279279+280280+ imxdmac->sg_list = kcalloc(periods + 1,281281+ sizeof(struct scatterlist), GFP_KERNEL);282282+ if (!imxdmac->sg_list)283283+ return NULL;284284+285285+ sg_init_table(imxdmac->sg_list, periods);286286+287287+ for (i = 0; i < periods; i++) {288288+ imxdmac->sg_list[i].page_link = 0;289289+ imxdmac->sg_list[i].offset = 0;290290+ imxdmac->sg_list[i].dma_address = dma_addr;291291+ imxdmac->sg_list[i].length = period_len;292292+ dma_addr += period_len;293293+ }294294+295295+ /* close the loop */296296+ imxdmac->sg_list[periods].offset = 0;297297+ imxdmac->sg_list[periods].length = 0;298298+ imxdmac->sg_list[periods].page_link =299299+ ((unsigned long)imxdmac->sg_list | 0x01) & ~0x02;300300+301301+ if (direction == DMA_FROM_DEVICE)302302+ dmamode = DMA_MODE_READ;303303+ else304304+ dmamode = DMA_MODE_WRITE;305305+306306+ ret = imx_dma_setup_sg(imxdmac->imxdma_channel, imxdmac->sg_list, periods,307307+ IMX_DMA_LENGTH_LOOP, imxdmac->per_address, dmamode);308308+ if (ret)309309+ return NULL;310310+311311+ return &imxdmac->desc;312312+}313313+314314+static void imxdma_issue_pending(struct dma_chan *chan)315315+{316316+ /*317317+ * Nothing to do. We only have a single descriptor318318+ */319319+}320320+321321+static int __init imxdma_probe(struct platform_device *pdev)322322+{323323+ struct imxdma_engine *imxdma;324324+ int ret, i;325325+326326+ imxdma = kzalloc(sizeof(*imxdma), GFP_KERNEL);327327+ if (!imxdma)328328+ return -ENOMEM;329329+330330+ INIT_LIST_HEAD(&imxdma->dma_device.channels);331331+332332+ /* Initialize channel parameters */333333+ for (i = 0; i < MAX_DMA_CHANNELS; i++) {334334+ struct imxdma_channel *imxdmac = &imxdma->channel[i];335335+336336+ imxdmac->imxdma_channel = imx_dma_request_by_prio("dmaengine",337337+ DMA_PRIO_MEDIUM);338338+ if (imxdmac->channel < 0)339339+ goto err_init;340340+341341+ imx_dma_setup_handlers(imxdmac->imxdma_channel,342342+ imxdma_irq_handler, imxdma_err_handler, imxdmac);343343+344344+ imxdmac->imxdma = imxdma;345345+ spin_lock_init(&imxdmac->lock);346346+347347+ dma_cap_set(DMA_SLAVE, imxdma->dma_device.cap_mask);348348+ dma_cap_set(DMA_CYCLIC, imxdma->dma_device.cap_mask);349349+350350+ imxdmac->chan.device = &imxdma->dma_device;351351+ imxdmac->chan.chan_id = i;352352+ imxdmac->channel = i;353353+354354+ /* Add the channel to the DMAC list */355355+ list_add_tail(&imxdmac->chan.device_node, &imxdma->dma_device.channels);356356+ }357357+358358+ imxdma->dev = &pdev->dev;359359+ imxdma->dma_device.dev = &pdev->dev;360360+361361+ imxdma->dma_device.device_alloc_chan_resources = imxdma_alloc_chan_resources;362362+ imxdma->dma_device.device_free_chan_resources = imxdma_free_chan_resources;363363+ imxdma->dma_device.device_tx_status = imxdma_tx_status;364364+ imxdma->dma_device.device_prep_slave_sg = imxdma_prep_slave_sg;365365+ imxdma->dma_device.device_prep_dma_cyclic = imxdma_prep_dma_cyclic;366366+ imxdma->dma_device.device_control = imxdma_control;367367+ imxdma->dma_device.device_issue_pending = imxdma_issue_pending;368368+369369+ platform_set_drvdata(pdev, imxdma);370370+371371+ ret = dma_async_device_register(&imxdma->dma_device);372372+ if (ret) {373373+ dev_err(&pdev->dev, "unable to register\n");374374+ goto err_init;375375+ }376376+377377+ return 0;378378+379379+err_init:380380+ while (i-- >= 0) {381381+ struct imxdma_channel *imxdmac = &imxdma->channel[i];382382+ imx_dma_free(imxdmac->imxdma_channel);383383+ }384384+385385+ kfree(imxdma);386386+ return ret;387387+}388388+389389+static int __exit imxdma_remove(struct platform_device *pdev)390390+{391391+ struct imxdma_engine *imxdma = platform_get_drvdata(pdev);392392+ int i;393393+394394+ dma_async_device_unregister(&imxdma->dma_device);395395+396396+ for (i = 0; i < MAX_DMA_CHANNELS; i++) {397397+ struct imxdma_channel *imxdmac = &imxdma->channel[i];398398+399399+ imx_dma_free(imxdmac->imxdma_channel);400400+ }401401+402402+ kfree(imxdma);403403+404404+ return 0;405405+}406406+407407+static struct platform_driver imxdma_driver = {408408+ .driver = {409409+ .name = "imx-dma",410410+ },411411+ .remove = __exit_p(imxdma_remove),412412+};413413+414414+static int __init imxdma_module_init(void)415415+{416416+ return platform_driver_probe(&imxdma_driver, imxdma_probe);417417+}418418+subsys_initcall(imxdma_module_init);419419+420420+MODULE_AUTHOR("Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>");421421+MODULE_DESCRIPTION("i.MX dma driver");422422+MODULE_LICENSE("GPL");
+1392
drivers/dma/imx-sdma.c
···11+/*22+ * drivers/dma/imx-sdma.c33+ *44+ * This file contains a driver for the Freescale Smart DMA engine55+ *66+ * Copyright 2010 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>77+ *88+ * Based on code from Freescale:99+ *1010+ * Copyright 2004-2009 Freescale Semiconductor, Inc. All Rights Reserved.1111+ *1212+ * The code contained herein is licensed under the GNU General Public1313+ * License. You may obtain a copy of the GNU General Public License1414+ * Version 2 or later at the following locations:1515+ *1616+ * http://www.opensource.org/licenses/gpl-license.html1717+ * http://www.gnu.org/copyleft/gpl.html1818+ */1919+2020+#include <linux/init.h>2121+#include <linux/types.h>2222+#include <linux/mm.h>2323+#include <linux/interrupt.h>2424+#include <linux/clk.h>2525+#include <linux/wait.h>2626+#include <linux/sched.h>2727+#include <linux/semaphore.h>2828+#include <linux/spinlock.h>2929+#include <linux/device.h>3030+#include <linux/dma-mapping.h>3131+#include <linux/firmware.h>3232+#include <linux/slab.h>3333+#include <linux/platform_device.h>3434+#include <linux/dmaengine.h>3535+3636+#include <asm/irq.h>3737+#include <mach/sdma.h>3838+#include <mach/dma.h>3939+#include <mach/hardware.h>4040+4141+/* SDMA registers */4242+#define SDMA_H_C0PTR 0x0004343+#define SDMA_H_INTR 0x0044444+#define SDMA_H_STATSTOP 0x0084545+#define SDMA_H_START 0x00c4646+#define SDMA_H_EVTOVR 0x0104747+#define SDMA_H_DSPOVR 0x0144848+#define SDMA_H_HOSTOVR 0x0184949+#define SDMA_H_EVTPEND 0x01c5050+#define SDMA_H_DSPENBL 0x0205151+#define SDMA_H_RESET 0x0245252+#define SDMA_H_EVTERR 0x0285353+#define SDMA_H_INTRMSK 0x02c5454+#define SDMA_H_PSW 0x0305555+#define SDMA_H_EVTERRDBG 0x0345656+#define SDMA_H_CONFIG 0x0385757+#define SDMA_ONCE_ENB 0x0405858+#define SDMA_ONCE_DATA 0x0445959+#define SDMA_ONCE_INSTR 0x0486060+#define SDMA_ONCE_STAT 0x04c6161+#define SDMA_ONCE_CMD 0x0506262+#define SDMA_EVT_MIRROR 0x0546363+#define SDMA_ILLINSTADDR 0x0586464+#define SDMA_CHN0ADDR 0x05c6565+#define SDMA_ONCE_RTB 0x0606666+#define SDMA_XTRIG_CONF1 0x0706767+#define SDMA_XTRIG_CONF2 0x0746868+#define SDMA_CHNENBL0_V2 0x2006969+#define SDMA_CHNENBL0_V1 0x0807070+#define SDMA_CHNPRI_0 0x1007171+7272+/*7373+ * Buffer descriptor status values.7474+ */7575+#define BD_DONE 0x017676+#define BD_WRAP 0x027777+#define BD_CONT 0x047878+#define BD_INTR 0x087979+#define BD_RROR 0x108080+#define BD_LAST 0x208181+#define BD_EXTD 0x808282+8383+/*8484+ * Data Node descriptor status values.8585+ */8686+#define DND_END_OF_FRAME 0x808787+#define DND_END_OF_XFER 0x408888+#define DND_DONE 0x208989+#define DND_UNUSED 0x019090+9191+/*9292+ * IPCV2 descriptor status values.9393+ */9494+#define BD_IPCV2_END_OF_FRAME 0x409595+9696+#define IPCV2_MAX_NODES 509797+/*9898+ * Error bit set in the CCB status field by the SDMA,9999+ * in setbd routine, in case of a transfer error100100+ */101101+#define DATA_ERROR 0x10000000102102+103103+/*104104+ * Buffer descriptor commands.105105+ */106106+#define C0_ADDR 0x01107107+#define C0_LOAD 0x02108108+#define C0_DUMP 0x03109109+#define C0_SETCTX 0x07110110+#define C0_GETCTX 0x03111111+#define C0_SETDM 0x01112112+#define C0_SETPM 0x04113113+#define C0_GETDM 0x02114114+#define C0_GETPM 0x08115115+/*116116+ * Change endianness indicator in the BD command field117117+ */118118+#define CHANGE_ENDIANNESS 0x80119119+120120+/*121121+ * Mode/Count of data node descriptors - IPCv2122122+ */123123+struct sdma_mode_count {124124+ u32 count : 16; /* size of the buffer pointed by this BD */125125+ u32 status : 8; /* E,R,I,C,W,D status bits stored here */126126+ u32 command : 8; /* command mostlky used for channel 0 */127127+};128128+129129+/*130130+ * Buffer descriptor131131+ */132132+struct sdma_buffer_descriptor {133133+ struct sdma_mode_count mode;134134+ u32 buffer_addr; /* address of the buffer described */135135+ u32 ext_buffer_addr; /* extended buffer address */136136+} __attribute__ ((packed));137137+138138+/**139139+ * struct sdma_channel_control - Channel control Block140140+ *141141+ * @current_bd_ptr current buffer descriptor processed142142+ * @base_bd_ptr first element of buffer descriptor array143143+ * @unused padding. The SDMA engine expects an array of 128 byte144144+ * control blocks145145+ */146146+struct sdma_channel_control {147147+ u32 current_bd_ptr;148148+ u32 base_bd_ptr;149149+ u32 unused[2];150150+} __attribute__ ((packed));151151+152152+/**153153+ * struct sdma_state_registers - SDMA context for a channel154154+ *155155+ * @pc: program counter156156+ * @t: test bit: status of arithmetic & test instruction157157+ * @rpc: return program counter158158+ * @sf: source fault while loading data159159+ * @spc: loop start program counter160160+ * @df: destination fault while storing data161161+ * @epc: loop end program counter162162+ * @lm: loop mode163163+ */164164+struct sdma_state_registers {165165+ u32 pc :14;166166+ u32 unused1: 1;167167+ u32 t : 1;168168+ u32 rpc :14;169169+ u32 unused0: 1;170170+ u32 sf : 1;171171+ u32 spc :14;172172+ u32 unused2: 1;173173+ u32 df : 1;174174+ u32 epc :14;175175+ u32 lm : 2;176176+} __attribute__ ((packed));177177+178178+/**179179+ * struct sdma_context_data - sdma context specific to a channel180180+ *181181+ * @channel_state: channel state bits182182+ * @gReg: general registers183183+ * @mda: burst dma destination address register184184+ * @msa: burst dma source address register185185+ * @ms: burst dma status register186186+ * @md: burst dma data register187187+ * @pda: peripheral dma destination address register188188+ * @psa: peripheral dma source address register189189+ * @ps: peripheral dma status register190190+ * @pd: peripheral dma data register191191+ * @ca: CRC polynomial register192192+ * @cs: CRC accumulator register193193+ * @dda: dedicated core destination address register194194+ * @dsa: dedicated core source address register195195+ * @ds: dedicated core status register196196+ * @dd: dedicated core data register197197+ */198198+struct sdma_context_data {199199+ struct sdma_state_registers channel_state;200200+ u32 gReg[8];201201+ u32 mda;202202+ u32 msa;203203+ u32 ms;204204+ u32 md;205205+ u32 pda;206206+ u32 psa;207207+ u32 ps;208208+ u32 pd;209209+ u32 ca;210210+ u32 cs;211211+ u32 dda;212212+ u32 dsa;213213+ u32 ds;214214+ u32 dd;215215+ u32 scratch0;216216+ u32 scratch1;217217+ u32 scratch2;218218+ u32 scratch3;219219+ u32 scratch4;220220+ u32 scratch5;221221+ u32 scratch6;222222+ u32 scratch7;223223+} __attribute__ ((packed));224224+225225+#define NUM_BD (int)(PAGE_SIZE / sizeof(struct sdma_buffer_descriptor))226226+227227+struct sdma_engine;228228+229229+/**230230+ * struct sdma_channel - housekeeping for a SDMA channel231231+ *232232+ * @sdma pointer to the SDMA engine for this channel233233+ * @channel the channel number, matches dmaengine chan_id234234+ * @direction transfer type. Needed for setting SDMA script235235+ * @peripheral_type Peripheral type. Needed for setting SDMA script236236+ * @event_id0 aka dma request line237237+ * @event_id1 for channels that use 2 events238238+ * @word_size peripheral access size239239+ * @buf_tail ID of the buffer that was processed240240+ * @done channel completion241241+ * @num_bd max NUM_BD. number of descriptors currently handling242242+ */243243+struct sdma_channel {244244+ struct sdma_engine *sdma;245245+ unsigned int channel;246246+ enum dma_data_direction direction;247247+ enum sdma_peripheral_type peripheral_type;248248+ unsigned int event_id0;249249+ unsigned int event_id1;250250+ enum dma_slave_buswidth word_size;251251+ unsigned int buf_tail;252252+ struct completion done;253253+ unsigned int num_bd;254254+ struct sdma_buffer_descriptor *bd;255255+ dma_addr_t bd_phys;256256+ unsigned int pc_from_device, pc_to_device;257257+ unsigned long flags;258258+ dma_addr_t per_address;259259+ u32 event_mask0, event_mask1;260260+ u32 watermark_level;261261+ u32 shp_addr, per_addr;262262+ struct dma_chan chan;263263+ spinlock_t lock;264264+ struct dma_async_tx_descriptor desc;265265+ dma_cookie_t last_completed;266266+ enum dma_status status;267267+};268268+269269+#define IMX_DMA_SG_LOOP (1 << 0)270270+271271+#define MAX_DMA_CHANNELS 32272272+#define MXC_SDMA_DEFAULT_PRIORITY 1273273+#define MXC_SDMA_MIN_PRIORITY 1274274+#define MXC_SDMA_MAX_PRIORITY 7275275+276276+/**277277+ * struct sdma_script_start_addrs - SDMA script start pointers278278+ *279279+ * start addresses of the different functions in the physical280280+ * address space of the SDMA engine.281281+ */282282+struct sdma_script_start_addrs {283283+ u32 ap_2_ap_addr;284284+ u32 ap_2_bp_addr;285285+ u32 ap_2_ap_fixed_addr;286286+ u32 bp_2_ap_addr;287287+ u32 loopback_on_dsp_side_addr;288288+ u32 mcu_interrupt_only_addr;289289+ u32 firi_2_per_addr;290290+ u32 firi_2_mcu_addr;291291+ u32 per_2_firi_addr;292292+ u32 mcu_2_firi_addr;293293+ u32 uart_2_per_addr;294294+ u32 uart_2_mcu_addr;295295+ u32 per_2_app_addr;296296+ u32 mcu_2_app_addr;297297+ u32 per_2_per_addr;298298+ u32 uartsh_2_per_addr;299299+ u32 uartsh_2_mcu_addr;300300+ u32 per_2_shp_addr;301301+ u32 mcu_2_shp_addr;302302+ u32 ata_2_mcu_addr;303303+ u32 mcu_2_ata_addr;304304+ u32 app_2_per_addr;305305+ u32 app_2_mcu_addr;306306+ u32 shp_2_per_addr;307307+ u32 shp_2_mcu_addr;308308+ u32 mshc_2_mcu_addr;309309+ u32 mcu_2_mshc_addr;310310+ u32 spdif_2_mcu_addr;311311+ u32 mcu_2_spdif_addr;312312+ u32 asrc_2_mcu_addr;313313+ u32 ext_mem_2_ipu_addr;314314+ u32 descrambler_addr;315315+ u32 dptc_dvfs_addr;316316+ u32 utra_addr;317317+ u32 ram_code_start_addr;318318+};319319+320320+#define SDMA_FIRMWARE_MAGIC 0x414d4453321321+322322+/**323323+ * struct sdma_firmware_header - Layout of the firmware image324324+ *325325+ * @magic "SDMA"326326+ * @version_major increased whenever layout of struct sdma_script_start_addrs327327+ * changes.328328+ * @version_minor firmware minor version (for binary compatible changes)329329+ * @script_addrs_start offset of struct sdma_script_start_addrs in this image330330+ * @num_script_addrs Number of script addresses in this image331331+ * @ram_code_start offset of SDMA ram image in this firmware image332332+ * @ram_code_size size of SDMA ram image333333+ * @script_addrs Stores the start address of the SDMA scripts334334+ * (in SDMA memory space)335335+ */336336+struct sdma_firmware_header {337337+ u32 magic;338338+ u32 version_major;339339+ u32 version_minor;340340+ u32 script_addrs_start;341341+ u32 num_script_addrs;342342+ u32 ram_code_start;343343+ u32 ram_code_size;344344+};345345+346346+struct sdma_engine {347347+ struct device *dev;348348+ struct sdma_channel channel[MAX_DMA_CHANNELS];349349+ struct sdma_channel_control *channel_control;350350+ void __iomem *regs;351351+ unsigned int version;352352+ unsigned int num_events;353353+ struct sdma_context_data *context;354354+ dma_addr_t context_phys;355355+ struct dma_device dma_device;356356+ struct clk *clk;357357+ struct sdma_script_start_addrs *script_addrs;358358+};359359+360360+#define SDMA_H_CONFIG_DSPDMA (1 << 12) /* indicates if the DSPDMA is used */361361+#define SDMA_H_CONFIG_RTD_PINS (1 << 11) /* indicates if Real-Time Debug pins are enabled */362362+#define SDMA_H_CONFIG_ACR (1 << 4) /* indicates if AHB freq /core freq = 2 or 1 */363363+#define SDMA_H_CONFIG_CSM (3) /* indicates which context switch mode is selected*/364364+365365+static inline u32 chnenbl_ofs(struct sdma_engine *sdma, unsigned int event)366366+{367367+ u32 chnenbl0 = (sdma->version == 2 ? SDMA_CHNENBL0_V2 : SDMA_CHNENBL0_V1);368368+369369+ return chnenbl0 + event * 4;370370+}371371+372372+static int sdma_config_ownership(struct sdma_channel *sdmac,373373+ bool event_override, bool mcu_override, bool dsp_override)374374+{375375+ struct sdma_engine *sdma = sdmac->sdma;376376+ int channel = sdmac->channel;377377+ u32 evt, mcu, dsp;378378+379379+ if (event_override && mcu_override && dsp_override)380380+ return -EINVAL;381381+382382+ evt = __raw_readl(sdma->regs + SDMA_H_EVTOVR);383383+ mcu = __raw_readl(sdma->regs + SDMA_H_HOSTOVR);384384+ dsp = __raw_readl(sdma->regs + SDMA_H_DSPOVR);385385+386386+ if (dsp_override)387387+ dsp &= ~(1 << channel);388388+ else389389+ dsp |= (1 << channel);390390+391391+ if (event_override)392392+ evt &= ~(1 << channel);393393+ else394394+ evt |= (1 << channel);395395+396396+ if (mcu_override)397397+ mcu &= ~(1 << channel);398398+ else399399+ mcu |= (1 << channel);400400+401401+ __raw_writel(evt, sdma->regs + SDMA_H_EVTOVR);402402+ __raw_writel(mcu, sdma->regs + SDMA_H_HOSTOVR);403403+ __raw_writel(dsp, sdma->regs + SDMA_H_DSPOVR);404404+405405+ return 0;406406+}407407+408408+/*409409+ * sdma_run_channel - run a channel and wait till it's done410410+ */411411+static int sdma_run_channel(struct sdma_channel *sdmac)412412+{413413+ struct sdma_engine *sdma = sdmac->sdma;414414+ int channel = sdmac->channel;415415+ int ret;416416+417417+ init_completion(&sdmac->done);418418+419419+ __raw_writel(1 << channel, sdma->regs + SDMA_H_START);420420+421421+ ret = wait_for_completion_timeout(&sdmac->done, HZ);422422+423423+ return ret ? 0 : -ETIMEDOUT;424424+}425425+426426+static int sdma_load_script(struct sdma_engine *sdma, void *buf, int size,427427+ u32 address)428428+{429429+ struct sdma_buffer_descriptor *bd0 = sdma->channel[0].bd;430430+ void *buf_virt;431431+ dma_addr_t buf_phys;432432+ int ret;433433+434434+ buf_virt = dma_alloc_coherent(NULL,435435+ size,436436+ &buf_phys, GFP_KERNEL);437437+ if (!buf_virt)438438+ return -ENOMEM;439439+440440+ bd0->mode.command = C0_SETPM;441441+ bd0->mode.status = BD_DONE | BD_INTR | BD_WRAP | BD_EXTD;442442+ bd0->mode.count = size / 2;443443+ bd0->buffer_addr = buf_phys;444444+ bd0->ext_buffer_addr = address;445445+446446+ memcpy(buf_virt, buf, size);447447+448448+ ret = sdma_run_channel(&sdma->channel[0]);449449+450450+ dma_free_coherent(NULL, size, buf_virt, buf_phys);451451+452452+ return ret;453453+}454454+455455+static void sdma_event_enable(struct sdma_channel *sdmac, unsigned int event)456456+{457457+ struct sdma_engine *sdma = sdmac->sdma;458458+ int channel = sdmac->channel;459459+ u32 val;460460+ u32 chnenbl = chnenbl_ofs(sdma, event);461461+462462+ val = __raw_readl(sdma->regs + chnenbl);463463+ val |= (1 << channel);464464+ __raw_writel(val, sdma->regs + chnenbl);465465+}466466+467467+static void sdma_event_disable(struct sdma_channel *sdmac, unsigned int event)468468+{469469+ struct sdma_engine *sdma = sdmac->sdma;470470+ int channel = sdmac->channel;471471+ u32 chnenbl = chnenbl_ofs(sdma, event);472472+ u32 val;473473+474474+ val = __raw_readl(sdma->regs + chnenbl);475475+ val &= ~(1 << channel);476476+ __raw_writel(val, sdma->regs + chnenbl);477477+}478478+479479+static void sdma_handle_channel_loop(struct sdma_channel *sdmac)480480+{481481+ struct sdma_buffer_descriptor *bd;482482+483483+ /*484484+ * loop mode. Iterate over descriptors, re-setup them and485485+ * call callback function.486486+ */487487+ while (1) {488488+ bd = &sdmac->bd[sdmac->buf_tail];489489+490490+ if (bd->mode.status & BD_DONE)491491+ break;492492+493493+ if (bd->mode.status & BD_RROR)494494+ sdmac->status = DMA_ERROR;495495+ else496496+ sdmac->status = DMA_SUCCESS;497497+498498+ bd->mode.status |= BD_DONE;499499+ sdmac->buf_tail++;500500+ sdmac->buf_tail %= sdmac->num_bd;501501+502502+ if (sdmac->desc.callback)503503+ sdmac->desc.callback(sdmac->desc.callback_param);504504+ }505505+}506506+507507+static void mxc_sdma_handle_channel_normal(struct sdma_channel *sdmac)508508+{509509+ struct sdma_buffer_descriptor *bd;510510+ int i, error = 0;511511+512512+ /*513513+ * non loop mode. Iterate over all descriptors, collect514514+ * errors and call callback function515515+ */516516+ for (i = 0; i < sdmac->num_bd; i++) {517517+ bd = &sdmac->bd[i];518518+519519+ if (bd->mode.status & (BD_DONE | BD_RROR))520520+ error = -EIO;521521+ }522522+523523+ if (error)524524+ sdmac->status = DMA_ERROR;525525+ else526526+ sdmac->status = DMA_SUCCESS;527527+528528+ if (sdmac->desc.callback)529529+ sdmac->desc.callback(sdmac->desc.callback_param);530530+ sdmac->last_completed = sdmac->desc.cookie;531531+}532532+533533+static void mxc_sdma_handle_channel(struct sdma_channel *sdmac)534534+{535535+ complete(&sdmac->done);536536+537537+ /* not interested in channel 0 interrupts */538538+ if (sdmac->channel == 0)539539+ return;540540+541541+ if (sdmac->flags & IMX_DMA_SG_LOOP)542542+ sdma_handle_channel_loop(sdmac);543543+ else544544+ mxc_sdma_handle_channel_normal(sdmac);545545+}546546+547547+static irqreturn_t sdma_int_handler(int irq, void *dev_id)548548+{549549+ struct sdma_engine *sdma = dev_id;550550+ u32 stat;551551+552552+ stat = __raw_readl(sdma->regs + SDMA_H_INTR);553553+ __raw_writel(stat, sdma->regs + SDMA_H_INTR);554554+555555+ while (stat) {556556+ int channel = fls(stat) - 1;557557+ struct sdma_channel *sdmac = &sdma->channel[channel];558558+559559+ mxc_sdma_handle_channel(sdmac);560560+561561+ stat &= ~(1 << channel);562562+ }563563+564564+ return IRQ_HANDLED;565565+}566566+567567+/*568568+ * sets the pc of SDMA script according to the peripheral type569569+ */570570+static void sdma_get_pc(struct sdma_channel *sdmac,571571+ enum sdma_peripheral_type peripheral_type)572572+{573573+ struct sdma_engine *sdma = sdmac->sdma;574574+ int per_2_emi = 0, emi_2_per = 0;575575+ /*576576+ * These are needed once we start to support transfers between577577+ * two peripherals or memory-to-memory transfers578578+ */579579+ int per_2_per = 0, emi_2_emi = 0;580580+581581+ sdmac->pc_from_device = 0;582582+ sdmac->pc_to_device = 0;583583+584584+ switch (peripheral_type) {585585+ case IMX_DMATYPE_MEMORY:586586+ emi_2_emi = sdma->script_addrs->ap_2_ap_addr;587587+ break;588588+ case IMX_DMATYPE_DSP:589589+ emi_2_per = sdma->script_addrs->bp_2_ap_addr;590590+ per_2_emi = sdma->script_addrs->ap_2_bp_addr;591591+ break;592592+ case IMX_DMATYPE_FIRI:593593+ per_2_emi = sdma->script_addrs->firi_2_mcu_addr;594594+ emi_2_per = sdma->script_addrs->mcu_2_firi_addr;595595+ break;596596+ case IMX_DMATYPE_UART:597597+ per_2_emi = sdma->script_addrs->uart_2_mcu_addr;598598+ emi_2_per = sdma->script_addrs->mcu_2_app_addr;599599+ break;600600+ case IMX_DMATYPE_UART_SP:601601+ per_2_emi = sdma->script_addrs->uartsh_2_mcu_addr;602602+ emi_2_per = sdma->script_addrs->mcu_2_shp_addr;603603+ break;604604+ case IMX_DMATYPE_ATA:605605+ per_2_emi = sdma->script_addrs->ata_2_mcu_addr;606606+ emi_2_per = sdma->script_addrs->mcu_2_ata_addr;607607+ break;608608+ case IMX_DMATYPE_CSPI:609609+ case IMX_DMATYPE_EXT:610610+ case IMX_DMATYPE_SSI:611611+ per_2_emi = sdma->script_addrs->app_2_mcu_addr;612612+ emi_2_per = sdma->script_addrs->mcu_2_app_addr;613613+ break;614614+ case IMX_DMATYPE_SSI_SP:615615+ case IMX_DMATYPE_MMC:616616+ case IMX_DMATYPE_SDHC:617617+ case IMX_DMATYPE_CSPI_SP:618618+ case IMX_DMATYPE_ESAI:619619+ case IMX_DMATYPE_MSHC_SP:620620+ per_2_emi = sdma->script_addrs->shp_2_mcu_addr;621621+ emi_2_per = sdma->script_addrs->mcu_2_shp_addr;622622+ break;623623+ case IMX_DMATYPE_ASRC:624624+ per_2_emi = sdma->script_addrs->asrc_2_mcu_addr;625625+ emi_2_per = sdma->script_addrs->asrc_2_mcu_addr;626626+ per_2_per = sdma->script_addrs->per_2_per_addr;627627+ break;628628+ case IMX_DMATYPE_MSHC:629629+ per_2_emi = sdma->script_addrs->mshc_2_mcu_addr;630630+ emi_2_per = sdma->script_addrs->mcu_2_mshc_addr;631631+ break;632632+ case IMX_DMATYPE_CCM:633633+ per_2_emi = sdma->script_addrs->dptc_dvfs_addr;634634+ break;635635+ case IMX_DMATYPE_SPDIF:636636+ per_2_emi = sdma->script_addrs->spdif_2_mcu_addr;637637+ emi_2_per = sdma->script_addrs->mcu_2_spdif_addr;638638+ break;639639+ case IMX_DMATYPE_IPU_MEMORY:640640+ emi_2_per = sdma->script_addrs->ext_mem_2_ipu_addr;641641+ break;642642+ default:643643+ break;644644+ }645645+646646+ sdmac->pc_from_device = per_2_emi;647647+ sdmac->pc_to_device = emi_2_per;648648+}649649+650650+static int sdma_load_context(struct sdma_channel *sdmac)651651+{652652+ struct sdma_engine *sdma = sdmac->sdma;653653+ int channel = sdmac->channel;654654+ int load_address;655655+ struct sdma_context_data *context = sdma->context;656656+ struct sdma_buffer_descriptor *bd0 = sdma->channel[0].bd;657657+ int ret;658658+659659+ if (sdmac->direction == DMA_FROM_DEVICE) {660660+ load_address = sdmac->pc_from_device;661661+ } else {662662+ load_address = sdmac->pc_to_device;663663+ }664664+665665+ if (load_address < 0)666666+ return load_address;667667+668668+ dev_dbg(sdma->dev, "load_address = %d\n", load_address);669669+ dev_dbg(sdma->dev, "wml = 0x%08x\n", sdmac->watermark_level);670670+ dev_dbg(sdma->dev, "shp_addr = 0x%08x\n", sdmac->shp_addr);671671+ dev_dbg(sdma->dev, "per_addr = 0x%08x\n", sdmac->per_addr);672672+ dev_dbg(sdma->dev, "event_mask0 = 0x%08x\n", sdmac->event_mask0);673673+ dev_dbg(sdma->dev, "event_mask1 = 0x%08x\n", sdmac->event_mask1);674674+675675+ memset(context, 0, sizeof(*context));676676+ context->channel_state.pc = load_address;677677+678678+ /* Send by context the event mask,base address for peripheral679679+ * and watermark level680680+ */681681+ context->gReg[0] = sdmac->event_mask1;682682+ context->gReg[1] = sdmac->event_mask0;683683+ context->gReg[2] = sdmac->per_addr;684684+ context->gReg[6] = sdmac->shp_addr;685685+ context->gReg[7] = sdmac->watermark_level;686686+687687+ bd0->mode.command = C0_SETDM;688688+ bd0->mode.status = BD_DONE | BD_INTR | BD_WRAP | BD_EXTD;689689+ bd0->mode.count = sizeof(*context) / 4;690690+ bd0->buffer_addr = sdma->context_phys;691691+ bd0->ext_buffer_addr = 2048 + (sizeof(*context) / 4) * channel;692692+693693+ ret = sdma_run_channel(&sdma->channel[0]);694694+695695+ return ret;696696+}697697+698698+static void sdma_disable_channel(struct sdma_channel *sdmac)699699+{700700+ struct sdma_engine *sdma = sdmac->sdma;701701+ int channel = sdmac->channel;702702+703703+ __raw_writel(1 << channel, sdma->regs + SDMA_H_STATSTOP);704704+ sdmac->status = DMA_ERROR;705705+}706706+707707+static int sdma_config_channel(struct sdma_channel *sdmac)708708+{709709+ int ret;710710+711711+ sdma_disable_channel(sdmac);712712+713713+ sdmac->event_mask0 = 0;714714+ sdmac->event_mask1 = 0;715715+ sdmac->shp_addr = 0;716716+ sdmac->per_addr = 0;717717+718718+ if (sdmac->event_id0) {719719+ if (sdmac->event_id0 > 32)720720+ return -EINVAL;721721+ sdma_event_enable(sdmac, sdmac->event_id0);722722+ }723723+724724+ switch (sdmac->peripheral_type) {725725+ case IMX_DMATYPE_DSP:726726+ sdma_config_ownership(sdmac, false, true, true);727727+ break;728728+ case IMX_DMATYPE_MEMORY:729729+ sdma_config_ownership(sdmac, false, true, false);730730+ break;731731+ default:732732+ sdma_config_ownership(sdmac, true, true, false);733733+ break;734734+ }735735+736736+ sdma_get_pc(sdmac, sdmac->peripheral_type);737737+738738+ if ((sdmac->peripheral_type != IMX_DMATYPE_MEMORY) &&739739+ (sdmac->peripheral_type != IMX_DMATYPE_DSP)) {740740+ /* Handle multiple event channels differently */741741+ if (sdmac->event_id1) {742742+ sdmac->event_mask1 = 1 << (sdmac->event_id1 % 32);743743+ if (sdmac->event_id1 > 31)744744+ sdmac->watermark_level |= 1 << 31;745745+ sdmac->event_mask0 = 1 << (sdmac->event_id0 % 32);746746+ if (sdmac->event_id0 > 31)747747+ sdmac->watermark_level |= 1 << 30;748748+ } else {749749+ sdmac->event_mask0 = 1 << sdmac->event_id0;750750+ sdmac->event_mask1 = 1 << (sdmac->event_id0 - 32);751751+ }752752+ /* Watermark Level */753753+ sdmac->watermark_level |= sdmac->watermark_level;754754+ /* Address */755755+ sdmac->shp_addr = sdmac->per_address;756756+ } else {757757+ sdmac->watermark_level = 0; /* FIXME: M3_BASE_ADDRESS */758758+ }759759+760760+ ret = sdma_load_context(sdmac);761761+762762+ return ret;763763+}764764+765765+static int sdma_set_channel_priority(struct sdma_channel *sdmac,766766+ unsigned int priority)767767+{768768+ struct sdma_engine *sdma = sdmac->sdma;769769+ int channel = sdmac->channel;770770+771771+ if (priority < MXC_SDMA_MIN_PRIORITY772772+ || priority > MXC_SDMA_MAX_PRIORITY) {773773+ return -EINVAL;774774+ }775775+776776+ __raw_writel(priority, sdma->regs + SDMA_CHNPRI_0 + 4 * channel);777777+778778+ return 0;779779+}780780+781781+static int sdma_request_channel(struct sdma_channel *sdmac)782782+{783783+ struct sdma_engine *sdma = sdmac->sdma;784784+ int channel = sdmac->channel;785785+ int ret = -EBUSY;786786+787787+ sdmac->bd = dma_alloc_coherent(NULL, PAGE_SIZE, &sdmac->bd_phys, GFP_KERNEL);788788+ if (!sdmac->bd) {789789+ ret = -ENOMEM;790790+ goto out;791791+ }792792+793793+ memset(sdmac->bd, 0, PAGE_SIZE);794794+795795+ sdma->channel_control[channel].base_bd_ptr = sdmac->bd_phys;796796+ sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys;797797+798798+ clk_enable(sdma->clk);799799+800800+ sdma_set_channel_priority(sdmac, MXC_SDMA_DEFAULT_PRIORITY);801801+802802+ init_completion(&sdmac->done);803803+804804+ sdmac->buf_tail = 0;805805+806806+ return 0;807807+out:808808+809809+ return ret;810810+}811811+812812+static void sdma_enable_channel(struct sdma_engine *sdma, int channel)813813+{814814+ __raw_writel(1 << channel, sdma->regs + SDMA_H_START);815815+}816816+817817+static dma_cookie_t sdma_assign_cookie(struct sdma_channel *sdma)818818+{819819+ dma_cookie_t cookie = sdma->chan.cookie;820820+821821+ if (++cookie < 0)822822+ cookie = 1;823823+824824+ sdma->chan.cookie = cookie;825825+ sdma->desc.cookie = cookie;826826+827827+ return cookie;828828+}829829+830830+static struct sdma_channel *to_sdma_chan(struct dma_chan *chan)831831+{832832+ return container_of(chan, struct sdma_channel, chan);833833+}834834+835835+static dma_cookie_t sdma_tx_submit(struct dma_async_tx_descriptor *tx)836836+{837837+ struct sdma_channel *sdmac = to_sdma_chan(tx->chan);838838+ struct sdma_engine *sdma = sdmac->sdma;839839+ dma_cookie_t cookie;840840+841841+ spin_lock_irq(&sdmac->lock);842842+843843+ cookie = sdma_assign_cookie(sdmac);844844+845845+ sdma_enable_channel(sdma, tx->chan->chan_id);846846+847847+ spin_unlock_irq(&sdmac->lock);848848+849849+ return cookie;850850+}851851+852852+static int sdma_alloc_chan_resources(struct dma_chan *chan)853853+{854854+ struct sdma_channel *sdmac = to_sdma_chan(chan);855855+ struct imx_dma_data *data = chan->private;856856+ int prio, ret;857857+858858+ /* No need to execute this for internal channel 0 */859859+ if (chan->chan_id == 0)860860+ return 0;861861+862862+ if (!data)863863+ return -EINVAL;864864+865865+ switch (data->priority) {866866+ case DMA_PRIO_HIGH:867867+ prio = 3;868868+ break;869869+ case DMA_PRIO_MEDIUM:870870+ prio = 2;871871+ break;872872+ case DMA_PRIO_LOW:873873+ default:874874+ prio = 1;875875+ break;876876+ }877877+878878+ sdmac->peripheral_type = data->peripheral_type;879879+ sdmac->event_id0 = data->dma_request;880880+ ret = sdma_set_channel_priority(sdmac, prio);881881+ if (ret)882882+ return ret;883883+884884+ ret = sdma_request_channel(sdmac);885885+ if (ret)886886+ return ret;887887+888888+ dma_async_tx_descriptor_init(&sdmac->desc, chan);889889+ sdmac->desc.tx_submit = sdma_tx_submit;890890+ /* txd.flags will be overwritten in prep funcs */891891+ sdmac->desc.flags = DMA_CTRL_ACK;892892+893893+ return 0;894894+}895895+896896+static void sdma_free_chan_resources(struct dma_chan *chan)897897+{898898+ struct sdma_channel *sdmac = to_sdma_chan(chan);899899+ struct sdma_engine *sdma = sdmac->sdma;900900+901901+ sdma_disable_channel(sdmac);902902+903903+ if (sdmac->event_id0)904904+ sdma_event_disable(sdmac, sdmac->event_id0);905905+ if (sdmac->event_id1)906906+ sdma_event_disable(sdmac, sdmac->event_id1);907907+908908+ sdmac->event_id0 = 0;909909+ sdmac->event_id1 = 0;910910+911911+ sdma_set_channel_priority(sdmac, 0);912912+913913+ dma_free_coherent(NULL, PAGE_SIZE, sdmac->bd, sdmac->bd_phys);914914+915915+ clk_disable(sdma->clk);916916+}917917+918918+static struct dma_async_tx_descriptor *sdma_prep_slave_sg(919919+ struct dma_chan *chan, struct scatterlist *sgl,920920+ unsigned int sg_len, enum dma_data_direction direction,921921+ unsigned long flags)922922+{923923+ struct sdma_channel *sdmac = to_sdma_chan(chan);924924+ struct sdma_engine *sdma = sdmac->sdma;925925+ int ret, i, count;926926+ int channel = chan->chan_id;927927+ struct scatterlist *sg;928928+929929+ if (sdmac->status == DMA_IN_PROGRESS)930930+ return NULL;931931+ sdmac->status = DMA_IN_PROGRESS;932932+933933+ sdmac->flags = 0;934934+935935+ dev_dbg(sdma->dev, "setting up %d entries for channel %d.\n",936936+ sg_len, channel);937937+938938+ sdmac->direction = direction;939939+ ret = sdma_load_context(sdmac);940940+ if (ret)941941+ goto err_out;942942+943943+ if (sg_len > NUM_BD) {944944+ dev_err(sdma->dev, "SDMA channel %d: maximum number of sg exceeded: %d > %d\n",945945+ channel, sg_len, NUM_BD);946946+ ret = -EINVAL;947947+ goto err_out;948948+ }949949+950950+ for_each_sg(sgl, sg, sg_len, i) {951951+ struct sdma_buffer_descriptor *bd = &sdmac->bd[i];952952+ int param;953953+954954+ bd->buffer_addr = sgl->dma_address;955955+956956+ count = sg->length;957957+958958+ if (count > 0xffff) {959959+ dev_err(sdma->dev, "SDMA channel %d: maximum bytes for sg entry exceeded: %d > %d\n",960960+ channel, count, 0xffff);961961+ ret = -EINVAL;962962+ goto err_out;963963+ }964964+965965+ bd->mode.count = count;966966+967967+ if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES) {968968+ ret = -EINVAL;969969+ goto err_out;970970+ }971971+ if (sdmac->word_size == DMA_SLAVE_BUSWIDTH_4_BYTES)972972+ bd->mode.command = 0;973973+ else974974+ bd->mode.command = sdmac->word_size;975975+976976+ param = BD_DONE | BD_EXTD | BD_CONT;977977+978978+ if (sdmac->flags & IMX_DMA_SG_LOOP) {979979+ param |= BD_INTR;980980+ if (i + 1 == sg_len)981981+ param |= BD_WRAP;982982+ }983983+984984+ if (i + 1 == sg_len)985985+ param |= BD_INTR;986986+987987+ dev_dbg(sdma->dev, "entry %d: count: %d dma: 0x%08x %s%s\n",988988+ i, count, sg->dma_address,989989+ param & BD_WRAP ? "wrap" : "",990990+ param & BD_INTR ? " intr" : "");991991+992992+ bd->mode.status = param;993993+ }994994+995995+ sdmac->num_bd = sg_len;996996+ sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys;997997+998998+ return &sdmac->desc;999999+err_out:10001000+ return NULL;10011001+}10021002+10031003+static struct dma_async_tx_descriptor *sdma_prep_dma_cyclic(10041004+ struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len,10051005+ size_t period_len, enum dma_data_direction direction)10061006+{10071007+ struct sdma_channel *sdmac = to_sdma_chan(chan);10081008+ struct sdma_engine *sdma = sdmac->sdma;10091009+ int num_periods = buf_len / period_len;10101010+ int channel = chan->chan_id;10111011+ int ret, i = 0, buf = 0;10121012+10131013+ dev_dbg(sdma->dev, "%s channel: %d\n", __func__, channel);10141014+10151015+ if (sdmac->status == DMA_IN_PROGRESS)10161016+ return NULL;10171017+10181018+ sdmac->status = DMA_IN_PROGRESS;10191019+10201020+ sdmac->flags |= IMX_DMA_SG_LOOP;10211021+ sdmac->direction = direction;10221022+ ret = sdma_load_context(sdmac);10231023+ if (ret)10241024+ goto err_out;10251025+10261026+ if (num_periods > NUM_BD) {10271027+ dev_err(sdma->dev, "SDMA channel %d: maximum number of sg exceeded: %d > %d\n",10281028+ channel, num_periods, NUM_BD);10291029+ goto err_out;10301030+ }10311031+10321032+ if (period_len > 0xffff) {10331033+ dev_err(sdma->dev, "SDMA channel %d: maximum period size exceeded: %d > %d\n",10341034+ channel, period_len, 0xffff);10351035+ goto err_out;10361036+ }10371037+10381038+ while (buf < buf_len) {10391039+ struct sdma_buffer_descriptor *bd = &sdmac->bd[i];10401040+ int param;10411041+10421042+ bd->buffer_addr = dma_addr;10431043+10441044+ bd->mode.count = period_len;10451045+10461046+ if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES)10471047+ goto err_out;10481048+ if (sdmac->word_size == DMA_SLAVE_BUSWIDTH_4_BYTES)10491049+ bd->mode.command = 0;10501050+ else10511051+ bd->mode.command = sdmac->word_size;10521052+10531053+ param = BD_DONE | BD_EXTD | BD_CONT | BD_INTR;10541054+ if (i + 1 == num_periods)10551055+ param |= BD_WRAP;10561056+10571057+ dev_dbg(sdma->dev, "entry %d: count: %d dma: 0x%08x %s%s\n",10581058+ i, period_len, dma_addr,10591059+ param & BD_WRAP ? "wrap" : "",10601060+ param & BD_INTR ? " intr" : "");10611061+10621062+ bd->mode.status = param;10631063+10641064+ dma_addr += period_len;10651065+ buf += period_len;10661066+10671067+ i++;10681068+ }10691069+10701070+ sdmac->num_bd = num_periods;10711071+ sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys;10721072+10731073+ return &sdmac->desc;10741074+err_out:10751075+ sdmac->status = DMA_ERROR;10761076+ return NULL;10771077+}10781078+10791079+static int sdma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,10801080+ unsigned long arg)10811081+{10821082+ struct sdma_channel *sdmac = to_sdma_chan(chan);10831083+ struct dma_slave_config *dmaengine_cfg = (void *)arg;10841084+10851085+ switch (cmd) {10861086+ case DMA_TERMINATE_ALL:10871087+ sdma_disable_channel(sdmac);10881088+ return 0;10891089+ case DMA_SLAVE_CONFIG:10901090+ if (dmaengine_cfg->direction == DMA_FROM_DEVICE) {10911091+ sdmac->per_address = dmaengine_cfg->src_addr;10921092+ sdmac->watermark_level = dmaengine_cfg->src_maxburst;10931093+ sdmac->word_size = dmaengine_cfg->src_addr_width;10941094+ } else {10951095+ sdmac->per_address = dmaengine_cfg->dst_addr;10961096+ sdmac->watermark_level = dmaengine_cfg->dst_maxburst;10971097+ sdmac->word_size = dmaengine_cfg->dst_addr_width;10981098+ }10991099+ return sdma_config_channel(sdmac);11001100+ default:11011101+ return -ENOSYS;11021102+ }11031103+11041104+ return -EINVAL;11051105+}11061106+11071107+static enum dma_status sdma_tx_status(struct dma_chan *chan,11081108+ dma_cookie_t cookie,11091109+ struct dma_tx_state *txstate)11101110+{11111111+ struct sdma_channel *sdmac = to_sdma_chan(chan);11121112+ dma_cookie_t last_used;11131113+ enum dma_status ret;11141114+11151115+ last_used = chan->cookie;11161116+11171117+ ret = dma_async_is_complete(cookie, sdmac->last_completed, last_used);11181118+ dma_set_tx_state(txstate, sdmac->last_completed, last_used, 0);11191119+11201120+ return ret;11211121+}11221122+11231123+static void sdma_issue_pending(struct dma_chan *chan)11241124+{11251125+ /*11261126+ * Nothing to do. We only have a single descriptor11271127+ */11281128+}11291129+11301130+static int __init sdma_init(struct sdma_engine *sdma,11311131+ void *ram_code, int ram_code_size)11321132+{11331133+ int i, ret;11341134+ dma_addr_t ccb_phys;11351135+11361136+ switch (sdma->version) {11371137+ case 1:11381138+ sdma->num_events = 32;11391139+ break;11401140+ case 2:11411141+ sdma->num_events = 48;11421142+ break;11431143+ default:11441144+ dev_err(sdma->dev, "Unknown version %d. aborting\n", sdma->version);11451145+ return -ENODEV;11461146+ }11471147+11481148+ clk_enable(sdma->clk);11491149+11501150+ /* Be sure SDMA has not started yet */11511151+ __raw_writel(0, sdma->regs + SDMA_H_C0PTR);11521152+11531153+ sdma->channel_control = dma_alloc_coherent(NULL,11541154+ MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control) +11551155+ sizeof(struct sdma_context_data),11561156+ &ccb_phys, GFP_KERNEL);11571157+11581158+ if (!sdma->channel_control) {11591159+ ret = -ENOMEM;11601160+ goto err_dma_alloc;11611161+ }11621162+11631163+ sdma->context = (void *)sdma->channel_control +11641164+ MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control);11651165+ sdma->context_phys = ccb_phys +11661166+ MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control);11671167+11681168+ /* Zero-out the CCB structures array just allocated */11691169+ memset(sdma->channel_control, 0,11701170+ MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control));11711171+11721172+ /* disable all channels */11731173+ for (i = 0; i < sdma->num_events; i++)11741174+ __raw_writel(0, sdma->regs + chnenbl_ofs(sdma, i));11751175+11761176+ /* All channels have priority 0 */11771177+ for (i = 0; i < MAX_DMA_CHANNELS; i++)11781178+ __raw_writel(0, sdma->regs + SDMA_CHNPRI_0 + i * 4);11791179+11801180+ ret = sdma_request_channel(&sdma->channel[0]);11811181+ if (ret)11821182+ goto err_dma_alloc;11831183+11841184+ sdma_config_ownership(&sdma->channel[0], false, true, false);11851185+11861186+ /* Set Command Channel (Channel Zero) */11871187+ __raw_writel(0x4050, sdma->regs + SDMA_CHN0ADDR);11881188+11891189+ /* Set bits of CONFIG register but with static context switching */11901190+ /* FIXME: Check whether to set ACR bit depending on clock ratios */11911191+ __raw_writel(0, sdma->regs + SDMA_H_CONFIG);11921192+11931193+ __raw_writel(ccb_phys, sdma->regs + SDMA_H_C0PTR);11941194+11951195+ /* download the RAM image for SDMA */11961196+ sdma_load_script(sdma, ram_code,11971197+ ram_code_size,11981198+ sdma->script_addrs->ram_code_start_addr);11991199+12001200+ /* Set bits of CONFIG register with given context switching mode */12011201+ __raw_writel(SDMA_H_CONFIG_CSM, sdma->regs + SDMA_H_CONFIG);12021202+12031203+ /* Initializes channel's priorities */12041204+ sdma_set_channel_priority(&sdma->channel[0], 7);12051205+12061206+ clk_disable(sdma->clk);12071207+12081208+ return 0;12091209+12101210+err_dma_alloc:12111211+ clk_disable(sdma->clk);12121212+ dev_err(sdma->dev, "initialisation failed with %d\n", ret);12131213+ return ret;12141214+}12151215+12161216+static int __init sdma_probe(struct platform_device *pdev)12171217+{12181218+ int ret;12191219+ const struct firmware *fw;12201220+ const struct sdma_firmware_header *header;12211221+ const struct sdma_script_start_addrs *addr;12221222+ int irq;12231223+ unsigned short *ram_code;12241224+ struct resource *iores;12251225+ struct sdma_platform_data *pdata = pdev->dev.platform_data;12261226+ char *fwname;12271227+ int i;12281228+ dma_cap_mask_t mask;12291229+ struct sdma_engine *sdma;12301230+12311231+ sdma = kzalloc(sizeof(*sdma), GFP_KERNEL);12321232+ if (!sdma)12331233+ return -ENOMEM;12341234+12351235+ sdma->dev = &pdev->dev;12361236+12371237+ iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);12381238+ irq = platform_get_irq(pdev, 0);12391239+ if (!iores || irq < 0 || !pdata) {12401240+ ret = -EINVAL;12411241+ goto err_irq;12421242+ }12431243+12441244+ if (!request_mem_region(iores->start, resource_size(iores), pdev->name)) {12451245+ ret = -EBUSY;12461246+ goto err_request_region;12471247+ }12481248+12491249+ sdma->clk = clk_get(&pdev->dev, NULL);12501250+ if (IS_ERR(sdma->clk)) {12511251+ ret = PTR_ERR(sdma->clk);12521252+ goto err_clk;12531253+ }12541254+12551255+ sdma->regs = ioremap(iores->start, resource_size(iores));12561256+ if (!sdma->regs) {12571257+ ret = -ENOMEM;12581258+ goto err_ioremap;12591259+ }12601260+12611261+ ret = request_irq(irq, sdma_int_handler, 0, "sdma", sdma);12621262+ if (ret)12631263+ goto err_request_irq;12641264+12651265+ fwname = kasprintf(GFP_KERNEL, "sdma-%s-to%d.bin",12661266+ pdata->cpu_name, pdata->to_version);12671267+ if (!fwname) {12681268+ ret = -ENOMEM;12691269+ goto err_cputype;12701270+ }12711271+12721272+ ret = request_firmware(&fw, fwname, &pdev->dev);12731273+ if (ret) {12741274+ dev_err(&pdev->dev, "request firmware \"%s\" failed with %d\n",12751275+ fwname, ret);12761276+ kfree(fwname);12771277+ goto err_cputype;12781278+ }12791279+ kfree(fwname);12801280+12811281+ if (fw->size < sizeof(*header))12821282+ goto err_firmware;12831283+12841284+ header = (struct sdma_firmware_header *)fw->data;12851285+12861286+ if (header->magic != SDMA_FIRMWARE_MAGIC)12871287+ goto err_firmware;12881288+ if (header->ram_code_start + header->ram_code_size > fw->size)12891289+ goto err_firmware;12901290+12911291+ addr = (void *)header + header->script_addrs_start;12921292+ ram_code = (void *)header + header->ram_code_start;12931293+ sdma->script_addrs = kmalloc(sizeof(*addr), GFP_KERNEL);12941294+ if (!sdma->script_addrs)12951295+ goto err_firmware;12961296+ memcpy(sdma->script_addrs, addr, sizeof(*addr));12971297+12981298+ sdma->version = pdata->sdma_version;12991299+13001300+ INIT_LIST_HEAD(&sdma->dma_device.channels);13011301+ /* Initialize channel parameters */13021302+ for (i = 0; i < MAX_DMA_CHANNELS; i++) {13031303+ struct sdma_channel *sdmac = &sdma->channel[i];13041304+13051305+ sdmac->sdma = sdma;13061306+ spin_lock_init(&sdmac->lock);13071307+13081308+ dma_cap_set(DMA_SLAVE, sdma->dma_device.cap_mask);13091309+ dma_cap_set(DMA_CYCLIC, sdma->dma_device.cap_mask);13101310+13111311+ sdmac->chan.device = &sdma->dma_device;13121312+ sdmac->chan.chan_id = i;13131313+ sdmac->channel = i;13141314+13151315+ /* Add the channel to the DMAC list */13161316+ list_add_tail(&sdmac->chan.device_node, &sdma->dma_device.channels);13171317+ }13181318+13191319+ ret = sdma_init(sdma, ram_code, header->ram_code_size);13201320+ if (ret)13211321+ goto err_init;13221322+13231323+ sdma->dma_device.dev = &pdev->dev;13241324+13251325+ sdma->dma_device.device_alloc_chan_resources = sdma_alloc_chan_resources;13261326+ sdma->dma_device.device_free_chan_resources = sdma_free_chan_resources;13271327+ sdma->dma_device.device_tx_status = sdma_tx_status;13281328+ sdma->dma_device.device_prep_slave_sg = sdma_prep_slave_sg;13291329+ sdma->dma_device.device_prep_dma_cyclic = sdma_prep_dma_cyclic;13301330+ sdma->dma_device.device_control = sdma_control;13311331+ sdma->dma_device.device_issue_pending = sdma_issue_pending;13321332+13331333+ ret = dma_async_device_register(&sdma->dma_device);13341334+ if (ret) {13351335+ dev_err(&pdev->dev, "unable to register\n");13361336+ goto err_init;13371337+ }13381338+13391339+ dev_info(&pdev->dev, "initialized (firmware %d.%d)\n",13401340+ header->version_major,13411341+ header->version_minor);13421342+13431343+ /* request channel 0. This is an internal control channel13441344+ * to the SDMA engine and not available to clients.13451345+ */13461346+ dma_cap_zero(mask);13471347+ dma_cap_set(DMA_SLAVE, mask);13481348+ dma_request_channel(mask, NULL, NULL);13491349+13501350+ release_firmware(fw);13511351+13521352+ return 0;13531353+13541354+err_init:13551355+ kfree(sdma->script_addrs);13561356+err_firmware:13571357+ release_firmware(fw);13581358+err_cputype:13591359+ free_irq(irq, sdma);13601360+err_request_irq:13611361+ iounmap(sdma->regs);13621362+err_ioremap:13631363+ clk_put(sdma->clk);13641364+err_clk:13651365+ release_mem_region(iores->start, resource_size(iores));13661366+err_request_region:13671367+err_irq:13681368+ kfree(sdma);13691369+ return 0;13701370+}13711371+13721372+static int __exit sdma_remove(struct platform_device *pdev)13731373+{13741374+ return -EBUSY;13751375+}13761376+13771377+static struct platform_driver sdma_driver = {13781378+ .driver = {13791379+ .name = "imx-sdma",13801380+ },13811381+ .remove = __exit_p(sdma_remove),13821382+};13831383+13841384+static int __init sdma_module_init(void)13851385+{13861386+ return platform_driver_probe(&sdma_driver, sdma_probe);13871387+}13881388+subsys_initcall(sdma_module_init);13891389+13901390+MODULE_AUTHOR("Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>");13911391+MODULE_DESCRIPTION("i.MX SDMA driver");13921392+MODULE_LICENSE("GPL");
+390-86
drivers/dma/intel_mid_dma.c
···2525 */2626#include <linux/pci.h>2727#include <linux/interrupt.h>2828+#include <linux/pm_runtime.h>2829#include <linux/intel_mid_dma.h>29303031#define MAX_CHAN 4 /*max ch across controllers*/···9291 int byte_width = 0, block_ts = 0;93929493 switch (tx_width) {9595- case LNW_DMA_WIDTH_8BIT:9494+ case DMA_SLAVE_BUSWIDTH_1_BYTE:9695 byte_width = 1;9796 break;9898- case LNW_DMA_WIDTH_16BIT:9797+ case DMA_SLAVE_BUSWIDTH_2_BYTES:9998 byte_width = 2;10099 break;101101- case LNW_DMA_WIDTH_32BIT:100100+ case DMA_SLAVE_BUSWIDTH_4_BYTES:102101 default:103102 byte_width = 4;104103 break;···248247 struct middma_device *mid = to_middma_device(midc->chan.device);249248250249 /* channel is idle */251251- if (midc->in_use && test_ch_en(midc->dma_base, midc->ch_id)) {250250+ if (midc->busy && test_ch_en(midc->dma_base, midc->ch_id)) {252251 /*error*/253252 pr_err("ERR_MDMA: channel is busy in start\n");254253 /* The tasklet will hopefully advance the queue... */255254 return;256255 }257257-256256+ midc->busy = true;258257 /*write registers and en*/259258 iowrite32(first->sar, midc->ch_regs + SAR);260259 iowrite32(first->dar, midc->ch_regs + DAR);260260+ iowrite32(first->lli_phys, midc->ch_regs + LLP);261261 iowrite32(first->cfg_hi, midc->ch_regs + CFG_HIGH);262262 iowrite32(first->cfg_lo, midc->ch_regs + CFG_LOW);263263 iowrite32(first->ctl_lo, midc->ch_regs + CTL_LOW);···266264 pr_debug("MDMA:TX SAR %x,DAR %x,CFGL %x,CFGH %x,CTLH %x, CTLL %x\n",267265 (int)first->sar, (int)first->dar, first->cfg_hi,268266 first->cfg_lo, first->ctl_hi, first->ctl_lo);267267+ first->status = DMA_IN_PROGRESS;269268270269 iowrite32(ENABLE_CHANNEL(midc->ch_id), mid->dma_base + DMA_CHAN_EN);271271- first->status = DMA_IN_PROGRESS;272270}273271274272/**···285283{286284 struct dma_async_tx_descriptor *txd = &desc->txd;287285 dma_async_tx_callback callback_txd = NULL;286286+ struct intel_mid_dma_lli *llitem;288287 void *param_txd = NULL;289288290289 midc->completed = txd->cookie;291290 callback_txd = txd->callback;292291 param_txd = txd->callback_param;293292294294- list_move(&desc->desc_node, &midc->free_list);295295-293293+ if (desc->lli != NULL) {294294+ /*clear the DONE bit of completed LLI in memory*/295295+ llitem = desc->lli + desc->current_lli;296296+ llitem->ctl_hi &= CLEAR_DONE;297297+ if (desc->current_lli < desc->lli_length-1)298298+ (desc->current_lli)++;299299+ else300300+ desc->current_lli = 0;301301+ }296302 spin_unlock_bh(&midc->lock);297303 if (callback_txd) {298304 pr_debug("MDMA: TXD callback set ... calling\n");299305 callback_txd(param_txd);300300- spin_lock_bh(&midc->lock);301301- return;306306+ }307307+ if (midc->raw_tfr) {308308+ desc->status = DMA_SUCCESS;309309+ if (desc->lli != NULL) {310310+ pci_pool_free(desc->lli_pool, desc->lli,311311+ desc->lli_phys);312312+ pci_pool_destroy(desc->lli_pool);313313+ }314314+ list_move(&desc->desc_node, &midc->free_list);315315+ midc->busy = false;302316 }303317 spin_lock_bh(&midc->lock);304318···335317336318 /*tx is complete*/337319 list_for_each_entry_safe(desc, _desc, &midc->active_list, desc_node) {338338- if (desc->status == DMA_IN_PROGRESS) {339339- desc->status = DMA_SUCCESS;320320+ if (desc->status == DMA_IN_PROGRESS)340321 midc_descriptor_complete(midc, desc);341341- }342322 }343323 return;344344-}324324+ }325325+/**326326+ * midc_lli_fill_sg - Helper function to convert327327+ * SG list to Linked List Items.328328+ *@midc: Channel329329+ *@desc: DMA descriptor330330+ *@sglist: Pointer to SG list331331+ *@sglen: SG list length332332+ *@flags: DMA transaction flags333333+ *334334+ * Walk through the SG list and convert the SG list into Linked335335+ * List Items (LLI).336336+ */337337+static int midc_lli_fill_sg(struct intel_mid_dma_chan *midc,338338+ struct intel_mid_dma_desc *desc,339339+ struct scatterlist *sglist,340340+ unsigned int sglen,341341+ unsigned int flags)342342+{343343+ struct intel_mid_dma_slave *mids;344344+ struct scatterlist *sg;345345+ dma_addr_t lli_next, sg_phy_addr;346346+ struct intel_mid_dma_lli *lli_bloc_desc;347347+ union intel_mid_dma_ctl_lo ctl_lo;348348+ union intel_mid_dma_ctl_hi ctl_hi;349349+ int i;345350351351+ pr_debug("MDMA: Entered midc_lli_fill_sg\n");352352+ mids = midc->mid_slave;353353+354354+ lli_bloc_desc = desc->lli;355355+ lli_next = desc->lli_phys;356356+357357+ ctl_lo.ctl_lo = desc->ctl_lo;358358+ ctl_hi.ctl_hi = desc->ctl_hi;359359+ for_each_sg(sglist, sg, sglen, i) {360360+ /*Populate CTL_LOW and LLI values*/361361+ if (i != sglen - 1) {362362+ lli_next = lli_next +363363+ sizeof(struct intel_mid_dma_lli);364364+ } else {365365+ /*Check for circular list, otherwise terminate LLI to ZERO*/366366+ if (flags & DMA_PREP_CIRCULAR_LIST) {367367+ pr_debug("MDMA: LLI is configured in circular mode\n");368368+ lli_next = desc->lli_phys;369369+ } else {370370+ lli_next = 0;371371+ ctl_lo.ctlx.llp_dst_en = 0;372372+ ctl_lo.ctlx.llp_src_en = 0;373373+ }374374+ }375375+ /*Populate CTL_HI values*/376376+ ctl_hi.ctlx.block_ts = get_block_ts(sg->length,377377+ desc->width,378378+ midc->dma->block_size);379379+ /*Populate SAR and DAR values*/380380+ sg_phy_addr = sg_phys(sg);381381+ if (desc->dirn == DMA_TO_DEVICE) {382382+ lli_bloc_desc->sar = sg_phy_addr;383383+ lli_bloc_desc->dar = mids->dma_slave.dst_addr;384384+ } else if (desc->dirn == DMA_FROM_DEVICE) {385385+ lli_bloc_desc->sar = mids->dma_slave.src_addr;386386+ lli_bloc_desc->dar = sg_phy_addr;387387+ }388388+ /*Copy values into block descriptor in system memroy*/389389+ lli_bloc_desc->llp = lli_next;390390+ lli_bloc_desc->ctl_lo = ctl_lo.ctl_lo;391391+ lli_bloc_desc->ctl_hi = ctl_hi.ctl_hi;392392+393393+ lli_bloc_desc++;394394+ }395395+ /*Copy very first LLI values to descriptor*/396396+ desc->ctl_lo = desc->lli->ctl_lo;397397+ desc->ctl_hi = desc->lli->ctl_hi;398398+ desc->sar = desc->lli->sar;399399+ desc->dar = desc->lli->dar;400400+401401+ return 0;402402+}346403/*****************************************************************************347404DMA engine callback Functions*/348405/**···442349 desc->txd.cookie = cookie;443350444351445445- if (list_empty(&midc->active_list)) {446446- midc_dostart(midc, desc);352352+ if (list_empty(&midc->active_list))447353 list_add_tail(&desc->desc_node, &midc->active_list);448448- } else {354354+ else449355 list_add_tail(&desc->desc_node, &midc->queue);450450- }356356+357357+ midc_dostart(midc, desc);451358 spin_unlock_bh(&midc->lock);452359453360 return cookie;···507414 return ret;508415}509416417417+static int dma_slave_control(struct dma_chan *chan, unsigned long arg)418418+{419419+ struct intel_mid_dma_chan *midc = to_intel_mid_dma_chan(chan);420420+ struct dma_slave_config *slave = (struct dma_slave_config *)arg;421421+ struct intel_mid_dma_slave *mid_slave;422422+423423+ BUG_ON(!midc);424424+ BUG_ON(!slave);425425+ pr_debug("MDMA: slave control called\n");426426+427427+ mid_slave = to_intel_mid_dma_slave(slave);428428+429429+ BUG_ON(!mid_slave);430430+431431+ midc->mid_slave = mid_slave;432432+ return 0;433433+}510434/**511435 * intel_mid_dma_device_control - DMA device control512436 * @chan: chan for DMA control···538428 struct intel_mid_dma_chan *midc = to_intel_mid_dma_chan(chan);539429 struct middma_device *mid = to_middma_device(chan->device);540430 struct intel_mid_dma_desc *desc, *_desc;541541- LIST_HEAD(list);431431+ union intel_mid_dma_cfg_lo cfg_lo;432432+433433+ if (cmd == DMA_SLAVE_CONFIG)434434+ return dma_slave_control(chan, arg);542435543436 if (cmd != DMA_TERMINATE_ALL)544437 return -ENXIO;545438546439 spin_lock_bh(&midc->lock);547547- if (midc->in_use == false) {440440+ if (midc->busy == false) {548441 spin_unlock_bh(&midc->lock);549442 return 0;550443 }551551- list_splice_init(&midc->free_list, &list);552552- midc->descs_allocated = 0;553553- midc->slave = NULL;554554-444444+ /*Suspend and disable the channel*/445445+ cfg_lo.cfg_lo = ioread32(midc->ch_regs + CFG_LOW);446446+ cfg_lo.cfgx.ch_susp = 1;447447+ iowrite32(cfg_lo.cfg_lo, midc->ch_regs + CFG_LOW);448448+ iowrite32(DISABLE_CHANNEL(midc->ch_id), mid->dma_base + DMA_CHAN_EN);449449+ midc->busy = false;555450 /* Disable interrupts */556451 disable_dma_interrupt(midc);452452+ midc->descs_allocated = 0;557453558454 spin_unlock_bh(&midc->lock);559559- list_for_each_entry_safe(desc, _desc, &list, desc_node) {560560- pr_debug("MDMA: freeing descriptor %p\n", desc);561561- pci_pool_free(mid->dma_pool, desc, desc->txd.phys);455455+ list_for_each_entry_safe(desc, _desc, &midc->active_list, desc_node) {456456+ if (desc->lli != NULL) {457457+ pci_pool_free(desc->lli_pool, desc->lli,458458+ desc->lli_phys);459459+ pci_pool_destroy(desc->lli_pool);460460+ }461461+ list_move(&desc->desc_node, &midc->free_list);562462 }563463 return 0;564464}565465566566-/**567567- * intel_mid_dma_prep_slave_sg - Prep slave sg txn568568- * @chan: chan for DMA transfer569569- * @sgl: scatter gather list570570- * @sg_len: length of sg txn571571- * @direction: DMA transfer dirtn572572- * @flags: DMA flags573573- *574574- * Do DMA sg txn: NOT supported now575575- */576576-static struct dma_async_tx_descriptor *intel_mid_dma_prep_slave_sg(577577- struct dma_chan *chan, struct scatterlist *sgl,578578- unsigned int sg_len, enum dma_data_direction direction,579579- unsigned long flags)580580-{581581- /*not supported now*/582582- return NULL;583583-}584466585467/**586468 * intel_mid_dma_prep_memcpy - Prep memcpy txn···597495 union intel_mid_dma_ctl_hi ctl_hi;598496 union intel_mid_dma_cfg_lo cfg_lo;599497 union intel_mid_dma_cfg_hi cfg_hi;600600- enum intel_mid_dma_width width = 0;498498+ enum dma_slave_buswidth width;601499602500 pr_debug("MDMA: Prep for memcpy\n");603603- WARN_ON(!chan);501501+ BUG_ON(!chan);604502 if (!len)605503 return NULL;606504607607- mids = chan->private;608608- WARN_ON(!mids);609609-610505 midc = to_intel_mid_dma_chan(chan);611611- WARN_ON(!midc);506506+ BUG_ON(!midc);507507+508508+ mids = midc->mid_slave;509509+ BUG_ON(!mids);612510613511 pr_debug("MDMA:called for DMA %x CH %d Length %zu\n",614512 midc->dma->pci_id, midc->ch_id, len);615513 pr_debug("MDMA:Cfg passed Mode %x, Dirn %x, HS %x, Width %x\n",616616- mids->cfg_mode, mids->dirn, mids->hs_mode, mids->src_width);514514+ mids->cfg_mode, mids->dma_slave.direction,515515+ mids->hs_mode, mids->dma_slave.src_addr_width);617516618517 /*calculate CFG_LO*/619518 if (mids->hs_mode == LNW_DMA_SW_HS) {···633530 if (midc->dma->pimr_mask) {634531 cfg_hi.cfgx.protctl = 0x0; /*default value*/635532 cfg_hi.cfgx.fifo_mode = 1;636636- if (mids->dirn == DMA_TO_DEVICE) {533533+ if (mids->dma_slave.direction == DMA_TO_DEVICE) {637534 cfg_hi.cfgx.src_per = 0;638535 if (mids->device_instance == 0)639536 cfg_hi.cfgx.dst_per = 3;640537 if (mids->device_instance == 1)641538 cfg_hi.cfgx.dst_per = 1;642642- } else if (mids->dirn == DMA_FROM_DEVICE) {539539+ } else if (mids->dma_slave.direction == DMA_FROM_DEVICE) {643540 if (mids->device_instance == 0)644541 cfg_hi.cfgx.src_per = 2;645542 if (mids->device_instance == 1)···655552656553 /*calculate CTL_HI*/657554 ctl_hi.ctlx.reser = 0;658658- width = mids->src_width;555555+ ctl_hi.ctlx.done = 0;556556+ width = mids->dma_slave.src_addr_width;659557660558 ctl_hi.ctlx.block_ts = get_block_ts(len, width, midc->dma->block_size);661559 pr_debug("MDMA:calc len %d for block size %d\n",···664560 /*calculate CTL_LO*/665561 ctl_lo.ctl_lo = 0;666562 ctl_lo.ctlx.int_en = 1;667667- ctl_lo.ctlx.dst_tr_width = mids->dst_width;668668- ctl_lo.ctlx.src_tr_width = mids->src_width;669669- ctl_lo.ctlx.dst_msize = mids->src_msize;670670- ctl_lo.ctlx.src_msize = mids->dst_msize;563563+ ctl_lo.ctlx.dst_tr_width = mids->dma_slave.dst_addr_width;564564+ ctl_lo.ctlx.src_tr_width = mids->dma_slave.src_addr_width;565565+ ctl_lo.ctlx.dst_msize = mids->dma_slave.src_maxburst;566566+ ctl_lo.ctlx.src_msize = mids->dma_slave.dst_maxburst;671567672568 if (mids->cfg_mode == LNW_DMA_MEM_TO_MEM) {673569 ctl_lo.ctlx.tt_fc = 0;674570 ctl_lo.ctlx.sinc = 0;675571 ctl_lo.ctlx.dinc = 0;676572 } else {677677- if (mids->dirn == DMA_TO_DEVICE) {573573+ if (mids->dma_slave.direction == DMA_TO_DEVICE) {678574 ctl_lo.ctlx.sinc = 0;679575 ctl_lo.ctlx.dinc = 2;680576 ctl_lo.ctlx.tt_fc = 1;681681- } else if (mids->dirn == DMA_FROM_DEVICE) {577577+ } else if (mids->dma_slave.direction == DMA_FROM_DEVICE) {682578 ctl_lo.ctlx.sinc = 2;683579 ctl_lo.ctlx.dinc = 0;684580 ctl_lo.ctlx.tt_fc = 2;···701597 desc->ctl_lo = ctl_lo.ctl_lo;702598 desc->ctl_hi = ctl_hi.ctl_hi;703599 desc->width = width;704704- desc->dirn = mids->dirn;600600+ desc->dirn = mids->dma_slave.direction;601601+ desc->lli_phys = 0;602602+ desc->lli = NULL;603603+ desc->lli_pool = NULL;705604 return &desc->txd;706605707606err_desc_get:708607 pr_err("ERR_MDMA: Failed to get desc\n");709608 midc_desc_put(midc, desc);710609 return NULL;610610+}611611+/**612612+ * intel_mid_dma_prep_slave_sg - Prep slave sg txn613613+ * @chan: chan for DMA transfer614614+ * @sgl: scatter gather list615615+ * @sg_len: length of sg txn616616+ * @direction: DMA transfer dirtn617617+ * @flags: DMA flags618618+ *619619+ * Prepares LLI based periphral transfer620620+ */621621+static struct dma_async_tx_descriptor *intel_mid_dma_prep_slave_sg(622622+ struct dma_chan *chan, struct scatterlist *sgl,623623+ unsigned int sg_len, enum dma_data_direction direction,624624+ unsigned long flags)625625+{626626+ struct intel_mid_dma_chan *midc = NULL;627627+ struct intel_mid_dma_slave *mids = NULL;628628+ struct intel_mid_dma_desc *desc = NULL;629629+ struct dma_async_tx_descriptor *txd = NULL;630630+ union intel_mid_dma_ctl_lo ctl_lo;631631+632632+ pr_debug("MDMA: Prep for slave SG\n");633633+634634+ if (!sg_len) {635635+ pr_err("MDMA: Invalid SG length\n");636636+ return NULL;637637+ }638638+ midc = to_intel_mid_dma_chan(chan);639639+ BUG_ON(!midc);640640+641641+ mids = midc->mid_slave;642642+ BUG_ON(!mids);643643+644644+ if (!midc->dma->pimr_mask) {645645+ pr_debug("MDMA: SG list is not supported by this controller\n");646646+ return NULL;647647+ }648648+649649+ pr_debug("MDMA: SG Length = %d, direction = %d, Flags = %#lx\n",650650+ sg_len, direction, flags);651651+652652+ txd = intel_mid_dma_prep_memcpy(chan, 0, 0, sgl->length, flags);653653+ if (NULL == txd) {654654+ pr_err("MDMA: Prep memcpy failed\n");655655+ return NULL;656656+ }657657+ desc = to_intel_mid_dma_desc(txd);658658+ desc->dirn = direction;659659+ ctl_lo.ctl_lo = desc->ctl_lo;660660+ ctl_lo.ctlx.llp_dst_en = 1;661661+ ctl_lo.ctlx.llp_src_en = 1;662662+ desc->ctl_lo = ctl_lo.ctl_lo;663663+ desc->lli_length = sg_len;664664+ desc->current_lli = 0;665665+ /* DMA coherent memory pool for LLI descriptors*/666666+ desc->lli_pool = pci_pool_create("intel_mid_dma_lli_pool",667667+ midc->dma->pdev,668668+ (sizeof(struct intel_mid_dma_lli)*sg_len),669669+ 32, 0);670670+ if (NULL == desc->lli_pool) {671671+ pr_err("MID_DMA:LLI pool create failed\n");672672+ return NULL;673673+ }674674+675675+ desc->lli = pci_pool_alloc(desc->lli_pool, GFP_KERNEL, &desc->lli_phys);676676+ if (!desc->lli) {677677+ pr_err("MID_DMA: LLI alloc failed\n");678678+ pci_pool_destroy(desc->lli_pool);679679+ return NULL;680680+ }681681+682682+ midc_lli_fill_sg(midc, desc, sgl, sg_len, flags);683683+ if (flags & DMA_PREP_INTERRUPT) {684684+ iowrite32(UNMASK_INTR_REG(midc->ch_id),685685+ midc->dma_base + MASK_BLOCK);686686+ pr_debug("MDMA:Enabled Block interrupt\n");687687+ }688688+ return &desc->txd;711689}712690713691/**···804618 struct middma_device *mid = to_middma_device(chan->device);805619 struct intel_mid_dma_desc *desc, *_desc;806620807807- if (true == midc->in_use) {621621+ if (true == midc->busy) {808622 /*trying to free ch in use!!!!!*/809623 pr_err("ERR_MDMA: trying to free ch in use\n");810624 }811811-625625+ pm_runtime_put(&mid->pdev->dev);812626 spin_lock_bh(&midc->lock);813627 midc->descs_allocated = 0;814628 list_for_each_entry_safe(desc, _desc, &midc->active_list, desc_node) {···825639 }826640 spin_unlock_bh(&midc->lock);827641 midc->in_use = false;642642+ midc->busy = false;828643 /* Disable CH interrupts */829644 iowrite32(MASK_INTR_REG(midc->ch_id), mid->dma_base + MASK_BLOCK);830645 iowrite32(MASK_INTR_REG(midc->ch_id), mid->dma_base + MASK_ERR);···846659 dma_addr_t phys;847660 int i = 0;848661662662+ pm_runtime_get_sync(&mid->pdev->dev);663663+664664+ if (mid->state == SUSPENDED) {665665+ if (dma_resume(mid->pdev)) {666666+ pr_err("ERR_MDMA: resume failed");667667+ return -EFAULT;668668+ }669669+ }849670850671 /* ASSERT: channel is idle */851672 if (test_ch_en(mid->dma_base, midc->ch_id)) {852673 /*ch is not idle*/853674 pr_err("ERR_MDMA: ch not idle\n");675675+ pm_runtime_put(&mid->pdev->dev);854676 return -EIO;855677 }856678 midc->completed = chan->cookie = 1;···870674 desc = pci_pool_alloc(mid->dma_pool, GFP_KERNEL, &phys);871675 if (!desc) {872676 pr_err("ERR_MDMA: desc failed\n");677677+ pm_runtime_put(&mid->pdev->dev);873678 return -ENOMEM;874679 /*check*/875680 }···883686 list_add_tail(&desc->desc_node, &midc->free_list);884687 }885688 spin_unlock_bh(&midc->lock);886886- midc->in_use = false;689689+ midc->in_use = true;690690+ midc->busy = false;887691 pr_debug("MID_DMA: Desc alloc done ret: %d desc\n", i);888692 return i;889693}···913715{914716 struct middma_device *mid = NULL;915717 struct intel_mid_dma_chan *midc = NULL;916916- u32 status;718718+ u32 status, raw_tfr, raw_block;917719 int i;918720919721 mid = (struct middma_device *)data;···922724 return;923725 }924726 pr_debug("MDMA: in tasklet for device %x\n", mid->pci_id);925925- status = ioread32(mid->dma_base + RAW_TFR);926926- pr_debug("MDMA:RAW_TFR %x\n", status);727727+ raw_tfr = ioread32(mid->dma_base + RAW_TFR);728728+ raw_block = ioread32(mid->dma_base + RAW_BLOCK);729729+ status = raw_tfr | raw_block;927730 status &= mid->intr_mask;928731 while (status) {929732 /*txn interrupt*/···940741 }941742 pr_debug("MDMA:Tx complete interrupt %x, Ch No %d Index %d\n",942743 status, midc->ch_id, i);744744+ midc->raw_tfr = raw_tfr;745745+ midc->raw_block = raw_block;746746+ spin_lock_bh(&midc->lock);943747 /*clearing this interrupts first*/944748 iowrite32((1 << midc->ch_id), mid->dma_base + CLEAR_TFR);945945- iowrite32((1 << midc->ch_id), mid->dma_base + CLEAR_BLOCK);946946-947947- spin_lock_bh(&midc->lock);749749+ if (raw_block) {750750+ iowrite32((1 << midc->ch_id),751751+ mid->dma_base + CLEAR_BLOCK);752752+ }948753 midc_scan_descriptors(mid, midc);949754 pr_debug("MDMA:Scan of desc... complete, unmasking\n");950755 iowrite32(UNMASK_INTR_REG(midc->ch_id),951756 mid->dma_base + MASK_TFR);757757+ if (raw_block) {758758+ iowrite32(UNMASK_INTR_REG(midc->ch_id),759759+ mid->dma_base + MASK_BLOCK);760760+ }952761 spin_unlock_bh(&midc->lock);953762 }954763···1011804static irqreturn_t intel_mid_dma_interrupt(int irq, void *data)1012805{1013806 struct middma_device *mid = data;10141014- u32 status;807807+ u32 tfr_status, err_status;1015808 int call_tasklet = 0;809809+810810+ tfr_status = ioread32(mid->dma_base + RAW_TFR);811811+ err_status = ioread32(mid->dma_base + RAW_ERR);812812+ if (!tfr_status && !err_status)813813+ return IRQ_NONE;10168141017815 /*DMA Interrupt*/1018816 pr_debug("MDMA:Got an interrupt on irq %d\n", irq);···1026814 return -EINVAL;1027815 }102881610291029- status = ioread32(mid->dma_base + RAW_TFR);10301030- pr_debug("MDMA: Status %x, Mask %x\n", status, mid->intr_mask);10311031- status &= mid->intr_mask;10321032- if (status) {817817+ pr_debug("MDMA: Status %x, Mask %x\n", tfr_status, mid->intr_mask);818818+ tfr_status &= mid->intr_mask;819819+ if (tfr_status) {1033820 /*need to disable intr*/10341034- iowrite32((status << 8), mid->dma_base + MASK_TFR);10351035- pr_debug("MDMA: Calling tasklet %x\n", status);821821+ iowrite32((tfr_status << INT_MASK_WE), mid->dma_base + MASK_TFR);822822+ iowrite32((tfr_status << INT_MASK_WE), mid->dma_base + MASK_BLOCK);823823+ pr_debug("MDMA: Calling tasklet %x\n", tfr_status);1036824 call_tasklet = 1;1037825 }10381038- status = ioread32(mid->dma_base + RAW_ERR);10391039- status &= mid->intr_mask;10401040- if (status) {10411041- iowrite32(MASK_INTR_REG(status), mid->dma_base + MASK_ERR);826826+ err_status &= mid->intr_mask;827827+ if (err_status) {828828+ iowrite32(MASK_INTR_REG(err_status), mid->dma_base + MASK_ERR);1042829 call_tasklet = 1;1043830 }1044831 if (call_tasklet)···1067856{1068857 struct middma_device *dma = pci_get_drvdata(pdev);1069858 int err, i;10701070- unsigned int irq_level;10718591072860 /* DMA coherent memory pool for DMA descriptor allocations */1073861 dma->dma_pool = pci_pool_create("intel_mid_dma_desc_pool", pdev,···1094884 pr_debug("MDMA:Adding %d channel for this controller\n", dma->max_chan);1095885 /*init CH structures*/1096886 dma->intr_mask = 0;887887+ dma->state = RUNNING;1097888 for (i = 0; i < dma->max_chan; i++) {1098889 struct intel_mid_dma_chan *midch = &dma->ch[i];1099890···11549431155944 /*register irq */1156945 if (dma->pimr_mask) {11571157- irq_level = IRQF_SHARED;1158946 pr_debug("MDMA:Requesting irq shared for DMAC1\n");1159947 err = request_irq(pdev->irq, intel_mid_dma_interrupt1,1160948 IRQF_SHARED, "INTEL_MID_DMAC1", dma);···1161951 goto err_irq;1162952 } else {1163953 dma->intr_mask = 0x03;11641164- irq_level = 0;1165954 pr_debug("MDMA:Requesting irq for DMAC2\n");1166955 err = request_irq(pdev->irq, intel_mid_dma_interrupt2,11671167- 0, "INTEL_MID_DMAC2", dma);956956+ IRQF_SHARED, "INTEL_MID_DMAC2", dma);1168957 if (0 != err)1169958 goto err_irq;1170959 }···12791070 if (err)12801071 goto err_dma;1281107210731073+ pm_runtime_set_active(&pdev->dev);10741074+ pm_runtime_enable(&pdev->dev);10751075+ pm_runtime_allow(&pdev->dev);12821076 return 0;1283107712841078err_dma:···13161104 pci_disable_device(pdev);13171105}1318110611071107+/* Power Management */11081108+/*11091109+* dma_suspend - PCI suspend function11101110+*11111111+* @pci: PCI device structure11121112+* @state: PM message11131113+*11141114+* This function is called by OS when a power event occurs11151115+*/11161116+int dma_suspend(struct pci_dev *pci, pm_message_t state)11171117+{11181118+ int i;11191119+ struct middma_device *device = pci_get_drvdata(pci);11201120+ pr_debug("MDMA: dma_suspend called\n");11211121+11221122+ for (i = 0; i < device->max_chan; i++) {11231123+ if (device->ch[i].in_use)11241124+ return -EAGAIN;11251125+ }11261126+ device->state = SUSPENDED;11271127+ pci_set_drvdata(pci, device);11281128+ pci_save_state(pci);11291129+ pci_disable_device(pci);11301130+ pci_set_power_state(pci, PCI_D3hot);11311131+ return 0;11321132+}11331133+11341134+/**11351135+* dma_resume - PCI resume function11361136+*11371137+* @pci: PCI device structure11381138+*11391139+* This function is called by OS when a power event occurs11401140+*/11411141+int dma_resume(struct pci_dev *pci)11421142+{11431143+ int ret;11441144+ struct middma_device *device = pci_get_drvdata(pci);11451145+11461146+ pr_debug("MDMA: dma_resume called\n");11471147+ pci_set_power_state(pci, PCI_D0);11481148+ pci_restore_state(pci);11491149+ ret = pci_enable_device(pci);11501150+ if (ret) {11511151+ pr_err("MDMA: device cant be enabled for %x\n", pci->device);11521152+ return ret;11531153+ }11541154+ device->state = RUNNING;11551155+ iowrite32(REG_BIT0, device->dma_base + DMA_CFG);11561156+ pci_set_drvdata(pci, device);11571157+ return 0;11581158+}11591159+11601160+static int dma_runtime_suspend(struct device *dev)11611161+{11621162+ struct pci_dev *pci_dev = to_pci_dev(dev);11631163+ return dma_suspend(pci_dev, PMSG_SUSPEND);11641164+}11651165+11661166+static int dma_runtime_resume(struct device *dev)11671167+{11681168+ struct pci_dev *pci_dev = to_pci_dev(dev);11691169+ return dma_resume(pci_dev);11701170+}11711171+11721172+static int dma_runtime_idle(struct device *dev)11731173+{11741174+ struct pci_dev *pdev = to_pci_dev(dev);11751175+ struct middma_device *device = pci_get_drvdata(pdev);11761176+ int i;11771177+11781178+ for (i = 0; i < device->max_chan; i++) {11791179+ if (device->ch[i].in_use)11801180+ return -EAGAIN;11811181+ }11821182+11831183+ return pm_schedule_suspend(dev, 0);11841184+}11851185+13191186/******************************************************************************13201187* PCI stuff13211188*/···14071116};14081117MODULE_DEVICE_TABLE(pci, intel_mid_dma_ids);1409111811191119+static const struct dev_pm_ops intel_mid_dma_pm = {11201120+ .runtime_suspend = dma_runtime_suspend,11211121+ .runtime_resume = dma_runtime_resume,11221122+ .runtime_idle = dma_runtime_idle,11231123+};11241124+14101125static struct pci_driver intel_mid_dma_pci = {14111126 .name = "Intel MID DMA",14121127 .id_table = intel_mid_dma_ids,14131128 .probe = intel_mid_dma_probe,14141129 .remove = __devexit_p(intel_mid_dma_remove),11301130+#ifdef CONFIG_PM11311131+ .suspend = dma_suspend,11321132+ .resume = dma_resume,11331133+ .driver = {11341134+ .pm = &intel_mid_dma_pm,11351135+ },11361136+#endif14151137};1416113814171139static int __init intel_mid_dma_init(void)
+47-6
drivers/dma/intel_mid_dma_regs.h
···2929#include <linux/dmapool.h>3030#include <linux/pci_ids.h>31313232-#define INTEL_MID_DMA_DRIVER_VERSION "1.0.5"3232+#define INTEL_MID_DMA_DRIVER_VERSION "1.1.0"33333434#define REG_BIT0 0x000000013535#define REG_BIT8 0x000001003636-3636+#define INT_MASK_WE 0x83737+#define CLEAR_DONE 0xFFFFEFFF3738#define UNMASK_INTR_REG(chan_num) \3839 ((REG_BIT0 << chan_num) | (REG_BIT8 << chan_num))3940#define MASK_INTR_REG(chan_num) (REG_BIT8 << chan_num)40414142#define ENABLE_CHANNEL(chan_num) \4243 ((REG_BIT0 << chan_num) | (REG_BIT8 << chan_num))4444+4545+#define DISABLE_CHANNEL(chan_num) \4646+ (REG_BIT8 << chan_num)43474448#define DESCS_PER_CHANNEL 164549/*DMA Registers*/···5450/*CH X REG = (DMA_CH_SIZE)*CH_NO + REG*/5551#define SAR 0x00 /* Source Address Register*/5652#define DAR 0x08 /* Destination Address Register*/5353+#define LLP 0x10 /* Linked List Pointer Register*/5754#define CTL_LOW 0x18 /* Control Register*/5855#define CTL_HIGH 0x1C /* Control Register*/5956#define CFG_LOW 0x40 /* Configuration Register Low*/···117112union intel_mid_dma_ctl_hi {118113 struct {119114 u32 block_ts:12; /*block transfer size*/120120- /*configured by DMAC*/121121- u32 reser:20;115115+ u32 done:1; /*Done - updated by DMAC*/116116+ u32 reser:19; /*configured by DMAC*/122117 } ctlx;123118 u32 ctl_hi;124119···157152 u32 cfg_hi;158153};159154155155+160156/**161157 * struct intel_mid_dma_chan - internal mid representation of a DMA channel162158 * @chan: dma_chan strcture represetation for mid chan···172166 * @slave: dma slave struture173167 * @descs_allocated: total number of decsiptors allocated174168 * @dma: dma device struture pointer169169+ * @busy: bool representing if ch is busy (active txn) or not175170 * @in_use: bool representing if ch is in use or not171171+ * @raw_tfr: raw trf interrupt recieved172172+ * @raw_block: raw block interrupt recieved176173 */177174struct intel_mid_dma_chan {178175 struct dma_chan chan;···187178 struct list_head active_list;188179 struct list_head queue;189180 struct list_head free_list;190190- struct intel_mid_dma_slave *slave;191181 unsigned int descs_allocated;192182 struct middma_device *dma;183183+ bool busy;193184 bool in_use;185185+ u32 raw_tfr;186186+ u32 raw_block;187187+ struct intel_mid_dma_slave *mid_slave;194188};195189196190static inline struct intel_mid_dma_chan *to_intel_mid_dma_chan(···202190 return container_of(chan, struct intel_mid_dma_chan, chan);203191}204192193193+enum intel_mid_dma_state {194194+ RUNNING = 0,195195+ SUSPENDED,196196+};205197/**206198 * struct middma_device - internal representation of a DMA device207199 * @pdev: PCI device···221205 * @max_chan: max number of chs supported (from drv_data)222206 * @block_size: Block size of DMA transfer supported (from drv_data)223207 * @pimr_mask: MMIO register addr for periphral interrupt (from drv_data)208208+ * @state: dma PM device state224209 */225210struct middma_device {226211 struct pci_dev *pdev;···237220 int max_chan;238221 int block_size;239222 unsigned int pimr_mask;223223+ enum intel_mid_dma_state state;240224};241225242226static inline struct middma_device *to_middma_device(struct dma_device *common)···256238 u32 cfg_lo;257239 u32 ctl_lo;258240 u32 ctl_hi;241241+ struct pci_pool *lli_pool;242242+ struct intel_mid_dma_lli *lli;243243+ dma_addr_t lli_phys;244244+ unsigned int lli_length;245245+ unsigned int current_lli;259246 dma_addr_t next;260247 enum dma_data_direction dirn;261248 enum dma_status status;262262- enum intel_mid_dma_width width; /*width of DMA txn*/249249+ enum dma_slave_buswidth width; /*width of DMA txn*/263250 enum intel_mid_dma_mode cfg_mode; /*mode configuration*/264251265252};253253+254254+struct intel_mid_dma_lli {255255+ dma_addr_t sar;256256+ dma_addr_t dar;257257+ dma_addr_t llp;258258+ u32 ctl_lo;259259+ u32 ctl_hi;260260+} __attribute__ ((packed));266261267262static inline int test_ch_en(void __iomem *dma, u32 ch_no)268263{···288257{289258 return container_of(txd, struct intel_mid_dma_desc, txd);290259}260260+261261+static inline struct intel_mid_dma_slave *to_intel_mid_dma_slave262262+ (struct dma_slave_config *slave)263263+{264264+ return container_of(slave, struct intel_mid_dma_slave, dma_slave);265265+}266266+267267+268268+int dma_resume(struct pci_dev *pci);269269+291270#endif /*__INTEL_MID_DMAC_REGS_H__*/
···11+/*22+ * linux/amba/pl08x.h - ARM PrimeCell DMA Controller driver33+ *44+ * Copyright (C) 2005 ARM Ltd55+ * Copyright (C) 2010 ST-Ericsson SA66+ *77+ * This program is free software; you can redistribute it and/or modify88+ * it under the terms of the GNU General Public License version 2 as99+ * published by the Free Software Foundation.1010+ *1111+ * pl08x information required by platform code1212+ *1313+ * Please credit ARM.com1414+ * Documentation: ARM DDI 0196D1515+ *1616+ */1717+1818+#ifndef AMBA_PL08X_H1919+#define AMBA_PL08X_H2020+2121+/* We need sizes of structs from this header */2222+#include <linux/dmaengine.h>2323+#include <linux/interrupt.h>2424+2525+/**2626+ * struct pl08x_channel_data - data structure to pass info between2727+ * platform and PL08x driver regarding channel configuration2828+ * @bus_id: name of this device channel, not just a device name since2929+ * devices may have more than one channel e.g. "foo_tx"3030+ * @min_signal: the minimum DMA signal number to be muxed in for this3131+ * channel (for platforms supporting muxed signals). If you have3232+ * static assignments, make sure this is set to the assigned signal3333+ * number, PL08x have 16 possible signals in number 0 thru 15 so3434+ * when these are not enough they often get muxed (in hardware)3535+ * disabling simultaneous use of the same channel for two devices.3636+ * @max_signal: the maximum DMA signal number to be muxed in for3737+ * the channel. Set to the same as min_signal for3838+ * devices with static assignments3939+ * @muxval: a number usually used to poke into some mux regiser to4040+ * mux in the signal to this channel4141+ * @cctl_opt: default options for the channel control register4242+ * @addr: source/target address in physical memory for this DMA channel,4343+ * can be the address of a FIFO register for burst requests for example.4444+ * This can be left undefined if the PrimeCell API is used for configuring4545+ * this.4646+ * @circular_buffer: whether the buffer passed in is circular and4747+ * shall simply be looped round round (like a record baby round4848+ * round round round)4949+ * @single: the device connected to this channel will request single5050+ * DMA transfers, not bursts. (Bursts are default.)5151+ */5252+struct pl08x_channel_data {5353+ char *bus_id;5454+ int min_signal;5555+ int max_signal;5656+ u32 muxval;5757+ u32 cctl;5858+ u32 ccfg;5959+ dma_addr_t addr;6060+ bool circular_buffer;6161+ bool single;6262+};6363+6464+/**6565+ * Struct pl08x_bus_data - information of source or destination6666+ * busses for a transfer6767+ * @addr: current address6868+ * @maxwidth: the maximum width of a transfer on this bus6969+ * @buswidth: the width of this bus in bytes: 1, 2 or 47070+ * @fill_bytes: bytes required to fill to the next bus memory7171+ * boundary7272+ */7373+struct pl08x_bus_data {7474+ dma_addr_t addr;7575+ u8 maxwidth;7676+ u8 buswidth;7777+ u32 fill_bytes;7878+};7979+8080+/**8181+ * struct pl08x_phy_chan - holder for the physical channels8282+ * @id: physical index to this channel8383+ * @lock: a lock to use when altering an instance of this struct8484+ * @signal: the physical signal (aka channel) serving this8585+ * physical channel right now8686+ * @serving: the virtual channel currently being served by this8787+ * physical channel8888+ */8989+struct pl08x_phy_chan {9090+ unsigned int id;9191+ void __iomem *base;9292+ spinlock_t lock;9393+ int signal;9494+ struct pl08x_dma_chan *serving;9595+ u32 csrc;9696+ u32 cdst;9797+ u32 clli;9898+ u32 cctl;9999+ u32 ccfg;100100+};101101+102102+/**103103+ * struct pl08x_txd - wrapper for struct dma_async_tx_descriptor104104+ * @llis_bus: DMA memory address (physical) start for the LLIs105105+ * @llis_va: virtual memory address start for the LLIs106106+ */107107+struct pl08x_txd {108108+ struct dma_async_tx_descriptor tx;109109+ struct list_head node;110110+ enum dma_data_direction direction;111111+ struct pl08x_bus_data srcbus;112112+ struct pl08x_bus_data dstbus;113113+ int len;114114+ dma_addr_t llis_bus;115115+ void *llis_va;116116+ struct pl08x_channel_data *cd;117117+ bool active;118118+ /*119119+ * Settings to be put into the physical channel when we120120+ * trigger this txd121121+ */122122+ u32 csrc;123123+ u32 cdst;124124+ u32 clli;125125+ u32 cctl;126126+};127127+128128+/**129129+ * struct pl08x_dma_chan_state - holds the PL08x specific virtual130130+ * channel states131131+ * @PL08X_CHAN_IDLE: the channel is idle132132+ * @PL08X_CHAN_RUNNING: the channel has allocated a physical transport133133+ * channel and is running a transfer on it134134+ * @PL08X_CHAN_PAUSED: the channel has allocated a physical transport135135+ * channel, but the transfer is currently paused136136+ * @PL08X_CHAN_WAITING: the channel is waiting for a physical transport137137+ * channel to become available (only pertains to memcpy channels)138138+ */139139+enum pl08x_dma_chan_state {140140+ PL08X_CHAN_IDLE,141141+ PL08X_CHAN_RUNNING,142142+ PL08X_CHAN_PAUSED,143143+ PL08X_CHAN_WAITING,144144+};145145+146146+/**147147+ * struct pl08x_dma_chan - this structure wraps a DMA ENGINE channel148148+ * @chan: wrappped abstract channel149149+ * @phychan: the physical channel utilized by this channel, if there is one150150+ * @tasklet: tasklet scheduled by the IRQ to handle actual work etc151151+ * @name: name of channel152152+ * @cd: channel platform data153153+ * @runtime_addr: address for RX/TX according to the runtime config154154+ * @runtime_direction: current direction of this channel according to155155+ * runtime config156156+ * @lc: last completed transaction on this channel157157+ * @desc_list: queued transactions pending on this channel158158+ * @at: active transaction on this channel159159+ * @lockflags: sometimes we let a lock last between two function calls,160160+ * especially prep/submit, and then we need to store the IRQ flags161161+ * in the channel state, here162162+ * @lock: a lock for this channel data163163+ * @host: a pointer to the host (internal use)164164+ * @state: whether the channel is idle, paused, running etc165165+ * @slave: whether this channel is a device (slave) or for memcpy166166+ * @waiting: a TX descriptor on this channel which is waiting for167167+ * a physical channel to become available168168+ */169169+struct pl08x_dma_chan {170170+ struct dma_chan chan;171171+ struct pl08x_phy_chan *phychan;172172+ struct tasklet_struct tasklet;173173+ char *name;174174+ struct pl08x_channel_data *cd;175175+ dma_addr_t runtime_addr;176176+ enum dma_data_direction runtime_direction;177177+ atomic_t last_issued;178178+ dma_cookie_t lc;179179+ struct list_head desc_list;180180+ struct pl08x_txd *at;181181+ unsigned long lockflags;182182+ spinlock_t lock;183183+ void *host;184184+ enum pl08x_dma_chan_state state;185185+ bool slave;186186+ struct pl08x_txd *waiting;187187+};188188+189189+/**190190+ * struct pl08x_platform_data - the platform configuration for the191191+ * PL08x PrimeCells.192192+ * @slave_channels: the channels defined for the different devices on the193193+ * platform, all inclusive, including multiplexed channels. The available194194+ * physical channels will be multiplexed around these signals as they195195+ * are requested, just enumerate all possible channels.196196+ * @get_signal: request a physical signal to be used for a DMA197197+ * transfer immediately: if there is some multiplexing or similar blocking198198+ * the use of the channel the transfer can be denied by returning199199+ * less than zero, else it returns the allocated signal number200200+ * @put_signal: indicate to the platform that this physical signal is not201201+ * running any DMA transfer and multiplexing can be recycled202202+ * @bus_bit_lli: Bit[0] of the address indicated which AHB bus master the203203+ * LLI addresses are on 0/1 Master 1/2.204204+ */205205+struct pl08x_platform_data {206206+ struct pl08x_channel_data *slave_channels;207207+ unsigned int num_slave_channels;208208+ struct pl08x_channel_data memcpy_channel;209209+ int (*get_signal)(struct pl08x_dma_chan *);210210+ void (*put_signal)(struct pl08x_dma_chan *);211211+};212212+213213+#ifdef CONFIG_AMBA_PL08X214214+bool pl08x_filter_id(struct dma_chan *chan, void *chan_id);215215+#else216216+static inline bool pl08x_filter_id(struct dma_chan *chan, void *chan_id)217217+{218218+ return false;219219+}220220+#endif221221+222222+#endif /* AMBA_PL08X_H */
+52-2
include/linux/dmaengine.h
···6464 DMA_PQ_VAL,6565 DMA_MEMSET,6666 DMA_INTERRUPT,6767+ DMA_SG,6768 DMA_PRIVATE,6869 DMA_ASYNC_TX,6970 DMA_SLAVE,7171+ DMA_CYCLIC,7072};71737274/* last transaction type for creation of the capabilities mask */7373-#define DMA_TX_TYPE_END (DMA_SLAVE + 1)7575+#define DMA_TX_TYPE_END (DMA_CYCLIC + 1)747675777678/**···121119 * configuration data in statically from the platform). An additional122120 * argument of struct dma_slave_config must be passed in with this123121 * command.122122+ * @FSLDMA_EXTERNAL_START: this command will put the Freescale DMA controller123123+ * into external start mode.124124 */125125enum dma_ctrl_cmd {126126 DMA_TERMINATE_ALL,127127 DMA_PAUSE,128128 DMA_RESUME,129129 DMA_SLAVE_CONFIG,130130+ FSLDMA_EXTERNAL_START,130131};131132132133/**···427422 * @device_prep_dma_memset: prepares a memset operation428423 * @device_prep_dma_interrupt: prepares an end of chain interrupt operation429424 * @device_prep_slave_sg: prepares a slave dma operation425425+ * @device_prep_dma_cyclic: prepare a cyclic dma operation suitable for audio.426426+ * The function takes a buffer of size buf_len. The callback function will427427+ * be called after period_len bytes have been transferred.430428 * @device_control: manipulate all pending operations on a channel, returns431429 * zero or error code432430 * @device_tx_status: poll for transaction completion, the optional···481473 unsigned long flags);482474 struct dma_async_tx_descriptor *(*device_prep_dma_interrupt)(483475 struct dma_chan *chan, unsigned long flags);476476+ struct dma_async_tx_descriptor *(*device_prep_dma_sg)(477477+ struct dma_chan *chan,478478+ struct scatterlist *dst_sg, unsigned int dst_nents,479479+ struct scatterlist *src_sg, unsigned int src_nents,480480+ unsigned long flags);484481485482 struct dma_async_tx_descriptor *(*device_prep_slave_sg)(486483 struct dma_chan *chan, struct scatterlist *sgl,487484 unsigned int sg_len, enum dma_data_direction direction,488485 unsigned long flags);486486+ struct dma_async_tx_descriptor *(*device_prep_dma_cyclic)(487487+ struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,488488+ size_t period_len, enum dma_data_direction direction);489489 int (*device_control)(struct dma_chan *chan, enum dma_ctrl_cmd cmd,490490 unsigned long arg);491491···502486 struct dma_tx_state *txstate);503487 void (*device_issue_pending)(struct dma_chan *chan);504488};489489+490490+static inline int dmaengine_device_control(struct dma_chan *chan,491491+ enum dma_ctrl_cmd cmd,492492+ unsigned long arg)493493+{494494+ return chan->device->device_control(chan, cmd, arg);495495+}496496+497497+static inline int dmaengine_slave_config(struct dma_chan *chan,498498+ struct dma_slave_config *config)499499+{500500+ return dmaengine_device_control(chan, DMA_SLAVE_CONFIG,501501+ (unsigned long)config);502502+}503503+504504+static inline int dmaengine_terminate_all(struct dma_chan *chan)505505+{506506+ return dmaengine_device_control(chan, DMA_TERMINATE_ALL, 0);507507+}508508+509509+static inline int dmaengine_pause(struct dma_chan *chan)510510+{511511+ return dmaengine_device_control(chan, DMA_PAUSE, 0);512512+}513513+514514+static inline int dmaengine_resume(struct dma_chan *chan)515515+{516516+ return dmaengine_device_control(chan, DMA_RESUME, 0);517517+}518518+519519+static inline int dmaengine_submit(struct dma_async_tx_descriptor *desc)520520+{521521+ return desc->tx_submit(desc);522522+}505523506524static inline bool dmaengine_check_align(u8 align, size_t off1, size_t off2, size_t len)507525{···598548 return (dma->max_pq & DMA_HAS_PQ_CONTINUE) == DMA_HAS_PQ_CONTINUE;599549}600550601601-static unsigned short dma_dev_to_maxpq(struct dma_device *dma)551551+static inline unsigned short dma_dev_to_maxpq(struct dma_device *dma)602552{603553 return dma->max_pq & ~DMA_HAS_PQ_CONTINUE;604554}
+3-13
include/linux/intel_mid_dma.h
···27272828#include <linux/dmaengine.h>29293030-/*DMA transaction width, src and dstn width would be same3131-The DMA length must be width aligned,3232-for 32 bit width the length must be 32 bit (4bytes) aligned only*/3333-enum intel_mid_dma_width {3434- LNW_DMA_WIDTH_8BIT = 0x0,3535- LNW_DMA_WIDTH_16BIT = 0x1,3636- LNW_DMA_WIDTH_32BIT = 0x2,3737-};3030+#define DMA_PREP_CIRCULAR_LIST (1 << 10)38313932/*DMA mode configurations*/4033enum intel_mid_dma_mode {···6269 * @cfg_mode: DMA data transfer mode (per-per/mem-per/mem-mem)6370 * @src_msize: Source DMA burst size6471 * @dst_msize: Dst DMA burst size7272+ * @per_addr: Periphral address6573 * @device_instance: DMA peripheral device instance, we can have multiple6674 * peripheral device connected to single DMAC6775 */6876struct intel_mid_dma_slave {6969- enum dma_data_direction dirn;7070- enum intel_mid_dma_width src_width; /*width of DMA src txn*/7171- enum intel_mid_dma_width dst_width; /*width of DMA dst txn*/7277 enum intel_mid_dma_hs_mode hs_mode; /*handshaking*/7378 enum intel_mid_dma_mode cfg_mode; /*mode configuration*/7474- enum intel_mid_dma_msize src_msize; /*size if src burst*/7575- enum intel_mid_dma_msize dst_msize; /*size of dst burst*/7679 unsigned int device_instance; /*0, 1 for periphral instance*/8080+ struct dma_slave_config dma_slave;7781};78827983#endif /*__INTEL_MID_DMA_H__*/