Merge with 2.4.0-test3-pre4.
[linux-2.6/linux-mips.git] / drivers / sound / mad16.c
blob5290b42ed373834b1532116be32d54cc65d22bca
1 /*
2 * Copyright (C) by Hannu Savolainen 1993-1997
4 * mad16.c
6 * Initialization code for OPTi MAD16 compatible audio chips. Including
8 * OPTi 82C928 MAD16 (replaced by C929)
9 * OAK OTI-601D Mozart
10 * OAK OTI-605 Mozart (later version with MPU401 Midi)
11 * OPTi 82C929 MAD16 Pro
12 * OPTi 82C930
13 * OPTi 82C924
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.
23 * NOTE! If you want to set CD-ROM address and/or joystick enable, define
24 * MAD16_CONF in local.h as combination of the following bits:
26 * 0x01 - joystick disabled
28 * CD-ROM type selection (select just one):
29 * 0x00 - none
30 * 0x02 - Sony 31A
31 * 0x04 - Mitsumi
32 * 0x06 - Panasonic (type "LaserMate", not "Sound Blaster")
33 * 0x08 - Secondary IDE (address 0x170)
34 * 0x0a - Primary IDE (address 0x1F0)
36 * For example Mitsumi with joystick disabled = 0x04|0x01 = 0x05
37 * For example LaserMate (for use with sbpcd) plus joystick = 0x06
39 * MAD16_CDSEL:
40 * This defaults to CD I/O 0x340, no IRQ and DMA3
41 * (DMA5 with Mitsumi or IDE). If you like to change these, define
42 * MAD16_CDSEL with the following bits:
44 * CD-ROM port: 0x00=340, 0x40=330, 0x80=360 or 0xc0=320
45 * OPL4 select: 0x20=OPL4, 0x00=OPL3
46 * CD-ROM irq: 0x00=disabled, 0x04=IRQ5, 0x08=IRQ7, 0x0c=IRQ3, 0x10=IRQ9,
47 * 0x14=IRQ10 and 0x18=IRQ11.
49 * CD-ROM DMA (Sony or Panasonic): 0x00=DMA3, 0x01=DMA2, 0x02=DMA1 or 0x03=disabled
50 * or
51 * CD-ROM DMA (Mitsumi or IDE): 0x00=DMA5, 0x01=DMA6, 0x02=DMA7 or 0x03=disabled
53 * For use with sbpcd, address 0x340, set MAD16_CDSEL to 0x03 or 0x23.
55 * Changes
57 * Alan Cox Clean up, added module selections.
59 * A. Wik Added support for Opti924 PnP.
60 * Improved debugging support. 16-May-1998
61 * Fixed bug. 16-Jun-1998
63 * Torsten Duwe Made Opti924 PnP support non-destructive
64 * 23-Dec-1998
66 * Paul Grayson Added support for Midi on later Mozart cards.
67 * 25-Nov-1999
68 * Christoph Hellwig Adapted to module_init/module_exit.
71 #include <linux/config.h>
72 #include <linux/init.h>
73 #include <linux/module.h>
75 #include "sound_config.h"
76 #include "soundmodule.h"
78 #include "ad1848.h"
79 #include "sb.h"
80 #include "mpu401.h"
82 static int mad16_conf;
83 static int mad16_cdsel;
85 static int already_initialized = 0;
87 #define C928 1
88 #define MOZART 2
89 #define C929 3
90 #define C930 4
91 #define C924 5
94 * Registers
96 * The MAD16 occupies I/O ports 0xf8d to 0xf93 (fixed locations).
97 * All ports are inactive by default. They can be activated by
98 * writing 0xE2 or 0xE3 to the password register. The password is valid
99 * only until the next I/O read or write.
101 * 82C930 uses 0xE4 as the password and indirect addressing to access
102 * the config registers.
105 #define MC0_PORT 0xf8c /* Dummy port */
106 #define MC1_PORT 0xf8d /* SB address, CD-ROM interface type, joystick */
107 #define MC2_PORT 0xf8e /* CD-ROM address, IRQ, DMA, plus OPL4 bit */
108 #define MC3_PORT 0xf8f
109 #define PASSWD_REG 0xf8f
110 #define MC4_PORT 0xf90
111 #define MC5_PORT 0xf91
112 #define MC6_PORT 0xf92
113 #define MC7_PORT 0xf93
114 #define MC8_PORT 0xf94
115 #define MC9_PORT 0xf95
116 #define MC10_PORT 0xf96
117 #define MC11_PORT 0xf97
118 #define MC12_PORT 0xf98
120 static int board_type = C928;
122 static int *mad16_osp;
123 static int c931_detected; /* minor differences from C930 */
124 static char c924pnp = 0; /* " " " C924 */
125 static int debug = 0; /* debugging output */
127 #ifdef DDB
128 #undef DDB
129 #endif
130 #define DDB(x) {if (debug) x;}
132 static unsigned char mad_read(int port)
134 unsigned long flags;
135 unsigned char tmp;
137 save_flags(flags);
138 cli();
140 switch (board_type) /* Output password */
142 case C928:
143 case MOZART:
144 outb((0xE2), PASSWD_REG);
145 break;
147 case C929:
148 outb((0xE3), PASSWD_REG);
149 break;
151 case C930:
152 /* outb(( 0xE4), PASSWD_REG); */
153 break;
155 case C924:
156 /* the c924 has its ports relocated by -128 if
157 PnP is enabled -aw */
158 if (!c924pnp)
159 outb((0xE5), PASSWD_REG); else
160 outb((0xE5), PASSWD_REG - 0x80);
161 break;
164 if (board_type == C930)
166 outb((port - MC0_PORT), 0xe0e); /* Write to index reg */
167 tmp = inb(0xe0f); /* Read from data reg */
169 else
170 if (!c924pnp)
171 tmp = inb(port); else
172 tmp = inb(port-0x80);
173 restore_flags(flags);
175 return tmp;
178 static void mad_write(int port, int value)
180 unsigned long flags;
182 save_flags(flags);
183 cli();
185 switch (board_type) /* Output password */
187 case C928:
188 case MOZART:
189 outb((0xE2), PASSWD_REG);
190 break;
192 case C929:
193 outb((0xE3), PASSWD_REG);
194 break;
196 case C930:
197 /* outb(( 0xE4), PASSWD_REG); */
198 break;
200 case C924:
201 if (!c924pnp)
202 outb((0xE5), PASSWD_REG); else
203 outb((0xE5), PASSWD_REG - 0x80);
204 break;
207 if (board_type == C930)
209 outb((port - MC0_PORT), 0xe0e); /* Write to index reg */
210 outb(((unsigned char) (value & 0xff)), 0xe0f);
212 else
213 if (!c924pnp)
214 outb(((unsigned char) (value & 0xff)), port); else
215 outb(((unsigned char) (value & 0xff)), port-0x80);
216 restore_flags(flags);
219 static int __init detect_c930(void)
221 unsigned char tmp = mad_read(MC1_PORT);
223 if ((tmp & 0x06) != 0x06)
225 DDB(printk("Wrong C930 signature (%x)\n", tmp));
226 /* return 0; */
228 mad_write(MC1_PORT, 0);
230 if (mad_read(MC1_PORT) != 0x06)
232 DDB(printk("Wrong C930 signature2 (%x)\n", tmp));
233 /* return 0; */
235 mad_write(MC1_PORT, tmp); /* Restore bits */
237 mad_write(MC7_PORT, 0);
238 if ((tmp = mad_read(MC7_PORT)) != 0)
240 DDB(printk("MC7 not writable (%x)\n", tmp));
241 return 0;
243 mad_write(MC7_PORT, 0xcb);
244 if ((tmp = mad_read(MC7_PORT)) != 0xcb)
246 DDB(printk("MC7 not writable2 (%x)\n", tmp));
247 return 0;
250 tmp = mad_read(MC0_PORT+18);
251 if (tmp == 0xff || tmp == 0x00)
252 return 1;
253 /* We probably have a C931 */
254 DDB(printk("Detected C931 config=0x%02x\n", tmp));
255 c931_detected = 1;
258 * We cannot configure the chip if it is in PnP mode.
259 * If we have a CSN assigned (bit 8 in MC13) we first try
260 * a software reset, then a software power off, finally
261 * Clearing PnP mode. The last option is not
262 * Bit 8 in MC13
264 if ((mad_read(MC0_PORT+13) & 0x80) == 0)
265 return 1;
267 /* Software reset */
268 mad_write(MC9_PORT, 0x02);
269 mad_write(MC9_PORT, 0x00);
271 if ((mad_read(MC0_PORT+13) & 0x80) == 0)
272 return 1;
274 /* Power off, and on again */
275 mad_write(MC9_PORT, 0xc2);
276 mad_write(MC9_PORT, 0xc0);
278 if ((mad_read(MC0_PORT+13) & 0x80) == 0)
279 return 1;
281 #if 0
282 /* Force off PnP mode. This is not recommended because
283 * the PnP bios will not recognize the chip on the next
284 * warm boot and may assignd different resources to other
285 * PnP/PCI cards.
287 mad_write(MC0_PORT+17, 0x04);
288 #endif
289 return 1;
292 static int __init detect_mad16(void)
294 unsigned char tmp, tmp2, bit;
295 int i, port;
298 * Check that reading a register doesn't return bus float (0xff)
299 * when the card is accessed using password. This may fail in case
300 * the card is in low power mode. Normally at least the power saving
301 * mode bit should be 0.
304 if ((tmp = mad_read(MC1_PORT)) == 0xff)
306 DDB(printk("MC1_PORT returned 0xff\n"));
307 return 0;
309 for (i = 0xf8d; i <= 0xf98; i++)
310 if (!c924pnp)
311 DDB(printk("Port %0x (init value) = %0x\n", i, mad_read(i))) else
312 DDB(printk("Port %0x (init value) = %0x\n", i-0x80, mad_read(i)));
314 if (board_type == C930)
315 return detect_c930();
318 * Now check that the gate is closed on first I/O after writing
319 * the password. (This is how a MAD16 compatible card works).
322 if ((tmp2 = inb(MC1_PORT)) == tmp) /* It didn't close */
324 DDB(printk("MC1_PORT didn't close after read (0x%02x)\n", tmp2));
325 return 0;
328 bit = (c924pnp) ? 0x20 : 0x80;
329 port = (c924pnp) ? MC2_PORT : MC1_PORT;
331 tmp = mad_read(port);
332 mad_write(port, tmp ^ bit); /* Toggle a bit */
333 if ((tmp2 = mad_read(port)) != (tmp ^ bit)) /* Compare the bit */
335 mad_write(port, tmp); /* Restore */
336 DDB(printk("Bit revert test failed (0x%02x, 0x%02x)\n", tmp, tmp2));
337 return 0;
339 mad_write(port, tmp); /* Restore */
340 return 1; /* Bingo */
343 static int __init wss_init(struct address_info *hw_config)
345 int ad_flags = 0;
348 * Verify the WSS parameters
351 if (check_region(hw_config->io_base, 8))
353 printk(KERN_ERR "MSS: I/O port conflict\n");
354 return 0;
356 if (!ad1848_detect(hw_config->io_base + 4, &ad_flags, mad16_osp))
357 return 0;
359 * Check if the IO port returns valid signature. The original MS Sound
360 * system returns 0x04 while some cards (AudioTrix Pro for example)
361 * return 0x00.
364 if ((inb(hw_config->io_base + 3) & 0x3f) != 0x04 &&
365 (inb(hw_config->io_base + 3) & 0x3f) != 0x00)
367 DDB(printk("No MSS signature detected on port 0x%x (0x%x)\n", hw_config->io_base, inb(hw_config->io_base + 3)));
368 return 0;
370 if (hw_config->irq > 11)
372 printk(KERN_ERR "MSS: Bad IRQ %d\n", hw_config->irq);
373 return 0;
375 if (hw_config->dma != 0 && hw_config->dma != 1 && hw_config->dma != 3)
377 printk(KERN_ERR "MSS: Bad DMA %d\n", hw_config->dma);
378 return 0;
381 * Check that DMA0 is not in use with a 8 bit board.
384 if (hw_config->dma == 0 && inb(hw_config->io_base + 3) & 0x80)
386 printk("MSS: Can't use DMA0 with a 8 bit card/slot\n");
387 return 0;
389 if (hw_config->irq > 7 && hw_config->irq != 9 && inb(hw_config->io_base + 3) & 0x80)
390 printk(KERN_ERR "MSS: Can't use IRQ%d with a 8 bit card/slot\n", hw_config->irq);
391 return 1;
394 static int __init init_c930(struct address_info *hw_config)
396 unsigned char cfg = 0;
398 #ifdef MAD16_CONF
399 cfg |= (0x0f & MAD16_CONF);
400 #endif
402 if(c931_detected)
404 /* Bit 0 has reversd meaning. Bits 1 and 2 sese
405 reversed on write.
406 Support only IDE cdrom. IDE port programmed
407 somewhere else. */
408 cfg = (cfg & 0x09) ^ 0x07;
411 switch (hw_config->io_base)
413 case 0x530:
414 cfg |= 0x00;
415 break;
416 case 0xe80:
417 cfg |= 0x10;
418 break;
419 case 0xf40:
420 cfg |= 0x20;
421 break;
422 case 0x604:
423 cfg |= 0x30;
424 break;
425 default:
426 printk(KERN_ERR "MAD16: Invalid codec port %x\n", hw_config->io_base);
427 return 0;
429 mad_write(MC1_PORT, cfg);
431 /* MC2 is CD configuration. Don't touch it. */
433 mad_write(MC3_PORT, 0); /* Disable SB mode IRQ and DMA */
435 /* bit 2 of MC4 reverses it's meaning between the C930
436 and the C931. */
437 cfg = c931_detected ? 0x04 : 0x00;
438 #ifdef MAD16_CDSEL
439 if(MAD16_CDSEL & 0x20)
440 mad_write(MC4_PORT, 0x62|cfg); /* opl4 */
441 else
442 mad_write(MC4_PORT, 0x52|cfg); /* opl3 */
443 #else
444 mad_write(MC4_PORT, 0x52|cfg);
445 #endif
446 mad_write(MC5_PORT, 0x3C); /* Init it into mode2 */
447 mad_write(MC6_PORT, 0x02); /* Enable WSS, Disable MPU and SB */
448 mad_write(MC7_PORT, 0xCB);
449 mad_write(MC10_PORT, 0x11);
451 return wss_init(hw_config);
454 static int __init chip_detect(void)
456 int i;
459 * Then try to detect with the old password
461 board_type = C924;
463 DDB(printk("Detect using password = 0xE5\n"));
465 if (!detect_mad16()) /* No luck. Try different model */
467 board_type = C928;
469 DDB(printk("Detect using password = 0xE2\n"));
471 if (!detect_mad16())
473 board_type = C929;
475 DDB(printk("Detect using password = 0xE3\n"));
477 if (!detect_mad16())
479 if (inb(PASSWD_REG) != 0xff)
480 return 0;
483 * First relocate MC# registers to 0xe0e/0xe0f, disable password
486 outb((0xE4), PASSWD_REG);
487 outb((0x80), PASSWD_REG);
489 board_type = C930;
491 DDB(printk("Detect using password = 0xE4\n"));
493 for (i = 0xf8d; i <= 0xf93; i++)
494 DDB(printk("port %03x = %02x\n", i, mad_read(i)));
495 if(!detect_mad16()) {
497 /* The C931 has the password reg at F8D */
498 outb((0xE4), 0xF8D);
499 outb((0x80), 0xF8D);
500 DDB(printk("Detect using password = 0xE4 for C931\n"));
502 if (!detect_mad16()) {
503 board_type = C924;
504 c924pnp++;
505 DDB(printk("Detect using password = 0xE5 (again), port offset -0x80\n"));
506 if (!detect_mad16()) {
507 c924pnp=0;
508 return 0;
511 DDB(printk("mad16.c: 82C924 PnP detected\n"));
514 else
515 DDB(printk("mad16.c: 82C930 detected\n"));
516 } else
517 DDB(printk("mad16.c: 82C929 detected\n"));
518 } else {
519 unsigned char model;
521 if (((model = mad_read(MC3_PORT)) & 0x03) == 0x03) {
522 DDB(printk("mad16.c: Mozart detected\n"));
523 board_type = MOZART;
524 } else {
525 DDB(printk("mad16.c: 82C928 detected???\n"));
526 board_type = C928;
530 return 1;
533 static int __init probe_mad16(struct address_info *hw_config)
535 int i;
536 static int valid_ports[] =
538 0x530, 0xe80, 0xf40, 0x604
540 unsigned char tmp;
541 unsigned char cs4231_mode = 0;
543 int ad_flags = 0;
545 if (already_initialized)
546 return 0;
548 mad16_osp = hw_config->osp;
551 * Check that all ports return 0xff (bus float) when no password
552 * is written to the password register.
555 DDB(printk("--- Detecting MAD16 / Mozart ---\n"));
556 if (!chip_detect())
557 return 0;
559 if (board_type == C930)
560 return init_c930(hw_config);
563 for (i = 0xf8d; i <= 0xf93; i++)
564 if (!c924pnp)
565 DDB(printk("port %03x = %02x\n", i, mad_read(i))) else
566 DDB(printk("port %03x = %02x\n", i-0x80, mad_read(i)));
569 * Set the WSS address
572 tmp = (mad_read(MC1_PORT) & 0x0f) | 0x80; /* Enable WSS, Disable SB */
574 for (i = 0; i < 5; i++)
576 if (i > 3) /* Not a valid port */
578 printk(KERN_ERR "MAD16/Mozart: Bad WSS base address 0x%x\n", hw_config->io_base);
579 return 0;
581 if (valid_ports[i] == hw_config->io_base)
583 tmp |= i << 4; /* WSS port select bits */
584 break;
589 * Set optional CD-ROM and joystick settings.
592 tmp &= ~0x0f;
593 #if defined(MAD16_CONF)
594 tmp |= ((MAD16_CONF) & 0x0f); /* CD-ROM and joystick bits */
595 #endif
596 mad_write(MC1_PORT, tmp);
598 #if defined(MAD16_CONF) && defined(MAD16_CDSEL)
599 tmp = MAD16_CDSEL;
600 #else
601 tmp = mad_read(MC2_PORT);
602 #endif
604 #ifdef MAD16_OPL4
605 tmp |= 0x20; /* Enable OPL4 access */
606 #endif
608 mad_write(MC2_PORT, tmp);
609 mad_write(MC3_PORT, 0xf0); /* Disable SB */
611 if (board_type == C924) /* Specific C924 init values */
613 mad_write(MC4_PORT, 0xA0);
614 mad_write(MC5_PORT, 0x05);
615 mad_write(MC6_PORT, 0x03);
617 if (!ad1848_detect(hw_config->io_base + 4, &ad_flags, mad16_osp))
618 return 0;
620 if (ad_flags & (AD_F_CS4231 | AD_F_CS4248))
621 cs4231_mode = 0x02; /* CS4248/CS4231 sync delay switch */
623 if (board_type == C929)
625 mad_write(MC4_PORT, 0xa2);
626 mad_write(MC5_PORT, 0xA5 | cs4231_mode);
627 mad_write(MC6_PORT, 0x03); /* Disable MPU401 */
629 else
631 mad_write(MC4_PORT, 0x02);
632 mad_write(MC5_PORT, 0x30 | cs4231_mode);
635 for (i = 0xf8d; i <= 0xf93; i++) if (!c924pnp)
636 DDB(printk("port %03x after init = %02x\n", i, mad_read(i))) else
637 DDB(printk("port %03x after init = %02x\n", i-0x80, mad_read(i)));
638 wss_init(hw_config);
640 return 1;
643 static void __init attach_mad16(struct address_info *hw_config)
646 static signed char interrupt_bits[12] = {
647 -1, -1, -1, -1, -1, -1, -1, 0x08, -1, 0x10, 0x18, 0x20
649 signed char bits;
651 static char dma_bits[4] = {
652 1, 2, 0, 3
655 int config_port = hw_config->io_base + 0, version_port = hw_config->io_base + 3;
656 int ad_flags = 0, dma = hw_config->dma, dma2 = hw_config->dma2;
657 unsigned char dma2_bit = 0;
659 already_initialized = 1;
661 if (!ad1848_detect(hw_config->io_base + 4, &ad_flags, mad16_osp))
662 return;
665 * Set the IRQ and DMA addresses.
668 if (board_type == C930 || c924pnp)
669 interrupt_bits[5] = 0x28; /* Also IRQ5 is possible on C930 */
671 bits = interrupt_bits[hw_config->irq];
672 if (bits == -1)
673 return;
675 outb((bits | 0x40), config_port);
676 if ((inb(version_port) & 0x40) == 0)
677 printk(KERN_ERR "[IRQ Conflict?]\n");
680 * Handle the capture DMA channel
683 if (ad_flags & AD_F_CS4231 && dma2 != -1 && dma2 != dma)
685 if (!((dma == 0 && dma2 == 1) ||
686 (dma == 1 && dma2 == 0) ||
687 (dma == 3 && dma2 == 0)))
688 { /* Unsupported combination. Try to swap channels */
689 int tmp = dma;
691 dma = dma2;
692 dma2 = tmp;
694 if ((dma == 0 && dma2 == 1) || (dma == 1 && dma2 == 0) ||
695 (dma == 3 && dma2 == 0))
697 dma2_bit = 0x04; /* Enable capture DMA */
699 else
701 printk("MAD16: Invalid capture DMA\n");
702 dma2 = dma;
705 else dma2 = dma;
707 outb((bits | dma_bits[dma] | dma2_bit), config_port); /* Write IRQ+DMA setup */
709 hw_config->slots[0] = ad1848_init("MAD16 WSS", hw_config->io_base + 4,
710 hw_config->irq,
711 dma,
712 dma2, 0,
713 hw_config->osp);
714 request_region(hw_config->io_base, 4, "MAD16 WSS config");
717 static void __init attach_mad16_mpu(struct address_info *hw_config)
719 #ifdef CONFIG_MAD16_OLDCARD
721 if (mad_read(MC1_PORT) & 0x20)
722 hw_config->io_base = 0x240;
723 else
724 hw_config->io_base = 0x220;
726 hw_config->name = "Mad16/Mozart";
727 sb_dsp_init(hw_config);
728 return;
729 #endif
731 if (!already_initialized)
732 return;
734 hw_config->driver_use_1 = SB_MIDI_ONLY;
735 hw_config->name = "Mad16/Mozart";
736 attach_uart401(hw_config);
739 static int __init probe_mad16_mpu(struct address_info *hw_config)
741 static int mpu_attached = 0;
742 static int valid_ports[] = {
743 0x330, 0x320, 0x310, 0x300
746 static short valid_irqs[] = {9, 10, 5, 7};
747 unsigned char tmp;
748 int i; /* A variable with secret power */
750 if (!already_initialized) /* The MSS port must be initialized first */
751 return 0;
753 if (mpu_attached) /* Don't let them call this twice */
754 return 0;
755 mpu_attached = 1;
757 if (board_type < C929) /* Early chip. No MPU support. Just SB MIDI */
760 #ifdef CONFIG_MAD16_OLDCARD
761 unsigned char tmp;
763 tmp = mad_read(MC3_PORT);
766 * MAD16 SB base is defined by the WSS base. It cannot be changed
767 * alone.
768 * Ignore configured I/O base. Use the active setting.
771 if (mad_read(MC1_PORT) & 0x20)
772 hw_config->io_base = 0x240;
773 else
774 hw_config->io_base = 0x220;
776 switch (hw_config->irq)
778 case 5:
779 tmp = (tmp & 0x3f) | 0x80;
780 break;
781 case 7:
782 tmp = (tmp & 0x3f);
783 break;
784 case 11:
785 tmp = (tmp & 0x3f) | 0x40;
786 break;
787 default:
788 printk(KERN_ERR "mad16/Mozart: Invalid MIDI IRQ\n");
789 return 0;
792 mad_write(MC3_PORT, tmp | 0x04);
793 hw_config->driver_use_1 = SB_MIDI_ONLY;
794 return sb_dsp_detect(hw_config, 0, 0, NULL);
795 #else
796 /* assuming all later Mozart cards are identified as
797 * either 82C928 or Mozart. If so, following code attempts
798 * to set MPU register. TODO - add probing
802 unsigned char tmp;
804 tmp = mad_read(MC8_PORT);
806 switch (hw_config->irq)
808 case 5:
809 tmp |= 0x08;
810 break;
811 case 7:
812 tmp |= 0x10;
813 break;
814 case 9:
815 tmp |= 0x18;
816 break;
817 case 10:
818 tmp |= 0x20;
819 break;
820 case 11:
821 tmp |= 0x28;
822 break;
823 default:
824 printk(KERN_ERR "mad16/MOZART: invalid mpu_irq\n");
825 return 0;
828 switch (hw_config->io_base)
830 case 0x300:
831 tmp |= 0x01;
832 break;
833 case 0x310:
834 tmp |= 0x03;
835 break;
836 case 0x320:
837 tmp |= 0x05;
838 break;
839 case 0x330:
840 tmp |= 0x07;
841 break;
842 default:
843 printk(KERN_ERR "mad16/MOZART: invalid mpu_io\n");
844 return 0;
847 mad_write(MC8_PORT, tmp); /* write MPU port parameters */
849 return probe_uart401(hw_config);
850 #endif
852 tmp = mad_read(MC6_PORT) & 0x83;
853 tmp |= 0x80; /* MPU-401 enable */
856 * Set the MPU base bits
859 for (i = 0; i < 5; i++)
861 if (i > 3) /* Out of array bounds */
863 printk(KERN_ERR "MAD16 / Mozart: Invalid MIDI port 0x%x\n", hw_config->io_base);
864 return 0;
866 if (valid_ports[i] == hw_config->io_base)
868 tmp |= i << 5;
869 break;
874 * Set the MPU IRQ bits
877 for (i = 0; i < 5; i++)
879 if (i > 3) /* Out of array bounds */
881 printk(KERN_ERR "MAD16 / Mozart: Invalid MIDI IRQ %d\n", hw_config->irq);
882 return 0;
884 if (valid_irqs[i] == hw_config->irq)
886 tmp |= i << 3;
887 break;
890 mad_write(MC6_PORT, tmp); /* Write MPU401 config */
892 return probe_uart401(hw_config);
895 static void __exit unload_mad16(struct address_info *hw_config)
897 ad1848_unload(hw_config->io_base + 4,
898 hw_config->irq,
899 hw_config->dma,
900 hw_config->dma2, 0);
901 release_region(hw_config->io_base, 4);
902 sound_unload_audiodev(hw_config->slots[0]);
905 static void __exit unload_mad16_mpu(struct address_info *hw_config)
907 #ifdef CONFIG_MAD16_OLDCARD
908 if (board_type < C929) /* Early chip. No MPU support. Just SB MIDI */
910 sb_dsp_unload(hw_config, 0);
911 return;
913 #endif
915 unload_uart401(hw_config);
918 static struct address_info cfg;
919 static struct address_info cfg_mpu;
921 static int found_mpu;
923 static int __initdata mpu_io = 0;
924 static int __initdata mpu_irq = 0;
925 static int __initdata io = -1;
926 static int __initdata dma = -1;
927 static int __initdata dma16 = -1; /* Set this for modules that need it */
928 static int __initdata irq = -1;
929 static int __initdata cdtype = 0;
930 static int __initdata cdirq = 0;
931 static int __initdata cdport = 0x340;
932 static int __initdata cddma = -1;
933 static int __initdata opl4 = 0;
934 static int __initdata joystick = 0;
936 MODULE_PARM(mpu_io, "i");
937 MODULE_PARM(mpu_irq, "i");
938 MODULE_PARM(io,"i");
939 MODULE_PARM(dma,"i");
940 MODULE_PARM(dma16,"i");
941 MODULE_PARM(irq,"i");
942 MODULE_PARM(cdtype,"i");
943 MODULE_PARM(cdirq,"i");
944 MODULE_PARM(cdport,"i");
945 MODULE_PARM(cddma,"i");
946 MODULE_PARM(opl4,"i");
947 MODULE_PARM(joystick,"i");
948 MODULE_PARM(debug,"i");
950 static int __initdata dma_map[2][8] =
952 {0x03, -1, -1, -1, -1, 0x00, 0x01, 0x02},
953 {0x03, -1, 0x01, 0x00, -1, -1, -1, -1}
956 static int __initdata irq_map[16] =
958 0x00, -1, -1, 0x0A,
959 -1, 0x04, -1, 0x08,
960 -1, 0x10, 0x14, 0x18,
961 -1, -1, -1, -1
964 static int __init init_mad16(void)
966 int dmatype = 0;
968 printk(KERN_INFO "MAD16 audio driver Copyright (C) by Hannu Savolainen 1993-1996\n");
970 printk(KERN_INFO "CDROM ");
971 switch (cdtype)
973 case 0x00:
974 printk("Disabled");
975 cdirq = 0;
976 break;
977 case 0x02:
978 printk("Sony CDU31A");
979 dmatype = 1;
980 if(cddma == -1) cddma = 3;
981 break;
982 case 0x04:
983 printk("Mitsumi");
984 dmatype = 0;
985 if(cddma == -1) cddma = 5;
986 break;
987 case 0x06:
988 printk("Panasonic Lasermate");
989 dmatype = 1;
990 if(cddma == -1) cddma = 3;
991 break;
992 case 0x08:
993 printk("Secondary IDE");
994 dmatype = 0;
995 if(cddma == -1) cddma = 5;
996 break;
997 case 0x0A:
998 printk("Primary IDE");
999 dmatype = 0;
1000 if(cddma == -1) cddma = 5;
1001 break;
1002 default:
1003 printk("\n");
1004 printk(KERN_ERR "Invalid CDROM type\n");
1005 return -EINVAL;
1009 * Build the config words
1012 mad16_conf = (joystick ^ 1) | cdtype;
1013 mad16_cdsel = 0;
1014 if (opl4)
1015 mad16_cdsel |= 0x20;
1017 if(cdtype){
1018 if (cddma > 7 || cddma < 0 || dma_map[dmatype][cddma] == -1)
1020 printk("\n");
1021 printk(KERN_ERR "Invalid CDROM DMA\n");
1022 return -EINVAL;
1024 if (cddma)
1025 printk(", DMA %d", cddma);
1026 else
1027 printk(", no DMA");
1029 if (!cdirq)
1030 printk(", no IRQ");
1031 else if (cdirq < 0 || cdirq > 15 || irq_map[cdirq] == -1)
1033 printk(", invalid IRQ (disabling)");
1034 cdirq = 0;
1036 else printk(", IRQ %d", cdirq);
1038 mad16_cdsel |= dma_map[dmatype][cddma];
1040 if (cdtype < 0x08)
1042 switch (cdport)
1044 case 0x340:
1045 mad16_cdsel |= 0x00;
1046 break;
1047 case 0x330:
1048 mad16_cdsel |= 0x40;
1049 break;
1050 case 0x360:
1051 mad16_cdsel |= 0x80;
1052 break;
1053 case 0x320:
1054 mad16_cdsel |= 0xC0;
1055 break;
1056 default:
1057 printk(KERN_ERR "Unknown CDROM I/O base %d\n", cdport);
1058 return -EINVAL;
1061 mad16_cdsel |= irq_map[cdirq];
1064 printk(".\n");
1065 printk(KERN_INFO "Joystick port ");
1066 if (joystick == 1)
1067 printk("enabled.\n");
1068 else
1070 joystick = 0;
1071 printk("disabled.\n");
1074 cfg.io_base = io;
1075 cfg.irq = irq;
1076 cfg.dma = dma;
1077 cfg.dma2 = dma16;
1079 if (cfg.io_base == -1 || cfg.dma == -1 || cfg.irq == -1) {
1080 printk(KERN_ERR "I/O, DMA and irq are mandatory\n");
1081 return -EINVAL;
1084 if (!probe_mad16(&cfg))
1085 return -ENODEV;
1087 cfg_mpu.io_base = mpu_io;
1088 cfg_mpu.irq = mpu_irq;
1090 attach_mad16(&cfg);
1092 found_mpu = probe_mad16_mpu(&cfg_mpu);
1094 if (found_mpu)
1095 attach_mad16_mpu(&cfg_mpu);
1097 SOUND_LOCK;
1098 return 0;
1101 static void __exit cleanup_mad16(void)
1103 if (found_mpu)
1104 unload_mad16_mpu(&cfg_mpu);
1105 unload_mad16(&cfg);
1106 SOUND_LOCK_END;
1109 module_init(init_mad16);
1110 module_exit(cleanup_mad16);
1112 #ifndef MODULE
1113 static int __init setup_mad16(char *str)
1115 /* io, irq */
1116 int ints[7];
1118 str = get_options(str, ARRAY_SIZE(ints), ints);
1120 io = ints[1];
1121 irq = ints[2];
1122 dma = ints[3];
1123 dma16 = ints[4];
1124 mpu_io = ints[5];
1125 mpu_irq = ints[6];
1127 return 1;
1130 __setup("mad16=", setup_mad16);
1131 #endif