]> Pileus Git - ~andy/linux/blobdiff - sound/soc/fsl/fsl_dma.c
Merge git://git.infradead.org/battery-2.6
[~andy/linux] / sound / soc / fsl / fsl_dma.c
index d09e1941b1faeddf7af9bfb8b805e110d0a100d5..4cf98c03af223cda837a25a6f0c98c8eb0071025 100644 (file)
@@ -23,6 +23,7 @@
 #include <linux/gfp.h>
 #include <linux/of_platform.h>
 #include <linux/list.h>
+#include <linux/slab.h>
 
 #include <sound/core.h>
 #include <sound/pcm.h>
                          SNDRV_PCM_RATE_CONTINUOUS)
 
 struct dma_object {
-       struct list_head list;
        struct snd_soc_platform_driver dai;
        dma_addr_t ssi_stx_phys;
        dma_addr_t ssi_srx_phys;
+       unsigned int ssi_fifo_depth;
        struct ccsr_dma_channel __iomem *channel;
        unsigned int irq;
        bool assigned;
@@ -100,6 +101,7 @@ struct fsl_dma_private {
        unsigned int irq;
        struct snd_pcm_substream *substream;
        dma_addr_t ssi_sxx_phys;
+       unsigned int ssi_fifo_depth;
        dma_addr_t ld_buf_phys;
        unsigned int current_link;
        dma_addr_t dma_buf_phys;
@@ -175,13 +177,23 @@ static void fsl_dma_update_pointers(struct fsl_dma_private *dma_private)
        struct fsl_dma_link_descriptor *link =
                &dma_private->link[dma_private->current_link];
 
-       /* Update our link descriptors to point to the next period */
-       if (dma_private->substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
-               link->source_addr =
-                       cpu_to_be32(dma_private->dma_buf_next);
-       else
-               link->dest_addr =
-                       cpu_to_be32(dma_private->dma_buf_next);
+       /* Update our link descriptors to point to the next period. On a 36-bit
+        * system, we also need to update the ESAD bits.  We also set (keep) the
+        * snoop bits.  See the comments in fsl_dma_hw_params() about snooping.
+        */
+       if (dma_private->substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
+               link->source_addr = cpu_to_be32(dma_private->dma_buf_next);
+#ifdef CONFIG_PHYS_64BIT
+               link->source_attr = cpu_to_be32(CCSR_DMA_ATR_SNOOP |
+                       upper_32_bits(dma_private->dma_buf_next));
+#endif
+       } else {
+               link->dest_addr = cpu_to_be32(dma_private->dma_buf_next);
+#ifdef CONFIG_PHYS_64BIT
+               link->dest_attr = cpu_to_be32(CCSR_DMA_ATR_SNOOP |
+                       upper_32_bits(dma_private->dma_buf_next));
+#endif
+       }
 
        /* Update our variables for next time */
        dma_private->dma_buf_next += dma_private->period_size;
@@ -273,11 +285,19 @@ static irqreturn_t fsl_dma_isr(int irq, void *dev_id)
  * This function is called when the codec driver calls snd_soc_new_pcms(),
  * once for each .dai_link in the machine driver's snd_soc_card
  * structure.
+ *
+ * snd_dma_alloc_pages() is just a front-end to dma_alloc_coherent(), which
+ * (currently) always allocates the DMA buffer in lowmem, even if GFP_HIGHMEM
+ * is specified. Therefore, any DMA buffers we allocate will always be in low
+ * memory, but we support for 36-bit physical addresses anyway.
+ *
+ * Regardless of where the memory is actually allocated, since the device can
+ * technically DMA to any 36-bit address, we do need to set the DMA mask to 36.
  */
 static int fsl_dma_new(struct snd_card *card, struct snd_soc_dai *dai,
        struct snd_pcm *pcm)
 {
-       static u64 fsl_dma_dmamask = DMA_BIT_MASK(32);
+       static u64 fsl_dma_dmamask = DMA_BIT_MASK(36);
        int ret;
 
        if (!card->dev->dma_mask)
@@ -286,21 +306,29 @@ static int fsl_dma_new(struct snd_card *card, struct snd_soc_dai *dai,
        if (!card->dev->coherent_dma_mask)
                card->dev->coherent_dma_mask = fsl_dma_dmamask;
 
-       ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, card->dev,
-               fsl_dma_hardware.buffer_bytes_max,
-               &pcm->streams[0].substream->dma_buffer);
-       if (ret) {
-               dev_err(card->dev, "can't allocate playback dma buffer\n");
-               return ret;
+       /* Some codecs have separate DAIs for playback and capture, so we
+        * should allocate a DMA buffer only for the streams that are valid.
+        */
+
+       if (dai->driver->playback.channels_min) {
+               ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, card->dev,
+                       fsl_dma_hardware.buffer_bytes_max,
+                       &pcm->streams[0].substream->dma_buffer);
+               if (ret) {
+                       dev_err(card->dev, "can't alloc playback dma buffer\n");
+                       return ret;
+               }
        }
 
-       ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, card->dev,
-               fsl_dma_hardware.buffer_bytes_max,
-               &pcm->streams[1].substream->dma_buffer);
-       if (ret) {
-               snd_dma_free_pages(&pcm->streams[0].substream->dma_buffer);
-               dev_err(card->dev, "can't allocate capture dma buffer\n");
-               return ret;
+       if (dai->driver->capture.channels_min) {
+               ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, card->dev,
+                       fsl_dma_hardware.buffer_bytes_max,
+                       &pcm->streams[1].substream->dma_buffer);
+               if (ret) {
+                       snd_dma_free_pages(&pcm->streams[0].substream->dma_buffer);
+                       dev_err(card->dev, "can't alloc capture dma buffer\n");
+                       return ret;
+               }
        }
 
        return 0;
@@ -414,6 +442,7 @@ static int fsl_dma_open(struct snd_pcm_substream *substream)
        else
                dma_private->ssi_sxx_phys = dma->ssi_srx_phys;
 
+       dma_private->ssi_fifo_depth = dma->ssi_fifo_depth;
        dma_private->dma_channel = dma->channel;
        dma_private->irq = dma->irq;
        dma_private->substream = substream;
@@ -527,11 +556,11 @@ static int fsl_dma_hw_params(struct snd_pcm_substream *substream,
        struct device *dev = rtd->platform->dev;
 
        /* Number of bits per sample */
-       unsigned int sample_size =
+       unsigned int sample_bits =
                snd_pcm_format_physical_width(params_format(hw_params));
 
        /* Number of bytes per frame */
-       unsigned int frame_size = 2 * (sample_size / 8);
+       unsigned int sample_bytes = sample_bits / 8;
 
        /* Bus address of SSI STX register */
        dma_addr_t ssi_sxx_phys = dma_private->ssi_sxx_phys;
@@ -571,7 +600,7 @@ static int fsl_dma_hw_params(struct snd_pcm_substream *substream,
         * that offset here.  While we're at it, also tell the DMA controller
         * how much data to transfer per sample.
         */
-       switch (sample_size) {
+       switch (sample_bits) {
        case 8:
                mr |= CCSR_DMA_MR_DAHTS_1 | CCSR_DMA_MR_SAHTS_1;
                ssi_sxx_phys += 3;
@@ -585,22 +614,42 @@ static int fsl_dma_hw_params(struct snd_pcm_substream *substream,
                break;
        default:
                /* We should never get here */
-               dev_err(dev, "unsupported sample size %u\n", sample_size);
+               dev_err(dev, "unsupported sample size %u\n", sample_bits);
                return -EINVAL;
        }
 
        /*
-        * BWC should always be a multiple of the frame size.  BWC determines
-        * how many bytes are sent/received before the DMA controller checks the
-        * SSI to see if it needs to stop.  For playback, the transmit FIFO can
-        * hold three frames, so we want to send two frames at a time. For
-        * capture, the receive FIFO is triggered when it contains one frame, so
-        * we want to receive one frame at a time.
+        * BWC determines how many bytes are sent/received before the DMA
+        * controller checks the SSI to see if it needs to stop. BWC should
+        * always be a multiple of the frame size, so that we always transmit
+        * whole frames.  Each frame occupies two slots in the FIFO.  The
+        * parameter for CCSR_DMA_MR_BWC() is rounded down the next power of two
+        * (MR[BWC] can only represent even powers of two).
+        *
+        * To simplify the process, we set BWC to the largest value that is
+        * less than or equal to the FIFO watermark.  For playback, this ensures
+        * that we transfer the maximum amount without overrunning the FIFO.
+        * For capture, this ensures that we transfer the maximum amount without
+        * underrunning the FIFO.
+        *
+        * f = SSI FIFO depth
+        * w = SSI watermark value (which equals f - 2)
+        * b = DMA bandwidth count (in bytes)
+        * s = sample size (in bytes, which equals frame_size * 2)
+        *
+        * For playback, we never transmit more than the transmit FIFO
+        * watermark, otherwise we might write more data than the FIFO can hold.
+        * The watermark is equal to the FIFO depth minus two.
+        *
+        * For capture, two equations must hold:
+        *      w > f - (b / s)
+        *      w >= b / s
+        *
+        * So, b > 2 * s, but b must also be <= s * w.  To simplify, we set
+        * b = s * w, which is equal to
+        *      (dma_private->ssi_fifo_depth - 2) * sample_bytes.
         */
-       if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
-               mr |= CCSR_DMA_MR_BWC(2 * frame_size);
-       else
-               mr |= CCSR_DMA_MR_BWC(frame_size);
+       mr |= CCSR_DMA_MR_BWC((dma_private->ssi_fifo_depth - 2) * sample_bytes);
 
        out_be32(&dma_channel->mr, mr);
 
@@ -609,12 +658,7 @@ static int fsl_dma_hw_params(struct snd_pcm_substream *substream,
 
                link->count = cpu_to_be32(period_size);
 
-               /* Even though the DMA controller supports 36-bit addressing,
-                * for simplicity we allow only 32-bit addresses for the audio
-                * buffer itself.  This was enforced in fsl_dma_new() with the
-                * DMA mask.
-                *
-                * The snoop bit tells the DMA controller whether it should tell
+               /* The snoop bit tells the DMA controller whether it should tell
                 * the ECM to snoop during a read or write to an address. For
                 * audio, we use DMA to transfer data between memory and an I/O
                 * device (the SSI's STX0 or SRX0 register). Snooping is only
@@ -629,20 +673,24 @@ static int fsl_dma_hw_params(struct snd_pcm_substream *substream,
                 * flush out the data for the previous period.  So if you
                 * increased period_bytes_min to a large enough size, you might
                 * get more performance by not snooping, and you'll still be
-                * okay.
+                * okay.  You'll need to update fsl_dma_update_pointers() also.
                 */
                if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
                        link->source_addr = cpu_to_be32(temp_addr);
-                       link->source_attr = cpu_to_be32(CCSR_DMA_ATR_SNOOP);
+                       link->source_attr = cpu_to_be32(CCSR_DMA_ATR_SNOOP |
+                               upper_32_bits(temp_addr));
 
                        link->dest_addr = cpu_to_be32(ssi_sxx_phys);
-                       link->dest_attr = cpu_to_be32(CCSR_DMA_ATR_NOSNOOP);
+                       link->dest_attr = cpu_to_be32(CCSR_DMA_ATR_NOSNOOP |
+                               upper_32_bits(ssi_sxx_phys));
                } else {
                        link->source_addr = cpu_to_be32(ssi_sxx_phys);
-                       link->source_attr = cpu_to_be32(CCSR_DMA_ATR_NOSNOOP);
+                       link->source_attr = cpu_to_be32(CCSR_DMA_ATR_NOSNOOP |
+                               upper_32_bits(ssi_sxx_phys));
 
                        link->dest_addr = cpu_to_be32(temp_addr);
-                       link->dest_attr = cpu_to_be32(CCSR_DMA_ATR_SNOOP);
+                       link->dest_attr = cpu_to_be32(CCSR_DMA_ATR_SNOOP |
+                               upper_32_bits(temp_addr));
                }
 
                temp_addr += period_size;
@@ -673,10 +721,23 @@ static snd_pcm_uframes_t fsl_dma_pointer(struct snd_pcm_substream *substream)
        dma_addr_t position;
        snd_pcm_uframes_t frames;
 
-       if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
+       /* Obtain the current DMA pointer, but don't read the ESAD bits if we
+        * only have 32-bit DMA addresses.  This function is typically called
+        * in interrupt context, so we need to optimize it.
+        */
+       if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
                position = in_be32(&dma_channel->sar);
-       else
+#ifdef CONFIG_PHYS_64BIT
+               position |= (u64)(in_be32(&dma_channel->satr) &
+                                 CCSR_DMA_ATR_ESAD_MASK) << 32;
+#endif
+       } else {
                position = in_be32(&dma_channel->dar);
+#ifdef CONFIG_PHYS_64BIT
+               position |= (u64)(in_be32(&dma_channel->datr) &
+                                 CCSR_DMA_ATR_ESAD_MASK) << 32;
+#endif
+       }
 
        /*
         * When capture is started, the SSI immediately starts to fill its FIFO.
@@ -795,9 +856,6 @@ static void fsl_dma_free_dma_buffers(struct snd_pcm *pcm)
        }
 }
 
-/* List of DMA nodes that we've probed */
-static LIST_HEAD(dma_list);
-
 /**
  * find_ssi_node -- returns the SSI node that points to his DMA channel node
  *
@@ -838,32 +896,35 @@ static struct snd_pcm_ops fsl_dma_ops = {
        .pointer        = fsl_dma_pointer,
 };
 
-static int __devinit fsl_soc_dma_probe(struct of_device *of_dev,
+static int __devinit fsl_soc_dma_probe(struct platform_device *pdev,
                                       const struct of_device_id *match)
  {
        struct dma_object *dma;
-       struct device_node *np = of_dev->dev.of_node;
+       struct device_node *np = pdev->dev.of_node;
        struct device_node *ssi_np;
        struct resource res;
+       const uint32_t *iprop;
        int ret;
 
        /* Find the SSI node that points to us. */
        ssi_np = find_ssi_node(np);
        if (!ssi_np) {
-               dev_err(&of_dev->dev, "cannot find parent SSI node\n");
+               dev_err(&pdev->dev, "cannot find parent SSI node\n");
                return -ENODEV;
        }
 
        ret = of_address_to_resource(ssi_np, 0, &res);
-       of_node_put(ssi_np);
        if (ret) {
-               dev_err(&of_dev->dev, "could not determine device resources\n");
+               dev_err(&pdev->dev, "could not determine resources for %s\n",
+                       ssi_np->full_name);
+               of_node_put(ssi_np);
                return ret;
        }
 
        dma = kzalloc(sizeof(*dma) + strlen(np->full_name), GFP_KERNEL);
        if (!dma) {
-               dev_err(&of_dev->dev, "could not allocate dma object\n");
+               dev_err(&pdev->dev, "could not allocate dma object\n");
+               of_node_put(ssi_np);
                return -ENOMEM;
        }
 
@@ -876,34 +937,38 @@ static int __devinit fsl_soc_dma_probe(struct of_device *of_dev,
        dma->ssi_stx_phys = res.start + offsetof(struct ccsr_ssi, stx0);
        dma->ssi_srx_phys = res.start + offsetof(struct ccsr_ssi, srx0);
 
-       ret = snd_soc_register_platform(&of_dev->dev, &dma->dai);
+       iprop = of_get_property(ssi_np, "fsl,fifo-depth", NULL);
+       if (iprop)
+               dma->ssi_fifo_depth = *iprop;
+       else
+                /* Older 8610 DTs didn't have the fifo-depth property */
+               dma->ssi_fifo_depth = 8;
+
+       of_node_put(ssi_np);
+
+       ret = snd_soc_register_platform(&pdev->dev, &dma->dai);
        if (ret) {
-               dev_err(&of_dev->dev, "could not register platform\n");
+               dev_err(&pdev->dev, "could not register platform\n");
                kfree(dma);
                return ret;
        }
 
        dma->channel = of_iomap(np, 0);
        dma->irq = irq_of_parse_and_map(np, 0);
-       list_add(&dma->list, &dma_list);
+
+       dev_set_drvdata(&pdev->dev, dma);
 
        return 0;
 }
 
-static int __devexit fsl_soc_dma_remove(struct of_device *of_dev)
+static int __devexit fsl_soc_dma_remove(struct platform_device *pdev)
 {
-       struct list_head *n, *ptr;
-       struct dma_object *dma;
+       struct dma_object *dma = dev_get_drvdata(&pdev->dev);
 
-       list_for_each_safe(ptr, n, &dma_list) {
-               dma = list_entry(ptr, struct dma_object, list);
-               list_del_init(ptr);
-
-               snd_soc_unregister_platform(&of_dev->dev);
-               iounmap(dma->channel);
-               irq_dispose_mapping(dma->irq);
-               kfree(dma);
-       }
+       snd_soc_unregister_platform(&pdev->dev);
+       iounmap(dma->channel);
+       irq_dispose_mapping(dma->irq);
+       kfree(dma);
 
        return 0;
 }
@@ -936,11 +1001,6 @@ static void __exit fsl_soc_dma_exit(void)
        of_unregister_platform_driver(&fsl_soc_dma_driver);
 }
 
-/* We want the DMA driver to be initialized before the SSI driver, so that
- * when the SSI driver calls fsl_soc_dma_dai_from_node(), the DMA driver
- * will already have been probed.  The easiest way to do that is to make the
- * __init function called via arch_initcall().
- */
 module_init(fsl_soc_dma_init);
 module_exit(fsl_soc_dma_exit);