xref: /openbsd-src/sys/dev/pci/neo.c (revision 0b7734b3d77bb9b21afec6f4621cae6c805dbd45)
1 /*      $OpenBSD: neo.c,v 1.30 2015/05/11 06:46:22 ratchov 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, WHETHER IN 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 THE POSSIBILITY 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  */
32 
33 
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/device.h>
40 
41 #include <dev/pci/pcidevs.h>
42 #include <dev/pci/pcivar.h>
43 
44 #include <sys/audioio.h>
45 #include <dev/audio_if.h>
46 #include <dev/ic/ac97.h>
47 
48 #include <dev/pci/neoreg.h>
49 
50 /* -------------------------------------------------------------------- */
51 /*
52  * As of 04/13/00, public documentation on the Neomagic 256 is not available.
53  * These comments were gleaned by looking at the driver carefully.
54  *
55  * The Neomagic 256 AV/ZX chips provide both video and audio capabilities
56  * on one chip. About 2-6 megabytes of memory are associated with
57  * the chip. Most of this goes to video frame buffers, but some is used for
58  * audio buffering.
59  *
60  * Unlike most PCI audio chips, the Neomagic chip does not rely on DMA.
61  * Instead, the chip allows you to carve two ring buffers out of its
62  * memory. How you carve this and how much you can carve seems to be
63  * voodoo. The algorithm is in nm_init.
64  *
65  * Most Neomagic audio chips use the AC-97 codec interface. However, there
66  * seem to be a select few chips 256AV chips that do not support AC-97.
67  * This driver does not support them but there are rumors that it
68  * might work with wss isa drivers. This might require some playing around
69  * with your BIOS.
70  *
71  * The Neomagic 256 AV/ZX have 2 PCI I/O region descriptors. Both of
72  * them describe a memory region. The frame buffer is the first region
73  * and the register set is the second region.
74  *
75  * The register manipulation logic is taken from the Linux driver,
76  * which is in the public domain.
77  *
78  * The Neomagic is even nice enough to map the AC-97 codec registers into
79  * the register space to allow direct manipulation. Watch out, accessing
80  * AC-97 registers on the Neomagic requires great delicateness, otherwise
81  * the thing will hang the PCI bus, rendering your system frozen.
82  *
83  * For one, it seems the Neomagic status register that reports AC-97
84  * readiness should NOT be polled more often than once each 1ms.
85  *
86  * Also, writes to the AC-97 register space may take over 40us to
87  * complete.
88  *
89  * Unlike many sound engines, the Neomagic does not support (as fas as
90  * we know :) ) the notion of interrupting every n bytes transferred,
91  * unlike many DMA engines.  Instead, it allows you to specify one
92  * location in each ring buffer (called the watermark). When the chip
93  * passes that location while playing, it signals an interrupt.
94  *
95  * The ring buffer size is currently 16k. That is about 100ms of audio
96  * at 44.1khz/stero/16 bit. However, to keep the buffer full, interrupts
97  * are generated more often than that, so 20-40 interrupts per second
98  * should not be unexpected. Increasing BUFFSIZE should help minimize
99  * the glitches due to drivers that spend too much time looping at high
100  * privelege levels as well as the impact of badly written audio
101  * interface clients.
102  *
103  * TO-DO list:
104  *    neo_malloc/neo_free are still seriously broken.
105  *
106  *    Figure out interaction with video stuff (look at Xfree86 driver?)
107  *
108  *    Power management (neoactivate)
109  *
110  *    Fix detection of Neo devices that don't work this driver (see neo_attach)
111  *
112  *    Figure out how to shrink that huge table neo-coeff.h
113  */
114 
115 #define	NM_BUFFSIZE	16384
116 
117 #define NM256AV_PCI_ID  0x800510c8
118 #define NM256ZX_PCI_ID  0x800610c8
119 
120 /* device private data */
121 struct neo_softc {
122 	struct          device dev;
123 
124 	bus_space_tag_t bufiot;
125 	bus_space_handle_t  bufioh;
126 
127 	bus_space_tag_t regiot;
128 	bus_space_handle_t  regioh;
129 
130 	u_int32_t	type;
131 	void            *ih;
132 
133 	void	(*pintr)(void *);	/* dma completion intr handler */
134 	void	*parg;		/* arg for intr() */
135 
136 	void	(*rintr)(void *);	/* dma completion intr handler */
137 	void	*rarg;		/* arg for intr() */
138 
139 	u_int32_t	ac97_base, ac97_status, ac97_busy;
140 	u_int32_t	buftop, pbuf, rbuf, cbuf, acbuf;
141 	u_int32_t	playint, recint, misc1int, misc2int;
142 	u_int32_t	irsz, badintr;
143 
144         u_int32_t       pbufsize;
145         u_int32_t       rbufsize;
146 
147 	u_int32_t       pblksize;
148 	u_int32_t       rblksize;
149 
150         u_int32_t       pwmark;
151         u_int32_t       rwmark;
152 
153 	struct ac97_codec_if *codec_if;
154 	struct ac97_host_if host_if;
155 };
156 
157 static struct neo_firmware *nf;
158 
159 /* -------------------------------------------------------------------- */
160 
161 /*
162  * prototypes
163  */
164 
165 static int	 nm_waitcd(struct neo_softc *sc);
166 static int	 nm_loadcoeff(struct neo_softc *sc, int dir, int num);
167 static int       nm_init(struct neo_softc *);
168 
169 int    nmchan_getptr(struct neo_softc *, int);
170 /* talk to the card */
171 static u_int32_t nm_rd(struct neo_softc *, int, int);
172 static void	 nm_wr(struct neo_softc *, int, u_int32_t, int);
173 static u_int32_t nm_rdbuf(struct neo_softc *, int, int);
174 static void	 nm_wrbuf(struct neo_softc *, int, u_int32_t, int);
175 
176 int	neo_match(struct device *, void *, void *);
177 void	neo_attach(struct device *, struct device *, void *);
178 int	neo_activate(struct device *, int);
179 int	neo_intr(void *);
180 
181 int	neo_open(void *, int);
182 void	neo_close(void *);
183 int	neo_query_encoding(void *, struct audio_encoding *);
184 int	neo_set_params(void *, int, int, struct audio_params *, struct audio_params *);
185 void	neo_get_default_params(void *, int, struct audio_params *);
186 int	neo_round_blocksize(void *, int);
187 int	neo_trigger_output(void *, void *, void *, int, void (*)(void *),
188 	    void *, struct audio_params *);
189 int	neo_trigger_input(void *, void *, void *, int, void (*)(void *),
190 	    void *, struct audio_params *);
191 int	neo_halt_output(void *);
192 int	neo_halt_input(void *);
193 int	neo_getdev(void *, struct audio_device *);
194 int	neo_mixer_set_port(void *, mixer_ctrl_t *);
195 int	neo_mixer_get_port(void *, mixer_ctrl_t *);
196 int     neo_attach_codec(void *sc, struct ac97_codec_if *);
197 int	neo_read_codec(void *sc, u_int8_t a, u_int16_t *d);
198 int	neo_write_codec(void *sc, u_int8_t a, u_int16_t d);
199 void    neo_reset_codec(void *sc);
200 enum ac97_host_flags neo_flags_codec(void *sc);
201 int	neo_query_devinfo(void *, mixer_devinfo_t *);
202 void   *neo_malloc(void *, int, size_t, int, int);
203 void	neo_free(void *, void *, int);
204 size_t	neo_round_buffersize(void *, int, size_t);
205 int	neo_get_props(void *);
206 void	neo_set_mixer(struct neo_softc *sc, int a, int d);
207 
208 struct cfdriver neo_cd = {
209 	NULL, "neo", DV_DULL
210 };
211 
212 
213 struct cfattach neo_ca = {
214 	sizeof(struct neo_softc), neo_match, neo_attach, NULL,
215 	neo_activate
216 };
217 
218 
219 struct audio_device neo_device = {
220 	"NeoMagic 256",
221 	"",
222 	"neo"
223 };
224 
225 #if 0
226 static u_int32_t badcards[] = {
227 	0x0007103c,
228 	0x008f1028,
229 };
230 #endif
231 
232 #define NUM_BADCARDS (sizeof(badcards) / sizeof(u_int32_t))
233 
234 /* The actual rates supported by the card. */
235 static int samplerates[9] = {
236 	8000,
237 	11025,
238 	16000,
239 	22050,
240 	24000,
241 	32000,
242 	44100,
243 	48000,
244 	99999999
245 };
246 
247 /* -------------------------------------------------------------------- */
248 
249 struct audio_hw_if neo_hw_if = {
250 	neo_open,
251 	neo_close,
252 	NULL,
253 	neo_query_encoding,
254 	neo_set_params,
255 #if 1
256 	neo_round_blocksize,
257 #else
258 	NULL,
259 #endif
260 	NULL,
261 	NULL,
262 	NULL,
263 	NULL,
264 	NULL,
265 	neo_halt_output,
266 	neo_halt_input,
267 	NULL,
268 	neo_getdev,
269 	NULL,
270 	neo_mixer_set_port,
271 	neo_mixer_get_port,
272 	neo_query_devinfo,
273 	neo_malloc,
274 	neo_free,
275 	neo_round_buffersize,
276 	0,				/* neo_mappage, */
277 	neo_get_props,
278 	neo_trigger_output,
279 	neo_trigger_input,
280 	neo_get_default_params
281 
282 };
283 
284 /* -------------------------------------------------------------------- */
285 
286 /* Hardware */
287 static u_int32_t
288 nm_rd(struct neo_softc *sc, int regno, int size)
289 {
290 	bus_space_tag_t st = sc->regiot;
291 	bus_space_handle_t sh = sc->regioh;
292 
293 	switch (size) {
294 	case 1:
295 		return bus_space_read_1(st, sh, regno);
296 	case 2:
297 		return bus_space_read_2(st, sh, regno);
298 	case 4:
299 		return bus_space_read_4(st, sh, regno);
300 	default:
301 		return (0xffffffff);
302 	}
303 }
304 
305 static void
306 nm_wr(struct neo_softc *sc, int regno, u_int32_t data, int size)
307 {
308 	bus_space_tag_t st = sc->regiot;
309 	bus_space_handle_t sh = sc->regioh;
310 
311 	switch (size) {
312 	case 1:
313 		bus_space_write_1(st, sh, regno, data);
314 		break;
315 	case 2:
316 		bus_space_write_2(st, sh, regno, data);
317 		break;
318 	case 4:
319 		bus_space_write_4(st, sh, regno, data);
320 		break;
321 	}
322 }
323 
324 static u_int32_t
325 nm_rdbuf(struct neo_softc *sc, int regno, int size)
326 {
327 	bus_space_tag_t st = sc->bufiot;
328 	bus_space_handle_t sh = sc->bufioh;
329 
330 	switch (size) {
331 	case 1:
332 		return bus_space_read_1(st, sh, regno);
333 	case 2:
334 		return bus_space_read_2(st, sh, regno);
335 	case 4:
336 		return bus_space_read_4(st, sh, regno);
337 	default:
338 		return (0xffffffff);
339 	}
340 }
341 
342 static void
343 nm_wrbuf(struct neo_softc *sc, int regno, u_int32_t data, int size)
344 {
345 	bus_space_tag_t st = sc->bufiot;
346 	bus_space_handle_t sh = sc->bufioh;
347 
348 	switch (size) {
349 	case 1:
350 		bus_space_write_1(st, sh, regno, data);
351 		break;
352 	case 2:
353 		bus_space_write_2(st, sh, regno, data);
354 		break;
355 	case 4:
356 		bus_space_write_4(st, sh, regno, data);
357 		break;
358 	}
359 }
360 
361 /* ac97 codec */
362 static int
363 nm_waitcd(struct neo_softc *sc)
364 {
365 	int cnt = 10;
366 	int fail = 1;
367 
368 	while (cnt-- > 0) {
369 		if (nm_rd(sc, sc->ac97_status, 2) & sc->ac97_busy)
370 			DELAY(100);
371 		else {
372 		        fail = 0;
373 			break;
374 		}
375 	}
376 	return (fail);
377 }
378 
379 
380 static void
381 nm_ackint(struct neo_softc *sc, u_int32_t num)
382 {
383 	if (sc->type == NM256AV_PCI_ID)
384 		nm_wr(sc, NM_INT_REG, num << 1, 2);
385 	else if (sc->type == NM256ZX_PCI_ID)
386 		nm_wr(sc, NM_INT_REG, num, 4);
387 }
388 
389 static int
390 nm_loadcoeff(struct neo_softc *sc, int dir, int num)
391 {
392 	int ofs, sz, i;
393 	u_int32_t addr;
394 
395 	if (nf == NULL) {
396 		size_t buflen;
397 		u_char *buf;
398 		int error;
399 
400 		error = loadfirmware("neo-coefficients", &buf, &buflen);
401 		if (error)
402 			return (error);
403 		nf = (struct neo_firmware *)buf;
404 	}
405 
406 	addr = (dir == AUMODE_PLAY)? 0x01c : 0x21c;
407 	if (dir == AUMODE_RECORD)
408 		num += 8;
409 	sz = nf->coefficientSizes[num];
410 	ofs = 0;
411 	while (num-- > 0)
412 		ofs+= nf->coefficientSizes[num];
413 	for (i = 0; i < sz; i++)
414 		nm_wrbuf(sc, sc->cbuf + i, nf->coefficients[ofs + i], 1);
415 	nm_wr(sc, addr, sc->cbuf, 4);
416 	if (dir == AUMODE_PLAY)
417 		sz--;
418 	nm_wr(sc, addr + 4, sc->cbuf + sz, 4);
419 	return (0);
420 }
421 
422 int
423 nmchan_getptr(struct neo_softc *sc, int mode)
424 {
425 	if (mode == AUMODE_PLAY)
426 		return (nm_rd(sc, NM_PBUFFER_CURRP, 4) - sc->pbuf);
427 	else
428 		return (nm_rd(sc, NM_RBUFFER_CURRP, 4) - sc->rbuf);
429 }
430 
431 
432 /* The interrupt handler */
433 int
434 neo_intr(void *p)
435 {
436 	struct neo_softc *sc = (struct neo_softc *)p;
437 	int status, x;
438 	int rv = 0;
439 
440 	mtx_enter(&audio_lock);
441 	status = nm_rd(sc, NM_INT_REG, sc->irsz);
442 
443 	if (status & sc->playint) {
444 		status &= ~sc->playint;
445 
446 		sc->pwmark += sc->pblksize;
447 		sc->pwmark %= sc->pbufsize;
448 
449 		nm_wr(sc, NM_PBUFFER_WMARK, sc->pbuf + sc->pwmark, 4);
450 
451 		nm_ackint(sc, sc->playint);
452 
453 		if (sc->pintr)
454 			(*sc->pintr)(sc->parg);
455 
456 		rv = 1;
457 	}
458 	if (status & sc->recint) {
459 		status &= ~sc->recint;
460 
461 		sc->rwmark += sc->rblksize;
462 		sc->rwmark %= sc->rbufsize;
463 
464 		nm_ackint(sc, sc->recint);
465 		if (sc->rintr)
466 			(*sc->rintr)(sc->rarg);
467 
468 		rv = 1;
469 	}
470 	if (status & sc->misc1int) {
471 		status &= ~sc->misc1int;
472 		nm_ackint(sc, sc->misc1int);
473 		x = nm_rd(sc, 0x400, 1);
474 		nm_wr(sc, 0x400, x | 2, 1);
475 		printf("%s: misc int 1\n", sc->dev.dv_xname);
476 		rv = 1;
477 	}
478 	if (status & sc->misc2int) {
479 		status &= ~sc->misc2int;
480 		nm_ackint(sc, sc->misc2int);
481 		x = nm_rd(sc, 0x400, 1);
482 		nm_wr(sc, 0x400, x & ~2, 1);
483 		printf("%s: misc int 2\n", sc->dev.dv_xname);
484 		rv = 1;
485 	}
486 	if (status) {
487 		status &= ~sc->misc2int;
488 		nm_ackint(sc, sc->misc2int);
489 		printf("%s: unknown int\n", sc->dev.dv_xname);
490 		rv = 1;
491 	}
492 	mtx_leave(&audio_lock);
493 	return (rv);
494 }
495 
496 /* -------------------------------------------------------------------- */
497 
498 /*
499  * Probe and attach the card
500  */
501 
502 static int
503 nm_init(struct neo_softc *sc)
504 {
505 	u_int32_t ofs, i;
506 
507 	if (sc->type == NM256AV_PCI_ID) {
508 		sc->ac97_base = NM_MIXER_OFFSET;
509 		sc->ac97_status = NM_MIXER_STATUS_OFFSET;
510 		sc->ac97_busy = NM_MIXER_READY_MASK;
511 
512 		sc->buftop = 2560 * 1024;
513 
514 		sc->irsz = 2;
515 		sc->playint = NM_PLAYBACK_INT;
516 		sc->recint = NM_RECORD_INT;
517 		sc->misc1int = NM_MISC_INT_1;
518 		sc->misc2int = NM_MISC_INT_2;
519 	} else if (sc->type == NM256ZX_PCI_ID) {
520 		sc->ac97_base = NM_MIXER_OFFSET;
521 		sc->ac97_status = NM2_MIXER_STATUS_OFFSET;
522 		sc->ac97_busy = NM2_MIXER_READY_MASK;
523 
524 		sc->buftop = (nm_rd(sc, 0xa0b, 2)? 6144 : 4096) * 1024;
525 
526 		sc->irsz = 4;
527 		sc->playint = NM2_PLAYBACK_INT;
528 		sc->recint = NM2_RECORD_INT;
529 		sc->misc1int = NM2_MISC_INT_1;
530 		sc->misc2int = NM2_MISC_INT_2;
531 	} else return -1;
532 	sc->badintr = 0;
533 	ofs = sc->buftop - 0x0400;
534 	sc->buftop -= 0x1400;
535 
536 	if ((nm_rdbuf(sc, ofs, 4) & NM_SIG_MASK) == NM_SIGNATURE) {
537 		i = nm_rdbuf(sc, ofs + 4, 4);
538 		if (i != 0 && i != 0xffffffff)
539 			sc->buftop = i;
540 	}
541 
542 	sc->cbuf = sc->buftop - NM_MAX_COEFFICIENT;
543 	sc->rbuf = sc->cbuf - NM_BUFFSIZE;
544 	sc->pbuf = sc->rbuf - NM_BUFFSIZE;
545 	sc->acbuf = sc->pbuf - (NM_TOTAL_COEFF_COUNT * 4);
546 
547 	nm_wr(sc, 0, 0x11, 1);
548 	nm_wr(sc, NM_RECORD_ENABLE_REG, 0, 1);
549 	nm_wr(sc, 0x214, 0, 2);
550 
551 	return 0;
552 }
553 
554 
555 void
556 neo_attach(struct device *parent, struct device *self, void *aux)
557 {
558 	struct neo_softc *sc = (struct neo_softc *)self;
559 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
560 	pci_chipset_tag_t pc = pa->pa_pc;
561 	char const *intrstr;
562 	pci_intr_handle_t ih;
563 	int error;
564 
565 	sc->type = pa->pa_id;
566 
567 	/* Map I/O register */
568 	if (pci_mapreg_map(pa, PCI_MAPS, PCI_MAPREG_TYPE_MEM, 0,
569 			   &sc->bufiot, &sc->bufioh, NULL, NULL, 0)) {
570 		printf("\n%s: can't map i/o space\n", sc->dev.dv_xname);
571 		return;
572 	}
573 
574 
575 	if (pci_mapreg_map(pa, PCI_MAPS + 4, PCI_MAPREG_TYPE_MEM, 0,
576 			   &sc->regiot, &sc->regioh, NULL, NULL, 0)) {
577 		printf("\n%s: can't map i/o space\n", sc->dev.dv_xname);
578 		return;
579 	}
580 
581 	/* Map and establish the interrupt. */
582 	if (pci_intr_map(pa, &ih)) {
583 		printf("\n%s: couldn't map interrupt\n", sc->dev.dv_xname);
584 		return;
585 	}
586 	intrstr = pci_intr_string(pc, ih);
587 	sc->ih = pci_intr_establish(pc, ih, IPL_AUDIO | IPL_MPSAFE,
588 	    neo_intr, sc, sc->dev.dv_xname);
589 
590 	if (sc->ih == NULL) {
591 		printf("\n%s: couldn't establish interrupt",
592 		       sc->dev.dv_xname);
593 		if (intrstr != NULL)
594 			printf(" at %s", intrstr);
595 		printf("\n");
596 		return;
597 	}
598 	printf(": %s\n", intrstr);
599 
600 	if ((error = nm_init(sc)) != 0)
601 		return;
602 
603 	sc->host_if.arg = sc;
604 
605 	sc->host_if.attach = neo_attach_codec;
606 	sc->host_if.read   = neo_read_codec;
607 	sc->host_if.write  = neo_write_codec;
608 	sc->host_if.reset  = neo_reset_codec;
609 	sc->host_if.flags  = neo_flags_codec;
610 
611 	if ((error = ac97_attach(&sc->host_if)) != 0)
612 		return;
613 
614 	audio_attach_mi(&neo_hw_if, sc, &sc->dev);
615 
616 	return;
617 }
618 
619 int
620 neo_activate(struct device *self, int act)
621 {
622 	struct neo_softc *sc = (struct neo_softc *)self;
623 
624 	switch (act) {
625 	case DVACT_SUSPEND:
626 		break;
627 	case DVACT_RESUME:
628 		nm_init(sc);
629 		(sc->codec_if->vtbl->restore_ports)(sc->codec_if);
630 		break;
631 	}
632 	return 0;
633 }
634 
635 int
636 neo_match(struct device *parent, void *match, void *aux)
637 {
638 	struct pci_attach_args *pa = (struct pci_attach_args *) aux;
639 #if 0
640 	u_int32_t subdev, badcard;
641 #endif
642 
643 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_NEOMAGIC)
644 		return (0);
645 
646 #if 0
647 	subdev = (pci_get_subdevice(dev) << 16) | pci_get_subvendor(dev);
648 #endif
649 	switch (PCI_PRODUCT(pa->pa_id)) {
650 	case PCI_PRODUCT_NEOMAGIC_NM256AV:
651 #if 0
652 		i = 0;
653 		while ((i < NUM_BADCARDS) && (badcards[i] != subdev))
654 			i++;
655 		if (i == NUM_BADCARDS)
656 			s = "NeoMagic 256AV";
657 		DEB(else)
658 			DEB(device_printf(dev, "this is a non-ac97 NM256AV, not attaching\n"));
659 		return (1);
660 #endif
661 	case PCI_PRODUCT_NEOMAGIC_NM256ZX:
662 		return (1);
663 	}
664 
665 	return (0);
666 }
667 
668 int
669 neo_read_codec(void *sc_, u_int8_t a, u_int16_t *d)
670 {
671 	struct neo_softc *sc = sc_;
672 
673 	if (!nm_waitcd(sc)) {
674 		*d = nm_rd(sc, sc->ac97_base + a, 2);
675 		DELAY(1000);
676 		return 0;
677 	}
678 
679 	return (ENXIO);
680 }
681 
682 
683 int
684 neo_write_codec(void *sc_, u_int8_t a, u_int16_t d)
685 {
686 	struct neo_softc *sc = sc_;
687 	int cnt = 3;
688 
689 	if (!nm_waitcd(sc)) {
690 		while (cnt-- > 0) {
691 			nm_wr(sc, sc->ac97_base + a, d, 2);
692 			if (!nm_waitcd(sc)) {
693 				DELAY(1000);
694 				return (0);
695 			}
696 		}
697 	}
698 
699         return (ENXIO);
700 }
701 
702 
703 int
704 neo_attach_codec(void *sc_, struct ac97_codec_if *codec_if)
705 {
706 	struct neo_softc *sc = sc_;
707 
708 	sc->codec_if = codec_if;
709 	return (0);
710 }
711 
712 void
713 neo_reset_codec(void *sc)
714 {
715 	nm_wr(sc, 0x6c0, 0x01, 1);
716 	nm_wr(sc, 0x6cc, 0x87, 1);
717 	nm_wr(sc, 0x6cc, 0x80, 1);
718 	nm_wr(sc, 0x6cc, 0x00, 1);
719 
720 	return;
721 }
722 
723 
724 enum ac97_host_flags
725 neo_flags_codec(void *sc)
726 {
727 	return (AC97_HOST_DONT_READANY);
728 }
729 
730 int
731 neo_open(void *addr, int flags)
732 {
733 	return (0);
734 }
735 
736 /*
737  * Close function is called at splaudio().
738  */
739 void
740 neo_close(void *addr)
741 {
742 	struct neo_softc *sc = addr;
743 
744 	neo_halt_output(sc);
745 	neo_halt_input(sc);
746 
747 	sc->pintr = 0;
748 	sc->rintr = 0;
749 }
750 
751 int
752 neo_query_encoding(void *addr, struct audio_encoding *fp)
753 {
754 	switch (fp->index) {
755 	case 0:
756 		strlcpy(fp->name, AudioEulinear, sizeof fp->name);
757 		fp->encoding = AUDIO_ENCODING_ULINEAR;
758 		fp->precision = 8;
759 		fp->flags = 0;
760 		break;
761 	case 1:
762 		strlcpy(fp->name, AudioEslinear_le, sizeof fp->name);
763 		fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
764 		fp->precision = 16;
765 		fp->flags = 0;
766 		break;
767 	default:
768 		return (EINVAL);
769 	}
770 	fp->bps = AUDIO_BPS(fp->precision);
771 	fp->msb = 1;
772 
773 	return (0);
774 }
775 
776 void
777 neo_get_default_params(void *addr, int mode, struct audio_params *params)
778 {
779 	ac97_get_default_params(params);
780 }
781 
782 /* Todo: don't commit settings to card until we've verified all parameters */
783 int
784 neo_set_params(void *addr, int setmode, int usemode,
785     struct audio_params *play, struct audio_params *rec)
786 {
787 	struct neo_softc *sc = addr;
788 	u_int32_t base;
789 	u_int8_t x;
790 	int mode;
791 	struct audio_params *p;
792 
793 	for (mode = AUMODE_RECORD; mode != -1;
794 	     mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
795 		if ((setmode & mode) == 0)
796 			continue;
797 
798 		p = mode == AUMODE_PLAY ? play : rec;
799 
800 		if (p == NULL) continue;
801 
802 		for (x = 0; x < 8; x++)
803 			if (p->sample_rate < (samplerates[x] + samplerates[x + 1]) / 2)
804 				break;
805 
806 		p->sample_rate = samplerates[x];
807 		nm_loadcoeff(sc, mode, x);
808 
809 		x <<= 4;
810 		x &= NM_RATE_MASK;
811 		if (p->precision == 16) x |= NM_RATE_BITS_16;
812 		if (p->channels == 2) x |= NM_RATE_STEREO;
813 
814 		base = (mode == AUMODE_PLAY) ?
815 		    NM_PLAYBACK_REG_OFFSET : NM_RECORD_REG_OFFSET;
816 		nm_wr(sc, base + NM_RATE_REG_OFFSET, x, 1);
817 
818 		switch (p->encoding) {
819 		case AUDIO_ENCODING_SLINEAR_LE:
820 			if (p->precision != 16)
821 				return EINVAL;
822 			break;
823 		case AUDIO_ENCODING_ULINEAR_LE:
824 		case AUDIO_ENCODING_ULINEAR_BE:
825 			if (p->precision != 8)
826 				return EINVAL;
827 			break;
828 		default:
829 			return (EINVAL);
830 		}
831 		p->bps = AUDIO_BPS(p->precision);
832 		p->msb = 1;
833 	}
834 
835 	return (0);
836 }
837 
838 int
839 neo_round_blocksize(void *addr, int blk)
840 {
841 	return (NM_BUFFSIZE / 2);
842 }
843 
844 int
845 neo_trigger_output(void *addr, void *start, void *end, int blksize,
846     void (*intr)(void *), void *arg, struct audio_params *param)
847 {
848 	struct neo_softc *sc = addr;
849 	int ssz;
850 
851 	mtx_enter(&audio_lock);
852 	sc->pintr = intr;
853 	sc->parg = arg;
854 
855 	ssz = (param->precision == 16) ? 2 : 1;
856 	if (param->channels == 2)
857 		ssz <<= 1;
858 
859 	sc->pbufsize = ((char *)end - (char *)start);
860 	sc->pblksize = blksize;
861 	sc->pwmark = blksize;
862 	nm_wr(sc, NM_PBUFFER_START, sc->pbuf, 4);
863 	nm_wr(sc, NM_PBUFFER_END, sc->pbuf + sc->pbufsize - ssz, 4);
864 	nm_wr(sc, NM_PBUFFER_CURRP, sc->pbuf, 4);
865 	nm_wr(sc, NM_PBUFFER_WMARK, sc->pbuf + sc->pwmark, 4);
866 	nm_wr(sc, NM_PLAYBACK_ENABLE_REG, NM_PLAYBACK_FREERUN |
867 	    NM_PLAYBACK_ENABLE_FLAG, 1);
868 	nm_wr(sc, NM_AUDIO_MUTE_REG, 0, 2);
869 	mtx_leave(&audio_lock);
870 	return (0);
871 }
872 
873 
874 
875 int
876 neo_trigger_input(void *addr, void *start, void *end, int blksize,
877     void (*intr)(void *), void *arg, struct audio_params *param)
878 {
879 	struct neo_softc *sc = addr;
880 	int ssz;
881 
882 	mtx_enter(&audio_lock);
883 	sc->rintr = intr;
884 	sc->rarg = arg;
885 
886 	ssz = (param->precision == 16) ? 2 : 1;
887 	if (param->channels == 2)
888 		ssz <<= 1;
889 
890 	sc->rbufsize = ((char *)end - (char *)start);
891 	sc->rblksize = blksize;
892 	sc->rwmark = blksize;
893 	nm_wr(sc, NM_RBUFFER_START, sc->rbuf, 4);
894 	nm_wr(sc, NM_RBUFFER_END, sc->rbuf + sc->rbufsize, 4);
895 	nm_wr(sc, NM_RBUFFER_CURRP, sc->rbuf, 4);
896 	nm_wr(sc, NM_RBUFFER_WMARK, sc->rbuf + sc->rwmark, 4);
897 	nm_wr(sc, NM_RECORD_ENABLE_REG, NM_RECORD_FREERUN |
898 	    NM_RECORD_ENABLE_FLAG, 1);
899 	mtx_leave(&audio_lock);
900 	return (0);
901 }
902 
903 int
904 neo_halt_output(void *addr)
905 {
906 	struct neo_softc *sc = (struct neo_softc *)addr;
907 
908 	mtx_enter(&audio_lock);
909 	nm_wr(sc, NM_PLAYBACK_ENABLE_REG, 0, 1);
910 	nm_wr(sc, NM_AUDIO_MUTE_REG, NM_AUDIO_MUTE_BOTH, 2);
911 
912 	sc->pintr = 0;
913 	mtx_leave(&audio_lock);
914 	return (0);
915 }
916 
917 int
918 neo_halt_input(void *addr)
919 {
920 	struct neo_softc *sc = (struct neo_softc *)addr;
921 
922 	mtx_enter(&audio_lock);
923 	nm_wr(sc, NM_RECORD_ENABLE_REG, 0, 1);
924 
925 	sc->rintr = 0;
926 	mtx_leave(&audio_lock);
927 	return (0);
928 }
929 
930 int
931 neo_getdev(void *addr, struct audio_device *retp)
932 {
933 	*retp = neo_device;
934 	return (0);
935 }
936 
937 int
938 neo_mixer_set_port(void *addr, mixer_ctrl_t *cp)
939 {
940 	struct neo_softc *sc = addr;
941 
942 	return ((sc->codec_if->vtbl->mixer_set_port)(sc->codec_if, cp));
943 }
944 
945 int
946 neo_mixer_get_port(void *addr, mixer_ctrl_t *cp)
947 {
948 	struct neo_softc *sc = addr;
949 
950 	return ((sc->codec_if->vtbl->mixer_get_port)(sc->codec_if, cp));
951 }
952 
953 int
954 neo_query_devinfo(void *addr, mixer_devinfo_t *dip)
955 {
956 	struct neo_softc *sc = addr;
957 
958 	return ((sc->codec_if->vtbl->query_devinfo)(sc->codec_if, dip));
959 }
960 
961 void *
962 neo_malloc(void *addr, int direction, size_t size, int pool, int flags)
963 {
964 	struct neo_softc *sc = addr;
965 	void *rv = 0;
966 
967 	switch (direction) {
968 	case AUMODE_PLAY:
969 		rv = (char *)sc->bufioh + sc->pbuf;
970 		break;
971 	case AUMODE_RECORD:
972 		rv = (char *)sc->bufioh + sc->rbuf;
973 		break;
974 	default:
975 		break;
976 	}
977 
978 	return (rv);
979 }
980 
981 void
982 neo_free(void *addr, void *ptr, int pool)
983 {
984 	return;
985 }
986 
987 size_t
988 neo_round_buffersize(void *addr, int direction, size_t size)
989 {
990 	return (NM_BUFFSIZE);
991 }
992 
993 
994 int
995 neo_get_props(void *addr)
996 {
997 	return (AUDIO_PROP_INDEPENDENT | AUDIO_PROP_FULLDUPLEX);
998 }
999