xref: /openbmc/linux/sound/pci/echoaudio/echoaudio_dsp.c (revision 2a833a02a12b1dbb605739d589d11b4400c2078c)
1dd7b254dSGiuliano Pochini /****************************************************************************
2dd7b254dSGiuliano Pochini 
3dd7b254dSGiuliano Pochini    Copyright Echo Digital Audio Corporation (c) 1998 - 2004
4dd7b254dSGiuliano Pochini    All rights reserved
5dd7b254dSGiuliano Pochini    www.echoaudio.com
6dd7b254dSGiuliano Pochini 
7dd7b254dSGiuliano Pochini    This file is part of Echo Digital Audio's generic driver library.
8dd7b254dSGiuliano Pochini 
9dd7b254dSGiuliano Pochini    Echo Digital Audio's generic driver library is free software;
10dd7b254dSGiuliano Pochini    you can redistribute it and/or modify it under the terms of
11dd7b254dSGiuliano Pochini    the GNU General Public License as published by the Free Software
12dd7b254dSGiuliano Pochini    Foundation.
13dd7b254dSGiuliano Pochini 
14dd7b254dSGiuliano Pochini    This program is distributed in the hope that it will be useful,
15dd7b254dSGiuliano Pochini    but WITHOUT ANY WARRANTY; without even the implied warranty of
16dd7b254dSGiuliano Pochini    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17dd7b254dSGiuliano Pochini    GNU General Public License for more details.
18dd7b254dSGiuliano Pochini 
19dd7b254dSGiuliano Pochini    You should have received a copy of the GNU General Public License
20dd7b254dSGiuliano Pochini    along with this program; if not, write to the Free Software
21dd7b254dSGiuliano Pochini    Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22dd7b254dSGiuliano Pochini    MA  02111-1307, USA.
23dd7b254dSGiuliano Pochini 
24dd7b254dSGiuliano Pochini    *************************************************************************
25dd7b254dSGiuliano Pochini 
26dd7b254dSGiuliano Pochini  Translation from C++ and adaptation for use in ALSA-Driver
27dd7b254dSGiuliano Pochini  were made by Giuliano Pochini <pochini@shiny.it>
28dd7b254dSGiuliano Pochini 
29dd7b254dSGiuliano Pochini ****************************************************************************/
30dd7b254dSGiuliano Pochini 
31dd7b254dSGiuliano Pochini #if PAGE_SIZE < 4096
32dd7b254dSGiuliano Pochini #error PAGE_SIZE is < 4k
33dd7b254dSGiuliano Pochini #endif
34dd7b254dSGiuliano Pochini 
35dd7b254dSGiuliano Pochini static int restore_dsp_rettings(struct echoaudio *chip);
36dd7b254dSGiuliano Pochini 
37dd7b254dSGiuliano Pochini 
38dd7b254dSGiuliano Pochini /* Some vector commands involve the DSP reading or writing data to and from the
39dd7b254dSGiuliano Pochini comm page; if you send one of these commands to the DSP, it will complete the
40dd7b254dSGiuliano Pochini command and then write a non-zero value to the Handshake field in the
41dd7b254dSGiuliano Pochini comm page.  This function waits for the handshake to show up. */
42dd7b254dSGiuliano Pochini static int wait_handshake(struct echoaudio *chip)
43dd7b254dSGiuliano Pochini {
44dd7b254dSGiuliano Pochini 	int i;
45dd7b254dSGiuliano Pochini 
4622d3a200SGiuliano Pochini 	/* Wait up to 20ms for the handshake from the DSP */
47dd7b254dSGiuliano Pochini 	for (i = 0; i < HANDSHAKE_TIMEOUT; i++) {
48dd7b254dSGiuliano Pochini 		/* Look for the handshake value */
4922d3a200SGiuliano Pochini 		barrier();
50dd7b254dSGiuliano Pochini 		if (chip->comm_page->handshake) {
51dd7b254dSGiuliano Pochini 			return 0;
52dd7b254dSGiuliano Pochini 		}
53dd7b254dSGiuliano Pochini 		udelay(1);
54dd7b254dSGiuliano Pochini 	}
55dd7b254dSGiuliano Pochini 
56ece7a36dSTakashi Iwai 	dev_err(chip->card->dev, "wait_handshake(): Timeout waiting for DSP\n");
57dd7b254dSGiuliano Pochini 	return -EBUSY;
58dd7b254dSGiuliano Pochini }
59dd7b254dSGiuliano Pochini 
60dd7b254dSGiuliano Pochini 
61dd7b254dSGiuliano Pochini 
62dd7b254dSGiuliano Pochini /* Much of the interaction between the DSP and the driver is done via vector
63dd7b254dSGiuliano Pochini commands; send_vector writes a vector command to the DSP.  Typically, this
64dd7b254dSGiuliano Pochini causes the DSP to read or write fields in the comm page.
65dd7b254dSGiuliano Pochini PCI posting is not required thanks to the handshake logic. */
66dd7b254dSGiuliano Pochini static int send_vector(struct echoaudio *chip, u32 command)
67dd7b254dSGiuliano Pochini {
68dd7b254dSGiuliano Pochini 	int i;
69dd7b254dSGiuliano Pochini 
70dd7b254dSGiuliano Pochini 	wmb();	/* Flush all pending writes before sending the command */
71dd7b254dSGiuliano Pochini 
72dd7b254dSGiuliano Pochini 	/* Wait up to 100ms for the "vector busy" bit to be off */
73dd7b254dSGiuliano Pochini 	for (i = 0; i < VECTOR_BUSY_TIMEOUT; i++) {
74dd7b254dSGiuliano Pochini 		if (!(get_dsp_register(chip, CHI32_VECTOR_REG) &
75dd7b254dSGiuliano Pochini 		      CHI32_VECTOR_BUSY)) {
76dd7b254dSGiuliano Pochini 			set_dsp_register(chip, CHI32_VECTOR_REG, command);
77dd7b254dSGiuliano Pochini 			/*if (i)  DE_ACT(("send_vector time: %d\n", i));*/
78dd7b254dSGiuliano Pochini 			return 0;
79dd7b254dSGiuliano Pochini 		}
80dd7b254dSGiuliano Pochini 		udelay(1);
81dd7b254dSGiuliano Pochini 	}
82dd7b254dSGiuliano Pochini 
83b5b4a41bSSudip Mukherjee 	dev_err(chip->card->dev, "timeout on send_vector\n");
84dd7b254dSGiuliano Pochini 	return -EBUSY;
85dd7b254dSGiuliano Pochini }
86dd7b254dSGiuliano Pochini 
87dd7b254dSGiuliano Pochini 
88dd7b254dSGiuliano Pochini 
89dd7b254dSGiuliano Pochini /* write_dsp writes a 32-bit value to the DSP; this is used almost
90dd7b254dSGiuliano Pochini exclusively for loading the DSP. */
91dd7b254dSGiuliano Pochini static int write_dsp(struct echoaudio *chip, u32 data)
92dd7b254dSGiuliano Pochini {
93dd7b254dSGiuliano Pochini 	u32 status, i;
94dd7b254dSGiuliano Pochini 
95dd7b254dSGiuliano Pochini 	for (i = 0; i < 10000000; i++) {	/* timeout = 10s */
96dd7b254dSGiuliano Pochini 		status = get_dsp_register(chip, CHI32_STATUS_REG);
97dd7b254dSGiuliano Pochini 		if ((status & CHI32_STATUS_HOST_WRITE_EMPTY) != 0) {
98dd7b254dSGiuliano Pochini 			set_dsp_register(chip, CHI32_DATA_REG, data);
99dd7b254dSGiuliano Pochini 			wmb();			/* write it immediately */
100dd7b254dSGiuliano Pochini 			return 0;
101dd7b254dSGiuliano Pochini 		}
102dd7b254dSGiuliano Pochini 		udelay(1);
103dd7b254dSGiuliano Pochini 		cond_resched();
104dd7b254dSGiuliano Pochini 	}
105dd7b254dSGiuliano Pochini 
1063f6175ecSMark Brown 	chip->bad_board = true;		/* Set true until DSP re-loaded */
1073f6175ecSMark Brown 	dev_dbg(chip->card->dev, "write_dsp: Set bad_board to true\n");
108dd7b254dSGiuliano Pochini 	return -EIO;
109dd7b254dSGiuliano Pochini }
110dd7b254dSGiuliano Pochini 
111dd7b254dSGiuliano Pochini 
112dd7b254dSGiuliano Pochini 
113dd7b254dSGiuliano Pochini /* read_dsp reads a 32-bit value from the DSP; this is used almost
114dd7b254dSGiuliano Pochini exclusively for loading the DSP and checking the status of the ASIC. */
115dd7b254dSGiuliano Pochini static int read_dsp(struct echoaudio *chip, u32 *data)
116dd7b254dSGiuliano Pochini {
117dd7b254dSGiuliano Pochini 	u32 status, i;
118dd7b254dSGiuliano Pochini 
119dd7b254dSGiuliano Pochini 	for (i = 0; i < READ_DSP_TIMEOUT; i++) {
120dd7b254dSGiuliano Pochini 		status = get_dsp_register(chip, CHI32_STATUS_REG);
121dd7b254dSGiuliano Pochini 		if ((status & CHI32_STATUS_HOST_READ_FULL) != 0) {
122dd7b254dSGiuliano Pochini 			*data = get_dsp_register(chip, CHI32_DATA_REG);
123dd7b254dSGiuliano Pochini 			return 0;
124dd7b254dSGiuliano Pochini 		}
125dd7b254dSGiuliano Pochini 		udelay(1);
126dd7b254dSGiuliano Pochini 		cond_resched();
127dd7b254dSGiuliano Pochini 	}
128dd7b254dSGiuliano Pochini 
1293f6175ecSMark Brown 	chip->bad_board = true;		/* Set true until DSP re-loaded */
1303f6175ecSMark Brown 	dev_err(chip->card->dev, "read_dsp: Set bad_board to true\n");
131dd7b254dSGiuliano Pochini 	return -EIO;
132dd7b254dSGiuliano Pochini }
133dd7b254dSGiuliano Pochini 
134dd7b254dSGiuliano Pochini 
135dd7b254dSGiuliano Pochini 
136dd7b254dSGiuliano Pochini /****************************************************************************
137dd7b254dSGiuliano Pochini 	Firmware loading functions
138dd7b254dSGiuliano Pochini  ****************************************************************************/
139dd7b254dSGiuliano Pochini 
140dd7b254dSGiuliano Pochini /* This function is used to read back the serial number from the DSP;
141dd7b254dSGiuliano Pochini this is triggered by the SET_COMMPAGE_ADDR command.
142dd7b254dSGiuliano Pochini Only some early Echogals products have serial numbers in the ROM;
143dd7b254dSGiuliano Pochini the serial number is not used, but you still need to do this as
144dd7b254dSGiuliano Pochini part of the DSP load process. */
145dd7b254dSGiuliano Pochini static int read_sn(struct echoaudio *chip)
146dd7b254dSGiuliano Pochini {
147dd7b254dSGiuliano Pochini 	int i;
148dd7b254dSGiuliano Pochini 	u32 sn[6];
149dd7b254dSGiuliano Pochini 
150dd7b254dSGiuliano Pochini 	for (i = 0; i < 5; i++) {
151dd7b254dSGiuliano Pochini 		if (read_dsp(chip, &sn[i])) {
152ece7a36dSTakashi Iwai 			dev_err(chip->card->dev,
153ece7a36dSTakashi Iwai 				"Failed to read serial number\n");
154dd7b254dSGiuliano Pochini 			return -EIO;
155dd7b254dSGiuliano Pochini 		}
156dd7b254dSGiuliano Pochini 	}
157b5b4a41bSSudip Mukherjee 	dev_dbg(chip->card->dev,
158b5b4a41bSSudip Mukherjee 		"Read serial number %08x %08x %08x %08x %08x\n",
159b5b4a41bSSudip Mukherjee 		 sn[0], sn[1], sn[2], sn[3], sn[4]);
160dd7b254dSGiuliano Pochini 	return 0;
161dd7b254dSGiuliano Pochini }
162dd7b254dSGiuliano Pochini 
163dd7b254dSGiuliano Pochini 
164dd7b254dSGiuliano Pochini 
165dd7b254dSGiuliano Pochini #ifndef ECHOCARD_HAS_ASIC
166dd7b254dSGiuliano Pochini /* This card has no ASIC, just return ok */
167dd7b254dSGiuliano Pochini static inline int check_asic_status(struct echoaudio *chip)
168dd7b254dSGiuliano Pochini {
1693f6175ecSMark Brown 	chip->asic_loaded = true;
170dd7b254dSGiuliano Pochini 	return 0;
171dd7b254dSGiuliano Pochini }
172dd7b254dSGiuliano Pochini 
173dd7b254dSGiuliano Pochini #endif /* !ECHOCARD_HAS_ASIC */
174dd7b254dSGiuliano Pochini 
175dd7b254dSGiuliano Pochini 
176dd7b254dSGiuliano Pochini 
177dd7b254dSGiuliano Pochini #ifdef ECHOCARD_HAS_ASIC
178dd7b254dSGiuliano Pochini 
179dd7b254dSGiuliano Pochini /* Load ASIC code - done after the DSP is loaded */
18019b50063SGiuliano Pochini static int load_asic_generic(struct echoaudio *chip, u32 cmd, short asic)
181dd7b254dSGiuliano Pochini {
182dd7b254dSGiuliano Pochini 	const struct firmware *fw;
183dd7b254dSGiuliano Pochini 	int err;
184dd7b254dSGiuliano Pochini 	u32 i, size;
185dd7b254dSGiuliano Pochini 	u8 *code;
186dd7b254dSGiuliano Pochini 
18719b50063SGiuliano Pochini 	err = get_firmware(&fw, chip, asic);
18819b50063SGiuliano Pochini 	if (err < 0) {
189ece7a36dSTakashi Iwai 		dev_warn(chip->card->dev, "Firmware not found !\n");
190dd7b254dSGiuliano Pochini 		return err;
191dd7b254dSGiuliano Pochini 	}
192dd7b254dSGiuliano Pochini 
193dd7b254dSGiuliano Pochini 	code = (u8 *)fw->data;
194dd7b254dSGiuliano Pochini 	size = fw->size;
195dd7b254dSGiuliano Pochini 
196dd7b254dSGiuliano Pochini 	/* Send the "Here comes the ASIC" command */
197dd7b254dSGiuliano Pochini 	if (write_dsp(chip, cmd) < 0)
198dd7b254dSGiuliano Pochini 		goto la_error;
199dd7b254dSGiuliano Pochini 
200dd7b254dSGiuliano Pochini 	/* Write length of ASIC file in bytes */
201dd7b254dSGiuliano Pochini 	if (write_dsp(chip, size) < 0)
202dd7b254dSGiuliano Pochini 		goto la_error;
203dd7b254dSGiuliano Pochini 
204dd7b254dSGiuliano Pochini 	for (i = 0; i < size; i++) {
205dd7b254dSGiuliano Pochini 		if (write_dsp(chip, code[i]) < 0)
206dd7b254dSGiuliano Pochini 			goto la_error;
207dd7b254dSGiuliano Pochini 	}
208dd7b254dSGiuliano Pochini 
209e3690869SSudip Mukherjee 	free_firmware(fw, chip);
210dd7b254dSGiuliano Pochini 	return 0;
211dd7b254dSGiuliano Pochini 
212dd7b254dSGiuliano Pochini la_error:
213b5b4a41bSSudip Mukherjee 	dev_err(chip->card->dev, "failed on write_dsp\n");
214e3690869SSudip Mukherjee 	free_firmware(fw, chip);
215dd7b254dSGiuliano Pochini 	return -EIO;
216dd7b254dSGiuliano Pochini }
217dd7b254dSGiuliano Pochini 
218dd7b254dSGiuliano Pochini #endif /* ECHOCARD_HAS_ASIC */
219dd7b254dSGiuliano Pochini 
220dd7b254dSGiuliano Pochini 
221dd7b254dSGiuliano Pochini 
222dd7b254dSGiuliano Pochini #ifdef DSP_56361
223dd7b254dSGiuliano Pochini 
224dd7b254dSGiuliano Pochini /* Install the resident loader for 56361 DSPs;  The resident loader is on
225dd7b254dSGiuliano Pochini the EPROM on the board for 56301 DSP. The resident loader is a tiny little
226dd7b254dSGiuliano Pochini program that is used to load the real DSP code. */
227dd7b254dSGiuliano Pochini static int install_resident_loader(struct echoaudio *chip)
228dd7b254dSGiuliano Pochini {
229dd7b254dSGiuliano Pochini 	u32 address;
230dd7b254dSGiuliano Pochini 	int index, words, i;
231dd7b254dSGiuliano Pochini 	u16 *code;
232dd7b254dSGiuliano Pochini 	u32 status;
233dd7b254dSGiuliano Pochini 	const struct firmware *fw;
234dd7b254dSGiuliano Pochini 
235dd7b254dSGiuliano Pochini 	/* 56361 cards only!  This check is required by the old 56301-based
236dd7b254dSGiuliano Pochini 	Mona and Gina24 */
237dd7b254dSGiuliano Pochini 	if (chip->device_id != DEVICE_ID_56361)
238dd7b254dSGiuliano Pochini 		return 0;
239dd7b254dSGiuliano Pochini 
240dd7b254dSGiuliano Pochini 	/* Look to see if the resident loader is present.  If the resident
241dd7b254dSGiuliano Pochini 	loader is already installed, host flag 5 will be on. */
242dd7b254dSGiuliano Pochini 	status = get_dsp_register(chip, CHI32_STATUS_REG);
243dd7b254dSGiuliano Pochini 	if (status & CHI32_STATUS_REG_HF5) {
244b5b4a41bSSudip Mukherjee 		dev_dbg(chip->card->dev,
245b5b4a41bSSudip Mukherjee 			"Resident loader already installed; status is 0x%x\n",
246b5b4a41bSSudip Mukherjee 			 status);
247dd7b254dSGiuliano Pochini 		return 0;
248dd7b254dSGiuliano Pochini 	}
249dd7b254dSGiuliano Pochini 
25019b50063SGiuliano Pochini 	i = get_firmware(&fw, chip, FW_361_LOADER);
25119b50063SGiuliano Pochini 	if (i < 0) {
252ece7a36dSTakashi Iwai 		dev_warn(chip->card->dev, "Firmware not found !\n");
253dd7b254dSGiuliano Pochini 		return i;
254dd7b254dSGiuliano Pochini 	}
255dd7b254dSGiuliano Pochini 
256dd7b254dSGiuliano Pochini 	/* The DSP code is an array of 16 bit words.  The array is divided up
257dd7b254dSGiuliano Pochini 	into sections.  The first word of each section is the size in words,
258dd7b254dSGiuliano Pochini 	followed by the section type.
259dd7b254dSGiuliano Pochini 	Since DSP addresses and data are 24 bits wide, they each take up two
260dd7b254dSGiuliano Pochini 	16 bit words in the array.
261dd7b254dSGiuliano Pochini 	This is a lot like the other loader loop, but it's not a loop, you
262dd7b254dSGiuliano Pochini 	don't write the memory type, and you don't write a zero at the end. */
263dd7b254dSGiuliano Pochini 
264dd7b254dSGiuliano Pochini 	/* Set DSP format bits for 24 bit mode */
265dd7b254dSGiuliano Pochini 	set_dsp_register(chip, CHI32_CONTROL_REG,
266dd7b254dSGiuliano Pochini 			 get_dsp_register(chip, CHI32_CONTROL_REG) | 0x900);
267dd7b254dSGiuliano Pochini 
268dd7b254dSGiuliano Pochini 	code = (u16 *)fw->data;
269dd7b254dSGiuliano Pochini 
270dd7b254dSGiuliano Pochini 	/* Skip the header section; the first word in the array is the size
271dd7b254dSGiuliano Pochini 	of the first section, so the first real section of code is pointed
272dd7b254dSGiuliano Pochini 	to by Code[0]. */
273dd7b254dSGiuliano Pochini 	index = code[0];
274dd7b254dSGiuliano Pochini 
275dd7b254dSGiuliano Pochini 	/* Skip the section size, LRS block type, and DSP memory type */
276dd7b254dSGiuliano Pochini 	index += 3;
277dd7b254dSGiuliano Pochini 
278dd7b254dSGiuliano Pochini 	/* Get the number of DSP words to write */
279dd7b254dSGiuliano Pochini 	words = code[index++];
280dd7b254dSGiuliano Pochini 
281dd7b254dSGiuliano Pochini 	/* Get the DSP address for this block; 24 bits, so build from two words */
282dd7b254dSGiuliano Pochini 	address = ((u32)code[index] << 16) + code[index + 1];
283dd7b254dSGiuliano Pochini 	index += 2;
284dd7b254dSGiuliano Pochini 
285dd7b254dSGiuliano Pochini 	/* Write the count to the DSP */
286dd7b254dSGiuliano Pochini 	if (write_dsp(chip, words)) {
287b5b4a41bSSudip Mukherjee 		dev_err(chip->card->dev,
288b5b4a41bSSudip Mukherjee 			"install_resident_loader: Failed to write word count!\n");
289dd7b254dSGiuliano Pochini 		goto irl_error;
290dd7b254dSGiuliano Pochini 	}
291dd7b254dSGiuliano Pochini 	/* Write the DSP address */
292dd7b254dSGiuliano Pochini 	if (write_dsp(chip, address)) {
293b5b4a41bSSudip Mukherjee 		dev_err(chip->card->dev,
294b5b4a41bSSudip Mukherjee 			"install_resident_loader: Failed to write DSP address!\n");
295dd7b254dSGiuliano Pochini 		goto irl_error;
296dd7b254dSGiuliano Pochini 	}
297dd7b254dSGiuliano Pochini 	/* Write out this block of code to the DSP */
298dd7b254dSGiuliano Pochini 	for (i = 0; i < words; i++) {
299dd7b254dSGiuliano Pochini 		u32 data;
300dd7b254dSGiuliano Pochini 
301dd7b254dSGiuliano Pochini 		data = ((u32)code[index] << 16) + code[index + 1];
302dd7b254dSGiuliano Pochini 		if (write_dsp(chip, data)) {
303b5b4a41bSSudip Mukherjee 			dev_err(chip->card->dev,
304b5b4a41bSSudip Mukherjee 				"install_resident_loader: Failed to write DSP code\n");
305dd7b254dSGiuliano Pochini 			goto irl_error;
306dd7b254dSGiuliano Pochini 		}
307dd7b254dSGiuliano Pochini 		index += 2;
308dd7b254dSGiuliano Pochini 	}
309dd7b254dSGiuliano Pochini 
310dd7b254dSGiuliano Pochini 	/* Wait for flag 5 to come up */
311dd7b254dSGiuliano Pochini 	for (i = 0; i < 200; i++) {	/* Timeout is 50us * 200 = 10ms */
312dd7b254dSGiuliano Pochini 		udelay(50);
313dd7b254dSGiuliano Pochini 		status = get_dsp_register(chip, CHI32_STATUS_REG);
314dd7b254dSGiuliano Pochini 		if (status & CHI32_STATUS_REG_HF5)
315dd7b254dSGiuliano Pochini 			break;
316dd7b254dSGiuliano Pochini 	}
317dd7b254dSGiuliano Pochini 
318dd7b254dSGiuliano Pochini 	if (i == 200) {
319b5b4a41bSSudip Mukherjee 		dev_err(chip->card->dev, "Resident loader failed to set HF5\n");
320dd7b254dSGiuliano Pochini 		goto irl_error;
321dd7b254dSGiuliano Pochini 	}
322dd7b254dSGiuliano Pochini 
323b5b4a41bSSudip Mukherjee 	dev_dbg(chip->card->dev, "Resident loader successfully installed\n");
324e3690869SSudip Mukherjee 	free_firmware(fw, chip);
325dd7b254dSGiuliano Pochini 	return 0;
326dd7b254dSGiuliano Pochini 
327dd7b254dSGiuliano Pochini irl_error:
328e3690869SSudip Mukherjee 	free_firmware(fw, chip);
329dd7b254dSGiuliano Pochini 	return -EIO;
330dd7b254dSGiuliano Pochini }
331dd7b254dSGiuliano Pochini 
332dd7b254dSGiuliano Pochini #endif /* DSP_56361 */
333dd7b254dSGiuliano Pochini 
334dd7b254dSGiuliano Pochini 
335dd7b254dSGiuliano Pochini static int load_dsp(struct echoaudio *chip, u16 *code)
336dd7b254dSGiuliano Pochini {
337dd7b254dSGiuliano Pochini 	u32 address, data;
338dd7b254dSGiuliano Pochini 	int index, words, i;
339dd7b254dSGiuliano Pochini 
340dd7b254dSGiuliano Pochini 	if (chip->dsp_code == code) {
341b5b4a41bSSudip Mukherjee 		dev_warn(chip->card->dev, "DSP is already loaded!\n");
342dd7b254dSGiuliano Pochini 		return 0;
343dd7b254dSGiuliano Pochini 	}
3443f6175ecSMark Brown 	chip->bad_board = true;		/* Set true until DSP loaded */
345dd7b254dSGiuliano Pochini 	chip->dsp_code = NULL;		/* Current DSP code not loaded */
3463f6175ecSMark Brown 	chip->asic_loaded = false;	/* Loading the DSP code will reset the ASIC */
347dd7b254dSGiuliano Pochini 
3483f6175ecSMark Brown 	dev_dbg(chip->card->dev, "load_dsp: Set bad_board to true\n");
349dd7b254dSGiuliano Pochini 
350dd7b254dSGiuliano Pochini 	/* If this board requires a resident loader, install it. */
351dd7b254dSGiuliano Pochini #ifdef DSP_56361
352dd7b254dSGiuliano Pochini 	if ((i = install_resident_loader(chip)) < 0)
353dd7b254dSGiuliano Pochini 		return i;
354dd7b254dSGiuliano Pochini #endif
355dd7b254dSGiuliano Pochini 
356dd7b254dSGiuliano Pochini 	/* Send software reset command */
357dd7b254dSGiuliano Pochini 	if (send_vector(chip, DSP_VC_RESET) < 0) {
358b5b4a41bSSudip Mukherjee 		dev_err(chip->card->dev,
359b5b4a41bSSudip Mukherjee 			"LoadDsp: send_vector DSP_VC_RESET failed, Critical Failure\n");
360dd7b254dSGiuliano Pochini 		return -EIO;
361dd7b254dSGiuliano Pochini 	}
362dd7b254dSGiuliano Pochini 	/* Delay 10us */
363dd7b254dSGiuliano Pochini 	udelay(10);
364dd7b254dSGiuliano Pochini 
365dd7b254dSGiuliano Pochini 	/* Wait 10ms for HF3 to indicate that software reset is complete */
366dd7b254dSGiuliano Pochini 	for (i = 0; i < 1000; i++) {	/* Timeout is 10us * 1000 = 10ms */
367dd7b254dSGiuliano Pochini 		if (get_dsp_register(chip, CHI32_STATUS_REG) &
368dd7b254dSGiuliano Pochini 		    CHI32_STATUS_REG_HF3)
369dd7b254dSGiuliano Pochini 			break;
370dd7b254dSGiuliano Pochini 		udelay(10);
371dd7b254dSGiuliano Pochini 	}
372dd7b254dSGiuliano Pochini 
373dd7b254dSGiuliano Pochini 	if (i == 1000) {
374b5b4a41bSSudip Mukherjee 		dev_err(chip->card->dev,
375b5b4a41bSSudip Mukherjee 			"load_dsp: Timeout waiting for CHI32_STATUS_REG_HF3\n");
376dd7b254dSGiuliano Pochini 		return -EIO;
377dd7b254dSGiuliano Pochini 	}
378dd7b254dSGiuliano Pochini 
379dd7b254dSGiuliano Pochini 	/* Set DSP format bits for 24 bit mode now that soft reset is done */
380dd7b254dSGiuliano Pochini 	set_dsp_register(chip, CHI32_CONTROL_REG,
381dd7b254dSGiuliano Pochini 			 get_dsp_register(chip, CHI32_CONTROL_REG) | 0x900);
382dd7b254dSGiuliano Pochini 
383dd7b254dSGiuliano Pochini 	/* Main loader loop */
384dd7b254dSGiuliano Pochini 
385dd7b254dSGiuliano Pochini 	index = code[0];
386dd7b254dSGiuliano Pochini 	for (;;) {
387dd7b254dSGiuliano Pochini 		int block_type, mem_type;
388dd7b254dSGiuliano Pochini 
389dd7b254dSGiuliano Pochini 		/* Total Block Size */
390dd7b254dSGiuliano Pochini 		index++;
391dd7b254dSGiuliano Pochini 
392dd7b254dSGiuliano Pochini 		/* Block Type */
393dd7b254dSGiuliano Pochini 		block_type = code[index];
394dd7b254dSGiuliano Pochini 		if (block_type == 4)	/* We're finished */
395dd7b254dSGiuliano Pochini 			break;
396dd7b254dSGiuliano Pochini 
397dd7b254dSGiuliano Pochini 		index++;
398dd7b254dSGiuliano Pochini 
399dd7b254dSGiuliano Pochini 		/* Memory Type  P=0,X=1,Y=2 */
400dd7b254dSGiuliano Pochini 		mem_type = code[index++];
401dd7b254dSGiuliano Pochini 
402dd7b254dSGiuliano Pochini 		/* Block Code Size */
403dd7b254dSGiuliano Pochini 		words = code[index++];
404dd7b254dSGiuliano Pochini 		if (words == 0)		/* We're finished */
405dd7b254dSGiuliano Pochini 			break;
406dd7b254dSGiuliano Pochini 
407dd7b254dSGiuliano Pochini 		/* Start Address */
408dd7b254dSGiuliano Pochini 		address = ((u32)code[index] << 16) + code[index + 1];
409dd7b254dSGiuliano Pochini 		index += 2;
410dd7b254dSGiuliano Pochini 
411dd7b254dSGiuliano Pochini 		if (write_dsp(chip, words) < 0) {
412b5b4a41bSSudip Mukherjee 			dev_err(chip->card->dev,
413b5b4a41bSSudip Mukherjee 				"load_dsp: failed to write number of DSP words\n");
414dd7b254dSGiuliano Pochini 			return -EIO;
415dd7b254dSGiuliano Pochini 		}
416dd7b254dSGiuliano Pochini 		if (write_dsp(chip, address) < 0) {
417b5b4a41bSSudip Mukherjee 			dev_err(chip->card->dev,
418b5b4a41bSSudip Mukherjee 				"load_dsp: failed to write DSP address\n");
419dd7b254dSGiuliano Pochini 			return -EIO;
420dd7b254dSGiuliano Pochini 		}
421dd7b254dSGiuliano Pochini 		if (write_dsp(chip, mem_type) < 0) {
422b5b4a41bSSudip Mukherjee 			dev_err(chip->card->dev,
423b5b4a41bSSudip Mukherjee 				"load_dsp: failed to write DSP memory type\n");
424dd7b254dSGiuliano Pochini 			return -EIO;
425dd7b254dSGiuliano Pochini 		}
426dd7b254dSGiuliano Pochini 		/* Code */
427dd7b254dSGiuliano Pochini 		for (i = 0; i < words; i++, index+=2) {
428dd7b254dSGiuliano Pochini 			data = ((u32)code[index] << 16) + code[index + 1];
429dd7b254dSGiuliano Pochini 			if (write_dsp(chip, data) < 0) {
430b5b4a41bSSudip Mukherjee 				dev_err(chip->card->dev,
431b5b4a41bSSudip Mukherjee 					"load_dsp: failed to write DSP data\n");
432dd7b254dSGiuliano Pochini 				return -EIO;
433dd7b254dSGiuliano Pochini 			}
434dd7b254dSGiuliano Pochini 		}
435dd7b254dSGiuliano Pochini 	}
436dd7b254dSGiuliano Pochini 
437dd7b254dSGiuliano Pochini 	if (write_dsp(chip, 0) < 0) {	/* We're done!!! */
438b5b4a41bSSudip Mukherjee 		dev_err(chip->card->dev,
439b5b4a41bSSudip Mukherjee 			"load_dsp: Failed to write final zero\n");
440dd7b254dSGiuliano Pochini 		return -EIO;
441dd7b254dSGiuliano Pochini 	}
442dd7b254dSGiuliano Pochini 	udelay(10);
443dd7b254dSGiuliano Pochini 
444dd7b254dSGiuliano Pochini 	for (i = 0; i < 5000; i++) {	/* Timeout is 100us * 5000 = 500ms */
445dd7b254dSGiuliano Pochini 		/* Wait for flag 4 - indicates that the DSP loaded OK */
446dd7b254dSGiuliano Pochini 		if (get_dsp_register(chip, CHI32_STATUS_REG) &
447dd7b254dSGiuliano Pochini 		    CHI32_STATUS_REG_HF4) {
448dd7b254dSGiuliano Pochini 			set_dsp_register(chip, CHI32_CONTROL_REG,
449dd7b254dSGiuliano Pochini 					 get_dsp_register(chip, CHI32_CONTROL_REG) & ~0x1b00);
450dd7b254dSGiuliano Pochini 
451dd7b254dSGiuliano Pochini 			if (write_dsp(chip, DSP_FNC_SET_COMMPAGE_ADDR) < 0) {
452b5b4a41bSSudip Mukherjee 				dev_err(chip->card->dev,
453b5b4a41bSSudip Mukherjee 					"load_dsp: Failed to write DSP_FNC_SET_COMMPAGE_ADDR\n");
454dd7b254dSGiuliano Pochini 				return -EIO;
455dd7b254dSGiuliano Pochini 			}
456dd7b254dSGiuliano Pochini 
457dd7b254dSGiuliano Pochini 			if (write_dsp(chip, chip->comm_page_phys) < 0) {
458b5b4a41bSSudip Mukherjee 				dev_err(chip->card->dev,
459b5b4a41bSSudip Mukherjee 					"load_dsp: Failed to write comm page address\n");
460dd7b254dSGiuliano Pochini 				return -EIO;
461dd7b254dSGiuliano Pochini 			}
462dd7b254dSGiuliano Pochini 
463dd7b254dSGiuliano Pochini 			/* Get the serial number via slave mode.
464dd7b254dSGiuliano Pochini 			This is triggered by the SET_COMMPAGE_ADDR command.
465dd7b254dSGiuliano Pochini 			We don't actually use the serial number but we have to
466dd7b254dSGiuliano Pochini 			get it as part of the DSP init voodoo. */
467dd7b254dSGiuliano Pochini 			if (read_sn(chip) < 0) {
468b5b4a41bSSudip Mukherjee 				dev_err(chip->card->dev,
469b5b4a41bSSudip Mukherjee 					"load_dsp: Failed to read serial number\n");
470dd7b254dSGiuliano Pochini 				return -EIO;
471dd7b254dSGiuliano Pochini 			}
472dd7b254dSGiuliano Pochini 
473dd7b254dSGiuliano Pochini 			chip->dsp_code = code;		/* Show which DSP code loaded */
4743f6175ecSMark Brown 			chip->bad_board = false;	/* DSP OK */
475dd7b254dSGiuliano Pochini 			return 0;
476dd7b254dSGiuliano Pochini 		}
477dd7b254dSGiuliano Pochini 		udelay(100);
478dd7b254dSGiuliano Pochini 	}
479dd7b254dSGiuliano Pochini 
480b5b4a41bSSudip Mukherjee 	dev_err(chip->card->dev,
481b5b4a41bSSudip Mukherjee 		"load_dsp: DSP load timed out waiting for HF4\n");
482dd7b254dSGiuliano Pochini 	return -EIO;
483dd7b254dSGiuliano Pochini }
484dd7b254dSGiuliano Pochini 
485dd7b254dSGiuliano Pochini 
486dd7b254dSGiuliano Pochini 
487dd7b254dSGiuliano Pochini /* load_firmware takes care of loading the DSP and any ASIC code. */
488dd7b254dSGiuliano Pochini static int load_firmware(struct echoaudio *chip)
489dd7b254dSGiuliano Pochini {
490dd7b254dSGiuliano Pochini 	const struct firmware *fw;
491dd7b254dSGiuliano Pochini 	int box_type, err;
492dd7b254dSGiuliano Pochini 
493c914f55fSMark Hills 	if (snd_BUG_ON(!chip->comm_page))
494da3cec35STakashi Iwai 		return -EPERM;
495dd7b254dSGiuliano Pochini 
496dd7b254dSGiuliano Pochini 	/* See if the ASIC is present and working - only if the DSP is already loaded */
497dd7b254dSGiuliano Pochini 	if (chip->dsp_code) {
498dd7b254dSGiuliano Pochini 		if ((box_type = check_asic_status(chip)) >= 0)
499dd7b254dSGiuliano Pochini 			return box_type;
500dd7b254dSGiuliano Pochini 		/* ASIC check failed; force the DSP to reload */
501dd7b254dSGiuliano Pochini 		chip->dsp_code = NULL;
502dd7b254dSGiuliano Pochini 	}
503dd7b254dSGiuliano Pochini 
50419b50063SGiuliano Pochini 	err = get_firmware(&fw, chip, chip->dsp_code_to_load);
50519b50063SGiuliano Pochini 	if (err < 0)
506dd7b254dSGiuliano Pochini 		return err;
507dd7b254dSGiuliano Pochini 	err = load_dsp(chip, (u16 *)fw->data);
508e3690869SSudip Mukherjee 	free_firmware(fw, chip);
509dd7b254dSGiuliano Pochini 	if (err < 0)
510dd7b254dSGiuliano Pochini 		return err;
511dd7b254dSGiuliano Pochini 
512dd7b254dSGiuliano Pochini 	if ((box_type = load_asic(chip)) < 0)
513dd7b254dSGiuliano Pochini 		return box_type;	/* error */
514dd7b254dSGiuliano Pochini 
515dd7b254dSGiuliano Pochini 	return box_type;
516dd7b254dSGiuliano Pochini }
517dd7b254dSGiuliano Pochini 
518dd7b254dSGiuliano Pochini 
519dd7b254dSGiuliano Pochini 
520dd7b254dSGiuliano Pochini /****************************************************************************
521dd7b254dSGiuliano Pochini 	Mixer functions
522dd7b254dSGiuliano Pochini  ****************************************************************************/
523dd7b254dSGiuliano Pochini 
524dd7b254dSGiuliano Pochini #if defined(ECHOCARD_HAS_INPUT_NOMINAL_LEVEL) || \
525dd7b254dSGiuliano Pochini 	defined(ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL)
526dd7b254dSGiuliano Pochini 
527dd7b254dSGiuliano Pochini /* Set the nominal level for an input or output bus (true = -10dBV, false = +4dBu) */
528dd7b254dSGiuliano Pochini static int set_nominal_level(struct echoaudio *chip, u16 index, char consumer)
529dd7b254dSGiuliano Pochini {
530da3cec35STakashi Iwai 	if (snd_BUG_ON(index >= num_busses_out(chip) + num_busses_in(chip)))
531da3cec35STakashi Iwai 		return -EINVAL;
532dd7b254dSGiuliano Pochini 
533dd7b254dSGiuliano Pochini 	/* Wait for the handshake (OK even if ASIC is not loaded) */
534dd7b254dSGiuliano Pochini 	if (wait_handshake(chip))
535dd7b254dSGiuliano Pochini 		return -EIO;
536dd7b254dSGiuliano Pochini 
537dd7b254dSGiuliano Pochini 	chip->nominal_level[index] = consumer;
538dd7b254dSGiuliano Pochini 
539dd7b254dSGiuliano Pochini 	if (consumer)
540dd7b254dSGiuliano Pochini 		chip->comm_page->nominal_level_mask |= cpu_to_le32(1 << index);
541dd7b254dSGiuliano Pochini 	else
542dd7b254dSGiuliano Pochini 		chip->comm_page->nominal_level_mask &= ~cpu_to_le32(1 << index);
543dd7b254dSGiuliano Pochini 
544dd7b254dSGiuliano Pochini 	return 0;
545dd7b254dSGiuliano Pochini }
546dd7b254dSGiuliano Pochini 
547dd7b254dSGiuliano Pochini #endif /* ECHOCARD_HAS_*_NOMINAL_LEVEL */
548dd7b254dSGiuliano Pochini 
549dd7b254dSGiuliano Pochini 
550dd7b254dSGiuliano Pochini 
551dd7b254dSGiuliano Pochini /* Set the gain for a single physical output channel (dB). */
552dd7b254dSGiuliano Pochini static int set_output_gain(struct echoaudio *chip, u16 channel, s8 gain)
553dd7b254dSGiuliano Pochini {
554da3cec35STakashi Iwai 	if (snd_BUG_ON(channel >= num_busses_out(chip)))
555da3cec35STakashi Iwai 		return -EINVAL;
556dd7b254dSGiuliano Pochini 
557dd7b254dSGiuliano Pochini 	if (wait_handshake(chip))
558dd7b254dSGiuliano Pochini 		return -EIO;
559dd7b254dSGiuliano Pochini 
560dd7b254dSGiuliano Pochini 	/* Save the new value */
561dd7b254dSGiuliano Pochini 	chip->output_gain[channel] = gain;
562dd7b254dSGiuliano Pochini 	chip->comm_page->line_out_level[channel] = gain;
563dd7b254dSGiuliano Pochini 	return 0;
564dd7b254dSGiuliano Pochini }
565dd7b254dSGiuliano Pochini 
566dd7b254dSGiuliano Pochini 
567dd7b254dSGiuliano Pochini 
568dd7b254dSGiuliano Pochini #ifdef ECHOCARD_HAS_MONITOR
569dd7b254dSGiuliano Pochini /* Set the monitor level from an input bus to an output bus. */
570dd7b254dSGiuliano Pochini static int set_monitor_gain(struct echoaudio *chip, u16 output, u16 input,
571dd7b254dSGiuliano Pochini 			    s8 gain)
572dd7b254dSGiuliano Pochini {
573da3cec35STakashi Iwai 	if (snd_BUG_ON(output >= num_busses_out(chip) ||
574da3cec35STakashi Iwai 		    input >= num_busses_in(chip)))
575da3cec35STakashi Iwai 		return -EINVAL;
576dd7b254dSGiuliano Pochini 
577dd7b254dSGiuliano Pochini 	if (wait_handshake(chip))
578dd7b254dSGiuliano Pochini 		return -EIO;
579dd7b254dSGiuliano Pochini 
580dd7b254dSGiuliano Pochini 	chip->monitor_gain[output][input] = gain;
581dd7b254dSGiuliano Pochini 	chip->comm_page->monitors[monitor_index(chip, output, input)] = gain;
582dd7b254dSGiuliano Pochini 	return 0;
583dd7b254dSGiuliano Pochini }
584dd7b254dSGiuliano Pochini #endif /* ECHOCARD_HAS_MONITOR */
585dd7b254dSGiuliano Pochini 
586dd7b254dSGiuliano Pochini 
587dd7b254dSGiuliano Pochini /* Tell the DSP to read and update output, nominal & monitor levels in comm page. */
588dd7b254dSGiuliano Pochini static int update_output_line_level(struct echoaudio *chip)
589dd7b254dSGiuliano Pochini {
590dd7b254dSGiuliano Pochini 	if (wait_handshake(chip))
591dd7b254dSGiuliano Pochini 		return -EIO;
592dd7b254dSGiuliano Pochini 	clear_handshake(chip);
593dd7b254dSGiuliano Pochini 	return send_vector(chip, DSP_VC_UPDATE_OUTVOL);
594dd7b254dSGiuliano Pochini }
595dd7b254dSGiuliano Pochini 
596dd7b254dSGiuliano Pochini 
597dd7b254dSGiuliano Pochini 
598dd7b254dSGiuliano Pochini /* Tell the DSP to read and update input levels in comm page */
599dd7b254dSGiuliano Pochini static int update_input_line_level(struct echoaudio *chip)
600dd7b254dSGiuliano Pochini {
601dd7b254dSGiuliano Pochini 	if (wait_handshake(chip))
602dd7b254dSGiuliano Pochini 		return -EIO;
603dd7b254dSGiuliano Pochini 	clear_handshake(chip);
604dd7b254dSGiuliano Pochini 	return send_vector(chip, DSP_VC_UPDATE_INGAIN);
605dd7b254dSGiuliano Pochini }
606dd7b254dSGiuliano Pochini 
607dd7b254dSGiuliano Pochini 
608dd7b254dSGiuliano Pochini 
609dd7b254dSGiuliano Pochini /* set_meters_on turns the meters on or off.  If meters are turned on, the DSP
610dd7b254dSGiuliano Pochini will write the meter and clock detect values to the comm page at about 30Hz */
611dd7b254dSGiuliano Pochini static void set_meters_on(struct echoaudio *chip, char on)
612dd7b254dSGiuliano Pochini {
613dd7b254dSGiuliano Pochini 	if (on && !chip->meters_enabled) {
614dd7b254dSGiuliano Pochini 		send_vector(chip, DSP_VC_METERS_ON);
615dd7b254dSGiuliano Pochini 		chip->meters_enabled = 1;
616dd7b254dSGiuliano Pochini 	} else if (!on && chip->meters_enabled) {
617dd7b254dSGiuliano Pochini 		send_vector(chip, DSP_VC_METERS_OFF);
618dd7b254dSGiuliano Pochini 		chip->meters_enabled = 0;
619dd7b254dSGiuliano Pochini 		memset((s8 *)chip->comm_page->vu_meter, ECHOGAIN_MUTED,
620dd7b254dSGiuliano Pochini 		       DSP_MAXPIPES);
621dd7b254dSGiuliano Pochini 		memset((s8 *)chip->comm_page->peak_meter, ECHOGAIN_MUTED,
622dd7b254dSGiuliano Pochini 		       DSP_MAXPIPES);
623dd7b254dSGiuliano Pochini 	}
624dd7b254dSGiuliano Pochini }
625dd7b254dSGiuliano Pochini 
626dd7b254dSGiuliano Pochini 
627dd7b254dSGiuliano Pochini 
628dd7b254dSGiuliano Pochini /* Fill out an the given array using the current values in the comm page.
629dd7b254dSGiuliano Pochini Meters are written in the comm page by the DSP in this order:
630dd7b254dSGiuliano Pochini  Output busses
631dd7b254dSGiuliano Pochini  Input busses
632dd7b254dSGiuliano Pochini  Output pipes (vmixer cards only)
633dd7b254dSGiuliano Pochini 
634dd7b254dSGiuliano Pochini This function assumes there are no more than 16 in/out busses or pipes
635dd7b254dSGiuliano Pochini Meters is an array [3][16][2] of long. */
636dd7b254dSGiuliano Pochini static void get_audio_meters(struct echoaudio *chip, long *meters)
637dd7b254dSGiuliano Pochini {
638dd7b254dSGiuliano Pochini 	int i, m, n;
639dd7b254dSGiuliano Pochini 
640dd7b254dSGiuliano Pochini 	m = 0;
641dd7b254dSGiuliano Pochini 	n = 0;
642dd7b254dSGiuliano Pochini 	for (i = 0; i < num_busses_out(chip); i++, m++) {
643dd7b254dSGiuliano Pochini 		meters[n++] = chip->comm_page->vu_meter[m];
644dd7b254dSGiuliano Pochini 		meters[n++] = chip->comm_page->peak_meter[m];
645dd7b254dSGiuliano Pochini 	}
646dd7b254dSGiuliano Pochini 	for (; n < 32; n++)
647dd7b254dSGiuliano Pochini 		meters[n] = 0;
648dd7b254dSGiuliano Pochini 
649dd7b254dSGiuliano Pochini #ifdef ECHOCARD_ECHO3G
650dd7b254dSGiuliano Pochini 	m = E3G_MAX_OUTPUTS;	/* Skip unused meters */
651dd7b254dSGiuliano Pochini #endif
652dd7b254dSGiuliano Pochini 
653dd7b254dSGiuliano Pochini 	for (i = 0; i < num_busses_in(chip); i++, m++) {
654dd7b254dSGiuliano Pochini 		meters[n++] = chip->comm_page->vu_meter[m];
655dd7b254dSGiuliano Pochini 		meters[n++] = chip->comm_page->peak_meter[m];
656dd7b254dSGiuliano Pochini 	}
657dd7b254dSGiuliano Pochini 	for (; n < 64; n++)
658dd7b254dSGiuliano Pochini 		meters[n] = 0;
659dd7b254dSGiuliano Pochini 
660dd7b254dSGiuliano Pochini #ifdef ECHOCARD_HAS_VMIXER
661dd7b254dSGiuliano Pochini 	for (i = 0; i < num_pipes_out(chip); i++, m++) {
662dd7b254dSGiuliano Pochini 		meters[n++] = chip->comm_page->vu_meter[m];
663dd7b254dSGiuliano Pochini 		meters[n++] = chip->comm_page->peak_meter[m];
664dd7b254dSGiuliano Pochini 	}
665dd7b254dSGiuliano Pochini #endif
666dd7b254dSGiuliano Pochini 	for (; n < 96; n++)
667dd7b254dSGiuliano Pochini 		meters[n] = 0;
668dd7b254dSGiuliano Pochini }
669dd7b254dSGiuliano Pochini 
670dd7b254dSGiuliano Pochini 
671dd7b254dSGiuliano Pochini 
672dd7b254dSGiuliano Pochini static int restore_dsp_rettings(struct echoaudio *chip)
673dd7b254dSGiuliano Pochini {
67447b5d028SGiuliano Pochini 	int i, o, err;
675dd7b254dSGiuliano Pochini 
676dd7b254dSGiuliano Pochini 	if ((err = check_asic_status(chip)) < 0)
677dd7b254dSGiuliano Pochini 		return err;
678dd7b254dSGiuliano Pochini 
67947b5d028SGiuliano Pochini 	/* Gina20/Darla20 only. Should be harmless for other cards. */
680dd7b254dSGiuliano Pochini 	chip->comm_page->gd_clock_state = GD_CLOCK_UNDEF;
681dd7b254dSGiuliano Pochini 	chip->comm_page->gd_spdif_status = GD_SPDIF_STATUS_UNDEF;
682*2a833a02STakashi Iwai 	chip->comm_page->handshake = cpu_to_le32(0xffffffff);
683dd7b254dSGiuliano Pochini 
68447b5d028SGiuliano Pochini 	/* Restore output busses */
68547b5d028SGiuliano Pochini 	for (i = 0; i < num_busses_out(chip); i++) {
68647b5d028SGiuliano Pochini 		err = set_output_gain(chip, i, chip->output_gain[i]);
68747b5d028SGiuliano Pochini 		if (err < 0)
68847b5d028SGiuliano Pochini 			return err;
68947b5d028SGiuliano Pochini 	}
69047b5d028SGiuliano Pochini 
69147b5d028SGiuliano Pochini #ifdef ECHOCARD_HAS_VMIXER
69247b5d028SGiuliano Pochini 	for (i = 0; i < num_pipes_out(chip); i++)
69347b5d028SGiuliano Pochini 		for (o = 0; o < num_busses_out(chip); o++) {
69447b5d028SGiuliano Pochini 			err = set_vmixer_gain(chip, o, i,
69547b5d028SGiuliano Pochini 						chip->vmixer_gain[o][i]);
69647b5d028SGiuliano Pochini 			if (err < 0)
69747b5d028SGiuliano Pochini 				return err;
69847b5d028SGiuliano Pochini 		}
69947b5d028SGiuliano Pochini 	if (update_vmixer_level(chip) < 0)
70047b5d028SGiuliano Pochini 		return -EIO;
70147b5d028SGiuliano Pochini #endif /* ECHOCARD_HAS_VMIXER */
70247b5d028SGiuliano Pochini 
70347b5d028SGiuliano Pochini #ifdef ECHOCARD_HAS_MONITOR
70447b5d028SGiuliano Pochini 	for (o = 0; o < num_busses_out(chip); o++)
70547b5d028SGiuliano Pochini 		for (i = 0; i < num_busses_in(chip); i++) {
70647b5d028SGiuliano Pochini 			err = set_monitor_gain(chip, o, i,
70747b5d028SGiuliano Pochini 						chip->monitor_gain[o][i]);
70847b5d028SGiuliano Pochini 			if (err < 0)
70947b5d028SGiuliano Pochini 				return err;
71047b5d028SGiuliano Pochini 		}
71147b5d028SGiuliano Pochini #endif /* ECHOCARD_HAS_MONITOR */
71247b5d028SGiuliano Pochini 
71347b5d028SGiuliano Pochini #ifdef ECHOCARD_HAS_INPUT_GAIN
71447b5d028SGiuliano Pochini 	for (i = 0; i < num_busses_in(chip); i++) {
71547b5d028SGiuliano Pochini 		err = set_input_gain(chip, i, chip->input_gain[i]);
71647b5d028SGiuliano Pochini 		if (err < 0)
71747b5d028SGiuliano Pochini 			return err;
71847b5d028SGiuliano Pochini 	}
71947b5d028SGiuliano Pochini #endif /* ECHOCARD_HAS_INPUT_GAIN */
72047b5d028SGiuliano Pochini 
72147b5d028SGiuliano Pochini 	err = update_output_line_level(chip);
72247b5d028SGiuliano Pochini 	if (err < 0)
723dd7b254dSGiuliano Pochini 		return err;
724dd7b254dSGiuliano Pochini 
72547b5d028SGiuliano Pochini 	err = update_input_line_level(chip);
72647b5d028SGiuliano Pochini 	if (err < 0)
72747b5d028SGiuliano Pochini 		return err;
72847b5d028SGiuliano Pochini 
72947b5d028SGiuliano Pochini 	err = set_sample_rate(chip, chip->sample_rate);
73047b5d028SGiuliano Pochini 	if (err < 0)
73147b5d028SGiuliano Pochini 		return err;
73247b5d028SGiuliano Pochini 
73347b5d028SGiuliano Pochini 	if (chip->meters_enabled) {
73447b5d028SGiuliano Pochini 		err = send_vector(chip, DSP_VC_METERS_ON);
73547b5d028SGiuliano Pochini 		if (err < 0)
73647b5d028SGiuliano Pochini 			return err;
73747b5d028SGiuliano Pochini 	}
73847b5d028SGiuliano Pochini 
73947b5d028SGiuliano Pochini #ifdef ECHOCARD_HAS_DIGITAL_MODE_SWITCH
74047b5d028SGiuliano Pochini 	if (set_digital_mode(chip, chip->digital_mode) < 0)
741dd7b254dSGiuliano Pochini 		return -EIO;
74247b5d028SGiuliano Pochini #endif
74347b5d028SGiuliano Pochini 
74447b5d028SGiuliano Pochini #ifdef ECHOCARD_HAS_DIGITAL_IO
74547b5d028SGiuliano Pochini 	if (set_professional_spdif(chip, chip->professional_spdif) < 0)
74647b5d028SGiuliano Pochini 		return -EIO;
74747b5d028SGiuliano Pochini #endif
74847b5d028SGiuliano Pochini 
74947b5d028SGiuliano Pochini #ifdef ECHOCARD_HAS_PHANTOM_POWER
75047b5d028SGiuliano Pochini 	if (set_phantom_power(chip, chip->phantom_power) < 0)
75147b5d028SGiuliano Pochini 		return -EIO;
75247b5d028SGiuliano Pochini #endif
753dd7b254dSGiuliano Pochini 
754dd7b254dSGiuliano Pochini #ifdef ECHOCARD_HAS_EXTERNAL_CLOCK
75547b5d028SGiuliano Pochini 	/* set_input_clock() also restores automute setting */
756dd7b254dSGiuliano Pochini 	if (set_input_clock(chip, chip->input_clock) < 0)
757dd7b254dSGiuliano Pochini 		return -EIO;
758dd7b254dSGiuliano Pochini #endif
759dd7b254dSGiuliano Pochini 
760dd7b254dSGiuliano Pochini #ifdef ECHOCARD_HAS_OUTPUT_CLOCK_SWITCH
761dd7b254dSGiuliano Pochini 	if (set_output_clock(chip, chip->output_clock) < 0)
762dd7b254dSGiuliano Pochini 		return -EIO;
763dd7b254dSGiuliano Pochini #endif
764dd7b254dSGiuliano Pochini 
765dd7b254dSGiuliano Pochini 	if (wait_handshake(chip) < 0)
766dd7b254dSGiuliano Pochini 		return -EIO;
767dd7b254dSGiuliano Pochini 	clear_handshake(chip);
76847b5d028SGiuliano Pochini 	if (send_vector(chip, DSP_VC_UPDATE_FLAGS) < 0)
76947b5d028SGiuliano Pochini 		return -EIO;
770dd7b254dSGiuliano Pochini 
77147b5d028SGiuliano Pochini 	return 0;
772dd7b254dSGiuliano Pochini }
773dd7b254dSGiuliano Pochini 
774dd7b254dSGiuliano Pochini 
775dd7b254dSGiuliano Pochini 
776dd7b254dSGiuliano Pochini /****************************************************************************
777dd7b254dSGiuliano Pochini 	Transport functions
778dd7b254dSGiuliano Pochini  ****************************************************************************/
779dd7b254dSGiuliano Pochini 
780dd7b254dSGiuliano Pochini /* set_audio_format() sets the format of the audio data in host memory for
781dd7b254dSGiuliano Pochini this pipe.  Note that _MS_ (mono-to-stereo) playback modes are not used by ALSA
782dd7b254dSGiuliano Pochini but they are here because they are just mono while capturing */
783dd7b254dSGiuliano Pochini static void set_audio_format(struct echoaudio *chip, u16 pipe_index,
784dd7b254dSGiuliano Pochini 			     const struct audioformat *format)
785dd7b254dSGiuliano Pochini {
786dd7b254dSGiuliano Pochini 	u16 dsp_format;
787dd7b254dSGiuliano Pochini 
788dd7b254dSGiuliano Pochini 	dsp_format = DSP_AUDIOFORM_SS_16LE;
789dd7b254dSGiuliano Pochini 
790dd7b254dSGiuliano Pochini 	/* Look for super-interleave (no big-endian and 8 bits) */
791dd7b254dSGiuliano Pochini 	if (format->interleave > 2) {
792dd7b254dSGiuliano Pochini 		switch (format->bits_per_sample) {
793dd7b254dSGiuliano Pochini 		case 16:
794dd7b254dSGiuliano Pochini 			dsp_format = DSP_AUDIOFORM_SUPER_INTERLEAVE_16LE;
795dd7b254dSGiuliano Pochini 			break;
796dd7b254dSGiuliano Pochini 		case 24:
797dd7b254dSGiuliano Pochini 			dsp_format = DSP_AUDIOFORM_SUPER_INTERLEAVE_24LE;
798dd7b254dSGiuliano Pochini 			break;
799dd7b254dSGiuliano Pochini 		case 32:
800dd7b254dSGiuliano Pochini 			dsp_format = DSP_AUDIOFORM_SUPER_INTERLEAVE_32LE;
801dd7b254dSGiuliano Pochini 			break;
802dd7b254dSGiuliano Pochini 		}
803dd7b254dSGiuliano Pochini 		dsp_format |= format->interleave;
804dd7b254dSGiuliano Pochini 	} else if (format->data_are_bigendian) {
805dd7b254dSGiuliano Pochini 		/* For big-endian data, only 32 bit samples are supported */
806dd7b254dSGiuliano Pochini 		switch (format->interleave) {
807dd7b254dSGiuliano Pochini 		case 1:
808dd7b254dSGiuliano Pochini 			dsp_format = DSP_AUDIOFORM_MM_32BE;
809dd7b254dSGiuliano Pochini 			break;
810dd7b254dSGiuliano Pochini #ifdef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
811dd7b254dSGiuliano Pochini 		case 2:
812dd7b254dSGiuliano Pochini 			dsp_format = DSP_AUDIOFORM_SS_32BE;
813dd7b254dSGiuliano Pochini 			break;
814dd7b254dSGiuliano Pochini #endif
815dd7b254dSGiuliano Pochini 		}
816dd7b254dSGiuliano Pochini 	} else if (format->interleave == 1 &&
817dd7b254dSGiuliano Pochini 		   format->bits_per_sample == 32 && !format->mono_to_stereo) {
818dd7b254dSGiuliano Pochini 		/* 32 bit little-endian mono->mono case */
819dd7b254dSGiuliano Pochini 		dsp_format = DSP_AUDIOFORM_MM_32LE;
820dd7b254dSGiuliano Pochini 	} else {
821dd7b254dSGiuliano Pochini 		/* Handle the other little-endian formats */
822dd7b254dSGiuliano Pochini 		switch (format->bits_per_sample) {
823dd7b254dSGiuliano Pochini 		case 8:
824dd7b254dSGiuliano Pochini 			if (format->interleave == 2)
825dd7b254dSGiuliano Pochini 				dsp_format = DSP_AUDIOFORM_SS_8;
826dd7b254dSGiuliano Pochini 			else
827dd7b254dSGiuliano Pochini 				dsp_format = DSP_AUDIOFORM_MS_8;
828dd7b254dSGiuliano Pochini 			break;
829dd7b254dSGiuliano Pochini 		default:
830dd7b254dSGiuliano Pochini 		case 16:
831dd7b254dSGiuliano Pochini 			if (format->interleave == 2)
832dd7b254dSGiuliano Pochini 				dsp_format = DSP_AUDIOFORM_SS_16LE;
833dd7b254dSGiuliano Pochini 			else
834dd7b254dSGiuliano Pochini 				dsp_format = DSP_AUDIOFORM_MS_16LE;
835dd7b254dSGiuliano Pochini 			break;
836dd7b254dSGiuliano Pochini 		case 24:
837dd7b254dSGiuliano Pochini 			if (format->interleave == 2)
838dd7b254dSGiuliano Pochini 				dsp_format = DSP_AUDIOFORM_SS_24LE;
839dd7b254dSGiuliano Pochini 			else
840dd7b254dSGiuliano Pochini 				dsp_format = DSP_AUDIOFORM_MS_24LE;
841dd7b254dSGiuliano Pochini 			break;
842dd7b254dSGiuliano Pochini 		case 32:
843dd7b254dSGiuliano Pochini 			if (format->interleave == 2)
844dd7b254dSGiuliano Pochini 				dsp_format = DSP_AUDIOFORM_SS_32LE;
845dd7b254dSGiuliano Pochini 			else
846dd7b254dSGiuliano Pochini 				dsp_format = DSP_AUDIOFORM_MS_32LE;
847dd7b254dSGiuliano Pochini 			break;
848dd7b254dSGiuliano Pochini 		}
849dd7b254dSGiuliano Pochini 	}
850b5b4a41bSSudip Mukherjee 	dev_dbg(chip->card->dev,
851b5b4a41bSSudip Mukherjee 		 "set_audio_format[%d] = %x\n", pipe_index, dsp_format);
852dd7b254dSGiuliano Pochini 	chip->comm_page->audio_format[pipe_index] = cpu_to_le16(dsp_format);
853dd7b254dSGiuliano Pochini }
854dd7b254dSGiuliano Pochini 
855dd7b254dSGiuliano Pochini 
856dd7b254dSGiuliano Pochini 
857dd7b254dSGiuliano Pochini /* start_transport starts transport for a set of pipes.
858dd7b254dSGiuliano Pochini The bits 1 in channel_mask specify what pipes to start. Only the bit of the
859dd7b254dSGiuliano Pochini first channel must be set, regardless its interleave.
860dd7b254dSGiuliano Pochini Same thing for pause_ and stop_ -trasport below. */
861dd7b254dSGiuliano Pochini static int start_transport(struct echoaudio *chip, u32 channel_mask,
862dd7b254dSGiuliano Pochini 			   u32 cyclic_mask)
863dd7b254dSGiuliano Pochini {
864dd7b254dSGiuliano Pochini 
865dd7b254dSGiuliano Pochini 	if (wait_handshake(chip))
866dd7b254dSGiuliano Pochini 		return -EIO;
867dd7b254dSGiuliano Pochini 
868dd7b254dSGiuliano Pochini 	chip->comm_page->cmd_start |= cpu_to_le32(channel_mask);
869dd7b254dSGiuliano Pochini 
870dd7b254dSGiuliano Pochini 	if (chip->comm_page->cmd_start) {
871dd7b254dSGiuliano Pochini 		clear_handshake(chip);
872dd7b254dSGiuliano Pochini 		send_vector(chip, DSP_VC_START_TRANSFER);
873dd7b254dSGiuliano Pochini 		if (wait_handshake(chip))
874dd7b254dSGiuliano Pochini 			return -EIO;
875dd7b254dSGiuliano Pochini 		/* Keep track of which pipes are transporting */
876dd7b254dSGiuliano Pochini 		chip->active_mask |= channel_mask;
877dd7b254dSGiuliano Pochini 		chip->comm_page->cmd_start = 0;
878dd7b254dSGiuliano Pochini 		return 0;
879dd7b254dSGiuliano Pochini 	}
880dd7b254dSGiuliano Pochini 
881b5b4a41bSSudip Mukherjee 	dev_err(chip->card->dev, "start_transport: No pipes to start!\n");
882dd7b254dSGiuliano Pochini 	return -EINVAL;
883dd7b254dSGiuliano Pochini }
884dd7b254dSGiuliano Pochini 
885dd7b254dSGiuliano Pochini 
886dd7b254dSGiuliano Pochini 
887dd7b254dSGiuliano Pochini static int pause_transport(struct echoaudio *chip, u32 channel_mask)
888dd7b254dSGiuliano Pochini {
889dd7b254dSGiuliano Pochini 
890dd7b254dSGiuliano Pochini 	if (wait_handshake(chip))
891dd7b254dSGiuliano Pochini 		return -EIO;
892dd7b254dSGiuliano Pochini 
893dd7b254dSGiuliano Pochini 	chip->comm_page->cmd_stop |= cpu_to_le32(channel_mask);
894dd7b254dSGiuliano Pochini 	chip->comm_page->cmd_reset = 0;
895dd7b254dSGiuliano Pochini 	if (chip->comm_page->cmd_stop) {
896dd7b254dSGiuliano Pochini 		clear_handshake(chip);
897dd7b254dSGiuliano Pochini 		send_vector(chip, DSP_VC_STOP_TRANSFER);
898dd7b254dSGiuliano Pochini 		if (wait_handshake(chip))
899dd7b254dSGiuliano Pochini 			return -EIO;
900dd7b254dSGiuliano Pochini 		/* Keep track of which pipes are transporting */
901dd7b254dSGiuliano Pochini 		chip->active_mask &= ~channel_mask;
902dd7b254dSGiuliano Pochini 		chip->comm_page->cmd_stop = 0;
903dd7b254dSGiuliano Pochini 		chip->comm_page->cmd_reset = 0;
904dd7b254dSGiuliano Pochini 		return 0;
905dd7b254dSGiuliano Pochini 	}
906dd7b254dSGiuliano Pochini 
907b5b4a41bSSudip Mukherjee 	dev_warn(chip->card->dev, "pause_transport: No pipes to stop!\n");
908dd7b254dSGiuliano Pochini 	return 0;
909dd7b254dSGiuliano Pochini }
910dd7b254dSGiuliano Pochini 
911dd7b254dSGiuliano Pochini 
912dd7b254dSGiuliano Pochini 
913dd7b254dSGiuliano Pochini static int stop_transport(struct echoaudio *chip, u32 channel_mask)
914dd7b254dSGiuliano Pochini {
915dd7b254dSGiuliano Pochini 
916dd7b254dSGiuliano Pochini 	if (wait_handshake(chip))
917dd7b254dSGiuliano Pochini 		return -EIO;
918dd7b254dSGiuliano Pochini 
919dd7b254dSGiuliano Pochini 	chip->comm_page->cmd_stop |= cpu_to_le32(channel_mask);
920dd7b254dSGiuliano Pochini 	chip->comm_page->cmd_reset |= cpu_to_le32(channel_mask);
921dd7b254dSGiuliano Pochini 	if (chip->comm_page->cmd_reset) {
922dd7b254dSGiuliano Pochini 		clear_handshake(chip);
923dd7b254dSGiuliano Pochini 		send_vector(chip, DSP_VC_STOP_TRANSFER);
924dd7b254dSGiuliano Pochini 		if (wait_handshake(chip))
925dd7b254dSGiuliano Pochini 			return -EIO;
926dd7b254dSGiuliano Pochini 		/* Keep track of which pipes are transporting */
927dd7b254dSGiuliano Pochini 		chip->active_mask &= ~channel_mask;
928dd7b254dSGiuliano Pochini 		chip->comm_page->cmd_stop = 0;
929dd7b254dSGiuliano Pochini 		chip->comm_page->cmd_reset = 0;
930dd7b254dSGiuliano Pochini 		return 0;
931dd7b254dSGiuliano Pochini 	}
932dd7b254dSGiuliano Pochini 
933b5b4a41bSSudip Mukherjee 	dev_warn(chip->card->dev, "stop_transport: No pipes to stop!\n");
934dd7b254dSGiuliano Pochini 	return 0;
935dd7b254dSGiuliano Pochini }
936dd7b254dSGiuliano Pochini 
937dd7b254dSGiuliano Pochini 
938dd7b254dSGiuliano Pochini 
939dd7b254dSGiuliano Pochini static inline int is_pipe_allocated(struct echoaudio *chip, u16 pipe_index)
940dd7b254dSGiuliano Pochini {
941dd7b254dSGiuliano Pochini 	return (chip->pipe_alloc_mask & (1 << pipe_index));
942dd7b254dSGiuliano Pochini }
943dd7b254dSGiuliano Pochini 
944dd7b254dSGiuliano Pochini 
945dd7b254dSGiuliano Pochini 
946dd7b254dSGiuliano Pochini /* Stops everything and turns off the DSP. All pipes should be already
947dd7b254dSGiuliano Pochini stopped and unallocated. */
948dd7b254dSGiuliano Pochini static int rest_in_peace(struct echoaudio *chip)
949dd7b254dSGiuliano Pochini {
950dd7b254dSGiuliano Pochini 
951dd7b254dSGiuliano Pochini 	/* Stops all active pipes (just to be sure) */
952dd7b254dSGiuliano Pochini 	stop_transport(chip, chip->active_mask);
953dd7b254dSGiuliano Pochini 
9543f6175ecSMark Brown 	set_meters_on(chip, false);
955dd7b254dSGiuliano Pochini 
956dd7b254dSGiuliano Pochini #ifdef ECHOCARD_HAS_MIDI
9573f6175ecSMark Brown 	enable_midi_input(chip, false);
958dd7b254dSGiuliano Pochini #endif
959dd7b254dSGiuliano Pochini 
960dd7b254dSGiuliano Pochini 	/* Go to sleep */
961dd7b254dSGiuliano Pochini 	if (chip->dsp_code) {
962dd7b254dSGiuliano Pochini 		/* Make load_firmware do a complete reload */
963dd7b254dSGiuliano Pochini 		chip->dsp_code = NULL;
964dd7b254dSGiuliano Pochini 		/* Put the DSP to sleep */
965dd7b254dSGiuliano Pochini 		return send_vector(chip, DSP_VC_GO_COMATOSE);
966dd7b254dSGiuliano Pochini 	}
967dd7b254dSGiuliano Pochini 	return 0;
968dd7b254dSGiuliano Pochini }
969dd7b254dSGiuliano Pochini 
970dd7b254dSGiuliano Pochini 
971dd7b254dSGiuliano Pochini 
972dd7b254dSGiuliano Pochini /* Fills the comm page with default values */
973dd7b254dSGiuliano Pochini static int init_dsp_comm_page(struct echoaudio *chip)
974dd7b254dSGiuliano Pochini {
975dd7b254dSGiuliano Pochini 	/* Check if the compiler added extra padding inside the structure */
976dd7b254dSGiuliano Pochini 	if (offsetof(struct comm_page, midi_output) != 0xbe0) {
977b5b4a41bSSudip Mukherjee 		dev_err(chip->card->dev,
978b5b4a41bSSudip Mukherjee 			"init_dsp_comm_page() - Invalid struct comm_page structure\n");
979dd7b254dSGiuliano Pochini 		return -EPERM;
980dd7b254dSGiuliano Pochini 	}
981dd7b254dSGiuliano Pochini 
982dd7b254dSGiuliano Pochini 	/* Init all the basic stuff */
983dd7b254dSGiuliano Pochini 	chip->card_name = ECHOCARD_NAME;
9843f6175ecSMark Brown 	chip->bad_board = true;	/* Set true until DSP loaded */
985dd7b254dSGiuliano Pochini 	chip->dsp_code = NULL;	/* Current DSP code not loaded */
9863f6175ecSMark Brown 	chip->asic_loaded = false;
987dd7b254dSGiuliano Pochini 	memset(chip->comm_page, 0, sizeof(struct comm_page));
988dd7b254dSGiuliano Pochini 
989dd7b254dSGiuliano Pochini 	/* Init the comm page */
990dd7b254dSGiuliano Pochini 	chip->comm_page->comm_size =
991e930e995SHarvey Harrison 		cpu_to_le32(sizeof(struct comm_page));
992*2a833a02STakashi Iwai 	chip->comm_page->handshake = cpu_to_le32(0xffffffff);
993dd7b254dSGiuliano Pochini 	chip->comm_page->midi_out_free_count =
994e930e995SHarvey Harrison 		cpu_to_le32(DSP_MIDI_OUT_FIFO_SIZE);
995e930e995SHarvey Harrison 	chip->comm_page->sample_rate = cpu_to_le32(44100);
996dd7b254dSGiuliano Pochini 
997dd7b254dSGiuliano Pochini 	/* Set line levels so we don't blast any inputs on startup */
998dd7b254dSGiuliano Pochini 	memset(chip->comm_page->monitors, ECHOGAIN_MUTED, MONITOR_ARRAY_SIZE);
999dd7b254dSGiuliano Pochini 	memset(chip->comm_page->vmixer, ECHOGAIN_MUTED, VMIXER_ARRAY_SIZE);
1000dd7b254dSGiuliano Pochini 
1001dd7b254dSGiuliano Pochini 	return 0;
1002dd7b254dSGiuliano Pochini }
1003dd7b254dSGiuliano Pochini 
1004dd7b254dSGiuliano Pochini 
1005dd7b254dSGiuliano Pochini 
100647b5d028SGiuliano Pochini /* This function initializes the chip structure with default values, ie. all
100747b5d028SGiuliano Pochini  * muted and internal clock source. Then it copies the settings to the DSP.
100847b5d028SGiuliano Pochini  * This MUST be called after the DSP is up and running !
100947b5d028SGiuliano Pochini  */
1010dd7b254dSGiuliano Pochini static int init_line_levels(struct echoaudio *chip)
1011dd7b254dSGiuliano Pochini {
101247b5d028SGiuliano Pochini 	memset(chip->output_gain, ECHOGAIN_MUTED, sizeof(chip->output_gain));
101347b5d028SGiuliano Pochini 	memset(chip->input_gain, ECHOGAIN_MUTED, sizeof(chip->input_gain));
101447b5d028SGiuliano Pochini 	memset(chip->monitor_gain, ECHOGAIN_MUTED, sizeof(chip->monitor_gain));
101547b5d028SGiuliano Pochini 	memset(chip->vmixer_gain, ECHOGAIN_MUTED, sizeof(chip->vmixer_gain));
101647b5d028SGiuliano Pochini 	chip->input_clock = ECHO_CLOCK_INTERNAL;
101747b5d028SGiuliano Pochini 	chip->output_clock = ECHO_CLOCK_WORD;
101847b5d028SGiuliano Pochini 	chip->sample_rate = 44100;
101947b5d028SGiuliano Pochini 	return restore_dsp_rettings(chip);
1020dd7b254dSGiuliano Pochini }
1021dd7b254dSGiuliano Pochini 
1022dd7b254dSGiuliano Pochini 
1023dd7b254dSGiuliano Pochini 
1024dd7b254dSGiuliano Pochini /* This is low level part of the interrupt handler.
1025dd7b254dSGiuliano Pochini It returns -1 if the IRQ is not ours, or N>=0 if it is, where N is the number
1026dd7b254dSGiuliano Pochini of midi data in the input queue. */
1027dd7b254dSGiuliano Pochini static int service_irq(struct echoaudio *chip)
1028dd7b254dSGiuliano Pochini {
1029dd7b254dSGiuliano Pochini 	int st;
1030dd7b254dSGiuliano Pochini 
1031dd7b254dSGiuliano Pochini 	/* Read the DSP status register and see if this DSP generated this interrupt */
1032dd7b254dSGiuliano Pochini 	if (get_dsp_register(chip, CHI32_STATUS_REG) & CHI32_STATUS_IRQ) {
1033dd7b254dSGiuliano Pochini 		st = 0;
1034dd7b254dSGiuliano Pochini #ifdef ECHOCARD_HAS_MIDI
1035dd7b254dSGiuliano Pochini 		/* Get and parse midi data if present */
1036dd7b254dSGiuliano Pochini 		if (chip->comm_page->midi_input[0])	/* The count is at index 0 */
1037dd7b254dSGiuliano Pochini 			st = midi_service_irq(chip);	/* Returns how many midi bytes we received */
1038dd7b254dSGiuliano Pochini #endif
1039dd7b254dSGiuliano Pochini 		/* Clear the hardware interrupt */
1040dd7b254dSGiuliano Pochini 		chip->comm_page->midi_input[0] = 0;
1041dd7b254dSGiuliano Pochini 		send_vector(chip, DSP_VC_ACK_INT);
1042dd7b254dSGiuliano Pochini 		return st;
1043dd7b254dSGiuliano Pochini 	}
1044dd7b254dSGiuliano Pochini 	return -1;
1045dd7b254dSGiuliano Pochini }
1046dd7b254dSGiuliano Pochini 
1047dd7b254dSGiuliano Pochini 
1048dd7b254dSGiuliano Pochini 
1049dd7b254dSGiuliano Pochini 
1050dd7b254dSGiuliano Pochini /******************************************************************************
1051dd7b254dSGiuliano Pochini 	Functions for opening and closing pipes
1052dd7b254dSGiuliano Pochini  ******************************************************************************/
1053dd7b254dSGiuliano Pochini 
1054dd7b254dSGiuliano Pochini /* allocate_pipes is used to reserve audio pipes for your exclusive use.
1055dd7b254dSGiuliano Pochini The call will fail if some pipes are already allocated. */
1056dd7b254dSGiuliano Pochini static int allocate_pipes(struct echoaudio *chip, struct audiopipe *pipe,
1057dd7b254dSGiuliano Pochini 			  int pipe_index, int interleave)
1058dd7b254dSGiuliano Pochini {
1059dd7b254dSGiuliano Pochini 	int i;
1060dd7b254dSGiuliano Pochini 	u32 channel_mask;
1061dd7b254dSGiuliano Pochini 	char is_cyclic;
1062dd7b254dSGiuliano Pochini 
1063b5b4a41bSSudip Mukherjee 	dev_dbg(chip->card->dev,
1064b5b4a41bSSudip Mukherjee 		"allocate_pipes: ch=%d int=%d\n", pipe_index, interleave);
1065dd7b254dSGiuliano Pochini 
1066dd7b254dSGiuliano Pochini 	if (chip->bad_board)
1067dd7b254dSGiuliano Pochini 		return -EIO;
1068dd7b254dSGiuliano Pochini 
1069dd7b254dSGiuliano Pochini 	is_cyclic = 1;	/* This driver uses cyclic buffers only */
1070dd7b254dSGiuliano Pochini 
1071dd7b254dSGiuliano Pochini 	for (channel_mask = i = 0; i < interleave; i++)
1072dd7b254dSGiuliano Pochini 		channel_mask |= 1 << (pipe_index + i);
1073dd7b254dSGiuliano Pochini 	if (chip->pipe_alloc_mask & channel_mask) {
1074b5b4a41bSSudip Mukherjee 		dev_err(chip->card->dev,
1075b5b4a41bSSudip Mukherjee 			"allocate_pipes: channel already open\n");
1076dd7b254dSGiuliano Pochini 		return -EAGAIN;
1077dd7b254dSGiuliano Pochini 	}
1078dd7b254dSGiuliano Pochini 
1079dd7b254dSGiuliano Pochini 	chip->comm_page->position[pipe_index] = 0;
1080dd7b254dSGiuliano Pochini 	chip->pipe_alloc_mask |= channel_mask;
1081dd7b254dSGiuliano Pochini 	if (is_cyclic)
1082dd7b254dSGiuliano Pochini 		chip->pipe_cyclic_mask |= channel_mask;
1083dd7b254dSGiuliano Pochini 	pipe->index = pipe_index;
1084dd7b254dSGiuliano Pochini 	pipe->interleave = interleave;
1085dd7b254dSGiuliano Pochini 	pipe->state = PIPE_STATE_STOPPED;
1086dd7b254dSGiuliano Pochini 
1087dd7b254dSGiuliano Pochini 	/* The counter register is where the DSP writes the 32 bit DMA
1088dd7b254dSGiuliano Pochini 	position for a pipe.  The DSP is constantly updating this value as
1089dd7b254dSGiuliano Pochini 	it moves data. The DMA counter is in units of bytes, not samples. */
1090*2a833a02STakashi Iwai 	pipe->dma_counter = (__le32 *)&chip->comm_page->position[pipe_index];
1091dd7b254dSGiuliano Pochini 	*pipe->dma_counter = 0;
1092dd7b254dSGiuliano Pochini 	return pipe_index;
1093dd7b254dSGiuliano Pochini }
1094dd7b254dSGiuliano Pochini 
1095dd7b254dSGiuliano Pochini 
1096dd7b254dSGiuliano Pochini 
1097dd7b254dSGiuliano Pochini static int free_pipes(struct echoaudio *chip, struct audiopipe *pipe)
1098dd7b254dSGiuliano Pochini {
1099dd7b254dSGiuliano Pochini 	u32 channel_mask;
1100dd7b254dSGiuliano Pochini 	int i;
1101dd7b254dSGiuliano Pochini 
1102da3cec35STakashi Iwai 	if (snd_BUG_ON(!is_pipe_allocated(chip, pipe->index)))
1103da3cec35STakashi Iwai 		return -EINVAL;
1104da3cec35STakashi Iwai 	if (snd_BUG_ON(pipe->state != PIPE_STATE_STOPPED))
1105da3cec35STakashi Iwai 		return -EINVAL;
1106dd7b254dSGiuliano Pochini 
1107dd7b254dSGiuliano Pochini 	for (channel_mask = i = 0; i < pipe->interleave; i++)
1108dd7b254dSGiuliano Pochini 		channel_mask |= 1 << (pipe->index + i);
1109dd7b254dSGiuliano Pochini 
1110dd7b254dSGiuliano Pochini 	chip->pipe_alloc_mask &= ~channel_mask;
1111dd7b254dSGiuliano Pochini 	chip->pipe_cyclic_mask &= ~channel_mask;
1112dd7b254dSGiuliano Pochini 	return 0;
1113dd7b254dSGiuliano Pochini }
1114dd7b254dSGiuliano Pochini 
1115dd7b254dSGiuliano Pochini 
1116dd7b254dSGiuliano Pochini 
1117dd7b254dSGiuliano Pochini /******************************************************************************
1118dd7b254dSGiuliano Pochini 	Functions for managing the scatter-gather list
1119dd7b254dSGiuliano Pochini ******************************************************************************/
1120dd7b254dSGiuliano Pochini 
1121dd7b254dSGiuliano Pochini static int sglist_init(struct echoaudio *chip, struct audiopipe *pipe)
1122dd7b254dSGiuliano Pochini {
1123dd7b254dSGiuliano Pochini 	pipe->sglist_head = 0;
1124dd7b254dSGiuliano Pochini 	memset(pipe->sgpage.area, 0, PAGE_SIZE);
1125dd7b254dSGiuliano Pochini 	chip->comm_page->sglist_addr[pipe->index].addr =
1126dd7b254dSGiuliano Pochini 		cpu_to_le32(pipe->sgpage.addr);
1127dd7b254dSGiuliano Pochini 	return 0;
1128dd7b254dSGiuliano Pochini }
1129dd7b254dSGiuliano Pochini 
1130dd7b254dSGiuliano Pochini 
1131dd7b254dSGiuliano Pochini 
1132dd7b254dSGiuliano Pochini static int sglist_add_mapping(struct echoaudio *chip, struct audiopipe *pipe,
1133dd7b254dSGiuliano Pochini 				dma_addr_t address, size_t length)
1134dd7b254dSGiuliano Pochini {
1135dd7b254dSGiuliano Pochini 	int head = pipe->sglist_head;
1136dd7b254dSGiuliano Pochini 	struct sg_entry *list = (struct sg_entry *)pipe->sgpage.area;
1137dd7b254dSGiuliano Pochini 
1138dd7b254dSGiuliano Pochini 	if (head < MAX_SGLIST_ENTRIES - 1) {
1139dd7b254dSGiuliano Pochini 		list[head].addr = cpu_to_le32(address);
1140dd7b254dSGiuliano Pochini 		list[head].size = cpu_to_le32(length);
1141dd7b254dSGiuliano Pochini 		pipe->sglist_head++;
1142dd7b254dSGiuliano Pochini 	} else {
1143b5b4a41bSSudip Mukherjee 		dev_err(chip->card->dev, "SGlist: too many fragments\n");
1144dd7b254dSGiuliano Pochini 		return -ENOMEM;
1145dd7b254dSGiuliano Pochini 	}
1146dd7b254dSGiuliano Pochini 	return 0;
1147dd7b254dSGiuliano Pochini }
1148dd7b254dSGiuliano Pochini 
1149dd7b254dSGiuliano Pochini 
1150dd7b254dSGiuliano Pochini 
1151dd7b254dSGiuliano Pochini static inline int sglist_add_irq(struct echoaudio *chip, struct audiopipe *pipe)
1152dd7b254dSGiuliano Pochini {
1153dd7b254dSGiuliano Pochini 	return sglist_add_mapping(chip, pipe, 0, 0);
1154dd7b254dSGiuliano Pochini }
1155dd7b254dSGiuliano Pochini 
1156dd7b254dSGiuliano Pochini 
1157dd7b254dSGiuliano Pochini 
1158dd7b254dSGiuliano Pochini static inline int sglist_wrap(struct echoaudio *chip, struct audiopipe *pipe)
1159dd7b254dSGiuliano Pochini {
1160dd7b254dSGiuliano Pochini 	return sglist_add_mapping(chip, pipe, pipe->sgpage.addr, 0);
1161dd7b254dSGiuliano Pochini }
1162