4 * Low level driver for Ensoniq Soundscape
6 * Copyright by Hannu Savolainen 1994
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met: 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 2.
12 * Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * $FreeBSD: src/sys/i386/isa/sound/sscape.c,v 1.9 1999/12/20 18:05:01 eivind Exp $
29 * $DragonFly: src/sys/dev/sound/isa/i386/sscape/Attic/sscape.c,v 1.2 2003/06/17 04:28:38 dillon Exp $
32 #include <i386/isa/sound/sound_config.h>
36 #include <i386/isa/sound/coproc.h>
38 #define EXCLUDE_NATIVE_PCM
55 #define GA_INTSTAT_REG 0
56 #define GA_INTENA_REG 1
59 #define GA_INTCFG_REG 4
60 #define GA_DMACFG_REG 5
61 #define GA_CDCFG_REG 6
62 #define GA_SMCFGA_REG 7
63 #define GA_SMCFGB_REG 8
64 #define GA_HMCTL_REG 9
67 * DMA channel identifiers (A and B)
69 #define SSCAPE_DMA_A 0
70 #define SSCAPE_DMA_B 1
72 #define PORT(name) (devc->base+name)
75 * Host commands recognized by the OBP microcode
77 #define CMD_GEN_HOST_ACK 0x80
78 #define CMD_GEN_MPU_ACK 0x81
79 #define CMD_GET_BOARD_TYPE 0x82
80 #define CMD_SET_CONTROL 0x88
81 #define CMD_GET_CONTROL 0x89
82 #define CTL_MASTER_VOL 0
83 #define CTL_MIC_MODE 2
84 #define CTL_SYNTH_VOL 4
85 #define CTL_WAVE_VOL 7
86 #define CMD_SET_MT32 0x96
87 #define CMD_GET_MT32 0x97
88 #define CMD_SET_EXTMIDI 0x9b
89 #define CMD_GET_EXTMIDI 0x9c
93 typedef struct sscape_info {
95 int ok; /* Properly detected */
104 static struct sscape_info dev_info =
106 static struct sscape_info *devc = &dev_info;
108 static int *sscape_sleeper = NULL;
109 static volatile struct snd_wait sscape_sleep_flag =
112 /* Some older cards have assigned interrupt bits differently than new ones */
113 static char valid_interrupts_old[] =
116 static char valid_interrupts_new[] =
119 static char *valid_interrupts = valid_interrupts_new;
122 static char old_hardware = 1;
125 static char old_hardware = 0;
130 sscape_read(struct sscape_info * devc, int reg)
136 outb(PORT(ODIE_ADDR), reg);
137 val = inb(PORT(ODIE_DATA));
143 sscape_write(struct sscape_info * devc, int reg, int data)
148 outb(PORT(ODIE_ADDR), reg);
149 outb(PORT(ODIE_DATA), data);
154 host_open(struct sscape_info * devc)
156 outb(PORT(HOST_CTRL), 0x00); /* Put the board to the host mode */
160 host_close(struct sscape_info * devc)
162 outb(PORT(HOST_CTRL), 0x03); /* Put the board to the MIDI mode */
166 host_write(struct sscape_info * devc, u_char *data, int count)
174 * Send the command and data bytes
177 for (i = 0; i < count; i++) {
178 for (timeout_val = 10000; timeout_val > 0; timeout_val--)
179 if (inb(PORT(HOST_CTRL)) & TX_READY)
182 if (timeout_val <= 0) {
186 outb(PORT(HOST_DATA), data[i]);
196 host_read(struct sscape_info * devc)
208 for (timeout_val = 10000; timeout_val > 0; timeout_val--)
209 if (inb(PORT(HOST_CTRL)) & RX_READY)
212 if (timeout_val <= 0) {
216 data = inb(PORT(HOST_DATA));
224 host_command1(struct sscape_info * devc, int cmd)
228 buf[0] = (u_char) (cmd & 0xff);
230 return host_write(devc, buf, 1);
234 host_command2(struct sscape_info * devc, int cmd, int parm1)
238 buf[0] = (u_char) (cmd & 0xff);
239 buf[1] = (u_char) (parm1 & 0xff);
241 return host_write(devc, buf, 2);
245 host_command3(struct sscape_info * devc, int cmd, int parm1, int parm2)
249 buf[0] = (u_char) (cmd & 0xff);
250 buf[1] = (u_char) (parm1 & 0xff);
251 buf[2] = (u_char) (parm2 & 0xff);
253 return host_write(devc, buf, 3);
257 set_mt32(struct sscape_info * devc, int value)
260 host_command2(devc, CMD_SET_MT32,
262 if (host_read(devc) != CMD_ACK) {
263 printf("SNDSCAPE: Setting MT32 mode failed\n");
269 set_control(struct sscape_info * devc, int ctrl, int value)
272 host_command3(devc, CMD_SET_CONTROL, ctrl, value);
273 if (host_read(devc) != CMD_ACK) {
274 printf("SNDSCAPE: Setting control (%d) failed\n", ctrl);
280 get_board_type(struct sscape_info * devc)
285 if (!host_command1(devc, CMD_GET_BOARD_TYPE))
288 tmp = host_read(devc);
298 bits = sscape_read(devc, GA_INTSTAT_REG);
299 DEB(printf("sscapeintr(0x%02x)\n", bits));
300 if ((sscape_sleep_flag.mode & WK_SLEEP)) {
301 sscape_sleep_flag.mode = WK_WAKEUP;
302 wakeup(sscape_sleeper);
304 if (bits & 0x02) { /* Host interface interrupt */
305 printf("SSCAPE: Host interrupt, data=%02x\n", host_read(devc));
307 #if (defined(CONFIG_MPU401) || defined(CONFIG_MPU_EMU)) && defined(CONFIG_MIDI)
309 static int debug = 0;
312 if (debug++ > 10) { /* Temporary debugging hack */
313 sscape_write(devc, GA_INTENA_REG, 0x00); /* Disable all interr. */
319 * Acknowledge interrupts (toggle the interrupt bits)
322 tmp = sscape_read(devc, GA_INTENA_REG);
323 sscape_write(devc, GA_INTENA_REG, (~bits & 0x0e) | (tmp & 0xf1));
328 do_dma(struct sscape_info * devc, int dma_chan, u_long buf, int blk_size, int mode)
332 if (dma_chan != SSCAPE_DMA_A) {
333 printf("SSCAPE: Tried to use DMA channel != A. Why?\n");
336 DMAbuf_start_dma(devc->my_audiodev, buf, blk_size, mode);
338 temp = devc->dma << 4; /* Setup DMA channel select bits */
340 temp |= 0x80; /* 8 bit DMA channel */
342 temp |= 1; /* Trigger DMA */
343 sscape_write(devc, GA_DMAA_REG, temp);
344 temp &= 0xfe; /* Clear DMA trigger */
345 sscape_write(devc, GA_DMAA_REG, temp);
349 verify_mpu(struct sscape_info * devc)
352 * The SoundScape board could be in three modes (MPU, 8250 and host).
353 * If the card is not in the MPU mode, enabling the MPU driver will
354 * cause infinite loop (the driver believes that there is always some
355 * received data in the buffer.
357 * Detect this by looking if there are more than 10 received MIDI bytes
358 * (0x00) in the buffer.
363 for (i = 0; i < 10; i++) {
364 if (inb(devc->base + HOST_CTRL) & 0x80)
367 if (inb(devc->base) != 0x00)
371 printf("SoundScape: The device is not in the MPU-401 mode\n");
376 sscape_coproc_open(void *dev_info, int sub_device)
378 if (sub_device == COPR_MIDI) {
380 if (!verify_mpu(devc))
383 sscape_sleep_flag.aborting = 0;
384 sscape_sleep_flag.mode = WK_NONE;
389 sscape_coproc_close(void *dev_info, int sub_device)
391 struct sscape_info *devc = dev_info;
395 if (devc->dma_allocated) {
396 sscape_write(devc, GA_DMAA_REG, 0x20); /* DMA channel disabled */
397 #ifdef CONFIG_NATIVE_PCM
399 devc->dma_allocated = 0;
401 sscape_sleep_flag.aborting = 0;
402 sscape_sleep_flag.mode = WK_NONE;
409 sscape_coproc_reset(void *dev_info)
414 sscape_download_boot(struct sscape_info * devc, u_char *block, int size, int flag)
418 int done, timeout_val;
420 if (flag & CPF_FIRST) {
422 * First block. Have to allocate DMA and to reset the board
427 if (devc->dma_allocated == 0) {
428 devc->dma_allocated = 1;
432 sscape_write(devc, GA_HMCTL_REG,
433 (temp = sscape_read(devc, GA_HMCTL_REG)) & 0x3f); /* Reset */
435 for (timeout_val = 10000; timeout_val > 0; timeout_val--)
436 sscape_read(devc, GA_HMCTL_REG); /* Delay */
438 /* Take board out of reset */
439 sscape_write(devc, GA_HMCTL_REG,
440 (temp = sscape_read(devc, GA_HMCTL_REG)) | 0x80);
443 * Transfer one code block using DMA
445 bcopy(block, audio_devs[devc->my_audiodev]->dmap_out->raw_buf, size);
448 /******** INTERRUPTS DISABLED NOW ********/
449 do_dma(devc, SSCAPE_DMA_A,
450 audio_devs[devc->my_audiodev]->dmap_out->raw_buf_phys,
454 * Wait until transfer completes.
456 sscape_sleep_flag.aborting = 0;
457 sscape_sleep_flag.mode = WK_NONE;
460 while (!done && timeout_val-- > 0) {
462 sscape_sleeper = &chn;
463 DO_SLEEP(chn, sscape_sleep_flag, 1);
472 if (flag & CPF_LAST) {
474 * Take the board out of reset
476 outb(PORT(HOST_CTRL), 0x00);
477 outb(PORT(MIDI_CTRL), 0x00);
479 temp = sscape_read(devc, GA_HMCTL_REG);
481 sscape_write(devc, GA_HMCTL_REG, temp); /* Kickstart the board */
484 * Wait until the ODB wakes up
489 timeout_val = 5 * hz;
490 while (!done && timeout_val-- > 0) {
493 sscape_sleeper = &chn;
494 DO_SLEEP(chn, sscape_sleep_flag, 1);
496 if (inb(PORT(HOST_DATA)) == 0xff) /* OBP startup acknowledge */
501 printf("SoundScape: The OBP didn't respond after code download\n");
506 timeout_val = 5 * hz;
507 while (!done && timeout_val-- > 0) {
510 sscape_sleeper = &chn;
511 DO_SLEEP(chn, sscape_sleep_flag, 1);
513 if (inb(PORT(HOST_DATA)) == 0xfe) /* Host startup acknowledge */
518 printf("SoundScape: OBP Initialization failed.\n");
521 printf("SoundScape board of type %d initialized OK\n",
522 get_board_type(devc));
524 set_control(devc, CTL_MASTER_VOL, 100);
525 set_control(devc, CTL_SYNTH_VOL, 100);
529 * Temporary debugging aid. Print contents of the registers
530 * after downloading the code.
535 for (i = 0; i < 13; i++)
536 printf("I%d = %02x (new value)\n", i, sscape_read(devc, i));
545 download_boot_block(void *dev_info, copr_buffer * buf)
547 if (buf->len <= 0 || buf->len > sizeof(buf->data))
550 if (!sscape_download_boot(devc, buf->data, buf->len, buf->flags)) {
551 printf("SSCAPE: Unable to load microcode block to the OBP.\n");
558 sscape_coproc_ioctl(void *dev_info, u_int cmd, ioctl_arg arg, int local)
562 case SNDCTL_COPR_RESET:
563 sscape_coproc_reset(dev_info);
567 case SNDCTL_COPR_LOAD:
572 buf = (copr_buffer *) malloc(sizeof(copr_buffer), M_TEMP, M_WAITOK);
575 bcopy(&(((char *) arg)[0]), (char *) buf, sizeof(*buf));
576 err = download_boot_block(dev_info, buf);
588 static coproc_operations sscape_coproc_operations =
598 #if !defined(EXCLUDE_NATIVE_PCM) && defined(CONFIG_AUDIO)
599 static struct audio_operations sscape_audio_operations =
603 AFMT_U8 | AFMT_S16_LE,
607 sscape_audio_output_block,
608 sscape_audio_start_input,
610 sscape_audio_prepare_for_input,
611 sscape_audio_prepare_for_output,
617 #endif /* !defined(EXCLUDE_NATIVE_PCM) && defined(CONFIG_AUDIO) */
619 static int sscape_detected = 0;
622 attach_sscape(struct address_info * hw_config)
626 * Config register values for Spea/V7 Media FX and Ensoniq S-2000.
627 * These values are card dependent. If you have another SoundScape
628 * based card, you have to find the correct values. Do the following:
629 * - Compile this driver with SSCAPE_DEBUG1 defined. - Shut down and
630 * power off your machine. - Boot with DOS so that the SSINIT.EXE
631 * program is run. - Warm boot to {Linux|SYSV|BSD} and write down the
632 * lines displayed when detecting the SoundScape. - Modify the
633 * following list to use the values printed during boot. Undefine the
636 #define SSCAPE_REGS { \
638 0xf0, /* Note! Ignored. Set always to 0xf0 */ \
639 0x20, /* Note! Ignored. Set always to 0x20 */ \
640 0x20, /* Note! Ignored. Set always to 0x20 */ \
641 0xf5, /* Ignored */ \
644 0x2e, /* I7 MEM config A. Likely to vary between models */ \
645 0x00, /* I8 MEM config B. Likely to vary between models */ \
646 /* I9 */ 0x40 /* Ignored */ \
651 static u_char regs[10] = SSCAPE_REGS;
653 int i, irq_bits = 0xff;
655 if (sscape_detected != hw_config->io_base)
659 valid_interrupts = valid_interrupts_old;
660 conf_printf("Ensoniq Soundscape (old)", hw_config);
662 conf_printf("Ensoniq Soundscape", hw_config);
664 for (i = 0; i < sizeof(valid_interrupts); i++)
665 if (hw_config->irq == valid_interrupts[i]) {
669 if (hw_config->irq > 15 || (regs[4] = irq_bits == 0xff)) {
670 printf("Invalid IRQ%d\n", hw_config->irq);
675 for (i = 1; i < 10; i++)
677 case 1: /* Host interrupt enable */
678 sscape_write(devc, i, 0xf0); /* All interrupts enabled */
681 case 2: /* DMA A status/trigger register */
682 case 3: /* DMA B status/trigger register */
683 sscape_write(devc, i, 0x20); /* DMA channel disabled */
686 case 4: /* Host interrupt config reg */
687 sscape_write(devc, i, 0xf0 | (irq_bits << 2) | irq_bits);
690 case 5: /* Don't destroy CD-ROM DMA config bits (0xc0) */
691 sscape_write(devc, i, (regs[i] & 0x3f) |
692 (sscape_read(devc, i) & 0xc0));
695 case 6: /* CD-ROM config. Don't touch. */
698 case 9: /* Master control reg. Don't modify CR-ROM
699 * bits. Disable SB emul */
700 sscape_write(devc, i, (sscape_read(devc, i) & 0xf0) | 0x00);
704 sscape_write(devc, i, regs[i]);
711 * Temporary debugging aid. Print contents of the registers after
717 for (i = 0; i < 13; i++)
718 printf("I%d = %02x (new value)\n", i, sscape_read(devc, i));
722 #if defined(CONFIG_MIDI) && defined(CONFIG_MPU_EMU)
723 if (probe_mpu401(hw_config))
724 hw_config->always_detect = 1;
728 prev_devs = num_midis;
729 attach_mpu401(hw_config);
731 if (num_midis == (prev_devs + 1)) /* The MPU driver
732 * installed itself */
733 midi_devs[prev_devs]->coproc = &sscape_coproc_operations;
737 #ifndef EXCLUDE_NATIVE_PCM
738 /* Not supported yet */
741 if (num_audiodevs < MAX_AUDIO_DEV) {
744 audio_devs[my_dev = num_audiodevs++] = &sscape_audio_operations;
745 audio_devs[my_dev]->dmachan1 = hw_config->dma;
746 audio_devs[my_dev]->buffsize = DSP_BUFFSIZE;
747 audio_devs[my_dev]->devc = devc;
748 devc->my_audiodev = my_dev;
750 audio_devs[my_dev]->coproc = &sscape_coproc_operations;
751 if (snd_set_irq_handler(hw_config->irq, sscapeintr, devc->osp) < 0)
752 printf("Error: Can't allocate IRQ for SoundScape\n");
753 sscape_write(devc, GA_INTENA_REG, 0x80); /* Master IRQ enable */
755 printf("SoundScape: More than enough audio devices detected\n");
764 probe_sscape(struct address_info * hw_config)
769 devc->base = hw_config->io_base;
770 devc->irq = hw_config->irq;
771 devc->dma = hw_config->dma;
772 devc->osp = hw_config->osp;
774 if (sscape_detected != 0 && sscape_detected != hw_config->io_base)
778 * First check that the address register of "ODIE" is there and that
779 * it has exactly 4 writeable bits. First 4 bits
781 if ((save = inb(PORT(ODIE_ADDR))) & 0xf0)
784 outb(PORT(ODIE_ADDR), 0x00);
785 if (inb(PORT(ODIE_ADDR)) != 0x00)
788 outb(PORT(ODIE_ADDR), 0xff);
789 if (inb(PORT(ODIE_ADDR)) != 0x0f)
792 outb(PORT(ODIE_ADDR), save);
795 * Now verify that some indirect registers return zero on some bits.
796 * This may break the driver with some future revisions of "ODIE"
800 if (sscape_read(devc, 0) & 0x0c)
803 if (sscape_read(devc, 1) & 0x0f)
806 if (sscape_read(devc, 5) & 0x0f)
811 * Temporary debugging aid. Print contents of the registers before
817 for (i = 0; i < 13; i++)
818 printf("I%d = %02x (old value)\n", i, sscape_read(devc, i));
822 if (old_hardware) { /* Check that it's really an old Spea/Reveal card. */
826 if (!((tmp = sscape_read(devc, GA_HMCTL_REG)) & 0xc0)) {
827 sscape_write(devc, GA_HMCTL_REG, tmp | 0x80);
828 for (cc = 0; cc < 200000; ++cc)
829 inb(devc->base + ODIE_ADDR);
834 printf("sscape.c: Can't allocate DMA channel\n");
837 sscape_detected = hw_config->io_base;
843 probe_ss_mss(struct address_info * hw_config)
845 int i, irq_bits = 0xff;
851 printf("SoundScape: Invalid initialization order.\n");
854 for (i = 0; i < sizeof(valid_interrupts); i++)
855 if (hw_config->irq == valid_interrupts[i]) {
859 if (hw_config->irq > 15 || irq_bits == 0xff) {
860 printf("SoundScape: Invalid MSS IRQ%d\n", hw_config->irq);
863 return ad1848_detect(hw_config->io_base, NULL, hw_config->osp);
867 attach_ss_mss(struct address_info * hw_config)
870 * This routine configures the SoundScape card for use with the Win
871 * Sound System driver. The AD1848 codec interface uses the CD-ROM
872 * config registers of the "ODIE".
875 int i, irq_bits = 0xff;
877 #ifndef CONFIG_NATIVE_PCM
878 int prev_devs = num_audiodevs;
882 * Setup the DMA polarity.
884 sscape_write(devc, GA_DMACFG_REG, 0x50);
887 * Take the gate-arry off of the DMA channel.
889 sscape_write(devc, GA_DMAB_REG, 0x20);
892 * Init the AD1848 (CD-ROM) config reg.
895 for (i = 0; i < sizeof(valid_interrupts); i++)
896 if (hw_config->irq == valid_interrupts[i]) {
900 sscape_write(devc, GA_CDCFG_REG, 0x89 | (hw_config->dma << 4) |
903 if (hw_config->irq == devc->irq)
904 printf("SoundScape: Warning! WSS mode can't share IRQ with MIDI\n");
906 ad1848_init("SoundScape", hw_config->io_base,
913 #ifndef CONFIG_NATIVE_PCM
914 /* Check if the AD1848 driver installed itself */
915 if (num_audiodevs == (prev_devs + 1))
916 audio_devs[prev_devs]->coproc = &sscape_coproc_operations;