···610610 help611611 Enable support for the on-chip DMA controller on Spreadtrum platform.612612613613-config S3C24XX_DMAC614614- bool "Samsung S3C24XX DMA support"615615- depends on ARCH_S3C24XX || COMPILE_TEST616616- select DMA_ENGINE617617- select DMA_VIRTUAL_CHANNELS618618- help619619- Support for the Samsung S3C24XX DMA controller driver. The620620- DMA controller is having multiple DMA channels which can be621621- configured for different peripherals like audio, UART, SPI.622622- The DMA controller can transfer data from memory to peripheral,623623- periphal to memory, periphal to periphal and memory to memory.624624-625613config TXX9_DMAC626614 tristate "Toshiba TXx9 SoC DMA support"627615 depends on MACH_TX49XX
···11-// SPDX-License-Identifier: GPL-2.0-or-later22-/*33- * S3C24XX DMA handling44- *55- * Copyright (c) 2013 Heiko Stuebner <heiko@sntech.de>66- *77- * based on amba-pl08x.c88- *99- * Copyright (c) 2006 ARM Ltd.1010- * Copyright (c) 2010 ST-Ericsson SA1111- *1212- * Author: Peter Pearse <peter.pearse@arm.com>1313- * Author: Linus Walleij <linus.walleij@stericsson.com>1414- *1515- * The DMA controllers in S3C24XX SoCs have a varying number of DMA signals1616- * that can be routed to any of the 4 to 8 hardware-channels.1717- *1818- * Therefore on these DMA controllers the number of channels1919- * and the number of incoming DMA signals are two totally different things.2020- * It is usually not possible to theoretically handle all physical signals,2121- * so a multiplexing scheme with possible denial of use is necessary.2222- *2323- * Open items:2424- * - bursts2525- */2626-2727-#include <linux/platform_device.h>2828-#include <linux/types.h>2929-#include <linux/dmaengine.h>3030-#include <linux/dma-mapping.h>3131-#include <linux/interrupt.h>3232-#include <linux/clk.h>3333-#include <linux/module.h>3434-#include <linux/mod_devicetable.h>3535-#include <linux/slab.h>3636-#include <linux/platform_data/dma-s3c24xx.h>3737-3838-#include "dmaengine.h"3939-#include "virt-dma.h"4040-4141-#define MAX_DMA_CHANNELS 84242-4343-#define S3C24XX_DISRC 0x004444-#define S3C24XX_DISRCC 0x044545-#define S3C24XX_DISRCC_INC_INCREMENT 04646-#define S3C24XX_DISRCC_INC_FIXED BIT(0)4747-#define S3C24XX_DISRCC_LOC_AHB 04848-#define S3C24XX_DISRCC_LOC_APB BIT(1)4949-5050-#define S3C24XX_DIDST 0x085151-#define S3C24XX_DIDSTC 0x0c5252-#define S3C24XX_DIDSTC_INC_INCREMENT 05353-#define S3C24XX_DIDSTC_INC_FIXED BIT(0)5454-#define S3C24XX_DIDSTC_LOC_AHB 05555-#define S3C24XX_DIDSTC_LOC_APB BIT(1)5656-#define S3C24XX_DIDSTC_INT_TC0 05757-#define S3C24XX_DIDSTC_INT_RELOAD BIT(2)5858-5959-#define S3C24XX_DCON 0x106060-6161-#define S3C24XX_DCON_TC_MASK 0xfffff6262-#define S3C24XX_DCON_DSZ_BYTE (0 << 20)6363-#define S3C24XX_DCON_DSZ_HALFWORD (1 << 20)6464-#define S3C24XX_DCON_DSZ_WORD (2 << 20)6565-#define S3C24XX_DCON_DSZ_MASK (3 << 20)6666-#define S3C24XX_DCON_DSZ_SHIFT 206767-#define S3C24XX_DCON_AUTORELOAD 06868-#define S3C24XX_DCON_NORELOAD BIT(22)6969-#define S3C24XX_DCON_HWTRIG BIT(23)7070-#define S3C24XX_DCON_HWSRC_SHIFT 247171-#define S3C24XX_DCON_SERV_SINGLE 07272-#define S3C24XX_DCON_SERV_WHOLE BIT(27)7373-#define S3C24XX_DCON_TSZ_UNIT 07474-#define S3C24XX_DCON_TSZ_BURST4 BIT(28)7575-#define S3C24XX_DCON_INT BIT(29)7676-#define S3C24XX_DCON_SYNC_PCLK 07777-#define S3C24XX_DCON_SYNC_HCLK BIT(30)7878-#define S3C24XX_DCON_DEMAND 07979-#define S3C24XX_DCON_HANDSHAKE BIT(31)8080-8181-#define S3C24XX_DSTAT 0x148282-#define S3C24XX_DSTAT_STAT_BUSY BIT(20)8383-#define S3C24XX_DSTAT_CURRTC_MASK 0xfffff8484-8585-#define S3C24XX_DMASKTRIG 0x208686-#define S3C24XX_DMASKTRIG_SWTRIG BIT(0)8787-#define S3C24XX_DMASKTRIG_ON BIT(1)8888-#define S3C24XX_DMASKTRIG_STOP BIT(2)8989-9090-#define S3C24XX_DMAREQSEL 0x249191-#define S3C24XX_DMAREQSEL_HW BIT(0)9292-9393-/*9494- * S3C2410, S3C2440 and S3C2442 SoCs cannot select any physical channel9595- * for a DMA source. Instead only specific channels are valid.9696- * All of these SoCs have 4 physical channels and the number of request9797- * source bits is 3. Additionally we also need 1 bit to mark the channel9898- * as valid.9999- * Therefore we separate the chansel element of the channel data into 4100100- * parts of 4 bits each, to hold the information if the channel is valid101101- * and the hw request source to use.102102- *103103- * Example:104104- * SDI is valid on channels 0, 2 and 3 - with varying hw request sources.105105- * For it the chansel field would look like106106- *107107- * ((BIT(3) | 1) << 3 * 4) | // channel 3, with request source 1108108- * ((BIT(3) | 2) << 2 * 4) | // channel 2, with request source 2109109- * ((BIT(3) | 2) << 0 * 4) // channel 0, with request source 2110110- */111111-#define S3C24XX_CHANSEL_WIDTH 4112112-#define S3C24XX_CHANSEL_VALID BIT(3)113113-#define S3C24XX_CHANSEL_REQ_MASK 7114114-115115-/*116116- * struct soc_data - vendor-specific config parameters for individual SoCs117117- * @stride: spacing between the registers of each channel118118- * @has_reqsel: does the controller use the newer requestselection mechanism119119- * @has_clocks: are controllable dma-clocks present120120- */121121-struct soc_data {122122- int stride;123123- bool has_reqsel;124124- bool has_clocks;125125-};126126-127127-/*128128- * enum s3c24xx_dma_chan_state - holds the virtual channel states129129- * @S3C24XX_DMA_CHAN_IDLE: the channel is idle130130- * @S3C24XX_DMA_CHAN_RUNNING: the channel has allocated a physical transport131131- * channel and is running a transfer on it132132- * @S3C24XX_DMA_CHAN_WAITING: the channel is waiting for a physical transport133133- * channel to become available (only pertains to memcpy channels)134134- */135135-enum s3c24xx_dma_chan_state {136136- S3C24XX_DMA_CHAN_IDLE,137137- S3C24XX_DMA_CHAN_RUNNING,138138- S3C24XX_DMA_CHAN_WAITING,139139-};140140-141141-/*142142- * struct s3c24xx_sg - structure containing data per sg143143- * @src_addr: src address of sg144144- * @dst_addr: dst address of sg145145- * @len: transfer len in bytes146146- * @node: node for txd's dsg_list147147- */148148-struct s3c24xx_sg {149149- dma_addr_t src_addr;150150- dma_addr_t dst_addr;151151- size_t len;152152- struct list_head node;153153-};154154-155155-/*156156- * struct s3c24xx_txd - wrapper for struct dma_async_tx_descriptor157157- * @vd: virtual DMA descriptor158158- * @dsg_list: list of children sg's159159- * @at: sg currently being transfered160160- * @width: transfer width161161- * @disrcc: value for source control register162162- * @didstc: value for destination control register163163- * @dcon: base value for dcon register164164- * @cyclic: indicate cyclic transfer165165- */166166-struct s3c24xx_txd {167167- struct virt_dma_desc vd;168168- struct list_head dsg_list;169169- struct list_head *at;170170- u8 width;171171- u32 disrcc;172172- u32 didstc;173173- u32 dcon;174174- bool cyclic;175175-};176176-177177-struct s3c24xx_dma_chan;178178-179179-/*180180- * struct s3c24xx_dma_phy - holder for the physical channels181181- * @id: physical index to this channel182182- * @valid: does the channel have all required elements183183- * @base: virtual memory base (remapped) for the this channel184184- * @irq: interrupt for this channel185185- * @clk: clock for this channel186186- * @lock: a lock to use when altering an instance of this struct187187- * @serving: virtual channel currently being served by this physicalchannel188188- * @host: a pointer to the host (internal use)189189- */190190-struct s3c24xx_dma_phy {191191- unsigned int id;192192- bool valid;193193- void __iomem *base;194194- int irq;195195- struct clk *clk;196196- spinlock_t lock;197197- struct s3c24xx_dma_chan *serving;198198- struct s3c24xx_dma_engine *host;199199-};200200-201201-/*202202- * struct s3c24xx_dma_chan - this structure wraps a DMA ENGINE channel203203- * @id: the id of the channel204204- * @name: name of the channel205205- * @vc: wrapped virtual channel206206- * @phy: the physical channel utilized by this channel, if there is one207207- * @runtime_addr: address for RX/TX according to the runtime config208208- * @at: active transaction on this channel209209- * @lock: a lock for this channel data210210- * @host: a pointer to the host (internal use)211211- * @state: whether the channel is idle, running etc212212- * @slave: whether this channel is a device (slave) or for memcpy213213- */214214-struct s3c24xx_dma_chan {215215- int id;216216- const char *name;217217- struct virt_dma_chan vc;218218- struct s3c24xx_dma_phy *phy;219219- struct dma_slave_config cfg;220220- struct s3c24xx_txd *at;221221- struct s3c24xx_dma_engine *host;222222- enum s3c24xx_dma_chan_state state;223223- bool slave;224224-};225225-226226-/*227227- * struct s3c24xx_dma_engine - the local state holder for the S3C24XX228228- * @pdev: the corresponding platform device229229- * @pdata: platform data passed in from the platform/machine230230- * @base: virtual memory base (remapped)231231- * @slave: slave engine for this instance232232- * @memcpy: memcpy engine for this instance233233- * @phy_chans: array of data for the physical channels234234- */235235-struct s3c24xx_dma_engine {236236- struct platform_device *pdev;237237- const struct s3c24xx_dma_platdata *pdata;238238- struct soc_data *sdata;239239- void __iomem *base;240240- struct dma_device slave;241241- struct dma_device memcpy;242242- struct s3c24xx_dma_phy *phy_chans;243243-};244244-245245-/*246246- * Physical channel handling247247- */248248-249249-/*250250- * Check whether a certain channel is busy or not.251251- */252252-static int s3c24xx_dma_phy_busy(struct s3c24xx_dma_phy *phy)253253-{254254- unsigned int val = readl(phy->base + S3C24XX_DSTAT);255255- return val & S3C24XX_DSTAT_STAT_BUSY;256256-}257257-258258-static bool s3c24xx_dma_phy_valid(struct s3c24xx_dma_chan *s3cchan,259259- struct s3c24xx_dma_phy *phy)260260-{261261- struct s3c24xx_dma_engine *s3cdma = s3cchan->host;262262- const struct s3c24xx_dma_platdata *pdata = s3cdma->pdata;263263- struct s3c24xx_dma_channel *cdata = &pdata->channels[s3cchan->id];264264- int phyvalid;265265-266266- /* every phy is valid for memcopy channels */267267- if (!s3cchan->slave)268268- return true;269269-270270- /* On newer variants all phys can be used for all virtual channels */271271- if (s3cdma->sdata->has_reqsel)272272- return true;273273-274274- phyvalid = (cdata->chansel >> (phy->id * S3C24XX_CHANSEL_WIDTH));275275- return (phyvalid & S3C24XX_CHANSEL_VALID) ? true : false;276276-}277277-278278-/*279279- * Allocate a physical channel for a virtual channel280280- *281281- * Try to locate a physical channel to be used for this transfer. If all282282- * are taken return NULL and the requester will have to cope by using283283- * some fallback PIO mode or retrying later.284284- */285285-static286286-struct s3c24xx_dma_phy *s3c24xx_dma_get_phy(struct s3c24xx_dma_chan *s3cchan)287287-{288288- struct s3c24xx_dma_engine *s3cdma = s3cchan->host;289289- struct s3c24xx_dma_phy *phy = NULL;290290- unsigned long flags;291291- int i;292292- int ret;293293-294294- for (i = 0; i < s3cdma->pdata->num_phy_channels; i++) {295295- phy = &s3cdma->phy_chans[i];296296-297297- if (!phy->valid)298298- continue;299299-300300- if (!s3c24xx_dma_phy_valid(s3cchan, phy))301301- continue;302302-303303- spin_lock_irqsave(&phy->lock, flags);304304-305305- if (!phy->serving) {306306- phy->serving = s3cchan;307307- spin_unlock_irqrestore(&phy->lock, flags);308308- break;309309- }310310-311311- spin_unlock_irqrestore(&phy->lock, flags);312312- }313313-314314- /* No physical channel available, cope with it */315315- if (i == s3cdma->pdata->num_phy_channels) {316316- dev_warn(&s3cdma->pdev->dev, "no phy channel available\n");317317- return NULL;318318- }319319-320320- /* start the phy clock */321321- if (s3cdma->sdata->has_clocks) {322322- ret = clk_enable(phy->clk);323323- if (ret) {324324- dev_err(&s3cdma->pdev->dev, "could not enable clock for channel %d, err %d\n",325325- phy->id, ret);326326- phy->serving = NULL;327327- return NULL;328328- }329329- }330330-331331- return phy;332332-}333333-334334-/*335335- * Mark the physical channel as free.336336- *337337- * This drops the link between the physical and virtual channel.338338- */339339-static inline void s3c24xx_dma_put_phy(struct s3c24xx_dma_phy *phy)340340-{341341- struct s3c24xx_dma_engine *s3cdma = phy->host;342342-343343- if (s3cdma->sdata->has_clocks)344344- clk_disable(phy->clk);345345-346346- phy->serving = NULL;347347-}348348-349349-/*350350- * Stops the channel by writing the stop bit.351351- * This should not be used for an on-going transfer, but as a method of352352- * shutting down a channel (eg, when it's no longer used) or terminating a353353- * transfer.354354- */355355-static void s3c24xx_dma_terminate_phy(struct s3c24xx_dma_phy *phy)356356-{357357- writel(S3C24XX_DMASKTRIG_STOP, phy->base + S3C24XX_DMASKTRIG);358358-}359359-360360-/*361361- * Virtual channel handling362362- */363363-364364-static inline365365-struct s3c24xx_dma_chan *to_s3c24xx_dma_chan(struct dma_chan *chan)366366-{367367- return container_of(chan, struct s3c24xx_dma_chan, vc.chan);368368-}369369-370370-static u32 s3c24xx_dma_getbytes_chan(struct s3c24xx_dma_chan *s3cchan)371371-{372372- struct s3c24xx_dma_phy *phy = s3cchan->phy;373373- struct s3c24xx_txd *txd = s3cchan->at;374374- u32 tc = readl(phy->base + S3C24XX_DSTAT) & S3C24XX_DSTAT_CURRTC_MASK;375375-376376- return tc * txd->width;377377-}378378-379379-static int s3c24xx_dma_set_runtime_config(struct dma_chan *chan,380380- struct dma_slave_config *config)381381-{382382- struct s3c24xx_dma_chan *s3cchan = to_s3c24xx_dma_chan(chan);383383- unsigned long flags;384384- int ret = 0;385385-386386- /* Reject definitely invalid configurations */387387- if (config->src_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES ||388388- config->dst_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES)389389- return -EINVAL;390390-391391- spin_lock_irqsave(&s3cchan->vc.lock, flags);392392-393393- if (!s3cchan->slave) {394394- ret = -EINVAL;395395- goto out;396396- }397397-398398- s3cchan->cfg = *config;399399-400400-out:401401- spin_unlock_irqrestore(&s3cchan->vc.lock, flags);402402- return ret;403403-}404404-405405-/*406406- * Transfer handling407407- */408408-409409-static inline410410-struct s3c24xx_txd *to_s3c24xx_txd(struct dma_async_tx_descriptor *tx)411411-{412412- return container_of(tx, struct s3c24xx_txd, vd.tx);413413-}414414-415415-static struct s3c24xx_txd *s3c24xx_dma_get_txd(void)416416-{417417- struct s3c24xx_txd *txd = kzalloc(sizeof(*txd), GFP_NOWAIT);418418-419419- if (txd) {420420- INIT_LIST_HEAD(&txd->dsg_list);421421- txd->dcon = S3C24XX_DCON_INT | S3C24XX_DCON_NORELOAD;422422- }423423-424424- return txd;425425-}426426-427427-static void s3c24xx_dma_free_txd(struct s3c24xx_txd *txd)428428-{429429- struct s3c24xx_sg *dsg, *_dsg;430430-431431- list_for_each_entry_safe(dsg, _dsg, &txd->dsg_list, node) {432432- list_del(&dsg->node);433433- kfree(dsg);434434- }435435-436436- kfree(txd);437437-}438438-439439-static void s3c24xx_dma_start_next_sg(struct s3c24xx_dma_chan *s3cchan,440440- struct s3c24xx_txd *txd)441441-{442442- struct s3c24xx_dma_engine *s3cdma = s3cchan->host;443443- struct s3c24xx_dma_phy *phy = s3cchan->phy;444444- const struct s3c24xx_dma_platdata *pdata = s3cdma->pdata;445445- struct s3c24xx_sg *dsg = list_entry(txd->at, struct s3c24xx_sg, node);446446- u32 dcon = txd->dcon;447447- u32 val;448448-449449- /* transfer-size and -count from len and width */450450- switch (txd->width) {451451- case 1:452452- dcon |= S3C24XX_DCON_DSZ_BYTE | dsg->len;453453- break;454454- case 2:455455- dcon |= S3C24XX_DCON_DSZ_HALFWORD | (dsg->len / 2);456456- break;457457- case 4:458458- dcon |= S3C24XX_DCON_DSZ_WORD | (dsg->len / 4);459459- break;460460- }461461-462462- if (s3cchan->slave) {463463- struct s3c24xx_dma_channel *cdata =464464- &pdata->channels[s3cchan->id];465465-466466- if (s3cdma->sdata->has_reqsel) {467467- writel_relaxed((cdata->chansel << 1) |468468- S3C24XX_DMAREQSEL_HW,469469- phy->base + S3C24XX_DMAREQSEL);470470- } else {471471- int csel = cdata->chansel >> (phy->id *472472- S3C24XX_CHANSEL_WIDTH);473473-474474- csel &= S3C24XX_CHANSEL_REQ_MASK;475475- dcon |= csel << S3C24XX_DCON_HWSRC_SHIFT;476476- dcon |= S3C24XX_DCON_HWTRIG;477477- }478478- } else {479479- if (s3cdma->sdata->has_reqsel)480480- writel_relaxed(0, phy->base + S3C24XX_DMAREQSEL);481481- }482482-483483- writel_relaxed(dsg->src_addr, phy->base + S3C24XX_DISRC);484484- writel_relaxed(txd->disrcc, phy->base + S3C24XX_DISRCC);485485- writel_relaxed(dsg->dst_addr, phy->base + S3C24XX_DIDST);486486- writel_relaxed(txd->didstc, phy->base + S3C24XX_DIDSTC);487487- writel_relaxed(dcon, phy->base + S3C24XX_DCON);488488-489489- val = readl_relaxed(phy->base + S3C24XX_DMASKTRIG);490490- val &= ~S3C24XX_DMASKTRIG_STOP;491491- val |= S3C24XX_DMASKTRIG_ON;492492-493493- /* trigger the dma operation for memcpy transfers */494494- if (!s3cchan->slave)495495- val |= S3C24XX_DMASKTRIG_SWTRIG;496496-497497- writel(val, phy->base + S3C24XX_DMASKTRIG);498498-}499499-500500-/*501501- * Set the initial DMA register values and start first sg.502502- */503503-static void s3c24xx_dma_start_next_txd(struct s3c24xx_dma_chan *s3cchan)504504-{505505- struct s3c24xx_dma_phy *phy = s3cchan->phy;506506- struct virt_dma_desc *vd = vchan_next_desc(&s3cchan->vc);507507- struct s3c24xx_txd *txd = to_s3c24xx_txd(&vd->tx);508508-509509- list_del(&txd->vd.node);510510-511511- s3cchan->at = txd;512512-513513- /* Wait for channel inactive */514514- while (s3c24xx_dma_phy_busy(phy))515515- cpu_relax();516516-517517- /* point to the first element of the sg list */518518- txd->at = txd->dsg_list.next;519519- s3c24xx_dma_start_next_sg(s3cchan, txd);520520-}521521-522522-/*523523- * Try to allocate a physical channel. When successful, assign it to524524- * this virtual channel, and initiate the next descriptor. The525525- * virtual channel lock must be held at this point.526526- */527527-static void s3c24xx_dma_phy_alloc_and_start(struct s3c24xx_dma_chan *s3cchan)528528-{529529- struct s3c24xx_dma_engine *s3cdma = s3cchan->host;530530- struct s3c24xx_dma_phy *phy;531531-532532- phy = s3c24xx_dma_get_phy(s3cchan);533533- if (!phy) {534534- dev_dbg(&s3cdma->pdev->dev, "no physical channel available for xfer on %s\n",535535- s3cchan->name);536536- s3cchan->state = S3C24XX_DMA_CHAN_WAITING;537537- return;538538- }539539-540540- dev_dbg(&s3cdma->pdev->dev, "allocated physical channel %d for xfer on %s\n",541541- phy->id, s3cchan->name);542542-543543- s3cchan->phy = phy;544544- s3cchan->state = S3C24XX_DMA_CHAN_RUNNING;545545-546546- s3c24xx_dma_start_next_txd(s3cchan);547547-}548548-549549-static void s3c24xx_dma_phy_reassign_start(struct s3c24xx_dma_phy *phy,550550- struct s3c24xx_dma_chan *s3cchan)551551-{552552- struct s3c24xx_dma_engine *s3cdma = s3cchan->host;553553-554554- dev_dbg(&s3cdma->pdev->dev, "reassigned physical channel %d for xfer on %s\n",555555- phy->id, s3cchan->name);556556-557557- /*558558- * We do this without taking the lock; we're really only concerned559559- * about whether this pointer is NULL or not, and we're guaranteed560560- * that this will only be called when it _already_ is non-NULL.561561- */562562- phy->serving = s3cchan;563563- s3cchan->phy = phy;564564- s3cchan->state = S3C24XX_DMA_CHAN_RUNNING;565565- s3c24xx_dma_start_next_txd(s3cchan);566566-}567567-568568-/*569569- * Free a physical DMA channel, potentially reallocating it to another570570- * virtual channel if we have any pending.571571- */572572-static void s3c24xx_dma_phy_free(struct s3c24xx_dma_chan *s3cchan)573573-{574574- struct s3c24xx_dma_engine *s3cdma = s3cchan->host;575575- struct s3c24xx_dma_chan *p, *next;576576-577577-retry:578578- next = NULL;579579-580580- /* Find a waiting virtual channel for the next transfer. */581581- list_for_each_entry(p, &s3cdma->memcpy.channels, vc.chan.device_node)582582- if (p->state == S3C24XX_DMA_CHAN_WAITING) {583583- next = p;584584- break;585585- }586586-587587- if (!next) {588588- list_for_each_entry(p, &s3cdma->slave.channels,589589- vc.chan.device_node)590590- if (p->state == S3C24XX_DMA_CHAN_WAITING &&591591- s3c24xx_dma_phy_valid(p, s3cchan->phy)) {592592- next = p;593593- break;594594- }595595- }596596-597597- /* Ensure that the physical channel is stopped */598598- s3c24xx_dma_terminate_phy(s3cchan->phy);599599-600600- if (next) {601601- bool success;602602-603603- /*604604- * Eww. We know this isn't going to deadlock605605- * but lockdep probably doesn't.606606- */607607- spin_lock(&next->vc.lock);608608- /* Re-check the state now that we have the lock */609609- success = next->state == S3C24XX_DMA_CHAN_WAITING;610610- if (success)611611- s3c24xx_dma_phy_reassign_start(s3cchan->phy, next);612612- spin_unlock(&next->vc.lock);613613-614614- /* If the state changed, try to find another channel */615615- if (!success)616616- goto retry;617617- } else {618618- /* No more jobs, so free up the physical channel */619619- s3c24xx_dma_put_phy(s3cchan->phy);620620- }621621-622622- s3cchan->phy = NULL;623623- s3cchan->state = S3C24XX_DMA_CHAN_IDLE;624624-}625625-626626-static void s3c24xx_dma_desc_free(struct virt_dma_desc *vd)627627-{628628- struct s3c24xx_txd *txd = to_s3c24xx_txd(&vd->tx);629629- struct s3c24xx_dma_chan *s3cchan = to_s3c24xx_dma_chan(vd->tx.chan);630630-631631- if (!s3cchan->slave)632632- dma_descriptor_unmap(&vd->tx);633633-634634- s3c24xx_dma_free_txd(txd);635635-}636636-637637-static irqreturn_t s3c24xx_dma_irq(int irq, void *data)638638-{639639- struct s3c24xx_dma_phy *phy = data;640640- struct s3c24xx_dma_chan *s3cchan = phy->serving;641641- struct s3c24xx_txd *txd;642642-643643- dev_dbg(&phy->host->pdev->dev, "interrupt on channel %d\n", phy->id);644644-645645- /*646646- * Interrupts happen to notify the completion of a transfer and the647647- * channel should have moved into its stop state already on its own.648648- * Therefore interrupts on channels not bound to a virtual channel649649- * should never happen. Nevertheless send a terminate command to the650650- * channel if the unlikely case happens.651651- */652652- if (unlikely(!s3cchan)) {653653- dev_err(&phy->host->pdev->dev, "interrupt on unused channel %d\n",654654- phy->id);655655-656656- s3c24xx_dma_terminate_phy(phy);657657-658658- return IRQ_HANDLED;659659- }660660-661661- spin_lock(&s3cchan->vc.lock);662662- txd = s3cchan->at;663663- if (txd) {664664- /* when more sg's are in this txd, start the next one */665665- if (!list_is_last(txd->at, &txd->dsg_list)) {666666- txd->at = txd->at->next;667667- if (txd->cyclic)668668- vchan_cyclic_callback(&txd->vd);669669- s3c24xx_dma_start_next_sg(s3cchan, txd);670670- } else if (!txd->cyclic) {671671- s3cchan->at = NULL;672672- vchan_cookie_complete(&txd->vd);673673-674674- /*675675- * And start the next descriptor (if any),676676- * otherwise free this channel.677677- */678678- if (vchan_next_desc(&s3cchan->vc))679679- s3c24xx_dma_start_next_txd(s3cchan);680680- else681681- s3c24xx_dma_phy_free(s3cchan);682682- } else {683683- vchan_cyclic_callback(&txd->vd);684684-685685- /* Cyclic: reset at beginning */686686- txd->at = txd->dsg_list.next;687687- s3c24xx_dma_start_next_sg(s3cchan, txd);688688- }689689- }690690- spin_unlock(&s3cchan->vc.lock);691691-692692- return IRQ_HANDLED;693693-}694694-695695-/*696696- * The DMA ENGINE API697697- */698698-699699-static int s3c24xx_dma_terminate_all(struct dma_chan *chan)700700-{701701- struct s3c24xx_dma_chan *s3cchan = to_s3c24xx_dma_chan(chan);702702- struct s3c24xx_dma_engine *s3cdma = s3cchan->host;703703- LIST_HEAD(head);704704- unsigned long flags;705705- int ret;706706-707707- spin_lock_irqsave(&s3cchan->vc.lock, flags);708708-709709- if (!s3cchan->phy && !s3cchan->at) {710710- dev_err(&s3cdma->pdev->dev, "trying to terminate already stopped channel %d\n",711711- s3cchan->id);712712- ret = -EINVAL;713713- goto unlock;714714- }715715-716716- s3cchan->state = S3C24XX_DMA_CHAN_IDLE;717717-718718- /* Mark physical channel as free */719719- if (s3cchan->phy)720720- s3c24xx_dma_phy_free(s3cchan);721721-722722- /* Dequeue current job */723723- if (s3cchan->at) {724724- vchan_terminate_vdesc(&s3cchan->at->vd);725725- s3cchan->at = NULL;726726- }727727-728728- /* Dequeue jobs not yet fired as well */729729-730730- vchan_get_all_descriptors(&s3cchan->vc, &head);731731-732732- spin_unlock_irqrestore(&s3cchan->vc.lock, flags);733733-734734- vchan_dma_desc_free_list(&s3cchan->vc, &head);735735-736736- return 0;737737-738738-unlock:739739- spin_unlock_irqrestore(&s3cchan->vc.lock, flags);740740-741741- return ret;742742-}743743-744744-static void s3c24xx_dma_synchronize(struct dma_chan *chan)745745-{746746- struct s3c24xx_dma_chan *s3cchan = to_s3c24xx_dma_chan(chan);747747-748748- vchan_synchronize(&s3cchan->vc);749749-}750750-751751-static void s3c24xx_dma_free_chan_resources(struct dma_chan *chan)752752-{753753- /* Ensure all queued descriptors are freed */754754- vchan_free_chan_resources(to_virt_chan(chan));755755-}756756-757757-static enum dma_status s3c24xx_dma_tx_status(struct dma_chan *chan,758758- dma_cookie_t cookie, struct dma_tx_state *txstate)759759-{760760- struct s3c24xx_dma_chan *s3cchan = to_s3c24xx_dma_chan(chan);761761- struct s3c24xx_txd *txd;762762- struct s3c24xx_sg *dsg;763763- struct virt_dma_desc *vd;764764- unsigned long flags;765765- enum dma_status ret;766766- size_t bytes = 0;767767-768768- spin_lock_irqsave(&s3cchan->vc.lock, flags);769769- ret = dma_cookie_status(chan, cookie, txstate);770770-771771- /*772772- * There's no point calculating the residue if there's773773- * no txstate to store the value.774774- */775775- if (ret == DMA_COMPLETE || !txstate) {776776- spin_unlock_irqrestore(&s3cchan->vc.lock, flags);777777- return ret;778778- }779779-780780- vd = vchan_find_desc(&s3cchan->vc, cookie);781781- if (vd) {782782- /* On the issued list, so hasn't been processed yet */783783- txd = to_s3c24xx_txd(&vd->tx);784784-785785- list_for_each_entry(dsg, &txd->dsg_list, node)786786- bytes += dsg->len;787787- } else {788788- /*789789- * Currently running, so sum over the pending sg's and790790- * the currently active one.791791- */792792- txd = s3cchan->at;793793-794794- dsg = list_entry(txd->at, struct s3c24xx_sg, node);795795- list_for_each_entry_from(dsg, &txd->dsg_list, node)796796- bytes += dsg->len;797797-798798- bytes += s3c24xx_dma_getbytes_chan(s3cchan);799799- }800800- spin_unlock_irqrestore(&s3cchan->vc.lock, flags);801801-802802- /*803803- * This cookie not complete yet804804- * Get number of bytes left in the active transactions and queue805805- */806806- dma_set_residue(txstate, bytes);807807-808808- /* Whether waiting or running, we're in progress */809809- return ret;810810-}811811-812812-/*813813- * Initialize a descriptor to be used by memcpy submit814814- */815815-static struct dma_async_tx_descriptor *s3c24xx_dma_prep_memcpy(816816- struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,817817- size_t len, unsigned long flags)818818-{819819- struct s3c24xx_dma_chan *s3cchan = to_s3c24xx_dma_chan(chan);820820- struct s3c24xx_dma_engine *s3cdma = s3cchan->host;821821- struct s3c24xx_txd *txd;822822- struct s3c24xx_sg *dsg;823823- int src_mod, dest_mod;824824-825825- dev_dbg(&s3cdma->pdev->dev, "prepare memcpy of %zu bytes from %s\n",826826- len, s3cchan->name);827827-828828- if ((len & S3C24XX_DCON_TC_MASK) != len) {829829- dev_err(&s3cdma->pdev->dev, "memcpy size %zu to large\n", len);830830- return NULL;831831- }832832-833833- txd = s3c24xx_dma_get_txd();834834- if (!txd)835835- return NULL;836836-837837- dsg = kzalloc(sizeof(*dsg), GFP_NOWAIT);838838- if (!dsg) {839839- s3c24xx_dma_free_txd(txd);840840- return NULL;841841- }842842- list_add_tail(&dsg->node, &txd->dsg_list);843843-844844- dsg->src_addr = src;845845- dsg->dst_addr = dest;846846- dsg->len = len;847847-848848- /*849849- * Determine a suitable transfer width.850850- * The DMA controller cannot fetch/store information which is not851851- * naturally aligned on the bus, i.e., a 4 byte fetch must start at852852- * an address divisible by 4 - more generally addr % width must be 0.853853- */854854- src_mod = src % 4;855855- dest_mod = dest % 4;856856- switch (len % 4) {857857- case 0:858858- txd->width = (src_mod == 0 && dest_mod == 0) ? 4 : 1;859859- break;860860- case 2:861861- txd->width = ((src_mod == 2 || src_mod == 0) &&862862- (dest_mod == 2 || dest_mod == 0)) ? 2 : 1;863863- break;864864- default:865865- txd->width = 1;866866- break;867867- }868868-869869- txd->disrcc = S3C24XX_DISRCC_LOC_AHB | S3C24XX_DISRCC_INC_INCREMENT;870870- txd->didstc = S3C24XX_DIDSTC_LOC_AHB | S3C24XX_DIDSTC_INC_INCREMENT;871871- txd->dcon |= S3C24XX_DCON_DEMAND | S3C24XX_DCON_SYNC_HCLK |872872- S3C24XX_DCON_SERV_WHOLE;873873-874874- return vchan_tx_prep(&s3cchan->vc, &txd->vd, flags);875875-}876876-877877-static struct dma_async_tx_descriptor *s3c24xx_dma_prep_dma_cyclic(878878- struct dma_chan *chan, dma_addr_t addr, size_t size, size_t period,879879- enum dma_transfer_direction direction, unsigned long flags)880880-{881881- struct s3c24xx_dma_chan *s3cchan = to_s3c24xx_dma_chan(chan);882882- struct s3c24xx_dma_engine *s3cdma = s3cchan->host;883883- const struct s3c24xx_dma_platdata *pdata = s3cdma->pdata;884884- struct s3c24xx_dma_channel *cdata = &pdata->channels[s3cchan->id];885885- struct s3c24xx_txd *txd;886886- struct s3c24xx_sg *dsg;887887- unsigned sg_len;888888- dma_addr_t slave_addr;889889- u32 hwcfg = 0;890890- int i;891891-892892- dev_dbg(&s3cdma->pdev->dev,893893- "prepare cyclic transaction of %zu bytes with period %zu from %s\n",894894- size, period, s3cchan->name);895895-896896- if (!is_slave_direction(direction)) {897897- dev_err(&s3cdma->pdev->dev,898898- "direction %d unsupported\n", direction);899899- return NULL;900900- }901901-902902- txd = s3c24xx_dma_get_txd();903903- if (!txd)904904- return NULL;905905-906906- txd->cyclic = 1;907907-908908- if (cdata->handshake)909909- txd->dcon |= S3C24XX_DCON_HANDSHAKE;910910-911911- switch (cdata->bus) {912912- case S3C24XX_DMA_APB:913913- txd->dcon |= S3C24XX_DCON_SYNC_PCLK;914914- hwcfg |= S3C24XX_DISRCC_LOC_APB;915915- break;916916- case S3C24XX_DMA_AHB:917917- txd->dcon |= S3C24XX_DCON_SYNC_HCLK;918918- hwcfg |= S3C24XX_DISRCC_LOC_AHB;919919- break;920920- }921921-922922- /*923923- * Always assume our peripheral desintation is a fixed924924- * address in memory.925925- */926926- hwcfg |= S3C24XX_DISRCC_INC_FIXED;927927-928928- /*929929- * Individual dma operations are requested by the slave,930930- * so serve only single atomic operations (S3C24XX_DCON_SERV_SINGLE).931931- */932932- txd->dcon |= S3C24XX_DCON_SERV_SINGLE;933933-934934- if (direction == DMA_MEM_TO_DEV) {935935- txd->disrcc = S3C24XX_DISRCC_LOC_AHB |936936- S3C24XX_DISRCC_INC_INCREMENT;937937- txd->didstc = hwcfg;938938- slave_addr = s3cchan->cfg.dst_addr;939939- txd->width = s3cchan->cfg.dst_addr_width;940940- } else {941941- txd->disrcc = hwcfg;942942- txd->didstc = S3C24XX_DIDSTC_LOC_AHB |943943- S3C24XX_DIDSTC_INC_INCREMENT;944944- slave_addr = s3cchan->cfg.src_addr;945945- txd->width = s3cchan->cfg.src_addr_width;946946- }947947-948948- sg_len = size / period;949949-950950- for (i = 0; i < sg_len; i++) {951951- dsg = kzalloc(sizeof(*dsg), GFP_NOWAIT);952952- if (!dsg) {953953- s3c24xx_dma_free_txd(txd);954954- return NULL;955955- }956956- list_add_tail(&dsg->node, &txd->dsg_list);957957-958958- dsg->len = period;959959- /* Check last period length */960960- if (i == sg_len - 1)961961- dsg->len = size - period * i;962962- if (direction == DMA_MEM_TO_DEV) {963963- dsg->src_addr = addr + period * i;964964- dsg->dst_addr = slave_addr;965965- } else { /* DMA_DEV_TO_MEM */966966- dsg->src_addr = slave_addr;967967- dsg->dst_addr = addr + period * i;968968- }969969- }970970-971971- return vchan_tx_prep(&s3cchan->vc, &txd->vd, flags);972972-}973973-974974-static struct dma_async_tx_descriptor *s3c24xx_dma_prep_slave_sg(975975- struct dma_chan *chan, struct scatterlist *sgl,976976- unsigned int sg_len, enum dma_transfer_direction direction,977977- unsigned long flags, void *context)978978-{979979- struct s3c24xx_dma_chan *s3cchan = to_s3c24xx_dma_chan(chan);980980- struct s3c24xx_dma_engine *s3cdma = s3cchan->host;981981- const struct s3c24xx_dma_platdata *pdata = s3cdma->pdata;982982- struct s3c24xx_dma_channel *cdata = &pdata->channels[s3cchan->id];983983- struct s3c24xx_txd *txd;984984- struct s3c24xx_sg *dsg;985985- struct scatterlist *sg;986986- dma_addr_t slave_addr;987987- u32 hwcfg = 0;988988- int tmp;989989-990990- dev_dbg(&s3cdma->pdev->dev, "prepare transaction of %d bytes from %s\n",991991- sg_dma_len(sgl), s3cchan->name);992992-993993- txd = s3c24xx_dma_get_txd();994994- if (!txd)995995- return NULL;996996-997997- if (cdata->handshake)998998- txd->dcon |= S3C24XX_DCON_HANDSHAKE;999999-10001000- switch (cdata->bus) {10011001- case S3C24XX_DMA_APB:10021002- txd->dcon |= S3C24XX_DCON_SYNC_PCLK;10031003- hwcfg |= S3C24XX_DISRCC_LOC_APB;10041004- break;10051005- case S3C24XX_DMA_AHB:10061006- txd->dcon |= S3C24XX_DCON_SYNC_HCLK;10071007- hwcfg |= S3C24XX_DISRCC_LOC_AHB;10081008- break;10091009- }10101010-10111011- /*10121012- * Always assume our peripheral desintation is a fixed10131013- * address in memory.10141014- */10151015- hwcfg |= S3C24XX_DISRCC_INC_FIXED;10161016-10171017- /*10181018- * Individual dma operations are requested by the slave,10191019- * so serve only single atomic operations (S3C24XX_DCON_SERV_SINGLE).10201020- */10211021- txd->dcon |= S3C24XX_DCON_SERV_SINGLE;10221022-10231023- if (direction == DMA_MEM_TO_DEV) {10241024- txd->disrcc = S3C24XX_DISRCC_LOC_AHB |10251025- S3C24XX_DISRCC_INC_INCREMENT;10261026- txd->didstc = hwcfg;10271027- slave_addr = s3cchan->cfg.dst_addr;10281028- txd->width = s3cchan->cfg.dst_addr_width;10291029- } else if (direction == DMA_DEV_TO_MEM) {10301030- txd->disrcc = hwcfg;10311031- txd->didstc = S3C24XX_DIDSTC_LOC_AHB |10321032- S3C24XX_DIDSTC_INC_INCREMENT;10331033- slave_addr = s3cchan->cfg.src_addr;10341034- txd->width = s3cchan->cfg.src_addr_width;10351035- } else {10361036- s3c24xx_dma_free_txd(txd);10371037- dev_err(&s3cdma->pdev->dev,10381038- "direction %d unsupported\n", direction);10391039- return NULL;10401040- }10411041-10421042- for_each_sg(sgl, sg, sg_len, tmp) {10431043- dsg = kzalloc(sizeof(*dsg), GFP_NOWAIT);10441044- if (!dsg) {10451045- s3c24xx_dma_free_txd(txd);10461046- return NULL;10471047- }10481048- list_add_tail(&dsg->node, &txd->dsg_list);10491049-10501050- dsg->len = sg_dma_len(sg);10511051- if (direction == DMA_MEM_TO_DEV) {10521052- dsg->src_addr = sg_dma_address(sg);10531053- dsg->dst_addr = slave_addr;10541054- } else { /* DMA_DEV_TO_MEM */10551055- dsg->src_addr = slave_addr;10561056- dsg->dst_addr = sg_dma_address(sg);10571057- }10581058- }10591059-10601060- return vchan_tx_prep(&s3cchan->vc, &txd->vd, flags);10611061-}10621062-10631063-/*10641064- * Slave transactions callback to the slave device to allow10651065- * synchronization of slave DMA signals with the DMAC enable10661066- */10671067-static void s3c24xx_dma_issue_pending(struct dma_chan *chan)10681068-{10691069- struct s3c24xx_dma_chan *s3cchan = to_s3c24xx_dma_chan(chan);10701070- unsigned long flags;10711071-10721072- spin_lock_irqsave(&s3cchan->vc.lock, flags);10731073- if (vchan_issue_pending(&s3cchan->vc)) {10741074- if (!s3cchan->phy && s3cchan->state != S3C24XX_DMA_CHAN_WAITING)10751075- s3c24xx_dma_phy_alloc_and_start(s3cchan);10761076- }10771077- spin_unlock_irqrestore(&s3cchan->vc.lock, flags);10781078-}10791079-10801080-/*10811081- * Bringup and teardown10821082- */10831083-10841084-/*10851085- * Initialise the DMAC memcpy/slave channels.10861086- * Make a local wrapper to hold required data10871087- */10881088-static int s3c24xx_dma_init_virtual_channels(struct s3c24xx_dma_engine *s3cdma,10891089- struct dma_device *dmadev, unsigned int channels, bool slave)10901090-{10911091- struct s3c24xx_dma_chan *chan;10921092- int i;10931093-10941094- INIT_LIST_HEAD(&dmadev->channels);10951095-10961096- /*10971097- * Register as many memcpy as we have physical channels,10981098- * we won't always be able to use all but the code will have10991099- * to cope with that situation.11001100- */11011101- for (i = 0; i < channels; i++) {11021102- chan = devm_kzalloc(dmadev->dev, sizeof(*chan), GFP_KERNEL);11031103- if (!chan)11041104- return -ENOMEM;11051105-11061106- chan->id = i;11071107- chan->host = s3cdma;11081108- chan->state = S3C24XX_DMA_CHAN_IDLE;11091109-11101110- if (slave) {11111111- chan->slave = true;11121112- chan->name = kasprintf(GFP_KERNEL, "slave%d", i);11131113- if (!chan->name)11141114- return -ENOMEM;11151115- } else {11161116- chan->name = kasprintf(GFP_KERNEL, "memcpy%d", i);11171117- if (!chan->name)11181118- return -ENOMEM;11191119- }11201120- dev_dbg(dmadev->dev,11211121- "initialize virtual channel \"%s\"\n",11221122- chan->name);11231123-11241124- chan->vc.desc_free = s3c24xx_dma_desc_free;11251125- vchan_init(&chan->vc, dmadev);11261126- }11271127- dev_info(dmadev->dev, "initialized %d virtual %s channels\n",11281128- i, slave ? "slave" : "memcpy");11291129- return i;11301130-}11311131-11321132-static void s3c24xx_dma_free_virtual_channels(struct dma_device *dmadev)11331133-{11341134- struct s3c24xx_dma_chan *chan = NULL;11351135- struct s3c24xx_dma_chan *next;11361136-11371137- list_for_each_entry_safe(chan,11381138- next, &dmadev->channels, vc.chan.device_node) {11391139- list_del(&chan->vc.chan.device_node);11401140- tasklet_kill(&chan->vc.task);11411141- }11421142-}11431143-11441144-/* s3c2410, s3c2440 and s3c2442 have a 0x40 stride without separate clocks */11451145-static struct soc_data soc_s3c2410 = {11461146- .stride = 0x40,11471147- .has_reqsel = false,11481148- .has_clocks = false,11491149-};11501150-11511151-/* s3c2412 and s3c2413 have a 0x40 stride and dmareqsel mechanism */11521152-static struct soc_data soc_s3c2412 = {11531153- .stride = 0x40,11541154- .has_reqsel = true,11551155- .has_clocks = true,11561156-};11571157-11581158-/* s3c2443 and following have a 0x100 stride and dmareqsel mechanism */11591159-static struct soc_data soc_s3c2443 = {11601160- .stride = 0x100,11611161- .has_reqsel = true,11621162- .has_clocks = true,11631163-};11641164-11651165-static const struct platform_device_id s3c24xx_dma_driver_ids[] = {11661166- {11671167- .name = "s3c2410-dma",11681168- .driver_data = (kernel_ulong_t)&soc_s3c2410,11691169- }, {11701170- .name = "s3c2412-dma",11711171- .driver_data = (kernel_ulong_t)&soc_s3c2412,11721172- }, {11731173- .name = "s3c2443-dma",11741174- .driver_data = (kernel_ulong_t)&soc_s3c2443,11751175- },11761176- { },11771177-};11781178-11791179-static struct soc_data *s3c24xx_dma_get_soc_data(struct platform_device *pdev)11801180-{11811181- return (struct soc_data *)11821182- platform_get_device_id(pdev)->driver_data;11831183-}11841184-11851185-static int s3c24xx_dma_probe(struct platform_device *pdev)11861186-{11871187- const struct s3c24xx_dma_platdata *pdata = dev_get_platdata(&pdev->dev);11881188- struct s3c24xx_dma_engine *s3cdma;11891189- struct soc_data *sdata;11901190- struct resource *res;11911191- int ret;11921192- int i;11931193-11941194- if (!pdata) {11951195- dev_err(&pdev->dev, "platform data missing\n");11961196- return -ENODEV;11971197- }11981198-11991199- /* Basic sanity check */12001200- if (pdata->num_phy_channels > MAX_DMA_CHANNELS) {12011201- dev_err(&pdev->dev, "too many dma channels %d, max %d\n",12021202- pdata->num_phy_channels, MAX_DMA_CHANNELS);12031203- return -EINVAL;12041204- }12051205-12061206- sdata = s3c24xx_dma_get_soc_data(pdev);12071207- if (!sdata)12081208- return -EINVAL;12091209-12101210- s3cdma = devm_kzalloc(&pdev->dev, sizeof(*s3cdma), GFP_KERNEL);12111211- if (!s3cdma)12121212- return -ENOMEM;12131213-12141214- s3cdma->pdev = pdev;12151215- s3cdma->pdata = pdata;12161216- s3cdma->sdata = sdata;12171217-12181218- res = platform_get_resource(pdev, IORESOURCE_MEM, 0);12191219- s3cdma->base = devm_ioremap_resource(&pdev->dev, res);12201220- if (IS_ERR(s3cdma->base))12211221- return PTR_ERR(s3cdma->base);12221222-12231223- s3cdma->phy_chans = devm_kcalloc(&pdev->dev,12241224- pdata->num_phy_channels,12251225- sizeof(struct s3c24xx_dma_phy),12261226- GFP_KERNEL);12271227- if (!s3cdma->phy_chans)12281228- return -ENOMEM;12291229-12301230- /* acquire irqs and clocks for all physical channels */12311231- for (i = 0; i < pdata->num_phy_channels; i++) {12321232- struct s3c24xx_dma_phy *phy = &s3cdma->phy_chans[i];12331233- char clk_name[6];12341234-12351235- phy->id = i;12361236- phy->base = s3cdma->base + (i * sdata->stride);12371237- phy->host = s3cdma;12381238-12391239- phy->irq = platform_get_irq(pdev, i);12401240- if (phy->irq < 0)12411241- continue;12421242-12431243- ret = devm_request_irq(&pdev->dev, phy->irq, s3c24xx_dma_irq,12441244- 0, pdev->name, phy);12451245- if (ret) {12461246- dev_err(&pdev->dev, "Unable to request irq for channel %d, error %d\n",12471247- i, ret);12481248- continue;12491249- }12501250-12511251- if (sdata->has_clocks) {12521252- sprintf(clk_name, "dma.%d", i);12531253- phy->clk = devm_clk_get(&pdev->dev, clk_name);12541254- if (IS_ERR(phy->clk) && sdata->has_clocks) {12551255- dev_err(&pdev->dev, "unable to acquire clock for channel %d, error %lu\n",12561256- i, PTR_ERR(phy->clk));12571257- continue;12581258- }12591259-12601260- ret = clk_prepare(phy->clk);12611261- if (ret) {12621262- dev_err(&pdev->dev, "clock for phy %d failed, error %d\n",12631263- i, ret);12641264- continue;12651265- }12661266- }12671267-12681268- spin_lock_init(&phy->lock);12691269- phy->valid = true;12701270-12711271- dev_dbg(&pdev->dev, "physical channel %d is %s\n",12721272- i, s3c24xx_dma_phy_busy(phy) ? "BUSY" : "FREE");12731273- }12741274-12751275- /* Initialize memcpy engine */12761276- dma_cap_set(DMA_MEMCPY, s3cdma->memcpy.cap_mask);12771277- dma_cap_set(DMA_PRIVATE, s3cdma->memcpy.cap_mask);12781278- s3cdma->memcpy.dev = &pdev->dev;12791279- s3cdma->memcpy.device_free_chan_resources =12801280- s3c24xx_dma_free_chan_resources;12811281- s3cdma->memcpy.device_prep_dma_memcpy = s3c24xx_dma_prep_memcpy;12821282- s3cdma->memcpy.device_tx_status = s3c24xx_dma_tx_status;12831283- s3cdma->memcpy.device_issue_pending = s3c24xx_dma_issue_pending;12841284- s3cdma->memcpy.device_config = s3c24xx_dma_set_runtime_config;12851285- s3cdma->memcpy.device_terminate_all = s3c24xx_dma_terminate_all;12861286- s3cdma->memcpy.device_synchronize = s3c24xx_dma_synchronize;12871287-12881288- /* Initialize slave engine for SoC internal dedicated peripherals */12891289- dma_cap_set(DMA_SLAVE, s3cdma->slave.cap_mask);12901290- dma_cap_set(DMA_CYCLIC, s3cdma->slave.cap_mask);12911291- dma_cap_set(DMA_PRIVATE, s3cdma->slave.cap_mask);12921292- s3cdma->slave.dev = &pdev->dev;12931293- s3cdma->slave.device_free_chan_resources =12941294- s3c24xx_dma_free_chan_resources;12951295- s3cdma->slave.device_tx_status = s3c24xx_dma_tx_status;12961296- s3cdma->slave.device_issue_pending = s3c24xx_dma_issue_pending;12971297- s3cdma->slave.device_prep_slave_sg = s3c24xx_dma_prep_slave_sg;12981298- s3cdma->slave.device_prep_dma_cyclic = s3c24xx_dma_prep_dma_cyclic;12991299- s3cdma->slave.device_config = s3c24xx_dma_set_runtime_config;13001300- s3cdma->slave.device_terminate_all = s3c24xx_dma_terminate_all;13011301- s3cdma->slave.device_synchronize = s3c24xx_dma_synchronize;13021302- s3cdma->slave.filter.map = pdata->slave_map;13031303- s3cdma->slave.filter.mapcnt = pdata->slavecnt;13041304- s3cdma->slave.filter.fn = s3c24xx_dma_filter;13051305-13061306- /* Register as many memcpy channels as there are physical channels */13071307- ret = s3c24xx_dma_init_virtual_channels(s3cdma, &s3cdma->memcpy,13081308- pdata->num_phy_channels, false);13091309- if (ret <= 0) {13101310- dev_warn(&pdev->dev,13111311- "%s failed to enumerate memcpy channels - %d\n",13121312- __func__, ret);13131313- goto err_memcpy;13141314- }13151315-13161316- /* Register slave channels */13171317- ret = s3c24xx_dma_init_virtual_channels(s3cdma, &s3cdma->slave,13181318- pdata->num_channels, true);13191319- if (ret <= 0) {13201320- dev_warn(&pdev->dev,13211321- "%s failed to enumerate slave channels - %d\n",13221322- __func__, ret);13231323- goto err_slave;13241324- }13251325-13261326- ret = dma_async_device_register(&s3cdma->memcpy);13271327- if (ret) {13281328- dev_warn(&pdev->dev,13291329- "%s failed to register memcpy as an async device - %d\n",13301330- __func__, ret);13311331- goto err_memcpy_reg;13321332- }13331333-13341334- ret = dma_async_device_register(&s3cdma->slave);13351335- if (ret) {13361336- dev_warn(&pdev->dev,13371337- "%s failed to register slave as an async device - %d\n",13381338- __func__, ret);13391339- goto err_slave_reg;13401340- }13411341-13421342- platform_set_drvdata(pdev, s3cdma);13431343- dev_info(&pdev->dev, "Loaded dma driver with %d physical channels\n",13441344- pdata->num_phy_channels);13451345-13461346- return 0;13471347-13481348-err_slave_reg:13491349- dma_async_device_unregister(&s3cdma->memcpy);13501350-err_memcpy_reg:13511351- s3c24xx_dma_free_virtual_channels(&s3cdma->slave);13521352-err_slave:13531353- s3c24xx_dma_free_virtual_channels(&s3cdma->memcpy);13541354-err_memcpy:13551355- if (sdata->has_clocks)13561356- for (i = 0; i < pdata->num_phy_channels; i++) {13571357- struct s3c24xx_dma_phy *phy = &s3cdma->phy_chans[i];13581358- if (phy->valid)13591359- clk_unprepare(phy->clk);13601360- }13611361-13621362- return ret;13631363-}13641364-13651365-static void s3c24xx_dma_free_irq(struct platform_device *pdev,13661366- struct s3c24xx_dma_engine *s3cdma)13671367-{13681368- int i;13691369-13701370- for (i = 0; i < s3cdma->pdata->num_phy_channels; i++) {13711371- struct s3c24xx_dma_phy *phy = &s3cdma->phy_chans[i];13721372-13731373- devm_free_irq(&pdev->dev, phy->irq, phy);13741374- }13751375-}13761376-13771377-static int s3c24xx_dma_remove(struct platform_device *pdev)13781378-{13791379- const struct s3c24xx_dma_platdata *pdata = dev_get_platdata(&pdev->dev);13801380- struct s3c24xx_dma_engine *s3cdma = platform_get_drvdata(pdev);13811381- struct soc_data *sdata = s3c24xx_dma_get_soc_data(pdev);13821382- int i;13831383-13841384- dma_async_device_unregister(&s3cdma->slave);13851385- dma_async_device_unregister(&s3cdma->memcpy);13861386-13871387- s3c24xx_dma_free_irq(pdev, s3cdma);13881388-13891389- s3c24xx_dma_free_virtual_channels(&s3cdma->slave);13901390- s3c24xx_dma_free_virtual_channels(&s3cdma->memcpy);13911391-13921392- if (sdata->has_clocks)13931393- for (i = 0; i < pdata->num_phy_channels; i++) {13941394- struct s3c24xx_dma_phy *phy = &s3cdma->phy_chans[i];13951395- if (phy->valid)13961396- clk_unprepare(phy->clk);13971397- }13981398-13991399- return 0;14001400-}14011401-14021402-static struct platform_driver s3c24xx_dma_driver = {14031403- .driver = {14041404- .name = "s3c24xx-dma",14051405- },14061406- .id_table = s3c24xx_dma_driver_ids,14071407- .probe = s3c24xx_dma_probe,14081408- .remove = s3c24xx_dma_remove,14091409-};14101410-14111411-module_platform_driver(s3c24xx_dma_driver);14121412-14131413-bool s3c24xx_dma_filter(struct dma_chan *chan, void *param)14141414-{14151415- struct s3c24xx_dma_chan *s3cchan;14161416-14171417- if (chan->device->dev->driver != &s3c24xx_dma_driver.driver)14181418- return false;14191419-14201420- s3cchan = to_s3c24xx_dma_chan(chan);14211421-14221422- return s3cchan->id == (uintptr_t)param;14231423-}14241424-EXPORT_SYMBOL(s3c24xx_dma_filter);14251425-14261426-MODULE_DESCRIPTION("S3C24XX DMA Driver");14271427-MODULE_AUTHOR("Heiko Stuebner");14281428-MODULE_LICENSE("GPL v2");
-48
include/linux/platform_data/dma-s3c24xx.h
···11-/* SPDX-License-Identifier: GPL-2.0-or-later */22-/*33- * S3C24XX DMA handling44- *55- * Copyright (c) 2013 Heiko Stuebner <heiko@sntech.de>66- */77-88-/* Helper to encode the source selection constraints for early s3c socs. */99-#define S3C24XX_DMA_CHANREQ(src, chan) ((BIT(3) | src) << chan * 4)1010-1111-enum s3c24xx_dma_bus {1212- S3C24XX_DMA_APB,1313- S3C24XX_DMA_AHB,1414-};1515-1616-/**1717- * @bus: on which bus does the peripheral reside - AHB or APB.1818- * @handshake: is a handshake with the peripheral necessary1919- * @chansel: channel selection information, depending on variant; reqsel for2020- * s3c2443 and later and channel-selection map for earlier SoCs2121- * see CHANSEL doc in s3c2443-dma.c2222- */2323-struct s3c24xx_dma_channel {2424- enum s3c24xx_dma_bus bus;2525- bool handshake;2626- u16 chansel;2727-};2828-2929-struct dma_slave_map;3030-3131-/**3232- * struct s3c24xx_dma_platdata - platform specific settings3333- * @num_phy_channels: number of physical channels3434- * @channels: array of virtual channel descriptions3535- * @num_channels: number of virtual channels3636- * @slave_map: dma slave map matching table3737- * @slavecnt: number of elements in slave_map3838- */3939-struct s3c24xx_dma_platdata {4040- int num_phy_channels;4141- struct s3c24xx_dma_channel *channels;4242- int num_channels;4343- const struct dma_slave_map *slave_map;4444- int slavecnt;4545-};4646-4747-struct dma_chan;4848-bool s3c24xx_dma_filter(struct dma_chan *chan, void *param);