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