Home | History | Annotate | Line # | Download | only in pci
neo.c revision 1.53
      1 /*	$NetBSD: neo.c,v 1.53 2019/05/08 13:40:19 isaki Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1999 Cameron Grant <gandalf (at) 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.53 2019/05/08 13:40:19 isaki 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/audio_if.h>
     46 
     47 #include <dev/ic/ac97var.h>
     48 
     49 #include <dev/pci/pcidevs.h>
     50 #include <dev/pci/pcivar.h>
     51 #include <dev/pci/neoreg.h>
     52 #include <dev/pci/neo-coeff.h>
     53 
     54 /* -------------------------------------------------------------------- */
     55 /*
     56  * As of 04/13/00, public documentation on the Neomagic 256 is not available.
     57  * These comments were gleaned by looking at the driver carefully.
     58  *
     59  * The Neomagic 256 AV/ZX chips provide both video and audio capabilities
     60  * on one chip. About 2-6 megabytes of memory are associated with
     61  * the chip. Most of this goes to video frame buffers, but some is used for
     62  * audio buffering
     63  *
     64  * Unlike most PCI audio chips, the Neomagic chip does not rely on DMA.
     65  * Instead, the chip allows you to carve out two ring buffers out of its
     66  * memory. However you carve this and how much you can carve seems to be
     67  * voodoo. The algorithm is in nm_init.
     68  *
     69  * Most Neomagic audio chips use the AC-97 codec interface. However, there
     70  * seem to be a select few chips 256AV chips that do not support AC-97.
     71  * This driver does not support them but there are rumors that it
     72  * might work with wss isa drivers. This might require some playing around
     73  * with your BIOS.
     74  *
     75  * The Neomagic 256 AV/ZX have 2 PCI I/O region descriptors. Both of
     76  * them describe a memory region. The frame buffer is the first region
     77  * and the register set is the second region.
     78  *
     79  * The register manipulation logic is taken from the Linux driver,
     80  * which is in the public domain.
     81  *
     82  * The Neomagic is even nice enough to map the AC-97 codec registers into
     83  * the register space to allow direct manipulation. Watch out, accessing
     84  * AC-97 registers on the Neomagic requires great delicateness, otherwise
     85  * the thing will hang the PCI bus, rendering your system frozen.
     86  *
     87  * For one, it seems the Neomagic status register that reports AC-97
     88  * readiness should NOT be polled more often than once each 1ms.
     89  *
     90  * Also, writes to the AC-97 register space may take order 40us to
     91  * complete.
     92  *
     93  * Unlike many sound engines, the Neomagic does not support (as far as
     94  * we know :) the notion of interrupting every n bytes transferred,
     95  * unlike many DMA engines.  Instead, it allows you to specify one
     96  * location in each ring buffer (called the watermark). When the chip
     97  * passes that location while playing, it signals an interrupt.
     98  *
     99  * The ring buffer size is currently 16k. That is about 100ms of audio
    100  * at 44.1kHz/stero/16 bit. However, to keep the buffer full, interrupts
    101  * are generated more often than that, so 20-40 interrupts per second
    102  * should not be unexpected. Increasing BUFFSIZE should help minimize
    103  * of glitches due to drivers that spend to much time looping at high
    104  * privelege levels as well as the impact of badly written audio
    105  * interface clients.
    106  *
    107  * TO-DO list:
    108  *    Figure out interaction with video stuff (look at Xfree86 driver?)
    109  *
    110  *    Figure out how to shrink that huge table neo-coeff.h
    111  */
    112 
    113 #define	NM_BUFFSIZE	16384
    114 
    115 /* device private data */
    116 struct neo_softc {
    117 	device_t	dev;
    118 	kmutex_t	lock;
    119 	kmutex_t	intr_lock;
    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 	uint32_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 	uint32_t	ac97_base, ac97_status, ac97_busy;
    147 	uint32_t	buftop, pbuf, rbuf, cbuf, acbuf;
    148 	uint32_t	playint, recint, misc1int, misc2int;
    149 	uint32_t	irsz, badintr;
    150 
    151 	uint32_t	pbufsize;
    152 	uint32_t	rbufsize;
    153 
    154 	uint32_t	pblksize;
    155 	uint32_t	rblksize;
    156 
    157 	uint32_t	pwmark;
    158 	uint32_t	rwmark;
    159 
    160 	struct ac97_codec_if *codec_if;
    161 	struct ac97_host_if host_if;
    162 };
    163 
    164 /* -------------------------------------------------------------------- */
    165 
    166 /*
    167  * prototypes
    168  */
    169 
    170 static int	nm_waitcd(struct neo_softc *);
    171 static int	nm_loadcoeff(struct neo_softc *, int, int);
    172 static int	nm_init(struct neo_softc *);
    173 
    174 static int	neo_match(device_t, cfdata_t, void *);
    175 static void	neo_attach(device_t, device_t, void *);
    176 static int	neo_intr(void *);
    177 static int	neo_rate2index(u_int);
    178 
    179 static int	neo_query_format(void *, audio_format_query_t *);
    180 static int	neo_set_format(void *, int,
    181 			       const audio_params_t *, const audio_params_t *,
    182 			       audio_filter_reg_t *, audio_filter_reg_t *);
    183 static int	neo_round_blocksize(void *, int, int, const audio_params_t *);
    184 static int	neo_trigger_output(void *, void *, void *, int,
    185 				   void (*)(void *), void *,
    186 				   const audio_params_t *);
    187 static int	neo_trigger_input(void *, void *, void *, int,
    188 				  void (*)(void *), void *,
    189 				  const audio_params_t *);
    190 static int	neo_halt_output(void *);
    191 static int	neo_halt_input(void *);
    192 static int	neo_getdev(void *, struct audio_device *);
    193 static int	neo_mixer_set_port(void *, mixer_ctrl_t *);
    194 static int	neo_mixer_get_port(void *, mixer_ctrl_t *);
    195 static int	neo_attach_codec(void *, struct ac97_codec_if *);
    196 static int	neo_read_codec(void *, uint8_t, uint16_t *);
    197 static int	neo_write_codec(void *, uint8_t, uint16_t);
    198 static int     neo_reset_codec(void *);
    199 static enum ac97_host_flags neo_flags_codec(void *);
    200 static int	neo_query_devinfo(void *, mixer_devinfo_t *);
    201 static void *	neo_malloc(void *, int, size_t);
    202 static void	neo_free(void *, void *, size_t);
    203 static size_t	neo_round_buffersize(void *, int, size_t);
    204 static int	neo_get_props(void *);
    205 static void	neo_get_locks(void *, kmutex_t **, kmutex_t **);
    206 
    207 CFATTACH_DECL_NEW(neo, sizeof(struct neo_softc),
    208     neo_match, neo_attach, NULL, NULL);
    209 
    210 static struct audio_device neo_device = {
    211 	"NeoMagic 256",
    212 	"",
    213 	"neo"
    214 };
    215 
    216 /*
    217  * The frequency list in this format is also referred from neo_rate2index().
    218  * So don't rearrange or delete entries.
    219  */
    220 static const struct audio_format neo_formats[] = {
    221 	{
    222 		.mode		= AUMODE_PLAY | AUMODE_RECORD,
    223 		.encoding	= AUDIO_ENCODING_SLINEAR_LE,
    224 		.validbits	= 16,
    225 		.precision	= 16,
    226 		.channels	= 2,
    227 		.channel_mask	= AUFMT_STEREO,
    228 		.frequency_type	= 8,
    229 		.frequency	=
    230 		    { 8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000 },
    231 	},
    232 };
    233 #define NEO_NFORMATS	__arraycount(neo_formats)
    234 
    235 /* -------------------------------------------------------------------- */
    236 
    237 static const struct audio_hw_if neo_hw_if = {
    238 	.query_format		= neo_query_format,
    239 	.set_format		= neo_set_format,
    240 	.round_blocksize	= neo_round_blocksize,
    241 	.halt_output		= neo_halt_output,
    242 	.halt_input		= neo_halt_input,
    243 	.getdev			= neo_getdev,
    244 	.set_port		= neo_mixer_set_port,
    245 	.get_port		= neo_mixer_get_port,
    246 	.query_devinfo		= neo_query_devinfo,
    247 	.allocm			= neo_malloc,
    248 	.freem			= neo_free,
    249 	.round_buffersize	= neo_round_buffersize,
    250 	.get_props		= neo_get_props,
    251 	.trigger_output		= neo_trigger_output,
    252 	.trigger_input		= neo_trigger_input,
    253 	.get_locks		= neo_get_locks,
    254 };
    255 
    256 /* -------------------------------------------------------------------- */
    257 
    258 #define	nm_rd_1(sc, regno)						\
    259 	bus_space_read_1((sc)->regiot, (sc)->regioh, (regno))
    260 
    261 #define	nm_rd_2(sc, regno)						\
    262 	bus_space_read_2((sc)->regiot, (sc)->regioh, (regno))
    263 
    264 #define	nm_rd_4(sc, regno)						\
    265 	bus_space_read_4((sc)->regiot, (sc)->regioh, (regno))
    266 
    267 #define	nm_wr_1(sc, regno, val)						\
    268 	bus_space_write_1((sc)->regiot, (sc)->regioh, (regno), (val))
    269 
    270 #define	nm_wr_2(sc, regno, val)						\
    271 	bus_space_write_2((sc)->regiot, (sc)->regioh, (regno), (val))
    272 
    273 #define	nm_wr_4(sc, regno, val)						\
    274 	bus_space_write_4((sc)->regiot, (sc)->regioh, (regno), (val))
    275 
    276 #define	nm_rdbuf_4(sc, regno)						\
    277 	bus_space_read_4((sc)->bufiot, (sc)->bufioh, (regno))
    278 
    279 #define	nm_wrbuf_1(sc, regno, val)					\
    280 	bus_space_write_1((sc)->bufiot, (sc)->bufioh, (regno), (val))
    281 
    282 /* ac97 codec */
    283 static int
    284 nm_waitcd(struct neo_softc *sc)
    285 {
    286 	int cnt;
    287 	int fail;
    288 
    289 	cnt  = 10;
    290 	fail = 1;
    291 	while (cnt-- > 0) {
    292 		if (nm_rd_2(sc, sc->ac97_status) & sc->ac97_busy)
    293 			DELAY(100);
    294 		else {
    295 			fail = 0;
    296 			break;
    297 		}
    298 	}
    299 	return fail;
    300 }
    301 
    302 static void
    303 nm_ackint(struct neo_softc *sc, uint32_t num)
    304 {
    305 
    306 	switch (sc->type) {
    307 	case PCI_PRODUCT_NEOMAGIC_NMMM256AV_AU:
    308 		nm_wr_2(sc, NM_INT_REG, num << 1);
    309 		break;
    310 
    311 	case PCI_PRODUCT_NEOMAGIC_NMMM256ZX_AU:
    312 		nm_wr_4(sc, NM_INT_REG, num);
    313 		break;
    314 	}
    315 }
    316 
    317 static int
    318 nm_loadcoeff(struct neo_softc *sc, int dir, int num)
    319 {
    320 	int ofs, sz, i;
    321 	uint32_t addr;
    322 
    323 	addr = (dir == AUMODE_PLAY)? 0x01c : 0x21c;
    324 	if (dir == AUMODE_RECORD)
    325 		num += 8;
    326 	sz = coefficientSizes[num];
    327 	ofs = 0;
    328 	while (num-- > 0)
    329 		ofs+= coefficientSizes[num];
    330 	for (i = 0; i < sz; i++)
    331 		nm_wrbuf_1(sc, sc->cbuf + i, coefficients[ofs + i]);
    332 	nm_wr_4(sc, addr, sc->cbuf);
    333 	if (dir == AUMODE_PLAY)
    334 		sz--;
    335 	nm_wr_4(sc, addr + 4, sc->cbuf + sz);
    336 	return 0;
    337 }
    338 
    339 /* The interrupt handler */
    340 static int
    341 neo_intr(void *p)
    342 {
    343 	struct neo_softc *sc;
    344 	int status, x;
    345 	int rv;
    346 
    347 	sc = (struct neo_softc *)p;
    348 	mutex_spin_enter(&sc->intr_lock);
    349 
    350 	rv = 0;
    351 	status = (sc->irsz == 2) ?
    352 	    nm_rd_2(sc, NM_INT_REG) :
    353 	    nm_rd_4(sc, NM_INT_REG);
    354 
    355 	if (status & sc->playint) {
    356 		status &= ~sc->playint;
    357 
    358 		sc->pwmark += sc->pblksize;
    359 		sc->pwmark %= sc->pbufsize;
    360 
    361 		nm_wr_4(sc, NM_PBUFFER_WMARK, sc->pbuf + sc->pwmark);
    362 
    363 		nm_ackint(sc, sc->playint);
    364 
    365 		if (sc->pintr)
    366 			(*sc->pintr)(sc->parg);
    367 
    368 		rv = 1;
    369 	}
    370 	if (status & sc->recint) {
    371 		status &= ~sc->recint;
    372 
    373 		sc->rwmark += sc->rblksize;
    374 		sc->rwmark %= sc->rbufsize;
    375 		nm_wr_4(sc, NM_RBUFFER_WMARK, sc->rbuf + sc->rwmark);
    376 		nm_ackint(sc, sc->recint);
    377 		if (sc->rintr)
    378 			(*sc->rintr)(sc->rarg);
    379 
    380 		rv = 1;
    381 	}
    382 	if (status & sc->misc1int) {
    383 		status &= ~sc->misc1int;
    384 		nm_ackint(sc, sc->misc1int);
    385 		x = nm_rd_1(sc, 0x400);
    386 		nm_wr_1(sc, 0x400, x | 2);
    387 		printf("%s: misc int 1\n", device_xname(sc->dev));
    388 		rv = 1;
    389 	}
    390 	if (status & sc->misc2int) {
    391 		status &= ~sc->misc2int;
    392 		nm_ackint(sc, sc->misc2int);
    393 		x = nm_rd_1(sc, 0x400);
    394 		nm_wr_1(sc, 0x400, x & ~2);
    395 		printf("%s: misc int 2\n", device_xname(sc->dev));
    396 		rv = 1;
    397 	}
    398 	if (status) {
    399 		status &= ~sc->misc2int;
    400 		nm_ackint(sc, sc->misc2int);
    401 		printf("%s: unknown int\n", device_xname(sc->dev));
    402 		rv = 1;
    403 	}
    404 
    405 	mutex_spin_exit(&sc->intr_lock);
    406 	return rv;
    407 }
    408 
    409 /* -------------------------------------------------------------------- */
    410 
    411 /*
    412  * Probe and attach the card
    413  */
    414 
    415 static int
    416 nm_init(struct neo_softc *sc)
    417 {
    418 	uint32_t ofs, i;
    419 
    420 	switch (sc->type) {
    421 	case PCI_PRODUCT_NEOMAGIC_NMMM256AV_AU:
    422 		sc->ac97_base = NM_MIXER_OFFSET;
    423 		sc->ac97_status = NM_MIXER_STATUS_OFFSET;
    424 		sc->ac97_busy = NM_MIXER_READY_MASK;
    425 
    426 		sc->buftop = 2560 * 1024;
    427 
    428 		sc->irsz = 2;
    429 		sc->playint = NM_PLAYBACK_INT;
    430 		sc->recint = NM_RECORD_INT;
    431 		sc->misc1int = NM_MISC_INT_1;
    432 		sc->misc2int = NM_MISC_INT_2;
    433 		break;
    434 
    435 	case PCI_PRODUCT_NEOMAGIC_NMMM256ZX_AU:
    436 		sc->ac97_base = NM_MIXER_OFFSET;
    437 		sc->ac97_status = NM2_MIXER_STATUS_OFFSET;
    438 		sc->ac97_busy = NM2_MIXER_READY_MASK;
    439 
    440 		sc->buftop = (nm_rd_2(sc, 0xa0b) ? 6144 : 4096) * 1024;
    441 
    442 		sc->irsz = 4;
    443 		sc->playint = NM2_PLAYBACK_INT;
    444 		sc->recint = NM2_RECORD_INT;
    445 		sc->misc1int = NM2_MISC_INT_1;
    446 		sc->misc2int = NM2_MISC_INT_2;
    447 		break;
    448 #ifdef DIAGNOSTIC
    449 	default:
    450 		panic("nm_init: impossible");
    451 #endif
    452 	}
    453 
    454 	sc->badintr = 0;
    455 	ofs = sc->buftop - 0x0400;
    456 	sc->buftop -= 0x1400;
    457 
    458 	if ((nm_rdbuf_4(sc, ofs) & NM_SIG_MASK) == NM_SIGNATURE) {
    459 		i = nm_rdbuf_4(sc, ofs + 4);
    460 		if (i != 0 && i != 0xffffffff)
    461 			sc->buftop = i;
    462 	}
    463 
    464 	sc->cbuf = sc->buftop - NM_MAX_COEFFICIENT;
    465 	sc->rbuf = sc->cbuf - NM_BUFFSIZE;
    466 	sc->pbuf = sc->rbuf - NM_BUFFSIZE;
    467 	sc->acbuf = sc->pbuf - (NM_TOTAL_COEFF_COUNT * 4);
    468 
    469 	sc->buf_vaddr = (vaddr_t) bus_space_vaddr(sc->bufiot, sc->bufioh);
    470 	sc->rbuf_vaddr = sc->buf_vaddr + sc->rbuf;
    471 	sc->pbuf_vaddr = sc->buf_vaddr + sc->pbuf;
    472 
    473 	sc->rbuf_pciaddr = sc->buf_pciaddr + sc->rbuf;
    474 	sc->pbuf_pciaddr = sc->buf_pciaddr + sc->pbuf;
    475 
    476 	nm_wr_1(sc, 0, 0x11);
    477 	nm_wr_1(sc, NM_RECORD_ENABLE_REG, 0);
    478 	nm_wr_2(sc, 0x214, 0);
    479 
    480 	return 0;
    481 }
    482 
    483 static int
    484 neo_match(device_t parent, cfdata_t match, void *aux)
    485 {
    486 	struct pci_attach_args *pa;
    487 	pcireg_t subdev;
    488 
    489 	pa = aux;
    490 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_NEOMAGIC)
    491 		return 0;
    492 
    493 	subdev = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
    494 
    495 	switch (PCI_PRODUCT(pa->pa_id)) {
    496 	case PCI_PRODUCT_NEOMAGIC_NMMM256AV_AU:
    497 		/*
    498 		 * We have to weed-out the non-AC'97 versions of
    499 		 * the chip (i.e. the ones that are known to work
    500 		 * in WSS emulation mode), as they won't work with
    501 		 * this driver.
    502 		 */
    503 		switch (PCI_VENDOR(subdev)) {
    504 		case PCI_VENDOR_DELL:
    505 			switch (PCI_PRODUCT(subdev)) {
    506 			case 0x008f:
    507 				return 0;
    508 			}
    509 			break;
    510 
    511 		case PCI_VENDOR_HP:
    512 			switch (PCI_PRODUCT(subdev)) {
    513 			case 0x0007:
    514 				return 0;
    515 			}
    516 			break;
    517 
    518 		case PCI_VENDOR_IBM:
    519 			switch (PCI_PRODUCT(subdev)) {
    520 			case 0x00dd:
    521 				return 0;
    522 			}
    523 			break;
    524 		}
    525 		return 1;
    526 
    527 	case PCI_PRODUCT_NEOMAGIC_NMMM256ZX_AU:
    528 		return 1;
    529 	}
    530 
    531 	return 0;
    532 }
    533 
    534 static bool
    535 neo_resume(device_t dv, const pmf_qual_t *qual)
    536 {
    537 	struct neo_softc *sc = device_private(dv);
    538 
    539 	mutex_enter(&sc->lock);
    540 	mutex_spin_enter(&sc->intr_lock);
    541 	nm_init(sc);
    542 	mutex_spin_exit(&sc->intr_lock);
    543 	sc->codec_if->vtbl->restore_ports(sc->codec_if);
    544 	mutex_exit(&sc->lock);
    545 
    546 	return true;
    547 }
    548 
    549 static void
    550 neo_attach(device_t parent, device_t self, void *aux)
    551 {
    552 	struct neo_softc *sc;
    553 	struct pci_attach_args *pa;
    554 	pci_chipset_tag_t pc;
    555 	char const *intrstr;
    556 	pci_intr_handle_t ih;
    557 	pcireg_t csr;
    558 	int error;
    559 	char intrbuf[PCI_INTRSTR_LEN];
    560 
    561 	sc = device_private(self);
    562 	pa = aux;
    563 	pc = pa->pa_pc;
    564 
    565 	sc->type = PCI_PRODUCT(pa->pa_id);
    566 
    567 	printf(": NeoMagic 256%s audio\n",
    568 	    sc->type == PCI_PRODUCT_NEOMAGIC_NMMM256AV_AU ? "AV" : "ZX");
    569 
    570 	/* Map I/O register */
    571 	if (pci_mapreg_map(pa, PCI_MAPREG_START, PCI_MAPREG_TYPE_MEM, 0,
    572 			   &sc->bufiot, &sc->bufioh, &sc->buf_pciaddr, NULL)) {
    573 		aprint_error_dev(self, "can't map buffer\n");
    574 		return;
    575 	}
    576 
    577 	if (pci_mapreg_map(pa, PCI_MAPREG_START + 4, PCI_MAPREG_TYPE_MEM,
    578 	    BUS_SPACE_MAP_LINEAR, &sc->regiot, &sc->regioh, NULL, NULL)) {
    579 		aprint_error_dev(self, "can't map registers\n");
    580 		return;
    581 	}
    582 
    583 	/* Map and establish the interrupt. */
    584 	if (pci_intr_map(pa, &ih)) {
    585 		aprint_error_dev(self, "couldn't map interrupt\n");
    586 		return;
    587 	}
    588 
    589 	mutex_init(&sc->lock, MUTEX_DEFAULT, IPL_NONE);
    590 	mutex_init(&sc->intr_lock, MUTEX_DEFAULT, IPL_AUDIO);
    591 
    592 	intrstr = pci_intr_string(pc, ih, intrbuf, sizeof(intrbuf));
    593 	sc->ih = pci_intr_establish_xname(pc, ih, IPL_AUDIO, neo_intr, sc,
    594 	    device_xname(self));
    595 
    596 	if (sc->ih == NULL) {
    597 		aprint_error_dev(self, "couldn't establish interrupt");
    598 		if (intrstr != NULL)
    599 			aprint_error(" at %s", intrstr);
    600 		aprint_error("\n");
    601 		mutex_destroy(&sc->lock);
    602 		mutex_destroy(&sc->intr_lock);
    603 		return;
    604 	}
    605 	aprint_normal_dev(self, "interrupting at %s\n", intrstr);
    606 
    607 	mutex_spin_enter(&sc->intr_lock);
    608 	error = nm_init(sc);
    609 	mutex_spin_exit(&sc->intr_lock);
    610 	if (error != 0) {
    611 		mutex_destroy(&sc->lock);
    612 		mutex_destroy(&sc->intr_lock);
    613 		return;
    614 	}
    615 
    616 	/* Enable the device. */
    617 	csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    618 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    619 		       csr | PCI_COMMAND_MASTER_ENABLE);
    620 
    621 	sc->host_if.arg = sc;
    622 	sc->host_if.attach = neo_attach_codec;
    623 	sc->host_if.read   = neo_read_codec;
    624 	sc->host_if.write  = neo_write_codec;
    625 	sc->host_if.reset  = neo_reset_codec;
    626 	sc->host_if.flags  = neo_flags_codec;
    627 
    628 	if (ac97_attach(&sc->host_if, self, &sc->lock) != 0) {
    629 		mutex_destroy(&sc->lock);
    630 		mutex_destroy(&sc->intr_lock);
    631 		return;
    632 	}
    633 
    634 	if (!pmf_device_register(self, NULL, neo_resume))
    635 		aprint_error_dev(self, "couldn't establish power handler\n");
    636 
    637 	audio_attach_mi(&neo_hw_if, sc, self);
    638 }
    639 
    640 static int
    641 neo_read_codec(void *v, uint8_t a, uint16_t *d)
    642 {
    643 	struct neo_softc *sc;
    644 
    645 	sc = v;
    646 	if (!nm_waitcd(sc)) {
    647 		*d = nm_rd_2(sc, sc->ac97_base + a);
    648 		DELAY(1000);
    649 		return 0;
    650 	}
    651 
    652 	return ENXIO;
    653 }
    654 
    655 
    656 static int
    657 neo_write_codec(void *v, u_int8_t a, u_int16_t d)
    658 {
    659 	struct neo_softc *sc;
    660 	int cnt;
    661 
    662 	sc = v;
    663 	cnt = 3;
    664 	if (!nm_waitcd(sc)) {
    665 		while (cnt-- > 0) {
    666 			nm_wr_2(sc, sc->ac97_base + a, d);
    667 			if (!nm_waitcd(sc)) {
    668 				DELAY(1000);
    669 				return 0;
    670 			}
    671 		}
    672 	}
    673 
    674 	return ENXIO;
    675 }
    676 
    677 static int
    678 neo_attach_codec(void *v, struct ac97_codec_if *codec_if)
    679 {
    680 	struct neo_softc *sc;
    681 
    682 	sc = v;
    683 	sc->codec_if = codec_if;
    684 	return 0;
    685 }
    686 
    687 static int
    688 neo_reset_codec(void *v)
    689 {
    690 	struct neo_softc *sc;
    691 
    692 	sc = v;
    693 	nm_wr_1(sc, 0x6c0, 0x01);
    694 	nm_wr_1(sc, 0x6cc, 0x87);
    695 	nm_wr_1(sc, 0x6cc, 0x80);
    696 	nm_wr_1(sc, 0x6cc, 0x00);
    697 	return 0;
    698 }
    699 
    700 static enum ac97_host_flags
    701 neo_flags_codec(void *v)
    702 {
    703 
    704 	return AC97_HOST_DONT_READ;
    705 }
    706 
    707 static int
    708 neo_query_format(void *addr, audio_format_query_t *afp)
    709 {
    710 
    711 	return audio_query_format(neo_formats, NEO_NFORMATS, afp);
    712 }
    713 
    714 /* Return index number of sample_rate */
    715 static int
    716 neo_rate2index(u_int sample_rate)
    717 {
    718 	int i;
    719 
    720 	for (i = 0; i < neo_formats[0].frequency_type; i++) {
    721 		if (sample_rate == neo_formats[0].frequency[i])
    722 			return i;
    723 	}
    724 
    725 	/* NOTREACHED */
    726 	panic("neo_formats.frequency mismatch?");
    727 }
    728 
    729 /* Todo: don't commit settings to card until we've verified all parameters */
    730 static int
    731 neo_set_format(void *addr, int setmode,
    732     const audio_params_t *play, const audio_params_t *rec,
    733     audio_filter_reg_t *pfil, audio_filter_reg_t *rfil)
    734 {
    735 	struct neo_softc *sc;
    736 	const audio_params_t *p;
    737 	uint32_t base;
    738 	uint8_t x;
    739 	int mode;
    740 
    741 	sc = addr;
    742 	for (mode = AUMODE_RECORD; mode != -1;
    743 	     mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
    744 		if ((setmode & mode) == 0)
    745 			continue;
    746 
    747 		p = mode == AUMODE_PLAY ? play : rec;
    748 
    749 		x = neo_rate2index(p->sample_rate);
    750 		nm_loadcoeff(sc, mode, x);
    751 
    752 		x <<= 4;
    753 		x &= NM_RATE_MASK;
    754 		x |= NM_RATE_BITS_16;
    755 		x |= NM_RATE_STEREO;
    756 
    757 		base = (mode == AUMODE_PLAY)?
    758 		    NM_PLAYBACK_REG_OFFSET : NM_RECORD_REG_OFFSET;
    759 		nm_wr_1(sc, base + NM_RATE_REG_OFFSET, x);
    760 	}
    761 
    762 	return 0;
    763 }
    764 
    765 static int
    766 neo_round_blocksize(void *addr, int blk, int mode,
    767     const audio_params_t *param)
    768 {
    769 
    770 	/* The number of blocks must be 3 or greater. */
    771 	return NM_BUFFSIZE / 4;
    772 }
    773 
    774 static int
    775 neo_trigger_output(void *addr, void *start, void *end, int blksize,
    776     void (*intr)(void *), void *arg, const audio_params_t *param)
    777 {
    778 	struct neo_softc *sc;
    779 	int ssz;
    780 
    781 	sc = addr;
    782 	sc->pintr = intr;
    783 	sc->parg = arg;
    784 
    785 	ssz = (param->precision == 16) ? 2 : 1;
    786 	if (param->channels == 2)
    787 		ssz <<= 1;
    788 
    789 	sc->pbufsize = ((char*)end - (char *)start);
    790 	sc->pblksize = blksize;
    791 	sc->pwmark = blksize;
    792 
    793 	nm_wr_4(sc, NM_PBUFFER_START, sc->pbuf);
    794 	nm_wr_4(sc, NM_PBUFFER_END, sc->pbuf + sc->pbufsize - ssz);
    795 	nm_wr_4(sc, NM_PBUFFER_CURRP, sc->pbuf);
    796 	nm_wr_4(sc, NM_PBUFFER_WMARK, sc->pbuf + sc->pwmark);
    797 	nm_wr_1(sc, NM_PLAYBACK_ENABLE_REG, NM_PLAYBACK_FREERUN |
    798 	    NM_PLAYBACK_ENABLE_FLAG);
    799 	nm_wr_2(sc, NM_AUDIO_MUTE_REG, 0);
    800 
    801 	return 0;
    802 }
    803 
    804 static int
    805 neo_trigger_input(void *addr, void *start, void *end, int blksize,
    806     void (*intr)(void *), void *arg, const audio_params_t *param)
    807 {
    808 	struct neo_softc *sc;
    809 	int ssz;
    810 
    811 	sc = addr;
    812 	sc->rintr = intr;
    813 	sc->rarg = arg;
    814 
    815 	ssz = (param->precision == 16) ? 2 : 1;
    816 	if (param->channels == 2)
    817 		ssz <<= 1;
    818 
    819 	sc->rbufsize = ((char*)end - (char *)start);
    820 	sc->rblksize = blksize;
    821 	sc->rwmark = blksize;
    822 
    823 	/*
    824 	 * XXX Doesn't it need to subtract ssz from BUFFER_END like
    825 	 * trigger_output()?
    826 	 */
    827 	nm_wr_4(sc, NM_RBUFFER_START, sc->rbuf);
    828 	nm_wr_4(sc, NM_RBUFFER_END, sc->rbuf + sc->rbufsize);
    829 	nm_wr_4(sc, NM_RBUFFER_CURRP, sc->rbuf);
    830 	nm_wr_4(sc, NM_RBUFFER_WMARK, sc->rbuf + sc->rwmark);
    831 	nm_wr_1(sc, NM_RECORD_ENABLE_REG, NM_RECORD_FREERUN |
    832 	    NM_RECORD_ENABLE_FLAG);
    833 
    834 	return 0;
    835 }
    836 
    837 static int
    838 neo_halt_output(void *addr)
    839 {
    840 	struct neo_softc *sc;
    841 
    842 	sc = (struct neo_softc *)addr;
    843 	nm_wr_1(sc, NM_PLAYBACK_ENABLE_REG, 0);
    844 	nm_wr_2(sc, NM_AUDIO_MUTE_REG, NM_AUDIO_MUTE_BOTH);
    845 	sc->pintr = 0;
    846 
    847 	return 0;
    848 }
    849 
    850 static int
    851 neo_halt_input(void *addr)
    852 {
    853 	struct neo_softc *sc;
    854 
    855 	sc = (struct neo_softc *)addr;
    856 	nm_wr_1(sc, NM_RECORD_ENABLE_REG, 0);
    857 	sc->rintr = 0;
    858 
    859 	return 0;
    860 }
    861 
    862 static int
    863 neo_getdev(void *addr, struct audio_device *retp)
    864 {
    865 
    866 	*retp = neo_device;
    867 	return 0;
    868 }
    869 
    870 static int
    871 neo_mixer_set_port(void *addr, mixer_ctrl_t *cp)
    872 {
    873 	struct neo_softc *sc;
    874 
    875 	sc = addr;
    876 	return sc->codec_if->vtbl->mixer_set_port(sc->codec_if, cp);
    877 }
    878 
    879 static int
    880 neo_mixer_get_port(void *addr, mixer_ctrl_t *cp)
    881 {
    882 	struct neo_softc *sc;
    883 
    884 	sc = addr;
    885 	return sc->codec_if->vtbl->mixer_get_port(sc->codec_if, cp);
    886 }
    887 
    888 static int
    889 neo_query_devinfo(void *addr, mixer_devinfo_t *dip)
    890 {
    891 	struct neo_softc *sc;
    892 
    893 	sc = addr;
    894 	return sc->codec_if->vtbl->query_devinfo(sc->codec_if, dip);
    895 }
    896 
    897 static void *
    898 neo_malloc(void *addr, int direction, size_t size)
    899 {
    900 	struct neo_softc *sc;
    901 	void *rv;
    902 
    903 	sc = addr;
    904 	rv = NULL;
    905 	switch (direction) {
    906 	case AUMODE_PLAY:
    907 		if (sc->pbuf_allocated == 0) {
    908 			rv = (void *) sc->pbuf_vaddr;
    909 			sc->pbuf_allocated = 1;
    910 		}
    911 		break;
    912 
    913 	case AUMODE_RECORD:
    914 		if (sc->rbuf_allocated == 0) {
    915 			rv = (void *) sc->rbuf_vaddr;
    916 			sc->rbuf_allocated = 1;
    917 		}
    918 		break;
    919 	}
    920 
    921 	return rv;
    922 }
    923 
    924 static void
    925 neo_free(void *addr, void *ptr, size_t size)
    926 {
    927 	struct neo_softc *sc;
    928 	vaddr_t v;
    929 
    930 	sc = addr;
    931 	v = (vaddr_t)ptr;
    932 	if (v == sc->pbuf_vaddr)
    933 		sc->pbuf_allocated = 0;
    934 	else if (v == sc->rbuf_vaddr)
    935 		sc->rbuf_allocated = 0;
    936 	else
    937 		printf("neo_free: bad address %p\n", ptr);
    938 }
    939 
    940 static size_t
    941 neo_round_buffersize(void *addr, int direction, size_t size)
    942 {
    943 
    944 	return NM_BUFFSIZE;
    945 }
    946 
    947 static int
    948 neo_get_props(void *addr)
    949 {
    950 
    951 	return AUDIO_PROP_INDEPENDENT | AUDIO_PROP_MMAP |
    952 	    AUDIO_PROP_FULLDUPLEX;
    953 }
    954 
    955 static void
    956 neo_get_locks(void *addr, kmutex_t **intr, kmutex_t **thread)
    957 {
    958 	struct neo_softc *sc;
    959 
    960 	sc = addr;
    961 	*intr = &sc->intr_lock;
    962 	*thread = &sc->lock;
    963 }
    964