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