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