xref: /netbsd-src/sys/dev/pci/neo.c (revision 3b01aba77a7a698587faaae455bbfe740923c1f5)
1 /*	$NetBSD: neo.c,v 1.8 2001/03/14 11:38:55 drochner 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/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/device.h>
39 
40 #include <machine/bus.h>
41 
42 #include <dev/pci/pcidevs.h>
43 #include <dev/pci/pcivar.h>
44 
45 #include <dev/pci/neoreg.h>
46 #include <dev/pci/neo-coeff.h>
47 
48 #include <sys/audioio.h>
49 #include <dev/audio_if.h>
50 #include <dev/mulaw.h>
51 #include <dev/auconv.h>
52 
53 #include <dev/ic/ac97var.h>
54 
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  * mgiht 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 secodn 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 fas 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 	struct          device dev;
120 
121 	bus_space_tag_t bufiot;
122 	bus_space_handle_t  bufioh;
123 
124 	bus_space_tag_t regiot;
125 	bus_space_handle_t  regioh;
126 
127 	u_int32_t 	type;
128 	void            *ih;
129 
130 	void	(*pintr)(void *);	/* dma completion intr handler */
131 	void	*parg;		/* arg for intr() */
132 
133 	void	(*rintr)(void *);	/* dma completion intr handler */
134 	void	*rarg;		/* arg for intr() */
135 
136 	vaddr_t	buf_vaddr;
137 	vaddr_t	rbuf_vaddr;
138 	vaddr_t	pbuf_vaddr;
139 	int	pbuf_allocated;
140 	int	rbuf_allocated;
141 
142 	bus_addr_t buf_pciaddr;
143 	bus_addr_t rbuf_pciaddr;
144 	bus_addr_t pbuf_pciaddr;
145 
146 	u_int32_t 	ac97_base, ac97_status, ac97_busy;
147 	u_int32_t	buftop, pbuf, rbuf, cbuf, acbuf;
148 	u_int32_t	playint, recint, misc1int, misc2int;
149 	u_int32_t	irsz, badintr;
150 
151         u_int32_t       pbufsize;
152         u_int32_t       rbufsize;
153 
154 	u_int32_t       pblksize;
155 	u_int32_t       rblksize;
156 
157         u_int32_t       pwmark;
158         u_int32_t       rwmark;
159 
160 	struct ac97_codec_if *codec_if;
161 	struct ac97_host_if host_if;
162 
163 	void		*powerhook;
164 };
165 
166 /* -------------------------------------------------------------------- */
167 
168 /*
169  * prototypes
170  */
171 
172 static int	nm_waitcd(struct neo_softc *sc);
173 static int	nm_loadcoeff(struct neo_softc *sc, int dir, int num);
174 static int	nm_init(struct neo_softc *);
175 
176 int	neo_match(struct device *, struct cfdata *, void *);
177 void	neo_attach(struct device *, struct device *, void *);
178 int	neo_intr(void *);
179 
180 int	neo_open(void *, int);
181 void	neo_close(void *);
182 int	neo_query_encoding(void *, struct audio_encoding *);
183 int	neo_set_params(void *, int, int, struct audio_params *,
184 	    struct audio_params *);
185 int	neo_round_blocksize(void *, int);
186 int	neo_trigger_output(void *, void *, void *, int, void (*)(void *),
187 	    void *, struct audio_params *);
188 int	neo_trigger_input(void *, void *, void *, int, void (*)(void *),
189 	    void *, struct audio_params *);
190 int	neo_halt_output(void *);
191 int	neo_halt_input(void *);
192 int	neo_getdev(void *, struct audio_device *);
193 int	neo_mixer_set_port(void *, mixer_ctrl_t *);
194 int	neo_mixer_get_port(void *, mixer_ctrl_t *);
195 int     neo_attach_codec(void *sc, struct ac97_codec_if *);
196 int	neo_read_codec(void *sc, u_int8_t a, u_int16_t *d);
197 int	neo_write_codec(void *sc, u_int8_t a, u_int16_t d);
198 void    neo_reset_codec(void *sc);
199 enum ac97_host_flags neo_flags_codec(void *sc);
200 int	neo_query_devinfo(void *, mixer_devinfo_t *);
201 void   *neo_malloc(void *, int, size_t, int, int);
202 void	neo_free(void *, void *, int);
203 size_t	neo_round_buffersize(void *, int, size_t);
204 paddr_t	neo_mappage(void *, void *, off_t, int);
205 int	neo_get_props(void *);
206 void	neo_set_mixer(struct neo_softc *sc, int a, int d);
207 void	neo_power(int why, void *arg);
208 
209 struct cfattach neo_ca = {
210 	sizeof(struct neo_softc), neo_match, neo_attach
211 };
212 
213 struct audio_device neo_device = {
214 	"NeoMagic 256",
215 	"",
216 	"neo"
217 };
218 
219 /* The actual rates supported by the card. */
220 static const int samplerates[9] = {
221 	8000,
222 	11025,
223 	16000,
224 	22050,
225 	24000,
226 	32000,
227 	44100,
228 	48000,
229 	99999999
230 };
231 
232 /* -------------------------------------------------------------------- */
233 
234 struct audio_hw_if neo_hw_if = {
235 	neo_open,
236 	neo_close,
237 	NULL,				/* drain */
238 	neo_query_encoding,
239 	neo_set_params,
240 	neo_round_blocksize,
241 	NULL,				/* commit_setting */
242 	NULL,				/* init_output */
243 	NULL,				/* init_input */
244 	NULL,				/* start_output */
245 	NULL,				/* start_input */
246 	neo_halt_output,
247 	neo_halt_input,
248 	NULL,				/* speaker_ctl */
249 	neo_getdev,
250 	NULL,				/* getfd */
251 	neo_mixer_set_port,
252 	neo_mixer_get_port,
253 	neo_query_devinfo,
254 	neo_malloc,
255 	neo_free,
256 	neo_round_buffersize,
257 	neo_mappage,
258 	neo_get_props,
259 	neo_trigger_output,
260 	neo_trigger_input,
261 };
262 
263 /* -------------------------------------------------------------------- */
264 
265 #define	nm_rd_1(sc, regno)						\
266 	bus_space_read_1((sc)->regiot, (sc)->regioh, (regno))
267 
268 #define	nm_rd_2(sc, regno)						\
269 	bus_space_read_2((sc)->regiot, (sc)->regioh, (regno))
270 
271 #define	nm_rd_4(sc, regno)						\
272 	bus_space_read_4((sc)->regiot, (sc)->regioh, (regno))
273 
274 #define	nm_wr_1(sc, regno, val)						\
275 	bus_space_write_1((sc)->regiot, (sc)->regioh, (regno), (val))
276 
277 #define	nm_wr_2(sc, regno, val)						\
278 	bus_space_write_2((sc)->regiot, (sc)->regioh, (regno), (val))
279 
280 #define	nm_wr_4(sc, regno, val)						\
281 	bus_space_write_4((sc)->regiot, (sc)->regioh, (regno), (val))
282 
283 #define	nm_rdbuf_4(sc, regno)						\
284 	bus_space_read_4((sc)->bufiot, (sc)->bufioh, (regno))
285 
286 #define	nm_wrbuf_1(sc, regno, val)					\
287 	bus_space_write_1((sc)->bufiot, (sc)->bufioh, (regno), (val))
288 
289 /* ac97 codec */
290 static int
291 nm_waitcd(struct neo_softc *sc)
292 {
293 	int cnt = 10;
294 	int fail = 1;
295 
296 	while (cnt-- > 0) {
297 		if (nm_rd_2(sc, sc->ac97_status) & sc->ac97_busy)
298 			DELAY(100);
299 		else {
300 		        fail = 0;
301 			break;
302 		}
303 	}
304 	return (fail);
305 }
306 
307 
308 static void
309 nm_ackint(struct neo_softc *sc, u_int32_t num)
310 {
311 
312 	switch (sc->type) {
313 	case PCI_PRODUCT_NEOMAGIC_NMMM256AV_AU:
314 		nm_wr_2(sc, NM_INT_REG, num << 1);
315 		break;
316 
317 	case PCI_PRODUCT_NEOMAGIC_NMMM256ZX_AU:
318 		nm_wr_4(sc, NM_INT_REG, num);
319 		break;
320 	}
321 }
322 
323 static int
324 nm_loadcoeff(struct neo_softc *sc, int dir, int num)
325 {
326 	int ofs, sz, i;
327 	u_int32_t addr;
328 
329 	addr = (dir == AUMODE_PLAY)? 0x01c : 0x21c;
330 	if (dir == AUMODE_RECORD)
331 		num += 8;
332 	sz = coefficientSizes[num];
333 	ofs = 0;
334 	while (num-- > 0)
335 		ofs+= coefficientSizes[num];
336 	for (i = 0; i < sz; i++)
337 		nm_wrbuf_1(sc, sc->cbuf + i, coefficients[ofs + i]);
338 	nm_wr_4(sc, addr, sc->cbuf);
339 	if (dir == AUMODE_PLAY)
340 		sz--;
341 	nm_wr_4(sc, addr + 4, sc->cbuf + sz);
342 	return 0;
343 }
344 
345 /* The interrupt handler */
346 int
347 neo_intr(void *p)
348 {
349 	struct neo_softc *sc = (struct neo_softc *)p;
350 	int status, x, active;
351 	int rv = 0;
352 
353 	active = (sc->pintr || sc->rintr);
354 	status = (sc->irsz == 2) ?
355 	    nm_rd_2(sc, NM_INT_REG) :
356 	    nm_rd_4(sc, NM_INT_REG);
357 
358 	if (status & sc->playint) {
359 		status &= ~sc->playint;
360 
361 		sc->pwmark += sc->pblksize;
362 		sc->pwmark %= sc->pbufsize;
363 
364 		nm_wr_4(sc, NM_PBUFFER_WMARK, sc->pbuf + sc->pwmark);
365 
366 		nm_ackint(sc, sc->playint);
367 
368 		if (sc->pintr)
369 			(*sc->pintr)(sc->parg);
370 
371 		rv = 1;
372 	}
373 	if (status & sc->recint) {
374 		status &= ~sc->recint;
375 
376 		sc->rwmark += sc->rblksize;
377 		sc->rwmark %= sc->rbufsize;
378 
379 		nm_ackint(sc, sc->recint);
380 		if (sc->rintr)
381 			(*sc->rintr)(sc->rarg);
382 
383 		rv = 1;
384 	}
385 	if (status & sc->misc1int) {
386 		status &= ~sc->misc1int;
387 		nm_ackint(sc, sc->misc1int);
388 		x = nm_rd_1(sc, 0x400);
389 		nm_wr_1(sc, 0x400, x | 2);
390 		printf("%s: misc int 1\n", sc->dev.dv_xname);
391 		rv = 1;
392 	}
393 	if (status & sc->misc2int) {
394 		status &= ~sc->misc2int;
395 		nm_ackint(sc, sc->misc2int);
396 		x = nm_rd_1(sc, 0x400);
397 		nm_wr_1(sc, 0x400, x & ~2);
398 		printf("%s: misc int 2\n", sc->dev.dv_xname);
399 		rv = 1;
400 	}
401 	if (status) {
402 		status &= ~sc->misc2int;
403 		nm_ackint(sc, sc->misc2int);
404 		printf("%s: unknown int\n", sc->dev.dv_xname);
405 		rv = 1;
406 	}
407 
408 	return (rv);
409 }
410 
411 /* -------------------------------------------------------------------- */
412 
413 /*
414  * Probe and attach the card
415  */
416 
417 static int
418 nm_init(struct neo_softc *sc)
419 {
420 	u_int32_t ofs, i;
421 
422 	switch (sc->type) {
423 	case PCI_PRODUCT_NEOMAGIC_NMMM256AV_AU:
424 		sc->ac97_base = NM_MIXER_OFFSET;
425 		sc->ac97_status = NM_MIXER_STATUS_OFFSET;
426 		sc->ac97_busy = NM_MIXER_READY_MASK;
427 
428 		sc->buftop = 2560 * 1024;
429 
430 		sc->irsz = 2;
431 		sc->playint = NM_PLAYBACK_INT;
432 		sc->recint = NM_RECORD_INT;
433 		sc->misc1int = NM_MISC_INT_1;
434 		sc->misc2int = NM_MISC_INT_2;
435 		break;
436 
437 	case PCI_PRODUCT_NEOMAGIC_NMMM256ZX_AU:
438 		sc->ac97_base = NM_MIXER_OFFSET;
439 		sc->ac97_status = NM2_MIXER_STATUS_OFFSET;
440 		sc->ac97_busy = NM2_MIXER_READY_MASK;
441 
442 		sc->buftop = (nm_rd_2(sc, 0xa0b) ? 6144 : 4096) * 1024;
443 
444 		sc->irsz = 4;
445 		sc->playint = NM2_PLAYBACK_INT;
446 		sc->recint = NM2_RECORD_INT;
447 		sc->misc1int = NM2_MISC_INT_1;
448 		sc->misc2int = NM2_MISC_INT_2;
449 		break;
450 #ifdef DIAGNOSTIC
451 	default:
452 		panic("nm_init: impossible");
453 #endif
454 	}
455 
456 	sc->badintr = 0;
457 	ofs = sc->buftop - 0x0400;
458 	sc->buftop -= 0x1400;
459 
460  	if ((nm_rdbuf_4(sc, ofs) & NM_SIG_MASK) == NM_SIGNATURE) {
461 		i = nm_rdbuf_4(sc, ofs + 4);
462 		if (i != 0 && i != 0xffffffff)
463 			sc->buftop = i;
464 	}
465 
466 	sc->cbuf = sc->buftop - NM_MAX_COEFFICIENT;
467 	sc->rbuf = sc->cbuf - NM_BUFFSIZE;
468 	sc->pbuf = sc->rbuf - NM_BUFFSIZE;
469 	sc->acbuf = sc->pbuf - (NM_TOTAL_COEFF_COUNT * 4);
470 
471 	sc->buf_vaddr = (vaddr_t) bus_space_vaddr(sc->bufiot, sc->bufioh);
472 	sc->rbuf_vaddr = sc->buf_vaddr + sc->rbuf;
473 	sc->pbuf_vaddr = sc->buf_vaddr + sc->pbuf;
474 
475 	sc->rbuf_pciaddr = sc->buf_pciaddr + sc->rbuf;
476 	sc->pbuf_pciaddr = sc->buf_pciaddr + sc->pbuf;
477 
478 	nm_wr_1(sc, 0, 0x11);
479 	nm_wr_1(sc, NM_RECORD_ENABLE_REG, 0);
480 	nm_wr_2(sc, 0x214, 0);
481 
482 	return 0;
483 }
484 
485 int
486 neo_match(struct device *parent, struct cfdata *match, void *aux)
487 {
488 	struct pci_attach_args *pa = aux;
489 	pcireg_t subdev;
490 
491 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_NEOMAGIC)
492 		return (0);
493 
494 	subdev = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
495 
496 	switch (PCI_PRODUCT(pa->pa_id)) {
497 	case PCI_PRODUCT_NEOMAGIC_NMMM256AV_AU:
498 		/*
499 		 * We have to weed-out the non-AC'97 versions of
500 		 * the chip (i.e. the ones that are known to work
501 		 * in WSS emulation mode), as they won't work with
502 		 * this driver.
503 		 */
504 		switch (PCI_VENDOR(subdev)) {
505 		case PCI_VENDOR_DELL:
506 			switch (PCI_PRODUCT(subdev)) {
507 			case 0x008f:
508 				return (0);
509 			}
510 			break;
511 
512 		case PCI_VENDOR_HP:
513 			switch (PCI_PRODUCT(subdev)) {
514 			case 0x0007:
515 				return (0);
516 			}
517 			break;
518 
519 		case PCI_VENDOR_IBM:
520 			switch (PCI_PRODUCT(subdev)) {
521 			case 0x00dd:
522 				return (0);
523 			}
524 			break;
525 		}
526 		return (1);
527 
528 	case PCI_PRODUCT_NEOMAGIC_NMMM256ZX_AU:
529 		return (1);
530 	}
531 
532 	return (0);
533 }
534 
535 void
536 neo_power(int why, void *addr)
537 {
538 	struct neo_softc *sc = (struct neo_softc *)addr;
539 
540 	if (why == PWR_RESUME) {
541 		nm_init(sc);
542 		(sc->codec_if->vtbl->restore_ports)(sc->codec_if);
543 	}
544 }
545 
546 void
547 neo_attach(struct device *parent, struct device *self, void *aux)
548 {
549 	struct neo_softc *sc = (struct neo_softc *)self;
550 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
551 	pci_chipset_tag_t pc = pa->pa_pc;
552 	char const *intrstr;
553 	pci_intr_handle_t ih;
554 	pcireg_t csr;
555 	int error;
556 
557 	sc->type = PCI_PRODUCT(pa->pa_id);
558 
559 	printf(": NeoMagic 256%s audio\n",
560 	    sc->type == PCI_PRODUCT_NEOMAGIC_NMMM256AV_AU ? "AV" : "ZX");
561 
562 	/* Map I/O register */
563 	if (pci_mapreg_map(pa, PCI_MAPREG_START, PCI_MAPREG_TYPE_MEM, 0,
564 			   &sc->bufiot, &sc->bufioh, &sc->buf_pciaddr, NULL)) {
565 		printf("%s: can't map buffer\n", sc->dev.dv_xname);
566 		return;
567 	}
568 
569 	if (pci_mapreg_map(pa, PCI_MAPREG_START + 4, PCI_MAPREG_TYPE_MEM, 0,
570 			   &sc->regiot, &sc->regioh, NULL, NULL)) {
571 		printf("%s: can't map registers\n", sc->dev.dv_xname);
572 		return;
573 	}
574 
575 	/* Map and establish the interrupt. */
576 	if (pci_intr_map(pa, &ih)) {
577 		printf("%s: couldn't map interrupt\n", sc->dev.dv_xname);
578 		return;
579 	}
580 
581 	intrstr = pci_intr_string(pc, ih);
582 	sc->ih = pci_intr_establish(pc, ih, IPL_AUDIO, neo_intr, sc);
583 
584 	if (sc->ih == NULL) {
585 		printf("%s: couldn't establish interrupt",
586 		       sc->dev.dv_xname);
587 		if (intrstr != NULL)
588 			printf(" at %s", intrstr);
589 		printf("\n");
590 		return;
591 	}
592 	printf("%s: interruping at %s\n", sc->dev.dv_xname, intrstr);
593 
594 	if ((error = nm_init(sc)) != 0)
595 		return;
596 
597 	/* Enable the device. */
598 	csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
599 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
600 		       csr | PCI_COMMAND_MASTER_ENABLE);
601 
602 	sc->host_if.arg = sc;
603 
604 	sc->host_if.attach = neo_attach_codec;
605 	sc->host_if.read   = neo_read_codec;
606 	sc->host_if.write  = neo_write_codec;
607 	sc->host_if.reset  = neo_reset_codec;
608 	sc->host_if.flags  = neo_flags_codec;
609 
610 	if ((error = ac97_attach(&sc->host_if)) != 0)
611 		return;
612 
613 	sc->powerhook = powerhook_establish(neo_power, sc);
614 
615 	audio_attach_mi(&neo_hw_if, sc, &sc->dev);
616 }
617 
618 int
619 neo_read_codec(void *v, u_int8_t a, u_int16_t *d)
620 {
621 	struct neo_softc *sc = v;
622 
623 	if (!nm_waitcd(sc)) {
624 		*d = nm_rd_2(sc, sc->ac97_base + a);
625 		DELAY(1000);
626 		return 0;
627 	}
628 
629 	return (ENXIO);
630 }
631 
632 
633 int
634 neo_write_codec(void *v, u_int8_t a, u_int16_t d)
635 {
636 	struct neo_softc *sc = v;
637 	int cnt = 3;
638 
639 	if (!nm_waitcd(sc)) {
640 		while (cnt-- > 0) {
641 			nm_wr_2(sc, sc->ac97_base + a, d);
642 			if (!nm_waitcd(sc)) {
643 				DELAY(1000);
644 				return (0);
645 			}
646 		}
647 	}
648 
649         return (ENXIO);
650 }
651 
652 int
653 neo_attach_codec(void *v, struct ac97_codec_if *codec_if)
654 {
655 	struct neo_softc *sc = v;
656 
657 	sc->codec_if = codec_if;
658 	return (0);
659 }
660 
661 void
662 neo_reset_codec(void *v)
663 {
664 	struct neo_softc *sc = v;
665 
666 	nm_wr_1(sc, 0x6c0, 0x01);
667 	nm_wr_1(sc, 0x6cc, 0x87);
668 	nm_wr_1(sc, 0x6cc, 0x80);
669 	nm_wr_1(sc, 0x6cc, 0x00);
670 }
671 
672 enum ac97_host_flags
673 neo_flags_codec(void *v)
674 {
675 
676 	return (AC97_HOST_DONT_READ);
677 }
678 
679 int
680 neo_open(void *addr, int flags)
681 {
682 
683 	return (0);
684 }
685 
686 /*
687  * Close function is called at splaudio().
688  */
689 void
690 neo_close(void *addr)
691 {
692 	struct neo_softc *sc = addr;
693 
694 	neo_halt_output(sc);
695 	neo_halt_input(sc);
696 
697 	sc->pintr = 0;
698 	sc->rintr = 0;
699 }
700 
701 int
702 neo_query_encoding(void *addr, struct audio_encoding *fp)
703 {
704 
705 	switch (fp->index) {
706 	case 0:
707 		strcpy(fp->name, AudioEulinear);
708 		fp->encoding = AUDIO_ENCODING_ULINEAR;
709 		fp->precision = 8;
710 		fp->flags = 0;
711 		return (0);
712 	case 1:
713 		strcpy(fp->name, AudioEmulaw);
714 		fp->encoding = AUDIO_ENCODING_ULAW;
715 		fp->precision = 8;
716 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
717 		return (0);
718 	case 2:
719 		strcpy(fp->name, AudioEalaw);
720 		fp->encoding = AUDIO_ENCODING_ALAW;
721 		fp->precision = 8;
722 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
723 		return (0);
724 	case 3:
725 		strcpy(fp->name, AudioEslinear);
726 		fp->encoding = AUDIO_ENCODING_SLINEAR;
727 		fp->precision = 8;
728 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
729 		return (0);
730 	case 4:
731 		strcpy(fp->name, AudioEslinear_le);
732 		fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
733 		fp->precision = 16;
734 		fp->flags = 0;
735 		return (0);
736 	case 5:
737 		strcpy(fp->name, AudioEulinear_le);
738 		fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
739 		fp->precision = 16;
740 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
741 		return (0);
742 	case 6:
743 		strcpy(fp->name, AudioEslinear_be);
744 		fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
745 		fp->precision = 16;
746 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
747 		return (0);
748 	case 7:
749 		strcpy(fp->name, AudioEulinear_be);
750 		fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
751 		fp->precision = 16;
752 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
753 		return (0);
754 	default:
755 		return (EINVAL);
756 	}
757 }
758 
759 /* Todo: don't commit settings to card until we've verified all parameters */
760 int
761 neo_set_params(void *addr, int setmode, int usemode, struct audio_params *play,
762     struct audio_params *rec)
763 {
764 	struct neo_softc *sc = addr;
765 	u_int32_t base;
766 	u_int8_t x;
767 	int mode;
768 	struct audio_params *p;
769 
770 	for (mode = AUMODE_RECORD; mode != -1;
771 	     mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
772 		if ((setmode & mode) == 0)
773 			continue;
774 
775 		p = mode == AUMODE_PLAY ? play : rec;
776 
777 		if (p == NULL) continue;
778 
779 		for (x = 0; x < 8; x++) {
780 			if (p->sample_rate <
781 			    (samplerates[x] + samplerates[x + 1]) / 2)
782 				break;
783 		}
784 		if (x == 8)
785 			return (EINVAL);
786 
787 		p->sample_rate = samplerates[x];
788 		nm_loadcoeff(sc, mode, x);
789 
790 		x <<= 4;
791 		x &= NM_RATE_MASK;
792 		if (p->precision == 16)
793 			x |= NM_RATE_BITS_16;
794 		if (p->channels == 2)
795 			x |= NM_RATE_STEREO;
796 
797 		base = (mode == AUMODE_PLAY)?
798 		    NM_PLAYBACK_REG_OFFSET : NM_RECORD_REG_OFFSET;
799 		nm_wr_1(sc, base + NM_RATE_REG_OFFSET, x);
800 
801 		p->factor = 1;
802 		p->sw_code = 0;
803 		switch (p->encoding) {
804 		case AUDIO_ENCODING_SLINEAR_BE:
805 			if (p->precision == 16)
806 				p->sw_code = swap_bytes;
807 			else
808 				p->sw_code = change_sign8;
809 			break;
810 		case AUDIO_ENCODING_SLINEAR_LE:
811 			if (p->precision != 16)
812 				p->sw_code = change_sign8;
813 			break;
814 		case AUDIO_ENCODING_ULINEAR_BE:
815 			if (p->precision == 16) {
816 				if (mode == AUMODE_PLAY)
817 					p->sw_code =
818 					    swap_bytes_change_sign16_le;
819 				else
820 					p->sw_code =
821 					    change_sign16_swap_bytes_le;
822 			}
823 			break;
824 		case AUDIO_ENCODING_ULINEAR_LE:
825 			if (p->precision == 16)
826 				p->sw_code = change_sign16_le;
827 			break;
828 		case AUDIO_ENCODING_ULAW:
829 			if (mode == AUMODE_PLAY) {
830 				p->factor = 2;
831 				p->sw_code = mulaw_to_slinear16_le;
832 			} else
833 				p->sw_code = ulinear8_to_mulaw;
834 			break;
835 		case AUDIO_ENCODING_ALAW:
836 			if (mode == AUMODE_PLAY) {
837 				p->factor = 2;
838 				p->sw_code = alaw_to_slinear16_le;
839 			} else
840 				p->sw_code = ulinear8_to_alaw;
841 			break;
842 		default:
843 			return (EINVAL);
844 		}
845 	}
846 
847 
848 	return (0);
849 }
850 
851 int
852 neo_round_blocksize(void *addr, int blk)
853 {
854 
855 	return (NM_BUFFSIZE / 2);
856 }
857 
858 int
859 neo_trigger_output(void *addr, void *start, void *end, int blksize,
860     void (*intr)(void *), void *arg, struct audio_params *param)
861 {
862 	struct neo_softc *sc = addr;
863 	int ssz;
864 
865 	sc->pintr = intr;
866 	sc->parg = arg;
867 
868 	ssz = (param->precision * param->factor == 16) ? 2 : 1;
869 	if (param->channels == 2)
870 		ssz <<= 1;
871 
872 	sc->pbufsize = ((char*)end - (char *)start);
873 	sc->pblksize = blksize;
874 	sc->pwmark = blksize;
875 
876 	nm_wr_4(sc, NM_PBUFFER_START, sc->pbuf);
877 	nm_wr_4(sc, NM_PBUFFER_END, sc->pbuf + sc->pbufsize - ssz);
878 	nm_wr_4(sc, NM_PBUFFER_CURRP, sc->pbuf);
879 	nm_wr_4(sc, NM_PBUFFER_WMARK, sc->pbuf + sc->pwmark);
880 	nm_wr_1(sc, NM_PLAYBACK_ENABLE_REG, NM_PLAYBACK_FREERUN |
881 	    NM_PLAYBACK_ENABLE_FLAG);
882 	nm_wr_2(sc, NM_AUDIO_MUTE_REG, 0);
883 
884 	return (0);
885 }
886 
887 int
888 neo_trigger_input(void *addr, void *start, void *end, int blksize,
889     void (*intr)(void *), void *arg, struct audio_params *param)
890 {
891 	struct neo_softc *sc = addr;
892 	int ssz;
893 
894 	sc->rintr = intr;
895 	sc->rarg = arg;
896 
897 	ssz = (param->precision * param->factor == 16) ? 2 : 1;
898 	if (param->channels == 2)
899 		ssz <<= 1;
900 
901 	sc->rbufsize = ((char*)end - (char *)start);
902 	sc->rblksize = blksize;
903 	sc->rwmark = blksize;
904 
905 	nm_wr_4(sc, NM_RBUFFER_START, sc->rbuf);
906 	nm_wr_4(sc, NM_RBUFFER_END, sc->rbuf + sc->rbufsize);
907 	nm_wr_4(sc, NM_RBUFFER_CURRP, sc->rbuf);
908 	nm_wr_4(sc, NM_RBUFFER_WMARK, sc->rbuf + sc->rwmark);
909 	nm_wr_1(sc, NM_RECORD_ENABLE_REG, NM_RECORD_FREERUN |
910 	    NM_RECORD_ENABLE_FLAG);
911 
912 	return (0);
913 }
914 
915 int
916 neo_halt_output(void *addr)
917 {
918 	struct neo_softc *sc = (struct neo_softc *)addr;
919 
920 	nm_wr_1(sc, NM_PLAYBACK_ENABLE_REG, 0);
921 	nm_wr_2(sc, NM_AUDIO_MUTE_REG, NM_AUDIO_MUTE_BOTH);
922 
923 	return (0);
924 }
925 
926 int
927 neo_halt_input(void *addr)
928 {
929 	struct neo_softc *sc = (struct neo_softc *)addr;
930 
931 	nm_wr_1(sc, NM_RECORD_ENABLE_REG, 0);
932 
933 	return (0);
934 }
935 
936 int
937 neo_getdev(void *addr, struct audio_device *retp)
938 {
939 
940 	*retp = neo_device;
941 	return (0);
942 }
943 
944 int
945 neo_mixer_set_port(void *addr, mixer_ctrl_t *cp)
946 {
947 	struct neo_softc *sc = addr;
948 
949 	return ((sc->codec_if->vtbl->mixer_set_port)(sc->codec_if, cp));
950 }
951 
952 int
953 neo_mixer_get_port(void *addr, mixer_ctrl_t *cp)
954 {
955 	struct neo_softc *sc = addr;
956 
957 	return ((sc->codec_if->vtbl->mixer_get_port)(sc->codec_if, cp));
958 }
959 
960 int
961 neo_query_devinfo(void *addr, mixer_devinfo_t *dip)
962 {
963 	struct neo_softc *sc = addr;
964 
965 	return ((sc->codec_if->vtbl->query_devinfo)(sc->codec_if, dip));
966 }
967 
968 void *
969 neo_malloc(void *addr, int direction, size_t size, int pool, int flags)
970 {
971 	struct neo_softc *sc = addr;
972 	void *rv = NULL;
973 
974 	switch (direction) {
975 	case AUMODE_PLAY:
976 		if (sc->pbuf_allocated == 0) {
977 			rv = (void *) sc->pbuf_vaddr;
978 			sc->pbuf_allocated = 1;
979 		}
980 		break;
981 
982 	case AUMODE_RECORD:
983 		if (sc->rbuf_allocated == 0) {
984 			rv = (void *) sc->rbuf_vaddr;
985 			sc->rbuf_allocated = 1;
986 		}
987 		break;
988 	}
989 
990 	return (rv);
991 }
992 
993 void
994 neo_free(void *addr, void *ptr, int pool)
995 {
996 	struct neo_softc *sc = addr;
997 	vaddr_t v = (vaddr_t) ptr;
998 
999 	if (v == sc->pbuf_vaddr)
1000 		sc->pbuf_allocated = 0;
1001 	else if (v == sc->rbuf_vaddr)
1002 		sc->rbuf_allocated = 0;
1003 	else
1004 		printf("neo_free: bad address %p\n", ptr);
1005 }
1006 
1007 size_t
1008 neo_round_buffersize(void *addr, int direction, size_t size)
1009 {
1010 
1011 	return (NM_BUFFSIZE);
1012 }
1013 
1014 paddr_t
1015 neo_mappage(void *addr, void *mem, off_t off, int prot)
1016 {
1017 	struct neo_softc *sc = addr;
1018 	vaddr_t v = (vaddr_t) mem;
1019 	bus_addr_t pciaddr;
1020 
1021 	/* XXX Need new mapping code. */
1022 
1023 	if (v == sc->pbuf_vaddr)
1024 		pciaddr = sc->pbuf_pciaddr;
1025 	else if (v == sc->rbuf_vaddr)
1026 		pciaddr = sc->rbuf_pciaddr;
1027 	else
1028 		return (-1);
1029 
1030 #ifdef __i386__
1031 	return (i386_btop(pciaddr + off));
1032 #else
1033 	return (-1);
1034 #endif
1035 }
1036 
1037 int
1038 neo_get_props(void *addr)
1039 {
1040 
1041 	return (AUDIO_PROP_INDEPENDENT | AUDIO_PROP_MMAP |
1042                 AUDIO_PROP_FULLDUPLEX);
1043 }
1044