1e073564fSAndra Danciu // SPDX-License-Identifier: GPL-2.0
2e073564fSAndra Danciu //
3e073564fSAndra Danciu // Freescale DMA ALSA SoC PCM driver
4e073564fSAndra Danciu //
5e073564fSAndra Danciu // Author: Timur Tabi <timur@freescale.com>
6e073564fSAndra Danciu //
7e073564fSAndra Danciu // Copyright 2007-2010 Freescale Semiconductor, Inc.
8e073564fSAndra Danciu //
9e073564fSAndra Danciu // This driver implements ASoC support for the Elo DMA controller, which is
10e073564fSAndra Danciu // the DMA controller on Freescale 83xx, 85xx, and 86xx SOCs. In ALSA terms,
11e073564fSAndra Danciu // the PCM driver is what handles the DMA buffer.
1217467f23STimur Tabi
1317467f23STimur Tabi #include <linux/module.h>
1417467f23STimur Tabi #include <linux/init.h>
1517467f23STimur Tabi #include <linux/platform_device.h>
1617467f23STimur Tabi #include <linux/dma-mapping.h>
1717467f23STimur Tabi #include <linux/interrupt.h>
1817467f23STimur Tabi #include <linux/delay.h>
195a0e3ad6STejun Heo #include <linux/gfp.h>
205af50730SRob Herring #include <linux/of_address.h>
215af50730SRob Herring #include <linux/of_irq.h>
22f0fba2adSLiam Girdwood #include <linux/of_platform.h>
23f0fba2adSLiam Girdwood #include <linux/list.h>
2438fec727STimur Tabi #include <linux/slab.h>
2517467f23STimur Tabi
2617467f23STimur Tabi #include <sound/core.h>
2717467f23STimur Tabi #include <sound/pcm.h>
2817467f23STimur Tabi #include <sound/pcm_params.h>
2917467f23STimur Tabi #include <sound/soc.h>
3017467f23STimur Tabi
3117467f23STimur Tabi #include <asm/io.h>
3217467f23STimur Tabi
3317467f23STimur Tabi #include "fsl_dma.h"
34f0fba2adSLiam Girdwood #include "fsl_ssi.h" /* For the offset of stx0 and srx0 */
3517467f23STimur Tabi
364cb1ea92SKuninori Morimoto #define DRV_NAME "fsl_dma"
374cb1ea92SKuninori Morimoto
3817467f23STimur Tabi /*
3917467f23STimur Tabi * The formats that the DMA controller supports, which is anything
4017467f23STimur Tabi * that is 8, 16, or 32 bits.
4117467f23STimur Tabi */
4217467f23STimur Tabi #define FSLDMA_PCM_FORMATS (SNDRV_PCM_FMTBIT_S8 | \
4317467f23STimur Tabi SNDRV_PCM_FMTBIT_U8 | \
4417467f23STimur Tabi SNDRV_PCM_FMTBIT_S16_LE | \
4517467f23STimur Tabi SNDRV_PCM_FMTBIT_S16_BE | \
4617467f23STimur Tabi SNDRV_PCM_FMTBIT_U16_LE | \
4717467f23STimur Tabi SNDRV_PCM_FMTBIT_U16_BE | \
4817467f23STimur Tabi SNDRV_PCM_FMTBIT_S24_LE | \
4917467f23STimur Tabi SNDRV_PCM_FMTBIT_S24_BE | \
5017467f23STimur Tabi SNDRV_PCM_FMTBIT_U24_LE | \
5117467f23STimur Tabi SNDRV_PCM_FMTBIT_U24_BE | \
5217467f23STimur Tabi SNDRV_PCM_FMTBIT_S32_LE | \
5317467f23STimur Tabi SNDRV_PCM_FMTBIT_S32_BE | \
5417467f23STimur Tabi SNDRV_PCM_FMTBIT_U32_LE | \
5517467f23STimur Tabi SNDRV_PCM_FMTBIT_U32_BE)
56f0fba2adSLiam Girdwood struct dma_object {
574cb1ea92SKuninori Morimoto struct snd_soc_component_driver dai;
5817467f23STimur Tabi dma_addr_t ssi_stx_phys;
5917467f23STimur Tabi dma_addr_t ssi_srx_phys;
608e9d8690STimur Tabi unsigned int ssi_fifo_depth;
61f0fba2adSLiam Girdwood struct ccsr_dma_channel __iomem *channel;
62f0fba2adSLiam Girdwood unsigned int irq;
63f0fba2adSLiam Girdwood bool assigned;
64f0fba2adSLiam Girdwood };
6517467f23STimur Tabi
6617467f23STimur Tabi /*
6717467f23STimur Tabi * The number of DMA links to use. Two is the bare minimum, but if you
6817467f23STimur Tabi * have really small links you might need more.
6917467f23STimur Tabi */
7017467f23STimur Tabi #define NUM_DMA_LINKS 2
7117467f23STimur Tabi
7217467f23STimur Tabi /** fsl_dma_private: p-substream DMA data
7317467f23STimur Tabi *
7417467f23STimur Tabi * Each substream has a 1-to-1 association with a DMA channel.
7517467f23STimur Tabi *
7617467f23STimur Tabi * The link[] array is first because it needs to be aligned on a 32-byte
7717467f23STimur Tabi * boundary, so putting it first will ensure alignment without padding the
7817467f23STimur Tabi * structure.
7917467f23STimur Tabi *
8017467f23STimur Tabi * @link[]: array of link descriptors
8117467f23STimur Tabi * @dma_channel: pointer to the DMA channel's registers
8217467f23STimur Tabi * @irq: IRQ for this DMA channel
8317467f23STimur Tabi * @substream: pointer to the substream object, needed by the ISR
8417467f23STimur Tabi * @ssi_sxx_phys: bus address of the STX or SRX register to use
8517467f23STimur Tabi * @ld_buf_phys: physical address of the LD buffer
8617467f23STimur Tabi * @current_link: index into link[] of the link currently being processed
8717467f23STimur Tabi * @dma_buf_phys: physical address of the DMA buffer
8817467f23STimur Tabi * @dma_buf_next: physical address of the next period to process
8917467f23STimur Tabi * @dma_buf_end: physical address of the byte after the end of the DMA
9017467f23STimur Tabi * @buffer period_size: the size of a single period
9117467f23STimur Tabi * @num_periods: the number of periods in the DMA buffer
9217467f23STimur Tabi */
9317467f23STimur Tabi struct fsl_dma_private {
9417467f23STimur Tabi struct fsl_dma_link_descriptor link[NUM_DMA_LINKS];
9517467f23STimur Tabi struct ccsr_dma_channel __iomem *dma_channel;
9617467f23STimur Tabi unsigned int irq;
9717467f23STimur Tabi struct snd_pcm_substream *substream;
9817467f23STimur Tabi dma_addr_t ssi_sxx_phys;
998e9d8690STimur Tabi unsigned int ssi_fifo_depth;
10017467f23STimur Tabi dma_addr_t ld_buf_phys;
10117467f23STimur Tabi unsigned int current_link;
10217467f23STimur Tabi dma_addr_t dma_buf_phys;
10317467f23STimur Tabi dma_addr_t dma_buf_next;
10417467f23STimur Tabi dma_addr_t dma_buf_end;
10517467f23STimur Tabi size_t period_size;
10617467f23STimur Tabi unsigned int num_periods;
10717467f23STimur Tabi };
10817467f23STimur Tabi
10917467f23STimur Tabi /**
11017467f23STimur Tabi * fsl_dma_hardare: define characteristics of the PCM hardware.
11117467f23STimur Tabi *
11217467f23STimur Tabi * The PCM hardware is the Freescale DMA controller. This structure defines
11317467f23STimur Tabi * the capabilities of that hardware.
11417467f23STimur Tabi *
11517467f23STimur Tabi * Since the sampling rate and data format are not controlled by the DMA
11617467f23STimur Tabi * controller, we specify no limits for those values. The only exception is
11717467f23STimur Tabi * period_bytes_min, which is set to a reasonably low value to prevent the
11817467f23STimur Tabi * DMA controller from generating too many interrupts per second.
11917467f23STimur Tabi *
12017467f23STimur Tabi * Since each link descriptor has a 32-bit byte count field, we set
12117467f23STimur Tabi * period_bytes_max to the largest 32-bit number. We also have no maximum
12217467f23STimur Tabi * number of periods.
123be41e941STimur Tabi *
124be41e941STimur Tabi * Note that we specify SNDRV_PCM_INFO_JOINT_DUPLEX here, but only because a
125be41e941STimur Tabi * limitation in the SSI driver requires the sample rates for playback and
126be41e941STimur Tabi * capture to be the same.
12717467f23STimur Tabi */
12817467f23STimur Tabi static const struct snd_pcm_hardware fsl_dma_hardware = {
12917467f23STimur Tabi
1304052ce4cSTimur Tabi .info = SNDRV_PCM_INFO_INTERLEAVED |
1314052ce4cSTimur Tabi SNDRV_PCM_INFO_MMAP |
132be41e941STimur Tabi SNDRV_PCM_INFO_MMAP_VALID |
1333a638ff2STimur Tabi SNDRV_PCM_INFO_JOINT_DUPLEX |
1343a638ff2STimur Tabi SNDRV_PCM_INFO_PAUSE,
13517467f23STimur Tabi .formats = FSLDMA_PCM_FORMATS,
13617467f23STimur Tabi .period_bytes_min = 512, /* A reasonable limit */
13717467f23STimur Tabi .period_bytes_max = (u32) -1,
13817467f23STimur Tabi .periods_min = NUM_DMA_LINKS,
13917467f23STimur Tabi .periods_max = (unsigned int) -1,
14017467f23STimur Tabi .buffer_bytes_max = 128 * 1024, /* A reasonable limit */
14117467f23STimur Tabi };
14217467f23STimur Tabi
14317467f23STimur Tabi /**
14417467f23STimur Tabi * fsl_dma_abort_stream: tell ALSA that the DMA transfer has aborted
14517467f23STimur Tabi *
14617467f23STimur Tabi * This function should be called by the ISR whenever the DMA controller
14717467f23STimur Tabi * halts data transfer.
14817467f23STimur Tabi */
fsl_dma_abort_stream(struct snd_pcm_substream * substream)14917467f23STimur Tabi static void fsl_dma_abort_stream(struct snd_pcm_substream *substream)
15017467f23STimur Tabi {
1511fb8510cSTakashi Iwai snd_pcm_stop_xrun(substream);
15217467f23STimur Tabi }
15317467f23STimur Tabi
15417467f23STimur Tabi /**
15517467f23STimur Tabi * fsl_dma_update_pointers - update LD pointers to point to the next period
15617467f23STimur Tabi *
1570d8aa2ccSRandy Dunlap * As each period is completed, this function changes the link
15817467f23STimur Tabi * descriptor pointers for that period to point to the next period.
15917467f23STimur Tabi */
fsl_dma_update_pointers(struct fsl_dma_private * dma_private)16017467f23STimur Tabi static void fsl_dma_update_pointers(struct fsl_dma_private *dma_private)
16117467f23STimur Tabi {
16217467f23STimur Tabi struct fsl_dma_link_descriptor *link =
16317467f23STimur Tabi &dma_private->link[dma_private->current_link];
16417467f23STimur Tabi
1651a3c5a49STimur Tabi /* Update our link descriptors to point to the next period. On a 36-bit
1661a3c5a49STimur Tabi * system, we also need to update the ESAD bits. We also set (keep) the
1671a3c5a49STimur Tabi * snoop bits. See the comments in fsl_dma_hw_params() about snooping.
1681a3c5a49STimur Tabi */
1691a3c5a49STimur Tabi if (dma_private->substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1701a3c5a49STimur Tabi link->source_addr = cpu_to_be32(dma_private->dma_buf_next);
1711a3c5a49STimur Tabi #ifdef CONFIG_PHYS_64BIT
1721a3c5a49STimur Tabi link->source_attr = cpu_to_be32(CCSR_DMA_ATR_SNOOP |
1731a3c5a49STimur Tabi upper_32_bits(dma_private->dma_buf_next));
1741a3c5a49STimur Tabi #endif
1751a3c5a49STimur Tabi } else {
1761a3c5a49STimur Tabi link->dest_addr = cpu_to_be32(dma_private->dma_buf_next);
1771a3c5a49STimur Tabi #ifdef CONFIG_PHYS_64BIT
1781a3c5a49STimur Tabi link->dest_attr = cpu_to_be32(CCSR_DMA_ATR_SNOOP |
1791a3c5a49STimur Tabi upper_32_bits(dma_private->dma_buf_next));
1801a3c5a49STimur Tabi #endif
1811a3c5a49STimur Tabi }
18217467f23STimur Tabi
18317467f23STimur Tabi /* Update our variables for next time */
18417467f23STimur Tabi dma_private->dma_buf_next += dma_private->period_size;
18517467f23STimur Tabi
18617467f23STimur Tabi if (dma_private->dma_buf_next >= dma_private->dma_buf_end)
18717467f23STimur Tabi dma_private->dma_buf_next = dma_private->dma_buf_phys;
18817467f23STimur Tabi
18917467f23STimur Tabi if (++dma_private->current_link >= NUM_DMA_LINKS)
19017467f23STimur Tabi dma_private->current_link = 0;
19117467f23STimur Tabi }
19217467f23STimur Tabi
19317467f23STimur Tabi /**
19417467f23STimur Tabi * fsl_dma_isr: interrupt handler for the DMA controller
19517467f23STimur Tabi *
19617467f23STimur Tabi * @irq: IRQ of the DMA channel
19717467f23STimur Tabi * @dev_id: pointer to the dma_private structure for this DMA channel
19817467f23STimur Tabi */
fsl_dma_isr(int irq,void * dev_id)19917467f23STimur Tabi static irqreturn_t fsl_dma_isr(int irq, void *dev_id)
20017467f23STimur Tabi {
20117467f23STimur Tabi struct fsl_dma_private *dma_private = dev_id;
202f0fba2adSLiam Girdwood struct snd_pcm_substream *substream = dma_private->substream;
2039f5f078aSKuninori Morimoto struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
204a21192a7SKuninori Morimoto struct device *dev = rtd->dev;
20517467f23STimur Tabi struct ccsr_dma_channel __iomem *dma_channel = dma_private->dma_channel;
20617467f23STimur Tabi irqreturn_t ret = IRQ_NONE;
20717467f23STimur Tabi u32 sr, sr2 = 0;
20817467f23STimur Tabi
20917467f23STimur Tabi /* We got an interrupt, so read the status register to see what we
21017467f23STimur Tabi were interrupted for.
21117467f23STimur Tabi */
21217467f23STimur Tabi sr = in_be32(&dma_channel->sr);
21317467f23STimur Tabi
21417467f23STimur Tabi if (sr & CCSR_DMA_SR_TE) {
215f0fba2adSLiam Girdwood dev_err(dev, "dma transmit error\n");
216f0fba2adSLiam Girdwood fsl_dma_abort_stream(substream);
21717467f23STimur Tabi sr2 |= CCSR_DMA_SR_TE;
21817467f23STimur Tabi ret = IRQ_HANDLED;
21917467f23STimur Tabi }
22017467f23STimur Tabi
22117467f23STimur Tabi if (sr & CCSR_DMA_SR_CH)
22217467f23STimur Tabi ret = IRQ_HANDLED;
22317467f23STimur Tabi
22417467f23STimur Tabi if (sr & CCSR_DMA_SR_PE) {
225f0fba2adSLiam Girdwood dev_err(dev, "dma programming error\n");
226f0fba2adSLiam Girdwood fsl_dma_abort_stream(substream);
22717467f23STimur Tabi sr2 |= CCSR_DMA_SR_PE;
22817467f23STimur Tabi ret = IRQ_HANDLED;
22917467f23STimur Tabi }
23017467f23STimur Tabi
23117467f23STimur Tabi if (sr & CCSR_DMA_SR_EOLNI) {
23217467f23STimur Tabi sr2 |= CCSR_DMA_SR_EOLNI;
23317467f23STimur Tabi ret = IRQ_HANDLED;
23417467f23STimur Tabi }
23517467f23STimur Tabi
23617467f23STimur Tabi if (sr & CCSR_DMA_SR_CB)
23717467f23STimur Tabi ret = IRQ_HANDLED;
23817467f23STimur Tabi
23917467f23STimur Tabi if (sr & CCSR_DMA_SR_EOSI) {
24017467f23STimur Tabi /* Tell ALSA we completed a period. */
24117467f23STimur Tabi snd_pcm_period_elapsed(substream);
24217467f23STimur Tabi
24317467f23STimur Tabi /*
24417467f23STimur Tabi * Update our link descriptors to point to the next period. We
24517467f23STimur Tabi * only need to do this if the number of periods is not equal to
24617467f23STimur Tabi * the number of links.
24717467f23STimur Tabi */
24817467f23STimur Tabi if (dma_private->num_periods != NUM_DMA_LINKS)
24917467f23STimur Tabi fsl_dma_update_pointers(dma_private);
25017467f23STimur Tabi
25117467f23STimur Tabi sr2 |= CCSR_DMA_SR_EOSI;
25217467f23STimur Tabi ret = IRQ_HANDLED;
25317467f23STimur Tabi }
25417467f23STimur Tabi
25517467f23STimur Tabi if (sr & CCSR_DMA_SR_EOLSI) {
25617467f23STimur Tabi sr2 |= CCSR_DMA_SR_EOLSI;
25717467f23STimur Tabi ret = IRQ_HANDLED;
25817467f23STimur Tabi }
25917467f23STimur Tabi
26017467f23STimur Tabi /* Clear the bits that we set */
26117467f23STimur Tabi if (sr2)
26217467f23STimur Tabi out_be32(&dma_channel->sr, sr2);
26317467f23STimur Tabi
26417467f23STimur Tabi return ret;
26517467f23STimur Tabi }
26617467f23STimur Tabi
26717467f23STimur Tabi /**
26817467f23STimur Tabi * fsl_dma_new: initialize this PCM driver.
26917467f23STimur Tabi *
27017467f23STimur Tabi * This function is called when the codec driver calls snd_soc_new_pcms(),
27187506549SMark Brown * once for each .dai_link in the machine driver's snd_soc_card
27217467f23STimur Tabi * structure.
2731a3c5a49STimur Tabi *
2741a3c5a49STimur Tabi * snd_dma_alloc_pages() is just a front-end to dma_alloc_coherent(), which
2751a3c5a49STimur Tabi * (currently) always allocates the DMA buffer in lowmem, even if GFP_HIGHMEM
2761a3c5a49STimur Tabi * is specified. Therefore, any DMA buffers we allocate will always be in low
2771a3c5a49STimur Tabi * memory, but we support for 36-bit physical addresses anyway.
2781a3c5a49STimur Tabi *
2791a3c5a49STimur Tabi * Regardless of where the memory is actually allocated, since the device can
2801a3c5a49STimur Tabi * technically DMA to any 36-bit address, we do need to set the DMA mask to 36.
28117467f23STimur Tabi */
fsl_dma_new(struct snd_soc_component * component,struct snd_soc_pcm_runtime * rtd)2824ac85de9SKuninori Morimoto static int fsl_dma_new(struct snd_soc_component *component,
2834ac85de9SKuninori Morimoto struct snd_soc_pcm_runtime *rtd)
28417467f23STimur Tabi {
285552d1ef6SLiam Girdwood struct snd_card *card = rtd->card->snd_card;
286552d1ef6SLiam Girdwood struct snd_pcm *pcm = rtd->pcm;
28717467f23STimur Tabi int ret;
28817467f23STimur Tabi
289c9bd5e69SRussell King ret = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(36));
290c9bd5e69SRussell King if (ret)
291c9bd5e69SRussell King return ret;
29217467f23STimur Tabi
293e159704fSTakashi Iwai return snd_pcm_set_fixed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
294e159704fSTakashi Iwai card->dev,
295e159704fSTakashi Iwai fsl_dma_hardware.buffer_bytes_max);
29617467f23STimur Tabi }
29717467f23STimur Tabi
29817467f23STimur Tabi /**
29917467f23STimur Tabi * fsl_dma_open: open a new substream.
30017467f23STimur Tabi *
30117467f23STimur Tabi * Each substream has its own DMA buffer.
302bf9c8c9dSTimur Tabi *
303bf9c8c9dSTimur Tabi * ALSA divides the DMA buffer into N periods. We create NUM_DMA_LINKS link
304bf9c8c9dSTimur Tabi * descriptors that ping-pong from one period to the next. For example, if
305bf9c8c9dSTimur Tabi * there are six periods and two link descriptors, this is how they look
306bf9c8c9dSTimur Tabi * before playback starts:
307bf9c8c9dSTimur Tabi *
308bf9c8c9dSTimur Tabi * The last link descriptor
309bf9c8c9dSTimur Tabi * ____________ points back to the first
310bf9c8c9dSTimur Tabi * | |
311bf9c8c9dSTimur Tabi * V |
312bf9c8c9dSTimur Tabi * ___ ___ |
313bf9c8c9dSTimur Tabi * | |->| |->|
314bf9c8c9dSTimur Tabi * |___| |___|
315bf9c8c9dSTimur Tabi * | |
316bf9c8c9dSTimur Tabi * | |
317bf9c8c9dSTimur Tabi * V V
318bf9c8c9dSTimur Tabi * _________________________________________
319bf9c8c9dSTimur Tabi * | | | | | | | The DMA buffer is
320bf9c8c9dSTimur Tabi * | | | | | | | divided into 6 parts
321bf9c8c9dSTimur Tabi * |______|______|______|______|______|______|
322bf9c8c9dSTimur Tabi *
323bf9c8c9dSTimur Tabi * and here's how they look after the first period is finished playing:
324bf9c8c9dSTimur Tabi *
325bf9c8c9dSTimur Tabi * ____________
326bf9c8c9dSTimur Tabi * | |
327bf9c8c9dSTimur Tabi * V |
328bf9c8c9dSTimur Tabi * ___ ___ |
329bf9c8c9dSTimur Tabi * | |->| |->|
330bf9c8c9dSTimur Tabi * |___| |___|
331bf9c8c9dSTimur Tabi * | |
332bf9c8c9dSTimur Tabi * |______________
333bf9c8c9dSTimur Tabi * | |
334bf9c8c9dSTimur Tabi * V V
335bf9c8c9dSTimur Tabi * _________________________________________
336bf9c8c9dSTimur Tabi * | | | | | | |
337bf9c8c9dSTimur Tabi * | | | | | | |
338bf9c8c9dSTimur Tabi * |______|______|______|______|______|______|
339bf9c8c9dSTimur Tabi *
340bf9c8c9dSTimur Tabi * The first link descriptor now points to the third period. The DMA
341bf9c8c9dSTimur Tabi * controller is currently playing the second period. When it finishes, it
342bf9c8c9dSTimur Tabi * will jump back to the first descriptor and play the third period.
343bf9c8c9dSTimur Tabi *
344bf9c8c9dSTimur Tabi * There are four reasons we do this:
345bf9c8c9dSTimur Tabi *
346bf9c8c9dSTimur Tabi * 1. The only way to get the DMA controller to automatically restart the
347bf9c8c9dSTimur Tabi * transfer when it gets to the end of the buffer is to use chaining
348bf9c8c9dSTimur Tabi * mode. Basic direct mode doesn't offer that feature.
349bf9c8c9dSTimur Tabi * 2. We need to receive an interrupt at the end of every period. The DMA
350bf9c8c9dSTimur Tabi * controller can generate an interrupt at the end of every link transfer
351bf9c8c9dSTimur Tabi * (aka segment). Making each period into a DMA segment will give us the
352bf9c8c9dSTimur Tabi * interrupts we need.
353bf9c8c9dSTimur Tabi * 3. By creating only two link descriptors, regardless of the number of
354bf9c8c9dSTimur Tabi * periods, we do not need to reallocate the link descriptors if the
355bf9c8c9dSTimur Tabi * number of periods changes.
356bf9c8c9dSTimur Tabi * 4. All of the audio data is still stored in a single, contiguous DMA
357bf9c8c9dSTimur Tabi * buffer, which is what ALSA expects. We're just dividing it into
358bf9c8c9dSTimur Tabi * contiguous parts, and creating a link descriptor for each one.
35917467f23STimur Tabi */
fsl_dma_open(struct snd_soc_component * component,struct snd_pcm_substream * substream)3604ac85de9SKuninori Morimoto static int fsl_dma_open(struct snd_soc_component *component,
3614ac85de9SKuninori Morimoto struct snd_pcm_substream *substream)
36217467f23STimur Tabi {
36317467f23STimur Tabi struct snd_pcm_runtime *runtime = substream->runtime;
3644cb1ea92SKuninori Morimoto struct device *dev = component->dev;
365f0fba2adSLiam Girdwood struct dma_object *dma =
3664cb1ea92SKuninori Morimoto container_of(component->driver, struct dma_object, dai);
36717467f23STimur Tabi struct fsl_dma_private *dma_private;
368bf9c8c9dSTimur Tabi struct ccsr_dma_channel __iomem *dma_channel;
36917467f23STimur Tabi dma_addr_t ld_buf_phys;
370bf9c8c9dSTimur Tabi u64 temp_link; /* Pointer to next link descriptor */
371bf9c8c9dSTimur Tabi u32 mr;
37217467f23STimur Tabi int ret = 0;
373bf9c8c9dSTimur Tabi unsigned int i;
37417467f23STimur Tabi
37517467f23STimur Tabi /*
37617467f23STimur Tabi * Reject any DMA buffer whose size is not a multiple of the period
37717467f23STimur Tabi * size. We need to make sure that the DMA buffer can be evenly divided
37817467f23STimur Tabi * into periods.
37917467f23STimur Tabi */
38017467f23STimur Tabi ret = snd_pcm_hw_constraint_integer(runtime,
38117467f23STimur Tabi SNDRV_PCM_HW_PARAM_PERIODS);
38217467f23STimur Tabi if (ret < 0) {
383f0fba2adSLiam Girdwood dev_err(dev, "invalid buffer size\n");
38417467f23STimur Tabi return ret;
38517467f23STimur Tabi }
38617467f23STimur Tabi
387f0fba2adSLiam Girdwood if (dma->assigned) {
388f0fba2adSLiam Girdwood dev_err(dev, "dma channel already assigned\n");
38917467f23STimur Tabi return -EBUSY;
39017467f23STimur Tabi }
39117467f23STimur Tabi
392f0fba2adSLiam Girdwood dma_private = dma_alloc_coherent(dev, sizeof(struct fsl_dma_private),
393f0fba2adSLiam Girdwood &ld_buf_phys, GFP_KERNEL);
39417467f23STimur Tabi if (!dma_private) {
395f0fba2adSLiam Girdwood dev_err(dev, "can't allocate dma private data\n");
39617467f23STimur Tabi return -ENOMEM;
39717467f23STimur Tabi }
39817467f23STimur Tabi if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
399f0fba2adSLiam Girdwood dma_private->ssi_sxx_phys = dma->ssi_stx_phys;
40017467f23STimur Tabi else
401f0fba2adSLiam Girdwood dma_private->ssi_sxx_phys = dma->ssi_srx_phys;
40217467f23STimur Tabi
4038e9d8690STimur Tabi dma_private->ssi_fifo_depth = dma->ssi_fifo_depth;
404f0fba2adSLiam Girdwood dma_private->dma_channel = dma->channel;
405f0fba2adSLiam Girdwood dma_private->irq = dma->irq;
40617467f23STimur Tabi dma_private->substream = substream;
40717467f23STimur Tabi dma_private->ld_buf_phys = ld_buf_phys;
40817467f23STimur Tabi dma_private->dma_buf_phys = substream->dma_buffer.addr;
40917467f23STimur Tabi
4100cd114ffSTimur Tabi ret = request_irq(dma_private->irq, fsl_dma_isr, 0, "fsldma-audio",
4110cd114ffSTimur Tabi dma_private);
41217467f23STimur Tabi if (ret) {
413f0fba2adSLiam Girdwood dev_err(dev, "can't register ISR for IRQ %u (ret=%i)\n",
41417467f23STimur Tabi dma_private->irq, ret);
415f0fba2adSLiam Girdwood dma_free_coherent(dev, sizeof(struct fsl_dma_private),
41617467f23STimur Tabi dma_private, dma_private->ld_buf_phys);
41717467f23STimur Tabi return ret;
41817467f23STimur Tabi }
41917467f23STimur Tabi
420d0657fe8SFabio Estevam dma->assigned = true;
42117467f23STimur Tabi
42217467f23STimur Tabi snd_soc_set_runtime_hwparams(substream, &fsl_dma_hardware);
42317467f23STimur Tabi runtime->private_data = dma_private;
42417467f23STimur Tabi
425bf9c8c9dSTimur Tabi /* Program the fixed DMA controller parameters */
42617467f23STimur Tabi
427bf9c8c9dSTimur Tabi dma_channel = dma_private->dma_channel;
42817467f23STimur Tabi
42917467f23STimur Tabi temp_link = dma_private->ld_buf_phys +
43017467f23STimur Tabi sizeof(struct fsl_dma_link_descriptor);
43117467f23STimur Tabi
43217467f23STimur Tabi for (i = 0; i < NUM_DMA_LINKS; i++) {
43385ef2375STimur Tabi dma_private->link[i].next = cpu_to_be64(temp_link);
43417467f23STimur Tabi
43517467f23STimur Tabi temp_link += sizeof(struct fsl_dma_link_descriptor);
43617467f23STimur Tabi }
43717467f23STimur Tabi /* The last link descriptor points to the first */
43817467f23STimur Tabi dma_private->link[i - 1].next = cpu_to_be64(dma_private->ld_buf_phys);
43917467f23STimur Tabi
44017467f23STimur Tabi /* Tell the DMA controller where the first link descriptor is */
44117467f23STimur Tabi out_be32(&dma_channel->clndar,
44217467f23STimur Tabi CCSR_DMA_CLNDAR_ADDR(dma_private->ld_buf_phys));
44317467f23STimur Tabi out_be32(&dma_channel->eclndar,
44417467f23STimur Tabi CCSR_DMA_ECLNDAR_ADDR(dma_private->ld_buf_phys));
44517467f23STimur Tabi
44617467f23STimur Tabi /* The manual says the BCR must be clear before enabling EMP */
44717467f23STimur Tabi out_be32(&dma_channel->bcr, 0);
44817467f23STimur Tabi
44917467f23STimur Tabi /*
45017467f23STimur Tabi * Program the mode register for interrupts, external master control,
45117467f23STimur Tabi * and source/destination hold. Also clear the Channel Abort bit.
45217467f23STimur Tabi */
45317467f23STimur Tabi mr = in_be32(&dma_channel->mr) &
45417467f23STimur Tabi ~(CCSR_DMA_MR_CA | CCSR_DMA_MR_DAHE | CCSR_DMA_MR_SAHE);
45517467f23STimur Tabi
45617467f23STimur Tabi /*
45717467f23STimur Tabi * We want External Master Start and External Master Pause enabled,
45817467f23STimur Tabi * because the SSI is controlling the DMA controller. We want the DMA
45917467f23STimur Tabi * controller to be set up in advance, and then we signal only the SSI
460bf9c8c9dSTimur Tabi * to start transferring.
46117467f23STimur Tabi *
46217467f23STimur Tabi * We want End-Of-Segment Interrupts enabled, because this will generate
46317467f23STimur Tabi * an interrupt at the end of each segment (each link descriptor
46417467f23STimur Tabi * represents one segment). Each DMA segment is the same thing as an
46517467f23STimur Tabi * ALSA period, so this is how we get an interrupt at the end of every
46617467f23STimur Tabi * period.
46717467f23STimur Tabi *
46817467f23STimur Tabi * We want Error Interrupt enabled, so that we can get an error if
46917467f23STimur Tabi * the DMA controller is mis-programmed somehow.
47017467f23STimur Tabi */
47117467f23STimur Tabi mr |= CCSR_DMA_MR_EOSIE | CCSR_DMA_MR_EIE | CCSR_DMA_MR_EMP_EN |
47217467f23STimur Tabi CCSR_DMA_MR_EMS_EN;
47317467f23STimur Tabi
47417467f23STimur Tabi /* For playback, we want the destination address to be held. For
47517467f23STimur Tabi capture, set the source address to be held. */
47617467f23STimur Tabi mr |= (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
47717467f23STimur Tabi CCSR_DMA_MR_DAHE : CCSR_DMA_MR_SAHE;
47817467f23STimur Tabi
47917467f23STimur Tabi out_be32(&dma_channel->mr, mr);
48017467f23STimur Tabi
48117467f23STimur Tabi return 0;
48217467f23STimur Tabi }
48317467f23STimur Tabi
48417467f23STimur Tabi /**
485bf9c8c9dSTimur Tabi * fsl_dma_hw_params: continue initializing the DMA links
486bf9c8c9dSTimur Tabi *
487bf9c8c9dSTimur Tabi * This function obtains hardware parameters about the opened stream and
488bf9c8c9dSTimur Tabi * programs the DMA controller accordingly.
489bf9c8c9dSTimur Tabi *
49085ef2375STimur Tabi * One drawback of big-endian is that when copying integers of different
49185ef2375STimur Tabi * sizes to a fixed-sized register, the address to which the integer must be
49285ef2375STimur Tabi * copied is dependent on the size of the integer.
49317467f23STimur Tabi *
49417467f23STimur Tabi * For example, if P is the address of a 32-bit register, and X is a 32-bit
49517467f23STimur Tabi * integer, then X should be copied to address P. However, if X is a 16-bit
49617467f23STimur Tabi * integer, then it should be copied to P+2. If X is an 8-bit register,
49717467f23STimur Tabi * then it should be copied to P+3.
49817467f23STimur Tabi *
49917467f23STimur Tabi * So for playback of 8-bit samples, the DMA controller must transfer single
50017467f23STimur Tabi * bytes from the DMA buffer to the last byte of the STX0 register, i.e.
50117467f23STimur Tabi * offset by 3 bytes. For 16-bit samples, the offset is two bytes.
50217467f23STimur Tabi *
50317467f23STimur Tabi * For 24-bit samples, the offset is 1 byte. However, the DMA controller
50417467f23STimur Tabi * does not support 3-byte copies (the DAHTS register supports only 1, 2, 4,
50517467f23STimur Tabi * and 8 bytes at a time). So we do not support packed 24-bit samples.
50617467f23STimur Tabi * 24-bit data must be padded to 32 bits.
50717467f23STimur Tabi */
fsl_dma_hw_params(struct snd_soc_component * component,struct snd_pcm_substream * substream,struct snd_pcm_hw_params * hw_params)5084ac85de9SKuninori Morimoto static int fsl_dma_hw_params(struct snd_soc_component *component,
5094ac85de9SKuninori Morimoto struct snd_pcm_substream *substream,
51085ef2375STimur Tabi struct snd_pcm_hw_params *hw_params)
51117467f23STimur Tabi {
51217467f23STimur Tabi struct snd_pcm_runtime *runtime = substream->runtime;
51317467f23STimur Tabi struct fsl_dma_private *dma_private = runtime->private_data;
5144cb1ea92SKuninori Morimoto struct device *dev = component->dev;
51517467f23STimur Tabi
51685ef2375STimur Tabi /* Number of bits per sample */
5178e9d8690STimur Tabi unsigned int sample_bits =
51885ef2375STimur Tabi snd_pcm_format_physical_width(params_format(hw_params));
51985ef2375STimur Tabi
52085ef2375STimur Tabi /* Number of bytes per frame */
5218e9d8690STimur Tabi unsigned int sample_bytes = sample_bits / 8;
52285ef2375STimur Tabi
52385ef2375STimur Tabi /* Bus address of SSI STX register */
52485ef2375STimur Tabi dma_addr_t ssi_sxx_phys = dma_private->ssi_sxx_phys;
52585ef2375STimur Tabi
52685ef2375STimur Tabi /* Size of the DMA buffer, in bytes */
52785ef2375STimur Tabi size_t buffer_size = params_buffer_bytes(hw_params);
52885ef2375STimur Tabi
52985ef2375STimur Tabi /* Number of bytes per period */
53085ef2375STimur Tabi size_t period_size = params_period_bytes(hw_params);
53185ef2375STimur Tabi
53285ef2375STimur Tabi /* Pointer to next period */
53385ef2375STimur Tabi dma_addr_t temp_addr = substream->dma_buffer.addr;
53485ef2375STimur Tabi
53585ef2375STimur Tabi /* Pointer to DMA controller */
53685ef2375STimur Tabi struct ccsr_dma_channel __iomem *dma_channel = dma_private->dma_channel;
53785ef2375STimur Tabi
53885ef2375STimur Tabi u32 mr; /* DMA Mode Register */
53985ef2375STimur Tabi
54085ef2375STimur Tabi unsigned int i;
54185ef2375STimur Tabi
54285ef2375STimur Tabi /* Initialize our DMA tracking variables */
54385ef2375STimur Tabi dma_private->period_size = period_size;
54485ef2375STimur Tabi dma_private->num_periods = params_periods(hw_params);
54585ef2375STimur Tabi dma_private->dma_buf_end = dma_private->dma_buf_phys + buffer_size;
54685ef2375STimur Tabi dma_private->dma_buf_next = dma_private->dma_buf_phys +
54785ef2375STimur Tabi (NUM_DMA_LINKS * period_size);
54885ef2375STimur Tabi
54985ef2375STimur Tabi if (dma_private->dma_buf_next >= dma_private->dma_buf_end)
55085ef2375STimur Tabi /* This happens if the number of periods == NUM_DMA_LINKS */
55185ef2375STimur Tabi dma_private->dma_buf_next = dma_private->dma_buf_phys;
55217467f23STimur Tabi
55317467f23STimur Tabi mr = in_be32(&dma_channel->mr) & ~(CCSR_DMA_MR_BWC_MASK |
55417467f23STimur Tabi CCSR_DMA_MR_SAHTS_MASK | CCSR_DMA_MR_DAHTS_MASK);
55517467f23STimur Tabi
55685ef2375STimur Tabi /* Due to a quirk of the SSI's STX register, the target address
55785ef2375STimur Tabi * for the DMA operations depends on the sample size. So we calculate
55885ef2375STimur Tabi * that offset here. While we're at it, also tell the DMA controller
55985ef2375STimur Tabi * how much data to transfer per sample.
56085ef2375STimur Tabi */
5618e9d8690STimur Tabi switch (sample_bits) {
56217467f23STimur Tabi case 8:
56317467f23STimur Tabi mr |= CCSR_DMA_MR_DAHTS_1 | CCSR_DMA_MR_SAHTS_1;
56417467f23STimur Tabi ssi_sxx_phys += 3;
56517467f23STimur Tabi break;
56617467f23STimur Tabi case 16:
56717467f23STimur Tabi mr |= CCSR_DMA_MR_DAHTS_2 | CCSR_DMA_MR_SAHTS_2;
56817467f23STimur Tabi ssi_sxx_phys += 2;
56917467f23STimur Tabi break;
57017467f23STimur Tabi case 32:
57117467f23STimur Tabi mr |= CCSR_DMA_MR_DAHTS_4 | CCSR_DMA_MR_SAHTS_4;
57217467f23STimur Tabi break;
57317467f23STimur Tabi default:
57485ef2375STimur Tabi /* We should never get here */
5758e9d8690STimur Tabi dev_err(dev, "unsupported sample size %u\n", sample_bits);
57617467f23STimur Tabi return -EINVAL;
57717467f23STimur Tabi }
57817467f23STimur Tabi
57917467f23STimur Tabi /*
5808e9d8690STimur Tabi * BWC determines how many bytes are sent/received before the DMA
5818e9d8690STimur Tabi * controller checks the SSI to see if it needs to stop. BWC should
5828e9d8690STimur Tabi * always be a multiple of the frame size, so that we always transmit
5838e9d8690STimur Tabi * whole frames. Each frame occupies two slots in the FIFO. The
5848e9d8690STimur Tabi * parameter for CCSR_DMA_MR_BWC() is rounded down the next power of two
5858e9d8690STimur Tabi * (MR[BWC] can only represent even powers of two).
5868e9d8690STimur Tabi *
5878e9d8690STimur Tabi * To simplify the process, we set BWC to the largest value that is
5888e9d8690STimur Tabi * less than or equal to the FIFO watermark. For playback, this ensures
5898e9d8690STimur Tabi * that we transfer the maximum amount without overrunning the FIFO.
5908e9d8690STimur Tabi * For capture, this ensures that we transfer the maximum amount without
5918e9d8690STimur Tabi * underrunning the FIFO.
5928e9d8690STimur Tabi *
5938e9d8690STimur Tabi * f = SSI FIFO depth
5948e9d8690STimur Tabi * w = SSI watermark value (which equals f - 2)
5958e9d8690STimur Tabi * b = DMA bandwidth count (in bytes)
5968e9d8690STimur Tabi * s = sample size (in bytes, which equals frame_size * 2)
5978e9d8690STimur Tabi *
5988e9d8690STimur Tabi * For playback, we never transmit more than the transmit FIFO
5998e9d8690STimur Tabi * watermark, otherwise we might write more data than the FIFO can hold.
6008e9d8690STimur Tabi * The watermark is equal to the FIFO depth minus two.
6018e9d8690STimur Tabi *
6028e9d8690STimur Tabi * For capture, two equations must hold:
6038e9d8690STimur Tabi * w > f - (b / s)
6048e9d8690STimur Tabi * w >= b / s
6058e9d8690STimur Tabi *
6068e9d8690STimur Tabi * So, b > 2 * s, but b must also be <= s * w. To simplify, we set
6078e9d8690STimur Tabi * b = s * w, which is equal to
6088e9d8690STimur Tabi * (dma_private->ssi_fifo_depth - 2) * sample_bytes.
60917467f23STimur Tabi */
6108e9d8690STimur Tabi mr |= CCSR_DMA_MR_BWC((dma_private->ssi_fifo_depth - 2) * sample_bytes);
61117467f23STimur Tabi
61217467f23STimur Tabi out_be32(&dma_channel->mr, mr);
61317467f23STimur Tabi
61417467f23STimur Tabi for (i = 0; i < NUM_DMA_LINKS; i++) {
61517467f23STimur Tabi struct fsl_dma_link_descriptor *link = &dma_private->link[i];
61617467f23STimur Tabi
61785ef2375STimur Tabi link->count = cpu_to_be32(period_size);
61885ef2375STimur Tabi
6191a3c5a49STimur Tabi /* The snoop bit tells the DMA controller whether it should tell
62085ef2375STimur Tabi * the ECM to snoop during a read or write to an address. For
62185ef2375STimur Tabi * audio, we use DMA to transfer data between memory and an I/O
62285ef2375STimur Tabi * device (the SSI's STX0 or SRX0 register). Snooping is only
62385ef2375STimur Tabi * needed if there is a cache, so we need to snoop memory
62485ef2375STimur Tabi * addresses only. For playback, that means we snoop the source
62585ef2375STimur Tabi * but not the destination. For capture, we snoop the
62685ef2375STimur Tabi * destination but not the source.
62785ef2375STimur Tabi *
62885ef2375STimur Tabi * Note that failing to snoop properly is unlikely to cause
62985ef2375STimur Tabi * cache incoherency if the period size is larger than the
63085ef2375STimur Tabi * size of L1 cache. This is because filling in one period will
63185ef2375STimur Tabi * flush out the data for the previous period. So if you
63285ef2375STimur Tabi * increased period_bytes_min to a large enough size, you might
63385ef2375STimur Tabi * get more performance by not snooping, and you'll still be
6341a3c5a49STimur Tabi * okay. You'll need to update fsl_dma_update_pointers() also.
63585ef2375STimur Tabi */
63685ef2375STimur Tabi if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
63785ef2375STimur Tabi link->source_addr = cpu_to_be32(temp_addr);
6381a3c5a49STimur Tabi link->source_attr = cpu_to_be32(CCSR_DMA_ATR_SNOOP |
6391a3c5a49STimur Tabi upper_32_bits(temp_addr));
64085ef2375STimur Tabi
64117467f23STimur Tabi link->dest_addr = cpu_to_be32(ssi_sxx_phys);
6421a3c5a49STimur Tabi link->dest_attr = cpu_to_be32(CCSR_DMA_ATR_NOSNOOP |
6431a3c5a49STimur Tabi upper_32_bits(ssi_sxx_phys));
64485ef2375STimur Tabi } else {
64517467f23STimur Tabi link->source_addr = cpu_to_be32(ssi_sxx_phys);
6461a3c5a49STimur Tabi link->source_attr = cpu_to_be32(CCSR_DMA_ATR_NOSNOOP |
6471a3c5a49STimur Tabi upper_32_bits(ssi_sxx_phys));
64885ef2375STimur Tabi
64985ef2375STimur Tabi link->dest_addr = cpu_to_be32(temp_addr);
6501a3c5a49STimur Tabi link->dest_attr = cpu_to_be32(CCSR_DMA_ATR_SNOOP |
6511a3c5a49STimur Tabi upper_32_bits(temp_addr));
65285ef2375STimur Tabi }
65385ef2375STimur Tabi
65485ef2375STimur Tabi temp_addr += period_size;
65517467f23STimur Tabi }
65617467f23STimur Tabi
65717467f23STimur Tabi return 0;
65817467f23STimur Tabi }
65917467f23STimur Tabi
66017467f23STimur Tabi /**
66117467f23STimur Tabi * fsl_dma_pointer: determine the current position of the DMA transfer
66217467f23STimur Tabi *
66317467f23STimur Tabi * This function is called by ALSA when ALSA wants to know where in the
66417467f23STimur Tabi * stream buffer the hardware currently is.
66517467f23STimur Tabi *
66617467f23STimur Tabi * For playback, the SAR register contains the physical address of the most
66717467f23STimur Tabi * recent DMA transfer. For capture, the value is in the DAR register.
66817467f23STimur Tabi *
66917467f23STimur Tabi * The base address of the buffer is stored in the source_addr field of the
67017467f23STimur Tabi * first link descriptor.
67117467f23STimur Tabi */
fsl_dma_pointer(struct snd_soc_component * component,struct snd_pcm_substream * substream)6724ac85de9SKuninori Morimoto static snd_pcm_uframes_t fsl_dma_pointer(struct snd_soc_component *component,
6734ac85de9SKuninori Morimoto struct snd_pcm_substream *substream)
67417467f23STimur Tabi {
67517467f23STimur Tabi struct snd_pcm_runtime *runtime = substream->runtime;
67617467f23STimur Tabi struct fsl_dma_private *dma_private = runtime->private_data;
6774cb1ea92SKuninori Morimoto struct device *dev = component->dev;
67817467f23STimur Tabi struct ccsr_dma_channel __iomem *dma_channel = dma_private->dma_channel;
67917467f23STimur Tabi dma_addr_t position;
68017467f23STimur Tabi snd_pcm_uframes_t frames;
68117467f23STimur Tabi
6821a3c5a49STimur Tabi /* Obtain the current DMA pointer, but don't read the ESAD bits if we
6831a3c5a49STimur Tabi * only have 32-bit DMA addresses. This function is typically called
6841a3c5a49STimur Tabi * in interrupt context, so we need to optimize it.
6851a3c5a49STimur Tabi */
6861a3c5a49STimur Tabi if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
68717467f23STimur Tabi position = in_be32(&dma_channel->sar);
6881a3c5a49STimur Tabi #ifdef CONFIG_PHYS_64BIT
6891a3c5a49STimur Tabi position |= (u64)(in_be32(&dma_channel->satr) &
6901a3c5a49STimur Tabi CCSR_DMA_ATR_ESAD_MASK) << 32;
6911a3c5a49STimur Tabi #endif
6921a3c5a49STimur Tabi } else {
69317467f23STimur Tabi position = in_be32(&dma_channel->dar);
6941a3c5a49STimur Tabi #ifdef CONFIG_PHYS_64BIT
6951a3c5a49STimur Tabi position |= (u64)(in_be32(&dma_channel->datr) &
6961a3c5a49STimur Tabi CCSR_DMA_ATR_ESAD_MASK) << 32;
6971a3c5a49STimur Tabi #endif
6981a3c5a49STimur Tabi }
69917467f23STimur Tabi
700a4d11fe5STimur Tabi /*
701a4d11fe5STimur Tabi * When capture is started, the SSI immediately starts to fill its FIFO.
702a4d11fe5STimur Tabi * This means that the DMA controller is not started until the FIFO is
703a4d11fe5STimur Tabi * full. However, ALSA calls this function before that happens, when
704a4d11fe5STimur Tabi * MR.DAR is still zero. In this case, just return zero to indicate
705a4d11fe5STimur Tabi * that nothing has been received yet.
706a4d11fe5STimur Tabi */
707a4d11fe5STimur Tabi if (!position)
708a4d11fe5STimur Tabi return 0;
709a4d11fe5STimur Tabi
710a4d11fe5STimur Tabi if ((position < dma_private->dma_buf_phys) ||
711a4d11fe5STimur Tabi (position > dma_private->dma_buf_end)) {
712f0fba2adSLiam Girdwood dev_err(dev, "dma pointer is out of range, halting stream\n");
713a4d11fe5STimur Tabi return SNDRV_PCM_POS_XRUN;
714a4d11fe5STimur Tabi }
715a4d11fe5STimur Tabi
71617467f23STimur Tabi frames = bytes_to_frames(runtime, position - dma_private->dma_buf_phys);
71717467f23STimur Tabi
71817467f23STimur Tabi /*
71917467f23STimur Tabi * If the current address is just past the end of the buffer, wrap it
72017467f23STimur Tabi * around.
72117467f23STimur Tabi */
72217467f23STimur Tabi if (frames == runtime->buffer_size)
72317467f23STimur Tabi frames = 0;
72417467f23STimur Tabi
72517467f23STimur Tabi return frames;
72617467f23STimur Tabi }
72717467f23STimur Tabi
72817467f23STimur Tabi /**
72917467f23STimur Tabi * fsl_dma_hw_free: release resources allocated in fsl_dma_hw_params()
73017467f23STimur Tabi *
73117467f23STimur Tabi * Release the resources allocated in fsl_dma_hw_params() and de-program the
73217467f23STimur Tabi * registers.
73317467f23STimur Tabi *
73417467f23STimur Tabi * This function can be called multiple times.
73517467f23STimur Tabi */
fsl_dma_hw_free(struct snd_soc_component * component,struct snd_pcm_substream * substream)7364ac85de9SKuninori Morimoto static int fsl_dma_hw_free(struct snd_soc_component *component,
7374ac85de9SKuninori Morimoto struct snd_pcm_substream *substream)
73817467f23STimur Tabi {
73917467f23STimur Tabi struct snd_pcm_runtime *runtime = substream->runtime;
74017467f23STimur Tabi struct fsl_dma_private *dma_private = runtime->private_data;
74117467f23STimur Tabi
74217467f23STimur Tabi if (dma_private) {
74317467f23STimur Tabi struct ccsr_dma_channel __iomem *dma_channel;
74417467f23STimur Tabi
74517467f23STimur Tabi dma_channel = dma_private->dma_channel;
74617467f23STimur Tabi
74717467f23STimur Tabi /* Stop the DMA */
74817467f23STimur Tabi out_be32(&dma_channel->mr, CCSR_DMA_MR_CA);
74917467f23STimur Tabi out_be32(&dma_channel->mr, 0);
75017467f23STimur Tabi
75117467f23STimur Tabi /* Reset all the other registers */
75217467f23STimur Tabi out_be32(&dma_channel->sr, -1);
75317467f23STimur Tabi out_be32(&dma_channel->clndar, 0);
75417467f23STimur Tabi out_be32(&dma_channel->eclndar, 0);
75517467f23STimur Tabi out_be32(&dma_channel->satr, 0);
75617467f23STimur Tabi out_be32(&dma_channel->sar, 0);
75717467f23STimur Tabi out_be32(&dma_channel->datr, 0);
75817467f23STimur Tabi out_be32(&dma_channel->dar, 0);
75917467f23STimur Tabi out_be32(&dma_channel->bcr, 0);
76017467f23STimur Tabi out_be32(&dma_channel->nlndar, 0);
76117467f23STimur Tabi out_be32(&dma_channel->enlndar, 0);
76217467f23STimur Tabi }
76317467f23STimur Tabi
76417467f23STimur Tabi return 0;
76517467f23STimur Tabi }
76617467f23STimur Tabi
76717467f23STimur Tabi /**
76817467f23STimur Tabi * fsl_dma_close: close the stream.
76917467f23STimur Tabi */
fsl_dma_close(struct snd_soc_component * component,struct snd_pcm_substream * substream)7704ac85de9SKuninori Morimoto static int fsl_dma_close(struct snd_soc_component *component,
7714ac85de9SKuninori Morimoto struct snd_pcm_substream *substream)
77217467f23STimur Tabi {
77317467f23STimur Tabi struct snd_pcm_runtime *runtime = substream->runtime;
77417467f23STimur Tabi struct fsl_dma_private *dma_private = runtime->private_data;
7754cb1ea92SKuninori Morimoto struct device *dev = component->dev;
776f0fba2adSLiam Girdwood struct dma_object *dma =
7774cb1ea92SKuninori Morimoto container_of(component->driver, struct dma_object, dai);
77817467f23STimur Tabi
77917467f23STimur Tabi if (dma_private) {
78017467f23STimur Tabi if (dma_private->irq)
78117467f23STimur Tabi free_irq(dma_private->irq, dma_private);
78217467f23STimur Tabi
78317467f23STimur Tabi /* Deallocate the fsl_dma_private structure */
784f0fba2adSLiam Girdwood dma_free_coherent(dev, sizeof(struct fsl_dma_private),
78517467f23STimur Tabi dma_private, dma_private->ld_buf_phys);
78617467f23STimur Tabi substream->runtime->private_data = NULL;
78717467f23STimur Tabi }
78817467f23STimur Tabi
789d0657fe8SFabio Estevam dma->assigned = false;
79017467f23STimur Tabi
79117467f23STimur Tabi return 0;
79217467f23STimur Tabi }
79317467f23STimur Tabi
794f0fba2adSLiam Girdwood /**
79505004cb4SMatthew Garrett * find_ssi_node -- returns the SSI node that points to its DMA channel node
796f0fba2adSLiam Girdwood *
797f0fba2adSLiam Girdwood * Although this DMA driver attempts to operate independently of the other
798f0fba2adSLiam Girdwood * devices, it still needs to determine some information about the SSI device
799f0fba2adSLiam Girdwood * that it's working with. Unfortunately, the device tree does not contain
800f0fba2adSLiam Girdwood * a pointer from the DMA channel node to the SSI node -- the pointer goes the
801f0fba2adSLiam Girdwood * other way. So we need to scan the device tree for SSI nodes until we find
802f0fba2adSLiam Girdwood * the one that points to the given DMA channel node. It's ugly, but at least
803f0fba2adSLiam Girdwood * it's contained in this one function.
804f0fba2adSLiam Girdwood */
find_ssi_node(struct device_node * dma_channel_np)805f0fba2adSLiam Girdwood static struct device_node *find_ssi_node(struct device_node *dma_channel_np)
806f0fba2adSLiam Girdwood {
807f0fba2adSLiam Girdwood struct device_node *ssi_np, *np;
808f0fba2adSLiam Girdwood
809f0fba2adSLiam Girdwood for_each_compatible_node(ssi_np, NULL, "fsl,mpc8610-ssi") {
810f0fba2adSLiam Girdwood /* Check each DMA phandle to see if it points to us. We
811f0fba2adSLiam Girdwood * assume that device_node pointers are a valid comparison.
812f0fba2adSLiam Girdwood */
813f0fba2adSLiam Girdwood np = of_parse_phandle(ssi_np, "fsl,playback-dma", 0);
81481a081ffSTimur Tabi of_node_put(np);
815f0fba2adSLiam Girdwood if (np == dma_channel_np)
816f0fba2adSLiam Girdwood return ssi_np;
817f0fba2adSLiam Girdwood
818f0fba2adSLiam Girdwood np = of_parse_phandle(ssi_np, "fsl,capture-dma", 0);
81981a081ffSTimur Tabi of_node_put(np);
820f0fba2adSLiam Girdwood if (np == dma_channel_np)
821f0fba2adSLiam Girdwood return ssi_np;
822f0fba2adSLiam Girdwood }
823f0fba2adSLiam Girdwood
824f0fba2adSLiam Girdwood return NULL;
825f0fba2adSLiam Girdwood }
826f0fba2adSLiam Girdwood
fsl_soc_dma_probe(struct platform_device * pdev)827a0a3d518SBill Pemberton static int fsl_soc_dma_probe(struct platform_device *pdev)
82817467f23STimur Tabi {
829f0fba2adSLiam Girdwood struct dma_object *dma;
83038fec727STimur Tabi struct device_node *np = pdev->dev.of_node;
831f0fba2adSLiam Girdwood struct device_node *ssi_np;
832f0fba2adSLiam Girdwood struct resource res;
8338e9d8690STimur Tabi const uint32_t *iprop;
834f0fba2adSLiam Girdwood int ret;
83517467f23STimur Tabi
836f0fba2adSLiam Girdwood /* Find the SSI node that points to us. */
837f0fba2adSLiam Girdwood ssi_np = find_ssi_node(np);
838f0fba2adSLiam Girdwood if (!ssi_np) {
83938fec727STimur Tabi dev_err(&pdev->dev, "cannot find parent SSI node\n");
840f0fba2adSLiam Girdwood return -ENODEV;
841f0fba2adSLiam Girdwood }
842f0fba2adSLiam Girdwood
843f0fba2adSLiam Girdwood ret = of_address_to_resource(ssi_np, 0, &res);
844f0fba2adSLiam Girdwood if (ret) {
84506d15a2eSRob Herring dev_err(&pdev->dev, "could not determine resources for %pOF\n",
84606d15a2eSRob Herring ssi_np);
8478e9d8690STimur Tabi of_node_put(ssi_np);
848f0fba2adSLiam Girdwood return ret;
849f0fba2adSLiam Girdwood }
850f0fba2adSLiam Girdwood
851b1dc00abSRob Herring dma = kzalloc(sizeof(*dma), GFP_KERNEL);
852f0fba2adSLiam Girdwood if (!dma) {
8538e9d8690STimur Tabi of_node_put(ssi_np);
854f0fba2adSLiam Girdwood return -ENOMEM;
855f0fba2adSLiam Girdwood }
856f0fba2adSLiam Girdwood
8574cb1ea92SKuninori Morimoto dma->dai.name = DRV_NAME;
8584ac85de9SKuninori Morimoto dma->dai.open = fsl_dma_open;
8594ac85de9SKuninori Morimoto dma->dai.close = fsl_dma_close;
8604ac85de9SKuninori Morimoto dma->dai.hw_params = fsl_dma_hw_params;
8614ac85de9SKuninori Morimoto dma->dai.hw_free = fsl_dma_hw_free;
8624ac85de9SKuninori Morimoto dma->dai.pointer = fsl_dma_pointer;
8634ac85de9SKuninori Morimoto dma->dai.pcm_construct = fsl_dma_new;
864f0fba2adSLiam Girdwood
865f0fba2adSLiam Girdwood /* Store the SSI-specific information that we need */
866a5a86a7fSGuenter Roeck dma->ssi_stx_phys = res.start + REG_SSI_STX0;
867a5a86a7fSGuenter Roeck dma->ssi_srx_phys = res.start + REG_SSI_SRX0;
868f0fba2adSLiam Girdwood
8698e9d8690STimur Tabi iprop = of_get_property(ssi_np, "fsl,fifo-depth", NULL);
8708e9d8690STimur Tabi if (iprop)
871147dfe90STimur Tabi dma->ssi_fifo_depth = be32_to_cpup(iprop);
8728e9d8690STimur Tabi else
8738e9d8690STimur Tabi /* Older 8610 DTs didn't have the fifo-depth property */
8748e9d8690STimur Tabi dma->ssi_fifo_depth = 8;
8758e9d8690STimur Tabi
8768e9d8690STimur Tabi of_node_put(ssi_np);
8778e9d8690STimur Tabi
8784cb1ea92SKuninori Morimoto ret = devm_snd_soc_register_component(&pdev->dev, &dma->dai, NULL, 0);
879f0fba2adSLiam Girdwood if (ret) {
88038fec727STimur Tabi dev_err(&pdev->dev, "could not register platform\n");
881f0fba2adSLiam Girdwood kfree(dma);
882f0fba2adSLiam Girdwood return ret;
883f0fba2adSLiam Girdwood }
884f0fba2adSLiam Girdwood
885f0fba2adSLiam Girdwood dma->channel = of_iomap(np, 0);
886f0fba2adSLiam Girdwood dma->irq = irq_of_parse_and_map(np, 0);
88787a0632bSTimur Tabi
88838fec727STimur Tabi dev_set_drvdata(&pdev->dev, dma);
889f0fba2adSLiam Girdwood
89017467f23STimur Tabi return 0;
89117467f23STimur Tabi }
89217467f23STimur Tabi
fsl_soc_dma_remove(struct platform_device * pdev)893*2c8a5de6SUwe Kleine-König static void fsl_soc_dma_remove(struct platform_device *pdev)
894958e792cSMark Brown {
89538fec727STimur Tabi struct dma_object *dma = dev_get_drvdata(&pdev->dev);
896f0fba2adSLiam Girdwood
897f0fba2adSLiam Girdwood iounmap(dma->channel);
898f0fba2adSLiam Girdwood irq_dispose_mapping(dma->irq);
899f0fba2adSLiam Girdwood kfree(dma);
900f0fba2adSLiam Girdwood }
901f0fba2adSLiam Girdwood
902f0fba2adSLiam Girdwood static const struct of_device_id fsl_soc_dma_ids[] = {
903f0fba2adSLiam Girdwood { .compatible = "fsl,ssi-dma-channel", },
904f0fba2adSLiam Girdwood {}
905f0fba2adSLiam Girdwood };
906f0fba2adSLiam Girdwood MODULE_DEVICE_TABLE(of, fsl_soc_dma_ids);
907f0fba2adSLiam Girdwood
908f07eb223SGrant Likely static struct platform_driver fsl_soc_dma_driver = {
909f0fba2adSLiam Girdwood .driver = {
910f0fba2adSLiam Girdwood .name = "fsl-pcm-audio",
911f0fba2adSLiam Girdwood .of_match_table = fsl_soc_dma_ids,
912f0fba2adSLiam Girdwood },
913f0fba2adSLiam Girdwood .probe = fsl_soc_dma_probe,
914*2c8a5de6SUwe Kleine-König .remove_new = fsl_soc_dma_remove,
915f0fba2adSLiam Girdwood };
916f0fba2adSLiam Girdwood
917ba0a7e02SAxel Lin module_platform_driver(fsl_soc_dma_driver);
918958e792cSMark Brown
91917467f23STimur Tabi MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
920f0fba2adSLiam Girdwood MODULE_DESCRIPTION("Freescale Elo DMA ASoC PCM Driver");
921f0fba2adSLiam Girdwood MODULE_LICENSE("GPL v2");
922