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