xref: /OK3568_Linux_fs/kernel/sound/soc/fsl/fsl_dma.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun //
3*4882a593Smuzhiyun // Freescale DMA ALSA SoC PCM driver
4*4882a593Smuzhiyun //
5*4882a593Smuzhiyun // Author: Timur Tabi <timur@freescale.com>
6*4882a593Smuzhiyun //
7*4882a593Smuzhiyun // Copyright 2007-2010 Freescale Semiconductor, Inc.
8*4882a593Smuzhiyun //
9*4882a593Smuzhiyun // This driver implements ASoC support for the Elo DMA controller, which is
10*4882a593Smuzhiyun // the DMA controller on Freescale 83xx, 85xx, and 86xx SOCs. In ALSA terms,
11*4882a593Smuzhiyun // the PCM driver is what handles the DMA buffer.
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun #include <linux/module.h>
14*4882a593Smuzhiyun #include <linux/init.h>
15*4882a593Smuzhiyun #include <linux/platform_device.h>
16*4882a593Smuzhiyun #include <linux/dma-mapping.h>
17*4882a593Smuzhiyun #include <linux/interrupt.h>
18*4882a593Smuzhiyun #include <linux/delay.h>
19*4882a593Smuzhiyun #include <linux/gfp.h>
20*4882a593Smuzhiyun #include <linux/of_address.h>
21*4882a593Smuzhiyun #include <linux/of_irq.h>
22*4882a593Smuzhiyun #include <linux/of_platform.h>
23*4882a593Smuzhiyun #include <linux/list.h>
24*4882a593Smuzhiyun #include <linux/slab.h>
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun #include <sound/core.h>
27*4882a593Smuzhiyun #include <sound/pcm.h>
28*4882a593Smuzhiyun #include <sound/pcm_params.h>
29*4882a593Smuzhiyun #include <sound/soc.h>
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun #include <asm/io.h>
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun #include "fsl_dma.h"
34*4882a593Smuzhiyun #include "fsl_ssi.h"	/* For the offset of stx0 and srx0 */
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun #define DRV_NAME "fsl_dma"
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun /*
39*4882a593Smuzhiyun  * The formats that the DMA controller supports, which is anything
40*4882a593Smuzhiyun  * that is 8, 16, or 32 bits.
41*4882a593Smuzhiyun  */
42*4882a593Smuzhiyun #define FSLDMA_PCM_FORMATS (SNDRV_PCM_FMTBIT_S8 	| \
43*4882a593Smuzhiyun 			    SNDRV_PCM_FMTBIT_U8 	| \
44*4882a593Smuzhiyun 			    SNDRV_PCM_FMTBIT_S16_LE     | \
45*4882a593Smuzhiyun 			    SNDRV_PCM_FMTBIT_S16_BE     | \
46*4882a593Smuzhiyun 			    SNDRV_PCM_FMTBIT_U16_LE     | \
47*4882a593Smuzhiyun 			    SNDRV_PCM_FMTBIT_U16_BE     | \
48*4882a593Smuzhiyun 			    SNDRV_PCM_FMTBIT_S24_LE     | \
49*4882a593Smuzhiyun 			    SNDRV_PCM_FMTBIT_S24_BE     | \
50*4882a593Smuzhiyun 			    SNDRV_PCM_FMTBIT_U24_LE     | \
51*4882a593Smuzhiyun 			    SNDRV_PCM_FMTBIT_U24_BE     | \
52*4882a593Smuzhiyun 			    SNDRV_PCM_FMTBIT_S32_LE     | \
53*4882a593Smuzhiyun 			    SNDRV_PCM_FMTBIT_S32_BE     | \
54*4882a593Smuzhiyun 			    SNDRV_PCM_FMTBIT_U32_LE     | \
55*4882a593Smuzhiyun 			    SNDRV_PCM_FMTBIT_U32_BE)
56*4882a593Smuzhiyun struct dma_object {
57*4882a593Smuzhiyun 	struct snd_soc_component_driver dai;
58*4882a593Smuzhiyun 	dma_addr_t ssi_stx_phys;
59*4882a593Smuzhiyun 	dma_addr_t ssi_srx_phys;
60*4882a593Smuzhiyun 	unsigned int ssi_fifo_depth;
61*4882a593Smuzhiyun 	struct ccsr_dma_channel __iomem *channel;
62*4882a593Smuzhiyun 	unsigned int irq;
63*4882a593Smuzhiyun 	bool assigned;
64*4882a593Smuzhiyun };
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun /*
67*4882a593Smuzhiyun  * The number of DMA links to use.  Two is the bare minimum, but if you
68*4882a593Smuzhiyun  * have really small links you might need more.
69*4882a593Smuzhiyun  */
70*4882a593Smuzhiyun #define NUM_DMA_LINKS   2
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun /** fsl_dma_private: p-substream DMA data
73*4882a593Smuzhiyun  *
74*4882a593Smuzhiyun  * Each substream has a 1-to-1 association with a DMA channel.
75*4882a593Smuzhiyun  *
76*4882a593Smuzhiyun  * The link[] array is first because it needs to be aligned on a 32-byte
77*4882a593Smuzhiyun  * boundary, so putting it first will ensure alignment without padding the
78*4882a593Smuzhiyun  * structure.
79*4882a593Smuzhiyun  *
80*4882a593Smuzhiyun  * @link[]: array of link descriptors
81*4882a593Smuzhiyun  * @dma_channel: pointer to the DMA channel's registers
82*4882a593Smuzhiyun  * @irq: IRQ for this DMA channel
83*4882a593Smuzhiyun  * @substream: pointer to the substream object, needed by the ISR
84*4882a593Smuzhiyun  * @ssi_sxx_phys: bus address of the STX or SRX register to use
85*4882a593Smuzhiyun  * @ld_buf_phys: physical address of the LD buffer
86*4882a593Smuzhiyun  * @current_link: index into link[] of the link currently being processed
87*4882a593Smuzhiyun  * @dma_buf_phys: physical address of the DMA buffer
88*4882a593Smuzhiyun  * @dma_buf_next: physical address of the next period to process
89*4882a593Smuzhiyun  * @dma_buf_end: physical address of the byte after the end of the DMA
90*4882a593Smuzhiyun  * @buffer period_size: the size of a single period
91*4882a593Smuzhiyun  * @num_periods: the number of periods in the DMA buffer
92*4882a593Smuzhiyun  */
93*4882a593Smuzhiyun struct fsl_dma_private {
94*4882a593Smuzhiyun 	struct fsl_dma_link_descriptor link[NUM_DMA_LINKS];
95*4882a593Smuzhiyun 	struct ccsr_dma_channel __iomem *dma_channel;
96*4882a593Smuzhiyun 	unsigned int irq;
97*4882a593Smuzhiyun 	struct snd_pcm_substream *substream;
98*4882a593Smuzhiyun 	dma_addr_t ssi_sxx_phys;
99*4882a593Smuzhiyun 	unsigned int ssi_fifo_depth;
100*4882a593Smuzhiyun 	dma_addr_t ld_buf_phys;
101*4882a593Smuzhiyun 	unsigned int current_link;
102*4882a593Smuzhiyun 	dma_addr_t dma_buf_phys;
103*4882a593Smuzhiyun 	dma_addr_t dma_buf_next;
104*4882a593Smuzhiyun 	dma_addr_t dma_buf_end;
105*4882a593Smuzhiyun 	size_t period_size;
106*4882a593Smuzhiyun 	unsigned int num_periods;
107*4882a593Smuzhiyun };
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun /**
110*4882a593Smuzhiyun  * fsl_dma_hardare: define characteristics of the PCM hardware.
111*4882a593Smuzhiyun  *
112*4882a593Smuzhiyun  * The PCM hardware is the Freescale DMA controller.  This structure defines
113*4882a593Smuzhiyun  * the capabilities of that hardware.
114*4882a593Smuzhiyun  *
115*4882a593Smuzhiyun  * Since the sampling rate and data format are not controlled by the DMA
116*4882a593Smuzhiyun  * controller, we specify no limits for those values.  The only exception is
117*4882a593Smuzhiyun  * period_bytes_min, which is set to a reasonably low value to prevent the
118*4882a593Smuzhiyun  * DMA controller from generating too many interrupts per second.
119*4882a593Smuzhiyun  *
120*4882a593Smuzhiyun  * Since each link descriptor has a 32-bit byte count field, we set
121*4882a593Smuzhiyun  * period_bytes_max to the largest 32-bit number.  We also have no maximum
122*4882a593Smuzhiyun  * number of periods.
123*4882a593Smuzhiyun  *
124*4882a593Smuzhiyun  * Note that we specify SNDRV_PCM_INFO_JOINT_DUPLEX here, but only because a
125*4882a593Smuzhiyun  * limitation in the SSI driver requires the sample rates for playback and
126*4882a593Smuzhiyun  * capture to be the same.
127*4882a593Smuzhiyun  */
128*4882a593Smuzhiyun static const struct snd_pcm_hardware fsl_dma_hardware = {
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun 	.info   		= SNDRV_PCM_INFO_INTERLEAVED |
131*4882a593Smuzhiyun 				  SNDRV_PCM_INFO_MMAP |
132*4882a593Smuzhiyun 				  SNDRV_PCM_INFO_MMAP_VALID |
133*4882a593Smuzhiyun 				  SNDRV_PCM_INFO_JOINT_DUPLEX |
134*4882a593Smuzhiyun 				  SNDRV_PCM_INFO_PAUSE,
135*4882a593Smuzhiyun 	.formats		= FSLDMA_PCM_FORMATS,
136*4882a593Smuzhiyun 	.period_bytes_min       = 512,  	/* A reasonable limit */
137*4882a593Smuzhiyun 	.period_bytes_max       = (u32) -1,
138*4882a593Smuzhiyun 	.periods_min    	= NUM_DMA_LINKS,
139*4882a593Smuzhiyun 	.periods_max    	= (unsigned int) -1,
140*4882a593Smuzhiyun 	.buffer_bytes_max       = 128 * 1024,   /* A reasonable limit */
141*4882a593Smuzhiyun };
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun /**
144*4882a593Smuzhiyun  * fsl_dma_abort_stream: tell ALSA that the DMA transfer has aborted
145*4882a593Smuzhiyun  *
146*4882a593Smuzhiyun  * This function should be called by the ISR whenever the DMA controller
147*4882a593Smuzhiyun  * halts data transfer.
148*4882a593Smuzhiyun  */
fsl_dma_abort_stream(struct snd_pcm_substream * substream)149*4882a593Smuzhiyun static void fsl_dma_abort_stream(struct snd_pcm_substream *substream)
150*4882a593Smuzhiyun {
151*4882a593Smuzhiyun 	snd_pcm_stop_xrun(substream);
152*4882a593Smuzhiyun }
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun /**
155*4882a593Smuzhiyun  * fsl_dma_update_pointers - update LD pointers to point to the next period
156*4882a593Smuzhiyun  *
157*4882a593Smuzhiyun  * As each period is completed, this function changes the link
158*4882a593Smuzhiyun  * descriptor pointers for that period to point to the next period.
159*4882a593Smuzhiyun  */
fsl_dma_update_pointers(struct fsl_dma_private * dma_private)160*4882a593Smuzhiyun static void fsl_dma_update_pointers(struct fsl_dma_private *dma_private)
161*4882a593Smuzhiyun {
162*4882a593Smuzhiyun 	struct fsl_dma_link_descriptor *link =
163*4882a593Smuzhiyun 		&dma_private->link[dma_private->current_link];
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 	/* Update our link descriptors to point to the next period. On a 36-bit
166*4882a593Smuzhiyun 	 * system, we also need to update the ESAD bits.  We also set (keep) the
167*4882a593Smuzhiyun 	 * snoop bits.  See the comments in fsl_dma_hw_params() about snooping.
168*4882a593Smuzhiyun 	 */
169*4882a593Smuzhiyun 	if (dma_private->substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
170*4882a593Smuzhiyun 		link->source_addr = cpu_to_be32(dma_private->dma_buf_next);
171*4882a593Smuzhiyun #ifdef CONFIG_PHYS_64BIT
172*4882a593Smuzhiyun 		link->source_attr = cpu_to_be32(CCSR_DMA_ATR_SNOOP |
173*4882a593Smuzhiyun 			upper_32_bits(dma_private->dma_buf_next));
174*4882a593Smuzhiyun #endif
175*4882a593Smuzhiyun 	} else {
176*4882a593Smuzhiyun 		link->dest_addr = cpu_to_be32(dma_private->dma_buf_next);
177*4882a593Smuzhiyun #ifdef CONFIG_PHYS_64BIT
178*4882a593Smuzhiyun 		link->dest_attr = cpu_to_be32(CCSR_DMA_ATR_SNOOP |
179*4882a593Smuzhiyun 			upper_32_bits(dma_private->dma_buf_next));
180*4882a593Smuzhiyun #endif
181*4882a593Smuzhiyun 	}
182*4882a593Smuzhiyun 
183*4882a593Smuzhiyun 	/* Update our variables for next time */
184*4882a593Smuzhiyun 	dma_private->dma_buf_next += dma_private->period_size;
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun 	if (dma_private->dma_buf_next >= dma_private->dma_buf_end)
187*4882a593Smuzhiyun 		dma_private->dma_buf_next = dma_private->dma_buf_phys;
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun 	if (++dma_private->current_link >= NUM_DMA_LINKS)
190*4882a593Smuzhiyun 		dma_private->current_link = 0;
191*4882a593Smuzhiyun }
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun /**
194*4882a593Smuzhiyun  * fsl_dma_isr: interrupt handler for the DMA controller
195*4882a593Smuzhiyun  *
196*4882a593Smuzhiyun  * @irq: IRQ of the DMA channel
197*4882a593Smuzhiyun  * @dev_id: pointer to the dma_private structure for this DMA channel
198*4882a593Smuzhiyun  */
fsl_dma_isr(int irq,void * dev_id)199*4882a593Smuzhiyun static irqreturn_t fsl_dma_isr(int irq, void *dev_id)
200*4882a593Smuzhiyun {
201*4882a593Smuzhiyun 	struct fsl_dma_private *dma_private = dev_id;
202*4882a593Smuzhiyun 	struct snd_pcm_substream *substream = dma_private->substream;
203*4882a593Smuzhiyun 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
204*4882a593Smuzhiyun 	struct device *dev = rtd->dev;
205*4882a593Smuzhiyun 	struct ccsr_dma_channel __iomem *dma_channel = dma_private->dma_channel;
206*4882a593Smuzhiyun 	irqreturn_t ret = IRQ_NONE;
207*4882a593Smuzhiyun 	u32 sr, sr2 = 0;
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun 	/* We got an interrupt, so read the status register to see what we
210*4882a593Smuzhiyun 	   were interrupted for.
211*4882a593Smuzhiyun 	 */
212*4882a593Smuzhiyun 	sr = in_be32(&dma_channel->sr);
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun 	if (sr & CCSR_DMA_SR_TE) {
215*4882a593Smuzhiyun 		dev_err(dev, "dma transmit error\n");
216*4882a593Smuzhiyun 		fsl_dma_abort_stream(substream);
217*4882a593Smuzhiyun 		sr2 |= CCSR_DMA_SR_TE;
218*4882a593Smuzhiyun 		ret = IRQ_HANDLED;
219*4882a593Smuzhiyun 	}
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun 	if (sr & CCSR_DMA_SR_CH)
222*4882a593Smuzhiyun 		ret = IRQ_HANDLED;
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun 	if (sr & CCSR_DMA_SR_PE) {
225*4882a593Smuzhiyun 		dev_err(dev, "dma programming error\n");
226*4882a593Smuzhiyun 		fsl_dma_abort_stream(substream);
227*4882a593Smuzhiyun 		sr2 |= CCSR_DMA_SR_PE;
228*4882a593Smuzhiyun 		ret = IRQ_HANDLED;
229*4882a593Smuzhiyun 	}
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun 	if (sr & CCSR_DMA_SR_EOLNI) {
232*4882a593Smuzhiyun 		sr2 |= CCSR_DMA_SR_EOLNI;
233*4882a593Smuzhiyun 		ret = IRQ_HANDLED;
234*4882a593Smuzhiyun 	}
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun 	if (sr & CCSR_DMA_SR_CB)
237*4882a593Smuzhiyun 		ret = IRQ_HANDLED;
238*4882a593Smuzhiyun 
239*4882a593Smuzhiyun 	if (sr & CCSR_DMA_SR_EOSI) {
240*4882a593Smuzhiyun 		/* Tell ALSA we completed a period. */
241*4882a593Smuzhiyun 		snd_pcm_period_elapsed(substream);
242*4882a593Smuzhiyun 
243*4882a593Smuzhiyun 		/*
244*4882a593Smuzhiyun 		 * Update our link descriptors to point to the next period. We
245*4882a593Smuzhiyun 		 * only need to do this if the number of periods is not equal to
246*4882a593Smuzhiyun 		 * the number of links.
247*4882a593Smuzhiyun 		 */
248*4882a593Smuzhiyun 		if (dma_private->num_periods != NUM_DMA_LINKS)
249*4882a593Smuzhiyun 			fsl_dma_update_pointers(dma_private);
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun 		sr2 |= CCSR_DMA_SR_EOSI;
252*4882a593Smuzhiyun 		ret = IRQ_HANDLED;
253*4882a593Smuzhiyun 	}
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun 	if (sr & CCSR_DMA_SR_EOLSI) {
256*4882a593Smuzhiyun 		sr2 |= CCSR_DMA_SR_EOLSI;
257*4882a593Smuzhiyun 		ret = IRQ_HANDLED;
258*4882a593Smuzhiyun 	}
259*4882a593Smuzhiyun 
260*4882a593Smuzhiyun 	/* Clear the bits that we set */
261*4882a593Smuzhiyun 	if (sr2)
262*4882a593Smuzhiyun 		out_be32(&dma_channel->sr, sr2);
263*4882a593Smuzhiyun 
264*4882a593Smuzhiyun 	return ret;
265*4882a593Smuzhiyun }
266*4882a593Smuzhiyun 
267*4882a593Smuzhiyun /**
268*4882a593Smuzhiyun  * fsl_dma_new: initialize this PCM driver.
269*4882a593Smuzhiyun  *
270*4882a593Smuzhiyun  * This function is called when the codec driver calls snd_soc_new_pcms(),
271*4882a593Smuzhiyun  * once for each .dai_link in the machine driver's snd_soc_card
272*4882a593Smuzhiyun  * structure.
273*4882a593Smuzhiyun  *
274*4882a593Smuzhiyun  * snd_dma_alloc_pages() is just a front-end to dma_alloc_coherent(), which
275*4882a593Smuzhiyun  * (currently) always allocates the DMA buffer in lowmem, even if GFP_HIGHMEM
276*4882a593Smuzhiyun  * is specified. Therefore, any DMA buffers we allocate will always be in low
277*4882a593Smuzhiyun  * memory, but we support for 36-bit physical addresses anyway.
278*4882a593Smuzhiyun  *
279*4882a593Smuzhiyun  * Regardless of where the memory is actually allocated, since the device can
280*4882a593Smuzhiyun  * technically DMA to any 36-bit address, we do need to set the DMA mask to 36.
281*4882a593Smuzhiyun  */
fsl_dma_new(struct snd_soc_component * component,struct snd_soc_pcm_runtime * rtd)282*4882a593Smuzhiyun static int fsl_dma_new(struct snd_soc_component *component,
283*4882a593Smuzhiyun 		       struct snd_soc_pcm_runtime *rtd)
284*4882a593Smuzhiyun {
285*4882a593Smuzhiyun 	struct snd_card *card = rtd->card->snd_card;
286*4882a593Smuzhiyun 	struct snd_pcm *pcm = rtd->pcm;
287*4882a593Smuzhiyun 	int ret;
288*4882a593Smuzhiyun 
289*4882a593Smuzhiyun 	ret = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(36));
290*4882a593Smuzhiyun 	if (ret)
291*4882a593Smuzhiyun 		return ret;
292*4882a593Smuzhiyun 
293*4882a593Smuzhiyun 	/* Some codecs have separate DAIs for playback and capture, so we
294*4882a593Smuzhiyun 	 * should allocate a DMA buffer only for the streams that are valid.
295*4882a593Smuzhiyun 	 */
296*4882a593Smuzhiyun 
297*4882a593Smuzhiyun 	if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
298*4882a593Smuzhiyun 		ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, card->dev,
299*4882a593Smuzhiyun 			fsl_dma_hardware.buffer_bytes_max,
300*4882a593Smuzhiyun 			&pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->dma_buffer);
301*4882a593Smuzhiyun 		if (ret) {
302*4882a593Smuzhiyun 			dev_err(card->dev, "can't alloc playback dma buffer\n");
303*4882a593Smuzhiyun 			return ret;
304*4882a593Smuzhiyun 		}
305*4882a593Smuzhiyun 	}
306*4882a593Smuzhiyun 
307*4882a593Smuzhiyun 	if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
308*4882a593Smuzhiyun 		ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, card->dev,
309*4882a593Smuzhiyun 			fsl_dma_hardware.buffer_bytes_max,
310*4882a593Smuzhiyun 			&pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->dma_buffer);
311*4882a593Smuzhiyun 		if (ret) {
312*4882a593Smuzhiyun 			dev_err(card->dev, "can't alloc capture dma buffer\n");
313*4882a593Smuzhiyun 			snd_dma_free_pages(&pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->dma_buffer);
314*4882a593Smuzhiyun 			return ret;
315*4882a593Smuzhiyun 		}
316*4882a593Smuzhiyun 	}
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun 	return 0;
319*4882a593Smuzhiyun }
320*4882a593Smuzhiyun 
321*4882a593Smuzhiyun /**
322*4882a593Smuzhiyun  * fsl_dma_open: open a new substream.
323*4882a593Smuzhiyun  *
324*4882a593Smuzhiyun  * Each substream has its own DMA buffer.
325*4882a593Smuzhiyun  *
326*4882a593Smuzhiyun  * ALSA divides the DMA buffer into N periods.  We create NUM_DMA_LINKS link
327*4882a593Smuzhiyun  * descriptors that ping-pong from one period to the next.  For example, if
328*4882a593Smuzhiyun  * there are six periods and two link descriptors, this is how they look
329*4882a593Smuzhiyun  * before playback starts:
330*4882a593Smuzhiyun  *
331*4882a593Smuzhiyun  *      	   The last link descriptor
332*4882a593Smuzhiyun  *   ____________  points back to the first
333*4882a593Smuzhiyun  *  |   	 |
334*4882a593Smuzhiyun  *  V   	 |
335*4882a593Smuzhiyun  *  ___    ___   |
336*4882a593Smuzhiyun  * |   |->|   |->|
337*4882a593Smuzhiyun  * |___|  |___|
338*4882a593Smuzhiyun  *   |      |
339*4882a593Smuzhiyun  *   |      |
340*4882a593Smuzhiyun  *   V      V
341*4882a593Smuzhiyun  *  _________________________________________
342*4882a593Smuzhiyun  * |      |      |      |      |      |      |  The DMA buffer is
343*4882a593Smuzhiyun  * |      |      |      |      |      |      |    divided into 6 parts
344*4882a593Smuzhiyun  * |______|______|______|______|______|______|
345*4882a593Smuzhiyun  *
346*4882a593Smuzhiyun  * and here's how they look after the first period is finished playing:
347*4882a593Smuzhiyun  *
348*4882a593Smuzhiyun  *   ____________
349*4882a593Smuzhiyun  *  |   	 |
350*4882a593Smuzhiyun  *  V   	 |
351*4882a593Smuzhiyun  *  ___    ___   |
352*4882a593Smuzhiyun  * |   |->|   |->|
353*4882a593Smuzhiyun  * |___|  |___|
354*4882a593Smuzhiyun  *   |      |
355*4882a593Smuzhiyun  *   |______________
356*4882a593Smuzhiyun  *          |       |
357*4882a593Smuzhiyun  *          V       V
358*4882a593Smuzhiyun  *  _________________________________________
359*4882a593Smuzhiyun  * |      |      |      |      |      |      |
360*4882a593Smuzhiyun  * |      |      |      |      |      |      |
361*4882a593Smuzhiyun  * |______|______|______|______|______|______|
362*4882a593Smuzhiyun  *
363*4882a593Smuzhiyun  * The first link descriptor now points to the third period.  The DMA
364*4882a593Smuzhiyun  * controller is currently playing the second period.  When it finishes, it
365*4882a593Smuzhiyun  * will jump back to the first descriptor and play the third period.
366*4882a593Smuzhiyun  *
367*4882a593Smuzhiyun  * There are four reasons we do this:
368*4882a593Smuzhiyun  *
369*4882a593Smuzhiyun  * 1. The only way to get the DMA controller to automatically restart the
370*4882a593Smuzhiyun  *    transfer when it gets to the end of the buffer is to use chaining
371*4882a593Smuzhiyun  *    mode.  Basic direct mode doesn't offer that feature.
372*4882a593Smuzhiyun  * 2. We need to receive an interrupt at the end of every period.  The DMA
373*4882a593Smuzhiyun  *    controller can generate an interrupt at the end of every link transfer
374*4882a593Smuzhiyun  *    (aka segment).  Making each period into a DMA segment will give us the
375*4882a593Smuzhiyun  *    interrupts we need.
376*4882a593Smuzhiyun  * 3. By creating only two link descriptors, regardless of the number of
377*4882a593Smuzhiyun  *    periods, we do not need to reallocate the link descriptors if the
378*4882a593Smuzhiyun  *    number of periods changes.
379*4882a593Smuzhiyun  * 4. All of the audio data is still stored in a single, contiguous DMA
380*4882a593Smuzhiyun  *    buffer, which is what ALSA expects.  We're just dividing it into
381*4882a593Smuzhiyun  *    contiguous parts, and creating a link descriptor for each one.
382*4882a593Smuzhiyun  */
fsl_dma_open(struct snd_soc_component * component,struct snd_pcm_substream * substream)383*4882a593Smuzhiyun static int fsl_dma_open(struct snd_soc_component *component,
384*4882a593Smuzhiyun 			struct snd_pcm_substream *substream)
385*4882a593Smuzhiyun {
386*4882a593Smuzhiyun 	struct snd_pcm_runtime *runtime = substream->runtime;
387*4882a593Smuzhiyun 	struct device *dev = component->dev;
388*4882a593Smuzhiyun 	struct dma_object *dma =
389*4882a593Smuzhiyun 		container_of(component->driver, struct dma_object, dai);
390*4882a593Smuzhiyun 	struct fsl_dma_private *dma_private;
391*4882a593Smuzhiyun 	struct ccsr_dma_channel __iomem *dma_channel;
392*4882a593Smuzhiyun 	dma_addr_t ld_buf_phys;
393*4882a593Smuzhiyun 	u64 temp_link;  	/* Pointer to next link descriptor */
394*4882a593Smuzhiyun 	u32 mr;
395*4882a593Smuzhiyun 	unsigned int channel;
396*4882a593Smuzhiyun 	int ret = 0;
397*4882a593Smuzhiyun 	unsigned int i;
398*4882a593Smuzhiyun 
399*4882a593Smuzhiyun 	/*
400*4882a593Smuzhiyun 	 * Reject any DMA buffer whose size is not a multiple of the period
401*4882a593Smuzhiyun 	 * size.  We need to make sure that the DMA buffer can be evenly divided
402*4882a593Smuzhiyun 	 * into periods.
403*4882a593Smuzhiyun 	 */
404*4882a593Smuzhiyun 	ret = snd_pcm_hw_constraint_integer(runtime,
405*4882a593Smuzhiyun 		SNDRV_PCM_HW_PARAM_PERIODS);
406*4882a593Smuzhiyun 	if (ret < 0) {
407*4882a593Smuzhiyun 		dev_err(dev, "invalid buffer size\n");
408*4882a593Smuzhiyun 		return ret;
409*4882a593Smuzhiyun 	}
410*4882a593Smuzhiyun 
411*4882a593Smuzhiyun 	channel = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ? 0 : 1;
412*4882a593Smuzhiyun 
413*4882a593Smuzhiyun 	if (dma->assigned) {
414*4882a593Smuzhiyun 		dev_err(dev, "dma channel already assigned\n");
415*4882a593Smuzhiyun 		return -EBUSY;
416*4882a593Smuzhiyun 	}
417*4882a593Smuzhiyun 
418*4882a593Smuzhiyun 	dma_private = dma_alloc_coherent(dev, sizeof(struct fsl_dma_private),
419*4882a593Smuzhiyun 					 &ld_buf_phys, GFP_KERNEL);
420*4882a593Smuzhiyun 	if (!dma_private) {
421*4882a593Smuzhiyun 		dev_err(dev, "can't allocate dma private data\n");
422*4882a593Smuzhiyun 		return -ENOMEM;
423*4882a593Smuzhiyun 	}
424*4882a593Smuzhiyun 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
425*4882a593Smuzhiyun 		dma_private->ssi_sxx_phys = dma->ssi_stx_phys;
426*4882a593Smuzhiyun 	else
427*4882a593Smuzhiyun 		dma_private->ssi_sxx_phys = dma->ssi_srx_phys;
428*4882a593Smuzhiyun 
429*4882a593Smuzhiyun 	dma_private->ssi_fifo_depth = dma->ssi_fifo_depth;
430*4882a593Smuzhiyun 	dma_private->dma_channel = dma->channel;
431*4882a593Smuzhiyun 	dma_private->irq = dma->irq;
432*4882a593Smuzhiyun 	dma_private->substream = substream;
433*4882a593Smuzhiyun 	dma_private->ld_buf_phys = ld_buf_phys;
434*4882a593Smuzhiyun 	dma_private->dma_buf_phys = substream->dma_buffer.addr;
435*4882a593Smuzhiyun 
436*4882a593Smuzhiyun 	ret = request_irq(dma_private->irq, fsl_dma_isr, 0, "fsldma-audio",
437*4882a593Smuzhiyun 			  dma_private);
438*4882a593Smuzhiyun 	if (ret) {
439*4882a593Smuzhiyun 		dev_err(dev, "can't register ISR for IRQ %u (ret=%i)\n",
440*4882a593Smuzhiyun 			dma_private->irq, ret);
441*4882a593Smuzhiyun 		dma_free_coherent(dev, sizeof(struct fsl_dma_private),
442*4882a593Smuzhiyun 			dma_private, dma_private->ld_buf_phys);
443*4882a593Smuzhiyun 		return ret;
444*4882a593Smuzhiyun 	}
445*4882a593Smuzhiyun 
446*4882a593Smuzhiyun 	dma->assigned = true;
447*4882a593Smuzhiyun 
448*4882a593Smuzhiyun 	snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
449*4882a593Smuzhiyun 	snd_soc_set_runtime_hwparams(substream, &fsl_dma_hardware);
450*4882a593Smuzhiyun 	runtime->private_data = dma_private;
451*4882a593Smuzhiyun 
452*4882a593Smuzhiyun 	/* Program the fixed DMA controller parameters */
453*4882a593Smuzhiyun 
454*4882a593Smuzhiyun 	dma_channel = dma_private->dma_channel;
455*4882a593Smuzhiyun 
456*4882a593Smuzhiyun 	temp_link = dma_private->ld_buf_phys +
457*4882a593Smuzhiyun 		sizeof(struct fsl_dma_link_descriptor);
458*4882a593Smuzhiyun 
459*4882a593Smuzhiyun 	for (i = 0; i < NUM_DMA_LINKS; i++) {
460*4882a593Smuzhiyun 		dma_private->link[i].next = cpu_to_be64(temp_link);
461*4882a593Smuzhiyun 
462*4882a593Smuzhiyun 		temp_link += sizeof(struct fsl_dma_link_descriptor);
463*4882a593Smuzhiyun 	}
464*4882a593Smuzhiyun 	/* The last link descriptor points to the first */
465*4882a593Smuzhiyun 	dma_private->link[i - 1].next = cpu_to_be64(dma_private->ld_buf_phys);
466*4882a593Smuzhiyun 
467*4882a593Smuzhiyun 	/* Tell the DMA controller where the first link descriptor is */
468*4882a593Smuzhiyun 	out_be32(&dma_channel->clndar,
469*4882a593Smuzhiyun 		CCSR_DMA_CLNDAR_ADDR(dma_private->ld_buf_phys));
470*4882a593Smuzhiyun 	out_be32(&dma_channel->eclndar,
471*4882a593Smuzhiyun 		CCSR_DMA_ECLNDAR_ADDR(dma_private->ld_buf_phys));
472*4882a593Smuzhiyun 
473*4882a593Smuzhiyun 	/* The manual says the BCR must be clear before enabling EMP */
474*4882a593Smuzhiyun 	out_be32(&dma_channel->bcr, 0);
475*4882a593Smuzhiyun 
476*4882a593Smuzhiyun 	/*
477*4882a593Smuzhiyun 	 * Program the mode register for interrupts, external master control,
478*4882a593Smuzhiyun 	 * and source/destination hold.  Also clear the Channel Abort bit.
479*4882a593Smuzhiyun 	 */
480*4882a593Smuzhiyun 	mr = in_be32(&dma_channel->mr) &
481*4882a593Smuzhiyun 		~(CCSR_DMA_MR_CA | CCSR_DMA_MR_DAHE | CCSR_DMA_MR_SAHE);
482*4882a593Smuzhiyun 
483*4882a593Smuzhiyun 	/*
484*4882a593Smuzhiyun 	 * We want External Master Start and External Master Pause enabled,
485*4882a593Smuzhiyun 	 * because the SSI is controlling the DMA controller.  We want the DMA
486*4882a593Smuzhiyun 	 * controller to be set up in advance, and then we signal only the SSI
487*4882a593Smuzhiyun 	 * to start transferring.
488*4882a593Smuzhiyun 	 *
489*4882a593Smuzhiyun 	 * We want End-Of-Segment Interrupts enabled, because this will generate
490*4882a593Smuzhiyun 	 * an interrupt at the end of each segment (each link descriptor
491*4882a593Smuzhiyun 	 * represents one segment).  Each DMA segment is the same thing as an
492*4882a593Smuzhiyun 	 * ALSA period, so this is how we get an interrupt at the end of every
493*4882a593Smuzhiyun 	 * period.
494*4882a593Smuzhiyun 	 *
495*4882a593Smuzhiyun 	 * We want Error Interrupt enabled, so that we can get an error if
496*4882a593Smuzhiyun 	 * the DMA controller is mis-programmed somehow.
497*4882a593Smuzhiyun 	 */
498*4882a593Smuzhiyun 	mr |= CCSR_DMA_MR_EOSIE | CCSR_DMA_MR_EIE | CCSR_DMA_MR_EMP_EN |
499*4882a593Smuzhiyun 		CCSR_DMA_MR_EMS_EN;
500*4882a593Smuzhiyun 
501*4882a593Smuzhiyun 	/* For playback, we want the destination address to be held.  For
502*4882a593Smuzhiyun 	   capture, set the source address to be held. */
503*4882a593Smuzhiyun 	mr |= (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
504*4882a593Smuzhiyun 		CCSR_DMA_MR_DAHE : CCSR_DMA_MR_SAHE;
505*4882a593Smuzhiyun 
506*4882a593Smuzhiyun 	out_be32(&dma_channel->mr, mr);
507*4882a593Smuzhiyun 
508*4882a593Smuzhiyun 	return 0;
509*4882a593Smuzhiyun }
510*4882a593Smuzhiyun 
511*4882a593Smuzhiyun /**
512*4882a593Smuzhiyun  * fsl_dma_hw_params: continue initializing the DMA links
513*4882a593Smuzhiyun  *
514*4882a593Smuzhiyun  * This function obtains hardware parameters about the opened stream and
515*4882a593Smuzhiyun  * programs the DMA controller accordingly.
516*4882a593Smuzhiyun  *
517*4882a593Smuzhiyun  * One drawback of big-endian is that when copying integers of different
518*4882a593Smuzhiyun  * sizes to a fixed-sized register, the address to which the integer must be
519*4882a593Smuzhiyun  * copied is dependent on the size of the integer.
520*4882a593Smuzhiyun  *
521*4882a593Smuzhiyun  * For example, if P is the address of a 32-bit register, and X is a 32-bit
522*4882a593Smuzhiyun  * integer, then X should be copied to address P.  However, if X is a 16-bit
523*4882a593Smuzhiyun  * integer, then it should be copied to P+2.  If X is an 8-bit register,
524*4882a593Smuzhiyun  * then it should be copied to P+3.
525*4882a593Smuzhiyun  *
526*4882a593Smuzhiyun  * So for playback of 8-bit samples, the DMA controller must transfer single
527*4882a593Smuzhiyun  * bytes from the DMA buffer to the last byte of the STX0 register, i.e.
528*4882a593Smuzhiyun  * offset by 3 bytes. For 16-bit samples, the offset is two bytes.
529*4882a593Smuzhiyun  *
530*4882a593Smuzhiyun  * For 24-bit samples, the offset is 1 byte.  However, the DMA controller
531*4882a593Smuzhiyun  * does not support 3-byte copies (the DAHTS register supports only 1, 2, 4,
532*4882a593Smuzhiyun  * and 8 bytes at a time).  So we do not support packed 24-bit samples.
533*4882a593Smuzhiyun  * 24-bit data must be padded to 32 bits.
534*4882a593Smuzhiyun  */
fsl_dma_hw_params(struct snd_soc_component * component,struct snd_pcm_substream * substream,struct snd_pcm_hw_params * hw_params)535*4882a593Smuzhiyun static int fsl_dma_hw_params(struct snd_soc_component *component,
536*4882a593Smuzhiyun 			     struct snd_pcm_substream *substream,
537*4882a593Smuzhiyun 			     struct snd_pcm_hw_params *hw_params)
538*4882a593Smuzhiyun {
539*4882a593Smuzhiyun 	struct snd_pcm_runtime *runtime = substream->runtime;
540*4882a593Smuzhiyun 	struct fsl_dma_private *dma_private = runtime->private_data;
541*4882a593Smuzhiyun 	struct device *dev = component->dev;
542*4882a593Smuzhiyun 
543*4882a593Smuzhiyun 	/* Number of bits per sample */
544*4882a593Smuzhiyun 	unsigned int sample_bits =
545*4882a593Smuzhiyun 		snd_pcm_format_physical_width(params_format(hw_params));
546*4882a593Smuzhiyun 
547*4882a593Smuzhiyun 	/* Number of bytes per frame */
548*4882a593Smuzhiyun 	unsigned int sample_bytes = sample_bits / 8;
549*4882a593Smuzhiyun 
550*4882a593Smuzhiyun 	/* Bus address of SSI STX register */
551*4882a593Smuzhiyun 	dma_addr_t ssi_sxx_phys = dma_private->ssi_sxx_phys;
552*4882a593Smuzhiyun 
553*4882a593Smuzhiyun 	/* Size of the DMA buffer, in bytes */
554*4882a593Smuzhiyun 	size_t buffer_size = params_buffer_bytes(hw_params);
555*4882a593Smuzhiyun 
556*4882a593Smuzhiyun 	/* Number of bytes per period */
557*4882a593Smuzhiyun 	size_t period_size = params_period_bytes(hw_params);
558*4882a593Smuzhiyun 
559*4882a593Smuzhiyun 	/* Pointer to next period */
560*4882a593Smuzhiyun 	dma_addr_t temp_addr = substream->dma_buffer.addr;
561*4882a593Smuzhiyun 
562*4882a593Smuzhiyun 	/* Pointer to DMA controller */
563*4882a593Smuzhiyun 	struct ccsr_dma_channel __iomem *dma_channel = dma_private->dma_channel;
564*4882a593Smuzhiyun 
565*4882a593Smuzhiyun 	u32 mr; /* DMA Mode Register */
566*4882a593Smuzhiyun 
567*4882a593Smuzhiyun 	unsigned int i;
568*4882a593Smuzhiyun 
569*4882a593Smuzhiyun 	/* Initialize our DMA tracking variables */
570*4882a593Smuzhiyun 	dma_private->period_size = period_size;
571*4882a593Smuzhiyun 	dma_private->num_periods = params_periods(hw_params);
572*4882a593Smuzhiyun 	dma_private->dma_buf_end = dma_private->dma_buf_phys + buffer_size;
573*4882a593Smuzhiyun 	dma_private->dma_buf_next = dma_private->dma_buf_phys +
574*4882a593Smuzhiyun 		(NUM_DMA_LINKS * period_size);
575*4882a593Smuzhiyun 
576*4882a593Smuzhiyun 	if (dma_private->dma_buf_next >= dma_private->dma_buf_end)
577*4882a593Smuzhiyun 		/* This happens if the number of periods == NUM_DMA_LINKS */
578*4882a593Smuzhiyun 		dma_private->dma_buf_next = dma_private->dma_buf_phys;
579*4882a593Smuzhiyun 
580*4882a593Smuzhiyun 	mr = in_be32(&dma_channel->mr) & ~(CCSR_DMA_MR_BWC_MASK |
581*4882a593Smuzhiyun 		  CCSR_DMA_MR_SAHTS_MASK | CCSR_DMA_MR_DAHTS_MASK);
582*4882a593Smuzhiyun 
583*4882a593Smuzhiyun 	/* Due to a quirk of the SSI's STX register, the target address
584*4882a593Smuzhiyun 	 * for the DMA operations depends on the sample size.  So we calculate
585*4882a593Smuzhiyun 	 * that offset here.  While we're at it, also tell the DMA controller
586*4882a593Smuzhiyun 	 * how much data to transfer per sample.
587*4882a593Smuzhiyun 	 */
588*4882a593Smuzhiyun 	switch (sample_bits) {
589*4882a593Smuzhiyun 	case 8:
590*4882a593Smuzhiyun 		mr |= CCSR_DMA_MR_DAHTS_1 | CCSR_DMA_MR_SAHTS_1;
591*4882a593Smuzhiyun 		ssi_sxx_phys += 3;
592*4882a593Smuzhiyun 		break;
593*4882a593Smuzhiyun 	case 16:
594*4882a593Smuzhiyun 		mr |= CCSR_DMA_MR_DAHTS_2 | CCSR_DMA_MR_SAHTS_2;
595*4882a593Smuzhiyun 		ssi_sxx_phys += 2;
596*4882a593Smuzhiyun 		break;
597*4882a593Smuzhiyun 	case 32:
598*4882a593Smuzhiyun 		mr |= CCSR_DMA_MR_DAHTS_4 | CCSR_DMA_MR_SAHTS_4;
599*4882a593Smuzhiyun 		break;
600*4882a593Smuzhiyun 	default:
601*4882a593Smuzhiyun 		/* We should never get here */
602*4882a593Smuzhiyun 		dev_err(dev, "unsupported sample size %u\n", sample_bits);
603*4882a593Smuzhiyun 		return -EINVAL;
604*4882a593Smuzhiyun 	}
605*4882a593Smuzhiyun 
606*4882a593Smuzhiyun 	/*
607*4882a593Smuzhiyun 	 * BWC determines how many bytes are sent/received before the DMA
608*4882a593Smuzhiyun 	 * controller checks the SSI to see if it needs to stop. BWC should
609*4882a593Smuzhiyun 	 * always be a multiple of the frame size, so that we always transmit
610*4882a593Smuzhiyun 	 * whole frames.  Each frame occupies two slots in the FIFO.  The
611*4882a593Smuzhiyun 	 * parameter for CCSR_DMA_MR_BWC() is rounded down the next power of two
612*4882a593Smuzhiyun 	 * (MR[BWC] can only represent even powers of two).
613*4882a593Smuzhiyun 	 *
614*4882a593Smuzhiyun 	 * To simplify the process, we set BWC to the largest value that is
615*4882a593Smuzhiyun 	 * less than or equal to the FIFO watermark.  For playback, this ensures
616*4882a593Smuzhiyun 	 * that we transfer the maximum amount without overrunning the FIFO.
617*4882a593Smuzhiyun 	 * For capture, this ensures that we transfer the maximum amount without
618*4882a593Smuzhiyun 	 * underrunning the FIFO.
619*4882a593Smuzhiyun 	 *
620*4882a593Smuzhiyun 	 * f = SSI FIFO depth
621*4882a593Smuzhiyun 	 * w = SSI watermark value (which equals f - 2)
622*4882a593Smuzhiyun 	 * b = DMA bandwidth count (in bytes)
623*4882a593Smuzhiyun 	 * s = sample size (in bytes, which equals frame_size * 2)
624*4882a593Smuzhiyun 	 *
625*4882a593Smuzhiyun 	 * For playback, we never transmit more than the transmit FIFO
626*4882a593Smuzhiyun 	 * watermark, otherwise we might write more data than the FIFO can hold.
627*4882a593Smuzhiyun 	 * The watermark is equal to the FIFO depth minus two.
628*4882a593Smuzhiyun 	 *
629*4882a593Smuzhiyun 	 * For capture, two equations must hold:
630*4882a593Smuzhiyun 	 *	w > f - (b / s)
631*4882a593Smuzhiyun 	 *	w >= b / s
632*4882a593Smuzhiyun 	 *
633*4882a593Smuzhiyun 	 * So, b > 2 * s, but b must also be <= s * w.  To simplify, we set
634*4882a593Smuzhiyun 	 * b = s * w, which is equal to
635*4882a593Smuzhiyun 	 *      (dma_private->ssi_fifo_depth - 2) * sample_bytes.
636*4882a593Smuzhiyun 	 */
637*4882a593Smuzhiyun 	mr |= CCSR_DMA_MR_BWC((dma_private->ssi_fifo_depth - 2) * sample_bytes);
638*4882a593Smuzhiyun 
639*4882a593Smuzhiyun 	out_be32(&dma_channel->mr, mr);
640*4882a593Smuzhiyun 
641*4882a593Smuzhiyun 	for (i = 0; i < NUM_DMA_LINKS; i++) {
642*4882a593Smuzhiyun 		struct fsl_dma_link_descriptor *link = &dma_private->link[i];
643*4882a593Smuzhiyun 
644*4882a593Smuzhiyun 		link->count = cpu_to_be32(period_size);
645*4882a593Smuzhiyun 
646*4882a593Smuzhiyun 		/* The snoop bit tells the DMA controller whether it should tell
647*4882a593Smuzhiyun 		 * the ECM to snoop during a read or write to an address. For
648*4882a593Smuzhiyun 		 * audio, we use DMA to transfer data between memory and an I/O
649*4882a593Smuzhiyun 		 * device (the SSI's STX0 or SRX0 register). Snooping is only
650*4882a593Smuzhiyun 		 * needed if there is a cache, so we need to snoop memory
651*4882a593Smuzhiyun 		 * addresses only.  For playback, that means we snoop the source
652*4882a593Smuzhiyun 		 * but not the destination.  For capture, we snoop the
653*4882a593Smuzhiyun 		 * destination but not the source.
654*4882a593Smuzhiyun 		 *
655*4882a593Smuzhiyun 		 * Note that failing to snoop properly is unlikely to cause
656*4882a593Smuzhiyun 		 * cache incoherency if the period size is larger than the
657*4882a593Smuzhiyun 		 * size of L1 cache.  This is because filling in one period will
658*4882a593Smuzhiyun 		 * flush out the data for the previous period.  So if you
659*4882a593Smuzhiyun 		 * increased period_bytes_min to a large enough size, you might
660*4882a593Smuzhiyun 		 * get more performance by not snooping, and you'll still be
661*4882a593Smuzhiyun 		 * okay.  You'll need to update fsl_dma_update_pointers() also.
662*4882a593Smuzhiyun 		 */
663*4882a593Smuzhiyun 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
664*4882a593Smuzhiyun 			link->source_addr = cpu_to_be32(temp_addr);
665*4882a593Smuzhiyun 			link->source_attr = cpu_to_be32(CCSR_DMA_ATR_SNOOP |
666*4882a593Smuzhiyun 				upper_32_bits(temp_addr));
667*4882a593Smuzhiyun 
668*4882a593Smuzhiyun 			link->dest_addr = cpu_to_be32(ssi_sxx_phys);
669*4882a593Smuzhiyun 			link->dest_attr = cpu_to_be32(CCSR_DMA_ATR_NOSNOOP |
670*4882a593Smuzhiyun 				upper_32_bits(ssi_sxx_phys));
671*4882a593Smuzhiyun 		} else {
672*4882a593Smuzhiyun 			link->source_addr = cpu_to_be32(ssi_sxx_phys);
673*4882a593Smuzhiyun 			link->source_attr = cpu_to_be32(CCSR_DMA_ATR_NOSNOOP |
674*4882a593Smuzhiyun 				upper_32_bits(ssi_sxx_phys));
675*4882a593Smuzhiyun 
676*4882a593Smuzhiyun 			link->dest_addr = cpu_to_be32(temp_addr);
677*4882a593Smuzhiyun 			link->dest_attr = cpu_to_be32(CCSR_DMA_ATR_SNOOP |
678*4882a593Smuzhiyun 				upper_32_bits(temp_addr));
679*4882a593Smuzhiyun 		}
680*4882a593Smuzhiyun 
681*4882a593Smuzhiyun 		temp_addr += period_size;
682*4882a593Smuzhiyun 	}
683*4882a593Smuzhiyun 
684*4882a593Smuzhiyun 	return 0;
685*4882a593Smuzhiyun }
686*4882a593Smuzhiyun 
687*4882a593Smuzhiyun /**
688*4882a593Smuzhiyun  * fsl_dma_pointer: determine the current position of the DMA transfer
689*4882a593Smuzhiyun  *
690*4882a593Smuzhiyun  * This function is called by ALSA when ALSA wants to know where in the
691*4882a593Smuzhiyun  * stream buffer the hardware currently is.
692*4882a593Smuzhiyun  *
693*4882a593Smuzhiyun  * For playback, the SAR register contains the physical address of the most
694*4882a593Smuzhiyun  * recent DMA transfer.  For capture, the value is in the DAR register.
695*4882a593Smuzhiyun  *
696*4882a593Smuzhiyun  * The base address of the buffer is stored in the source_addr field of the
697*4882a593Smuzhiyun  * first link descriptor.
698*4882a593Smuzhiyun  */
fsl_dma_pointer(struct snd_soc_component * component,struct snd_pcm_substream * substream)699*4882a593Smuzhiyun static snd_pcm_uframes_t fsl_dma_pointer(struct snd_soc_component *component,
700*4882a593Smuzhiyun 					 struct snd_pcm_substream *substream)
701*4882a593Smuzhiyun {
702*4882a593Smuzhiyun 	struct snd_pcm_runtime *runtime = substream->runtime;
703*4882a593Smuzhiyun 	struct fsl_dma_private *dma_private = runtime->private_data;
704*4882a593Smuzhiyun 	struct device *dev = component->dev;
705*4882a593Smuzhiyun 	struct ccsr_dma_channel __iomem *dma_channel = dma_private->dma_channel;
706*4882a593Smuzhiyun 	dma_addr_t position;
707*4882a593Smuzhiyun 	snd_pcm_uframes_t frames;
708*4882a593Smuzhiyun 
709*4882a593Smuzhiyun 	/* Obtain the current DMA pointer, but don't read the ESAD bits if we
710*4882a593Smuzhiyun 	 * only have 32-bit DMA addresses.  This function is typically called
711*4882a593Smuzhiyun 	 * in interrupt context, so we need to optimize it.
712*4882a593Smuzhiyun 	 */
713*4882a593Smuzhiyun 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
714*4882a593Smuzhiyun 		position = in_be32(&dma_channel->sar);
715*4882a593Smuzhiyun #ifdef CONFIG_PHYS_64BIT
716*4882a593Smuzhiyun 		position |= (u64)(in_be32(&dma_channel->satr) &
717*4882a593Smuzhiyun 				  CCSR_DMA_ATR_ESAD_MASK) << 32;
718*4882a593Smuzhiyun #endif
719*4882a593Smuzhiyun 	} else {
720*4882a593Smuzhiyun 		position = in_be32(&dma_channel->dar);
721*4882a593Smuzhiyun #ifdef CONFIG_PHYS_64BIT
722*4882a593Smuzhiyun 		position |= (u64)(in_be32(&dma_channel->datr) &
723*4882a593Smuzhiyun 				  CCSR_DMA_ATR_ESAD_MASK) << 32;
724*4882a593Smuzhiyun #endif
725*4882a593Smuzhiyun 	}
726*4882a593Smuzhiyun 
727*4882a593Smuzhiyun 	/*
728*4882a593Smuzhiyun 	 * When capture is started, the SSI immediately starts to fill its FIFO.
729*4882a593Smuzhiyun 	 * This means that the DMA controller is not started until the FIFO is
730*4882a593Smuzhiyun 	 * full.  However, ALSA calls this function before that happens, when
731*4882a593Smuzhiyun 	 * MR.DAR is still zero.  In this case, just return zero to indicate
732*4882a593Smuzhiyun 	 * that nothing has been received yet.
733*4882a593Smuzhiyun 	 */
734*4882a593Smuzhiyun 	if (!position)
735*4882a593Smuzhiyun 		return 0;
736*4882a593Smuzhiyun 
737*4882a593Smuzhiyun 	if ((position < dma_private->dma_buf_phys) ||
738*4882a593Smuzhiyun 	    (position > dma_private->dma_buf_end)) {
739*4882a593Smuzhiyun 		dev_err(dev, "dma pointer is out of range, halting stream\n");
740*4882a593Smuzhiyun 		return SNDRV_PCM_POS_XRUN;
741*4882a593Smuzhiyun 	}
742*4882a593Smuzhiyun 
743*4882a593Smuzhiyun 	frames = bytes_to_frames(runtime, position - dma_private->dma_buf_phys);
744*4882a593Smuzhiyun 
745*4882a593Smuzhiyun 	/*
746*4882a593Smuzhiyun 	 * If the current address is just past the end of the buffer, wrap it
747*4882a593Smuzhiyun 	 * around.
748*4882a593Smuzhiyun 	 */
749*4882a593Smuzhiyun 	if (frames == runtime->buffer_size)
750*4882a593Smuzhiyun 		frames = 0;
751*4882a593Smuzhiyun 
752*4882a593Smuzhiyun 	return frames;
753*4882a593Smuzhiyun }
754*4882a593Smuzhiyun 
755*4882a593Smuzhiyun /**
756*4882a593Smuzhiyun  * fsl_dma_hw_free: release resources allocated in fsl_dma_hw_params()
757*4882a593Smuzhiyun  *
758*4882a593Smuzhiyun  * Release the resources allocated in fsl_dma_hw_params() and de-program the
759*4882a593Smuzhiyun  * registers.
760*4882a593Smuzhiyun  *
761*4882a593Smuzhiyun  * This function can be called multiple times.
762*4882a593Smuzhiyun  */
fsl_dma_hw_free(struct snd_soc_component * component,struct snd_pcm_substream * substream)763*4882a593Smuzhiyun static int fsl_dma_hw_free(struct snd_soc_component *component,
764*4882a593Smuzhiyun 			   struct snd_pcm_substream *substream)
765*4882a593Smuzhiyun {
766*4882a593Smuzhiyun 	struct snd_pcm_runtime *runtime = substream->runtime;
767*4882a593Smuzhiyun 	struct fsl_dma_private *dma_private = runtime->private_data;
768*4882a593Smuzhiyun 
769*4882a593Smuzhiyun 	if (dma_private) {
770*4882a593Smuzhiyun 		struct ccsr_dma_channel __iomem *dma_channel;
771*4882a593Smuzhiyun 
772*4882a593Smuzhiyun 		dma_channel = dma_private->dma_channel;
773*4882a593Smuzhiyun 
774*4882a593Smuzhiyun 		/* Stop the DMA */
775*4882a593Smuzhiyun 		out_be32(&dma_channel->mr, CCSR_DMA_MR_CA);
776*4882a593Smuzhiyun 		out_be32(&dma_channel->mr, 0);
777*4882a593Smuzhiyun 
778*4882a593Smuzhiyun 		/* Reset all the other registers */
779*4882a593Smuzhiyun 		out_be32(&dma_channel->sr, -1);
780*4882a593Smuzhiyun 		out_be32(&dma_channel->clndar, 0);
781*4882a593Smuzhiyun 		out_be32(&dma_channel->eclndar, 0);
782*4882a593Smuzhiyun 		out_be32(&dma_channel->satr, 0);
783*4882a593Smuzhiyun 		out_be32(&dma_channel->sar, 0);
784*4882a593Smuzhiyun 		out_be32(&dma_channel->datr, 0);
785*4882a593Smuzhiyun 		out_be32(&dma_channel->dar, 0);
786*4882a593Smuzhiyun 		out_be32(&dma_channel->bcr, 0);
787*4882a593Smuzhiyun 		out_be32(&dma_channel->nlndar, 0);
788*4882a593Smuzhiyun 		out_be32(&dma_channel->enlndar, 0);
789*4882a593Smuzhiyun 	}
790*4882a593Smuzhiyun 
791*4882a593Smuzhiyun 	return 0;
792*4882a593Smuzhiyun }
793*4882a593Smuzhiyun 
794*4882a593Smuzhiyun /**
795*4882a593Smuzhiyun  * fsl_dma_close: close the stream.
796*4882a593Smuzhiyun  */
fsl_dma_close(struct snd_soc_component * component,struct snd_pcm_substream * substream)797*4882a593Smuzhiyun static int fsl_dma_close(struct snd_soc_component *component,
798*4882a593Smuzhiyun 			 struct snd_pcm_substream *substream)
799*4882a593Smuzhiyun {
800*4882a593Smuzhiyun 	struct snd_pcm_runtime *runtime = substream->runtime;
801*4882a593Smuzhiyun 	struct fsl_dma_private *dma_private = runtime->private_data;
802*4882a593Smuzhiyun 	struct device *dev = component->dev;
803*4882a593Smuzhiyun 	struct dma_object *dma =
804*4882a593Smuzhiyun 		container_of(component->driver, struct dma_object, dai);
805*4882a593Smuzhiyun 
806*4882a593Smuzhiyun 	if (dma_private) {
807*4882a593Smuzhiyun 		if (dma_private->irq)
808*4882a593Smuzhiyun 			free_irq(dma_private->irq, dma_private);
809*4882a593Smuzhiyun 
810*4882a593Smuzhiyun 		/* Deallocate the fsl_dma_private structure */
811*4882a593Smuzhiyun 		dma_free_coherent(dev, sizeof(struct fsl_dma_private),
812*4882a593Smuzhiyun 				  dma_private, dma_private->ld_buf_phys);
813*4882a593Smuzhiyun 		substream->runtime->private_data = NULL;
814*4882a593Smuzhiyun 	}
815*4882a593Smuzhiyun 
816*4882a593Smuzhiyun 	dma->assigned = false;
817*4882a593Smuzhiyun 
818*4882a593Smuzhiyun 	return 0;
819*4882a593Smuzhiyun }
820*4882a593Smuzhiyun 
821*4882a593Smuzhiyun /*
822*4882a593Smuzhiyun  * Remove this PCM driver.
823*4882a593Smuzhiyun  */
fsl_dma_free_dma_buffers(struct snd_soc_component * component,struct snd_pcm * pcm)824*4882a593Smuzhiyun static void fsl_dma_free_dma_buffers(struct snd_soc_component *component,
825*4882a593Smuzhiyun 				     struct snd_pcm *pcm)
826*4882a593Smuzhiyun {
827*4882a593Smuzhiyun 	struct snd_pcm_substream *substream;
828*4882a593Smuzhiyun 	unsigned int i;
829*4882a593Smuzhiyun 
830*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(pcm->streams); i++) {
831*4882a593Smuzhiyun 		substream = pcm->streams[i].substream;
832*4882a593Smuzhiyun 		if (substream) {
833*4882a593Smuzhiyun 			snd_dma_free_pages(&substream->dma_buffer);
834*4882a593Smuzhiyun 			substream->dma_buffer.area = NULL;
835*4882a593Smuzhiyun 			substream->dma_buffer.addr = 0;
836*4882a593Smuzhiyun 		}
837*4882a593Smuzhiyun 	}
838*4882a593Smuzhiyun }
839*4882a593Smuzhiyun 
840*4882a593Smuzhiyun /**
841*4882a593Smuzhiyun  * find_ssi_node -- returns the SSI node that points to its DMA channel node
842*4882a593Smuzhiyun  *
843*4882a593Smuzhiyun  * Although this DMA driver attempts to operate independently of the other
844*4882a593Smuzhiyun  * devices, it still needs to determine some information about the SSI device
845*4882a593Smuzhiyun  * that it's working with.  Unfortunately, the device tree does not contain
846*4882a593Smuzhiyun  * a pointer from the DMA channel node to the SSI node -- the pointer goes the
847*4882a593Smuzhiyun  * other way.  So we need to scan the device tree for SSI nodes until we find
848*4882a593Smuzhiyun  * the one that points to the given DMA channel node.  It's ugly, but at least
849*4882a593Smuzhiyun  * it's contained in this one function.
850*4882a593Smuzhiyun  */
find_ssi_node(struct device_node * dma_channel_np)851*4882a593Smuzhiyun static struct device_node *find_ssi_node(struct device_node *dma_channel_np)
852*4882a593Smuzhiyun {
853*4882a593Smuzhiyun 	struct device_node *ssi_np, *np;
854*4882a593Smuzhiyun 
855*4882a593Smuzhiyun 	for_each_compatible_node(ssi_np, NULL, "fsl,mpc8610-ssi") {
856*4882a593Smuzhiyun 		/* Check each DMA phandle to see if it points to us.  We
857*4882a593Smuzhiyun 		 * assume that device_node pointers are a valid comparison.
858*4882a593Smuzhiyun 		 */
859*4882a593Smuzhiyun 		np = of_parse_phandle(ssi_np, "fsl,playback-dma", 0);
860*4882a593Smuzhiyun 		of_node_put(np);
861*4882a593Smuzhiyun 		if (np == dma_channel_np)
862*4882a593Smuzhiyun 			return ssi_np;
863*4882a593Smuzhiyun 
864*4882a593Smuzhiyun 		np = of_parse_phandle(ssi_np, "fsl,capture-dma", 0);
865*4882a593Smuzhiyun 		of_node_put(np);
866*4882a593Smuzhiyun 		if (np == dma_channel_np)
867*4882a593Smuzhiyun 			return ssi_np;
868*4882a593Smuzhiyun 	}
869*4882a593Smuzhiyun 
870*4882a593Smuzhiyun 	return NULL;
871*4882a593Smuzhiyun }
872*4882a593Smuzhiyun 
fsl_soc_dma_probe(struct platform_device * pdev)873*4882a593Smuzhiyun static int fsl_soc_dma_probe(struct platform_device *pdev)
874*4882a593Smuzhiyun {
875*4882a593Smuzhiyun 	struct dma_object *dma;
876*4882a593Smuzhiyun 	struct device_node *np = pdev->dev.of_node;
877*4882a593Smuzhiyun 	struct device_node *ssi_np;
878*4882a593Smuzhiyun 	struct resource res;
879*4882a593Smuzhiyun 	const uint32_t *iprop;
880*4882a593Smuzhiyun 	int ret;
881*4882a593Smuzhiyun 
882*4882a593Smuzhiyun 	/* Find the SSI node that points to us. */
883*4882a593Smuzhiyun 	ssi_np = find_ssi_node(np);
884*4882a593Smuzhiyun 	if (!ssi_np) {
885*4882a593Smuzhiyun 		dev_err(&pdev->dev, "cannot find parent SSI node\n");
886*4882a593Smuzhiyun 		return -ENODEV;
887*4882a593Smuzhiyun 	}
888*4882a593Smuzhiyun 
889*4882a593Smuzhiyun 	ret = of_address_to_resource(ssi_np, 0, &res);
890*4882a593Smuzhiyun 	if (ret) {
891*4882a593Smuzhiyun 		dev_err(&pdev->dev, "could not determine resources for %pOF\n",
892*4882a593Smuzhiyun 			ssi_np);
893*4882a593Smuzhiyun 		of_node_put(ssi_np);
894*4882a593Smuzhiyun 		return ret;
895*4882a593Smuzhiyun 	}
896*4882a593Smuzhiyun 
897*4882a593Smuzhiyun 	dma = kzalloc(sizeof(*dma), GFP_KERNEL);
898*4882a593Smuzhiyun 	if (!dma) {
899*4882a593Smuzhiyun 		of_node_put(ssi_np);
900*4882a593Smuzhiyun 		return -ENOMEM;
901*4882a593Smuzhiyun 	}
902*4882a593Smuzhiyun 
903*4882a593Smuzhiyun 	dma->dai.name = DRV_NAME;
904*4882a593Smuzhiyun 	dma->dai.open = fsl_dma_open;
905*4882a593Smuzhiyun 	dma->dai.close = fsl_dma_close;
906*4882a593Smuzhiyun 	dma->dai.hw_params = fsl_dma_hw_params;
907*4882a593Smuzhiyun 	dma->dai.hw_free = fsl_dma_hw_free;
908*4882a593Smuzhiyun 	dma->dai.pointer = fsl_dma_pointer;
909*4882a593Smuzhiyun 	dma->dai.pcm_construct = fsl_dma_new;
910*4882a593Smuzhiyun 	dma->dai.pcm_destruct = fsl_dma_free_dma_buffers;
911*4882a593Smuzhiyun 
912*4882a593Smuzhiyun 	/* Store the SSI-specific information that we need */
913*4882a593Smuzhiyun 	dma->ssi_stx_phys = res.start + REG_SSI_STX0;
914*4882a593Smuzhiyun 	dma->ssi_srx_phys = res.start + REG_SSI_SRX0;
915*4882a593Smuzhiyun 
916*4882a593Smuzhiyun 	iprop = of_get_property(ssi_np, "fsl,fifo-depth", NULL);
917*4882a593Smuzhiyun 	if (iprop)
918*4882a593Smuzhiyun 		dma->ssi_fifo_depth = be32_to_cpup(iprop);
919*4882a593Smuzhiyun 	else
920*4882a593Smuzhiyun                 /* Older 8610 DTs didn't have the fifo-depth property */
921*4882a593Smuzhiyun 		dma->ssi_fifo_depth = 8;
922*4882a593Smuzhiyun 
923*4882a593Smuzhiyun 	of_node_put(ssi_np);
924*4882a593Smuzhiyun 
925*4882a593Smuzhiyun 	ret = devm_snd_soc_register_component(&pdev->dev, &dma->dai, NULL, 0);
926*4882a593Smuzhiyun 	if (ret) {
927*4882a593Smuzhiyun 		dev_err(&pdev->dev, "could not register platform\n");
928*4882a593Smuzhiyun 		kfree(dma);
929*4882a593Smuzhiyun 		return ret;
930*4882a593Smuzhiyun 	}
931*4882a593Smuzhiyun 
932*4882a593Smuzhiyun 	dma->channel = of_iomap(np, 0);
933*4882a593Smuzhiyun 	dma->irq = irq_of_parse_and_map(np, 0);
934*4882a593Smuzhiyun 
935*4882a593Smuzhiyun 	dev_set_drvdata(&pdev->dev, dma);
936*4882a593Smuzhiyun 
937*4882a593Smuzhiyun 	return 0;
938*4882a593Smuzhiyun }
939*4882a593Smuzhiyun 
fsl_soc_dma_remove(struct platform_device * pdev)940*4882a593Smuzhiyun static int fsl_soc_dma_remove(struct platform_device *pdev)
941*4882a593Smuzhiyun {
942*4882a593Smuzhiyun 	struct dma_object *dma = dev_get_drvdata(&pdev->dev);
943*4882a593Smuzhiyun 
944*4882a593Smuzhiyun 	iounmap(dma->channel);
945*4882a593Smuzhiyun 	irq_dispose_mapping(dma->irq);
946*4882a593Smuzhiyun 	kfree(dma);
947*4882a593Smuzhiyun 
948*4882a593Smuzhiyun 	return 0;
949*4882a593Smuzhiyun }
950*4882a593Smuzhiyun 
951*4882a593Smuzhiyun static const struct of_device_id fsl_soc_dma_ids[] = {
952*4882a593Smuzhiyun 	{ .compatible = "fsl,ssi-dma-channel", },
953*4882a593Smuzhiyun 	{}
954*4882a593Smuzhiyun };
955*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, fsl_soc_dma_ids);
956*4882a593Smuzhiyun 
957*4882a593Smuzhiyun static struct platform_driver fsl_soc_dma_driver = {
958*4882a593Smuzhiyun 	.driver = {
959*4882a593Smuzhiyun 		.name = "fsl-pcm-audio",
960*4882a593Smuzhiyun 		.of_match_table = fsl_soc_dma_ids,
961*4882a593Smuzhiyun 	},
962*4882a593Smuzhiyun 	.probe = fsl_soc_dma_probe,
963*4882a593Smuzhiyun 	.remove = fsl_soc_dma_remove,
964*4882a593Smuzhiyun };
965*4882a593Smuzhiyun 
966*4882a593Smuzhiyun module_platform_driver(fsl_soc_dma_driver);
967*4882a593Smuzhiyun 
968*4882a593Smuzhiyun MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
969*4882a593Smuzhiyun MODULE_DESCRIPTION("Freescale Elo DMA ASoC PCM Driver");
970*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
971