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