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