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