xref: /netbsd-src/sys/dev/pci/neo.c (revision ba65fde2d7fefa7d39838fa5fa855e62bd606b5e)
1 /*	$NetBSD: neo.c,v 1.49 2012/10/27 17:18:35 chs Exp $	*/
2 
3 /*
4  * Copyright (c) 1999 Cameron Grant <gandalf@vilnya.demon.co.uk>
5  * All rights reserved.
6  *
7  * Derived from the public domain Linux driver
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHERIN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THEPOSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  * FreeBSD: src/sys/dev/sound/pci/neomagic.c,v 1.8 2000/03/20 15:30:50 cg Exp
31  * OpenBSD: neo.c,v 1.4 2000/07/19 09:04:37 csapuntz Exp
32  */
33 
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: neo.c,v 1.49 2012/10/27 17:18:35 chs Exp $");
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/kmem.h>
41 #include <sys/device.h>
42 #include <sys/bus.h>
43 #include <sys/audioio.h>
44 
45 #include <dev/audio_if.h>
46 #include <dev/mulaw.h>
47 #include <dev/auconv.h>
48 
49 #include <dev/ic/ac97var.h>
50 
51 #include <dev/pci/pcidevs.h>
52 #include <dev/pci/pcivar.h>
53 #include <dev/pci/neoreg.h>
54 #include <dev/pci/neo-coeff.h>
55 
56 /* -------------------------------------------------------------------- */
57 /*
58  * As of 04/13/00, public documentation on the Neomagic 256 is not available.
59  * These comments were gleaned by looking at the driver carefully.
60  *
61  * The Neomagic 256 AV/ZX chips provide both video and audio capabilities
62  * on one chip. About 2-6 megabytes of memory are associated with
63  * the chip. Most of this goes to video frame buffers, but some is used for
64  * audio buffering
65  *
66  * Unlike most PCI audio chips, the Neomagic chip does not rely on DMA.
67  * Instead, the chip allows you to carve out two ring buffers out of its
68  * memory. However you carve this and how much you can carve seems to be
69  * voodoo. The algorithm is in nm_init.
70  *
71  * Most Neomagic audio chips use the AC-97 codec interface. However, there
72  * seem to be a select few chips 256AV chips that do not support AC-97.
73  * This driver does not support them but there are rumors that it
74  * might work with wss isa drivers. This might require some playing around
75  * with your BIOS.
76  *
77  * The Neomagic 256 AV/ZX have 2 PCI I/O region descriptors. Both of
78  * them describe a memory region. The frame buffer is the first region
79  * and the register set is the second region.
80  *
81  * The register manipulation logic is taken from the Linux driver,
82  * which is in the public domain.
83  *
84  * The Neomagic is even nice enough to map the AC-97 codec registers into
85  * the register space to allow direct manipulation. Watch out, accessing
86  * AC-97 registers on the Neomagic requires great delicateness, otherwise
87  * the thing will hang the PCI bus, rendering your system frozen.
88  *
89  * For one, it seems the Neomagic status register that reports AC-97
90  * readiness should NOT be polled more often than once each 1ms.
91  *
92  * Also, writes to the AC-97 register space may take order 40us to
93  * complete.
94  *
95  * Unlike many sound engines, the Neomagic does not support (as far as
96  * we know :) the notion of interrupting every n bytes transferred,
97  * unlike many DMA engines.  Instead, it allows you to specify one
98  * location in each ring buffer (called the watermark). When the chip
99  * passes that location while playing, it signals an interrupt.
100  *
101  * The ring buffer size is currently 16k. That is about 100ms of audio
102  * at 44.1kHz/stero/16 bit. However, to keep the buffer full, interrupts
103  * are generated more often than that, so 20-40 interrupts per second
104  * should not be unexpected. Increasing BUFFSIZE should help minimize
105  * of glitches due to drivers that spend to much time looping at high
106  * privelege levels as well as the impact of badly written audio
107  * interface clients.
108  *
109  * TO-DO list:
110  *    Figure out interaction with video stuff (look at Xfree86 driver?)
111  *
112  *    Figure out how to shrink that huge table neo-coeff.h
113  */
114 
115 #define	NM_BUFFSIZE	16384
116 
117 /* device private data */
118 struct neo_softc {
119 	device_t	dev;
120 	kmutex_t	lock;
121 	kmutex_t	intr_lock;
122 
123 	bus_space_tag_t bufiot;
124 	bus_space_handle_t bufioh;
125 
126 	bus_space_tag_t regiot;
127 	bus_space_handle_t regioh;
128 
129 	uint32_t	type;
130 	void		*ih;
131 
132 	void	(*pintr)(void *);	/* DMA completion intr handler */
133 	void	*parg;		/* arg for intr() */
134 
135 	void	(*rintr)(void *);	/* DMA completion intr handler */
136 	void	*rarg;		/* arg for intr() */
137 
138 	vaddr_t	buf_vaddr;
139 	vaddr_t	rbuf_vaddr;
140 	vaddr_t	pbuf_vaddr;
141 	int	pbuf_allocated;
142 	int	rbuf_allocated;
143 
144 	bus_addr_t buf_pciaddr;
145 	bus_addr_t rbuf_pciaddr;
146 	bus_addr_t pbuf_pciaddr;
147 
148 	uint32_t	ac97_base, ac97_status, ac97_busy;
149 	uint32_t	buftop, pbuf, rbuf, cbuf, acbuf;
150 	uint32_t	playint, recint, misc1int, misc2int;
151 	uint32_t	irsz, badintr;
152 
153 	uint32_t	pbufsize;
154 	uint32_t	rbufsize;
155 
156 	uint32_t	pblksize;
157 	uint32_t	rblksize;
158 
159 	uint32_t	pwmark;
160 	uint32_t	rwmark;
161 
162 	struct ac97_codec_if *codec_if;
163 	struct ac97_host_if host_if;
164 };
165 
166 /* -------------------------------------------------------------------- */
167 
168 /*
169  * prototypes
170  */
171 
172 static int	nm_waitcd(struct neo_softc *);
173 static int	nm_loadcoeff(struct neo_softc *, int, int);
174 static int	nm_init(struct neo_softc *);
175 
176 static int	neo_match(device_t, cfdata_t, void *);
177 static void	neo_attach(device_t, device_t, void *);
178 static int	neo_intr(void *);
179 
180 static int	neo_query_encoding(void *, struct audio_encoding *);
181 static int	neo_set_params(void *, int, int, audio_params_t *,
182 			       audio_params_t *, stream_filter_list_t *,
183 			       stream_filter_list_t *);
184 static int	neo_round_blocksize(void *, int, int, const audio_params_t *);
185 static int	neo_trigger_output(void *, void *, void *, int,
186 				   void (*)(void *), void *,
187 				   const audio_params_t *);
188 static int	neo_trigger_input(void *, void *, void *, int,
189 				  void (*)(void *), void *,
190 				  const audio_params_t *);
191 static int	neo_halt_output(void *);
192 static int	neo_halt_input(void *);
193 static int	neo_getdev(void *, struct audio_device *);
194 static int	neo_mixer_set_port(void *, mixer_ctrl_t *);
195 static int	neo_mixer_get_port(void *, mixer_ctrl_t *);
196 static int	neo_attach_codec(void *, struct ac97_codec_if *);
197 static int	neo_read_codec(void *, uint8_t, uint16_t *);
198 static int	neo_write_codec(void *, uint8_t, uint16_t);
199 static int     neo_reset_codec(void *);
200 static enum ac97_host_flags neo_flags_codec(void *);
201 static int	neo_query_devinfo(void *, mixer_devinfo_t *);
202 static void *	neo_malloc(void *, int, size_t);
203 static void	neo_free(void *, void *, size_t);
204 static size_t	neo_round_buffersize(void *, int, size_t);
205 static paddr_t	neo_mappage(void *, void *, off_t, int);
206 static int	neo_get_props(void *);
207 static void	neo_get_locks(void *, kmutex_t **, kmutex_t **);
208 
209 CFATTACH_DECL_NEW(neo, sizeof(struct neo_softc),
210     neo_match, neo_attach, NULL, NULL);
211 
212 static struct audio_device neo_device = {
213 	"NeoMagic 256",
214 	"",
215 	"neo"
216 };
217 
218 /* The actual rates supported by the card. */
219 static const int samplerates[9] = {
220 	8000,
221 	11025,
222 	16000,
223 	22050,
224 	24000,
225 	32000,
226 	44100,
227 	48000,
228 	99999999
229 };
230 
231 #define NEO_NFORMATS	4
232 static const struct audio_format neo_formats[NEO_NFORMATS] = {
233 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
234 	 2, AUFMT_STEREO, 8, {8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000}},
235 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
236 	 1, AUFMT_MONAURAL, 8, {8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000}},
237 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_LE, 8, 8,
238 	 2, AUFMT_STEREO, 8, {8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000}},
239 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_LE, 8, 8,
240 	 1, AUFMT_MONAURAL, 8, {8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000}},
241 };
242 
243 /* -------------------------------------------------------------------- */
244 
245 static const struct audio_hw_if neo_hw_if = {
246 	NULL,				/* open */
247 	NULL,				/* close */
248 	NULL,				/* drain */
249 	neo_query_encoding,
250 	neo_set_params,
251 	neo_round_blocksize,
252 	NULL,				/* commit_setting */
253 	NULL,				/* init_output */
254 	NULL,				/* init_input */
255 	NULL,				/* start_output */
256 	NULL,				/* start_input */
257 	neo_halt_output,
258 	neo_halt_input,
259 	NULL,				/* speaker_ctl */
260 	neo_getdev,
261 	NULL,				/* getfd */
262 	neo_mixer_set_port,
263 	neo_mixer_get_port,
264 	neo_query_devinfo,
265 	neo_malloc,
266 	neo_free,
267 	neo_round_buffersize,
268 	neo_mappage,
269 	neo_get_props,
270 	neo_trigger_output,
271 	neo_trigger_input,
272 	NULL,
273 	neo_get_locks,
274 };
275 
276 /* -------------------------------------------------------------------- */
277 
278 #define	nm_rd_1(sc, regno)						\
279 	bus_space_read_1((sc)->regiot, (sc)->regioh, (regno))
280 
281 #define	nm_rd_2(sc, regno)						\
282 	bus_space_read_2((sc)->regiot, (sc)->regioh, (regno))
283 
284 #define	nm_rd_4(sc, regno)						\
285 	bus_space_read_4((sc)->regiot, (sc)->regioh, (regno))
286 
287 #define	nm_wr_1(sc, regno, val)						\
288 	bus_space_write_1((sc)->regiot, (sc)->regioh, (regno), (val))
289 
290 #define	nm_wr_2(sc, regno, val)						\
291 	bus_space_write_2((sc)->regiot, (sc)->regioh, (regno), (val))
292 
293 #define	nm_wr_4(sc, regno, val)						\
294 	bus_space_write_4((sc)->regiot, (sc)->regioh, (regno), (val))
295 
296 #define	nm_rdbuf_4(sc, regno)						\
297 	bus_space_read_4((sc)->bufiot, (sc)->bufioh, (regno))
298 
299 #define	nm_wrbuf_1(sc, regno, val)					\
300 	bus_space_write_1((sc)->bufiot, (sc)->bufioh, (regno), (val))
301 
302 /* ac97 codec */
303 static int
304 nm_waitcd(struct neo_softc *sc)
305 {
306 	int cnt;
307 	int fail;
308 
309 	cnt  = 10;
310 	fail = 1;
311 	while (cnt-- > 0) {
312 		if (nm_rd_2(sc, sc->ac97_status) & sc->ac97_busy)
313 			DELAY(100);
314 		else {
315 			fail = 0;
316 			break;
317 		}
318 	}
319 	return fail;
320 }
321 
322 static void
323 nm_ackint(struct neo_softc *sc, uint32_t num)
324 {
325 
326 	switch (sc->type) {
327 	case PCI_PRODUCT_NEOMAGIC_NMMM256AV_AU:
328 		nm_wr_2(sc, NM_INT_REG, num << 1);
329 		break;
330 
331 	case PCI_PRODUCT_NEOMAGIC_NMMM256ZX_AU:
332 		nm_wr_4(sc, NM_INT_REG, num);
333 		break;
334 	}
335 }
336 
337 static int
338 nm_loadcoeff(struct neo_softc *sc, int dir, int num)
339 {
340 	int ofs, sz, i;
341 	uint32_t addr;
342 
343 	addr = (dir == AUMODE_PLAY)? 0x01c : 0x21c;
344 	if (dir == AUMODE_RECORD)
345 		num += 8;
346 	sz = coefficientSizes[num];
347 	ofs = 0;
348 	while (num-- > 0)
349 		ofs+= coefficientSizes[num];
350 	for (i = 0; i < sz; i++)
351 		nm_wrbuf_1(sc, sc->cbuf + i, coefficients[ofs + i]);
352 	nm_wr_4(sc, addr, sc->cbuf);
353 	if (dir == AUMODE_PLAY)
354 		sz--;
355 	nm_wr_4(sc, addr + 4, sc->cbuf + sz);
356 	return 0;
357 }
358 
359 /* The interrupt handler */
360 static int
361 neo_intr(void *p)
362 {
363 	struct neo_softc *sc;
364 	int status, x;
365 	int rv;
366 
367 	sc = (struct neo_softc *)p;
368 	mutex_spin_enter(&sc->intr_lock);
369 
370 	rv = 0;
371 	status = (sc->irsz == 2) ?
372 	    nm_rd_2(sc, NM_INT_REG) :
373 	    nm_rd_4(sc, NM_INT_REG);
374 
375 	if (status & sc->playint) {
376 		status &= ~sc->playint;
377 
378 		sc->pwmark += sc->pblksize;
379 		sc->pwmark %= sc->pbufsize;
380 
381 		nm_wr_4(sc, NM_PBUFFER_WMARK, sc->pbuf + sc->pwmark);
382 
383 		nm_ackint(sc, sc->playint);
384 
385 		if (sc->pintr)
386 			(*sc->pintr)(sc->parg);
387 
388 		rv = 1;
389 	}
390 	if (status & sc->recint) {
391 		status &= ~sc->recint;
392 
393 		sc->rwmark += sc->rblksize;
394 		sc->rwmark %= sc->rbufsize;
395 		nm_wr_4(sc, NM_RBUFFER_WMARK, sc->rbuf + sc->rwmark);
396 		nm_ackint(sc, sc->recint);
397 		if (sc->rintr)
398 			(*sc->rintr)(sc->rarg);
399 
400 		rv = 1;
401 	}
402 	if (status & sc->misc1int) {
403 		status &= ~sc->misc1int;
404 		nm_ackint(sc, sc->misc1int);
405 		x = nm_rd_1(sc, 0x400);
406 		nm_wr_1(sc, 0x400, x | 2);
407 		printf("%s: misc int 1\n", device_xname(sc->dev));
408 		rv = 1;
409 	}
410 	if (status & sc->misc2int) {
411 		status &= ~sc->misc2int;
412 		nm_ackint(sc, sc->misc2int);
413 		x = nm_rd_1(sc, 0x400);
414 		nm_wr_1(sc, 0x400, x & ~2);
415 		printf("%s: misc int 2\n", device_xname(sc->dev));
416 		rv = 1;
417 	}
418 	if (status) {
419 		status &= ~sc->misc2int;
420 		nm_ackint(sc, sc->misc2int);
421 		printf("%s: unknown int\n", device_xname(sc->dev));
422 		rv = 1;
423 	}
424 
425 	mutex_spin_exit(&sc->intr_lock);
426 	return rv;
427 }
428 
429 /* -------------------------------------------------------------------- */
430 
431 /*
432  * Probe and attach the card
433  */
434 
435 static int
436 nm_init(struct neo_softc *sc)
437 {
438 	uint32_t ofs, i;
439 
440 	switch (sc->type) {
441 	case PCI_PRODUCT_NEOMAGIC_NMMM256AV_AU:
442 		sc->ac97_base = NM_MIXER_OFFSET;
443 		sc->ac97_status = NM_MIXER_STATUS_OFFSET;
444 		sc->ac97_busy = NM_MIXER_READY_MASK;
445 
446 		sc->buftop = 2560 * 1024;
447 
448 		sc->irsz = 2;
449 		sc->playint = NM_PLAYBACK_INT;
450 		sc->recint = NM_RECORD_INT;
451 		sc->misc1int = NM_MISC_INT_1;
452 		sc->misc2int = NM_MISC_INT_2;
453 		break;
454 
455 	case PCI_PRODUCT_NEOMAGIC_NMMM256ZX_AU:
456 		sc->ac97_base = NM_MIXER_OFFSET;
457 		sc->ac97_status = NM2_MIXER_STATUS_OFFSET;
458 		sc->ac97_busy = NM2_MIXER_READY_MASK;
459 
460 		sc->buftop = (nm_rd_2(sc, 0xa0b) ? 6144 : 4096) * 1024;
461 
462 		sc->irsz = 4;
463 		sc->playint = NM2_PLAYBACK_INT;
464 		sc->recint = NM2_RECORD_INT;
465 		sc->misc1int = NM2_MISC_INT_1;
466 		sc->misc2int = NM2_MISC_INT_2;
467 		break;
468 #ifdef DIAGNOSTIC
469 	default:
470 		panic("nm_init: impossible");
471 #endif
472 	}
473 
474 	sc->badintr = 0;
475 	ofs = sc->buftop - 0x0400;
476 	sc->buftop -= 0x1400;
477 
478 	if ((nm_rdbuf_4(sc, ofs) & NM_SIG_MASK) == NM_SIGNATURE) {
479 		i = nm_rdbuf_4(sc, ofs + 4);
480 		if (i != 0 && i != 0xffffffff)
481 			sc->buftop = i;
482 	}
483 
484 	sc->cbuf = sc->buftop - NM_MAX_COEFFICIENT;
485 	sc->rbuf = sc->cbuf - NM_BUFFSIZE;
486 	sc->pbuf = sc->rbuf - NM_BUFFSIZE;
487 	sc->acbuf = sc->pbuf - (NM_TOTAL_COEFF_COUNT * 4);
488 
489 	sc->buf_vaddr = (vaddr_t) bus_space_vaddr(sc->bufiot, sc->bufioh);
490 	sc->rbuf_vaddr = sc->buf_vaddr + sc->rbuf;
491 	sc->pbuf_vaddr = sc->buf_vaddr + sc->pbuf;
492 
493 	sc->rbuf_pciaddr = sc->buf_pciaddr + sc->rbuf;
494 	sc->pbuf_pciaddr = sc->buf_pciaddr + sc->pbuf;
495 
496 	nm_wr_1(sc, 0, 0x11);
497 	nm_wr_1(sc, NM_RECORD_ENABLE_REG, 0);
498 	nm_wr_2(sc, 0x214, 0);
499 
500 	return 0;
501 }
502 
503 static int
504 neo_match(device_t parent, cfdata_t match, void *aux)
505 {
506 	struct pci_attach_args *pa;
507 	pcireg_t subdev;
508 
509 	pa = aux;
510 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_NEOMAGIC)
511 		return 0;
512 
513 	subdev = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
514 
515 	switch (PCI_PRODUCT(pa->pa_id)) {
516 	case PCI_PRODUCT_NEOMAGIC_NMMM256AV_AU:
517 		/*
518 		 * We have to weed-out the non-AC'97 versions of
519 		 * the chip (i.e. the ones that are known to work
520 		 * in WSS emulation mode), as they won't work with
521 		 * this driver.
522 		 */
523 		switch (PCI_VENDOR(subdev)) {
524 		case PCI_VENDOR_DELL:
525 			switch (PCI_PRODUCT(subdev)) {
526 			case 0x008f:
527 				return 0;
528 			}
529 			break;
530 
531 		case PCI_VENDOR_HP:
532 			switch (PCI_PRODUCT(subdev)) {
533 			case 0x0007:
534 				return 0;
535 			}
536 			break;
537 
538 		case PCI_VENDOR_IBM:
539 			switch (PCI_PRODUCT(subdev)) {
540 			case 0x00dd:
541 				return 0;
542 			}
543 			break;
544 		}
545 		return 1;
546 
547 	case PCI_PRODUCT_NEOMAGIC_NMMM256ZX_AU:
548 		return 1;
549 	}
550 
551 	return 0;
552 }
553 
554 static bool
555 neo_resume(device_t dv, const pmf_qual_t *qual)
556 {
557 	struct neo_softc *sc = device_private(dv);
558 
559 	mutex_enter(&sc->lock);
560 	mutex_spin_enter(&sc->intr_lock);
561 	nm_init(sc);
562 	mutex_spin_exit(&sc->intr_lock);
563 	sc->codec_if->vtbl->restore_ports(sc->codec_if);
564 	mutex_exit(&sc->lock);
565 
566 	return true;
567 }
568 
569 static void
570 neo_attach(device_t parent, device_t self, void *aux)
571 {
572 	struct neo_softc *sc;
573 	struct pci_attach_args *pa;
574 	pci_chipset_tag_t pc;
575 	char const *intrstr;
576 	pci_intr_handle_t ih;
577 	pcireg_t csr;
578 	int error;
579 
580 	sc = device_private(self);
581 	pa = aux;
582 	pc = pa->pa_pc;
583 
584 	sc->type = PCI_PRODUCT(pa->pa_id);
585 
586 	printf(": NeoMagic 256%s audio\n",
587 	    sc->type == PCI_PRODUCT_NEOMAGIC_NMMM256AV_AU ? "AV" : "ZX");
588 
589 	/* Map I/O register */
590 	if (pci_mapreg_map(pa, PCI_MAPREG_START, PCI_MAPREG_TYPE_MEM, 0,
591 			   &sc->bufiot, &sc->bufioh, &sc->buf_pciaddr, NULL)) {
592 		aprint_error_dev(self, "can't map buffer\n");
593 		return;
594 	}
595 
596 	if (pci_mapreg_map(pa, PCI_MAPREG_START + 4, PCI_MAPREG_TYPE_MEM,
597 	    BUS_SPACE_MAP_LINEAR, &sc->regiot, &sc->regioh, NULL, NULL)) {
598 		aprint_error_dev(self, "can't map registers\n");
599 		return;
600 	}
601 
602 	/* Map and establish the interrupt. */
603 	if (pci_intr_map(pa, &ih)) {
604 		aprint_error_dev(self, "couldn't map interrupt\n");
605 		return;
606 	}
607 
608 	mutex_init(&sc->lock, MUTEX_DEFAULT, IPL_NONE);
609 	mutex_init(&sc->intr_lock, MUTEX_DEFAULT, IPL_AUDIO);
610 
611 	intrstr = pci_intr_string(pc, ih);
612 	sc->ih = pci_intr_establish(pc, ih, IPL_AUDIO, neo_intr, sc);
613 
614 	if (sc->ih == NULL) {
615 		aprint_error_dev(self, "couldn't establish interrupt");
616 		if (intrstr != NULL)
617 			aprint_error(" at %s", intrstr);
618 		aprint_error("\n");
619 		mutex_destroy(&sc->lock);
620 		mutex_destroy(&sc->intr_lock);
621 		return;
622 	}
623 	aprint_normal_dev(self, "interrupting at %s\n", intrstr);
624 
625 	mutex_spin_enter(&sc->intr_lock);
626 	error = nm_init(sc);
627 	mutex_spin_exit(&sc->intr_lock);
628 	if (error != 0) {
629 		mutex_destroy(&sc->lock);
630 		mutex_destroy(&sc->intr_lock);
631 		return;
632 	}
633 
634 	/* Enable the device. */
635 	csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
636 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
637 		       csr | PCI_COMMAND_MASTER_ENABLE);
638 
639 	sc->host_if.arg = sc;
640 	sc->host_if.attach = neo_attach_codec;
641 	sc->host_if.read   = neo_read_codec;
642 	sc->host_if.write  = neo_write_codec;
643 	sc->host_if.reset  = neo_reset_codec;
644 	sc->host_if.flags  = neo_flags_codec;
645 
646 	if (ac97_attach(&sc->host_if, self, &sc->lock) != 0) {
647 		mutex_destroy(&sc->lock);
648 		mutex_destroy(&sc->intr_lock);
649 		return;
650 	}
651 
652 	if (!pmf_device_register(self, NULL, neo_resume))
653 		aprint_error_dev(self, "couldn't establish power handler\n");
654 
655 	audio_attach_mi(&neo_hw_if, sc, self);
656 }
657 
658 static int
659 neo_read_codec(void *v, uint8_t a, uint16_t *d)
660 {
661 	struct neo_softc *sc;
662 
663 	sc = v;
664 	if (!nm_waitcd(sc)) {
665 		*d = nm_rd_2(sc, sc->ac97_base + a);
666 		DELAY(1000);
667 		return 0;
668 	}
669 
670 	return ENXIO;
671 }
672 
673 
674 static int
675 neo_write_codec(void *v, u_int8_t a, u_int16_t d)
676 {
677 	struct neo_softc *sc;
678 	int cnt;
679 
680 	sc = v;
681 	cnt = 3;
682 	if (!nm_waitcd(sc)) {
683 		while (cnt-- > 0) {
684 			nm_wr_2(sc, sc->ac97_base + a, d);
685 			if (!nm_waitcd(sc)) {
686 				DELAY(1000);
687 				return 0;
688 			}
689 		}
690 	}
691 
692 	return ENXIO;
693 }
694 
695 static int
696 neo_attach_codec(void *v, struct ac97_codec_if *codec_if)
697 {
698 	struct neo_softc *sc;
699 
700 	sc = v;
701 	sc->codec_if = codec_if;
702 	return 0;
703 }
704 
705 static int
706 neo_reset_codec(void *v)
707 {
708 	struct neo_softc *sc;
709 
710 	sc = v;
711 	nm_wr_1(sc, 0x6c0, 0x01);
712 	nm_wr_1(sc, 0x6cc, 0x87);
713 	nm_wr_1(sc, 0x6cc, 0x80);
714 	nm_wr_1(sc, 0x6cc, 0x00);
715 	return 0;
716 }
717 
718 static enum ac97_host_flags
719 neo_flags_codec(void *v)
720 {
721 
722 	return AC97_HOST_DONT_READ;
723 }
724 
725 static int
726 neo_query_encoding(void *addr, struct audio_encoding *fp)
727 {
728 
729 	switch (fp->index) {
730 	case 0:
731 		strcpy(fp->name, AudioEulinear);
732 		fp->encoding = AUDIO_ENCODING_ULINEAR;
733 		fp->precision = 8;
734 		fp->flags = 0;
735 		return 0;
736 	case 1:
737 		strcpy(fp->name, AudioEmulaw);
738 		fp->encoding = AUDIO_ENCODING_ULAW;
739 		fp->precision = 8;
740 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
741 		return 0;
742 	case 2:
743 		strcpy(fp->name, AudioEalaw);
744 		fp->encoding = AUDIO_ENCODING_ALAW;
745 		fp->precision = 8;
746 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
747 		return 0;
748 	case 3:
749 		strcpy(fp->name, AudioEslinear);
750 		fp->encoding = AUDIO_ENCODING_SLINEAR;
751 		fp->precision = 8;
752 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
753 		return (0);
754 	case 4:
755 		strcpy(fp->name, AudioEslinear_le);
756 		fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
757 		fp->precision = 16;
758 		fp->flags = 0;
759 		return 0;
760 	case 5:
761 		strcpy(fp->name, AudioEulinear_le);
762 		fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
763 		fp->precision = 16;
764 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
765 		return 0;
766 	case 6:
767 		strcpy(fp->name, AudioEslinear_be);
768 		fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
769 		fp->precision = 16;
770 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
771 		return 0;
772 	case 7:
773 		strcpy(fp->name, AudioEulinear_be);
774 		fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
775 		fp->precision = 16;
776 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
777 		return 0;
778 	default:
779 		return EINVAL;
780 	}
781 }
782 
783 /* Todo: don't commit settings to card until we've verified all parameters */
784 static int
785 neo_set_params(void *addr, int setmode, int usemode,
786     audio_params_t *play, audio_params_t *rec, stream_filter_list_t *pfil,
787     stream_filter_list_t *rfil)
788 {
789 	struct neo_softc *sc;
790 	audio_params_t *p;
791 	stream_filter_list_t *fil;
792 	uint32_t base;
793 	uint8_t x;
794 	int mode, i;
795 
796 	sc = addr;
797 	for (mode = AUMODE_RECORD; mode != -1;
798 	     mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
799 		if ((setmode & mode) == 0)
800 			continue;
801 
802 		p = mode == AUMODE_PLAY ? play : rec;
803 
804 		if (p == NULL) continue;
805 
806 		for (x = 0; x < 8; x++) {
807 			if (p->sample_rate <
808 			    (samplerates[x] + samplerates[x + 1]) / 2)
809 				break;
810 		}
811 		if (x == 8)
812 			return EINVAL;
813 
814 		p->sample_rate = samplerates[x];
815 		nm_loadcoeff(sc, mode, x);
816 
817 		x <<= 4;
818 		x &= NM_RATE_MASK;
819 		if (p->precision == 16)
820 			x |= NM_RATE_BITS_16;
821 		if (p->channels == 2)
822 			x |= NM_RATE_STEREO;
823 
824 		base = (mode == AUMODE_PLAY)?
825 		    NM_PLAYBACK_REG_OFFSET : NM_RECORD_REG_OFFSET;
826 		nm_wr_1(sc, base + NM_RATE_REG_OFFSET, x);
827 
828 		fil = mode == AUMODE_PLAY ? pfil : rfil;
829 		i = auconv_set_converter(neo_formats, NEO_NFORMATS,
830 					 mode, p, FALSE, fil);
831 		if (i < 0)
832 			return EINVAL;
833 	}
834 
835 	return 0;
836 }
837 
838 static int
839 neo_round_blocksize(void *addr, int blk, int mode,
840     const audio_params_t *param)
841 {
842 
843 	return NM_BUFFSIZE / 2;
844 }
845 
846 static int
847 neo_trigger_output(void *addr, void *start, void *end, int blksize,
848     void (*intr)(void *), void *arg, const audio_params_t *param)
849 {
850 	struct neo_softc *sc;
851 	int ssz;
852 
853 	sc = addr;
854 	sc->pintr = intr;
855 	sc->parg = arg;
856 
857 	ssz = (param->precision == 16) ? 2 : 1;
858 	if (param->channels == 2)
859 		ssz <<= 1;
860 
861 	sc->pbufsize = ((char*)end - (char *)start);
862 	sc->pblksize = blksize;
863 	sc->pwmark = blksize;
864 
865 	nm_wr_4(sc, NM_PBUFFER_START, sc->pbuf);
866 	nm_wr_4(sc, NM_PBUFFER_END, sc->pbuf + sc->pbufsize - ssz);
867 	nm_wr_4(sc, NM_PBUFFER_CURRP, sc->pbuf);
868 	nm_wr_4(sc, NM_PBUFFER_WMARK, sc->pbuf + sc->pwmark);
869 	nm_wr_1(sc, NM_PLAYBACK_ENABLE_REG, NM_PLAYBACK_FREERUN |
870 	    NM_PLAYBACK_ENABLE_FLAG);
871 	nm_wr_2(sc, NM_AUDIO_MUTE_REG, 0);
872 
873 	return 0;
874 }
875 
876 static int
877 neo_trigger_input(void *addr, void *start, void *end, int blksize,
878     void (*intr)(void *), void *arg, const audio_params_t *param)
879 {
880 	struct neo_softc *sc;
881 	int ssz;
882 
883 	sc = addr;
884 	sc->rintr = intr;
885 	sc->rarg = arg;
886 
887 	ssz = (param->precision == 16) ? 2 : 1;
888 	if (param->channels == 2)
889 		ssz <<= 1;
890 
891 	sc->rbufsize = ((char*)end - (char *)start);
892 	sc->rblksize = blksize;
893 	sc->rwmark = blksize;
894 
895 	nm_wr_4(sc, NM_RBUFFER_START, sc->rbuf);
896 	nm_wr_4(sc, NM_RBUFFER_END, sc->rbuf + sc->rbufsize);
897 	nm_wr_4(sc, NM_RBUFFER_CURRP, sc->rbuf);
898 	nm_wr_4(sc, NM_RBUFFER_WMARK, sc->rbuf + sc->rwmark);
899 	nm_wr_1(sc, NM_RECORD_ENABLE_REG, NM_RECORD_FREERUN |
900 	    NM_RECORD_ENABLE_FLAG);
901 
902 	return 0;
903 }
904 
905 static int
906 neo_halt_output(void *addr)
907 {
908 	struct neo_softc *sc;
909 
910 	sc = (struct neo_softc *)addr;
911 	nm_wr_1(sc, NM_PLAYBACK_ENABLE_REG, 0);
912 	nm_wr_2(sc, NM_AUDIO_MUTE_REG, NM_AUDIO_MUTE_BOTH);
913 	sc->pintr = 0;
914 
915 	return 0;
916 }
917 
918 static int
919 neo_halt_input(void *addr)
920 {
921 	struct neo_softc *sc;
922 
923 	sc = (struct neo_softc *)addr;
924 	nm_wr_1(sc, NM_RECORD_ENABLE_REG, 0);
925 	sc->rintr = 0;
926 
927 	return 0;
928 }
929 
930 static int
931 neo_getdev(void *addr, struct audio_device *retp)
932 {
933 
934 	*retp = neo_device;
935 	return 0;
936 }
937 
938 static int
939 neo_mixer_set_port(void *addr, mixer_ctrl_t *cp)
940 {
941 	struct neo_softc *sc;
942 
943 	sc = addr;
944 	return sc->codec_if->vtbl->mixer_set_port(sc->codec_if, cp);
945 }
946 
947 static int
948 neo_mixer_get_port(void *addr, mixer_ctrl_t *cp)
949 {
950 	struct neo_softc *sc;
951 
952 	sc = addr;
953 	return sc->codec_if->vtbl->mixer_get_port(sc->codec_if, cp);
954 }
955 
956 static int
957 neo_query_devinfo(void *addr, mixer_devinfo_t *dip)
958 {
959 	struct neo_softc *sc;
960 
961 	sc = addr;
962 	return sc->codec_if->vtbl->query_devinfo(sc->codec_if, dip);
963 }
964 
965 static void *
966 neo_malloc(void *addr, int direction, size_t size)
967 {
968 	struct neo_softc *sc;
969 	void *rv;
970 
971 	sc = addr;
972 	rv = NULL;
973 	switch (direction) {
974 	case AUMODE_PLAY:
975 		if (sc->pbuf_allocated == 0) {
976 			rv = (void *) sc->pbuf_vaddr;
977 			sc->pbuf_allocated = 1;
978 		}
979 		break;
980 
981 	case AUMODE_RECORD:
982 		if (sc->rbuf_allocated == 0) {
983 			rv = (void *) sc->rbuf_vaddr;
984 			sc->rbuf_allocated = 1;
985 		}
986 		break;
987 	}
988 
989 	return rv;
990 }
991 
992 static void
993 neo_free(void *addr, void *ptr, size_t size)
994 {
995 	struct neo_softc *sc;
996 	vaddr_t v;
997 
998 	sc = addr;
999 	v = (vaddr_t)ptr;
1000 	if (v == sc->pbuf_vaddr)
1001 		sc->pbuf_allocated = 0;
1002 	else if (v == sc->rbuf_vaddr)
1003 		sc->rbuf_allocated = 0;
1004 	else
1005 		printf("neo_free: bad address %p\n", ptr);
1006 }
1007 
1008 static size_t
1009 neo_round_buffersize(void *addr, int direction, size_t size)
1010 {
1011 
1012 	return NM_BUFFSIZE;
1013 }
1014 
1015 static paddr_t
1016 neo_mappage(void *addr, void *mem, off_t off, int prot)
1017 {
1018 	struct neo_softc *sc;
1019 	vaddr_t v;
1020 	bus_addr_t pciaddr;
1021 
1022 	sc = addr;
1023 	v = (vaddr_t)mem;
1024 	if (v == sc->pbuf_vaddr)
1025 		pciaddr = sc->pbuf_pciaddr;
1026 	else if (v == sc->rbuf_vaddr)
1027 		pciaddr = sc->rbuf_pciaddr;
1028 	else
1029 		return -1;
1030 
1031 	return bus_space_mmap(sc->bufiot, pciaddr, off, prot,
1032 	    BUS_SPACE_MAP_LINEAR);
1033 }
1034 
1035 static int
1036 neo_get_props(void *addr)
1037 {
1038 
1039 	return AUDIO_PROP_INDEPENDENT | AUDIO_PROP_MMAP |
1040 	    AUDIO_PROP_FULLDUPLEX;
1041 }
1042 
1043 static void
1044 neo_get_locks(void *addr, kmutex_t **intr, kmutex_t **thread)
1045 {
1046 	struct neo_softc *sc;
1047 
1048 	sc = addr;
1049 	*intr = &sc->intr_lock;
1050 	*thread = &sc->lock;
1051 }
1052