cs4281.c revision 1.33
1/*	$NetBSD: cs4281.c,v 1.33 2006/11/16 01:33:08 christos Exp $	*/
2
3/*
4 * Copyright (c) 2000 Tatoku Ogaito.  All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 *    must display the following acknowledgement:
16 *      This product includes software developed by Tatoku Ogaito
17 *	for the NetBSD Project.
18 * 4. The name of the author may not be used to endorse or promote products
19 *    derived from this software without specific prior written permission
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33/*
34 * Cirrus Logic CS4281 driver.
35 * Data sheets can be found
36 * http://www.cirrus.com/ftp/pub/4281.pdf
37 * ftp://ftp.alsa-project.org/pub/manuals/cirrus/cs4281tm.pdf
38 *
39 * TODO:
40 *   1: midi and FM support
41 *   2: ...
42 *
43 */
44
45#include <sys/cdefs.h>
46__KERNEL_RCSID(0, "$NetBSD: cs4281.c,v 1.33 2006/11/16 01:33:08 christos Exp $");
47
48#include <sys/param.h>
49#include <sys/systm.h>
50#include <sys/kernel.h>
51#include <sys/malloc.h>
52#include <sys/fcntl.h>
53#include <sys/device.h>
54#include <sys/systm.h>
55
56#include <dev/pci/pcidevs.h>
57#include <dev/pci/pcivar.h>
58#include <dev/pci/cs4281reg.h>
59#include <dev/pci/cs428xreg.h>
60
61#include <sys/audioio.h>
62#include <dev/audio_if.h>
63#include <dev/midi_if.h>
64#include <dev/mulaw.h>
65#include <dev/auconv.h>
66
67#include <dev/ic/ac97reg.h>
68#include <dev/ic/ac97var.h>
69
70#include <dev/pci/cs428x.h>
71
72#include <machine/bus.h>
73
74#if defined(ENABLE_SECONDARY_CODEC)
75#define MAX_CHANNELS  (4)
76#define MAX_FIFO_SIZE 32 /* 128/4channels */
77#else
78#define MAX_CHANNELS  (2)
79#define MAX_FIFO_SIZE 64 /* 128/2channels */
80#endif
81
82/* IF functions for audio driver */
83static int	cs4281_match(struct device *, struct cfdata *, void *);
84static void	cs4281_attach(struct device *, struct device *, void *);
85static int	cs4281_intr(void *);
86static int	cs4281_query_encoding(void *, struct audio_encoding *);
87static int	cs4281_set_params(void *, int, int, audio_params_t *,
88				  audio_params_t *, stream_filter_list_t *,
89				  stream_filter_list_t *);
90static int	cs4281_halt_output(void *);
91static int	cs4281_halt_input(void *);
92static int	cs4281_getdev(void *, struct audio_device *);
93static int	cs4281_trigger_output(void *, void *, void *, int,
94				      void (*)(void *), void *,
95				      const audio_params_t *);
96static int	cs4281_trigger_input(void *, void *, void *, int,
97				     void (*)(void *), void *,
98				     const audio_params_t *);
99
100static int     cs4281_reset_codec(void *);
101
102/* Internal functions */
103static uint8_t cs4281_sr2regval(int);
104static void	 cs4281_set_dac_rate(struct cs428x_softc *, int);
105static void	 cs4281_set_adc_rate(struct cs428x_softc *, int);
106static int      cs4281_init(struct cs428x_softc *, int);
107
108/* Power Management */
109static void cs4281_power(int, void *);
110
111static const struct audio_hw_if cs4281_hw_if = {
112	NULL,			/* open */
113	NULL,			/* close */
114	NULL,
115	cs4281_query_encoding,
116	cs4281_set_params,
117	cs428x_round_blocksize,
118	NULL,
119	NULL,
120	NULL,
121	NULL,
122	NULL,
123	cs4281_halt_output,
124	cs4281_halt_input,
125	NULL,
126	cs4281_getdev,
127	NULL,
128	cs428x_mixer_set_port,
129	cs428x_mixer_get_port,
130	cs428x_query_devinfo,
131	cs428x_malloc,
132	cs428x_free,
133	cs428x_round_buffersize,
134	cs428x_mappage,
135	cs428x_get_props,
136	cs4281_trigger_output,
137	cs4281_trigger_input,
138	NULL,
139	NULL,
140};
141
142#if NMIDI > 0 && 0
143/* Midi Interface */
144static void	cs4281_midi_close(void*);
145static void	cs4281_midi_getinfo(void *, struct midi_info *);
146static int	cs4281_midi_open(void *, int, void (*)(void *, int),
147			 void (*)(void *), void *);
148static int	cs4281_midi_output(void *, int);
149
150static const struct midi_hw_if cs4281_midi_hw_if = {
151	cs4281_midi_open,
152	cs4281_midi_close,
153	cs4281_midi_output,
154	cs4281_midi_getinfo,
155	0,
156};
157#endif
158
159CFATTACH_DECL(clct, sizeof(struct cs428x_softc),
160    cs4281_match, cs4281_attach, NULL, NULL);
161
162static struct audio_device cs4281_device = {
163	"CS4281",
164	"",
165	"cs4281"
166};
167
168
169static int
170cs4281_match(struct device *parent, struct cfdata *match,
171    void *aux)
172{
173	struct pci_attach_args *pa;
174
175	pa = (struct pci_attach_args *)aux;
176	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_CIRRUS)
177		return 0;
178	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_CIRRUS_CS4281)
179		return 1;
180	return 0;
181}
182
183static void
184cs4281_attach(struct device *parent, struct device *self, void *aux)
185{
186	struct cs428x_softc *sc;
187	struct pci_attach_args *pa;
188	pci_chipset_tag_t pc;
189	char const *intrstr;
190	pci_intr_handle_t ih;
191	pcireg_t reg;
192	char devinfo[256];
193	int error;
194
195	sc = (struct cs428x_softc *)self;
196	pa = (struct pci_attach_args *)aux;
197	pc = pa->pa_pc;
198	aprint_naive(": Audio controller\n");
199
200	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
201	aprint_normal(": %s (rev. 0x%02x)\n", devinfo,
202	    PCI_REVISION(pa->pa_class));
203
204	/* Map I/O register */
205	if (pci_mapreg_map(pa, PCI_BA0,
206	    PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0,
207	    &sc->ba0t, &sc->ba0h, NULL, NULL)) {
208		aprint_error("%s: can't map BA0 space\n", sc->sc_dev.dv_xname);
209		return;
210	}
211	if (pci_mapreg_map(pa, PCI_BA1,
212	    PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0,
213	    &sc->ba1t, &sc->ba1h, NULL, NULL)) {
214		aprint_error("%s: can't map BA1 space\n", sc->sc_dev.dv_xname);
215		return;
216	}
217
218	sc->sc_dmatag = pa->pa_dmat;
219
220	/* power up chip */
221	if ((error = pci_activate(pa->pa_pc, pa->pa_tag, sc,
222	    pci_activate_null)) && error != EOPNOTSUPP) {
223		aprint_error("%s: cannot activate %d\n", sc->sc_dev.dv_xname,
224		    error);
225		return;
226	}
227
228	/* Enable the device (set bus master flag) */
229	reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
230	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
231	    reg | PCI_COMMAND_MASTER_ENABLE);
232
233#if 0
234	/* LATENCY_TIMER setting */
235	temp1 = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_BHLC_REG);
236	if (PCI_LATTIMER(temp1) < 32) {
237		temp1 &= 0xffff00ff;
238		temp1 |= 0x00002000;
239		pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_BHLC_REG, temp1);
240	}
241#endif
242
243	/* Map and establish the interrupt. */
244	if (pci_intr_map(pa, &ih)) {
245		aprint_error("%s: couldn't map interrupt\n",
246		    sc->sc_dev.dv_xname);
247		return;
248	}
249	intrstr = pci_intr_string(pc, ih);
250
251	sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO, cs4281_intr, sc);
252	if (sc->sc_ih == NULL) {
253		aprint_error("%s: couldn't establish interrupt",
254		    sc->sc_dev.dv_xname);
255		if (intrstr != NULL)
256			aprint_normal(" at %s", intrstr);
257		aprint_normal("\n");
258		return;
259	}
260	aprint_normal("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
261
262	/*
263	 * Sound System start-up
264	 */
265	if (cs4281_init(sc, 1) != 0)
266		return;
267
268	sc->type = TYPE_CS4281;
269	sc->halt_input  = cs4281_halt_input;
270	sc->halt_output = cs4281_halt_output;
271
272	sc->dma_size     = CS4281_BUFFER_SIZE / MAX_CHANNELS;
273	sc->dma_align    = 0x10;
274	sc->hw_blocksize = sc->dma_size / 2;
275
276	/* AC 97 attachment */
277	sc->host_if.arg = sc;
278	sc->host_if.attach = cs428x_attach_codec;
279	sc->host_if.read   = cs428x_read_codec;
280	sc->host_if.write  = cs428x_write_codec;
281	sc->host_if.reset  = cs4281_reset_codec;
282	if (ac97_attach(&sc->host_if, self) != 0) {
283		aprint_error("%s: ac97_attach failed\n", sc->sc_dev.dv_xname);
284		return;
285	}
286	audio_attach_mi(&cs4281_hw_if, sc, &sc->sc_dev);
287
288#if NMIDI > 0 && 0
289	midi_attach_mi(&cs4281_midi_hw_if, sc, &sc->sc_dev);
290#endif
291
292	sc->sc_suspend = PWR_RESUME;
293	sc->sc_powerhook = powerhook_establish(sc->sc_dev.dv_xname,
294	    cs4281_power, sc);
295}
296
297static int
298cs4281_intr(void *p)
299{
300	struct cs428x_softc *sc;
301	uint32_t intr, hdsr0, hdsr1;
302	char *empty_dma;
303	int handled;
304
305	sc = p;
306	handled = 0;
307	hdsr0 = 0;
308	hdsr1 = 0;
309
310	/* grab interrupt register */
311	intr = BA0READ4(sc, CS4281_HISR);
312
313	DPRINTF(("cs4281_intr:"));
314	/* not for me */
315	if ((intr & HISR_INTENA) == 0) {
316		/* clear the interrupt register */
317		BA0WRITE4(sc, CS4281_HICR, HICR_CHGM | HICR_IEV);
318		return 0;
319	}
320
321	if (intr & HISR_DMA0)
322		hdsr0 = BA0READ4(sc, CS4281_HDSR0); /* clear intr condition */
323	if (intr & HISR_DMA1)
324		hdsr1 = BA0READ4(sc, CS4281_HDSR1); /* clear intr condition */
325	/* clear the interrupt register */
326	BA0WRITE4(sc, CS4281_HICR, HICR_CHGM | HICR_IEV);
327
328	DPRINTF(("intr = 0x%08x, hdsr0 = 0x%08x hdsr1 = 0x%08x\n",
329		 intr, hdsr0, hdsr1));
330
331	/* Playback Interrupt */
332	if (intr & HISR_DMA0) {
333		handled = 1;
334		if (sc->sc_prun) {
335			DPRINTF((" PB DMA 0x%x(%d)",
336				(int)BA0READ4(sc, CS4281_DCA0),
337				(int)BA0READ4(sc, CS4281_DCC0)));
338			if ((sc->sc_pi%sc->sc_pcount) == 0)
339				sc->sc_pintr(sc->sc_parg);
340			/* copy buffer */
341			++sc->sc_pi;
342			empty_dma = sc->sc_pdma->addr;
343			if (sc->sc_pi&1)
344				empty_dma += sc->hw_blocksize;
345			memcpy(empty_dma, sc->sc_pn, sc->hw_blocksize);
346			sc->sc_pn += sc->hw_blocksize;
347			if (sc->sc_pn >= sc->sc_pe)
348				sc->sc_pn = sc->sc_ps;
349		} else {
350			printf("%s: unexpected play intr\n",
351			       sc->sc_dev.dv_xname);
352		}
353	}
354	if (intr & HISR_DMA1) {
355		handled = 1;
356		if (sc->sc_rrun) {
357			/* copy from DMA */
358			DPRINTF((" CP DMA 0x%x(%d)", (int)BA0READ4(sc, CS4281_DCA1),
359				(int)BA0READ4(sc, CS4281_DCC1)));
360			++sc->sc_ri;
361			empty_dma = sc->sc_rdma->addr;
362			if ((sc->sc_ri & 1) == 0)
363				empty_dma += sc->hw_blocksize;
364			memcpy(sc->sc_rn, empty_dma, sc->hw_blocksize);
365			sc->sc_rn += sc->hw_blocksize;
366			if (sc->sc_rn >= sc->sc_re)
367				sc->sc_rn = sc->sc_rs;
368			if ((sc->sc_ri % sc->sc_rcount) == 0)
369				sc->sc_rintr(sc->sc_rarg);
370		} else {
371			printf("%s: unexpected record intr\n",
372			       sc->sc_dev.dv_xname);
373		}
374	}
375	DPRINTF(("\n"));
376
377	return handled;
378}
379
380static int
381cs4281_query_encoding(void *addr, struct audio_encoding *fp)
382{
383
384	switch (fp->index) {
385	case 0:
386		strcpy(fp->name, AudioEulinear);
387		fp->encoding = AUDIO_ENCODING_ULINEAR;
388		fp->precision = 8;
389		fp->flags = 0;
390		break;
391	case 1:
392		strcpy(fp->name, AudioEmulaw);
393		fp->encoding = AUDIO_ENCODING_ULAW;
394		fp->precision = 8;
395		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
396		break;
397	case 2:
398		strcpy(fp->name, AudioEalaw);
399		fp->encoding = AUDIO_ENCODING_ALAW;
400		fp->precision = 8;
401		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
402		break;
403	case 3:
404		strcpy(fp->name, AudioEslinear);
405		fp->encoding = AUDIO_ENCODING_SLINEAR;
406		fp->precision = 8;
407		fp->flags = 0;
408		break;
409	case 4:
410		strcpy(fp->name, AudioEslinear_le);
411		fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
412		fp->precision = 16;
413		fp->flags = 0;
414		break;
415	case 5:
416		strcpy(fp->name, AudioEulinear_le);
417		fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
418		fp->precision = 16;
419		fp->flags = 0;
420		break;
421	case 6:
422		strcpy(fp->name, AudioEslinear_be);
423		fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
424		fp->precision = 16;
425		fp->flags = 0;
426		break;
427	case 7:
428		strcpy(fp->name, AudioEulinear_be);
429		fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
430		fp->precision = 16;
431		fp->flags = 0;
432		break;
433	default:
434		return EINVAL;
435	}
436	return 0;
437}
438
439static int
440cs4281_set_params(void *addr, int setmode, int usemode,
441    audio_params_t *play, audio_params_t *rec, stream_filter_list_t *pfil,
442    stream_filter_list_t *rfil)
443{
444	audio_params_t hw;
445	struct cs428x_softc *sc;
446	audio_params_t *p;
447	stream_filter_list_t *fil;
448	int mode;
449
450	sc = addr;
451	for (mode = AUMODE_RECORD; mode != -1;
452	    mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
453		if ((setmode & mode) == 0)
454			continue;
455
456		p = mode == AUMODE_PLAY ? play : rec;
457
458		if (p == play) {
459			DPRINTFN(5,
460			    ("play: sample=%u precision=%u channels=%u\n",
461			    p->sample_rate, p->precision, p->channels));
462			if (p->sample_rate < 6023 || p->sample_rate > 48000 ||
463			    (p->precision != 8 && p->precision != 16) ||
464			    (p->channels != 1  && p->channels != 2)) {
465				return EINVAL;
466			}
467		} else {
468			DPRINTFN(5,
469			    ("rec: sample=%u precision=%u channels=%u\n",
470			    p->sample_rate, p->precision, p->channels));
471			if (p->sample_rate < 6023 || p->sample_rate > 48000 ||
472			    (p->precision != 8 && p->precision != 16) ||
473			    (p->channels != 1 && p->channels != 2)) {
474				return EINVAL;
475			}
476		}
477		hw = *p;
478		fil = mode == AUMODE_PLAY ? pfil : rfil;
479
480		switch (p->encoding) {
481		case AUDIO_ENCODING_SLINEAR_BE:
482			break;
483		case AUDIO_ENCODING_SLINEAR_LE:
484			break;
485		case AUDIO_ENCODING_ULINEAR_BE:
486			break;
487		case AUDIO_ENCODING_ULINEAR_LE:
488			break;
489		case AUDIO_ENCODING_ULAW:
490			hw.encoding = AUDIO_ENCODING_SLINEAR_LE;
491			fil->append(fil, mode == AUMODE_PLAY ? mulaw_to_linear8
492				    :  linear8_to_mulaw, &hw);
493			break;
494		case AUDIO_ENCODING_ALAW:
495			hw.encoding = AUDIO_ENCODING_SLINEAR_LE;
496			fil->append(fil, mode == AUMODE_PLAY ? alaw_to_linear8
497				    : linear8_to_alaw, &hw);
498			break;
499		default:
500			return EINVAL;
501		}
502	}
503
504	/* set sample rate */
505	cs4281_set_dac_rate(sc, play->sample_rate);
506	cs4281_set_adc_rate(sc, rec->sample_rate);
507	return 0;
508}
509
510static int
511cs4281_halt_output(void *addr)
512{
513	struct cs428x_softc *sc;
514
515	sc = addr;
516	BA0WRITE4(sc, CS4281_DCR0, BA0READ4(sc, CS4281_DCR0) | DCRn_MSK);
517	sc->sc_prun = 0;
518	return 0;
519}
520
521static int
522cs4281_halt_input(void *addr)
523{
524	struct cs428x_softc *sc;
525
526	sc = addr;
527	BA0WRITE4(sc, CS4281_DCR1, BA0READ4(sc, CS4281_DCR1) | DCRn_MSK);
528	sc->sc_rrun = 0;
529	return 0;
530}
531
532static int
533cs4281_getdev(void *addr, struct audio_device *retp)
534{
535
536	*retp = cs4281_device;
537	return 0;
538}
539
540static int
541cs4281_trigger_output(void *addr, void *start, void *end, int blksize,
542		      void (*intr)(void *), void *arg,
543		      const audio_params_t *param)
544{
545	struct cs428x_softc *sc;
546	uint32_t fmt;
547	struct cs428x_dma *p;
548	int dma_count;
549
550	sc = addr;
551	fmt = 0;
552#ifdef DIAGNOSTIC
553	if (sc->sc_prun)
554		printf("cs4281_trigger_output: already running\n");
555#endif
556	sc->sc_prun = 1;
557
558	DPRINTF(("cs4281_trigger_output: sc=%p start=%p end=%p "
559		 "blksize=%d intr=%p(%p)\n", addr, start, end, blksize, intr, arg));
560	sc->sc_pintr = intr;
561	sc->sc_parg  = arg;
562
563	/* stop playback DMA */
564	BA0WRITE4(sc, CS4281_DCR0, BA0READ4(sc, CS4281_DCR0) | DCRn_MSK);
565
566	DPRINTF(("param: precision=%d channels=%d encoding=%d\n",
567	       param->precision, param->channels, param->encoding));
568	for (p = sc->sc_dmas; p != NULL && BUFADDR(p) != start; p = p->next)
569		continue;
570	if (p == NULL) {
571		printf("cs4281_trigger_output: bad addr %p\n", start);
572		return EINVAL;
573	}
574
575	sc->sc_pcount = blksize / sc->hw_blocksize;
576	sc->sc_ps = (char *)start;
577	sc->sc_pe = (char *)end;
578	sc->sc_pdma = p;
579	sc->sc_pbuf = KERNADDR(p);
580	sc->sc_pi = 0;
581	sc->sc_pn = sc->sc_ps;
582	if (blksize >= sc->dma_size) {
583		sc->sc_pn = sc->sc_ps + sc->dma_size;
584		memcpy(sc->sc_pbuf, start, sc->dma_size);
585		++sc->sc_pi;
586	} else {
587		sc->sc_pn = sc->sc_ps + sc->hw_blocksize;
588		memcpy(sc->sc_pbuf, start, sc->hw_blocksize);
589	}
590
591	dma_count = sc->dma_size;
592	if (param->precision != 8)
593		dma_count /= 2;   /* 16 bit */
594	if (param->channels > 1)
595		dma_count /= 2;   /* Stereo */
596
597	DPRINTF(("cs4281_trigger_output: DMAADDR(p)=0x%x count=%d\n",
598		 (int)DMAADDR(p), dma_count));
599	BA0WRITE4(sc, CS4281_DBA0, DMAADDR(p));
600	BA0WRITE4(sc, CS4281_DBC0, dma_count-1);
601
602	/* set playback format */
603	fmt = BA0READ4(sc, CS4281_DMR0) & ~DMRn_FMTMSK;
604	if (param->precision == 8)
605		fmt |= DMRn_SIZE8;
606	if (param->channels == 1)
607		fmt |= DMRn_MONO;
608	if (param->encoding == AUDIO_ENCODING_ULINEAR_BE ||
609	    param->encoding == AUDIO_ENCODING_SLINEAR_BE)
610		fmt |= DMRn_BEND;
611	if (param->encoding == AUDIO_ENCODING_ULINEAR_BE ||
612	    param->encoding == AUDIO_ENCODING_ULINEAR_LE)
613		fmt |= DMRn_USIGN;
614	BA0WRITE4(sc, CS4281_DMR0, fmt);
615
616	/* set sample rate */
617	sc->sc_prate = param->sample_rate;
618	cs4281_set_dac_rate(sc, param->sample_rate);
619
620	/* start DMA */
621	BA0WRITE4(sc, CS4281_DCR0, BA0READ4(sc, CS4281_DCR0) & ~DCRn_MSK);
622	/* Enable interrupts */
623	BA0WRITE4(sc, CS4281_HICR, HICR_IEV | HICR_CHGM);
624
625	DPRINTF(("HICR =0x%08x(expected 0x00000001)\n", BA0READ4(sc, CS4281_HICR)));
626	DPRINTF(("HIMR =0x%08x(expected 0x00f0fc3f)\n", BA0READ4(sc, CS4281_HIMR)));
627	DPRINTF(("DMR0 =0x%08x(expected 0x2???0018)\n", BA0READ4(sc, CS4281_DMR0)));
628	DPRINTF(("DCR0 =0x%08x(expected 0x00030000)\n", BA0READ4(sc, CS4281_DCR0)));
629	DPRINTF(("FCR0 =0x%08x(expected 0x81000f00)\n", BA0READ4(sc, CS4281_FCR0)));
630	DPRINTF(("DACSR=0x%08x(expected 1 for 44kHz 5 for 8kHz)\n",
631		 BA0READ4(sc, CS4281_DACSR)));
632	DPRINTF(("SRCSA=0x%08x(expected 0x0b0a0100)\n", BA0READ4(sc, CS4281_SRCSA)));
633	DPRINTF(("SSPM&SSPM_PSRCEN =0x%08x(expected 0x00000010)\n",
634		 BA0READ4(sc, CS4281_SSPM) & SSPM_PSRCEN));
635
636	return 0;
637}
638
639static int
640cs4281_trigger_input(void *addr, void *start, void *end, int blksize,
641		     void (*intr)(void *), void *arg,
642		     const audio_params_t *param)
643{
644	struct cs428x_softc *sc;
645	struct cs428x_dma *p;
646	uint32_t fmt;
647	int dma_count;
648
649	sc = addr;
650	fmt = 0;
651#ifdef DIAGNOSTIC
652	if (sc->sc_rrun)
653		printf("cs4281_trigger_input: already running\n");
654#endif
655	sc->sc_rrun = 1;
656	DPRINTF(("cs4281_trigger_input: sc=%p start=%p end=%p "
657	    "blksize=%d intr=%p(%p)\n", addr, start, end, blksize, intr, arg));
658	sc->sc_rintr = intr;
659	sc->sc_rarg  = arg;
660
661	/* stop recording DMA */
662	BA0WRITE4(sc, CS4281_DCR1, BA0READ4(sc, CS4281_DCR1) | DCRn_MSK);
663
664	for (p = sc->sc_dmas; p && BUFADDR(p) != start; p = p->next)
665		continue;
666	if (!p) {
667		printf("cs4281_trigger_input: bad addr %p\n", start);
668		return EINVAL;
669	}
670
671	sc->sc_rcount = blksize / sc->hw_blocksize;
672	sc->sc_rs = (char *)start;
673	sc->sc_re = (char *)end;
674	sc->sc_rdma = p;
675	sc->sc_rbuf = KERNADDR(p);
676	sc->sc_ri = 0;
677	sc->sc_rn = sc->sc_rs;
678
679	dma_count = sc->dma_size;
680	if (param->precision != 8)
681		dma_count /= 2;
682	if (param->channels > 1)
683		dma_count /= 2;
684
685	DPRINTF(("cs4281_trigger_input: DMAADDR(p)=0x%x count=%d\n",
686		 (int)DMAADDR(p), dma_count));
687	BA0WRITE4(sc, CS4281_DBA1, DMAADDR(p));
688	BA0WRITE4(sc, CS4281_DBC1, dma_count-1);
689
690	/* set recording format */
691	fmt = BA0READ4(sc, CS4281_DMR1) & ~DMRn_FMTMSK;
692	if (param->precision == 8)
693		fmt |= DMRn_SIZE8;
694	if (param->channels == 1)
695		fmt |= DMRn_MONO;
696	if (param->encoding == AUDIO_ENCODING_ULINEAR_BE ||
697	    param->encoding == AUDIO_ENCODING_SLINEAR_BE)
698		fmt |= DMRn_BEND;
699	if (param->encoding == AUDIO_ENCODING_ULINEAR_BE ||
700	    param->encoding == AUDIO_ENCODING_ULINEAR_LE)
701		fmt |= DMRn_USIGN;
702	BA0WRITE4(sc, CS4281_DMR1, fmt);
703
704	/* set sample rate */
705	sc->sc_rrate = param->sample_rate;
706	cs4281_set_adc_rate(sc, param->sample_rate);
707
708	/* Start DMA */
709	BA0WRITE4(sc, CS4281_DCR1, BA0READ4(sc, CS4281_DCR1) & ~DCRn_MSK);
710	/* Enable interrupts */
711	BA0WRITE4(sc, CS4281_HICR, HICR_IEV | HICR_CHGM);
712
713	DPRINTF(("HICR=0x%08x\n", BA0READ4(sc, CS4281_HICR)));
714	DPRINTF(("HIMR=0x%08x\n", BA0READ4(sc, CS4281_HIMR)));
715	DPRINTF(("DMR1=0x%08x\n", BA0READ4(sc, CS4281_DMR1)));
716	DPRINTF(("DCR1=0x%08x\n", BA0READ4(sc, CS4281_DCR1)));
717
718	return 0;
719}
720
721/* Power Hook */
722static void
723cs4281_power(int why, void *v)
724{
725	static uint32_t dba0 = 0, dbc0 = 0, dmr0 = 0, dcr0 = 0;
726	static uint32_t dba1 = 0, dbc1 = 0, dmr1 = 0, dcr1 = 0;
727	struct cs428x_softc *sc;
728
729	sc = (struct cs428x_softc *)v;
730	DPRINTF(("%s: cs4281_power why=%d\n", sc->sc_dev.dv_xname, why));
731	switch (why) {
732	case PWR_SUSPEND:
733	case PWR_STANDBY:
734		sc->sc_suspend = why;
735
736		/* save current playback status */
737		if (sc->sc_prun) {
738			dcr0 = BA0READ4(sc, CS4281_DCR0);
739			dmr0 = BA0READ4(sc, CS4281_DMR0);
740			dbc0 = BA0READ4(sc, CS4281_DBC0);
741			dba0 = BA0READ4(sc, CS4281_DBA0);
742		}
743
744		/* save current capture status */
745		if (sc->sc_rrun) {
746			dcr1 = BA0READ4(sc, CS4281_DCR1);
747			dmr1 = BA0READ4(sc, CS4281_DMR1);
748			dbc1 = BA0READ4(sc, CS4281_DBC1);
749			dba1 = BA0READ4(sc, CS4281_DBA1);
750		}
751		/* Stop DMA */
752		BA0WRITE4(sc, CS4281_DCR0, BA0READ4(sc, CS4281_DCR0) | DCRn_MSK);
753		BA0WRITE4(sc, CS4281_DCR1, BA0READ4(sc, CS4281_DCR1) | DCRn_MSK);
754		break;
755	case PWR_RESUME:
756		if (sc->sc_suspend == PWR_RESUME) {
757			printf("cs4281_power: odd, resume without suspend.\n");
758			sc->sc_suspend = why;
759			return;
760		}
761		sc->sc_suspend = why;
762		cs4281_init(sc, 0);
763		cs4281_reset_codec(sc);
764
765		/* restore ac97 registers */
766		(*sc->codec_if->vtbl->restore_ports)(sc->codec_if);
767
768		/* restore DMA related status */
769		if (sc->sc_prun) {
770			cs4281_set_dac_rate(sc, sc->sc_prate);
771			BA0WRITE4(sc, CS4281_DBA0, dba0);
772			BA0WRITE4(sc, CS4281_DBC0, dbc0);
773			BA0WRITE4(sc, CS4281_DMR0, dmr0);
774			BA0WRITE4(sc, CS4281_DCR0, dcr0);
775		}
776		if (sc->sc_rrun) {
777			cs4281_set_adc_rate(sc, sc->sc_rrate);
778			BA0WRITE4(sc, CS4281_DBA1, dba1);
779			BA0WRITE4(sc, CS4281_DBC1, dbc1);
780			BA0WRITE4(sc, CS4281_DMR1, dmr1);
781			BA0WRITE4(sc, CS4281_DCR1, dcr1);
782		}
783		/* enable intterupts */
784		if (sc->sc_prun || sc->sc_rrun)
785			BA0WRITE4(sc, CS4281_HICR, HICR_IEV | HICR_CHGM);
786		break;
787	case PWR_SOFTSUSPEND:
788	case PWR_SOFTSTANDBY:
789	case PWR_SOFTRESUME:
790		break;
791	}
792}
793
794/* control AC97 codec */
795static int
796cs4281_reset_codec(void *addr)
797{
798	struct cs428x_softc *sc;
799	uint16_t data;
800	uint32_t dat32;
801	int n;
802
803	sc = addr;
804
805	DPRINTFN(3, ("cs4281_reset_codec\n"));
806
807	/* Reset codec */
808	BA0WRITE4(sc, CS428X_ACCTL, 0);
809	delay(50);    /* delay 50us */
810
811	BA0WRITE4(sc, CS4281_SPMC, 0);
812	delay(100);	/* delay 100us */
813	BA0WRITE4(sc, CS4281_SPMC, SPMC_RSTN);
814#if defined(ENABLE_SECONDARY_CODEC)
815	BA0WRITE4(sc, CS4281_SPMC, SPMC_RSTN | SPCM_ASDIN2E);
816	BA0WRITE4(sc, CS4281_SERMC, SERMC_TCID);
817#endif
818	delay(50000);   /* XXX: delay 50ms */
819
820	/* Enable ASYNC generation */
821	BA0WRITE4(sc, CS428X_ACCTL, ACCTL_ESYN);
822
823	/* Wait for codec ready. Linux driver waits 50ms here */
824	n = 0;
825	while ((BA0READ4(sc, CS428X_ACSTS) & ACSTS_CRDY) == 0) {
826		delay(100);
827		if (++n > 1000) {
828			printf("reset_codec: AC97 codec ready timeout\n");
829			return ETIMEDOUT;
830		}
831	}
832#if defined(ENABLE_SECONDARY_CODEC)
833	/* secondary codec ready*/
834	n = 0;
835	while ((BA0READ4(sc, CS4281_ACSTS2) & ACSTS2_CRDY2) == 0) {
836		delay(100);
837		if (++n > 1000)
838			return 0;
839	}
840#endif
841	/* Set the serial timing configuration */
842	/* XXX: undocumented but the Linux driver do this */
843	BA0WRITE4(sc, CS4281_SERMC, SERMC_PTCAC97);
844
845	/* Wait for codec ready signal */
846	n = 0;
847	do {
848		delay(1000);
849		if (++n > 1000) {
850			printf("%s: timeout waiting for codec ready\n",
851			       sc->sc_dev.dv_xname);
852			return ETIMEDOUT;
853		}
854		dat32 = BA0READ4(sc, CS428X_ACSTS) & ACSTS_CRDY;
855	} while (dat32 == 0);
856
857	/* Enable Valid Frame output on ASDOUT */
858	BA0WRITE4(sc, CS428X_ACCTL, ACCTL_ESYN | ACCTL_VFRM);
859
860	/* Wait until codec calibration is finished. Codec register 26h */
861	n = 0;
862	do {
863		delay(1);
864		if (++n > 1000) {
865			printf("%s: timeout waiting for codec calibration\n",
866			       sc->sc_dev.dv_xname);
867			return ETIMEDOUT;
868		}
869		cs428x_read_codec(sc, AC97_REG_POWER, &data);
870	} while ((data & 0x0f) != 0x0f);
871
872	/* Set the serial timing configuration again */
873	/* XXX: undocumented but the Linux driver do this */
874	BA0WRITE4(sc, CS4281_SERMC, SERMC_PTCAC97);
875
876	/* Wait until we've sampled input slots 3 & 4 as valid */
877	n = 0;
878	do {
879		delay(1000);
880		if (++n > 1000) {
881			printf("%s: timeout waiting for sampled input slots as valid\n",
882			       sc->sc_dev.dv_xname);
883			return ETIMEDOUT;
884		}
885		dat32 = BA0READ4(sc, CS428X_ACISV) & (ACISV_ISV3 | ACISV_ISV4) ;
886	} while (dat32 != (ACISV_ISV3 | ACISV_ISV4));
887
888	/* Start digital data transfer of audio data to the codec */
889	BA0WRITE4(sc, CS428X_ACOSV, (ACOSV_SLV3 | ACOSV_SLV4));
890	return 0;
891}
892
893
894/* Internal functions */
895
896/* convert sample rate to register value */
897static uint8_t
898cs4281_sr2regval(int rate)
899{
900	uint8_t retval;
901
902	/* We don't have to change here. but anyway ... */
903	if (rate > 48000)
904		rate = 48000;
905	if (rate < 6023)
906		rate = 6023;
907
908	switch (rate) {
909	case 8000:
910		retval = 5;
911		break;
912	case 11025:
913		retval = 4;
914		break;
915	case 16000:
916		retval = 3;
917		break;
918	case 22050:
919		retval = 2;
920		break;
921	case 44100:
922		retval = 1;
923		break;
924	case 48000:
925		retval = 0;
926		break;
927	default:
928		retval = 1536000/rate; /* == 24576000/(rate*16) */
929	}
930	return retval;
931}
932
933static void
934cs4281_set_adc_rate(struct cs428x_softc *sc, int rate)
935{
936
937	BA0WRITE4(sc, CS4281_ADCSR, cs4281_sr2regval(rate));
938}
939
940static void
941cs4281_set_dac_rate(struct cs428x_softc *sc, int rate)
942{
943
944	BA0WRITE4(sc, CS4281_DACSR, cs4281_sr2regval(rate));
945}
946
947static int
948cs4281_init(struct cs428x_softc *sc, int init)
949{
950	int n;
951	uint16_t data;
952	uint32_t dat32;
953
954	/* set "Configuration Write Protect" register to
955	 * 0x4281 to allow to write */
956	BA0WRITE4(sc, CS4281_CWPR, 0x4281);
957
958	/*
959	 * Unset "Full Power-Down bit of Extended PCI Power Management
960	 * Control" register to release the reset state.
961	 */
962	dat32 = BA0READ4(sc, CS4281_EPPMC);
963	if (dat32 & EPPMC_FPDN) {
964		BA0WRITE4(sc, CS4281_EPPMC, dat32 & ~EPPMC_FPDN);
965	}
966
967	/* Start PLL out in known state */
968	BA0WRITE4(sc, CS4281_CLKCR1, 0);
969	/* Start serial ports out in known state */
970	BA0WRITE4(sc, CS4281_SERMC, 0);
971
972	/* Reset codec */
973	BA0WRITE4(sc, CS428X_ACCTL, 0);
974	delay(50);	/* delay 50us */
975
976	BA0WRITE4(sc, CS4281_SPMC, 0);
977	delay(100);	/* delay 100us */
978	BA0WRITE4(sc, CS4281_SPMC, SPMC_RSTN);
979#if defined(ENABLE_SECONDARY_CODEC)
980	BA0WRITE4(sc, CS4281_SPMC, SPMC_RSTN | SPCM_ASDIN2E);
981	BA0WRITE4(sc, CS4281_SERMC, SERMC_TCID);
982#endif
983	delay(50000);   /* XXX: delay 50ms */
984
985	/* Turn on Sound System clocks based on ABITCLK */
986	BA0WRITE4(sc, CS4281_CLKCR1, CLKCR1_DLLP);
987	delay(50000);   /* XXX: delay 50ms */
988	BA0WRITE4(sc, CS4281_CLKCR1, CLKCR1_SWCE | CLKCR1_DLLP);
989
990	/* Set enables for sections that are needed in the SSPM registers */
991	BA0WRITE4(sc, CS4281_SSPM,
992		  SSPM_MIXEN |		/* Mixer */
993		  SSPM_CSRCEN |		/* Capture SRC */
994		  SSPM_PSRCEN |		/* Playback SRC */
995		  SSPM_JSEN |		/* Joystick */
996		  SSPM_ACLEN |		/* AC LINK */
997		  SSPM_FMEN		/* FM */
998		  );
999
1000	/* Wait for clock stabilization */
1001	n = 0;
1002#if 1
1003	/* what document says */
1004	while ((BA0READ4(sc, CS4281_CLKCR1)& (CLKCR1_DLLRDY | CLKCR1_CLKON))
1005		 != (CLKCR1_DLLRDY | CLKCR1_CLKON)) {
1006		delay(100);
1007		if (++n > 1000) {
1008			printf("%s: timeout waiting for clock stabilization\n",
1009			       sc->sc_dev.dv_xname);
1010			return -1;
1011		}
1012	}
1013#else
1014	/* Cirrus driver for Linux does */
1015	while (!(BA0READ4(sc, CS4281_CLKCR1) & CLKCR1_DLLRDY)) {
1016		delay(1000);
1017		if (++n > 1000) {
1018			printf("%s: timeout waiting for clock stabilization\n",
1019			       sc->sc_dev.dv_xname);
1020			return -1;
1021		}
1022	}
1023#endif
1024
1025	/* Enable ASYNC generation */
1026	BA0WRITE4(sc, CS428X_ACCTL, ACCTL_ESYN);
1027
1028	/* Wait for codec ready. Linux driver waits 50ms here */
1029	n = 0;
1030	while ((BA0READ4(sc, CS428X_ACSTS) & ACSTS_CRDY) == 0) {
1031		delay(100);
1032		if (++n > 1000) {
1033			printf("%s: timeout waiting for codec ready\n",
1034			       sc->sc_dev.dv_xname);
1035			return -1;
1036		}
1037	}
1038
1039#if defined(ENABLE_SECONDARY_CODEC)
1040	/* secondary codec ready*/
1041	n = 0;
1042	while ((BA0READ4(sc, CS4281_ACSTS2) & ACSTS2_CRDY2) == 0) {
1043		delay(100);
1044		if (++n > 1000) {
1045			printf("%s: timeout waiting for secondary codec ready\n",
1046			       sc->sc_dev.dv_xname);
1047			return -1;
1048		}
1049	}
1050#endif
1051
1052	/* Set the serial timing configuration */
1053	/* XXX: undocumented but the Linux driver do this */
1054	BA0WRITE4(sc, CS4281_SERMC, SERMC_PTCAC97);
1055
1056	/* Wait for codec ready signal */
1057	n = 0;
1058	do {
1059		delay(1000);
1060		if (++n > 1000) {
1061			printf("%s: timeout waiting for codec ready\n",
1062			       sc->sc_dev.dv_xname);
1063			return -1;
1064		}
1065		dat32 = BA0READ4(sc, CS428X_ACSTS) & ACSTS_CRDY;
1066	} while (dat32 == 0);
1067
1068	/* Enable Valid Frame output on ASDOUT */
1069	BA0WRITE4(sc, CS428X_ACCTL, ACCTL_ESYN | ACCTL_VFRM);
1070
1071	/* Wait until codec calibration is finished. codec register 26h */
1072	n = 0;
1073	do {
1074		delay(1);
1075		if (++n > 1000) {
1076			printf("%s: timeout waiting for codec calibration\n",
1077			       sc->sc_dev.dv_xname);
1078			return -1;
1079		}
1080		cs428x_read_codec(sc, AC97_REG_POWER, &data);
1081	} while ((data & 0x0f) != 0x0f);
1082
1083	/* Set the serial timing configuration again */
1084	/* XXX: undocumented but the Linux driver do this */
1085	BA0WRITE4(sc, CS4281_SERMC, SERMC_PTCAC97);
1086
1087	/* Wait until we've sampled input slots 3 & 4 as valid */
1088	n = 0;
1089	do {
1090		delay(1000);
1091		if (++n > 1000) {
1092			printf("%s: timeout waiting for sampled input slots as valid\n",
1093			       sc->sc_dev.dv_xname);
1094			return -1;
1095		}
1096		dat32 = BA0READ4(sc, CS428X_ACISV) & (ACISV_ISV3 | ACISV_ISV4);
1097	} while (dat32 != (ACISV_ISV3 | ACISV_ISV4));
1098
1099	/* Start digital data transfer of audio data to the codec */
1100	BA0WRITE4(sc, CS428X_ACOSV, (ACOSV_SLV3 | ACOSV_SLV4));
1101
1102	cs428x_write_codec(sc, AC97_REG_HEADPHONE_VOLUME, 0);
1103	cs428x_write_codec(sc, AC97_REG_MASTER_VOLUME, 0);
1104
1105	/* Power on the DAC */
1106	cs428x_read_codec(sc, AC97_REG_POWER, &data);
1107	cs428x_write_codec(sc, AC97_REG_POWER, data & 0xfdff);
1108
1109	/* Wait until we sample a DAC ready state.
1110	 * Not documented, but Linux driver does.
1111	 */
1112	for (n = 0; n < 32; ++n) {
1113		delay(1000);
1114		cs428x_read_codec(sc, AC97_REG_POWER, &data);
1115		if (data & 0x02)
1116			break;
1117	}
1118
1119	/* Power on the ADC */
1120	cs428x_read_codec(sc, AC97_REG_POWER, &data);
1121	cs428x_write_codec(sc, AC97_REG_POWER, data & 0xfeff);
1122
1123	/* Wait until we sample ADC ready state.
1124	 * Not documented, but Linux driver does.
1125	 */
1126	for (n = 0; n < 32; ++n) {
1127		delay(1000);
1128		cs428x_read_codec(sc, AC97_REG_POWER, &data);
1129		if (data & 0x01)
1130			break;
1131	}
1132
1133#if 0
1134	/* Initialize AC-Link features */
1135	/* variable sample-rate support */
1136	mem = BA0READ4(sc, CS4281_SERMC);
1137	mem |=  (SERMC_ODSEN1 | SERMC_ODSEN2);
1138	BA0WRITE4(sc, CS4281_SERMC, mem);
1139	/* XXX: more... */
1140
1141	/* Initialize SSCR register features */
1142	/* XXX: hardware volume setting */
1143	BA0WRITE4(sc, CS4281_SSCR, ~SSCR_HVC); /* disable HW volume setting */
1144#endif
1145
1146	/* disable Sound Blaster Pro emulation */
1147	/* XXX:
1148	 * Cannot set since the documents does not describe which bit is
1149	 * correspond to SSCR_SB. Since the reset value of SSCR is 0,
1150	 * we can ignore it.*/
1151#if 0
1152	BA0WRITE4(sc, CS4281_SSCR, SSCR_SB);
1153#endif
1154
1155	/* map AC97 PCM playback to DMA Channel 0 */
1156	/* Reset FEN bit to setup first */
1157	BA0WRITE4(sc, CS4281_FCR0, (BA0READ4(sc, CS4281_FCR0) & ~FCRn_FEN));
1158	/*
1159	 *| RS[4:0]/|        |
1160	 *| LS[4:0] |  AC97  | Slot Function
1161	 *|---------+--------+--------------------
1162	 *|     0   |    3   | Left PCM Playback
1163	 *|     1   |    4   | Right PCM Playback
1164	 *|     2   |    5   | Phone Line 1 DAC
1165	 *|     3   |    6   | Center PCM Playback
1166	 *....
1167	 *  quoted from Table 29(p109)
1168	 */
1169	dat32 = 0x01 << 24 |   /* RS[4:0] =  1 see above */
1170		0x00 << 16 |   /* LS[4:0] =  0 see above */
1171		0x0f <<  8 |   /* SZ[6:0] = 15 size of buffer */
1172		0x00 <<  0 ;   /* OF[6:0] =  0 offset */
1173	BA0WRITE4(sc, CS4281_FCR0, dat32);
1174	BA0WRITE4(sc, CS4281_FCR0, dat32 | FCRn_FEN);
1175
1176	/* map AC97 PCM record to DMA Channel 1 */
1177	/* Reset FEN bit to setup first */
1178	BA0WRITE4(sc, CS4281_FCR1, (BA0READ4(sc, CS4281_FCR1) & ~FCRn_FEN));
1179	/*
1180	 *| RS[4:0]/|
1181	 *| LS[4:0] | AC97 | Slot Function
1182	 *|---------+------+-------------------
1183	 *|   10    |   3  | Left PCM Record
1184	 *|   11    |   4  | Right PCM Record
1185	 *|   12    |   5  | Phone Line 1 ADC
1186	 *|   13    |   6  | Mic ADC
1187	 *....
1188	 * quoted from Table 30(p109)
1189	 */
1190	dat32 = 0x0b << 24 |    /* RS[4:0] = 11 See above */
1191		0x0a << 16 |    /* LS[4:0] = 10 See above */
1192		0x0f <<  8 |    /* SZ[6:0] = 15 Size of buffer */
1193		0x10 <<  0 ;    /* OF[6:0] = 16 offset */
1194
1195	/* XXX: I cannot understand why FCRn_PSH is needed here. */
1196	BA0WRITE4(sc, CS4281_FCR1, dat32 | FCRn_PSH);
1197	BA0WRITE4(sc, CS4281_FCR1, dat32 | FCRn_FEN);
1198
1199#if 0
1200	/* Disable DMA Channel 2, 3 */
1201	BA0WRITE4(sc, CS4281_FCR2, (BA0READ4(sc, CS4281_FCR2) & ~FCRn_FEN));
1202	BA0WRITE4(sc, CS4281_FCR3, (BA0READ4(sc, CS4281_FCR3) & ~FCRn_FEN));
1203#endif
1204
1205	/* Set the SRC Slot Assignment accordingly */
1206	/*| PLSS[4:0]/
1207	 *| PRSS[4:0] | AC97 | Slot Function
1208	 *|-----------+------+----------------
1209	 *|     0     |  3   | Left PCM Playback
1210	 *|     1     |  4   | Right PCM Playback
1211	 *|     2     |  5   | phone line 1 DAC
1212	 *|     3     |  6   | Center PCM Playback
1213	 *|     4     |  7   | Left Surround PCM Playback
1214	 *|     5     |  8   | Right Surround PCM Playback
1215	 *......
1216	 *
1217	 *| CLSS[4:0]/
1218	 *| CRSS[4:0] | AC97 | Codec |Slot Function
1219	 *|-----------+------+-------+-----------------
1220	 *|    10     |   3  |Primary| Left PCM Record
1221	 *|    11     |   4  |Primary| Right PCM Record
1222	 *|    12     |   5  |Primary| Phone Line 1 ADC
1223	 *|    13     |   6  |Primary| Mic ADC
1224	 *|.....
1225	 *|    20     |   3  |  Sec. | Left PCM Record
1226	 *|    21     |   4  |  Sec. | Right PCM Record
1227	 *|    22     |   5  |  Sec. | Phone Line 1 ADC
1228	 *|    23     |   6  |  Sec. | Mic ADC
1229	 */
1230	dat32 = 0x0b << 24 |   /* CRSS[4:0] Right PCM Record(primary) */
1231		0x0a << 16 |   /* CLSS[4:0] Left  PCM Record(primary) */
1232		0x01 <<  8 |   /* PRSS[4:0] Right PCM Playback */
1233		0x00 <<  0;    /* PLSS[4:0] Left  PCM Playback */
1234	BA0WRITE4(sc, CS4281_SRCSA, dat32);
1235
1236	/* Set interrupt to occurred at Half and Full terminal
1237	 * count interrupt enable for DMA channel 0 and 1.
1238	 * To keep DMA stop, set MSK.
1239	 */
1240	dat32 = DCRn_HTCIE | DCRn_TCIE | DCRn_MSK;
1241	BA0WRITE4(sc, CS4281_DCR0, dat32);
1242	BA0WRITE4(sc, CS4281_DCR1, dat32);
1243
1244	/* Set Auto-Initialize Contorl enable */
1245	BA0WRITE4(sc, CS4281_DMR0,
1246		  DMRn_DMA | DMRn_AUTO | DMRn_TR_READ);
1247	BA0WRITE4(sc, CS4281_DMR1,
1248		  DMRn_DMA | DMRn_AUTO | DMRn_TR_WRITE);
1249
1250	/* Clear DMA Mask in HIMR */
1251	dat32 = ~HIMR_DMAIM & ~HIMR_D1IM & ~HIMR_D0IM;
1252	BA0WRITE4(sc, CS4281_HIMR,
1253		  BA0READ4(sc, CS4281_HIMR) & dat32);
1254
1255	/* set current status */
1256	if (init != 0) {
1257		sc->sc_prun = 0;
1258		sc->sc_rrun = 0;
1259	}
1260
1261	/* setup playback volume */
1262	BA0WRITE4(sc, CS4281_PPRVC, 7);
1263	BA0WRITE4(sc, CS4281_PPLVC, 7);
1264
1265	return 0;
1266}
1267