auvia.c revision 1.55 1 /* $NetBSD: auvia.c,v 1.55 2006/08/28 00:02:21 christos 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.55 2006/08/28 00:02:21 christos 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(auvia_powerhook, sc);
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, boolean_t 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,
656 stream_filter_list_t *pfil, 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, struct malloc_type * pool,
809 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 = s;
979 l = l - blksize;
980 if (!l) {
981 /* if last block */
982 op->flags = AUVIA_DMAOP_EOL | blksize;
983 } else {
984 op->flags = 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,
996 int blksize, void (*intr)(void *), void *arg,
997 const audio_params_t *param)
998 {
999 struct auvia_softc *sc;
1000 struct auvia_softc_chan *ch;
1001 struct auvia_dma *p;
1002
1003 sc = addr;
1004 ch = &(sc->sc_play);
1005 for (p = sc->sc_dmas; p && p->addr != start; p = p->next)
1006 continue;
1007
1008 if (!p)
1009 panic("auvia_trigger_output: request with bad start "
1010 "address (%p)", start);
1011
1012 if (auvia_build_dma_ops(sc, ch, p, start, end, blksize)) {
1013 return 1;
1014 }
1015
1016 ch->sc_intr = intr;
1017 ch->sc_arg = arg;
1018
1019 CH_WRITE4(sc, ch, AUVIA_RP_DMAOPS_BASE,
1020 ch->sc_dma_ops_dma->map->dm_segs[0].ds_addr);
1021
1022 if (sc->sc_flags & AUVIA_FLAGS_VT8233) {
1023 if (ch->sc_base != VIA8233_MP_BASE) {
1024 CH_WRITE1(sc, ch, VIA8233_RP_DXS_LVOL, 0);
1025 CH_WRITE1(sc, ch, VIA8233_RP_DXS_RVOL, 0);
1026 }
1027 CH_WRITE1(sc, ch, AUVIA_RP_CONTROL,
1028 AUVIA_RPCTRL_START | AUVIA_RPCTRL_AUTOSTART |
1029 AUVIA_RPCTRL_STOP | AUVIA_RPCTRL_EOL | AUVIA_RPCTRL_FLAG);
1030 } else {
1031 CH_WRITE1(sc, ch, AUVIA_RP_MODE, ch->sc_reg);
1032 CH_WRITE1(sc, ch, AUVIA_RP_CONTROL, AUVIA_RPCTRL_START);
1033 }
1034
1035 return 0;
1036 }
1037
1038 static int
1039 auvia_trigger_input(void *addr, void *start, void *end,
1040 int blksize, void (*intr)(void *), void *arg,
1041 const audio_params_t *param)
1042 {
1043 struct auvia_softc *sc;
1044 struct auvia_softc_chan *ch;
1045 struct auvia_dma *p;
1046
1047 sc = addr;
1048 ch = &(sc->sc_record);
1049 for (p = sc->sc_dmas; p && p->addr != start; p = p->next)
1050 continue;
1051
1052 if (!p)
1053 panic("auvia_trigger_input: request with bad start "
1054 "address (%p)", start);
1055
1056 if (auvia_build_dma_ops(sc, ch, p, start, end, blksize)) {
1057 return 1;
1058 }
1059
1060 ch->sc_intr = intr;
1061 ch->sc_arg = arg;
1062
1063 CH_WRITE4(sc, ch, AUVIA_RP_DMAOPS_BASE,
1064 ch->sc_dma_ops_dma->map->dm_segs[0].ds_addr);
1065
1066 if (sc->sc_flags & AUVIA_FLAGS_VT8233) {
1067 CH_WRITE1(sc, ch, VIA8233_RP_DXS_LVOL, 0);
1068 CH_WRITE1(sc, ch, VIA8233_RP_DXS_RVOL, 0);
1069 CH_WRITE1(sc, ch, AUVIA_RP_CONTROL,
1070 AUVIA_RPCTRL_START | AUVIA_RPCTRL_AUTOSTART |
1071 AUVIA_RPCTRL_STOP | AUVIA_RPCTRL_EOL | AUVIA_RPCTRL_FLAG);
1072 } else {
1073 CH_WRITE1(sc, ch, AUVIA_RP_MODE, ch->sc_reg);
1074 CH_WRITE1(sc, ch, AUVIA_RP_CONTROL, AUVIA_RPCTRL_START);
1075 }
1076
1077 return 0;
1078 }
1079
1080 static int
1081 auvia_intr(void *arg)
1082 {
1083 struct auvia_softc *sc;
1084 struct auvia_softc_chan *ch;
1085 u_int8_t r;
1086 int rval;
1087
1088 sc = arg;
1089 rval = 0;
1090
1091 ch = &sc->sc_record;
1092 r = CH_READ1(sc, ch, AUVIA_RP_STAT);
1093 if (r & AUVIA_RPSTAT_INTR) {
1094 if (sc->sc_record.sc_intr)
1095 sc->sc_record.sc_intr(sc->sc_record.sc_arg);
1096
1097 /* clear interrupts */
1098 CH_WRITE1(sc, ch, AUVIA_RP_STAT, AUVIA_RPSTAT_INTR);
1099 rval = 1;
1100 }
1101
1102 ch = &sc->sc_play;
1103 r = CH_READ1(sc, ch, AUVIA_RP_STAT);
1104 if (r & AUVIA_RPSTAT_INTR) {
1105 if (sc->sc_play.sc_intr)
1106 sc->sc_play.sc_intr(sc->sc_play.sc_arg);
1107
1108 /* clear interrupts */
1109 CH_WRITE1(sc, ch, AUVIA_RP_STAT, AUVIA_RPSTAT_INTR);
1110 rval = 1;
1111 }
1112
1113 return rval;
1114 }
1115
1116 static void
1117 auvia_powerhook(int why, void *addr)
1118 {
1119 struct auvia_softc *sc;
1120
1121 sc = addr;
1122 switch (why) {
1123 case PWR_SUSPEND:
1124 case PWR_STANDBY:
1125 /* Power down */
1126 sc->sc_suspend = why;
1127 break;
1128
1129 case PWR_RESUME:
1130 /* Wake up */
1131 if (sc->sc_suspend == PWR_RESUME) {
1132 printf("%s: resume without suspend.\n",
1133 sc->sc_dev.dv_xname);
1134 sc->sc_suspend = why;
1135 return;
1136 }
1137 sc->sc_suspend = why;
1138 auvia_reset_codec(sc);
1139 DELAY(1000);
1140 (sc->codec_if->vtbl->restore_ports)(sc->codec_if);
1141 break;
1142
1143 case PWR_SOFTSUSPEND:
1144 case PWR_SOFTSTANDBY:
1145 case PWR_SOFTRESUME:
1146 break;
1147 }
1148 }
1149