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 $
31 #include <i386/isa/sound/sound_config.h>
35 #include <i386/isa/sound/coproc.h>
37 #define EXCLUDE_NATIVE_PCM
54 #define GA_INTSTAT_REG 0
55 #define GA_INTENA_REG 1
58 #define GA_INTCFG_REG 4
59 #define GA_DMACFG_REG 5
60 #define GA_CDCFG_REG 6
61 #define GA_SMCFGA_REG 7
62 #define GA_SMCFGB_REG 8
63 #define GA_HMCTL_REG 9
66 * DMA channel identifiers (A and B)
68 #define SSCAPE_DMA_A 0
69 #define SSCAPE_DMA_B 1
71 #define PORT(name) (devc->base+name)
74 * Host commands recognized by the OBP microcode
76 #define CMD_GEN_HOST_ACK 0x80
77 #define CMD_GEN_MPU_ACK 0x81
78 #define CMD_GET_BOARD_TYPE 0x82
79 #define CMD_SET_CONTROL 0x88
80 #define CMD_GET_CONTROL 0x89
81 #define CTL_MASTER_VOL 0
82 #define CTL_MIC_MODE 2
83 #define CTL_SYNTH_VOL 4
84 #define CTL_WAVE_VOL 7
85 #define CMD_SET_MT32 0x96
86 #define CMD_GET_MT32 0x97
87 #define CMD_SET_EXTMIDI 0x9b
88 #define CMD_GET_EXTMIDI 0x9c
92 typedef struct sscape_info {
94 int ok; /* Properly detected */
103 static struct sscape_info dev_info =
105 static struct sscape_info *devc = &dev_info;
107 static int *sscape_sleeper = NULL;
108 static volatile struct snd_wait sscape_sleep_flag =
111 /* Some older cards have assigned interrupt bits differently than new ones */
112 static char valid_interrupts_old[] =
115 static char valid_interrupts_new[] =
118 static char *valid_interrupts = valid_interrupts_new;
121 static char old_hardware = 1;
124 static char old_hardware = 0;
129 sscape_read(struct sscape_info * devc, int reg)
135 outb(PORT(ODIE_ADDR), reg);
136 val = inb(PORT(ODIE_DATA));
142 sscape_write(struct sscape_info * devc, int reg, int data)
147 outb(PORT(ODIE_ADDR), reg);
148 outb(PORT(ODIE_DATA), data);
153 host_open(struct sscape_info * devc)
155 outb(PORT(HOST_CTRL), 0x00); /* Put the board to the host mode */
159 host_close(struct sscape_info * devc)
161 outb(PORT(HOST_CTRL), 0x03); /* Put the board to the MIDI mode */
165 host_write(struct sscape_info * devc, u_char *data, int count)
173 * Send the command and data bytes
176 for (i = 0; i < count; i++) {
177 for (timeout_val = 10000; timeout_val > 0; timeout_val--)
178 if (inb(PORT(HOST_CTRL)) & TX_READY)
181 if (timeout_val <= 0) {
185 outb(PORT(HOST_DATA), data[i]);
195 host_read(struct sscape_info * devc)
207 for (timeout_val = 10000; timeout_val > 0; timeout_val--)
208 if (inb(PORT(HOST_CTRL)) & RX_READY)
211 if (timeout_val <= 0) {
215 data = inb(PORT(HOST_DATA));
223 host_command1(struct sscape_info * devc, int cmd)
227 buf[0] = (u_char) (cmd & 0xff);
229 return host_write(devc, buf, 1);
233 host_command2(struct sscape_info * devc, int cmd, int parm1)
237 buf[0] = (u_char) (cmd & 0xff);
238 buf[1] = (u_char) (parm1 & 0xff);
240 return host_write(devc, buf, 2);
244 host_command3(struct sscape_info * devc, int cmd, int parm1, int parm2)
248 buf[0] = (u_char) (cmd & 0xff);
249 buf[1] = (u_char) (parm1 & 0xff);
250 buf[2] = (u_char) (parm2 & 0xff);
252 return host_write(devc, buf, 3);
256 set_mt32(struct sscape_info * devc, int value)
259 host_command2(devc, CMD_SET_MT32,
261 if (host_read(devc) != CMD_ACK) {
262 printf("SNDSCAPE: Setting MT32 mode failed\n");
268 set_control(struct sscape_info * devc, int ctrl, int value)
271 host_command3(devc, CMD_SET_CONTROL, ctrl, value);
272 if (host_read(devc) != CMD_ACK) {
273 printf("SNDSCAPE: Setting control (%d) failed\n", ctrl);
279 get_board_type(struct sscape_info * devc)
284 if (!host_command1(devc, CMD_GET_BOARD_TYPE))
287 tmp = host_read(devc);
297 bits = sscape_read(devc, GA_INTSTAT_REG);
298 DEB(printf("sscapeintr(0x%02x)\n", bits));
299 if ((sscape_sleep_flag.mode & WK_SLEEP)) {
300 sscape_sleep_flag.mode = WK_WAKEUP;
301 wakeup(sscape_sleeper);
303 if (bits & 0x02) { /* Host interface interrupt */
304 printf("SSCAPE: Host interrupt, data=%02x\n", host_read(devc));
306 #if (defined(CONFIG_MPU401) || defined(CONFIG_MPU_EMU)) && defined(CONFIG_MIDI)
308 static int debug = 0;
311 if (debug++ > 10) { /* Temporary debugging hack */
312 sscape_write(devc, GA_INTENA_REG, 0x00); /* Disable all interr. */
318 * Acknowledge interrupts (toggle the interrupt bits)
321 tmp = sscape_read(devc, GA_INTENA_REG);
322 sscape_write(devc, GA_INTENA_REG, (~bits & 0x0e) | (tmp & 0xf1));
327 do_dma(struct sscape_info * devc, int dma_chan, u_long buf, int blk_size, int mode)
331 if (dma_chan != SSCAPE_DMA_A) {
332 printf("SSCAPE: Tried to use DMA channel != A. Why?\n");
335 DMAbuf_start_dma(devc->my_audiodev, buf, blk_size, mode);
337 temp = devc->dma << 4; /* Setup DMA channel select bits */
339 temp |= 0x80; /* 8 bit DMA channel */
341 temp |= 1; /* Trigger DMA */
342 sscape_write(devc, GA_DMAA_REG, temp);
343 temp &= 0xfe; /* Clear DMA trigger */
344 sscape_write(devc, GA_DMAA_REG, temp);
348 verify_mpu(struct sscape_info * devc)
351 * The SoundScape board could be in three modes (MPU, 8250 and host).
352 * If the card is not in the MPU mode, enabling the MPU driver will
353 * cause infinite loop (the driver believes that there is always some
354 * received data in the buffer.
356 * Detect this by looking if there are more than 10 received MIDI bytes
357 * (0x00) in the buffer.
362 for (i = 0; i < 10; i++) {
363 if (inb(devc->base + HOST_CTRL) & 0x80)
366 if (inb(devc->base) != 0x00)
370 printf("SoundScape: The device is not in the MPU-401 mode\n");
375 sscape_coproc_open(void *dev_info, int sub_device)
377 if (sub_device == COPR_MIDI) {
379 if (!verify_mpu(devc))
382 sscape_sleep_flag.aborting = 0;
383 sscape_sleep_flag.mode = WK_NONE;
388 sscape_coproc_close(void *dev_info, int sub_device)
390 struct sscape_info *devc = dev_info;
394 if (devc->dma_allocated) {
395 sscape_write(devc, GA_DMAA_REG, 0x20); /* DMA channel disabled */
396 #ifdef CONFIG_NATIVE_PCM
398 devc->dma_allocated = 0;
400 sscape_sleep_flag.aborting = 0;
401 sscape_sleep_flag.mode = WK_NONE;
408 sscape_coproc_reset(void *dev_info)
413 sscape_download_boot(struct sscape_info * devc, u_char *block, int size, int flag)
417 int done, timeout_val;
419 if (flag & CPF_FIRST) {
421 * First block. Have to allocate DMA and to reset the board
426 if (devc->dma_allocated == 0) {
427 devc->dma_allocated = 1;
431 sscape_write(devc, GA_HMCTL_REG,
432 (temp = sscape_read(devc, GA_HMCTL_REG)) & 0x3f); /* Reset */
434 for (timeout_val = 10000; timeout_val > 0; timeout_val--)
435 sscape_read(devc, GA_HMCTL_REG); /* Delay */
437 /* Take board out of reset */
438 sscape_write(devc, GA_HMCTL_REG,
439 (temp = sscape_read(devc, GA_HMCTL_REG)) | 0x80);
442 * Transfer one code block using DMA
444 bcopy(block, audio_devs[devc->my_audiodev]->dmap_out->raw_buf, size);
447 /******** INTERRUPTS DISABLED NOW ********/
448 do_dma(devc, SSCAPE_DMA_A,
449 audio_devs[devc->my_audiodev]->dmap_out->raw_buf_phys,
453 * Wait until transfer completes.
455 sscape_sleep_flag.aborting = 0;
456 sscape_sleep_flag.mode = WK_NONE;
459 while (!done && timeout_val-- > 0) {
461 sscape_sleeper = &chn;
462 DO_SLEEP(chn, sscape_sleep_flag, 1);
471 if (flag & CPF_LAST) {
473 * Take the board out of reset
475 outb(PORT(HOST_CTRL), 0x00);
476 outb(PORT(MIDI_CTRL), 0x00);
478 temp = sscape_read(devc, GA_HMCTL_REG);
480 sscape_write(devc, GA_HMCTL_REG, temp); /* Kickstart the board */
483 * Wait until the ODB wakes up
488 timeout_val = 5 * hz;
489 while (!done && timeout_val-- > 0) {
492 sscape_sleeper = &chn;
493 DO_SLEEP(chn, sscape_sleep_flag, 1);
495 if (inb(PORT(HOST_DATA)) == 0xff) /* OBP startup acknowledge */
500 printf("SoundScape: The OBP didn't respond after code download\n");
505 timeout_val = 5 * hz;
506 while (!done && timeout_val-- > 0) {
509 sscape_sleeper = &chn;
510 DO_SLEEP(chn, sscape_sleep_flag, 1);
512 if (inb(PORT(HOST_DATA)) == 0xfe) /* Host startup acknowledge */
517 printf("SoundScape: OBP Initialization failed.\n");
520 printf("SoundScape board of type %d initialized OK\n",
521 get_board_type(devc));
523 set_control(devc, CTL_MASTER_VOL, 100);
524 set_control(devc, CTL_SYNTH_VOL, 100);
528 * Temporary debugging aid. Print contents of the registers
529 * after downloading the code.
534 for (i = 0; i < 13; i++)
535 printf("I%d = %02x (new value)\n", i, sscape_read(devc, i));
544 download_boot_block(void *dev_info, copr_buffer * buf)
546 if (buf->len <= 0 || buf->len > sizeof(buf->data))
549 if (!sscape_download_boot(devc, buf->data, buf->len, buf->flags)) {
550 printf("SSCAPE: Unable to load microcode block to the OBP.\n");
557 sscape_coproc_ioctl(void *dev_info, u_int cmd, ioctl_arg arg, int local)
561 case SNDCTL_COPR_RESET:
562 sscape_coproc_reset(dev_info);
566 case SNDCTL_COPR_LOAD:
571 buf = (copr_buffer *) malloc(sizeof(copr_buffer), M_TEMP, M_WAITOK);
574 bcopy(&(((char *) arg)[0]), (char *) buf, sizeof(*buf));
575 err = download_boot_block(dev_info, buf);
587 static coproc_operations sscape_coproc_operations =
597 #if !defined(EXCLUDE_NATIVE_PCM) && defined(CONFIG_AUDIO)
598 static struct audio_operations sscape_audio_operations =
602 AFMT_U8 | AFMT_S16_LE,
606 sscape_audio_output_block,
607 sscape_audio_start_input,
609 sscape_audio_prepare_for_input,
610 sscape_audio_prepare_for_output,
616 #endif /* !defined(EXCLUDE_NATIVE_PCM) && defined(CONFIG_AUDIO) */
618 static int sscape_detected = 0;
621 attach_sscape(struct address_info * hw_config)
625 * Config register values for Spea/V7 Media FX and Ensoniq S-2000.
626 * These values are card dependent. If you have another SoundScape
627 * based card, you have to find the correct values. Do the following:
628 * - Compile this driver with SSCAPE_DEBUG1 defined. - Shut down and
629 * power off your machine. - Boot with DOS so that the SSINIT.EXE
630 * program is run. - Warm boot to {Linux|SYSV|BSD} and write down the
631 * lines displayed when detecting the SoundScape. - Modify the
632 * following list to use the values printed during boot. Undefine the
635 #define SSCAPE_REGS { \
637 0xf0, /* Note! Ignored. Set always to 0xf0 */ \
638 0x20, /* Note! Ignored. Set always to 0x20 */ \
639 0x20, /* Note! Ignored. Set always to 0x20 */ \
640 0xf5, /* Ignored */ \
643 0x2e, /* I7 MEM config A. Likely to vary between models */ \
644 0x00, /* I8 MEM config B. Likely to vary between models */ \
645 /* I9 */ 0x40 /* Ignored */ \
650 static u_char regs[10] = SSCAPE_REGS;
652 int i, irq_bits = 0xff;
654 if (sscape_detected != hw_config->io_base)
658 valid_interrupts = valid_interrupts_old;
659 conf_printf("Ensoniq Soundscape (old)", hw_config);
661 conf_printf("Ensoniq Soundscape", hw_config);
663 for (i = 0; i < sizeof(valid_interrupts); i++)
664 if (hw_config->irq == valid_interrupts[i]) {
668 if (hw_config->irq > 15 || (regs[4] = irq_bits == 0xff)) {
669 printf("Invalid IRQ%d\n", hw_config->irq);
674 for (i = 1; i < 10; i++)
676 case 1: /* Host interrupt enable */
677 sscape_write(devc, i, 0xf0); /* All interrupts enabled */
680 case 2: /* DMA A status/trigger register */
681 case 3: /* DMA B status/trigger register */
682 sscape_write(devc, i, 0x20); /* DMA channel disabled */
685 case 4: /* Host interrupt config reg */
686 sscape_write(devc, i, 0xf0 | (irq_bits << 2) | irq_bits);
689 case 5: /* Don't destroy CD-ROM DMA config bits (0xc0) */
690 sscape_write(devc, i, (regs[i] & 0x3f) |
691 (sscape_read(devc, i) & 0xc0));
694 case 6: /* CD-ROM config. Don't touch. */
697 case 9: /* Master control reg. Don't modify CR-ROM
698 * bits. Disable SB emul */
699 sscape_write(devc, i, (sscape_read(devc, i) & 0xf0) | 0x00);
703 sscape_write(devc, i, regs[i]);
710 * Temporary debugging aid. Print contents of the registers after
716 for (i = 0; i < 13; i++)
717 printf("I%d = %02x (new value)\n", i, sscape_read(devc, i));
721 #if defined(CONFIG_MIDI) && defined(CONFIG_MPU_EMU)
722 if (probe_mpu401(hw_config))
723 hw_config->always_detect = 1;
727 prev_devs = num_midis;
728 attach_mpu401(hw_config);
730 if (num_midis == (prev_devs + 1)) /* The MPU driver
731 * installed itself */
732 midi_devs[prev_devs]->coproc = &sscape_coproc_operations;
736 #ifndef EXCLUDE_NATIVE_PCM
737 /* Not supported yet */
740 if (num_audiodevs < MAX_AUDIO_DEV) {
743 audio_devs[my_dev = num_audiodevs++] = &sscape_audio_operations;
744 audio_devs[my_dev]->dmachan1 = hw_config->dma;
745 audio_devs[my_dev]->buffsize = DSP_BUFFSIZE;
746 audio_devs[my_dev]->devc = devc;
747 devc->my_audiodev = my_dev;
749 audio_devs[my_dev]->coproc = &sscape_coproc_operations;
750 if (snd_set_irq_handler(hw_config->irq, sscapeintr, devc->osp) < 0)
751 printf("Error: Can't allocate IRQ for SoundScape\n");
752 sscape_write(devc, GA_INTENA_REG, 0x80); /* Master IRQ enable */
754 printf("SoundScape: More than enough audio devices detected\n");
763 probe_sscape(struct address_info * hw_config)
768 devc->base = hw_config->io_base;
769 devc->irq = hw_config->irq;
770 devc->dma = hw_config->dma;
771 devc->osp = hw_config->osp;
773 if (sscape_detected != 0 && sscape_detected != hw_config->io_base)
777 * First check that the address register of "ODIE" is there and that
778 * it has exactly 4 writeable bits. First 4 bits
780 if ((save = inb(PORT(ODIE_ADDR))) & 0xf0)
783 outb(PORT(ODIE_ADDR), 0x00);
784 if (inb(PORT(ODIE_ADDR)) != 0x00)
787 outb(PORT(ODIE_ADDR), 0xff);
788 if (inb(PORT(ODIE_ADDR)) != 0x0f)
791 outb(PORT(ODIE_ADDR), save);
794 * Now verify that some indirect registers return zero on some bits.
795 * This may break the driver with some future revisions of "ODIE"
799 if (sscape_read(devc, 0) & 0x0c)
802 if (sscape_read(devc, 1) & 0x0f)
805 if (sscape_read(devc, 5) & 0x0f)
810 * Temporary debugging aid. Print contents of the registers before
816 for (i = 0; i < 13; i++)
817 printf("I%d = %02x (old value)\n", i, sscape_read(devc, i));
821 if (old_hardware) { /* Check that it's really an old Spea/Reveal card. */
825 if (!((tmp = sscape_read(devc, GA_HMCTL_REG)) & 0xc0)) {
826 sscape_write(devc, GA_HMCTL_REG, tmp | 0x80);
827 for (cc = 0; cc < 200000; ++cc)
828 inb(devc->base + ODIE_ADDR);
833 printf("sscape.c: Can't allocate DMA channel\n");
836 sscape_detected = hw_config->io_base;
842 probe_ss_mss(struct address_info * hw_config)
844 int i, irq_bits = 0xff;
850 printf("SoundScape: Invalid initialization order.\n");
853 for (i = 0; i < sizeof(valid_interrupts); i++)
854 if (hw_config->irq == valid_interrupts[i]) {
858 if (hw_config->irq > 15 || irq_bits == 0xff) {
859 printf("SoundScape: Invalid MSS IRQ%d\n", hw_config->irq);
862 return ad1848_detect(hw_config->io_base, NULL, hw_config->osp);
866 attach_ss_mss(struct address_info * hw_config)
869 * This routine configures the SoundScape card for use with the Win
870 * Sound System driver. The AD1848 codec interface uses the CD-ROM
871 * config registers of the "ODIE".
874 int i, irq_bits = 0xff;
876 #ifndef CONFIG_NATIVE_PCM
877 int prev_devs = num_audiodevs;
881 * Setup the DMA polarity.
883 sscape_write(devc, GA_DMACFG_REG, 0x50);
886 * Take the gate-arry off of the DMA channel.
888 sscape_write(devc, GA_DMAB_REG, 0x20);
891 * Init the AD1848 (CD-ROM) config reg.
894 for (i = 0; i < sizeof(valid_interrupts); i++)
895 if (hw_config->irq == valid_interrupts[i]) {
899 sscape_write(devc, GA_CDCFG_REG, 0x89 | (hw_config->dma << 4) |
902 if (hw_config->irq == devc->irq)
903 printf("SoundScape: Warning! WSS mode can't share IRQ with MIDI\n");
905 ad1848_init("SoundScape", hw_config->io_base,
912 #ifndef CONFIG_NATIVE_PCM
913 /* Check if the AD1848 driver installed itself */
914 if (num_audiodevs == (prev_devs + 1))
915 audio_devs[prev_devs]->coproc = &sscape_coproc_operations;