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