xref: /linux/sound/isa/gus/gus_reset.c (revision ca64d84e93762f4e587e040a44ad9f6089afc777)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
4  */
5 
6 #include <linux/delay.h>
7 #include <linux/interrupt.h>
8 #include <linux/time.h>
9 #include <sound/core.h>
10 #include <sound/gus.h>
11 
12 extern void snd_gf1_timers_init(struct snd_gus_card * gus);
13 extern void snd_gf1_timers_done(struct snd_gus_card * gus);
14 extern int snd_gf1_synth_init(struct snd_gus_card * gus);
15 extern void snd_gf1_synth_done(struct snd_gus_card * gus);
16 
17 /*
18  *  ok.. default interrupt handlers...
19  */
20 
21 static void snd_gf1_default_interrupt_handler_midi_out(struct snd_gus_card * gus)
22 {
23 	snd_gf1_uart_cmd(gus, gus->gf1.uart_cmd &= ~0x20);
24 }
25 
26 static void snd_gf1_default_interrupt_handler_midi_in(struct snd_gus_card * gus)
27 {
28 	snd_gf1_uart_cmd(gus, gus->gf1.uart_cmd &= ~0x80);
29 }
30 
31 static void snd_gf1_default_interrupt_handler_timer1(struct snd_gus_card * gus)
32 {
33 	snd_gf1_i_write8(gus, SNDRV_GF1_GB_SOUND_BLASTER_CONTROL, gus->gf1.timer_enabled &= ~4);
34 }
35 
36 static void snd_gf1_default_interrupt_handler_timer2(struct snd_gus_card * gus)
37 {
38 	snd_gf1_i_write8(gus, SNDRV_GF1_GB_SOUND_BLASTER_CONTROL, gus->gf1.timer_enabled &= ~8);
39 }
40 
41 static void snd_gf1_default_interrupt_handler_wave_and_volume(struct snd_gus_card * gus, struct snd_gus_voice * voice)
42 {
43 	snd_gf1_i_ctrl_stop(gus, 0x00);
44 	snd_gf1_i_ctrl_stop(gus, 0x0d);
45 }
46 
47 static void snd_gf1_default_interrupt_handler_dma_write(struct snd_gus_card * gus)
48 {
49 	snd_gf1_i_write8(gus, 0x41, 0x00);
50 }
51 
52 static void snd_gf1_default_interrupt_handler_dma_read(struct snd_gus_card * gus)
53 {
54 	snd_gf1_i_write8(gus, 0x49, 0x00);
55 }
56 
57 void snd_gf1_set_default_handlers(struct snd_gus_card * gus, unsigned int what)
58 {
59 	if (what & SNDRV_GF1_HANDLER_MIDI_OUT)
60 		gus->gf1.interrupt_handler_midi_out = snd_gf1_default_interrupt_handler_midi_out;
61 	if (what & SNDRV_GF1_HANDLER_MIDI_IN)
62 		gus->gf1.interrupt_handler_midi_in = snd_gf1_default_interrupt_handler_midi_in;
63 	if (what & SNDRV_GF1_HANDLER_TIMER1)
64 		gus->gf1.interrupt_handler_timer1 = snd_gf1_default_interrupt_handler_timer1;
65 	if (what & SNDRV_GF1_HANDLER_TIMER2)
66 		gus->gf1.interrupt_handler_timer2 = snd_gf1_default_interrupt_handler_timer2;
67 	if (what & SNDRV_GF1_HANDLER_VOICE) {
68 		struct snd_gus_voice *voice;
69 
70 		voice = &gus->gf1.voices[what & 0xffff];
71 		voice->handler_wave =
72 		voice->handler_volume = snd_gf1_default_interrupt_handler_wave_and_volume;
73 		voice->handler_effect = NULL;
74 		voice->volume_change = NULL;
75 	}
76 	if (what & SNDRV_GF1_HANDLER_DMA_WRITE)
77 		gus->gf1.interrupt_handler_dma_write = snd_gf1_default_interrupt_handler_dma_write;
78 	if (what & SNDRV_GF1_HANDLER_DMA_READ)
79 		gus->gf1.interrupt_handler_dma_read = snd_gf1_default_interrupt_handler_dma_read;
80 }
81 
82 /*
83 
84  */
85 
86 static void snd_gf1_clear_regs(struct snd_gus_card * gus)
87 {
88 	unsigned long flags;
89 
90 	spin_lock_irqsave(&gus->reg_lock, flags);
91 	inb(GUSP(gus, IRQSTAT));
92 	snd_gf1_write8(gus, 0x41, 0);	/* DRAM DMA Control Register */
93 	snd_gf1_write8(gus, 0x45, 0);	/* Timer Control */
94 	snd_gf1_write8(gus, 0x49, 0);	/* Sampling Control Register */
95 	spin_unlock_irqrestore(&gus->reg_lock, flags);
96 }
97 
98 static void snd_gf1_look_regs(struct snd_gus_card * gus)
99 {
100 	unsigned long flags;
101 
102 	spin_lock_irqsave(&gus->reg_lock, flags);
103 	snd_gf1_look8(gus, 0x41);	/* DRAM DMA Control Register */
104 	snd_gf1_look8(gus, 0x49);	/* Sampling Control Register */
105 	inb(GUSP(gus, IRQSTAT));
106 	snd_gf1_read8(gus, 0x0f);	/* IRQ Source Register */
107 	spin_unlock_irqrestore(&gus->reg_lock, flags);
108 }
109 
110 /*
111  *  put selected GF1 voices to initial stage...
112  */
113 
114 void snd_gf1_smart_stop_voice(struct snd_gus_card * gus, unsigned short voice)
115 {
116 	unsigned long flags;
117 
118 	spin_lock_irqsave(&gus->reg_lock, flags);
119 	snd_gf1_select_voice(gus, voice);
120 #if 0
121 	printk(KERN_DEBUG " -%i- smart stop voice - volume = 0x%x\n", voice, snd_gf1_i_read16(gus, SNDRV_GF1_VW_VOLUME));
122 #endif
123 	snd_gf1_ctrl_stop(gus, SNDRV_GF1_VB_ADDRESS_CONTROL);
124 	snd_gf1_ctrl_stop(gus, SNDRV_GF1_VB_VOLUME_CONTROL);
125 	spin_unlock_irqrestore(&gus->reg_lock, flags);
126 }
127 
128 void snd_gf1_stop_voice(struct snd_gus_card * gus, unsigned short voice)
129 {
130 	unsigned long flags;
131 
132 	spin_lock_irqsave(&gus->reg_lock, flags);
133 	snd_gf1_select_voice(gus, voice);
134 #if 0
135 	printk(KERN_DEBUG " -%i- stop voice - volume = 0x%x\n", voice, snd_gf1_i_read16(gus, SNDRV_GF1_VW_VOLUME));
136 #endif
137 	snd_gf1_ctrl_stop(gus, SNDRV_GF1_VB_ADDRESS_CONTROL);
138 	snd_gf1_ctrl_stop(gus, SNDRV_GF1_VB_VOLUME_CONTROL);
139 	if (gus->gf1.enh_mode)
140 		snd_gf1_write8(gus, SNDRV_GF1_VB_ACCUMULATOR, 0);
141 	spin_unlock_irqrestore(&gus->reg_lock, flags);
142 #if 0
143 	snd_gf1_lfo_shutdown(gus, voice, ULTRA_LFO_VIBRATO);
144 	snd_gf1_lfo_shutdown(gus, voice, ULTRA_LFO_TREMOLO);
145 #endif
146 }
147 
148 static void snd_gf1_clear_voices(struct snd_gus_card * gus, unsigned short v_min,
149 				 unsigned short v_max)
150 {
151 	unsigned long flags;
152 	unsigned int daddr;
153 	unsigned short i, w_16;
154 
155 	daddr = gus->gf1.default_voice_address << 4;
156 	for (i = v_min; i <= v_max; i++) {
157 #if 0
158 		if (gus->gf1.syn_voices)
159 			gus->gf1.syn_voices[i].flags = ~VFLG_DYNAMIC;
160 #endif
161 		spin_lock_irqsave(&gus->reg_lock, flags);
162 		snd_gf1_select_voice(gus, i);
163 		snd_gf1_ctrl_stop(gus, SNDRV_GF1_VB_ADDRESS_CONTROL);	/* Voice Control Register = voice stop */
164 		snd_gf1_ctrl_stop(gus, SNDRV_GF1_VB_VOLUME_CONTROL);	/* Volume Ramp Control Register = ramp off */
165 		if (gus->gf1.enh_mode)
166 			snd_gf1_write8(gus, SNDRV_GF1_VB_MODE, gus->gf1.memory ? 0x02 : 0x82);	/* Deactivate voice */
167 		w_16 = snd_gf1_read8(gus, SNDRV_GF1_VB_ADDRESS_CONTROL) & 0x04;
168 		snd_gf1_write16(gus, SNDRV_GF1_VW_FREQUENCY, 0x400);
169 		snd_gf1_write_addr(gus, SNDRV_GF1_VA_START, daddr, w_16);
170 		snd_gf1_write_addr(gus, SNDRV_GF1_VA_END, daddr, w_16);
171 		snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_START, 0);
172 		snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_END, 0);
173 		snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_RATE, 0);
174 		snd_gf1_write16(gus, SNDRV_GF1_VW_VOLUME, 0);
175 		snd_gf1_write_addr(gus, SNDRV_GF1_VA_CURRENT, daddr, w_16);
176 		snd_gf1_write8(gus, SNDRV_GF1_VB_PAN, 7);
177 		if (gus->gf1.enh_mode) {
178 			snd_gf1_write8(gus, SNDRV_GF1_VB_ACCUMULATOR, 0);
179 			snd_gf1_write16(gus, SNDRV_GF1_VW_EFFECT_VOLUME, 0);
180 			snd_gf1_write16(gus, SNDRV_GF1_VW_EFFECT_VOLUME_FINAL, 0);
181 		}
182 		spin_unlock_irqrestore(&gus->reg_lock, flags);
183 #if 0
184 		snd_gf1_lfo_shutdown(gus, i, ULTRA_LFO_VIBRATO);
185 		snd_gf1_lfo_shutdown(gus, i, ULTRA_LFO_TREMOLO);
186 #endif
187 	}
188 }
189 
190 void snd_gf1_stop_voices(struct snd_gus_card * gus, unsigned short v_min, unsigned short v_max)
191 {
192 	unsigned long flags;
193 	short i, ramp_ok;
194 	unsigned short ramp_end;
195 
196 	if (!in_interrupt()) {	/* this can't be done in interrupt */
197 		for (i = v_min, ramp_ok = 0; i <= v_max; i++) {
198 			spin_lock_irqsave(&gus->reg_lock, flags);
199 			snd_gf1_select_voice(gus, i);
200 			ramp_end = snd_gf1_read16(gus, 9) >> 8;
201 			if (ramp_end > SNDRV_GF1_MIN_OFFSET) {
202 				ramp_ok++;
203 				snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_RATE, 20);	/* ramp rate */
204 				snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_START, SNDRV_GF1_MIN_OFFSET);	/* ramp start */
205 				snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_END, ramp_end);	/* ramp end */
206 				snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_CONTROL, 0x40);	/* ramp down */
207 				if (gus->gf1.enh_mode) {
208 					snd_gf1_delay(gus);
209 					snd_gf1_write8(gus, SNDRV_GF1_VB_VOLUME_CONTROL, 0x40);
210 				}
211 			}
212 			spin_unlock_irqrestore(&gus->reg_lock, flags);
213 		}
214 		msleep_interruptible(50);
215 	}
216 	snd_gf1_clear_voices(gus, v_min, v_max);
217 }
218 
219 static void snd_gf1_alloc_voice_use(struct snd_gus_card * gus,
220 				    struct snd_gus_voice * pvoice,
221 				    int type, int client, int port)
222 {
223 	pvoice->use = 1;
224 	switch (type) {
225 	case SNDRV_GF1_VOICE_TYPE_PCM:
226 		gus->gf1.pcm_alloc_voices++;
227 		pvoice->pcm = 1;
228 		break;
229 	case SNDRV_GF1_VOICE_TYPE_SYNTH:
230 		pvoice->synth = 1;
231 		pvoice->client = client;
232 		pvoice->port = port;
233 		break;
234 	case SNDRV_GF1_VOICE_TYPE_MIDI:
235 		pvoice->midi = 1;
236 		pvoice->client = client;
237 		pvoice->port = port;
238 		break;
239 	}
240 }
241 
242 struct snd_gus_voice *snd_gf1_alloc_voice(struct snd_gus_card * gus, int type, int client, int port)
243 {
244 	struct snd_gus_voice *pvoice;
245 	unsigned long flags;
246 	int idx;
247 
248 	spin_lock_irqsave(&gus->voice_alloc, flags);
249 	if (type == SNDRV_GF1_VOICE_TYPE_PCM) {
250 		if (gus->gf1.pcm_alloc_voices >= gus->gf1.pcm_channels) {
251 			spin_unlock_irqrestore(&gus->voice_alloc, flags);
252 			return NULL;
253 		}
254 	}
255 	for (idx = 0; idx < 32; idx++) {
256 		pvoice = &gus->gf1.voices[idx];
257 		if (!pvoice->use) {
258 			snd_gf1_alloc_voice_use(gus, pvoice, type, client, port);
259 			spin_unlock_irqrestore(&gus->voice_alloc, flags);
260 			return pvoice;
261 		}
262 	}
263 	for (idx = 0; idx < 32; idx++) {
264 		pvoice = &gus->gf1.voices[idx];
265 		if (pvoice->midi && !pvoice->client) {
266 			snd_gf1_clear_voices(gus, pvoice->number, pvoice->number);
267 			snd_gf1_alloc_voice_use(gus, pvoice, type, client, port);
268 			spin_unlock_irqrestore(&gus->voice_alloc, flags);
269 			return pvoice;
270 		}
271 	}
272 	spin_unlock_irqrestore(&gus->voice_alloc, flags);
273 	return NULL;
274 }
275 
276 void snd_gf1_free_voice(struct snd_gus_card * gus, struct snd_gus_voice *voice)
277 {
278 	unsigned long flags;
279 	void (*private_free)(struct snd_gus_voice *voice);
280 
281 	if (voice == NULL || !voice->use)
282 		return;
283 	snd_gf1_set_default_handlers(gus, SNDRV_GF1_HANDLER_VOICE | voice->number);
284 	snd_gf1_clear_voices(gus, voice->number, voice->number);
285 	spin_lock_irqsave(&gus->voice_alloc, flags);
286 	private_free = voice->private_free;
287 	voice->private_free = NULL;
288 	voice->private_data = NULL;
289 	if (voice->pcm)
290 		gus->gf1.pcm_alloc_voices--;
291 	voice->use = voice->pcm = 0;
292 	voice->sample_ops = NULL;
293 	spin_unlock_irqrestore(&gus->voice_alloc, flags);
294 	if (private_free)
295 		private_free(voice);
296 }
297 
298 /*
299  *  call this function only by start of driver
300  */
301 
302 int snd_gf1_start(struct snd_gus_card * gus)
303 {
304 	unsigned long flags;
305 	unsigned int i;
306 
307 	snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 0);	/* reset GF1 */
308 	udelay(160);
309 	snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 1);	/* disable IRQ & DAC */
310 	udelay(160);
311 	snd_gf1_i_write8(gus, SNDRV_GF1_GB_JOYSTICK_DAC_LEVEL, gus->joystick_dac);
312 
313 	snd_gf1_set_default_handlers(gus, SNDRV_GF1_HANDLER_ALL);
314 	for (i = 0; i < 32; i++) {
315 		gus->gf1.voices[i].number = i;
316 		snd_gf1_set_default_handlers(gus, SNDRV_GF1_HANDLER_VOICE | i);
317 	}
318 
319 	snd_gf1_uart_cmd(gus, 0x03);	/* huh.. this cleanup took me some time... */
320 
321 	if (gus->gf1.enh_mode) {	/* enhanced mode !!!! */
322 		snd_gf1_i_write8(gus, SNDRV_GF1_GB_GLOBAL_MODE, snd_gf1_i_look8(gus, SNDRV_GF1_GB_GLOBAL_MODE) | 0x01);
323 		snd_gf1_i_write8(gus, SNDRV_GF1_GB_MEMORY_CONTROL, 0x01);
324 	}
325 	snd_gf1_clear_regs(gus);
326 	snd_gf1_select_active_voices(gus);
327 	snd_gf1_delay(gus);
328 	gus->gf1.default_voice_address = gus->gf1.memory > 0 ? 0 : 512 - 8;
329 	/* initialize LFOs & clear LFOs memory */
330 	if (gus->gf1.enh_mode && gus->gf1.memory) {
331 		gus->gf1.hw_lfo = 1;
332 		gus->gf1.default_voice_address += 1024;
333 	} else {
334 		gus->gf1.sw_lfo = 1;
335 	}
336 #if 0
337 	snd_gf1_lfo_init(gus);
338 #endif
339 	if (gus->gf1.memory > 0)
340 		for (i = 0; i < 4; i++)
341 			snd_gf1_poke(gus, gus->gf1.default_voice_address + i, 0);
342 	snd_gf1_clear_regs(gus);
343 	snd_gf1_clear_voices(gus, 0, 31);
344 	snd_gf1_look_regs(gus);
345 	udelay(160);
346 	snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 7);	/* Reset Register = IRQ enable, DAC enable */
347 	udelay(160);
348 	snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 7);	/* Reset Register = IRQ enable, DAC enable */
349 	if (gus->gf1.enh_mode) {	/* enhanced mode !!!! */
350 		snd_gf1_i_write8(gus, SNDRV_GF1_GB_GLOBAL_MODE, snd_gf1_i_look8(gus, SNDRV_GF1_GB_GLOBAL_MODE) | 0x01);
351 		snd_gf1_i_write8(gus, SNDRV_GF1_GB_MEMORY_CONTROL, 0x01);
352 	}
353 	while ((snd_gf1_i_read8(gus, SNDRV_GF1_GB_VOICES_IRQ) & 0xc0) != 0xc0);
354 
355 	spin_lock_irqsave(&gus->reg_lock, flags);
356 	outb(gus->gf1.active_voice = 0, GUSP(gus, GF1PAGE));
357 	outb(gus->mix_cntrl_reg, GUSP(gus, MIXCNTRLREG));
358 	spin_unlock_irqrestore(&gus->reg_lock, flags);
359 
360 	snd_gf1_timers_init(gus);
361 	snd_gf1_look_regs(gus);
362 	snd_gf1_mem_init(gus);
363 	snd_gf1_mem_proc_init(gus);
364 #ifdef CONFIG_SND_DEBUG
365 	snd_gus_irq_profile_init(gus);
366 #endif
367 
368 #if 0
369 	if (gus->pnp_flag) {
370 		if (gus->chip.playback_fifo_size > 0)
371 			snd_gf1_i_write16(gus, SNDRV_GF1_GW_FIFO_RECORD_BASE_ADDR, gus->chip.playback_fifo_block->ptr >> 8);
372 		if (gus->chip.record_fifo_size > 0)
373 			snd_gf1_i_write16(gus, SNDRV_GF1_GW_FIFO_PLAY_BASE_ADDR, gus->chip.record_fifo_block->ptr >> 8);
374 		snd_gf1_i_write16(gus, SNDRV_GF1_GW_FIFO_SIZE, gus->chip.interwave_fifo_reg);
375 	}
376 #endif
377 
378 	return 0;
379 }
380 
381 /*
382  *  call this function only by shutdown of driver
383  */
384 
385 int snd_gf1_stop(struct snd_gus_card * gus)
386 {
387 	snd_gf1_i_write8(gus, SNDRV_GF1_GB_SOUND_BLASTER_CONTROL, 0); /* stop all timers */
388 	snd_gf1_stop_voices(gus, 0, 31);		/* stop all voices */
389 	snd_gf1_i_write8(gus, SNDRV_GF1_GB_RESET, 1);	/* disable IRQ & DAC */
390 	snd_gf1_timers_done(gus);
391 	snd_gf1_mem_done(gus);
392 #if 0
393 	snd_gf1_lfo_done(gus);
394 #endif
395 	return 0;
396 }
397