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