xref: /netbsd-src/sys/dev/pci/auvia.c (revision d710132b4b8ce7f7cccaaf660cb16aa16b4077a0)
1 /*	$NetBSD: auvia.c,v 1.31 2003/05/03 18:11:32 wiz Exp $	*/
2 
3 /*-
4  * Copyright (c) 2000 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Tyler C. Sarna
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the NetBSD
21  *	Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * VIA Technologies VT82C686A / VT8233 / VT8235 Southbridge Audio Driver
41  *
42  * Documentation links:
43  *
44  * ftp://ftp.alsa-project.org/pub/manuals/via/686a.pdf
45  * ftp://ftp.alsa-project.org/pub/manuals/general/ac97r21.pdf
46  * ftp://ftp.alsa-project.org/pub/manuals/ad/AD1881_0.pdf (example AC'97 codec)
47  */
48 
49 #include <sys/cdefs.h>
50 __KERNEL_RCSID(0, "$NetBSD: auvia.c,v 1.31 2003/05/03 18:11:32 wiz Exp $");
51 
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/malloc.h>
55 #include <sys/device.h>
56 #include <sys/audioio.h>
57 
58 #include <uvm/uvm_extern.h>
59 
60 #include <dev/pci/pcidevs.h>
61 #include <dev/pci/pcivar.h>
62 
63 #include <dev/audio_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/auviavar.h>
71 
72 struct auvia_dma {
73 	struct auvia_dma *next;
74 	caddr_t addr;
75 	size_t size;
76 	bus_dmamap_t map;
77 	bus_dma_segment_t seg;
78 };
79 
80 struct auvia_dma_op {
81 	u_int32_t ptr;
82 	u_int32_t flags;
83 #define AUVIA_DMAOP_EOL		0x80000000
84 #define AUVIA_DMAOP_FLAG	0x40000000
85 #define AUVIA_DMAOP_STOP	0x20000000
86 #define AUVIA_DMAOP_COUNT(x)	((x)&0x00FFFFFF)
87 };
88 
89 int	auvia_match(struct device *, struct cfdata *, void *);
90 void	auvia_attach(struct device *, struct device *, void *);
91 int	auvia_open(void *, int);
92 void	auvia_close(void *);
93 int	auvia_query_encoding(void *, struct audio_encoding *);
94 void	auvia_set_params_sub(struct auvia_softc *, struct auvia_softc_chan *,
95 	struct audio_params *);
96 int	auvia_set_params(void *, int, int, struct audio_params *,
97 	struct audio_params *);
98 int	auvia_round_blocksize(void *, int);
99 int	auvia_halt_output(void *);
100 int	auvia_halt_input(void *);
101 int	auvia_getdev(void *, struct audio_device *);
102 int	auvia_set_port(void *, mixer_ctrl_t *);
103 int	auvia_get_port(void *, mixer_ctrl_t *);
104 int	auvia_query_devinfo(void *, mixer_devinfo_t *);
105 void *	auvia_malloc(void *, int, size_t, struct malloc_type *, int);
106 void	auvia_free(void *, void *, struct malloc_type *);
107 size_t	auvia_round_buffersize(void *, int, size_t);
108 paddr_t	auvia_mappage(void *, void *, off_t, int);
109 int	auvia_get_props(void *);
110 int	auvia_build_dma_ops(struct auvia_softc *, struct auvia_softc_chan *,
111 	struct auvia_dma *, void *, void *, int);
112 int	auvia_trigger_output(void *, void *, void *, int, void (*)(void *),
113 	void *, struct audio_params *);
114 int	auvia_trigger_input(void *, void *, void *, int, void (*)(void *),
115 	void *, struct audio_params *);
116 
117 int	auvia_intr __P((void *));
118 
119 CFATTACH_DECL(auvia, sizeof (struct auvia_softc),
120     auvia_match, auvia_attach, NULL, NULL);
121 
122 #define AUVIA_PCICONF_JUNK	0x40
123 #define		AUVIA_PCICONF_ENABLES	 0x00FF0000	/* reg 42 mask */
124 #define		AUVIA_PCICONF_ACLINKENAB 0x00008000	/* ac link enab */
125 #define		AUVIA_PCICONF_ACNOTRST	 0x00004000	/* ~(ac reset) */
126 #define		AUVIA_PCICONF_ACSYNC	 0x00002000	/* ac sync */
127 #define		AUVIA_PCICONF_ACVSR	 0x00000800	/* var. samp. rate */
128 #define		AUVIA_PCICONF_ACSGD	 0x00000400	/* SGD enab */
129 #define		AUVIA_PCICONF_ACFM	 0x00000200	/* FM enab */
130 #define		AUVIA_PCICONF_ACSB	 0x00000100	/* SB enab */
131 
132 #define	AUVIA_PLAY_BASE			0x00
133 #define	AUVIA_RECORD_BASE		0x10
134 
135 /* *_RP_* are offsets from AUVIA_PLAY_BASE or AUVIA_RECORD_BASE */
136 #define	AUVIA_RP_STAT			0x00
137 #define		AUVIA_RPSTAT_INTR		0x03
138 #define	AUVIA_RP_CONTROL		0x01
139 #define		AUVIA_RPCTRL_START		0x80
140 #define		AUVIA_RPCTRL_TERMINATE		0x40
141 #define		AUVIA_RPCTRL_AUTOSTART		0x20
142 /* The following are 8233 specific */
143 #define		AUVIA_RPCTRL_STOP		0x04
144 #define		AUVIA_RPCTRL_EOL		0x02
145 #define		AUVIA_RPCTRL_FLAG		0x01
146 #define	AUVIA_RP_MODE			0x02		/* 82c686 specific */
147 #define		AUVIA_RPMODE_INTR_FLAG		0x01
148 #define		AUVIA_RPMODE_INTR_EOL		0x02
149 #define		AUVIA_RPMODE_STEREO		0x10
150 #define		AUVIA_RPMODE_16BIT		0x20
151 #define		AUVIA_RPMODE_AUTOSTART		0x80
152 #define	AUVIA_RP_DMAOPS_BASE		0x04
153 
154 #define	VIA8233_RP_DXS_LVOL		0x02
155 #define	VIA8233_RP_DXS_RVOL		0x03
156 #define	VIA8233_RP_RATEFMT		0x08
157 #define		VIA8233_RATEFMT_48K		0xfffff
158 #define		VIA8233_RATEFMT_STEREO		0x00100000
159 #define		VIA8233_RATEFMT_16BIT		0x00200000
160 
161 #define	VIA_RP_DMAOPS_COUNT		0x0c
162 
163 #define VIA8233_MP_BASE			0x40
164 	/* STAT, CONTROL, DMAOPS_BASE, DMAOPS_COUNT are valid */
165 #define VIA8233_OFF_MP_FORMAT		0x02
166 #define		VIA8233_MP_FORMAT_8BIT		0x00
167 #define		VIA8233_MP_FORMAT_16BIT		0x80
168 #define		VIA8233_MP_FORMAT_CHANNLE_MASK	0x70 /* 1, 2, 4, 6 */
169 #define VIA8233_OFF_MP_SCRATCH		0x03
170 #define VIA8233_OFF_MP_STOP		0x08
171 
172 #define	AUVIA_CODEC_CTL			0x80
173 #define		AUVIA_CODEC_READ		0x00800000
174 #define		AUVIA_CODEC_BUSY		0x01000000
175 #define		AUVIA_CODEC_PRIVALID		0x02000000
176 #define		AUVIA_CODEC_INDEX(x)		((x)<<16)
177 
178 #define CH_WRITE1(sc, ch, off, v)	\
179 	bus_space_write_1((sc)->sc_iot,	(sc)->sc_ioh, (ch)->sc_base + (off), v)
180 #define CH_WRITE4(sc, ch, off, v)	\
181 	bus_space_write_4((sc)->sc_iot,	(sc)->sc_ioh, (ch)->sc_base + (off), v)
182 #define CH_READ1(sc, ch, off)		\
183 	bus_space_read_1((sc)->sc_iot, (sc)->sc_ioh, (ch)->sc_base + (off))
184 #define CH_READ4(sc, ch, off)		\
185 	bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (ch)->sc_base + (off))
186 
187 #define TIMEOUT	50
188 
189 struct audio_hw_if auvia_hw_if = {
190 	auvia_open,
191 	auvia_close,
192 	NULL, /* drain */
193 	auvia_query_encoding,
194 	auvia_set_params,
195 	auvia_round_blocksize,
196 	NULL, /* commit_settings */
197 	NULL, /* init_output */
198 	NULL, /* init_input */
199 	NULL, /* start_output */
200 	NULL, /* start_input */
201 	auvia_halt_output,
202 	auvia_halt_input,
203 	NULL, /* speaker_ctl */
204 	auvia_getdev,
205 	NULL, /* setfd */
206 	auvia_set_port,
207 	auvia_get_port,
208 	auvia_query_devinfo,
209 	auvia_malloc,
210 	auvia_free,
211 	auvia_round_buffersize,
212 	auvia_mappage,
213 	auvia_get_props,
214 	auvia_trigger_output,
215 	auvia_trigger_input,
216 	NULL, /* dev_ioctl */
217 };
218 
219 int	auvia_attach_codec(void *, struct ac97_codec_if *);
220 int	auvia_write_codec(void *, u_int8_t, u_int16_t);
221 int	auvia_read_codec(void *, u_int8_t, u_int16_t *);
222 void	auvia_reset_codec(void *);
223 int	auvia_waitready_codec(struct auvia_softc *sc);
224 int	auvia_waitvalid_codec(struct auvia_softc *sc);
225 
226 
227 int
228 auvia_match(struct device *parent, struct cfdata *match, void *aux)
229 {
230 	struct pci_attach_args *pa = (struct pci_attach_args *) aux;
231 
232 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_VIATECH)
233 		return 0;
234 	switch (PCI_PRODUCT(pa->pa_id)) {
235 	case PCI_PRODUCT_VIATECH_VT82C686A_AC97:
236 	case PCI_PRODUCT_VIATECH_VT8233_AC97:
237 		break;
238 	default:
239 		return 0;
240 	}
241 
242 	return 1;
243 }
244 
245 
246 void
247 auvia_attach(struct device *parent, struct device *self, void *aux)
248 {
249 	struct pci_attach_args *pa = aux;
250 	struct auvia_softc *sc = (struct auvia_softc *) self;
251 	const char *intrstr = NULL;
252 	pci_chipset_tag_t pc = pa->pa_pc;
253 	pcitag_t pt = pa->pa_tag;
254 	pci_intr_handle_t ih;
255 	bus_size_t iosize;
256 	pcireg_t pr;
257 	int r;
258 
259 	aprint_naive(": Audio controller\n");
260 
261 	sc->sc_play.sc_base = AUVIA_PLAY_BASE;
262 	sc->sc_record.sc_base = AUVIA_RECORD_BASE;
263 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_VIATECH_VT8233_AC97) {
264 		sc->sc_flags |= AUVIA_FLAGS_VT8233;
265 		sc->sc_play.sc_base = VIA8233_MP_BASE;
266 	}
267 
268 	if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_IO, 0, &sc->sc_iot,
269 		&sc->sc_ioh, NULL, &iosize)) {
270 		aprint_error(": can't map i/o space\n");
271 		return;
272 	}
273 
274 	sc->sc_dmat = pa->pa_dmat;
275 	sc->sc_pc = pc;
276 	sc->sc_pt = pt;
277 
278 	r = PCI_REVISION(pa->pa_class);
279 	if (sc->sc_flags & AUVIA_FLAGS_VT8233) {
280 		sprintf(sc->sc_revision, "0x%02X", r);
281 		if (r < 0x50) {
282 			aprint_normal(": VIA VT8233 AC'97 Audio (rev %s)\n",
283 			       sc->sc_revision);
284 		} else {
285 			aprint_normal(": VIA VT8235 AC'97 Audio (rev %s)\n",
286 			       sc->sc_revision);
287 		}
288 	} else {
289 		sc->sc_revision[1] = '\0';
290 		if (r == 0x20) {
291 			sc->sc_revision[0] = 'H';
292 		} else if ((r >= 0x10) && (r <= 0x14)) {
293 			sc->sc_revision[0] = 'A' + (r - 0x10);
294 		} else {
295 			sprintf(sc->sc_revision, "0x%02X", r);
296 		}
297 
298 		aprint_normal(": VIA VT82C686A AC'97 Audio (rev %s)\n",
299 		       sc->sc_revision);
300 	}
301 
302 	if (pci_intr_map(pa, &ih)) {
303 		aprint_error(": couldn't map interrupt\n");
304 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, iosize);
305 		return;
306 	}
307 	intrstr = pci_intr_string(pc, ih);
308 
309 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO, auvia_intr, sc);
310 	if (sc->sc_ih == NULL) {
311 		aprint_error("%s: couldn't establish interrupt",
312 		    sc->sc_dev.dv_xname);
313 		if (intrstr != NULL)
314 			aprint_normal(" at %s", intrstr);
315 		aprint_normal("\n");
316 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, iosize);
317 		return;
318 	}
319 
320 	aprint_normal("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
321 
322 	/* disable SBPro compat & others */
323 	pr = pci_conf_read(pc, pt, AUVIA_PCICONF_JUNK);
324 
325 	pr &= ~AUVIA_PCICONF_ENABLES; /* clear compat function enables */
326 	/* XXX what to do about MIDI, FM, joystick? */
327 
328 	pr |= (AUVIA_PCICONF_ACLINKENAB | AUVIA_PCICONF_ACNOTRST
329 		| AUVIA_PCICONF_ACVSR | AUVIA_PCICONF_ACSGD);
330 
331 	pr &= ~(AUVIA_PCICONF_ACFM | AUVIA_PCICONF_ACSB);
332 
333 	pci_conf_write(pc, pt, AUVIA_PCICONF_JUNK, pr);
334 
335 	sc->host_if.arg = sc;
336 	sc->host_if.attach = auvia_attach_codec;
337 	sc->host_if.read = auvia_read_codec;
338 	sc->host_if.write = auvia_write_codec;
339 	sc->host_if.reset = auvia_reset_codec;
340 
341 	if ((r = ac97_attach(&sc->host_if)) != 0) {
342 		aprint_error("%s: can't attach codec (error 0x%X)\n",
343 			sc->sc_dev.dv_xname, r);
344 		pci_intr_disestablish(pc, sc->sc_ih);
345 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, iosize);
346 		return;
347 	}
348 
349 	audio_attach_mi(&auvia_hw_if, sc, &sc->sc_dev);
350 }
351 
352 
353 int
354 auvia_attach_codec(void *addr, struct ac97_codec_if *cif)
355 {
356 	struct auvia_softc *sc = addr;
357 
358 	sc->codec_if = cif;
359 
360 	return 0;
361 }
362 
363 
364 void
365 auvia_reset_codec(void *addr)
366 {
367 #ifdef notyet /* XXX seems to make codec become unready... ??? */
368 	struct auvia_softc *sc = addr;
369 	pcireg_t r;
370 
371 	/* perform a codec cold reset */
372 
373 	r = pci_conf_read(sc->sc_pc, sc->sc_pt, AUVIA_PCICONF_JUNK);
374 
375 	r &= ~AUVIA_PCICONF_ACNOTRST;	/* enable RESET (active low) */
376 	pci_conf_write(sc->sc_pc, sc->sc_pt, AUVIA_PCICONF_JUNK, r);
377 	delay(2);
378 
379 	r |= AUVIA_PCICONF_ACNOTRST;	/* disable RESET (inactive high) */
380 	pci_conf_write(sc->sc_pc, sc->sc_pt, AUVIA_PCICONF_JUNK, r);
381 	delay(200);
382 
383 	auvia_waitready_codec(sc);
384 #endif
385 }
386 
387 
388 int
389 auvia_waitready_codec(struct auvia_softc *sc)
390 {
391 	int i;
392 
393 	/* poll until codec not busy */
394 	for (i = 0; (i < TIMEOUT) && (bus_space_read_4(sc->sc_iot, sc->sc_ioh,
395 		AUVIA_CODEC_CTL) & AUVIA_CODEC_BUSY); i++)
396 		delay(1);
397 	if (i >= TIMEOUT) {
398 		printf("%s: codec busy\n", sc->sc_dev.dv_xname);
399 		return 1;
400 	}
401 
402 	return 0;
403 }
404 
405 
406 int
407 auvia_waitvalid_codec(struct auvia_softc *sc)
408 {
409 	int i;
410 
411 	/* poll until codec valid */
412 	for (i = 0; (i < TIMEOUT) && !(bus_space_read_4(sc->sc_iot, sc->sc_ioh,
413 		AUVIA_CODEC_CTL) & AUVIA_CODEC_PRIVALID); i++)
414 			delay(1);
415 	if (i >= TIMEOUT) {
416 		printf("%s: codec invalid\n", sc->sc_dev.dv_xname);
417 		return 1;
418 	}
419 
420 	return 0;
421 }
422 
423 
424 int
425 auvia_write_codec(void *addr, u_int8_t reg, u_int16_t val)
426 {
427 	struct auvia_softc *sc = addr;
428 
429 	if (auvia_waitready_codec(sc))
430 		return 1;
431 
432 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, AUVIA_CODEC_CTL,
433 		AUVIA_CODEC_PRIVALID | AUVIA_CODEC_INDEX(reg) | val);
434 
435 	return 0;
436 }
437 
438 
439 int
440 auvia_read_codec(void *addr, u_int8_t reg, u_int16_t *val)
441 {
442 	struct auvia_softc *sc = addr;
443 
444 	if (auvia_waitready_codec(sc))
445 		return 1;
446 
447 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, AUVIA_CODEC_CTL,
448 		AUVIA_CODEC_PRIVALID | AUVIA_CODEC_READ | AUVIA_CODEC_INDEX(reg));
449 
450 	if (auvia_waitready_codec(sc))
451 		return 1;
452 
453 	if (auvia_waitvalid_codec(sc))
454 		return 1;
455 
456 	*val = bus_space_read_2(sc->sc_iot, sc->sc_ioh, AUVIA_CODEC_CTL);
457 
458 	return 0;
459 }
460 
461 
462 int
463 auvia_open(void *addr, int flags)
464 {
465 	return 0;
466 }
467 
468 
469 void
470 auvia_close(void *addr)
471 {
472 	struct auvia_softc *sc = addr;
473 
474 	auvia_halt_output(sc);
475 	auvia_halt_input(sc);
476 
477 	sc->sc_play.sc_intr = NULL;
478 	sc->sc_record.sc_intr = NULL;
479 }
480 
481 
482 int
483 auvia_query_encoding(void *addr, struct audio_encoding *fp)
484 {
485 	switch (fp->index) {
486 	case 0:
487 		strcpy(fp->name, AudioEulinear);
488 		fp->encoding = AUDIO_ENCODING_ULINEAR;
489 		fp->precision = 8;
490 		fp->flags = 0;
491 		return (0);
492 	case 1:
493 		strcpy(fp->name, AudioEmulaw);
494 		fp->encoding = AUDIO_ENCODING_ULAW;
495 		fp->precision = 8;
496 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
497 		return (0);
498 	case 2:
499 		strcpy(fp->name, AudioEalaw);
500 		fp->encoding = AUDIO_ENCODING_ALAW;
501 		fp->precision = 8;
502 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
503 		return (0);
504 	case 3:
505 		strcpy(fp->name, AudioEslinear);
506 		fp->encoding = AUDIO_ENCODING_SLINEAR;
507 		fp->precision = 8;
508 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
509 		return (0);
510 	case 4:
511 		strcpy(fp->name, AudioEslinear_le);
512 		fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
513 		fp->precision = 16;
514 		fp->flags = 0;
515 		return (0);
516 	case 5:
517 		strcpy(fp->name, AudioEulinear_le);
518 		fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
519 		fp->precision = 16;
520 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
521 		return (0);
522 	case 6:
523 		strcpy(fp->name, AudioEslinear_be);
524 		fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
525 		fp->precision = 16;
526 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
527 		return (0);
528 	case 7:
529 		strcpy(fp->name, AudioEulinear_be);
530 		fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
531 		fp->precision = 16;
532 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
533 		return (0);
534 	default:
535 		return (EINVAL);
536 	}
537 }
538 
539 void
540 auvia_set_params_sub(struct auvia_softc *sc, struct auvia_softc_chan *ch,
541 		     struct audio_params *p)
542 {
543 	u_int32_t v;
544 	u_int16_t regval;
545 
546 	if (!(sc->sc_flags & AUVIA_FLAGS_VT8233)) {
547 		regval = (p->channels == 2 ? AUVIA_RPMODE_STEREO : 0)
548 			| (p->precision * p->factor == 16 ?
549 				AUVIA_RPMODE_16BIT : 0)
550 			| AUVIA_RPMODE_INTR_FLAG | AUVIA_RPMODE_INTR_EOL
551 			| AUVIA_RPMODE_AUTOSTART;
552 		ch->sc_reg = regval;
553 	} else if (ch->sc_base != VIA8233_MP_BASE) {
554 		v = CH_READ4(sc, ch, VIA8233_RP_RATEFMT);
555 		v &= ~(VIA8233_RATEFMT_48K | VIA8233_RATEFMT_STEREO
556 			| VIA8233_RATEFMT_16BIT);
557 
558 		v |= VIA8233_RATEFMT_48K * (p->sample_rate / 20)
559 			/ (48000 / 20);
560 		if (p->channels == 2)
561 			v |= VIA8233_RATEFMT_STEREO;
562 		if (p->precision == 16)
563 			v |= VIA8233_RATEFMT_16BIT;
564 
565 		CH_WRITE4(sc, ch, VIA8233_RP_RATEFMT, v);
566 	} else {
567 		static const u_int32_t slottab[7] =
568 			{ 0, 0xff000011, 0xff000021, 0,
569 			  0xff004321, 0, 0xff436521};
570 
571 		regval = (p->hw_precision == 16
572 			? VIA8233_MP_FORMAT_16BIT : VIA8233_MP_FORMAT_8BIT)
573 			| (p->hw_channels << 4);
574 		CH_WRITE1(sc, ch, VIA8233_OFF_MP_FORMAT, regval);
575 		CH_WRITE4(sc, ch, VIA8233_OFF_MP_STOP, slottab[p->hw_channels]);
576 	}
577 }
578 
579 int
580 auvia_set_params(void *addr, int setmode, int usemode,
581 	struct audio_params *play, struct audio_params *rec)
582 {
583 	struct auvia_softc *sc = addr;
584 	struct auvia_softc_chan *ch;
585 	struct audio_params *p;
586 	struct ac97_codec_if* codec;
587 	int reg, mode;
588 	u_int16_t ext_id;
589 
590 	codec = sc->codec_if;
591 	/* for mode in (RECORD, PLAY) */
592 	for (mode = AUMODE_RECORD; mode != -1;
593 	     mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
594 		if ((setmode & mode) == 0)
595 			continue;
596 
597 		if (mode == AUMODE_PLAY ) {
598 			p = play;
599 			ch = &sc->sc_play;
600 			reg = AC97_REG_PCM_FRONT_DAC_RATE;
601 		} else {
602 			p = rec;
603 			ch = &sc->sc_record;
604 			reg = AC97_REG_PCM_LR_ADC_RATE;
605 		}
606 
607 		if (ch->sc_base == VIA8233_MP_BASE && mode == AUMODE_PLAY) {
608 			ext_id = codec->vtbl->get_extcaps(codec);
609 			if (p->channels == 1) {
610 				/* ok */
611 			} else if (p->channels == 2) {
612 				/* ok */
613 			} else if (p->channels == 4
614 				&& ext_id & AC97_EXT_AUDIO_SDAC) {
615 				/* ok */
616 #define BITS_6CH	(AC97_EXT_AUDIO_SDAC | AC97_EXT_AUDIO_CDAC | AC97_EXT_AUDIO_LDAC)
617 			} else if (p->channels == 6
618 				&& (ext_id & BITS_6CH) == BITS_6CH) {
619 				/* ok */
620 			} else {
621 				return (EINVAL);
622 			}
623 		} else {
624 			if (p->channels != 1 && p->channels != 2)
625 				return (EINVAL);
626 		}
627 		if (p->sample_rate < 4000 || p->sample_rate > 48000 ||
628 		    (p->precision != 8 && p->precision != 16))
629 			return (EINVAL);
630 
631 		if (IS_FIXED_RATE(codec)) {
632 			/* Enable aurateconv */
633 			p->hw_sample_rate = AC97_SINGLE_RATE;
634 		} else {
635 			if (codec->vtbl->set_rate(codec, reg, &p->sample_rate))
636 				return (EINVAL);
637 			reg = AC97_REG_PCM_SURR_DAC_RATE;
638 			if (p->channels >= 4
639 			    && codec->vtbl->set_rate(codec, reg,
640 						     &p->sample_rate))
641 				return (EINVAL);
642 			reg = AC97_REG_PCM_LFE_DAC_RATE;
643 			if (p->channels == 6
644 			    && codec->vtbl->set_rate(codec, reg,
645 						     &p->sample_rate))
646 				return (EINVAL);
647 		}
648 
649 		p->factor = 1;
650 		p->sw_code = 0;
651 		switch (p->encoding) {
652 		case AUDIO_ENCODING_SLINEAR_BE:
653 			if (p->precision == 16) {
654 				p->sw_code = swap_bytes;
655 				p->hw_encoding = AUDIO_ENCODING_SLINEAR_LE;
656 			} else {
657 				p->sw_code = change_sign8;
658 				p->hw_encoding = AUDIO_ENCODING_ULINEAR;
659 			}
660 			break;
661 		case AUDIO_ENCODING_SLINEAR_LE:
662 			if (p->precision != 16) {
663 				p->sw_code = change_sign8;
664 				p->hw_encoding = AUDIO_ENCODING_ULINEAR;
665 			}
666 			break;
667 		case AUDIO_ENCODING_ULINEAR_BE:
668 			if (p->precision == 16) {
669 				if (mode == AUMODE_PLAY)
670 					p->sw_code = swap_bytes_change_sign16_le;
671 				else
672 					p->sw_code = change_sign16_swap_bytes_le;
673 				p->hw_encoding = AUDIO_ENCODING_SLINEAR_LE;
674 			}
675 			break;
676 		case AUDIO_ENCODING_ULINEAR_LE:
677 			if (p->precision == 16) {
678 				p->sw_code = change_sign16_le;
679 				p->hw_encoding = AUDIO_ENCODING_SLINEAR_LE;
680 			}
681 			break;
682 		case AUDIO_ENCODING_ULAW:
683 			if (p->precision != 8)
684 				return (EINVAL);
685 			if (mode == AUMODE_PLAY) {
686 				p->factor = 2;
687 				p->sw_code = mulaw_to_slinear16_le;
688 				p->hw_encoding = AUDIO_ENCODING_SLINEAR_LE;
689 				p->hw_precision = 16;
690 			} else if (!IS_FIXED_RATE(codec)) {
691 				p->sw_code = ulinear8_to_mulaw;
692 				p->hw_encoding = AUDIO_ENCODING_ULINEAR;
693 			} else {
694 				/* aurateconv supports no 8 bit PCM */
695 				p->factor = 2;
696 				p->sw_code = slinear16_to_mulaw_le;
697 				p->hw_encoding = AUDIO_ENCODING_SLINEAR_LE;
698 				p->hw_precision = 16;
699 			}
700 			break;
701 		case AUDIO_ENCODING_ALAW:
702 			if (p->precision != 8)
703 				return (EINVAL);
704 			if (mode == AUMODE_PLAY) {
705 				p->factor = 2;
706 				p->sw_code = alaw_to_slinear16_le;
707 				p->hw_encoding = AUDIO_ENCODING_SLINEAR_LE;
708 				p->hw_precision = 16;
709 			} else if (!IS_FIXED_RATE(codec)) {
710 				p->sw_code = ulinear8_to_alaw;
711 				p->hw_encoding = AUDIO_ENCODING_ULINEAR;
712 			} else {
713 				/* aurateconv supports no 8 bit PCM */
714 				p->factor = 2;
715 				p->sw_code = slinear16_to_alaw_le;
716 				p->hw_encoding = AUDIO_ENCODING_SLINEAR_LE;
717 				p->hw_precision = 16;
718 			}
719 			break;
720 		default:
721 			return (EINVAL);
722 		}
723 		auvia_set_params_sub(sc, ch, p);
724 	}
725 
726 	return 0;
727 }
728 
729 
730 int
731 auvia_round_blocksize(void *addr, int blk)
732 {
733 	return (blk & -32);
734 }
735 
736 
737 int
738 auvia_halt_output(void *addr)
739 {
740 	struct auvia_softc *sc = addr;
741 	struct auvia_softc_chan *ch = &(sc->sc_play);
742 
743 	CH_WRITE1(sc, ch, AUVIA_RP_CONTROL, AUVIA_RPCTRL_TERMINATE);
744 	return 0;
745 }
746 
747 
748 int
749 auvia_halt_input(void *addr)
750 {
751 	struct auvia_softc *sc = addr;
752 	struct auvia_softc_chan *ch = &(sc->sc_record);
753 
754 	CH_WRITE1(sc, ch, AUVIA_RP_CONTROL, AUVIA_RPCTRL_TERMINATE);
755 	return 0;
756 }
757 
758 
759 int
760 auvia_getdev(void *addr, struct audio_device *retp)
761 {
762 	struct auvia_softc *sc = addr;
763 
764 	if (retp) {
765 		if (sc->sc_flags & AUVIA_FLAGS_VT8233) {
766 			strncpy(retp->name, "VIA VT8233/8235",
767 				sizeof(retp->name));
768 		} else {
769 			strncpy(retp->name, "VIA VT82C686A",
770 				sizeof(retp->name));
771 		}
772 		strncpy(retp->version, sc->sc_revision, sizeof(retp->version));
773 		strncpy(retp->config, "auvia", sizeof(retp->config));
774 	}
775 
776 	return 0;
777 }
778 
779 
780 int
781 auvia_set_port(void *addr, mixer_ctrl_t *cp)
782 {
783 	struct auvia_softc *sc = addr;
784 
785 	return (sc->codec_if->vtbl->mixer_set_port(sc->codec_if, cp));
786 }
787 
788 
789 int
790 auvia_get_port(void *addr, mixer_ctrl_t *cp)
791 {
792 	struct auvia_softc *sc = addr;
793 
794 	return (sc->codec_if->vtbl->mixer_get_port(sc->codec_if, cp));
795 }
796 
797 
798 int
799 auvia_query_devinfo(void *addr, mixer_devinfo_t *dip)
800 {
801 	struct auvia_softc *sc = addr;
802 
803 	return (sc->codec_if->vtbl->query_devinfo(sc->codec_if, dip));
804 }
805 
806 
807 void *
808 auvia_malloc(void *addr, int direction, size_t size, struct malloc_type * pool,
809     int flags)
810 {
811 	struct auvia_softc *sc = addr;
812 	struct auvia_dma *p;
813 	int error;
814 	int rseg;
815 
816 	p = malloc(sizeof(*p), pool, flags);
817 	if (!p)
818 		return 0;
819 
820 	p->size = size;
821 	if ((error = bus_dmamem_alloc(sc->sc_dmat, size, PAGE_SIZE, 0, &p->seg,
822 				      1, &rseg, BUS_DMA_NOWAIT)) != 0) {
823 		printf("%s: unable to allocate DMA, error = %d\n",
824 		       sc->sc_dev.dv_xname, error);
825 		goto fail_alloc;
826 	}
827 
828 	if ((error = bus_dmamem_map(sc->sc_dmat, &p->seg, rseg, size, &p->addr,
829 				    BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
830 		printf("%s: unable to map DMA, error = %d\n",
831 		       sc->sc_dev.dv_xname, error);
832 		goto fail_map;
833 	}
834 
835 	if ((error = bus_dmamap_create(sc->sc_dmat, size, 1, size, 0,
836 				       BUS_DMA_NOWAIT, &p->map)) != 0) {
837 		printf("%s: unable to create DMA map, error = %d\n",
838 		       sc->sc_dev.dv_xname, error);
839 		goto fail_create;
840 	}
841 
842 	if ((error = bus_dmamap_load(sc->sc_dmat, p->map, p->addr, size, NULL,
843 				     BUS_DMA_NOWAIT)) != 0) {
844 		printf("%s: unable to load DMA map, error = %d\n",
845 		       sc->sc_dev.dv_xname, error);
846 		goto fail_load;
847 	}
848 
849 	p->next = sc->sc_dmas;
850 	sc->sc_dmas = p;
851 
852 	return p->addr;
853 
854 
855 fail_load:
856 	bus_dmamap_destroy(sc->sc_dmat, p->map);
857 fail_create:
858 	bus_dmamem_unmap(sc->sc_dmat, p->addr, size);
859 fail_map:
860 	bus_dmamem_free(sc->sc_dmat, &p->seg, 1);
861 fail_alloc:
862 	free(p, pool);
863 	return 0;
864 }
865 
866 
867 void
868 auvia_free(void *addr, void *ptr, struct malloc_type *pool)
869 {
870 	struct auvia_softc *sc = addr;
871 	struct auvia_dma **pp, *p;
872 
873 	for (pp = &(sc->sc_dmas); (p = *pp) != NULL; pp = &p->next)
874 		if (p->addr == ptr) {
875 			bus_dmamap_unload(sc->sc_dmat, p->map);
876 			bus_dmamap_destroy(sc->sc_dmat, p->map);
877 			bus_dmamem_unmap(sc->sc_dmat, p->addr, p->size);
878 			bus_dmamem_free(sc->sc_dmat, &p->seg, 1);
879 
880 			*pp = p->next;
881 			free(p, pool);
882 			return;
883 		}
884 
885 	panic("auvia_free: trying to free unallocated memory");
886 }
887 
888 
889 size_t
890 auvia_round_buffersize(void *addr, int direction, size_t size)
891 {
892 	return size;
893 }
894 
895 
896 paddr_t
897 auvia_mappage(void *addr, void *mem, off_t off, int prot)
898 {
899 	struct auvia_softc *sc = addr;
900 	struct auvia_dma *p;
901 
902 	if (off < 0)
903 		return -1;
904 
905 	for (p = sc->sc_dmas; p && p->addr != mem; p = p->next)
906 		;
907 
908 	if (!p)
909 		return -1;
910 
911 	return bus_dmamem_mmap(sc->sc_dmat, &p->seg, 1, off, prot,
912 	       BUS_DMA_WAITOK);
913 }
914 
915 
916 int
917 auvia_get_props(void *addr)
918 {
919 	struct auvia_softc *sc = addr;
920 	int props;
921 
922 	props = AUDIO_PROP_INDEPENDENT | AUDIO_PROP_FULLDUPLEX;
923 	/*
924 	 * Even if the codec is fixed-rate, set_param() succeeds for any sample
925 	 * rate because of aurateconv.  Applications can't know what rate the
926 	 * device can process in the case of mmap().
927 	 */
928 	if (!IS_FIXED_RATE(sc->codec_if))
929 		props |= AUDIO_PROP_MMAP;
930 	return props;
931 }
932 
933 
934 int
935 auvia_build_dma_ops(struct auvia_softc *sc, struct auvia_softc_chan *ch,
936 	struct auvia_dma *p, void *start, void *end, int blksize)
937 {
938 	struct auvia_dma_op *op;
939 	struct auvia_dma *dp;
940 	bus_addr_t s, e;
941 	size_t l;
942 	int segs;
943 
944 	s = p->map->dm_segs[0].ds_addr;
945 	l = ((char *)end - (char *)start);
946 	e = s + l;
947 	segs = (l + blksize - 1) / blksize;
948 
949 	if (segs > (ch->sc_dma_op_count)) {
950 		/* if old list was too small, free it */
951 		if (ch->sc_dma_ops) {
952 			auvia_free(sc, ch->sc_dma_ops, M_DEVBUF);
953 		}
954 
955 		ch->sc_dma_ops = auvia_malloc(sc, 0,
956 			sizeof(struct auvia_dma_op) * segs, M_DEVBUF, M_WAITOK);
957 
958 		if (ch->sc_dma_ops == NULL) {
959 			printf("%s: couldn't build dmaops\n", sc->sc_dev.dv_xname);
960 			return 1;
961 		}
962 
963 		for (dp = sc->sc_dmas;
964 			dp && dp->addr != (void *)(ch->sc_dma_ops);
965 			dp = dp->next)
966 				;
967 
968 		if (!dp)
969 			panic("%s: build_dma_ops: where'd my memory go??? "
970 				"address (%p)\n", sc->sc_dev.dv_xname,
971 				ch->sc_dma_ops);
972 
973 		ch->sc_dma_op_count = segs;
974 		ch->sc_dma_ops_dma = dp;
975 	}
976 
977 	dp = ch->sc_dma_ops_dma;
978 	op = ch->sc_dma_ops;
979 
980 	while (l) {
981 		op->ptr = s;
982 		l = l - blksize;
983 		if (!l) {
984 			/* if last block */
985 			op->flags = AUVIA_DMAOP_EOL | blksize;
986 		} else {
987 			op->flags = AUVIA_DMAOP_FLAG | blksize;
988 		}
989 		s += blksize;
990 		op++;
991 	}
992 
993 	return 0;
994 }
995 
996 
997 int
998 auvia_trigger_output(void *addr, void *start, void *end,
999 	int blksize, void (*intr)(void *), void *arg,
1000 	struct audio_params *param)
1001 {
1002 	struct auvia_softc *sc = addr;
1003 	struct auvia_softc_chan *ch = &(sc->sc_play);
1004 	struct auvia_dma *p;
1005 
1006 	for (p = sc->sc_dmas; p && p->addr != start; p = p->next)
1007 		;
1008 
1009 	if (!p)
1010 		panic("auvia_trigger_output: request with bad start "
1011 			"address (%p)", start);
1012 
1013 	if (auvia_build_dma_ops(sc, ch, p, start, end, blksize)) {
1014 		return 1;
1015 	}
1016 
1017 	ch->sc_intr = intr;
1018 	ch->sc_arg = arg;
1019 
1020 	CH_WRITE4(sc, ch, AUVIA_RP_DMAOPS_BASE,
1021 		ch->sc_dma_ops_dma->map->dm_segs[0].ds_addr);
1022 
1023 	if (sc->sc_flags & AUVIA_FLAGS_VT8233) {
1024 		if (ch->sc_base != VIA8233_MP_BASE) {
1025 			CH_WRITE1(sc, ch, VIA8233_RP_DXS_LVOL, 0);
1026 			CH_WRITE1(sc, ch, VIA8233_RP_DXS_RVOL, 0);
1027 		}
1028 		CH_WRITE1(sc, ch, AUVIA_RP_CONTROL,
1029 			AUVIA_RPCTRL_START | AUVIA_RPCTRL_AUTOSTART |
1030 			AUVIA_RPCTRL_STOP  | AUVIA_RPCTRL_EOL | AUVIA_RPCTRL_FLAG);
1031 	} else {
1032 		CH_WRITE1(sc, ch, AUVIA_RP_MODE, ch->sc_reg);
1033 		CH_WRITE1(sc, ch, AUVIA_RP_CONTROL, AUVIA_RPCTRL_START);
1034 	}
1035 
1036 	return 0;
1037 }
1038 
1039 
1040 int
1041 auvia_trigger_input(void *addr, void *start, void *end,
1042 	int blksize, void (*intr)(void *), void *arg,
1043 	struct audio_params *param)
1044 {
1045 	struct auvia_softc *sc = addr;
1046 	struct auvia_softc_chan *ch = &(sc->sc_record);
1047 	struct auvia_dma *p;
1048 
1049 	for (p = sc->sc_dmas; p && p->addr != start; p = p->next)
1050 		;
1051 
1052 	if (!p)
1053 		panic("auvia_trigger_input: request with bad start "
1054 			"address (%p)", start);
1055 
1056 	if (auvia_build_dma_ops(sc, ch, p, start, end, blksize)) {
1057 		return 1;
1058 	}
1059 
1060 	ch->sc_intr = intr;
1061 	ch->sc_arg = arg;
1062 
1063 	CH_WRITE4(sc, ch, AUVIA_RP_DMAOPS_BASE,
1064 		  ch->sc_dma_ops_dma->map->dm_segs[0].ds_addr);
1065 
1066 	if (sc->sc_flags & AUVIA_FLAGS_VT8233) {
1067 		CH_WRITE1(sc, ch, VIA8233_RP_DXS_LVOL, 0);
1068 		CH_WRITE1(sc, ch, VIA8233_RP_DXS_RVOL, 0);
1069 		CH_WRITE1(sc, ch, AUVIA_RP_CONTROL,
1070 			AUVIA_RPCTRL_START | AUVIA_RPCTRL_AUTOSTART |
1071 			AUVIA_RPCTRL_STOP  | AUVIA_RPCTRL_EOL | AUVIA_RPCTRL_FLAG);
1072 	} else {
1073 		CH_WRITE1(sc, ch, AUVIA_RP_MODE, ch->sc_reg);
1074 		CH_WRITE1(sc, ch, AUVIA_RP_CONTROL, AUVIA_RPCTRL_START);
1075 	}
1076 
1077 	return 0;
1078 }
1079 
1080 
1081 int
1082 auvia_intr(void *arg)
1083 {
1084 	struct auvia_softc *sc = arg;
1085 	struct auvia_softc_chan *ch;
1086 	u_int8_t r;
1087 	int rval;
1088 
1089 	rval = 0;
1090 
1091 	ch = &sc->sc_record;
1092 	r = CH_READ1(sc, ch, AUVIA_RP_STAT);
1093 	if (r & AUVIA_RPSTAT_INTR) {
1094 		if (sc->sc_record.sc_intr)
1095 			sc->sc_record.sc_intr(sc->sc_record.sc_arg);
1096 
1097 		/* clear interrupts */
1098 		CH_WRITE1(sc, ch, AUVIA_RP_STAT, AUVIA_RPSTAT_INTR);
1099 		rval = 1;
1100 	}
1101 
1102 	ch = &sc->sc_play;
1103 	r = CH_READ1(sc, ch, AUVIA_RP_STAT);
1104 	if (r & AUVIA_RPSTAT_INTR) {
1105 		if (sc->sc_play.sc_intr)
1106 			sc->sc_play.sc_intr(sc->sc_play.sc_arg);
1107 
1108 		/* clear interrupts */
1109 		CH_WRITE1(sc, ch, AUVIA_RP_STAT, AUVIA_RPSTAT_INTR);
1110 		rval = 1;
1111 	}
1112 
1113 	return rval;
1114 }
1115