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