| [7a69340] | 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>
|
|---|
| [01282fc] | 30 | #include <str_error.h>
|
|---|
| [365f259] | 31 | #include <macros.h>
|
|---|
| [25c98a8e] | 32 | #include <audio_mixer_iface.h>
|
|---|
| [90f05b0f] | 33 | #include <audio_pcm_iface.h>
|
|---|
| [7a69340] | 34 |
|
|---|
| [9dd79bc7] | 35 | #include "ddf_log.h"
|
|---|
| 36 | #include "dsp_commands.h"
|
|---|
| [cf083e8] | 37 | #include "dsp.h"
|
|---|
| [7a69340] | 38 | #include "sb16.h"
|
|---|
| 39 |
|
|---|
| [25c98a8e] | 40 | extern audio_mixer_iface_t sb_mixer_iface;
|
|---|
| [90f05b0f] | 41 | extern audio_pcm_iface_t sb_pcm_iface;
|
|---|
| [25c98a8e] | 42 |
|
|---|
| 43 | static ddf_dev_ops_t sb_mixer_ops = {
|
|---|
| 44 | .interfaces[AUDIO_MIXER_IFACE] = &sb_mixer_iface,
|
|---|
| 45 | };
|
|---|
| 46 |
|
|---|
| [9808edd] | 47 | static ddf_dev_ops_t sb_pcm_ops = {
|
|---|
| 48 | .interfaces[AUDIO_PCM_BUFFER_IFACE] = &sb_pcm_iface,
|
|---|
| 49 | };
|
|---|
| 50 |
|
|---|
| [7a69340] | 51 | /* ISA interrupts should be edge-triggered so there should be no need for
|
|---|
| [413225d] | 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 | };
|
|---|
| [7a69340] | 58 |
|
|---|
| [bde691c] | 59 | static inline sb_mixer_type_t sb_mixer_type_by_dsp_version(
|
|---|
| 60 | unsigned major, unsigned minor)
|
|---|
| [01282fc] | 61 | {
|
|---|
| [7a0a0f5] | 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 | }
|
|---|
| [01282fc] | 70 | }
|
|---|
| [e941bf8] | 71 |
|
|---|
| [413225d] | 72 | size_t sb16_irq_code_size(void)
|
|---|
| [7a69340] | 73 | {
|
|---|
| [365f259] | 74 | return ARRAY_SIZE(irq_cmds);
|
|---|
| [413225d] | 75 | }
|
|---|
| [e941bf8] | 76 |
|
|---|
| [7de1988c] | 77 | void sb16_irq_code(addr_range_t *regs, int dma8, int dma16, irq_cmd_t cmds[],
|
|---|
| 78 | irq_pio_range_t ranges[])
|
|---|
| [413225d] | 79 | {
|
|---|
| 80 | assert(regs);
|
|---|
| 81 | assert(dma8 > 0 && dma8 < 4);
|
|---|
| [7de1988c] | 82 |
|
|---|
| 83 | sb16_regs_t *registers = RNGABSPTR(*regs);
|
|---|
| [413225d] | 84 | memcpy(cmds, irq_cmds, sizeof(irq_cmds));
|
|---|
| [7de1988c] | 85 | cmds[0].addr = (void *) ®isters->dsp_read_status;
|
|---|
| 86 | ranges[0].base = (uintptr_t) registers;
|
|---|
| [ea150dc6] | 87 | ranges[0].size = sizeof(*registers);
|
|---|
| [413225d] | 88 | if (dma16 > 4 && dma16 < 8) {
|
|---|
| 89 | /* Valid dma16 */
|
|---|
| [7de1988c] | 90 | cmds[1].addr = (void *) ®isters->dma16_ack;
|
|---|
| [413225d] | 91 | } else {
|
|---|
| 92 | cmds[1].cmd = CMD_ACCEPT;
|
|---|
| 93 | }
|
|---|
| [7a69340] | 94 | }
|
|---|
| [e941bf8] | 95 |
|
|---|
| [46577995] | 96 | errno_t sb16_init_sb16(sb16_t *sb, addr_range_t *regs, ddf_dev_t *dev, int dma8,
|
|---|
| [7de1988c] | 97 | int dma16)
|
|---|
| [7a69340] | 98 | {
|
|---|
| [c885a21] | 99 | assert(sb);
|
|---|
| [7de1988c] | 100 |
|
|---|
| [9dd79bc7] | 101 | /* Setup registers */
|
|---|
| [46577995] | 102 | errno_t ret = pio_enable_range(regs, (void **) &sb->regs);
|
|---|
| [9dd79bc7] | 103 | if (ret != EOK)
|
|---|
| 104 | return ret;
|
|---|
| [7de1988c] | 105 | ddf_log_note("PIO registers at %p accessible.", sb->regs);
|
|---|
| [9dd79bc7] | 106 |
|
|---|
| [bf38143] | 107 | /* Initialize DSP */
|
|---|
| [ce047249] | 108 | ddf_fun_t *dsp_fun = ddf_fun_create(dev, fun_exposed, "pcm");
|
|---|
| [9808edd] | 109 | if (!dsp_fun) {
|
|---|
| [124f9bd] | 110 | ddf_log_error("Failed to create dsp function.");
|
|---|
| [9808edd] | 111 | return ENOMEM;
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| [c885a21] | 114 | ret = sb_dsp_init(&sb->dsp, sb->regs, dev, dma8, dma16);
|
|---|
| [cf083e8] | 115 | if (ret != EOK) {
|
|---|
| [124f9bd] | 116 | ddf_log_error("Failed to initialize SB DSP: %s.",
|
|---|
| [bf38143] | 117 | str_error(ret));
|
|---|
| [94694a4] | 118 | ddf_fun_destroy(dsp_fun);
|
|---|
| [cf083e8] | 119 | return ret;
|
|---|
| 120 | }
|
|---|
| [9bd4615] | 121 |
|
|---|
| [03362fbd] | 122 | ddf_fun_set_ops(dsp_fun, &sb_pcm_ops);
|
|---|
| [124f9bd] | 123 | ddf_log_note("Sound blaster DSP (%x.%x) initialized.",
|
|---|
| [c885a21] | 124 | sb->dsp.version.major, sb->dsp.version.minor);
|
|---|
| [9dd79bc7] | 125 |
|
|---|
| [9808edd] | 126 | ret = ddf_fun_bind(dsp_fun);
|
|---|
| 127 | if (ret != EOK) {
|
|---|
| 128 | ddf_log_error(
|
|---|
| [94694a4] | 129 | "Failed to bind PCM function: %s.", str_error(ret));
|
|---|
| [9808edd] | 130 | ddf_fun_destroy(dsp_fun);
|
|---|
| 131 | return ret;
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| [94694a4] | 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 |
|
|---|
| [01282fc] | 143 | /* Initialize mixer */
|
|---|
| [bde691c] | 144 | const sb_mixer_type_t mixer_type = sb_mixer_type_by_dsp_version(
|
|---|
| [c885a21] | 145 | sb->dsp.version.major, sb->dsp.version.minor);
|
|---|
| [01282fc] | 146 |
|
|---|
| [ce047249] | 147 | ddf_fun_t *mixer_fun = ddf_fun_create(dev, fun_exposed, "control");
|
|---|
| [25c98a8e] | 148 | if (!mixer_fun) {
|
|---|
| [124f9bd] | 149 | ddf_log_error("Failed to create mixer function.");
|
|---|
| [9808edd] | 150 | ddf_fun_unbind(dsp_fun);
|
|---|
| 151 | ddf_fun_destroy(dsp_fun);
|
|---|
| [25c98a8e] | 152 | return ENOMEM;
|
|---|
| 153 | }
|
|---|
| [c885a21] | 154 | ret = sb_mixer_init(&sb->mixer, sb->regs, mixer_type);
|
|---|
| [01282fc] | 155 | if (ret != EOK) {
|
|---|
| [124f9bd] | 156 | ddf_log_error("Failed to initialize SB mixer: %s.",
|
|---|
| [01282fc] | 157 | str_error(ret));
|
|---|
| [9808edd] | 158 | ddf_fun_unbind(dsp_fun);
|
|---|
| 159 | ddf_fun_destroy(dsp_fun);
|
|---|
| 160 | ddf_fun_destroy(mixer_fun);
|
|---|
| [01282fc] | 161 | return ret;
|
|---|
| 162 | }
|
|---|
| [25c98a8e] | 163 |
|
|---|
| [124f9bd] | 164 | ddf_log_note("Initialized mixer: %s.",
|
|---|
| [c885a21] | 165 | sb_mixer_type_str(sb->mixer.type));
|
|---|
| [03362fbd] | 166 | ddf_fun_set_ops(mixer_fun, &sb_mixer_ops);
|
|---|
| [25c98a8e] | 167 |
|
|---|
| 168 | ret = ddf_fun_bind(mixer_fun);
|
|---|
| 169 | if (ret != EOK) {
|
|---|
| 170 | ddf_log_error(
|
|---|
| [124f9bd] | 171 | "Failed to bind mixer function: %s.", str_error(ret));
|
|---|
| [25c98a8e] | 172 | ddf_fun_destroy(mixer_fun);
|
|---|
| [9808edd] | 173 | ddf_fun_unbind(dsp_fun);
|
|---|
| 174 | ddf_fun_destroy(dsp_fun);
|
|---|
| [25c98a8e] | 175 | return ret;
|
|---|
| 176 | }
|
|---|
| [9dd79bc7] | 177 |
|
|---|
| [7a69340] | 178 | return EOK;
|
|---|
| 179 | }
|
|---|
| [e941bf8] | 180 |
|
|---|
| [46577995] | 181 | errno_t sb16_init_mpu(sb16_t *sb, addr_range_t *regs)
|
|---|
| [7a69340] | 182 | {
|
|---|
| [c885a21] | 183 | sb->mpu_regs = NULL;
|
|---|
| [7a69340] | 184 | return ENOTSUP;
|
|---|
| 185 | }
|
|---|
| [e941bf8] | 186 |
|
|---|
| [c885a21] | 187 | void sb16_interrupt(sb16_t *sb)
|
|---|
| [f14e6ea] | 188 | {
|
|---|
| [c885a21] | 189 | assert(sb);
|
|---|
| [f14e6ea] | 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 */
|
|---|
| [c885a21] | 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);
|
|---|
| [f14e6ea] | 196 | /* Third bit is MPU-401 interrupt */
|
|---|
| 197 | if (irq_mask & 0x4) {
|
|---|
| 198 | return;
|
|---|
| 199 | }
|
|---|
| [dce7e41] | 200 | } else {
|
|---|
| [124f9bd] | 201 | ddf_log_debug("SB16 interrupt.");
|
|---|
| [f14e6ea] | 202 | }
|
|---|
| [c885a21] | 203 | sb_dsp_interrupt(&sb->dsp);
|
|---|
| [f14e6ea] | 204 | }
|
|---|