• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *   This program is free software; you can redistribute it and/or modify
3  *   it under the terms of the GNU General Public License as published by
4  *   the Free Software Foundation; either version 2 of the License, or
5  *   (at your option) any later version.
6  *
7  *   This program is distributed in the hope that it will be useful,
8  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *   GNU General Public License for more details.
11  *
12  *   You should have received a copy of the GNU General Public License
13  *   along with this program; if not, write to the Free Software
14  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15  *
16  */
17 
18 /*
19  * 2002-07 Benny Sjostrand benny@hostmobility.com
20  */
21 
22 
23 #include <linux/io.h>
24 #include <linux/delay.h>
25 #include <linux/pm.h>
26 #include <linux/init.h>
27 #include <linux/slab.h>
28 #include <linux/vmalloc.h>
29 #include <linux/mutex.h>
30 
31 #include <sound/core.h>
32 #include <sound/control.h>
33 #include <sound/info.h>
34 #include <sound/asoundef.h>
35 #include "cs46xx.h"
36 
37 #include "cs46xx_lib.h"
38 #include "dsp_spos.h"
39 
40 static int cs46xx_dsp_async_init (struct snd_cs46xx *chip,
41 				  struct dsp_scb_descriptor * fg_entry);
42 
43 static enum wide_opcode wide_opcodes[] = {
44 	WIDE_FOR_BEGIN_LOOP,
45 	WIDE_FOR_BEGIN_LOOP2,
46 	WIDE_COND_GOTO_ADDR,
47 	WIDE_COND_GOTO_CALL,
48 	WIDE_TBEQ_COND_GOTO_ADDR,
49 	WIDE_TBEQ_COND_CALL_ADDR,
50 	WIDE_TBEQ_NCOND_GOTO_ADDR,
51 	WIDE_TBEQ_NCOND_CALL_ADDR,
52 	WIDE_TBEQ_COND_GOTO1_ADDR,
53 	WIDE_TBEQ_COND_CALL1_ADDR,
54 	WIDE_TBEQ_NCOND_GOTOI_ADDR,
55 	WIDE_TBEQ_NCOND_CALL1_ADDR
56 };
57 
shadow_and_reallocate_code(struct snd_cs46xx * chip,u32 * data,u32 size,u32 overlay_begin_address)58 static int shadow_and_reallocate_code (struct snd_cs46xx * chip, u32 * data, u32 size,
59 				       u32 overlay_begin_address)
60 {
61 	unsigned int i = 0, j, nreallocated = 0;
62 	u32 hival,loval,address;
63 	u32 mop_operands,mop_type,wide_op;
64 	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
65 
66 	if (snd_BUG_ON(size %2))
67 		return -EINVAL;
68 
69 	while (i < size) {
70 		loval = data[i++];
71 		hival = data[i++];
72 
73 		if (ins->code.offset > 0) {
74 			mop_operands = (hival >> 6) & 0x03fff;
75 			mop_type = mop_operands >> 10;
76 
77 			/* check for wide type instruction */
78 			if (mop_type == 0 &&
79 			    (mop_operands & WIDE_LADD_INSTR_MASK) == 0 &&
80 			    (mop_operands & WIDE_INSTR_MASK) != 0) {
81 				wide_op = loval & 0x7f;
82 				for (j = 0;j < ARRAY_SIZE(wide_opcodes); ++j) {
83 					if (wide_opcodes[j] == wide_op) {
84 						/* need to reallocate instruction */
85 						address  = (hival & 0x00FFF) << 5;
86 						address |=  loval >> 15;
87 
88 						dev_dbg(chip->card->dev,
89 							"handle_wideop[1]: %05x:%05x addr %04x\n",
90 							hival, loval, address);
91 
92 						if ( !(address & 0x8000) ) {
93 							address += (ins->code.offset / 2) - overlay_begin_address;
94 						} else {
95 							dev_dbg(chip->card->dev,
96 								"handle_wideop[1]: ROM symbol not reallocated\n");
97 						}
98 
99 						hival &= 0xFF000;
100 						loval &= 0x07FFF;
101 
102 						hival |= ( (address >> 5)  & 0x00FFF);
103 						loval |= ( (address << 15) & 0xF8000);
104 
105 						address  = (hival & 0x00FFF) << 5;
106 						address |=  loval >> 15;
107 
108 						dev_dbg(chip->card->dev,
109 							"handle_wideop:[2] %05x:%05x addr %04x\n",
110 							hival, loval, address);						nreallocated++;
111 					} /* wide_opcodes[j] == wide_op */
112 				} /* for */
113 			} /* mod_type == 0 ... */
114 		} /* ins->code.offset > 0 */
115 
116 		ins->code.data[ins->code.size++] = loval;
117 		ins->code.data[ins->code.size++] = hival;
118 	}
119 
120 	dev_dbg(chip->card->dev,
121 		"dsp_spos: %d instructions reallocated\n", nreallocated);
122 	return nreallocated;
123 }
124 
get_segment_desc(struct dsp_module_desc * module,int seg_type)125 static struct dsp_segment_desc * get_segment_desc (struct dsp_module_desc * module, int seg_type)
126 {
127 	int i;
128 	for (i = 0;i < module->nsegments; ++i) {
129 		if (module->segments[i].segment_type == seg_type) {
130 			return (module->segments + i);
131 		}
132 	}
133 
134 	return NULL;
135 };
136 
find_free_symbol_index(struct dsp_spos_instance * ins)137 static int find_free_symbol_index (struct dsp_spos_instance * ins)
138 {
139 	int index = ins->symbol_table.nsymbols,i;
140 
141 	for (i = ins->symbol_table.highest_frag_index; i < ins->symbol_table.nsymbols; ++i) {
142 		if (ins->symbol_table.symbols[i].deleted) {
143 			index = i;
144 			break;
145 		}
146 	}
147 
148 	return index;
149 }
150 
add_symbols(struct snd_cs46xx * chip,struct dsp_module_desc * module)151 static int add_symbols (struct snd_cs46xx * chip, struct dsp_module_desc * module)
152 {
153 	int i;
154 	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
155 
156 	if (module->symbol_table.nsymbols > 0) {
157 		if (!strcmp(module->symbol_table.symbols[0].symbol_name, "OVERLAYBEGINADDRESS") &&
158 		    module->symbol_table.symbols[0].symbol_type == SYMBOL_CONSTANT ) {
159 			module->overlay_begin_address = module->symbol_table.symbols[0].address;
160 		}
161 	}
162 
163 	for (i = 0;i < module->symbol_table.nsymbols; ++i) {
164 		if (ins->symbol_table.nsymbols == (DSP_MAX_SYMBOLS - 1)) {
165 			dev_err(chip->card->dev,
166 				"dsp_spos: symbol table is full\n");
167 			return -ENOMEM;
168 		}
169 
170 
171 		if (cs46xx_dsp_lookup_symbol(chip,
172 					     module->symbol_table.symbols[i].symbol_name,
173 					     module->symbol_table.symbols[i].symbol_type) == NULL) {
174 
175 			ins->symbol_table.symbols[ins->symbol_table.nsymbols] = module->symbol_table.symbols[i];
176 			ins->symbol_table.symbols[ins->symbol_table.nsymbols].address += ((ins->code.offset / 2) - module->overlay_begin_address);
177 			ins->symbol_table.symbols[ins->symbol_table.nsymbols].module = module;
178 			ins->symbol_table.symbols[ins->symbol_table.nsymbols].deleted = 0;
179 
180 			if (ins->symbol_table.nsymbols > ins->symbol_table.highest_frag_index)
181 				ins->symbol_table.highest_frag_index = ins->symbol_table.nsymbols;
182 
183 			ins->symbol_table.nsymbols++;
184 		} else {
185 #if 0
186 			dev_dbg(chip->card->dev,
187 				"dsp_spos: symbol <%s> duplicated, probably nothing wrong with that (Cirrus?)\n",
188 				module->symbol_table.symbols[i].symbol_name); */
189 #endif
190 		}
191 	}
192 
193 	return 0;
194 }
195 
196 static struct dsp_symbol_entry *
add_symbol(struct snd_cs46xx * chip,char * symbol_name,u32 address,int type)197 add_symbol (struct snd_cs46xx * chip, char * symbol_name, u32 address, int type)
198 {
199 	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
200 	struct dsp_symbol_entry * symbol = NULL;
201 	int index;
202 
203 	if (ins->symbol_table.nsymbols == (DSP_MAX_SYMBOLS - 1)) {
204 		dev_err(chip->card->dev, "dsp_spos: symbol table is full\n");
205 		return NULL;
206 	}
207 
208 	if (cs46xx_dsp_lookup_symbol(chip,
209 				     symbol_name,
210 				     type) != NULL) {
211 		dev_err(chip->card->dev,
212 			"dsp_spos: symbol <%s> duplicated\n", symbol_name);
213 		return NULL;
214 	}
215 
216 	index = find_free_symbol_index (ins);
217 
218 	strcpy (ins->symbol_table.symbols[index].symbol_name, symbol_name);
219 	ins->symbol_table.symbols[index].address = address;
220 	ins->symbol_table.symbols[index].symbol_type = type;
221 	ins->symbol_table.symbols[index].module = NULL;
222 	ins->symbol_table.symbols[index].deleted = 0;
223 	symbol = (ins->symbol_table.symbols + index);
224 
225 	if (index > ins->symbol_table.highest_frag_index)
226 		ins->symbol_table.highest_frag_index = index;
227 
228 	if (index == ins->symbol_table.nsymbols)
229 		ins->symbol_table.nsymbols++; /* no frag. in list */
230 
231 	return symbol;
232 }
233 
cs46xx_dsp_spos_create(struct snd_cs46xx * chip)234 struct dsp_spos_instance *cs46xx_dsp_spos_create (struct snd_cs46xx * chip)
235 {
236 	struct dsp_spos_instance * ins = kzalloc(sizeof(struct dsp_spos_instance), GFP_KERNEL);
237 
238 	if (ins == NULL)
239 		return NULL;
240 
241 	/* better to use vmalloc for this big table */
242 	ins->symbol_table.symbols = vmalloc(sizeof(struct dsp_symbol_entry) *
243 					    DSP_MAX_SYMBOLS);
244 	ins->code.data = kmalloc(DSP_CODE_BYTE_SIZE, GFP_KERNEL);
245 	ins->modules = kmalloc(sizeof(struct dsp_module_desc) * DSP_MAX_MODULES, GFP_KERNEL);
246 	if (!ins->symbol_table.symbols || !ins->code.data || !ins->modules) {
247 		cs46xx_dsp_spos_destroy(chip);
248 		goto error;
249 	}
250 	ins->symbol_table.nsymbols = 0;
251 	ins->symbol_table.highest_frag_index = 0;
252 	ins->code.offset = 0;
253 	ins->code.size = 0;
254 	ins->nscb = 0;
255 	ins->ntask = 0;
256 	ins->nmodules = 0;
257 
258 	/* default SPDIF input sample rate
259 	   to 48000 khz */
260 	ins->spdif_in_sample_rate = 48000;
261 
262 	/* maximize volume */
263 	ins->dac_volume_right = 0x8000;
264 	ins->dac_volume_left = 0x8000;
265 	ins->spdif_input_volume_right = 0x8000;
266 	ins->spdif_input_volume_left = 0x8000;
267 
268 	/* set left and right validity bits and
269 	   default channel status */
270 	ins->spdif_csuv_default =
271 		ins->spdif_csuv_stream =
272 	 /* byte 0 */  ((unsigned int)_wrap_all_bits(  (SNDRV_PCM_DEFAULT_CON_SPDIF        & 0xff)) << 24) |
273 	 /* byte 1 */  ((unsigned int)_wrap_all_bits( ((SNDRV_PCM_DEFAULT_CON_SPDIF >> 8) & 0xff)) << 16) |
274 	 /* byte 3 */   (unsigned int)_wrap_all_bits(  (SNDRV_PCM_DEFAULT_CON_SPDIF >> 24) & 0xff) |
275 	 /* left and right validity bits */ (1 << 13) | (1 << 12);
276 
277 	return ins;
278 
279 error:
280 	kfree(ins->modules);
281 	kfree(ins->code.data);
282 	vfree(ins->symbol_table.symbols);
283 	kfree(ins);
284 	return NULL;
285 }
286 
cs46xx_dsp_spos_destroy(struct snd_cs46xx * chip)287 void  cs46xx_dsp_spos_destroy (struct snd_cs46xx * chip)
288 {
289 	int i;
290 	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
291 
292 	if (snd_BUG_ON(!ins))
293 		return;
294 
295 	mutex_lock(&chip->spos_mutex);
296 	for (i = 0; i < ins->nscb; ++i) {
297 		if (ins->scbs[i].deleted) continue;
298 
299 		cs46xx_dsp_proc_free_scb_desc ( (ins->scbs + i) );
300 #ifdef CONFIG_PM_SLEEP
301 		kfree(ins->scbs[i].data);
302 #endif
303 	}
304 
305 	kfree(ins->code.data);
306 	vfree(ins->symbol_table.symbols);
307 	kfree(ins->modules);
308 	kfree(ins);
309 	mutex_unlock(&chip->spos_mutex);
310 }
311 
dsp_load_parameter(struct snd_cs46xx * chip,struct dsp_segment_desc * parameter)312 static int dsp_load_parameter(struct snd_cs46xx *chip,
313 			      struct dsp_segment_desc *parameter)
314 {
315 	u32 doffset, dsize;
316 
317 	if (!parameter) {
318 		dev_dbg(chip->card->dev,
319 			"dsp_spos: module got no parameter segment\n");
320 		return 0;
321 	}
322 
323 	doffset = (parameter->offset * 4 + DSP_PARAMETER_BYTE_OFFSET);
324 	dsize   = parameter->size * 4;
325 
326 	dev_dbg(chip->card->dev,
327 		"dsp_spos: downloading parameter data to chip (%08x-%08x)\n",
328 		    doffset,doffset + dsize);
329 	if (snd_cs46xx_download (chip, parameter->data, doffset, dsize)) {
330 		dev_err(chip->card->dev,
331 			"dsp_spos: failed to download parameter data to DSP\n");
332 		return -EINVAL;
333 	}
334 	return 0;
335 }
336 
dsp_load_sample(struct snd_cs46xx * chip,struct dsp_segment_desc * sample)337 static int dsp_load_sample(struct snd_cs46xx *chip,
338 			   struct dsp_segment_desc *sample)
339 {
340 	u32 doffset, dsize;
341 
342 	if (!sample) {
343 		dev_dbg(chip->card->dev,
344 			"dsp_spos: module got no sample segment\n");
345 		return 0;
346 	}
347 
348 	doffset = (sample->offset * 4  + DSP_SAMPLE_BYTE_OFFSET);
349 	dsize   =  sample->size * 4;
350 
351 	dev_dbg(chip->card->dev,
352 		"dsp_spos: downloading sample data to chip (%08x-%08x)\n",
353 		    doffset,doffset + dsize);
354 
355 	if (snd_cs46xx_download (chip,sample->data,doffset,dsize)) {
356 		dev_err(chip->card->dev,
357 			"dsp_spos: failed to sample data to DSP\n");
358 		return -EINVAL;
359 	}
360 	return 0;
361 }
362 
cs46xx_dsp_load_module(struct snd_cs46xx * chip,struct dsp_module_desc * module)363 int cs46xx_dsp_load_module (struct snd_cs46xx * chip, struct dsp_module_desc * module)
364 {
365 	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
366 	struct dsp_segment_desc * code = get_segment_desc (module,SEGTYPE_SP_PROGRAM);
367 	u32 doffset, dsize;
368 	int err;
369 
370 	if (ins->nmodules == DSP_MAX_MODULES - 1) {
371 		dev_err(chip->card->dev,
372 			"dsp_spos: to many modules loaded into DSP\n");
373 		return -ENOMEM;
374 	}
375 
376 	dev_dbg(chip->card->dev,
377 		"dsp_spos: loading module %s into DSP\n", module->module_name);
378 
379 	if (ins->nmodules == 0) {
380 		dev_dbg(chip->card->dev, "dsp_spos: clearing parameter area\n");
381 		snd_cs46xx_clear_BA1(chip, DSP_PARAMETER_BYTE_OFFSET, DSP_PARAMETER_BYTE_SIZE);
382 	}
383 
384 	err = dsp_load_parameter(chip, get_segment_desc(module,
385 							SEGTYPE_SP_PARAMETER));
386 	if (err < 0)
387 		return err;
388 
389 	if (ins->nmodules == 0) {
390 		dev_dbg(chip->card->dev, "dsp_spos: clearing sample area\n");
391 		snd_cs46xx_clear_BA1(chip, DSP_SAMPLE_BYTE_OFFSET, DSP_SAMPLE_BYTE_SIZE);
392 	}
393 
394 	err = dsp_load_sample(chip, get_segment_desc(module,
395 						     SEGTYPE_SP_SAMPLE));
396 	if (err < 0)
397 		return err;
398 
399 	if (ins->nmodules == 0) {
400 		dev_dbg(chip->card->dev, "dsp_spos: clearing code area\n");
401 		snd_cs46xx_clear_BA1(chip, DSP_CODE_BYTE_OFFSET, DSP_CODE_BYTE_SIZE);
402 	}
403 
404 	if (code == NULL) {
405 		dev_dbg(chip->card->dev,
406 			"dsp_spos: module got no code segment\n");
407 	} else {
408 		if (ins->code.offset + code->size > DSP_CODE_BYTE_SIZE) {
409 			dev_err(chip->card->dev,
410 				"dsp_spos: no space available in DSP\n");
411 			return -ENOMEM;
412 		}
413 
414 		module->load_address = ins->code.offset;
415 		module->overlay_begin_address = 0x000;
416 
417 		/* if module has a code segment it must have
418 		   symbol table */
419 		if (snd_BUG_ON(!module->symbol_table.symbols))
420 			return -ENOMEM;
421 		if (add_symbols(chip,module)) {
422 			dev_err(chip->card->dev,
423 				"dsp_spos: failed to load symbol table\n");
424 			return -ENOMEM;
425 		}
426 
427 		doffset = (code->offset * 4 + ins->code.offset * 4 + DSP_CODE_BYTE_OFFSET);
428 		dsize   = code->size * 4;
429 		dev_dbg(chip->card->dev,
430 			"dsp_spos: downloading code to chip (%08x-%08x)\n",
431 			    doffset,doffset + dsize);
432 
433 		module->nfixups = shadow_and_reallocate_code(chip,code->data,code->size,module->overlay_begin_address);
434 
435 		if (snd_cs46xx_download (chip,(ins->code.data + ins->code.offset),doffset,dsize)) {
436 			dev_err(chip->card->dev,
437 				"dsp_spos: failed to download code to DSP\n");
438 			return -EINVAL;
439 		}
440 
441 		ins->code.offset += code->size;
442 	}
443 
444 	/* NOTE: module segments and symbol table must be
445 	   statically allocated. Case that module data is
446 	   not generated by the ospparser */
447 	ins->modules[ins->nmodules] = *module;
448 	ins->nmodules++;
449 
450 	return 0;
451 }
452 
453 struct dsp_symbol_entry *
cs46xx_dsp_lookup_symbol(struct snd_cs46xx * chip,char * symbol_name,int symbol_type)454 cs46xx_dsp_lookup_symbol (struct snd_cs46xx * chip, char * symbol_name, int symbol_type)
455 {
456 	int i;
457 	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
458 
459 	for ( i = 0; i < ins->symbol_table.nsymbols; ++i ) {
460 
461 		if (ins->symbol_table.symbols[i].deleted)
462 			continue;
463 
464 		if (!strcmp(ins->symbol_table.symbols[i].symbol_name,symbol_name) &&
465 		    ins->symbol_table.symbols[i].symbol_type == symbol_type) {
466 			return (ins->symbol_table.symbols + i);
467 		}
468 	}
469 
470 #if 0
471 	dev_err(chip->card->dev, "dsp_spos: symbol <%s> type %02x not found\n",
472 		symbol_name,symbol_type);
473 #endif
474 
475 	return NULL;
476 }
477 
478 
479 #ifdef CONFIG_SND_PROC_FS
480 static struct dsp_symbol_entry *
cs46xx_dsp_lookup_symbol_addr(struct snd_cs46xx * chip,u32 address,int symbol_type)481 cs46xx_dsp_lookup_symbol_addr (struct snd_cs46xx * chip, u32 address, int symbol_type)
482 {
483 	int i;
484 	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
485 
486 	for ( i = 0; i < ins->symbol_table.nsymbols; ++i ) {
487 
488 		if (ins->symbol_table.symbols[i].deleted)
489 			continue;
490 
491 		if (ins->symbol_table.symbols[i].address == address &&
492 		    ins->symbol_table.symbols[i].symbol_type == symbol_type) {
493 			return (ins->symbol_table.symbols + i);
494 		}
495 	}
496 
497 
498 	return NULL;
499 }
500 
501 
cs46xx_dsp_proc_symbol_table_read(struct snd_info_entry * entry,struct snd_info_buffer * buffer)502 static void cs46xx_dsp_proc_symbol_table_read (struct snd_info_entry *entry,
503 					       struct snd_info_buffer *buffer)
504 {
505 	struct snd_cs46xx *chip = entry->private_data;
506 	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
507 	int i;
508 
509 	snd_iprintf(buffer, "SYMBOLS:\n");
510 	for ( i = 0; i < ins->symbol_table.nsymbols; ++i ) {
511 		char *module_str = "system";
512 
513 		if (ins->symbol_table.symbols[i].deleted)
514 			continue;
515 
516 		if (ins->symbol_table.symbols[i].module != NULL) {
517 			module_str = ins->symbol_table.symbols[i].module->module_name;
518 		}
519 
520 
521 		snd_iprintf(buffer, "%04X <%02X> %s [%s]\n",
522 			    ins->symbol_table.symbols[i].address,
523 			    ins->symbol_table.symbols[i].symbol_type,
524 			    ins->symbol_table.symbols[i].symbol_name,
525 			    module_str);
526 	}
527 }
528 
529 
cs46xx_dsp_proc_modules_read(struct snd_info_entry * entry,struct snd_info_buffer * buffer)530 static void cs46xx_dsp_proc_modules_read (struct snd_info_entry *entry,
531 					  struct snd_info_buffer *buffer)
532 {
533 	struct snd_cs46xx *chip = entry->private_data;
534 	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
535 	int i,j;
536 
537 	mutex_lock(&chip->spos_mutex);
538 	snd_iprintf(buffer, "MODULES:\n");
539 	for ( i = 0; i < ins->nmodules; ++i ) {
540 		snd_iprintf(buffer, "\n%s:\n", ins->modules[i].module_name);
541 		snd_iprintf(buffer, "   %d symbols\n", ins->modules[i].symbol_table.nsymbols);
542 		snd_iprintf(buffer, "   %d fixups\n", ins->modules[i].nfixups);
543 
544 		for (j = 0; j < ins->modules[i].nsegments; ++ j) {
545 			struct dsp_segment_desc * desc = (ins->modules[i].segments + j);
546 			snd_iprintf(buffer, "   segment %02x offset %08x size %08x\n",
547 				    desc->segment_type,desc->offset, desc->size);
548 		}
549 	}
550 	mutex_unlock(&chip->spos_mutex);
551 }
552 
cs46xx_dsp_proc_task_tree_read(struct snd_info_entry * entry,struct snd_info_buffer * buffer)553 static void cs46xx_dsp_proc_task_tree_read (struct snd_info_entry *entry,
554 					    struct snd_info_buffer *buffer)
555 {
556 	struct snd_cs46xx *chip = entry->private_data;
557 	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
558 	int i, j, col;
559 	void __iomem *dst = chip->region.idx[1].remap_addr + DSP_PARAMETER_BYTE_OFFSET;
560 
561 	mutex_lock(&chip->spos_mutex);
562 	snd_iprintf(buffer, "TASK TREES:\n");
563 	for ( i = 0; i < ins->ntask; ++i) {
564 		snd_iprintf(buffer,"\n%04x %s:\n",ins->tasks[i].address,ins->tasks[i].task_name);
565 
566 		for (col = 0,j = 0;j < ins->tasks[i].size; j++,col++) {
567 			u32 val;
568 			if (col == 4) {
569 				snd_iprintf(buffer,"\n");
570 				col = 0;
571 			}
572 			val = readl(dst + (ins->tasks[i].address + j) * sizeof(u32));
573 			snd_iprintf(buffer,"%08x ",val);
574 		}
575 	}
576 
577 	snd_iprintf(buffer,"\n");
578 	mutex_unlock(&chip->spos_mutex);
579 }
580 
cs46xx_dsp_proc_scb_read(struct snd_info_entry * entry,struct snd_info_buffer * buffer)581 static void cs46xx_dsp_proc_scb_read (struct snd_info_entry *entry,
582 				      struct snd_info_buffer *buffer)
583 {
584 	struct snd_cs46xx *chip = entry->private_data;
585 	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
586 	int i;
587 
588 	mutex_lock(&chip->spos_mutex);
589 	snd_iprintf(buffer, "SCB's:\n");
590 	for ( i = 0; i < ins->nscb; ++i) {
591 		if (ins->scbs[i].deleted)
592 			continue;
593 		snd_iprintf(buffer,"\n%04x %s:\n\n",ins->scbs[i].address,ins->scbs[i].scb_name);
594 
595 		if (ins->scbs[i].parent_scb_ptr != NULL) {
596 			snd_iprintf(buffer,"parent [%s:%04x] ",
597 				    ins->scbs[i].parent_scb_ptr->scb_name,
598 				    ins->scbs[i].parent_scb_ptr->address);
599 		} else snd_iprintf(buffer,"parent [none] ");
600 
601 		snd_iprintf(buffer,"sub_list_ptr [%s:%04x]\nnext_scb_ptr [%s:%04x]  task_entry [%s:%04x]\n",
602 			    ins->scbs[i].sub_list_ptr->scb_name,
603 			    ins->scbs[i].sub_list_ptr->address,
604 			    ins->scbs[i].next_scb_ptr->scb_name,
605 			    ins->scbs[i].next_scb_ptr->address,
606 			    ins->scbs[i].task_entry->symbol_name,
607 			    ins->scbs[i].task_entry->address);
608 	}
609 
610 	snd_iprintf(buffer,"\n");
611 	mutex_unlock(&chip->spos_mutex);
612 }
613 
cs46xx_dsp_proc_parameter_dump_read(struct snd_info_entry * entry,struct snd_info_buffer * buffer)614 static void cs46xx_dsp_proc_parameter_dump_read (struct snd_info_entry *entry,
615 						 struct snd_info_buffer *buffer)
616 {
617 	struct snd_cs46xx *chip = entry->private_data;
618 	/*struct dsp_spos_instance * ins = chip->dsp_spos_instance; */
619 	unsigned int i, col = 0;
620 	void __iomem *dst = chip->region.idx[1].remap_addr + DSP_PARAMETER_BYTE_OFFSET;
621 	struct dsp_symbol_entry * symbol;
622 
623 	for (i = 0;i < DSP_PARAMETER_BYTE_SIZE; i += sizeof(u32),col ++) {
624 		if (col == 4) {
625 			snd_iprintf(buffer,"\n");
626 			col = 0;
627 		}
628 
629 		if ( (symbol = cs46xx_dsp_lookup_symbol_addr (chip,i / sizeof(u32), SYMBOL_PARAMETER)) != NULL) {
630 			col = 0;
631 			snd_iprintf (buffer,"\n%s:\n",symbol->symbol_name);
632 		}
633 
634 		if (col == 0) {
635 			snd_iprintf(buffer, "%04X ", i / (unsigned int)sizeof(u32));
636 		}
637 
638 		snd_iprintf(buffer,"%08X ",readl(dst + i));
639 	}
640 }
641 
cs46xx_dsp_proc_sample_dump_read(struct snd_info_entry * entry,struct snd_info_buffer * buffer)642 static void cs46xx_dsp_proc_sample_dump_read (struct snd_info_entry *entry,
643 					      struct snd_info_buffer *buffer)
644 {
645 	struct snd_cs46xx *chip = entry->private_data;
646 	int i,col = 0;
647 	void __iomem *dst = chip->region.idx[2].remap_addr;
648 
649 	snd_iprintf(buffer,"PCMREADER:\n");
650 	for (i = PCM_READER_BUF1;i < PCM_READER_BUF1 + 0x30; i += sizeof(u32),col ++) {
651 		if (col == 4) {
652 			snd_iprintf(buffer,"\n");
653 			col = 0;
654 		}
655 
656 		if (col == 0) {
657 			snd_iprintf(buffer, "%04X ",i);
658 		}
659 
660 		snd_iprintf(buffer,"%08X ",readl(dst + i));
661 	}
662 
663 	snd_iprintf(buffer,"\nMIX_SAMPLE_BUF1:\n");
664 
665 	col = 0;
666 	for (i = MIX_SAMPLE_BUF1;i < MIX_SAMPLE_BUF1 + 0x40; i += sizeof(u32),col ++) {
667 		if (col == 4) {
668 			snd_iprintf(buffer,"\n");
669 			col = 0;
670 		}
671 
672 		if (col == 0) {
673 			snd_iprintf(buffer, "%04X ",i);
674 		}
675 
676 		snd_iprintf(buffer,"%08X ",readl(dst + i));
677 	}
678 
679 	snd_iprintf(buffer,"\nSRC_TASK_SCB1:\n");
680 	col = 0;
681 	for (i = 0x2480 ; i < 0x2480 + 0x40 ; i += sizeof(u32),col ++) {
682 		if (col == 4) {
683 			snd_iprintf(buffer,"\n");
684 			col = 0;
685 		}
686 
687 		if (col == 0) {
688 			snd_iprintf(buffer, "%04X ",i);
689 		}
690 
691 		snd_iprintf(buffer,"%08X ",readl(dst + i));
692 	}
693 
694 
695 	snd_iprintf(buffer,"\nSPDIFO_BUFFER:\n");
696 	col = 0;
697 	for (i = SPDIFO_IP_OUTPUT_BUFFER1;i < SPDIFO_IP_OUTPUT_BUFFER1 + 0x30; i += sizeof(u32),col ++) {
698 		if (col == 4) {
699 			snd_iprintf(buffer,"\n");
700 			col = 0;
701 		}
702 
703 		if (col == 0) {
704 			snd_iprintf(buffer, "%04X ",i);
705 		}
706 
707 		snd_iprintf(buffer,"%08X ",readl(dst + i));
708 	}
709 
710 	snd_iprintf(buffer,"\n...\n");
711 	col = 0;
712 
713 	for (i = SPDIFO_IP_OUTPUT_BUFFER1+0xD0;i < SPDIFO_IP_OUTPUT_BUFFER1 + 0x110; i += sizeof(u32),col ++) {
714 		if (col == 4) {
715 			snd_iprintf(buffer,"\n");
716 			col = 0;
717 		}
718 
719 		if (col == 0) {
720 			snd_iprintf(buffer, "%04X ",i);
721 		}
722 
723 		snd_iprintf(buffer,"%08X ",readl(dst + i));
724 	}
725 
726 
727 	snd_iprintf(buffer,"\nOUTPUT_SNOOP:\n");
728 	col = 0;
729 	for (i = OUTPUT_SNOOP_BUFFER;i < OUTPUT_SNOOP_BUFFER + 0x40; i += sizeof(u32),col ++) {
730 		if (col == 4) {
731 			snd_iprintf(buffer,"\n");
732 			col = 0;
733 		}
734 
735 		if (col == 0) {
736 			snd_iprintf(buffer, "%04X ",i);
737 		}
738 
739 		snd_iprintf(buffer,"%08X ",readl(dst + i));
740 	}
741 
742 	snd_iprintf(buffer,"\nCODEC_INPUT_BUF1: \n");
743 	col = 0;
744 	for (i = CODEC_INPUT_BUF1;i < CODEC_INPUT_BUF1 + 0x40; i += sizeof(u32),col ++) {
745 		if (col == 4) {
746 			snd_iprintf(buffer,"\n");
747 			col = 0;
748 		}
749 
750 		if (col == 0) {
751 			snd_iprintf(buffer, "%04X ",i);
752 		}
753 
754 		snd_iprintf(buffer,"%08X ",readl(dst + i));
755 	}
756 #if 0
757 	snd_iprintf(buffer,"\nWRITE_BACK_BUF1: \n");
758 	col = 0;
759 	for (i = WRITE_BACK_BUF1;i < WRITE_BACK_BUF1 + 0x40; i += sizeof(u32),col ++) {
760 		if (col == 4) {
761 			snd_iprintf(buffer,"\n");
762 			col = 0;
763 		}
764 
765 		if (col == 0) {
766 			snd_iprintf(buffer, "%04X ",i);
767 		}
768 
769 		snd_iprintf(buffer,"%08X ",readl(dst + i));
770 	}
771 #endif
772 
773 	snd_iprintf(buffer,"\nSPDIFI_IP_OUTPUT_BUFFER1: \n");
774 	col = 0;
775 	for (i = SPDIFI_IP_OUTPUT_BUFFER1;i < SPDIFI_IP_OUTPUT_BUFFER1 + 0x80; i += sizeof(u32),col ++) {
776 		if (col == 4) {
777 			snd_iprintf(buffer,"\n");
778 			col = 0;
779 		}
780 
781 		if (col == 0) {
782 			snd_iprintf(buffer, "%04X ",i);
783 		}
784 
785 		snd_iprintf(buffer,"%08X ",readl(dst + i));
786 	}
787 	snd_iprintf(buffer,"\n");
788 }
789 
cs46xx_dsp_proc_init(struct snd_card * card,struct snd_cs46xx * chip)790 int cs46xx_dsp_proc_init (struct snd_card *card, struct snd_cs46xx *chip)
791 {
792 	struct snd_info_entry *entry;
793 	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
794 	int i;
795 
796 	ins->snd_card = card;
797 
798 	if ((entry = snd_info_create_card_entry(card, "dsp", card->proc_root)) != NULL) {
799 		entry->content = SNDRV_INFO_CONTENT_TEXT;
800 		entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
801 
802 		if (snd_info_register(entry) < 0) {
803 			snd_info_free_entry(entry);
804 			entry = NULL;
805 		}
806 	}
807 
808 	ins->proc_dsp_dir = entry;
809 
810 	if (!ins->proc_dsp_dir)
811 		return -ENOMEM;
812 
813 	if ((entry = snd_info_create_card_entry(card, "spos_symbols", ins->proc_dsp_dir)) != NULL) {
814 		entry->content = SNDRV_INFO_CONTENT_TEXT;
815 		entry->private_data = chip;
816 		entry->mode = S_IFREG | S_IRUGO | S_IWUSR;
817 		entry->c.text.read = cs46xx_dsp_proc_symbol_table_read;
818 		if (snd_info_register(entry) < 0) {
819 			snd_info_free_entry(entry);
820 			entry = NULL;
821 		}
822 	}
823 	ins->proc_sym_info_entry = entry;
824 
825 	if ((entry = snd_info_create_card_entry(card, "spos_modules", ins->proc_dsp_dir)) != NULL) {
826 		entry->content = SNDRV_INFO_CONTENT_TEXT;
827 		entry->private_data = chip;
828 		entry->mode = S_IFREG | S_IRUGO | S_IWUSR;
829 		entry->c.text.read = cs46xx_dsp_proc_modules_read;
830 		if (snd_info_register(entry) < 0) {
831 			snd_info_free_entry(entry);
832 			entry = NULL;
833 		}
834 	}
835 	ins->proc_modules_info_entry = entry;
836 
837 	if ((entry = snd_info_create_card_entry(card, "parameter", ins->proc_dsp_dir)) != NULL) {
838 		entry->content = SNDRV_INFO_CONTENT_TEXT;
839 		entry->private_data = chip;
840 		entry->mode = S_IFREG | S_IRUGO | S_IWUSR;
841 		entry->c.text.read = cs46xx_dsp_proc_parameter_dump_read;
842 		if (snd_info_register(entry) < 0) {
843 			snd_info_free_entry(entry);
844 			entry = NULL;
845 		}
846 	}
847 	ins->proc_parameter_dump_info_entry = entry;
848 
849 	if ((entry = snd_info_create_card_entry(card, "sample", ins->proc_dsp_dir)) != NULL) {
850 		entry->content = SNDRV_INFO_CONTENT_TEXT;
851 		entry->private_data = chip;
852 		entry->mode = S_IFREG | S_IRUGO | S_IWUSR;
853 		entry->c.text.read = cs46xx_dsp_proc_sample_dump_read;
854 		if (snd_info_register(entry) < 0) {
855 			snd_info_free_entry(entry);
856 			entry = NULL;
857 		}
858 	}
859 	ins->proc_sample_dump_info_entry = entry;
860 
861 	if ((entry = snd_info_create_card_entry(card, "task_tree", ins->proc_dsp_dir)) != NULL) {
862 		entry->content = SNDRV_INFO_CONTENT_TEXT;
863 		entry->private_data = chip;
864 		entry->mode = S_IFREG | S_IRUGO | S_IWUSR;
865 		entry->c.text.read = cs46xx_dsp_proc_task_tree_read;
866 		if (snd_info_register(entry) < 0) {
867 			snd_info_free_entry(entry);
868 			entry = NULL;
869 		}
870 	}
871 	ins->proc_task_info_entry = entry;
872 
873 	if ((entry = snd_info_create_card_entry(card, "scb_info", ins->proc_dsp_dir)) != NULL) {
874 		entry->content = SNDRV_INFO_CONTENT_TEXT;
875 		entry->private_data = chip;
876 		entry->mode = S_IFREG | S_IRUGO | S_IWUSR;
877 		entry->c.text.read = cs46xx_dsp_proc_scb_read;
878 		if (snd_info_register(entry) < 0) {
879 			snd_info_free_entry(entry);
880 			entry = NULL;
881 		}
882 	}
883 	ins->proc_scb_info_entry = entry;
884 
885 	mutex_lock(&chip->spos_mutex);
886 	/* register/update SCB's entries on proc */
887 	for (i = 0; i < ins->nscb; ++i) {
888 		if (ins->scbs[i].deleted) continue;
889 
890 		cs46xx_dsp_proc_register_scb_desc (chip, (ins->scbs + i));
891 	}
892 	mutex_unlock(&chip->spos_mutex);
893 
894 	return 0;
895 }
896 
cs46xx_dsp_proc_done(struct snd_cs46xx * chip)897 int cs46xx_dsp_proc_done (struct snd_cs46xx *chip)
898 {
899 	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
900 	int i;
901 
902 	if (!ins)
903 		return 0;
904 
905 	snd_info_free_entry(ins->proc_sym_info_entry);
906 	ins->proc_sym_info_entry = NULL;
907 
908 	snd_info_free_entry(ins->proc_modules_info_entry);
909 	ins->proc_modules_info_entry = NULL;
910 
911 	snd_info_free_entry(ins->proc_parameter_dump_info_entry);
912 	ins->proc_parameter_dump_info_entry = NULL;
913 
914 	snd_info_free_entry(ins->proc_sample_dump_info_entry);
915 	ins->proc_sample_dump_info_entry = NULL;
916 
917 	snd_info_free_entry(ins->proc_scb_info_entry);
918 	ins->proc_scb_info_entry = NULL;
919 
920 	snd_info_free_entry(ins->proc_task_info_entry);
921 	ins->proc_task_info_entry = NULL;
922 
923 	mutex_lock(&chip->spos_mutex);
924 	for (i = 0; i < ins->nscb; ++i) {
925 		if (ins->scbs[i].deleted) continue;
926 		cs46xx_dsp_proc_free_scb_desc ( (ins->scbs + i) );
927 	}
928 	mutex_unlock(&chip->spos_mutex);
929 
930 	snd_info_free_entry(ins->proc_dsp_dir);
931 	ins->proc_dsp_dir = NULL;
932 
933 	return 0;
934 }
935 #endif /* CONFIG_SND_PROC_FS */
936 
_dsp_create_task_tree(struct snd_cs46xx * chip,u32 * task_data,u32 dest,int size)937 static void _dsp_create_task_tree (struct snd_cs46xx *chip, u32 * task_data,
938 				   u32  dest, int size)
939 {
940 	void __iomem *spdst = chip->region.idx[1].remap_addr +
941 		DSP_PARAMETER_BYTE_OFFSET + dest * sizeof(u32);
942 	int i;
943 
944 	for (i = 0; i < size; ++i) {
945 		dev_dbg(chip->card->dev, "addr %p, val %08x\n",
946 			spdst, task_data[i]);
947 		writel(task_data[i],spdst);
948 		spdst += sizeof(u32);
949 	}
950 }
951 
_dsp_create_scb(struct snd_cs46xx * chip,u32 * scb_data,u32 dest)952 static void _dsp_create_scb (struct snd_cs46xx *chip, u32 * scb_data, u32 dest)
953 {
954 	void __iomem *spdst = chip->region.idx[1].remap_addr +
955 		DSP_PARAMETER_BYTE_OFFSET + dest * sizeof(u32);
956 	int i;
957 
958 	for (i = 0; i < 0x10; ++i) {
959 		dev_dbg(chip->card->dev, "addr %p, val %08x\n",
960 			spdst, scb_data[i]);
961 		writel(scb_data[i],spdst);
962 		spdst += sizeof(u32);
963 	}
964 }
965 
find_free_scb_index(struct dsp_spos_instance * ins)966 static int find_free_scb_index (struct dsp_spos_instance * ins)
967 {
968 	int index = ins->nscb, i;
969 
970 	for (i = ins->scb_highest_frag_index; i < ins->nscb; ++i) {
971 		if (ins->scbs[i].deleted) {
972 			index = i;
973 			break;
974 		}
975 	}
976 
977 	return index;
978 }
979 
_map_scb(struct snd_cs46xx * chip,char * name,u32 dest)980 static struct dsp_scb_descriptor * _map_scb (struct snd_cs46xx *chip, char * name, u32 dest)
981 {
982 	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
983 	struct dsp_scb_descriptor * desc = NULL;
984 	int index;
985 
986 	if (ins->nscb == DSP_MAX_SCB_DESC - 1) {
987 		dev_err(chip->card->dev,
988 			"dsp_spos: got no place for other SCB\n");
989 		return NULL;
990 	}
991 
992 	index = find_free_scb_index (ins);
993 
994 	memset(&ins->scbs[index], 0, sizeof(ins->scbs[index]));
995 	strcpy(ins->scbs[index].scb_name, name);
996 	ins->scbs[index].address = dest;
997 	ins->scbs[index].index = index;
998 	ins->scbs[index].ref_count = 1;
999 
1000 	desc = (ins->scbs + index);
1001 	ins->scbs[index].scb_symbol = add_symbol (chip, name, dest, SYMBOL_PARAMETER);
1002 
1003 	if (index > ins->scb_highest_frag_index)
1004 		ins->scb_highest_frag_index = index;
1005 
1006 	if (index == ins->nscb)
1007 		ins->nscb++;
1008 
1009 	return desc;
1010 }
1011 
1012 static struct dsp_task_descriptor *
_map_task_tree(struct snd_cs46xx * chip,char * name,u32 dest,u32 size)1013 _map_task_tree (struct snd_cs46xx *chip, char * name, u32 dest, u32 size)
1014 {
1015 	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
1016 	struct dsp_task_descriptor * desc = NULL;
1017 
1018 	if (ins->ntask == DSP_MAX_TASK_DESC - 1) {
1019 		dev_err(chip->card->dev,
1020 			"dsp_spos: got no place for other TASK\n");
1021 		return NULL;
1022 	}
1023 
1024 	if (name)
1025 		strcpy(ins->tasks[ins->ntask].task_name, name);
1026 	else
1027 		strcpy(ins->tasks[ins->ntask].task_name, "(NULL)");
1028 	ins->tasks[ins->ntask].address = dest;
1029 	ins->tasks[ins->ntask].size = size;
1030 
1031 	/* quick find in list */
1032 	ins->tasks[ins->ntask].index = ins->ntask;
1033 	desc = (ins->tasks + ins->ntask);
1034 	ins->ntask++;
1035 
1036 	if (name)
1037 		add_symbol (chip,name,dest,SYMBOL_PARAMETER);
1038 	return desc;
1039 }
1040 
1041 #define SCB_BYTES	(0x10 * 4)
1042 
1043 struct dsp_scb_descriptor *
cs46xx_dsp_create_scb(struct snd_cs46xx * chip,char * name,u32 * scb_data,u32 dest)1044 cs46xx_dsp_create_scb (struct snd_cs46xx *chip, char * name, u32 * scb_data, u32 dest)
1045 {
1046 	struct dsp_scb_descriptor * desc;
1047 
1048 #ifdef CONFIG_PM_SLEEP
1049 	/* copy the data for resume */
1050 	scb_data = kmemdup(scb_data, SCB_BYTES, GFP_KERNEL);
1051 	if (!scb_data)
1052 		return NULL;
1053 #endif
1054 
1055 	desc = _map_scb (chip,name,dest);
1056 	if (desc) {
1057 		desc->data = scb_data;
1058 		_dsp_create_scb(chip,scb_data,dest);
1059 	} else {
1060 		dev_err(chip->card->dev, "dsp_spos: failed to map SCB\n");
1061 #ifdef CONFIG_PM_SLEEP
1062 		kfree(scb_data);
1063 #endif
1064 	}
1065 
1066 	return desc;
1067 }
1068 
1069 
1070 static struct dsp_task_descriptor *
cs46xx_dsp_create_task_tree(struct snd_cs46xx * chip,char * name,u32 * task_data,u32 dest,int size)1071 cs46xx_dsp_create_task_tree (struct snd_cs46xx *chip, char * name, u32 * task_data,
1072 			     u32 dest, int size)
1073 {
1074 	struct dsp_task_descriptor * desc;
1075 
1076 	desc = _map_task_tree (chip,name,dest,size);
1077 	if (desc) {
1078 		desc->data = task_data;
1079 		_dsp_create_task_tree(chip,task_data,dest,size);
1080 	} else {
1081 		dev_err(chip->card->dev, "dsp_spos: failed to map TASK\n");
1082 	}
1083 
1084 	return desc;
1085 }
1086 
cs46xx_dsp_scb_and_task_init(struct snd_cs46xx * chip)1087 int cs46xx_dsp_scb_and_task_init (struct snd_cs46xx *chip)
1088 {
1089 	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
1090 	struct dsp_symbol_entry * fg_task_tree_header_code;
1091 	struct dsp_symbol_entry * task_tree_header_code;
1092 	struct dsp_symbol_entry * task_tree_thread;
1093 	struct dsp_symbol_entry * null_algorithm;
1094 	struct dsp_symbol_entry * magic_snoop_task;
1095 
1096 	struct dsp_scb_descriptor * timing_master_scb;
1097 	struct dsp_scb_descriptor * codec_out_scb;
1098 	struct dsp_scb_descriptor * codec_in_scb;
1099 	struct dsp_scb_descriptor * src_task_scb;
1100 	struct dsp_scb_descriptor * master_mix_scb;
1101 	struct dsp_scb_descriptor * rear_mix_scb;
1102 	struct dsp_scb_descriptor * record_mix_scb;
1103 	struct dsp_scb_descriptor * write_back_scb;
1104 	struct dsp_scb_descriptor * vari_decimate_scb;
1105 	struct dsp_scb_descriptor * rear_codec_out_scb;
1106 	struct dsp_scb_descriptor * clfe_codec_out_scb;
1107 	struct dsp_scb_descriptor * magic_snoop_scb;
1108 
1109 	int fifo_addr, fifo_span, valid_slots;
1110 
1111 	static struct dsp_spos_control_block sposcb = {
1112 		/* 0 */ HFG_TREE_SCB,HFG_STACK,
1113 		/* 1 */ SPOSCB_ADDR,BG_TREE_SCB_ADDR,
1114 		/* 2 */ DSP_SPOS_DC,0,
1115 		/* 3 */ DSP_SPOS_DC,DSP_SPOS_DC,
1116 		/* 4 */ 0,0,
1117 		/* 5 */ DSP_SPOS_UU,0,
1118 		/* 6 */ FG_TASK_HEADER_ADDR,0,
1119 		/* 7 */ 0,0,
1120 		/* 8 */ DSP_SPOS_UU,DSP_SPOS_DC,
1121 		/* 9 */ 0,
1122 		/* A */ 0,HFG_FIRST_EXECUTE_MODE,
1123 		/* B */ DSP_SPOS_UU,DSP_SPOS_UU,
1124 		/* C */ DSP_SPOS_DC_DC,
1125 		/* D */ DSP_SPOS_DC_DC,
1126 		/* E */ DSP_SPOS_DC_DC,
1127 		/* F */ DSP_SPOS_DC_DC
1128 	};
1129 
1130 	cs46xx_dsp_create_task_tree(chip, "sposCB", (u32 *)&sposcb, SPOSCB_ADDR, 0x10);
1131 
1132 	null_algorithm  = cs46xx_dsp_lookup_symbol(chip, "NULLALGORITHM", SYMBOL_CODE);
1133 	if (null_algorithm == NULL) {
1134 		dev_err(chip->card->dev,
1135 			"dsp_spos: symbol NULLALGORITHM not found\n");
1136 		return -EIO;
1137 	}
1138 
1139 	fg_task_tree_header_code = cs46xx_dsp_lookup_symbol(chip, "FGTASKTREEHEADERCODE", SYMBOL_CODE);
1140 	if (fg_task_tree_header_code == NULL) {
1141 		dev_err(chip->card->dev,
1142 			"dsp_spos: symbol FGTASKTREEHEADERCODE not found\n");
1143 		return -EIO;
1144 	}
1145 
1146 	task_tree_header_code = cs46xx_dsp_lookup_symbol(chip, "TASKTREEHEADERCODE", SYMBOL_CODE);
1147 	if (task_tree_header_code == NULL) {
1148 		dev_err(chip->card->dev,
1149 			"dsp_spos: symbol TASKTREEHEADERCODE not found\n");
1150 		return -EIO;
1151 	}
1152 
1153 	task_tree_thread = cs46xx_dsp_lookup_symbol(chip, "TASKTREETHREAD", SYMBOL_CODE);
1154 	if (task_tree_thread == NULL) {
1155 		dev_err(chip->card->dev,
1156 			"dsp_spos: symbol TASKTREETHREAD not found\n");
1157 		return -EIO;
1158 	}
1159 
1160 	magic_snoop_task = cs46xx_dsp_lookup_symbol(chip, "MAGICSNOOPTASK", SYMBOL_CODE);
1161 	if (magic_snoop_task == NULL) {
1162 		dev_err(chip->card->dev,
1163 			"dsp_spos: symbol MAGICSNOOPTASK not found\n");
1164 		return -EIO;
1165 	}
1166 
1167 	{
1168 		/* create the null SCB */
1169 		static struct dsp_generic_scb null_scb = {
1170 			{ 0, 0, 0, 0 },
1171 			{ 0, 0, 0, 0, 0 },
1172 			NULL_SCB_ADDR, NULL_SCB_ADDR,
1173 			0, 0, 0, 0, 0,
1174 			{
1175 				0,0,
1176 				0,0,
1177 			}
1178 		};
1179 
1180 		null_scb.entry_point = null_algorithm->address;
1181 		ins->the_null_scb = cs46xx_dsp_create_scb(chip, "nullSCB", (u32 *)&null_scb, NULL_SCB_ADDR);
1182 		ins->the_null_scb->task_entry = null_algorithm;
1183 		ins->the_null_scb->sub_list_ptr = ins->the_null_scb;
1184 		ins->the_null_scb->next_scb_ptr = ins->the_null_scb;
1185 		ins->the_null_scb->parent_scb_ptr = NULL;
1186 		cs46xx_dsp_proc_register_scb_desc (chip,ins->the_null_scb);
1187 	}
1188 
1189 	{
1190 		/* setup foreground task tree */
1191 		static struct dsp_task_tree_control_block fg_task_tree_hdr =  {
1192 			{ FG_TASK_HEADER_ADDR | (DSP_SPOS_DC << 0x10),
1193 			  DSP_SPOS_DC_DC,
1194 			  DSP_SPOS_DC_DC,
1195 			  0x0000,DSP_SPOS_DC,
1196 			  DSP_SPOS_DC, DSP_SPOS_DC,
1197 			  DSP_SPOS_DC_DC,
1198 			  DSP_SPOS_DC_DC,
1199 			  DSP_SPOS_DC_DC,
1200 			  DSP_SPOS_DC,DSP_SPOS_DC },
1201 
1202 			{
1203 				BG_TREE_SCB_ADDR,TIMINGMASTER_SCB_ADDR,
1204 				0,
1205 				FG_TASK_HEADER_ADDR + TCBData,
1206 			},
1207 
1208 			{
1209 				4,0,
1210 				1,0,
1211 				2,SPOSCB_ADDR + HFGFlags,
1212 				0,0,
1213 				FG_TASK_HEADER_ADDR + TCBContextBlk,FG_STACK
1214 			},
1215 
1216 			{
1217 				DSP_SPOS_DC,0,
1218 				DSP_SPOS_DC,DSP_SPOS_DC,
1219 				DSP_SPOS_DC,DSP_SPOS_DC,
1220 				DSP_SPOS_DC,DSP_SPOS_DC,
1221 				DSP_SPOS_DC,DSP_SPOS_DC,
1222 				DSP_SPOS_DCDC,
1223 				DSP_SPOS_UU,1,
1224 				DSP_SPOS_DCDC,
1225 				DSP_SPOS_DCDC,
1226 				DSP_SPOS_DCDC,
1227 				DSP_SPOS_DCDC,
1228 				DSP_SPOS_DCDC,
1229 				DSP_SPOS_DCDC,
1230 				DSP_SPOS_DCDC,
1231 				DSP_SPOS_DCDC,
1232 				DSP_SPOS_DCDC,
1233 				DSP_SPOS_DCDC,
1234 				DSP_SPOS_DCDC,
1235 				DSP_SPOS_DCDC,
1236 				DSP_SPOS_DCDC,
1237 				DSP_SPOS_DCDC,
1238 				DSP_SPOS_DCDC,
1239 				DSP_SPOS_DCDC,
1240 				DSP_SPOS_DCDC,
1241 				DSP_SPOS_DCDC,
1242 				DSP_SPOS_DCDC,
1243 				DSP_SPOS_DCDC,
1244 				DSP_SPOS_DCDC,
1245 				DSP_SPOS_DCDC,
1246 				DSP_SPOS_DCDC,
1247 				DSP_SPOS_DCDC,
1248 				DSP_SPOS_DCDC,
1249 				DSP_SPOS_DCDC,
1250 				DSP_SPOS_DCDC,
1251 				DSP_SPOS_DCDC
1252 			},
1253 			{
1254 				FG_INTERVAL_TIMER_PERIOD,DSP_SPOS_UU,
1255 				0,0
1256 			}
1257 		};
1258 
1259 		fg_task_tree_hdr.links.entry_point = fg_task_tree_header_code->address;
1260 		fg_task_tree_hdr.context_blk.stack0 = task_tree_thread->address;
1261 		cs46xx_dsp_create_task_tree(chip,"FGtaskTreeHdr",(u32 *)&fg_task_tree_hdr,FG_TASK_HEADER_ADDR,0x35);
1262 	}
1263 
1264 
1265 	{
1266 		/* setup foreground task tree */
1267 		static struct dsp_task_tree_control_block bg_task_tree_hdr =  {
1268 			{ DSP_SPOS_DC_DC,
1269 			  DSP_SPOS_DC_DC,
1270 			  DSP_SPOS_DC_DC,
1271 			  DSP_SPOS_DC, DSP_SPOS_DC,
1272 			  DSP_SPOS_DC, DSP_SPOS_DC,
1273 			  DSP_SPOS_DC_DC,
1274 			  DSP_SPOS_DC_DC,
1275 			  DSP_SPOS_DC_DC,
1276 			  DSP_SPOS_DC,DSP_SPOS_DC },
1277 
1278 			{
1279 				NULL_SCB_ADDR,NULL_SCB_ADDR,  /* Set up the background to do nothing */
1280 				0,
1281 				BG_TREE_SCB_ADDR + TCBData,
1282 			},
1283 
1284 			{
1285 				9999,0,
1286 				0,1,
1287 				0,SPOSCB_ADDR + HFGFlags,
1288 				0,0,
1289 				BG_TREE_SCB_ADDR + TCBContextBlk,BG_STACK
1290 			},
1291 
1292 			{
1293 				DSP_SPOS_DC,0,
1294 				DSP_SPOS_DC,DSP_SPOS_DC,
1295 				DSP_SPOS_DC,DSP_SPOS_DC,
1296 				DSP_SPOS_DC,DSP_SPOS_DC,
1297 				DSP_SPOS_DC,DSP_SPOS_DC,
1298 				DSP_SPOS_DCDC,
1299 				DSP_SPOS_UU,1,
1300 				DSP_SPOS_DCDC,
1301 				DSP_SPOS_DCDC,
1302 				DSP_SPOS_DCDC,
1303 				DSP_SPOS_DCDC,
1304 				DSP_SPOS_DCDC,
1305 				DSP_SPOS_DCDC,
1306 				DSP_SPOS_DCDC,
1307 				DSP_SPOS_DCDC,
1308 				DSP_SPOS_DCDC,
1309 				DSP_SPOS_DCDC,
1310 				DSP_SPOS_DCDC,
1311 				DSP_SPOS_DCDC,
1312 				DSP_SPOS_DCDC,
1313 				DSP_SPOS_DCDC,
1314 				DSP_SPOS_DCDC,
1315 				DSP_SPOS_DCDC,
1316 				DSP_SPOS_DCDC,
1317 				DSP_SPOS_DCDC,
1318 				DSP_SPOS_DCDC,
1319 				DSP_SPOS_DCDC,
1320 				DSP_SPOS_DCDC,
1321 				DSP_SPOS_DCDC,
1322 				DSP_SPOS_DCDC,
1323 				DSP_SPOS_DCDC,
1324 				DSP_SPOS_DCDC,
1325 				DSP_SPOS_DCDC,
1326 				DSP_SPOS_DCDC,
1327 				DSP_SPOS_DCDC
1328 			},
1329 			{
1330 				BG_INTERVAL_TIMER_PERIOD,DSP_SPOS_UU,
1331 				0,0
1332 			}
1333 		};
1334 
1335 		bg_task_tree_hdr.links.entry_point = task_tree_header_code->address;
1336 		bg_task_tree_hdr.context_blk.stack0 = task_tree_thread->address;
1337 		cs46xx_dsp_create_task_tree(chip,"BGtaskTreeHdr",(u32 *)&bg_task_tree_hdr,BG_TREE_SCB_ADDR,0x35);
1338 	}
1339 
1340 	/* create timing master SCB */
1341 	timing_master_scb = cs46xx_dsp_create_timing_master_scb(chip);
1342 
1343 	/* create the CODEC output task */
1344 	codec_out_scb = cs46xx_dsp_create_codec_out_scb(chip,"CodecOutSCB_I",0x0010,0x0000,
1345 							MASTERMIX_SCB_ADDR,
1346 							CODECOUT_SCB_ADDR,timing_master_scb,
1347 							SCB_ON_PARENT_SUBLIST_SCB);
1348 
1349 	if (!codec_out_scb) goto _fail_end;
1350 	/* create the master mix SCB */
1351 	master_mix_scb = cs46xx_dsp_create_mix_only_scb(chip,"MasterMixSCB",
1352 							MIX_SAMPLE_BUF1,MASTERMIX_SCB_ADDR,
1353 							codec_out_scb,
1354 							SCB_ON_PARENT_SUBLIST_SCB);
1355 	ins->master_mix_scb = master_mix_scb;
1356 
1357 	if (!master_mix_scb) goto _fail_end;
1358 
1359 	/* create codec in */
1360 	codec_in_scb = cs46xx_dsp_create_codec_in_scb(chip,"CodecInSCB",0x0010,0x00A0,
1361 						      CODEC_INPUT_BUF1,
1362 						      CODECIN_SCB_ADDR,codec_out_scb,
1363 						      SCB_ON_PARENT_NEXT_SCB);
1364 	if (!codec_in_scb) goto _fail_end;
1365 	ins->codec_in_scb = codec_in_scb;
1366 
1367 	/* create write back scb */
1368 	write_back_scb = cs46xx_dsp_create_mix_to_ostream_scb(chip,"WriteBackSCB",
1369 							      WRITE_BACK_BUF1,WRITE_BACK_SPB,
1370 							      WRITEBACK_SCB_ADDR,
1371 							      timing_master_scb,
1372 							      SCB_ON_PARENT_NEXT_SCB);
1373 	if (!write_back_scb) goto _fail_end;
1374 
1375 	{
1376 		static struct dsp_mix2_ostream_spb mix2_ostream_spb = {
1377 			0x00020000,
1378 			0x0000ffff
1379 		};
1380 
1381 		if (!cs46xx_dsp_create_task_tree(chip, NULL,
1382 						 (u32 *)&mix2_ostream_spb,
1383 						 WRITE_BACK_SPB, 2))
1384 			goto _fail_end;
1385 	}
1386 
1387 	/* input sample converter */
1388 	vari_decimate_scb = cs46xx_dsp_create_vari_decimate_scb(chip,"VariDecimateSCB",
1389 								VARI_DECIMATE_BUF0,
1390 								VARI_DECIMATE_BUF1,
1391 								VARIDECIMATE_SCB_ADDR,
1392 								write_back_scb,
1393 								SCB_ON_PARENT_SUBLIST_SCB);
1394 	if (!vari_decimate_scb) goto _fail_end;
1395 
1396 	/* create the record mixer SCB */
1397 	record_mix_scb = cs46xx_dsp_create_mix_only_scb(chip,"RecordMixerSCB",
1398 							MIX_SAMPLE_BUF2,
1399 							RECORD_MIXER_SCB_ADDR,
1400 							vari_decimate_scb,
1401 							SCB_ON_PARENT_SUBLIST_SCB);
1402 	ins->record_mixer_scb = record_mix_scb;
1403 
1404 	if (!record_mix_scb) goto _fail_end;
1405 
1406 	valid_slots = snd_cs46xx_peekBA0(chip, BA0_ACOSV);
1407 
1408 	if (snd_BUG_ON(chip->nr_ac97_codecs != 1 && chip->nr_ac97_codecs != 2))
1409 		goto _fail_end;
1410 
1411 	if (chip->nr_ac97_codecs == 1) {
1412 		/* output on slot 5 and 11
1413 		   on primary CODEC */
1414 		fifo_addr = 0x20;
1415 		fifo_span = 0x60;
1416 
1417 		/* enable slot 5 and 11 */
1418 		valid_slots |= ACOSV_SLV5 | ACOSV_SLV11;
1419 	} else {
1420 		/* output on slot 7 and 8
1421 		   on secondary CODEC */
1422 		fifo_addr = 0x40;
1423 		fifo_span = 0x10;
1424 
1425 		/* enable slot 7 and 8 */
1426 		valid_slots |= ACOSV_SLV7 | ACOSV_SLV8;
1427 	}
1428 	/* create CODEC tasklet for rear speakers output*/
1429 	rear_codec_out_scb = cs46xx_dsp_create_codec_out_scb(chip,"CodecOutSCB_Rear",fifo_span,fifo_addr,
1430 							     REAR_MIXER_SCB_ADDR,
1431 							     REAR_CODECOUT_SCB_ADDR,codec_in_scb,
1432 							     SCB_ON_PARENT_NEXT_SCB);
1433 	if (!rear_codec_out_scb) goto _fail_end;
1434 
1435 
1436 	/* create the rear PCM channel  mixer SCB */
1437 	rear_mix_scb = cs46xx_dsp_create_mix_only_scb(chip,"RearMixerSCB",
1438 						      MIX_SAMPLE_BUF3,
1439 						      REAR_MIXER_SCB_ADDR,
1440 						      rear_codec_out_scb,
1441 						      SCB_ON_PARENT_SUBLIST_SCB);
1442 	ins->rear_mix_scb = rear_mix_scb;
1443 	if (!rear_mix_scb) goto _fail_end;
1444 
1445 	if (chip->nr_ac97_codecs == 2) {
1446 		/* create CODEC tasklet for rear Center/LFE output
1447 		   slot 6 and 9 on seconadry CODEC */
1448 		clfe_codec_out_scb = cs46xx_dsp_create_codec_out_scb(chip,"CodecOutSCB_CLFE",0x0030,0x0030,
1449 								     CLFE_MIXER_SCB_ADDR,
1450 								     CLFE_CODEC_SCB_ADDR,
1451 								     rear_codec_out_scb,
1452 								     SCB_ON_PARENT_NEXT_SCB);
1453 		if (!clfe_codec_out_scb) goto _fail_end;
1454 
1455 
1456 		/* create the rear PCM channel  mixer SCB */
1457 		ins->center_lfe_mix_scb = cs46xx_dsp_create_mix_only_scb(chip,"CLFEMixerSCB",
1458 									 MIX_SAMPLE_BUF4,
1459 									 CLFE_MIXER_SCB_ADDR,
1460 									 clfe_codec_out_scb,
1461 									 SCB_ON_PARENT_SUBLIST_SCB);
1462 		if (!ins->center_lfe_mix_scb) goto _fail_end;
1463 
1464 		/* enable slot 6 and 9 */
1465 		valid_slots |= ACOSV_SLV6 | ACOSV_SLV9;
1466 	} else {
1467 		clfe_codec_out_scb = rear_codec_out_scb;
1468 		ins->center_lfe_mix_scb = rear_mix_scb;
1469 	}
1470 
1471 	/* enable slots depending on CODEC configuration */
1472 	snd_cs46xx_pokeBA0(chip, BA0_ACOSV, valid_slots);
1473 
1474 	/* the magic snooper */
1475 	magic_snoop_scb = cs46xx_dsp_create_magic_snoop_scb (chip,"MagicSnoopSCB_I",OUTPUTSNOOP_SCB_ADDR,
1476 							     OUTPUT_SNOOP_BUFFER,
1477 							     codec_out_scb,
1478 							     clfe_codec_out_scb,
1479 							     SCB_ON_PARENT_NEXT_SCB);
1480 
1481 
1482 	if (!magic_snoop_scb) goto _fail_end;
1483 	ins->ref_snoop_scb = magic_snoop_scb;
1484 
1485 	/* SP IO access */
1486 	if (!cs46xx_dsp_create_spio_write_scb(chip,"SPIOWriteSCB",SPIOWRITE_SCB_ADDR,
1487 					      magic_snoop_scb,
1488 					      SCB_ON_PARENT_NEXT_SCB))
1489 		goto _fail_end;
1490 
1491 	/* SPDIF input sampel rate converter */
1492 	src_task_scb = cs46xx_dsp_create_src_task_scb(chip,"SrcTaskSCB_SPDIFI",
1493 						      ins->spdif_in_sample_rate,
1494 						      SRC_OUTPUT_BUF1,
1495 						      SRC_DELAY_BUF1,SRCTASK_SCB_ADDR,
1496 						      master_mix_scb,
1497 						      SCB_ON_PARENT_SUBLIST_SCB,1);
1498 
1499 	if (!src_task_scb) goto _fail_end;
1500 	cs46xx_src_unlink(chip,src_task_scb);
1501 
1502 	/* NOTE: when we now how to detect the SPDIF input
1503 	   sample rate we will use this SRC to adjust it */
1504 	ins->spdif_in_src = src_task_scb;
1505 
1506 	cs46xx_dsp_async_init(chip,timing_master_scb);
1507 	return 0;
1508 
1509  _fail_end:
1510 	dev_err(chip->card->dev, "dsp_spos: failed to setup SCB's in DSP\n");
1511 	return -EINVAL;
1512 }
1513 
cs46xx_dsp_async_init(struct snd_cs46xx * chip,struct dsp_scb_descriptor * fg_entry)1514 static int cs46xx_dsp_async_init (struct snd_cs46xx *chip,
1515 				  struct dsp_scb_descriptor * fg_entry)
1516 {
1517 	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
1518 	struct dsp_symbol_entry * s16_async_codec_input_task;
1519 	struct dsp_symbol_entry * spdifo_task;
1520 	struct dsp_symbol_entry * spdifi_task;
1521 	struct dsp_scb_descriptor * spdifi_scb_desc, * spdifo_scb_desc, * async_codec_scb_desc;
1522 
1523 	s16_async_codec_input_task = cs46xx_dsp_lookup_symbol(chip, "S16_ASYNCCODECINPUTTASK", SYMBOL_CODE);
1524 	if (s16_async_codec_input_task == NULL) {
1525 		dev_err(chip->card->dev,
1526 			"dsp_spos: symbol S16_ASYNCCODECINPUTTASK not found\n");
1527 		return -EIO;
1528 	}
1529 	spdifo_task = cs46xx_dsp_lookup_symbol(chip, "SPDIFOTASK", SYMBOL_CODE);
1530 	if (spdifo_task == NULL) {
1531 		dev_err(chip->card->dev,
1532 			"dsp_spos: symbol SPDIFOTASK not found\n");
1533 		return -EIO;
1534 	}
1535 
1536 	spdifi_task = cs46xx_dsp_lookup_symbol(chip, "SPDIFITASK", SYMBOL_CODE);
1537 	if (spdifi_task == NULL) {
1538 		dev_err(chip->card->dev,
1539 			"dsp_spos: symbol SPDIFITASK not found\n");
1540 		return -EIO;
1541 	}
1542 
1543 	{
1544 		/* 0xBC0 */
1545 		struct dsp_spdifoscb spdifo_scb = {
1546 			/* 0 */ DSP_SPOS_UUUU,
1547 			{
1548 				/* 1 */ 0xb0,
1549 				/* 2 */ 0,
1550 				/* 3 */ 0,
1551 				/* 4 */ 0,
1552 			},
1553 			/* NOTE: the SPDIF output task read samples in mono
1554 			   format, the AsynchFGTxSCB task writes to buffer
1555 			   in stereo format
1556 			*/
1557 			/* 5 */ RSCONFIG_SAMPLE_16MONO + RSCONFIG_MODULO_256,
1558 			/* 6 */ ( SPDIFO_IP_OUTPUT_BUFFER1 << 0x10 )  |  0xFFFC,
1559 			/* 7 */ 0,0,
1560 			/* 8 */ 0,
1561 			/* 9 */ FG_TASK_HEADER_ADDR, NULL_SCB_ADDR,
1562 			/* A */ spdifo_task->address,
1563 			SPDIFO_SCB_INST + SPDIFOFIFOPointer,
1564 			{
1565 				/* B */ 0x0040, /*DSP_SPOS_UUUU,*/
1566 				/* C */ 0x20ff, /*DSP_SPOS_UUUU,*/
1567 			},
1568 			/* D */ 0x804c,0,							  /* SPDIFOFIFOPointer:SPDIFOStatRegAddr; */
1569 			/* E */ 0x0108,0x0001,					  /* SPDIFOStMoFormat:SPDIFOFIFOBaseAddr; */
1570 			/* F */ DSP_SPOS_UUUU	  			          /* SPDIFOFree; */
1571 		};
1572 
1573 		/* 0xBB0 */
1574 		struct dsp_spdifiscb spdifi_scb = {
1575 			/* 0 */ DSP_SPOS_UULO,DSP_SPOS_UUHI,
1576 			/* 1 */ 0,
1577 			/* 2 */ 0,
1578 			/* 3 */ 1,4000,        /* SPDIFICountLimit SPDIFICount */
1579 			/* 4 */ DSP_SPOS_UUUU, /* SPDIFIStatusData */
1580 			/* 5 */ 0,DSP_SPOS_UUHI, /* StatusData, Free4 */
1581 			/* 6 */ DSP_SPOS_UUUU,  /* Free3 */
1582 			/* 7 */ DSP_SPOS_UU,DSP_SPOS_DC,  /* Free2 BitCount*/
1583 			/* 8 */ DSP_SPOS_UUUU,	/* TempStatus */
1584 			/* 9 */ SPDIFO_SCB_INST, NULL_SCB_ADDR,
1585 			/* A */ spdifi_task->address,
1586 			SPDIFI_SCB_INST + SPDIFIFIFOPointer,
1587 			/* NOTE: The SPDIF input task write the sample in mono
1588 			   format from the HW FIFO, the AsynchFGRxSCB task  reads
1589 			   them in stereo
1590 			*/
1591 			/* B */ RSCONFIG_SAMPLE_16MONO + RSCONFIG_MODULO_128,
1592 			/* C */ (SPDIFI_IP_OUTPUT_BUFFER1 << 0x10) | 0xFFFC,
1593 			/* D */ 0x8048,0,
1594 			/* E */ 0x01f0,0x0001,
1595 			/* F */ DSP_SPOS_UUUU /* SPDIN_STATUS monitor */
1596 		};
1597 
1598 		/* 0xBA0 */
1599 		struct dsp_async_codec_input_scb async_codec_input_scb = {
1600 			/* 0 */ DSP_SPOS_UUUU,
1601 			/* 1 */ 0,
1602 			/* 2 */ 0,
1603 			/* 3 */ 1,4000,
1604 			/* 4 */ 0x0118,0x0001,
1605 			/* 5 */ RSCONFIG_SAMPLE_16MONO + RSCONFIG_MODULO_64,
1606 			/* 6 */ (ASYNC_IP_OUTPUT_BUFFER1 << 0x10) | 0xFFFC,
1607 			/* 7 */ DSP_SPOS_UU,0x3,
1608 			/* 8 */ DSP_SPOS_UUUU,
1609 			/* 9 */ SPDIFI_SCB_INST,NULL_SCB_ADDR,
1610 			/* A */ s16_async_codec_input_task->address,
1611 			HFG_TREE_SCB + AsyncCIOFIFOPointer,
1612 
1613 			/* B */ RSCONFIG_SAMPLE_16STEREO + RSCONFIG_MODULO_64,
1614 			/* C */ (ASYNC_IP_OUTPUT_BUFFER1 << 0x10),  /*(ASYNC_IP_OUTPUT_BUFFER1 << 0x10) | 0xFFFC,*/
1615 
1616 #ifdef UseASER1Input
1617 			/* short AsyncCIFIFOPointer:AsyncCIStatRegAddr;
1618 			   Init. 0000:8042: for ASER1
1619 			   0000:8044: for ASER2 */
1620 			/* D */ 0x8042,0,
1621 
1622 			/* short AsyncCIStMoFormat:AsyncCIFIFOBaseAddr;
1623 			   Init 1 stero:8050 ASER1
1624 			   Init 0  mono:8070 ASER2
1625 			   Init 1 Stereo : 0100 ASER1 (Set by script) */
1626 			/* E */ 0x0100,0x0001,
1627 
1628 #endif
1629 
1630 #ifdef UseASER2Input
1631 			/* short AsyncCIFIFOPointer:AsyncCIStatRegAddr;
1632 			   Init. 0000:8042: for ASER1
1633 			   0000:8044: for ASER2 */
1634 			/* D */ 0x8044,0,
1635 
1636 			/* short AsyncCIStMoFormat:AsyncCIFIFOBaseAddr;
1637 			   Init 1 stero:8050 ASER1
1638 			   Init 0  mono:8070 ASER2
1639 			   Init 1 Stereo : 0100 ASER1 (Set by script) */
1640 			/* E */ 0x0110,0x0001,
1641 
1642 #endif
1643 
1644 			/* short AsyncCIOutputBufModulo:AsyncCIFree;
1645 			   AsyncCIOutputBufModulo: The modulo size for
1646 			   the output buffer of this task */
1647 			/* F */ 0, /* DSP_SPOS_UUUU */
1648 		};
1649 
1650 		spdifo_scb_desc = cs46xx_dsp_create_scb(chip,"SPDIFOSCB",(u32 *)&spdifo_scb,SPDIFO_SCB_INST);
1651 
1652 		if (snd_BUG_ON(!spdifo_scb_desc))
1653 			return -EIO;
1654 		spdifi_scb_desc = cs46xx_dsp_create_scb(chip,"SPDIFISCB",(u32 *)&spdifi_scb,SPDIFI_SCB_INST);
1655 		if (snd_BUG_ON(!spdifi_scb_desc))
1656 			return -EIO;
1657 		async_codec_scb_desc = cs46xx_dsp_create_scb(chip,"AsynCodecInputSCB",(u32 *)&async_codec_input_scb, HFG_TREE_SCB);
1658 		if (snd_BUG_ON(!async_codec_scb_desc))
1659 			return -EIO;
1660 
1661 		async_codec_scb_desc->parent_scb_ptr = NULL;
1662 		async_codec_scb_desc->next_scb_ptr = spdifi_scb_desc;
1663 		async_codec_scb_desc->sub_list_ptr = ins->the_null_scb;
1664 		async_codec_scb_desc->task_entry = s16_async_codec_input_task;
1665 
1666 		spdifi_scb_desc->parent_scb_ptr = async_codec_scb_desc;
1667 		spdifi_scb_desc->next_scb_ptr = spdifo_scb_desc;
1668 		spdifi_scb_desc->sub_list_ptr = ins->the_null_scb;
1669 		spdifi_scb_desc->task_entry = spdifi_task;
1670 
1671 		spdifo_scb_desc->parent_scb_ptr = spdifi_scb_desc;
1672 		spdifo_scb_desc->next_scb_ptr = fg_entry;
1673 		spdifo_scb_desc->sub_list_ptr = ins->the_null_scb;
1674 		spdifo_scb_desc->task_entry = spdifo_task;
1675 
1676 		/* this one is faked, as the parnet of SPDIFO task
1677 		   is the FG task tree */
1678 		fg_entry->parent_scb_ptr = spdifo_scb_desc;
1679 
1680 		/* for proc fs */
1681 		cs46xx_dsp_proc_register_scb_desc (chip,spdifo_scb_desc);
1682 		cs46xx_dsp_proc_register_scb_desc (chip,spdifi_scb_desc);
1683 		cs46xx_dsp_proc_register_scb_desc (chip,async_codec_scb_desc);
1684 
1685 		/* Async MASTER ENABLE, affects both SPDIF input and output */
1686 		snd_cs46xx_pokeBA0(chip, BA0_ASER_MASTER, 0x1 );
1687 	}
1688 
1689 	return 0;
1690 }
1691 
cs46xx_dsp_disable_spdif_hw(struct snd_cs46xx * chip)1692 static void cs46xx_dsp_disable_spdif_hw (struct snd_cs46xx *chip)
1693 {
1694 	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
1695 
1696 	/* set SPDIF output FIFO slot */
1697 	snd_cs46xx_pokeBA0(chip, BA0_ASER_FADDR, 0);
1698 
1699 	/* SPDIF output MASTER ENABLE */
1700 	cs46xx_poke_via_dsp (chip,SP_SPDOUT_CONTROL, 0);
1701 
1702 	/* right and left validate bit */
1703 	/*cs46xx_poke_via_dsp (chip,SP_SPDOUT_CSUV, ins->spdif_csuv_default);*/
1704 	cs46xx_poke_via_dsp (chip,SP_SPDOUT_CSUV, 0x0);
1705 
1706 	/* clear fifo pointer */
1707 	cs46xx_poke_via_dsp (chip,SP_SPDIN_FIFOPTR, 0x0);
1708 
1709 	/* monitor state */
1710 	ins->spdif_status_out &= ~DSP_SPDIF_STATUS_HW_ENABLED;
1711 }
1712 
cs46xx_dsp_enable_spdif_hw(struct snd_cs46xx * chip)1713 int cs46xx_dsp_enable_spdif_hw (struct snd_cs46xx *chip)
1714 {
1715 	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
1716 
1717 	/* if hw-ctrl already enabled, turn off to reset logic ... */
1718 	cs46xx_dsp_disable_spdif_hw (chip);
1719 	udelay(50);
1720 
1721 	/* set SPDIF output FIFO slot */
1722 	snd_cs46xx_pokeBA0(chip, BA0_ASER_FADDR, ( 0x8000 | ((SP_SPDOUT_FIFO >> 4) << 4) ));
1723 
1724 	/* SPDIF output MASTER ENABLE */
1725 	cs46xx_poke_via_dsp (chip,SP_SPDOUT_CONTROL, 0x80000000);
1726 
1727 	/* right and left validate bit */
1728 	cs46xx_poke_via_dsp (chip,SP_SPDOUT_CSUV, ins->spdif_csuv_default);
1729 
1730 	/* monitor state */
1731 	ins->spdif_status_out |= DSP_SPDIF_STATUS_HW_ENABLED;
1732 
1733 	return 0;
1734 }
1735 
cs46xx_dsp_enable_spdif_in(struct snd_cs46xx * chip)1736 int cs46xx_dsp_enable_spdif_in (struct snd_cs46xx *chip)
1737 {
1738 	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
1739 
1740 	/* turn on amplifier */
1741 	chip->active_ctrl(chip, 1);
1742 	chip->amplifier_ctrl(chip, 1);
1743 
1744 	if (snd_BUG_ON(ins->asynch_rx_scb))
1745 		return -EINVAL;
1746 	if (snd_BUG_ON(!ins->spdif_in_src))
1747 		return -EINVAL;
1748 
1749 	mutex_lock(&chip->spos_mutex);
1750 
1751 	if ( ! (ins->spdif_status_out & DSP_SPDIF_STATUS_INPUT_CTRL_ENABLED) ) {
1752 		/* time countdown enable */
1753 		cs46xx_poke_via_dsp (chip,SP_ASER_COUNTDOWN, 0x80000005);
1754 		/* NOTE: 80000005 value is just magic. With all values
1755 		   that I've tested this one seem to give the best result.
1756 		   Got no explication why. (Benny) */
1757 
1758 		/* SPDIF input MASTER ENABLE */
1759 		cs46xx_poke_via_dsp (chip,SP_SPDIN_CONTROL, 0x800003ff);
1760 
1761 		ins->spdif_status_out |= DSP_SPDIF_STATUS_INPUT_CTRL_ENABLED;
1762 	}
1763 
1764 	/* create and start the asynchronous receiver SCB */
1765 	ins->asynch_rx_scb = cs46xx_dsp_create_asynch_fg_rx_scb(chip,"AsynchFGRxSCB",
1766 								ASYNCRX_SCB_ADDR,
1767 								SPDIFI_SCB_INST,
1768 								SPDIFI_IP_OUTPUT_BUFFER1,
1769 								ins->spdif_in_src,
1770 								SCB_ON_PARENT_SUBLIST_SCB);
1771 
1772 	spin_lock_irq(&chip->reg_lock);
1773 
1774 	/* reset SPDIF input sample buffer pointer */
1775 	/*snd_cs46xx_poke (chip, (SPDIFI_SCB_INST + 0x0c) << 2,
1776 	  (SPDIFI_IP_OUTPUT_BUFFER1 << 0x10) | 0xFFFC);*/
1777 
1778 	/* reset FIFO ptr */
1779 	/*cs46xx_poke_via_dsp (chip,SP_SPDIN_FIFOPTR, 0x0);*/
1780 	cs46xx_src_link(chip,ins->spdif_in_src);
1781 
1782 	/* unmute SRC volume */
1783 	cs46xx_dsp_scb_set_volume (chip,ins->spdif_in_src,0x7fff,0x7fff);
1784 
1785 	spin_unlock_irq(&chip->reg_lock);
1786 
1787 	/* set SPDIF input sample rate and unmute
1788 	   NOTE: only 48khz support for SPDIF input this time */
1789 	/* cs46xx_dsp_set_src_sample_rate(chip,ins->spdif_in_src,48000); */
1790 
1791 	/* monitor state */
1792 	ins->spdif_status_in = 1;
1793 	mutex_unlock(&chip->spos_mutex);
1794 
1795 	return 0;
1796 }
1797 
cs46xx_dsp_disable_spdif_in(struct snd_cs46xx * chip)1798 int cs46xx_dsp_disable_spdif_in (struct snd_cs46xx *chip)
1799 {
1800 	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
1801 
1802 	if (snd_BUG_ON(!ins->asynch_rx_scb))
1803 		return -EINVAL;
1804 	if (snd_BUG_ON(!ins->spdif_in_src))
1805 		return -EINVAL;
1806 
1807 	mutex_lock(&chip->spos_mutex);
1808 
1809 	/* Remove the asynchronous receiver SCB */
1810 	cs46xx_dsp_remove_scb (chip,ins->asynch_rx_scb);
1811 	ins->asynch_rx_scb = NULL;
1812 
1813 	cs46xx_src_unlink(chip,ins->spdif_in_src);
1814 
1815 	/* monitor state */
1816 	ins->spdif_status_in = 0;
1817 	mutex_unlock(&chip->spos_mutex);
1818 
1819 	/* restore amplifier */
1820 	chip->active_ctrl(chip, -1);
1821 	chip->amplifier_ctrl(chip, -1);
1822 
1823 	return 0;
1824 }
1825 
cs46xx_dsp_enable_pcm_capture(struct snd_cs46xx * chip)1826 int cs46xx_dsp_enable_pcm_capture (struct snd_cs46xx *chip)
1827 {
1828 	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
1829 
1830 	if (snd_BUG_ON(ins->pcm_input))
1831 		return -EINVAL;
1832 	if (snd_BUG_ON(!ins->ref_snoop_scb))
1833 		return -EINVAL;
1834 
1835 	mutex_lock(&chip->spos_mutex);
1836 	ins->pcm_input = cs46xx_add_record_source(chip,ins->ref_snoop_scb,PCMSERIALIN_PCM_SCB_ADDR,
1837                                                   "PCMSerialInput_Wave");
1838 	mutex_unlock(&chip->spos_mutex);
1839 
1840 	return 0;
1841 }
1842 
cs46xx_dsp_disable_pcm_capture(struct snd_cs46xx * chip)1843 int cs46xx_dsp_disable_pcm_capture (struct snd_cs46xx *chip)
1844 {
1845 	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
1846 
1847 	if (snd_BUG_ON(!ins->pcm_input))
1848 		return -EINVAL;
1849 
1850 	mutex_lock(&chip->spos_mutex);
1851 	cs46xx_dsp_remove_scb (chip,ins->pcm_input);
1852 	ins->pcm_input = NULL;
1853 	mutex_unlock(&chip->spos_mutex);
1854 
1855 	return 0;
1856 }
1857 
cs46xx_dsp_enable_adc_capture(struct snd_cs46xx * chip)1858 int cs46xx_dsp_enable_adc_capture (struct snd_cs46xx *chip)
1859 {
1860 	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
1861 
1862 	if (snd_BUG_ON(ins->adc_input))
1863 		return -EINVAL;
1864 	if (snd_BUG_ON(!ins->codec_in_scb))
1865 		return -EINVAL;
1866 
1867 	mutex_lock(&chip->spos_mutex);
1868 	ins->adc_input = cs46xx_add_record_source(chip,ins->codec_in_scb,PCMSERIALIN_SCB_ADDR,
1869 						  "PCMSerialInput_ADC");
1870 	mutex_unlock(&chip->spos_mutex);
1871 
1872 	return 0;
1873 }
1874 
cs46xx_dsp_disable_adc_capture(struct snd_cs46xx * chip)1875 int cs46xx_dsp_disable_adc_capture (struct snd_cs46xx *chip)
1876 {
1877 	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
1878 
1879 	if (snd_BUG_ON(!ins->adc_input))
1880 		return -EINVAL;
1881 
1882 	mutex_lock(&chip->spos_mutex);
1883 	cs46xx_dsp_remove_scb (chip,ins->adc_input);
1884 	ins->adc_input = NULL;
1885 	mutex_unlock(&chip->spos_mutex);
1886 
1887 	return 0;
1888 }
1889 
cs46xx_poke_via_dsp(struct snd_cs46xx * chip,u32 address,u32 data)1890 int cs46xx_poke_via_dsp (struct snd_cs46xx *chip, u32 address, u32 data)
1891 {
1892 	u32 temp;
1893 	int  i;
1894 
1895 	/* santiy check the parameters.  (These numbers are not 100% correct.  They are
1896 	   a rough guess from looking at the controller spec.) */
1897 	if (address < 0x8000 || address >= 0x9000)
1898 		return -EINVAL;
1899 
1900 	/* initialize the SP_IO_WRITE SCB with the data. */
1901 	temp = ( address << 16 ) | ( address & 0x0000FFFF);   /* offset 0 <-- address2 : address1 */
1902 
1903 	snd_cs46xx_poke(chip,( SPIOWRITE_SCB_ADDR      << 2), temp);
1904 	snd_cs46xx_poke(chip,((SPIOWRITE_SCB_ADDR + 1) << 2), data); /* offset 1 <-- data1 */
1905 	snd_cs46xx_poke(chip,((SPIOWRITE_SCB_ADDR + 2) << 2), data); /* offset 1 <-- data2 */
1906 
1907 	/* Poke this location to tell the task to start */
1908 	snd_cs46xx_poke(chip,((SPIOWRITE_SCB_ADDR + 6) << 2), SPIOWRITE_SCB_ADDR << 0x10);
1909 
1910 	/* Verify that the task ran */
1911 	for (i=0; i<25; i++) {
1912 		udelay(125);
1913 
1914 		temp =  snd_cs46xx_peek(chip,((SPIOWRITE_SCB_ADDR + 6) << 2));
1915 		if (temp == 0x00000000)
1916 			break;
1917 	}
1918 
1919 	if (i == 25) {
1920 		dev_err(chip->card->dev,
1921 			"dsp_spos: SPIOWriteTask not responding\n");
1922 		return -EBUSY;
1923 	}
1924 
1925 	return 0;
1926 }
1927 
cs46xx_dsp_set_dac_volume(struct snd_cs46xx * chip,u16 left,u16 right)1928 int cs46xx_dsp_set_dac_volume (struct snd_cs46xx * chip, u16 left, u16 right)
1929 {
1930 	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
1931 	struct dsp_scb_descriptor * scb;
1932 
1933 	mutex_lock(&chip->spos_mutex);
1934 
1935 	/* main output */
1936 	scb = ins->master_mix_scb->sub_list_ptr;
1937 	while (scb != ins->the_null_scb) {
1938 		cs46xx_dsp_scb_set_volume (chip,scb,left,right);
1939 		scb = scb->next_scb_ptr;
1940 	}
1941 
1942 	/* rear output */
1943 	scb = ins->rear_mix_scb->sub_list_ptr;
1944 	while (scb != ins->the_null_scb) {
1945 		cs46xx_dsp_scb_set_volume (chip,scb,left,right);
1946 		scb = scb->next_scb_ptr;
1947 	}
1948 
1949 	ins->dac_volume_left = left;
1950 	ins->dac_volume_right = right;
1951 
1952 	mutex_unlock(&chip->spos_mutex);
1953 
1954 	return 0;
1955 }
1956 
cs46xx_dsp_set_iec958_volume(struct snd_cs46xx * chip,u16 left,u16 right)1957 int cs46xx_dsp_set_iec958_volume (struct snd_cs46xx * chip, u16 left, u16 right)
1958 {
1959 	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
1960 
1961 	mutex_lock(&chip->spos_mutex);
1962 
1963 	if (ins->asynch_rx_scb != NULL)
1964 		cs46xx_dsp_scb_set_volume (chip,ins->asynch_rx_scb,
1965 					   left,right);
1966 
1967 	ins->spdif_input_volume_left = left;
1968 	ins->spdif_input_volume_right = right;
1969 
1970 	mutex_unlock(&chip->spos_mutex);
1971 
1972 	return 0;
1973 }
1974 
1975 #ifdef CONFIG_PM_SLEEP
cs46xx_dsp_resume(struct snd_cs46xx * chip)1976 int cs46xx_dsp_resume(struct snd_cs46xx * chip)
1977 {
1978 	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
1979 	int i, err;
1980 
1981 	/* clear parameter, sample and code areas */
1982 	snd_cs46xx_clear_BA1(chip, DSP_PARAMETER_BYTE_OFFSET,
1983 			     DSP_PARAMETER_BYTE_SIZE);
1984 	snd_cs46xx_clear_BA1(chip, DSP_SAMPLE_BYTE_OFFSET,
1985 			     DSP_SAMPLE_BYTE_SIZE);
1986 	snd_cs46xx_clear_BA1(chip, DSP_CODE_BYTE_OFFSET, DSP_CODE_BYTE_SIZE);
1987 
1988 	for (i = 0; i < ins->nmodules; i++) {
1989 		struct dsp_module_desc *module = &ins->modules[i];
1990 		struct dsp_segment_desc *seg;
1991 		u32 doffset, dsize;
1992 
1993 		seg = get_segment_desc(module, SEGTYPE_SP_PARAMETER);
1994 		err = dsp_load_parameter(chip, seg);
1995 		if (err < 0)
1996 			return err;
1997 
1998 		seg = get_segment_desc(module, SEGTYPE_SP_SAMPLE);
1999 		err = dsp_load_sample(chip, seg);
2000 		if (err < 0)
2001 			return err;
2002 
2003 		seg = get_segment_desc(module, SEGTYPE_SP_PROGRAM);
2004 		if (!seg)
2005 			continue;
2006 
2007 		doffset = seg->offset * 4 + module->load_address * 4
2008 			+ DSP_CODE_BYTE_OFFSET;
2009 		dsize   = seg->size * 4;
2010 		err = snd_cs46xx_download(chip,
2011 					  ins->code.data + module->load_address,
2012 					  doffset, dsize);
2013 		if (err < 0)
2014 			return err;
2015 	}
2016 
2017 	for (i = 0; i < ins->ntask; i++) {
2018 		struct dsp_task_descriptor *t = &ins->tasks[i];
2019 		_dsp_create_task_tree(chip, t->data, t->address, t->size);
2020 	}
2021 
2022 	for (i = 0; i < ins->nscb; i++) {
2023 		struct dsp_scb_descriptor *s = &ins->scbs[i];
2024 		if (s->deleted)
2025 			continue;
2026 		_dsp_create_scb(chip, s->data, s->address);
2027 	}
2028 	for (i = 0; i < ins->nscb; i++) {
2029 		struct dsp_scb_descriptor *s = &ins->scbs[i];
2030 		if (s->deleted)
2031 			continue;
2032 		if (s->updated)
2033 			cs46xx_dsp_spos_update_scb(chip, s);
2034 		if (s->volume_set)
2035 			cs46xx_dsp_scb_set_volume(chip, s,
2036 						  s->volume[0], s->volume[1]);
2037 	}
2038 	if (ins->spdif_status_out & DSP_SPDIF_STATUS_HW_ENABLED) {
2039 		cs46xx_dsp_enable_spdif_hw(chip);
2040 		snd_cs46xx_poke(chip, (ins->ref_snoop_scb->address + 2) << 2,
2041 				(OUTPUT_SNOOP_BUFFER + 0x10) << 0x10);
2042 		if (ins->spdif_status_out & DSP_SPDIF_STATUS_PLAYBACK_OPEN)
2043 			cs46xx_poke_via_dsp(chip, SP_SPDOUT_CSUV,
2044 					    ins->spdif_csuv_stream);
2045 	}
2046 	if (chip->dsp_spos_instance->spdif_status_in) {
2047 		cs46xx_poke_via_dsp(chip, SP_ASER_COUNTDOWN, 0x80000005);
2048 		cs46xx_poke_via_dsp(chip, SP_SPDIN_CONTROL, 0x800003ff);
2049 	}
2050 	return 0;
2051 }
2052 #endif
2053