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