2 * Copyright (C) by Hannu Savolainen 1993-1997
6 * Initialization code for OPTi MAD16 compatible audio chips. Including
8 * OPTi 82C928 MAD16 (replaced by C929)
10 * OAK OTI-605 Mozart (later version with MPU401 Midi)
11 * OPTi 82C929 MAD16 Pro
15 * These audio interface chips don't produce sound themselves. They just
16 * connect some other components (OPL-[234] and a WSS compatible codec)
17 * to the PC bus and perform I/O, DMA and IRQ address decoding. There is
18 * also a UART for the MPU-401 mode (not 82C928/Mozart).
19 * The Mozart chip appears to be compatible with the 82C928, although later
20 * issues of the card, using the OTI-605 chip, have an MPU-401 compatable Midi
21 * port. This port is configured differently to that of the OPTi audio chips.
25 * Alan Cox Clean up, added module selections.
27 * A. Wik Added support for Opti924 PnP.
28 * Improved debugging support. 16-May-1998
29 * Fixed bug. 16-Jun-1998
31 * Torsten Duwe Made Opti924 PnP support non-destructive
34 * Paul Grayson Added support for Midi on later Mozart cards.
36 * Christoph Hellwig Adapted to module_init/module_exit.
37 * Arnaldo C. de Melo got rid of attach_uart401 21-Sep-2000
39 * Pavel Rabel Clean up Nov-2000
42 #include <linux/config.h>
43 #include <linux/init.h>
44 #include <linux/module.h>
45 #include <linux/gameport.h>
47 #include "sound_config.h"
53 static int mad16_conf;
54 static int mad16_cdsel;
55 static struct gameport gameport;
57 static int already_initialized = 0;
68 * The MAD16 occupies I/O ports 0xf8d to 0xf93 (fixed locations).
69 * All ports are inactive by default. They can be activated by
70 * writing 0xE2 or 0xE3 to the password register. The password is valid
71 * only until the next I/O read or write.
73 * 82C930 uses 0xE4 as the password and indirect addressing to access
74 * the config registers.
77 #define MC0_PORT 0xf8c /* Dummy port */
78 #define MC1_PORT 0xf8d /* SB address, CD-ROM interface type, joystick */
79 #define MC2_PORT 0xf8e /* CD-ROM address, IRQ, DMA, plus OPL4 bit */
80 #define MC3_PORT 0xf8f
81 #define PASSWD_REG 0xf8f
82 #define MC4_PORT 0xf90
83 #define MC5_PORT 0xf91
84 #define MC6_PORT 0xf92
85 #define MC7_PORT 0xf93
86 #define MC8_PORT 0xf94
87 #define MC9_PORT 0xf95
88 #define MC10_PORT 0xf96
89 #define MC11_PORT 0xf97
90 #define MC12_PORT 0xf98
92 static int board_type = C928;
94 static int *mad16_osp;
95 static int c931_detected; /* minor differences from C930 */
96 static char c924pnp = 0; /* " " " C924 */
97 static int debug = 0; /* debugging output */
102 #define DDB(x) {if (debug) x;}
104 static unsigned char mad_read(int port)
112 switch (board_type) /* Output password */
116 outb((0xE2), PASSWD_REG);
120 outb((0xE3), PASSWD_REG);
124 /* outb(( 0xE4), PASSWD_REG); */
128 /* the c924 has its ports relocated by -128 if
129 PnP is enabled -aw */
131 outb((0xE5), PASSWD_REG); else
132 outb((0xE5), PASSWD_REG - 0x80);
136 if (board_type == C930)
138 outb((port - MC0_PORT), 0xe0e); /* Write to index reg */
139 tmp = inb(0xe0f); /* Read from data reg */
143 tmp = inb(port); else
144 tmp = inb(port-0x80);
145 restore_flags(flags);
150 static void mad_write(int port, int value)
157 switch (board_type) /* Output password */
161 outb((0xE2), PASSWD_REG);
165 outb((0xE3), PASSWD_REG);
169 /* outb(( 0xE4), PASSWD_REG); */
174 outb((0xE5), PASSWD_REG); else
175 outb((0xE5), PASSWD_REG - 0x80);
179 if (board_type == C930)
181 outb((port - MC0_PORT), 0xe0e); /* Write to index reg */
182 outb(((unsigned char) (value & 0xff)), 0xe0f);
186 outb(((unsigned char) (value & 0xff)), port); else
187 outb(((unsigned char) (value & 0xff)), port-0x80);
188 restore_flags(flags);
191 static int __init detect_c930(void)
193 unsigned char tmp = mad_read(MC1_PORT);
195 if ((tmp & 0x06) != 0x06)
197 DDB(printk("Wrong C930 signature (%x)\n", tmp));
200 mad_write(MC1_PORT, 0);
202 if (mad_read(MC1_PORT) != 0x06)
204 DDB(printk("Wrong C930 signature2 (%x)\n", tmp));
207 mad_write(MC1_PORT, tmp); /* Restore bits */
209 mad_write(MC7_PORT, 0);
210 if ((tmp = mad_read(MC7_PORT)) != 0)
212 DDB(printk("MC7 not writable (%x)\n", tmp));
215 mad_write(MC7_PORT, 0xcb);
216 if ((tmp = mad_read(MC7_PORT)) != 0xcb)
218 DDB(printk("MC7 not writable2 (%x)\n", tmp));
222 tmp = mad_read(MC0_PORT+18);
223 if (tmp == 0xff || tmp == 0x00)
225 /* We probably have a C931 */
226 DDB(printk("Detected C931 config=0x%02x\n", tmp));
230 * We cannot configure the chip if it is in PnP mode.
231 * If we have a CSN assigned (bit 8 in MC13) we first try
232 * a software reset, then a software power off, finally
233 * Clearing PnP mode. The last option is not
236 if ((mad_read(MC0_PORT+13) & 0x80) == 0)
240 mad_write(MC9_PORT, 0x02);
241 mad_write(MC9_PORT, 0x00);
243 if ((mad_read(MC0_PORT+13) & 0x80) == 0)
246 /* Power off, and on again */
247 mad_write(MC9_PORT, 0xc2);
248 mad_write(MC9_PORT, 0xc0);
250 if ((mad_read(MC0_PORT+13) & 0x80) == 0)
254 /* Force off PnP mode. This is not recommended because
255 * the PnP bios will not recognize the chip on the next
256 * warm boot and may assignd different resources to other
259 mad_write(MC0_PORT+17, 0x04);
264 static int __init detect_mad16(void)
266 unsigned char tmp, tmp2, bit;
270 * Check that reading a register doesn't return bus float (0xff)
271 * when the card is accessed using password. This may fail in case
272 * the card is in low power mode. Normally at least the power saving
273 * mode bit should be 0.
276 if ((tmp = mad_read(MC1_PORT)) == 0xff)
278 DDB(printk("MC1_PORT returned 0xff\n"));
281 for (i = 0xf8d; i <= 0xf98; i++)
283 DDB(printk("Port %0x (init value) = %0x\n", i, mad_read(i))) else
284 DDB(printk("Port %0x (init value) = %0x\n", i-0x80, mad_read(i)));
286 if (board_type == C930)
287 return detect_c930();
290 * Now check that the gate is closed on first I/O after writing
291 * the password. (This is how a MAD16 compatible card works).
294 if ((tmp2 = inb(MC1_PORT)) == tmp) /* It didn't close */
296 DDB(printk("MC1_PORT didn't close after read (0x%02x)\n", tmp2));
300 bit = (c924pnp) ? 0x20 : 0x80;
301 port = (c924pnp) ? MC2_PORT : MC1_PORT;
303 tmp = mad_read(port);
304 mad_write(port, tmp ^ bit); /* Toggle a bit */
305 if ((tmp2 = mad_read(port)) != (tmp ^ bit)) /* Compare the bit */
307 mad_write(port, tmp); /* Restore */
308 DDB(printk("Bit revert test failed (0x%02x, 0x%02x)\n", tmp, tmp2));
311 mad_write(port, tmp); /* Restore */
312 return 1; /* Bingo */
315 static int __init wss_init(struct address_info *hw_config)
320 * Verify the WSS parameters
323 if (check_region(hw_config->io_base, 8))
325 printk(KERN_ERR "MSS: I/O port conflict\n");
328 if (!ad1848_detect(hw_config->io_base + 4, &ad_flags, mad16_osp))
331 * Check if the IO port returns valid signature. The original MS Sound
332 * system returns 0x04 while some cards (AudioTrix Pro for example)
336 if ((inb(hw_config->io_base + 3) & 0x3f) != 0x04 &&
337 (inb(hw_config->io_base + 3) & 0x3f) != 0x00)
339 DDB(printk("No MSS signature detected on port 0x%x (0x%x)\n", hw_config->io_base, inb(hw_config->io_base + 3)));
342 if (hw_config->irq > 11)
344 printk(KERN_ERR "MSS: Bad IRQ %d\n", hw_config->irq);
347 if (hw_config->dma != 0 && hw_config->dma != 1 && hw_config->dma != 3)
349 printk(KERN_ERR "MSS: Bad DMA %d\n", hw_config->dma);
353 * Check that DMA0 is not in use with a 8 bit board.
356 if (hw_config->dma == 0 && inb(hw_config->io_base + 3) & 0x80)
358 printk("MSS: Can't use DMA0 with a 8 bit card/slot\n");
361 if (hw_config->irq > 7 && hw_config->irq != 9 && inb(hw_config->io_base + 3) & 0x80)
362 printk(KERN_ERR "MSS: Can't use IRQ%d with a 8 bit card/slot\n", hw_config->irq);
366 static int __init init_c930(struct address_info *hw_config)
368 unsigned char cfg = 0;
370 cfg |= (0x0f & mad16_conf);
374 /* Bit 0 has reversd meaning. Bits 1 and 2 sese
376 Support only IDE cdrom. IDE port programmed
378 cfg = (cfg & 0x09) ^ 0x07;
381 switch (hw_config->io_base)
396 printk(KERN_ERR "MAD16: Invalid codec port %x\n", hw_config->io_base);
399 mad_write(MC1_PORT, cfg);
401 /* MC2 is CD configuration. Don't touch it. */
403 mad_write(MC3_PORT, 0); /* Disable SB mode IRQ and DMA */
405 /* bit 2 of MC4 reverses it's meaning between the C930
407 cfg = c931_detected ? 0x04 : 0x00;
409 if(mad16_cdsel & 0x20)
410 mad_write(MC4_PORT, 0x62|cfg); /* opl4 */
412 mad_write(MC4_PORT, 0x52|cfg); /* opl3 */
414 mad_write(MC5_PORT, 0x3C); /* Init it into mode2 */
415 mad_write(MC6_PORT, 0x02); /* Enable WSS, Disable MPU and SB */
416 mad_write(MC7_PORT, 0xCB);
417 mad_write(MC10_PORT, 0x11);
419 return wss_init(hw_config);
422 static int __init chip_detect(void)
427 * Then try to detect with the old password
431 DDB(printk("Detect using password = 0xE5\n"));
433 if (detect_mad16()) {
439 DDB(printk("Detect using password = 0xE2\n"));
445 if (((model = mad_read(MC3_PORT)) & 0x03) == 0x03) {
446 DDB(printk("mad16.c: Mozart detected\n"));
449 DDB(printk("mad16.c: 82C928 detected???\n"));
457 DDB(printk("Detect using password = 0xE3\n"));
461 DDB(printk("mad16.c: 82C929 detected\n"));
465 if (inb(PASSWD_REG) != 0xff)
469 * First relocate MC# registers to 0xe0e/0xe0f, disable password
472 outb((0xE4), PASSWD_REG);
473 outb((0x80), PASSWD_REG);
477 DDB(printk("Detect using password = 0xE4\n"));
479 for (i = 0xf8d; i <= 0xf93; i++)
480 DDB(printk("port %03x = %02x\n", i, mad_read(i)));
483 DDB(printk("mad16.c: 82C930 detected\n"));
487 /* The C931 has the password reg at F8D */
490 DDB(printk("Detect using password = 0xE4 for C931\n"));
492 if (detect_mad16()) {
498 DDB(printk("Detect using password = 0xE5 (again), port offset -0x80\n"));
499 if (detect_mad16()) {
500 DDB(printk("mad16.c: 82C924 PnP detected\n"));
509 static int __init probe_mad16(struct address_info *hw_config)
512 static int valid_ports[] =
514 0x530, 0xe80, 0xf40, 0x604
517 unsigned char cs4231_mode = 0;
521 if (already_initialized)
524 mad16_osp = hw_config->osp;
527 * Check that all ports return 0xff (bus float) when no password
528 * is written to the password register.
531 DDB(printk("--- Detecting MAD16 / Mozart ---\n"));
535 if (board_type == C930)
536 return init_c930(hw_config);
539 for (i = 0xf8d; i <= 0xf93; i++)
541 DDB(printk("port %03x = %02x\n", i, mad_read(i))) else
542 DDB(printk("port %03x = %02x\n", i-0x80, mad_read(i)));
545 * Set the WSS address
548 tmp = (mad_read(MC1_PORT) & 0x0f) | 0x80; /* Enable WSS, Disable SB */
550 for (i = 0; i < 5; i++)
552 if (i > 3) /* Not a valid port */
554 printk(KERN_ERR "MAD16/Mozart: Bad WSS base address 0x%x\n", hw_config->io_base);
557 if (valid_ports[i] == hw_config->io_base)
559 tmp |= i << 4; /* WSS port select bits */
565 * Set optional CD-ROM and joystick settings.
569 tmp |= (mad16_conf & 0x0f); /* CD-ROM and joystick bits */
570 mad_write(MC1_PORT, tmp);
573 mad_write(MC2_PORT, tmp);
574 mad_write(MC3_PORT, 0xf0); /* Disable SB */
576 if (board_type == C924) /* Specific C924 init values */
578 mad_write(MC4_PORT, 0xA0);
579 mad_write(MC5_PORT, 0x05);
580 mad_write(MC6_PORT, 0x03);
582 if (!ad1848_detect(hw_config->io_base + 4, &ad_flags, mad16_osp))
585 if (ad_flags & (AD_F_CS4231 | AD_F_CS4248))
586 cs4231_mode = 0x02; /* CS4248/CS4231 sync delay switch */
588 if (board_type == C929)
590 mad_write(MC4_PORT, 0xa2);
591 mad_write(MC5_PORT, 0xA5 | cs4231_mode);
592 mad_write(MC6_PORT, 0x03); /* Disable MPU401 */
596 mad_write(MC4_PORT, 0x02);
597 mad_write(MC5_PORT, 0x30 | cs4231_mode);
600 for (i = 0xf8d; i <= 0xf93; i++) if (!c924pnp)
601 DDB(printk("port %03x after init = %02x\n", i, mad_read(i))) else
602 DDB(printk("port %03x after init = %02x\n", i-0x80, mad_read(i)));
608 static void __init attach_mad16(struct address_info *hw_config)
611 static signed char interrupt_bits[12] = {
612 -1, -1, -1, -1, -1, -1, -1, 0x08, -1, 0x10, 0x18, 0x20
616 static char dma_bits[4] = {
620 int config_port = hw_config->io_base + 0, version_port = hw_config->io_base + 3;
621 int ad_flags = 0, dma = hw_config->dma, dma2 = hw_config->dma2;
622 unsigned char dma2_bit = 0;
624 already_initialized = 1;
626 if (!ad1848_detect(hw_config->io_base + 4, &ad_flags, mad16_osp))
630 * Set the IRQ and DMA addresses.
633 if (board_type == C930 || c924pnp)
634 interrupt_bits[5] = 0x28; /* Also IRQ5 is possible on C930 */
636 bits = interrupt_bits[hw_config->irq];
640 outb((bits | 0x40), config_port);
641 if ((inb(version_port) & 0x40) == 0)
642 printk(KERN_ERR "[IRQ Conflict?]\n");
645 * Handle the capture DMA channel
648 if (ad_flags & AD_F_CS4231 && dma2 != -1 && dma2 != dma)
650 if (!((dma == 0 && dma2 == 1) ||
651 (dma == 1 && dma2 == 0) ||
652 (dma == 3 && dma2 == 0)))
653 { /* Unsupported combination. Try to swap channels */
659 if ((dma == 0 && dma2 == 1) || (dma == 1 && dma2 == 0) ||
660 (dma == 3 && dma2 == 0))
662 dma2_bit = 0x04; /* Enable capture DMA */
666 printk("MAD16: Invalid capture DMA\n");
672 outb((bits | dma_bits[dma] | dma2_bit), config_port); /* Write IRQ+DMA setup */
674 hw_config->slots[0] = ad1848_init("mad16 WSS", hw_config->io_base + 4,
680 request_region(hw_config->io_base, 4, "mad16 WSS config");
683 static int __init probe_mad16_mpu(struct address_info *hw_config)
685 static int mpu_attached = 0;
688 if (!already_initialized) /* The MSS port must be initialized first */
691 if (mpu_attached) /* Don't let them call this twice */
695 if (board_type < C929) /* Early chip. No MPU support. Just SB MIDI */
698 #ifdef CONFIG_MAD16_OLDCARD
700 tmp = mad_read(MC3_PORT);
703 * MAD16 SB base is defined by the WSS base. It cannot be changed
705 * Ignore configured I/O base. Use the active setting.
708 if (mad_read(MC1_PORT) & 0x20)
709 hw_config->io_base = 0x240;
711 hw_config->io_base = 0x220;
713 switch (hw_config->irq)
716 tmp = (tmp & 0x3f) | 0x80;
722 tmp = (tmp & 0x3f) | 0x40;
725 printk(KERN_ERR "mad16/Mozart: Invalid MIDI IRQ\n");
729 mad_write(MC3_PORT, tmp | 0x04);
730 hw_config->driver_use_1 = SB_MIDI_ONLY;
731 if (!sb_dsp_detect(hw_config, 0, 0, NULL))
734 if (mad_read(MC1_PORT) & 0x20)
735 hw_config->io_base = 0x240;
737 hw_config->io_base = 0x220;
739 hw_config->name = "Mad16/Mozart";
740 sb_dsp_init(hw_config, THIS_MODULE);
743 /* assuming all later Mozart cards are identified as
744 * either 82C928 or Mozart. If so, following code attempts
745 * to set MPU register. TODO - add probing
748 tmp = mad_read(MC8_PORT);
750 switch (hw_config->irq)
768 printk(KERN_ERR "mad16/MOZART: invalid mpu_irq\n");
772 switch (hw_config->io_base)
787 printk(KERN_ERR "mad16/MOZART: invalid mpu_io\n");
791 mad_write(MC8_PORT, tmp); /* write MPU port parameters */
795 tmp = mad_read(MC6_PORT) & 0x83;
796 tmp |= 0x80; /* MPU-401 enable */
798 /* Set the MPU base bits */
800 switch (hw_config->io_base)
815 printk(KERN_ERR "MAD16: Invalid MIDI port 0x%x\n", hw_config->io_base);
819 /* Set the MPU IRQ bits */
821 switch (hw_config->irq)
836 printk(KERN_ERR "MAD16: Invalid MIDI IRQ %d\n", hw_config->irq);
840 mad_write(MC6_PORT, tmp); /* Write MPU401 config */
842 #ifndef CONFIG_MAD16_OLDCARD
845 hw_config->driver_use_1 = SB_MIDI_ONLY;
846 hw_config->name = "Mad16/Mozart";
847 return probe_uart401(hw_config, THIS_MODULE);
850 static void __exit unload_mad16(struct address_info *hw_config)
852 ad1848_unload(hw_config->io_base + 4,
856 release_region(hw_config->io_base, 4);
857 sound_unload_audiodev(hw_config->slots[0]);
860 static void __exit unload_mad16_mpu(struct address_info *hw_config)
862 #ifdef CONFIG_MAD16_OLDCARD
863 if (board_type < C929) /* Early chip. No MPU support. Just SB MIDI */
865 sb_dsp_unload(hw_config, 0);
870 unload_uart401(hw_config);
873 static struct address_info cfg;
874 static struct address_info cfg_mpu;
876 static int found_mpu;
878 static int __initdata mpu_io = 0;
879 static int __initdata mpu_irq = 0;
880 static int __initdata io = -1;
881 static int __initdata dma = -1;
882 static int __initdata dma16 = -1; /* Set this for modules that need it */
883 static int __initdata irq = -1;
884 static int __initdata cdtype = 0;
885 static int __initdata cdirq = 0;
886 static int __initdata cdport = 0x340;
887 static int __initdata cddma = -1;
888 static int __initdata opl4 = 0;
889 static int __initdata joystick = 0;
891 MODULE_PARM(mpu_io, "i");
892 MODULE_PARM(mpu_irq, "i");
894 MODULE_PARM(dma,"i");
895 MODULE_PARM(dma16,"i");
896 MODULE_PARM(irq,"i");
897 MODULE_PARM(cdtype,"i");
898 MODULE_PARM(cdirq,"i");
899 MODULE_PARM(cdport,"i");
900 MODULE_PARM(cddma,"i");
901 MODULE_PARM(opl4,"i");
902 MODULE_PARM(joystick,"i");
903 MODULE_PARM(debug,"i");
905 static int __initdata dma_map[2][8] =
907 {0x03, -1, -1, -1, -1, 0x00, 0x01, 0x02},
908 {0x03, -1, 0x01, 0x00, -1, -1, -1, -1}
911 static int __initdata irq_map[16] =
915 -1, 0x10, 0x14, 0x18,
919 static int __init init_mad16(void)
923 printk(KERN_INFO "MAD16 audio driver Copyright (C) by Hannu Savolainen 1993-1996\n");
925 printk(KERN_INFO "CDROM ");
933 printk("Sony CDU31A");
935 if(cddma == -1) cddma = 3;
940 if(cddma == -1) cddma = 5;
943 printk("Panasonic Lasermate");
945 if(cddma == -1) cddma = 3;
948 printk("Secondary IDE");
950 if(cddma == -1) cddma = 5;
953 printk("Primary IDE");
955 if(cddma == -1) cddma = 5;
959 printk(KERN_ERR "Invalid CDROM type\n");
964 * Build the config words
967 mad16_conf = (joystick ^ 1) | cdtype;
973 if (cddma > 7 || cddma < 0 || dma_map[dmatype][cddma] == -1)
976 printk(KERN_ERR "Invalid CDROM DMA\n");
980 printk(", DMA %d", cddma);
986 else if (cdirq < 0 || cdirq > 15 || irq_map[cdirq] == -1)
988 printk(", invalid IRQ (disabling)");
991 else printk(", IRQ %d", cdirq);
993 mad16_cdsel |= dma_map[dmatype][cddma];
1000 mad16_cdsel |= 0x00;
1003 mad16_cdsel |= 0x40;
1006 mad16_cdsel |= 0x80;
1009 mad16_cdsel |= 0xC0;
1012 printk(KERN_ERR "Unknown CDROM I/O base %d\n", cdport);
1016 mad16_cdsel |= irq_map[cdirq];
1026 if (cfg.io_base == -1 || cfg.dma == -1 || cfg.irq == -1) {
1027 printk(KERN_ERR "I/O, DMA and irq are mandatory\n");
1031 if (!probe_mad16(&cfg))
1034 cfg_mpu.io_base = mpu_io;
1035 cfg_mpu.irq = mpu_irq;
1039 found_mpu = probe_mad16_mpu(&cfg_mpu);
1041 if (joystick == 1) {
1042 /* register gameport */
1043 if (!request_region(0x201, 1, "mad16 gameport"))
1044 printk(KERN_ERR "mad16: gameport address 0x201 already in use\n");
1046 printk(KERN_ERR "mad16: gameport enabled at 0x201\n");
1047 gameport.io = 0x201;
1048 gameport_register_port(&gameport);
1051 else printk(KERN_ERR "mad16: gameport disabled.\n");
1055 static void __exit cleanup_mad16(void)
1058 unload_mad16_mpu(&cfg_mpu);
1060 /* the gameport was initialized so we must free it up */
1061 gameport_unregister_port(&gameport);
1063 release_region(0x201, 1);
1068 module_init(init_mad16);
1069 module_exit(cleanup_mad16);
1072 static int __init setup_mad16(char *str)
1077 str = get_options(str, ARRAY_SIZE(ints), ints);
1090 __setup("mad16=", setup_mad16);
1092 MODULE_LICENSE("GPL");