/*1* Freescale DMA ALSA SoC PCM driver2*3* Author: Timur Tabi <[email protected]>4*5* Copyright 2007-2010 Freescale Semiconductor, Inc.6*7* This file is licensed under the terms of the GNU General Public License8* version 2. This program is licensed "as is" without any warranty of any9* kind, whether express or implied.10*11* This driver implements ASoC support for the Elo DMA controller, which is12* the DMA controller on Freescale 83xx, 85xx, and 86xx SOCs. In ALSA terms,13* the PCM driver is what handles the DMA buffer.14*/1516#include <linux/module.h>17#include <linux/init.h>18#include <linux/platform_device.h>19#include <linux/dma-mapping.h>20#include <linux/interrupt.h>21#include <linux/delay.h>22#include <linux/gfp.h>23#include <linux/of_platform.h>24#include <linux/list.h>25#include <linux/slab.h>2627#include <sound/core.h>28#include <sound/pcm.h>29#include <sound/pcm_params.h>30#include <sound/soc.h>3132#include <asm/io.h>3334#include "fsl_dma.h"35#include "fsl_ssi.h" /* For the offset of stx0 and srx0 */3637/*38* The formats that the DMA controller supports, which is anything39* that is 8, 16, or 32 bits.40*/41#define FSLDMA_PCM_FORMATS (SNDRV_PCM_FMTBIT_S8 | \42SNDRV_PCM_FMTBIT_U8 | \43SNDRV_PCM_FMTBIT_S16_LE | \44SNDRV_PCM_FMTBIT_S16_BE | \45SNDRV_PCM_FMTBIT_U16_LE | \46SNDRV_PCM_FMTBIT_U16_BE | \47SNDRV_PCM_FMTBIT_S24_LE | \48SNDRV_PCM_FMTBIT_S24_BE | \49SNDRV_PCM_FMTBIT_U24_LE | \50SNDRV_PCM_FMTBIT_U24_BE | \51SNDRV_PCM_FMTBIT_S32_LE | \52SNDRV_PCM_FMTBIT_S32_BE | \53SNDRV_PCM_FMTBIT_U32_LE | \54SNDRV_PCM_FMTBIT_U32_BE)5556#define FSLDMA_PCM_RATES (SNDRV_PCM_RATE_5512 | SNDRV_PCM_RATE_8000_192000 | \57SNDRV_PCM_RATE_CONTINUOUS)5859struct dma_object {60struct snd_soc_platform_driver dai;61dma_addr_t ssi_stx_phys;62dma_addr_t ssi_srx_phys;63unsigned int ssi_fifo_depth;64struct ccsr_dma_channel __iomem *channel;65unsigned int irq;66bool assigned;67char path[1];68};6970/*71* The number of DMA links to use. Two is the bare minimum, but if you72* have really small links you might need more.73*/74#define NUM_DMA_LINKS 27576/** fsl_dma_private: p-substream DMA data77*78* Each substream has a 1-to-1 association with a DMA channel.79*80* The link[] array is first because it needs to be aligned on a 32-byte81* boundary, so putting it first will ensure alignment without padding the82* structure.83*84* @link[]: array of link descriptors85* @dma_channel: pointer to the DMA channel's registers86* @irq: IRQ for this DMA channel87* @substream: pointer to the substream object, needed by the ISR88* @ssi_sxx_phys: bus address of the STX or SRX register to use89* @ld_buf_phys: physical address of the LD buffer90* @current_link: index into link[] of the link currently being processed91* @dma_buf_phys: physical address of the DMA buffer92* @dma_buf_next: physical address of the next period to process93* @dma_buf_end: physical address of the byte after the end of the DMA94* @buffer period_size: the size of a single period95* @num_periods: the number of periods in the DMA buffer96*/97struct fsl_dma_private {98struct fsl_dma_link_descriptor link[NUM_DMA_LINKS];99struct ccsr_dma_channel __iomem *dma_channel;100unsigned int irq;101struct snd_pcm_substream *substream;102dma_addr_t ssi_sxx_phys;103unsigned int ssi_fifo_depth;104dma_addr_t ld_buf_phys;105unsigned int current_link;106dma_addr_t dma_buf_phys;107dma_addr_t dma_buf_next;108dma_addr_t dma_buf_end;109size_t period_size;110unsigned int num_periods;111};112113/**114* fsl_dma_hardare: define characteristics of the PCM hardware.115*116* The PCM hardware is the Freescale DMA controller. This structure defines117* the capabilities of that hardware.118*119* Since the sampling rate and data format are not controlled by the DMA120* controller, we specify no limits for those values. The only exception is121* period_bytes_min, which is set to a reasonably low value to prevent the122* DMA controller from generating too many interrupts per second.123*124* Since each link descriptor has a 32-bit byte count field, we set125* period_bytes_max to the largest 32-bit number. We also have no maximum126* number of periods.127*128* Note that we specify SNDRV_PCM_INFO_JOINT_DUPLEX here, but only because a129* limitation in the SSI driver requires the sample rates for playback and130* capture to be the same.131*/132static const struct snd_pcm_hardware fsl_dma_hardware = {133134.info = SNDRV_PCM_INFO_INTERLEAVED |135SNDRV_PCM_INFO_MMAP |136SNDRV_PCM_INFO_MMAP_VALID |137SNDRV_PCM_INFO_JOINT_DUPLEX |138SNDRV_PCM_INFO_PAUSE,139.formats = FSLDMA_PCM_FORMATS,140.rates = FSLDMA_PCM_RATES,141.rate_min = 5512,142.rate_max = 192000,143.period_bytes_min = 512, /* A reasonable limit */144.period_bytes_max = (u32) -1,145.periods_min = NUM_DMA_LINKS,146.periods_max = (unsigned int) -1,147.buffer_bytes_max = 128 * 1024, /* A reasonable limit */148};149150/**151* fsl_dma_abort_stream: tell ALSA that the DMA transfer has aborted152*153* This function should be called by the ISR whenever the DMA controller154* halts data transfer.155*/156static void fsl_dma_abort_stream(struct snd_pcm_substream *substream)157{158unsigned long flags;159160snd_pcm_stream_lock_irqsave(substream, flags);161162if (snd_pcm_running(substream))163snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);164165snd_pcm_stream_unlock_irqrestore(substream, flags);166}167168/**169* fsl_dma_update_pointers - update LD pointers to point to the next period170*171* As each period is completed, this function changes the the link172* descriptor pointers for that period to point to the next period.173*/174static void fsl_dma_update_pointers(struct fsl_dma_private *dma_private)175{176struct fsl_dma_link_descriptor *link =177&dma_private->link[dma_private->current_link];178179/* Update our link descriptors to point to the next period. On a 36-bit180* system, we also need to update the ESAD bits. We also set (keep) the181* snoop bits. See the comments in fsl_dma_hw_params() about snooping.182*/183if (dma_private->substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {184link->source_addr = cpu_to_be32(dma_private->dma_buf_next);185#ifdef CONFIG_PHYS_64BIT186link->source_attr = cpu_to_be32(CCSR_DMA_ATR_SNOOP |187upper_32_bits(dma_private->dma_buf_next));188#endif189} else {190link->dest_addr = cpu_to_be32(dma_private->dma_buf_next);191#ifdef CONFIG_PHYS_64BIT192link->dest_attr = cpu_to_be32(CCSR_DMA_ATR_SNOOP |193upper_32_bits(dma_private->dma_buf_next));194#endif195}196197/* Update our variables for next time */198dma_private->dma_buf_next += dma_private->period_size;199200if (dma_private->dma_buf_next >= dma_private->dma_buf_end)201dma_private->dma_buf_next = dma_private->dma_buf_phys;202203if (++dma_private->current_link >= NUM_DMA_LINKS)204dma_private->current_link = 0;205}206207/**208* fsl_dma_isr: interrupt handler for the DMA controller209*210* @irq: IRQ of the DMA channel211* @dev_id: pointer to the dma_private structure for this DMA channel212*/213static irqreturn_t fsl_dma_isr(int irq, void *dev_id)214{215struct fsl_dma_private *dma_private = dev_id;216struct snd_pcm_substream *substream = dma_private->substream;217struct snd_soc_pcm_runtime *rtd = substream->private_data;218struct device *dev = rtd->platform->dev;219struct ccsr_dma_channel __iomem *dma_channel = dma_private->dma_channel;220irqreturn_t ret = IRQ_NONE;221u32 sr, sr2 = 0;222223/* We got an interrupt, so read the status register to see what we224were interrupted for.225*/226sr = in_be32(&dma_channel->sr);227228if (sr & CCSR_DMA_SR_TE) {229dev_err(dev, "dma transmit error\n");230fsl_dma_abort_stream(substream);231sr2 |= CCSR_DMA_SR_TE;232ret = IRQ_HANDLED;233}234235if (sr & CCSR_DMA_SR_CH)236ret = IRQ_HANDLED;237238if (sr & CCSR_DMA_SR_PE) {239dev_err(dev, "dma programming error\n");240fsl_dma_abort_stream(substream);241sr2 |= CCSR_DMA_SR_PE;242ret = IRQ_HANDLED;243}244245if (sr & CCSR_DMA_SR_EOLNI) {246sr2 |= CCSR_DMA_SR_EOLNI;247ret = IRQ_HANDLED;248}249250if (sr & CCSR_DMA_SR_CB)251ret = IRQ_HANDLED;252253if (sr & CCSR_DMA_SR_EOSI) {254/* Tell ALSA we completed a period. */255snd_pcm_period_elapsed(substream);256257/*258* Update our link descriptors to point to the next period. We259* only need to do this if the number of periods is not equal to260* the number of links.261*/262if (dma_private->num_periods != NUM_DMA_LINKS)263fsl_dma_update_pointers(dma_private);264265sr2 |= CCSR_DMA_SR_EOSI;266ret = IRQ_HANDLED;267}268269if (sr & CCSR_DMA_SR_EOLSI) {270sr2 |= CCSR_DMA_SR_EOLSI;271ret = IRQ_HANDLED;272}273274/* Clear the bits that we set */275if (sr2)276out_be32(&dma_channel->sr, sr2);277278return ret;279}280281/**282* fsl_dma_new: initialize this PCM driver.283*284* This function is called when the codec driver calls snd_soc_new_pcms(),285* once for each .dai_link in the machine driver's snd_soc_card286* structure.287*288* snd_dma_alloc_pages() is just a front-end to dma_alloc_coherent(), which289* (currently) always allocates the DMA buffer in lowmem, even if GFP_HIGHMEM290* is specified. Therefore, any DMA buffers we allocate will always be in low291* memory, but we support for 36-bit physical addresses anyway.292*293* Regardless of where the memory is actually allocated, since the device can294* technically DMA to any 36-bit address, we do need to set the DMA mask to 36.295*/296static int fsl_dma_new(struct snd_card *card, struct snd_soc_dai *dai,297struct snd_pcm *pcm)298{299static u64 fsl_dma_dmamask = DMA_BIT_MASK(36);300int ret;301302if (!card->dev->dma_mask)303card->dev->dma_mask = &fsl_dma_dmamask;304305if (!card->dev->coherent_dma_mask)306card->dev->coherent_dma_mask = fsl_dma_dmamask;307308/* Some codecs have separate DAIs for playback and capture, so we309* should allocate a DMA buffer only for the streams that are valid.310*/311312if (pcm->streams[0].substream) {313ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, card->dev,314fsl_dma_hardware.buffer_bytes_max,315&pcm->streams[0].substream->dma_buffer);316if (ret) {317dev_err(card->dev, "can't alloc playback dma buffer\n");318return ret;319}320}321322if (pcm->streams[1].substream) {323ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, card->dev,324fsl_dma_hardware.buffer_bytes_max,325&pcm->streams[1].substream->dma_buffer);326if (ret) {327dev_err(card->dev, "can't alloc capture dma buffer\n");328snd_dma_free_pages(&pcm->streams[0].substream->dma_buffer);329return ret;330}331}332333return 0;334}335336/**337* fsl_dma_open: open a new substream.338*339* Each substream has its own DMA buffer.340*341* ALSA divides the DMA buffer into N periods. We create NUM_DMA_LINKS link342* descriptors that ping-pong from one period to the next. For example, if343* there are six periods and two link descriptors, this is how they look344* before playback starts:345*346* The last link descriptor347* ____________ points back to the first348* | |349* V |350* ___ ___ |351* | |->| |->|352* |___| |___|353* | |354* | |355* V V356* _________________________________________357* | | | | | | | The DMA buffer is358* | | | | | | | divided into 6 parts359* |______|______|______|______|______|______|360*361* and here's how they look after the first period is finished playing:362*363* ____________364* | |365* V |366* ___ ___ |367* | |->| |->|368* |___| |___|369* | |370* |______________371* | |372* V V373* _________________________________________374* | | | | | | |375* | | | | | | |376* |______|______|______|______|______|______|377*378* The first link descriptor now points to the third period. The DMA379* controller is currently playing the second period. When it finishes, it380* will jump back to the first descriptor and play the third period.381*382* There are four reasons we do this:383*384* 1. The only way to get the DMA controller to automatically restart the385* transfer when it gets to the end of the buffer is to use chaining386* mode. Basic direct mode doesn't offer that feature.387* 2. We need to receive an interrupt at the end of every period. The DMA388* controller can generate an interrupt at the end of every link transfer389* (aka segment). Making each period into a DMA segment will give us the390* interrupts we need.391* 3. By creating only two link descriptors, regardless of the number of392* periods, we do not need to reallocate the link descriptors if the393* number of periods changes.394* 4. All of the audio data is still stored in a single, contiguous DMA395* buffer, which is what ALSA expects. We're just dividing it into396* contiguous parts, and creating a link descriptor for each one.397*/398static int fsl_dma_open(struct snd_pcm_substream *substream)399{400struct snd_pcm_runtime *runtime = substream->runtime;401struct snd_soc_pcm_runtime *rtd = substream->private_data;402struct device *dev = rtd->platform->dev;403struct dma_object *dma =404container_of(rtd->platform->driver, struct dma_object, dai);405struct fsl_dma_private *dma_private;406struct ccsr_dma_channel __iomem *dma_channel;407dma_addr_t ld_buf_phys;408u64 temp_link; /* Pointer to next link descriptor */409u32 mr;410unsigned int channel;411int ret = 0;412unsigned int i;413414/*415* Reject any DMA buffer whose size is not a multiple of the period416* size. We need to make sure that the DMA buffer can be evenly divided417* into periods.418*/419ret = snd_pcm_hw_constraint_integer(runtime,420SNDRV_PCM_HW_PARAM_PERIODS);421if (ret < 0) {422dev_err(dev, "invalid buffer size\n");423return ret;424}425426channel = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ? 0 : 1;427428if (dma->assigned) {429dev_err(dev, "dma channel already assigned\n");430return -EBUSY;431}432433dma_private = dma_alloc_coherent(dev, sizeof(struct fsl_dma_private),434&ld_buf_phys, GFP_KERNEL);435if (!dma_private) {436dev_err(dev, "can't allocate dma private data\n");437return -ENOMEM;438}439if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)440dma_private->ssi_sxx_phys = dma->ssi_stx_phys;441else442dma_private->ssi_sxx_phys = dma->ssi_srx_phys;443444dma_private->ssi_fifo_depth = dma->ssi_fifo_depth;445dma_private->dma_channel = dma->channel;446dma_private->irq = dma->irq;447dma_private->substream = substream;448dma_private->ld_buf_phys = ld_buf_phys;449dma_private->dma_buf_phys = substream->dma_buffer.addr;450451ret = request_irq(dma_private->irq, fsl_dma_isr, 0, "fsldma-audio",452dma_private);453if (ret) {454dev_err(dev, "can't register ISR for IRQ %u (ret=%i)\n",455dma_private->irq, ret);456dma_free_coherent(dev, sizeof(struct fsl_dma_private),457dma_private, dma_private->ld_buf_phys);458return ret;459}460461dma->assigned = 1;462463snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);464snd_soc_set_runtime_hwparams(substream, &fsl_dma_hardware);465runtime->private_data = dma_private;466467/* Program the fixed DMA controller parameters */468469dma_channel = dma_private->dma_channel;470471temp_link = dma_private->ld_buf_phys +472sizeof(struct fsl_dma_link_descriptor);473474for (i = 0; i < NUM_DMA_LINKS; i++) {475dma_private->link[i].next = cpu_to_be64(temp_link);476477temp_link += sizeof(struct fsl_dma_link_descriptor);478}479/* The last link descriptor points to the first */480dma_private->link[i - 1].next = cpu_to_be64(dma_private->ld_buf_phys);481482/* Tell the DMA controller where the first link descriptor is */483out_be32(&dma_channel->clndar,484CCSR_DMA_CLNDAR_ADDR(dma_private->ld_buf_phys));485out_be32(&dma_channel->eclndar,486CCSR_DMA_ECLNDAR_ADDR(dma_private->ld_buf_phys));487488/* The manual says the BCR must be clear before enabling EMP */489out_be32(&dma_channel->bcr, 0);490491/*492* Program the mode register for interrupts, external master control,493* and source/destination hold. Also clear the Channel Abort bit.494*/495mr = in_be32(&dma_channel->mr) &496~(CCSR_DMA_MR_CA | CCSR_DMA_MR_DAHE | CCSR_DMA_MR_SAHE);497498/*499* We want External Master Start and External Master Pause enabled,500* because the SSI is controlling the DMA controller. We want the DMA501* controller to be set up in advance, and then we signal only the SSI502* to start transferring.503*504* We want End-Of-Segment Interrupts enabled, because this will generate505* an interrupt at the end of each segment (each link descriptor506* represents one segment). Each DMA segment is the same thing as an507* ALSA period, so this is how we get an interrupt at the end of every508* period.509*510* We want Error Interrupt enabled, so that we can get an error if511* the DMA controller is mis-programmed somehow.512*/513mr |= CCSR_DMA_MR_EOSIE | CCSR_DMA_MR_EIE | CCSR_DMA_MR_EMP_EN |514CCSR_DMA_MR_EMS_EN;515516/* For playback, we want the destination address to be held. For517capture, set the source address to be held. */518mr |= (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ?519CCSR_DMA_MR_DAHE : CCSR_DMA_MR_SAHE;520521out_be32(&dma_channel->mr, mr);522523return 0;524}525526/**527* fsl_dma_hw_params: continue initializing the DMA links528*529* This function obtains hardware parameters about the opened stream and530* programs the DMA controller accordingly.531*532* One drawback of big-endian is that when copying integers of different533* sizes to a fixed-sized register, the address to which the integer must be534* copied is dependent on the size of the integer.535*536* For example, if P is the address of a 32-bit register, and X is a 32-bit537* integer, then X should be copied to address P. However, if X is a 16-bit538* integer, then it should be copied to P+2. If X is an 8-bit register,539* then it should be copied to P+3.540*541* So for playback of 8-bit samples, the DMA controller must transfer single542* bytes from the DMA buffer to the last byte of the STX0 register, i.e.543* offset by 3 bytes. For 16-bit samples, the offset is two bytes.544*545* For 24-bit samples, the offset is 1 byte. However, the DMA controller546* does not support 3-byte copies (the DAHTS register supports only 1, 2, 4,547* and 8 bytes at a time). So we do not support packed 24-bit samples.548* 24-bit data must be padded to 32 bits.549*/550static int fsl_dma_hw_params(struct snd_pcm_substream *substream,551struct snd_pcm_hw_params *hw_params)552{553struct snd_pcm_runtime *runtime = substream->runtime;554struct fsl_dma_private *dma_private = runtime->private_data;555struct snd_soc_pcm_runtime *rtd = substream->private_data;556struct device *dev = rtd->platform->dev;557558/* Number of bits per sample */559unsigned int sample_bits =560snd_pcm_format_physical_width(params_format(hw_params));561562/* Number of bytes per frame */563unsigned int sample_bytes = sample_bits / 8;564565/* Bus address of SSI STX register */566dma_addr_t ssi_sxx_phys = dma_private->ssi_sxx_phys;567568/* Size of the DMA buffer, in bytes */569size_t buffer_size = params_buffer_bytes(hw_params);570571/* Number of bytes per period */572size_t period_size = params_period_bytes(hw_params);573574/* Pointer to next period */575dma_addr_t temp_addr = substream->dma_buffer.addr;576577/* Pointer to DMA controller */578struct ccsr_dma_channel __iomem *dma_channel = dma_private->dma_channel;579580u32 mr; /* DMA Mode Register */581582unsigned int i;583584/* Initialize our DMA tracking variables */585dma_private->period_size = period_size;586dma_private->num_periods = params_periods(hw_params);587dma_private->dma_buf_end = dma_private->dma_buf_phys + buffer_size;588dma_private->dma_buf_next = dma_private->dma_buf_phys +589(NUM_DMA_LINKS * period_size);590591if (dma_private->dma_buf_next >= dma_private->dma_buf_end)592/* This happens if the number of periods == NUM_DMA_LINKS */593dma_private->dma_buf_next = dma_private->dma_buf_phys;594595mr = in_be32(&dma_channel->mr) & ~(CCSR_DMA_MR_BWC_MASK |596CCSR_DMA_MR_SAHTS_MASK | CCSR_DMA_MR_DAHTS_MASK);597598/* Due to a quirk of the SSI's STX register, the target address599* for the DMA operations depends on the sample size. So we calculate600* that offset here. While we're at it, also tell the DMA controller601* how much data to transfer per sample.602*/603switch (sample_bits) {604case 8:605mr |= CCSR_DMA_MR_DAHTS_1 | CCSR_DMA_MR_SAHTS_1;606ssi_sxx_phys += 3;607break;608case 16:609mr |= CCSR_DMA_MR_DAHTS_2 | CCSR_DMA_MR_SAHTS_2;610ssi_sxx_phys += 2;611break;612case 32:613mr |= CCSR_DMA_MR_DAHTS_4 | CCSR_DMA_MR_SAHTS_4;614break;615default:616/* We should never get here */617dev_err(dev, "unsupported sample size %u\n", sample_bits);618return -EINVAL;619}620621/*622* BWC determines how many bytes are sent/received before the DMA623* controller checks the SSI to see if it needs to stop. BWC should624* always be a multiple of the frame size, so that we always transmit625* whole frames. Each frame occupies two slots in the FIFO. The626* parameter for CCSR_DMA_MR_BWC() is rounded down the next power of two627* (MR[BWC] can only represent even powers of two).628*629* To simplify the process, we set BWC to the largest value that is630* less than or equal to the FIFO watermark. For playback, this ensures631* that we transfer the maximum amount without overrunning the FIFO.632* For capture, this ensures that we transfer the maximum amount without633* underrunning the FIFO.634*635* f = SSI FIFO depth636* w = SSI watermark value (which equals f - 2)637* b = DMA bandwidth count (in bytes)638* s = sample size (in bytes, which equals frame_size * 2)639*640* For playback, we never transmit more than the transmit FIFO641* watermark, otherwise we might write more data than the FIFO can hold.642* The watermark is equal to the FIFO depth minus two.643*644* For capture, two equations must hold:645* w > f - (b / s)646* w >= b / s647*648* So, b > 2 * s, but b must also be <= s * w. To simplify, we set649* b = s * w, which is equal to650* (dma_private->ssi_fifo_depth - 2) * sample_bytes.651*/652mr |= CCSR_DMA_MR_BWC((dma_private->ssi_fifo_depth - 2) * sample_bytes);653654out_be32(&dma_channel->mr, mr);655656for (i = 0; i < NUM_DMA_LINKS; i++) {657struct fsl_dma_link_descriptor *link = &dma_private->link[i];658659link->count = cpu_to_be32(period_size);660661/* The snoop bit tells the DMA controller whether it should tell662* the ECM to snoop during a read or write to an address. For663* audio, we use DMA to transfer data between memory and an I/O664* device (the SSI's STX0 or SRX0 register). Snooping is only665* needed if there is a cache, so we need to snoop memory666* addresses only. For playback, that means we snoop the source667* but not the destination. For capture, we snoop the668* destination but not the source.669*670* Note that failing to snoop properly is unlikely to cause671* cache incoherency if the period size is larger than the672* size of L1 cache. This is because filling in one period will673* flush out the data for the previous period. So if you674* increased period_bytes_min to a large enough size, you might675* get more performance by not snooping, and you'll still be676* okay. You'll need to update fsl_dma_update_pointers() also.677*/678if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {679link->source_addr = cpu_to_be32(temp_addr);680link->source_attr = cpu_to_be32(CCSR_DMA_ATR_SNOOP |681upper_32_bits(temp_addr));682683link->dest_addr = cpu_to_be32(ssi_sxx_phys);684link->dest_attr = cpu_to_be32(CCSR_DMA_ATR_NOSNOOP |685upper_32_bits(ssi_sxx_phys));686} else {687link->source_addr = cpu_to_be32(ssi_sxx_phys);688link->source_attr = cpu_to_be32(CCSR_DMA_ATR_NOSNOOP |689upper_32_bits(ssi_sxx_phys));690691link->dest_addr = cpu_to_be32(temp_addr);692link->dest_attr = cpu_to_be32(CCSR_DMA_ATR_SNOOP |693upper_32_bits(temp_addr));694}695696temp_addr += period_size;697}698699return 0;700}701702/**703* fsl_dma_pointer: determine the current position of the DMA transfer704*705* This function is called by ALSA when ALSA wants to know where in the706* stream buffer the hardware currently is.707*708* For playback, the SAR register contains the physical address of the most709* recent DMA transfer. For capture, the value is in the DAR register.710*711* The base address of the buffer is stored in the source_addr field of the712* first link descriptor.713*/714static snd_pcm_uframes_t fsl_dma_pointer(struct snd_pcm_substream *substream)715{716struct snd_pcm_runtime *runtime = substream->runtime;717struct fsl_dma_private *dma_private = runtime->private_data;718struct snd_soc_pcm_runtime *rtd = substream->private_data;719struct device *dev = rtd->platform->dev;720struct ccsr_dma_channel __iomem *dma_channel = dma_private->dma_channel;721dma_addr_t position;722snd_pcm_uframes_t frames;723724/* Obtain the current DMA pointer, but don't read the ESAD bits if we725* only have 32-bit DMA addresses. This function is typically called726* in interrupt context, so we need to optimize it.727*/728if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {729position = in_be32(&dma_channel->sar);730#ifdef CONFIG_PHYS_64BIT731position |= (u64)(in_be32(&dma_channel->satr) &732CCSR_DMA_ATR_ESAD_MASK) << 32;733#endif734} else {735position = in_be32(&dma_channel->dar);736#ifdef CONFIG_PHYS_64BIT737position |= (u64)(in_be32(&dma_channel->datr) &738CCSR_DMA_ATR_ESAD_MASK) << 32;739#endif740}741742/*743* When capture is started, the SSI immediately starts to fill its FIFO.744* This means that the DMA controller is not started until the FIFO is745* full. However, ALSA calls this function before that happens, when746* MR.DAR is still zero. In this case, just return zero to indicate747* that nothing has been received yet.748*/749if (!position)750return 0;751752if ((position < dma_private->dma_buf_phys) ||753(position > dma_private->dma_buf_end)) {754dev_err(dev, "dma pointer is out of range, halting stream\n");755return SNDRV_PCM_POS_XRUN;756}757758frames = bytes_to_frames(runtime, position - dma_private->dma_buf_phys);759760/*761* If the current address is just past the end of the buffer, wrap it762* around.763*/764if (frames == runtime->buffer_size)765frames = 0;766767return frames;768}769770/**771* fsl_dma_hw_free: release resources allocated in fsl_dma_hw_params()772*773* Release the resources allocated in fsl_dma_hw_params() and de-program the774* registers.775*776* This function can be called multiple times.777*/778static int fsl_dma_hw_free(struct snd_pcm_substream *substream)779{780struct snd_pcm_runtime *runtime = substream->runtime;781struct fsl_dma_private *dma_private = runtime->private_data;782783if (dma_private) {784struct ccsr_dma_channel __iomem *dma_channel;785786dma_channel = dma_private->dma_channel;787788/* Stop the DMA */789out_be32(&dma_channel->mr, CCSR_DMA_MR_CA);790out_be32(&dma_channel->mr, 0);791792/* Reset all the other registers */793out_be32(&dma_channel->sr, -1);794out_be32(&dma_channel->clndar, 0);795out_be32(&dma_channel->eclndar, 0);796out_be32(&dma_channel->satr, 0);797out_be32(&dma_channel->sar, 0);798out_be32(&dma_channel->datr, 0);799out_be32(&dma_channel->dar, 0);800out_be32(&dma_channel->bcr, 0);801out_be32(&dma_channel->nlndar, 0);802out_be32(&dma_channel->enlndar, 0);803}804805return 0;806}807808/**809* fsl_dma_close: close the stream.810*/811static int fsl_dma_close(struct snd_pcm_substream *substream)812{813struct snd_pcm_runtime *runtime = substream->runtime;814struct fsl_dma_private *dma_private = runtime->private_data;815struct snd_soc_pcm_runtime *rtd = substream->private_data;816struct device *dev = rtd->platform->dev;817struct dma_object *dma =818container_of(rtd->platform->driver, struct dma_object, dai);819820if (dma_private) {821if (dma_private->irq)822free_irq(dma_private->irq, dma_private);823824if (dma_private->ld_buf_phys) {825dma_unmap_single(dev, dma_private->ld_buf_phys,826sizeof(dma_private->link),827DMA_TO_DEVICE);828}829830/* Deallocate the fsl_dma_private structure */831dma_free_coherent(dev, sizeof(struct fsl_dma_private),832dma_private, dma_private->ld_buf_phys);833substream->runtime->private_data = NULL;834}835836dma->assigned = 0;837838return 0;839}840841/*842* Remove this PCM driver.843*/844static void fsl_dma_free_dma_buffers(struct snd_pcm *pcm)845{846struct snd_pcm_substream *substream;847unsigned int i;848849for (i = 0; i < ARRAY_SIZE(pcm->streams); i++) {850substream = pcm->streams[i].substream;851if (substream) {852snd_dma_free_pages(&substream->dma_buffer);853substream->dma_buffer.area = NULL;854substream->dma_buffer.addr = 0;855}856}857}858859/**860* find_ssi_node -- returns the SSI node that points to his DMA channel node861*862* Although this DMA driver attempts to operate independently of the other863* devices, it still needs to determine some information about the SSI device864* that it's working with. Unfortunately, the device tree does not contain865* a pointer from the DMA channel node to the SSI node -- the pointer goes the866* other way. So we need to scan the device tree for SSI nodes until we find867* the one that points to the given DMA channel node. It's ugly, but at least868* it's contained in this one function.869*/870static struct device_node *find_ssi_node(struct device_node *dma_channel_np)871{872struct device_node *ssi_np, *np;873874for_each_compatible_node(ssi_np, NULL, "fsl,mpc8610-ssi") {875/* Check each DMA phandle to see if it points to us. We876* assume that device_node pointers are a valid comparison.877*/878np = of_parse_phandle(ssi_np, "fsl,playback-dma", 0);879if (np == dma_channel_np)880return ssi_np;881882np = of_parse_phandle(ssi_np, "fsl,capture-dma", 0);883if (np == dma_channel_np)884return ssi_np;885}886887return NULL;888}889890static struct snd_pcm_ops fsl_dma_ops = {891.open = fsl_dma_open,892.close = fsl_dma_close,893.ioctl = snd_pcm_lib_ioctl,894.hw_params = fsl_dma_hw_params,895.hw_free = fsl_dma_hw_free,896.pointer = fsl_dma_pointer,897};898899static int __devinit fsl_soc_dma_probe(struct platform_device *pdev)900{901struct dma_object *dma;902struct device_node *np = pdev->dev.of_node;903struct device_node *ssi_np;904struct resource res;905const uint32_t *iprop;906int ret;907908/* Find the SSI node that points to us. */909ssi_np = find_ssi_node(np);910if (!ssi_np) {911dev_err(&pdev->dev, "cannot find parent SSI node\n");912return -ENODEV;913}914915ret = of_address_to_resource(ssi_np, 0, &res);916if (ret) {917dev_err(&pdev->dev, "could not determine resources for %s\n",918ssi_np->full_name);919of_node_put(ssi_np);920return ret;921}922923dma = kzalloc(sizeof(*dma) + strlen(np->full_name), GFP_KERNEL);924if (!dma) {925dev_err(&pdev->dev, "could not allocate dma object\n");926of_node_put(ssi_np);927return -ENOMEM;928}929930strcpy(dma->path, np->full_name);931dma->dai.ops = &fsl_dma_ops;932dma->dai.pcm_new = fsl_dma_new;933dma->dai.pcm_free = fsl_dma_free_dma_buffers;934935/* Store the SSI-specific information that we need */936dma->ssi_stx_phys = res.start + offsetof(struct ccsr_ssi, stx0);937dma->ssi_srx_phys = res.start + offsetof(struct ccsr_ssi, srx0);938939iprop = of_get_property(ssi_np, "fsl,fifo-depth", NULL);940if (iprop)941dma->ssi_fifo_depth = *iprop;942else943/* Older 8610 DTs didn't have the fifo-depth property */944dma->ssi_fifo_depth = 8;945946of_node_put(ssi_np);947948ret = snd_soc_register_platform(&pdev->dev, &dma->dai);949if (ret) {950dev_err(&pdev->dev, "could not register platform\n");951kfree(dma);952return ret;953}954955dma->channel = of_iomap(np, 0);956dma->irq = irq_of_parse_and_map(np, 0);957958dev_set_drvdata(&pdev->dev, dma);959960return 0;961}962963static int __devexit fsl_soc_dma_remove(struct platform_device *pdev)964{965struct dma_object *dma = dev_get_drvdata(&pdev->dev);966967snd_soc_unregister_platform(&pdev->dev);968iounmap(dma->channel);969irq_dispose_mapping(dma->irq);970kfree(dma);971972return 0;973}974975static const struct of_device_id fsl_soc_dma_ids[] = {976{ .compatible = "fsl,ssi-dma-channel", },977{}978};979MODULE_DEVICE_TABLE(of, fsl_soc_dma_ids);980981static struct platform_driver fsl_soc_dma_driver = {982.driver = {983.name = "fsl-pcm-audio",984.owner = THIS_MODULE,985.of_match_table = fsl_soc_dma_ids,986},987.probe = fsl_soc_dma_probe,988.remove = __devexit_p(fsl_soc_dma_remove),989};990991static int __init fsl_soc_dma_init(void)992{993pr_info("Freescale Elo DMA ASoC PCM Driver\n");994995return platform_driver_register(&fsl_soc_dma_driver);996}997998static void __exit fsl_soc_dma_exit(void)999{1000platform_driver_unregister(&fsl_soc_dma_driver);1001}10021003module_init(fsl_soc_dma_init);1004module_exit(fsl_soc_dma_exit);10051006MODULE_AUTHOR("Timur Tabi <[email protected]>");1007MODULE_DESCRIPTION("Freescale Elo DMA ASoC PCM Driver");1008MODULE_LICENSE("GPL v2");100910101011