-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathgus_wave.c
3464 lines (2903 loc) · 79 KB
/
gus_wave.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* sound/gus_wave.c
*
* Driver for the Gravis UltraSound wave table synth.
*
*
* Copyright (C) by Hannu Savolainen 1993-1997
*
* OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
* Version 2 (June 1991). See the "COPYING" file distributed with this software
* for more info.
*
*
* Thomas Sailer : ioctl code reworked (vmalloc/vfree removed)
* Frank van de Pol : Fixed GUS MAX interrupt handling. Enabled simultanious
* usage of CS4231A codec, GUS wave and MIDI for GUS MAX.
* Bartlomiej Zolnierkiewicz : added some __init/__exit
*/
#include <linux/init.h>
#include <linux/config.h>
#include <linux/spinlock.h>
#define GUSPNP_AUTODETECT
#include "sound_config.h"
#include <linux/ultrasound.h>
#include "gus.h"
#include "gus_hw.h"
#define GUS_BANK_SIZE (((iw_mode) ? 256*1024*1024 : 256*1024))
#define MAX_SAMPLE 150
#define MAX_PATCH 256
#define NOT_SAMPLE 0xffff
struct voice_info
{
unsigned long orig_freq;
unsigned long current_freq;
unsigned long mode;
int fixed_pitch;
int bender;
int bender_range;
int panning;
int midi_volume;
unsigned int initial_volume;
unsigned int current_volume;
int loop_irq_mode, loop_irq_parm;
#define LMODE_FINISH 1
#define LMODE_PCM 2
#define LMODE_PCM_STOP 3
int volume_irq_mode, volume_irq_parm;
#define VMODE_HALT 1
#define VMODE_ENVELOPE 2
#define VMODE_START_NOTE 3
int env_phase;
unsigned char env_rate[6];
unsigned char env_offset[6];
/*
* Volume computation parameters for gus_adagio_vol()
*/
int main_vol, expression_vol, patch_vol;
/* Variables for "Ultraclick" removal */
int dev_pending, note_pending, volume_pending,
sample_pending;
char kill_pending;
long offset_pending;
};
static struct voice_alloc_info *voice_alloc;
static struct address_info *gus_hw_config;
extern int gus_base;
extern int gus_irq, gus_dma;
extern int gus_pnp_flag;
extern int gus_no_wave_dma;
static int gus_dma2 = -1;
static int dual_dma_mode;
static long gus_mem_size;
static long free_mem_ptr;
static int gus_busy;
static int gus_no_dma;
static int nr_voices;
static int gus_devnum;
static int volume_base, volume_scale, volume_method;
static int gus_recmask = SOUND_MASK_MIC;
static int recording_active;
static int only_read_access;
static int only_8_bits;
static int iw_mode = 0;
int gus_wave_volume = 60;
int gus_pcm_volume = 80;
int have_gus_max = 0;
static int gus_line_vol = 100, gus_mic_vol;
static unsigned char mix_image = 0x00;
int gus_timer_enabled = 0;
/*
* Current version of this driver doesn't allow synth and PCM functions
* at the same time. The active_device specifies the active driver
*/
static int active_device;
#define GUS_DEV_WAVE 1 /* Wave table synth */
#define GUS_DEV_PCM_DONE 2 /* PCM device, transfer done */
#define GUS_DEV_PCM_CONTINUE 3 /* PCM device, transfer done ch. 1/2 */
static int gus_audio_speed;
static int gus_audio_channels;
static int gus_audio_bits;
static int gus_audio_bsize;
static char bounce_buf[8 * 1024]; /* Must match value set to max_fragment */
static DECLARE_WAIT_QUEUE_HEAD(dram_sleeper);
/*
* Variables and buffers for PCM output
*/
#define MAX_PCM_BUFFERS (128*MAX_REALTIME_FACTOR) /* Don't change */
static int pcm_bsize, pcm_nblk, pcm_banksize;
static int pcm_datasize[MAX_PCM_BUFFERS];
static volatile int pcm_head, pcm_tail, pcm_qlen;
static volatile int pcm_active;
static volatile int dma_active;
static int pcm_opened;
static int pcm_current_dev;
static int pcm_current_block;
static unsigned long pcm_current_buf;
static int pcm_current_count;
static int pcm_current_intrflag;
DEFINE_SPINLOCK(gus_lock);
extern int *gus_osp;
static struct voice_info voices[32];
static int freq_div_table[] =
{
44100, /* 14 */
41160, /* 15 */
38587, /* 16 */
36317, /* 17 */
34300, /* 18 */
32494, /* 19 */
30870, /* 20 */
29400, /* 21 */
28063, /* 22 */
26843, /* 23 */
25725, /* 24 */
24696, /* 25 */
23746, /* 26 */
22866, /* 27 */
22050, /* 28 */
21289, /* 29 */
20580, /* 30 */
19916, /* 31 */
19293 /* 32 */
};
static struct patch_info *samples;
static long sample_ptrs[MAX_SAMPLE + 1];
static int sample_map[32];
static int free_sample;
static int mixer_type;
static int patch_table[MAX_PATCH];
static int patch_map[32];
static struct synth_info gus_info = {
"Gravis UltraSound", 0, SYNTH_TYPE_SAMPLE, SAMPLE_TYPE_GUS,
0, 16, 0, MAX_PATCH
};
static void gus_poke(long addr, unsigned char data);
static void compute_and_set_volume(int voice, int volume, int ramp_time);
extern unsigned short gus_adagio_vol(int vel, int mainv, int xpn, int voicev);
extern unsigned short gus_linear_vol(int vol, int mainvol);
static void compute_volume(int voice, int volume);
static void do_volume_irq(int voice);
static void set_input_volumes(void);
static void gus_tmr_install(int io_base);
#define INSTANT_RAMP -1 /* Instant change. No ramping */
#define FAST_RAMP 0 /* Fastest possible ramp */
static void reset_sample_memory(void)
{
int i;
for (i = 0; i <= MAX_SAMPLE; i++)
sample_ptrs[i] = -1;
for (i = 0; i < 32; i++)
sample_map[i] = -1;
for (i = 0; i < 32; i++)
patch_map[i] = -1;
gus_poke(0, 0); /* Put a silent sample to the beginning */
gus_poke(1, 0);
free_mem_ptr = 2;
free_sample = 0;
for (i = 0; i < MAX_PATCH; i++)
patch_table[i] = NOT_SAMPLE;
}
void gus_delay(void)
{
int i;
for (i = 0; i < 7; i++)
inb(u_DRAMIO);
}
static void gus_poke(long addr, unsigned char data)
{ /* Writes a byte to the DRAM */
outb((0x43), u_Command);
outb((addr & 0xff), u_DataLo);
outb(((addr >> 8) & 0xff), u_DataHi);
outb((0x44), u_Command);
outb(((addr >> 16) & 0xff), u_DataHi);
outb((data), u_DRAMIO);
}
static unsigned char gus_peek(long addr)
{ /* Reads a byte from the DRAM */
unsigned char tmp;
outb((0x43), u_Command);
outb((addr & 0xff), u_DataLo);
outb(((addr >> 8) & 0xff), u_DataHi);
outb((0x44), u_Command);
outb(((addr >> 16) & 0xff), u_DataHi);
tmp = inb(u_DRAMIO);
return tmp;
}
void gus_write8(int reg, unsigned int data)
{ /* Writes to an indirect register (8 bit) */
outb((reg), u_Command);
outb(((unsigned char) (data & 0xff)), u_DataHi);
}
static unsigned char gus_read8(int reg)
{
/* Reads from an indirect register (8 bit). Offset 0x80. */
unsigned char val;
outb((reg | 0x80), u_Command);
val = inb(u_DataHi);
return val;
}
static unsigned char gus_look8(int reg)
{
/* Reads from an indirect register (8 bit). No additional offset. */
unsigned char val;
outb((reg), u_Command);
val = inb(u_DataHi);
return val;
}
static void gus_write16(int reg, unsigned int data)
{
/* Writes to an indirect register (16 bit) */
outb((reg), u_Command);
outb(((unsigned char) (data & 0xff)), u_DataLo);
outb(((unsigned char) ((data >> 8) & 0xff)), u_DataHi);
}
static unsigned short gus_read16(int reg)
{
/* Reads from an indirect register (16 bit). Offset 0x80. */
unsigned char hi, lo;
outb((reg | 0x80), u_Command);
lo = inb(u_DataLo);
hi = inb(u_DataHi);
return ((hi << 8) & 0xff00) | lo;
}
static unsigned short gus_look16(int reg)
{
/* Reads from an indirect register (16 bit). No additional offset. */
unsigned char hi, lo;
outb((reg), u_Command);
lo = inb(u_DataLo);
hi = inb(u_DataHi);
return ((hi << 8) & 0xff00) | lo;
}
static void gus_write_addr(int reg, unsigned long address, int frac, int is16bit)
{
/* Writes an 24 bit memory address */
unsigned long hold_address;
if (is16bit)
{
if (iw_mode)
{
/* Interwave spesific address translations */
address >>= 1;
}
else
{
/*
* Special processing required for 16 bit patches
*/
hold_address = address;
address = address >> 1;
address &= 0x0001ffffL;
address |= (hold_address & 0x000c0000L);
}
}
gus_write16(reg, (unsigned short) ((address >> 7) & 0xffff));
gus_write16(reg + 1, (unsigned short) ((address << 9) & 0xffff)
+ (frac << 5));
/* Could writing twice fix problems with GUS_VOICE_POS()? Let's try. */
gus_delay();
gus_write16(reg, (unsigned short) ((address >> 7) & 0xffff));
gus_write16(reg + 1, (unsigned short) ((address << 9) & 0xffff)
+ (frac << 5));
}
static void gus_select_voice(int voice)
{
if (voice < 0 || voice > 31)
return;
outb((voice), u_Voice);
}
static void gus_select_max_voices(int nvoices)
{
if (iw_mode)
nvoices = 32;
if (nvoices < 14)
nvoices = 14;
if (nvoices > 32)
nvoices = 32;
voice_alloc->max_voice = nr_voices = nvoices;
gus_write8(0x0e, (nvoices - 1) | 0xc0);
}
static void gus_voice_on(unsigned int mode)
{
gus_write8(0x00, (unsigned char) (mode & 0xfc));
gus_delay();
gus_write8(0x00, (unsigned char) (mode & 0xfc));
}
static void gus_voice_off(void)
{
gus_write8(0x00, gus_read8(0x00) | 0x03);
}
static void gus_voice_mode(unsigned int m)
{
unsigned char mode = (unsigned char) (m & 0xff);
gus_write8(0x00, (gus_read8(0x00) & 0x03) |
(mode & 0xfc)); /* Don't touch last two bits */
gus_delay();
gus_write8(0x00, (gus_read8(0x00) & 0x03) | (mode & 0xfc));
}
static void gus_voice_freq(unsigned long freq)
{
unsigned long divisor = freq_div_table[nr_voices - 14];
unsigned short fc;
/* Interwave plays at 44100 Hz with any number of voices */
if (iw_mode)
fc = (unsigned short) (((freq << 9) + (44100 >> 1)) / 44100);
else
fc = (unsigned short) (((freq << 9) + (divisor >> 1)) / divisor);
fc = fc << 1;
gus_write16(0x01, fc);
}
static void gus_voice_volume(unsigned int vol)
{
gus_write8(0x0d, 0x03); /* Stop ramp before setting volume */
gus_write16(0x09, (unsigned short) (vol << 4));
}
static void gus_voice_balance(unsigned int balance)
{
gus_write8(0x0c, (unsigned char) (balance & 0xff));
}
static void gus_ramp_range(unsigned int low, unsigned int high)
{
gus_write8(0x07, (unsigned char) ((low >> 4) & 0xff));
gus_write8(0x08, (unsigned char) ((high >> 4) & 0xff));
}
static void gus_ramp_rate(unsigned int scale, unsigned int rate)
{
gus_write8(0x06, (unsigned char) (((scale & 0x03) << 6) | (rate & 0x3f)));
}
static void gus_rampon(unsigned int m)
{
unsigned char mode = (unsigned char) (m & 0xff);
gus_write8(0x0d, mode & 0xfc);
gus_delay();
gus_write8(0x0d, mode & 0xfc);
}
static void gus_ramp_mode(unsigned int m)
{
unsigned char mode = (unsigned char) (m & 0xff);
gus_write8(0x0d, (gus_read8(0x0d) & 0x03) |
(mode & 0xfc)); /* Leave the last 2 bits alone */
gus_delay();
gus_write8(0x0d, (gus_read8(0x0d) & 0x03) | (mode & 0xfc));
}
static void gus_rampoff(void)
{
gus_write8(0x0d, 0x03);
}
static void gus_set_voice_pos(int voice, long position)
{
int sample_no;
if ((sample_no = sample_map[voice]) != -1) {
if (position < samples[sample_no].len) {
if (voices[voice].volume_irq_mode == VMODE_START_NOTE)
voices[voice].offset_pending = position;
else
gus_write_addr(0x0a, sample_ptrs[sample_no] + position, 0,
samples[sample_no].mode & WAVE_16_BITS);
}
}
}
static void gus_voice_init(int voice)
{
unsigned long flags;
spin_lock_irqsave(&gus_lock,flags);
gus_select_voice(voice);
gus_voice_volume(0);
gus_voice_off();
gus_write_addr(0x0a, 0, 0, 0); /* Set current position to 0 */
gus_write8(0x00, 0x03); /* Voice off */
gus_write8(0x0d, 0x03); /* Ramping off */
voice_alloc->map[voice] = 0;
voice_alloc->alloc_times[voice] = 0;
spin_unlock_irqrestore(&gus_lock,flags);
}
static void gus_voice_init2(int voice)
{
voices[voice].panning = 0;
voices[voice].mode = 0;
voices[voice].orig_freq = 20000;
voices[voice].current_freq = 20000;
voices[voice].bender = 0;
voices[voice].bender_range = 200;
voices[voice].initial_volume = 0;
voices[voice].current_volume = 0;
voices[voice].loop_irq_mode = 0;
voices[voice].loop_irq_parm = 0;
voices[voice].volume_irq_mode = 0;
voices[voice].volume_irq_parm = 0;
voices[voice].env_phase = 0;
voices[voice].main_vol = 127;
voices[voice].patch_vol = 127;
voices[voice].expression_vol = 127;
voices[voice].sample_pending = -1;
voices[voice].fixed_pitch = 0;
}
static void step_envelope(int voice)
{
unsigned vol, prev_vol, phase;
unsigned char rate;
unsigned long flags;
if (voices[voice].mode & WAVE_SUSTAIN_ON && voices[voice].env_phase == 2)
{
spin_lock_irqsave(&gus_lock,flags);
gus_select_voice(voice);
gus_rampoff();
spin_unlock_irqrestore(&gus_lock,flags);
return;
/*
* Sustain phase begins. Continue envelope after receiving note off.
*/
}
if (voices[voice].env_phase >= 5)
{
/* Envelope finished. Shoot the voice down */
gus_voice_init(voice);
return;
}
prev_vol = voices[voice].current_volume;
phase = ++voices[voice].env_phase;
compute_volume(voice, voices[voice].midi_volume);
vol = voices[voice].initial_volume * voices[voice].env_offset[phase] / 255;
rate = voices[voice].env_rate[phase];
spin_lock_irqsave(&gus_lock,flags);
gus_select_voice(voice);
gus_voice_volume(prev_vol);
gus_write8(0x06, rate); /* Ramping rate */
voices[voice].volume_irq_mode = VMODE_ENVELOPE;
if (((vol - prev_vol) / 64) == 0) /* No significant volume change */
{
spin_unlock_irqrestore(&gus_lock,flags);
step_envelope(voice); /* Continue the envelope on the next step */
return;
}
if (vol > prev_vol)
{
if (vol >= (4096 - 64))
vol = 4096 - 65;
gus_ramp_range(0, vol);
gus_rampon(0x20); /* Increasing volume, with IRQ */
}
else
{
if (vol <= 64)
vol = 65;
gus_ramp_range(vol, 4030);
gus_rampon(0x60); /* Decreasing volume, with IRQ */
}
voices[voice].current_volume = vol;
spin_unlock_irqrestore(&gus_lock,flags);
}
static void init_envelope(int voice)
{
voices[voice].env_phase = -1;
voices[voice].current_volume = 64;
step_envelope(voice);
}
static void start_release(int voice)
{
if (gus_read8(0x00) & 0x03)
return; /* Voice already stopped */
voices[voice].env_phase = 2; /* Will be incremented by step_envelope */
voices[voice].current_volume = voices[voice].initial_volume =
gus_read16(0x09) >> 4; /* Get current volume */
voices[voice].mode &= ~WAVE_SUSTAIN_ON;
gus_rampoff();
step_envelope(voice);
}
static void gus_voice_fade(int voice)
{
int instr_no = sample_map[voice], is16bits;
unsigned long flags;
spin_lock_irqsave(&gus_lock,flags);
gus_select_voice(voice);
if (instr_no < 0 || instr_no > MAX_SAMPLE)
{
gus_write8(0x00, 0x03); /* Hard stop */
voice_alloc->map[voice] = 0;
spin_unlock_irqrestore(&gus_lock,flags);
return;
}
is16bits = (samples[instr_no].mode & WAVE_16_BITS) ? 1 : 0; /* 8 or 16 bits */
if (voices[voice].mode & WAVE_ENVELOPES)
{
start_release(voice);
spin_unlock_irqrestore(&gus_lock,flags);
return;
}
/*
* Ramp the volume down but not too quickly.
*/
if ((int) (gus_read16(0x09) >> 4) < 100) /* Get current volume */
{
gus_voice_off();
gus_rampoff();
gus_voice_init(voice);
spin_unlock_irqrestore(&gus_lock,flags);
return;
}
gus_ramp_range(65, 4030);
gus_ramp_rate(2, 4);
gus_rampon(0x40 | 0x20); /* Down, once, with IRQ */
voices[voice].volume_irq_mode = VMODE_HALT;
spin_unlock_irqrestore(&gus_lock,flags);
}
static void gus_reset(void)
{
int i;
gus_select_max_voices(24);
volume_base = 3071;
volume_scale = 4;
volume_method = VOL_METHOD_ADAGIO;
for (i = 0; i < 32; i++)
{
gus_voice_init(i); /* Turn voice off */
gus_voice_init2(i);
}
}
static void gus_initialize(void)
{
unsigned long flags;
unsigned char dma_image, irq_image, tmp;
static unsigned char gus_irq_map[16] = {
0, 0, 0, 3, 0, 2, 0, 4, 0, 1, 0, 5, 6, 0, 0, 7
};
static unsigned char gus_dma_map[8] = {
0, 1, 0, 2, 0, 3, 4, 5
};
spin_lock_irqsave(&gus_lock,flags);
gus_write8(0x4c, 0); /* Reset GF1 */
gus_delay();
gus_delay();
gus_write8(0x4c, 1); /* Release Reset */
gus_delay();
gus_delay();
/*
* Clear all interrupts
*/
gus_write8(0x41, 0); /* DMA control */
gus_write8(0x45, 0); /* Timer control */
gus_write8(0x49, 0); /* Sample control */
gus_select_max_voices(24);
inb(u_Status); /* Touch the status register */
gus_look8(0x41); /* Clear any pending DMA IRQs */
gus_look8(0x49); /* Clear any pending sample IRQs */
gus_read8(0x0f); /* Clear pending IRQs */
gus_reset(); /* Resets all voices */
gus_look8(0x41); /* Clear any pending DMA IRQs */
gus_look8(0x49); /* Clear any pending sample IRQs */
gus_read8(0x0f); /* Clear pending IRQs */
gus_write8(0x4c, 7); /* Master reset | DAC enable | IRQ enable */
/*
* Set up for Digital ASIC
*/
outb((0x05), gus_base + 0x0f);
mix_image |= 0x02; /* Disable line out (for a moment) */
outb((mix_image), u_Mixer);
outb((0x00), u_IRQDMAControl);
outb((0x00), gus_base + 0x0f);
/*
* Now set up the DMA and IRQ interface
*
* The GUS supports two IRQs and two DMAs.
*
* Just one DMA channel is used. This prevents simultaneous ADC and DAC.
* Adding this support requires significant changes to the dmabuf.c, dsp.c
* and audio.c also.
*/
irq_image = 0;
tmp = gus_irq_map[gus_irq];
if (!gus_pnp_flag && !tmp)
printk(KERN_WARNING "Warning! GUS IRQ not selected\n");
irq_image |= tmp;
irq_image |= 0x40; /* Combine IRQ1 (GF1) and IRQ2 (Midi) */
dual_dma_mode = 1;
if (gus_dma2 == gus_dma || gus_dma2 == -1)
{
dual_dma_mode = 0;
dma_image = 0x40; /* Combine DMA1 (DRAM) and IRQ2 (ADC) */
tmp = gus_dma_map[gus_dma];
if (!tmp)
printk(KERN_WARNING "Warning! GUS DMA not selected\n");
dma_image |= tmp;
}
else
{
/* Setup dual DMA channel mode for GUS MAX */
dma_image = gus_dma_map[gus_dma];
if (!dma_image)
printk(KERN_WARNING "Warning! GUS DMA not selected\n");
tmp = gus_dma_map[gus_dma2] << 3;
if (!tmp)
{
printk(KERN_WARNING "Warning! Invalid GUS MAX DMA\n");
tmp = 0x40; /* Combine DMA channels */
dual_dma_mode = 0;
}
dma_image |= tmp;
}
/*
* For some reason the IRQ and DMA addresses must be written twice
*/
/*
* Doing it first time
*/
outb((mix_image), u_Mixer); /* Select DMA control */
outb((dma_image | 0x80), u_IRQDMAControl); /* Set DMA address */
outb((mix_image | 0x40), u_Mixer); /* Select IRQ control */
outb((irq_image), u_IRQDMAControl); /* Set IRQ address */
/*
* Doing it second time
*/
outb((mix_image), u_Mixer); /* Select DMA control */
outb((dma_image), u_IRQDMAControl); /* Set DMA address */
outb((mix_image | 0x40), u_Mixer); /* Select IRQ control */
outb((irq_image), u_IRQDMAControl); /* Set IRQ address */
gus_select_voice(0); /* This disables writes to IRQ/DMA reg */
mix_image &= ~0x02; /* Enable line out */
mix_image |= 0x08; /* Enable IRQ */
outb((mix_image), u_Mixer); /*
* Turn mixer channels on
* Note! Mic in is left off.
*/
gus_select_voice(0); /* This disables writes to IRQ/DMA reg */
gusintr(gus_irq, (void *)gus_hw_config, NULL); /* Serve pending interrupts */
inb(u_Status); /* Touch the status register */
gus_look8(0x41); /* Clear any pending DMA IRQs */
gus_look8(0x49); /* Clear any pending sample IRQs */
gus_read8(0x0f); /* Clear pending IRQs */
if (iw_mode)
gus_write8(0x19, gus_read8(0x19) | 0x01);
spin_unlock_irqrestore(&gus_lock,flags);
}
static void __init pnp_mem_init(void)
{
#include "iwmem.h"
#define CHUNK_SIZE (256*1024)
#define BANK_SIZE (4*1024*1024)
#define CHUNKS_PER_BANK (BANK_SIZE/CHUNK_SIZE)
int bank, chunk, addr, total = 0;
int bank_sizes[4];
int i, j, bits = -1, testbits = -1, nbanks = 0;
/*
* This routine determines what kind of RAM is installed in each of the four
* SIMM banks and configures the DRAM address decode logic accordingly.
*/
/*
* Place the chip into enhanced mode
*/
gus_write8(0x19, gus_read8(0x19) | 0x01);
gus_write8(0x53, gus_look8(0x53) & ~0x02); /* Select DRAM I/O access */
/*
* Set memory configuration to 4 DRAM banks of 4M in each (16M total).
*/
gus_write16(0x52, (gus_look16(0x52) & 0xfff0) | 0x000c);
/*
* Perform the DRAM size detection for each bank individually.
*/
for (bank = 0; bank < 4; bank++)
{
int size = 0;
addr = bank * BANK_SIZE;
/* Clean check points of each chunk */
for (chunk = 0; chunk < CHUNKS_PER_BANK; chunk++)
{
gus_poke(addr + chunk * CHUNK_SIZE + 0L, 0x00);
gus_poke(addr + chunk * CHUNK_SIZE + 1L, 0x00);
}
/* Write a value to each chunk point and verify the result */
for (chunk = 0; chunk < CHUNKS_PER_BANK; chunk++)
{
gus_poke(addr + chunk * CHUNK_SIZE + 0L, 0x55);
gus_poke(addr + chunk * CHUNK_SIZE + 1L, 0xAA);
if (gus_peek(addr + chunk * CHUNK_SIZE + 0L) == 0x55 &&
gus_peek(addr + chunk * CHUNK_SIZE + 1L) == 0xAA)
{
/* OK. There is RAM. Now check for possible shadows */
int ok = 1, chunk2;
for (chunk2 = 0; ok && chunk2 < chunk; chunk2++)
if (gus_peek(addr + chunk2 * CHUNK_SIZE + 0L) ||
gus_peek(addr + chunk2 * CHUNK_SIZE + 1L))
ok = 0; /* Addressing wraps */
if (ok)
size = (chunk + 1) * CHUNK_SIZE;
}
gus_poke(addr + chunk * CHUNK_SIZE + 0L, 0x00);
gus_poke(addr + chunk * CHUNK_SIZE + 1L, 0x00);
}
bank_sizes[bank] = size;
if (size)
nbanks = bank + 1;
DDB(printk("Interwave: Bank %d, size=%dk\n", bank, size / 1024));
}
if (nbanks == 0) /* No RAM - Give up */
{
printk(KERN_ERR "Sound: An Interwave audio chip detected but no DRAM\n");
printk(KERN_ERR "Sound: Unable to work with this card.\n");
gus_write8(0x19, gus_read8(0x19) & ~0x01);
gus_mem_size = 0;
return;
}
/*
* Now we know how much DRAM there is in each bank. The next step is
* to find a DRAM size encoding (0 to 12) which is best for the combination
* we have.
*
* First try if any of the possible alternatives matches exactly the amount
* of memory we have.
*/
for (i = 0; bits == -1 && i < 13; i++)
{
bits = i;
for (j = 0; bits != -1 && j < 4; j++)
if (mem_decode[i][j] != bank_sizes[j])
bits = -1; /* No hit */
}
/*
* If necessary, try to find a combination where other than the last
* bank matches our configuration and the last bank is left oversized.
* In this way we don't leave holes in the middle of memory.
*/
if (bits == -1) /* No luck yet */
{
for (i = 0; bits == -1 && i < 13; i++)
{
bits = i;
for (j = 0; bits != -1 && j < nbanks - 1; j++)
if (mem_decode[i][j] != bank_sizes[j])
bits = -1; /* No hit */
if (mem_decode[i][nbanks - 1] < bank_sizes[nbanks - 1])
bits = -1; /* The last bank is too small */
}
}
/*
* The last resort is to search for a combination where the banks are
* smaller than the actual SIMMs. This leaves some memory in the banks
* unused but doesn't leave holes in the DRAM address space.
*/
if (bits == -1) /* No luck yet */
{
for (i = 0; i < 13; i++)
{
testbits = i;
for (j = 0; testbits != -1 && j < nbanks - 1; j++)
if (mem_decode[i][j] > bank_sizes[j]) {
testbits = -1;
}
if(testbits > bits) bits = testbits;
}
if (bits != -1)
{
printk(KERN_INFO "Interwave: Can't use all installed RAM.\n");
printk(KERN_INFO "Interwave: Try reordering SIMMS.\n");
}
printk(KERN_INFO "Interwave: Can't find working DRAM encoding.\n");
printk(KERN_INFO "Interwave: Defaulting to 256k. Try reordering SIMMS.\n");
bits = 0;
}
DDB(printk("Interwave: Selecting DRAM addressing mode %d\n", bits));
for (bank = 0; bank < 4; bank++)
{
DDB(printk(" Bank %d, mem=%dk (limit %dk)\n", bank, bank_sizes[bank] / 1024, mem_decode[bits][bank] / 1024));
if (bank_sizes[bank] > mem_decode[bits][bank])
total += mem_decode[bits][bank];
else
total += bank_sizes[bank];
}
DDB(printk("Total %dk of DRAM (enhanced mode)\n", total / 1024));
/*
* Set the memory addressing mode.
*/
gus_write16(0x52, (gus_look16(0x52) & 0xfff0) | bits);
/* Leave the chip into enhanced mode. Disable LFO */
gus_mem_size = total;
iw_mode = 1;
gus_write8(0x19, (gus_read8(0x19) | 0x01) & ~0x02);
}
int __init gus_wave_detect(int baseaddr)
{
unsigned long i, max_mem = 1024L;
unsigned long loc;
unsigned char val;
if (!request_region(baseaddr, 16, "GUS"))
return 0;
if (!request_region(baseaddr + 0x100, 12, "GUS")) { /* 0x10c-> is MAX */
release_region(baseaddr, 16);
return 0;
}
gus_base = baseaddr;
gus_write8(0x4c, 0); /* Reset GF1 */
gus_delay();
gus_delay();
gus_write8(0x4c, 1); /* Release Reset */
gus_delay();
gus_delay();
#ifdef GUSPNP_AUTODETECT
val = gus_look8(0x5b); /* Version number register */
gus_write8(0x5b, ~val); /* Invert all bits */