1 // SPDX-License-Identifier: GPL-2.0-or-later << 2 /* 1 /* 3 * kirkwood-dma.c 2 * kirkwood-dma.c 4 * 3 * 5 * (c) 2010 Arnaud Patard <apatard@mandriva.co 4 * (c) 2010 Arnaud Patard <apatard@mandriva.com> 6 * (c) 2010 Arnaud Patard <arnaud.patard@rtp-n 5 * (c) 2010 Arnaud Patard <arnaud.patard@rtp-net.org> >> 6 * >> 7 * This program is free software; you can redistribute it and/or modify it >> 8 * under the terms of the GNU General Public License as published by the >> 9 * Free Software Foundation; either version 2 of the License, or (at your >> 10 * option) any later version. 7 */ 11 */ 8 12 9 #include <linux/init.h> 13 #include <linux/init.h> 10 #include <linux/module.h> 14 #include <linux/module.h> 11 #include <linux/device.h> 15 #include <linux/device.h> 12 #include <linux/io.h> 16 #include <linux/io.h> 13 #include <linux/slab.h> 17 #include <linux/slab.h> 14 #include <linux/interrupt.h> 18 #include <linux/interrupt.h> 15 #include <linux/dma-mapping.h> 19 #include <linux/dma-mapping.h> 16 #include <linux/mbus.h> 20 #include <linux/mbus.h> 17 #include <sound/soc.h> 21 #include <sound/soc.h> 18 #include "kirkwood.h" 22 #include "kirkwood.h" 19 23 20 static struct kirkwood_dma_data *kirkwood_priv 24 static struct kirkwood_dma_data *kirkwood_priv(struct snd_pcm_substream *subs) 21 { 25 { 22 struct snd_soc_pcm_runtime *soc_runtim !! 26 struct snd_soc_pcm_runtime *soc_runtime = subs->private_data; 23 return snd_soc_dai_get_drvdata(snd_soc !! 27 return snd_soc_dai_get_drvdata(soc_runtime->cpu_dai); 24 } 28 } 25 29 26 static const struct snd_pcm_hardware kirkwood_ 30 static const struct snd_pcm_hardware kirkwood_dma_snd_hw = { 27 .info = SNDRV_PCM_INFO_INTERLEAVED | 31 .info = SNDRV_PCM_INFO_INTERLEAVED | 28 SNDRV_PCM_INFO_MMAP | 32 SNDRV_PCM_INFO_MMAP | 29 SNDRV_PCM_INFO_MMAP_VALID | 33 SNDRV_PCM_INFO_MMAP_VALID | 30 SNDRV_PCM_INFO_BLOCK_TRANSFER 34 SNDRV_PCM_INFO_BLOCK_TRANSFER | 31 SNDRV_PCM_INFO_PAUSE | 35 SNDRV_PCM_INFO_PAUSE | 32 SNDRV_PCM_INFO_NO_PERIOD_WAKEU 36 SNDRV_PCM_INFO_NO_PERIOD_WAKEUP, 33 .buffer_bytes_max = KIRKWOOD_SND 37 .buffer_bytes_max = KIRKWOOD_SND_MAX_BUFFER_BYTES, 34 .period_bytes_min = KIRKWOOD_SND 38 .period_bytes_min = KIRKWOOD_SND_MIN_PERIOD_BYTES, 35 .period_bytes_max = KIRKWOOD_SND 39 .period_bytes_max = KIRKWOOD_SND_MAX_PERIOD_BYTES, 36 .periods_min = KIRKWOOD_SND 40 .periods_min = KIRKWOOD_SND_MIN_PERIODS, 37 .periods_max = KIRKWOOD_SND 41 .periods_max = KIRKWOOD_SND_MAX_PERIODS, 38 .fifo_size = 0, 42 .fifo_size = 0, 39 }; 43 }; 40 44 41 static irqreturn_t kirkwood_dma_irq(int irq, v 45 static irqreturn_t kirkwood_dma_irq(int irq, void *dev_id) 42 { 46 { 43 struct kirkwood_dma_data *priv = dev_i 47 struct kirkwood_dma_data *priv = dev_id; 44 unsigned long mask, status, cause; 48 unsigned long mask, status, cause; 45 49 46 mask = readl(priv->io + KIRKWOOD_INT_M 50 mask = readl(priv->io + KIRKWOOD_INT_MASK); 47 status = readl(priv->io + KIRKWOOD_INT 51 status = readl(priv->io + KIRKWOOD_INT_CAUSE) & mask; 48 52 49 cause = readl(priv->io + KIRKWOOD_ERR_ 53 cause = readl(priv->io + KIRKWOOD_ERR_CAUSE); 50 if (unlikely(cause)) { 54 if (unlikely(cause)) { 51 printk(KERN_WARNING "%s: got e 55 printk(KERN_WARNING "%s: got err interrupt 0x%lx\n", 52 __func__, caus 56 __func__, cause); 53 writel(cause, priv->io + KIRKW 57 writel(cause, priv->io + KIRKWOOD_ERR_CAUSE); 54 } 58 } 55 59 56 /* we've enabled only bytes interrupts 60 /* we've enabled only bytes interrupts ... */ 57 if (status & ~(KIRKWOOD_INT_CAUSE_PLAY 61 if (status & ~(KIRKWOOD_INT_CAUSE_PLAY_BYTES | \ 58 KIRKWOOD_INT_CAUSE_REC 62 KIRKWOOD_INT_CAUSE_REC_BYTES)) { 59 printk(KERN_WARNING "%s: unexp 63 printk(KERN_WARNING "%s: unexpected interrupt %lx\n", 60 __func__, status); 64 __func__, status); 61 return IRQ_NONE; 65 return IRQ_NONE; 62 } 66 } 63 67 64 /* ack int */ 68 /* ack int */ 65 writel(status, priv->io + KIRKWOOD_INT 69 writel(status, priv->io + KIRKWOOD_INT_CAUSE); 66 70 67 if (status & KIRKWOOD_INT_CAUSE_PLAY_B 71 if (status & KIRKWOOD_INT_CAUSE_PLAY_BYTES) 68 snd_pcm_period_elapsed(priv->s 72 snd_pcm_period_elapsed(priv->substream_play); 69 73 70 if (status & KIRKWOOD_INT_CAUSE_REC_BY 74 if (status & KIRKWOOD_INT_CAUSE_REC_BYTES) 71 snd_pcm_period_elapsed(priv->s 75 snd_pcm_period_elapsed(priv->substream_rec); 72 76 73 return IRQ_HANDLED; 77 return IRQ_HANDLED; 74 } 78 } 75 79 76 static void 80 static void 77 kirkwood_dma_conf_mbus_windows(void __iomem *b 81 kirkwood_dma_conf_mbus_windows(void __iomem *base, int win, 78 unsigned long d 82 unsigned long dma, 79 const struct mb 83 const struct mbus_dram_target_info *dram) 80 { 84 { 81 int i; 85 int i; 82 86 83 /* First disable and clear windows */ 87 /* First disable and clear windows */ 84 writel(0, base + KIRKWOOD_AUDIO_WIN_CT 88 writel(0, base + KIRKWOOD_AUDIO_WIN_CTRL_REG(win)); 85 writel(0, base + KIRKWOOD_AUDIO_WIN_BA 89 writel(0, base + KIRKWOOD_AUDIO_WIN_BASE_REG(win)); 86 90 87 /* try to find matching cs for current 91 /* try to find matching cs for current dma address */ 88 for (i = 0; i < dram->num_cs; i++) { 92 for (i = 0; i < dram->num_cs; i++) { 89 const struct mbus_dram_window 93 const struct mbus_dram_window *cs = &dram->cs[i]; 90 if ((cs->base & 0xffff0000) < 94 if ((cs->base & 0xffff0000) < (dma & 0xffff0000)) { 91 writel(cs->base & 0xff 95 writel(cs->base & 0xffff0000, 92 base + KIRKWOO 96 base + KIRKWOOD_AUDIO_WIN_BASE_REG(win)); 93 writel(((cs->size - 1) 97 writel(((cs->size - 1) & 0xffff0000) | 94 (cs->mbus_attr 98 (cs->mbus_attr << 8) | 95 (dram->mbus_dr 99 (dram->mbus_dram_target_id << 4) | 1, 96 base + KIRKWOO 100 base + KIRKWOOD_AUDIO_WIN_CTRL_REG(win)); 97 } 101 } 98 } 102 } 99 } 103 } 100 104 101 static int kirkwood_dma_open(struct snd_soc_co !! 105 static int kirkwood_dma_open(struct snd_pcm_substream *substream) 102 struct snd_pcm_su << 103 { 106 { 104 int err; 107 int err; 105 struct snd_pcm_runtime *runtime = subs 108 struct snd_pcm_runtime *runtime = substream->runtime; 106 struct kirkwood_dma_data *priv = kirkw 109 struct kirkwood_dma_data *priv = kirkwood_priv(substream); >> 110 const struct mbus_dram_target_info *dram; >> 111 unsigned long addr; 107 112 108 snd_soc_set_runtime_hwparams(substream 113 snd_soc_set_runtime_hwparams(substream, &kirkwood_dma_snd_hw); 109 114 110 /* Ensure that all constraints linked 115 /* Ensure that all constraints linked to dma burst are fulfilled */ 111 err = snd_pcm_hw_constraint_minmax(run 116 err = snd_pcm_hw_constraint_minmax(runtime, 112 SNDRV_PCM_HW_PARAM_BUF 117 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 113 priv->burst * 2, 118 priv->burst * 2, 114 KIRKWOOD_AUDIO_BUF_MAX 119 KIRKWOOD_AUDIO_BUF_MAX-1); 115 if (err < 0) 120 if (err < 0) 116 return err; 121 return err; 117 122 118 err = snd_pcm_hw_constraint_step(runti 123 err = snd_pcm_hw_constraint_step(runtime, 0, 119 SNDRV_PCM_HW_PARAM_BUF 124 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 120 priv->burst); 125 priv->burst); 121 if (err < 0) 126 if (err < 0) 122 return err; 127 return err; 123 128 124 err = snd_pcm_hw_constraint_step(subst 129 err = snd_pcm_hw_constraint_step(substream->runtime, 0, 125 SNDRV_PCM_HW_PARAM_PE 130 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 126 priv->burst); 131 priv->burst); 127 if (err < 0) 132 if (err < 0) 128 return err; 133 return err; 129 134 130 if (!priv->substream_play && !priv->su 135 if (!priv->substream_play && !priv->substream_rec) { 131 err = request_irq(priv->irq, k 136 err = request_irq(priv->irq, kirkwood_dma_irq, IRQF_SHARED, 132 "kirkwood-i2 137 "kirkwood-i2s", priv); 133 if (err) 138 if (err) 134 return err; 139 return err; 135 140 136 /* 141 /* 137 * Enable Error interrupts. We 142 * Enable Error interrupts. We're only ack'ing them but 138 * it's useful for diagnostics 143 * it's useful for diagnostics 139 */ 144 */ 140 writel((unsigned int)-1, priv- 145 writel((unsigned int)-1, priv->io + KIRKWOOD_ERR_MASK); 141 } 146 } 142 147 >> 148 dram = mv_mbus_dram_info(); >> 149 addr = substream->dma_buffer.addr; 143 if (substream->stream == SNDRV_PCM_STR 150 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 144 if (priv->substream_play) 151 if (priv->substream_play) 145 return -EBUSY; 152 return -EBUSY; 146 priv->substream_play = substre 153 priv->substream_play = substream; >> 154 kirkwood_dma_conf_mbus_windows(priv->io, >> 155 KIRKWOOD_PLAYBACK_WIN, addr, dram); 147 } else { 156 } else { 148 if (priv->substream_rec) 157 if (priv->substream_rec) 149 return -EBUSY; 158 return -EBUSY; 150 priv->substream_rec = substrea 159 priv->substream_rec = substream; >> 160 kirkwood_dma_conf_mbus_windows(priv->io, >> 161 KIRKWOOD_RECORD_WIN, addr, dram); 151 } 162 } 152 163 153 return 0; 164 return 0; 154 } 165 } 155 166 156 static int kirkwood_dma_close(struct snd_soc_c !! 167 static int kirkwood_dma_close(struct snd_pcm_substream *substream) 157 struct snd_pcm_s << 158 { 168 { 159 struct kirkwood_dma_data *priv = kirkw 169 struct kirkwood_dma_data *priv = kirkwood_priv(substream); 160 170 161 if (!priv) 171 if (!priv) 162 return 0; 172 return 0; 163 173 164 if (substream->stream == SNDRV_PCM_STR 174 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 165 priv->substream_play = NULL; 175 priv->substream_play = NULL; 166 else 176 else 167 priv->substream_rec = NULL; 177 priv->substream_rec = NULL; 168 178 169 if (!priv->substream_play && !priv->su 179 if (!priv->substream_play && !priv->substream_rec) { 170 writel(0, priv->io + KIRKWOOD_ 180 writel(0, priv->io + KIRKWOOD_ERR_MASK); 171 free_irq(priv->irq, priv); 181 free_irq(priv->irq, priv); 172 } 182 } 173 183 174 return 0; 184 return 0; 175 } 185 } 176 186 177 static int kirkwood_dma_hw_params(struct snd_s !! 187 static int kirkwood_dma_hw_params(struct snd_pcm_substream *substream, 178 struct snd_p !! 188 struct snd_pcm_hw_params *params) 179 struct snd_p << 180 { 189 { 181 struct kirkwood_dma_data *priv = kirkw !! 190 struct snd_pcm_runtime *runtime = substream->runtime; 182 const struct mbus_dram_target_info *dr << 183 unsigned long addr = substream->runtim << 184 191 185 if (!dram) !! 192 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer); 186 return 0; !! 193 runtime->dma_bytes = params_buffer_bytes(params); 187 194 188 if (substream->stream == SNDRV_PCM_STR << 189 kirkwood_dma_conf_mbus_windows << 190 KIRKWOOD_PLAYBACK_WIN, << 191 else << 192 kirkwood_dma_conf_mbus_windows << 193 KIRKWOOD_RECORD_WIN, a << 194 return 0; 195 return 0; 195 } 196 } 196 197 197 static int kirkwood_dma_prepare(struct snd_soc !! 198 static int kirkwood_dma_hw_free(struct snd_pcm_substream *substream) 198 struct snd_pcm !! 199 { >> 200 snd_pcm_set_runtime_buffer(substream, NULL); >> 201 return 0; >> 202 } >> 203 >> 204 static int kirkwood_dma_prepare(struct snd_pcm_substream *substream) 199 { 205 { 200 struct snd_pcm_runtime *runtime = subs 206 struct snd_pcm_runtime *runtime = substream->runtime; 201 struct kirkwood_dma_data *priv = kirkw 207 struct kirkwood_dma_data *priv = kirkwood_priv(substream); 202 unsigned long size, count; 208 unsigned long size, count; 203 209 204 /* compute buffer size in term of "wor 210 /* compute buffer size in term of "words" as requested in specs */ 205 size = frames_to_bytes(runtime, runtim 211 size = frames_to_bytes(runtime, runtime->buffer_size); 206 size = (size>>2)-1; 212 size = (size>>2)-1; 207 count = snd_pcm_lib_period_bytes(subst 213 count = snd_pcm_lib_period_bytes(substream); 208 214 209 if (substream->stream == SNDRV_PCM_STR 215 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 210 writel(count, priv->io + KIRKW 216 writel(count, priv->io + KIRKWOOD_PLAY_BYTE_INT_COUNT); 211 writel(runtime->dma_addr, priv 217 writel(runtime->dma_addr, priv->io + KIRKWOOD_PLAY_BUF_ADDR); 212 writel(size, priv->io + KIRKWO 218 writel(size, priv->io + KIRKWOOD_PLAY_BUF_SIZE); 213 } else { 219 } else { 214 writel(count, priv->io + KIRKW 220 writel(count, priv->io + KIRKWOOD_REC_BYTE_INT_COUNT); 215 writel(runtime->dma_addr, priv 221 writel(runtime->dma_addr, priv->io + KIRKWOOD_REC_BUF_ADDR); 216 writel(size, priv->io + KIRKWO 222 writel(size, priv->io + KIRKWOOD_REC_BUF_SIZE); 217 } 223 } 218 224 219 225 220 return 0; 226 return 0; 221 } 227 } 222 228 223 static snd_pcm_uframes_t kirkwood_dma_pointer( !! 229 static snd_pcm_uframes_t kirkwood_dma_pointer(struct snd_pcm_substream 224 struct snd_soc_component *component, !! 230 *substream) 225 struct snd_pcm_substream *substream) << 226 { 231 { 227 struct kirkwood_dma_data *priv = kirkw 232 struct kirkwood_dma_data *priv = kirkwood_priv(substream); 228 snd_pcm_uframes_t count; 233 snd_pcm_uframes_t count; 229 234 230 if (substream->stream == SNDRV_PCM_STR 235 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 231 count = bytes_to_frames(substr 236 count = bytes_to_frames(substream->runtime, 232 readl(priv->io + KIRKW 237 readl(priv->io + KIRKWOOD_PLAY_BYTE_COUNT)); 233 else 238 else 234 count = bytes_to_frames(substr 239 count = bytes_to_frames(substream->runtime, 235 readl(priv->io + KIRKW 240 readl(priv->io + KIRKWOOD_REC_BYTE_COUNT)); 236 241 237 return count; 242 return count; 238 } 243 } 239 244 240 static int kirkwood_dma_new(struct snd_soc_com !! 245 static const struct snd_pcm_ops kirkwood_dma_ops = { 241 struct snd_soc_pcm !! 246 .open = kirkwood_dma_open, >> 247 .close = kirkwood_dma_close, >> 248 .ioctl = snd_pcm_lib_ioctl, >> 249 .hw_params = kirkwood_dma_hw_params, >> 250 .hw_free = kirkwood_dma_hw_free, >> 251 .prepare = kirkwood_dma_prepare, >> 252 .pointer = kirkwood_dma_pointer, >> 253 }; >> 254 >> 255 static int kirkwood_dma_preallocate_dma_buffer(struct snd_pcm *pcm, >> 256 int stream) 242 { 257 { >> 258 struct snd_pcm_substream *substream = pcm->streams[stream].substream; >> 259 struct snd_dma_buffer *buf = &substream->dma_buffer; 243 size_t size = kirkwood_dma_snd_hw.buff 260 size_t size = kirkwood_dma_snd_hw.buffer_bytes_max; >> 261 >> 262 buf->dev.type = SNDRV_DMA_TYPE_DEV; >> 263 buf->dev.dev = pcm->card->dev; >> 264 buf->area = dma_alloc_coherent(pcm->card->dev, size, >> 265 &buf->addr, GFP_KERNEL); >> 266 if (!buf->area) >> 267 return -ENOMEM; >> 268 buf->bytes = size; >> 269 buf->private_data = NULL; >> 270 >> 271 return 0; >> 272 } >> 273 >> 274 static int kirkwood_dma_new(struct snd_soc_pcm_runtime *rtd) >> 275 { 244 struct snd_card *card = rtd->card->snd 276 struct snd_card *card = rtd->card->snd_card; >> 277 struct snd_pcm *pcm = rtd->pcm; 245 int ret; 278 int ret; 246 279 247 ret = dma_coerce_mask_and_coherent(car 280 ret = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(32)); 248 if (ret) 281 if (ret) 249 return ret; 282 return ret; 250 283 251 snd_pcm_set_managed_buffer_all(rtd->pc !! 284 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) { 252 card->d !! 285 ret = kirkwood_dma_preallocate_dma_buffer(pcm, >> 286 SNDRV_PCM_STREAM_PLAYBACK); >> 287 if (ret) >> 288 return ret; >> 289 } >> 290 >> 291 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) { >> 292 ret = kirkwood_dma_preallocate_dma_buffer(pcm, >> 293 SNDRV_PCM_STREAM_CAPTURE); >> 294 if (ret) >> 295 return ret; >> 296 } 253 297 254 return 0; 298 return 0; 255 } 299 } 256 300 >> 301 static void kirkwood_dma_free_dma_buffers(struct snd_pcm *pcm) >> 302 { >> 303 struct snd_pcm_substream *substream; >> 304 struct snd_dma_buffer *buf; >> 305 int stream; >> 306 >> 307 for (stream = 0; stream < 2; stream++) { >> 308 substream = pcm->streams[stream].substream; >> 309 if (!substream) >> 310 continue; >> 311 buf = &substream->dma_buffer; >> 312 if (!buf->area) >> 313 continue; >> 314 >> 315 dma_free_coherent(pcm->card->dev, buf->bytes, >> 316 buf->area, buf->addr); >> 317 buf->area = NULL; >> 318 } >> 319 } >> 320 257 const struct snd_soc_component_driver kirkwood 321 const struct snd_soc_component_driver kirkwood_soc_component = { 258 .name = DRV_NAME, 322 .name = DRV_NAME, 259 .open = kirkwood_dma_open, !! 323 .ops = &kirkwood_dma_ops, 260 .close = kirkwood_dma_close, !! 324 .pcm_new = kirkwood_dma_new, 261 .hw_params = kirkwood_dma_hw_para !! 325 .pcm_free = kirkwood_dma_free_dma_buffers, 262 .prepare = kirkwood_dma_prepare << 263 .pointer = kirkwood_dma_pointer << 264 .pcm_construct = kirkwood_dma_new, << 265 }; 326 }; 266 327
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.