xref: /netbsd-src/sys/dev/pci/auvia.c (revision aaf4ece63a859a04e37cf3a7229b5fab0157cc06)
1 /*	$NetBSD: auvia.c,v 1.53 2005/11/28 19:00:49 rpaulo 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.53 2005/11/28 19:00:49 rpaulo 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 	uint32_t ptr;
82 	uint32_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 static int	auvia_match(struct device *, struct cfdata *, void *);
90 static void	auvia_attach(struct device *, struct device *, void *);
91 static int	auvia_query_encoding(void *, struct audio_encoding *);
92 static void	auvia_set_params_sub(struct auvia_softc *,
93 				     struct auvia_softc_chan *,
94 				     const audio_params_t *);
95 static int	auvia_set_params(void *, int, int, audio_params_t *,
96 				 audio_params_t *, stream_filter_list_t *,
97 				 stream_filter_list_t *);
98 static int	auvia_round_blocksize(void *, int, int, const audio_params_t *);
99 static int	auvia_halt_output(void *);
100 static int	auvia_halt_input(void *);
101 static int	auvia_getdev(void *, struct audio_device *);
102 static int	auvia_set_port(void *, mixer_ctrl_t *);
103 static int	auvia_get_port(void *, mixer_ctrl_t *);
104 static int	auvia_query_devinfo(void *, mixer_devinfo_t *);
105 static void *	auvia_malloc(void *, int, size_t, struct malloc_type *, int);
106 static void	auvia_free(void *, void *, struct malloc_type *);
107 static size_t	auvia_round_buffersize(void *, int, size_t);
108 static paddr_t	auvia_mappage(void *, void *, off_t, int);
109 static int	auvia_get_props(void *);
110 static int	auvia_build_dma_ops(struct auvia_softc *,
111 				    struct auvia_softc_chan *,
112 				    struct auvia_dma *, void *, void *, int);
113 static int	auvia_trigger_output(void *, void *, void *, int,
114 				     void (*)(void *), void *,
115 				     const audio_params_t *);
116 static int	auvia_trigger_input(void *, void *, void *, int,
117 				    void (*)(void *), void *,
118 				    const audio_params_t *);
119 static void	auvia_powerhook(int, void *);
120 static int	auvia_intr(void *);
121 
122 CFATTACH_DECL(auvia, sizeof (struct auvia_softc),
123     auvia_match, auvia_attach, NULL, NULL);
124 
125 /* VIA VT823xx revision number */
126 #define VIA_REV_8233PRE	0x10
127 #define VIA_REV_8233C	0x20
128 #define VIA_REV_8233	0x30
129 #define VIA_REV_8233A	0x40
130 #define VIA_REV_8235	0x50
131 #define VIA_REV_8237	0x60
132 
133 #define AUVIA_PCICONF_JUNK	0x40
134 #define		AUVIA_PCICONF_ENABLES	 0x00FF0000	/* reg 42 mask */
135 #define		AUVIA_PCICONF_ACLINKENAB 0x00008000	/* ac link enab */
136 #define		AUVIA_PCICONF_ACNOTRST	 0x00004000	/* ~(ac reset) */
137 #define		AUVIA_PCICONF_ACSYNC	 0x00002000	/* ac sync */
138 #define		AUVIA_PCICONF_ACVSR	 0x00000800	/* var. samp. rate */
139 #define		AUVIA_PCICONF_ACSGD	 0x00000400	/* SGD enab */
140 #define		AUVIA_PCICONF_ACFM	 0x00000200	/* FM enab */
141 #define		AUVIA_PCICONF_ACSB	 0x00000100	/* SB enab */
142 #define		AUVIA_PCICONF_PRIVALID	 0x00000001	/* primary codec rdy */
143 
144 #define	AUVIA_PLAY_BASE			0x00
145 #define	AUVIA_RECORD_BASE		0x10
146 
147 /* *_RP_* are offsets from AUVIA_PLAY_BASE or AUVIA_RECORD_BASE */
148 #define	AUVIA_RP_STAT			0x00
149 #define		AUVIA_RPSTAT_INTR		0x03
150 #define	AUVIA_RP_CONTROL		0x01
151 #define		AUVIA_RPCTRL_START		0x80
152 #define		AUVIA_RPCTRL_TERMINATE		0x40
153 #define		AUVIA_RPCTRL_AUTOSTART		0x20
154 /* The following are 8233 specific */
155 #define		AUVIA_RPCTRL_STOP		0x04
156 #define		AUVIA_RPCTRL_EOL		0x02
157 #define		AUVIA_RPCTRL_FLAG		0x01
158 #define	AUVIA_RP_MODE			0x02		/* 82c686 specific */
159 #define		AUVIA_RPMODE_INTR_FLAG		0x01
160 #define		AUVIA_RPMODE_INTR_EOL		0x02
161 #define		AUVIA_RPMODE_STEREO		0x10
162 #define		AUVIA_RPMODE_16BIT		0x20
163 #define		AUVIA_RPMODE_AUTOSTART		0x80
164 #define	AUVIA_RP_DMAOPS_BASE		0x04
165 
166 #define	VIA8233_RP_DXS_LVOL		0x02
167 #define	VIA8233_RP_DXS_RVOL		0x03
168 #define	VIA8233_RP_RATEFMT		0x08
169 #define		VIA8233_RATEFMT_48K		0xfffff
170 #define		VIA8233_RATEFMT_STEREO		0x00100000
171 #define		VIA8233_RATEFMT_16BIT		0x00200000
172 
173 #define	VIA_RP_DMAOPS_COUNT		0x0c
174 
175 #define VIA8233_MP_BASE			0x40
176 	/* STAT, CONTROL, DMAOPS_BASE, DMAOPS_COUNT are valid */
177 #define VIA8233_OFF_MP_FORMAT		0x02
178 #define		VIA8233_MP_FORMAT_8BIT		0x00
179 #define		VIA8233_MP_FORMAT_16BIT		0x80
180 #define		VIA8233_MP_FORMAT_CHANNLE_MASK	0x70 /* 1, 2, 4, 6 */
181 #define VIA8233_OFF_MP_SCRATCH		0x03
182 #define VIA8233_OFF_MP_STOP		0x08
183 
184 #define	AUVIA_CODEC_CTL			0x80
185 #define		AUVIA_CODEC_READ		0x00800000
186 #define		AUVIA_CODEC_BUSY		0x01000000
187 #define		AUVIA_CODEC_PRIVALID		0x02000000
188 #define		AUVIA_CODEC_INDEX(x)		((x)<<16)
189 
190 #define CH_WRITE1(sc, ch, off, v)	\
191 	bus_space_write_1((sc)->sc_iot,	(sc)->sc_ioh, (ch)->sc_base + (off), v)
192 #define CH_WRITE4(sc, ch, off, v)	\
193 	bus_space_write_4((sc)->sc_iot,	(sc)->sc_ioh, (ch)->sc_base + (off), v)
194 #define CH_READ1(sc, ch, off)		\
195 	bus_space_read_1((sc)->sc_iot, (sc)->sc_ioh, (ch)->sc_base + (off))
196 #define CH_READ4(sc, ch, off)		\
197 	bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (ch)->sc_base + (off))
198 
199 #define TIMEOUT	50
200 
201 static const struct audio_hw_if auvia_hw_if = {
202 	NULL, /* open */
203 	NULL, /* close */
204 	NULL, /* drain */
205 	auvia_query_encoding,
206 	auvia_set_params,
207 	auvia_round_blocksize,
208 	NULL, /* commit_settings */
209 	NULL, /* init_output */
210 	NULL, /* init_input */
211 	NULL, /* start_output */
212 	NULL, /* start_input */
213 	auvia_halt_output,
214 	auvia_halt_input,
215 	NULL, /* speaker_ctl */
216 	auvia_getdev,
217 	NULL, /* setfd */
218 	auvia_set_port,
219 	auvia_get_port,
220 	auvia_query_devinfo,
221 	auvia_malloc,
222 	auvia_free,
223 	auvia_round_buffersize,
224 	auvia_mappage,
225 	auvia_get_props,
226 	auvia_trigger_output,
227 	auvia_trigger_input,
228 	NULL, /* dev_ioctl */
229 };
230 
231 #define AUVIA_FORMATS_4CH_16	2
232 #define AUVIA_FORMATS_6CH_16	3
233 #define AUVIA_FORMATS_4CH_8	6
234 #define AUVIA_FORMATS_6CH_8	7
235 static const struct audio_format auvia_formats[AUVIA_NFORMATS] = {
236 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
237 	 1, AUFMT_MONAURAL, 0, {8000, 48000}},
238 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
239 	 2, AUFMT_STEREO, 0, {8000, 48000}},
240 	{NULL, AUMODE_PLAY, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
241 	 4, AUFMT_SURROUND4, 0, {8000, 48000}},
242 	{NULL, AUMODE_PLAY, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
243 	 6, AUFMT_DOLBY_5_1, 0, {8000, 48000}},
244 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_LE, 8, 8,
245 	 1, AUFMT_MONAURAL, 0, {8000, 48000}},
246 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_LE, 8, 8,
247 	 2, AUFMT_STEREO, 0, {8000, 48000}},
248 	{NULL, AUMODE_PLAY, AUDIO_ENCODING_ULINEAR_LE, 8, 8,
249 	 4, AUFMT_SURROUND4, 0, {8000, 48000}},
250 	{NULL, AUMODE_PLAY, AUDIO_ENCODING_SLINEAR_LE, 8, 8,
251 	 6, AUFMT_DOLBY_5_1, 0, {8000, 48000}},
252 };
253 
254 static int	auvia_attach_codec(void *, struct ac97_codec_if *);
255 static int	auvia_write_codec(void *, uint8_t, uint16_t);
256 static int	auvia_read_codec(void *, uint8_t, uint16_t *);
257 static int	auvia_reset_codec(void *);
258 static int	auvia_waitready_codec(struct auvia_softc *sc);
259 static int	auvia_waitvalid_codec(struct auvia_softc *sc);
260 
261 
262 static int
263 auvia_match(struct device *parent, struct cfdata *match, void *aux)
264 {
265 	struct pci_attach_args *pa;
266 
267 	pa = (struct pci_attach_args *) aux;
268 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_VIATECH)
269 		return 0;
270 	switch (PCI_PRODUCT(pa->pa_id)) {
271 	case PCI_PRODUCT_VIATECH_VT82C686A_AC97:
272 	case PCI_PRODUCT_VIATECH_VT8233_AC97:
273 		break;
274 	default:
275 		return 0;
276 	}
277 
278 	return 1;
279 }
280 
281 static void
282 auvia_attach(struct device *parent, struct device *self, void *aux)
283 {
284 	struct pci_attach_args *pa;
285 	struct auvia_softc *sc;
286 	const char *intrstr;
287 	pci_chipset_tag_t pc;
288 	pcitag_t pt;
289 	pci_intr_handle_t ih;
290 	bus_size_t iosize;
291 	pcireg_t pr;
292 	int r;
293 	const char *revnum;	/* VT823xx revision number */
294 
295 	pa = aux;
296 	sc = (struct auvia_softc *)self;
297 	intrstr = NULL;
298 	pc = pa->pa_pc;
299 	pt = pa->pa_tag;
300 	revnum = NULL;
301 
302 	aprint_naive(": Audio controller\n");
303 
304 	sc->sc_play.sc_base = AUVIA_PLAY_BASE;
305 	sc->sc_record.sc_base = AUVIA_RECORD_BASE;
306 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_VIATECH_VT8233_AC97) {
307 		sc->sc_flags |= AUVIA_FLAGS_VT8233;
308 		sc->sc_play.sc_base = VIA8233_MP_BASE;
309 	}
310 
311 	if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_IO, 0, &sc->sc_iot,
312 		&sc->sc_ioh, NULL, &iosize)) {
313 		aprint_error(": can't map i/o space\n");
314 		return;
315 	}
316 
317 	sc->sc_dmat = pa->pa_dmat;
318 	sc->sc_pc = pc;
319 	sc->sc_pt = pt;
320 
321 	r = PCI_REVISION(pa->pa_class);
322 	if (sc->sc_flags & AUVIA_FLAGS_VT8233) {
323 		snprintf(sc->sc_revision, sizeof(sc->sc_revision), "0x%02X", r);
324 		switch(r) {
325 		case VIA_REV_8233PRE:
326 			/* same as 8233, but should not be in the market */
327 			revnum = "3-Pre";
328 			break;
329 		case VIA_REV_8233C:
330 			/* 2 rec, 4 pb, 1 multi-pb */
331 			revnum = "3C";
332 			break;
333 		case VIA_REV_8233:
334 			/* 2 rec, 4 pb, 1 multi-pb, spdif */
335 			revnum = "3";
336 			break;
337 		case VIA_REV_8233A:
338 			/* 1 rec, 1 multi-pb, spdif */
339 			revnum = "3A";
340 			break;
341 		default:
342 			break;
343 		}
344 		if (r >= VIA_REV_8237)
345 			revnum = "7";
346 		else if (r >= VIA_REV_8235) /* 2 rec, 4 pb, 1 multi-pb, spdif */
347 			revnum = "5";
348 		aprint_normal(": VIA Technologies VT823%s AC'97 Audio "
349 		    "(rev %s)\n", revnum, sc->sc_revision);
350 	} else {
351 		sc->sc_revision[1] = '\0';
352 		if (r == 0x20) {
353 			sc->sc_revision[0] = 'H';
354 		} else if ((r >= 0x10) && (r <= 0x14)) {
355 			sc->sc_revision[0] = 'A' + (r - 0x10);
356 		} else {
357 			snprintf(sc->sc_revision, sizeof(sc->sc_revision),
358 			    "0x%02X", r);
359 		}
360 
361 		aprint_normal(": VIA Technologies VT82C686A AC'97 Audio "
362 		    "(rev %s)\n", sc->sc_revision);
363 	}
364 
365 	if (pci_intr_map(pa, &ih)) {
366 		aprint_error(": couldn't map interrupt\n");
367 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, iosize);
368 		return;
369 	}
370 	intrstr = pci_intr_string(pc, ih);
371 
372 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO, auvia_intr, sc);
373 	if (sc->sc_ih == NULL) {
374 		aprint_error("%s: couldn't establish interrupt",
375 		    sc->sc_dev.dv_xname);
376 		if (intrstr != NULL)
377 			aprint_normal(" at %s", intrstr);
378 		aprint_normal("\n");
379 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, iosize);
380 		return;
381 	}
382 
383 	aprint_normal("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
384 
385 	/* disable SBPro compat & others */
386 	pr = pci_conf_read(pc, pt, AUVIA_PCICONF_JUNK);
387 
388 	pr &= ~AUVIA_PCICONF_ENABLES; /* clear compat function enables */
389 	/* XXX what to do about MIDI, FM, joystick? */
390 
391 	pr |= (AUVIA_PCICONF_ACLINKENAB | AUVIA_PCICONF_ACNOTRST
392 		| AUVIA_PCICONF_ACVSR | AUVIA_PCICONF_ACSGD);
393 
394 	pr &= ~(AUVIA_PCICONF_ACFM | AUVIA_PCICONF_ACSB);
395 
396 	pci_conf_write(pc, pt, AUVIA_PCICONF_JUNK, pr);
397 
398 	sc->host_if.arg = sc;
399 	sc->host_if.attach = auvia_attach_codec;
400 	sc->host_if.read = auvia_read_codec;
401 	sc->host_if.write = auvia_write_codec;
402 	sc->host_if.reset = auvia_reset_codec;
403 
404 	if ((r = ac97_attach(&sc->host_if, self)) != 0) {
405 		aprint_error("%s: can't attach codec (error 0x%X)\n",
406 			sc->sc_dev.dv_xname, r);
407 		pci_intr_disestablish(pc, sc->sc_ih);
408 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, iosize);
409 		return;
410 	}
411 
412 	/* setup audio_format */
413 	memcpy(sc->sc_formats, auvia_formats, sizeof(auvia_formats));
414 	if (sc->sc_play.sc_base != VIA8233_MP_BASE || !AC97_IS_4CH(sc->codec_if)) {
415 		AUFMT_INVALIDATE(&sc->sc_formats[AUVIA_FORMATS_4CH_8]);
416 		AUFMT_INVALIDATE(&sc->sc_formats[AUVIA_FORMATS_4CH_16]);
417 	}
418 	if (sc->sc_play.sc_base != VIA8233_MP_BASE || !AC97_IS_6CH(sc->codec_if)) {
419 		AUFMT_INVALIDATE(&sc->sc_formats[AUVIA_FORMATS_6CH_8]);
420 		AUFMT_INVALIDATE(&sc->sc_formats[AUVIA_FORMATS_6CH_16]);
421 	}
422 	if (AC97_IS_FIXED_RATE(sc->codec_if)) {
423 		for (r = 0; r < AUVIA_NFORMATS; r++) {
424 			sc->sc_formats[r].frequency_type = 1;
425 			sc->sc_formats[r].frequency[0] = 48000;
426 		}
427 	}
428 
429 	if (0 != auconv_create_encodings(sc->sc_formats, AUVIA_NFORMATS,
430 					 &sc->sc_encodings)) {
431 		sc->codec_if->vtbl->detach(sc->codec_if);
432 		pci_intr_disestablish(pc, sc->sc_ih);
433 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, iosize);
434 		return;
435 	}
436 
437 	/* Watch for power change */
438 	sc->sc_suspend = PWR_RESUME;
439 	sc->sc_powerhook = powerhook_establish(auvia_powerhook, sc);
440 
441 	audio_attach_mi(&auvia_hw_if, sc, &sc->sc_dev);
442 	return;
443 }
444 
445 static int
446 auvia_attach_codec(void *addr, struct ac97_codec_if *cif)
447 {
448 	struct auvia_softc *sc;
449 
450 	sc = addr;
451 	sc->codec_if = cif;
452 	return 0;
453 }
454 
455 static int
456 auvia_reset_codec(void *addr)
457 {
458 	struct auvia_softc *sc;
459 	pcireg_t r;
460 	int i;
461 
462 	/* perform a codec cold reset */
463 	sc = addr;
464 	r = pci_conf_read(sc->sc_pc, sc->sc_pt, AUVIA_PCICONF_JUNK);
465 
466 	r &= ~AUVIA_PCICONF_ACNOTRST;	/* enable RESET (active low) */
467 	pci_conf_write(sc->sc_pc, sc->sc_pt, AUVIA_PCICONF_JUNK, r);
468 	delay(2);
469 
470 	r |= AUVIA_PCICONF_ACNOTRST;	/* disable RESET (inactive high) */
471 	pci_conf_write(sc->sc_pc, sc->sc_pt, AUVIA_PCICONF_JUNK, r);
472 	delay(200);
473 
474 	for (i = 500000; i != 0 && !(pci_conf_read(sc->sc_pc, sc->sc_pt,
475 		AUVIA_PCICONF_JUNK) & AUVIA_PCICONF_PRIVALID); i--)
476 		DELAY(1);
477 	if (i == 0) {
478 		printf("%s: codec reset timed out\n", sc->sc_dev.dv_xname);
479 		return ETIMEDOUT;
480 	}
481 	return 0;
482 }
483 
484 static int
485 auvia_waitready_codec(struct auvia_softc *sc)
486 {
487 	int i;
488 
489 	/* poll until codec not busy */
490 	for (i = 0; (i < TIMEOUT) && (bus_space_read_4(sc->sc_iot, sc->sc_ioh,
491 		AUVIA_CODEC_CTL) & AUVIA_CODEC_BUSY); i++)
492 		delay(1);
493 	if (i >= TIMEOUT) {
494 		printf("%s: codec busy\n", sc->sc_dev.dv_xname);
495 		return 1;
496 	}
497 
498 	return 0;
499 }
500 
501 static int
502 auvia_waitvalid_codec(struct auvia_softc *sc)
503 {
504 	int i;
505 
506 	/* poll until codec valid */
507 	for (i = 0; (i < TIMEOUT) && !(bus_space_read_4(sc->sc_iot, sc->sc_ioh,
508 		AUVIA_CODEC_CTL) & AUVIA_CODEC_PRIVALID); i++)
509 			delay(1);
510 	if (i >= TIMEOUT) {
511 		printf("%s: codec invalid\n", sc->sc_dev.dv_xname);
512 		return 1;
513 	}
514 
515 	return 0;
516 }
517 
518 static int
519 auvia_write_codec(void *addr, u_int8_t reg, u_int16_t val)
520 {
521 	struct auvia_softc *sc;
522 
523 	sc = addr;
524 	if (auvia_waitready_codec(sc))
525 		return 1;
526 
527 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, AUVIA_CODEC_CTL,
528 		AUVIA_CODEC_PRIVALID | AUVIA_CODEC_INDEX(reg) | val);
529 
530 	return 0;
531 }
532 
533 static int
534 auvia_read_codec(void *addr, u_int8_t reg, u_int16_t *val)
535 {
536 	struct auvia_softc *sc;
537 
538 	sc = addr;
539 	if (auvia_waitready_codec(sc))
540 		return 1;
541 
542 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, AUVIA_CODEC_CTL,
543 		AUVIA_CODEC_PRIVALID | AUVIA_CODEC_READ | AUVIA_CODEC_INDEX(reg));
544 
545 	if (auvia_waitready_codec(sc))
546 		return 1;
547 
548 	if (auvia_waitvalid_codec(sc))
549 		return 1;
550 
551 	*val = bus_space_read_2(sc->sc_iot, sc->sc_ioh, AUVIA_CODEC_CTL);
552 
553 	return 0;
554 }
555 
556 static int
557 auvia_query_encoding(void *addr, struct audio_encoding *fp)
558 {
559 	struct auvia_softc *sc;
560 
561 	sc = (struct auvia_softc *)addr;
562 	return auconv_query_encoding(sc->sc_encodings, fp);
563 }
564 
565 static void
566 auvia_set_params_sub(struct auvia_softc *sc, struct auvia_softc_chan *ch,
567 		     const audio_params_t *p)
568 {
569 	uint32_t v;
570 	uint16_t regval;
571 
572 	if (!(sc->sc_flags & AUVIA_FLAGS_VT8233)) {
573 		regval = (p->channels == 2 ? AUVIA_RPMODE_STEREO : 0)
574 			| (p->precision  == 16 ?
575 				AUVIA_RPMODE_16BIT : 0)
576 			| AUVIA_RPMODE_INTR_FLAG | AUVIA_RPMODE_INTR_EOL
577 			| AUVIA_RPMODE_AUTOSTART;
578 		ch->sc_reg = regval;
579 	} else if (ch->sc_base != VIA8233_MP_BASE) {
580 		v = CH_READ4(sc, ch, VIA8233_RP_RATEFMT);
581 		v &= ~(VIA8233_RATEFMT_48K | VIA8233_RATEFMT_STEREO
582 			| VIA8233_RATEFMT_16BIT);
583 
584 		v |= VIA8233_RATEFMT_48K * (p->sample_rate / 20)
585 			/ (48000 / 20);
586 		if (p->channels == 2)
587 			v |= VIA8233_RATEFMT_STEREO;
588 		if (p->precision == 16)
589 			v |= VIA8233_RATEFMT_16BIT;
590 
591 		CH_WRITE4(sc, ch, VIA8233_RP_RATEFMT, v);
592 	} else {
593 		static const u_int32_t slottab[7] =
594 			{ 0, 0xff000011, 0xff000021, 0,
595 			  0xff004321, 0, 0xff436521};
596 
597 		regval = (p->precision == 16
598 			? VIA8233_MP_FORMAT_16BIT : VIA8233_MP_FORMAT_8BIT)
599 			| (p->channels << 4);
600 		CH_WRITE1(sc, ch, VIA8233_OFF_MP_FORMAT, regval);
601 		CH_WRITE4(sc, ch, VIA8233_OFF_MP_STOP, slottab[p->channels]);
602 	}
603 }
604 
605 static int
606 auvia_set_params(void *addr, int setmode, int usemode,
607 		 audio_params_t *play, audio_params_t *rec,
608 		 stream_filter_list_t *pfil, stream_filter_list_t *rfil)
609 {
610 	struct auvia_softc *sc;
611 	struct auvia_softc_chan *ch;
612 	struct audio_params *p;
613 	struct ac97_codec_if* codec;
614 	stream_filter_list_t *fil;
615 	int reg, mode;
616 	int index;
617 
618 	sc = addr;
619 	codec = sc->codec_if;
620 	/* for mode in (RECORD, PLAY) */
621 	for (mode = AUMODE_RECORD; mode != -1;
622 	     mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
623 		if ((setmode & mode) == 0)
624 			continue;
625 
626 		if (mode == AUMODE_PLAY ) {
627 			p = play;
628 			ch = &sc->sc_play;
629 			reg = AC97_REG_PCM_FRONT_DAC_RATE;
630 			fil = pfil;
631 		} else {
632 			p = rec;
633 			ch = &sc->sc_record;
634 			reg = AC97_REG_PCM_LR_ADC_RATE;
635 			fil = rfil;
636 		}
637 
638 		if (p->sample_rate < 4000 || p->sample_rate > 48000 ||
639 		    (p->precision != 8 && p->precision != 16))
640 			return (EINVAL);
641 		index = auconv_set_converter(sc->sc_formats, AUVIA_NFORMATS,
642 					     mode, p, TRUE, fil);
643 		if (index < 0)
644 			return EINVAL;
645 		if (fil->req_size > 0)
646 			p = &fil->filters[0].param;
647 		if (!AC97_IS_FIXED_RATE(codec)) {
648 			if (codec->vtbl->set_rate(codec, reg, &p->sample_rate))
649 				return EINVAL;
650 			reg = AC97_REG_PCM_SURR_DAC_RATE;
651 			if (p->channels >= 4
652 			    && codec->vtbl->set_rate(codec, reg,
653 						     &p->sample_rate))
654 				return EINVAL;
655 			reg = AC97_REG_PCM_LFE_DAC_RATE;
656 			if (p->channels == 6
657 			    && codec->vtbl->set_rate(codec, reg,
658 						     &p->sample_rate))
659 				return EINVAL;
660 		}
661 		auvia_set_params_sub(sc, ch, p);
662 	}
663 
664 	return 0;
665 }
666 
667 static int
668 auvia_round_blocksize(void *addr, int blk,
669 		      int mode, const audio_params_t *param)
670 {
671 	struct auvia_softc *sc;
672 
673 	sc = addr;
674 	/* XXX VT823x might have the limitation of dma_ops size */
675 	if (sc->sc_flags & AUVIA_FLAGS_VT8233 && blk < 288)
676 		blk = 288;
677 
678 	return (blk & -32);
679 }
680 
681 static int
682 auvia_halt_output(void *addr)
683 {
684 	struct auvia_softc *sc;
685 	struct auvia_softc_chan *ch;
686 
687 	sc = addr;
688 	ch = &(sc->sc_play);
689 	CH_WRITE1(sc, ch, AUVIA_RP_CONTROL, AUVIA_RPCTRL_TERMINATE);
690 	ch->sc_intr = NULL;
691 	return 0;
692 }
693 
694 static int
695 auvia_halt_input(void *addr)
696 {
697 	struct auvia_softc *sc;
698 	struct auvia_softc_chan *ch;
699 
700 	sc = addr;
701 	ch = &(sc->sc_record);
702 	CH_WRITE1(sc, ch, AUVIA_RP_CONTROL, AUVIA_RPCTRL_TERMINATE);
703 	ch->sc_intr = NULL;
704 	return 0;
705 }
706 
707 static int
708 auvia_getdev(void *addr, struct audio_device *retp)
709 {
710 	struct auvia_softc *sc;
711 
712 	if (retp) {
713 		sc = addr;
714 		if (sc->sc_flags & AUVIA_FLAGS_VT8233) {
715 			strncpy(retp->name, "VIA VT823x",
716 				sizeof(retp->name));
717 		} else {
718 			strncpy(retp->name, "VIA VT82C686A",
719 				sizeof(retp->name));
720 		}
721 		strncpy(retp->version, sc->sc_revision, sizeof(retp->version));
722 		strncpy(retp->config, "auvia", sizeof(retp->config));
723 	}
724 
725 	return 0;
726 }
727 
728 static int
729 auvia_set_port(void *addr, mixer_ctrl_t *cp)
730 {
731 	struct auvia_softc *sc;
732 
733 	sc = addr;
734 	return sc->codec_if->vtbl->mixer_set_port(sc->codec_if, cp);
735 }
736 
737 static int
738 auvia_get_port(void *addr, mixer_ctrl_t *cp)
739 {
740 	struct auvia_softc *sc;
741 
742 	sc = addr;
743 	return sc->codec_if->vtbl->mixer_get_port(sc->codec_if, cp);
744 }
745 
746 static int
747 auvia_query_devinfo(void *addr, mixer_devinfo_t *dip)
748 {
749 	struct auvia_softc *sc;
750 
751 	sc = addr;
752 	return sc->codec_if->vtbl->query_devinfo(sc->codec_if, dip);
753 }
754 
755 static void *
756 auvia_malloc(void *addr, int direction, size_t size, struct malloc_type * pool,
757     int flags)
758 {
759 	struct auvia_softc *sc;
760 	struct auvia_dma *p;
761 	int error;
762 	int rseg;
763 
764 	p = malloc(sizeof(*p), pool, flags);
765 	if (!p)
766 		return 0;
767 	sc = addr;
768 	p->size = size;
769 	if ((error = bus_dmamem_alloc(sc->sc_dmat, size, PAGE_SIZE, 0, &p->seg,
770 				      1, &rseg, BUS_DMA_NOWAIT)) != 0) {
771 		printf("%s: unable to allocate DMA, error = %d\n",
772 		       sc->sc_dev.dv_xname, error);
773 		goto fail_alloc;
774 	}
775 
776 	if ((error = bus_dmamem_map(sc->sc_dmat, &p->seg, rseg, size, &p->addr,
777 				    BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
778 		printf("%s: unable to map DMA, error = %d\n",
779 		       sc->sc_dev.dv_xname, error);
780 		goto fail_map;
781 	}
782 
783 	if ((error = bus_dmamap_create(sc->sc_dmat, size, 1, size, 0,
784 				       BUS_DMA_NOWAIT, &p->map)) != 0) {
785 		printf("%s: unable to create DMA map, error = %d\n",
786 		       sc->sc_dev.dv_xname, error);
787 		goto fail_create;
788 	}
789 
790 	if ((error = bus_dmamap_load(sc->sc_dmat, p->map, p->addr, size, NULL,
791 				     BUS_DMA_NOWAIT)) != 0) {
792 		printf("%s: unable to load DMA map, error = %d\n",
793 		       sc->sc_dev.dv_xname, error);
794 		goto fail_load;
795 	}
796 
797 	p->next = sc->sc_dmas;
798 	sc->sc_dmas = p;
799 
800 	return p->addr;
801 
802 
803 fail_load:
804 	bus_dmamap_destroy(sc->sc_dmat, p->map);
805 fail_create:
806 	bus_dmamem_unmap(sc->sc_dmat, p->addr, size);
807 fail_map:
808 	bus_dmamem_free(sc->sc_dmat, &p->seg, 1);
809 fail_alloc:
810 	free(p, pool);
811 	return NULL;
812 }
813 
814 static void
815 auvia_free(void *addr, void *ptr, struct malloc_type *pool)
816 {
817 	struct auvia_softc *sc;
818 	struct auvia_dma **pp, *p;
819 
820 	sc = addr;
821 	for (pp = &(sc->sc_dmas); (p = *pp) != NULL; pp = &p->next)
822 		if (p->addr == ptr) {
823 			bus_dmamap_unload(sc->sc_dmat, p->map);
824 			bus_dmamap_destroy(sc->sc_dmat, p->map);
825 			bus_dmamem_unmap(sc->sc_dmat, p->addr, p->size);
826 			bus_dmamem_free(sc->sc_dmat, &p->seg, 1);
827 
828 			*pp = p->next;
829 			free(p, pool);
830 			return;
831 		}
832 
833 	panic("auvia_free: trying to free unallocated memory");
834 }
835 
836 static size_t
837 auvia_round_buffersize(void *addr, int direction, size_t size)
838 {
839 
840 	return size;
841 }
842 
843 static paddr_t
844 auvia_mappage(void *addr, void *mem, off_t off, int prot)
845 {
846 	struct auvia_softc *sc;
847 	struct auvia_dma *p;
848 
849 	if (off < 0)
850 		return -1;
851 	sc = addr;
852 	for (p = sc->sc_dmas; p && p->addr != mem; p = p->next)
853 		continue;
854 
855 	if (!p)
856 		return -1;
857 
858 	return bus_dmamem_mmap(sc->sc_dmat, &p->seg, 1, off, prot,
859 	    BUS_DMA_WAITOK);
860 }
861 
862 static int
863 auvia_get_props(void *addr)
864 {
865 	struct auvia_softc *sc;
866 	int props;
867 
868 	props = AUDIO_PROP_INDEPENDENT | AUDIO_PROP_FULLDUPLEX;
869 	sc = addr;
870 	/*
871 	 * Even if the codec is fixed-rate, set_param() succeeds for any sample
872 	 * rate because of aurateconv.  Applications can't know what rate the
873 	 * device can process in the case of mmap().
874 	 */
875 	if (!AC97_IS_FIXED_RATE(sc->codec_if))
876 		props |= AUDIO_PROP_MMAP;
877 	return props;
878 }
879 
880 static int
881 auvia_build_dma_ops(struct auvia_softc *sc, struct auvia_softc_chan *ch,
882 	struct auvia_dma *p, void *start, void *end, int blksize)
883 {
884 	struct auvia_dma_op *op;
885 	struct auvia_dma *dp;
886 	bus_addr_t s;
887 	size_t l;
888 	int segs;
889 
890 	s = p->map->dm_segs[0].ds_addr;
891 	l = ((char *)end - (char *)start);
892 	segs = (l + blksize - 1) / blksize;
893 
894 	if (segs > (ch->sc_dma_op_count)) {
895 		/* if old list was too small, free it */
896 		if (ch->sc_dma_ops) {
897 			auvia_free(sc, ch->sc_dma_ops, M_DEVBUF);
898 		}
899 
900 		ch->sc_dma_ops = auvia_malloc(sc, 0,
901 			sizeof(struct auvia_dma_op) * segs, M_DEVBUF, M_WAITOK);
902 
903 		if (ch->sc_dma_ops == NULL) {
904 			printf("%s: couldn't build dmaops\n", sc->sc_dev.dv_xname);
905 			return 1;
906 		}
907 
908 		for (dp = sc->sc_dmas;
909 		     dp && dp->addr != (void *)(ch->sc_dma_ops);
910 		     dp = dp->next)
911 			continue;
912 
913 		if (!dp)
914 			panic("%s: build_dma_ops: where'd my memory go??? "
915 				"address (%p)\n", sc->sc_dev.dv_xname,
916 				ch->sc_dma_ops);
917 
918 		ch->sc_dma_op_count = segs;
919 		ch->sc_dma_ops_dma = dp;
920 	}
921 
922 	dp = ch->sc_dma_ops_dma;
923 	op = ch->sc_dma_ops;
924 
925 	while (l) {
926 		op->ptr = s;
927 		l = l - blksize;
928 		if (!l) {
929 			/* if last block */
930 			op->flags = AUVIA_DMAOP_EOL | blksize;
931 		} else {
932 			op->flags = AUVIA_DMAOP_FLAG | blksize;
933 		}
934 		s += blksize;
935 		op++;
936 	}
937 
938 	return 0;
939 }
940 
941 
942 static int
943 auvia_trigger_output(void *addr, void *start, void *end,
944 	int blksize, void (*intr)(void *), void *arg,
945 	const audio_params_t *param)
946 {
947 	struct auvia_softc *sc;
948 	struct auvia_softc_chan *ch;
949 	struct auvia_dma *p;
950 
951 	sc = addr;
952 	ch = &(sc->sc_play);
953 	for (p = sc->sc_dmas; p && p->addr != start; p = p->next)
954 		continue;
955 
956 	if (!p)
957 		panic("auvia_trigger_output: request with bad start "
958 			"address (%p)", start);
959 
960 	if (auvia_build_dma_ops(sc, ch, p, start, end, blksize)) {
961 		return 1;
962 	}
963 
964 	ch->sc_intr = intr;
965 	ch->sc_arg = arg;
966 
967 	CH_WRITE4(sc, ch, AUVIA_RP_DMAOPS_BASE,
968 		ch->sc_dma_ops_dma->map->dm_segs[0].ds_addr);
969 
970 	if (sc->sc_flags & AUVIA_FLAGS_VT8233) {
971 		if (ch->sc_base != VIA8233_MP_BASE) {
972 			CH_WRITE1(sc, ch, VIA8233_RP_DXS_LVOL, 0);
973 			CH_WRITE1(sc, ch, VIA8233_RP_DXS_RVOL, 0);
974 		}
975 		CH_WRITE1(sc, ch, AUVIA_RP_CONTROL,
976 			AUVIA_RPCTRL_START | AUVIA_RPCTRL_AUTOSTART |
977 			AUVIA_RPCTRL_STOP  | AUVIA_RPCTRL_EOL | AUVIA_RPCTRL_FLAG);
978 	} else {
979 		CH_WRITE1(sc, ch, AUVIA_RP_MODE, ch->sc_reg);
980 		CH_WRITE1(sc, ch, AUVIA_RP_CONTROL, AUVIA_RPCTRL_START);
981 	}
982 
983 	return 0;
984 }
985 
986 static int
987 auvia_trigger_input(void *addr, void *start, void *end,
988 	int blksize, void (*intr)(void *), void *arg,
989 	const audio_params_t *param)
990 {
991 	struct auvia_softc *sc;
992 	struct auvia_softc_chan *ch;
993 	struct auvia_dma *p;
994 
995 	sc = addr;
996 	ch = &(sc->sc_record);
997 	for (p = sc->sc_dmas; p && p->addr != start; p = p->next)
998 		continue;
999 
1000 	if (!p)
1001 		panic("auvia_trigger_input: request with bad start "
1002 			"address (%p)", start);
1003 
1004 	if (auvia_build_dma_ops(sc, ch, p, start, end, blksize)) {
1005 		return 1;
1006 	}
1007 
1008 	ch->sc_intr = intr;
1009 	ch->sc_arg = arg;
1010 
1011 	CH_WRITE4(sc, ch, AUVIA_RP_DMAOPS_BASE,
1012 		  ch->sc_dma_ops_dma->map->dm_segs[0].ds_addr);
1013 
1014 	if (sc->sc_flags & AUVIA_FLAGS_VT8233) {
1015 		CH_WRITE1(sc, ch, VIA8233_RP_DXS_LVOL, 0);
1016 		CH_WRITE1(sc, ch, VIA8233_RP_DXS_RVOL, 0);
1017 		CH_WRITE1(sc, ch, AUVIA_RP_CONTROL,
1018 			AUVIA_RPCTRL_START | AUVIA_RPCTRL_AUTOSTART |
1019 			AUVIA_RPCTRL_STOP  | AUVIA_RPCTRL_EOL | AUVIA_RPCTRL_FLAG);
1020 	} else {
1021 		CH_WRITE1(sc, ch, AUVIA_RP_MODE, ch->sc_reg);
1022 		CH_WRITE1(sc, ch, AUVIA_RP_CONTROL, AUVIA_RPCTRL_START);
1023 	}
1024 
1025 	return 0;
1026 }
1027 
1028 static int
1029 auvia_intr(void *arg)
1030 {
1031 	struct auvia_softc *sc;
1032 	struct auvia_softc_chan *ch;
1033 	u_int8_t r;
1034 	int rval;
1035 
1036 	sc = arg;
1037 	rval = 0;
1038 
1039 	ch = &sc->sc_record;
1040 	r = CH_READ1(sc, ch, AUVIA_RP_STAT);
1041 	if (r & AUVIA_RPSTAT_INTR) {
1042 		if (sc->sc_record.sc_intr)
1043 			sc->sc_record.sc_intr(sc->sc_record.sc_arg);
1044 
1045 		/* clear interrupts */
1046 		CH_WRITE1(sc, ch, AUVIA_RP_STAT, AUVIA_RPSTAT_INTR);
1047 		rval = 1;
1048 	}
1049 
1050 	ch = &sc->sc_play;
1051 	r = CH_READ1(sc, ch, AUVIA_RP_STAT);
1052 	if (r & AUVIA_RPSTAT_INTR) {
1053 		if (sc->sc_play.sc_intr)
1054 			sc->sc_play.sc_intr(sc->sc_play.sc_arg);
1055 
1056 		/* clear interrupts */
1057 		CH_WRITE1(sc, ch, AUVIA_RP_STAT, AUVIA_RPSTAT_INTR);
1058 		rval = 1;
1059 	}
1060 
1061 	return rval;
1062 }
1063 
1064 static void
1065 auvia_powerhook(int why, void *addr)
1066 {
1067 	struct auvia_softc *sc;
1068 
1069 	sc = addr;
1070 	switch (why) {
1071 	case PWR_SUSPEND:
1072 	case PWR_STANDBY:
1073 		/* Power down */
1074 		sc->sc_suspend = why;
1075 		break;
1076 
1077 	case PWR_RESUME:
1078 		/* Wake up */
1079 		if (sc->sc_suspend == PWR_RESUME) {
1080 			printf("%s: resume without suspend.\n",
1081 			    sc->sc_dev.dv_xname);
1082 			sc->sc_suspend = why;
1083 			return;
1084 		}
1085 		sc->sc_suspend = why;
1086 		auvia_reset_codec(sc);
1087 		DELAY(1000);
1088 		(sc->codec_if->vtbl->restore_ports)(sc->codec_if);
1089 		break;
1090 
1091 	case PWR_SOFTSUSPEND:
1092 	case PWR_SOFTSTANDBY:
1093 	case PWR_SOFTRESUME:
1094 		break;
1095 	}
1096 }
1097