117467f23STimur Tabi /* 217467f23STimur Tabi * Freescale DMA ALSA SoC PCM driver 317467f23STimur Tabi * 417467f23STimur Tabi * Author: Timur Tabi <timur@freescale.com> 517467f23STimur Tabi * 6f0fba2adSLiam Girdwood * Copyright 2007-2010 Freescale Semiconductor, Inc. 7f0fba2adSLiam Girdwood * 8f0fba2adSLiam Girdwood * This file is licensed under the terms of the GNU General Public License 9f0fba2adSLiam Girdwood * version 2. This program is licensed "as is" without any warranty of any 10f0fba2adSLiam Girdwood * kind, whether express or implied. 1117467f23STimur Tabi * 1217467f23STimur Tabi * This driver implements ASoC support for the Elo DMA controller, which is 1317467f23STimur Tabi * the DMA controller on Freescale 83xx, 85xx, and 86xx SOCs. In ALSA terms, 1417467f23STimur Tabi * the PCM driver is what handles the DMA buffer. 1517467f23STimur Tabi */ 1617467f23STimur Tabi 1717467f23STimur Tabi #include <linux/module.h> 1817467f23STimur Tabi #include <linux/init.h> 1917467f23STimur Tabi #include <linux/platform_device.h> 2017467f23STimur Tabi #include <linux/dma-mapping.h> 2117467f23STimur Tabi #include <linux/interrupt.h> 2217467f23STimur Tabi #include <linux/delay.h> 235a0e3ad6STejun Heo #include <linux/gfp.h> 245af50730SRob Herring #include <linux/of_address.h> 255af50730SRob Herring #include <linux/of_irq.h> 26f0fba2adSLiam Girdwood #include <linux/of_platform.h> 27f0fba2adSLiam Girdwood #include <linux/list.h> 2838fec727STimur Tabi #include <linux/slab.h> 2917467f23STimur Tabi 3017467f23STimur Tabi #include <sound/core.h> 3117467f23STimur Tabi #include <sound/pcm.h> 3217467f23STimur Tabi #include <sound/pcm_params.h> 3317467f23STimur Tabi #include <sound/soc.h> 3417467f23STimur Tabi 3517467f23STimur Tabi #include <asm/io.h> 3617467f23STimur Tabi 3717467f23STimur Tabi #include "fsl_dma.h" 38f0fba2adSLiam Girdwood #include "fsl_ssi.h" /* For the offset of stx0 and srx0 */ 3917467f23STimur Tabi 4017467f23STimur Tabi /* 4117467f23STimur Tabi * The formats that the DMA controller supports, which is anything 4217467f23STimur Tabi * that is 8, 16, or 32 bits. 4317467f23STimur Tabi */ 4417467f23STimur Tabi #define FSLDMA_PCM_FORMATS (SNDRV_PCM_FMTBIT_S8 | \ 4517467f23STimur Tabi SNDRV_PCM_FMTBIT_U8 | \ 4617467f23STimur Tabi SNDRV_PCM_FMTBIT_S16_LE | \ 4717467f23STimur Tabi SNDRV_PCM_FMTBIT_S16_BE | \ 4817467f23STimur Tabi SNDRV_PCM_FMTBIT_U16_LE | \ 4917467f23STimur Tabi SNDRV_PCM_FMTBIT_U16_BE | \ 5017467f23STimur Tabi SNDRV_PCM_FMTBIT_S24_LE | \ 5117467f23STimur Tabi SNDRV_PCM_FMTBIT_S24_BE | \ 5217467f23STimur Tabi SNDRV_PCM_FMTBIT_U24_LE | \ 5317467f23STimur Tabi SNDRV_PCM_FMTBIT_U24_BE | \ 5417467f23STimur Tabi SNDRV_PCM_FMTBIT_S32_LE | \ 5517467f23STimur Tabi SNDRV_PCM_FMTBIT_S32_BE | \ 5617467f23STimur Tabi SNDRV_PCM_FMTBIT_U32_LE | \ 5717467f23STimur Tabi SNDRV_PCM_FMTBIT_U32_BE) 58f0fba2adSLiam Girdwood struct dma_object { 59f0fba2adSLiam Girdwood struct snd_soc_platform_driver dai; 6017467f23STimur Tabi dma_addr_t ssi_stx_phys; 6117467f23STimur Tabi dma_addr_t ssi_srx_phys; 628e9d8690STimur Tabi unsigned int ssi_fifo_depth; 63f0fba2adSLiam Girdwood struct ccsr_dma_channel __iomem *channel; 64f0fba2adSLiam Girdwood unsigned int irq; 65f0fba2adSLiam Girdwood bool assigned; 66f0fba2adSLiam Girdwood }; 6717467f23STimur Tabi 6817467f23STimur Tabi /* 6917467f23STimur Tabi * The number of DMA links to use. Two is the bare minimum, but if you 7017467f23STimur Tabi * have really small links you might need more. 7117467f23STimur Tabi */ 7217467f23STimur Tabi #define NUM_DMA_LINKS 2 7317467f23STimur Tabi 7417467f23STimur Tabi /** fsl_dma_private: p-substream DMA data 7517467f23STimur Tabi * 7617467f23STimur Tabi * Each substream has a 1-to-1 association with a DMA channel. 7717467f23STimur Tabi * 7817467f23STimur Tabi * The link[] array is first because it needs to be aligned on a 32-byte 7917467f23STimur Tabi * boundary, so putting it first will ensure alignment without padding the 8017467f23STimur Tabi * structure. 8117467f23STimur Tabi * 8217467f23STimur Tabi * @link[]: array of link descriptors 8317467f23STimur Tabi * @dma_channel: pointer to the DMA channel's registers 8417467f23STimur Tabi * @irq: IRQ for this DMA channel 8517467f23STimur Tabi * @substream: pointer to the substream object, needed by the ISR 8617467f23STimur Tabi * @ssi_sxx_phys: bus address of the STX or SRX register to use 8717467f23STimur Tabi * @ld_buf_phys: physical address of the LD buffer 8817467f23STimur Tabi * @current_link: index into link[] of the link currently being processed 8917467f23STimur Tabi * @dma_buf_phys: physical address of the DMA buffer 9017467f23STimur Tabi * @dma_buf_next: physical address of the next period to process 9117467f23STimur Tabi * @dma_buf_end: physical address of the byte after the end of the DMA 9217467f23STimur Tabi * @buffer period_size: the size of a single period 9317467f23STimur Tabi * @num_periods: the number of periods in the DMA buffer 9417467f23STimur Tabi */ 9517467f23STimur Tabi struct fsl_dma_private { 9617467f23STimur Tabi struct fsl_dma_link_descriptor link[NUM_DMA_LINKS]; 9717467f23STimur Tabi struct ccsr_dma_channel __iomem *dma_channel; 9817467f23STimur Tabi unsigned int irq; 9917467f23STimur Tabi struct snd_pcm_substream *substream; 10017467f23STimur Tabi dma_addr_t ssi_sxx_phys; 1018e9d8690STimur Tabi unsigned int ssi_fifo_depth; 10217467f23STimur Tabi dma_addr_t ld_buf_phys; 10317467f23STimur Tabi unsigned int current_link; 10417467f23STimur Tabi dma_addr_t dma_buf_phys; 10517467f23STimur Tabi dma_addr_t dma_buf_next; 10617467f23STimur Tabi dma_addr_t dma_buf_end; 10717467f23STimur Tabi size_t period_size; 10817467f23STimur Tabi unsigned int num_periods; 10917467f23STimur Tabi }; 11017467f23STimur Tabi 11117467f23STimur Tabi /** 11217467f23STimur Tabi * fsl_dma_hardare: define characteristics of the PCM hardware. 11317467f23STimur Tabi * 11417467f23STimur Tabi * The PCM hardware is the Freescale DMA controller. This structure defines 11517467f23STimur Tabi * the capabilities of that hardware. 11617467f23STimur Tabi * 11717467f23STimur Tabi * Since the sampling rate and data format are not controlled by the DMA 11817467f23STimur Tabi * controller, we specify no limits for those values. The only exception is 11917467f23STimur Tabi * period_bytes_min, which is set to a reasonably low value to prevent the 12017467f23STimur Tabi * DMA controller from generating too many interrupts per second. 12117467f23STimur Tabi * 12217467f23STimur Tabi * Since each link descriptor has a 32-bit byte count field, we set 12317467f23STimur Tabi * period_bytes_max to the largest 32-bit number. We also have no maximum 12417467f23STimur Tabi * number of periods. 125be41e941STimur Tabi * 126be41e941STimur Tabi * Note that we specify SNDRV_PCM_INFO_JOINT_DUPLEX here, but only because a 127be41e941STimur Tabi * limitation in the SSI driver requires the sample rates for playback and 128be41e941STimur Tabi * capture to be the same. 12917467f23STimur Tabi */ 13017467f23STimur Tabi static const struct snd_pcm_hardware fsl_dma_hardware = { 13117467f23STimur Tabi 1324052ce4cSTimur Tabi .info = SNDRV_PCM_INFO_INTERLEAVED | 1334052ce4cSTimur Tabi SNDRV_PCM_INFO_MMAP | 134be41e941STimur Tabi SNDRV_PCM_INFO_MMAP_VALID | 1353a638ff2STimur Tabi SNDRV_PCM_INFO_JOINT_DUPLEX | 1363a638ff2STimur Tabi SNDRV_PCM_INFO_PAUSE, 13717467f23STimur Tabi .formats = FSLDMA_PCM_FORMATS, 13817467f23STimur Tabi .period_bytes_min = 512, /* A reasonable limit */ 13917467f23STimur Tabi .period_bytes_max = (u32) -1, 14017467f23STimur Tabi .periods_min = NUM_DMA_LINKS, 14117467f23STimur Tabi .periods_max = (unsigned int) -1, 14217467f23STimur Tabi .buffer_bytes_max = 128 * 1024, /* A reasonable limit */ 14317467f23STimur Tabi }; 14417467f23STimur Tabi 14517467f23STimur Tabi /** 14617467f23STimur Tabi * fsl_dma_abort_stream: tell ALSA that the DMA transfer has aborted 14717467f23STimur Tabi * 14817467f23STimur Tabi * This function should be called by the ISR whenever the DMA controller 14917467f23STimur Tabi * halts data transfer. 15017467f23STimur Tabi */ 15117467f23STimur Tabi static void fsl_dma_abort_stream(struct snd_pcm_substream *substream) 15217467f23STimur Tabi { 1531fb8510cSTakashi Iwai snd_pcm_stop_xrun(substream); 15417467f23STimur Tabi } 15517467f23STimur Tabi 15617467f23STimur Tabi /** 15717467f23STimur Tabi * fsl_dma_update_pointers - update LD pointers to point to the next period 15817467f23STimur Tabi * 15917467f23STimur Tabi * As each period is completed, this function changes the the link 16017467f23STimur Tabi * descriptor pointers for that period to point to the next period. 16117467f23STimur Tabi */ 16217467f23STimur Tabi static void fsl_dma_update_pointers(struct fsl_dma_private *dma_private) 16317467f23STimur Tabi { 16417467f23STimur Tabi struct fsl_dma_link_descriptor *link = 16517467f23STimur Tabi &dma_private->link[dma_private->current_link]; 16617467f23STimur Tabi 1671a3c5a49STimur Tabi /* Update our link descriptors to point to the next period. On a 36-bit 1681a3c5a49STimur Tabi * system, we also need to update the ESAD bits. We also set (keep) the 1691a3c5a49STimur Tabi * snoop bits. See the comments in fsl_dma_hw_params() about snooping. 1701a3c5a49STimur Tabi */ 1711a3c5a49STimur Tabi if (dma_private->substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 1721a3c5a49STimur Tabi link->source_addr = cpu_to_be32(dma_private->dma_buf_next); 1731a3c5a49STimur Tabi #ifdef CONFIG_PHYS_64BIT 1741a3c5a49STimur Tabi link->source_attr = cpu_to_be32(CCSR_DMA_ATR_SNOOP | 1751a3c5a49STimur Tabi upper_32_bits(dma_private->dma_buf_next)); 1761a3c5a49STimur Tabi #endif 1771a3c5a49STimur Tabi } else { 1781a3c5a49STimur Tabi link->dest_addr = cpu_to_be32(dma_private->dma_buf_next); 1791a3c5a49STimur Tabi #ifdef CONFIG_PHYS_64BIT 1801a3c5a49STimur Tabi link->dest_attr = cpu_to_be32(CCSR_DMA_ATR_SNOOP | 1811a3c5a49STimur Tabi upper_32_bits(dma_private->dma_buf_next)); 1821a3c5a49STimur Tabi #endif 1831a3c5a49STimur Tabi } 18417467f23STimur Tabi 18517467f23STimur Tabi /* Update our variables for next time */ 18617467f23STimur Tabi dma_private->dma_buf_next += dma_private->period_size; 18717467f23STimur Tabi 18817467f23STimur Tabi if (dma_private->dma_buf_next >= dma_private->dma_buf_end) 18917467f23STimur Tabi dma_private->dma_buf_next = dma_private->dma_buf_phys; 19017467f23STimur Tabi 19117467f23STimur Tabi if (++dma_private->current_link >= NUM_DMA_LINKS) 19217467f23STimur Tabi dma_private->current_link = 0; 19317467f23STimur Tabi } 19417467f23STimur Tabi 19517467f23STimur Tabi /** 19617467f23STimur Tabi * fsl_dma_isr: interrupt handler for the DMA controller 19717467f23STimur Tabi * 19817467f23STimur Tabi * @irq: IRQ of the DMA channel 19917467f23STimur Tabi * @dev_id: pointer to the dma_private structure for this DMA channel 20017467f23STimur Tabi */ 20117467f23STimur Tabi static irqreturn_t fsl_dma_isr(int irq, void *dev_id) 20217467f23STimur Tabi { 20317467f23STimur Tabi struct fsl_dma_private *dma_private = dev_id; 204f0fba2adSLiam Girdwood struct snd_pcm_substream *substream = dma_private->substream; 205f0fba2adSLiam Girdwood struct snd_soc_pcm_runtime *rtd = substream->private_data; 206f0fba2adSLiam Girdwood struct device *dev = rtd->platform->dev; 20717467f23STimur Tabi struct ccsr_dma_channel __iomem *dma_channel = dma_private->dma_channel; 20817467f23STimur Tabi irqreturn_t ret = IRQ_NONE; 20917467f23STimur Tabi u32 sr, sr2 = 0; 21017467f23STimur Tabi 21117467f23STimur Tabi /* We got an interrupt, so read the status register to see what we 21217467f23STimur Tabi were interrupted for. 21317467f23STimur Tabi */ 21417467f23STimur Tabi sr = in_be32(&dma_channel->sr); 21517467f23STimur Tabi 21617467f23STimur Tabi if (sr & CCSR_DMA_SR_TE) { 217f0fba2adSLiam Girdwood dev_err(dev, "dma transmit error\n"); 218f0fba2adSLiam Girdwood fsl_dma_abort_stream(substream); 21917467f23STimur Tabi sr2 |= CCSR_DMA_SR_TE; 22017467f23STimur Tabi ret = IRQ_HANDLED; 22117467f23STimur Tabi } 22217467f23STimur Tabi 22317467f23STimur Tabi if (sr & CCSR_DMA_SR_CH) 22417467f23STimur Tabi ret = IRQ_HANDLED; 22517467f23STimur Tabi 22617467f23STimur Tabi if (sr & CCSR_DMA_SR_PE) { 227f0fba2adSLiam Girdwood dev_err(dev, "dma programming error\n"); 228f0fba2adSLiam Girdwood fsl_dma_abort_stream(substream); 22917467f23STimur Tabi sr2 |= CCSR_DMA_SR_PE; 23017467f23STimur Tabi ret = IRQ_HANDLED; 23117467f23STimur Tabi } 23217467f23STimur Tabi 23317467f23STimur Tabi if (sr & CCSR_DMA_SR_EOLNI) { 23417467f23STimur Tabi sr2 |= CCSR_DMA_SR_EOLNI; 23517467f23STimur Tabi ret = IRQ_HANDLED; 23617467f23STimur Tabi } 23717467f23STimur Tabi 23817467f23STimur Tabi if (sr & CCSR_DMA_SR_CB) 23917467f23STimur Tabi ret = IRQ_HANDLED; 24017467f23STimur Tabi 24117467f23STimur Tabi if (sr & CCSR_DMA_SR_EOSI) { 24217467f23STimur Tabi /* Tell ALSA we completed a period. */ 24317467f23STimur Tabi snd_pcm_period_elapsed(substream); 24417467f23STimur Tabi 24517467f23STimur Tabi /* 24617467f23STimur Tabi * Update our link descriptors to point to the next period. We 24717467f23STimur Tabi * only need to do this if the number of periods is not equal to 24817467f23STimur Tabi * the number of links. 24917467f23STimur Tabi */ 25017467f23STimur Tabi if (dma_private->num_periods != NUM_DMA_LINKS) 25117467f23STimur Tabi fsl_dma_update_pointers(dma_private); 25217467f23STimur Tabi 25317467f23STimur Tabi sr2 |= CCSR_DMA_SR_EOSI; 25417467f23STimur Tabi ret = IRQ_HANDLED; 25517467f23STimur Tabi } 25617467f23STimur Tabi 25717467f23STimur Tabi if (sr & CCSR_DMA_SR_EOLSI) { 25817467f23STimur Tabi sr2 |= CCSR_DMA_SR_EOLSI; 25917467f23STimur Tabi ret = IRQ_HANDLED; 26017467f23STimur Tabi } 26117467f23STimur Tabi 26217467f23STimur Tabi /* Clear the bits that we set */ 26317467f23STimur Tabi if (sr2) 26417467f23STimur Tabi out_be32(&dma_channel->sr, sr2); 26517467f23STimur Tabi 26617467f23STimur Tabi return ret; 26717467f23STimur Tabi } 26817467f23STimur Tabi 26917467f23STimur Tabi /** 27017467f23STimur Tabi * fsl_dma_new: initialize this PCM driver. 27117467f23STimur Tabi * 27217467f23STimur Tabi * This function is called when the codec driver calls snd_soc_new_pcms(), 27387506549SMark Brown * once for each .dai_link in the machine driver's snd_soc_card 27417467f23STimur Tabi * structure. 2751a3c5a49STimur Tabi * 2761a3c5a49STimur Tabi * snd_dma_alloc_pages() is just a front-end to dma_alloc_coherent(), which 2771a3c5a49STimur Tabi * (currently) always allocates the DMA buffer in lowmem, even if GFP_HIGHMEM 2781a3c5a49STimur Tabi * is specified. Therefore, any DMA buffers we allocate will always be in low 2791a3c5a49STimur Tabi * memory, but we support for 36-bit physical addresses anyway. 2801a3c5a49STimur Tabi * 2811a3c5a49STimur Tabi * Regardless of where the memory is actually allocated, since the device can 2821a3c5a49STimur Tabi * technically DMA to any 36-bit address, we do need to set the DMA mask to 36. 28317467f23STimur Tabi */ 284552d1ef6SLiam Girdwood static int fsl_dma_new(struct snd_soc_pcm_runtime *rtd) 28517467f23STimur Tabi { 286552d1ef6SLiam Girdwood struct snd_card *card = rtd->card->snd_card; 287552d1ef6SLiam Girdwood struct snd_pcm *pcm = rtd->pcm; 28817467f23STimur Tabi int ret; 28917467f23STimur Tabi 290c9bd5e69SRussell King ret = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(36)); 291c9bd5e69SRussell King if (ret) 292c9bd5e69SRussell King return ret; 29317467f23STimur Tabi 294c04019d4STimur Tabi /* Some codecs have separate DAIs for playback and capture, so we 295c04019d4STimur Tabi * should allocate a DMA buffer only for the streams that are valid. 296c04019d4STimur Tabi */ 297c04019d4STimur Tabi 2986296914cSJoachim Eastwood if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) { 2995c15a686SAnton Vorontsov ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, card->dev, 30017467f23STimur Tabi fsl_dma_hardware.buffer_bytes_max, 3016296914cSJoachim Eastwood &pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->dma_buffer); 30217467f23STimur Tabi if (ret) { 303c04019d4STimur Tabi dev_err(card->dev, "can't alloc playback dma buffer\n"); 304f0fba2adSLiam Girdwood return ret; 30517467f23STimur Tabi } 306c04019d4STimur Tabi } 30717467f23STimur Tabi 3086296914cSJoachim Eastwood if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) { 3095c15a686SAnton Vorontsov ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, card->dev, 31017467f23STimur Tabi fsl_dma_hardware.buffer_bytes_max, 3116296914cSJoachim Eastwood &pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->dma_buffer); 31217467f23STimur Tabi if (ret) { 313c04019d4STimur Tabi dev_err(card->dev, "can't alloc capture dma buffer\n"); 3146296914cSJoachim Eastwood snd_dma_free_pages(&pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->dma_buffer); 315f0fba2adSLiam Girdwood return ret; 31617467f23STimur Tabi } 317c04019d4STimur Tabi } 31817467f23STimur Tabi 31917467f23STimur Tabi return 0; 32017467f23STimur Tabi } 32117467f23STimur Tabi 32217467f23STimur Tabi /** 32317467f23STimur Tabi * fsl_dma_open: open a new substream. 32417467f23STimur Tabi * 32517467f23STimur Tabi * Each substream has its own DMA buffer. 326bf9c8c9dSTimur Tabi * 327bf9c8c9dSTimur Tabi * ALSA divides the DMA buffer into N periods. We create NUM_DMA_LINKS link 328bf9c8c9dSTimur Tabi * descriptors that ping-pong from one period to the next. For example, if 329bf9c8c9dSTimur Tabi * there are six periods and two link descriptors, this is how they look 330bf9c8c9dSTimur Tabi * before playback starts: 331bf9c8c9dSTimur Tabi * 332bf9c8c9dSTimur Tabi * The last link descriptor 333bf9c8c9dSTimur Tabi * ____________ points back to the first 334bf9c8c9dSTimur Tabi * | | 335bf9c8c9dSTimur Tabi * V | 336bf9c8c9dSTimur Tabi * ___ ___ | 337bf9c8c9dSTimur Tabi * | |->| |->| 338bf9c8c9dSTimur Tabi * |___| |___| 339bf9c8c9dSTimur Tabi * | | 340bf9c8c9dSTimur Tabi * | | 341bf9c8c9dSTimur Tabi * V V 342bf9c8c9dSTimur Tabi * _________________________________________ 343bf9c8c9dSTimur Tabi * | | | | | | | The DMA buffer is 344bf9c8c9dSTimur Tabi * | | | | | | | divided into 6 parts 345bf9c8c9dSTimur Tabi * |______|______|______|______|______|______| 346bf9c8c9dSTimur Tabi * 347bf9c8c9dSTimur Tabi * and here's how they look after the first period is finished playing: 348bf9c8c9dSTimur Tabi * 349bf9c8c9dSTimur Tabi * ____________ 350bf9c8c9dSTimur Tabi * | | 351bf9c8c9dSTimur Tabi * V | 352bf9c8c9dSTimur Tabi * ___ ___ | 353bf9c8c9dSTimur Tabi * | |->| |->| 354bf9c8c9dSTimur Tabi * |___| |___| 355bf9c8c9dSTimur Tabi * | | 356bf9c8c9dSTimur Tabi * |______________ 357bf9c8c9dSTimur Tabi * | | 358bf9c8c9dSTimur Tabi * V V 359bf9c8c9dSTimur Tabi * _________________________________________ 360bf9c8c9dSTimur Tabi * | | | | | | | 361bf9c8c9dSTimur Tabi * | | | | | | | 362bf9c8c9dSTimur Tabi * |______|______|______|______|______|______| 363bf9c8c9dSTimur Tabi * 364bf9c8c9dSTimur Tabi * The first link descriptor now points to the third period. The DMA 365bf9c8c9dSTimur Tabi * controller is currently playing the second period. When it finishes, it 366bf9c8c9dSTimur Tabi * will jump back to the first descriptor and play the third period. 367bf9c8c9dSTimur Tabi * 368bf9c8c9dSTimur Tabi * There are four reasons we do this: 369bf9c8c9dSTimur Tabi * 370bf9c8c9dSTimur Tabi * 1. The only way to get the DMA controller to automatically restart the 371bf9c8c9dSTimur Tabi * transfer when it gets to the end of the buffer is to use chaining 372bf9c8c9dSTimur Tabi * mode. Basic direct mode doesn't offer that feature. 373bf9c8c9dSTimur Tabi * 2. We need to receive an interrupt at the end of every period. The DMA 374bf9c8c9dSTimur Tabi * controller can generate an interrupt at the end of every link transfer 375bf9c8c9dSTimur Tabi * (aka segment). Making each period into a DMA segment will give us the 376bf9c8c9dSTimur Tabi * interrupts we need. 377bf9c8c9dSTimur Tabi * 3. By creating only two link descriptors, regardless of the number of 378bf9c8c9dSTimur Tabi * periods, we do not need to reallocate the link descriptors if the 379bf9c8c9dSTimur Tabi * number of periods changes. 380bf9c8c9dSTimur Tabi * 4. All of the audio data is still stored in a single, contiguous DMA 381bf9c8c9dSTimur Tabi * buffer, which is what ALSA expects. We're just dividing it into 382bf9c8c9dSTimur Tabi * contiguous parts, and creating a link descriptor for each one. 38317467f23STimur Tabi */ 38417467f23STimur Tabi static int fsl_dma_open(struct snd_pcm_substream *substream) 38517467f23STimur Tabi { 38617467f23STimur Tabi struct snd_pcm_runtime *runtime = substream->runtime; 387f0fba2adSLiam Girdwood struct snd_soc_pcm_runtime *rtd = substream->private_data; 388f0fba2adSLiam Girdwood struct device *dev = rtd->platform->dev; 389f0fba2adSLiam Girdwood struct dma_object *dma = 390f0fba2adSLiam Girdwood container_of(rtd->platform->driver, struct dma_object, dai); 39117467f23STimur Tabi struct fsl_dma_private *dma_private; 392bf9c8c9dSTimur Tabi struct ccsr_dma_channel __iomem *dma_channel; 39317467f23STimur Tabi dma_addr_t ld_buf_phys; 394bf9c8c9dSTimur Tabi u64 temp_link; /* Pointer to next link descriptor */ 395bf9c8c9dSTimur Tabi u32 mr; 39617467f23STimur Tabi unsigned int channel; 39717467f23STimur Tabi int ret = 0; 398bf9c8c9dSTimur Tabi unsigned int i; 39917467f23STimur Tabi 40017467f23STimur Tabi /* 40117467f23STimur Tabi * Reject any DMA buffer whose size is not a multiple of the period 40217467f23STimur Tabi * size. We need to make sure that the DMA buffer can be evenly divided 40317467f23STimur Tabi * into periods. 40417467f23STimur Tabi */ 40517467f23STimur Tabi ret = snd_pcm_hw_constraint_integer(runtime, 40617467f23STimur Tabi SNDRV_PCM_HW_PARAM_PERIODS); 40717467f23STimur Tabi if (ret < 0) { 408f0fba2adSLiam Girdwood dev_err(dev, "invalid buffer size\n"); 40917467f23STimur Tabi return ret; 41017467f23STimur Tabi } 41117467f23STimur Tabi 41217467f23STimur Tabi channel = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ? 0 : 1; 41317467f23STimur Tabi 414f0fba2adSLiam Girdwood if (dma->assigned) { 415f0fba2adSLiam Girdwood dev_err(dev, "dma channel already assigned\n"); 41617467f23STimur Tabi return -EBUSY; 41717467f23STimur Tabi } 41817467f23STimur Tabi 419f0fba2adSLiam Girdwood dma_private = dma_alloc_coherent(dev, sizeof(struct fsl_dma_private), 420f0fba2adSLiam Girdwood &ld_buf_phys, GFP_KERNEL); 42117467f23STimur Tabi if (!dma_private) { 422f0fba2adSLiam Girdwood dev_err(dev, "can't allocate dma private data\n"); 42317467f23STimur Tabi return -ENOMEM; 42417467f23STimur Tabi } 42517467f23STimur Tabi if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 426f0fba2adSLiam Girdwood dma_private->ssi_sxx_phys = dma->ssi_stx_phys; 42717467f23STimur Tabi else 428f0fba2adSLiam Girdwood dma_private->ssi_sxx_phys = dma->ssi_srx_phys; 42917467f23STimur Tabi 4308e9d8690STimur Tabi dma_private->ssi_fifo_depth = dma->ssi_fifo_depth; 431f0fba2adSLiam Girdwood dma_private->dma_channel = dma->channel; 432f0fba2adSLiam Girdwood dma_private->irq = dma->irq; 43317467f23STimur Tabi dma_private->substream = substream; 43417467f23STimur Tabi dma_private->ld_buf_phys = ld_buf_phys; 43517467f23STimur Tabi dma_private->dma_buf_phys = substream->dma_buffer.addr; 43617467f23STimur Tabi 4370cd114ffSTimur Tabi ret = request_irq(dma_private->irq, fsl_dma_isr, 0, "fsldma-audio", 4380cd114ffSTimur Tabi dma_private); 43917467f23STimur Tabi if (ret) { 440f0fba2adSLiam Girdwood dev_err(dev, "can't register ISR for IRQ %u (ret=%i)\n", 44117467f23STimur Tabi dma_private->irq, ret); 442f0fba2adSLiam Girdwood dma_free_coherent(dev, sizeof(struct fsl_dma_private), 44317467f23STimur Tabi dma_private, dma_private->ld_buf_phys); 44417467f23STimur Tabi return ret; 44517467f23STimur Tabi } 44617467f23STimur Tabi 447d0657fe8SFabio Estevam dma->assigned = true; 44817467f23STimur Tabi 44917467f23STimur Tabi snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer); 45017467f23STimur Tabi snd_soc_set_runtime_hwparams(substream, &fsl_dma_hardware); 45117467f23STimur Tabi runtime->private_data = dma_private; 45217467f23STimur Tabi 453bf9c8c9dSTimur Tabi /* Program the fixed DMA controller parameters */ 45417467f23STimur Tabi 455bf9c8c9dSTimur Tabi dma_channel = dma_private->dma_channel; 45617467f23STimur Tabi 45717467f23STimur Tabi temp_link = dma_private->ld_buf_phys + 45817467f23STimur Tabi sizeof(struct fsl_dma_link_descriptor); 45917467f23STimur Tabi 46017467f23STimur Tabi for (i = 0; i < NUM_DMA_LINKS; i++) { 46185ef2375STimur Tabi dma_private->link[i].next = cpu_to_be64(temp_link); 46217467f23STimur Tabi 46317467f23STimur Tabi temp_link += sizeof(struct fsl_dma_link_descriptor); 46417467f23STimur Tabi } 46517467f23STimur Tabi /* The last link descriptor points to the first */ 46617467f23STimur Tabi dma_private->link[i - 1].next = cpu_to_be64(dma_private->ld_buf_phys); 46717467f23STimur Tabi 46817467f23STimur Tabi /* Tell the DMA controller where the first link descriptor is */ 46917467f23STimur Tabi out_be32(&dma_channel->clndar, 47017467f23STimur Tabi CCSR_DMA_CLNDAR_ADDR(dma_private->ld_buf_phys)); 47117467f23STimur Tabi out_be32(&dma_channel->eclndar, 47217467f23STimur Tabi CCSR_DMA_ECLNDAR_ADDR(dma_private->ld_buf_phys)); 47317467f23STimur Tabi 47417467f23STimur Tabi /* The manual says the BCR must be clear before enabling EMP */ 47517467f23STimur Tabi out_be32(&dma_channel->bcr, 0); 47617467f23STimur Tabi 47717467f23STimur Tabi /* 47817467f23STimur Tabi * Program the mode register for interrupts, external master control, 47917467f23STimur Tabi * and source/destination hold. Also clear the Channel Abort bit. 48017467f23STimur Tabi */ 48117467f23STimur Tabi mr = in_be32(&dma_channel->mr) & 48217467f23STimur Tabi ~(CCSR_DMA_MR_CA | CCSR_DMA_MR_DAHE | CCSR_DMA_MR_SAHE); 48317467f23STimur Tabi 48417467f23STimur Tabi /* 48517467f23STimur Tabi * We want External Master Start and External Master Pause enabled, 48617467f23STimur Tabi * because the SSI is controlling the DMA controller. We want the DMA 48717467f23STimur Tabi * controller to be set up in advance, and then we signal only the SSI 488bf9c8c9dSTimur Tabi * to start transferring. 48917467f23STimur Tabi * 49017467f23STimur Tabi * We want End-Of-Segment Interrupts enabled, because this will generate 49117467f23STimur Tabi * an interrupt at the end of each segment (each link descriptor 49217467f23STimur Tabi * represents one segment). Each DMA segment is the same thing as an 49317467f23STimur Tabi * ALSA period, so this is how we get an interrupt at the end of every 49417467f23STimur Tabi * period. 49517467f23STimur Tabi * 49617467f23STimur Tabi * We want Error Interrupt enabled, so that we can get an error if 49717467f23STimur Tabi * the DMA controller is mis-programmed somehow. 49817467f23STimur Tabi */ 49917467f23STimur Tabi mr |= CCSR_DMA_MR_EOSIE | CCSR_DMA_MR_EIE | CCSR_DMA_MR_EMP_EN | 50017467f23STimur Tabi CCSR_DMA_MR_EMS_EN; 50117467f23STimur Tabi 50217467f23STimur Tabi /* For playback, we want the destination address to be held. For 50317467f23STimur Tabi capture, set the source address to be held. */ 50417467f23STimur Tabi mr |= (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ? 50517467f23STimur Tabi CCSR_DMA_MR_DAHE : CCSR_DMA_MR_SAHE; 50617467f23STimur Tabi 50717467f23STimur Tabi out_be32(&dma_channel->mr, mr); 50817467f23STimur Tabi 50917467f23STimur Tabi return 0; 51017467f23STimur Tabi } 51117467f23STimur Tabi 51217467f23STimur Tabi /** 513bf9c8c9dSTimur Tabi * fsl_dma_hw_params: continue initializing the DMA links 514bf9c8c9dSTimur Tabi * 515bf9c8c9dSTimur Tabi * This function obtains hardware parameters about the opened stream and 516bf9c8c9dSTimur Tabi * programs the DMA controller accordingly. 517bf9c8c9dSTimur Tabi * 51885ef2375STimur Tabi * One drawback of big-endian is that when copying integers of different 51985ef2375STimur Tabi * sizes to a fixed-sized register, the address to which the integer must be 52085ef2375STimur Tabi * copied is dependent on the size of the integer. 52117467f23STimur Tabi * 52217467f23STimur Tabi * For example, if P is the address of a 32-bit register, and X is a 32-bit 52317467f23STimur Tabi * integer, then X should be copied to address P. However, if X is a 16-bit 52417467f23STimur Tabi * integer, then it should be copied to P+2. If X is an 8-bit register, 52517467f23STimur Tabi * then it should be copied to P+3. 52617467f23STimur Tabi * 52717467f23STimur Tabi * So for playback of 8-bit samples, the DMA controller must transfer single 52817467f23STimur Tabi * bytes from the DMA buffer to the last byte of the STX0 register, i.e. 52917467f23STimur Tabi * offset by 3 bytes. For 16-bit samples, the offset is two bytes. 53017467f23STimur Tabi * 53117467f23STimur Tabi * For 24-bit samples, the offset is 1 byte. However, the DMA controller 53217467f23STimur Tabi * does not support 3-byte copies (the DAHTS register supports only 1, 2, 4, 53317467f23STimur Tabi * and 8 bytes at a time). So we do not support packed 24-bit samples. 53417467f23STimur Tabi * 24-bit data must be padded to 32 bits. 53517467f23STimur Tabi */ 53685ef2375STimur Tabi static int fsl_dma_hw_params(struct snd_pcm_substream *substream, 53785ef2375STimur Tabi struct snd_pcm_hw_params *hw_params) 53817467f23STimur Tabi { 53917467f23STimur Tabi struct snd_pcm_runtime *runtime = substream->runtime; 54017467f23STimur Tabi struct fsl_dma_private *dma_private = runtime->private_data; 541f0fba2adSLiam Girdwood struct snd_soc_pcm_runtime *rtd = substream->private_data; 542f0fba2adSLiam Girdwood struct device *dev = rtd->platform->dev; 54317467f23STimur Tabi 54485ef2375STimur Tabi /* Number of bits per sample */ 5458e9d8690STimur Tabi unsigned int sample_bits = 54685ef2375STimur Tabi snd_pcm_format_physical_width(params_format(hw_params)); 54785ef2375STimur Tabi 54885ef2375STimur Tabi /* Number of bytes per frame */ 5498e9d8690STimur Tabi unsigned int sample_bytes = sample_bits / 8; 55085ef2375STimur Tabi 55185ef2375STimur Tabi /* Bus address of SSI STX register */ 55285ef2375STimur Tabi dma_addr_t ssi_sxx_phys = dma_private->ssi_sxx_phys; 55385ef2375STimur Tabi 55485ef2375STimur Tabi /* Size of the DMA buffer, in bytes */ 55585ef2375STimur Tabi size_t buffer_size = params_buffer_bytes(hw_params); 55685ef2375STimur Tabi 55785ef2375STimur Tabi /* Number of bytes per period */ 55885ef2375STimur Tabi size_t period_size = params_period_bytes(hw_params); 55985ef2375STimur Tabi 56085ef2375STimur Tabi /* Pointer to next period */ 56185ef2375STimur Tabi dma_addr_t temp_addr = substream->dma_buffer.addr; 56285ef2375STimur Tabi 56385ef2375STimur Tabi /* Pointer to DMA controller */ 56485ef2375STimur Tabi struct ccsr_dma_channel __iomem *dma_channel = dma_private->dma_channel; 56585ef2375STimur Tabi 56685ef2375STimur Tabi u32 mr; /* DMA Mode Register */ 56785ef2375STimur Tabi 56885ef2375STimur Tabi unsigned int i; 56985ef2375STimur Tabi 57085ef2375STimur Tabi /* Initialize our DMA tracking variables */ 57185ef2375STimur Tabi dma_private->period_size = period_size; 57285ef2375STimur Tabi dma_private->num_periods = params_periods(hw_params); 57385ef2375STimur Tabi dma_private->dma_buf_end = dma_private->dma_buf_phys + buffer_size; 57485ef2375STimur Tabi dma_private->dma_buf_next = dma_private->dma_buf_phys + 57585ef2375STimur Tabi (NUM_DMA_LINKS * period_size); 57685ef2375STimur Tabi 57785ef2375STimur Tabi if (dma_private->dma_buf_next >= dma_private->dma_buf_end) 57885ef2375STimur Tabi /* This happens if the number of periods == NUM_DMA_LINKS */ 57985ef2375STimur Tabi dma_private->dma_buf_next = dma_private->dma_buf_phys; 58017467f23STimur Tabi 58117467f23STimur Tabi mr = in_be32(&dma_channel->mr) & ~(CCSR_DMA_MR_BWC_MASK | 58217467f23STimur Tabi CCSR_DMA_MR_SAHTS_MASK | CCSR_DMA_MR_DAHTS_MASK); 58317467f23STimur Tabi 58485ef2375STimur Tabi /* Due to a quirk of the SSI's STX register, the target address 58585ef2375STimur Tabi * for the DMA operations depends on the sample size. So we calculate 58685ef2375STimur Tabi * that offset here. While we're at it, also tell the DMA controller 58785ef2375STimur Tabi * how much data to transfer per sample. 58885ef2375STimur Tabi */ 5898e9d8690STimur Tabi switch (sample_bits) { 59017467f23STimur Tabi case 8: 59117467f23STimur Tabi mr |= CCSR_DMA_MR_DAHTS_1 | CCSR_DMA_MR_SAHTS_1; 59217467f23STimur Tabi ssi_sxx_phys += 3; 59317467f23STimur Tabi break; 59417467f23STimur Tabi case 16: 59517467f23STimur Tabi mr |= CCSR_DMA_MR_DAHTS_2 | CCSR_DMA_MR_SAHTS_2; 59617467f23STimur Tabi ssi_sxx_phys += 2; 59717467f23STimur Tabi break; 59817467f23STimur Tabi case 32: 59917467f23STimur Tabi mr |= CCSR_DMA_MR_DAHTS_4 | CCSR_DMA_MR_SAHTS_4; 60017467f23STimur Tabi break; 60117467f23STimur Tabi default: 60285ef2375STimur Tabi /* We should never get here */ 6038e9d8690STimur Tabi dev_err(dev, "unsupported sample size %u\n", sample_bits); 60417467f23STimur Tabi return -EINVAL; 60517467f23STimur Tabi } 60617467f23STimur Tabi 60717467f23STimur Tabi /* 6088e9d8690STimur Tabi * BWC determines how many bytes are sent/received before the DMA 6098e9d8690STimur Tabi * controller checks the SSI to see if it needs to stop. BWC should 6108e9d8690STimur Tabi * always be a multiple of the frame size, so that we always transmit 6118e9d8690STimur Tabi * whole frames. Each frame occupies two slots in the FIFO. The 6128e9d8690STimur Tabi * parameter for CCSR_DMA_MR_BWC() is rounded down the next power of two 6138e9d8690STimur Tabi * (MR[BWC] can only represent even powers of two). 6148e9d8690STimur Tabi * 6158e9d8690STimur Tabi * To simplify the process, we set BWC to the largest value that is 6168e9d8690STimur Tabi * less than or equal to the FIFO watermark. For playback, this ensures 6178e9d8690STimur Tabi * that we transfer the maximum amount without overrunning the FIFO. 6188e9d8690STimur Tabi * For capture, this ensures that we transfer the maximum amount without 6198e9d8690STimur Tabi * underrunning the FIFO. 6208e9d8690STimur Tabi * 6218e9d8690STimur Tabi * f = SSI FIFO depth 6228e9d8690STimur Tabi * w = SSI watermark value (which equals f - 2) 6238e9d8690STimur Tabi * b = DMA bandwidth count (in bytes) 6248e9d8690STimur Tabi * s = sample size (in bytes, which equals frame_size * 2) 6258e9d8690STimur Tabi * 6268e9d8690STimur Tabi * For playback, we never transmit more than the transmit FIFO 6278e9d8690STimur Tabi * watermark, otherwise we might write more data than the FIFO can hold. 6288e9d8690STimur Tabi * The watermark is equal to the FIFO depth minus two. 6298e9d8690STimur Tabi * 6308e9d8690STimur Tabi * For capture, two equations must hold: 6318e9d8690STimur Tabi * w > f - (b / s) 6328e9d8690STimur Tabi * w >= b / s 6338e9d8690STimur Tabi * 6348e9d8690STimur Tabi * So, b > 2 * s, but b must also be <= s * w. To simplify, we set 6358e9d8690STimur Tabi * b = s * w, which is equal to 6368e9d8690STimur Tabi * (dma_private->ssi_fifo_depth - 2) * sample_bytes. 63717467f23STimur Tabi */ 6388e9d8690STimur Tabi mr |= CCSR_DMA_MR_BWC((dma_private->ssi_fifo_depth - 2) * sample_bytes); 63917467f23STimur Tabi 64017467f23STimur Tabi out_be32(&dma_channel->mr, mr); 64117467f23STimur Tabi 64217467f23STimur Tabi for (i = 0; i < NUM_DMA_LINKS; i++) { 64317467f23STimur Tabi struct fsl_dma_link_descriptor *link = &dma_private->link[i]; 64417467f23STimur Tabi 64585ef2375STimur Tabi link->count = cpu_to_be32(period_size); 64685ef2375STimur Tabi 6471a3c5a49STimur Tabi /* The snoop bit tells the DMA controller whether it should tell 64885ef2375STimur Tabi * the ECM to snoop during a read or write to an address. For 64985ef2375STimur Tabi * audio, we use DMA to transfer data between memory and an I/O 65085ef2375STimur Tabi * device (the SSI's STX0 or SRX0 register). Snooping is only 65185ef2375STimur Tabi * needed if there is a cache, so we need to snoop memory 65285ef2375STimur Tabi * addresses only. For playback, that means we snoop the source 65385ef2375STimur Tabi * but not the destination. For capture, we snoop the 65485ef2375STimur Tabi * destination but not the source. 65585ef2375STimur Tabi * 65685ef2375STimur Tabi * Note that failing to snoop properly is unlikely to cause 65785ef2375STimur Tabi * cache incoherency if the period size is larger than the 65885ef2375STimur Tabi * size of L1 cache. This is because filling in one period will 65985ef2375STimur Tabi * flush out the data for the previous period. So if you 66085ef2375STimur Tabi * increased period_bytes_min to a large enough size, you might 66185ef2375STimur Tabi * get more performance by not snooping, and you'll still be 6621a3c5a49STimur Tabi * okay. You'll need to update fsl_dma_update_pointers() also. 66385ef2375STimur Tabi */ 66485ef2375STimur Tabi if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 66585ef2375STimur Tabi link->source_addr = cpu_to_be32(temp_addr); 6661a3c5a49STimur Tabi link->source_attr = cpu_to_be32(CCSR_DMA_ATR_SNOOP | 6671a3c5a49STimur Tabi upper_32_bits(temp_addr)); 66885ef2375STimur Tabi 66917467f23STimur Tabi link->dest_addr = cpu_to_be32(ssi_sxx_phys); 6701a3c5a49STimur Tabi link->dest_attr = cpu_to_be32(CCSR_DMA_ATR_NOSNOOP | 6711a3c5a49STimur Tabi upper_32_bits(ssi_sxx_phys)); 67285ef2375STimur Tabi } else { 67317467f23STimur Tabi link->source_addr = cpu_to_be32(ssi_sxx_phys); 6741a3c5a49STimur Tabi link->source_attr = cpu_to_be32(CCSR_DMA_ATR_NOSNOOP | 6751a3c5a49STimur Tabi upper_32_bits(ssi_sxx_phys)); 67685ef2375STimur Tabi 67785ef2375STimur Tabi link->dest_addr = cpu_to_be32(temp_addr); 6781a3c5a49STimur Tabi link->dest_attr = cpu_to_be32(CCSR_DMA_ATR_SNOOP | 6791a3c5a49STimur Tabi upper_32_bits(temp_addr)); 68085ef2375STimur Tabi } 68185ef2375STimur Tabi 68285ef2375STimur Tabi temp_addr += period_size; 68317467f23STimur Tabi } 68417467f23STimur Tabi 68517467f23STimur Tabi return 0; 68617467f23STimur Tabi } 68717467f23STimur Tabi 68817467f23STimur Tabi /** 68917467f23STimur Tabi * fsl_dma_pointer: determine the current position of the DMA transfer 69017467f23STimur Tabi * 69117467f23STimur Tabi * This function is called by ALSA when ALSA wants to know where in the 69217467f23STimur Tabi * stream buffer the hardware currently is. 69317467f23STimur Tabi * 69417467f23STimur Tabi * For playback, the SAR register contains the physical address of the most 69517467f23STimur Tabi * recent DMA transfer. For capture, the value is in the DAR register. 69617467f23STimur Tabi * 69717467f23STimur Tabi * The base address of the buffer is stored in the source_addr field of the 69817467f23STimur Tabi * first link descriptor. 69917467f23STimur Tabi */ 70017467f23STimur Tabi static snd_pcm_uframes_t fsl_dma_pointer(struct snd_pcm_substream *substream) 70117467f23STimur Tabi { 70217467f23STimur Tabi struct snd_pcm_runtime *runtime = substream->runtime; 70317467f23STimur Tabi struct fsl_dma_private *dma_private = runtime->private_data; 704f0fba2adSLiam Girdwood struct snd_soc_pcm_runtime *rtd = substream->private_data; 705f0fba2adSLiam Girdwood struct device *dev = rtd->platform->dev; 70617467f23STimur Tabi struct ccsr_dma_channel __iomem *dma_channel = dma_private->dma_channel; 70717467f23STimur Tabi dma_addr_t position; 70817467f23STimur Tabi snd_pcm_uframes_t frames; 70917467f23STimur Tabi 7101a3c5a49STimur Tabi /* Obtain the current DMA pointer, but don't read the ESAD bits if we 7111a3c5a49STimur Tabi * only have 32-bit DMA addresses. This function is typically called 7121a3c5a49STimur Tabi * in interrupt context, so we need to optimize it. 7131a3c5a49STimur Tabi */ 7141a3c5a49STimur Tabi if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 71517467f23STimur Tabi position = in_be32(&dma_channel->sar); 7161a3c5a49STimur Tabi #ifdef CONFIG_PHYS_64BIT 7171a3c5a49STimur Tabi position |= (u64)(in_be32(&dma_channel->satr) & 7181a3c5a49STimur Tabi CCSR_DMA_ATR_ESAD_MASK) << 32; 7191a3c5a49STimur Tabi #endif 7201a3c5a49STimur Tabi } else { 72117467f23STimur Tabi position = in_be32(&dma_channel->dar); 7221a3c5a49STimur Tabi #ifdef CONFIG_PHYS_64BIT 7231a3c5a49STimur Tabi position |= (u64)(in_be32(&dma_channel->datr) & 7241a3c5a49STimur Tabi CCSR_DMA_ATR_ESAD_MASK) << 32; 7251a3c5a49STimur Tabi #endif 7261a3c5a49STimur Tabi } 72717467f23STimur Tabi 728a4d11fe5STimur Tabi /* 729a4d11fe5STimur Tabi * When capture is started, the SSI immediately starts to fill its FIFO. 730a4d11fe5STimur Tabi * This means that the DMA controller is not started until the FIFO is 731a4d11fe5STimur Tabi * full. However, ALSA calls this function before that happens, when 732a4d11fe5STimur Tabi * MR.DAR is still zero. In this case, just return zero to indicate 733a4d11fe5STimur Tabi * that nothing has been received yet. 734a4d11fe5STimur Tabi */ 735a4d11fe5STimur Tabi if (!position) 736a4d11fe5STimur Tabi return 0; 737a4d11fe5STimur Tabi 738a4d11fe5STimur Tabi if ((position < dma_private->dma_buf_phys) || 739a4d11fe5STimur Tabi (position > dma_private->dma_buf_end)) { 740f0fba2adSLiam Girdwood dev_err(dev, "dma pointer is out of range, halting stream\n"); 741a4d11fe5STimur Tabi return SNDRV_PCM_POS_XRUN; 742a4d11fe5STimur Tabi } 743a4d11fe5STimur Tabi 74417467f23STimur Tabi frames = bytes_to_frames(runtime, position - dma_private->dma_buf_phys); 74517467f23STimur Tabi 74617467f23STimur Tabi /* 74717467f23STimur Tabi * If the current address is just past the end of the buffer, wrap it 74817467f23STimur Tabi * around. 74917467f23STimur Tabi */ 75017467f23STimur Tabi if (frames == runtime->buffer_size) 75117467f23STimur Tabi frames = 0; 75217467f23STimur Tabi 75317467f23STimur Tabi return frames; 75417467f23STimur Tabi } 75517467f23STimur Tabi 75617467f23STimur Tabi /** 75717467f23STimur Tabi * fsl_dma_hw_free: release resources allocated in fsl_dma_hw_params() 75817467f23STimur Tabi * 75917467f23STimur Tabi * Release the resources allocated in fsl_dma_hw_params() and de-program the 76017467f23STimur Tabi * registers. 76117467f23STimur Tabi * 76217467f23STimur Tabi * This function can be called multiple times. 76317467f23STimur Tabi */ 76417467f23STimur Tabi static int fsl_dma_hw_free(struct snd_pcm_substream *substream) 76517467f23STimur Tabi { 76617467f23STimur Tabi struct snd_pcm_runtime *runtime = substream->runtime; 76717467f23STimur Tabi struct fsl_dma_private *dma_private = runtime->private_data; 76817467f23STimur Tabi 76917467f23STimur Tabi if (dma_private) { 77017467f23STimur Tabi struct ccsr_dma_channel __iomem *dma_channel; 77117467f23STimur Tabi 77217467f23STimur Tabi dma_channel = dma_private->dma_channel; 77317467f23STimur Tabi 77417467f23STimur Tabi /* Stop the DMA */ 77517467f23STimur Tabi out_be32(&dma_channel->mr, CCSR_DMA_MR_CA); 77617467f23STimur Tabi out_be32(&dma_channel->mr, 0); 77717467f23STimur Tabi 77817467f23STimur Tabi /* Reset all the other registers */ 77917467f23STimur Tabi out_be32(&dma_channel->sr, -1); 78017467f23STimur Tabi out_be32(&dma_channel->clndar, 0); 78117467f23STimur Tabi out_be32(&dma_channel->eclndar, 0); 78217467f23STimur Tabi out_be32(&dma_channel->satr, 0); 78317467f23STimur Tabi out_be32(&dma_channel->sar, 0); 78417467f23STimur Tabi out_be32(&dma_channel->datr, 0); 78517467f23STimur Tabi out_be32(&dma_channel->dar, 0); 78617467f23STimur Tabi out_be32(&dma_channel->bcr, 0); 78717467f23STimur Tabi out_be32(&dma_channel->nlndar, 0); 78817467f23STimur Tabi out_be32(&dma_channel->enlndar, 0); 78917467f23STimur Tabi } 79017467f23STimur Tabi 79117467f23STimur Tabi return 0; 79217467f23STimur Tabi } 79317467f23STimur Tabi 79417467f23STimur Tabi /** 79517467f23STimur Tabi * fsl_dma_close: close the stream. 79617467f23STimur Tabi */ 79717467f23STimur Tabi static int fsl_dma_close(struct snd_pcm_substream *substream) 79817467f23STimur Tabi { 79917467f23STimur Tabi struct snd_pcm_runtime *runtime = substream->runtime; 80017467f23STimur Tabi struct fsl_dma_private *dma_private = runtime->private_data; 801f0fba2adSLiam Girdwood struct snd_soc_pcm_runtime *rtd = substream->private_data; 802f0fba2adSLiam Girdwood struct device *dev = rtd->platform->dev; 803f0fba2adSLiam Girdwood struct dma_object *dma = 804f0fba2adSLiam Girdwood container_of(rtd->platform->driver, struct dma_object, dai); 80517467f23STimur Tabi 80617467f23STimur Tabi if (dma_private) { 80717467f23STimur Tabi if (dma_private->irq) 80817467f23STimur Tabi free_irq(dma_private->irq, dma_private); 80917467f23STimur Tabi 81017467f23STimur Tabi /* Deallocate the fsl_dma_private structure */ 811f0fba2adSLiam Girdwood dma_free_coherent(dev, sizeof(struct fsl_dma_private), 81217467f23STimur Tabi dma_private, dma_private->ld_buf_phys); 81317467f23STimur Tabi substream->runtime->private_data = NULL; 81417467f23STimur Tabi } 81517467f23STimur Tabi 816d0657fe8SFabio Estevam dma->assigned = false; 81717467f23STimur Tabi 81817467f23STimur Tabi return 0; 81917467f23STimur Tabi } 82017467f23STimur Tabi 82117467f23STimur Tabi /* 82217467f23STimur Tabi * Remove this PCM driver. 82317467f23STimur Tabi */ 82417467f23STimur Tabi static void fsl_dma_free_dma_buffers(struct snd_pcm *pcm) 82517467f23STimur Tabi { 82617467f23STimur Tabi struct snd_pcm_substream *substream; 82717467f23STimur Tabi unsigned int i; 82817467f23STimur Tabi 82917467f23STimur Tabi for (i = 0; i < ARRAY_SIZE(pcm->streams); i++) { 83017467f23STimur Tabi substream = pcm->streams[i].substream; 83117467f23STimur Tabi if (substream) { 83217467f23STimur Tabi snd_dma_free_pages(&substream->dma_buffer); 83317467f23STimur Tabi substream->dma_buffer.area = NULL; 83417467f23STimur Tabi substream->dma_buffer.addr = 0; 83517467f23STimur Tabi } 83617467f23STimur Tabi } 83717467f23STimur Tabi } 83817467f23STimur Tabi 839f0fba2adSLiam Girdwood /** 84005004cb4SMatthew Garrett * find_ssi_node -- returns the SSI node that points to its DMA channel node 841f0fba2adSLiam Girdwood * 842f0fba2adSLiam Girdwood * Although this DMA driver attempts to operate independently of the other 843f0fba2adSLiam Girdwood * devices, it still needs to determine some information about the SSI device 844f0fba2adSLiam Girdwood * that it's working with. Unfortunately, the device tree does not contain 845f0fba2adSLiam Girdwood * a pointer from the DMA channel node to the SSI node -- the pointer goes the 846f0fba2adSLiam Girdwood * other way. So we need to scan the device tree for SSI nodes until we find 847f0fba2adSLiam Girdwood * the one that points to the given DMA channel node. It's ugly, but at least 848f0fba2adSLiam Girdwood * it's contained in this one function. 849f0fba2adSLiam Girdwood */ 850f0fba2adSLiam Girdwood static struct device_node *find_ssi_node(struct device_node *dma_channel_np) 851f0fba2adSLiam Girdwood { 852f0fba2adSLiam Girdwood struct device_node *ssi_np, *np; 853f0fba2adSLiam Girdwood 854f0fba2adSLiam Girdwood for_each_compatible_node(ssi_np, NULL, "fsl,mpc8610-ssi") { 855f0fba2adSLiam Girdwood /* Check each DMA phandle to see if it points to us. We 856f0fba2adSLiam Girdwood * assume that device_node pointers are a valid comparison. 857f0fba2adSLiam Girdwood */ 858f0fba2adSLiam Girdwood np = of_parse_phandle(ssi_np, "fsl,playback-dma", 0); 85981a081ffSTimur Tabi of_node_put(np); 860f0fba2adSLiam Girdwood if (np == dma_channel_np) 861f0fba2adSLiam Girdwood return ssi_np; 862f0fba2adSLiam Girdwood 863f0fba2adSLiam Girdwood np = of_parse_phandle(ssi_np, "fsl,capture-dma", 0); 86481a081ffSTimur Tabi of_node_put(np); 865f0fba2adSLiam Girdwood if (np == dma_channel_np) 866f0fba2adSLiam Girdwood return ssi_np; 867f0fba2adSLiam Girdwood } 868f0fba2adSLiam Girdwood 869f0fba2adSLiam Girdwood return NULL; 870f0fba2adSLiam Girdwood } 871f0fba2adSLiam Girdwood 872b6ed0720SArvind Yadav static const struct snd_pcm_ops fsl_dma_ops = { 87317467f23STimur Tabi .open = fsl_dma_open, 87417467f23STimur Tabi .close = fsl_dma_close, 87517467f23STimur Tabi .ioctl = snd_pcm_lib_ioctl, 87617467f23STimur Tabi .hw_params = fsl_dma_hw_params, 87717467f23STimur Tabi .hw_free = fsl_dma_hw_free, 87817467f23STimur Tabi .pointer = fsl_dma_pointer, 87917467f23STimur Tabi }; 88017467f23STimur Tabi 881a0a3d518SBill Pemberton static int fsl_soc_dma_probe(struct platform_device *pdev) 88217467f23STimur Tabi { 883f0fba2adSLiam Girdwood struct dma_object *dma; 88438fec727STimur Tabi struct device_node *np = pdev->dev.of_node; 885f0fba2adSLiam Girdwood struct device_node *ssi_np; 886f0fba2adSLiam Girdwood struct resource res; 8878e9d8690STimur Tabi const uint32_t *iprop; 888f0fba2adSLiam Girdwood int ret; 88917467f23STimur Tabi 890f0fba2adSLiam Girdwood /* Find the SSI node that points to us. */ 891f0fba2adSLiam Girdwood ssi_np = find_ssi_node(np); 892f0fba2adSLiam Girdwood if (!ssi_np) { 89338fec727STimur Tabi dev_err(&pdev->dev, "cannot find parent SSI node\n"); 894f0fba2adSLiam Girdwood return -ENODEV; 895f0fba2adSLiam Girdwood } 896f0fba2adSLiam Girdwood 897f0fba2adSLiam Girdwood ret = of_address_to_resource(ssi_np, 0, &res); 898f0fba2adSLiam Girdwood if (ret) { 89906d15a2eSRob Herring dev_err(&pdev->dev, "could not determine resources for %pOF\n", 90006d15a2eSRob Herring ssi_np); 9018e9d8690STimur Tabi of_node_put(ssi_np); 902f0fba2adSLiam Girdwood return ret; 903f0fba2adSLiam Girdwood } 904f0fba2adSLiam Girdwood 905*b1dc00abSRob Herring dma = kzalloc(sizeof(*dma), GFP_KERNEL); 906f0fba2adSLiam Girdwood if (!dma) { 9078e9d8690STimur Tabi of_node_put(ssi_np); 908f0fba2adSLiam Girdwood return -ENOMEM; 909f0fba2adSLiam Girdwood } 910f0fba2adSLiam Girdwood 911f0fba2adSLiam Girdwood dma->dai.ops = &fsl_dma_ops; 912f0fba2adSLiam Girdwood dma->dai.pcm_new = fsl_dma_new; 913f0fba2adSLiam Girdwood dma->dai.pcm_free = fsl_dma_free_dma_buffers; 914f0fba2adSLiam Girdwood 915f0fba2adSLiam Girdwood /* Store the SSI-specific information that we need */ 9163d5f615fSGuenter Roeck dma->ssi_stx_phys = res.start + CCSR_SSI_STX0; 9173d5f615fSGuenter Roeck dma->ssi_srx_phys = res.start + CCSR_SSI_SRX0; 918f0fba2adSLiam Girdwood 9198e9d8690STimur Tabi iprop = of_get_property(ssi_np, "fsl,fifo-depth", NULL); 9208e9d8690STimur Tabi if (iprop) 921147dfe90STimur Tabi dma->ssi_fifo_depth = be32_to_cpup(iprop); 9228e9d8690STimur Tabi else 9238e9d8690STimur Tabi /* Older 8610 DTs didn't have the fifo-depth property */ 9248e9d8690STimur Tabi dma->ssi_fifo_depth = 8; 9258e9d8690STimur Tabi 9268e9d8690STimur Tabi of_node_put(ssi_np); 9278e9d8690STimur Tabi 92838fec727STimur Tabi ret = snd_soc_register_platform(&pdev->dev, &dma->dai); 929f0fba2adSLiam Girdwood if (ret) { 93038fec727STimur Tabi dev_err(&pdev->dev, "could not register platform\n"); 931f0fba2adSLiam Girdwood kfree(dma); 932f0fba2adSLiam Girdwood return ret; 933f0fba2adSLiam Girdwood } 934f0fba2adSLiam Girdwood 935f0fba2adSLiam Girdwood dma->channel = of_iomap(np, 0); 936f0fba2adSLiam Girdwood dma->irq = irq_of_parse_and_map(np, 0); 93787a0632bSTimur Tabi 93838fec727STimur Tabi dev_set_drvdata(&pdev->dev, dma); 939f0fba2adSLiam Girdwood 94017467f23STimur Tabi return 0; 94117467f23STimur Tabi } 94217467f23STimur Tabi 943a0a3d518SBill Pemberton static int fsl_soc_dma_remove(struct platform_device *pdev) 944958e792cSMark Brown { 94538fec727STimur Tabi struct dma_object *dma = dev_get_drvdata(&pdev->dev); 946f0fba2adSLiam Girdwood 94738fec727STimur Tabi snd_soc_unregister_platform(&pdev->dev); 948f0fba2adSLiam Girdwood iounmap(dma->channel); 949f0fba2adSLiam Girdwood irq_dispose_mapping(dma->irq); 950f0fba2adSLiam Girdwood kfree(dma); 951f0fba2adSLiam Girdwood 952f0fba2adSLiam Girdwood return 0; 953f0fba2adSLiam Girdwood } 954f0fba2adSLiam Girdwood 955f0fba2adSLiam Girdwood static const struct of_device_id fsl_soc_dma_ids[] = { 956f0fba2adSLiam Girdwood { .compatible = "fsl,ssi-dma-channel", }, 957f0fba2adSLiam Girdwood {} 958f0fba2adSLiam Girdwood }; 959f0fba2adSLiam Girdwood MODULE_DEVICE_TABLE(of, fsl_soc_dma_ids); 960f0fba2adSLiam Girdwood 961f07eb223SGrant Likely static struct platform_driver fsl_soc_dma_driver = { 962f0fba2adSLiam Girdwood .driver = { 963f0fba2adSLiam Girdwood .name = "fsl-pcm-audio", 964f0fba2adSLiam Girdwood .of_match_table = fsl_soc_dma_ids, 965f0fba2adSLiam Girdwood }, 966f0fba2adSLiam Girdwood .probe = fsl_soc_dma_probe, 967a0a3d518SBill Pemberton .remove = fsl_soc_dma_remove, 968f0fba2adSLiam Girdwood }; 969f0fba2adSLiam Girdwood 970ba0a7e02SAxel Lin module_platform_driver(fsl_soc_dma_driver); 971958e792cSMark Brown 97217467f23STimur Tabi MODULE_AUTHOR("Timur Tabi <timur@freescale.com>"); 973f0fba2adSLiam Girdwood MODULE_DESCRIPTION("Freescale Elo DMA ASoC PCM Driver"); 974f0fba2adSLiam Girdwood MODULE_LICENSE("GPL v2"); 975