📄 echoaudio_dsp.c
字号:
/**************************************************************************** Copyright Echo Digital Audio Corporation (c) 1998 - 2004 All rights reserved www.echoaudio.com This file is part of Echo Digital Audio's generic driver library. Echo Digital Audio's generic driver library is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. ************************************************************************* Translation from C++ and adaptation for use in ALSA-Driver were made by Giuliano Pochini <pochini@shiny.it>****************************************************************************/#if PAGE_SIZE < 4096#error PAGE_SIZE is < 4k#endifstatic int restore_dsp_rettings(struct echoaudio *chip);/* Some vector commands involve the DSP reading or writing data to and from thecomm page; if you send one of these commands to the DSP, it will complete thecommand and then write a non-zero value to the Handshake field in thecomm page. This function waits for the handshake to show up. */static int wait_handshake(struct echoaudio *chip){ int i; /* Wait up to 20ms for the handshake from the DSP */ for (i = 0; i < HANDSHAKE_TIMEOUT; i++) { /* Look for the handshake value */ barrier(); if (chip->comm_page->handshake) { return 0; } udelay(1); } snd_printk(KERN_ERR "wait_handshake(): Timeout waiting for DSP\n"); return -EBUSY;}/* Much of the interaction between the DSP and the driver is done via vectorcommands; send_vector writes a vector command to the DSP. Typically, thiscauses the DSP to read or write fields in the comm page.PCI posting is not required thanks to the handshake logic. */static int send_vector(struct echoaudio *chip, u32 command){ int i; wmb(); /* Flush all pending writes before sending the command */ /* Wait up to 100ms for the "vector busy" bit to be off */ for (i = 0; i < VECTOR_BUSY_TIMEOUT; i++) { if (!(get_dsp_register(chip, CHI32_VECTOR_REG) & CHI32_VECTOR_BUSY)) { set_dsp_register(chip, CHI32_VECTOR_REG, command); /*if (i) DE_ACT(("send_vector time: %d\n", i));*/ return 0; } udelay(1); } DE_ACT((KERN_ERR "timeout on send_vector\n")); return -EBUSY;}/* write_dsp writes a 32-bit value to the DSP; this is used almostexclusively for loading the DSP. */static int write_dsp(struct echoaudio *chip, u32 data){ u32 status, i; for (i = 0; i < 10000000; i++) { /* timeout = 10s */ status = get_dsp_register(chip, CHI32_STATUS_REG); if ((status & CHI32_STATUS_HOST_WRITE_EMPTY) != 0) { set_dsp_register(chip, CHI32_DATA_REG, data); wmb(); /* write it immediately */ return 0; } udelay(1); cond_resched(); } chip->bad_board = TRUE; /* Set TRUE until DSP re-loaded */ DE_ACT((KERN_ERR "write_dsp: Set bad_board to TRUE\n")); return -EIO;}/* read_dsp reads a 32-bit value from the DSP; this is used almostexclusively for loading the DSP and checking the status of the ASIC. */static int read_dsp(struct echoaudio *chip, u32 *data){ u32 status, i; for (i = 0; i < READ_DSP_TIMEOUT; i++) { status = get_dsp_register(chip, CHI32_STATUS_REG); if ((status & CHI32_STATUS_HOST_READ_FULL) != 0) { *data = get_dsp_register(chip, CHI32_DATA_REG); return 0; } udelay(1); cond_resched(); } chip->bad_board = TRUE; /* Set TRUE until DSP re-loaded */ DE_INIT((KERN_ERR "read_dsp: Set bad_board to TRUE\n")); return -EIO;}/**************************************************************************** Firmware loading functions ****************************************************************************//* This function is used to read back the serial number from the DSP;this is triggered by the SET_COMMPAGE_ADDR command.Only some early Echogals products have serial numbers in the ROM;the serial number is not used, but you still need to do this aspart of the DSP load process. */static int read_sn(struct echoaudio *chip){ int i; u32 sn[6]; for (i = 0; i < 5; i++) { if (read_dsp(chip, &sn[i])) { snd_printk(KERN_ERR "Failed to read serial number\n"); return -EIO; } } DE_INIT(("Read serial number %08x %08x %08x %08x %08x\n", sn[0], sn[1], sn[2], sn[3], sn[4])); return 0;}#ifndef ECHOCARD_HAS_ASIC/* This card has no ASIC, just return ok */static inline int check_asic_status(struct echoaudio *chip){ chip->asic_loaded = TRUE; return 0;}#endif /* !ECHOCARD_HAS_ASIC */#ifdef ECHOCARD_HAS_ASIC/* Load ASIC code - done after the DSP is loaded */static int load_asic_generic(struct echoaudio *chip, u32 cmd, const struct firmware *asic){ const struct firmware *fw; int err; u32 i, size; u8 *code; if ((err = get_firmware(&fw, asic, chip)) < 0) { snd_printk(KERN_WARNING "Firmware not found !\n"); return err; } code = (u8 *)fw->data; size = fw->size; /* Send the "Here comes the ASIC" command */ if (write_dsp(chip, cmd) < 0) goto la_error; /* Write length of ASIC file in bytes */ if (write_dsp(chip, size) < 0) goto la_error; for (i = 0; i < size; i++) { if (write_dsp(chip, code[i]) < 0) goto la_error; } DE_INIT(("ASIC loaded\n")); free_firmware(fw); return 0;la_error: DE_INIT(("failed on write_dsp\n")); free_firmware(fw); return -EIO;}#endif /* ECHOCARD_HAS_ASIC */#ifdef DSP_56361/* Install the resident loader for 56361 DSPs; The resident loader is onthe EPROM on the board for 56301 DSP. The resident loader is a tiny littleprogram that is used to load the real DSP code. */static int install_resident_loader(struct echoaudio *chip){ u32 address; int index, words, i; u16 *code; u32 status; const struct firmware *fw; /* 56361 cards only! This check is required by the old 56301-based Mona and Gina24 */ if (chip->device_id != DEVICE_ID_56361) return 0; /* Look to see if the resident loader is present. If the resident loader is already installed, host flag 5 will be on. */ status = get_dsp_register(chip, CHI32_STATUS_REG); if (status & CHI32_STATUS_REG_HF5) { DE_INIT(("Resident loader already installed; status is 0x%x\n", status)); return 0; } if ((i = get_firmware(&fw, &card_fw[FW_361_LOADER], chip)) < 0) { snd_printk(KERN_WARNING "Firmware not found !\n"); return i; } /* The DSP code is an array of 16 bit words. The array is divided up into sections. The first word of each section is the size in words, followed by the section type. Since DSP addresses and data are 24 bits wide, they each take up two 16 bit words in the array. This is a lot like the other loader loop, but it's not a loop, you don't write the memory type, and you don't write a zero at the end. */ /* Set DSP format bits for 24 bit mode */ set_dsp_register(chip, CHI32_CONTROL_REG, get_dsp_register(chip, CHI32_CONTROL_REG) | 0x900); code = (u16 *)fw->data; /* Skip the header section; the first word in the array is the size of the first section, so the first real section of code is pointed to by Code[0]. */ index = code[0]; /* Skip the section size, LRS block type, and DSP memory type */ index += 3; /* Get the number of DSP words to write */ words = code[index++]; /* Get the DSP address for this block; 24 bits, so build from two words */ address = ((u32)code[index] << 16) + code[index + 1]; index += 2; /* Write the count to the DSP */ if (write_dsp(chip, words)) { DE_INIT(("install_resident_loader: Failed to write word count!\n")); goto irl_error; } /* Write the DSP address */ if (write_dsp(chip, address)) { DE_INIT(("install_resident_loader: Failed to write DSP address!\n")); goto irl_error; } /* Write out this block of code to the DSP */ for (i = 0; i < words; i++) { u32 data; data = ((u32)code[index] << 16) + code[index + 1]; if (write_dsp(chip, data)) { DE_INIT(("install_resident_loader: Failed to write DSP code\n")); goto irl_error; } index += 2; } /* Wait for flag 5 to come up */ for (i = 0; i < 200; i++) { /* Timeout is 50us * 200 = 10ms */ udelay(50); status = get_dsp_register(chip, CHI32_STATUS_REG); if (status & CHI32_STATUS_REG_HF5) break; } if (i == 200) { DE_INIT(("Resident loader failed to set HF5\n")); goto irl_error; } DE_INIT(("Resident loader successfully installed\n")); free_firmware(fw); return 0;irl_error: free_firmware(fw); return -EIO;}#endif /* DSP_56361 */static int load_dsp(struct echoaudio *chip, u16 *code){ u32 address, data; int index, words, i; if (chip->dsp_code == code) { DE_INIT(("DSP is already loaded!\n")); return 0; } chip->bad_board = TRUE; /* Set TRUE until DSP loaded */ chip->dsp_code = NULL; /* Current DSP code not loaded */ chip->asic_loaded = FALSE; /* Loading the DSP code will reset the ASIC */ DE_INIT(("load_dsp: Set bad_board to TRUE\n")); /* If this board requires a resident loader, install it. */#ifdef DSP_56361 if ((i = install_resident_loader(chip)) < 0) return i;#endif /* Send software reset command */ if (send_vector(chip, DSP_VC_RESET) < 0) { DE_INIT(("LoadDsp: send_vector DSP_VC_RESET failed, Critical Failure\n")); return -EIO; } /* Delay 10us */ udelay(10); /* Wait 10ms for HF3 to indicate that software reset is complete */ for (i = 0; i < 1000; i++) { /* Timeout is 10us * 1000 = 10ms */ if (get_dsp_register(chip, CHI32_STATUS_REG) & CHI32_STATUS_REG_HF3) break; udelay(10); } if (i == 1000) { DE_INIT(("load_dsp: Timeout waiting for CHI32_STATUS_REG_HF3\n")); return -EIO; } /* Set DSP format bits for 24 bit mode now that soft reset is done */ set_dsp_register(chip, CHI32_CONTROL_REG, get_dsp_register(chip, CHI32_CONTROL_REG) | 0x900); /* Main loader loop */ index = code[0]; for (;;) { int block_type, mem_type; /* Total Block Size */ index++; /* Block Type */ block_type = code[index]; if (block_type == 4) /* We're finished */ break; index++; /* Memory Type P=0,X=1,Y=2 */ mem_type = code[index++]; /* Block Code Size */ words = code[index++]; if (words == 0) /* We're finished */ break; /* Start Address */ address = ((u32)code[index] << 16) + code[index + 1]; index += 2; if (write_dsp(chip, words) < 0) { DE_INIT(("load_dsp: failed to write number of DSP words\n")); return -EIO; } if (write_dsp(chip, address) < 0) { DE_INIT(("load_dsp: failed to write DSP address\n")); return -EIO; } if (write_dsp(chip, mem_type) < 0) { DE_INIT(("load_dsp: failed to write DSP memory type\n")); return -EIO; } /* Code */ for (i = 0; i < words; i++, index+=2) { data = ((u32)code[index] << 16) + code[index + 1]; if (write_dsp(chip, data) < 0) { DE_INIT(("load_dsp: failed to write DSP data\n")); return -EIO; } } } if (write_dsp(chip, 0) < 0) { /* We're done!!! */ DE_INIT(("load_dsp: Failed to write final zero\n")); return -EIO; } udelay(10); for (i = 0; i < 5000; i++) { /* Timeout is 100us * 5000 = 500ms */ /* Wait for flag 4 - indicates that the DSP loaded OK */ if (get_dsp_register(chip, CHI32_STATUS_REG) & CHI32_STATUS_REG_HF4) { set_dsp_register(chip, CHI32_CONTROL_REG, get_dsp_register(chip, CHI32_CONTROL_REG) & ~0x1b00); if (write_dsp(chip, DSP_FNC_SET_COMMPAGE_ADDR) < 0) { DE_INIT(("load_dsp: Failed to write DSP_FNC_SET_COMMPAGE_ADDR\n")); return -EIO; } if (write_dsp(chip, chip->comm_page_phys) < 0) { DE_INIT(("load_dsp: Failed to write comm page address\n")); return -EIO; } /* Get the serial number via slave mode. This is triggered by the SET_COMMPAGE_ADDR command. We don't actually use the serial number but we have to get it as part of the DSP init voodoo. */ if (read_sn(chip) < 0) { DE_INIT(("load_dsp: Failed to read serial number\n")); return -EIO; } chip->dsp_code = code; /* Show which DSP code loaded */ chip->bad_board = FALSE; /* DSP OK */ DE_INIT(("load_dsp: OK!\n")); return 0; } udelay(100); } DE_INIT(("load_dsp: DSP load timed out waiting for HF4\n")); return -EIO;}/* load_firmware takes care of loading the DSP and any ASIC code. */static int load_firmware(struct echoaudio *chip){ const struct firmware *fw; int box_type, err; snd_assert(chip->dsp_code_to_load && chip->comm_page, return -EPERM); /* See if the ASIC is present and working - only if the DSP is already loaded */ if (chip->dsp_code) { if ((box_type = check_asic_status(chip)) >= 0) return box_type; /* ASIC check failed; force the DSP to reload */ chip->dsp_code = NULL; } if ((err = get_firmware(&fw, chip->dsp_code_to_load, chip)) < 0) return err; err = load_dsp(chip, (u16 *)fw->data); free_firmware(fw); if (err < 0) return err; if ((box_type = load_asic(chip)) < 0) return box_type; /* error */ if ((err = restore_dsp_rettings(chip)) < 0) return err; return box_type;}/**************************************************************************** Mixer functions ****************************************************************************/#if defined(ECHOCARD_HAS_INPUT_NOMINAL_LEVEL) || \ defined(ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL)/* Set the nominal level for an input or output bus (true = -10dBV, false = +4dBu) */static int set_nominal_level(struct echoaudio *chip, u16 index, char consumer){ snd_assert(index < num_busses_out(chip) + num_busses_in(chip), return -EINVAL); /* Wait for the handshake (OK even if ASIC is not loaded) */ if (wait_handshake(chip)) return -EIO; chip->nominal_level[index] = consumer; if (consumer) chip->comm_page->nominal_level_mask |= cpu_to_le32(1 << index); else chip->comm_page->nominal_level_mask &= ~cpu_to_le32(1 << index); return 0;}#endif /* ECHOCARD_HAS_*_NOMINAL_LEVEL *//* Set the gain for a single physical output channel (dB). */static int set_output_gain(struct echoaudio *chip, u16 channel, s8 gain){ snd_assert(channel < num_busses_out(chip), return -EINVAL); if (wait_handshake(chip)) return -EIO; /* Save the new value */ chip->output_gain[channel] = gain; chip->comm_page->line_out_level[channel] = gain; return 0;}#ifdef ECHOCARD_HAS_MONITOR/* Set the monitor level from an input bus to an output bus. */static int set_monitor_gain(struct echoaudio *chip, u16 output, u16 input, s8 gain){ snd_assert(output < num_busses_out(chip) && input < num_busses_in(chip), return -EINVAL); if (wait_handshake(chip)) return -EIO; chip->monitor_gain[output][input] = gain;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -