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