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 compatible 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/init.h>
43 #include <linux/module.h>
44 #include <linux/gameport.h>
45 #include <linux/spinlock.h>
46 #include "sound_config.h"
52 #if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
53 #define SUPPORT_JOYSTICK 1
56 static int mad16_conf;
57 static int mad16_cdsel;
58 static DEFINE_SPINLOCK(lock);
69 * The MAD16 occupies I/O ports 0xf8d to 0xf93 (fixed locations).
70 * All ports are inactive by default. They can be activated by
71 * writing 0xE2 or 0xE3 to the password register. The password is valid
72 * only until the next I/O read or write.
74 * 82C930 uses 0xE4 as the password and indirect addressing to access
75 * the config registers.
78 #define MC0_PORT 0xf8c /* Dummy port */
79 #define MC1_PORT 0xf8d /* SB address, CD-ROM interface type, joystick */
80 #define MC2_PORT 0xf8e /* CD-ROM address, IRQ, DMA, plus OPL4 bit */
81 #define MC3_PORT 0xf8f
82 #define PASSWD_REG 0xf8f
83 #define MC4_PORT 0xf90
84 #define MC5_PORT 0xf91
85 #define MC6_PORT 0xf92
86 #define MC7_PORT 0xf93
87 #define MC8_PORT 0xf94
88 #define MC9_PORT 0xf95
89 #define MC10_PORT 0xf96
90 #define MC11_PORT 0xf97
91 #define MC12_PORT 0xf98
93 static int board_type = C928;
95 static int *mad16_osp;
96 static int c931_detected; /* minor differences from C930 */
97 static char c924pnp; /* " " " C924 */
98 static int debug; /* debugging output */
103 #define DDB(x) do {if (debug) x;} while (0)
105 static unsigned char mad_read(int port)
110 spin_lock_irqsave(&lock,flags);
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 spin_unlock_irqrestore(&lock,flags);
150 static void mad_write(int port, int value)
154 spin_lock_irqsave(&lock,flags);
156 switch (board_type) /* Output password */
160 outb((0xE2), PASSWD_REG);
164 outb((0xE3), PASSWD_REG);
168 /* outb(( 0xE4), PASSWD_REG); */
173 outb((0xE5), PASSWD_REG); else
174 outb((0xE5), PASSWD_REG - 0x80);
178 if (board_type == C930)
180 outb((port - MC0_PORT), 0xe0e); /* Write to index reg */
181 outb(((unsigned char) (value & 0xff)), 0xe0f);
185 outb(((unsigned char) (value & 0xff)), port); else
186 outb(((unsigned char) (value & 0xff)), port-0x80);
187 spin_unlock_irqrestore(&lock,flags);
190 static int __init detect_c930(void)
192 unsigned char tmp = mad_read(MC1_PORT);
194 if ((tmp & 0x06) != 0x06)
196 DDB(printk("Wrong C930 signature (%x)\n", tmp));
199 mad_write(MC1_PORT, 0);
201 if (mad_read(MC1_PORT) != 0x06)
203 DDB(printk("Wrong C930 signature2 (%x)\n", tmp));
206 mad_write(MC1_PORT, tmp); /* Restore bits */
208 mad_write(MC7_PORT, 0);
209 if ((tmp = mad_read(MC7_PORT)) != 0)
211 DDB(printk("MC7 not writable (%x)\n", tmp));
214 mad_write(MC7_PORT, 0xcb);
215 if ((tmp = mad_read(MC7_PORT)) != 0xcb)
217 DDB(printk("MC7 not writable2 (%x)\n", tmp));
221 tmp = mad_read(MC0_PORT+18);
222 if (tmp == 0xff || tmp == 0x00)
224 /* We probably have a C931 */
225 DDB(printk("Detected C931 config=0x%02x\n", tmp));
229 * We cannot configure the chip if it is in PnP mode.
230 * If we have a CSN assigned (bit 8 in MC13) we first try
231 * a software reset, then a software power off, finally
232 * Clearing PnP mode. The last option is not
235 if ((mad_read(MC0_PORT+13) & 0x80) == 0)
239 mad_write(MC9_PORT, 0x02);
240 mad_write(MC9_PORT, 0x00);
242 if ((mad_read(MC0_PORT+13) & 0x80) == 0)
245 /* Power off, and on again */
246 mad_write(MC9_PORT, 0xc2);
247 mad_write(MC9_PORT, 0xc0);
249 if ((mad_read(MC0_PORT+13) & 0x80) == 0)
253 /* Force off PnP mode. This is not recommended because
254 * the PnP bios will not recognize the chip on the next
255 * warm boot and may assignd different resources to other
258 mad_write(MC0_PORT+17, 0x04);
263 static int __init detect_mad16(void)
265 unsigned char tmp, tmp2, bit;
269 * Check that reading a register doesn't return bus float (0xff)
270 * when the card is accessed using password. This may fail in case
271 * the card is in low power mode. Normally at least the power saving
272 * mode bit should be 0.
275 if ((tmp = mad_read(MC1_PORT)) == 0xff)
277 DDB(printk("MC1_PORT returned 0xff\n"));
280 for (i = 0xf8d; i <= 0xf98; i++)
282 DDB(printk("Port %0x (init value) = %0x\n", i, mad_read(i)));
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)
318 * Check if the IO port returns valid signature. The original MS Sound
319 * system returns 0x04 while some cards (AudioTrix Pro for example)
323 if ((inb(hw_config->io_base + 3) & 0x3f) != 0x04 &&
324 (inb(hw_config->io_base + 3) & 0x3f) != 0x00)
326 DDB(printk("No MSS signature detected on port 0x%x (0x%x)\n", hw_config->io_base, inb(hw_config->io_base + 3)));
330 * Check that DMA0 is not in use with a 8 bit board.
332 if (hw_config->dma == 0 && inb(hw_config->io_base + 3) & 0x80)
334 printk("MSS: Can't use DMA0 with a 8 bit card/slot\n");
337 if (hw_config->irq > 9 && inb(hw_config->io_base + 3) & 0x80)
338 printk(KERN_ERR "MSS: Can't use IRQ%d with a 8 bit card/slot\n", hw_config->irq);
342 static void __init init_c930(struct address_info *hw_config, int base)
344 unsigned char cfg = 0;
346 cfg |= (0x0f & mad16_conf);
350 /* Bit 0 has reversd meaning. Bits 1 and 2 sese
352 Support only IDE cdrom. IDE port programmed
354 cfg = (cfg & 0x09) ^ 0x07;
357 mad_write(MC1_PORT, cfg);
359 /* MC2 is CD configuration. Don't touch it. */
361 mad_write(MC3_PORT, 0); /* Disable SB mode IRQ and DMA */
363 /* bit 2 of MC4 reverses it's meaning between the C930
365 cfg = c931_detected ? 0x04 : 0x00;
367 if(mad16_cdsel & 0x20)
368 mad_write(MC4_PORT, 0x62|cfg); /* opl4 */
370 mad_write(MC4_PORT, 0x52|cfg); /* opl3 */
372 mad_write(MC5_PORT, 0x3C); /* Init it into mode2 */
373 mad_write(MC6_PORT, 0x02); /* Enable WSS, Disable MPU and SB */
374 mad_write(MC7_PORT, 0xCB);
375 mad_write(MC10_PORT, 0x11);
378 static int __init chip_detect(void)
383 * Then try to detect with the old password
387 DDB(printk("Detect using password = 0xE5\n"));
389 if (detect_mad16()) {
395 DDB(printk("Detect using password = 0xE2\n"));
401 if (((model = mad_read(MC3_PORT)) & 0x03) == 0x03) {
402 DDB(printk("mad16.c: Mozart detected\n"));
405 DDB(printk("mad16.c: 82C928 detected???\n"));
413 DDB(printk("Detect using password = 0xE3\n"));
417 DDB(printk("mad16.c: 82C929 detected\n"));
421 if (inb(PASSWD_REG) != 0xff)
425 * First relocate MC# registers to 0xe0e/0xe0f, disable password
428 outb((0xE4), PASSWD_REG);
429 outb((0x80), PASSWD_REG);
433 DDB(printk("Detect using password = 0xE4\n"));
435 for (i = 0xf8d; i <= 0xf93; i++)
436 DDB(printk("port %03x = %02x\n", i, mad_read(i)));
439 DDB(printk("mad16.c: 82C930 detected\n"));
443 /* The C931 has the password reg at F8D */
446 DDB(printk("Detect using password = 0xE4 for C931\n"));
448 if (detect_mad16()) {
454 DDB(printk("Detect using password = 0xE5 (again), port offset -0x80\n"));
455 if (detect_mad16()) {
456 DDB(printk("mad16.c: 82C924 PnP detected\n"));
465 static int __init probe_mad16(struct address_info *hw_config)
469 unsigned char cs4231_mode = 0;
475 static char dma_bits[4] = {
479 int config_port = hw_config->io_base + 0, version_port = hw_config->io_base + 3;
480 int dma = hw_config->dma, dma2 = hw_config->dma2;
481 unsigned char dma2_bit = 0;
483 struct resource *ports;
485 mad16_osp = hw_config->osp;
487 switch (hw_config->io_base) {
501 printk(KERN_ERR "MAD16/Mozart: Bad WSS base address 0x%x\n", hw_config->io_base);
505 if (dma != 0 && dma != 1 && dma != 3) {
506 printk(KERN_ERR "MSS: Bad DMA %d\n", dma);
511 * Check that all ports return 0xff (bus float) when no password
512 * is written to the password register.
515 DDB(printk("--- Detecting MAD16 / Mozart ---\n"));
519 switch (hw_config->irq) {
532 case 5: /* Also IRQ5 is possible on C930 */
533 if (board_type == C930 || c924pnp) {
538 printk(KERN_ERR "MAD16/Mozart: Bad IRQ %d\n", hw_config->irq);
542 ports = request_region(hw_config->io_base + 4, 4, "ad1848");
544 printk(KERN_ERR "MSS: I/O port conflict\n");
547 if (!request_region(hw_config->io_base, 4, "mad16 WSS config")) {
548 release_region(hw_config->io_base + 4, 4);
549 printk(KERN_ERR "MSS: I/O port conflict\n");
553 if (board_type == C930) {
554 init_c930(hw_config, base);
558 for (i = 0xf8d; i <= 0xf93; i++) {
560 DDB(printk("port %03x = %02x\n", i, mad_read(i)));
562 DDB(printk("port %03x = %02x\n", i-0x80, mad_read(i)));
566 * Set the WSS address
569 tmp = (mad_read(MC1_PORT) & 0x0f) | 0x80; /* Enable WSS, Disable SB */
570 tmp |= base << 4; /* WSS port select bits */
573 * Set optional CD-ROM and joystick settings.
577 tmp |= (mad16_conf & 0x0f); /* CD-ROM and joystick bits */
578 mad_write(MC1_PORT, tmp);
581 mad_write(MC2_PORT, tmp);
582 mad_write(MC3_PORT, 0xf0); /* Disable SB */
584 if (board_type == C924) /* Specific C924 init values */
586 mad_write(MC4_PORT, 0xA0);
587 mad_write(MC5_PORT, 0x05);
588 mad_write(MC6_PORT, 0x03);
590 if (!ad1848_detect(ports, &ad_flags, mad16_osp))
593 if (ad_flags & (AD_F_CS4231 | AD_F_CS4248))
594 cs4231_mode = 0x02; /* CS4248/CS4231 sync delay switch */
596 if (board_type == C929)
598 mad_write(MC4_PORT, 0xa2);
599 mad_write(MC5_PORT, 0xA5 | cs4231_mode);
600 mad_write(MC6_PORT, 0x03); /* Disable MPU401 */
604 mad_write(MC4_PORT, 0x02);
605 mad_write(MC5_PORT, 0x30 | cs4231_mode);
608 for (i = 0xf8d; i <= 0xf93; i++) {
610 DDB(printk("port %03x after init = %02x\n", i, mad_read(i)));
612 DDB(printk("port %03x after init = %02x\n", i-0x80, mad_read(i)));
617 if (!ad1848_detect(ports, &ad_flags, mad16_osp))
620 if (!wss_init(hw_config))
624 * Set the IRQ and DMA addresses.
627 outb((bits | 0x40), config_port);
628 if ((inb(version_port) & 0x40) == 0)
629 printk(KERN_ERR "[IRQ Conflict?]\n");
632 * Handle the capture DMA channel
635 if (ad_flags & AD_F_CS4231 && dma2 != -1 && dma2 != dma)
637 if (!((dma == 0 && dma2 == 1) ||
638 (dma == 1 && dma2 == 0) ||
639 (dma == 3 && dma2 == 0)))
640 { /* Unsupported combination. Try to swap channels */
646 if ((dma == 0 && dma2 == 1) || (dma == 1 && dma2 == 0) ||
647 (dma == 3 && dma2 == 0))
649 dma2_bit = 0x04; /* Enable capture DMA */
653 printk("MAD16: Invalid capture DMA\n");
659 outb((bits | dma_bits[dma] | dma2_bit), config_port); /* Write IRQ+DMA setup */
661 hw_config->slots[0] = ad1848_init("mad16 WSS", ports,
670 release_region(hw_config->io_base + 4, 4);
671 release_region(hw_config->io_base, 4);
675 static int __init probe_mad16_mpu(struct address_info *hw_config)
679 if (board_type < C929) /* Early chip. No MPU support. Just SB MIDI */
682 #ifdef CONFIG_MAD16_OLDCARD
684 tmp = mad_read(MC3_PORT);
687 * MAD16 SB base is defined by the WSS base. It cannot be changed
689 * Ignore configured I/O base. Use the active setting.
692 if (mad_read(MC1_PORT) & 0x20)
693 hw_config->io_base = 0x240;
695 hw_config->io_base = 0x220;
697 switch (hw_config->irq)
700 tmp = (tmp & 0x3f) | 0x80;
706 tmp = (tmp & 0x3f) | 0x40;
709 printk(KERN_ERR "mad16/Mozart: Invalid MIDI IRQ\n");
713 mad_write(MC3_PORT, tmp | 0x04);
714 hw_config->driver_use_1 = SB_MIDI_ONLY;
715 if (!request_region(hw_config->io_base, 16, "soundblaster"))
717 if (!sb_dsp_detect(hw_config, 0, 0, NULL)) {
718 release_region(hw_config->io_base, 16);
722 if (mad_read(MC1_PORT) & 0x20)
723 hw_config->io_base = 0x240;
725 hw_config->io_base = 0x220;
727 hw_config->name = "Mad16/Mozart";
728 sb_dsp_init(hw_config, THIS_MODULE);
731 /* assuming all later Mozart cards are identified as
732 * either 82C928 or Mozart. If so, following code attempts
733 * to set MPU register. TODO - add probing
736 tmp = mad_read(MC8_PORT);
738 switch (hw_config->irq)
756 printk(KERN_ERR "mad16/MOZART: invalid mpu_irq\n");
760 switch (hw_config->io_base)
775 printk(KERN_ERR "mad16/MOZART: invalid mpu_io\n");
779 mad_write(MC8_PORT, tmp); /* write MPU port parameters */
783 tmp = mad_read(MC6_PORT) & 0x83;
784 tmp |= 0x80; /* MPU-401 enable */
786 /* Set the MPU base bits */
788 switch (hw_config->io_base)
803 printk(KERN_ERR "MAD16: Invalid MIDI port 0x%x\n", hw_config->io_base);
807 /* Set the MPU IRQ bits */
809 switch (hw_config->irq)
824 printk(KERN_ERR "MAD16: Invalid MIDI IRQ %d\n", hw_config->irq);
828 mad_write(MC6_PORT, tmp); /* Write MPU401 config */
830 #ifndef CONFIG_MAD16_OLDCARD
833 hw_config->driver_use_1 = SB_MIDI_ONLY;
834 hw_config->name = "Mad16/Mozart";
835 return probe_uart401(hw_config, THIS_MODULE);
838 static void __exit unload_mad16(struct address_info *hw_config)
840 ad1848_unload(hw_config->io_base + 4,
844 release_region(hw_config->io_base, 4);
845 sound_unload_audiodev(hw_config->slots[0]);
848 static void __exit unload_mad16_mpu(struct address_info *hw_config)
850 #ifdef CONFIG_MAD16_OLDCARD
851 if (board_type < C929) /* Early chip. No MPU support. Just SB MIDI */
853 sb_dsp_unload(hw_config, 0);
858 unload_uart401(hw_config);
861 static struct address_info cfg;
862 static struct address_info cfg_mpu;
864 static int found_mpu;
866 static int __initdata mpu_io = 0;
867 static int __initdata mpu_irq = 0;
868 static int __initdata io = -1;
869 static int __initdata dma = -1;
870 static int __initdata dma16 = -1; /* Set this for modules that need it */
871 static int __initdata irq = -1;
872 static int __initdata cdtype = 0;
873 static int __initdata cdirq = 0;
874 static int __initdata cdport = 0x340;
875 static int __initdata cddma = -1;
876 static int __initdata opl4 = 0;
877 static int __initdata joystick = 0;
879 module_param(mpu_io, int, 0);
880 module_param(mpu_irq, int, 0);
881 module_param(io, int, 0);
882 module_param(dma, int, 0);
883 module_param(dma16, int, 0);
884 module_param(irq, int, 0);
885 module_param(cdtype, int, 0);
886 module_param(cdirq, int, 0);
887 module_param(cdport, int, 0);
888 module_param(cddma, int, 0);
889 module_param(opl4, int, 0);
890 module_param(joystick, bool, 0);
891 module_param(debug, bool, 0644);
893 static int __initdata dma_map[2][8] =
895 {0x03, -1, -1, -1, -1, 0x00, 0x01, 0x02},
896 {0x03, -1, 0x01, 0x00, -1, -1, -1, -1}
899 static int __initdata irq_map[16] =
903 -1, 0x10, 0x14, 0x18,
907 #ifdef SUPPORT_JOYSTICK
909 static struct gameport *gameport;
911 static int __devinit mad16_register_gameport(int io_port)
913 if (!request_region(io_port, 1, "mad16 gameport")) {
914 printk(KERN_ERR "mad16: gameport address 0x%#x already in use\n", io_port);
918 gameport = gameport_allocate_port();
920 printk(KERN_ERR "mad16: can not allocate memory for gameport\n");
921 release_region(io_port, 1);
925 gameport_set_name(gameport, "MAD16 Gameport");
926 gameport_set_phys(gameport, "isa%04x/gameport0", io_port);
927 gameport->io = io_port;
929 gameport_register_port(gameport);
934 static inline void mad16_unregister_gameport(void)
937 /* the gameport was initialized so we must free it up */
938 gameport_unregister_port(gameport);
940 release_region(0x201, 1);
944 static inline int mad16_register_gameport(int io_port) { return -ENOSYS; }
945 static inline void mad16_unregister_gameport(void) { }
948 static int __devinit init_mad16(void)
952 printk(KERN_INFO "MAD16 audio driver Copyright (C) by Hannu Savolainen 1993-1996\n");
954 printk(KERN_INFO "CDROM ");
962 printk("Sony CDU31A");
964 if(cddma == -1) cddma = 3;
969 if(cddma == -1) cddma = 5;
972 printk("Panasonic Lasermate");
974 if(cddma == -1) cddma = 3;
977 printk("Secondary IDE");
979 if(cddma == -1) cddma = 5;
982 printk("Primary IDE");
984 if(cddma == -1) cddma = 5;
988 printk(KERN_ERR "Invalid CDROM type\n");
993 * Build the config words
996 mad16_conf = (joystick ^ 1) | cdtype;
1002 if (cddma > 7 || cddma < 0 || dma_map[dmatype][cddma] == -1)
1005 printk(KERN_ERR "Invalid CDROM DMA\n");
1009 printk(", DMA %d", cddma);
1015 else if (cdirq < 0 || cdirq > 15 || irq_map[cdirq] == -1)
1017 printk(", invalid IRQ (disabling)");
1020 else printk(", IRQ %d", cdirq);
1022 mad16_cdsel |= dma_map[dmatype][cddma];
1029 mad16_cdsel |= 0x00;
1032 mad16_cdsel |= 0x40;
1035 mad16_cdsel |= 0x80;
1038 mad16_cdsel |= 0xC0;
1041 printk(KERN_ERR "Unknown CDROM I/O base %d\n", cdport);
1045 mad16_cdsel |= irq_map[cdirq];
1055 if (cfg.io_base == -1 || cfg.dma == -1 || cfg.irq == -1) {
1056 printk(KERN_ERR "I/O, DMA and irq are mandatory\n");
1060 if (!request_region(MC0_PORT, 12, "mad16"))
1063 if (!probe_mad16(&cfg)) {
1064 release_region(MC0_PORT, 12);
1068 cfg_mpu.io_base = mpu_io;
1069 cfg_mpu.irq = mpu_irq;
1071 found_mpu = probe_mad16_mpu(&cfg_mpu);
1074 mad16_register_gameport(0x201);
1079 static void __exit cleanup_mad16(void)
1082 unload_mad16_mpu(&cfg_mpu);
1083 mad16_unregister_gameport();
1085 release_region(MC0_PORT, 12);
1088 module_init(init_mad16);
1089 module_exit(cleanup_mad16);
1092 static int __init setup_mad16(char *str)
1097 str = get_options(str, ARRAY_SIZE(ints), ints);
1110 __setup("mad16=", setup_mad16);
1112 MODULE_LICENSE("GPL");