source: mainline/uspace/drv/audio/sb16/sb16.c@ 1798e66

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1798e66 was 7de1988c, checked in by Jakub Jermar <jakub@…>, 12 years ago

Adapt drivers using parsed HW resources to use the new interface.

  • Property mode set to 100644
File size: 6.2 KB
RevLine 
[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
[03362fbd]29#define _DDF_DATA_IMPLANT
30
[7a69340]31#include <errno.h>
[01282fc]32#include <str_error.h>
[365f259]33#include <macros.h>
[25c98a8e]34#include <audio_mixer_iface.h>
[90f05b0f]35#include <audio_pcm_iface.h>
[7a69340]36
[9dd79bc7]37#include "ddf_log.h"
38#include "dsp_commands.h"
[cf083e8]39#include "dsp.h"
[7a69340]40#include "sb16.h"
41
[25c98a8e]42extern audio_mixer_iface_t sb_mixer_iface;
[90f05b0f]43extern audio_pcm_iface_t sb_pcm_iface;
[25c98a8e]44
45static ddf_dev_ops_t sb_mixer_ops = {
46 .interfaces[AUDIO_MIXER_IFACE] = &sb_mixer_iface,
47};
48
[9808edd]49static ddf_dev_ops_t sb_pcm_ops = {
50 .interfaces[AUDIO_PCM_BUFFER_IFACE] = &sb_pcm_iface,
51};
52
[7a69340]53/* ISA interrupts should be edge-triggered so there should be no need for
[413225d]54 * irq code magic, but we still need to ack those interrupts ASAP. */
55static const irq_cmd_t irq_cmds[] = {
56 { .cmd = CMD_PIO_READ_8, .dstarg = 1 }, /* Address patched at runtime */
57 { .cmd = CMD_PIO_READ_8, .dstarg = 1 }, /* Address patched at runtime */
58 { .cmd = CMD_ACCEPT },
59};
[7a69340]60
[bde691c]61static inline sb_mixer_type_t sb_mixer_type_by_dsp_version(
62 unsigned major, unsigned minor)
[01282fc]63{
[7a0a0f5]64 switch (major)
65 {
66 case 1: return SB_MIXER_NONE; /* SB 1.5 and early 2.0 = no mixer chip */
67 case 2: return (minor == 0) ? SB_MIXER_NONE : SB_MIXER_CT1335;
68 case 3: return SB_MIXER_CT1345; /* SB Pro */
69 case 4: return SB_MIXER_CT1745; /* SB 16 */
70 default: return SB_MIXER_UNKNOWN;
71 }
[01282fc]72}
[e941bf8]73
[413225d]74size_t sb16_irq_code_size(void)
[7a69340]75{
[365f259]76 return ARRAY_SIZE(irq_cmds);
[413225d]77}
[e941bf8]78
[7de1988c]79void sb16_irq_code(addr_range_t *regs, int dma8, int dma16, irq_cmd_t cmds[],
80 irq_pio_range_t ranges[])
[413225d]81{
82 assert(regs);
83 assert(dma8 > 0 && dma8 < 4);
[7de1988c]84
85 sb16_regs_t *registers = RNGABSPTR(*regs);
[413225d]86 memcpy(cmds, irq_cmds, sizeof(irq_cmds));
[7de1988c]87 cmds[0].addr = (void *) &registers->dsp_read_status;
88 ranges[0].base = (uintptr_t) registers;
[ea150dc6]89 ranges[0].size = sizeof(*registers);
[413225d]90 if (dma16 > 4 && dma16 < 8) {
91 /* Valid dma16 */
[7de1988c]92 cmds[1].addr = (void *) &registers->dma16_ack;
[413225d]93 } else {
94 cmds[1].cmd = CMD_ACCEPT;
95 }
[7a69340]96}
[e941bf8]97
[7de1988c]98int sb16_init_sb16(sb16_t *sb, addr_range_t *regs, ddf_dev_t *dev, int dma8,
99 int dma16)
[7a69340]100{
[c885a21]101 assert(sb);
[7de1988c]102
[9dd79bc7]103 /* Setup registers */
[7de1988c]104 int ret = pio_enable_range(regs, (void **) &sb->regs);
[9dd79bc7]105 if (ret != EOK)
106 return ret;
[7de1988c]107 ddf_log_note("PIO registers at %p accessible.", sb->regs);
[9dd79bc7]108
[bf38143]109 /* Initialize DSP */
[ce047249]110 ddf_fun_t *dsp_fun = ddf_fun_create(dev, fun_exposed, "pcm");
[9808edd]111 if (!dsp_fun) {
[124f9bd]112 ddf_log_error("Failed to create dsp function.");
[9808edd]113 return ENOMEM;
114 }
115
[c885a21]116 ret = sb_dsp_init(&sb->dsp, sb->regs, dev, dma8, dma16);
[cf083e8]117 if (ret != EOK) {
[124f9bd]118 ddf_log_error("Failed to initialize SB DSP: %s.",
[bf38143]119 str_error(ret));
[94694a4]120 ddf_fun_destroy(dsp_fun);
[cf083e8]121 return ret;
122 }
[03362fbd]123 //TODO remove data implant
124 ddf_fun_data_implant(dsp_fun, &sb->dsp);
125 ddf_fun_set_ops(dsp_fun, &sb_pcm_ops);
[124f9bd]126 ddf_log_note("Sound blaster DSP (%x.%x) initialized.",
[c885a21]127 sb->dsp.version.major, sb->dsp.version.minor);
[9dd79bc7]128
[9808edd]129 ret = ddf_fun_bind(dsp_fun);
130 if (ret != EOK) {
131 ddf_log_error(
[94694a4]132 "Failed to bind PCM function: %s.", str_error(ret));
[03362fbd]133 // TODO implanted data
[9808edd]134 ddf_fun_destroy(dsp_fun);
135 return ret;
136 }
137
[94694a4]138 ret = ddf_fun_add_to_category(dsp_fun, "audio-pcm");
139 if (ret != EOK) {
140 ddf_log_error("Failed register PCM function in category: %s.",
141 str_error(ret));
142 ddf_fun_unbind(dsp_fun);
[03362fbd]143 // TODO implanted data
[94694a4]144 ddf_fun_destroy(dsp_fun);
145 return ret;
146 }
147
[01282fc]148 /* Initialize mixer */
[bde691c]149 const sb_mixer_type_t mixer_type = sb_mixer_type_by_dsp_version(
[c885a21]150 sb->dsp.version.major, sb->dsp.version.minor);
[01282fc]151
[ce047249]152 ddf_fun_t *mixer_fun = ddf_fun_create(dev, fun_exposed, "control");
[25c98a8e]153 if (!mixer_fun) {
[124f9bd]154 ddf_log_error("Failed to create mixer function.");
[9808edd]155 ddf_fun_unbind(dsp_fun);
[03362fbd]156 // TODO implanted data
[9808edd]157 ddf_fun_destroy(dsp_fun);
[25c98a8e]158 return ENOMEM;
159 }
[c885a21]160 ret = sb_mixer_init(&sb->mixer, sb->regs, mixer_type);
[01282fc]161 if (ret != EOK) {
[124f9bd]162 ddf_log_error("Failed to initialize SB mixer: %s.",
[01282fc]163 str_error(ret));
[9808edd]164 ddf_fun_unbind(dsp_fun);
[03362fbd]165 // TODO implanted data
[9808edd]166 ddf_fun_destroy(dsp_fun);
167 ddf_fun_destroy(mixer_fun);
[01282fc]168 return ret;
169 }
[25c98a8e]170
[124f9bd]171 ddf_log_note("Initialized mixer: %s.",
[c885a21]172 sb_mixer_type_str(sb->mixer.type));
[03362fbd]173 ddf_fun_data_implant(mixer_fun, &sb->mixer);
174 ddf_fun_set_ops(mixer_fun, &sb_mixer_ops);
[25c98a8e]175
176 ret = ddf_fun_bind(mixer_fun);
177 if (ret != EOK) {
178 ddf_log_error(
[124f9bd]179 "Failed to bind mixer function: %s.", str_error(ret));
[03362fbd]180 // TODO implanted data
[25c98a8e]181 ddf_fun_destroy(mixer_fun);
[9808edd]182
183 ddf_fun_unbind(dsp_fun);
[03362fbd]184 // TODO implanted data
[9808edd]185 ddf_fun_destroy(dsp_fun);
[25c98a8e]186 return ret;
187 }
[9dd79bc7]188
[7a69340]189 return EOK;
190}
[e941bf8]191
[7de1988c]192int sb16_init_mpu(sb16_t *sb, addr_range_t *regs)
[7a69340]193{
[c885a21]194 sb->mpu_regs = NULL;
[7a69340]195 return ENOTSUP;
196}
[e941bf8]197
[c885a21]198void sb16_interrupt(sb16_t *sb)
[f14e6ea]199{
[c885a21]200 assert(sb);
[f14e6ea]201 /* The acknowledgment of interrupts on DSP version 4.xx is different;
202 * It can contain MPU-401 indicator and DMA16 transfers are acked
203 * differently */
[c885a21]204 if (sb->dsp.version.major >= 4) {
205 pio_write_8(&sb->regs->mixer_address, MIXER_IRQ_STATUS_ADDRESS);
206 const uint8_t irq_mask = pio_read_8(&sb->regs->mixer_data);
[f14e6ea]207 /* Third bit is MPU-401 interrupt */
208 if (irq_mask & 0x4) {
209 return;
210 }
[dce7e41]211 } else {
[124f9bd]212 ddf_log_debug("SB16 interrupt.");
[f14e6ea]213 }
[c885a21]214 sb_dsp_interrupt(&sb->dsp);
[f14e6ea]215}
Note: See TracBrowser for help on using the repository browser.