// SPDX-License-Identifier: GPL-2.01//2// Freescale DMA ALSA SoC PCM driver3//4// Author: Timur Tabi <[email protected]>5//6// Copyright 2007-2010 Freescale Semiconductor, Inc.7//8// This driver implements ASoC support for the Elo DMA controller, which is9// the DMA controller on Freescale 83xx, 85xx, and 86xx SOCs. In ALSA terms,10// the PCM driver is what handles the DMA buffer.1112#include <linux/module.h>13#include <linux/init.h>14#include <linux/platform_device.h>15#include <linux/dma-mapping.h>16#include <linux/interrupt.h>17#include <linux/delay.h>18#include <linux/gfp.h>19#include <linux/of_address.h>20#include <linux/of_irq.h>21#include <linux/of_platform.h>22#include <linux/list.h>23#include <linux/slab.h>2425#include <sound/core.h>26#include <sound/pcm.h>27#include <sound/pcm_params.h>28#include <sound/soc.h>2930#include <asm/io.h>3132#include "fsl_dma.h"33#include "fsl_ssi.h" /* For the offset of stx0 and srx0 */3435#define DRV_NAME "fsl_dma"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)55struct dma_object {56struct snd_soc_component_driver dai;57dma_addr_t ssi_stx_phys;58dma_addr_t ssi_srx_phys;59unsigned int ssi_fifo_depth;60struct ccsr_dma_channel __iomem *channel;61unsigned int irq;62bool assigned;63};6465/*66* The number of DMA links to use. Two is the bare minimum, but if you67* have really small links you might need more.68*/69#define NUM_DMA_LINKS 27071/** fsl_dma_private: p-substream DMA data72*73* Each substream has a 1-to-1 association with a DMA channel.74*75* The link[] array is first because it needs to be aligned on a 32-byte76* boundary, so putting it first will ensure alignment without padding the77* structure.78*79* @link[]: array of link descriptors80* @dma_channel: pointer to the DMA channel's registers81* @irq: IRQ for this DMA channel82* @substream: pointer to the substream object, needed by the ISR83* @ssi_sxx_phys: bus address of the STX or SRX register to use84* @ld_buf_phys: physical address of the LD buffer85* @current_link: index into link[] of the link currently being processed86* @dma_buf_phys: physical address of the DMA buffer87* @dma_buf_next: physical address of the next period to process88* @dma_buf_end: physical address of the byte after the end of the DMA89* @buffer period_size: the size of a single period90* @num_periods: the number of periods in the DMA buffer91*/92struct fsl_dma_private {93struct fsl_dma_link_descriptor link[NUM_DMA_LINKS];94struct ccsr_dma_channel __iomem *dma_channel;95unsigned int irq;96struct snd_pcm_substream *substream;97dma_addr_t ssi_sxx_phys;98unsigned int ssi_fifo_depth;99dma_addr_t ld_buf_phys;100unsigned int current_link;101dma_addr_t dma_buf_phys;102dma_addr_t dma_buf_next;103dma_addr_t dma_buf_end;104size_t period_size;105unsigned int num_periods;106};107108/**109* fsl_dma_hardare: define characteristics of the PCM hardware.110*111* The PCM hardware is the Freescale DMA controller. This structure defines112* the capabilities of that hardware.113*114* Since the sampling rate and data format are not controlled by the DMA115* controller, we specify no limits for those values. The only exception is116* period_bytes_min, which is set to a reasonably low value to prevent the117* DMA controller from generating too many interrupts per second.118*119* Since each link descriptor has a 32-bit byte count field, we set120* period_bytes_max to the largest 32-bit number. We also have no maximum121* number of periods.122*123* Note that we specify SNDRV_PCM_INFO_JOINT_DUPLEX here, but only because a124* limitation in the SSI driver requires the sample rates for playback and125* capture to be the same.126*/127static const struct snd_pcm_hardware fsl_dma_hardware = {128129.info = SNDRV_PCM_INFO_INTERLEAVED |130SNDRV_PCM_INFO_MMAP |131SNDRV_PCM_INFO_MMAP_VALID |132SNDRV_PCM_INFO_JOINT_DUPLEX |133SNDRV_PCM_INFO_PAUSE,134.formats = FSLDMA_PCM_FORMATS,135.period_bytes_min = 512, /* A reasonable limit */136.period_bytes_max = (u32) -1,137.periods_min = NUM_DMA_LINKS,138.periods_max = (unsigned int) -1,139.buffer_bytes_max = 128 * 1024, /* A reasonable limit */140};141142/**143* fsl_dma_abort_stream: tell ALSA that the DMA transfer has aborted144*145* This function should be called by the ISR whenever the DMA controller146* halts data transfer.147*/148static void fsl_dma_abort_stream(struct snd_pcm_substream *substream)149{150snd_pcm_stop_xrun(substream);151}152153/**154* fsl_dma_update_pointers - update LD pointers to point to the next period155*156* As each period is completed, this function changes the link157* descriptor pointers for that period to point to the next period.158*/159static void fsl_dma_update_pointers(struct fsl_dma_private *dma_private)160{161struct fsl_dma_link_descriptor *link =162&dma_private->link[dma_private->current_link];163164/* Update our link descriptors to point to the next period. On a 36-bit165* system, we also need to update the ESAD bits. We also set (keep) the166* snoop bits. See the comments in fsl_dma_hw_params() about snooping.167*/168if (dma_private->substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {169link->source_addr = cpu_to_be32(dma_private->dma_buf_next);170#ifdef CONFIG_PHYS_64BIT171link->source_attr = cpu_to_be32(CCSR_DMA_ATR_SNOOP |172upper_32_bits(dma_private->dma_buf_next));173#endif174} else {175link->dest_addr = cpu_to_be32(dma_private->dma_buf_next);176#ifdef CONFIG_PHYS_64BIT177link->dest_attr = cpu_to_be32(CCSR_DMA_ATR_SNOOP |178upper_32_bits(dma_private->dma_buf_next));179#endif180}181182/* Update our variables for next time */183dma_private->dma_buf_next += dma_private->period_size;184185if (dma_private->dma_buf_next >= dma_private->dma_buf_end)186dma_private->dma_buf_next = dma_private->dma_buf_phys;187188if (++dma_private->current_link >= NUM_DMA_LINKS)189dma_private->current_link = 0;190}191192/**193* fsl_dma_isr: interrupt handler for the DMA controller194*195* @irq: IRQ of the DMA channel196* @dev_id: pointer to the dma_private structure for this DMA channel197*/198static irqreturn_t fsl_dma_isr(int irq, void *dev_id)199{200struct fsl_dma_private *dma_private = dev_id;201struct snd_pcm_substream *substream = dma_private->substream;202struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);203struct device *dev = rtd->dev;204struct ccsr_dma_channel __iomem *dma_channel = dma_private->dma_channel;205irqreturn_t ret = IRQ_NONE;206u32 sr, sr2 = 0;207208/* We got an interrupt, so read the status register to see what we209were interrupted for.210*/211sr = in_be32(&dma_channel->sr);212213if (sr & CCSR_DMA_SR_TE) {214dev_err(dev, "dma transmit error\n");215fsl_dma_abort_stream(substream);216sr2 |= CCSR_DMA_SR_TE;217ret = IRQ_HANDLED;218}219220if (sr & CCSR_DMA_SR_CH)221ret = IRQ_HANDLED;222223if (sr & CCSR_DMA_SR_PE) {224dev_err(dev, "dma programming error\n");225fsl_dma_abort_stream(substream);226sr2 |= CCSR_DMA_SR_PE;227ret = IRQ_HANDLED;228}229230if (sr & CCSR_DMA_SR_EOLNI) {231sr2 |= CCSR_DMA_SR_EOLNI;232ret = IRQ_HANDLED;233}234235if (sr & CCSR_DMA_SR_CB)236ret = IRQ_HANDLED;237238if (sr & CCSR_DMA_SR_EOSI) {239/* Tell ALSA we completed a period. */240snd_pcm_period_elapsed(substream);241242/*243* Update our link descriptors to point to the next period. We244* only need to do this if the number of periods is not equal to245* the number of links.246*/247if (dma_private->num_periods != NUM_DMA_LINKS)248fsl_dma_update_pointers(dma_private);249250sr2 |= CCSR_DMA_SR_EOSI;251ret = IRQ_HANDLED;252}253254if (sr & CCSR_DMA_SR_EOLSI) {255sr2 |= CCSR_DMA_SR_EOLSI;256ret = IRQ_HANDLED;257}258259/* Clear the bits that we set */260if (sr2)261out_be32(&dma_channel->sr, sr2);262263return ret;264}265266/**267* fsl_dma_new: initialize this PCM driver.268*269* This function is called when the codec driver calls snd_soc_new_pcms(),270* once for each .dai_link in the machine driver's snd_soc_card271* structure.272*273* snd_dma_alloc_pages() is just a front-end to dma_alloc_coherent(), which274* (currently) always allocates the DMA buffer in lowmem, even if GFP_HIGHMEM275* is specified. Therefore, any DMA buffers we allocate will always be in low276* memory, but we support for 36-bit physical addresses anyway.277*278* Regardless of where the memory is actually allocated, since the device can279* technically DMA to any 36-bit address, we do need to set the DMA mask to 36.280*/281static int fsl_dma_new(struct snd_soc_component *component,282struct snd_soc_pcm_runtime *rtd)283{284struct snd_card *card = rtd->card->snd_card;285struct snd_pcm *pcm = rtd->pcm;286int ret;287288ret = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(36));289if (ret)290return ret;291292return snd_pcm_set_fixed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,293card->dev,294fsl_dma_hardware.buffer_bytes_max);295}296297/**298* fsl_dma_open: open a new substream.299*300* Each substream has its own DMA buffer.301*302* ALSA divides the DMA buffer into N periods. We create NUM_DMA_LINKS link303* descriptors that ping-pong from one period to the next. For example, if304* there are six periods and two link descriptors, this is how they look305* before playback starts:306*307* The last link descriptor308* ____________ points back to the first309* | |310* V |311* ___ ___ |312* | |->| |->|313* |___| |___|314* | |315* | |316* V V317* _________________________________________318* | | | | | | | The DMA buffer is319* | | | | | | | divided into 6 parts320* |______|______|______|______|______|______|321*322* and here's how they look after the first period is finished playing:323*324* ____________325* | |326* V |327* ___ ___ |328* | |->| |->|329* |___| |___|330* | |331* |______________332* | |333* V V334* _________________________________________335* | | | | | | |336* | | | | | | |337* |______|______|______|______|______|______|338*339* The first link descriptor now points to the third period. The DMA340* controller is currently playing the second period. When it finishes, it341* will jump back to the first descriptor and play the third period.342*343* There are four reasons we do this:344*345* 1. The only way to get the DMA controller to automatically restart the346* transfer when it gets to the end of the buffer is to use chaining347* mode. Basic direct mode doesn't offer that feature.348* 2. We need to receive an interrupt at the end of every period. The DMA349* controller can generate an interrupt at the end of every link transfer350* (aka segment). Making each period into a DMA segment will give us the351* interrupts we need.352* 3. By creating only two link descriptors, regardless of the number of353* periods, we do not need to reallocate the link descriptors if the354* number of periods changes.355* 4. All of the audio data is still stored in a single, contiguous DMA356* buffer, which is what ALSA expects. We're just dividing it into357* contiguous parts, and creating a link descriptor for each one.358*/359static int fsl_dma_open(struct snd_soc_component *component,360struct snd_pcm_substream *substream)361{362struct snd_pcm_runtime *runtime = substream->runtime;363struct device *dev = component->dev;364struct dma_object *dma =365container_of(component->driver, struct dma_object, dai);366struct fsl_dma_private *dma_private;367struct ccsr_dma_channel __iomem *dma_channel;368dma_addr_t ld_buf_phys;369u64 temp_link; /* Pointer to next link descriptor */370u32 mr;371int ret = 0;372unsigned int i;373374/*375* Reject any DMA buffer whose size is not a multiple of the period376* size. We need to make sure that the DMA buffer can be evenly divided377* into periods.378*/379ret = snd_pcm_hw_constraint_integer(runtime,380SNDRV_PCM_HW_PARAM_PERIODS);381if (ret < 0) {382dev_err(dev, "invalid buffer size\n");383return ret;384}385386if (dma->assigned) {387dev_err(dev, "dma channel already assigned\n");388return -EBUSY;389}390391dma_private = dma_alloc_coherent(dev, sizeof(struct fsl_dma_private),392&ld_buf_phys, GFP_KERNEL);393if (!dma_private) {394dev_err(dev, "can't allocate dma private data\n");395return -ENOMEM;396}397if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)398dma_private->ssi_sxx_phys = dma->ssi_stx_phys;399else400dma_private->ssi_sxx_phys = dma->ssi_srx_phys;401402dma_private->ssi_fifo_depth = dma->ssi_fifo_depth;403dma_private->dma_channel = dma->channel;404dma_private->irq = dma->irq;405dma_private->substream = substream;406dma_private->ld_buf_phys = ld_buf_phys;407dma_private->dma_buf_phys = substream->dma_buffer.addr;408409ret = request_irq(dma_private->irq, fsl_dma_isr, 0, "fsldma-audio",410dma_private);411if (ret) {412dev_err(dev, "can't register ISR for IRQ %u (ret=%i)\n",413dma_private->irq, ret);414dma_free_coherent(dev, sizeof(struct fsl_dma_private),415dma_private, dma_private->ld_buf_phys);416return ret;417}418419dma->assigned = true;420421snd_soc_set_runtime_hwparams(substream, &fsl_dma_hardware);422runtime->private_data = dma_private;423424/* Program the fixed DMA controller parameters */425426dma_channel = dma_private->dma_channel;427428temp_link = dma_private->ld_buf_phys +429sizeof(struct fsl_dma_link_descriptor);430431for (i = 0; i < NUM_DMA_LINKS; i++) {432dma_private->link[i].next = cpu_to_be64(temp_link);433434temp_link += sizeof(struct fsl_dma_link_descriptor);435}436/* The last link descriptor points to the first */437dma_private->link[i - 1].next = cpu_to_be64(dma_private->ld_buf_phys);438439/* Tell the DMA controller where the first link descriptor is */440out_be32(&dma_channel->clndar,441CCSR_DMA_CLNDAR_ADDR(dma_private->ld_buf_phys));442out_be32(&dma_channel->eclndar,443CCSR_DMA_ECLNDAR_ADDR(dma_private->ld_buf_phys));444445/* The manual says the BCR must be clear before enabling EMP */446out_be32(&dma_channel->bcr, 0);447448/*449* Program the mode register for interrupts, external master control,450* and source/destination hold. Also clear the Channel Abort bit.451*/452mr = in_be32(&dma_channel->mr) &453~(CCSR_DMA_MR_CA | CCSR_DMA_MR_DAHE | CCSR_DMA_MR_SAHE);454455/*456* We want External Master Start and External Master Pause enabled,457* because the SSI is controlling the DMA controller. We want the DMA458* controller to be set up in advance, and then we signal only the SSI459* to start transferring.460*461* We want End-Of-Segment Interrupts enabled, because this will generate462* an interrupt at the end of each segment (each link descriptor463* represents one segment). Each DMA segment is the same thing as an464* ALSA period, so this is how we get an interrupt at the end of every465* period.466*467* We want Error Interrupt enabled, so that we can get an error if468* the DMA controller is mis-programmed somehow.469*/470mr |= CCSR_DMA_MR_EOSIE | CCSR_DMA_MR_EIE | CCSR_DMA_MR_EMP_EN |471CCSR_DMA_MR_EMS_EN;472473/* For playback, we want the destination address to be held. For474capture, set the source address to be held. */475mr |= (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ?476CCSR_DMA_MR_DAHE : CCSR_DMA_MR_SAHE;477478out_be32(&dma_channel->mr, mr);479480return 0;481}482483/**484* fsl_dma_hw_params: continue initializing the DMA links485*486* This function obtains hardware parameters about the opened stream and487* programs the DMA controller accordingly.488*489* One drawback of big-endian is that when copying integers of different490* sizes to a fixed-sized register, the address to which the integer must be491* copied is dependent on the size of the integer.492*493* For example, if P is the address of a 32-bit register, and X is a 32-bit494* integer, then X should be copied to address P. However, if X is a 16-bit495* integer, then it should be copied to P+2. If X is an 8-bit register,496* then it should be copied to P+3.497*498* So for playback of 8-bit samples, the DMA controller must transfer single499* bytes from the DMA buffer to the last byte of the STX0 register, i.e.500* offset by 3 bytes. For 16-bit samples, the offset is two bytes.501*502* For 24-bit samples, the offset is 1 byte. However, the DMA controller503* does not support 3-byte copies (the DAHTS register supports only 1, 2, 4,504* and 8 bytes at a time). So we do not support packed 24-bit samples.505* 24-bit data must be padded to 32 bits.506*/507static int fsl_dma_hw_params(struct snd_soc_component *component,508struct snd_pcm_substream *substream,509struct snd_pcm_hw_params *hw_params)510{511struct snd_pcm_runtime *runtime = substream->runtime;512struct fsl_dma_private *dma_private = runtime->private_data;513struct device *dev = component->dev;514515/* Number of bits per sample */516unsigned int sample_bits =517snd_pcm_format_physical_width(params_format(hw_params));518519/* Number of bytes per frame */520unsigned int sample_bytes = sample_bits / 8;521522/* Bus address of SSI STX register */523dma_addr_t ssi_sxx_phys = dma_private->ssi_sxx_phys;524525/* Size of the DMA buffer, in bytes */526size_t buffer_size = params_buffer_bytes(hw_params);527528/* Number of bytes per period */529size_t period_size = params_period_bytes(hw_params);530531/* Pointer to next period */532dma_addr_t temp_addr = substream->dma_buffer.addr;533534/* Pointer to DMA controller */535struct ccsr_dma_channel __iomem *dma_channel = dma_private->dma_channel;536537u32 mr; /* DMA Mode Register */538539unsigned int i;540541/* Initialize our DMA tracking variables */542dma_private->period_size = period_size;543dma_private->num_periods = params_periods(hw_params);544dma_private->dma_buf_end = dma_private->dma_buf_phys + buffer_size;545dma_private->dma_buf_next = dma_private->dma_buf_phys +546(NUM_DMA_LINKS * period_size);547548if (dma_private->dma_buf_next >= dma_private->dma_buf_end)549/* This happens if the number of periods == NUM_DMA_LINKS */550dma_private->dma_buf_next = dma_private->dma_buf_phys;551552mr = in_be32(&dma_channel->mr) & ~(CCSR_DMA_MR_BWC_MASK |553CCSR_DMA_MR_SAHTS_MASK | CCSR_DMA_MR_DAHTS_MASK);554555/* Due to a quirk of the SSI's STX register, the target address556* for the DMA operations depends on the sample size. So we calculate557* that offset here. While we're at it, also tell the DMA controller558* how much data to transfer per sample.559*/560switch (sample_bits) {561case 8:562mr |= CCSR_DMA_MR_DAHTS_1 | CCSR_DMA_MR_SAHTS_1;563ssi_sxx_phys += 3;564break;565case 16:566mr |= CCSR_DMA_MR_DAHTS_2 | CCSR_DMA_MR_SAHTS_2;567ssi_sxx_phys += 2;568break;569case 32:570mr |= CCSR_DMA_MR_DAHTS_4 | CCSR_DMA_MR_SAHTS_4;571break;572default:573/* We should never get here */574dev_err(dev, "unsupported sample size %u\n", sample_bits);575return -EINVAL;576}577578/*579* BWC determines how many bytes are sent/received before the DMA580* controller checks the SSI to see if it needs to stop. BWC should581* always be a multiple of the frame size, so that we always transmit582* whole frames. Each frame occupies two slots in the FIFO. The583* parameter for CCSR_DMA_MR_BWC() is rounded down the next power of two584* (MR[BWC] can only represent even powers of two).585*586* To simplify the process, we set BWC to the largest value that is587* less than or equal to the FIFO watermark. For playback, this ensures588* that we transfer the maximum amount without overrunning the FIFO.589* For capture, this ensures that we transfer the maximum amount without590* underrunning the FIFO.591*592* f = SSI FIFO depth593* w = SSI watermark value (which equals f - 2)594* b = DMA bandwidth count (in bytes)595* s = sample size (in bytes, which equals frame_size * 2)596*597* For playback, we never transmit more than the transmit FIFO598* watermark, otherwise we might write more data than the FIFO can hold.599* The watermark is equal to the FIFO depth minus two.600*601* For capture, two equations must hold:602* w > f - (b / s)603* w >= b / s604*605* So, b > 2 * s, but b must also be <= s * w. To simplify, we set606* b = s * w, which is equal to607* (dma_private->ssi_fifo_depth - 2) * sample_bytes.608*/609mr |= CCSR_DMA_MR_BWC((dma_private->ssi_fifo_depth - 2) * sample_bytes);610611out_be32(&dma_channel->mr, mr);612613for (i = 0; i < NUM_DMA_LINKS; i++) {614struct fsl_dma_link_descriptor *link = &dma_private->link[i];615616link->count = cpu_to_be32(period_size);617618/* The snoop bit tells the DMA controller whether it should tell619* the ECM to snoop during a read or write to an address. For620* audio, we use DMA to transfer data between memory and an I/O621* device (the SSI's STX0 or SRX0 register). Snooping is only622* needed if there is a cache, so we need to snoop memory623* addresses only. For playback, that means we snoop the source624* but not the destination. For capture, we snoop the625* destination but not the source.626*627* Note that failing to snoop properly is unlikely to cause628* cache incoherency if the period size is larger than the629* size of L1 cache. This is because filling in one period will630* flush out the data for the previous period. So if you631* increased period_bytes_min to a large enough size, you might632* get more performance by not snooping, and you'll still be633* okay. You'll need to update fsl_dma_update_pointers() also.634*/635if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {636link->source_addr = cpu_to_be32(temp_addr);637link->source_attr = cpu_to_be32(CCSR_DMA_ATR_SNOOP |638upper_32_bits(temp_addr));639640link->dest_addr = cpu_to_be32(ssi_sxx_phys);641link->dest_attr = cpu_to_be32(CCSR_DMA_ATR_NOSNOOP |642upper_32_bits(ssi_sxx_phys));643} else {644link->source_addr = cpu_to_be32(ssi_sxx_phys);645link->source_attr = cpu_to_be32(CCSR_DMA_ATR_NOSNOOP |646upper_32_bits(ssi_sxx_phys));647648link->dest_addr = cpu_to_be32(temp_addr);649link->dest_attr = cpu_to_be32(CCSR_DMA_ATR_SNOOP |650upper_32_bits(temp_addr));651}652653temp_addr += period_size;654}655656return 0;657}658659/**660* fsl_dma_pointer: determine the current position of the DMA transfer661*662* This function is called by ALSA when ALSA wants to know where in the663* stream buffer the hardware currently is.664*665* For playback, the SAR register contains the physical address of the most666* recent DMA transfer. For capture, the value is in the DAR register.667*668* The base address of the buffer is stored in the source_addr field of the669* first link descriptor.670*/671static snd_pcm_uframes_t fsl_dma_pointer(struct snd_soc_component *component,672struct snd_pcm_substream *substream)673{674struct snd_pcm_runtime *runtime = substream->runtime;675struct fsl_dma_private *dma_private = runtime->private_data;676struct device *dev = component->dev;677struct ccsr_dma_channel __iomem *dma_channel = dma_private->dma_channel;678dma_addr_t position;679snd_pcm_uframes_t frames;680681/* Obtain the current DMA pointer, but don't read the ESAD bits if we682* only have 32-bit DMA addresses. This function is typically called683* in interrupt context, so we need to optimize it.684*/685if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {686position = in_be32(&dma_channel->sar);687#ifdef CONFIG_PHYS_64BIT688position |= (u64)(in_be32(&dma_channel->satr) &689CCSR_DMA_ATR_ESAD_MASK) << 32;690#endif691} else {692position = in_be32(&dma_channel->dar);693#ifdef CONFIG_PHYS_64BIT694position |= (u64)(in_be32(&dma_channel->datr) &695CCSR_DMA_ATR_ESAD_MASK) << 32;696#endif697}698699/*700* When capture is started, the SSI immediately starts to fill its FIFO.701* This means that the DMA controller is not started until the FIFO is702* full. However, ALSA calls this function before that happens, when703* MR.DAR is still zero. In this case, just return zero to indicate704* that nothing has been received yet.705*/706if (!position)707return 0;708709if ((position < dma_private->dma_buf_phys) ||710(position > dma_private->dma_buf_end)) {711dev_err(dev, "dma pointer is out of range, halting stream\n");712return SNDRV_PCM_POS_XRUN;713}714715frames = bytes_to_frames(runtime, position - dma_private->dma_buf_phys);716717/*718* If the current address is just past the end of the buffer, wrap it719* around.720*/721if (frames == runtime->buffer_size)722frames = 0;723724return frames;725}726727/**728* fsl_dma_hw_free: release resources allocated in fsl_dma_hw_params()729*730* Release the resources allocated in fsl_dma_hw_params() and de-program the731* registers.732*733* This function can be called multiple times.734*/735static int fsl_dma_hw_free(struct snd_soc_component *component,736struct snd_pcm_substream *substream)737{738struct snd_pcm_runtime *runtime = substream->runtime;739struct fsl_dma_private *dma_private = runtime->private_data;740741if (dma_private) {742struct ccsr_dma_channel __iomem *dma_channel;743744dma_channel = dma_private->dma_channel;745746/* Stop the DMA */747out_be32(&dma_channel->mr, CCSR_DMA_MR_CA);748out_be32(&dma_channel->mr, 0);749750/* Reset all the other registers */751out_be32(&dma_channel->sr, -1);752out_be32(&dma_channel->clndar, 0);753out_be32(&dma_channel->eclndar, 0);754out_be32(&dma_channel->satr, 0);755out_be32(&dma_channel->sar, 0);756out_be32(&dma_channel->datr, 0);757out_be32(&dma_channel->dar, 0);758out_be32(&dma_channel->bcr, 0);759out_be32(&dma_channel->nlndar, 0);760out_be32(&dma_channel->enlndar, 0);761}762763return 0;764}765766/**767* fsl_dma_close: close the stream.768*/769static int fsl_dma_close(struct snd_soc_component *component,770struct snd_pcm_substream *substream)771{772struct snd_pcm_runtime *runtime = substream->runtime;773struct fsl_dma_private *dma_private = runtime->private_data;774struct device *dev = component->dev;775struct dma_object *dma =776container_of(component->driver, struct dma_object, dai);777778if (dma_private) {779if (dma_private->irq)780free_irq(dma_private->irq, dma_private);781782/* Deallocate the fsl_dma_private structure */783dma_free_coherent(dev, sizeof(struct fsl_dma_private),784dma_private, dma_private->ld_buf_phys);785substream->runtime->private_data = NULL;786}787788dma->assigned = false;789790return 0;791}792793/**794* find_ssi_node -- returns the SSI node that points to its DMA channel node795*796* Although this DMA driver attempts to operate independently of the other797* devices, it still needs to determine some information about the SSI device798* that it's working with. Unfortunately, the device tree does not contain799* a pointer from the DMA channel node to the SSI node -- the pointer goes the800* other way. So we need to scan the device tree for SSI nodes until we find801* the one that points to the given DMA channel node. It's ugly, but at least802* it's contained in this one function.803*/804static struct device_node *find_ssi_node(struct device_node *dma_channel_np)805{806struct device_node *ssi_np, *np;807808for_each_compatible_node(ssi_np, NULL, "fsl,mpc8610-ssi") {809/* Check each DMA phandle to see if it points to us. We810* assume that device_node pointers are a valid comparison.811*/812np = of_parse_phandle(ssi_np, "fsl,playback-dma", 0);813of_node_put(np);814if (np == dma_channel_np)815return ssi_np;816817np = of_parse_phandle(ssi_np, "fsl,capture-dma", 0);818of_node_put(np);819if (np == dma_channel_np)820return ssi_np;821}822823return NULL;824}825826static int fsl_soc_dma_probe(struct platform_device *pdev)827{828struct dma_object *dma;829struct device_node *np = pdev->dev.of_node;830struct device_node *ssi_np;831struct resource res;832const uint32_t *iprop;833int ret;834835/* Find the SSI node that points to us. */836ssi_np = find_ssi_node(np);837if (!ssi_np) {838dev_err(&pdev->dev, "cannot find parent SSI node\n");839return -ENODEV;840}841842ret = of_address_to_resource(ssi_np, 0, &res);843if (ret) {844dev_err(&pdev->dev, "could not determine resources for %pOF\n",845ssi_np);846of_node_put(ssi_np);847return ret;848}849850dma = kzalloc(sizeof(*dma), GFP_KERNEL);851if (!dma) {852of_node_put(ssi_np);853return -ENOMEM;854}855856dma->dai.name = DRV_NAME;857dma->dai.open = fsl_dma_open;858dma->dai.close = fsl_dma_close;859dma->dai.hw_params = fsl_dma_hw_params;860dma->dai.hw_free = fsl_dma_hw_free;861dma->dai.pointer = fsl_dma_pointer;862dma->dai.pcm_construct = fsl_dma_new;863864/* Store the SSI-specific information that we need */865dma->ssi_stx_phys = res.start + REG_SSI_STX0;866dma->ssi_srx_phys = res.start + REG_SSI_SRX0;867868iprop = of_get_property(ssi_np, "fsl,fifo-depth", NULL);869if (iprop)870dma->ssi_fifo_depth = be32_to_cpup(iprop);871else872/* Older 8610 DTs didn't have the fifo-depth property */873dma->ssi_fifo_depth = 8;874875of_node_put(ssi_np);876877ret = devm_snd_soc_register_component(&pdev->dev, &dma->dai, NULL, 0);878if (ret) {879dev_err(&pdev->dev, "could not register platform\n");880kfree(dma);881return ret;882}883884dma->channel = of_iomap(np, 0);885dma->irq = irq_of_parse_and_map(np, 0);886887dev_set_drvdata(&pdev->dev, dma);888889return 0;890}891892static void fsl_soc_dma_remove(struct platform_device *pdev)893{894struct dma_object *dma = dev_get_drvdata(&pdev->dev);895896iounmap(dma->channel);897irq_dispose_mapping(dma->irq);898kfree(dma);899}900901static const struct of_device_id fsl_soc_dma_ids[] = {902{ .compatible = "fsl,ssi-dma-channel", },903{}904};905MODULE_DEVICE_TABLE(of, fsl_soc_dma_ids);906907static struct platform_driver fsl_soc_dma_driver = {908.driver = {909.name = "fsl-pcm-audio",910.of_match_table = fsl_soc_dma_ids,911},912.probe = fsl_soc_dma_probe,913.remove = fsl_soc_dma_remove,914};915916module_platform_driver(fsl_soc_dma_driver);917918MODULE_AUTHOR("Timur Tabi <[email protected]>");919MODULE_DESCRIPTION("Freescale Elo DMA ASoC PCM Driver");920MODULE_LICENSE("GPL v2");921922923