| 1 | /*
|
|---|
| 2 | * Copyright (c) 2011 Jan Vesely
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | #include <errno.h>
|
|---|
| 30 | #include <str_error.h>
|
|---|
| 31 | #include <macros.h>
|
|---|
| 32 | #include <audio_mixer_iface.h>
|
|---|
| 33 | #include <audio_pcm_iface.h>
|
|---|
| 34 |
|
|---|
| 35 | #include "ddf_log.h"
|
|---|
| 36 | #include "dsp_commands.h"
|
|---|
| 37 | #include "dsp.h"
|
|---|
| 38 | #include "sb16.h"
|
|---|
| 39 |
|
|---|
| 40 | extern audio_mixer_iface_t sb_mixer_iface;
|
|---|
| 41 | extern audio_pcm_iface_t sb_pcm_iface;
|
|---|
| 42 |
|
|---|
| 43 | static ddf_dev_ops_t sb_mixer_ops = {
|
|---|
| 44 | .interfaces[AUDIO_MIXER_IFACE] = &sb_mixer_iface,
|
|---|
| 45 | };
|
|---|
| 46 |
|
|---|
| 47 | static ddf_dev_ops_t sb_pcm_ops = {
|
|---|
| 48 | .interfaces[AUDIO_PCM_BUFFER_IFACE] = &sb_pcm_iface,
|
|---|
| 49 | };
|
|---|
| 50 |
|
|---|
| 51 | /* ISA interrupts should be edge-triggered so there should be no need for
|
|---|
| 52 | * irq code magic, but we still need to ack those interrupts ASAP. */
|
|---|
| 53 | static const irq_cmd_t irq_cmds[] = {
|
|---|
| 54 | { .cmd = CMD_PIO_READ_8, .dstarg = 1 }, /* Address patched at runtime */
|
|---|
| 55 | { .cmd = CMD_PIO_READ_8, .dstarg = 1 }, /* Address patched at runtime */
|
|---|
| 56 | { .cmd = CMD_ACCEPT },
|
|---|
| 57 | };
|
|---|
| 58 |
|
|---|
| 59 | static inline sb_mixer_type_t sb_mixer_type_by_dsp_version(
|
|---|
| 60 | unsigned major, unsigned minor)
|
|---|
| 61 | {
|
|---|
| 62 | switch (major)
|
|---|
| 63 | {
|
|---|
| 64 | case 1: return SB_MIXER_NONE; /* SB 1.5 and early 2.0 = no mixer chip */
|
|---|
| 65 | case 2: return (minor == 0) ? SB_MIXER_NONE : SB_MIXER_CT1335;
|
|---|
| 66 | case 3: return SB_MIXER_CT1345; /* SB Pro */
|
|---|
| 67 | case 4: return SB_MIXER_CT1745; /* SB 16 */
|
|---|
| 68 | default: return SB_MIXER_UNKNOWN;
|
|---|
| 69 | }
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | size_t sb16_irq_code_size(void)
|
|---|
| 73 | {
|
|---|
| 74 | return ARRAY_SIZE(irq_cmds);
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | void sb16_irq_code(addr_range_t *regs, int dma8, int dma16, irq_cmd_t cmds[],
|
|---|
| 78 | irq_pio_range_t ranges[])
|
|---|
| 79 | {
|
|---|
| 80 | assert(regs);
|
|---|
| 81 | assert(dma8 > 0 && dma8 < 4);
|
|---|
| 82 |
|
|---|
| 83 | sb16_regs_t *registers = RNGABSPTR(*regs);
|
|---|
| 84 | memcpy(cmds, irq_cmds, sizeof(irq_cmds));
|
|---|
| 85 | cmds[0].addr = (void *) ®isters->dsp_read_status;
|
|---|
| 86 | ranges[0].base = (uintptr_t) registers;
|
|---|
| 87 | ranges[0].size = sizeof(*registers);
|
|---|
| 88 | if (dma16 > 4 && dma16 < 8) {
|
|---|
| 89 | /* Valid dma16 */
|
|---|
| 90 | cmds[1].addr = (void *) ®isters->dma16_ack;
|
|---|
| 91 | } else {
|
|---|
| 92 | cmds[1].cmd = CMD_ACCEPT;
|
|---|
| 93 | }
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | int sb16_init_sb16(sb16_t *sb, addr_range_t *regs, ddf_dev_t *dev, int dma8,
|
|---|
| 97 | int dma16)
|
|---|
| 98 | {
|
|---|
| 99 | assert(sb);
|
|---|
| 100 |
|
|---|
| 101 | /* Setup registers */
|
|---|
| 102 | int ret = pio_enable_range(regs, (void **) &sb->regs);
|
|---|
| 103 | if (ret != EOK)
|
|---|
| 104 | return ret;
|
|---|
| 105 | ddf_log_note("PIO registers at %p accessible.", sb->regs);
|
|---|
| 106 |
|
|---|
| 107 | /* Initialize DSP */
|
|---|
| 108 | ddf_fun_t *dsp_fun = ddf_fun_create(dev, fun_exposed, "pcm");
|
|---|
| 109 | if (!dsp_fun) {
|
|---|
| 110 | ddf_log_error("Failed to create dsp function.");
|
|---|
| 111 | return ENOMEM;
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | ret = sb_dsp_init(&sb->dsp, sb->regs, dev, dma8, dma16);
|
|---|
| 115 | if (ret != EOK) {
|
|---|
| 116 | ddf_log_error("Failed to initialize SB DSP: %s.",
|
|---|
| 117 | str_error(ret));
|
|---|
| 118 | ddf_fun_destroy(dsp_fun);
|
|---|
| 119 | return ret;
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | ddf_fun_set_ops(dsp_fun, &sb_pcm_ops);
|
|---|
| 123 | ddf_log_note("Sound blaster DSP (%x.%x) initialized.",
|
|---|
| 124 | sb->dsp.version.major, sb->dsp.version.minor);
|
|---|
| 125 |
|
|---|
| 126 | ret = ddf_fun_bind(dsp_fun);
|
|---|
| 127 | if (ret != EOK) {
|
|---|
| 128 | ddf_log_error(
|
|---|
| 129 | "Failed to bind PCM function: %s.", str_error(ret));
|
|---|
| 130 | ddf_fun_destroy(dsp_fun);
|
|---|
| 131 | return ret;
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | ret = ddf_fun_add_to_category(dsp_fun, "audio-pcm");
|
|---|
| 135 | if (ret != EOK) {
|
|---|
| 136 | ddf_log_error("Failed register PCM function in category: %s.",
|
|---|
| 137 | str_error(ret));
|
|---|
| 138 | ddf_fun_unbind(dsp_fun);
|
|---|
| 139 | ddf_fun_destroy(dsp_fun);
|
|---|
| 140 | return ret;
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | /* Initialize mixer */
|
|---|
| 144 | const sb_mixer_type_t mixer_type = sb_mixer_type_by_dsp_version(
|
|---|
| 145 | sb->dsp.version.major, sb->dsp.version.minor);
|
|---|
| 146 |
|
|---|
| 147 | ddf_fun_t *mixer_fun = ddf_fun_create(dev, fun_exposed, "control");
|
|---|
| 148 | if (!mixer_fun) {
|
|---|
| 149 | ddf_log_error("Failed to create mixer function.");
|
|---|
| 150 | ddf_fun_unbind(dsp_fun);
|
|---|
| 151 | ddf_fun_destroy(dsp_fun);
|
|---|
| 152 | return ENOMEM;
|
|---|
| 153 | }
|
|---|
| 154 | ret = sb_mixer_init(&sb->mixer, sb->regs, mixer_type);
|
|---|
| 155 | if (ret != EOK) {
|
|---|
| 156 | ddf_log_error("Failed to initialize SB mixer: %s.",
|
|---|
| 157 | str_error(ret));
|
|---|
| 158 | ddf_fun_unbind(dsp_fun);
|
|---|
| 159 | ddf_fun_destroy(dsp_fun);
|
|---|
| 160 | ddf_fun_destroy(mixer_fun);
|
|---|
| 161 | return ret;
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | ddf_log_note("Initialized mixer: %s.",
|
|---|
| 165 | sb_mixer_type_str(sb->mixer.type));
|
|---|
| 166 | ddf_fun_set_ops(mixer_fun, &sb_mixer_ops);
|
|---|
| 167 |
|
|---|
| 168 | ret = ddf_fun_bind(mixer_fun);
|
|---|
| 169 | if (ret != EOK) {
|
|---|
| 170 | ddf_log_error(
|
|---|
| 171 | "Failed to bind mixer function: %s.", str_error(ret));
|
|---|
| 172 | ddf_fun_destroy(mixer_fun);
|
|---|
| 173 | ddf_fun_unbind(dsp_fun);
|
|---|
| 174 | ddf_fun_destroy(dsp_fun);
|
|---|
| 175 | return ret;
|
|---|
| 176 | }
|
|---|
| 177 |
|
|---|
| 178 | return EOK;
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | int sb16_init_mpu(sb16_t *sb, addr_range_t *regs)
|
|---|
| 182 | {
|
|---|
| 183 | sb->mpu_regs = NULL;
|
|---|
| 184 | return ENOTSUP;
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | void sb16_interrupt(sb16_t *sb)
|
|---|
| 188 | {
|
|---|
| 189 | assert(sb);
|
|---|
| 190 | /* The acknowledgment of interrupts on DSP version 4.xx is different;
|
|---|
| 191 | * It can contain MPU-401 indicator and DMA16 transfers are acked
|
|---|
| 192 | * differently */
|
|---|
| 193 | if (sb->dsp.version.major >= 4) {
|
|---|
| 194 | pio_write_8(&sb->regs->mixer_address, MIXER_IRQ_STATUS_ADDRESS);
|
|---|
| 195 | const uint8_t irq_mask = pio_read_8(&sb->regs->mixer_data);
|
|---|
| 196 | /* Third bit is MPU-401 interrupt */
|
|---|
| 197 | if (irq_mask & 0x4) {
|
|---|
| 198 | return;
|
|---|
| 199 | }
|
|---|
| 200 | } else {
|
|---|
| 201 | ddf_log_debug("SB16 interrupt.");
|
|---|
| 202 | }
|
|---|
| 203 | sb_dsp_interrupt(&sb->dsp);
|
|---|
| 204 | }
|
|---|