auich.c revision 1.69 1 /* $NetBSD: auich.c,v 1.69 2004/10/31 18:30:52 mycroft 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 Jason R. Thorpe.
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 * Copyright (c) 2000 Michael Shalayeff
41 * All rights reserved.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. The name of the author may not be used to endorse or promote products
52 * derived from this software without specific prior written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
55 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
56 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
57 * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
58 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
59 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
60 * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
62 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
63 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
64 * THE POSSIBILITY OF SUCH DAMAGE.
65 *
66 * from OpenBSD: ich.c,v 1.3 2000/08/11 06:17:18 mickey Exp
67 */
68
69 /*
70 * Copyright (c) 2000 Katsurajima Naoto <raven (at) katsurajima.seya.yokohama.jp>
71 * Copyright (c) 2001 Cameron Grant <cg (at) freebsd.org>
72 * All rights reserved.
73 *
74 * Redistribution and use in source and binary forms, with or without
75 * modification, are permitted provided that the following conditions
76 * are met:
77 * 1. Redistributions of source code must retain the above copyright
78 * notice, this list of conditions and the following disclaimer.
79 * 2. Redistributions in binary form must reproduce the above copyright
80 * notice, this list of conditions and the following disclaimer in the
81 * documentation and/or other materials provided with the distribution.
82 *
83 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
84 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
85 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
86 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
87 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
88 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
89 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
90 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHERIN CONTRACT, STRICT
91 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
92 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THEPOSSIBILITY OF
93 * SUCH DAMAGE.
94 *
95 * auich_calibrate() was from FreeBSD: ich.c,v 1.22 2002/06/27 22:36:01 scottl Exp
96 */
97
98
99 /* #define AUICH_DEBUG */
100 /*
101 * AC'97 audio found on Intel 810/820/440MX chipsets.
102 * http://developer.intel.com/design/chipsets/datashts/290655.htm
103 * http://developer.intel.com/design/chipsets/manuals/298028.htm
104 * ICH3:http://www.intel.com/design/chipsets/datashts/290716.htm
105 * ICH4:http://www.intel.com/design/chipsets/datashts/290744.htm
106 * ICH5:http://www.intel.com/design/chipsets/datashts/252516.htm
107 * AMD8111:
108 * http://www.amd.com/us-en/assets/content_type/white_papers_and_tech_docs/24674.pdf
109 * http://www.amd.com/us-en/assets/content_type/white_papers_and_tech_docs/25720.pdf
110 *
111 * TODO:
112 * - Add support for the dedicated microphone input.
113 *
114 * NOTE:
115 * - The 440MX B-stepping at running 100MHz has a hardware erratum.
116 * It causes PCI master abort and hangups until cold reboot.
117 * http://www.intel.com/design/chipsets/specupdt/245051.htm
118 */
119
120 #include <sys/cdefs.h>
121 __KERNEL_RCSID(0, "$NetBSD: auich.c,v 1.69 2004/10/31 18:30:52 mycroft Exp $");
122
123 #include <sys/param.h>
124 #include <sys/systm.h>
125 #include <sys/kernel.h>
126 #include <sys/malloc.h>
127 #include <sys/device.h>
128 #include <sys/fcntl.h>
129 #include <sys/proc.h>
130 #include <sys/sysctl.h>
131
132 #include <uvm/uvm_extern.h> /* for PAGE_SIZE */
133
134 #include <dev/pci/pcidevs.h>
135 #include <dev/pci/pcivar.h>
136 #include <dev/pci/auichreg.h>
137
138 #include <sys/audioio.h>
139 #include <dev/audio_if.h>
140 #include <dev/mulaw.h>
141 #include <dev/auconv.h>
142
143 #include <machine/bus.h>
144
145 #include <dev/ic/ac97reg.h>
146 #include <dev/ic/ac97var.h>
147
148 struct auich_dma {
149 bus_dmamap_t map;
150 caddr_t addr;
151 bus_dma_segment_t segs[1];
152 int nsegs;
153 size_t size;
154 struct auich_dma *next;
155 };
156
157 #define DMAADDR(p) ((p)->map->dm_segs[0].ds_addr)
158 #define KERNADDR(p) ((void *)((p)->addr))
159
160 struct auich_cdata {
161 struct auich_dmalist ic_dmalist_pcmo[ICH_DMALIST_MAX];
162 struct auich_dmalist ic_dmalist_pcmi[ICH_DMALIST_MAX];
163 struct auich_dmalist ic_dmalist_mici[ICH_DMALIST_MAX];
164 };
165
166 #define ICH_CDOFF(x) offsetof(struct auich_cdata, x)
167 #define ICH_PCMO_OFF(x) ICH_CDOFF(ic_dmalist_pcmo[(x)])
168 #define ICH_PCMI_OFF(x) ICH_CDOFF(ic_dmalist_pcmi[(x)])
169 #define ICH_MICI_OFF(x) ICH_CDOFF(ic_dmalist_mici[(x)])
170
171 struct auich_softc {
172 struct device sc_dev;
173 void *sc_ih;
174
175 struct device *sc_audiodev;
176 audio_device_t sc_audev;
177
178 bus_space_tag_t iot;
179 bus_space_handle_t mix_ioh;
180 bus_space_handle_t aud_ioh;
181 bus_dma_tag_t dmat;
182
183 struct ac97_codec_if *codec_if;
184 struct ac97_host_if host_if;
185
186 /* DMA scatter-gather lists. */
187 bus_dmamap_t sc_cddmamap;
188 #define sc_cddma sc_cddmamap->dm_segs[0].ds_addr
189
190 struct auich_cdata *sc_cdata;
191 #define dmalist_pcmo sc_cdata->ic_dmalist_pcmo
192 #define dmalist_pcmi sc_cdata->ic_dmalist_pcmi
193 #define dmalist_mici sc_cdata->ic_dmalist_mici
194
195 int ptr_pcmo,
196 ptr_pcmi,
197 ptr_mici;
198
199 /* i/o buffer pointers */
200 u_int32_t pcmo_start, pcmo_p, pcmo_end;
201 int pcmo_blksize, pcmo_fifoe;
202
203 u_int32_t pcmi_start, pcmi_p, pcmi_end;
204 int pcmi_blksize, pcmi_fifoe;
205
206 u_int32_t mici_start, mici_p, mici_end;
207 int mici_blksize, mici_fifoe;
208
209 struct auich_dma *sc_dmas;
210
211 #ifdef DIAGNOSTIC
212 pci_chipset_tag_t sc_pc;
213 pcitag_t sc_pt;
214 #endif
215 /* SiS 7012 hack */
216 int sc_sample_size;
217 int sc_sts_reg;
218 /* 440MX workaround */
219 int sc_dmamap_flags;
220
221 void (*sc_pintr)(void *);
222 void *sc_parg;
223
224 void (*sc_rintr)(void *);
225 void *sc_rarg;
226
227 /* Power Management */
228 void *sc_powerhook;
229 int sc_suspend;
230
231 /* sysctl */
232 struct sysctllog *sc_log;
233 uint32_t sc_ac97_clock;
234 int sc_ac97_clock_mib;
235 };
236
237 #define IS_FIXED_RATE(codec) !((codec)->vtbl->get_extcaps(codec) \
238 & AC97_EXT_AUDIO_VRA)
239 #define SUPPORTS_4CH(codec) ((codec)->vtbl->get_extcaps(codec) \
240 & AC97_EXT_AUDIO_SDAC)
241 #define AC97_6CH_DACS (AC97_EXT_AUDIO_SDAC | AC97_EXT_AUDIO_CDAC \
242 | AC97_EXT_AUDIO_LDAC)
243 #define SUPPORTS_6CH(codec) (((codec)->vtbl->get_extcaps(codec) \
244 & AC97_6CH_DACS) == AC97_6CH_DACS)
245
246 /* Debug */
247 #ifdef AUICH_DEBUG
248 #define DPRINTF(l,x) do { if (auich_debug & (l)) printf x; } while(0)
249 int auich_debug = 0xfffe;
250 #define ICH_DEBUG_CODECIO 0x0001
251 #define ICH_DEBUG_DMA 0x0002
252 #define ICH_DEBUG_INTR 0x0004
253 #else
254 #define DPRINTF(x,y) /* nothing */
255 #endif
256
257 int auich_match(struct device *, struct cfdata *, void *);
258 void auich_attach(struct device *, struct device *, void *);
259 int auich_intr(void *);
260
261 CFATTACH_DECL(auich, sizeof(struct auich_softc),
262 auich_match, auich_attach, NULL, NULL);
263
264 int auich_open(void *, int);
265 void auich_close(void *);
266 int auich_query_encoding(void *, struct audio_encoding *);
267 int auich_set_params(void *, int, int, struct audio_params *,
268 struct audio_params *);
269 int auich_round_blocksize(void *, int);
270 int auich_halt_output(void *);
271 int auich_halt_input(void *);
272 int auich_getdev(void *, struct audio_device *);
273 int auich_set_port(void *, mixer_ctrl_t *);
274 int auich_get_port(void *, mixer_ctrl_t *);
275 int auich_query_devinfo(void *, mixer_devinfo_t *);
276 void *auich_allocm(void *, int, size_t, struct malloc_type *, int);
277 void auich_freem(void *, void *, struct malloc_type *);
278 size_t auich_round_buffersize(void *, int, size_t);
279 paddr_t auich_mappage(void *, void *, off_t, int);
280 int auich_get_props(void *);
281 int auich_trigger_output(void *, void *, void *, int, void (*)(void *),
282 void *, struct audio_params *);
283 int auich_trigger_input(void *, void *, void *, int, void (*)(void *),
284 void *, struct audio_params *);
285
286 int auich_alloc_cdata(struct auich_softc *);
287
288 int auich_allocmem(struct auich_softc *, size_t, size_t,
289 struct auich_dma *);
290 int auich_freemem(struct auich_softc *, struct auich_dma *);
291
292 void auich_powerhook(int, void *);
293 int auich_set_rate(struct auich_softc *, int, u_long);
294 static int auich_sysctl_verify(SYSCTLFN_ARGS);
295 void auich_finish_attach(struct device *);
296 void auich_calibrate(struct auich_softc *);
297
298
299 const struct audio_hw_if auich_hw_if = {
300 auich_open,
301 auich_close,
302 NULL, /* drain */
303 auich_query_encoding,
304 auich_set_params,
305 auich_round_blocksize,
306 NULL, /* commit_setting */
307 NULL, /* init_output */
308 NULL, /* init_input */
309 NULL, /* start_output */
310 NULL, /* start_input */
311 auich_halt_output,
312 auich_halt_input,
313 NULL, /* speaker_ctl */
314 auich_getdev,
315 NULL, /* getfd */
316 auich_set_port,
317 auich_get_port,
318 auich_query_devinfo,
319 auich_allocm,
320 auich_freem,
321 auich_round_buffersize,
322 auich_mappage,
323 auich_get_props,
324 auich_trigger_output,
325 auich_trigger_input,
326 NULL, /* dev_ioctl */
327 };
328
329 int auich_attach_codec(void *, struct ac97_codec_if *);
330 int auich_read_codec(void *, u_int8_t, u_int16_t *);
331 int auich_write_codec(void *, u_int8_t, u_int16_t);
332 int auich_reset_codec(void *);
333
334 static const struct auich_devtype {
335 int vendor;
336 int product;
337 const char *name;
338 const char *shortname; /* must be less than 11 characters */
339 } auich_devices[] = {
340 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801AA_ACA,
341 "i82801AA (ICH) AC-97 Audio", "ICH" },
342 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801AB_ACA,
343 "i82801AB (ICH0) AC-97 Audio", "ICH0" },
344 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801BA_ACA,
345 "i82801BA (ICH2) AC-97 Audio", "ICH2" },
346 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82440MX_ACA,
347 "i82440MX AC-97 Audio", "440MX" },
348 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801CA_AC,
349 "i82801CA (ICH3) AC-97 Audio", "ICH3" },
350 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801DB_AC,
351 "i82801DB/DBM (ICH4/ICH4M) AC-97 Audio", "ICH4" },
352 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82801EB_AC,
353 "i82801EB (ICH5) AC-97 Audio", "ICH5" },
354 { PCI_VENDOR_SIS, PCI_PRODUCT_SIS_7012_AC,
355 "SiS 7012 AC-97 Audio", "SiS7012" },
356 { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_NFORCE_MCP_AC,
357 "nForce MCP AC-97 Audio", "nForce" },
358 { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_NFORCE2_MCPT_AC,
359 "nForce2 MCP-T AC-97 Audio", "nForce2" },
360 { PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_NFORCE3_MCPT_AC,
361 "nForce3 MCP-T AC-97 Audio", "nForce3" },
362 { PCI_VENDOR_AMD, PCI_PRODUCT_AMD_PBC768_AC,
363 "AMD768 AC-97 Audio", "AMD768" },
364 { PCI_VENDOR_AMD, PCI_PRODUCT_AMD_PBC8111_AC,
365 "AMD8111 AC-97 Audio", "AMD8111" },
366 { 0, 0,
367 NULL, NULL },
368 };
369
370 static const struct auich_devtype *
371 auich_lookup(struct pci_attach_args *pa)
372 {
373 const struct auich_devtype *d;
374
375 for (d = auich_devices; d->name != NULL; d++) {
376 if (PCI_VENDOR(pa->pa_id) == d->vendor
377 && PCI_PRODUCT(pa->pa_id) == d->product)
378 return (d);
379 }
380
381 return (NULL);
382 }
383
384 int
385 auich_match(struct device *parent, struct cfdata *match, void *aux)
386 {
387 struct pci_attach_args *pa = aux;
388
389 if (auich_lookup(pa) != NULL)
390 return (1);
391
392 return (0);
393 }
394
395 void
396 auich_attach(struct device *parent, struct device *self, void *aux)
397 {
398 struct auich_softc *sc = (struct auich_softc *)self;
399 struct pci_attach_args *pa = aux;
400 pci_intr_handle_t ih;
401 bus_size_t mix_size, aud_size;
402 pcireg_t v;
403 const char *intrstr;
404 const struct auich_devtype *d;
405 struct sysctlnode *node;
406 int err, node_mib;
407
408 aprint_naive(": Audio controller\n");
409
410 d = auich_lookup(pa);
411 if (d == NULL)
412 panic("auich_attach: impossible");
413
414 #ifdef DIAGNOSTIC
415 sc->sc_pc = pa->pa_pc;
416 sc->sc_pt = pa->pa_tag;
417 #endif
418
419 aprint_normal(": %s\n", d->name);
420
421 if ((d->vendor == PCI_VENDOR_INTEL
422 && d->product == PCI_PRODUCT_INTEL_82801DB_AC)
423 || (d->vendor == PCI_VENDOR_INTEL
424 && d->product == PCI_PRODUCT_INTEL_82801EB_AC)) {
425 /*
426 * Use native mode for ICH4/ICH5
427 */
428 if (pci_mapreg_map(pa, ICH_MMBAR, PCI_MAPREG_TYPE_MEM, 0,
429 &sc->iot, &sc->mix_ioh, NULL, &mix_size)) {
430 v = pci_conf_read(pa->pa_pc, pa->pa_tag, ICH_CFG);
431 pci_conf_write(pa->pa_pc, pa->pa_tag, ICH_CFG,
432 v | ICH_CFG_IOSE);
433 if (pci_mapreg_map(pa, ICH_NAMBAR, PCI_MAPREG_TYPE_IO,
434 0, &sc->iot, &sc->mix_ioh, NULL,
435 &mix_size)) {
436 aprint_error("%s: can't map codec i/o space\n",
437 sc->sc_dev.dv_xname);
438 return;
439 }
440 }
441 if (pci_mapreg_map(pa, ICH_MBBAR, PCI_MAPREG_TYPE_MEM, 0,
442 &sc->iot, &sc->aud_ioh, NULL, &aud_size)) {
443 v = pci_conf_read(pa->pa_pc, pa->pa_tag, ICH_CFG);
444 pci_conf_write(pa->pa_pc, pa->pa_tag, ICH_CFG,
445 v | ICH_CFG_IOSE);
446 if (pci_mapreg_map(pa, ICH_NABMBAR, PCI_MAPREG_TYPE_IO,
447 0, &sc->iot, &sc->aud_ioh, NULL,
448 &aud_size)) {
449 aprint_error("%s: can't map device i/o space\n",
450 sc->sc_dev.dv_xname);
451 return;
452 }
453 }
454 } else {
455 if (pci_mapreg_map(pa, ICH_NAMBAR, PCI_MAPREG_TYPE_IO, 0,
456 &sc->iot, &sc->mix_ioh, NULL, &mix_size)) {
457 aprint_error("%s: can't map codec i/o space\n",
458 sc->sc_dev.dv_xname);
459 return;
460 }
461 if (pci_mapreg_map(pa, ICH_NABMBAR, PCI_MAPREG_TYPE_IO, 0,
462 &sc->iot, &sc->aud_ioh, NULL, &aud_size)) {
463 aprint_error("%s: can't map device i/o space\n",
464 sc->sc_dev.dv_xname);
465 return;
466 }
467 }
468 sc->dmat = pa->pa_dmat;
469
470 /* enable bus mastering */
471 v = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
472 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
473 v | PCI_COMMAND_MASTER_ENABLE | PCI_COMMAND_BACKTOBACK_ENABLE);
474
475 /* Map and establish the interrupt. */
476 if (pci_intr_map(pa, &ih)) {
477 aprint_error("%s: can't map interrupt\n", sc->sc_dev.dv_xname);
478 return;
479 }
480 intrstr = pci_intr_string(pa->pa_pc, ih);
481 sc->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_AUDIO,
482 auich_intr, sc);
483 if (sc->sc_ih == NULL) {
484 aprint_error("%s: can't establish interrupt",
485 sc->sc_dev.dv_xname);
486 if (intrstr != NULL)
487 aprint_normal(" at %s", intrstr);
488 aprint_normal("\n");
489 return;
490 }
491 aprint_normal("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
492
493 snprintf(sc->sc_audev.name, MAX_AUDIO_DEV_LEN, "%s AC97", d->shortname);
494 snprintf(sc->sc_audev.version, MAX_AUDIO_DEV_LEN,
495 "0x%02x", PCI_REVISION(pa->pa_class));
496 strlcpy(sc->sc_audev.config, sc->sc_dev.dv_xname, MAX_AUDIO_DEV_LEN);
497
498 /* SiS 7012 needs special handling */
499 if (d->vendor == PCI_VENDOR_SIS
500 && d->product == PCI_PRODUCT_SIS_7012_AC) {
501 sc->sc_sts_reg = ICH_PICB;
502 sc->sc_sample_size = 1;
503 } else {
504 sc->sc_sts_reg = ICH_STS;
505 sc->sc_sample_size = 2;
506 }
507
508 /* Workaround for a 440MX B-stepping erratum */
509 sc->sc_dmamap_flags = BUS_DMA_COHERENT;
510 if (d->vendor == PCI_VENDOR_INTEL
511 && d->product == PCI_PRODUCT_INTEL_82440MX_ACA) {
512 sc->sc_dmamap_flags |= BUS_DMA_NOCACHE;
513 printf("%s: DMA bug workaround enabled\n", sc->sc_dev.dv_xname);
514 }
515
516 /* Set up DMA lists. */
517 sc->ptr_pcmo = sc->ptr_pcmi = sc->ptr_mici = 0;
518 auich_alloc_cdata(sc);
519
520 DPRINTF(ICH_DEBUG_DMA, ("auich_attach: lists %p %p %p\n",
521 sc->dmalist_pcmo, sc->dmalist_pcmi, sc->dmalist_mici));
522
523 sc->host_if.arg = sc;
524 sc->host_if.attach = auich_attach_codec;
525 sc->host_if.read = auich_read_codec;
526 sc->host_if.write = auich_write_codec;
527 sc->host_if.reset = auich_reset_codec;
528
529 if (ac97_attach(&sc->host_if) != 0)
530 return;
531
532 /* Watch for power change */
533 sc->sc_suspend = PWR_RESUME;
534 sc->sc_powerhook = powerhook_establish(auich_powerhook, sc);
535
536 config_interrupts(self, auich_finish_attach);
537
538 /* sysctl setup */
539 if (IS_FIXED_RATE(sc->codec_if))
540 return;
541 err = sysctl_createv(&sc->sc_log, 0, NULL, NULL, 0,
542 CTLTYPE_NODE, "hw", NULL, NULL, 0, NULL, 0,
543 CTL_HW, CTL_EOL);
544 if (err != 0)
545 goto sysctl_err;
546 err = sysctl_createv(&sc->sc_log, 0, NULL, &node, 0,
547 CTLTYPE_NODE, sc->sc_dev.dv_xname, NULL, NULL, 0,
548 NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
549 if (err != 0)
550 goto sysctl_err;
551 node_mib = node->sysctl_num;
552 /* passing the sc address instead of &sc->sc_ac97_clock */
553 err = sysctl_createv(&sc->sc_log, 0, NULL, &node, CTLFLAG_READWRITE,
554 CTLTYPE_INT, "ac97rate",
555 SYSCTL_DESCR("AC'97 codec link rate"),
556 auich_sysctl_verify, 0, sc, 0,
557 CTL_HW, node_mib, CTL_CREATE, CTL_EOL);
558 if (err != 0)
559 goto sysctl_err;
560 sc->sc_ac97_clock_mib = node->sysctl_num;
561
562 return;
563
564 sysctl_err:
565 printf("%s: failed to add sysctl nodes. (%d)\n",
566 sc->sc_dev.dv_xname, err);
567 return; /* failure of sysctl is not fatal. */
568 }
569
570 #if 0
571 int
572 auich_detach(struct device *self, int flags)
573 {
574 struct auich_softc *sc;
575
576 sc = (struct auich_softc *)self;
577 /* sysctl */
578 sysctl_teardown(&sc->sc_log);
579 /* audio */
580 if (sc->sc_audiodev != NULL)
581 config_detach(sc->sc_audiodev, flags);
582 /* XXX ac97 */
583 /* XXX memory */
584 return 0;
585 }
586 #endif
587
588 static int
589 auich_sysctl_verify(SYSCTLFN_ARGS)
590 {
591 int error, tmp;
592 struct sysctlnode node;
593 struct auich_softc *sc;
594
595 node = *rnode;
596 sc = rnode->sysctl_data;
597 tmp = sc->sc_ac97_clock;
598 node.sysctl_data = &tmp;
599 error = sysctl_lookup(SYSCTLFN_CALL(&node));
600 if (error || newp == NULL)
601 return error;
602
603 if (node.sysctl_num == sc->sc_ac97_clock_mib) {
604 if (tmp < 48000 || tmp > 96000)
605 return EINVAL;
606 sc->sc_ac97_clock = tmp;
607 }
608
609 return 0;
610 }
611
612 void
613 auich_finish_attach(struct device *self)
614 {
615 struct auich_softc *sc = (void *)self;
616
617 if (!IS_FIXED_RATE(sc->codec_if))
618 auich_calibrate(sc);
619
620 sc->sc_audiodev = audio_attach_mi(&auich_hw_if, sc, &sc->sc_dev);
621 }
622
623 #define ICH_CODECIO_INTERVAL 10
624 int
625 auich_read_codec(void *v, u_int8_t reg, u_int16_t *val)
626 {
627 struct auich_softc *sc = v;
628 int i;
629 uint32_t status;
630
631 /* wait for an access semaphore */
632 for (i = ICH_SEMATIMO / ICH_CODECIO_INTERVAL; i-- &&
633 bus_space_read_1(sc->iot, sc->aud_ioh, ICH_CAS) & 1;
634 DELAY(ICH_CODECIO_INTERVAL));
635
636 if (i > 0) {
637 *val = bus_space_read_2(sc->iot, sc->mix_ioh, reg);
638 DPRINTF(ICH_DEBUG_CODECIO,
639 ("auich_read_codec(%x, %x)\n", reg, *val));
640 status = bus_space_read_4(sc->iot, sc->aud_ioh, ICH_GSTS);
641 if (status & ICH_RCS) {
642 bus_space_write_4(sc->iot, sc->aud_ioh, ICH_GSTS,
643 status & ~(ICH_SRI|ICH_PRI|ICH_GSCI));
644 *val = 0xffff;
645 }
646 return 0;
647 } else {
648 DPRINTF(ICH_DEBUG_CODECIO,
649 ("%s: read_codec timeout\n", sc->sc_dev.dv_xname));
650 return -1;
651 }
652 }
653
654 int
655 auich_write_codec(void *v, u_int8_t reg, u_int16_t val)
656 {
657 struct auich_softc *sc = v;
658 int i;
659
660 DPRINTF(ICH_DEBUG_CODECIO, ("auich_write_codec(%x, %x)\n", reg, val));
661 /* wait for an access semaphore */
662 for (i = ICH_SEMATIMO / ICH_CODECIO_INTERVAL; i-- &&
663 bus_space_read_1(sc->iot, sc->aud_ioh, ICH_CAS) & 1;
664 DELAY(ICH_CODECIO_INTERVAL));
665
666 if (i > 0) {
667 bus_space_write_2(sc->iot, sc->mix_ioh, reg, val);
668 return 0;
669 } else {
670 DPRINTF(ICH_DEBUG_CODECIO,
671 ("%s: write_codec timeout\n", sc->sc_dev.dv_xname));
672 return -1;
673 }
674 }
675
676 int
677 auich_attach_codec(void *v, struct ac97_codec_if *cif)
678 {
679 struct auich_softc *sc = v;
680
681 sc->codec_if = cif;
682 return 0;
683 }
684
685 int
686 auich_reset_codec(void *v)
687 {
688 struct auich_softc *sc = v;
689 int i;
690 uint32_t control, status;
691
692 control = bus_space_read_4(sc->iot, sc->aud_ioh, ICH_GCTRL);
693 control &= ~(ICH_ACLSO | ICH_PCM246_MASK);
694 control |= (control & ICH_CRESET) ? ICH_WRESET : ICH_CRESET;
695 bus_space_write_4(sc->iot, sc->aud_ioh, ICH_GCTRL, control);
696
697 for (i = 500000; i >= 0; i--) {
698 status = bus_space_read_4(sc->iot, sc->aud_ioh, ICH_GSTS);
699 if (status & (ICH_PCR | ICH_SCR | ICH_S2CR))
700 break;
701 DELAY(1);
702 }
703 if (i <= 0) {
704 printf("%s: auich_reset_codec: time out\n", sc->sc_dev.dv_xname);
705 return ETIMEDOUT;
706 }
707 #ifdef DEBUG
708 if (status & ICH_SCR)
709 printf("%s: The 2nd codec is ready.\n",
710 sc->sc_dev.dv_xname);
711 if (status & ICH_S2CR)
712 printf("%s: The 3rd codec is ready.\n",
713 sc->sc_dev.dv_xname);
714 #endif
715 return 0;
716 }
717
718 int
719 auich_open(void *v, int flags)
720 {
721 return 0;
722 }
723
724 void
725 auich_close(void *v)
726 {
727 }
728
729 int
730 auich_query_encoding(void *v, struct audio_encoding *aep)
731 {
732
733 switch (aep->index) {
734 case 0:
735 strcpy(aep->name, AudioEulinear);
736 aep->encoding = AUDIO_ENCODING_ULINEAR;
737 aep->precision = 8;
738 aep->flags = AUDIO_ENCODINGFLAG_EMULATED;
739 return (0);
740 case 1:
741 strcpy(aep->name, AudioEmulaw);
742 aep->encoding = AUDIO_ENCODING_ULAW;
743 aep->precision = 8;
744 aep->flags = AUDIO_ENCODINGFLAG_EMULATED;
745 return (0);
746 case 2:
747 strcpy(aep->name, AudioEalaw);
748 aep->encoding = AUDIO_ENCODING_ALAW;
749 aep->precision = 8;
750 aep->flags = AUDIO_ENCODINGFLAG_EMULATED;
751 return (0);
752 case 3:
753 strcpy(aep->name, AudioEslinear);
754 aep->encoding = AUDIO_ENCODING_SLINEAR;
755 aep->precision = 8;
756 aep->flags = AUDIO_ENCODINGFLAG_EMULATED;
757 return (0);
758 case 4:
759 strcpy(aep->name, AudioEslinear_le);
760 aep->encoding = AUDIO_ENCODING_SLINEAR_LE;
761 aep->precision = 16;
762 aep->flags = 0;
763 return (0);
764 case 5:
765 strcpy(aep->name, AudioEulinear_le);
766 aep->encoding = AUDIO_ENCODING_ULINEAR_LE;
767 aep->precision = 16;
768 aep->flags = AUDIO_ENCODINGFLAG_EMULATED;
769 return (0);
770 case 6:
771 strcpy(aep->name, AudioEslinear_be);
772 aep->encoding = AUDIO_ENCODING_SLINEAR_BE;
773 aep->precision = 16;
774 aep->flags = AUDIO_ENCODINGFLAG_EMULATED;
775 return (0);
776 case 7:
777 strcpy(aep->name, AudioEulinear_be);
778 aep->encoding = AUDIO_ENCODING_ULINEAR_BE;
779 aep->precision = 16;
780 aep->flags = AUDIO_ENCODINGFLAG_EMULATED;
781 return (0);
782 default:
783 return (EINVAL);
784 }
785 }
786
787 int
788 auich_set_rate(struct auich_softc *sc, int mode, u_long srate)
789 {
790 int ret;
791 u_long ratetmp;
792
793 sc->codec_if->vtbl->set_clock(sc->codec_if, sc->sc_ac97_clock);
794 ratetmp = srate;
795 if (mode == AUMODE_RECORD)
796 return sc->codec_if->vtbl->set_rate(sc->codec_if,
797 AC97_REG_PCM_LR_ADC_RATE, &ratetmp);
798 ret = sc->codec_if->vtbl->set_rate(sc->codec_if,
799 AC97_REG_PCM_FRONT_DAC_RATE, &ratetmp);
800 if (ret)
801 return ret;
802 ratetmp = srate;
803 ret = sc->codec_if->vtbl->set_rate(sc->codec_if,
804 AC97_REG_PCM_SURR_DAC_RATE, &ratetmp);
805 if (ret)
806 return ret;
807 ratetmp = srate;
808 ret = sc->codec_if->vtbl->set_rate(sc->codec_if,
809 AC97_REG_PCM_LFE_DAC_RATE, &ratetmp);
810 return ret;
811 }
812
813 int
814 auich_set_params(void *v, int setmode, int usemode, struct audio_params *play,
815 struct audio_params *rec)
816 {
817 struct auich_softc *sc = v;
818 struct audio_params *p;
819 int mode;
820 u_int32_t control;
821
822 for (mode = AUMODE_RECORD; mode != -1;
823 mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
824 if ((setmode & mode) == 0)
825 continue;
826
827 p = mode == AUMODE_PLAY ? play : rec;
828 if (p == NULL)
829 continue;
830
831 if ((p->sample_rate != 8000) &&
832 (p->sample_rate != 11025) &&
833 (p->sample_rate != 12000) &&
834 (p->sample_rate != 16000) &&
835 (p->sample_rate != 22050) &&
836 (p->sample_rate != 24000) &&
837 (p->sample_rate != 32000) &&
838 (p->sample_rate != 44100) &&
839 (p->sample_rate != 48000))
840 return (EINVAL);
841
842 p->factor = 1;
843 if (p->precision == 8)
844 p->factor *= 2;
845
846 p->sw_code = NULL;
847 /* setup hardware formats */
848 p->hw_encoding = AUDIO_ENCODING_SLINEAR_LE;
849 p->hw_precision = 16;
850
851 if (mode == AUMODE_RECORD) {
852 if (p->channels < 1 || p->channels > 2)
853 return EINVAL;
854 } else {
855 switch (p->channels) {
856 case 1:
857 break;
858 case 2:
859 break;
860 case 4:
861 if (!SUPPORTS_4CH(sc->codec_if))
862 return EINVAL;
863 break;
864 case 6:
865 if (!SUPPORTS_6CH(sc->codec_if))
866 return EINVAL;
867 break;
868 default:
869 return EINVAL;
870 }
871 }
872 /* If monaural is requested, aurateconv expands a monaural
873 * stream to stereo. */
874 if (p->channels == 1)
875 p->hw_channels = 2;
876
877 switch (p->encoding) {
878 case AUDIO_ENCODING_SLINEAR_BE:
879 if (p->precision == 16) {
880 p->sw_code = swap_bytes;
881 } else {
882 if (mode == AUMODE_PLAY)
883 p->sw_code = linear8_to_linear16_le;
884 else
885 p->sw_code = linear16_to_linear8_le;
886 }
887 break;
888
889 case AUDIO_ENCODING_SLINEAR_LE:
890 if (p->precision != 16) {
891 if (mode == AUMODE_PLAY)
892 p->sw_code = linear8_to_linear16_le;
893 else
894 p->sw_code = linear16_to_linear8_le;
895 }
896 break;
897
898 case AUDIO_ENCODING_ULINEAR_BE:
899 if (p->precision == 16) {
900 if (mode == AUMODE_PLAY)
901 p->sw_code =
902 swap_bytes_change_sign16_le;
903 else
904 p->sw_code =
905 change_sign16_swap_bytes_le;
906 } else {
907 if (mode == AUMODE_PLAY)
908 p->sw_code =
909 ulinear8_to_slinear16_le;
910 else
911 p->sw_code =
912 slinear16_to_ulinear8_le;
913 }
914 break;
915
916 case AUDIO_ENCODING_ULINEAR_LE:
917 if (p->precision == 16) {
918 p->sw_code = change_sign16_le;
919 } else {
920 if (mode == AUMODE_PLAY)
921 p->sw_code =
922 ulinear8_to_slinear16_le;
923 else
924 p->sw_code =
925 slinear16_to_ulinear8_le;
926 }
927 break;
928
929 case AUDIO_ENCODING_ULAW:
930 if (mode == AUMODE_PLAY) {
931 p->sw_code = mulaw_to_slinear16_le;
932 } else {
933 p->sw_code = slinear16_to_mulaw_le;
934 }
935 break;
936
937 case AUDIO_ENCODING_ALAW:
938 if (mode == AUMODE_PLAY) {
939 p->sw_code = alaw_to_slinear16_le;
940 } else {
941 p->sw_code = slinear16_to_alaw_le;
942 }
943 break;
944
945 default:
946 return (EINVAL);
947 }
948
949 if (IS_FIXED_RATE(sc->codec_if)) {
950 p->hw_sample_rate = AC97_SINGLE_RATE;
951 /* If hw_sample_rate is changed, aurateconv works. */
952 } else {
953 if (auich_set_rate(sc, mode, p->sample_rate))
954 return EINVAL;
955 }
956 if (mode == AUMODE_PLAY) {
957 control = bus_space_read_4(sc->iot, sc->aud_ioh, ICH_GCTRL);
958 control &= ~ICH_PCM246_MASK;
959 if (p->channels == 4) {
960 control |= ICH_PCM4;
961 } else if (p->channels == 6) {
962 control |= ICH_PCM6;
963 }
964 bus_space_write_4(sc->iot, sc->aud_ioh, ICH_GCTRL, control);
965 }
966 }
967
968 return (0);
969 }
970
971 int
972 auich_round_blocksize(void *v, int blk)
973 {
974
975 return (blk & ~0x3f); /* keep good alignment */
976 }
977
978 int
979 auich_halt_output(void *v)
980 {
981 struct auich_softc *sc = v;
982
983 DPRINTF(ICH_DEBUG_DMA, ("%s: halt_output\n", sc->sc_dev.dv_xname));
984
985 bus_space_write_1(sc->iot, sc->aud_ioh, ICH_PCMO + ICH_CTRL, ICH_RR);
986 sc->sc_pintr = NULL;
987
988 return (0);
989 }
990
991 int
992 auich_halt_input(void *v)
993 {
994 struct auich_softc *sc = v;
995
996 DPRINTF(ICH_DEBUG_DMA,
997 ("%s: halt_input\n", sc->sc_dev.dv_xname));
998
999 /* XXX halt both unless known otherwise */
1000
1001 bus_space_write_1(sc->iot, sc->aud_ioh, ICH_PCMI + ICH_CTRL, ICH_RR);
1002 bus_space_write_1(sc->iot, sc->aud_ioh, ICH_MICI + ICH_CTRL, ICH_RR);
1003 sc->sc_rintr = NULL;
1004
1005 return (0);
1006 }
1007
1008 int
1009 auich_getdev(void *v, struct audio_device *adp)
1010 {
1011 struct auich_softc *sc = v;
1012
1013 *adp = sc->sc_audev;
1014 return (0);
1015 }
1016
1017 int
1018 auich_set_port(void *v, mixer_ctrl_t *cp)
1019 {
1020 struct auich_softc *sc = v;
1021
1022 return (sc->codec_if->vtbl->mixer_set_port(sc->codec_if, cp));
1023 }
1024
1025 int
1026 auich_get_port(void *v, mixer_ctrl_t *cp)
1027 {
1028 struct auich_softc *sc = v;
1029
1030 return (sc->codec_if->vtbl->mixer_get_port(sc->codec_if, cp));
1031 }
1032
1033 int
1034 auich_query_devinfo(void *v, mixer_devinfo_t *dp)
1035 {
1036 struct auich_softc *sc = v;
1037
1038 return (sc->codec_if->vtbl->query_devinfo(sc->codec_if, dp));
1039 }
1040
1041 void *
1042 auich_allocm(void *v, int direction, size_t size, struct malloc_type *pool,
1043 int flags)
1044 {
1045 struct auich_softc *sc = v;
1046 struct auich_dma *p;
1047 int error;
1048
1049 if (size > (ICH_DMALIST_MAX * ICH_DMASEG_MAX))
1050 return (NULL);
1051
1052 p = malloc(sizeof(*p), pool, flags|M_ZERO);
1053 if (p == NULL)
1054 return (NULL);
1055
1056 error = auich_allocmem(sc, size, 0, p);
1057 if (error) {
1058 free(p, pool);
1059 return (NULL);
1060 }
1061
1062 p->next = sc->sc_dmas;
1063 sc->sc_dmas = p;
1064
1065 return (KERNADDR(p));
1066 }
1067
1068 void
1069 auich_freem(void *v, void *ptr, struct malloc_type *pool)
1070 {
1071 struct auich_softc *sc = v;
1072 struct auich_dma *p, **pp;
1073
1074 for (pp = &sc->sc_dmas; (p = *pp) != NULL; pp = &p->next) {
1075 if (KERNADDR(p) == ptr) {
1076 auich_freemem(sc, p);
1077 *pp = p->next;
1078 free(p, pool);
1079 return;
1080 }
1081 }
1082 }
1083
1084 size_t
1085 auich_round_buffersize(void *v, int direction, size_t size)
1086 {
1087
1088 if (size > (ICH_DMALIST_MAX * ICH_DMASEG_MAX))
1089 size = ICH_DMALIST_MAX * ICH_DMASEG_MAX;
1090
1091 return size;
1092 }
1093
1094 paddr_t
1095 auich_mappage(void *v, void *mem, off_t off, int prot)
1096 {
1097 struct auich_softc *sc = v;
1098 struct auich_dma *p;
1099
1100 if (off < 0)
1101 return (-1);
1102
1103 for (p = sc->sc_dmas; p && KERNADDR(p) != mem; p = p->next)
1104 ;
1105 if (!p)
1106 return (-1);
1107 return (bus_dmamem_mmap(sc->dmat, p->segs, p->nsegs,
1108 off, prot, BUS_DMA_WAITOK));
1109 }
1110
1111 int
1112 auich_get_props(void *v)
1113 {
1114 struct auich_softc *sc = v;
1115 int props;
1116
1117 props = AUDIO_PROP_INDEPENDENT | AUDIO_PROP_FULLDUPLEX;
1118 /*
1119 * Even if the codec is fixed-rate, set_param() succeeds for any sample
1120 * rate because of aurateconv. Applications can't know what rate the
1121 * device can process in the case of mmap().
1122 */
1123 if (!IS_FIXED_RATE(sc->codec_if))
1124 props |= AUDIO_PROP_MMAP;
1125 return props;
1126 }
1127
1128 int
1129 auich_intr(void *v)
1130 {
1131 struct auich_softc *sc = v;
1132 int ret = 0, sts, gsts, i;
1133
1134 #ifdef DIAGNOSTIC
1135 int csts;
1136 #endif
1137
1138 #ifdef DIAGNOSTIC
1139 csts = pci_conf_read(sc->sc_pc, sc->sc_pt, PCI_COMMAND_STATUS_REG);
1140 if (csts & PCI_STATUS_MASTER_ABORT) {
1141 printf("auich_intr: PCI master abort\n");
1142 }
1143 #endif
1144
1145 gsts = bus_space_read_4(sc->iot, sc->aud_ioh, ICH_GSTS);
1146 DPRINTF(ICH_DEBUG_INTR, ("auich_intr: gsts=0x%x\n", gsts));
1147
1148 if (gsts & ICH_POINT) {
1149 sts = bus_space_read_2(sc->iot, sc->aud_ioh,
1150 ICH_PCMO + sc->sc_sts_reg);
1151 DPRINTF(ICH_DEBUG_INTR,
1152 ("auich_intr: osts=0x%x\n", sts));
1153
1154 if (sts & ICH_FIFOE) {
1155 printf("%s: fifo underrun # %u\n",
1156 sc->sc_dev.dv_xname, ++sc->pcmo_fifoe);
1157 }
1158
1159 i = bus_space_read_1(sc->iot, sc->aud_ioh, ICH_PCMO + ICH_CIV);
1160 if (sts & (ICH_BCIS | ICH_LVBCI | ICH_CELV)) {
1161 struct auich_dmalist *q;
1162 int blksize, qptr;
1163
1164 blksize = sc->pcmo_blksize;
1165 qptr = sc->ptr_pcmo;
1166
1167 while (qptr != i) {
1168 q = &sc->dmalist_pcmo[qptr];
1169
1170 q->base = sc->pcmo_p;
1171 q->len = (blksize / sc->sc_sample_size) |
1172 ICH_DMAF_IOC;
1173 DPRINTF(ICH_DEBUG_INTR,
1174 ("auich_intr: %p, %p = %x @ 0x%x\n",
1175 &sc->dmalist_pcmo[i], q, blksize / 2,
1176 sc->pcmo_p));
1177
1178 sc->pcmo_p += blksize;
1179 if (sc->pcmo_p >= sc->pcmo_end)
1180 sc->pcmo_p = sc->pcmo_start;
1181
1182 if (++qptr == ICH_DMALIST_MAX)
1183 qptr = 0;
1184 if (sc->sc_pintr)
1185 sc->sc_pintr(sc->sc_parg);
1186 }
1187
1188 sc->ptr_pcmo = qptr;
1189 bus_space_write_1(sc->iot, sc->aud_ioh,
1190 ICH_PCMO + ICH_LVI, (qptr - 1) & ICH_LVI_MASK);
1191 }
1192
1193 /* int ack */
1194 bus_space_write_2(sc->iot, sc->aud_ioh, ICH_PCMO +
1195 sc->sc_sts_reg, sts & (ICH_CELV | ICH_LVBCI | ICH_BCIS | ICH_FIFOE));
1196 bus_space_write_4(sc->iot, sc->aud_ioh, ICH_GSTS, ICH_POINT);
1197 ret++;
1198 }
1199
1200 if (gsts & ICH_PIINT) {
1201 sts = bus_space_read_2(sc->iot, sc->aud_ioh,
1202 ICH_PCMI + sc->sc_sts_reg);
1203 DPRINTF(ICH_DEBUG_INTR,
1204 ("auich_intr: ists=0x%x\n", sts));
1205
1206 if (sts & ICH_FIFOE) {
1207 printf("%s: fifo overrun # %u\n",
1208 sc->sc_dev.dv_xname, ++sc->pcmi_fifoe);
1209 }
1210
1211 i = bus_space_read_1(sc->iot, sc->aud_ioh, ICH_PCMI + ICH_CIV);
1212 if (sts & (ICH_BCIS | ICH_LVBCI | ICH_CELV)) {
1213 struct auich_dmalist *q;
1214 int blksize, qptr;
1215
1216 blksize = sc->pcmi_blksize;
1217 qptr = sc->ptr_pcmi;
1218
1219 while (qptr != i) {
1220 q = &sc->dmalist_pcmi[qptr];
1221
1222 q->base = sc->pcmi_p;
1223 q->len = (blksize / sc->sc_sample_size) |
1224 ICH_DMAF_IOC;
1225 DPRINTF(ICH_DEBUG_INTR,
1226 ("auich_intr: %p, %p = %x @ 0x%x\n",
1227 &sc->dmalist_pcmi[i], q, blksize / 2,
1228 sc->pcmi_p));
1229
1230 sc->pcmi_p += blksize;
1231 if (sc->pcmi_p >= sc->pcmi_end)
1232 sc->pcmi_p = sc->pcmi_start;
1233
1234 if (++qptr == ICH_DMALIST_MAX)
1235 qptr = 0;
1236 if (sc->sc_rintr)
1237 sc->sc_rintr(sc->sc_rarg);
1238 }
1239
1240 sc->ptr_pcmi = qptr;
1241 bus_space_write_1(sc->iot, sc->aud_ioh,
1242 ICH_PCMI + ICH_LVI, (qptr - 1) & ICH_LVI_MASK);
1243 }
1244
1245 /* int ack */
1246 bus_space_write_2(sc->iot, sc->aud_ioh, ICH_PCMI +
1247 sc->sc_sts_reg, sts & (ICH_CELV | ICH_LVBCI | ICH_BCIS | ICH_FIFOE));
1248 bus_space_write_4(sc->iot, sc->aud_ioh, ICH_GSTS, ICH_PIINT);
1249 ret++;
1250 }
1251
1252 if (gsts & ICH_MIINT) {
1253 sts = bus_space_read_2(sc->iot, sc->aud_ioh,
1254 ICH_MICI + sc->sc_sts_reg);
1255 DPRINTF(ICH_DEBUG_INTR,
1256 ("auich_intr: ists=0x%x\n", sts));
1257 if (sts & ICH_FIFOE)
1258 printf("%s: fifo overrun\n", sc->sc_dev.dv_xname);
1259
1260 /* TODO mic input DMA */
1261
1262 bus_space_write_4(sc->iot, sc->aud_ioh, ICH_GSTS, ICH_MIINT);
1263 }
1264
1265 return ret;
1266 }
1267
1268 int
1269 auich_trigger_output(void *v, void *start, void *end, int blksize,
1270 void (*intr)(void *), void *arg, struct audio_params *param)
1271 {
1272 struct auich_softc *sc = v;
1273 struct auich_dmalist *q;
1274 struct auich_dma *p;
1275 size_t size;
1276 int qptr;
1277 #ifdef DIAGNOSTIC
1278 int csts;
1279 #endif
1280
1281 DPRINTF(ICH_DEBUG_DMA,
1282 ("auich_trigger_output(%p, %p, %d, %p, %p, %p)\n",
1283 start, end, blksize, intr, arg, param));
1284
1285 sc->sc_pintr = intr;
1286 sc->sc_parg = arg;
1287 #ifdef DIAGNOSTIC
1288 csts = pci_conf_read(sc->sc_pc, sc->sc_pt, PCI_COMMAND_STATUS_REG);
1289 if (csts & PCI_STATUS_MASTER_ABORT) {
1290 printf("auich_trigger_output: PCI master abort\n");
1291 }
1292 #endif
1293
1294 for (p = sc->sc_dmas; p && KERNADDR(p) != start; p = p->next)
1295 ;
1296 if (!p) {
1297 printf("auich_trigger_output: bad addr %p\n", start);
1298 return (EINVAL);
1299 }
1300
1301 size = (size_t)((caddr_t)end - (caddr_t)start);
1302
1303 /*
1304 * The logic behind this is:
1305 * setup one buffer to play, then LVI dump out the rest
1306 * to the scatter-gather chain.
1307 */
1308 sc->pcmo_start = DMAADDR(p);
1309 sc->pcmo_p = sc->pcmo_start;
1310 sc->pcmo_end = sc->pcmo_start + size;
1311 sc->pcmo_blksize = blksize;
1312
1313 for (qptr = 0; qptr < ICH_DMALIST_MAX; qptr++) {
1314 q = &sc->dmalist_pcmo[qptr];
1315
1316 q->base = sc->pcmo_p;
1317 q->len = (blksize / sc->sc_sample_size) | ICH_DMAF_IOC;
1318
1319 sc->pcmo_p += blksize;
1320 if (sc->pcmo_p >= sc->pcmo_end)
1321 sc->pcmo_p = sc->pcmo_start;
1322 }
1323
1324 sc->ptr_pcmo = qptr = 0;
1325 bus_space_write_1(sc->iot, sc->aud_ioh, ICH_PCMO + ICH_LVI,
1326 (qptr - 1) & ICH_LVI_MASK);
1327
1328 bus_space_write_4(sc->iot, sc->aud_ioh, ICH_PCMO + ICH_BDBAR,
1329 sc->sc_cddma + ICH_PCMO_OFF(0));
1330 bus_space_write_1(sc->iot, sc->aud_ioh, ICH_PCMO + ICH_CTRL,
1331 ICH_IOCE | ICH_FEIE | ICH_LVBIE | ICH_RPBM);
1332
1333 return (0);
1334 }
1335
1336 int
1337 auich_trigger_input(v, start, end, blksize, intr, arg, param)
1338 void *v;
1339 void *start, *end;
1340 int blksize;
1341 void (*intr)(void *);
1342 void *arg;
1343 struct audio_params *param;
1344 {
1345 struct auich_softc *sc = v;
1346 struct auich_dmalist *q;
1347 struct auich_dma *p;
1348 size_t size;
1349 int qptr;
1350 #ifdef DIAGNOSTIC
1351 int csts;
1352 #endif
1353
1354 DPRINTF(ICH_DEBUG_DMA,
1355 ("auich_trigger_input(%p, %p, %d, %p, %p, %p)\n",
1356 start, end, blksize, intr, arg, param));
1357
1358 sc->sc_rintr = intr;
1359 sc->sc_rarg = arg;
1360
1361 #ifdef DIAGNOSTIC
1362 csts = pci_conf_read(sc->sc_pc, sc->sc_pt, PCI_COMMAND_STATUS_REG);
1363 if (csts & PCI_STATUS_MASTER_ABORT) {
1364 printf("auich_trigger_input: PCI master abort\n");
1365 }
1366 #endif
1367
1368 for (p = sc->sc_dmas; p && KERNADDR(p) != start; p = p->next)
1369 ;
1370 if (!p) {
1371 printf("auich_trigger_input: bad addr %p\n", start);
1372 return (EINVAL);
1373 }
1374
1375 size = (size_t)((caddr_t)end - (caddr_t)start);
1376
1377 /*
1378 * The logic behind this is:
1379 * setup one buffer to play, then LVI dump out the rest
1380 * to the scatter-gather chain.
1381 */
1382 sc->pcmi_start = DMAADDR(p);
1383 sc->pcmi_p = sc->pcmi_start;
1384 sc->pcmi_end = sc->pcmi_start + size;
1385 sc->pcmi_blksize = blksize;
1386
1387 for (qptr = 0; qptr < ICH_DMALIST_MAX; qptr++) {
1388 q = &sc->dmalist_pcmi[qptr];
1389
1390 q->base = sc->pcmi_p;
1391 q->len = (blksize / sc->sc_sample_size) | ICH_DMAF_IOC;
1392
1393 sc->pcmi_p += blksize;
1394 if (sc->pcmi_p >= sc->pcmi_end)
1395 sc->pcmi_p = sc->pcmi_start;
1396 }
1397
1398 sc->ptr_pcmi = qptr = 0;
1399 bus_space_write_1(sc->iot, sc->aud_ioh, ICH_PCMI + ICH_LVI,
1400 (qptr - 1) & ICH_LVI_MASK);
1401
1402 bus_space_write_4(sc->iot, sc->aud_ioh, ICH_PCMI + ICH_BDBAR,
1403 sc->sc_cddma + ICH_PCMI_OFF(0));
1404 bus_space_write_1(sc->iot, sc->aud_ioh, ICH_PCMI + ICH_CTRL,
1405 ICH_IOCE | ICH_FEIE | ICH_LVBIE | ICH_RPBM);
1406
1407 return (0);
1408 }
1409
1410 int
1411 auich_allocmem(struct auich_softc *sc, size_t size, size_t align,
1412 struct auich_dma *p)
1413 {
1414 int error;
1415
1416 p->size = size;
1417 error = bus_dmamem_alloc(sc->dmat, p->size, align, 0,
1418 p->segs, sizeof(p->segs)/sizeof(p->segs[0]),
1419 &p->nsegs, BUS_DMA_NOWAIT);
1420 if (error)
1421 return (error);
1422
1423 error = bus_dmamem_map(sc->dmat, p->segs, p->nsegs, p->size,
1424 &p->addr, BUS_DMA_NOWAIT|sc->sc_dmamap_flags);
1425 if (error)
1426 goto free;
1427
1428 error = bus_dmamap_create(sc->dmat, p->size, 1, p->size,
1429 0, BUS_DMA_NOWAIT, &p->map);
1430 if (error)
1431 goto unmap;
1432
1433 error = bus_dmamap_load(sc->dmat, p->map, p->addr, p->size, NULL,
1434 BUS_DMA_NOWAIT);
1435 if (error)
1436 goto destroy;
1437 return (0);
1438
1439 destroy:
1440 bus_dmamap_destroy(sc->dmat, p->map);
1441 unmap:
1442 bus_dmamem_unmap(sc->dmat, p->addr, p->size);
1443 free:
1444 bus_dmamem_free(sc->dmat, p->segs, p->nsegs);
1445 return (error);
1446 }
1447
1448 int
1449 auich_freemem(struct auich_softc *sc, struct auich_dma *p)
1450 {
1451
1452 bus_dmamap_unload(sc->dmat, p->map);
1453 bus_dmamap_destroy(sc->dmat, p->map);
1454 bus_dmamem_unmap(sc->dmat, p->addr, p->size);
1455 bus_dmamem_free(sc->dmat, p->segs, p->nsegs);
1456 return (0);
1457 }
1458
1459 int
1460 auich_alloc_cdata(struct auich_softc *sc)
1461 {
1462 bus_dma_segment_t seg;
1463 int error, rseg;
1464
1465 /*
1466 * Allocate the control data structure, and create and load the
1467 * DMA map for it.
1468 */
1469 if ((error = bus_dmamem_alloc(sc->dmat,
1470 sizeof(struct auich_cdata),
1471 PAGE_SIZE, 0, &seg, 1, &rseg, 0)) != 0) {
1472 printf("%s: unable to allocate control data, error = %d\n",
1473 sc->sc_dev.dv_xname, error);
1474 goto fail_0;
1475 }
1476
1477 if ((error = bus_dmamem_map(sc->dmat, &seg, rseg,
1478 sizeof(struct auich_cdata),
1479 (caddr_t *) &sc->sc_cdata,
1480 sc->sc_dmamap_flags)) != 0) {
1481 printf("%s: unable to map control data, error = %d\n",
1482 sc->sc_dev.dv_xname, error);
1483 goto fail_1;
1484 }
1485
1486 if ((error = bus_dmamap_create(sc->dmat, sizeof(struct auich_cdata), 1,
1487 sizeof(struct auich_cdata), 0, 0,
1488 &sc->sc_cddmamap)) != 0) {
1489 printf("%s: unable to create control data DMA map, "
1490 "error = %d\n", sc->sc_dev.dv_xname, error);
1491 goto fail_2;
1492 }
1493
1494 if ((error = bus_dmamap_load(sc->dmat, sc->sc_cddmamap,
1495 sc->sc_cdata, sizeof(struct auich_cdata),
1496 NULL, 0)) != 0) {
1497 printf("%s: unable tp load control data DMA map, "
1498 "error = %d\n", sc->sc_dev.dv_xname, error);
1499 goto fail_3;
1500 }
1501
1502 return (0);
1503
1504 fail_3:
1505 bus_dmamap_destroy(sc->dmat, sc->sc_cddmamap);
1506 fail_2:
1507 bus_dmamem_unmap(sc->dmat, (caddr_t) sc->sc_cdata,
1508 sizeof(struct auich_cdata));
1509 fail_1:
1510 bus_dmamem_free(sc->dmat, &seg, rseg);
1511 fail_0:
1512 return (error);
1513 }
1514
1515 void
1516 auich_powerhook(int why, void *addr)
1517 {
1518 struct auich_softc *sc = (struct auich_softc *)addr;
1519
1520 switch (why) {
1521 case PWR_SUSPEND:
1522 case PWR_STANDBY:
1523 /* Power down */
1524 DPRINTF(1, ("%s: power down\n", sc->sc_dev.dv_xname));
1525 sc->sc_suspend = why;
1526 break;
1527
1528 case PWR_RESUME:
1529 /* Wake up */
1530 DPRINTF(1, ("%s: power resume\n", sc->sc_dev.dv_xname));
1531 if (sc->sc_suspend == PWR_RESUME) {
1532 printf("%s: resume without suspend.\n",
1533 sc->sc_dev.dv_xname);
1534 sc->sc_suspend = why;
1535 return;
1536 }
1537 sc->sc_suspend = why;
1538 auich_reset_codec(sc);
1539 DELAY(1000);
1540 (sc->codec_if->vtbl->restore_ports)(sc->codec_if);
1541 break;
1542
1543 case PWR_SOFTSUSPEND:
1544 case PWR_SOFTSTANDBY:
1545 case PWR_SOFTRESUME:
1546 break;
1547 }
1548 }
1549
1550 /*
1551 * Calibrate card (some boards are overclocked and need scaling)
1552 */
1553 void
1554 auich_calibrate(struct auich_softc *sc)
1555 {
1556 struct timeval t1, t2;
1557 uint8_t ociv, nciv;
1558 uint64_t wait_us;
1559 uint32_t actual_48k_rate, bytes, ac97rate;
1560 void *temp_buffer;
1561 struct auich_dma *p;
1562 u_long rate;
1563
1564 /*
1565 * Grab audio from input for fixed interval and compare how
1566 * much we actually get with what we expect. Interval needs
1567 * to be sufficiently short that no interrupts are
1568 * generated.
1569 */
1570
1571 /* Force the codec to a known state first. */
1572 sc->codec_if->vtbl->set_clock(sc->codec_if, 48000);
1573 rate = 48000;
1574 sc->codec_if->vtbl->set_rate(sc->codec_if, AC97_REG_PCM_LR_ADC_RATE,
1575 &rate);
1576
1577 /* Setup a buffer */
1578 bytes = 64000;
1579 temp_buffer = auich_allocm(sc, AUMODE_RECORD, bytes, M_DEVBUF, M_WAITOK);
1580
1581 for (p = sc->sc_dmas; p && KERNADDR(p) != temp_buffer; p = p->next)
1582 ;
1583 if (p == NULL) {
1584 printf("auich_calibrate: bad address %p\n", temp_buffer);
1585 return;
1586 }
1587 sc->dmalist_pcmi[0].base = DMAADDR(p);
1588 sc->dmalist_pcmi[0].len = (bytes / sc->sc_sample_size);
1589
1590 /*
1591 * our data format is stereo, 16 bit so each sample is 4 bytes.
1592 * assuming we get 48000 samples per second, we get 192000 bytes/sec.
1593 * we're going to start recording with interrupts disabled and measure
1594 * the time taken for one block to complete. we know the block size,
1595 * we know the time in microseconds, we calculate the sample rate:
1596 *
1597 * actual_rate [bps] = bytes / (time [s] * 4)
1598 * actual_rate [bps] = (bytes * 1000000) / (time [us] * 4)
1599 * actual_rate [Hz] = (bytes * 250000) / time [us]
1600 */
1601
1602 /* prepare */
1603 ociv = bus_space_read_1(sc->iot, sc->aud_ioh, ICH_PCMI + ICH_CIV);
1604 bus_space_write_4(sc->iot, sc->aud_ioh, ICH_PCMI + ICH_BDBAR,
1605 sc->sc_cddma + ICH_PCMI_OFF(0));
1606 bus_space_write_1(sc->iot, sc->aud_ioh, ICH_PCMI + ICH_LVI,
1607 (0 - 1) & ICH_LVI_MASK);
1608
1609 /* start */
1610 microtime(&t1);
1611 bus_space_write_1(sc->iot, sc->aud_ioh, ICH_PCMI + ICH_CTRL, ICH_RPBM);
1612
1613 /* wait */
1614 nciv = ociv;
1615 do {
1616 microtime(&t2);
1617 if (t2.tv_sec - t1.tv_sec > 1)
1618 break;
1619 nciv = bus_space_read_1(sc->iot, sc->aud_ioh,
1620 ICH_PCMI + ICH_CIV);
1621 } while (nciv == ociv);
1622 microtime(&t2);
1623
1624 /* stop */
1625 bus_space_write_1(sc->iot, sc->aud_ioh, ICH_PCMI + ICH_CTRL, 0);
1626
1627 /* reset */
1628 DELAY(100);
1629 bus_space_write_1(sc->iot, sc->aud_ioh, ICH_PCMI + ICH_CTRL, ICH_RR);
1630
1631 /* turn time delta into us */
1632 wait_us = ((t2.tv_sec - t1.tv_sec) * 1000000) + t2.tv_usec - t1.tv_usec;
1633
1634 auich_freem(sc, temp_buffer, M_DEVBUF);
1635
1636 if (nciv == ociv) {
1637 printf("%s: ac97 link rate calibration timed out after %"
1638 PRIu64 " us\n", sc->sc_dev.dv_xname, wait_us);
1639 return;
1640 }
1641
1642 actual_48k_rate = (bytes * UINT64_C(250000)) / wait_us;
1643
1644 if (actual_48k_rate < 50000)
1645 ac97rate = 48000;
1646 else
1647 ac97rate = ((actual_48k_rate + 500) / 1000) * 1000;
1648
1649 printf("%s: measured ac97 link rate at %d Hz",
1650 sc->sc_dev.dv_xname, actual_48k_rate);
1651 if (ac97rate != actual_48k_rate)
1652 printf(", will use %d Hz", ac97rate);
1653 printf("\n");
1654
1655 sc->sc_ac97_clock = ac97rate;
1656 }
1657