1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Routines for GF1 DMA control
4 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
8 #include <linux/slab.h>
9 #include <sound/core.h>
10 #include <sound/gus.h>
12 static void snd_gf1_dma_ack(struct snd_gus_card * gus)
16 spin_lock_irqsave(&gus->reg_lock, flags);
17 snd_gf1_write8(gus, SNDRV_GF1_GB_DRAM_DMA_CONTROL, 0x00);
18 snd_gf1_look8(gus, SNDRV_GF1_GB_DRAM_DMA_CONTROL);
19 spin_unlock_irqrestore(&gus->reg_lock, flags);
22 static void snd_gf1_dma_program(struct snd_gus_card * gus,
24 unsigned long buf_addr,
30 unsigned char dma_cmd;
31 unsigned int address_high;
33 snd_printdd("dma_transfer: addr=0x%x, buf=0x%lx, count=0x%x\n",
34 addr, buf_addr, count);
36 if (gus->gf1.dma1 > 3) {
37 if (gus->gf1.enh_mode) {
41 snd_printd("snd_gf1_dma_transfer: unaligned address (0x%x)?\n", addr);
44 address = (addr & 0x000c0000) | ((addr & 0x0003ffff) >> 1);
50 dma_cmd = SNDRV_GF1_DMA_ENABLE | (unsigned short) cmd;
54 if (dma_cmd & SNDRV_GF1_DMA_16BIT) {
56 count &= ~1; /* align */
58 if (gus->gf1.dma1 > 3) {
59 dma_cmd |= SNDRV_GF1_DMA_WIDTH16;
61 count &= ~1; /* align */
64 snd_dma_program(gus->gf1.dma1, buf_addr, count, dma_cmd & SNDRV_GF1_DMA_READ ? DMA_MODE_READ : DMA_MODE_WRITE);
66 snd_printk(KERN_DEBUG "address = 0x%x, count = 0x%x, dma_cmd = 0x%x\n",
67 address << 1, count, dma_cmd);
69 spin_lock_irqsave(&gus->reg_lock, flags);
70 if (gus->gf1.enh_mode) {
71 address_high = ((address >> 16) & 0x000000f0) | (address & 0x0000000f);
72 snd_gf1_write16(gus, SNDRV_GF1_GW_DRAM_DMA_LOW, (unsigned short) (address >> 4));
73 snd_gf1_write8(gus, SNDRV_GF1_GB_DRAM_DMA_HIGH, (unsigned char) address_high);
75 snd_gf1_write16(gus, SNDRV_GF1_GW_DRAM_DMA_LOW, (unsigned short) (address >> 4));
76 snd_gf1_write8(gus, SNDRV_GF1_GB_DRAM_DMA_CONTROL, dma_cmd);
77 spin_unlock_irqrestore(&gus->reg_lock, flags);
80 static struct snd_gf1_dma_block *snd_gf1_dma_next_block(struct snd_gus_card * gus)
82 struct snd_gf1_dma_block *block;
84 /* PCM block have bigger priority than synthesizer one */
85 if (gus->gf1.dma_data_pcm) {
86 block = gus->gf1.dma_data_pcm;
87 if (gus->gf1.dma_data_pcm_last == block) {
88 gus->gf1.dma_data_pcm =
89 gus->gf1.dma_data_pcm_last = NULL;
91 gus->gf1.dma_data_pcm = block->next;
93 } else if (gus->gf1.dma_data_synth) {
94 block = gus->gf1.dma_data_synth;
95 if (gus->gf1.dma_data_synth_last == block) {
96 gus->gf1.dma_data_synth =
97 gus->gf1.dma_data_synth_last = NULL;
99 gus->gf1.dma_data_synth = block->next;
105 gus->gf1.dma_ack = block->ack;
106 gus->gf1.dma_private_data = block->private_data;
112 static void snd_gf1_dma_interrupt(struct snd_gus_card * gus)
114 struct snd_gf1_dma_block *block;
116 snd_gf1_dma_ack(gus);
117 if (gus->gf1.dma_ack)
118 gus->gf1.dma_ack(gus, gus->gf1.dma_private_data);
119 spin_lock(&gus->dma_lock);
120 if (gus->gf1.dma_data_pcm == NULL &&
121 gus->gf1.dma_data_synth == NULL) {
122 gus->gf1.dma_ack = NULL;
123 gus->gf1.dma_flags &= ~SNDRV_GF1_DMA_TRIGGER;
124 spin_unlock(&gus->dma_lock);
127 block = snd_gf1_dma_next_block(gus);
128 spin_unlock(&gus->dma_lock);
129 snd_gf1_dma_program(gus, block->addr, block->buf_addr, block->count, (unsigned short) block->cmd);
132 snd_printd(KERN_DEBUG "program dma (IRQ) - "
133 "addr = 0x%x, buffer = 0x%lx, count = 0x%x, cmd = 0x%x\n",
134 block->addr, block->buf_addr, block->count, block->cmd);
138 int snd_gf1_dma_init(struct snd_gus_card * gus)
140 mutex_lock(&gus->dma_mutex);
141 gus->gf1.dma_shared++;
142 if (gus->gf1.dma_shared > 1) {
143 mutex_unlock(&gus->dma_mutex);
146 gus->gf1.interrupt_handler_dma_write = snd_gf1_dma_interrupt;
147 gus->gf1.dma_data_pcm =
148 gus->gf1.dma_data_pcm_last =
149 gus->gf1.dma_data_synth =
150 gus->gf1.dma_data_synth_last = NULL;
151 mutex_unlock(&gus->dma_mutex);
155 int snd_gf1_dma_done(struct snd_gus_card * gus)
157 struct snd_gf1_dma_block *block;
159 mutex_lock(&gus->dma_mutex);
160 gus->gf1.dma_shared--;
161 if (!gus->gf1.dma_shared) {
162 snd_dma_disable(gus->gf1.dma1);
163 snd_gf1_set_default_handlers(gus, SNDRV_GF1_HANDLER_DMA_WRITE);
164 snd_gf1_dma_ack(gus);
165 while ((block = gus->gf1.dma_data_pcm)) {
166 gus->gf1.dma_data_pcm = block->next;
169 while ((block = gus->gf1.dma_data_synth)) {
170 gus->gf1.dma_data_synth = block->next;
173 gus->gf1.dma_data_pcm_last =
174 gus->gf1.dma_data_synth_last = NULL;
176 mutex_unlock(&gus->dma_mutex);
180 int snd_gf1_dma_transfer_block(struct snd_gus_card * gus,
181 struct snd_gf1_dma_block * __block,
186 struct snd_gf1_dma_block *block;
188 block = kmalloc(sizeof(*block), atomic ? GFP_ATOMIC : GFP_KERNEL);
195 snd_printdd("addr = 0x%x, buffer = 0x%lx, count = 0x%x, cmd = 0x%x\n",
196 block->addr, (long) block->buffer, block->count,
199 snd_printdd("gus->gf1.dma_data_pcm_last = 0x%lx\n",
200 (long)gus->gf1.dma_data_pcm_last);
201 snd_printdd("gus->gf1.dma_data_pcm = 0x%lx\n",
202 (long)gus->gf1.dma_data_pcm);
204 spin_lock_irqsave(&gus->dma_lock, flags);
206 if (gus->gf1.dma_data_synth_last) {
207 gus->gf1.dma_data_synth_last->next = block;
208 gus->gf1.dma_data_synth_last = block;
210 gus->gf1.dma_data_synth =
211 gus->gf1.dma_data_synth_last = block;
214 if (gus->gf1.dma_data_pcm_last) {
215 gus->gf1.dma_data_pcm_last->next = block;
216 gus->gf1.dma_data_pcm_last = block;
218 gus->gf1.dma_data_pcm =
219 gus->gf1.dma_data_pcm_last = block;
222 if (!(gus->gf1.dma_flags & SNDRV_GF1_DMA_TRIGGER)) {
223 gus->gf1.dma_flags |= SNDRV_GF1_DMA_TRIGGER;
224 block = snd_gf1_dma_next_block(gus);
225 spin_unlock_irqrestore(&gus->dma_lock, flags);
228 snd_gf1_dma_program(gus, block->addr, block->buf_addr, block->count, (unsigned short) block->cmd);
232 spin_unlock_irqrestore(&gus->dma_lock, flags);